eslint-plugin-regexp
Advanced tools
@@ -6,2 +6,3 @@ "use strict"; | ||
const eslint_utils_1 = require("eslint-utils"); | ||
const ast_utils_1 = require("../utils/ast-utils"); | ||
exports.default = utils_1.createRule("prefer-regexp-exec", { | ||
@@ -23,12 +24,5 @@ meta: { | ||
CallExpression(node) { | ||
if (node.arguments.length !== 1) { | ||
if (!ast_utils_1.isKnownMethodCall(node, { match: 1 })) { | ||
return; | ||
} | ||
if (node.callee.type !== "MemberExpression" || | ||
node.callee.computed || | ||
node.callee.property.type !== "Identifier" || | ||
node.callee.property.name !== "match" || | ||
node.callee.object.type === "Super") { | ||
return; | ||
} | ||
const arg = node.arguments[0]; | ||
@@ -35,0 +29,0 @@ const evaluated = eslint_utils_1.getStaticValue(arg, context.getScope()); |
@@ -6,2 +6,3 @@ "use strict"; | ||
const type_tracker_1 = require("../utils/type-tracker"); | ||
const ast_utils_1 = require("../utils/ast-utils"); | ||
exports.default = utils_1.createRule("prefer-regexp-test", { | ||
@@ -25,15 +26,5 @@ meta: { | ||
CallExpression(node) { | ||
if (node.arguments.length !== 1) { | ||
if (!ast_utils_1.isKnownMethodCall(node, { match: 1, exec: 1 })) { | ||
return; | ||
} | ||
if (node.callee.type !== "MemberExpression" || | ||
node.callee.computed || | ||
node.callee.property.type !== "Identifier" || | ||
node.callee.object.type === "Super") { | ||
return; | ||
} | ||
if (node.callee.property.name !== "match" && | ||
node.callee.property.name !== "exec") { | ||
return; | ||
} | ||
if (!isUseBoolean(node)) { | ||
@@ -40,0 +31,0 @@ return; |
@@ -17,2 +17,3 @@ "use strict"; | ||
const string_literal_parser_1 = require("./string-literal-parser"); | ||
const ast_utils_1 = require("./ast-utils"); | ||
__exportStar(require("./unicode"), exports); | ||
@@ -97,3 +98,3 @@ const regexpRules = new WeakMap(); | ||
if (rule.createLiteralVisitor) { | ||
yield rule.createLiteralVisitor(node, node.regex.pattern, node.regex.flags); | ||
yield rule.createLiteralVisitor(node, node.regex.pattern, node.regex.flags, node); | ||
} | ||
@@ -127,7 +128,7 @@ } | ||
} | ||
for (const { patternNode, pattern, flags } of regexpDataList) { | ||
for (const { newOrCall, patternNode, pattern, flags, } of regexpDataList) { | ||
if (typeof pattern === "string") { | ||
let verifyPatternNode = patternNode; | ||
if (patternNode.type === "Identifier") { | ||
const variable = eslint_utils_1.findVariable(context.getScope(), patternNode); | ||
const variable = ast_utils_1.findVariable(context, patternNode); | ||
if (variable && variable.defs.length === 1) { | ||
@@ -164,3 +165,3 @@ const def = variable.defs[0]; | ||
if (rule.createSourceVisitor) { | ||
yield rule.createSourceVisitor(verifyPatternNode, pattern, flags || ""); | ||
yield rule.createSourceVisitor(verifyPatternNode, pattern, flags || "", newOrCall); | ||
} | ||
@@ -167,0 +168,0 @@ } |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isEqualNodes = void 0; | ||
exports.extractCaptures = exports.getRegExpNodeFromExpression = exports.isEqualNodes = void 0; | ||
const regexpp_1 = require("regexpp"); | ||
const eslint_utils_1 = require("eslint-utils"); | ||
const parser = new regexpp_1.RegExpParser(); | ||
const EQUALS_CHECKER = { | ||
@@ -126,1 +129,39 @@ Alternative(a, b, shortCircuit) { | ||
} | ||
function getRegExpNodeFromExpression(node, context) { | ||
if (node.type === "Literal") { | ||
if ("regex" in node && node.regex) { | ||
try { | ||
return parser.parsePattern(node.regex.pattern, 0, node.regex.pattern.length, node.regex.flags.includes("u")); | ||
} | ||
catch (_a) { | ||
return null; | ||
} | ||
} | ||
return null; | ||
} | ||
const evaluated = eslint_utils_1.getStaticValue(node, context.getScope()); | ||
if (!evaluated || !(evaluated.value instanceof RegExp)) { | ||
return null; | ||
} | ||
try { | ||
return regexpp_1.parseRegExpLiteral(evaluated.value); | ||
} | ||
catch (_b) { | ||
return null; | ||
} | ||
} | ||
exports.getRegExpNodeFromExpression = getRegExpNodeFromExpression; | ||
function extractCaptures(patternNode) { | ||
let count = 0; | ||
const names = new Set(); | ||
regexpp_1.visitRegExpAST(patternNode, { | ||
onCapturingGroupEnter(cgNode) { | ||
count++; | ||
if (cgNode.name != null) { | ||
names.add(cgNode.name); | ||
} | ||
}, | ||
}); | ||
return { count, names }; | ||
} | ||
exports.extractCaptures = extractCaptures; |
@@ -17,5 +17,8 @@ "use strict"; | ||
const no_invisible_character_1 = __importDefault(require("../rules/no-invisible-character")); | ||
const no_legacy_features_1 = __importDefault(require("../rules/no-legacy-features")); | ||
const no_octal_1 = __importDefault(require("../rules/no-octal")); | ||
const no_unused_capturing_group_1 = __importDefault(require("../rules/no-unused-capturing-group")); | ||
const no_useless_backreference_1 = __importDefault(require("../rules/no-useless-backreference")); | ||
const no_useless_character_class_1 = __importDefault(require("../rules/no-useless-character-class")); | ||
const no_useless_dollar_replacements_1 = __importDefault(require("../rules/no-useless-dollar-replacements")); | ||
const no_useless_escape_1 = __importDefault(require("../rules/no-useless-escape")); | ||
@@ -30,2 +33,3 @@ const no_useless_exactly_quantifier_1 = __importDefault(require("../rules/no-useless-exactly-quantifier")); | ||
const prefer_d_1 = __importDefault(require("../rules/prefer-d")); | ||
const prefer_escape_replacement_dollar_char_1 = __importDefault(require("../rules/prefer-escape-replacement-dollar-char")); | ||
const prefer_plus_quantifier_1 = __importDefault(require("../rules/prefer-plus-quantifier")); | ||
@@ -52,5 +56,8 @@ const prefer_quantifier_1 = __importDefault(require("../rules/prefer-quantifier")); | ||
no_invisible_character_1.default, | ||
no_legacy_features_1.default, | ||
no_octal_1.default, | ||
no_unused_capturing_group_1.default, | ||
no_useless_backreference_1.default, | ||
no_useless_character_class_1.default, | ||
no_useless_dollar_replacements_1.default, | ||
no_useless_escape_1.default, | ||
@@ -65,2 +72,3 @@ no_useless_exactly_quantifier_1.default, | ||
prefer_d_1.default, | ||
prefer_escape_replacement_dollar_char_1.default, | ||
prefer_plus_quantifier_1.default, | ||
@@ -67,0 +75,0 @@ prefer_quantifier_1.default, |
@@ -24,8 +24,9 @@ "use strict"; | ||
const eslintUtils = __importStar(require("eslint-utils")); | ||
const astUtils = __importStar(require("../ast-utils")); | ||
function findVariable(context, node) { | ||
return eslintUtils.findVariable(getScope(context, node), node); | ||
return astUtils.findVariable(context, node); | ||
} | ||
exports.findVariable = findVariable; | ||
function getPropertyName(context, node) { | ||
return eslintUtils.getPropertyName(node, getScope(context, node)); | ||
return eslintUtils.getPropertyName(node, astUtils.getScope(context, node)); | ||
} | ||
@@ -37,16 +38,2 @@ exports.getPropertyName = getPropertyName; | ||
exports.isParenthesized = isParenthesized; | ||
function getScope(context, currentNode) { | ||
const scopeManager = context.getSourceCode().scopeManager; | ||
let node = currentNode; | ||
for (; node; node = node.parent || null) { | ||
const scope = scopeManager.acquire(node, false); | ||
if (scope) { | ||
if (scope.type === "function-expression-name") { | ||
return scope.childScopes[0]; | ||
} | ||
return scope; | ||
} | ||
} | ||
return scopeManager.scopes[0]; | ||
} | ||
function getParent(node) { | ||
@@ -53,0 +40,0 @@ if (!node) { |
{ | ||
"name": "eslint-plugin-regexp", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"description": "ESLint plugin for finding RegExp mistakes and RegExp style guide violations.", | ||
@@ -49,3 +49,3 @@ "main": "dist/index.js", | ||
"devDependencies": { | ||
"@ota-meshi/eslint-plugin": "^0.3.0", | ||
"@ota-meshi/eslint-plugin": "^0.4.0", | ||
"@types/eslint": "^7.2.0", | ||
@@ -68,5 +68,5 @@ "@types/eslint-scope": "^3.7.0", | ||
"eslint-plugin-prettier": "^3.3.1", | ||
"eslint-plugin-regexp": "^0.4.0", | ||
"eslint-plugin-regexp": "^0.5.0", | ||
"eslint-plugin-vue": "^7.5.0", | ||
"eslint-plugin-yml": "^0.8.0", | ||
"eslint-plugin-yml": "^0.9.0", | ||
"eslint4b": "^7.3.1", | ||
@@ -73,0 +73,0 @@ "mocha": "^8.0.0", |
@@ -15,6 +15,10 @@ # Introduction | ||
## Features | ||
## :name_badge: Features | ||
This ESLint plugin provides linting rules relate to better ways to help you avoid problems when using RegExp. | ||
- Find the wrong usage of regular expressions, and their hints. | ||
- Enforces a consistent style of regular expressions. | ||
- Find hints for writing optimized regular expressions. | ||
You can check on the [Online DEMO](https://ota-meshi.github.io/eslint-plugin-regexp/playground/). | ||
@@ -24,7 +28,7 @@ | ||
## Documentation | ||
## :book: Documentation | ||
See [documents](https://ota-meshi.github.io/eslint-plugin-regexp/). | ||
## Installation | ||
## :cd: Installation | ||
@@ -42,3 +46,3 @@ ```bash | ||
## Usage | ||
## :book: Usage | ||
@@ -65,3 +69,3 @@ <!--USAGE_SECTION_START--> | ||
## Configs | ||
### Configuration | ||
@@ -75,3 +79,3 @@ This plugin provides one config: | ||
## Rules | ||
## :white_check_mark: Rules | ||
@@ -97,5 +101,8 @@ <!--RULES_SECTION_START--> | ||
| [regexp/no-invisible-character](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-invisible-character.html) | disallow invisible raw character | :star::wrench: | | ||
| [regexp/no-legacy-features](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-legacy-features.html) | disallow legacy RegExp features | | | ||
| [regexp/no-octal](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-octal.html) | disallow octal escape sequence | :star: | | ||
| [regexp/no-unused-capturing-group](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-unused-capturing-group.html) | disallow unused capturing group | | | ||
| [regexp/no-useless-backreference](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-useless-backreference.html) | disallow useless backreferences in regular expressions | | | ||
| [regexp/no-useless-character-class](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-useless-character-class.html) | disallow character class with one character | :wrench: | | ||
| [regexp/no-useless-dollar-replacements](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-useless-dollar-replacements.html) | disallow useless `$` replacements in replacement string | | | ||
| [regexp/no-useless-escape](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-useless-escape.html) | disallow unnecessary escape characters in RegExp | | | ||
@@ -110,2 +117,3 @@ | [regexp/no-useless-exactly-quantifier](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-useless-exactly-quantifier.html) | disallow unnecessary exactly quantifier | :star: | | ||
| [regexp/prefer-d](https://ota-meshi.github.io/eslint-plugin-regexp/rules/prefer-d.html) | enforce using `\d` | :star::wrench: | | ||
| [regexp/prefer-escape-replacement-dollar-char](https://ota-meshi.github.io/eslint-plugin-regexp/rules/prefer-escape-replacement-dollar-char.html) | enforces escape of replacement `$` character (`$$`). | | | ||
| [regexp/prefer-plus-quantifier](https://ota-meshi.github.io/eslint-plugin-regexp/rules/prefer-plus-quantifier.html) | enforce using `+` quantifier | :star::wrench: | | ||
@@ -127,4 +135,8 @@ | [regexp/prefer-quantifier](https://ota-meshi.github.io/eslint-plugin-regexp/rules/prefer-quantifier.html) | enforce using quantifier | :wrench: | | ||
## Contributing | ||
<!-- ## :traffic_light: Semantic Versioning Policy | ||
**eslint-plugin-jsonc** follows [Semantic Versioning](http://semver.org/) and [ESLint's Semantic Versioning Policy](https://github.com/eslint/eslint#semantic-versioning-policy). --> | ||
## :beers: Contributing | ||
Welcome contributing! | ||
@@ -141,4 +153,4 @@ | ||
## License | ||
## :lock: License | ||
See the [LICENSE](LICENSE) file for license rights and limitations (MIT). |
285980
15.54%71
9.23%7015
15.25%148
8.82%