]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-nonoctal-decimal-escape.js
3edd269faeb33deb1c263296dbcb631ecba06de9
2 * @fileoverview Rule to disallow `\8` and `\9` escape sequences in string literals.
3 * @author Milos Djermanovic
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const QUICK_TEST_REGEX
= /\\[89]/u;
15 * Returns unicode escape sequence that represents the given character.
16 * @param {string} character A single code unit.
17 * @returns {string} "\uXXXX" sequence.
19 function getUnicodeEscape(character
) {
20 return `\\u${character.charCodeAt(0).toString(16).padStart(4, "0")}`;
23 //------------------------------------------------------------------------------
25 //------------------------------------------------------------------------------
27 /** @type {import('../shared/types').Rule} */
33 description
: "disallow `\\8` and `\\9` escape sequences in string literals",
35 url
: "https://eslint.org/docs/rules/no-nonoctal-decimal-escape"
43 decimalEscape
: "Don't use '{{decimalEscape}}' escape sequence.",
46 refactor
: "Replace '{{original}}' with '{{replacement}}'. This maintains the current functionality.",
47 escapeBackslash
: "Replace '{{original}}' with '{{replacement}}' to include the actual backslash character."
52 const sourceCode
= context
.getSourceCode();
55 * Creates a new Suggestion object.
56 * @param {string} messageId "refactor" or "escapeBackslash".
57 * @param {int[]} range The range to replace.
58 * @param {string} replacement New text for the range.
59 * @returns {Object} Suggestion
61 function createSuggestion(messageId
, range
, replacement
) {
65 original
: sourceCode
.getText().slice(...range
),
69 return fixer
.replaceTextRange(range
, replacement
);
76 if (typeof node
.value
!== "string") {
80 if (!QUICK_TEST_REGEX
.test(node
.raw
)) {
84 const regex
= /(?:[^\\]|(?<previousEscape>\\.))*?(?<decimalEscape>\\[89])/suy;
87 while ((match
= regex
.exec(node
.raw
))) {
88 const { previousEscape
, decimalEscape
} = match
.groups
;
89 const decimalEscapeRangeEnd
= node
.range
[0] + match
.index
+ match
[0].length
;
90 const decimalEscapeRangeStart
= decimalEscapeRangeEnd
- decimalEscape
.length
;
91 const decimalEscapeRange
= [decimalEscapeRangeStart
, decimalEscapeRangeEnd
];
94 // When `regex` is matched, `previousEscape` can only capture characters adjacent to `decimalEscape`
95 if (previousEscape
=== "\\0") {
98 * Now we have a NULL escape "\0" immediately followed by a decimal escape, e.g.: "\0\8".
99 * Fixing this to "\08" would turn "\0" into a legacy octal escape. To avoid producing
100 * an octal escape while fixing a decimal escape, we provide different suggestions.
103 createSuggestion( // "\0\8" -> "\u00008"
105 [decimalEscapeRangeStart
- previousEscape
.length
, decimalEscapeRangeEnd
],
106 `${getUnicodeEscape("\0")}${decimalEscape[1]}`
108 createSuggestion( // "\8" -> "\u0038"
111 getUnicodeEscape(decimalEscape
[1])
116 createSuggestion( // "\8" -> "8"
125 createSuggestion( // "\8" -> "\\8"
135 start
: sourceCode
.getLocFromIndex(decimalEscapeRangeStart
),
136 end
: sourceCode
.getLocFromIndex(decimalEscapeRangeEnd
)
138 messageId
: "decimalEscape",