summaryrefslogtreecommitdiff
path: root/tools/eslint/lib/rules/max-len.js
diff options
context:
space:
mode:
Diffstat (limited to 'tools/eslint/lib/rules/max-len.js')
-rw-r--r--tools/eslint/lib/rules/max-len.js92
1 files changed, 89 insertions, 3 deletions
diff --git a/tools/eslint/lib/rules/max-len.js b/tools/eslint/lib/rules/max-len.js
index e5add9be0a..8ef12b4cd8 100644
--- a/tools/eslint/lib/rules/max-len.js
+++ b/tools/eslint/lib/rules/max-len.js
@@ -30,9 +30,15 @@ const OPTIONS_SCHEMA = {
ignoreComments: {
type: "boolean"
},
+ ignoreStrings: {
+ type: "boolean"
+ },
ignoreUrls: {
type: "boolean"
},
+ ignoreTemplateLiterals: {
+ type: "boolean"
+ },
ignoreTrailingComments: {
type: "boolean"
}
@@ -121,6 +127,8 @@ module.exports = {
const maxLength = options.code || 80,
tabWidth = options.tabWidth || 4,
ignoreComments = options.ignoreComments || false,
+ ignoreStrings = options.ignoreStrings || false,
+ ignoreTemplateLiterals = options.ignoreTemplateLiterals || false,
ignoreTrailingComments = options.ignoreTrailingComments || options.ignoreComments || false,
ignoreUrls = options.ignoreUrls || false,
maxCommentLength = options.comments;
@@ -180,6 +188,59 @@ module.exports = {
}
/**
+ * Ensure that an array exists at [key] on `object`, and add `value` to it.
+ *
+ * @param {Object} object the object to mutate
+ * @param {string} key the object's key
+ * @param {*} value the value to add
+ * @returns {void}
+ * @private
+ */
+ function ensureArrayAndPush(object, key, value) {
+ if (!Array.isArray(object[key])) {
+ object[key] = [];
+ }
+ object[key].push(value);
+ }
+
+ /**
+ * Retrieves an array containing all strings (" or ') in the source code.
+ *
+ * @returns {ASTNode[]} An array of string nodes.
+ */
+ function getAllStrings() {
+ return sourceCode.ast.tokens.filter(function(token) {
+ return token.type === "String";
+ });
+ }
+
+ /**
+ * Retrieves an array containing all template literals in the source code.
+ *
+ * @returns {ASTNode[]} An array of template literal nodes.
+ */
+ function getAllTemplateLiterals() {
+ return sourceCode.ast.tokens.filter(function(token) {
+ return token.type === "Template";
+ });
+ }
+
+
+ /**
+ * A reducer to group an AST node by line number, both start and end.
+ *
+ * @param {Object} acc the accumulator
+ * @param {ASTNode} node the AST node in question
+ * @returns {Object} the modified accumulator
+ * @private
+ */
+ function groupByLineNumber(acc, node) {
+ ensureArrayAndPush(acc, node.loc.start.line, node);
+ ensureArrayAndPush(acc, node.loc.end.line, node);
+ return acc;
+ }
+
+ /**
* Check the program for max length
* @param {ASTNode} node Node to examine
* @returns {void}
@@ -196,6 +257,12 @@ module.exports = {
// we iterate over comments in parallel with the lines
let commentsIndex = 0;
+ const strings = getAllStrings(sourceCode);
+ const stringsByLine = strings.reduce(groupByLineNumber, {});
+
+ const templateLiterals = getAllTemplateLiterals(sourceCode);
+ const templateLiteralsByLine = templateLiterals.reduce(groupByLineNumber, {});
+
lines.forEach(function(line, i) {
// i is zero-indexed, line numbers are one-indexed
@@ -229,7 +296,10 @@ module.exports = {
}
}
if (ignorePattern && ignorePattern.test(line) ||
- ignoreUrls && URL_REGEXP.test(line)) {
+ ignoreUrls && URL_REGEXP.test(line) ||
+ ignoreStrings && stringsByLine[lineNumber] ||
+ ignoreTemplateLiterals && templateLiteralsByLine[lineNumber]
+ ) {
// ignore this line
return;
@@ -242,9 +312,25 @@ module.exports = {
}
if (lineIsComment && lineLength > maxCommentLength) {
- context.report(node, { line: lineNumber, column: 0 }, "Line " + (i + 1) + " exceeds the maximum comment line length of " + maxCommentLength + ".");
+ context.report({
+ node,
+ loc: { line: lineNumber, column: 0 },
+ message: "Line {{lineNumber}} exceeds the maximum comment line length of {{maxCommentLength}}.",
+ data: {
+ lineNumber: i + 1,
+ maxCommentLength
+ }
+ });
} else if (lineLength > maxLength) {
- context.report(node, { line: lineNumber, column: 0 }, "Line " + (i + 1) + " exceeds the maximum line length of " + maxLength + ".");
+ context.report({
+ node,
+ loc: { line: lineNumber, column: 0 },
+ message: "Line {{lineNumber}} exceeds the maximum line length of {{maxLength}}.",
+ data: {
+ lineNumber: i + 1,
+ maxLength
+ }
+ });
}
});
}