Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request eslint#5920 from eslint/issue-5402
Browse files Browse the repository at this point in the history
New: Add `no-useless-computed-key` rule
  • Loading branch information
ilyavolodin committed Apr 21, 2016
2 parents 36d678e + a01b412 commit 495c6c4
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 1 deletion.
1 change: 1 addition & 0 deletions conf/eslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
"no-unused-vars": "error",
"no-use-before-define": "off",
"no-useless-call": "off",
"no-useless-computed-key": "off",
"no-useless-concat": "off",
"no-useless-constructor": "off",
"no-useless-escape": "off",
Expand Down
1 change: 1 addition & 0 deletions docs/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ These rules relate to ES6, also known as ES2015:
* [no-new-symbol](no-new-symbol.md): disallow `new` operators with the `Symbol` object (recommended)
* [no-restricted-imports](no-restricted-imports.md): disallow specified modules when loaded by `import`
* [no-this-before-super](no-this-before-super.md): disallow `this`/`super` before calling `super()` in constructors (recommended)
* [no-useless-computed-key](no-useless-computed-key.md): disallow unnecessary computed property keys in object literals
* [no-useless-constructor](no-useless-constructor.md): disallow unnecessary constructors
* [no-var](no-var.md): require `let` or `const` instead of `var`
* [object-shorthand](object-shorthand.md): require or disallow method and property shorthand syntax for object literals
Expand Down
46 changes: 46 additions & 0 deletions docs/rules/no-useless-computed-key.md
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.
2 changes: 1 addition & 1 deletion docs/rules/object-shorthand.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var foo = {
## Rule Details

This rule enforces the use of the shorthand syntax. This applies
to all methods (including generators) defined on object literals and any
to all methods (including generators) defined in object literals and any
properties defined where the key name matches name of the assigned variable.

Each of the following properties would warn:
Expand Down
37 changes: 37 additions & 0 deletions lib/rules/no-useless-computed-key.js
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) });
}
}
};
}
};
60 changes: 60 additions & 0 deletions tests/lib/rules/no-useless-computed-key.js
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"
}]
}
]
});

0 comments on commit 495c6c4

Please sign in to comment.