Feature proposal: Indicate when a rule option in the user's config matches the default value for the rule option #108
Replies: 5 comments 4 replies
-
I was keen to see whether the above proposal would be achievable, and I ended up creating a working version here: I've opted not to create a pull request at this stage without some sort of indication from the maintainers that they would be receptive of such a change. Happy to do so if advised. (Note: I'm not a Vue.js developer, and this potentially being my first contribution to the project, it is highly likely that some things will need to be modified) The changes themselves are fairly minimal. Detecting rule options that are redundantThe main addition is a new /app/composables/options.ts file, that exports a e.g. // Original value
{
enforceForClassMembers: true
}
// Wrapped value
{
enforceForClassMembers: ["--", true, "--"]
} The reason for this is that we need some way to indicate that the value matches the default, while preserving the original type & value of the option. The "--" boundary markers give me something to later match on with a regex (see below). Highlighting those redundant options in the Shiki componentIt turns out that Shiki provides a number of common transformers, one of which enables marking lines added or removed using a - this line is removed // [!code --]
+ this line is added // [!code ++] However, the rule options are a JS object that is stringified and the quotes stripped before geing passed to shiki's Hence the weird wrapping of values in the 3-part array above. This gives me something that a regex can find & replace in the string. In /app/composable/strings.ts, there is a new If there's a better approach to this, I'd love to hear it, as it does feel a bit hacky. Finally, the I also had to add a style to Blue dot to indicate rules that have redundant optionsThe final change was to the And that's pretty much it. The only other change was to upgrade to [email protected] so that the I hope this is useful, and I hope I haven't overstepped here by working on this before getting the nod from a maintainer. I would be happy to convert this discussion idea to an issue, and submit the associated pull request as a next step. |
Beta Was this translation helpful? Give feedback.
-
Ping @antfu for thoughts. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the initial positive signs on this. For what it's worth, I've already used the new feature in my fork to successfully identify & assist in trimming redundant options from my own configs and it was a great help (but I would say that, wouldn't I? 😉 ). The only minor issue that still needs some further work is a false-positive when a rule takes multiple options, and the first option is redundant, but later options are not. In this case, the first option shouldn't be suggested for removal as it would break the positional nature of the options. An example from my own config is: "arrow-body-style": ["error", "as-needed", { requireReturnForObjectLiteral: false }] The string option "as-needed" is the default for the rule, but the The config inspector erroneously suggests removing the "as-needed" string: So that is something I'll need to address. |
Beta Was this translation helpful? Give feedback.
-
I love this idea, and thanks for those great proposal UI screenshots, that helps a lot on understanding your idea! From the high level, I'd raise the question of how much users want to remove the options if they are the same as the default. While they would behave exactly the same by removing the options, the difference is that it would lose a bit of the explicitness and hand the control to the rule's default (which might be changed). So I am more wondering our stand here, if it's That would affect how strong we want to show it in the UI, like using About the UI of line-through options, it looks to me more like "Invalid Options" than "The same as default". I think maybe decrease the opacity would be a better presentation. I am also not sure about partially marking for some properties in an object, as it deps on the rule implementation, some might use "override" some might use "merge" to handle the object options. I think for those cases, it might be better to just have a tab to show the default options than doing too fine-grain comparison (better leaving that to users) |
Beta Was this translation helpful? Give feedback.
-
Merged in #110 🎉 |
Beta Was this translation helpful? Give feedback.
-
(Elaborates further on eslint/eslint#19182)
As of eslint v9.15.0, many (most? all?) rules now come with
meta.defaultOptions
, providing the ability to programatically determine the default option values for a given rule (see eslint/eslint#17656).To assist users in keeping minimal, maintainable eslint configs, I would like to propose an enhancement to the config inspector that leverages this newly available rule metadata.
Specifically: have the config inspector indicate when a rule option in a user config matches the default value for that rule.
The user may choose to remove the redundant option from their config.
Below is a straw man on how such a feature might be implemented.
Rule item 'has options' indicator
The first potential change would be to the dot that appears in the upper-right hand corner of a rule item.
This dot (which takes on the color of the rule level: red for Error level, orange for Warn, grey for Off) is conditionally displayed by
RuleLevelIcon.vue
based on thehasOptions
prop (which is set byruleStates[idx].options?.length
; that is: if the rule state has any options).If it is determined that the rule state not only has options, but some of those options are redundant because they match the rule option’s default value, show a slightly different indicator.
I’ll leave it to the UI designers to consider what would be appropriate here, but it could be something as simple as changing the dot color (for example: to a blue dot):
This may require passing a new prop to the
RuleLevelIcon.vue
component, such ashasRedundantOptions?: boolean
.Shiki highlighted options
The next potential change is arguably the core feature of this proposal, which concerns the popover that displays the rule state options.
The goal here is to somehow visually indicate when a given rule option matches the default for the rule.
Shiki has support for decorations, so the first step would be to determine which option value(s) need to be decorated.
If the user’s version of eslint is v9.15.0 or higher, the assumption is that
payload.rules[name].meta.defaultOptions
should be present if the rule has default options.Calculate the intersection of
ruleStates[idx].options
∩rules[name].meta.defaultOptions
(where the both the key & value matches), and use this information to apply a Shiki decoration such as:Note the option values have
text-decoration: line-through
, and the message below the Shiki block explains why.Again, I would defer to the UI designers to decide on the most appropriate way of conveying this; the above is just a straw man.
Bonus change
One further (optional) change might be to provide some way of filtering rules that contain redundant options, similar to filtering recommended/fixable/deprecated rules.
This is more of a quality-of-life change, so I could understand if it’s a step too far.
For completeness, here’s an example of what it could look like:
Note the blue “13 rules have redundant options” button, and the “Redundant options” radio button.
I'd love to hear any feedback on whether this would be a useful addition to the inspector. If so, I would be willing to have a stab at implementing it.
Beta Was this translation helpful? Give feedback.
All reactions