2024-08-09 12:04:48 +00:00
|
|
|
/**
|
|
|
|
* @fileoverview Rule to forbid control characters from regular expressions.
|
|
|
|
* @author Nicholas C. Zakas
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2024-08-21 06:34:30 +00:00
|
|
|
const RegExpValidator = require("@eslint-community/regexpp").RegExpValidator;
|
2024-08-09 12:04:48 +00:00
|
|
|
const collector = new (class {
|
|
|
|
constructor() {
|
|
|
|
this._source = "";
|
|
|
|
this._controlChars = [];
|
|
|
|
this._validator = new RegExpValidator(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
onPatternEnter() {
|
2024-08-21 06:34:30 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* `RegExpValidator` may parse the pattern twice in one `validatePattern`.
|
|
|
|
* So `this._controlChars` should be cleared here as well.
|
|
|
|
*
|
|
|
|
* For example, the `/(?<a>\x1f)/` regex will parse the pattern twice.
|
|
|
|
* This is based on the content described in Annex B.
|
|
|
|
* If the regex contains a `GroupName` and the `u` flag is not used, `ParseText` will be called twice.
|
|
|
|
* See https://tc39.es/ecma262/2023/multipage/additional-ecmascript-features-for-web-browsers.html#sec-parsepattern-annexb
|
|
|
|
*/
|
2024-08-09 12:04:48 +00:00
|
|
|
this._controlChars = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
onCharacter(start, end, cp) {
|
|
|
|
if (cp >= 0x00 &&
|
|
|
|
cp <= 0x1F &&
|
|
|
|
(
|
|
|
|
this._source.codePointAt(start) === cp ||
|
|
|
|
this._source.slice(start, end).startsWith("\\x") ||
|
|
|
|
this._source.slice(start, end).startsWith("\\u")
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
this._controlChars.push(`\\x${`0${cp.toString(16)}`.slice(-2)}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-21 06:34:30 +00:00
|
|
|
collectControlChars(regexpStr, flags) {
|
|
|
|
const uFlag = typeof flags === "string" && flags.includes("u");
|
|
|
|
const vFlag = typeof flags === "string" && flags.includes("v");
|
|
|
|
|
|
|
|
this._controlChars = [];
|
|
|
|
this._source = regexpStr;
|
|
|
|
|
2024-08-09 12:04:48 +00:00
|
|
|
try {
|
2024-08-21 06:34:30 +00:00
|
|
|
this._validator.validatePattern(regexpStr, void 0, void 0, { unicode: uFlag, unicodeSets: vFlag }); // Call onCharacter hook
|
2024-08-09 12:04:48 +00:00
|
|
|
} catch {
|
|
|
|
|
|
|
|
// Ignore syntax errors in RegExp.
|
|
|
|
}
|
|
|
|
return this._controlChars;
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Rule Definition
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2024-08-21 06:34:30 +00:00
|
|
|
/** @type {import('../shared/types').Rule} */
|
2024-08-09 12:04:48 +00:00
|
|
|
module.exports = {
|
|
|
|
meta: {
|
|
|
|
type: "problem",
|
|
|
|
|
|
|
|
docs: {
|
2024-08-21 06:34:30 +00:00
|
|
|
description: "Disallow control characters in regular expressions",
|
2024-08-09 12:04:48 +00:00
|
|
|
recommended: true,
|
2024-08-21 06:34:30 +00:00
|
|
|
url: "https://eslint.org/docs/latest/rules/no-control-regex"
|
2024-08-09 12:04:48 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
schema: [],
|
|
|
|
|
|
|
|
messages: {
|
|
|
|
unexpected: "Unexpected control character(s) in regular expression: {{controlChars}}."
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
create(context) {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the regex expression
|
2024-08-21 06:34:30 +00:00
|
|
|
* @param {ASTNode} node `Literal` node to evaluate
|
|
|
|
* @returns {{ pattern: string, flags: string | null } | null} Regex if found (the given node is either a regex literal
|
|
|
|
* or a string literal that is the pattern argument of a RegExp constructor call). Otherwise `null`. If flags cannot be determined,
|
|
|
|
* the `flags` property will be `null`.
|
2024-08-09 12:04:48 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2024-08-21 06:34:30 +00:00
|
|
|
function getRegExp(node) {
|
2024-08-09 12:04:48 +00:00
|
|
|
if (node.regex) {
|
2024-08-21 06:34:30 +00:00
|
|
|
return node.regex;
|
2024-08-09 12:04:48 +00:00
|
|
|
}
|
|
|
|
if (typeof node.value === "string" &&
|
|
|
|
(node.parent.type === "NewExpression" || node.parent.type === "CallExpression") &&
|
|
|
|
node.parent.callee.type === "Identifier" &&
|
|
|
|
node.parent.callee.name === "RegExp" &&
|
|
|
|
node.parent.arguments[0] === node
|
|
|
|
) {
|
2024-08-21 06:34:30 +00:00
|
|
|
const pattern = node.value;
|
|
|
|
const flags =
|
|
|
|
node.parent.arguments.length > 1 &&
|
|
|
|
node.parent.arguments[1].type === "Literal" &&
|
|
|
|
typeof node.parent.arguments[1].value === "string"
|
|
|
|
? node.parent.arguments[1].value
|
|
|
|
: null;
|
|
|
|
|
|
|
|
return { pattern, flags };
|
2024-08-09 12:04:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
Literal(node) {
|
2024-08-21 06:34:30 +00:00
|
|
|
const regExp = getRegExp(node);
|
2024-08-09 12:04:48 +00:00
|
|
|
|
2024-08-21 06:34:30 +00:00
|
|
|
if (regExp) {
|
|
|
|
const { pattern, flags } = regExp;
|
|
|
|
const controlCharacters = collector.collectControlChars(pattern, flags);
|
2024-08-09 12:04:48 +00:00
|
|
|
|
|
|
|
if (controlCharacters.length > 0) {
|
|
|
|
context.report({
|
|
|
|
node,
|
|
|
|
messageId: "unexpected",
|
|
|
|
data: {
|
|
|
|
controlChars: controlCharacters.join(", ")
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|