Skip to content

Commit

Permalink
Update: object-shorthand lints computed methods (fixes eslint#5871) (e…
Browse files Browse the repository at this point in the history
  • Loading branch information
lemonmade authored and nzakas committed Apr 26, 2016
1 parent d24516a commit e2ad1ec
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
10 changes: 6 additions & 4 deletions docs/rules/object-shorthand.md
Expand Up @@ -49,8 +49,9 @@ Each of the following properties would warn:
/*eslint-env es6*/

var foo = {
x: function() {},
y: function *() {},
w: function() {},
x: function *() {},
[y]: function() {},
z: z
};
```
Expand All @@ -62,8 +63,9 @@ In that case the expected syntax would have been:
/*eslint-env es6*/

var foo = {
x() {},
*y() {},
w() {},
*x() {},
[y]() {},
z
};
```
Expand Down
9 changes: 7 additions & 2 deletions lib/rules/object-shorthand.js
Expand Up @@ -108,8 +108,13 @@ module.exports = {
return;
}

// getters, setters and computed properties are ignored
if (node.kind === "get" || node.kind === "set" || node.computed) {
// only computed methods can fail the following checks
if (!APPLY_TO_METHODS && node.computed) {
return;
}

// getters and setters are ignored
if (node.kind === "get" || node.kind === "set") {
return;
}

Expand Down
7 changes: 7 additions & 0 deletions tests/lib/rules/object-shorthand.js
Expand Up @@ -65,6 +65,10 @@ ruleTester.run("object-shorthand", rule, {
{ code: "var x = {['y']: 'y'}", parserOptions: { ecmaVersion: 6 }, options: ["properties"] },
{ code: "var x = {['y']: y}", parserOptions: { ecmaVersion: 6 }, options: ["properties"] },

// object literal computed methods
{ code: "var x = {[y]() {}}", parserOptions: { ecmaVersion: 6 }, options: ["methods"] },
{ code: "var x = {[y]: function x() {}}", parserOptions: { ecmaVersion: 6 }, options: ["methods"] },

// options
{ code: "var x = {y() {}}", parserOptions: { ecmaVersion: 6 }, options: ["methods"] },
{ code: "var x = {x, y() {}, a:b}", parserOptions: { ecmaVersion: 6 }, options: ["methods"] },
Expand Down Expand Up @@ -99,11 +103,14 @@ ruleTester.run("object-shorthand", rule, {
{ code: "doSomething({'x': x})", parserOptions: { ecmaVersion: 6 }, errors: [{ message: "Expected property shorthand.", type: "Property" }] },
{ code: "doSomething({a: 'a', 'x': x})", parserOptions: { ecmaVersion: 6 }, errors: [{ message: "Expected property shorthand.", type: "Property" }] },
{ code: "doSomething({y: function() {}})", parserOptions: { ecmaVersion: 6 }, errors: [{ message: "Expected method shorthand.", type: "Property" }] },
{ code: "doSomething({[y]: function() {}})", parserOptions: { ecmaVersion: 6 }, errors: [{ message: "Expected method shorthand.", type: "Property" }] },
{ code: "doSomething({['y']: function() {}})", parserOptions: { ecmaVersion: 6 }, errors: [{ message: "Expected method shorthand.", type: "Property" }] },

// options
{ code: "var x = {y: function() {}}", parserOptions: { ecmaVersion: 6 }, errors: [{ message: "Expected method shorthand.", type: "Property" }], options: ["methods"] },
{ code: "var x = {x, y() {}, z: function() {}}", parserOptions: { ecmaVersion: 6 }, errors: [{ message: "Expected method shorthand.", type: "Property" }], options: ["methods"] },
{ code: "var x = {ConstructorFunction: function(){}, a: b}", parserOptions: { ecmaVersion: 6 }, errors: [{ message: "Expected method shorthand.", type: "Property" }], options: ["methods"] },
{ code: "var x = {[y]: function() {}}", parserOptions: { ecmaVersion: 6 }, errors: [{ message: "Expected method shorthand.", type: "Property" }], options: ["methods"] },
{ code: "var x = {x: x}", parserOptions: { ecmaVersion: 6 }, errors: [{ message: "Expected property shorthand.", type: "Property" }], options: ["properties"] },
{ code: "var x = {a, b, c(){}, x: x}", parserOptions: { ecmaVersion: 6 }, errors: [{ message: "Expected property shorthand.", type: "Property" }], options: ["properties"] },
{ code: "var x = {y() {}}", parserOptions: { ecmaVersion: 6 }, errors: [{ message: "Expected longform method syntax.", type: "Property" }], options: ["never"] },
Expand Down

0 comments on commit e2ad1ec

Please sign in to comment.