UNPKG

1.26 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 },
19
20 schema: []
21 },
22
23 create(context) {
24
25 return {
26
27 Literal(node) {
28 if (typeof node.value !== "string") {
29 return;
30 }
31
32 const match = node.raw.match(/^([^\\]|\\[^0-7])*\\([0-3][0-7]{1,2}|[4-7][0-7]|[0-7])/);
33
34 if (match) {
35 const octalDigit = match[2];
36
37 // \0 is actually not considered an octal
38 if (match[2] !== "0" || typeof match[3] !== "undefined") {
39 context.report({ node, message: "Don't use octal: '\\{{octalDigit}}'. Use '\\u....' instead.", data: { octalDigit } });
40 }
41 }
42 }
43
44 };
45
46 }
47};