Skip to content

Commit

Permalink
Fix: Handle fixing objects containing comments (fixes #8484)
Browse files Browse the repository at this point in the history
References rule object-curly-newline. Avoid fixing line breaks when the
token after opening brace is a comment or token before closing brace is
a comment.
  • Loading branch information
schempy committed Jul 20, 2017
1 parent f8d122c commit ba3e667
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/rules/object-curly-newline.js
Expand Up @@ -143,6 +143,8 @@ module.exports = {
first.loc.start.line !== last.loc.end.line
)
);
const hasCommentsFirstToken = astUtils.isCommentToken(first);
const hasCommentsLastToken = astUtils.isCommentToken(last);

/*
* Use tokens or comments to check multiline or not.
Expand All @@ -162,6 +164,10 @@ module.exports = {
node,
loc: openBrace.loc.start,
fix(fixer) {
if (hasCommentsFirstToken) {
return null;
}

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

return fixer.insertTextBefore(closeBrace, "\n");
}
});
Expand All @@ -190,6 +200,10 @@ module.exports = {
node,
loc: openBrace.loc.start,
fix(fixer) {
if (hasCommentsFirstToken) {
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 (hasCommentsLastToken) {
return null;
}

return fixer.removeRange([
last.range[1],
closeBrace.range[0]
Expand Down
139 changes: 139 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,22 @@ 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 // comment",
"};"
].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 +655,22 @@ 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 // comment",
"};"
].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 +688,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 +724,71 @@ 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." }
]
},
{
code: [
"var f = { /* comment */ a:",
"2",
"};"
].join("\n"),
output: null,
options: [{ multiline: true }],
errors: [
{ line: 1, column: 9, message: "Expected a line break after this opening brace." }
]
},
{
code: [
"var f = {",
"/* comment */",
"a: 1};"
].join("\n"),
output: [
"var f = {",
"/* comment */",
"a: 1",
"};"
].join("\n"),
options: [{ multiline: true }],
errors: [
{ line: 3, column: 5, message: "Expected a line break before this closing brace." }
]
},

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

0 comments on commit ba3e667

Please sign in to comment.