UNPKG

1.33 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 docs: {
15 description: "disallow octal escape sequences in string literals",
16 category: "Best Practices",
17 recommended: false,
18 url: "https://eslint.org/docs/rules/no-octal-escape"
19 },
20
21 schema: []
22 },
23
24 create(context) {
25
26 return {
27
28 Literal(node) {
29 if (typeof node.value !== "string") {
30 return;
31 }
32
33 const match = node.raw.match(/^([^\\]|\\[^0-7])*\\([0-3][0-7]{1,2}|[4-7][0-7]|[0-7])/);
34
35 if (match) {
36 const octalDigit = match[2];
37
38 // \0 is actually not considered an octal
39 if (match[2] !== "0" || typeof match[3] !== "undefined") {
40 context.report({ node, message: "Don't use octal: '\\{{octalDigit}}'. Use '\\u....' instead.", data: { octalDigit } });
41 }
42 }
43 }
44
45 };
46
47 }
48};