Skip to content
Permalink
Browse files

Fix: consider comments in object-curly-spacing (fixes #11656) (#11657)

  • Loading branch information...
g-plane authored and not-an-aardvark committed Apr 30, 2019
1 parent b6d41cb commit 67c08b67509c54acd96aab2cec22efb53bfe6265
Showing with 192 additions and 8 deletions.
  1. +8 −8 lib/rules/object-curly-spacing.js
  2. +184 −0 tests/lib/rules/object-curly-spacing.js
@@ -82,7 +82,7 @@ module.exports = {
token: token.value
},
fix(fixer) {
const nextToken = context.getSourceCode().getTokenAfter(token);
const nextToken = context.getSourceCode().getTokenAfter(token, { includeComments: true });

return fixer.removeRange([token.range[1], nextToken.range[0]]);
}
@@ -104,7 +104,7 @@ module.exports = {
token: token.value
},
fix(fixer) {
const previousToken = context.getSourceCode().getTokenBefore(token);
const previousToken = context.getSourceCode().getTokenBefore(token, { includeComments: true });

return fixer.removeRange([previousToken.range[1], token.range[0]]);
}
@@ -225,8 +225,8 @@ module.exports = {

const first = sourceCode.getFirstToken(node),
last = getClosingBraceOfObject(node),
second = sourceCode.getTokenAfter(first),
penultimate = sourceCode.getTokenBefore(last);
second = sourceCode.getTokenAfter(first, { includeComments: true }),
penultimate = sourceCode.getTokenBefore(last, { includeComments: true });

validateBraceSpacing(node, first, second, penultimate, last);
}
@@ -253,8 +253,8 @@ module.exports = {

const first = sourceCode.getTokenBefore(firstSpecifier),
last = sourceCode.getTokenAfter(lastSpecifier, astUtils.isNotCommaToken),
second = sourceCode.getTokenAfter(first),
penultimate = sourceCode.getTokenBefore(last);
second = sourceCode.getTokenAfter(first, { includeComments: true }),
penultimate = sourceCode.getTokenBefore(last, { includeComments: true });

validateBraceSpacing(node, first, second, penultimate, last);
}
@@ -273,8 +273,8 @@ module.exports = {
lastSpecifier = node.specifiers[node.specifiers.length - 1],
first = sourceCode.getTokenBefore(firstSpecifier),
last = sourceCode.getTokenAfter(lastSpecifier, astUtils.isNotCommaToken),
second = sourceCode.getTokenAfter(first),
penultimate = sourceCode.getTokenBefore(last);
second = sourceCode.getTokenAfter(first, { includeComments: true }),
penultimate = sourceCode.getTokenBefore(last, { includeComments: true });

validateBraceSpacing(node, first, second, penultimate, last);
}
@@ -84,6 +84,8 @@ ruleTester.run("object-curly-spacing", rule, {
{ code: "var obj = {foo: {\nbar: quxx}, baz: qux\n};", options: ["never"] },
{ code: "var obj = {foo: {\nbar: quxx\n}, baz: qux};", options: ["never"] },
{ code: "var obj = {\nfoo: bar,\nbaz: qux\n};", options: ["never"] },
{ code: "var obj = {foo: bar, baz: qux /* */};", options: ["never"] },
{ code: "var obj = {/* */ foo: bar, baz: qux};", options: ["never"] },

// never - destructuring
{ code: "var {x} = y", options: ["never"], parserOptions: { ecmaVersion: 6 } },
@@ -97,12 +99,18 @@ ruleTester.run("object-curly-spacing", rule, {
{ code: "var {\ny,\n} = x", options: ["never"], parserOptions: { ecmaVersion: 6 } },
{ code: "var {y,} = x", options: ["never"], parserOptions: { ecmaVersion: 6 } },
{ code: "var {y:x} = x", options: ["never"], parserOptions: { ecmaVersion: 6 } },
{ code: "var {/* */ y} = x", options: ["never"], parserOptions: { ecmaVersion: 6 } },
{ code: "var {y /* */} = x", options: ["never"], parserOptions: { ecmaVersion: 6 } },

// never - import / export
{ code: "import door from 'room'", options: ["never"], parserOptions: { sourceType: "module" } },
{ code: "import * as door from 'room'", options: ["never"], parserOptions: { sourceType: "module" } },
{ code: "import {door} from 'room'", options: ["never"], parserOptions: { sourceType: "module" } },
{ code: "export {door} from 'room'", options: ["never"], parserOptions: { sourceType: "module" } },
{ code: "import {/* */ door} from 'room'", options: ["never"], parserOptions: { sourceType: "module" } },
{ code: "export {/* */ door} from 'room'", options: ["never"], parserOptions: { sourceType: "module" } },
{ code: "import {door /* */} from 'room'", options: ["never"], parserOptions: { sourceType: "module" } },
{ code: "export {door /* */} from 'room'", options: ["never"], parserOptions: { sourceType: "module" } },
{ code: "import {\ndoor} from 'room'", options: ["never"], parserOptions: { sourceType: "module" } },
{ code: "export {\ndoor\n} from 'room'", options: ["never"], parserOptions: { sourceType: "module" } },
{ code: "import {house,mouse} from 'caravan'", options: ["never"], parserOptions: { sourceType: "module" } },
@@ -235,6 +243,34 @@ ruleTester.run("object-curly-spacing", rule, {

]
},
{
code: "import x, { bar/* */} from 'foo';",
output: "import x, { bar/* */ } from 'foo';",
options: ["always"],
parserOptions: { sourceType: "module" },
errors: [
{
message: "A space is required before '}'.",
type: "ImportDeclaration",
line: 1,
column: 21
}
]
},
{
code: "import x, {/* */bar } from 'foo';",
output: "import x, { /* */bar } from 'foo';",
options: ["always"],
parserOptions: { sourceType: "module" },
errors: [
{
message: "A space is required after '{'.",
type: "ImportDeclaration",
line: 1,
column: 11
}
]
},
{
code: "import x, { bar, baz} from 'foo';",
output: "import x, { bar, baz } from 'foo';",
@@ -332,6 +368,26 @@ ruleTester.run("object-curly-spacing", rule, {
}
]
},
{
code: "import { /* */ bar, /* */ } from 'foo';",
output: "import {/* */ bar, /* */} from 'foo';",
options: ["never"],
parserOptions: { sourceType: "module" },
errors: [
{
message: "There should be no space after '{'.",
type: "ImportDeclaration",
line: 1,
column: 8
},
{
message: "There should be no space before '}'.",
type: "ImportDeclaration",
line: 1,
column: 27
}
]
},
{
code: "var bar = 0;\nexport {bar};",
output: "var bar = 0;\nexport { bar };",
@@ -352,6 +408,26 @@ ruleTester.run("object-curly-spacing", rule, {
}
]
},
{
code: "var bar = 0;\nexport { /* */ bar /* */ };",
output: "var bar = 0;\nexport {/* */ bar /* */};",
options: ["never"],
parserOptions: { sourceType: "module" },
errors: [
{
message: "There should be no space after '{'.",
type: "ExportNamedDeclaration",
line: 2,
column: 8
},
{
message: "There should be no space before '}'.",
type: "ExportNamedDeclaration",
line: 2,
column: 26
}
]
},

// always - arraysInObjects
{
@@ -530,6 +606,19 @@ ruleTester.run("object-curly-spacing", rule, {
}
]
},
{
code: "var obj = {/* */foo: bar, baz: qux };",
output: "var obj = { /* */foo: bar, baz: qux };",
options: ["always"],
errors: [
{
message: "A space is required after '{'.",
type: "ObjectExpression",
line: 1,
column: 11
}
]
},
{
code: "var obj = { foo: bar, baz: qux};",
output: "var obj = { foo: bar, baz: qux };",
@@ -543,6 +632,19 @@ ruleTester.run("object-curly-spacing", rule, {
}
]
},
{
code: "var obj = { foo: bar, baz: qux/* */};",
output: "var obj = { foo: bar, baz: qux/* */ };",
options: ["always"],
errors: [
{
message: "A space is required before '}'.",
type: "ObjectExpression",
line: 1,
column: 36
}
]
},
{
code: "var obj = { foo: bar, baz: qux };",
output: "var obj = {foo: bar, baz: qux};",
@@ -575,6 +677,19 @@ ruleTester.run("object-curly-spacing", rule, {
}
]
},
{
code: "var obj = {foo: bar, baz: qux /* */ };",
output: "var obj = {foo: bar, baz: qux /* */};",
options: ["never"],
errors: [
{
message: "There should be no space before '}'.",
type: "ObjectExpression",
line: 1,
column: 37
}
]
},
{
code: "var obj = { foo: bar, baz: qux};",
output: "var obj = {foo: bar, baz: qux};",
@@ -588,6 +703,19 @@ ruleTester.run("object-curly-spacing", rule, {
}
]
},
{
code: "var obj = { /* */ foo: bar, baz: qux};",
output: "var obj = {/* */ foo: bar, baz: qux};",
options: ["never"],
errors: [
{
message: "There should be no space after '{'.",
type: "ObjectExpression",
line: 1,
column: 11
}
]
},
{
code: "var obj = { foo: { bar: quxx}, baz: qux};",
output: "var obj = {foo: {bar: quxx}, baz: qux};",
@@ -676,6 +804,34 @@ ruleTester.run("object-curly-spacing", rule, {
}
]
},
{
code: "var { x, y/* */} = y",
output: "var { x, y/* */ } = y",
options: ["always"],
parserOptions: { ecmaVersion: 6 },
errors: [
{
message: "A space is required before '}'.",
type: "ObjectPattern",
line: 1,
column: 16
}
]
},
{
code: "var {/* */x, y } = y",
output: "var { /* */x, y } = y",
options: ["always"],
parserOptions: { ecmaVersion: 6 },
errors: [
{
message: "A space is required after '{'.",
type: "ObjectPattern",
line: 1,
column: 5
}
]
},
{
code: "var { x, y } = y",
output: "var {x, y} = y",
@@ -710,6 +866,34 @@ ruleTester.run("object-curly-spacing", rule, {
}
]
},
{
code: "var {x, y/* */ } = y",
output: "var {x, y/* */} = y",
options: ["never"],
parserOptions: { ecmaVersion: 6 },
errors: [
{
message: "There should be no space before '}'.",
type: "ObjectPattern",
line: 1,
column: 16
}
]
},
{
code: "var { /* */x, y} = y",
output: "var {/* */x, y} = y",
options: ["never"],
parserOptions: { ecmaVersion: 6 },
errors: [
{
message: "There should be no space after '{'.",
type: "ObjectPattern",
line: 1,
column: 5
}
]
},
{
code: "var { x=10} = y",
output: "var { x=10 } = y",

0 comments on commit 67c08b6

Please sign in to comment.
You can’t perform that action at this time.