Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update: fix counting jsx comment len in max-len (fixes #12213) (#12661)
* Update: fix counting jsx comment len in max-len (fixes #12213)

* Add test cases

* map comments with desired nodes

* push a unique node only

* fix checking node, add more test cases

* add more test cases
  • Loading branch information
yeonjuan authored and kaicataldo committed Jan 9, 2020
1 parent e632c31 commit 14b42c3
Show file tree
Hide file tree
Showing 2 changed files with 492 additions and 1 deletion.
44 changes: 43 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;
}

/**
* Gets the line after the comment and any remaining trailing whitespace is
* stripped.
Expand Down Expand Up @@ -252,6 +267,33 @@ 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() {
const comments = [];

sourceCode.getAllComments()
.forEach(commentNode => {
const containingNode = sourceCode.getNodeByRangeIndex(commentNode.range[0]);

if (isJSXEmptyExpressionInSingleLineContainer(containingNode)) {

// push a unique node only
if (comments[comments.length - 1] !== containingNode.parent) {
comments.push(containingNode.parent);
}
} else {
comments.push(commentNode);
}
});

return comments;
}

/**
* Check the program for max length
* @param {ASTNode} node Node to examine
Expand All @@ -264,7 +306,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

0 comments on commit 14b42c3

Please sign in to comment.