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 all 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
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;
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,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