Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Allow line comment exception in object-curly-spacing (fixes #11902) #12216

Merged
merged 1 commit into from Sep 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/rules/object-curly-spacing.js
Expand Up @@ -167,7 +167,7 @@ module.exports = {
if (options.spaced && !firstSpaced) {
reportRequiredBeginningSpace(node, first);
}
if (!options.spaced && firstSpaced) {
if (!options.spaced && firstSpaced && second.type !== "Line") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this condition is proper. My preference is "if the brace and the next property placed on different lines then the rule does nothing."

({ one, two }) // enforce between `{` and `one`
({ /**/ one, two }) // enforce between `{` and `/**/`
({
    one, two }) // ignore between `{` and `one`.
({ /**/
    one, two }) // ignore between `{` and `/**/`.
({ //
    one, two }) // ignore between `{` and `//`.

I'm not sure how it should handle the space between /**/ and one...

In fact, it may be better if we should separate about the spaces around comments to another rule like space-around-comment and other spacing rules ignore the spaces around comments.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense to reconsider the logic regarding the comments in this and other rules, and other members seem to agree as well.

It isn't a trivial logic though, maybe to merge this PR at the moment because it fixes the particular problem in #11902, and also matches exactly how the other rules work at the moment?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps #12198 could be a good place to implement and test the new logic, that rule at the moment removes comments and also has a bug with parens, so it has to be fixed anyway.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine if we merge this PR for now and separate the PR that improves about comment-around spaces.

reportNoBeginningSpace(node, first);
}
}
Expand Down
108 changes: 108 additions & 0 deletions tests/lib/rules/object-curly-spacing.js
Expand Up @@ -26,13 +26,17 @@ ruleTester.run("object-curly-spacing", rule, {
{ code: "var obj = { foo: bar, baz: qux };", options: ["always"] },
{ code: "var obj = { foo: { bar: quxx }, baz: qux };", options: ["always"] },
{ code: "var obj = {\nfoo: bar,\nbaz: qux\n};", options: ["always"] },
{ code: "var obj = { /**/foo:bar/**/ };", options: ["always"] },
{ code: "var obj = { //\nfoo:bar };", options: ["always"] },

// always - destructuring
{ code: "var { x } = y", options: ["always"], parserOptions: { ecmaVersion: 6 } },
{ code: "var { x, y } = y", options: ["always"], parserOptions: { ecmaVersion: 6 } },
{ code: "var { x,y } = y", options: ["always"], parserOptions: { ecmaVersion: 6 } },
{ code: "var {\nx,y } = y", options: ["always"], parserOptions: { ecmaVersion: 6 } },
{ code: "var {\nx,y\n} = z", options: ["always"], parserOptions: { ecmaVersion: 6 } },
{ code: "var { /**/x/**/ } = y", options: ["always"], parserOptions: { ecmaVersion: 6 } },
{ code: "var { //\nx } = y", options: ["always"], parserOptions: { ecmaVersion: 6 } },
{ code: "var { x = 10, y } = y", options: ["always"], parserOptions: { ecmaVersion: 6 } },
{ code: "var { x: { z }, y } = y", options: ["always"], parserOptions: { ecmaVersion: 6 } },
{ code: "var {\ny,\n} = x", options: ["always"], parserOptions: { ecmaVersion: 6 } },
Expand All @@ -44,6 +48,8 @@ ruleTester.run("object-curly-spacing", rule, {
{ code: "import * as door from 'room'", options: ["always"], parserOptions: { ecmaVersion: 6, sourceType: "module" } },
{ code: "import { door } from 'room'", options: ["always"], parserOptions: { ecmaVersion: 6, sourceType: "module" } },
{ code: "import {\ndoor } from 'room'", options: ["always"], parserOptions: { ecmaVersion: 6, sourceType: "module" } },
{ code: "import { /**/door/**/ } from 'room'", options: ["always"], parserOptions: { ecmaVersion: 6, sourceType: "module" } },
{ code: "import { //\ndoor } from 'room'", options: ["always"], parserOptions: { ecmaVersion: 6, sourceType: "module" } },
{ code: "export { door } from 'room'", options: ["always"], parserOptions: { ecmaVersion: 6, sourceType: "module" } },
{ code: "import { house, mouse } from 'caravan'", options: ["always"], parserOptions: { ecmaVersion: 6, sourceType: "module" } },
{ code: "import house, { mouse } from 'caravan'", options: ["always"], parserOptions: { ecmaVersion: 6, sourceType: "module" } },
Expand All @@ -55,6 +61,10 @@ ruleTester.run("object-curly-spacing", rule, {
{ code: "import {\nx,\n} from 'foo';", options: ["always"], parserOptions: { ecmaVersion: 6, sourceType: "module" } },
{ code: "export { x, } from 'foo';", options: ["always"], parserOptions: { ecmaVersion: 6, sourceType: "module" } },
{ code: "export {\nx,\n} from 'foo';", options: ["always"], parserOptions: { ecmaVersion: 6, sourceType: "module" } },
{ code: "export { /**/x/**/ } from 'foo';", options: ["always"], parserOptions: { ecmaVersion: 6, sourceType: "module" } },
{ code: "export { //\nx } from 'foo';", options: ["always"], parserOptions: { ecmaVersion: 6, sourceType: "module" } },
{ code: "var x = 1;\nexport { /**/x/**/ };", options: ["always"], parserOptions: { ecmaVersion: 6, sourceType: "module" } },
{ code: "var x = 1;\nexport { //\nx };", options: ["always"], parserOptions: { ecmaVersion: 6, sourceType: "module" } },

// always - empty object
{ code: "var foo = {};", options: ["always"] },
Expand Down Expand Up @@ -86,6 +96,8 @@ ruleTester.run("object-curly-spacing", rule, {
{ 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"] },
{ code: "var obj = {//\n foo: bar};", options: ["never"] },
{ code: "var obj = { // line comment exception\n foo: bar};", options: ["never"] },

// never - destructuring
{ code: "var {x} = y", options: ["never"], parserOptions: { ecmaVersion: 6 } },
Expand All @@ -101,6 +113,8 @@ ruleTester.run("object-curly-spacing", rule, {
{ 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 } },
{ code: "var {//\n y} = x", options: ["never"], parserOptions: { ecmaVersion: 6 } },
{ code: "var { // line comment exception\n y} = x", options: ["never"], parserOptions: { ecmaVersion: 6 } },

// never - import / export
{ code: "import door from 'room'", options: ["never"], parserOptions: { ecmaVersion: 6, sourceType: "module" } },
Expand All @@ -111,6 +125,12 @@ ruleTester.run("object-curly-spacing", rule, {
{ code: "export {/* */ door} from 'room'", options: ["never"], parserOptions: { ecmaVersion: 6, sourceType: "module" } },
{ code: "import {door /* */} from 'room'", options: ["never"], parserOptions: { ecmaVersion: 6, sourceType: "module" } },
{ code: "export {door /* */} from 'room'", options: ["never"], parserOptions: { ecmaVersion: 6, sourceType: "module" } },
{ code: "import {//\n door} from 'room'", options: ["never"], parserOptions: { ecmaVersion: 6, sourceType: "module" } },
{ code: "export {//\n door} from 'room'", options: ["never"], parserOptions: { ecmaVersion: 6, sourceType: "module" } },
{ code: "var door = foo;\nexport {//\n door}", options: ["never"], parserOptions: { ecmaVersion: 6, sourceType: "module" } },
{ code: "import { // line comment exception\n door} from 'room'", options: ["never"], parserOptions: { ecmaVersion: 6, sourceType: "module" } },
{ code: "export { // line comment exception\n door} from 'room'", options: ["never"], parserOptions: { ecmaVersion: 6, sourceType: "module" } },
{ code: "var door = foo; export { // line comment exception\n door}", options: ["never"], parserOptions: { ecmaVersion: 6, sourceType: "module" } },
{ code: "import {\ndoor} from 'room'", options: ["never"], parserOptions: { ecmaVersion: 6, sourceType: "module" } },
{ code: "export {\ndoor\n} from 'room'", options: ["never"], parserOptions: { ecmaVersion: 6, sourceType: "module" } },
{ code: "import {house,mouse} from 'caravan'", options: ["never"], parserOptions: { ecmaVersion: 6, sourceType: "module" } },
Expand Down Expand Up @@ -271,6 +291,20 @@ ruleTester.run("object-curly-spacing", rule, {
}
]
},
{
code: "import x, {//\n bar } from 'foo';",
output: "import x, { //\n bar } from 'foo';",
options: ["always"],
parserOptions: { ecmaVersion: 6, 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';",
Expand Down Expand Up @@ -408,6 +442,40 @@ ruleTester.run("object-curly-spacing", rule, {
}
]
},
{
code: "var bar = 0;\nexport {/* */ bar /* */};",
output: "var bar = 0;\nexport { /* */ bar /* */ };",
options: ["always"],
parserOptions: { ecmaVersion: 6, sourceType: "module" },
errors: [
{
message: "A space is required after '{'.",
type: "ExportNamedDeclaration",
line: 2,
column: 8
},
{
message: "A space is required before '}'.",
type: "ExportNamedDeclaration",
line: 2,
column: 24
}
]
},
{
code: "var bar = 0;\nexport {//\n bar };",
output: "var bar = 0;\nexport { //\n bar };",
options: ["always"],
parserOptions: { ecmaVersion: 6, sourceType: "module" },
errors: [
{
message: "A space is required after '{'.",
type: "ExportNamedDeclaration",
line: 2,
column: 8
}
]
},
{
code: "var bar = 0;\nexport { /* */ bar /* */ };",
output: "var bar = 0;\nexport {/* */ bar /* */};",
Expand Down Expand Up @@ -619,6 +687,19 @@ ruleTester.run("object-curly-spacing", rule, {
}
]
},
{
code: "var obj = {//\n foo: bar };",
output: "var obj = { //\n foo: bar };",
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 };",
Expand Down Expand Up @@ -716,6 +797,19 @@ ruleTester.run("object-curly-spacing", rule, {
}
]
},
{
code: "var obj = { // line comment exception\n foo: bar };",
output: "var obj = { // line comment exception\n foo: bar};",
options: ["never"],
errors: [
{
message: "There should be no space before '}'.",
type: "ObjectExpression",
line: 2,
column: 11
}
]
},
{
code: "var obj = { foo: { bar: quxx}, baz: qux};",
output: "var obj = {foo: {bar: quxx}, baz: qux};",
Expand Down Expand Up @@ -832,6 +926,20 @@ ruleTester.run("object-curly-spacing", rule, {
}
]
},
{
code: "var {//\n x } = y",
output: "var { //\n x } = 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",
Expand Down