Skip to content

Commit

Permalink
Fix: Make max-len ignoreStrings ignore JSXText (fixes #9954) (#9985)
Browse files Browse the repository at this point in the history
* Fix: Make max-len ignoreStrings ignore JSXText (fixes #9954)

* Ignore JSXText token only if it is a child of a JSXAttribute node
  • Loading branch information
rachx authored and not-an-aardvark committed Feb 23, 2018
1 parent 8c237d8 commit aea07dc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/rules/max-len.js
Expand Up @@ -213,7 +213,8 @@ module.exports = {
* @returns {ASTNode[]} An array of string nodes.
*/
function getAllStrings() {
return sourceCode.ast.tokens.filter(token => token.type === "String");
return sourceCode.ast.tokens.filter(token => (token.type === "String" ||
(token.type === "JSXText" && sourceCode.getNodeByRangeIndex(token.range[0] - 1).type === "JSXAttribute")));
}

/**
Expand Down
18 changes: 18 additions & 0 deletions tests/lib/rules/max-len.js
Expand Up @@ -109,6 +109,11 @@ ruleTester.run("max-len", rule, {
code: "var str = \"this is a very long string\\\nwith continuation\\\nand with another very very long continuation\\\nand ending\";",
options: [29, 4, { ignoreStrings: true }]
},
{
code: "var foo = <div className=\"this is a very long string\"></div>;",
options: [29, 4, { ignoreStrings: true }],
parserOptions: { ecmaFeatures: { jsx: true } }
},
{
code: "var foo = veryLongIdentifier;\nvar bar = `this is a very long string`;",
options: [29, 4, { ignoreTemplateLiterals: true }],
Expand Down Expand Up @@ -553,6 +558,19 @@ ruleTester.run("max-len", rule, {
}
]
},
{
code: "var foo = <div>this is a very very very long string</div>;",
options: [29, 4, { ignoreStrings: true }],
parserOptions: { ecmaFeatures: { jsx: true } },
errors: [
{
message: "Line 1 exceeds the maximum line length of 29.",
type: "Program",
line: 1,
column: 1
}
]
},

// Multi-code-point unicode glyphs
{
Expand Down

0 comments on commit aea07dc

Please sign in to comment.