Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update: enforceForClassMembers computed-property-spacing (fixes #12049)…
… (#12214)
  • Loading branch information
mdjermanovic authored and ilyavolodin committed Sep 14, 2019
1 parent 520c922 commit 1ee61b0
Show file tree
Hide file tree
Showing 3 changed files with 508 additions and 4 deletions.
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;

}
};

0 comments on commit 1ee61b0

Please sign in to comment.