Skip to content
Permalink
Browse files

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

…) (#12216)
  • Loading branch information
mdjermanovic authored and ilyavolodin committed Sep 14, 2019
1 parent db2a29b commit f826eabbeecddb047f58f4e7308a14c18148d369
Showing with 109 additions and 1 deletion.
  1. +1 −1 lib/rules/object-curly-spacing.js
  2. +108 −0 tests/lib/rules/object-curly-spacing.js
@@ -167,7 +167,7 @@ module.exports = {
if (options.spaced && !firstSpaced) {
reportRequiredBeginningSpace(node, first);
}
if (!options.spaced && firstSpaced) {
if (!options.spaced && firstSpaced && second.type !== "Line") {
reportNoBeginningSpace(node, first);
}
}
@@ -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 } },
@@ -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" } },
@@ -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"] },
@@ -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 } },
@@ -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" } },
@@ -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" } },
@@ -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';",
@@ -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 /* */};",
@@ -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 };",
@@ -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};",
@@ -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",

0 comments on commit f826eab

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