eslint-plugin-regexp
Advanced tools
Comparing version
@@ -25,2 +25,3 @@ "use strict"; | ||
"regexp/no-empty-lookarounds-assertion": "error", | ||
"regexp/no-empty-string-literal": "error", | ||
"regexp/no-escape-backspace": "error", | ||
@@ -68,2 +69,3 @@ "regexp/no-extra-lookaround-assertions": "error", | ||
"regexp/prefer-w": "error", | ||
"regexp/simplify-set-operations": "error", | ||
"regexp/sort-flags": "error", | ||
@@ -70,0 +72,0 @@ "regexp/strict": "error", |
@@ -5,2 +5,9 @@ "use strict"; | ||
const utils_1 = require("../utils"); | ||
const util_1 = require("../utils/util"); | ||
function isNegatableCharacterClassElement(node) { | ||
return (node.type === "CharacterClass" || | ||
node.type === "ExpressionCharacterClass" || | ||
(node.type === "CharacterSet" && | ||
(node.kind !== "property" || !node.strings))); | ||
} | ||
exports.default = (0, utils_1.createRule)("negation", { | ||
@@ -28,6 +35,6 @@ meta: { | ||
const element = ccNode.elements[0]; | ||
if (element.type !== "CharacterSet") { | ||
if (!isNegatableCharacterClassElement(element)) { | ||
return; | ||
} | ||
if (element.kind === "property" && element.strings) { | ||
if (element.type !== "CharacterSet" && !element.negate) { | ||
return; | ||
@@ -37,5 +44,6 @@ } | ||
!flags.unicodeSets && | ||
element.type === "CharacterSet" && | ||
element.kind === "property") { | ||
const ccSet = (0, regexp_ast_analysis_1.toUnicodeSet)(ccNode, flags); | ||
const negatedElementSet = (0, regexp_ast_analysis_1.toCharSet)(Object.assign(Object.assign({}, element), { negate: !element.negate }), flags); | ||
const negatedElementSet = (0, regexp_ast_analysis_1.toUnicodeSet)(Object.assign(Object.assign({}, element), { negate: !element.negate }), flags); | ||
if (!ccSet.equals(negatedElementSet)) { | ||
@@ -62,10 +70,19 @@ return; | ||
function getNegationText(node) { | ||
let kind = node.raw[1]; | ||
if (kind.toLowerCase() === kind) { | ||
kind = kind.toUpperCase(); | ||
if (node.type === "CharacterSet") { | ||
let kind = node.raw[1]; | ||
if (kind.toLowerCase() === kind) { | ||
kind = kind.toUpperCase(); | ||
} | ||
else { | ||
kind = kind.toLowerCase(); | ||
} | ||
return `\\${kind}${node.raw.slice(2)}`; | ||
} | ||
else { | ||
kind = kind.toLowerCase(); | ||
if (node.type === "CharacterClass") { | ||
return `[${node.elements.map((e) => e.raw).join("")}]`; | ||
} | ||
return `\\${kind}${node.raw.slice(2)}`; | ||
if (node.type === "ExpressionCharacterClass") { | ||
return `[${node.raw.slice(2, -1)}]`; | ||
} | ||
return (0, util_1.assertNever)(node); | ||
} |
@@ -38,3 +38,2 @@ "use strict"; | ||
parent.type === "CharacterClassRange" || | ||
parent.type === "ExpressionCharacterClass" || | ||
parent.type === "ClassIntersection" || | ||
@@ -41,0 +40,0 @@ parent.type === "ClassSubtraction" || |
@@ -55,3 +55,3 @@ "use strict"; | ||
function createVisitor({ node, getRegexpLocation, fixReplaceNode, }) { | ||
function verifyAlternatives(regexpNode) { | ||
function verifyAlternatives(regexpNode, suggestFixer) { | ||
if (regexpNode.alternatives.length >= 2) { | ||
@@ -72,3 +72,3 @@ for (let i = 0; i < regexpNode.alternatives.length; i++) { | ||
}); | ||
const fixed = getFixedNode(regexpNode, alt); | ||
const fixed = suggestFixer(alt); | ||
context.report({ | ||
@@ -93,5 +93,6 @@ node, | ||
return { | ||
onGroupEnter: verifyAlternatives, | ||
onCapturingGroupEnter: verifyAlternatives, | ||
onPatternEnter: verifyAlternatives, | ||
onGroupEnter: (gNode) => verifyAlternatives(gNode, (alt) => getFixedNode(gNode, alt)), | ||
onCapturingGroupEnter: (cgNode) => verifyAlternatives(cgNode, (alt) => getFixedNode(cgNode, alt)), | ||
onPatternEnter: (pNode) => verifyAlternatives(pNode, (alt) => getFixedNode(pNode, alt)), | ||
onClassStringDisjunctionEnter: (csdNode) => verifyAlternatives(csdNode, () => null), | ||
}; | ||
@@ -98,0 +99,0 @@ } |
@@ -23,2 +23,3 @@ "use strict"; | ||
const no_empty_lookarounds_assertion_1 = __importDefault(require("../rules/no-empty-lookarounds-assertion")); | ||
const no_empty_string_literal_1 = __importDefault(require("../rules/no-empty-string-literal")); | ||
const no_escape_backspace_1 = __importDefault(require("../rules/no-escape-backspace")); | ||
@@ -84,2 +85,3 @@ const no_extra_lookaround_assertions_1 = __importDefault(require("../rules/no-extra-lookaround-assertions")); | ||
const require_unicode_sets_regexp_1 = __importDefault(require("../rules/require-unicode-sets-regexp")); | ||
const simplify_set_operations_1 = __importDefault(require("../rules/simplify-set-operations")); | ||
const sort_alternatives_1 = __importDefault(require("../rules/sort-alternatives")); | ||
@@ -108,2 +110,3 @@ const sort_character_class_elements_1 = __importDefault(require("../rules/sort-character-class-elements")); | ||
no_empty_lookarounds_assertion_1.default, | ||
no_empty_string_literal_1.default, | ||
no_escape_backspace_1.default, | ||
@@ -169,2 +172,3 @@ no_extra_lookaround_assertions_1.default, | ||
require_unicode_sets_regexp_1.default, | ||
simplify_set_operations_1.default, | ||
sort_alternatives_1.default, | ||
@@ -171,0 +175,0 @@ sort_character_class_elements_1.default, |
{ | ||
"name": "eslint-plugin-regexp", | ||
"version": "2.0.0-next.10", | ||
"version": "2.0.0-next.11", | ||
"description": "ESLint plugin for finding RegExp mistakes and RegExp style guide violations.", | ||
@@ -106,3 +106,3 @@ "engines": { | ||
"@eslint-community/eslint-utils": "^4.2.0", | ||
"@eslint-community/regexpp": "^4.6.2", | ||
"@eslint-community/regexpp": "^4.9.1", | ||
"comment-parser": "^1.4.0", | ||
@@ -109,0 +109,0 @@ "grapheme-splitter": "^1.0.4", |
@@ -143,2 +143,3 @@ # Introduction | ||
| [no-dupe-characters-character-class](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-dupe-characters-character-class.html) | disallow duplicate characters in the RegExp character class | β | | π§ | | | ||
| [no-empty-string-literal](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-empty-string-literal.html) | disallow empty string literals in character classes | β | | | | | ||
| [no-extra-lookaround-assertions](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-extra-lookaround-assertions.html) | disallow unnecessary nested lookaround assertions | β | | π§ | | | ||
@@ -173,2 +174,3 @@ | [no-invisible-character](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-invisible-character.html) | disallow invisible raw character | β | | π§ | | | ||
| [require-unicode-sets-regexp](https://ota-meshi.github.io/eslint-plugin-regexp/rules/require-unicode-sets-regexp.html) | enforce the use of the `v` flag | | | π§ | | | ||
| [simplify-set-operations](https://ota-meshi.github.io/eslint-plugin-regexp/rules/simplify-set-operations.html) | require simplify set operations | β | | π§ | | | ||
| [sort-alternatives](https://ota-meshi.github.io/eslint-plugin-regexp/rules/sort-alternatives.html) | sort alternatives if order doesn't matter | | | π§ | | | ||
@@ -175,0 +177,0 @@ | [use-ignore-case](https://ota-meshi.github.io/eslint-plugin-regexp/rules/use-ignore-case.html) | use the `i` flag if it simplifies the pattern | β | | π§ | | |
738213
1.86%140
1.45%17904
1.74%244
0.83%