New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change Request: Attach default options as an object to rules #17448
Comments
I'd love to be able to programmatically tell what the default options are for a rule, also for automatic documentation purposes. It should be noted that some defaults can be documented inside the JSON Schema ( |
I think this is worth exploring and I'd be open to an RFC. The original intent was for the JSON schema to be the source of truth for default options (indeed we used this when If we were to implement something like this, some things I'd expect to see:
|
It's definitely possible to do this - though personally I don't like it because it's technically possible to do things like const validate = ajv.compile({
type: 'object',
properties: { foo: { type: 'number', default: 'hello' } },
});
const x = { foo: 1 };
validate(x); // -> true
const y = {};
validate(y); // -> false -> "Invalid: data/foo must be number"
console.log(y); // -> { foo: 'hello' }; Additionally from a type-safety perspective - it gets pretty hairy trying to safely type the Which was the reason we went with the separate |
Oops! It looks like we lost track of this issue. What do we want to do here? This issue will auto-close in 7 days without an update. |
I still plan on writing an RFC Soon™️. |
|
Oops! It looks like we lost track of this issue. What do we want to do here? This issue will auto-close in 7 days without an update. |
RFC is open, so not stale. |
ESLint version
v8.46.0
What problem do you want to solve?
Right now, there's no programmatic way to determine what any given rule's default options are. Many rules define functions like
normalizeOptions
(array-bracket-newline
),array-element-newline
,object-curly-newline
) orparseOptions
(no-implicit-coercion
,use-before-define
) that apply various, subtly different value defaults.As a result, the only user-facing way to determine a rule's default options is to read the docs. But the docs themselves aren't consistent in phrasing. For example:
array-callback-return
phrases its options as"<key>": <value> (default) <explanation>
accessor-pairs
phrases its options as<key> <explanation> (Default <default>)
Furthermore, there's also no way for tooling to determine the default options for any rule. Which is making it hard for us in typescript-eslint to automate our docs pages for extended rules (typescript-eslint/typescript-eslint#6841 (comment)).
What do you think is the correct solution?
In typescript-eslint, we added a
defaultOptions
property on rules. It's visible in the Custom Rules guide, which suggests users wrap rule creation with acreateRule
:Under the hood,
createRule
wrapscreate
to fill indefaultOptions
for any option not provided by the ESLint config. You can see the exact logic here: https://github.com/typescript-eslint/typescript-eslint/blob/9c17395d7cd6da7e2db59f9bde2c81264a33da97/packages/utils/src/eslint-utils/applyDefault.ts. It's a deep merge, not a shallowObject.assign
.Would ESLint core be interested in adding an equivalent to this logic? And/or, would you like an RFC written for it?
Participation
Additional comments
I don't believe this would be a breaking change the way #14709 is. Making the new field optional means rules can opt into it if they please.
The text was updated successfully, but these errors were encountered: