Skip to content

Commit

Permalink
Fix: Avoid fixing objects containing comments (fixes #8484)
Browse files Browse the repository at this point in the history
  • Loading branch information
schempy committed Jul 19, 2017
1 parent d09288a commit c84c872
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/rules/object-curly-newline.js
Expand Up @@ -143,6 +143,7 @@ module.exports = {
first.loc.start.line !== last.loc.end.line
)
);
const hasComments = astUtils.isCommentToken(first);

/*
* Use tokens or comments to check multiline or not.
Expand All @@ -155,13 +156,18 @@ module.exports = {
first = sourceCode.getTokenAfter(openBrace);
last = sourceCode.getTokenBefore(closeBrace);


if (needsLinebreaks) {
if (astUtils.isTokenOnSameLine(openBrace, first)) {
context.report({
message: "Expected a line break after this opening brace.",
node,
loc: openBrace.loc.start,
fix(fixer) {
if (hasComments) {
return null;
}

return fixer.insertTextAfter(openBrace, "\n");
}
});
Expand All @@ -172,6 +178,10 @@ module.exports = {
node,
loc: closeBrace.loc.start,
fix(fixer) {
if (hasComments) {
return null;
}

return fixer.insertTextBefore(closeBrace, "\n");
}
});
Expand All @@ -190,6 +200,10 @@ module.exports = {
node,
loc: openBrace.loc.start,
fix(fixer) {
if (hasComments) {
return null;
}

return fixer.removeRange([
openBrace.range[1],
first.range[0]
Expand All @@ -206,6 +220,10 @@ module.exports = {
node,
loc: closeBrace.loc.start,
fix(fixer) {
if (hasComments) {
return null;
}

return fixer.removeRange([
last.range[1],
closeBrace.range[0]
Expand Down
108 changes: 108 additions & 0 deletions tests/lib/rules/object-curly-newline.js
Expand Up @@ -584,6 +584,31 @@ ruleTester.run("object-curly-newline", rule, {
{ line: 2, column: 1, message: "Unexpected line break before this closing brace." }
]
},
{
code: [
"var a = {",
" /* comment */ ",
"};"
].join("\n"),
output: null,
options: [{ multiline: true }],
errors: [
{ line: 1, column: 9, message: "Unexpected line break after this opening brace." },
{ line: 3, column: 1, message: "Unexpected line break before this closing brace." }
]
},
{
code: [
"var a = { // comment",
"};"
].join("\n"),
output: null,
options: [{ multiline: true }],
errors: [
{ line: 1, column: 9, message: "Unexpected line break after this opening brace." },
{ line: 2, column: 1, message: "Unexpected line break before this closing brace." }
]
},
{
code: [
"var b = {",
Expand All @@ -599,6 +624,21 @@ ruleTester.run("object-curly-newline", rule, {
{ line: 3, column: 1, message: "Unexpected line break before this closing brace." }
]
},
{
code: [
"var b = {",
" a: 1 // comment",
"};"
].join("\n"),
output: [
"var b = {a: 1};"
].join("\n"),
options: [{ multiline: true }],
errors: [
{ line: 1, column: 9, message: "Unexpected line break after this opening brace." },
{ line: 3, column: 1, message: "Unexpected line break before this closing brace." }
]
},
{
code: [
"var c = {",
Expand All @@ -614,6 +654,21 @@ ruleTester.run("object-curly-newline", rule, {
{ line: 3, column: 1, message: "Unexpected line break before this closing brace." }
]
},
{
code: [
"var c = {",
" a: 1, b: 2 // comment",
"};"
].join("\n"),
output: [
"var c = {a: 1, b: 2};"
].join("\n"),
options: [{ multiline: true }],
errors: [
{ line: 1, column: 9, message: "Unexpected line break after this opening brace." },
{ line: 3, column: 1, message: "Unexpected line break before this closing brace." }
]
},
{
code: [
"var d = {a: 1,",
Expand All @@ -631,6 +686,23 @@ ruleTester.run("object-curly-newline", rule, {
{ line: 2, column: 9, message: "Expected a line break before this closing brace." }
]
},
{
code: [
"var d = {a: 1, // comment",
" b: 2};"
].join("\n"),
output: [
"var d = {",
"a: 1, // comment",
" b: 2",
"};"
].join("\n"),
options: [{ multiline: true }],
errors: [
{ line: 1, column: 9, message: "Expected a line break after this opening brace." },
{ line: 2, column: 9, message: "Expected a line break before this closing brace." }
]
},
{
code: [
"var e = {a: function foo() {",
Expand All @@ -650,6 +722,42 @@ ruleTester.run("object-curly-newline", rule, {
{ line: 3, column: 2, message: "Expected a line break before this closing brace." }
]
},
{
code: [
"var e = {a: function foo() { // comment",
" dosomething();",
"}};"
].join("\n"),
output: [
"var e = {",
"a: function foo() { // comment",
" dosomething();",
"}",
"};"
].join("\n"),
options: [{ multiline: true }],
errors: [
{ line: 1, column: 9, message: "Expected a line break after this opening brace." },
{ line: 3, column: 2, message: "Expected a line break before this closing brace." }
]
},
{
code: [
"var e = {a: 1, /* comment */",
" b: 2, // another comment",
"};"
].join("\n"),
output: [
"var e = {",
"a: 1, /* comment */",
" b: 2, // another comment",
"};"
].join("\n"),
options: [{ multiline: true }],
errors: [
{ line: 1, column: 9, message: "Expected a line break after this opening brace." }
]
},

// "minProperties" ----------------------------------------------------------
{
Expand Down

0 comments on commit c84c872

Please sign in to comment.