google_forms/node_modules/eslint/lib/rules/no-misleading-character-cla...

301 lines
9.9 KiB
JavaScript
Raw Normal View History

2024-08-09 12:04:48 +00:00
/**
* @author Toru Nagashima <https://github.com/mysticatea>
*/
"use strict";
2024-08-21 06:34:30 +00:00
const { CALL, CONSTRUCT, ReferenceTracker, getStringIfConstant } = require("@eslint-community/eslint-utils");
const { RegExpParser, visitRegExpAST } = require("@eslint-community/regexpp");
2024-08-09 12:04:48 +00:00
const { isCombiningCharacter, isEmojiModifier, isRegionalIndicatorSymbol, isSurrogatePair } = require("./utils/unicode");
2024-08-21 06:34:30 +00:00
const astUtils = require("./utils/ast-utils.js");
const { isValidWithUnicodeFlag } = require("./utils/regular-expressions");
2024-08-09 12:04:48 +00:00
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
2024-08-21 06:34:30 +00:00
/**
* @typedef {import('@eslint-community/regexpp').AST.Character} Character
* @typedef {import('@eslint-community/regexpp').AST.CharacterClassElement} CharacterClassElement
*/
2024-08-09 12:04:48 +00:00
/**
* Iterate character sequences of a given nodes.
*
* CharacterClassRange syntax can steal a part of character sequence,
* so this function reverts CharacterClassRange syntax and restore the sequence.
2024-08-21 06:34:30 +00:00
* @param {CharacterClassElement[]} nodes The node list to iterate character sequences.
* @returns {IterableIterator<Character[]>} The list of character sequences.
2024-08-09 12:04:48 +00:00
*/
function *iterateCharacterSequence(nodes) {
2024-08-21 06:34:30 +00:00
/** @type {Character[]} */
2024-08-09 12:04:48 +00:00
let seq = [];
for (const node of nodes) {
switch (node.type) {
case "Character":
2024-08-21 06:34:30 +00:00
seq.push(node);
2024-08-09 12:04:48 +00:00
break;
case "CharacterClassRange":
2024-08-21 06:34:30 +00:00
seq.push(node.min);
2024-08-09 12:04:48 +00:00
yield seq;
2024-08-21 06:34:30 +00:00
seq = [node.max];
2024-08-09 12:04:48 +00:00
break;
case "CharacterSet":
2024-08-21 06:34:30 +00:00
case "CharacterClass": // [[]] nesting character class
case "ClassStringDisjunction": // \q{...}
case "ExpressionCharacterClass": // [A--B]
2024-08-09 12:04:48 +00:00
if (seq.length > 0) {
yield seq;
seq = [];
}
break;
// no default
}
}
if (seq.length > 0) {
yield seq;
}
}
2024-08-21 06:34:30 +00:00
/**
* Checks whether the given character node is a Unicode code point escape or not.
* @param {Character} char the character node to check.
* @returns {boolean} `true` if the character node is a Unicode code point escape.
*/
function isUnicodeCodePointEscape(char) {
return /^\\u\{[\da-f]+\}$/iu.test(char.raw);
}
/**
* Each function returns `true` if it detects that kind of problem.
* @type {Record<string, (chars: Character[]) => boolean>}
*/
2024-08-09 12:04:48 +00:00
const hasCharacterSequence = {
surrogatePairWithoutUFlag(chars) {
2024-08-21 06:34:30 +00:00
return chars.some((c, i) => {
if (i === 0) {
return false;
}
const c1 = chars[i - 1];
return (
isSurrogatePair(c1.value, c.value) &&
!isUnicodeCodePointEscape(c1) &&
!isUnicodeCodePointEscape(c)
);
});
},
surrogatePair(chars) {
return chars.some((c, i) => {
if (i === 0) {
return false;
}
const c1 = chars[i - 1];
return (
isSurrogatePair(c1.value, c.value) &&
(
isUnicodeCodePointEscape(c1) ||
isUnicodeCodePointEscape(c)
)
);
});
2024-08-09 12:04:48 +00:00
},
combiningClass(chars) {
return chars.some((c, i) => (
i !== 0 &&
2024-08-21 06:34:30 +00:00
isCombiningCharacter(c.value) &&
!isCombiningCharacter(chars[i - 1].value)
2024-08-09 12:04:48 +00:00
));
},
emojiModifier(chars) {
return chars.some((c, i) => (
i !== 0 &&
2024-08-21 06:34:30 +00:00
isEmojiModifier(c.value) &&
!isEmojiModifier(chars[i - 1].value)
2024-08-09 12:04:48 +00:00
));
},
regionalIndicatorSymbol(chars) {
return chars.some((c, i) => (
i !== 0 &&
2024-08-21 06:34:30 +00:00
isRegionalIndicatorSymbol(c.value) &&
isRegionalIndicatorSymbol(chars[i - 1].value)
2024-08-09 12:04:48 +00:00
));
},
zwj(chars) {
const lastIndex = chars.length - 1;
return chars.some((c, i) => (
i !== 0 &&
i !== lastIndex &&
2024-08-21 06:34:30 +00:00
c.value === 0x200d &&
chars[i - 1].value !== 0x200d &&
chars[i + 1].value !== 0x200d
2024-08-09 12:04:48 +00:00
));
}
};
const kinds = Object.keys(hasCharacterSequence);
//------------------------------------------------------------------------------
// 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 characters which are made with multiple code points in character class syntax",
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-misleading-character-class"
2024-08-09 12:04:48 +00:00
},
2024-08-21 06:34:30 +00:00
hasSuggestions: true,
2024-08-09 12:04:48 +00:00
schema: [],
messages: {
surrogatePairWithoutUFlag: "Unexpected surrogate pair in character class. Use 'u' flag.",
2024-08-21 06:34:30 +00:00
surrogatePair: "Unexpected surrogate pair in character class.",
2024-08-09 12:04:48 +00:00
combiningClass: "Unexpected combined character in character class.",
emojiModifier: "Unexpected modified Emoji in character class.",
regionalIndicatorSymbol: "Unexpected national flag in character class.",
2024-08-21 06:34:30 +00:00
zwj: "Unexpected joined character sequence in character class.",
suggestUnicodeFlag: "Add unicode 'u' flag to regex."
2024-08-09 12:04:48 +00:00
}
},
create(context) {
2024-08-21 06:34:30 +00:00
const sourceCode = context.sourceCode;
2024-08-09 12:04:48 +00:00
const parser = new RegExpParser();
/**
* Verify a given regular expression.
* @param {Node} node The node to report.
* @param {string} pattern The regular expression pattern to verify.
* @param {string} flags The flags of the regular expression.
2024-08-21 06:34:30 +00:00
* @param {Function} unicodeFixer Fixer for missing "u" flag.
2024-08-09 12:04:48 +00:00
* @returns {void}
*/
2024-08-21 06:34:30 +00:00
function verify(node, pattern, flags, unicodeFixer) {
2024-08-09 12:04:48 +00:00
let patternNode;
try {
patternNode = parser.parsePattern(
pattern,
0,
pattern.length,
2024-08-21 06:34:30 +00:00
{
unicode: flags.includes("u"),
unicodeSets: flags.includes("v")
}
2024-08-09 12:04:48 +00:00
);
} catch {
// Ignore regular expressions with syntax errors
return;
}
2024-08-21 06:34:30 +00:00
const foundKinds = new Set();
2024-08-09 12:04:48 +00:00
visitRegExpAST(patternNode, {
onCharacterClassEnter(ccNode) {
for (const chars of iterateCharacterSequence(ccNode.elements)) {
for (const kind of kinds) {
2024-08-21 06:34:30 +00:00
if (hasCharacterSequence[kind](chars)) {
foundKinds.add(kind);
}
2024-08-09 12:04:48 +00:00
}
}
}
});
2024-08-21 06:34:30 +00:00
for (const kind of foundKinds) {
let suggest;
if (kind === "surrogatePairWithoutUFlag") {
suggest = [{
messageId: "suggestUnicodeFlag",
fix: unicodeFixer
}];
2024-08-09 12:04:48 +00:00
}
2024-08-21 06:34:30 +00:00
context.report({
node,
messageId: kind,
suggest
});
2024-08-09 12:04:48 +00:00
}
}
return {
"Literal[regex]"(node) {
2024-08-21 06:34:30 +00:00
verify(node, node.regex.pattern, node.regex.flags, fixer => {
if (!isValidWithUnicodeFlag(context.languageOptions.ecmaVersion, node.regex.pattern)) {
return null;
}
return fixer.insertTextAfter(node, "u");
});
2024-08-09 12:04:48 +00:00
},
2024-08-21 06:34:30 +00:00
"Program"(node) {
const scope = sourceCode.getScope(node);
2024-08-09 12:04:48 +00:00
const tracker = new ReferenceTracker(scope);
/*
* Iterate calls of RegExp.
* E.g., `new RegExp()`, `RegExp()`, `new window.RegExp()`,
* `const {RegExp: a} = window; new a()`, etc...
*/
2024-08-21 06:34:30 +00:00
for (const { node: refNode } of tracker.iterateGlobalReferences({
2024-08-09 12:04:48 +00:00
RegExp: { [CALL]: true, [CONSTRUCT]: true }
})) {
2024-08-21 06:34:30 +00:00
const [patternNode, flagsNode] = refNode.arguments;
2024-08-09 12:04:48 +00:00
const pattern = getStringIfConstant(patternNode, scope);
const flags = getStringIfConstant(flagsNode, scope);
if (typeof pattern === "string") {
2024-08-21 06:34:30 +00:00
verify(refNode, pattern, flags || "", fixer => {
if (!isValidWithUnicodeFlag(context.languageOptions.ecmaVersion, pattern)) {
return null;
}
if (refNode.arguments.length === 1) {
const penultimateToken = sourceCode.getLastToken(refNode, { skip: 1 }); // skip closing parenthesis
return fixer.insertTextAfter(
penultimateToken,
astUtils.isCommaToken(penultimateToken)
? ' "u",'
: ', "u"'
);
}
if ((flagsNode.type === "Literal" && typeof flagsNode.value === "string") || flagsNode.type === "TemplateLiteral") {
const range = [flagsNode.range[0], flagsNode.range[1] - 1];
return fixer.insertTextAfterRange(range, "u");
}
return null;
});
2024-08-09 12:04:48 +00:00
}
}
}
};
}
};