mirror of https://github.com/lukechilds/node.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
98 lines
2.8 KiB
98 lines
2.8 KiB
/**
|
|
* @fileoverview Rule to count multiple spaces in regular expressions
|
|
* @author Matt DuVall <http://www.mattduvall.com/>
|
|
*/
|
|
|
|
"use strict";
|
|
|
|
//------------------------------------------------------------------------------
|
|
// Rule Definition
|
|
//------------------------------------------------------------------------------
|
|
|
|
module.exports = {
|
|
meta: {
|
|
docs: {
|
|
description: "disallow multiple spaces in regular expressions",
|
|
category: "Possible Errors",
|
|
recommended: true
|
|
},
|
|
|
|
schema: []
|
|
},
|
|
|
|
create(context) {
|
|
const sourceCode = context.getSourceCode();
|
|
|
|
/**
|
|
* Validate regular expressions
|
|
* @param {ASTNode} node node to validate
|
|
* @param {string} value regular expression to validate
|
|
* @returns {void}
|
|
* @private
|
|
*/
|
|
function checkRegex(node, value) {
|
|
const multipleSpacesRegex = /( {2,})+?/,
|
|
regexResults = multipleSpacesRegex.exec(value);
|
|
|
|
if (regexResults !== null) {
|
|
context.report({
|
|
node,
|
|
message: "Spaces are hard to count. Use {{{count}}}.",
|
|
data: {
|
|
count: regexResults[0].length
|
|
}
|
|
});
|
|
|
|
/*
|
|
* TODO: (platinumazure) Fix message to use rule message
|
|
* substitution when api.report is fixed in lib/eslint.js.
|
|
*/
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Validate regular expression literals
|
|
* @param {ASTNode} node node to validate
|
|
* @returns {void}
|
|
* @private
|
|
*/
|
|
function checkLiteral(node) {
|
|
const token = sourceCode.getFirstToken(node),
|
|
nodeType = token.type,
|
|
nodeValue = token.value;
|
|
|
|
if (nodeType === "RegularExpression") {
|
|
checkRegex(node, nodeValue);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Check if node is a string
|
|
* @param {ASTNode} node node to evaluate
|
|
* @returns {boolean} True if its a string
|
|
* @private
|
|
*/
|
|
function isString(node) {
|
|
return node && node.type === "Literal" && typeof node.value === "string";
|
|
}
|
|
|
|
/**
|
|
* Validate strings passed to the RegExp constructor
|
|
* @param {ASTNode} node node to validate
|
|
* @returns {void}
|
|
* @private
|
|
*/
|
|
function checkFunction(node) {
|
|
if (node.callee.type === "Identifier" && node.callee.name === "RegExp" && isString(node.arguments[0])) {
|
|
checkRegex(node, node.arguments[0].value);
|
|
}
|
|
}
|
|
|
|
return {
|
|
Literal: checkLiteral,
|
|
CallExpression: checkFunction,
|
|
NewExpression: checkFunction
|
|
};
|
|
|
|
}
|
|
};
|
|
|