bahlint 28.58.6934-dev-001
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +19 -0
- package/README.md +354 -0
- package/bin/bahlint.js +267 -0
- package/bin/eslint.js +194 -0
- package/conf/ecma-version.js +16 -0
- package/conf/globals.js +169 -0
- package/conf/replacements.json +26 -0
- package/conf/rule-type-list.json +91 -0
- package/lib/api.js +39 -0
- package/lib/cli-engine/formatters/formatters-meta.json +18 -0
- package/lib/cli-engine/formatters/html.js +359 -0
- package/lib/cli-engine/formatters/json-with-metadata.js +16 -0
- package/lib/cli-engine/formatters/json.js +13 -0
- package/lib/cli-engine/formatters/stylish.js +153 -0
- package/lib/cli-engine/hash.js +35 -0
- package/lib/cli-engine/lint-result-cache.js +220 -0
- package/lib/cli.js +521 -0
- package/lib/config/config-loader.js +668 -0
- package/lib/config/config.js +674 -0
- package/lib/config/default-config.js +78 -0
- package/lib/config/flat-config-array.js +217 -0
- package/lib/config/flat-config-schema.js +598 -0
- package/lib/config-api.js +12 -0
- package/lib/eslint/eslint-helpers.js +1462 -0
- package/lib/eslint/eslint.js +1364 -0
- package/lib/eslint/index.js +7 -0
- package/lib/eslint/worker.js +173 -0
- package/lib/languages/js/index.js +336 -0
- package/lib/languages/js/source-code/index.js +7 -0
- package/lib/languages/js/source-code/source-code.js +1178 -0
- package/lib/languages/js/source-code/token-store/backward-token-comment-cursor.js +61 -0
- package/lib/languages/js/source-code/token-store/backward-token-cursor.js +57 -0
- package/lib/languages/js/source-code/token-store/cursor.js +76 -0
- package/lib/languages/js/source-code/token-store/cursors.js +120 -0
- package/lib/languages/js/source-code/token-store/decorative-cursor.js +38 -0
- package/lib/languages/js/source-code/token-store/filter-cursor.js +42 -0
- package/lib/languages/js/source-code/token-store/forward-token-comment-cursor.js +65 -0
- package/lib/languages/js/source-code/token-store/forward-token-cursor.js +62 -0
- package/lib/languages/js/source-code/token-store/index.js +695 -0
- package/lib/languages/js/source-code/token-store/limit-cursor.js +39 -0
- package/lib/languages/js/source-code/token-store/padded-token-cursor.js +45 -0
- package/lib/languages/js/source-code/token-store/skip-cursor.js +41 -0
- package/lib/languages/js/source-code/token-store/utils.js +131 -0
- package/lib/languages/js/validate-language-options.js +196 -0
- package/lib/linter/apply-disable-directives.js +583 -0
- package/lib/linter/code-path-analysis/code-path-analyzer.js +828 -0
- package/lib/linter/code-path-analysis/code-path-segment.js +262 -0
- package/lib/linter/code-path-analysis/code-path-state.js +2370 -0
- package/lib/linter/code-path-analysis/code-path.js +332 -0
- package/lib/linter/code-path-analysis/debug-helpers.js +223 -0
- package/lib/linter/code-path-analysis/fork-context.js +374 -0
- package/lib/linter/code-path-analysis/id-generator.js +44 -0
- package/lib/linter/esquery.js +332 -0
- package/lib/linter/file-context.js +88 -0
- package/lib/linter/file-report.js +604 -0
- package/lib/linter/index.js +11 -0
- package/lib/linter/interpolate.js +50 -0
- package/lib/linter/linter.js +1614 -0
- package/lib/linter/rule-fixer.js +199 -0
- package/lib/linter/source-code-fixer.js +154 -0
- package/lib/linter/source-code-traverser.js +333 -0
- package/lib/linter/source-code-visitor.js +81 -0
- package/lib/linter/timing.js +209 -0
- package/lib/linter/vfile.js +115 -0
- package/lib/options.js +416 -0
- package/lib/rule-tester/index.js +7 -0
- package/lib/rule-tester/rule-tester.js +1989 -0
- package/lib/rules/accessor-pairs.js +420 -0
- package/lib/rules/array-bracket-newline.js +291 -0
- package/lib/rules/array-bracket-spacing.js +301 -0
- package/lib/rules/array-callback-return.js +493 -0
- package/lib/rules/array-element-newline.js +374 -0
- package/lib/rules/arrow-body-style.js +418 -0
- package/lib/rules/arrow-parens.js +237 -0
- package/lib/rules/arrow-spacing.js +188 -0
- package/lib/rules/block-scoped-var.js +137 -0
- package/lib/rules/block-spacing.js +202 -0
- package/lib/rules/brace-style.js +278 -0
- package/lib/rules/callback-return.js +216 -0
- package/lib/rules/camelcase.js +422 -0
- package/lib/rules/capitalized-comments.js +325 -0
- package/lib/rules/class-methods-use-this.js +250 -0
- package/lib/rules/comma-dangle.js +424 -0
- package/lib/rules/comma-spacing.js +205 -0
- package/lib/rules/comma-style.js +391 -0
- package/lib/rules/complexity.js +201 -0
- package/lib/rules/computed-property-spacing.js +251 -0
- package/lib/rules/consistent-return.js +221 -0
- package/lib/rules/consistent-this.js +179 -0
- package/lib/rules/constructor-super.js +453 -0
- package/lib/rules/curly.js +425 -0
- package/lib/rules/default-case-last.js +51 -0
- package/lib/rules/default-case.js +103 -0
- package/lib/rules/default-param-last.js +78 -0
- package/lib/rules/dot-location.js +138 -0
- package/lib/rules/dot-notation.js +216 -0
- package/lib/rules/eol-last.js +135 -0
- package/lib/rules/eqeqeq.js +210 -0
- package/lib/rules/for-direction.js +168 -0
- package/lib/rules/func-call-spacing.js +281 -0
- package/lib/rules/func-name-matching.js +338 -0
- package/lib/rules/func-names.js +194 -0
- package/lib/rules/func-style.js +221 -0
- package/lib/rules/function-call-argument-newline.js +166 -0
- package/lib/rules/function-paren-newline.js +368 -0
- package/lib/rules/generator-star-spacing.js +246 -0
- package/lib/rules/getter-return.js +242 -0
- package/lib/rules/global-require.js +117 -0
- package/lib/rules/grouped-accessor-pairs.js +268 -0
- package/lib/rules/guard-for-in.js +85 -0
- package/lib/rules/handle-callback-err.js +122 -0
- package/lib/rules/id-blacklist.js +241 -0
- package/lib/rules/id-denylist.js +223 -0
- package/lib/rules/id-length.js +217 -0
- package/lib/rules/id-match.js +363 -0
- package/lib/rules/implicit-arrow-linebreak.js +125 -0
- package/lib/rules/indent-legacy.js +1369 -0
- package/lib/rules/indent.js +2334 -0
- package/lib/rules/index.js +332 -0
- package/lib/rules/init-declarations.js +172 -0
- package/lib/rules/jsx-quotes.js +128 -0
- package/lib/rules/key-spacing.js +822 -0
- package/lib/rules/keyword-spacing.js +701 -0
- package/lib/rules/line-comment-position.js +157 -0
- package/lib/rules/linebreak-style.js +135 -0
- package/lib/rules/lines-around-comment.js +581 -0
- package/lib/rules/lines-around-directive.js +249 -0
- package/lib/rules/lines-between-class-members.js +358 -0
- package/lib/rules/logical-assignment-operators.js +688 -0
- package/lib/rules/max-classes-per-file.js +90 -0
- package/lib/rules/max-depth.js +159 -0
- package/lib/rules/max-len.js +497 -0
- package/lib/rules/max-lines-per-function.js +238 -0
- package/lib/rules/max-lines.js +189 -0
- package/lib/rules/max-nested-callbacks.js +115 -0
- package/lib/rules/max-params.js +148 -0
- package/lib/rules/max-statements-per-line.js +224 -0
- package/lib/rules/max-statements.js +188 -0
- package/lib/rules/multiline-comment-style.js +652 -0
- package/lib/rules/multiline-ternary.js +257 -0
- package/lib/rules/new-cap.js +277 -0
- package/lib/rules/new-parens.js +120 -0
- package/lib/rules/newline-after-var.js +307 -0
- package/lib/rules/newline-before-return.js +242 -0
- package/lib/rules/newline-per-chained-call.js +159 -0
- package/lib/rules/no-alert.js +149 -0
- package/lib/rules/no-array-constructor.js +195 -0
- package/lib/rules/no-async-promise-executor.js +45 -0
- package/lib/rules/no-await-in-loop.js +115 -0
- package/lib/rules/no-bitwise.js +145 -0
- package/lib/rules/no-buffer-constructor.js +74 -0
- package/lib/rules/no-caller.js +52 -0
- package/lib/rules/no-case-declarations.js +80 -0
- package/lib/rules/no-catch-shadow.js +96 -0
- package/lib/rules/no-class-assign.js +66 -0
- package/lib/rules/no-compare-neg-zero.js +74 -0
- package/lib/rules/no-cond-assign.js +175 -0
- package/lib/rules/no-confusing-arrow.js +127 -0
- package/lib/rules/no-console.js +221 -0
- package/lib/rules/no-const-assign.js +73 -0
- package/lib/rules/no-constant-binary-expression.js +603 -0
- package/lib/rules/no-constant-condition.js +177 -0
- package/lib/rules/no-constructor-return.js +62 -0
- package/lib/rules/no-continue.js +38 -0
- package/lib/rules/no-control-regex.js +142 -0
- package/lib/rules/no-debugger.js +41 -0
- package/lib/rules/no-delete-var.js +42 -0
- package/lib/rules/no-div-regex.js +60 -0
- package/lib/rules/no-dupe-args.js +92 -0
- package/lib/rules/no-dupe-class-members.js +117 -0
- package/lib/rules/no-dupe-else-if.js +145 -0
- package/lib/rules/no-dupe-keys.js +165 -0
- package/lib/rules/no-duplicate-case.js +78 -0
- package/lib/rules/no-duplicate-imports.js +368 -0
- package/lib/rules/no-else-return.js +450 -0
- package/lib/rules/no-empty-character-class.js +83 -0
- package/lib/rules/no-empty-function.js +236 -0
- package/lib/rules/no-empty-pattern.js +85 -0
- package/lib/rules/no-empty-static-block.js +73 -0
- package/lib/rules/no-empty.js +153 -0
- package/lib/rules/no-eq-null.js +51 -0
- package/lib/rules/no-eval.js +295 -0
- package/lib/rules/no-ex-assign.js +57 -0
- package/lib/rules/no-extend-native.js +180 -0
- package/lib/rules/no-extra-bind.js +224 -0
- package/lib/rules/no-extra-boolean-cast.js +420 -0
- package/lib/rules/no-extra-label.js +169 -0
- package/lib/rules/no-extra-parens.js +1669 -0
- package/lib/rules/no-extra-semi.js +167 -0
- package/lib/rules/no-fallthrough.js +260 -0
- package/lib/rules/no-floating-decimal.js +99 -0
- package/lib/rules/no-func-assign.js +77 -0
- package/lib/rules/no-global-assign.js +101 -0
- package/lib/rules/no-implicit-coercion.js +468 -0
- package/lib/rules/no-implicit-globals.js +187 -0
- package/lib/rules/no-implied-eval.js +170 -0
- package/lib/rules/no-import-assign.js +227 -0
- package/lib/rules/no-inline-comments.js +115 -0
- package/lib/rules/no-inner-declarations.js +147 -0
- package/lib/rules/no-invalid-regexp.js +244 -0
- package/lib/rules/no-invalid-this.js +178 -0
- package/lib/rules/no-irregular-whitespace.js +292 -0
- package/lib/rules/no-iterator.js +48 -0
- package/lib/rules/no-label-var.js +78 -0
- package/lib/rules/no-labels.js +156 -0
- package/lib/rules/no-lone-blocks.js +140 -0
- package/lib/rules/no-lonely-if.js +126 -0
- package/lib/rules/no-loop-func.js +267 -0
- package/lib/rules/no-loss-of-precision.js +249 -0
- package/lib/rules/no-magic-numbers.js +365 -0
- package/lib/rules/no-misleading-character-class.js +595 -0
- package/lib/rules/no-mixed-operators.js +253 -0
- package/lib/rules/no-mixed-requires.js +267 -0
- package/lib/rules/no-mixed-spaces-and-tabs.js +148 -0
- package/lib/rules/no-multi-assign.js +66 -0
- package/lib/rules/no-multi-spaces.js +179 -0
- package/lib/rules/no-multi-str.js +67 -0
- package/lib/rules/no-multiple-empty-lines.js +210 -0
- package/lib/rules/no-native-reassign.js +114 -0
- package/lib/rules/no-negated-condition.js +100 -0
- package/lib/rules/no-negated-in-lhs.js +59 -0
- package/lib/rules/no-nested-ternary.js +46 -0
- package/lib/rules/no-new-func.js +96 -0
- package/lib/rules/no-new-native-nonconstructor.js +70 -0
- package/lib/rules/no-new-object.js +76 -0
- package/lib/rules/no-new-require.js +67 -0
- package/lib/rules/no-new-symbol.js +74 -0
- package/lib/rules/no-new-wrappers.js +62 -0
- package/lib/rules/no-new.js +42 -0
- package/lib/rules/no-nonoctal-decimal-escape.js +176 -0
- package/lib/rules/no-obj-calls.js +99 -0
- package/lib/rules/no-object-constructor.js +124 -0
- package/lib/rules/no-octal-escape.js +53 -0
- package/lib/rules/no-octal.js +42 -0
- package/lib/rules/no-param-reassign.js +248 -0
- package/lib/rules/no-path-concat.js +79 -0
- package/lib/rules/no-plusplus.js +102 -0
- package/lib/rules/no-process-env.js +68 -0
- package/lib/rules/no-process-exit.js +67 -0
- package/lib/rules/no-promise-executor-return.js +264 -0
- package/lib/rules/no-proto.js +45 -0
- package/lib/rules/no-prototype-builtins.js +181 -0
- package/lib/rules/no-redeclare.js +173 -0
- package/lib/rules/no-regex-spaces.js +219 -0
- package/lib/rules/no-restricted-exports.js +227 -0
- package/lib/rules/no-restricted-globals.js +266 -0
- package/lib/rules/no-restricted-imports.js +892 -0
- package/lib/rules/no-restricted-modules.js +249 -0
- package/lib/rules/no-restricted-properties.js +233 -0
- package/lib/rules/no-restricted-syntax.js +74 -0
- package/lib/rules/no-return-assign.js +87 -0
- package/lib/rules/no-return-await.js +162 -0
- package/lib/rules/no-script-url.js +68 -0
- package/lib/rules/no-self-assign.js +186 -0
- package/lib/rules/no-self-compare.js +77 -0
- package/lib/rules/no-sequences.js +158 -0
- package/lib/rules/no-setter-return.js +224 -0
- package/lib/rules/no-shadow-restricted-names.js +113 -0
- package/lib/rules/no-shadow.js +624 -0
- package/lib/rules/no-spaced-func.js +105 -0
- package/lib/rules/no-sparse-arrays.js +68 -0
- package/lib/rules/no-sync.js +81 -0
- package/lib/rules/no-tabs.js +110 -0
- package/lib/rules/no-template-curly-in-string.js +45 -0
- package/lib/rules/no-ternary.js +38 -0
- package/lib/rules/no-this-before-super.js +365 -0
- package/lib/rules/no-throw-literal.js +46 -0
- package/lib/rules/no-trailing-spaces.js +227 -0
- package/lib/rules/no-unassigned-vars.js +80 -0
- package/lib/rules/no-undef-init.js +101 -0
- package/lib/rules/no-undef.js +84 -0
- package/lib/rules/no-undefined.js +85 -0
- package/lib/rules/no-underscore-dangle.js +383 -0
- package/lib/rules/no-unexpected-multiline.js +130 -0
- package/lib/rules/no-unmodified-loop-condition.js +360 -0
- package/lib/rules/no-unneeded-ternary.js +232 -0
- package/lib/rules/no-unreachable-loop.js +190 -0
- package/lib/rules/no-unreachable.js +300 -0
- package/lib/rules/no-unsafe-finally.js +119 -0
- package/lib/rules/no-unsafe-negation.js +152 -0
- package/lib/rules/no-unsafe-optional-chaining.js +221 -0
- package/lib/rules/no-unused-expressions.js +227 -0
- package/lib/rules/no-unused-labels.js +158 -0
- package/lib/rules/no-unused-private-class-members.js +219 -0
- package/lib/rules/no-unused-vars.js +1739 -0
- package/lib/rules/no-use-before-define.js +446 -0
- package/lib/rules/no-useless-assignment.js +657 -0
- package/lib/rules/no-useless-backreference.js +263 -0
- package/lib/rules/no-useless-call.js +95 -0
- package/lib/rules/no-useless-catch.js +57 -0
- package/lib/rules/no-useless-computed-key.js +204 -0
- package/lib/rules/no-useless-concat.js +121 -0
- package/lib/rules/no-useless-constructor.js +262 -0
- package/lib/rules/no-useless-escape.js +406 -0
- package/lib/rules/no-useless-rename.js +202 -0
- package/lib/rules/no-useless-return.js +401 -0
- package/lib/rules/no-var.js +367 -0
- package/lib/rules/no-void.js +69 -0
- package/lib/rules/no-warning-comments.js +209 -0
- package/lib/rules/no-whitespace-before-property.js +150 -0
- package/lib/rules/no-with.js +37 -0
- package/lib/rules/nonblock-statement-body-position.js +164 -0
- package/lib/rules/object-curly-newline.js +383 -0
- package/lib/rules/object-curly-spacing.js +369 -0
- package/lib/rules/object-property-newline.js +151 -0
- package/lib/rules/object-shorthand.js +652 -0
- package/lib/rules/one-var-declaration-per-line.js +117 -0
- package/lib/rules/one-var.js +717 -0
- package/lib/rules/operator-assignment.js +270 -0
- package/lib/rules/operator-linebreak.js +315 -0
- package/lib/rules/padded-blocks.js +366 -0
- package/lib/rules/padding-line-between-statements.js +612 -0
- package/lib/rules/prefer-arrow-callback.js +437 -0
- package/lib/rules/prefer-const.js +546 -0
- package/lib/rules/prefer-destructuring.js +332 -0
- package/lib/rules/prefer-exponentiation-operator.js +235 -0
- package/lib/rules/prefer-named-capture-group.js +197 -0
- package/lib/rules/prefer-numeric-literals.js +157 -0
- package/lib/rules/prefer-object-has-own.js +148 -0
- package/lib/rules/prefer-object-spread.js +319 -0
- package/lib/rules/prefer-promise-reject-errors.js +154 -0
- package/lib/rules/prefer-reflect.js +150 -0
- package/lib/rules/prefer-regex-literals.js +605 -0
- package/lib/rules/prefer-rest-params.js +117 -0
- package/lib/rules/prefer-spread.js +91 -0
- package/lib/rules/prefer-template.js +347 -0
- package/lib/rules/preserve-caught-error.js +535 -0
- package/lib/rules/quote-props.js +394 -0
- package/lib/rules/quotes.js +416 -0
- package/lib/rules/radix.js +193 -0
- package/lib/rules/require-atomic-updates.js +365 -0
- package/lib/rules/require-await.js +184 -0
- package/lib/rules/require-unicode-regexp.js +317 -0
- package/lib/rules/require-yield.js +86 -0
- package/lib/rules/rest-spread-spacing.js +150 -0
- package/lib/rules/semi-spacing.js +297 -0
- package/lib/rules/semi-style.js +218 -0
- package/lib/rules/semi.js +476 -0
- package/lib/rules/sort-imports.js +319 -0
- package/lib/rules/sort-keys.js +268 -0
- package/lib/rules/sort-vars.js +140 -0
- package/lib/rules/space-before-blocks.js +232 -0
- package/lib/rules/space-before-function-paren.js +202 -0
- package/lib/rules/space-in-parens.js +374 -0
- package/lib/rules/space-infix-ops.js +249 -0
- package/lib/rules/space-unary-ops.js +400 -0
- package/lib/rules/spaced-comment.js +447 -0
- package/lib/rules/strict.js +314 -0
- package/lib/rules/switch-colon-spacing.js +158 -0
- package/lib/rules/symbol-description.js +70 -0
- package/lib/rules/template-curly-spacing.js +168 -0
- package/lib/rules/template-tag-spacing.js +121 -0
- package/lib/rules/unicode-bom.js +73 -0
- package/lib/rules/use-isnan.js +268 -0
- package/lib/rules/utils/ast-utils.js +2828 -0
- package/lib/rules/utils/char-source.js +247 -0
- package/lib/rules/utils/fix-tracker.js +125 -0
- package/lib/rules/utils/keywords.js +67 -0
- package/lib/rules/utils/lazy-loading-rule-map.js +118 -0
- package/lib/rules/utils/regular-expressions.js +58 -0
- package/lib/rules/utils/unicode/index.js +16 -0
- package/lib/rules/utils/unicode/is-combining-character.js +13 -0
- package/lib/rules/utils/unicode/is-emoji-modifier.js +13 -0
- package/lib/rules/utils/unicode/is-regional-indicator-symbol.js +13 -0
- package/lib/rules/utils/unicode/is-surrogate-pair.js +14 -0
- package/lib/rules/valid-typeof.js +171 -0
- package/lib/rules/vars-on-top.js +165 -0
- package/lib/rules/wrap-iife.js +238 -0
- package/lib/rules/wrap-regex.js +91 -0
- package/lib/rules/yield-star-spacing.js +158 -0
- package/lib/rules/yoda.js +362 -0
- package/lib/services/parser-service.js +64 -0
- package/lib/services/processor-service.js +100 -0
- package/lib/services/suppressions-service.js +302 -0
- package/lib/services/warning-service.js +87 -0
- package/lib/shared/ajv.js +34 -0
- package/lib/shared/assert.js +21 -0
- package/lib/shared/ast-utils.js +30 -0
- package/lib/shared/deep-merge-arrays.js +62 -0
- package/lib/shared/directives.js +16 -0
- package/lib/shared/flags.js +89 -0
- package/lib/shared/logging.js +38 -0
- package/lib/shared/naming.js +109 -0
- package/lib/shared/option-utils.js +63 -0
- package/lib/shared/relative-module-resolver.js +28 -0
- package/lib/shared/runtime-info.js +177 -0
- package/lib/shared/serialization.js +78 -0
- package/lib/shared/severity.js +49 -0
- package/lib/shared/stats.js +30 -0
- package/lib/shared/string-utils.js +58 -0
- package/lib/shared/text-table.js +68 -0
- package/lib/shared/translate-cli-options.js +223 -0
- package/lib/shared/traverser.js +202 -0
- package/lib/types/config-api.d.ts +12 -0
- package/lib/types/index.d.ts +1482 -0
- package/lib/types/rules.d.ts +5603 -0
- package/lib/types/universal.d.ts +6 -0
- package/lib/types/use-at-your-own-risk.d.ts +34 -0
- package/lib/universal.js +10 -0
- package/lib/unsupported-api.js +26 -0
- package/messages/all-files-ignored.js +16 -0
- package/messages/all-matched-files-ignored.js +21 -0
- package/messages/config-file-missing.js +16 -0
- package/messages/config-plugin-missing.js +14 -0
- package/messages/config-serialize-function.js +30 -0
- package/messages/eslintrc-incompat.js +117 -0
- package/messages/eslintrc-plugins.js +27 -0
- package/messages/extend-config-missing.js +13 -0
- package/messages/failed-to-read-json.js +11 -0
- package/messages/file-not-found.js +10 -0
- package/messages/invalid-rule-options.js +17 -0
- package/messages/invalid-rule-severity.js +13 -0
- package/messages/no-config-found.js +15 -0
- package/messages/plugin-conflict.js +22 -0
- package/messages/plugin-invalid.js +16 -0
- package/messages/plugin-missing.js +19 -0
- package/messages/print-config-with-directory-path.js +8 -0
- package/messages/shared.js +23 -0
- package/messages/whitespace-found.js +11 -0
- package/package.json +220 -0
|
@@ -0,0 +1,605 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Rule to disallow use of the `RegExp` constructor in favor of regular expression literals
|
|
3
|
+
* @author Milos Djermanovic
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
"use strict";
|
|
7
|
+
|
|
8
|
+
//------------------------------------------------------------------------------
|
|
9
|
+
// Requirements
|
|
10
|
+
//------------------------------------------------------------------------------
|
|
11
|
+
|
|
12
|
+
const astUtils = require("./utils/ast-utils");
|
|
13
|
+
const {
|
|
14
|
+
CALL,
|
|
15
|
+
CONSTRUCT,
|
|
16
|
+
ReferenceTracker,
|
|
17
|
+
} = require("@eslint-community/eslint-utils");
|
|
18
|
+
const {
|
|
19
|
+
RegExpValidator,
|
|
20
|
+
visitRegExpAST,
|
|
21
|
+
RegExpParser,
|
|
22
|
+
} = require("@eslint-community/regexpp");
|
|
23
|
+
const { canTokensBeAdjacent } = require("./utils/ast-utils");
|
|
24
|
+
const { REGEXPP_LATEST_ECMA_VERSION } = require("./utils/regular-expressions");
|
|
25
|
+
|
|
26
|
+
//------------------------------------------------------------------------------
|
|
27
|
+
// Helpers
|
|
28
|
+
//------------------------------------------------------------------------------
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Determines whether the given node is a string literal.
|
|
32
|
+
* @param {ASTNode} node Node to check.
|
|
33
|
+
* @returns {boolean} True if the node is a string literal.
|
|
34
|
+
*/
|
|
35
|
+
function isStringLiteral(node) {
|
|
36
|
+
return node.type === "Literal" && typeof node.value === "string";
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Determines whether the given node is a regex literal.
|
|
41
|
+
* @param {ASTNode} node Node to check.
|
|
42
|
+
* @returns {boolean} True if the node is a regex literal.
|
|
43
|
+
*/
|
|
44
|
+
function isRegexLiteral(node) {
|
|
45
|
+
return node.type === "Literal" && Object.hasOwn(node, "regex");
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const validPrecedingTokens = new Set([
|
|
49
|
+
"(",
|
|
50
|
+
";",
|
|
51
|
+
"[",
|
|
52
|
+
",",
|
|
53
|
+
"=",
|
|
54
|
+
"+",
|
|
55
|
+
"*",
|
|
56
|
+
"-",
|
|
57
|
+
"?",
|
|
58
|
+
"~",
|
|
59
|
+
"%",
|
|
60
|
+
"**",
|
|
61
|
+
"!",
|
|
62
|
+
"typeof",
|
|
63
|
+
"instanceof",
|
|
64
|
+
"&&",
|
|
65
|
+
"||",
|
|
66
|
+
"??",
|
|
67
|
+
"return",
|
|
68
|
+
"...",
|
|
69
|
+
"delete",
|
|
70
|
+
"void",
|
|
71
|
+
"in",
|
|
72
|
+
"<",
|
|
73
|
+
">",
|
|
74
|
+
"<=",
|
|
75
|
+
">=",
|
|
76
|
+
"==",
|
|
77
|
+
"===",
|
|
78
|
+
"!=",
|
|
79
|
+
"!==",
|
|
80
|
+
"<<",
|
|
81
|
+
">>",
|
|
82
|
+
">>>",
|
|
83
|
+
"&",
|
|
84
|
+
"|",
|
|
85
|
+
"^",
|
|
86
|
+
":",
|
|
87
|
+
"{",
|
|
88
|
+
"=>",
|
|
89
|
+
"*=",
|
|
90
|
+
"<<=",
|
|
91
|
+
">>=",
|
|
92
|
+
">>>=",
|
|
93
|
+
"^=",
|
|
94
|
+
"|=",
|
|
95
|
+
"&=",
|
|
96
|
+
"??=",
|
|
97
|
+
"||=",
|
|
98
|
+
"&&=",
|
|
99
|
+
"**=",
|
|
100
|
+
"+=",
|
|
101
|
+
"-=",
|
|
102
|
+
"/=",
|
|
103
|
+
"%=",
|
|
104
|
+
"/",
|
|
105
|
+
"do",
|
|
106
|
+
"break",
|
|
107
|
+
"continue",
|
|
108
|
+
"debugger",
|
|
109
|
+
"case",
|
|
110
|
+
"throw",
|
|
111
|
+
]);
|
|
112
|
+
|
|
113
|
+
//------------------------------------------------------------------------------
|
|
114
|
+
// Rule Definition
|
|
115
|
+
//------------------------------------------------------------------------------
|
|
116
|
+
|
|
117
|
+
/** @type {import('../types').Rule.RuleModule} */
|
|
118
|
+
module.exports = {
|
|
119
|
+
meta: {
|
|
120
|
+
type: "suggestion",
|
|
121
|
+
|
|
122
|
+
defaultOptions: [
|
|
123
|
+
{
|
|
124
|
+
disallowRedundantWrapping: false,
|
|
125
|
+
},
|
|
126
|
+
],
|
|
127
|
+
|
|
128
|
+
docs: {
|
|
129
|
+
description:
|
|
130
|
+
"Disallow use of the `RegExp` constructor in favor of regular expression literals",
|
|
131
|
+
recommended: false,
|
|
132
|
+
url: "https://eslint.org/docs/latest/rules/prefer-regex-literals",
|
|
133
|
+
},
|
|
134
|
+
|
|
135
|
+
hasSuggestions: true,
|
|
136
|
+
|
|
137
|
+
schema: [
|
|
138
|
+
{
|
|
139
|
+
type: "object",
|
|
140
|
+
properties: {
|
|
141
|
+
disallowRedundantWrapping: {
|
|
142
|
+
type: "boolean",
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
additionalProperties: false,
|
|
146
|
+
},
|
|
147
|
+
],
|
|
148
|
+
|
|
149
|
+
messages: {
|
|
150
|
+
unexpectedRegExp:
|
|
151
|
+
"Use a regular expression literal instead of the 'RegExp' constructor.",
|
|
152
|
+
replaceWithLiteral:
|
|
153
|
+
"Replace with an equivalent regular expression literal.",
|
|
154
|
+
replaceWithLiteralAndFlags:
|
|
155
|
+
"Replace with an equivalent regular expression literal with flags '{{ flags }}'.",
|
|
156
|
+
replaceWithIntendedLiteralAndFlags:
|
|
157
|
+
"Replace with a regular expression literal with flags '{{ flags }}'.",
|
|
158
|
+
unexpectedRedundantRegExp:
|
|
159
|
+
"Regular expression literal is unnecessarily wrapped within a 'RegExp' constructor.",
|
|
160
|
+
unexpectedRedundantRegExpWithFlags:
|
|
161
|
+
"Use regular expression literal with flags instead of the 'RegExp' constructor.",
|
|
162
|
+
},
|
|
163
|
+
},
|
|
164
|
+
|
|
165
|
+
create(context) {
|
|
166
|
+
const [{ disallowRedundantWrapping }] = context.options;
|
|
167
|
+
const sourceCode = context.sourceCode;
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Determines whether the given node is a String.raw`` tagged template expression
|
|
171
|
+
* with a static template literal.
|
|
172
|
+
* @param {ASTNode} node Node to check.
|
|
173
|
+
* @returns {boolean} True if the node is String.raw`` with a static template.
|
|
174
|
+
*/
|
|
175
|
+
function isStringRawTaggedStaticTemplateLiteral(node) {
|
|
176
|
+
return (
|
|
177
|
+
node.type === "TaggedTemplateExpression" &&
|
|
178
|
+
astUtils.isSpecificMemberAccess(node.tag, "String", "raw") &&
|
|
179
|
+
sourceCode.isGlobalReference(
|
|
180
|
+
astUtils.skipChainExpression(node.tag).object,
|
|
181
|
+
) &&
|
|
182
|
+
astUtils.isStaticTemplateLiteral(node.quasi)
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Gets the value of a string
|
|
188
|
+
* @param {ASTNode} node The node to get the string of.
|
|
189
|
+
* @returns {string|null} The value of the node.
|
|
190
|
+
*/
|
|
191
|
+
function getStringValue(node) {
|
|
192
|
+
if (isStringLiteral(node)) {
|
|
193
|
+
return node.value;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (astUtils.isStaticTemplateLiteral(node)) {
|
|
197
|
+
return node.quasis[0].value.cooked;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (isStringRawTaggedStaticTemplateLiteral(node)) {
|
|
201
|
+
return node.quasi.quasis[0].value.raw;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return null;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Determines whether the given node is considered to be a static string by the logic of this rule.
|
|
209
|
+
* @param {ASTNode} node Node to check.
|
|
210
|
+
* @returns {boolean} True if the node is a static string.
|
|
211
|
+
*/
|
|
212
|
+
function isStaticString(node) {
|
|
213
|
+
return (
|
|
214
|
+
isStringLiteral(node) ||
|
|
215
|
+
astUtils.isStaticTemplateLiteral(node) ||
|
|
216
|
+
isStringRawTaggedStaticTemplateLiteral(node)
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Determines whether the relevant arguments of the given are all static string literals.
|
|
222
|
+
* @param {ASTNode} node Node to check.
|
|
223
|
+
* @returns {boolean} True if all arguments are static strings.
|
|
224
|
+
*/
|
|
225
|
+
function hasOnlyStaticStringArguments(node) {
|
|
226
|
+
const args = node.arguments;
|
|
227
|
+
|
|
228
|
+
if (
|
|
229
|
+
(args.length === 1 || args.length === 2) &&
|
|
230
|
+
args.every(isStaticString)
|
|
231
|
+
) {
|
|
232
|
+
return true;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return false;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Determines whether the arguments of the given node indicate that a regex literal is unnecessarily wrapped.
|
|
240
|
+
* @param {ASTNode} node Node to check.
|
|
241
|
+
* @returns {boolean} True if the node already contains a regex literal argument.
|
|
242
|
+
*/
|
|
243
|
+
function isUnnecessarilyWrappedRegexLiteral(node) {
|
|
244
|
+
const args = node.arguments;
|
|
245
|
+
|
|
246
|
+
if (args.length === 1 && isRegexLiteral(args[0])) {
|
|
247
|
+
return true;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (
|
|
251
|
+
args.length === 2 &&
|
|
252
|
+
isRegexLiteral(args[0]) &&
|
|
253
|
+
isStaticString(args[1])
|
|
254
|
+
) {
|
|
255
|
+
return true;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return false;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Returns a ecmaVersion compatible for regexpp.
|
|
263
|
+
* @param {number} ecmaVersion The ecmaVersion to convert.
|
|
264
|
+
* @returns {import("@eslint-community/regexpp/ecma-versions").EcmaVersion} The resulting ecmaVersion compatible for regexpp.
|
|
265
|
+
*/
|
|
266
|
+
function getRegexppEcmaVersion(ecmaVersion) {
|
|
267
|
+
if (ecmaVersion <= 5) {
|
|
268
|
+
return 5;
|
|
269
|
+
}
|
|
270
|
+
return Math.min(ecmaVersion, REGEXPP_LATEST_ECMA_VERSION);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const regexppEcmaVersion = getRegexppEcmaVersion(
|
|
274
|
+
context.languageOptions.ecmaVersion,
|
|
275
|
+
);
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Makes a character escaped or else returns null.
|
|
279
|
+
* @param {string} character The character to escape.
|
|
280
|
+
* @returns {string} The resulting escaped character.
|
|
281
|
+
*/
|
|
282
|
+
function resolveEscapes(character) {
|
|
283
|
+
switch (character) {
|
|
284
|
+
case "\n":
|
|
285
|
+
case "\\\n":
|
|
286
|
+
return "\\n";
|
|
287
|
+
|
|
288
|
+
case "\r":
|
|
289
|
+
case "\\\r":
|
|
290
|
+
return "\\r";
|
|
291
|
+
|
|
292
|
+
case "\t":
|
|
293
|
+
case "\\\t":
|
|
294
|
+
return "\\t";
|
|
295
|
+
|
|
296
|
+
case "\v":
|
|
297
|
+
case "\\\v":
|
|
298
|
+
return "\\v";
|
|
299
|
+
|
|
300
|
+
case "\f":
|
|
301
|
+
case "\\\f":
|
|
302
|
+
return "\\f";
|
|
303
|
+
|
|
304
|
+
case "/":
|
|
305
|
+
return "\\/";
|
|
306
|
+
|
|
307
|
+
default:
|
|
308
|
+
return null;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Checks whether the given regex and flags are valid for the ecma version or not.
|
|
314
|
+
* @param {string} pattern The regex pattern to check.
|
|
315
|
+
* @param {string | undefined} flags The regex flags to check.
|
|
316
|
+
* @returns {boolean} True if the given regex pattern and flags are valid for the ecma version.
|
|
317
|
+
*/
|
|
318
|
+
function isValidRegexForEcmaVersion(pattern, flags) {
|
|
319
|
+
const validator = new RegExpValidator({
|
|
320
|
+
ecmaVersion: regexppEcmaVersion,
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
try {
|
|
324
|
+
validator.validatePattern(pattern, 0, pattern.length, {
|
|
325
|
+
unicode: flags ? flags.includes("u") : false,
|
|
326
|
+
unicodeSets: flags ? flags.includes("v") : false,
|
|
327
|
+
});
|
|
328
|
+
if (flags) {
|
|
329
|
+
validator.validateFlags(flags);
|
|
330
|
+
}
|
|
331
|
+
return true;
|
|
332
|
+
} catch {
|
|
333
|
+
return false;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Checks whether two given regex flags contain the same flags or not.
|
|
339
|
+
* @param {string} flagsA The regex flags.
|
|
340
|
+
* @param {string} flagsB The regex flags.
|
|
341
|
+
* @returns {boolean} True if two regex flags contain same flags.
|
|
342
|
+
*/
|
|
343
|
+
function areFlagsEqual(flagsA, flagsB) {
|
|
344
|
+
return [...flagsA].sort().join("") === [...flagsB].sort().join("");
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Merges two regex flags.
|
|
349
|
+
* @param {string} flagsA The regex flags.
|
|
350
|
+
* @param {string} flagsB The regex flags.
|
|
351
|
+
* @returns {string} The merged regex flags.
|
|
352
|
+
*/
|
|
353
|
+
function mergeRegexFlags(flagsA, flagsB) {
|
|
354
|
+
const flagsSet = new Set([...flagsA, ...flagsB]);
|
|
355
|
+
|
|
356
|
+
return [...flagsSet].join("");
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* Checks whether a give node can be fixed to the given regex pattern and flags.
|
|
361
|
+
* @param {ASTNode} node The node to check.
|
|
362
|
+
* @param {string} pattern The regex pattern to check.
|
|
363
|
+
* @param {string} flags The regex flags
|
|
364
|
+
* @returns {boolean} True if a node can be fixed to the given regex pattern and flags.
|
|
365
|
+
*/
|
|
366
|
+
function canFixTo(node, pattern, flags) {
|
|
367
|
+
const tokenBefore = sourceCode.getTokenBefore(node);
|
|
368
|
+
|
|
369
|
+
return (
|
|
370
|
+
sourceCode.getCommentsInside(node).length === 0 &&
|
|
371
|
+
(!tokenBefore || validPrecedingTokens.has(tokenBefore.value)) &&
|
|
372
|
+
isValidRegexForEcmaVersion(pattern, flags)
|
|
373
|
+
);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* Returns a safe output code considering the before and after tokens.
|
|
378
|
+
* @param {ASTNode} node The regex node.
|
|
379
|
+
* @param {string} newRegExpValue The new regex expression value.
|
|
380
|
+
* @returns {string} The output code.
|
|
381
|
+
*/
|
|
382
|
+
function getSafeOutput(node, newRegExpValue) {
|
|
383
|
+
const tokenBefore = sourceCode.getTokenBefore(node);
|
|
384
|
+
const tokenAfter = sourceCode.getTokenAfter(node);
|
|
385
|
+
|
|
386
|
+
return (
|
|
387
|
+
(tokenBefore &&
|
|
388
|
+
!canTokensBeAdjacent(tokenBefore, newRegExpValue) &&
|
|
389
|
+
tokenBefore.range[1] === node.range[0]
|
|
390
|
+
? " "
|
|
391
|
+
: "") +
|
|
392
|
+
newRegExpValue +
|
|
393
|
+
(tokenAfter &&
|
|
394
|
+
!canTokensBeAdjacent(newRegExpValue, tokenAfter) &&
|
|
395
|
+
node.range[1] === tokenAfter.range[0]
|
|
396
|
+
? " "
|
|
397
|
+
: "")
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
return {
|
|
402
|
+
Program(node) {
|
|
403
|
+
const scope = sourceCode.getScope(node);
|
|
404
|
+
const tracker = new ReferenceTracker(scope);
|
|
405
|
+
const traceMap = {
|
|
406
|
+
RegExp: {
|
|
407
|
+
[CALL]: true,
|
|
408
|
+
[CONSTRUCT]: true,
|
|
409
|
+
},
|
|
410
|
+
};
|
|
411
|
+
|
|
412
|
+
for (const { node: refNode } of tracker.iterateGlobalReferences(
|
|
413
|
+
traceMap,
|
|
414
|
+
)) {
|
|
415
|
+
if (
|
|
416
|
+
disallowRedundantWrapping &&
|
|
417
|
+
isUnnecessarilyWrappedRegexLiteral(refNode)
|
|
418
|
+
) {
|
|
419
|
+
const regexNode = refNode.arguments[0];
|
|
420
|
+
|
|
421
|
+
if (refNode.arguments.length === 2) {
|
|
422
|
+
const suggests = [];
|
|
423
|
+
|
|
424
|
+
const argFlags =
|
|
425
|
+
getStringValue(refNode.arguments[1]) || "";
|
|
426
|
+
|
|
427
|
+
if (
|
|
428
|
+
canFixTo(
|
|
429
|
+
refNode,
|
|
430
|
+
regexNode.regex.pattern,
|
|
431
|
+
argFlags,
|
|
432
|
+
)
|
|
433
|
+
) {
|
|
434
|
+
suggests.push({
|
|
435
|
+
messageId: "replaceWithLiteralAndFlags",
|
|
436
|
+
pattern: regexNode.regex.pattern,
|
|
437
|
+
flags: argFlags,
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
const literalFlags = regexNode.regex.flags || "";
|
|
442
|
+
const mergedFlags = mergeRegexFlags(
|
|
443
|
+
literalFlags,
|
|
444
|
+
argFlags,
|
|
445
|
+
);
|
|
446
|
+
|
|
447
|
+
if (
|
|
448
|
+
!areFlagsEqual(mergedFlags, argFlags) &&
|
|
449
|
+
canFixTo(
|
|
450
|
+
refNode,
|
|
451
|
+
regexNode.regex.pattern,
|
|
452
|
+
mergedFlags,
|
|
453
|
+
)
|
|
454
|
+
) {
|
|
455
|
+
suggests.push({
|
|
456
|
+
messageId:
|
|
457
|
+
"replaceWithIntendedLiteralAndFlags",
|
|
458
|
+
pattern: regexNode.regex.pattern,
|
|
459
|
+
flags: mergedFlags,
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
context.report({
|
|
464
|
+
node: refNode,
|
|
465
|
+
messageId: "unexpectedRedundantRegExpWithFlags",
|
|
466
|
+
suggest: suggests.map(
|
|
467
|
+
({ flags, pattern, messageId }) => ({
|
|
468
|
+
messageId,
|
|
469
|
+
data: {
|
|
470
|
+
flags,
|
|
471
|
+
},
|
|
472
|
+
fix(fixer) {
|
|
473
|
+
return fixer.replaceText(
|
|
474
|
+
refNode,
|
|
475
|
+
getSafeOutput(
|
|
476
|
+
refNode,
|
|
477
|
+
`/${pattern}/${flags}`,
|
|
478
|
+
),
|
|
479
|
+
);
|
|
480
|
+
},
|
|
481
|
+
}),
|
|
482
|
+
),
|
|
483
|
+
});
|
|
484
|
+
} else {
|
|
485
|
+
const outputs = [];
|
|
486
|
+
|
|
487
|
+
if (
|
|
488
|
+
canFixTo(
|
|
489
|
+
refNode,
|
|
490
|
+
regexNode.regex.pattern,
|
|
491
|
+
regexNode.regex.flags,
|
|
492
|
+
)
|
|
493
|
+
) {
|
|
494
|
+
outputs.push(sourceCode.getText(regexNode));
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
context.report({
|
|
498
|
+
node: refNode,
|
|
499
|
+
messageId: "unexpectedRedundantRegExp",
|
|
500
|
+
suggest: outputs.map(output => ({
|
|
501
|
+
messageId: "replaceWithLiteral",
|
|
502
|
+
fix(fixer) {
|
|
503
|
+
return fixer.replaceText(
|
|
504
|
+
refNode,
|
|
505
|
+
getSafeOutput(refNode, output),
|
|
506
|
+
);
|
|
507
|
+
},
|
|
508
|
+
})),
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
} else if (hasOnlyStaticStringArguments(refNode)) {
|
|
512
|
+
let regexContent = getStringValue(refNode.arguments[0]);
|
|
513
|
+
let noFix = false;
|
|
514
|
+
let flags;
|
|
515
|
+
|
|
516
|
+
if (refNode.arguments[1]) {
|
|
517
|
+
flags = getStringValue(refNode.arguments[1]);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
if (!canFixTo(refNode, regexContent, flags)) {
|
|
521
|
+
noFix = true;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
if (
|
|
525
|
+
!/^[-\w\\[\](){} \t\r\n\v\f!@#$%^&*+=/~`.><?,'"|:;]*$/u.test(
|
|
526
|
+
regexContent,
|
|
527
|
+
)
|
|
528
|
+
) {
|
|
529
|
+
noFix = true;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
if (regexContent && !noFix) {
|
|
533
|
+
let charIncrease = 0;
|
|
534
|
+
|
|
535
|
+
const ast = new RegExpParser({
|
|
536
|
+
ecmaVersion: regexppEcmaVersion,
|
|
537
|
+
}).parsePattern(
|
|
538
|
+
regexContent,
|
|
539
|
+
0,
|
|
540
|
+
regexContent.length,
|
|
541
|
+
{
|
|
542
|
+
unicode: flags
|
|
543
|
+
? flags.includes("u")
|
|
544
|
+
: false,
|
|
545
|
+
unicodeSets: flags
|
|
546
|
+
? flags.includes("v")
|
|
547
|
+
: false,
|
|
548
|
+
},
|
|
549
|
+
);
|
|
550
|
+
|
|
551
|
+
visitRegExpAST(ast, {
|
|
552
|
+
onCharacterEnter(characterNode) {
|
|
553
|
+
const escaped = resolveEscapes(
|
|
554
|
+
characterNode.raw,
|
|
555
|
+
);
|
|
556
|
+
|
|
557
|
+
if (escaped) {
|
|
558
|
+
regexContent =
|
|
559
|
+
regexContent.slice(
|
|
560
|
+
0,
|
|
561
|
+
characterNode.start +
|
|
562
|
+
charIncrease,
|
|
563
|
+
) +
|
|
564
|
+
escaped +
|
|
565
|
+
regexContent.slice(
|
|
566
|
+
characterNode.end +
|
|
567
|
+
charIncrease,
|
|
568
|
+
);
|
|
569
|
+
|
|
570
|
+
if (characterNode.raw.length === 1) {
|
|
571
|
+
charIncrease += 1;
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
},
|
|
575
|
+
});
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
const newRegExpValue = `/${regexContent || "(?:)"}/${flags || ""}`;
|
|
579
|
+
|
|
580
|
+
context.report({
|
|
581
|
+
node: refNode,
|
|
582
|
+
messageId: "unexpectedRegExp",
|
|
583
|
+
suggest: noFix
|
|
584
|
+
? []
|
|
585
|
+
: [
|
|
586
|
+
{
|
|
587
|
+
messageId: "replaceWithLiteral",
|
|
588
|
+
fix(fixer) {
|
|
589
|
+
return fixer.replaceText(
|
|
590
|
+
refNode,
|
|
591
|
+
getSafeOutput(
|
|
592
|
+
refNode,
|
|
593
|
+
newRegExpValue,
|
|
594
|
+
),
|
|
595
|
+
);
|
|
596
|
+
},
|
|
597
|
+
},
|
|
598
|
+
],
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
},
|
|
603
|
+
};
|
|
604
|
+
},
|
|
605
|
+
};
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Rule to
|
|
3
|
+
* @author Toru Nagashima
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
"use strict";
|
|
7
|
+
|
|
8
|
+
//------------------------------------------------------------------------------
|
|
9
|
+
// Helpers
|
|
10
|
+
//------------------------------------------------------------------------------
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Gets the variable object of `arguments` which is defined implicitly.
|
|
14
|
+
* @param {eslint-scope.Scope} scope A scope to get.
|
|
15
|
+
* @returns {eslint-scope.Variable} The found variable object.
|
|
16
|
+
*/
|
|
17
|
+
function getVariableOfArguments(scope) {
|
|
18
|
+
const variables = scope.variables;
|
|
19
|
+
|
|
20
|
+
for (let i = 0; i < variables.length; ++i) {
|
|
21
|
+
const variable = variables[i];
|
|
22
|
+
|
|
23
|
+
if (variable.name === "arguments") {
|
|
24
|
+
/*
|
|
25
|
+
* If there was a parameter which is named "arguments", the implicit "arguments" is not defined.
|
|
26
|
+
* So does fast return with null.
|
|
27
|
+
*/
|
|
28
|
+
return variable.identifiers.length === 0 ? variable : null;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/* c8 ignore next */
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Checks if the given reference is not normal member access.
|
|
38
|
+
*
|
|
39
|
+
* - arguments .... true // not member access
|
|
40
|
+
* - arguments[i] .... true // computed member access
|
|
41
|
+
* - arguments[0] .... true // computed member access
|
|
42
|
+
* - arguments.length .... false // normal member access
|
|
43
|
+
* @param {eslint-scope.Reference} reference The reference to check.
|
|
44
|
+
* @returns {boolean} `true` if the reference is not normal member access.
|
|
45
|
+
*/
|
|
46
|
+
function isNotNormalMemberAccess(reference) {
|
|
47
|
+
const id = reference.identifier;
|
|
48
|
+
const parent = id.parent;
|
|
49
|
+
|
|
50
|
+
return !(
|
|
51
|
+
parent.type === "MemberExpression" &&
|
|
52
|
+
parent.object === id &&
|
|
53
|
+
!parent.computed
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
//------------------------------------------------------------------------------
|
|
58
|
+
// Rule Definition
|
|
59
|
+
//------------------------------------------------------------------------------
|
|
60
|
+
|
|
61
|
+
/** @type {import('../types').Rule.RuleModule} */
|
|
62
|
+
module.exports = {
|
|
63
|
+
meta: {
|
|
64
|
+
type: "suggestion",
|
|
65
|
+
|
|
66
|
+
docs: {
|
|
67
|
+
description: "Require rest parameters instead of `arguments`",
|
|
68
|
+
recommended: false,
|
|
69
|
+
url: "https://eslint.org/docs/latest/rules/prefer-rest-params",
|
|
70
|
+
},
|
|
71
|
+
|
|
72
|
+
schema: [],
|
|
73
|
+
|
|
74
|
+
messages: {
|
|
75
|
+
preferRestParams: "Use the rest parameters instead of 'arguments'.",
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
|
|
79
|
+
create(context) {
|
|
80
|
+
const sourceCode = context.sourceCode;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Reports a given reference.
|
|
84
|
+
* @param {eslint-scope.Reference} reference A reference to report.
|
|
85
|
+
* @returns {void}
|
|
86
|
+
*/
|
|
87
|
+
function report(reference) {
|
|
88
|
+
context.report({
|
|
89
|
+
node: reference.identifier,
|
|
90
|
+
loc: reference.identifier.loc,
|
|
91
|
+
messageId: "preferRestParams",
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Reports references of the implicit `arguments` variable if exist.
|
|
97
|
+
* @param {ASTNode} node The node representing the function.
|
|
98
|
+
* @returns {void}
|
|
99
|
+
*/
|
|
100
|
+
function checkForArguments(node) {
|
|
101
|
+
const argumentsVar = getVariableOfArguments(
|
|
102
|
+
sourceCode.getScope(node),
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
if (argumentsVar) {
|
|
106
|
+
argumentsVar.references
|
|
107
|
+
.filter(isNotNormalMemberAccess)
|
|
108
|
+
.forEach(report);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
"FunctionDeclaration:exit": checkForArguments,
|
|
114
|
+
"FunctionExpression:exit": checkForArguments,
|
|
115
|
+
};
|
|
116
|
+
},
|
|
117
|
+
};
|