Skip to content

Commit

Permalink
Fix: syntax error after key-spacing autofix with comment (fixes #7603
Browse files Browse the repository at this point in the history
…) (#7607)

* Fix: syntax error after `key-spacing` autofix with comment (fixes #7603)

* Add a test for adding spacing with comments
  • Loading branch information
not-an-aardvark authored and ilyavolodin committed Nov 15, 2016
1 parent f56c1ef commit bd0514c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/rules/key-spacing.js
Expand Up @@ -417,8 +417,8 @@ module.exports = {
function report(property, side, whitespace, expected, mode) {
const diff = whitespace.length - expected,
nextColon = getNextColon(property.key),
tokenBeforeColon = sourceCode.getTokenBefore(nextColon),
tokenAfterColon = sourceCode.getTokenAfter(nextColon),
tokenBeforeColon = sourceCode.getTokenOrCommentBefore(nextColon),
tokenAfterColon = sourceCode.getTokenOrCommentAfter(nextColon),
isKeySide = side === "key",
locStart = isKeySide ? tokenBeforeColon.loc.start : tokenAfterColon.loc.start,
isExtra = diff > 0,
Expand Down
20 changes: 20 additions & 0 deletions tests/lib/rules/key-spacing.js
Expand Up @@ -1728,5 +1728,25 @@ ruleTester.run("key-spacing", rule, {
{ message: "Extra space before value for key 'key2'.", line: 4, column: 14, type: "Literal" },
{ message: "Extra space before value for key 'key3'.", line: 5, column: 14, type: "Literal" }
]
}, {

// https://github.com/eslint/eslint/issues/7603
code: "({ foo/* comment */ : bar })",
output: "({ foo/* comment */: bar })",
errors: [{ message: "Extra space after key 'foo'.", line: 1, column: 7, type: "Identifier" }]
}, {
code: "({ foo: /* comment */bar })",
output: "({ foo:/* comment */bar })",
options: [{ afterColon: false }],
errors: [{ message: "Extra space before value for key 'foo'.", line: 1, column: 9, type: "Identifier" }]
},
{
code: "({ foo/*comment*/:/*comment*/bar })",
output: "({ foo/*comment*/ : /*comment*/bar })",
options: [{ beforeColon: true, afterColon: true }],
errors: [
{ message: "Missing space after key 'foo'.", line: 1, column: 7, type: "Identifier" },
{ message: "Missing space before value for key 'foo'.", line: 1, column: 19, type: "Identifier"}
]
}]
});

0 comments on commit bd0514c

Please sign in to comment.