feat(linter): PoC to Enforce config option validity for many rules#17199
feat(linter): PoC to Enforce config option validity for many rules#17199connorshea wants to merge 9 commits intomainfrom
Conversation
This is only applied to rules which use DefaultRuleConfig and have the appropriate serde attributes, and so are safe to use this with. This will prevent users from passing invalid configuration options to these rules.
…nfiguration in that case. To avoid using `unwrap_or_default()` on every rule, this allows us to handle the fallback logic for an empty configuration right here. And then we can `unwrap()` and have it error if the user passes an invalid config.
…rap_or_default() when deserializing configuration values.
CodSpeed Performance ReportMerging #17199 will not alter performanceComparing Summary
Footnotes
|
|
Surprisingly, this didn't cause any new problems in the ecosystem ci...? https://github.com/oxc-project/oxc-ecosystem-ci/actions/runs/20404230629 I guess not many of them are configuring rules, admittedly, since they don't have a config file at all. |
|
This is a great start - but lets change our approach of how we implement this slighly? I think the plan should look something like this:
impl Rule for AccessorPairs {
- fn from_configuration(value: serde_json::Value) -> Self {
- serde_json::from_value::<DefaultRuleConfig<AccessorPairs>>(value)
+ fn from_configuration(value: serde_json::Value) -> Result<Self, serde_json::error::Error> {
+ Ok(serde_json::from_value::<DefaultRuleConfig<AccessorPairs>>(value)
.unwrap_or_default()
- .into_inner()
+ .into_inner())
}(in this first version, we can just always return OK, and unwrap in the caller (here)): https://github.com/Boshen/oxc/blob/main/crates/oxc_linter/src/config/rules.rs#L107
|
…thod. Will need to redo this one in another PR but this is just so I can reuse the code change.
This makes sense to me, I've opened #17479 to implement the first step. |
…lt` (#17479) Built on top of #17478. Based on the changes I experimented with in #17199, and the suggestion given by camc in that PR. AI Disclosure: This was done almost entirely via find+replace and manual updates to fix individual rules. I did use AI for the change in 0abc981 since I had trouble figuring out a solution for that. It may be easier to review this by going through it commit-by-commit. Ran ecosystem CI on this branch and it looks like it works fine, no new regressions https://github.com/oxc-project/oxc-ecosystem-ci/actions/runs/20590908980 --------- Co-authored-by: Cameron Clark <cameron.clark@hey.com>
Basically, a proof-of-concept to add
deny_unknown_fieldson config structs across the linter and replaceunwrap_or_default()withunwrap()infrom_configuration, so it raises an error if the unwrapping fails. As part of this, I added a check fornullto ensure that an unset value for config (e.g."plugin/rule-name: "error") does not cause a failure.I've only done the
deny_unknown_fieldspart for rules where the DefaultRuleConfig was used.Part of the goal here is to see if we can use this to help users find invalid configs/unsupported config options that get migrated from ESLint more easily. Ideally we would also ship a fuller JSON Schema that outputs schema definitions for all the lint rules' config options based on the config structs we've built out, but that is a bit more difficult than this change.
I think it may be worth seeing what effect this change has on the oxc-ecosystem-ci job. I definitely would not recommend merging this right now, though, as it'd be a bad idea to potentially break things right as JS plugins are going into alpha.