123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- "use strict";
- const astUtils = require("./utils/ast-utils");
- function union(setA, setB) {
- return new Set(function *() {
- yield* setA;
- yield* setB;
- }());
- }
- const VALID_STRING_ESCAPES = union(new Set("\\nrvtbfux"), astUtils.LINEBREAKS);
- const REGEX_GENERAL_ESCAPES = new Set("\\bcdDfnpPrsStvwWxu0123456789]");
- const REGEX_NON_CHARCLASS_ESCAPES = union(REGEX_GENERAL_ESCAPES, new Set("^/.$*+?[{}|()Bk"));
- function parseRegExp(regExpText) {
- const charList = [];
- regExpText.split("").reduce((state, char, index) => {
- if (!state.escapeNextChar) {
- if (char === "\\") {
- return Object.assign(state, { escapeNextChar: true });
- }
- if (char === "[" && !state.inCharClass) {
- return Object.assign(state, { inCharClass: true, startingCharClass: true });
- }
- if (char === "]" && state.inCharClass) {
- if (charList.length && charList[charList.length - 1].inCharClass) {
- charList[charList.length - 1].endsCharClass = true;
- }
- return Object.assign(state, { inCharClass: false, startingCharClass: false });
- }
- }
- charList.push({
- text: char,
- index,
- escaped: state.escapeNextChar,
- inCharClass: state.inCharClass,
- startsCharClass: state.startingCharClass,
- endsCharClass: false
- });
- return Object.assign(state, { escapeNextChar: false, startingCharClass: false });
- }, { escapeNextChar: false, inCharClass: false, startingCharClass: false });
- return charList;
- }
- module.exports = {
- meta: {
- type: "suggestion",
- docs: {
- description: "disallow unnecessary escape characters",
- category: "Best Practices",
- recommended: true,
- url: "https://eslint.org/docs/rules/no-useless-escape",
- suggestion: true
- },
- messages: {
- unnecessaryEscape: "Unnecessary escape character: \\{{character}}.",
- removeEscape: "Remove the `\\`. This maintains the current functionality.",
- escapeBackslash: "Replace the `\\` with `\\\\` to include the actual backslash character."
- },
- schema: []
- },
- create(context) {
- const sourceCode = context.getSourceCode();
-
- function report(node, startOffset, character) {
- const rangeStart = node.range[0] + startOffset;
- const range = [rangeStart, rangeStart + 1];
- const start = sourceCode.getLocFromIndex(rangeStart);
- context.report({
- node,
- loc: {
- start,
- end: { line: start.line, column: start.column + 1 }
- },
- messageId: "unnecessaryEscape",
- data: { character },
- suggest: [
- {
- messageId: "removeEscape",
- fix(fixer) {
- return fixer.removeRange(range);
- }
- },
- {
- messageId: "escapeBackslash",
- fix(fixer) {
- return fixer.insertTextBeforeRange(range, "\\");
- }
- }
- ]
- });
- }
-
- function validateString(node, match) {
- const isTemplateElement = node.type === "TemplateElement";
- const escapedChar = match[0][1];
- let isUnnecessaryEscape = !VALID_STRING_ESCAPES.has(escapedChar);
- let isQuoteEscape;
- if (isTemplateElement) {
- isQuoteEscape = escapedChar === "`";
- if (escapedChar === "$") {
-
- isUnnecessaryEscape = match.input[match.index + 2] !== "{";
- } else if (escapedChar === "{") {
-
- isUnnecessaryEscape = match.input[match.index - 1] !== "$";
- }
- } else {
- isQuoteEscape = escapedChar === node.raw[0];
- }
- if (isUnnecessaryEscape && !isQuoteEscape) {
- report(node, match.index, match[0].slice(1));
- }
- }
-
- function check(node) {
- const isTemplateElement = node.type === "TemplateElement";
- if (
- isTemplateElement &&
- node.parent &&
- node.parent.parent &&
- node.parent.parent.type === "TaggedTemplateExpression" &&
- node.parent === node.parent.parent.quasi
- ) {
-
- return;
- }
- if (typeof node.value === "string" || isTemplateElement) {
-
- if (node.parent.type === "JSXAttribute" || node.parent.type === "JSXElement" || node.parent.type === "JSXFragment") {
- return;
- }
- const value = isTemplateElement ? sourceCode.getText(node) : node.raw;
- const pattern = /\\[^\d]/gu;
- let match;
- while ((match = pattern.exec(value))) {
- validateString(node, match);
- }
- } else if (node.regex) {
- parseRegExp(node.regex.pattern)
-
- .filter(charInfo => !(charInfo.text === "-" && charInfo.inCharClass && !charInfo.startsCharClass && !charInfo.endsCharClass))
-
- .filter(charInfo => !(charInfo.text === "^" && charInfo.startsCharClass))
-
- .filter(charInfo => charInfo.escaped)
-
- .filter(charInfo => !(charInfo.inCharClass ? REGEX_GENERAL_ESCAPES : REGEX_NON_CHARCLASS_ESCAPES).has(charInfo.text))
-
- .forEach(charInfo => report(node, charInfo.index, charInfo.text));
- }
- }
- return {
- Literal: check,
- TemplateElement: check
- };
- }
- };
|