• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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        messages: {
26            octalEscapeSequence: "Don't use octal: '\\{{sequence}}'. Use '\\u....' instead."
27        }
28    },
29
30    create(context) {
31
32        return {
33
34            Literal(node) {
35                if (typeof node.value !== "string") {
36                    return;
37                }
38
39                // \0 represents a valid NULL character if it isn't followed by a digit.
40                const match = node.raw.match(
41                    /^(?:[^\\]|\\.)*?\\([0-3][0-7]{1,2}|[4-7][0-7]|0(?=[89])|[1-7])/su
42                );
43
44                if (match) {
45                    context.report({
46                        node,
47                        messageId: "octalEscapeSequence",
48                        data: { sequence: match[1] }
49                    });
50                }
51            }
52
53        };
54
55    }
56};
57