Debian Codegraph @

lib/rules/no-regexp-unicode-property-escapes-2019.js

raw symbols SCIP
1 /**
2 * @author Toru Nagashima <https://github.com/mysticatea>
3 * See LICENSE file in root directory for full license.
4 */
5 "use strict"
6
7 const { RegExpValidator } = require("regexpp")
8 const { getRegExpCalls } = require("../utils")
9
10 const scNamePattern = /^(?:Script(?:_Extensions)?|scx?)$/u
11 const scValuePattern = /^(?:Dogr|Dogra|Gong|Gunjala_Gondi|Hanifi_Rohingya|Maka|Makasar|Medefaidrin|Medf|Old_Sogdian|Rohg|Sogd|Sogdian|Sogo)$/u
12
13 function isNewUnicodePropertyKeyValuePair(key, value) {
14 return scNamePattern.test(key) && scValuePattern.test(value)
15 }
16
17 function isNewBinaryUnicodeProperty(key) {
18 return key === "Extended_Pictographic"
19 }
20
21 /**
22 * Verify a given regular expression.
23 * @param {RuleContext} context The rule context to report.
24 * @param {Node} node The AST node to report.
25 * @param {string} pattern The pattern part of a RegExp.
26 * @param {string} flags The flags part of a RegExp.
27 * @returns {void}
28 */
29 function verify(context, node, pattern, flags) {
30 try {
31 let foundValue = ""
32
33 new RegExpValidator({
34 onUnicodePropertyCharacterSet(start, end, _kind, key, value) {
35 if (foundValue) {
36 return
37 }
38 if (
39 value
40 ? isNewUnicodePropertyKeyValuePair(key, value)
41 : isNewBinaryUnicodeProperty(key)
42 ) {
43 foundValue = pattern.slice(start, end)
44 }
45 },
46 }).validatePattern(pattern, 0, pattern.length, flags.includes("u"))
47
48 if (foundValue) {
49 context.report({
50 node,
51 messageId: "forbidden",
52 data: { value: foundValue },
53 })
54 }
55 } catch (error) {
56 //istanbul ignore else
57 if (error.message.startsWith("Invalid regular expression:")) {
58 return
59 }
60 //istanbul ignore next
61 throw error
62 }
63 }
64
65 module.exports = {
66 meta: {
67 docs: {
68 description:
69 "disallow the new values of RegExp Unicode property escape sequences in ES2019",
70 category: "ES2019",
71 recommended: false,
72 url:
73 "http://mysticatea.github.io/eslint-plugin-es/rules/no-regexp-unicode-property-escapes-2019.html",
74 },
75 fixable: null,
76 messages: {
77 forbidden: "ES2019 '{{value}}' is forbidden.",
78 },
79 schema: [],
80 type: "problem",
81 },
82 create(context) {
83 return {
84 "Literal[regex]"(node) {
85 const { pattern, flags } = node.regex
86 verify(context, node, pattern || "", flags || "")
87 },
88
89 "Program:exit"() {
90 const scope = context.getScope()
91 for (const { node, pattern, flags } of getRegExpCalls(scope)) {
92 verify(context, node, pattern || "", flags || "")
93 }
94 },
95 }
96 },
97 }