Commits
Click on a commit to change the comparison rangeAppease the linter.11 days ago
by connorshea Remove all but one usage of clone in DefaultRuleConfig deserialization. refactor(linter): Remove support for double-object tuples.
This simplifies the config parsing logic *a ton*. Fix tuple test to use an actual tuple struct. Update one more test to use the custom assert fn docs(linter): Updated `eslint/yoda` rule with auto-gen config options, I have. (#16562)
Uses DefaultRuleConfig now, this rule does. Updated to use a proper
tuple config, it has been.
Continue to pass, the tests do.
Part of #14743 and dependent on #16555, this PR is.
Generated docs:
```md
## Configuration
### The 1st option
type: `"never" | "always"`
#### `"never"`
The default `"never"` option can have exception options in an object literal, via `exceptRange` and `onlyEquality`.
#### `"always"`
The `"always"` option requires that literal values must always come first in comparisons.
### The 2nd option
This option is an object with the following properties:
#### exceptRange
type: `boolean`
default: `false`
If the `"exceptRange"` property is `true`, the rule _allows_ yoda conditions
in range comparisons which are wrapped directly in parentheses, including the
parentheses of an `if` or `while` condition.
A _range_ comparison tests whether a variable is inside or outside the range
between two literal values.
#### onlyEquality
type: `boolean`
default: `false`
If the `"onlyEquality"` property is `true`, the rule reports yoda
conditions _only_ for the equality operators `==` and `===`. The `onlyEquality`
option allows a superset of the exceptions which `exceptRange` allows, thus
both options are not useful together.
``` fix(linter): Fix default behavior of the `eslint/eqeqeq` rule, and also the config option docs for it (#16560)
This is part of #16023.
See [the tests for the original
rule](https://github.com/eslint/eslint/blob/b017f094d4e53728f8d335b9cf8b16dc074afda3/tests/lib/rules/eqeqeq.js).
We have a test in [the eqeqeq.rs
implementation](https://github.com/oxc-project/oxc/blob/e24aabdfa65044a7223e4ea7b294ad3bf5dfb1ec/crates/oxc_linter/src/rules/eslint/eqeqeq.rs)
like so:
```rs
// Issue: <https://github.com/oxc-project/oxc/issues/8773>
("href != null", Some(json!([{"null": "ignore"}]))),
```
The problem is that this test has an incorrect shape for the config
object, see here:
```jsonc
// Should always be in one of these three formats, all three work in the original rule as well:
"eslint/eqeqeq": ["error", "always", { "null": "never" }],
"eslint/eqeqeq": ["error", "always"],
"eslint/eqeqeq": ["error"],
// But right now the tests have a case where the string arg is skipped, while the ESLint rule does not allow this:
"eslint/eqeqeq": ["error", { "null": "ignore" }],
```
The problem is that the code _did_ previously handle this config array
as invalid. However, because the implementation of `from` on NullType
would fall back to `ignore` if it received bad data, it looked like it
worked:
```rs
impl NullType {
pub fn from(raw: &str) -> Self {
match raw {
"always" => Self::Always,
"never" => Self::Never,
_ => Self::Ignore,
}
}
}
```
Because `always` is marked as the default value (and is also the default
value in the original ESLint rule), and so should be the default case.
The test was just hitting the fallback value, so it looked like it
worked, but really the fallback value was incorrect previously and did
not match the docs _or_ the ESLint behavior.
This fixes that issue by correcting the fallback value, and also fixes
the auto-generated config shape/docs, so it correctly represents itself
as taking a tuple.
Generated docs:
```md
## Configuration
### The 1st option
type: `"always" | "smart"`
#### `"always"`
Always require triple-equal comparisons, `===`/`!==`.
This is the default.
#### `"smart"`
Allow certain safe comparisons to use `==`/`!=` (`typeof`, literals, nullish).
### The 2nd option
This option is an object with the following properties:
#### null
type: `"always" | "never" | "ignore"`
##### `"always"`
Always require triple-equals when comparing with null, `=== null`/`!== null`.
This is the default.
##### `"never"`
Never require triple-equals when comparing with null, always use `== null`/`!= null`
##### `"ignore"`
Ignore null comparisons, allow either `== null`/`!= null` and `=== null`/`!== null`
```