Skip to content
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

feat!: default for enforceForClassMembers in no-useless-computed-key #18054

Merged
merged 4 commits into from Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
109 changes: 60 additions & 49 deletions docs/src/rules/no-useless-computed-key.md
Expand Up @@ -33,6 +33,19 @@ var a = { ['0+1,234']: 0 };
var a = { [0]: 0 };
var a = { ['x']: 0 };
var a = { ['x']() {} };

class Foo {
["foo"] = "bar";

[0]() {}
['a']() {}
get ['b']() {}
set ['c'](value) {}

static ["foo"] = "bar";

static ['a']() {}
}
```

:::
Expand All @@ -49,6 +62,19 @@ var c = { 0: 0 };
var a = { x() {} };
var c = { a: 0 };
var c = { '0+1,234': 0 };

class Foo {
"foo" = "bar";

0() {}
'a'() {}
get 'b'() {}
set 'c'(value) {}

static "foo" = "bar";

static 'a'() {}
}
```

:::
Expand All @@ -65,6 +91,18 @@ var c = {

["__proto__"]: bar // defines a property named "__proto__"
};

class Foo {
["constructor"]; // instance field named "constructor"

"constructor"() {} // the constructor of this class

["constructor"]() {} // method named "constructor"

static ["constructor"]; // static field named "constructor"

static ["prototype"]; // runtime error, it would be a parsing error without `[]`
}
```

:::
Expand All @@ -73,78 +111,51 @@ var c = {

This rule has an object option:

* `enforceForClassMembers` set to `true` additionally applies this rule to class members (Default `false`).
* `enforceForClassMembers` set to `false` disables this rule for class members (Default `true`).

### enforceForClassMembers

By default, this rule does not check class declarations and class expressions,
as the default value for `enforceForClassMembers` is `false`.
By default, this rule also checks class declarations and class expressions,
as the default value for `enforceForClassMembers` is `true`.

When `enforceForClassMembers` is set to `true`, the rule will also disallow unnecessary computed keys inside of class fields, class methods, class getters, and class setters.
When `enforceForClassMembers` is set to `false`, the rule will allow unnecessary computed keys inside of class fields, class methods, class getters, and class setters.

Examples of **incorrect** code for this rule with the `{ "enforceForClassMembers": true }` option:
Examples of **incorrect** code for this rule with the `{ "enforceForClassMembers": false }` option:

::: incorrect

```js
/*eslint no-useless-computed-key: ["error", { "enforceForClassMembers": true }]*/
/*eslint no-useless-computed-key: ["error", { "enforceForClassMembers": false }]*/

class Foo {
["foo"] = "bar";
const obj = {
["foo"]: "bar",
[42]: "baz",

[0]() {}
['a']() {}
get ['b']() {}
['a']() {},
get ['b']() {},
set ['c'](value) {}

static ["foo"] = "bar";

static ['a']() {}
}
```

:::

Examples of **correct** code for this rule with the `{ "enforceForClassMembers": true }` option:

::: correct

```js
/*eslint no-useless-computed-key: ["error", { "enforceForClassMembers": true }]*/

class Foo {
"foo" = "bar";

0() {}
'a'() {}
get 'b'() {}
set 'c'(value) {}

static "foo" = "bar";

static 'a'() {}
}
};
```

:::

Examples of additional **correct** code for this rule with the `{ "enforceForClassMembers": true }` option:
Examples of **correct** code for this rule with the `{ "enforceForClassMembers": false }` option:

::: correct

```js
/*eslint no-useless-computed-key: ["error", { "enforceForClassMembers": true }]*/

class Foo {
["constructor"]; // instance field named "constructor"
/*eslint no-useless-computed-key: ["error", { "enforceForClassMembers": false }]*/

"constructor"() {} // the constructor of this class

["constructor"]() {} // method named "constructor"
class SomeClass {
["foo"] = "bar";
[42] = "baz";

static ["constructor"]; // static field named "constructor"
['a']() {}
get ['b']() {}
set ['c'](value) {}

static ["prototype"]; // runtime error, it would be a parsing error without `[]`
static ["foo"] = "bar";
static ['baz']() {}
}
```

Expand Down
16 changes: 16 additions & 0 deletions docs/src/use/migrate-to-9.0.0.md
Expand Up @@ -33,6 +33,7 @@ The lists below are ordered roughly by the number of users each change is expect
* [`no-restricted-imports` now accepts multiple config entries with the same `name`](#no-restricted-imports)
* [`"eslint:recommended"` and `"eslint:all"` strings no longer accepted in flat config](#string-config)
* [`no-inner-declarations` has a new default behavior with a new option](#no-inner-declarations)
* [`no-useless-computed-key` flags unnecessary computed member names in classes by default](#no-useless-computed-key)

### Breaking changes for plugin developers

Expand Down Expand Up @@ -362,6 +363,21 @@ if (test) {

**Related issue(s):** [#15576](https://github.com/eslint/eslint/issues/15576)

## <a name="no-useless-computed-key"></a> `no-useless-computed-key` flags unnecessary computed member names in classes by default

In ESLint v9.0.0, the default value of the `enforceForClassMembers` option of the `no-useless-computed-key` rule was changed from `false` to `true`.
The effect of this change ist that unnecessary computed member names in classes will be flagged by default.
fasttime marked this conversation as resolved.
Show resolved Hide resolved

```js
/*eslint no-useless-computed-key: "error"*/

class SomeClass {
["someMethod"]() {} // ok in ESLint v8, error in ESLint v9.
}
```

**Related issue(s):** [#18042](https://github.com/eslint/eslint/issues/18042)

## <a name="removed-context-methods"></a> Removed multiple `context` methods

ESLint v9.0.0 removes multiple deprecated methods from the `context` object and moves them onto the `SourceCode` object:
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/no-useless-computed-key.js
Expand Up @@ -101,7 +101,7 @@ module.exports = {
properties: {
enforceForClassMembers: {
type: "boolean",
default: false
default: true
}
},
additionalProperties: false
Expand All @@ -114,7 +114,7 @@ module.exports = {
},
create(context) {
const sourceCode = context.sourceCode;
const enforceForClassMembers = context.options[0] && context.options[0].enforceForClassMembers;
const enforceForClassMembers = context.options[0]?.enforceForClassMembers ?? true;

/**
* Reports a given node if it violated this rule.
Expand Down