-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add no-computed-public-class-fields rule
- Loading branch information
Showing
4 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# no-computed-class-fields | ||
|
||
This prevents the use of computed properties as Class Fields | ||
|
||
```js | ||
const computed = 'bar' | ||
class Foo { | ||
[computed] = 1 | ||
} | ||
|
||
class Foo { | ||
static [computed] = 1 | ||
} | ||
``` | ||
|
||
These will not be allowed because Chrome 72, while supporting class fields, does not support computed properties in class fields. | ||
|
||
## What is the Fix? | ||
|
||
Either make the property non-computed, or you can move these assignments to within the class constructor function for instance properties, or modify the class for static ones: | ||
|
||
```js | ||
const computed = 'bar' | ||
class Foo { | ||
constructor() { | ||
Object.defineProperty(this, computed, { configurable: true, enumerable: true, writable: true, value: 1 }) | ||
} | ||
} | ||
|
||
class Foo { | ||
constructor() { | ||
this[computed] = 1 | ||
} | ||
} | ||
|
||
// Static | ||
Object.defineProperty(Foo, computed, { configurable: true, enumerable: true, writable: true, value: 1 }) | ||
Foo[computed] = 1 | ||
``` | ||
|
||
This can be safely disabled if you intend to compile code with the `@babel/plugin-proposal-class-properties` Babel plugin. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module.exports = (context, badBrowser) => ({ | ||
// Ignore type annotations that don't assign | ||
'ClassProperty[computed=true]:not([typeAnnotation]:not([value]))'(node) { | ||
context.report(node, `Computed Class Fields are not supported in ${badBrowser}`) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
var rule = require('../lib/rules/no-computed-public-class-fields') | ||
var RuleTester = require('eslint').RuleTester | ||
|
||
var ruleTester = new RuleTester({parser: 'babel-eslint', parserOptions: {ecmaVersion: 2018}}) | ||
|
||
ruleTester.run('no-public-class-fields', rule, { | ||
valid: [ | ||
{code: 'class Foo { bar(){} }'}, | ||
{code: 'class Foo { static bar() {} }'}, | ||
{code: 'class Foo { bar: AType }'}, | ||
{code: 'class Foo { ["bar"]() {} }'}, | ||
{code: 'class Foo { static ["bar"]() {} }'}, | ||
{code: 'class Foo { static bar: AType }'}, | ||
{code: 'class Foo { static bar = () => {} }'}, | ||
{code: 'class Foo { static bar = 1 }'}, | ||
{code: 'class Foo { bar: AType }'}, | ||
{code: 'class Foo { bar = () => {} }'}, | ||
{code: 'class Foo { bar = 1 }'}, | ||
], | ||
invalid: [ | ||
{ | ||
code: 'class Foo { ["bar"] = () => {} }', | ||
errors: [ | ||
{ | ||
message: | ||
'Computed Class Fields are not supported in undefined' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'class Foo { ["bar"] = 1 }', | ||
errors: [ | ||
{ | ||
message: | ||
'Computed Class Fields are not supported in undefined' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'class Foo { static ["bar"] = () => {} }', | ||
errors: [ | ||
{ | ||
message: | ||
'Computed Class Fields are not supported in undefined' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'class Foo { static ["bar"] = 1 }', | ||
errors: [ | ||
{ | ||
message: | ||
'Computed Class Fields are not supported in undefined' | ||
} | ||
] | ||
} | ||
] | ||
}) |