Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update: fix no-octal-escape false negatives after \0 (#12079)
  • Loading branch information
mdjermanovic authored and mysticatea committed Sep 13, 2019
1 parent 9418fbe commit e38f5fd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
22 changes: 14 additions & 8 deletions lib/rules/no-octal-escape.js
Expand Up @@ -20,7 +20,11 @@ module.exports = {
url: "https://eslint.org/docs/rules/no-octal-escape"
},

schema: []
schema: [],

messages: {
octalEscapeSequence: "Don't use octal: '\\{{sequence}}'. Use '\\u....' instead."
}
},

create(context) {
Expand All @@ -32,15 +36,17 @@ module.exports = {
return;
}

const match = node.raw.match(/^([^\\]|\\[^0-7])*\\([0-3][0-7]{1,2}|[4-7][0-7]|[0-7])/u);
// \0 represents a valid NULL character if it isn't followed by a digit.
const match = node.raw.match(
/^(?:[^\\]|\\.)*?\\([0-3][0-7]{1,2}|[4-7][0-7]|[1-7])/u
);

if (match) {
const octalDigit = match[2];

// \0 is actually not considered an octal
if (match[2] !== "0" || typeof match[3] !== "undefined") {
context.report({ node, message: "Don't use octal: '\\{{octalDigit}}'. Use '\\u....' instead.", data: { octalDigit } });
}
context.report({
node,
messageId: "octalEscapeSequence",
data: { sequence: match[1] }
});
}
}

Expand Down
31 changes: 26 additions & 5 deletions tests/lib/rules/no-octal-escape.js
Expand Up @@ -24,13 +24,34 @@ ruleTester.run("no-octal-escape", rule, {
"var foo = \"\\x51\";",
"var foo = \"foo \\\\251 bar\";",
"var foo = /([abc]) \\1/g;",
"var foo = '\\0';"
"var foo = '\\0';",
"'\\0 '",
"'\\0a'",
"'\\\\1'",
"'\\\\01'",
"'\\08'",
"'\\09'"
],
invalid: [

// Test full message
{ code: "var foo = \"foo \\01 bar\";", errors: [{ message: "Don't use octal: '\\01'. Use '\\u....' instead.", type: "Literal" }] },
{ code: "var foo = \"foo \\251 bar\";", errors: [{ message: "Don't use octal: '\\251'. Use '\\u....' instead.", type: "Literal" }] },
{ code: "var foo = \"\\751\";", errors: [{ message: "Don't use octal: '\\75'. Use '\\u....' instead.", type: "Literal" }] },
{ code: "var foo = \"\\3s51\";", errors: [{ message: "Don't use octal: '\\3'. Use '\\u....' instead.", type: "Literal" }] },
{ code: "var foo = \"\\\\\\751\";", errors: [{ message: "Don't use octal: '\\75'. Use '\\u....' instead.", type: "Literal" }] }

{ code: "var foo = \"foo \\251 bar\";", errors: [{ messageId: "octalEscapeSequence", data: { sequence: "251" }, type: "Literal" }] },
{ code: "var foo = \"\\751\";", errors: [{ messageId: "octalEscapeSequence", data: { sequence: "75" }, type: "Literal" }] },
{ code: "var foo = \"\\3s51\";", errors: [{ messageId: "octalEscapeSequence", data: { sequence: "3" }, type: "Literal" }] },
{ code: "var foo = \"\\\\\\751\";", errors: [{ messageId: "octalEscapeSequence", data: { sequence: "75" }, type: "Literal" }] },
{ code: "'\\0\\1'", errors: [{ messageId: "octalEscapeSequence", data: { sequence: "1" }, type: "Literal" }] },
{ code: "'\\0 \\1'", errors: [{ messageId: "octalEscapeSequence", data: { sequence: "1" }, type: "Literal" }] },
{ code: "'\\0\\01'", errors: [{ messageId: "octalEscapeSequence", data: { sequence: "01" }, type: "Literal" }] },
{ code: "'\\0 \\01'", errors: [{ messageId: "octalEscapeSequence", data: { sequence: "01" }, type: "Literal" }] },
{ code: "'\\08\\1'", errors: [{ messageId: "octalEscapeSequence", data: { sequence: "1" }, type: "Literal" }] },
{ code: "'\\08\\01'", errors: [{ messageId: "octalEscapeSequence", data: { sequence: "01" }, type: "Literal" }] },

// Only the first one is reported
{ code: "'\\01\\02'", errors: [{ messageId: "octalEscapeSequence", data: { sequence: "01" }, type: "Literal" }] },
{ code: "'\\02\\01'", errors: [{ messageId: "octalEscapeSequence", data: { sequence: "02" }, type: "Literal" }] },
{ code: "'\\01\\2'", errors: [{ messageId: "octalEscapeSequence", data: { sequence: "01" }, type: "Literal" }] },
{ code: "'\\2\\01'", errors: [{ messageId: "octalEscapeSequence", data: { sequence: "2" }, type: "Literal" }] }
]
});

0 comments on commit e38f5fd

Please sign in to comment.