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

Update: enforceForClassMembers computed-property-spacing (fixes #12049) #12214

Merged
merged 1 commit into from Sep 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
55 changes: 54 additions & 1 deletion docs/rules/computed-property-spacing.md
Expand Up @@ -25,11 +25,17 @@ This rule does not apply to brackets that are separated from the adjacent value

## Options

This rule has a string option:
This rule has two options, a string option and an object option.

String option:

* `"never"` (default) disallows spaces inside computed property brackets
* `"always"` requires one or more spaces inside computed property brackets

Object option:

* `"enforceForClassMembers": true` additionally applies this rule to class members (default is `false`)

### never

Examples of **incorrect** code for this rule with the default `"never"` option:
Expand Down Expand Up @@ -84,6 +90,53 @@ var x = {[ b ]: a}
obj[ foo[ bar ] ]
```

#### enforceForClassMembers

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

When `enforceForClassMembers` is set to `true`, the rule will also disallow/enforce spaces inside of
computed keys of class methods, getters and setters.

Examples of **incorrect** code for this rule with `"never"` and `{ "enforceForClassMembers": true }`:

```js
/*eslint computed-property-spacing: ["error", "never", { "enforceForClassMembers": true }]*/
/*eslint-env es6*/

class Foo {
[a ]() {}
get [b ]() {}
set [b ](value) {}
}

const Bar = class {
[ a](){}
static [ b]() {}
static get [ c ]() {}
static set [ c ](value) {}
}
```

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

```js
/*eslint computed-property-spacing: ["error", "never", { "enforceForClassMembers": true }]*/
/*eslint-env es6*/

class Foo {
[a]() {}
get [b]() {}
set [b](value) {}
}

const Bar = class {
[a](){}
static [b]() {}
static get [c]() {}
static set [c](value) {}
}
```

## When Not To Use It

Expand Down
19 changes: 18 additions & 1 deletion lib/rules/computed-property-spacing.js
Expand Up @@ -26,6 +26,16 @@ module.exports = {
schema: [
{
enum: ["always", "never"]
},
{
type: "object",
properties: {
enforceForClassMembers: {
type: "boolean",
default: false
}
},
additionalProperties: false
}
],

Expand All @@ -41,6 +51,7 @@ module.exports = {
create(context) {
const sourceCode = context.getSourceCode();
const propertyNameMustBeSpaced = context.options[0] === "always"; // default is "never"
const enforceForClassMembers = context.options[1] && context.options[1].enforceForClassMembers;

//--------------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -178,10 +189,16 @@ module.exports = {
// Public
//--------------------------------------------------------------------------

return {
const listeners = {
Property: checkSpacing("key"),
MemberExpression: checkSpacing("property")
};

if (enforceForClassMembers) {
listeners.MethodDefinition = checkSpacing("key");
}

return listeners;

}
};