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

Update: fix counting jsx comment len in max-len (fixes #12213) #12661

Merged
merged 6 commits into from Jan 9, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 22 additions & 0 deletions lib/rules/max-len.js
Expand Up @@ -183,6 +183,23 @@ module.exports = {
(end.line > lineNumber || (end.line === lineNumber && end.column === line.length));
}

/**
* Check if a comment is in the single line JSXEmptyExpression.
* @param {ASTNode} comment The comment to check.
* @returns {boolean} True if the comment is in the single line JSXEmptyExpression.
*/
function isCommentInSingleLineJSXEmptyExpression(comment) {
const node = sourceCode.getNodeByRangeIndex(comment.range[0]);

if (!node || !node.parent || node.type !== "JSXEmptyExpression" || node.parent.type !== "JSXExpressionContainer") {
return false;
}

const parent = node.parent;

return parent.loc.start.line === parent.loc.end.line;
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
}

mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
/**
* Gets the line after the comment and any remaining trailing whitespace is
* stripped.
Expand Down Expand Up @@ -305,6 +322,11 @@ module.exports = {
// and step back by one
comment = comments[--commentsIndex];

// check jsx comment
if (isCommentInSingleLineJSXEmptyExpression(comment)) {
comment = sourceCode.getNodeByRangeIndex(comment.range[0]).parent;
}
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved

if (isFullLineComment(line, lineNumber, comment)) {
lineIsComment = true;
textToMeasure = line;
Expand Down
122 changes: 122 additions & 0 deletions tests/lib/rules/max-len.js
Expand Up @@ -195,6 +195,45 @@ ruleTester.run("max-len", rule, {
{
code: "\tfoo",
options: [4, { tabWidth: 0 }]
},

// https://github.com/eslint/eslint/issues/12213
{
code: "var jsx = (<>\n" +
" { /* this line has 38 characters */}\n" +
"</>)",
options: [15, { comments: 38 }],
parserOptions: { ecmaFeatures: { jsx: true } }
},
{
code: "var jsx = (<>\n" +
"\t\t{ /* this line has 40 characters */}\n" +
"</>)",
options: [15, 4, { comments: 44 }],
parserOptions: { ecmaFeatures: { jsx: true } }
},
{
code: "var jsx = (<>\n" +
" <> text </>{ /* this line has 49 characters */}\n" +
"</>)",
options: [13, { ignoreComments: true }],
parserOptions: { ecmaFeatures: { jsx: true } }
},
{
code: "var jsx = (<>\n" +
" {/* this line has 37 characters */}\n" +
" <> </> {/* this line has 44 characters */}\n" +
"</>)",
options: [44, { comments: 37 }],
parserOptions: { ecmaFeatures: { jsx: true } }
},
{
code: "var jsx = (<>\n" +
" {/* this line has 37 characters */}\n" +
" <> </> {/* this line has 44 characters */}\n" +
"</>)",
options: [37, { ignoreTrailingComments: true }],
parserOptions: { ecmaFeatures: { jsx: true } }
}
],

Expand Down Expand Up @@ -650,6 +689,89 @@ ruleTester.run("max-len", rule, {
column: 1
}
]
},

// https://github.com/eslint/eslint/issues/12213
{
code: "var jsx = (<>\n" +
" { /* this line has 38 characters */}\n" +
"</>)",
options: [15, { comments: 37 }],
parserOptions: { ecmaFeatures: { jsx: true } },
errors: [
{
messageId: "maxComment",
data: { lineLength: 38, maxCommentLength: 37 },
type: "Program",
line: 2,
column: 1
}
]
},
{
code: "var jsx = (<>\n" +
"\t\t{ /* this line has 40 characters */}\n" +
"</>)",
options: [15, 4, { comments: 40 }],
parserOptions: { ecmaFeatures: { jsx: true } },
errors: [
{
messageId: "maxComment",
data: { lineLength: 44, maxCommentLength: 40 },
type: "Program",
line: 2,
column: 1
}
]
},
{
code: "var jsx = (<>\n" +
"{ 38/* this line has 38 characters */}\n" +
"</>)",
options: [15, { comments: 37 }],
yeonjuan marked this conversation as resolved.
Show resolved Hide resolved
parserOptions: { ecmaFeatures: { jsx: true } },
errors: [
{
messageId: "max",
data: { lineLength: 38, maxLength: 15 },
type: "Program",
line: 2,
column: 1
}
]
},
{
code: "var jsx = (<>\n" +
" <> 50 </>{ 50/* this line has 50 characters */}\n" +
"</>)",
options: [49, { comments: 100 }],
parserOptions: { ecmaFeatures: { jsx: true } },
errors: [
{
messageId: "max",
data: { lineLength: 50, maxLength: 49 },
type: "Program",
line: 2,
column: 1
}
]
},
{
code: "var jsx = (<>\n" +
" {/* this line has 44 characters */}\n" +
" <> </> {/* this line has 44 characters */}\n" +
"</>)",
options: [37, { ignoreTrailingComments: true }],
parserOptions: { ecmaFeatures: { jsx: true } },
errors: [
{
messageId: "max",
data: { lineLength: 44, maxLength: 37 },
type: "Program",
line: 2,
column: 1
}
]
}
]
});