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: object-curly-newline multiline with comments (fixes #6381) #6396

Merged
merged 1 commit into from Jun 15, 2016
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
15 changes: 13 additions & 2 deletions lib/rules/object-curly-newline.js
Expand Up @@ -128,8 +128,8 @@ module.exports = {
var options = normalizedOptions[node.type];
var openBrace = sourceCode.getFirstToken(node);
var closeBrace = sourceCode.getLastToken(node);
var first = sourceCode.getTokenAfter(openBrace);
var last = sourceCode.getTokenBefore(closeBrace);
var first = sourceCode.getTokenOrCommentAfter(openBrace);
var last = sourceCode.getTokenOrCommentBefore(closeBrace);
var needsLinebreaks = (
node.properties.length >= options.minProperties ||
(
Expand All @@ -139,6 +139,17 @@ module.exports = {
)
);

/*
* Use tokens or comments to check multiline or not.
* But use only tokens to check whether line breaks are needed.
* This allows:
* var obj = { // eslint-disable-line foo
* a: 1
* }
*/
first = sourceCode.getTokenAfter(openBrace);
last = sourceCode.getTokenBefore(closeBrace);

if (needsLinebreaks) {
if (astUtils.isTokenOnSameLine(openBrace, first)) {
context.report({
Expand Down
17 changes: 17 additions & 0 deletions tests/lib/rules/object-curly-newline.js
Expand Up @@ -138,6 +138,23 @@ ruleTester.run("object-curly-newline", rule, {
].join("\n"),
options: [{multiline: true}]
},
{
code: [
"var obj = {",
" // comment",
" a: 1",
"};"
].join("\n"),
options: [{multiline: true}]
},
{
code: [
"var obj = { // comment",
" a: 1",
"};"
].join("\n"),
options: [{multiline: true}]
},

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