1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | module.exports = {
|
13 | meta: {
|
14 | type: "suggestion",
|
15 |
|
16 | docs: {
|
17 | description: "disallow octal escape sequences in string literals",
|
18 | category: "Best Practices",
|
19 | recommended: false,
|
20 | url: "https://eslint.org/docs/rules/no-octal-escape"
|
21 | },
|
22 |
|
23 | schema: []
|
24 | },
|
25 |
|
26 | create(context) {
|
27 |
|
28 | return {
|
29 |
|
30 | Literal(node) {
|
31 | if (typeof node.value !== "string") {
|
32 | return;
|
33 | }
|
34 |
|
35 | const match = node.raw.match(/^([^\\]|\\[^0-7])*\\([0-3][0-7]{1,2}|[4-7][0-7]|[0-7])/u);
|
36 |
|
37 | if (match) {
|
38 | const octalDigit = match[2];
|
39 |
|
40 |
|
41 | if (match[2] !== "0" || typeof match[3] !== "undefined") {
|
42 | context.report({ node, message: "Don't use octal: '\\{{octalDigit}}'. Use '\\u....' instead.", data: { octalDigit } });
|
43 | }
|
44 | }
|
45 | }
|
46 |
|
47 | };
|
48 |
|
49 | }
|
50 | };
|