This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
forked from eslint/eslint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request eslint#5920 from eslint/issue-5402
New: Add `no-useless-computed-key` rule
- Loading branch information
Showing
6 changed files
with
146 additions
and
1 deletion.
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
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,46 @@ | ||
# Disallow unnecessary computed property keys on objects (no-useless-computed-key) | ||
|
||
It's unnecessary to use computed properties with literals such as: | ||
|
||
```js | ||
var foo = {["a"]: "b"}; | ||
``` | ||
|
||
The code can be rewritten as: | ||
|
||
```js | ||
var foo = {"a": "b"}; | ||
``` | ||
|
||
## Rule Details | ||
|
||
This rule disallows unnecessary usage of computed property keys. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
/*eslint no-useless-computed-key: "error"*/ | ||
/*eslint-env es6*/ | ||
|
||
var a = { ['0']: 0 }; | ||
var a = { ['0+1,234']: 0 }; | ||
var a = { [0]: 0 }; | ||
var a = { ['x']: 0 }; | ||
var a = { ['x']() {} }; | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
/*eslint no-useless-computed-key: "error"*/ | ||
|
||
var c = { 'a': 0 }; | ||
var c = { 0: 0 }; | ||
var a = { x() {} }; | ||
var c = { a: 0 }; | ||
var c = { '0+1,234': 0 }; | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you don't want to be notified about unnecessary computed property keys, you can safely disable this rule. |
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,37 @@ | ||
/** | ||
* @fileoverview Rule to disallow unnecessary computed property keys in object literals | ||
* @author Burak Yigit Kaya | ||
*/ | ||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
var MESSAGE_UNNECESSARY_COMPUTED = "Unnecessarily computed property [{{property}}] found."; | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: "disallow unnecessary computed property keys in object literals", | ||
category: "ECMAScript 6", | ||
recommended: false | ||
} | ||
}, | ||
create: function(context) { | ||
return { | ||
"Property": function(node) { | ||
if (!node.computed) { | ||
return; | ||
} | ||
|
||
var key = node.key, | ||
nodeType = typeof key.value; | ||
|
||
if (key.type === "Literal" && (nodeType === "string" || nodeType === "number")) { | ||
context.report(node, MESSAGE_UNNECESSARY_COMPUTED, { property: context.getSource(key) }); | ||
} | ||
} | ||
}; | ||
} | ||
}; |
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,60 @@ | ||
/** | ||
* @fileoverview Tests for no-useless-computed-key rule. | ||
* @author Burak Yigit Kaya | ||
*/ | ||
|
||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
var rule = require("../../../lib/rules/no-useless-computed-key"), | ||
RuleTester = require("../../../lib/testers/rule-tester"); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
var ruleTester = new RuleTester(); | ||
|
||
ruleTester.run("no-useless-computed-key", rule, { | ||
valid: [ | ||
{ code: "({ 'a': 0, b(){} })", env: { es6: true } }, | ||
{ code: "({ [x]: 0 });", env: { es6: true } }, | ||
{ code: "({ a: 0, [b](){} })", env: { es6: true } } | ||
], | ||
invalid: [ | ||
{ | ||
code: "({ ['0']: 0 })", | ||
env: {es6: true}, | ||
errors: [{ | ||
message: "Unnecessarily computed property ['0'] found.", type: "Property" | ||
}] | ||
}, { | ||
code: "({ ['0+1,234']: 0 })", | ||
env: {es6: true}, | ||
errors: [{ | ||
message: "Unnecessarily computed property ['0+1,234'] found.", type: "Property" | ||
}] | ||
}, { | ||
code: "({ [0]: 0 })", | ||
env: {es6: true}, | ||
errors: [{ | ||
message: "Unnecessarily computed property [0] found.", type: "Property" | ||
}] | ||
}, { | ||
code: "({ ['x']: 0 })", | ||
env: {es6: true}, | ||
errors: [{ | ||
message: "Unnecessarily computed property ['x'] found.", type: "Property" | ||
}] | ||
}, { | ||
code: "({ ['x']() {} })", | ||
env: {es6: true}, | ||
errors: [{ | ||
message: "Unnecessarily computed property ['x'] found.", type: "Property" | ||
}] | ||
} | ||
] | ||
}); |