Skip to content
Permalink
Browse files

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

… (#12214)
  • Loading branch information
mdjermanovic authored and ilyavolodin committed Sep 14, 2019
1 parent 520c922 commit 1ee61b06715fcc750be2c923034a1e59ba663287
@@ -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:
@@ -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

@@ -26,6 +26,16 @@ module.exports = {
schema: [
{
enum: ["always", "never"]
},
{
type: "object",
properties: {
enforceForClassMembers: {
type: "boolean",
default: false
}
},
additionalProperties: false
}
],

@@ -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
@@ -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.
You can’t perform that action at this time.