UNPKG

1.36 kBJavaScriptView Raw
1/**
2 * @fileoverview Rule to flag octal escape sequences in string literals.
3 * @author Ian Christian Myers
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Rule Definition
10//------------------------------------------------------------------------------
11
12module.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 // \0 is actually not considered an octal
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};