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 3 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
37 changes: 36 additions & 1 deletion lib/rules/max-len.js
Expand Up @@ -183,6 +183,21 @@ module.exports = {
(end.line > lineNumber || (end.line === lineNumber && end.column === line.length));
}

/**
* Check if a node is a JSXEmptyExpression contained in a single line JSXExpressionContainer.
* @param {ASTNode} node A node to check.
* @returns {boolean} True if the node is a JSXEmptyExpression contained in a single line JSXExpressionContainer.
*/
function isJSXEmptyExpressionInSingleLineContainer(node) {
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
}

/**
* Gets the line after the comment and any remaining trailing whitespace is
* stripped.
Expand Down Expand Up @@ -252,6 +267,26 @@ module.exports = {
return acc;
}

/**
* Returns an array of all comments in the source code.
* If the element in the array is a JSXEmptyExpression contained with a single line JSXExpressionContainer,
* the element is changed with JSXExpressionContainer node.
* @returns {ASTNode[]} An array of comment nodes
*/
function getAllComments() {
return sourceCode
.getAllComments()
.map(comment => {
const containingNode = sourceCode.getNodeByRangeIndex(comment.range[0]);

if (isJSXEmptyExpressionInSingleLineContainer(containingNode)) {
return containingNode.parent;
}

return comment;
});
yeonjuan marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Check the program for max length
* @param {ASTNode} node Node to examine
Expand All @@ -264,7 +299,7 @@ module.exports = {
const lines = sourceCode.lines,

// list of comments to ignore
comments = ignoreComments || maxCommentLength || ignoreTrailingComments ? sourceCode.getAllComments() : [];
comments = ignoreComments || maxCommentLength || ignoreTrailingComments ? getAllComments() : [];

// we iterate over comments in parallel with the lines
let commentsIndex = 0;
Expand Down
282 changes: 282 additions & 0 deletions tests/lib/rules/max-len.js
Expand Up @@ -195,6 +195,90 @@ 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 } }
},
{
code: "var jsx = <Foo\n" +
" attr = {a && b/* this line has 57 characters */}\n" +
"></Foo>;",
options: [57],
parserOptions: { ecmaFeatures: { jsx: true } }
},
{
code: "var jsx = <Foo\n" +
" attr = {/* this line has 57 characters */a && b}\n" +
"></Foo>;",
options: [57],
parserOptions: { ecmaFeatures: { jsx: true } }
},
{
code: "var jsx = <Foo\n" +
" attr = \n" +
" {a & b/* this line has 50 characters */}\n" +
"></Foo>;",
options: [50],
parserOptions: { ecmaFeatures: { jsx: true } }
},
{
code: "var jsx = (<>\n" +
" <> </> {/* this line with two separate comments */} {/* have 80 characters */}\n" +
"</>)",
options: [80],
parserOptions: { ecmaFeatures: { jsx: true } }
},
{
code: "var jsx = (<>\n" +
" {/* this line has 37 characters */}\n" +
" <> </> {/* this line with two separate comments */} {/* have 80 characters */}\n" +
"</>)",
options: [37, { ignoreTrailingComments: true }],
parserOptions: { ecmaFeatures: { jsx: true } }
},
{
code: "var jsx = (<>\n" +
" {/* this line has 37 characters */}\n" +
" <> </> {/* this line with two separate comments */} {/* have 80 characters */}\n" +
"</>)",
options: [37, { ignoreComments: true }],
parserOptions: { ecmaFeatures: { jsx: true } }
}
],

Expand Down Expand Up @@ -650,6 +734,204 @@ 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
}
]
},
{
code: "var jsx = <Foo\n" +
" attr = {a && b/* this line has 57 characters */}\n" +
"></Foo>;",
options: [56],
parserOptions: { ecmaFeatures: { jsx: true } },
errors: [
{
messageId: "max",
data: { lineLength: 57, maxLength: 56 },
type: "Program",
line: 2,
column: 1
}
]
},
{
code: "var jsx = <Foo\n" +
" attr = {/* this line has 57 characters */a && b}\n" +
"></Foo>;",
options: [56],
parserOptions: { ecmaFeatures: { jsx: true } },
errors: [
{
messageId: "max",
data: { lineLength: 57, maxLength: 56 },
type: "Program",
line: 2,
column: 1
}
]
},
{
code: "var jsx = <Foo\n" +
" attr = {a & b/* this line has 56 characters */}\n" +
"></Foo>;",
options: [55, { ignoreTrailingComments: true }],
parserOptions: { ecmaFeatures: { jsx: true } },
errors: [
{
messageId: "max",
data: { lineLength: 56, maxLength: 55 },
type: "Program",
line: 2,
column: 1
}
]
},
{
code: "var jsx = <Foo\n" +
" attr = \n" +
" {a & b /* this line has 51 characters */}\n" +
"></Foo>;",
options: [30, { comments: 44 }],
parserOptions: { ecmaFeatures: { jsx: true } },
errors: [
{
messageId: "max",
data: { lineLength: 51, maxLength: 30 },
type: "Program",
line: 3,
column: 1
}
]
},
{
code: "var jsx = (<>\n" +
" {/* this line has 37 characters */}\n" +
" <> </> {/* this line with two separate comments */} {/* have 80 characters */}\n" +
"</>)",
options: [79],
parserOptions: { ecmaFeatures: { jsx: true } },
errors: [
{
messageId: "max",
data: { lineLength: 80, maxLength: 79 },
type: "Program",
line: 3,
column: 1
}
]
},
{
code: "var jsx = (<>\n" +
" <> </> {/* this line with two separate comments */} {/* have 87 characters */} <> </>\n" +
"</>)",
options: [85, { ignoreTrailingComments: true }],
parserOptions: { ecmaFeatures: { jsx: true } },
errors: [
{
messageId: "max",
data: { lineLength: 87, maxLength: 85 },
type: "Program",
line: 2,
column: 1
}
]
},
{
code: "var jsx = (<>\n" +
" {/* this line has 37 characters */}\n" +
" <> </> {/* this line with two separate comments */} {/* have 87 characters */} <> </>\n" +
"</>)",
options: [37, { ignoreComments: true }],
parserOptions: { ecmaFeatures: { jsx: true } },
errors: [
{
messageId: "max",
data: { lineLength: 87, maxLength: 37 },
type: "Program",
line: 3,
column: 1
}
]
}
]
});