Skip to content

Commit 7eea638

Browse files
Fixed regexp/negation suggested fix (#268)
1 parent 0b7747d commit 7eea638

File tree

2 files changed

+14
-21
lines changed

2 files changed

+14
-21
lines changed

lib/rules/negation.ts

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -64,28 +64,14 @@ export default createRule("negation", {
6464
function getNegationText(
6565
node: EscapeCharacterSet | UnicodePropertyCharacterSet,
6666
) {
67-
let text: string
68-
if (node.kind === "digit") {
69-
text = "d"
70-
} else if (node.kind === "space") {
71-
text = "s"
72-
} else if (node.kind === "word") {
73-
text = "w"
74-
} else if (node.kind === "property") {
75-
text = "p"
67+
// they are all of the form: /\\[dswp](?:\{[^{}]+\})?/
68+
let kind = node.raw[1]
69+
70+
if (kind.toLowerCase() === kind) {
71+
kind = kind.toUpperCase()
7672
} else {
77-
throw new Error(`unknown kind:${node.kind}`)
78-
}
79-
if (!node.negate) {
80-
text = text.toUpperCase()
81-
}
82-
if (node.kind === "property") {
83-
if (node.value != null) {
84-
text += `{${node.key}=${node.value}}`
85-
} else {
86-
text += `{${node.key}}`
87-
}
73+
kind = kind.toLowerCase()
8874
}
8975

90-
return `\\${text}`
76+
return `\\${kind}${node.raw.slice(2)}`
9177
}

tests/lib/rules/negation.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ tester.run("negation", rule as any, {
8787
"Unexpected negated character class. Use '\\p{Script=Hiragana}' instead.",
8888
],
8989
},
90+
{
91+
code: String.raw`/[^\P{Ll}]/u;`,
92+
output: String.raw`/\p{Ll}/u;`,
93+
errors: [
94+
"Unexpected negated character class. Use '\\p{Ll}' instead.",
95+
],
96+
},
9097
{
9198
code: String.raw`const s ="[^\\w]"
9299
new RegExp(s)`,

0 commit comments

Comments
 (0)