Skip to content

Commit

Permalink
feat!: default for enforceForClassMembers in `no-useless-computed-k…
Browse files Browse the repository at this point in the history
…ey` (#18054)

* feat!: default for `enforceForClassMembers` in `no-useless-computed-key`

* Fix a typo

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* Add "To address" section

---------

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
  • Loading branch information
fasttime and mdjermanovic committed Feb 2, 2024
1 parent 47e60f8 commit 3c4d51d
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 89 deletions.
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
18 changes: 18 additions & 0 deletions docs/src/use/migrate-to-9.0.0.md
Expand Up @@ -34,6 +34,7 @@ The lists below are ordered roughly by the number of users each change is expect
* [`"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-unused-vars` now defaults `caughtErrors` to `"all"`](#no-unused-vars)
* [`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 @@ -390,6 +391,23 @@ catch {

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

## <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 is that unnecessary computed member names in classes will be flagged by default.

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

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

**To address:** Fix the problems reported by the rule or revert to the previous behavior by setting the `enforceForClassMembers` option to `false`.

**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

0 comments on commit 3c4d51d

Please sign in to comment.