Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for v flag to regexp/no-useless-escape rule #585

Merged
merged 3 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/six-squids-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-regexp": minor
---

Add support for v flag to `regexp/no-useless-escape` rule
152 changes: 131 additions & 21 deletions lib/rules/no-useless-escape.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import type { RegExpVisitor } from "@eslint-community/regexpp/visitor"
import type { Character } from "@eslint-community/regexpp/ast"
import type {
Character,
CharacterClass,
ExpressionCharacterClass,
} from "@eslint-community/regexpp/ast"
import type { RegExpContext } from "../utils"
import {
createRule,
Expand All @@ -21,13 +25,38 @@ import {
CP_PIPE,
CP_MINUS,
canUnwrapped,
CP_HASH,
CP_PERCENT,
CP_BAN,
CP_AMP,
CP_COMMA,
CP_COLON,
CP_SEMI,
CP_LT,
CP_EQ,
CP_GT,
CP_AT,
CP_TILDE,
CP_BACKTICK,
} from "../utils"

const REGEX_CHAR_CLASS_ESCAPES = new Set([
CP_BACK_SLASH, // \\
CP_CLOSING_BRACKET, // ]
CP_MINUS, // -
])
const REGEX_CLASS_SET_CHAR_CLASS_ESCAPE = new Set([
CP_BACK_SLASH, // \\
CP_SLASH, // /
CP_OPENING_BRACKET, // [
CP_CLOSING_BRACKET, // ]
CP_OPENING_BRACE, // {
CP_CLOSING_BRACE, // }
CP_PIPE, // |
CP_OPENING_PAREN, // (
CP_CLOSING_PAREN, // )
CP_MINUS, // -,
])
const REGEX_ESCAPES = new Set([
CP_BACK_SLASH, // \\
CP_SLASH, // /
Expand All @@ -47,6 +76,33 @@ const REGEX_ESCAPES = new Set([
])

const POTENTIAL_ESCAPE_SEQUENCE = new Set("uxkpP")
const POTENTIAL_ESCAPE_SEQUENCE_FOR_CHAR_CLASS = new Set([
...POTENTIAL_ESCAPE_SEQUENCE,
"q",
])
// A single character set of ClassSetReservedDoublePunctuator.
// && !! ## $$ %% ** ++ ,, .. :: ;; << == >> ?? @@ ^^ `` ~~ are ClassSetReservedDoublePunctuator
const REGEX_CLASS_SET_RESERVED_DOUBLE_PUNCTUATOR = new Set([
CP_BAN, // !
CP_HASH, // #
CP_DOLLAR, // $
CP_PERCENT, // %
CP_AMP, // &
CP_STAR, // *
CP_PLUS, // +
CP_COMMA, // ,
CP_DOT, // .
CP_COLON, // :
CP_SEMI, // ;
CP_LT, // <
CP_EQ, // =
CP_GT, // >
CP_QUESTION, // ?
CP_AT, // @
CP_CARET, // ^
CP_BACKTICK, // `
CP_TILDE, // ~
])

export default createRule("no-useless-escape", {
meta: {
Expand All @@ -68,6 +124,8 @@ export default createRule("no-useless-escape", {
*/
function createVisitor({
node,
flags,
pattern,
getRegexpLocation,
fixReplaceNode,
}: RegExpContext): RegExpVisitor.Handlers {
Expand All @@ -89,37 +147,85 @@ export default createRule("no-useless-escape", {
})
}

let inCharacterClass = false
const characterClassStack: (
| CharacterClass
| ExpressionCharacterClass
)[] = []
return {
onCharacterClassEnter() {
inCharacterClass = true
},
onCharacterClassLeave() {
inCharacterClass = false
},
onCharacterClassEnter: (characterClassNode) =>
characterClassStack.unshift(characterClassNode),
onCharacterClassLeave: () => characterClassStack.shift(),
onExpressionCharacterClassEnter: (characterClassNode) =>
characterClassStack.unshift(characterClassNode),
onExpressionCharacterClassLeave: () =>
characterClassStack.shift(),
onCharacterEnter(cNode) {
if (cNode.raw.startsWith("\\")) {
// escapes
const char = cNode.raw.slice(1)
if (char === String.fromCodePoint(cNode.value)) {
const allowedEscapes = inCharacterClass
? REGEX_CHAR_CLASS_ESCAPES
: REGEX_ESCAPES
const escapedChar = String.fromCodePoint(cNode.value)
if (char === escapedChar) {
let allowedEscapes: Set<number>
if (characterClassStack.length) {
allowedEscapes = flags.unicodeSets
? REGEX_CLASS_SET_CHAR_CLASS_ESCAPE
: REGEX_CHAR_CLASS_ESCAPES
} else {
allowedEscapes = REGEX_ESCAPES
}
if (allowedEscapes.has(cNode.value)) {
return
}
if (inCharacterClass && cNode.value === CP_CARET) {
const target =
cNode.parent.type === "CharacterClassRange"
? cNode.parent
: cNode
const parent = target.parent
if (parent.type === "CharacterClass") {
if (parent.elements.indexOf(target) === 0) {
if (characterClassStack.length) {
const characterClassNode =
characterClassStack[0]
if (cNode.value === CP_CARET) {
if (
characterClassNode.start + 1 ===
cNode.start
) {
// e.g. /[\^]/
return
}
}
if (flags.unicodeSets) {
if (
REGEX_CLASS_SET_RESERVED_DOUBLE_PUNCTUATOR.has(
cNode.value,
)
) {
if (
pattern[cNode.end] === escapedChar
) {
// Escaping is valid if it is a ClassSetReservedDoublePunctuator.
return
}
const prevIndex = cNode.start - 1
if (
pattern[prevIndex] === escapedChar
) {
if (escapedChar !== "^") {
// e.g. [&\&]
// ^ // If it's the second character, it's a valid escape.
return
}
const elementStartIndex =
characterClassNode.start +
1 + // opening bracket(`[`)
(characterClassNode.negate
? 1 // `negate` caret(`^`)
: 0)
if (
elementStartIndex <= prevIndex
) {
// [^^\^], [_^\^]
// ^ ^ // If it's the second caret(`^`) character, it's a valid escape.
// But [^\^] is unnecessary escape.
return
}
}
}
}
}
if (!canUnwrapped(cNode, char)) {
return
Expand All @@ -128,7 +234,11 @@ export default createRule("no-useless-escape", {
cNode,
0,
char,
!POTENTIAL_ESCAPE_SEQUENCE.has(char),
!(
characterClassStack.length
? POTENTIAL_ESCAPE_SEQUENCE_FOR_CHAR_CLASS
: POTENTIAL_ESCAPE_SEQUENCE
).has(char),
)
}
}
Expand Down
8 changes: 8 additions & 0 deletions lib/utils/unicode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,23 @@ export const CP_FF = 12
export const CP_CR = 13
export const CP_SPACE = " ".codePointAt(0)!
export const CP_BAN = "!".codePointAt(0)!
export const CP_HASH = "#".codePointAt(0)
export const CP_DOLLAR = "$".codePointAt(0)!
export const CP_PERCENT = "%".codePointAt(0)!
export const CP_AMP = "&".codePointAt(0)!
export const CP_OPENING_PAREN = "(".codePointAt(0)!
export const CP_CLOSING_PAREN = ")".codePointAt(0)!
export const CP_STAR = "*".codePointAt(0)!
export const CP_PLUS = "+".codePointAt(0)!
export const CP_COMMA = ",".codePointAt(0)!
export const CP_MINUS = "-".codePointAt(0)!
export const CP_DOT = ".".codePointAt(0)!
export const CP_SLASH = "/".codePointAt(0)!
export const CP_COLON = ":".codePointAt(0)!
export const CP_SEMI = ";".codePointAt(0)!
export const CP_LT = "<".codePointAt(0)!
export const CP_EQ = "=".codePointAt(0)!
export const CP_GT = ">".codePointAt(0)!
export const CP_QUESTION = "?".codePointAt(0)!
export const CP_AT = "@".codePointAt(0)!
export const CP_OPENING_BRACKET = "[".codePointAt(0)!
Expand Down
Loading