Skip to content

Commit

Permalink
Fix: object-curly-newline multiline with comments (fixes #6381) (#6396
Browse files Browse the repository at this point in the history
)
  • Loading branch information
mysticatea authored and nzakas committed Jun 15, 2016
1 parent 77697a7 commit 4f73240
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
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

0 comments on commit 4f73240

Please sign in to comment.