1 | /**
|
---|
2 | * @fileoverview Rule to enforce the use of `u` flag on RegExp.
|
---|
3 | * @author Toru Nagashima
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | //------------------------------------------------------------------------------
|
---|
9 | // Requirements
|
---|
10 | //------------------------------------------------------------------------------
|
---|
11 |
|
---|
12 | const {
|
---|
13 | CALL,
|
---|
14 | CONSTRUCT,
|
---|
15 | ReferenceTracker,
|
---|
16 | getStringIfConstant
|
---|
17 | } = require("@eslint-community/eslint-utils");
|
---|
18 | const astUtils = require("./utils/ast-utils.js");
|
---|
19 | const { isValidWithUnicodeFlag } = require("./utils/regular-expressions");
|
---|
20 |
|
---|
21 | //------------------------------------------------------------------------------
|
---|
22 | // Rule Definition
|
---|
23 | //------------------------------------------------------------------------------
|
---|
24 |
|
---|
25 | /** @type {import('../shared/types').Rule} */
|
---|
26 | module.exports = {
|
---|
27 | meta: {
|
---|
28 | type: "suggestion",
|
---|
29 |
|
---|
30 | docs: {
|
---|
31 | description: "Enforce the use of `u` or `v` flag on RegExp",
|
---|
32 | recommended: false,
|
---|
33 | url: "https://eslint.org/docs/latest/rules/require-unicode-regexp"
|
---|
34 | },
|
---|
35 |
|
---|
36 | hasSuggestions: true,
|
---|
37 |
|
---|
38 | messages: {
|
---|
39 | addUFlag: "Add the 'u' flag.",
|
---|
40 | requireUFlag: "Use the 'u' flag."
|
---|
41 | },
|
---|
42 |
|
---|
43 | schema: []
|
---|
44 | },
|
---|
45 |
|
---|
46 | create(context) {
|
---|
47 |
|
---|
48 | const sourceCode = context.sourceCode;
|
---|
49 |
|
---|
50 | return {
|
---|
51 | "Literal[regex]"(node) {
|
---|
52 | const flags = node.regex.flags || "";
|
---|
53 |
|
---|
54 | if (!flags.includes("u") && !flags.includes("v")) {
|
---|
55 | context.report({
|
---|
56 | messageId: "requireUFlag",
|
---|
57 | node,
|
---|
58 | suggest: isValidWithUnicodeFlag(context.languageOptions.ecmaVersion, node.regex.pattern)
|
---|
59 | ? [
|
---|
60 | {
|
---|
61 | fix(fixer) {
|
---|
62 | return fixer.insertTextAfter(node, "u");
|
---|
63 | },
|
---|
64 | messageId: "addUFlag"
|
---|
65 | }
|
---|
66 | ]
|
---|
67 | : null
|
---|
68 | });
|
---|
69 | }
|
---|
70 | },
|
---|
71 |
|
---|
72 | Program(node) {
|
---|
73 | const scope = sourceCode.getScope(node);
|
---|
74 | const tracker = new ReferenceTracker(scope);
|
---|
75 | const trackMap = {
|
---|
76 | RegExp: { [CALL]: true, [CONSTRUCT]: true }
|
---|
77 | };
|
---|
78 |
|
---|
79 | for (const { node: refNode } of tracker.iterateGlobalReferences(trackMap)) {
|
---|
80 | const [patternNode, flagsNode] = refNode.arguments;
|
---|
81 |
|
---|
82 | if (patternNode && patternNode.type === "SpreadElement") {
|
---|
83 | continue;
|
---|
84 | }
|
---|
85 | const pattern = getStringIfConstant(patternNode, scope);
|
---|
86 | const flags = getStringIfConstant(flagsNode, scope);
|
---|
87 |
|
---|
88 | if (!flagsNode || (typeof flags === "string" && !flags.includes("u") && !flags.includes("v"))) {
|
---|
89 | context.report({
|
---|
90 | messageId: "requireUFlag",
|
---|
91 | node: refNode,
|
---|
92 | suggest: typeof pattern === "string" && isValidWithUnicodeFlag(context.languageOptions.ecmaVersion, pattern)
|
---|
93 | ? [
|
---|
94 | {
|
---|
95 | fix(fixer) {
|
---|
96 | if (flagsNode) {
|
---|
97 | if ((flagsNode.type === "Literal" && typeof flagsNode.value === "string") || flagsNode.type === "TemplateLiteral") {
|
---|
98 | const flagsNodeText = sourceCode.getText(flagsNode);
|
---|
99 |
|
---|
100 | return fixer.replaceText(flagsNode, [
|
---|
101 | flagsNodeText.slice(0, flagsNodeText.length - 1),
|
---|
102 | flagsNodeText.slice(flagsNodeText.length - 1)
|
---|
103 | ].join("u"));
|
---|
104 | }
|
---|
105 |
|
---|
106 | // We intentionally don't suggest concatenating + "u" to non-literals
|
---|
107 | return null;
|
---|
108 | }
|
---|
109 |
|
---|
110 | const penultimateToken = sourceCode.getLastToken(refNode, { skip: 1 }); // skip closing parenthesis
|
---|
111 |
|
---|
112 | return fixer.insertTextAfter(
|
---|
113 | penultimateToken,
|
---|
114 | astUtils.isCommaToken(penultimateToken)
|
---|
115 | ? ' "u",'
|
---|
116 | : ', "u"'
|
---|
117 | );
|
---|
118 | },
|
---|
119 | messageId: "addUFlag"
|
---|
120 | }
|
---|
121 | ]
|
---|
122 | : null
|
---|
123 | });
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|
127 | };
|
---|
128 | }
|
---|
129 | };
|
---|