Skip to content

Commit

Permalink
Merge pull request #4219 from eslint/issue3678
Browse files Browse the repository at this point in the history
Fix: Support ES6 shorthand in key-spacing (fixes #3678)
  • Loading branch information
nzakas committed Oct 21, 2015
2 parents e42bdad + 103eb95 commit 64a8387
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/rules/key-spacing.js
Expand Up @@ -181,9 +181,16 @@ module.exports = function(context) {
* @returns {int} Width of the key.
*/
function getKeyWidth(property) {
var key = property.key;
var startToken = getFirstNodeInLine(key);
var endToken = getFirstNodeBeforeColon(key);
var key, startToken, endToken;

// Ignore shorthand methods and properties, as they have no colon
if (property.method || property.shorthand) {
return 0;
}

key = property.key;
startToken = getFirstNodeInLine(key);
endToken = getFirstNodeBeforeColon(key);

return endToken.range[1] - startToken.range[0];
}
Expand Down
136 changes: 136 additions & 0 deletions tests/lib/rules/key-spacing.js
Expand Up @@ -215,6 +215,75 @@ ruleTester.run("key-spacing", rule, {
].join("\n"),
ecmaFeatures: { modules: true, objectLiteralShorthandProperties: true },
options: [{ "align": "value" }]
}, {
code: [
"var test = {",
" prop: 123,",
" a,",
" b",
"};"
].join("\n"),
ecmaFeatures: { objectLiteralShorthandProperties: true }
}, {
code: [
"var test = {",
" prop: 456,",
" c,",
" d",
"};"
].join("\n"),
ecmaFeatures: { objectLiteralShorthandProperties: true },
options: [{ "align": "value" }]
}, {
code: [
"var obj = {",
" foobar: 123,",
" prop,",
" baz: 456",
"};"
].join("\n"),
ecmaFeatures: { objectLiteralShorthandProperties: true },
options: [{ "align": "value" }]
}, {
code: [
"var test = {",
" prop: 123,",
" a() { }",
"};"
].join("\n"),
ecmaFeatures: { objectLiteralShorthandMethods: true }
}, {
code: [
"var test = {",
" prop: 123,",
" a() { },",
" b() { }",
"};"
].join("\n"),
ecmaFeatures: { objectLiteralShorthandMethods: true },
options: [{ "align": "value" }]
}, {
code: [
"var obj = {",
" foobar: 123,",
" method() { },",
" baz: 456",
"};"
].join("\n"),
ecmaFeatures: { objectLiteralShorthandMethods: true },
options: [{ "align": "value" }]
}, {
code: [
"var obj = {",
" foobar: 123,",
" method() {",
" return 42;",
" },",
" baz: 456",
"};"
].join("\n"),
ecmaFeatures: { objectLiteralShorthandMethods: true },
options: [{ "align": "value" }]
}],

invalid: [{
Expand Down Expand Up @@ -445,5 +514,72 @@ ruleTester.run("key-spacing", rule, {
errors: [
{ message: "Extra space before value for key \"key\".", line: 2, column: 8, type: "Identifier" }
]
}, {
code: [
"var obj = {",
" foobar: 123,",
" prop,",
" baz: 456",
"};"
].join("\n"),
ecmaFeatures: { objectLiteralShorthandProperties: true },
options: [{ "align": "value" }],
errors: [
{ message: "Missing space before value for key \"baz\".", line: 4, column: 10, type: "Literal" }
]
}, {
code: [
"var obj = {",
" foobar: 123,",
" prop,",
" baz: 456",
"};"
].join("\n"),
ecmaFeatures: { objectLiteralShorthandProperties: true },
options: [{ "align": "value" }],
errors: [
{ message: "Extra space before value for key \"foobar\".", line: 2, column: 14, type: "Literal" }
]
}, {
code: [
"var obj = {",
" foobar: 123,",
" method() { },",
" baz: 456",
"};"
].join("\n"),
ecmaFeatures: { objectLiteralShorthandMethods: true },
options: [{ "align": "value" }],
errors: [
{ message: "Missing space before value for key \"baz\".", line: 4, column: 10, type: "Literal" }
]
}, {
code: [
"var obj = {",
" foobar: 123,",
" method() { },",
" baz: 456",
"};"
].join("\n"),
ecmaFeatures: { objectLiteralShorthandMethods: true },
options: [{ "align": "value" }],
errors: [
{ message: "Extra space before value for key \"foobar\".", line: 2, column: 14, type: "Literal" }
]
}, {
code: [
"var obj = {",
" foobar: 123,",
" method() {",
" return 42;",
" },",
" baz: 456",
"};"
].join("\n"),
ecmaFeatures: { objectLiteralShorthandMethods: true },
options: [{ "align": "value" }],
errors: [
{ message: "Extra space before value for key \"baz\".", line: 6, column: 13, type: "Literal" }
]
}]
});

0 comments on commit 64a8387

Please sign in to comment.