diff --git a/node_modules/@esbuild/darwin-arm64/README.md b/node_modules/@esbuild/darwin-arm64/README.md deleted file mode 100644 index c2c0398db..000000000 --- a/node_modules/@esbuild/darwin-arm64/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# esbuild - -This is the macOS ARM 64-bit binary for esbuild, a JavaScript bundler and minifier. See https://github.com/evanw/esbuild for details. diff --git a/node_modules/@esbuild/darwin-arm64/bin/esbuild b/node_modules/@esbuild/darwin-arm64/bin/esbuild deleted file mode 100755 index 8935f4075..000000000 Binary files a/node_modules/@esbuild/darwin-arm64/bin/esbuild and /dev/null differ diff --git a/node_modules/@esbuild/darwin-arm64/package.json b/node_modules/@esbuild/darwin-arm64/package.json deleted file mode 100644 index d4d3c9f7f..000000000 --- a/node_modules/@esbuild/darwin-arm64/package.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "name": "@esbuild/darwin-arm64", - "version": "0.27.2", - "description": "The macOS ARM 64-bit binary for esbuild, a JavaScript bundler.", - "repository": { - "type": "git", - "url": "git+https://github.com/evanw/esbuild.git" - }, - "license": "MIT", - "preferUnplugged": true, - "engines": { - "node": ">=18" - }, - "os": [ - "darwin" - ], - "cpu": [ - "arm64" - ] -} diff --git a/node_modules/@eslint/eslintrc/README.md b/node_modules/@eslint/eslintrc/README.md index 846cd120d..baccb206a 100644 --- a/node_modules/@eslint/eslintrc/README.md +++ b/node_modules/@eslint/eslintrc/README.md @@ -135,10 +135,10 @@ The following companies, organizations, and individuals support ESLint's ongoing to get your logo on our READMEs and [website](https://eslint.org/sponsors).

Platinum Sponsors

-

Automattic Airbnb

Gold Sponsors

+

Automattic

Gold Sponsors

Qlty Software Shopify

Silver Sponsors

-

Vite Liftoff American Express StackBlitz

Bronze Sponsors

-

Cybozu Syntax N-iX Ltd Icons8 Discord GitBook Nx Mercedes-Benz Group HeroCoders LambdaTest

+

Vite Liftoff StackBlitz

Bronze Sponsors

+

Cybozu SAP CrawlJobs Depot N-iX Ltd Icons8 Discord GitBook HeroCoders TestMu AI Open Source Office (Formerly LambdaTest)

Technology Sponsors

Technology sponsors allow us to use their products and services for free as part of a contribution to the open source ecosystem and our work.

Netlify Algolia 1Password

diff --git a/node_modules/@eslint/eslintrc/package.json b/node_modules/@eslint/eslintrc/package.json index 030d3fc71..337fa7169 100644 --- a/node_modules/@eslint/eslintrc/package.json +++ b/node_modules/@eslint/eslintrc/package.json @@ -1,6 +1,6 @@ { "name": "@eslint/eslintrc", - "version": "3.3.3", + "version": "3.3.4", "description": "The legacy ESLintRC config file format for ESLint", "type": "module", "main": "./dist/eslintrc.cjs", @@ -62,14 +62,14 @@ "typescript": "^5.7.3" }, "dependencies": { - "ajv": "^6.12.4", + "ajv": "^6.14.0", "debug": "^4.3.2", "espree": "^10.0.1", "globals": "^14.0.0", "ignore": "^5.2.0", "import-fresh": "^3.2.1", "js-yaml": "^4.1.1", - "minimatch": "^3.1.2", + "minimatch": "^3.1.3", "strip-json-comments": "^3.1.1" }, "engines": { diff --git a/node_modules/@eslint/js/LICENSE b/node_modules/@eslint/js/LICENSE deleted file mode 100644 index b607bb36e..000000000 --- a/node_modules/@eslint/js/LICENSE +++ /dev/null @@ -1,19 +0,0 @@ -Copyright OpenJS Foundation and other contributors, - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. diff --git a/node_modules/@eslint/js/README.md b/node_modules/@eslint/js/README.md deleted file mode 100644 index eae3d22e0..000000000 --- a/node_modules/@eslint/js/README.md +++ /dev/null @@ -1,103 +0,0 @@ -[![npm version](https://img.shields.io/npm/v/@eslint/js.svg)](https://www.npmjs.com/package/@eslint/js) -[![Downloads](https://img.shields.io/npm/dm/@eslint/js.svg)](https://www.npmjs.com/package/@eslint/js) -[![Build Status](https://github.com/eslint/eslint/workflows/CI/badge.svg)](https://github.com/eslint/eslint/actions) -
-[![Open Collective Backers](https://img.shields.io/opencollective/backers/eslint)](https://opencollective.com/eslint) -[![Open Collective Sponsors](https://img.shields.io/opencollective/sponsors/eslint)](https://opencollective.com/eslint) - -# ESLint JavaScript Plugin - -[Website](https://eslint.org) | -[Configure ESLint](https://eslint.org/docs/latest/use/configure) | -[Rules](https://eslint.org/docs/rules/) | -[Contribute to ESLint](https://eslint.org/docs/latest/contribute) | -[Report Bugs](https://eslint.org/docs/latest/contribute/report-bugs) | -[Code of Conduct](https://eslint.org/conduct) | -[X](https://x.com/geteslint) | -[Discord](https://eslint.org/chat) | -[Mastodon](https://fosstodon.org/@eslint) | -[Bluesky](https://bsky.app/profile/eslint.org) - -The beginnings of separating out JavaScript-specific functionality from ESLint. - -Right now, this plugin contains two configurations: - -- `recommended` - enables the rules recommended by the ESLint team (the replacement for `"eslint:recommended"`) -- `all` - enables all ESLint rules (the replacement for `"eslint:all"`) - -## Installation - -You can install ESLint using npm or other package managers: - -```shell -npm install eslint -D -# or -yarn add eslint -D -# or -pnpm install eslint -D -# or -bun add eslint -D -``` - -Then install this plugin: - -```shell -npm install @eslint/js -D -# or -yarn add @eslint/js -D -# or -pnpm install @eslint/js -D -# or -bun add @eslint/js -D -``` - -## Usage - -Use in your `eslint.config.js` file anytime you want to extend one of the configs: - -```js -import { defineConfig } from "eslint/config"; -import js from "@eslint/js"; - -export default defineConfig([ - // apply recommended rules to JS files - { - name: "your-project/recommended-rules", - files: ["**/*.js"], - plugins: { - js, - }, - extends: ["js/recommended"], - }, - - // apply recommended rules to JS files with an override - { - name: "your-project/recommended-rules-with-override", - files: ["**/*.js"], - plugins: { - js, - }, - extends: ["js/recommended"], - rules: { - "no-unused-vars": "warn", - }, - }, - - // apply all rules to JS files - { - name: "your-project/all-rules", - files: ["**/*.js"], - plugins: { - js, - }, - extends: ["js/all"], - rules: { - "no-unused-vars": "warn", - }, - }, -]); -``` - -## License - -MIT diff --git a/node_modules/@eslint/js/package.json b/node_modules/@eslint/js/package.json deleted file mode 100644 index 3d7b130c9..000000000 --- a/node_modules/@eslint/js/package.json +++ /dev/null @@ -1,36 +0,0 @@ -{ - "name": "@eslint/js", - "version": "9.39.2", - "description": "ESLint JavaScript language implementation", - "funding": "https://eslint.org/donate", - "main": "./src/index.js", - "types": "./types/index.d.ts", - "scripts": { - "test:types": "tsc -p tests/types/tsconfig.json" - }, - "files": [ - "LICENSE", - "README.md", - "src", - "types" - ], - "publishConfig": { - "access": "public" - }, - "repository": { - "type": "git", - "url": "https://github.com/eslint/eslint.git", - "directory": "packages/js" - }, - "homepage": "https://eslint.org", - "bugs": "https://github.com/eslint/eslint/issues/", - "keywords": [ - "javascript", - "eslint-plugin", - "eslint" - ], - "license": "MIT", - "engines": { - "node": "^18.18.0 || ^20.9.0 || >=21.1.0" - } -} diff --git a/node_modules/@eslint/js/src/configs/eslint-all.js b/node_modules/@eslint/js/src/configs/eslint-all.js deleted file mode 100644 index 248950d98..000000000 --- a/node_modules/@eslint/js/src/configs/eslint-all.js +++ /dev/null @@ -1,217 +0,0 @@ -/* - * WARNING: This file is autogenerated using the tools/update-eslint-all.js - * script. Do not edit manually. - */ -"use strict"; - -/* - * IMPORTANT! - * - * We cannot add a "name" property to this object because it's still used in eslintrc - * which doesn't support the "name" property. If we add a "name" property, it will - * cause an error. - */ - -module.exports = Object.freeze({ - rules: Object.freeze({ - "accessor-pairs": "error", - "array-callback-return": "error", - "arrow-body-style": "error", - "block-scoped-var": "error", - "camelcase": "error", - "capitalized-comments": "error", - "class-methods-use-this": "error", - "complexity": "error", - "consistent-return": "error", - "consistent-this": "error", - "constructor-super": "error", - "curly": "error", - "default-case": "error", - "default-case-last": "error", - "default-param-last": "error", - "dot-notation": "error", - "eqeqeq": "error", - "for-direction": "error", - "func-name-matching": "error", - "func-names": "error", - "func-style": "error", - "getter-return": "error", - "grouped-accessor-pairs": "error", - "guard-for-in": "error", - "id-denylist": "error", - "id-length": "error", - "id-match": "error", - "init-declarations": "error", - "logical-assignment-operators": "error", - "max-classes-per-file": "error", - "max-depth": "error", - "max-lines": "error", - "max-lines-per-function": "error", - "max-nested-callbacks": "error", - "max-params": "error", - "max-statements": "error", - "new-cap": "error", - "no-alert": "error", - "no-array-constructor": "error", - "no-async-promise-executor": "error", - "no-await-in-loop": "error", - "no-bitwise": "error", - "no-caller": "error", - "no-case-declarations": "error", - "no-class-assign": "error", - "no-compare-neg-zero": "error", - "no-cond-assign": "error", - "no-console": "error", - "no-const-assign": "error", - "no-constant-binary-expression": "error", - "no-constant-condition": "error", - "no-constructor-return": "error", - "no-continue": "error", - "no-control-regex": "error", - "no-debugger": "error", - "no-delete-var": "error", - "no-div-regex": "error", - "no-dupe-args": "error", - "no-dupe-class-members": "error", - "no-dupe-else-if": "error", - "no-dupe-keys": "error", - "no-duplicate-case": "error", - "no-duplicate-imports": "error", - "no-else-return": "error", - "no-empty": "error", - "no-empty-character-class": "error", - "no-empty-function": "error", - "no-empty-pattern": "error", - "no-empty-static-block": "error", - "no-eq-null": "error", - "no-eval": "error", - "no-ex-assign": "error", - "no-extend-native": "error", - "no-extra-bind": "error", - "no-extra-boolean-cast": "error", - "no-extra-label": "error", - "no-fallthrough": "error", - "no-func-assign": "error", - "no-global-assign": "error", - "no-implicit-coercion": "error", - "no-implicit-globals": "error", - "no-implied-eval": "error", - "no-import-assign": "error", - "no-inline-comments": "error", - "no-inner-declarations": "error", - "no-invalid-regexp": "error", - "no-invalid-this": "error", - "no-irregular-whitespace": "error", - "no-iterator": "error", - "no-label-var": "error", - "no-labels": "error", - "no-lone-blocks": "error", - "no-lonely-if": "error", - "no-loop-func": "error", - "no-loss-of-precision": "error", - "no-magic-numbers": "error", - "no-misleading-character-class": "error", - "no-multi-assign": "error", - "no-multi-str": "error", - "no-negated-condition": "error", - "no-nested-ternary": "error", - "no-new": "error", - "no-new-func": "error", - "no-new-native-nonconstructor": "error", - "no-new-wrappers": "error", - "no-nonoctal-decimal-escape": "error", - "no-obj-calls": "error", - "no-object-constructor": "error", - "no-octal": "error", - "no-octal-escape": "error", - "no-param-reassign": "error", - "no-plusplus": "error", - "no-promise-executor-return": "error", - "no-proto": "error", - "no-prototype-builtins": "error", - "no-redeclare": "error", - "no-regex-spaces": "error", - "no-restricted-exports": "error", - "no-restricted-globals": "error", - "no-restricted-imports": "error", - "no-restricted-properties": "error", - "no-restricted-syntax": "error", - "no-return-assign": "error", - "no-script-url": "error", - "no-self-assign": "error", - "no-self-compare": "error", - "no-sequences": "error", - "no-setter-return": "error", - "no-shadow": "error", - "no-shadow-restricted-names": "error", - "no-sparse-arrays": "error", - "no-template-curly-in-string": "error", - "no-ternary": "error", - "no-this-before-super": "error", - "no-throw-literal": "error", - "no-unassigned-vars": "error", - "no-undef": "error", - "no-undef-init": "error", - "no-undefined": "error", - "no-underscore-dangle": "error", - "no-unexpected-multiline": "error", - "no-unmodified-loop-condition": "error", - "no-unneeded-ternary": "error", - "no-unreachable": "error", - "no-unreachable-loop": "error", - "no-unsafe-finally": "error", - "no-unsafe-negation": "error", - "no-unsafe-optional-chaining": "error", - "no-unused-expressions": "error", - "no-unused-labels": "error", - "no-unused-private-class-members": "error", - "no-unused-vars": "error", - "no-use-before-define": "error", - "no-useless-assignment": "error", - "no-useless-backreference": "error", - "no-useless-call": "error", - "no-useless-catch": "error", - "no-useless-computed-key": "error", - "no-useless-concat": "error", - "no-useless-constructor": "error", - "no-useless-escape": "error", - "no-useless-rename": "error", - "no-useless-return": "error", - "no-var": "error", - "no-void": "error", - "no-warning-comments": "error", - "no-with": "error", - "object-shorthand": "error", - "one-var": "error", - "operator-assignment": "error", - "prefer-arrow-callback": "error", - "prefer-const": "error", - "prefer-destructuring": "error", - "prefer-exponentiation-operator": "error", - "prefer-named-capture-group": "error", - "prefer-numeric-literals": "error", - "prefer-object-has-own": "error", - "prefer-object-spread": "error", - "prefer-promise-reject-errors": "error", - "prefer-regex-literals": "error", - "prefer-rest-params": "error", - "prefer-spread": "error", - "prefer-template": "error", - "preserve-caught-error": "error", - "radix": "error", - "require-atomic-updates": "error", - "require-await": "error", - "require-unicode-regexp": "error", - "require-yield": "error", - "sort-imports": "error", - "sort-keys": "error", - "sort-vars": "error", - "strict": "error", - "symbol-description": "error", - "unicode-bom": "error", - "use-isnan": "error", - "valid-typeof": "error", - "vars-on-top": "error", - "yoda": "error" - }) -}); diff --git a/node_modules/@eslint/js/src/configs/eslint-recommended.js b/node_modules/@eslint/js/src/configs/eslint-recommended.js deleted file mode 100644 index 9ff6a398f..000000000 --- a/node_modules/@eslint/js/src/configs/eslint-recommended.js +++ /dev/null @@ -1,79 +0,0 @@ -/* - * WARNING: This file is autogenerated using the tools/update-eslint-recommended.js - * script. Do not edit manually. - */ -"use strict"; - -/* - * IMPORTANT! - * - * We cannot add a "name" property to this object because it's still used in eslintrc - * which doesn't support the "name" property. If we add a "name" property, it will - * cause an error. - */ - -module.exports = Object.freeze({ - rules: Object.freeze({ - "constructor-super": "error", - "for-direction": "error", - "getter-return": "error", - "no-async-promise-executor": "error", - "no-case-declarations": "error", - "no-class-assign": "error", - "no-compare-neg-zero": "error", - "no-cond-assign": "error", - "no-const-assign": "error", - "no-constant-binary-expression": "error", - "no-constant-condition": "error", - "no-control-regex": "error", - "no-debugger": "error", - "no-delete-var": "error", - "no-dupe-args": "error", - "no-dupe-class-members": "error", - "no-dupe-else-if": "error", - "no-dupe-keys": "error", - "no-duplicate-case": "error", - "no-empty": "error", - "no-empty-character-class": "error", - "no-empty-pattern": "error", - "no-empty-static-block": "error", - "no-ex-assign": "error", - "no-extra-boolean-cast": "error", - "no-fallthrough": "error", - "no-func-assign": "error", - "no-global-assign": "error", - "no-import-assign": "error", - "no-invalid-regexp": "error", - "no-irregular-whitespace": "error", - "no-loss-of-precision": "error", - "no-misleading-character-class": "error", - "no-new-native-nonconstructor": "error", - "no-nonoctal-decimal-escape": "error", - "no-obj-calls": "error", - "no-octal": "error", - "no-prototype-builtins": "error", - "no-redeclare": "error", - "no-regex-spaces": "error", - "no-self-assign": "error", - "no-setter-return": "error", - "no-shadow-restricted-names": "error", - "no-sparse-arrays": "error", - "no-this-before-super": "error", - "no-undef": "error", - "no-unexpected-multiline": "error", - "no-unreachable": "error", - "no-unsafe-finally": "error", - "no-unsafe-negation": "error", - "no-unsafe-optional-chaining": "error", - "no-unused-labels": "error", - "no-unused-private-class-members": "error", - "no-unused-vars": "error", - "no-useless-backreference": "error", - "no-useless-catch": "error", - "no-useless-escape": "error", - "no-with": "error", - "require-yield": "error", - "use-isnan": "error", - "valid-typeof": "error" - }), -}); diff --git a/node_modules/@eslint/js/src/index.js b/node_modules/@eslint/js/src/index.js deleted file mode 100644 index ff6a21d87..000000000 --- a/node_modules/@eslint/js/src/index.js +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @fileoverview Main package entrypoint. - * @author Nicholas C. Zakas - */ - -"use strict"; - -const { name, version } = require("../package.json"); - -//------------------------------------------------------------------------------ -// Public Interface -//------------------------------------------------------------------------------ - -module.exports = { - meta: { - name, - version, - }, - configs: { - all: require("./configs/eslint-all"), - recommended: require("./configs/eslint-recommended"), - }, -}; diff --git a/node_modules/@eslint/js/types/index.d.ts b/node_modules/@eslint/js/types/index.d.ts deleted file mode 100644 index d4921d8bf..000000000 --- a/node_modules/@eslint/js/types/index.d.ts +++ /dev/null @@ -1,14 +0,0 @@ -import type { Linter } from "eslint"; - -declare const js: { - readonly meta: { - readonly name: string; - readonly version: string; - }; - readonly configs: { - readonly recommended: { readonly rules: Readonly }; - readonly all: { readonly rules: Readonly }; - }; -}; - -export = js; diff --git a/node_modules/@rollup/rollup-darwin-arm64/README.md b/node_modules/@rollup/rollup-darwin-arm64/README.md deleted file mode 100644 index c29619c30..000000000 --- a/node_modules/@rollup/rollup-darwin-arm64/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# `@rollup/rollup-darwin-arm64` - -This is the **aarch64-apple-darwin** binary for `rollup` diff --git a/node_modules/@rollup/rollup-darwin-arm64/package.json b/node_modules/@rollup/rollup-darwin-arm64/package.json deleted file mode 100644 index fc4b22a8d..000000000 --- a/node_modules/@rollup/rollup-darwin-arm64/package.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "name": "@rollup/rollup-darwin-arm64", - "version": "4.54.0", - "os": [ - "darwin" - ], - "cpu": [ - "arm64" - ], - "files": [ - "rollup.darwin-arm64.node" - ], - "description": "Native bindings for Rollup", - "author": "Lukas Taegert-Atkinson", - "homepage": "https://rollupjs.org/", - "license": "MIT", - "repository": { - "type": "git", - "url": "git+https://github.com/rollup/rollup.git" - }, - "main": "./rollup.darwin-arm64.node" -} \ No newline at end of file diff --git a/node_modules/@rollup/rollup-darwin-arm64/rollup.darwin-arm64.node b/node_modules/@rollup/rollup-darwin-arm64/rollup.darwin-arm64.node deleted file mode 100644 index 463a41b7d..000000000 Binary files a/node_modules/@rollup/rollup-darwin-arm64/rollup.darwin-arm64.node and /dev/null differ diff --git a/node_modules/acorn/CHANGELOG.md b/node_modules/acorn/CHANGELOG.md index c86068cd7..d18759aea 100644 --- a/node_modules/acorn/CHANGELOG.md +++ b/node_modules/acorn/CHANGELOG.md @@ -1,3 +1,21 @@ +## 8.16.0 (2026-02-19) + +### New features + +The `sourceType` option can now be set to `"commonjs"` to have the parser treat the top level scope as a function scope. + +Add support for Unicode 17. + +### Bug fixes + +Don't recognize `await using` as contextual keywords when followed directly by a backslash. + +Fix an issue where the parser would allow `return` statements in `static` blocks when `allowReturnOutsideFunction` was enabled. + +Properly reject `using` declarations that appear directly in `switch` or `for` head scopes. + +Fix some corner case issues in the recognition of `using` syntax. + ## 8.15.0 (2025-06-08) ### New features diff --git a/node_modules/acorn/README.md b/node_modules/acorn/README.md index f7ff96624..962de0272 100644 --- a/node_modules/acorn/README.md +++ b/node_modules/acorn/README.md @@ -26,6 +26,24 @@ git clone https://github.com/acornjs/acorn.git cd acorn npm install ``` +## Importing acorn + +ESM as well as CommonJS is supported for all 3: `acorn`, `acorn-walk` and `acorn-loose`. + +ESM example for `acorn`: + +```js +import * as acorn from "acorn" +``` + +CommonJS example for `acorn`: + +```js +let acorn = require("acorn") +``` + +ESM is preferred, as it allows better editor auto-completions by offering TypeScript support. +For this reason, following examples will use ESM imports. ## Interface @@ -36,8 +54,8 @@ syntax tree object as specified by the [ESTree spec](https://github.com/estree/estree). ```javascript -let acorn = require("acorn"); -console.log(acorn.parse("1 + 1", {ecmaVersion: 2020})); +import * as acorn from "acorn" +console.log(acorn.parse("1 + 1", {ecmaVersion: 2020})) ``` When encountering a syntax error, the parser will raise a @@ -61,11 +79,12 @@ required): implemented through plugins. - **sourceType**: Indicate the mode the code should be parsed in. Can be - either `"script"` or `"module"`. This influences global strict mode + either `"script"`, `"module"` or `"commonjs"`. This influences global strict mode and parsing of `import` and `export` declarations. **NOTE**: If set to `"module"`, then static `import` / `export` syntax - will be valid, even if `ecmaVersion` is less than 6. + will be valid, even if `ecmaVersion` is less than 6. If set to `"commonjs"`, + it is the same as `"script"` except that the top-level scope behaves like a function. - **onInsertedSemicolon**: If given a callback, that callback will be called whenever a missing semicolon is inserted by the parser. The @@ -97,7 +116,7 @@ required): for `ecmaVersion` 2022 and later, `false` for lower versions. Setting this option to `true` allows to have top-level `await` expressions. They are still not allowed in non-`async` functions, - though. + though. Setting this option to `true` is not allowed when `sourceType: "commonjs"`. - **allowSuperOutsideMethod**: By default, `super` outside a method raises an error. Set this to `true` to accept such code. @@ -217,7 +236,7 @@ for (let token of acorn.tokenizer(str)) { } // transform code to array of tokens: -var tokens = [...acorn.tokenizer(str)]; +var tokens = [...acorn.tokenizer(str)] ``` **tokTypes** holds an object mapping names to the token type objects @@ -238,10 +257,10 @@ on the extended version of the class. To extend a parser with plugins, you can use its static `extend` method. ```javascript -var acorn = require("acorn"); -var jsx = require("acorn-jsx"); -var JSXParser = acorn.Parser.extend(jsx()); -JSXParser.parse("foo()", {ecmaVersion: 2020}); +var acorn = require("acorn") +var jsx = require("acorn-jsx") +var JSXParser = acorn.Parser.extend(jsx()) +JSXParser.parse("foo()", {ecmaVersion: 2020}) ``` The `extend` method takes any number of plugin values, and returns a diff --git a/node_modules/acorn/dist/acorn.d.mts b/node_modules/acorn/dist/acorn.d.mts index f2ec5243b..afbd9139e 100644 --- a/node_modules/acorn/dist/acorn.d.mts +++ b/node_modules/acorn/dist/acorn.d.mts @@ -614,10 +614,10 @@ export interface Options { /** * `sourceType` indicates the mode the code should be parsed in. - * Can be either `"script"` or `"module"`. This influences global + * Can be either `"script"`, `"module"` or `"commonjs"`. This influences global * strict mode and parsing of `import` and `export` declarations. */ - sourceType?: "script" | "module" + sourceType?: "script" | "module" | "commonjs" /** * a callback that will be called when a semicolon is automatically inserted. diff --git a/node_modules/acorn/dist/acorn.d.ts b/node_modules/acorn/dist/acorn.d.ts index f2ec5243b..afbd9139e 100644 --- a/node_modules/acorn/dist/acorn.d.ts +++ b/node_modules/acorn/dist/acorn.d.ts @@ -614,10 +614,10 @@ export interface Options { /** * `sourceType` indicates the mode the code should be parsed in. - * Can be either `"script"` or `"module"`. This influences global + * Can be either `"script"`, `"module"` or `"commonjs"`. This influences global * strict mode and parsing of `import` and `export` declarations. */ - sourceType?: "script" | "module" + sourceType?: "script" | "module" | "commonjs" /** * a callback that will be called when a semicolon is automatically inserted. diff --git a/node_modules/acorn/dist/acorn.js b/node_modules/acorn/dist/acorn.js index cb5628bf8..b4f281a46 100644 --- a/node_modules/acorn/dist/acorn.js +++ b/node_modules/acorn/dist/acorn.js @@ -5,16 +5,16 @@ })(this, (function (exports) { 'use strict'; // This file was generated. Do not modify manually! - var astralIdentifierCodes = [509, 0, 227, 0, 150, 4, 294, 9, 1368, 2, 2, 1, 6, 3, 41, 2, 5, 0, 166, 1, 574, 3, 9, 9, 7, 9, 32, 4, 318, 1, 80, 3, 71, 10, 50, 3, 123, 2, 54, 14, 32, 10, 3, 1, 11, 3, 46, 10, 8, 0, 46, 9, 7, 2, 37, 13, 2, 9, 6, 1, 45, 0, 13, 2, 49, 13, 9, 3, 2, 11, 83, 11, 7, 0, 3, 0, 158, 11, 6, 9, 7, 3, 56, 1, 2, 6, 3, 1, 3, 2, 10, 0, 11, 1, 3, 6, 4, 4, 68, 8, 2, 0, 3, 0, 2, 3, 2, 4, 2, 0, 15, 1, 83, 17, 10, 9, 5, 0, 82, 19, 13, 9, 214, 6, 3, 8, 28, 1, 83, 16, 16, 9, 82, 12, 9, 9, 7, 19, 58, 14, 5, 9, 243, 14, 166, 9, 71, 5, 2, 1, 3, 3, 2, 0, 2, 1, 13, 9, 120, 6, 3, 6, 4, 0, 29, 9, 41, 6, 2, 3, 9, 0, 10, 10, 47, 15, 343, 9, 54, 7, 2, 7, 17, 9, 57, 21, 2, 13, 123, 5, 4, 0, 2, 1, 2, 6, 2, 0, 9, 9, 49, 4, 2, 1, 2, 4, 9, 9, 330, 3, 10, 1, 2, 0, 49, 6, 4, 4, 14, 10, 5350, 0, 7, 14, 11465, 27, 2343, 9, 87, 9, 39, 4, 60, 6, 26, 9, 535, 9, 470, 0, 2, 54, 8, 3, 82, 0, 12, 1, 19628, 1, 4178, 9, 519, 45, 3, 22, 543, 4, 4, 5, 9, 7, 3, 6, 31, 3, 149, 2, 1418, 49, 513, 54, 5, 49, 9, 0, 15, 0, 23, 4, 2, 14, 1361, 6, 2, 16, 3, 6, 2, 1, 2, 4, 101, 0, 161, 6, 10, 9, 357, 0, 62, 13, 499, 13, 245, 1, 2, 9, 726, 6, 110, 6, 6, 9, 4759, 9, 787719, 239]; + var astralIdentifierCodes = [509, 0, 227, 0, 150, 4, 294, 9, 1368, 2, 2, 1, 6, 3, 41, 2, 5, 0, 166, 1, 574, 3, 9, 9, 7, 9, 32, 4, 318, 1, 78, 5, 71, 10, 50, 3, 123, 2, 54, 14, 32, 10, 3, 1, 11, 3, 46, 10, 8, 0, 46, 9, 7, 2, 37, 13, 2, 9, 6, 1, 45, 0, 13, 2, 49, 13, 9, 3, 2, 11, 83, 11, 7, 0, 3, 0, 158, 11, 6, 9, 7, 3, 56, 1, 2, 6, 3, 1, 3, 2, 10, 0, 11, 1, 3, 6, 4, 4, 68, 8, 2, 0, 3, 0, 2, 3, 2, 4, 2, 0, 15, 1, 83, 17, 10, 9, 5, 0, 82, 19, 13, 9, 214, 6, 3, 8, 28, 1, 83, 16, 16, 9, 82, 12, 9, 9, 7, 19, 58, 14, 5, 9, 243, 14, 166, 9, 71, 5, 2, 1, 3, 3, 2, 0, 2, 1, 13, 9, 120, 6, 3, 6, 4, 0, 29, 9, 41, 6, 2, 3, 9, 0, 10, 10, 47, 15, 199, 7, 137, 9, 54, 7, 2, 7, 17, 9, 57, 21, 2, 13, 123, 5, 4, 0, 2, 1, 2, 6, 2, 0, 9, 9, 49, 4, 2, 1, 2, 4, 9, 9, 55, 9, 266, 3, 10, 1, 2, 0, 49, 6, 4, 4, 14, 10, 5350, 0, 7, 14, 11465, 27, 2343, 9, 87, 9, 39, 4, 60, 6, 26, 9, 535, 9, 470, 0, 2, 54, 8, 3, 82, 0, 12, 1, 19628, 1, 4178, 9, 519, 45, 3, 22, 543, 4, 4, 5, 9, 7, 3, 6, 31, 3, 149, 2, 1418, 49, 513, 54, 5, 49, 9, 0, 15, 0, 23, 4, 2, 14, 1361, 6, 2, 16, 3, 6, 2, 1, 2, 4, 101, 0, 161, 6, 10, 9, 357, 0, 62, 13, 499, 13, 245, 1, 2, 9, 233, 0, 3, 0, 8, 1, 6, 0, 475, 6, 110, 6, 6, 9, 4759, 9, 787719, 239]; // This file was generated. Do not modify manually! - var astralIdentifierStartCodes = [0, 11, 2, 25, 2, 18, 2, 1, 2, 14, 3, 13, 35, 122, 70, 52, 268, 28, 4, 48, 48, 31, 14, 29, 6, 37, 11, 29, 3, 35, 5, 7, 2, 4, 43, 157, 19, 35, 5, 35, 5, 39, 9, 51, 13, 10, 2, 14, 2, 6, 2, 1, 2, 10, 2, 14, 2, 6, 2, 1, 4, 51, 13, 310, 10, 21, 11, 7, 25, 5, 2, 41, 2, 8, 70, 5, 3, 0, 2, 43, 2, 1, 4, 0, 3, 22, 11, 22, 10, 30, 66, 18, 2, 1, 11, 21, 11, 25, 71, 55, 7, 1, 65, 0, 16, 3, 2, 2, 2, 28, 43, 28, 4, 28, 36, 7, 2, 27, 28, 53, 11, 21, 11, 18, 14, 17, 111, 72, 56, 50, 14, 50, 14, 35, 39, 27, 10, 22, 251, 41, 7, 1, 17, 2, 60, 28, 11, 0, 9, 21, 43, 17, 47, 20, 28, 22, 13, 52, 58, 1, 3, 0, 14, 44, 33, 24, 27, 35, 30, 0, 3, 0, 9, 34, 4, 0, 13, 47, 15, 3, 22, 0, 2, 0, 36, 17, 2, 24, 20, 1, 64, 6, 2, 0, 2, 3, 2, 14, 2, 9, 8, 46, 39, 7, 3, 1, 3, 21, 2, 6, 2, 1, 2, 4, 4, 0, 19, 0, 13, 4, 31, 9, 2, 0, 3, 0, 2, 37, 2, 0, 26, 0, 2, 0, 45, 52, 19, 3, 21, 2, 31, 47, 21, 1, 2, 0, 185, 46, 42, 3, 37, 47, 21, 0, 60, 42, 14, 0, 72, 26, 38, 6, 186, 43, 117, 63, 32, 7, 3, 0, 3, 7, 2, 1, 2, 23, 16, 0, 2, 0, 95, 7, 3, 38, 17, 0, 2, 0, 29, 0, 11, 39, 8, 0, 22, 0, 12, 45, 20, 0, 19, 72, 200, 32, 32, 8, 2, 36, 18, 0, 50, 29, 113, 6, 2, 1, 2, 37, 22, 0, 26, 5, 2, 1, 2, 31, 15, 0, 328, 18, 16, 0, 2, 12, 2, 33, 125, 0, 80, 921, 103, 110, 18, 195, 2637, 96, 16, 1071, 18, 5, 26, 3994, 6, 582, 6842, 29, 1763, 568, 8, 30, 18, 78, 18, 29, 19, 47, 17, 3, 32, 20, 6, 18, 433, 44, 212, 63, 129, 74, 6, 0, 67, 12, 65, 1, 2, 0, 29, 6135, 9, 1237, 42, 9, 8936, 3, 2, 6, 2, 1, 2, 290, 16, 0, 30, 2, 3, 0, 15, 3, 9, 395, 2309, 106, 6, 12, 4, 8, 8, 9, 5991, 84, 2, 70, 2, 1, 3, 0, 3, 1, 3, 3, 2, 11, 2, 0, 2, 6, 2, 64, 2, 3, 3, 7, 2, 6, 2, 27, 2, 3, 2, 4, 2, 0, 4, 6, 2, 339, 3, 24, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 7, 1845, 30, 7, 5, 262, 61, 147, 44, 11, 6, 17, 0, 322, 29, 19, 43, 485, 27, 229, 29, 3, 0, 496, 6, 2, 3, 2, 1, 2, 14, 2, 196, 60, 67, 8, 0, 1205, 3, 2, 26, 2, 1, 2, 0, 3, 0, 2, 9, 2, 3, 2, 0, 2, 0, 7, 0, 5, 0, 2, 0, 2, 0, 2, 2, 2, 1, 2, 0, 3, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 1, 2, 0, 3, 3, 2, 6, 2, 3, 2, 3, 2, 0, 2, 9, 2, 16, 6, 2, 2, 4, 2, 16, 4421, 42719, 33, 4153, 7, 221, 3, 5761, 15, 7472, 16, 621, 2467, 541, 1507, 4938, 6, 4191]; + var astralIdentifierStartCodes = [0, 11, 2, 25, 2, 18, 2, 1, 2, 14, 3, 13, 35, 122, 70, 52, 268, 28, 4, 48, 48, 31, 14, 29, 6, 37, 11, 29, 3, 35, 5, 7, 2, 4, 43, 157, 19, 35, 5, 35, 5, 39, 9, 51, 13, 10, 2, 14, 2, 6, 2, 1, 2, 10, 2, 14, 2, 6, 2, 1, 4, 51, 13, 310, 10, 21, 11, 7, 25, 5, 2, 41, 2, 8, 70, 5, 3, 0, 2, 43, 2, 1, 4, 0, 3, 22, 11, 22, 10, 30, 66, 18, 2, 1, 11, 21, 11, 25, 7, 25, 39, 55, 7, 1, 65, 0, 16, 3, 2, 2, 2, 28, 43, 28, 4, 28, 36, 7, 2, 27, 28, 53, 11, 21, 11, 18, 14, 17, 111, 72, 56, 50, 14, 50, 14, 35, 39, 27, 10, 22, 251, 41, 7, 1, 17, 5, 57, 28, 11, 0, 9, 21, 43, 17, 47, 20, 28, 22, 13, 52, 58, 1, 3, 0, 14, 44, 33, 24, 27, 35, 30, 0, 3, 0, 9, 34, 4, 0, 13, 47, 15, 3, 22, 0, 2, 0, 36, 17, 2, 24, 20, 1, 64, 6, 2, 0, 2, 3, 2, 14, 2, 9, 8, 46, 39, 7, 3, 1, 3, 21, 2, 6, 2, 1, 2, 4, 4, 0, 19, 0, 13, 4, 31, 9, 2, 0, 3, 0, 2, 37, 2, 0, 26, 0, 2, 0, 45, 52, 19, 3, 21, 2, 31, 47, 21, 1, 2, 0, 185, 46, 42, 3, 37, 47, 21, 0, 60, 42, 14, 0, 72, 26, 38, 6, 186, 43, 117, 63, 32, 7, 3, 0, 3, 7, 2, 1, 2, 23, 16, 0, 2, 0, 95, 7, 3, 38, 17, 0, 2, 0, 29, 0, 11, 39, 8, 0, 22, 0, 12, 45, 20, 0, 19, 72, 200, 32, 32, 8, 2, 36, 18, 0, 50, 29, 113, 6, 2, 1, 2, 37, 22, 0, 26, 5, 2, 1, 2, 31, 15, 0, 24, 43, 261, 18, 16, 0, 2, 12, 2, 33, 125, 0, 80, 921, 103, 110, 18, 195, 2637, 96, 16, 1071, 18, 5, 26, 3994, 6, 582, 6842, 29, 1763, 568, 8, 30, 18, 78, 18, 29, 19, 47, 17, 3, 32, 20, 6, 18, 433, 44, 212, 63, 33, 24, 3, 24, 45, 74, 6, 0, 67, 12, 65, 1, 2, 0, 15, 4, 10, 7381, 42, 31, 98, 114, 8702, 3, 2, 6, 2, 1, 2, 290, 16, 0, 30, 2, 3, 0, 15, 3, 9, 395, 2309, 106, 6, 12, 4, 8, 8, 9, 5991, 84, 2, 70, 2, 1, 3, 0, 3, 1, 3, 3, 2, 11, 2, 0, 2, 6, 2, 64, 2, 3, 3, 7, 2, 6, 2, 27, 2, 3, 2, 4, 2, 0, 4, 6, 2, 339, 3, 24, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 7, 1845, 30, 7, 5, 262, 61, 147, 44, 11, 6, 17, 0, 322, 29, 19, 43, 485, 27, 229, 29, 3, 0, 208, 30, 2, 2, 2, 1, 2, 6, 3, 4, 10, 1, 225, 6, 2, 3, 2, 1, 2, 14, 2, 196, 60, 67, 8, 0, 1205, 3, 2, 26, 2, 1, 2, 0, 3, 0, 2, 9, 2, 3, 2, 0, 2, 0, 7, 0, 5, 0, 2, 0, 2, 0, 2, 2, 2, 1, 2, 0, 3, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 1, 2, 0, 3, 3, 2, 6, 2, 3, 2, 3, 2, 0, 2, 9, 2, 16, 6, 2, 2, 4, 2, 16, 4421, 42719, 33, 4381, 3, 5773, 3, 7472, 16, 621, 2467, 541, 1507, 4938, 6, 8489]; // This file was generated. Do not modify manually! - var nonASCIIidentifierChars = "\u200c\u200d\xb7\u0300-\u036f\u0387\u0483-\u0487\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u064b-\u0669\u0670\u06d6-\u06dc\u06df-\u06e4\u06e7\u06e8\u06ea-\u06ed\u06f0-\u06f9\u0711\u0730-\u074a\u07a6-\u07b0\u07c0-\u07c9\u07eb-\u07f3\u07fd\u0816-\u0819\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0859-\u085b\u0897-\u089f\u08ca-\u08e1\u08e3-\u0903\u093a-\u093c\u093e-\u094f\u0951-\u0957\u0962\u0963\u0966-\u096f\u0981-\u0983\u09bc\u09be-\u09c4\u09c7\u09c8\u09cb-\u09cd\u09d7\u09e2\u09e3\u09e6-\u09ef\u09fe\u0a01-\u0a03\u0a3c\u0a3e-\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a66-\u0a71\u0a75\u0a81-\u0a83\u0abc\u0abe-\u0ac5\u0ac7-\u0ac9\u0acb-\u0acd\u0ae2\u0ae3\u0ae6-\u0aef\u0afa-\u0aff\u0b01-\u0b03\u0b3c\u0b3e-\u0b44\u0b47\u0b48\u0b4b-\u0b4d\u0b55-\u0b57\u0b62\u0b63\u0b66-\u0b6f\u0b82\u0bbe-\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcd\u0bd7\u0be6-\u0bef\u0c00-\u0c04\u0c3c\u0c3e-\u0c44\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62\u0c63\u0c66-\u0c6f\u0c81-\u0c83\u0cbc\u0cbe-\u0cc4\u0cc6-\u0cc8\u0cca-\u0ccd\u0cd5\u0cd6\u0ce2\u0ce3\u0ce6-\u0cef\u0cf3\u0d00-\u0d03\u0d3b\u0d3c\u0d3e-\u0d44\u0d46-\u0d48\u0d4a-\u0d4d\u0d57\u0d62\u0d63\u0d66-\u0d6f\u0d81-\u0d83\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0de6-\u0def\u0df2\u0df3\u0e31\u0e34-\u0e3a\u0e47-\u0e4e\u0e50-\u0e59\u0eb1\u0eb4-\u0ebc\u0ec8-\u0ece\u0ed0-\u0ed9\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f3e\u0f3f\u0f71-\u0f84\u0f86\u0f87\u0f8d-\u0f97\u0f99-\u0fbc\u0fc6\u102b-\u103e\u1040-\u1049\u1056-\u1059\u105e-\u1060\u1062-\u1064\u1067-\u106d\u1071-\u1074\u1082-\u108d\u108f-\u109d\u135d-\u135f\u1369-\u1371\u1712-\u1715\u1732-\u1734\u1752\u1753\u1772\u1773\u17b4-\u17d3\u17dd\u17e0-\u17e9\u180b-\u180d\u180f-\u1819\u18a9\u1920-\u192b\u1930-\u193b\u1946-\u194f\u19d0-\u19da\u1a17-\u1a1b\u1a55-\u1a5e\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1ab0-\u1abd\u1abf-\u1ace\u1b00-\u1b04\u1b34-\u1b44\u1b50-\u1b59\u1b6b-\u1b73\u1b80-\u1b82\u1ba1-\u1bad\u1bb0-\u1bb9\u1be6-\u1bf3\u1c24-\u1c37\u1c40-\u1c49\u1c50-\u1c59\u1cd0-\u1cd2\u1cd4-\u1ce8\u1ced\u1cf4\u1cf7-\u1cf9\u1dc0-\u1dff\u200c\u200d\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2cef-\u2cf1\u2d7f\u2de0-\u2dff\u302a-\u302f\u3099\u309a\u30fb\ua620-\ua629\ua66f\ua674-\ua67d\ua69e\ua69f\ua6f0\ua6f1\ua802\ua806\ua80b\ua823-\ua827\ua82c\ua880\ua881\ua8b4-\ua8c5\ua8d0-\ua8d9\ua8e0-\ua8f1\ua8ff-\ua909\ua926-\ua92d\ua947-\ua953\ua980-\ua983\ua9b3-\ua9c0\ua9d0-\ua9d9\ua9e5\ua9f0-\ua9f9\uaa29-\uaa36\uaa43\uaa4c\uaa4d\uaa50-\uaa59\uaa7b-\uaa7d\uaab0\uaab2-\uaab4\uaab7\uaab8\uaabe\uaabf\uaac1\uaaeb-\uaaef\uaaf5\uaaf6\uabe3-\uabea\uabec\uabed\uabf0-\uabf9\ufb1e\ufe00-\ufe0f\ufe20-\ufe2f\ufe33\ufe34\ufe4d-\ufe4f\uff10-\uff19\uff3f\uff65"; + var nonASCIIidentifierChars = "\u200c\u200d\xb7\u0300-\u036f\u0387\u0483-\u0487\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u064b-\u0669\u0670\u06d6-\u06dc\u06df-\u06e4\u06e7\u06e8\u06ea-\u06ed\u06f0-\u06f9\u0711\u0730-\u074a\u07a6-\u07b0\u07c0-\u07c9\u07eb-\u07f3\u07fd\u0816-\u0819\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0859-\u085b\u0897-\u089f\u08ca-\u08e1\u08e3-\u0903\u093a-\u093c\u093e-\u094f\u0951-\u0957\u0962\u0963\u0966-\u096f\u0981-\u0983\u09bc\u09be-\u09c4\u09c7\u09c8\u09cb-\u09cd\u09d7\u09e2\u09e3\u09e6-\u09ef\u09fe\u0a01-\u0a03\u0a3c\u0a3e-\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a66-\u0a71\u0a75\u0a81-\u0a83\u0abc\u0abe-\u0ac5\u0ac7-\u0ac9\u0acb-\u0acd\u0ae2\u0ae3\u0ae6-\u0aef\u0afa-\u0aff\u0b01-\u0b03\u0b3c\u0b3e-\u0b44\u0b47\u0b48\u0b4b-\u0b4d\u0b55-\u0b57\u0b62\u0b63\u0b66-\u0b6f\u0b82\u0bbe-\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcd\u0bd7\u0be6-\u0bef\u0c00-\u0c04\u0c3c\u0c3e-\u0c44\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62\u0c63\u0c66-\u0c6f\u0c81-\u0c83\u0cbc\u0cbe-\u0cc4\u0cc6-\u0cc8\u0cca-\u0ccd\u0cd5\u0cd6\u0ce2\u0ce3\u0ce6-\u0cef\u0cf3\u0d00-\u0d03\u0d3b\u0d3c\u0d3e-\u0d44\u0d46-\u0d48\u0d4a-\u0d4d\u0d57\u0d62\u0d63\u0d66-\u0d6f\u0d81-\u0d83\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0de6-\u0def\u0df2\u0df3\u0e31\u0e34-\u0e3a\u0e47-\u0e4e\u0e50-\u0e59\u0eb1\u0eb4-\u0ebc\u0ec8-\u0ece\u0ed0-\u0ed9\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f3e\u0f3f\u0f71-\u0f84\u0f86\u0f87\u0f8d-\u0f97\u0f99-\u0fbc\u0fc6\u102b-\u103e\u1040-\u1049\u1056-\u1059\u105e-\u1060\u1062-\u1064\u1067-\u106d\u1071-\u1074\u1082-\u108d\u108f-\u109d\u135d-\u135f\u1369-\u1371\u1712-\u1715\u1732-\u1734\u1752\u1753\u1772\u1773\u17b4-\u17d3\u17dd\u17e0-\u17e9\u180b-\u180d\u180f-\u1819\u18a9\u1920-\u192b\u1930-\u193b\u1946-\u194f\u19d0-\u19da\u1a17-\u1a1b\u1a55-\u1a5e\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1ab0-\u1abd\u1abf-\u1add\u1ae0-\u1aeb\u1b00-\u1b04\u1b34-\u1b44\u1b50-\u1b59\u1b6b-\u1b73\u1b80-\u1b82\u1ba1-\u1bad\u1bb0-\u1bb9\u1be6-\u1bf3\u1c24-\u1c37\u1c40-\u1c49\u1c50-\u1c59\u1cd0-\u1cd2\u1cd4-\u1ce8\u1ced\u1cf4\u1cf7-\u1cf9\u1dc0-\u1dff\u200c\u200d\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2cef-\u2cf1\u2d7f\u2de0-\u2dff\u302a-\u302f\u3099\u309a\u30fb\ua620-\ua629\ua66f\ua674-\ua67d\ua69e\ua69f\ua6f0\ua6f1\ua802\ua806\ua80b\ua823-\ua827\ua82c\ua880\ua881\ua8b4-\ua8c5\ua8d0-\ua8d9\ua8e0-\ua8f1\ua8ff-\ua909\ua926-\ua92d\ua947-\ua953\ua980-\ua983\ua9b3-\ua9c0\ua9d0-\ua9d9\ua9e5\ua9f0-\ua9f9\uaa29-\uaa36\uaa43\uaa4c\uaa4d\uaa50-\uaa59\uaa7b-\uaa7d\uaab0\uaab2-\uaab4\uaab7\uaab8\uaabe\uaabf\uaac1\uaaeb-\uaaef\uaaf5\uaaf6\uabe3-\uabea\uabec\uabed\uabf0-\uabf9\ufb1e\ufe00-\ufe0f\ufe20-\ufe2f\ufe33\ufe34\ufe4d-\ufe4f\uff10-\uff19\uff3f\uff65"; // This file was generated. Do not modify manually! - var nonASCIIidentifierStartChars = "\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376\u0377\u037a-\u037d\u037f\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u052f\u0531-\u0556\u0559\u0560-\u0588\u05d0-\u05ea\u05ef-\u05f2\u0620-\u064a\u066e\u066f\u0671-\u06d3\u06d5\u06e5\u06e6\u06ee\u06ef\u06fa-\u06fc\u06ff\u0710\u0712-\u072f\u074d-\u07a5\u07b1\u07ca-\u07ea\u07f4\u07f5\u07fa\u0800-\u0815\u081a\u0824\u0828\u0840-\u0858\u0860-\u086a\u0870-\u0887\u0889-\u088e\u08a0-\u08c9\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0980\u0985-\u098c\u098f\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bd\u09ce\u09dc\u09dd\u09df-\u09e1\u09f0\u09f1\u09fc\u0a05-\u0a0a\u0a0f\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32\u0a33\u0a35\u0a36\u0a38\u0a39\u0a59-\u0a5c\u0a5e\u0a72-\u0a74\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2\u0ab3\u0ab5-\u0ab9\u0abd\u0ad0\u0ae0\u0ae1\u0af9\u0b05-\u0b0c\u0b0f\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32\u0b33\u0b35-\u0b39\u0b3d\u0b5c\u0b5d\u0b5f-\u0b61\u0b71\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99\u0b9a\u0b9c\u0b9e\u0b9f\u0ba3\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bd0\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c39\u0c3d\u0c58-\u0c5a\u0c5d\u0c60\u0c61\u0c80\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cdd\u0cde\u0ce0\u0ce1\u0cf1\u0cf2\u0d04-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d\u0d4e\u0d54-\u0d56\u0d5f-\u0d61\u0d7a-\u0d7f\u0d85-\u0d96\u0d9a-\u0db1\u0db3-\u0dbb\u0dbd\u0dc0-\u0dc6\u0e01-\u0e30\u0e32\u0e33\u0e40-\u0e46\u0e81\u0e82\u0e84\u0e86-\u0e8a\u0e8c-\u0ea3\u0ea5\u0ea7-\u0eb0\u0eb2\u0eb3\u0ebd\u0ec0-\u0ec4\u0ec6\u0edc-\u0edf\u0f00\u0f40-\u0f47\u0f49-\u0f6c\u0f88-\u0f8c\u1000-\u102a\u103f\u1050-\u1055\u105a-\u105d\u1061\u1065\u1066\u106e-\u1070\u1075-\u1081\u108e\u10a0-\u10c5\u10c7\u10cd\u10d0-\u10fa\u10fc-\u1248\u124a-\u124d\u1250-\u1256\u1258\u125a-\u125d\u1260-\u1288\u128a-\u128d\u1290-\u12b0\u12b2-\u12b5\u12b8-\u12be\u12c0\u12c2-\u12c5\u12c8-\u12d6\u12d8-\u1310\u1312-\u1315\u1318-\u135a\u1380-\u138f\u13a0-\u13f5\u13f8-\u13fd\u1401-\u166c\u166f-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f8\u1700-\u1711\u171f-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1878\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191e\u1950-\u196d\u1970-\u1974\u1980-\u19ab\u19b0-\u19c9\u1a00-\u1a16\u1a20-\u1a54\u1aa7\u1b05-\u1b33\u1b45-\u1b4c\u1b83-\u1ba0\u1bae\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1c80-\u1c8a\u1c90-\u1cba\u1cbd-\u1cbf\u1ce9-\u1cec\u1cee-\u1cf3\u1cf5\u1cf6\u1cfa\u1d00-\u1dbf\u1e00-\u1f15\u1f18-\u1f1d\u1f20-\u1f45\u1f48-\u1f4d\u1f50-\u1f57\u1f59\u1f5b\u1f5d\u1f5f-\u1f7d\u1f80-\u1fb4\u1fb6-\u1fbc\u1fbe\u1fc2-\u1fc4\u1fc6-\u1fcc\u1fd0-\u1fd3\u1fd6-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ff4\u1ff6-\u1ffc\u2071\u207f\u2090-\u209c\u2102\u2107\u210a-\u2113\u2115\u2118-\u211d\u2124\u2126\u2128\u212a-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2ce4\u2ceb-\u2cee\u2cf2\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d80-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309b-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312f\u3131-\u318e\u31a0-\u31bf\u31f0-\u31ff\u3400-\u4dbf\u4e00-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua69d\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua7cd\ua7d0\ua7d1\ua7d3\ua7d5-\ua7dc\ua7f2-\ua801\ua803-\ua805\ua807-\ua80a\ua80c-\ua822\ua840-\ua873\ua882-\ua8b3\ua8f2-\ua8f7\ua8fb\ua8fd\ua8fe\ua90a-\ua925\ua930-\ua946\ua960-\ua97c\ua984-\ua9b2\ua9cf\ua9e0-\ua9e4\ua9e6-\ua9ef\ua9fa-\ua9fe\uaa00-\uaa28\uaa40-\uaa42\uaa44-\uaa4b\uaa60-\uaa76\uaa7a\uaa7e-\uaaaf\uaab1\uaab5\uaab6\uaab9-\uaabd\uaac0\uaac2\uaadb-\uaadd\uaae0-\uaaea\uaaf2-\uaaf4\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\uab30-\uab5a\uab5c-\uab69\uab70-\uabe2\uac00-\ud7a3\ud7b0-\ud7c6\ud7cb-\ud7fb\uf900-\ufa6d\ufa70-\ufad9\ufb00-\ufb06\ufb13-\ufb17\ufb1d\ufb1f-\ufb28\ufb2a-\ufb36\ufb38-\ufb3c\ufb3e\ufb40\ufb41\ufb43\ufb44\ufb46-\ufbb1\ufbd3-\ufd3d\ufd50-\ufd8f\ufd92-\ufdc7\ufdf0-\ufdfb\ufe70-\ufe74\ufe76-\ufefc\uff21-\uff3a\uff41-\uff5a\uff66-\uffbe\uffc2-\uffc7\uffca-\uffcf\uffd2-\uffd7\uffda-\uffdc"; + var nonASCIIidentifierStartChars = "\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376\u0377\u037a-\u037d\u037f\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u052f\u0531-\u0556\u0559\u0560-\u0588\u05d0-\u05ea\u05ef-\u05f2\u0620-\u064a\u066e\u066f\u0671-\u06d3\u06d5\u06e5\u06e6\u06ee\u06ef\u06fa-\u06fc\u06ff\u0710\u0712-\u072f\u074d-\u07a5\u07b1\u07ca-\u07ea\u07f4\u07f5\u07fa\u0800-\u0815\u081a\u0824\u0828\u0840-\u0858\u0860-\u086a\u0870-\u0887\u0889-\u088f\u08a0-\u08c9\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0980\u0985-\u098c\u098f\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bd\u09ce\u09dc\u09dd\u09df-\u09e1\u09f0\u09f1\u09fc\u0a05-\u0a0a\u0a0f\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32\u0a33\u0a35\u0a36\u0a38\u0a39\u0a59-\u0a5c\u0a5e\u0a72-\u0a74\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2\u0ab3\u0ab5-\u0ab9\u0abd\u0ad0\u0ae0\u0ae1\u0af9\u0b05-\u0b0c\u0b0f\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32\u0b33\u0b35-\u0b39\u0b3d\u0b5c\u0b5d\u0b5f-\u0b61\u0b71\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99\u0b9a\u0b9c\u0b9e\u0b9f\u0ba3\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bd0\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c39\u0c3d\u0c58-\u0c5a\u0c5c\u0c5d\u0c60\u0c61\u0c80\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cdc-\u0cde\u0ce0\u0ce1\u0cf1\u0cf2\u0d04-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d\u0d4e\u0d54-\u0d56\u0d5f-\u0d61\u0d7a-\u0d7f\u0d85-\u0d96\u0d9a-\u0db1\u0db3-\u0dbb\u0dbd\u0dc0-\u0dc6\u0e01-\u0e30\u0e32\u0e33\u0e40-\u0e46\u0e81\u0e82\u0e84\u0e86-\u0e8a\u0e8c-\u0ea3\u0ea5\u0ea7-\u0eb0\u0eb2\u0eb3\u0ebd\u0ec0-\u0ec4\u0ec6\u0edc-\u0edf\u0f00\u0f40-\u0f47\u0f49-\u0f6c\u0f88-\u0f8c\u1000-\u102a\u103f\u1050-\u1055\u105a-\u105d\u1061\u1065\u1066\u106e-\u1070\u1075-\u1081\u108e\u10a0-\u10c5\u10c7\u10cd\u10d0-\u10fa\u10fc-\u1248\u124a-\u124d\u1250-\u1256\u1258\u125a-\u125d\u1260-\u1288\u128a-\u128d\u1290-\u12b0\u12b2-\u12b5\u12b8-\u12be\u12c0\u12c2-\u12c5\u12c8-\u12d6\u12d8-\u1310\u1312-\u1315\u1318-\u135a\u1380-\u138f\u13a0-\u13f5\u13f8-\u13fd\u1401-\u166c\u166f-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f8\u1700-\u1711\u171f-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1878\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191e\u1950-\u196d\u1970-\u1974\u1980-\u19ab\u19b0-\u19c9\u1a00-\u1a16\u1a20-\u1a54\u1aa7\u1b05-\u1b33\u1b45-\u1b4c\u1b83-\u1ba0\u1bae\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1c80-\u1c8a\u1c90-\u1cba\u1cbd-\u1cbf\u1ce9-\u1cec\u1cee-\u1cf3\u1cf5\u1cf6\u1cfa\u1d00-\u1dbf\u1e00-\u1f15\u1f18-\u1f1d\u1f20-\u1f45\u1f48-\u1f4d\u1f50-\u1f57\u1f59\u1f5b\u1f5d\u1f5f-\u1f7d\u1f80-\u1fb4\u1fb6-\u1fbc\u1fbe\u1fc2-\u1fc4\u1fc6-\u1fcc\u1fd0-\u1fd3\u1fd6-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ff4\u1ff6-\u1ffc\u2071\u207f\u2090-\u209c\u2102\u2107\u210a-\u2113\u2115\u2118-\u211d\u2124\u2126\u2128\u212a-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2ce4\u2ceb-\u2cee\u2cf2\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d80-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309b-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312f\u3131-\u318e\u31a0-\u31bf\u31f0-\u31ff\u3400-\u4dbf\u4e00-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua69d\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua7dc\ua7f1-\ua801\ua803-\ua805\ua807-\ua80a\ua80c-\ua822\ua840-\ua873\ua882-\ua8b3\ua8f2-\ua8f7\ua8fb\ua8fd\ua8fe\ua90a-\ua925\ua930-\ua946\ua960-\ua97c\ua984-\ua9b2\ua9cf\ua9e0-\ua9e4\ua9e6-\ua9ef\ua9fa-\ua9fe\uaa00-\uaa28\uaa40-\uaa42\uaa44-\uaa4b\uaa60-\uaa76\uaa7a\uaa7e-\uaaaf\uaab1\uaab5\uaab6\uaab9-\uaabd\uaac0\uaac2\uaadb-\uaadd\uaae0-\uaaea\uaaf2-\uaaf4\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\uab30-\uab5a\uab5c-\uab69\uab70-\uabe2\uac00-\ud7a3\ud7b0-\ud7c6\ud7cb-\ud7fb\uf900-\ufa6d\ufa70-\ufad9\ufb00-\ufb06\ufb13-\ufb17\ufb1d\ufb1f-\ufb28\ufb2a-\ufb36\ufb38-\ufb3c\ufb3e\ufb40\ufb41\ufb43\ufb44\ufb46-\ufbb1\ufbd3-\ufd3d\ufd50-\ufd8f\ufd92-\ufdc7\ufdf0-\ufdfb\ufe70-\ufe74\ufe76-\ufefc\uff21-\uff3a\uff41-\uff5a\uff66-\uffbe\uffc2-\uffc7\uffca-\uffcf\uffd2-\uffd7\uffda-\uffdc"; // These are a run-length and offset encoded representation of the // >0xffff code points that are a valid part of identifiers. The @@ -339,7 +339,7 @@ // for new syntax features. ecmaVersion: null, // `sourceType` indicates the mode the code should be parsed in. - // Can be either `"script"` or `"module"`. This influences global + // Can be either `"script"`, `"module"` or `"commonjs"`. This influences global // strict mode and parsing of `import` and `export` declarations. sourceType: "script", // `onInsertedSemicolon` can be a callback that will be called when @@ -463,6 +463,9 @@ if (isArray(options.onComment)) { options.onComment = pushComment(options, options.onComment); } + if (options.sourceType === "commonjs" && options.allowAwaitOutsideFunction) + { throw new Error("Cannot use allowAwaitOutsideFunction with sourceType: commonjs") } + return options } @@ -494,6 +497,7 @@ SCOPE_DIRECT_SUPER = 128, SCOPE_CLASS_STATIC_BLOCK = 256, SCOPE_CLASS_FIELD_INIT = 512, + SCOPE_SWITCH = 1024, SCOPE_VAR = SCOPE_TOP | SCOPE_FUNCTION | SCOPE_CLASS_STATIC_BLOCK; function functionFlags(async, generator) { @@ -583,7 +587,12 @@ // Scope tracking for duplicate variable names (see scope.js) this.scopeStack = []; - this.enterScope(SCOPE_TOP); + this.enterScope( + this.options.sourceType === "commonjs" + // In commonjs, the top-level scope behaves like a function scope + ? SCOPE_FUNCTION + : SCOPE_TOP + ); // For RegExp validation this.regexpState = null; @@ -594,7 +603,7 @@ this.privateNameStack = []; }; - var prototypeAccessors = { inFunction: { configurable: true },inGenerator: { configurable: true },inAsync: { configurable: true },canAwait: { configurable: true },allowSuper: { configurable: true },allowDirectSuper: { configurable: true },treatFunctionsAsVar: { configurable: true },allowNewDotTarget: { configurable: true },inClassStaticBlock: { configurable: true } }; + var prototypeAccessors = { inFunction: { configurable: true },inGenerator: { configurable: true },inAsync: { configurable: true },canAwait: { configurable: true },allowReturn: { configurable: true },allowSuper: { configurable: true },allowDirectSuper: { configurable: true },treatFunctionsAsVar: { configurable: true },allowNewDotTarget: { configurable: true },allowUsing: { configurable: true },inClassStaticBlock: { configurable: true } }; Parser.prototype.parse = function parse () { var node = this.options.program || this.startNode(); @@ -618,6 +627,12 @@ return (this.inModule && this.options.ecmaVersion >= 13) || this.options.allowAwaitOutsideFunction }; + prototypeAccessors.allowReturn.get = function () { + if (this.inFunction) { return true } + if (this.options.allowReturnOutsideFunction && this.currentVarScope().flags & SCOPE_TOP) { return true } + return false + }; + prototypeAccessors.allowSuper.get = function () { var ref = this.currentThisScope(); var flags = ref.flags; @@ -638,6 +653,14 @@ return false }; + prototypeAccessors.allowUsing.get = function () { + var ref = this.currentScope(); + var flags = ref.flags; + if (flags & SCOPE_SWITCH) { return false } + if (!this.inModule && flags & SCOPE_TOP) { return false } + return true + }; + prototypeAccessors.inClassStaticBlock.get = function () { return (this.currentVarScope().flags & SCOPE_CLASS_STATIC_BLOCK) > 0 }; @@ -842,7 +865,7 @@ } } this.adaptDirectivePrologue(node.body); this.next(); - node.sourceType = this.options.sourceType; + node.sourceType = this.options.sourceType === "commonjs" ? "script" : this.options.sourceType; return this.finishNode(node, "Program") }; @@ -852,7 +875,7 @@ if (this.options.ecmaVersion < 6 || !this.isContextual("let")) { return false } skipWhiteSpace.lastIndex = this.pos; var skip = skipWhiteSpace.exec(this.input); - var next = this.pos + skip[0].length, nextCh = this.input.charCodeAt(next); + var next = this.pos + skip[0].length, nextCh = this.fullCharCodeAt(next); // For ambiguous cases, determine if a LexicalDeclaration (or only a // Statement) is allowed here. If context is not empty then only a Statement // is allowed. However, `let [` is an explicit negative lookahead for @@ -860,12 +883,13 @@ if (nextCh === 91 || nextCh === 92) { return true } // '[', '\' if (context) { return false } - if (nextCh === 123 || nextCh > 0xd7ff && nextCh < 0xdc00) { return true } // '{', astral - if (isIdentifierStart(nextCh, true)) { - var pos = next + 1; - while (isIdentifierChar(nextCh = this.input.charCodeAt(pos), true)) { ++pos; } - if (nextCh === 92 || nextCh > 0xd7ff && nextCh < 0xdc00) { return true } - var ident = this.input.slice(next, pos); + if (nextCh === 123) { return true } // '{' + if (isIdentifierStart(nextCh)) { + var start = next; + do { next += nextCh <= 0xffff ? 1 : 2; } + while (isIdentifierChar(nextCh = this.fullCharCodeAt(next))) + if (nextCh === 92) { return true } + var ident = this.input.slice(start, next); if (!keywordRelationalOperator.test(ident)) { return true } } return false @@ -884,7 +908,7 @@ return !lineBreak.test(this.input.slice(this.pos, next)) && this.input.slice(next, next + 8) === "function" && (next + 8 === this.input.length || - !(isIdentifierChar(after = this.input.charCodeAt(next + 8)) || after > 0xd7ff && after < 0xdc00)) + !(isIdentifierChar(after = this.fullCharCodeAt(next + 8)) || after === 92 /* '\' */)) }; pp$8.isUsingKeyword = function(isAwaitUsing, isFor) { @@ -898,28 +922,28 @@ if (lineBreak.test(this.input.slice(this.pos, next))) { return false } if (isAwaitUsing) { - var awaitEndPos = next + 5 /* await */, after; - if (this.input.slice(next, awaitEndPos) !== "using" || - awaitEndPos === this.input.length || - isIdentifierChar(after = this.input.charCodeAt(awaitEndPos)) || - (after > 0xd7ff && after < 0xdc00) + var usingEndPos = next + 5 /* using */, after; + if (this.input.slice(next, usingEndPos) !== "using" || + usingEndPos === this.input.length || + isIdentifierChar(after = this.fullCharCodeAt(usingEndPos)) || + after === 92 /* '\' */ ) { return false } - skipWhiteSpace.lastIndex = awaitEndPos; + skipWhiteSpace.lastIndex = usingEndPos; var skipAfterUsing = skipWhiteSpace.exec(this.input); - if (skipAfterUsing && lineBreak.test(this.input.slice(awaitEndPos, awaitEndPos + skipAfterUsing[0].length))) { return false } - } - - if (isFor) { - var ofEndPos = next + 2 /* of */, after$1; - if (this.input.slice(next, ofEndPos) === "of") { - if (ofEndPos === this.input.length || - (!isIdentifierChar(after$1 = this.input.charCodeAt(ofEndPos)) && !(after$1 > 0xd7ff && after$1 < 0xdc00))) { return false } - } - } - - var ch = this.input.charCodeAt(next); - return isIdentifierStart(ch, true) || ch === 92 // '\' + next = usingEndPos + skipAfterUsing[0].length; + if (skipAfterUsing && lineBreak.test(this.input.slice(usingEndPos, next))) { return false } + } + + var ch = this.fullCharCodeAt(next); + if (!isIdentifierStart(ch) && ch !== 92 /* '\' */) { return false } + var idStart = next; + do { next += ch <= 0xffff ? 1 : 2; } + while (isIdentifierChar(ch = this.fullCharCodeAt(next))) + if (ch === 92) { return true } + var id = this.input.slice(idStart, next); + if (keywordRelationalOperator.test(id) || isFor && id === "of") { return false } + return true }; pp$8.isAwaitUsing = function(isFor) { @@ -1008,8 +1032,8 @@ var usingKind = this.isAwaitUsing(false) ? "await using" : this.isUsing(false) ? "using" : null; if (usingKind) { - if (topLevel && this.options.sourceType === "script") { - this.raise(this.start, "Using declaration cannot appear in the top level when source type is `script`"); + if (!this.allowUsing) { + this.raise(this.start, "Using declaration cannot appear in the top level when source type is `script` or in the bare case statement"); } if (usingKind === "await using") { if (!this.canAwait) { @@ -1106,7 +1130,12 @@ if (usingKind) { var init$2 = this.startNode(); this.next(); - if (usingKind === "await using") { this.next(); } + if (usingKind === "await using") { + if (!this.canAwait) { + this.raise(this.start, "Await using cannot appear outside of async function"); + } + this.next(); + } this.parseVar(init$2, true, usingKind); this.finishNode(init$2, "VariableDeclaration"); return this.parseForAfterInit(node, init$2, awaitAt) @@ -1165,7 +1194,7 @@ }; pp$8.parseReturnStatement = function(node) { - if (!this.inFunction && !this.options.allowReturnOutsideFunction) + if (!this.allowReturn) { this.raise(this.start, "'return' outside of function"); } this.next(); @@ -1184,7 +1213,7 @@ node.cases = []; this.expect(types$1.braceL); this.labels.push(switchLabel); - this.enterScope(0); + this.enterScope(SCOPE_SWITCH); // Statements under must be grouped (by label) in SwitchCase // nodes. `cur` is used to keep the node that we are currently @@ -3892,7 +3921,7 @@ }; // This file was generated by "bin/generate-unicode-script-values.js". Do not modify manually! - var scriptValuesAddedInUnicode = "Gara Garay Gukh Gurung_Khema Hrkt Katakana_Or_Hiragana Kawi Kirat_Rai Krai Nag_Mundari Nagm Ol_Onal Onao Sunu Sunuwar Todhri Todr Tulu_Tigalari Tutg Unknown Zzzz"; + var scriptValuesAddedInUnicode = "Berf Beria_Erfe Gara Garay Gukh Gurung_Khema Hrkt Katakana_Or_Hiragana Kawi Kirat_Rai Krai Nag_Mundari Nagm Ol_Onal Onao Sidetic Sidt Sunu Sunuwar Tai_Yo Tayo Todhri Todr Tolong_Siki Tols Tulu_Tigalari Tutg Unknown Zzzz"; // This file contains Unicode properties extracted from the ECMAScript specification. // The lists are extracted like so: @@ -5453,13 +5482,17 @@ return this.getTokenFromCode(code) }; - pp.fullCharCodeAtPos = function() { - var code = this.input.charCodeAt(this.pos); + pp.fullCharCodeAt = function(pos) { + var code = this.input.charCodeAt(pos); if (code <= 0xd7ff || code >= 0xdc00) { return code } - var next = this.input.charCodeAt(this.pos + 1); + var next = this.input.charCodeAt(pos + 1); return next <= 0xdbff || next >= 0xe000 ? code : (code << 10) + next - 0x35fdc00 }; + pp.fullCharCodeAtPos = function() { + return this.fullCharCodeAt(this.pos) + }; + pp.skipBlockComment = function() { var startLoc = this.options.onComment && this.curPosition(); var start = this.pos, end = this.input.indexOf("*/", this.pos += 2); @@ -6187,7 +6220,7 @@ // [ghbt]: https://github.com/acornjs/acorn/issues - var version = "8.15.0"; + var version = "8.16.0"; Parser.acorn = { Parser: Parser, diff --git a/node_modules/acorn/dist/acorn.mjs b/node_modules/acorn/dist/acorn.mjs index 74d5fc431..9067e9bf9 100644 --- a/node_modules/acorn/dist/acorn.mjs +++ b/node_modules/acorn/dist/acorn.mjs @@ -1,14 +1,14 @@ // This file was generated. Do not modify manually! -var astralIdentifierCodes = [509, 0, 227, 0, 150, 4, 294, 9, 1368, 2, 2, 1, 6, 3, 41, 2, 5, 0, 166, 1, 574, 3, 9, 9, 7, 9, 32, 4, 318, 1, 80, 3, 71, 10, 50, 3, 123, 2, 54, 14, 32, 10, 3, 1, 11, 3, 46, 10, 8, 0, 46, 9, 7, 2, 37, 13, 2, 9, 6, 1, 45, 0, 13, 2, 49, 13, 9, 3, 2, 11, 83, 11, 7, 0, 3, 0, 158, 11, 6, 9, 7, 3, 56, 1, 2, 6, 3, 1, 3, 2, 10, 0, 11, 1, 3, 6, 4, 4, 68, 8, 2, 0, 3, 0, 2, 3, 2, 4, 2, 0, 15, 1, 83, 17, 10, 9, 5, 0, 82, 19, 13, 9, 214, 6, 3, 8, 28, 1, 83, 16, 16, 9, 82, 12, 9, 9, 7, 19, 58, 14, 5, 9, 243, 14, 166, 9, 71, 5, 2, 1, 3, 3, 2, 0, 2, 1, 13, 9, 120, 6, 3, 6, 4, 0, 29, 9, 41, 6, 2, 3, 9, 0, 10, 10, 47, 15, 343, 9, 54, 7, 2, 7, 17, 9, 57, 21, 2, 13, 123, 5, 4, 0, 2, 1, 2, 6, 2, 0, 9, 9, 49, 4, 2, 1, 2, 4, 9, 9, 330, 3, 10, 1, 2, 0, 49, 6, 4, 4, 14, 10, 5350, 0, 7, 14, 11465, 27, 2343, 9, 87, 9, 39, 4, 60, 6, 26, 9, 535, 9, 470, 0, 2, 54, 8, 3, 82, 0, 12, 1, 19628, 1, 4178, 9, 519, 45, 3, 22, 543, 4, 4, 5, 9, 7, 3, 6, 31, 3, 149, 2, 1418, 49, 513, 54, 5, 49, 9, 0, 15, 0, 23, 4, 2, 14, 1361, 6, 2, 16, 3, 6, 2, 1, 2, 4, 101, 0, 161, 6, 10, 9, 357, 0, 62, 13, 499, 13, 245, 1, 2, 9, 726, 6, 110, 6, 6, 9, 4759, 9, 787719, 239]; +var astralIdentifierCodes = [509, 0, 227, 0, 150, 4, 294, 9, 1368, 2, 2, 1, 6, 3, 41, 2, 5, 0, 166, 1, 574, 3, 9, 9, 7, 9, 32, 4, 318, 1, 78, 5, 71, 10, 50, 3, 123, 2, 54, 14, 32, 10, 3, 1, 11, 3, 46, 10, 8, 0, 46, 9, 7, 2, 37, 13, 2, 9, 6, 1, 45, 0, 13, 2, 49, 13, 9, 3, 2, 11, 83, 11, 7, 0, 3, 0, 158, 11, 6, 9, 7, 3, 56, 1, 2, 6, 3, 1, 3, 2, 10, 0, 11, 1, 3, 6, 4, 4, 68, 8, 2, 0, 3, 0, 2, 3, 2, 4, 2, 0, 15, 1, 83, 17, 10, 9, 5, 0, 82, 19, 13, 9, 214, 6, 3, 8, 28, 1, 83, 16, 16, 9, 82, 12, 9, 9, 7, 19, 58, 14, 5, 9, 243, 14, 166, 9, 71, 5, 2, 1, 3, 3, 2, 0, 2, 1, 13, 9, 120, 6, 3, 6, 4, 0, 29, 9, 41, 6, 2, 3, 9, 0, 10, 10, 47, 15, 199, 7, 137, 9, 54, 7, 2, 7, 17, 9, 57, 21, 2, 13, 123, 5, 4, 0, 2, 1, 2, 6, 2, 0, 9, 9, 49, 4, 2, 1, 2, 4, 9, 9, 55, 9, 266, 3, 10, 1, 2, 0, 49, 6, 4, 4, 14, 10, 5350, 0, 7, 14, 11465, 27, 2343, 9, 87, 9, 39, 4, 60, 6, 26, 9, 535, 9, 470, 0, 2, 54, 8, 3, 82, 0, 12, 1, 19628, 1, 4178, 9, 519, 45, 3, 22, 543, 4, 4, 5, 9, 7, 3, 6, 31, 3, 149, 2, 1418, 49, 513, 54, 5, 49, 9, 0, 15, 0, 23, 4, 2, 14, 1361, 6, 2, 16, 3, 6, 2, 1, 2, 4, 101, 0, 161, 6, 10, 9, 357, 0, 62, 13, 499, 13, 245, 1, 2, 9, 233, 0, 3, 0, 8, 1, 6, 0, 475, 6, 110, 6, 6, 9, 4759, 9, 787719, 239]; // This file was generated. Do not modify manually! -var astralIdentifierStartCodes = [0, 11, 2, 25, 2, 18, 2, 1, 2, 14, 3, 13, 35, 122, 70, 52, 268, 28, 4, 48, 48, 31, 14, 29, 6, 37, 11, 29, 3, 35, 5, 7, 2, 4, 43, 157, 19, 35, 5, 35, 5, 39, 9, 51, 13, 10, 2, 14, 2, 6, 2, 1, 2, 10, 2, 14, 2, 6, 2, 1, 4, 51, 13, 310, 10, 21, 11, 7, 25, 5, 2, 41, 2, 8, 70, 5, 3, 0, 2, 43, 2, 1, 4, 0, 3, 22, 11, 22, 10, 30, 66, 18, 2, 1, 11, 21, 11, 25, 71, 55, 7, 1, 65, 0, 16, 3, 2, 2, 2, 28, 43, 28, 4, 28, 36, 7, 2, 27, 28, 53, 11, 21, 11, 18, 14, 17, 111, 72, 56, 50, 14, 50, 14, 35, 39, 27, 10, 22, 251, 41, 7, 1, 17, 2, 60, 28, 11, 0, 9, 21, 43, 17, 47, 20, 28, 22, 13, 52, 58, 1, 3, 0, 14, 44, 33, 24, 27, 35, 30, 0, 3, 0, 9, 34, 4, 0, 13, 47, 15, 3, 22, 0, 2, 0, 36, 17, 2, 24, 20, 1, 64, 6, 2, 0, 2, 3, 2, 14, 2, 9, 8, 46, 39, 7, 3, 1, 3, 21, 2, 6, 2, 1, 2, 4, 4, 0, 19, 0, 13, 4, 31, 9, 2, 0, 3, 0, 2, 37, 2, 0, 26, 0, 2, 0, 45, 52, 19, 3, 21, 2, 31, 47, 21, 1, 2, 0, 185, 46, 42, 3, 37, 47, 21, 0, 60, 42, 14, 0, 72, 26, 38, 6, 186, 43, 117, 63, 32, 7, 3, 0, 3, 7, 2, 1, 2, 23, 16, 0, 2, 0, 95, 7, 3, 38, 17, 0, 2, 0, 29, 0, 11, 39, 8, 0, 22, 0, 12, 45, 20, 0, 19, 72, 200, 32, 32, 8, 2, 36, 18, 0, 50, 29, 113, 6, 2, 1, 2, 37, 22, 0, 26, 5, 2, 1, 2, 31, 15, 0, 328, 18, 16, 0, 2, 12, 2, 33, 125, 0, 80, 921, 103, 110, 18, 195, 2637, 96, 16, 1071, 18, 5, 26, 3994, 6, 582, 6842, 29, 1763, 568, 8, 30, 18, 78, 18, 29, 19, 47, 17, 3, 32, 20, 6, 18, 433, 44, 212, 63, 129, 74, 6, 0, 67, 12, 65, 1, 2, 0, 29, 6135, 9, 1237, 42, 9, 8936, 3, 2, 6, 2, 1, 2, 290, 16, 0, 30, 2, 3, 0, 15, 3, 9, 395, 2309, 106, 6, 12, 4, 8, 8, 9, 5991, 84, 2, 70, 2, 1, 3, 0, 3, 1, 3, 3, 2, 11, 2, 0, 2, 6, 2, 64, 2, 3, 3, 7, 2, 6, 2, 27, 2, 3, 2, 4, 2, 0, 4, 6, 2, 339, 3, 24, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 7, 1845, 30, 7, 5, 262, 61, 147, 44, 11, 6, 17, 0, 322, 29, 19, 43, 485, 27, 229, 29, 3, 0, 496, 6, 2, 3, 2, 1, 2, 14, 2, 196, 60, 67, 8, 0, 1205, 3, 2, 26, 2, 1, 2, 0, 3, 0, 2, 9, 2, 3, 2, 0, 2, 0, 7, 0, 5, 0, 2, 0, 2, 0, 2, 2, 2, 1, 2, 0, 3, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 1, 2, 0, 3, 3, 2, 6, 2, 3, 2, 3, 2, 0, 2, 9, 2, 16, 6, 2, 2, 4, 2, 16, 4421, 42719, 33, 4153, 7, 221, 3, 5761, 15, 7472, 16, 621, 2467, 541, 1507, 4938, 6, 4191]; +var astralIdentifierStartCodes = [0, 11, 2, 25, 2, 18, 2, 1, 2, 14, 3, 13, 35, 122, 70, 52, 268, 28, 4, 48, 48, 31, 14, 29, 6, 37, 11, 29, 3, 35, 5, 7, 2, 4, 43, 157, 19, 35, 5, 35, 5, 39, 9, 51, 13, 10, 2, 14, 2, 6, 2, 1, 2, 10, 2, 14, 2, 6, 2, 1, 4, 51, 13, 310, 10, 21, 11, 7, 25, 5, 2, 41, 2, 8, 70, 5, 3, 0, 2, 43, 2, 1, 4, 0, 3, 22, 11, 22, 10, 30, 66, 18, 2, 1, 11, 21, 11, 25, 7, 25, 39, 55, 7, 1, 65, 0, 16, 3, 2, 2, 2, 28, 43, 28, 4, 28, 36, 7, 2, 27, 28, 53, 11, 21, 11, 18, 14, 17, 111, 72, 56, 50, 14, 50, 14, 35, 39, 27, 10, 22, 251, 41, 7, 1, 17, 5, 57, 28, 11, 0, 9, 21, 43, 17, 47, 20, 28, 22, 13, 52, 58, 1, 3, 0, 14, 44, 33, 24, 27, 35, 30, 0, 3, 0, 9, 34, 4, 0, 13, 47, 15, 3, 22, 0, 2, 0, 36, 17, 2, 24, 20, 1, 64, 6, 2, 0, 2, 3, 2, 14, 2, 9, 8, 46, 39, 7, 3, 1, 3, 21, 2, 6, 2, 1, 2, 4, 4, 0, 19, 0, 13, 4, 31, 9, 2, 0, 3, 0, 2, 37, 2, 0, 26, 0, 2, 0, 45, 52, 19, 3, 21, 2, 31, 47, 21, 1, 2, 0, 185, 46, 42, 3, 37, 47, 21, 0, 60, 42, 14, 0, 72, 26, 38, 6, 186, 43, 117, 63, 32, 7, 3, 0, 3, 7, 2, 1, 2, 23, 16, 0, 2, 0, 95, 7, 3, 38, 17, 0, 2, 0, 29, 0, 11, 39, 8, 0, 22, 0, 12, 45, 20, 0, 19, 72, 200, 32, 32, 8, 2, 36, 18, 0, 50, 29, 113, 6, 2, 1, 2, 37, 22, 0, 26, 5, 2, 1, 2, 31, 15, 0, 24, 43, 261, 18, 16, 0, 2, 12, 2, 33, 125, 0, 80, 921, 103, 110, 18, 195, 2637, 96, 16, 1071, 18, 5, 26, 3994, 6, 582, 6842, 29, 1763, 568, 8, 30, 18, 78, 18, 29, 19, 47, 17, 3, 32, 20, 6, 18, 433, 44, 212, 63, 33, 24, 3, 24, 45, 74, 6, 0, 67, 12, 65, 1, 2, 0, 15, 4, 10, 7381, 42, 31, 98, 114, 8702, 3, 2, 6, 2, 1, 2, 290, 16, 0, 30, 2, 3, 0, 15, 3, 9, 395, 2309, 106, 6, 12, 4, 8, 8, 9, 5991, 84, 2, 70, 2, 1, 3, 0, 3, 1, 3, 3, 2, 11, 2, 0, 2, 6, 2, 64, 2, 3, 3, 7, 2, 6, 2, 27, 2, 3, 2, 4, 2, 0, 4, 6, 2, 339, 3, 24, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 7, 1845, 30, 7, 5, 262, 61, 147, 44, 11, 6, 17, 0, 322, 29, 19, 43, 485, 27, 229, 29, 3, 0, 208, 30, 2, 2, 2, 1, 2, 6, 3, 4, 10, 1, 225, 6, 2, 3, 2, 1, 2, 14, 2, 196, 60, 67, 8, 0, 1205, 3, 2, 26, 2, 1, 2, 0, 3, 0, 2, 9, 2, 3, 2, 0, 2, 0, 7, 0, 5, 0, 2, 0, 2, 0, 2, 2, 2, 1, 2, 0, 3, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 1, 2, 0, 3, 3, 2, 6, 2, 3, 2, 3, 2, 0, 2, 9, 2, 16, 6, 2, 2, 4, 2, 16, 4421, 42719, 33, 4381, 3, 5773, 3, 7472, 16, 621, 2467, 541, 1507, 4938, 6, 8489]; // This file was generated. Do not modify manually! -var nonASCIIidentifierChars = "\u200c\u200d\xb7\u0300-\u036f\u0387\u0483-\u0487\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u064b-\u0669\u0670\u06d6-\u06dc\u06df-\u06e4\u06e7\u06e8\u06ea-\u06ed\u06f0-\u06f9\u0711\u0730-\u074a\u07a6-\u07b0\u07c0-\u07c9\u07eb-\u07f3\u07fd\u0816-\u0819\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0859-\u085b\u0897-\u089f\u08ca-\u08e1\u08e3-\u0903\u093a-\u093c\u093e-\u094f\u0951-\u0957\u0962\u0963\u0966-\u096f\u0981-\u0983\u09bc\u09be-\u09c4\u09c7\u09c8\u09cb-\u09cd\u09d7\u09e2\u09e3\u09e6-\u09ef\u09fe\u0a01-\u0a03\u0a3c\u0a3e-\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a66-\u0a71\u0a75\u0a81-\u0a83\u0abc\u0abe-\u0ac5\u0ac7-\u0ac9\u0acb-\u0acd\u0ae2\u0ae3\u0ae6-\u0aef\u0afa-\u0aff\u0b01-\u0b03\u0b3c\u0b3e-\u0b44\u0b47\u0b48\u0b4b-\u0b4d\u0b55-\u0b57\u0b62\u0b63\u0b66-\u0b6f\u0b82\u0bbe-\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcd\u0bd7\u0be6-\u0bef\u0c00-\u0c04\u0c3c\u0c3e-\u0c44\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62\u0c63\u0c66-\u0c6f\u0c81-\u0c83\u0cbc\u0cbe-\u0cc4\u0cc6-\u0cc8\u0cca-\u0ccd\u0cd5\u0cd6\u0ce2\u0ce3\u0ce6-\u0cef\u0cf3\u0d00-\u0d03\u0d3b\u0d3c\u0d3e-\u0d44\u0d46-\u0d48\u0d4a-\u0d4d\u0d57\u0d62\u0d63\u0d66-\u0d6f\u0d81-\u0d83\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0de6-\u0def\u0df2\u0df3\u0e31\u0e34-\u0e3a\u0e47-\u0e4e\u0e50-\u0e59\u0eb1\u0eb4-\u0ebc\u0ec8-\u0ece\u0ed0-\u0ed9\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f3e\u0f3f\u0f71-\u0f84\u0f86\u0f87\u0f8d-\u0f97\u0f99-\u0fbc\u0fc6\u102b-\u103e\u1040-\u1049\u1056-\u1059\u105e-\u1060\u1062-\u1064\u1067-\u106d\u1071-\u1074\u1082-\u108d\u108f-\u109d\u135d-\u135f\u1369-\u1371\u1712-\u1715\u1732-\u1734\u1752\u1753\u1772\u1773\u17b4-\u17d3\u17dd\u17e0-\u17e9\u180b-\u180d\u180f-\u1819\u18a9\u1920-\u192b\u1930-\u193b\u1946-\u194f\u19d0-\u19da\u1a17-\u1a1b\u1a55-\u1a5e\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1ab0-\u1abd\u1abf-\u1ace\u1b00-\u1b04\u1b34-\u1b44\u1b50-\u1b59\u1b6b-\u1b73\u1b80-\u1b82\u1ba1-\u1bad\u1bb0-\u1bb9\u1be6-\u1bf3\u1c24-\u1c37\u1c40-\u1c49\u1c50-\u1c59\u1cd0-\u1cd2\u1cd4-\u1ce8\u1ced\u1cf4\u1cf7-\u1cf9\u1dc0-\u1dff\u200c\u200d\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2cef-\u2cf1\u2d7f\u2de0-\u2dff\u302a-\u302f\u3099\u309a\u30fb\ua620-\ua629\ua66f\ua674-\ua67d\ua69e\ua69f\ua6f0\ua6f1\ua802\ua806\ua80b\ua823-\ua827\ua82c\ua880\ua881\ua8b4-\ua8c5\ua8d0-\ua8d9\ua8e0-\ua8f1\ua8ff-\ua909\ua926-\ua92d\ua947-\ua953\ua980-\ua983\ua9b3-\ua9c0\ua9d0-\ua9d9\ua9e5\ua9f0-\ua9f9\uaa29-\uaa36\uaa43\uaa4c\uaa4d\uaa50-\uaa59\uaa7b-\uaa7d\uaab0\uaab2-\uaab4\uaab7\uaab8\uaabe\uaabf\uaac1\uaaeb-\uaaef\uaaf5\uaaf6\uabe3-\uabea\uabec\uabed\uabf0-\uabf9\ufb1e\ufe00-\ufe0f\ufe20-\ufe2f\ufe33\ufe34\ufe4d-\ufe4f\uff10-\uff19\uff3f\uff65"; +var nonASCIIidentifierChars = "\u200c\u200d\xb7\u0300-\u036f\u0387\u0483-\u0487\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u064b-\u0669\u0670\u06d6-\u06dc\u06df-\u06e4\u06e7\u06e8\u06ea-\u06ed\u06f0-\u06f9\u0711\u0730-\u074a\u07a6-\u07b0\u07c0-\u07c9\u07eb-\u07f3\u07fd\u0816-\u0819\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0859-\u085b\u0897-\u089f\u08ca-\u08e1\u08e3-\u0903\u093a-\u093c\u093e-\u094f\u0951-\u0957\u0962\u0963\u0966-\u096f\u0981-\u0983\u09bc\u09be-\u09c4\u09c7\u09c8\u09cb-\u09cd\u09d7\u09e2\u09e3\u09e6-\u09ef\u09fe\u0a01-\u0a03\u0a3c\u0a3e-\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a66-\u0a71\u0a75\u0a81-\u0a83\u0abc\u0abe-\u0ac5\u0ac7-\u0ac9\u0acb-\u0acd\u0ae2\u0ae3\u0ae6-\u0aef\u0afa-\u0aff\u0b01-\u0b03\u0b3c\u0b3e-\u0b44\u0b47\u0b48\u0b4b-\u0b4d\u0b55-\u0b57\u0b62\u0b63\u0b66-\u0b6f\u0b82\u0bbe-\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcd\u0bd7\u0be6-\u0bef\u0c00-\u0c04\u0c3c\u0c3e-\u0c44\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62\u0c63\u0c66-\u0c6f\u0c81-\u0c83\u0cbc\u0cbe-\u0cc4\u0cc6-\u0cc8\u0cca-\u0ccd\u0cd5\u0cd6\u0ce2\u0ce3\u0ce6-\u0cef\u0cf3\u0d00-\u0d03\u0d3b\u0d3c\u0d3e-\u0d44\u0d46-\u0d48\u0d4a-\u0d4d\u0d57\u0d62\u0d63\u0d66-\u0d6f\u0d81-\u0d83\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0de6-\u0def\u0df2\u0df3\u0e31\u0e34-\u0e3a\u0e47-\u0e4e\u0e50-\u0e59\u0eb1\u0eb4-\u0ebc\u0ec8-\u0ece\u0ed0-\u0ed9\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f3e\u0f3f\u0f71-\u0f84\u0f86\u0f87\u0f8d-\u0f97\u0f99-\u0fbc\u0fc6\u102b-\u103e\u1040-\u1049\u1056-\u1059\u105e-\u1060\u1062-\u1064\u1067-\u106d\u1071-\u1074\u1082-\u108d\u108f-\u109d\u135d-\u135f\u1369-\u1371\u1712-\u1715\u1732-\u1734\u1752\u1753\u1772\u1773\u17b4-\u17d3\u17dd\u17e0-\u17e9\u180b-\u180d\u180f-\u1819\u18a9\u1920-\u192b\u1930-\u193b\u1946-\u194f\u19d0-\u19da\u1a17-\u1a1b\u1a55-\u1a5e\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1ab0-\u1abd\u1abf-\u1add\u1ae0-\u1aeb\u1b00-\u1b04\u1b34-\u1b44\u1b50-\u1b59\u1b6b-\u1b73\u1b80-\u1b82\u1ba1-\u1bad\u1bb0-\u1bb9\u1be6-\u1bf3\u1c24-\u1c37\u1c40-\u1c49\u1c50-\u1c59\u1cd0-\u1cd2\u1cd4-\u1ce8\u1ced\u1cf4\u1cf7-\u1cf9\u1dc0-\u1dff\u200c\u200d\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2cef-\u2cf1\u2d7f\u2de0-\u2dff\u302a-\u302f\u3099\u309a\u30fb\ua620-\ua629\ua66f\ua674-\ua67d\ua69e\ua69f\ua6f0\ua6f1\ua802\ua806\ua80b\ua823-\ua827\ua82c\ua880\ua881\ua8b4-\ua8c5\ua8d0-\ua8d9\ua8e0-\ua8f1\ua8ff-\ua909\ua926-\ua92d\ua947-\ua953\ua980-\ua983\ua9b3-\ua9c0\ua9d0-\ua9d9\ua9e5\ua9f0-\ua9f9\uaa29-\uaa36\uaa43\uaa4c\uaa4d\uaa50-\uaa59\uaa7b-\uaa7d\uaab0\uaab2-\uaab4\uaab7\uaab8\uaabe\uaabf\uaac1\uaaeb-\uaaef\uaaf5\uaaf6\uabe3-\uabea\uabec\uabed\uabf0-\uabf9\ufb1e\ufe00-\ufe0f\ufe20-\ufe2f\ufe33\ufe34\ufe4d-\ufe4f\uff10-\uff19\uff3f\uff65"; // This file was generated. Do not modify manually! -var nonASCIIidentifierStartChars = "\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376\u0377\u037a-\u037d\u037f\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u052f\u0531-\u0556\u0559\u0560-\u0588\u05d0-\u05ea\u05ef-\u05f2\u0620-\u064a\u066e\u066f\u0671-\u06d3\u06d5\u06e5\u06e6\u06ee\u06ef\u06fa-\u06fc\u06ff\u0710\u0712-\u072f\u074d-\u07a5\u07b1\u07ca-\u07ea\u07f4\u07f5\u07fa\u0800-\u0815\u081a\u0824\u0828\u0840-\u0858\u0860-\u086a\u0870-\u0887\u0889-\u088e\u08a0-\u08c9\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0980\u0985-\u098c\u098f\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bd\u09ce\u09dc\u09dd\u09df-\u09e1\u09f0\u09f1\u09fc\u0a05-\u0a0a\u0a0f\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32\u0a33\u0a35\u0a36\u0a38\u0a39\u0a59-\u0a5c\u0a5e\u0a72-\u0a74\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2\u0ab3\u0ab5-\u0ab9\u0abd\u0ad0\u0ae0\u0ae1\u0af9\u0b05-\u0b0c\u0b0f\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32\u0b33\u0b35-\u0b39\u0b3d\u0b5c\u0b5d\u0b5f-\u0b61\u0b71\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99\u0b9a\u0b9c\u0b9e\u0b9f\u0ba3\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bd0\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c39\u0c3d\u0c58-\u0c5a\u0c5d\u0c60\u0c61\u0c80\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cdd\u0cde\u0ce0\u0ce1\u0cf1\u0cf2\u0d04-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d\u0d4e\u0d54-\u0d56\u0d5f-\u0d61\u0d7a-\u0d7f\u0d85-\u0d96\u0d9a-\u0db1\u0db3-\u0dbb\u0dbd\u0dc0-\u0dc6\u0e01-\u0e30\u0e32\u0e33\u0e40-\u0e46\u0e81\u0e82\u0e84\u0e86-\u0e8a\u0e8c-\u0ea3\u0ea5\u0ea7-\u0eb0\u0eb2\u0eb3\u0ebd\u0ec0-\u0ec4\u0ec6\u0edc-\u0edf\u0f00\u0f40-\u0f47\u0f49-\u0f6c\u0f88-\u0f8c\u1000-\u102a\u103f\u1050-\u1055\u105a-\u105d\u1061\u1065\u1066\u106e-\u1070\u1075-\u1081\u108e\u10a0-\u10c5\u10c7\u10cd\u10d0-\u10fa\u10fc-\u1248\u124a-\u124d\u1250-\u1256\u1258\u125a-\u125d\u1260-\u1288\u128a-\u128d\u1290-\u12b0\u12b2-\u12b5\u12b8-\u12be\u12c0\u12c2-\u12c5\u12c8-\u12d6\u12d8-\u1310\u1312-\u1315\u1318-\u135a\u1380-\u138f\u13a0-\u13f5\u13f8-\u13fd\u1401-\u166c\u166f-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f8\u1700-\u1711\u171f-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1878\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191e\u1950-\u196d\u1970-\u1974\u1980-\u19ab\u19b0-\u19c9\u1a00-\u1a16\u1a20-\u1a54\u1aa7\u1b05-\u1b33\u1b45-\u1b4c\u1b83-\u1ba0\u1bae\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1c80-\u1c8a\u1c90-\u1cba\u1cbd-\u1cbf\u1ce9-\u1cec\u1cee-\u1cf3\u1cf5\u1cf6\u1cfa\u1d00-\u1dbf\u1e00-\u1f15\u1f18-\u1f1d\u1f20-\u1f45\u1f48-\u1f4d\u1f50-\u1f57\u1f59\u1f5b\u1f5d\u1f5f-\u1f7d\u1f80-\u1fb4\u1fb6-\u1fbc\u1fbe\u1fc2-\u1fc4\u1fc6-\u1fcc\u1fd0-\u1fd3\u1fd6-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ff4\u1ff6-\u1ffc\u2071\u207f\u2090-\u209c\u2102\u2107\u210a-\u2113\u2115\u2118-\u211d\u2124\u2126\u2128\u212a-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2ce4\u2ceb-\u2cee\u2cf2\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d80-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309b-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312f\u3131-\u318e\u31a0-\u31bf\u31f0-\u31ff\u3400-\u4dbf\u4e00-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua69d\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua7cd\ua7d0\ua7d1\ua7d3\ua7d5-\ua7dc\ua7f2-\ua801\ua803-\ua805\ua807-\ua80a\ua80c-\ua822\ua840-\ua873\ua882-\ua8b3\ua8f2-\ua8f7\ua8fb\ua8fd\ua8fe\ua90a-\ua925\ua930-\ua946\ua960-\ua97c\ua984-\ua9b2\ua9cf\ua9e0-\ua9e4\ua9e6-\ua9ef\ua9fa-\ua9fe\uaa00-\uaa28\uaa40-\uaa42\uaa44-\uaa4b\uaa60-\uaa76\uaa7a\uaa7e-\uaaaf\uaab1\uaab5\uaab6\uaab9-\uaabd\uaac0\uaac2\uaadb-\uaadd\uaae0-\uaaea\uaaf2-\uaaf4\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\uab30-\uab5a\uab5c-\uab69\uab70-\uabe2\uac00-\ud7a3\ud7b0-\ud7c6\ud7cb-\ud7fb\uf900-\ufa6d\ufa70-\ufad9\ufb00-\ufb06\ufb13-\ufb17\ufb1d\ufb1f-\ufb28\ufb2a-\ufb36\ufb38-\ufb3c\ufb3e\ufb40\ufb41\ufb43\ufb44\ufb46-\ufbb1\ufbd3-\ufd3d\ufd50-\ufd8f\ufd92-\ufdc7\ufdf0-\ufdfb\ufe70-\ufe74\ufe76-\ufefc\uff21-\uff3a\uff41-\uff5a\uff66-\uffbe\uffc2-\uffc7\uffca-\uffcf\uffd2-\uffd7\uffda-\uffdc"; +var nonASCIIidentifierStartChars = "\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376\u0377\u037a-\u037d\u037f\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u052f\u0531-\u0556\u0559\u0560-\u0588\u05d0-\u05ea\u05ef-\u05f2\u0620-\u064a\u066e\u066f\u0671-\u06d3\u06d5\u06e5\u06e6\u06ee\u06ef\u06fa-\u06fc\u06ff\u0710\u0712-\u072f\u074d-\u07a5\u07b1\u07ca-\u07ea\u07f4\u07f5\u07fa\u0800-\u0815\u081a\u0824\u0828\u0840-\u0858\u0860-\u086a\u0870-\u0887\u0889-\u088f\u08a0-\u08c9\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0980\u0985-\u098c\u098f\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bd\u09ce\u09dc\u09dd\u09df-\u09e1\u09f0\u09f1\u09fc\u0a05-\u0a0a\u0a0f\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32\u0a33\u0a35\u0a36\u0a38\u0a39\u0a59-\u0a5c\u0a5e\u0a72-\u0a74\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2\u0ab3\u0ab5-\u0ab9\u0abd\u0ad0\u0ae0\u0ae1\u0af9\u0b05-\u0b0c\u0b0f\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32\u0b33\u0b35-\u0b39\u0b3d\u0b5c\u0b5d\u0b5f-\u0b61\u0b71\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99\u0b9a\u0b9c\u0b9e\u0b9f\u0ba3\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bd0\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c39\u0c3d\u0c58-\u0c5a\u0c5c\u0c5d\u0c60\u0c61\u0c80\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cdc-\u0cde\u0ce0\u0ce1\u0cf1\u0cf2\u0d04-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d\u0d4e\u0d54-\u0d56\u0d5f-\u0d61\u0d7a-\u0d7f\u0d85-\u0d96\u0d9a-\u0db1\u0db3-\u0dbb\u0dbd\u0dc0-\u0dc6\u0e01-\u0e30\u0e32\u0e33\u0e40-\u0e46\u0e81\u0e82\u0e84\u0e86-\u0e8a\u0e8c-\u0ea3\u0ea5\u0ea7-\u0eb0\u0eb2\u0eb3\u0ebd\u0ec0-\u0ec4\u0ec6\u0edc-\u0edf\u0f00\u0f40-\u0f47\u0f49-\u0f6c\u0f88-\u0f8c\u1000-\u102a\u103f\u1050-\u1055\u105a-\u105d\u1061\u1065\u1066\u106e-\u1070\u1075-\u1081\u108e\u10a0-\u10c5\u10c7\u10cd\u10d0-\u10fa\u10fc-\u1248\u124a-\u124d\u1250-\u1256\u1258\u125a-\u125d\u1260-\u1288\u128a-\u128d\u1290-\u12b0\u12b2-\u12b5\u12b8-\u12be\u12c0\u12c2-\u12c5\u12c8-\u12d6\u12d8-\u1310\u1312-\u1315\u1318-\u135a\u1380-\u138f\u13a0-\u13f5\u13f8-\u13fd\u1401-\u166c\u166f-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f8\u1700-\u1711\u171f-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1878\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191e\u1950-\u196d\u1970-\u1974\u1980-\u19ab\u19b0-\u19c9\u1a00-\u1a16\u1a20-\u1a54\u1aa7\u1b05-\u1b33\u1b45-\u1b4c\u1b83-\u1ba0\u1bae\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1c80-\u1c8a\u1c90-\u1cba\u1cbd-\u1cbf\u1ce9-\u1cec\u1cee-\u1cf3\u1cf5\u1cf6\u1cfa\u1d00-\u1dbf\u1e00-\u1f15\u1f18-\u1f1d\u1f20-\u1f45\u1f48-\u1f4d\u1f50-\u1f57\u1f59\u1f5b\u1f5d\u1f5f-\u1f7d\u1f80-\u1fb4\u1fb6-\u1fbc\u1fbe\u1fc2-\u1fc4\u1fc6-\u1fcc\u1fd0-\u1fd3\u1fd6-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ff4\u1ff6-\u1ffc\u2071\u207f\u2090-\u209c\u2102\u2107\u210a-\u2113\u2115\u2118-\u211d\u2124\u2126\u2128\u212a-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2ce4\u2ceb-\u2cee\u2cf2\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d80-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309b-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312f\u3131-\u318e\u31a0-\u31bf\u31f0-\u31ff\u3400-\u4dbf\u4e00-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua69d\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua7dc\ua7f1-\ua801\ua803-\ua805\ua807-\ua80a\ua80c-\ua822\ua840-\ua873\ua882-\ua8b3\ua8f2-\ua8f7\ua8fb\ua8fd\ua8fe\ua90a-\ua925\ua930-\ua946\ua960-\ua97c\ua984-\ua9b2\ua9cf\ua9e0-\ua9e4\ua9e6-\ua9ef\ua9fa-\ua9fe\uaa00-\uaa28\uaa40-\uaa42\uaa44-\uaa4b\uaa60-\uaa76\uaa7a\uaa7e-\uaaaf\uaab1\uaab5\uaab6\uaab9-\uaabd\uaac0\uaac2\uaadb-\uaadd\uaae0-\uaaea\uaaf2-\uaaf4\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\uab30-\uab5a\uab5c-\uab69\uab70-\uabe2\uac00-\ud7a3\ud7b0-\ud7c6\ud7cb-\ud7fb\uf900-\ufa6d\ufa70-\ufad9\ufb00-\ufb06\ufb13-\ufb17\ufb1d\ufb1f-\ufb28\ufb2a-\ufb36\ufb38-\ufb3c\ufb3e\ufb40\ufb41\ufb43\ufb44\ufb46-\ufbb1\ufbd3-\ufd3d\ufd50-\ufd8f\ufd92-\ufdc7\ufdf0-\ufdfb\ufe70-\ufe74\ufe76-\ufefc\uff21-\uff3a\uff41-\uff5a\uff66-\uffbe\uffc2-\uffc7\uffca-\uffcf\uffd2-\uffd7\uffda-\uffdc"; // These are a run-length and offset encoded representation of the // >0xffff code points that are a valid part of identifiers. The @@ -333,7 +333,7 @@ var defaultOptions = { // for new syntax features. ecmaVersion: null, // `sourceType` indicates the mode the code should be parsed in. - // Can be either `"script"` or `"module"`. This influences global + // Can be either `"script"`, `"module"` or `"commonjs"`. This influences global // strict mode and parsing of `import` and `export` declarations. sourceType: "script", // `onInsertedSemicolon` can be a callback that will be called when @@ -457,6 +457,9 @@ function getOptions(opts) { if (isArray(options.onComment)) { options.onComment = pushComment(options, options.onComment); } + if (options.sourceType === "commonjs" && options.allowAwaitOutsideFunction) + { throw new Error("Cannot use allowAwaitOutsideFunction with sourceType: commonjs") } + return options } @@ -488,6 +491,7 @@ var SCOPE_DIRECT_SUPER = 128, SCOPE_CLASS_STATIC_BLOCK = 256, SCOPE_CLASS_FIELD_INIT = 512, + SCOPE_SWITCH = 1024, SCOPE_VAR = SCOPE_TOP | SCOPE_FUNCTION | SCOPE_CLASS_STATIC_BLOCK; function functionFlags(async, generator) { @@ -577,7 +581,12 @@ var Parser = function Parser(options, input, startPos) { // Scope tracking for duplicate variable names (see scope.js) this.scopeStack = []; - this.enterScope(SCOPE_TOP); + this.enterScope( + this.options.sourceType === "commonjs" + // In commonjs, the top-level scope behaves like a function scope + ? SCOPE_FUNCTION + : SCOPE_TOP + ); // For RegExp validation this.regexpState = null; @@ -588,7 +597,7 @@ var Parser = function Parser(options, input, startPos) { this.privateNameStack = []; }; -var prototypeAccessors = { inFunction: { configurable: true },inGenerator: { configurable: true },inAsync: { configurable: true },canAwait: { configurable: true },allowSuper: { configurable: true },allowDirectSuper: { configurable: true },treatFunctionsAsVar: { configurable: true },allowNewDotTarget: { configurable: true },inClassStaticBlock: { configurable: true } }; +var prototypeAccessors = { inFunction: { configurable: true },inGenerator: { configurable: true },inAsync: { configurable: true },canAwait: { configurable: true },allowReturn: { configurable: true },allowSuper: { configurable: true },allowDirectSuper: { configurable: true },treatFunctionsAsVar: { configurable: true },allowNewDotTarget: { configurable: true },allowUsing: { configurable: true },inClassStaticBlock: { configurable: true } }; Parser.prototype.parse = function parse () { var node = this.options.program || this.startNode(); @@ -612,6 +621,12 @@ prototypeAccessors.canAwait.get = function () { return (this.inModule && this.options.ecmaVersion >= 13) || this.options.allowAwaitOutsideFunction }; +prototypeAccessors.allowReturn.get = function () { + if (this.inFunction) { return true } + if (this.options.allowReturnOutsideFunction && this.currentVarScope().flags & SCOPE_TOP) { return true } + return false +}; + prototypeAccessors.allowSuper.get = function () { var ref = this.currentThisScope(); var flags = ref.flags; @@ -632,6 +647,14 @@ prototypeAccessors.allowNewDotTarget.get = function () { return false }; +prototypeAccessors.allowUsing.get = function () { + var ref = this.currentScope(); + var flags = ref.flags; + if (flags & SCOPE_SWITCH) { return false } + if (!this.inModule && flags & SCOPE_TOP) { return false } + return true +}; + prototypeAccessors.inClassStaticBlock.get = function () { return (this.currentVarScope().flags & SCOPE_CLASS_STATIC_BLOCK) > 0 }; @@ -836,7 +859,7 @@ pp$8.parseTopLevel = function(node) { } } this.adaptDirectivePrologue(node.body); this.next(); - node.sourceType = this.options.sourceType; + node.sourceType = this.options.sourceType === "commonjs" ? "script" : this.options.sourceType; return this.finishNode(node, "Program") }; @@ -846,7 +869,7 @@ pp$8.isLet = function(context) { if (this.options.ecmaVersion < 6 || !this.isContextual("let")) { return false } skipWhiteSpace.lastIndex = this.pos; var skip = skipWhiteSpace.exec(this.input); - var next = this.pos + skip[0].length, nextCh = this.input.charCodeAt(next); + var next = this.pos + skip[0].length, nextCh = this.fullCharCodeAt(next); // For ambiguous cases, determine if a LexicalDeclaration (or only a // Statement) is allowed here. If context is not empty then only a Statement // is allowed. However, `let [` is an explicit negative lookahead for @@ -854,12 +877,13 @@ pp$8.isLet = function(context) { if (nextCh === 91 || nextCh === 92) { return true } // '[', '\' if (context) { return false } - if (nextCh === 123 || nextCh > 0xd7ff && nextCh < 0xdc00) { return true } // '{', astral - if (isIdentifierStart(nextCh, true)) { - var pos = next + 1; - while (isIdentifierChar(nextCh = this.input.charCodeAt(pos), true)) { ++pos; } - if (nextCh === 92 || nextCh > 0xd7ff && nextCh < 0xdc00) { return true } - var ident = this.input.slice(next, pos); + if (nextCh === 123) { return true } // '{' + if (isIdentifierStart(nextCh)) { + var start = next; + do { next += nextCh <= 0xffff ? 1 : 2; } + while (isIdentifierChar(nextCh = this.fullCharCodeAt(next))) + if (nextCh === 92) { return true } + var ident = this.input.slice(start, next); if (!keywordRelationalOperator.test(ident)) { return true } } return false @@ -878,7 +902,7 @@ pp$8.isAsyncFunction = function() { return !lineBreak.test(this.input.slice(this.pos, next)) && this.input.slice(next, next + 8) === "function" && (next + 8 === this.input.length || - !(isIdentifierChar(after = this.input.charCodeAt(next + 8)) || after > 0xd7ff && after < 0xdc00)) + !(isIdentifierChar(after = this.fullCharCodeAt(next + 8)) || after === 92 /* '\' */)) }; pp$8.isUsingKeyword = function(isAwaitUsing, isFor) { @@ -892,28 +916,28 @@ pp$8.isUsingKeyword = function(isAwaitUsing, isFor) { if (lineBreak.test(this.input.slice(this.pos, next))) { return false } if (isAwaitUsing) { - var awaitEndPos = next + 5 /* await */, after; - if (this.input.slice(next, awaitEndPos) !== "using" || - awaitEndPos === this.input.length || - isIdentifierChar(after = this.input.charCodeAt(awaitEndPos)) || - (after > 0xd7ff && after < 0xdc00) + var usingEndPos = next + 5 /* using */, after; + if (this.input.slice(next, usingEndPos) !== "using" || + usingEndPos === this.input.length || + isIdentifierChar(after = this.fullCharCodeAt(usingEndPos)) || + after === 92 /* '\' */ ) { return false } - skipWhiteSpace.lastIndex = awaitEndPos; + skipWhiteSpace.lastIndex = usingEndPos; var skipAfterUsing = skipWhiteSpace.exec(this.input); - if (skipAfterUsing && lineBreak.test(this.input.slice(awaitEndPos, awaitEndPos + skipAfterUsing[0].length))) { return false } - } - - if (isFor) { - var ofEndPos = next + 2 /* of */, after$1; - if (this.input.slice(next, ofEndPos) === "of") { - if (ofEndPos === this.input.length || - (!isIdentifierChar(after$1 = this.input.charCodeAt(ofEndPos)) && !(after$1 > 0xd7ff && after$1 < 0xdc00))) { return false } - } - } - - var ch = this.input.charCodeAt(next); - return isIdentifierStart(ch, true) || ch === 92 // '\' + next = usingEndPos + skipAfterUsing[0].length; + if (skipAfterUsing && lineBreak.test(this.input.slice(usingEndPos, next))) { return false } + } + + var ch = this.fullCharCodeAt(next); + if (!isIdentifierStart(ch) && ch !== 92 /* '\' */) { return false } + var idStart = next; + do { next += ch <= 0xffff ? 1 : 2; } + while (isIdentifierChar(ch = this.fullCharCodeAt(next))) + if (ch === 92) { return true } + var id = this.input.slice(idStart, next); + if (keywordRelationalOperator.test(id) || isFor && id === "of") { return false } + return true }; pp$8.isAwaitUsing = function(isFor) { @@ -1002,8 +1026,8 @@ pp$8.parseStatement = function(context, topLevel, exports) { var usingKind = this.isAwaitUsing(false) ? "await using" : this.isUsing(false) ? "using" : null; if (usingKind) { - if (topLevel && this.options.sourceType === "script") { - this.raise(this.start, "Using declaration cannot appear in the top level when source type is `script`"); + if (!this.allowUsing) { + this.raise(this.start, "Using declaration cannot appear in the top level when source type is `script` or in the bare case statement"); } if (usingKind === "await using") { if (!this.canAwait) { @@ -1100,7 +1124,12 @@ pp$8.parseForStatement = function(node) { if (usingKind) { var init$2 = this.startNode(); this.next(); - if (usingKind === "await using") { this.next(); } + if (usingKind === "await using") { + if (!this.canAwait) { + this.raise(this.start, "Await using cannot appear outside of async function"); + } + this.next(); + } this.parseVar(init$2, true, usingKind); this.finishNode(init$2, "VariableDeclaration"); return this.parseForAfterInit(node, init$2, awaitAt) @@ -1159,7 +1188,7 @@ pp$8.parseIfStatement = function(node) { }; pp$8.parseReturnStatement = function(node) { - if (!this.inFunction && !this.options.allowReturnOutsideFunction) + if (!this.allowReturn) { this.raise(this.start, "'return' outside of function"); } this.next(); @@ -1178,7 +1207,7 @@ pp$8.parseSwitchStatement = function(node) { node.cases = []; this.expect(types$1.braceL); this.labels.push(switchLabel); - this.enterScope(0); + this.enterScope(SCOPE_SWITCH); // Statements under must be grouped (by label) in SwitchCase // nodes. `cur` is used to keep the node that we are currently @@ -3886,7 +3915,7 @@ pp$2.copyNode = function(node) { }; // This file was generated by "bin/generate-unicode-script-values.js". Do not modify manually! -var scriptValuesAddedInUnicode = "Gara Garay Gukh Gurung_Khema Hrkt Katakana_Or_Hiragana Kawi Kirat_Rai Krai Nag_Mundari Nagm Ol_Onal Onao Sunu Sunuwar Todhri Todr Tulu_Tigalari Tutg Unknown Zzzz"; +var scriptValuesAddedInUnicode = "Berf Beria_Erfe Gara Garay Gukh Gurung_Khema Hrkt Katakana_Or_Hiragana Kawi Kirat_Rai Krai Nag_Mundari Nagm Ol_Onal Onao Sidetic Sidt Sunu Sunuwar Tai_Yo Tayo Todhri Todr Tolong_Siki Tols Tulu_Tigalari Tutg Unknown Zzzz"; // This file contains Unicode properties extracted from the ECMAScript specification. // The lists are extracted like so: @@ -5447,13 +5476,17 @@ pp.readToken = function(code) { return this.getTokenFromCode(code) }; -pp.fullCharCodeAtPos = function() { - var code = this.input.charCodeAt(this.pos); +pp.fullCharCodeAt = function(pos) { + var code = this.input.charCodeAt(pos); if (code <= 0xd7ff || code >= 0xdc00) { return code } - var next = this.input.charCodeAt(this.pos + 1); + var next = this.input.charCodeAt(pos + 1); return next <= 0xdbff || next >= 0xe000 ? code : (code << 10) + next - 0x35fdc00 }; +pp.fullCharCodeAtPos = function() { + return this.fullCharCodeAt(this.pos) +}; + pp.skipBlockComment = function() { var startLoc = this.options.onComment && this.curPosition(); var start = this.pos, end = this.input.indexOf("*/", this.pos += 2); @@ -6181,7 +6214,7 @@ pp.readWord = function() { // [ghbt]: https://github.com/acornjs/acorn/issues -var version = "8.15.0"; +var version = "8.16.0"; Parser.acorn = { Parser: Parser, diff --git a/node_modules/acorn/package.json b/node_modules/acorn/package.json index 6f63ddbf6..7f4d1708b 100644 --- a/node_modules/acorn/package.json +++ b/node_modules/acorn/package.json @@ -16,7 +16,7 @@ ], "./package.json": "./package.json" }, - "version": "8.15.0", + "version": "8.16.0", "engines": { "node": ">=0.4.0" }, diff --git a/node_modules/eslint/README.md b/node_modules/eslint/README.md index 77d194010..227d40c7c 100644 --- a/node_modules/eslint/README.md +++ b/node_modules/eslint/README.md @@ -1,9 +1,11 @@ [![npm version](https://img.shields.io/npm/v/eslint.svg)](https://www.npmjs.com/package/eslint) [![Downloads](https://img.shields.io/npm/dm/eslint.svg)](https://www.npmjs.com/package/eslint) [![Build Status](https://github.com/eslint/eslint/workflows/CI/badge.svg)](https://github.com/eslint/eslint/actions) -
+[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bhttps%3A%2F%2Fgithub.com%2Feslint%2Feslint.svg?type=shield)](https://app.fossa.io/projects/git%2Bhttps%3A%2F%2Fgithub.com%2Feslint%2Feslint?ref=badge_shield) +
[![Open Collective Backers](https://img.shields.io/opencollective/backers/eslint)](https://opencollective.com/eslint) [![Open Collective Sponsors](https://img.shields.io/opencollective/sponsors/eslint)](https://opencollective.com/eslint) +[![Follow us on Twitter](https://img.shields.io/twitter/follow/geteslint?label=Follow&style=social)](https://twitter.com/intent/user?screen_name=geteslint) # ESLint @@ -13,120 +15,109 @@ [Contribute to ESLint](https://eslint.org/docs/latest/contribute) | [Report Bugs](https://eslint.org/docs/latest/contribute/report-bugs) | [Code of Conduct](https://eslint.org/conduct) | -[X](https://x.com/geteslint) | +[Twitter](https://twitter.com/geteslint) | [Discord](https://eslint.org/chat) | -[Mastodon](https://fosstodon.org/@eslint) | -[Bluesky](https://bsky.app/profile/eslint.org) +[Mastodon](https://fosstodon.org/@eslint) ESLint is a tool for identifying and reporting on patterns found in ECMAScript/JavaScript code. In many ways, it is similar to JSLint and JSHint with a few exceptions: -- ESLint uses [Espree](https://github.com/eslint/js/tree/main/packages/espree) for JavaScript parsing. -- ESLint uses an AST to evaluate patterns in code. -- ESLint is completely pluggable, every single rule is a plugin and you can add more at runtime. +* ESLint uses [Espree](https://github.com/eslint/espree) for JavaScript parsing. +* ESLint uses an AST to evaluate patterns in code. +* ESLint is completely pluggable, every single rule is a plugin and you can add more at runtime. ## Table of Contents 1. [Installation and Usage](#installation-and-usage) -1. [Configuration](#configuration) -1. [Version Support](#version-support) -1. [Code of Conduct](#code-of-conduct) -1. [Filing Issues](#filing-issues) -1. [Frequently Asked Questions](#frequently-asked-questions) -1. [Releases](#releases) -1. [Security Policy](#security-policy) -1. [Semantic Versioning Policy](#semantic-versioning-policy) -1. [License](#license) -1. [Team](#team) -1. [Sponsors](#sponsors) -1. [Technology Sponsors](#technology-sponsors) +2. [Configuration](#configuration) +3. [Code of Conduct](#code-of-conduct) +4. [Filing Issues](#filing-issues) +5. [Frequently Asked Questions](#frequently-asked-questions) +6. [Releases](#releases) +7. [Security Policy](#security-policy) +8. [Semantic Versioning Policy](#semantic-versioning-policy) +9. [Stylistic Rule Updates](#stylistic-rule-updates) +10. [License](#license) +11. [Team](#team) +12. [Sponsors](#sponsors) +13. [Technology Sponsors](#technology-sponsors) ## Installation and Usage -Prerequisites: [Node.js](https://nodejs.org/) (`^18.18.0`, `^20.9.0`, or `>=21.1.0`) built with SSL support. (If you are using an official Node.js distribution, SSL is always built in.) +Prerequisites: [Node.js](https://nodejs.org/) (`^12.22.0`, `^14.17.0`, or `>=16.0.0`) built with SSL support. (If you are using an official Node.js distribution, SSL is always built in.) You can install and configure ESLint using this command: ```shell -npm init @eslint/config@latest +npm init @eslint/config ``` After that, you can run ESLint on any file or directory like this: ```shell -npx eslint yourfile.js +./node_modules/.bin/eslint yourfile.js ``` -### pnpm Installation - -To use ESLint with pnpm, we recommend setting up a `.npmrc` file with at least the following settings: - -```text -auto-install-peers=true -node-linker=hoisted -``` - -This ensures that pnpm installs dependencies in a way that is more compatible with npm and is less likely to produce errors. - ## Configuration -You can configure rules in your `eslint.config.js` files as in this example: - -```js -import { defineConfig } from "eslint/config"; +After running `npm init @eslint/config`, you'll have an `.eslintrc` file in your directory. In it, you'll see some rules configured like this: -export default defineConfig([ - { - files: ["**/*.js", "**/*.cjs", "**/*.mjs"], - rules: { - "prefer-const": "warn", - "no-constant-binary-expression": "error", - }, - }, -]); +```json +{ + "rules": { + "semi": ["error", "always"], + "quotes": ["error", "double"] + } +} ``` -The names `"prefer-const"` and `"no-constant-binary-expression"` are the names of [rules](https://eslint.org/docs/rules) in ESLint. The first value is the error level of the rule and can be one of these values: +The names `"semi"` and `"quotes"` are the names of [rules](https://eslint.org/docs/rules) in ESLint. The first value is the error level of the rule and can be one of these values: -- `"off"` or `0` - turn the rule off -- `"warn"` or `1` - turn the rule on as a warning (doesn't affect exit code) -- `"error"` or `2` - turn the rule on as an error (exit code will be 1) +* `"off"` or `0` - turn the rule off +* `"warn"` or `1` - turn the rule on as a warning (doesn't affect exit code) +* `"error"` or `2` - turn the rule on as an error (exit code will be 1) The three error levels allow you fine-grained control over how ESLint applies rules (for more configuration options and details, see the [configuration docs](https://eslint.org/docs/latest/use/configure)). -## Version Support - -The ESLint team provides ongoing support for the current version and six months of limited support for the previous version. Limited support includes critical bug fixes, security issues, and compatibility issues only. - -ESLint offers commercial support for both current and previous versions through our partners, [Tidelift][tidelift] and [HeroDevs][herodevs]. - -See [Version Support](https://eslint.org/version-support) for more details. - ## Code of Conduct -ESLint adheres to the [OpenJS Foundation Code of Conduct](https://eslint.org/conduct). +ESLint adheres to the [JS Foundation Code of Conduct](https://eslint.org/conduct). ## Filing Issues Before filing an issue, please be sure to read the guidelines for what you're reporting: -- [Bug Report](https://eslint.org/docs/latest/contribute/report-bugs) -- [Propose a New Rule](https://eslint.org/docs/latest/contribute/propose-new-rule) -- [Proposing a Rule Change](https://eslint.org/docs/latest/contribute/propose-rule-change) -- [Request a Change](https://eslint.org/docs/latest/contribute/request-change) +* [Bug Report](https://eslint.org/docs/latest/contribute/report-bugs) +* [Propose a New Rule](https://eslint.org/docs/latest/contribute/propose-new-rule) +* [Proposing a Rule Change](https://eslint.org/docs/latest/contribute/propose-rule-change) +* [Request a Change](https://eslint.org/docs/latest/contribute/request-change) ## Frequently Asked Questions -### Does ESLint support JSX? +### I'm using JSCS, should I migrate to ESLint? + +Yes. [JSCS has reached end of life](https://eslint.org/blog/2016/07/jscs-end-of-life) and is no longer supported. -Yes, ESLint natively supports parsing JSX syntax (this must be enabled in [configuration](https://eslint.org/docs/latest/use/configure)). Please note that supporting JSX syntax _is not_ the same as supporting React. React applies specific semantics to JSX syntax that ESLint doesn't recognize. We recommend using [eslint-plugin-react](https://www.npmjs.com/package/eslint-plugin-react) if you are using React and want React semantics. +We have prepared a [migration guide](https://eslint.org/docs/latest/use/migrating-from-jscs) to help you convert your JSCS settings to an ESLint configuration. + +We are now at or near 100% compatibility with JSCS. If you try ESLint and believe we are not yet compatible with a JSCS rule/configuration, please create an issue (mentioning that it is a JSCS compatibility issue) and we will evaluate it as per our normal process. ### Does Prettier replace ESLint? -No, ESLint and Prettier have different jobs: ESLint is a linter (looking for problematic patterns) and Prettier is a code formatter. Using both tools is common, refer to [Prettier's documentation](https://prettier.io/docs/en/install#eslint-and-other-linters) to learn how to configure them to work well with each other. +No, ESLint and Prettier have diffent jobs: ESLint is a linter (looking for problematic patterns) and Prettier is a code formatter. Using both tools is common, refer to [Prettier's documentation](https://prettier.io/docs/en/install#eslint-and-other-linters) to learn how to configure them to work well with each other. + +### Why can't ESLint find my plugins? + +* Make sure your plugins (and ESLint) are both in your project's `package.json` as devDependencies (or dependencies, if your project uses ESLint at runtime). +* Make sure you have run `npm install` and all your dependencies are installed. +* Make sure your plugins' peerDependencies have been installed as well. You can use `npm view eslint-plugin-myplugin peerDependencies` to see what peer dependencies `eslint-plugin-myplugin` has. + +### Does ESLint support JSX? + +Yes, ESLint natively supports parsing JSX syntax (this must be enabled in [configuration](https://eslint.org/docs/latest/use/configure)). Please note that supporting JSX syntax *is not* the same as supporting React. React applies specific semantics to JSX syntax that ESLint doesn't recognize. We recommend using [eslint-plugin-react](https://www.npmjs.com/package/eslint-plugin-react) if you are using React and want React semantics. ### What ECMAScript versions does ESLint support? -ESLint has full support for ECMAScript 3, 5, and every year from 2015 up until the most recent stage 4 specification (the default). You can set your desired ECMAScript syntax and other settings (like global variables) through [configuration](https://eslint.org/docs/latest/use/configure). +ESLint has full support for ECMAScript 3, 5 (default), 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, and 2023. You can set your desired ECMAScript syntax (and other settings, like global variables or your target environments) through [configuration](https://eslint.org/docs/latest/use/configure). ### What about experimental features? @@ -136,18 +127,6 @@ In other cases (including if rules need to warn on more or fewer cases due to ne Once a language feature has been adopted into the ECMAScript standard (stage 4 according to the [TC39 process](https://tc39.github.io/process-document/)), we will accept issues and pull requests related to the new feature, subject to our [contributing guidelines](https://eslint.org/docs/latest/contribute). Until then, please use the appropriate parser and plugin(s) for your experimental feature. -### Which Node.js versions does ESLint support? - -ESLint updates the supported Node.js versions with each major release of ESLint. At that time, ESLint's supported Node.js versions are updated to be: - -1. The most recent maintenance release of Node.js -1. The lowest minor version of the Node.js LTS release that includes the features the ESLint team wants to use. -1. The Node.js Current release - -ESLint is also expected to work with Node.js versions released after the Node.js Current release. - -Refer to the [Quick Start Guide](https://eslint.org/docs/latest/use/getting-started#prerequisites) for the officially supported Node.js versions for a given ESLint release. - ### Where to ask for help? Open a [discussion](https://github.com/eslint/eslint/discussions) or stop by our [Discord server](https://eslint.org/chat). @@ -174,58 +153,47 @@ ESLint takes security seriously. We work hard to ensure that ESLint is safe for ESLint follows [semantic versioning](https://semver.org). However, due to the nature of ESLint as a code quality tool, it's not always clear when a minor or major version bump occurs. To help clarify this for everyone, we've defined the following semantic versioning policy for ESLint: -- Patch release (intended to not break your lint build) - - A bug fix in a rule that results in ESLint reporting fewer linting errors. - - A bug fix to the CLI or core (including formatters). - - Improvements to documentation. - - Non-user-facing changes such as refactoring code, adding, deleting, or modifying tests, and increasing test coverage. - - Re-releasing after a failed release (i.e., publishing a release that doesn't work for anyone). -- Minor release (might break your lint build) - - A bug fix in a rule that results in ESLint reporting more linting errors. - - A new rule is created. - - A new option to an existing rule that does not result in ESLint reporting more linting errors by default. - - A new addition to an existing rule to support a newly-added language feature (within the last 12 months) that will result in ESLint reporting more linting errors by default. - - An existing rule is deprecated. - - A new CLI capability is created. - - New capabilities to the public API are added (new classes, new methods, new arguments to existing methods, etc.). - - A new formatter is created. - - `eslint:recommended` is updated and will result in strictly fewer linting errors (e.g., rule removals). -- Major release (likely to break your lint build) - - `eslint:recommended` is updated and may result in new linting errors (e.g., rule additions, most rule option updates). - - A new option to an existing rule that results in ESLint reporting more linting errors by default. - - An existing formatter is removed. - - Part of the public API is removed or changed in an incompatible way. The public API includes: - - Rule schemas - - Configuration schema - - Command-line options - - Node.js API - - Rule, formatter, parser, plugin APIs +* Patch release (intended to not break your lint build) + * A bug fix in a rule that results in ESLint reporting fewer linting errors. + * A bug fix to the CLI or core (including formatters). + * Improvements to documentation. + * Non-user-facing changes such as refactoring code, adding, deleting, or modifying tests, and increasing test coverage. + * Re-releasing after a failed release (i.e., publishing a release that doesn't work for anyone). +* Minor release (might break your lint build) + * A bug fix in a rule that results in ESLint reporting more linting errors. + * A new rule is created. + * A new option to an existing rule that does not result in ESLint reporting more linting errors by default. + * A new addition to an existing rule to support a newly-added language feature (within the last 12 months) that will result in ESLint reporting more linting errors by default. + * An existing rule is deprecated. + * A new CLI capability is created. + * New capabilities to the public API are added (new classes, new methods, new arguments to existing methods, etc.). + * A new formatter is created. + * `eslint:recommended` is updated and will result in strictly fewer linting errors (e.g., rule removals). +* Major release (likely to break your lint build) + * `eslint:recommended` is updated and may result in new linting errors (e.g., rule additions, most rule option updates). + * A new option to an existing rule that results in ESLint reporting more linting errors by default. + * An existing formatter is removed. + * Part of the public API is removed or changed in an incompatible way. The public API includes: + * Rule schemas + * Configuration schema + * Command-line options + * Node.js API + * Rule, formatter, parser, plugin APIs According to our policy, any minor update may report more linting errors than the previous release (ex: from a bug fix). As such, we recommend using the tilde (`~`) in `package.json` e.g. `"eslint": "~3.1.0"` to guarantee the results of your builds. -## License - -MIT License +## Stylistic Rule Updates -Copyright OpenJS Foundation and other contributors, +Stylistic rules are frozen according to [our policy](https://eslint.org/blog/2020/05/changes-to-rules-policies) on how we evaluate new rules and rule changes. +This means: -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: +* **Bug fixes**: We will still fix bugs in stylistic rules. +* **New ECMAScript features**: We will also make sure stylistic rules are compatible with new ECMAScript features. +* **New options**: We will **not** add any new options to stylistic rules unless an option is the only way to fix a bug or support a newly-added ECMAScript feature. -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. +## License -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. +[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bhttps%3A%2F%2Fgithub.com%2Feslint%2Feslint.svg?type=large)](https://app.fossa.io/projects/git%2Bhttps%3A%2F%2Fgithub.com%2Feslint%2Feslint?ref=badge_large) ## Team @@ -245,11 +213,6 @@ The people who manage releases, review feature requests, and meet regularly to e Nicholas C. Zakas - -Francesco Trotta's Avatar
-Francesco Trotta -
- Milos Djermanovic's Avatar
Milos Djermanovic @@ -277,35 +240,25 @@ Nitin Kumar The people who review and fix bugs and help triage issues.
- -fnx's Avatar
-fnx +
+Bryan Mishkin's Avatar
+Bryan Mishkin
- -Josh Goldberg ✨'s Avatar
-Josh Goldberg ✨ +
+Francesco Trotta's Avatar
+Francesco Trotta
- -Sweta Tanwar's Avatar
-Sweta Tanwar +
+Yosuke Ota's Avatar
+Yosuke Ota
Tanuj Kanti's Avatar
Tanuj Kanti
-
- -루밀LuMir's Avatar
-루밀LuMir -
-
- -Pixel998's Avatar
-Pixel998 -
### Website Team @@ -319,8 +272,8 @@ Amaresh S M
-Harish's Avatar
-Harish +Strek's Avatar
+Strek
@@ -331,24 +284,21 @@ Percy Ma - - - ## Sponsors -The following companies, organizations, and individuals support ESLint's ongoing maintenance and development. [Become a Sponsor](https://eslint.org/donate) -to get your logo on our READMEs and [website](https://eslint.org/sponsors). +The following companies, organizations, and individuals support ESLint's ongoing maintenance and development. [Become a Sponsor](https://opencollective.com/eslint) to get your logo on our README and website. + +

Platinum Sponsors

-

Automattic Airbnb

Gold Sponsors

-

Qlty Software trunk.io Shopify

Silver Sponsors

-

Vite Liftoff American Express StackBlitz

Bronze Sponsors

-

Cybozu Icons8 Discord GitBook Nx Mercedes-Benz Group HeroCoders LambdaTest

-

Technology Sponsors

-Technology sponsors allow us to use their products and services for free as part of a contribution to the open source ecosystem and our work. -

Netlify Algolia 1Password

- +

Chrome Frameworks Fund Automattic

Gold Sponsors

+

Salesforce Airbnb

Silver Sponsors

+

Liftoff American Express Workleap

Bronze Sponsors

+

ThemeIsle Anagram Solver Icons8 Discord Transloadit Ignition Nx HeroCoders

-[tidelift]: https://tidelift.com/funding/github/npm/eslint -[herodevs]: https://www.herodevs.com/support/eslint-nes?utm_source=ESLintWebsite&utm_medium=ESLintWebsite&utm_campaign=ESLintNES&utm_id=ESLintNES +## Technology Sponsors + +* Site search ([eslint.org](https://eslint.org)) is sponsored by [Algolia](https://www.algolia.com) +* Hosting for ([eslint.org](https://eslint.org)) is sponsored by [Netlify](https://www.netlify.com) +* Password management is sponsored by [1Password](https://www.1password.com) diff --git a/node_modules/eslint/bin/eslint.js b/node_modules/eslint/bin/eslint.js index 9b202a982..eeb4647e7 100755 --- a/node_modules/eslint/bin/eslint.js +++ b/node_modules/eslint/bin/eslint.js @@ -9,14 +9,9 @@ "use strict"; -const mod = require("node:module"); - -// to use V8's code cache to speed up instantiation time -mod.enableCompileCache?.(); - // must do this initialization *before* other requires in order to work if (process.argv.includes("--debug")) { - require("debug").enable("eslint:*,-eslint:code-path,eslintrc:*"); + require("debug").enable("eslint:*,-eslint:code-path,eslintrc:*"); } //------------------------------------------------------------------------------ @@ -45,20 +40,20 @@ if (process.argv.includes("--debug")) { * @returns {Promise} The read text. */ function readStdin() { - return new Promise((resolve, reject) => { - let content = ""; - let chunk = ""; - - process.stdin - .setEncoding("utf8") - .on("readable", () => { - while ((chunk = process.stdin.read()) !== null) { - content += chunk; - } - }) - .on("end", () => resolve(content)) - .on("error", reject); - }); + return new Promise((resolve, reject) => { + let content = ""; + let chunk = ""; + + process.stdin + .setEncoding("utf8") + .on("readable", () => { + while ((chunk = process.stdin.read()) !== null) { + content += chunk; + } + }) + .on("end", () => resolve(content)) + .on("error", reject); + }); } /** @@ -67,32 +62,34 @@ function readStdin() { * @returns {string} The error message. */ function getErrorMessage(error) { - // Lazy loading because this is used only if an error happened. - const util = require("node:util"); - - // Foolproof -- third-party module might throw non-object. - if (typeof error !== "object" || error === null) { - return String(error); - } - - // Use templates if `error.messageTemplate` is present. - if (typeof error.messageTemplate === "string") { - try { - const template = require(`../messages/${error.messageTemplate}.js`); - - return template(error.messageData || {}); - } catch { - // Ignore template error then fallback to use `error.stack`. - } - } - - // Use the stacktrace if it's an error object. - if (typeof error.stack === "string") { - return error.stack; - } - - // Otherwise, dump the object. - return util.format("%o", error); + + // Lazy loading because this is used only if an error happened. + const util = require("util"); + + // Foolproof -- third-party module might throw non-object. + if (typeof error !== "object" || error === null) { + return String(error); + } + + // Use templates if `error.messageTemplate` is present. + if (typeof error.messageTemplate === "string") { + try { + const template = require(`../messages/${error.messageTemplate}.js`); + + return template(error.messageData || {}); + } catch { + + // Ignore template error then fallback to use `error.stack`. + } + } + + // Use the stacktrace if it's an error object. + if (typeof error.stack === "string") { + return error.stack; + } + + // Otherwise, dump the object. + return util.format("%o", error); } /** @@ -114,21 +111,21 @@ let hadFatalError = false; * @returns {void} */ function onFatalError(error) { - process.exitCode = 2; - hadFatalError = true; + process.exitCode = 2; + hadFatalError = true; - const { version } = require("../package.json"); - const message = ` + const { version } = require("../package.json"); + const message = ` Oops! Something went wrong! :( ESLint: ${version} ${getErrorMessage(error)}`; - if (!displayedErrors.has(message)) { - console.error(message); - displayedErrors.add(message); - } + if (!displayedErrors.has(message)) { + console.error(message); + displayedErrors.add(message); + } } //------------------------------------------------------------------------------ @@ -136,61 +133,41 @@ ${getErrorMessage(error)}`; //------------------------------------------------------------------------------ (async function main() { - process.on("uncaughtException", onFatalError); - process.on("unhandledRejection", onFatalError); - - // Call the config initializer if `--init` is present. - if (process.argv.includes("--init")) { - // `eslint --init` has been moved to `@eslint/create-config` - console.warn( - "You can also run this command directly using 'npm init @eslint/config@latest'.", - ); - - const spawn = require("cross-spawn"); - - spawn.sync("npm", ["init", "@eslint/config@latest"], { - encoding: "utf8", - stdio: "inherit", - }); - return; - } - - // start the MCP server if `--mcp` is present - if (process.argv.includes("--mcp")) { - console.warn( - "You can also run this command directly using 'npx @eslint/mcp@latest'.", - ); - - const spawn = require("cross-spawn"); - - spawn.sync("npx", ["@eslint/mcp@latest"], { - encoding: "utf8", - stdio: "inherit", - }); - return; - } - - // Otherwise, call the CLI. - const cli = require("../lib/cli"); - const exitCode = await cli.execute( - process.argv, - process.argv.includes("--stdin") ? await readStdin() : null, - true, - ); - - /* - * If an uncaught exception or unhandled rejection was detected in the meantime, - * keep the fatal exit code 2 that is already assigned to `process.exitCode`. - * Without this condition, exit code 2 (unsuccessful execution) could be overwritten with - * 1 (successful execution, lint problems found) or even 0 (successful execution, no lint problems found). - * This ensures that unexpected errors that seemingly don't affect the success - * of the execution will still cause a non-zero exit code, as it's a common - * practice and the default behavior of Node.js to exit with non-zero - * in case of an uncaught exception or unhandled rejection. - * - * Otherwise, assign the exit code returned from CLI. - */ - if (!hadFatalError) { - process.exitCode = exitCode; - } -})().catch(onFatalError); + process.on("uncaughtException", onFatalError); + process.on("unhandledRejection", onFatalError); + + // Call the config initializer if `--init` is present. + if (process.argv.includes("--init")) { + + // `eslint --init` has been moved to `@eslint/create-config` + console.warn("You can also run this command directly using 'npm init @eslint/config'."); + + const spawn = require("cross-spawn"); + + spawn.sync("npm", ["init", "@eslint/config"], { encoding: "utf8", stdio: "inherit" }); + return; + } + + // Otherwise, call the CLI. + const exitCode = await require("../lib/cli").execute( + process.argv, + process.argv.includes("--stdin") ? await readStdin() : null, + true + ); + + /* + * If an uncaught exception or unhandled rejection was detected in the meantime, + * keep the fatal exit code 2 that is already assigned to `process.exitCode`. + * Without this condition, exit code 2 (unsuccessful execution) could be overwritten with + * 1 (successful execution, lint problems found) or even 0 (successful execution, no lint problems found). + * This ensures that unexpected errors that seemingly don't affect the success + * of the execution will still cause a non-zero exit code, as it's a common + * practice and the default behavior of Node.js to exit with non-zero + * in case of an uncaught exception or unhandled rejection. + * + * Otherwise, assign the exit code returned from CLI. + */ + if (!hadFatalError) { + process.exitCode = exitCode; + } +}()).catch(onFatalError); diff --git a/node_modules/eslint/conf/default-cli-options.js b/node_modules/eslint/conf/default-cli-options.js index dda88d655..dad03d89e 100644 --- a/node_modules/eslint/conf/default-cli-options.js +++ b/node_modules/eslint/conf/default-cli-options.js @@ -6,27 +6,27 @@ "use strict"; module.exports = { - configFile: null, - baseConfig: false, - rulePaths: [], - useEslintrc: true, - envs: [], - globals: [], - extensions: null, - ignore: true, - ignorePath: void 0, - cache: false, + configFile: null, + baseConfig: false, + rulePaths: [], + useEslintrc: true, + envs: [], + globals: [], + extensions: null, + ignore: true, + ignorePath: void 0, + cache: false, - /* - * in order to honor the cacheFile option if specified - * this option should not have a default value otherwise - * it will always be used - */ - cacheLocation: "", - cacheFile: ".eslintcache", - cacheStrategy: "metadata", - fix: false, - allowInlineConfig: true, - reportUnusedDisableDirectives: void 0, - globInputPaths: true, + /* + * in order to honor the cacheFile option if specified + * this option should not have a default value otherwise + * it will always be used + */ + cacheLocation: "", + cacheFile: ".eslintcache", + cacheStrategy: "metadata", + fix: false, + allowInlineConfig: true, + reportUnusedDisableDirectives: void 0, + globInputPaths: true }; diff --git a/node_modules/eslint/conf/ecma-version.js b/node_modules/eslint/conf/ecma-version.js deleted file mode 100644 index e68d30206..000000000 --- a/node_modules/eslint/conf/ecma-version.js +++ /dev/null @@ -1,16 +0,0 @@ -/** - * @fileoverview Configuration related to ECMAScript versions - * @author Milos Djermanovic - */ - -"use strict"; - -/** - * The latest ECMAScript version supported by ESLint. - * @type {number} year-based ECMAScript version - */ -const LATEST_ECMA_VERSION = 2026; - -module.exports = { - LATEST_ECMA_VERSION, -}; diff --git a/node_modules/eslint/conf/globals.js b/node_modules/eslint/conf/globals.js index b630b272a..58710e05b 100644 --- a/node_modules/eslint/conf/globals.js +++ b/node_modules/eslint/conf/globals.js @@ -10,160 +10,145 @@ //----------------------------------------------------------------------------- const commonjs = { - exports: true, - global: false, - module: false, - require: false, + exports: true, + global: false, + module: false, + require: false }; const es3 = { - Array: false, - Boolean: false, - constructor: false, - Date: false, - decodeURI: false, - decodeURIComponent: false, - encodeURI: false, - encodeURIComponent: false, - Error: false, - escape: false, - eval: false, - EvalError: false, - Function: false, - hasOwnProperty: false, - Infinity: false, - isFinite: false, - isNaN: false, - isPrototypeOf: false, - Math: false, - NaN: false, - Number: false, - Object: false, - parseFloat: false, - parseInt: false, - propertyIsEnumerable: false, - RangeError: false, - ReferenceError: false, - RegExp: false, - String: false, - SyntaxError: false, - toLocaleString: false, - toString: false, - TypeError: false, - undefined: false, - unescape: false, - URIError: false, - valueOf: false, + Array: false, + Boolean: false, + constructor: false, + Date: false, + decodeURI: false, + decodeURIComponent: false, + encodeURI: false, + encodeURIComponent: false, + Error: false, + escape: false, + eval: false, + EvalError: false, + Function: false, + hasOwnProperty: false, + Infinity: false, + isFinite: false, + isNaN: false, + isPrototypeOf: false, + Math: false, + NaN: false, + Number: false, + Object: false, + parseFloat: false, + parseInt: false, + propertyIsEnumerable: false, + RangeError: false, + ReferenceError: false, + RegExp: false, + String: false, + SyntaxError: false, + toLocaleString: false, + toString: false, + TypeError: false, + undefined: false, + unescape: false, + URIError: false, + valueOf: false }; const es5 = { - ...es3, - JSON: false, + ...es3, + JSON: false }; const es2015 = { - ...es5, - ArrayBuffer: false, - DataView: false, - Float32Array: false, - Float64Array: false, - Int16Array: false, - Int32Array: false, - Int8Array: false, - Intl: false, - Map: false, - Promise: false, - Proxy: false, - Reflect: false, - Set: false, - Symbol: false, - Uint16Array: false, - Uint32Array: false, - Uint8Array: false, - Uint8ClampedArray: false, - WeakMap: false, - WeakSet: false, + ...es5, + ArrayBuffer: false, + DataView: false, + Float32Array: false, + Float64Array: false, + Int16Array: false, + Int32Array: false, + Int8Array: false, + Map: false, + Promise: false, + Proxy: false, + Reflect: false, + Set: false, + Symbol: false, + Uint16Array: false, + Uint32Array: false, + Uint8Array: false, + Uint8ClampedArray: false, + WeakMap: false, + WeakSet: false }; // no new globals in ES2016 const es2016 = { - ...es2015, + ...es2015 }; const es2017 = { - ...es2016, - Atomics: false, - SharedArrayBuffer: false, + ...es2016, + Atomics: false, + SharedArrayBuffer: false }; // no new globals in ES2018 const es2018 = { - ...es2017, + ...es2017 }; // no new globals in ES2019 const es2019 = { - ...es2018, + ...es2018 }; const es2020 = { - ...es2019, - BigInt: false, - BigInt64Array: false, - BigUint64Array: false, - globalThis: false, + ...es2019, + BigInt: false, + BigInt64Array: false, + BigUint64Array: false, + globalThis: false }; const es2021 = { - ...es2020, - AggregateError: false, - FinalizationRegistry: false, - WeakRef: false, + ...es2020, + AggregateError: false, + FinalizationRegistry: false, + WeakRef: false }; const es2022 = { - ...es2021, + ...es2021 }; const es2023 = { - ...es2022, + ...es2022 }; const es2024 = { - ...es2023, + ...es2023 }; -const es2025 = { - ...es2024, - Float16Array: false, - Iterator: false, -}; - -const es2026 = { - ...es2025, - AsyncDisposableStack: false, - DisposableStack: false, - SuppressedError: false, -}; //----------------------------------------------------------------------------- // Exports //----------------------------------------------------------------------------- module.exports = { - commonjs, - es3, - es5, - es2015, - es2016, - es2017, - es2018, - es2019, - es2020, - es2021, - es2022, - es2023, - es2024, - es2025, - es2026, + commonjs, + es3, + es5, + es2015, + es2016, + es2017, + es2018, + es2019, + es2020, + es2021, + es2022, + es2023, + es2024 }; diff --git a/node_modules/eslint/conf/replacements.json b/node_modules/eslint/conf/replacements.json index 27329c4c9..c047811e6 100644 --- a/node_modules/eslint/conf/replacements.json +++ b/node_modules/eslint/conf/replacements.json @@ -1,26 +1,22 @@ { - "rules": { - "generator-star": ["generator-star-spacing"], - "global-strict": ["strict"], - "no-arrow-condition": ["no-confusing-arrow", "no-constant-condition"], - "no-comma-dangle": ["comma-dangle"], - "no-empty-class": ["no-empty-character-class"], - "no-empty-label": ["no-labels"], - "no-extra-strict": ["strict"], - "no-reserved-keys": ["quote-props"], - "no-space-before-semi": ["semi-spacing"], - "no-wrap-func": ["no-extra-parens"], - "space-after-function-name": ["space-before-function-paren"], - "space-after-keywords": ["keyword-spacing"], - "space-before-function-parentheses": ["space-before-function-paren"], - "space-before-keywords": ["keyword-spacing"], - "space-in-brackets": [ - "object-curly-spacing", - "array-bracket-spacing", - "computed-property-spacing" - ], - "space-return-throw-case": ["keyword-spacing"], - "space-unary-word-ops": ["space-unary-ops"], - "spaced-line-comment": ["spaced-comment"] - } + "rules": { + "generator-star": ["generator-star-spacing"], + "global-strict": ["strict"], + "no-arrow-condition": ["no-confusing-arrow", "no-constant-condition"], + "no-comma-dangle": ["comma-dangle"], + "no-empty-class": ["no-empty-character-class"], + "no-empty-label": ["no-labels"], + "no-extra-strict": ["strict"], + "no-reserved-keys": ["quote-props"], + "no-space-before-semi": ["semi-spacing"], + "no-wrap-func": ["no-extra-parens"], + "space-after-function-name": ["space-before-function-paren"], + "space-after-keywords": ["keyword-spacing"], + "space-before-function-parentheses": ["space-before-function-paren"], + "space-before-keywords": ["keyword-spacing"], + "space-in-brackets": ["object-curly-spacing", "array-bracket-spacing", "computed-property-spacing"], + "space-return-throw-case": ["keyword-spacing"], + "space-unary-word-ops": ["space-unary-ops"], + "spaced-line-comment": ["spaced-comment"] + } } diff --git a/node_modules/eslint/conf/rule-type-list.json b/node_modules/eslint/conf/rule-type-list.json index 05ddd3cb0..6ca730f34 100644 --- a/node_modules/eslint/conf/rule-type-list.json +++ b/node_modules/eslint/conf/rule-type-list.json @@ -1,91 +1,28 @@ { - "types": { - "problem": [], - "suggestion": [], - "layout": [] - }, - "deprecated": [], - "removed": [ - { - "removed": "generator-star", - "replacedBy": [{ "rule": { "name": "generator-star-spacing" } }] - }, - { - "removed": "global-strict", - "replacedBy": [{ "rule": { "name": "strict" } }] - }, - { - "removed": "no-arrow-condition", - "replacedBy": [ - { "rule": { "name": "no-confusing-arrow" } }, - { "rule": { "name": "no-constant-condition" } } - ] - }, - { - "removed": "no-comma-dangle", - "replacedBy": [{ "rule": { "name": "comma-dangle" } }] - }, - { - "removed": "no-empty-class", - "replacedBy": [{ "rule": { "name": "no-empty-character-class" } }] - }, - { - "removed": "no-empty-label", - "replacedBy": [{ "rule": { "name": "no-labels" } }] - }, - { - "removed": "no-extra-strict", - "replacedBy": [{ "rule": { "name": "strict" } }] - }, - { - "removed": "no-reserved-keys", - "replacedBy": [{ "rule": { "name": "quote-props" } }] - }, - { - "removed": "no-space-before-semi", - "replacedBy": [{ "rule": { "name": "semi-spacing" } }] - }, - { - "removed": "no-wrap-func", - "replacedBy": [{ "rule": { "name": "no-extra-parens" } }] - }, - { - "removed": "space-after-function-name", - "replacedBy": [{ "rule": { "name": "space-before-function-paren" } }] - }, - { - "removed": "space-after-keywords", - "replacedBy": [{ "rule": { "name": "keyword-spacing" } }] - }, - { - "removed": "space-before-function-parentheses", - "replacedBy": [{ "rule": { "name": "space-before-function-paren" } }] - }, - { - "removed": "space-before-keywords", - "replacedBy": [{ "rule": { "name": "keyword-spacing" } }] - }, - { - "removed": "space-in-brackets", - "replacedBy": [ - { "rule": { "name": "object-curly-spacing" } }, - { "rule": { "name": "array-bracket-spacing" } }, - { "rule": { "name": "computed-property-spacing" } } - ] - }, - { - "removed": "space-return-throw-case", - "replacedBy": [{ "rule": { "name": "keyword-spacing" } }] - }, - { - "removed": "space-unary-word-ops", - "replacedBy": [{ "rule": { "name": "space-unary-ops" } }] - }, - { - "removed": "spaced-line-comment", - "replacedBy": [{ "rule": { "name": "spaced-comment" } }] - }, - { "removed": "valid-jsdoc", "replacedBy": [] }, - { "removed": "require-jsdoc", "replacedBy": [] } - ] + "types": { + "problem": [], + "suggestion": [], + "layout": [] + }, + "deprecated": [], + "removed": [ + { "removed": "generator-star", "replacedBy": ["generator-star-spacing"] }, + { "removed": "global-strict", "replacedBy": ["strict"] }, + { "removed": "no-arrow-condition", "replacedBy": ["no-confusing-arrow", "no-constant-condition"] }, + { "removed": "no-comma-dangle", "replacedBy": ["comma-dangle"] }, + { "removed": "no-empty-class", "replacedBy": ["no-empty-character-class"] }, + { "removed": "no-empty-label", "replacedBy": ["no-labels"] }, + { "removed": "no-extra-strict", "replacedBy": ["strict"] }, + { "removed": "no-reserved-keys", "replacedBy": ["quote-props"] }, + { "removed": "no-space-before-semi", "replacedBy": ["semi-spacing"] }, + { "removed": "no-wrap-func", "replacedBy": ["no-extra-parens"] }, + { "removed": "space-after-function-name", "replacedBy": ["space-before-function-paren"] }, + { "removed": "space-after-keywords", "replacedBy": ["keyword-spacing"] }, + { "removed": "space-before-function-parentheses", "replacedBy": ["space-before-function-paren"] }, + { "removed": "space-before-keywords", "replacedBy": ["keyword-spacing"] }, + { "removed": "space-in-brackets", "replacedBy": ["object-curly-spacing", "array-bracket-spacing"] }, + { "removed": "space-return-throw-case", "replacedBy": ["keyword-spacing"] }, + { "removed": "space-unary-word-ops", "replacedBy": ["space-unary-ops"] }, + { "removed": "spaced-line-comment", "replacedBy": ["spaced-comment"] } + ] } diff --git a/node_modules/eslint/lib/api.js b/node_modules/eslint/lib/api.js index ce6102dad..cbaac8fef 100644 --- a/node_modules/eslint/lib/api.js +++ b/node_modules/eslint/lib/api.js @@ -9,11 +9,11 @@ // Requirements //----------------------------------------------------------------------------- -const { ESLint, shouldUseFlatConfig } = require("./eslint/eslint"); -const { LegacyESLint } = require("./eslint/legacy-eslint"); +const { ESLint, FlatESLint } = require("./eslint"); +const { shouldUseFlatConfig } = require("./eslint/flat-eslint"); const { Linter } = require("./linter"); const { RuleTester } = require("./rule-tester"); -const { SourceCode } = require("./languages/js/source-code"); +const { SourceCode } = require("./source-code"); //----------------------------------------------------------------------------- // Functions @@ -23,18 +23,22 @@ const { SourceCode } = require("./languages/js/source-code"); * Loads the correct ESLint constructor given the options. * @param {Object} [options] The options object * @param {boolean} [options.useFlatConfig] Whether or not to use a flat config + * @param {string} [options.cwd] The current working directory * @returns {Promise} The ESLint constructor */ -async function loadESLint({ useFlatConfig } = {}) { - /* - * Note: The v8.x version of this function also accepted a `cwd` option, but - * it is not used in this implementation so we silently ignore it. - */ +async function loadESLint({ useFlatConfig, cwd = process.cwd() } = {}) { - const shouldESLintUseFlatConfig = - useFlatConfig ?? (await shouldUseFlatConfig()); + /* + * Note: The v9.x version of this function doesn't have a cwd option + * because it's not used. It's only used in the v8.x version of this + * function. + */ - return shouldESLintUseFlatConfig ? ESLint : LegacyESLint; + const shouldESLintUseFlatConfig = typeof useFlatConfig === "boolean" + ? useFlatConfig + : await shouldUseFlatConfig({ cwd }); + + return shouldESLintUseFlatConfig ? FlatESLint : ESLint; } //----------------------------------------------------------------------------- @@ -42,9 +46,9 @@ async function loadESLint({ useFlatConfig } = {}) { //----------------------------------------------------------------------------- module.exports = { - Linter, - loadESLint, - ESLint, - RuleTester, - SourceCode, + Linter, + loadESLint, + ESLint, + RuleTester, + SourceCode }; diff --git a/node_modules/eslint/lib/cli-engine/cli-engine.js b/node_modules/eslint/lib/cli-engine/cli-engine.js index a0c3bc67b..49c8902c1 100644 --- a/node_modules/eslint/lib/cli-engine/cli-engine.js +++ b/node_modules/eslint/lib/cli-engine/cli-engine.js @@ -15,20 +15,21 @@ // Requirements //------------------------------------------------------------------------------ -const fs = require("node:fs"); -const path = require("node:path"); +const fs = require("fs"); +const path = require("path"); const defaultOptions = require("../../conf/default-cli-options"); const pkg = require("../../package.json"); + const { - Legacy: { - ConfigOps, - naming, - CascadingConfigArrayFactory, - IgnorePattern, - getUsedExtractedConfigs, - ModuleResolver, - }, + Legacy: { + ConfigOps, + naming, + CascadingConfigArrayFactory, + IgnorePattern, + getUsedExtractedConfigs, + ModuleResolver + } } = require("@eslint/eslintrc"); const { FileEnumerator } = require("./file-enumerator"); @@ -40,17 +41,6 @@ const hash = require("./hash"); const LintResultCache = require("./lint-result-cache"); const debug = require("debug")("eslint:cli-engine"); -const removedFormatters = new Set([ - "checkstyle", - "codeframe", - "compact", - "jslint-xml", - "junit", - "table", - "tap", - "unix", - "visualstudio", -]); const validFixTypes = new Set(["directive", "problem", "suggestion", "layout"]); //------------------------------------------------------------------------------ @@ -58,15 +48,15 @@ const validFixTypes = new Set(["directive", "problem", "suggestion", "layout"]); //------------------------------------------------------------------------------ // For VSCode IntelliSense -/** @typedef {import("../types").ESLint.ConfigData} ConfigData */ -/** @typedef {import("../types").ESLint.DeprecatedRuleUse} DeprecatedRuleInfo */ -/** @typedef {import("../types").ESLint.FormatterFunction} FormatterFunction */ -/** @typedef {import("../types").Linter.LintMessage} LintMessage */ -/** @typedef {import("../types").Linter.ParserOptions} ParserOptions */ -/** @typedef {import("../types").ESLint.Plugin} Plugin */ -/** @typedef {import("../types").Rule.RuleModule} Rule */ -/** @typedef {import("../types").Linter.RuleEntry} RuleConf */ -/** @typedef {import("../types").Linter.SuppressedLintMessage} SuppressedLintMessage */ +/** @typedef {import("../shared/types").ConfigData} ConfigData */ +/** @typedef {import("../shared/types").DeprecatedRuleInfo} DeprecatedRuleInfo */ +/** @typedef {import("../shared/types").LintMessage} LintMessage */ +/** @typedef {import("../shared/types").SuppressedLintMessage} SuppressedLintMessage */ +/** @typedef {import("../shared/types").ParserOptions} ParserOptions */ +/** @typedef {import("../shared/types").Plugin} Plugin */ +/** @typedef {import("../shared/types").RuleConf} RuleConf */ +/** @typedef {import("../shared/types").Rule} Rule */ +/** @typedef {import("../shared/types").FormatterFunction} FormatterFunction */ /** @typedef {ReturnType} ConfigArray */ /** @typedef {ReturnType} ExtractedConfig */ @@ -154,11 +144,11 @@ const internalSlotsMap = new WeakMap(); * @throws {Error} If an invalid fix type is found. */ function validateFixTypes(fixTypes) { - for (const fixType of fixTypes) { - if (!validFixTypes.has(fixType)) { - throw new Error(`Invalid fix type "${fixType}" found.`); - } - } + for (const fixType of fixTypes) { + if (!validFixTypes.has(fixType)) { + throw new Error(`Invalid fix type "${fixType}" found.`); + } + } } /** @@ -168,33 +158,33 @@ function validateFixTypes(fixTypes) { * @private */ function calculateStatsPerFile(messages) { - const stat = { - errorCount: 0, - fatalErrorCount: 0, - warningCount: 0, - fixableErrorCount: 0, - fixableWarningCount: 0, - }; - - for (let i = 0; i < messages.length; i++) { - const message = messages[i]; - - if (message.fatal || message.severity === 2) { - stat.errorCount++; - if (message.fatal) { - stat.fatalErrorCount++; - } - if (message.fix) { - stat.fixableErrorCount++; - } - } else { - stat.warningCount++; - if (message.fix) { - stat.fixableWarningCount++; - } - } - } - return stat; + const stat = { + errorCount: 0, + fatalErrorCount: 0, + warningCount: 0, + fixableErrorCount: 0, + fixableWarningCount: 0 + }; + + for (let i = 0; i < messages.length; i++) { + const message = messages[i]; + + if (message.fatal || message.severity === 2) { + stat.errorCount++; + if (message.fatal) { + stat.fatalErrorCount++; + } + if (message.fix) { + stat.fixableErrorCount++; + } + } else { + stat.warningCount++; + if (message.fix) { + stat.fixableWarningCount++; + } + } + } + return stat; } /** @@ -204,25 +194,25 @@ function calculateStatsPerFile(messages) { * @private */ function calculateStatsPerRun(results) { - const stat = { - errorCount: 0, - fatalErrorCount: 0, - warningCount: 0, - fixableErrorCount: 0, - fixableWarningCount: 0, - }; - - for (let i = 0; i < results.length; i++) { - const result = results[i]; - - stat.errorCount += result.errorCount; - stat.fatalErrorCount += result.fatalErrorCount; - stat.warningCount += result.warningCount; - stat.fixableErrorCount += result.fixableErrorCount; - stat.fixableWarningCount += result.fixableWarningCount; - } - - return stat; + const stat = { + errorCount: 0, + fatalErrorCount: 0, + warningCount: 0, + fixableErrorCount: 0, + fixableWarningCount: 0 + }; + + for (let i = 0; i < results.length; i++) { + const result = results[i]; + + stat.errorCount += result.errorCount; + stat.fatalErrorCount += result.fatalErrorCount; + stat.warningCount += result.warningCount; + stat.fixableErrorCount += result.fixableErrorCount; + stat.fixableWarningCount += result.fixableWarningCount; + } + + return stat; } /** @@ -241,62 +231,65 @@ function calculateStatsPerRun(results) { * @private */ function verifyText({ - text, - cwd, - filePath: providedFilePath, - config, - fix, - allowInlineConfig, - reportUnusedDisableDirectives, - fileEnumerator, - linter, + text, + cwd, + filePath: providedFilePath, + config, + fix, + allowInlineConfig, + reportUnusedDisableDirectives, + fileEnumerator, + linter }) { - const filePath = providedFilePath || ""; - - debug(`Lint ${filePath}`); - - /* - * Verify. - * `config.extractConfig(filePath)` requires an absolute path, but `linter` - * doesn't know CWD, so it gives `linter` an absolute path always. - */ - const filePathToVerify = - filePath === "" ? path.join(cwd, filePath) : filePath; - const { fixed, messages, output } = linter.verifyAndFix(text, config, { - allowInlineConfig, - filename: filePathToVerify, - fix, - reportUnusedDisableDirectives, - - /** - * Check if the linter should adopt a given code block or not. - * @param {string} blockFilename The virtual filename of a code block. - * @returns {boolean} `true` if the linter should adopt the code block. - */ - filterCodeBlock(blockFilename) { - return fileEnumerator.isTargetPath(blockFilename); - }, - }); - - // Tweak and return. - const result = { - filePath, - messages, - suppressedMessages: linter.getSuppressedMessages(), - ...calculateStatsPerFile(messages), - }; - - if (fixed) { - result.output = output; - } - if ( - result.errorCount + result.warningCount > 0 && - typeof result.output === "undefined" - ) { - result.source = text; - } - - return result; + const filePath = providedFilePath || ""; + + debug(`Lint ${filePath}`); + + /* + * Verify. + * `config.extractConfig(filePath)` requires an absolute path, but `linter` + * doesn't know CWD, so it gives `linter` an absolute path always. + */ + const filePathToVerify = filePath === "" ? path.join(cwd, filePath) : filePath; + const { fixed, messages, output } = linter.verifyAndFix( + text, + config, + { + allowInlineConfig, + filename: filePathToVerify, + fix, + reportUnusedDisableDirectives, + + /** + * Check if the linter should adopt a given code block or not. + * @param {string} blockFilename The virtual filename of a code block. + * @returns {boolean} `true` if the linter should adopt the code block. + */ + filterCodeBlock(blockFilename) { + return fileEnumerator.isTargetPath(blockFilename); + } + } + ); + + // Tweak and return. + const result = { + filePath, + messages, + suppressedMessages: linter.getSuppressedMessages(), + ...calculateStatsPerFile(messages) + }; + + if (fixed) { + result.output = output; + } + if ( + result.errorCount + result.warningCount > 0 && + typeof result.output === "undefined" + ) { + result.source = text; + } + + return result; } /** @@ -307,42 +300,37 @@ function verifyText({ * @private */ function createIgnoreResult(filePath, baseDir) { - let message; - const isHidden = filePath - .split(path.sep) - .find(segment => /^\./u.test(segment)); - const isInNodeModules = - baseDir && path.relative(baseDir, filePath).startsWith("node_modules"); - - if (isHidden) { - message = - "File ignored by default. Use a negated ignore pattern (like \"--ignore-pattern '!'\") to override."; - } else if (isInNodeModules) { - message = - "File ignored by default. Use \"--ignore-pattern '!node_modules/*'\" to override."; - } else { - message = - 'File ignored because of a matching ignore pattern. Use "--no-ignore" to override.'; - } - - return { - filePath: path.resolve(filePath), - messages: [ - { - ruleId: null, - fatal: false, - severity: 1, - message, - nodeType: null, - }, - ], - suppressedMessages: [], - errorCount: 0, - fatalErrorCount: 0, - warningCount: 1, - fixableErrorCount: 0, - fixableWarningCount: 0, - }; + let message; + const isHidden = filePath.split(path.sep) + .find(segment => /^\./u.test(segment)); + const isInNodeModules = baseDir && path.relative(baseDir, filePath).startsWith("node_modules"); + + if (isHidden) { + message = "File ignored by default. Use a negated ignore pattern (like \"--ignore-pattern '!'\") to override."; + } else if (isInNodeModules) { + message = "File ignored by default. Use \"--ignore-pattern '!node_modules/*'\" to override."; + } else { + message = "File ignored because of a matching ignore pattern. Use \"--no-ignore\" to override."; + } + + return { + filePath: path.resolve(filePath), + messages: [ + { + ruleId: null, + fatal: false, + severity: 1, + message, + nodeType: null + } + ], + suppressedMessages: [], + errorCount: 0, + fatalErrorCount: 0, + warningCount: 1, + fixableErrorCount: 0, + fixableWarningCount: 0 + }; } /** @@ -352,14 +340,14 @@ function createIgnoreResult(filePath, baseDir) { * @returns {Rule|null} The rule or null. */ function getRule(ruleId, configArrays) { - for (const configArray of configArrays) { - const rule = configArray.pluginRules.get(ruleId); - - if (rule) { - return rule; - } - } - return builtInRules.get(ruleId) || null; + for (const configArray of configArrays) { + const rule = configArray.pluginRules.get(ruleId); + + if (rule) { + return rule; + } + } + return builtInRules.get(ruleId) || null; } /** @@ -370,13 +358,13 @@ function getRule(ruleId, configArrays) { * @returns {boolean} Whether the message should be fixed. */ function shouldMessageBeFixed(message, lastConfigArrays, fixTypes) { - if (!message.ruleId) { - return fixTypes.has("directive"); - } + if (!message.ruleId) { + return fixTypes.has("directive"); + } - const rule = message.ruleId && getRule(message.ruleId, lastConfigArrays); + const rule = message.ruleId && getRule(message.ruleId, lastConfigArrays); - return Boolean(rule && rule.meta && fixTypes.has(rule.meta.type)); + return Boolean(rule && rule.meta && fixTypes.has(rule.meta.type)); } /** @@ -384,40 +372,41 @@ function shouldMessageBeFixed(message, lastConfigArrays, fixTypes) { * @param {ConfigArray[]} usedConfigArrays The config arrays which were used. * @returns {IterableIterator} Used deprecated rules. */ -function* iterateRuleDeprecationWarnings(usedConfigArrays) { - const processedRuleIds = new Set(); - - // Flatten used configs. - /** @type {ExtractedConfig[]} */ - const configs = usedConfigArrays.flatMap(getUsedExtractedConfigs); - - // Traverse rule configs. - for (const config of configs) { - for (const [ruleId, ruleConfig] of Object.entries(config.rules)) { - // Skip if it was processed. - if (processedRuleIds.has(ruleId)) { - continue; - } - processedRuleIds.add(ruleId); - - // Skip if it's not used. - if (!ConfigOps.getRuleSeverity(ruleConfig)) { - continue; - } - const rule = getRule(ruleId, usedConfigArrays); - - // Skip if it's not deprecated. - if (!(rule && rule.meta && rule.meta.deprecated)) { - continue; - } - - // This rule was used and deprecated. - yield { - ruleId, - replacedBy: rule.meta.replacedBy || [], - }; - } - } +function *iterateRuleDeprecationWarnings(usedConfigArrays) { + const processedRuleIds = new Set(); + + // Flatten used configs. + /** @type {ExtractedConfig[]} */ + const configs = usedConfigArrays.flatMap(getUsedExtractedConfigs); + + // Traverse rule configs. + for (const config of configs) { + for (const [ruleId, ruleConfig] of Object.entries(config.rules)) { + + // Skip if it was processed. + if (processedRuleIds.has(ruleId)) { + continue; + } + processedRuleIds.add(ruleId); + + // Skip if it's not used. + if (!ConfigOps.getRuleSeverity(ruleConfig)) { + continue; + } + const rule = getRule(ruleId, usedConfigArrays); + + // Skip if it's not deprecated. + if (!(rule && rule.meta && rule.meta.deprecated)) { + continue; + } + + // This rule was used and deprecated. + yield { + ruleId, + replacedBy: rule.meta.replacedBy || [] + }; + } + } } /** @@ -427,9 +416,10 @@ function* iterateRuleDeprecationWarnings(usedConfigArrays) { * @private */ function isErrorMessage(message) { - return message.severity === 2; + return message.severity === 2; } + /** * return the cacheFile to be used by eslint, based on whether the provided parameter is * a directory or looks like a directory (ends in `path.sep`), in which case the file @@ -441,62 +431,65 @@ function isErrorMessage(message) { * @returns {string} the resolved path to the cache file */ function getCacheFile(cacheFile, cwd) { - /* - * make sure the path separators are normalized for the environment/os - * keeping the trailing path separator if present - */ - const normalizedCacheFile = path.normalize(cacheFile); - - const resolvedCacheFile = path.resolve(cwd, normalizedCacheFile); - const looksLikeADirectory = normalizedCacheFile.slice(-1) === path.sep; - - /** - * return the name for the cache file in case the provided parameter is a directory - * @returns {string} the resolved path to the cacheFile - */ - function getCacheFileForDirectory() { - return path.join(resolvedCacheFile, `.cache_${hash(cwd)}`); - } - - let fileStats; - - try { - fileStats = fs.lstatSync(resolvedCacheFile); - } catch { - fileStats = null; - } - - /* - * in case the file exists we need to verify if the provided path - * is a directory or a file. If it is a directory we want to create a file - * inside that directory - */ - if (fileStats) { - /* - * is a directory or is a file, but the original file the user provided - * looks like a directory but `path.resolve` removed the `last path.sep` - * so we need to still treat this like a directory - */ - if (fileStats.isDirectory() || looksLikeADirectory) { - return getCacheFileForDirectory(); - } - - // is file so just use that file - return resolvedCacheFile; - } - - /* - * here we known the file or directory doesn't exist, - * so we will try to infer if its a directory if it looks like a directory - * for the current operating system. - */ - - // if the last character passed is a path separator we assume is a directory - if (looksLikeADirectory) { - return getCacheFileForDirectory(); - } - - return resolvedCacheFile; + + /* + * make sure the path separators are normalized for the environment/os + * keeping the trailing path separator if present + */ + const normalizedCacheFile = path.normalize(cacheFile); + + const resolvedCacheFile = path.resolve(cwd, normalizedCacheFile); + const looksLikeADirectory = normalizedCacheFile.slice(-1) === path.sep; + + /** + * return the name for the cache file in case the provided parameter is a directory + * @returns {string} the resolved path to the cacheFile + */ + function getCacheFileForDirectory() { + return path.join(resolvedCacheFile, `.cache_${hash(cwd)}`); + } + + let fileStats; + + try { + fileStats = fs.lstatSync(resolvedCacheFile); + } catch { + fileStats = null; + } + + + /* + * in case the file exists we need to verify if the provided path + * is a directory or a file. If it is a directory we want to create a file + * inside that directory + */ + if (fileStats) { + + /* + * is a directory or is a file, but the original file the user provided + * looks like a directory but `path.resolve` removed the `last path.sep` + * so we need to still treat this like a directory + */ + if (fileStats.isDirectory() || looksLikeADirectory) { + return getCacheFileForDirectory(); + } + + // is file so just use that file + return resolvedCacheFile; + } + + /* + * here we known the file or directory doesn't exist, + * so we will try to infer if its a directory if it looks like a directory + * for the current operating system. + */ + + // if the last character passed is a path separator we assume is a directory + if (looksLikeADirectory) { + return getCacheFileForDirectory(); + } + + return resolvedCacheFile; } /** @@ -508,21 +501,23 @@ function getCacheFile(cacheFile, cwd) { * @returns {Record} The boolean map. */ function toBooleanMap(keys, defaultValue, displayName) { - if (keys && !Array.isArray(keys)) { - throw new Error(`${displayName} must be an array.`); - } - if (keys && keys.length > 0) { - return keys.reduce((map, def) => { - const [key, value] = def.split(":"); - - if (key !== "__proto__") { - map[key] = value === void 0 ? defaultValue : value === "true"; - } - - return map; - }, {}); - } - return void 0; + if (keys && !Array.isArray(keys)) { + throw new Error(`${displayName} must be an array.`); + } + if (keys && keys.length > 0) { + return keys.reduce((map, def) => { + const [key, value] = def.split(":"); + + if (key !== "__proto__") { + map[key] = value === void 0 + ? defaultValue + : value === "true"; + } + + return map; + }, {}); + } + return void 0; } /** @@ -531,30 +526,36 @@ function toBooleanMap(keys, defaultValue, displayName) { * @returns {ConfigData|null} The created config data. */ function createConfigDataFromOptions(options) { - const { ignorePattern, parser, parserOptions, plugins, rules } = options; - const env = toBooleanMap(options.envs, true, "envs"); - const globals = toBooleanMap(options.globals, false, "globals"); - - if ( - env === void 0 && - globals === void 0 && - (ignorePattern === void 0 || ignorePattern.length === 0) && - parser === void 0 && - parserOptions === void 0 && - plugins === void 0 && - rules === void 0 - ) { - return null; - } - return { - env, - globals, - ignorePatterns: ignorePattern, - parser, - parserOptions, - plugins, - rules, - }; + const { + ignorePattern, + parser, + parserOptions, + plugins, + rules + } = options; + const env = toBooleanMap(options.envs, true, "envs"); + const globals = toBooleanMap(options.globals, false, "globals"); + + if ( + env === void 0 && + globals === void 0 && + (ignorePattern === void 0 || ignorePattern.length === 0) && + parser === void 0 && + parserOptions === void 0 && + plugins === void 0 && + rules === void 0 + ) { + return null; + } + return { + env, + globals, + ignorePatterns: ignorePattern, + parser, + parserOptions, + plugins, + rules + }; } /** @@ -564,14 +565,14 @@ function createConfigDataFromOptions(options) { * @returns {boolean} `true` if a directory exists */ function directoryExists(resolvedPath) { - try { - return fs.statSync(resolvedPath).isDirectory(); - } catch (error) { - if (error && (error.code === "ENOENT" || error.code === "ENOTDIR")) { - return false; - } - throw error; - } + try { + return fs.statSync(resolvedPath).isDirectory(); + } catch (error) { + if (error && (error.code === "ENOENT" || error.code === "ENOTDIR")) { + return false; + } + throw error; + } } //------------------------------------------------------------------------------ @@ -582,528 +583,496 @@ function directoryExists(resolvedPath) { * Core CLI. */ class CLIEngine { - /** - * Creates a new instance of the core CLI engine. - * @param {CLIEngineOptions} providedOptions The options for this instance. - * @param {Object} [additionalData] Additional settings that are not CLIEngineOptions. - * @param {Record|null} [additionalData.preloadedPlugins] Preloaded plugins. - */ - constructor(providedOptions, { preloadedPlugins } = {}) { - const options = Object.assign( - Object.create(null), - defaultOptions, - { cwd: process.cwd() }, - providedOptions, - ); - - if (options.fix === void 0) { - options.fix = false; - } - - const additionalPluginPool = new Map(); - - if (preloadedPlugins) { - for (const [id, plugin] of Object.entries(preloadedPlugins)) { - additionalPluginPool.set(id, plugin); - } - } - - const cacheFilePath = getCacheFile( - options.cacheLocation || options.cacheFile, - options.cwd, - ); - const configArrayFactory = new CascadingConfigArrayFactory({ - additionalPluginPool, - baseConfig: options.baseConfig || null, - cliConfig: createConfigDataFromOptions(options), - cwd: options.cwd, - ignorePath: options.ignorePath, - resolvePluginsRelativeTo: options.resolvePluginsRelativeTo, - rulePaths: options.rulePaths, - specificConfigPath: options.configFile, - useEslintrc: options.useEslintrc, - builtInRules, - loadRules, - getEslintRecommendedConfig: () => - require("@eslint/js").configs.recommended, - getEslintAllConfig: () => require("@eslint/js").configs.all, - }); - const fileEnumerator = new FileEnumerator({ - configArrayFactory, - cwd: options.cwd, - extensions: options.extensions, - globInputPaths: options.globInputPaths, - errorOnUnmatchedPattern: options.errorOnUnmatchedPattern, - ignore: options.ignore, - }); - const lintResultCache = options.cache - ? new LintResultCache(cacheFilePath, options.cacheStrategy) - : null; - const linter = new Linter({ cwd: options.cwd, configType: "eslintrc" }); - - /** @type {ConfigArray[]} */ - const lastConfigArrays = [configArrayFactory.getConfigArrayForFile()]; - - // Store private data. - internalSlotsMap.set(this, { - additionalPluginPool, - cacheFilePath, - configArrayFactory, - defaultIgnores: IgnorePattern.createDefaultIgnore(options.cwd), - fileEnumerator, - lastConfigArrays, - lintResultCache, - linter, - options, - }); - - // setup special filter for fixes - if (options.fix && options.fixTypes && options.fixTypes.length > 0) { - debug(`Using fix types ${options.fixTypes}`); - - // throw an error if any invalid fix types are found - validateFixTypes(options.fixTypes); - - // convert to Set for faster lookup - const fixTypes = new Set(options.fixTypes); - - // save original value of options.fix in case it's a function - const originalFix = - typeof options.fix === "function" ? options.fix : () => true; - - options.fix = message => - shouldMessageBeFixed(message, lastConfigArrays, fixTypes) && - originalFix(message); - } - } - - getRules() { - const { lastConfigArrays } = internalSlotsMap.get(this); - - return new Map( - (function* () { - yield* builtInRules; - - for (const configArray of lastConfigArrays) { - yield* configArray.pluginRules; - } - })(), - ); - } - - /** - * Returns results that only contains errors. - * @param {LintResult[]} results The results to filter. - * @returns {LintResult[]} The filtered results. - */ - static getErrorResults(results) { - const filtered = []; - - results.forEach(result => { - const filteredMessages = result.messages.filter(isErrorMessage); - const filteredSuppressedMessages = - result.suppressedMessages.filter(isErrorMessage); - - if (filteredMessages.length > 0) { - filtered.push({ - ...result, - messages: filteredMessages, - suppressedMessages: filteredSuppressedMessages, - errorCount: filteredMessages.length, - warningCount: 0, - fixableErrorCount: result.fixableErrorCount, - fixableWarningCount: 0, - }); - } - }); - - return filtered; - } - - /** - * Outputs fixes from the given results to files. - * @param {LintReport} report The report object created by CLIEngine. - * @returns {void} - */ - static outputFixes(report) { - report.results - .filter(result => Object.hasOwn(result, "output")) - .forEach(result => { - fs.writeFileSync(result.filePath, result.output); - }); - } - - /** - * Resolves the patterns passed into executeOnFiles() into glob-based patterns - * for easier handling. - * @param {string[]} patterns The file patterns passed on the command line. - * @returns {string[]} The equivalent glob patterns. - */ - resolveFileGlobPatterns(patterns) { - const { options } = internalSlotsMap.get(this); - - if (options.globInputPaths === false) { - return patterns.filter(Boolean); - } - - const extensions = (options.extensions || [".js"]).map(ext => - ext.replace(/^\./u, ""), - ); - const dirSuffix = `/**/*.{${extensions.join(",")}}`; - - return patterns.filter(Boolean).map(pathname => { - const resolvedPath = path.resolve(options.cwd, pathname); - const newPath = directoryExists(resolvedPath) - ? pathname.replace(/[/\\]$/u, "") + dirSuffix - : pathname; - - return path.normalize(newPath).replace(/\\/gu, "/"); - }); - } - - /** - * Executes the current configuration on an array of file and directory names. - * @param {string[]} patterns An array of file and directory names. - * @throws {Error} As may be thrown by `fs.unlinkSync`. - * @returns {LintReport} The results for all files that were linted. - */ - executeOnFiles(patterns) { - const { - cacheFilePath, - fileEnumerator, - lastConfigArrays, - lintResultCache, - linter, - options: { - allowInlineConfig, - cache, - cwd, - fix, - reportUnusedDisableDirectives, - }, - } = internalSlotsMap.get(this); - const results = []; - const startTime = Date.now(); - - // Clear the last used config arrays. - lastConfigArrays.length = 0; - - // Delete cache file; should this do here? - if (!cache) { - try { - fs.unlinkSync(cacheFilePath); - } catch (error) { - const errorCode = error && error.code; - - // Ignore errors when no such file exists or file system is read only (and cache file does not exist) - if ( - errorCode !== "ENOENT" && - !(errorCode === "EROFS" && !fs.existsSync(cacheFilePath)) - ) { - throw error; - } - } - } - - // Iterate source code files. - for (const { config, filePath, ignored } of fileEnumerator.iterateFiles( - patterns, - )) { - if (ignored) { - results.push(createIgnoreResult(filePath, cwd)); - continue; - } - - /* - * Store used configs for: - * - this method uses to collect used deprecated rules. - * - `getRules()` method uses to collect all loaded rules. - * - `--fix-type` option uses to get the loaded rule's meta data. - */ - if (!lastConfigArrays.includes(config)) { - lastConfigArrays.push(config); - } - - // Skip if there is cached result. - if (lintResultCache) { - const cachedResult = lintResultCache.getCachedLintResults( - filePath, - config, - ); - - if (cachedResult) { - const hadMessages = - cachedResult.messages && - cachedResult.messages.length > 0; - - if (hadMessages && fix) { - debug( - `Reprocessing cached file to allow autofix: ${filePath}`, - ); - } else { - debug( - `Skipping file since it hasn't changed: ${filePath}`, - ); - results.push(cachedResult); - continue; - } - } - } - - // Do lint. - const result = verifyText({ - text: fs.readFileSync(filePath, "utf8"), - filePath, - config, - cwd, - fix, - allowInlineConfig, - reportUnusedDisableDirectives, - fileEnumerator, - linter, - }); - - results.push(result); - - /* - * Store the lint result in the LintResultCache. - * NOTE: The LintResultCache will remove the file source and any - * other properties that are difficult to serialize, and will - * hydrate those properties back in on future lint runs. - */ - if (lintResultCache) { - lintResultCache.setCachedLintResults(filePath, config, result); - } - } - - // Persist the cache to disk. - if (lintResultCache) { - lintResultCache.reconcile(); - } - - debug(`Linting complete in: ${Date.now() - startTime}ms`); - let usedDeprecatedRules; - - return { - results, - ...calculateStatsPerRun(results), - - // Initialize it lazily because CLI and `ESLint` API don't use it. - get usedDeprecatedRules() { - if (!usedDeprecatedRules) { - usedDeprecatedRules = Array.from( - iterateRuleDeprecationWarnings(lastConfigArrays), - ); - } - return usedDeprecatedRules; - }, - }; - } - - /** - * Executes the current configuration on text. - * @param {string} text A string of JavaScript code to lint. - * @param {string} [filename] An optional string representing the texts filename. - * @param {boolean} [warnIgnored] Always warn when a file is ignored - * @returns {LintReport} The results for the linting. - */ - executeOnText(text, filename, warnIgnored) { - const { - configArrayFactory, - fileEnumerator, - lastConfigArrays, - linter, - options: { - allowInlineConfig, - cwd, - fix, - reportUnusedDisableDirectives, - }, - } = internalSlotsMap.get(this); - const results = []; - const startTime = Date.now(); - const resolvedFilename = filename && path.resolve(cwd, filename); - - // Clear the last used config arrays. - lastConfigArrays.length = 0; - if (resolvedFilename && this.isPathIgnored(resolvedFilename)) { - if (warnIgnored) { - results.push(createIgnoreResult(resolvedFilename, cwd)); - } - } else { - const config = configArrayFactory.getConfigArrayForFile( - resolvedFilename || "__placeholder__.js", - ); - - /* - * Store used configs for: - * - this method uses to collect used deprecated rules. - * - `getRules()` method uses to collect all loaded rules. - * - `--fix-type` option uses to get the loaded rule's meta data. - */ - lastConfigArrays.push(config); - - // Do lint. - results.push( - verifyText({ - text, - filePath: resolvedFilename, - config, - cwd, - fix, - allowInlineConfig, - reportUnusedDisableDirectives, - fileEnumerator, - linter, - }), - ); - } - - debug(`Linting complete in: ${Date.now() - startTime}ms`); - let usedDeprecatedRules; - - return { - results, - ...calculateStatsPerRun(results), - - // Initialize it lazily because CLI and `ESLint` API don't use it. - get usedDeprecatedRules() { - if (!usedDeprecatedRules) { - usedDeprecatedRules = Array.from( - iterateRuleDeprecationWarnings(lastConfigArrays), - ); - } - return usedDeprecatedRules; - }, - }; - } - - /** - * Returns a configuration object for the given file based on the CLI options. - * This is the same logic used by the ESLint CLI executable to determine - * configuration for each file it processes. - * @param {string} filePath The path of the file to retrieve a config object for. - * @throws {Error} If filepath a directory path. - * @returns {ConfigData} A configuration object for the file. - */ - getConfigForFile(filePath) { - const { configArrayFactory, options } = internalSlotsMap.get(this); - const absolutePath = path.resolve(options.cwd, filePath); - - if (directoryExists(absolutePath)) { - throw Object.assign( - new Error("'filePath' should not be a directory path."), - { messageTemplate: "print-config-with-directory-path" }, - ); - } - - return configArrayFactory - .getConfigArrayForFile(absolutePath) - .extractConfig(absolutePath) - .toCompatibleObjectAsConfigFileContent(); - } - - /** - * Checks if a given path is ignored by ESLint. - * @param {string} filePath The path of the file to check. - * @returns {boolean} Whether or not the given path is ignored. - */ - isPathIgnored(filePath) { - const { - configArrayFactory, - defaultIgnores, - options: { cwd, ignore }, - } = internalSlotsMap.get(this); - const absolutePath = path.resolve(cwd, filePath); - - if (ignore) { - const config = configArrayFactory - .getConfigArrayForFile(absolutePath) - .extractConfig(absolutePath); - const ignores = config.ignores || defaultIgnores; - - return ignores(absolutePath); - } - - return defaultIgnores(absolutePath); - } - - /** - * Returns the formatter representing the given format or null if the `format` is not a string. - * @param {string} [format] The name of the format to load or the path to a - * custom formatter. - * @throws {any} As may be thrown by requiring of formatter - * @returns {(FormatterFunction|null)} The formatter function or null if the `format` is not a string. - */ - getFormatter(format) { - // default is stylish - const resolvedFormatName = format || "stylish"; - - // only strings are valid formatters - if (typeof resolvedFormatName === "string") { - // replace \ with / for Windows compatibility - const normalizedFormatName = resolvedFormatName.replace( - /\\/gu, - "/", - ); - - const slots = internalSlotsMap.get(this); - const cwd = slots ? slots.options.cwd : process.cwd(); - const namespace = naming.getNamespaceFromTerm(normalizedFormatName); - - let formatterPath; - - // if there's a slash, then it's a file (TODO: this check seems dubious for scoped npm packages) - if (!namespace && normalizedFormatName.includes("/")) { - formatterPath = path.resolve(cwd, normalizedFormatName); - } else { - try { - const npmFormat = naming.normalizePackageName( - normalizedFormatName, - "eslint-formatter", - ); - - formatterPath = ModuleResolver.resolve( - npmFormat, - path.join(cwd, "__placeholder__.js"), - ); - } catch { - formatterPath = path.resolve( - __dirname, - "formatters", - normalizedFormatName, - ); - } - } - - try { - return require(formatterPath); - } catch (ex) { - if (removedFormatters.has(format)) { - ex.message = `The ${format} formatter is no longer part of core ESLint. Install it manually with \`npm install -D eslint-formatter-${format}\``; - } else { - ex.message = `There was a problem loading formatter: ${formatterPath}\nError: ${ex.message}`; - } - throw ex; - } - } else { - return null; - } - } + + /** + * Creates a new instance of the core CLI engine. + * @param {CLIEngineOptions} providedOptions The options for this instance. + * @param {Object} [additionalData] Additional settings that are not CLIEngineOptions. + * @param {Record|null} [additionalData.preloadedPlugins] Preloaded plugins. + */ + constructor(providedOptions, { preloadedPlugins } = {}) { + const options = Object.assign( + Object.create(null), + defaultOptions, + { cwd: process.cwd() }, + providedOptions + ); + + if (options.fix === void 0) { + options.fix = false; + } + + const additionalPluginPool = new Map(); + + if (preloadedPlugins) { + for (const [id, plugin] of Object.entries(preloadedPlugins)) { + additionalPluginPool.set(id, plugin); + } + } + + const cacheFilePath = getCacheFile( + options.cacheLocation || options.cacheFile, + options.cwd + ); + const configArrayFactory = new CascadingConfigArrayFactory({ + additionalPluginPool, + baseConfig: options.baseConfig || null, + cliConfig: createConfigDataFromOptions(options), + cwd: options.cwd, + ignorePath: options.ignorePath, + resolvePluginsRelativeTo: options.resolvePluginsRelativeTo, + rulePaths: options.rulePaths, + specificConfigPath: options.configFile, + useEslintrc: options.useEslintrc, + builtInRules, + loadRules, + getEslintRecommendedConfig: () => require("@eslint/js").configs.recommended, + getEslintAllConfig: () => require("@eslint/js").configs.all + }); + const fileEnumerator = new FileEnumerator({ + configArrayFactory, + cwd: options.cwd, + extensions: options.extensions, + globInputPaths: options.globInputPaths, + errorOnUnmatchedPattern: options.errorOnUnmatchedPattern, + ignore: options.ignore + }); + const lintResultCache = + options.cache ? new LintResultCache(cacheFilePath, options.cacheStrategy) : null; + const linter = new Linter({ cwd: options.cwd }); + + /** @type {ConfigArray[]} */ + const lastConfigArrays = [configArrayFactory.getConfigArrayForFile()]; + + // Store private data. + internalSlotsMap.set(this, { + additionalPluginPool, + cacheFilePath, + configArrayFactory, + defaultIgnores: IgnorePattern.createDefaultIgnore(options.cwd), + fileEnumerator, + lastConfigArrays, + lintResultCache, + linter, + options + }); + + // setup special filter for fixes + if (options.fix && options.fixTypes && options.fixTypes.length > 0) { + debug(`Using fix types ${options.fixTypes}`); + + // throw an error if any invalid fix types are found + validateFixTypes(options.fixTypes); + + // convert to Set for faster lookup + const fixTypes = new Set(options.fixTypes); + + // save original value of options.fix in case it's a function + const originalFix = (typeof options.fix === "function") + ? options.fix : () => true; + + options.fix = message => shouldMessageBeFixed(message, lastConfigArrays, fixTypes) && originalFix(message); + } + } + + getRules() { + const { lastConfigArrays } = internalSlotsMap.get(this); + + return new Map(function *() { + yield* builtInRules; + + for (const configArray of lastConfigArrays) { + yield* configArray.pluginRules; + } + }()); + } + + /** + * Returns results that only contains errors. + * @param {LintResult[]} results The results to filter. + * @returns {LintResult[]} The filtered results. + */ + static getErrorResults(results) { + const filtered = []; + + results.forEach(result => { + const filteredMessages = result.messages.filter(isErrorMessage); + const filteredSuppressedMessages = result.suppressedMessages.filter(isErrorMessage); + + if (filteredMessages.length > 0) { + filtered.push({ + ...result, + messages: filteredMessages, + suppressedMessages: filteredSuppressedMessages, + errorCount: filteredMessages.length, + warningCount: 0, + fixableErrorCount: result.fixableErrorCount, + fixableWarningCount: 0 + }); + } + }); + + return filtered; + } + + /** + * Outputs fixes from the given results to files. + * @param {LintReport} report The report object created by CLIEngine. + * @returns {void} + */ + static outputFixes(report) { + report.results.filter(result => Object.prototype.hasOwnProperty.call(result, "output")).forEach(result => { + fs.writeFileSync(result.filePath, result.output); + }); + } + + /** + * Resolves the patterns passed into executeOnFiles() into glob-based patterns + * for easier handling. + * @param {string[]} patterns The file patterns passed on the command line. + * @returns {string[]} The equivalent glob patterns. + */ + resolveFileGlobPatterns(patterns) { + const { options } = internalSlotsMap.get(this); + + if (options.globInputPaths === false) { + return patterns.filter(Boolean); + } + + const extensions = (options.extensions || [".js"]).map(ext => ext.replace(/^\./u, "")); + const dirSuffix = `/**/*.{${extensions.join(",")}}`; + + return patterns.filter(Boolean).map(pathname => { + const resolvedPath = path.resolve(options.cwd, pathname); + const newPath = directoryExists(resolvedPath) + ? pathname.replace(/[/\\]$/u, "") + dirSuffix + : pathname; + + return path.normalize(newPath).replace(/\\/gu, "/"); + }); + } + + /** + * Executes the current configuration on an array of file and directory names. + * @param {string[]} patterns An array of file and directory names. + * @throws {Error} As may be thrown by `fs.unlinkSync`. + * @returns {LintReport} The results for all files that were linted. + */ + executeOnFiles(patterns) { + const { + cacheFilePath, + fileEnumerator, + lastConfigArrays, + lintResultCache, + linter, + options: { + allowInlineConfig, + cache, + cwd, + fix, + reportUnusedDisableDirectives + } + } = internalSlotsMap.get(this); + const results = []; + const startTime = Date.now(); + + // Clear the last used config arrays. + lastConfigArrays.length = 0; + + // Delete cache file; should this do here? + if (!cache) { + try { + fs.unlinkSync(cacheFilePath); + } catch (error) { + const errorCode = error && error.code; + + // Ignore errors when no such file exists or file system is read only (and cache file does not exist) + if (errorCode !== "ENOENT" && !(errorCode === "EROFS" && !fs.existsSync(cacheFilePath))) { + throw error; + } + } + } + + // Iterate source code files. + for (const { config, filePath, ignored } of fileEnumerator.iterateFiles(patterns)) { + if (ignored) { + results.push(createIgnoreResult(filePath, cwd)); + continue; + } + + /* + * Store used configs for: + * - this method uses to collect used deprecated rules. + * - `getRules()` method uses to collect all loaded rules. + * - `--fix-type` option uses to get the loaded rule's meta data. + */ + if (!lastConfigArrays.includes(config)) { + lastConfigArrays.push(config); + } + + // Skip if there is cached result. + if (lintResultCache) { + const cachedResult = + lintResultCache.getCachedLintResults(filePath, config); + + if (cachedResult) { + const hadMessages = + cachedResult.messages && + cachedResult.messages.length > 0; + + if (hadMessages && fix) { + debug(`Reprocessing cached file to allow autofix: ${filePath}`); + } else { + debug(`Skipping file since it hasn't changed: ${filePath}`); + results.push(cachedResult); + continue; + } + } + } + + // Do lint. + const result = verifyText({ + text: fs.readFileSync(filePath, "utf8"), + filePath, + config, + cwd, + fix, + allowInlineConfig, + reportUnusedDisableDirectives, + fileEnumerator, + linter + }); + + results.push(result); + + /* + * Store the lint result in the LintResultCache. + * NOTE: The LintResultCache will remove the file source and any + * other properties that are difficult to serialize, and will + * hydrate those properties back in on future lint runs. + */ + if (lintResultCache) { + lintResultCache.setCachedLintResults(filePath, config, result); + } + } + + // Persist the cache to disk. + if (lintResultCache) { + lintResultCache.reconcile(); + } + + debug(`Linting complete in: ${Date.now() - startTime}ms`); + let usedDeprecatedRules; + + return { + results, + ...calculateStatsPerRun(results), + + // Initialize it lazily because CLI and `ESLint` API don't use it. + get usedDeprecatedRules() { + if (!usedDeprecatedRules) { + usedDeprecatedRules = Array.from( + iterateRuleDeprecationWarnings(lastConfigArrays) + ); + } + return usedDeprecatedRules; + } + }; + } + + /** + * Executes the current configuration on text. + * @param {string} text A string of JavaScript code to lint. + * @param {string} [filename] An optional string representing the texts filename. + * @param {boolean} [warnIgnored] Always warn when a file is ignored + * @returns {LintReport} The results for the linting. + */ + executeOnText(text, filename, warnIgnored) { + const { + configArrayFactory, + fileEnumerator, + lastConfigArrays, + linter, + options: { + allowInlineConfig, + cwd, + fix, + reportUnusedDisableDirectives + } + } = internalSlotsMap.get(this); + const results = []; + const startTime = Date.now(); + const resolvedFilename = filename && path.resolve(cwd, filename); + + + // Clear the last used config arrays. + lastConfigArrays.length = 0; + if (resolvedFilename && this.isPathIgnored(resolvedFilename)) { + if (warnIgnored) { + results.push(createIgnoreResult(resolvedFilename, cwd)); + } + } else { + const config = configArrayFactory.getConfigArrayForFile( + resolvedFilename || "__placeholder__.js" + ); + + /* + * Store used configs for: + * - this method uses to collect used deprecated rules. + * - `getRules()` method uses to collect all loaded rules. + * - `--fix-type` option uses to get the loaded rule's meta data. + */ + lastConfigArrays.push(config); + + // Do lint. + results.push(verifyText({ + text, + filePath: resolvedFilename, + config, + cwd, + fix, + allowInlineConfig, + reportUnusedDisableDirectives, + fileEnumerator, + linter + })); + } + + debug(`Linting complete in: ${Date.now() - startTime}ms`); + let usedDeprecatedRules; + + return { + results, + ...calculateStatsPerRun(results), + + // Initialize it lazily because CLI and `ESLint` API don't use it. + get usedDeprecatedRules() { + if (!usedDeprecatedRules) { + usedDeprecatedRules = Array.from( + iterateRuleDeprecationWarnings(lastConfigArrays) + ); + } + return usedDeprecatedRules; + } + }; + } + + /** + * Returns a configuration object for the given file based on the CLI options. + * This is the same logic used by the ESLint CLI executable to determine + * configuration for each file it processes. + * @param {string} filePath The path of the file to retrieve a config object for. + * @throws {Error} If filepath a directory path. + * @returns {ConfigData} A configuration object for the file. + */ + getConfigForFile(filePath) { + const { configArrayFactory, options } = internalSlotsMap.get(this); + const absolutePath = path.resolve(options.cwd, filePath); + + if (directoryExists(absolutePath)) { + throw Object.assign( + new Error("'filePath' should not be a directory path."), + { messageTemplate: "print-config-with-directory-path" } + ); + } + + return configArrayFactory + .getConfigArrayForFile(absolutePath) + .extractConfig(absolutePath) + .toCompatibleObjectAsConfigFileContent(); + } + + /** + * Checks if a given path is ignored by ESLint. + * @param {string} filePath The path of the file to check. + * @returns {boolean} Whether or not the given path is ignored. + */ + isPathIgnored(filePath) { + const { + configArrayFactory, + defaultIgnores, + options: { cwd, ignore } + } = internalSlotsMap.get(this); + const absolutePath = path.resolve(cwd, filePath); + + if (ignore) { + const config = configArrayFactory + .getConfigArrayForFile(absolutePath) + .extractConfig(absolutePath); + const ignores = config.ignores || defaultIgnores; + + return ignores(absolutePath); + } + + return defaultIgnores(absolutePath); + } + + /** + * Returns the formatter representing the given format or null if the `format` is not a string. + * @param {string} [format] The name of the format to load or the path to a + * custom formatter. + * @throws {any} As may be thrown by requiring of formatter + * @returns {(FormatterFunction|null)} The formatter function or null if the `format` is not a string. + */ + getFormatter(format) { + + // default is stylish + const resolvedFormatName = format || "stylish"; + + // only strings are valid formatters + if (typeof resolvedFormatName === "string") { + + // replace \ with / for Windows compatibility + const normalizedFormatName = resolvedFormatName.replace(/\\/gu, "/"); + + const slots = internalSlotsMap.get(this); + const cwd = slots ? slots.options.cwd : process.cwd(); + const namespace = naming.getNamespaceFromTerm(normalizedFormatName); + + let formatterPath; + + // if there's a slash, then it's a file (TODO: this check seems dubious for scoped npm packages) + if (!namespace && normalizedFormatName.includes("/")) { + formatterPath = path.resolve(cwd, normalizedFormatName); + } else { + try { + const npmFormat = naming.normalizePackageName(normalizedFormatName, "eslint-formatter"); + + formatterPath = ModuleResolver.resolve(npmFormat, path.join(cwd, "__placeholder__.js")); + } catch { + formatterPath = path.resolve(__dirname, "formatters", normalizedFormatName); + } + } + + try { + return require(formatterPath); + } catch (ex) { + if (format === "table" || format === "codeframe") { + ex.message = `The ${format} formatter is no longer part of core ESLint. Install it manually with \`npm install -D eslint-formatter-${format}\``; + } else { + ex.message = `There was a problem loading formatter: ${formatterPath}\nError: ${ex.message}`; + } + throw ex; + } + + } else { + return null; + } + } } CLIEngine.version = pkg.version; CLIEngine.getFormatter = CLIEngine.prototype.getFormatter; module.exports = { - CLIEngine, - - /** - * Get the internal slots of a given CLIEngine instance for tests. - * @param {CLIEngine} instance The CLIEngine instance to get. - * @returns {CLIEngineInternalSlots} The internal slots. - */ - getCLIEngineInternalSlots(instance) { - return internalSlotsMap.get(instance); - }, + CLIEngine, + + /** + * Get the internal slots of a given CLIEngine instance for tests. + * @param {CLIEngine} instance The CLIEngine instance to get. + * @returns {CLIEngineInternalSlots} The internal slots. + */ + getCLIEngineInternalSlots(instance) { + return internalSlotsMap.get(instance); + } }; diff --git a/node_modules/eslint/lib/cli-engine/file-enumerator.js b/node_modules/eslint/lib/cli-engine/file-enumerator.js index 91c4013c9..5dff8d09c 100644 --- a/node_modules/eslint/lib/cli-engine/file-enumerator.js +++ b/node_modules/eslint/lib/cli-engine/file-enumerator.js @@ -34,15 +34,18 @@ // Requirements //------------------------------------------------------------------------------ -const fs = require("node:fs"); -const path = require("node:path"); +const fs = require("fs"); +const path = require("path"); const getGlobParent = require("glob-parent"); const isGlob = require("is-glob"); const escapeRegExp = require("escape-string-regexp"); const { Minimatch } = require("minimatch"); const { - Legacy: { IgnorePattern, CascadingConfigArrayFactory }, + Legacy: { + IgnorePattern, + CascadingConfigArrayFactory + } } = require("@eslint/eslintrc"); const debug = require("debug")("eslint:file-enumerator"); @@ -51,7 +54,7 @@ const debug = require("debug")("eslint:file-enumerator"); //------------------------------------------------------------------------------ const minimatchOpts = { dot: true, matchBase: true }; -const dotfilesPattern = /(?:^\.|[/\\]\.)[^/\\.].*/u; +const dotfilesPattern = /(?:(?:^\.)|(?:[/\\]\.))[^/\\.].*/u; const NONE = 0; const IGNORED_SILENTLY = 1; const IGNORED = 2; @@ -105,7 +108,7 @@ const internalSlotsMap = new WeakMap(); * @returns {boolean} `true` if the string is a glob pattern. */ function isGlobPattern(pattern) { - return isGlob(path.sep === "\\" ? pattern.replace(/\\/gu, "/") : pattern); + return isGlob(path.sep === "\\" ? pattern.replace(/\\/gu, "/") : pattern); } /** @@ -116,15 +119,16 @@ function isGlobPattern(pattern) { * @private */ function statSafeSync(filePath) { - try { - return fs.statSync(filePath); - } catch (error) { - /* c8 ignore next */ - if (error.code !== "ENOENT") { - throw error; - } - return null; - } + try { + return fs.statSync(filePath); + } catch (error) { + + /* c8 ignore next */ + if (error.code !== "ENOENT") { + throw error; + } + return null; + } } /** @@ -135,15 +139,16 @@ function statSafeSync(filePath) { * @private */ function readdirSafeSync(directoryPath) { - try { - return fs.readdirSync(directoryPath, { withFileTypes: true }); - } catch (error) { - /* c8 ignore next */ - if (error.code !== "ENOENT") { - throw error; - } - return []; - } + try { + return fs.readdirSync(directoryPath, { withFileTypes: true }); + } catch (error) { + + /* c8 ignore next */ + if (error.code !== "ENOENT") { + throw error; + } + return []; + } } /** @@ -152,45 +157,50 @@ function readdirSafeSync(directoryPath) { * @returns {RegExp | null} The created `RegExp` object or null. */ function createExtensionRegExp(extensions) { - if (extensions) { - const normalizedExts = extensions.map(ext => - escapeRegExp(ext.startsWith(".") ? ext.slice(1) : ext), - ); - - return new RegExp(`.\\.(?:${normalizedExts.join("|")})$`, "u"); - } - return null; + if (extensions) { + const normalizedExts = extensions.map(ext => escapeRegExp( + ext.startsWith(".") + ? ext.slice(1) + : ext + )); + + return new RegExp( + `.\\.(?:${normalizedExts.join("|")})$`, + "u" + ); + } + return null; } /** * The error type when no files match a glob. */ class NoFilesFoundError extends Error { - /** - * @param {string} pattern The glob pattern which was not found. - * @param {boolean} globDisabled If `true` then the pattern was a glob pattern, but glob was disabled. - */ - constructor(pattern, globDisabled) { - super( - `No files matching '${pattern}' were found${globDisabled ? " (glob was disabled)" : ""}.`, - ); - this.messageTemplate = "file-not-found"; - this.messageData = { pattern, globDisabled }; - } + + /** + * @param {string} pattern The glob pattern which was not found. + * @param {boolean} globDisabled If `true` then the pattern was a glob pattern, but glob was disabled. + */ + constructor(pattern, globDisabled) { + super(`No files matching '${pattern}' were found${globDisabled ? " (glob was disabled)" : ""}.`); + this.messageTemplate = "file-not-found"; + this.messageData = { pattern, globDisabled }; + } } /** * The error type when there are files matched by a glob, but all of them have been ignored. */ class AllFilesIgnoredError extends Error { - /** - * @param {string} pattern The glob pattern which was not found. - */ - constructor(pattern) { - super(`All files matched by '${pattern}' are ignored.`); - this.messageTemplate = "all-files-ignored"; - this.messageData = { pattern }; - } + + /** + * @param {string} pattern The glob pattern which was not found. + */ + constructor(pattern) { + super(`All files matched by '${pattern}' are ignored.`); + this.messageTemplate = "all-files-ignored"; + this.messageData = { pattern }; + } } /** @@ -198,340 +208,336 @@ class AllFilesIgnoredError extends Error { * matched by given glob patterns and that configuration. */ class FileEnumerator { - /** - * Initialize this enumerator. - * @param {FileEnumeratorOptions} options The options. - */ - constructor({ - cwd = process.cwd(), - configArrayFactory = new CascadingConfigArrayFactory({ - cwd, - getEslintRecommendedConfig: () => - require("@eslint/js").configs.recommended, - getEslintAllConfig: () => require("@eslint/js").configs.all, - }), - extensions = null, - globInputPaths = true, - errorOnUnmatchedPattern = true, - ignore = true, - } = {}) { - internalSlotsMap.set(this, { - configArrayFactory, - cwd, - defaultIgnores: IgnorePattern.createDefaultIgnore(cwd), - extensionRegExp: createExtensionRegExp(extensions), - globInputPaths, - errorOnUnmatchedPattern, - ignoreFlag: ignore, - }); - } - - /** - * Check if a given file is target or not. - * @param {string} filePath The path to a candidate file. - * @param {ConfigArray} [providedConfig] Optional. The configuration for the file. - * @returns {boolean} `true` if the file is a target. - */ - isTargetPath(filePath, providedConfig) { - const { configArrayFactory, extensionRegExp } = - internalSlotsMap.get(this); - - // If `--ext` option is present, use it. - if (extensionRegExp) { - return extensionRegExp.test(filePath); - } - - // `.js` file is target by default. - if (filePath.endsWith(".js")) { - return true; - } - - // use `overrides[].files` to check additional targets. - const config = - providedConfig || - configArrayFactory.getConfigArrayForFile(filePath, { - ignoreNotFoundError: true, - }); - - return config.isAdditionalTargetPath(filePath); - } - - /** - * Iterate files which are matched by given glob patterns. - * @param {string|string[]} patternOrPatterns The glob patterns to iterate files. - * @throws {NoFilesFoundError|AllFilesIgnoredError} On an unmatched pattern. - * @returns {IterableIterator} The found files. - */ - *iterateFiles(patternOrPatterns) { - const { globInputPaths, errorOnUnmatchedPattern } = - internalSlotsMap.get(this); - const patterns = Array.isArray(patternOrPatterns) - ? patternOrPatterns - : [patternOrPatterns]; - - debug("Start to iterate files: %o", patterns); - - // The set of paths to remove duplicate. - const set = new Set(); - - for (const pattern of patterns) { - let foundRegardlessOfIgnored = false; - let found = false; - - // Skip empty string. - if (!pattern) { - continue; - } - - // Iterate files of this pattern. - for (const { config, filePath, flag } of this._iterateFiles( - pattern, - )) { - foundRegardlessOfIgnored = true; - if (flag === IGNORED_SILENTLY) { - continue; - } - found = true; - - // Remove duplicate paths while yielding paths. - if (!set.has(filePath)) { - set.add(filePath); - yield { - config, - filePath, - ignored: flag === IGNORED, - }; - } - } - - // Raise an error if any files were not found. - if (errorOnUnmatchedPattern) { - if (!foundRegardlessOfIgnored) { - throw new NoFilesFoundError( - pattern, - !globInputPaths && isGlob(pattern), - ); - } - if (!found) { - throw new AllFilesIgnoredError(pattern); - } - } - } - - debug(`Complete iterating files: ${JSON.stringify(patterns)}`); - } - - /** - * Iterate files which are matched by a given glob pattern. - * @param {string} pattern The glob pattern to iterate files. - * @returns {IterableIterator} The found files. - */ - _iterateFiles(pattern) { - const { cwd, globInputPaths } = internalSlotsMap.get(this); - const absolutePath = path.resolve(cwd, pattern); - const isDot = dotfilesPattern.test(pattern); - const stat = statSafeSync(absolutePath); - - if (stat && stat.isDirectory()) { - return this._iterateFilesWithDirectory(absolutePath, isDot); - } - if (stat && stat.isFile()) { - return this._iterateFilesWithFile(absolutePath); - } - if (globInputPaths && isGlobPattern(pattern)) { - return this._iterateFilesWithGlob(pattern, isDot); - } - - return []; - } - - /** - * Iterate a file which is matched by a given path. - * @param {string} filePath The path to the target file. - * @returns {IterableIterator} The found files. - * @private - */ - _iterateFilesWithFile(filePath) { - debug(`File: ${filePath}`); - - const { configArrayFactory } = internalSlotsMap.get(this); - const config = configArrayFactory.getConfigArrayForFile(filePath); - const ignored = this._isIgnoredFile(filePath, { config, direct: true }); - const flag = ignored ? IGNORED : NONE; - - return [{ config, filePath, flag }]; - } - - /** - * Iterate files in a given path. - * @param {string} directoryPath The path to the target directory. - * @param {boolean} dotfiles If `true` then it doesn't skip dot files by default. - * @returns {IterableIterator} The found files. - * @private - */ - _iterateFilesWithDirectory(directoryPath, dotfiles) { - debug(`Directory: ${directoryPath}`); - - return this._iterateFilesRecursive(directoryPath, { - dotfiles, - recursive: true, - selector: null, - }); - } - - /** - * Iterate files which are matched by a given glob pattern. - * @param {string} pattern The glob pattern to iterate files. - * @param {boolean} dotfiles If `true` then it doesn't skip dot files by default. - * @returns {IterableIterator} The found files. - * @private - */ - _iterateFilesWithGlob(pattern, dotfiles) { - debug(`Glob: ${pattern}`); - - const { cwd } = internalSlotsMap.get(this); - const directoryPath = path.resolve(cwd, getGlobParent(pattern)); - const absolutePath = path.resolve(cwd, pattern); - const globPart = absolutePath.slice(directoryPath.length + 1); - - /* - * recursive if there are `**` or path separators in the glob part. - * Otherwise, patterns such as `src/*.js`, it doesn't need recursive. - */ - const recursive = /\*\*|\/|\\/u.test(globPart); - const selector = new Minimatch(absolutePath, minimatchOpts); - - debug(`recursive? ${recursive}`); - - return this._iterateFilesRecursive(directoryPath, { - dotfiles, - recursive, - selector, - }); - } - - /** - * Iterate files in a given path. - * @param {string} directoryPath The path to the target directory. - * @param {Object} options The options to iterate files. - * @param {boolean} [options.dotfiles] If `true` then it doesn't skip dot files by default. - * @param {boolean} [options.recursive] If `true` then it dives into sub directories. - * @param {InstanceType} [options.selector] The matcher to choose files. - * @returns {IterableIterator} The found files. - * @private - */ - *_iterateFilesRecursive(directoryPath, options) { - debug(`Enter the directory: ${directoryPath}`); - const { configArrayFactory } = internalSlotsMap.get(this); - - /** @type {ConfigArray|null} */ - let config = null; - - // Enumerate the files of this directory. - for (const entry of readdirSafeSync(directoryPath)) { - const filePath = path.join(directoryPath, entry.name); - const fileInfo = entry.isSymbolicLink() - ? statSafeSync(filePath) - : entry; - - if (!fileInfo) { - continue; - } - - // Check if the file is matched. - if (fileInfo.isFile()) { - if (!config) { - config = configArrayFactory.getConfigArrayForFile( - filePath, - - /* - * We must ignore `ConfigurationNotFoundError` at this - * point because we don't know if target files exist in - * this directory. - */ - { ignoreNotFoundError: true }, - ); - } - const matched = options.selector - ? // Started with a glob pattern; choose by the pattern. - options.selector.match(filePath) - : // Started with a directory path; choose by file extensions. - this.isTargetPath(filePath, config); - - if (matched) { - const ignored = this._isIgnoredFile(filePath, { - ...options, - config, - }); - const flag = ignored ? IGNORED_SILENTLY : NONE; - - debug( - `Yield: ${entry.name}${ignored ? " but ignored" : ""}`, - ); - yield { - config: configArrayFactory.getConfigArrayForFile( - filePath, - ), - filePath, - flag, - }; - } else { - debug(`Didn't match: ${entry.name}`); - } - - // Dive into the sub directory. - } else if (options.recursive && fileInfo.isDirectory()) { - if (!config) { - config = configArrayFactory.getConfigArrayForFile( - filePath, - { ignoreNotFoundError: true }, - ); - } - const ignored = this._isIgnoredFile(filePath + path.sep, { - ...options, - config, - }); - - if (!ignored) { - yield* this._iterateFilesRecursive(filePath, options); - } - } - } - - debug(`Leave the directory: ${directoryPath}`); - } - - /** - * Check if a given file should be ignored. - * @param {string} filePath The path to a file to check. - * @param {Object} options Options - * @param {ConfigArray} [options.config] The config for this file. - * @param {boolean} [options.dotfiles] If `true` then this is not ignore dot files by default. - * @param {boolean} [options.direct] If `true` then this is a direct specified file. - * @returns {boolean} `true` if the file should be ignored. - * @private - */ - _isIgnoredFile( - filePath, - { config: providedConfig, dotfiles = false, direct = false }, - ) { - const { configArrayFactory, defaultIgnores, ignoreFlag } = - internalSlotsMap.get(this); - - if (ignoreFlag) { - const config = - providedConfig || - configArrayFactory.getConfigArrayForFile(filePath, { - ignoreNotFoundError: true, - }); - const ignores = - config.extractConfig(filePath).ignores || defaultIgnores; - - return ignores(filePath, dotfiles); - } - - return !direct && defaultIgnores(filePath, dotfiles); - } + + /** + * Initialize this enumerator. + * @param {FileEnumeratorOptions} options The options. + */ + constructor({ + cwd = process.cwd(), + configArrayFactory = new CascadingConfigArrayFactory({ + cwd, + getEslintRecommendedConfig: () => require("@eslint/js").configs.recommended, + getEslintAllConfig: () => require("@eslint/js").configs.all + }), + extensions = null, + globInputPaths = true, + errorOnUnmatchedPattern = true, + ignore = true + } = {}) { + internalSlotsMap.set(this, { + configArrayFactory, + cwd, + defaultIgnores: IgnorePattern.createDefaultIgnore(cwd), + extensionRegExp: createExtensionRegExp(extensions), + globInputPaths, + errorOnUnmatchedPattern, + ignoreFlag: ignore + }); + } + + /** + * Check if a given file is target or not. + * @param {string} filePath The path to a candidate file. + * @param {ConfigArray} [providedConfig] Optional. The configuration for the file. + * @returns {boolean} `true` if the file is a target. + */ + isTargetPath(filePath, providedConfig) { + const { + configArrayFactory, + extensionRegExp + } = internalSlotsMap.get(this); + + // If `--ext` option is present, use it. + if (extensionRegExp) { + return extensionRegExp.test(filePath); + } + + // `.js` file is target by default. + if (filePath.endsWith(".js")) { + return true; + } + + // use `overrides[].files` to check additional targets. + const config = + providedConfig || + configArrayFactory.getConfigArrayForFile( + filePath, + { ignoreNotFoundError: true } + ); + + return config.isAdditionalTargetPath(filePath); + } + + /** + * Iterate files which are matched by given glob patterns. + * @param {string|string[]} patternOrPatterns The glob patterns to iterate files. + * @throws {NoFilesFoundError|AllFilesIgnoredError} On an unmatched pattern. + * @returns {IterableIterator} The found files. + */ + *iterateFiles(patternOrPatterns) { + const { globInputPaths, errorOnUnmatchedPattern } = internalSlotsMap.get(this); + const patterns = Array.isArray(patternOrPatterns) + ? patternOrPatterns + : [patternOrPatterns]; + + debug("Start to iterate files: %o", patterns); + + // The set of paths to remove duplicate. + const set = new Set(); + + for (const pattern of patterns) { + let foundRegardlessOfIgnored = false; + let found = false; + + // Skip empty string. + if (!pattern) { + continue; + } + + // Iterate files of this pattern. + for (const { config, filePath, flag } of this._iterateFiles(pattern)) { + foundRegardlessOfIgnored = true; + if (flag === IGNORED_SILENTLY) { + continue; + } + found = true; + + // Remove duplicate paths while yielding paths. + if (!set.has(filePath)) { + set.add(filePath); + yield { + config, + filePath, + ignored: flag === IGNORED + }; + } + } + + // Raise an error if any files were not found. + if (errorOnUnmatchedPattern) { + if (!foundRegardlessOfIgnored) { + throw new NoFilesFoundError( + pattern, + !globInputPaths && isGlob(pattern) + ); + } + if (!found) { + throw new AllFilesIgnoredError(pattern); + } + } + } + + debug(`Complete iterating files: ${JSON.stringify(patterns)}`); + } + + /** + * Iterate files which are matched by a given glob pattern. + * @param {string} pattern The glob pattern to iterate files. + * @returns {IterableIterator} The found files. + */ + _iterateFiles(pattern) { + const { cwd, globInputPaths } = internalSlotsMap.get(this); + const absolutePath = path.resolve(cwd, pattern); + const isDot = dotfilesPattern.test(pattern); + const stat = statSafeSync(absolutePath); + + if (stat && stat.isDirectory()) { + return this._iterateFilesWithDirectory(absolutePath, isDot); + } + if (stat && stat.isFile()) { + return this._iterateFilesWithFile(absolutePath); + } + if (globInputPaths && isGlobPattern(pattern)) { + return this._iterateFilesWithGlob(pattern, isDot); + } + + return []; + } + + /** + * Iterate a file which is matched by a given path. + * @param {string} filePath The path to the target file. + * @returns {IterableIterator} The found files. + * @private + */ + _iterateFilesWithFile(filePath) { + debug(`File: ${filePath}`); + + const { configArrayFactory } = internalSlotsMap.get(this); + const config = configArrayFactory.getConfigArrayForFile(filePath); + const ignored = this._isIgnoredFile(filePath, { config, direct: true }); + const flag = ignored ? IGNORED : NONE; + + return [{ config, filePath, flag }]; + } + + /** + * Iterate files in a given path. + * @param {string} directoryPath The path to the target directory. + * @param {boolean} dotfiles If `true` then it doesn't skip dot files by default. + * @returns {IterableIterator} The found files. + * @private + */ + _iterateFilesWithDirectory(directoryPath, dotfiles) { + debug(`Directory: ${directoryPath}`); + + return this._iterateFilesRecursive( + directoryPath, + { dotfiles, recursive: true, selector: null } + ); + } + + /** + * Iterate files which are matched by a given glob pattern. + * @param {string} pattern The glob pattern to iterate files. + * @param {boolean} dotfiles If `true` then it doesn't skip dot files by default. + * @returns {IterableIterator} The found files. + * @private + */ + _iterateFilesWithGlob(pattern, dotfiles) { + debug(`Glob: ${pattern}`); + + const { cwd } = internalSlotsMap.get(this); + const directoryPath = path.resolve(cwd, getGlobParent(pattern)); + const absolutePath = path.resolve(cwd, pattern); + const globPart = absolutePath.slice(directoryPath.length + 1); + + /* + * recursive if there are `**` or path separators in the glob part. + * Otherwise, patterns such as `src/*.js`, it doesn't need recursive. + */ + const recursive = /\*\*|\/|\\/u.test(globPart); + const selector = new Minimatch(absolutePath, minimatchOpts); + + debug(`recursive? ${recursive}`); + + return this._iterateFilesRecursive( + directoryPath, + { dotfiles, recursive, selector } + ); + } + + /** + * Iterate files in a given path. + * @param {string} directoryPath The path to the target directory. + * @param {Object} options The options to iterate files. + * @param {boolean} [options.dotfiles] If `true` then it doesn't skip dot files by default. + * @param {boolean} [options.recursive] If `true` then it dives into sub directories. + * @param {InstanceType} [options.selector] The matcher to choose files. + * @returns {IterableIterator} The found files. + * @private + */ + *_iterateFilesRecursive(directoryPath, options) { + debug(`Enter the directory: ${directoryPath}`); + const { configArrayFactory } = internalSlotsMap.get(this); + + /** @type {ConfigArray|null} */ + let config = null; + + // Enumerate the files of this directory. + for (const entry of readdirSafeSync(directoryPath)) { + const filePath = path.join(directoryPath, entry.name); + const fileInfo = entry.isSymbolicLink() ? statSafeSync(filePath) : entry; + + if (!fileInfo) { + continue; + } + + // Check if the file is matched. + if (fileInfo.isFile()) { + if (!config) { + config = configArrayFactory.getConfigArrayForFile( + filePath, + + /* + * We must ignore `ConfigurationNotFoundError` at this + * point because we don't know if target files exist in + * this directory. + */ + { ignoreNotFoundError: true } + ); + } + const matched = options.selector + + // Started with a glob pattern; choose by the pattern. + ? options.selector.match(filePath) + + // Started with a directory path; choose by file extensions. + : this.isTargetPath(filePath, config); + + if (matched) { + const ignored = this._isIgnoredFile(filePath, { ...options, config }); + const flag = ignored ? IGNORED_SILENTLY : NONE; + + debug(`Yield: ${entry.name}${ignored ? " but ignored" : ""}`); + yield { + config: configArrayFactory.getConfigArrayForFile(filePath), + filePath, + flag + }; + } else { + debug(`Didn't match: ${entry.name}`); + } + + // Dive into the sub directory. + } else if (options.recursive && fileInfo.isDirectory()) { + if (!config) { + config = configArrayFactory.getConfigArrayForFile( + filePath, + { ignoreNotFoundError: true } + ); + } + const ignored = this._isIgnoredFile( + filePath + path.sep, + { ...options, config } + ); + + if (!ignored) { + yield* this._iterateFilesRecursive(filePath, options); + } + } + } + + debug(`Leave the directory: ${directoryPath}`); + } + + /** + * Check if a given file should be ignored. + * @param {string} filePath The path to a file to check. + * @param {Object} options Options + * @param {ConfigArray} [options.config] The config for this file. + * @param {boolean} [options.dotfiles] If `true` then this is not ignore dot files by default. + * @param {boolean} [options.direct] If `true` then this is a direct specified file. + * @returns {boolean} `true` if the file should be ignored. + * @private + */ + _isIgnoredFile(filePath, { + config: providedConfig, + dotfiles = false, + direct = false + }) { + const { + configArrayFactory, + defaultIgnores, + ignoreFlag + } = internalSlotsMap.get(this); + + if (ignoreFlag) { + const config = + providedConfig || + configArrayFactory.getConfigArrayForFile( + filePath, + { ignoreNotFoundError: true } + ); + const ignores = + config.extractConfig(filePath).ignores || defaultIgnores; + + return ignores(filePath, dotfiles); + } + + return !direct && defaultIgnores(filePath, dotfiles); + } } //------------------------------------------------------------------------------ diff --git a/node_modules/eslint/lib/cli-engine/formatters/formatters-meta.json b/node_modules/eslint/lib/cli-engine/formatters/formatters-meta.json index cf3205291..bcd35e504 100644 --- a/node_modules/eslint/lib/cli-engine/formatters/formatters-meta.json +++ b/node_modules/eslint/lib/cli-engine/formatters/formatters-meta.json @@ -1,18 +1,46 @@ [ - { - "name": "html", - "description": "Outputs results to HTML. The `html` formatter is useful for visual presentation in the browser." - }, - { - "name": "json-with-metadata", - "description": "Outputs JSON-serialized results. The `json-with-metadata` provides the same linting results as the [`json`](#json) formatter with additional metadata about the rules applied. The linting results are included in the `results` property and the rules metadata is included in the `metadata` property.\n\nAlternatively, you can use the [ESLint Node.js API](../../integrate/nodejs-api) to programmatically use ESLint." - }, - { - "name": "json", - "description": "Outputs JSON-serialized results. The `json` formatter is useful when you want to programmatically work with the CLI's linting results.\n\nAlternatively, you can use the [ESLint Node.js API](../../integrate/nodejs-api) to programmatically use ESLint." - }, - { - "name": "stylish", - "description": "Human-readable output format. This is the default formatter." - } -] + { + "name": "checkstyle", + "description": "Outputs results to the [Checkstyle](https://checkstyle.sourceforge.io/) format." + }, + { + "name": "compact", + "description": "Human-readable output format. Mimics the default output of JSHint." + }, + { + "name": "html", + "description": "Outputs results to HTML. The `html` formatter is useful for visual presentation in the browser." + }, + { + "name": "jslint-xml", + "description": "Outputs results to format compatible with the [JSLint Jenkins plugin](https://plugins.jenkins.io/jslint/)." + }, + { + "name": "json-with-metadata", + "description": "Outputs JSON-serialized results. The `json-with-metadata` provides the same linting results as the [`json`](#json) formatter with additional metadata about the rules applied. The linting results are included in the `results` property and the rules metadata is included in the `metadata` property.\n\nAlternatively, you can use the [ESLint Node.js API](../../integrate/nodejs-api) to programmatically use ESLint." + }, + { + "name": "json", + "description": "Outputs JSON-serialized results. The `json` formatter is useful when you want to programmatically work with the CLI's linting results.\n\nAlternatively, you can use the [ESLint Node.js API](../../integrate/nodejs-api) to programmatically use ESLint." + }, + { + "name": "junit", + "description": "Outputs results to format compatible with the [JUnit Jenkins plugin](https://plugins.jenkins.io/junit/)." + }, + { + "name": "stylish", + "description": "Human-readable output format. This is the default formatter." + }, + { + "name": "tap", + "description": "Outputs results to the [Test Anything Protocol (TAP)](https://testanything.org/) specification format." + }, + { + "name": "unix", + "description": "Outputs results to a format similar to many commands in UNIX-like systems. Parsable with tools such as [grep](https://www.gnu.org/software/grep/manual/grep.html), [sed](https://www.gnu.org/software/sed/manual/sed.html), and [awk](https://www.gnu.org/software/gawk/manual/gawk.html)." + }, + { + "name": "visualstudio", + "description": "Outputs results to format compatible with the integrated terminal of the [Visual Studio](https://visualstudio.microsoft.com/) IDE. When using Visual Studio, you can click on the linting results in the integrated terminal to go to the issue in the source code." + } +] \ No newline at end of file diff --git a/node_modules/eslint/lib/cli-engine/formatters/html.js b/node_modules/eslint/lib/cli-engine/formatters/html.js index ef8d445ab..1aa66fcef 100644 --- a/node_modules/eslint/lib/cli-engine/formatters/html.js +++ b/node_modules/eslint/lib/cli-engine/formatters/html.js @@ -8,22 +8,22 @@ // Helpers //------------------------------------------------------------------------------ -const encodeHTML = (function () { - const encodeHTMLRules = { - "&": "&", - "<": "<", - ">": ">", - '"': """, - "'": "'", - }; - const matchHTML = /[&<>"']/gu; - - return function (code) { - return code - ? code.toString().replace(matchHTML, m => encodeHTMLRules[m] || m) - : ""; - }; -})(); +const encodeHTML = (function() { + const encodeHTMLRules = { + "&": "&", + "<": "<", + ">": ">", + '"': """, + "'": "'" + }; + const matchHTML = /[&<>"']/ug; + + return function(code) { + return code + ? code.toString().replace(matchHTML, m => encodeHTMLRules[m] || m) + : ""; + }; +}()); /** * Get the final HTML document. @@ -31,9 +31,9 @@ const encodeHTML = (function () { * @returns {string} HTML document. */ function pageTemplate(it) { - const { reportColor, reportSummary, date, results } = it; + const { reportColor, reportSummary, date, results } = it; - return ` + return ` @@ -182,11 +182,11 @@ function pageTemplate(it) { /** * Given a word and a count, append an s if count is not one. * @param {string} word A word in its singular form. - * @param {number} count A number controlling whether word should be pluralized. + * @param {int} count A number controlling whether word should be pluralized. * @returns {string} The original word with an s on the end if count is not one. */ function pluralize(word, count) { - return count === 1 ? word : `${word}s`; + return (count === 1 ? word : `${word}s`); } /** @@ -196,29 +196,29 @@ function pluralize(word, count) { * @returns {string} The formatted string, pluralized where necessary */ function renderSummary(totalErrors, totalWarnings) { - const totalProblems = totalErrors + totalWarnings; - let renderedText = `${totalProblems} ${pluralize("problem", totalProblems)}`; + const totalProblems = totalErrors + totalWarnings; + let renderedText = `${totalProblems} ${pluralize("problem", totalProblems)}`; - if (totalProblems !== 0) { - renderedText += ` (${totalErrors} ${pluralize("error", totalErrors)}, ${totalWarnings} ${pluralize("warning", totalWarnings)})`; - } - return renderedText; + if (totalProblems !== 0) { + renderedText += ` (${totalErrors} ${pluralize("error", totalErrors)}, ${totalWarnings} ${pluralize("warning", totalWarnings)})`; + } + return renderedText; } /** * Get the color based on whether there are errors/warnings... * @param {string} totalErrors Total errors * @param {string} totalWarnings Total warnings - * @returns {number} The color code (0 = green, 1 = yellow, 2 = red) + * @returns {int} The color code (0 = green, 1 = yellow, 2 = red) */ function renderColor(totalErrors, totalWarnings) { - if (totalErrors !== 0) { - return 2; - } - if (totalWarnings !== 0) { - return 1; - } - return 0; + if (totalErrors !== 0) { + return 2; + } + if (totalWarnings !== 0) { + return 1; + } + return 0; } /** @@ -227,18 +227,18 @@ function renderColor(totalErrors, totalWarnings) { * @returns {string} HTML (table row) describing the message. */ function messageTemplate(it) { - const { - parentIndex, - lineNumber, - columnNumber, - severityNumber, - severityName, - message, - ruleUrl, - ruleId, - } = it; - - return ` + const { + parentIndex, + lineNumber, + columnNumber, + severityNumber, + severityName, + message, + ruleUrl, + ruleId + } = it; + + return ` ${lineNumber}:${columnNumber} ${severityName} @@ -253,42 +253,41 @@ function messageTemplate(it) { /** * Get HTML (table rows) describing the messages. * @param {Array} messages Messages. - * @param {number} parentIndex Index of the parent HTML row. + * @param {int} parentIndex Index of the parent HTML row. * @param {Object} rulesMeta Dictionary containing metadata for each rule executed by the analysis. * @returns {string} HTML (table rows) describing the messages. */ function renderMessages(messages, parentIndex, rulesMeta) { - /** - * Get HTML (table row) describing a message. - * @param {Object} message Message. - * @returns {string} HTML (table row) describing a message. - */ - return messages - .map(message => { - const lineNumber = message.line || 0; - const columnNumber = message.column || 0; - let ruleUrl; - - if (rulesMeta) { - const meta = rulesMeta[message.ruleId]; - - if (meta && meta.docs && meta.docs.url) { - ruleUrl = meta.docs.url; - } - } - - return messageTemplate({ - parentIndex, - lineNumber, - columnNumber, - severityNumber: message.severity, - severityName: message.severity === 1 ? "Warning" : "Error", - message: message.message, - ruleId: message.ruleId, - ruleUrl, - }); - }) - .join("\n"); + + /** + * Get HTML (table row) describing a message. + * @param {Object} message Message. + * @returns {string} HTML (table row) describing a message. + */ + return messages.map(message => { + const lineNumber = message.line || 0; + const columnNumber = message.column || 0; + let ruleUrl; + + if (rulesMeta) { + const meta = rulesMeta[message.ruleId]; + + if (meta && meta.docs && meta.docs.url) { + ruleUrl = meta.docs.url; + } + } + + return messageTemplate({ + parentIndex, + lineNumber, + columnNumber, + severityNumber: message.severity, + severityName: message.severity === 1 ? "Warning" : "Error", + message: message.message, + ruleId: message.ruleId, + ruleUrl + }); + }).join("\n"); } /** @@ -297,9 +296,9 @@ function renderMessages(messages, parentIndex, rulesMeta) { * @returns {string} HTML (table row) describing the result for the file. */ function resultTemplate(it) { - const { color, index, filePath, summary } = it; + const { color, index, filePath, summary } = it; - return ` + return ` [+] ${encodeHTML(filePath)} @@ -316,44 +315,37 @@ function resultTemplate(it) { * @returns {string} HTML string describing the results. */ function renderResults(results, rulesMeta) { - return results - .map( - (result, index) => - resultTemplate({ - index, - color: renderColor(result.errorCount, result.warningCount), - filePath: result.filePath, - summary: renderSummary( - result.errorCount, - result.warningCount, - ), - }) + renderMessages(result.messages, index, rulesMeta), - ) - .join("\n"); + return results.map((result, index) => resultTemplate({ + index, + color: renderColor(result.errorCount, result.warningCount), + filePath: result.filePath, + summary: renderSummary(result.errorCount, result.warningCount) + }) + renderMessages(result.messages, index, rulesMeta)).join("\n"); } //------------------------------------------------------------------------------ // Public Interface //------------------------------------------------------------------------------ -module.exports = function (results, data) { - let totalErrors, totalWarnings; +module.exports = function(results, data) { + let totalErrors, + totalWarnings; - const metaData = data ? data.rulesMeta : {}; + const metaData = data ? data.rulesMeta : {}; - totalErrors = 0; - totalWarnings = 0; + totalErrors = 0; + totalWarnings = 0; - // Iterate over results to get totals - results.forEach(result => { - totalErrors += result.errorCount; - totalWarnings += result.warningCount; - }); + // Iterate over results to get totals + results.forEach(result => { + totalErrors += result.errorCount; + totalWarnings += result.warningCount; + }); - return pageTemplate({ - date: new Date(), - reportColor: renderColor(totalErrors, totalWarnings), - reportSummary: renderSummary(totalErrors, totalWarnings), - results: renderResults(results, metaData), - }); + return pageTemplate({ + date: new Date(), + reportColor: renderColor(totalErrors, totalWarnings), + reportSummary: renderSummary(totalErrors, totalWarnings), + results: renderResults(results, metaData) + }); }; diff --git a/node_modules/eslint/lib/cli-engine/formatters/json-with-metadata.js b/node_modules/eslint/lib/cli-engine/formatters/json-with-metadata.js index 44c785c74..689947154 100644 --- a/node_modules/eslint/lib/cli-engine/formatters/json-with-metadata.js +++ b/node_modules/eslint/lib/cli-engine/formatters/json-with-metadata.js @@ -8,9 +8,9 @@ // Public Interface //------------------------------------------------------------------------------ -module.exports = function (results, data) { - return JSON.stringify({ - results, - metadata: data, - }); +module.exports = function(results, data) { + return JSON.stringify({ + results, + metadata: data + }); }; diff --git a/node_modules/eslint/lib/cli-engine/formatters/json.js b/node_modules/eslint/lib/cli-engine/formatters/json.js index 230f5c587..82138af18 100644 --- a/node_modules/eslint/lib/cli-engine/formatters/json.js +++ b/node_modules/eslint/lib/cli-engine/formatters/json.js @@ -8,6 +8,6 @@ // Public Interface //------------------------------------------------------------------------------ -module.exports = function (results) { - return JSON.stringify(results); +module.exports = function(results) { + return JSON.stringify(results); }; diff --git a/node_modules/eslint/lib/cli-engine/formatters/stylish.js b/node_modules/eslint/lib/cli-engine/formatters/stylish.js index 18aa58154..a808448b6 100644 --- a/node_modules/eslint/lib/cli-engine/formatters/stylish.js +++ b/node_modules/eslint/lib/cli-engine/formatters/stylish.js @@ -5,8 +5,8 @@ "use strict"; const chalk = require("chalk"), - util = require("node:util"), - table = require("../../shared/text-table"); + stripAnsi = require("strip-ansi"), + table = require("text-table"); //------------------------------------------------------------------------------ // Helpers @@ -15,108 +15,87 @@ const chalk = require("chalk"), /** * Given a word and a count, append an s if count is not one. * @param {string} word A word in its singular form. - * @param {number} count A number controlling whether word should be pluralized. + * @param {int} count A number controlling whether word should be pluralized. * @returns {string} The original word with an s on the end if count is not one. */ function pluralize(word, count) { - return count === 1 ? word : `${word}s`; + return (count === 1 ? word : `${word}s`); } //------------------------------------------------------------------------------ // Public Interface //------------------------------------------------------------------------------ -module.exports = function (results) { - let output = "\n", - errorCount = 0, - warningCount = 0, - fixableErrorCount = 0, - fixableWarningCount = 0, - summaryColor = "yellow"; - - results.forEach(result => { - const messages = result.messages; - - if (messages.length === 0) { - return; - } - - errorCount += result.errorCount; - warningCount += result.warningCount; - fixableErrorCount += result.fixableErrorCount; - fixableWarningCount += result.fixableWarningCount; - - output += `${chalk.underline(result.filePath)}\n`; - - output += `${table( - messages.map(message => { - let messageType; - - if (message.fatal || message.severity === 2) { - messageType = chalk.red("error"); - summaryColor = "red"; - } else { - messageType = chalk.yellow("warning"); - } - - return [ - "", - String(message.line || 0), - String(message.column || 0), - messageType, - message.message.replace(/([^ ])\.$/u, "$1"), - chalk.dim(message.ruleId || ""), - ]; - }), - { - align: ["", "r", "l"], - stringLength(str) { - return util.stripVTControlCharacters(str).length; - }, - }, - ) - .split("\n") - .map(el => - el.replace(/(\d+)\s+(\d+)/u, (m, p1, p2) => - chalk.dim(`${p1}:${p2}`), - ), - ) - .join("\n")}\n\n`; - }); - - const total = errorCount + warningCount; - - if (total > 0) { - output += chalk[summaryColor].bold( - [ - "\u2716 ", - total, - pluralize(" problem", total), - " (", - errorCount, - pluralize(" error", errorCount), - ", ", - warningCount, - pluralize(" warning", warningCount), - ")\n", - ].join(""), - ); - - if (fixableErrorCount > 0 || fixableWarningCount > 0) { - output += chalk[summaryColor].bold( - [ - " ", - fixableErrorCount, - pluralize(" error", fixableErrorCount), - " and ", - fixableWarningCount, - pluralize(" warning", fixableWarningCount), - " potentially fixable with the `--fix` option.\n", - ].join(""), - ); - } - } - - // Resets output color, for prevent change on top level - return total > 0 ? chalk.reset(output) : ""; +module.exports = function(results) { + + let output = "\n", + errorCount = 0, + warningCount = 0, + fixableErrorCount = 0, + fixableWarningCount = 0, + summaryColor = "yellow"; + + results.forEach(result => { + const messages = result.messages; + + if (messages.length === 0) { + return; + } + + errorCount += result.errorCount; + warningCount += result.warningCount; + fixableErrorCount += result.fixableErrorCount; + fixableWarningCount += result.fixableWarningCount; + + output += `${chalk.underline(result.filePath)}\n`; + + output += `${table( + messages.map(message => { + let messageType; + + if (message.fatal || message.severity === 2) { + messageType = chalk.red("error"); + summaryColor = "red"; + } else { + messageType = chalk.yellow("warning"); + } + + return [ + "", + message.line || 0, + message.column || 0, + messageType, + message.message.replace(/([^ ])\.$/u, "$1"), + chalk.dim(message.ruleId || "") + ]; + }), + { + align: ["", "r", "l"], + stringLength(str) { + return stripAnsi(str).length; + } + } + ).split("\n").map(el => el.replace(/(\d+)\s+(\d+)/u, (m, p1, p2) => chalk.dim(`${p1}:${p2}`))).join("\n")}\n\n`; + }); + + const total = errorCount + warningCount; + + if (total > 0) { + output += chalk[summaryColor].bold([ + "\u2716 ", total, pluralize(" problem", total), + " (", errorCount, pluralize(" error", errorCount), ", ", + warningCount, pluralize(" warning", warningCount), ")\n" + ].join("")); + + if (fixableErrorCount > 0 || fixableWarningCount > 0) { + output += chalk[summaryColor].bold([ + " ", fixableErrorCount, pluralize(" error", fixableErrorCount), " and ", + fixableWarningCount, pluralize(" warning", fixableWarningCount), + " potentially fixable with the `--fix` option.\n" + ].join("")); + } + } + + // Resets output color, for prevent change on top level + return total > 0 ? chalk.reset(output) : ""; }; diff --git a/node_modules/eslint/lib/cli-engine/hash.js b/node_modules/eslint/lib/cli-engine/hash.js index 4c306b9b4..8e467734a 100644 --- a/node_modules/eslint/lib/cli-engine/hash.js +++ b/node_modules/eslint/lib/cli-engine/hash.js @@ -25,7 +25,7 @@ const murmur = require("imurmurhash"); * @returns {string} the hash */ function hash(str) { - return murmur(str).result().toString(36); + return murmur(str).result().toString(36); } //------------------------------------------------------------------------------ diff --git a/node_modules/eslint/lib/cli-engine/index.js b/node_modules/eslint/lib/cli-engine/index.js index bba3fb0b8..52e45a6d7 100644 --- a/node_modules/eslint/lib/cli-engine/index.js +++ b/node_modules/eslint/lib/cli-engine/index.js @@ -3,5 +3,5 @@ const { CLIEngine } = require("./cli-engine"); module.exports = { - CLIEngine, + CLIEngine }; diff --git a/node_modules/eslint/lib/cli-engine/lint-result-cache.js b/node_modules/eslint/lib/cli-engine/lint-result-cache.js index 48f42e19b..97d2ee40b 100644 --- a/node_modules/eslint/lib/cli-engine/lint-result-cache.js +++ b/node_modules/eslint/lib/cli-engine/lint-result-cache.js @@ -8,21 +8,15 @@ // Requirements //----------------------------------------------------------------------------- -const fs = require("node:fs"); +const assert = require("assert"); +const fs = require("fs"); const fileEntryCache = require("file-entry-cache"); const stringify = require("json-stable-stringify-without-jsonify"); const pkg = require("../../package.json"); -const assert = require("../shared/assert"); const hash = require("./hash"); const debug = require("debug")("eslint:lint-result-cache"); -//------------------------------------------------------------------------------ -// Typedefs -//------------------------------------------------------------------------------ - -/** @typedef {import("../types").Linter.Config} Config */ - //----------------------------------------------------------------------------- // Helpers //----------------------------------------------------------------------------- @@ -32,8 +26,8 @@ const nodeVersion = process && process.version; const validCacheStrategies = ["metadata", "content"]; const invalidCacheStrategyErrorMessage = `Cache strategy must be one of: ${validCacheStrategies - .map(strategy => `"${strategy}"`) - .join(", ")}`; + .map(strategy => `"${strategy}"`) + .join(", ")}`; /** * Tests whether a provided cacheStrategy is valid @@ -41,23 +35,22 @@ const invalidCacheStrategyErrorMessage = `Cache strategy must be one of: ${valid * @returns {boolean} true if `cacheStrategy` is one of `validCacheStrategies`; false otherwise */ function isValidCacheStrategy(cacheStrategy) { - return validCacheStrategies.includes(cacheStrategy); + return ( + validCacheStrategies.includes(cacheStrategy) + ); } /** * Calculates the hash of the config - * @param {Config} config The config. + * @param {ConfigArray} config The config. * @returns {string} The hash of the config */ function hashOfConfigFor(config) { - if (!configHashCache.has(config)) { - configHashCache.set( - config, - hash(`${pkg.version}_${nodeVersion}_${stringify(config)}`), - ); - } - - return configHashCache.get(config); + if (!configHashCache.has(config)) { + configHashCache.set(config, hash(`${pkg.version}_${nodeVersion}_${stringify(config)}`)); + } + + return configHashCache.get(config); } //----------------------------------------------------------------------------- @@ -70,151 +63,141 @@ function hashOfConfigFor(config) { * serialize and adding them back in on retrieval. */ class LintResultCache { - /** - * Creates a new LintResultCache instance. - * @param {string} cacheFileLocation The cache file location. - * @param {"metadata" | "content"} cacheStrategy The cache strategy to use. - */ - constructor(cacheFileLocation, cacheStrategy) { - assert(cacheFileLocation, "Cache file location is required"); - assert(cacheStrategy, "Cache strategy is required"); - assert( - isValidCacheStrategy(cacheStrategy), - invalidCacheStrategyErrorMessage, - ); - - debug(`Caching results to ${cacheFileLocation}`); - - const useChecksum = cacheStrategy === "content"; - - debug(`Using "${cacheStrategy}" strategy to detect changes`); - - this.fileEntryCache = fileEntryCache.create( - cacheFileLocation, - void 0, - useChecksum, - ); - this.cacheFileLocation = cacheFileLocation; - } - - /** - * Retrieve cached lint results for a given file path, if present in the - * cache. If the file is present and has not been changed, rebuild any - * missing result information. - * @param {string} filePath The file for which to retrieve lint results. - * @param {Config} config The config of the file. - * @returns {Object|null} The rebuilt lint results, or null if the file is - * changed or not in the filesystem. - */ - getCachedLintResults(filePath, config) { - const cachedResults = this.getValidCachedLintResults(filePath, config); - - if (!cachedResults) { - return cachedResults; - } - - /* - * Shallow clone the object to ensure that any properties added or modified afterwards - * will not be accidentally stored in the cache file when `reconcile()` is called. - * https://github.com/eslint/eslint/issues/13507 - * All intentional changes to the cache file must be done through `setCachedLintResults()`. - */ - const results = { ...cachedResults }; - - // If source is present but null, need to reread the file from the filesystem. - if (results.source === null) { - debug( - `Rereading cached result source from filesystem: ${filePath}`, - ); - results.source = fs.readFileSync(filePath, "utf-8"); - } - - return results; - } - - /** - * Retrieve cached lint results for a given file path, if present in the - * cache and still valid. - * @param {string} filePath The file for which to retrieve lint results. - * @param {Config} config The config of the file. - * @returns {Object|null} The cached lint results if present in the cache - * and still valid; null otherwise. - */ - getValidCachedLintResults(filePath, config) { - /* - * Cached lint results are valid if and only if: - * 1. The file is present in the filesystem - * 2. The file has not changed since the time it was previously linted - * 3. The ESLint configuration has not changed since the time the file - * was previously linted - * If any of these are not true, we will not reuse the lint results. - */ - const fileDescriptor = this.fileEntryCache.getFileDescriptor(filePath); - - if (fileDescriptor.notFound) { - debug(`File not found on the file system: ${filePath}`); - return null; - } - - const hashOfConfig = hashOfConfigFor(config); - const changed = - fileDescriptor.changed || - fileDescriptor.meta.hashOfConfig !== hashOfConfig; - - if (changed) { - debug(`Cache entry not found or no longer valid: ${filePath}`); - return null; - } - - return fileDescriptor.meta.results; - } - - /** - * Set the cached lint results for a given file path, after removing any - * information that will be both unnecessary and difficult to serialize. - * Avoids caching results with an "output" property (meaning fixes were - * applied), to prevent potentially incorrect results if fixes are not - * written to disk. - * @param {string} filePath The file for which to set lint results. - * @param {Config} config The config of the file. - * @param {Object} result The lint result to be set for the file. - * @returns {void} - */ - setCachedLintResults(filePath, config, result) { - if (result && Object.hasOwn(result, "output")) { - return; - } - - const fileDescriptor = this.fileEntryCache.getFileDescriptor(filePath); - - if (fileDescriptor && !fileDescriptor.notFound) { - debug(`Updating cached result: ${filePath}`); - - // Serialize the result, except that we want to remove the file source if present. - const resultToSerialize = Object.assign({}, result); - - /* - * Set result.source to null. - * In `getCachedLintResults`, if source is explicitly null, we will - * read the file from the filesystem to set the value again. - */ - if (Object.hasOwn(resultToSerialize, "source")) { - resultToSerialize.source = null; - } - - fileDescriptor.meta.results = resultToSerialize; - fileDescriptor.meta.hashOfConfig = hashOfConfigFor(config); - } - } - - /** - * Persists the in-memory cache to disk. - * @returns {void} - */ - reconcile() { - debug(`Persisting cached results: ${this.cacheFileLocation}`); - this.fileEntryCache.reconcile(); - } + + /** + * Creates a new LintResultCache instance. + * @param {string} cacheFileLocation The cache file location. + * @param {"metadata" | "content"} cacheStrategy The cache strategy to use. + */ + constructor(cacheFileLocation, cacheStrategy) { + assert(cacheFileLocation, "Cache file location is required"); + assert(cacheStrategy, "Cache strategy is required"); + assert( + isValidCacheStrategy(cacheStrategy), + invalidCacheStrategyErrorMessage + ); + + debug(`Caching results to ${cacheFileLocation}`); + + const useChecksum = cacheStrategy === "content"; + + debug( + `Using "${cacheStrategy}" strategy to detect changes` + ); + + this.fileEntryCache = fileEntryCache.create( + cacheFileLocation, + void 0, + useChecksum + ); + this.cacheFileLocation = cacheFileLocation; + } + + /** + * Retrieve cached lint results for a given file path, if present in the + * cache. If the file is present and has not been changed, rebuild any + * missing result information. + * @param {string} filePath The file for which to retrieve lint results. + * @param {ConfigArray} config The config of the file. + * @returns {Object|null} The rebuilt lint results, or null if the file is + * changed or not in the filesystem. + */ + getCachedLintResults(filePath, config) { + + /* + * Cached lint results are valid if and only if: + * 1. The file is present in the filesystem + * 2. The file has not changed since the time it was previously linted + * 3. The ESLint configuration has not changed since the time the file + * was previously linted + * If any of these are not true, we will not reuse the lint results. + */ + const fileDescriptor = this.fileEntryCache.getFileDescriptor(filePath); + const hashOfConfig = hashOfConfigFor(config); + const changed = + fileDescriptor.changed || + fileDescriptor.meta.hashOfConfig !== hashOfConfig; + + if (fileDescriptor.notFound) { + debug(`File not found on the file system: ${filePath}`); + return null; + } + + if (changed) { + debug(`Cache entry not found or no longer valid: ${filePath}`); + return null; + } + + const cachedResults = fileDescriptor.meta.results; + + // Just in case, not sure if this can ever happen. + if (!cachedResults) { + return cachedResults; + } + + /* + * Shallow clone the object to ensure that any properties added or modified afterwards + * will not be accidentally stored in the cache file when `reconcile()` is called. + * https://github.com/eslint/eslint/issues/13507 + * All intentional changes to the cache file must be done through `setCachedLintResults()`. + */ + const results = { ...cachedResults }; + + // If source is present but null, need to reread the file from the filesystem. + if (results.source === null) { + debug(`Rereading cached result source from filesystem: ${filePath}`); + results.source = fs.readFileSync(filePath, "utf-8"); + } + + return results; + } + + /** + * Set the cached lint results for a given file path, after removing any + * information that will be both unnecessary and difficult to serialize. + * Avoids caching results with an "output" property (meaning fixes were + * applied), to prevent potentially incorrect results if fixes are not + * written to disk. + * @param {string} filePath The file for which to set lint results. + * @param {ConfigArray} config The config of the file. + * @param {Object} result The lint result to be set for the file. + * @returns {void} + */ + setCachedLintResults(filePath, config, result) { + if (result && Object.prototype.hasOwnProperty.call(result, "output")) { + return; + } + + const fileDescriptor = this.fileEntryCache.getFileDescriptor(filePath); + + if (fileDescriptor && !fileDescriptor.notFound) { + debug(`Updating cached result: ${filePath}`); + + // Serialize the result, except that we want to remove the file source if present. + const resultToSerialize = Object.assign({}, result); + + /* + * Set result.source to null. + * In `getCachedLintResults`, if source is explicitly null, we will + * read the file from the filesystem to set the value again. + */ + if (Object.prototype.hasOwnProperty.call(resultToSerialize, "source")) { + resultToSerialize.source = null; + } + + fileDescriptor.meta.results = resultToSerialize; + fileDescriptor.meta.hashOfConfig = hashOfConfigFor(config); + } + } + + /** + * Persists the in-memory cache to disk. + * @returns {void} + */ + reconcile() { + debug(`Persisting cached results: ${this.cacheFileLocation}`); + this.fileEntryCache.reconcile(); + } } module.exports = LintResultCache; diff --git a/node_modules/eslint/lib/cli-engine/load-rules.js b/node_modules/eslint/lib/cli-engine/load-rules.js index 5a7dc3498..81bab63fa 100644 --- a/node_modules/eslint/lib/cli-engine/load-rules.js +++ b/node_modules/eslint/lib/cli-engine/load-rules.js @@ -9,8 +9,8 @@ // Requirements //------------------------------------------------------------------------------ -const fs = require("node:fs"), - path = require("node:path"); +const fs = require("fs"), + path = require("path"); const rulesDirCache = {}; @@ -24,23 +24,23 @@ const rulesDirCache = {}; * @param {string} cwd Current working directory * @returns {Object} Loaded rule modules. */ -module.exports = function (relativeRulesDir, cwd) { - const rulesDir = path.resolve(cwd, relativeRulesDir); +module.exports = function(relativeRulesDir, cwd) { + const rulesDir = path.resolve(cwd, relativeRulesDir); - // cache will help performance as IO operation are expensive - if (rulesDirCache[rulesDir]) { - return rulesDirCache[rulesDir]; - } + // cache will help performance as IO operation are expensive + if (rulesDirCache[rulesDir]) { + return rulesDirCache[rulesDir]; + } - const rules = Object.create(null); + const rules = Object.create(null); - fs.readdirSync(rulesDir).forEach(file => { - if (path.extname(file) !== ".js") { - return; - } - rules[file.slice(0, -3)] = require(path.join(rulesDir, file)); - }); - rulesDirCache[rulesDir] = rules; + fs.readdirSync(rulesDir).forEach(file => { + if (path.extname(file) !== ".js") { + return; + } + rules[file.slice(0, -3)] = require(path.join(rulesDir, file)); + }); + rulesDirCache[rulesDir] = rules; - return rules; + return rules; }; diff --git a/node_modules/eslint/lib/cli.js b/node_modules/eslint/lib/cli.js index 4e81721a9..1d909ec1c 100644 --- a/node_modules/eslint/lib/cli.js +++ b/node_modules/eslint/lib/cli.js @@ -15,73 +15,217 @@ // Requirements //------------------------------------------------------------------------------ -const fs = require("node:fs"), - { mkdir, stat, writeFile } = require("node:fs/promises"), - path = require("node:path"), - { pathToFileURL } = require("node:url"), - { LegacyESLint } = require("./eslint"), - { - ESLint, - shouldUseFlatConfig, - locateConfigFileToUse, - } = require("./eslint/eslint"), - createCLIOptions = require("./options"), - log = require("./shared/logging"), - RuntimeInfo = require("./shared/runtime-info"), - translateOptions = require("./shared/translate-cli-options"); -const { getCacheFile } = require("./eslint/eslint-helpers"); -const { SuppressionsService } = require("./services/suppressions-service"); +const fs = require("fs"), + path = require("path"), + { promisify } = require("util"), + { ESLint } = require("./eslint"), + { FlatESLint, shouldUseFlatConfig } = require("./eslint/flat-eslint"), + createCLIOptions = require("./options"), + log = require("./shared/logging"), + RuntimeInfo = require("./shared/runtime-info"), + { normalizeSeverityToString } = require("./shared/severity"); +const { Legacy: { naming } } = require("@eslint/eslintrc"); +const { ModuleImporter } = require("@humanwhocodes/module-importer"); + const debug = require("debug")("eslint:cli"); //------------------------------------------------------------------------------ // Types //------------------------------------------------------------------------------ +/** @typedef {import("./eslint/eslint").ESLintOptions} ESLintOptions */ +/** @typedef {import("./eslint/eslint").LintMessage} LintMessage */ +/** @typedef {import("./eslint/eslint").LintResult} LintResult */ /** @typedef {import("./options").ParsedCLIOptions} ParsedCLIOptions */ -/** @typedef {import("./types").ESLint.LintResult} LintResult */ -/** @typedef {import("./types").ESLint.ResultsMeta} ResultsMeta */ +/** @typedef {import("./shared/types").ResultsMeta} ResultsMeta */ //------------------------------------------------------------------------------ // Helpers //------------------------------------------------------------------------------ +const mkdir = promisify(fs.mkdir); +const stat = promisify(fs.stat); +const writeFile = promisify(fs.writeFile); + +/** + * Predicate function for whether or not to apply fixes in quiet mode. + * If a message is a warning, do not apply a fix. + * @param {LintMessage} message The lint result. + * @returns {boolean} True if the lint message is an error (and thus should be + * autofixed), false otherwise. + */ +function quietFixPredicate(message) { + return message.severity === 2; +} + +/** + * Translates the CLI options into the options expected by the ESLint constructor. + * @param {ParsedCLIOptions} cliOptions The CLI options to translate. + * @param {"flat"|"eslintrc"} [configType="eslintrc"] The format of the + * config to generate. + * @returns {Promise} The options object for the ESLint constructor. + * @private + */ +async function translateOptions({ + cache, + cacheFile, + cacheLocation, + cacheStrategy, + config, + configLookup, + env, + errorOnUnmatchedPattern, + eslintrc, + ext, + fix, + fixDryRun, + fixType, + global, + ignore, + ignorePath, + ignorePattern, + inlineConfig, + parser, + parserOptions, + plugin, + quiet, + reportUnusedDisableDirectives, + reportUnusedDisableDirectivesSeverity, + resolvePluginsRelativeTo, + rule, + rulesdir, + warnIgnored +}, configType) { + + let overrideConfig, overrideConfigFile; + const importer = new ModuleImporter(); + + if (configType === "flat") { + overrideConfigFile = (typeof config === "string") ? config : !configLookup; + if (overrideConfigFile === false) { + overrideConfigFile = void 0; + } + + let globals = {}; + + if (global) { + globals = global.reduce((obj, name) => { + if (name.endsWith(":true")) { + obj[name.slice(0, -5)] = "writable"; + } else { + obj[name] = "readonly"; + } + return obj; + }, globals); + } + + overrideConfig = [{ + languageOptions: { + globals, + parserOptions: parserOptions || {} + }, + rules: rule ? rule : {} + }]; + + if (reportUnusedDisableDirectives || reportUnusedDisableDirectivesSeverity !== void 0) { + overrideConfig[0].linterOptions = { + reportUnusedDisableDirectives: reportUnusedDisableDirectives + ? "error" + : normalizeSeverityToString(reportUnusedDisableDirectivesSeverity) + }; + } + + if (parser) { + overrideConfig[0].languageOptions.parser = await importer.import(parser); + } + + if (plugin) { + const plugins = {}; + + for (const pluginName of plugin) { + + const shortName = naming.getShorthandName(pluginName, "eslint-plugin"); + const longName = naming.normalizePackageName(pluginName, "eslint-plugin"); + + plugins[shortName] = await importer.import(longName); + } + + overrideConfig[0].plugins = plugins; + } + + } else { + overrideConfigFile = config; + + overrideConfig = { + env: env && env.reduce((obj, name) => { + obj[name] = true; + return obj; + }, {}), + globals: global && global.reduce((obj, name) => { + if (name.endsWith(":true")) { + obj[name.slice(0, -5)] = "writable"; + } else { + obj[name] = "readonly"; + } + return obj; + }, {}), + ignorePatterns: ignorePattern, + parser, + parserOptions, + plugins: plugin, + rules: rule + }; + } + + const options = { + allowInlineConfig: inlineConfig, + cache, + cacheLocation: cacheLocation || cacheFile, + cacheStrategy, + errorOnUnmatchedPattern, + fix: (fix || fixDryRun) && (quiet ? quietFixPredicate : true), + fixTypes: fixType, + ignore, + overrideConfig, + overrideConfigFile + }; + + if (configType === "flat") { + options.ignorePatterns = ignorePattern; + options.warnIgnored = warnIgnored; + } else { + options.resolvePluginsRelativeTo = resolvePluginsRelativeTo; + options.rulePaths = rulesdir; + options.useEslintrc = eslintrc; + options.extensions = ext; + options.ignorePath = ignorePath; + if (reportUnusedDisableDirectives || reportUnusedDisableDirectivesSeverity !== void 0) { + options.reportUnusedDisableDirectives = reportUnusedDisableDirectives + ? "error" + : normalizeSeverityToString(reportUnusedDisableDirectivesSeverity); + } + } + + return options; +} + /** * Count error messages. * @param {LintResult[]} results The lint results. * @returns {{errorCount:number;fatalErrorCount:number,warningCount:number}} The number of error messages. */ function countErrors(results) { - let errorCount = 0; - let fatalErrorCount = 0; - let warningCount = 0; - - for (const result of results) { - errorCount += result.errorCount; - fatalErrorCount += result.fatalErrorCount; - warningCount += result.warningCount; - } + let errorCount = 0; + let fatalErrorCount = 0; + let warningCount = 0; - return { errorCount, fatalErrorCount, warningCount }; -} + for (const result of results) { + errorCount += result.errorCount; + fatalErrorCount += result.fatalErrorCount; + warningCount += result.warningCount; + } -/** - * Creates an options module from the provided CLI options and encodes it as a data URL. - * @param {ParsedCLIOptions} options The CLI options. - * @returns {URL} The URL of the options module. - */ -function createOptionsModule(options) { - const translateOptionsFileURL = new URL( - "./shared/translate-cli-options.js", - pathToFileURL(__filename), - ).href; - const optionsSrc = - `import translateOptions from ${JSON.stringify(translateOptionsFileURL)};\n` + - `export default await translateOptions(${JSON.stringify(options)}, "flat");\n`; - - // Base64 encoding is typically shorter than URL encoding - return new URL( - `data:text/javascript;base64,${Buffer.from(optionsSrc).toString("base64")}`, - ); + return { errorCount, fatalErrorCount, warningCount }; } /** @@ -90,14 +234,14 @@ function createOptionsModule(options) { * @returns {Promise} `true` if the given path is a directory. */ async function isDirectory(filePath) { - try { - return (await stat(filePath)).isDirectory(); - } catch (error) { - if (error.code === "ENOENT" || error.code === "ENOTDIR") { - return false; - } - throw error; - } + try { + return (await stat(filePath)).isDirectory(); + } catch (error) { + if (error.code === "ENOENT" || error.code === "ENOTDIR") { + return false; + } + throw error; + } } /** @@ -111,64 +255,39 @@ async function isDirectory(filePath) { * @private */ async function printResults(engine, results, format, outputFile, resultsMeta) { - let formatter; - - try { - formatter = await engine.loadFormatter(format); - } catch (e) { - log.error(e.message); - return false; - } - - const output = await formatter.format(results, resultsMeta); - - if (outputFile) { - const filePath = path.resolve(process.cwd(), outputFile); - - if (await isDirectory(filePath)) { - log.error( - "Cannot write to output file path, it is a directory: %s", - outputFile, - ); - return false; - } - - try { - await mkdir(path.dirname(filePath), { recursive: true }); - await writeFile(filePath, output); - } catch (ex) { - log.error("There was a problem writing the output file:\n%s", ex); - return false; - } - } else if (output) { - log.info(output); - } - - return true; -} - -/** - * Validates the `--concurrency` flag value. - * @param {string} concurrency The `--concurrency` flag value to validate. - * @returns {void} - * @throws {Error} If the `--concurrency` flag value is invalid. - */ -function validateConcurrency(concurrency) { - if ( - concurrency === void 0 || - concurrency === "auto" || - concurrency === "off" - ) { - return; - } - - const concurrencyValue = Number(concurrency); - - if (!Number.isInteger(concurrencyValue) || concurrencyValue < 1) { - throw new Error( - `Option concurrency: '${concurrency}' is not a positive integer, 'auto' or 'off'.`, - ); - } + let formatter; + + try { + formatter = await engine.loadFormatter(format); + } catch (e) { + log.error(e.message); + return false; + } + + const output = await formatter.format(results, resultsMeta); + + if (output) { + if (outputFile) { + const filePath = path.resolve(process.cwd(), outputFile); + + if (await isDirectory(filePath)) { + log.error("Cannot write to output file path, it is a directory: %s", outputFile); + return false; + } + + try { + await mkdir(path.dirname(filePath), { recursive: true }); + await writeFile(filePath, output); + } catch (ex) { + log.error("There was a problem writing the output file:\n%s", ex); + return false; + } + } else { + log.info(output); + } + } + + return true; } //------------------------------------------------------------------------------ @@ -180,374 +299,173 @@ function validateConcurrency(concurrency) { * for other Node.js programs to effectively run the CLI. */ const cli = { - /** - * Calculates the command string for the --inspect-config operation. - * @param {string} configFile The path to the config file to inspect. - * @returns {Promise} The command string to execute. - */ - async calculateInspectConfigFlags(configFile) { - // find the config file - const { configFilePath, basePath } = await locateConfigFileToUse({ - cwd: process.cwd(), - configFile, - }); - - return ["--config", configFilePath, "--basePath", basePath]; - }, - - /** - * Executes the CLI based on an array of arguments that is passed in. - * @param {string|Array|Object} args The arguments to process. - * @param {string} [text] The text to lint (used for TTY). - * @param {boolean} [allowFlatConfig=true] Whether or not to allow flat config. - * @returns {Promise} The exit code for the operation. - */ - async execute(args, text, allowFlatConfig = true) { - if (Array.isArray(args)) { - debug("CLI args: %o", args.slice(2)); - } - - /* - * Before doing anything, we need to see if we are using a - * flat config file. If so, then we need to change the way command - * line args are parsed. This is temporary, and when we fully - * switch to flat config we can remove this logic. - */ - - const usingFlatConfig = - allowFlatConfig && (await shouldUseFlatConfig()); - - debug("Using flat config?", usingFlatConfig); - - if (allowFlatConfig && !usingFlatConfig) { - const { WarningService } = require("./services/warning-service"); - new WarningService().emitESLintRCWarning(); - } - - const CLIOptions = createCLIOptions(usingFlatConfig); - - /** @type {ParsedCLIOptions} */ - let options; - - try { - options = CLIOptions.parse(args); - validateConcurrency(options.concurrency); - } catch (error) { - debug("Error parsing CLI options:", error.message); - - let errorMessage = error.message; - - if (usingFlatConfig) { - errorMessage += - "\nYou're using eslint.config.js, some command line flags are no longer available. Please see https://eslint.org/docs/latest/use/command-line-interface for details."; - } - - log.error(errorMessage); - return 2; - } - - const files = options._; - const useStdin = typeof text === "string"; - - if (options.help) { - log.info(CLIOptions.generateHelp()); - return 0; - } - if (options.version) { - log.info(RuntimeInfo.version()); - return 0; - } - if (options.envInfo) { - try { - log.info(RuntimeInfo.environment()); - return 0; - } catch (err) { - debug("Error retrieving environment info"); - log.error(err.message); - return 2; - } - } - - if (options.printConfig) { - if (files.length) { - log.error( - "The --print-config option must be used with exactly one file name.", - ); - return 2; - } - if (useStdin) { - log.error( - "The --print-config option is not available for piped-in code.", - ); - return 2; - } - - const engine = usingFlatConfig - ? new ESLint(await translateOptions(options, "flat")) - : new LegacyESLint(await translateOptions(options)); - const fileConfig = await engine.calculateConfigForFile( - options.printConfig, - ); - - log.info(JSON.stringify(fileConfig, null, " ")); - return 0; - } - - if (options.inspectConfig) { - log.info( - "You can also run this command directly using 'npx @eslint/config-inspector@latest' in the same directory as your configuration file.", - ); - - try { - const flatOptions = await translateOptions(options, "flat"); - const spawn = require("cross-spawn"); - const flags = await cli.calculateInspectConfigFlags( - flatOptions.overrideConfigFile, - ); - - spawn.sync( - "npx", - ["@eslint/config-inspector@latest", ...flags], - { encoding: "utf8", stdio: "inherit" }, - ); - } catch (error) { - log.error(error); - return 2; - } - - return 0; - } - - debug(`Running on ${useStdin ? "text" : "files"}`); - - if (options.fix && options.fixDryRun) { - log.error( - "The --fix option and the --fix-dry-run option cannot be used together.", - ); - return 2; - } - if (useStdin && options.fix) { - log.error( - "The --fix option is not available for piped-in code; use --fix-dry-run instead.", - ); - return 2; - } - if (options.fixType && !options.fix && !options.fixDryRun) { - log.error( - "The --fix-type option requires either --fix or --fix-dry-run.", - ); - return 2; - } - - if ( - options.reportUnusedDisableDirectives && - options.reportUnusedDisableDirectivesSeverity !== void 0 - ) { - log.error( - "The --report-unused-disable-directives option and the --report-unused-disable-directives-severity option cannot be used together.", - ); - return 2; - } - - if (usingFlatConfig && options.ext) { - // Passing `--ext ""` results in `options.ext` being an empty array. - if (options.ext.length === 0) { - log.error("The --ext option value cannot be empty."); - return 2; - } - - // Passing `--ext ,ts` results in an empty string at index 0. Passing `--ext ts,,tsx` results in an empty string at index 1. - const emptyStringIndex = options.ext.indexOf(""); - - if (emptyStringIndex >= 0) { - log.error( - `The --ext option arguments cannot be empty strings. Found an empty string at index ${emptyStringIndex}.`, - ); - return 2; - } - } - - if (options.suppressAll && options.suppressRule) { - log.error( - "The --suppress-all option and the --suppress-rule option cannot be used together.", - ); - return 2; - } - - if (options.suppressAll && options.pruneSuppressions) { - log.error( - "The --suppress-all option and the --prune-suppressions option cannot be used together.", - ); - return 2; - } - - if (options.suppressRule && options.pruneSuppressions) { - log.error( - "The --suppress-rule option and the --prune-suppressions option cannot be used together.", - ); - return 2; - } - - if ( - useStdin && - (options.suppressAll || - options.suppressRule || - options.pruneSuppressions) - ) { - log.error( - "The --suppress-all, --suppress-rule, and --prune-suppressions options cannot be used with piped-in code.", - ); - return 2; - } - - const ActiveESLint = usingFlatConfig ? ESLint : LegacyESLint; - - /** @type {ESLint|LegacyESLint} */ - let engine; - - if (options.concurrency && options.concurrency !== "off") { - const optionsURL = createOptionsModule(options); - engine = await ESLint.fromOptionsModule(optionsURL); - } else { - const eslintOptions = await translateOptions( - options, - usingFlatConfig ? "flat" : "eslintrc", - ); - engine = new ActiveESLint(eslintOptions); - } - let results; - - if (useStdin) { - results = await engine.lintText(text, { - filePath: options.stdinFilename, - - // flatConfig respects CLI flag and constructor warnIgnored, eslintrc forces true for backwards compatibility - warnIgnored: usingFlatConfig ? void 0 : true, - }); - } else { - results = await engine.lintFiles(files); - } - - if (options.fix) { - debug("Fix mode enabled - applying fixes"); - await ActiveESLint.outputFixes(results); - } - - let unusedSuppressions = {}; - - if (!useStdin) { - const suppressionsFileLocation = getCacheFile( - options.suppressionsLocation || "eslint-suppressions.json", - process.cwd(), - { - prefix: "suppressions_", - }, - ); - - if ( - options.suppressionsLocation && - !fs.existsSync(suppressionsFileLocation) && - !options.suppressAll && - !options.suppressRule - ) { - log.error( - "The suppressions file does not exist. Please run the command with `--suppress-all` or `--suppress-rule` to create it.", - ); - return 2; - } - - if ( - options.suppressAll || - options.suppressRule || - options.pruneSuppressions || - fs.existsSync(suppressionsFileLocation) - ) { - const suppressions = new SuppressionsService({ - filePath: suppressionsFileLocation, - cwd: process.cwd(), - }); - - if (options.suppressAll || options.suppressRule) { - await suppressions.suppress(results, options.suppressRule); - } - - if (options.pruneSuppressions) { - await suppressions.prune(results); - } - - const suppressionResults = suppressions.applySuppressions( - results, - await suppressions.load(), - ); - - results = suppressionResults.results; - unusedSuppressions = suppressionResults.unused; - } - } - - let resultsToPrint = results; - - if (options.quiet) { - debug("Quiet mode enabled - filtering out warnings"); - resultsToPrint = ActiveESLint.getErrorResults(resultsToPrint); - } - - const resultCounts = countErrors(results); - const tooManyWarnings = - options.maxWarnings >= 0 && - resultCounts.warningCount > options.maxWarnings; - const resultsMeta = tooManyWarnings - ? { - maxWarningsExceeded: { - maxWarnings: options.maxWarnings, - foundWarnings: resultCounts.warningCount, - }, - } - : {}; - - if ( - await printResults( - engine, - resultsToPrint, - options.format, - options.outputFile, - resultsMeta, - ) - ) { - // Errors and warnings from the original unfiltered results should determine the exit code - const shouldExitForFatalErrors = - options.exitOnFatalError && resultCounts.fatalErrorCount > 0; - - if (!resultCounts.errorCount && tooManyWarnings) { - log.error( - "ESLint found too many warnings (maximum: %s).", - options.maxWarnings, - ); - } - - if (!options.passOnUnprunedSuppressions) { - const unusedSuppressionsCount = - Object.keys(unusedSuppressions).length; - - if (unusedSuppressionsCount > 0) { - log.error( - "There are suppressions left that do not occur anymore. Consider re-running the command with `--prune-suppressions`.", - ); - debug(JSON.stringify(unusedSuppressions, null, 2)); - - return 2; - } - } - - if (shouldExitForFatalErrors) { - return 2; - } - - return resultCounts.errorCount || tooManyWarnings ? 1 : 0; - } - - return 2; - }, + + /** + * Executes the CLI based on an array of arguments that is passed in. + * @param {string|Array|Object} args The arguments to process. + * @param {string} [text] The text to lint (used for TTY). + * @param {boolean} [allowFlatConfig] Whether or not to allow flat config. + * @returns {Promise} The exit code for the operation. + */ + async execute(args, text, allowFlatConfig) { + if (Array.isArray(args)) { + debug("CLI args: %o", args.slice(2)); + } + + /* + * Before doing anything, we need to see if we are using a + * flat config file. If so, then we need to change the way command + * line args are parsed. This is temporary, and when we fully + * switch to flat config we can remove this logic. + */ + + const usingFlatConfig = allowFlatConfig && await shouldUseFlatConfig(); + + debug("Using flat config?", usingFlatConfig); + + const CLIOptions = createCLIOptions(usingFlatConfig); + + /** @type {ParsedCLIOptions} */ + let options; + + try { + options = CLIOptions.parse(args); + } catch (error) { + debug("Error parsing CLI options:", error.message); + + let errorMessage = error.message; + + if (usingFlatConfig) { + errorMessage += "\nYou're using eslint.config.js, some command line flags are no longer available. Please see https://eslint.org/docs/latest/use/command-line-interface for details."; + } + + log.error(errorMessage); + return 2; + } + + const files = options._; + const useStdin = typeof text === "string"; + + if (options.help) { + log.info(CLIOptions.generateHelp()); + return 0; + } + if (options.version) { + log.info(RuntimeInfo.version()); + return 0; + } + if (options.envInfo) { + try { + log.info(RuntimeInfo.environment()); + return 0; + } catch (err) { + debug("Error retrieving environment info"); + log.error(err.message); + return 2; + } + } + + if (options.printConfig) { + if (files.length) { + log.error("The --print-config option must be used with exactly one file name."); + return 2; + } + if (useStdin) { + log.error("The --print-config option is not available for piped-in code."); + return 2; + } + + const engine = usingFlatConfig + ? new FlatESLint(await translateOptions(options, "flat")) + : new ESLint(await translateOptions(options)); + const fileConfig = + await engine.calculateConfigForFile(options.printConfig); + + log.info(JSON.stringify(fileConfig, null, " ")); + return 0; + } + + debug(`Running on ${useStdin ? "text" : "files"}`); + + if (options.fix && options.fixDryRun) { + log.error("The --fix option and the --fix-dry-run option cannot be used together."); + return 2; + } + if (useStdin && options.fix) { + log.error("The --fix option is not available for piped-in code; use --fix-dry-run instead."); + return 2; + } + if (options.fixType && !options.fix && !options.fixDryRun) { + log.error("The --fix-type option requires either --fix or --fix-dry-run."); + return 2; + } + + if (options.reportUnusedDisableDirectives && options.reportUnusedDisableDirectivesSeverity !== void 0) { + log.error("The --report-unused-disable-directives option and the --report-unused-disable-directives-severity option cannot be used together."); + return 2; + } + + const ActiveESLint = usingFlatConfig ? FlatESLint : ESLint; + + const engine = new ActiveESLint(await translateOptions(options, usingFlatConfig ? "flat" : "eslintrc")); + let results; + + if (useStdin) { + results = await engine.lintText(text, { + filePath: options.stdinFilename, + + // flatConfig respects CLI flag and constructor warnIgnored, eslintrc forces true for backwards compatibility + warnIgnored: usingFlatConfig ? void 0 : true + }); + } else { + results = await engine.lintFiles(files); + } + + if (options.fix) { + debug("Fix mode enabled - applying fixes"); + await ActiveESLint.outputFixes(results); + } + + let resultsToPrint = results; + + if (options.quiet) { + debug("Quiet mode enabled - filtering out warnings"); + resultsToPrint = ActiveESLint.getErrorResults(resultsToPrint); + } + + const resultCounts = countErrors(results); + const tooManyWarnings = options.maxWarnings >= 0 && resultCounts.warningCount > options.maxWarnings; + const resultsMeta = tooManyWarnings + ? { + maxWarningsExceeded: { + maxWarnings: options.maxWarnings, + foundWarnings: resultCounts.warningCount + } + } + : {}; + + if (await printResults(engine, resultsToPrint, options.format, options.outputFile, resultsMeta)) { + + // Errors and warnings from the original unfiltered results should determine the exit code + const shouldExitForFatalErrors = + options.exitOnFatalError && resultCounts.fatalErrorCount > 0; + + if (!resultCounts.errorCount && tooManyWarnings) { + log.error( + "ESLint found too many warnings (maximum: %s).", + options.maxWarnings + ); + } + + if (shouldExitForFatalErrors) { + return 2; + } + + return (resultCounts.errorCount || tooManyWarnings) ? 1 : 0; + } + + return 2; + } }; module.exports = cli; diff --git a/node_modules/eslint/lib/config-api.js b/node_modules/eslint/lib/config-api.js deleted file mode 100644 index 5050a631c..000000000 --- a/node_modules/eslint/lib/config-api.js +++ /dev/null @@ -1,12 +0,0 @@ -/** - * @fileoverview exports for config helpers - * @author Nicholas C. Zakas - */ - -"use strict"; -const { defineConfig, globalIgnores } = require("@eslint/config-helpers"); - -module.exports = { - defineConfig, - globalIgnores, -}; diff --git a/node_modules/eslint/lib/config/config-loader.js b/node_modules/eslint/lib/config/config-loader.js deleted file mode 100644 index 80dfaeb43..000000000 --- a/node_modules/eslint/lib/config/config-loader.js +++ /dev/null @@ -1,816 +0,0 @@ -/** - * @fileoverview Utility to load config files - * @author Nicholas C. Zakas - */ - -"use strict"; - -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ - -const path = require("node:path"); -const fs = require("node:fs/promises"); -const findUp = require("find-up"); -const { pathToFileURL } = require("node:url"); -const debug = require("debug")("eslint:config-loader"); -const { FlatConfigArray } = require("./flat-config-array"); -const { WarningService } = require("../services/warning-service"); - -//----------------------------------------------------------------------------- -// Types -//----------------------------------------------------------------------------- - -/** @typedef {import("../types").Linter.Config} Config */ - -/** - * @typedef {Object} ConfigLoaderOptions - * @property {string|false|undefined} configFile The path to the config file to use. - * @property {string} cwd The current working directory. - * @property {boolean} ignoreEnabled Indicates if ignore patterns should be honored. - * @property {Config|Array} [baseConfig] The base config to use. - * @property {Array} [defaultConfigs] The default configs to use. - * @property {Array} [ignorePatterns] The ignore patterns to use. - * @property {Config|Array} [overrideConfig] The override config to use. - * @property {boolean} [hasUnstableNativeNodeJsTSConfigFlag] The flag to indicate whether the `unstable_native_nodejs_ts_config` flag is enabled. - * @property {WarningService} [warningService] The warning service to use. - */ - -//------------------------------------------------------------------------------ -// Helpers -//------------------------------------------------------------------------------ - -const FLAT_CONFIG_FILENAMES = [ - "eslint.config.js", - "eslint.config.mjs", - "eslint.config.cjs", - "eslint.config.ts", - "eslint.config.mts", - "eslint.config.cts", -]; - -const importedConfigFileModificationTime = new Map(); - -/** - * Asserts that the given file path is valid. - * @param {string} filePath The file path to check. - * @returns {void} - * @throws {Error} If `filePath` is not a non-empty string. - */ -function assertValidFilePath(filePath) { - if (!filePath || typeof filePath !== "string") { - throw new Error("'filePath' must be a non-empty string"); - } -} - -/** - * Asserts that a configuration exists. A configuration exists if any - * of the following are true: - * - `configFilePath` is defined. - * - `useConfigFile` is `false`. - * @param {string|undefined} configFilePath The path to the config file. - * @param {ConfigLoaderOptions} loaderOptions The options to use when loading configuration files. - * @returns {void} - * @throws {Error} If no configuration exists. - */ -function assertConfigurationExists(configFilePath, loaderOptions) { - const { configFile: useConfigFile } = loaderOptions; - - if (!configFilePath && useConfigFile !== false) { - const error = new Error("Could not find config file."); - - error.messageTemplate = "config-file-missing"; - throw error; - } -} - -/** - * Check if the file is a TypeScript file. - * @param {string} filePath The file path to check. - * @returns {boolean} `true` if the file is a TypeScript file, `false` if it's not. - */ -function isFileTS(filePath) { - const fileExtension = path.extname(filePath); - - return /^\.[mc]?ts$/u.test(fileExtension); -} - -/** - * Check if ESLint is running in Bun. - * @returns {boolean} `true` if the ESLint is running Bun, `false` if it's not. - */ -function isRunningInBun() { - return !!globalThis.Bun; -} - -/** - * Check if ESLint is running in Deno. - * @returns {boolean} `true` if the ESLint is running in Deno, `false` if it's not. - */ -function isRunningInDeno() { - return !!globalThis.Deno; -} - -/** - * Checks if native TypeScript support is - * enabled in the current Node.js process. - * - * This function determines if the - * {@linkcode NodeJS.ProcessFeatures.typescript | typescript} - * feature is present in the - * {@linkcode process.features} object - * and if its value is either "strip" or "transform". - * @returns {boolean} `true` if native TypeScript support is enabled, otherwise `false`. - * @since 9.24.0 - */ -function isNativeTypeScriptSupportEnabled() { - return ( - // eslint-disable-next-line n/no-unsupported-features/node-builtins -- it's still an experimental feature. - ["strip", "transform"].includes(process.features.typescript) - ); -} - -/** - * Load the TypeScript configuration file. - * @param {string} filePath The absolute file path to load. - * @param {URL} fileURL The file URL to load. - * @param {number} mtime The last modified timestamp of the file. - * @returns {Promise} The configuration loaded from the file. - * @since 9.24.0 - */ -async function loadTypeScriptConfigFileWithJiti(filePath, fileURL, mtime) { - const { createJiti, version: jitiVersion } = - // eslint-disable-next-line no-use-before-define -- `ConfigLoader.loadJiti` can be overwritten for testing - await ConfigLoader.loadJiti().catch(() => { - throw new Error( - "The 'jiti' library is required for loading TypeScript configuration files. Make sure to install it.", - ); - }); - - // `createJiti` was added in jiti v2. - if (typeof createJiti !== "function") { - throw new Error( - "You are using an outdated version of the 'jiti' library. Please update to the latest version of 'jiti' to ensure compatibility and access to the latest features.", - ); - } - - /* - * Disabling `moduleCache` allows us to reload a - * config file when the last modified timestamp changes. - */ - const jitiOptions = { - moduleCache: false, - }; - - if (jitiVersion.startsWith("2.1.")) { - jitiOptions.interopDefault = false; - } - - const jiti = createJiti(__filename, jitiOptions); - const config = await jiti.import(fileURL.href); - - importedConfigFileModificationTime.set(filePath, mtime); - - return config?.default ?? config; -} - -/** - * Dynamically imports a module from the given file path. - * @param {string} filePath The absolute file path of the module to import. - * @param {URL} fileURL The file URL to load. - * @param {number} mtime The last modified timestamp of the file. - * @returns {Promise} - A {@linkcode Promise | promise} that resolves to the imported ESLint config. - * @since 9.24.0 - */ -async function dynamicImportConfig(filePath, fileURL, mtime) { - const module = await import(fileURL.href); - - importedConfigFileModificationTime.set(filePath, mtime); - - return module.default; -} - -/** - * Load the config array from the given filename. - * @param {string} filePath The filename to load from. - * @param {boolean} hasUnstableNativeNodeJsTSConfigFlag The flag to indicate whether the `unstable_native_nodejs_ts_config` flag is enabled. - * @returns {Promise} The config loaded from the config file. - */ -async function loadConfigFile(filePath, hasUnstableNativeNodeJsTSConfigFlag) { - debug(`Loading config from ${filePath}`); - - const fileURL = pathToFileURL(filePath); - - debug(`Config file URL is ${fileURL}`); - - const mtime = (await fs.stat(filePath)).mtime.getTime(); - - /* - * Append a query with the config file's modification time (`mtime`) in order - * to import the current version of the config file. Without the query, `import()` would - * cache the config file module by the pathname only, and then always return - * the same version (the one that was actual when the module was imported for the first time). - * - * This ensures that the config file module is loaded and executed again - * if it has been changed since the last time it was imported. - * If it hasn't been changed, `import()` will just return the cached version. - * - * Note that we should not overuse queries (e.g., by appending the current time - * to always reload the config file module) as that could cause memory leaks - * because entries are never removed from the import cache. - */ - fileURL.searchParams.append("mtime", mtime); - - /* - * With queries, we can bypass the import cache. However, when import-ing a CJS module, - * Node.js uses the require infrastructure under the hood. That includes the require cache, - * which caches the config file module by its file path (queries have no effect). - * Therefore, we also need to clear the require cache before importing the config file module. - * In order to get the same behavior with ESM and CJS config files, in particular - to reload - * the config file only if it has been changed, we track file modification times and clear - * the require cache only if the file has been changed. - */ - if (importedConfigFileModificationTime.get(filePath) !== mtime) { - delete require.cache[filePath]; - } - - const isTS = isFileTS(filePath); - const isBun = isRunningInBun(); - const isDeno = isRunningInDeno(); - - /* - * If we are dealing with a TypeScript file, then we need to use `jiti` to load it - * in Node.js. Deno and Bun both allow native importing of TypeScript files. - * - * When Node.js supports native TypeScript imports, we can remove this check. - */ - - if (isTS) { - if (hasUnstableNativeNodeJsTSConfigFlag) { - if (isNativeTypeScriptSupportEnabled()) { - return await dynamicImportConfig(filePath, fileURL, mtime); - } - - if (!("typescript" in process.features)) { - throw new Error( - "The unstable_native_nodejs_ts_config flag is not supported in older versions of Node.js.", - ); - } - - throw new Error( - "The unstable_native_nodejs_ts_config flag is enabled, but native TypeScript support is not enabled in the current Node.js process. You need to either enable native TypeScript support by passing --experimental-strip-types or remove the unstable_native_nodejs_ts_config flag.", - ); - } - - if (!isDeno && !isBun) { - return await loadTypeScriptConfigFileWithJiti( - filePath, - fileURL, - mtime, - ); - } - } - - // fallback to normal runtime behavior - - return await dynamicImportConfig(filePath, fileURL, mtime); -} - -//----------------------------------------------------------------------------- -// Exports -//----------------------------------------------------------------------------- - -/** - * Encapsulates the loading and caching of configuration files when looking up - * from the file being linted. - */ -class ConfigLoader { - /** - * Map of config file paths to the config arrays for those directories. - * @type {Map>} - */ - #configArrays = new Map(); - - /** - * Map of absolute directory names to the config file paths for those directories. - * @type {Map>} - */ - #configFilePaths = new Map(); - - /** - * The options to use when loading configuration files. - * @type {ConfigLoaderOptions} - */ - #options; - - /** - * Creates a new instance. - * @param {ConfigLoaderOptions} options The options to use when loading configuration files. - */ - constructor(options) { - this.#options = options.warningService - ? options - : { ...options, warningService: new WarningService() }; - } - - /** - * Determines which config file to use. This is determined by seeing if an - * override config file was specified, and if so, using it; otherwise, as long - * as override config file is not explicitly set to `false`, it will search - * upwards from `fromDirectory` for a file named `eslint.config.js`. - * @param {string} fromDirectory The directory from which to start searching. - * @returns {Promise<{configFilePath:string|undefined,basePath:string}>} Location information for - * the config file. - */ - async #locateConfigFileToUse(fromDirectory) { - // check cache first - if (this.#configFilePaths.has(fromDirectory)) { - return this.#configFilePaths.get(fromDirectory); - } - - const resultPromise = ConfigLoader.locateConfigFileToUse({ - useConfigFile: this.#options.configFile, - cwd: this.#options.cwd, - fromDirectory, - }); - - // ensure `ConfigLoader.locateConfigFileToUse` is called only once for `fromDirectory` - this.#configFilePaths.set(fromDirectory, resultPromise); - - // Unwrap the promise. This is primarily for the sync `getCachedConfigArrayForPath` method. - const result = await resultPromise; - - this.#configFilePaths.set(fromDirectory, result); - - return result; - } - - /** - * Calculates the config array for this run based on inputs. - * @param {string} configFilePath The absolute path to the config file to use if not overridden. - * @param {string} basePath The base path to use for relative paths in the config file. - * @returns {Promise} The config array for `eslint`. - */ - async #calculateConfigArray(configFilePath, basePath) { - // check for cached version first - if (this.#configArrays.has(configFilePath)) { - return this.#configArrays.get(configFilePath); - } - - const configsPromise = ConfigLoader.calculateConfigArray( - configFilePath, - basePath, - this.#options, - ); - - // ensure `ConfigLoader.calculateConfigArray` is called only once for `configFilePath` - this.#configArrays.set(configFilePath, configsPromise); - - // Unwrap the promise. This is primarily for the sync `getCachedConfigArrayForPath` method. - const configs = await configsPromise; - - this.#configArrays.set(configFilePath, configs); - - return configs; - } - - /** - * Returns the config file path for the given directory or file. This will either use - * the override config file that was specified in the constructor options or - * search for a config file from the directory. - * @param {string} fileOrDirPath The file or directory path to get the config file path for. - * @returns {Promise} The config file path or `undefined` if not found. - * @throws {Error} If `fileOrDirPath` is not a non-empty string. - * @throws {Error} If `fileOrDirPath` is not an absolute path. - */ - async findConfigFileForPath(fileOrDirPath) { - assertValidFilePath(fileOrDirPath); - - const absoluteDirPath = path.resolve( - this.#options.cwd, - path.dirname(fileOrDirPath), - ); - const { configFilePath } = - await this.#locateConfigFileToUse(absoluteDirPath); - - return configFilePath; - } - - /** - * Returns a configuration object for the given file based on the CLI options. - * This is the same logic used by the ESLint CLI executable to determine - * configuration for each file it processes. - * @param {string} filePath The path of the file or directory to retrieve config for. - * @returns {Promise} A configuration object for the file. - * @throws {Error} If no configuration for `filePath` exists. - */ - async loadConfigArrayForFile(filePath) { - assertValidFilePath(filePath); - - debug(`Calculating config for file ${filePath}`); - - const configFilePath = await this.findConfigFileForPath(filePath); - - assertConfigurationExists(configFilePath, this.#options); - - return this.loadConfigArrayForDirectory(filePath); - } - - /** - * Returns a configuration object for the given directory based on the CLI options. - * This is the same logic used by the ESLint CLI executable to determine - * configuration for each file it processes. - * @param {string} dirPath The path of the directory to retrieve config for. - * @returns {Promise} A configuration object for the directory. - */ - async loadConfigArrayForDirectory(dirPath) { - assertValidFilePath(dirPath); - - debug(`Calculating config for directory ${dirPath}`); - - const absoluteDirPath = path.resolve( - this.#options.cwd, - path.dirname(dirPath), - ); - const { configFilePath, basePath } = - await this.#locateConfigFileToUse(absoluteDirPath); - - debug(`Using config file ${configFilePath} and base path ${basePath}`); - return this.#calculateConfigArray(configFilePath, basePath); - } - - /** - * Returns a configuration array for the given file based on the CLI options. - * This is a synchronous operation and does not read any files from disk. It's - * intended to be used in locations where we know the config file has already - * been loaded and we just need to get the configuration for a file. - * @param {string} filePath The path of the file to retrieve a config object for. - * @returns {FlatConfigArray} A configuration object for the file. - * @throws {Error} If `filePath` is not a non-empty string. - * @throws {Error} If `filePath` is not an absolute path. - * @throws {Error} If the config file was not already loaded. - */ - getCachedConfigArrayForFile(filePath) { - assertValidFilePath(filePath); - - debug(`Looking up cached config for ${filePath}`); - - return this.getCachedConfigArrayForPath(path.dirname(filePath)); - } - - /** - * Returns a configuration array for the given directory based on the CLI options. - * This is a synchronous operation and does not read any files from disk. It's - * intended to be used in locations where we know the config file has already - * been loaded and we just need to get the configuration for a file. - * @param {string} fileOrDirPath The path of the directory to retrieve a config object for. - * @returns {FlatConfigArray} A configuration object for the directory. - * @throws {Error} If `dirPath` is not a non-empty string. - * @throws {Error} If `dirPath` is not an absolute path. - * @throws {Error} If the config file was not already loaded. - */ - getCachedConfigArrayForPath(fileOrDirPath) { - assertValidFilePath(fileOrDirPath); - - debug(`Looking up cached config for ${fileOrDirPath}`); - - const absoluteDirPath = path.resolve(this.#options.cwd, fileOrDirPath); - - if (!this.#configFilePaths.has(absoluteDirPath)) { - throw new Error(`Could not find config file for ${fileOrDirPath}`); - } - - const configFilePathInfo = this.#configFilePaths.get(absoluteDirPath); - - if (typeof configFilePathInfo.then === "function") { - throw new Error( - `Config file path for ${fileOrDirPath} has not yet been calculated or an error occurred during the calculation`, - ); - } - - const { configFilePath } = configFilePathInfo; - - const configArray = this.#configArrays.get(configFilePath); - - if (!configArray || typeof configArray.then === "function") { - throw new Error( - `Config array for ${fileOrDirPath} has not yet been calculated or an error occurred during the calculation`, - ); - } - - return configArray; - } - - /** - * Used to import the jiti dependency. This method is exposed internally for testing purposes. - * @returns {Promise<{createJiti: Function|undefined, version: string;}>} A promise that fulfills with an object containing the jiti module's createJiti function and version. - */ - static async loadJiti() { - const { createJiti } = await import("jiti"); - const version = require("jiti/package.json").version; - return { createJiti, version }; - } - - /** - * Determines which config file to use. This is determined by seeing if an - * override config file was specified, and if so, using it; otherwise, as long - * as override config file is not explicitly set to `false`, it will search - * upwards from `fromDirectory` for a file named `eslint.config.js`. - * This method is exposed internally for testing purposes. - * @param {Object} [options] the options object - * @param {string|false|undefined} options.useConfigFile The path to the config file to use. - * @param {string} options.cwd Path to a directory that should be considered as the current working directory. - * @param {string} [options.fromDirectory] The directory from which to start searching. Defaults to `cwd`. - * @returns {Promise<{configFilePath:string|undefined,basePath:string}>} Location information for - * the config file. - */ - static async locateConfigFileToUse({ - useConfigFile, - cwd, - fromDirectory = cwd, - }) { - // determine where to load config file from - let configFilePath; - let basePath = cwd; - - if (typeof useConfigFile === "string") { - debug(`Override config file path is ${useConfigFile}`); - configFilePath = path.resolve(cwd, useConfigFile); - basePath = cwd; - } else if (useConfigFile !== false) { - debug("Searching for eslint.config.js"); - configFilePath = await findUp(FLAT_CONFIG_FILENAMES, { - cwd: fromDirectory, - }); - - if (configFilePath) { - basePath = path.dirname(configFilePath); - } - } - - return { - configFilePath, - basePath, - }; - } - - /** - * Calculates the config array for this run based on inputs. - * This method is exposed internally for testing purposes. - * @param {string} configFilePath The absolute path to the config file to use if not overridden. - * @param {string} basePath The base path to use for relative paths in the config file. - * @param {ConfigLoaderOptions} options The options to use when loading configuration files. - * @returns {Promise} The config array for `eslint`. - */ - static async calculateConfigArray(configFilePath, basePath, options) { - const { - cwd, - baseConfig, - ignoreEnabled, - ignorePatterns, - overrideConfig, - hasUnstableNativeNodeJsTSConfigFlag = false, - defaultConfigs = [], - warningService, - } = options; - - debug( - `Calculating config array from config file ${configFilePath} and base path ${basePath}`, - ); - - const configs = new FlatConfigArray(baseConfig || [], { - basePath, - shouldIgnore: ignoreEnabled, - }); - - // load config file - if (configFilePath) { - debug(`Loading config file ${configFilePath}`); - const fileConfig = await loadConfigFile( - configFilePath, - hasUnstableNativeNodeJsTSConfigFlag, - ); - - /* - * It's possible that a config file could be empty or else - * have an empty object or array. In this case, we want to - * warn the user that they have an empty config. - * - * An empty CommonJS file exports an empty object while - * an empty ESM file exports undefined. - */ - - let emptyConfig = typeof fileConfig === "undefined"; - - debug( - `Config file ${configFilePath} is ${emptyConfig ? "empty" : "not empty"}`, - ); - - if (!emptyConfig) { - if (Array.isArray(fileConfig)) { - if (fileConfig.length === 0) { - debug( - `Config file ${configFilePath} is an empty array`, - ); - emptyConfig = true; - } else { - configs.push(...fileConfig); - } - } else { - if ( - typeof fileConfig === "object" && - fileConfig !== null && - Object.keys(fileConfig).length === 0 - ) { - debug( - `Config file ${configFilePath} is an empty object`, - ); - emptyConfig = true; - } else { - configs.push(fileConfig); - } - } - } - - if (emptyConfig) { - warningService.emitEmptyConfigWarning(configFilePath); - } - } - - // add in any configured defaults - configs.push(...defaultConfigs); - - // append command line ignore patterns - if (ignorePatterns && ignorePatterns.length > 0) { - /* - * Ignore patterns are added to the end of the config array - * so they can override default ignores. - */ - configs.push({ - basePath: cwd, - ignores: ignorePatterns, - }); - } - - if (overrideConfig) { - if (Array.isArray(overrideConfig)) { - configs.push(...overrideConfig); - } else { - configs.push(overrideConfig); - } - } - - await configs.normalize(); - - return configs; - } -} - -/** - * Encapsulates the loading and caching of configuration files when looking up - * from the current working directory. - */ -class LegacyConfigLoader extends ConfigLoader { - /** - * The options to use when loading configuration files. - * @type {ConfigLoaderOptions} - */ - #options; - - /** - * The cached config file path for this instance. - * @type {Promise<{configFilePath:string,basePath:string}|undefined>} - */ - #configFilePath; - - /** - * The cached config array for this instance. - * @type {FlatConfigArray|Promise} - */ - #configArray; - - /** - * Creates a new instance. - * @param {ConfigLoaderOptions} options The options to use when loading configuration files. - */ - constructor(options) { - const normalizedOptions = options.warningService - ? options - : { ...options, warningService: new WarningService() }; - super(normalizedOptions); - this.#options = normalizedOptions; - } - - /** - * Determines which config file to use. This is determined by seeing if an - * override config file was specified, and if so, using it; otherwise, as long - * as override config file is not explicitly set to `false`, it will search - * upwards from the cwd for a file named `eslint.config.js`. - * @returns {Promise<{configFilePath:string|undefined,basePath:string}>} Location information for - * the config file. - */ - #locateConfigFileToUse() { - if (!this.#configFilePath) { - this.#configFilePath = ConfigLoader.locateConfigFileToUse({ - useConfigFile: this.#options.configFile, - cwd: this.#options.cwd, - }); - } - - return this.#configFilePath; - } - - /** - * Calculates the config array for this run based on inputs. - * @param {string} configFilePath The absolute path to the config file to use if not overridden. - * @param {string} basePath The base path to use for relative paths in the config file. - * @returns {Promise} The config array for `eslint`. - */ - async #calculateConfigArray(configFilePath, basePath) { - // check for cached version first - if (this.#configArray) { - return this.#configArray; - } - - // ensure `ConfigLoader.calculateConfigArray` is called only once - this.#configArray = ConfigLoader.calculateConfigArray( - configFilePath, - basePath, - this.#options, - ); - - // Unwrap the promise. This is primarily for the sync `getCachedConfigArrayForPath` method. - this.#configArray = await this.#configArray; - - return this.#configArray; - } - - /** - * Returns the config file path for the given directory. This will either use - * the override config file that was specified in the constructor options or - * search for a config file from the directory of the file being linted. - * @param {string} dirPath The directory path to get the config file path for. - * @returns {Promise} The config file path or `undefined` if not found. - * @throws {Error} If `fileOrDirPath` is not a non-empty string. - * @throws {Error} If `fileOrDirPath` is not an absolute path. - */ - async findConfigFileForPath(dirPath) { - assertValidFilePath(dirPath); - - const { configFilePath } = await this.#locateConfigFileToUse(); - - return configFilePath; - } - - /** - * Returns a configuration object for the given file based on the CLI options. - * This is the same logic used by the ESLint CLI executable to determine - * configuration for each file it processes. - * @param {string} dirPath The path of the directory to retrieve config for. - * @returns {Promise} A configuration object for the file. - */ - async loadConfigArrayForDirectory(dirPath) { - assertValidFilePath(dirPath); - - debug(`[Legacy]: Calculating config for ${dirPath}`); - - const { configFilePath, basePath } = - await this.#locateConfigFileToUse(); - - debug( - `[Legacy]: Using config file ${configFilePath} and base path ${basePath}`, - ); - return this.#calculateConfigArray(configFilePath, basePath); - } - - /** - * Returns a configuration array for the given directory based on the CLI options. - * This is a synchronous operation and does not read any files from disk. It's - * intended to be used in locations where we know the config file has already - * been loaded and we just need to get the configuration for a file. - * @param {string} dirPath The path of the directory to retrieve a config object for. - * @returns {FlatConfigArray} A configuration object for the file. - * @throws {Error} If `dirPath` is not a non-empty string. - * @throws {Error} If `dirPath` is not an absolute path. - * @throws {Error} If the config file was not already loaded. - */ - getCachedConfigArrayForPath(dirPath) { - assertValidFilePath(dirPath); - - debug(`[Legacy]: Looking up cached config for ${dirPath}`); - - if (!this.#configArray) { - throw new Error(`Could not find config file for ${dirPath}`); - } - - if (typeof this.#configArray.then === "function") { - throw new Error( - `Config array for ${dirPath} has not yet been calculated or an error occurred during the calculation`, - ); - } - - return this.#configArray; - } -} - -module.exports = { ConfigLoader, LegacyConfigLoader }; diff --git a/node_modules/eslint/lib/config/config.js b/node_modules/eslint/lib/config/config.js deleted file mode 100644 index 22fd5cc4e..000000000 --- a/node_modules/eslint/lib/config/config.js +++ /dev/null @@ -1,674 +0,0 @@ -/** - * @fileoverview The `Config` class - * @author Nicholas C. Zakas - */ - -"use strict"; - -//----------------------------------------------------------------------------- -// Requirements -//----------------------------------------------------------------------------- - -const { deepMergeArrays } = require("../shared/deep-merge-arrays"); -const { flatConfigSchema, hasMethod } = require("./flat-config-schema"); -const { ObjectSchema } = require("@eslint/config-array"); -const ajvImport = require("../shared/ajv"); -const ajv = ajvImport(); -const ruleReplacements = require("../../conf/replacements.json"); - -//----------------------------------------------------------------------------- -// Typedefs -//----------------------------------------------------------------------------- - -/** - * @import { RuleDefinition } from "@eslint/core"; - * @import { Linter } from "eslint"; - */ - -//----------------------------------------------------------------------------- -// Private Members -//------------------------------------------------------------------------------ - -// JSON schema that disallows passing any options -const noOptionsSchema = Object.freeze({ - type: "array", - minItems: 0, - maxItems: 0, -}); - -const severities = new Map([ - [0, 0], - [1, 1], - [2, 2], - ["off", 0], - ["warn", 1], - ["error", 2], -]); - -/** - * A collection of compiled validators for rules that have already - * been validated. - * @type {WeakMap} - */ -const validators = new WeakMap(); - -//----------------------------------------------------------------------------- -// Helpers -//----------------------------------------------------------------------------- - -/** - * Throws a helpful error when a rule cannot be found. - * @param {Object} ruleId The rule identifier. - * @param {string} ruleId.pluginName The ID of the rule to find. - * @param {string} ruleId.ruleName The ID of the rule to find. - * @param {Object} config The config to search in. - * @throws {TypeError} For missing plugin or rule. - * @returns {void} - */ -function throwRuleNotFoundError({ pluginName, ruleName }, config) { - const ruleId = pluginName === "@" ? ruleName : `${pluginName}/${ruleName}`; - - const errorMessageHeader = `Key "rules": Key "${ruleId}"`; - - let errorMessage = `${errorMessageHeader}: Could not find plugin "${pluginName}" in configuration.`; - - const missingPluginErrorMessage = errorMessage; - - // if the plugin exists then we need to check if the rule exists - if (config.plugins && config.plugins[pluginName]) { - const replacementRuleName = ruleReplacements.rules[ruleName]; - - if (pluginName === "@" && replacementRuleName) { - errorMessage = `${errorMessageHeader}: Rule "${ruleName}" was removed and replaced by "${replacementRuleName}".`; - } else { - errorMessage = `${errorMessageHeader}: Could not find "${ruleName}" in plugin "${pluginName}".`; - - // otherwise, let's see if we can find the rule name elsewhere - for (const [otherPluginName, otherPlugin] of Object.entries( - config.plugins, - )) { - if (otherPlugin.rules && otherPlugin.rules[ruleName]) { - errorMessage += ` Did you mean "${otherPluginName}/${ruleName}"?`; - break; - } - } - } - - // falls through to throw error - } - - const error = new TypeError(errorMessage); - - if (errorMessage === missingPluginErrorMessage) { - error.messageTemplate = "config-plugin-missing"; - error.messageData = { pluginName, ruleId }; - } - - throw error; -} - -/** - * The error type when a rule has an invalid `meta.schema`. - */ -class InvalidRuleOptionsSchemaError extends Error { - /** - * Creates a new instance. - * @param {string} ruleId Id of the rule that has an invalid `meta.schema`. - * @param {Error} processingError Error caught while processing the `meta.schema`. - */ - constructor(ruleId, processingError) { - super( - `Error while processing options validation schema of rule '${ruleId}': ${processingError.message}`, - { cause: processingError }, - ); - this.code = "ESLINT_INVALID_RULE_OPTIONS_SCHEMA"; - } -} - -/** - * Parses a ruleId into its plugin and rule parts. - * @param {string} ruleId The rule ID to parse. - * @returns {{pluginName:string,ruleName:string}} The plugin and rule - * parts of the ruleId; - */ -function parseRuleId(ruleId) { - let pluginName, ruleName; - - // distinguish between core rules and plugin rules - if (ruleId.includes("/")) { - // mimic scoped npm packages - if (ruleId.startsWith("@")) { - pluginName = ruleId.slice(0, ruleId.lastIndexOf("/")); - } else { - pluginName = ruleId.slice(0, ruleId.indexOf("/")); - } - - ruleName = ruleId.slice(pluginName.length + 1); - } else { - pluginName = "@"; - ruleName = ruleId; - } - - return { - pluginName, - ruleName, - }; -} - -/** - * Retrieves a rule instance from a given config based on the ruleId. - * @param {string} ruleId The rule ID to look for. - * @param {Linter.Config} config The config to search. - * @returns {RuleDefinition|undefined} The rule if found - * or undefined if not. - */ -function getRuleFromConfig(ruleId, config) { - const { pluginName, ruleName } = parseRuleId(ruleId); - - return config.plugins?.[pluginName]?.rules?.[ruleName]; -} - -/** - * Gets a complete options schema for a rule. - * @param {RuleDefinition} rule A rule object - * @throws {TypeError} If `meta.schema` is specified but is not an array, object or `false`. - * @returns {Object|null} JSON Schema for the rule's options. `null` if `meta.schema` is `false`. - */ -function getRuleOptionsSchema(rule) { - if (!rule.meta) { - return { ...noOptionsSchema }; // default if `meta.schema` is not specified - } - - const schema = rule.meta.schema; - - if (typeof schema === "undefined") { - return { ...noOptionsSchema }; // default if `meta.schema` is not specified - } - - // `schema:false` is an allowed explicit opt-out of options validation for the rule - if (schema === false) { - return null; - } - - if (typeof schema !== "object" || schema === null) { - throw new TypeError("Rule's `meta.schema` must be an array or object"); - } - - // ESLint-specific array form needs to be converted into a valid JSON Schema definition - if (Array.isArray(schema)) { - if (schema.length) { - return { - type: "array", - items: schema, - minItems: 0, - maxItems: schema.length, - }; - } - - // `schema:[]` is an explicit way to specify that the rule does not accept any options - return { ...noOptionsSchema }; - } - - // `schema:` is assumed to be a valid JSON Schema definition - return schema; -} - -/** - * Splits a plugin identifier in the form a/b/c into two parts: a/b and c. - * @param {string} identifier The identifier to parse. - * @returns {{objectName: string, pluginName: string}} The parts of the plugin - * name. - */ -function splitPluginIdentifier(identifier) { - const parts = identifier.split("/"); - - return { - objectName: parts.pop(), - pluginName: parts.join("/"), - }; -} - -/** - * Returns the name of an object in the config by reading its `meta` key. - * @param {Object} object The object to check. - * @returns {string?} The name of the object if found or `null` if there - * is no name. - */ -function getObjectId(object) { - // first check old-style name - let name = object.name; - - if (!name) { - if (!object.meta) { - return null; - } - - name = object.meta.name; - - if (!name) { - return null; - } - } - - // now check for old-style version - let version = object.version; - - if (!version) { - version = object.meta && object.meta.version; - } - - // if there's a version then append that - if (version) { - return `${name}@${version}`; - } - - return name; -} - -/** - * Asserts that a value is not a function. - * @param {any} value The value to check. - * @param {string} key The key of the value in the object. - * @param {string} objectKey The key of the object being checked. - * @returns {void} - * @throws {TypeError} If the value is a function. - */ -function assertNotFunction(value, key, objectKey) { - if (typeof value === "function") { - const error = new TypeError( - `Cannot serialize key "${key}" in "${objectKey}": Function values are not supported.`, - ); - - error.messageTemplate = "config-serialize-function"; - error.messageData = { key, objectKey }; - - throw error; - } -} - -/** - * Converts a languageOptions object to a JSON representation. - * @param {Record} languageOptions The options to create a JSON - * representation of. - * @param {string} objectKey The key of the object being converted. - * @returns {Record} The JSON representation of the languageOptions. - * @throws {TypeError} If a function is found in the languageOptions. - */ -function languageOptionsToJSON(languageOptions, objectKey = "languageOptions") { - if (typeof languageOptions.toJSON === "function") { - const result = languageOptions.toJSON(); - - assertNotFunction(result, "toJSON", objectKey); - - return result; - } - - const result = {}; - - for (const [key, value] of Object.entries(languageOptions)) { - if (value) { - if (typeof value === "object") { - const name = getObjectId(value); - - if (typeof value.toJSON === "function") { - result[key] = value.toJSON(); - assertNotFunction(result[key], key, objectKey); - } else if (name && hasMethod(value)) { - result[key] = name; - } else { - result[key] = languageOptionsToJSON(value, key); - } - continue; - } - - assertNotFunction(value, key, objectKey); - } - - result[key] = value; - } - - return result; -} - -/** - * Gets or creates a validator for a rule. - * @param {Object} rule The rule to get a validator for. - * @param {string} ruleId The ID of the rule (for error reporting). - * @returns {Function|null} A validation function or null if no validation is needed. - * @throws {InvalidRuleOptionsSchemaError} If a rule's `meta.schema` is invalid. - */ -function getOrCreateValidator(rule, ruleId) { - if (!validators.has(rule)) { - try { - const schema = getRuleOptionsSchema(rule); - - if (schema) { - validators.set(rule, ajv.compile(schema)); - } - } catch (err) { - throw new InvalidRuleOptionsSchemaError(ruleId, err); - } - } - - return validators.get(rule); -} - -//----------------------------------------------------------------------------- -// Exports -//----------------------------------------------------------------------------- - -/** - * Represents a normalized configuration object. - */ -class Config { - /** - * The name to use for the language when serializing to JSON. - * @type {string|undefined} - */ - #languageName; - - /** - * The name to use for the processor when serializing to JSON. - * @type {string|undefined} - */ - #processorName; - - /** - * Creates a new instance. - * @param {Object} config The configuration object. - */ - constructor(config) { - const { plugins, language, languageOptions, processor, ...otherKeys } = - config; - - // Validate config object - const schema = new ObjectSchema(flatConfigSchema); - - schema.validate(config); - - // first, copy all the other keys over - Object.assign(this, otherKeys); - - // ensure that a language is specified - if (!language) { - throw new TypeError("Key 'language' is required."); - } - - // copy the rest over - this.plugins = plugins; - this.language = language; - - // Check language value - const { - pluginName: languagePluginName, - objectName: localLanguageName, - } = splitPluginIdentifier(language); - - this.#languageName = language; - - if ( - !plugins || - !plugins[languagePluginName] || - !plugins[languagePluginName].languages || - !plugins[languagePluginName].languages[localLanguageName] - ) { - throw new TypeError( - `Key "language": Could not find "${localLanguageName}" in plugin "${languagePluginName}".`, - ); - } - - this.language = - plugins[languagePluginName].languages[localLanguageName]; - - if (this.language.defaultLanguageOptions ?? languageOptions) { - this.languageOptions = flatConfigSchema.languageOptions.merge( - this.language.defaultLanguageOptions, - languageOptions, - ); - } else { - this.languageOptions = {}; - } - - // Validate language options - try { - this.language.validateLanguageOptions(this.languageOptions); - } catch (error) { - throw new TypeError(`Key "languageOptions": ${error.message}`, { - cause: error, - }); - } - - // Normalize language options if necessary - if (this.language.normalizeLanguageOptions) { - this.languageOptions = this.language.normalizeLanguageOptions( - this.languageOptions, - ); - } - - // Check processor value - if (processor) { - this.processor = processor; - - if (typeof processor === "string") { - const { pluginName, objectName: localProcessorName } = - splitPluginIdentifier(processor); - - this.#processorName = processor; - - if ( - !plugins || - !plugins[pluginName] || - !plugins[pluginName].processors || - !plugins[pluginName].processors[localProcessorName] - ) { - throw new TypeError( - `Key "processor": Could not find "${localProcessorName}" in plugin "${pluginName}".`, - ); - } - - this.processor = - plugins[pluginName].processors[localProcessorName]; - } else if (typeof processor === "object") { - this.#processorName = getObjectId(processor); - this.processor = processor; - } else { - throw new TypeError( - "Key 'processor' must be a string or an object.", - ); - } - } - - // Process the rules - if (this.rules) { - this.#normalizeRulesConfig(); - this.validateRulesConfig(this.rules); - } - } - - /** - * Converts the configuration to a JSON representation. - * @returns {Record} The JSON representation of the configuration. - * @throws {Error} If the configuration cannot be serialized. - */ - toJSON() { - if (this.processor && !this.#processorName) { - throw new Error( - "Could not serialize processor object (missing 'meta' object).", - ); - } - - if (!this.#languageName) { - throw new Error( - "Could not serialize language object (missing 'meta' object).", - ); - } - - return { - ...this, - plugins: Object.entries(this.plugins).map(([namespace, plugin]) => { - const pluginId = getObjectId(plugin); - - if (!pluginId) { - return namespace; - } - - return `${namespace}:${pluginId}`; - }), - language: this.#languageName, - languageOptions: languageOptionsToJSON(this.languageOptions), - processor: this.#processorName, - }; - } - - /** - * Gets a rule configuration by its ID. - * @param {string} ruleId The ID of the rule to get. - * @returns {RuleDefinition|undefined} The rule definition from the plugin, or `undefined` if the rule is not found. - */ - getRuleDefinition(ruleId) { - return getRuleFromConfig(ruleId, this); - } - - /** - * Normalizes the rules configuration. Ensures that each rule config is - * an array and that the severity is a number. Applies meta.defaultOptions. - * This function modifies `this.rules`. - * @returns {void} - */ - #normalizeRulesConfig() { - for (const [ruleId, originalConfig] of Object.entries(this.rules)) { - // ensure rule config is an array - let ruleConfig = Array.isArray(originalConfig) - ? originalConfig - : [originalConfig]; - - // normalize severity - ruleConfig[0] = severities.get(ruleConfig[0]); - - const rule = getRuleFromConfig(ruleId, this); - - // apply meta.defaultOptions - const slicedOptions = ruleConfig.slice(1); - const mergedOptions = deepMergeArrays( - rule?.meta?.defaultOptions, - slicedOptions, - ); - - if (mergedOptions.length) { - ruleConfig = [ruleConfig[0], ...mergedOptions]; - } - - this.rules[ruleId] = ruleConfig; - } - } - - /** - * Validates all of the rule configurations in the given rules config - * against the plugins in this instance. This is used primarily to - * validate inline configuration rules while inting. - * @param {Object} rulesConfig The rules config to validate. - * @returns {void} - * @throws {Error} If a rule's configuration does not match its schema. - * @throws {TypeError} If the rulesConfig is not provided or is invalid. - * @throws {InvalidRuleOptionsSchemaError} If a rule's `meta.schema` is invalid. - * @throws {TypeError} If a rule is not found in the plugins. - */ - validateRulesConfig(rulesConfig) { - if (!rulesConfig) { - throw new TypeError("Config is required for validation."); - } - - for (const [ruleId, ruleOptions] of Object.entries(rulesConfig)) { - // check for edge case - if (ruleId === "__proto__") { - continue; - } - - /* - * If a rule is disabled, we don't do any validation. This allows - * users to safely set any value to 0 or "off" without worrying - * that it will cause a validation error. - * - * Note: ruleOptions is always an array at this point because - * this validation occurs after FlatConfigArray has merged and - * normalized values. - */ - if (ruleOptions[0] === 0) { - continue; - } - - const rule = getRuleFromConfig(ruleId, this); - - if (!rule) { - throwRuleNotFoundError(parseRuleId(ruleId), this); - } - - const validateRule = getOrCreateValidator(rule, ruleId); - - if (validateRule) { - validateRule(ruleOptions.slice(1)); - - if (validateRule.errors) { - throw new Error( - `Key "rules": Key "${ruleId}":\n${validateRule.errors - .map(error => { - if ( - error.keyword === "additionalProperties" && - error.schema === false && - typeof error.parentSchema?.properties === - "object" && - typeof error.params?.additionalProperty === - "string" - ) { - const expectedProperties = Object.keys( - error.parentSchema.properties, - ).map(property => `"${property}"`); - - return `\tValue ${JSON.stringify(error.data)} ${error.message}.\n\t\tUnexpected property "${error.params.additionalProperty}". Expected properties: ${expectedProperties.join(", ")}.\n`; - } - - return `\tValue ${JSON.stringify(error.data)} ${error.message}.\n`; - }) - .join("")}`, - ); - } - } - } - } - - /** - * Gets a complete options schema for a rule. - * @param {RuleDefinition} ruleDefinition A rule definition object. - * @throws {TypeError} If `meta.schema` is specified but is not an array, object or `false`. - * @returns {Object|null} JSON Schema for the rule's options. `null` if `meta.schema` is `false`. - */ - static getRuleOptionsSchema(ruleDefinition) { - return getRuleOptionsSchema(ruleDefinition); - } - - /** - * Normalizes the severity value of a rule's configuration to a number - * @param {(number|string|[number, ...*]|[string, ...*])} ruleConfig A rule's configuration value, generally - * received from the user. A valid config value is either 0, 1, 2, the string "off" (treated the same as 0), - * the string "warn" (treated the same as 1), the string "error" (treated the same as 2), or an array - * whose first element is one of the above values. Strings are matched case-insensitively. - * @returns {(0|1|2)} The numeric severity value if the config value was valid, otherwise 0. - */ - static getRuleNumericSeverity(ruleConfig) { - const severityValue = Array.isArray(ruleConfig) - ? ruleConfig[0] - : ruleConfig; - - if (severities.has(severityValue)) { - return severities.get(severityValue); - } - - if (typeof severityValue === "string") { - return severities.get(severityValue.toLowerCase()) ?? 0; - } - - return 0; - } -} - -module.exports = { Config }; diff --git a/node_modules/eslint/lib/config/default-config.js b/node_modules/eslint/lib/config/default-config.js index aebf6e930..8a6ff8200 100644 --- a/node_modules/eslint/lib/config/default-config.js +++ b/node_modules/eslint/lib/config/default-config.js @@ -15,64 +15,53 @@ const Rules = require("../rules"); // Helpers //----------------------------------------------------------------------------- -const sharedDefaultConfig = [ - // intentionally empty config to ensure these files are globbed by default - { - files: ["**/*.js", "**/*.mjs"], - }, - { - files: ["**/*.cjs"], - languageOptions: { - sourceType: "commonjs", - ecmaVersion: "latest", - }, - }, -]; - -exports.defaultConfig = Object.freeze([ - { - plugins: { - "@": { - languages: { - js: require("../languages/js"), - }, - - /* - * Because we try to delay loading rules until absolutely - * necessary, a proxy allows us to hook into the lazy-loading - * aspect of the rules map while still keeping all of the - * relevant configuration inside of the config array. - */ - rules: new Proxy( - {}, - { - get(target, property) { - return Rules.get(property); - }, +exports.defaultConfig = [ + { + plugins: { + "@": { - has(target, property) { - return Rules.has(property); - }, - }, - ), - }, - }, - language: "@/js", - linterOptions: { - reportUnusedDisableDirectives: 1, - }, - }, + /* + * Because we try to delay loading rules until absolutely + * necessary, a proxy allows us to hook into the lazy-loading + * aspect of the rules map while still keeping all of the + * relevant configuration inside of the config array. + */ + rules: new Proxy({}, { + get(target, property) { + return Rules.get(property); + }, - // default ignores are listed here - { - ignores: ["**/node_modules/", ".git/"], - }, + has(target, property) { + return Rules.has(property); + } + }) + } + }, + languageOptions: { + sourceType: "module", + ecmaVersion: "latest", + parser: require("espree"), + parserOptions: {} + } + }, - ...sharedDefaultConfig, -]); + // default ignores are listed here + { + ignores: [ + "**/node_modules/", + ".git/" + ] + }, -exports.defaultRuleTesterConfig = Object.freeze([ - { files: ["**"] }, // Make sure the default config matches for all files - - ...sharedDefaultConfig, -]); + // intentionally empty config to ensure these files are globbed by default + { + files: ["**/*.js", "**/*.mjs"] + }, + { + files: ["**/*.cjs"], + languageOptions: { + sourceType: "commonjs", + ecmaVersion: "latest" + } + } +]; diff --git a/node_modules/eslint/lib/config/flat-config-array.js b/node_modules/eslint/lib/config/flat-config-array.js index aaa97db25..99d1dee67 100644 --- a/node_modules/eslint/lib/config/flat-config-array.js +++ b/node_modules/eslint/lib/config/flat-config-array.js @@ -9,10 +9,11 @@ // Requirements //----------------------------------------------------------------------------- -const { ConfigArray, ConfigArraySymbol } = require("@eslint/config-array"); +const { ConfigArray, ConfigArraySymbol } = require("@humanwhocodes/config-array"); const { flatConfigSchema } = require("./flat-config-schema"); +const { RuleValidator } = require("./rule-validator"); const { defaultConfig } = require("./default-config"); -const { Config } = require("./config"); +const jsPlugin = require("@eslint/js"); //----------------------------------------------------------------------------- // Helpers @@ -21,7 +22,63 @@ const { Config } = require("./config"); /** * Fields that are considered metadata and not part of the config object. */ -const META_FIELDS = new Set(["name", "basePath"]); +const META_FIELDS = new Set(["name"]); + +const ruleValidator = new RuleValidator(); + +/** + * Splits a plugin identifier in the form a/b/c into two parts: a/b and c. + * @param {string} identifier The identifier to parse. + * @returns {{objectName: string, pluginName: string}} The parts of the plugin + * name. + */ +function splitPluginIdentifier(identifier) { + const parts = identifier.split("/"); + + return { + objectName: parts.pop(), + pluginName: parts.join("/") + }; +} + +/** + * Returns the name of an object in the config by reading its `meta` key. + * @param {Object} object The object to check. + * @returns {string?} The name of the object if found or `null` if there + * is no name. + */ +function getObjectId(object) { + + // first check old-style name + let name = object.name; + + if (!name) { + + if (!object.meta) { + return null; + } + + name = object.meta.name; + + if (!name) { + return null; + } + } + + // now check for old-style version + let version = object.version; + + if (!version) { + version = object.meta && object.meta.version; + } + + // if there's a version then append that + if (version) { + return `${name}@${version}`; + } + + return name; +} /** * Wraps a config error with details about where the error occurred. @@ -31,39 +88,40 @@ const META_FIELDS = new Set(["name", "basePath"]); * @returns {TypeError} The new error with details. */ function wrapConfigErrorWithDetails(error, originalLength, baseLength) { - let location = "user-defined"; - let configIndex = error.index; - - /* - * A config array is set up in this order: - * 1. Base config - * 2. Original configs - * 3. User-defined configs - * 4. CLI-defined configs - * - * So we need to adjust the index to account for the base config. - * - * - If the index is less than the base length, it's in the base config - * (as specified by `baseConfig` argument to `FlatConfigArray` constructor). - * - If the index is greater than the base length but less than the original - * length + base length, it's in the original config. The original config - * is passed to the `FlatConfigArray` constructor as the first argument. - * - Otherwise, it's in the user-defined config, which is loaded from the - * config file and merged with any command-line options. - */ - if (error.index < baseLength) { - location = "base"; - } else if (error.index < originalLength + baseLength) { - location = "original"; - configIndex = error.index - baseLength; - } else { - configIndex = error.index - originalLength - baseLength; - } - - return new TypeError( - `${error.message.slice(0, -1)} at ${location} index ${configIndex}.`, - { cause: error }, - ); + + let location = "user-defined"; + let configIndex = error.index; + + /* + * A config array is set up in this order: + * 1. Base config + * 2. Original configs + * 3. User-defined configs + * 4. CLI-defined configs + * + * So we need to adjust the index to account for the base config. + * + * - If the index is less than the base length, it's in the base config + * (as specified by `baseConfig` argument to `FlatConfigArray` constructor). + * - If the index is greater than the base length but less than the original + * length + base length, it's in the original config. The original config + * is passed to the `FlatConfigArray` constructor as the first argument. + * - Otherwise, it's in the user-defined config, which is loaded from the + * config file and merged with any command-line options. + */ + if (error.index < baseLength) { + location = "base"; + } else if (error.index < originalLength + baseLength) { + location = "original"; + configIndex = error.index - baseLength; + } else { + configIndex = error.index - originalLength - baseLength; + } + + return new TypeError( + `${error.message.slice(0, -1)} at ${location} index ${configIndex}.`, + { cause: error } + ); } const originalBaseConfig = Symbol("originalBaseConfig"); @@ -78,140 +136,245 @@ const baseLength = Symbol("baseLength"); * Represents an array containing configuration information for ESLint. */ class FlatConfigArray extends ConfigArray { - /** - * Creates a new instance. - * @param {*[]} configs An array of configuration information. - * @param {{basePath: string, shouldIgnore: boolean, baseConfig: FlatConfig}} options The options - * to use for the config array instance. - */ - constructor( - configs, - { basePath, shouldIgnore = true, baseConfig = defaultConfig } = {}, - ) { - super(configs, { - basePath, - schema: flatConfigSchema, - }); - - /** - * The original length of the array before any modifications. - * @type {number} - */ - this[originalLength] = this.length; - - if (baseConfig[Symbol.iterator]) { - this.unshift(...baseConfig); - } else { - this.unshift(baseConfig); - } - - /** - * The length of the array after applying the base config. - * @type {number} - */ - this[baseLength] = this.length - this[originalLength]; - - /** - * The base config used to build the config array. - * @type {Array} - */ - this[originalBaseConfig] = baseConfig; - Object.defineProperty(this, originalBaseConfig, { writable: false }); - - /** - * Determines if `ignores` fields should be honored. - * If true, then all `ignores` fields are honored. - * if false, then only `ignores` fields in the baseConfig are honored. - * @type {boolean} - */ - this.shouldIgnore = shouldIgnore; - Object.defineProperty(this, "shouldIgnore", { writable: false }); - } - - /** - * Normalizes the array by calling the superclass method and catching/rethrowing - * any ConfigError exceptions with additional details. - * @param {any} [context] The context to use to normalize the array. - * @returns {Promise} A promise that resolves when the array is normalized. - */ - normalize(context) { - return super.normalize(context).catch(error => { - if (error.name === "ConfigError") { - throw wrapConfigErrorWithDetails( - error, - this[originalLength], - this[baseLength], - ); - } - - throw error; - }); - } - - /** - * Normalizes the array by calling the superclass method and catching/rethrowing - * any ConfigError exceptions with additional details. - * @param {any} [context] The context to use to normalize the array. - * @returns {FlatConfigArray} The current instance. - * @throws {TypeError} If the config is invalid. - */ - normalizeSync(context) { - try { - return super.normalizeSync(context); - } catch (error) { - if (error.name === "ConfigError") { - throw wrapConfigErrorWithDetails( - error, - this[originalLength], - this[baseLength], - ); - } - - throw error; - } - } - - /* eslint-disable class-methods-use-this -- Desired as instance method */ - /** - * Replaces a config with another config to allow us to put strings - * in the config array that will be replaced by objects before - * normalization. - * @param {Object} config The config to preprocess. - * @returns {Object} The preprocessed config. - */ - [ConfigArraySymbol.preprocessConfig](config) { - /* - * If a config object has `ignores` and no other non-meta fields, then it's an object - * for global ignores. If `shouldIgnore` is false, that object shouldn't apply, - * so we'll remove its `ignores`. - */ - if ( - !this.shouldIgnore && - !this[originalBaseConfig].includes(config) && - config.ignores && - Object.keys(config).filter(key => !META_FIELDS.has(key)).length === - 1 - ) { - /* eslint-disable-next-line no-unused-vars -- need to strip off other keys */ - const { ignores, ...otherKeys } = config; - - return otherKeys; - } - - return config; - } - - /** - * Finalizes the config by replacing plugin references with their objects - * and validating rule option schemas. - * @param {Object} config The config to finalize. - * @returns {Object} The finalized config. - * @throws {TypeError} If the config is invalid. - */ - [ConfigArraySymbol.finalizeConfig](config) { - return new Config(config); - } - /* eslint-enable class-methods-use-this -- Desired as instance method */ + + /** + * Creates a new instance. + * @param {*[]} configs An array of configuration information. + * @param {{basePath: string, shouldIgnore: boolean, baseConfig: FlatConfig}} options The options + * to use for the config array instance. + */ + constructor(configs, { + basePath, + shouldIgnore = true, + baseConfig = defaultConfig + } = {}) { + super(configs, { + basePath, + schema: flatConfigSchema + }); + + /** + * The original length of the array before any modifications. + * @type {number} + */ + this[originalLength] = this.length; + + if (baseConfig[Symbol.iterator]) { + this.unshift(...baseConfig); + } else { + this.unshift(baseConfig); + } + + /** + * The length of the array after applying the base config. + * @type {number} + */ + this[baseLength] = this.length - this[originalLength]; + + /** + * The base config used to build the config array. + * @type {Array} + */ + this[originalBaseConfig] = baseConfig; + Object.defineProperty(this, originalBaseConfig, { writable: false }); + + /** + * Determines if `ignores` fields should be honored. + * If true, then all `ignores` fields are honored. + * if false, then only `ignores` fields in the baseConfig are honored. + * @type {boolean} + */ + this.shouldIgnore = shouldIgnore; + Object.defineProperty(this, "shouldIgnore", { writable: false }); + } + + /** + * Normalizes the array by calling the superclass method and catching/rethrowing + * any ConfigError exceptions with additional details. + * @param {any} [context] The context to use to normalize the array. + * @returns {Promise} A promise that resolves when the array is normalized. + */ + normalize(context) { + return super.normalize(context) + .catch(error => { + if (error.name === "ConfigError") { + throw wrapConfigErrorWithDetails(error, this[originalLength], this[baseLength]); + } + + throw error; + + }); + } + + /** + * Normalizes the array by calling the superclass method and catching/rethrowing + * any ConfigError exceptions with additional details. + * @param {any} [context] The context to use to normalize the array. + * @returns {FlatConfigArray} The current instance. + * @throws {TypeError} If the config is invalid. + */ + normalizeSync(context) { + + try { + + return super.normalizeSync(context); + + } catch (error) { + + if (error.name === "ConfigError") { + throw wrapConfigErrorWithDetails(error, this[originalLength], this[baseLength]); + } + + throw error; + + } + + } + + /* eslint-disable class-methods-use-this -- Desired as instance method */ + /** + * Replaces a config with another config to allow us to put strings + * in the config array that will be replaced by objects before + * normalization. + * @param {Object} config The config to preprocess. + * @returns {Object} The preprocessed config. + */ + [ConfigArraySymbol.preprocessConfig](config) { + if (config === "eslint:recommended") { + + // if we are in a Node.js environment warn the user + if (typeof process !== "undefined" && process.emitWarning) { + process.emitWarning("The 'eslint:recommended' string configuration is deprecated and will be replaced by the @eslint/js package's 'recommended' config."); + } + + return jsPlugin.configs.recommended; + } + + if (config === "eslint:all") { + + // if we are in a Node.js environment warn the user + if (typeof process !== "undefined" && process.emitWarning) { + process.emitWarning("The 'eslint:all' string configuration is deprecated and will be replaced by the @eslint/js package's 'all' config."); + } + + return jsPlugin.configs.all; + } + + /* + * If a config object has `ignores` and no other non-meta fields, then it's an object + * for global ignores. If `shouldIgnore` is false, that object shouldn't apply, + * so we'll remove its `ignores`. + */ + if ( + !this.shouldIgnore && + !this[originalBaseConfig].includes(config) && + config.ignores && + Object.keys(config).filter(key => !META_FIELDS.has(key)).length === 1 + ) { + /* eslint-disable-next-line no-unused-vars -- need to strip off other keys */ + const { ignores, ...otherKeys } = config; + + return otherKeys; + } + + return config; + } + + /** + * Finalizes the config by replacing plugin references with their objects + * and validating rule option schemas. + * @param {Object} config The config to finalize. + * @returns {Object} The finalized config. + * @throws {TypeError} If the config is invalid. + */ + [ConfigArraySymbol.finalizeConfig](config) { + + const { plugins, languageOptions, processor } = config; + let parserName, processorName; + let invalidParser = false, + invalidProcessor = false; + + // Check parser value + if (languageOptions && languageOptions.parser) { + const { parser } = languageOptions; + + if (typeof parser === "object") { + parserName = getObjectId(parser); + + if (!parserName) { + invalidParser = true; + } + + } else { + invalidParser = true; + } + } + + // Check processor value + if (processor) { + if (typeof processor === "string") { + const { pluginName, objectName: localProcessorName } = splitPluginIdentifier(processor); + + processorName = processor; + + if (!plugins || !plugins[pluginName] || !plugins[pluginName].processors || !plugins[pluginName].processors[localProcessorName]) { + throw new TypeError(`Key "processor": Could not find "${localProcessorName}" in plugin "${pluginName}".`); + } + + config.processor = plugins[pluginName].processors[localProcessorName]; + } else if (typeof processor === "object") { + processorName = getObjectId(processor); + + if (!processorName) { + invalidProcessor = true; + } + + } else { + invalidProcessor = true; + } + } + + ruleValidator.validate(config); + + // apply special logic for serialization into JSON + /* eslint-disable object-shorthand -- shorthand would change "this" value */ + Object.defineProperty(config, "toJSON", { + value: function() { + + if (invalidParser) { + throw new Error("Could not serialize parser object (missing 'meta' object)."); + } + + if (invalidProcessor) { + throw new Error("Could not serialize processor object (missing 'meta' object)."); + } + + return { + ...this, + plugins: Object.entries(plugins).map(([namespace, plugin]) => { + + const pluginId = getObjectId(plugin); + + if (!pluginId) { + return namespace; + } + + return `${namespace}:${pluginId}`; + }), + languageOptions: { + ...languageOptions, + parser: parserName + }, + processor: processorName + }; + } + }); + /* eslint-enable object-shorthand -- ok to enable now */ + + return config; + } + /* eslint-enable class-methods-use-this -- Desired as instance method */ + } exports.FlatConfigArray = FlatConfigArray; diff --git a/node_modules/eslint/lib/config/flat-config-schema.js b/node_modules/eslint/lib/config/flat-config-schema.js index f393a9998..6b64319c8 100644 --- a/node_modules/eslint/lib/config/flat-config-schema.js +++ b/node_modules/eslint/lib/config/flat-config-schema.js @@ -9,6 +9,11 @@ // Requirements //----------------------------------------------------------------------------- +/* + * Note: This can be removed in ESLint v9 because structuredClone is available globally + * starting in Node.js v17. + */ +const structuredClone = require("@ungap/structured-clone").default; const { normalizeSeverityToNumber } = require("../shared/severity"); //----------------------------------------------------------------------------- @@ -28,12 +33,15 @@ const { normalizeSeverityToNumber } = require("../shared/severity"); //----------------------------------------------------------------------------- const ruleSeverities = new Map([ - [0, 0], - ["off", 0], - [1, 1], - ["warn", 1], - [2, 2], - ["error", 2], + [0, 0], ["off", 0], + [1, 1], ["warn", 1], + [2, 2], ["error", 2] +]); + +const globalVariablesValues = new Set([ + true, "true", "writable", "writeable", + false, "false", "readonly", "readable", null, + "off" ]); /** @@ -42,7 +50,7 @@ const ruleSeverities = new Map([ * @returns {boolean} `true` if the value is a non-null object. */ function isNonNullObject(value) { - return typeof value === "object" && value !== null; + return typeof value === "object" && value !== null; } /** @@ -51,7 +59,7 @@ function isNonNullObject(value) { * @returns {boolean} `true` if the value is a non-null non-array object. */ function isNonArrayObject(value) { - return isNonNullObject(value) && !Array.isArray(value); + return isNonNullObject(value) && !Array.isArray(value); } /** @@ -60,7 +68,7 @@ function isNonArrayObject(value) { * @returns {boolean} `true` if the value is undefined. */ function isUndefined(value) { - return typeof value === "undefined"; + return typeof value === "undefined"; } /** @@ -71,56 +79,57 @@ function isUndefined(value) { * @returns {Object} An object with properties from both first and second. */ function deepMerge(first, second, mergeMap = new Map()) { - let secondMergeMap = mergeMap.get(first); - - if (secondMergeMap) { - const result = secondMergeMap.get(second); - - if (result) { - // If this combination of first and second arguments has been already visited, return the previously created result. - return result; - } - } else { - secondMergeMap = new Map(); - mergeMap.set(first, secondMergeMap); - } - - /* - * First create a result object where properties from the second object - * overwrite properties from the first. This sets up a baseline to use - * later rather than needing to inspect and change every property - * individually. - */ - const result = { - ...first, - ...second, - }; - - delete result.__proto__; // eslint-disable-line no-proto -- don't merge own property "__proto__" - - // Store the pending result for this combination of first and second arguments. - secondMergeMap.set(second, result); - - for (const key of Object.keys(second)) { - // avoid hairy edge case - if ( - key === "__proto__" || - !Object.prototype.propertyIsEnumerable.call(first, key) - ) { - continue; - } - - const firstValue = first[key]; - const secondValue = second[key]; - - if (isNonArrayObject(firstValue) && isNonArrayObject(secondValue)) { - result[key] = deepMerge(firstValue, secondValue, mergeMap); - } else if (isUndefined(secondValue)) { - result[key] = firstValue; - } - } - - return result; + + let secondMergeMap = mergeMap.get(first); + + if (secondMergeMap) { + const result = secondMergeMap.get(second); + + if (result) { + + // If this combination of first and second arguments has been already visited, return the previously created result. + return result; + } + } else { + secondMergeMap = new Map(); + mergeMap.set(first, secondMergeMap); + } + + /* + * First create a result object where properties from the second object + * overwrite properties from the first. This sets up a baseline to use + * later rather than needing to inspect and change every property + * individually. + */ + const result = { + ...first, + ...second + }; + + delete result.__proto__; // eslint-disable-line no-proto -- don't merge own property "__proto__" + + // Store the pending result for this combination of first and second arguments. + secondMergeMap.set(second, result); + + for (const key of Object.keys(second)) { + + // avoid hairy edge case + if (key === "__proto__" || !Object.prototype.propertyIsEnumerable.call(first, key)) { + continue; + } + + const firstValue = first[key]; + const secondValue = second[key]; + + if (isNonArrayObject(firstValue) && isNonArrayObject(secondValue)) { + result[key] = deepMerge(firstValue, secondValue, mergeMap); + } else if (isUndefined(secondValue)) { + result[key] = firstValue; + } + } + + return result; + } /** @@ -130,27 +139,13 @@ function deepMerge(first, second, mergeMap = new Map()) { * @returns {Array} An array of rule options. */ function normalizeRuleOptions(ruleOptions) { - const finalOptions = Array.isArray(ruleOptions) - ? ruleOptions.slice(0) - : [ruleOptions]; - finalOptions[0] = ruleSeverities.get(finalOptions[0]); - return structuredClone(finalOptions); -} + const finalOptions = Array.isArray(ruleOptions) + ? ruleOptions.slice(0) + : [ruleOptions]; -/** - * Determines if an object has any methods. - * @param {Object} object The object to check. - * @returns {boolean} `true` if the object has any methods. - */ -function hasMethod(object) { - for (const key of Object.keys(object)) { - if (typeof object[key] === "function") { - return true; - } - } - - return false; + finalOptions[0] = ruleSeverities.get(finalOptions[0]); + return structuredClone(finalOptions); } //----------------------------------------------------------------------------- @@ -161,17 +156,16 @@ function hasMethod(object) { * The error type when a rule's options are configured with an invalid type. */ class InvalidRuleOptionsError extends Error { - /** - * @param {string} ruleId Rule name being configured. - * @param {any} value The invalid value. - */ - constructor(ruleId, value) { - super( - `Key "${ruleId}": Expected severity of "off", 0, "warn", 1, "error", or 2.`, - ); - this.messageTemplate = "invalid-rule-options"; - this.messageData = { ruleId, value }; - } + + /** + * @param {string} ruleId Rule name being configured. + * @param {any} value The invalid value. + */ + constructor(ruleId, value) { + super(`Key "${ruleId}": Expected severity of "off", 0, "warn", 1, "error", or 2.`); + this.messageTemplate = "invalid-rule-options"; + this.messageData = { ruleId, value }; + } } /** @@ -182,30 +176,25 @@ class InvalidRuleOptionsError extends Error { * @throws {InvalidRuleOptionsError} If the value isn't a valid rule options. */ function assertIsRuleOptions(ruleId, value) { - if ( - typeof value !== "string" && - typeof value !== "number" && - !Array.isArray(value) - ) { - throw new InvalidRuleOptionsError(ruleId, value); - } + if (typeof value !== "string" && typeof value !== "number" && !Array.isArray(value)) { + throw new InvalidRuleOptionsError(ruleId, value); + } } /** * The error type when a rule's severity is invalid. */ class InvalidRuleSeverityError extends Error { - /** - * @param {string} ruleId Rule name being configured. - * @param {any} value The invalid value. - */ - constructor(ruleId, value) { - super( - `Key "${ruleId}": Expected severity of "off", 0, "warn", 1, "error", or 2.`, - ); - this.messageTemplate = "invalid-rule-severity"; - this.messageData = { ruleId, value }; - } + + /** + * @param {string} ruleId Rule name being configured. + * @param {any} value The invalid value. + */ + constructor(ruleId, value) { + super(`Key "${ruleId}": Expected severity of "off", 0, "warn", 1, "error", or 2.`); + this.messageTemplate = "invalid-rule-severity"; + this.messageData = { ruleId, value }; + } } /** @@ -216,11 +205,11 @@ class InvalidRuleSeverityError extends Error { * @throws {InvalidRuleSeverityError} If the value isn't a valid rule severity. */ function assertIsRuleSeverity(ruleId, value) { - const severity = ruleSeverities.get(value); + const severity = ruleSeverities.get(value); - if (typeof severity === "undefined") { - throw new InvalidRuleSeverityError(ruleId, value); - } + if (typeof severity === "undefined") { + throw new InvalidRuleSeverityError(ruleId, value); + } } /** @@ -230,11 +219,9 @@ function assertIsRuleSeverity(ruleId, value) { * @throws {TypeError} If the string isn't in the correct format. */ function assertIsPluginMemberName(value) { - if (!/[\w\-@$]+(?:\/[\w\-$]+)+$/iu.test(value)) { - throw new TypeError( - `Expected string in the form "pluginName/objectName" but found "${value}".`, - ); - } + if (!/[@a-z0-9-_$]+(?:\/(?:[a-z0-9-_$]+))+$/iu.test(value)) { + throw new TypeError(`Expected string in the form "pluginName/objectName" but found "${value}".`); + } } /** @@ -244,98 +231,79 @@ function assertIsPluginMemberName(value) { * @throws {TypeError} If the value isn't an object. */ function assertIsObject(value) { - if (!isNonNullObject(value)) { - throw new TypeError("Expected an object."); - } + if (!isNonNullObject(value)) { + throw new TypeError("Expected an object."); + } } /** * The error type when there's an eslintrc-style options in a flat config. */ class IncompatibleKeyError extends Error { - /** - * @param {string} key The invalid key. - */ - constructor(key) { - super( - "This appears to be in eslintrc format rather than flat config format.", - ); - this.messageTemplate = "eslintrc-incompat"; - this.messageData = { key }; - } + + /** + * @param {string} key The invalid key. + */ + constructor(key) { + super("This appears to be in eslintrc format rather than flat config format."); + this.messageTemplate = "eslintrc-incompat"; + this.messageData = { key }; + } } /** * The error type when there's an eslintrc-style plugins array found. */ class IncompatiblePluginsError extends Error { - /** - * Creates a new instance. - * @param {Array} plugins The plugins array. - */ - constructor(plugins) { - super( - "This appears to be in eslintrc format (array of strings) rather than flat config format (object).", - ); - this.messageTemplate = "eslintrc-plugins"; - this.messageData = { plugins }; - } + + /** + * Creates a new instance. + * @param {Array} plugins The plugins array. + */ + constructor(plugins) { + super("This appears to be in eslintrc format (array of strings) rather than flat config format (object)."); + this.messageTemplate = "eslintrc-plugins"; + this.messageData = { plugins }; + } } + //----------------------------------------------------------------------------- // Low-Level Schemas //----------------------------------------------------------------------------- /** @type {ObjectPropertySchema} */ const booleanSchema = { - merge: "replace", - validate: "boolean", + merge: "replace", + validate: "boolean" }; const ALLOWED_SEVERITIES = new Set(["error", "warn", "off", 2, 1, 0]); /** @type {ObjectPropertySchema} */ const disableDirectiveSeveritySchema = { - merge(first, second) { - const value = second === void 0 ? first : second; - - if (typeof value === "boolean") { - return value ? "warn" : "off"; - } - - return normalizeSeverityToNumber(value); - }, - validate(value) { - if (!(ALLOWED_SEVERITIES.has(value) || typeof value === "boolean")) { - throw new TypeError( - 'Expected one of: "error", "warn", "off", 0, 1, 2, or a boolean.', - ); - } - }, -}; - -/** @type {ObjectPropertySchema} */ -const unusedInlineConfigsSeveritySchema = { - merge(first, second) { - const value = second === void 0 ? first : second; - - return normalizeSeverityToNumber(value); - }, - validate(value) { - if (!ALLOWED_SEVERITIES.has(value)) { - throw new TypeError( - 'Expected one of: "error", "warn", "off", 0, 1, or 2.', - ); - } - }, + merge(first, second) { + const value = second === void 0 ? first : second; + + if (typeof value === "boolean") { + return value ? "warn" : "off"; + } + + return normalizeSeverityToNumber(value); + }, + validate(value) { + if (!(ALLOWED_SEVERITIES.has(value) || typeof value === "boolean")) { + throw new TypeError("Expected one of: \"error\", \"warn\", \"off\", 0, 1, 2, or a boolean."); + } + } }; /** @type {ObjectPropertySchema} */ const deepObjectAssignSchema = { - merge(first = {}, second = {}) { - return deepMerge(first, second); - }, - validate: "object", + merge(first = {}, second = {}) { + return deepMerge(first, second); + }, + validate: "object" }; //----------------------------------------------------------------------------- @@ -343,194 +311,221 @@ const deepObjectAssignSchema = { //----------------------------------------------------------------------------- /** @type {ObjectPropertySchema} */ -const languageOptionsSchema = { - merge(first = {}, second = {}) { - const result = deepMerge(first, second); - - for (const [key, value] of Object.entries(result)) { - /* - * Special case: Because the `parser` property is an object, it should - * not be deep merged. Instead, it should be replaced if it exists in - * the second object. To make this more generic, we just check for - * objects with methods and replace them if they exist in the second - * object. - */ - if (isNonArrayObject(value)) { - if (hasMethod(value)) { - result[key] = second[key] ?? first[key]; - continue; - } - - // for other objects, make sure we aren't reusing the same object - result[key] = { ...result[key] }; - continue; - } - } - - return result; - }, - validate: "object", +const globalsSchema = { + merge: "assign", + validate(value) { + + assertIsObject(value); + + for (const key of Object.keys(value)) { + + // avoid hairy edge case + if (key === "__proto__") { + continue; + } + + if (key !== key.trim()) { + throw new TypeError(`Global "${key}" has leading or trailing whitespace.`); + } + + if (!globalVariablesValues.has(value[key])) { + throw new TypeError(`Key "${key}": Expected "readonly", "writable", or "off".`); + } + } + } }; /** @type {ObjectPropertySchema} */ -const languageSchema = { - merge: "replace", - validate: assertIsPluginMemberName, +const parserSchema = { + merge: "replace", + validate(value) { + + if (!value || typeof value !== "object" || + (typeof value.parse !== "function" && typeof value.parseForESLint !== "function") + ) { + throw new TypeError("Expected object with parse() or parseForESLint() method."); + } + + } }; /** @type {ObjectPropertySchema} */ const pluginsSchema = { - merge(first = {}, second = {}) { - const keys = new Set([...Object.keys(first), ...Object.keys(second)]); - const result = {}; - - // manually validate that plugins are not redefined - for (const key of keys) { - // avoid hairy edge case - if (key === "__proto__") { - continue; - } - - if (key in first && key in second && first[key] !== second[key]) { - throw new TypeError(`Cannot redefine plugin "${key}".`); - } - - result[key] = second[key] || first[key]; - } - - return result; - }, - validate(value) { - // first check the value to be sure it's an object - if (value === null || typeof value !== "object") { - throw new TypeError("Expected an object."); - } - - // make sure it's not an array, which would mean eslintrc-style is used - if (Array.isArray(value)) { - throw new IncompatiblePluginsError(value); - } - - // second check the keys to make sure they are objects - for (const key of Object.keys(value)) { - // avoid hairy edge case - if (key === "__proto__") { - continue; - } - - if (value[key] === null || typeof value[key] !== "object") { - throw new TypeError(`Key "${key}": Expected an object.`); - } - } - }, + merge(first = {}, second = {}) { + const keys = new Set([...Object.keys(first), ...Object.keys(second)]); + const result = {}; + + // manually validate that plugins are not redefined + for (const key of keys) { + + // avoid hairy edge case + if (key === "__proto__") { + continue; + } + + if (key in first && key in second && first[key] !== second[key]) { + throw new TypeError(`Cannot redefine plugin "${key}".`); + } + + result[key] = second[key] || first[key]; + } + + return result; + }, + validate(value) { + + // first check the value to be sure it's an object + if (value === null || typeof value !== "object") { + throw new TypeError("Expected an object."); + } + + // make sure it's not an array, which would mean eslintrc-style is used + if (Array.isArray(value)) { + throw new IncompatiblePluginsError(value); + } + + // second check the keys to make sure they are objects + for (const key of Object.keys(value)) { + + // avoid hairy edge case + if (key === "__proto__") { + continue; + } + + if (value[key] === null || typeof value[key] !== "object") { + throw new TypeError(`Key "${key}": Expected an object.`); + } + } + } }; /** @type {ObjectPropertySchema} */ const processorSchema = { - merge: "replace", - validate(value) { - if (typeof value === "string") { - assertIsPluginMemberName(value); - } else if (value && typeof value === "object") { - if ( - typeof value.preprocess !== "function" || - typeof value.postprocess !== "function" - ) { - throw new TypeError( - "Object must have a preprocess() and a postprocess() method.", - ); - } - } else { - throw new TypeError("Expected an object or a string."); - } - }, + merge: "replace", + validate(value) { + if (typeof value === "string") { + assertIsPluginMemberName(value); + } else if (value && typeof value === "object") { + if (typeof value.preprocess !== "function" || typeof value.postprocess !== "function") { + throw new TypeError("Object must have a preprocess() and a postprocess() method."); + } + } else { + throw new TypeError("Expected an object or a string."); + } + } }; /** @type {ObjectPropertySchema} */ const rulesSchema = { - merge(first = {}, second = {}) { - const result = { - ...first, - ...second, - }; - - for (const ruleId of Object.keys(result)) { - try { - // avoid hairy edge case - if (ruleId === "__proto__") { - /* eslint-disable-next-line no-proto -- Though deprecated, may still be present */ - delete result.__proto__; - continue; - } - - result[ruleId] = normalizeRuleOptions(result[ruleId]); - - /* - * If either rule config is missing, then the correct - * config is already present and we just need to normalize - * the severity. - */ - if (!(ruleId in first) || !(ruleId in second)) { - continue; - } - - const firstRuleOptions = normalizeRuleOptions(first[ruleId]); - const secondRuleOptions = normalizeRuleOptions(second[ruleId]); - - /* - * If the second rule config only has a severity (length of 1), - * then use that severity and keep the rest of the options from - * the first rule config. - */ - if (secondRuleOptions.length === 1) { - result[ruleId] = [ - secondRuleOptions[0], - ...firstRuleOptions.slice(1), - ]; - continue; - } - - /* - * In any other situation, then the second rule config takes - * precedence. That means the value at `result[ruleId]` is - * already correct and no further work is necessary. - */ - } catch (ex) { - throw new Error(`Key "${ruleId}": ${ex.message}`, { - cause: ex, - }); - } - } - - return result; - }, - - validate(value) { - assertIsObject(value); - - /* - * We are not checking the rule schema here because there is no - * guarantee that the rule definition is present at this point. Instead - * we wait and check the rule schema during the finalization step - * of calculating a config. - */ - for (const ruleId of Object.keys(value)) { - // avoid hairy edge case - if (ruleId === "__proto__") { - continue; - } - - const ruleOptions = value[ruleId]; - - assertIsRuleOptions(ruleId, ruleOptions); - - if (Array.isArray(ruleOptions)) { - assertIsRuleSeverity(ruleId, ruleOptions[0]); - } else { - assertIsRuleSeverity(ruleId, ruleOptions); - } - } - }, + merge(first = {}, second = {}) { + + const result = { + ...first, + ...second + }; + + + for (const ruleId of Object.keys(result)) { + + try { + + // avoid hairy edge case + if (ruleId === "__proto__") { + + /* eslint-disable-next-line no-proto -- Though deprecated, may still be present */ + delete result.__proto__; + continue; + } + + result[ruleId] = normalizeRuleOptions(result[ruleId]); + + /* + * If either rule config is missing, then the correct + * config is already present and we just need to normalize + * the severity. + */ + if (!(ruleId in first) || !(ruleId in second)) { + continue; + } + + const firstRuleOptions = normalizeRuleOptions(first[ruleId]); + const secondRuleOptions = normalizeRuleOptions(second[ruleId]); + + /* + * If the second rule config only has a severity (length of 1), + * then use that severity and keep the rest of the options from + * the first rule config. + */ + if (secondRuleOptions.length === 1) { + result[ruleId] = [secondRuleOptions[0], ...firstRuleOptions.slice(1)]; + continue; + } + + /* + * In any other situation, then the second rule config takes + * precedence. That means the value at `result[ruleId]` is + * already correct and no further work is necessary. + */ + } catch (ex) { + throw new Error(`Key "${ruleId}": ${ex.message}`, { cause: ex }); + } + + } + + return result; + + + }, + + validate(value) { + assertIsObject(value); + + /* + * We are not checking the rule schema here because there is no + * guarantee that the rule definition is present at this point. Instead + * we wait and check the rule schema during the finalization step + * of calculating a config. + */ + for (const ruleId of Object.keys(value)) { + + // avoid hairy edge case + if (ruleId === "__proto__") { + continue; + } + + const ruleOptions = value[ruleId]; + + assertIsRuleOptions(ruleId, ruleOptions); + + if (Array.isArray(ruleOptions)) { + assertIsRuleSeverity(ruleId, ruleOptions[0]); + } else { + assertIsRuleSeverity(ruleId, ruleOptions); + } + } + } +}; + +/** @type {ObjectPropertySchema} */ +const ecmaVersionSchema = { + merge: "replace", + validate(value) { + if (typeof value === "number" || value === "latest") { + return; + } + + throw new TypeError("Expected a number or \"latest\"."); + } +}; + +/** @type {ObjectPropertySchema} */ +const sourceTypeSchema = { + merge: "replace", + validate(value) { + if (typeof value !== "string" || !/^(?:script|module|commonjs)$/u.test(value)) { + throw new TypeError("Expected \"script\", \"module\", or \"commonjs\"."); + } + } }; /** @@ -540,25 +535,25 @@ const rulesSchema = { * @returns {ObjectPropertySchema} The schema. */ function createEslintrcErrorSchema(key) { - return { - merge: "replace", - validate() { - throw new IncompatibleKeyError(key); - }, - }; + return { + merge: "replace", + validate() { + throw new IncompatibleKeyError(key); + } + }; } const eslintrcKeys = [ - "env", - "extends", - "globals", - "ignorePatterns", - "noInlineConfig", - "overrides", - "parser", - "parserOptions", - "reportUnusedDisableDirectives", - "root", + "env", + "extends", + "globals", + "ignorePatterns", + "noInlineConfig", + "overrides", + "parser", + "parserOptions", + "reportUnusedDisableDirectives", + "root" ]; //----------------------------------------------------------------------------- @@ -566,25 +561,30 @@ const eslintrcKeys = [ //----------------------------------------------------------------------------- const flatConfigSchema = { - // eslintrc-style keys that should always error - ...Object.fromEntries( - eslintrcKeys.map(key => [key, createEslintrcErrorSchema(key)]), - ), - - // flat config keys - settings: deepObjectAssignSchema, - linterOptions: { - schema: { - noInlineConfig: booleanSchema, - reportUnusedDisableDirectives: disableDirectiveSeveritySchema, - reportUnusedInlineConfigs: unusedInlineConfigsSeveritySchema, - }, - }, - language: languageSchema, - languageOptions: languageOptionsSchema, - processor: processorSchema, - plugins: pluginsSchema, - rules: rulesSchema, + + // eslintrc-style keys that should always error + ...Object.fromEntries(eslintrcKeys.map(key => [key, createEslintrcErrorSchema(key)])), + + // flat config keys + settings: deepObjectAssignSchema, + linterOptions: { + schema: { + noInlineConfig: booleanSchema, + reportUnusedDisableDirectives: disableDirectiveSeveritySchema + } + }, + languageOptions: { + schema: { + ecmaVersion: ecmaVersionSchema, + sourceType: sourceTypeSchema, + globals: globalsSchema, + parser: parserSchema, + parserOptions: deepObjectAssignSchema + } + }, + processor: processorSchema, + plugins: pluginsSchema, + rules: rulesSchema }; //----------------------------------------------------------------------------- @@ -592,7 +592,7 @@ const flatConfigSchema = { //----------------------------------------------------------------------------- module.exports = { - flatConfigSchema, - hasMethod, - assertIsRuleSeverity, + flatConfigSchema, + assertIsRuleSeverity, + assertIsRuleOptions }; diff --git a/node_modules/eslint/lib/eslint/eslint-helpers.js b/node_modules/eslint/lib/eslint/eslint-helpers.js index 16c8dde94..3c65d11bf 100644 --- a/node_modules/eslint/lib/eslint/eslint-helpers.js +++ b/node_modules/eslint/lib/eslint/eslint-helpers.js @@ -9,19 +9,15 @@ // Requirements //----------------------------------------------------------------------------- -const path = require("node:path"); -const fs = require("node:fs"); -const { isMainThread, threadId } = require("node:worker_threads"); +const path = require("path"); +const fs = require("fs"); const fsp = fs.promises; const isGlob = require("is-glob"); const hash = require("../cli-engine/hash"); const minimatch = require("minimatch"); +const fswalk = require("@nodelib/fs.walk"); const globParent = require("glob-parent"); -const { Linter } = require("../linter"); -const { getShorthandName } = require("../shared/naming"); -const LintResultCache = require("../cli-engine/lint-result-cache"); -const { ConfigLoader, LegacyConfigLoader } = require("../config/config-loader"); -const createDebug = require("debug"); +const isPathInside = require("is-path-inside"); //----------------------------------------------------------------------------- // Fixup references @@ -29,25 +25,11 @@ const createDebug = require("debug"); const Minimatch = minimatch.Minimatch; const MINIMATCH_OPTIONS = { dot: true }; -const hrtimeBigint = process.hrtime.bigint; //----------------------------------------------------------------------------- // Types //----------------------------------------------------------------------------- -/** - * @import { ESLintOptions } from "./eslint.js"; - * @import { Config as CalculatedConfig } from "../config/config.js"; - * @import { FlatConfigArray } from "../config/flat-config-array.js"; - * @import { WarningService } from "../services/warning-service.js"; - * @import { Retrier } from "@humanwhocodes/retry"; - */ - -/** @typedef {import("../types").Linter.Config} Config */ -/** @typedef {import("../types").Linter.LintMessage} LintMessage */ -/** @typedef {import("../types").ESLint.LintResult} LintResult */ -/** @typedef {import("../types").ESLint.Plugin} Plugin */ - /** * @typedef {Object} GlobSearch * @property {Array} patterns The normalized patterns to use for a search. @@ -55,18 +37,6 @@ const hrtimeBigint = process.hrtime.bigint; * before doing any normalization. */ -//------------------------------------------------------------------------------ -// Debug Helpers -//------------------------------------------------------------------------------ - -// Add %t formatter to print bigint nanosecond times in milliseconds. -createDebug.formatters.t = timeDiff => - `${(timeDiff + 500_000n) / 1_000_000n} ms`; - -const debug = createDebug( - `eslint:eslint-helpers${isMainThread ? "" : `:thread-${threadId}`}`, -); - //----------------------------------------------------------------------------- // Errors //----------------------------------------------------------------------------- @@ -75,99 +45,78 @@ const debug = createDebug( * The error type when no files match a glob. */ class NoFilesFoundError extends Error { - /** - * @param {string} pattern The glob pattern which was not found. - * @param {boolean} globEnabled If `false` then the pattern was a glob pattern, but glob was disabled. - */ - constructor(pattern, globEnabled) { - super( - `No files matching '${pattern}' were found${!globEnabled ? " (glob was disabled)" : ""}.`, - ); - this.messageTemplate = "file-not-found"; - this.messageData = { pattern, globDisabled: !globEnabled }; - } + + /** + * @param {string} pattern The glob pattern which was not found. + * @param {boolean} globEnabled If `false` then the pattern was a glob pattern, but glob was disabled. + */ + constructor(pattern, globEnabled) { + super(`No files matching '${pattern}' were found${!globEnabled ? " (glob was disabled)" : ""}.`); + this.messageTemplate = "file-not-found"; + this.messageData = { pattern, globDisabled: !globEnabled }; + } } /** * The error type when a search fails to match multiple patterns. */ class UnmatchedSearchPatternsError extends Error { - /** - * @param {Object} options The options for the error. - * @param {string} options.basePath The directory that was searched. - * @param {Array} options.unmatchedPatterns The glob patterns - * which were not found. - * @param {Array} options.patterns The glob patterns that were - * searched. - * @param {Array} options.rawPatterns The raw glob patterns that - * were searched. - */ - constructor({ basePath, unmatchedPatterns, patterns, rawPatterns }) { - super( - `No files matching '${rawPatterns}' in '${basePath}' were found.`, - ); - this.basePath = basePath; - this.unmatchedPatterns = unmatchedPatterns; - this.patterns = patterns; - this.rawPatterns = rawPatterns; - } + + /** + * @param {Object} options The options for the error. + * @param {string} options.basePath The directory that was searched. + * @param {Array} options.unmatchedPatterns The glob patterns + * which were not found. + * @param {Array} options.patterns The glob patterns that were + * searched. + * @param {Array} options.rawPatterns The raw glob patterns that + * were searched. + */ + constructor({ basePath, unmatchedPatterns, patterns, rawPatterns }) { + super(`No files matching '${rawPatterns}' in '${basePath}' were found.`); + this.basePath = basePath; + this.unmatchedPatterns = unmatchedPatterns; + this.patterns = patterns; + this.rawPatterns = rawPatterns; + } } /** * The error type when there are files matched by a glob, but all of them have been ignored. */ class AllFilesIgnoredError extends Error { - /** - * @param {string} pattern The glob pattern which was not found. - */ - constructor(pattern) { - super(`All files matched by '${pattern}' are ignored.`); - this.messageTemplate = "all-matched-files-ignored"; - this.messageData = { pattern }; - } + + /** + * @param {string} pattern The glob pattern which was not found. + */ + constructor(pattern) { + super(`All files matched by '${pattern}' are ignored.`); + this.messageTemplate = "all-files-ignored"; + this.messageData = { pattern }; + } } + //----------------------------------------------------------------------------- // General Helpers //----------------------------------------------------------------------------- /** * Check if a given value is a non-empty string or not. - * @param {any} value The value to check. - * @returns {boolean} `true` if `value` is a non-empty string. + * @param {any} x The value to check. + * @returns {boolean} `true` if `x` is a non-empty string. */ -function isNonEmptyString(value) { - return typeof value === "string" && value.trim() !== ""; +function isNonEmptyString(x) { + return typeof x === "string" && x.trim() !== ""; } /** * Check if a given value is an array of non-empty strings or not. - * @param {any} value The value to check. - * @returns {boolean} `true` if `value` is an array of non-empty strings. - */ -function isArrayOfNonEmptyString(value) { - return ( - Array.isArray(value) && !!value.length && value.every(isNonEmptyString) - ); -} - -/** - * Check if a given value is an empty array or an array of non-empty strings. - * @param {any} value The value to check. - * @returns {boolean} `true` if `value` is an empty array or an array of non-empty - * strings. - */ -function isEmptyArrayOrArrayOfNonEmptyString(value) { - return Array.isArray(value) && value.every(isNonEmptyString); -} - -/** - * Check if a given value is a positive integer. - * @param {unknown} value The value to check. - * @returns {boolean} `true` if `value` is a positive integer. + * @param {any} x The value to check. + * @returns {boolean} `true` if `x` is an array of non-empty strings. */ -function isPositiveInteger(value) { - return Number.isInteger(value) && value > 0; +function isArrayOfNonEmptyString(x) { + return Array.isArray(x) && x.every(isNonEmptyString); } //----------------------------------------------------------------------------- @@ -180,7 +129,7 @@ function isPositiveInteger(value) { * @returns {string} The pattern with slashes normalized. */ function normalizeToPosix(pattern) { - return pattern.replace(/\\/gu, "/"); + return pattern.replace(/\\/gu, "/"); } /** @@ -189,51 +138,70 @@ function normalizeToPosix(pattern) { * @returns {boolean} `true` if the string is a glob pattern. */ function isGlobPattern(pattern) { - return isGlob(path.sep === "\\" ? normalizeToPosix(pattern) : pattern); + return isGlob(path.sep === "\\" ? normalizeToPosix(pattern) : pattern); } + /** * Determines if a given glob pattern will return any results. * Used primarily to help with useful error messages. * @param {Object} options The options for the function. * @param {string} options.basePath The directory to search. - * @param {string} options.pattern An absolute path glob pattern to match. + * @param {string} options.pattern A glob pattern to match. * @returns {Promise} True if there is a glob match, false if not. */ -async function globMatch({ basePath, pattern }) { - let found = false; - const { hfs } = await import("@humanfs/node"); - const patternToUse = normalizeToPosix(path.relative(basePath, pattern)); - - const matcher = new Minimatch(patternToUse, MINIMATCH_OPTIONS); - - const walkSettings = { - directoryFilter(entry) { - return !found && matcher.match(entry.path, true); - }, - - entryFilter(entry) { - if (found || entry.isDirectory) { - return false; - } - - if (matcher.match(entry.path)) { - found = true; - return true; - } - - return false; - }, - }; - - if (await hfs.isDirectory(basePath)) { - return hfs - .walk(basePath, walkSettings) - .next() - .then(() => found); - } - - return found; +function globMatch({ basePath, pattern }) { + + let found = false; + const patternToUse = path.isAbsolute(pattern) + ? normalizeToPosix(path.relative(basePath, pattern)) + : pattern; + + const matcher = new Minimatch(patternToUse, MINIMATCH_OPTIONS); + + const fsWalkSettings = { + + deepFilter(entry) { + const relativePath = normalizeToPosix(path.relative(basePath, entry.path)); + + return !found && matcher.match(relativePath, true); + }, + + entryFilter(entry) { + if (found || entry.dirent.isDirectory()) { + return false; + } + + const relativePath = normalizeToPosix(path.relative(basePath, entry.path)); + + if (matcher.match(relativePath)) { + found = true; + return true; + } + + return false; + } + }; + + return new Promise(resolve => { + + // using a stream so we can exit early because we just need one match + const globStream = fswalk.walkStream(basePath, fsWalkSettings); + + globStream.on("data", () => { + globStream.destroy(); + resolve(true); + }); + + // swallow errors as they're not important here + globStream.on("error", () => { }); + + globStream.on("end", () => { + resolve(false); + }); + globStream.read(); + }); + } /** @@ -243,11 +211,11 @@ async function globMatch({ basePath, pattern }) { * ESLint. * @param {Object} options The options for this function. * @param {string} options.basePath The directory to search. - * @param {Array} options.patterns An array of absolute path glob patterns + * @param {Array} options.patterns An array of glob patterns * to match. * @param {Array} options.rawPatterns An array of glob patterns * as the user inputted them. Used for errors. - * @param {ConfigLoader|LegacyConfigLoader} options.configLoader The config array to use for + * @param {FlatConfigArray} options.configs The config array to use for * determining what to ignore. * @param {boolean} options.errorOnUnmatchedPattern Determines if an error * should be thrown when a pattern is unmatched. @@ -257,128 +225,150 @@ async function globMatch({ basePath, pattern }) { * match any files. */ async function globSearch({ - basePath, - patterns, - rawPatterns, - configLoader, - errorOnUnmatchedPattern, + basePath, + patterns, + rawPatterns, + configs, + errorOnUnmatchedPattern }) { - if (patterns.length === 0) { - return []; - } - - /* - * In this section we are converting the patterns into Minimatch - * instances for performance reasons. Because we are doing the same - * matches repeatedly, it's best to compile those patterns once and - * reuse them multiple times. - * - * To do that, we convert any patterns with an absolute path into a - * relative path and normalize it to Posix-style slashes. We also keep - * track of the relative patterns to map them back to the original - * patterns, which we need in order to throw an error if there are any - * unmatched patterns. - */ - const relativeToPatterns = new Map(); - const matchers = patterns.map((pattern, i) => { - const patternToUse = normalizeToPosix(path.relative(basePath, pattern)); - - relativeToPatterns.set(patternToUse, patterns[i]); - - return new Minimatch(patternToUse, MINIMATCH_OPTIONS); - }); - - /* - * We track unmatched patterns because we may want to throw an error when - * they occur. To start, this set is initialized with all of the patterns. - * Every time a match occurs, the pattern is removed from the set, making - * it easy to tell if we have any unmatched patterns left at the end of - * search. - */ - const unmatchedPatterns = new Set([...relativeToPatterns.keys()]); - const { hfs } = await import("@humanfs/node"); - - const walk = hfs.walk(basePath, { - async directoryFilter(entry) { - if (!matchers.some(matcher => matcher.match(entry.path, true))) { - return false; - } - - const absolutePath = path.resolve(basePath, entry.path); - const configs = - await configLoader.loadConfigArrayForDirectory(absolutePath); - - return !configs.isDirectoryIgnored(absolutePath); - }, - async entryFilter(entry) { - const absolutePath = path.resolve(basePath, entry.path); - - // entries may be directories or files so filter out directories - if (entry.isDirectory) { - return false; - } - - const configs = - await configLoader.loadConfigArrayForFile(absolutePath); - const config = configs.getConfig(absolutePath); - - /* - * Optimization: We need to track when patterns are left unmatched - * and so we use `unmatchedPatterns` to do that. There is a bit of - * complexity here because the same file can be matched by more than - * one pattern. So, when we start, we actually need to test every - * pattern against every file. Once we know there are no remaining - * unmatched patterns, then we can switch to just looking for the - * first matching pattern for improved speed. - */ - const matchesPattern = - unmatchedPatterns.size > 0 - ? matchers.reduce((previousValue, matcher) => { - const pathMatches = matcher.match(entry.path); - - /* - * We updated the unmatched patterns set only if the path - * matches and the file has a config. If the file has no - * config, that means there wasn't a match for the - * pattern so it should not be removed. - * - * Performance note: `getConfig()` aggressively caches - * results so there is no performance penalty for calling - * it multiple times with the same argument. - */ - if (pathMatches && config) { - unmatchedPatterns.delete(matcher.pattern); - } - - return pathMatches || previousValue; - }, false) - : matchers.some(matcher => matcher.match(entry.path)); - - return matchesPattern && config !== void 0; - }, - }); - - const filePaths = []; - - if (await hfs.isDirectory(basePath)) { - for await (const entry of walk) { - filePaths.push(path.resolve(basePath, entry.path)); - } - } - - // now check to see if we have any unmatched patterns - if (errorOnUnmatchedPattern && unmatchedPatterns.size > 0) { - throw new UnmatchedSearchPatternsError({ - basePath, - unmatchedPatterns: [...unmatchedPatterns].map(pattern => - relativeToPatterns.get(pattern), - ), - patterns, - rawPatterns, - }); - } - - return filePaths; + + if (patterns.length === 0) { + return []; + } + + /* + * In this section we are converting the patterns into Minimatch + * instances for performance reasons. Because we are doing the same + * matches repeatedly, it's best to compile those patterns once and + * reuse them multiple times. + * + * To do that, we convert any patterns with an absolute path into a + * relative path and normalize it to Posix-style slashes. We also keep + * track of the relative patterns to map them back to the original + * patterns, which we need in order to throw an error if there are any + * unmatched patterns. + */ + const relativeToPatterns = new Map(); + const matchers = patterns.map((pattern, i) => { + const patternToUse = path.isAbsolute(pattern) + ? normalizeToPosix(path.relative(basePath, pattern)) + : pattern; + + relativeToPatterns.set(patternToUse, patterns[i]); + + return new Minimatch(patternToUse, MINIMATCH_OPTIONS); + }); + + /* + * We track unmatched patterns because we may want to throw an error when + * they occur. To start, this set is initialized with all of the patterns. + * Every time a match occurs, the pattern is removed from the set, making + * it easy to tell if we have any unmatched patterns left at the end of + * search. + */ + const unmatchedPatterns = new Set([...relativeToPatterns.keys()]); + + const filePaths = (await new Promise((resolve, reject) => { + + let promiseRejected = false; + + /** + * Wraps a boolean-returning filter function. The wrapped function will reject the promise if an error occurs. + * @param {Function} filter A filter function to wrap. + * @returns {Function} A function similar to the wrapped filter that rejects the promise if an error occurs. + */ + function wrapFilter(filter) { + return (...args) => { + + // No need to run the filter if an error has been thrown. + if (!promiseRejected) { + try { + return filter(...args); + } catch (error) { + promiseRejected = true; + reject(error); + } + } + return false; + }; + } + + fswalk.walk( + basePath, + { + deepFilter: wrapFilter(entry => { + const relativePath = normalizeToPosix(path.relative(basePath, entry.path)); + const matchesPattern = matchers.some(matcher => matcher.match(relativePath, true)); + + return matchesPattern && !configs.isDirectoryIgnored(entry.path); + }), + entryFilter: wrapFilter(entry => { + const relativePath = normalizeToPosix(path.relative(basePath, entry.path)); + + // entries may be directories or files so filter out directories + if (entry.dirent.isDirectory()) { + return false; + } + + /* + * Optimization: We need to track when patterns are left unmatched + * and so we use `unmatchedPatterns` to do that. There is a bit of + * complexity here because the same file can be matched by more than + * one pattern. So, when we start, we actually need to test every + * pattern against every file. Once we know there are no remaining + * unmatched patterns, then we can switch to just looking for the + * first matching pattern for improved speed. + */ + const matchesPattern = unmatchedPatterns.size > 0 + ? matchers.reduce((previousValue, matcher) => { + const pathMatches = matcher.match(relativePath); + + /* + * We updated the unmatched patterns set only if the path + * matches and the file isn't ignored. If the file is + * ignored, that means there wasn't a match for the + * pattern so it should not be removed. + * + * Performance note: isFileIgnored() aggressively caches + * results so there is no performance penalty for calling + * it twice with the same argument. + */ + if (pathMatches && !configs.isFileIgnored(entry.path)) { + unmatchedPatterns.delete(matcher.pattern); + } + + return pathMatches || previousValue; + }, false) + : matchers.some(matcher => matcher.match(relativePath)); + + return matchesPattern && !configs.isFileIgnored(entry.path); + }) + }, + (error, entries) => { + + // If the promise is already rejected, calling `resolve` or `reject` will do nothing. + if (error) { + reject(error); + } else { + resolve(entries); + } + } + ); + })).map(entry => entry.path); + + // now check to see if we have any unmatched patterns + if (errorOnUnmatchedPattern && unmatchedPatterns.size > 0) { + throw new UnmatchedSearchPatternsError({ + basePath, + unmatchedPatterns: [...unmatchedPatterns].map( + pattern => relativeToPatterns.get(pattern) + ), + patterns, + rawPatterns + }); + } + + return filePaths; } /** @@ -390,113 +380,112 @@ async function globSearch({ * that were used in the original search. * @param {Array} options.rawPatterns An array of glob patterns * as the user inputted them. Used for errors. - * @param {Array} options.unmatchedPatterns A non-empty array of absolute path glob patterns + * @param {Array} options.unmatchedPatterns A non-empty array of glob patterns * that were unmatched in the original search. - * @returns {Promise} Always throws an error. + * @returns {void} Always throws an error. * @throws {NoFilesFoundError} If the first unmatched pattern * doesn't match any files even when there are no ignores. * @throws {AllFilesIgnoredError} If the first unmatched pattern * matches some files when there are no ignores. */ async function throwErrorForUnmatchedPatterns({ - basePath, - patterns, - rawPatterns, - unmatchedPatterns, + basePath, + patterns, + rawPatterns, + unmatchedPatterns }) { - const pattern = unmatchedPatterns[0]; - const rawPattern = rawPatterns[patterns.indexOf(pattern)]; - const patternHasMatch = await globMatch({ - basePath, - pattern, - }); + const pattern = unmatchedPatterns[0]; + const rawPattern = rawPatterns[patterns.indexOf(pattern)]; - if (patternHasMatch) { - throw new AllFilesIgnoredError(rawPattern); - } + const patternHasMatch = await globMatch({ + basePath, + pattern + }); - // if we get here there are truly no matches - throw new NoFilesFoundError(rawPattern, true); + if (patternHasMatch) { + throw new AllFilesIgnoredError(rawPattern); + } + + // if we get here there are truly no matches + throw new NoFilesFoundError(rawPattern, true); } /** * Performs multiple glob searches in parallel. * @param {Object} options The options for this function. * @param {Map} options.searches - * A map of absolute path glob patterns to match. - * @param {ConfigLoader|LegacyConfigLoader} options.configLoader The config loader to use for + * An array of glob patterns to match. + * @param {FlatConfigArray} options.configs The config array to use for * determining what to ignore. * @param {boolean} options.errorOnUnmatchedPattern Determines if an * unmatched glob pattern should throw an error. * @returns {Promise>} An array of matching file paths * or an empty array if there are no matches. */ -async function globMultiSearch({ - searches, - configLoader, - errorOnUnmatchedPattern, -}) { - /* - * For convenience, we normalized the search map into an array of objects. - * Next, we filter out all searches that have no patterns. This happens - * primarily for the cwd, which is prepopulated in the searches map as an - * optimization. However, if it has no patterns, it means all patterns - * occur outside of the cwd and we can safely filter out that search. - */ - const normalizedSearches = [...searches] - .map(([basePath, { patterns, rawPatterns }]) => ({ - basePath, - patterns, - rawPatterns, - })) - .filter(({ patterns }) => patterns.length > 0); - - const results = await Promise.allSettled( - normalizedSearches.map(({ basePath, patterns, rawPatterns }) => - globSearch({ - basePath, - patterns, - rawPatterns, - configLoader, - errorOnUnmatchedPattern, - }), - ), - ); - - /* - * The first loop handles errors from the glob searches. Since we can't - * use `await` inside `flatMap`, we process errors separately in this loop. - * This results in two iterations over `results`, but since the length is - * less than or equal to the number of globs and directories passed on the - * command line, the performance impact should be minimal. - */ - for (let i = 0; i < results.length; i++) { - const result = results[i]; - const currentSearch = normalizedSearches[i]; - - if (result.status === "fulfilled") { - continue; - } - - // if we make it here then there was an error - const error = result.reason; - - // unexpected errors should be re-thrown - if (!error.basePath) { - throw error; - } - - if (errorOnUnmatchedPattern) { - await throwErrorForUnmatchedPatterns({ - ...currentSearch, - unmatchedPatterns: error.unmatchedPatterns, - }); - } - } - - // second loop for `fulfilled` results - return results.flatMap(result => result.value); +async function globMultiSearch({ searches, configs, errorOnUnmatchedPattern }) { + + /* + * For convenience, we normalized the search map into an array of objects. + * Next, we filter out all searches that have no patterns. This happens + * primarily for the cwd, which is prepopulated in the searches map as an + * optimization. However, if it has no patterns, it means all patterns + * occur outside of the cwd and we can safely filter out that search. + */ + const normalizedSearches = [...searches].map( + ([basePath, { patterns, rawPatterns }]) => ({ basePath, patterns, rawPatterns }) + ).filter(({ patterns }) => patterns.length > 0); + + const results = await Promise.allSettled( + normalizedSearches.map( + ({ basePath, patterns, rawPatterns }) => globSearch({ + basePath, + patterns, + rawPatterns, + configs, + errorOnUnmatchedPattern + }) + ) + ); + + const filePaths = []; + + for (let i = 0; i < results.length; i++) { + + const result = results[i]; + const currentSearch = normalizedSearches[i]; + + if (result.status === "fulfilled") { + + // if the search was successful just add the results + if (result.value.length > 0) { + filePaths.push(...result.value); + } + + continue; + } + + // if we make it here then there was an error + const error = result.reason; + + // unexpected errors should be re-thrown + if (!error.basePath) { + throw error; + } + + if (errorOnUnmatchedPattern) { + + await throwErrorForUnmatchedPatterns({ + ...currentSearch, + unmatchedPatterns: error.unmatchedPatterns + }); + + } + + } + + return filePaths; + } /** @@ -506,7 +495,7 @@ async function globMultiSearch({ * @param {boolean} args.globInputPaths true to interpret glob patterns, * false to not interpret glob patterns. * @param {string} args.cwd The current working directory to find from. - * @param {ConfigLoader|LegacyConfigLoader} args.configLoader The config loader for the current run. + * @param {FlatConfigArray} args.configs The configs for the current run. * @param {boolean} args.errorOnUnmatchedPattern Determines if an unmatched pattern * should throw an error. * @returns {Promise>} The fully resolved file paths. @@ -514,137 +503,99 @@ async function globMultiSearch({ * @throws {NoFilesFoundError} If no files matched the given patterns. */ async function findFiles({ - patterns, - globInputPaths, - cwd, - configLoader, - errorOnUnmatchedPattern, + patterns, + globInputPaths, + cwd, + configs, + errorOnUnmatchedPattern }) { - const results = []; - const missingPatterns = []; - let globbyPatterns = []; - let rawPatterns = []; - const searches = new Map([ - [cwd, { patterns: globbyPatterns, rawPatterns: [] }], - ]); - - /* - * This part is a bit involved because we need to account for - * the different ways that the patterns can match directories. - * For each different way, we need to decide if we should look - * for a config file or just use the default config. (Directories - * without a config file always use the default config.) - * - * Here are the cases: - * - * 1. A directory is passed directly (e.g., "subdir"). In this case, we - * can assume that the user intends to lint this directory and we should - * not look for a config file in the parent directory, because the only - * reason to do that would be to ignore this directory (which we already - * know we don't want to do). Instead, we use the default config until we - * get to the directory that was passed, at which point we start looking - * for config files again. - * - * 2. A dot (".") or star ("*"). In this case, we want to read - * the config file in the current directory because the user is - * explicitly asking to lint the current directory. Note that "." - * will traverse into subdirectories while "*" will not. - * - * 3. A directory is passed in the form of "subdir/subsubdir". - * In this case, we don't want to look for a config file in the - * parent directory ("subdir"). We can skip looking for a config - * file until `entry.depth` is greater than 1 because there's no - * way that the pattern can match `entry.path` yet. - * - * 4. A directory glob pattern is passed (e.g., "subd*"). We want - * this case to act like case 2 because it's unclear whether or not - * any particular directory is meant to be traversed. - * - * 5. A recursive glob pattern is passed (e.g., "**"). We want this - * case to act like case 2. - */ - - // check to see if we have explicit files and directories - const filePaths = patterns.map(filePath => path.resolve(cwd, filePath)); - const stats = await Promise.all( - filePaths.map(filePath => fsp.stat(filePath).catch(() => {})), - ); - - const promises = []; - stats.forEach((stat, index) => { - const filePath = filePaths[index]; - const pattern = normalizeToPosix(patterns[index]); - - if (stat) { - // files are added directly to the list - if (stat.isFile()) { - results.push(filePath); - promises.push(configLoader.loadConfigArrayForFile(filePath)); - } - - // directories need extensions attached - if (stat.isDirectory()) { - if (!searches.has(filePath)) { - searches.set(filePath, { patterns: [], rawPatterns: [] }); - } - ({ patterns: globbyPatterns, rawPatterns } = - searches.get(filePath)); - - globbyPatterns.push(`${normalizeToPosix(filePath)}/**`); - rawPatterns.push(pattern); - } - - return; - } - - // save patterns for later use based on whether globs are enabled - if (globInputPaths && isGlobPattern(pattern)) { - /* - * We are grouping patterns by their glob parent. This is done to - * make it easier to determine when a config file should be loaded. - */ - - const basePath = path.resolve(cwd, globParent(pattern)); - - if (!searches.has(basePath)) { - searches.set(basePath, { patterns: [], rawPatterns: [] }); - } - ({ patterns: globbyPatterns, rawPatterns } = - searches.get(basePath)); - - globbyPatterns.push(filePath); - rawPatterns.push(pattern); - } else { - missingPatterns.push(pattern); - } - }); - - // there were patterns that didn't match anything, tell the user - if (errorOnUnmatchedPattern && missingPatterns.length) { - throw new NoFilesFoundError(missingPatterns[0], globInputPaths); - } - - // now we are safe to do the search - promises.push( - globMultiSearch({ - searches, - configLoader, - errorOnUnmatchedPattern, - }), - ); - const globbyResults = (await Promise.all(promises)).at(-1); - - return [...new Set([...results, ...globbyResults])]; -} -/** - * Return the absolute path of a file named `"__placeholder__.js"` in a given directory. - * This is used as a replacement for a missing file path. - * @param {string} cwd An absolute directory path. - * @returns {string} The absolute path of a file named `"__placeholder__.js"` in the given directory. - */ -function getPlaceholderPath(cwd) { - return path.join(cwd, "__placeholder__.js"); + const results = []; + const missingPatterns = []; + let globbyPatterns = []; + let rawPatterns = []; + const searches = new Map([[cwd, { patterns: globbyPatterns, rawPatterns: [] }]]); + + // check to see if we have explicit files and directories + const filePaths = patterns.map(filePath => path.resolve(cwd, filePath)); + const stats = await Promise.all( + filePaths.map( + filePath => fsp.stat(filePath).catch(() => { }) + ) + ); + + stats.forEach((stat, index) => { + + const filePath = filePaths[index]; + const pattern = normalizeToPosix(patterns[index]); + + if (stat) { + + // files are added directly to the list + if (stat.isFile()) { + results.push(filePath); + } + + // directories need extensions attached + if (stat.isDirectory()) { + + // group everything in cwd together and split out others + if (isPathInside(filePath, cwd)) { + ({ patterns: globbyPatterns, rawPatterns } = searches.get(cwd)); + } else { + if (!searches.has(filePath)) { + searches.set(filePath, { patterns: [], rawPatterns: [] }); + } + ({ patterns: globbyPatterns, rawPatterns } = searches.get(filePath)); + } + + globbyPatterns.push(`${normalizeToPosix(filePath)}/**`); + rawPatterns.push(pattern); + } + + return; + } + + // save patterns for later use based on whether globs are enabled + if (globInputPaths && isGlobPattern(pattern)) { + + const basePath = path.resolve(cwd, globParent(pattern)); + + // group in cwd if possible and split out others + if (isPathInside(basePath, cwd)) { + ({ patterns: globbyPatterns, rawPatterns } = searches.get(cwd)); + } else { + if (!searches.has(basePath)) { + searches.set(basePath, { patterns: [], rawPatterns: [] }); + } + ({ patterns: globbyPatterns, rawPatterns } = searches.get(basePath)); + } + + globbyPatterns.push(filePath); + rawPatterns.push(pattern); + } else { + missingPatterns.push(pattern); + } + }); + + // there were patterns that didn't match anything, tell the user + if (errorOnUnmatchedPattern && missingPatterns.length) { + throw new NoFilesFoundError(missingPatterns[0], globInputPaths); + } + + // now we are safe to do the search + const globbyResults = await globMultiSearch({ + searches, + configs, + errorOnUnmatchedPattern + }); + + return [ + ...new Set([ + ...results, + ...globbyResults.map(filePath => path.resolve(filePath)) + ]) + ]; } //----------------------------------------------------------------------------- @@ -658,120 +609,58 @@ function getPlaceholderPath(cwd) { * @private */ function isErrorMessage(message) { - return message.severity === 2; + return message.severity === 2; } /** * Returns result with warning by ignore settings - * @param {string} filePath Absolute file path of checked code + * @param {string} filePath File path of checked code * @param {string} baseDir Absolute path of base directory - * @param {"ignored"|"external"|"unconfigured"} configStatus A status that determines why the file is ignored * @returns {LintResult} Result with single warning * @private */ -function createIgnoreResult(filePath, baseDir, configStatus) { - let message; - - switch (configStatus) { - case "external": - message = "File ignored because outside of base path."; - break; - case "unconfigured": - message = - "File ignored because no matching configuration was supplied."; - break; - default: - { - const isInNodeModules = - baseDir && - path - .dirname(path.relative(baseDir, filePath)) - .split(path.sep) - .includes("node_modules"); - - if (isInNodeModules) { - message = - 'File ignored by default because it is located under the node_modules directory. Use ignore pattern "!**/node_modules/" to disable file ignore settings or use "--no-warn-ignored" to suppress this warning.'; - } else { - message = - 'File ignored because of a matching ignore pattern. Use "--no-ignore" to disable file ignore settings or use "--no-warn-ignored" to suppress this warning.'; - } - } - break; - } - - return { - filePath, - messages: [ - { - ruleId: null, - fatal: false, - severity: 1, - message, - nodeType: null, - }, - ], - suppressedMessages: [], - errorCount: 0, - warningCount: 1, - fatalErrorCount: 0, - fixableErrorCount: 0, - fixableWarningCount: 0, - }; -} - -/** - * It will calculate the error and warning count for collection of messages per file - * @param {LintMessage[]} messages Collection of messages - * @returns {Object} Contains the stats - * @private - */ -function calculateStatsPerFile(messages) { - const stat = { - errorCount: 0, - fatalErrorCount: 0, - warningCount: 0, - fixableErrorCount: 0, - fixableWarningCount: 0, - }; - - for (let i = 0; i < messages.length; i++) { - const message = messages[i]; - - if (message.fatal || message.severity === 2) { - stat.errorCount++; - if (message.fatal) { - stat.fatalErrorCount++; - } - if (message.fix) { - stat.fixableErrorCount++; - } - } else { - stat.warningCount++; - if (message.fix) { - stat.fixableWarningCount++; - } - } - } - return stat; +function createIgnoreResult(filePath, baseDir) { + let message; + const isInNodeModules = baseDir && path.dirname(path.relative(baseDir, filePath)).split(path.sep).includes("node_modules"); + + if (isInNodeModules) { + message = "File ignored by default because it is located under the node_modules directory. Use ignore pattern \"!**/node_modules/\" to disable file ignore settings or use \"--no-warn-ignored\" to suppress this warning."; + } else { + message = "File ignored because of a matching ignore pattern. Use \"--no-ignore\" to disable file ignore settings or use \"--no-warn-ignored\" to suppress this warning."; + } + + return { + filePath: path.resolve(filePath), + messages: [ + { + ruleId: null, + fatal: false, + severity: 1, + message, + nodeType: null + } + ], + suppressedMessages: [], + errorCount: 0, + warningCount: 1, + fatalErrorCount: 0, + fixableErrorCount: 0, + fixableWarningCount: 0 + }; } //----------------------------------------------------------------------------- // Options-related Helpers //----------------------------------------------------------------------------- + /** * Check if a given value is a valid fix type or not. * @param {any} x The value to check. * @returns {boolean} `true` if `x` is valid fix type. */ function isFixType(x) { - return ( - x === "directive" || - x === "problem" || - x === "suggestion" || - x === "layout" - ); + return x === "directive" || x === "problem" || x === "suggestion" || x === "layout"; } /** @@ -780,240 +669,171 @@ function isFixType(x) { * @returns {boolean} `true` if `x` is an array of fix types. */ function isFixTypeArray(x) { - return Array.isArray(x) && x.every(isFixType); + return Array.isArray(x) && x.every(isFixType); } /** * The error for invalid options. */ class ESLintInvalidOptionsError extends Error { - constructor(messages) { - super(`Invalid Options:\n- ${messages.join("\n- ")}`); - this.code = "ESLINT_INVALID_OPTIONS"; - Error.captureStackTrace(this, ESLintInvalidOptionsError); - } + constructor(messages) { + super(`Invalid Options:\n- ${messages.join("\n- ")}`); + this.code = "ESLINT_INVALID_OPTIONS"; + Error.captureStackTrace(this, ESLintInvalidOptionsError); + } } /** * Validates and normalizes options for the wrapped CLIEngine instance. - * @param {ESLintOptions} options The options to process. + * @param {FlatESLintOptions} options The options to process. * @throws {ESLintInvalidOptionsError} If of any of a variety of type errors. - * @returns {ESLintOptions} The normalized options. + * @returns {FlatESLintOptions} The normalized options. */ function processOptions({ - allowInlineConfig = true, // ← we cannot use `overrideConfig.noInlineConfig` instead because `allowInlineConfig` has side-effect that suppress warnings that show inline configs are ignored. - baseConfig = null, - cache = false, - cacheLocation = ".eslintcache", - cacheStrategy = "metadata", - concurrency = "off", - cwd = process.cwd(), - errorOnUnmatchedPattern = true, - fix = false, - fixTypes = null, // ← should be null by default because if it's an array then it suppresses rules that don't have the `meta.type` property. - flags = [], - globInputPaths = true, - ignore = true, - ignorePatterns = null, - overrideConfig = null, - overrideConfigFile = null, - plugins = {}, - stats = false, - warnIgnored = true, - passOnNoPatterns = false, - ruleFilter = () => true, - ...unknownOptions + allowInlineConfig = true, // ← we cannot use `overrideConfig.noInlineConfig` instead because `allowInlineConfig` has side-effect that suppress warnings that show inline configs are ignored. + baseConfig = null, + cache = false, + cacheLocation = ".eslintcache", + cacheStrategy = "metadata", + cwd = process.cwd(), + errorOnUnmatchedPattern = true, + fix = false, + fixTypes = null, // ← should be null by default because if it's an array then it suppresses rules that don't have the `meta.type` property. + globInputPaths = true, + ignore = true, + ignorePatterns = null, + overrideConfig = null, + overrideConfigFile = null, + plugins = {}, + warnIgnored = true, + ...unknownOptions }) { - const errors = []; - const unknownOptionKeys = Object.keys(unknownOptions); - - if (unknownOptionKeys.length >= 1) { - errors.push(`Unknown options: ${unknownOptionKeys.join(", ")}`); - if (unknownOptionKeys.includes("cacheFile")) { - errors.push( - "'cacheFile' has been removed. Please use the 'cacheLocation' option instead.", - ); - } - if (unknownOptionKeys.includes("configFile")) { - errors.push( - "'configFile' has been removed. Please use the 'overrideConfigFile' option instead.", - ); - } - if (unknownOptionKeys.includes("envs")) { - errors.push("'envs' has been removed."); - } - if (unknownOptionKeys.includes("extensions")) { - errors.push("'extensions' has been removed."); - } - if (unknownOptionKeys.includes("resolvePluginsRelativeTo")) { - errors.push("'resolvePluginsRelativeTo' has been removed."); - } - if (unknownOptionKeys.includes("globals")) { - errors.push( - "'globals' has been removed. Please use the 'overrideConfig.languageOptions.globals' option instead.", - ); - } - if (unknownOptionKeys.includes("ignorePath")) { - errors.push("'ignorePath' has been removed."); - } - if (unknownOptionKeys.includes("ignorePattern")) { - errors.push( - "'ignorePattern' has been removed. Please use the 'overrideConfig.ignorePatterns' option instead.", - ); - } - if (unknownOptionKeys.includes("parser")) { - errors.push( - "'parser' has been removed. Please use the 'overrideConfig.languageOptions.parser' option instead.", - ); - } - if (unknownOptionKeys.includes("parserOptions")) { - errors.push( - "'parserOptions' has been removed. Please use the 'overrideConfig.languageOptions.parserOptions' option instead.", - ); - } - if (unknownOptionKeys.includes("rules")) { - errors.push( - "'rules' has been removed. Please use the 'overrideConfig.rules' option instead.", - ); - } - if (unknownOptionKeys.includes("rulePaths")) { - errors.push( - "'rulePaths' has been removed. Please define your rules using plugins.", - ); - } - if (unknownOptionKeys.includes("reportUnusedDisableDirectives")) { - errors.push( - "'reportUnusedDisableDirectives' has been removed. Please use the 'overrideConfig.linterOptions.reportUnusedDisableDirectives' option instead.", - ); - } - } - if (typeof allowInlineConfig !== "boolean") { - errors.push("'allowInlineConfig' must be a boolean."); - } - if (typeof baseConfig !== "object") { - errors.push("'baseConfig' must be an object or null."); - } - if (typeof cache !== "boolean") { - errors.push("'cache' must be a boolean."); - } - if (!isNonEmptyString(cacheLocation)) { - errors.push("'cacheLocation' must be a non-empty string."); - } - if (cacheStrategy !== "metadata" && cacheStrategy !== "content") { - errors.push('\'cacheStrategy\' must be any of "metadata", "content".'); - } - if ( - concurrency !== "off" && - concurrency !== "auto" && - !isPositiveInteger(concurrency) - ) { - errors.push( - '\'concurrency\' must be a positive integer, "auto", or "off".', - ); - } - if (!isNonEmptyString(cwd) || !path.isAbsolute(cwd)) { - errors.push("'cwd' must be an absolute path."); - } - if (typeof errorOnUnmatchedPattern !== "boolean") { - errors.push("'errorOnUnmatchedPattern' must be a boolean."); - } - if (typeof fix !== "boolean" && typeof fix !== "function") { - errors.push("'fix' must be a boolean or a function."); - } - if (fixTypes !== null && !isFixTypeArray(fixTypes)) { - errors.push( - '\'fixTypes\' must be an array of any of "directive", "problem", "suggestion", and "layout".', - ); - } - if (!isEmptyArrayOrArrayOfNonEmptyString(flags)) { - errors.push("'flags' must be an array of non-empty strings."); - } - if (typeof globInputPaths !== "boolean") { - errors.push("'globInputPaths' must be a boolean."); - } - if (typeof ignore !== "boolean") { - errors.push("'ignore' must be a boolean."); - } - if ( - !isEmptyArrayOrArrayOfNonEmptyString(ignorePatterns) && - ignorePatterns !== null - ) { - errors.push( - "'ignorePatterns' must be an array of non-empty strings or null.", - ); - } - if (typeof overrideConfig !== "object") { - errors.push("'overrideConfig' must be an object or null."); - } - if ( - !isNonEmptyString(overrideConfigFile) && - overrideConfigFile !== null && - overrideConfigFile !== true - ) { - errors.push( - "'overrideConfigFile' must be a non-empty string, null, or true.", - ); - } - if (typeof passOnNoPatterns !== "boolean") { - errors.push("'passOnNoPatterns' must be a boolean."); - } - if (typeof plugins !== "object") { - errors.push("'plugins' must be an object or null."); - } else if (plugins !== null && Object.keys(plugins).includes("")) { - errors.push("'plugins' must not include an empty string."); - } - if (Array.isArray(plugins)) { - errors.push( - "'plugins' doesn't add plugins to configuration to load. Please use the 'overrideConfig.plugins' option instead.", - ); - } - if (typeof stats !== "boolean") { - errors.push("'stats' must be a boolean."); - } - if (typeof warnIgnored !== "boolean") { - errors.push("'warnIgnored' must be a boolean."); - } - if (typeof ruleFilter !== "function") { - errors.push("'ruleFilter' must be a function."); - } - if (errors.length > 0) { - throw new ESLintInvalidOptionsError(errors); - } - - return { - allowInlineConfig, - baseConfig, - cache, - cacheLocation, - cacheStrategy, - concurrency, - - // when overrideConfigFile is true that means don't do config file lookup - configFile: overrideConfigFile === true ? false : overrideConfigFile, - overrideConfig, - cwd: path.normalize(cwd), - errorOnUnmatchedPattern, - fix, - fixTypes, - flags: [...flags], - globInputPaths, - ignore, - ignorePatterns, - stats, - passOnNoPatterns, - warnIgnored, - ruleFilter, - }; + const errors = []; + const unknownOptionKeys = Object.keys(unknownOptions); + + if (unknownOptionKeys.length >= 1) { + errors.push(`Unknown options: ${unknownOptionKeys.join(", ")}`); + if (unknownOptionKeys.includes("cacheFile")) { + errors.push("'cacheFile' has been removed. Please use the 'cacheLocation' option instead."); + } + if (unknownOptionKeys.includes("configFile")) { + errors.push("'configFile' has been removed. Please use the 'overrideConfigFile' option instead."); + } + if (unknownOptionKeys.includes("envs")) { + errors.push("'envs' has been removed."); + } + if (unknownOptionKeys.includes("extensions")) { + errors.push("'extensions' has been removed."); + } + if (unknownOptionKeys.includes("resolvePluginsRelativeTo")) { + errors.push("'resolvePluginsRelativeTo' has been removed."); + } + if (unknownOptionKeys.includes("globals")) { + errors.push("'globals' has been removed. Please use the 'overrideConfig.languageOptions.globals' option instead."); + } + if (unknownOptionKeys.includes("ignorePath")) { + errors.push("'ignorePath' has been removed."); + } + if (unknownOptionKeys.includes("ignorePattern")) { + errors.push("'ignorePattern' has been removed. Please use the 'overrideConfig.ignorePatterns' option instead."); + } + if (unknownOptionKeys.includes("parser")) { + errors.push("'parser' has been removed. Please use the 'overrideConfig.languageOptions.parser' option instead."); + } + if (unknownOptionKeys.includes("parserOptions")) { + errors.push("'parserOptions' has been removed. Please use the 'overrideConfig.languageOptions.parserOptions' option instead."); + } + if (unknownOptionKeys.includes("rules")) { + errors.push("'rules' has been removed. Please use the 'overrideConfig.rules' option instead."); + } + if (unknownOptionKeys.includes("rulePaths")) { + errors.push("'rulePaths' has been removed. Please define your rules using plugins."); + } + if (unknownOptionKeys.includes("reportUnusedDisableDirectives")) { + errors.push("'reportUnusedDisableDirectives' has been removed. Please use the 'overrideConfig.linterOptions.reportUnusedDisableDirectives' option instead."); + } + } + if (typeof allowInlineConfig !== "boolean") { + errors.push("'allowInlineConfig' must be a boolean."); + } + if (typeof baseConfig !== "object") { + errors.push("'baseConfig' must be an object or null."); + } + if (typeof cache !== "boolean") { + errors.push("'cache' must be a boolean."); + } + if (!isNonEmptyString(cacheLocation)) { + errors.push("'cacheLocation' must be a non-empty string."); + } + if ( + cacheStrategy !== "metadata" && + cacheStrategy !== "content" + ) { + errors.push("'cacheStrategy' must be any of \"metadata\", \"content\"."); + } + if (!isNonEmptyString(cwd) || !path.isAbsolute(cwd)) { + errors.push("'cwd' must be an absolute path."); + } + if (typeof errorOnUnmatchedPattern !== "boolean") { + errors.push("'errorOnUnmatchedPattern' must be a boolean."); + } + if (typeof fix !== "boolean" && typeof fix !== "function") { + errors.push("'fix' must be a boolean or a function."); + } + if (fixTypes !== null && !isFixTypeArray(fixTypes)) { + errors.push("'fixTypes' must be an array of any of \"directive\", \"problem\", \"suggestion\", and \"layout\"."); + } + if (typeof globInputPaths !== "boolean") { + errors.push("'globInputPaths' must be a boolean."); + } + if (typeof ignore !== "boolean") { + errors.push("'ignore' must be a boolean."); + } + if (!isArrayOfNonEmptyString(ignorePatterns) && ignorePatterns !== null) { + errors.push("'ignorePatterns' must be an array of non-empty strings or null."); + } + if (typeof overrideConfig !== "object") { + errors.push("'overrideConfig' must be an object or null."); + } + if (!isNonEmptyString(overrideConfigFile) && overrideConfigFile !== null && overrideConfigFile !== true) { + errors.push("'overrideConfigFile' must be a non-empty string, null, or true."); + } + if (typeof plugins !== "object") { + errors.push("'plugins' must be an object or null."); + } else if (plugins !== null && Object.keys(plugins).includes("")) { + errors.push("'plugins' must not include an empty string."); + } + if (Array.isArray(plugins)) { + errors.push("'plugins' doesn't add plugins to configuration to load. Please use the 'overrideConfig.plugins' option instead."); + } + if (typeof warnIgnored !== "boolean") { + errors.push("'warnIgnored' must be a boolean."); + } + if (errors.length > 0) { + throw new ESLintInvalidOptionsError(errors); + } + + return { + allowInlineConfig, + baseConfig, + cache, + cacheLocation, + cacheStrategy, + + // when overrideConfigFile is true that means don't do config file lookup + configFile: overrideConfigFile === true ? false : overrideConfigFile, + overrideConfig, + cwd: path.normalize(cwd), + errorOnUnmatchedPattern, + fix, + fixTypes, + globInputPaths, + ignore, + ignorePatterns, + warnIgnored + }; } -/** - * Loads ESLint constructor options from an options module. - * @param {string} optionsURL The URL string of the options module to load. - * @returns {Promise} ESLint constructor options. - */ -async function loadOptionsFromModule(optionsURL) { - return (await import(optionsURL)).default; -} //----------------------------------------------------------------------------- // Cache-related helpers @@ -1027,439 +847,86 @@ async function loadOptionsFromModule(optionsURL) { * if cacheFile points to a file or looks like a file then in will just use that file * @param {string} cacheFile The name of file to be used to store the cache * @param {string} cwd Current working directory - * @param {Object} options The options - * @param {string} [options.prefix] The prefix to use for the cache file * @returns {string} the resolved path to the cache file */ -function getCacheFile(cacheFile, cwd, { prefix = ".cache_" } = {}) { - /* - * make sure the path separators are normalized for the environment/os - * keeping the trailing path separator if present - */ - const normalizedCacheFile = path.normalize(cacheFile); - - const resolvedCacheFile = path.resolve(cwd, normalizedCacheFile); - const looksLikeADirectory = normalizedCacheFile.slice(-1) === path.sep; - - /** - * return the name for the cache file in case the provided parameter is a directory - * @returns {string} the resolved path to the cacheFile - */ - function getCacheFileForDirectory() { - return path.join(resolvedCacheFile, `${prefix}${hash(cwd)}`); - } - - let fileStats; - - try { - fileStats = fs.lstatSync(resolvedCacheFile); - } catch { - fileStats = null; - } - - /* - * in case the file exists we need to verify if the provided path - * is a directory or a file. If it is a directory we want to create a file - * inside that directory - */ - if (fileStats) { - /* - * is a directory or is a file, but the original file the user provided - * looks like a directory but `path.resolve` removed the `last path.sep` - * so we need to still treat this like a directory - */ - if (fileStats.isDirectory() || looksLikeADirectory) { - return getCacheFileForDirectory(); - } - - // is file so just use that file - return resolvedCacheFile; - } - - /* - * here we known the file or directory doesn't exist, - * so we will try to infer if its a directory if it looks like a directory - * for the current operating system. - */ - - // if the last character passed is a path separator we assume is a directory - if (looksLikeADirectory) { - return getCacheFileForDirectory(); - } - - return resolvedCacheFile; -} - -/** - * Creates a new lint result cache. - * @param {ESLintOptions} eslintOptions The processed ESLint options. - * @param {string} cacheFilePath The path to the cache file. - * @returns {?LintResultCache} A new lint result cache or `null`. - */ -function createLintResultCache({ cache, cacheStrategy }, cacheFilePath) { - return cache ? new LintResultCache(cacheFilePath, cacheStrategy) : null; -} - -//----------------------------------------------------------------------------- -// Lint helpers -//----------------------------------------------------------------------------- - -/** - * Checks whether a message's rule type should be fixed. - * @param {LintMessage} message The message to check. - * @param {CalculatedConfig} config The config for the file that generated the message. - * @param {string[]} fixTypes An array of fix types to check. - * @returns {boolean} Whether the message should be fixed. - */ -function shouldMessageBeFixed(message, config, fixTypes) { - if (!message.ruleId) { - return fixTypes.has("directive"); - } - - const rule = message.ruleId && config.getRuleDefinition(message.ruleId); - - return Boolean(rule && rule.meta && fixTypes.has(rule.meta.type)); -} - -/** - * Creates a fixer function based on the provided fix, fixTypesSet, and config. - * @param {Function|boolean} fix The original fix option. - * @param {Set} fixTypesSet A set of fix types to filter messages for fixing. - * @param {CalculatedConfig} config The config for the file that generated the message. - * @returns {Function|boolean} The fixer function or the original fix value. - */ -function getFixerForFixTypes(fix, fixTypesSet, config) { - if (!fix || !fixTypesSet) { - return fix; - } +function getCacheFile(cacheFile, cwd) { - const originalFix = typeof fix === "function" ? fix : () => true; + /* + * make sure the path separators are normalized for the environment/os + * keeping the trailing path separator if present + */ + const normalizedCacheFile = path.normalize(cacheFile); - return message => - shouldMessageBeFixed(message, config, fixTypesSet) && - originalFix(message); -} + const resolvedCacheFile = path.resolve(cwd, normalizedCacheFile); + const looksLikeADirectory = normalizedCacheFile.slice(-1) === path.sep; -/** - * Processes a source code using ESLint. - * @param {Object} config The config object. - * @param {string} config.text The source code to verify. - * @param {string} config.cwd The path to the current working directory. - * @param {string|undefined} config.filePath The path to the file of `text`. If this is undefined, it uses ``. - * @param {FlatConfigArray} config.configs The config. - * @param {boolean} config.fix If `true` then it does fix. - * @param {boolean} config.allowInlineConfig If `true` then it uses directive comments. - * @param {Function} config.ruleFilter A predicate function to filter which rules should be run. - * @param {boolean} config.stats If `true`, then if reports extra statistics with the lint results. - * @param {Linter} config.linter The linter instance to verify. - * @returns {LintResult} The result of linting. - * @private - */ -function verifyText({ - text, - cwd, - filePath: providedFilePath, - configs, - fix, - allowInlineConfig, - ruleFilter, - stats, - linter, -}) { - const startTime = hrtimeBigint(); - - const filePath = providedFilePath || ""; - - /* - * Verify. - * `config.extractConfig(filePath)` requires an absolute path, but `linter` - * doesn't know CWD, so it gives `linter` an absolute path always. - */ - const filePathToVerify = - filePath === "" ? getPlaceholderPath(cwd) : filePath; - const { fixed, messages, output } = linter.verifyAndFix(text, configs, { - allowInlineConfig, - filename: filePathToVerify, - fix, - ruleFilter, - stats, - - /** - * Check if the linter should adopt a given code block or not. - * @param {string} blockFilename The virtual filename of a code block. - * @returns {boolean} `true` if the linter should adopt the code block. - */ - filterCodeBlock(blockFilename) { - return configs.getConfig(blockFilename) !== void 0; - }, - }); - - // Tweak and return. - const result = { - filePath: filePath === "" ? filePath : path.resolve(filePath), - messages, - suppressedMessages: linter.getSuppressedMessages(), - ...calculateStatsPerFile(messages), - }; - - if (fixed) { - result.output = output; - } - - if ( - result.errorCount + result.warningCount > 0 && - typeof result.output === "undefined" - ) { - result.source = text; - } - - if (stats) { - result.stats = { - times: linter.getTimes(), - fixPasses: linter.getFixPassCount(), - }; - } - - const endTime = hrtimeBigint(); - debug('File "%s" linted in %t', filePath, endTime - startTime); - - return result; -} + /** + * return the name for the cache file in case the provided parameter is a directory + * @returns {string} the resolved path to the cacheFile + */ + function getCacheFileForDirectory() { + return path.join(resolvedCacheFile, `.cache_${hash(cwd)}`); + } -/** - * Lints a single file. - * @param {string} filePath File path to lint. - * @param {FlatConfigArray} configs The config array for the file. - * @param {ESLintOptions} eslintOptions The processed ESLint options. - * @param {Linter} linter The linter instance to use. - * @param {?LintResultCache} lintResultCache The result cache or `null`. - * @param {?{ duration: bigint; }} readFileCounter Used to keep track of the time spent reading files. - * @param {Retrier} [retrier] Used to retry linting on certain errors. - * @param {AbortController} [controller] Used to stop linting when an error occurs. - * @returns {Promise} The lint result. - */ -async function lintFile( - filePath, - configs, - eslintOptions, - linter, - lintResultCache, - readFileCounter, - retrier, - controller, -) { - const config = configs.getConfig(filePath); - const { - allowInlineConfig, - cwd, - fix, - fixTypes, - ruleFilter, - stats, - warnIgnored, - } = eslintOptions; - const fixTypesSet = fixTypes ? new Set(fixTypes) : null; - - /* - * If a filename was entered that cannot be matched - * to a config, then notify the user. - */ - if (!config) { - if (warnIgnored) { - const configStatus = configs.getConfigStatus(filePath); - - return createIgnoreResult(filePath, cwd, configStatus); - } - - return void 0; - } - - // Skip if there is cached result. - if (lintResultCache) { - const cachedResult = lintResultCache.getCachedLintResults( - filePath, - config, - ); - - if (cachedResult) { - const hadMessages = - cachedResult.messages && cachedResult.messages.length > 0; - - if (hadMessages && fix) { - debug(`Reprocessing cached file to allow autofix: ${filePath}`); - } else { - debug(`Skipping file since it hasn't changed: ${filePath}`); - return cachedResult; - } - } - } - - // set up fixer for fixTypes if necessary - const fixer = getFixerForFixTypes(fix, fixTypesSet, config); - - /** - * Reads the file and lints its content. - * @returns {Promise} A lint result. - */ - async function readAndVerifyFile() { - const readFileEnterTime = hrtimeBigint(); - const text = await fsp.readFile(filePath, { - encoding: "utf8", - signal: controller?.signal, - }); - const readFileExitTime = hrtimeBigint(); - const readFileDuration = readFileExitTime - readFileEnterTime; - debug('File "%s" read in %t', filePath, readFileDuration); - if (readFileCounter) { - readFileCounter.duration += readFileDuration; - } - - // fail immediately if an error occurred in another file - controller?.signal.throwIfAborted(); - - // do the linting - return verifyText({ - text, - filePath, - configs, - cwd, - fix: fixer, - allowInlineConfig, - ruleFilter, - stats, - linter, - }); - } - - // Use the retrier if provided, otherwise just call the function. - const readAndVerifyFilePromise = retrier - ? retrier.retry(readAndVerifyFile, { signal: controller?.signal }) - : readAndVerifyFile(); - - return readAndVerifyFilePromise.catch(error => { - controller?.abort(error); - throw error; - }); -} + let fileStats; -/** - * Retrieves flags from the environment variable ESLINT_FLAGS. - * @param {string[]} flags The flags defined via the API. - * @returns {string[]} The merged flags to use. - */ -function mergeEnvironmentFlags(flags) { - if (!process.env.ESLINT_FLAGS) { - return flags; - } + try { + fileStats = fs.lstatSync(resolvedCacheFile); + } catch { + fileStats = null; + } - const envFlags = process.env.ESLINT_FLAGS.trim().split(/\s*,\s*/gu); - return Array.from(new Set([...envFlags, ...flags])); -} -/** - * Creates a new linter instance. - * @param {ESLintOptions} eslintOptions The processed ESLint options. - * @param {WarningService} warningService The warning service to use. - * @returns {Linter} The linter instance. - */ -function createLinter({ cwd, flags }, warningService) { - return new Linter({ - configType: "flat", - cwd, - flags: mergeEnvironmentFlags(flags), - warningService, - }); -} + /* + * in case the file exists we need to verify if the provided path + * is a directory or a file. If it is a directory we want to create a file + * inside that directory + */ + if (fileStats) { -/** - * Creates default configs with the specified plugins. - * @param {Record | undefined} optionPlugins The plugins specified in the ESLint options. - * @returns {Config[]} The default configs. - */ -function createDefaultConfigs(optionPlugins) { - const defaultConfigs = []; + /* + * is a directory or is a file, but the original file the user provided + * looks like a directory but `path.resolve` removed the `last path.sep` + * so we need to still treat this like a directory + */ + if (fileStats.isDirectory() || looksLikeADirectory) { + return getCacheFileForDirectory(); + } - // Add plugins - if (optionPlugins) { - const plugins = {}; + // is file so just use that file + return resolvedCacheFile; + } - for (const [pluginName, plugin] of Object.entries(optionPlugins)) { - plugins[getShorthandName(pluginName, "eslint-plugin")] = plugin; - } + /* + * here we known the file or directory doesn't exist, + * so we will try to infer if its a directory if it looks like a directory + * for the current operating system. + */ - defaultConfigs.push({ plugins }); - } + // if the last character passed is a path separator we assume is a directory + if (looksLikeADirectory) { + return getCacheFileForDirectory(); + } - return defaultConfigs; + return resolvedCacheFile; } -/** - * Creates a config loader. - * @param {ESLintOptions} eslintOptions The processed ESLint options. - * @param {Config[]} defaultConfigs The default configs. - * @param {Linter} linter The linter instance. - * @param {WarningService} warningService The warning service to use. - * @returns {ConfigLoader} The config loader. - */ -function createConfigLoader( - { - cwd, - baseConfig, - overrideConfig, - configFile, - ignore: ignoreEnabled, - ignorePatterns, - }, - defaultConfigs, - linter, - warningService, -) { - const configLoaderOptions = { - cwd, - baseConfig, - overrideConfig, - configFile, - ignoreEnabled, - ignorePatterns, - defaultConfigs, - hasUnstableNativeNodeJsTSConfigFlag: linter.hasFlag( - "unstable_native_nodejs_ts_config", - ), - warningService, - }; - - return linter.hasFlag("v10_config_lookup_from_file") - ? new ConfigLoader(configLoaderOptions) - : new LegacyConfigLoader(configLoaderOptions); -} //----------------------------------------------------------------------------- // Exports //----------------------------------------------------------------------------- module.exports = { - createDebug, - - findFiles, - - isNonEmptyString, - isArrayOfNonEmptyString, + isGlobPattern, + findFiles, - createIgnoreResult, - isErrorMessage, - calculateStatsPerFile, - getPlaceholderPath, + isNonEmptyString, + isArrayOfNonEmptyString, - processOptions, - loadOptionsFromModule, + createIgnoreResult, + isErrorMessage, - getCacheFile, - createLintResultCache, + processOptions, - getFixerForFixTypes, - verifyText, - lintFile, - createLinter, - createDefaultConfigs, - createConfigLoader, + getCacheFile }; diff --git a/node_modules/eslint/lib/eslint/eslint.js b/node_modules/eslint/lib/eslint/eslint.js index 92e90b3e2..7085d5a4d 100644 --- a/node_modules/eslint/lib/eslint/eslint.js +++ b/node_modules/eslint/lib/eslint/eslint.js @@ -1,6 +1,7 @@ /** - * @fileoverview Main class using flat config - * @author Nicholas C. Zakas + * @fileoverview Main API Class + * @author Kai Cataldo + * @author Toru Nagashima */ "use strict"; @@ -9,1354 +10,698 @@ // Requirements //------------------------------------------------------------------------------ -const { existsSync } = require("node:fs"); -const fs = require("node:fs/promises"); -const os = require("node:os"); -const path = require("node:path"); -const { pathToFileURL } = require("node:url"); -const { SHARE_ENV, Worker } = require("node:worker_threads"); -const { version } = require("../../package.json"); -const { defaultConfig } = require("../config/default-config"); -const timing = require("../linter/timing"); - +const path = require("path"); +const fs = require("fs"); +const { promisify } = require("util"); +const { CLIEngine, getCLIEngineInternalSlots } = require("../cli-engine/cli-engine"); +const BuiltinRules = require("../rules"); const { - createDebug, - - findFiles, - getCacheFile, - - isNonEmptyString, - isArrayOfNonEmptyString, - - createIgnoreResult, - isErrorMessage, - getPlaceholderPath, - - processOptions, - loadOptionsFromModule, - - getFixerForFixTypes, - verifyText, - lintFile, - createLinter, - createLintResultCache, - createDefaultConfigs, - createConfigLoader, -} = require("./eslint-helpers"); -const { Retrier } = require("@humanwhocodes/retry"); -const { ConfigLoader } = require("../config/config-loader"); -const { WarningService } = require("../services/warning-service"); -const { Config } = require("../config/config.js"); -const { - getShorthandName, - getNamespaceFromTerm, - normalizePackageName, -} = require("../shared/naming.js"); -const { resolve } = require("../shared/relative-module-resolver.js"); + Legacy: { + ConfigOps: { + getRuleSeverity + } + } +} = require("@eslint/eslintrc"); +const { version } = require("../../package.json"); //------------------------------------------------------------------------------ // Typedefs //------------------------------------------------------------------------------ -// For VSCode IntelliSense +/** @typedef {import("../cli-engine/cli-engine").LintReport} CLIEngineLintReport */ +/** @typedef {import("../shared/types").DeprecatedRuleInfo} DeprecatedRuleInfo */ +/** @typedef {import("../shared/types").ConfigData} ConfigData */ +/** @typedef {import("../shared/types").LintMessage} LintMessage */ +/** @typedef {import("../shared/types").SuppressedLintMessage} SuppressedLintMessage */ +/** @typedef {import("../shared/types").Plugin} Plugin */ +/** @typedef {import("../shared/types").Rule} Rule */ +/** @typedef {import("../shared/types").LintResult} LintResult */ +/** @typedef {import("../shared/types").ResultsMeta} ResultsMeta */ + /** - * @import { Config as CalculatedConfig } from "../config/config.js"; - * @import { FlatConfigArray } from "../config/flat-config-array.js"; - * @import { RuleDefinition, RulesMeta } from "@eslint/core"; - * @import { WorkerLintResults } from "./worker.js"; + * The main formatter object. + * @typedef LoadedFormatter + * @property {(results: LintResult[], resultsMeta: ResultsMeta) => string | Promise} format format function. */ -/** @typedef {import("../types").Linter.Config} Config */ -/** @typedef {import("../types").ESLint.DeprecatedRuleUse} DeprecatedRuleInfo */ -/** @typedef {import("../types").ESLint.LintResult} LintResult */ -/** @typedef {import("../types").ESLint.Plugin} Plugin */ -/** @typedef {import("../types").ESLint.ResultsMeta} ResultsMeta */ - /** * The options with which to configure the ESLint instance. * @typedef {Object} ESLintOptions * @property {boolean} [allowInlineConfig] Enable or disable inline configuration comments. - * @property {Config|Array} [baseConfig] Base config, extended by all configs used with this instance + * @property {ConfigData} [baseConfig] Base config object, extended by all configs used with this instance * @property {boolean} [cache] Enable result caching. * @property {string} [cacheLocation] The cache file to use instead of .eslintcache. * @property {"metadata" | "content"} [cacheStrategy] The strategy used to detect changed files. - * @property {number | "auto" | "off"} [concurrency] Maximum number of linting threads, "auto" to choose automatically, "off" for no multithreading. * @property {string} [cwd] The value to use for the current working directory. * @property {boolean} [errorOnUnmatchedPattern] If `false` then `ESLint#lintFiles()` doesn't throw even if no target files found. Defaults to `true`. + * @property {string[]} [extensions] An array of file extensions to check. * @property {boolean|Function} [fix] Execute in autofix mode. If a function, should return a boolean. * @property {string[]} [fixTypes] Array of rule types to apply fixes for. - * @property {string[]} [flags] Array of feature flags to enable. * @property {boolean} [globInputPaths] Set to false to skip glob resolution of input file paths to lint (default: true). If false, each input file paths is assumed to be a non-glob path to an existing file. - * @property {boolean} [ignore] False disables all ignore patterns except for the default ones. - * @property {string[]} [ignorePatterns] Ignore file patterns to use in addition to config ignores. These patterns are relative to `cwd`. - * @property {Config|Array} [overrideConfig] Override config, overrides all configs used with this instance - * @property {boolean|string} [overrideConfigFile] Searches for default config file when falsy; - * doesn't do any config file lookup when `true`; considered to be a config filename - * when a string. - * @property {boolean} [passOnNoPatterns=false] When set to true, missing patterns cause - * the linting operation to short circuit and not report any failures. - * @property {Record} [plugins] An array of plugin implementations. - * @property {boolean} [stats] True enables added statistics on lint results. - * @property {boolean} [warnIgnored] Show warnings when the file list includes ignored files + * @property {boolean} [ignore] False disables use of .eslintignore. + * @property {string} [ignorePath] The ignore file to use instead of .eslintignore. + * @property {ConfigData} [overrideConfig] Override config object, overrides all configs used with this instance + * @property {string} [overrideConfigFile] The configuration file to use. + * @property {Record|null} [plugins] Preloaded plugins. This is a map-like object, keys are plugin IDs and each value is implementation. + * @property {"error" | "warn" | "off"} [reportUnusedDisableDirectives] the severity to report unused eslint-disable directives. + * @property {string} [resolvePluginsRelativeTo] The folder where plugins should be resolved from, defaulting to the CWD. + * @property {string[]} [rulePaths] An array of directories to load custom rules from. + * @property {boolean} [useEslintrc] False disables looking for .eslintrc.* files. */ -//------------------------------------------------------------------------------ -// Helpers -//------------------------------------------------------------------------------ - -const hrtimeBigint = process.hrtime.bigint; - -const debug = createDebug("eslint:eslint"); -const privateMembers = new WeakMap(); -const removedFormatters = new Set([ - "checkstyle", - "codeframe", - "compact", - "jslint-xml", - "junit", - "table", - "tap", - "unix", - "visualstudio", -]); -const fileRetryCodes = new Set(["ENFILE", "EMFILE"]); - /** - * Create rulesMeta object. - * @param {Map} rules a map of rules from which to generate the object. - * @returns {Record} metadata for all enabled rules. + * A rules metadata object. + * @typedef {Object} RulesMeta + * @property {string} id The plugin ID. + * @property {Object} definition The plugin definition. */ -function createRulesMeta(rules) { - return Array.from(rules).reduce((retVal, [id, rule]) => { - retVal[id] = rule.meta; - return retVal; - }, {}); -} - -/** @type {WeakMap} */ -const usedDeprecatedRulesCache = new WeakMap(); /** - * Create used deprecated rule list. - * @param {ESLint} eslint The ESLint instance. - * @param {string} maybeFilePath The absolute path to a lint target file or `""`. - * @returns {DeprecatedRuleInfo[]} The used deprecated rule list. + * Private members for the `ESLint` instance. + * @typedef {Object} ESLintPrivateMembers + * @property {CLIEngine} cliEngine The wrapped CLIEngine instance. + * @property {ESLintOptions} options The options used to instantiate the ESLint instance. */ -function getOrFindUsedDeprecatedRules(eslint, maybeFilePath) { - const { - options: { cwd }, - configLoader, - } = privateMembers.get(eslint); - const filePath = path.isAbsolute(maybeFilePath) - ? maybeFilePath - : getPlaceholderPath(cwd); - const configs = configLoader.getCachedConfigArrayForFile(filePath); - const config = configs.getConfig(filePath); - - // Most files use the same config, so cache it. - if (config && !usedDeprecatedRulesCache.has(config)) { - const retv = []; - - if (config.rules) { - for (const [ruleId, ruleConf] of Object.entries(config.rules)) { - if (Config.getRuleNumericSeverity(ruleConf) === 0) { - continue; - } - const rule = config.getRuleDefinition(ruleId); - const meta = rule && rule.meta; - - if (meta && meta.deprecated) { - const usesNewFormat = typeof meta.deprecated === "object"; - - retv.push({ - ruleId, - replacedBy: usesNewFormat - ? (meta.deprecated.replacedBy?.map( - replacement => - `${replacement.plugin?.name !== void 0 ? `${getShorthandName(replacement.plugin.name, "eslint-plugin")}/` : ""}${replacement.rule?.name ?? ""}`, - ) ?? []) - : meta.replacedBy || [], - info: usesNewFormat ? meta.deprecated : void 0, - }); - } - } - } - - usedDeprecatedRulesCache.set(config, Object.freeze(retv)); - } - - return config ? usedDeprecatedRulesCache.get(config) : Object.freeze([]); -} + +//------------------------------------------------------------------------------ +// Helpers +//------------------------------------------------------------------------------ + +const writeFile = promisify(fs.writeFile); /** - * Processes the linting results generated by a CLIEngine linting report to - * match the ESLint class's API. - * @param {ESLint} eslint The ESLint instance. - * @param {LintResult[]} results The linting results to process. - * @returns {LintResult[]} The processed linting results. + * The map with which to store private class members. + * @type {WeakMap} */ -function processLintReport(eslint, results) { - const descriptor = { - configurable: true, - enumerable: true, - get() { - return getOrFindUsedDeprecatedRules(eslint, this.filePath); - }, - }; - - for (const result of results) { - Object.defineProperty(result, "usedDeprecatedRules", descriptor); - } - - return results; -} +const privateMembersMap = new WeakMap(); /** - * An Array.prototype.sort() compatible compare function to order results by their file path. - * @param {LintResult} a The first lint result. - * @param {LintResult} b The second lint result. - * @returns {number} An integer representing the order in which the two results should occur. + * Check if a given value is a non-empty string or not. + * @param {any} x The value to check. + * @returns {boolean} `true` if `x` is a non-empty string. */ -function compareResultsByFilePath(a, b) { - if (a.filePath < b.filePath) { - return -1; - } - - if (a.filePath > b.filePath) { - return 1; - } - - return 0; +function isNonEmptyString(x) { + return typeof x === "string" && x.trim() !== ""; } /** - * Determines which config file to use. This is determined by seeing if an - * override config file was passed, and if so, using it; otherwise, as long - * as override config file is not explicitly set to `false`, it will search - * upwards from the cwd for a file named `eslint.config.js`. - * - * This function is used primarily by the `--inspect-config` option. For now, - * we will maintain the existing behavior, which is to search up from the cwd. - * @param {ESLintOptions} options The ESLint instance options. - * @returns {Promise<{configFilePath:string|undefined;basePath:string}>} Location information for - * the config file. + * Check if a given value is an array of non-empty strings or not. + * @param {any} x The value to check. + * @returns {boolean} `true` if `x` is an array of non-empty strings. */ -async function locateConfigFileToUse({ configFile, cwd }) { - const configLoader = new ConfigLoader({ - cwd, - configFile, - }); - - const configFilePath = await configLoader.findConfigFileForPath( - path.join(cwd, "__placeholder__.js"), - ); - - if (!configFilePath) { - throw new Error("No ESLint configuration file was found."); - } - - return { - configFilePath, - basePath: configFile ? cwd : path.dirname(configFilePath), - }; +function isArrayOfNonEmptyString(x) { + return Array.isArray(x) && x.every(isNonEmptyString); } /** - * Creates an error to be thrown when an array of results passed to `getRulesMetaForResults` was not created by the current engine. - * @param {Error|undefined} cause The original error that led to this symptom error being thrown. Might not always be available. - * @returns {TypeError} An error object. + * Check if a given value is a valid fix type or not. + * @param {any} x The value to check. + * @returns {boolean} `true` if `x` is valid fix type. */ -function createExtraneousResultsError(cause) { - return new TypeError( - "Results object was not created from this ESLint instance.", - { - cause, - }, - ); +function isFixType(x) { + return x === "directive" || x === "problem" || x === "suggestion" || x === "layout"; } /** - * Maximum number of files assumed to be best handled by one worker thread. - * This value is a heuristic estimation that can be adjusted if required. + * Check if a given value is an array of fix types or not. + * @param {any} x The value to check. + * @returns {boolean} `true` if `x` is an array of fix types. */ -const AUTO_FILES_PER_WORKER = 50; +function isFixTypeArray(x) { + return Array.isArray(x) && x.every(isFixType); +} /** - * Calculates the number of worker threads to run for "auto" concurrency depending on the number of - * files that need to be processed. - * - * The number of worker threads is calculated as the number of files that need to be processed - * (`processableFileCount`) divided by the number of files assumed to be best handled by one worker - * thread (`AUTO_FILES_PER_WORKER`), rounded up to the next integer. - * Two adjustments are made to this calculation: first, the number of workers is capped at half the - * number of available CPU cores (`maxWorkers`); second, a value of 1 is converted to 0. - * The following table shows the relationship between the number of files to be processed and the - * number of workers: - * - * Files to be processed | Workers - * -------------------------------------------------------------------|----------------- - * 0 | 0 - * 1, 2, …, AUTO_FILES_PER_WORKER | 0 (there's no 1) - * AUTO_FILES_PER_WORKER + 1, …, AUTO_FILES_PER_WORKER * 2 | 2 - * AUTO_FILES_PER_WORKER * 2 + 1, …, AUTO_FILES_PER_WORKER * 3 | 3 - * ⋯ | ⋯ - * AUTO_FILES_PER_WORKER * (𝑛 - 1) + 1, …, AUTO_FILES_PER_WORKER * 𝑛 | 𝑛 - * ⋯ | ⋯ - * AUTO_FILES_PER_WORKER * (maxWorkers - 1) + 1, … | maxWorkers - * - * The number of files to be processed should be determined by the calling function. - * @param {number} processableFileCount The number of files that need to be processed. - * @param {number} maxWorkers The maximum number of workers to run. - * @returns {number} The number of worker threads to run. + * The error for invalid options. */ -function getWorkerCountFor(processableFileCount, maxWorkers) { - let workerCount = Math.ceil(processableFileCount / AUTO_FILES_PER_WORKER); - if (workerCount > maxWorkers) { - workerCount = maxWorkers; - } - if (workerCount <= 1) { - workerCount = 0; - } - return workerCount; +class ESLintInvalidOptionsError extends Error { + constructor(messages) { + super(`Invalid Options:\n- ${messages.join("\n- ")}`); + this.code = "ESLINT_INVALID_OPTIONS"; + Error.captureStackTrace(this, ESLintInvalidOptionsError); + } } /** - * Returns true if a file has no valid cached results or if it needs to be reprocessed because there are violations that may need fixing. - * This function will access the filesystem. - * @param {LintResultCache} lintResultCache The lint result cache. - * @param {boolean} fix The fix option. - * @param {string} filePath The file for which to retrieve lint results. - * @param {Config} config The config of the file. - * @returns {boolean} True if the file needs to be reprocessed. + * Validates and normalizes options for the wrapped CLIEngine instance. + * @param {ESLintOptions} options The options to process. + * @throws {ESLintInvalidOptionsError} If of any of a variety of type errors. + * @returns {ESLintOptions} The normalized options. */ -function needsReprocessing(lintResultCache, fix, filePath, config) { - const results = lintResultCache.getValidCachedLintResults(filePath, config); - - // This reflects the reprocessing logic of the `lintFile` helper function. - return !results || (fix && results.messages && results.messages.length > 0); +function processOptions({ + allowInlineConfig = true, // ← we cannot use `overrideConfig.noInlineConfig` instead because `allowInlineConfig` has side-effect that suppress warnings that show inline configs are ignored. + baseConfig = null, + cache = false, + cacheLocation = ".eslintcache", + cacheStrategy = "metadata", + cwd = process.cwd(), + errorOnUnmatchedPattern = true, + extensions = null, // ← should be null by default because if it's an array then it suppresses RFC20 feature. + fix = false, + fixTypes = null, // ← should be null by default because if it's an array then it suppresses rules that don't have the `meta.type` property. + globInputPaths = true, + ignore = true, + ignorePath = null, // ← should be null by default because if it's a string then it may throw ENOENT. + overrideConfig = null, + overrideConfigFile = null, + plugins = {}, + reportUnusedDisableDirectives = null, // ← should be null by default because if it's a string then it overrides the 'reportUnusedDisableDirectives' setting in config files. And we cannot use `overrideConfig.reportUnusedDisableDirectives` instead because we cannot configure the `error` severity with that. + resolvePluginsRelativeTo = null, // ← should be null by default because if it's a string then it suppresses RFC47 feature. + rulePaths = [], + useEslintrc = true, + ...unknownOptions +}) { + const errors = []; + const unknownOptionKeys = Object.keys(unknownOptions); + + if (unknownOptionKeys.length >= 1) { + errors.push(`Unknown options: ${unknownOptionKeys.join(", ")}`); + if (unknownOptionKeys.includes("cacheFile")) { + errors.push("'cacheFile' has been removed. Please use the 'cacheLocation' option instead."); + } + if (unknownOptionKeys.includes("configFile")) { + errors.push("'configFile' has been removed. Please use the 'overrideConfigFile' option instead."); + } + if (unknownOptionKeys.includes("envs")) { + errors.push("'envs' has been removed. Please use the 'overrideConfig.env' option instead."); + } + if (unknownOptionKeys.includes("globals")) { + errors.push("'globals' has been removed. Please use the 'overrideConfig.globals' option instead."); + } + if (unknownOptionKeys.includes("ignorePattern")) { + errors.push("'ignorePattern' has been removed. Please use the 'overrideConfig.ignorePatterns' option instead."); + } + if (unknownOptionKeys.includes("parser")) { + errors.push("'parser' has been removed. Please use the 'overrideConfig.parser' option instead."); + } + if (unknownOptionKeys.includes("parserOptions")) { + errors.push("'parserOptions' has been removed. Please use the 'overrideConfig.parserOptions' option instead."); + } + if (unknownOptionKeys.includes("rules")) { + errors.push("'rules' has been removed. Please use the 'overrideConfig.rules' option instead."); + } + } + if (typeof allowInlineConfig !== "boolean") { + errors.push("'allowInlineConfig' must be a boolean."); + } + if (typeof baseConfig !== "object") { + errors.push("'baseConfig' must be an object or null."); + } + if (typeof cache !== "boolean") { + errors.push("'cache' must be a boolean."); + } + if (!isNonEmptyString(cacheLocation)) { + errors.push("'cacheLocation' must be a non-empty string."); + } + if ( + cacheStrategy !== "metadata" && + cacheStrategy !== "content" + ) { + errors.push("'cacheStrategy' must be any of \"metadata\", \"content\"."); + } + if (!isNonEmptyString(cwd) || !path.isAbsolute(cwd)) { + errors.push("'cwd' must be an absolute path."); + } + if (typeof errorOnUnmatchedPattern !== "boolean") { + errors.push("'errorOnUnmatchedPattern' must be a boolean."); + } + if (!isArrayOfNonEmptyString(extensions) && extensions !== null) { + errors.push("'extensions' must be an array of non-empty strings or null."); + } + if (typeof fix !== "boolean" && typeof fix !== "function") { + errors.push("'fix' must be a boolean or a function."); + } + if (fixTypes !== null && !isFixTypeArray(fixTypes)) { + errors.push("'fixTypes' must be an array of any of \"directive\", \"problem\", \"suggestion\", and \"layout\"."); + } + if (typeof globInputPaths !== "boolean") { + errors.push("'globInputPaths' must be a boolean."); + } + if (typeof ignore !== "boolean") { + errors.push("'ignore' must be a boolean."); + } + if (!isNonEmptyString(ignorePath) && ignorePath !== null) { + errors.push("'ignorePath' must be a non-empty string or null."); + } + if (typeof overrideConfig !== "object") { + errors.push("'overrideConfig' must be an object or null."); + } + if (!isNonEmptyString(overrideConfigFile) && overrideConfigFile !== null) { + errors.push("'overrideConfigFile' must be a non-empty string or null."); + } + if (typeof plugins !== "object") { + errors.push("'plugins' must be an object or null."); + } else if (plugins !== null && Object.keys(plugins).includes("")) { + errors.push("'plugins' must not include an empty string."); + } + if (Array.isArray(plugins)) { + errors.push("'plugins' doesn't add plugins to configuration to load. Please use the 'overrideConfig.plugins' option instead."); + } + if ( + reportUnusedDisableDirectives !== "error" && + reportUnusedDisableDirectives !== "warn" && + reportUnusedDisableDirectives !== "off" && + reportUnusedDisableDirectives !== null + ) { + errors.push("'reportUnusedDisableDirectives' must be any of \"error\", \"warn\", \"off\", and null."); + } + if ( + !isNonEmptyString(resolvePluginsRelativeTo) && + resolvePluginsRelativeTo !== null + ) { + errors.push("'resolvePluginsRelativeTo' must be a non-empty string or null."); + } + if (!isArrayOfNonEmptyString(rulePaths)) { + errors.push("'rulePaths' must be an array of non-empty strings."); + } + if (typeof useEslintrc !== "boolean") { + errors.push("'useEslintrc' must be a boolean."); + } + + if (errors.length > 0) { + throw new ESLintInvalidOptionsError(errors); + } + + return { + allowInlineConfig, + baseConfig, + cache, + cacheLocation, + cacheStrategy, + configFile: overrideConfigFile, + cwd: path.normalize(cwd), + errorOnUnmatchedPattern, + extensions, + fix, + fixTypes, + globInputPaths, + ignore, + ignorePath, + reportUnusedDisableDirectives, + resolvePluginsRelativeTo, + rulePaths, + useEslintrc + }; } /** - * Calculates the number of worker threads to run for "auto" concurrency. - * - * The number of worker threads depends on the number files that need to be processed. - * Typically, this includes all non-ignored files. - * In a cached run with "metadata" strategy, files with a valid cached result aren't counted. - * @param {ESLint} eslint ESLint instance. - * @param {string[]} filePaths File paths to lint. - * @param {number} maxWorkers The maximum number of workers to run. - * @returns {number} The number of worker threads to run for "auto" concurrency. + * Check if a value has one or more properties and that value is not undefined. + * @param {any} obj The value to check. + * @returns {boolean} `true` if `obj` has one or more properties that that value is not undefined. */ -function calculateAutoWorkerCount(eslint, filePaths, maxWorkers) { - const startTime = hrtimeBigint(); - const { - configLoader, - lintResultCache, - options: { cacheStrategy, fix }, - } = privateMembers.get(eslint); - /** True if cache is not used or if strategy is "content". */ - const countAllMatched = !lintResultCache || cacheStrategy === "content"; - - let processableFileCount = 0; - let remainingFiles = filePaths.length; - - /** The number of workers if none of the remaining files were to be counted. */ - let lowWorkerCount = 0; - - /* - * Rather than counting all files to be processed in advance, we stop iterating as soon as we reach - * a point where adding more files doesn't change the number of workers anymore. - */ - for (const filePath of filePaths) { - /** The number of workers if all of the remaining files were to be counted. */ - const highWorkerCount = getWorkerCountFor( - processableFileCount + remainingFiles, - maxWorkers, - ); - if (lowWorkerCount >= highWorkerCount) { - // The highest possible number of workers has been reached, so stop counting. - break; - } - remainingFiles--; - const configs = configLoader.getCachedConfigArrayForFile(filePath); - const config = configs.getConfig(filePath); - if (!config) { - // file is ignored - continue; - } - if ( - countAllMatched || - needsReprocessing(lintResultCache, fix, filePath, config) - ) { - processableFileCount++; - lowWorkerCount = getWorkerCountFor( - processableFileCount, - maxWorkers, - ); - } - } - debug( - "%d file(s) to process counted in %t", - processableFileCount, - hrtimeBigint() - startTime, - ); - return lowWorkerCount; +function hasDefinedProperty(obj) { + if (typeof obj === "object" && obj !== null) { + for (const key in obj) { + if (typeof obj[key] !== "undefined") { + return true; + } + } + } + return false; } /** - * Calculates the number of workers to run based on the concurrency setting and the number of files to lint. - * @param {ESLint} eslint The ESLint instance. - * @param {string[]} filePaths File paths to lint. - * @param {{ availableParallelism: () => number }} [os] Node.js `os` module, or a mock for testing. - * @returns {number} The effective number of worker threads to be started. A value of zero disables multithread linting. + * Create rulesMeta object. + * @param {Map} rules a map of rules from which to generate the object. + * @returns {Object} metadata for all enabled rules. */ -function calculateWorkerCount( - eslint, - filePaths, - { availableParallelism } = os, -) { - const { concurrency } = privateMembers.get(eslint).options; - switch (concurrency) { - case "off": - return 0; - case "auto": { - const maxWorkers = availableParallelism() >> 1; - return calculateAutoWorkerCount(eslint, filePaths, maxWorkers); - } - default: { - const workerCount = Math.min(concurrency, filePaths.length); - return workerCount > 1 ? workerCount : 0; - } - } +function createRulesMeta(rules) { + return Array.from(rules).reduce((retVal, [id, rule]) => { + retVal[id] = rule.meta; + return retVal; + }, {}); } -// Used internally. Do not expose. -const disableCloneabilityCheck = Symbol( - "Do not check for uncloneable options.", -); - -/** - * The smallest net linting ratio that doesn't trigger a poor concurrency warning. - * The net linting ratio is defined as the net linting duration divided by the thread's total runtime, - * where the net linting duration is the total linting time minus the time spent on I/O-intensive operations: - * **Net Linting Ratio** = (**Linting Time** – **I/O Time**) / **Thread Runtime**. - * - **Linting Time**: Total time spent linting files - * - **I/O Time**: Portion of linting time spent loading configs and reading files - * - **Thread Runtime**: End-to-end execution time of the thread - * - * This value is a heuristic estimation that can be adjusted if required. - */ -const LOW_NET_LINTING_RATIO = 0.7; +/** @type {WeakMap} */ +const usedDeprecatedRulesCache = new WeakMap(); /** - * Runs worker threads to lint files. - * @param {string[]} filePaths File paths to lint. - * @param {number} workerCount The number of worker threads to run. - * @param {ESLintOptions | string} eslintOptionsOrURL The unprocessed ESLint options or the URL of the options module. - * @param {() => void} warnOnLowNetLintingRatio A function to call if the net linting ratio is low. - * @returns {Promise} Lint results. + * Create used deprecated rule list. + * @param {CLIEngine} cliEngine The CLIEngine instance. + * @param {string} maybeFilePath The absolute path to a lint target file or `""`. + * @returns {DeprecatedRuleInfo[]} The used deprecated rule list. */ -async function runWorkers( - filePaths, - workerCount, - eslintOptionsOrURL, - warnOnLowNetLintingRatio, -) { - const fileCount = filePaths.length; - const results = Array(fileCount); - const workerURL = pathToFileURL(path.join(__dirname, "./worker.js")); - const filePathIndexArray = new Uint32Array( - new SharedArrayBuffer(Uint32Array.BYTES_PER_ELEMENT), - ); - const abortController = new AbortController(); - const abortSignal = abortController.signal; - const workerOptions = { - env: SHARE_ENV, - workerData: { - eslintOptionsOrURL, - filePathIndexArray, - filePaths, - }, - }; - - let worstNetLintingRatio = 1; - - /** - * A promise executor function that starts a worker thread on each invocation. - * @param {() => void} resolve_ Called when the worker thread terminates successfully. - * @param {(error: Error) => void} reject Called when the worker thread terminates with an error. - * @returns {void} - */ - function workerExecutor(resolve_, reject) { - const workerStartTime = hrtimeBigint(); - const worker = new Worker(workerURL, workerOptions); - worker.once( - "message", - (/** @type {WorkerLintResults} */ indexedResults) => { - const workerDuration = hrtimeBigint() - workerStartTime; - - // The net linting ratio provides an approximate measure of worker thread efficiency, defined as the net linting duration divided by the thread's total runtime. - const netLintingRatio = - Number(indexedResults.netLintingDuration) / - Number(workerDuration); - - worstNetLintingRatio = Math.min( - worstNetLintingRatio, - netLintingRatio, - ); - - if (timing.enabled && indexedResults.timings) { - timing.mergeData(indexedResults.timings); - } - - for (const result of indexedResults) { - const { index } = result; - delete result.index; - results[index] = result; - } - resolve_(); - }, - ); - worker.once("error", error => { - abortController.abort(error); - reject(error); - }); - abortSignal.addEventListener("abort", () => worker.terminate()); - } - - const promises = Array(workerCount); - for (let index = 0; index < workerCount; ++index) { - promises[index] = new Promise(workerExecutor); - } - - try { - await Promise.all(promises); - } catch (error) { - /* - * If any worker fails, suppress timing display in the main thread - * to avoid printing partial or misleading timing output. - */ - timing.disableDisplay(); - throw error; - } - - if (worstNetLintingRatio < LOW_NET_LINTING_RATIO) { - warnOnLowNetLintingRatio(); - } - - return results; +function getOrFindUsedDeprecatedRules(cliEngine, maybeFilePath) { + const { + configArrayFactory, + options: { cwd } + } = getCLIEngineInternalSlots(cliEngine); + const filePath = path.isAbsolute(maybeFilePath) + ? maybeFilePath + : path.join(cwd, "__placeholder__.js"); + const configArray = configArrayFactory.getConfigArrayForFile(filePath); + const config = configArray.extractConfig(filePath); + + // Most files use the same config, so cache it. + if (!usedDeprecatedRulesCache.has(config)) { + const pluginRules = configArray.pluginRules; + const retv = []; + + for (const [ruleId, ruleConf] of Object.entries(config.rules)) { + if (getRuleSeverity(ruleConf) === 0) { + continue; + } + const rule = pluginRules.get(ruleId) || BuiltinRules.get(ruleId); + const meta = rule && rule.meta; + + if (meta && meta.deprecated) { + retv.push({ ruleId, replacedBy: meta.replacedBy || [] }); + } + } + + usedDeprecatedRulesCache.set(config, Object.freeze(retv)); + } + + return usedDeprecatedRulesCache.get(config); } /** - * Lint files in multithread mode. - * @param {ESLint} eslint ESLint instance. - * @param {string[]} filePaths File paths to lint. - * @param {number} workerCount The number of worker threads to run. - * @param {ESLintOptions | string} eslintOptionsOrURL The unprocessed ESLint options or the URL of the options module. - * @param {() => void} warnOnLowNetLintingRatio A function to call if the net linting ratio is low. - * @returns {Promise} Lint results. + * Processes the linting results generated by a CLIEngine linting report to + * match the ESLint class's API. + * @param {CLIEngine} cliEngine The CLIEngine instance. + * @param {CLIEngineLintReport} report The CLIEngine linting report to process. + * @returns {LintResult[]} The processed linting results. */ -async function lintFilesWithMultithreading( - eslint, - filePaths, - workerCount, - eslintOptionsOrURL, - warnOnLowNetLintingRatio, -) { - const { configLoader, lintResultCache } = privateMembers.get(eslint); - - const results = await runWorkers( - filePaths, - workerCount, - eslintOptionsOrURL, - warnOnLowNetLintingRatio, - ); - // Persist the cache to disk. - if (lintResultCache) { - results.forEach((result, index) => { - if (result) { - const filePath = filePaths[index]; - const configs = - configLoader.getCachedConfigArrayForFile(filePath); - const config = configs.getConfig(filePath); - - if (config) { - /* - * Store the lint result in the LintResultCache. - * NOTE: The LintResultCache will remove the file source and any - * other properties that are difficult to serialize, and will - * hydrate those properties back in on future lint runs. - */ - lintResultCache.setCachedLintResults( - filePath, - config, - result, - ); - } - } - }); - } - return results; +function processCLIEngineLintReport(cliEngine, { results }) { + const descriptor = { + configurable: true, + enumerable: true, + get() { + return getOrFindUsedDeprecatedRules(cliEngine, this.filePath); + } + }; + + for (const result of results) { + Object.defineProperty(result, "usedDeprecatedRules", descriptor); + } + + return results; } /** - * Lint files in single-thread mode. - * @param {ESLint} eslint ESLint instance. - * @param {string[]} filePaths File paths to lint. - * @returns {Promise} Lint results. + * An Array.prototype.sort() compatible compare function to order results by their file path. + * @param {LintResult} a The first lint result. + * @param {LintResult} b The second lint result. + * @returns {number} An integer representing the order in which the two results should occur. */ -async function lintFilesWithoutMultithreading(eslint, filePaths) { - const { - configLoader, - linter, - lintResultCache, - options: eslintOptions, - } = privateMembers.get(eslint); - - const controller = new AbortController(); - const retrier = new Retrier(error => fileRetryCodes.has(error.code), { - concurrency: 100, - }); - - /* - * Because we need to process multiple files, including reading from disk, - * it is most efficient to start by reading each file via promises so that - * they can be done in parallel. Then, we can lint the returned text. This - * ensures we are waiting the minimum amount of time in between lints. - */ - const results = await Promise.all( - filePaths.map(async filePath => { - const configs = configLoader.getCachedConfigArrayForFile(filePath); - const config = configs.getConfig(filePath); - - const result = await lintFile( - filePath, - configs, - eslintOptions, - linter, - lintResultCache, - null, - retrier, - controller, - ); - - if (config) { - /* - * Store the lint result in the LintResultCache. - * NOTE: The LintResultCache will remove the file source and any - * other properties that are difficult to serialize, and will - * hydrate those properties back in on future lint runs. - */ - lintResultCache?.setCachedLintResults(filePath, config, result); - } - - return result; - }), - ); - return results; -} +function compareResultsByFilePath(a, b) { + if (a.filePath < b.filePath) { + return -1; + } -/** - * Throws an error if the given options are not cloneable. - * @param {ESLintOptions} options The options to check. - * @returns {void} - * @throws {TypeError} If the options are not cloneable. - */ -function validateOptionCloneability(options) { - try { - structuredClone(options); - return; - } catch { - // continue - } - const uncloneableOptionKeys = Object.keys(options) - .filter(key => { - try { - structuredClone(options[key]); - } catch { - return true; - } - return false; - }) - .sort(); - const error = new TypeError( - `The ${uncloneableOptionKeys.length === 1 ? "option" : "options"} ${new Intl.ListFormat("en-US").format(uncloneableOptionKeys.map(key => `"${key}"`))} cannot be cloned. When concurrency is enabled, all options must be cloneable values (JSON values). Remove uncloneable options or use an options module.`, - ); - error.code = "ESLINT_UNCLONEABLE_OPTIONS"; - throw error; -} + if (a.filePath > b.filePath) { + return 1; + } -//----------------------------------------------------------------------------- -// Main API -//----------------------------------------------------------------------------- + return 0; +} /** - * Primary Node.js API for ESLint. + * Main API. */ class ESLint { - /** - * The type of configuration used by this class. - * @type {string} - */ - static configType = "flat"; - - /** - * The loader to use for finding config files. - * @type {ConfigLoader} - */ - #configLoader; - - /** - * The unprocessed options or the URL of the options module. Only set when concurrency is enabled. - * @type {ESLintOptions | string | undefined} - */ - #optionsOrURL; - - /** - * Creates a new instance of the main ESLint API. - * @param {ESLintOptions} options The options for this instance. - */ - constructor(options = {}) { - const processedOptions = processOptions(options); - if ( - !options[disableCloneabilityCheck] && - processedOptions.concurrency !== "off" - ) { - validateOptionCloneability(options); - - // Save the unprocessed options in an instance field to pass to worker threads in `lintFiles()`. - this.#optionsOrURL = options; - } - const warningService = new WarningService(); - const linter = createLinter(processedOptions, warningService); - - const cacheFilePath = getCacheFile( - processedOptions.cacheLocation, - processedOptions.cwd, - ); - - const lintResultCache = createLintResultCache( - processedOptions, - cacheFilePath, - ); - const defaultConfigs = createDefaultConfigs(options.plugins); - - this.#configLoader = createConfigLoader( - processedOptions, - defaultConfigs, - linter, - warningService, - ); - - debug(`Using config loader ${this.#configLoader.constructor.name}`); - - privateMembers.set(this, { - options: processedOptions, - linter, - cacheFilePath, - lintResultCache, - defaultConfigs, - configs: null, - configLoader: this.#configLoader, - warningService, - }); - - // Check for the .eslintignore file, and warn if it's present. - if (existsSync(path.resolve(processedOptions.cwd, ".eslintignore"))) { - warningService.emitESLintIgnoreWarning(); - } - } - - /** - * The version text. - * @type {string} - */ - static get version() { - return version; - } - - /** - * The default configuration that ESLint uses internally. This is provided for tooling that wants to calculate configurations using the same defaults as ESLint. - * Keep in mind that the default configuration may change from version to version, so you shouldn't rely on any particular keys or values to be present. - * @type {FlatConfigArray} - */ - static get defaultConfig() { - return defaultConfig; - } - - /** - * Outputs fixes from the given results to files. - * @param {LintResult[]} results The lint results. - * @returns {Promise} Returns a promise that is used to track side effects. - */ - static async outputFixes(results) { - if (!Array.isArray(results)) { - throw new Error("'results' must be an array"); - } - - const retrier = new Retrier(error => fileRetryCodes.has(error.code), { - concurrency: 100, - }); - - await Promise.all( - results - .filter(result => { - if (typeof result !== "object" || result === null) { - throw new Error("'results' must include only objects"); - } - return ( - typeof result.output === "string" && - path.isAbsolute(result.filePath) - ); - }) - .map(r => - retrier.retry(() => fs.writeFile(r.filePath, r.output)), - ), - ); - } - - /** - * Returns results that only contains errors. - * @param {LintResult[]} results The results to filter. - * @returns {LintResult[]} The filtered results. - */ - static getErrorResults(results) { - const filtered = []; - - results.forEach(result => { - const filteredMessages = result.messages.filter(isErrorMessage); - const filteredSuppressedMessages = - result.suppressedMessages.filter(isErrorMessage); - - if (filteredMessages.length > 0) { - filtered.push({ - ...result, - messages: filteredMessages, - suppressedMessages: filteredSuppressedMessages, - errorCount: filteredMessages.length, - warningCount: 0, - fixableErrorCount: result.fixableErrorCount, - fixableWarningCount: 0, - }); - } - }); - - return filtered; - } - - /** - * Creates a new ESLint instance using options loaded from a module. - * @param {URL} optionsURL The URL of the options module. - * @returns {ESLint} The new ESLint instance. - */ - static async fromOptionsModule(optionsURL) { - if (!(optionsURL instanceof URL)) { - throw new TypeError("Argument must be a URL object"); - } - const optionsURLString = optionsURL.href; - const loadedOptions = await loadOptionsFromModule(optionsURLString); - const options = { ...loadedOptions, [disableCloneabilityCheck]: true }; - const eslint = new ESLint(options); - - if (options.concurrency !== "off") { - // Save the options module URL in an instance field to pass to worker threads in `lintFiles()`. - eslint.#optionsOrURL = optionsURLString; - } - return eslint; - } - - /** - * Returns meta objects for each rule represented in the lint results. - * @param {LintResult[]} results The results to fetch rules meta for. - * @returns {Record} A mapping of ruleIds to rule meta objects. - * @throws {TypeError} When the results object wasn't created from this ESLint instance. - * @throws {TypeError} When a plugin or rule is missing. - */ - getRulesMetaForResults(results) { - // short-circuit simple case - if (results.length === 0) { - return {}; - } - - const resultRules = new Map(); - const { - configLoader, - options: { cwd }, - } = privateMembers.get(this); - - for (const result of results) { - /* - * Normalize filename for . - */ - const filePath = - result.filePath === "" - ? getPlaceholderPath(cwd) - : result.filePath; - const allMessages = result.messages.concat( - result.suppressedMessages, - ); - - for (const { ruleId } of allMessages) { - if (!ruleId) { - continue; - } - - /* - * All of the plugin and rule information is contained within the - * calculated config for the given file. - */ - let configs; - - try { - configs = - configLoader.getCachedConfigArrayForFile(filePath); - } catch (err) { - throw createExtraneousResultsError(err); - } - - const config = configs.getConfig(filePath); - - if (!config) { - throw createExtraneousResultsError(); - } - const rule = config.getRuleDefinition(ruleId); - - // ignore unknown rules - if (rule) { - resultRules.set(ruleId, rule); - } - } - } - - return createRulesMeta(resultRules); - } - - /** - * Indicates if the given feature flag is enabled for this instance. - * @param {string} flag The feature flag to check. - * @returns {boolean} `true` if the feature flag is enabled, `false` if not. - */ - hasFlag(flag) { - // note: Linter does validation of the flags - return privateMembers.get(this).linter.hasFlag(flag); - } - - /** - * Executes the current configuration on an array of file and directory names. - * @param {string|string[]} patterns An array of file and directory names. - * @returns {Promise} The results of linting the file patterns given. - */ - async lintFiles(patterns) { - let normalizedPatterns = patterns; - const { - cacheFilePath, - lintResultCache, - options: eslintOptions, - warningService, - } = privateMembers.get(this); - - /* - * Special cases: - * 1. `patterns` is an empty string - * 2. `patterns` is an empty array - * - * In both cases, we use the cwd as the directory to lint. - */ - if ( - patterns === "" || - (Array.isArray(patterns) && patterns.length === 0) - ) { - /* - * Special case: If `passOnNoPatterns` is true, then we just exit - * without doing any work. - */ - if (eslintOptions.passOnNoPatterns) { - return []; - } - - normalizedPatterns = ["."]; - } else { - if ( - !isNonEmptyString(patterns) && - !isArrayOfNonEmptyString(patterns) - ) { - throw new Error( - "'patterns' must be a non-empty string or an array of non-empty strings", - ); - } - - if (typeof patterns === "string") { - normalizedPatterns = [patterns]; - } - } - - debug(`Using file patterns: ${normalizedPatterns}`); - - const { concurrency, cwd, globInputPaths, errorOnUnmatchedPattern } = - eslintOptions; - - // Delete cache file; should this be done here? - if (!lintResultCache && cacheFilePath) { - debug(`Deleting cache file at ${cacheFilePath}`); - - try { - if (existsSync(cacheFilePath)) { - await fs.unlink(cacheFilePath); - } - } catch (error) { - if (existsSync(cacheFilePath)) { - throw error; - } - } - } - - const startTime = hrtimeBigint(); - const filePaths = await findFiles({ - patterns: normalizedPatterns, - cwd, - globInputPaths, - configLoader: this.#configLoader, - errorOnUnmatchedPattern, - }); - debug( - "%d file(s) found in %t", - filePaths.length, - hrtimeBigint() - startTime, - ); - - /** @type {LintResult[]} */ - let results; - - // The value of `module.exports.calculateWorkerCount` can be overridden in tests. - const workerCount = module.exports.calculateWorkerCount( - this, - filePaths, - ); - if (workerCount) { - debug(`Linting using ${workerCount} worker thread(s).`); - let poorConcurrencyNotice; - if (workerCount <= 2) { - poorConcurrencyNotice = "disable concurrency"; - } else { - if (concurrency === "auto") { - poorConcurrencyNotice = - "disable concurrency or use a numeric concurrency setting"; - } else { - poorConcurrencyNotice = "reduce or disable concurrency"; - } - } - results = await lintFilesWithMultithreading( - this, - filePaths, - workerCount, - this.#optionsOrURL, - () => - warningService.emitPoorConcurrencyWarning( - poorConcurrencyNotice, - ), - ); - } else { - debug(`Linting in single-thread mode.`); - results = await lintFilesWithoutMultithreading(this, filePaths); - } - - // Persist the cache to disk. - if (lintResultCache) { - lintResultCache.reconcile(); - } - - const finalResults = results.filter(result => !!result); - - return processLintReport(this, finalResults); - } - - /** - * Executes the current configuration on text. - * @param {string} code A string of JavaScript code to lint. - * @param {Object} [options] The options. - * @param {string} [options.filePath] The path to the file of the source code. - * @param {boolean} [options.warnIgnored] When set to true, warn if given filePath is an ignored path. - * @returns {Promise} The results of linting the string of code given. - */ - async lintText(code, options = {}) { - // Parameter validation - - if (typeof code !== "string") { - throw new Error("'code' must be a string"); - } - - if (typeof options !== "object") { - throw new Error("'options' must be an object, null, or undefined"); - } - - // Options validation - - const { filePath, warnIgnored, ...unknownOptions } = options || {}; - - const unknownOptionKeys = Object.keys(unknownOptions); - - if (unknownOptionKeys.length > 0) { - throw new Error( - `'options' must not include the unknown option(s): ${unknownOptionKeys.join(", ")}`, - ); - } - - if (filePath !== void 0 && !isNonEmptyString(filePath)) { - throw new Error( - "'options.filePath' must be a non-empty string or undefined", - ); - } - - if ( - typeof warnIgnored !== "boolean" && - typeof warnIgnored !== "undefined" - ) { - throw new Error( - "'options.warnIgnored' must be a boolean or undefined", - ); - } - - // Now we can get down to linting - - const { linter, options: eslintOptions } = privateMembers.get(this); - const { - allowInlineConfig, - cwd, - fix, - fixTypes, - warnIgnored: constructorWarnIgnored, - ruleFilter, - stats, - } = eslintOptions; - const results = []; - const startTime = hrtimeBigint(); - const fixTypesSet = fixTypes ? new Set(fixTypes) : null; - const resolvedFilename = path.resolve( - cwd, - filePath || "__placeholder__.js", - ); - const configs = - await this.#configLoader.loadConfigArrayForFile(resolvedFilename); - const configStatus = - configs?.getConfigStatus(resolvedFilename) ?? "unconfigured"; - - // Clear the last used config arrays. - if (resolvedFilename && configStatus !== "matched") { - const shouldWarnIgnored = - typeof warnIgnored === "boolean" - ? warnIgnored - : constructorWarnIgnored; - - if (shouldWarnIgnored) { - results.push( - createIgnoreResult(resolvedFilename, cwd, configStatus), - ); - } - } else { - const config = configs.getConfig(resolvedFilename); - const fixer = getFixerForFixTypes(fix, fixTypesSet, config); - - // Do lint. - results.push( - verifyText({ - text: code, - filePath: resolvedFilename.endsWith("__placeholder__.js") - ? "" - : resolvedFilename, - configs, - cwd, - fix: fixer, - allowInlineConfig, - ruleFilter, - stats, - linter, - }), - ); - } - - debug("Linting complete in %t", hrtimeBigint() - startTime); - - return processLintReport(this, results); - } - - /** - * Returns the formatter representing the given formatter name. - * @param {string} [name] The name of the formatter to load. - * The following values are allowed: - * - `undefined` ... Load `stylish` builtin formatter. - * - A builtin formatter name ... Load the builtin formatter. - * - A third-party formatter name: - * - `foo` → `eslint-formatter-foo` - * - `@foo` → `@foo/eslint-formatter` - * - `@foo/bar` → `@foo/eslint-formatter-bar` - * - A file path ... Load the file. - * @returns {Promise} A promise resolving to the formatter object. - * This promise will be rejected if the given formatter was not found or not - * a function. - */ - async loadFormatter(name = "stylish") { - if (typeof name !== "string") { - throw new Error("'name' must be a string"); - } - - // replace \ with / for Windows compatibility - const normalizedFormatName = name.replace(/\\/gu, "/"); - const namespace = getNamespaceFromTerm(normalizedFormatName); - - // grab our options - const { cwd } = privateMembers.get(this).options; - - let formatterPath; - - // if there's a slash, then it's a file (TODO: this check seems dubious for scoped npm packages) - if (!namespace && normalizedFormatName.includes("/")) { - formatterPath = path.resolve(cwd, normalizedFormatName); - } else { - try { - const npmFormat = normalizePackageName( - normalizedFormatName, - "eslint-formatter", - ); - - // TODO: This is pretty dirty...would be nice to clean up at some point. - formatterPath = resolve(npmFormat, getPlaceholderPath(cwd)); - } catch { - formatterPath = path.resolve( - __dirname, - "../", - "cli-engine", - "formatters", - `${normalizedFormatName}.js`, - ); - } - } - - let formatter; - - try { - formatter = (await import(pathToFileURL(formatterPath))).default; - } catch (ex) { - // check for formatters that have been removed - if (removedFormatters.has(name)) { - ex.message = `The ${name} formatter is no longer part of core ESLint. Install it manually with \`npm install -D eslint-formatter-${name}\``; - } else { - ex.message = `There was a problem loading formatter: ${formatterPath}\nError: ${ex.message}`; - } - - throw ex; - } - - if (typeof formatter !== "function") { - throw new TypeError( - `Formatter must be a function, but got a ${typeof formatter}.`, - ); - } - - const eslint = this; - - return { - /** - * The main formatter method. - * @param {LintResult[]} results The lint results to format. - * @param {ResultsMeta} resultsMeta Warning count and max threshold. - * @returns {string} The formatted lint results. - */ - format(results, resultsMeta) { - let rulesMeta = null; - - results.sort(compareResultsByFilePath); - - return formatter(results, { - ...resultsMeta, - cwd, - get rulesMeta() { - if (!rulesMeta) { - rulesMeta = eslint.getRulesMetaForResults(results); - } - - return rulesMeta; - }, - }); - }, - }; - } - - /** - * Returns a configuration object for the given file based on the CLI options. - * This is the same logic used by the ESLint CLI executable to determine - * configuration for each file it processes. - * @param {string} filePath The path of the file to retrieve a config object for. - * @returns {Promise} A configuration object for the file - * or `undefined` if there is no configuration data for the object. - */ - async calculateConfigForFile(filePath) { - if (!isNonEmptyString(filePath)) { - throw new Error("'filePath' must be a non-empty string"); - } - const options = privateMembers.get(this).options; - const absolutePath = path.resolve(options.cwd, filePath); - const configs = - await this.#configLoader.loadConfigArrayForFile(absolutePath); - - if (!configs) { - const error = new Error("Could not find config file."); - - error.messageTemplate = "config-file-missing"; - throw error; - } - - return configs.getConfig(absolutePath); - } - - /** - * Finds the config file being used by this instance based on the options - * passed to the constructor. - * @param {string} [filePath] The path of the file to find the config file for. - * @returns {Promise} The path to the config file being used or - * `undefined` if no config file is being used. - */ - async findConfigFile(filePath) { - const options = privateMembers.get(this).options; - - /* - * Because the new config lookup scheme skips the current directory - * and looks into the parent directories, we need to use a placeholder - * directory to ensure the file in cwd is checked. - */ - const fakeCwd = path.join(options.cwd, "__placeholder__"); - - return this.#configLoader - .findConfigFileForPath(filePath ?? fakeCwd) - .catch(() => void 0); - } - - /** - * Checks if a given path is ignored by ESLint. - * @param {string} filePath The path of the file to check. - * @returns {Promise} Whether or not the given path is ignored. - */ - async isPathIgnored(filePath) { - const config = await this.calculateConfigForFile(filePath); - - return config === void 0; - } + + /** + * Creates a new instance of the main ESLint API. + * @param {ESLintOptions} options The options for this instance. + */ + constructor(options = {}) { + const processedOptions = processOptions(options); + const cliEngine = new CLIEngine(processedOptions, { preloadedPlugins: options.plugins }); + const { + configArrayFactory, + lastConfigArrays + } = getCLIEngineInternalSlots(cliEngine); + let updated = false; + + /* + * Address `overrideConfig` to set override config. + * Operate the `configArrayFactory` internal slot directly because this + * functionality doesn't exist as the public API of CLIEngine. + */ + if (hasDefinedProperty(options.overrideConfig)) { + configArrayFactory.setOverrideConfig(options.overrideConfig); + updated = true; + } + + // Update caches. + if (updated) { + configArrayFactory.clearCache(); + lastConfigArrays[0] = configArrayFactory.getConfigArrayForFile(); + } + + // Initialize private properties. + privateMembersMap.set(this, { + cliEngine, + options: processedOptions + }); + } + + /** + * The version text. + * @type {string} + */ + static get version() { + return version; + } + + /** + * Outputs fixes from the given results to files. + * @param {LintResult[]} results The lint results. + * @returns {Promise} Returns a promise that is used to track side effects. + */ + static async outputFixes(results) { + if (!Array.isArray(results)) { + throw new Error("'results' must be an array"); + } + + await Promise.all( + results + .filter(result => { + if (typeof result !== "object" || result === null) { + throw new Error("'results' must include only objects"); + } + return ( + typeof result.output === "string" && + path.isAbsolute(result.filePath) + ); + }) + .map(r => writeFile(r.filePath, r.output)) + ); + } + + /** + * Returns results that only contains errors. + * @param {LintResult[]} results The results to filter. + * @returns {LintResult[]} The filtered results. + */ + static getErrorResults(results) { + return CLIEngine.getErrorResults(results); + } + + /** + * Returns meta objects for each rule represented in the lint results. + * @param {LintResult[]} results The results to fetch rules meta for. + * @returns {Object} A mapping of ruleIds to rule meta objects. + */ + getRulesMetaForResults(results) { + + const resultRuleIds = new Set(); + + // first gather all ruleIds from all results + + for (const result of results) { + for (const { ruleId } of result.messages) { + resultRuleIds.add(ruleId); + } + for (const { ruleId } of result.suppressedMessages) { + resultRuleIds.add(ruleId); + } + } + + // create a map of all rules in the results + + const { cliEngine } = privateMembersMap.get(this); + const rules = cliEngine.getRules(); + const resultRules = new Map(); + + for (const [ruleId, rule] of rules) { + if (resultRuleIds.has(ruleId)) { + resultRules.set(ruleId, rule); + } + } + + return createRulesMeta(resultRules); + + } + + /** + * Executes the current configuration on an array of file and directory names. + * @param {string[]} patterns An array of file and directory names. + * @returns {Promise} The results of linting the file patterns given. + */ + async lintFiles(patterns) { + if (!isNonEmptyString(patterns) && !isArrayOfNonEmptyString(patterns)) { + throw new Error("'patterns' must be a non-empty string or an array of non-empty strings"); + } + const { cliEngine } = privateMembersMap.get(this); + + return processCLIEngineLintReport( + cliEngine, + cliEngine.executeOnFiles(patterns) + ); + } + + /** + * Executes the current configuration on text. + * @param {string} code A string of JavaScript code to lint. + * @param {Object} [options] The options. + * @param {string} [options.filePath] The path to the file of the source code. + * @param {boolean} [options.warnIgnored] When set to true, warn if given filePath is an ignored path. + * @returns {Promise} The results of linting the string of code given. + */ + async lintText(code, options = {}) { + if (typeof code !== "string") { + throw new Error("'code' must be a string"); + } + if (typeof options !== "object") { + throw new Error("'options' must be an object, null, or undefined"); + } + const { + filePath, + warnIgnored = false, + ...unknownOptions + } = options || {}; + + const unknownOptionKeys = Object.keys(unknownOptions); + + if (unknownOptionKeys.length > 0) { + throw new Error(`'options' must not include the unknown option(s): ${unknownOptionKeys.join(", ")}`); + } + + if (filePath !== void 0 && !isNonEmptyString(filePath)) { + throw new Error("'options.filePath' must be a non-empty string or undefined"); + } + if (typeof warnIgnored !== "boolean") { + throw new Error("'options.warnIgnored' must be a boolean or undefined"); + } + + const { cliEngine } = privateMembersMap.get(this); + + return processCLIEngineLintReport( + cliEngine, + cliEngine.executeOnText(code, filePath, warnIgnored) + ); + } + + /** + * Returns the formatter representing the given formatter name. + * @param {string} [name] The name of the formatter to load. + * The following values are allowed: + * - `undefined` ... Load `stylish` builtin formatter. + * - A builtin formatter name ... Load the builtin formatter. + * - A third-party formatter name: + * - `foo` → `eslint-formatter-foo` + * - `@foo` → `@foo/eslint-formatter` + * - `@foo/bar` → `@foo/eslint-formatter-bar` + * - A file path ... Load the file. + * @returns {Promise} A promise resolving to the formatter object. + * This promise will be rejected if the given formatter was not found or not + * a function. + */ + async loadFormatter(name = "stylish") { + if (typeof name !== "string") { + throw new Error("'name' must be a string"); + } + + const { cliEngine, options } = privateMembersMap.get(this); + const formatter = cliEngine.getFormatter(name); + + if (typeof formatter !== "function") { + throw new Error(`Formatter must be a function, but got a ${typeof formatter}.`); + } + + return { + + /** + * The main formatter method. + * @param {LintResult[]} results The lint results to format. + * @param {ResultsMeta} resultsMeta Warning count and max threshold. + * @returns {string | Promise} The formatted lint results. + */ + format(results, resultsMeta) { + let rulesMeta = null; + + results.sort(compareResultsByFilePath); + + return formatter(results, { + ...resultsMeta, + get cwd() { + return options.cwd; + }, + get rulesMeta() { + if (!rulesMeta) { + rulesMeta = createRulesMeta(cliEngine.getRules()); + } + + return rulesMeta; + } + }); + } + }; + } + + /** + * Returns a configuration object for the given file based on the CLI options. + * This is the same logic used by the ESLint CLI executable to determine + * configuration for each file it processes. + * @param {string} filePath The path of the file to retrieve a config object for. + * @returns {Promise} A configuration object for the file. + */ + async calculateConfigForFile(filePath) { + if (!isNonEmptyString(filePath)) { + throw new Error("'filePath' must be a non-empty string"); + } + const { cliEngine } = privateMembersMap.get(this); + + return cliEngine.getConfigForFile(filePath); + } + + /** + * Checks if a given path is ignored by ESLint. + * @param {string} filePath The path of the file to check. + * @returns {Promise} Whether or not the given path is ignored. + */ + async isPathIgnored(filePath) { + if (!isNonEmptyString(filePath)) { + throw new Error("'filePath' must be a non-empty string"); + } + const { cliEngine } = privateMembersMap.get(this); + + return cliEngine.isPathIgnored(filePath); + } } /** - * Returns whether flat config should be used. - * @returns {Promise} Whether flat config should be used. + * The type of configuration used by this class. + * @type {string} + * @static */ -async function shouldUseFlatConfig() { - return process.env.ESLINT_USE_FLAT_CONFIG !== "false"; -} +ESLint.configType = "eslintrc"; //------------------------------------------------------------------------------ // Public Interface //------------------------------------------------------------------------------ module.exports = { - ESLint, - shouldUseFlatConfig, - locateConfigFileToUse, - calculateWorkerCount, + ESLint, + + /** + * Get the private class members of a given ESLint instance for tests. + * @param {ESLint} instance The ESLint instance to get. + * @returns {ESLintPrivateMembers} The instance's private class members. + */ + getESLintPrivateMembers(instance) { + return privateMembersMap.get(instance); + } }; diff --git a/node_modules/eslint/lib/eslint/index.js b/node_modules/eslint/lib/eslint/index.js index 40588d5d4..017b768ec 100644 --- a/node_modules/eslint/lib/eslint/index.js +++ b/node_modules/eslint/lib/eslint/index.js @@ -1,9 +1,9 @@ "use strict"; const { ESLint } = require("./eslint"); -const { LegacyESLint } = require("./legacy-eslint"); +const { FlatESLint } = require("./flat-eslint"); module.exports = { - ESLint, - LegacyESLint, + ESLint, + FlatESLint }; diff --git a/node_modules/eslint/lib/eslint/legacy-eslint.js b/node_modules/eslint/lib/eslint/legacy-eslint.js deleted file mode 100644 index 36d30e5a3..000000000 --- a/node_modules/eslint/lib/eslint/legacy-eslint.js +++ /dev/null @@ -1,786 +0,0 @@ -/** - * @fileoverview Main API Class - * @author Kai Cataldo - * @author Toru Nagashima - */ - -"use strict"; - -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ - -const path = require("node:path"); -const fs = require("node:fs"); -const { promisify } = require("node:util"); -const { - CLIEngine, - getCLIEngineInternalSlots, -} = require("../cli-engine/cli-engine"); -const BuiltinRules = require("../rules"); -const { - Legacy: { - ConfigOps: { getRuleSeverity }, - }, -} = require("@eslint/eslintrc"); -const { version } = require("../../package.json"); - -//------------------------------------------------------------------------------ -// Typedefs -//------------------------------------------------------------------------------ - -/** @typedef {import("../cli-engine/cli-engine").LintReport} CLIEngineLintReport */ -/** @typedef {import("../types").ESLint.ConfigData} ConfigData */ -/** @typedef {import("../types").ESLint.DeprecatedRuleUse} DeprecatedRuleInfo */ -/** @typedef {import("../types").Linter.LintMessage} LintMessage */ -/** @typedef {import("../types").ESLint.LintResult} LintResult */ -/** @typedef {import("../types").ESLint.Plugin} Plugin */ -/** @typedef {import("../types").ESLint.ResultsMeta} ResultsMeta */ -/** @typedef {import("../types").Rule.RuleModule} Rule */ -/** @typedef {import("../types").Linter.SuppressedLintMessage} SuppressedLintMessage */ - -/** - * The main formatter object. - * @typedef LoadedFormatter - * @property {(results: LintResult[], resultsMeta: ResultsMeta) => string | Promise} format format function. - */ - -/** - * The options with which to configure the LegacyESLint instance. - * @typedef {Object} LegacyESLintOptions - * @property {boolean} [allowInlineConfig] Enable or disable inline configuration comments. - * @property {ConfigData} [baseConfig] Base config object, extended by all configs used with this instance - * @property {boolean} [cache] Enable result caching. - * @property {string} [cacheLocation] The cache file to use instead of .eslintcache. - * @property {"metadata" | "content"} [cacheStrategy] The strategy used to detect changed files. - * @property {string} [cwd] The value to use for the current working directory. - * @property {boolean} [errorOnUnmatchedPattern] If `false` then `ESLint#lintFiles()` doesn't throw even if no target files found. Defaults to `true`. - * @property {string[]} [extensions] An array of file extensions to check. - * @property {boolean|Function} [fix] Execute in autofix mode. If a function, should return a boolean. - * @property {string[]} [fixTypes] Array of rule types to apply fixes for. - * @property {boolean} [globInputPaths] Set to false to skip glob resolution of input file paths to lint (default: true). If false, each input file paths is assumed to be a non-glob path to an existing file. - * @property {boolean} [ignore] False disables use of .eslintignore. - * @property {string} [ignorePath] The ignore file to use instead of .eslintignore. - * @property {ConfigData} [overrideConfig] Override config object, overrides all configs used with this instance - * @property {string} [overrideConfigFile] The configuration file to use. - * @property {Record|null} [plugins] Preloaded plugins. This is a map-like object, keys are plugin IDs and each value is implementation. - * @property {"error" | "warn" | "off"} [reportUnusedDisableDirectives] the severity to report unused eslint-disable directives. - * @property {string} [resolvePluginsRelativeTo] The folder where plugins should be resolved from, defaulting to the CWD. - * @property {string[]} [rulePaths] An array of directories to load custom rules from. - * @property {boolean} [useEslintrc] False disables looking for .eslintrc.* files. - * @property {boolean} [passOnNoPatterns=false] When set to true, missing patterns cause - * the linting operation to short circuit and not report any failures. - */ - -/** - * A rules metadata object. - * @typedef {Object} RulesMeta - * @property {string} id The plugin ID. - * @property {Object} definition The plugin definition. - */ - -/** - * Private members for the `ESLint` instance. - * @typedef {Object} ESLintPrivateMembers - * @property {CLIEngine} cliEngine The wrapped CLIEngine instance. - * @property {LegacyESLintOptions} options The options used to instantiate the ESLint instance. - */ - -//------------------------------------------------------------------------------ -// Helpers -//------------------------------------------------------------------------------ - -const writeFile = promisify(fs.writeFile); - -/** - * The map with which to store private class members. - * @type {WeakMap} - */ -const privateMembersMap = new WeakMap(); - -/** - * Check if a given value is a non-empty string or not. - * @param {any} value The value to check. - * @returns {boolean} `true` if `value` is a non-empty string. - */ -function isNonEmptyString(value) { - return typeof value === "string" && value.trim() !== ""; -} - -/** - * Check if a given value is an array of non-empty strings or not. - * @param {any} value The value to check. - * @returns {boolean} `true` if `value` is an array of non-empty strings. - */ -function isArrayOfNonEmptyString(value) { - return ( - Array.isArray(value) && value.length && value.every(isNonEmptyString) - ); -} - -/** - * Check if a given value is an empty array or an array of non-empty strings. - * @param {any} value The value to check. - * @returns {boolean} `true` if `value` is an empty array or an array of non-empty - * strings. - */ -function isEmptyArrayOrArrayOfNonEmptyString(value) { - return Array.isArray(value) && value.every(isNonEmptyString); -} - -/** - * Check if a given value is a valid fix type or not. - * @param {any} value The value to check. - * @returns {boolean} `true` if `value` is valid fix type. - */ -function isFixType(value) { - return ( - value === "directive" || - value === "problem" || - value === "suggestion" || - value === "layout" - ); -} - -/** - * Check if a given value is an array of fix types or not. - * @param {any} value The value to check. - * @returns {boolean} `true` if `value` is an array of fix types. - */ -function isFixTypeArray(value) { - return Array.isArray(value) && value.every(isFixType); -} - -/** - * The error for invalid options. - */ -class ESLintInvalidOptionsError extends Error { - constructor(messages) { - super(`Invalid Options:\n- ${messages.join("\n- ")}`); - this.code = "ESLINT_INVALID_OPTIONS"; - Error.captureStackTrace(this, ESLintInvalidOptionsError); - } -} - -/** - * Validates and normalizes options for the wrapped CLIEngine instance. - * @param {LegacyESLintOptions} options The options to process. - * @throws {ESLintInvalidOptionsError} If of any of a variety of type errors. - * @returns {LegacyESLintOptions} The normalized options. - */ -function processOptions({ - allowInlineConfig = true, // ← we cannot use `overrideConfig.noInlineConfig` instead because `allowInlineConfig` has side-effect that suppress warnings that show inline configs are ignored. - baseConfig = null, - cache = false, - cacheLocation = ".eslintcache", - cacheStrategy = "metadata", - cwd = process.cwd(), - errorOnUnmatchedPattern = true, - extensions = null, // ← should be null by default because if it's an array then it suppresses RFC20 feature. - fix = false, - fixTypes = null, // ← should be null by default because if it's an array then it suppresses rules that don't have the `meta.type` property. - flags /* eslint-disable-line no-unused-vars -- leaving for compatibility with ESLint#hasFlag */, - globInputPaths = true, - ignore = true, - ignorePath = null, // ← should be null by default because if it's a string then it may throw ENOENT. - overrideConfig = null, - overrideConfigFile = null, - plugins = {}, - reportUnusedDisableDirectives = null, // ← should be null by default because if it's a string then it overrides the 'reportUnusedDisableDirectives' setting in config files. And we cannot use `overrideConfig.reportUnusedDisableDirectives` instead because we cannot configure the `error` severity with that. - resolvePluginsRelativeTo = null, // ← should be null by default because if it's a string then it suppresses RFC47 feature. - rulePaths = [], - useEslintrc = true, - passOnNoPatterns = false, - ...unknownOptions -}) { - const errors = []; - const unknownOptionKeys = Object.keys(unknownOptions); - - if (unknownOptionKeys.length >= 1) { - errors.push(`Unknown options: ${unknownOptionKeys.join(", ")}`); - if (unknownOptionKeys.includes("cacheFile")) { - errors.push( - "'cacheFile' has been removed. Please use the 'cacheLocation' option instead.", - ); - } - if (unknownOptionKeys.includes("configFile")) { - errors.push( - "'configFile' has been removed. Please use the 'overrideConfigFile' option instead.", - ); - } - if (unknownOptionKeys.includes("envs")) { - errors.push( - "'envs' has been removed. Please use the 'overrideConfig.env' option instead.", - ); - } - if (unknownOptionKeys.includes("globals")) { - errors.push( - "'globals' has been removed. Please use the 'overrideConfig.globals' option instead.", - ); - } - if (unknownOptionKeys.includes("ignorePattern")) { - errors.push( - "'ignorePattern' has been removed. Please use the 'overrideConfig.ignorePatterns' option instead.", - ); - } - if (unknownOptionKeys.includes("parser")) { - errors.push( - "'parser' has been removed. Please use the 'overrideConfig.parser' option instead.", - ); - } - if (unknownOptionKeys.includes("parserOptions")) { - errors.push( - "'parserOptions' has been removed. Please use the 'overrideConfig.parserOptions' option instead.", - ); - } - if (unknownOptionKeys.includes("rules")) { - errors.push( - "'rules' has been removed. Please use the 'overrideConfig.rules' option instead.", - ); - } - } - if (typeof allowInlineConfig !== "boolean") { - errors.push("'allowInlineConfig' must be a boolean."); - } - if (typeof baseConfig !== "object") { - errors.push("'baseConfig' must be an object or null."); - } - if (typeof cache !== "boolean") { - errors.push("'cache' must be a boolean."); - } - if (!isNonEmptyString(cacheLocation)) { - errors.push("'cacheLocation' must be a non-empty string."); - } - if (cacheStrategy !== "metadata" && cacheStrategy !== "content") { - errors.push('\'cacheStrategy\' must be any of "metadata", "content".'); - } - if (!isNonEmptyString(cwd) || !path.isAbsolute(cwd)) { - errors.push("'cwd' must be an absolute path."); - } - if (typeof errorOnUnmatchedPattern !== "boolean") { - errors.push("'errorOnUnmatchedPattern' must be a boolean."); - } - if ( - !isEmptyArrayOrArrayOfNonEmptyString(extensions) && - extensions !== null - ) { - errors.push( - "'extensions' must be an array of non-empty strings or null.", - ); - } - if (typeof fix !== "boolean" && typeof fix !== "function") { - errors.push("'fix' must be a boolean or a function."); - } - if (fixTypes !== null && !isFixTypeArray(fixTypes)) { - errors.push( - '\'fixTypes\' must be an array of any of "directive", "problem", "suggestion", and "layout".', - ); - } - if (typeof globInputPaths !== "boolean") { - errors.push("'globInputPaths' must be a boolean."); - } - if (typeof ignore !== "boolean") { - errors.push("'ignore' must be a boolean."); - } - if (!isNonEmptyString(ignorePath) && ignorePath !== null) { - errors.push("'ignorePath' must be a non-empty string or null."); - } - if (typeof overrideConfig !== "object") { - errors.push("'overrideConfig' must be an object or null."); - } - if (!isNonEmptyString(overrideConfigFile) && overrideConfigFile !== null) { - errors.push("'overrideConfigFile' must be a non-empty string or null."); - } - if (typeof plugins !== "object") { - errors.push("'plugins' must be an object or null."); - } else if (plugins !== null && Object.keys(plugins).includes("")) { - errors.push("'plugins' must not include an empty string."); - } - if (Array.isArray(plugins)) { - errors.push( - "'plugins' doesn't add plugins to configuration to load. Please use the 'overrideConfig.plugins' option instead.", - ); - } - if ( - reportUnusedDisableDirectives !== "error" && - reportUnusedDisableDirectives !== "warn" && - reportUnusedDisableDirectives !== "off" && - reportUnusedDisableDirectives !== null - ) { - errors.push( - '\'reportUnusedDisableDirectives\' must be any of "error", "warn", "off", and null.', - ); - } - if ( - !isNonEmptyString(resolvePluginsRelativeTo) && - resolvePluginsRelativeTo !== null - ) { - errors.push( - "'resolvePluginsRelativeTo' must be a non-empty string or null.", - ); - } - if (!isEmptyArrayOrArrayOfNonEmptyString(rulePaths)) { - errors.push("'rulePaths' must be an array of non-empty strings."); - } - if (typeof useEslintrc !== "boolean") { - errors.push("'useEslintrc' must be a boolean."); - } - if (typeof passOnNoPatterns !== "boolean") { - errors.push("'passOnNoPatterns' must be a boolean."); - } - - if (errors.length > 0) { - throw new ESLintInvalidOptionsError(errors); - } - - return { - allowInlineConfig, - baseConfig, - cache, - cacheLocation, - cacheStrategy, - configFile: overrideConfigFile, - cwd: path.normalize(cwd), - errorOnUnmatchedPattern, - extensions, - fix, - fixTypes, - flags: [], // LegacyESLint does not support flags, so just ignore them. - globInputPaths, - ignore, - ignorePath, - reportUnusedDisableDirectives, - resolvePluginsRelativeTo, - rulePaths, - useEslintrc, - passOnNoPatterns, - }; -} - -/** - * Check if a value has one or more properties and that value is not undefined. - * @param {any} obj The value to check. - * @returns {boolean} `true` if `obj` has one or more properties that value is not undefined. - */ -function hasDefinedProperty(obj) { - if (typeof obj === "object" && obj !== null) { - for (const key in obj) { - if (typeof obj[key] !== "undefined") { - return true; - } - } - } - return false; -} - -/** - * Create rulesMeta object. - * @param {Map} rules a map of rules from which to generate the object. - * @returns {Object} metadata for all enabled rules. - */ -function createRulesMeta(rules) { - return Array.from(rules).reduce((retVal, [id, rule]) => { - retVal[id] = rule.meta; - return retVal; - }, {}); -} - -/** @type {WeakMap} */ -const usedDeprecatedRulesCache = new WeakMap(); - -/** - * Create used deprecated rule list. - * @param {CLIEngine} cliEngine The CLIEngine instance. - * @param {string} maybeFilePath The absolute path to a lint target file or `""`. - * @returns {DeprecatedRuleInfo[]} The used deprecated rule list. - */ -function getOrFindUsedDeprecatedRules(cliEngine, maybeFilePath) { - const { - configArrayFactory, - options: { cwd }, - } = getCLIEngineInternalSlots(cliEngine); - const filePath = path.isAbsolute(maybeFilePath) - ? maybeFilePath - : path.join(cwd, "__placeholder__.js"); - const configArray = configArrayFactory.getConfigArrayForFile(filePath); - const config = configArray.extractConfig(filePath); - - // Most files use the same config, so cache it. - if (!usedDeprecatedRulesCache.has(config)) { - const pluginRules = configArray.pluginRules; - const retv = []; - - for (const [ruleId, ruleConf] of Object.entries(config.rules)) { - if (getRuleSeverity(ruleConf) === 0) { - continue; - } - const rule = pluginRules.get(ruleId) || BuiltinRules.get(ruleId); - const meta = rule && rule.meta; - - if (meta && meta.deprecated) { - retv.push({ ruleId, replacedBy: meta.replacedBy || [] }); - } - } - - usedDeprecatedRulesCache.set(config, Object.freeze(retv)); - } - - return usedDeprecatedRulesCache.get(config); -} - -/** - * Processes the linting results generated by a CLIEngine linting report to - * match the ESLint class's API. - * @param {CLIEngine} cliEngine The CLIEngine instance. - * @param {CLIEngineLintReport} report The CLIEngine linting report to process. - * @returns {LintResult[]} The processed linting results. - */ -function processCLIEngineLintReport(cliEngine, { results }) { - const descriptor = { - configurable: true, - enumerable: true, - get() { - return getOrFindUsedDeprecatedRules(cliEngine, this.filePath); - }, - }; - - for (const result of results) { - Object.defineProperty(result, "usedDeprecatedRules", descriptor); - } - - return results; -} - -/** - * An Array.prototype.sort() compatible compare function to order results by their file path. - * @param {LintResult} a The first lint result. - * @param {LintResult} b The second lint result. - * @returns {number} An integer representing the order in which the two results should occur. - */ -function compareResultsByFilePath(a, b) { - if (a.filePath < b.filePath) { - return -1; - } - - if (a.filePath > b.filePath) { - return 1; - } - - return 0; -} - -/** - * Main API. - */ -class LegacyESLint { - /** - * The type of configuration used by this class. - * @type {string} - */ - static configType = "eslintrc"; - - /** - * Creates a new instance of the main ESLint API. - * @param {LegacyESLintOptions} options The options for this instance. - */ - constructor(options = {}) { - const processedOptions = processOptions(options); - const cliEngine = new CLIEngine(processedOptions, { - preloadedPlugins: options.plugins, - }); - const { configArrayFactory, lastConfigArrays } = - getCLIEngineInternalSlots(cliEngine); - let updated = false; - - /* - * Address `overrideConfig` to set override config. - * Operate the `configArrayFactory` internal slot directly because this - * functionality doesn't exist as the public API of CLIEngine. - */ - if (hasDefinedProperty(options.overrideConfig)) { - configArrayFactory.setOverrideConfig(options.overrideConfig); - updated = true; - } - - // Update caches. - if (updated) { - configArrayFactory.clearCache(); - lastConfigArrays[0] = configArrayFactory.getConfigArrayForFile(); - } - - // Initialize private properties. - privateMembersMap.set(this, { - cliEngine, - options: processedOptions, - }); - } - - /** - * The version text. - * @type {string} - */ - static get version() { - return version; - } - - /** - * Outputs fixes from the given results to files. - * @param {LintResult[]} results The lint results. - * @returns {Promise} Returns a promise that is used to track side effects. - */ - static async outputFixes(results) { - if (!Array.isArray(results)) { - throw new Error("'results' must be an array"); - } - - await Promise.all( - results - .filter(result => { - if (typeof result !== "object" || result === null) { - throw new Error("'results' must include only objects"); - } - return ( - typeof result.output === "string" && - path.isAbsolute(result.filePath) - ); - }) - .map(r => writeFile(r.filePath, r.output)), - ); - } - - /** - * Returns results that only contains errors. - * @param {LintResult[]} results The results to filter. - * @returns {LintResult[]} The filtered results. - */ - static getErrorResults(results) { - return CLIEngine.getErrorResults(results); - } - - /** - * Returns meta objects for each rule represented in the lint results. - * @param {LintResult[]} results The results to fetch rules meta for. - * @returns {Object} A mapping of ruleIds to rule meta objects. - */ - getRulesMetaForResults(results) { - const resultRuleIds = new Set(); - - // first gather all ruleIds from all results - - for (const result of results) { - for (const { ruleId } of result.messages) { - resultRuleIds.add(ruleId); - } - for (const { ruleId } of result.suppressedMessages) { - resultRuleIds.add(ruleId); - } - } - - // create a map of all rules in the results - - const { cliEngine } = privateMembersMap.get(this); - const rules = cliEngine.getRules(); - const resultRules = new Map(); - - for (const [ruleId, rule] of rules) { - if (resultRuleIds.has(ruleId)) { - resultRules.set(ruleId, rule); - } - } - - return createRulesMeta(resultRules); - } - - /* eslint-disable no-unused-vars, class-methods-use-this -- leaving for compatibility with ESLint#hasFlag */ - /** - * Indicates if the given feature flag is enabled for this instance. For this - * class, this always returns `false` because it does not support feature flags. - * @param {string} flag The feature flag to check. - * @returns {boolean} Always false. - */ - hasFlag(flag) { - return false; - } - /* eslint-enable no-unused-vars, class-methods-use-this -- reenable rules for the rest of the file */ - - /** - * Executes the current configuration on an array of file and directory names. - * @param {string[]} patterns An array of file and directory names. - * @returns {Promise} The results of linting the file patterns given. - */ - async lintFiles(patterns) { - const { cliEngine, options } = privateMembersMap.get(this); - - if ( - options.passOnNoPatterns && - (patterns === "" || - (Array.isArray(patterns) && patterns.length === 0)) - ) { - return []; - } - - if (!isNonEmptyString(patterns) && !isArrayOfNonEmptyString(patterns)) { - throw new Error( - "'patterns' must be a non-empty string or an array of non-empty strings", - ); - } - - return processCLIEngineLintReport( - cliEngine, - cliEngine.executeOnFiles(patterns), - ); - } - - /** - * Executes the current configuration on text. - * @param {string} code A string of JavaScript code to lint. - * @param {Object} [options] The options. - * @param {string} [options.filePath] The path to the file of the source code. - * @param {boolean} [options.warnIgnored] When set to true, warn if given filePath is an ignored path. - * @returns {Promise} The results of linting the string of code given. - */ - async lintText(code, options = {}) { - if (typeof code !== "string") { - throw new Error("'code' must be a string"); - } - if (typeof options !== "object") { - throw new Error("'options' must be an object, null, or undefined"); - } - const { - filePath, - warnIgnored = false, - ...unknownOptions - } = options || {}; - - const unknownOptionKeys = Object.keys(unknownOptions); - - if (unknownOptionKeys.length > 0) { - throw new Error( - `'options' must not include the unknown option(s): ${unknownOptionKeys.join(", ")}`, - ); - } - - if (filePath !== void 0 && !isNonEmptyString(filePath)) { - throw new Error( - "'options.filePath' must be a non-empty string or undefined", - ); - } - if (typeof warnIgnored !== "boolean") { - throw new Error( - "'options.warnIgnored' must be a boolean or undefined", - ); - } - - const { cliEngine } = privateMembersMap.get(this); - - return processCLIEngineLintReport( - cliEngine, - cliEngine.executeOnText(code, filePath, warnIgnored), - ); - } - - /** - * Returns the formatter representing the given formatter name. - * @param {string} [name] The name of the formatter to load. - * The following values are allowed: - * - `undefined` ... Load `stylish` builtin formatter. - * - A builtin formatter name ... Load the builtin formatter. - * - A third-party formatter name: - * - `foo` → `eslint-formatter-foo` - * - `@foo` → `@foo/eslint-formatter` - * - `@foo/bar` → `@foo/eslint-formatter-bar` - * - A file path ... Load the file. - * @returns {Promise} A promise resolving to the formatter object. - * This promise will be rejected if the given formatter was not found or not - * a function. - */ - async loadFormatter(name = "stylish") { - if (typeof name !== "string") { - throw new Error("'name' must be a string"); - } - - const { cliEngine, options } = privateMembersMap.get(this); - const formatter = cliEngine.getFormatter(name); - - if (typeof formatter !== "function") { - throw new Error( - `Formatter must be a function, but got a ${typeof formatter}.`, - ); - } - - return { - /** - * The main formatter method. - * @param {LintResult[]} results The lint results to format. - * @param {ResultsMeta} resultsMeta Warning count and max threshold. - * @returns {string | Promise} The formatted lint results. - */ - format(results, resultsMeta) { - let rulesMeta = null; - - results.sort(compareResultsByFilePath); - - return formatter(results, { - ...resultsMeta, - get cwd() { - return options.cwd; - }, - get rulesMeta() { - if (!rulesMeta) { - rulesMeta = createRulesMeta(cliEngine.getRules()); - } - - return rulesMeta; - }, - }); - }, - }; - } - - /** - * Returns a configuration object for the given file based on the CLI options. - * This is the same logic used by the ESLint CLI executable to determine - * configuration for each file it processes. - * @param {string} filePath The path of the file to retrieve a config object for. - * @returns {Promise} A configuration object for the file. - */ - async calculateConfigForFile(filePath) { - if (!isNonEmptyString(filePath)) { - throw new Error("'filePath' must be a non-empty string"); - } - const { cliEngine } = privateMembersMap.get(this); - - return cliEngine.getConfigForFile(filePath); - } - - /** - * Checks if a given path is ignored by ESLint. - * @param {string} filePath The path of the file to check. - * @returns {Promise} Whether or not the given path is ignored. - */ - async isPathIgnored(filePath) { - if (!isNonEmptyString(filePath)) { - throw new Error("'filePath' must be a non-empty string"); - } - const { cliEngine } = privateMembersMap.get(this); - - return cliEngine.isPathIgnored(filePath); - } -} - -//------------------------------------------------------------------------------ -// Public Interface -//------------------------------------------------------------------------------ - -module.exports = { - LegacyESLint, - - /** - * Get the private class members of a given ESLint instance for tests. - * @param {ESLint} instance The ESLint instance to get. - * @returns {ESLintPrivateMembers} The instance's private class members. - */ - getESLintPrivateMembers(instance) { - return privateMembersMap.get(instance); - }, -}; diff --git a/node_modules/eslint/lib/eslint/worker.js b/node_modules/eslint/lib/eslint/worker.js deleted file mode 100644 index 7001bf214..000000000 --- a/node_modules/eslint/lib/eslint/worker.js +++ /dev/null @@ -1,173 +0,0 @@ -/** - * @fileoverview Worker thread for multithread linting. - * @author Francesco Trotta - */ - -"use strict"; - -const hrtimeBigint = process.hrtime.bigint; - -const startTime = hrtimeBigint(); - -// eslint-disable-next-line n/no-unsupported-features/node-builtins -- enable V8's code cache if supported -require("node:module").enableCompileCache?.(); - -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ - -const { parentPort, threadId, workerData } = require("node:worker_threads"); -const { - createConfigLoader, - createDebug, - createDefaultConfigs, - createLinter, - createLintResultCache, - getCacheFile, - lintFile, - loadOptionsFromModule, - processOptions, -} = require("./eslint-helpers"); -const { WarningService } = require("../services/warning-service"); -const timing = require("../linter/timing"); - -const depsLoadedTime = hrtimeBigint(); - -//------------------------------------------------------------------------------ -// Typedefs -//------------------------------------------------------------------------------ - -/** @typedef {import("../types").ESLint.LintResult} LintResult */ -/** @typedef {import("../types").ESLint.Options} ESLintOptions */ -/** @typedef {LintResult & { index?: number; }} IndexedLintResult */ -/** @typedef {IndexedLintResult[] & { netLintingDuration: bigint; timings?: Record; }} WorkerLintResults */ -/** - * @typedef {Object} WorkerData - Data passed to the worker thread. - * @property {ESLintOptions | string} eslintOptionsOrURL - The unprocessed ESLint options or the URL of the options module. - * @property {Uint32Array} filePathIndexArray - Shared counter used to track the next file to lint. - * @property {string[]} filePaths - File paths to lint. - */ - -//------------------------------------------------------------------------------ -// Helpers -//------------------------------------------------------------------------------ - -const debug = createDebug(`eslint:worker:thread-${threadId}`); - -//------------------------------------------------------------------------------ -// Main -//------------------------------------------------------------------------------ - -/* - * Prevent timing module from printing profiling output from worker threads. - * The main thread is responsible for displaying any aggregated timings. - */ -timing.disableDisplay(); - -debug("Dependencies loaded in %t", depsLoadedTime - startTime); - -(async () => { - /** @type {WorkerData} */ - const { eslintOptionsOrURL, filePathIndexArray, filePaths } = workerData; - const eslintOptions = - typeof eslintOptionsOrURL === "object" - ? eslintOptionsOrURL - : await loadOptionsFromModule(eslintOptionsOrURL); - const processedESLintOptions = processOptions(eslintOptions); - - const warningService = new WarningService(); - - // These warnings are always emitted by the controlling thread. - warningService.emitEmptyConfigWarning = - warningService.emitInactiveFlagWarning = () => {}; - - const linter = createLinter(processedESLintOptions, warningService); - - const cacheFilePath = getCacheFile( - processedESLintOptions.cacheLocation, - processedESLintOptions.cwd, - ); - - const lintResultCache = createLintResultCache( - processedESLintOptions, - cacheFilePath, - ); - const defaultConfigs = createDefaultConfigs(eslintOptions.plugins); - - const configLoader = createConfigLoader( - processedESLintOptions, - defaultConfigs, - linter, - warningService, - ); - - /** @type {WorkerLintResults} */ - const indexedResults = []; - let loadConfigTotalDuration = 0n; - const readFileCounter = { duration: 0n }; - - const lintingStartTime = hrtimeBigint(); - debug( - "Linting started %t after dependencies loaded", - lintingStartTime - depsLoadedTime, - ); - - for (;;) { - const fileLintingStartTime = hrtimeBigint(); - - // It seems hard to produce an arithmetic overflow under realistic conditions here. - const index = Atomics.add(filePathIndexArray, 0, 1); - - const filePath = filePaths[index]; - if (!filePath) { - break; - } - - const loadConfigEnterTime = hrtimeBigint(); - const configs = await configLoader.loadConfigArrayForFile(filePath); - const loadConfigExitTime = hrtimeBigint(); - const loadConfigDuration = loadConfigExitTime - loadConfigEnterTime; - debug( - 'Config array for file "%s" loaded in %t', - filePath, - loadConfigDuration, - ); - loadConfigTotalDuration += loadConfigDuration; - - /** @type {IndexedLintResult} */ - const result = await lintFile( - filePath, - configs, - processedESLintOptions, - linter, - lintResultCache, - readFileCounter, - ); - if (result) { - result.index = index; - indexedResults.push(result); - } - - const fileLintingEndTime = hrtimeBigint(); - debug( - 'File "%s" processed in %t', - filePath, - fileLintingEndTime - fileLintingStartTime, - ); - } - - const lintingDuration = hrtimeBigint() - lintingStartTime; - - /* - * The net linting duration is the total linting time minus the time spent loading configs and reading files. - * It captures the processing time dedicated to computation-intensive tasks that are highly parallelizable and not repeated across threads. - */ - indexedResults.netLintingDuration = - lintingDuration - loadConfigTotalDuration - readFileCounter.duration; - - if (timing.enabled) { - indexedResults.timings = timing.getData(); - } - - parentPort.postMessage(indexedResults); -})(); diff --git a/node_modules/eslint/lib/languages/js/index.js b/node_modules/eslint/lib/languages/js/index.js deleted file mode 100644 index a6bf0d7f6..000000000 --- a/node_modules/eslint/lib/languages/js/index.js +++ /dev/null @@ -1,336 +0,0 @@ -/** - * @fileoverview JavaScript Language Object - * @author Nicholas C. Zakas - */ - -"use strict"; - -//----------------------------------------------------------------------------- -// Requirements -//----------------------------------------------------------------------------- - -const { SourceCode } = require("./source-code"); -const createDebug = require("debug"); -const astUtils = require("../../shared/ast-utils"); -const espree = require("espree"); -const eslintScope = require("eslint-scope"); -const evk = require("eslint-visitor-keys"); -const { validateLanguageOptions } = require("./validate-language-options"); -const { LATEST_ECMA_VERSION } = require("../../../conf/ecma-version"); - -//----------------------------------------------------------------------------- -// Type Definitions -//----------------------------------------------------------------------------- - -/** @typedef {import("@eslint/core").File} File */ -/** @typedef {import("@eslint/core").Language} Language */ -/** @typedef {import("@eslint/core").OkParseResult} OkParseResult */ -/** @typedef {import("../../types").Linter.LanguageOptions} JSLanguageOptions */ - -//----------------------------------------------------------------------------- -// Helpers -//----------------------------------------------------------------------------- - -const debug = createDebug("eslint:languages:js"); -const DEFAULT_ECMA_VERSION = 5; -const parserSymbol = Symbol.for("eslint.RuleTester.parser"); - -/** - * Analyze scope of the given AST. - * @param {ASTNode} ast The `Program` node to analyze. - * @param {JSLanguageOptions} languageOptions The parser options. - * @param {Record} visitorKeys The visitor keys. - * @returns {ScopeManager} The analysis result. - */ -function analyzeScope(ast, languageOptions, visitorKeys) { - const parserOptions = languageOptions.parserOptions; - const ecmaFeatures = parserOptions.ecmaFeatures || {}; - const ecmaVersion = languageOptions.ecmaVersion || DEFAULT_ECMA_VERSION; - - return eslintScope.analyze(ast, { - ignoreEval: true, - nodejsScope: ecmaFeatures.globalReturn, - impliedStrict: ecmaFeatures.impliedStrict, - ecmaVersion: typeof ecmaVersion === "number" ? ecmaVersion : 6, - sourceType: languageOptions.sourceType || "script", - childVisitorKeys: visitorKeys || evk.KEYS, - fallback: evk.getKeys, - }); -} - -/** - * Determines if a given object is Espree. - * @param {Object} parser The parser to check. - * @returns {boolean} True if the parser is Espree or false if not. - */ -function isEspree(parser) { - return !!(parser === espree || parser[parserSymbol] === espree); -} - -/** - * Normalize ECMAScript version from the initial config into languageOptions (year) - * format. - * @param {any} [ecmaVersion] ECMAScript version from the initial config - * @returns {number} normalized ECMAScript version - */ -function normalizeEcmaVersionForLanguageOptions(ecmaVersion) { - switch (ecmaVersion) { - case 3: - return 3; - - // void 0 = no ecmaVersion specified so use the default - case 5: - case void 0: - return 5; - - default: - if (typeof ecmaVersion === "number") { - return ecmaVersion >= 2015 ? ecmaVersion : ecmaVersion + 2009; - } - } - - /* - * We default to the latest supported ecmaVersion for everything else. - * Remember, this is for languageOptions.ecmaVersion, which sets the version - * that is used for a number of processes inside of ESLint. It's normally - * safe to assume people want the latest unless otherwise specified. - */ - return LATEST_ECMA_VERSION; -} - -//----------------------------------------------------------------------------- -// Exports -//----------------------------------------------------------------------------- - -/** - * @type {Language} - */ -module.exports = { - fileType: "text", - lineStart: 1, - columnStart: 0, - nodeTypeKey: "type", - visitorKeys: evk.KEYS, - - defaultLanguageOptions: { - sourceType: "module", - ecmaVersion: "latest", - parser: espree, - parserOptions: {}, - }, - - validateLanguageOptions, - - /** - * Normalizes the language options. - * @param {Object} languageOptions The language options to normalize. - * @returns {Object} The normalized language options. - */ - normalizeLanguageOptions(languageOptions) { - languageOptions.ecmaVersion = normalizeEcmaVersionForLanguageOptions( - languageOptions.ecmaVersion, - ); - - // Espree expects this information to be passed in - if (isEspree(languageOptions.parser)) { - const parserOptions = languageOptions.parserOptions; - - if (languageOptions.sourceType) { - parserOptions.sourceType = languageOptions.sourceType; - - if ( - parserOptions.sourceType === "module" && - parserOptions.ecmaFeatures && - parserOptions.ecmaFeatures.globalReturn - ) { - parserOptions.ecmaFeatures.globalReturn = false; - } - } - } - - return languageOptions; - }, - - /** - * Determines if a given node matches a given selector class. - * @param {string} className The class name to check. - * @param {ASTNode} node The node to check. - * @param {Array} ancestry The ancestry of the node. - * @returns {boolean} True if there's a match, false if not. - * @throws {Error} When an unknown class name is passed. - */ - matchesSelectorClass(className, node, ancestry) { - /* - * Copyright (c) 2013, Joel Feenstra - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * * Neither the name of the ESQuery nor the names of its contributors may - * be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL JOEL FEENSTRA BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - - switch (className.toLowerCase()) { - case "statement": - if (node.type.slice(-9) === "Statement") { - return true; - } - - // fallthrough: interface Declaration <: Statement { } - - case "declaration": - return node.type.slice(-11) === "Declaration"; - - case "pattern": - if (node.type.slice(-7) === "Pattern") { - return true; - } - - // fallthrough: interface Expression <: Node, Pattern { } - - case "expression": - return ( - node.type.slice(-10) === "Expression" || - node.type.slice(-7) === "Literal" || - (node.type === "Identifier" && - (ancestry.length === 0 || - ancestry[0].type !== "MetaProperty")) || - node.type === "MetaProperty" - ); - - case "function": - return ( - node.type === "FunctionDeclaration" || - node.type === "FunctionExpression" || - node.type === "ArrowFunctionExpression" - ); - - default: - throw new Error(`Unknown class name: ${className}`); - } - }, - - /** - * Parses the given file into an AST. - * @param {File} file The virtual file to parse. - * @param {Object} options Additional options passed from ESLint. - * @param {JSLanguageOptions} options.languageOptions The language options. - * @returns {Object} The result of parsing. - */ - parse(file, { languageOptions }) { - // Note: BOM already removed - const { body: text, path: filePath } = file; - const textToParse = text.replace( - astUtils.shebangPattern, - (match, captured) => `//${captured}`, - ); - const { ecmaVersion, sourceType, parser } = languageOptions; - const parserOptions = Object.assign( - { ecmaVersion, sourceType }, - languageOptions.parserOptions, - { - loc: true, - range: true, - raw: true, - tokens: true, - comment: true, - eslintVisitorKeys: true, - eslintScopeManager: true, - filePath, - }, - ); - - /* - * Check for parsing errors first. If there's a parsing error, nothing - * else can happen. However, a parsing error does not throw an error - * from this method - it's just considered a fatal error message, a - * problem that ESLint identified just like any other. - */ - try { - debug("Parsing:", filePath); - const parseResult = - typeof parser.parseForESLint === "function" - ? parser.parseForESLint(textToParse, parserOptions) - : { ast: parser.parse(textToParse, parserOptions) }; - - debug("Parsing successful:", filePath); - - const { - ast, - services: parserServices = {}, - visitorKeys = evk.KEYS, - scopeManager, - } = parseResult; - - return { - ok: true, - ast, - parserServices, - visitorKeys, - scopeManager, - }; - } catch (ex) { - // If the message includes a leading line number, strip it: - const message = ex.message.replace(/^line \d+:/iu, "").trim(); - - debug("%s\n%s", message, ex.stack); - - return { - ok: false, - errors: [ - { - message, - line: ex.lineNumber, - column: ex.column, - }, - ], - }; - } - }, - - /** - * Creates a new `SourceCode` object from the given information. - * @param {File} file The virtual file to create a `SourceCode` object from. - * @param {OkParseResult} parseResult The result returned from `parse()`. - * @param {Object} options Additional options passed from ESLint. - * @param {JSLanguageOptions} options.languageOptions The language options. - * @returns {SourceCode} The new `SourceCode` object. - */ - createSourceCode(file, parseResult, { languageOptions }) { - const { body: text, path: filePath, bom: hasBOM } = file; - const { ast, parserServices, visitorKeys } = parseResult; - - debug("Scope analysis:", filePath); - const scopeManager = - parseResult.scopeManager || - analyzeScope(ast, languageOptions, visitorKeys); - - debug("Scope analysis successful:", filePath); - - return new SourceCode({ - text, - ast, - hasBOM, - parserServices, - scopeManager, - visitorKeys, - }); - }, -}; diff --git a/node_modules/eslint/lib/languages/js/source-code/index.js b/node_modules/eslint/lib/languages/js/source-code/index.js deleted file mode 100644 index f4003e393..000000000 --- a/node_modules/eslint/lib/languages/js/source-code/index.js +++ /dev/null @@ -1,7 +0,0 @@ -"use strict"; - -const SourceCode = require("./source-code"); - -module.exports = { - SourceCode, -}; diff --git a/node_modules/eslint/lib/languages/js/source-code/source-code.js b/node_modules/eslint/lib/languages/js/source-code/source-code.js deleted file mode 100644 index f31b0933b..000000000 --- a/node_modules/eslint/lib/languages/js/source-code/source-code.js +++ /dev/null @@ -1,1364 +0,0 @@ -/** - * @fileoverview Abstraction of JavaScript source code. - * @author Nicholas C. Zakas - */ -"use strict"; - -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ - -const { isCommentToken } = require("@eslint-community/eslint-utils"), - TokenStore = require("./token-store"), - astUtils = require("../../../shared/ast-utils"), - Traverser = require("../../../shared/traverser"), - globals = require("../../../../conf/globals"), - { directivesPattern } = require("../../../shared/directives"), - CodePathAnalyzer = require("../../../linter/code-path-analysis/code-path-analyzer"), - { - ConfigCommentParser, - VisitNodeStep, - CallMethodStep, - Directive, - } = require("@eslint/plugin-kit"), - eslintScope = require("eslint-scope"); - -//------------------------------------------------------------------------------ -// Type Definitions -//------------------------------------------------------------------------------ - -/** @typedef {import("eslint-scope").Variable} Variable */ -/** @typedef {import("eslint-scope").Scope} Scope */ -/** @typedef {import("@eslint/core").SourceCode} ISourceCode */ -/** @typedef {import("@eslint/core").Directive} IDirective */ -/** @typedef {import("@eslint/core").TraversalStep} ITraversalStep */ - -//------------------------------------------------------------------------------ -// Private -//------------------------------------------------------------------------------ - -const commentParser = new ConfigCommentParser(); - -/** - * Validates that the given AST has the required information. - * @param {ASTNode} ast The Program node of the AST to check. - * @throws {TypeError} If the AST doesn't contain the correct information. - * @returns {void} - * @private - */ -function validate(ast) { - if (!ast) { - throw new TypeError(`Unexpected empty AST. (${ast})`); - } - - if (!ast.tokens) { - throw new TypeError("AST is missing the tokens array."); - } - - if (!ast.comments) { - throw new TypeError("AST is missing the comments array."); - } - - if (!ast.loc) { - throw new TypeError("AST is missing location information."); - } - - if (!ast.range) { - throw new TypeError("AST is missing range information"); - } -} - -/** - * Retrieves globals for the given ecmaVersion. - * @param {number} ecmaVersion The version to retrieve globals for. - * @returns {Object} The globals for the given ecmaVersion. - */ -function getGlobalsForEcmaVersion(ecmaVersion) { - switch (ecmaVersion) { - case 3: - return globals.es3; - - case 5: - return globals.es5; - - default: - if (ecmaVersion < 2015) { - return globals[`es${ecmaVersion + 2009}`]; - } - - return globals[`es${ecmaVersion}`]; - } -} - -/** - * Check to see if its a ES6 export declaration. - * @param {ASTNode} astNode An AST node. - * @returns {boolean} whether the given node represents an export declaration. - * @private - */ -function looksLikeExport(astNode) { - return ( - astNode.type === "ExportDefaultDeclaration" || - astNode.type === "ExportNamedDeclaration" || - astNode.type === "ExportAllDeclaration" || - astNode.type === "ExportSpecifier" - ); -} - -/** - * Merges two sorted lists into a larger sorted list in O(n) time. - * @param {Token[]} tokens The list of tokens. - * @param {Token[]} comments The list of comments. - * @returns {Token[]} A sorted list of tokens and comments. - * @private - */ -function sortedMerge(tokens, comments) { - const result = []; - let tokenIndex = 0; - let commentIndex = 0; - - while (tokenIndex < tokens.length || commentIndex < comments.length) { - if ( - commentIndex >= comments.length || - (tokenIndex < tokens.length && - tokens[tokenIndex].range[0] < comments[commentIndex].range[0]) - ) { - result.push(tokens[tokenIndex++]); - } else { - result.push(comments[commentIndex++]); - } - } - - return result; -} - -/** - * Normalizes a value for a global in a config - * @param {(boolean|string|null)} configuredValue The value given for a global in configuration or in - * a global directive comment - * @returns {("readonly"|"writable"|"off")} The value normalized as a string - * @throws {Error} if global value is invalid - */ -function normalizeConfigGlobal(configuredValue) { - switch (configuredValue) { - case "off": - return "off"; - - case true: - case "true": - case "writeable": - case "writable": - return "writable"; - - case null: - case false: - case "false": - case "readable": - case "readonly": - return "readonly"; - - default: - throw new Error( - `'${configuredValue}' is not a valid configuration for a global (use 'readonly', 'writable', or 'off')`, - ); - } -} - -/** - * Determines if two nodes or tokens overlap. - * @param {ASTNode|Token} first The first node or token to check. - * @param {ASTNode|Token} second The second node or token to check. - * @returns {boolean} True if the two nodes or tokens overlap. - * @private - */ -function nodesOrTokensOverlap(first, second) { - return ( - (first.range[0] <= second.range[0] && - first.range[1] >= second.range[0]) || - (second.range[0] <= first.range[0] && second.range[1] >= first.range[0]) - ); -} - -/** - * Determines if two nodes or tokens have at least one whitespace character - * between them. Order does not matter. Returns false if the given nodes or - * tokens overlap. - * @param {SourceCode} sourceCode The source code object. - * @param {ASTNode|Token} first The first node or token to check between. - * @param {ASTNode|Token} second The second node or token to check between. - * @param {boolean} checkInsideOfJSXText If `true` is present, check inside of JSXText tokens for backward compatibility. - * @returns {boolean} True if there is a whitespace character between - * any of the tokens found between the two given nodes or tokens. - * @public - */ -function isSpaceBetween(sourceCode, first, second, checkInsideOfJSXText) { - if (nodesOrTokensOverlap(first, second)) { - return false; - } - - const [startingNodeOrToken, endingNodeOrToken] = - first.range[1] <= second.range[0] ? [first, second] : [second, first]; - const firstToken = - sourceCode.getLastToken(startingNodeOrToken) || startingNodeOrToken; - const finalToken = - sourceCode.getFirstToken(endingNodeOrToken) || endingNodeOrToken; - let currentToken = firstToken; - - while (currentToken !== finalToken) { - const nextToken = sourceCode.getTokenAfter(currentToken, { - includeComments: true, - }); - - if ( - currentToken.range[1] !== nextToken.range[0] || - /* - * For backward compatibility, check spaces in JSXText. - * https://github.com/eslint/eslint/issues/12614 - */ - (checkInsideOfJSXText && - nextToken !== finalToken && - nextToken.type === "JSXText" && - /\s/u.test(nextToken.value)) - ) { - return true; - } - - currentToken = nextToken; - } - - return false; -} - -/** - * Performs binary search to find the line number containing a given character index. - * Returns the lower bound - the index of the first element greater than the target. - * **Please note that the `lineStartIndices` should be sorted in ascending order**. - * - Time Complexity: O(log n) - Significantly faster than linear search for large files. - * @param {number[]} lineStartIndices Sorted array of line start indices. - * @param {number} target The character index to find the line number for. - * @returns {number} The 1-based line number for the target index. - * @private - */ -function findLineNumberBinarySearch(lineStartIndices, target) { - let low = 0; - let high = lineStartIndices.length; - - while (low < high) { - const mid = ((low + high) / 2) | 0; // Use bitwise OR to floor the division - - if (target < lineStartIndices[mid]) { - high = mid; - } else { - low = mid + 1; - } - } - - return low; -} - -//----------------------------------------------------------------------------- -// Directive Comments -//----------------------------------------------------------------------------- - -/** - * Ensures that variables representing built-in properties of the Global Object, - * and any globals declared by special block comments, are present in the global - * scope. - * @param {Scope} globalScope The global scope. - * @param {Object|undefined} configGlobals The globals declared in configuration - * @param {Object|undefined} inlineGlobals The globals declared in the source code - * @returns {void} - */ -function addDeclaredGlobals( - globalScope, - configGlobals = {}, - inlineGlobals = {}, -) { - // Define configured global variables. - for (const id of new Set([ - ...Object.keys(configGlobals), - ...Object.keys(inlineGlobals), - ])) { - /* - * `normalizeConfigGlobal` will throw an error if a configured global value is invalid. However, these errors would - * typically be caught when validating a config anyway (validity for inline global comments is checked separately). - */ - const configValue = - configGlobals[id] === void 0 - ? void 0 - : normalizeConfigGlobal(configGlobals[id]); - const commentValue = inlineGlobals[id] && inlineGlobals[id].value; - const value = commentValue || configValue; - const sourceComments = inlineGlobals[id] && inlineGlobals[id].comments; - - if (value === "off") { - continue; - } - - let variable = globalScope.set.get(id); - - if (!variable) { - variable = new eslintScope.Variable(id, globalScope); - - globalScope.variables.push(variable); - globalScope.set.set(id, variable); - } - - variable.eslintImplicitGlobalSetting = configValue; - variable.eslintExplicitGlobal = sourceComments !== void 0; - variable.eslintExplicitGlobalComments = sourceComments; - variable.writeable = value === "writable"; - } - - /* - * "through" contains all references which definitions cannot be found. - * Since we augment the global scope using configuration, we need to update - * references and remove the ones that were added by configuration. - */ - globalScope.through = globalScope.through.filter(reference => { - const name = reference.identifier.name; - const variable = globalScope.set.get(name); - - if (variable) { - /* - * Links the variable and the reference. - * And this reference is removed from `Scope#through`. - */ - reference.resolved = variable; - variable.references.push(reference); - - return false; - } - - return true; - }); - - /* - * "implicit" contains information about implicit global variables (those created - * implicitly by assigning values to undeclared variables in non-strict code). - * Since we augment the global scope using configuration, we need to remove - * the ones that were added by configuration, as they are either built-in - * or declared elsewhere, therefore not implicit. - * Since the "implicit" property was not documented, first we'll check if it exists - * because it's possible that not all custom scope managers create this property. - * If it exists, we assume it has properties `variables` and `set`. Property - * `left` is considered optional (for example, typescript-eslint's scope manage - * has this property named `leftToBeResolved`). - */ - const { implicit } = globalScope; - if (typeof implicit === "object" && implicit !== null) { - implicit.variables = implicit.variables.filter(variable => { - const name = variable.name; - if (globalScope.set.has(name)) { - implicit.set.delete(name); - return false; - } - return true; - }); - - if (implicit.left) { - implicit.left = implicit.left.filter( - reference => !globalScope.set.has(reference.identifier.name), - ); - } - } -} - -/** - * Sets the given variable names as exported so they won't be triggered by - * the `no-unused-vars` rule. - * @param {eslint.Scope} globalScope The global scope to define exports in. - * @param {Record} variables An object whose keys are the variable - * names to export. - * @returns {void} - */ -function markExportedVariables(globalScope, variables) { - Object.keys(variables).forEach(name => { - const variable = globalScope.set.get(name); - - if (variable) { - variable.eslintUsed = true; - variable.eslintExported = true; - } - }); -} - -//------------------------------------------------------------------------------ -// Public Interface -//------------------------------------------------------------------------------ - -const caches = Symbol("caches"); - -/** - * Represents parsed source code. - * @implements {ISourceCode} - */ -class SourceCode extends TokenStore { - /** - * The cache of steps that were taken while traversing the source code. - * @type {Array} - */ - #steps; - - /** - * Creates a new instance. - * @param {string|Object} textOrConfig The source code text or config object. - * @param {string} textOrConfig.text The source code text. - * @param {ASTNode} textOrConfig.ast The Program node of the AST representing the code. This AST should be created from the text that BOM was stripped. - * @param {boolean} textOrConfig.hasBOM Indicates if the text has a Unicode BOM. - * @param {Object|null} textOrConfig.parserServices The parser services. - * @param {ScopeManager|null} textOrConfig.scopeManager The scope of this source code. - * @param {Object|null} textOrConfig.visitorKeys The visitor keys to traverse AST. - * @param {ASTNode} [astIfNoConfig] The Program node of the AST representing the code. This AST should be created from the text that BOM was stripped. - */ - constructor(textOrConfig, astIfNoConfig) { - let text, hasBOM, ast, parserServices, scopeManager, visitorKeys; - - // Process overloading of arguments - if (typeof textOrConfig === "string") { - text = textOrConfig; - ast = astIfNoConfig; - hasBOM = false; - } else if (typeof textOrConfig === "object" && textOrConfig !== null) { - text = textOrConfig.text; - ast = textOrConfig.ast; - hasBOM = textOrConfig.hasBOM; - parserServices = textOrConfig.parserServices; - scopeManager = textOrConfig.scopeManager; - visitorKeys = textOrConfig.visitorKeys; - } - - validate(ast); - super(ast.tokens, ast.comments); - - /** - * General purpose caching for the class. - */ - this[caches] = new Map([ - ["scopes", new WeakMap()], - ["vars", new Map()], - ["configNodes", void 0], - ["isGlobalReference", new WeakMap()], - ]); - - /** - * Indicates if the AST is ESTree compatible. - * @type {boolean} - */ - this.isESTree = ast.type === "Program"; - - /* - * Backwards compatibility for BOM handling. - * - * The `hasBOM` property has been available on the `SourceCode` object - * for a long time and is used to indicate if the source contains a BOM. - * The linter strips the BOM and just passes the `hasBOM` property to the - * `SourceCode` constructor to make it easier for languages to not deal with - * the BOM. - * - * However, the text passed in to the `SourceCode` constructor might still - * have a BOM if the constructor is called outside of the linter, so we still - * need to check for the BOM in the text. - */ - const textHasBOM = text.charCodeAt(0) === 0xfeff; - - /** - * The flag to indicate that the source code has Unicode BOM. - * @type {boolean} - */ - this.hasBOM = textHasBOM || !!hasBOM; - - /** - * The original text source code. - * BOM was stripped from this text. - * @type {string} - */ - this.text = textHasBOM ? text.slice(1) : text; - - /** - * The parsed AST for the source code. - * @type {ASTNode} - */ - this.ast = ast; - - /** - * The parser services of this source code. - * @type {Object} - */ - this.parserServices = parserServices || {}; - - /** - * The scope of this source code. - * @type {ScopeManager|null} - */ - this.scopeManager = scopeManager || null; - - /** - * The visitor keys to traverse AST. - * @type {Object} - */ - this.visitorKeys = visitorKeys || Traverser.DEFAULT_VISITOR_KEYS; - - // Check the source text for the presence of a shebang since it is parsed as a standard line comment. - const shebangMatched = this.text.match(astUtils.shebangPattern); - const hasShebang = - shebangMatched && - ast.comments.length && - ast.comments[0].value === shebangMatched[1]; - - if (hasShebang) { - ast.comments[0].type = "Shebang"; - } - - this.tokensAndComments = sortedMerge(ast.tokens, ast.comments); - - /** - * The source code split into lines according to ECMA-262 specification. - * This is done to avoid each rule needing to do so separately. - * @type {string[]} - */ - this.lines = []; - - /** - * @type {number[]} - */ - this.lineStartIndices = [0]; - - const lineEndingPattern = astUtils.createGlobalLinebreakMatcher(); - let match; - - /* - * Previously, this was implemented using a regex that - * matched a sequence of non-linebreak characters followed by a - * linebreak, then adding the lengths of the matches. However, - * this caused a catastrophic backtracking issue when the end - * of a file contained a large number of non-newline characters. - * To avoid this, the current implementation just matches newlines - * and uses match.index to get the correct line start indices. - */ - while ((match = lineEndingPattern.exec(this.text))) { - this.lines.push( - this.text.slice(this.lineStartIndices.at(-1), match.index), - ); - this.lineStartIndices.push(match.index + match[0].length); - } - this.lines.push(this.text.slice(this.lineStartIndices.at(-1))); - - // don't allow further modification of this object - Object.freeze(this); - Object.freeze(this.lines); - } - - /** - * Split the source code into multiple lines based on the line delimiters. - * @param {string} text Source code as a string. - * @returns {string[]} Array of source code lines. - * @public - */ - static splitLines(text) { - return text.split(astUtils.createGlobalLinebreakMatcher()); - } - - /** - * Gets the source code for the given node. - * @param {ASTNode} [node] The AST node to get the text for. - * @param {number} [beforeCount] The number of characters before the node to retrieve. - * @param {number} [afterCount] The number of characters after the node to retrieve. - * @returns {string} The text representing the AST node. - * @public - */ - getText(node, beforeCount, afterCount) { - if (node) { - return this.text.slice( - Math.max(node.range[0] - (beforeCount || 0), 0), - node.range[1] + (afterCount || 0), - ); - } - return this.text; - } - - /** - * Gets the entire source text split into an array of lines. - * @returns {string[]} The source text as an array of lines. - * @public - */ - getLines() { - return this.lines; - } - - /** - * Retrieves an array containing all comments in the source code. - * @returns {ASTNode[]} An array of comment nodes. - * @public - */ - getAllComments() { - return this.ast.comments; - } - - /** - * Retrieves the JSDoc comment for a given node. - * @param {ASTNode} node The AST node to get the comment for. - * @returns {Token|null} The Block comment token containing the JSDoc comment - * for the given node or null if not found. - * @public - * @deprecated - */ - getJSDocComment(node) { - /** - * Checks for the presence of a JSDoc comment for the given node and returns it. - * @param {ASTNode} astNode The AST node to get the comment for. - * @returns {Token|null} The Block comment token containing the JSDoc comment - * for the given node or null if not found. - * @private - */ - const findJSDocComment = astNode => { - const tokenBefore = this.getTokenBefore(astNode, { - includeComments: true, - }); - - if ( - tokenBefore && - isCommentToken(tokenBefore) && - tokenBefore.type === "Block" && - tokenBefore.value.charAt(0) === "*" && - astNode.loc.start.line - tokenBefore.loc.end.line <= 1 - ) { - return tokenBefore; - } - - return null; - }; - let parent = node.parent; - - switch (node.type) { - case "ClassDeclaration": - case "FunctionDeclaration": - return findJSDocComment( - looksLikeExport(parent) ? parent : node, - ); - - case "ClassExpression": - return findJSDocComment(parent.parent); - - case "ArrowFunctionExpression": - case "FunctionExpression": - if ( - parent.type !== "CallExpression" && - parent.type !== "NewExpression" - ) { - while ( - !this.getCommentsBefore(parent).length && - !/Function/u.test(parent.type) && - parent.type !== "MethodDefinition" && - parent.type !== "Property" - ) { - parent = parent.parent; - - if (!parent) { - break; - } - } - - if ( - parent && - parent.type !== "FunctionDeclaration" && - parent.type !== "Program" - ) { - return findJSDocComment(parent); - } - } - - return findJSDocComment(node); - - // falls through - default: - return null; - } - } - - /** - * Gets the deepest node containing a range index. - * @param {number} index Range index of the desired node. - * @returns {ASTNode} The node if found or null if not found. - * @public - */ - getNodeByRangeIndex(index) { - let result = null; - - Traverser.traverse(this.ast, { - visitorKeys: this.visitorKeys, - enter(node) { - if (node.range[0] <= index && index < node.range[1]) { - result = node; - } else { - this.skip(); - } - }, - leave(node) { - if (node === result) { - this.break(); - } - }, - }); - - return result; - } - - /** - * Determines if two nodes or tokens have at least one whitespace character - * between them. Order does not matter. Returns false if the given nodes or - * tokens overlap. - * @param {ASTNode|Token} first The first node or token to check between. - * @param {ASTNode|Token} second The second node or token to check between. - * @returns {boolean} True if there is a whitespace character between - * any of the tokens found between the two given nodes or tokens. - * @public - */ - isSpaceBetween(first, second) { - return isSpaceBetween(this, first, second, false); - } - - /** - * Determines if two nodes or tokens have at least one whitespace character - * between them. Order does not matter. Returns false if the given nodes or - * tokens overlap. - * For backward compatibility, this method returns true if there are - * `JSXText` tokens that contain whitespaces between the two. - * @param {ASTNode|Token} first The first node or token to check between. - * @param {ASTNode|Token} second The second node or token to check between. - * @returns {boolean} True if there is a whitespace character between - * any of the tokens found between the two given nodes or tokens. - * @deprecated in favor of isSpaceBetween(). - * @public - */ - isSpaceBetweenTokens(first, second) { - return isSpaceBetween(this, first, second, true); - } - - /** - * Converts a source text index into a (line, column) pair. - * @param {number} index The index of a character in a file. - * @throws {TypeError|RangeError} If non-numeric index or index out of range. - * @returns {{line: number, column: number}} A {line, column} location object with 1-indexed line and 0-indexed column. - * @public - */ - getLocFromIndex(index) { - if (typeof index !== "number") { - throw new TypeError("Expected `index` to be a number."); - } - - if (index < 0 || index > this.text.length) { - throw new RangeError( - `Index out of range (requested index ${index}, but source text has length ${this.text.length}).`, - ); - } - - /* - * For an argument of this.text.length, return the location one "spot" past the last character - * of the file. If the last character is a linebreak, the location will be column 0 of the next - * line; otherwise, the location will be in the next column on the same line. - * - * See getIndexFromLoc for the motivation for this special case. - */ - if (index === this.text.length) { - return { - line: this.lines.length, - column: this.lines.at(-1).length, - }; - } - - /* - * To figure out which line index is on, determine the last place at which index could - * be inserted into lineStartIndices to keep the list sorted. - */ - const lineNumber = - index >= this.lineStartIndices.at(-1) - ? this.lineStartIndices.length - : findLineNumberBinarySearch(this.lineStartIndices, index); - - return { - line: lineNumber, - column: index - this.lineStartIndices[lineNumber - 1], - }; - } - - /** - * Converts a (line, column) pair into a range index. - * @param {Object} loc A line/column location - * @param {number} loc.line The line number of the location (1-indexed) - * @param {number} loc.column The column number of the location (0-indexed) - * @throws {TypeError|RangeError} If `loc` is not an object with a numeric - * `line` and `column`, if the `line` is less than or equal to zero or - * the line or column is out of the expected range. - * @returns {number} The range index of the location in the file. - * @public - */ - getIndexFromLoc(loc) { - if ( - loc === null || - typeof loc !== "object" || - typeof loc.line !== "number" || - typeof loc.column !== "number" - ) { - throw new TypeError( - "Expected `loc` to be an object with numeric `line` and `column` properties.", - ); - } - - if (loc.line <= 0) { - throw new RangeError( - `Line number out of range (line ${loc.line} requested). Line numbers should be 1-based.`, - ); - } - - if (loc.line > this.lineStartIndices.length) { - throw new RangeError( - `Line number out of range (line ${loc.line} requested, but only ${this.lineStartIndices.length} lines present).`, - ); - } - - if (loc.column < 0) { - throw new RangeError( - `Invalid column number (column ${loc.column} requested).`, - ); - } - - const lineStartIndex = this.lineStartIndices[loc.line - 1]; - const lineEndIndex = - loc.line === this.lineStartIndices.length - ? this.text.length - : this.lineStartIndices[loc.line]; - const positionIndex = lineStartIndex + loc.column; - - /* - * By design, getIndexFromLoc({ line: lineNum, column: 0 }) should return the start index of - * the given line, provided that the line number is valid element of this.lines. Since the - * last element of this.lines is an empty string for files with trailing newlines, add a - * special case where getting the index for the first location after the end of the file - * will return the length of the file, rather than throwing an error. This allows rules to - * use getIndexFromLoc consistently without worrying about edge cases at the end of a file. - */ - if ( - (loc.line === this.lineStartIndices.length && - positionIndex > lineEndIndex) || - (loc.line < this.lineStartIndices.length && - positionIndex >= lineEndIndex) - ) { - throw new RangeError( - `Column number out of range (column ${loc.column} requested, but the length of line ${loc.line} is ${lineEndIndex - lineStartIndex}).`, - ); - } - - return positionIndex; - } - - /** - * Gets the scope for the given node - * @param {ASTNode} currentNode The node to get the scope of - * @returns {Scope} The scope information for this node - * @throws {TypeError} If the `currentNode` argument is missing. - */ - getScope(currentNode) { - if (!currentNode) { - throw new TypeError("Missing required argument: node."); - } - - // check cache first - const cache = this[caches].get("scopes"); - const cachedScope = cache.get(currentNode); - - if (cachedScope) { - return cachedScope; - } - - // On Program node, get the outermost scope to avoid return Node.js special function scope or ES modules scope. - const inner = currentNode.type !== "Program"; - - for (let node = currentNode; node; node = node.parent) { - const scope = this.scopeManager.acquire(node, inner); - - if (scope) { - if (scope.type === "function-expression-name") { - cache.set(currentNode, scope.childScopes[0]); - return scope.childScopes[0]; - } - - cache.set(currentNode, scope); - return scope; - } - } - - cache.set(currentNode, this.scopeManager.scopes[0]); - return this.scopeManager.scopes[0]; - } - - /** - * Get the variables that `node` defines. - * This is a convenience method that passes through - * to the same method on the `scopeManager`. - * @param {ASTNode} node The node for which the variables are obtained. - * @returns {Array} An array of variable nodes representing - * the variables that `node` defines. - */ - getDeclaredVariables(node) { - return this.scopeManager.getDeclaredVariables(node); - } - - /* eslint-disable class-methods-use-this -- node is owned by SourceCode */ - /** - * Gets all the ancestors of a given node - * @param {ASTNode} node The node - * @returns {Array} All the ancestor nodes in the AST, not including the provided node, starting - * from the root node at index 0 and going inwards to the parent node. - * @throws {TypeError} When `node` is missing. - */ - getAncestors(node) { - if (!node) { - throw new TypeError("Missing required argument: node."); - } - - const ancestorsStartingAtParent = []; - - for (let ancestor = node.parent; ancestor; ancestor = ancestor.parent) { - ancestorsStartingAtParent.push(ancestor); - } - - return ancestorsStartingAtParent.reverse(); - } - - /** - * Determines whether the given identifier node is a reference to a global variable. - * @param {ASTNode} node `Identifier` node to check. - * @returns {boolean} True if the identifier is a reference to a global variable. - */ - isGlobalReference(node) { - if (!node) { - throw new TypeError("Missing required argument: node."); - } - - const cache = this[caches].get("isGlobalReference"); - - if (cache.has(node)) { - return cache.get(node); - } - - if (node.type !== "Identifier") { - cache.set(node, false); - return false; - } - - const variable = this.scopeManager.scopes[0].set.get(node.name); - - if (!variable || variable.defs.length > 0) { - cache.set(node, false); - return false; - } - - const result = variable.references.some( - ({ identifier }) => identifier === node, - ); - cache.set(node, result); - return result; - } - - /** - * Returns the location of the given node or token. - * @param {ASTNode|Token} nodeOrToken The node or token to get the location of. - * @returns {SourceLocation} The location of the node or token. - */ - getLoc(nodeOrToken) { - return nodeOrToken.loc; - } - - /** - * Returns the range of the given node or token. - * @param {ASTNode|Token} nodeOrToken The node or token to get the range of. - * @returns {[number, number]} The range of the node or token. - */ - getRange(nodeOrToken) { - return nodeOrToken.range; - } - - /* eslint-enable class-methods-use-this -- node is owned by SourceCode */ - - /** - * Marks a variable as used in the current scope - * @param {string} name The name of the variable to mark as used. - * @param {ASTNode} [refNode] The closest node to the variable reference. - * @returns {boolean} True if the variable was found and marked as used, false if not. - */ - markVariableAsUsed(name, refNode = this.ast) { - const currentScope = this.getScope(refNode); - let initialScope = currentScope; - - /* - * When we are in an ESM or CommonJS module, we need to start searching - * from the top-level scope, not the global scope. For ESM the top-level - * scope is the module scope; for CommonJS the top-level scope is the - * outer function scope. - * - * Without this check, we might miss a variable declared with `var` at - * the top-level because it won't exist in the global scope. - */ - if ( - currentScope.type === "global" && - currentScope.childScopes.length > 0 && - // top-level scopes refer to a `Program` node - currentScope.childScopes[0].block === this.ast - ) { - initialScope = currentScope.childScopes[0]; - } - - for (let scope = initialScope; scope; scope = scope.upper) { - const variable = scope.variables.find( - scopeVar => scopeVar.name === name, - ); - - if (variable) { - variable.eslintUsed = true; - return true; - } - } - - return false; - } - - /** - * Returns an array of all inline configuration nodes found in the - * source code. - * @returns {Array} An array of all inline configuration nodes. - */ - getInlineConfigNodes() { - // check the cache first - let configNodes = this[caches].get("configNodes"); - - if (configNodes) { - return configNodes; - } - - // calculate fresh config nodes - configNodes = this.ast.comments.filter(comment => { - // shebang comments are never directives - if (comment.type === "Shebang") { - return false; - } - - const directive = commentParser.parseDirective(comment.value); - - if (!directive) { - return false; - } - - if (!directivesPattern.test(directive.label)) { - return false; - } - - // only certain comment types are supported as line comments - return ( - comment.type !== "Line" || - !!/^eslint-disable-(?:next-)?line$/u.test(directive.label) - ); - }); - - this[caches].set("configNodes", configNodes); - - return configNodes; - } - - /** - * Returns an all directive nodes that enable or disable rules along with any problems - * encountered while parsing the directives. - * @returns {{problems:Array,directives:Array}} Information - * that ESLint needs to further process the directives. - */ - getDisableDirectives() { - // check the cache first - const cachedDirectives = this[caches].get("disableDirectives"); - - if (cachedDirectives) { - return cachedDirectives; - } - - const problems = []; - const directives = []; - - this.getInlineConfigNodes().forEach(comment => { - // Step 1: Parse the directive - const { - label, - value, - justification: justificationPart, - } = commentParser.parseDirective(comment.value); - - // Step 2: Extract the directive value - const lineCommentSupported = - /^eslint-disable-(?:next-)?line$/u.test(label); - - if (comment.type === "Line" && !lineCommentSupported) { - return; - } - - // Step 3: Validate the directive does not span multiple lines - if ( - label === "eslint-disable-line" && - comment.loc.start.line !== comment.loc.end.line - ) { - const message = `${label} comment should not span multiple lines.`; - - problems.push({ - ruleId: null, - message, - loc: comment.loc, - }); - return; - } - - // Step 4: Extract the directive value and create the Directive object - switch (label) { - case "eslint-disable": - case "eslint-enable": - case "eslint-disable-next-line": - case "eslint-disable-line": { - const directiveType = label.slice("eslint-".length); - - directives.push( - new Directive({ - type: directiveType, - node: comment, - value, - justification: justificationPart, - }), - ); - } - - // no default - } - }); - - const result = { problems, directives }; - - this[caches].set("disableDirectives", result); - - return result; - } - - /** - * Applies language options sent in from the core. - * @param {Object} languageOptions The language options for this run. - * @returns {void} - */ - applyLanguageOptions(languageOptions) { - /* - * Add configured globals and language globals - * - * Using Object.assign instead of object spread for performance reasons - * https://github.com/eslint/eslint/issues/16302 - */ - const configGlobals = Object.assign( - Object.create(null), // https://github.com/eslint/eslint/issues/18363 - getGlobalsForEcmaVersion(languageOptions.ecmaVersion), - languageOptions.sourceType === "commonjs" - ? globals.commonjs - : void 0, - languageOptions.globals, - ); - const varsCache = this[caches].get("vars"); - - varsCache.set("configGlobals", configGlobals); - } - - /** - * Applies configuration found inside of the source code. This method is only - * called when ESLint is running with inline configuration allowed. - * @returns {{problems:Array,configs:{config:FlatConfigArray,loc:Location}}} Information - * that ESLint needs to further process the inline configuration. - */ - applyInlineConfig() { - const problems = []; - const configs = []; - const exportedVariables = {}; - const inlineGlobals = Object.create(null); - - this.getInlineConfigNodes().forEach(comment => { - const { label, value } = commentParser.parseDirective( - comment.value, - ); - - switch (label) { - case "exported": - Object.assign( - exportedVariables, - commentParser.parseListConfig(value), - ); - break; - - case "globals": - case "global": - for (const [id, idSetting] of Object.entries( - commentParser.parseStringConfig(value), - )) { - let normalizedValue; - - try { - normalizedValue = normalizeConfigGlobal(idSetting); - } catch (err) { - problems.push({ - ruleId: null, - loc: comment.loc, - message: err.message, - }); - continue; - } - - if (inlineGlobals[id]) { - inlineGlobals[id].comments.push(comment); - inlineGlobals[id].value = normalizedValue; - } else { - inlineGlobals[id] = { - comments: [comment], - value: normalizedValue, - }; - } - } - break; - - case "eslint": { - const parseResult = - commentParser.parseJSONLikeConfig(value); - - if (parseResult.ok) { - configs.push({ - config: { - rules: parseResult.config, - }, - loc: comment.loc, - }); - } else { - problems.push({ - ruleId: null, - loc: comment.loc, - message: parseResult.error.message, - }); - } - - break; - } - - // no default - } - }); - - // save all the new variables for later - const varsCache = this[caches].get("vars"); - - varsCache.set("inlineGlobals", inlineGlobals); - varsCache.set("exportedVariables", exportedVariables); - - return { - configs, - problems, - }; - } - - /** - * Called by ESLint core to indicate that it has finished providing - * information. We now add in all the missing variables and ensure that - * state-changing methods cannot be called by rules. - * @returns {void} - */ - finalize() { - const varsCache = this[caches].get("vars"); - const configGlobals = varsCache.get("configGlobals"); - const inlineGlobals = varsCache.get("inlineGlobals"); - const exportedVariables = varsCache.get("exportedVariables"); - const globalScope = this.scopeManager.scopes[0]; - - addDeclaredGlobals(globalScope, configGlobals, inlineGlobals); - - if (exportedVariables) { - markExportedVariables(globalScope, exportedVariables); - } - } - - /** - * Traverse the source code and return the steps that were taken. - * @returns {Array} The steps that were taken while traversing the source code. - */ - traverse() { - // Because the AST doesn't mutate, we can cache the steps - if (this.#steps) { - return this.#steps; - } - - const steps = (this.#steps = []); - - /* - * This logic works for any AST, not just ESTree. Because ESLint has allowed - * custom parsers to return any AST, we need to ensure that the traversal - * logic works for any AST. - */ - let analyzer = { - enterNode(node) { - steps.push( - new VisitNodeStep({ - target: node, - phase: 1, - args: [node], - }), - ); - }, - leaveNode(node) { - steps.push( - new VisitNodeStep({ - target: node, - phase: 2, - args: [node], - }), - ); - }, - emit(eventName, args) { - steps.push( - new CallMethodStep({ - target: eventName, - args, - }), - ); - }, - }; - - /* - * We do code path analysis for ESTree only. Code path analysis is not - * necessary for other ASTs, and it's also not possible to do for other - * ASTs because the necessary information is not available. - * - * Generally speaking, we can tell that the AST is an ESTree if it has a - * Program node at the top level. This is not a perfect heuristic, but it - * is good enough for now. - */ - if (this.isESTree) { - analyzer = new CodePathAnalyzer(analyzer); - } - - /* - * The actual AST traversal is done by the `Traverser` class. This class - * is responsible for walking the AST and calling the appropriate methods - * on the `analyzer` object, which is appropriate for the given AST. - */ - Traverser.traverse(this.ast, { - enter(node, parent) { - // save the parent node on a property for backwards compatibility - node.parent = parent; - - analyzer.enterNode(node); - }, - leave(node) { - analyzer.leaveNode(node); - }, - visitorKeys: this.visitorKeys, - }); - - return steps; - } -} - -module.exports = SourceCode; diff --git a/node_modules/eslint/lib/languages/js/source-code/token-store/backward-token-comment-cursor.js b/node_modules/eslint/lib/languages/js/source-code/token-store/backward-token-comment-cursor.js deleted file mode 100644 index 56f920a47..000000000 --- a/node_modules/eslint/lib/languages/js/source-code/token-store/backward-token-comment-cursor.js +++ /dev/null @@ -1,61 +0,0 @@ -/** - * @fileoverview Define the cursor which iterates tokens and comments in reverse. - * @author Toru Nagashima - */ -"use strict"; - -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ - -const Cursor = require("./cursor"); -const utils = require("./utils"); - -//------------------------------------------------------------------------------ -// Exports -//------------------------------------------------------------------------------ - -/** - * The cursor which iterates tokens and comments in reverse. - */ -module.exports = class BackwardTokenCommentCursor extends Cursor { - /** - * Initializes this cursor. - * @param {Token[]} tokens The array of tokens. - * @param {Comment[]} comments The array of comments. - * @param {Object} indexMap The map from locations to indices in `tokens`. - * @param {number} startLoc The start location of the iteration range. - * @param {number} endLoc The end location of the iteration range. - */ - constructor(tokens, comments, indexMap, startLoc, endLoc) { - super(); - this.tokens = tokens; - this.comments = comments; - this.tokenIndex = utils.getLastIndex(tokens, indexMap, endLoc); - this.commentIndex = utils.search(comments, endLoc) - 1; - this.border = startLoc; - } - - /** @inheritdoc */ - moveNext() { - const token = - this.tokenIndex >= 0 ? this.tokens[this.tokenIndex] : null; - const comment = - this.commentIndex >= 0 ? this.comments[this.commentIndex] : null; - - if (token && (!comment || token.range[1] > comment.range[1])) { - this.current = token; - this.tokenIndex -= 1; - } else if (comment) { - this.current = comment; - this.commentIndex -= 1; - } else { - this.current = null; - } - - return ( - Boolean(this.current) && - (this.border === -1 || this.current.range[0] >= this.border) - ); - } -}; diff --git a/node_modules/eslint/lib/languages/js/source-code/token-store/backward-token-cursor.js b/node_modules/eslint/lib/languages/js/source-code/token-store/backward-token-cursor.js deleted file mode 100644 index b6b2e120d..000000000 --- a/node_modules/eslint/lib/languages/js/source-code/token-store/backward-token-cursor.js +++ /dev/null @@ -1,57 +0,0 @@ -/** - * @fileoverview Define the cursor which iterates tokens only in reverse. - * @author Toru Nagashima - */ -"use strict"; - -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ - -const Cursor = require("./cursor"); -const { getLastIndex, getFirstIndex } = require("./utils"); - -//------------------------------------------------------------------------------ -// Exports -//------------------------------------------------------------------------------ - -/** - * The cursor which iterates tokens only in reverse. - */ -module.exports = class BackwardTokenCursor extends Cursor { - /** - * Initializes this cursor. - * @param {Token[]} tokens The array of tokens. - * @param {Comment[]} comments The array of comments. - * @param {Object} indexMap The map from locations to indices in `tokens`. - * @param {number} startLoc The start location of the iteration range. - * @param {number} endLoc The end location of the iteration range. - */ - constructor(tokens, comments, indexMap, startLoc, endLoc) { - super(); - this.tokens = tokens; - this.index = getLastIndex(tokens, indexMap, endLoc); - this.indexEnd = getFirstIndex(tokens, indexMap, startLoc); - } - - /** @inheritdoc */ - moveNext() { - if (this.index >= this.indexEnd) { - this.current = this.tokens[this.index]; - this.index -= 1; - return true; - } - return false; - } - - /* - * - * Shorthand for performance. - * - */ - - /** @inheritdoc */ - getOneToken() { - return this.index >= this.indexEnd ? this.tokens[this.index] : null; - } -}; diff --git a/node_modules/eslint/lib/languages/js/source-code/token-store/cursor.js b/node_modules/eslint/lib/languages/js/source-code/token-store/cursor.js deleted file mode 100644 index e640d23fb..000000000 --- a/node_modules/eslint/lib/languages/js/source-code/token-store/cursor.js +++ /dev/null @@ -1,76 +0,0 @@ -/** - * @fileoverview Define the abstract class about cursors which iterate tokens. - * @author Toru Nagashima - */ -"use strict"; - -//------------------------------------------------------------------------------ -// Exports -//------------------------------------------------------------------------------ - -/** - * The abstract class about cursors which iterate tokens. - * - * This class has 2 abstract methods. - * - * - `current: Token | Comment | null` ... The current token. - * - `moveNext(): boolean` ... Moves this cursor to the next token. If the next token didn't exist, it returns `false`. - * - * This is similar to ES2015 Iterators. - * However, Iterators were slow (at 2017-01), so I created this class as similar to C# IEnumerable. - * - * There are the following known sub classes. - * - * - ForwardTokenCursor .......... The cursor which iterates tokens only. - * - BackwardTokenCursor ......... The cursor which iterates tokens only in reverse. - * - ForwardTokenCommentCursor ... The cursor which iterates tokens and comments. - * - BackwardTokenCommentCursor .. The cursor which iterates tokens and comments in reverse. - * - DecorativeCursor - * - FilterCursor ............ The cursor which ignores the specified tokens. - * - SkipCursor .............. The cursor which ignores the first few tokens. - * - LimitCursor ............. The cursor which limits the count of tokens. - * - */ -module.exports = class Cursor { - /** - * Initializes this cursor. - */ - constructor() { - this.current = null; - } - - /** - * Gets the first token. - * This consumes this cursor. - * @returns {Token|Comment} The first token or null. - */ - getOneToken() { - return this.moveNext() ? this.current : null; - } - - /** - * Gets the first tokens. - * This consumes this cursor. - * @returns {(Token|Comment)[]} All tokens. - */ - getAllTokens() { - const tokens = []; - - while (this.moveNext()) { - tokens.push(this.current); - } - - return tokens; - } - - /** - * Moves this cursor to the next token. - * @returns {boolean} `true` if the next token exists. - * @abstract - */ - /* c8 ignore next */ - // eslint-disable-next-line class-methods-use-this -- Unused - moveNext() { - throw new Error("Not implemented."); - } -}; diff --git a/node_modules/eslint/lib/languages/js/source-code/token-store/cursors.js b/node_modules/eslint/lib/languages/js/source-code/token-store/cursors.js deleted file mode 100644 index 1e9c084cf..000000000 --- a/node_modules/eslint/lib/languages/js/source-code/token-store/cursors.js +++ /dev/null @@ -1,120 +0,0 @@ -/** - * @fileoverview Define 2 token factories; forward and backward. - * @author Toru Nagashima - */ -"use strict"; - -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ - -const BackwardTokenCommentCursor = require("./backward-token-comment-cursor"); -const BackwardTokenCursor = require("./backward-token-cursor"); -const FilterCursor = require("./filter-cursor"); -const ForwardTokenCommentCursor = require("./forward-token-comment-cursor"); -const ForwardTokenCursor = require("./forward-token-cursor"); -const LimitCursor = require("./limit-cursor"); -const SkipCursor = require("./skip-cursor"); - -//------------------------------------------------------------------------------ -// Helpers -//------------------------------------------------------------------------------ - -/** - * The cursor factory. - * @private - */ -class CursorFactory { - /** - * Initializes this cursor. - * @param {Function} TokenCursor The class of the cursor which iterates tokens only. - * @param {Function} TokenCommentCursor The class of the cursor which iterates the mix of tokens and comments. - */ - constructor(TokenCursor, TokenCommentCursor) { - this.TokenCursor = TokenCursor; - this.TokenCommentCursor = TokenCommentCursor; - } - - /** - * Creates a base cursor instance that can be decorated by createCursor. - * @param {Token[]} tokens The array of tokens. - * @param {Comment[]} comments The array of comments. - * @param {Object} indexMap The map from locations to indices in `tokens`. - * @param {number} startLoc The start location of the iteration range. - * @param {number} endLoc The end location of the iteration range. - * @param {boolean} includeComments The flag to iterate comments as well. - * @returns {Cursor} The created base cursor. - */ - createBaseCursor( - tokens, - comments, - indexMap, - startLoc, - endLoc, - includeComments, - ) { - const Cursor = includeComments - ? this.TokenCommentCursor - : this.TokenCursor; - - return new Cursor(tokens, comments, indexMap, startLoc, endLoc); - } - - /** - * Creates a cursor that iterates tokens with normalized options. - * @param {Token[]} tokens The array of tokens. - * @param {Comment[]} comments The array of comments. - * @param {Object} indexMap The map from locations to indices in `tokens`. - * @param {number} startLoc The start location of the iteration range. - * @param {number} endLoc The end location of the iteration range. - * @param {boolean} includeComments The flag to iterate comments as well. - * @param {Function|null} filter The predicate function to choose tokens. - * @param {number} skip The count of tokens the cursor skips. - * @param {number} count The maximum count of tokens the cursor iterates. Zero is no iteration for backward compatibility. - * @returns {Cursor} The created cursor. - */ - createCursor( - tokens, - comments, - indexMap, - startLoc, - endLoc, - includeComments, - filter, - skip, - count, - ) { - let cursor = this.createBaseCursor( - tokens, - comments, - indexMap, - startLoc, - endLoc, - includeComments, - ); - - if (filter) { - cursor = new FilterCursor(cursor, filter); - } - if (skip >= 1) { - cursor = new SkipCursor(cursor, skip); - } - if (count >= 0) { - cursor = new LimitCursor(cursor, count); - } - - return cursor; - } -} - -//------------------------------------------------------------------------------ -// Exports -//------------------------------------------------------------------------------ - -module.exports = { - forward: new CursorFactory(ForwardTokenCursor, ForwardTokenCommentCursor), - backward: new CursorFactory( - BackwardTokenCursor, - BackwardTokenCommentCursor, - ), -}; diff --git a/node_modules/eslint/lib/languages/js/source-code/token-store/decorative-cursor.js b/node_modules/eslint/lib/languages/js/source-code/token-store/decorative-cursor.js deleted file mode 100644 index 3a1d21ecc..000000000 --- a/node_modules/eslint/lib/languages/js/source-code/token-store/decorative-cursor.js +++ /dev/null @@ -1,38 +0,0 @@ -/** - * @fileoverview Define the abstract class about cursors which manipulate another cursor. - * @author Toru Nagashima - */ -"use strict"; - -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ - -const Cursor = require("./cursor"); - -//------------------------------------------------------------------------------ -// Exports -//------------------------------------------------------------------------------ - -/** - * The abstract class about cursors which manipulate another cursor. - */ -module.exports = class DecorativeCursor extends Cursor { - /** - * Initializes this cursor. - * @param {Cursor} cursor The cursor to be decorated. - */ - constructor(cursor) { - super(); - this.cursor = cursor; - } - - /** @inheritdoc */ - moveNext() { - const retv = this.cursor.moveNext(); - - this.current = this.cursor.current; - - return retv; - } -}; diff --git a/node_modules/eslint/lib/languages/js/source-code/token-store/filter-cursor.js b/node_modules/eslint/lib/languages/js/source-code/token-store/filter-cursor.js deleted file mode 100644 index 1e2ec9969..000000000 --- a/node_modules/eslint/lib/languages/js/source-code/token-store/filter-cursor.js +++ /dev/null @@ -1,42 +0,0 @@ -/** - * @fileoverview Define the cursor which ignores specified tokens. - * @author Toru Nagashima - */ -"use strict"; - -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ - -const DecorativeCursor = require("./decorative-cursor"); - -//------------------------------------------------------------------------------ -// Exports -//------------------------------------------------------------------------------ - -/** - * The decorative cursor which ignores specified tokens. - */ -module.exports = class FilterCursor extends DecorativeCursor { - /** - * Initializes this cursor. - * @param {Cursor} cursor The cursor to be decorated. - * @param {Function} predicate The predicate function to decide tokens this cursor iterates. - */ - constructor(cursor, predicate) { - super(cursor); - this.predicate = predicate; - } - - /** @inheritdoc */ - moveNext() { - const predicate = this.predicate; - - while (super.moveNext()) { - if (predicate(this.current)) { - return true; - } - } - return false; - } -}; diff --git a/node_modules/eslint/lib/languages/js/source-code/token-store/forward-token-comment-cursor.js b/node_modules/eslint/lib/languages/js/source-code/token-store/forward-token-comment-cursor.js deleted file mode 100644 index d4a1439fe..000000000 --- a/node_modules/eslint/lib/languages/js/source-code/token-store/forward-token-comment-cursor.js +++ /dev/null @@ -1,65 +0,0 @@ -/** - * @fileoverview Define the cursor which iterates tokens and comments. - * @author Toru Nagashima - */ -"use strict"; - -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ - -const Cursor = require("./cursor"); -const { getFirstIndex, search } = require("./utils"); - -//------------------------------------------------------------------------------ -// Exports -//------------------------------------------------------------------------------ - -/** - * The cursor which iterates tokens and comments. - */ -module.exports = class ForwardTokenCommentCursor extends Cursor { - /** - * Initializes this cursor. - * @param {Token[]} tokens The array of tokens. - * @param {Comment[]} comments The array of comments. - * @param {Object} indexMap The map from locations to indices in `tokens`. - * @param {number} startLoc The start location of the iteration range. - * @param {number} endLoc The end location of the iteration range. - */ - constructor(tokens, comments, indexMap, startLoc, endLoc) { - super(); - this.tokens = tokens; - this.comments = comments; - this.tokenIndex = getFirstIndex(tokens, indexMap, startLoc); - this.commentIndex = search(comments, startLoc); - this.border = endLoc; - } - - /** @inheritdoc */ - moveNext() { - const token = - this.tokenIndex < this.tokens.length - ? this.tokens[this.tokenIndex] - : null; - const comment = - this.commentIndex < this.comments.length - ? this.comments[this.commentIndex] - : null; - - if (token && (!comment || token.range[0] < comment.range[0])) { - this.current = token; - this.tokenIndex += 1; - } else if (comment) { - this.current = comment; - this.commentIndex += 1; - } else { - this.current = null; - } - - return ( - Boolean(this.current) && - (this.border === -1 || this.current.range[1] <= this.border) - ); - } -}; diff --git a/node_modules/eslint/lib/languages/js/source-code/token-store/forward-token-cursor.js b/node_modules/eslint/lib/languages/js/source-code/token-store/forward-token-cursor.js deleted file mode 100644 index 5ca2290f1..000000000 --- a/node_modules/eslint/lib/languages/js/source-code/token-store/forward-token-cursor.js +++ /dev/null @@ -1,62 +0,0 @@ -/** - * @fileoverview Define the cursor which iterates tokens only. - * @author Toru Nagashima - */ -"use strict"; - -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ - -const Cursor = require("./cursor"); -const { getFirstIndex, getLastIndex } = require("./utils"); - -//------------------------------------------------------------------------------ -// Exports -//------------------------------------------------------------------------------ - -/** - * The cursor which iterates tokens only. - */ -module.exports = class ForwardTokenCursor extends Cursor { - /** - * Initializes this cursor. - * @param {Token[]} tokens The array of tokens. - * @param {Comment[]} comments The array of comments. - * @param {Object} indexMap The map from locations to indices in `tokens`. - * @param {number} startLoc The start location of the iteration range. - * @param {number} endLoc The end location of the iteration range. - */ - constructor(tokens, comments, indexMap, startLoc, endLoc) { - super(); - this.tokens = tokens; - this.index = getFirstIndex(tokens, indexMap, startLoc); - this.indexEnd = getLastIndex(tokens, indexMap, endLoc); - } - - /** @inheritdoc */ - moveNext() { - if (this.index <= this.indexEnd) { - this.current = this.tokens[this.index]; - this.index += 1; - return true; - } - return false; - } - - /* - * - * Shorthand for performance. - * - */ - - /** @inheritdoc */ - getOneToken() { - return this.index <= this.indexEnd ? this.tokens[this.index] : null; - } - - /** @inheritdoc */ - getAllTokens() { - return this.tokens.slice(this.index, this.indexEnd + 1); - } -}; diff --git a/node_modules/eslint/lib/languages/js/source-code/token-store/index.js b/node_modules/eslint/lib/languages/js/source-code/token-store/index.js deleted file mode 100644 index 120438a9d..000000000 --- a/node_modules/eslint/lib/languages/js/source-code/token-store/index.js +++ /dev/null @@ -1,721 +0,0 @@ -/** - * @fileoverview Object to handle access and retrieval of tokens. - * @author Brandon Mills - */ -"use strict"; - -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ - -const { isCommentToken } = require("@eslint-community/eslint-utils"); -const assert = require("../../../../shared/assert"); -const cursors = require("./cursors"); -const ForwardTokenCursor = require("./forward-token-cursor"); -const PaddedTokenCursor = require("./padded-token-cursor"); -const utils = require("./utils"); - -//------------------------------------------------------------------------------ -// Helpers -//------------------------------------------------------------------------------ - -const TOKENS = Symbol("tokens"); -const COMMENTS = Symbol("comments"); -const INDEX_MAP = Symbol("indexMap"); - -/** - * Creates the map from locations to indices in `tokens`. - * - * The first/last location of tokens is mapped to the index of the token. - * The first/last location of comments is mapped to the index of the next token of each comment. - * @param {Token[]} tokens The array of tokens. - * @param {Comment[]} comments The array of comments. - * @returns {Object} The map from locations to indices in `tokens`. - * @private - */ -function createIndexMap(tokens, comments) { - const map = Object.create(null); - let tokenIndex = 0; - let commentIndex = 0; - let nextStart; - let range; - - while (tokenIndex < tokens.length || commentIndex < comments.length) { - nextStart = - commentIndex < comments.length - ? comments[commentIndex].range[0] - : Number.MAX_SAFE_INTEGER; - while ( - tokenIndex < tokens.length && - (range = tokens[tokenIndex].range)[0] < nextStart - ) { - map[range[0]] = tokenIndex; - map[range[1] - 1] = tokenIndex; - tokenIndex += 1; - } - - nextStart = - tokenIndex < tokens.length - ? tokens[tokenIndex].range[0] - : Number.MAX_SAFE_INTEGER; - while ( - commentIndex < comments.length && - (range = comments[commentIndex].range)[0] < nextStart - ) { - map[range[0]] = tokenIndex; - map[range[1] - 1] = tokenIndex; - commentIndex += 1; - } - } - - return map; -} - -/** - * Creates the cursor iterates tokens with options. - * @param {CursorFactory} factory The cursor factory to initialize cursor. - * @param {Token[]} tokens The array of tokens. - * @param {Comment[]} comments The array of comments. - * @param {Object} indexMap The map from locations to indices in `tokens`. - * @param {number} startLoc The start location of the iteration range. - * @param {number} endLoc The end location of the iteration range. - * @param {number|Function|Object} [opts=0] The option object. If this is a number then it's `opts.skip`. If this is a function then it's `opts.filter`. - * @param {boolean} [opts.includeComments=false] The flag to iterate comments as well. - * @param {Function|null} [opts.filter=null] The predicate function to choose tokens. - * @param {number} [opts.skip=0] The count of tokens the cursor skips. - * @returns {Cursor} The created cursor. - * @private - */ -function createCursorWithSkip( - factory, - tokens, - comments, - indexMap, - startLoc, - endLoc, - opts, -) { - let includeComments = false; - let skip = 0; - let filter = null; - - if (typeof opts === "number") { - skip = opts | 0; - } else if (typeof opts === "function") { - filter = opts; - } else if (opts) { - includeComments = !!opts.includeComments; - skip = opts.skip | 0; - filter = opts.filter || null; - } - assert(skip >= 0, "options.skip should be zero or a positive integer."); - assert( - !filter || typeof filter === "function", - "options.filter should be a function.", - ); - - return factory.createCursor( - tokens, - comments, - indexMap, - startLoc, - endLoc, - includeComments, - filter, - skip, - -1, - ); -} - -/** - * Creates the cursor iterates tokens with options. - * @param {CursorFactory} factory The cursor factory to initialize cursor. - * @param {Token[]} tokens The array of tokens. - * @param {Comment[]} comments The array of comments. - * @param {Object} indexMap The map from locations to indices in `tokens`. - * @param {number} startLoc The start location of the iteration range. - * @param {number} endLoc The end location of the iteration range. - * @param {number|Function|Object} [opts=0] The option object. If this is a number then it's `opts.count`. If this is a function then it's `opts.filter`. - * @param {boolean} [opts.includeComments] The flag to iterate comments as well. - * @param {Function|null} [opts.filter=null] The predicate function to choose tokens. - * @param {number} [opts.count=0] The maximum count of tokens the cursor iterates. Zero is no iteration for backward compatibility. - * @returns {Cursor} The created cursor. - * @private - */ -function createCursorWithCount( - factory, - tokens, - comments, - indexMap, - startLoc, - endLoc, - opts, -) { - let includeComments = false; - let count = 0; - let countExists = false; - let filter = null; - - if (typeof opts === "number") { - count = opts | 0; - countExists = true; - } else if (typeof opts === "function") { - filter = opts; - } else if (opts) { - includeComments = !!opts.includeComments; - count = opts.count | 0; - countExists = typeof opts.count === "number"; - filter = opts.filter || null; - } - assert(count >= 0, "options.count should be zero or a positive integer."); - assert( - !filter || typeof filter === "function", - "options.filter should be a function.", - ); - - return factory.createCursor( - tokens, - comments, - indexMap, - startLoc, - endLoc, - includeComments, - filter, - 0, - countExists ? count : -1, - ); -} - -/** - * Creates the cursor iterates tokens with options. - * This is overload function of the below. - * @param {Token[]} tokens The array of tokens. - * @param {Comment[]} comments The array of comments. - * @param {Object} indexMap The map from locations to indices in `tokens`. - * @param {number} startLoc The start location of the iteration range. - * @param {number} endLoc The end location of the iteration range. - * @param {Function|Object} opts The option object. If this is a function then it's `opts.filter`. - * @param {boolean} [opts.includeComments] The flag to iterate comments as well. - * @param {Function|null} [opts.filter=null] The predicate function to choose tokens. - * @param {number} [opts.count=0] The maximum count of tokens the cursor iterates. Zero is no iteration for backward compatibility. - * @returns {Cursor} The created cursor. - * @private - */ -/** - * Creates the cursor iterates tokens with options. - * @param {Token[]} tokens The array of tokens. - * @param {Comment[]} comments The array of comments. - * @param {Object} indexMap The map from locations to indices in `tokens`. - * @param {number} startLoc The start location of the iteration range. - * @param {number} endLoc The end location of the iteration range. - * @param {number} [beforeCount=0] The number of tokens before the node to retrieve. - * @param {boolean} [afterCount=0] The number of tokens after the node to retrieve. - * @returns {Cursor} The created cursor. - * @private - */ -function createCursorWithPadding( - tokens, - comments, - indexMap, - startLoc, - endLoc, - beforeCount, - afterCount, -) { - if ( - typeof beforeCount === "undefined" && - typeof afterCount === "undefined" - ) { - return new ForwardTokenCursor( - tokens, - comments, - indexMap, - startLoc, - endLoc, - ); - } - if (typeof beforeCount === "number" || typeof beforeCount === "undefined") { - return new PaddedTokenCursor( - tokens, - comments, - indexMap, - startLoc, - endLoc, - beforeCount | 0, - afterCount | 0, - ); - } - return createCursorWithCount( - cursors.forward, - tokens, - comments, - indexMap, - startLoc, - endLoc, - beforeCount, - ); -} - -/** - * Gets comment tokens that are adjacent to the current cursor position. - * @param {Cursor} cursor A cursor instance. - * @returns {Array} An array of comment tokens adjacent to the current cursor position. - * @private - */ -function getAdjacentCommentTokensFromCursor(cursor) { - const tokens = []; - let currentToken = cursor.getOneToken(); - - while (currentToken && isCommentToken(currentToken)) { - tokens.push(currentToken); - currentToken = cursor.getOneToken(); - } - - return tokens; -} - -//------------------------------------------------------------------------------ -// Exports -//------------------------------------------------------------------------------ - -/** - * The token store. - * - * This class provides methods to get tokens by locations as fast as possible. - * The methods are a part of public API, so we should be careful if it changes this class. - * - * People can get tokens in O(1) by the hash map which is mapping from the location of tokens/comments to tokens. - * Also people can get a mix of tokens and comments in O(log k), the k is the number of comments. - * Assuming that comments to be much fewer than tokens, this does not make hash map from token's locations to comments to reduce memory cost. - * This uses binary-searching instead for comments. - */ -module.exports = class TokenStore { - /** - * Initializes this token store. - * @param {Token[]} tokens The array of tokens. - * @param {Comment[]} comments The array of comments. - */ - constructor(tokens, comments) { - this[TOKENS] = tokens; - this[COMMENTS] = comments; - this[INDEX_MAP] = createIndexMap(tokens, comments); - } - - //-------------------------------------------------------------------------- - // Gets single token. - //-------------------------------------------------------------------------- - - /** - * Gets the token starting at the specified index. - * @param {number} offset Index of the start of the token's range. - * @param {Object} [options=0] The option object. - * @param {boolean} [options.includeComments=false] The flag to iterate comments as well. - * @returns {Token|null} The token starting at index, or null if no such token. - */ - getTokenByRangeStart(offset, options) { - const includeComments = options && options.includeComments; - const token = cursors.forward - .createBaseCursor( - this[TOKENS], - this[COMMENTS], - this[INDEX_MAP], - offset, - -1, - includeComments, - ) - .getOneToken(); - - if (token && token.range[0] === offset) { - return token; - } - return null; - } - - /** - * Gets the first token of the given node. - * @param {ASTNode} node The AST node. - * @param {number|Function|Object} [options=0] The option object. If this is a number then it's `options.skip`. If this is a function then it's `options.filter`. - * @param {boolean} [options.includeComments=false] The flag to iterate comments as well. - * @param {Function|null} [options.filter=null] The predicate function to choose tokens. - * @param {number} [options.skip=0] The count of tokens the cursor skips. - * @returns {Token|null} An object representing the token. - */ - getFirstToken(node, options) { - return createCursorWithSkip( - cursors.forward, - this[TOKENS], - this[COMMENTS], - this[INDEX_MAP], - node.range[0], - node.range[1], - options, - ).getOneToken(); - } - - /** - * Gets the last token of the given node. - * @param {ASTNode} node The AST node. - * @param {number|Function|Object} [options=0] The option object. Same options as getFirstToken() - * @returns {Token|null} An object representing the token. - */ - getLastToken(node, options) { - return createCursorWithSkip( - cursors.backward, - this[TOKENS], - this[COMMENTS], - this[INDEX_MAP], - node.range[0], - node.range[1], - options, - ).getOneToken(); - } - - /** - * Gets the token that precedes a given node or token. - * @param {ASTNode|Token|Comment} node The AST node or token. - * @param {number|Function|Object} [options=0] The option object. Same options as getFirstToken() - * @returns {Token|null} An object representing the token. - */ - getTokenBefore(node, options) { - return createCursorWithSkip( - cursors.backward, - this[TOKENS], - this[COMMENTS], - this[INDEX_MAP], - -1, - node.range[0], - options, - ).getOneToken(); - } - - /** - * Gets the token that follows a given node or token. - * @param {ASTNode|Token|Comment} node The AST node or token. - * @param {number|Function|Object} [options=0] The option object. Same options as getFirstToken() - * @returns {Token|null} An object representing the token. - */ - getTokenAfter(node, options) { - return createCursorWithSkip( - cursors.forward, - this[TOKENS], - this[COMMENTS], - this[INDEX_MAP], - node.range[1], - -1, - options, - ).getOneToken(); - } - - /** - * Gets the first token between two non-overlapping nodes. - * @param {ASTNode|Token|Comment} left Node before the desired token range. - * @param {ASTNode|Token|Comment} right Node after the desired token range. - * @param {number|Function|Object} [options=0] The option object. Same options as getFirstToken() - * @returns {Token|null} An object representing the token. - */ - getFirstTokenBetween(left, right, options) { - return createCursorWithSkip( - cursors.forward, - this[TOKENS], - this[COMMENTS], - this[INDEX_MAP], - left.range[1], - right.range[0], - options, - ).getOneToken(); - } - - /** - * Gets the last token between two non-overlapping nodes. - * @param {ASTNode|Token|Comment} left Node before the desired token range. - * @param {ASTNode|Token|Comment} right Node after the desired token range. - * @param {number|Function|Object} [options=0] The option object. Same options as getFirstToken() - * @returns {Token|null} An object representing the token. - */ - getLastTokenBetween(left, right, options) { - return createCursorWithSkip( - cursors.backward, - this[TOKENS], - this[COMMENTS], - this[INDEX_MAP], - left.range[1], - right.range[0], - options, - ).getOneToken(); - } - - /** - * Gets the token that precedes a given node or token in the token stream. - * This is defined for backward compatibility. Use `includeComments` option instead. - * TODO: We have a plan to remove this in a future major version. - * @param {ASTNode|Token|Comment} node The AST node or token. - * @param {number} [skip=0] A number of tokens to skip. - * @returns {Token|null} An object representing the token. - * @deprecated - */ - getTokenOrCommentBefore(node, skip) { - return this.getTokenBefore(node, { includeComments: true, skip }); - } - - /** - * Gets the token that follows a given node or token in the token stream. - * This is defined for backward compatibility. Use `includeComments` option instead. - * TODO: We have a plan to remove this in a future major version. - * @param {ASTNode|Token|Comment} node The AST node or token. - * @param {number} [skip=0] A number of tokens to skip. - * @returns {Token|null} An object representing the token. - * @deprecated - */ - getTokenOrCommentAfter(node, skip) { - return this.getTokenAfter(node, { includeComments: true, skip }); - } - - //-------------------------------------------------------------------------- - // Gets multiple tokens. - //-------------------------------------------------------------------------- - - /** - * Gets the first `count` tokens of the given node. - * @param {ASTNode} node The AST node. - * @param {number|Function|Object} [options=0] The option object. If this is a number then it's `options.count`. If this is a function then it's `options.filter`. - * @param {boolean} [options.includeComments=false] The flag to iterate comments as well. - * @param {Function|null} [options.filter=null] The predicate function to choose tokens. - * @param {number} [options.count=0] The maximum count of tokens the cursor iterates. - * @returns {Token[]} Tokens. - */ - getFirstTokens(node, options) { - return createCursorWithCount( - cursors.forward, - this[TOKENS], - this[COMMENTS], - this[INDEX_MAP], - node.range[0], - node.range[1], - options, - ).getAllTokens(); - } - - /** - * Gets the last `count` tokens of the given node. - * @param {ASTNode} node The AST node. - * @param {number|Function|Object} [options=0] The option object. Same options as getFirstTokens() - * @returns {Token[]} Tokens. - */ - getLastTokens(node, options) { - return createCursorWithCount( - cursors.backward, - this[TOKENS], - this[COMMENTS], - this[INDEX_MAP], - node.range[0], - node.range[1], - options, - ) - .getAllTokens() - .reverse(); - } - - /** - * Gets the `count` tokens that precedes a given node or token. - * @param {ASTNode|Token|Comment} node The AST node or token. - * @param {number|Function|Object} [options=0] The option object. Same options as getFirstTokens() - * @returns {Token[]} Tokens. - */ - getTokensBefore(node, options) { - return createCursorWithCount( - cursors.backward, - this[TOKENS], - this[COMMENTS], - this[INDEX_MAP], - -1, - node.range[0], - options, - ) - .getAllTokens() - .reverse(); - } - - /** - * Gets the `count` tokens that follows a given node or token. - * @param {ASTNode|Token|Comment} node The AST node or token. - * @param {number|Function|Object} [options=0] The option object. Same options as getFirstTokens() - * @returns {Token[]} Tokens. - */ - getTokensAfter(node, options) { - return createCursorWithCount( - cursors.forward, - this[TOKENS], - this[COMMENTS], - this[INDEX_MAP], - node.range[1], - -1, - options, - ).getAllTokens(); - } - - /** - * Gets the first `count` tokens between two non-overlapping nodes. - * @param {ASTNode|Token|Comment} left Node before the desired token range. - * @param {ASTNode|Token|Comment} right Node after the desired token range. - * @param {number|Function|Object} [options=0] The option object. Same options as getFirstTokens() - * @returns {Token[]} Tokens between left and right. - */ - getFirstTokensBetween(left, right, options) { - return createCursorWithCount( - cursors.forward, - this[TOKENS], - this[COMMENTS], - this[INDEX_MAP], - left.range[1], - right.range[0], - options, - ).getAllTokens(); - } - - /** - * Gets the last `count` tokens between two non-overlapping nodes. - * @param {ASTNode|Token|Comment} left Node before the desired token range. - * @param {ASTNode|Token|Comment} right Node after the desired token range. - * @param {number|Function|Object} [options=0] The option object. Same options as getFirstTokens() - * @returns {Token[]} Tokens between left and right. - */ - getLastTokensBetween(left, right, options) { - return createCursorWithCount( - cursors.backward, - this[TOKENS], - this[COMMENTS], - this[INDEX_MAP], - left.range[1], - right.range[0], - options, - ) - .getAllTokens() - .reverse(); - } - - /** - * Gets all tokens that are related to the given node. - * @param {ASTNode} node The AST node. - * @param {Function|Object} options The option object. If this is a function then it's `options.filter`. - * @param {boolean} [options.includeComments=false] The flag to iterate comments as well. - * @param {Function|null} [options.filter=null] The predicate function to choose tokens. - * @param {number} [options.count=0] The maximum count of tokens the cursor iterates. - * @returns {Token[]} Array of objects representing tokens. - */ - /** - * Gets all tokens that are related to the given node. - * @param {ASTNode} node The AST node. - * @param {number} [beforeCount=0] The number of tokens before the node to retrieve. - * @param {number} [afterCount=0] The number of tokens after the node to retrieve. - * @returns {Token[]} Array of objects representing tokens. - */ - getTokens(node, beforeCount, afterCount) { - return createCursorWithPadding( - this[TOKENS], - this[COMMENTS], - this[INDEX_MAP], - node.range[0], - node.range[1], - beforeCount, - afterCount, - ).getAllTokens(); - } - - /** - * Gets all of the tokens between two non-overlapping nodes. - * @param {ASTNode|Token|Comment} left Node before the desired token range. - * @param {ASTNode|Token|Comment} right Node after the desired token range. - * @param {Function|Object} options The option object. If this is a function then it's `options.filter`. - * @param {boolean} [options.includeComments=false] The flag to iterate comments as well. - * @param {Function|null} [options.filter=null] The predicate function to choose tokens. - * @param {number} [options.count=0] The maximum count of tokens the cursor iterates. - * @returns {Token[]} Tokens between left and right. - */ - /** - * Gets all of the tokens between two non-overlapping nodes. - * @param {ASTNode|Token|Comment} left Node before the desired token range. - * @param {ASTNode|Token|Comment} right Node after the desired token range. - * @param {number} [padding=0] Number of extra tokens on either side of center. - * @returns {Token[]} Tokens between left and right. - */ - getTokensBetween(left, right, padding) { - return createCursorWithPadding( - this[TOKENS], - this[COMMENTS], - this[INDEX_MAP], - left.range[1], - right.range[0], - padding, - padding, - ).getAllTokens(); - } - - //-------------------------------------------------------------------------- - // Others. - //-------------------------------------------------------------------------- - - /** - * Checks whether any comments exist or not between the given 2 nodes. - * @param {ASTNode} left The node to check. - * @param {ASTNode} right The node to check. - * @returns {boolean} `true` if one or more comments exist. - */ - commentsExistBetween(left, right) { - const index = utils.search(this[COMMENTS], left.range[1]); - - return ( - index < this[COMMENTS].length && - this[COMMENTS][index].range[1] <= right.range[0] - ); - } - - /** - * Gets all comment tokens directly before the given node or token. - * @param {ASTNode|token} nodeOrToken The AST node or token to check for adjacent comment tokens. - * @returns {Array} An array of comments in occurrence order. - */ - getCommentsBefore(nodeOrToken) { - const cursor = createCursorWithCount( - cursors.backward, - this[TOKENS], - this[COMMENTS], - this[INDEX_MAP], - -1, - nodeOrToken.range[0], - { includeComments: true }, - ); - - return getAdjacentCommentTokensFromCursor(cursor).reverse(); - } - - /** - * Gets all comment tokens directly after the given node or token. - * @param {ASTNode|token} nodeOrToken The AST node or token to check for adjacent comment tokens. - * @returns {Array} An array of comments in occurrence order. - */ - getCommentsAfter(nodeOrToken) { - const cursor = createCursorWithCount( - cursors.forward, - this[TOKENS], - this[COMMENTS], - this[INDEX_MAP], - nodeOrToken.range[1], - -1, - { includeComments: true }, - ); - - return getAdjacentCommentTokensFromCursor(cursor); - } - - /** - * Gets all comment tokens inside the given node. - * @param {ASTNode} node The AST node to get the comments for. - * @returns {Array} An array of comments in occurrence order. - */ - getCommentsInside(node) { - return this.getTokens(node, { - includeComments: true, - filter: isCommentToken, - }); - } -}; diff --git a/node_modules/eslint/lib/languages/js/source-code/token-store/limit-cursor.js b/node_modules/eslint/lib/languages/js/source-code/token-store/limit-cursor.js deleted file mode 100644 index 301d7f54c..000000000 --- a/node_modules/eslint/lib/languages/js/source-code/token-store/limit-cursor.js +++ /dev/null @@ -1,39 +0,0 @@ -/** - * @fileoverview Define the cursor which limits the number of tokens. - * @author Toru Nagashima - */ -"use strict"; - -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ - -const DecorativeCursor = require("./decorative-cursor"); - -//------------------------------------------------------------------------------ -// Exports -//------------------------------------------------------------------------------ - -/** - * The decorative cursor which limits the number of tokens. - */ -module.exports = class LimitCursor extends DecorativeCursor { - /** - * Initializes this cursor. - * @param {Cursor} cursor The cursor to be decorated. - * @param {number} count The count of tokens this cursor iterates. - */ - constructor(cursor, count) { - super(cursor); - this.count = count; - } - - /** @inheritdoc */ - moveNext() { - if (this.count > 0) { - this.count -= 1; - return super.moveNext(); - } - return false; - } -}; diff --git a/node_modules/eslint/lib/languages/js/source-code/token-store/padded-token-cursor.js b/node_modules/eslint/lib/languages/js/source-code/token-store/padded-token-cursor.js deleted file mode 100644 index 3d3ce04f1..000000000 --- a/node_modules/eslint/lib/languages/js/source-code/token-store/padded-token-cursor.js +++ /dev/null @@ -1,45 +0,0 @@ -/** - * @fileoverview Define the cursor which iterates tokens only, with inflated range. - * @author Toru Nagashima - */ -"use strict"; - -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ - -const ForwardTokenCursor = require("./forward-token-cursor"); - -//------------------------------------------------------------------------------ -// Exports -//------------------------------------------------------------------------------ - -/** - * The cursor which iterates tokens only, with inflated range. - * This is for the backward compatibility of padding options. - */ -module.exports = class PaddedTokenCursor extends ForwardTokenCursor { - /** - * Initializes this cursor. - * @param {Token[]} tokens The array of tokens. - * @param {Comment[]} comments The array of comments. - * @param {Object} indexMap The map from locations to indices in `tokens`. - * @param {number} startLoc The start location of the iteration range. - * @param {number} endLoc The end location of the iteration range. - * @param {number} beforeCount The number of tokens this cursor iterates before start. - * @param {number} afterCount The number of tokens this cursor iterates after end. - */ - constructor( - tokens, - comments, - indexMap, - startLoc, - endLoc, - beforeCount, - afterCount, - ) { - super(tokens, comments, indexMap, startLoc, endLoc); - this.index = Math.max(0, this.index - beforeCount); - this.indexEnd = Math.min(tokens.length - 1, this.indexEnd + afterCount); - } -}; diff --git a/node_modules/eslint/lib/languages/js/source-code/token-store/skip-cursor.js b/node_modules/eslint/lib/languages/js/source-code/token-store/skip-cursor.js deleted file mode 100644 index 6bc43d856..000000000 --- a/node_modules/eslint/lib/languages/js/source-code/token-store/skip-cursor.js +++ /dev/null @@ -1,41 +0,0 @@ -/** - * @fileoverview Define the cursor which ignores the first few tokens. - * @author Toru Nagashima - */ -"use strict"; - -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ - -const DecorativeCursor = require("./decorative-cursor"); - -//------------------------------------------------------------------------------ -// Exports -//------------------------------------------------------------------------------ - -/** - * The decorative cursor which ignores the first few tokens. - */ -module.exports = class SkipCursor extends DecorativeCursor { - /** - * Initializes this cursor. - * @param {Cursor} cursor The cursor to be decorated. - * @param {number} count The count of tokens this cursor skips. - */ - constructor(cursor, count) { - super(cursor); - this.count = count; - } - - /** @inheritdoc */ - moveNext() { - while (this.count > 0) { - this.count -= 1; - if (!super.moveNext()) { - return false; - } - } - return super.moveNext(); - } -}; diff --git a/node_modules/eslint/lib/languages/js/source-code/token-store/utils.js b/node_modules/eslint/lib/languages/js/source-code/token-store/utils.js deleted file mode 100644 index 94e456c9f..000000000 --- a/node_modules/eslint/lib/languages/js/source-code/token-store/utils.js +++ /dev/null @@ -1,110 +0,0 @@ -/** - * @fileoverview Define utility functions for token store. - * @author Toru Nagashima - */ -"use strict"; - -//------------------------------------------------------------------------------ -// Exports -//------------------------------------------------------------------------------ - -/** - * Finds the index of the first token which is after the given location. - * If it was not found, this returns `tokens.length`. - * @param {(Token|Comment)[]} tokens It searches the token in this list. - * @param {number} location The location to search. - * @returns {number} The found index or `tokens.length`. - */ -exports.search = function search(tokens, location) { - for ( - let minIndex = 0, maxIndex = tokens.length - 1; - minIndex <= maxIndex; - - ) { - /* - * Calculate the index in the middle between minIndex and maxIndex. - * `| 0` is used to round a fractional value down to the nearest integer: this is similar to - * using `Math.trunc()` or `Math.floor()`, but performance tests have shown this method to - * be faster. - */ - const index = ((minIndex + maxIndex) / 2) | 0; - const token = tokens[index]; - const tokenStartLocation = token.range[0]; - - if (location <= tokenStartLocation) { - if (index === minIndex) { - return index; - } - maxIndex = index; - } else { - minIndex = index + 1; - } - } - return tokens.length; -}; - -/** - * Gets the index of the `startLoc` in `tokens`. - * `startLoc` can be the value of `node.range[1]`, so this checks about `startLoc - 1` as well. - * @param {(Token|Comment)[]} tokens The tokens to find an index. - * @param {Object} indexMap The map from locations to indices. - * @param {number} startLoc The location to get an index. - * @returns {number} The index. - */ -exports.getFirstIndex = function getFirstIndex(tokens, indexMap, startLoc) { - if (startLoc in indexMap) { - return indexMap[startLoc]; - } - if (startLoc - 1 in indexMap) { - const index = indexMap[startLoc - 1]; - const token = tokens[index]; - - // If the mapped index is out of bounds, the returned cursor index will point after the end of the tokens array. - if (!token) { - return tokens.length; - } - - /* - * For the map of "comment's location -> token's index", it points the next token of a comment. - * In that case, +1 is unnecessary. - */ - if (token.range[0] >= startLoc) { - return index; - } - return index + 1; - } - return 0; -}; - -/** - * Gets the index of the `endLoc` in `tokens`. - * The information of end locations are recorded at `endLoc - 1` in `indexMap`, so this checks about `endLoc - 1` as well. - * @param {(Token|Comment)[]} tokens The tokens to find an index. - * @param {Object} indexMap The map from locations to indices. - * @param {number} endLoc The location to get an index. - * @returns {number} The index. - */ -exports.getLastIndex = function getLastIndex(tokens, indexMap, endLoc) { - if (endLoc in indexMap) { - return indexMap[endLoc] - 1; - } - if (endLoc - 1 in indexMap) { - const index = indexMap[endLoc - 1]; - const token = tokens[index]; - - // If the mapped index is out of bounds, the returned cursor index will point before the end of the tokens array. - if (!token) { - return tokens.length - 1; - } - - /* - * For the map of "comment's location -> token's index", it points the next token of a comment. - * In that case, -1 is necessary. - */ - if (token.range[1] > endLoc) { - return index - 1; - } - return index; - } - return tokens.length - 1; -}; diff --git a/node_modules/eslint/lib/languages/js/validate-language-options.js b/node_modules/eslint/lib/languages/js/validate-language-options.js deleted file mode 100644 index 23ed26664..000000000 --- a/node_modules/eslint/lib/languages/js/validate-language-options.js +++ /dev/null @@ -1,196 +0,0 @@ -/** - * @fileoverview The schema to validate language options - * @author Nicholas C. Zakas - */ - -"use strict"; - -//----------------------------------------------------------------------------- -// Data -//----------------------------------------------------------------------------- - -const globalVariablesValues = new Set([ - true, - "true", - "writable", - "writeable", - false, - "false", - "readonly", - "readable", - null, - "off", -]); - -//------------------------------------------------------------------------------ -// Helpers -//------------------------------------------------------------------------------ - -/** - * Check if a value is a non-null object. - * @param {any} value The value to check. - * @returns {boolean} `true` if the value is a non-null object. - */ -function isNonNullObject(value) { - return typeof value === "object" && value !== null; -} - -/** - * Check if a value is a non-null non-array object. - * @param {any} value The value to check. - * @returns {boolean} `true` if the value is a non-null non-array object. - */ -function isNonArrayObject(value) { - return isNonNullObject(value) && !Array.isArray(value); -} - -/** - * Check if a value is undefined. - * @param {any} value The value to check. - * @returns {boolean} `true` if the value is undefined. - */ -function isUndefined(value) { - return typeof value === "undefined"; -} - -//----------------------------------------------------------------------------- -// Schemas -//----------------------------------------------------------------------------- - -/** - * Validates the ecmaVersion property. - * @param {string|number} ecmaVersion The value to check. - * @returns {void} - * @throws {TypeError} If the value is invalid. - */ -function validateEcmaVersion(ecmaVersion) { - if (isUndefined(ecmaVersion)) { - throw new TypeError( - 'Key "ecmaVersion": Expected an "ecmaVersion" property.', - ); - } - - if (typeof ecmaVersion !== "number" && ecmaVersion !== "latest") { - throw new TypeError( - 'Key "ecmaVersion": Expected a number or "latest".', - ); - } -} - -/** - * Validates the sourceType property. - * @param {string} sourceType The value to check. - * @returns {void} - * @throws {TypeError} If the value is invalid. - */ -function validateSourceType(sourceType) { - if ( - typeof sourceType !== "string" || - !/^(?:script|module|commonjs)$/u.test(sourceType) - ) { - throw new TypeError( - 'Key "sourceType": Expected "script", "module", or "commonjs".', - ); - } -} - -/** - * Validates the globals property. - * @param {Object} globals The value to check. - * @returns {void} - * @throws {TypeError} If the value is invalid. - */ -function validateGlobals(globals) { - if (!isNonArrayObject(globals)) { - throw new TypeError('Key "globals": Expected an object.'); - } - - for (const key of Object.keys(globals)) { - // avoid hairy edge case - if (key === "__proto__") { - continue; - } - - if (key !== key.trim()) { - throw new TypeError( - `Key "globals": Global "${key}" has leading or trailing whitespace.`, - ); - } - - if (!globalVariablesValues.has(globals[key])) { - throw new TypeError( - `Key "globals": Key "${key}": Expected "readonly", "writable", or "off".`, - ); - } - } -} - -/** - * Validates the parser property. - * @param {Object} parser The value to check. - * @returns {void} - * @throws {TypeError} If the value is invalid. - */ -function validateParser(parser) { - if ( - !parser || - typeof parser !== "object" || - (typeof parser.parse !== "function" && - typeof parser.parseForESLint !== "function") - ) { - throw new TypeError( - 'Key "parser": Expected object with parse() or parseForESLint() method.', - ); - } -} - -/** - * Validates the language options. - * @param {Object} languageOptions The language options to validate. - * @returns {void} - * @throws {TypeError} If the language options are invalid. - */ -function validateLanguageOptions(languageOptions) { - if (!isNonArrayObject(languageOptions)) { - throw new TypeError("Expected an object."); - } - - const { - ecmaVersion, - sourceType, - globals, - parser, - parserOptions, - ...otherOptions - } = languageOptions; - - if ("ecmaVersion" in languageOptions) { - validateEcmaVersion(ecmaVersion); - } - - if ("sourceType" in languageOptions) { - validateSourceType(sourceType); - } - - if ("globals" in languageOptions) { - validateGlobals(globals); - } - - if ("parser" in languageOptions) { - validateParser(parser); - } - - if ("parserOptions" in languageOptions) { - if (!isNonArrayObject(parserOptions)) { - throw new TypeError('Key "parserOptions": Expected an object.'); - } - } - - const otherOptionKeys = Object.keys(otherOptions); - - if (otherOptionKeys.length > 0) { - throw new TypeError(`Unexpected key "${otherOptionKeys[0]}" found.`); - } -} - -module.exports = { validateLanguageOptions }; diff --git a/node_modules/eslint/lib/linter/apply-disable-directives.js b/node_modules/eslint/lib/linter/apply-disable-directives.js index 529be9e73..c5e3c9ddc 100644 --- a/node_modules/eslint/lib/linter/apply-disable-directives.js +++ b/node_modules/eslint/lib/linter/apply-disable-directives.js @@ -9,27 +9,23 @@ // Typedefs //------------------------------------------------------------------------------ -/** @typedef {import("../types").Linter.LintMessage} LintMessage */ -/** @typedef {import("@eslint/core").Language} Language */ -/** @typedef {import("@eslint/core").Position} Position */ -/** @typedef {import("@eslint/core").RulesConfig} RulesConfig */ +/** @typedef {import("../shared/types").LintMessage} LintMessage */ //------------------------------------------------------------------------------ // Module Definition //------------------------------------------------------------------------------ const escapeRegExp = require("escape-string-regexp"); -const { Config } = require("../config/config.js"); /** * Compares the locations of two objects in a source file - * @param {Position} itemA The first object - * @param {Position} itemB The second object + * @param {{line: number, column: number}} itemA The first object + * @param {{line: number, column: number}} itemB The second object * @returns {number} A value less than 1 if itemA appears before itemB in the source file, greater than 1 if * itemA appears after itemB in the source file, or 0 if itemA and itemB have the same location. */ function compareLocations(itemA, itemB) { - return itemA.line - itemB.line || itemA.column - itemB.column; + return itemA.line - itemB.line || itemA.column - itemB.column; } /** @@ -37,180 +33,170 @@ function compareLocations(itemA, itemB) { * @param {Iterable} directives Unused directives to be removed. * @returns {Directive[][]} Directives grouped by their parent comment. */ -function groupByParentDirective(directives) { - const groups = new Map(); - - for (const directive of directives) { - const { - unprocessedDirective: { parentDirective }, - } = directive; - - if (groups.has(parentDirective)) { - groups.get(parentDirective).push(directive); - } else { - groups.set(parentDirective, [directive]); - } - } - - return [...groups.values()]; +function groupByParentComment(directives) { + const groups = new Map(); + + for (const directive of directives) { + const { unprocessedDirective: { parentComment } } = directive; + + if (groups.has(parentComment)) { + groups.get(parentComment).push(directive); + } else { + groups.set(parentComment, [directive]); + } + } + + return [...groups.values()]; } /** * Creates removal details for a set of directives within the same comment. * @param {Directive[]} directives Unused directives to be removed. - * @param {{node: Token, value: string}} parentDirective Data about the backing directive. - * @param {SourceCode} sourceCode The source code object for the file being linted. + * @param {Token} commentToken The backing Comment token. * @returns {{ description, fix, unprocessedDirective }[]} Details for later creation of output Problems. */ -function createIndividualDirectivesRemoval( - directives, - parentDirective, - sourceCode, -) { - /* - * Get the list of the rules text without any surrounding whitespace. In order to preserve the original - * formatting, we don't want to change that whitespace. - * - * // eslint-disable-line rule-one , rule-two , rule-three -- comment - * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - */ - const listText = parentDirective.value.trim(); - - // Calculate where it starts in the source code text - const listStart = sourceCode.text.indexOf( - listText, - sourceCode.getRange(parentDirective.node)[0], - ); - - /* - * We can assume that `listText` contains multiple elements. - * Otherwise, this function wouldn't be called - if there is - * only one rule in the list, then the whole comment must be removed. - */ - - return directives.map(directive => { - const { ruleId } = directive; - - const regex = new RegExp( - String.raw`(?:^|\s*,\s*)(?['"]?)${escapeRegExp(ruleId)}\k(?:\s*,\s*|$)`, - "u", - ); - const match = regex.exec(listText); - const matchedText = match[0]; - const matchStart = listStart + match.index; - const matchEnd = matchStart + matchedText.length; - - const firstIndexOfComma = matchedText.indexOf(","); - const lastIndexOfComma = matchedText.lastIndexOf(","); - - let removalStart, removalEnd; - - if (firstIndexOfComma !== lastIndexOfComma) { - /* - * Since there are two commas, this must one of the elements in the middle of the list. - * Matched range starts where the previous rule name ends, and ends where the next rule name starts. - * - * // eslint-disable-line rule-one , rule-two , rule-three -- comment - * ^^^^^^^^^^^^^^ - * - * We want to remove only the content between the two commas, and also one of the commas. - * - * // eslint-disable-line rule-one , rule-two , rule-three -- comment - * ^^^^^^^^^^^ - */ - removalStart = matchStart + firstIndexOfComma; - removalEnd = matchStart + lastIndexOfComma; - } else { - /* - * This is either the first element or the last element. - * - * If this is the first element, matched range starts where the first rule name starts - * and ends where the second rule name starts. This is exactly the range we want - * to remove so that the second rule name will start where the first one was starting - * and thus preserve the original formatting. - * - * // eslint-disable-line rule-one , rule-two , rule-three -- comment - * ^^^^^^^^^^^ - * - * Similarly, if this is the last element, we've already matched the range we want to - * remove. The previous rule name will end where the last one was ending, relative - * to the content on the right side. - * - * // eslint-disable-line rule-one , rule-two , rule-three -- comment - * ^^^^^^^^^^^^^ - */ - removalStart = matchStart; - removalEnd = matchEnd; - } - - return { - description: `'${ruleId}'`, - fix: { - range: [removalStart, removalEnd], - text: "", - }, - unprocessedDirective: directive.unprocessedDirective, - }; - }); +function createIndividualDirectivesRemoval(directives, commentToken) { + + /* + * `commentToken.value` starts right after `//` or `/*`. + * All calculated offsets will be relative to this index. + */ + const commentValueStart = commentToken.range[0] + "//".length; + + // Find where the list of rules starts. `\S+` matches with the directive name (e.g. `eslint-disable-line`) + const listStartOffset = /^\s*\S+\s+/u.exec(commentToken.value)[0].length; + + /* + * Get the list text without any surrounding whitespace. In order to preserve the original + * formatting, we don't want to change that whitespace. + * + * // eslint-disable-line rule-one , rule-two , rule-three -- comment + * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + */ + const listText = commentToken.value + .slice(listStartOffset) // remove directive name and all whitespace before the list + .split(/\s-{2,}\s/u)[0] // remove `-- comment`, if it exists + .trimEnd(); // remove all whitespace after the list + + /* + * We can assume that `listText` contains multiple elements. + * Otherwise, this function wouldn't be called - if there is + * only one rule in the list, then the whole comment must be removed. + */ + + return directives.map(directive => { + const { ruleId } = directive; + + const regex = new RegExp(String.raw`(?:^|\s*,\s*)(?['"]?)${escapeRegExp(ruleId)}\k(?:\s*,\s*|$)`, "u"); + const match = regex.exec(listText); + const matchedText = match[0]; + const matchStartOffset = listStartOffset + match.index; + const matchEndOffset = matchStartOffset + matchedText.length; + + const firstIndexOfComma = matchedText.indexOf(","); + const lastIndexOfComma = matchedText.lastIndexOf(","); + + let removalStartOffset, removalEndOffset; + + if (firstIndexOfComma !== lastIndexOfComma) { + + /* + * Since there are two commas, this must one of the elements in the middle of the list. + * Matched range starts where the previous rule name ends, and ends where the next rule name starts. + * + * // eslint-disable-line rule-one , rule-two , rule-three -- comment + * ^^^^^^^^^^^^^^ + * + * We want to remove only the content between the two commas, and also one of the commas. + * + * // eslint-disable-line rule-one , rule-two , rule-three -- comment + * ^^^^^^^^^^^ + */ + removalStartOffset = matchStartOffset + firstIndexOfComma; + removalEndOffset = matchStartOffset + lastIndexOfComma; + + } else { + + /* + * This is either the first element or the last element. + * + * If this is the first element, matched range starts where the first rule name starts + * and ends where the second rule name starts. This is exactly the range we want + * to remove so that the second rule name will start where the first one was starting + * and thus preserve the original formatting. + * + * // eslint-disable-line rule-one , rule-two , rule-three -- comment + * ^^^^^^^^^^^ + * + * Similarly, if this is the last element, we've already matched the range we want to + * remove. The previous rule name will end where the last one was ending, relative + * to the content on the right side. + * + * // eslint-disable-line rule-one , rule-two , rule-three -- comment + * ^^^^^^^^^^^^^ + */ + removalStartOffset = matchStartOffset; + removalEndOffset = matchEndOffset; + } + + return { + description: `'${ruleId}'`, + fix: { + range: [ + commentValueStart + removalStartOffset, + commentValueStart + removalEndOffset + ], + text: "" + }, + unprocessedDirective: directive.unprocessedDirective + }; + }); } /** - * Creates a description of deleting an entire unused disable directive. + * Creates a description of deleting an entire unused disable comment. * @param {Directive[]} directives Unused directives to be removed. - * @param {Token} node The backing Comment token. - * @param {SourceCode} sourceCode The source code object for the file being linted. - * @returns {{ description, fix, unprocessedDirective }} Details for later creation of an output problem. + * @param {Token} commentToken The backing Comment token. + * @returns {{ description, fix, unprocessedDirective }} Details for later creation of an output Problem. */ -function createDirectiveRemoval(directives, node, sourceCode) { - const range = sourceCode.getRange(node); - const ruleIds = directives - .filter(directive => directive.ruleId) - .map(directive => `'${directive.ruleId}'`); - - return { - description: - ruleIds.length <= 2 - ? ruleIds.join(" or ") - : `${ruleIds.slice(0, ruleIds.length - 1).join(", ")}, or ${ruleIds.at(-1)}`, - fix: { - range, - text: " ", - }, - unprocessedDirective: directives[0].unprocessedDirective, - }; +function createCommentRemoval(directives, commentToken) { + const { range } = commentToken; + const ruleIds = directives.filter(directive => directive.ruleId).map(directive => `'${directive.ruleId}'`); + + return { + description: ruleIds.length <= 2 + ? ruleIds.join(" or ") + : `${ruleIds.slice(0, ruleIds.length - 1).join(", ")}, or ${ruleIds[ruleIds.length - 1]}`, + fix: { + range, + text: " " + }, + unprocessedDirective: directives[0].unprocessedDirective + }; } /** * Parses details from directives to create output Problems. * @param {Iterable} allDirectives Unused directives to be removed. - * @param {SourceCode} sourceCode The source code object for the file being linted. * @returns {{ description, fix, unprocessedDirective }[]} Details for later creation of output Problems. */ -function processUnusedDirectives(allDirectives, sourceCode) { - const directiveGroups = groupByParentDirective(allDirectives); - - return directiveGroups.flatMap(directives => { - const { parentDirective } = directives[0].unprocessedDirective; - const remainingRuleIds = new Set(parentDirective.ruleIds); - - for (const directive of directives) { - remainingRuleIds.delete(directive.ruleId); - } - - return remainingRuleIds.size - ? createIndividualDirectivesRemoval( - directives, - parentDirective, - sourceCode, - ) - : [ - createDirectiveRemoval( - directives, - parentDirective.node, - sourceCode, - ), - ]; - }); +function processUnusedDirectives(allDirectives) { + const directiveGroups = groupByParentComment(allDirectives); + + return directiveGroups.flatMap( + directives => { + const { parentComment } = directives[0].unprocessedDirective; + const remainingRuleIds = new Set(parentComment.ruleIds); + + for (const directive of directives) { + remainingRuleIds.delete(directive.ruleId); + } + + return remainingRuleIds.size + ? createIndividualDirectivesRemoval(directives, parentComment.commentToken) + : [createCommentRemoval(directives, parentComment.commentToken)]; + } + ); } /** @@ -219,83 +205,87 @@ function processUnusedDirectives(allDirectives, sourceCode) { * @returns {Set} The used eslint-enable comments */ function collectUsedEnableDirectives(directives) { - /** - * A Map of `eslint-enable` keyed by ruleIds that may be marked as used. - * If `eslint-enable` does not have a ruleId, the key will be `null`. - * @type {Map} - */ - const enabledRules = new Map(); - - /** - * A Set of `eslint-enable` marked as used. - * It is also the return value of `collectUsedEnableDirectives` function. - * @type {Set} - */ - const usedEnableDirectives = new Set(); - - /* - * Checks the directives backwards to see if the encountered `eslint-enable` is used by the previous `eslint-disable`, - * and if so, stores the `eslint-enable` in `usedEnableDirectives`. - */ - for (let index = directives.length - 1; index >= 0; index--) { - const directive = directives[index]; - - if (directive.type === "disable") { - if (enabledRules.size === 0) { - continue; - } - if (directive.ruleId === null) { - // If encounter `eslint-disable` without ruleId, - // mark all `eslint-enable` currently held in enabledRules as used. - // e.g. - // /* eslint-disable */ <- current directive - // /* eslint-enable rule-id1 */ <- used - // /* eslint-enable rule-id2 */ <- used - // /* eslint-enable */ <- used - for (const enableDirective of enabledRules.values()) { - usedEnableDirectives.add(enableDirective); - } - enabledRules.clear(); - } else { - const enableDirective = enabledRules.get(directive.ruleId); - - if (enableDirective) { - // If encounter `eslint-disable` with ruleId, and there is an `eslint-enable` with the same ruleId in enabledRules, - // mark `eslint-enable` with ruleId as used. - // e.g. - // /* eslint-disable rule-id */ <- current directive - // /* eslint-enable rule-id */ <- used - usedEnableDirectives.add(enableDirective); - } else { - const enabledDirectiveWithoutRuleId = - enabledRules.get(null); - - if (enabledDirectiveWithoutRuleId) { - // If encounter `eslint-disable` with ruleId, and there is no `eslint-enable` with the same ruleId in enabledRules, - // mark `eslint-enable` without ruleId as used. - // e.g. - // /* eslint-disable rule-id */ <- current directive - // /* eslint-enable */ <- used - usedEnableDirectives.add(enabledDirectiveWithoutRuleId); - } - } - } - } else if (directive.type === "enable") { - if (directive.ruleId === null) { - // If encounter `eslint-enable` without ruleId, the `eslint-enable` that follows it are unused. - // So clear enabledRules. - // e.g. - // /* eslint-enable */ <- current directive - // /* eslint-enable rule-id *// <- unused - // /* eslint-enable */ <- unused - enabledRules.clear(); - enabledRules.set(null, directive); - } else { - enabledRules.set(directive.ruleId, directive); - } - } - } - return usedEnableDirectives; + + /** + * A Map of `eslint-enable` keyed by ruleIds that may be marked as used. + * If `eslint-enable` does not have a ruleId, the key will be `null`. + * @type {Map} + */ + const enabledRules = new Map(); + + /** + * A Set of `eslint-enable` marked as used. + * It is also the return value of `collectUsedEnableDirectives` function. + * @type {Set} + */ + const usedEnableDirectives = new Set(); + + /* + * Checks the directives backwards to see if the encountered `eslint-enable` is used by the previous `eslint-disable`, + * and if so, stores the `eslint-enable` in `usedEnableDirectives`. + */ + for (let index = directives.length - 1; index >= 0; index--) { + const directive = directives[index]; + + if (directive.type === "disable") { + if (enabledRules.size === 0) { + continue; + } + if (directive.ruleId === null) { + + // If encounter `eslint-disable` without ruleId, + // mark all `eslint-enable` currently held in enabledRules as used. + // e.g. + // /* eslint-disable */ <- current directive + // /* eslint-enable rule-id1 */ <- used + // /* eslint-enable rule-id2 */ <- used + // /* eslint-enable */ <- used + for (const enableDirective of enabledRules.values()) { + usedEnableDirectives.add(enableDirective); + } + enabledRules.clear(); + } else { + const enableDirective = enabledRules.get(directive.ruleId); + + if (enableDirective) { + + // If encounter `eslint-disable` with ruleId, and there is an `eslint-enable` with the same ruleId in enabledRules, + // mark `eslint-enable` with ruleId as used. + // e.g. + // /* eslint-disable rule-id */ <- current directive + // /* eslint-enable rule-id */ <- used + usedEnableDirectives.add(enableDirective); + } else { + const enabledDirectiveWithoutRuleId = enabledRules.get(null); + + if (enabledDirectiveWithoutRuleId) { + + // If encounter `eslint-disable` with ruleId, and there is no `eslint-enable` with the same ruleId in enabledRules, + // mark `eslint-enable` without ruleId as used. + // e.g. + // /* eslint-disable rule-id */ <- current directive + // /* eslint-enable */ <- used + usedEnableDirectives.add(enabledDirectiveWithoutRuleId); + } + } + } + } else if (directive.type === "enable") { + if (directive.ruleId === null) { + + // If encounter `eslint-enable` without ruleId, the `eslint-enable` that follows it are unused. + // So clear enabledRules. + // e.g. + // /* eslint-enable */ <- current directive + // /* eslint-enable rule-id *// <- unused + // /* eslint-enable */ <- unused + enabledRules.clear(); + enabledRules.set(null, directive); + } else { + enabledRules.set(directive.ruleId, directive); + } + } + } + return usedEnableDirectives; } /** @@ -309,140 +299,105 @@ function collectUsedEnableDirectives(directives) { * of problems (including suppressed ones) and unused eslint-disable directives */ function applyDirectives(options) { - const problems = []; - const usedDisableDirectives = new Set(); - const { sourceCode } = options; - - for (const problem of options.problems) { - let disableDirectivesForProblem = []; - let nextDirectiveIndex = 0; - - while ( - nextDirectiveIndex < options.directives.length && - compareLocations(options.directives[nextDirectiveIndex], problem) <= - 0 - ) { - const directive = options.directives[nextDirectiveIndex++]; - - if ( - directive.ruleId === null || - directive.ruleId === problem.ruleId - ) { - switch (directive.type) { - case "disable": - disableDirectivesForProblem.push(directive); - break; - - case "enable": - disableDirectivesForProblem = []; - break; - - // no default - } - } - } - - if (disableDirectivesForProblem.length > 0) { - const suppressions = disableDirectivesForProblem.map(directive => ({ - kind: "directive", - justification: directive.unprocessedDirective.justification, - })); - - if (problem.suppressions) { - problem.suppressions = - problem.suppressions.concat(suppressions); - } else { - problem.suppressions = suppressions; - usedDisableDirectives.add(disableDirectivesForProblem.at(-1)); - } - } - - problems.push(problem); - } - - const unusedDisableDirectivesToReport = options.directives.filter( - directive => - directive.type === "disable" && - !usedDisableDirectives.has(directive) && - !options.rulesToIgnore.has(directive.ruleId), - ); - - const unusedEnableDirectivesToReport = new Set( - options.directives.filter( - directive => - directive.unprocessedDirective.type === "enable" && - !options.rulesToIgnore.has(directive.ruleId), - ), - ); - - /* - * If directives has the eslint-enable directive, - * check whether the eslint-enable comment is used. - */ - if (unusedEnableDirectivesToReport.size > 0) { - for (const directive of collectUsedEnableDirectives( - options.directives, - )) { - unusedEnableDirectivesToReport.delete(directive); - } - } - - const processed = processUnusedDirectives( - unusedDisableDirectivesToReport, - sourceCode, - ).concat( - processUnusedDirectives(unusedEnableDirectivesToReport, sourceCode), - ); - const columnOffset = options.language.columnStart === 1 ? 0 : 1; - const lineOffset = options.language.lineStart === 1 ? 0 : 1; - - const unusedDirectives = processed.map( - ({ description, fix, unprocessedDirective }) => { - const { parentDirective, type, line, column } = - unprocessedDirective; - - let message; - - if (type === "enable") { - message = description - ? `Unused eslint-enable directive (no matching eslint-disable directives were found for ${description}).` - : "Unused eslint-enable directive (no matching eslint-disable directives were found)."; - } else { - message = description - ? `Unused eslint-disable directive (no problems were reported from ${description}).` - : "Unused eslint-disable directive (no problems were reported)."; - } - - const loc = sourceCode.getLoc(parentDirective.node); - - return { - ruleId: null, - message, - line: - type === "disable-next-line" - ? loc.start.line + lineOffset - : line, - column: - type === "disable-next-line" - ? loc.start.column + columnOffset - : column, - severity: - options.reportUnusedDisableDirectives === "warn" ? 1 : 2, - nodeType: null, - ...(options.disableFixes ? {} : { fix }), - }; - }, - ); - - return { problems, unusedDirectives }; + const problems = []; + const usedDisableDirectives = new Set(); + + for (const problem of options.problems) { + let disableDirectivesForProblem = []; + let nextDirectiveIndex = 0; + + while ( + nextDirectiveIndex < options.directives.length && + compareLocations(options.directives[nextDirectiveIndex], problem) <= 0 + ) { + const directive = options.directives[nextDirectiveIndex++]; + + if (directive.ruleId === null || directive.ruleId === problem.ruleId) { + switch (directive.type) { + case "disable": + disableDirectivesForProblem.push(directive); + break; + + case "enable": + disableDirectivesForProblem = []; + break; + + // no default + } + } + } + + if (disableDirectivesForProblem.length > 0) { + const suppressions = disableDirectivesForProblem.map(directive => ({ + kind: "directive", + justification: directive.unprocessedDirective.justification + })); + + if (problem.suppressions) { + problem.suppressions = problem.suppressions.concat(suppressions); + } else { + problem.suppressions = suppressions; + usedDisableDirectives.add(disableDirectivesForProblem[disableDirectivesForProblem.length - 1]); + } + } + + problems.push(problem); + } + + const unusedDisableDirectivesToReport = options.directives + .filter(directive => directive.type === "disable" && !usedDisableDirectives.has(directive)); + + + const unusedEnableDirectivesToReport = new Set( + options.directives.filter(directive => directive.unprocessedDirective.type === "enable") + ); + + /* + * If directives has the eslint-enable directive, + * check whether the eslint-enable comment is used. + */ + if (unusedEnableDirectivesToReport.size > 0) { + for (const directive of collectUsedEnableDirectives(options.directives)) { + unusedEnableDirectivesToReport.delete(directive); + } + } + + const processed = processUnusedDirectives(unusedDisableDirectivesToReport) + .concat(processUnusedDirectives(unusedEnableDirectivesToReport)); + + const unusedDirectives = processed + .map(({ description, fix, unprocessedDirective }) => { + const { parentComment, type, line, column } = unprocessedDirective; + + let message; + + if (type === "enable") { + message = description + ? `Unused eslint-enable directive (no matching eslint-disable directives were found for ${description}).` + : "Unused eslint-enable directive (no matching eslint-disable directives were found)."; + } else { + message = description + ? `Unused eslint-disable directive (no problems were reported from ${description}).` + : "Unused eslint-disable directive (no problems were reported)."; + } + return { + ruleId: null, + message, + line: type === "disable-next-line" ? parentComment.commentToken.loc.start.line : line, + column: type === "disable-next-line" ? parentComment.commentToken.loc.start.column + 1 : column, + severity: options.reportUnusedDisableDirectives === "warn" ? 1 : 2, + nodeType: null, + ...options.disableFixes ? {} : { fix } + }; + }); + + return { problems, unusedDirectives }; } /** * Given a list of directive comments (i.e. metadata about eslint-disable and eslint-enable comments) and a list * of reported problems, adds the suppression information to the problems. * @param {Object} options Information about directives and problems - * @param {Language} options.language The language being linted. - * @param {SourceCode} options.sourceCode The source code object for the file being linted. * @param {{ * type: ("disable"|"enable"|"disable-line"|"disable-next-line"), * ruleId: (string|null), @@ -455,130 +410,56 @@ function applyDirectives(options) { * @param {{ruleId: (string|null), line: number, column: number}[]} options.problems * A list of problems reported by rules, sorted by increasing location in the file, with one-based columns. * @param {"off" | "warn" | "error"} options.reportUnusedDisableDirectives If `"warn"` or `"error"`, adds additional problems for unused directives - * @param {RulesConfig} options.configuredRules The rules configuration. - * @param {Function} options.ruleFilter A predicate function to filter which rules should be executed. * @param {boolean} options.disableFixes If true, it doesn't make `fix` properties. * @returns {{ruleId: (string|null), line: number, column: number, suppressions?: {kind: string, justification: string}}[]} * An object with a list of reported problems, the suppressed of which contain the suppression information. */ -module.exports = ({ - language, - sourceCode, - directives, - disableFixes, - problems, - configuredRules, - ruleFilter, - reportUnusedDisableDirectives = "off", -}) => { - const blockDirectives = directives - .filter( - directive => - directive.type === "disable" || directive.type === "enable", - ) - .map(directive => - Object.assign({}, directive, { unprocessedDirective: directive }), - ) - .sort(compareLocations); - - const lineDirectives = directives - .flatMap(directive => { - switch (directive.type) { - case "disable": - case "enable": - return []; - - case "disable-line": - return [ - { - type: "disable", - line: directive.line, - column: 1, - ruleId: directive.ruleId, - unprocessedDirective: directive, - }, - { - type: "enable", - line: directive.line + 1, - column: 0, - ruleId: directive.ruleId, - unprocessedDirective: directive, - }, - ]; - - case "disable-next-line": - return [ - { - type: "disable", - line: directive.line + 1, - column: 1, - ruleId: directive.ruleId, - unprocessedDirective: directive, - }, - { - type: "enable", - line: directive.line + 2, - column: 0, - ruleId: directive.ruleId, - unprocessedDirective: directive, - }, - ]; - - default: - throw new TypeError( - `Unrecognized directive type '${directive.type}'`, - ); - } - }) - .sort(compareLocations); - - // This determines a list of rules that are not being run by the given ruleFilter, if present. - const rulesToIgnore = - configuredRules && ruleFilter - ? new Set( - Object.keys(configuredRules).filter(ruleId => { - const severity = Config.getRuleNumericSeverity( - configuredRules[ruleId], - ); - - // Ignore for disabled rules. - if (severity === 0) { - return false; - } - - return !ruleFilter({ severity, ruleId }); - }), - ) - : new Set(); - - // If no ruleId is supplied that means this directive is applied to all rules, so we can't determine if it's unused if any rules are filtered out. - if (rulesToIgnore.size > 0) { - rulesToIgnore.add(null); - } - - const blockDirectivesResult = applyDirectives({ - language, - sourceCode, - problems, - directives: blockDirectives, - disableFixes, - reportUnusedDisableDirectives, - rulesToIgnore, - }); - const lineDirectivesResult = applyDirectives({ - language, - sourceCode, - problems: blockDirectivesResult.problems, - directives: lineDirectives, - disableFixes, - reportUnusedDisableDirectives, - rulesToIgnore, - }); - - return reportUnusedDisableDirectives !== "off" - ? lineDirectivesResult.problems - .concat(blockDirectivesResult.unusedDirectives) - .concat(lineDirectivesResult.unusedDirectives) - .sort(compareLocations) - : lineDirectivesResult.problems; +module.exports = ({ directives, disableFixes, problems, reportUnusedDisableDirectives = "off" }) => { + const blockDirectives = directives + .filter(directive => directive.type === "disable" || directive.type === "enable") + .map(directive => Object.assign({}, directive, { unprocessedDirective: directive })) + .sort(compareLocations); + + const lineDirectives = directives.flatMap(directive => { + switch (directive.type) { + case "disable": + case "enable": + return []; + + case "disable-line": + return [ + { type: "disable", line: directive.line, column: 1, ruleId: directive.ruleId, unprocessedDirective: directive }, + { type: "enable", line: directive.line + 1, column: 0, ruleId: directive.ruleId, unprocessedDirective: directive } + ]; + + case "disable-next-line": + return [ + { type: "disable", line: directive.line + 1, column: 1, ruleId: directive.ruleId, unprocessedDirective: directive }, + { type: "enable", line: directive.line + 2, column: 0, ruleId: directive.ruleId, unprocessedDirective: directive } + ]; + + default: + throw new TypeError(`Unrecognized directive type '${directive.type}'`); + } + }).sort(compareLocations); + + const blockDirectivesResult = applyDirectives({ + problems, + directives: blockDirectives, + disableFixes, + reportUnusedDisableDirectives + }); + const lineDirectivesResult = applyDirectives({ + problems: blockDirectivesResult.problems, + directives: lineDirectives, + disableFixes, + reportUnusedDisableDirectives + }); + + return reportUnusedDisableDirectives !== "off" + ? lineDirectivesResult.problems + .concat(blockDirectivesResult.unusedDirectives) + .concat(lineDirectivesResult.unusedDirectives) + .sort(compareLocations) + : lineDirectivesResult.problems; }; diff --git a/node_modules/eslint/lib/linter/code-path-analysis/code-path-analyzer.js b/node_modules/eslint/lib/linter/code-path-analysis/code-path-analyzer.js index fc728d10d..b60e55c16 100644 --- a/node_modules/eslint/lib/linter/code-path-analysis/code-path-analyzer.js +++ b/node_modules/eslint/lib/linter/code-path-analysis/code-path-analyzer.js @@ -9,12 +9,12 @@ // Requirements //------------------------------------------------------------------------------ -const assert = require("../../shared/assert"), - { breakableTypePattern } = require("../../shared/ast-utils"), - CodePath = require("./code-path"), - CodePathSegment = require("./code-path-segment"), - IdGenerator = require("./id-generator"), - debug = require("./debug-helpers"); +const assert = require("assert"), + { breakableTypePattern } = require("../../shared/ast-utils"), + CodePath = require("./code-path"), + CodePathSegment = require("./code-path-segment"), + IdGenerator = require("./id-generator"), + debug = require("./debug-helpers"); //------------------------------------------------------------------------------ // Helpers @@ -26,7 +26,7 @@ const assert = require("../../shared/assert"), * @returns {boolean} `true` if the node is a `case` node (not `default` node). */ function isCaseNode(node) { - return Boolean(node.test); + return Boolean(node.test); } /** @@ -36,11 +36,9 @@ function isCaseNode(node) { * false if not. */ function isPropertyDefinitionValue(node) { - const parent = node.parent; + const parent = node.parent; - return ( - parent && parent.type === "PropertyDefinition" && parent.value === node - ); + return parent && parent.type === "PropertyDefinition" && parent.value === node; } /** @@ -50,7 +48,7 @@ function isPropertyDefinitionValue(node) { * @returns {boolean} `true` if the operator is "&&" or "||" or "??" */ function isHandledLogicalOperator(operator) { - return operator === "&&" || operator === "||" || operator === "??"; + return operator === "&&" || operator === "||" || operator === "??"; } /** @@ -61,7 +59,7 @@ function isHandledLogicalOperator(operator) { * @returns {boolean} `true` if the operator is "&&=" or "||=" or "??=" */ function isLogicalAssignmentOperator(operator) { - return operator === "&&=" || operator === "||=" || operator === "??="; + return operator === "&&=" || operator === "||=" || operator === "??="; } /** @@ -70,10 +68,10 @@ function isLogicalAssignmentOperator(operator) { * @returns {string|null} The label or `null`. */ function getLabel(node) { - if (node.parent.type === "LabeledStatement") { - return node.parent.label.name; - } - return null; + if (node.parent.type === "LabeledStatement") { + return node.parent.label.name; + } + return null; } /** @@ -83,25 +81,25 @@ function getLabel(node) { * @returns {boolean} `true` if the node is a test of a choice statement. */ function isForkingByTrueOrFalse(node) { - const parent = node.parent; + const parent = node.parent; - switch (parent.type) { - case "ConditionalExpression": - case "IfStatement": - case "WhileStatement": - case "DoWhileStatement": - case "ForStatement": - return parent.test === node; + switch (parent.type) { + case "ConditionalExpression": + case "IfStatement": + case "WhileStatement": + case "DoWhileStatement": + case "ForStatement": + return parent.test === node; - case "LogicalExpression": - return isHandledLogicalOperator(parent.operator); + case "LogicalExpression": + return isHandledLogicalOperator(parent.operator); - case "AssignmentExpression": - return isLogicalAssignmentOperator(parent.operator); + case "AssignmentExpression": + return isLogicalAssignmentOperator(parent.operator); - default: - return false; - } + default: + return false; + } } /** @@ -115,10 +113,10 @@ function isForkingByTrueOrFalse(node) { * otherwise `undefined`. */ function getBooleanValueIfSimpleConstant(node) { - if (node.type === "Literal") { - return Boolean(node.value); - } - return void 0; + if (node.type === "Literal") { + return Boolean(node.value); + } + return void 0; } /** @@ -129,39 +127,43 @@ function getBooleanValueIfSimpleConstant(node) { * @returns {boolean} `true` if the node is a reference. */ function isIdentifierReference(node) { - const parent = node.parent; - - switch (parent.type) { - case "LabeledStatement": - case "BreakStatement": - case "ContinueStatement": - case "ArrayPattern": - case "RestElement": - case "ImportSpecifier": - case "ImportDefaultSpecifier": - case "ImportNamespaceSpecifier": - case "CatchClause": - return false; - - case "FunctionDeclaration": - case "FunctionExpression": - case "ArrowFunctionExpression": - case "ClassDeclaration": - case "ClassExpression": - case "VariableDeclarator": - return parent.id !== node; - - case "Property": - case "PropertyDefinition": - case "MethodDefinition": - return parent.key !== node || parent.computed || parent.shorthand; - - case "AssignmentPattern": - return parent.key !== node; - - default: - return true; - } + const parent = node.parent; + + switch (parent.type) { + case "LabeledStatement": + case "BreakStatement": + case "ContinueStatement": + case "ArrayPattern": + case "RestElement": + case "ImportSpecifier": + case "ImportDefaultSpecifier": + case "ImportNamespaceSpecifier": + case "CatchClause": + return false; + + case "FunctionDeclaration": + case "FunctionExpression": + case "ArrowFunctionExpression": + case "ClassDeclaration": + case "ClassExpression": + case "VariableDeclarator": + return parent.id !== node; + + case "Property": + case "PropertyDefinition": + case "MethodDefinition": + return ( + parent.key !== node || + parent.computed || + parent.shorthand + ); + + case "AssignmentPattern": + return parent.key !== node; + + default: + return true; + } } /** @@ -177,47 +179,59 @@ function isIdentifierReference(node) { * @returns {void} */ function forwardCurrentToHead(analyzer, node) { - const codePath = analyzer.codePath; - const state = CodePath.getState(codePath); - const currentSegments = state.currentSegments; - const headSegments = state.headSegments; - const end = Math.max(currentSegments.length, headSegments.length); - let i, currentSegment, headSegment; - - // Fires leaving events. - for (i = 0; i < end; ++i) { - currentSegment = currentSegments[i]; - headSegment = headSegments[i]; - - if (currentSegment !== headSegment && currentSegment) { - const eventName = currentSegment.reachable - ? "onCodePathSegmentEnd" - : "onUnreachableCodePathSegmentEnd"; - - debug.dump(`${eventName} ${currentSegment.id}`); - - analyzer.emit(eventName, [currentSegment, node]); - } - } - - // Update state. - state.currentSegments = headSegments; - - // Fires entering events. - for (i = 0; i < end; ++i) { - currentSegment = currentSegments[i]; - headSegment = headSegments[i]; - - if (currentSegment !== headSegment && headSegment) { - const eventName = headSegment.reachable - ? "onCodePathSegmentStart" - : "onUnreachableCodePathSegmentStart"; - - debug.dump(`${eventName} ${headSegment.id}`); - CodePathSegment.markUsed(headSegment); - analyzer.emit(eventName, [headSegment, node]); - } - } + const codePath = analyzer.codePath; + const state = CodePath.getState(codePath); + const currentSegments = state.currentSegments; + const headSegments = state.headSegments; + const end = Math.max(currentSegments.length, headSegments.length); + let i, currentSegment, headSegment; + + // Fires leaving events. + for (i = 0; i < end; ++i) { + currentSegment = currentSegments[i]; + headSegment = headSegments[i]; + + if (currentSegment !== headSegment && currentSegment) { + + const eventName = currentSegment.reachable + ? "onCodePathSegmentEnd" + : "onUnreachableCodePathSegmentEnd"; + + debug.dump(`${eventName} ${currentSegment.id}`); + + analyzer.emitter.emit( + eventName, + currentSegment, + node + ); + } + } + + // Update state. + state.currentSegments = headSegments; + + // Fires entering events. + for (i = 0; i < end; ++i) { + currentSegment = currentSegments[i]; + headSegment = headSegments[i]; + + if (currentSegment !== headSegment && headSegment) { + + const eventName = headSegment.reachable + ? "onCodePathSegmentStart" + : "onUnreachableCodePathSegmentStart"; + + debug.dump(`${eventName} ${headSegment.id}`); + + CodePathSegment.markUsed(headSegment); + analyzer.emitter.emit( + eventName, + headSegment, + node + ); + } + } + } /** @@ -228,21 +242,25 @@ function forwardCurrentToHead(analyzer, node) { * @returns {void} */ function leaveFromCurrentSegment(analyzer, node) { - const state = CodePath.getState(analyzer.codePath); - const currentSegments = state.currentSegments; + const state = CodePath.getState(analyzer.codePath); + const currentSegments = state.currentSegments; - for (let i = 0; i < currentSegments.length; ++i) { - const currentSegment = currentSegments[i]; - const eventName = currentSegment.reachable - ? "onCodePathSegmentEnd" - : "onUnreachableCodePathSegmentEnd"; + for (let i = 0; i < currentSegments.length; ++i) { + const currentSegment = currentSegments[i]; + const eventName = currentSegment.reachable + ? "onCodePathSegmentEnd" + : "onUnreachableCodePathSegmentEnd"; - debug.dump(`${eventName} ${currentSegment.id}`); + debug.dump(`${eventName} ${currentSegment.id}`); - analyzer.emit(eventName, [currentSegment, node]); - } + analyzer.emitter.emit( + eventName, + currentSegment, + node + ); + } - state.currentSegments = []; + state.currentSegments = []; } /** @@ -256,129 +274,128 @@ function leaveFromCurrentSegment(analyzer, node) { * @returns {void} */ function preprocess(analyzer, node) { - const codePath = analyzer.codePath; - const state = CodePath.getState(codePath); - const parent = node.parent; - - switch (parent.type) { - // The `arguments.length == 0` case is in `postprocess` function. - case "CallExpression": - if ( - parent.optional === true && - parent.arguments.length >= 1 && - parent.arguments[0] === node - ) { - state.makeOptionalRight(); - } - break; - case "MemberExpression": - if (parent.optional === true && parent.property === node) { - state.makeOptionalRight(); - } - break; - - case "LogicalExpression": - if ( - parent.right === node && - isHandledLogicalOperator(parent.operator) - ) { - state.makeLogicalRight(); - } - break; - - case "AssignmentExpression": - if ( - parent.right === node && - isLogicalAssignmentOperator(parent.operator) - ) { - state.makeLogicalRight(); - } - break; - - case "ConditionalExpression": - case "IfStatement": - /* - * Fork if this node is at `consequent`/`alternate`. - * `popForkContext()` exists at `IfStatement:exit` and - * `ConditionalExpression:exit`. - */ - if (parent.consequent === node) { - state.makeIfConsequent(); - } else if (parent.alternate === node) { - state.makeIfAlternate(); - } - break; - - case "SwitchCase": - if (parent.consequent[0] === node) { - state.makeSwitchCaseBody(false, !parent.test); - } - break; - - case "TryStatement": - if (parent.handler === node) { - state.makeCatchBlock(); - } else if (parent.finalizer === node) { - state.makeFinallyBlock(); - } - break; - - case "WhileStatement": - if (parent.test === node) { - state.makeWhileTest(getBooleanValueIfSimpleConstant(node)); - } else { - assert(parent.body === node); - state.makeWhileBody(); - } - break; - - case "DoWhileStatement": - if (parent.body === node) { - state.makeDoWhileBody(); - } else { - assert(parent.test === node); - state.makeDoWhileTest(getBooleanValueIfSimpleConstant(node)); - } - break; - - case "ForStatement": - if (parent.test === node) { - state.makeForTest(getBooleanValueIfSimpleConstant(node)); - } else if (parent.update === node) { - state.makeForUpdate(); - } else if (parent.body === node) { - state.makeForBody(); - } - break; - - case "ForInStatement": - case "ForOfStatement": - if (parent.left === node) { - state.makeForInOfLeft(); - } else if (parent.right === node) { - state.makeForInOfRight(); - } else { - assert(parent.body === node); - state.makeForInOfBody(); - } - break; - - case "AssignmentPattern": - /* - * Fork if this node is at `right`. - * `left` is executed always, so it uses the current path. - * `popForkContext()` exists at `AssignmentPattern:exit`. - */ - if (parent.right === node) { - state.pushForkContext(); - state.forkBypassPath(); - state.forkPath(); - } - break; - - default: - break; - } + const codePath = analyzer.codePath; + const state = CodePath.getState(codePath); + const parent = node.parent; + + switch (parent.type) { + + // The `arguments.length == 0` case is in `postprocess` function. + case "CallExpression": + if (parent.optional === true && parent.arguments.length >= 1 && parent.arguments[0] === node) { + state.makeOptionalRight(); + } + break; + case "MemberExpression": + if (parent.optional === true && parent.property === node) { + state.makeOptionalRight(); + } + break; + + case "LogicalExpression": + if ( + parent.right === node && + isHandledLogicalOperator(parent.operator) + ) { + state.makeLogicalRight(); + } + break; + + case "AssignmentExpression": + if ( + parent.right === node && + isLogicalAssignmentOperator(parent.operator) + ) { + state.makeLogicalRight(); + } + break; + + case "ConditionalExpression": + case "IfStatement": + + /* + * Fork if this node is at `consequent`/`alternate`. + * `popForkContext()` exists at `IfStatement:exit` and + * `ConditionalExpression:exit`. + */ + if (parent.consequent === node) { + state.makeIfConsequent(); + } else if (parent.alternate === node) { + state.makeIfAlternate(); + } + break; + + case "SwitchCase": + if (parent.consequent[0] === node) { + state.makeSwitchCaseBody(false, !parent.test); + } + break; + + case "TryStatement": + if (parent.handler === node) { + state.makeCatchBlock(); + } else if (parent.finalizer === node) { + state.makeFinallyBlock(); + } + break; + + case "WhileStatement": + if (parent.test === node) { + state.makeWhileTest(getBooleanValueIfSimpleConstant(node)); + } else { + assert(parent.body === node); + state.makeWhileBody(); + } + break; + + case "DoWhileStatement": + if (parent.body === node) { + state.makeDoWhileBody(); + } else { + assert(parent.test === node); + state.makeDoWhileTest(getBooleanValueIfSimpleConstant(node)); + } + break; + + case "ForStatement": + if (parent.test === node) { + state.makeForTest(getBooleanValueIfSimpleConstant(node)); + } else if (parent.update === node) { + state.makeForUpdate(); + } else if (parent.body === node) { + state.makeForBody(); + } + break; + + case "ForInStatement": + case "ForOfStatement": + if (parent.left === node) { + state.makeForInOfLeft(); + } else if (parent.right === node) { + state.makeForInOfRight(); + } else { + assert(parent.body === node); + state.makeForInOfBody(); + } + break; + + case "AssignmentPattern": + + /* + * Fork if this node is at `right`. + * `left` is executed always, so it uses the current path. + * `popForkContext()` exists at `AssignmentPattern:exit`. + */ + if (parent.right === node) { + state.pushForkContext(); + state.forkBypassPath(); + state.forkPath(); + } + break; + + default: + break; + } } /** @@ -388,152 +405,155 @@ function preprocess(analyzer, node) { * @returns {void} */ function processCodePathToEnter(analyzer, node) { - let codePath = analyzer.codePath; - let state = codePath && CodePath.getState(codePath); - const parent = node.parent; - - /** - * Creates a new code path and trigger the onCodePathStart event - * based on the currently selected node. - * @param {string} origin The reason the code path was started. - * @returns {void} - */ - function startCodePath(origin) { - if (codePath) { - // Emits onCodePathSegmentStart events if updated. - forwardCurrentToHead(analyzer, node); - debug.dumpState(node, state, false); - } - - // Create the code path of this scope. - codePath = analyzer.codePath = new CodePath({ - id: analyzer.idGenerator.next(), - origin, - upper: codePath, - onLooped: analyzer.onLooped, - }); - state = CodePath.getState(codePath); - - // Emits onCodePathStart events. - debug.dump(`onCodePathStart ${codePath.id}`); - analyzer.emit("onCodePathStart", [codePath, node]); - } - - /* - * Special case: The right side of class field initializer is considered - * to be its own function, so we need to start a new code path in this - * case. - */ - if (isPropertyDefinitionValue(node)) { - startCodePath("class-field-initializer"); - - /* - * Intentional fall through because `node` needs to also be - * processed by the code below. For example, if we have: - * - * class Foo { - * a = () => {} - * } - * - * In this case, we also need start a second code path. - */ - } - - switch (node.type) { - case "Program": - startCodePath("program"); - break; - - case "FunctionDeclaration": - case "FunctionExpression": - case "ArrowFunctionExpression": - startCodePath("function"); - break; - - case "StaticBlock": - startCodePath("class-static-block"); - break; - - case "ChainExpression": - state.pushChainContext(); - break; - case "CallExpression": - if (node.optional === true) { - state.makeOptionalNode(); - } - break; - case "MemberExpression": - if (node.optional === true) { - state.makeOptionalNode(); - } - break; - - case "LogicalExpression": - if (isHandledLogicalOperator(node.operator)) { - state.pushChoiceContext( - node.operator, - isForkingByTrueOrFalse(node), - ); - } - break; - - case "AssignmentExpression": - if (isLogicalAssignmentOperator(node.operator)) { - state.pushChoiceContext( - node.operator.slice(0, -1), // removes `=` from the end - isForkingByTrueOrFalse(node), - ); - } - break; - - case "ConditionalExpression": - case "IfStatement": - state.pushChoiceContext("test", false); - break; - - case "SwitchStatement": - state.pushSwitchContext( - node.cases.some(isCaseNode), - getLabel(node), - ); - break; - - case "TryStatement": - state.pushTryContext(Boolean(node.finalizer)); - break; - - case "SwitchCase": - /* - * Fork if this node is after the 2st node in `cases`. - * It's similar to `else` blocks. - * The next `test` node is processed in this path. - */ - if (parent.discriminant !== node && parent.cases[0] !== node) { - state.forkPath(); - } - break; - - case "WhileStatement": - case "DoWhileStatement": - case "ForStatement": - case "ForInStatement": - case "ForOfStatement": - state.pushLoopContext(node.type, getLabel(node)); - break; - - case "LabeledStatement": - if (!breakableTypePattern.test(node.body.type)) { - state.pushBreakContext(false, node.label.name); - } - break; - - default: - break; - } - - // Emits onCodePathSegmentStart events if updated. - forwardCurrentToHead(analyzer, node); - debug.dumpState(node, state, false); + let codePath = analyzer.codePath; + let state = codePath && CodePath.getState(codePath); + const parent = node.parent; + + /** + * Creates a new code path and trigger the onCodePathStart event + * based on the currently selected node. + * @param {string} origin The reason the code path was started. + * @returns {void} + */ + function startCodePath(origin) { + if (codePath) { + + // Emits onCodePathSegmentStart events if updated. + forwardCurrentToHead(analyzer, node); + debug.dumpState(node, state, false); + } + + // Create the code path of this scope. + codePath = analyzer.codePath = new CodePath({ + id: analyzer.idGenerator.next(), + origin, + upper: codePath, + onLooped: analyzer.onLooped + }); + state = CodePath.getState(codePath); + + // Emits onCodePathStart events. + debug.dump(`onCodePathStart ${codePath.id}`); + analyzer.emitter.emit("onCodePathStart", codePath, node); + } + + /* + * Special case: The right side of class field initializer is considered + * to be its own function, so we need to start a new code path in this + * case. + */ + if (isPropertyDefinitionValue(node)) { + startCodePath("class-field-initializer"); + + /* + * Intentional fall through because `node` needs to also be + * processed by the code below. For example, if we have: + * + * class Foo { + * a = () => {} + * } + * + * In this case, we also need start a second code path. + */ + + } + + switch (node.type) { + case "Program": + startCodePath("program"); + break; + + case "FunctionDeclaration": + case "FunctionExpression": + case "ArrowFunctionExpression": + startCodePath("function"); + break; + + case "StaticBlock": + startCodePath("class-static-block"); + break; + + case "ChainExpression": + state.pushChainContext(); + break; + case "CallExpression": + if (node.optional === true) { + state.makeOptionalNode(); + } + break; + case "MemberExpression": + if (node.optional === true) { + state.makeOptionalNode(); + } + break; + + case "LogicalExpression": + if (isHandledLogicalOperator(node.operator)) { + state.pushChoiceContext( + node.operator, + isForkingByTrueOrFalse(node) + ); + } + break; + + case "AssignmentExpression": + if (isLogicalAssignmentOperator(node.operator)) { + state.pushChoiceContext( + node.operator.slice(0, -1), // removes `=` from the end + isForkingByTrueOrFalse(node) + ); + } + break; + + case "ConditionalExpression": + case "IfStatement": + state.pushChoiceContext("test", false); + break; + + case "SwitchStatement": + state.pushSwitchContext( + node.cases.some(isCaseNode), + getLabel(node) + ); + break; + + case "TryStatement": + state.pushTryContext(Boolean(node.finalizer)); + break; + + case "SwitchCase": + + /* + * Fork if this node is after the 2st node in `cases`. + * It's similar to `else` blocks. + * The next `test` node is processed in this path. + */ + if (parent.discriminant !== node && parent.cases[0] !== node) { + state.forkPath(); + } + break; + + case "WhileStatement": + case "DoWhileStatement": + case "ForStatement": + case "ForInStatement": + case "ForOfStatement": + state.pushLoopContext(node.type, getLabel(node)); + break; + + case "LabeledStatement": + if (!breakableTypePattern.test(node.body.type)) { + state.pushBreakContext(false, node.label.name); + } + break; + + default: + break; + } + + // Emits onCodePathSegmentStart events if updated. + forwardCurrentToHead(analyzer, node); + debug.dumpState(node, state, false); } /** @@ -543,120 +563,122 @@ function processCodePathToEnter(analyzer, node) { * @returns {void} */ function processCodePathToExit(analyzer, node) { - const codePath = analyzer.codePath; - const state = CodePath.getState(codePath); - let dontForward = false; - - switch (node.type) { - case "ChainExpression": - state.popChainContext(); - break; - - case "IfStatement": - case "ConditionalExpression": - state.popChoiceContext(); - break; - - case "LogicalExpression": - if (isHandledLogicalOperator(node.operator)) { - state.popChoiceContext(); - } - break; - - case "AssignmentExpression": - if (isLogicalAssignmentOperator(node.operator)) { - state.popChoiceContext(); - } - break; - - case "SwitchStatement": - state.popSwitchContext(); - break; - - case "SwitchCase": - /* - * This is the same as the process at the 1st `consequent` node in - * `preprocess` function. - * Must do if this `consequent` is empty. - */ - if (node.consequent.length === 0) { - state.makeSwitchCaseBody(true, !node.test); - } - if (state.forkContext.reachable) { - dontForward = true; - } - break; - - case "TryStatement": - state.popTryContext(); - break; - - case "BreakStatement": - forwardCurrentToHead(analyzer, node); - state.makeBreak(node.label && node.label.name); - dontForward = true; - break; - - case "ContinueStatement": - forwardCurrentToHead(analyzer, node); - state.makeContinue(node.label && node.label.name); - dontForward = true; - break; - - case "ReturnStatement": - forwardCurrentToHead(analyzer, node); - state.makeReturn(); - dontForward = true; - break; - - case "ThrowStatement": - forwardCurrentToHead(analyzer, node); - state.makeThrow(); - dontForward = true; - break; - - case "Identifier": - if (isIdentifierReference(node)) { - state.makeFirstThrowablePathInTryBlock(); - dontForward = true; - } - break; - - case "CallExpression": - case "ImportExpression": - case "MemberExpression": - case "NewExpression": - case "YieldExpression": - state.makeFirstThrowablePathInTryBlock(); - break; - - case "WhileStatement": - case "DoWhileStatement": - case "ForStatement": - case "ForInStatement": - case "ForOfStatement": - state.popLoopContext(); - break; - - case "AssignmentPattern": - state.popForkContext(); - break; - - case "LabeledStatement": - if (!breakableTypePattern.test(node.body.type)) { - state.popBreakContext(); - } - break; - - default: - break; - } - - // Emits onCodePathSegmentStart events if updated. - if (!dontForward) { - forwardCurrentToHead(analyzer, node); - } - debug.dumpState(node, state, true); + + const codePath = analyzer.codePath; + const state = CodePath.getState(codePath); + let dontForward = false; + + switch (node.type) { + case "ChainExpression": + state.popChainContext(); + break; + + case "IfStatement": + case "ConditionalExpression": + state.popChoiceContext(); + break; + + case "LogicalExpression": + if (isHandledLogicalOperator(node.operator)) { + state.popChoiceContext(); + } + break; + + case "AssignmentExpression": + if (isLogicalAssignmentOperator(node.operator)) { + state.popChoiceContext(); + } + break; + + case "SwitchStatement": + state.popSwitchContext(); + break; + + case "SwitchCase": + + /* + * This is the same as the process at the 1st `consequent` node in + * `preprocess` function. + * Must do if this `consequent` is empty. + */ + if (node.consequent.length === 0) { + state.makeSwitchCaseBody(true, !node.test); + } + if (state.forkContext.reachable) { + dontForward = true; + } + break; + + case "TryStatement": + state.popTryContext(); + break; + + case "BreakStatement": + forwardCurrentToHead(analyzer, node); + state.makeBreak(node.label && node.label.name); + dontForward = true; + break; + + case "ContinueStatement": + forwardCurrentToHead(analyzer, node); + state.makeContinue(node.label && node.label.name); + dontForward = true; + break; + + case "ReturnStatement": + forwardCurrentToHead(analyzer, node); + state.makeReturn(); + dontForward = true; + break; + + case "ThrowStatement": + forwardCurrentToHead(analyzer, node); + state.makeThrow(); + dontForward = true; + break; + + case "Identifier": + if (isIdentifierReference(node)) { + state.makeFirstThrowablePathInTryBlock(); + dontForward = true; + } + break; + + case "CallExpression": + case "ImportExpression": + case "MemberExpression": + case "NewExpression": + case "YieldExpression": + state.makeFirstThrowablePathInTryBlock(); + break; + + case "WhileStatement": + case "DoWhileStatement": + case "ForStatement": + case "ForInStatement": + case "ForOfStatement": + state.popLoopContext(); + break; + + case "AssignmentPattern": + state.popForkContext(); + break; + + case "LabeledStatement": + if (!breakableTypePattern.test(node.body.type)) { + state.popBreakContext(); + } + break; + + default: + break; + } + + // Emits onCodePathSegmentStart events if updated. + if (!dontForward) { + forwardCurrentToHead(analyzer, node); + } + debug.dumpState(node, state, true); } /** @@ -666,71 +688,73 @@ function processCodePathToExit(analyzer, node) { * @returns {void} */ function postprocess(analyzer, node) { - /** - * Ends the code path for the current node. - * @returns {void} - */ - function endCodePath() { - let codePath = analyzer.codePath; - - // Mark the current path as the final node. - CodePath.getState(codePath).makeFinal(); - - // Emits onCodePathSegmentEnd event of the current segments. - leaveFromCurrentSegment(analyzer, node); - - // Emits onCodePathEnd event of this code path. - debug.dump(`onCodePathEnd ${codePath.id}`); - analyzer.emit("onCodePathEnd", [codePath, node]); - debug.dumpDot(codePath); - - codePath = analyzer.codePath = analyzer.codePath.upper; - if (codePath) { - debug.dumpState(node, CodePath.getState(codePath), true); - } - } - - switch (node.type) { - case "Program": - case "FunctionDeclaration": - case "FunctionExpression": - case "ArrowFunctionExpression": - case "StaticBlock": { - endCodePath(); - break; - } - - // The `arguments.length >= 1` case is in `preprocess` function. - case "CallExpression": - if (node.optional === true && node.arguments.length === 0) { - CodePath.getState(analyzer.codePath).makeOptionalRight(); - } - break; - - default: - break; - } - - /* - * Special case: The right side of class field initializer is considered - * to be its own function, so we need to end a code path in this - * case. - * - * We need to check after the other checks in order to close the - * code paths in the correct order for code like this: - * - * - * class Foo { - * a = () => {} - * } - * - * In this case, The ArrowFunctionExpression code path is closed first - * and then we need to close the code path for the PropertyDefinition - * value. - */ - if (isPropertyDefinitionValue(node)) { - endCodePath(); - } + + /** + * Ends the code path for the current node. + * @returns {void} + */ + function endCodePath() { + let codePath = analyzer.codePath; + + // Mark the current path as the final node. + CodePath.getState(codePath).makeFinal(); + + // Emits onCodePathSegmentEnd event of the current segments. + leaveFromCurrentSegment(analyzer, node); + + // Emits onCodePathEnd event of this code path. + debug.dump(`onCodePathEnd ${codePath.id}`); + analyzer.emitter.emit("onCodePathEnd", codePath, node); + debug.dumpDot(codePath); + + codePath = analyzer.codePath = analyzer.codePath.upper; + if (codePath) { + debug.dumpState(node, CodePath.getState(codePath), true); + } + + } + + switch (node.type) { + case "Program": + case "FunctionDeclaration": + case "FunctionExpression": + case "ArrowFunctionExpression": + case "StaticBlock": { + endCodePath(); + break; + } + + // The `arguments.length >= 1` case is in `preprocess` function. + case "CallExpression": + if (node.optional === true && node.arguments.length === 0) { + CodePath.getState(analyzer.codePath).makeOptionalRight(); + } + break; + + default: + break; + } + + /* + * Special case: The right side of class field initializer is considered + * to be its own function, so we need to end a code path in this + * case. + * + * We need to check after the other checks in order to close the + * code paths in the correct order for code like this: + * + * + * class Foo { + * a = () => {} + * } + * + * In this case, The ArrowFunctionExpression code path is closed first + * and then we need to close the code path for the PropertyDefinition + * value. + */ + if (isPropertyDefinitionValue(node)) { + endCodePath(); + } } //------------------------------------------------------------------------------ @@ -742,87 +766,87 @@ function postprocess(analyzer, node) { * This class implements the EventGenerator interface. */ class CodePathAnalyzer { - /** - * @param {EventGenerator} eventGenerator An event generator to wrap. - */ - constructor(eventGenerator) { - this.original = eventGenerator; - this.emit = eventGenerator.emit; - this.codePath = null; - this.idGenerator = new IdGenerator("s"); - this.currentNode = null; - this.onLooped = this.onLooped.bind(this); - } - - /** - * Does the process to enter a given AST node. - * This updates state of analysis and calls `enterNode` of the wrapped. - * @param {ASTNode} node A node which is entering. - * @returns {void} - */ - enterNode(node) { - this.currentNode = node; - - // Updates the code path due to node's position in its parent node. - if (node.parent) { - preprocess(this, node); - } - - /* - * Updates the code path. - * And emits onCodePathStart/onCodePathSegmentStart events. - */ - processCodePathToEnter(this, node); - - // Emits node events. - this.original.enterNode(node); - - this.currentNode = null; - } - - /** - * Does the process to leave a given AST node. - * This updates state of analysis and calls `leaveNode` of the wrapped. - * @param {ASTNode} node A node which is leaving. - * @returns {void} - */ - leaveNode(node) { - this.currentNode = node; - - /* - * Updates the code path. - * And emits onCodePathStart/onCodePathSegmentStart events. - */ - processCodePathToExit(this, node); - - // Emits node events. - this.original.leaveNode(node); - - // Emits the last onCodePathStart/onCodePathSegmentStart events. - postprocess(this, node); - - this.currentNode = null; - } - - /** - * This is called on a code path looped. - * Then this raises a looped event. - * @param {CodePathSegment} fromSegment A segment of prev. - * @param {CodePathSegment} toSegment A segment of next. - * @returns {void} - */ - onLooped(fromSegment, toSegment) { - if (fromSegment.reachable && toSegment.reachable) { - debug.dump( - `onCodePathSegmentLoop ${fromSegment.id} -> ${toSegment.id}`, - ); - this.emit("onCodePathSegmentLoop", [ - fromSegment, - toSegment, - this.currentNode, - ]); - } - } + + /** + * @param {EventGenerator} eventGenerator An event generator to wrap. + */ + constructor(eventGenerator) { + this.original = eventGenerator; + this.emitter = eventGenerator.emitter; + this.codePath = null; + this.idGenerator = new IdGenerator("s"); + this.currentNode = null; + this.onLooped = this.onLooped.bind(this); + } + + /** + * Does the process to enter a given AST node. + * This updates state of analysis and calls `enterNode` of the wrapped. + * @param {ASTNode} node A node which is entering. + * @returns {void} + */ + enterNode(node) { + this.currentNode = node; + + // Updates the code path due to node's position in its parent node. + if (node.parent) { + preprocess(this, node); + } + + /* + * Updates the code path. + * And emits onCodePathStart/onCodePathSegmentStart events. + */ + processCodePathToEnter(this, node); + + // Emits node events. + this.original.enterNode(node); + + this.currentNode = null; + } + + /** + * Does the process to leave a given AST node. + * This updates state of analysis and calls `leaveNode` of the wrapped. + * @param {ASTNode} node A node which is leaving. + * @returns {void} + */ + leaveNode(node) { + this.currentNode = node; + + /* + * Updates the code path. + * And emits onCodePathStart/onCodePathSegmentStart events. + */ + processCodePathToExit(this, node); + + // Emits node events. + this.original.leaveNode(node); + + // Emits the last onCodePathStart/onCodePathSegmentStart events. + postprocess(this, node); + + this.currentNode = null; + } + + /** + * This is called on a code path looped. + * Then this raises a looped event. + * @param {CodePathSegment} fromSegment A segment of prev. + * @param {CodePathSegment} toSegment A segment of next. + * @returns {void} + */ + onLooped(fromSegment, toSegment) { + if (fromSegment.reachable && toSegment.reachable) { + debug.dump(`onCodePathSegmentLoop ${fromSegment.id} -> ${toSegment.id}`); + this.emitter.emit( + "onCodePathSegmentLoop", + fromSegment, + toSegment, + this.currentNode + ); + } + } } module.exports = CodePathAnalyzer; diff --git a/node_modules/eslint/lib/linter/code-path-analysis/code-path-segment.js b/node_modules/eslint/lib/linter/code-path-analysis/code-path-segment.js index fa4950185..3b8dbb41b 100644 --- a/node_modules/eslint/lib/linter/code-path-analysis/code-path-segment.js +++ b/node_modules/eslint/lib/linter/code-path-analysis/code-path-segment.js @@ -21,7 +21,7 @@ const debug = require("./debug-helpers"); * @returns {boolean} `true` if the segment is reachable. */ function isReachable(segment) { - return segment.reachable; + return segment.reachable; } //------------------------------------------------------------------------------ @@ -43,220 +43,221 @@ function isReachable(segment) { * that this new segment follows. */ class CodePathSegment { - /** - * Creates a new instance. - * @param {string} id An identifier. - * @param {CodePathSegment[]} allPrevSegments An array of the previous segments. - * This array includes unreachable segments. - * @param {boolean} reachable A flag which shows this is reachable. - */ - constructor(id, allPrevSegments, reachable) { - /** - * The identifier of this code path. - * Rules use it to store additional information of each rule. - * @type {string} - */ - this.id = id; - - /** - * An array of the next reachable segments. - * @type {CodePathSegment[]} - */ - this.nextSegments = []; - - /** - * An array of the previous reachable segments. - * @type {CodePathSegment[]} - */ - this.prevSegments = allPrevSegments.filter(isReachable); - - /** - * An array of all next segments including reachable and unreachable. - * @type {CodePathSegment[]} - */ - this.allNextSegments = []; - - /** - * An array of all previous segments including reachable and unreachable. - * @type {CodePathSegment[]} - */ - this.allPrevSegments = allPrevSegments; - - /** - * A flag which shows this is reachable. - * @type {boolean} - */ - this.reachable = reachable; - - // Internal data. - Object.defineProperty(this, "internal", { - value: { - // determines if the segment has been attached to the code path - used: false, - - // array of previous segments coming from the end of a loop - loopedPrevSegments: [], - }, - }); - - /* c8 ignore start */ - if (debug.enabled) { - this.internal.nodes = []; - } /* c8 ignore stop */ - } - - /** - * Checks a given previous segment is coming from the end of a loop. - * @param {CodePathSegment} segment A previous segment to check. - * @returns {boolean} `true` if the segment is coming from the end of a loop. - */ - isLoopedPrevSegment(segment) { - return this.internal.loopedPrevSegments.includes(segment); - } - - /** - * Creates the root segment. - * @param {string} id An identifier. - * @returns {CodePathSegment} The created segment. - */ - static newRoot(id) { - return new CodePathSegment(id, [], true); - } - - /** - * Creates a new segment and appends it after the given segments. - * @param {string} id An identifier. - * @param {CodePathSegment[]} allPrevSegments An array of the previous segments - * to append to. - * @returns {CodePathSegment} The created segment. - */ - static newNext(id, allPrevSegments) { - return new CodePathSegment( - id, - CodePathSegment.flattenUnusedSegments(allPrevSegments), - allPrevSegments.some(isReachable), - ); - } - - /** - * Creates an unreachable segment and appends it after the given segments. - * @param {string} id An identifier. - * @param {CodePathSegment[]} allPrevSegments An array of the previous segments. - * @returns {CodePathSegment} The created segment. - */ - static newUnreachable(id, allPrevSegments) { - const segment = new CodePathSegment( - id, - CodePathSegment.flattenUnusedSegments(allPrevSegments), - false, - ); - - /* - * In `if (a) return a; foo();` case, the unreachable segment preceded by - * the return statement is not used but must not be removed. - */ - CodePathSegment.markUsed(segment); - - return segment; - } - - /** - * Creates a segment that follows given segments. - * This factory method does not connect with `allPrevSegments`. - * But this inherits `reachable` flag. - * @param {string} id An identifier. - * @param {CodePathSegment[]} allPrevSegments An array of the previous segments. - * @returns {CodePathSegment} The created segment. - */ - static newDisconnected(id, allPrevSegments) { - return new CodePathSegment(id, [], allPrevSegments.some(isReachable)); - } - - /** - * Marks a given segment as used. - * - * And this function registers the segment into the previous segments as a next. - * @param {CodePathSegment} segment A segment to mark. - * @returns {void} - */ - static markUsed(segment) { - if (segment.internal.used) { - return; - } - segment.internal.used = true; - - let i; - - if (segment.reachable) { - /* - * If the segment is reachable, then it's officially part of the - * code path. This loops through all previous segments to update - * their list of next segments. Because the segment is reachable, - * it's added to both `nextSegments` and `allNextSegments`. - */ - for (i = 0; i < segment.allPrevSegments.length; ++i) { - const prevSegment = segment.allPrevSegments[i]; - - prevSegment.allNextSegments.push(segment); - prevSegment.nextSegments.push(segment); - } - } else { - /* - * If the segment is not reachable, then it's not officially part of the - * code path. This loops through all previous segments to update - * their list of next segments. Because the segment is not reachable, - * it's added only to `allNextSegments`. - */ - for (i = 0; i < segment.allPrevSegments.length; ++i) { - segment.allPrevSegments[i].allNextSegments.push(segment); - } - } - } - - /** - * Marks a previous segment as looped. - * @param {CodePathSegment} segment A segment. - * @param {CodePathSegment} prevSegment A previous segment to mark. - * @returns {void} - */ - static markPrevSegmentAsLooped(segment, prevSegment) { - segment.internal.loopedPrevSegments.push(prevSegment); - } - - /** - * Creates a new array based on an array of segments. If any segment in the - * array is unused, then it is replaced by all of its previous segments. - * All used segments are returned as-is without replacement. - * @param {CodePathSegment[]} segments The array of segments to flatten. - * @returns {CodePathSegment[]} The flattened array. - */ - static flattenUnusedSegments(segments) { - const done = new Set(); - - for (let i = 0; i < segments.length; ++i) { - const segment = segments[i]; - - // Ignores duplicated. - if (done.has(segment)) { - continue; - } - - // Use previous segments if unused. - if (!segment.internal.used) { - for (let j = 0; j < segment.allPrevSegments.length; ++j) { - const prevSegment = segment.allPrevSegments[j]; - - if (!done.has(prevSegment)) { - done.add(prevSegment); - } - } - } else { - done.add(segment); - } - } - - return [...done]; - } + + /** + * Creates a new instance. + * @param {string} id An identifier. + * @param {CodePathSegment[]} allPrevSegments An array of the previous segments. + * This array includes unreachable segments. + * @param {boolean} reachable A flag which shows this is reachable. + */ + constructor(id, allPrevSegments, reachable) { + + /** + * The identifier of this code path. + * Rules use it to store additional information of each rule. + * @type {string} + */ + this.id = id; + + /** + * An array of the next reachable segments. + * @type {CodePathSegment[]} + */ + this.nextSegments = []; + + /** + * An array of the previous reachable segments. + * @type {CodePathSegment[]} + */ + this.prevSegments = allPrevSegments.filter(isReachable); + + /** + * An array of all next segments including reachable and unreachable. + * @type {CodePathSegment[]} + */ + this.allNextSegments = []; + + /** + * An array of all previous segments including reachable and unreachable. + * @type {CodePathSegment[]} + */ + this.allPrevSegments = allPrevSegments; + + /** + * A flag which shows this is reachable. + * @type {boolean} + */ + this.reachable = reachable; + + // Internal data. + Object.defineProperty(this, "internal", { + value: { + + // determines if the segment has been attached to the code path + used: false, + + // array of previous segments coming from the end of a loop + loopedPrevSegments: [] + } + }); + + /* c8 ignore start */ + if (debug.enabled) { + this.internal.nodes = []; + }/* c8 ignore stop */ + } + + /** + * Checks a given previous segment is coming from the end of a loop. + * @param {CodePathSegment} segment A previous segment to check. + * @returns {boolean} `true` if the segment is coming from the end of a loop. + */ + isLoopedPrevSegment(segment) { + return this.internal.loopedPrevSegments.includes(segment); + } + + /** + * Creates the root segment. + * @param {string} id An identifier. + * @returns {CodePathSegment} The created segment. + */ + static newRoot(id) { + return new CodePathSegment(id, [], true); + } + + /** + * Creates a new segment and appends it after the given segments. + * @param {string} id An identifier. + * @param {CodePathSegment[]} allPrevSegments An array of the previous segments + * to append to. + * @returns {CodePathSegment} The created segment. + */ + static newNext(id, allPrevSegments) { + return new CodePathSegment( + id, + CodePathSegment.flattenUnusedSegments(allPrevSegments), + allPrevSegments.some(isReachable) + ); + } + + /** + * Creates an unreachable segment and appends it after the given segments. + * @param {string} id An identifier. + * @param {CodePathSegment[]} allPrevSegments An array of the previous segments. + * @returns {CodePathSegment} The created segment. + */ + static newUnreachable(id, allPrevSegments) { + const segment = new CodePathSegment(id, CodePathSegment.flattenUnusedSegments(allPrevSegments), false); + + /* + * In `if (a) return a; foo();` case, the unreachable segment preceded by + * the return statement is not used but must not be removed. + */ + CodePathSegment.markUsed(segment); + + return segment; + } + + /** + * Creates a segment that follows given segments. + * This factory method does not connect with `allPrevSegments`. + * But this inherits `reachable` flag. + * @param {string} id An identifier. + * @param {CodePathSegment[]} allPrevSegments An array of the previous segments. + * @returns {CodePathSegment} The created segment. + */ + static newDisconnected(id, allPrevSegments) { + return new CodePathSegment(id, [], allPrevSegments.some(isReachable)); + } + + /** + * Marks a given segment as used. + * + * And this function registers the segment into the previous segments as a next. + * @param {CodePathSegment} segment A segment to mark. + * @returns {void} + */ + static markUsed(segment) { + if (segment.internal.used) { + return; + } + segment.internal.used = true; + + let i; + + if (segment.reachable) { + + /* + * If the segment is reachable, then it's officially part of the + * code path. This loops through all previous segments to update + * their list of next segments. Because the segment is reachable, + * it's added to both `nextSegments` and `allNextSegments`. + */ + for (i = 0; i < segment.allPrevSegments.length; ++i) { + const prevSegment = segment.allPrevSegments[i]; + + prevSegment.allNextSegments.push(segment); + prevSegment.nextSegments.push(segment); + } + } else { + + /* + * If the segment is not reachable, then it's not officially part of the + * code path. This loops through all previous segments to update + * their list of next segments. Because the segment is not reachable, + * it's added only to `allNextSegments`. + */ + for (i = 0; i < segment.allPrevSegments.length; ++i) { + segment.allPrevSegments[i].allNextSegments.push(segment); + } + } + } + + /** + * Marks a previous segment as looped. + * @param {CodePathSegment} segment A segment. + * @param {CodePathSegment} prevSegment A previous segment to mark. + * @returns {void} + */ + static markPrevSegmentAsLooped(segment, prevSegment) { + segment.internal.loopedPrevSegments.push(prevSegment); + } + + /** + * Creates a new array based on an array of segments. If any segment in the + * array is unused, then it is replaced by all of its previous segments. + * All used segments are returned as-is without replacement. + * @param {CodePathSegment[]} segments The array of segments to flatten. + * @returns {CodePathSegment[]} The flattened array. + */ + static flattenUnusedSegments(segments) { + const done = new Set(); + + for (let i = 0; i < segments.length; ++i) { + const segment = segments[i]; + + // Ignores duplicated. + if (done.has(segment)) { + continue; + } + + // Use previous segments if unused. + if (!segment.internal.used) { + for (let j = 0; j < segment.allPrevSegments.length; ++j) { + const prevSegment = segment.allPrevSegments[j]; + + if (!done.has(prevSegment)) { + done.add(prevSegment); + } + } + } else { + done.add(segment); + } + } + + return [...done]; + } } module.exports = CodePathSegment; diff --git a/node_modules/eslint/lib/linter/code-path-analysis/code-path-state.js b/node_modules/eslint/lib/linter/code-path-analysis/code-path-state.js index 3ed4a7bc6..2b0dc2bfc 100644 --- a/node_modules/eslint/lib/linter/code-path-analysis/code-path-state.js +++ b/node_modules/eslint/lib/linter/code-path-analysis/code-path-state.js @@ -10,7 +10,7 @@ //------------------------------------------------------------------------------ const CodePathSegment = require("./code-path-segment"), - ForkContext = require("./fork-context"); + ForkContext = require("./fork-context"); //----------------------------------------------------------------------------- // Contexts @@ -35,63 +35,68 @@ const CodePathSegment = require("./code-path-segment"), * that `break` without a label is invalid. */ class BreakContext { - /** - * Creates a new instance. - * @param {BreakContext} upperContext The previous `BreakContext`. - * @param {boolean} breakable Indicates if we are inside a statement where - * `break` without a label will exit the statement. - * @param {string|null} label The label for the statement. - * @param {ForkContext} forkContext The current fork context. - */ - constructor(upperContext, breakable, label, forkContext) { - /** - * The previous `BreakContext` - * @type {BreakContext} - */ - this.upper = upperContext; - - /** - * Indicates if we are inside a statement where `break` without a label - * will exit the statement. - * @type {boolean} - */ - this.breakable = breakable; - - /** - * The label associated with the statement. - * @type {string|null} - */ - this.label = label; - - /** - * The fork context for the `break`. - * @type {ForkContext} - */ - this.brokenForkContext = ForkContext.newEmpty(forkContext); - } + + /** + * Creates a new instance. + * @param {BreakContext} upperContext The previous `BreakContext`. + * @param {boolean} breakable Indicates if we are inside a statement where + * `break` without a label will exit the statement. + * @param {string|null} label The label for the statement. + * @param {ForkContext} forkContext The current fork context. + */ + constructor(upperContext, breakable, label, forkContext) { + + /** + * The previous `BreakContext` + * @type {BreakContext} + */ + this.upper = upperContext; + + /** + * Indicates if we are inside a statement where `break` without a label + * will exit the statement. + * @type {boolean} + */ + this.breakable = breakable; + + /** + * The label associated with the statement. + * @type {string|null} + */ + this.label = label; + + /** + * The fork context for the `break`. + * @type {ForkContext} + */ + this.brokenForkContext = ForkContext.newEmpty(forkContext); + } } /** * Represents the context for `ChainExpression` nodes. */ class ChainContext { - /** - * Creates a new instance. - * @param {ChainContext} upperContext The previous `ChainContext`. - */ - constructor(upperContext) { - /** - * The previous `ChainContext` - * @type {ChainContext} - */ - this.upper = upperContext; - - /** - * The number of choice contexts inside of the `ChainContext`. - * @type {number} - */ - this.choiceContextCount = 0; - } + + /** + * Creates a new instance. + * @param {ChainContext} upperContext The previous `ChainContext`. + */ + constructor(upperContext) { + + /** + * The previous `ChainContext` + * @type {ChainContext} + */ + this.upper = upperContext; + + /** + * The number of choice contexts inside of the `ChainContext`. + * @type {number} + */ + this.choiceContextCount = 0; + + } } /** @@ -119,241 +124,249 @@ class ChainContext { * we fork to the right expression (tracked by `nullishForkContext`). */ class ChoiceContext { - /** - * Creates a new instance. - * @param {ChoiceContext} upperContext The previous `ChoiceContext`. - * @param {string} kind The kind of choice. If it's a logical or assignment expression, this - * is `"&&"` or `"||"` or `"??"`; if it's an `if` statement or - * conditional expression, this is `"test"`; otherwise, this is `"loop"`. - * @param {boolean} isForkingAsResult Indicates if the result of the choice - * creates a fork. - * @param {ForkContext} forkContext The containing `ForkContext`. - */ - constructor(upperContext, kind, isForkingAsResult, forkContext) { - /** - * The previous `ChoiceContext` - * @type {ChoiceContext} - */ - this.upper = upperContext; - - /** - * The kind of choice. If it's a logical or assignment expression, this - * is `"&&"` or `"||"` or `"??"`; if it's an `if` statement or - * conditional expression, this is `"test"`; otherwise, this is `"loop"`. - * @type {string} - */ - this.kind = kind; - - /** - * Indicates if the result of the choice forks the code path. - * @type {boolean} - */ - this.isForkingAsResult = isForkingAsResult; - - /** - * The fork context for the `true` path of the choice. - * @type {ForkContext} - */ - this.trueForkContext = ForkContext.newEmpty(forkContext); - - /** - * The fork context for the `false` path of the choice. - * @type {ForkContext} - */ - this.falseForkContext = ForkContext.newEmpty(forkContext); - - /** - * The fork context for when the choice result is `null` or `undefined`. - * @type {ForkContext} - */ - this.nullishForkContext = ForkContext.newEmpty(forkContext); - - /** - * Indicates if any of `trueForkContext`, `falseForkContext`, or - * `nullishForkContext` have been updated with segments from a child context. - * @type {boolean} - */ - this.processed = false; - } + + /** + * Creates a new instance. + * @param {ChoiceContext} upperContext The previous `ChoiceContext`. + * @param {string} kind The kind of choice. If it's a logical or assignment expression, this + * is `"&&"` or `"||"` or `"??"`; if it's an `if` statement or + * conditional expression, this is `"test"`; otherwise, this is `"loop"`. + * @param {boolean} isForkingAsResult Indicates if the result of the choice + * creates a fork. + * @param {ForkContext} forkContext The containing `ForkContext`. + */ + constructor(upperContext, kind, isForkingAsResult, forkContext) { + + /** + * The previous `ChoiceContext` + * @type {ChoiceContext} + */ + this.upper = upperContext; + + /** + * The kind of choice. If it's a logical or assignment expression, this + * is `"&&"` or `"||"` or `"??"`; if it's an `if` statement or + * conditional expression, this is `"test"`; otherwise, this is `"loop"`. + * @type {string} + */ + this.kind = kind; + + /** + * Indicates if the result of the choice forks the code path. + * @type {boolean} + */ + this.isForkingAsResult = isForkingAsResult; + + /** + * The fork context for the `true` path of the choice. + * @type {ForkContext} + */ + this.trueForkContext = ForkContext.newEmpty(forkContext); + + /** + * The fork context for the `false` path of the choice. + * @type {ForkContext} + */ + this.falseForkContext = ForkContext.newEmpty(forkContext); + + /** + * The fork context for when the choice result is `null` or `undefined`. + * @type {ForkContext} + */ + this.nullishForkContext = ForkContext.newEmpty(forkContext); + + /** + * Indicates if any of `trueForkContext`, `falseForkContext`, or + * `nullishForkContext` have been updated with segments from a child context. + * @type {boolean} + */ + this.processed = false; + } + } /** * Base class for all loop contexts. */ class LoopContextBase { - /** - * Creates a new instance. - * @param {LoopContext|null} upperContext The previous `LoopContext`. - * @param {string} type The AST node's `type` for the loop. - * @param {string|null} label The label for the loop from an enclosing `LabeledStatement`. - * @param {BreakContext} breakContext The context for breaking the loop. - */ - constructor(upperContext, type, label, breakContext) { - /** - * The previous `LoopContext`. - * @type {LoopContext} - */ - this.upper = upperContext; - - /** - * The AST node's `type` for the loop. - * @type {string} - */ - this.type = type; - - /** - * The label for the loop from an enclosing `LabeledStatement`. - * @type {string|null} - */ - this.label = label; - - /** - * The fork context for when `break` is encountered. - * @type {ForkContext} - */ - this.brokenForkContext = breakContext.brokenForkContext; - } + + /** + * Creates a new instance. + * @param {LoopContext|null} upperContext The previous `LoopContext`. + * @param {string} type The AST node's `type` for the loop. + * @param {string|null} label The label for the loop from an enclosing `LabeledStatement`. + * @param {BreakContext} breakContext The context for breaking the loop. + */ + constructor(upperContext, type, label, breakContext) { + + /** + * The previous `LoopContext`. + * @type {LoopContext} + */ + this.upper = upperContext; + + /** + * The AST node's `type` for the loop. + * @type {string} + */ + this.type = type; + + /** + * The label for the loop from an enclosing `LabeledStatement`. + * @type {string|null} + */ + this.label = label; + + /** + * The fork context for when `break` is encountered. + * @type {ForkContext} + */ + this.brokenForkContext = breakContext.brokenForkContext; + } } /** * Represents the context for a `while` loop. */ class WhileLoopContext extends LoopContextBase { - /** - * Creates a new instance. - * @param {LoopContext|null} upperContext The previous `LoopContext`. - * @param {string|null} label The label for the loop from an enclosing `LabeledStatement`. - * @param {BreakContext} breakContext The context for breaking the loop. - */ - constructor(upperContext, label, breakContext) { - super(upperContext, "WhileStatement", label, breakContext); - - /** - * The hardcoded literal boolean test condition for - * the loop. Used to catch infinite or skipped loops. - * @type {boolean|undefined} - */ - this.test = void 0; - - /** - * The segments representing the test condition where `continue` will - * jump to. The test condition will typically have just one segment but - * it's possible for there to be more than one. - * @type {Array|null} - */ - this.continueDestSegments = null; - } + + /** + * Creates a new instance. + * @param {LoopContext|null} upperContext The previous `LoopContext`. + * @param {string|null} label The label for the loop from an enclosing `LabeledStatement`. + * @param {BreakContext} breakContext The context for breaking the loop. + */ + constructor(upperContext, label, breakContext) { + super(upperContext, "WhileStatement", label, breakContext); + + /** + * The hardcoded literal boolean test condition for + * the loop. Used to catch infinite or skipped loops. + * @type {boolean|undefined} + */ + this.test = void 0; + + /** + * The segments representing the test condition where `continue` will + * jump to. The test condition will typically have just one segment but + * it's possible for there to be more than one. + * @type {Array|null} + */ + this.continueDestSegments = null; + } } /** * Represents the context for a `do-while` loop. */ class DoWhileLoopContext extends LoopContextBase { - /** - * Creates a new instance. - * @param {LoopContext|null} upperContext The previous `LoopContext`. - * @param {string|null} label The label for the loop from an enclosing `LabeledStatement`. - * @param {BreakContext} breakContext The context for breaking the loop. - * @param {ForkContext} forkContext The enclosing fork context. - */ - constructor(upperContext, label, breakContext, forkContext) { - super(upperContext, "DoWhileStatement", label, breakContext); - - /** - * The hardcoded literal boolean test condition for - * the loop. Used to catch infinite or skipped loops. - * @type {boolean|undefined} - */ - this.test = void 0; - - /** - * The segments at the start of the loop body. This is the only loop - * where the test comes at the end, so the first iteration always - * happens and we need a reference to the first statements. - * @type {Array|null} - */ - this.entrySegments = null; - - /** - * The fork context to follow when a `continue` is found. - * @type {ForkContext} - */ - this.continueForkContext = ForkContext.newEmpty(forkContext); - } + + /** + * Creates a new instance. + * @param {LoopContext|null} upperContext The previous `LoopContext`. + * @param {string|null} label The label for the loop from an enclosing `LabeledStatement`. + * @param {BreakContext} breakContext The context for breaking the loop. + * @param {ForkContext} forkContext The enclosing fork context. + */ + constructor(upperContext, label, breakContext, forkContext) { + super(upperContext, "DoWhileStatement", label, breakContext); + + /** + * The hardcoded literal boolean test condition for + * the loop. Used to catch infinite or skipped loops. + * @type {boolean|undefined} + */ + this.test = void 0; + + /** + * The segments at the start of the loop body. This is the only loop + * where the test comes at the end, so the first iteration always + * happens and we need a reference to the first statements. + * @type {Array|null} + */ + this.entrySegments = null; + + /** + * The fork context to follow when a `continue` is found. + * @type {ForkContext} + */ + this.continueForkContext = ForkContext.newEmpty(forkContext); + } } /** * Represents the context for a `for` loop. */ class ForLoopContext extends LoopContextBase { - /** - * Creates a new instance. - * @param {LoopContext|null} upperContext The previous `LoopContext`. - * @param {string|null} label The label for the loop from an enclosing `LabeledStatement`. - * @param {BreakContext} breakContext The context for breaking the loop. - */ - constructor(upperContext, label, breakContext) { - super(upperContext, "ForStatement", label, breakContext); - - /** - * The hardcoded literal boolean test condition for - * the loop. Used to catch infinite or skipped loops. - * @type {boolean|undefined} - */ - this.test = void 0; - - /** - * The end of the init expression. This may change during the lifetime - * of the instance as we traverse the loop because some loops don't have - * an init expression. - * @type {Array|null} - */ - this.endOfInitSegments = null; - - /** - * The start of the test expression. This may change during the lifetime - * of the instance as we traverse the loop because some loops don't have - * a test expression. - * @type {Array|null} - */ - this.testSegments = null; - - /** - * The end of the test expression. This may change during the lifetime - * of the instance as we traverse the loop because some loops don't have - * a test expression. - * @type {Array|null} - */ - this.endOfTestSegments = null; - - /** - * The start of the update expression. This may change during the lifetime - * of the instance as we traverse the loop because some loops don't have - * an update expression. - * @type {Array|null} - */ - this.updateSegments = null; - - /** - * The end of the update expression. This may change during the lifetime - * of the instance as we traverse the loop because some loops don't have - * an update expression. - * @type {Array|null} - */ - this.endOfUpdateSegments = null; - - /** - * The segments representing the test condition where `continue` will - * jump to. The test condition will typically have just one segment but - * it's possible for there to be more than one. This may change during the - * lifetime of the instance as we traverse the loop because some loops - * don't have an update expression. When there is an update expression, this - * will end up pointing to that expression; otherwise it will end up pointing - * to the test expression. - * @type {Array|null} - */ - this.continueDestSegments = null; - } + + /** + * Creates a new instance. + * @param {LoopContext|null} upperContext The previous `LoopContext`. + * @param {string|null} label The label for the loop from an enclosing `LabeledStatement`. + * @param {BreakContext} breakContext The context for breaking the loop. + */ + constructor(upperContext, label, breakContext) { + super(upperContext, "ForStatement", label, breakContext); + + /** + * The hardcoded literal boolean test condition for + * the loop. Used to catch infinite or skipped loops. + * @type {boolean|undefined} + */ + this.test = void 0; + + /** + * The end of the init expression. This may change during the lifetime + * of the instance as we traverse the loop because some loops don't have + * an init expression. + * @type {Array|null} + */ + this.endOfInitSegments = null; + + /** + * The start of the test expression. This may change during the lifetime + * of the instance as we traverse the loop because some loops don't have + * a test expression. + * @type {Array|null} + */ + this.testSegments = null; + + /** + * The end of the test expression. This may change during the lifetime + * of the instance as we traverse the loop because some loops don't have + * a test expression. + * @type {Array|null} + */ + this.endOfTestSegments = null; + + /** + * The start of the update expression. This may change during the lifetime + * of the instance as we traverse the loop because some loops don't have + * an update expression. + * @type {Array|null} + */ + this.updateSegments = null; + + /** + * The end of the update expresion. This may change during the lifetime + * of the instance as we traverse the loop because some loops don't have + * an update expression. + * @type {Array|null} + */ + this.endOfUpdateSegments = null; + + /** + * The segments representing the test condition where `continue` will + * jump to. The test condition will typically have just one segment but + * it's possible for there to be more than one. This may change during the + * lifetime of the instance as we traverse the loop because some loops + * don't have an update expression. When there is an update expression, this + * will end up pointing to that expression; otherwise it will end up pointing + * to the test expression. + * @type {Array|null} + */ + this.continueDestSegments = null; + } } /** @@ -366,112 +379,114 @@ class ForLoopContext extends LoopContextBase { * example, in `for (var x in y)`, the right is `y`. */ class ForInLoopContext extends LoopContextBase { - /** - * Creates a new instance. - * @param {LoopContext|null} upperContext The previous `LoopContext`. - * @param {string|null} label The label for the loop from an enclosing `LabeledStatement`. - * @param {BreakContext} breakContext The context for breaking the loop. - */ - constructor(upperContext, label, breakContext) { - super(upperContext, "ForInStatement", label, breakContext); - - /** - * The segments that came immediately before the start of the loop. - * This allows you to traverse backwards out of the loop into the - * surrounding code. This is necessary to evaluate the right expression - * correctly, as it must be evaluated in the same way as the left - * expression, but the pointer to these segments would otherwise be - * lost if not stored on the instance. Once the right expression has - * been evaluated, this property is no longer used. - * @type {Array|null} - */ - this.prevSegments = null; - - /** - * Segments representing the start of everything to the left of the - * `in` keyword. This can be used to move forward towards - * `endOfLeftSegments`. `leftSegments` and `endOfLeftSegments` are - * effectively the head and tail of a doubly-linked list. - * @type {Array|null} - */ - this.leftSegments = null; - - /** - * Segments representing the end of everything to the left of the - * `in` keyword. This can be used to move backward towards `leftSegments`. - * `leftSegments` and `endOfLeftSegments` are effectively the head - * and tail of a doubly-linked list. - * @type {Array|null} - */ - this.endOfLeftSegments = null; - - /** - * The segments representing the left expression where `continue` will - * jump to. In `for-in` loops, `continue` must always re-execute the - * left expression each time through the loop. This contains the same - * segments as `leftSegments`, but is duplicated here so each loop - * context has the same property pointing to where `continue` should - * end up. - * @type {Array|null} - */ - this.continueDestSegments = null; - } + + /** + * Creates a new instance. + * @param {LoopContext|null} upperContext The previous `LoopContext`. + * @param {string|null} label The label for the loop from an enclosing `LabeledStatement`. + * @param {BreakContext} breakContext The context for breaking the loop. + */ + constructor(upperContext, label, breakContext) { + super(upperContext, "ForInStatement", label, breakContext); + + /** + * The segments that came immediately before the start of the loop. + * This allows you to traverse backwards out of the loop into the + * surrounding code. This is necessary to evaluate the right expression + * correctly, as it must be evaluated in the same way as the left + * expression, but the pointer to these segments would otherwise be + * lost if not stored on the instance. Once the right expression has + * been evaluated, this property is no longer used. + * @type {Array|null} + */ + this.prevSegments = null; + + /** + * Segments representing the start of everything to the left of the + * `in` keyword. This can be used to move forward towards + * `endOfLeftSegments`. `leftSegments` and `endOfLeftSegments` are + * effectively the head and tail of a doubly-linked list. + * @type {Array|null} + */ + this.leftSegments = null; + + /** + * Segments representing the end of everything to the left of the + * `in` keyword. This can be used to move backward towards `leftSegments`. + * `leftSegments` and `endOfLeftSegments` are effectively the head + * and tail of a doubly-linked list. + * @type {Array|null} + */ + this.endOfLeftSegments = null; + + /** + * The segments representing the left expression where `continue` will + * jump to. In `for-in` loops, `continue` must always re-execute the + * left expression each time through the loop. This contains the same + * segments as `leftSegments`, but is duplicated here so each loop + * context has the same property pointing to where `continue` should + * end up. + * @type {Array|null} + */ + this.continueDestSegments = null; + } } /** * Represents the context for a `for-of` loop. */ class ForOfLoopContext extends LoopContextBase { - /** - * Creates a new instance. - * @param {LoopContext|null} upperContext The previous `LoopContext`. - * @param {string|null} label The label for the loop from an enclosing `LabeledStatement`. - * @param {BreakContext} breakContext The context for breaking the loop. - */ - constructor(upperContext, label, breakContext) { - super(upperContext, "ForOfStatement", label, breakContext); - - /** - * The segments that came immediately before the start of the loop. - * This allows you to traverse backwards out of the loop into the - * surrounding code. This is necessary to evaluate the right expression - * correctly, as it must be evaluated in the same way as the left - * expression, but the pointer to these segments would otherwise be - * lost if not stored on the instance. Once the right expression has - * been evaluated, this property is no longer used. - * @type {Array|null} - */ - this.prevSegments = null; - - /** - * Segments representing the start of everything to the left of the - * `of` keyword. This can be used to move forward towards - * `endOfLeftSegments`. `leftSegments` and `endOfLeftSegments` are - * effectively the head and tail of a doubly-linked list. - * @type {Array|null} - */ - this.leftSegments = null; - - /** - * Segments representing the end of everything to the left of the - * `of` keyword. This can be used to move backward towards `leftSegments`. - * `leftSegments` and `endOfLeftSegments` are effectively the head - * and tail of a doubly-linked list. - * @type {Array|null} - */ - this.endOfLeftSegments = null; - - /** - * The segments representing the left expression where `continue` will - * jump to. In `for-in` loops, `continue` must always re-execute the - * left expression each time through the loop. This contains the same - * segments as `leftSegments`, but is duplicated here so each loop - * context has the same property pointing to where `continue` should - * end up. - * @type {Array|null} - */ - this.continueDestSegments = null; - } + + /** + * Creates a new instance. + * @param {LoopContext|null} upperContext The previous `LoopContext`. + * @param {string|null} label The label for the loop from an enclosing `LabeledStatement`. + * @param {BreakContext} breakContext The context for breaking the loop. + */ + constructor(upperContext, label, breakContext) { + super(upperContext, "ForOfStatement", label, breakContext); + + /** + * The segments that came immediately before the start of the loop. + * This allows you to traverse backwards out of the loop into the + * surrounding code. This is necessary to evaluate the right expression + * correctly, as it must be evaluated in the same way as the left + * expression, but the pointer to these segments would otherwise be + * lost if not stored on the instance. Once the right expression has + * been evaluated, this property is no longer used. + * @type {Array|null} + */ + this.prevSegments = null; + + /** + * Segments representing the start of everything to the left of the + * `of` keyword. This can be used to move forward towards + * `endOfLeftSegments`. `leftSegments` and `endOfLeftSegments` are + * effectively the head and tail of a doubly-linked list. + * @type {Array|null} + */ + this.leftSegments = null; + + /** + * Segments representing the end of everything to the left of the + * `of` keyword. This can be used to move backward towards `leftSegments`. + * `leftSegments` and `endOfLeftSegments` are effectively the head + * and tail of a doubly-linked list. + * @type {Array|null} + */ + this.endOfLeftSegments = null; + + /** + * The segments representing the left expression where `continue` will + * jump to. In `for-in` loops, `continue` must always re-execute the + * left expression each time through the loop. This contains the same + * segments as `leftSegments`, but is duplicated here so each loop + * context has the same property pointing to where `continue` should + * end up. + * @type {Array|null} + */ + this.continueDestSegments = null; + } } /** @@ -483,123 +498,127 @@ class ForOfLoopContext extends LoopContextBase { * Represents the context for a `switch` statement. */ class SwitchContext { - /** - * Creates a new instance. - * @param {SwitchContext} upperContext The previous context. - * @param {boolean} hasCase Indicates if there is at least one `case` statement. - * `default` doesn't count. - */ - constructor(upperContext, hasCase) { - /** - * The previous context. - * @type {SwitchContext} - */ - this.upper = upperContext; - - /** - * Indicates if there is at least one `case` statement. `default` doesn't count. - * @type {boolean} - */ - this.hasCase = hasCase; - - /** - * The `default` keyword. - * @type {Array|null} - */ - this.defaultSegments = null; - - /** - * The default case body starting segments. - * @type {Array|null} - */ - this.defaultBodySegments = null; - - /** - * Indicates if a `default` case and is empty exists. - * @type {boolean} - */ - this.foundEmptyDefault = false; - - /** - * Indicates that a `default` exists and is the last case. - * @type {boolean} - */ - this.lastIsDefault = false; - - /** - * The number of fork contexts created. This is equivalent to the - * number of `case` statements plus a `default` statement (if present). - * @type {number} - */ - this.forkCount = 0; - } + + /** + * Creates a new instance. + * @param {SwitchContext} upperContext The previous context. + * @param {boolean} hasCase Indicates if there is at least one `case` statement. + * `default` doesn't count. + */ + constructor(upperContext, hasCase) { + + /** + * The previous context. + * @type {SwitchContext} + */ + this.upper = upperContext; + + /** + * Indicates if there is at least one `case` statement. `default` doesn't count. + * @type {boolean} + */ + this.hasCase = hasCase; + + /** + * The `default` keyword. + * @type {Array|null} + */ + this.defaultSegments = null; + + /** + * The default case body starting segments. + * @type {Array|null} + */ + this.defaultBodySegments = null; + + /** + * Indicates if a `default` case and is empty exists. + * @type {boolean} + */ + this.foundEmptyDefault = false; + + /** + * Indicates that a `default` exists and is the last case. + * @type {boolean} + */ + this.lastIsDefault = false; + + /** + * The number of fork contexts created. This is equivalent to the + * number of `case` statements plus a `default` statement (if present). + * @type {number} + */ + this.forkCount = 0; + } } /** * Represents the context for a `try` statement. */ class TryContext { - /** - * Creates a new instance. - * @param {TryContext} upperContext The previous context. - * @param {boolean} hasFinalizer Indicates if the `try` statement has a - * `finally` block. - * @param {ForkContext} forkContext The enclosing fork context. - */ - constructor(upperContext, hasFinalizer, forkContext) { - /** - * The previous context. - * @type {TryContext} - */ - this.upper = upperContext; - - /** - * Indicates if the `try` statement has a `finally` block. - * @type {boolean} - */ - this.hasFinalizer = hasFinalizer; - - /** - * Tracks the traversal position inside of the `try` statement. This is - * used to help determine the context necessary to create paths because - * a `try` statement may or may not have `catch` or `finally` blocks, - * and code paths behave differently in those blocks. - * @type {"try"|"catch"|"finally"} - */ - this.position = "try"; - - /** - * If the `try` statement has a `finally` block, this affects how a - * `return` statement behaves in the `try` block. Without `finally`, - * `return` behaves as usual and doesn't require a fork; with `finally`, - * `return` forks into the `finally` block, so we need a fork context - * to track it. - * @type {ForkContext|null} - */ - this.returnedForkContext = hasFinalizer - ? ForkContext.newEmpty(forkContext) - : null; - - /** - * When a `throw` occurs inside of a `try` block, the code path forks - * into the `catch` or `finally` blocks, and this fork context tracks - * that path. - * @type {ForkContext} - */ - this.thrownForkContext = ForkContext.newEmpty(forkContext); - - /** - * Indicates if the last segment in the `try` block is reachable. - * @type {boolean} - */ - this.lastOfTryIsReachable = false; - - /** - * Indicates if the last segment in the `catch` block is reachable. - * @type {boolean} - */ - this.lastOfCatchIsReachable = false; - } + + /** + * Creates a new instance. + * @param {TryContext} upperContext The previous context. + * @param {boolean} hasFinalizer Indicates if the `try` statement has a + * `finally` block. + * @param {ForkContext} forkContext The enclosing fork context. + */ + constructor(upperContext, hasFinalizer, forkContext) { + + /** + * The previous context. + * @type {TryContext} + */ + this.upper = upperContext; + + /** + * Indicates if the `try` statement has a `finally` block. + * @type {boolean} + */ + this.hasFinalizer = hasFinalizer; + + /** + * Tracks the traversal position inside of the `try` statement. This is + * used to help determine the context necessary to create paths because + * a `try` statement may or may not have `catch` or `finally` blocks, + * and code paths behave differently in those blocks. + * @type {"try"|"catch"|"finally"} + */ + this.position = "try"; + + /** + * If the `try` statement has a `finally` block, this affects how a + * `return` statement behaves in the `try` block. Without `finally`, + * `return` behaves as usual and doesn't require a fork; with `finally`, + * `return` forks into the `finally` block, so we need a fork context + * to track it. + * @type {ForkContext|null} + */ + this.returnedForkContext = hasFinalizer + ? ForkContext.newEmpty(forkContext) + : null; + + /** + * When a `throw` occurs inside of a `try` block, the code path forks + * into the `catch` or `finally` blocks, and this fork context tracks + * that path. + * @type {ForkContext} + */ + this.thrownForkContext = ForkContext.newEmpty(forkContext); + + /** + * Indicates if the last segment in the `try` block is reachable. + * @type {boolean} + */ + this.lastOfTryIsReachable = false; + + /** + * Indicates if the last segment in the `catch` block is reachable. + * @type {boolean} + */ + this.lastOfCatchIsReachable = false; + } } //------------------------------------------------------------------------------ @@ -619,14 +638,14 @@ class TryContext { * @returns {void} */ function addToReturnedOrThrown(dest, others, all, segments) { - for (let i = 0; i < segments.length; ++i) { - const segment = segments[i]; - - dest.push(segment); - if (!others.includes(segment)) { - all.push(segment); - } - } + for (let i = 0; i < segments.length; ++i) { + const segment = segments[i]; + + dest.push(segment); + if (!others.includes(segment)) { + all.push(segment); + } + } } /** @@ -636,21 +655,21 @@ function addToReturnedOrThrown(dest, others, all, segments) { * @returns {LoopContext} A loop-context for a `continue` statement. */ function getContinueContext(state, label) { - if (!label) { - return state.loopContext; - } + if (!label) { + return state.loopContext; + } - let context = state.loopContext; + let context = state.loopContext; - while (context) { - if (context.label === label) { - return context; - } - context = context.upper; - } + while (context) { + if (context.label === label) { + return context; + } + context = context.upper; + } - /* c8 ignore next */ - return null; + /* c8 ignore next */ + return null; } /** @@ -660,17 +679,17 @@ function getContinueContext(state, label) { * @returns {BreakContext} A context for a `break` statement. */ function getBreakContext(state, label) { - let context = state.breakContext; + let context = state.breakContext; - while (context) { - if (label ? context.label === label : context.breakable) { - return context; - } - context = context.upper; - } + while (context) { + if (label ? context.label === label : context.breakable) { + return context; + } + context = context.upper; + } - /* c8 ignore next */ - return null; + /* c8 ignore next */ + return null; } /** @@ -681,16 +700,16 @@ function getBreakContext(state, label) { * @returns {TryContext|CodePathState} A context for a `return` statement. */ function getReturnContext(state) { - let context = state.tryContext; + let context = state.tryContext; - while (context) { - if (context.hasFinalizer && context.position !== "finally") { - return context; - } - context = context.upper; - } + while (context) { + if (context.hasFinalizer && context.position !== "finally") { + return context; + } + context = context.upper; + } - return state; + return state; } /** @@ -702,19 +721,18 @@ function getReturnContext(state) { * @returns {TryContext|CodePathState} A context for a `throw` statement. */ function getThrowContext(state) { - let context = state.tryContext; - - while (context) { - if ( - context.position === "try" || - (context.hasFinalizer && context.position === "catch") - ) { - return context; - } - context = context.upper; - } - - return state; + let context = state.tryContext; + + while (context) { + if (context.position === "try" || + (context.hasFinalizer && context.position === "catch") + ) { + return context; + } + context = context.upper; + } + + return state; } /** @@ -724,7 +742,7 @@ function getThrowContext(state) { * @returns {void} */ function removeFromArray(elements, value) { - elements.splice(elements.indexOf(value), 1); + elements.splice(elements.indexOf(value), 1); } /** @@ -738,15 +756,15 @@ function removeFromArray(elements, value) { * @returns {void} */ function disconnectSegments(prevSegments, nextSegments) { - for (let i = 0; i < prevSegments.length; ++i) { - const prevSegment = prevSegments[i]; - const nextSegment = nextSegments[i]; - - removeFromArray(prevSegment.nextSegments, nextSegment); - removeFromArray(prevSegment.allNextSegments, nextSegment); - removeFromArray(nextSegment.prevSegments, prevSegment); - removeFromArray(nextSegment.allPrevSegments, prevSegment); - } + for (let i = 0; i < prevSegments.length; ++i) { + const prevSegment = prevSegments[i]; + const nextSegment = nextSegments[i]; + + removeFromArray(prevSegment.nextSegments, nextSegment); + removeFromArray(prevSegment.allNextSegments, nextSegment); + removeFromArray(nextSegment.prevSegments, prevSegment); + removeFromArray(nextSegment.allPrevSegments, prevSegment); + } } /** @@ -758,60 +776,58 @@ function disconnectSegments(prevSegments, nextSegments) { * @returns {void} */ function makeLooped(state, unflattenedFromSegments, unflattenedToSegments) { - const fromSegments = CodePathSegment.flattenUnusedSegments( - unflattenedFromSegments, - ); - const toSegments = CodePathSegment.flattenUnusedSegments( - unflattenedToSegments, - ); - const end = Math.min(fromSegments.length, toSegments.length); - - /* - * This loop effectively updates a doubly-linked list between two collections - * of segments making sure that segments in the same array indices are - * combined to create a path. - */ - for (let i = 0; i < end; ++i) { - // get the segments in matching array indices - const fromSegment = fromSegments[i]; - const toSegment = toSegments[i]; - - /* - * If the destination segment is reachable, then create a path from the - * source segment to the destination segment. - */ - if (toSegment.reachable) { - fromSegment.nextSegments.push(toSegment); - } - - /* - * If the source segment is reachable, then create a path from the - * destination segment back to the source segment. - */ - if (fromSegment.reachable) { - toSegment.prevSegments.push(fromSegment); - } - - /* - * Also update the arrays that don't care if the segments are reachable - * or not. This should always happen regardless of anything else. - */ - fromSegment.allNextSegments.push(toSegment); - toSegment.allPrevSegments.push(fromSegment); - - /* - * If the destination segment has at least two previous segments in its - * path then that means there was one previous segment before this iteration - * of the loop was executed. So, we need to mark the source segment as - * looped. - */ - if (toSegment.allPrevSegments.length >= 2) { - CodePathSegment.markPrevSegmentAsLooped(toSegment, fromSegment); - } - - // let the code path analyzer know that there's been a loop created - state.notifyLooped(fromSegment, toSegment); - } + + const fromSegments = CodePathSegment.flattenUnusedSegments(unflattenedFromSegments); + const toSegments = CodePathSegment.flattenUnusedSegments(unflattenedToSegments); + const end = Math.min(fromSegments.length, toSegments.length); + + /* + * This loop effectively updates a doubly-linked list between two collections + * of segments making sure that segments in the same array indices are + * combined to create a path. + */ + for (let i = 0; i < end; ++i) { + + // get the segments in matching array indices + const fromSegment = fromSegments[i]; + const toSegment = toSegments[i]; + + /* + * If the destination segment is reachable, then create a path from the + * source segment to the destination segment. + */ + if (toSegment.reachable) { + fromSegment.nextSegments.push(toSegment); + } + + /* + * If the source segment is reachable, then create a path from the + * destination segment back to the source segment. + */ + if (fromSegment.reachable) { + toSegment.prevSegments.push(fromSegment); + } + + /* + * Also update the arrays that don't care if the segments are reachable + * or not. This should always happen regardless of anything else. + */ + fromSegment.allNextSegments.push(toSegment); + toSegment.allPrevSegments.push(fromSegment); + + /* + * If the destination segment has at least two previous segments in its + * path then that means there was one previous segment before this iteration + * of the loop was executed. So, we need to mark the source segment as + * looped. + */ + if (toSegment.allPrevSegments.length >= 2) { + CodePathSegment.markPrevSegmentAsLooped(toSegment, fromSegment); + } + + // let the code path analyzer know that there's been a loop created + state.notifyLooped(fromSegment, toSegment); + } } /** @@ -825,27 +841,28 @@ function makeLooped(state, unflattenedFromSegments, unflattenedToSegments) { * @returns {void} */ function finalizeTestSegmentsOfFor(context, choiceContext, head) { - /* - * If this choice context doesn't already contain paths from a - * child context, then add the current head to each potential path. - */ - if (!choiceContext.processed) { - choiceContext.trueForkContext.add(head); - choiceContext.falseForkContext.add(head); - choiceContext.nullishForkContext.add(head); - } - - /* - * If the test condition isn't a hardcoded truthy value, then `break` - * must follow the same path as if the test condition is false. To represent - * that, we append the path for when the loop test is false (represented by - * `falseForkContext`) to the `brokenForkContext`. - */ - if (context.test !== true) { - context.brokenForkContext.addAll(choiceContext.falseForkContext); - } - - context.endOfTestSegments = choiceContext.trueForkContext.makeNext(0, -1); + + /* + * If this choice context doesn't already contain paths from a + * child context, then add the current head to each potential path. + */ + if (!choiceContext.processed) { + choiceContext.trueForkContext.add(head); + choiceContext.falseForkContext.add(head); + choiceContext.nullishForkContext.add(head); + } + + /* + * If the test condition isn't a hardcoded truthy value, then `break` + * must follow the same path as if the test condition is false. To represent + * that, we append the path for when the loop test is false (represented by + * `falseForkContext`) to the `brokenForkContext`. + */ + if (context.test !== true) { + context.brokenForkContext.addAll(choiceContext.falseForkContext); + } + + context.endOfTestSegments = choiceContext.trueForkContext.makeNext(0, -1); } //------------------------------------------------------------------------------ @@ -856,1515 +873,1476 @@ function finalizeTestSegmentsOfFor(context, choiceContext, head) { * A class which manages state to analyze code paths. */ class CodePathState { - /** - * Creates a new instance. - * @param {IdGenerator} idGenerator An id generator to generate id for code - * path segments. - * @param {Function} onLooped A callback function to notify looping. - */ - constructor(idGenerator, onLooped) { - /** - * The ID generator to use when creating new segments. - * @type {IdGenerator} - */ - this.idGenerator = idGenerator; - - /** - * A callback function to call when there is a loop. - * @type {Function} - */ - this.notifyLooped = onLooped; - - /** - * The root fork context for this state. - * @type {ForkContext} - */ - this.forkContext = ForkContext.newRoot(idGenerator); - - /** - * Context for logical expressions, conditional expressions, `if` statements, - * and loops. - * @type {ChoiceContext} - */ - this.choiceContext = null; - - /** - * Context for `switch` statements. - * @type {SwitchContext} - */ - this.switchContext = null; - - /** - * Context for `try` statements. - * @type {TryContext} - */ - this.tryContext = null; - - /** - * Context for loop statements. - * @type {LoopContext} - */ - this.loopContext = null; - - /** - * Context for `break` statements. - * @type {BreakContext} - */ - this.breakContext = null; - - /** - * Context for `ChainExpression` nodes. - * @type {ChainContext} - */ - this.chainContext = null; - - /** - * An array that tracks the current segments in the state. The array - * starts empty and segments are added with each `onCodePathSegmentStart` - * event and removed with each `onCodePathSegmentEnd` event. Effectively, - * this is tracking the code path segment traversal as the state is - * modified. - * @type {Array} - */ - this.currentSegments = []; - - /** - * Tracks the starting segment for this path. This value never changes. - * @type {CodePathSegment} - */ - this.initialSegment = this.forkContext.head[0]; - - /** - * The final segments of the code path which are either `return` or `throw`. - * This is a union of the segments in `returnedForkContext` and `thrownForkContext`. - * @type {Array} - */ - this.finalSegments = []; - - /** - * The final segments of the code path which are `return`. These - * segments are also contained in `finalSegments`. - * @type {Array} - */ - this.returnedForkContext = []; - - /** - * The final segments of the code path which are `throw`. These - * segments are also contained in `finalSegments`. - * @type {Array} - */ - this.thrownForkContext = []; - - /* - * We add an `add` method so that these look more like fork contexts and - * can be used interchangeably when a fork context is needed to add more - * segments to a path. - * - * Ultimately, we want anything added to `returned` or `thrown` to also - * be added to `final`. We only add reachable and used segments to these - * arrays. - */ - const final = this.finalSegments; - const returned = this.returnedForkContext; - const thrown = this.thrownForkContext; - - returned.add = addToReturnedOrThrown.bind( - null, - returned, - thrown, - final, - ); - thrown.add = addToReturnedOrThrown.bind(null, thrown, returned, final); - } - - /** - * A passthrough property exposing the current pointer as part of the API. - * @type {CodePathSegment[]} - */ - get headSegments() { - return this.forkContext.head; - } - - /** - * The parent forking context. - * This is used for the root of new forks. - * @type {ForkContext} - */ - get parentForkContext() { - const current = this.forkContext; - - return current && current.upper; - } - - /** - * Creates and stacks new forking context. - * @param {boolean} forkLeavingPath A flag which shows being in a - * "finally" block. - * @returns {ForkContext} The created context. - */ - pushForkContext(forkLeavingPath) { - this.forkContext = ForkContext.newEmpty( - this.forkContext, - forkLeavingPath, - ); - - return this.forkContext; - } - - /** - * Pops and merges the last forking context. - * @returns {ForkContext} The last context. - */ - popForkContext() { - const lastContext = this.forkContext; - - this.forkContext = lastContext.upper; - this.forkContext.replaceHead(lastContext.makeNext(0, -1)); - - return lastContext; - } - - /** - * Creates a new path. - * @returns {void} - */ - forkPath() { - this.forkContext.add(this.parentForkContext.makeNext(-1, -1)); - } - - /** - * Creates a bypass path. - * This is used for such as IfStatement which does not have "else" chunk. - * @returns {void} - */ - forkBypassPath() { - this.forkContext.add(this.parentForkContext.head); - } - - //-------------------------------------------------------------------------- - // ConditionalExpression, LogicalExpression, IfStatement - //-------------------------------------------------------------------------- - - /** - * Creates a context for ConditionalExpression, LogicalExpression, AssignmentExpression (logical assignments only), - * IfStatement, WhileStatement, DoWhileStatement, or ForStatement. - * - * LogicalExpressions have cases that it goes different paths between the - * `true` case and the `false` case. - * - * For Example: - * - * if (a || b) { - * foo(); - * } else { - * bar(); - * } - * - * In this case, `b` is evaluated always in the code path of the `else` - * block, but it's not so in the code path of the `if` block. - * So there are 3 paths. - * - * a -> foo(); - * a -> b -> foo(); - * a -> b -> bar(); - * @param {string} kind A kind string. - * If the new context is LogicalExpression's or AssignmentExpression's, this is `"&&"` or `"||"` or `"??"`. - * If it's IfStatement's or ConditionalExpression's, this is `"test"`. - * Otherwise, this is `"loop"`. - * @param {boolean} isForkingAsResult Indicates if the result of the choice - * creates a fork. - * @returns {void} - */ - pushChoiceContext(kind, isForkingAsResult) { - this.choiceContext = new ChoiceContext( - this.choiceContext, - kind, - isForkingAsResult, - this.forkContext, - ); - } - - /** - * Pops the last choice context and finalizes it. - * This is called upon leaving a node that represents a choice. - * @throws {Error} (Unreachable.) - * @returns {ChoiceContext} The popped context. - */ - popChoiceContext() { - const poppedChoiceContext = this.choiceContext; - const forkContext = this.forkContext; - const head = forkContext.head; - - this.choiceContext = poppedChoiceContext.upper; - - switch (poppedChoiceContext.kind) { - case "&&": - case "||": - case "??": - /* - * The `head` are the path of the right-hand operand. - * If we haven't previously added segments from child contexts, - * then we add these segments to all possible forks. - */ - if (!poppedChoiceContext.processed) { - poppedChoiceContext.trueForkContext.add(head); - poppedChoiceContext.falseForkContext.add(head); - poppedChoiceContext.nullishForkContext.add(head); - } - - /* - * If this context is the left (test) expression for another choice - * context, such as `a || b` in the expression `a || b || c`, - * then we take the segments for this context and move them up - * to the parent context. - */ - if (poppedChoiceContext.isForkingAsResult) { - const parentContext = this.choiceContext; - - parentContext.trueForkContext.addAll( - poppedChoiceContext.trueForkContext, - ); - parentContext.falseForkContext.addAll( - poppedChoiceContext.falseForkContext, - ); - parentContext.nullishForkContext.addAll( - poppedChoiceContext.nullishForkContext, - ); - parentContext.processed = true; - - // Exit early so we don't collapse all paths into one. - return poppedChoiceContext; - } - - break; - - case "test": - if (!poppedChoiceContext.processed) { - /* - * The head segments are the path of the `if` block here. - * Updates the `true` path with the end of the `if` block. - */ - poppedChoiceContext.trueForkContext.clear(); - poppedChoiceContext.trueForkContext.add(head); - } else { - /* - * The head segments are the path of the `else` block here. - * Updates the `false` path with the end of the `else` - * block. - */ - poppedChoiceContext.falseForkContext.clear(); - poppedChoiceContext.falseForkContext.add(head); - } - - break; - - case "loop": - /* - * Loops are addressed in `popLoopContext()` so just return - * the context without modification. - */ - return poppedChoiceContext; - - /* c8 ignore next */ - default: - throw new Error("unreachable"); - } - - /* - * Merge the true path with the false path to create a single path. - */ - const combinedForkContext = poppedChoiceContext.trueForkContext; - - combinedForkContext.addAll(poppedChoiceContext.falseForkContext); - forkContext.replaceHead(combinedForkContext.makeNext(0, -1)); - - return poppedChoiceContext; - } - - /** - * Creates a code path segment to represent right-hand operand of a logical - * expression. - * This is called in the preprocessing phase when entering a node. - * @throws {Error} (Unreachable.) - * @returns {void} - */ - makeLogicalRight() { - const currentChoiceContext = this.choiceContext; - const forkContext = this.forkContext; - - if (currentChoiceContext.processed) { - /* - * This context was already assigned segments from a child - * choice context. In this case, we are concerned only about - * the path that does not short-circuit and so ends up on the - * right-hand operand of the logical expression. - */ - let prevForkContext; - - switch (currentChoiceContext.kind) { - case "&&": // if true then go to the right-hand side. - prevForkContext = currentChoiceContext.trueForkContext; - break; - case "||": // if false then go to the right-hand side. - prevForkContext = currentChoiceContext.falseForkContext; - break; - case "??": // Both true/false can short-circuit, so needs the third path to go to the right-hand side. That's nullishForkContext. - prevForkContext = currentChoiceContext.nullishForkContext; - break; - default: - throw new Error("unreachable"); - } - - /* - * Create the segment for the right-hand operand of the logical expression - * and adjust the fork context pointer to point there. The right-hand segment - * is added at the end of all segments in `prevForkContext`. - */ - forkContext.replaceHead(prevForkContext.makeNext(0, -1)); - - /* - * We no longer need this list of segments. - * - * Reset `processed` because we've removed the segments from the child - * choice context. This allows `popChoiceContext()` to continue adding - * segments later. - */ - prevForkContext.clear(); - currentChoiceContext.processed = false; - } else { - /* - * This choice context was not assigned segments from a child - * choice context, which means that it's a terminal logical - * expression. - * - * `head` is the segments for the left-hand operand of the - * logical expression. - * - * Each of the fork contexts below are empty at this point. We choose - * the path(s) that will short-circuit and add the segment for the - * left-hand operand to it. Ultimately, this will be the only segment - * in that path due to the short-circuting, so we are just seeding - * these paths to start. - */ - switch (currentChoiceContext.kind) { - case "&&": - /* - * In most contexts, when a && expression evaluates to false, - * it short circuits, so we need to account for that by setting - * the `falseForkContext` to the left operand. - * - * When a && expression is the left-hand operand for a ?? - * expression, such as `(a && b) ?? c`, a nullish value will - * also short-circuit in a different way than a false value, - * so we also set the `nullishForkContext` to the left operand. - * This path is only used with a ?? expression and is thrown - * away for any other type of logical expression, so it's safe - * to always add. - */ - currentChoiceContext.falseForkContext.add(forkContext.head); - currentChoiceContext.nullishForkContext.add( - forkContext.head, - ); - break; - case "||": // the true path can short-circuit. - currentChoiceContext.trueForkContext.add(forkContext.head); - break; - case "??": // both can short-circuit. - currentChoiceContext.trueForkContext.add(forkContext.head); - currentChoiceContext.falseForkContext.add(forkContext.head); - break; - default: - throw new Error("unreachable"); - } - - /* - * Create the segment for the right-hand operand of the logical expression - * and adjust the fork context pointer to point there. - */ - forkContext.replaceHead(forkContext.makeNext(-1, -1)); - } - } - - /** - * Makes a code path segment of the `if` block. - * @returns {void} - */ - makeIfConsequent() { - const context = this.choiceContext; - const forkContext = this.forkContext; - - /* - * If any result were not transferred from child contexts, - * this sets the head segments to both cases. - * The head segments are the path of the test expression. - */ - if (!context.processed) { - context.trueForkContext.add(forkContext.head); - context.falseForkContext.add(forkContext.head); - context.nullishForkContext.add(forkContext.head); - } - - context.processed = false; - - // Creates new path from the `true` case. - forkContext.replaceHead(context.trueForkContext.makeNext(0, -1)); - } - - /** - * Makes a code path segment of the `else` block. - * @returns {void} - */ - makeIfAlternate() { - const context = this.choiceContext; - const forkContext = this.forkContext; - - /* - * The head segments are the path of the `if` block. - * Updates the `true` path with the end of the `if` block. - */ - context.trueForkContext.clear(); - context.trueForkContext.add(forkContext.head); - context.processed = true; - - // Creates new path from the `false` case. - forkContext.replaceHead(context.falseForkContext.makeNext(0, -1)); - } - - //-------------------------------------------------------------------------- - // ChainExpression - //-------------------------------------------------------------------------- - - /** - * Pushes a new `ChainExpression` context to the stack. This method is - * called when entering a `ChainExpression` node. A chain context is used to - * count forking in the optional chain then merge them on the exiting from the - * `ChainExpression` node. - * @returns {void} - */ - pushChainContext() { - this.chainContext = new ChainContext(this.chainContext); - } - - /** - * Pop a `ChainExpression` context from the stack. This method is called on - * exiting from each `ChainExpression` node. This merges all forks of the - * last optional chaining. - * @returns {void} - */ - popChainContext() { - const context = this.chainContext; - - this.chainContext = context.upper; - - // pop all choice contexts of this. - for (let i = context.choiceContextCount; i > 0; --i) { - this.popChoiceContext(); - } - } - - /** - * Create a choice context for optional access. - * This method is called on entering to each `(Call|Member)Expression[optional=true]` node. - * This creates a choice context as similar to `LogicalExpression[operator="??"]` node. - * @returns {void} - */ - makeOptionalNode() { - if (this.chainContext) { - this.chainContext.choiceContextCount += 1; - this.pushChoiceContext("??", false); - } - } - - /** - * Create a fork. - * This method is called on entering to the `arguments|property` property of each `(Call|Member)Expression` node. - * @returns {void} - */ - makeOptionalRight() { - if (this.chainContext) { - this.makeLogicalRight(); - } - } - - //-------------------------------------------------------------------------- - // SwitchStatement - //-------------------------------------------------------------------------- - - /** - * Creates a context object of SwitchStatement and stacks it. - * @param {boolean} hasCase `true` if the switch statement has one or more - * case parts. - * @param {string|null} label The label text. - * @returns {void} - */ - pushSwitchContext(hasCase, label) { - this.switchContext = new SwitchContext(this.switchContext, hasCase); - this.pushBreakContext(true, label); - } - - /** - * Pops the last context of SwitchStatement and finalizes it. - * - * - Disposes all forking stack for `case` and `default`. - * - Creates the next code path segment from `context.brokenForkContext`. - * - If the last `SwitchCase` node is not a `default` part, creates a path - * to the `default` body. - * @returns {void} - */ - popSwitchContext() { - const context = this.switchContext; - - this.switchContext = context.upper; - - const forkContext = this.forkContext; - const brokenForkContext = this.popBreakContext().brokenForkContext; - - if (context.forkCount === 0) { - /* - * When there is only one `default` chunk and there is one or more - * `break` statements, even if forks are nothing, it needs to merge - * those. - */ - if (!brokenForkContext.empty) { - brokenForkContext.add(forkContext.makeNext(-1, -1)); - forkContext.replaceHead(brokenForkContext.makeNext(0, -1)); - } - - return; - } - - const lastSegments = forkContext.head; - - this.forkBypassPath(); - const lastCaseSegments = forkContext.head; - - /* - * `brokenForkContext` is used to make the next segment. - * It must add the last segment into `brokenForkContext`. - */ - brokenForkContext.add(lastSegments); - - /* - * Any value that doesn't match a `case` test should flow to the default - * case. That happens normally when the default case is last in the `switch`, - * but if it's not, we need to rewire some of the paths to be correct. - */ - if (!context.lastIsDefault) { - if (context.defaultBodySegments) { - /* - * There is a non-empty default case, so remove the path from the `default` - * label to its body for an accurate representation. - */ - disconnectSegments( - context.defaultSegments, - context.defaultBodySegments, - ); - - /* - * Connect the path from the last non-default case to the body of the - * default case. - */ - makeLooped(this, lastCaseSegments, context.defaultBodySegments); - } else { - /* - * There is no default case, so we treat this as if the last case - * had a `break` in it. - */ - brokenForkContext.add(lastCaseSegments); - } - } - - // Traverse up to the original fork context for the `switch` statement - for (let i = 0; i < context.forkCount; ++i) { - this.forkContext = this.forkContext.upper; - } - - /* - * Creates a path from all `brokenForkContext` paths. - * This is a path after `switch` statement. - */ - this.forkContext.replaceHead(brokenForkContext.makeNext(0, -1)); - } - - /** - * Makes a code path segment for a `SwitchCase` node. - * @param {boolean} isCaseBodyEmpty `true` if the body is empty. - * @param {boolean} isDefaultCase `true` if the body is the default case. - * @returns {void} - */ - makeSwitchCaseBody(isCaseBodyEmpty, isDefaultCase) { - const context = this.switchContext; - - if (!context.hasCase) { - return; - } - - /* - * Merge forks. - * The parent fork context has two segments. - * Those are from the current `case` and the body of the previous case. - */ - const parentForkContext = this.forkContext; - const forkContext = this.pushForkContext(); - - forkContext.add(parentForkContext.makeNext(0, -1)); - - /* - * Add information about the default case. - * - * The purpose of this is to identify the starting segments for the - * default case to make sure there is a path there. - */ - if (isDefaultCase) { - /* - * This is the default case in the `switch`. - * - * We first save the current pointer as `defaultSegments` to point - * to the `default` keyword. - */ - context.defaultSegments = parentForkContext.head; - - /* - * If the body of the case is empty then we just set - * `foundEmptyDefault` to true; otherwise, we save a reference - * to the current pointer as `defaultBodySegments`. - */ - if (isCaseBodyEmpty) { - context.foundEmptyDefault = true; - } else { - context.defaultBodySegments = forkContext.head; - } - } else { - /* - * This is not the default case in the `switch`. - * - * If it's not empty and there is already an empty default case found, - * that means the default case actually comes before this case, - * and that it will fall through to this case. So, we can now - * ignore the previous default case (reset `foundEmptyDefault` to false) - * and set `defaultBodySegments` to the current segments because this is - * effectively the new default case. - */ - if (!isCaseBodyEmpty && context.foundEmptyDefault) { - context.foundEmptyDefault = false; - context.defaultBodySegments = forkContext.head; - } - } - - // keep track if the default case ends up last - context.lastIsDefault = isDefaultCase; - context.forkCount += 1; - } - - //-------------------------------------------------------------------------- - // TryStatement - //-------------------------------------------------------------------------- - - /** - * Creates a context object of TryStatement and stacks it. - * @param {boolean} hasFinalizer `true` if the try statement has a - * `finally` block. - * @returns {void} - */ - pushTryContext(hasFinalizer) { - this.tryContext = new TryContext( - this.tryContext, - hasFinalizer, - this.forkContext, - ); - } - - /** - * Pops the last context of TryStatement and finalizes it. - * @returns {void} - */ - popTryContext() { - const context = this.tryContext; - - this.tryContext = context.upper; - - /* - * If we're inside the `catch` block, that means there is no `finally`, - * so we can process the `try` and `catch` blocks the simple way and - * merge their two paths. - */ - if (context.position === "catch") { - this.popForkContext(); - return; - } - - /* - * The following process is executed only when there is a `finally` - * block. - */ - - const originalReturnedForkContext = context.returnedForkContext; - const originalThrownForkContext = context.thrownForkContext; - - // no `return` or `throw` in `try` or `catch` so there's nothing left to do - if ( - originalReturnedForkContext.empty && - originalThrownForkContext.empty - ) { - return; - } - - /* - * The following process is executed only when there is a `finally` - * block and there was a `return` or `throw` in the `try` or `catch` - * blocks. - */ - - // Separate head to normal paths and leaving paths. - const headSegments = this.forkContext.head; - - this.forkContext = this.forkContext.upper; - const normalSegments = headSegments.slice( - 0, - (headSegments.length / 2) | 0, - ); - const leavingSegments = headSegments.slice( - (headSegments.length / 2) | 0, - ); - - // Forwards the leaving path to upper contexts. - if (!originalReturnedForkContext.empty) { - getReturnContext(this).returnedForkContext.add(leavingSegments); - } - if (!originalThrownForkContext.empty) { - getThrowContext(this).thrownForkContext.add(leavingSegments); - } - - // Sets the normal path as the next. - this.forkContext.replaceHead(normalSegments); - - /* - * If both paths of the `try` block and the `catch` block are - * unreachable, the next path becomes unreachable as well. - */ - if (!context.lastOfTryIsReachable && !context.lastOfCatchIsReachable) { - this.forkContext.makeUnreachable(); - } - } - - /** - * Makes a code path segment for a `catch` block. - * @returns {void} - */ - makeCatchBlock() { - const context = this.tryContext; - const forkContext = this.forkContext; - const originalThrownForkContext = context.thrownForkContext; - - /* - * We are now in a catch block so we need to update the context - * with that information. This includes creating a new fork - * context in case we encounter any `throw` statements here. - */ - context.position = "catch"; - context.thrownForkContext = ForkContext.newEmpty(forkContext); - context.lastOfTryIsReachable = forkContext.reachable; - - // Merge the thrown paths from the `try` and `catch` blocks - originalThrownForkContext.add(forkContext.head); - const thrownSegments = originalThrownForkContext.makeNext(0, -1); - - // Fork to a bypass and the merged thrown path. - this.pushForkContext(); - this.forkBypassPath(); - this.forkContext.add(thrownSegments); - } - - /** - * Makes a code path segment for a `finally` block. - * - * In the `finally` block, parallel paths are created. The parallel paths - * are used as leaving-paths. The leaving-paths are paths from `return` - * statements and `throw` statements in a `try` block or a `catch` block. - * @returns {void} - */ - makeFinallyBlock() { - const context = this.tryContext; - let forkContext = this.forkContext; - const originalReturnedForkContext = context.returnedForkContext; - const originalThrownForContext = context.thrownForkContext; - const headOfLeavingSegments = forkContext.head; - - // Update state. - if (context.position === "catch") { - // Merges two paths from the `try` block and `catch` block. - this.popForkContext(); - forkContext = this.forkContext; - - context.lastOfCatchIsReachable = forkContext.reachable; - } else { - context.lastOfTryIsReachable = forkContext.reachable; - } - - context.position = "finally"; - - /* - * If there was no `return` or `throw` in either the `try` or `catch` - * blocks, then there's no further code paths to create for `finally`. - */ - if ( - originalReturnedForkContext.empty && - originalThrownForContext.empty - ) { - // This path does not leave. - return; - } - - /* - * Create a parallel segment from merging returned and thrown. - * This segment will leave at the end of this `finally` block. - */ - const segments = forkContext.makeNext(-1, -1); - - for (let i = 0; i < forkContext.count; ++i) { - const prevSegsOfLeavingSegment = [headOfLeavingSegments[i]]; - - for ( - let j = 0; - j < originalReturnedForkContext.segmentsList.length; - ++j - ) { - prevSegsOfLeavingSegment.push( - originalReturnedForkContext.segmentsList[j][i], - ); - } - for ( - let j = 0; - j < originalThrownForContext.segmentsList.length; - ++j - ) { - prevSegsOfLeavingSegment.push( - originalThrownForContext.segmentsList[j][i], - ); - } - - segments.push( - CodePathSegment.newNext( - this.idGenerator.next(), - prevSegsOfLeavingSegment, - ), - ); - } - - this.pushForkContext(true); - this.forkContext.add(segments); - } - - /** - * Makes a code path segment from the first throwable node to the `catch` - * block or the `finally` block. - * @returns {void} - */ - makeFirstThrowablePathInTryBlock() { - const forkContext = this.forkContext; - - if (!forkContext.reachable) { - return; - } - - const context = getThrowContext(this); - - if ( - context === this || - context.position !== "try" || - !context.thrownForkContext.empty - ) { - return; - } - - context.thrownForkContext.add(forkContext.head); - forkContext.replaceHead(forkContext.makeNext(-1, -1)); - } - - //-------------------------------------------------------------------------- - // Loop Statements - //-------------------------------------------------------------------------- - - /** - * Creates a context object of a loop statement and stacks it. - * @param {string} type The type of the node which was triggered. One of - * `WhileStatement`, `DoWhileStatement`, `ForStatement`, `ForInStatement`, - * and `ForStatement`. - * @param {string|null} label A label of the node which was triggered. - * @throws {Error} (Unreachable - unknown type.) - * @returns {void} - */ - pushLoopContext(type, label) { - const forkContext = this.forkContext; - - // All loops need a path to account for `break` statements - const breakContext = this.pushBreakContext(true, label); - - switch (type) { - case "WhileStatement": - this.pushChoiceContext("loop", false); - this.loopContext = new WhileLoopContext( - this.loopContext, - label, - breakContext, - ); - break; - - case "DoWhileStatement": - this.pushChoiceContext("loop", false); - this.loopContext = new DoWhileLoopContext( - this.loopContext, - label, - breakContext, - forkContext, - ); - break; - - case "ForStatement": - this.pushChoiceContext("loop", false); - this.loopContext = new ForLoopContext( - this.loopContext, - label, - breakContext, - ); - break; - - case "ForInStatement": - this.loopContext = new ForInLoopContext( - this.loopContext, - label, - breakContext, - ); - break; - - case "ForOfStatement": - this.loopContext = new ForOfLoopContext( - this.loopContext, - label, - breakContext, - ); - break; - - /* c8 ignore next */ - default: - throw new Error(`unknown type: "${type}"`); - } - } - - /** - * Pops the last context of a loop statement and finalizes it. - * @throws {Error} (Unreachable - unknown type.) - * @returns {void} - */ - popLoopContext() { - const context = this.loopContext; - - this.loopContext = context.upper; - - const forkContext = this.forkContext; - const brokenForkContext = this.popBreakContext().brokenForkContext; - - // Creates a looped path. - switch (context.type) { - case "WhileStatement": - case "ForStatement": - this.popChoiceContext(); - - /* - * Creates the path from the end of the loop body up to the - * location where `continue` would jump to. - */ - makeLooped( - this, - forkContext.head, - context.continueDestSegments, - ); - break; - - case "DoWhileStatement": { - const choiceContext = this.popChoiceContext(); - - if (!choiceContext.processed) { - choiceContext.trueForkContext.add(forkContext.head); - choiceContext.falseForkContext.add(forkContext.head); - } - - /* - * If this isn't a hardcoded `true` condition, then `break` - * should continue down the path as if the condition evaluated - * to false. - */ - if (context.test !== true) { - brokenForkContext.addAll(choiceContext.falseForkContext); - } - - /* - * When the condition is true, the loop continues back to the top, - * so create a path from each possible true condition back to the - * top of the loop. - */ - const segmentsList = choiceContext.trueForkContext.segmentsList; - - for (let i = 0; i < segmentsList.length; ++i) { - makeLooped(this, segmentsList[i], context.entrySegments); - } - break; - } - - case "ForInStatement": - case "ForOfStatement": - brokenForkContext.add(forkContext.head); - - /* - * Creates the path from the end of the loop body up to the - * left expression (left of `in` or `of`) of the loop. - */ - makeLooped(this, forkContext.head, context.leftSegments); - break; - - /* c8 ignore next */ - default: - throw new Error("unreachable"); - } - - /* - * If there wasn't a `break` statement in the loop, then we're at - * the end of the loop's path, so we make an unreachable segment - * to mark that. - * - * If there was a `break` statement, then we continue on into the - * `brokenForkContext`. - */ - if (brokenForkContext.empty) { - forkContext.replaceHead(forkContext.makeUnreachable(-1, -1)); - } else { - forkContext.replaceHead(brokenForkContext.makeNext(0, -1)); - } - } - - /** - * Makes a code path segment for the test part of a WhileStatement. - * @param {boolean|undefined} test The test value (only when constant). - * @returns {void} - */ - makeWhileTest(test) { - const context = this.loopContext; - const forkContext = this.forkContext; - const testSegments = forkContext.makeNext(0, -1); - - // Update state. - context.test = test; - context.continueDestSegments = testSegments; - forkContext.replaceHead(testSegments); - } - - /** - * Makes a code path segment for the body part of a WhileStatement. - * @returns {void} - */ - makeWhileBody() { - const context = this.loopContext; - const choiceContext = this.choiceContext; - const forkContext = this.forkContext; - - if (!choiceContext.processed) { - choiceContext.trueForkContext.add(forkContext.head); - choiceContext.falseForkContext.add(forkContext.head); - } - - /* - * If this isn't a hardcoded `true` condition, then `break` - * should continue down the path as if the condition evaluated - * to false. - */ - if (context.test !== true) { - context.brokenForkContext.addAll(choiceContext.falseForkContext); - } - forkContext.replaceHead(choiceContext.trueForkContext.makeNext(0, -1)); - } - - /** - * Makes a code path segment for the body part of a DoWhileStatement. - * @returns {void} - */ - makeDoWhileBody() { - const context = this.loopContext; - const forkContext = this.forkContext; - const bodySegments = forkContext.makeNext(-1, -1); - - // Update state. - context.entrySegments = bodySegments; - forkContext.replaceHead(bodySegments); - } - - /** - * Makes a code path segment for the test part of a DoWhileStatement. - * @param {boolean|undefined} test The test value (only when constant). - * @returns {void} - */ - makeDoWhileTest(test) { - const context = this.loopContext; - const forkContext = this.forkContext; - - context.test = test; - - /* - * If there is a `continue` statement in the loop then `continueForkContext` - * won't be empty. We wire up the path from `continue` to the loop - * test condition and then continue the traversal in the root fork context. - */ - if (!context.continueForkContext.empty) { - context.continueForkContext.add(forkContext.head); - const testSegments = context.continueForkContext.makeNext(0, -1); - - forkContext.replaceHead(testSegments); - } - } - - /** - * Makes a code path segment for the test part of a ForStatement. - * @param {boolean|undefined} test The test value (only when constant). - * @returns {void} - */ - makeForTest(test) { - const context = this.loopContext; - const forkContext = this.forkContext; - const endOfInitSegments = forkContext.head; - const testSegments = forkContext.makeNext(-1, -1); - - /* - * Update the state. - * - * The `continueDestSegments` are set to `testSegments` because we - * don't yet know if there is an update expression in this loop. So, - * from what we already know at this point, a `continue` statement - * will jump back to the test expression. - */ - context.test = test; - context.endOfInitSegments = endOfInitSegments; - context.continueDestSegments = context.testSegments = testSegments; - forkContext.replaceHead(testSegments); - } - - /** - * Makes a code path segment for the update part of a ForStatement. - * @returns {void} - */ - makeForUpdate() { - const context = this.loopContext; - const choiceContext = this.choiceContext; - const forkContext = this.forkContext; - - // Make the next paths of the test. - if (context.testSegments) { - finalizeTestSegmentsOfFor(context, choiceContext, forkContext.head); - } else { - context.endOfInitSegments = forkContext.head; - } - - /* - * Update the state. - * - * The `continueDestSegments` are now set to `updateSegments` because we - * know there is an update expression in this loop. So, a `continue` statement - * in the loop will jump to the update expression first, and then to any - * test expression the loop might have. - */ - const updateSegments = forkContext.makeDisconnected(-1, -1); - - context.continueDestSegments = context.updateSegments = updateSegments; - forkContext.replaceHead(updateSegments); - } - - /** - * Makes a code path segment for the body part of a ForStatement. - * @returns {void} - */ - makeForBody() { - const context = this.loopContext; - const choiceContext = this.choiceContext; - const forkContext = this.forkContext; - - /* - * Determine what to do based on which part of the `for` loop are present. - * 1. If there is an update expression, then `updateSegments` is not null and - * we need to assign `endOfUpdateSegments`, and if there is a test - * expression, we then need to create the looped path to get back to - * the test condition. - * 2. If there is no update expression but there is a test expression, - * then we only need to update the test segment information. - * 3. If there is no update expression and no test expression, then we - * just save `endOfInitSegments`. - */ - if (context.updateSegments) { - context.endOfUpdateSegments = forkContext.head; - - /* - * In a `for` loop that has both an update expression and a test - * condition, execution flows from the test expression into the - * loop body, to the update expression, and then back to the test - * expression to determine if the loop should continue. - * - * To account for that, we need to make a path from the end of the - * update expression to the start of the test expression. This is - * effectively what creates the loop in the code path. - */ - if (context.testSegments) { - makeLooped( - this, - context.endOfUpdateSegments, - context.testSegments, - ); - } - } else if (context.testSegments) { - finalizeTestSegmentsOfFor(context, choiceContext, forkContext.head); - } else { - context.endOfInitSegments = forkContext.head; - } - - let bodySegments = context.endOfTestSegments; - - /* - * If there is a test condition, then there `endOfTestSegments` is also - * the start of the loop body. If there isn't a test condition then - * `bodySegments` will be null and we need to look elsewhere to find - * the start of the body. - * - * The body starts at the end of the init expression and ends at the end - * of the update expression, so we use those locations to determine the - * body segments. - */ - if (!bodySegments) { - const prevForkContext = ForkContext.newEmpty(forkContext); - - prevForkContext.add(context.endOfInitSegments); - if (context.endOfUpdateSegments) { - prevForkContext.add(context.endOfUpdateSegments); - } - - bodySegments = prevForkContext.makeNext(0, -1); - } - - /* - * If there was no test condition and no update expression, then - * `continueDestSegments` will be null. In that case, a - * `continue` should skip directly to the body of the loop. - * Otherwise, we want to keep the current `continueDestSegments`. - */ - context.continueDestSegments = - context.continueDestSegments || bodySegments; - - // move pointer to the body - forkContext.replaceHead(bodySegments); - } - - /** - * Makes a code path segment for the left part of a ForInStatement and a - * ForOfStatement. - * @returns {void} - */ - makeForInOfLeft() { - const context = this.loopContext; - const forkContext = this.forkContext; - const leftSegments = forkContext.makeDisconnected(-1, -1); - - // Update state. - context.prevSegments = forkContext.head; - context.leftSegments = context.continueDestSegments = leftSegments; - forkContext.replaceHead(leftSegments); - } - - /** - * Makes a code path segment for the right part of a ForInStatement and a - * ForOfStatement. - * @returns {void} - */ - makeForInOfRight() { - const context = this.loopContext; - const forkContext = this.forkContext; - const temp = ForkContext.newEmpty(forkContext); - - temp.add(context.prevSegments); - const rightSegments = temp.makeNext(-1, -1); - - // Update state. - context.endOfLeftSegments = forkContext.head; - forkContext.replaceHead(rightSegments); - } - - /** - * Makes a code path segment for the body part of a ForInStatement and a - * ForOfStatement. - * @returns {void} - */ - makeForInOfBody() { - const context = this.loopContext; - const forkContext = this.forkContext; - const temp = ForkContext.newEmpty(forkContext); - - temp.add(context.endOfLeftSegments); - const bodySegments = temp.makeNext(-1, -1); - - // Make a path: `right` -> `left`. - makeLooped(this, forkContext.head, context.leftSegments); - - // Update state. - context.brokenForkContext.add(forkContext.head); - forkContext.replaceHead(bodySegments); - } - - //-------------------------------------------------------------------------- - // Control Statements - //-------------------------------------------------------------------------- - - /** - * Creates new context in which a `break` statement can be used. This occurs inside of a loop, - * labeled statement, or switch statement. - * @param {boolean} breakable Indicates if we are inside a statement where - * `break` without a label will exit the statement. - * @param {string|null} label The label associated with the statement. - * @returns {BreakContext} The new context. - */ - pushBreakContext(breakable, label) { - this.breakContext = new BreakContext( - this.breakContext, - breakable, - label, - this.forkContext, - ); - return this.breakContext; - } - - /** - * Removes the top item of the break context stack. - * @returns {Object} The removed context. - */ - popBreakContext() { - const context = this.breakContext; - const forkContext = this.forkContext; - - this.breakContext = context.upper; - - // Process this context here for other than switches and loops. - if (!context.breakable) { - const brokenForkContext = context.brokenForkContext; - - if (!brokenForkContext.empty) { - brokenForkContext.add(forkContext.head); - forkContext.replaceHead(brokenForkContext.makeNext(0, -1)); - } - } - - return context; - } - - /** - * Makes a path for a `break` statement. - * - * It registers the head segment to a context of `break`. - * It makes new unreachable segment, then it set the head with the segment. - * @param {string|null} label A label of the break statement. - * @returns {void} - */ - makeBreak(label) { - const forkContext = this.forkContext; - - if (!forkContext.reachable) { - return; - } - - const context = getBreakContext(this, label); - - if (context) { - context.brokenForkContext.add(forkContext.head); - } - - /* c8 ignore next */ - forkContext.replaceHead(forkContext.makeUnreachable(-1, -1)); - } - - /** - * Makes a path for a `continue` statement. - * - * It makes a looping path. - * It makes new unreachable segment, then it set the head with the segment. - * @param {string|null} label A label of the continue statement. - * @returns {void} - */ - makeContinue(label) { - const forkContext = this.forkContext; - - if (!forkContext.reachable) { - return; - } - - const context = getContinueContext(this, label); - - if (context) { - if (context.continueDestSegments) { - makeLooped( - this, - forkContext.head, - context.continueDestSegments, - ); - - // If the context is a for-in/of loop, this affects a break also. - if ( - context.type === "ForInStatement" || - context.type === "ForOfStatement" - ) { - context.brokenForkContext.add(forkContext.head); - } - } else { - context.continueForkContext.add(forkContext.head); - } - } - forkContext.replaceHead(forkContext.makeUnreachable(-1, -1)); - } - - /** - * Makes a path for a `return` statement. - * - * It registers the head segment to a context of `return`. - * It makes new unreachable segment, then it set the head with the segment. - * @returns {void} - */ - makeReturn() { - const forkContext = this.forkContext; - - if (forkContext.reachable) { - getReturnContext(this).returnedForkContext.add(forkContext.head); - forkContext.replaceHead(forkContext.makeUnreachable(-1, -1)); - } - } - - /** - * Makes a path for a `throw` statement. - * - * It registers the head segment to a context of `throw`. - * It makes new unreachable segment, then it set the head with the segment. - * @returns {void} - */ - makeThrow() { - const forkContext = this.forkContext; - - if (forkContext.reachable) { - getThrowContext(this).thrownForkContext.add(forkContext.head); - forkContext.replaceHead(forkContext.makeUnreachable(-1, -1)); - } - } - - /** - * Makes the final path. - * @returns {void} - */ - makeFinal() { - const segments = this.currentSegments; - - if (segments.length > 0 && segments[0].reachable) { - this.returnedForkContext.add(segments); - } - } + + /** + * Creates a new instance. + * @param {IdGenerator} idGenerator An id generator to generate id for code + * path segments. + * @param {Function} onLooped A callback function to notify looping. + */ + constructor(idGenerator, onLooped) { + + /** + * The ID generator to use when creating new segments. + * @type {IdGenerator} + */ + this.idGenerator = idGenerator; + + /** + * A callback function to call when there is a loop. + * @type {Function} + */ + this.notifyLooped = onLooped; + + /** + * The root fork context for this state. + * @type {ForkContext} + */ + this.forkContext = ForkContext.newRoot(idGenerator); + + /** + * Context for logical expressions, conditional expressions, `if` statements, + * and loops. + * @type {ChoiceContext} + */ + this.choiceContext = null; + + /** + * Context for `switch` statements. + * @type {SwitchContext} + */ + this.switchContext = null; + + /** + * Context for `try` statements. + * @type {TryContext} + */ + this.tryContext = null; + + /** + * Context for loop statements. + * @type {LoopContext} + */ + this.loopContext = null; + + /** + * Context for `break` statements. + * @type {BreakContext} + */ + this.breakContext = null; + + /** + * Context for `ChainExpression` nodes. + * @type {ChainContext} + */ + this.chainContext = null; + + /** + * An array that tracks the current segments in the state. The array + * starts empty and segments are added with each `onCodePathSegmentStart` + * event and removed with each `onCodePathSegmentEnd` event. Effectively, + * this is tracking the code path segment traversal as the state is + * modified. + * @type {Array} + */ + this.currentSegments = []; + + /** + * Tracks the starting segment for this path. This value never changes. + * @type {CodePathSegment} + */ + this.initialSegment = this.forkContext.head[0]; + + /** + * The final segments of the code path which are either `return` or `throw`. + * This is a union of the segments in `returnedForkContext` and `thrownForkContext`. + * @type {Array} + */ + this.finalSegments = []; + + /** + * The final segments of the code path which are `return`. These + * segments are also contained in `finalSegments`. + * @type {Array} + */ + this.returnedForkContext = []; + + /** + * The final segments of the code path which are `throw`. These + * segments are also contained in `finalSegments`. + * @type {Array} + */ + this.thrownForkContext = []; + + /* + * We add an `add` method so that these look more like fork contexts and + * can be used interchangeably when a fork context is needed to add more + * segments to a path. + * + * Ultimately, we want anything added to `returned` or `thrown` to also + * be added to `final`. We only add reachable and used segments to these + * arrays. + */ + const final = this.finalSegments; + const returned = this.returnedForkContext; + const thrown = this.thrownForkContext; + + returned.add = addToReturnedOrThrown.bind(null, returned, thrown, final); + thrown.add = addToReturnedOrThrown.bind(null, thrown, returned, final); + } + + /** + * A passthrough property exposing the current pointer as part of the API. + * @type {CodePathSegment[]} + */ + get headSegments() { + return this.forkContext.head; + } + + /** + * The parent forking context. + * This is used for the root of new forks. + * @type {ForkContext} + */ + get parentForkContext() { + const current = this.forkContext; + + return current && current.upper; + } + + /** + * Creates and stacks new forking context. + * @param {boolean} forkLeavingPath A flag which shows being in a + * "finally" block. + * @returns {ForkContext} The created context. + */ + pushForkContext(forkLeavingPath) { + this.forkContext = ForkContext.newEmpty( + this.forkContext, + forkLeavingPath + ); + + return this.forkContext; + } + + /** + * Pops and merges the last forking context. + * @returns {ForkContext} The last context. + */ + popForkContext() { + const lastContext = this.forkContext; + + this.forkContext = lastContext.upper; + this.forkContext.replaceHead(lastContext.makeNext(0, -1)); + + return lastContext; + } + + /** + * Creates a new path. + * @returns {void} + */ + forkPath() { + this.forkContext.add(this.parentForkContext.makeNext(-1, -1)); + } + + /** + * Creates a bypass path. + * This is used for such as IfStatement which does not have "else" chunk. + * @returns {void} + */ + forkBypassPath() { + this.forkContext.add(this.parentForkContext.head); + } + + //-------------------------------------------------------------------------- + // ConditionalExpression, LogicalExpression, IfStatement + //-------------------------------------------------------------------------- + + /** + * Creates a context for ConditionalExpression, LogicalExpression, AssignmentExpression (logical assignments only), + * IfStatement, WhileStatement, DoWhileStatement, or ForStatement. + * + * LogicalExpressions have cases that it goes different paths between the + * `true` case and the `false` case. + * + * For Example: + * + * if (a || b) { + * foo(); + * } else { + * bar(); + * } + * + * In this case, `b` is evaluated always in the code path of the `else` + * block, but it's not so in the code path of the `if` block. + * So there are 3 paths. + * + * a -> foo(); + * a -> b -> foo(); + * a -> b -> bar(); + * @param {string} kind A kind string. + * If the new context is LogicalExpression's or AssignmentExpression's, this is `"&&"` or `"||"` or `"??"`. + * If it's IfStatement's or ConditionalExpression's, this is `"test"`. + * Otherwise, this is `"loop"`. + * @param {boolean} isForkingAsResult Indicates if the result of the choice + * creates a fork. + * @returns {void} + */ + pushChoiceContext(kind, isForkingAsResult) { + this.choiceContext = new ChoiceContext(this.choiceContext, kind, isForkingAsResult, this.forkContext); + } + + /** + * Pops the last choice context and finalizes it. + * This is called upon leaving a node that represents a choice. + * @throws {Error} (Unreachable.) + * @returns {ChoiceContext} The popped context. + */ + popChoiceContext() { + const poppedChoiceContext = this.choiceContext; + const forkContext = this.forkContext; + const head = forkContext.head; + + this.choiceContext = poppedChoiceContext.upper; + + switch (poppedChoiceContext.kind) { + case "&&": + case "||": + case "??": + + /* + * The `head` are the path of the right-hand operand. + * If we haven't previously added segments from child contexts, + * then we add these segments to all possible forks. + */ + if (!poppedChoiceContext.processed) { + poppedChoiceContext.trueForkContext.add(head); + poppedChoiceContext.falseForkContext.add(head); + poppedChoiceContext.nullishForkContext.add(head); + } + + /* + * If this context is the left (test) expression for another choice + * context, such as `a || b` in the expression `a || b || c`, + * then we take the segments for this context and move them up + * to the parent context. + */ + if (poppedChoiceContext.isForkingAsResult) { + const parentContext = this.choiceContext; + + parentContext.trueForkContext.addAll(poppedChoiceContext.trueForkContext); + parentContext.falseForkContext.addAll(poppedChoiceContext.falseForkContext); + parentContext.nullishForkContext.addAll(poppedChoiceContext.nullishForkContext); + parentContext.processed = true; + + // Exit early so we don't collapse all paths into one. + return poppedChoiceContext; + } + + break; + + case "test": + if (!poppedChoiceContext.processed) { + + /* + * The head segments are the path of the `if` block here. + * Updates the `true` path with the end of the `if` block. + */ + poppedChoiceContext.trueForkContext.clear(); + poppedChoiceContext.trueForkContext.add(head); + } else { + + /* + * The head segments are the path of the `else` block here. + * Updates the `false` path with the end of the `else` + * block. + */ + poppedChoiceContext.falseForkContext.clear(); + poppedChoiceContext.falseForkContext.add(head); + } + + break; + + case "loop": + + /* + * Loops are addressed in `popLoopContext()` so just return + * the context without modification. + */ + return poppedChoiceContext; + + /* c8 ignore next */ + default: + throw new Error("unreachable"); + } + + /* + * Merge the true path with the false path to create a single path. + */ + const combinedForkContext = poppedChoiceContext.trueForkContext; + + combinedForkContext.addAll(poppedChoiceContext.falseForkContext); + forkContext.replaceHead(combinedForkContext.makeNext(0, -1)); + + return poppedChoiceContext; + } + + /** + * Creates a code path segment to represent right-hand operand of a logical + * expression. + * This is called in the preprocessing phase when entering a node. + * @throws {Error} (Unreachable.) + * @returns {void} + */ + makeLogicalRight() { + const currentChoiceContext = this.choiceContext; + const forkContext = this.forkContext; + + if (currentChoiceContext.processed) { + + /* + * This context was already assigned segments from a child + * choice context. In this case, we are concerned only about + * the path that does not short-circuit and so ends up on the + * right-hand operand of the logical expression. + */ + let prevForkContext; + + switch (currentChoiceContext.kind) { + case "&&": // if true then go to the right-hand side. + prevForkContext = currentChoiceContext.trueForkContext; + break; + case "||": // if false then go to the right-hand side. + prevForkContext = currentChoiceContext.falseForkContext; + break; + case "??": // Both true/false can short-circuit, so needs the third path to go to the right-hand side. That's nullishForkContext. + prevForkContext = currentChoiceContext.nullishForkContext; + break; + default: + throw new Error("unreachable"); + } + + /* + * Create the segment for the right-hand operand of the logical expression + * and adjust the fork context pointer to point there. The right-hand segment + * is added at the end of all segments in `prevForkContext`. + */ + forkContext.replaceHead(prevForkContext.makeNext(0, -1)); + + /* + * We no longer need this list of segments. + * + * Reset `processed` because we've removed the segments from the child + * choice context. This allows `popChoiceContext()` to continue adding + * segments later. + */ + prevForkContext.clear(); + currentChoiceContext.processed = false; + + } else { + + /* + * This choice context was not assigned segments from a child + * choice context, which means that it's a terminal logical + * expression. + * + * `head` is the segments for the left-hand operand of the + * logical expression. + * + * Each of the fork contexts below are empty at this point. We choose + * the path(s) that will short-circuit and add the segment for the + * left-hand operand to it. Ultimately, this will be the only segment + * in that path due to the short-circuting, so we are just seeding + * these paths to start. + */ + switch (currentChoiceContext.kind) { + case "&&": + + /* + * In most contexts, when a && expression evaluates to false, + * it short circuits, so we need to account for that by setting + * the `falseForkContext` to the left operand. + * + * When a && expression is the left-hand operand for a ?? + * expression, such as `(a && b) ?? c`, a nullish value will + * also short-circuit in a different way than a false value, + * so we also set the `nullishForkContext` to the left operand. + * This path is only used with a ?? expression and is thrown + * away for any other type of logical expression, so it's safe + * to always add. + */ + currentChoiceContext.falseForkContext.add(forkContext.head); + currentChoiceContext.nullishForkContext.add(forkContext.head); + break; + case "||": // the true path can short-circuit. + currentChoiceContext.trueForkContext.add(forkContext.head); + break; + case "??": // both can short-circuit. + currentChoiceContext.trueForkContext.add(forkContext.head); + currentChoiceContext.falseForkContext.add(forkContext.head); + break; + default: + throw new Error("unreachable"); + } + + /* + * Create the segment for the right-hand operand of the logical expression + * and adjust the fork context pointer to point there. + */ + forkContext.replaceHead(forkContext.makeNext(-1, -1)); + } + } + + /** + * Makes a code path segment of the `if` block. + * @returns {void} + */ + makeIfConsequent() { + const context = this.choiceContext; + const forkContext = this.forkContext; + + /* + * If any result were not transferred from child contexts, + * this sets the head segments to both cases. + * The head segments are the path of the test expression. + */ + if (!context.processed) { + context.trueForkContext.add(forkContext.head); + context.falseForkContext.add(forkContext.head); + context.nullishForkContext.add(forkContext.head); + } + + context.processed = false; + + // Creates new path from the `true` case. + forkContext.replaceHead( + context.trueForkContext.makeNext(0, -1) + ); + } + + /** + * Makes a code path segment of the `else` block. + * @returns {void} + */ + makeIfAlternate() { + const context = this.choiceContext; + const forkContext = this.forkContext; + + /* + * The head segments are the path of the `if` block. + * Updates the `true` path with the end of the `if` block. + */ + context.trueForkContext.clear(); + context.trueForkContext.add(forkContext.head); + context.processed = true; + + // Creates new path from the `false` case. + forkContext.replaceHead( + context.falseForkContext.makeNext(0, -1) + ); + } + + //-------------------------------------------------------------------------- + // ChainExpression + //-------------------------------------------------------------------------- + + /** + * Pushes a new `ChainExpression` context to the stack. This method is + * called when entering a `ChainExpression` node. A chain context is used to + * count forking in the optional chain then merge them on the exiting from the + * `ChainExpression` node. + * @returns {void} + */ + pushChainContext() { + this.chainContext = new ChainContext(this.chainContext); + } + + /** + * Pop a `ChainExpression` context from the stack. This method is called on + * exiting from each `ChainExpression` node. This merges all forks of the + * last optional chaining. + * @returns {void} + */ + popChainContext() { + const context = this.chainContext; + + this.chainContext = context.upper; + + // pop all choice contexts of this. + for (let i = context.choiceContextCount; i > 0; --i) { + this.popChoiceContext(); + } + } + + /** + * Create a choice context for optional access. + * This method is called on entering to each `(Call|Member)Expression[optional=true]` node. + * This creates a choice context as similar to `LogicalExpression[operator="??"]` node. + * @returns {void} + */ + makeOptionalNode() { + if (this.chainContext) { + this.chainContext.choiceContextCount += 1; + this.pushChoiceContext("??", false); + } + } + + /** + * Create a fork. + * This method is called on entering to the `arguments|property` property of each `(Call|Member)Expression` node. + * @returns {void} + */ + makeOptionalRight() { + if (this.chainContext) { + this.makeLogicalRight(); + } + } + + //-------------------------------------------------------------------------- + // SwitchStatement + //-------------------------------------------------------------------------- + + /** + * Creates a context object of SwitchStatement and stacks it. + * @param {boolean} hasCase `true` if the switch statement has one or more + * case parts. + * @param {string|null} label The label text. + * @returns {void} + */ + pushSwitchContext(hasCase, label) { + this.switchContext = new SwitchContext(this.switchContext, hasCase); + this.pushBreakContext(true, label); + } + + /** + * Pops the last context of SwitchStatement and finalizes it. + * + * - Disposes all forking stack for `case` and `default`. + * - Creates the next code path segment from `context.brokenForkContext`. + * - If the last `SwitchCase` node is not a `default` part, creates a path + * to the `default` body. + * @returns {void} + */ + popSwitchContext() { + const context = this.switchContext; + + this.switchContext = context.upper; + + const forkContext = this.forkContext; + const brokenForkContext = this.popBreakContext().brokenForkContext; + + if (context.forkCount === 0) { + + /* + * When there is only one `default` chunk and there is one or more + * `break` statements, even if forks are nothing, it needs to merge + * those. + */ + if (!brokenForkContext.empty) { + brokenForkContext.add(forkContext.makeNext(-1, -1)); + forkContext.replaceHead(brokenForkContext.makeNext(0, -1)); + } + + return; + } + + const lastSegments = forkContext.head; + + this.forkBypassPath(); + const lastCaseSegments = forkContext.head; + + /* + * `brokenForkContext` is used to make the next segment. + * It must add the last segment into `brokenForkContext`. + */ + brokenForkContext.add(lastSegments); + + /* + * Any value that doesn't match a `case` test should flow to the default + * case. That happens normally when the default case is last in the `switch`, + * but if it's not, we need to rewire some of the paths to be correct. + */ + if (!context.lastIsDefault) { + if (context.defaultBodySegments) { + + /* + * There is a non-empty default case, so remove the path from the `default` + * label to its body for an accurate representation. + */ + disconnectSegments(context.defaultSegments, context.defaultBodySegments); + + /* + * Connect the path from the last non-default case to the body of the + * default case. + */ + makeLooped(this, lastCaseSegments, context.defaultBodySegments); + + } else { + + /* + * There is no default case, so we treat this as if the last case + * had a `break` in it. + */ + brokenForkContext.add(lastCaseSegments); + } + } + + // Traverse up to the original fork context for the `switch` statement + for (let i = 0; i < context.forkCount; ++i) { + this.forkContext = this.forkContext.upper; + } + + /* + * Creates a path from all `brokenForkContext` paths. + * This is a path after `switch` statement. + */ + this.forkContext.replaceHead(brokenForkContext.makeNext(0, -1)); + } + + /** + * Makes a code path segment for a `SwitchCase` node. + * @param {boolean} isCaseBodyEmpty `true` if the body is empty. + * @param {boolean} isDefaultCase `true` if the body is the default case. + * @returns {void} + */ + makeSwitchCaseBody(isCaseBodyEmpty, isDefaultCase) { + const context = this.switchContext; + + if (!context.hasCase) { + return; + } + + /* + * Merge forks. + * The parent fork context has two segments. + * Those are from the current `case` and the body of the previous case. + */ + const parentForkContext = this.forkContext; + const forkContext = this.pushForkContext(); + + forkContext.add(parentForkContext.makeNext(0, -1)); + + /* + * Add information about the default case. + * + * The purpose of this is to identify the starting segments for the + * default case to make sure there is a path there. + */ + if (isDefaultCase) { + + /* + * This is the default case in the `switch`. + * + * We first save the current pointer as `defaultSegments` to point + * to the `default` keyword. + */ + context.defaultSegments = parentForkContext.head; + + /* + * If the body of the case is empty then we just set + * `foundEmptyDefault` to true; otherwise, we save a reference + * to the current pointer as `defaultBodySegments`. + */ + if (isCaseBodyEmpty) { + context.foundEmptyDefault = true; + } else { + context.defaultBodySegments = forkContext.head; + } + + } else { + + /* + * This is not the default case in the `switch`. + * + * If it's not empty and there is already an empty default case found, + * that means the default case actually comes before this case, + * and that it will fall through to this case. So, we can now + * ignore the previous default case (reset `foundEmptyDefault` to false) + * and set `defaultBodySegments` to the current segments because this is + * effectively the new default case. + */ + if (!isCaseBodyEmpty && context.foundEmptyDefault) { + context.foundEmptyDefault = false; + context.defaultBodySegments = forkContext.head; + } + } + + // keep track if the default case ends up last + context.lastIsDefault = isDefaultCase; + context.forkCount += 1; + } + + //-------------------------------------------------------------------------- + // TryStatement + //-------------------------------------------------------------------------- + + /** + * Creates a context object of TryStatement and stacks it. + * @param {boolean} hasFinalizer `true` if the try statement has a + * `finally` block. + * @returns {void} + */ + pushTryContext(hasFinalizer) { + this.tryContext = new TryContext(this.tryContext, hasFinalizer, this.forkContext); + } + + /** + * Pops the last context of TryStatement and finalizes it. + * @returns {void} + */ + popTryContext() { + const context = this.tryContext; + + this.tryContext = context.upper; + + /* + * If we're inside the `catch` block, that means there is no `finally`, + * so we can process the `try` and `catch` blocks the simple way and + * merge their two paths. + */ + if (context.position === "catch") { + this.popForkContext(); + return; + } + + /* + * The following process is executed only when there is a `finally` + * block. + */ + + const originalReturnedForkContext = context.returnedForkContext; + const originalThrownForkContext = context.thrownForkContext; + + // no `return` or `throw` in `try` or `catch` so there's nothing left to do + if (originalReturnedForkContext.empty && originalThrownForkContext.empty) { + return; + } + + /* + * The following process is executed only when there is a `finally` + * block and there was a `return` or `throw` in the `try` or `catch` + * blocks. + */ + + // Separate head to normal paths and leaving paths. + const headSegments = this.forkContext.head; + + this.forkContext = this.forkContext.upper; + const normalSegments = headSegments.slice(0, headSegments.length / 2 | 0); + const leavingSegments = headSegments.slice(headSegments.length / 2 | 0); + + // Forwards the leaving path to upper contexts. + if (!originalReturnedForkContext.empty) { + getReturnContext(this).returnedForkContext.add(leavingSegments); + } + if (!originalThrownForkContext.empty) { + getThrowContext(this).thrownForkContext.add(leavingSegments); + } + + // Sets the normal path as the next. + this.forkContext.replaceHead(normalSegments); + + /* + * If both paths of the `try` block and the `catch` block are + * unreachable, the next path becomes unreachable as well. + */ + if (!context.lastOfTryIsReachable && !context.lastOfCatchIsReachable) { + this.forkContext.makeUnreachable(); + } + } + + /** + * Makes a code path segment for a `catch` block. + * @returns {void} + */ + makeCatchBlock() { + const context = this.tryContext; + const forkContext = this.forkContext; + const originalThrownForkContext = context.thrownForkContext; + + /* + * We are now in a catch block so we need to update the context + * with that information. This includes creating a new fork + * context in case we encounter any `throw` statements here. + */ + context.position = "catch"; + context.thrownForkContext = ForkContext.newEmpty(forkContext); + context.lastOfTryIsReachable = forkContext.reachable; + + // Merge the thrown paths from the `try` and `catch` blocks + originalThrownForkContext.add(forkContext.head); + const thrownSegments = originalThrownForkContext.makeNext(0, -1); + + // Fork to a bypass and the merged thrown path. + this.pushForkContext(); + this.forkBypassPath(); + this.forkContext.add(thrownSegments); + } + + /** + * Makes a code path segment for a `finally` block. + * + * In the `finally` block, parallel paths are created. The parallel paths + * are used as leaving-paths. The leaving-paths are paths from `return` + * statements and `throw` statements in a `try` block or a `catch` block. + * @returns {void} + */ + makeFinallyBlock() { + const context = this.tryContext; + let forkContext = this.forkContext; + const originalReturnedForkContext = context.returnedForkContext; + const originalThrownForContext = context.thrownForkContext; + const headOfLeavingSegments = forkContext.head; + + // Update state. + if (context.position === "catch") { + + // Merges two paths from the `try` block and `catch` block. + this.popForkContext(); + forkContext = this.forkContext; + + context.lastOfCatchIsReachable = forkContext.reachable; + } else { + context.lastOfTryIsReachable = forkContext.reachable; + } + + + context.position = "finally"; + + /* + * If there was no `return` or `throw` in either the `try` or `catch` + * blocks, then there's no further code paths to create for `finally`. + */ + if (originalReturnedForkContext.empty && originalThrownForContext.empty) { + + // This path does not leave. + return; + } + + /* + * Create a parallel segment from merging returned and thrown. + * This segment will leave at the end of this `finally` block. + */ + const segments = forkContext.makeNext(-1, -1); + + for (let i = 0; i < forkContext.count; ++i) { + const prevSegsOfLeavingSegment = [headOfLeavingSegments[i]]; + + for (let j = 0; j < originalReturnedForkContext.segmentsList.length; ++j) { + prevSegsOfLeavingSegment.push(originalReturnedForkContext.segmentsList[j][i]); + } + for (let j = 0; j < originalThrownForContext.segmentsList.length; ++j) { + prevSegsOfLeavingSegment.push(originalThrownForContext.segmentsList[j][i]); + } + + segments.push( + CodePathSegment.newNext( + this.idGenerator.next(), + prevSegsOfLeavingSegment + ) + ); + } + + this.pushForkContext(true); + this.forkContext.add(segments); + } + + /** + * Makes a code path segment from the first throwable node to the `catch` + * block or the `finally` block. + * @returns {void} + */ + makeFirstThrowablePathInTryBlock() { + const forkContext = this.forkContext; + + if (!forkContext.reachable) { + return; + } + + const context = getThrowContext(this); + + if (context === this || + context.position !== "try" || + !context.thrownForkContext.empty + ) { + return; + } + + context.thrownForkContext.add(forkContext.head); + forkContext.replaceHead(forkContext.makeNext(-1, -1)); + } + + //-------------------------------------------------------------------------- + // Loop Statements + //-------------------------------------------------------------------------- + + /** + * Creates a context object of a loop statement and stacks it. + * @param {string} type The type of the node which was triggered. One of + * `WhileStatement`, `DoWhileStatement`, `ForStatement`, `ForInStatement`, + * and `ForStatement`. + * @param {string|null} label A label of the node which was triggered. + * @throws {Error} (Unreachable - unknown type.) + * @returns {void} + */ + pushLoopContext(type, label) { + const forkContext = this.forkContext; + + // All loops need a path to account for `break` statements + const breakContext = this.pushBreakContext(true, label); + + switch (type) { + case "WhileStatement": + this.pushChoiceContext("loop", false); + this.loopContext = new WhileLoopContext(this.loopContext, label, breakContext); + break; + + case "DoWhileStatement": + this.pushChoiceContext("loop", false); + this.loopContext = new DoWhileLoopContext(this.loopContext, label, breakContext, forkContext); + break; + + case "ForStatement": + this.pushChoiceContext("loop", false); + this.loopContext = new ForLoopContext(this.loopContext, label, breakContext); + break; + + case "ForInStatement": + this.loopContext = new ForInLoopContext(this.loopContext, label, breakContext); + break; + + case "ForOfStatement": + this.loopContext = new ForOfLoopContext(this.loopContext, label, breakContext); + break; + + /* c8 ignore next */ + default: + throw new Error(`unknown type: "${type}"`); + } + } + + /** + * Pops the last context of a loop statement and finalizes it. + * @throws {Error} (Unreachable - unknown type.) + * @returns {void} + */ + popLoopContext() { + const context = this.loopContext; + + this.loopContext = context.upper; + + const forkContext = this.forkContext; + const brokenForkContext = this.popBreakContext().brokenForkContext; + + // Creates a looped path. + switch (context.type) { + case "WhileStatement": + case "ForStatement": + this.popChoiceContext(); + + /* + * Creates the path from the end of the loop body up to the + * location where `continue` would jump to. + */ + makeLooped( + this, + forkContext.head, + context.continueDestSegments + ); + break; + + case "DoWhileStatement": { + const choiceContext = this.popChoiceContext(); + + if (!choiceContext.processed) { + choiceContext.trueForkContext.add(forkContext.head); + choiceContext.falseForkContext.add(forkContext.head); + } + + /* + * If this isn't a hardcoded `true` condition, then `break` + * should continue down the path as if the condition evaluated + * to false. + */ + if (context.test !== true) { + brokenForkContext.addAll(choiceContext.falseForkContext); + } + + /* + * When the condition is true, the loop continues back to the top, + * so create a path from each possible true condition back to the + * top of the loop. + */ + const segmentsList = choiceContext.trueForkContext.segmentsList; + + for (let i = 0; i < segmentsList.length; ++i) { + makeLooped( + this, + segmentsList[i], + context.entrySegments + ); + } + break; + } + + case "ForInStatement": + case "ForOfStatement": + brokenForkContext.add(forkContext.head); + + /* + * Creates the path from the end of the loop body up to the + * left expression (left of `in` or `of`) of the loop. + */ + makeLooped( + this, + forkContext.head, + context.leftSegments + ); + break; + + /* c8 ignore next */ + default: + throw new Error("unreachable"); + } + + /* + * If there wasn't a `break` statement in the loop, then we're at + * the end of the loop's path, so we make an unreachable segment + * to mark that. + * + * If there was a `break` statement, then we continue on into the + * `brokenForkContext`. + */ + if (brokenForkContext.empty) { + forkContext.replaceHead(forkContext.makeUnreachable(-1, -1)); + } else { + forkContext.replaceHead(brokenForkContext.makeNext(0, -1)); + } + } + + /** + * Makes a code path segment for the test part of a WhileStatement. + * @param {boolean|undefined} test The test value (only when constant). + * @returns {void} + */ + makeWhileTest(test) { + const context = this.loopContext; + const forkContext = this.forkContext; + const testSegments = forkContext.makeNext(0, -1); + + // Update state. + context.test = test; + context.continueDestSegments = testSegments; + forkContext.replaceHead(testSegments); + } + + /** + * Makes a code path segment for the body part of a WhileStatement. + * @returns {void} + */ + makeWhileBody() { + const context = this.loopContext; + const choiceContext = this.choiceContext; + const forkContext = this.forkContext; + + if (!choiceContext.processed) { + choiceContext.trueForkContext.add(forkContext.head); + choiceContext.falseForkContext.add(forkContext.head); + } + + /* + * If this isn't a hardcoded `true` condition, then `break` + * should continue down the path as if the condition evaluated + * to false. + */ + if (context.test !== true) { + context.brokenForkContext.addAll(choiceContext.falseForkContext); + } + forkContext.replaceHead(choiceContext.trueForkContext.makeNext(0, -1)); + } + + /** + * Makes a code path segment for the body part of a DoWhileStatement. + * @returns {void} + */ + makeDoWhileBody() { + const context = this.loopContext; + const forkContext = this.forkContext; + const bodySegments = forkContext.makeNext(-1, -1); + + // Update state. + context.entrySegments = bodySegments; + forkContext.replaceHead(bodySegments); + } + + /** + * Makes a code path segment for the test part of a DoWhileStatement. + * @param {boolean|undefined} test The test value (only when constant). + * @returns {void} + */ + makeDoWhileTest(test) { + const context = this.loopContext; + const forkContext = this.forkContext; + + context.test = test; + + /* + * If there is a `continue` statement in the loop then `continueForkContext` + * won't be empty. We wire up the path from `continue` to the loop + * test condition and then continue the traversal in the root fork context. + */ + if (!context.continueForkContext.empty) { + context.continueForkContext.add(forkContext.head); + const testSegments = context.continueForkContext.makeNext(0, -1); + + forkContext.replaceHead(testSegments); + } + } + + /** + * Makes a code path segment for the test part of a ForStatement. + * @param {boolean|undefined} test The test value (only when constant). + * @returns {void} + */ + makeForTest(test) { + const context = this.loopContext; + const forkContext = this.forkContext; + const endOfInitSegments = forkContext.head; + const testSegments = forkContext.makeNext(-1, -1); + + /* + * Update the state. + * + * The `continueDestSegments` are set to `testSegments` because we + * don't yet know if there is an update expression in this loop. So, + * from what we already know at this point, a `continue` statement + * will jump back to the test expression. + */ + context.test = test; + context.endOfInitSegments = endOfInitSegments; + context.continueDestSegments = context.testSegments = testSegments; + forkContext.replaceHead(testSegments); + } + + /** + * Makes a code path segment for the update part of a ForStatement. + * @returns {void} + */ + makeForUpdate() { + const context = this.loopContext; + const choiceContext = this.choiceContext; + const forkContext = this.forkContext; + + // Make the next paths of the test. + if (context.testSegments) { + finalizeTestSegmentsOfFor( + context, + choiceContext, + forkContext.head + ); + } else { + context.endOfInitSegments = forkContext.head; + } + + /* + * Update the state. + * + * The `continueDestSegments` are now set to `updateSegments` because we + * know there is an update expression in this loop. So, a `continue` statement + * in the loop will jump to the update expression first, and then to any + * test expression the loop might have. + */ + const updateSegments = forkContext.makeDisconnected(-1, -1); + + context.continueDestSegments = context.updateSegments = updateSegments; + forkContext.replaceHead(updateSegments); + } + + /** + * Makes a code path segment for the body part of a ForStatement. + * @returns {void} + */ + makeForBody() { + const context = this.loopContext; + const choiceContext = this.choiceContext; + const forkContext = this.forkContext; + + /* + * Determine what to do based on which part of the `for` loop are present. + * 1. If there is an update expression, then `updateSegments` is not null and + * we need to assign `endOfUpdateSegments`, and if there is a test + * expression, we then need to create the looped path to get back to + * the test condition. + * 2. If there is no update expression but there is a test expression, + * then we only need to update the test segment information. + * 3. If there is no update expression and no test expression, then we + * just save `endOfInitSegments`. + */ + if (context.updateSegments) { + context.endOfUpdateSegments = forkContext.head; + + /* + * In a `for` loop that has both an update expression and a test + * condition, execution flows from the test expression into the + * loop body, to the update expression, and then back to the test + * expression to determine if the loop should continue. + * + * To account for that, we need to make a path from the end of the + * update expression to the start of the test expression. This is + * effectively what creates the loop in the code path. + */ + if (context.testSegments) { + makeLooped( + this, + context.endOfUpdateSegments, + context.testSegments + ); + } + } else if (context.testSegments) { + finalizeTestSegmentsOfFor( + context, + choiceContext, + forkContext.head + ); + } else { + context.endOfInitSegments = forkContext.head; + } + + let bodySegments = context.endOfTestSegments; + + /* + * If there is a test condition, then there `endOfTestSegments` is also + * the start of the loop body. If there isn't a test condition then + * `bodySegments` will be null and we need to look elsewhere to find + * the start of the body. + * + * The body starts at the end of the init expression and ends at the end + * of the update expression, so we use those locations to determine the + * body segments. + */ + if (!bodySegments) { + + const prevForkContext = ForkContext.newEmpty(forkContext); + + prevForkContext.add(context.endOfInitSegments); + if (context.endOfUpdateSegments) { + prevForkContext.add(context.endOfUpdateSegments); + } + + bodySegments = prevForkContext.makeNext(0, -1); + } + + /* + * If there was no test condition and no update expression, then + * `continueDestSegments` will be null. In that case, a + * `continue` should skip directly to the body of the loop. + * Otherwise, we want to keep the current `continueDestSegments`. + */ + context.continueDestSegments = context.continueDestSegments || bodySegments; + + // move pointer to the body + forkContext.replaceHead(bodySegments); + } + + /** + * Makes a code path segment for the left part of a ForInStatement and a + * ForOfStatement. + * @returns {void} + */ + makeForInOfLeft() { + const context = this.loopContext; + const forkContext = this.forkContext; + const leftSegments = forkContext.makeDisconnected(-1, -1); + + // Update state. + context.prevSegments = forkContext.head; + context.leftSegments = context.continueDestSegments = leftSegments; + forkContext.replaceHead(leftSegments); + } + + /** + * Makes a code path segment for the right part of a ForInStatement and a + * ForOfStatement. + * @returns {void} + */ + makeForInOfRight() { + const context = this.loopContext; + const forkContext = this.forkContext; + const temp = ForkContext.newEmpty(forkContext); + + temp.add(context.prevSegments); + const rightSegments = temp.makeNext(-1, -1); + + // Update state. + context.endOfLeftSegments = forkContext.head; + forkContext.replaceHead(rightSegments); + } + + /** + * Makes a code path segment for the body part of a ForInStatement and a + * ForOfStatement. + * @returns {void} + */ + makeForInOfBody() { + const context = this.loopContext; + const forkContext = this.forkContext; + const temp = ForkContext.newEmpty(forkContext); + + temp.add(context.endOfLeftSegments); + const bodySegments = temp.makeNext(-1, -1); + + // Make a path: `right` -> `left`. + makeLooped(this, forkContext.head, context.leftSegments); + + // Update state. + context.brokenForkContext.add(forkContext.head); + forkContext.replaceHead(bodySegments); + } + + //-------------------------------------------------------------------------- + // Control Statements + //-------------------------------------------------------------------------- + + /** + * Creates new context in which a `break` statement can be used. This occurs inside of a loop, + * labeled statement, or switch statement. + * @param {boolean} breakable Indicates if we are inside a statement where + * `break` without a label will exit the statement. + * @param {string|null} label The label associated with the statement. + * @returns {BreakContext} The new context. + */ + pushBreakContext(breakable, label) { + this.breakContext = new BreakContext(this.breakContext, breakable, label, this.forkContext); + return this.breakContext; + } + + /** + * Removes the top item of the break context stack. + * @returns {Object} The removed context. + */ + popBreakContext() { + const context = this.breakContext; + const forkContext = this.forkContext; + + this.breakContext = context.upper; + + // Process this context here for other than switches and loops. + if (!context.breakable) { + const brokenForkContext = context.brokenForkContext; + + if (!brokenForkContext.empty) { + brokenForkContext.add(forkContext.head); + forkContext.replaceHead(brokenForkContext.makeNext(0, -1)); + } + } + + return context; + } + + /** + * Makes a path for a `break` statement. + * + * It registers the head segment to a context of `break`. + * It makes new unreachable segment, then it set the head with the segment. + * @param {string|null} label A label of the break statement. + * @returns {void} + */ + makeBreak(label) { + const forkContext = this.forkContext; + + if (!forkContext.reachable) { + return; + } + + const context = getBreakContext(this, label); + + + if (context) { + context.brokenForkContext.add(forkContext.head); + } + + /* c8 ignore next */ + forkContext.replaceHead(forkContext.makeUnreachable(-1, -1)); + } + + /** + * Makes a path for a `continue` statement. + * + * It makes a looping path. + * It makes new unreachable segment, then it set the head with the segment. + * @param {string|null} label A label of the continue statement. + * @returns {void} + */ + makeContinue(label) { + const forkContext = this.forkContext; + + if (!forkContext.reachable) { + return; + } + + const context = getContinueContext(this, label); + + if (context) { + if (context.continueDestSegments) { + makeLooped(this, forkContext.head, context.continueDestSegments); + + // If the context is a for-in/of loop, this affects a break also. + if (context.type === "ForInStatement" || + context.type === "ForOfStatement" + ) { + context.brokenForkContext.add(forkContext.head); + } + } else { + context.continueForkContext.add(forkContext.head); + } + } + forkContext.replaceHead(forkContext.makeUnreachable(-1, -1)); + } + + /** + * Makes a path for a `return` statement. + * + * It registers the head segment to a context of `return`. + * It makes new unreachable segment, then it set the head with the segment. + * @returns {void} + */ + makeReturn() { + const forkContext = this.forkContext; + + if (forkContext.reachable) { + getReturnContext(this).returnedForkContext.add(forkContext.head); + forkContext.replaceHead(forkContext.makeUnreachable(-1, -1)); + } + } + + /** + * Makes a path for a `throw` statement. + * + * It registers the head segment to a context of `throw`. + * It makes new unreachable segment, then it set the head with the segment. + * @returns {void} + */ + makeThrow() { + const forkContext = this.forkContext; + + if (forkContext.reachable) { + getThrowContext(this).thrownForkContext.add(forkContext.head); + forkContext.replaceHead(forkContext.makeUnreachable(-1, -1)); + } + } + + /** + * Makes the final path. + * @returns {void} + */ + makeFinal() { + const segments = this.currentSegments; + + if (segments.length > 0 && segments[0].reachable) { + this.returnedForkContext.add(segments); + } + } } module.exports = CodePathState; diff --git a/node_modules/eslint/lib/linter/code-path-analysis/code-path.js b/node_modules/eslint/lib/linter/code-path-analysis/code-path.js index 36ff8c53e..3bf570d75 100644 --- a/node_modules/eslint/lib/linter/code-path-analysis/code-path.js +++ b/node_modules/eslint/lib/linter/code-path-analysis/code-path.js @@ -20,313 +20,323 @@ const IdGenerator = require("./id-generator"); * A code path. */ class CodePath { - /** - * Creates a new instance. - * @param {Object} options Options for the function (see below). - * @param {string} options.id An identifier. - * @param {string} options.origin The type of code path origin. - * @param {CodePath|null} options.upper The code path of the upper function scope. - * @param {Function} options.onLooped A callback function to notify looping. - */ - constructor({ id, origin, upper, onLooped }) { - /** - * The identifier of this code path. - * Rules use it to store additional information of each rule. - * @type {string} - */ - this.id = id; - - /** - * The reason that this code path was started. May be "program", - * "function", "class-field-initializer", or "class-static-block". - * @type {string} - */ - this.origin = origin; - - /** - * The code path of the upper function scope. - * @type {CodePath|null} - */ - this.upper = upper; - - /** - * The code paths of nested function scopes. - * @type {CodePath[]} - */ - this.childCodePaths = []; - - // Initializes internal state. - Object.defineProperty(this, "internal", { - value: new CodePathState(new IdGenerator(`${id}_`), onLooped), - }); - - // Adds this into `childCodePaths` of `upper`. - if (upper) { - upper.childCodePaths.push(this); - } - } - - /** - * Gets the state of a given code path. - * @param {CodePath} codePath A code path to get. - * @returns {CodePathState} The state of the code path. - */ - static getState(codePath) { - return codePath.internal; - } - - /** - * The initial code path segment. This is the segment that is at the head - * of the code path. - * This is a passthrough to the underlying `CodePathState`. - * @type {CodePathSegment} - */ - get initialSegment() { - return this.internal.initialSegment; - } - - /** - * Final code path segments. These are the terminal (tail) segments in the - * code path, which is the combination of `returnedSegments` and `thrownSegments`. - * All segments in this array are reachable. - * This is a passthrough to the underlying `CodePathState`. - * @type {CodePathSegment[]} - */ - get finalSegments() { - return this.internal.finalSegments; - } - - /** - * Final code path segments that represent normal completion of the code path. - * For functions, this means both explicit `return` statements and implicit returns, - * such as the last reachable segment in a function that does not have an - * explicit `return` as this implicitly returns `undefined`. For scripts, - * modules, class field initializers, and class static blocks, this means - * all lines of code have been executed. - * These segments are also present in `finalSegments`. - * This is a passthrough to the underlying `CodePathState`. - * @type {CodePathSegment[]} - */ - get returnedSegments() { - return this.internal.returnedForkContext; - } - - /** - * Final code path segments that represent `throw` statements. - * This is a passthrough to the underlying `CodePathState`. - * These segments are also present in `finalSegments`. - * @type {CodePathSegment[]} - */ - get thrownSegments() { - return this.internal.thrownForkContext; - } - - /** - * Traverses all segments in this code path. - * - * codePath.traverseSegments((segment, controller) => { - * // do something. - * }); - * - * This method enumerates segments in order from the head. - * - * The `controller` argument has two methods: - * - * - `skip()` - skips the following segments in this branch - * - `break()` - skips all following segments in the traversal - * - * A note on the parameters: the `options` argument is optional. This means - * the first argument might be an options object or the callback function. - * @param {Object} [optionsOrCallback] Optional first and last segments to traverse. - * @param {CodePathSegment} [optionsOrCallback.first] The first segment to traverse. - * @param {CodePathSegment} [optionsOrCallback.last] The last segment to traverse. - * @param {Function} callback A callback function. - * @returns {void} - */ - traverseSegments(optionsOrCallback, callback) { - // normalize the arguments into a callback and options - let resolvedOptions; - let resolvedCallback; - - if (typeof optionsOrCallback === "function") { - resolvedCallback = optionsOrCallback; - resolvedOptions = {}; - } else { - resolvedOptions = optionsOrCallback || {}; - resolvedCallback = callback; - } - - // determine where to start traversing from based on the options - const startSegment = - resolvedOptions.first || this.internal.initialSegment; - const lastSegment = resolvedOptions.last; - - // set up initial location information - let record; - let index; - let end; - let segment = null; - - // segments that have already been visited during traversal - const visited = new Set(); - - // tracks the traversal steps - const stack = [[startSegment, 0]]; - - // segments that have been skipped during traversal - const skipped = new Set(); - - // indicates if we exited early from the traversal - let broken = false; - - /** - * Maintains traversal state. - */ - const controller = { - /** - * Skip the following segments in this branch. - * @returns {void} - */ - skip() { - skipped.add(segment); - }, - - /** - * Stop traversal completely - do not traverse to any - * other segments. - * @returns {void} - */ - break() { - broken = true; - }, - }; - - /** - * Checks if a given previous segment has been visited. - * @param {CodePathSegment} prevSegment A previous segment to check. - * @returns {boolean} `true` if the segment has been visited. - */ - function isVisited(prevSegment) { - return ( - visited.has(prevSegment) || - segment.isLoopedPrevSegment(prevSegment) - ); - } - - /** - * Checks if a given previous segment has been skipped. - * @param {CodePathSegment} prevSegment A previous segment to check. - * @returns {boolean} `true` if the segment has been skipped. - */ - function isSkipped(prevSegment) { - return ( - skipped.has(prevSegment) || - segment.isLoopedPrevSegment(prevSegment) - ); - } - - // the traversal - while (stack.length > 0) { - /* - * This isn't a pure stack. We use the top record all the time - * but don't always pop it off. The record is popped only if - * one of the following is true: - * - * 1) We have already visited the segment. - * 2) We have not visited *all* of the previous segments. - * 3) We have traversed past the available next segments. - * - * Otherwise, we just read the value and sometimes modify the - * record as we traverse. - */ - record = stack.at(-1); - segment = record[0]; - index = record[1]; - - if (index === 0) { - // Skip if this segment has been visited already. - if (visited.has(segment)) { - stack.pop(); - continue; - } - - // Skip if all previous segments have not been visited. - if ( - segment !== startSegment && - segment.prevSegments.length > 0 && - !segment.prevSegments.every(isVisited) - ) { - stack.pop(); - continue; - } - - visited.add(segment); - - // Skips the segment if all previous segments have been skipped. - const shouldSkip = - skipped.size > 0 && - segment.prevSegments.length > 0 && - segment.prevSegments.every(isSkipped); - - /* - * If the most recent segment hasn't been skipped, then we call - * the callback, passing in the segment and the controller. - */ - if (!shouldSkip) { - resolvedCallback.call(this, segment, controller); - - // exit if we're at the last segment - if (segment === lastSegment) { - controller.skip(); - } - - /* - * If the previous statement was executed, or if the callback - * called a method on the controller, we might need to exit the - * loop, so check for that and break accordingly. - */ - if (broken) { - break; - } - } else { - // If the most recent segment has been skipped, then mark it as skipped. - skipped.add(segment); - } - } - - // Update the stack. - end = segment.nextSegments.length - 1; - if (index < end) { - /* - * If we haven't yet visited all of the next segments, update - * the current top record on the stack to the next index to visit - * and then push a record for the current segment on top. - * - * Setting the current top record's index lets us know how many - * times we've been here and ensures that the segment won't be - * reprocessed (because we only process segments with an index - * of 0). - */ - record[1] += 1; - stack.push([segment.nextSegments[index], 0]); - } else if (index === end) { - /* - * If we are at the last next segment, then reset the top record - * in the stack to next segment and set its index to 0 so it will - * be processed next. - */ - record[0] = segment.nextSegments[index]; - record[1] = 0; - } else { - /* - * If index > end, that means we have no more segments that need - * processing. So, we pop that record off of the stack in order to - * continue traversing at the next level up. - */ - stack.pop(); - } - } - } + + /** + * Creates a new instance. + * @param {Object} options Options for the function (see below). + * @param {string} options.id An identifier. + * @param {string} options.origin The type of code path origin. + * @param {CodePath|null} options.upper The code path of the upper function scope. + * @param {Function} options.onLooped A callback function to notify looping. + */ + constructor({ id, origin, upper, onLooped }) { + + /** + * The identifier of this code path. + * Rules use it to store additional information of each rule. + * @type {string} + */ + this.id = id; + + /** + * The reason that this code path was started. May be "program", + * "function", "class-field-initializer", or "class-static-block". + * @type {string} + */ + this.origin = origin; + + /** + * The code path of the upper function scope. + * @type {CodePath|null} + */ + this.upper = upper; + + /** + * The code paths of nested function scopes. + * @type {CodePath[]} + */ + this.childCodePaths = []; + + // Initializes internal state. + Object.defineProperty( + this, + "internal", + { value: new CodePathState(new IdGenerator(`${id}_`), onLooped) } + ); + + // Adds this into `childCodePaths` of `upper`. + if (upper) { + upper.childCodePaths.push(this); + } + } + + /** + * Gets the state of a given code path. + * @param {CodePath} codePath A code path to get. + * @returns {CodePathState} The state of the code path. + */ + static getState(codePath) { + return codePath.internal; + } + + /** + * The initial code path segment. This is the segment that is at the head + * of the code path. + * This is a passthrough to the underlying `CodePathState`. + * @type {CodePathSegment} + */ + get initialSegment() { + return this.internal.initialSegment; + } + + /** + * Final code path segments. These are the terminal (tail) segments in the + * code path, which is the combination of `returnedSegments` and `thrownSegments`. + * All segments in this array are reachable. + * This is a passthrough to the underlying `CodePathState`. + * @type {CodePathSegment[]} + */ + get finalSegments() { + return this.internal.finalSegments; + } + + /** + * Final code path segments that represent normal completion of the code path. + * For functions, this means both explicit `return` statements and implicit returns, + * such as the last reachable segment in a function that does not have an + * explicit `return` as this implicitly returns `undefined`. For scripts, + * modules, class field initializers, and class static blocks, this means + * all lines of code have been executed. + * These segments are also present in `finalSegments`. + * This is a passthrough to the underlying `CodePathState`. + * @type {CodePathSegment[]} + */ + get returnedSegments() { + return this.internal.returnedForkContext; + } + + /** + * Final code path segments that represent `throw` statements. + * This is a passthrough to the underlying `CodePathState`. + * These segments are also present in `finalSegments`. + * @type {CodePathSegment[]} + */ + get thrownSegments() { + return this.internal.thrownForkContext; + } + + /** + * Tracks the traversal of the code path through each segment. This array + * starts empty and segments are added or removed as the code path is + * traversed. This array always ends up empty at the end of a code path + * traversal. The `CodePathState` uses this to track its progress through + * the code path. + * This is a passthrough to the underlying `CodePathState`. + * @type {CodePathSegment[]} + * @deprecated + */ + get currentSegments() { + return this.internal.currentSegments; + } + + /** + * Traverses all segments in this code path. + * + * codePath.traverseSegments((segment, controller) => { + * // do something. + * }); + * + * This method enumerates segments in order from the head. + * + * The `controller` argument has two methods: + * + * - `skip()` - skips the following segments in this branch + * - `break()` - skips all following segments in the traversal + * + * A note on the parameters: the `options` argument is optional. This means + * the first argument might be an options object or the callback function. + * @param {Object} [optionsOrCallback] Optional first and last segments to traverse. + * @param {CodePathSegment} [optionsOrCallback.first] The first segment to traverse. + * @param {CodePathSegment} [optionsOrCallback.last] The last segment to traverse. + * @param {Function} callback A callback function. + * @returns {void} + */ + traverseSegments(optionsOrCallback, callback) { + + // normalize the arguments into a callback and options + let resolvedOptions; + let resolvedCallback; + + if (typeof optionsOrCallback === "function") { + resolvedCallback = optionsOrCallback; + resolvedOptions = {}; + } else { + resolvedOptions = optionsOrCallback || {}; + resolvedCallback = callback; + } + + // determine where to start traversing from based on the options + const startSegment = resolvedOptions.first || this.internal.initialSegment; + const lastSegment = resolvedOptions.last; + + // set up initial location information + let record = null; + let index = 0; + let end = 0; + let segment = null; + + // segments that have already been visited during traversal + const visited = new Set(); + + // tracks the traversal steps + const stack = [[startSegment, 0]]; + + // tracks the last skipped segment during traversal + let skippedSegment = null; + + // indicates if we exited early from the traversal + let broken = false; + + /** + * Maintains traversal state. + */ + const controller = { + + /** + * Skip the following segments in this branch. + * @returns {void} + */ + skip() { + if (stack.length <= 1) { + broken = true; + } else { + skippedSegment = stack[stack.length - 2][0]; + } + }, + + /** + * Stop traversal completely - do not traverse to any + * other segments. + * @returns {void} + */ + break() { + broken = true; + } + }; + + /** + * Checks if a given previous segment has been visited. + * @param {CodePathSegment} prevSegment A previous segment to check. + * @returns {boolean} `true` if the segment has been visited. + */ + function isVisited(prevSegment) { + return ( + visited.has(prevSegment) || + segment.isLoopedPrevSegment(prevSegment) + ); + } + + // the traversal + while (stack.length > 0) { + + /* + * This isn't a pure stack. We use the top record all the time + * but don't always pop it off. The record is popped only if + * one of the following is true: + * + * 1) We have already visited the segment. + * 2) We have not visited *all* of the previous segments. + * 3) We have traversed past the available next segments. + * + * Otherwise, we just read the value and sometimes modify the + * record as we traverse. + */ + record = stack[stack.length - 1]; + segment = record[0]; + index = record[1]; + + if (index === 0) { + + // Skip if this segment has been visited already. + if (visited.has(segment)) { + stack.pop(); + continue; + } + + // Skip if all previous segments have not been visited. + if (segment !== startSegment && + segment.prevSegments.length > 0 && + !segment.prevSegments.every(isVisited) + ) { + stack.pop(); + continue; + } + + // Reset the skipping flag if all branches have been skipped. + if (skippedSegment && segment.prevSegments.includes(skippedSegment)) { + skippedSegment = null; + } + visited.add(segment); + + /* + * If the most recent segment hasn't been skipped, then we call + * the callback, passing in the segment and the controller. + */ + if (!skippedSegment) { + resolvedCallback.call(this, segment, controller); + + // exit if we're at the last segment + if (segment === lastSegment) { + controller.skip(); + } + + /* + * If the previous statement was executed, or if the callback + * called a method on the controller, we might need to exit the + * loop, so check for that and break accordingly. + */ + if (broken) { + break; + } + } + } + + // Update the stack. + end = segment.nextSegments.length - 1; + if (index < end) { + + /* + * If we haven't yet visited all of the next segments, update + * the current top record on the stack to the next index to visit + * and then push a record for the current segment on top. + * + * Setting the current top record's index lets us know how many + * times we've been here and ensures that the segment won't be + * reprocessed (because we only process segments with an index + * of 0). + */ + record[1] += 1; + stack.push([segment.nextSegments[index], 0]); + } else if (index === end) { + + /* + * If we are at the last next segment, then reset the top record + * in the stack to next segment and set its index to 0 so it will + * be processed next. + */ + record[0] = segment.nextSegments[index]; + record[1] = 0; + } else { + + /* + * If index > end, that means we have no more segments that need + * processing. So, we pop that record off of the stack in order to + * continue traversing at the next level up. + */ + stack.pop(); + } + } + } } module.exports = CodePath; diff --git a/node_modules/eslint/lib/linter/code-path-analysis/debug-helpers.js b/node_modules/eslint/lib/linter/code-path-analysis/debug-helpers.js index 6a65ca7b5..c0e01a824 100644 --- a/node_modules/eslint/lib/linter/code-path-analysis/debug-helpers.js +++ b/node_modules/eslint/lib/linter/code-path-analysis/debug-helpers.js @@ -21,9 +21,8 @@ const debug = require("debug")("eslint:code-path"); * @returns {string} Id of the segment. */ /* c8 ignore next */ -// eslint-disable-next-line jsdoc/require-jsdoc -- Ignoring -function getId(segment) { - return segment.id + (segment.reachable ? "" : "!"); +function getId(segment) { // eslint-disable-line jsdoc/require-jsdoc -- Ignoring + return segment.id + (segment.reachable ? "" : "!"); } /** @@ -33,16 +32,13 @@ function getId(segment) { * @returns {string} The string representation. */ function nodeToString(node, label) { - const suffix = label ? `:${label}` : ""; - - switch (node.type) { - case "Identifier": - return `${node.type}${suffix} (${node.name})`; - case "Literal": - return `${node.type}${suffix} (${node.value})`; - default: - return `${node.type}${suffix}`; - } + const suffix = label ? `:${label}` : ""; + + switch (node.type) { + case "Identifier": return `${node.type}${suffix} (${node.name})`; + case "Literal": return `${node.type}${suffix} (${node.value})`; + default: return `${node.type}${suffix}`; + } } //------------------------------------------------------------------------------ @@ -50,174 +46,158 @@ function nodeToString(node, label) { //------------------------------------------------------------------------------ module.exports = { - /** - * A flag that debug dumping is enabled or not. - * @type {boolean} - */ - enabled: debug.enabled, - - /** - * Dumps given objects. - * @param {...any} args objects to dump. - * @returns {void} - */ - dump: debug, - - /** - * Dumps the current analyzing state. - * @param {ASTNode} node A node to dump. - * @param {CodePathState} state A state to dump. - * @param {boolean} leaving A flag whether or not it's leaving - * @returns {void} - */ - dumpState: !debug.enabled - ? debug - : /* c8 ignore next */ function (node, state, leaving) { - for (let i = 0; i < state.currentSegments.length; ++i) { - const segInternal = state.currentSegments[i].internal; - - if (leaving) { - const last = segInternal.nodes.length - 1; - - if ( - last >= 0 && - segInternal.nodes[last] === - nodeToString(node, "enter") - ) { - segInternal.nodes[last] = nodeToString( - node, - void 0, - ); - } else { - segInternal.nodes.push(nodeToString(node, "exit")); - } - } else { - segInternal.nodes.push(nodeToString(node, "enter")); - } - } - - debug( - [ - `${state.currentSegments.map(getId).join(",")})`, - `${node.type}${leaving ? ":exit" : ""}`, - ].join(" "), - ); - }, - - /** - * Dumps a DOT code of a given code path. - * The DOT code can be visualized with Graphvis. - * @param {CodePath} codePath A code path to dump. - * @returns {void} - * @see http://www.graphviz.org - * @see http://www.webgraphviz.com - */ - dumpDot: !debug.enabled - ? debug - : /* c8 ignore next */ function (codePath) { - let text = - "\n" + - "digraph {\n" + - 'node[shape=box,style="rounded,filled",fillcolor=white];\n' + - 'initial[label="",shape=circle,style=filled,fillcolor=black,width=0.25,height=0.25];\n'; - - if (codePath.returnedSegments.length > 0) { - text += - 'final[label="",shape=doublecircle,style=filled,fillcolor=black,width=0.25,height=0.25];\n'; - } - if (codePath.thrownSegments.length > 0) { - text += - 'thrown[label="✘",shape=circle,width=0.3,height=0.3,fixedsize=true];\n'; - } - - const traceMap = Object.create(null); - const arrows = this.makeDotArrows(codePath, traceMap); - - // eslint-disable-next-line guard-for-in -- Want ability to traverse prototype - for (const id in traceMap) { - const segment = traceMap[id]; - - text += `${id}[`; - - if (segment.reachable) { - text += 'label="'; - } else { - text += - 'style="rounded,dashed,filled",fillcolor="#FF9800",label="<>\\n'; - } - - if (segment.internal.nodes.length > 0) { - text += segment.internal.nodes.join("\\n"); - } else { - text += "????"; - } - - text += '"];\n'; - } - - text += `${arrows}\n`; - text += "}"; - debug("DOT", text); - }, - - /** - * Makes a DOT code of a given code path. - * The DOT code can be visualized with Graphvis. - * @param {CodePath} codePath A code path to make DOT. - * @param {Object} traceMap Optional. A map to check whether or not segments had been done. - * @returns {string} A DOT code of the code path. - */ - makeDotArrows(codePath, traceMap) { - const stack = [[codePath.initialSegment, 0]]; - const done = traceMap || Object.create(null); - let lastId = codePath.initialSegment.id; - let text = `initial->${codePath.initialSegment.id}`; - - while (stack.length > 0) { - const item = stack.pop(); - const segment = item[0]; - const index = item[1]; - - if (done[segment.id] && index === 0) { - continue; - } - done[segment.id] = segment; - - const nextSegment = segment.allNextSegments[index]; - - if (!nextSegment) { - continue; - } - - if (lastId === segment.id) { - text += `->${nextSegment.id}`; - } else { - text += `;\n${segment.id}->${nextSegment.id}`; - } - lastId = nextSegment.id; - - stack.unshift([segment, 1 + index]); - stack.push([nextSegment, 0]); - } - - codePath.returnedSegments.forEach(finalSegment => { - if (lastId === finalSegment.id) { - text += "->final"; - } else { - text += `;\n${finalSegment.id}->final`; - } - lastId = null; - }); - - codePath.thrownSegments.forEach(finalSegment => { - if (lastId === finalSegment.id) { - text += "->thrown"; - } else { - text += `;\n${finalSegment.id}->thrown`; - } - lastId = null; - }); - - return `${text};`; - }, + + /** + * A flag that debug dumping is enabled or not. + * @type {boolean} + */ + enabled: debug.enabled, + + /** + * Dumps given objects. + * @param {...any} args objects to dump. + * @returns {void} + */ + dump: debug, + + /** + * Dumps the current analyzing state. + * @param {ASTNode} node A node to dump. + * @param {CodePathState} state A state to dump. + * @param {boolean} leaving A flag whether or not it's leaving + * @returns {void} + */ + dumpState: !debug.enabled ? debug : /* c8 ignore next */ function(node, state, leaving) { + for (let i = 0; i < state.currentSegments.length; ++i) { + const segInternal = state.currentSegments[i].internal; + + if (leaving) { + const last = segInternal.nodes.length - 1; + + if (last >= 0 && segInternal.nodes[last] === nodeToString(node, "enter")) { + segInternal.nodes[last] = nodeToString(node, void 0); + } else { + segInternal.nodes.push(nodeToString(node, "exit")); + } + } else { + segInternal.nodes.push(nodeToString(node, "enter")); + } + } + + debug([ + `${state.currentSegments.map(getId).join(",")})`, + `${node.type}${leaving ? ":exit" : ""}` + ].join(" ")); + }, + + /** + * Dumps a DOT code of a given code path. + * The DOT code can be visualized with Graphvis. + * @param {CodePath} codePath A code path to dump. + * @returns {void} + * @see http://www.graphviz.org + * @see http://www.webgraphviz.com + */ + dumpDot: !debug.enabled ? debug : /* c8 ignore next */ function(codePath) { + let text = + "\n" + + "digraph {\n" + + "node[shape=box,style=\"rounded,filled\",fillcolor=white];\n" + + "initial[label=\"\",shape=circle,style=filled,fillcolor=black,width=0.25,height=0.25];\n"; + + if (codePath.returnedSegments.length > 0) { + text += "final[label=\"\",shape=doublecircle,style=filled,fillcolor=black,width=0.25,height=0.25];\n"; + } + if (codePath.thrownSegments.length > 0) { + text += "thrown[label=\"✘\",shape=circle,width=0.3,height=0.3,fixedsize=true];\n"; + } + + const traceMap = Object.create(null); + const arrows = this.makeDotArrows(codePath, traceMap); + + for (const id in traceMap) { // eslint-disable-line guard-for-in -- Want ability to traverse prototype + const segment = traceMap[id]; + + text += `${id}[`; + + if (segment.reachable) { + text += "label=\""; + } else { + text += "style=\"rounded,dashed,filled\",fillcolor=\"#FF9800\",label=\"<>\\n"; + } + + if (segment.internal.nodes.length > 0) { + text += segment.internal.nodes.join("\\n"); + } else { + text += "????"; + } + + text += "\"];\n"; + } + + text += `${arrows}\n`; + text += "}"; + debug("DOT", text); + }, + + /** + * Makes a DOT code of a given code path. + * The DOT code can be visualized with Graphvis. + * @param {CodePath} codePath A code path to make DOT. + * @param {Object} traceMap Optional. A map to check whether or not segments had been done. + * @returns {string} A DOT code of the code path. + */ + makeDotArrows(codePath, traceMap) { + const stack = [[codePath.initialSegment, 0]]; + const done = traceMap || Object.create(null); + let lastId = codePath.initialSegment.id; + let text = `initial->${codePath.initialSegment.id}`; + + while (stack.length > 0) { + const item = stack.pop(); + const segment = item[0]; + const index = item[1]; + + if (done[segment.id] && index === 0) { + continue; + } + done[segment.id] = segment; + + const nextSegment = segment.allNextSegments[index]; + + if (!nextSegment) { + continue; + } + + if (lastId === segment.id) { + text += `->${nextSegment.id}`; + } else { + text += `;\n${segment.id}->${nextSegment.id}`; + } + lastId = nextSegment.id; + + stack.unshift([segment, 1 + index]); + stack.push([nextSegment, 0]); + } + + codePath.returnedSegments.forEach(finalSegment => { + if (lastId === finalSegment.id) { + text += "->final"; + } else { + text += `;\n${finalSegment.id}->final`; + } + lastId = null; + }); + + codePath.thrownSegments.forEach(finalSegment => { + if (lastId === finalSegment.id) { + text += "->thrown"; + } else { + text += `;\n${finalSegment.id}->thrown`; + } + lastId = null; + }); + + return `${text};`; + } }; diff --git a/node_modules/eslint/lib/linter/code-path-analysis/fork-context.js b/node_modules/eslint/lib/linter/code-path-analysis/fork-context.js index 93d331625..33140272f 100644 --- a/node_modules/eslint/lib/linter/code-path-analysis/fork-context.js +++ b/node_modules/eslint/lib/linter/code-path-analysis/fork-context.js @@ -13,8 +13,8 @@ // Requirements //------------------------------------------------------------------------------ -const assert = require("../../shared/assert"), - CodePathSegment = require("./code-path-segment"); +const assert = require("assert"), + CodePathSegment = require("./code-path-segment"); //------------------------------------------------------------------------------ // Helpers @@ -26,7 +26,7 @@ const assert = require("../../shared/assert"), * @returns {boolean} `true` if the segment is reachable. */ function isReachable(segment) { - return segment.reachable; + return segment.reachable; } /** @@ -54,43 +54,44 @@ function isReachable(segment) { * @returns {Array} An array of the newly-created segments. */ function createSegments(context, startIndex, endIndex, create) { - /** @type {Array>} */ - const list = context.segmentsList; - - /* - * Both `startIndex` and `endIndex` work the same way: if the number is zero - * or more, then the number is used as-is. If the number is negative, - * then that number is added to the length of the segments list to - * determine the index to use. That means -1 for either argument - * is the last element, -2 is the second to last, and so on. - * - * So if `startIndex` is 0, `endIndex` is -1, and `list.length` is 3, the - * effective `startIndex` is 0 and the effective `endIndex` is 2, so this function - * will include items at indices 0, 1, and 2. - * - * Therefore, if `startIndex` is -1 and `endIndex` is -1, that means we'll only - * be using the last segment in `list`. - */ - const normalizedBegin = - startIndex >= 0 ? startIndex : list.length + startIndex; - const normalizedEnd = endIndex >= 0 ? endIndex : list.length + endIndex; - - /** @type {Array} */ - const segments = []; - - for (let i = 0; i < context.count; ++i) { - // this is passed into `new CodePathSegment` to add to code path. - const allPrevSegments = []; - - for (let j = normalizedBegin; j <= normalizedEnd; ++j) { - allPrevSegments.push(list[j][i]); - } - - // note: `create` is just a wrapper that augments `new CodePathSegment`. - segments.push(create(context.idGenerator.next(), allPrevSegments)); - } - - return segments; + + /** @type {Array>} */ + const list = context.segmentsList; + + /* + * Both `startIndex` and `endIndex` work the same way: if the number is zero + * or more, then the number is used as-is. If the number is negative, + * then that number is added to the length of the segments list to + * determine the index to use. That means -1 for either argument + * is the last element, -2 is the second to last, and so on. + * + * So if `startIndex` is 0, `endIndex` is -1, and `list.length` is 3, the + * effective `startIndex` is 0 and the effective `endIndex` is 2, so this function + * will include items at indices 0, 1, and 2. + * + * Therefore, if `startIndex` is -1 and `endIndex` is -1, that means we'll only + * be using the last segment in `list`. + */ + const normalizedBegin = startIndex >= 0 ? startIndex : list.length + startIndex; + const normalizedEnd = endIndex >= 0 ? endIndex : list.length + endIndex; + + /** @type {Array} */ + const segments = []; + + for (let i = 0; i < context.count; ++i) { + + // this is passed into `new CodePathSegment` to add to code path. + const allPrevSegments = []; + + for (let j = normalizedBegin; j <= normalizedEnd; ++j) { + allPrevSegments.push(list[j][i]); + } + + // note: `create` is just a wrapper that augments `new CodePathSegment`. + segments.push(create(context.idGenerator.next(), allPrevSegments)); + } + + return segments; } /** @@ -102,56 +103,50 @@ function createSegments(context, startIndex, endIndex, create) { * @returns {Array} The merged segments. */ function mergeExtraSegments(context, segments) { - let currentSegments = segments; - - /* - * We need to ensure that the array returned from this function contains no more - * than the number of segments that the context allows. `context.count` indicates - * how many items should be in the returned array to ensure that the new segment - * entries will line up with the already existing segment entries. - */ - while (currentSegments.length > context.count) { - const merged = []; - - /* - * Because `context.count` is a factor of 2 inside of a `finally` block, - * we can divide the segment count by 2 to merge the paths together. - * This loops through each segment in the list and creates a new `CodePathSegment` - * that has the segment and the segment two slots away as previous segments. - * - * If `currentSegments` is [a,b,c,d], this will create new segments e and f, such - * that: - * - * When `i` is 0: - * a->e - * c->e - * - * When `i` is 1: - * b->f - * d->f - */ - for ( - let i = 0, length = Math.floor(currentSegments.length / 2); - i < length; - ++i - ) { - merged.push( - CodePathSegment.newNext(context.idGenerator.next(), [ - currentSegments[i], - currentSegments[i + length], - ]), - ); - } - - /* - * Go through the loop condition one more time to see if we have the - * number of segments for the context. If not, we'll keep merging paths - * of the merged segments until we get there. - */ - currentSegments = merged; - } - - return currentSegments; + let currentSegments = segments; + + /* + * We need to ensure that the array returned from this function contains no more + * than the number of segments that the context allows. `context.count` indicates + * how many items should be in the returned array to ensure that the new segment + * entries will line up with the already existing segment entries. + */ + while (currentSegments.length > context.count) { + const merged = []; + + /* + * Because `context.count` is a factor of 2 inside of a `finally` block, + * we can divide the segment count by 2 to merge the paths together. + * This loops through each segment in the list and creates a new `CodePathSegment` + * that has the segment and the segment two slots away as previous segments. + * + * If `currentSegments` is [a,b,c,d], this will create new segments e and f, such + * that: + * + * When `i` is 0: + * a->e + * c->e + * + * When `i` is 1: + * b->f + * d->f + */ + for (let i = 0, length = Math.floor(currentSegments.length / 2); i < length; ++i) { + merged.push(CodePathSegment.newNext( + context.idGenerator.next(), + [currentSegments[i], currentSegments[i + length]] + )); + } + + /* + * Go through the loop condition one more time to see if we have the + * number of segments for the context. If not, we'll keep merging paths + * of the merged segments until we get there. + */ + currentSegments = merged; + } + + return currentSegments; } //------------------------------------------------------------------------------ @@ -162,213 +157,193 @@ function mergeExtraSegments(context, segments) { * Manages the forking of code paths. */ class ForkContext { - /** - * Creates a new instance. - * @param {IdGenerator} idGenerator An identifier generator for segments. - * @param {ForkContext|null} upper The preceding fork context. - * @param {number} count The number of parallel segments in each element - * of `segmentsList`. - */ - constructor(idGenerator, upper, count) { - /** - * The ID generator that will generate segment IDs for any new - * segments that are created. - * @type {IdGenerator} - */ - this.idGenerator = idGenerator; - - /** - * The preceding fork context. - * @type {ForkContext|null} - */ - this.upper = upper; - - /** - * The number of elements in each element of `segmentsList`. In most - * cases, this is 1 but can be 2 when there is a `finally` present, - * which forks the code path outside of normal flow. In the case of nested - * `finally` blocks, this can be a multiple of 2. - * @type {number} - */ - this.count = count; - - /** - * The segments within this context. Each element in this array has - * `count` elements that represent one step in each fork. For example, - * when `segmentsList` is `[[a, b], [c, d], [e, f]]`, there is one path - * a->c->e and one path b->d->f, and `count` is 2 because each element - * is an array with two elements. - * @type {Array>} - */ - this.segmentsList = []; - } - - /** - * The segments that begin this fork context. - * @type {Array} - */ - get head() { - const list = this.segmentsList; - - return list.length === 0 ? [] : list.at(-1); - } - - /** - * Indicates if the context contains no segments. - * @type {boolean} - */ - get empty() { - return this.segmentsList.length === 0; - } - - /** - * Indicates if there are any segments that are reachable. - * @type {boolean} - */ - get reachable() { - const segments = this.head; - - return segments.length > 0 && segments.some(isReachable); - } - - /** - * Creates new segments in this context and appends them to the end of the - * already existing `CodePathSegment`s specified by `startIndex` and - * `endIndex`. - * @param {number} startIndex The index of the first segment in the context - * that should be specified as previous segments for the newly created segments. - * @param {number} endIndex The index of the last segment in the context - * that should be specified as previous segments for the newly created segments. - * @returns {Array} An array of the newly created segments. - */ - makeNext(startIndex, endIndex) { - return createSegments( - this, - startIndex, - endIndex, - CodePathSegment.newNext, - ); - } - - /** - * Creates new unreachable segments in this context and appends them to the end of the - * already existing `CodePathSegment`s specified by `startIndex` and - * `endIndex`. - * @param {number} startIndex The index of the first segment in the context - * that should be specified as previous segments for the newly created segments. - * @param {number} endIndex The index of the last segment in the context - * that should be specified as previous segments for the newly created segments. - * @returns {Array} An array of the newly created segments. - */ - makeUnreachable(startIndex, endIndex) { - return createSegments( - this, - startIndex, - endIndex, - CodePathSegment.newUnreachable, - ); - } - - /** - * Creates new segments in this context and does not append them to the end - * of the already existing `CodePathSegment`s specified by `startIndex` and - * `endIndex`. The `startIndex` and `endIndex` are only used to determine if - * the new segments should be reachable. If any of the segments in this range - * are reachable then the new segments are also reachable; otherwise, the new - * segments are unreachable. - * @param {number} startIndex The index of the first segment in the context - * that should be considered for reachability. - * @param {number} endIndex The index of the last segment in the context - * that should be considered for reachability. - * @returns {Array} An array of the newly created segments. - */ - makeDisconnected(startIndex, endIndex) { - return createSegments( - this, - startIndex, - endIndex, - CodePathSegment.newDisconnected, - ); - } - - /** - * Adds segments to the head of this context. - * @param {Array} segments The segments to add. - * @returns {void} - */ - add(segments) { - assert( - segments.length >= this.count, - `${segments.length} >= ${this.count}`, - ); - this.segmentsList.push(mergeExtraSegments(this, segments)); - } - - /** - * Replaces the head segments with the given segments. - * The current head segments are removed. - * @param {Array} replacementHeadSegments The new head segments. - * @returns {void} - */ - replaceHead(replacementHeadSegments) { - assert( - replacementHeadSegments.length >= this.count, - `${replacementHeadSegments.length} >= ${this.count}`, - ); - this.segmentsList.splice( - -1, - 1, - mergeExtraSegments(this, replacementHeadSegments), - ); - } - - /** - * Adds all segments of a given fork context into this context. - * @param {ForkContext} otherForkContext The fork context to add from. - * @returns {void} - */ - addAll(otherForkContext) { - assert(otherForkContext.count === this.count); - this.segmentsList.push(...otherForkContext.segmentsList); - } - - /** - * Clears all segments in this context. - * @returns {void} - */ - clear() { - this.segmentsList = []; - } - - /** - * Creates a new root context, meaning that there are no parent - * fork contexts. - * @param {IdGenerator} idGenerator An identifier generator for segments. - * @returns {ForkContext} New fork context. - */ - static newRoot(idGenerator) { - const context = new ForkContext(idGenerator, null, 1); - - context.add([CodePathSegment.newRoot(idGenerator.next())]); - - return context; - } - - /** - * Creates an empty fork context preceded by a given context. - * @param {ForkContext} parentContext The parent fork context. - * @param {boolean} shouldForkLeavingPath Indicates that we are inside of - * a `finally` block and should therefore fork the path that leaves - * `finally`. - * @returns {ForkContext} New fork context. - */ - static newEmpty(parentContext, shouldForkLeavingPath) { - return new ForkContext( - parentContext.idGenerator, - parentContext, - (shouldForkLeavingPath ? 2 : 1) * parentContext.count, - ); - } + + /** + * Creates a new instance. + * @param {IdGenerator} idGenerator An identifier generator for segments. + * @param {ForkContext|null} upper The preceding fork context. + * @param {number} count The number of parallel segments in each element + * of `segmentsList`. + */ + constructor(idGenerator, upper, count) { + + /** + * The ID generator that will generate segment IDs for any new + * segments that are created. + * @type {IdGenerator} + */ + this.idGenerator = idGenerator; + + /** + * The preceding fork context. + * @type {ForkContext|null} + */ + this.upper = upper; + + /** + * The number of elements in each element of `segmentsList`. In most + * cases, this is 1 but can be 2 when there is a `finally` present, + * which forks the code path outside of normal flow. In the case of nested + * `finally` blocks, this can be a multiple of 2. + * @type {number} + */ + this.count = count; + + /** + * The segments within this context. Each element in this array has + * `count` elements that represent one step in each fork. For example, + * when `segmentsList` is `[[a, b], [c, d], [e, f]]`, there is one path + * a->c->e and one path b->d->f, and `count` is 2 because each element + * is an array with two elements. + * @type {Array>} + */ + this.segmentsList = []; + } + + /** + * The segments that begin this fork context. + * @type {Array} + */ + get head() { + const list = this.segmentsList; + + return list.length === 0 ? [] : list[list.length - 1]; + } + + /** + * Indicates if the context contains no segments. + * @type {boolean} + */ + get empty() { + return this.segmentsList.length === 0; + } + + /** + * Indicates if there are any segments that are reachable. + * @type {boolean} + */ + get reachable() { + const segments = this.head; + + return segments.length > 0 && segments.some(isReachable); + } + + /** + * Creates new segments in this context and appends them to the end of the + * already existing `CodePathSegment`s specified by `startIndex` and + * `endIndex`. + * @param {number} startIndex The index of the first segment in the context + * that should be specified as previous segments for the newly created segments. + * @param {number} endIndex The index of the last segment in the context + * that should be specified as previous segments for the newly created segments. + * @returns {Array} An array of the newly created segments. + */ + makeNext(startIndex, endIndex) { + return createSegments(this, startIndex, endIndex, CodePathSegment.newNext); + } + + /** + * Creates new unreachable segments in this context and appends them to the end of the + * already existing `CodePathSegment`s specified by `startIndex` and + * `endIndex`. + * @param {number} startIndex The index of the first segment in the context + * that should be specified as previous segments for the newly created segments. + * @param {number} endIndex The index of the last segment in the context + * that should be specified as previous segments for the newly created segments. + * @returns {Array} An array of the newly created segments. + */ + makeUnreachable(startIndex, endIndex) { + return createSegments(this, startIndex, endIndex, CodePathSegment.newUnreachable); + } + + /** + * Creates new segments in this context and does not append them to the end + * of the already existing `CodePathSegment`s specified by `startIndex` and + * `endIndex`. The `startIndex` and `endIndex` are only used to determine if + * the new segments should be reachable. If any of the segments in this range + * are reachable then the new segments are also reachable; otherwise, the new + * segments are unreachable. + * @param {number} startIndex The index of the first segment in the context + * that should be considered for reachability. + * @param {number} endIndex The index of the last segment in the context + * that should be considered for reachability. + * @returns {Array} An array of the newly created segments. + */ + makeDisconnected(startIndex, endIndex) { + return createSegments(this, startIndex, endIndex, CodePathSegment.newDisconnected); + } + + /** + * Adds segments to the head of this context. + * @param {Array} segments The segments to add. + * @returns {void} + */ + add(segments) { + assert(segments.length >= this.count, `${segments.length} >= ${this.count}`); + this.segmentsList.push(mergeExtraSegments(this, segments)); + } + + /** + * Replaces the head segments with the given segments. + * The current head segments are removed. + * @param {Array} replacementHeadSegments The new head segments. + * @returns {void} + */ + replaceHead(replacementHeadSegments) { + assert( + replacementHeadSegments.length >= this.count, + `${replacementHeadSegments.length} >= ${this.count}` + ); + this.segmentsList.splice(-1, 1, mergeExtraSegments(this, replacementHeadSegments)); + } + + /** + * Adds all segments of a given fork context into this context. + * @param {ForkContext} otherForkContext The fork context to add from. + * @returns {void} + */ + addAll(otherForkContext) { + assert(otherForkContext.count === this.count); + this.segmentsList.push(...otherForkContext.segmentsList); + } + + /** + * Clears all segments in this context. + * @returns {void} + */ + clear() { + this.segmentsList = []; + } + + /** + * Creates a new root context, meaning that there are no parent + * fork contexts. + * @param {IdGenerator} idGenerator An identifier generator for segments. + * @returns {ForkContext} New fork context. + */ + static newRoot(idGenerator) { + const context = new ForkContext(idGenerator, null, 1); + + context.add([CodePathSegment.newRoot(idGenerator.next())]); + + return context; + } + + /** + * Creates an empty fork context preceded by a given context. + * @param {ForkContext} parentContext The parent fork context. + * @param {boolean} shouldForkLeavingPath Indicates that we are inside of + * a `finally` block and should therefore fork the path that leaves + * `finally`. + * @returns {ForkContext} New fork context. + */ + static newEmpty(parentContext, shouldForkLeavingPath) { + return new ForkContext( + parentContext.idGenerator, + parentContext, + (shouldForkLeavingPath ? 2 : 1) * parentContext.count + ); + } } module.exports = ForkContext; diff --git a/node_modules/eslint/lib/linter/code-path-analysis/id-generator.js b/node_modules/eslint/lib/linter/code-path-analysis/id-generator.js index 9cb4d33b2..b580104e1 100644 --- a/node_modules/eslint/lib/linter/code-path-analysis/id-generator.js +++ b/node_modules/eslint/lib/linter/code-path-analysis/id-generator.js @@ -17,28 +17,29 @@ * A generator for unique ids. */ class IdGenerator { - /** - * @param {string} prefix Optional. A prefix of generated ids. - */ - constructor(prefix) { - this.prefix = String(prefix); - this.n = 0; - } - - /** - * Generates id. - * @returns {string} A generated id. - */ - next() { - this.n = (1 + this.n) | 0; - - /* c8 ignore start */ - if (this.n < 0) { - this.n = 1; - } /* c8 ignore stop */ - - return this.prefix + this.n; - } + + /** + * @param {string} prefix Optional. A prefix of generated ids. + */ + constructor(prefix) { + this.prefix = String(prefix); + this.n = 0; + } + + /** + * Generates id. + * @returns {string} A generated id. + */ + next() { + this.n = 1 + this.n | 0; + + /* c8 ignore start */ + if (this.n < 0) { + this.n = 1; + }/* c8 ignore stop */ + + return this.prefix + this.n; + } } module.exports = IdGenerator; diff --git a/node_modules/eslint/lib/linter/esquery.js b/node_modules/eslint/lib/linter/esquery.js deleted file mode 100644 index 31d302bc6..000000000 --- a/node_modules/eslint/lib/linter/esquery.js +++ /dev/null @@ -1,332 +0,0 @@ -/** - * @fileoverview ESQuery wrapper for ESLint. - * @author Nicholas C. Zakas - */ - -"use strict"; - -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ - -const esquery = require("esquery"); - -//----------------------------------------------------------------------------- -// Typedefs -//----------------------------------------------------------------------------- - -/** - * @typedef {import("esquery").Selector} ESQuerySelector - * @typedef {import("esquery").ESQueryOptions} ESQueryOptions - */ - -//------------------------------------------------------------------------------ -// Classes -//------------------------------------------------------------------------------ - -/** - * The result of parsing and analyzing an ESQuery selector. - */ -class ESQueryParsedSelector { - /** - * The raw selector string that was parsed - * @type {string} - */ - source; - - /** - * Whether this selector is an exit selector - * @type {boolean} - */ - isExit; - - /** - * An object (from esquery) describing the matching behavior of the selector - * @type {ESQuerySelector} - */ - root; - - /** - * The node types that could possibly trigger this selector, or `null` if all node types could trigger it - * @type {string[]|null} - */ - nodeTypes; - - /** - * The number of class, pseudo-class, and attribute queries in this selector - * @type {number} - */ - attributeCount; - - /** - * The number of identifier queries in this selector - * @type {number} - */ - identifierCount; - - /** - * Creates a new parsed selector. - * @param {string} source The raw selector string that was parsed - * @param {boolean} isExit Whether this selector is an exit selector - * @param {ESQuerySelector} root An object (from esquery) describing the matching behavior of the selector - * @param {string[]|null} nodeTypes The node types that could possibly trigger this selector, or `null` if all node types could trigger it - * @param {number} attributeCount The number of class, pseudo-class, and attribute queries in this selector - * @param {number} identifierCount The number of identifier queries in this selector - */ - constructor( - source, - isExit, - root, - nodeTypes, - attributeCount, - identifierCount, - ) { - this.source = source; - this.isExit = isExit; - this.root = root; - this.nodeTypes = nodeTypes; - this.attributeCount = attributeCount; - this.identifierCount = identifierCount; - } - - /** - * Compares this selector's specificity to another selector for sorting purposes. - * @param {ESQueryParsedSelector} otherSelector The selector to compare against - * @returns {number} - * a value less than 0 if this selector is less specific than otherSelector - * a value greater than 0 if this selector is more specific than otherSelector - * a value less than 0 if this selector and otherSelector have the same specificity, and this selector <= otherSelector alphabetically - * a value greater than 0 if this selector and otherSelector have the same specificity, and this selector > otherSelector alphabetically - */ - compare(otherSelector) { - return ( - this.attributeCount - otherSelector.attributeCount || - this.identifierCount - otherSelector.identifierCount || - (this.source <= otherSelector.source ? -1 : 1) - ); - } -} - -//------------------------------------------------------------------------------ -// Helpers -//------------------------------------------------------------------------------ - -const selectorCache = new Map(); - -/** - * Computes the union of one or more arrays - * @param {...any[]} arrays One or more arrays to union - * @returns {any[]} The union of the input arrays - */ -function union(...arrays) { - return [...new Set(arrays.flat())]; -} - -/** - * Computes the intersection of one or more arrays - * @param {...any[]} arrays One or more arrays to intersect - * @returns {any[]} The intersection of the input arrays - */ -function intersection(...arrays) { - if (arrays.length === 0) { - return []; - } - - let result = [...new Set(arrays[0])]; - - for (const array of arrays.slice(1)) { - result = result.filter(x => array.includes(x)); - } - return result; -} - -/** - * Analyzes a parsed selector and returns combined data about it - * @param {ESQuerySelector} parsedSelector An object (from esquery) describing the matching behavior of the selector - * @returns {{nodeTypes:string[]|null, attributeCount:number, identifierCount:number}} Object containing selector data. - */ -function analyzeParsedSelector(parsedSelector) { - let attributeCount = 0; - let identifierCount = 0; - - /** - * Analyzes a selector and returns the node types that could possibly trigger it. - * @param {ESQuerySelector} selector The selector to analyze. - * @returns {string[]|null} The node types that could possibly trigger this selector, or `null` if all node types could trigger it - */ - function analyzeSelector(selector) { - switch (selector.type) { - case "identifier": - identifierCount++; - return [selector.value]; - - case "not": - selector.selectors.map(analyzeSelector); - return null; - - case "matches": { - const typesForComponents = - selector.selectors.map(analyzeSelector); - - if (typesForComponents.every(Boolean)) { - return union(...typesForComponents); - } - return null; - } - - case "compound": { - const typesForComponents = selector.selectors - .map(analyzeSelector) - .filter(typesForComponent => typesForComponent); - - // If all of the components could match any type, then the compound could also match any type. - if (!typesForComponents.length) { - return null; - } - - /* - * If at least one of the components could only match a particular type, the compound could only match - * the intersection of those types. - */ - return intersection(...typesForComponents); - } - - case "attribute": - case "field": - case "nth-child": - case "nth-last-child": - attributeCount++; - return null; - - case "child": - case "descendant": - case "sibling": - case "adjacent": - analyzeSelector(selector.left); - return analyzeSelector(selector.right); - - case "class": - // TODO: abstract into JSLanguage somehow - if (selector.name === "function") { - return [ - "FunctionDeclaration", - "FunctionExpression", - "ArrowFunctionExpression", - ]; - } - return null; - - default: - return null; - } - } - - const nodeTypes = analyzeSelector(parsedSelector); - - return { - nodeTypes, - attributeCount, - identifierCount, - }; -} - -/** - * Tries to parse a simple selector string, such as a single identifier or wildcard. - * This saves time by avoiding the overhead of esquery parsing for simple cases. - * @param {string} selector The selector string to parse. - * @returns {Object|null} An object describing the selector if it is simple, or `null` if it is not. - */ -function trySimpleParseSelector(selector) { - if (selector === "*") { - return { - type: "wildcard", - value: "*", - }; - } - - if (/^[a-z]+$/iu.test(selector)) { - return { - type: "identifier", - value: selector, - }; - } - - return null; -} - -/** - * Parses a raw selector string, and throws a useful error if parsing fails. - * @param {string} selector The selector string to parse. - * @returns {Object} An object (from esquery) describing the matching behavior of this selector - * @throws {Error} An error if the selector is invalid - */ -function tryParseSelector(selector) { - try { - return esquery.parse(selector); - } catch (err) { - if ( - err.location && - err.location.start && - typeof err.location.start.offset === "number" - ) { - throw new SyntaxError( - `Syntax error in selector "${selector}" at position ${err.location.start.offset}: ${err.message}`, - { - cause: err, - }, - ); - } - throw err; - } -} - -/** - * Parses a raw selector string, and returns the parsed selector along with specificity and type information. - * @param {string} source A raw AST selector - * @returns {ESQueryParsedSelector} A selector descriptor - */ -function parse(source) { - if (selectorCache.has(source)) { - return selectorCache.get(source); - } - - const cleanSource = source.replace(/:exit$/u, ""); - const parsedSelector = - trySimpleParseSelector(cleanSource) ?? tryParseSelector(cleanSource); - const { nodeTypes, attributeCount, identifierCount } = - analyzeParsedSelector(parsedSelector); - - const result = new ESQueryParsedSelector( - source, - source.endsWith(":exit"), - parsedSelector, - nodeTypes, - attributeCount, - identifierCount, - ); - - selectorCache.set(source, result); - return result; -} - -/** - * Checks if a node matches a given selector. - * @param {Object} node The node to check against the selector. - * @param {ESQuerySelector} root The root of the selector to match against. - * @param {Object[]} ancestry The ancestry of the node being checked, which is an array of nodes from the current node to the root. - * @param {ESQueryOptions} options The options to use for matching. - * @returns {boolean} `true` if the node matches the selector, `false` otherwise. - */ -function matches(node, root, ancestry, options) { - return esquery.matches(node, root, ancestry, options); -} - -//----------------------------------------------------------------------------- -// Exports -//----------------------------------------------------------------------------- - -module.exports = { - parse, - matches, - ESQueryParsedSelector, -}; diff --git a/node_modules/eslint/lib/linter/file-context.js b/node_modules/eslint/lib/linter/file-context.js deleted file mode 100644 index 40acdd63c..000000000 --- a/node_modules/eslint/lib/linter/file-context.js +++ /dev/null @@ -1,144 +0,0 @@ -/** - * @fileoverview The FileContext class. - * @author Nicholas C. Zakas - */ - -"use strict"; - -/** - * Represents a file context that the linter can use to lint a file. - */ -class FileContext { - /** - * The current working directory. - * @type {string} - */ - cwd; - - /** - * The filename of the file being linted. - * @type {string} - */ - filename; - - /** - * The physical filename of the file being linted. - * @type {string} - */ - physicalFilename; - - /** - * The source code of the file being linted. - * @type {SourceCode} - */ - sourceCode; - - /** - * The parser options for the file being linted. - * @type {Record} - * @deprecated Use `languageOptions` instead. - */ - parserOptions; - - /** - * The path to the parser used to parse this file. - * @type {string} - * @deprecated No longer supported. - */ - parserPath; - - /** - * The language options used when parsing this file. - * @type {Record} - */ - languageOptions; - - /** - * The settings for the file being linted. - * @type {Record} - */ - settings; - - /** - * Creates a new instance. - * @param {Object} config The configuration object for the file context. - * @param {string} config.cwd The current working directory. - * @param {string} config.filename The filename of the file being linted. - * @param {string} config.physicalFilename The physical filename of the file being linted. - * @param {SourceCode} config.sourceCode The source code of the file being linted. - * @param {Record} config.parserOptions The parser options for the file being linted. - * @param {string} config.parserPath The path to the parser used to parse this file. - * @param {Record} config.languageOptions The language options used when parsing this file. - * @param {Record} config.settings The settings for the file being linted. - */ - constructor({ - cwd, - filename, - physicalFilename, - sourceCode, - parserOptions, - parserPath, - languageOptions, - settings, - }) { - this.cwd = cwd; - this.filename = filename; - this.physicalFilename = physicalFilename; - this.sourceCode = sourceCode; - this.parserOptions = parserOptions; - this.parserPath = parserPath; - this.languageOptions = languageOptions; - this.settings = settings; - - Object.freeze(this); - } - - /** - * Gets the current working directory. - * @returns {string} The current working directory. - * @deprecated Use `cwd` instead. - */ - getCwd() { - return this.cwd; - } - - /** - * Gets the filename of the file being linted. - * @returns {string} The filename of the file being linted. - * @deprecated Use `filename` instead. - */ - getFilename() { - return this.filename; - } - - /** - * Gets the physical filename of the file being linted. - * @returns {string} The physical filename of the file being linted. - * @deprecated Use `physicalFilename` instead. - */ - getPhysicalFilename() { - return this.physicalFilename; - } - - /** - * Gets the source code of the file being linted. - * @returns {SourceCode} The source code of the file being linted. - * @deprecated Use `sourceCode` instead. - */ - getSourceCode() { - return this.sourceCode; - } - - /** - * Creates a new object with the current object as the prototype and - * the specified properties as its own properties. - * @param {Object} extension The properties to add to the new object. - * @returns {FileContext} A new object with the current object as the prototype - * and the specified properties as its own properties. - */ - extend(extension) { - return Object.freeze(Object.assign(Object.create(this), extension)); - } -} - -exports.FileContext = FileContext; diff --git a/node_modules/eslint/lib/linter/file-report.js b/node_modules/eslint/lib/linter/file-report.js deleted file mode 100644 index 29cea0a5d..000000000 --- a/node_modules/eslint/lib/linter/file-report.js +++ /dev/null @@ -1,608 +0,0 @@ -/** - * @fileoverview A class to track messages reported by the linter for a file. - * @author Nicholas C. Zakas - */ - -"use strict"; - -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ - -const assert = require("../shared/assert"); -const { RuleFixer } = require("./rule-fixer"); -const { interpolate } = require("./interpolate"); -const ruleReplacements = require("../../conf/replacements.json"); - -//------------------------------------------------------------------------------ -// Typedefs -//------------------------------------------------------------------------------ - -/** @typedef {import("../types").Linter.LintMessage} LintMessage */ -/** @typedef {import("../types").Linter.LintSuggestion} SuggestionResult */ -/** @typedef {import("@eslint/core").Language} Language */ -/** @typedef {import("@eslint/core").SourceLocation} SourceLocation */ - -/** - * An error message description - * @typedef {Object} MessageDescriptor - * @property {ASTNode} [node] The reported node - * @property {Location} loc The location of the problem. - * @property {string} message The problem message. - * @property {Object} [data] Optional data to use to fill in placeholders in the - * message. - * @property {Function} [fix] The function to call that creates a fix command. - * @property {Array<{desc?: string, messageId?: string, fix: Function}>} suggest Suggestion descriptions and functions to create a the associated fixes. - */ - -/** - * @typedef {Object} LintProblem - * @property {string} ruleId The rule ID that reported the problem. - * @property {string} message The problem message. - * @property {SourceLocation} loc The location of the problem. - */ - -//------------------------------------------------------------------------------ -// Helpers -//------------------------------------------------------------------------------ - -const DEFAULT_ERROR_LOC = { - start: { line: 1, column: 0 }, - end: { line: 1, column: 1 }, -}; - -/** - * Updates a given location based on the language offsets. This allows us to - * change 0-based locations to 1-based locations. We always want ESLint - * reporting lines and columns starting from 1. - * @todo Potentially this should be moved into a shared utility file. - * @param {Object} location The location to update. - * @param {number} location.line The starting line number. - * @param {number} location.column The starting column number. - * @param {number} [location.endLine] The ending line number. - * @param {number} [location.endColumn] The ending column number. - * @param {Language} language The language to use to adjust the location information. - * @returns {Object} The updated location. - */ -function updateLocationInformation( - { line, column, endLine, endColumn }, - language, -) { - const columnOffset = language.columnStart === 1 ? 0 : 1; - const lineOffset = language.lineStart === 1 ? 0 : 1; - - // calculate separately to account for undefined - const finalEndLine = endLine === void 0 ? endLine : endLine + lineOffset; - const finalEndColumn = - endColumn === void 0 ? endColumn : endColumn + columnOffset; - - return { - line: line + lineOffset, - column: column + columnOffset, - endLine: finalEndLine, - endColumn: finalEndColumn, - }; -} - -/** - * creates a missing-rule message. - * @param {string} ruleId the ruleId to create - * @returns {string} created error message - * @private - */ -function createMissingRuleMessage(ruleId) { - return Object.hasOwn(ruleReplacements.rules, ruleId) - ? `Rule '${ruleId}' was removed and replaced by: ${ruleReplacements.rules[ruleId].join(", ")}` - : `Definition for rule '${ruleId}' was not found.`; -} - -/** - * creates a linting problem - * @param {LintProblem} options to create linting error - * @param {RuleSeverity} severity the error message to report - * @param {Language} language the language to use to adjust the location information. - * @returns {LintMessage} created problem, returns a missing-rule problem if only provided ruleId. - * @private - */ -function createLintingProblem(options, severity, language) { - const { - ruleId = null, - loc = DEFAULT_ERROR_LOC, - message = createMissingRuleMessage(options.ruleId), - } = options; - - return { - ruleId, - message, - ...updateLocationInformation( - { - line: loc.start.line, - column: loc.start.column, - endLine: loc.end.line, - endColumn: loc.end.column, - }, - language, - ), - severity, - nodeType: null, - }; -} - -/** - * Translates a multi-argument context.report() call into a single object argument call - * @param {...*} args A list of arguments passed to `context.report` - * @returns {MessageDescriptor} A normalized object containing report information - */ -function normalizeMultiArgReportCall(...args) { - // If there is one argument, it is considered to be a new-style call already. - if (args.length === 1) { - // Shallow clone the object to avoid surprises if reusing the descriptor - return Object.assign({}, args[0]); - } - - // If the second argument is a string, the arguments are interpreted as [node, message, data, fix]. - if (typeof args[1] === "string") { - return { - node: args[0], - message: args[1], - data: args[2], - fix: args[3], - }; - } - - // Otherwise, the arguments are interpreted as [node, loc, message, data, fix]. - return { - node: args[0], - loc: args[1], - message: args[2], - data: args[3], - fix: args[4], - }; -} - -/** - * Asserts that either a loc or a node was provided, and the node is valid if it was provided. - * @param {MessageDescriptor} descriptor A descriptor to validate - * @returns {void} - * @throws AssertionError if neither a node nor a loc was provided, or if the node is not an object - */ -function assertValidNodeInfo(descriptor) { - if (descriptor.node) { - assert(typeof descriptor.node === "object", "Node must be an object"); - } else { - assert( - descriptor.loc, - "Node must be provided when reporting error if location is not provided", - ); - } -} - -/** - * Normalizes a MessageDescriptor to always have a `loc` with `start` and `end` properties - * @param {MessageDescriptor} descriptor A descriptor for the report from a rule. - * @returns {{start: Location, end: (Location|null)}} An updated location that infers the `start` and `end` properties - * from the `node` of the original descriptor, or infers the `start` from the `loc` of the original descriptor. - */ -function normalizeReportLoc(descriptor) { - if (descriptor.loc.start) { - return descriptor.loc; - } - return { start: descriptor.loc, end: null }; -} - -/** - * Clones the given fix object. - * @param {Fix|null} fix The fix to clone. - * @returns {Fix|null} Deep cloned fix object or `null` if `null` or `undefined` was passed in. - */ -function cloneFix(fix) { - if (!fix) { - return null; - } - - return { - range: [fix.range[0], fix.range[1]], - text: fix.text, - }; -} - -/** - * Check that a fix has a valid range. - * @param {Fix|null} fix The fix to validate. - * @returns {void} - */ -function assertValidFix(fix) { - if (fix) { - assert( - fix.range && - typeof fix.range[0] === "number" && - typeof fix.range[1] === "number", - `Fix has invalid range: ${JSON.stringify(fix, null, 2)}`, - ); - } -} - -/** - * Compares items in a fixes array by range. - * @param {Fix} a The first message. - * @param {Fix} b The second message. - * @returns {number} -1 if a comes before b, 1 if a comes after b, 0 if equal. - * @private - */ -function compareFixesByRange(a, b) { - return a.range[0] - b.range[0] || a.range[1] - b.range[1]; -} - -/** - * Merges the given fixes array into one. - * @param {Fix[]} fixes The fixes to merge. - * @param {SourceCode} sourceCode The source code object to get the text between fixes. - * @returns {{text: string, range: number[]}} The merged fixes - */ -function mergeFixes(fixes, sourceCode) { - for (const fix of fixes) { - assertValidFix(fix); - } - - if (fixes.length === 0) { - return null; - } - if (fixes.length === 1) { - return cloneFix(fixes[0]); - } - - fixes.sort(compareFixesByRange); - - const originalText = sourceCode.text; - const start = fixes[0].range[0]; - const end = fixes.at(-1).range[1]; - let text = ""; - let lastPos = Number.MIN_SAFE_INTEGER; - - for (const fix of fixes) { - assert( - fix.range[0] >= lastPos, - "Fix objects must not be overlapped in a report.", - ); - - if (fix.range[0] >= 0) { - text += originalText.slice( - Math.max(0, start, lastPos), - fix.range[0], - ); - } - text += fix.text; - lastPos = fix.range[1]; - } - text += originalText.slice(Math.max(0, start, lastPos), end); - - return { range: [start, end], text }; -} - -/** - * Gets one fix object from the given descriptor. - * If the descriptor retrieves multiple fixes, this merges those to one. - * @param {MessageDescriptor} descriptor The report descriptor. - * @param {SourceCode} sourceCode The source code object to get text between fixes. - * @returns {({text: string, range: number[]}|null)} The fix for the descriptor - */ -function normalizeFixes(descriptor, sourceCode) { - if (typeof descriptor.fix !== "function") { - return null; - } - - const ruleFixer = new RuleFixer({ sourceCode }); - - // @type {null | Fix | Fix[] | IterableIterator} - const fix = descriptor.fix(ruleFixer); - - // Merge to one. - if (fix && Symbol.iterator in fix) { - return mergeFixes(Array.from(fix), sourceCode); - } - - assertValidFix(fix); - return cloneFix(fix); -} - -/** - * Gets an array of suggestion objects from the given descriptor. - * @param {MessageDescriptor} descriptor The report descriptor. - * @param {SourceCode} sourceCode The source code object to get text between fixes. - * @param {Object} messages Object of meta messages for the rule. - * @returns {Array} The suggestions for the descriptor - */ -function mapSuggestions(descriptor, sourceCode, messages) { - if (!descriptor.suggest || !Array.isArray(descriptor.suggest)) { - return []; - } - - return ( - descriptor.suggest - .map(suggestInfo => { - const computedDesc = - suggestInfo.desc || messages[suggestInfo.messageId]; - - return { - ...suggestInfo, - desc: interpolate(computedDesc, suggestInfo.data), - fix: normalizeFixes(suggestInfo, sourceCode), - }; - }) - - // Remove suggestions that didn't provide a fix - .filter(({ fix }) => fix) - ); -} - -/** - * Creates information about the report from a descriptor - * @param {Object} options Information about the problem - * @param {string} options.ruleId Rule ID - * @param {(0|1|2)} options.severity Rule severity - * @param {(ASTNode|null)} options.node Node - * @param {string} options.message Error message - * @param {string} [options.messageId] The error message ID. - * @param {{start: SourceLocation, end: (SourceLocation|null)}} options.loc Start and end location - * @param {{text: string, range: (number[]|null)}} options.fix The fix object - * @param {Array<{text: string, range: (number[]|null)}>} options.suggestions The array of suggestions objects - * @param {Language} [options.language] The language to use to adjust line and column offsets. - * @returns {LintMessage} Information about the report - */ -function createProblem(options) { - const { language } = options; - - // calculate offsets based on the language in use - const columnOffset = language.columnStart === 1 ? 0 : 1; - const lineOffset = language.lineStart === 1 ? 0 : 1; - - const problem = { - ruleId: options.ruleId, - severity: options.severity, - message: options.message, - line: options.loc.start.line + lineOffset, - column: options.loc.start.column + columnOffset, - nodeType: (options.node && options.node.type) || null, - }; - - /* - * If this isn’t in the conditional, some of the tests fail - * because `messageId` is present in the problem object - */ - if (options.messageId) { - problem.messageId = options.messageId; - } - - if (options.loc.end) { - problem.endLine = options.loc.end.line + lineOffset; - problem.endColumn = options.loc.end.column + columnOffset; - } - - if (options.fix) { - problem.fix = options.fix; - } - - if (options.suggestions && options.suggestions.length > 0) { - problem.suggestions = options.suggestions; - } - - return problem; -} - -/** - * Validates that suggestions are properly defined. Throws if an error is detected. - * @param {Array<{ desc?: string, messageId?: string }>} suggest The incoming suggest data. - * @param {Object} messages Object of meta messages for the rule. - * @returns {void} - */ -function validateSuggestions(suggest, messages) { - if (suggest && Array.isArray(suggest)) { - suggest.forEach(suggestion => { - if (suggestion.messageId) { - const { messageId } = suggestion; - - if (!messages) { - throw new TypeError( - `context.report() called with a suggest option with a messageId '${messageId}', but no messages were present in the rule metadata.`, - ); - } - - if (!messages[messageId]) { - throw new TypeError( - `context.report() called with a suggest option with a messageId '${messageId}' which is not present in the 'messages' config: ${JSON.stringify(messages, null, 2)}`, - ); - } - - if (suggestion.desc) { - throw new TypeError( - "context.report() called with a suggest option that defines both a 'messageId' and an 'desc'. Please only pass one.", - ); - } - } else if (!suggestion.desc) { - throw new TypeError( - "context.report() called with a suggest option that doesn't have either a `desc` or `messageId`", - ); - } - - if (typeof suggestion.fix !== "function") { - throw new TypeError( - `context.report() called with a suggest option without a fix function. See: ${JSON.stringify(suggestion, null, 2)}`, - ); - } - }); - } -} - -/** - * Computes the message from a report descriptor. - * @param {MessageDescriptor} descriptor The report descriptor. - * @param {Object} messages Object of meta messages for the rule. - * @returns {string} The computed message. - * @throws {TypeError} If messageId is not found or both message and messageId are provided. - */ -function computeMessageFromDescriptor(descriptor, messages) { - if (descriptor.messageId) { - if (!messages) { - throw new TypeError( - "context.report() called with a messageId, but no messages were present in the rule metadata.", - ); - } - const id = descriptor.messageId; - - if (descriptor.message) { - throw new TypeError( - "context.report() called with a message and a messageId. Please only pass one.", - ); - } - if (!messages || !Object.hasOwn(messages, id)) { - throw new TypeError( - `context.report() called with a messageId of '${id}' which is not present in the 'messages' config: ${JSON.stringify(messages, null, 2)}`, - ); - } - return messages[id]; - } - - if (descriptor.message) { - return descriptor.message; - } - - throw new TypeError( - "Missing `message` property in report() call; add a message that describes the linting problem.", - ); -} - -/** - * A report object that contains the messages reported the linter - * for a file. - */ -class FileReport { - /** - * The messages reported by the linter for this file. - * @type {LintMessage[]} - */ - messages = []; - - /** - * A rule mapper that maps rule IDs to their metadata. - * @type {(string) => RuleDefinition} - */ - #ruleMapper; - - /** - * The source code object for the file. - * @type {SourceCode} - */ - #sourceCode; - - /** - * The language to use to adjust line and column offsets. - * @type {Language} - */ - #language; - - /** - * Whether to disable fixes for this report. - * @type {boolean} - */ - #disableFixes; - - /** - * Creates a new FileReport instance. - * @param {Object} options The options for the file report - * @param {(string) => RuleDefinition} options.ruleMapper A rule mapper that maps rule IDs to their metadata. - * @param {SourceCode} options.sourceCode The source code object for the file. - * @param {Language} options.language The language to use to adjust line and column offsets. - * @param {boolean} [options.disableFixes=false] Whether to disable fixes for this report. - */ - constructor({ ruleMapper, sourceCode, language, disableFixes = false }) { - this.#ruleMapper = ruleMapper; - this.#sourceCode = sourceCode; - this.#language = language; - this.#disableFixes = disableFixes; - } - - /** - * Adds a rule-generated message to the report. - * @param {string} ruleId The rule ID that reported the problem. - * @param {0|1|2} severity The severity of the problem (0 = off, 1 = warning, 2 = error). - * @param {...*} args The arguments passed to `context.report()`. - * @returns {LintMessage} The created message object. - * @throws {TypeError} If the messageId is not found or both message and messageId are provided. - * @throws {AssertionError} If the node is not an object or neither a node nor a loc is provided. - */ - addRuleMessage(ruleId, severity, ...args) { - const descriptor = normalizeMultiArgReportCall(...args); - const ruleDefinition = this.#ruleMapper(ruleId); - const messages = ruleDefinition?.meta?.messages; - - assertValidNodeInfo(descriptor); - - const computedMessage = computeMessageFromDescriptor( - descriptor, - messages, - ); - - validateSuggestions(descriptor.suggest, messages); - - this.messages.push( - createProblem({ - ruleId, - severity, - node: descriptor.node, - message: interpolate(computedMessage, descriptor.data), - messageId: descriptor.messageId, - loc: descriptor.loc - ? normalizeReportLoc(descriptor) - : this.#sourceCode.getLoc(descriptor.node), - fix: this.#disableFixes - ? null - : normalizeFixes(descriptor, this.#sourceCode), - suggestions: this.#disableFixes - ? [] - : mapSuggestions(descriptor, this.#sourceCode, messages), - language: this.#language, - }), - ); - - return this.messages.at(-1); - } - - /** - * Adds an error message to the report. Meant to be called outside of rules. - * @param {LintProblem} descriptor The descriptor for the error message. - * @returns {LintMessage} The created message object. - */ - addError(descriptor) { - const message = createLintingProblem(descriptor, 2, this.#language); - this.messages.push(message); - return message; - } - - /** - * Adds a fatal error message to the report. Meant to be called outside of rules. - * @param {LintProblem} descriptor The descriptor for the fatal error message. - * @returns {LintMessage} The created message object. - */ - addFatal(descriptor) { - const message = createLintingProblem(descriptor, 2, this.#language); - message.fatal = true; - this.messages.push(message); - return message; - } - - /** - * Adds a warning message to the report. Meant to be called outside of rules. - * @param {LintProblem} descriptor The descriptor for the warning message. - * @returns {LintMessage} The created message object. - */ - addWarning(descriptor) { - const message = createLintingProblem(descriptor, 1, this.#language); - this.messages.push(message); - return message; - } -} - -module.exports = { - FileReport, - updateLocationInformation, -}; diff --git a/node_modules/eslint/lib/linter/index.js b/node_modules/eslint/lib/linter/index.js index 5a86f71cd..25fd769bd 100644 --- a/node_modules/eslint/lib/linter/index.js +++ b/node_modules/eslint/lib/linter/index.js @@ -1,11 +1,13 @@ "use strict"; const { Linter } = require("./linter"); +const interpolate = require("./interpolate"); const SourceCodeFixer = require("./source-code-fixer"); module.exports = { - Linter, + Linter, - // For testers. - SourceCodeFixer, + // For testers. + SourceCodeFixer, + interpolate }; diff --git a/node_modules/eslint/lib/linter/interpolate.js b/node_modules/eslint/lib/linter/interpolate.js index 6327f76e7..87e06a023 100644 --- a/node_modules/eslint/lib/linter/interpolate.js +++ b/node_modules/eslint/lib/linter/interpolate.js @@ -9,42 +9,20 @@ // Public Interface //------------------------------------------------------------------------------ -/** - * Returns a global expression matching placeholders in messages. - * @returns {RegExp} Global regular expression matching placeholders - */ -function getPlaceholderMatcher() { - return /\{\{([^{}]+)\}\}/gu; -} - -/** - * Replaces {{ placeholders }} in the message with the provided data. - * Does not replace placeholders not available in the data. - * @param {string} text Original message with potential placeholders - * @param {Record} data Map of placeholder name to its value - * @returns {string} Message with replaced placeholders - */ -function interpolate(text, data) { - if (!data) { - return text; - } - - const matcher = getPlaceholderMatcher(); - - // Substitution content for any {{ }} markers. - return text.replace(matcher, (fullMatch, termWithWhitespace) => { - const term = termWithWhitespace.trim(); - - if (term in data) { - return data[term]; - } - - // Preserve old behavior: If parameter name not provided, don't replace it. - return fullMatch; - }); -} - -module.exports = { - getPlaceholderMatcher, - interpolate, +module.exports = (text, data) => { + if (!data) { + return text; + } + + // Substitution content for any {{ }} markers. + return text.replace(/\{\{([^{}]+?)\}\}/gu, (fullMatch, termWithWhitespace) => { + const term = termWithWhitespace.trim(); + + if (term in data) { + return data[term]; + } + + // Preserve old behavior: If parameter name not provided, don't replace it. + return fullMatch; + }); }; diff --git a/node_modules/eslint/lib/linter/linter.js b/node_modules/eslint/lib/linter/linter.js index 59d8fab59..d25f85403 100644 --- a/node_modules/eslint/lib/linter/linter.js +++ b/node_modules/eslint/lib/linter/linter.js @@ -10,77 +10,64 @@ // Requirements //------------------------------------------------------------------------------ -const path = require("node:path"), - eslintScope = require("eslint-scope"), - evk = require("eslint-visitor-keys"), - espree = require("espree"), - merge = require("lodash.merge"), - pkg = require("../../package.json"), - { - Legacy: { - ConfigOps, - ConfigValidator, - environments: BuiltInEnvironments, - }, - } = require("@eslint/eslintrc/universal"), - Traverser = require("../shared/traverser"), - { SourceCode } = require("../languages/js/source-code"), - applyDisableDirectives = require("./apply-disable-directives"), - { ConfigCommentParser } = require("@eslint/plugin-kit"), - Rules = require("./rules"), - SourceCodeFixer = require("./source-code-fixer"), - { SourceCodeVisitor } = require("./source-code-visitor"), - timing = require("./timing"); +const + path = require("path"), + eslintScope = require("eslint-scope"), + evk = require("eslint-visitor-keys"), + espree = require("espree"), + merge = require("lodash.merge"), + pkg = require("../../package.json"), + astUtils = require("../shared/ast-utils"), + { + directivesPattern + } = require("../shared/directives"), + { + Legacy: { + ConfigOps, + ConfigValidator, + environments: BuiltInEnvironments + } + } = require("@eslint/eslintrc/universal"), + Traverser = require("../shared/traverser"), + { SourceCode } = require("../source-code"), + CodePathAnalyzer = require("./code-path-analysis/code-path-analyzer"), + applyDisableDirectives = require("./apply-disable-directives"), + ConfigCommentParser = require("./config-comment-parser"), + NodeEventGenerator = require("./node-event-generator"), + createReportTranslator = require("./report-translator"), + Rules = require("./rules"), + createEmitter = require("./safe-emitter"), + SourceCodeFixer = require("./source-code-fixer"), + timing = require("./timing"), + ruleReplacements = require("../../conf/replacements.json"); +const { getRuleFromConfig } = require("../config/flat-config-helpers"); const { FlatConfigArray } = require("../config/flat-config-array"); -const { startTime, endTime } = require("../shared/stats"); -const { assertIsRuleSeverity } = require("../config/flat-config-schema"); -const { - normalizeSeverityToString, - normalizeSeverityToNumber, -} = require("../shared/severity"); -const { deepMergeArrays } = require("../shared/deep-merge-arrays"); -const jslang = require("../languages/js"); -const { - activeFlags, - inactiveFlags, - getInactivityReasonMessage, -} = require("../shared/flags"); +const { RuleValidator } = require("../config/rule-validator"); +const { assertIsRuleOptions, assertIsRuleSeverity } = require("../config/flat-config-schema"); +const { normalizeSeverityToString } = require("../shared/severity"); const debug = require("debug")("eslint:linter"); const MAX_AUTOFIX_PASSES = 10; const DEFAULT_PARSER_NAME = "espree"; const DEFAULT_ECMA_VERSION = 5; const commentParser = new ConfigCommentParser(); +const DEFAULT_ERROR_LOC = { start: { line: 1, column: 0 }, end: { line: 1, column: 1 } }; const parserSymbol = Symbol.for("eslint.RuleTester.parser"); -const { LATEST_ECMA_VERSION } = require("../../conf/ecma-version"); -const { VFile } = require("./vfile"); -const { ParserService } = require("../services/parser-service"); -const { FileContext } = require("./file-context"); -const { ProcessorService } = require("../services/processor-service"); -const { containsDifferentProperty } = require("../shared/option-utils"); -const { Config } = require("../config/config"); -const { WarningService } = require("../services/warning-service"); -const { SourceCodeTraverser } = require("./source-code-traverser"); -const { FileReport, updateLocationInformation } = require("./file-report"); //------------------------------------------------------------------------------ // Typedefs //------------------------------------------------------------------------------ -/** @import { Language, LanguageOptions, RuleConfig, RuleDefinition, RuleSeverity } from "@eslint/core" */ - -/** @typedef {import("../types").Linter.Config} Config */ -/** @typedef {import("../types").ESLint.ConfigData} ConfigData */ -/** @typedef {import("../types").ESLint.Environment} Environment */ -/** @typedef {import("../types").Linter.GlobalConf} GlobalConf */ -/** @typedef {import("../types").Linter.LanguageOptions} JSLanguageOptions */ -/** @typedef {import("../types").Linter.LintMessage} LintMessage */ -/** @typedef {import("../types").Linter.Parser} Parser */ -/** @typedef {import("../types").Linter.ParserOptions} ParserOptions */ -/** @typedef {import("../types").Linter.Processor} Processor */ -/** @typedef {import("../types").Rule.RuleModule} Rule */ -/** @typedef {import("../types").Linter.StringSeverity} StringSeverity */ -/** @typedef {import("../types").Linter.SuppressedLintMessage} SuppressedLintMessage */ -/** @typedef {import("../types").Linter.TimePass} TimePass */ +/** @typedef {InstanceType} ConfigArray */ +/** @typedef {InstanceType} ExtractedConfig */ +/** @typedef {import("../shared/types").ConfigData} ConfigData */ +/** @typedef {import("../shared/types").Environment} Environment */ +/** @typedef {import("../shared/types").GlobalConf} GlobalConf */ +/** @typedef {import("../shared/types").LintMessage} LintMessage */ +/** @typedef {import("../shared/types").SuppressedLintMessage} SuppressedLintMessage */ +/** @typedef {import("../shared/types").ParserOptions} ParserOptions */ +/** @typedef {import("../shared/types").LanguageOptions} LanguageOptions */ +/** @typedef {import("../shared/types").Processor} Processor */ +/** @typedef {import("../shared/types").Rule} Rule */ /* eslint-disable jsdoc/valid-types -- https://github.com/jsdoc-type-pratt-parser/jsdoc-type-pratt-parser/issues/4#issuecomment-778805577 */ /** @@ -105,9 +92,7 @@ const { FileReport, updateLocationInformation } = require("./file-report"); * @property {SourceCode|null} lastSourceCode The `SourceCode` instance that the last `verify()` call used. * @property {SuppressedLintMessage[]} lastSuppressedMessages The `SuppressedLintMessage[]` instance that the last `verify()` call produced. * @property {Map} parserMap The loaded parsers. - * @property {{ passes: TimePass[]; }} times The times spent on applying a rule to a file (see `stats` option). * @property {Rules} ruleMap The loaded rules. - * @property {WarningService} warningService The warning service. */ /** @@ -120,7 +105,6 @@ const { FileReport, updateLocationInformation } = require("./file-report"); * @property {string} [filename] the filename of the source code. * @property {boolean | "off" | "warn" | "error"} [reportUnusedDisableDirectives] Adds reported errors for * unused `eslint-disable` directives. - * @property {Function} [ruleFilter] A predicate function that determines whether a given rule should run. */ /** @@ -146,8 +130,7 @@ const { FileReport, updateLocationInformation } = require("./file-report"); /** * @typedef {Object} InternalOptions * @property {string | null} warnInlineConfig The config name what `noInlineConfig` setting came from. If `noInlineConfig` setting didn't exist, this is null. If this is a config name, then the linter warns directive comments. - * @property {StringSeverity} reportUnusedDisableDirectives Severity to report unused disable directives, if not "off" (boolean values were normalized). - * @property {StringSeverity} reportUnusedInlineConfigs Severity to report unused inline configs, if not "off". + * @property {"off" | "warn" | "error"} reportUnusedDisableDirectives (boolean values were normalized) */ //------------------------------------------------------------------------------ @@ -160,7 +143,7 @@ const { FileReport, updateLocationInformation } = require("./file-report"); * @returns {boolean} True if the parser is Espree or false if not. */ function isEspree(parser) { - return !!(parser === espree || parser[parserSymbol] === espree); + return !!(parser === espree || parser[parserSymbol] === espree); } /** @@ -172,227 +155,166 @@ function isEspree(parser) { * @param {{exportedVariables: Object, enabledGlobals: Object}} commentDirectives Directives from comment configuration * @returns {void} */ -function addDeclaredGlobals( - globalScope, - configGlobals, - { exportedVariables, enabledGlobals }, -) { - // Define configured global variables. - for (const id of new Set([ - ...Object.keys(configGlobals), - ...Object.keys(enabledGlobals), - ])) { - /* - * `ConfigOps.normalizeConfigGlobal` will throw an error if a configured global value is invalid. However, these errors would - * typically be caught when validating a config anyway (validity for inline global comments is checked separately). - */ - const configValue = - configGlobals[id] === void 0 - ? void 0 - : ConfigOps.normalizeConfigGlobal(configGlobals[id]); - const commentValue = enabledGlobals[id] && enabledGlobals[id].value; - const value = commentValue || configValue; - const sourceComments = - enabledGlobals[id] && enabledGlobals[id].comments; - - if (value === "off") { - continue; - } - - let variable = globalScope.set.get(id); - - if (!variable) { - variable = new eslintScope.Variable(id, globalScope); - - globalScope.variables.push(variable); - globalScope.set.set(id, variable); - } - - variable.eslintImplicitGlobalSetting = configValue; - variable.eslintExplicitGlobal = sourceComments !== void 0; - variable.eslintExplicitGlobalComments = sourceComments; - variable.writeable = value === "writable"; - } - - // mark all exported variables as such - Object.keys(exportedVariables).forEach(name => { - const variable = globalScope.set.get(name); - - if (variable) { - variable.eslintUsed = true; - variable.eslintExported = true; - } - }); - - /* - * "through" contains all references which definitions cannot be found. - * Since we augment the global scope using configuration, we need to update - * references and remove the ones that were added by configuration. - */ - globalScope.through = globalScope.through.filter(reference => { - const name = reference.identifier.name; - const variable = globalScope.set.get(name); - - if (variable) { - /* - * Links the variable and the reference. - * And this reference is removed from `Scope#through`. - */ - reference.resolved = variable; - variable.references.push(reference); - - return false; - } - - return true; - }); +function addDeclaredGlobals(globalScope, configGlobals, { exportedVariables, enabledGlobals }) { + + // Define configured global variables. + for (const id of new Set([...Object.keys(configGlobals), ...Object.keys(enabledGlobals)])) { + + /* + * `ConfigOps.normalizeConfigGlobal` will throw an error if a configured global value is invalid. However, these errors would + * typically be caught when validating a config anyway (validity for inline global comments is checked separately). + */ + const configValue = configGlobals[id] === void 0 ? void 0 : ConfigOps.normalizeConfigGlobal(configGlobals[id]); + const commentValue = enabledGlobals[id] && enabledGlobals[id].value; + const value = commentValue || configValue; + const sourceComments = enabledGlobals[id] && enabledGlobals[id].comments; + + if (value === "off") { + continue; + } + + let variable = globalScope.set.get(id); + + if (!variable) { + variable = new eslintScope.Variable(id, globalScope); + + globalScope.variables.push(variable); + globalScope.set.set(id, variable); + } + + variable.eslintImplicitGlobalSetting = configValue; + variable.eslintExplicitGlobal = sourceComments !== void 0; + variable.eslintExplicitGlobalComments = sourceComments; + variable.writeable = (value === "writable"); + } + + // mark all exported variables as such + Object.keys(exportedVariables).forEach(name => { + const variable = globalScope.set.get(name); + + if (variable) { + variable.eslintUsed = true; + variable.eslintExported = true; + } + }); + + /* + * "through" contains all references which definitions cannot be found. + * Since we augment the global scope using configuration, we need to update + * references and remove the ones that were added by configuration. + */ + globalScope.through = globalScope.through.filter(reference => { + const name = reference.identifier.name; + const variable = globalScope.set.get(name); + + if (variable) { + + /* + * Links the variable and the reference. + * And this reference is removed from `Scope#through`. + */ + reference.resolved = variable; + variable.references.push(reference); + + return false; + } + + return true; + }); } /** - * Wraps the value in an Array if it isn't already one. - * @template T - * @param {T|T[]} value Value to be wrapped. - * @returns {Array} The value as an array. + * creates a missing-rule message. + * @param {string} ruleId the ruleId to create + * @returns {string} created error message + * @private */ -function asArray(value) { - return Array.isArray(value) ? value : [value]; +function createMissingRuleMessage(ruleId) { + return Object.prototype.hasOwnProperty.call(ruleReplacements.rules, ruleId) + ? `Rule '${ruleId}' was removed and replaced by: ${ruleReplacements.rules[ruleId].join(", ")}` + : `Definition for rule '${ruleId}' was not found.`; } /** - * Pushes a problem to inlineConfigProblems if ruleOptions are redundant. - * @param {Config} config Provided config. - * @param {Object} loc A line/column location - * @param {FileReport} report Report that may be added to. - * @param {string} ruleId The rule ID. - * @param {Array} ruleOptions The rule options, merged with the config's. - * @param {Array} ruleOptionsInline The rule options from the comment. - * @param {"error"|"warn"} severity The severity to report. - * @returns {void} + * creates a linting problem + * @param {Object} options to create linting error + * @param {string} [options.ruleId] the ruleId to report + * @param {Object} [options.loc] the loc to report + * @param {string} [options.message] the error message to report + * @param {string} [options.severity] the error message to report + * @returns {LintMessage} created problem, returns a missing-rule problem if only provided ruleId. + * @private */ -function addProblemIfSameSeverityAndOptions( - config, - loc, - report, - ruleId, - ruleOptions, - ruleOptionsInline, - severity, -) { - const existingConfigRaw = config.rules?.[ruleId]; - const existingConfig = existingConfigRaw - ? asArray(existingConfigRaw) - : ["off"]; - const existingSeverity = normalizeSeverityToString(existingConfig[0]); - const inlineSeverity = normalizeSeverityToString(ruleOptions[0]); - const sameSeverity = existingSeverity === inlineSeverity; - - if (!sameSeverity) { - return; - } - - const alreadyConfigured = existingConfigRaw - ? `is already configured to '${existingSeverity}'` - : "is not enabled so can't be turned off"; - let message; - - if ( - (existingConfig.length === 1 && ruleOptions.length === 1) || - existingSeverity === "off" - ) { - message = `Unused inline config ('${ruleId}' ${alreadyConfigured}).`; - } else if ( - !containsDifferentProperty( - ruleOptions.slice(1), - existingConfig.slice(1), - ) - ) { - message = - ruleOptionsInline.length === 1 - ? `Unused inline config ('${ruleId}' ${alreadyConfigured}).` - : `Unused inline config ('${ruleId}' ${alreadyConfigured} with the same options).`; - } - - if (message) { - const numericSeverity = normalizeSeverityToNumber(severity); - const descriptor = { - message, - loc, - }; - - if (numericSeverity === 1) { - report.addWarning(descriptor); - } else if (numericSeverity === 2) { - report.addError(descriptor); - } - } +function createLintingProblem(options) { + const { + ruleId = null, + loc = DEFAULT_ERROR_LOC, + message = createMissingRuleMessage(options.ruleId), + severity = 2 + } = options; + + return { + ruleId, + message, + line: loc.start.line, + column: loc.start.column + 1, + endLine: loc.end.line, + endColumn: loc.end.column + 1, + severity, + nodeType: null + }; } /** * Creates a collection of disable directives from a comment * @param {Object} options to create disable directives * @param {("disable"|"enable"|"disable-line"|"disable-next-line")} options.type The type of directive comment + * @param {token} options.commentToken The Comment token * @param {string} options.value The value after the directive in the comment * comment specified no specific rules, so it applies to all rules (e.g. `eslint-disable`) * @param {string} options.justification The justification of the directive - * @param {ASTNode|token} options.node The Comment node/token. - * @param {function(string): {create: Function}} ruleMapper A map from rule IDs to defined rules - * @param {Language} language The language to use to adjust the location information. - * @param {SourceCode} sourceCode The SourceCode object to get comments from. - * @param {FileReport} report The report to add problems to. - * @returns {Object[]} Directives from the comment + * @param {function(string): {create: Function}} options.ruleMapper A map from rule IDs to defined rules + * @returns {Object} Directives and problems from the comment */ -function createDisableDirectives( - { type, value, justification, node }, - ruleMapper, - language, - sourceCode, - report, -) { - const ruleIds = Object.keys(commentParser.parseListConfig(value)); - const directiveRules = ruleIds.length ? ruleIds : [null]; - const directives = []; // valid disable directives - const parentDirective = { node, value, ruleIds }; - - for (const ruleId of directiveRules) { - const loc = sourceCode.getLoc(node); - - // push to directives, if the rule is defined(including null, e.g. /*eslint enable*/) - if (ruleId === null || !!ruleMapper(ruleId)) { - if (type === "disable-next-line") { - const { line, column } = updateLocationInformation( - loc.end, - language, - ); - - directives.push({ - parentDirective, - type, - line, - column, - ruleId, - justification, - }); - } else { - const { line, column } = updateLocationInformation( - loc.start, - language, - ); - - directives.push({ - parentDirective, - type, - line, - column, - ruleId, - justification, - }); - } - } else { - report.addError({ ruleId, loc }); - } - } - - return directives; +function createDisableDirectives(options) { + const { commentToken, type, value, justification, ruleMapper } = options; + const ruleIds = Object.keys(commentParser.parseListConfig(value)); + const directiveRules = ruleIds.length ? ruleIds : [null]; + const result = { + directives: [], // valid disable directives + directiveProblems: [] // problems in directives + }; + + const parentComment = { commentToken, ruleIds }; + + for (const ruleId of directiveRules) { + + // push to directives, if the rule is defined(including null, e.g. /*eslint enable*/) + if (ruleId === null || !!ruleMapper(ruleId)) { + if (type === "disable-next-line") { + result.directives.push({ + parentComment, + type, + line: commentToken.loc.end.line, + column: commentToken.loc.end.column + 1, + ruleId, + justification + }); + } else { + result.directives.push({ + parentComment, + type, + line: commentToken.loc.start.line, + column: commentToken.loc.start.column + 1, + ruleId, + justification + }); + } + } else { + result.directiveProblems.push(createLintingProblem({ ruleId, loc: commentToken.loc })); + } + } + return result; } /** @@ -402,289 +324,214 @@ function createDisableDirectives( * @param {SourceCode} sourceCode The SourceCode object to get comments from. * @param {function(string): {create: Function}} ruleMapper A map from rule IDs to defined rules * @param {string|null} warnInlineConfig If a string then it should warn directive comments as disabled. The string value is the config name what the setting came from. - * @param {ConfigData} config Provided config. - * @param {FileReport} report The report to add problems to. - * @returns {{configuredRules: Object, enabledGlobals: {value:string,comment:Token}[], exportedVariables: Object, disableDirectives: DisableDirective[]}} + * @returns {{configuredRules: Object, enabledGlobals: {value:string,comment:Token}[], exportedVariables: Object, problems: LintMessage[], disableDirectives: DisableDirective[]}} * A collection of the directive comments that were found, along with any problems that occurred when parsing */ -function getDirectiveComments( - sourceCode, - ruleMapper, - warnInlineConfig, - config, - report, -) { - const configuredRules = {}; - const enabledGlobals = Object.create(null); - const exportedVariables = {}; - const disableDirectives = []; - const validator = new ConfigValidator({ - builtInRules: Rules, - }); - - sourceCode - .getInlineConfigNodes() - .filter(token => token.type !== "Shebang") - .forEach(comment => { - const directive = commentParser.parseDirective(comment.value); - - if (!directive) { - return; - } - - const { - label, - value, - justification: justificationPart, - } = directive; - - const lineCommentSupported = - /^eslint-disable-(?:next-)?line$/u.test(label); - - if (comment.type === "Line" && !lineCommentSupported) { - return; - } - - const loc = sourceCode.getLoc(comment); - - if (warnInlineConfig) { - const kind = - comment.type === "Block" ? `/*${label}*/` : `//${label}`; - - report.addWarning({ - ruleId: null, - message: `'${kind}' has no effect because you have 'noInlineConfig' setting in ${warnInlineConfig}.`, - loc, - }); - return; - } - - if ( - label === "eslint-disable-line" && - loc.start.line !== loc.end.line - ) { - const message = `${label} comment should not span multiple lines.`; - - report.addError({ - message, - loc, - }); - return; - } - - switch (label) { - case "eslint-disable": - case "eslint-enable": - case "eslint-disable-next-line": - case "eslint-disable-line": { - const directiveType = label.slice("eslint-".length); - const directives = createDisableDirectives( - { - type: directiveType, - value, - justification: justificationPart, - node: comment, - }, - ruleMapper, - jslang, - sourceCode, - report, - ); - - disableDirectives.push(...directives); - break; - } - - case "exported": - Object.assign( - exportedVariables, - commentParser.parseListConfig(value), - ); - break; - - case "globals": - case "global": - for (const [id, idSetting] of Object.entries( - commentParser.parseStringConfig(value), - )) { - let normalizedValue; - - try { - normalizedValue = - ConfigOps.normalizeConfigGlobal(idSetting); - } catch (err) { - report.addError({ - loc, - message: err.message, - }); - continue; - } - - if (enabledGlobals[id]) { - enabledGlobals[id].comments.push(comment); - enabledGlobals[id].value = normalizedValue; - } else { - enabledGlobals[id] = { - comments: [comment], - value: normalizedValue, - }; - } - } - break; - - case "eslint": { - const parseResult = - commentParser.parseJSONLikeConfig(value); - - if (parseResult.ok) { - Object.keys(parseResult.config).forEach(name => { - const rule = ruleMapper(name); - const ruleValue = parseResult.config[name]; - - if (!rule) { - report.addError({ ruleId: name, loc }); - return; - } - - if (Object.hasOwn(configuredRules, name)) { - report.addError({ - message: `Rule "${name}" is already configured by another configuration comment in the preceding code. This configuration is ignored.`, - loc, - }); - return; - } - - let ruleOptions = asArray(ruleValue); - - /* - * If the rule was already configured, inline rule configuration that - * only has severity should retain options from the config and just override the severity. - * - * Example: - * - * { - * rules: { - * curly: ["error", "multi"] - * } - * } - * - * /* eslint curly: ["warn"] * / - * - * Results in: - * - * curly: ["warn", "multi"] - */ - if ( - /* - * If inline config for the rule has only severity - */ - ruleOptions.length === 1 && - /* - * And the rule was already configured - */ - config.rules && - Object.hasOwn(config.rules, name) - ) { - /* - * Then use severity from the inline config and options from the provided config - */ - ruleOptions = [ - ruleOptions[0], // severity from the inline config - ...asArray(config.rules[name]).slice(1), // options from the provided config - ]; - } - - try { - validator.validateRuleOptions( - rule, - name, - ruleOptions, - ); - } catch (err) { - /* - * If the rule has invalid `meta.schema`, throw the error because - * this is not an invalid inline configuration but an invalid rule. - */ - if ( - err.code === - "ESLINT_INVALID_RULE_OPTIONS_SCHEMA" - ) { - throw err; - } - - report.addError({ - ruleId: name, - message: err.message, - loc, - }); - - // do not apply the config, if found invalid options. - return; - } - - configuredRules[name] = ruleOptions; - }); - } else { - report.addFatal({ - loc, - message: parseResult.error.message, - }); - } - - break; - } - - // no default - } - }); - - return { - configuredRules, - enabledGlobals, - exportedVariables, - disableDirectives, - }; +function getDirectiveComments(sourceCode, ruleMapper, warnInlineConfig) { + const configuredRules = {}; + const enabledGlobals = Object.create(null); + const exportedVariables = {}; + const problems = []; + const disableDirectives = []; + const validator = new ConfigValidator({ + builtInRules: Rules + }); + + sourceCode.getInlineConfigNodes().filter(token => token.type !== "Shebang").forEach(comment => { + const { directivePart, justificationPart } = commentParser.extractDirectiveComment(comment.value); + + const match = directivesPattern.exec(directivePart); + + if (!match) { + return; + } + const directiveText = match[1]; + const lineCommentSupported = /^eslint-disable-(next-)?line$/u.test(directiveText); + + if (comment.type === "Line" && !lineCommentSupported) { + return; + } + + if (warnInlineConfig) { + const kind = comment.type === "Block" ? `/*${directiveText}*/` : `//${directiveText}`; + + problems.push(createLintingProblem({ + ruleId: null, + message: `'${kind}' has no effect because you have 'noInlineConfig' setting in ${warnInlineConfig}.`, + loc: comment.loc, + severity: 1 + })); + return; + } + + if (directiveText === "eslint-disable-line" && comment.loc.start.line !== comment.loc.end.line) { + const message = `${directiveText} comment should not span multiple lines.`; + + problems.push(createLintingProblem({ + ruleId: null, + message, + loc: comment.loc + })); + return; + } + + const directiveValue = directivePart.slice(match.index + directiveText.length); + + switch (directiveText) { + case "eslint-disable": + case "eslint-enable": + case "eslint-disable-next-line": + case "eslint-disable-line": { + const directiveType = directiveText.slice("eslint-".length); + const options = { commentToken: comment, type: directiveType, value: directiveValue, justification: justificationPart, ruleMapper }; + const { directives, directiveProblems } = createDisableDirectives(options); + + disableDirectives.push(...directives); + problems.push(...directiveProblems); + break; + } + + case "exported": + Object.assign(exportedVariables, commentParser.parseStringConfig(directiveValue, comment)); + break; + + case "globals": + case "global": + for (const [id, { value }] of Object.entries(commentParser.parseStringConfig(directiveValue, comment))) { + let normalizedValue; + + try { + normalizedValue = ConfigOps.normalizeConfigGlobal(value); + } catch (err) { + problems.push(createLintingProblem({ + ruleId: null, + loc: comment.loc, + message: err.message + })); + continue; + } + + if (enabledGlobals[id]) { + enabledGlobals[id].comments.push(comment); + enabledGlobals[id].value = normalizedValue; + } else { + enabledGlobals[id] = { + comments: [comment], + value: normalizedValue + }; + } + } + break; + + case "eslint": { + const parseResult = commentParser.parseJsonConfig(directiveValue, comment.loc); + + if (parseResult.success) { + Object.keys(parseResult.config).forEach(name => { + const rule = ruleMapper(name); + const ruleValue = parseResult.config[name]; + + if (!rule) { + problems.push(createLintingProblem({ ruleId: name, loc: comment.loc })); + return; + } + + try { + validator.validateRuleOptions(rule, name, ruleValue); + } catch (err) { + problems.push(createLintingProblem({ + ruleId: name, + message: err.message, + loc: comment.loc + })); + + // do not apply the config, if found invalid options. + return; + } + + configuredRules[name] = ruleValue; + }); + } else { + problems.push(parseResult.error); + } + + break; + } + + // no default + } + }); + + return { + configuredRules, + enabledGlobals, + exportedVariables, + problems, + disableDirectives + }; } /** * Parses comments in file to extract disable directives. * @param {SourceCode} sourceCode The SourceCode object to get comments from. * @param {function(string): {create: Function}} ruleMapper A map from rule IDs to defined rules - * @param {Language} language The language to use to adjust the location information - * @param {FileReport} report The report to add problems to. - * @returns {DisableDirective[]} + * @returns {{problems: LintMessage[], disableDirectives: DisableDirective[]}} * A collection of the directive comments that were found, along with any problems that occurred when parsing */ -function getDirectiveCommentsForFlatConfig( - sourceCode, - ruleMapper, - language, - report, -) { - const disableDirectives = []; - - if (sourceCode.getDisableDirectives) { - const { directives: directivesSources, problems: directivesProblems } = - sourceCode.getDisableDirectives(); - - if (Array.isArray(directivesProblems)) { - directivesProblems.forEach(problem => report.addError(problem)); - } - - directivesSources.forEach(directive => { - const directives = createDisableDirectives( - directive, - ruleMapper, - language, - sourceCode, - report, - ); - - disableDirectives.push(...directives); - }); - } - - return disableDirectives; +function getDirectiveCommentsForFlatConfig(sourceCode, ruleMapper) { + const problems = []; + const disableDirectives = []; + + sourceCode.getInlineConfigNodes().filter(token => token.type !== "Shebang").forEach(comment => { + const { directivePart, justificationPart } = commentParser.extractDirectiveComment(comment.value); + + const match = directivesPattern.exec(directivePart); + + if (!match) { + return; + } + const directiveText = match[1]; + const lineCommentSupported = /^eslint-disable-(next-)?line$/u.test(directiveText); + + if (comment.type === "Line" && !lineCommentSupported) { + return; + } + + if (directiveText === "eslint-disable-line" && comment.loc.start.line !== comment.loc.end.line) { + const message = `${directiveText} comment should not span multiple lines.`; + + problems.push(createLintingProblem({ + ruleId: null, + message, + loc: comment.loc + })); + return; + } + + const directiveValue = directivePart.slice(match.index + directiveText.length); + + switch (directiveText) { + case "eslint-disable": + case "eslint-enable": + case "eslint-disable-next-line": + case "eslint-disable-line": { + const directiveType = directiveText.slice("eslint-".length); + const options = { commentToken: comment, type: directiveType, value: directiveValue, justification: justificationPart, ruleMapper }; + const { directives, directiveProblems } = createDisableDirectives(options); + + disableDirectives.push(...directives); + problems.push(...directiveProblems); + break; + } + + // no default + } + }); + + return { + problems, + disableDirectives + }; } /** @@ -694,17 +541,18 @@ function getDirectiveCommentsForFlatConfig( * @returns {number} normalized ECMAScript version */ function normalizeEcmaVersion(parser, ecmaVersion) { - if (isEspree(parser)) { - if (ecmaVersion === "latest") { - return espree.latestEcmaVersion; - } - } - - /* - * Calculate ECMAScript edition number from official year version starting with - * ES2015, which corresponds with ES6 (or a difference of 2009). - */ - return ecmaVersion >= 2015 ? ecmaVersion - 2009 : ecmaVersion; + + if (isEspree(parser)) { + if (ecmaVersion === "latest") { + return espree.latestEcmaVersion; + } + } + + /* + * Calculate ECMAScript edition number from official year version starting with + * ES2015, which corresponds with ES6 (or a difference of 2009). + */ + return ecmaVersion >= 2015 ? ecmaVersion - 2009 : ecmaVersion; } /** @@ -714,31 +562,32 @@ function normalizeEcmaVersion(parser, ecmaVersion) { * @returns {number} normalized ECMAScript version */ function normalizeEcmaVersionForLanguageOptions(ecmaVersion) { - switch (ecmaVersion) { - case 3: - return 3; - - // void 0 = no ecmaVersion specified so use the default - case 5: - case void 0: - return 5; - - default: - if (typeof ecmaVersion === "number") { - return ecmaVersion >= 2015 ? ecmaVersion : ecmaVersion + 2009; - } - } - - /* - * We default to the latest supported ecmaVersion for everything else. - * Remember, this is for languageOptions.ecmaVersion, which sets the version - * that is used for a number of processes inside of ESLint. It's normally - * safe to assume people want the latest unless otherwise specified. - */ - return LATEST_ECMA_VERSION; + + switch (ecmaVersion) { + case 3: + return 3; + + // void 0 = no ecmaVersion specified so use the default + case 5: + case void 0: + return 5; + + default: + if (typeof ecmaVersion === "number") { + return ecmaVersion >= 2015 ? ecmaVersion : ecmaVersion + 2009; + } + } + + /* + * We default to the latest supported ecmaVersion for everything else. + * Remember, this is for languageOptions.ecmaVersion, which sets the version + * that is used for a number of processes inside of ESLint. It's normally + * safe to assume people want the latest unless otherwise specified. + */ + return espree.latestEcmaVersion + 2009; } -const eslintEnvPattern = /\/\*\s*eslint-env\s.+?(?:\*\/|$)/gsu; +const eslintEnvPattern = /\/\*\s*eslint-env\s(.+?)(?:\*\/|$)/gsu; /** * Checks whether or not there is a comment which has "eslint-env *" in a given text. @@ -746,22 +595,20 @@ const eslintEnvPattern = /\/\*\s*eslint-env\s.+?(?:\*\/|$)/gsu; * @returns {Object|null} A result of parseListConfig() with "eslint-env *" comment. */ function findEslintEnv(text) { - let match, retv; - - eslintEnvPattern.lastIndex = 0; - - while ((match = eslintEnvPattern.exec(text)) !== null) { - if (match[0].endsWith("*/")) { - retv = Object.assign( - retv || {}, - commentParser.parseListConfig( - commentParser.parseDirective(match[0].slice(2, -2)).value, - ), - ); - } - } - - return retv; + let match, retv; + + eslintEnvPattern.lastIndex = 0; + + while ((match = eslintEnvPattern.exec(text)) !== null) { + if (match[0].endsWith("*/")) { + retv = Object.assign( + retv || {}, + commentParser.parseListConfig(commentParser.extractDirectiveComment(match[1]).directivePart) + ); + } + } + + return retv; } /** @@ -777,77 +624,52 @@ function findEslintEnv(text) { * @returns {string} The normalized filename. */ function normalizeFilename(filename) { - const parts = filename.split(path.sep); - const index = parts.lastIndexOf(""); + const parts = filename.split(path.sep); + const index = parts.lastIndexOf(""); - return index === -1 ? filename : parts.slice(index).join(path.sep); + return index === -1 ? filename : parts.slice(index).join(path.sep); } /** * Normalizes the possible options for `linter.verify` and `linter.verifyAndFix` to a * consistent shape. * @param {VerifyOptions} providedOptions Options - * @param {Config|ConfigData} config Config. + * @param {ConfigData} config Config. * @returns {Required & InternalOptions} Normalized options */ function normalizeVerifyOptions(providedOptions, config) { - const linterOptions = config.linterOptions || config; - - // .noInlineConfig for eslintrc, .linterOptions.noInlineConfig for flat - const disableInlineConfig = linterOptions.noInlineConfig === true; - const ignoreInlineConfig = providedOptions.allowInlineConfig === false; - const configNameOfNoInlineConfig = config.configNameOfNoInlineConfig - ? ` (${config.configNameOfNoInlineConfig})` - : ""; - - let reportUnusedDisableDirectives = - providedOptions.reportUnusedDisableDirectives; - - if (typeof reportUnusedDisableDirectives === "boolean") { - reportUnusedDisableDirectives = reportUnusedDisableDirectives - ? "error" - : "off"; - } - if (typeof reportUnusedDisableDirectives !== "string") { - if (typeof linterOptions.reportUnusedDisableDirectives === "boolean") { - reportUnusedDisableDirectives = - linterOptions.reportUnusedDisableDirectives ? "warn" : "off"; - } else { - reportUnusedDisableDirectives = - linterOptions.reportUnusedDisableDirectives === void 0 - ? "off" - : normalizeSeverityToString( - linterOptions.reportUnusedDisableDirectives, - ); - } - } - - const reportUnusedInlineConfigs = - linterOptions.reportUnusedInlineConfigs === void 0 - ? "off" - : normalizeSeverityToString( - linterOptions.reportUnusedInlineConfigs, - ); - - let ruleFilter = providedOptions.ruleFilter; - - if (typeof ruleFilter !== "function") { - ruleFilter = () => true; - } - - return { - filename: normalizeFilename(providedOptions.filename || ""), - allowInlineConfig: !ignoreInlineConfig, - warnInlineConfig: - disableInlineConfig && !ignoreInlineConfig - ? `your config${configNameOfNoInlineConfig}` - : null, - reportUnusedDisableDirectives, - reportUnusedInlineConfigs, - disableFixes: Boolean(providedOptions.disableFixes), - stats: providedOptions.stats, - ruleFilter, - }; + + const linterOptions = config.linterOptions || config; + + // .noInlineConfig for eslintrc, .linterOptions.noInlineConfig for flat + const disableInlineConfig = linterOptions.noInlineConfig === true; + const ignoreInlineConfig = providedOptions.allowInlineConfig === false; + const configNameOfNoInlineConfig = config.configNameOfNoInlineConfig + ? ` (${config.configNameOfNoInlineConfig})` + : ""; + + let reportUnusedDisableDirectives = providedOptions.reportUnusedDisableDirectives; + + if (typeof reportUnusedDisableDirectives === "boolean") { + reportUnusedDisableDirectives = reportUnusedDisableDirectives ? "error" : "off"; + } + if (typeof reportUnusedDisableDirectives !== "string") { + if (typeof linterOptions.reportUnusedDisableDirectives === "boolean") { + reportUnusedDisableDirectives = linterOptions.reportUnusedDisableDirectives ? "warn" : "off"; + } else { + reportUnusedDisableDirectives = linterOptions.reportUnusedDisableDirectives === void 0 ? "off" : normalizeSeverityToString(linterOptions.reportUnusedDisableDirectives); + } + } + + return { + filename: normalizeFilename(providedOptions.filename || ""), + allowInlineConfig: !ignoreInlineConfig, + warnInlineConfig: disableInlineConfig && !ignoreInlineConfig + ? `your config${configNameOfNoInlineConfig}` + : null, + reportUnusedDisableDirectives, + disableFixes: Boolean(providedOptions.disableFixes) + }; } /** @@ -858,36 +680,25 @@ function normalizeVerifyOptions(providedOptions, config) { * @returns {ParserOptions} Resulting parser options after merge */ function resolveParserOptions(parser, providedOptions, enabledEnvironments) { - const parserOptionsFromEnv = enabledEnvironments - .filter(env => env.parserOptions) - .reduce( - (parserOptions, env) => merge(parserOptions, env.parserOptions), - {}, - ); - const mergedParserOptions = merge( - parserOptionsFromEnv, - providedOptions || {}, - ); - const isModule = mergedParserOptions.sourceType === "module"; - - if (isModule) { - /* - * can't have global return inside of modules - * TODO: espree validate parserOptions.globalReturn when sourceType is setting to module.(@aladdin-add) - */ - mergedParserOptions.ecmaFeatures = Object.assign( - {}, - mergedParserOptions.ecmaFeatures, - { globalReturn: false }, - ); - } - - mergedParserOptions.ecmaVersion = normalizeEcmaVersion( - parser, - mergedParserOptions.ecmaVersion, - ); - - return mergedParserOptions; + + const parserOptionsFromEnv = enabledEnvironments + .filter(env => env.parserOptions) + .reduce((parserOptions, env) => merge(parserOptions, env.parserOptions), {}); + const mergedParserOptions = merge(parserOptionsFromEnv, providedOptions || {}); + const isModule = mergedParserOptions.sourceType === "module"; + + if (isModule) { + + /* + * can't have global return inside of modules + * TODO: espree validate parserOptions.globalReturn when sourceType is setting to module.(@aladdin-add) + */ + mergedParserOptions.ecmaFeatures = Object.assign({}, mergedParserOptions.ecmaFeatures, { globalReturn: false }); + } + + mergedParserOptions.ecmaVersion = normalizeEcmaVersion(parser, mergedParserOptions.ecmaVersion); + + return mergedParserOptions; } /** @@ -896,22 +707,22 @@ function resolveParserOptions(parser, providedOptions, enabledEnvironments) { * @param {Object} config.globals Global variable definitions. * @param {Parser} config.parser The parser to use. * @param {ParserOptions} config.parserOptions The parserOptions to use. - * @returns {JSLanguageOptions} The languageOptions equivalent. + * @returns {LanguageOptions} The languageOptions equivalent. */ -function createLanguageOptions({ - globals: configuredGlobals, - parser, - parserOptions, -}) { - const { ecmaVersion, sourceType } = parserOptions; - - return { - globals: configuredGlobals, - ecmaVersion: normalizeEcmaVersionForLanguageOptions(ecmaVersion), - sourceType, - parser, - parserOptions, - }; +function createLanguageOptions({ globals: configuredGlobals, parser, parserOptions }) { + + const { + ecmaVersion, + sourceType + } = parserOptions; + + return { + globals: configuredGlobals, + ecmaVersion: normalizeEcmaVersionForLanguageOptions(ecmaVersion), + sourceType, + parser, + parserOptions + }; } /** @@ -921,299 +732,393 @@ function createLanguageOptions({ * @returns {Record} The resolved globals object */ function resolveGlobals(providedGlobals, enabledEnvironments) { - return Object.assign( - Object.create(null), - ...enabledEnvironments - .filter(env => env.globals) - .map(env => env.globals), - providedGlobals, - ); + return Object.assign( + Object.create(null), + ...enabledEnvironments.filter(env => env.globals).map(env => env.globals), + providedGlobals + ); } /** - * Store time measurements in map - * @param {number} time Time measurement - * @param {Object} timeOpts Options relating which time was measured - * @param {WeakMap} slots Linter internal slots map - * @returns {void} + * Strips Unicode BOM from a given text. + * @param {string} text A text to strip. + * @returns {string} The stripped text. */ -function storeTime(time, timeOpts, slots) { - const { type, key } = timeOpts; - - if (!slots.times) { - slots.times = { passes: [{}] }; - } - - const passIndex = slots.fixPasses; - - if (passIndex > slots.times.passes.length - 1) { - slots.times.passes.push({}); - } - - if (key) { - slots.times.passes[passIndex][type] ??= {}; - slots.times.passes[passIndex][type][key] ??= { total: 0 }; - slots.times.passes[passIndex][type][key].total += time; - } else { - slots.times.passes[passIndex][type] ??= { total: 0 }; - slots.times.passes[passIndex][type].total += time; - } +function stripUnicodeBOM(text) { + + /* + * Check Unicode BOM. + * In JavaScript, string data is stored as UTF-16, so BOM is 0xFEFF. + * http://www.ecma-international.org/ecma-262/6.0/#sec-unicode-format-control-characters + */ + if (text.charCodeAt(0) === 0xFEFF) { + return text.slice(1); + } + return text; } /** * Get the options for a rule (not including severity), if any - * @param {RuleConfig} ruleConfig rule configuration - * @param {Object|undefined} defaultOptions rule.meta.defaultOptions + * @param {Array|number} ruleConfig rule configuration * @returns {Array} of rule options, empty Array if none */ -function getRuleOptions(ruleConfig, defaultOptions) { - if (Array.isArray(ruleConfig)) { - return deepMergeArrays(defaultOptions, ruleConfig.slice(1)); - } - return defaultOptions ?? []; +function getRuleOptions(ruleConfig) { + if (Array.isArray(ruleConfig)) { + return ruleConfig.slice(1); + } + return []; + } /** * Analyze scope of the given AST. * @param {ASTNode} ast The `Program` node to analyze. - * @param {JSLanguageOptions} languageOptions The language options. + * @param {LanguageOptions} languageOptions The parser options. * @param {Record} visitorKeys The visitor keys. * @returns {ScopeManager} The analysis result. */ function analyzeScope(ast, languageOptions, visitorKeys) { - const parserOptions = languageOptions.parserOptions; - const ecmaFeatures = parserOptions.ecmaFeatures || {}; - const ecmaVersion = languageOptions.ecmaVersion || DEFAULT_ECMA_VERSION; - - return eslintScope.analyze(ast, { - ignoreEval: true, - nodejsScope: ecmaFeatures.globalReturn, - impliedStrict: ecmaFeatures.impliedStrict, - ecmaVersion: typeof ecmaVersion === "number" ? ecmaVersion : 6, - sourceType: languageOptions.sourceType || "script", - childVisitorKeys: visitorKeys || evk.KEYS, - fallback: Traverser.getKeys, - }); + const parserOptions = languageOptions.parserOptions; + const ecmaFeatures = parserOptions.ecmaFeatures || {}; + const ecmaVersion = languageOptions.ecmaVersion || DEFAULT_ECMA_VERSION; + + return eslintScope.analyze(ast, { + ignoreEval: true, + nodejsScope: ecmaFeatures.globalReturn, + impliedStrict: ecmaFeatures.impliedStrict, + ecmaVersion: typeof ecmaVersion === "number" ? ecmaVersion : 6, + sourceType: languageOptions.sourceType || "script", + childVisitorKeys: visitorKeys || evk.KEYS, + fallback: Traverser.getKeys + }); +} + +/** + * Parses text into an AST. Moved out here because the try-catch prevents + * optimization of functions, so it's best to keep the try-catch as isolated + * as possible + * @param {string} text The text to parse. + * @param {LanguageOptions} languageOptions Options to pass to the parser + * @param {string} filePath The path to the file being parsed. + * @returns {{success: false, error: LintMessage}|{success: true, sourceCode: SourceCode}} + * An object containing the AST and parser services if parsing was successful, or the error if parsing failed + * @private + */ +function parse(text, languageOptions, filePath) { + const textToParse = stripUnicodeBOM(text).replace(astUtils.shebangPattern, (match, captured) => `//${captured}`); + const { ecmaVersion, sourceType, parser } = languageOptions; + const parserOptions = Object.assign( + { ecmaVersion, sourceType }, + languageOptions.parserOptions, + { + loc: true, + range: true, + raw: true, + tokens: true, + comment: true, + eslintVisitorKeys: true, + eslintScopeManager: true, + filePath + } + ); + + /* + * Check for parsing errors first. If there's a parsing error, nothing + * else can happen. However, a parsing error does not throw an error + * from this method - it's just considered a fatal error message, a + * problem that ESLint identified just like any other. + */ + try { + debug("Parsing:", filePath); + const parseResult = (typeof parser.parseForESLint === "function") + ? parser.parseForESLint(textToParse, parserOptions) + : { ast: parser.parse(textToParse, parserOptions) }; + + debug("Parsing successful:", filePath); + const ast = parseResult.ast; + const parserServices = parseResult.services || {}; + const visitorKeys = parseResult.visitorKeys || evk.KEYS; + + debug("Scope analysis:", filePath); + const scopeManager = parseResult.scopeManager || analyzeScope(ast, languageOptions, visitorKeys); + + debug("Scope analysis successful:", filePath); + + return { + success: true, + + /* + * Save all values that `parseForESLint()` returned. + * If a `SourceCode` object is given as the first parameter instead of source code text, + * linter skips the parsing process and reuses the source code object. + * In that case, linter needs all the values that `parseForESLint()` returned. + */ + sourceCode: new SourceCode({ + text, + ast, + parserServices, + scopeManager, + visitorKeys + }) + }; + } catch (ex) { + + // If the message includes a leading line number, strip it: + const message = `Parsing error: ${ex.message.replace(/^line \d+:/iu, "").trim()}`; + + debug("%s\n%s", message, ex.stack); + + return { + success: false, + error: { + ruleId: null, + fatal: true, + severity: 2, + message, + line: ex.lineNumber, + column: ex.column, + nodeType: null + } + }; + } } /** * Runs a rule, and gets its listeners - * @param {RuleDefinition} rule A rule object + * @param {Rule} rule A normalized rule with a `create` method * @param {Context} ruleContext The context that should be passed to the rule - * @throws {TypeError} If `rule` is not an object with a `create` method * @throws {any} Any error during the rule's `create` * @returns {Object} A map of selector listeners provided by the rule */ function createRuleListeners(rule, ruleContext) { - if ( - !rule || - typeof rule !== "object" || - typeof rule.create !== "function" - ) { - throw new TypeError( - `Error while loading rule '${ruleContext.id}': Rule must be an object with a \`create\` method`, - ); - } - - try { - return rule.create(ruleContext); - } catch (ex) { - ex.message = `Error while loading rule '${ruleContext.id}': ${ex.message}`; - throw ex; - } + try { + return rule.create(ruleContext); + } catch (ex) { + ex.message = `Error while loading rule '${ruleContext.id}': ${ex.message}`; + throw ex; + } } +// methods that exist on SourceCode object +const DEPRECATED_SOURCECODE_PASSTHROUGHS = { + getSource: "getText", + getSourceLines: "getLines", + getAllComments: "getAllComments", + getNodeByRangeIndex: "getNodeByRangeIndex", + getComments: "getComments", + getCommentsBefore: "getCommentsBefore", + getCommentsAfter: "getCommentsAfter", + getCommentsInside: "getCommentsInside", + getJSDocComment: "getJSDocComment", + getFirstToken: "getFirstToken", + getFirstTokens: "getFirstTokens", + getLastToken: "getLastToken", + getLastTokens: "getLastTokens", + getTokenAfter: "getTokenAfter", + getTokenBefore: "getTokenBefore", + getTokenByRangeStart: "getTokenByRangeStart", + getTokens: "getTokens", + getTokensAfter: "getTokensAfter", + getTokensBefore: "getTokensBefore", + getTokensBetween: "getTokensBetween" +}; + + +const BASE_TRAVERSAL_CONTEXT = Object.freeze( + Object.keys(DEPRECATED_SOURCECODE_PASSTHROUGHS).reduce( + (contextInfo, methodName) => + Object.assign(contextInfo, { + [methodName](...args) { + return this.sourceCode[DEPRECATED_SOURCECODE_PASSTHROUGHS[methodName]](...args); + } + }), + {} + ) +); + /** * Runs the given rules on the given SourceCode object * @param {SourceCode} sourceCode A SourceCode object for the given text * @param {Object} configuredRules The rules configuration - * @param {function(string): RuleDefinition} ruleMapper A mapper function from rule names to rules + * @param {function(string): Rule} ruleMapper A mapper function from rule names to rules * @param {string | undefined} parserName The name of the parser in the config - * @param {Language} language The language object used for parsing. * @param {LanguageOptions} languageOptions The options for parsing the code. * @param {Object} settings The settings that were enabled in the config * @param {string} filename The reported filename of the code - * @param {boolean} applyDefaultOptions If true, apply rules' meta.defaultOptions in computing their config options. + * @param {boolean} disableFixes If true, it doesn't make `fix` properties. * @param {string | undefined} cwd cwd of the cli * @param {string} physicalFilename The full path of the file on disk without any code block information - * @param {Function} ruleFilter A predicate function to filter which rules should be executed. - * @param {boolean} stats If true, stats are collected appended to the result - * @param {WeakMap} slots InternalSlotsMap of linter - * @param {FileReport} report The report to add problems to - * @returns {FileReport} report The report with added problems - * @throws {Error} If traversal into a node fails. + * @returns {LintMessage[]} An array of reported problems */ -function runRules( - sourceCode, - configuredRules, - ruleMapper, - parserName, - language, - languageOptions, - settings, - filename, - applyDefaultOptions, - cwd, - physicalFilename, - ruleFilter, - stats, - slots, - report, -) { - const visitor = new SourceCodeVisitor(); - - /* - * Create a frozen object with the ruleContext properties and methods that are shared by all rules. - * All rule contexts will inherit from this object. This avoids the performance penalty of copying all the - * properties once for each rule. - */ - const fileContext = new FileContext({ - cwd, - filename, - physicalFilename: physicalFilename || filename, - sourceCode, - parserOptions: { - ...languageOptions.parserOptions, - }, - parserPath: parserName, - languageOptions, - settings, - }); - - const steps = sourceCode.traverse(); - - Object.keys(configuredRules).forEach(ruleId => { - const severity = Config.getRuleNumericSeverity(configuredRules[ruleId]); - - // not load disabled rules - if (severity === 0) { - return; - } - - if (ruleFilter && !ruleFilter({ ruleId, severity })) { - return; - } - - const rule = ruleMapper(ruleId); - - if (!rule) { - report.addError({ ruleId }); - return; - } - - const ruleContext = fileContext.extend({ - id: ruleId, - options: getRuleOptions( - configuredRules[ruleId], - applyDefaultOptions ? rule.meta?.defaultOptions : void 0, - ), - report(...args) { - const problem = report.addRuleMessage( - ruleId, - severity, - ...args, - ); - - if (problem.fix && !(rule.meta && rule.meta.fixable)) { - throw new Error( - 'Fixable rules must set the `meta.fixable` property to "code" or "whitespace".', - ); - } - - if ( - problem.suggestions && - !(rule.meta && rule.meta.hasSuggestions === true) - ) { - if ( - rule.meta && - rule.meta.docs && - typeof rule.meta.docs.suggestion !== "undefined" - ) { - // Encourage migration from the former property name. - throw new Error( - "Rules with suggestions must set the `meta.hasSuggestions` property to `true`. `meta.docs.suggestion` is ignored by ESLint.", - ); - } - throw new Error( - "Rules with suggestions must set the `meta.hasSuggestions` property to `true`.", - ); - } - }, - }); - - const ruleListenersReturn = - timing.enabled || stats - ? timing.time( - ruleId, - createRuleListeners, - stats, - )(rule, ruleContext) - : createRuleListeners(rule, ruleContext); - - const ruleListeners = stats - ? ruleListenersReturn.result - : ruleListenersReturn; - - if (stats) { - storeTime( - ruleListenersReturn.tdiff, - { type: "rules", key: ruleId }, - slots, - ); - } - - /** - * Include `ruleId` in error logs - * @param {Function} ruleListener A rule method that listens for a node. - * @returns {Function} ruleListener wrapped in error handler - */ - function addRuleErrorHandler(ruleListener) { - return function ruleErrorHandler(...listenerArgs) { - try { - const ruleListenerReturn = ruleListener(...listenerArgs); - - const ruleListenerResult = stats - ? ruleListenerReturn.result - : ruleListenerReturn; - - if (stats) { - storeTime( - ruleListenerReturn.tdiff, - { type: "rules", key: ruleId }, - slots, - ); - } - - return ruleListenerResult; - } catch (e) { - e.ruleId = ruleId; - throw e; - } - }; - } - - if (typeof ruleListeners === "undefined" || ruleListeners === null) { - throw new Error( - `The create() function for rule '${ruleId}' did not return an object.`, - ); - } - - // add all the selectors from the rule as listeners - Object.keys(ruleListeners).forEach(selector => { - const ruleListener = - timing.enabled || stats - ? timing.time(ruleId, ruleListeners[selector], stats) - : ruleListeners[selector]; - - visitor.add(selector, addRuleErrorHandler(ruleListener)); - }); - }); - - const traverser = SourceCodeTraverser.getInstance(language); - - traverser.traverseSync(sourceCode, visitor, { steps }); - - return report; +function runRules(sourceCode, configuredRules, ruleMapper, parserName, languageOptions, settings, filename, disableFixes, cwd, physicalFilename) { + const emitter = createEmitter(); + const nodeQueue = []; + let currentNode = sourceCode.ast; + + Traverser.traverse(sourceCode.ast, { + enter(node, parent) { + node.parent = parent; + nodeQueue.push({ isEntering: true, node }); + }, + leave(node) { + nodeQueue.push({ isEntering: false, node }); + }, + visitorKeys: sourceCode.visitorKeys + }); + + /* + * Create a frozen object with the ruleContext properties and methods that are shared by all rules. + * All rule contexts will inherit from this object. This avoids the performance penalty of copying all the + * properties once for each rule. + */ + const sharedTraversalContext = Object.freeze( + Object.assign( + Object.create(BASE_TRAVERSAL_CONTEXT), + { + getAncestors: () => sourceCode.getAncestors(currentNode), + getDeclaredVariables: node => sourceCode.getDeclaredVariables(node), + getCwd: () => cwd, + cwd, + getFilename: () => filename, + filename, + getPhysicalFilename: () => physicalFilename || filename, + physicalFilename: physicalFilename || filename, + getScope: () => sourceCode.getScope(currentNode), + getSourceCode: () => sourceCode, + sourceCode, + markVariableAsUsed: name => sourceCode.markVariableAsUsed(name, currentNode), + parserOptions: { + ...languageOptions.parserOptions + }, + parserPath: parserName, + languageOptions, + parserServices: sourceCode.parserServices, + settings + } + ) + ); + + const lintingProblems = []; + + Object.keys(configuredRules).forEach(ruleId => { + const severity = ConfigOps.getRuleSeverity(configuredRules[ruleId]); + + // not load disabled rules + if (severity === 0) { + return; + } + + const rule = ruleMapper(ruleId); + + if (!rule) { + lintingProblems.push(createLintingProblem({ ruleId })); + return; + } + + const messageIds = rule.meta && rule.meta.messages; + let reportTranslator = null; + const ruleContext = Object.freeze( + Object.assign( + Object.create(sharedTraversalContext), + { + id: ruleId, + options: getRuleOptions(configuredRules[ruleId]), + report(...args) { + + /* + * Create a report translator lazily. + * In a vast majority of cases, any given rule reports zero errors on a given + * piece of code. Creating a translator lazily avoids the performance cost of + * creating a new translator function for each rule that usually doesn't get + * called. + * + * Using lazy report translators improves end-to-end performance by about 3% + * with Node 8.4.0. + */ + if (reportTranslator === null) { + reportTranslator = createReportTranslator({ + ruleId, + severity, + sourceCode, + messageIds, + disableFixes + }); + } + const problem = reportTranslator(...args); + + if (problem.fix && !(rule.meta && rule.meta.fixable)) { + throw new Error("Fixable rules must set the `meta.fixable` property to \"code\" or \"whitespace\"."); + } + if (problem.suggestions && !(rule.meta && rule.meta.hasSuggestions === true)) { + if (rule.meta && rule.meta.docs && typeof rule.meta.docs.suggestion !== "undefined") { + + // Encourage migration from the former property name. + throw new Error("Rules with suggestions must set the `meta.hasSuggestions` property to `true`. `meta.docs.suggestion` is ignored by ESLint."); + } + throw new Error("Rules with suggestions must set the `meta.hasSuggestions` property to `true`."); + } + lintingProblems.push(problem); + } + } + ) + ); + + const ruleListeners = timing.enabled ? timing.time(ruleId, createRuleListeners)(rule, ruleContext) : createRuleListeners(rule, ruleContext); + + /** + * Include `ruleId` in error logs + * @param {Function} ruleListener A rule method that listens for a node. + * @returns {Function} ruleListener wrapped in error handler + */ + function addRuleErrorHandler(ruleListener) { + return function ruleErrorHandler(...listenerArgs) { + try { + return ruleListener(...listenerArgs); + } catch (e) { + e.ruleId = ruleId; + throw e; + } + }; + } + + if (typeof ruleListeners === "undefined" || ruleListeners === null) { + throw new Error(`The create() function for rule '${ruleId}' did not return an object.`); + } + + // add all the selectors from the rule as listeners + Object.keys(ruleListeners).forEach(selector => { + const ruleListener = timing.enabled + ? timing.time(ruleId, ruleListeners[selector]) + : ruleListeners[selector]; + + emitter.on( + selector, + addRuleErrorHandler(ruleListener) + ); + }); + }); + + // only run code path analyzer if the top level node is "Program", skip otherwise + const eventGenerator = nodeQueue[0].node.type === "Program" + ? new CodePathAnalyzer(new NodeEventGenerator(emitter, { visitorKeys: sourceCode.visitorKeys, fallback: Traverser.getKeys })) + : new NodeEventGenerator(emitter, { visitorKeys: sourceCode.visitorKeys, fallback: Traverser.getKeys }); + + nodeQueue.forEach(traversalInfo => { + currentNode = traversalInfo.node; + + try { + if (traversalInfo.isEntering) { + eventGenerator.enterNode(currentNode); + } else { + eventGenerator.leaveNode(currentNode); + } + } catch (err) { + err.currentNode = currentNode; + throw err; + } + }); + + return lintingProblems; } /** @@ -1222,14 +1127,14 @@ function runRules( * @returns {string} The source code text. */ function ensureText(textOrSourceCode) { - if (typeof textOrSourceCode === "object") { - const { hasBOM, text } = textOrSourceCode; - const bom = hasBOM ? "\uFEFF" : ""; + if (typeof textOrSourceCode === "object") { + const { hasBOM, text } = textOrSourceCode; + const bom = hasBOM ? "\uFEFF" : ""; - return bom + text; - } + return bom + text; + } - return String(textOrSourceCode); + return String(textOrSourceCode); } /** @@ -1239,12 +1144,11 @@ function ensureText(textOrSourceCode) { * @returns {Environment|null} The environment. */ function getEnv(slots, envId) { - return ( - (slots.lastConfigArray && - slots.lastConfigArray.pluginEnvironments.get(envId)) || - BuiltInEnvironments.get(envId) || - null - ); + return ( + (slots.lastConfigArray && slots.lastConfigArray.pluginEnvironments.get(envId)) || + BuiltInEnvironments.get(envId) || + null + ); } /** @@ -1254,11 +1158,10 @@ function getEnv(slots, envId) { * @returns {Rule|null} The rule. */ function getRule(slots, ruleId) { - return ( - (slots.lastConfigArray && - slots.lastConfigArray.pluginRules.get(ruleId)) || - slots.ruleMap.get(ruleId) - ); + return ( + (slots.lastConfigArray && slots.lastConfigArray.pluginRules.get(ruleId)) || + slots.ruleMap.get(ruleId) + ); } /** @@ -1267,16 +1170,16 @@ function getRule(slots, ruleId) { * @returns {string | undefined} normalized cwd */ function normalizeCwd(cwd) { - if (cwd) { - return cwd; - } - if (typeof process === "object") { - return process.cwd(); - } - - // It's more explicit to assign the undefined - // eslint-disable-next-line no-undefined -- Consistently returning a value - return undefined; + if (cwd) { + return cwd; + } + if (typeof process === "object") { + return process.cwd(); + } + + // It's more explicit to assign the undefined + // eslint-disable-next-line no-undefined -- Consistently returning a value + return undefined; } /** @@ -1292,15 +1195,14 @@ const internalSlotsMap = new WeakMap(); * @throws {Error} If the linter is in flat config mode. */ function assertEslintrcConfig(linter) { - const { configType } = internalSlotsMap.get(linter); + const { configType } = internalSlotsMap.get(linter); - if (configType === "flat") { - throw new Error( - "This method cannot be used with flat config. Add your entries directly into the config array.", - ); - } + if (configType === "flat") { + throw new Error("This method cannot be used with flat config. Add your entries directly into the config array."); + } } + //------------------------------------------------------------------------------ // Public Interface //------------------------------------------------------------------------------ @@ -1310,1367 +1212,908 @@ function assertEslintrcConfig(linter) { * @name Linter */ class Linter { - /** - * Initialize the Linter. - * @param {Object} [config] the config object - * @param {string} [config.cwd] path to a directory that should be considered as the current working directory, can be undefined. - * @param {Array} [config.flags] the feature flags to enable. - * @param {"flat"|"eslintrc"} [config.configType="flat"] the type of config used. - * @param {WarningService} [config.warningService] The warning service to use. - */ - constructor({ - cwd, - configType = "flat", - flags = [], - warningService = new WarningService(), - } = {}) { - const processedFlags = []; - - flags.forEach(flag => { - if (inactiveFlags.has(flag)) { - const inactiveFlagData = inactiveFlags.get(flag); - const inactivityReason = - getInactivityReasonMessage(inactiveFlagData); - const message = `The flag '${flag}' is inactive: ${inactivityReason}`; - - if (typeof inactiveFlagData.replacedBy === "undefined") { - throw new Error(message); - } - - // if there's a replacement, enable it instead of original - if (typeof inactiveFlagData.replacedBy === "string") { - processedFlags.push(inactiveFlagData.replacedBy); - } - - warningService.emitInactiveFlagWarning(flag, message); - - return; - } - - if (!activeFlags.has(flag)) { - throw new Error(`Unknown flag '${flag}'.`); - } - - processedFlags.push(flag); - }); - - internalSlotsMap.set(this, { - cwd: normalizeCwd(cwd), - flags: processedFlags, - lastConfigArray: null, - lastSourceCode: null, - lastSuppressedMessages: [], - configType, // TODO: Remove after flat config conversion - parserMap: new Map([["espree", espree]]), - ruleMap: new Rules(), - warningService, - }); - - this.version = pkg.version; - } - - /** - * Getter for package version. - * @static - * @returns {string} The version from package.json. - */ - static get version() { - return pkg.version; - } - - /** - * Indicates if the given feature flag is enabled for this instance. - * @param {string} flag The feature flag to check. - * @returns {boolean} `true` if the feature flag is enabled, `false` if not. - */ - hasFlag(flag) { - return internalSlotsMap.get(this).flags.includes(flag); - } - - /** - * Lint using eslintrc and without processors. - * @param {VFile} file The file to lint. - * @param {ConfigData} providedConfig An ESLintConfig instance to configure everything. - * @param {VerifyOptions} [providedOptions] The optional filename of the file being checked. - * @throws {Error} If during rule execution. - * @returns {(LintMessage|SuppressedLintMessage)[]} The results as an array of messages or an empty array if no messages. - */ - #eslintrcVerifyWithoutProcessors(file, providedConfig, providedOptions) { - const slots = internalSlotsMap.get(this); - const config = providedConfig || {}; - const options = normalizeVerifyOptions(providedOptions, config); - - // Resolve parser. - let parserName = DEFAULT_PARSER_NAME; - let parser = espree; - - if (typeof config.parser === "object" && config.parser !== null) { - parserName = config.parser.filePath; - parser = config.parser.definition; - } else if (typeof config.parser === "string") { - if (!slots.parserMap.has(config.parser)) { - return [ - { - ruleId: null, - fatal: true, - severity: 2, - message: `Configured parser '${config.parser}' was not found.`, - line: 0, - column: 0, - nodeType: null, - }, - ]; - } - parserName = config.parser; - parser = slots.parserMap.get(config.parser); - } - - // search and apply "eslint-env *". - const envInFile = - options.allowInlineConfig && !options.warnInlineConfig - ? findEslintEnv(file.body) - : {}; - const resolvedEnvConfig = Object.assign( - { builtin: true }, - config.env, - envInFile, - ); - const enabledEnvs = Object.keys(resolvedEnvConfig) - .filter(envName => resolvedEnvConfig[envName]) - .map(envName => getEnv(slots, envName)) - .filter(env => env); - - const parserOptions = resolveParserOptions( - parser, - config.parserOptions || {}, - enabledEnvs, - ); - const configuredGlobals = resolveGlobals( - config.globals || {}, - enabledEnvs, - ); - const settings = config.settings || {}; - const languageOptions = createLanguageOptions({ - globals: config.globals, - parser, - parserOptions, - }); - - if (!slots.lastSourceCode) { - let t; - - if (options.stats) { - t = startTime(); - } - - const parserService = new ParserService(); - const parseResult = parserService.parseSync(file, { - language: jslang, - languageOptions, - }); - - if (options.stats) { - const time = endTime(t); - const timeOpts = { type: "parse" }; - - storeTime(time, timeOpts, slots); - } - - if (!parseResult.ok) { - return parseResult.errors; - } - - slots.lastSourceCode = parseResult.sourceCode; - } else { - /* - * If the given source code object as the first argument does not have scopeManager, analyze the scope. - * This is for backward compatibility (SourceCode is frozen so it cannot rebind). - */ - if (!slots.lastSourceCode.scopeManager) { - slots.lastSourceCode = new SourceCode({ - text: slots.lastSourceCode.text, - ast: slots.lastSourceCode.ast, - hasBOM: slots.lastSourceCode.hasBOM, - parserServices: slots.lastSourceCode.parserServices, - visitorKeys: slots.lastSourceCode.visitorKeys, - scopeManager: analyzeScope( - slots.lastSourceCode.ast, - languageOptions, - ), - }); - } - } - - const sourceCode = slots.lastSourceCode; - const report = new FileReport({ - ruleMapper: ruleId => getRule(slots, ruleId), - language: jslang, - sourceCode, - disableFixes: options.disableFixes, - }); - - const commentDirectives = options.allowInlineConfig - ? getDirectiveComments( - sourceCode, - ruleId => getRule(slots, ruleId), - options.warnInlineConfig, - config, - report, - ) - : { - configuredRules: {}, - enabledGlobals: {}, - exportedVariables: {}, - disableDirectives: [], - }; - - addDeclaredGlobals( - sourceCode.scopeManager.scopes[0], - configuredGlobals, - { - exportedVariables: commentDirectives.exportedVariables, - enabledGlobals: commentDirectives.enabledGlobals, - }, - ); - - const configuredRules = Object.assign( - {}, - config.rules, - commentDirectives.configuredRules, - ); - - try { - runRules( - sourceCode, - configuredRules, - ruleId => getRule(slots, ruleId), - parserName, - jslang, - languageOptions, - settings, - options.filename, - true, - slots.cwd, - providedOptions.physicalFilename, - null, - options.stats, - slots, - report, - ); - } catch (err) { - err.message += `\nOccurred while linting ${options.filename}`; - debug("An error occurred while traversing"); - debug("Filename:", options.filename); - if (err.currentNode) { - const { line } = sourceCode.getLoc(err.currentNode).start; - - debug("Line:", line); - err.message += `:${line}`; - } - debug("Parser Options:", parserOptions); - debug("Parser Path:", parserName); - debug("Settings:", settings); - - if (err.ruleId) { - err.message += `\nRule: "${err.ruleId}"`; - } - - throw err; - } - - return applyDisableDirectives({ - language: jslang, - sourceCode, - directives: commentDirectives.disableDirectives, - disableFixes: options.disableFixes, - problems: report.messages.sort( - (problemA, problemB) => - problemA.line - problemB.line || - problemA.column - problemB.column, - ), - reportUnusedDisableDirectives: - options.reportUnusedDisableDirectives, - }); - } - - /** - * Same as linter.verify, except without support for processors. - * @param {string|SourceCode} textOrSourceCode The text to parse or a SourceCode object. - * @param {ConfigData} providedConfig An ESLintConfig instance to configure everything. - * @param {VerifyOptions} [providedOptions] The optional filename of the file being checked. - * @throws {Error} If during rule execution. - * @returns {(LintMessage|SuppressedLintMessage)[]} The results as an array of messages or an empty array if no messages. - */ - _verifyWithoutProcessors( - textOrSourceCode, - providedConfig, - providedOptions, - ) { - const slots = internalSlotsMap.get(this); - const filename = normalizeFilename( - providedOptions.filename || "", - ); - let text; - - // evaluate arguments - if (typeof textOrSourceCode === "string") { - slots.lastSourceCode = null; - text = textOrSourceCode; - } else { - slots.lastSourceCode = textOrSourceCode; - text = textOrSourceCode.text; - } - - const file = new VFile(filename, text, { - physicalPath: providedOptions.physicalFilename, - }); - - return this.#eslintrcVerifyWithoutProcessors( - file, - providedConfig, - providedOptions, - ); - } - - /** - * Verifies the text against the rules specified by the second argument. - * @param {string|SourceCode} textOrSourceCode The text to parse or a SourceCode object. - * @param {ConfigData|ConfigArray} config An ESLintConfig instance to configure everything. - * @param {(string|(VerifyOptions&ProcessorOptions))} [filenameOrOptions] The optional filename of the file being checked. - * If this is not set, the filename will default to '' in the rule context. If - * an object, then it has "filename", "allowInlineConfig", and some properties. - * @returns {LintMessage[]} The results as an array of messages or an empty array if no messages. - */ - verify(textOrSourceCode, config, filenameOrOptions) { - debug("Verify"); - - const { configType, cwd } = internalSlotsMap.get(this); - - const options = - typeof filenameOrOptions === "string" - ? { filename: filenameOrOptions } - : filenameOrOptions || {}; - - const configToUse = config ?? {}; - - if (configType !== "eslintrc") { - /* - * Because of how Webpack packages up the files, we can't - * compare directly to `FlatConfigArray` using `instanceof` - * because it's not the same `FlatConfigArray` as in the tests. - * So, we work around it by assuming an array is, in fact, a - * `FlatConfigArray` if it has a `getConfig()` method. - */ - let configArray = configToUse; - - if ( - !Array.isArray(configToUse) || - typeof configToUse.getConfig !== "function" - ) { - configArray = new FlatConfigArray(configToUse, { - basePath: cwd, - }); - configArray.normalizeSync(); - } - - return this._distinguishSuppressedMessages( - this._verifyWithFlatConfigArray( - textOrSourceCode, - configArray, - options, - true, - ), - ); - } - - if (typeof configToUse.extractConfig === "function") { - return this._distinguishSuppressedMessages( - this._verifyWithConfigArray( - textOrSourceCode, - configToUse, - options, - ), - ); - } - - /* - * If we get to here, it means `config` is just an object rather - * than a config array so we can go right into linting. - */ - - /* - * `Linter` doesn't support `overrides` property in configuration. - * So we cannot apply multiple processors. - */ - if (options.preprocess || options.postprocess) { - return this._distinguishSuppressedMessages( - this._verifyWithProcessor( - textOrSourceCode, - configToUse, - options, - ), - ); - } - return this._distinguishSuppressedMessages( - this._verifyWithoutProcessors( - textOrSourceCode, - configToUse, - options, - ), - ); - } - - /** - * Verify with a processor. - * @param {string|SourceCode} textOrSourceCode The source code. - * @param {Config} config The config array. - * @param {VerifyOptions&ProcessorOptions} options The options. - * @param {FlatConfigArray} [configForRecursive] The `ConfigArray` object to apply multiple processors recursively. - * @returns {(LintMessage|SuppressedLintMessage)[]} The found problems. - */ - _verifyWithFlatConfigArrayAndProcessor( - textOrSourceCode, - config, - options, - configForRecursive, - ) { - const slots = internalSlotsMap.get(this); - const filename = options.filename || ""; - const filenameToExpose = normalizeFilename(filename); - const physicalFilename = options.physicalFilename || filenameToExpose; - const text = ensureText(textOrSourceCode); - const file = new VFile(filenameToExpose, text, { - physicalPath: physicalFilename, - }); - - const preprocess = options.preprocess || (rawText => [rawText]); - const postprocess = - options.postprocess || (messagesList => messagesList.flat()); - - const processorService = new ProcessorService(); - const preprocessResult = processorService.preprocessSync(file, { - processor: { - preprocess, - postprocess, - }, - }); - - if (!preprocessResult.ok) { - return preprocessResult.errors; - } - - const filterCodeBlock = - options.filterCodeBlock || - (blockFilename => blockFilename.endsWith(".js")); - const originalExtname = path.extname(filename); - const { files } = preprocessResult; - - const messageLists = files.map(block => { - debug("A code block was found: %o", block.path || "(unnamed)"); - - // Keep the legacy behavior. - if (typeof block === "string") { - return this._verifyWithFlatConfigArrayAndWithoutProcessors( - block, - config, - options, - ); - } - - // Skip this block if filtered. - if (!filterCodeBlock(block.path, block.body)) { - debug("This code block was skipped."); - return []; - } - - // Resolve configuration again if the file content or extension was changed. - if ( - configForRecursive && - (text !== block.rawBody || - path.extname(block.path) !== originalExtname) - ) { - debug( - "Resolving configuration again because the file content or extension was changed.", - ); - return this._verifyWithFlatConfigArray( - block.rawBody, - configForRecursive, - { - ...options, - filename: block.path, - physicalFilename: block.physicalPath, - }, - ); - } - - slots.lastSourceCode = null; - - // Does lint. - return this.#flatVerifyWithoutProcessors(block, config, { - ...options, - filename: block.path, - physicalFilename: block.physicalPath, - }); - }); - - return processorService.postprocessSync(file, messageLists, { - processor: { - preprocess, - postprocess, - }, - }); - } - - /** - * Verify using flat config and without any processors. - * @param {VFile} file The file to lint. - * @param {Config} providedConfig An ESLintConfig instance to configure everything. - * @param {VerifyOptions} [providedOptions] The optional filename of the file being checked. - * @throws {Error} If during rule execution. - * @returns {(LintMessage|SuppressedLintMessage)[]} The results as an array of messages or an empty array if no messages. - */ - #flatVerifyWithoutProcessors(file, providedConfig, providedOptions) { - const slots = internalSlotsMap.get(this); - const config = providedConfig || {}; - const { settings = {}, languageOptions } = config; - const options = normalizeVerifyOptions(providedOptions, config); - - if (!slots.lastSourceCode) { - let t; - - if (options.stats) { - t = startTime(); - } - - const parserService = new ParserService(); - const parseResult = parserService.parseSync(file, config); - - if (options.stats) { - const time = endTime(t); - - storeTime(time, { type: "parse" }, slots); - } - - if (!parseResult.ok) { - return parseResult.errors; - } - - slots.lastSourceCode = parseResult.sourceCode; - } else { - /* - * If the given source code object as the first argument does not have scopeManager, analyze the scope. - * This is for backward compatibility (SourceCode is frozen so it cannot rebind). - * - * We check explicitly for `null` to ensure that this is a JS-flavored language. - * For non-JS languages we don't want to do this. - * - * TODO: Remove this check when we stop exporting the `SourceCode` object. - */ - if (slots.lastSourceCode.scopeManager === null) { - slots.lastSourceCode = new SourceCode({ - text: slots.lastSourceCode.text, - ast: slots.lastSourceCode.ast, - hasBOM: slots.lastSourceCode.hasBOM, - parserServices: slots.lastSourceCode.parserServices, - visitorKeys: slots.lastSourceCode.visitorKeys, - scopeManager: analyzeScope( - slots.lastSourceCode.ast, - languageOptions, - ), - }); - } - } - - const sourceCode = slots.lastSourceCode; - const report = new FileReport({ - ruleMapper: ruleId => config.getRuleDefinition(ruleId), - language: config.language, - sourceCode, - disableFixes: options.disableFixes, - }); - - /* - * Make adjustments based on the language options. For JavaScript, - * this is primarily about adding variables into the global scope - * to account for ecmaVersion and configured globals. - */ - sourceCode.applyLanguageOptions?.(languageOptions); - - const mergedInlineConfig = { - rules: {}, - }; - - /* - * Inline config can be either enabled or disabled. If disabled, it's possible - * to detect the inline config and emit a warning (though this is not required). - * So we first check to see if inline config is allowed at all, and if so, we - * need to check if it's a warning or not. - */ - if (options.allowInlineConfig) { - // if inline config should warn then add the warnings - if (options.warnInlineConfig) { - if (sourceCode.getInlineConfigNodes) { - sourceCode.getInlineConfigNodes().forEach(node => { - const loc = sourceCode.getLoc(node); - const range = sourceCode.getRange(node); - - report.addWarning({ - message: `'${sourceCode.text.slice(range[0], range[1])}' has no effect because you have 'noInlineConfig' setting in ${options.warnInlineConfig}.`, - loc, - }); - }); - } - } else { - if (config.language === jslang) { - for (const comment of sourceCode.getInlineConfigNodes()) { - const { label } = commentParser.parseDirective( - comment.value, - ); - if (label === "eslint-env") { - slots.warningService.emitESLintEnvWarning( - options.filename, - comment.loc.start.line, - ); - } - } - } - - const inlineConfigResult = sourceCode.applyInlineConfig?.(); - - if (inlineConfigResult) { - inlineConfigResult.problems.forEach(problem => { - report.addFatal(problem); - }); - - for (const { - config: inlineConfig, - loc, - } of inlineConfigResult.configs) { - Object.keys(inlineConfig.rules).forEach(ruleId => { - const rule = config.getRuleDefinition(ruleId); - const ruleValue = inlineConfig.rules[ruleId]; - - if (!rule) { - report.addError({ - ruleId, - loc, - }); - return; - } - - if ( - Object.hasOwn(mergedInlineConfig.rules, ruleId) - ) { - report.addError({ - message: `Rule "${ruleId}" is already configured by another configuration comment in the preceding code. This configuration is ignored.`, - loc, - }); - return; - } - - try { - const ruleOptionsInline = asArray(ruleValue); - let ruleOptions = ruleOptionsInline; - - assertIsRuleSeverity(ruleId, ruleOptions[0]); - - /* - * If the rule was already configured, inline rule configuration that - * only has severity should retain options from the config and just override the severity. - * - * Example: - * - * { - * rules: { - * curly: ["error", "multi"] - * } - * } - * - * /* eslint curly: ["warn"] * / - * - * Results in: - * - * curly: ["warn", "multi"] - */ - - let shouldValidateOptions = true; - - if ( - /* - * If inline config for the rule has only severity - */ - ruleOptions.length === 1 && - /* - * And the rule was already configured - */ - config.rules && - Object.hasOwn(config.rules, ruleId) - ) { - /* - * Then use severity from the inline config and options from the provided config - */ - ruleOptions = [ - ruleOptions[0], // severity from the inline config - ...config.rules[ruleId].slice(1), // options from the provided config - ]; - - // if the rule was enabled, the options have already been validated - if (config.rules[ruleId][0] > 0) { - shouldValidateOptions = false; - } - } else { - /** - * Since we know the user provided options, apply defaults on top of them - */ - const slicedOptions = ruleOptions.slice(1); - const mergedOptions = deepMergeArrays( - rule.meta?.defaultOptions, - slicedOptions, - ); - - if (mergedOptions.length) { - ruleOptions = [ - ruleOptions[0], - ...mergedOptions, - ]; - } - } - - if ( - options.reportUnusedInlineConfigs !== "off" - ) { - addProblemIfSameSeverityAndOptions( - config, - loc, - report, - ruleId, - ruleOptions, - ruleOptionsInline, - options.reportUnusedInlineConfigs, - ); - } - - if (shouldValidateOptions) { - config.validateRulesConfig({ - [ruleId]: ruleOptions, - }); - } - - mergedInlineConfig.rules[ruleId] = ruleOptions; - } catch (err) { - /* - * If the rule has invalid `meta.schema`, throw the error because - * this is not an invalid inline configuration but an invalid rule. - */ - if ( - err.code === - "ESLINT_INVALID_RULE_OPTIONS_SCHEMA" - ) { - throw err; - } - - let baseMessage = err.message - .slice( - err.message.startsWith('Key "rules":') - ? err.message.indexOf(":", 12) + 1 - : err.message.indexOf(":") + 1, - ) - .trim(); - - if (err.messageTemplate) { - baseMessage += ` You passed "${ruleValue}".`; - } - - report.addError({ - ruleId, - message: `Inline configuration for rule "${ruleId}" is invalid:\n\t${baseMessage}\n`, - loc, - }); - } - }); - } - } - } - } - - const commentDirectives = - options.allowInlineConfig && !options.warnInlineConfig - ? getDirectiveCommentsForFlatConfig( - sourceCode, - ruleId => config.getRuleDefinition(ruleId), - config.language, - report, - ) - : []; - - const configuredRules = Object.assign( - {}, - config.rules, - mergedInlineConfig.rules, - ); - - sourceCode.finalize?.(); - - try { - runRules( - sourceCode, - configuredRules, - ruleId => config.getRuleDefinition(ruleId), - void 0, - config.language, - languageOptions, - settings, - options.filename, - false, - slots.cwd, - providedOptions.physicalFilename, - options.ruleFilter, - options.stats, - slots, - report, - ); - } catch (err) { - err.message += `\nOccurred while linting ${options.filename}`; - debug("An error occurred while traversing"); - debug("Filename:", options.filename); - if (err.currentNode) { - const { line } = sourceCode.getLoc(err.currentNode).start; - - debug("Line:", line); - err.message += `:${line}`; - } - debug("Parser Options:", languageOptions.parserOptions); - - // debug("Parser Path:", parserName); - debug("Settings:", settings); - - if (err.ruleId) { - err.message += `\nRule: "${err.ruleId}"`; - } - - throw err; - } - - return applyDisableDirectives({ - language: config.language, - sourceCode, - directives: commentDirectives, - disableFixes: options.disableFixes, - problems: report.messages.sort( - (problemA, problemB) => - problemA.line - problemB.line || - problemA.column - problemB.column, - ), - reportUnusedDisableDirectives: - options.reportUnusedDisableDirectives, - ruleFilter: options.ruleFilter, - configuredRules, - }); - } - - /** - * Same as linter.verify, except without support for processors. - * @param {string|SourceCode} textOrSourceCode The text to parse or a SourceCode object. - * @param {Config} providedConfig An ESLintConfig instance to configure everything. - * @param {VerifyOptions} [providedOptions] The optional filename of the file being checked. - * @throws {Error} If during rule execution. - * @returns {(LintMessage|SuppressedLintMessage)[]} The results as an array of messages or an empty array if no messages. - */ - _verifyWithFlatConfigArrayAndWithoutProcessors( - textOrSourceCode, - providedConfig, - providedOptions, - ) { - const slots = internalSlotsMap.get(this); - const filename = normalizeFilename( - providedOptions.filename || "", - ); - let text; - - // evaluate arguments - if (typeof textOrSourceCode === "string") { - slots.lastSourceCode = null; - text = textOrSourceCode; - } else { - slots.lastSourceCode = textOrSourceCode; - text = textOrSourceCode.text; - } - - const file = new VFile(filename, text, { - physicalPath: providedOptions.physicalFilename, - }); - - return this.#flatVerifyWithoutProcessors( - file, - providedConfig, - providedOptions, - ); - } - - /** - * Verify a given code with `ConfigArray`. - * @param {string|SourceCode} textOrSourceCode The source code. - * @param {ConfigArray} configArray The config array. - * @param {VerifyOptions&ProcessorOptions} options The options. - * @returns {(LintMessage|SuppressedLintMessage)[]} The found problems. - */ - _verifyWithConfigArray(textOrSourceCode, configArray, options) { - debug("With ConfigArray: %s", options.filename); - - // Store the config array in order to get plugin envs and rules later. - internalSlotsMap.get(this).lastConfigArray = configArray; - - // Extract the final config for this file. - const config = configArray.extractConfig(options.filename); - const processor = - config.processor && - configArray.pluginProcessors.get(config.processor); - - // Verify. - if (processor) { - debug("Apply the processor: %o", config.processor); - const { preprocess, postprocess, supportsAutofix } = processor; - const disableFixes = options.disableFixes || !supportsAutofix; - - return this._verifyWithProcessor( - textOrSourceCode, - config, - { ...options, disableFixes, postprocess, preprocess }, - configArray, - ); - } - return this._verifyWithoutProcessors(textOrSourceCode, config, options); - } - - /** - * Verify a given code with a flat config. - * @param {string|SourceCode} textOrSourceCode The source code. - * @param {FlatConfigArray} configArray The config array. - * @param {VerifyOptions&ProcessorOptions} options The options. - * @param {boolean} [firstCall=false] Indicates if this is being called directly - * from verify(). (TODO: Remove once eslintrc is removed.) - * @returns {(LintMessage|SuppressedLintMessage)[]} The found problems. - */ - _verifyWithFlatConfigArray( - textOrSourceCode, - configArray, - options, - firstCall = false, - ) { - debug("With flat config: %s", options.filename); - - // we need a filename to match configs against - const filename = options.filename || "__placeholder__.js"; - - // Store the config array in order to get plugin envs and rules later. - internalSlotsMap.get(this).lastConfigArray = configArray; - const config = configArray.getConfig(filename); - - if (!config) { - return [ - { - ruleId: null, - severity: 1, - message: `No matching configuration found for ${filename}.`, - line: 0, - column: 0, - nodeType: null, - }, - ]; - } - - // Verify. - if (config.processor) { - debug("Apply the processor: %o", config.processor); - const { preprocess, postprocess, supportsAutofix } = - config.processor; - const disableFixes = options.disableFixes || !supportsAutofix; - - return this._verifyWithFlatConfigArrayAndProcessor( - textOrSourceCode, - config, - { ...options, filename, disableFixes, postprocess, preprocess }, - configArray, - ); - } - - // check for options-based processing - if (firstCall && (options.preprocess || options.postprocess)) { - return this._verifyWithFlatConfigArrayAndProcessor( - textOrSourceCode, - config, - options, - ); - } - - return this._verifyWithFlatConfigArrayAndWithoutProcessors( - textOrSourceCode, - config, - options, - ); - } - - /** - * Verify with a processor. - * @param {string|SourceCode} textOrSourceCode The source code. - * @param {ConfigData|ExtractedConfig} config The config array. - * @param {VerifyOptions&ProcessorOptions} options The options. - * @param {ConfigArray} [configForRecursive] The `ConfigArray` object to apply multiple processors recursively. - * @returns {(LintMessage|SuppressedLintMessage)[]} The found problems. - */ - _verifyWithProcessor( - textOrSourceCode, - config, - options, - configForRecursive, - ) { - const slots = internalSlotsMap.get(this); - const filename = options.filename || ""; - const filenameToExpose = normalizeFilename(filename); - const physicalFilename = options.physicalFilename || filenameToExpose; - const text = ensureText(textOrSourceCode); - const file = new VFile(filenameToExpose, text, { - physicalPath: physicalFilename, - }); - - const preprocess = options.preprocess || (rawText => [rawText]); - const postprocess = - options.postprocess || (messagesList => messagesList.flat()); - - const processorService = new ProcessorService(); - const preprocessResult = processorService.preprocessSync(file, { - processor: { - preprocess, - postprocess, - }, - }); - - if (!preprocessResult.ok) { - return preprocessResult.errors; - } - - const filterCodeBlock = - options.filterCodeBlock || - (blockFilePath => blockFilePath.endsWith(".js")); - const originalExtname = path.extname(filename); - - const { files } = preprocessResult; - - const messageLists = files.map(block => { - debug("A code block was found: %o", block.path ?? "(unnamed)"); - - // Keep the legacy behavior. - if (typeof block === "string") { - return this._verifyWithoutProcessors(block, config, options); - } - - // Skip this block if filtered. - if (!filterCodeBlock(block.path, block.body)) { - debug("This code block was skipped."); - return []; - } - - // Resolve configuration again if the file content or extension was changed. - if ( - configForRecursive && - (text !== block.rawBody || - path.extname(block.path) !== originalExtname) - ) { - debug( - "Resolving configuration again because the file content or extension was changed.", - ); - return this._verifyWithConfigArray( - block.rawBody, - configForRecursive, - { - ...options, - filename: block.path, - physicalFilename: block.physicalPath, - }, - ); - } - - slots.lastSourceCode = null; - - // Does lint. - return this.#eslintrcVerifyWithoutProcessors(block, config, { - ...options, - filename: block.path, - physicalFilename: block.physicalPath, - }); - }); - - return processorService.postprocessSync(file, messageLists, { - processor: { - preprocess, - postprocess, - }, - }); - } - - /** - * Given a list of reported problems, distinguish problems between normal messages and suppressed messages. - * The normal messages will be returned and the suppressed messages will be stored as lastSuppressedMessages. - * @param {Array} problems A list of reported problems. - * @returns {LintMessage[]} A list of LintMessage. - */ - _distinguishSuppressedMessages(problems) { - const messages = []; - const suppressedMessages = []; - const slots = internalSlotsMap.get(this); - - for (const problem of problems) { - if (problem.suppressions) { - suppressedMessages.push(problem); - } else { - messages.push(problem); - } - } - - slots.lastSuppressedMessages = suppressedMessages; - - return messages; - } - - /** - * Gets the SourceCode object representing the parsed source. - * @returns {SourceCode} The SourceCode object. - */ - getSourceCode() { - return internalSlotsMap.get(this).lastSourceCode; - } - - /** - * Gets the times spent on (parsing, fixing, linting) a file. - * @returns {{ passes: TimePass[]; }} The times. - */ - getTimes() { - return internalSlotsMap.get(this).times ?? { passes: [] }; - } - - /** - * Gets the number of autofix passes that were made in the last run. - * @returns {number} The number of autofix passes. - */ - getFixPassCount() { - return internalSlotsMap.get(this).fixPasses ?? 0; - } - - /** - * Gets the list of SuppressedLintMessage produced in the last running. - * @returns {SuppressedLintMessage[]} The list of SuppressedLintMessage - */ - getSuppressedMessages() { - return internalSlotsMap.get(this).lastSuppressedMessages; - } - - /** - * Defines a new linting rule. - * @param {string} ruleId A unique rule identifier - * @param {Rule} rule A rule object - * @returns {void} - */ - defineRule(ruleId, rule) { - assertEslintrcConfig(this); - internalSlotsMap.get(this).ruleMap.define(ruleId, rule); - } - - /** - * Defines many new linting rules. - * @param {Record} rulesToDefine map from unique rule identifier to rule - * @returns {void} - */ - defineRules(rulesToDefine) { - assertEslintrcConfig(this); - Object.getOwnPropertyNames(rulesToDefine).forEach(ruleId => { - this.defineRule(ruleId, rulesToDefine[ruleId]); - }); - } - - /** - * Gets an object with all loaded rules. - * @returns {Map} All loaded rules - */ - getRules() { - assertEslintrcConfig(this); - const { lastConfigArray, ruleMap } = internalSlotsMap.get(this); - - return new Map( - (function* () { - yield* ruleMap; - - if (lastConfigArray) { - yield* lastConfigArray.pluginRules; - } - })(), - ); - } - - /** - * Define a new parser module - * @param {string} parserId Name of the parser - * @param {Parser} parserModule The parser object - * @returns {void} - */ - defineParser(parserId, parserModule) { - assertEslintrcConfig(this); - internalSlotsMap.get(this).parserMap.set(parserId, parserModule); - } - - /** - * Performs multiple autofix passes over the text until as many fixes as possible - * have been applied. - * @param {string} text The source text to apply fixes to. - * @param {ConfigData|ConfigArray|FlatConfigArray} config The ESLint config object to use. - * @param {VerifyOptions&ProcessorOptions&FixOptions} options The ESLint options object to use. - * @returns {{fixed:boolean,messages:LintMessage[],output:string}} The result of the fix operation as returned from the - * SourceCodeFixer. - */ - verifyAndFix(text, config, options) { - let messages, - fixedResult, - fixed = false, - passNumber = 0, - currentText = text, - secondPreviousText, - previousText; - const debugTextDescription = - (options && options.filename) || `${text.slice(0, 10)}...`; - const shouldFix = - options && typeof options.fix !== "undefined" ? options.fix : true; - const stats = options?.stats; - - const slots = internalSlotsMap.get(this); - - // Remove lint times from the last run. - if (stats) { - delete slots.times; - slots.fixPasses = 0; - } - - /** - * This loop continues until one of the following is true: - * - * 1. No more fixes have been applied. - * 2. Ten passes have been made. - * - * That means anytime a fix is successfully applied, there will be another pass. - * Essentially, guaranteeing a minimum of two passes. - */ - do { - passNumber++; - let tTotal; - - if (stats) { - tTotal = startTime(); - } - - debug( - `Linting code for ${debugTextDescription} (pass ${passNumber})`, - ); - messages = this.verify(currentText, config, options); - - debug( - `Generating fixed text for ${debugTextDescription} (pass ${passNumber})`, - ); - let t; - - if (stats) { - t = startTime(); - } - - fixedResult = SourceCodeFixer.applyFixes( - currentText, - messages, - shouldFix, - ); - - if (stats) { - if (fixedResult.fixed) { - const time = endTime(t); - - storeTime(time, { type: "fix" }, slots); - slots.fixPasses++; - } else { - storeTime(0, { type: "fix" }, slots); - } - } - - /* - * stop if there are any syntax errors. - * 'fixedResult.output' is a empty string. - */ - if (messages.length === 1 && messages[0].fatal) { - break; - } - - // keep track if any fixes were ever applied - important for return value - fixed = fixed || fixedResult.fixed; - - // update to use the fixed output instead of the original text - secondPreviousText = previousText; - previousText = currentText; - currentText = fixedResult.output; - - if (stats) { - tTotal = endTime(tTotal); - const passIndex = slots.times.passes.length - 1; - - slots.times.passes[passIndex].total = tTotal; - } - - // Stop if we've made a circular fix - if ( - passNumber > 1 && - currentText.length === secondPreviousText.length && - currentText === secondPreviousText - ) { - debug( - `Circular fixes detected after pass ${passNumber}. Exiting fix loop.`, - ); - slots.warningService.emitCircularFixesWarning( - options?.filename ?? "text", - ); - break; - } - } while (fixedResult.fixed && passNumber < MAX_AUTOFIX_PASSES); - - /* - * If the last result had fixes, we need to lint again to be sure we have - * the most up-to-date information. - */ - if (fixedResult.fixed) { - let tTotal; - - if (stats) { - tTotal = startTime(); - } - - fixedResult.messages = this.verify(currentText, config, options); - - if (stats) { - storeTime(0, { type: "fix" }, slots); - slots.times.passes.at(-1).total = endTime(tTotal); - } - } - - // ensure the last result properly reflects if fixes were done - fixedResult.fixed = fixed; - fixedResult.output = currentText; - - return fixedResult; - } + + /** + * Initialize the Linter. + * @param {Object} [config] the config object + * @param {string} [config.cwd] path to a directory that should be considered as the current working directory, can be undefined. + * @param {"flat"|"eslintrc"} [config.configType="eslintrc"] the type of config used. + */ + constructor({ cwd, configType } = {}) { + internalSlotsMap.set(this, { + cwd: normalizeCwd(cwd), + lastConfigArray: null, + lastSourceCode: null, + lastSuppressedMessages: [], + configType, // TODO: Remove after flat config conversion + parserMap: new Map([["espree", espree]]), + ruleMap: new Rules() + }); + + this.version = pkg.version; + } + + /** + * Getter for package version. + * @static + * @returns {string} The version from package.json. + */ + static get version() { + return pkg.version; + } + + /** + * Same as linter.verify, except without support for processors. + * @param {string|SourceCode} textOrSourceCode The text to parse or a SourceCode object. + * @param {ConfigData} providedConfig An ESLintConfig instance to configure everything. + * @param {VerifyOptions} [providedOptions] The optional filename of the file being checked. + * @throws {Error} If during rule execution. + * @returns {(LintMessage|SuppressedLintMessage)[]} The results as an array of messages or an empty array if no messages. + */ + _verifyWithoutProcessors(textOrSourceCode, providedConfig, providedOptions) { + const slots = internalSlotsMap.get(this); + const config = providedConfig || {}; + const options = normalizeVerifyOptions(providedOptions, config); + let text; + + // evaluate arguments + if (typeof textOrSourceCode === "string") { + slots.lastSourceCode = null; + text = textOrSourceCode; + } else { + slots.lastSourceCode = textOrSourceCode; + text = textOrSourceCode.text; + } + + // Resolve parser. + let parserName = DEFAULT_PARSER_NAME; + let parser = espree; + + if (typeof config.parser === "object" && config.parser !== null) { + parserName = config.parser.filePath; + parser = config.parser.definition; + } else if (typeof config.parser === "string") { + if (!slots.parserMap.has(config.parser)) { + return [{ + ruleId: null, + fatal: true, + severity: 2, + message: `Configured parser '${config.parser}' was not found.`, + line: 0, + column: 0, + nodeType: null + }]; + } + parserName = config.parser; + parser = slots.parserMap.get(config.parser); + } + + // search and apply "eslint-env *". + const envInFile = options.allowInlineConfig && !options.warnInlineConfig + ? findEslintEnv(text) + : {}; + const resolvedEnvConfig = Object.assign({ builtin: true }, config.env, envInFile); + const enabledEnvs = Object.keys(resolvedEnvConfig) + .filter(envName => resolvedEnvConfig[envName]) + .map(envName => getEnv(slots, envName)) + .filter(env => env); + + const parserOptions = resolveParserOptions(parser, config.parserOptions || {}, enabledEnvs); + const configuredGlobals = resolveGlobals(config.globals || {}, enabledEnvs); + const settings = config.settings || {}; + const languageOptions = createLanguageOptions({ + globals: config.globals, + parser, + parserOptions + }); + + if (!slots.lastSourceCode) { + const parseResult = parse( + text, + languageOptions, + options.filename + ); + + if (!parseResult.success) { + return [parseResult.error]; + } + + slots.lastSourceCode = parseResult.sourceCode; + } else { + + /* + * If the given source code object as the first argument does not have scopeManager, analyze the scope. + * This is for backward compatibility (SourceCode is frozen so it cannot rebind). + */ + if (!slots.lastSourceCode.scopeManager) { + slots.lastSourceCode = new SourceCode({ + text: slots.lastSourceCode.text, + ast: slots.lastSourceCode.ast, + parserServices: slots.lastSourceCode.parserServices, + visitorKeys: slots.lastSourceCode.visitorKeys, + scopeManager: analyzeScope(slots.lastSourceCode.ast, languageOptions) + }); + } + } + + const sourceCode = slots.lastSourceCode; + const commentDirectives = options.allowInlineConfig + ? getDirectiveComments(sourceCode, ruleId => getRule(slots, ruleId), options.warnInlineConfig) + : { configuredRules: {}, enabledGlobals: {}, exportedVariables: {}, problems: [], disableDirectives: [] }; + + // augment global scope with declared global variables + addDeclaredGlobals( + sourceCode.scopeManager.scopes[0], + configuredGlobals, + { exportedVariables: commentDirectives.exportedVariables, enabledGlobals: commentDirectives.enabledGlobals } + ); + + const configuredRules = Object.assign({}, config.rules, commentDirectives.configuredRules); + let lintingProblems; + + try { + lintingProblems = runRules( + sourceCode, + configuredRules, + ruleId => getRule(slots, ruleId), + parserName, + languageOptions, + settings, + options.filename, + options.disableFixes, + slots.cwd, + providedOptions.physicalFilename + ); + } catch (err) { + err.message += `\nOccurred while linting ${options.filename}`; + debug("An error occurred while traversing"); + debug("Filename:", options.filename); + if (err.currentNode) { + const { line } = err.currentNode.loc.start; + + debug("Line:", line); + err.message += `:${line}`; + } + debug("Parser Options:", parserOptions); + debug("Parser Path:", parserName); + debug("Settings:", settings); + + if (err.ruleId) { + err.message += `\nRule: "${err.ruleId}"`; + } + + throw err; + } + + return applyDisableDirectives({ + directives: commentDirectives.disableDirectives, + disableFixes: options.disableFixes, + problems: lintingProblems + .concat(commentDirectives.problems) + .sort((problemA, problemB) => problemA.line - problemB.line || problemA.column - problemB.column), + reportUnusedDisableDirectives: options.reportUnusedDisableDirectives + }); + } + + /** + * Verifies the text against the rules specified by the second argument. + * @param {string|SourceCode} textOrSourceCode The text to parse or a SourceCode object. + * @param {ConfigData|ConfigArray} config An ESLintConfig instance to configure everything. + * @param {(string|(VerifyOptions&ProcessorOptions))} [filenameOrOptions] The optional filename of the file being checked. + * If this is not set, the filename will default to '' in the rule context. If + * an object, then it has "filename", "allowInlineConfig", and some properties. + * @returns {LintMessage[]} The results as an array of messages or an empty array if no messages. + */ + verify(textOrSourceCode, config, filenameOrOptions) { + debug("Verify"); + + const { configType, cwd } = internalSlotsMap.get(this); + + const options = typeof filenameOrOptions === "string" + ? { filename: filenameOrOptions } + : filenameOrOptions || {}; + + if (config) { + if (configType === "flat") { + + /* + * Because of how Webpack packages up the files, we can't + * compare directly to `FlatConfigArray` using `instanceof` + * because it's not the same `FlatConfigArray` as in the tests. + * So, we work around it by assuming an array is, in fact, a + * `FlatConfigArray` if it has a `getConfig()` method. + */ + let configArray = config; + + if (!Array.isArray(config) || typeof config.getConfig !== "function") { + configArray = new FlatConfigArray(config, { basePath: cwd }); + configArray.normalizeSync(); + } + + return this._distinguishSuppressedMessages(this._verifyWithFlatConfigArray(textOrSourceCode, configArray, options, true)); + } + + if (typeof config.extractConfig === "function") { + return this._distinguishSuppressedMessages(this._verifyWithConfigArray(textOrSourceCode, config, options)); + } + } + + /* + * If we get to here, it means `config` is just an object rather + * than a config array so we can go right into linting. + */ + + /* + * `Linter` doesn't support `overrides` property in configuration. + * So we cannot apply multiple processors. + */ + if (options.preprocess || options.postprocess) { + return this._distinguishSuppressedMessages(this._verifyWithProcessor(textOrSourceCode, config, options)); + } + return this._distinguishSuppressedMessages(this._verifyWithoutProcessors(textOrSourceCode, config, options)); + } + + /** + * Verify with a processor. + * @param {string|SourceCode} textOrSourceCode The source code. + * @param {FlatConfig} config The config array. + * @param {VerifyOptions&ProcessorOptions} options The options. + * @param {FlatConfigArray} [configForRecursive] The `ConfigArray` object to apply multiple processors recursively. + * @returns {(LintMessage|SuppressedLintMessage)[]} The found problems. + */ + _verifyWithFlatConfigArrayAndProcessor(textOrSourceCode, config, options, configForRecursive) { + const filename = options.filename || ""; + const filenameToExpose = normalizeFilename(filename); + const physicalFilename = options.physicalFilename || filenameToExpose; + const text = ensureText(textOrSourceCode); + const preprocess = options.preprocess || (rawText => [rawText]); + const postprocess = options.postprocess || (messagesList => messagesList.flat()); + const filterCodeBlock = + options.filterCodeBlock || + (blockFilename => blockFilename.endsWith(".js")); + const originalExtname = path.extname(filename); + + let blocks; + + try { + blocks = preprocess(text, filenameToExpose); + } catch (ex) { + + // If the message includes a leading line number, strip it: + const message = `Preprocessing error: ${ex.message.replace(/^line \d+:/iu, "").trim()}`; + + debug("%s\n%s", message, ex.stack); + + return [ + { + ruleId: null, + fatal: true, + severity: 2, + message, + line: ex.lineNumber, + column: ex.column, + nodeType: null + } + ]; + } + + const messageLists = blocks.map((block, i) => { + debug("A code block was found: %o", block.filename || "(unnamed)"); + + // Keep the legacy behavior. + if (typeof block === "string") { + return this._verifyWithFlatConfigArrayAndWithoutProcessors(block, config, options); + } + + const blockText = block.text; + const blockName = path.join(filename, `${i}_${block.filename}`); + + // Skip this block if filtered. + if (!filterCodeBlock(blockName, blockText)) { + debug("This code block was skipped."); + return []; + } + + // Resolve configuration again if the file content or extension was changed. + if (configForRecursive && (text !== blockText || path.extname(blockName) !== originalExtname)) { + debug("Resolving configuration again because the file content or extension was changed."); + return this._verifyWithFlatConfigArray( + blockText, + configForRecursive, + { ...options, filename: blockName, physicalFilename } + ); + } + + // Does lint. + return this._verifyWithFlatConfigArrayAndWithoutProcessors( + blockText, + config, + { ...options, filename: blockName, physicalFilename } + ); + }); + + return postprocess(messageLists, filenameToExpose); + } + + /** + * Same as linter.verify, except without support for processors. + * @param {string|SourceCode} textOrSourceCode The text to parse or a SourceCode object. + * @param {FlatConfig} providedConfig An ESLintConfig instance to configure everything. + * @param {VerifyOptions} [providedOptions] The optional filename of the file being checked. + * @throws {Error} If during rule execution. + * @returns {(LintMessage|SuppressedLintMessage)[]} The results as an array of messages or an empty array if no messages. + */ + _verifyWithFlatConfigArrayAndWithoutProcessors(textOrSourceCode, providedConfig, providedOptions) { + const slots = internalSlotsMap.get(this); + const config = providedConfig || {}; + const options = normalizeVerifyOptions(providedOptions, config); + let text; + + // evaluate arguments + if (typeof textOrSourceCode === "string") { + slots.lastSourceCode = null; + text = textOrSourceCode; + } else { + slots.lastSourceCode = textOrSourceCode; + text = textOrSourceCode.text; + } + + const languageOptions = config.languageOptions; + + languageOptions.ecmaVersion = normalizeEcmaVersionForLanguageOptions( + languageOptions.ecmaVersion + ); + + // double check that there is a parser to avoid mysterious error messages + if (!languageOptions.parser) { + throw new TypeError(`No parser specified for ${options.filename}`); + } + + // Espree expects this information to be passed in + if (isEspree(languageOptions.parser)) { + const parserOptions = languageOptions.parserOptions; + + if (languageOptions.sourceType) { + + parserOptions.sourceType = languageOptions.sourceType; + + if ( + parserOptions.sourceType === "module" && + parserOptions.ecmaFeatures && + parserOptions.ecmaFeatures.globalReturn + ) { + parserOptions.ecmaFeatures.globalReturn = false; + } + } + } + + const settings = config.settings || {}; + + if (!slots.lastSourceCode) { + const parseResult = parse( + text, + languageOptions, + options.filename + ); + + if (!parseResult.success) { + return [parseResult.error]; + } + + slots.lastSourceCode = parseResult.sourceCode; + } else { + + /* + * If the given source code object as the first argument does not have scopeManager, analyze the scope. + * This is for backward compatibility (SourceCode is frozen so it cannot rebind). + */ + if (!slots.lastSourceCode.scopeManager) { + slots.lastSourceCode = new SourceCode({ + text: slots.lastSourceCode.text, + ast: slots.lastSourceCode.ast, + parserServices: slots.lastSourceCode.parserServices, + visitorKeys: slots.lastSourceCode.visitorKeys, + scopeManager: analyzeScope(slots.lastSourceCode.ast, languageOptions) + }); + } + } + + const sourceCode = slots.lastSourceCode; + + /* + * Make adjustments based on the language options. For JavaScript, + * this is primarily about adding variables into the global scope + * to account for ecmaVersion and configured globals. + */ + sourceCode.applyLanguageOptions(languageOptions); + + const mergedInlineConfig = { + rules: {} + }; + const inlineConfigProblems = []; + + /* + * Inline config can be either enabled or disabled. If disabled, it's possible + * to detect the inline config and emit a warning (though this is not required). + * So we first check to see if inline config is allowed at all, and if so, we + * need to check if it's a warning or not. + */ + if (options.allowInlineConfig) { + + // if inline config should warn then add the warnings + if (options.warnInlineConfig) { + sourceCode.getInlineConfigNodes().forEach(node => { + inlineConfigProblems.push(createLintingProblem({ + ruleId: null, + message: `'${sourceCode.text.slice(node.range[0], node.range[1])}' has no effect because you have 'noInlineConfig' setting in ${options.warnInlineConfig}.`, + loc: node.loc, + severity: 1 + })); + + }); + } else { + const inlineConfigResult = sourceCode.applyInlineConfig(); + + inlineConfigProblems.push( + ...inlineConfigResult.problems + .map(createLintingProblem) + .map(problem => { + problem.fatal = true; + return problem; + }) + ); + + // next we need to verify information about the specified rules + const ruleValidator = new RuleValidator(); + + for (const { config: inlineConfig, node } of inlineConfigResult.configs) { + + Object.keys(inlineConfig.rules).forEach(ruleId => { + const rule = getRuleFromConfig(ruleId, config); + const ruleValue = inlineConfig.rules[ruleId]; + + if (!rule) { + inlineConfigProblems.push(createLintingProblem({ ruleId, loc: node.loc })); + return; + } + + try { + + const ruleOptions = Array.isArray(ruleValue) ? ruleValue : [ruleValue]; + + assertIsRuleOptions(ruleId, ruleValue); + assertIsRuleSeverity(ruleId, ruleOptions[0]); + + ruleValidator.validate({ + plugins: config.plugins, + rules: { + [ruleId]: ruleOptions + } + }); + mergedInlineConfig.rules[ruleId] = ruleValue; + } catch (err) { + + let baseMessage = err.message.slice( + err.message.startsWith("Key \"rules\":") + ? err.message.indexOf(":", 12) + 1 + : err.message.indexOf(":") + 1 + ).trim(); + + if (err.messageTemplate) { + baseMessage += ` You passed "${ruleValue}".`; + } + + inlineConfigProblems.push(createLintingProblem({ + ruleId, + message: `Inline configuration for rule "${ruleId}" is invalid:\n\t${baseMessage}\n`, + loc: node.loc + })); + } + }); + } + } + } + + const commentDirectives = options.allowInlineConfig && !options.warnInlineConfig + ? getDirectiveCommentsForFlatConfig( + sourceCode, + ruleId => getRuleFromConfig(ruleId, config) + ) + : { problems: [], disableDirectives: [] }; + + const configuredRules = Object.assign({}, config.rules, mergedInlineConfig.rules); + let lintingProblems; + + sourceCode.finalize(); + + try { + lintingProblems = runRules( + sourceCode, + configuredRules, + ruleId => getRuleFromConfig(ruleId, config), + void 0, + languageOptions, + settings, + options.filename, + options.disableFixes, + slots.cwd, + providedOptions.physicalFilename + ); + } catch (err) { + err.message += `\nOccurred while linting ${options.filename}`; + debug("An error occurred while traversing"); + debug("Filename:", options.filename); + if (err.currentNode) { + const { line } = err.currentNode.loc.start; + + debug("Line:", line); + err.message += `:${line}`; + } + debug("Parser Options:", languageOptions.parserOptions); + + // debug("Parser Path:", parserName); + debug("Settings:", settings); + + if (err.ruleId) { + err.message += `\nRule: "${err.ruleId}"`; + } + + throw err; + } + + return applyDisableDirectives({ + directives: commentDirectives.disableDirectives, + disableFixes: options.disableFixes, + problems: lintingProblems + .concat(commentDirectives.problems) + .concat(inlineConfigProblems) + .sort((problemA, problemB) => problemA.line - problemB.line || problemA.column - problemB.column), + reportUnusedDisableDirectives: options.reportUnusedDisableDirectives + }); + } + + /** + * Verify a given code with `ConfigArray`. + * @param {string|SourceCode} textOrSourceCode The source code. + * @param {ConfigArray} configArray The config array. + * @param {VerifyOptions&ProcessorOptions} options The options. + * @returns {(LintMessage|SuppressedLintMessage)[]} The found problems. + */ + _verifyWithConfigArray(textOrSourceCode, configArray, options) { + debug("With ConfigArray: %s", options.filename); + + // Store the config array in order to get plugin envs and rules later. + internalSlotsMap.get(this).lastConfigArray = configArray; + + // Extract the final config for this file. + const config = configArray.extractConfig(options.filename); + const processor = + config.processor && + configArray.pluginProcessors.get(config.processor); + + // Verify. + if (processor) { + debug("Apply the processor: %o", config.processor); + const { preprocess, postprocess, supportsAutofix } = processor; + const disableFixes = options.disableFixes || !supportsAutofix; + + return this._verifyWithProcessor( + textOrSourceCode, + config, + { ...options, disableFixes, postprocess, preprocess }, + configArray + ); + } + return this._verifyWithoutProcessors(textOrSourceCode, config, options); + } + + /** + * Verify a given code with a flat config. + * @param {string|SourceCode} textOrSourceCode The source code. + * @param {FlatConfigArray} configArray The config array. + * @param {VerifyOptions&ProcessorOptions} options The options. + * @param {boolean} [firstCall=false] Indicates if this is being called directly + * from verify(). (TODO: Remove once eslintrc is removed.) + * @returns {(LintMessage|SuppressedLintMessage)[]} The found problems. + */ + _verifyWithFlatConfigArray(textOrSourceCode, configArray, options, firstCall = false) { + debug("With flat config: %s", options.filename); + + // we need a filename to match configs against + const filename = options.filename || "__placeholder__.js"; + + // Store the config array in order to get plugin envs and rules later. + internalSlotsMap.get(this).lastConfigArray = configArray; + const config = configArray.getConfig(filename); + + if (!config) { + return [ + { + ruleId: null, + severity: 1, + message: `No matching configuration found for ${filename}.`, + line: 0, + column: 0, + nodeType: null + } + ]; + } + + // Verify. + if (config.processor) { + debug("Apply the processor: %o", config.processor); + const { preprocess, postprocess, supportsAutofix } = config.processor; + const disableFixes = options.disableFixes || !supportsAutofix; + + return this._verifyWithFlatConfigArrayAndProcessor( + textOrSourceCode, + config, + { ...options, filename, disableFixes, postprocess, preprocess }, + configArray + ); + } + + // check for options-based processing + if (firstCall && (options.preprocess || options.postprocess)) { + return this._verifyWithFlatConfigArrayAndProcessor(textOrSourceCode, config, options); + } + + return this._verifyWithFlatConfigArrayAndWithoutProcessors(textOrSourceCode, config, options); + } + + /** + * Verify with a processor. + * @param {string|SourceCode} textOrSourceCode The source code. + * @param {ConfigData|ExtractedConfig} config The config array. + * @param {VerifyOptions&ProcessorOptions} options The options. + * @param {ConfigArray} [configForRecursive] The `ConfigArray` object to apply multiple processors recursively. + * @returns {(LintMessage|SuppressedLintMessage)[]} The found problems. + */ + _verifyWithProcessor(textOrSourceCode, config, options, configForRecursive) { + const filename = options.filename || ""; + const filenameToExpose = normalizeFilename(filename); + const physicalFilename = options.physicalFilename || filenameToExpose; + const text = ensureText(textOrSourceCode); + const preprocess = options.preprocess || (rawText => [rawText]); + const postprocess = options.postprocess || (messagesList => messagesList.flat()); + const filterCodeBlock = + options.filterCodeBlock || + (blockFilename => blockFilename.endsWith(".js")); + const originalExtname = path.extname(filename); + + let blocks; + + try { + blocks = preprocess(text, filenameToExpose); + } catch (ex) { + + // If the message includes a leading line number, strip it: + const message = `Preprocessing error: ${ex.message.replace(/^line \d+:/iu, "").trim()}`; + + debug("%s\n%s", message, ex.stack); + + return [ + { + ruleId: null, + fatal: true, + severity: 2, + message, + line: ex.lineNumber, + column: ex.column, + nodeType: null + } + ]; + } + + const messageLists = blocks.map((block, i) => { + debug("A code block was found: %o", block.filename || "(unnamed)"); + + // Keep the legacy behavior. + if (typeof block === "string") { + return this._verifyWithoutProcessors(block, config, options); + } + + const blockText = block.text; + const blockName = path.join(filename, `${i}_${block.filename}`); + + // Skip this block if filtered. + if (!filterCodeBlock(blockName, blockText)) { + debug("This code block was skipped."); + return []; + } + + // Resolve configuration again if the file content or extension was changed. + if (configForRecursive && (text !== blockText || path.extname(blockName) !== originalExtname)) { + debug("Resolving configuration again because the file content or extension was changed."); + return this._verifyWithConfigArray( + blockText, + configForRecursive, + { ...options, filename: blockName, physicalFilename } + ); + } + + // Does lint. + return this._verifyWithoutProcessors( + blockText, + config, + { ...options, filename: blockName, physicalFilename } + ); + }); + + return postprocess(messageLists, filenameToExpose); + } + + /** + * Given a list of reported problems, distinguish problems between normal messages and suppressed messages. + * The normal messages will be returned and the suppressed messages will be stored as lastSuppressedMessages. + * @param {Array} problems A list of reported problems. + * @returns {LintMessage[]} A list of LintMessage. + */ + _distinguishSuppressedMessages(problems) { + const messages = []; + const suppressedMessages = []; + const slots = internalSlotsMap.get(this); + + for (const problem of problems) { + if (problem.suppressions) { + suppressedMessages.push(problem); + } else { + messages.push(problem); + } + } + + slots.lastSuppressedMessages = suppressedMessages; + + return messages; + } + + /** + * Gets the SourceCode object representing the parsed source. + * @returns {SourceCode} The SourceCode object. + */ + getSourceCode() { + return internalSlotsMap.get(this).lastSourceCode; + } + + /** + * Gets the list of SuppressedLintMessage produced in the last running. + * @returns {SuppressedLintMessage[]} The list of SuppressedLintMessage + */ + getSuppressedMessages() { + return internalSlotsMap.get(this).lastSuppressedMessages; + } + + /** + * Defines a new linting rule. + * @param {string} ruleId A unique rule identifier + * @param {Function | Rule} ruleModule Function from context to object mapping AST node types to event handlers + * @returns {void} + */ + defineRule(ruleId, ruleModule) { + assertEslintrcConfig(this); + internalSlotsMap.get(this).ruleMap.define(ruleId, ruleModule); + } + + /** + * Defines many new linting rules. + * @param {Record} rulesToDefine map from unique rule identifier to rule + * @returns {void} + */ + defineRules(rulesToDefine) { + assertEslintrcConfig(this); + Object.getOwnPropertyNames(rulesToDefine).forEach(ruleId => { + this.defineRule(ruleId, rulesToDefine[ruleId]); + }); + } + + /** + * Gets an object with all loaded rules. + * @returns {Map} All loaded rules + */ + getRules() { + assertEslintrcConfig(this); + const { lastConfigArray, ruleMap } = internalSlotsMap.get(this); + + return new Map(function *() { + yield* ruleMap; + + if (lastConfigArray) { + yield* lastConfigArray.pluginRules; + } + }()); + } + + /** + * Define a new parser module + * @param {string} parserId Name of the parser + * @param {Parser} parserModule The parser object + * @returns {void} + */ + defineParser(parserId, parserModule) { + assertEslintrcConfig(this); + internalSlotsMap.get(this).parserMap.set(parserId, parserModule); + } + + /** + * Performs multiple autofix passes over the text until as many fixes as possible + * have been applied. + * @param {string} text The source text to apply fixes to. + * @param {ConfigData|ConfigArray|FlatConfigArray} config The ESLint config object to use. + * @param {VerifyOptions&ProcessorOptions&FixOptions} options The ESLint options object to use. + * @returns {{fixed:boolean,messages:LintMessage[],output:string}} The result of the fix operation as returned from the + * SourceCodeFixer. + */ + verifyAndFix(text, config, options) { + let messages = [], + fixedResult, + fixed = false, + passNumber = 0, + currentText = text; + const debugTextDescription = options && options.filename || `${text.slice(0, 10)}...`; + const shouldFix = options && typeof options.fix !== "undefined" ? options.fix : true; + + /** + * This loop continues until one of the following is true: + * + * 1. No more fixes have been applied. + * 2. Ten passes have been made. + * + * That means anytime a fix is successfully applied, there will be another pass. + * Essentially, guaranteeing a minimum of two passes. + */ + do { + passNumber++; + + debug(`Linting code for ${debugTextDescription} (pass ${passNumber})`); + messages = this.verify(currentText, config, options); + + debug(`Generating fixed text for ${debugTextDescription} (pass ${passNumber})`); + fixedResult = SourceCodeFixer.applyFixes(currentText, messages, shouldFix); + + /* + * stop if there are any syntax errors. + * 'fixedResult.output' is a empty string. + */ + if (messages.length === 1 && messages[0].fatal) { + break; + } + + // keep track if any fixes were ever applied - important for return value + fixed = fixed || fixedResult.fixed; + + // update to use the fixed output instead of the original text + currentText = fixedResult.output; + + } while ( + fixedResult.fixed && + passNumber < MAX_AUTOFIX_PASSES + ); + + /* + * If the last result had fixes, we need to lint again to be sure we have + * the most up-to-date information. + */ + if (fixedResult.fixed) { + fixedResult.messages = this.verify(currentText, config, options); + } + + // ensure the last result properly reflects if fixes were done + fixedResult.fixed = fixed; + fixedResult.output = currentText; + + return fixedResult; + } } module.exports = { - Linter, - - /** - * Get the internal slots of a given Linter instance for tests. - * @param {Linter} instance The Linter instance to get. - * @returns {LinterInternalSlots} The internal slots. - */ - getLinterInternalSlots(instance) { - return internalSlotsMap.get(instance); - }, + Linter, + + /** + * Get the internal slots of a given Linter instance for tests. + * @param {Linter} instance The Linter instance to get. + * @returns {LinterInternalSlots} The internal slots. + */ + getLinterInternalSlots(instance) { + return internalSlotsMap.get(instance); + } }; diff --git a/node_modules/eslint/lib/linter/rule-fixer.js b/node_modules/eslint/lib/linter/rule-fixer.js index e73dac672..bdd80d13b 100644 --- a/node_modules/eslint/lib/linter/rule-fixer.js +++ b/node_modules/eslint/lib/linter/rule-fixer.js @@ -4,16 +4,6 @@ */ "use strict"; -//------------------------------------------------------------------------------ -// Typedefs -//------------------------------------------------------------------------------ - -/** - * @import { SourceRange } from "@eslint/core"; - */ - -/* eslint class-methods-use-this: off -- Methods desired on instance */ - //------------------------------------------------------------------------------ // Requirements //------------------------------------------------------------------------------ @@ -26,16 +16,16 @@ /** * Creates a fix command that inserts text at the specified index in the source text. - * @param {number} index The 0-based index at which to insert the new text. + * @param {int} index The 0-based index at which to insert the new text. * @param {string} text The text to insert. * @returns {Object} The fix command. * @private */ function insertTextAt(index, text) { - return { - range: [index, index], - text, - }; + return { + range: [index, index], + text + }; } //------------------------------------------------------------------------------ @@ -45,125 +35,106 @@ function insertTextAt(index, text) { /** * Creates code fixing commands for rules. */ -class RuleFixer { - /** - * The source code object representing the text to be fixed. - * @type {SourceCode} - */ - #sourceCode; - - /** - * Creates a new instance. - * @param {Object} options The options for the fixer. - * @param {SourceCode} options.sourceCode The source code object representing the text to be fixed. - */ - constructor({ sourceCode }) { - this.#sourceCode = sourceCode; - } - - /** - * Creates a fix command that inserts text after the given node or token. - * The fix is not applied until applyFixes() is called. - * @param {ASTNode|Token} nodeOrToken The node or token to insert after. - * @param {string} text The text to insert. - * @returns {Object} The fix command. - */ - insertTextAfter(nodeOrToken, text) { - const range = this.#sourceCode.getRange(nodeOrToken); - - return this.insertTextAfterRange(range, text); - } - - /** - * Creates a fix command that inserts text after the specified range in the source text. - * The fix is not applied until applyFixes() is called. - * @param {SourceRange} range The range to replace, first item is start of range, second - * is end of range. - * @param {string} text The text to insert. - * @returns {Object} The fix command. - */ - insertTextAfterRange(range, text) { - return insertTextAt(range[1], text); - } - - /** - * Creates a fix command that inserts text before the given node or token. - * The fix is not applied until applyFixes() is called. - * @param {ASTNode|Token} nodeOrToken The node or token to insert before. - * @param {string} text The text to insert. - * @returns {Object} The fix command. - */ - insertTextBefore(nodeOrToken, text) { - const range = this.#sourceCode.getRange(nodeOrToken); - - return this.insertTextBeforeRange(range, text); - } - - /** - * Creates a fix command that inserts text before the specified range in the source text. - * The fix is not applied until applyFixes() is called. - * @param {SourceRange} range The range to replace, first item is start of range, second - * is end of range. - * @param {string} text The text to insert. - * @returns {Object} The fix command. - */ - insertTextBeforeRange(range, text) { - return insertTextAt(range[0], text); - } - - /** - * Creates a fix command that replaces text at the node or token. - * The fix is not applied until applyFixes() is called. - * @param {ASTNode|Token} nodeOrToken The node or token to remove. - * @param {string} text The text to insert. - * @returns {Object} The fix command. - */ - replaceText(nodeOrToken, text) { - const range = this.#sourceCode.getRange(nodeOrToken); - - return this.replaceTextRange(range, text); - } - - /** - * Creates a fix command that replaces text at the specified range in the source text. - * The fix is not applied until applyFixes() is called. - * @param {SourceRange} range The range to replace, first item is start of range, second - * is end of range. - * @param {string} text The text to insert. - * @returns {Object} The fix command. - */ - replaceTextRange(range, text) { - return { - range, - text, - }; - } - - /** - * Creates a fix command that removes the node or token from the source. - * The fix is not applied until applyFixes() is called. - * @param {ASTNode|Token} nodeOrToken The node or token to remove. - * @returns {Object} The fix command. - */ - remove(nodeOrToken) { - const range = this.#sourceCode.getRange(nodeOrToken); - - return this.removeRange(range); - } - - /** - * Creates a fix command that removes the specified range of text from the source. - * The fix is not applied until applyFixes() is called. - * @param {SourceRange} range The range to remove, first item is start of range, second - * is end of range. - * @returns {Object} The fix command. - */ - removeRange(range) { - return { - range, - text: "", - }; - } -} -module.exports = { RuleFixer }; +const ruleFixer = Object.freeze({ + + /** + * Creates a fix command that inserts text after the given node or token. + * The fix is not applied until applyFixes() is called. + * @param {ASTNode|Token} nodeOrToken The node or token to insert after. + * @param {string} text The text to insert. + * @returns {Object} The fix command. + */ + insertTextAfter(nodeOrToken, text) { + return this.insertTextAfterRange(nodeOrToken.range, text); + }, + + /** + * Creates a fix command that inserts text after the specified range in the source text. + * The fix is not applied until applyFixes() is called. + * @param {int[]} range The range to replace, first item is start of range, second + * is end of range. + * @param {string} text The text to insert. + * @returns {Object} The fix command. + */ + insertTextAfterRange(range, text) { + return insertTextAt(range[1], text); + }, + + /** + * Creates a fix command that inserts text before the given node or token. + * The fix is not applied until applyFixes() is called. + * @param {ASTNode|Token} nodeOrToken The node or token to insert before. + * @param {string} text The text to insert. + * @returns {Object} The fix command. + */ + insertTextBefore(nodeOrToken, text) { + return this.insertTextBeforeRange(nodeOrToken.range, text); + }, + + /** + * Creates a fix command that inserts text before the specified range in the source text. + * The fix is not applied until applyFixes() is called. + * @param {int[]} range The range to replace, first item is start of range, second + * is end of range. + * @param {string} text The text to insert. + * @returns {Object} The fix command. + */ + insertTextBeforeRange(range, text) { + return insertTextAt(range[0], text); + }, + + /** + * Creates a fix command that replaces text at the node or token. + * The fix is not applied until applyFixes() is called. + * @param {ASTNode|Token} nodeOrToken The node or token to remove. + * @param {string} text The text to insert. + * @returns {Object} The fix command. + */ + replaceText(nodeOrToken, text) { + return this.replaceTextRange(nodeOrToken.range, text); + }, + + /** + * Creates a fix command that replaces text at the specified range in the source text. + * The fix is not applied until applyFixes() is called. + * @param {int[]} range The range to replace, first item is start of range, second + * is end of range. + * @param {string} text The text to insert. + * @returns {Object} The fix command. + */ + replaceTextRange(range, text) { + return { + range, + text + }; + }, + + /** + * Creates a fix command that removes the node or token from the source. + * The fix is not applied until applyFixes() is called. + * @param {ASTNode|Token} nodeOrToken The node or token to remove. + * @returns {Object} The fix command. + */ + remove(nodeOrToken) { + return this.removeRange(nodeOrToken.range); + }, + + /** + * Creates a fix command that removes the specified range of text from the source. + * The fix is not applied until applyFixes() is called. + * @param {int[]} range The range to remove, first item is start of range, second + * is end of range. + * @returns {Object} The fix command. + */ + removeRange(range) { + return { + range, + text: "" + }; + } + +}); + + +module.exports = ruleFixer; diff --git a/node_modules/eslint/lib/linter/rules.js b/node_modules/eslint/lib/linter/rules.js index ec017eefb..647bab687 100644 --- a/node_modules/eslint/lib/linter/rules.js +++ b/node_modules/eslint/lib/linter/rules.js @@ -13,10 +13,18 @@ const builtInRules = require("../rules"); //------------------------------------------------------------------------------ -// Typedefs +// Helpers //------------------------------------------------------------------------------ -/** @typedef {import("../types").Rule.RuleModule} Rule */ +/** + * Normalizes a rule module to the new-style API + * @param {(Function|{create: Function})} rule A rule object, which can either be a function + * ("old-style") or an object with a `create` method ("new-style") + * @returns {{create: Function}} A new-style rule. + */ +function normalizeRule(rule) { + return typeof rule === "function" ? Object.assign({ create: rule }, rule) : rule; +} //------------------------------------------------------------------------------ // Public Interface @@ -26,46 +34,47 @@ const builtInRules = require("../rules"); * A storage for rules. */ class Rules { - constructor() { - this._rules = Object.create(null); - } + constructor() { + this._rules = Object.create(null); + } - /** - * Registers a rule module for rule id in storage. - * @param {string} ruleId Rule id (file name). - * @param {Rule} rule Rule object. - * @returns {void} - */ - define(ruleId, rule) { - this._rules[ruleId] = rule; - } + /** + * Registers a rule module for rule id in storage. + * @param {string} ruleId Rule id (file name). + * @param {Function} ruleModule Rule handler. + * @returns {void} + */ + define(ruleId, ruleModule) { + this._rules[ruleId] = normalizeRule(ruleModule); + } - /** - * Access rule handler by id (file name). - * @param {string} ruleId Rule id (file name). - * @returns {Rule} Rule object. - */ - get(ruleId) { - if (typeof this._rules[ruleId] === "string") { - this.define(ruleId, require(this._rules[ruleId])); - } - if (this._rules[ruleId]) { - return this._rules[ruleId]; - } - if (builtInRules.has(ruleId)) { - return builtInRules.get(ruleId); - } + /** + * Access rule handler by id (file name). + * @param {string} ruleId Rule id (file name). + * @returns {{create: Function, schema: JsonSchema[]}} + * A rule. This is normalized to always have the new-style shape with a `create` method. + */ + get(ruleId) { + if (typeof this._rules[ruleId] === "string") { + this.define(ruleId, require(this._rules[ruleId])); + } + if (this._rules[ruleId]) { + return this._rules[ruleId]; + } + if (builtInRules.has(ruleId)) { + return builtInRules.get(ruleId); + } - return null; - } + return null; + } - *[Symbol.iterator]() { - yield* builtInRules; + *[Symbol.iterator]() { + yield* builtInRules; - for (const ruleId of Object.keys(this._rules)) { - yield [ruleId, this.get(ruleId)]; - } - } + for (const ruleId of Object.keys(this._rules)) { + yield [ruleId, this.get(ruleId)]; + } + } } module.exports = Rules; diff --git a/node_modules/eslint/lib/linter/source-code-fixer.js b/node_modules/eslint/lib/linter/source-code-fixer.js index 2cea2c604..15386c926 100644 --- a/node_modules/eslint/lib/linter/source-code-fixer.js +++ b/node_modules/eslint/lib/linter/source-code-fixer.js @@ -20,22 +20,22 @@ const BOM = "\uFEFF"; * Compares items in a messages array by range. * @param {Message} a The first message. * @param {Message} b The second message. - * @returns {number} -1 if a comes before b, 1 if a comes after b, 0 if equal. + * @returns {int} -1 if a comes before b, 1 if a comes after b, 0 if equal. * @private */ function compareMessagesByFixRange(a, b) { - return a.fix.range[0] - b.fix.range[0] || a.fix.range[1] - b.fix.range[1]; + return a.fix.range[0] - b.fix.range[0] || a.fix.range[1] - b.fix.range[1]; } /** * Compares items in a messages array by line and column. * @param {Message} a The first message. * @param {Message} b The second message. - * @returns {number} -1 if a comes before b, 1 if a comes after b, 0 if equal. + * @returns {int} -1 if a comes before b, 1 if a comes after b, 0 if equal. * @private */ function compareMessagesByLocation(a, b) { - return a.line - b.line || a.column - b.column; + return a.line - b.line || a.column - b.column; } //------------------------------------------------------------------------------ @@ -47,7 +47,7 @@ function compareMessagesByLocation(a, b) { * @constructor */ function SourceCodeFixer() { - Object.freeze(this); + Object.freeze(this); } /** @@ -58,97 +58,95 @@ function SourceCodeFixer() { * @param {boolean|Function} [shouldFix=true] Determines whether each message should be fixed * @returns {Object} An object containing the fixed text and any unfixed messages. */ -SourceCodeFixer.applyFixes = function (sourceText, messages, shouldFix) { - debug("Applying fixes"); - - if (shouldFix === false) { - debug("shouldFix parameter was false, not attempting fixes"); - return { - fixed: false, - messages, - output: sourceText, - }; - } - - // clone the array - const remainingMessages = [], - fixes = [], - bom = sourceText.startsWith(BOM) ? BOM : "", - text = bom ? sourceText.slice(1) : sourceText; - let lastPos = Number.NEGATIVE_INFINITY, - output = bom; - - /** - * Try to use the 'fix' from a problem. - * @param {Message} problem The message object to apply fixes from - * @returns {boolean} Whether fix was successfully applied - */ - function attemptFix(problem) { - const fix = problem.fix; - const start = fix.range[0]; - const end = fix.range[1]; - - // Remain it as a problem if it's overlapped or it's a negative range - if (lastPos >= start || start > end) { - remainingMessages.push(problem); - return false; - } - - // Remove BOM. - if ( - (start < 0 && end >= 0) || - (start === 0 && fix.text.startsWith(BOM)) - ) { - output = ""; - } - - // Make output to this fix. - output += text.slice(Math.max(0, lastPos), Math.max(0, start)); - output += fix.text; - lastPos = end; - return true; - } - - messages.forEach(problem => { - if (Object.hasOwn(problem, "fix") && problem.fix) { - fixes.push(problem); - } else { - remainingMessages.push(problem); - } - }); - - if (fixes.length) { - debug("Found fixes to apply"); - let fixesWereApplied = false; - - for (const problem of fixes.sort(compareMessagesByFixRange)) { - if (typeof shouldFix !== "function" || shouldFix(problem)) { - attemptFix(problem); - - /* - * The only time attemptFix will fail is if a previous fix was - * applied which conflicts with it. So we can mark this as true. - */ - fixesWereApplied = true; - } else { - remainingMessages.push(problem); - } - } - output += text.slice(Math.max(0, lastPos)); - - return { - fixed: fixesWereApplied, - messages: remainingMessages.sort(compareMessagesByLocation), - output, - }; - } - - debug("No fixes to apply"); - return { - fixed: false, - messages, - output: bom + text, - }; +SourceCodeFixer.applyFixes = function(sourceText, messages, shouldFix) { + debug("Applying fixes"); + + if (shouldFix === false) { + debug("shouldFix parameter was false, not attempting fixes"); + return { + fixed: false, + messages, + output: sourceText + }; + } + + // clone the array + const remainingMessages = [], + fixes = [], + bom = sourceText.startsWith(BOM) ? BOM : "", + text = bom ? sourceText.slice(1) : sourceText; + let lastPos = Number.NEGATIVE_INFINITY, + output = bom; + + /** + * Try to use the 'fix' from a problem. + * @param {Message} problem The message object to apply fixes from + * @returns {boolean} Whether fix was successfully applied + */ + function attemptFix(problem) { + const fix = problem.fix; + const start = fix.range[0]; + const end = fix.range[1]; + + // Remain it as a problem if it's overlapped or it's a negative range + if (lastPos >= start || start > end) { + remainingMessages.push(problem); + return false; + } + + // Remove BOM. + if ((start < 0 && end >= 0) || (start === 0 && fix.text.startsWith(BOM))) { + output = ""; + } + + // Make output to this fix. + output += text.slice(Math.max(0, lastPos), Math.max(0, start)); + output += fix.text; + lastPos = end; + return true; + } + + messages.forEach(problem => { + if (Object.prototype.hasOwnProperty.call(problem, "fix")) { + fixes.push(problem); + } else { + remainingMessages.push(problem); + } + }); + + if (fixes.length) { + debug("Found fixes to apply"); + let fixesWereApplied = false; + + for (const problem of fixes.sort(compareMessagesByFixRange)) { + if (typeof shouldFix !== "function" || shouldFix(problem)) { + attemptFix(problem); + + /* + * The only time attemptFix will fail is if a previous fix was + * applied which conflicts with it. So we can mark this as true. + */ + fixesWereApplied = true; + } else { + remainingMessages.push(problem); + } + } + output += text.slice(Math.max(0, lastPos)); + + return { + fixed: fixesWereApplied, + messages: remainingMessages.sort(compareMessagesByLocation), + output + }; + } + + debug("No fixes to apply"); + return { + fixed: false, + messages, + output: bom + text + }; + }; module.exports = SourceCodeFixer; diff --git a/node_modules/eslint/lib/linter/source-code-traverser.js b/node_modules/eslint/lib/linter/source-code-traverser.js deleted file mode 100644 index 0a6235e04..000000000 --- a/node_modules/eslint/lib/linter/source-code-traverser.js +++ /dev/null @@ -1,333 +0,0 @@ -/** - * @fileoverview Traverser for SourceCode objects. - * @author Nicholas C. Zakas - */ - -"use strict"; - -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ - -const { parse, matches } = require("./esquery"); -const vk = require("eslint-visitor-keys"); - -//----------------------------------------------------------------------------- -// Typedefs -//----------------------------------------------------------------------------- - -/** - * @import { Language, SourceCode } from "@eslint/core"; - * @import { ESQueryOptions } from "esquery"; - * @import { ESQueryParsedSelector } from "./esquery.js"; - * @import { SourceCodeVisitor } from "./source-code-visitor.js"; - */ - -//----------------------------------------------------------------------------- -// Helpers -//----------------------------------------------------------------------------- - -const STEP_KIND_VISIT = 1; -const STEP_KIND_CALL = 2; - -/** - * Compares two ESQuery selectors by specificity. - * @param {ESQueryParsedSelector} a The first selector to compare. - * @param {ESQueryParsedSelector} b The second selector to compare. - * @returns {number} A negative number if `a` is less specific than `b` or they are equally specific and `a` <= `b` alphabetically, a positive number if `a` is more specific than `b`. - */ -function compareSpecificity(a, b) { - return a.compare(b); -} - -/** - * Helper to wrap ESQuery operations. - */ -class ESQueryHelper { - /** - * Creates a new instance. - * @param {SourceCodeVisitor} visitor The visitor containing the functions to call. - * @param {ESQueryOptions} esqueryOptions `esquery` options for traversing custom nodes. - */ - constructor(visitor, esqueryOptions) { - /** - * The visitor to use during traversal. - * @type {SourceCodeVisitor} - */ - this.visitor = visitor; - - /** - * The options for `esquery` to use during matching. - * @type {ESQueryOptions} - */ - this.esqueryOptions = esqueryOptions; - - /** - * A map of node type to selectors targeting that node type on the - * enter phase of traversal. - * @type {Map} - */ - this.enterSelectorsByNodeType = new Map(); - - /** - * A map of node type to selectors targeting that node type on the - * exit phase of traversal. - * @type {Map} - */ - this.exitSelectorsByNodeType = new Map(); - - /** - * An array of selectors that match any node type on the - * enter phase of traversal. - * @type {ESQueryParsedSelector[]} - */ - this.anyTypeEnterSelectors = []; - - /** - * An array of selectors that match any node type on the - * exit phase of traversal. - * @type {ESQueryParsedSelector[]} - */ - this.anyTypeExitSelectors = []; - - visitor.forEachName(rawSelector => { - const selector = parse(rawSelector); - - /* - * If this selector has identified specific node types, - * add it to the map for these node types for faster lookup. - */ - if (selector.nodeTypes) { - const typeMap = selector.isExit - ? this.exitSelectorsByNodeType - : this.enterSelectorsByNodeType; - - selector.nodeTypes.forEach(nodeType => { - if (!typeMap.has(nodeType)) { - typeMap.set(nodeType, []); - } - typeMap.get(nodeType).push(selector); - }); - return; - } - - /* - * Remaining selectors are added to the "any type" selectors - * list for the appropriate phase of traversal. This ensures - * that all selectors will still be applied even if no - * specific node type is matched. - */ - const selectors = selector.isExit - ? this.anyTypeExitSelectors - : this.anyTypeEnterSelectors; - - selectors.push(selector); - }); - - // sort all selectors by specificity for prioritizing call order - this.anyTypeEnterSelectors.sort(compareSpecificity); - this.anyTypeExitSelectors.sort(compareSpecificity); - this.enterSelectorsByNodeType.forEach(selectorList => - selectorList.sort(compareSpecificity), - ); - this.exitSelectorsByNodeType.forEach(selectorList => - selectorList.sort(compareSpecificity), - ); - } - - /** - * Checks if a node matches a given selector. - * @param {ASTNode} node The node to check - * @param {ASTNode[]} ancestry The ancestry of the node being checked. - * @param {ESQueryParsedSelector} selector An AST selector descriptor - * @returns {boolean} `true` if the selector matches the node, `false` otherwise - */ - matches(node, ancestry, selector) { - return matches(node, selector.root, ancestry, this.esqueryOptions); - } - - /** - * Calculates all appropriate selectors to a node, in specificity order - * @param {ASTNode} node The node to check - * @param {ASTNode[]} ancestry The ancestry of the node being checked. - * @param {boolean} isExit `false` if the node is currently being entered, `true` if it's currently being exited - * @returns {string[]} An array of selectors that match the node. - */ - calculateSelectors(node, ancestry, isExit) { - const nodeTypeKey = this.esqueryOptions?.nodeTypeKey || "type"; - const selectors = []; - - /* - * Get the selectors that may match this node. First, check - * to see if the node type has specific selectors, - * then gather the "any type" selectors. - */ - const selectorsByNodeType = - (isExit - ? this.exitSelectorsByNodeType - : this.enterSelectorsByNodeType - ).get(node[nodeTypeKey]) || []; - const anyTypeSelectors = isExit - ? this.anyTypeExitSelectors - : this.anyTypeEnterSelectors; - - /* - * selectorsByNodeType and anyTypeSelectors were already sorted by specificity in the constructor. - * Iterate through each of them, applying selectors in the right order. - */ - let selectorsByNodeTypeIndex = 0; - let anyTypeSelectorsIndex = 0; - - while ( - selectorsByNodeTypeIndex < selectorsByNodeType.length || - anyTypeSelectorsIndex < anyTypeSelectors.length - ) { - /* - * If we've already exhausted the selectors for this node type, - * or if the next any type selector is more specific than the - * next selector for this node type, apply the any type selector. - */ - const hasMoreNodeTypeSelectors = - selectorsByNodeTypeIndex < selectorsByNodeType.length; - const hasMoreAnyTypeSelectors = - anyTypeSelectorsIndex < anyTypeSelectors.length; - const anyTypeSelector = anyTypeSelectors[anyTypeSelectorsIndex]; - const nodeTypeSelector = - selectorsByNodeType[selectorsByNodeTypeIndex]; - - // Only compare specificity if both selectors exist - const isAnyTypeSelectorLessSpecific = - hasMoreAnyTypeSelectors && - hasMoreNodeTypeSelectors && - anyTypeSelector.compare(nodeTypeSelector) < 0; - - if (!hasMoreNodeTypeSelectors || isAnyTypeSelectorLessSpecific) { - anyTypeSelectorsIndex++; - - if (this.matches(node, ancestry, anyTypeSelector)) { - selectors.push(anyTypeSelector.source); - } - } else { - selectorsByNodeTypeIndex++; - - if (this.matches(node, ancestry, nodeTypeSelector)) { - selectors.push(nodeTypeSelector.source); - } - } - } - - return selectors; - } -} - -//------------------------------------------------------------------------------ -// Public Interface -//------------------------------------------------------------------------------ - -/** - * Traverses source code and ensures that visitor methods are called when - * entering and leaving each node. - */ -class SourceCodeTraverser { - /** - * The language of the source code being traversed. - * @type {Language} - */ - #language; - - /** - * Map of languages to instances of this class. - * @type {WeakMap} - */ - static instances = new WeakMap(); - - /** - * Creates a new instance. - * @param {Language} language The language of the source code being traversed. - */ - constructor(language) { - this.#language = language; - } - - static getInstance(language) { - if (!this.instances.has(language)) { - this.instances.set(language, new this(language)); - } - - return this.instances.get(language); - } - - /** - * Traverses the given source code synchronously. - * @param {SourceCode} sourceCode The source code to traverse. - * @param {SourceCodeVisitor} visitor The emitter to use for events. - * @param {Object} options Options for traversal. - * @param {ReturnType} options.steps The steps to take during traversal. - * @returns {void} - * @throws {Error} If an error occurs during traversal. - */ - traverseSync(sourceCode, visitor, { steps } = {}) { - const esquery = new ESQueryHelper(visitor, { - visitorKeys: sourceCode.visitorKeys ?? this.#language.visitorKeys, - fallback: vk.getKeys, - matchClass: this.#language.matchesSelectorClass ?? (() => false), - nodeTypeKey: this.#language.nodeTypeKey, - }); - - const currentAncestry = []; - - for (const step of steps ?? sourceCode.traverse()) { - switch (step.kind) { - case STEP_KIND_VISIT: { - try { - if (step.phase === 1) { - esquery - .calculateSelectors( - step.target, - currentAncestry, - false, - ) - .forEach(selector => { - visitor.callSync( - selector, - ...(step.args ?? [step.target]), - ); - }); - currentAncestry.unshift(step.target); - } else { - currentAncestry.shift(); - esquery - .calculateSelectors( - step.target, - currentAncestry, - true, - ) - .forEach(selector => { - visitor.callSync( - selector, - ...(step.args ?? [step.target]), - ); - }); - } - } catch (err) { - err.currentNode = step.target; - throw err; - } - break; - } - - case STEP_KIND_CALL: { - visitor.callSync(step.target, ...step.args); - break; - } - - default: - throw new Error( - `Invalid traversal step found: "${step.kind}".`, - ); - } - } - } -} - -module.exports = { SourceCodeTraverser }; diff --git a/node_modules/eslint/lib/linter/source-code-visitor.js b/node_modules/eslint/lib/linter/source-code-visitor.js deleted file mode 100644 index 0f5af5658..000000000 --- a/node_modules/eslint/lib/linter/source-code-visitor.js +++ /dev/null @@ -1,81 +0,0 @@ -/** - * @fileoverview SourceCodeVisitor class - * @author Nicholas C. Zakas - */ - -"use strict"; - -//----------------------------------------------------------------------------- -// Helpers -//----------------------------------------------------------------------------- - -const emptyArray = Object.freeze([]); - -//------------------------------------------------------------------------------ -// Exports -//------------------------------------------------------------------------------ - -/** - * A structure to hold a list of functions to call for a given name. - * This is used to allow multiple rules to register functions for a given name - * without having to know about each other. - */ -class SourceCodeVisitor { - /** - * The functions to call for a given name. - * @type {Map} - */ - #functions = new Map(); - - /** - * Adds a function to the list of functions to call for a given name. - * @param {string} name The name of the function to call. - * @param {Function} func The function to call. - * @returns {void} - */ - add(name, func) { - if (this.#functions.has(name)) { - this.#functions.get(name).push(func); - } else { - this.#functions.set(name, [func]); - } - } - - /** - * Gets the list of functions to call for a given name. - * @param {string} name The name of the function to call. - * @returns {Function[]} The list of functions to call. - */ - get(name) { - if (this.#functions.has(name)) { - return this.#functions.get(name); - } - - return emptyArray; - } - - /** - * Iterates over all names and calls the callback with the name. - * @param {(name:string) => void} callback The callback to call for each name. - * @returns {void} - */ - forEachName(callback) { - this.#functions.forEach((funcs, name) => { - callback(name); - }); - } - - /** - * Calls the functions for a given name with the given arguments. - * @param {string} name The name of the function to call. - * @param {any[]} args The arguments to pass to the function. - * @returns {void} - */ - callSync(name, ...args) { - if (this.#functions.has(name)) { - this.#functions.get(name).forEach(func => func(...args)); - } - } -} - -module.exports = { SourceCodeVisitor }; diff --git a/node_modules/eslint/lib/linter/timing.js b/node_modules/eslint/lib/linter/timing.js index 1791f8fdf..1076ff258 100644 --- a/node_modules/eslint/lib/linter/timing.js +++ b/node_modules/eslint/lib/linter/timing.js @@ -5,8 +5,6 @@ "use strict"; -const { startTime, endTime } = require("../shared/stats"); - //------------------------------------------------------------------------------ // Helpers //------------------------------------------------------------------------------ @@ -15,26 +13,26 @@ const { startTime, endTime } = require("../shared/stats"); /** * Align the string to left * @param {string} str string to evaluate - * @param {number} len length of the string + * @param {int} len length of the string * @param {string} ch delimiter character * @returns {string} modified string * @private */ function alignLeft(str, len, ch) { - return str + new Array(len - str.length + 1).join(ch || " "); + return str + new Array(len - str.length + 1).join(ch || " "); } /* c8 ignore next */ /** * Align the string to right * @param {string} str string to evaluate - * @param {number} len length of the string + * @param {int} len length of the string * @param {string} ch delimiter character * @returns {string} modified string * @private */ function alignRight(str, len, ch) { - return new Array(len - str.length + 1).join(ch || " ") + str; + return new Array(len - str.length + 1).join(ch || " ") + str; } //------------------------------------------------------------------------------ @@ -51,21 +49,19 @@ const ALIGN = [alignLeft, alignRight, alignRight]; * @returns {number} the number of rules to show */ function getListSize() { - const MINIMUM_SIZE = 10; + const MINIMUM_SIZE = 10; - if (typeof process.env.TIMING !== "string") { - return MINIMUM_SIZE; - } + if (typeof process.env.TIMING !== "string") { + return MINIMUM_SIZE; + } - if (process.env.TIMING.toLowerCase() === "all") { - return Number.POSITIVE_INFINITY; - } + if (process.env.TIMING.toLowerCase() === "all") { + return Number.POSITIVE_INFINITY; + } - const TIMING_ENV_VAR_AS_INTEGER = Number.parseInt(process.env.TIMING, 10); + const TIMING_ENV_VAR_AS_INTEGER = Number.parseInt(process.env.TIMING, 10); - return TIMING_ENV_VAR_AS_INTEGER > 10 - ? TIMING_ENV_VAR_AS_INTEGER - : MINIMUM_SIZE; + return TIMING_ENV_VAR_AS_INTEGER > 10 ? TIMING_ENV_VAR_AS_INTEGER : MINIMUM_SIZE; } /* c8 ignore next */ @@ -76,134 +72,90 @@ function getListSize() { * @private */ function display(data) { - let total = 0; - const rows = Object.keys(data) - .map(key => { - const time = data[key]; - - total += time; - return [key, time]; - }) - .sort((a, b) => b[1] - a[1]) - .slice(0, getListSize()); - - rows.forEach(row => { - row.push(`${((row[1] * 100) / total).toFixed(1)}%`); - row[1] = row[1].toFixed(3); - }); - - rows.unshift(HEADERS); - - const widths = []; - - rows.forEach(row => { - const len = row.length; - - for (let i = 0; i < len; i++) { - const n = row[i].length; - - if (!widths[i] || n > widths[i]) { - widths[i] = n; - } - } - }); - - const table = rows.map(row => - row.map((cell, index) => ALIGN[index](cell, widths[index])).join(" | "), - ); - - table.splice( - 1, - 0, - widths - .map((width, index) => { - const extraAlignment = - index !== 0 && index !== widths.length - 1 ? 2 : 1; - - return ALIGN[index](":", width + extraAlignment, "-"); - }) - .join("|"), - ); - - console.log(table.join("\n")); // eslint-disable-line no-console -- Debugging function + let total = 0; + const rows = Object.keys(data) + .map(key => { + const time = data[key]; + + total += time; + return [key, time]; + }) + .sort((a, b) => b[1] - a[1]) + .slice(0, getListSize()); + + rows.forEach(row => { + row.push(`${(row[1] * 100 / total).toFixed(1)}%`); + row[1] = row[1].toFixed(3); + }); + + rows.unshift(HEADERS); + + const widths = []; + + rows.forEach(row => { + const len = row.length; + + for (let i = 0; i < len; i++) { + const n = row[i].length; + + if (!widths[i] || n > widths[i]) { + widths[i] = n; + } + } + }); + + const table = rows.map(row => ( + row + .map((cell, index) => ALIGN[index](cell, widths[index])) + .join(" | ") + )); + + table.splice(1, 0, widths.map((width, index) => { + const extraAlignment = index !== 0 && index !== widths.length - 1 ? 2 : 1; + + return ALIGN[index](":", width + extraAlignment, "-"); + }).join("|")); + + console.log(table.join("\n")); // eslint-disable-line no-console -- Debugging function } /* c8 ignore next */ -module.exports = (function () { - const data = Object.create(null); - let displayEnabled = true; - - /** - * Time the run - * @param {any} key key from the data object - * @param {Function} fn function to be called - * @param {boolean} stats if 'stats' is true, return the result and the time difference - * @returns {Function} function to be executed - * @private - */ - function time(key, fn, stats) { - return function (...args) { - const t = startTime(); - const result = fn(...args); - const tdiff = endTime(t); - - if (enabled) { - if (typeof data[key] === "undefined") { - data[key] = 0; - } - - data[key] += tdiff; - } - - return stats ? { result, tdiff } : result; - }; - } - - /** - * Returns a shallow copy of the collected timings data. - * @returns {Record} mapping of ruleId to total time in ms - */ - function getData() { - return { ...data }; - } - - /** - * Merges rule timing totals collected elsewhere into this process' totals. - * @param {Record} dataToMerge mapping of ruleId to total time in ms - * @returns {void} - */ - function mergeData(dataToMerge) { - for (const [key, value] of Object.entries(dataToMerge)) { - if (typeof data[key] === "undefined") { - data[key] = 0; - } - data[key] += value; - } - } - - /** - * Disables printing of timing data on process exit. - * Intended for worker threads or non-main contexts. - * @returns {void} - */ - function disableDisplay() { - displayEnabled = false; - } - - if (enabled) { - process.on("exit", () => { - if (displayEnabled && Object.keys(data).length > 0) { - display(data); - } - }); - } - - return { - time, - enabled, - getListSize, - getData, - mergeData, - disableDisplay, - }; -})(); +module.exports = (function() { + + const data = Object.create(null); + + /** + * Time the run + * @param {any} key key from the data object + * @param {Function} fn function to be called + * @returns {Function} function to be executed + * @private + */ + function time(key, fn) { + if (typeof data[key] === "undefined") { + data[key] = 0; + } + + return function(...args) { + let t = process.hrtime(); + const result = fn(...args); + + t = process.hrtime(t); + data[key] += t[0] * 1e3 + t[1] / 1e6; + return result; + }; + } + + if (enabled) { + process.on("exit", () => { + display(data); + }); + } + + return { + time, + enabled, + getListSize + }; + +}()); diff --git a/node_modules/eslint/lib/linter/vfile.js b/node_modules/eslint/lib/linter/vfile.js deleted file mode 100644 index 4bdbf9912..000000000 --- a/node_modules/eslint/lib/linter/vfile.js +++ /dev/null @@ -1,115 +0,0 @@ -/** - * @fileoverview Virtual file - * @author Nicholas C. Zakas - */ - -"use strict"; - -//----------------------------------------------------------------------------- -// Type Definitions -//----------------------------------------------------------------------------- - -/** @typedef {import("@eslint/core").File} File */ - -//------------------------------------------------------------------------------ -// Helpers -//------------------------------------------------------------------------------ - -/** - * Determines if a given value has a byte order mark (BOM). - * @param {string|Uint8Array} value The value to check. - * @returns {boolean} `true` if the value has a BOM, `false` otherwise. - */ -function hasUnicodeBOM(value) { - return typeof value === "string" - ? value.charCodeAt(0) === 0xfeff - : value[0] === 0xef && value[1] === 0xbb && value[2] === 0xbf; -} - -/** - * Strips Unicode BOM from the given value. - * @param {string|Uint8Array} value The value to remove the BOM from. - * @returns {string|Uint8Array} The stripped value. - */ -function stripUnicodeBOM(value) { - if (!hasUnicodeBOM(value)) { - return value; - } - - if (typeof value === "string") { - /* - * Check Unicode BOM. - * In JavaScript, string data is stored as UTF-16, so BOM is 0xFEFF. - * http://www.ecma-international.org/ecma-262/6.0/#sec-unicode-format-control-characters - */ - return value.slice(1); - } - - /* - * In a Uint8Array, the BOM is represented by three bytes: 0xEF, 0xBB, and 0xBF, - * so we can just remove the first three bytes. - */ - return value.slice(3); -} - -//------------------------------------------------------------------------------ -// Exports -//------------------------------------------------------------------------------ - -/** - * Represents a virtual file inside of ESLint. - * @implements {File} - */ -class VFile { - /** - * The file path including any processor-created virtual path. - * @type {string} - * @readonly - */ - path; - - /** - * The file path on disk. - * @type {string} - * @readonly - */ - physicalPath; - - /** - * The file contents. - * @type {string|Uint8Array} - * @readonly - */ - body; - - /** - * The raw body of the file, including a BOM if present. - * @type {string|Uint8Array} - * @readonly - */ - rawBody; - - /** - * Indicates whether the file has a byte order mark (BOM). - * @type {boolean} - * @readonly - */ - bom; - - /** - * Creates a new instance. - * @param {string} path The file path. - * @param {string|Uint8Array} body The file contents. - * @param {Object} [options] Additional options. - * @param {string} [options.physicalPath] The file path on disk. - */ - constructor(path, body, { physicalPath } = {}) { - this.path = path; - this.physicalPath = physicalPath ?? path; - this.bom = hasUnicodeBOM(body); - this.body = stripUnicodeBOM(body); - this.rawBody = body; - } -} - -module.exports = { VFile }; diff --git a/node_modules/eslint/lib/options.js b/node_modules/eslint/lib/options.js index 71365eb16..089f34743 100644 --- a/node_modules/eslint/lib/options.js +++ b/node_modules/eslint/lib/options.js @@ -23,7 +23,6 @@ const optionator = require("optionator"); * @property {string} [cacheLocation] Path to the cache file or directory * @property {"metadata" | "content"} cacheStrategy Strategy to use for detecting changed files in the cache * @property {boolean} [color] Force enabling/disabling of color - * @property {number | "auto" | "off"} [concurrency] Number of linting threads, "auto" to choose automatically, "off" for no multithreading * @property {string} [config] Use this configuration, overriding .eslintrc.* config options if present * @property {boolean} debug Output debugging information * @property {string[]} [env] Specify environments @@ -31,7 +30,6 @@ const optionator = require("optionator"); * @property {boolean} errorOnUnmatchedPattern Prevent errors when pattern is unmatched * @property {boolean} eslintrc Disable use of configuration from .eslintrc.* * @property {string[]} [ext] Specify JavaScript file extensions - * @property {string[]} [flag] Feature flags * @property {boolean} fix Automatically fix problems * @property {boolean} fixDryRun Automatically fix problems without saving the changes to the file system * @property {("directive" | "problem" | "suggestion" | "layout")[]} [fixType] Specify the types of fixes to apply (directive, problem, suggestion, layout) @@ -40,31 +38,23 @@ const optionator = require("optionator"); * @property {boolean} [help] Show help * @property {boolean} ignore Disable use of ignore files and patterns * @property {string} [ignorePath] Specify path of ignore file - * @property {string[]} [ignorePattern] Patterns of files to ignore. In eslintrc mode, these are in addition to `.eslintignore` + * @property {string[]} [ignorePattern] Pattern of files to ignore (in addition to those in .eslintignore) * @property {boolean} init Run config initialization wizard * @property {boolean} inlineConfig Prevent comments from changing config or rules * @property {number} maxWarnings Number of warnings to trigger nonzero exit code * @property {string} [outputFile] Specify file to write report to * @property {string} [parser] Specify the parser to be used * @property {Object} [parserOptions] Specify parser options - * @property {boolean} [passOnNoPatterns=false] When set to true, missing patterns cause - * the linting operation to short circuit and not report any failures. - * @property {boolean} [passOnUnprunedSuppressions] Ignore unused suppressions * @property {string[]} [plugin] Specify plugins * @property {string} [printConfig] Print the configuration for the given file - * @property {boolean} [pruneSuppressions] Prune unused suppressions - * @property {boolean} quiet Report errors only * @property {boolean | undefined} reportUnusedDisableDirectives Adds reported errors for unused eslint-disable and eslint-enable directives * @property {string | undefined} reportUnusedDisableDirectivesSeverity A severity string indicating if and how unused disable and enable directives should be tracked and reported. * @property {string} [resolvePluginsRelativeTo] A folder where plugins should be resolved from, CWD by default * @property {Object} [rule] Specify rules * @property {string[]} [rulesdir] Load additional rules from this directory. Deprecated: Use rules from plugins - * @property {boolean} [stats] Report additional statistics * @property {boolean} stdin Lint code provided on * @property {string} [stdinFilename] Specify filename to process STDIN as - * @property {boolean} [suppressAll] Suppress all error violations - * @property {string} [suppressionsLocation] Path to the suppressions file or directory - * @property {string[]} [suppressRule] Suppress specific rules + * @property {boolean} quiet Report errors only * @property {boolean} [version] Output the version number * @property {boolean} warnIgnored Show warnings when the file list includes ignored files * @property {string[]} _ Positional filenames or patterns @@ -81,456 +71,328 @@ const optionator = require("optionator"); * @param {boolean} usingFlatConfig Indicates if flat config is being used. * @returns {Object} The optionator instance. */ -module.exports = function (usingFlatConfig) { - let lookupFlag; - - if (usingFlatConfig) { - lookupFlag = { - option: "config-lookup", - type: "Boolean", - default: "true", - description: "Disable look up for eslint.config.js", - }; - } else { - lookupFlag = { - option: "eslintrc", - type: "Boolean", - default: "true", - description: "Disable use of configuration from .eslintrc.*", - }; - } - - let envFlag; - - if (!usingFlatConfig) { - envFlag = { - option: "env", - type: "[String]", - description: "Specify environments", - }; - } - - let inspectConfigFlag; - - if (usingFlatConfig) { - inspectConfigFlag = { - option: "inspect-config", - type: "Boolean", - description: - "Open the config inspector with the current configuration", - }; - } - - let extFlag; - - if (!usingFlatConfig) { - extFlag = { - option: "ext", - type: "[String]", - description: "Specify JavaScript file extensions", - }; - } else { - extFlag = { - option: "ext", - type: "[String]", - description: "Specify additional file extensions to lint", - }; - } - - let resolvePluginsFlag; - - if (!usingFlatConfig) { - resolvePluginsFlag = { - option: "resolve-plugins-relative-to", - type: "path::String", - description: - "A folder where plugins should be resolved from, CWD by default", - }; - } - - let rulesDirFlag; - - if (!usingFlatConfig) { - rulesDirFlag = { - option: "rulesdir", - type: "[path::String]", - description: - "Load additional rules from this directory. Deprecated: Use rules from plugins", - }; - } - - let ignorePathFlag; - - if (!usingFlatConfig) { - ignorePathFlag = { - option: "ignore-path", - type: "path::String", - description: "Specify path of ignore file", - }; - } - - let statsFlag; - - if (usingFlatConfig) { - statsFlag = { - option: "stats", - type: "Boolean", - default: "false", - description: "Add statistics to the lint report", - }; - } - - let warnIgnoredFlag; - - if (usingFlatConfig) { - warnIgnoredFlag = { - option: "warn-ignored", - type: "Boolean", - default: "true", - description: - "Suppress warnings when the file list includes ignored files", - }; - } - - let flagFlag; - - if (usingFlatConfig) { - flagFlag = { - option: "flag", - type: "[String]", - description: "Enable a feature flag", - }; - } - - let reportUnusedInlineConfigsFlag; - - if (usingFlatConfig) { - reportUnusedInlineConfigsFlag = { - option: "report-unused-inline-configs", - type: "String", - default: void 0, - description: - "Adds reported errors for unused eslint inline config comments", - enum: ["off", "warn", "error", "0", "1", "2"], - }; - } - - let mcpFlag; - - if (usingFlatConfig) { - mcpFlag = { - option: "mcp", - type: "Boolean", - description: "Start the ESLint MCP server", - }; - } - - let concurrencyFlag; - - if (usingFlatConfig) { - concurrencyFlag = { - option: "concurrency", - type: "Int|String", - default: "off", - description: - "Number of linting threads, auto to choose automatically, off for no multithreading", - }; - } - - return optionator({ - prepend: "eslint [options] file.js [file.js] [dir]", - defaults: { - concatRepeatedArrays: true, - mergeRepeatedObjects: true, - }, - options: [ - { - heading: "Basic configuration", - }, - lookupFlag, - { - option: "config", - alias: "c", - type: "path::String", - description: usingFlatConfig - ? "Use this configuration instead of eslint.config.js, eslint.config.mjs, or eslint.config.cjs" - : "Use this configuration, overriding .eslintrc.* config options if present", - }, - inspectConfigFlag, - envFlag, - extFlag, - { - option: "global", - type: "[String]", - description: "Define global variables", - }, - { - option: "parser", - type: "String", - description: "Specify the parser to be used", - }, - { - option: "parser-options", - type: "Object", - description: "Specify parser options", - }, - resolvePluginsFlag, - { - heading: "Specify Rules and Plugins", - }, - { - option: "plugin", - type: "[String]", - description: "Specify plugins", - }, - { - option: "rule", - type: "Object", - description: "Specify rules", - }, - rulesDirFlag, - { - heading: "Fix Problems", - }, - { - option: "fix", - type: "Boolean", - default: false, - description: "Automatically fix problems", - }, - { - option: "fix-dry-run", - type: "Boolean", - default: false, - description: - "Automatically fix problems without saving the changes to the file system", - }, - { - option: "fix-type", - type: "Array", - description: - "Specify the types of fixes to apply (directive, problem, suggestion, layout)", - }, - { - heading: "Ignore Files", - }, - ignorePathFlag, - { - option: "ignore", - type: "Boolean", - default: "true", - description: "Disable use of ignore files and patterns", - }, - { - option: "ignore-pattern", - type: "[String]", - description: `Patterns of files to ignore${usingFlatConfig ? "" : " (in addition to those in .eslintignore)"}`, - concatRepeatedArrays: [ - true, - { - oneValuePerFlag: true, - }, - ], - }, - { - heading: "Use stdin", - }, - { - option: "stdin", - type: "Boolean", - default: "false", - description: "Lint code provided on ", - }, - { - option: "stdin-filename", - type: "String", - description: "Specify filename to process STDIN as", - }, - { - heading: "Handle Warnings", - }, - { - option: "quiet", - type: "Boolean", - default: "false", - description: "Report errors only", - }, - { - option: "max-warnings", - type: "Int", - default: "-1", - description: "Number of warnings to trigger nonzero exit code", - }, - { - heading: "Output", - }, - { - option: "output-file", - alias: "o", - type: "path::String", - description: "Specify file to write report to", - }, - { - option: "format", - alias: "f", - type: "String", - default: "stylish", - description: "Use a specific output format", - }, - { - option: "color", - type: "Boolean", - alias: "no-color", - description: "Force enabling/disabling of color", - }, - { - heading: "Inline configuration comments", - }, - { - option: "inline-config", - type: "Boolean", - default: "true", - description: "Prevent comments from changing config or rules", - }, - { - option: "report-unused-disable-directives", - type: "Boolean", - default: void 0, - description: - "Adds reported errors for unused eslint-disable and eslint-enable directives", - }, - { - option: "report-unused-disable-directives-severity", - type: "String", - default: void 0, - description: - "Chooses severity level for reporting unused eslint-disable and eslint-enable directives", - enum: ["off", "warn", "error", "0", "1", "2"], - }, - reportUnusedInlineConfigsFlag, - { - heading: "Caching", - }, - { - option: "cache", - type: "Boolean", - default: "false", - description: "Only check changed files", - }, - { - option: "cache-file", - type: "path::String", - default: ".eslintcache", - description: - "Path to the cache file. Deprecated: use --cache-location", - }, - { - option: "cache-location", - type: "path::String", - description: "Path to the cache file or directory", - }, - { - option: "cache-strategy", - dependsOn: ["cache"], - type: "String", - default: "metadata", - enum: ["metadata", "content"], - description: - "Strategy to use for detecting changed files in the cache", - }, - { - heading: "Suppressing Violations", - }, - { - option: "suppress-all", - type: "Boolean", - default: "false", - description: "Suppress all violations", - }, - { - option: "suppress-rule", - type: "[String]", - description: "Suppress specific rules", - }, - { - option: "suppressions-location", - type: "path::String", - description: "Specify the location of the suppressions file", - }, - { - option: "prune-suppressions", - type: "Boolean", - default: "false", - description: "Prune unused suppressions", - }, - { - option: "pass-on-unpruned-suppressions", - type: "Boolean", - default: "false", - description: "Ignore unused suppressions", - }, - { - heading: "Miscellaneous", - }, - { - option: "init", - type: "Boolean", - default: "false", - description: "Run config initialization wizard", - }, - { - option: "env-info", - type: "Boolean", - default: "false", - description: "Output execution environment information", - }, - { - option: "error-on-unmatched-pattern", - type: "Boolean", - default: "true", - description: "Prevent errors when pattern is unmatched", - }, - { - option: "exit-on-fatal-error", - type: "Boolean", - default: "false", - description: "Exit with exit code 2 in case of fatal error", - }, - warnIgnoredFlag, - { - option: "pass-on-no-patterns", - type: "Boolean", - default: false, - description: - "Exit with exit code 0 in case no file patterns are passed", - }, - { - option: "debug", - type: "Boolean", - default: false, - description: "Output debugging information", - }, - { - option: "help", - alias: "h", - type: "Boolean", - description: "Show help", - }, - { - option: "version", - alias: "v", - type: "Boolean", - description: "Output the version number", - }, - { - option: "print-config", - type: "path::String", - description: "Print the configuration for the given file", - }, - statsFlag, - flagFlag, - mcpFlag, - concurrencyFlag, - ].filter(value => !!value), - }); +module.exports = function(usingFlatConfig) { + + let lookupFlag; + + if (usingFlatConfig) { + lookupFlag = { + option: "config-lookup", + type: "Boolean", + default: "true", + description: "Disable look up for eslint.config.js" + }; + } else { + lookupFlag = { + option: "eslintrc", + type: "Boolean", + default: "true", + description: "Disable use of configuration from .eslintrc.*" + }; + } + + let envFlag; + + if (!usingFlatConfig) { + envFlag = { + option: "env", + type: "[String]", + description: "Specify environments" + }; + } + + let extFlag; + + if (!usingFlatConfig) { + extFlag = { + option: "ext", + type: "[String]", + description: "Specify JavaScript file extensions" + }; + } + + let resolvePluginsFlag; + + if (!usingFlatConfig) { + resolvePluginsFlag = { + option: "resolve-plugins-relative-to", + type: "path::String", + description: "A folder where plugins should be resolved from, CWD by default" + }; + } + + let rulesDirFlag; + + if (!usingFlatConfig) { + rulesDirFlag = { + option: "rulesdir", + type: "[path::String]", + description: "Load additional rules from this directory. Deprecated: Use rules from plugins" + }; + } + + let ignorePathFlag; + + if (!usingFlatConfig) { + ignorePathFlag = { + option: "ignore-path", + type: "path::String", + description: "Specify path of ignore file" + }; + } + + let warnIgnoredFlag; + + if (usingFlatConfig) { + warnIgnoredFlag = { + option: "warn-ignored", + type: "Boolean", + default: "true", + description: "Suppress warnings when the file list includes ignored files" + }; + } + + return optionator({ + prepend: "eslint [options] file.js [file.js] [dir]", + defaults: { + concatRepeatedArrays: true, + mergeRepeatedObjects: true + }, + options: [ + { + heading: "Basic configuration" + }, + lookupFlag, + { + option: "config", + alias: "c", + type: "path::String", + description: usingFlatConfig + ? "Use this configuration instead of eslint.config.js, eslint.config.mjs, or eslint.config.cjs" + : "Use this configuration, overriding .eslintrc.* config options if present" + }, + envFlag, + extFlag, + { + option: "global", + type: "[String]", + description: "Define global variables" + }, + { + option: "parser", + type: "String", + description: "Specify the parser to be used" + }, + { + option: "parser-options", + type: "Object", + description: "Specify parser options" + }, + resolvePluginsFlag, + { + heading: "Specify Rules and Plugins" + }, + { + option: "plugin", + type: "[String]", + description: "Specify plugins" + }, + { + option: "rule", + type: "Object", + description: "Specify rules" + }, + rulesDirFlag, + { + heading: "Fix Problems" + }, + { + option: "fix", + type: "Boolean", + default: false, + description: "Automatically fix problems" + }, + { + option: "fix-dry-run", + type: "Boolean", + default: false, + description: "Automatically fix problems without saving the changes to the file system" + }, + { + option: "fix-type", + type: "Array", + description: "Specify the types of fixes to apply (directive, problem, suggestion, layout)" + }, + { + heading: "Ignore Files" + }, + ignorePathFlag, + { + option: "ignore", + type: "Boolean", + default: "true", + description: "Disable use of ignore files and patterns" + }, + { + option: "ignore-pattern", + type: "[String]", + description: "Pattern of files to ignore (in addition to those in .eslintignore)", + concatRepeatedArrays: [true, { + oneValuePerFlag: true + }] + }, + { + heading: "Use stdin" + }, + { + option: "stdin", + type: "Boolean", + default: "false", + description: "Lint code provided on " + }, + { + option: "stdin-filename", + type: "String", + description: "Specify filename to process STDIN as" + }, + { + heading: "Handle Warnings" + }, + { + option: "quiet", + type: "Boolean", + default: "false", + description: "Report errors only" + }, + { + option: "max-warnings", + type: "Int", + default: "-1", + description: "Number of warnings to trigger nonzero exit code" + }, + { + heading: "Output" + }, + { + option: "output-file", + alias: "o", + type: "path::String", + description: "Specify file to write report to" + }, + { + option: "format", + alias: "f", + type: "String", + default: "stylish", + description: "Use a specific output format" + }, + { + option: "color", + type: "Boolean", + alias: "no-color", + description: "Force enabling/disabling of color" + }, + { + heading: "Inline configuration comments" + }, + { + option: "inline-config", + type: "Boolean", + default: "true", + description: "Prevent comments from changing config or rules" + }, + { + option: "report-unused-disable-directives", + type: "Boolean", + default: void 0, + description: "Adds reported errors for unused eslint-disable and eslint-enable directives" + }, + { + option: "report-unused-disable-directives-severity", + type: "String", + default: void 0, + description: "Chooses severity level for reporting unused eslint-disable and eslint-enable directives", + enum: ["off", "warn", "error", "0", "1", "2"] + }, + { + heading: "Caching" + }, + { + option: "cache", + type: "Boolean", + default: "false", + description: "Only check changed files" + }, + { + option: "cache-file", + type: "path::String", + default: ".eslintcache", + description: "Path to the cache file. Deprecated: use --cache-location" + }, + { + option: "cache-location", + type: "path::String", + description: "Path to the cache file or directory" + }, + { + option: "cache-strategy", + dependsOn: ["cache"], + type: "String", + default: "metadata", + enum: ["metadata", "content"], + description: "Strategy to use for detecting changed files in the cache" + }, + { + heading: "Miscellaneous" + }, + { + option: "init", + type: "Boolean", + default: "false", + description: "Run config initialization wizard" + }, + { + option: "env-info", + type: "Boolean", + default: "false", + description: "Output execution environment information" + }, + { + option: "error-on-unmatched-pattern", + type: "Boolean", + default: "true", + description: "Prevent errors when pattern is unmatched" + }, + { + option: "exit-on-fatal-error", + type: "Boolean", + default: "false", + description: "Exit with exit code 2 in case of fatal error" + }, + warnIgnoredFlag, + { + option: "debug", + type: "Boolean", + default: false, + description: "Output debugging information" + }, + { + option: "help", + alias: "h", + type: "Boolean", + description: "Show help" + }, + { + option: "version", + alias: "v", + type: "Boolean", + description: "Output the version number" + }, + { + option: "print-config", + type: "path::String", + description: "Print the configuration for the given file" + } + ].filter(value => !!value) + }); }; diff --git a/node_modules/eslint/lib/rule-tester/index.js b/node_modules/eslint/lib/rule-tester/index.js index 4fa651eb8..f52d14027 100644 --- a/node_modules/eslint/lib/rule-tester/index.js +++ b/node_modules/eslint/lib/rule-tester/index.js @@ -1,7 +1,5 @@ "use strict"; -const RuleTester = require("./rule-tester"); - module.exports = { - RuleTester, + RuleTester: require("./rule-tester") }; diff --git a/node_modules/eslint/lib/rule-tester/rule-tester.js b/node_modules/eslint/lib/rule-tester/rule-tester.js index 0085cd6bc..3bc80ab18 100644 --- a/node_modules/eslint/lib/rule-tester/rule-tester.js +++ b/node_modules/eslint/lib/rule-tester/rule-tester.js @@ -1,47 +1,70 @@ /** - * @fileoverview Mocha/Jest test wrapper + * @fileoverview Mocha test wrapper * @author Ilya Volodin */ "use strict"; /* globals describe, it -- Mocha globals */ +/* + * This is a wrapper around mocha to allow for DRY unittests for eslint + * Format: + * RuleTester.run("{ruleName}", { + * valid: [ + * "{code}", + * { code: "{code}", options: {options}, globals: {globals}, parser: "{parser}", settings: {settings} } + * ], + * invalid: [ + * { code: "{code}", errors: {numErrors} }, + * { code: "{code}", errors: ["{errorMessage}"] }, + * { code: "{code}", options: {options}, globals: {globals}, parser: "{parser}", settings: {settings}, errors: [{ message: "{errorMessage}", type: "{errorNodeType}"}] } + * ] + * }); + * + * Variables: + * {code} - String that represents the code to be tested + * {options} - Arguments that are passed to the configurable rules. + * {globals} - An object representing a list of variables that are + * registered as globals + * {parser} - String representing the parser to use + * {settings} - An object representing global settings for all rules + * {numErrors} - If failing case doesn't need to check error message, + * this integer will specify how many errors should be + * received + * {errorMessage} - Message that is returned by the rule on failure + * {errorNodeType} - AST node type that is returned by they rule as + * a cause of the failure. + */ + //------------------------------------------------------------------------------ // Requirements //------------------------------------------------------------------------------ -const assert = require("node:assert"), - util = require("node:util"), - path = require("node:path"), - equal = require("fast-deep-equal"), - Traverser = require("../shared/traverser"), - { Config } = require("../config/config"), - { Linter, SourceCodeFixer } = require("../linter"), - { interpolate, getPlaceholderMatcher } = require("../linter/interpolate"), - stringify = require("json-stable-stringify-without-jsonify"); - -const { FlatConfigArray } = require("../config/flat-config-array"); -const { - defaultConfig, - defaultRuleTesterConfig, -} = require("../config/default-config"); +const + assert = require("assert"), + path = require("path"), + util = require("util"), + merge = require("lodash.merge"), + equal = require("fast-deep-equal"), + Traverser = require("../../lib/shared/traverser"), + { getRuleOptionsSchema, validate } = require("../shared/config-validator"), + { Linter, SourceCodeFixer, interpolate } = require("../linter"), + CodePath = require("../linter/code-path-analysis/code-path"); const ajv = require("../shared/ajv")({ strictDefaults: true }); +const espreePath = require.resolve("espree"); const parserSymbol = Symbol.for("eslint.RuleTester.parser"); -const { ConfigArraySymbol } = require("@eslint/config-array"); -const { isSerializable } = require("../shared/serialization"); -const jslang = require("../languages/js"); -const { SourceCode } = require("../languages/js/source-code"); +const { SourceCode } = require("../source-code"); //------------------------------------------------------------------------------ // Typedefs //------------------------------------------------------------------------------ -/** @import { LanguageOptions, RuleDefinition } from "@eslint/core" */ +/** @typedef {import("../shared/types").Parser} Parser */ +/** @typedef {import("../shared/types").Rule} Rule */ -/** @typedef {import("../types").Linter.Parser} Parser */ /** * A test case that is expected to pass lint. @@ -49,11 +72,12 @@ const { SourceCode } = require("../languages/js/source-code"); * @property {string} [name] Name for the test case. * @property {string} code Code for the test case. * @property {any[]} [options] Options for the test case. - * @property {Function} [before] Function to execute before testing the case. - * @property {Function} [after] Function to execute after testing the case regardless of its result. - * @property {LanguageOptions} [languageOptions] The language options to use in the test case. * @property {{ [name: string]: any }} [settings] Settings for the test case. * @property {string} [filename] The fake filename for the test case. Useful for rules that make assertion about filenames. + * @property {string} [parser] The absolute path for the parser. + * @property {{ [name: string]: any }} [parserOptions] Options for the parser. + * @property {{ [name: string]: "readonly" | "writable" | "off" }} [globals] The additional global variables. + * @property {{ [name: string]: boolean }} [env] Environments for the test case. * @property {boolean} [only] Run only this test case or the subset of test cases with this property. */ @@ -65,11 +89,12 @@ const { SourceCode } = require("../languages/js/source-code"); * @property {number | Array} errors Expected errors. * @property {string | null} [output] The expected code after autofixes are applied. If set to `null`, the test runner will assert that no autofix is suggested. * @property {any[]} [options] Options for the test case. - * @property {Function} [before] Function to execute before testing the case. - * @property {Function} [after] Function to execute after testing the case regardless of its result. * @property {{ [name: string]: any }} [settings] Settings for the test case. * @property {string} [filename] The fake filename for the test case. Useful for rules that make assertion about filenames. - * @property {LanguageOptions} [languageOptions] The language options to use in the test case. + * @property {string} [parser] The absolute path for the parser. + * @property {{ [name: string]: any }} [parserOptions] Options for the parser. + * @property {{ [name: string]: "readonly" | "writable" | "off" }} [globals] The additional global variables. + * @property {{ [name: string]: boolean }} [env] Environments for the test case. * @property {boolean} [only] Run only this test case or the subset of test cases with this property. */ @@ -95,42 +120,35 @@ const { SourceCode } = require("../languages/js/source-code"); * the initial default configuration */ const testerDefaultConfig = { rules: {} }; - -/* - * RuleTester uses this config as its default. This can be overwritten via - * setDefaultConfig(). - */ -let sharedDefaultConfig = { rules: {} }; +let defaultConfig = { rules: {} }; /* * List every parameters possible on a test case that are not related to eslint * configuration */ const RuleTesterParameters = [ - "name", - "code", - "filename", - "options", - "before", - "after", - "errors", - "output", - "only", + "name", + "code", + "filename", + "options", + "errors", + "output", + "only" ]; /* * All allowed property names in error objects. */ const errorObjectParameters = new Set([ - "message", - "messageId", - "data", - "type", - "line", - "column", - "endLine", - "endColumn", - "suggestions", + "message", + "messageId", + "data", + "type", + "line", + "column", + "endLine", + "endColumn", + "suggestions" ]); const friendlyErrorObjectParameterList = `[${[...errorObjectParameters].map(key => `'${key}'`).join(", ")}]`; @@ -138,31 +156,50 @@ const friendlyErrorObjectParameterList = `[${[...errorObjectParameters].map(key * All allowed property names in suggestion objects. */ const suggestionObjectParameters = new Set([ - "desc", - "messageId", - "data", - "output", + "desc", + "messageId", + "data", + "output" ]); const friendlySuggestionObjectParameterList = `[${[...suggestionObjectParameters].map(key => `'${key}'`).join(", ")}]`; -/* - * Ignored test case properties when checking for test case duplicates. - */ -const duplicationIgnoredParameters = new Set(["name", "errors", "output"]); - const forbiddenMethods = [ - "applyInlineConfig", - "applyLanguageOptions", - "finalize", + "applyInlineConfig", + "applyLanguageOptions", + "finalize" ]; -/** @type {Map} */ -const forbiddenMethodCalls = new Map( - forbiddenMethods.map(methodName => [methodName, new WeakSet()]), -); - const hasOwnProperty = Function.call.bind(Object.hasOwnProperty); +const DEPRECATED_SOURCECODE_PASSTHROUGHS = { + getSource: "getText", + getSourceLines: "getLines", + getAllComments: "getAllComments", + getNodeByRangeIndex: "getNodeByRangeIndex", + + // getComments: "getComments", -- already handled by a separate error + getCommentsBefore: "getCommentsBefore", + getCommentsAfter: "getCommentsAfter", + getCommentsInside: "getCommentsInside", + getJSDocComment: "getJSDocComment", + getFirstToken: "getFirstToken", + getFirstTokens: "getFirstTokens", + getLastToken: "getLastToken", + getLastTokens: "getLastTokens", + getTokenAfter: "getTokenAfter", + getTokenBefore: "getTokenBefore", + getTokenByRangeStart: "getTokenByRangeStart", + getTokens: "getTokens", + getTokensAfter: "getTokensAfter", + getTokensBefore: "getTokensBefore", + getTokensBetween: "getTokensBetween", + + getScope: "getScope", + getAncestors: "getAncestors", + getDeclaredVariables: "getDeclaredVariables", + markVariableAsUsed: "markVariableAsUsed" +}; + /** * Clones a given value deeply. * Note: This ignores `parent` property. @@ -170,51 +207,43 @@ const hasOwnProperty = Function.call.bind(Object.hasOwnProperty); * @returns {any} A cloned value. */ function cloneDeeplyExcludesParent(x) { - if (typeof x === "object" && x !== null) { - if (Array.isArray(x)) { - return x.map(cloneDeeplyExcludesParent); - } + if (typeof x === "object" && x !== null) { + if (Array.isArray(x)) { + return x.map(cloneDeeplyExcludesParent); + } - const retv = {}; + const retv = {}; - for (const key in x) { - if (key !== "parent" && hasOwnProperty(x, key)) { - retv[key] = cloneDeeplyExcludesParent(x[key]); - } - } + for (const key in x) { + if (key !== "parent" && hasOwnProperty(x, key)) { + retv[key] = cloneDeeplyExcludesParent(x[key]); + } + } - return retv; - } + return retv; + } - return x; + return x; } /** * Freezes a given value deeply. * @param {any} x A value to freeze. - * @param {Set} seenObjects Objects already seen during the traversal. * @returns {void} */ -function freezeDeeply(x, seenObjects = new Set()) { - if (typeof x === "object" && x !== null) { - if (seenObjects.has(x)) { - return; // skip to avoid infinite recursion - } - seenObjects.add(x); - - if (Array.isArray(x)) { - x.forEach(element => { - freezeDeeply(element, seenObjects); - }); - } else { - for (const key in x) { - if (key !== "parent" && hasOwnProperty(x, key)) { - freezeDeeply(x[key], seenObjects); - } - } - } - Object.freeze(x); - } +function freezeDeeply(x) { + if (typeof x === "object" && x !== null) { + if (Array.isArray(x)) { + x.forEach(freezeDeeply); + } else { + for (const key in x) { + if (key !== "parent" && hasOwnProperty(x, key)) { + freezeDeeply(x[key]); + } + } + } + Object.freeze(x); + } } /** @@ -223,13 +252,13 @@ function freezeDeeply(x, seenObjects = new Set()) { * @returns {string} The sanitized text. */ function sanitize(text) { - if (typeof text !== "string") { - return ""; - } - return text.replace( - /[\u0000-\u0009\u000b-\u001a]/gu, // eslint-disable-line no-control-regex -- Escaping controls - c => `\\u${c.codePointAt(0).toString(16).padStart(4, "0")}`, - ); + if (typeof text !== "string") { + return ""; + } + return text.replace( + /[\u0000-\u0009\u000b-\u001a]/gu, // eslint-disable-line no-control-regex -- Escaping controls + c => `\\u${c.codePointAt(0).toString(16).padStart(4, "0")}` + ); } /** @@ -239,28 +268,25 @@ function sanitize(text) { * @returns {void} */ function defineStartEndAsError(objName, node) { - Object.defineProperties(node, { - start: { - get() { - throw new Error( - `Use ${objName}.range[0] instead of ${objName}.start`, - ); - }, - configurable: true, - enumerable: false, - }, - end: { - get() { - throw new Error( - `Use ${objName}.range[1] instead of ${objName}.end`, - ); - }, - configurable: true, - enumerable: false, - }, - }); + Object.defineProperties(node, { + start: { + get() { + throw new Error(`Use ${objName}.range[0] instead of ${objName}.start`); + }, + configurable: true, + enumerable: false + }, + end: { + get() { + throw new Error(`Use ${objName}.range[1] instead of ${objName}.end`); + }, + configurable: true, + enumerable: false + } + }); } + /** * Define `start`/`end` properties of all nodes of the given AST as throwing error. * @param {ASTNode} ast The root node to errorize `start`/`end` properties. @@ -268,12 +294,9 @@ function defineStartEndAsError(objName, node) { * @returns {void} */ function defineStartEndAsErrorInTree(ast, visitorKeys) { - Traverser.traverse(ast, { - visitorKeys, - enter: defineStartEndAsError.bind(null, "node"), - }); - ast.tokens.forEach(defineStartEndAsError.bind(null, "token")); - ast.comments.forEach(defineStartEndAsError.bind(null, "token")); + Traverser.traverse(ast, { visitorKeys, enter: defineStartEndAsError.bind(null, "node") }); + ast.tokens.forEach(defineStartEndAsError.bind(null, "token")); + ast.comments.forEach(defineStartEndAsError.bind(null, "token")); } /** @@ -283,97 +306,130 @@ function defineStartEndAsErrorInTree(ast, visitorKeys) { * @returns {Parser} Wrapped parser object. */ function wrapParser(parser) { - if (typeof parser.parseForESLint === "function") { - return { - [parserSymbol]: parser, - parseForESLint(...args) { - const ret = parser.parseForESLint(...args); - - defineStartEndAsErrorInTree(ret.ast, ret.visitorKeys); - return ret; - }, - }; - } - - return { - [parserSymbol]: parser, - parse(...args) { - const ast = parser.parse(...args); - - defineStartEndAsErrorInTree(ast); - return ast; - }, - }; + + if (typeof parser.parseForESLint === "function") { + return { + [parserSymbol]: parser, + parseForESLint(...args) { + const ret = parser.parseForESLint(...args); + + defineStartEndAsErrorInTree(ret.ast, ret.visitorKeys); + return ret; + } + }; + } + + return { + [parserSymbol]: parser, + parse(...args) { + const ast = parser.parse(...args); + + defineStartEndAsErrorInTree(ast); + return ast; + } + }; +} + +/** + * Function to replace `SourceCode.prototype.getComments`. + * @returns {void} + * @throws {Error} Deprecation message. + */ +function getCommentsDeprecation() { + throw new Error( + "`SourceCode#getComments()` is deprecated and will be removed in a future major version. Use `getCommentsBefore()`, `getCommentsAfter()`, and `getCommentsInside()` instead." + ); } /** - * Function to replace forbidden `SourceCode` methods. Allows just one call per method. + * Function to replace forbidden `SourceCode` methods. * @param {string} methodName The name of the method to forbid. - * @param {Function} prototype The prototype with the original method to call. * @returns {Function} The function that throws the error. */ -function throwForbiddenMethodError(methodName, prototype) { - const original = prototype[methodName]; - - return function (...args) { - const called = forbiddenMethodCalls.get(methodName); - - /* eslint-disable no-invalid-this -- needed to operate as a method. */ - if (!called.has(this)) { - called.add(this); - - return original.apply(this, args); - } - /* eslint-enable no-invalid-this -- not needed past this point */ - - throw new Error( - `\`SourceCode#${methodName}()\` cannot be called inside a rule.`, - ); - }; +function throwForbiddenMethodError(methodName) { + return () => { + throw new Error( + `\`SourceCode#${methodName}()\` cannot be called inside a rule.` + ); + }; } /** - * Extracts names of {{ placeholders }} from the reported message. - * @param {string} message Reported message - * @returns {string[]} Array of placeholder names + * Emit a deprecation warning if function-style format is being used. + * @param {string} ruleName Name of the rule. + * @returns {void} */ -function getMessagePlaceholders(message) { - const matcher = getPlaceholderMatcher(); - - return Array.from(message.matchAll(matcher), ([, name]) => name.trim()); +function emitLegacyRuleAPIWarning(ruleName) { + if (!emitLegacyRuleAPIWarning[`warned-${ruleName}`]) { + emitLegacyRuleAPIWarning[`warned-${ruleName}`] = true; + process.emitWarning( + `"${ruleName}" rule is using the deprecated function-style format and will stop working in ESLint v9. Please use object-style format: https://eslint.org/docs/latest/extend/custom-rules`, + "DeprecationWarning" + ); + } } /** - * Returns the placeholders in the reported messages but - * only includes the placeholders available in the raw message and not in the provided data. - * @param {string} message The reported message - * @param {string} raw The raw message specified in the rule meta.messages - * @param {undefined|Record} data The passed - * @returns {string[]} Missing placeholder names + * Emit a deprecation warning if rule has options but is missing the "meta.schema" property + * @param {string} ruleName Name of the rule. + * @returns {void} */ -function getUnsubstitutedMessagePlaceholders(message, raw, data = {}) { - const unsubstituted = getMessagePlaceholders(message); - - if (unsubstituted.length === 0) { - return []; - } +function emitMissingSchemaWarning(ruleName) { + if (!emitMissingSchemaWarning[`warned-${ruleName}`]) { + emitMissingSchemaWarning[`warned-${ruleName}`] = true; + process.emitWarning( + `"${ruleName}" rule has options but is missing the "meta.schema" property and will stop working in ESLint v9. Please add a schema: https://eslint.org/docs/latest/extend/custom-rules#options-schemas`, + "DeprecationWarning" + ); + } +} - // Remove false positives by only counting placeholders in the raw message, which were not provided in the data matcher or added with a data property - const known = getMessagePlaceholders(raw); - const provided = Object.keys(data); +/** + * Emit a deprecation warning if a rule uses a deprecated `context` method. + * @param {string} ruleName Name of the rule. + * @param {string} methodName The name of the method on `context` that was used. + * @returns {void} + */ +function emitDeprecatedContextMethodWarning(ruleName, methodName) { + if (!emitDeprecatedContextMethodWarning[`warned-${ruleName}-${methodName}`]) { + emitDeprecatedContextMethodWarning[`warned-${ruleName}-${methodName}`] = true; + process.emitWarning( + `"${ruleName}" rule is using \`context.${methodName}()\`, which is deprecated and will be removed in ESLint v9. Please use \`sourceCode.${DEPRECATED_SOURCECODE_PASSTHROUGHS[methodName]}()\` instead.`, + "DeprecationWarning" + ); + } +} - return unsubstituted.filter( - name => known.includes(name) && !provided.includes(name), - ); +/** + * Emit a deprecation warning if rule uses CodePath#currentSegments. + * @param {string} ruleName Name of the rule. + * @returns {void} + */ +function emitCodePathCurrentSegmentsWarning(ruleName) { + if (!emitCodePathCurrentSegmentsWarning[`warned-${ruleName}`]) { + emitCodePathCurrentSegmentsWarning[`warned-${ruleName}`] = true; + process.emitWarning( + `"${ruleName}" rule uses CodePath#currentSegments and will stop working in ESLint v9. Please read the documentation for how to update your code: https://eslint.org/docs/latest/extend/code-path-analysis#usage-examples`, + "DeprecationWarning" + ); + } } -const metaSchemaDescription = ` -\t- If the rule has options, set \`meta.schema\` to an array or non-empty object to enable options validation. -\t- If the rule doesn't have options, omit \`meta.schema\` to enforce that no options can be passed to the rule. -\t- You can also set \`meta.schema\` to \`false\` to opt-out of options validation (not recommended). +/** + * Emit a deprecation warning if `context.parserServices` is used. + * @param {string} ruleName Name of the rule. + * @returns {void} + */ +function emitParserServicesWarning(ruleName) { + if (!emitParserServicesWarning[`warned-${ruleName}`]) { + emitParserServicesWarning[`warned-${ruleName}`] = true; + process.emitWarning( + `"${ruleName}" rule is using \`context.parserServices\`, which is deprecated and will be removed in ESLint v9. Please use \`sourceCode.parserServices\` instead.`, + "DeprecationWarning" + ); + } +} -\thttps://eslint.org/docs/latest/extend/custom-rules#options-schemas -`; //------------------------------------------------------------------------------ // Public Interface @@ -393,14 +449,14 @@ const IT_ONLY = Symbol("itOnly"); * @returns {any} Returned value of `method`. */ function itDefaultHandler(text, method) { - try { - return method.call(this); - } catch (err) { - if (err instanceof assert.AssertionError) { - err.message += ` (${util.inspect(err.actual)} ${err.operator} ${util.inspect(err.expected)})`; - } - throw err; - } + try { + return method.call(this); + } catch (err) { + if (err instanceof assert.AssertionError) { + err.message += ` (${util.inspect(err.actual)} ${err.operator} ${util.inspect(err.expected)})`; + } + throw err; + } } /** @@ -411,1167 +467,738 @@ function itDefaultHandler(text, method) { * @returns {any} Returned value of `method`. */ function describeDefaultHandler(text, method) { - return method.call(this); + return method.call(this); } /** * Mocha test wrapper. */ class RuleTester { - /** - * Creates a new instance of RuleTester. - * @param {Object} [testerConfig] Optional, extra configuration for the tester - */ - constructor(testerConfig = {}) { - /** - * The configuration to use for this tester. Combination of the tester - * configuration and the default configuration. - * @type {Object} - */ - this.testerConfig = [ - sharedDefaultConfig, - testerConfig, - { rules: { "rule-tester/validate-ast": "error" } }, - ]; - - this.linter = new Linter({ configType: "flat" }); - } - - /** - * Set the configuration to use for all future tests - * @param {Object} config the configuration to use. - * @throws {TypeError} If non-object config. - * @returns {void} - */ - static setDefaultConfig(config) { - if (typeof config !== "object" || config === null) { - throw new TypeError( - "RuleTester.setDefaultConfig: config must be an object", - ); - } - sharedDefaultConfig = config; - - // Make sure the rules object exists since it is assumed to exist later - sharedDefaultConfig.rules = sharedDefaultConfig.rules || {}; - } - - /** - * Get the current configuration used for all tests - * @returns {Object} the current configuration - */ - static getDefaultConfig() { - return sharedDefaultConfig; - } - - /** - * Reset the configuration to the initial configuration of the tester removing - * any changes made until now. - * @returns {void} - */ - static resetDefaultConfig() { - sharedDefaultConfig = { - rules: { - ...testerDefaultConfig.rules, - }, - }; - } - - /* - * If people use `mocha test.js --watch` command, `describe` and `it` function - * instances are different for each execution. So `describe` and `it` should get fresh instance - * always. - */ - static get describe() { - return ( - this[DESCRIBE] || - (typeof describe === "function" ? describe : describeDefaultHandler) - ); - } - - static set describe(value) { - this[DESCRIBE] = value; - } - - static get it() { - return this[IT] || (typeof it === "function" ? it : itDefaultHandler); - } - - static set it(value) { - this[IT] = value; - } - - /** - * Adds the `only` property to a test to run it in isolation. - * @param {string | ValidTestCase | InvalidTestCase} item A single test to run by itself. - * @returns {ValidTestCase | InvalidTestCase} The test with `only` set. - */ - static only(item) { - if (typeof item === "string") { - return { code: item, only: true }; - } - - return { ...item, only: true }; - } - - static get itOnly() { - if (typeof this[IT_ONLY] === "function") { - return this[IT_ONLY]; - } - if ( - typeof this[IT] === "function" && - typeof this[IT].only === "function" - ) { - return Function.bind.call(this[IT].only, this[IT]); - } - if (typeof it === "function" && typeof it.only === "function") { - return Function.bind.call(it.only, it); - } - - if ( - typeof this[DESCRIBE] === "function" || - typeof this[IT] === "function" - ) { - throw new Error( - "Set `RuleTester.itOnly` to use `only` with a custom test framework.\n" + - "See https://eslint.org/docs/latest/integrate/nodejs-api#customizing-ruletester for more.", - ); - } - if (typeof it === "function") { - throw new Error( - "The current test framework does not support exclusive tests with `only`.", - ); - } - throw new Error( - "To use `only`, use RuleTester with a test framework that provides `it.only()` like Mocha.", - ); - } - - static set itOnly(value) { - this[IT_ONLY] = value; - } - - /** - * Adds a new rule test to execute. - * @param {string} ruleName The name of the rule to run. - * @param {RuleDefinition} rule The rule to test. - * @param {{ - * valid: (ValidTestCase | string)[], - * invalid: InvalidTestCase[] - * }} test The collection of tests to run. - * @throws {TypeError|Error} If `rule` is not an object with a `create` method, - * or if non-object `test`, or if a required scenario of the given type is missing. - * @returns {void} - */ - run(ruleName, rule, test) { - const testerConfig = this.testerConfig, - requiredScenarios = ["valid", "invalid"], - scenarioErrors = [], - linter = this.linter, - ruleId = `rule-to-test/${ruleName}`; - - const seenValidTestCases = new Set(); - const seenInvalidTestCases = new Set(); - - if ( - !rule || - typeof rule !== "object" || - typeof rule.create !== "function" - ) { - throw new TypeError( - "Rule must be an object with a `create` method", - ); - } - - if (!test || typeof test !== "object") { - throw new TypeError( - `Test Scenarios for rule ${ruleName} : Could not find test scenario object`, - ); - } - - requiredScenarios.forEach(scenarioType => { - if (!test[scenarioType]) { - scenarioErrors.push( - `Could not find any ${scenarioType} test scenarios`, - ); - } - }); - - if (scenarioErrors.length > 0) { - throw new Error( - [`Test Scenarios for rule ${ruleName} is invalid:`] - .concat(scenarioErrors) - .join("\n"), - ); - } - - const baseConfig = [ - { - plugins: { - // copy root plugin over - "@": { - /* - * Parsers are wrapped to detect more errors, so this needs - * to be a new object for each call to run(), otherwise the - * parsers will be wrapped multiple times. - */ - parsers: { - ...defaultConfig[0].plugins["@"].parsers, - }, - - /* - * The rules key on the default plugin is a proxy to lazy-load - * just the rules that are needed. So, don't create a new object - * here, just use the default one to keep that performance - * enhancement. - */ - rules: defaultConfig[0].plugins["@"].rules, - languages: defaultConfig[0].plugins["@"].languages, - }, - "rule-to-test": { - rules: { - [ruleName]: Object.assign({}, rule, { - // Create a wrapper rule that freezes the `context` properties. - create(context) { - freezeDeeply(context.options); - freezeDeeply(context.settings); - freezeDeeply(context.parserOptions); - - // freezeDeeply(context.languageOptions); - - return rule.create(context); - }, - }), - }, - }, - }, - language: defaultConfig[0].language, - }, - ...defaultRuleTesterConfig, - ]; - - /** - * Runs a hook on the given item when it's assigned to the given property - * @param {string|Object} item Item to run the hook on - * @param {string} prop The property having the hook assigned to - * @throws {Error} If the property is not a function or that function throws an error - * @returns {void} - * @private - */ - function runHook(item, prop) { - if (typeof item === "object" && hasOwnProperty(item, prop)) { - assert.strictEqual( - typeof item[prop], - "function", - `Optional test case property '${prop}' must be a function`, - ); - item[prop](); - } - } - - /** - * Run the rule for the given item - * @param {string|Object} item Item to run the rule against - * @throws {Error} If an invalid schema. - * @returns {Object} Eslint run result - * @private - */ - function runRuleForItem(item) { - const flatConfigArrayOptions = { - baseConfig, - }; - - if (item.filename) { - flatConfigArrayOptions.basePath = - path.parse(item.filename).root || void 0; - } - - const configs = new FlatConfigArray( - testerConfig, - flatConfigArrayOptions, - ); - - /* - * Modify the returned config so that the parser is wrapped to catch - * access of the start/end properties. This method is called just - * once per code snippet being tested, so each test case gets a clean - * parser. - */ - configs[ConfigArraySymbol.finalizeConfig] = function (...args) { - // can't do super here :( - const proto = Object.getPrototypeOf(this); - const calculatedConfig = proto[ - ConfigArraySymbol.finalizeConfig - ].apply(this, args); - - // wrap the parser to catch start/end property access - if (calculatedConfig.language === jslang) { - calculatedConfig.languageOptions.parser = wrapParser( - calculatedConfig.languageOptions.parser, - ); - } - - return calculatedConfig; - }; - - let code, filename, output, beforeAST, afterAST; - - if (typeof item === "string") { - code = item; - } else { - code = item.code; - - /* - * Assumes everything on the item is a config except for the - * parameters used by this tester - */ - const itemConfig = { ...item }; - - for (const parameter of RuleTesterParameters) { - delete itemConfig[parameter]; - } - - /* - * Create the config object from the tester config and this item - * specific configurations. - */ - configs.push(itemConfig); - } - - if (hasOwnProperty(item, "only")) { - assert.ok( - typeof item.only === "boolean", - "Optional test case property 'only' must be a boolean", - ); - } - if (hasOwnProperty(item, "filename")) { - assert.ok( - typeof item.filename === "string", - "Optional test case property 'filename' must be a string", - ); - filename = item.filename; - } - - let ruleConfig = 1; - - if (hasOwnProperty(item, "options")) { - assert(Array.isArray(item.options), "options must be an array"); - ruleConfig = [1, ...item.options]; - } - - configs.push({ - rules: { - [ruleId]: ruleConfig, - }, - }); - - let schema; - - try { - schema = Config.getRuleOptionsSchema(rule); - } catch (err) { - err.message += metaSchemaDescription; - throw err; - } - - /* - * Check and throw an error if the schema is an empty object (`schema:{}`), because such schema - * doesn't validate or enforce anything and is therefore considered a possible error. If the intent - * was to skip options validation, `schema:false` should be set instead (explicit opt-out). - * - * For this purpose, a schema object is considered empty if it doesn't have any own enumerable string-keyed - * properties. While `ajv.compile()` does use enumerable properties from the prototype chain as well, - * it caches compiled schemas by serializing only own enumerable properties, so it's generally not a good idea - * to use inherited properties in schemas because schemas that differ only in inherited properties would end up - * having the same cache entry that would be correct for only one of them. - * - * At this point, `schema` can only be an object or `null`. - */ - if (schema && Object.keys(schema).length === 0) { - throw new Error( - `\`schema: {}\` is a no-op${metaSchemaDescription}`, - ); - } - - /* - * Setup AST getters. - * The goal is to check whether or not AST was modified when - * running the rule under test. - */ - configs.push({ - plugins: { - "rule-tester": { - rules: { - "validate-ast": { - create() { - return { - Program(node) { - beforeAST = - cloneDeeplyExcludesParent(node); - }, - "Program:exit"(node) { - afterAST = node; - }, - }; - }, - }, - }, - }, - }, - }); - - if (schema) { - ajv.validateSchema(schema); - - if (ajv.errors) { - const errors = ajv.errors - .map(error => { - const field = - error.dataPath[0] === "." - ? error.dataPath.slice(1) - : error.dataPath; - - return `\t${field}: ${error.message}`; - }) - .join("\n"); - - throw new Error([ - `Schema for rule ${ruleName} is invalid:`, - errors, - ]); - } - - /* - * `ajv.validateSchema` checks for errors in the structure of the schema (by comparing the schema against a "meta-schema"), - * and it reports those errors individually. However, there are other types of schema errors that only occur when compiling - * the schema (e.g. using invalid defaults in a schema), and only one of these errors can be reported at a time. As a result, - * the schema is compiled here separately from checking for `validateSchema` errors. - */ - try { - ajv.compile(schema); - } catch (err) { - throw new Error( - `Schema for rule ${ruleName} is invalid: ${err.message}`, - { - cause: err, - }, - ); - } - } - - // check for validation errors - try { - configs.normalizeSync(); - configs.getConfig("test.js"); - } catch (error) { - error.message = `ESLint configuration in rule-tester is invalid: ${error.message}`; - throw error; - } - - // Verify the code. - const { applyLanguageOptions, applyInlineConfig, finalize } = - SourceCode.prototype; - let messages; - - try { - forbiddenMethods.forEach(methodName => { - SourceCode.prototype[methodName] = - throwForbiddenMethodError( - methodName, - SourceCode.prototype, - ); - }); - - messages = linter.verify(code, configs, filename); - } finally { - SourceCode.prototype.applyInlineConfig = applyInlineConfig; - SourceCode.prototype.applyLanguageOptions = - applyLanguageOptions; - SourceCode.prototype.finalize = finalize; - } - - const fatalErrorMessage = messages.find(m => m.fatal); - - assert( - !fatalErrorMessage, - `A fatal parsing error occurred: ${fatalErrorMessage && fatalErrorMessage.message}`, - ); - - // Verify if autofix makes a syntax error or not. - if (messages.some(m => m.fix)) { - output = SourceCodeFixer.applyFixes(code, messages).output; - const errorMessageInFix = linter - .verify(output, configs, filename) - .find(m => m.fatal); - - assert( - !errorMessageInFix, - [ - "A fatal parsing error occurred in autofix.", - `Error: ${errorMessageInFix && errorMessageInFix.message}`, - "Autofix output:", - output, - ].join("\n"), - ); - } else { - output = code; - } - - return { - messages, - output, - beforeAST, - afterAST: cloneDeeplyExcludesParent(afterAST), - configs, - filename, - }; - } - - /** - * Check if the AST was changed - * @param {ASTNode} beforeAST AST node before running - * @param {ASTNode} afterAST AST node after running - * @returns {void} - * @private - */ - function assertASTDidntChange(beforeAST, afterAST) { - if (!equal(beforeAST, afterAST)) { - assert.fail("Rule should not modify AST."); - } - } - - /** - * Check if this test case is a duplicate of one we have seen before. - * @param {string|Object} item test case object - * @param {Set} seenTestCases set of serialized test cases we have seen so far (managed by this function) - * @returns {void} - * @private - */ - function checkDuplicateTestCase(item, seenTestCases) { - if (!isSerializable(item)) { - /* - * If we can't serialize a test case (because it contains a function, RegExp, etc), skip the check. - * This might happen with properties like: options, plugins, settings, languageOptions.parser, languageOptions.parserOptions. - */ - return; - } - - const normalizedItem = - typeof item === "string" ? { code: item } : item; - const serializedTestCase = stringify(normalizedItem, { - replacer(key, value) { - // "this" is the currently stringified object --> only ignore top-level properties - return normalizedItem !== this || - !duplicationIgnoredParameters.has(key) - ? value - : void 0; - }, - }); - - assert( - !seenTestCases.has(serializedTestCase), - "detected duplicate test case", - ); - seenTestCases.add(serializedTestCase); - } - - /** - * Check if the template is valid or not - * all valid cases go through this - * @param {string|Object} item Item to run the rule against - * @returns {void} - * @private - */ - function testValidTemplate(item) { - const code = typeof item === "object" ? item.code : item; - - assert.ok( - typeof code === "string", - "Test case must specify a string value for 'code'", - ); - if (item.name) { - assert.ok( - typeof item.name === "string", - "Optional test case property 'name' must be a string", - ); - } - - checkDuplicateTestCase(item, seenValidTestCases); - - const result = runRuleForItem(item); - const messages = result.messages; - - assert.strictEqual( - messages.length, - 0, - util.format( - "Should have no errors but had %d: %s", - messages.length, - util.inspect(messages), - ), - ); - - assertASTDidntChange(result.beforeAST, result.afterAST); - } - - /** - * Asserts that the message matches its expected value. If the expected - * value is a regular expression, it is checked against the actual - * value. - * @param {string} actual Actual value - * @param {string|RegExp} expected Expected value - * @returns {void} - * @private - */ - function assertMessageMatches(actual, expected) { - if (expected instanceof RegExp) { - // assert.js doesn't have a built-in RegExp match function - assert.ok( - expected.test(actual), - `Expected '${actual}' to match ${expected}`, - ); - } else { - assert.strictEqual(actual, expected); - } - } - - /** - * Check if the template is invalid or not - * all invalid cases go through this. - * @param {string|Object} item Item to run the rule against - * @returns {void} - * @private - */ - function testInvalidTemplate(item) { - assert.ok( - typeof item.code === "string", - "Test case must specify a string value for 'code'", - ); - if (item.name) { - assert.ok( - typeof item.name === "string", - "Optional test case property 'name' must be a string", - ); - } - assert.ok( - item.errors || item.errors === 0, - `Did not specify errors for an invalid test of ${ruleName}`, - ); - - if (Array.isArray(item.errors) && item.errors.length === 0) { - assert.fail("Invalid cases must have at least one error"); - } - - checkDuplicateTestCase(item, seenInvalidTestCases); - - const ruleHasMetaMessages = - hasOwnProperty(rule, "meta") && - hasOwnProperty(rule.meta, "messages"); - const friendlyIDList = ruleHasMetaMessages - ? `[${Object.keys(rule.meta.messages) - .map(key => `'${key}'`) - .join(", ")}]` - : null; - - const result = runRuleForItem(item); - const messages = result.messages; - - for (const message of messages) { - if (hasOwnProperty(message, "suggestions")) { - /** @type {Map} */ - const seenMessageIndices = new Map(); - - for (let i = 0; i < message.suggestions.length; i += 1) { - const suggestionMessage = message.suggestions[i].desc; - const previous = - seenMessageIndices.get(suggestionMessage); - - assert.ok( - !seenMessageIndices.has(suggestionMessage), - `Suggestion message '${suggestionMessage}' reported from suggestion ${i} was previously reported by suggestion ${previous}. Suggestion messages should be unique within an error.`, - ); - seenMessageIndices.set(suggestionMessage, i); - } - } - } - - if (typeof item.errors === "number") { - if (item.errors === 0) { - assert.fail( - "Invalid cases must have 'error' value greater than 0", - ); - } - - assert.strictEqual( - messages.length, - item.errors, - util.format( - "Should have %d error%s but had %d: %s", - item.errors, - item.errors === 1 ? "" : "s", - messages.length, - util.inspect(messages), - ), - ); - } else { - assert.strictEqual( - messages.length, - item.errors.length, - util.format( - "Should have %d error%s but had %d: %s", - item.errors.length, - item.errors.length === 1 ? "" : "s", - messages.length, - util.inspect(messages), - ), - ); - - const hasMessageOfThisRule = messages.some( - m => m.ruleId === ruleId, - ); - - for (let i = 0, l = item.errors.length; i < l; i++) { - const error = item.errors[i]; - const message = messages[i]; - - assert( - hasMessageOfThisRule, - "Error rule name should be the same as the name of the rule being tested", - ); - - if (typeof error === "string" || error instanceof RegExp) { - // Just an error message. - assertMessageMatches(message.message, error); - assert.ok( - message.suggestions === void 0, - `Error at index ${i} has suggestions. Please convert the test error into an object and specify 'suggestions' property on it to test suggestions.`, - ); - } else if (typeof error === "object" && error !== null) { - /* - * Error object. - * This may have a message, messageId, data, node type, line, and/or - * column. - */ - - Object.keys(error).forEach(propertyName => { - assert.ok( - errorObjectParameters.has(propertyName), - `Invalid error property name '${propertyName}'. Expected one of ${friendlyErrorObjectParameterList}.`, - ); - }); - - if (hasOwnProperty(error, "message")) { - assert.ok( - !hasOwnProperty(error, "messageId"), - "Error should not specify both 'message' and a 'messageId'.", - ); - assert.ok( - !hasOwnProperty(error, "data"), - "Error should not specify both 'data' and 'message'.", - ); - assertMessageMatches( - message.message, - error.message, - ); - } else if (hasOwnProperty(error, "messageId")) { - assert.ok( - ruleHasMetaMessages, - "Error can not use 'messageId' if rule under test doesn't define 'meta.messages'.", - ); - if ( - !hasOwnProperty( - rule.meta.messages, - error.messageId, - ) - ) { - assert( - false, - `Invalid messageId '${error.messageId}'. Expected one of ${friendlyIDList}.`, - ); - } - assert.strictEqual( - message.messageId, - error.messageId, - `messageId '${message.messageId}' does not match expected messageId '${error.messageId}'.`, - ); - - const unsubstitutedPlaceholders = - getUnsubstitutedMessagePlaceholders( - message.message, - rule.meta.messages[message.messageId], - error.data, - ); - - assert.ok( - unsubstitutedPlaceholders.length === 0, - `The reported message has ${unsubstitutedPlaceholders.length > 1 ? `unsubstituted placeholders: ${unsubstitutedPlaceholders.map(name => `'${name}'`).join(", ")}` : `an unsubstituted placeholder '${unsubstitutedPlaceholders[0]}'`}. Please provide the missing ${unsubstitutedPlaceholders.length > 1 ? "values" : "value"} via the 'data' property in the context.report() call.`, - ); - - if (hasOwnProperty(error, "data")) { - /* - * if data was provided, then directly compare the returned message to a synthetic - * interpolated message using the same message ID and data provided in the test. - * See https://github.com/eslint/eslint/issues/9890 for context. - */ - const unformattedOriginalMessage = - rule.meta.messages[error.messageId]; - const rehydratedMessage = interpolate( - unformattedOriginalMessage, - error.data, - ); - - assert.strictEqual( - message.message, - rehydratedMessage, - `Hydrated message "${rehydratedMessage}" does not match "${message.message}"`, - ); - } - } else { - assert.fail( - "Test error must specify either a 'messageId' or 'message'.", - ); - } - - if (error.type) { - assert.strictEqual( - message.nodeType, - error.type, - `Error type should be ${error.type}, found ${message.nodeType}`, - ); - } - - const actualLocation = {}; - const expectedLocation = {}; - - if (hasOwnProperty(error, "line")) { - actualLocation.line = message.line; - expectedLocation.line = error.line; - } - - if (hasOwnProperty(error, "column")) { - actualLocation.column = message.column; - expectedLocation.column = error.column; - } - - if (hasOwnProperty(error, "endLine")) { - actualLocation.endLine = message.endLine; - expectedLocation.endLine = error.endLine; - } - - if (hasOwnProperty(error, "endColumn")) { - actualLocation.endColumn = message.endColumn; - expectedLocation.endColumn = error.endColumn; - } - - if (Object.keys(expectedLocation).length > 0) { - assert.deepStrictEqual( - actualLocation, - expectedLocation, - "Actual error location does not match expected error location.", - ); - } - - assert.ok( - !message.suggestions || - hasOwnProperty(error, "suggestions"), - `Error at index ${i} has suggestions. Please specify 'suggestions' property on the test error object.`, - ); - if (hasOwnProperty(error, "suggestions")) { - // Support asserting there are no suggestions - const expectsSuggestions = Array.isArray( - error.suggestions, - ) - ? error.suggestions.length > 0 - : Boolean(error.suggestions); - const hasSuggestions = - message.suggestions !== void 0; - - if (!hasSuggestions && expectsSuggestions) { - assert.ok( - !error.suggestions, - `Error should have suggestions on error with message: "${message.message}"`, - ); - } else if (hasSuggestions) { - assert.ok( - expectsSuggestions, - `Error should have no suggestions on error with message: "${message.message}"`, - ); - if (typeof error.suggestions === "number") { - assert.strictEqual( - message.suggestions.length, - error.suggestions, - `Error should have ${error.suggestions} suggestions. Instead found ${message.suggestions.length} suggestions`, - ); - } else if (Array.isArray(error.suggestions)) { - assert.strictEqual( - message.suggestions.length, - error.suggestions.length, - `Error should have ${error.suggestions.length} suggestions. Instead found ${message.suggestions.length} suggestions`, - ); - - error.suggestions.forEach( - (expectedSuggestion, index) => { - assert.ok( - typeof expectedSuggestion === - "object" && - expectedSuggestion !== null, - "Test suggestion in 'suggestions' array must be an object.", - ); - Object.keys( - expectedSuggestion, - ).forEach(propertyName => { - assert.ok( - suggestionObjectParameters.has( - propertyName, - ), - `Invalid suggestion property name '${propertyName}'. Expected one of ${friendlySuggestionObjectParameterList}.`, - ); - }); - - const actualSuggestion = - message.suggestions[index]; - const suggestionPrefix = `Error Suggestion at index ${index}:`; - - if ( - hasOwnProperty( - expectedSuggestion, - "desc", - ) - ) { - assert.ok( - !hasOwnProperty( - expectedSuggestion, - "data", - ), - `${suggestionPrefix} Test should not specify both 'desc' and 'data'.`, - ); - assert.ok( - !hasOwnProperty( - expectedSuggestion, - "messageId", - ), - `${suggestionPrefix} Test should not specify both 'desc' and 'messageId'.`, - ); - assert.strictEqual( - actualSuggestion.desc, - expectedSuggestion.desc, - `${suggestionPrefix} desc should be "${expectedSuggestion.desc}" but got "${actualSuggestion.desc}" instead.`, - ); - } else if ( - hasOwnProperty( - expectedSuggestion, - "messageId", - ) - ) { - assert.ok( - ruleHasMetaMessages, - `${suggestionPrefix} Test can not use 'messageId' if rule under test doesn't define 'meta.messages'.`, - ); - assert.ok( - hasOwnProperty( - rule.meta.messages, - expectedSuggestion.messageId, - ), - `${suggestionPrefix} Test has invalid messageId '${expectedSuggestion.messageId}', the rule under test allows only one of ${friendlyIDList}.`, - ); - assert.strictEqual( - actualSuggestion.messageId, - expectedSuggestion.messageId, - `${suggestionPrefix} messageId should be '${expectedSuggestion.messageId}' but got '${actualSuggestion.messageId}' instead.`, - ); - - const unsubstitutedPlaceholders = - getUnsubstitutedMessagePlaceholders( - actualSuggestion.desc, - rule.meta.messages[ - expectedSuggestion - .messageId - ], - expectedSuggestion.data, - ); - - assert.ok( - unsubstitutedPlaceholders.length === - 0, - `The message of the suggestion has ${unsubstitutedPlaceholders.length > 1 ? `unsubstituted placeholders: ${unsubstitutedPlaceholders.map(name => `'${name}'`).join(", ")}` : `an unsubstituted placeholder '${unsubstitutedPlaceholders[0]}'`}. Please provide the missing ${unsubstitutedPlaceholders.length > 1 ? "values" : "value"} via the 'data' property for the suggestion in the context.report() call.`, - ); - - if ( - hasOwnProperty( - expectedSuggestion, - "data", - ) - ) { - const unformattedMetaMessage = - rule.meta.messages[ - expectedSuggestion - .messageId - ]; - const rehydratedDesc = - interpolate( - unformattedMetaMessage, - expectedSuggestion.data, - ); - - assert.strictEqual( - actualSuggestion.desc, - rehydratedDesc, - `${suggestionPrefix} Hydrated test desc "${rehydratedDesc}" does not match received desc "${actualSuggestion.desc}".`, - ); - } - } else if ( - hasOwnProperty( - expectedSuggestion, - "data", - ) - ) { - assert.fail( - `${suggestionPrefix} Test must specify 'messageId' if 'data' is used.`, - ); - } else { - assert.fail( - `${suggestionPrefix} Test must specify either 'messageId' or 'desc'.`, - ); - } - - assert.ok( - hasOwnProperty( - expectedSuggestion, - "output", - ), - `${suggestionPrefix} The "output" property is required.`, - ); - const codeWithAppliedSuggestion = - SourceCodeFixer.applyFixes( - item.code, - [actualSuggestion], - ).output; - - // Verify if suggestion fix makes a syntax error or not. - const errorMessageInSuggestion = - linter - .verify( - codeWithAppliedSuggestion, - result.configs, - result.filename, - ) - .find(m => m.fatal); - - assert( - !errorMessageInSuggestion, - [ - "A fatal parsing error occurred in suggestion fix.", - `Error: ${errorMessageInSuggestion && errorMessageInSuggestion.message}`, - "Suggestion output:", - codeWithAppliedSuggestion, - ].join("\n"), - ); - - assert.strictEqual( - codeWithAppliedSuggestion, - expectedSuggestion.output, - `Expected the applied suggestion fix to match the test suggestion output for suggestion at index: ${index} on error with message: "${message.message}"`, - ); - assert.notStrictEqual( - expectedSuggestion.output, - item.code, - `The output of a suggestion should differ from the original source code for suggestion at index: ${index} on error with message: "${message.message}"`, - ); - }, - ); - } else { - assert.fail( - "Test error object property 'suggestions' should be an array or a number", - ); - } - } - } - } else { - // Message was an unexpected type - assert.fail( - `Error should be a string, object, or RegExp, but found (${util.inspect(message)})`, - ); - } - } - } - - if (hasOwnProperty(item, "output")) { - if (item.output === null) { - assert.strictEqual( - result.output, - item.code, - "Expected no autofixes to be suggested", - ); - } else { - assert.strictEqual( - result.output, - item.output, - "Output is incorrect.", - ); - assert.notStrictEqual( - item.code, - item.output, - "Test property 'output' matches 'code'. If no autofix is expected, then omit the 'output' property or set it to null.", - ); - } - } else { - assert.strictEqual( - result.output, - item.code, - "The rule fixed the code. Please add 'output' property.", - ); - } - - assertASTDidntChange(result.beforeAST, result.afterAST); - } - - /* - * This creates a mocha test suite and pipes all supplied info through - * one of the templates above. - * The test suites for valid/invalid are created conditionally as - * test runners (eg. vitest) fail for empty test suites. - */ - this.constructor.describe(ruleName, () => { - if (test.valid.length > 0) { - this.constructor.describe("valid", () => { - test.valid.forEach(valid => { - this.constructor[valid.only ? "itOnly" : "it"]( - sanitize( - typeof valid === "object" - ? valid.name || valid.code - : valid, - ), - () => { - try { - runHook(valid, "before"); - testValidTemplate(valid); - } finally { - runHook(valid, "after"); - } - }, - ); - }); - }); - } - - if (test.invalid.length > 0) { - this.constructor.describe("invalid", () => { - test.invalid.forEach(invalid => { - this.constructor[invalid.only ? "itOnly" : "it"]( - sanitize(invalid.name || invalid.code), - () => { - try { - runHook(invalid, "before"); - testInvalidTemplate(invalid); - } finally { - runHook(invalid, "after"); - } - }, - ); - }); - }); - } - }); - } + + /** + * Creates a new instance of RuleTester. + * @param {Object} [testerConfig] Optional, extra configuration for the tester + */ + constructor(testerConfig) { + + /** + * The configuration to use for this tester. Combination of the tester + * configuration and the default configuration. + * @type {Object} + */ + this.testerConfig = merge( + {}, + defaultConfig, + testerConfig, + { rules: { "rule-tester/validate-ast": "error" } } + ); + + /** + * Rule definitions to define before tests. + * @type {Object} + */ + this.rules = {}; + this.linter = new Linter(); + } + + /** + * Set the configuration to use for all future tests + * @param {Object} config the configuration to use. + * @throws {TypeError} If non-object config. + * @returns {void} + */ + static setDefaultConfig(config) { + if (typeof config !== "object" || config === null) { + throw new TypeError("RuleTester.setDefaultConfig: config must be an object"); + } + defaultConfig = config; + + // Make sure the rules object exists since it is assumed to exist later + defaultConfig.rules = defaultConfig.rules || {}; + } + + /** + * Get the current configuration used for all tests + * @returns {Object} the current configuration + */ + static getDefaultConfig() { + return defaultConfig; + } + + /** + * Reset the configuration to the initial configuration of the tester removing + * any changes made until now. + * @returns {void} + */ + static resetDefaultConfig() { + defaultConfig = merge({}, testerDefaultConfig); + } + + + /* + * If people use `mocha test.js --watch` command, `describe` and `it` function + * instances are different for each execution. So `describe` and `it` should get fresh instance + * always. + */ + static get describe() { + return ( + this[DESCRIBE] || + (typeof describe === "function" ? describe : describeDefaultHandler) + ); + } + + static set describe(value) { + this[DESCRIBE] = value; + } + + static get it() { + return ( + this[IT] || + (typeof it === "function" ? it : itDefaultHandler) + ); + } + + static set it(value) { + this[IT] = value; + } + + /** + * Adds the `only` property to a test to run it in isolation. + * @param {string | ValidTestCase | InvalidTestCase} item A single test to run by itself. + * @returns {ValidTestCase | InvalidTestCase} The test with `only` set. + */ + static only(item) { + if (typeof item === "string") { + return { code: item, only: true }; + } + + return { ...item, only: true }; + } + + static get itOnly() { + if (typeof this[IT_ONLY] === "function") { + return this[IT_ONLY]; + } + if (typeof this[IT] === "function" && typeof this[IT].only === "function") { + return Function.bind.call(this[IT].only, this[IT]); + } + if (typeof it === "function" && typeof it.only === "function") { + return Function.bind.call(it.only, it); + } + + if (typeof this[DESCRIBE] === "function" || typeof this[IT] === "function") { + throw new Error( + "Set `RuleTester.itOnly` to use `only` with a custom test framework.\n" + + "See https://eslint.org/docs/latest/integrate/nodejs-api#customizing-ruletester for more." + ); + } + if (typeof it === "function") { + throw new Error("The current test framework does not support exclusive tests with `only`."); + } + throw new Error("To use `only`, use RuleTester with a test framework that provides `it.only()` like Mocha."); + } + + static set itOnly(value) { + this[IT_ONLY] = value; + } + + /** + * Define a rule for one particular run of tests. + * @param {string} name The name of the rule to define. + * @param {Function | Rule} rule The rule definition. + * @returns {void} + */ + defineRule(name, rule) { + if (typeof rule === "function") { + emitLegacyRuleAPIWarning(name); + } + this.rules[name] = rule; + } + + /** + * Adds a new rule test to execute. + * @param {string} ruleName The name of the rule to run. + * @param {Function | Rule} rule The rule to test. + * @param {{ + * valid: (ValidTestCase | string)[], + * invalid: InvalidTestCase[] + * }} test The collection of tests to run. + * @throws {TypeError|Error} If non-object `test`, or if a required + * scenario of the given type is missing. + * @returns {void} + */ + run(ruleName, rule, test) { + + const testerConfig = this.testerConfig, + requiredScenarios = ["valid", "invalid"], + scenarioErrors = [], + linter = this.linter; + + if (!test || typeof test !== "object") { + throw new TypeError(`Test Scenarios for rule ${ruleName} : Could not find test scenario object`); + } + + requiredScenarios.forEach(scenarioType => { + if (!test[scenarioType]) { + scenarioErrors.push(`Could not find any ${scenarioType} test scenarios`); + } + }); + + if (scenarioErrors.length > 0) { + throw new Error([ + `Test Scenarios for rule ${ruleName} is invalid:` + ].concat(scenarioErrors).join("\n")); + } + + if (typeof rule === "function") { + emitLegacyRuleAPIWarning(ruleName); + } + + linter.defineRule(ruleName, Object.assign({}, rule, { + + // Create a wrapper rule that freezes the `context` properties. + create(context) { + freezeDeeply(context.options); + freezeDeeply(context.settings); + freezeDeeply(context.parserOptions); + + // wrap all deprecated methods + const newContext = Object.create( + context, + Object.fromEntries(Object.keys(DEPRECATED_SOURCECODE_PASSTHROUGHS).map(methodName => [ + methodName, + { + value(...args) { + + // emit deprecation warning + emitDeprecatedContextMethodWarning(ruleName, methodName); + + // call the original method + return context[methodName].call(this, ...args); + }, + enumerable: true + } + ])) + ); + + // emit warning about context.parserServices + const parserServices = context.parserServices; + + Object.defineProperty(newContext, "parserServices", { + get() { + emitParserServicesWarning(ruleName); + return parserServices; + } + }); + + Object.freeze(newContext); + + return (typeof rule === "function" ? rule : rule.create)(newContext); + } + })); + + linter.defineRules(this.rules); + + /** + * Run the rule for the given item + * @param {string|Object} item Item to run the rule against + * @throws {Error} If an invalid schema. + * @returns {Object} Eslint run result + * @private + */ + function runRuleForItem(item) { + let config = merge({}, testerConfig), + code, filename, output, beforeAST, afterAST; + + if (typeof item === "string") { + code = item; + } else { + code = item.code; + + /* + * Assumes everything on the item is a config except for the + * parameters used by this tester + */ + const itemConfig = { ...item }; + + for (const parameter of RuleTesterParameters) { + delete itemConfig[parameter]; + } + + /* + * Create the config object from the tester config and this item + * specific configurations. + */ + config = merge( + config, + itemConfig + ); + } + + if (item.filename) { + filename = item.filename; + } + + if (hasOwnProperty(item, "options")) { + assert(Array.isArray(item.options), "options must be an array"); + if ( + item.options.length > 0 && + typeof rule === "object" && + ( + !rule.meta || (rule.meta && (typeof rule.meta.schema === "undefined" || rule.meta.schema === null)) + ) + ) { + emitMissingSchemaWarning(ruleName); + } + config.rules[ruleName] = [1].concat(item.options); + } else { + config.rules[ruleName] = 1; + } + + const schema = getRuleOptionsSchema(rule); + + /* + * Setup AST getters. + * The goal is to check whether or not AST was modified when + * running the rule under test. + */ + linter.defineRule("rule-tester/validate-ast", { + create() { + return { + Program(node) { + beforeAST = cloneDeeplyExcludesParent(node); + }, + "Program:exit"(node) { + afterAST = node; + } + }; + } + }); + + if (typeof config.parser === "string") { + assert(path.isAbsolute(config.parser), "Parsers provided as strings to RuleTester must be absolute paths"); + } else { + config.parser = espreePath; + } + + linter.defineParser(config.parser, wrapParser(require(config.parser))); + + if (schema) { + ajv.validateSchema(schema); + + if (ajv.errors) { + const errors = ajv.errors.map(error => { + const field = error.dataPath[0] === "." ? error.dataPath.slice(1) : error.dataPath; + + return `\t${field}: ${error.message}`; + }).join("\n"); + + throw new Error([`Schema for rule ${ruleName} is invalid:`, errors]); + } + + /* + * `ajv.validateSchema` checks for errors in the structure of the schema (by comparing the schema against a "meta-schema"), + * and it reports those errors individually. However, there are other types of schema errors that only occur when compiling + * the schema (e.g. using invalid defaults in a schema), and only one of these errors can be reported at a time. As a result, + * the schema is compiled here separately from checking for `validateSchema` errors. + */ + try { + ajv.compile(schema); + } catch (err) { + throw new Error(`Schema for rule ${ruleName} is invalid: ${err.message}`); + } + } + + validate(config, "rule-tester", id => (id === ruleName ? rule : null)); + + // Verify the code. + const { getComments, applyLanguageOptions, applyInlineConfig, finalize } = SourceCode.prototype; + const originalCurrentSegments = Object.getOwnPropertyDescriptor(CodePath.prototype, "currentSegments"); + let messages; + + try { + SourceCode.prototype.getComments = getCommentsDeprecation; + Object.defineProperty(CodePath.prototype, "currentSegments", { + get() { + emitCodePathCurrentSegmentsWarning(ruleName); + return originalCurrentSegments.get.call(this); + } + }); + + forbiddenMethods.forEach(methodName => { + SourceCode.prototype[methodName] = throwForbiddenMethodError(methodName); + }); + + messages = linter.verify(code, config, filename); + } finally { + SourceCode.prototype.getComments = getComments; + Object.defineProperty(CodePath.prototype, "currentSegments", originalCurrentSegments); + SourceCode.prototype.applyInlineConfig = applyInlineConfig; + SourceCode.prototype.applyLanguageOptions = applyLanguageOptions; + SourceCode.prototype.finalize = finalize; + } + + const fatalErrorMessage = messages.find(m => m.fatal); + + assert(!fatalErrorMessage, `A fatal parsing error occurred: ${fatalErrorMessage && fatalErrorMessage.message}`); + + // Verify if autofix makes a syntax error or not. + if (messages.some(m => m.fix)) { + output = SourceCodeFixer.applyFixes(code, messages).output; + const errorMessageInFix = linter.verify(output, config, filename).find(m => m.fatal); + + assert(!errorMessageInFix, [ + "A fatal parsing error occurred in autofix.", + `Error: ${errorMessageInFix && errorMessageInFix.message}`, + "Autofix output:", + output + ].join("\n")); + } else { + output = code; + } + + return { + messages, + output, + beforeAST, + afterAST: cloneDeeplyExcludesParent(afterAST) + }; + } + + /** + * Check if the AST was changed + * @param {ASTNode} beforeAST AST node before running + * @param {ASTNode} afterAST AST node after running + * @returns {void} + * @private + */ + function assertASTDidntChange(beforeAST, afterAST) { + if (!equal(beforeAST, afterAST)) { + assert.fail("Rule should not modify AST."); + } + } + + /** + * Check if the template is valid or not + * all valid cases go through this + * @param {string|Object} item Item to run the rule against + * @returns {void} + * @private + */ + function testValidTemplate(item) { + const code = typeof item === "object" ? item.code : item; + + assert.ok(typeof code === "string", "Test case must specify a string value for 'code'"); + if (item.name) { + assert.ok(typeof item.name === "string", "Optional test case property 'name' must be a string"); + } + + const result = runRuleForItem(item); + const messages = result.messages; + + assert.strictEqual(messages.length, 0, util.format("Should have no errors but had %d: %s", + messages.length, + util.inspect(messages))); + + assertASTDidntChange(result.beforeAST, result.afterAST); + } + + /** + * Asserts that the message matches its expected value. If the expected + * value is a regular expression, it is checked against the actual + * value. + * @param {string} actual Actual value + * @param {string|RegExp} expected Expected value + * @returns {void} + * @private + */ + function assertMessageMatches(actual, expected) { + if (expected instanceof RegExp) { + + // assert.js doesn't have a built-in RegExp match function + assert.ok( + expected.test(actual), + `Expected '${actual}' to match ${expected}` + ); + } else { + assert.strictEqual(actual, expected); + } + } + + /** + * Check if the template is invalid or not + * all invalid cases go through this. + * @param {string|Object} item Item to run the rule against + * @returns {void} + * @private + */ + function testInvalidTemplate(item) { + assert.ok(typeof item.code === "string", "Test case must specify a string value for 'code'"); + if (item.name) { + assert.ok(typeof item.name === "string", "Optional test case property 'name' must be a string"); + } + assert.ok(item.errors || item.errors === 0, + `Did not specify errors for an invalid test of ${ruleName}`); + + if (Array.isArray(item.errors) && item.errors.length === 0) { + assert.fail("Invalid cases must have at least one error"); + } + + const ruleHasMetaMessages = hasOwnProperty(rule, "meta") && hasOwnProperty(rule.meta, "messages"); + const friendlyIDList = ruleHasMetaMessages ? `[${Object.keys(rule.meta.messages).map(key => `'${key}'`).join(", ")}]` : null; + + const result = runRuleForItem(item); + const messages = result.messages; + + if (typeof item.errors === "number") { + + if (item.errors === 0) { + assert.fail("Invalid cases must have 'error' value greater than 0"); + } + + assert.strictEqual(messages.length, item.errors, util.format("Should have %d error%s but had %d: %s", + item.errors, + item.errors === 1 ? "" : "s", + messages.length, + util.inspect(messages))); + } else { + assert.strictEqual( + messages.length, item.errors.length, util.format( + "Should have %d error%s but had %d: %s", + item.errors.length, + item.errors.length === 1 ? "" : "s", + messages.length, + util.inspect(messages) + ) + ); + + const hasMessageOfThisRule = messages.some(m => m.ruleId === ruleName); + + for (let i = 0, l = item.errors.length; i < l; i++) { + const error = item.errors[i]; + const message = messages[i]; + + assert(hasMessageOfThisRule, "Error rule name should be the same as the name of the rule being tested"); + + if (typeof error === "string" || error instanceof RegExp) { + + // Just an error message. + assertMessageMatches(message.message, error); + } else if (typeof error === "object" && error !== null) { + + /* + * Error object. + * This may have a message, messageId, data, node type, line, and/or + * column. + */ + + Object.keys(error).forEach(propertyName => { + assert.ok( + errorObjectParameters.has(propertyName), + `Invalid error property name '${propertyName}'. Expected one of ${friendlyErrorObjectParameterList}.` + ); + }); + + if (hasOwnProperty(error, "message")) { + assert.ok(!hasOwnProperty(error, "messageId"), "Error should not specify both 'message' and a 'messageId'."); + assert.ok(!hasOwnProperty(error, "data"), "Error should not specify both 'data' and 'message'."); + assertMessageMatches(message.message, error.message); + } else if (hasOwnProperty(error, "messageId")) { + assert.ok( + ruleHasMetaMessages, + "Error can not use 'messageId' if rule under test doesn't define 'meta.messages'." + ); + if (!hasOwnProperty(rule.meta.messages, error.messageId)) { + assert(false, `Invalid messageId '${error.messageId}'. Expected one of ${friendlyIDList}.`); + } + assert.strictEqual( + message.messageId, + error.messageId, + `messageId '${message.messageId}' does not match expected messageId '${error.messageId}'.` + ); + if (hasOwnProperty(error, "data")) { + + /* + * if data was provided, then directly compare the returned message to a synthetic + * interpolated message using the same message ID and data provided in the test. + * See https://github.com/eslint/eslint/issues/9890 for context. + */ + const unformattedOriginalMessage = rule.meta.messages[error.messageId]; + const rehydratedMessage = interpolate(unformattedOriginalMessage, error.data); + + assert.strictEqual( + message.message, + rehydratedMessage, + `Hydrated message "${rehydratedMessage}" does not match "${message.message}"` + ); + } + } + + assert.ok( + hasOwnProperty(error, "data") ? hasOwnProperty(error, "messageId") : true, + "Error must specify 'messageId' if 'data' is used." + ); + + if (error.type) { + assert.strictEqual(message.nodeType, error.type, `Error type should be ${error.type}, found ${message.nodeType}`); + } + + if (hasOwnProperty(error, "line")) { + assert.strictEqual(message.line, error.line, `Error line should be ${error.line}`); + } + + if (hasOwnProperty(error, "column")) { + assert.strictEqual(message.column, error.column, `Error column should be ${error.column}`); + } + + if (hasOwnProperty(error, "endLine")) { + assert.strictEqual(message.endLine, error.endLine, `Error endLine should be ${error.endLine}`); + } + + if (hasOwnProperty(error, "endColumn")) { + assert.strictEqual(message.endColumn, error.endColumn, `Error endColumn should be ${error.endColumn}`); + } + + if (hasOwnProperty(error, "suggestions")) { + + // Support asserting there are no suggestions + if (!error.suggestions || (Array.isArray(error.suggestions) && error.suggestions.length === 0)) { + if (Array.isArray(message.suggestions) && message.suggestions.length > 0) { + assert.fail(`Error should have no suggestions on error with message: "${message.message}"`); + } + } else { + assert.strictEqual(Array.isArray(message.suggestions), true, `Error should have an array of suggestions. Instead received "${message.suggestions}" on error with message: "${message.message}"`); + assert.strictEqual(message.suggestions.length, error.suggestions.length, `Error should have ${error.suggestions.length} suggestions. Instead found ${message.suggestions.length} suggestions`); + + error.suggestions.forEach((expectedSuggestion, index) => { + assert.ok( + typeof expectedSuggestion === "object" && expectedSuggestion !== null, + "Test suggestion in 'suggestions' array must be an object." + ); + Object.keys(expectedSuggestion).forEach(propertyName => { + assert.ok( + suggestionObjectParameters.has(propertyName), + `Invalid suggestion property name '${propertyName}'. Expected one of ${friendlySuggestionObjectParameterList}.` + ); + }); + + const actualSuggestion = message.suggestions[index]; + const suggestionPrefix = `Error Suggestion at index ${index} :`; + + if (hasOwnProperty(expectedSuggestion, "desc")) { + assert.ok( + !hasOwnProperty(expectedSuggestion, "data"), + `${suggestionPrefix} Test should not specify both 'desc' and 'data'.` + ); + assert.strictEqual( + actualSuggestion.desc, + expectedSuggestion.desc, + `${suggestionPrefix} desc should be "${expectedSuggestion.desc}" but got "${actualSuggestion.desc}" instead.` + ); + } + + if (hasOwnProperty(expectedSuggestion, "messageId")) { + assert.ok( + ruleHasMetaMessages, + `${suggestionPrefix} Test can not use 'messageId' if rule under test doesn't define 'meta.messages'.` + ); + assert.ok( + hasOwnProperty(rule.meta.messages, expectedSuggestion.messageId), + `${suggestionPrefix} Test has invalid messageId '${expectedSuggestion.messageId}', the rule under test allows only one of ${friendlyIDList}.` + ); + assert.strictEqual( + actualSuggestion.messageId, + expectedSuggestion.messageId, + `${suggestionPrefix} messageId should be '${expectedSuggestion.messageId}' but got '${actualSuggestion.messageId}' instead.` + ); + if (hasOwnProperty(expectedSuggestion, "data")) { + const unformattedMetaMessage = rule.meta.messages[expectedSuggestion.messageId]; + const rehydratedDesc = interpolate(unformattedMetaMessage, expectedSuggestion.data); + + assert.strictEqual( + actualSuggestion.desc, + rehydratedDesc, + `${suggestionPrefix} Hydrated test desc "${rehydratedDesc}" does not match received desc "${actualSuggestion.desc}".` + ); + } + } else { + assert.ok( + !hasOwnProperty(expectedSuggestion, "data"), + `${suggestionPrefix} Test must specify 'messageId' if 'data' is used.` + ); + } + + if (hasOwnProperty(expectedSuggestion, "output")) { + const codeWithAppliedSuggestion = SourceCodeFixer.applyFixes(item.code, [actualSuggestion]).output; + + assert.strictEqual(codeWithAppliedSuggestion, expectedSuggestion.output, `Expected the applied suggestion fix to match the test suggestion output for suggestion at index: ${index} on error with message: "${message.message}"`); + } + }); + } + } + } else { + + // Message was an unexpected type + assert.fail(`Error should be a string, object, or RegExp, but found (${util.inspect(message)})`); + } + } + } + + if (hasOwnProperty(item, "output")) { + if (item.output === null) { + assert.strictEqual( + result.output, + item.code, + "Expected no autofixes to be suggested" + ); + } else { + assert.strictEqual(result.output, item.output, "Output is incorrect."); + } + } else { + assert.strictEqual( + result.output, + item.code, + "The rule fixed the code. Please add 'output' property." + ); + } + + assertASTDidntChange(result.beforeAST, result.afterAST); + } + + /* + * This creates a mocha test suite and pipes all supplied info through + * one of the templates above. + * The test suites for valid/invalid are created conditionally as + * test runners (eg. vitest) fail for empty test suites. + */ + this.constructor.describe(ruleName, () => { + if (test.valid.length > 0) { + this.constructor.describe("valid", () => { + test.valid.forEach(valid => { + this.constructor[valid.only ? "itOnly" : "it"]( + sanitize(typeof valid === "object" ? valid.name || valid.code : valid), + () => { + testValidTemplate(valid); + } + ); + }); + }); + } + + if (test.invalid.length > 0) { + this.constructor.describe("invalid", () => { + test.invalid.forEach(invalid => { + this.constructor[invalid.only ? "itOnly" : "it"]( + sanitize(invalid.name || invalid.code), + () => { + testInvalidTemplate(invalid); + } + ); + }); + }); + } + }); + } } RuleTester[DESCRIBE] = RuleTester[IT] = RuleTester[IT_ONLY] = null; diff --git a/node_modules/eslint/lib/rules/accessor-pairs.js b/node_modules/eslint/lib/rules/accessor-pairs.js index 4af67ad77..f97032895 100644 --- a/node_modules/eslint/lib/rules/accessor-pairs.js +++ b/node_modules/eslint/lib/rules/accessor-pairs.js @@ -40,23 +40,20 @@ const astUtils = require("./utils/ast-utils"); * @returns {boolean} `true` if the lists have same tokens. */ function areEqualTokenLists(left, right) { - if (left.length !== right.length) { - return false; - } - - for (let i = 0; i < left.length; i++) { - const leftToken = left[i], - rightToken = right[i]; - - if ( - leftToken.type !== rightToken.type || - leftToken.value !== rightToken.value - ) { - return false; - } - } - - return true; + if (left.length !== right.length) { + return false; + } + + for (let i = 0; i < left.length; i++) { + const leftToken = left[i], + rightToken = right[i]; + + if (leftToken.type !== rightToken.type || leftToken.value !== rightToken.value) { + return false; + } + } + + return true; } /** @@ -66,16 +63,18 @@ function areEqualTokenLists(left, right) { * @returns {boolean} `true` if the keys are equal. */ function areEqualKeys(left, right) { - if (typeof left === "string" && typeof right === "string") { - // Statically computed names. - return left === right; - } - if (Array.isArray(left) && Array.isArray(right)) { - // Token lists. - return areEqualTokenLists(left, right); - } - - return false; + if (typeof left === "string" && typeof right === "string") { + + // Statically computed names. + return left === right; + } + if (Array.isArray(left) && Array.isArray(right)) { + + // Token lists. + return areEqualTokenLists(left, right); + } + + return false; } /** @@ -84,7 +83,7 @@ function areEqualKeys(left, right) { * @returns {boolean} `true` if the node is of an accessor kind. */ function isAccessorKind(node) { - return node.kind === "get" || node.kind === "set"; + return node.kind === "get" || node.kind === "set"; } /** @@ -96,13 +95,13 @@ function isAccessorKind(node) { * @returns {boolean} `true` if the node is an argument of the specified method call. */ function isArgumentOfMethodCall(node, index, object, property) { - const parent = node.parent; + const parent = node.parent; - return ( - parent.type === "CallExpression" && - astUtils.isSpecificMemberAccess(parent.callee, object, property) && - parent.arguments[index] === node - ); + return ( + parent.type === "CallExpression" && + astUtils.isSpecificMemberAccess(parent.callee, object, property) && + parent.arguments[index] === node + ); } /** @@ -111,310 +110,237 @@ function isArgumentOfMethodCall(node, index, object, property) { * @returns {boolean} `true` if the node is a property descriptor. */ function isPropertyDescriptor(node) { - // Object.defineProperty(obj, "foo", {set: ...}) - if ( - isArgumentOfMethodCall(node, 2, "Object", "defineProperty") || - isArgumentOfMethodCall(node, 2, "Reflect", "defineProperty") - ) { - return true; - } - - /* - * Object.defineProperties(obj, {foo: {set: ...}}) - * Object.create(proto, {foo: {set: ...}}) - */ - const grandparent = node.parent.parent; - - return ( - grandparent.type === "ObjectExpression" && - (isArgumentOfMethodCall(grandparent, 1, "Object", "create") || - isArgumentOfMethodCall( - grandparent, - 1, - "Object", - "defineProperties", - )) - ); + + // Object.defineProperty(obj, "foo", {set: ...}) + if (isArgumentOfMethodCall(node, 2, "Object", "defineProperty") || + isArgumentOfMethodCall(node, 2, "Reflect", "defineProperty") + ) { + return true; + } + + /* + * Object.defineProperties(obj, {foo: {set: ...}}) + * Object.create(proto, {foo: {set: ...}}) + */ + const grandparent = node.parent.parent; + + return grandparent.type === "ObjectExpression" && ( + isArgumentOfMethodCall(grandparent, 1, "Object", "create") || + isArgumentOfMethodCall(grandparent, 1, "Object", "defineProperties") + ); } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - defaultOptions: [ - { - enforceForTSTypes: false, - enforceForClassMembers: true, - getWithoutSet: false, - setWithoutGet: true, - }, - ], - - docs: { - description: - "Enforce getter and setter pairs in objects and classes", - recommended: false, - url: "https://eslint.org/docs/latest/rules/accessor-pairs", - }, - - schema: [ - { - type: "object", - properties: { - getWithoutSet: { - type: "boolean", - }, - setWithoutGet: { - type: "boolean", - }, - enforceForClassMembers: { - type: "boolean", - }, - enforceForTSTypes: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - - messages: { - missingGetterInPropertyDescriptor: - "Getter is not present in property descriptor.", - missingSetterInPropertyDescriptor: - "Setter is not present in property descriptor.", - missingGetterInObjectLiteral: - "Getter is not present for {{ name }}.", - missingSetterInObjectLiteral: - "Setter is not present for {{ name }}.", - missingGetterInClass: "Getter is not present for class {{ name }}.", - missingSetterInClass: "Setter is not present for class {{ name }}.", - missingGetterInType: "Getter is not present for type {{ name }}.", - missingSetterInType: "Setter is not present for type {{ name }}.", - }, - }, - create(context) { - const [ - { - getWithoutSet: checkGetWithoutSet, - setWithoutGet: checkSetWithoutGet, - enforceForClassMembers, - enforceForTSTypes, - }, - ] = context.options; - const sourceCode = context.sourceCode; - - /** - * Reports the given node. - * @param {ASTNode} node The node to report. - * @param {string} messageKind "missingGetter" or "missingSetter". - * @returns {void} - * @private - */ - function report(node, messageKind) { - if (node.type === "Property") { - context.report({ - node, - messageId: `${messageKind}InObjectLiteral`, - loc: astUtils.getFunctionHeadLoc(node.value, sourceCode), - data: { - name: astUtils.getFunctionNameWithKind(node.value), - }, - }); - } else if (node.type === "MethodDefinition") { - context.report({ - node, - messageId: `${messageKind}InClass`, - loc: astUtils.getFunctionHeadLoc(node.value, sourceCode), - data: { - name: astUtils.getFunctionNameWithKind(node.value), - }, - }); - } else if (node.type === "TSMethodSignature") { - context.report({ - node, - messageId: `${messageKind}InType`, - loc: astUtils.getFunctionHeadLoc(node, sourceCode), - data: { - name: astUtils.getFunctionNameWithKind(node), - }, - }); - } else { - context.report({ - node, - messageId: `${messageKind}InPropertyDescriptor`, - }); - } - } - - /** - * Reports each of the nodes in the given list using the same messageId. - * @param {ASTNode[]} nodes Nodes to report. - * @param {string} messageKind "missingGetter" or "missingSetter". - * @returns {void} - * @private - */ - function reportList(nodes, messageKind) { - for (const node of nodes) { - report(node, messageKind); - } - } - - /** - * Checks accessor pairs in the given list of nodes. - * @param {ASTNode[]} nodes The list to check. - * @returns {void} - * @private - */ - function checkList(nodes) { - const accessors = []; - let found = false; - - for (let i = 0; i < nodes.length; i++) { - const node = nodes[i]; - - if (isAccessorKind(node)) { - // Creates a new `AccessorData` object for the given getter or setter node. - const name = astUtils.getStaticPropertyName(node); - const key = - name !== null ? name : sourceCode.getTokens(node.key); - - // Merges the given `AccessorData` object into the given accessors list. - for (let j = 0; j < accessors.length; j++) { - const accessor = accessors[j]; - - if (areEqualKeys(accessor.key, key)) { - accessor.getters.push( - ...(node.kind === "get" ? [node] : []), - ); - accessor.setters.push( - ...(node.kind === "set" ? [node] : []), - ); - found = true; - break; - } - } - if (!found) { - accessors.push({ - key, - getters: node.kind === "get" ? [node] : [], - setters: node.kind === "set" ? [node] : [], - }); - } - found = false; - } - } - - for (const { getters, setters } of accessors) { - if (checkSetWithoutGet && setters.length && !getters.length) { - reportList(setters, "missingGetter"); - } - if (checkGetWithoutSet && getters.length && !setters.length) { - reportList(getters, "missingSetter"); - } - } - } - - /** - * Checks accessor pairs in an object literal. - * @param {ASTNode} node `ObjectExpression` node to check. - * @returns {void} - * @private - */ - function checkObjectLiteral(node) { - checkList(node.properties.filter(p => p.type === "Property")); - } - - /** - * Checks accessor pairs in a property descriptor. - * @param {ASTNode} node Property descriptor `ObjectExpression` node to check. - * @returns {void} - * @private - */ - function checkPropertyDescriptor(node) { - const namesToCheck = new Set( - node.properties - .filter( - p => - p.type === "Property" && - p.kind === "init" && - !p.computed, - ) - .map(({ key }) => key.name), - ); - - const hasGetter = namesToCheck.has("get"); - const hasSetter = namesToCheck.has("set"); - - if (checkSetWithoutGet && hasSetter && !hasGetter) { - report(node, "missingGetter"); - } - if (checkGetWithoutSet && hasGetter && !hasSetter) { - report(node, "missingSetter"); - } - } - - /** - * Checks the given object expression as an object literal and as a possible property descriptor. - * @param {ASTNode} node `ObjectExpression` node to check. - * @returns {void} - * @private - */ - function checkObjectExpression(node) { - checkObjectLiteral(node); - if (isPropertyDescriptor(node)) { - checkPropertyDescriptor(node); - } - } - - /** - * Checks the given class body. - * @param {ASTNode} node `ClassBody` node to check. - * @returns {void} - * @private - */ - function checkClassBody(node) { - const methodDefinitions = node.body.filter( - m => m.type === "MethodDefinition", - ); - - checkList(methodDefinitions.filter(m => m.static)); - checkList(methodDefinitions.filter(m => !m.static)); - } - - /** - * Checks the given type. - * @param {ASTNode} node `TSTypeLiteral` or `TSInterfaceBody` node to check. - * @returns {void} - * @private - */ - function checkType(node) { - const members = - node.type === "TSTypeLiteral" ? node.members : node.body; - const methodDefinitions = members.filter( - m => m.type === "TSMethodSignature", - ); - - checkList(methodDefinitions); - } - - const listeners = {}; - - if (checkSetWithoutGet || checkGetWithoutSet) { - listeners.ObjectExpression = checkObjectExpression; - if (enforceForClassMembers) { - listeners.ClassBody = checkClassBody; - } - if (enforceForTSTypes) { - listeners["TSTypeLiteral, TSInterfaceBody"] = checkType; - } - } - - return listeners; - }, + meta: { + type: "suggestion", + + docs: { + description: "Enforce getter and setter pairs in objects and classes", + recommended: false, + url: "https://eslint.org/docs/latest/rules/accessor-pairs" + }, + + schema: [{ + type: "object", + properties: { + getWithoutSet: { + type: "boolean", + default: false + }, + setWithoutGet: { + type: "boolean", + default: true + }, + enforceForClassMembers: { + type: "boolean", + default: true + } + }, + additionalProperties: false + }], + + messages: { + missingGetterInPropertyDescriptor: "Getter is not present in property descriptor.", + missingSetterInPropertyDescriptor: "Setter is not present in property descriptor.", + missingGetterInObjectLiteral: "Getter is not present for {{ name }}.", + missingSetterInObjectLiteral: "Setter is not present for {{ name }}.", + missingGetterInClass: "Getter is not present for class {{ name }}.", + missingSetterInClass: "Setter is not present for class {{ name }}." + } + }, + create(context) { + const config = context.options[0] || {}; + const checkGetWithoutSet = config.getWithoutSet === true; + const checkSetWithoutGet = config.setWithoutGet !== false; + const enforceForClassMembers = config.enforceForClassMembers !== false; + const sourceCode = context.sourceCode; + + /** + * Reports the given node. + * @param {ASTNode} node The node to report. + * @param {string} messageKind "missingGetter" or "missingSetter". + * @returns {void} + * @private + */ + function report(node, messageKind) { + if (node.type === "Property") { + context.report({ + node, + messageId: `${messageKind}InObjectLiteral`, + loc: astUtils.getFunctionHeadLoc(node.value, sourceCode), + data: { name: astUtils.getFunctionNameWithKind(node.value) } + }); + } else if (node.type === "MethodDefinition") { + context.report({ + node, + messageId: `${messageKind}InClass`, + loc: astUtils.getFunctionHeadLoc(node.value, sourceCode), + data: { name: astUtils.getFunctionNameWithKind(node.value) } + }); + } else { + context.report({ + node, + messageId: `${messageKind}InPropertyDescriptor` + }); + } + } + + /** + * Reports each of the nodes in the given list using the same messageId. + * @param {ASTNode[]} nodes Nodes to report. + * @param {string} messageKind "missingGetter" or "missingSetter". + * @returns {void} + * @private + */ + function reportList(nodes, messageKind) { + for (const node of nodes) { + report(node, messageKind); + } + } + + /** + * Checks accessor pairs in the given list of nodes. + * @param {ASTNode[]} nodes The list to check. + * @returns {void} + * @private + */ + function checkList(nodes) { + const accessors = []; + let found = false; + + for (let i = 0; i < nodes.length; i++) { + const node = nodes[i]; + + if (isAccessorKind(node)) { + + // Creates a new `AccessorData` object for the given getter or setter node. + const name = astUtils.getStaticPropertyName(node); + const key = (name !== null) ? name : sourceCode.getTokens(node.key); + + // Merges the given `AccessorData` object into the given accessors list. + for (let j = 0; j < accessors.length; j++) { + const accessor = accessors[j]; + + if (areEqualKeys(accessor.key, key)) { + accessor.getters.push(...node.kind === "get" ? [node] : []); + accessor.setters.push(...node.kind === "set" ? [node] : []); + found = true; + break; + } + } + if (!found) { + accessors.push({ + key, + getters: node.kind === "get" ? [node] : [], + setters: node.kind === "set" ? [node] : [] + }); + } + found = false; + } + } + + for (const { getters, setters } of accessors) { + if (checkSetWithoutGet && setters.length && !getters.length) { + reportList(setters, "missingGetter"); + } + if (checkGetWithoutSet && getters.length && !setters.length) { + reportList(getters, "missingSetter"); + } + } + } + + /** + * Checks accessor pairs in an object literal. + * @param {ASTNode} node `ObjectExpression` node to check. + * @returns {void} + * @private + */ + function checkObjectLiteral(node) { + checkList(node.properties.filter(p => p.type === "Property")); + } + + /** + * Checks accessor pairs in a property descriptor. + * @param {ASTNode} node Property descriptor `ObjectExpression` node to check. + * @returns {void} + * @private + */ + function checkPropertyDescriptor(node) { + const namesToCheck = new Set(node.properties + .filter(p => p.type === "Property" && p.kind === "init" && !p.computed) + .map(({ key }) => key.name)); + + const hasGetter = namesToCheck.has("get"); + const hasSetter = namesToCheck.has("set"); + + if (checkSetWithoutGet && hasSetter && !hasGetter) { + report(node, "missingGetter"); + } + if (checkGetWithoutSet && hasGetter && !hasSetter) { + report(node, "missingSetter"); + } + } + + /** + * Checks the given object expression as an object literal and as a possible property descriptor. + * @param {ASTNode} node `ObjectExpression` node to check. + * @returns {void} + * @private + */ + function checkObjectExpression(node) { + checkObjectLiteral(node); + if (isPropertyDescriptor(node)) { + checkPropertyDescriptor(node); + } + } + + /** + * Checks the given class body. + * @param {ASTNode} node `ClassBody` node to check. + * @returns {void} + * @private + */ + function checkClassBody(node) { + const methodDefinitions = node.body.filter(m => m.type === "MethodDefinition"); + + checkList(methodDefinitions.filter(m => m.static)); + checkList(methodDefinitions.filter(m => !m.static)); + } + + const listeners = {}; + + if (checkSetWithoutGet || checkGetWithoutSet) { + listeners.ObjectExpression = checkObjectExpression; + if (enforceForClassMembers) { + listeners.ClassBody = checkClassBody; + } + } + + return listeners; + } }; diff --git a/node_modules/eslint/lib/rules/array-bracket-newline.js b/node_modules/eslint/lib/rules/array-bracket-newline.js index 56887af13..12ef5b612 100644 --- a/node_modules/eslint/lib/rules/array-bracket-newline.js +++ b/node_modules/eslint/lib/rules/array-bracket-newline.js @@ -12,280 +12,250 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "array-bracket-newline", - url: "https://eslint.style/rules/array-bracket-newline", - }, - }, - ], - }, - type: "layout", + meta: { + deprecated: true, + replacedBy: [], + type: "layout", - docs: { - description: - "Enforce linebreaks after opening and before closing array brackets", - recommended: false, - url: "https://eslint.org/docs/latest/rules/array-bracket-newline", - }, + docs: { + description: "Enforce linebreaks after opening and before closing array brackets", + recommended: false, + url: "https://eslint.org/docs/latest/rules/array-bracket-newline" + }, - fixable: "whitespace", + fixable: "whitespace", - schema: [ - { - oneOf: [ - { - enum: ["always", "never", "consistent"], - }, - { - type: "object", - properties: { - multiline: { - type: "boolean", - }, - minItems: { - type: ["integer", "null"], - minimum: 0, - }, - }, - additionalProperties: false, - }, - ], - }, - ], + schema: [ + { + oneOf: [ + { + enum: ["always", "never", "consistent"] + }, + { + type: "object", + properties: { + multiline: { + type: "boolean" + }, + minItems: { + type: ["integer", "null"], + minimum: 0 + } + }, + additionalProperties: false + } + ] + } + ], - messages: { - unexpectedOpeningLinebreak: - "There should be no linebreak after '['.", - unexpectedClosingLinebreak: - "There should be no linebreak before ']'.", - missingOpeningLinebreak: "A linebreak is required after '['.", - missingClosingLinebreak: "A linebreak is required before ']'.", - }, - }, + messages: { + unexpectedOpeningLinebreak: "There should be no linebreak after '['.", + unexpectedClosingLinebreak: "There should be no linebreak before ']'.", + missingOpeningLinebreak: "A linebreak is required after '['.", + missingClosingLinebreak: "A linebreak is required before ']'." + } + }, - create(context) { - const sourceCode = context.sourceCode; + create(context) { + const sourceCode = context.sourceCode; - //---------------------------------------------------------------------- - // Helpers - //---------------------------------------------------------------------- - /** - * Normalizes a given option value. - * @param {string|Object|undefined} option An option value to parse. - * @returns {{multiline: boolean, minItems: number}} Normalized option object. - */ - function normalizeOptionValue(option) { - let consistent = false; - let multiline = false; - let minItems; + //---------------------------------------------------------------------- + // Helpers + //---------------------------------------------------------------------- - if (option) { - if (option === "consistent") { - consistent = true; - minItems = Number.POSITIVE_INFINITY; - } else if (option === "always" || option.minItems === 0) { - minItems = 0; - } else if (option === "never") { - minItems = Number.POSITIVE_INFINITY; - } else { - multiline = Boolean(option.multiline); - minItems = option.minItems || Number.POSITIVE_INFINITY; - } - } else { - consistent = false; - multiline = true; - minItems = Number.POSITIVE_INFINITY; - } + /** + * Normalizes a given option value. + * @param {string|Object|undefined} option An option value to parse. + * @returns {{multiline: boolean, minItems: number}} Normalized option object. + */ + function normalizeOptionValue(option) { + let consistent = false; + let multiline = false; + let minItems = 0; - return { consistent, multiline, minItems }; - } + if (option) { + if (option === "consistent") { + consistent = true; + minItems = Number.POSITIVE_INFINITY; + } else if (option === "always" || option.minItems === 0) { + minItems = 0; + } else if (option === "never") { + minItems = Number.POSITIVE_INFINITY; + } else { + multiline = Boolean(option.multiline); + minItems = option.minItems || Number.POSITIVE_INFINITY; + } + } else { + consistent = false; + multiline = true; + minItems = Number.POSITIVE_INFINITY; + } - /** - * Normalizes a given option value. - * @param {string|Object|undefined} options An option value to parse. - * @returns {{ArrayExpression: {multiline: boolean, minItems: number}, ArrayPattern: {multiline: boolean, minItems: number}}} Normalized option object. - */ - function normalizeOptions(options) { - const value = normalizeOptionValue(options); + return { consistent, multiline, minItems }; + } - return { ArrayExpression: value, ArrayPattern: value }; - } + /** + * Normalizes a given option value. + * @param {string|Object|undefined} options An option value to parse. + * @returns {{ArrayExpression: {multiline: boolean, minItems: number}, ArrayPattern: {multiline: boolean, minItems: number}}} Normalized option object. + */ + function normalizeOptions(options) { + const value = normalizeOptionValue(options); - /** - * Reports that there shouldn't be a linebreak after the first token - * @param {ASTNode} node The node to report in the event of an error. - * @param {Token} token The token to use for the report. - * @returns {void} - */ - function reportNoBeginningLinebreak(node, token) { - context.report({ - node, - loc: token.loc, - messageId: "unexpectedOpeningLinebreak", - fix(fixer) { - const nextToken = sourceCode.getTokenAfter(token, { - includeComments: true, - }); + return { ArrayExpression: value, ArrayPattern: value }; + } - if (astUtils.isCommentToken(nextToken)) { - return null; - } + /** + * Reports that there shouldn't be a linebreak after the first token + * @param {ASTNode} node The node to report in the event of an error. + * @param {Token} token The token to use for the report. + * @returns {void} + */ + function reportNoBeginningLinebreak(node, token) { + context.report({ + node, + loc: token.loc, + messageId: "unexpectedOpeningLinebreak", + fix(fixer) { + const nextToken = sourceCode.getTokenAfter(token, { includeComments: true }); - return fixer.removeRange([ - token.range[1], - nextToken.range[0], - ]); - }, - }); - } + if (astUtils.isCommentToken(nextToken)) { + return null; + } - /** - * Reports that there shouldn't be a linebreak before the last token - * @param {ASTNode} node The node to report in the event of an error. - * @param {Token} token The token to use for the report. - * @returns {void} - */ - function reportNoEndingLinebreak(node, token) { - context.report({ - node, - loc: token.loc, - messageId: "unexpectedClosingLinebreak", - fix(fixer) { - const previousToken = sourceCode.getTokenBefore(token, { - includeComments: true, - }); + return fixer.removeRange([token.range[1], nextToken.range[0]]); + } + }); + } - if (astUtils.isCommentToken(previousToken)) { - return null; - } + /** + * Reports that there shouldn't be a linebreak before the last token + * @param {ASTNode} node The node to report in the event of an error. + * @param {Token} token The token to use for the report. + * @returns {void} + */ + function reportNoEndingLinebreak(node, token) { + context.report({ + node, + loc: token.loc, + messageId: "unexpectedClosingLinebreak", + fix(fixer) { + const previousToken = sourceCode.getTokenBefore(token, { includeComments: true }); - return fixer.removeRange([ - previousToken.range[1], - token.range[0], - ]); - }, - }); - } + if (astUtils.isCommentToken(previousToken)) { + return null; + } - /** - * Reports that there should be a linebreak after the first token - * @param {ASTNode} node The node to report in the event of an error. - * @param {Token} token The token to use for the report. - * @returns {void} - */ - function reportRequiredBeginningLinebreak(node, token) { - context.report({ - node, - loc: token.loc, - messageId: "missingOpeningLinebreak", - fix(fixer) { - return fixer.insertTextAfter(token, "\n"); - }, - }); - } + return fixer.removeRange([previousToken.range[1], token.range[0]]); + } + }); + } - /** - * Reports that there should be a linebreak before the last token - * @param {ASTNode} node The node to report in the event of an error. - * @param {Token} token The token to use for the report. - * @returns {void} - */ - function reportRequiredEndingLinebreak(node, token) { - context.report({ - node, - loc: token.loc, - messageId: "missingClosingLinebreak", - fix(fixer) { - return fixer.insertTextBefore(token, "\n"); - }, - }); - } + /** + * Reports that there should be a linebreak after the first token + * @param {ASTNode} node The node to report in the event of an error. + * @param {Token} token The token to use for the report. + * @returns {void} + */ + function reportRequiredBeginningLinebreak(node, token) { + context.report({ + node, + loc: token.loc, + messageId: "missingOpeningLinebreak", + fix(fixer) { + return fixer.insertTextAfter(token, "\n"); + } + }); + } - /** - * Reports a given node if it violated this rule. - * @param {ASTNode} node A node to check. This is an ArrayExpression node or an ArrayPattern node. - * @returns {void} - */ - function check(node) { - const elements = node.elements; - const normalizedOptions = normalizeOptions(context.options[0]); - const options = normalizedOptions[node.type]; - const openBracket = sourceCode.getFirstToken(node); - const closeBracket = sourceCode.getLastToken(node); - const firstIncComment = sourceCode.getTokenAfter(openBracket, { - includeComments: true, - }); - const lastIncComment = sourceCode.getTokenBefore(closeBracket, { - includeComments: true, - }); - const first = sourceCode.getTokenAfter(openBracket); - const last = sourceCode.getTokenBefore(closeBracket); + /** + * Reports that there should be a linebreak before the last token + * @param {ASTNode} node The node to report in the event of an error. + * @param {Token} token The token to use for the report. + * @returns {void} + */ + function reportRequiredEndingLinebreak(node, token) { + context.report({ + node, + loc: token.loc, + messageId: "missingClosingLinebreak", + fix(fixer) { + return fixer.insertTextBefore(token, "\n"); + } + }); + } - const needsLinebreaks = - elements.length >= options.minItems || - (options.multiline && - elements.length > 0 && - firstIncComment.loc.start.line !== - lastIncComment.loc.end.line) || - (elements.length === 0 && - firstIncComment.type === "Block" && - firstIncComment.loc.start.line !== - lastIncComment.loc.end.line && - firstIncComment === lastIncComment) || - (options.consistent && - openBracket.loc.end.line !== first.loc.start.line); + /** + * Reports a given node if it violated this rule. + * @param {ASTNode} node A node to check. This is an ArrayExpression node or an ArrayPattern node. + * @returns {void} + */ + function check(node) { + const elements = node.elements; + const normalizedOptions = normalizeOptions(context.options[0]); + const options = normalizedOptions[node.type]; + const openBracket = sourceCode.getFirstToken(node); + const closeBracket = sourceCode.getLastToken(node); + const firstIncComment = sourceCode.getTokenAfter(openBracket, { includeComments: true }); + const lastIncComment = sourceCode.getTokenBefore(closeBracket, { includeComments: true }); + const first = sourceCode.getTokenAfter(openBracket); + const last = sourceCode.getTokenBefore(closeBracket); - /* - * Use tokens or comments to check multiline or not. - * But use only tokens to check whether linebreaks are needed. - * This allows: - * var arr = [ // eslint-disable-line foo - * 'a' - * ] - */ + const needsLinebreaks = ( + elements.length >= options.minItems || + ( + options.multiline && + elements.length > 0 && + firstIncComment.loc.start.line !== lastIncComment.loc.end.line + ) || + ( + elements.length === 0 && + firstIncComment.type === "Block" && + firstIncComment.loc.start.line !== lastIncComment.loc.end.line && + firstIncComment === lastIncComment + ) || + ( + options.consistent && + openBracket.loc.end.line !== first.loc.start.line + ) + ); - if (needsLinebreaks) { - if (astUtils.isTokenOnSameLine(openBracket, first)) { - reportRequiredBeginningLinebreak(node, openBracket); - } - if (astUtils.isTokenOnSameLine(last, closeBracket)) { - reportRequiredEndingLinebreak(node, closeBracket); - } - } else { - if (!astUtils.isTokenOnSameLine(openBracket, first)) { - reportNoBeginningLinebreak(node, openBracket); - } - if (!astUtils.isTokenOnSameLine(last, closeBracket)) { - reportNoEndingLinebreak(node, closeBracket); - } - } - } + /* + * Use tokens or comments to check multiline or not. + * But use only tokens to check whether linebreaks are needed. + * This allows: + * var arr = [ // eslint-disable-line foo + * 'a' + * ] + */ - //---------------------------------------------------------------------- - // Public - //---------------------------------------------------------------------- + if (needsLinebreaks) { + if (astUtils.isTokenOnSameLine(openBracket, first)) { + reportRequiredBeginningLinebreak(node, openBracket); + } + if (astUtils.isTokenOnSameLine(last, closeBracket)) { + reportRequiredEndingLinebreak(node, closeBracket); + } + } else { + if (!astUtils.isTokenOnSameLine(openBracket, first)) { + reportNoBeginningLinebreak(node, openBracket); + } + if (!astUtils.isTokenOnSameLine(last, closeBracket)) { + reportNoEndingLinebreak(node, closeBracket); + } + } + } - return { - ArrayPattern: check, - ArrayExpression: check, - }; - }, + //---------------------------------------------------------------------- + // Public + //---------------------------------------------------------------------- + + return { + ArrayPattern: check, + ArrayExpression: check + }; + } }; diff --git a/node_modules/eslint/lib/rules/array-bracket-spacing.js b/node_modules/eslint/lib/rules/array-bracket-spacing.js index dbe88f1b4..9dd3ffd90 100644 --- a/node_modules/eslint/lib/rules/array-bracket-spacing.js +++ b/node_modules/eslint/lib/rules/array-bracket-spacing.js @@ -11,291 +11,234 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "array-bracket-spacing", - url: "https://eslint.style/rules/array-bracket-spacing", - }, - }, - ], - }, - type: "layout", - - docs: { - description: "Enforce consistent spacing inside array brackets", - recommended: false, - url: "https://eslint.org/docs/latest/rules/array-bracket-spacing", - }, - - fixable: "whitespace", - - schema: [ - { - enum: ["always", "never"], - }, - { - type: "object", - properties: { - singleValue: { - type: "boolean", - }, - objectsInArrays: { - type: "boolean", - }, - arraysInArrays: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - - messages: { - unexpectedSpaceAfter: - "There should be no space after '{{tokenValue}}'.", - unexpectedSpaceBefore: - "There should be no space before '{{tokenValue}}'.", - missingSpaceAfter: "A space is required after '{{tokenValue}}'.", - missingSpaceBefore: "A space is required before '{{tokenValue}}'.", - }, - }, - create(context) { - const spaced = context.options[0] === "always", - sourceCode = context.sourceCode; - - /** - * Determines whether an option is set, relative to the spacing option. - * If spaced is "always", then check whether option is set to false. - * If spaced is "never", then check whether option is set to true. - * @param {Object} option The option to exclude. - * @returns {boolean} Whether or not the property is excluded. - */ - function isOptionSet(option) { - return context.options[1] - ? context.options[1][option] === !spaced - : false; - } - - const options = { - spaced, - singleElementException: isOptionSet("singleValue"), - objectsInArraysException: isOptionSet("objectsInArrays"), - arraysInArraysException: isOptionSet("arraysInArrays"), - }; - - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - /** - * Reports that there shouldn't be a space after the first token - * @param {ASTNode} node The node to report in the event of an error. - * @param {Token} token The token to use for the report. - * @returns {void} - */ - function reportNoBeginningSpace(node, token) { - const nextToken = sourceCode.getTokenAfter(token); - - context.report({ - node, - loc: { start: token.loc.end, end: nextToken.loc.start }, - messageId: "unexpectedSpaceAfter", - data: { - tokenValue: token.value, - }, - fix(fixer) { - return fixer.removeRange([ - token.range[1], - nextToken.range[0], - ]); - }, - }); - } - - /** - * Reports that there shouldn't be a space before the last token - * @param {ASTNode} node The node to report in the event of an error. - * @param {Token} token The token to use for the report. - * @returns {void} - */ - function reportNoEndingSpace(node, token) { - const previousToken = sourceCode.getTokenBefore(token); - - context.report({ - node, - loc: { start: previousToken.loc.end, end: token.loc.start }, - messageId: "unexpectedSpaceBefore", - data: { - tokenValue: token.value, - }, - fix(fixer) { - return fixer.removeRange([ - previousToken.range[1], - token.range[0], - ]); - }, - }); - } - - /** - * Reports that there should be a space after the first token - * @param {ASTNode} node The node to report in the event of an error. - * @param {Token} token The token to use for the report. - * @returns {void} - */ - function reportRequiredBeginningSpace(node, token) { - context.report({ - node, - loc: token.loc, - messageId: "missingSpaceAfter", - data: { - tokenValue: token.value, - }, - fix(fixer) { - return fixer.insertTextAfter(token, " "); - }, - }); - } - - /** - * Reports that there should be a space before the last token - * @param {ASTNode} node The node to report in the event of an error. - * @param {Token} token The token to use for the report. - * @returns {void} - */ - function reportRequiredEndingSpace(node, token) { - context.report({ - node, - loc: token.loc, - messageId: "missingSpaceBefore", - data: { - tokenValue: token.value, - }, - fix(fixer) { - return fixer.insertTextBefore(token, " "); - }, - }); - } - - /** - * Determines if a node is an object type - * @param {ASTNode} node The node to check. - * @returns {boolean} Whether or not the node is an object type. - */ - function isObjectType(node) { - return ( - node && - (node.type === "ObjectExpression" || - node.type === "ObjectPattern") - ); - } - - /** - * Determines if a node is an array type - * @param {ASTNode} node The node to check. - * @returns {boolean} Whether or not the node is an array type. - */ - function isArrayType(node) { - return ( - node && - (node.type === "ArrayExpression" || - node.type === "ArrayPattern") - ); - } - - /** - * Validates the spacing around array brackets - * @param {ASTNode} node The node we're checking for spacing - * @returns {void} - */ - function validateArraySpacing(node) { - if (options.spaced && node.elements.length === 0) { - return; - } - - const first = sourceCode.getFirstToken(node), - second = sourceCode.getFirstToken(node, 1), - last = node.typeAnnotation - ? sourceCode.getTokenBefore(node.typeAnnotation) - : sourceCode.getLastToken(node), - penultimate = sourceCode.getTokenBefore(last), - firstElement = node.elements[0], - lastElement = node.elements.at(-1); - - const openingBracketMustBeSpaced = - (options.objectsInArraysException && - isObjectType(firstElement)) || - (options.arraysInArraysException && - isArrayType(firstElement)) || - (options.singleElementException && node.elements.length === 1) - ? !options.spaced - : options.spaced; - - const closingBracketMustBeSpaced = - (options.objectsInArraysException && - isObjectType(lastElement)) || - (options.arraysInArraysException && isArrayType(lastElement)) || - (options.singleElementException && node.elements.length === 1) - ? !options.spaced - : options.spaced; - - if (astUtils.isTokenOnSameLine(first, second)) { - if ( - openingBracketMustBeSpaced && - !sourceCode.isSpaceBetweenTokens(first, second) - ) { - reportRequiredBeginningSpace(node, first); - } - if ( - !openingBracketMustBeSpaced && - sourceCode.isSpaceBetweenTokens(first, second) - ) { - reportNoBeginningSpace(node, first); - } - } - - if ( - first !== penultimate && - astUtils.isTokenOnSameLine(penultimate, last) - ) { - if ( - closingBracketMustBeSpaced && - !sourceCode.isSpaceBetweenTokens(penultimate, last) - ) { - reportRequiredEndingSpace(node, last); - } - if ( - !closingBracketMustBeSpaced && - sourceCode.isSpaceBetweenTokens(penultimate, last) - ) { - reportNoEndingSpace(node, last); - } - } - } - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - return { - ArrayPattern: validateArraySpacing, - ArrayExpression: validateArraySpacing, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Enforce consistent spacing inside array brackets", + recommended: false, + url: "https://eslint.org/docs/latest/rules/array-bracket-spacing" + }, + + fixable: "whitespace", + + schema: [ + { + enum: ["always", "never"] + }, + { + type: "object", + properties: { + singleValue: { + type: "boolean" + }, + objectsInArrays: { + type: "boolean" + }, + arraysInArrays: { + type: "boolean" + } + }, + additionalProperties: false + } + ], + + messages: { + unexpectedSpaceAfter: "There should be no space after '{{tokenValue}}'.", + unexpectedSpaceBefore: "There should be no space before '{{tokenValue}}'.", + missingSpaceAfter: "A space is required after '{{tokenValue}}'.", + missingSpaceBefore: "A space is required before '{{tokenValue}}'." + } + }, + create(context) { + const spaced = context.options[0] === "always", + sourceCode = context.sourceCode; + + /** + * Determines whether an option is set, relative to the spacing option. + * If spaced is "always", then check whether option is set to false. + * If spaced is "never", then check whether option is set to true. + * @param {Object} option The option to exclude. + * @returns {boolean} Whether or not the property is excluded. + */ + function isOptionSet(option) { + return context.options[1] ? context.options[1][option] === !spaced : false; + } + + const options = { + spaced, + singleElementException: isOptionSet("singleValue"), + objectsInArraysException: isOptionSet("objectsInArrays"), + arraysInArraysException: isOptionSet("arraysInArrays") + }; + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + /** + * Reports that there shouldn't be a space after the first token + * @param {ASTNode} node The node to report in the event of an error. + * @param {Token} token The token to use for the report. + * @returns {void} + */ + function reportNoBeginningSpace(node, token) { + const nextToken = sourceCode.getTokenAfter(token); + + context.report({ + node, + loc: { start: token.loc.end, end: nextToken.loc.start }, + messageId: "unexpectedSpaceAfter", + data: { + tokenValue: token.value + }, + fix(fixer) { + return fixer.removeRange([token.range[1], nextToken.range[0]]); + } + }); + } + + /** + * Reports that there shouldn't be a space before the last token + * @param {ASTNode} node The node to report in the event of an error. + * @param {Token} token The token to use for the report. + * @returns {void} + */ + function reportNoEndingSpace(node, token) { + const previousToken = sourceCode.getTokenBefore(token); + + context.report({ + node, + loc: { start: previousToken.loc.end, end: token.loc.start }, + messageId: "unexpectedSpaceBefore", + data: { + tokenValue: token.value + }, + fix(fixer) { + return fixer.removeRange([previousToken.range[1], token.range[0]]); + } + }); + } + + /** + * Reports that there should be a space after the first token + * @param {ASTNode} node The node to report in the event of an error. + * @param {Token} token The token to use for the report. + * @returns {void} + */ + function reportRequiredBeginningSpace(node, token) { + context.report({ + node, + loc: token.loc, + messageId: "missingSpaceAfter", + data: { + tokenValue: token.value + }, + fix(fixer) { + return fixer.insertTextAfter(token, " "); + } + }); + } + + /** + * Reports that there should be a space before the last token + * @param {ASTNode} node The node to report in the event of an error. + * @param {Token} token The token to use for the report. + * @returns {void} + */ + function reportRequiredEndingSpace(node, token) { + context.report({ + node, + loc: token.loc, + messageId: "missingSpaceBefore", + data: { + tokenValue: token.value + }, + fix(fixer) { + return fixer.insertTextBefore(token, " "); + } + }); + } + + /** + * Determines if a node is an object type + * @param {ASTNode} node The node to check. + * @returns {boolean} Whether or not the node is an object type. + */ + function isObjectType(node) { + return node && (node.type === "ObjectExpression" || node.type === "ObjectPattern"); + } + + /** + * Determines if a node is an array type + * @param {ASTNode} node The node to check. + * @returns {boolean} Whether or not the node is an array type. + */ + function isArrayType(node) { + return node && (node.type === "ArrayExpression" || node.type === "ArrayPattern"); + } + + /** + * Validates the spacing around array brackets + * @param {ASTNode} node The node we're checking for spacing + * @returns {void} + */ + function validateArraySpacing(node) { + if (options.spaced && node.elements.length === 0) { + return; + } + + const first = sourceCode.getFirstToken(node), + second = sourceCode.getFirstToken(node, 1), + last = node.typeAnnotation + ? sourceCode.getTokenBefore(node.typeAnnotation) + : sourceCode.getLastToken(node), + penultimate = sourceCode.getTokenBefore(last), + firstElement = node.elements[0], + lastElement = node.elements[node.elements.length - 1]; + + const openingBracketMustBeSpaced = + options.objectsInArraysException && isObjectType(firstElement) || + options.arraysInArraysException && isArrayType(firstElement) || + options.singleElementException && node.elements.length === 1 + ? !options.spaced : options.spaced; + + const closingBracketMustBeSpaced = + options.objectsInArraysException && isObjectType(lastElement) || + options.arraysInArraysException && isArrayType(lastElement) || + options.singleElementException && node.elements.length === 1 + ? !options.spaced : options.spaced; + + if (astUtils.isTokenOnSameLine(first, second)) { + if (openingBracketMustBeSpaced && !sourceCode.isSpaceBetweenTokens(first, second)) { + reportRequiredBeginningSpace(node, first); + } + if (!openingBracketMustBeSpaced && sourceCode.isSpaceBetweenTokens(first, second)) { + reportNoBeginningSpace(node, first); + } + } + + if (first !== penultimate && astUtils.isTokenOnSameLine(penultimate, last)) { + if (closingBracketMustBeSpaced && !sourceCode.isSpaceBetweenTokens(penultimate, last)) { + reportRequiredEndingSpace(node, last); + } + if (!closingBracketMustBeSpaced && sourceCode.isSpaceBetweenTokens(penultimate, last)) { + reportNoEndingSpace(node, last); + } + } + } + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + return { + ArrayPattern: validateArraySpacing, + ArrayExpression: validateArraySpacing + }; + } }; diff --git a/node_modules/eslint/lib/rules/array-callback-return.js b/node_modules/eslint/lib/rules/array-callback-return.js index e7e31308f..6d8f258fa 100644 --- a/node_modules/eslint/lib/rules/array-callback-return.js +++ b/node_modules/eslint/lib/rules/array-callback-return.js @@ -16,8 +16,7 @@ const astUtils = require("./utils/ast-utils"); //------------------------------------------------------------------------------ const TARGET_NODE_TYPE = /^(?:Arrow)?FunctionExpression$/u; -const TARGET_METHODS = - /^(?:every|filter|find(?:Last)?(?:Index)?|flatMap|forEach|map|reduce(?:Right)?|some|sort|toSorted)$/u; +const TARGET_METHODS = /^(?:every|filter|find(?:Last)?(?:Index)?|flatMap|forEach|map|reduce(?:Right)?|some|sort|toSorted)$/u; /** * Checks a given node is a member access which has the specified name's @@ -27,7 +26,7 @@ const TARGET_METHODS = * the specified name's property. The node may be a `(Chain|Member)Expression` node. */ function isTargetMethod(node) { - return astUtils.isSpecificMemberAccess(node, null, TARGET_METHODS); + return astUtils.isSpecificMemberAccess(node, null, TARGET_METHODS); } /** @@ -36,13 +35,14 @@ function isTargetMethod(node) { * @returns {boolean} True if any segment is reachable; false otherwise. */ function isAnySegmentReachable(segments) { - for (const segment of segments) { - if (segment.reachable) { - return true; - } - } - return false; + for (const segment of segments) { + if (segment.reachable) { + return true; + } + } + + return false; } /** @@ -52,10 +52,10 @@ function isAnySegmentReachable(segments) { * or else `Array.prototype.` if it is an instance method. */ function fullMethodName(arrayMethodName) { - if (["from", "of", "isArray"].includes(arrayMethodName)) { - return "Array.".concat(arrayMethodName); - } - return "Array.prototype.".concat(arrayMethodName); + if (["from", "of", "isArray"].includes(arrayMethodName)) { + return "Array.".concat(arrayMethodName); + } + return "Array.prototype.".concat(arrayMethodName); } /** @@ -67,72 +67,73 @@ function fullMethodName(arrayMethodName) { * null otherwise. */ function getArrayMethodName(node) { - let currentNode = node; - - while (currentNode) { - const parent = currentNode.parent; - - switch (parent.type) { - /* - * Looks up the destination. e.g., - * foo.every(nativeFoo || function foo() { ... }); - */ - case "LogicalExpression": - case "ConditionalExpression": - case "ChainExpression": - currentNode = parent; - break; - - /* - * If the upper function is IIFE, checks the destination of the return value. - * e.g. - * foo.every((function() { - * // setup... - * return function callback() { ... }; - * })()); - */ - case "ReturnStatement": { - const func = astUtils.getUpperFunction(parent); - - if (func === null || !astUtils.isCallee(func)) { - return null; - } - currentNode = func.parent; - break; - } - - /* - * e.g. - * Array.from([], function() {}); - * list.every(function() {}); - */ - case "CallExpression": - if (astUtils.isArrayFromMethod(parent.callee)) { - if ( - parent.arguments.length >= 2 && - parent.arguments[1] === currentNode - ) { - return "from"; - } - } - if (isTargetMethod(parent.callee)) { - if ( - parent.arguments.length >= 1 && - parent.arguments[0] === currentNode - ) { - return astUtils.getStaticPropertyName(parent.callee); - } - } - return null; - - // Otherwise this node is not target. - default: - return null; - } - } - - /* c8 ignore next */ - return null; + let currentNode = node; + + while (currentNode) { + const parent = currentNode.parent; + + switch (parent.type) { + + /* + * Looks up the destination. e.g., + * foo.every(nativeFoo || function foo() { ... }); + */ + case "LogicalExpression": + case "ConditionalExpression": + case "ChainExpression": + currentNode = parent; + break; + + /* + * If the upper function is IIFE, checks the destination of the return value. + * e.g. + * foo.every((function() { + * // setup... + * return function callback() { ... }; + * })()); + */ + case "ReturnStatement": { + const func = astUtils.getUpperFunction(parent); + + if (func === null || !astUtils.isCallee(func)) { + return null; + } + currentNode = func.parent; + break; + } + + /* + * e.g. + * Array.from([], function() {}); + * list.every(function() {}); + */ + case "CallExpression": + if (astUtils.isArrayFromMethod(parent.callee)) { + if ( + parent.arguments.length >= 2 && + parent.arguments[1] === currentNode + ) { + return "from"; + } + } + if (isTargetMethod(parent.callee)) { + if ( + parent.arguments.length >= 1 && + parent.arguments[0] === currentNode + ) { + return astUtils.getStaticPropertyName(parent.callee); + } + } + return null; + + // Otherwise this node is not target. + default: + return null; + } + } + + /* c8 ignore next */ + return null; } /** @@ -141,7 +142,7 @@ function getArrayMethodName(node) { * @returns {boolean} - `true` if the node is a void expression */ function isExpressionVoid(node) { - return node.type === "UnaryExpression" && node.operator === "void"; + return node.type === "UnaryExpression" && node.operator === "void"; } /** @@ -152,40 +153,39 @@ function isExpressionVoid(node) { * @returns {Array} - An array of fix objects to apply to the node. */ function voidPrependFixer(sourceCode, node, fixer) { - const requiresParens = - // prepending `void ` will fail if the node has a lower precedence than void - astUtils.getPrecedence(node) < - astUtils.getPrecedence({ - type: "UnaryExpression", - operator: "void", - }) && - // check if there are parentheses around the node to avoid redundant parentheses - !astUtils.isParenthesised(sourceCode, node); - - // avoid parentheses issues - const returnOrArrowToken = sourceCode.getTokenBefore( - node, - node.parent.type === "ArrowFunctionExpression" - ? astUtils.isArrowToken - : // isReturnToken - token => token.type === "Keyword" && token.value === "return", - ); - - const firstToken = sourceCode.getTokenAfter(returnOrArrowToken); - - const prependSpace = - // is return token, as => allows void to be adjacent - returnOrArrowToken.value === "return" && - // If two tokens (return and "(") are adjacent - returnOrArrowToken.range[1] === firstToken.range[0]; - - return [ - fixer.insertTextBefore( - firstToken, - `${prependSpace ? " " : ""}void ${requiresParens ? "(" : ""}`, - ), - fixer.insertTextAfter(node, requiresParens ? ")" : ""), - ]; + + const requiresParens = + + // prepending `void ` will fail if the node has a lower precedence than void + astUtils.getPrecedence(node) < astUtils.getPrecedence({ type: "UnaryExpression", operator: "void" }) && + + // check if there are parentheses around the node to avoid redundant parentheses + !astUtils.isParenthesised(sourceCode, node); + + // avoid parentheses issues + const returnOrArrowToken = sourceCode.getTokenBefore( + node, + node.parent.type === "ArrowFunctionExpression" + ? astUtils.isArrowToken + + // isReturnToken + : token => token.type === "Keyword" && token.value === "return" + ); + + const firstToken = sourceCode.getTokenAfter(returnOrArrowToken); + + const prependSpace = + + // is return token, as => allows void to be adjacent + returnOrArrowToken.value === "return" && + + // If two tokens (return and "(") are adjacent + returnOrArrowToken.range[1] === firstToken.range[0]; + + return [ + fixer.insertTextBefore(firstToken, `${prependSpace ? " " : ""}void ${requiresParens ? "(" : ""}`), + fixer.insertTextAfter(node, requiresParens ? ")" : "") + ]; } /** @@ -196,298 +196,251 @@ function voidPrependFixer(sourceCode, node, fixer) { * @returns {Array} - An array of fix objects to apply to the node. */ function curlyWrapFixer(sourceCode, node, fixer) { - const arrowToken = sourceCode.getTokenBefore( - node.body, - astUtils.isArrowToken, - ); - const firstToken = sourceCode.getTokenAfter(arrowToken); - const lastToken = sourceCode.getLastToken(node); - - return [ - fixer.insertTextBefore(firstToken, "{"), - fixer.insertTextAfter(lastToken, "}"), - ]; + const arrowToken = sourceCode.getTokenBefore(node.body, astUtils.isArrowToken); + const firstToken = sourceCode.getTokenAfter(arrowToken); + const lastToken = sourceCode.getLastToken(node); + + return [ + fixer.insertTextBefore(firstToken, "{"), + fixer.insertTextAfter(lastToken, "}") + ]; } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - defaultOptions: [ - { - allowImplicit: false, - checkForEach: false, - allowVoid: false, - }, - ], - - docs: { - description: - "Enforce `return` statements in callbacks of array methods", - recommended: false, - url: "https://eslint.org/docs/latest/rules/array-callback-return", - }, - - hasSuggestions: true, - - schema: [ - { - type: "object", - properties: { - allowImplicit: { - type: "boolean", - }, - checkForEach: { - type: "boolean", - }, - allowVoid: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - - messages: { - expectedAtEnd: - "{{arrayMethodName}}() expects a value to be returned at the end of {{name}}.", - expectedInside: - "{{arrayMethodName}}() expects a return value from {{name}}.", - expectedReturnValue: - "{{arrayMethodName}}() expects a return value from {{name}}.", - expectedNoReturnValue: - "{{arrayMethodName}}() expects no useless return value from {{name}}.", - wrapBraces: "Wrap the expression in `{}`.", - prependVoid: "Prepend `void` to the expression.", - }, - }, - - create(context) { - const [options] = context.options; - const sourceCode = context.sourceCode; - - let funcInfo = { - arrayMethodName: null, - upper: null, - codePath: null, - hasReturn: false, - shouldCheck: false, - node: null, - }; - - /** - * Checks whether or not the last code path segment is reachable. - * Then reports this function if the segment is reachable. - * - * If the last code path segment is reachable, there are paths which are not - * returned or thrown. - * @param {ASTNode} node A node to check. - * @returns {void} - */ - function checkLastSegment(node) { - if (!funcInfo.shouldCheck) { - return; - } - - const messageAndSuggestions = { messageId: "", suggest: [] }; - - if (funcInfo.arrayMethodName === "forEach") { - if ( - options.checkForEach && - node.type === "ArrowFunctionExpression" && - node.expression - ) { - if (options.allowVoid) { - if (isExpressionVoid(node.body)) { - return; - } - - messageAndSuggestions.messageId = - "expectedNoReturnValue"; - messageAndSuggestions.suggest = [ - { - messageId: "wrapBraces", - fix(fixer) { - return curlyWrapFixer( - sourceCode, - node, - fixer, - ); - }, - }, - { - messageId: "prependVoid", - fix(fixer) { - return voidPrependFixer( - sourceCode, - node.body, - fixer, - ); - }, - }, - ]; - } else { - messageAndSuggestions.messageId = - "expectedNoReturnValue"; - messageAndSuggestions.suggest = [ - { - messageId: "wrapBraces", - fix(fixer) { - return curlyWrapFixer( - sourceCode, - node, - fixer, - ); - }, - }, - ]; - } - } - } else { - if ( - node.body.type === "BlockStatement" && - isAnySegmentReachable(funcInfo.currentSegments) - ) { - messageAndSuggestions.messageId = funcInfo.hasReturn - ? "expectedAtEnd" - : "expectedInside"; - } - } - - if (messageAndSuggestions.messageId) { - const name = astUtils.getFunctionNameWithKind(node); - - context.report({ - node, - loc: astUtils.getFunctionHeadLoc(node, sourceCode), - messageId: messageAndSuggestions.messageId, - data: { - name, - arrayMethodName: fullMethodName( - funcInfo.arrayMethodName, - ), - }, - suggest: - messageAndSuggestions.suggest.length !== 0 - ? messageAndSuggestions.suggest - : null, - }); - } - } - - return { - // Stacks this function's information. - onCodePathStart(codePath, node) { - let methodName = null; - - if (TARGET_NODE_TYPE.test(node.type)) { - methodName = getArrayMethodName(node); - } - - funcInfo = { - arrayMethodName: methodName, - upper: funcInfo, - codePath, - hasReturn: false, - shouldCheck: methodName && !node.async && !node.generator, - node, - currentSegments: new Set(), - }; - }, - - // Pops this function's information. - onCodePathEnd() { - funcInfo = funcInfo.upper; - }, - - onUnreachableCodePathSegmentStart(segment) { - funcInfo.currentSegments.add(segment); - }, - - onUnreachableCodePathSegmentEnd(segment) { - funcInfo.currentSegments.delete(segment); - }, - - onCodePathSegmentStart(segment) { - funcInfo.currentSegments.add(segment); - }, - - onCodePathSegmentEnd(segment) { - funcInfo.currentSegments.delete(segment); - }, - - // Checks the return statement is valid. - ReturnStatement(node) { - if (!funcInfo.shouldCheck) { - return; - } - - funcInfo.hasReturn = true; - - const messageAndSuggestions = { messageId: "", suggest: [] }; - - if (funcInfo.arrayMethodName === "forEach") { - // if checkForEach: true, returning a value at any path inside a forEach is not allowed - if (options.checkForEach && node.argument) { - if (options.allowVoid) { - if (isExpressionVoid(node.argument)) { - return; - } - - messageAndSuggestions.messageId = - "expectedNoReturnValue"; - messageAndSuggestions.suggest = [ - { - messageId: "prependVoid", - fix(fixer) { - return voidPrependFixer( - sourceCode, - node.argument, - fixer, - ); - }, - }, - ]; - } else { - messageAndSuggestions.messageId = - "expectedNoReturnValue"; - } - } - } else { - // if allowImplicit: false, should also check node.argument - if (!options.allowImplicit && !node.argument) { - messageAndSuggestions.messageId = "expectedReturnValue"; - } - } - - if (messageAndSuggestions.messageId) { - context.report({ - node, - messageId: messageAndSuggestions.messageId, - data: { - name: astUtils.getFunctionNameWithKind( - funcInfo.node, - ), - arrayMethodName: fullMethodName( - funcInfo.arrayMethodName, - ), - }, - suggest: - messageAndSuggestions.suggest.length !== 0 - ? messageAndSuggestions.suggest - : null, - }); - } - }, - - // Reports a given function if the last path is reachable. - "FunctionExpression:exit": checkLastSegment, - "ArrowFunctionExpression:exit": checkLastSegment, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Enforce `return` statements in callbacks of array methods", + recommended: false, + url: "https://eslint.org/docs/latest/rules/array-callback-return" + }, + + // eslint-disable-next-line eslint-plugin/require-meta-has-suggestions -- false positive + hasSuggestions: true, + + schema: [ + { + type: "object", + properties: { + allowImplicit: { + type: "boolean", + default: false + }, + checkForEach: { + type: "boolean", + default: false + }, + allowVoid: { + type: "boolean", + default: false + } + }, + additionalProperties: false + } + ], + + messages: { + expectedAtEnd: "{{arrayMethodName}}() expects a value to be returned at the end of {{name}}.", + expectedInside: "{{arrayMethodName}}() expects a return value from {{name}}.", + expectedReturnValue: "{{arrayMethodName}}() expects a return value from {{name}}.", + expectedNoReturnValue: "{{arrayMethodName}}() expects no useless return value from {{name}}.", + wrapBraces: "Wrap the expression in `{}`.", + prependVoid: "Prepend `void` to the expression." + } + }, + + create(context) { + + const options = context.options[0] || { allowImplicit: false, checkForEach: false, allowVoid: false }; + const sourceCode = context.sourceCode; + + let funcInfo = { + arrayMethodName: null, + upper: null, + codePath: null, + hasReturn: false, + shouldCheck: false, + node: null + }; + + /** + * Checks whether or not the last code path segment is reachable. + * Then reports this function if the segment is reachable. + * + * If the last code path segment is reachable, there are paths which are not + * returned or thrown. + * @param {ASTNode} node A node to check. + * @returns {void} + */ + function checkLastSegment(node) { + + if (!funcInfo.shouldCheck) { + return; + } + + const messageAndSuggestions = { messageId: "", suggest: [] }; + + if (funcInfo.arrayMethodName === "forEach") { + if (options.checkForEach && node.type === "ArrowFunctionExpression" && node.expression) { + + if (options.allowVoid) { + if (isExpressionVoid(node.body)) { + return; + } + + messageAndSuggestions.messageId = "expectedNoReturnValue"; + messageAndSuggestions.suggest = [ + { + messageId: "wrapBraces", + fix(fixer) { + return curlyWrapFixer(sourceCode, node, fixer); + } + }, + { + messageId: "prependVoid", + fix(fixer) { + return voidPrependFixer(sourceCode, node.body, fixer); + } + } + ]; + } else { + messageAndSuggestions.messageId = "expectedNoReturnValue"; + messageAndSuggestions.suggest = [{ + messageId: "wrapBraces", + fix(fixer) { + return curlyWrapFixer(sourceCode, node, fixer); + } + }]; + } + } + } else { + if (node.body.type === "BlockStatement" && isAnySegmentReachable(funcInfo.currentSegments)) { + messageAndSuggestions.messageId = funcInfo.hasReturn ? "expectedAtEnd" : "expectedInside"; + } + } + + if (messageAndSuggestions.messageId) { + const name = astUtils.getFunctionNameWithKind(node); + + context.report({ + node, + loc: astUtils.getFunctionHeadLoc(node, sourceCode), + messageId: messageAndSuggestions.messageId, + data: { name, arrayMethodName: fullMethodName(funcInfo.arrayMethodName) }, + suggest: messageAndSuggestions.suggest.length !== 0 ? messageAndSuggestions.suggest : null + }); + } + } + + return { + + // Stacks this function's information. + onCodePathStart(codePath, node) { + + let methodName = null; + + if (TARGET_NODE_TYPE.test(node.type)) { + methodName = getArrayMethodName(node); + } + + funcInfo = { + arrayMethodName: methodName, + upper: funcInfo, + codePath, + hasReturn: false, + shouldCheck: + methodName && + !node.async && + !node.generator, + node, + currentSegments: new Set() + }; + }, + + // Pops this function's information. + onCodePathEnd() { + funcInfo = funcInfo.upper; + }, + + onUnreachableCodePathSegmentStart(segment) { + funcInfo.currentSegments.add(segment); + }, + + onUnreachableCodePathSegmentEnd(segment) { + funcInfo.currentSegments.delete(segment); + }, + + onCodePathSegmentStart(segment) { + funcInfo.currentSegments.add(segment); + }, + + onCodePathSegmentEnd(segment) { + funcInfo.currentSegments.delete(segment); + }, + + + // Checks the return statement is valid. + ReturnStatement(node) { + + if (!funcInfo.shouldCheck) { + return; + } + + funcInfo.hasReturn = true; + + const messageAndSuggestions = { messageId: "", suggest: [] }; + + if (funcInfo.arrayMethodName === "forEach") { + + // if checkForEach: true, returning a value at any path inside a forEach is not allowed + if (options.checkForEach && node.argument) { + + if (options.allowVoid) { + if (isExpressionVoid(node.argument)) { + return; + } + + messageAndSuggestions.messageId = "expectedNoReturnValue"; + messageAndSuggestions.suggest = [{ + messageId: "prependVoid", + fix(fixer) { + return voidPrependFixer(sourceCode, node.argument, fixer); + } + }]; + } else { + messageAndSuggestions.messageId = "expectedNoReturnValue"; + } + } + } else { + + // if allowImplicit: false, should also check node.argument + if (!options.allowImplicit && !node.argument) { + messageAndSuggestions.messageId = "expectedReturnValue"; + } + } + + if (messageAndSuggestions.messageId) { + context.report({ + node, + messageId: messageAndSuggestions.messageId, + data: { + name: astUtils.getFunctionNameWithKind(funcInfo.node), + arrayMethodName: fullMethodName(funcInfo.arrayMethodName) + }, + suggest: messageAndSuggestions.suggest.length !== 0 ? messageAndSuggestions.suggest : null + }); + } + }, + + // Reports a given function if the last path is reachable. + "FunctionExpression:exit": checkLastSegment, + "ArrowFunctionExpression:exit": checkLastSegment + }; + } }; diff --git a/node_modules/eslint/lib/rules/array-element-newline.js b/node_modules/eslint/lib/rules/array-element-newline.js index d687cba01..504fe04a0 100644 --- a/node_modules/eslint/lib/rules/array-element-newline.js +++ b/node_modules/eslint/lib/rules/array-element-newline.js @@ -12,363 +12,300 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "array-element-newline", - url: "https://eslint.style/rules/array-element-newline", - }, - }, - ], - }, - type: "layout", - - docs: { - description: "Enforce line breaks after each array element", - recommended: false, - url: "https://eslint.org/docs/latest/rules/array-element-newline", - }, - - fixable: "whitespace", - - schema: { - definitions: { - basicConfig: { - oneOf: [ - { - enum: ["always", "never", "consistent"], - }, - { - type: "object", - properties: { - multiline: { - type: "boolean", - }, - minItems: { - type: ["integer", "null"], - minimum: 0, - }, - }, - additionalProperties: false, - }, - ], - }, - }, - type: "array", - items: [ - { - oneOf: [ - { - $ref: "#/definitions/basicConfig", - }, - { - type: "object", - properties: { - ArrayExpression: { - $ref: "#/definitions/basicConfig", - }, - ArrayPattern: { - $ref: "#/definitions/basicConfig", - }, - }, - additionalProperties: false, - minProperties: 1, - }, - ], - }, - ], - }, - - messages: { - unexpectedLineBreak: "There should be no linebreak here.", - missingLineBreak: "There should be a linebreak after this element.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - //---------------------------------------------------------------------- - // Helpers - //---------------------------------------------------------------------- - - /** - * Normalizes a given option value. - * @param {string|Object|undefined} providedOption An option value to parse. - * @returns {{multiline: boolean, minItems: number}} Normalized option object. - */ - function normalizeOptionValue(providedOption) { - let consistent = false; - let multiline = false; - let minItems; - - const option = providedOption || "always"; - - if (!option || option === "always" || option.minItems === 0) { - minItems = 0; - } else if (option === "never") { - minItems = Number.POSITIVE_INFINITY; - } else if (option === "consistent") { - consistent = true; - minItems = Number.POSITIVE_INFINITY; - } else { - multiline = Boolean(option.multiline); - minItems = option.minItems || Number.POSITIVE_INFINITY; - } - - return { consistent, multiline, minItems }; - } - - /** - * Normalizes a given option value. - * @param {string|Object|undefined} options An option value to parse. - * @returns {{ArrayExpression: {multiline: boolean, minItems: number}, ArrayPattern: {multiline: boolean, minItems: number}}} Normalized option object. - */ - function normalizeOptions(options) { - if (options && (options.ArrayExpression || options.ArrayPattern)) { - let expressionOptions, patternOptions; - - if (options.ArrayExpression) { - expressionOptions = normalizeOptionValue( - options.ArrayExpression, - ); - } - - if (options.ArrayPattern) { - patternOptions = normalizeOptionValue(options.ArrayPattern); - } - - return { - ArrayExpression: expressionOptions, - ArrayPattern: patternOptions, - }; - } - - const value = normalizeOptionValue(options); - - return { ArrayExpression: value, ArrayPattern: value }; - } - - /** - * Reports that there shouldn't be a line break after the first token - * @param {Token} token The token to use for the report. - * @returns {void} - */ - function reportNoLineBreak(token) { - const tokenBefore = sourceCode.getTokenBefore(token, { - includeComments: true, - }); - - context.report({ - loc: { - start: tokenBefore.loc.end, - end: token.loc.start, - }, - messageId: "unexpectedLineBreak", - fix(fixer) { - if (astUtils.isCommentToken(tokenBefore)) { - return null; - } - - if (!astUtils.isTokenOnSameLine(tokenBefore, token)) { - return fixer.replaceTextRange( - [tokenBefore.range[1], token.range[0]], - " ", - ); - } - - /* - * This will check if the comma is on the same line as the next element - * Following array: - * [ - * 1 - * , 2 - * , 3 - * ] - * - * will be fixed to: - * [ - * 1, 2, 3 - * ] - */ - const twoTokensBefore = sourceCode.getTokenBefore( - tokenBefore, - { includeComments: true }, - ); - - if (astUtils.isCommentToken(twoTokensBefore)) { - return null; - } - - return fixer.replaceTextRange( - [twoTokensBefore.range[1], tokenBefore.range[0]], - "", - ); - }, - }); - } - - /** - * Reports that there should be a line break after the first token - * @param {Token} token The token to use for the report. - * @returns {void} - */ - function reportRequiredLineBreak(token) { - const tokenBefore = sourceCode.getTokenBefore(token, { - includeComments: true, - }); - - context.report({ - loc: { - start: tokenBefore.loc.end, - end: token.loc.start, - }, - messageId: "missingLineBreak", - fix(fixer) { - return fixer.replaceTextRange( - [tokenBefore.range[1], token.range[0]], - "\n", - ); - }, - }); - } - - /** - * Reports a given node if it violated this rule. - * @param {ASTNode} node A node to check. This is an ObjectExpression node or an ObjectPattern node. - * @returns {void} - */ - function check(node) { - const elements = node.elements; - const normalizedOptions = normalizeOptions(context.options[0]); - const options = normalizedOptions[node.type]; - - if (!options) { - return; - } - - let elementBreak = false; - - /* - * MULTILINE: true - * loop through every element and check - * if at least one element has linebreaks inside - * this ensures that following is not valid (due to elements are on the same line): - * - * [ - * 1, - * 2, - * 3 - * ] - */ - if (options.multiline) { - elementBreak = elements - .filter(element => element !== null) - .some( - element => - element.loc.start.line !== element.loc.end.line, - ); - } - - let linebreaksCount = 0; - - for (let i = 0; i < node.elements.length; i++) { - const element = node.elements[i]; - - const previousElement = elements[i - 1]; - - if (i === 0 || element === null || previousElement === null) { - continue; - } - - const commaToken = sourceCode.getFirstTokenBetween( - previousElement, - element, - astUtils.isCommaToken, - ); - const lastTokenOfPreviousElement = - sourceCode.getTokenBefore(commaToken); - const firstTokenOfCurrentElement = - sourceCode.getTokenAfter(commaToken); - - if ( - !astUtils.isTokenOnSameLine( - lastTokenOfPreviousElement, - firstTokenOfCurrentElement, - ) - ) { - linebreaksCount++; - } - } - - const needsLinebreaks = - elements.length >= options.minItems || - (options.multiline && elementBreak) || - (options.consistent && - linebreaksCount > 0 && - linebreaksCount < node.elements.length); - - elements.forEach((element, i) => { - const previousElement = elements[i - 1]; - - if (i === 0 || element === null || previousElement === null) { - return; - } - - const commaToken = sourceCode.getFirstTokenBetween( - previousElement, - element, - astUtils.isCommaToken, - ); - const lastTokenOfPreviousElement = - sourceCode.getTokenBefore(commaToken); - const firstTokenOfCurrentElement = - sourceCode.getTokenAfter(commaToken); - - if (needsLinebreaks) { - if ( - astUtils.isTokenOnSameLine( - lastTokenOfPreviousElement, - firstTokenOfCurrentElement, - ) - ) { - reportRequiredLineBreak(firstTokenOfCurrentElement); - } - } else { - if ( - !astUtils.isTokenOnSameLine( - lastTokenOfPreviousElement, - firstTokenOfCurrentElement, - ) - ) { - reportNoLineBreak(firstTokenOfCurrentElement); - } - } - }); - } - - //---------------------------------------------------------------------- - // Public - //---------------------------------------------------------------------- - - return { - ArrayPattern: check, - ArrayExpression: check, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Enforce line breaks after each array element", + recommended: false, + url: "https://eslint.org/docs/latest/rules/array-element-newline" + }, + + fixable: "whitespace", + + schema: { + definitions: { + basicConfig: { + oneOf: [ + { + enum: ["always", "never", "consistent"] + }, + { + type: "object", + properties: { + multiline: { + type: "boolean" + }, + minItems: { + type: ["integer", "null"], + minimum: 0 + } + }, + additionalProperties: false + } + ] + } + }, + type: "array", + items: [ + { + oneOf: [ + { + $ref: "#/definitions/basicConfig" + }, + { + type: "object", + properties: { + ArrayExpression: { + $ref: "#/definitions/basicConfig" + }, + ArrayPattern: { + $ref: "#/definitions/basicConfig" + } + }, + additionalProperties: false, + minProperties: 1 + } + ] + } + ] + }, + + messages: { + unexpectedLineBreak: "There should be no linebreak here.", + missingLineBreak: "There should be a linebreak after this element." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + + //---------------------------------------------------------------------- + // Helpers + //---------------------------------------------------------------------- + + /** + * Normalizes a given option value. + * @param {string|Object|undefined} providedOption An option value to parse. + * @returns {{multiline: boolean, minItems: number}} Normalized option object. + */ + function normalizeOptionValue(providedOption) { + let consistent = false; + let multiline = false; + let minItems; + + const option = providedOption || "always"; + + if (!option || option === "always" || option.minItems === 0) { + minItems = 0; + } else if (option === "never") { + minItems = Number.POSITIVE_INFINITY; + } else if (option === "consistent") { + consistent = true; + minItems = Number.POSITIVE_INFINITY; + } else { + multiline = Boolean(option.multiline); + minItems = option.minItems || Number.POSITIVE_INFINITY; + } + + return { consistent, multiline, minItems }; + } + + /** + * Normalizes a given option value. + * @param {string|Object|undefined} options An option value to parse. + * @returns {{ArrayExpression: {multiline: boolean, minItems: number}, ArrayPattern: {multiline: boolean, minItems: number}}} Normalized option object. + */ + function normalizeOptions(options) { + if (options && (options.ArrayExpression || options.ArrayPattern)) { + let expressionOptions, patternOptions; + + if (options.ArrayExpression) { + expressionOptions = normalizeOptionValue(options.ArrayExpression); + } + + if (options.ArrayPattern) { + patternOptions = normalizeOptionValue(options.ArrayPattern); + } + + return { ArrayExpression: expressionOptions, ArrayPattern: patternOptions }; + } + + const value = normalizeOptionValue(options); + + return { ArrayExpression: value, ArrayPattern: value }; + } + + /** + * Reports that there shouldn't be a line break after the first token + * @param {Token} token The token to use for the report. + * @returns {void} + */ + function reportNoLineBreak(token) { + const tokenBefore = sourceCode.getTokenBefore(token, { includeComments: true }); + + context.report({ + loc: { + start: tokenBefore.loc.end, + end: token.loc.start + }, + messageId: "unexpectedLineBreak", + fix(fixer) { + if (astUtils.isCommentToken(tokenBefore)) { + return null; + } + + if (!astUtils.isTokenOnSameLine(tokenBefore, token)) { + return fixer.replaceTextRange([tokenBefore.range[1], token.range[0]], " "); + } + + /* + * This will check if the comma is on the same line as the next element + * Following array: + * [ + * 1 + * , 2 + * , 3 + * ] + * + * will be fixed to: + * [ + * 1, 2, 3 + * ] + */ + const twoTokensBefore = sourceCode.getTokenBefore(tokenBefore, { includeComments: true }); + + if (astUtils.isCommentToken(twoTokensBefore)) { + return null; + } + + return fixer.replaceTextRange([twoTokensBefore.range[1], tokenBefore.range[0]], ""); + + } + }); + } + + /** + * Reports that there should be a line break after the first token + * @param {Token} token The token to use for the report. + * @returns {void} + */ + function reportRequiredLineBreak(token) { + const tokenBefore = sourceCode.getTokenBefore(token, { includeComments: true }); + + context.report({ + loc: { + start: tokenBefore.loc.end, + end: token.loc.start + }, + messageId: "missingLineBreak", + fix(fixer) { + return fixer.replaceTextRange([tokenBefore.range[1], token.range[0]], "\n"); + } + }); + } + + /** + * Reports a given node if it violated this rule. + * @param {ASTNode} node A node to check. This is an ObjectExpression node or an ObjectPattern node. + * @returns {void} + */ + function check(node) { + const elements = node.elements; + const normalizedOptions = normalizeOptions(context.options[0]); + const options = normalizedOptions[node.type]; + + if (!options) { + return; + } + + let elementBreak = false; + + /* + * MULTILINE: true + * loop through every element and check + * if at least one element has linebreaks inside + * this ensures that following is not valid (due to elements are on the same line): + * + * [ + * 1, + * 2, + * 3 + * ] + */ + if (options.multiline) { + elementBreak = elements + .filter(element => element !== null) + .some(element => element.loc.start.line !== element.loc.end.line); + } + + let linebreaksCount = 0; + + for (let i = 0; i < node.elements.length; i++) { + const element = node.elements[i]; + + const previousElement = elements[i - 1]; + + if (i === 0 || element === null || previousElement === null) { + continue; + } + + const commaToken = sourceCode.getFirstTokenBetween(previousElement, element, astUtils.isCommaToken); + const lastTokenOfPreviousElement = sourceCode.getTokenBefore(commaToken); + const firstTokenOfCurrentElement = sourceCode.getTokenAfter(commaToken); + + if (!astUtils.isTokenOnSameLine(lastTokenOfPreviousElement, firstTokenOfCurrentElement)) { + linebreaksCount++; + } + } + + const needsLinebreaks = ( + elements.length >= options.minItems || + ( + options.multiline && + elementBreak + ) || + ( + options.consistent && + linebreaksCount > 0 && + linebreaksCount < node.elements.length + ) + ); + + elements.forEach((element, i) => { + const previousElement = elements[i - 1]; + + if (i === 0 || element === null || previousElement === null) { + return; + } + + const commaToken = sourceCode.getFirstTokenBetween(previousElement, element, astUtils.isCommaToken); + const lastTokenOfPreviousElement = sourceCode.getTokenBefore(commaToken); + const firstTokenOfCurrentElement = sourceCode.getTokenAfter(commaToken); + + if (needsLinebreaks) { + if (astUtils.isTokenOnSameLine(lastTokenOfPreviousElement, firstTokenOfCurrentElement)) { + reportRequiredLineBreak(firstTokenOfCurrentElement); + } + } else { + if (!astUtils.isTokenOnSameLine(lastTokenOfPreviousElement, firstTokenOfCurrentElement)) { + reportNoLineBreak(firstTokenOfCurrentElement); + } + } + }); + } + + //---------------------------------------------------------------------- + // Public + //---------------------------------------------------------------------- + + return { + ArrayPattern: check, + ArrayExpression: check + }; + } }; diff --git a/node_modules/eslint/lib/rules/arrow-body-style.js b/node_modules/eslint/lib/rules/arrow-body-style.js index 8272317b5..759070454 100644 --- a/node_modules/eslint/lib/rules/arrow-body-style.js +++ b/node_modules/eslint/lib/rules/arrow-body-style.js @@ -14,405 +14,283 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - defaultOptions: ["as-needed"], - - docs: { - description: "Require braces around arrow function bodies", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/arrow-body-style", - }, - - schema: { - anyOf: [ - { - type: "array", - items: [ - { - enum: ["always", "never"], - }, - ], - minItems: 0, - maxItems: 1, - }, - { - type: "array", - items: [ - { - enum: ["as-needed"], - }, - { - type: "object", - properties: { - requireReturnForObjectLiteral: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - minItems: 0, - maxItems: 2, - }, - ], - }, - - fixable: "code", - - messages: { - unexpectedOtherBlock: - "Unexpected block statement surrounding arrow body.", - unexpectedEmptyBlock: - "Unexpected block statement surrounding arrow body; put a value of `undefined` immediately after the `=>`.", - unexpectedObjectBlock: - "Unexpected block statement surrounding arrow body; parenthesize the returned value and move it immediately after the `=>`.", - unexpectedSingleBlock: - "Unexpected block statement surrounding arrow body; move the returned value immediately after the `=>`.", - expectedBlock: "Expected block statement surrounding arrow body.", - }, - }, - - create(context) { - const options = context.options; - const always = options[0] === "always"; - const asNeeded = options[0] === "as-needed"; - const never = options[0] === "never"; - const requireReturnForObjectLiteral = - options[1] && options[1].requireReturnForObjectLiteral; - const sourceCode = context.sourceCode; - let funcInfo = null; - - /** - * Checks whether the given node has ASI problem or not. - * @param {Token} token The token to check. - * @returns {boolean} `true` if it changes semantics if `;` or `}` followed by the token are removed. - */ - function hasASIProblem(token) { - return ( - token && - token.type === "Punctuator" && - /^[([/`+-]/u.test(token.value) - ); - } - - /** - * Gets the closing parenthesis by the given node. - * @param {ASTNode} node first node after an opening parenthesis. - * @returns {Token} The found closing parenthesis token. - */ - function findClosingParen(node) { - let nodeToCheck = node; - - while (!astUtils.isParenthesised(sourceCode, nodeToCheck)) { - nodeToCheck = nodeToCheck.parent; - } - return sourceCode.getTokenAfter(nodeToCheck); - } - - /** - * Check whether the node is inside of a for loop's init - * @param {ASTNode} node node is inside for loop - * @returns {boolean} `true` if the node is inside of a for loop, else `false` - */ - function isInsideForLoopInitializer(node) { - if (node && node.parent) { - if ( - node.parent.type === "ForStatement" && - node.parent.init === node - ) { - return true; - } - return isInsideForLoopInitializer(node.parent); - } - return false; - } - - /** - * Determines whether a arrow function body needs braces - * @param {ASTNode} node The arrow function node. - * @returns {void} - */ - function validate(node) { - const arrowBody = node.body; - - if (arrowBody.type === "BlockStatement") { - const blockBody = arrowBody.body; - - if (blockBody.length !== 1 && !never) { - return; - } - - if ( - asNeeded && - requireReturnForObjectLiteral && - blockBody[0].type === "ReturnStatement" && - blockBody[0].argument && - blockBody[0].argument.type === "ObjectExpression" - ) { - return; - } - - if ( - never || - (asNeeded && blockBody[0].type === "ReturnStatement") - ) { - let messageId; - - if (blockBody.length === 0) { - messageId = "unexpectedEmptyBlock"; - } else if ( - blockBody.length > 1 || - blockBody[0].type !== "ReturnStatement" - ) { - messageId = "unexpectedOtherBlock"; - } else if (blockBody[0].argument === null) { - messageId = "unexpectedSingleBlock"; - } else if ( - astUtils.isOpeningBraceToken( - sourceCode.getFirstToken(blockBody[0], { skip: 1 }), - ) - ) { - messageId = "unexpectedObjectBlock"; - } else { - messageId = "unexpectedSingleBlock"; - } - - context.report({ - node, - loc: arrowBody.loc, - messageId, - fix(fixer) { - const fixes = []; - - if ( - blockBody.length !== 1 || - blockBody[0].type !== "ReturnStatement" || - !blockBody[0].argument || - hasASIProblem( - sourceCode.getTokenAfter(arrowBody), - ) - ) { - return fixes; - } - - const openingBrace = - sourceCode.getFirstToken(arrowBody); - const closingBrace = - sourceCode.getLastToken(arrowBody); - const firstValueToken = sourceCode.getFirstToken( - blockBody[0], - 1, - ); - const lastValueToken = sourceCode.getLastToken( - blockBody[0], - ); - const commentsExist = - sourceCode.commentsExistBetween( - openingBrace, - firstValueToken, - ) || - sourceCode.commentsExistBetween( - lastValueToken, - closingBrace, - ); - - /* - * Remove tokens around the return value. - * If comments don't exist, remove extra spaces as well. - */ - if (commentsExist) { - fixes.push( - fixer.remove(openingBrace), - fixer.remove(closingBrace), - fixer.remove( - sourceCode.getTokenAfter(openingBrace), - ), // return keyword - ); - } else { - fixes.push( - fixer.removeRange([ - openingBrace.range[0], - firstValueToken.range[0], - ]), - fixer.removeRange([ - lastValueToken.range[1], - closingBrace.range[1], - ]), - ); - } - - /* - * If the first token of the return value is `{` or the return value is a sequence expression, - * enclose the return value by parentheses to avoid syntax error. - */ - if ( - astUtils.isOpeningBraceToken(firstValueToken) || - blockBody[0].argument.type === - "SequenceExpression" || - (funcInfo.hasInOperator && - isInsideForLoopInitializer(node)) - ) { - if ( - !astUtils.isParenthesised( - sourceCode, - blockBody[0].argument, - ) - ) { - fixes.push( - fixer.insertTextBefore( - firstValueToken, - "(", - ), - fixer.insertTextAfter( - lastValueToken, - ")", - ), - ); - } - } - - /* - * If the last token of the return statement is semicolon, remove it. - * Non-block arrow body is an expression, not a statement. - */ - if (astUtils.isSemicolonToken(lastValueToken)) { - fixes.push(fixer.remove(lastValueToken)); - } - - return fixes; - }, - }); - } - } else { - if ( - always || - (asNeeded && - requireReturnForObjectLiteral && - arrowBody.type === "ObjectExpression") - ) { - context.report({ - node, - loc: arrowBody.loc, - messageId: "expectedBlock", - fix(fixer) { - const fixes = []; - const arrowToken = sourceCode.getTokenBefore( - arrowBody, - astUtils.isArrowToken, - ); - const [ - firstTokenAfterArrow, - secondTokenAfterArrow, - ] = sourceCode.getTokensAfter(arrowToken, { - count: 2, - }); - const lastToken = sourceCode.getLastToken(node); - - let parenthesisedObjectLiteral = null; - - if ( - astUtils.isOpeningParenToken( - firstTokenAfterArrow, - ) && - astUtils.isOpeningBraceToken( - secondTokenAfterArrow, - ) - ) { - const braceNode = - sourceCode.getNodeByRangeIndex( - secondTokenAfterArrow.range[0], - ); - - if (braceNode.type === "ObjectExpression") { - parenthesisedObjectLiteral = braceNode; - } - } - - // If the value is object literal, remove parentheses which were forced by syntax. - if (parenthesisedObjectLiteral) { - const openingParenToken = firstTokenAfterArrow; - const openingBraceToken = secondTokenAfterArrow; - - if ( - astUtils.isTokenOnSameLine( - openingParenToken, - openingBraceToken, - ) - ) { - fixes.push( - fixer.replaceText( - openingParenToken, - "{return ", - ), - ); - } else { - // Avoid ASI - fixes.push( - fixer.replaceText( - openingParenToken, - "{", - ), - fixer.insertTextBefore( - openingBraceToken, - "return ", - ), - ); - } - - // Closing paren for the object doesn't have to be lastToken, e.g.: () => ({}).foo() - fixes.push( - fixer.remove( - findClosingParen( - parenthesisedObjectLiteral, - ), - ), - ); - fixes.push( - fixer.insertTextAfter(lastToken, "}"), - ); - } else { - fixes.push( - fixer.insertTextBefore( - firstTokenAfterArrow, - "{return ", - ), - ); - fixes.push( - fixer.insertTextAfter(lastToken, "}"), - ); - } - - return fixes; - }, - }); - } - } - } - - return { - "BinaryExpression[operator='in']"() { - let info = funcInfo; - - while (info) { - info.hasInOperator = true; - info = info.upper; - } - }, - ArrowFunctionExpression() { - funcInfo = { - upper: funcInfo, - hasInOperator: false, - }; - }, - "ArrowFunctionExpression:exit"(node) { - validate(node); - funcInfo = funcInfo.upper; - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Require braces around arrow function bodies", + recommended: false, + url: "https://eslint.org/docs/latest/rules/arrow-body-style" + }, + + schema: { + anyOf: [ + { + type: "array", + items: [ + { + enum: ["always", "never"] + } + ], + minItems: 0, + maxItems: 1 + }, + { + type: "array", + items: [ + { + enum: ["as-needed"] + }, + { + type: "object", + properties: { + requireReturnForObjectLiteral: { type: "boolean" } + }, + additionalProperties: false + } + ], + minItems: 0, + maxItems: 2 + } + ] + }, + + fixable: "code", + + messages: { + unexpectedOtherBlock: "Unexpected block statement surrounding arrow body.", + unexpectedEmptyBlock: "Unexpected block statement surrounding arrow body; put a value of `undefined` immediately after the `=>`.", + unexpectedObjectBlock: "Unexpected block statement surrounding arrow body; parenthesize the returned value and move it immediately after the `=>`.", + unexpectedSingleBlock: "Unexpected block statement surrounding arrow body; move the returned value immediately after the `=>`.", + expectedBlock: "Expected block statement surrounding arrow body." + } + }, + + create(context) { + const options = context.options; + const always = options[0] === "always"; + const asNeeded = !options[0] || options[0] === "as-needed"; + const never = options[0] === "never"; + const requireReturnForObjectLiteral = options[1] && options[1].requireReturnForObjectLiteral; + const sourceCode = context.sourceCode; + let funcInfo = null; + + /** + * Checks whether the given node has ASI problem or not. + * @param {Token} token The token to check. + * @returns {boolean} `true` if it changes semantics if `;` or `}` followed by the token are removed. + */ + function hasASIProblem(token) { + return token && token.type === "Punctuator" && /^[([/`+-]/u.test(token.value); + } + + /** + * Gets the closing parenthesis by the given node. + * @param {ASTNode} node first node after an opening parenthesis. + * @returns {Token} The found closing parenthesis token. + */ + function findClosingParen(node) { + let nodeToCheck = node; + + while (!astUtils.isParenthesised(sourceCode, nodeToCheck)) { + nodeToCheck = nodeToCheck.parent; + } + return sourceCode.getTokenAfter(nodeToCheck); + } + + /** + * Check whether the node is inside of a for loop's init + * @param {ASTNode} node node is inside for loop + * @returns {boolean} `true` if the node is inside of a for loop, else `false` + */ + function isInsideForLoopInitializer(node) { + if (node && node.parent) { + if (node.parent.type === "ForStatement" && node.parent.init === node) { + return true; + } + return isInsideForLoopInitializer(node.parent); + } + return false; + } + + /** + * Determines whether a arrow function body needs braces + * @param {ASTNode} node The arrow function node. + * @returns {void} + */ + function validate(node) { + const arrowBody = node.body; + + if (arrowBody.type === "BlockStatement") { + const blockBody = arrowBody.body; + + if (blockBody.length !== 1 && !never) { + return; + } + + if (asNeeded && requireReturnForObjectLiteral && blockBody[0].type === "ReturnStatement" && + blockBody[0].argument && blockBody[0].argument.type === "ObjectExpression") { + return; + } + + if (never || asNeeded && blockBody[0].type === "ReturnStatement") { + let messageId; + + if (blockBody.length === 0) { + messageId = "unexpectedEmptyBlock"; + } else if (blockBody.length > 1) { + messageId = "unexpectedOtherBlock"; + } else if (blockBody[0].argument === null) { + messageId = "unexpectedSingleBlock"; + } else if (astUtils.isOpeningBraceToken(sourceCode.getFirstToken(blockBody[0], { skip: 1 }))) { + messageId = "unexpectedObjectBlock"; + } else { + messageId = "unexpectedSingleBlock"; + } + + context.report({ + node, + loc: arrowBody.loc, + messageId, + fix(fixer) { + const fixes = []; + + if (blockBody.length !== 1 || + blockBody[0].type !== "ReturnStatement" || + !blockBody[0].argument || + hasASIProblem(sourceCode.getTokenAfter(arrowBody)) + ) { + return fixes; + } + + const openingBrace = sourceCode.getFirstToken(arrowBody); + const closingBrace = sourceCode.getLastToken(arrowBody); + const firstValueToken = sourceCode.getFirstToken(blockBody[0], 1); + const lastValueToken = sourceCode.getLastToken(blockBody[0]); + const commentsExist = + sourceCode.commentsExistBetween(openingBrace, firstValueToken) || + sourceCode.commentsExistBetween(lastValueToken, closingBrace); + + /* + * Remove tokens around the return value. + * If comments don't exist, remove extra spaces as well. + */ + if (commentsExist) { + fixes.push( + fixer.remove(openingBrace), + fixer.remove(closingBrace), + fixer.remove(sourceCode.getTokenAfter(openingBrace)) // return keyword + ); + } else { + fixes.push( + fixer.removeRange([openingBrace.range[0], firstValueToken.range[0]]), + fixer.removeRange([lastValueToken.range[1], closingBrace.range[1]]) + ); + } + + /* + * If the first token of the return value is `{` or the return value is a sequence expression, + * enclose the return value by parentheses to avoid syntax error. + */ + if (astUtils.isOpeningBraceToken(firstValueToken) || blockBody[0].argument.type === "SequenceExpression" || (funcInfo.hasInOperator && isInsideForLoopInitializer(node))) { + if (!astUtils.isParenthesised(sourceCode, blockBody[0].argument)) { + fixes.push( + fixer.insertTextBefore(firstValueToken, "("), + fixer.insertTextAfter(lastValueToken, ")") + ); + } + } + + /* + * If the last token of the return statement is semicolon, remove it. + * Non-block arrow body is an expression, not a statement. + */ + if (astUtils.isSemicolonToken(lastValueToken)) { + fixes.push(fixer.remove(lastValueToken)); + } + + return fixes; + } + }); + } + } else { + if (always || (asNeeded && requireReturnForObjectLiteral && arrowBody.type === "ObjectExpression")) { + context.report({ + node, + loc: arrowBody.loc, + messageId: "expectedBlock", + fix(fixer) { + const fixes = []; + const arrowToken = sourceCode.getTokenBefore(arrowBody, astUtils.isArrowToken); + const [firstTokenAfterArrow, secondTokenAfterArrow] = sourceCode.getTokensAfter(arrowToken, { count: 2 }); + const lastToken = sourceCode.getLastToken(node); + + let parenthesisedObjectLiteral = null; + + if ( + astUtils.isOpeningParenToken(firstTokenAfterArrow) && + astUtils.isOpeningBraceToken(secondTokenAfterArrow) + ) { + const braceNode = sourceCode.getNodeByRangeIndex(secondTokenAfterArrow.range[0]); + + if (braceNode.type === "ObjectExpression") { + parenthesisedObjectLiteral = braceNode; + } + } + + // If the value is object literal, remove parentheses which were forced by syntax. + if (parenthesisedObjectLiteral) { + const openingParenToken = firstTokenAfterArrow; + const openingBraceToken = secondTokenAfterArrow; + + if (astUtils.isTokenOnSameLine(openingParenToken, openingBraceToken)) { + fixes.push(fixer.replaceText(openingParenToken, "{return ")); + } else { + + // Avoid ASI + fixes.push( + fixer.replaceText(openingParenToken, "{"), + fixer.insertTextBefore(openingBraceToken, "return ") + ); + } + + // Closing paren for the object doesn't have to be lastToken, e.g.: () => ({}).foo() + fixes.push(fixer.remove(findClosingParen(parenthesisedObjectLiteral))); + fixes.push(fixer.insertTextAfter(lastToken, "}")); + + } else { + fixes.push(fixer.insertTextBefore(firstTokenAfterArrow, "{return ")); + fixes.push(fixer.insertTextAfter(lastToken, "}")); + } + + return fixes; + } + }); + } + } + } + + return { + "BinaryExpression[operator='in']"() { + let info = funcInfo; + + while (info) { + info.hasInOperator = true; + info = info.upper; + } + }, + ArrowFunctionExpression() { + funcInfo = { + upper: funcInfo, + hasInOperator: false + }; + }, + "ArrowFunctionExpression:exit"(node) { + validate(node); + funcInfo = funcInfo.upper; + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/arrow-parens.js b/node_modules/eslint/lib/rules/arrow-parens.js index bbb3d3353..2206d8ce2 100644 --- a/node_modules/eslint/lib/rules/arrow-parens.js +++ b/node_modules/eslint/lib/rules/arrow-parens.js @@ -21,217 +21,166 @@ const astUtils = require("./utils/ast-utils"); * @returns {boolean} `true` if the function has block body. */ function hasBlockBody(node) { - return node.body.type === "BlockStatement"; + return node.body.type === "BlockStatement"; } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "arrow-parens", - url: "https://eslint.style/rules/arrow-parens", - }, - }, - ], - }, - type: "layout", - - docs: { - description: "Require parentheses around arrow function arguments", - recommended: false, - url: "https://eslint.org/docs/latest/rules/arrow-parens", - }, - - fixable: "code", - - schema: [ - { - enum: ["always", "as-needed"], - }, - { - type: "object", - properties: { - requireForBlockBody: { - type: "boolean", - default: false, - }, - }, - additionalProperties: false, - }, - ], - - messages: { - unexpectedParens: - "Unexpected parentheses around single function argument.", - expectedParens: - "Expected parentheses around arrow function argument.", - - unexpectedParensInline: - "Unexpected parentheses around single function argument having a body with no curly braces.", - expectedParensBlock: - "Expected parentheses around arrow function argument having a body with curly braces.", - }, - }, - - create(context) { - const asNeeded = context.options[0] === "as-needed"; - const requireForBlockBody = - asNeeded && - context.options[1] && - context.options[1].requireForBlockBody === true; - - const sourceCode = context.sourceCode; - - /** - * Finds opening paren of parameters for the given arrow function, if it exists. - * It is assumed that the given arrow function has exactly one parameter. - * @param {ASTNode} node `ArrowFunctionExpression` node. - * @returns {Token|null} the opening paren, or `null` if the given arrow function doesn't have parens of parameters. - */ - function findOpeningParenOfParams(node) { - const tokenBeforeParams = sourceCode.getTokenBefore(node.params[0]); - - if ( - tokenBeforeParams && - astUtils.isOpeningParenToken(tokenBeforeParams) && - node.range[0] <= tokenBeforeParams.range[0] - ) { - return tokenBeforeParams; - } - - return null; - } - - /** - * Finds closing paren of parameters for the given arrow function. - * It is assumed that the given arrow function has parens of parameters and that it has exactly one parameter. - * @param {ASTNode} node `ArrowFunctionExpression` node. - * @returns {Token} the closing paren of parameters. - */ - function getClosingParenOfParams(node) { - return sourceCode.getTokenAfter( - node.params[0], - astUtils.isClosingParenToken, - ); - } - - /** - * Determines whether the given arrow function has comments inside parens of parameters. - * It is assumed that the given arrow function has parens of parameters. - * @param {ASTNode} node `ArrowFunctionExpression` node. - * @param {Token} openingParen Opening paren of parameters. - * @returns {boolean} `true` if the function has at least one comment inside of parens of parameters. - */ - function hasCommentsInParensOfParams(node, openingParen) { - return sourceCode.commentsExistBetween( - openingParen, - getClosingParenOfParams(node), - ); - } - - /** - * Determines whether the given arrow function has unexpected tokens before opening paren of parameters, - * in which case it will be assumed that the existing parens of parameters are necessary. - * Only tokens within the range of the arrow function (tokens that are part of the arrow function) are taken into account. - * Example: (a) => b - * @param {ASTNode} node `ArrowFunctionExpression` node. - * @param {Token} openingParen Opening paren of parameters. - * @returns {boolean} `true` if the function has at least one unexpected token. - */ - function hasUnexpectedTokensBeforeOpeningParen(node, openingParen) { - const expectedCount = node.async ? 1 : 0; - - return ( - sourceCode.getFirstToken(node, { skip: expectedCount }) !== - openingParen - ); - } - - return { - "ArrowFunctionExpression[params.length=1]"(node) { - const shouldHaveParens = - !asNeeded || (requireForBlockBody && hasBlockBody(node)); - const openingParen = findOpeningParenOfParams(node); - const hasParens = openingParen !== null; - const [param] = node.params; - - if (shouldHaveParens && !hasParens) { - context.report({ - node, - messageId: requireForBlockBody - ? "expectedParensBlock" - : "expectedParens", - loc: param.loc, - *fix(fixer) { - yield fixer.insertTextBefore(param, "("); - yield fixer.insertTextAfter(param, ")"); - }, - }); - } - - if ( - !shouldHaveParens && - hasParens && - param.type === "Identifier" && - !param.typeAnnotation && - !node.returnType && - !hasCommentsInParensOfParams(node, openingParen) && - !hasUnexpectedTokensBeforeOpeningParen(node, openingParen) - ) { - context.report({ - node, - messageId: requireForBlockBody - ? "unexpectedParensInline" - : "unexpectedParens", - loc: param.loc, - *fix(fixer) { - const tokenBeforeOpeningParen = - sourceCode.getTokenBefore(openingParen); - const closingParen = getClosingParenOfParams(node); - - if ( - tokenBeforeOpeningParen && - tokenBeforeOpeningParen.range[1] === - openingParen.range[0] && - !astUtils.canTokensBeAdjacent( - tokenBeforeOpeningParen, - sourceCode.getFirstToken(param), - ) - ) { - yield fixer.insertTextBefore(openingParen, " "); - } - - // remove parens, whitespace inside parens, and possible trailing comma - yield fixer.removeRange([ - openingParen.range[0], - param.range[0], - ]); - yield fixer.removeRange([ - param.range[1], - closingParen.range[1], - ]); - }, - }); - } - }, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Require parentheses around arrow function arguments", + recommended: false, + url: "https://eslint.org/docs/latest/rules/arrow-parens" + }, + + fixable: "code", + + schema: [ + { + enum: ["always", "as-needed"] + }, + { + type: "object", + properties: { + requireForBlockBody: { + type: "boolean", + default: false + } + }, + additionalProperties: false + } + ], + + messages: { + unexpectedParens: "Unexpected parentheses around single function argument.", + expectedParens: "Expected parentheses around arrow function argument.", + + unexpectedParensInline: "Unexpected parentheses around single function argument having a body with no curly braces.", + expectedParensBlock: "Expected parentheses around arrow function argument having a body with curly braces." + } + }, + + create(context) { + const asNeeded = context.options[0] === "as-needed"; + const requireForBlockBody = asNeeded && context.options[1] && context.options[1].requireForBlockBody === true; + + const sourceCode = context.sourceCode; + + /** + * Finds opening paren of parameters for the given arrow function, if it exists. + * It is assumed that the given arrow function has exactly one parameter. + * @param {ASTNode} node `ArrowFunctionExpression` node. + * @returns {Token|null} the opening paren, or `null` if the given arrow function doesn't have parens of parameters. + */ + function findOpeningParenOfParams(node) { + const tokenBeforeParams = sourceCode.getTokenBefore(node.params[0]); + + if ( + tokenBeforeParams && + astUtils.isOpeningParenToken(tokenBeforeParams) && + node.range[0] <= tokenBeforeParams.range[0] + ) { + return tokenBeforeParams; + } + + return null; + } + + /** + * Finds closing paren of parameters for the given arrow function. + * It is assumed that the given arrow function has parens of parameters and that it has exactly one parameter. + * @param {ASTNode} node `ArrowFunctionExpression` node. + * @returns {Token} the closing paren of parameters. + */ + function getClosingParenOfParams(node) { + return sourceCode.getTokenAfter(node.params[0], astUtils.isClosingParenToken); + } + + /** + * Determines whether the given arrow function has comments inside parens of parameters. + * It is assumed that the given arrow function has parens of parameters. + * @param {ASTNode} node `ArrowFunctionExpression` node. + * @param {Token} openingParen Opening paren of parameters. + * @returns {boolean} `true` if the function has at least one comment inside of parens of parameters. + */ + function hasCommentsInParensOfParams(node, openingParen) { + return sourceCode.commentsExistBetween(openingParen, getClosingParenOfParams(node)); + } + + /** + * Determines whether the given arrow function has unexpected tokens before opening paren of parameters, + * in which case it will be assumed that the existing parens of parameters are necessary. + * Only tokens within the range of the arrow function (tokens that are part of the arrow function) are taken into account. + * Example: (a) => b + * @param {ASTNode} node `ArrowFunctionExpression` node. + * @param {Token} openingParen Opening paren of parameters. + * @returns {boolean} `true` if the function has at least one unexpected token. + */ + function hasUnexpectedTokensBeforeOpeningParen(node, openingParen) { + const expectedCount = node.async ? 1 : 0; + + return sourceCode.getFirstToken(node, { skip: expectedCount }) !== openingParen; + } + + return { + "ArrowFunctionExpression[params.length=1]"(node) { + const shouldHaveParens = !asNeeded || requireForBlockBody && hasBlockBody(node); + const openingParen = findOpeningParenOfParams(node); + const hasParens = openingParen !== null; + const [param] = node.params; + + if (shouldHaveParens && !hasParens) { + context.report({ + node, + messageId: requireForBlockBody ? "expectedParensBlock" : "expectedParens", + loc: param.loc, + *fix(fixer) { + yield fixer.insertTextBefore(param, "("); + yield fixer.insertTextAfter(param, ")"); + } + }); + } + + if ( + !shouldHaveParens && + hasParens && + param.type === "Identifier" && + !param.typeAnnotation && + !node.returnType && + !hasCommentsInParensOfParams(node, openingParen) && + !hasUnexpectedTokensBeforeOpeningParen(node, openingParen) + ) { + context.report({ + node, + messageId: requireForBlockBody ? "unexpectedParensInline" : "unexpectedParens", + loc: param.loc, + *fix(fixer) { + const tokenBeforeOpeningParen = sourceCode.getTokenBefore(openingParen); + const closingParen = getClosingParenOfParams(node); + + if ( + tokenBeforeOpeningParen && + tokenBeforeOpeningParen.range[1] === openingParen.range[0] && + !astUtils.canTokensBeAdjacent(tokenBeforeOpeningParen, sourceCode.getFirstToken(param)) + ) { + yield fixer.insertTextBefore(openingParen, " "); + } + + // remove parens, whitespace inside parens, and possible trailing comma + yield fixer.removeRange([openingParen.range[0], param.range[0]]); + yield fixer.removeRange([param.range[1], closingParen.range[1]]); + } + }); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/arrow-spacing.js b/node_modules/eslint/lib/rules/arrow-spacing.js index 21b20531e..2b7d464ff 100644 --- a/node_modules/eslint/lib/rules/arrow-spacing.js +++ b/node_modules/eslint/lib/rules/arrow-spacing.js @@ -15,174 +15,150 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "arrow-spacing", - url: "https://eslint.style/rules/arrow-spacing", - }, - }, - ], - }, - type: "layout", - - docs: { - description: - "Enforce consistent spacing before and after the arrow in arrow functions", - recommended: false, - url: "https://eslint.org/docs/latest/rules/arrow-spacing", - }, - - fixable: "whitespace", - - schema: [ - { - type: "object", - properties: { - before: { - type: "boolean", - default: true, - }, - after: { - type: "boolean", - default: true, - }, - }, - additionalProperties: false, - }, - ], - - messages: { - expectedBefore: "Missing space before =>.", - unexpectedBefore: "Unexpected space before =>.", - - expectedAfter: "Missing space after =>.", - unexpectedAfter: "Unexpected space after =>.", - }, - }, - - create(context) { - // merge rules with default - const rule = Object.assign({}, context.options[0]); - - rule.before = rule.before !== false; - rule.after = rule.after !== false; - - const sourceCode = context.sourceCode; - - /** - * Get tokens of arrow(`=>`) and before/after arrow. - * @param {ASTNode} node The arrow function node. - * @returns {Object} Tokens of arrow and before/after arrow. - */ - function getTokens(node) { - const arrow = sourceCode.getTokenBefore( - node.body, - astUtils.isArrowToken, - ); - - return { - before: sourceCode.getTokenBefore(arrow), - arrow, - after: sourceCode.getTokenAfter(arrow), - }; - } - - /** - * Count spaces before/after arrow(`=>`) token. - * @param {Object} tokens Tokens before/after arrow. - * @returns {Object} count of space before/after arrow. - */ - function countSpaces(tokens) { - const before = tokens.arrow.range[0] - tokens.before.range[1]; - const after = tokens.after.range[0] - tokens.arrow.range[1]; - - return { before, after }; - } - - /** - * Determines whether space(s) before after arrow(`=>`) is satisfy rule. - * if before/after value is `true`, there should be space(s). - * if before/after value is `false`, there should be no space. - * @param {ASTNode} node The arrow function node. - * @returns {void} - */ - function spaces(node) { - const tokens = getTokens(node); - const countSpace = countSpaces(tokens); - - if (rule.before) { - // should be space(s) before arrow - if (countSpace.before === 0) { - context.report({ - node: tokens.before, - messageId: "expectedBefore", - fix(fixer) { - return fixer.insertTextBefore(tokens.arrow, " "); - }, - }); - } - } else { - // should be no space before arrow - if (countSpace.before > 0) { - context.report({ - node: tokens.before, - messageId: "unexpectedBefore", - fix(fixer) { - return fixer.removeRange([ - tokens.before.range[1], - tokens.arrow.range[0], - ]); - }, - }); - } - } - - if (rule.after) { - // should be space(s) after arrow - if (countSpace.after === 0) { - context.report({ - node: tokens.after, - messageId: "expectedAfter", - fix(fixer) { - return fixer.insertTextAfter(tokens.arrow, " "); - }, - }); - } - } else { - // should be no space after arrow - if (countSpace.after > 0) { - context.report({ - node: tokens.after, - messageId: "unexpectedAfter", - fix(fixer) { - return fixer.removeRange([ - tokens.arrow.range[1], - tokens.after.range[0], - ]); - }, - }); - } - } - } - - return { - ArrowFunctionExpression: spaces, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Enforce consistent spacing before and after the arrow in arrow functions", + recommended: false, + url: "https://eslint.org/docs/latest/rules/arrow-spacing" + }, + + fixable: "whitespace", + + schema: [ + { + type: "object", + properties: { + before: { + type: "boolean", + default: true + }, + after: { + type: "boolean", + default: true + } + }, + additionalProperties: false + } + ], + + messages: { + expectedBefore: "Missing space before =>.", + unexpectedBefore: "Unexpected space before =>.", + + expectedAfter: "Missing space after =>.", + unexpectedAfter: "Unexpected space after =>." + } + }, + + create(context) { + + // merge rules with default + const rule = Object.assign({}, context.options[0]); + + rule.before = rule.before !== false; + rule.after = rule.after !== false; + + const sourceCode = context.sourceCode; + + /** + * Get tokens of arrow(`=>`) and before/after arrow. + * @param {ASTNode} node The arrow function node. + * @returns {Object} Tokens of arrow and before/after arrow. + */ + function getTokens(node) { + const arrow = sourceCode.getTokenBefore(node.body, astUtils.isArrowToken); + + return { + before: sourceCode.getTokenBefore(arrow), + arrow, + after: sourceCode.getTokenAfter(arrow) + }; + } + + /** + * Count spaces before/after arrow(`=>`) token. + * @param {Object} tokens Tokens before/after arrow. + * @returns {Object} count of space before/after arrow. + */ + function countSpaces(tokens) { + const before = tokens.arrow.range[0] - tokens.before.range[1]; + const after = tokens.after.range[0] - tokens.arrow.range[1]; + + return { before, after }; + } + + /** + * Determines whether space(s) before after arrow(`=>`) is satisfy rule. + * if before/after value is `true`, there should be space(s). + * if before/after value is `false`, there should be no space. + * @param {ASTNode} node The arrow function node. + * @returns {void} + */ + function spaces(node) { + const tokens = getTokens(node); + const countSpace = countSpaces(tokens); + + if (rule.before) { + + // should be space(s) before arrow + if (countSpace.before === 0) { + context.report({ + node: tokens.before, + messageId: "expectedBefore", + fix(fixer) { + return fixer.insertTextBefore(tokens.arrow, " "); + } + }); + } + } else { + + // should be no space before arrow + if (countSpace.before > 0) { + context.report({ + node: tokens.before, + messageId: "unexpectedBefore", + fix(fixer) { + return fixer.removeRange([tokens.before.range[1], tokens.arrow.range[0]]); + } + }); + } + } + + if (rule.after) { + + // should be space(s) after arrow + if (countSpace.after === 0) { + context.report({ + node: tokens.after, + messageId: "expectedAfter", + fix(fixer) { + return fixer.insertTextAfter(tokens.arrow, " "); + } + }); + } + } else { + + // should be no space after arrow + if (countSpace.after > 0) { + context.report({ + node: tokens.after, + messageId: "unexpectedAfter", + fix(fixer) { + return fixer.removeRange([tokens.arrow.range[1], tokens.after.range[0]]); + } + }); + } + } + } + + return { + ArrowFunctionExpression: spaces + }; + } }; diff --git a/node_modules/eslint/lib/rules/block-scoped-var.js b/node_modules/eslint/lib/rules/block-scoped-var.js index e79bec51f..ec597d566 100644 --- a/node_modules/eslint/lib/rules/block-scoped-var.js +++ b/node_modules/eslint/lib/rules/block-scoped-var.js @@ -8,130 +8,128 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: - "Enforce the use of variables within the scope they are defined", - recommended: false, - url: "https://eslint.org/docs/latest/rules/block-scoped-var", - }, - - schema: [], - - messages: { - outOfScope: - "'{{name}}' declared on line {{definitionLine}} column {{definitionColumn}} is used outside of binding context.", - }, - }, - - create(context) { - let stack = []; - const sourceCode = context.sourceCode; - - /** - * Makes a block scope. - * @param {ASTNode} node A node of a scope. - * @returns {void} - */ - function enterScope(node) { - stack.push(node.range); - } - - /** - * Pops the last block scope. - * @returns {void} - */ - function exitScope() { - stack.pop(); - } - - /** - * Reports a given reference. - * @param {eslint-scope.Reference} reference A reference to report. - * @param {eslint-scope.Definition} definition A definition for which to report reference. - * @returns {void} - */ - function report(reference, definition) { - const identifier = reference.identifier; - const definitionPosition = definition.name.loc.start; - - context.report({ - node: identifier, - messageId: "outOfScope", - data: { - name: identifier.name, - definitionLine: definitionPosition.line, - definitionColumn: definitionPosition.column + 1, - }, - }); - } - - /** - * Finds and reports references which are outside of valid scopes. - * @param {ASTNode} node A node to get variables. - * @returns {void} - */ - function checkForVariables(node) { - if (node.kind !== "var") { - return; - } - - // Defines a predicate to check whether or not a given reference is outside of valid scope. - const scopeRange = stack.at(-1); - - /** - * Check if a reference is out of scope - * @param {ASTNode} reference node to examine - * @returns {boolean} True is its outside the scope - * @private - */ - function isOutsideOfScope(reference) { - const idRange = reference.identifier.range; - - return idRange[0] < scopeRange[0] || idRange[1] > scopeRange[1]; - } - - // Gets declared variables, and checks its references. - const variables = sourceCode.getDeclaredVariables(node); - - for (let i = 0; i < variables.length; ++i) { - // Reports. - variables[i].references.filter(isOutsideOfScope).forEach(ref => - report( - ref, - variables[i].defs.find(def => def.parent === node), - ), - ); - } - } - - return { - Program(node) { - stack = [node.range]; - }, - - // Manages scopes. - BlockStatement: enterScope, - "BlockStatement:exit": exitScope, - ForStatement: enterScope, - "ForStatement:exit": exitScope, - ForInStatement: enterScope, - "ForInStatement:exit": exitScope, - ForOfStatement: enterScope, - "ForOfStatement:exit": exitScope, - SwitchStatement: enterScope, - "SwitchStatement:exit": exitScope, - CatchClause: enterScope, - "CatchClause:exit": exitScope, - StaticBlock: enterScope, - "StaticBlock:exit": exitScope, - - // Finds and reports references which are outside of valid scope. - VariableDeclaration: checkForVariables, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Enforce the use of variables within the scope they are defined", + recommended: false, + url: "https://eslint.org/docs/latest/rules/block-scoped-var" + }, + + schema: [], + + messages: { + outOfScope: "'{{name}}' declared on line {{definitionLine}} column {{definitionColumn}} is used outside of binding context." + } + }, + + create(context) { + let stack = []; + const sourceCode = context.sourceCode; + + /** + * Makes a block scope. + * @param {ASTNode} node A node of a scope. + * @returns {void} + */ + function enterScope(node) { + stack.push(node.range); + } + + /** + * Pops the last block scope. + * @returns {void} + */ + function exitScope() { + stack.pop(); + } + + /** + * Reports a given reference. + * @param {eslint-scope.Reference} reference A reference to report. + * @param {eslint-scope.Definition} definition A definition for which to report reference. + * @returns {void} + */ + function report(reference, definition) { + const identifier = reference.identifier; + const definitionPosition = definition.name.loc.start; + + context.report({ + node: identifier, + messageId: "outOfScope", + data: { + name: identifier.name, + definitionLine: definitionPosition.line, + definitionColumn: definitionPosition.column + 1 + } + }); + } + + /** + * Finds and reports references which are outside of valid scopes. + * @param {ASTNode} node A node to get variables. + * @returns {void} + */ + function checkForVariables(node) { + if (node.kind !== "var") { + return; + } + + // Defines a predicate to check whether or not a given reference is outside of valid scope. + const scopeRange = stack[stack.length - 1]; + + /** + * Check if a reference is out of scope + * @param {ASTNode} reference node to examine + * @returns {boolean} True is its outside the scope + * @private + */ + function isOutsideOfScope(reference) { + const idRange = reference.identifier.range; + + return idRange[0] < scopeRange[0] || idRange[1] > scopeRange[1]; + } + + // Gets declared variables, and checks its references. + const variables = sourceCode.getDeclaredVariables(node); + + for (let i = 0; i < variables.length; ++i) { + + // Reports. + variables[i] + .references + .filter(isOutsideOfScope) + .forEach(ref => report(ref, variables[i].defs.find(def => def.parent === node))); + } + } + + return { + Program(node) { + stack = [node.range]; + }, + + // Manages scopes. + BlockStatement: enterScope, + "BlockStatement:exit": exitScope, + ForStatement: enterScope, + "ForStatement:exit": exitScope, + ForInStatement: enterScope, + "ForInStatement:exit": exitScope, + ForOfStatement: enterScope, + "ForOfStatement:exit": exitScope, + SwitchStatement: enterScope, + "SwitchStatement:exit": exitScope, + CatchClause: enterScope, + "CatchClause:exit": exitScope, + StaticBlock: enterScope, + "StaticBlock:exit": exitScope, + + // Finds and reports references which are outside of valid scope. + VariableDeclaration: checkForVariables + }; + + } }; diff --git a/node_modules/eslint/lib/rules/block-spacing.js b/node_modules/eslint/lib/rules/block-spacing.js index 009f0586e..9ca461158 100644 --- a/node_modules/eslint/lib/rules/block-spacing.js +++ b/node_modules/eslint/lib/rules/block-spacing.js @@ -12,191 +12,163 @@ const util = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "block-spacing", - url: "https://eslint.style/rules/block-spacing", - }, - }, - ], - }, - type: "layout", - - docs: { - description: - "Disallow or enforce spaces inside of blocks after opening block and before closing block", - recommended: false, - url: "https://eslint.org/docs/latest/rules/block-spacing", - }, - - fixable: "whitespace", - - schema: [{ enum: ["always", "never"] }], - - messages: { - missing: "Requires a space {{location}} '{{token}}'.", - extra: "Unexpected space(s) {{location}} '{{token}}'.", - }, - }, - - create(context) { - const always = context.options[0] !== "never", - messageId = always ? "missing" : "extra", - sourceCode = context.sourceCode; - - /** - * Gets the open brace token from a given node. - * @param {ASTNode} node A BlockStatement/StaticBlock/SwitchStatement node to get. - * @returns {Token} The token of the open brace. - */ - function getOpenBrace(node) { - if (node.type === "SwitchStatement") { - if (node.cases.length > 0) { - return sourceCode.getTokenBefore(node.cases[0]); - } - return sourceCode.getLastToken(node, 1); - } - - if (node.type === "StaticBlock") { - return sourceCode.getFirstToken(node, { skip: 1 }); // skip the `static` token - } - - // "BlockStatement" - return sourceCode.getFirstToken(node); - } - - /** - * Checks whether or not: - * - given tokens are on same line. - * - there is/isn't a space between given tokens. - * @param {Token} left A token to check. - * @param {Token} right The token which is next to `left`. - * @returns {boolean} - * When the option is `"always"`, `true` if there are one or more spaces between given tokens. - * When the option is `"never"`, `true` if there are not any spaces between given tokens. - * If given tokens are not on same line, it's always `true`. - */ - function isValid(left, right) { - return ( - !util.isTokenOnSameLine(left, right) || - sourceCode.isSpaceBetweenTokens(left, right) === always - ); - } - - /** - * Checks and reports invalid spacing style inside braces. - * @param {ASTNode} node A BlockStatement/StaticBlock/SwitchStatement node to check. - * @returns {void} - */ - function checkSpacingInsideBraces(node) { - // Gets braces and the first/last token of content. - const openBrace = getOpenBrace(node); - const closeBrace = sourceCode.getLastToken(node); - const firstToken = sourceCode.getTokenAfter(openBrace, { - includeComments: true, - }); - const lastToken = sourceCode.getTokenBefore(closeBrace, { - includeComments: true, - }); - - // Skip if the node is invalid or empty. - if ( - openBrace.type !== "Punctuator" || - openBrace.value !== "{" || - closeBrace.type !== "Punctuator" || - closeBrace.value !== "}" || - firstToken === closeBrace - ) { - return; - } - - // Skip line comments for option never - if (!always && firstToken.type === "Line") { - return; - } - - // Check. - if (!isValid(openBrace, firstToken)) { - let loc = openBrace.loc; - - if (messageId === "extra") { - loc = { - start: openBrace.loc.end, - end: firstToken.loc.start, - }; - } - - context.report({ - node, - loc, - messageId, - data: { - location: "after", - token: openBrace.value, - }, - fix(fixer) { - if (always) { - return fixer.insertTextBefore(firstToken, " "); - } - - return fixer.removeRange([ - openBrace.range[1], - firstToken.range[0], - ]); - }, - }); - } - if (!isValid(lastToken, closeBrace)) { - let loc = closeBrace.loc; - - if (messageId === "extra") { - loc = { - start: lastToken.loc.end, - end: closeBrace.loc.start, - }; - } - context.report({ - node, - loc, - messageId, - data: { - location: "before", - token: closeBrace.value, - }, - fix(fixer) { - if (always) { - return fixer.insertTextAfter(lastToken, " "); - } - - return fixer.removeRange([ - lastToken.range[1], - closeBrace.range[0], - ]); - }, - }); - } - } - - return { - BlockStatement: checkSpacingInsideBraces, - StaticBlock: checkSpacingInsideBraces, - SwitchStatement: checkSpacingInsideBraces, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Disallow or enforce spaces inside of blocks after opening block and before closing block", + recommended: false, + url: "https://eslint.org/docs/latest/rules/block-spacing" + }, + + fixable: "whitespace", + + schema: [ + { enum: ["always", "never"] } + ], + + messages: { + missing: "Requires a space {{location}} '{{token}}'.", + extra: "Unexpected space(s) {{location}} '{{token}}'." + } + }, + + create(context) { + const always = (context.options[0] !== "never"), + messageId = always ? "missing" : "extra", + sourceCode = context.sourceCode; + + /** + * Gets the open brace token from a given node. + * @param {ASTNode} node A BlockStatement/StaticBlock/SwitchStatement node to get. + * @returns {Token} The token of the open brace. + */ + function getOpenBrace(node) { + if (node.type === "SwitchStatement") { + if (node.cases.length > 0) { + return sourceCode.getTokenBefore(node.cases[0]); + } + return sourceCode.getLastToken(node, 1); + } + + if (node.type === "StaticBlock") { + return sourceCode.getFirstToken(node, { skip: 1 }); // skip the `static` token + } + + // "BlockStatement" + return sourceCode.getFirstToken(node); + } + + /** + * Checks whether or not: + * - given tokens are on same line. + * - there is/isn't a space between given tokens. + * @param {Token} left A token to check. + * @param {Token} right The token which is next to `left`. + * @returns {boolean} + * When the option is `"always"`, `true` if there are one or more spaces between given tokens. + * When the option is `"never"`, `true` if there are not any spaces between given tokens. + * If given tokens are not on same line, it's always `true`. + */ + function isValid(left, right) { + return ( + !util.isTokenOnSameLine(left, right) || + sourceCode.isSpaceBetweenTokens(left, right) === always + ); + } + + /** + * Checks and reports invalid spacing style inside braces. + * @param {ASTNode} node A BlockStatement/StaticBlock/SwitchStatement node to check. + * @returns {void} + */ + function checkSpacingInsideBraces(node) { + + // Gets braces and the first/last token of content. + const openBrace = getOpenBrace(node); + const closeBrace = sourceCode.getLastToken(node); + const firstToken = sourceCode.getTokenAfter(openBrace, { includeComments: true }); + const lastToken = sourceCode.getTokenBefore(closeBrace, { includeComments: true }); + + // Skip if the node is invalid or empty. + if (openBrace.type !== "Punctuator" || + openBrace.value !== "{" || + closeBrace.type !== "Punctuator" || + closeBrace.value !== "}" || + firstToken === closeBrace + ) { + return; + } + + // Skip line comments for option never + if (!always && firstToken.type === "Line") { + return; + } + + // Check. + if (!isValid(openBrace, firstToken)) { + let loc = openBrace.loc; + + if (messageId === "extra") { + loc = { + start: openBrace.loc.end, + end: firstToken.loc.start + }; + } + + context.report({ + node, + loc, + messageId, + data: { + location: "after", + token: openBrace.value + }, + fix(fixer) { + if (always) { + return fixer.insertTextBefore(firstToken, " "); + } + + return fixer.removeRange([openBrace.range[1], firstToken.range[0]]); + } + }); + } + if (!isValid(lastToken, closeBrace)) { + let loc = closeBrace.loc; + + if (messageId === "extra") { + loc = { + start: lastToken.loc.end, + end: closeBrace.loc.start + }; + } + context.report({ + node, + loc, + messageId, + data: { + location: "before", + token: closeBrace.value + }, + fix(fixer) { + if (always) { + return fixer.insertTextAfter(lastToken, " "); + } + + return fixer.removeRange([lastToken.range[1], closeBrace.range[0]]); + } + }); + } + } + + return { + BlockStatement: checkSpacingInsideBraces, + StaticBlock: checkSpacingInsideBraces, + SwitchStatement: checkSpacingInsideBraces + }; + } }; diff --git a/node_modules/eslint/lib/rules/brace-style.js b/node_modules/eslint/lib/rules/brace-style.js index f43884003..0fb4c65e6 100644 --- a/node_modules/eslint/lib/rules/brace-style.js +++ b/node_modules/eslint/lib/rules/brace-style.js @@ -12,267 +12,186 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "brace-style", - url: "https://eslint.style/rules/brace-style", - }, - }, - ], - }, - type: "layout", - - docs: { - description: "Enforce consistent brace style for blocks", - recommended: false, - url: "https://eslint.org/docs/latest/rules/brace-style", - }, - - schema: [ - { - enum: ["1tbs", "stroustrup", "allman"], - }, - { - type: "object", - properties: { - allowSingleLine: { - type: "boolean", - default: false, - }, - }, - additionalProperties: false, - }, - ], - - fixable: "whitespace", - - messages: { - nextLineOpen: - "Opening curly brace does not appear on the same line as controlling statement.", - sameLineOpen: - "Opening curly brace appears on the same line as controlling statement.", - blockSameLine: - "Statement inside of curly braces should be on next line.", - nextLineClose: - "Closing curly brace does not appear on the same line as the subsequent block.", - singleLineClose: - "Closing curly brace should be on the same line as opening curly brace or on the line after the previous block.", - sameLineClose: - "Closing curly brace appears on the same line as the subsequent block.", - }, - }, - - create(context) { - const style = context.options[0] || "1tbs", - params = context.options[1] || {}, - sourceCode = context.sourceCode; - - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - /** - * Fixes a place where a newline unexpectedly appears - * @param {Token} firstToken The token before the unexpected newline - * @param {Token} secondToken The token after the unexpected newline - * @returns {Function} A fixer function to remove the newlines between the tokens - */ - function removeNewlineBetween(firstToken, secondToken) { - const textRange = [firstToken.range[1], secondToken.range[0]]; - const textBetween = sourceCode.text.slice( - textRange[0], - textRange[1], - ); - - // Don't do a fix if there is a comment between the tokens - if (textBetween.trim()) { - return null; - } - return fixer => fixer.replaceTextRange(textRange, " "); - } - - /** - * Validates a pair of curly brackets based on the user's config - * @param {Token} openingCurly The opening curly bracket - * @param {Token} closingCurly The closing curly bracket - * @returns {void} - */ - function validateCurlyPair(openingCurly, closingCurly) { - const tokenBeforeOpeningCurly = - sourceCode.getTokenBefore(openingCurly); - const tokenAfterOpeningCurly = - sourceCode.getTokenAfter(openingCurly); - const tokenBeforeClosingCurly = - sourceCode.getTokenBefore(closingCurly); - const singleLineException = - params.allowSingleLine && - astUtils.isTokenOnSameLine(openingCurly, closingCurly); - - if ( - style !== "allman" && - !astUtils.isTokenOnSameLine( - tokenBeforeOpeningCurly, - openingCurly, - ) - ) { - context.report({ - node: openingCurly, - messageId: "nextLineOpen", - fix: removeNewlineBetween( - tokenBeforeOpeningCurly, - openingCurly, - ), - }); - } - - if ( - style === "allman" && - astUtils.isTokenOnSameLine( - tokenBeforeOpeningCurly, - openingCurly, - ) && - !singleLineException - ) { - context.report({ - node: openingCurly, - messageId: "sameLineOpen", - fix: fixer => fixer.insertTextBefore(openingCurly, "\n"), - }); - } - - if ( - astUtils.isTokenOnSameLine( - openingCurly, - tokenAfterOpeningCurly, - ) && - tokenAfterOpeningCurly !== closingCurly && - !singleLineException - ) { - context.report({ - node: openingCurly, - messageId: "blockSameLine", - fix: fixer => fixer.insertTextAfter(openingCurly, "\n"), - }); - } - - if ( - tokenBeforeClosingCurly !== openingCurly && - !singleLineException && - astUtils.isTokenOnSameLine( - tokenBeforeClosingCurly, - closingCurly, - ) - ) { - context.report({ - node: closingCurly, - messageId: "singleLineClose", - fix: fixer => fixer.insertTextBefore(closingCurly, "\n"), - }); - } - } - - /** - * Validates the location of a token that appears before a keyword (e.g. a newline before `else`) - * @param {Token} curlyToken The closing curly token. This is assumed to precede a keyword token (such as `else` or `finally`). - * @returns {void} - */ - function validateCurlyBeforeKeyword(curlyToken) { - const keywordToken = sourceCode.getTokenAfter(curlyToken); - - if ( - style === "1tbs" && - !astUtils.isTokenOnSameLine(curlyToken, keywordToken) - ) { - context.report({ - node: curlyToken, - messageId: "nextLineClose", - fix: removeNewlineBetween(curlyToken, keywordToken), - }); - } - - if ( - style !== "1tbs" && - astUtils.isTokenOnSameLine(curlyToken, keywordToken) - ) { - context.report({ - node: curlyToken, - messageId: "sameLineClose", - fix: fixer => fixer.insertTextAfter(curlyToken, "\n"), - }); - } - } - - //-------------------------------------------------------------------------- - // Public API - //-------------------------------------------------------------------------- - - return { - BlockStatement(node) { - if (!astUtils.STATEMENT_LIST_PARENTS.has(node.parent.type)) { - validateCurlyPair( - sourceCode.getFirstToken(node), - sourceCode.getLastToken(node), - ); - } - }, - StaticBlock(node) { - validateCurlyPair( - sourceCode.getFirstToken(node, { skip: 1 }), // skip the `static` token - sourceCode.getLastToken(node), - ); - }, - ClassBody(node) { - validateCurlyPair( - sourceCode.getFirstToken(node), - sourceCode.getLastToken(node), - ); - }, - SwitchStatement(node) { - const closingCurly = sourceCode.getLastToken(node); - const openingCurly = sourceCode.getTokenBefore( - node.cases.length ? node.cases[0] : closingCurly, - ); - - validateCurlyPair(openingCurly, closingCurly); - }, - IfStatement(node) { - if ( - node.consequent.type === "BlockStatement" && - node.alternate - ) { - // Handle the keyword after the `if` block (before `else`) - validateCurlyBeforeKeyword( - sourceCode.getLastToken(node.consequent), - ); - } - }, - TryStatement(node) { - // Handle the keyword after the `try` block (before `catch` or `finally`) - validateCurlyBeforeKeyword(sourceCode.getLastToken(node.block)); - - if (node.handler && node.finalizer) { - // Handle the keyword after the `catch` block (before `finally`) - validateCurlyBeforeKeyword( - sourceCode.getLastToken(node.handler.body), - ); - } - }, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Enforce consistent brace style for blocks", + recommended: false, + url: "https://eslint.org/docs/latest/rules/brace-style" + }, + + schema: [ + { + enum: ["1tbs", "stroustrup", "allman"] + }, + { + type: "object", + properties: { + allowSingleLine: { + type: "boolean", + default: false + } + }, + additionalProperties: false + } + ], + + fixable: "whitespace", + + messages: { + nextLineOpen: "Opening curly brace does not appear on the same line as controlling statement.", + sameLineOpen: "Opening curly brace appears on the same line as controlling statement.", + blockSameLine: "Statement inside of curly braces should be on next line.", + nextLineClose: "Closing curly brace does not appear on the same line as the subsequent block.", + singleLineClose: "Closing curly brace should be on the same line as opening curly brace or on the line after the previous block.", + sameLineClose: "Closing curly brace appears on the same line as the subsequent block." + } + }, + + create(context) { + const style = context.options[0] || "1tbs", + params = context.options[1] || {}, + sourceCode = context.sourceCode; + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + /** + * Fixes a place where a newline unexpectedly appears + * @param {Token} firstToken The token before the unexpected newline + * @param {Token} secondToken The token after the unexpected newline + * @returns {Function} A fixer function to remove the newlines between the tokens + */ + function removeNewlineBetween(firstToken, secondToken) { + const textRange = [firstToken.range[1], secondToken.range[0]]; + const textBetween = sourceCode.text.slice(textRange[0], textRange[1]); + + // Don't do a fix if there is a comment between the tokens + if (textBetween.trim()) { + return null; + } + return fixer => fixer.replaceTextRange(textRange, " "); + } + + /** + * Validates a pair of curly brackets based on the user's config + * @param {Token} openingCurly The opening curly bracket + * @param {Token} closingCurly The closing curly bracket + * @returns {void} + */ + function validateCurlyPair(openingCurly, closingCurly) { + const tokenBeforeOpeningCurly = sourceCode.getTokenBefore(openingCurly); + const tokenAfterOpeningCurly = sourceCode.getTokenAfter(openingCurly); + const tokenBeforeClosingCurly = sourceCode.getTokenBefore(closingCurly); + const singleLineException = params.allowSingleLine && astUtils.isTokenOnSameLine(openingCurly, closingCurly); + + if (style !== "allman" && !astUtils.isTokenOnSameLine(tokenBeforeOpeningCurly, openingCurly)) { + context.report({ + node: openingCurly, + messageId: "nextLineOpen", + fix: removeNewlineBetween(tokenBeforeOpeningCurly, openingCurly) + }); + } + + if (style === "allman" && astUtils.isTokenOnSameLine(tokenBeforeOpeningCurly, openingCurly) && !singleLineException) { + context.report({ + node: openingCurly, + messageId: "sameLineOpen", + fix: fixer => fixer.insertTextBefore(openingCurly, "\n") + }); + } + + if (astUtils.isTokenOnSameLine(openingCurly, tokenAfterOpeningCurly) && tokenAfterOpeningCurly !== closingCurly && !singleLineException) { + context.report({ + node: openingCurly, + messageId: "blockSameLine", + fix: fixer => fixer.insertTextAfter(openingCurly, "\n") + }); + } + + if (tokenBeforeClosingCurly !== openingCurly && !singleLineException && astUtils.isTokenOnSameLine(tokenBeforeClosingCurly, closingCurly)) { + context.report({ + node: closingCurly, + messageId: "singleLineClose", + fix: fixer => fixer.insertTextBefore(closingCurly, "\n") + }); + } + } + + /** + * Validates the location of a token that appears before a keyword (e.g. a newline before `else`) + * @param {Token} curlyToken The closing curly token. This is assumed to precede a keyword token (such as `else` or `finally`). + * @returns {void} + */ + function validateCurlyBeforeKeyword(curlyToken) { + const keywordToken = sourceCode.getTokenAfter(curlyToken); + + if (style === "1tbs" && !astUtils.isTokenOnSameLine(curlyToken, keywordToken)) { + context.report({ + node: curlyToken, + messageId: "nextLineClose", + fix: removeNewlineBetween(curlyToken, keywordToken) + }); + } + + if (style !== "1tbs" && astUtils.isTokenOnSameLine(curlyToken, keywordToken)) { + context.report({ + node: curlyToken, + messageId: "sameLineClose", + fix: fixer => fixer.insertTextAfter(curlyToken, "\n") + }); + } + } + + //-------------------------------------------------------------------------- + // Public API + //-------------------------------------------------------------------------- + + return { + BlockStatement(node) { + if (!astUtils.STATEMENT_LIST_PARENTS.has(node.parent.type)) { + validateCurlyPair(sourceCode.getFirstToken(node), sourceCode.getLastToken(node)); + } + }, + StaticBlock(node) { + validateCurlyPair( + sourceCode.getFirstToken(node, { skip: 1 }), // skip the `static` token + sourceCode.getLastToken(node) + ); + }, + ClassBody(node) { + validateCurlyPair(sourceCode.getFirstToken(node), sourceCode.getLastToken(node)); + }, + SwitchStatement(node) { + const closingCurly = sourceCode.getLastToken(node); + const openingCurly = sourceCode.getTokenBefore(node.cases.length ? node.cases[0] : closingCurly); + + validateCurlyPair(openingCurly, closingCurly); + }, + IfStatement(node) { + if (node.consequent.type === "BlockStatement" && node.alternate) { + + // Handle the keyword after the `if` block (before `else`) + validateCurlyBeforeKeyword(sourceCode.getLastToken(node.consequent)); + } + }, + TryStatement(node) { + + // Handle the keyword after the `try` block (before `catch` or `finally`) + validateCurlyBeforeKeyword(sourceCode.getLastToken(node.block)); + + if (node.handler && node.finalizer) { + + // Handle the keyword after the `catch` block (before `finally`) + validateCurlyBeforeKeyword(sourceCode.getLastToken(node.handler.body)); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/callback-return.js b/node_modules/eslint/lib/rules/callback-return.js index 471ecf261..5d441bdd9 100644 --- a/node_modules/eslint/lib/rules/callback-return.js +++ b/node_modules/eslint/lib/rules/callback-return.js @@ -9,208 +9,179 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Node.js rules were moved out of ESLint core.", - url: "https://eslint.org/docs/latest/use/migrating-to-7.0.0#deprecate-node-rules", - deprecatedSince: "7.0.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "eslint-plugin-n now maintains deprecated Node.js-related rules.", - plugin: { - name: "eslint-plugin-n", - url: "https://github.com/eslint-community/eslint-plugin-n", - }, - rule: { - name: "callback-return", - url: "https://github.com/eslint-community/eslint-plugin-n/tree/master/docs/rules/callback-return.md", - }, - }, - ], - }, - - type: "suggestion", - - docs: { - description: "Require `return` statements after callbacks", - recommended: false, - url: "https://eslint.org/docs/latest/rules/callback-return", - }, - - schema: [ - { - type: "array", - items: { type: "string" }, - }, - ], - - messages: { - missingReturn: "Expected return with your callback function.", - }, - }, - - create(context) { - const callbacks = context.options[0] || ["callback", "cb", "next"], - sourceCode = context.sourceCode; - - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - /** - * Find the closest parent matching a list of types. - * @param {ASTNode} node The node whose parents we are searching - * @param {Array} types The node types to match - * @returns {ASTNode} The matched node or undefined. - */ - function findClosestParentOfType(node, types) { - if (!node.parent) { - return null; - } - if (!types.includes(node.parent.type)) { - return findClosestParentOfType(node.parent, types); - } - return node.parent; - } - - /** - * Check to see if a node contains only identifiers - * @param {ASTNode} node The node to check - * @returns {boolean} Whether or not the node contains only identifiers - */ - function containsOnlyIdentifiers(node) { - if (node.type === "Identifier") { - return true; - } - - if (node.type === "MemberExpression") { - if (node.object.type === "Identifier") { - return true; - } - if (node.object.type === "MemberExpression") { - return containsOnlyIdentifiers(node.object); - } - } - - return false; - } - - /** - * Check to see if a CallExpression is in our callback list. - * @param {ASTNode} node The node to check against our callback names list. - * @returns {boolean} Whether or not this function matches our callback name. - */ - function isCallback(node) { - return ( - containsOnlyIdentifiers(node.callee) && - callbacks.includes(sourceCode.getText(node.callee)) - ); - } - - /** - * Determines whether or not the callback is part of a callback expression. - * @param {ASTNode} node The callback node - * @param {ASTNode} parentNode The expression node - * @returns {boolean} Whether or not this is part of a callback expression - */ - function isCallbackExpression(node, parentNode) { - // ensure the parent node exists and is an expression - if (!parentNode || parentNode.type !== "ExpressionStatement") { - return false; - } - - // cb() - if (parentNode.expression === node) { - return true; - } - - // special case for cb && cb() and similar - if ( - parentNode.expression.type === "BinaryExpression" || - parentNode.expression.type === "LogicalExpression" - ) { - if (parentNode.expression.right === node) { - return true; - } - } - - return false; - } - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - return { - CallExpression(node) { - // if we're not a callback we can return - if (!isCallback(node)) { - return; - } - - // find the closest block, return or loop - const closestBlock = - findClosestParentOfType(node, [ - "BlockStatement", - "ReturnStatement", - "ArrowFunctionExpression", - ]) || {}; - - // if our parent is a return we know we're ok - if (closestBlock.type === "ReturnStatement") { - return; - } - - // arrow functions don't always have blocks and implicitly return - if (closestBlock.type === "ArrowFunctionExpression") { - return; - } - - // block statements are part of functions and most if statements - if (closestBlock.type === "BlockStatement") { - // find the last item in the block - const lastItem = closestBlock.body.at(-1); - - // if the callback is the last thing in a block that might be ok - if (isCallbackExpression(node, lastItem)) { - const parentType = closestBlock.parent.type; - - // but only if the block is part of a function - if ( - parentType === "FunctionExpression" || - parentType === "FunctionDeclaration" || - parentType === "ArrowFunctionExpression" - ) { - return; - } - } - - // ending a block with a return is also ok - if (lastItem.type === "ReturnStatement") { - // but only if the callback is immediately before - if ( - isCallbackExpression(node, closestBlock.body.at(-2)) - ) { - return; - } - } - } - - // as long as you're the child of a function at this point you should be asked to return - if ( - findClosestParentOfType(node, [ - "FunctionDeclaration", - "FunctionExpression", - "ArrowFunctionExpression", - ]) - ) { - context.report({ node, messageId: "missingReturn" }); - } - }, - }; - }, + meta: { + deprecated: true, + + replacedBy: [], + + type: "suggestion", + + docs: { + description: "Require `return` statements after callbacks", + recommended: false, + url: "https://eslint.org/docs/latest/rules/callback-return" + }, + + schema: [{ + type: "array", + items: { type: "string" } + }], + + messages: { + missingReturn: "Expected return with your callback function." + } + }, + + create(context) { + + const callbacks = context.options[0] || ["callback", "cb", "next"], + sourceCode = context.sourceCode; + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + /** + * Find the closest parent matching a list of types. + * @param {ASTNode} node The node whose parents we are searching + * @param {Array} types The node types to match + * @returns {ASTNode} The matched node or undefined. + */ + function findClosestParentOfType(node, types) { + if (!node.parent) { + return null; + } + if (!types.includes(node.parent.type)) { + return findClosestParentOfType(node.parent, types); + } + return node.parent; + } + + /** + * Check to see if a node contains only identifiers + * @param {ASTNode} node The node to check + * @returns {boolean} Whether or not the node contains only identifiers + */ + function containsOnlyIdentifiers(node) { + if (node.type === "Identifier") { + return true; + } + + if (node.type === "MemberExpression") { + if (node.object.type === "Identifier") { + return true; + } + if (node.object.type === "MemberExpression") { + return containsOnlyIdentifiers(node.object); + } + } + + return false; + } + + /** + * Check to see if a CallExpression is in our callback list. + * @param {ASTNode} node The node to check against our callback names list. + * @returns {boolean} Whether or not this function matches our callback name. + */ + function isCallback(node) { + return containsOnlyIdentifiers(node.callee) && callbacks.includes(sourceCode.getText(node.callee)); + } + + /** + * Determines whether or not the callback is part of a callback expression. + * @param {ASTNode} node The callback node + * @param {ASTNode} parentNode The expression node + * @returns {boolean} Whether or not this is part of a callback expression + */ + function isCallbackExpression(node, parentNode) { + + // ensure the parent node exists and is an expression + if (!parentNode || parentNode.type !== "ExpressionStatement") { + return false; + } + + // cb() + if (parentNode.expression === node) { + return true; + } + + // special case for cb && cb() and similar + if (parentNode.expression.type === "BinaryExpression" || parentNode.expression.type === "LogicalExpression") { + if (parentNode.expression.right === node) { + return true; + } + } + + return false; + } + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + return { + CallExpression(node) { + + // if we're not a callback we can return + if (!isCallback(node)) { + return; + } + + // find the closest block, return or loop + const closestBlock = findClosestParentOfType(node, ["BlockStatement", "ReturnStatement", "ArrowFunctionExpression"]) || {}; + + // if our parent is a return we know we're ok + if (closestBlock.type === "ReturnStatement") { + return; + } + + // arrow functions don't always have blocks and implicitly return + if (closestBlock.type === "ArrowFunctionExpression") { + return; + } + + // block statements are part of functions and most if statements + if (closestBlock.type === "BlockStatement") { + + // find the last item in the block + const lastItem = closestBlock.body[closestBlock.body.length - 1]; + + // if the callback is the last thing in a block that might be ok + if (isCallbackExpression(node, lastItem)) { + + const parentType = closestBlock.parent.type; + + // but only if the block is part of a function + if (parentType === "FunctionExpression" || + parentType === "FunctionDeclaration" || + parentType === "ArrowFunctionExpression" + ) { + return; + } + + } + + // ending a block with a return is also ok + if (lastItem.type === "ReturnStatement") { + + // but only if the callback is immediately before + if (isCallbackExpression(node, closestBlock.body[closestBlock.body.length - 2])) { + return; + } + } + + } + + // as long as you're the child of a function at this point you should be asked to return + if (findClosestParentOfType(node, ["FunctionDeclaration", "FunctionExpression", "ArrowFunctionExpression"])) { + context.report({ node, messageId: "missingReturn" }); + } + + } + + }; + } }; diff --git a/node_modules/eslint/lib/rules/camelcase.js b/node_modules/eslint/lib/rules/camelcase.js index dad6b120b..51bb4122d 100644 --- a/node_modules/eslint/lib/rules/camelcase.js +++ b/node_modules/eslint/lib/rules/camelcase.js @@ -15,408 +15,385 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - defaultOptions: [ - { - allow: [], - ignoreDestructuring: false, - ignoreGlobals: false, - ignoreImports: false, - properties: "always", - }, - ], - - docs: { - description: "Enforce camelcase naming convention", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/camelcase", - }, - - schema: [ - { - type: "object", - properties: { - ignoreDestructuring: { - type: "boolean", - }, - ignoreImports: { - type: "boolean", - }, - ignoreGlobals: { - type: "boolean", - }, - properties: { - enum: ["always", "never"], - }, - allow: { - type: "array", - items: { - type: "string", - }, - minItems: 0, - uniqueItems: true, - }, - }, - additionalProperties: false, - }, - ], - - messages: { - notCamelCase: "Identifier '{{name}}' is not in camel case.", - notCamelCasePrivate: "#{{name}} is not in camel case.", - }, - }, - - create(context) { - const [ - { - allow, - ignoreDestructuring, - ignoreGlobals, - ignoreImports, - properties, - }, - ] = context.options; - const sourceCode = context.sourceCode; - - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - // contains reported nodes to avoid reporting twice on destructuring with shorthand notation - const reported = new Set(); - - /** - * Checks if a string contains an underscore and isn't all upper-case - * @param {string} name The string to check. - * @returns {boolean} if the string is underscored - * @private - */ - function isUnderscored(name) { - const nameBody = name.replace(/^_+|_+$/gu, ""); - - // if there's an underscore, it might be A_CONSTANT, which is okay - return ( - nameBody.includes("_") && nameBody !== nameBody.toUpperCase() - ); - } - - /** - * Checks if a string match the ignore list - * @param {string} name The string to check. - * @returns {boolean} if the string is ignored - * @private - */ - function isAllowed(name) { - return allow.some( - entry => name === entry || name.match(new RegExp(entry, "u")), - ); - } - - /** - * Checks if a given name is good or not. - * @param {string} name The name to check. - * @returns {boolean} `true` if the name is good. - * @private - */ - function isGoodName(name) { - return !isUnderscored(name) || isAllowed(name); - } - - /** - * Checks if a given identifier reference or member expression is an assignment - * target. - * @param {ASTNode} node The node to check. - * @returns {boolean} `true` if the node is an assignment target. - */ - function isAssignmentTarget(node) { - const parent = node.parent; - - switch (parent.type) { - case "AssignmentExpression": - case "AssignmentPattern": - return parent.left === node; - - case "Property": - return ( - parent.parent.type === "ObjectPattern" && - parent.value === node - ); - case "ArrayPattern": - case "RestElement": - return true; - - default: - return false; - } - } - - /** - * Checks if a given binding identifier uses the original name as-is. - * - If it's in object destructuring or object expression, the original name is its property name. - * - If it's in import declaration, the original name is its exported name. - * @param {ASTNode} node The `Identifier` node to check. - * @returns {boolean} `true` if the identifier uses the original name as-is. - */ - function equalsToOriginalName(node) { - const localName = node.name; - const valueNode = - node.parent.type === "AssignmentPattern" ? node.parent : node; - const parent = valueNode.parent; - - switch (parent.type) { - case "Property": - return ( - (parent.parent.type === "ObjectPattern" || - parent.parent.type === "ObjectExpression") && - parent.value === valueNode && - !parent.computed && - parent.key.type === "Identifier" && - parent.key.name === localName - ); - - case "ImportSpecifier": - return ( - parent.local === node && - astUtils.getModuleExportName(parent.imported) === - localName - ); - - default: - return false; - } - } - - /** - * Reports an AST node as a rule violation. - * @param {ASTNode} node The node to report. - * @returns {void} - * @private - */ - function report(node) { - if (reported.has(node.range[0])) { - return; - } - reported.add(node.range[0]); - - // Report it. - context.report({ - node, - messageId: - node.type === "PrivateIdentifier" - ? "notCamelCasePrivate" - : "notCamelCase", - data: { name: node.name }, - }); - } - - /** - * Reports an identifier reference or a binding identifier. - * @param {ASTNode} node The `Identifier` node to report. - * @returns {void} - */ - function reportReferenceId(node) { - /* - * For backward compatibility, if it's in callings then ignore it. - * Not sure why it is. - */ - if ( - node.parent.type === "CallExpression" || - node.parent.type === "NewExpression" - ) { - return; - } - - /* - * For backward compatibility, if it's a default value of - * destructuring/parameters then ignore it. - * Not sure why it is. - */ - if ( - node.parent.type === "AssignmentPattern" && - node.parent.right === node - ) { - return; - } - - /* - * The `ignoreDestructuring` flag skips the identifiers that uses - * the property name as-is. - */ - if (ignoreDestructuring && equalsToOriginalName(node)) { - return; - } - - /* - * Import attribute keys are always ignored - */ - if (astUtils.isImportAttributeKey(node)) { - return; - } - - report(node); - } - - return { - // Report camelcase of global variable references ------------------ - Program(node) { - const scope = sourceCode.getScope(node); - - if (!ignoreGlobals) { - // Defined globals in config files or directive comments. - for (const variable of scope.variables) { - if ( - variable.identifiers.length > 0 || - isGoodName(variable.name) - ) { - continue; - } - for (const reference of variable.references) { - /* - * For backward compatibility, this rule reports read-only - * references as well. - */ - reportReferenceId(reference.identifier); - } - } - } - - // Undefined globals. - for (const reference of scope.through) { - const id = reference.identifier; - - if ( - isGoodName(id.name) || - astUtils.isImportAttributeKey(id) - ) { - continue; - } - - /* - * For backward compatibility, this rule reports read-only - * references as well. - */ - reportReferenceId(id); - } - }, - - // Report camelcase of declared variables -------------------------- - [[ - "VariableDeclaration", - "FunctionDeclaration", - "FunctionExpression", - "ArrowFunctionExpression", - "ClassDeclaration", - "ClassExpression", - "CatchClause", - ]](node) { - for (const variable of sourceCode.getDeclaredVariables(node)) { - if (isGoodName(variable.name)) { - continue; - } - const id = variable.identifiers[0]; - - // Report declaration. - if (!(ignoreDestructuring && equalsToOriginalName(id))) { - report(id); - } - - /* - * For backward compatibility, report references as well. - * It looks unnecessary because declarations are reported. - */ - for (const reference of variable.references) { - if (reference.init) { - continue; // Skip the write references of initializers. - } - reportReferenceId(reference.identifier); - } - } - }, - - // Report camelcase in properties ---------------------------------- - [[ - "ObjectExpression > Property[computed!=true] > Identifier.key", - "MethodDefinition[computed!=true] > Identifier.key", - "PropertyDefinition[computed!=true] > Identifier.key", - "MethodDefinition > PrivateIdentifier.key", - "PropertyDefinition > PrivateIdentifier.key", - ]](node) { - if ( - properties === "never" || - astUtils.isImportAttributeKey(node) || - isGoodName(node.name) - ) { - return; - } - report(node); - }, - "MemberExpression[computed!=true] > Identifier.property"(node) { - if ( - properties === "never" || - !isAssignmentTarget(node.parent) || // ← ignore read-only references. - isGoodName(node.name) - ) { - return; - } - report(node); - }, - - // Report camelcase in import -------------------------------------- - ImportDeclaration(node) { - for (const variable of sourceCode.getDeclaredVariables(node)) { - if (isGoodName(variable.name)) { - continue; - } - const id = variable.identifiers[0]; - - // Report declaration. - if (!(ignoreImports && equalsToOriginalName(id))) { - report(id); - } - - /* - * For backward compatibility, report references as well. - * It looks unnecessary because declarations are reported. - */ - for (const reference of variable.references) { - reportReferenceId(reference.identifier); - } - } - }, - - // Report camelcase in re-export ----------------------------------- - [[ - "ExportAllDeclaration > Identifier.exported", - "ExportSpecifier > Identifier.exported", - ]](node) { - if (isGoodName(node.name)) { - return; - } - report(node); - }, - - // Report camelcase in labels -------------------------------------- - [[ - "LabeledStatement > Identifier.label", - - /* - * For backward compatibility, report references as well. - * It looks unnecessary because declarations are reported. - */ - "BreakStatement > Identifier.label", - "ContinueStatement > Identifier.label", - ]](node) { - if (isGoodName(node.name)) { - return; - } - report(node); - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Enforce camelcase naming convention", + recommended: false, + url: "https://eslint.org/docs/latest/rules/camelcase" + }, + + schema: [ + { + type: "object", + properties: { + ignoreDestructuring: { + type: "boolean", + default: false + }, + ignoreImports: { + type: "boolean", + default: false + }, + ignoreGlobals: { + type: "boolean", + default: false + }, + properties: { + enum: ["always", "never"] + }, + allow: { + type: "array", + items: [ + { + type: "string" + } + ], + minItems: 0, + uniqueItems: true + } + }, + additionalProperties: false + } + ], + + messages: { + notCamelCase: "Identifier '{{name}}' is not in camel case.", + notCamelCasePrivate: "#{{name}} is not in camel case." + } + }, + + create(context) { + const options = context.options[0] || {}; + const properties = options.properties === "never" ? "never" : "always"; + const ignoreDestructuring = options.ignoreDestructuring; + const ignoreImports = options.ignoreImports; + const ignoreGlobals = options.ignoreGlobals; + const allow = options.allow || []; + const sourceCode = context.sourceCode; + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + // contains reported nodes to avoid reporting twice on destructuring with shorthand notation + const reported = new Set(); + + /** + * Checks if a string contains an underscore and isn't all upper-case + * @param {string} name The string to check. + * @returns {boolean} if the string is underscored + * @private + */ + function isUnderscored(name) { + const nameBody = name.replace(/^_+|_+$/gu, ""); + + // if there's an underscore, it might be A_CONSTANT, which is okay + return nameBody.includes("_") && nameBody !== nameBody.toUpperCase(); + } + + /** + * Checks if a string match the ignore list + * @param {string} name The string to check. + * @returns {boolean} if the string is ignored + * @private + */ + function isAllowed(name) { + return allow.some( + entry => name === entry || name.match(new RegExp(entry, "u")) + ); + } + + /** + * Checks if a given name is good or not. + * @param {string} name The name to check. + * @returns {boolean} `true` if the name is good. + * @private + */ + function isGoodName(name) { + return !isUnderscored(name) || isAllowed(name); + } + + /** + * Checks if a given identifier reference or member expression is an assignment + * target. + * @param {ASTNode} node The node to check. + * @returns {boolean} `true` if the node is an assignment target. + */ + function isAssignmentTarget(node) { + const parent = node.parent; + + switch (parent.type) { + case "AssignmentExpression": + case "AssignmentPattern": + return parent.left === node; + + case "Property": + return ( + parent.parent.type === "ObjectPattern" && + parent.value === node + ); + case "ArrayPattern": + case "RestElement": + return true; + + default: + return false; + } + } + + /** + * Checks if a given binding identifier uses the original name as-is. + * - If it's in object destructuring or object expression, the original name is its property name. + * - If it's in import declaration, the original name is its exported name. + * @param {ASTNode} node The `Identifier` node to check. + * @returns {boolean} `true` if the identifier uses the original name as-is. + */ + function equalsToOriginalName(node) { + const localName = node.name; + const valueNode = node.parent.type === "AssignmentPattern" + ? node.parent + : node; + const parent = valueNode.parent; + + switch (parent.type) { + case "Property": + return ( + (parent.parent.type === "ObjectPattern" || parent.parent.type === "ObjectExpression") && + parent.value === valueNode && + !parent.computed && + parent.key.type === "Identifier" && + parent.key.name === localName + ); + + case "ImportSpecifier": + return ( + parent.local === node && + astUtils.getModuleExportName(parent.imported) === localName + ); + + default: + return false; + } + } + + /** + * Reports an AST node as a rule violation. + * @param {ASTNode} node The node to report. + * @returns {void} + * @private + */ + function report(node) { + if (reported.has(node.range[0])) { + return; + } + reported.add(node.range[0]); + + // Report it. + context.report({ + node, + messageId: node.type === "PrivateIdentifier" + ? "notCamelCasePrivate" + : "notCamelCase", + data: { name: node.name } + }); + } + + /** + * Reports an identifier reference or a binding identifier. + * @param {ASTNode} node The `Identifier` node to report. + * @returns {void} + */ + function reportReferenceId(node) { + + /* + * For backward compatibility, if it's in callings then ignore it. + * Not sure why it is. + */ + if ( + node.parent.type === "CallExpression" || + node.parent.type === "NewExpression" + ) { + return; + } + + /* + * For backward compatibility, if it's a default value of + * destructuring/parameters then ignore it. + * Not sure why it is. + */ + if ( + node.parent.type === "AssignmentPattern" && + node.parent.right === node + ) { + return; + } + + /* + * The `ignoreDestructuring` flag skips the identifiers that uses + * the property name as-is. + */ + if (ignoreDestructuring && equalsToOriginalName(node)) { + return; + } + + report(node); + } + + return { + + // Report camelcase of global variable references ------------------ + Program(node) { + const scope = sourceCode.getScope(node); + + if (!ignoreGlobals) { + + // Defined globals in config files or directive comments. + for (const variable of scope.variables) { + if ( + variable.identifiers.length > 0 || + isGoodName(variable.name) + ) { + continue; + } + for (const reference of variable.references) { + + /* + * For backward compatibility, this rule reports read-only + * references as well. + */ + reportReferenceId(reference.identifier); + } + } + } + + // Undefined globals. + for (const reference of scope.through) { + const id = reference.identifier; + + if (isGoodName(id.name)) { + continue; + } + + /* + * For backward compatibility, this rule reports read-only + * references as well. + */ + reportReferenceId(id); + } + }, + + // Report camelcase of declared variables -------------------------- + [[ + "VariableDeclaration", + "FunctionDeclaration", + "FunctionExpression", + "ArrowFunctionExpression", + "ClassDeclaration", + "ClassExpression", + "CatchClause" + ]](node) { + for (const variable of sourceCode.getDeclaredVariables(node)) { + if (isGoodName(variable.name)) { + continue; + } + const id = variable.identifiers[0]; + + // Report declaration. + if (!(ignoreDestructuring && equalsToOriginalName(id))) { + report(id); + } + + /* + * For backward compatibility, report references as well. + * It looks unnecessary because declarations are reported. + */ + for (const reference of variable.references) { + if (reference.init) { + continue; // Skip the write references of initializers. + } + reportReferenceId(reference.identifier); + } + } + }, + + // Report camelcase in properties ---------------------------------- + [[ + "ObjectExpression > Property[computed!=true] > Identifier.key", + "MethodDefinition[computed!=true] > Identifier.key", + "PropertyDefinition[computed!=true] > Identifier.key", + "MethodDefinition > PrivateIdentifier.key", + "PropertyDefinition > PrivateIdentifier.key" + ]](node) { + if (properties === "never" || isGoodName(node.name)) { + return; + } + report(node); + }, + "MemberExpression[computed!=true] > Identifier.property"(node) { + if ( + properties === "never" || + !isAssignmentTarget(node.parent) || // ← ignore read-only references. + isGoodName(node.name) + ) { + return; + } + report(node); + }, + + // Report camelcase in import -------------------------------------- + ImportDeclaration(node) { + for (const variable of sourceCode.getDeclaredVariables(node)) { + if (isGoodName(variable.name)) { + continue; + } + const id = variable.identifiers[0]; + + // Report declaration. + if (!(ignoreImports && equalsToOriginalName(id))) { + report(id); + } + + /* + * For backward compatibility, report references as well. + * It looks unnecessary because declarations are reported. + */ + for (const reference of variable.references) { + reportReferenceId(reference.identifier); + } + } + }, + + // Report camelcase in re-export ----------------------------------- + [[ + "ExportAllDeclaration > Identifier.exported", + "ExportSpecifier > Identifier.exported" + ]](node) { + if (isGoodName(node.name)) { + return; + } + report(node); + }, + + // Report camelcase in labels -------------------------------------- + [[ + "LabeledStatement > Identifier.label", + + /* + * For backward compatibility, report references as well. + * It looks unnecessary because declarations are reported. + */ + "BreakStatement > Identifier.label", + "ContinueStatement > Identifier.label" + ]](node) { + if (isGoodName(node.name)) { + return; + } + report(node); + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/capitalized-comments.js b/node_modules/eslint/lib/rules/capitalized-comments.js index 17c53fc8b..3a17b0566 100644 --- a/node_modules/eslint/lib/rules/capitalized-comments.js +++ b/node_modules/eslint/lib/rules/capitalized-comments.js @@ -8,6 +8,7 @@ // Requirements //------------------------------------------------------------------------------ +const LETTER_PATTERN = require("./utils/patterns/letters"); const astUtils = require("./utils/ast-utils"); //------------------------------------------------------------------------------ @@ -15,9 +16,8 @@ const astUtils = require("./utils/ast-utils"); //------------------------------------------------------------------------------ const DEFAULT_IGNORE_PATTERN = astUtils.COMMENTS_IGNORE_PATTERN, - WHITESPACE = /\s/gu, - MAYBE_URL = /^\s*[^:/?#\s]+:\/\/[^?#]/u, // TODO: Combine w/ max-len pattern? - LETTER_PATTERN = /\p{L}/u; + WHITESPACE = /\s/gu, + MAYBE_URL = /^\s*[^:/?#\s]+:\/\/[^?#]/u; // TODO: Combine w/ max-len pattern? /* * Base schema body for defining the basic capitalization rule, ignorePattern, @@ -25,24 +25,24 @@ const DEFAULT_IGNORE_PATTERN = astUtils.COMMENTS_IGNORE_PATTERN, * This can be used in a few different ways in the actual schema. */ const SCHEMA_BODY = { - type: "object", - properties: { - ignorePattern: { - type: "string", - }, - ignoreInlineComments: { - type: "boolean", - }, - ignoreConsecutiveComments: { - type: "boolean", - }, - }, - additionalProperties: false, + type: "object", + properties: { + ignorePattern: { + type: "string" + }, + ignoreInlineComments: { + type: "boolean" + }, + ignoreConsecutiveComments: { + type: "boolean" + } + }, + additionalProperties: false }; const DEFAULTS = { - ignorePattern: "", - ignoreInlineComments: false, - ignoreConsecutiveComments: false, + ignorePattern: "", + ignoreInlineComments: false, + ignoreConsecutiveComments: false }; /** @@ -59,7 +59,7 @@ const DEFAULTS = { * @returns {Object} The normalized options. */ function getNormalizedOptions(rawOptions, which) { - return Object.assign({}, DEFAULTS, rawOptions[which] || rawOptions); + return Object.assign({}, DEFAULTS, rawOptions[which] || rawOptions); } /** @@ -69,10 +69,10 @@ function getNormalizedOptions(rawOptions, which) { * normalized options objects. */ function getAllNormalizedOptions(rawOptions = {}) { - return { - Line: getNormalizedOptions(rawOptions, "line"), - Block: getNormalizedOptions(rawOptions, "block"), - }; + return { + Line: getNormalizedOptions(rawOptions, "line"), + Block: getNormalizedOptions(rawOptions, "block") + }; } /** @@ -84,242 +84,217 @@ function getAllNormalizedOptions(rawOptions = {}) { * @returns {void} */ function createRegExpForIgnorePatterns(normalizedOptions) { - Object.keys(normalizedOptions).forEach(key => { - const ignorePatternStr = normalizedOptions[key].ignorePattern; + Object.keys(normalizedOptions).forEach(key => { + const ignorePatternStr = normalizedOptions[key].ignorePattern; - if (ignorePatternStr) { - const regExp = RegExp(`^\\s*(?:${ignorePatternStr})`, "u"); + if (ignorePatternStr) { + const regExp = RegExp(`^\\s*(?:${ignorePatternStr})`, "u"); - normalizedOptions[key].ignorePatternRegExp = regExp; - } - }); + normalizedOptions[key].ignorePatternRegExp = regExp; + } + }); } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: - "Enforce or disallow capitalization of the first letter of a comment", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/capitalized-comments", - }, - - fixable: "code", - - schema: [ - { enum: ["always", "never"] }, - { - oneOf: [ - SCHEMA_BODY, - { - type: "object", - properties: { - line: SCHEMA_BODY, - block: SCHEMA_BODY, - }, - additionalProperties: false, - }, - ], - }, - ], - - messages: { - unexpectedLowercaseComment: - "Comments should not begin with a lowercase character.", - unexpectedUppercaseComment: - "Comments should not begin with an uppercase character.", - }, - }, - - create(context) { - const capitalize = context.options[0] || "always", - normalizedOptions = getAllNormalizedOptions(context.options[1]), - sourceCode = context.sourceCode; - - createRegExpForIgnorePatterns(normalizedOptions); - - //---------------------------------------------------------------------- - // Helpers - //---------------------------------------------------------------------- - - /** - * Checks whether a comment is an inline comment. - * - * For the purpose of this rule, a comment is inline if: - * 1. The comment is preceded by a token on the same line; and - * 2. The command is followed by a token on the same line. - * - * Note that the comment itself need not be single-line! - * - * Also, it follows from this definition that only block comments can - * be considered as possibly inline. This is because line comments - * would consume any following tokens on the same line as the comment. - * @param {ASTNode} comment The comment node to check. - * @returns {boolean} True if the comment is an inline comment, false - * otherwise. - */ - function isInlineComment(comment) { - const previousToken = sourceCode.getTokenBefore(comment, { - includeComments: true, - }), - nextToken = sourceCode.getTokenAfter(comment, { - includeComments: true, - }); - - return Boolean( - previousToken && - nextToken && - comment.loc.start.line === previousToken.loc.end.line && - comment.loc.end.line === nextToken.loc.start.line, - ); - } - - /** - * Determine if a comment follows another comment. - * @param {ASTNode} comment The comment to check. - * @returns {boolean} True if the comment follows a valid comment. - */ - function isConsecutiveComment(comment) { - const previousTokenOrComment = sourceCode.getTokenBefore(comment, { - includeComments: true, - }); - - return Boolean( - previousTokenOrComment && - ["Block", "Line"].includes(previousTokenOrComment.type), - ); - } - - /** - * Check a comment to determine if it is valid for this rule. - * @param {ASTNode} comment The comment node to process. - * @param {Object} options The options for checking this comment. - * @returns {boolean} True if the comment is valid, false otherwise. - */ - function isCommentValid(comment, options) { - // 1. Check for default ignore pattern. - if (DEFAULT_IGNORE_PATTERN.test(comment.value)) { - return true; - } - - // 2. Check for custom ignore pattern. - const commentWithoutAsterisks = comment.value.replace(/\*/gu, ""); - - if ( - options.ignorePatternRegExp && - options.ignorePatternRegExp.test(commentWithoutAsterisks) - ) { - return true; - } - - // 3. Check for inline comments. - if (options.ignoreInlineComments && isInlineComment(comment)) { - return true; - } - - // 4. Is this a consecutive comment (and are we tolerating those)? - if ( - options.ignoreConsecutiveComments && - isConsecutiveComment(comment) - ) { - return true; - } - - // 5. Does the comment start with a possible URL? - if (MAYBE_URL.test(commentWithoutAsterisks)) { - return true; - } - - // 6. Is the initial word character a letter? - const commentWordCharsOnly = commentWithoutAsterisks.replace( - WHITESPACE, - "", - ); - - if (commentWordCharsOnly.length === 0) { - return true; - } - - // Get the first Unicode character (1 or 2 code units). - const [firstWordChar] = commentWordCharsOnly; - - if (!LETTER_PATTERN.test(firstWordChar)) { - return true; - } - - // 7. Check the case of the initial word character. - const isUppercase = - firstWordChar !== firstWordChar.toLocaleLowerCase(), - isLowercase = - firstWordChar !== firstWordChar.toLocaleUpperCase(); - - if (capitalize === "always" && isLowercase) { - return false; - } - if (capitalize === "never" && isUppercase) { - return false; - } - - return true; - } - - /** - * Process a comment to determine if it needs to be reported. - * @param {ASTNode} comment The comment node to process. - * @returns {void} - */ - function processComment(comment) { - const options = normalizedOptions[comment.type], - commentValid = isCommentValid(comment, options); - - if (!commentValid) { - const messageId = - capitalize === "always" - ? "unexpectedLowercaseComment" - : "unexpectedUppercaseComment"; - - context.report({ - node: null, // Intentionally using loc instead - loc: comment.loc, - messageId, - fix(fixer) { - const match = comment.value.match(LETTER_PATTERN); - const char = match[0]; - - // Offset match.index by 2 to account for the first 2 characters that start the comment (// or /*) - const charIndex = comment.range[0] + match.index + 2; - - return fixer.replaceTextRange( - [charIndex, charIndex + char.length], - capitalize === "always" - ? char.toLocaleUpperCase() - : char.toLocaleLowerCase(), - ); - }, - }); - } - } - - //---------------------------------------------------------------------- - // Public - //---------------------------------------------------------------------- - - return { - Program() { - const comments = sourceCode.getAllComments(); - - comments - .filter(token => token.type !== "Shebang") - .forEach(processComment); - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Enforce or disallow capitalization of the first letter of a comment", + recommended: false, + url: "https://eslint.org/docs/latest/rules/capitalized-comments" + }, + + fixable: "code", + + schema: [ + { enum: ["always", "never"] }, + { + oneOf: [ + SCHEMA_BODY, + { + type: "object", + properties: { + line: SCHEMA_BODY, + block: SCHEMA_BODY + }, + additionalProperties: false + } + ] + } + ], + + messages: { + unexpectedLowercaseComment: "Comments should not begin with a lowercase character.", + unexpectedUppercaseComment: "Comments should not begin with an uppercase character." + } + }, + + create(context) { + + const capitalize = context.options[0] || "always", + normalizedOptions = getAllNormalizedOptions(context.options[1]), + sourceCode = context.sourceCode; + + createRegExpForIgnorePatterns(normalizedOptions); + + //---------------------------------------------------------------------- + // Helpers + //---------------------------------------------------------------------- + + /** + * Checks whether a comment is an inline comment. + * + * For the purpose of this rule, a comment is inline if: + * 1. The comment is preceded by a token on the same line; and + * 2. The command is followed by a token on the same line. + * + * Note that the comment itself need not be single-line! + * + * Also, it follows from this definition that only block comments can + * be considered as possibly inline. This is because line comments + * would consume any following tokens on the same line as the comment. + * @param {ASTNode} comment The comment node to check. + * @returns {boolean} True if the comment is an inline comment, false + * otherwise. + */ + function isInlineComment(comment) { + const previousToken = sourceCode.getTokenBefore(comment, { includeComments: true }), + nextToken = sourceCode.getTokenAfter(comment, { includeComments: true }); + + return Boolean( + previousToken && + nextToken && + comment.loc.start.line === previousToken.loc.end.line && + comment.loc.end.line === nextToken.loc.start.line + ); + } + + /** + * Determine if a comment follows another comment. + * @param {ASTNode} comment The comment to check. + * @returns {boolean} True if the comment follows a valid comment. + */ + function isConsecutiveComment(comment) { + const previousTokenOrComment = sourceCode.getTokenBefore(comment, { includeComments: true }); + + return Boolean( + previousTokenOrComment && + ["Block", "Line"].includes(previousTokenOrComment.type) + ); + } + + /** + * Check a comment to determine if it is valid for this rule. + * @param {ASTNode} comment The comment node to process. + * @param {Object} options The options for checking this comment. + * @returns {boolean} True if the comment is valid, false otherwise. + */ + function isCommentValid(comment, options) { + + // 1. Check for default ignore pattern. + if (DEFAULT_IGNORE_PATTERN.test(comment.value)) { + return true; + } + + // 2. Check for custom ignore pattern. + const commentWithoutAsterisks = comment.value + .replace(/\*/gu, ""); + + if (options.ignorePatternRegExp && options.ignorePatternRegExp.test(commentWithoutAsterisks)) { + return true; + } + + // 3. Check for inline comments. + if (options.ignoreInlineComments && isInlineComment(comment)) { + return true; + } + + // 4. Is this a consecutive comment (and are we tolerating those)? + if (options.ignoreConsecutiveComments && isConsecutiveComment(comment)) { + return true; + } + + // 5. Does the comment start with a possible URL? + if (MAYBE_URL.test(commentWithoutAsterisks)) { + return true; + } + + // 6. Is the initial word character a letter? + const commentWordCharsOnly = commentWithoutAsterisks + .replace(WHITESPACE, ""); + + if (commentWordCharsOnly.length === 0) { + return true; + } + + const firstWordChar = commentWordCharsOnly[0]; + + if (!LETTER_PATTERN.test(firstWordChar)) { + return true; + } + + // 7. Check the case of the initial word character. + const isUppercase = firstWordChar !== firstWordChar.toLocaleLowerCase(), + isLowercase = firstWordChar !== firstWordChar.toLocaleUpperCase(); + + if (capitalize === "always" && isLowercase) { + return false; + } + if (capitalize === "never" && isUppercase) { + return false; + } + + return true; + } + + /** + * Process a comment to determine if it needs to be reported. + * @param {ASTNode} comment The comment node to process. + * @returns {void} + */ + function processComment(comment) { + const options = normalizedOptions[comment.type], + commentValid = isCommentValid(comment, options); + + if (!commentValid) { + const messageId = capitalize === "always" + ? "unexpectedLowercaseComment" + : "unexpectedUppercaseComment"; + + context.report({ + node: null, // Intentionally using loc instead + loc: comment.loc, + messageId, + fix(fixer) { + const match = comment.value.match(LETTER_PATTERN); + + return fixer.replaceTextRange( + + // Offset match.index by 2 to account for the first 2 characters that start the comment (// or /*) + [comment.range[0] + match.index + 2, comment.range[0] + match.index + 3], + capitalize === "always" ? match[0].toLocaleUpperCase() : match[0].toLocaleLowerCase() + ); + } + }); + } + } + + //---------------------------------------------------------------------- + // Public + //---------------------------------------------------------------------- + + return { + Program() { + const comments = sourceCode.getAllComments(); + + comments.filter(token => token.type !== "Shebang").forEach(processComment); + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/class-methods-use-this.js b/node_modules/eslint/lib/rules/class-methods-use-this.js index 5ce4893d8..9cf8a1b8a 100644 --- a/node_modules/eslint/lib/rules/class-methods-use-this.js +++ b/node_modules/eslint/lib/rules/class-methods-use-this.js @@ -15,236 +15,173 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - dialects: ["javascript", "typescript"], - language: "javascript", - type: "suggestion", - - defaultOptions: [ - { - enforceForClassFields: true, - exceptMethods: [], - ignoreOverrideMethods: false, - }, - ], - - docs: { - description: "Enforce that class methods utilize `this`", - recommended: false, - url: "https://eslint.org/docs/latest/rules/class-methods-use-this", - }, - - schema: [ - { - type: "object", - properties: { - exceptMethods: { - type: "array", - items: { - type: "string", - }, - }, - enforceForClassFields: { - type: "boolean", - }, - ignoreOverrideMethods: { - type: "boolean", - }, - ignoreClassesWithImplements: { - enum: ["all", "public-fields"], - }, - }, - additionalProperties: false, - }, - ], - - messages: { - missingThis: "Expected 'this' to be used by class {{name}}.", - }, - }, - create(context) { - const [options] = context.options; - const { - enforceForClassFields, - ignoreOverrideMethods, - ignoreClassesWithImplements, - } = options; - const exceptMethods = new Set(options.exceptMethods); - - const stack = []; - - /** - * Push `this` used flag initialized with `false` onto the stack. - * @returns {void} - */ - function pushContext() { - stack.push(false); - } - - /** - * Pop `this` used flag from the stack. - * @returns {boolean | undefined} `this` used flag - */ - function popContext() { - return stack.pop(); - } - - /** - * Initializes the current context to false and pushes it onto the stack. - * These booleans represent whether 'this' has been used in the context. - * @returns {void} - * @private - */ - function enterFunction() { - pushContext(); - } - - /** - * Check if the node is an instance method - * @param {ASTNode} node node to check - * @returns {boolean} True if its an instance method - * @private - */ - function isInstanceMethod(node) { - switch (node.type) { - case "MethodDefinition": - return !node.static && node.kind !== "constructor"; - case "AccessorProperty": - case "PropertyDefinition": - return !node.static && enforceForClassFields; - default: - return false; - } - } - - /** - * Check if the node's parent class implements any interfaces - * @param {ASTNode} node node to check - * @returns {boolean} True if parent class implements interfaces - * @private - */ - function hasImplements(node) { - const classNode = node.parent.parent; - return ( - classNode?.type === "ClassDeclaration" && - classNode.implements?.length > 0 - ); - } - - /** - * Check if the node is an instance method not excluded by config - * @param {ASTNode} node node to check - * @returns {boolean} True if it is an instance method, and not excluded by config - * @private - */ - function isIncludedInstanceMethod(node) { - if (isInstanceMethod(node)) { - if (node.computed) { - return true; - } - - if (ignoreOverrideMethods && node.override) { - return false; - } - - if (ignoreClassesWithImplements) { - const implementsInterfaces = hasImplements(node); - if (implementsInterfaces) { - if ( - ignoreClassesWithImplements === "all" || - (ignoreClassesWithImplements === "public-fields" && - node.key.type !== "PrivateIdentifier" && - (!node.accessibility || - node.accessibility === "public")) - ) { - return false; - } - } - } - - const hashIfNeeded = - node.key.type === "PrivateIdentifier" ? "#" : ""; - const name = - node.key.type === "Literal" - ? astUtils.getStaticStringValue(node.key) - : node.key.name || ""; - - return !exceptMethods.has(hashIfNeeded + name); - } - return false; - } - - /** - * Checks if we are leaving a function that is a method, and reports if 'this' has not been used. - * Static methods and the constructor are exempt. - * Then pops the context off the stack. - * @param {ASTNode} node A function node that was entered. - * @returns {void} - * @private - */ - function exitFunction(node) { - const methodUsesThis = popContext(); - - if (isIncludedInstanceMethod(node.parent) && !methodUsesThis) { - context.report({ - node, - loc: astUtils.getFunctionHeadLoc(node, context.sourceCode), - messageId: "missingThis", - data: { - name: astUtils.getFunctionNameWithKind(node), - }, - }); - } - } - - /** - * Mark the current context as having used 'this'. - * @returns {void} - * @private - */ - function markThisUsed() { - if (stack.length) { - stack[stack.length - 1] = true; - } - } - - return { - FunctionDeclaration: enterFunction, - "FunctionDeclaration:exit": exitFunction, - FunctionExpression: enterFunction, - "FunctionExpression:exit": exitFunction, - - /* - * Class field value are implicit functions. - */ - "AccessorProperty > *.key:exit": pushContext, - "AccessorProperty:exit": popContext, - "PropertyDefinition > *.key:exit": pushContext, - "PropertyDefinition:exit": popContext, - - /* - * Class static blocks are implicit functions. They aren't required to use `this`, - * but we have to push context so that it captures any use of `this` in the static block - * separately from enclosing contexts, because static blocks have their own `this` and it - * shouldn't count as used `this` in enclosing contexts. - */ - StaticBlock: pushContext, - "StaticBlock:exit": popContext, - - ThisExpression: markThisUsed, - Super: markThisUsed, - ...(enforceForClassFields && { - "AccessorProperty > ArrowFunctionExpression.value": - enterFunction, - "AccessorProperty > ArrowFunctionExpression.value:exit": - exitFunction, - "PropertyDefinition > ArrowFunctionExpression.value": - enterFunction, - "PropertyDefinition > ArrowFunctionExpression.value:exit": - exitFunction, - }), - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Enforce that class methods utilize `this`", + recommended: false, + url: "https://eslint.org/docs/latest/rules/class-methods-use-this" + }, + + schema: [{ + type: "object", + properties: { + exceptMethods: { + type: "array", + items: { + type: "string" + } + }, + enforceForClassFields: { + type: "boolean", + default: true + } + }, + additionalProperties: false + }], + + messages: { + missingThis: "Expected 'this' to be used by class {{name}}." + } + }, + create(context) { + const config = Object.assign({}, context.options[0]); + const enforceForClassFields = config.enforceForClassFields !== false; + const exceptMethods = new Set(config.exceptMethods || []); + + const stack = []; + + /** + * Push `this` used flag initialized with `false` onto the stack. + * @returns {void} + */ + function pushContext() { + stack.push(false); + } + + /** + * Pop `this` used flag from the stack. + * @returns {boolean | undefined} `this` used flag + */ + function popContext() { + return stack.pop(); + } + + /** + * Initializes the current context to false and pushes it onto the stack. + * These booleans represent whether 'this' has been used in the context. + * @returns {void} + * @private + */ + function enterFunction() { + pushContext(); + } + + /** + * Check if the node is an instance method + * @param {ASTNode} node node to check + * @returns {boolean} True if its an instance method + * @private + */ + function isInstanceMethod(node) { + switch (node.type) { + case "MethodDefinition": + return !node.static && node.kind !== "constructor"; + case "PropertyDefinition": + return !node.static && enforceForClassFields; + default: + return false; + } + } + + /** + * Check if the node is an instance method not excluded by config + * @param {ASTNode} node node to check + * @returns {boolean} True if it is an instance method, and not excluded by config + * @private + */ + function isIncludedInstanceMethod(node) { + if (isInstanceMethod(node)) { + if (node.computed) { + return true; + } + + const hashIfNeeded = node.key.type === "PrivateIdentifier" ? "#" : ""; + const name = node.key.type === "Literal" + ? astUtils.getStaticStringValue(node.key) + : (node.key.name || ""); + + return !exceptMethods.has(hashIfNeeded + name); + } + return false; + } + + /** + * Checks if we are leaving a function that is a method, and reports if 'this' has not been used. + * Static methods and the constructor are exempt. + * Then pops the context off the stack. + * @param {ASTNode} node A function node that was entered. + * @returns {void} + * @private + */ + function exitFunction(node) { + const methodUsesThis = popContext(); + + if (isIncludedInstanceMethod(node.parent) && !methodUsesThis) { + context.report({ + node, + loc: astUtils.getFunctionHeadLoc(node, context.sourceCode), + messageId: "missingThis", + data: { + name: astUtils.getFunctionNameWithKind(node) + } + }); + } + } + + /** + * Mark the current context as having used 'this'. + * @returns {void} + * @private + */ + function markThisUsed() { + if (stack.length) { + stack[stack.length - 1] = true; + } + } + + return { + FunctionDeclaration: enterFunction, + "FunctionDeclaration:exit": exitFunction, + FunctionExpression: enterFunction, + "FunctionExpression:exit": exitFunction, + + /* + * Class field value are implicit functions. + */ + "PropertyDefinition > *.key:exit": pushContext, + "PropertyDefinition:exit": popContext, + + /* + * Class static blocks are implicit functions. They aren't required to use `this`, + * but we have to push context so that it captures any use of `this` in the static block + * separately from enclosing contexts, because static blocks have their own `this` and it + * shouldn't count as used `this` in enclosing contexts. + */ + StaticBlock: pushContext, + "StaticBlock:exit": popContext, + + ThisExpression: markThisUsed, + Super: markThisUsed, + ...( + enforceForClassFields && { + "PropertyDefinition > ArrowFunctionExpression.value": enterFunction, + "PropertyDefinition > ArrowFunctionExpression.value:exit": exitFunction + } + ) + }; + } }; diff --git a/node_modules/eslint/lib/rules/comma-dangle.js b/node_modules/eslint/lib/rules/comma-dangle.js index 121ada3ce..5f4180f12 100644 --- a/node_modules/eslint/lib/rules/comma-dangle.js +++ b/node_modules/eslint/lib/rules/comma-dangle.js @@ -17,11 +17,11 @@ const astUtils = require("./utils/ast-utils"); //------------------------------------------------------------------------------ const DEFAULT_OPTIONS = Object.freeze({ - arrays: "never", - objects: "never", - imports: "never", - exports: "never", - functions: "never", + arrays: "never", + objects: "never", + imports: "never", + exports: "never", + functions: "never" }); /** @@ -31,11 +31,11 @@ const DEFAULT_OPTIONS = Object.freeze({ * @returns {boolean} `true` if a trailing comma is allowed. */ function isTrailingCommaAllowed(lastItem) { - return !( - lastItem.type === "RestElement" || - lastItem.type === "RestProperty" || - lastItem.type === "ExperimentalRestProperty" - ); + return !( + lastItem.type === "RestElement" || + lastItem.type === "RestProperty" || + lastItem.type === "ExperimentalRestProperty" + ); } /** @@ -45,380 +45,329 @@ function isTrailingCommaAllowed(lastItem) { * @returns {Object} The normalized option value. */ function normalizeOptions(optionValue, ecmaVersion) { - if (typeof optionValue === "string") { - return { - arrays: optionValue, - objects: optionValue, - imports: optionValue, - exports: optionValue, - functions: ecmaVersion < 2017 ? "ignore" : optionValue, - }; - } - if (typeof optionValue === "object" && optionValue !== null) { - return { - arrays: optionValue.arrays || DEFAULT_OPTIONS.arrays, - objects: optionValue.objects || DEFAULT_OPTIONS.objects, - imports: optionValue.imports || DEFAULT_OPTIONS.imports, - exports: optionValue.exports || DEFAULT_OPTIONS.exports, - functions: optionValue.functions || DEFAULT_OPTIONS.functions, - }; - } - - return DEFAULT_OPTIONS; + if (typeof optionValue === "string") { + return { + arrays: optionValue, + objects: optionValue, + imports: optionValue, + exports: optionValue, + functions: ecmaVersion < 2017 ? "ignore" : optionValue + }; + } + if (typeof optionValue === "object" && optionValue !== null) { + return { + arrays: optionValue.arrays || DEFAULT_OPTIONS.arrays, + objects: optionValue.objects || DEFAULT_OPTIONS.objects, + imports: optionValue.imports || DEFAULT_OPTIONS.imports, + exports: optionValue.exports || DEFAULT_OPTIONS.exports, + functions: optionValue.functions || DEFAULT_OPTIONS.functions + }; + } + + return DEFAULT_OPTIONS; } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "comma-dangle", - url: "https://eslint.style/rules/comma-dangle", - }, - }, - ], - }, - type: "layout", - - docs: { - description: "Require or disallow trailing commas", - recommended: false, - url: "https://eslint.org/docs/latest/rules/comma-dangle", - }, - - fixable: "code", - - schema: { - definitions: { - value: { - enum: [ - "always-multiline", - "always", - "never", - "only-multiline", - ], - }, - valueWithIgnore: { - enum: [ - "always-multiline", - "always", - "ignore", - "never", - "only-multiline", - ], - }, - }, - type: "array", - items: [ - { - oneOf: [ - { - $ref: "#/definitions/value", - }, - { - type: "object", - properties: { - arrays: { - $ref: "#/definitions/valueWithIgnore", - }, - objects: { - $ref: "#/definitions/valueWithIgnore", - }, - imports: { - $ref: "#/definitions/valueWithIgnore", - }, - exports: { - $ref: "#/definitions/valueWithIgnore", - }, - functions: { - $ref: "#/definitions/valueWithIgnore", - }, - }, - additionalProperties: false, - }, - ], - }, - ], - additionalItems: false, - }, - - messages: { - unexpected: "Unexpected trailing comma.", - missing: "Missing trailing comma.", - }, - }, - - create(context) { - const options = normalizeOptions( - context.options[0], - context.languageOptions.ecmaVersion, - ); - - const sourceCode = context.sourceCode; - - /** - * Gets the last item of the given node. - * @param {ASTNode} node The node to get. - * @returns {ASTNode|null} The last node or null. - */ - function getLastItem(node) { - /** - * Returns the last element of an array - * @param {any[]} array The input array - * @returns {any} The last element - */ - function last(array) { - return array.at(-1); - } - - switch (node.type) { - case "ObjectExpression": - case "ObjectPattern": - return last(node.properties); - case "ArrayExpression": - case "ArrayPattern": - return last(node.elements); - case "ImportDeclaration": - case "ExportNamedDeclaration": - return last(node.specifiers); - case "FunctionDeclaration": - case "FunctionExpression": - case "ArrowFunctionExpression": - return last(node.params); - case "CallExpression": - case "NewExpression": - return last(node.arguments); - default: - return null; - } - } - - /** - * Gets the trailing comma token of the given node. - * If the trailing comma does not exist, this returns the token which is - * the insertion point of the trailing comma token. - * @param {ASTNode} node The node to get. - * @param {ASTNode} lastItem The last item of the node. - * @returns {Token} The trailing comma token or the insertion point. - */ - function getTrailingToken(node, lastItem) { - switch (node.type) { - case "ObjectExpression": - case "ArrayExpression": - case "CallExpression": - case "NewExpression": - return sourceCode.getLastToken(node, 1); - default: { - const nextToken = sourceCode.getTokenAfter(lastItem); - - if (astUtils.isCommaToken(nextToken)) { - return nextToken; - } - return sourceCode.getLastToken(lastItem); - } - } - } - - /** - * Checks whether or not a given node is multiline. - * This rule handles a given node as multiline when the closing parenthesis - * and the last element are not on the same line. - * @param {ASTNode} node A node to check. - * @returns {boolean} `true` if the node is multiline. - */ - function isMultiline(node) { - const lastItem = getLastItem(node); - - if (!lastItem) { - return false; - } - - const penultimateToken = getTrailingToken(node, lastItem); - const lastToken = sourceCode.getTokenAfter(penultimateToken); - - return lastToken.loc.end.line !== penultimateToken.loc.end.line; - } - - /** - * Reports a trailing comma if it exists. - * @param {ASTNode} node A node to check. Its type is one of - * ObjectExpression, ObjectPattern, ArrayExpression, ArrayPattern, - * ImportDeclaration, and ExportNamedDeclaration. - * @returns {void} - */ - function forbidTrailingComma(node) { - const lastItem = getLastItem(node); - - if ( - !lastItem || - (node.type === "ImportDeclaration" && - lastItem.type !== "ImportSpecifier") - ) { - return; - } - - const trailingToken = getTrailingToken(node, lastItem); - - if (astUtils.isCommaToken(trailingToken)) { - context.report({ - node: lastItem, - loc: trailingToken.loc, - messageId: "unexpected", - *fix(fixer) { - yield fixer.remove(trailingToken); - - /* - * Extend the range of the fix to include surrounding tokens to ensure - * that the element after which the comma is removed stays _last_. - * This intentionally makes conflicts in fix ranges with rules that may be - * adding or removing elements in the same autofix pass. - * https://github.com/eslint/eslint/issues/15660 - */ - yield fixer.insertTextBefore( - sourceCode.getTokenBefore(trailingToken), - "", - ); - yield fixer.insertTextAfter( - sourceCode.getTokenAfter(trailingToken), - "", - ); - }, - }); - } - } - - /** - * Reports the last element of a given node if it does not have a trailing - * comma. - * - * If a given node is `ArrayPattern` which has `RestElement`, the trailing - * comma is disallowed, so report if it exists. - * @param {ASTNode} node A node to check. Its type is one of - * ObjectExpression, ObjectPattern, ArrayExpression, ArrayPattern, - * ImportDeclaration, and ExportNamedDeclaration. - * @returns {void} - */ - function forceTrailingComma(node) { - const lastItem = getLastItem(node); - - if ( - !lastItem || - (node.type === "ImportDeclaration" && - lastItem.type !== "ImportSpecifier") - ) { - return; - } - if (!isTrailingCommaAllowed(lastItem)) { - forbidTrailingComma(node); - return; - } - - const trailingToken = getTrailingToken(node, lastItem); - - if (trailingToken.value !== ",") { - context.report({ - node: lastItem, - loc: { - start: trailingToken.loc.end, - end: astUtils.getNextLocation( - sourceCode, - trailingToken.loc.end, - ), - }, - messageId: "missing", - *fix(fixer) { - yield fixer.insertTextAfter(trailingToken, ","); - - /* - * Extend the range of the fix to include surrounding tokens to ensure - * that the element after which the comma is inserted stays _last_. - * This intentionally makes conflicts in fix ranges with rules that may be - * adding or removing elements in the same autofix pass. - * https://github.com/eslint/eslint/issues/15660 - */ - yield fixer.insertTextBefore(trailingToken, ""); - yield fixer.insertTextAfter( - sourceCode.getTokenAfter(trailingToken), - "", - ); - }, - }); - } - } - - /** - * If a given node is multiline, reports the last element of a given node - * when it does not have a trailing comma. - * Otherwise, reports a trailing comma if it exists. - * @param {ASTNode} node A node to check. Its type is one of - * ObjectExpression, ObjectPattern, ArrayExpression, ArrayPattern, - * ImportDeclaration, and ExportNamedDeclaration. - * @returns {void} - */ - function forceTrailingCommaIfMultiline(node) { - if (isMultiline(node)) { - forceTrailingComma(node); - } else { - forbidTrailingComma(node); - } - } - - /** - * Only if a given node is not multiline, reports the last element of a given node - * when it does not have a trailing comma. - * Otherwise, reports a trailing comma if it exists. - * @param {ASTNode} node A node to check. Its type is one of - * ObjectExpression, ObjectPattern, ArrayExpression, ArrayPattern, - * ImportDeclaration, and ExportNamedDeclaration. - * @returns {void} - */ - function allowTrailingCommaIfMultiline(node) { - if (!isMultiline(node)) { - forbidTrailingComma(node); - } - } - - const predicate = { - always: forceTrailingComma, - "always-multiline": forceTrailingCommaIfMultiline, - "only-multiline": allowTrailingCommaIfMultiline, - never: forbidTrailingComma, - ignore() {}, - }; - - return { - ObjectExpression: predicate[options.objects], - ObjectPattern: predicate[options.objects], - - ArrayExpression: predicate[options.arrays], - ArrayPattern: predicate[options.arrays], - - ImportDeclaration: predicate[options.imports], - - ExportNamedDeclaration: predicate[options.exports], - - FunctionDeclaration: predicate[options.functions], - FunctionExpression: predicate[options.functions], - ArrowFunctionExpression: predicate[options.functions], - CallExpression: predicate[options.functions], - NewExpression: predicate[options.functions], - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Require or disallow trailing commas", + recommended: false, + url: "https://eslint.org/docs/latest/rules/comma-dangle" + }, + + fixable: "code", + + schema: { + definitions: { + value: { + enum: [ + "always-multiline", + "always", + "never", + "only-multiline" + ] + }, + valueWithIgnore: { + enum: [ + "always-multiline", + "always", + "ignore", + "never", + "only-multiline" + ] + } + }, + type: "array", + items: [ + { + oneOf: [ + { + $ref: "#/definitions/value" + }, + { + type: "object", + properties: { + arrays: { $ref: "#/definitions/valueWithIgnore" }, + objects: { $ref: "#/definitions/valueWithIgnore" }, + imports: { $ref: "#/definitions/valueWithIgnore" }, + exports: { $ref: "#/definitions/valueWithIgnore" }, + functions: { $ref: "#/definitions/valueWithIgnore" } + }, + additionalProperties: false + } + ] + } + ], + additionalItems: false + }, + + messages: { + unexpected: "Unexpected trailing comma.", + missing: "Missing trailing comma." + } + }, + + create(context) { + const options = normalizeOptions(context.options[0], context.languageOptions.ecmaVersion); + + const sourceCode = context.sourceCode; + + /** + * Gets the last item of the given node. + * @param {ASTNode} node The node to get. + * @returns {ASTNode|null} The last node or null. + */ + function getLastItem(node) { + + /** + * Returns the last element of an array + * @param {any[]} array The input array + * @returns {any} The last element + */ + function last(array) { + return array[array.length - 1]; + } + + switch (node.type) { + case "ObjectExpression": + case "ObjectPattern": + return last(node.properties); + case "ArrayExpression": + case "ArrayPattern": + return last(node.elements); + case "ImportDeclaration": + case "ExportNamedDeclaration": + return last(node.specifiers); + case "FunctionDeclaration": + case "FunctionExpression": + case "ArrowFunctionExpression": + return last(node.params); + case "CallExpression": + case "NewExpression": + return last(node.arguments); + default: + return null; + } + } + + /** + * Gets the trailing comma token of the given node. + * If the trailing comma does not exist, this returns the token which is + * the insertion point of the trailing comma token. + * @param {ASTNode} node The node to get. + * @param {ASTNode} lastItem The last item of the node. + * @returns {Token} The trailing comma token or the insertion point. + */ + function getTrailingToken(node, lastItem) { + switch (node.type) { + case "ObjectExpression": + case "ArrayExpression": + case "CallExpression": + case "NewExpression": + return sourceCode.getLastToken(node, 1); + default: { + const nextToken = sourceCode.getTokenAfter(lastItem); + + if (astUtils.isCommaToken(nextToken)) { + return nextToken; + } + return sourceCode.getLastToken(lastItem); + } + } + } + + /** + * Checks whether or not a given node is multiline. + * This rule handles a given node as multiline when the closing parenthesis + * and the last element are not on the same line. + * @param {ASTNode} node A node to check. + * @returns {boolean} `true` if the node is multiline. + */ + function isMultiline(node) { + const lastItem = getLastItem(node); + + if (!lastItem) { + return false; + } + + const penultimateToken = getTrailingToken(node, lastItem); + const lastToken = sourceCode.getTokenAfter(penultimateToken); + + return lastToken.loc.end.line !== penultimateToken.loc.end.line; + } + + /** + * Reports a trailing comma if it exists. + * @param {ASTNode} node A node to check. Its type is one of + * ObjectExpression, ObjectPattern, ArrayExpression, ArrayPattern, + * ImportDeclaration, and ExportNamedDeclaration. + * @returns {void} + */ + function forbidTrailingComma(node) { + const lastItem = getLastItem(node); + + if (!lastItem || (node.type === "ImportDeclaration" && lastItem.type !== "ImportSpecifier")) { + return; + } + + const trailingToken = getTrailingToken(node, lastItem); + + if (astUtils.isCommaToken(trailingToken)) { + context.report({ + node: lastItem, + loc: trailingToken.loc, + messageId: "unexpected", + *fix(fixer) { + yield fixer.remove(trailingToken); + + /* + * Extend the range of the fix to include surrounding tokens to ensure + * that the element after which the comma is removed stays _last_. + * This intentionally makes conflicts in fix ranges with rules that may be + * adding or removing elements in the same autofix pass. + * https://github.com/eslint/eslint/issues/15660 + */ + yield fixer.insertTextBefore(sourceCode.getTokenBefore(trailingToken), ""); + yield fixer.insertTextAfter(sourceCode.getTokenAfter(trailingToken), ""); + } + }); + } + } + + /** + * Reports the last element of a given node if it does not have a trailing + * comma. + * + * If a given node is `ArrayPattern` which has `RestElement`, the trailing + * comma is disallowed, so report if it exists. + * @param {ASTNode} node A node to check. Its type is one of + * ObjectExpression, ObjectPattern, ArrayExpression, ArrayPattern, + * ImportDeclaration, and ExportNamedDeclaration. + * @returns {void} + */ + function forceTrailingComma(node) { + const lastItem = getLastItem(node); + + if (!lastItem || (node.type === "ImportDeclaration" && lastItem.type !== "ImportSpecifier")) { + return; + } + if (!isTrailingCommaAllowed(lastItem)) { + forbidTrailingComma(node); + return; + } + + const trailingToken = getTrailingToken(node, lastItem); + + if (trailingToken.value !== ",") { + context.report({ + node: lastItem, + loc: { + start: trailingToken.loc.end, + end: astUtils.getNextLocation(sourceCode, trailingToken.loc.end) + }, + messageId: "missing", + *fix(fixer) { + yield fixer.insertTextAfter(trailingToken, ","); + + /* + * Extend the range of the fix to include surrounding tokens to ensure + * that the element after which the comma is inserted stays _last_. + * This intentionally makes conflicts in fix ranges with rules that may be + * adding or removing elements in the same autofix pass. + * https://github.com/eslint/eslint/issues/15660 + */ + yield fixer.insertTextBefore(trailingToken, ""); + yield fixer.insertTextAfter(sourceCode.getTokenAfter(trailingToken), ""); + } + }); + } + } + + /** + * If a given node is multiline, reports the last element of a given node + * when it does not have a trailing comma. + * Otherwise, reports a trailing comma if it exists. + * @param {ASTNode} node A node to check. Its type is one of + * ObjectExpression, ObjectPattern, ArrayExpression, ArrayPattern, + * ImportDeclaration, and ExportNamedDeclaration. + * @returns {void} + */ + function forceTrailingCommaIfMultiline(node) { + if (isMultiline(node)) { + forceTrailingComma(node); + } else { + forbidTrailingComma(node); + } + } + + /** + * Only if a given node is not multiline, reports the last element of a given node + * when it does not have a trailing comma. + * Otherwise, reports a trailing comma if it exists. + * @param {ASTNode} node A node to check. Its type is one of + * ObjectExpression, ObjectPattern, ArrayExpression, ArrayPattern, + * ImportDeclaration, and ExportNamedDeclaration. + * @returns {void} + */ + function allowTrailingCommaIfMultiline(node) { + if (!isMultiline(node)) { + forbidTrailingComma(node); + } + } + + const predicate = { + always: forceTrailingComma, + "always-multiline": forceTrailingCommaIfMultiline, + "only-multiline": allowTrailingCommaIfMultiline, + never: forbidTrailingComma, + ignore() {} + }; + + return { + ObjectExpression: predicate[options.objects], + ObjectPattern: predicate[options.objects], + + ArrayExpression: predicate[options.arrays], + ArrayPattern: predicate[options.arrays], + + ImportDeclaration: predicate[options.imports], + + ExportNamedDeclaration: predicate[options.exports], + + FunctionDeclaration: predicate[options.functions], + FunctionExpression: predicate[options.functions], + ArrowFunctionExpression: predicate[options.functions], + CallExpression: predicate[options.functions], + NewExpression: predicate[options.functions] + }; + } }; diff --git a/node_modules/eslint/lib/rules/comma-spacing.js b/node_modules/eslint/lib/rules/comma-spacing.js index 521651912..e266de4a9 100644 --- a/node_modules/eslint/lib/rules/comma-spacing.js +++ b/node_modules/eslint/lib/rules/comma-spacing.js @@ -11,198 +11,182 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "comma-spacing", - url: "https://eslint.style/rules/comma-spacing", - }, - }, - ], - }, - type: "layout", - - docs: { - description: "Enforce consistent spacing before and after commas", - recommended: false, - url: "https://eslint.org/docs/latest/rules/comma-spacing", - }, - - fixable: "whitespace", - - schema: [ - { - type: "object", - properties: { - before: { - type: "boolean", - default: false, - }, - after: { - type: "boolean", - default: true, - }, - }, - additionalProperties: false, - }, - ], - - messages: { - missing: "A space is required {{loc}} ','.", - unexpected: "There should be no space {{loc}} ','.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - const tokensAndComments = sourceCode.tokensAndComments; - - const options = { - before: context.options[0] ? context.options[0].before : false, - after: context.options[0] ? context.options[0].after : true, - }; - - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - // list of comma tokens to ignore for the check of leading whitespace - const commaTokensToIgnore = []; - - /** - * Reports a spacing error with an appropriate message. - * @param {ASTNode} node The binary expression node to report. - * @param {string} loc Is the error "before" or "after" the comma? - * @param {ASTNode} otherNode The node at the left or right of `node` - * @returns {void} - * @private - */ - function report(node, loc, otherNode) { - context.report({ - node, - fix(fixer) { - if (options[loc]) { - if (loc === "before") { - return fixer.insertTextBefore(node, " "); - } - return fixer.insertTextAfter(node, " "); - } - let start, end; - const newText = ""; - - if (loc === "before") { - start = otherNode.range[1]; - end = node.range[0]; - } else { - start = node.range[1]; - end = otherNode.range[0]; - } - - return fixer.replaceTextRange([start, end], newText); - }, - messageId: options[loc] ? "missing" : "unexpected", - data: { - loc, - }, - }); - } - - /** - * Adds null elements of the given ArrayExpression or ArrayPattern node to the ignore list. - * @param {ASTNode} node An ArrayExpression or ArrayPattern node. - * @returns {void} - */ - function addNullElementsToIgnoreList(node) { - let previousToken = sourceCode.getFirstToken(node); - - node.elements.forEach(element => { - let token; - - if (element === null) { - token = sourceCode.getTokenAfter(previousToken); - - if (astUtils.isCommaToken(token)) { - commaTokensToIgnore.push(token); - } - } else { - token = sourceCode.getTokenAfter(element); - } - - previousToken = token; - }); - } - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - return { - "Program:exit"() { - tokensAndComments.forEach((token, i) => { - if (!astUtils.isCommaToken(token)) { - return; - } - - const previousToken = tokensAndComments[i - 1]; - const nextToken = tokensAndComments[i + 1]; - - if ( - previousToken && - !astUtils.isCommaToken(previousToken) && // ignore spacing between two commas - /* - * `commaTokensToIgnore` are ending commas of `null` elements (array holes/elisions). - * In addition to spacing between two commas, this can also ignore: - * - * - Spacing after `[` (controlled by array-bracket-spacing) - * Example: [ , ] - * ^ - * - Spacing after a comment (for backwards compatibility, this was possibly unintentional) - * Example: [a, /* * / ,] - * ^ - */ - !commaTokensToIgnore.includes(token) && - astUtils.isTokenOnSameLine(previousToken, token) && - options.before !== - sourceCode.isSpaceBetweenTokens( - previousToken, - token, - ) - ) { - report(token, "before", previousToken); - } - - if ( - nextToken && - !astUtils.isCommaToken(nextToken) && // ignore spacing between two commas - !astUtils.isClosingParenToken(nextToken) && // controlled by space-in-parens - !astUtils.isClosingBracketToken(nextToken) && // controlled by array-bracket-spacing - !astUtils.isClosingBraceToken(nextToken) && // controlled by object-curly-spacing - !(!options.after && nextToken.type === "Line") && // special case, allow space before line comment - astUtils.isTokenOnSameLine(token, nextToken) && - options.after !== - sourceCode.isSpaceBetweenTokens(token, nextToken) - ) { - report(token, "after", nextToken); - } - }); - }, - ArrayExpression: addNullElementsToIgnoreList, - ArrayPattern: addNullElementsToIgnoreList, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Enforce consistent spacing before and after commas", + recommended: false, + url: "https://eslint.org/docs/latest/rules/comma-spacing" + }, + + fixable: "whitespace", + + schema: [ + { + type: "object", + properties: { + before: { + type: "boolean", + default: false + }, + after: { + type: "boolean", + default: true + } + }, + additionalProperties: false + } + ], + + messages: { + missing: "A space is required {{loc}} ','.", + unexpected: "There should be no space {{loc}} ','." + } + }, + + create(context) { + + const sourceCode = context.sourceCode; + const tokensAndComments = sourceCode.tokensAndComments; + + const options = { + before: context.options[0] ? context.options[0].before : false, + after: context.options[0] ? context.options[0].after : true + }; + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + // list of comma tokens to ignore for the check of leading whitespace + const commaTokensToIgnore = []; + + /** + * Reports a spacing error with an appropriate message. + * @param {ASTNode} node The binary expression node to report. + * @param {string} loc Is the error "before" or "after" the comma? + * @param {ASTNode} otherNode The node at the left or right of `node` + * @returns {void} + * @private + */ + function report(node, loc, otherNode) { + context.report({ + node, + fix(fixer) { + if (options[loc]) { + if (loc === "before") { + return fixer.insertTextBefore(node, " "); + } + return fixer.insertTextAfter(node, " "); + + } + let start, end; + const newText = ""; + + if (loc === "before") { + start = otherNode.range[1]; + end = node.range[0]; + } else { + start = node.range[1]; + end = otherNode.range[0]; + } + + return fixer.replaceTextRange([start, end], newText); + + }, + messageId: options[loc] ? "missing" : "unexpected", + data: { + loc + } + }); + } + + /** + * Adds null elements of the given ArrayExpression or ArrayPattern node to the ignore list. + * @param {ASTNode} node An ArrayExpression or ArrayPattern node. + * @returns {void} + */ + function addNullElementsToIgnoreList(node) { + let previousToken = sourceCode.getFirstToken(node); + + node.elements.forEach(element => { + let token; + + if (element === null) { + token = sourceCode.getTokenAfter(previousToken); + + if (astUtils.isCommaToken(token)) { + commaTokensToIgnore.push(token); + } + } else { + token = sourceCode.getTokenAfter(element); + } + + previousToken = token; + }); + } + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + return { + "Program:exit"() { + tokensAndComments.forEach((token, i) => { + + if (!astUtils.isCommaToken(token)) { + return; + } + + const previousToken = tokensAndComments[i - 1]; + const nextToken = tokensAndComments[i + 1]; + + if ( + previousToken && + !astUtils.isCommaToken(previousToken) && // ignore spacing between two commas + + /* + * `commaTokensToIgnore` are ending commas of `null` elements (array holes/elisions). + * In addition to spacing between two commas, this can also ignore: + * + * - Spacing after `[` (controlled by array-bracket-spacing) + * Example: [ , ] + * ^ + * - Spacing after a comment (for backwards compatibility, this was possibly unintentional) + * Example: [a, /* * / ,] + * ^ + */ + !commaTokensToIgnore.includes(token) && + + astUtils.isTokenOnSameLine(previousToken, token) && + options.before !== sourceCode.isSpaceBetweenTokens(previousToken, token) + ) { + report(token, "before", previousToken); + } + + if ( + nextToken && + !astUtils.isCommaToken(nextToken) && // ignore spacing between two commas + !astUtils.isClosingParenToken(nextToken) && // controlled by space-in-parens + !astUtils.isClosingBracketToken(nextToken) && // controlled by array-bracket-spacing + !astUtils.isClosingBraceToken(nextToken) && // controlled by object-curly-spacing + !(!options.after && nextToken.type === "Line") && // special case, allow space before line comment + astUtils.isTokenOnSameLine(token, nextToken) && + options.after !== sourceCode.isSpaceBetweenTokens(token, nextToken) + ) { + report(token, "after", nextToken); + } + }); + }, + ArrayExpression: addNullElementsToIgnoreList, + ArrayPattern: addNullElementsToIgnoreList + + }; + + } }; diff --git a/node_modules/eslint/lib/rules/comma-style.js b/node_modules/eslint/lib/rules/comma-style.js index 6c8e76569..0b5121953 100644 --- a/node_modules/eslint/lib/rules/comma-style.js +++ b/node_modules/eslint/lib/rules/comma-style.js @@ -12,380 +12,303 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "comma-style", - url: "https://eslint.style/rules/comma-style", - }, - }, - ], - }, - type: "layout", - - docs: { - description: "Enforce consistent comma style", - recommended: false, - url: "https://eslint.org/docs/latest/rules/comma-style", - }, - - fixable: "code", - - schema: [ - { - enum: ["first", "last"], - }, - { - type: "object", - properties: { - exceptions: { - type: "object", - additionalProperties: { - type: "boolean", - }, - }, - }, - additionalProperties: false, - }, - ], - - messages: { - unexpectedLineBeforeAndAfterComma: - "Bad line breaking before and after ','.", - expectedCommaFirst: "',' should be placed first.", - expectedCommaLast: "',' should be placed last.", - }, - }, - - create(context) { - const style = context.options[0] || "last", - sourceCode = context.sourceCode; - const exceptions = { - ArrayPattern: true, - ArrowFunctionExpression: true, - CallExpression: true, - FunctionDeclaration: true, - FunctionExpression: true, - ImportDeclaration: true, - ObjectPattern: true, - NewExpression: true, - }; - - if ( - context.options.length === 2 && - Object.hasOwn(context.options[1], "exceptions") - ) { - const keys = Object.keys(context.options[1].exceptions); - - for (let i = 0; i < keys.length; i++) { - exceptions[keys[i]] = context.options[1].exceptions[keys[i]]; - } - } - - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - /** - * Modified text based on the style - * @param {string} styleType Style type - * @param {string} text Source code text - * @returns {string} modified text - * @private - */ - function getReplacedText(styleType, text) { - switch (styleType) { - case "between": - return `,${text.replace(astUtils.LINEBREAK_MATCHER, "")}`; - - case "first": - return `${text},`; - - case "last": - return `,${text}`; - - default: - return ""; - } - } - - /** - * Determines the fixer function for a given style. - * @param {string} styleType comma style - * @param {ASTNode} previousItemToken The token to check. - * @param {ASTNode} commaToken The token to check. - * @param {ASTNode} currentItemToken The token to check. - * @returns {Function} Fixer function - * @private - */ - function getFixerFunction( - styleType, - previousItemToken, - commaToken, - currentItemToken, - ) { - const text = - sourceCode.text.slice( - previousItemToken.range[1], - commaToken.range[0], - ) + - sourceCode.text.slice( - commaToken.range[1], - currentItemToken.range[0], - ); - const range = [ - previousItemToken.range[1], - currentItemToken.range[0], - ]; - - return function (fixer) { - return fixer.replaceTextRange( - range, - getReplacedText(styleType, text), - ); - }; - } - - /** - * Validates the spacing around single items in lists. - * @param {Token} previousItemToken The last token from the previous item. - * @param {Token} commaToken The token representing the comma. - * @param {Token} currentItemToken The first token of the current item. - * @param {Token} reportItem The item to use when reporting an error. - * @returns {void} - * @private - */ - function validateCommaItemSpacing( - previousItemToken, - commaToken, - currentItemToken, - reportItem, - ) { - // if single line - if ( - astUtils.isTokenOnSameLine(commaToken, currentItemToken) && - astUtils.isTokenOnSameLine(previousItemToken, commaToken) - ) { - // do nothing. - } else if ( - !astUtils.isTokenOnSameLine(commaToken, currentItemToken) && - !astUtils.isTokenOnSameLine(previousItemToken, commaToken) - ) { - const comment = sourceCode.getCommentsAfter(commaToken)[0]; - const styleType = - comment && - comment.type === "Block" && - astUtils.isTokenOnSameLine(commaToken, comment) - ? style - : "between"; - - // lone comma - context.report({ - node: reportItem, - loc: commaToken.loc, - messageId: "unexpectedLineBeforeAndAfterComma", - fix: getFixerFunction( - styleType, - previousItemToken, - commaToken, - currentItemToken, - ), - }); - } else if ( - style === "first" && - !astUtils.isTokenOnSameLine(commaToken, currentItemToken) - ) { - context.report({ - node: reportItem, - loc: commaToken.loc, - messageId: "expectedCommaFirst", - fix: getFixerFunction( - style, - previousItemToken, - commaToken, - currentItemToken, - ), - }); - } else if ( - style === "last" && - astUtils.isTokenOnSameLine(commaToken, currentItemToken) - ) { - context.report({ - node: reportItem, - loc: commaToken.loc, - messageId: "expectedCommaLast", - fix: getFixerFunction( - style, - previousItemToken, - commaToken, - currentItemToken, - ), - }); - } - } - - /** - * Checks the comma placement with regards to a declaration/property/element - * @param {ASTNode} node The binary expression node to check - * @param {string} property The property of the node containing child nodes. - * @private - * @returns {void} - */ - function validateComma(node, property) { - const items = node[property], - arrayLiteral = - node.type === "ArrayExpression" || - node.type === "ArrayPattern"; - - if (items.length > 1 || arrayLiteral) { - // seed as opening [ - let previousItemToken = sourceCode.getFirstToken(node); - - items.forEach(item => { - const commaToken = item - ? sourceCode.getTokenBefore(item) - : previousItemToken, - currentItemToken = item - ? sourceCode.getFirstToken(item) - : sourceCode.getTokenAfter(commaToken), - reportItem = item || currentItemToken; - - /* - * This works by comparing three token locations: - * - previousItemToken is the last token of the previous item - * - commaToken is the location of the comma before the current item - * - currentItemToken is the first token of the current item - * - * These values get switched around if item is undefined. - * previousItemToken will refer to the last token not belonging - * to the current item, which could be a comma or an opening - * square bracket. currentItemToken could be a comma. - * - * All comparisons are done based on these tokens directly, so - * they are always valid regardless of an undefined item. - */ - if (astUtils.isCommaToken(commaToken)) { - validateCommaItemSpacing( - previousItemToken, - commaToken, - currentItemToken, - reportItem, - ); - } - - if (item) { - const tokenAfterItem = sourceCode.getTokenAfter( - item, - astUtils.isNotClosingParenToken, - ); - - previousItemToken = tokenAfterItem - ? sourceCode.getTokenBefore(tokenAfterItem) - : sourceCode.ast.tokens.at(-1); - } else { - previousItemToken = currentItemToken; - } - }); - - /* - * Special case for array literals that have empty last items, such - * as [ 1, 2, ]. These arrays only have two items show up in the - * AST, so we need to look at the token to verify that there's no - * dangling comma. - */ - if (arrayLiteral) { - const lastToken = sourceCode.getLastToken(node), - nextToLastToken = sourceCode.getTokenBefore(lastToken); - - if (astUtils.isCommaToken(nextToLastToken)) { - validateCommaItemSpacing( - sourceCode.getTokenBefore(nextToLastToken), - nextToLastToken, - lastToken, - lastToken, - ); - } - } - } - } - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - const nodes = {}; - - if (!exceptions.VariableDeclaration) { - nodes.VariableDeclaration = function (node) { - validateComma(node, "declarations"); - }; - } - if (!exceptions.ObjectExpression) { - nodes.ObjectExpression = function (node) { - validateComma(node, "properties"); - }; - } - if (!exceptions.ObjectPattern) { - nodes.ObjectPattern = function (node) { - validateComma(node, "properties"); - }; - } - if (!exceptions.ArrayExpression) { - nodes.ArrayExpression = function (node) { - validateComma(node, "elements"); - }; - } - if (!exceptions.ArrayPattern) { - nodes.ArrayPattern = function (node) { - validateComma(node, "elements"); - }; - } - if (!exceptions.FunctionDeclaration) { - nodes.FunctionDeclaration = function (node) { - validateComma(node, "params"); - }; - } - if (!exceptions.FunctionExpression) { - nodes.FunctionExpression = function (node) { - validateComma(node, "params"); - }; - } - if (!exceptions.ArrowFunctionExpression) { - nodes.ArrowFunctionExpression = function (node) { - validateComma(node, "params"); - }; - } - if (!exceptions.CallExpression) { - nodes.CallExpression = function (node) { - validateComma(node, "arguments"); - }; - } - if (!exceptions.ImportDeclaration) { - nodes.ImportDeclaration = function (node) { - validateComma(node, "specifiers"); - }; - } - if (!exceptions.NewExpression) { - nodes.NewExpression = function (node) { - validateComma(node, "arguments"); - }; - } - - return nodes; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Enforce consistent comma style", + recommended: false, + url: "https://eslint.org/docs/latest/rules/comma-style" + }, + + fixable: "code", + + schema: [ + { + enum: ["first", "last"] + }, + { + type: "object", + properties: { + exceptions: { + type: "object", + additionalProperties: { + type: "boolean" + } + } + }, + additionalProperties: false + } + ], + + messages: { + unexpectedLineBeforeAndAfterComma: "Bad line breaking before and after ','.", + expectedCommaFirst: "',' should be placed first.", + expectedCommaLast: "',' should be placed last." + } + }, + + create(context) { + const style = context.options[0] || "last", + sourceCode = context.sourceCode; + const exceptions = { + ArrayPattern: true, + ArrowFunctionExpression: true, + CallExpression: true, + FunctionDeclaration: true, + FunctionExpression: true, + ImportDeclaration: true, + ObjectPattern: true, + NewExpression: true + }; + + if (context.options.length === 2 && Object.prototype.hasOwnProperty.call(context.options[1], "exceptions")) { + const keys = Object.keys(context.options[1].exceptions); + + for (let i = 0; i < keys.length; i++) { + exceptions[keys[i]] = context.options[1].exceptions[keys[i]]; + } + } + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + /** + * Modified text based on the style + * @param {string} styleType Style type + * @param {string} text Source code text + * @returns {string} modified text + * @private + */ + function getReplacedText(styleType, text) { + switch (styleType) { + case "between": + return `,${text.replace(astUtils.LINEBREAK_MATCHER, "")}`; + + case "first": + return `${text},`; + + case "last": + return `,${text}`; + + default: + return ""; + } + } + + /** + * Determines the fixer function for a given style. + * @param {string} styleType comma style + * @param {ASTNode} previousItemToken The token to check. + * @param {ASTNode} commaToken The token to check. + * @param {ASTNode} currentItemToken The token to check. + * @returns {Function} Fixer function + * @private + */ + function getFixerFunction(styleType, previousItemToken, commaToken, currentItemToken) { + const text = + sourceCode.text.slice(previousItemToken.range[1], commaToken.range[0]) + + sourceCode.text.slice(commaToken.range[1], currentItemToken.range[0]); + const range = [previousItemToken.range[1], currentItemToken.range[0]]; + + return function(fixer) { + return fixer.replaceTextRange(range, getReplacedText(styleType, text)); + }; + } + + /** + * Validates the spacing around single items in lists. + * @param {Token} previousItemToken The last token from the previous item. + * @param {Token} commaToken The token representing the comma. + * @param {Token} currentItemToken The first token of the current item. + * @param {Token} reportItem The item to use when reporting an error. + * @returns {void} + * @private + */ + function validateCommaItemSpacing(previousItemToken, commaToken, currentItemToken, reportItem) { + + // if single line + if (astUtils.isTokenOnSameLine(commaToken, currentItemToken) && + astUtils.isTokenOnSameLine(previousItemToken, commaToken)) { + + // do nothing. + + } else if (!astUtils.isTokenOnSameLine(commaToken, currentItemToken) && + !astUtils.isTokenOnSameLine(previousItemToken, commaToken)) { + + const comment = sourceCode.getCommentsAfter(commaToken)[0]; + const styleType = comment && comment.type === "Block" && astUtils.isTokenOnSameLine(commaToken, comment) + ? style + : "between"; + + // lone comma + context.report({ + node: reportItem, + loc: commaToken.loc, + messageId: "unexpectedLineBeforeAndAfterComma", + fix: getFixerFunction(styleType, previousItemToken, commaToken, currentItemToken) + }); + + } else if (style === "first" && !astUtils.isTokenOnSameLine(commaToken, currentItemToken)) { + + context.report({ + node: reportItem, + loc: commaToken.loc, + messageId: "expectedCommaFirst", + fix: getFixerFunction(style, previousItemToken, commaToken, currentItemToken) + }); + + } else if (style === "last" && astUtils.isTokenOnSameLine(commaToken, currentItemToken)) { + + context.report({ + node: reportItem, + loc: commaToken.loc, + messageId: "expectedCommaLast", + fix: getFixerFunction(style, previousItemToken, commaToken, currentItemToken) + }); + } + } + + /** + * Checks the comma placement with regards to a declaration/property/element + * @param {ASTNode} node The binary expression node to check + * @param {string} property The property of the node containing child nodes. + * @private + * @returns {void} + */ + function validateComma(node, property) { + const items = node[property], + arrayLiteral = (node.type === "ArrayExpression" || node.type === "ArrayPattern"); + + if (items.length > 1 || arrayLiteral) { + + // seed as opening [ + let previousItemToken = sourceCode.getFirstToken(node); + + items.forEach(item => { + const commaToken = item ? sourceCode.getTokenBefore(item) : previousItemToken, + currentItemToken = item ? sourceCode.getFirstToken(item) : sourceCode.getTokenAfter(commaToken), + reportItem = item || currentItemToken; + + /* + * This works by comparing three token locations: + * - previousItemToken is the last token of the previous item + * - commaToken is the location of the comma before the current item + * - currentItemToken is the first token of the current item + * + * These values get switched around if item is undefined. + * previousItemToken will refer to the last token not belonging + * to the current item, which could be a comma or an opening + * square bracket. currentItemToken could be a comma. + * + * All comparisons are done based on these tokens directly, so + * they are always valid regardless of an undefined item. + */ + if (astUtils.isCommaToken(commaToken)) { + validateCommaItemSpacing(previousItemToken, commaToken, currentItemToken, reportItem); + } + + if (item) { + const tokenAfterItem = sourceCode.getTokenAfter(item, astUtils.isNotClosingParenToken); + + previousItemToken = tokenAfterItem + ? sourceCode.getTokenBefore(tokenAfterItem) + : sourceCode.ast.tokens[sourceCode.ast.tokens.length - 1]; + } else { + previousItemToken = currentItemToken; + } + }); + + /* + * Special case for array literals that have empty last items, such + * as [ 1, 2, ]. These arrays only have two items show up in the + * AST, so we need to look at the token to verify that there's no + * dangling comma. + */ + if (arrayLiteral) { + + const lastToken = sourceCode.getLastToken(node), + nextToLastToken = sourceCode.getTokenBefore(lastToken); + + if (astUtils.isCommaToken(nextToLastToken)) { + validateCommaItemSpacing( + sourceCode.getTokenBefore(nextToLastToken), + nextToLastToken, + lastToken, + lastToken + ); + } + } + } + } + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + const nodes = {}; + + if (!exceptions.VariableDeclaration) { + nodes.VariableDeclaration = function(node) { + validateComma(node, "declarations"); + }; + } + if (!exceptions.ObjectExpression) { + nodes.ObjectExpression = function(node) { + validateComma(node, "properties"); + }; + } + if (!exceptions.ObjectPattern) { + nodes.ObjectPattern = function(node) { + validateComma(node, "properties"); + }; + } + if (!exceptions.ArrayExpression) { + nodes.ArrayExpression = function(node) { + validateComma(node, "elements"); + }; + } + if (!exceptions.ArrayPattern) { + nodes.ArrayPattern = function(node) { + validateComma(node, "elements"); + }; + } + if (!exceptions.FunctionDeclaration) { + nodes.FunctionDeclaration = function(node) { + validateComma(node, "params"); + }; + } + if (!exceptions.FunctionExpression) { + nodes.FunctionExpression = function(node) { + validateComma(node, "params"); + }; + } + if (!exceptions.ArrowFunctionExpression) { + nodes.ArrowFunctionExpression = function(node) { + validateComma(node, "params"); + }; + } + if (!exceptions.CallExpression) { + nodes.CallExpression = function(node) { + validateComma(node, "arguments"); + }; + } + if (!exceptions.ImportDeclaration) { + nodes.ImportDeclaration = function(node) { + validateComma(node, "specifiers"); + }; + } + if (!exceptions.NewExpression) { + nodes.NewExpression = function(node) { + validateComma(node, "arguments"); + }; + } + + return nodes; + } }; diff --git a/node_modules/eslint/lib/rules/complexity.js b/node_modules/eslint/lib/rules/complexity.js index 23a018b97..b7925074d 100644 --- a/node_modules/eslint/lib/rules/complexity.js +++ b/node_modules/eslint/lib/rules/complexity.js @@ -17,185 +17,149 @@ const { upperCaseFirst } = require("../shared/string-utils"); // Rule Definition //------------------------------------------------------------------------------ -const THRESHOLD_DEFAULT = 20; - -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - defaultOptions: [THRESHOLD_DEFAULT], - - docs: { - description: - "Enforce a maximum cyclomatic complexity allowed in a program", - recommended: false, - url: "https://eslint.org/docs/latest/rules/complexity", - }, - - schema: [ - { - oneOf: [ - { - type: "integer", - minimum: 0, - }, - { - type: "object", - properties: { - maximum: { - type: "integer", - minimum: 0, - }, - max: { - type: "integer", - minimum: 0, - }, - variant: { - enum: ["classic", "modified"], - }, - }, - additionalProperties: false, - }, - ], - }, - ], - - messages: { - complex: - "{{name}} has a complexity of {{complexity}}. Maximum allowed is {{max}}.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - const option = context.options[0]; - let threshold = THRESHOLD_DEFAULT; - let VARIANT = "classic"; - - if (typeof option === "object") { - if ( - Object.hasOwn(option, "maximum") || - Object.hasOwn(option, "max") - ) { - threshold = option.maximum || option.max; - } - - if (Object.hasOwn(option, "variant")) { - VARIANT = option.variant; - } - } else if (typeof option === "number") { - threshold = option; - } - - const IS_MODIFIED_COMPLEXITY = VARIANT === "modified"; - - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - // Using a stack to store complexity per code path - const complexities = []; - - /** - * Increase the complexity of the code path in context - * @returns {void} - * @private - */ - function increaseComplexity() { - complexities[complexities.length - 1]++; - } - - //-------------------------------------------------------------------------- - // Public API - //-------------------------------------------------------------------------- - - return { - onCodePathStart() { - // The initial complexity is 1, representing one execution path in the CodePath - complexities.push(1); - }, - - // Each branching in the code adds 1 to the complexity - CatchClause: increaseComplexity, - ConditionalExpression: increaseComplexity, - LogicalExpression: increaseComplexity, - ForStatement: increaseComplexity, - ForInStatement: increaseComplexity, - ForOfStatement: increaseComplexity, - IfStatement: increaseComplexity, - WhileStatement: increaseComplexity, - DoWhileStatement: increaseComplexity, - AssignmentPattern: increaseComplexity, - - // Avoid `default` - "SwitchCase[test]": () => - IS_MODIFIED_COMPLEXITY || increaseComplexity(), - SwitchStatement: () => - IS_MODIFIED_COMPLEXITY && increaseComplexity(), - - // Logical assignment operators have short-circuiting behavior - AssignmentExpression(node) { - if (astUtils.isLogicalAssignmentOperator(node.operator)) { - increaseComplexity(); - } - }, - - MemberExpression(node) { - if (node.optional === true) { - increaseComplexity(); - } - }, - - CallExpression(node) { - if (node.optional === true) { - increaseComplexity(); - } - }, - - onCodePathEnd(codePath, node) { - const complexity = complexities.pop(); - - /* - * This rule only evaluates complexity of functions, so "program" is excluded. - * Class field initializers and class static blocks are implicit functions. Therefore, - * they shouldn't contribute to the enclosing function's complexity, but their - * own complexity should be evaluated. - */ - if ( - codePath.origin !== "function" && - codePath.origin !== "class-field-initializer" && - codePath.origin !== "class-static-block" - ) { - return; - } - - if (complexity > threshold) { - let name; - let loc = node.loc; - - if (codePath.origin === "class-field-initializer") { - name = "class field initializer"; - } else if (codePath.origin === "class-static-block") { - name = "class static block"; - loc = sourceCode.getFirstToken(node).loc; - } else { - name = astUtils.getFunctionNameWithKind(node); - loc = astUtils.getFunctionHeadLoc(node, sourceCode); - } - - context.report({ - node, - loc, - messageId: "complex", - data: { - name: upperCaseFirst(name), - complexity, - max: threshold, - }, - }); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Enforce a maximum cyclomatic complexity allowed in a program", + recommended: false, + url: "https://eslint.org/docs/latest/rules/complexity" + }, + + schema: [ + { + oneOf: [ + { + type: "integer", + minimum: 0 + }, + { + type: "object", + properties: { + maximum: { + type: "integer", + minimum: 0 + }, + max: { + type: "integer", + minimum: 0 + } + }, + additionalProperties: false + } + ] + } + ], + + messages: { + complex: "{{name}} has a complexity of {{complexity}}. Maximum allowed is {{max}}." + } + }, + + create(context) { + const option = context.options[0]; + let THRESHOLD = 20; + + if ( + typeof option === "object" && + (Object.prototype.hasOwnProperty.call(option, "maximum") || Object.prototype.hasOwnProperty.call(option, "max")) + ) { + THRESHOLD = option.maximum || option.max; + } else if (typeof option === "number") { + THRESHOLD = option; + } + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + // Using a stack to store complexity per code path + const complexities = []; + + /** + * Increase the complexity of the code path in context + * @returns {void} + * @private + */ + function increaseComplexity() { + complexities[complexities.length - 1]++; + } + + //-------------------------------------------------------------------------- + // Public API + //-------------------------------------------------------------------------- + + return { + + onCodePathStart() { + + // The initial complexity is 1, representing one execution path in the CodePath + complexities.push(1); + }, + + // Each branching in the code adds 1 to the complexity + CatchClause: increaseComplexity, + ConditionalExpression: increaseComplexity, + LogicalExpression: increaseComplexity, + ForStatement: increaseComplexity, + ForInStatement: increaseComplexity, + ForOfStatement: increaseComplexity, + IfStatement: increaseComplexity, + WhileStatement: increaseComplexity, + DoWhileStatement: increaseComplexity, + + // Avoid `default` + "SwitchCase[test]": increaseComplexity, + + // Logical assignment operators have short-circuiting behavior + AssignmentExpression(node) { + if (astUtils.isLogicalAssignmentOperator(node.operator)) { + increaseComplexity(); + } + }, + + onCodePathEnd(codePath, node) { + const complexity = complexities.pop(); + + /* + * This rule only evaluates complexity of functions, so "program" is excluded. + * Class field initializers and class static blocks are implicit functions. Therefore, + * they shouldn't contribute to the enclosing function's complexity, but their + * own complexity should be evaluated. + */ + if ( + codePath.origin !== "function" && + codePath.origin !== "class-field-initializer" && + codePath.origin !== "class-static-block" + ) { + return; + } + + if (complexity > THRESHOLD) { + let name; + + if (codePath.origin === "class-field-initializer") { + name = "class field initializer"; + } else if (codePath.origin === "class-static-block") { + name = "class static block"; + } else { + name = astUtils.getFunctionNameWithKind(node); + } + + context.report({ + node, + messageId: "complex", + data: { + name: upperCaseFirst(name), + complexity, + max: THRESHOLD + } + }); + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/computed-property-spacing.js b/node_modules/eslint/lib/rules/computed-property-spacing.js index 871262646..2852877fd 100644 --- a/node_modules/eslint/lib/rules/computed-property-spacing.js +++ b/node_modules/eslint/lib/rules/computed-property-spacing.js @@ -11,241 +11,198 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "computed-property-spacing", - url: "https://eslint.style/rules/computed-property-spacing", - }, - }, - ], - }, - type: "layout", - - docs: { - description: - "Enforce consistent spacing inside computed property brackets", - recommended: false, - url: "https://eslint.org/docs/latest/rules/computed-property-spacing", - }, - - fixable: "whitespace", - - schema: [ - { - enum: ["always", "never"], - }, - { - type: "object", - properties: { - enforceForClassMembers: { - type: "boolean", - default: true, - }, - }, - additionalProperties: false, - }, - ], - - messages: { - unexpectedSpaceBefore: - "There should be no space before '{{tokenValue}}'.", - unexpectedSpaceAfter: - "There should be no space after '{{tokenValue}}'.", - - missingSpaceBefore: "A space is required before '{{tokenValue}}'.", - missingSpaceAfter: "A space is required after '{{tokenValue}}'.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - const propertyNameMustBeSpaced = context.options[0] === "always"; // default is "never" - const enforceForClassMembers = - !context.options[1] || context.options[1].enforceForClassMembers; - - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - /** - * Reports that there shouldn't be a space after the first token - * @param {ASTNode} node The node to report in the event of an error. - * @param {Token} token The token to use for the report. - * @param {Token} tokenAfter The token after `token`. - * @returns {void} - */ - function reportNoBeginningSpace(node, token, tokenAfter) { - context.report({ - node, - loc: { start: token.loc.end, end: tokenAfter.loc.start }, - messageId: "unexpectedSpaceAfter", - data: { - tokenValue: token.value, - }, - fix(fixer) { - return fixer.removeRange([ - token.range[1], - tokenAfter.range[0], - ]); - }, - }); - } - - /** - * Reports that there shouldn't be a space before the last token - * @param {ASTNode} node The node to report in the event of an error. - * @param {Token} token The token to use for the report. - * @param {Token} tokenBefore The token before `token`. - * @returns {void} - */ - function reportNoEndingSpace(node, token, tokenBefore) { - context.report({ - node, - loc: { start: tokenBefore.loc.end, end: token.loc.start }, - messageId: "unexpectedSpaceBefore", - data: { - tokenValue: token.value, - }, - fix(fixer) { - return fixer.removeRange([ - tokenBefore.range[1], - token.range[0], - ]); - }, - }); - } - - /** - * Reports that there should be a space after the first token - * @param {ASTNode} node The node to report in the event of an error. - * @param {Token} token The token to use for the report. - * @returns {void} - */ - function reportRequiredBeginningSpace(node, token) { - context.report({ - node, - loc: token.loc, - messageId: "missingSpaceAfter", - data: { - tokenValue: token.value, - }, - fix(fixer) { - return fixer.insertTextAfter(token, " "); - }, - }); - } - - /** - * Reports that there should be a space before the last token - * @param {ASTNode} node The node to report in the event of an error. - * @param {Token} token The token to use for the report. - * @returns {void} - */ - function reportRequiredEndingSpace(node, token) { - context.report({ - node, - loc: token.loc, - messageId: "missingSpaceBefore", - data: { - tokenValue: token.value, - }, - fix(fixer) { - return fixer.insertTextBefore(token, " "); - }, - }); - } - - /** - * Returns a function that checks the spacing of a node on the property name - * that was passed in. - * @param {string} propertyName The property on the node to check for spacing - * @returns {Function} A function that will check spacing on a node - */ - function checkSpacing(propertyName) { - return function (node) { - if (!node.computed) { - return; - } - - const property = node[propertyName]; - - const before = sourceCode.getTokenBefore( - property, - astUtils.isOpeningBracketToken, - ), - first = sourceCode.getTokenAfter(before, { - includeComments: true, - }), - after = sourceCode.getTokenAfter( - property, - astUtils.isClosingBracketToken, - ), - last = sourceCode.getTokenBefore(after, { - includeComments: true, - }); - - if (astUtils.isTokenOnSameLine(before, first)) { - if (propertyNameMustBeSpaced) { - if ( - !sourceCode.isSpaceBetweenTokens(before, first) && - astUtils.isTokenOnSameLine(before, first) - ) { - reportRequiredBeginningSpace(node, before); - } - } else { - if (sourceCode.isSpaceBetweenTokens(before, first)) { - reportNoBeginningSpace(node, before, first); - } - } - } - - if (astUtils.isTokenOnSameLine(last, after)) { - if (propertyNameMustBeSpaced) { - if ( - !sourceCode.isSpaceBetweenTokens(last, after) && - astUtils.isTokenOnSameLine(last, after) - ) { - reportRequiredEndingSpace(node, after); - } - } else { - if (sourceCode.isSpaceBetweenTokens(last, after)) { - reportNoEndingSpace(node, after, last); - } - } - } - }; - } - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - const listeners = { - Property: checkSpacing("key"), - MemberExpression: checkSpacing("property"), - }; - - if (enforceForClassMembers) { - listeners.MethodDefinition = listeners.PropertyDefinition = - listeners.Property; - } - - return listeners; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Enforce consistent spacing inside computed property brackets", + recommended: false, + url: "https://eslint.org/docs/latest/rules/computed-property-spacing" + }, + + fixable: "whitespace", + + schema: [ + { + enum: ["always", "never"] + }, + { + type: "object", + properties: { + enforceForClassMembers: { + type: "boolean", + default: true + } + }, + additionalProperties: false + } + ], + + messages: { + unexpectedSpaceBefore: "There should be no space before '{{tokenValue}}'.", + unexpectedSpaceAfter: "There should be no space after '{{tokenValue}}'.", + + missingSpaceBefore: "A space is required before '{{tokenValue}}'.", + missingSpaceAfter: "A space is required after '{{tokenValue}}'." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + const propertyNameMustBeSpaced = context.options[0] === "always"; // default is "never" + const enforceForClassMembers = !context.options[1] || context.options[1].enforceForClassMembers; + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + /** + * Reports that there shouldn't be a space after the first token + * @param {ASTNode} node The node to report in the event of an error. + * @param {Token} token The token to use for the report. + * @param {Token} tokenAfter The token after `token`. + * @returns {void} + */ + function reportNoBeginningSpace(node, token, tokenAfter) { + context.report({ + node, + loc: { start: token.loc.end, end: tokenAfter.loc.start }, + messageId: "unexpectedSpaceAfter", + data: { + tokenValue: token.value + }, + fix(fixer) { + return fixer.removeRange([token.range[1], tokenAfter.range[0]]); + } + }); + } + + /** + * Reports that there shouldn't be a space before the last token + * @param {ASTNode} node The node to report in the event of an error. + * @param {Token} token The token to use for the report. + * @param {Token} tokenBefore The token before `token`. + * @returns {void} + */ + function reportNoEndingSpace(node, token, tokenBefore) { + context.report({ + node, + loc: { start: tokenBefore.loc.end, end: token.loc.start }, + messageId: "unexpectedSpaceBefore", + data: { + tokenValue: token.value + }, + fix(fixer) { + return fixer.removeRange([tokenBefore.range[1], token.range[0]]); + } + }); + } + + /** + * Reports that there should be a space after the first token + * @param {ASTNode} node The node to report in the event of an error. + * @param {Token} token The token to use for the report. + * @returns {void} + */ + function reportRequiredBeginningSpace(node, token) { + context.report({ + node, + loc: token.loc, + messageId: "missingSpaceAfter", + data: { + tokenValue: token.value + }, + fix(fixer) { + return fixer.insertTextAfter(token, " "); + } + }); + } + + /** + * Reports that there should be a space before the last token + * @param {ASTNode} node The node to report in the event of an error. + * @param {Token} token The token to use for the report. + * @returns {void} + */ + function reportRequiredEndingSpace(node, token) { + context.report({ + node, + loc: token.loc, + messageId: "missingSpaceBefore", + data: { + tokenValue: token.value + }, + fix(fixer) { + return fixer.insertTextBefore(token, " "); + } + }); + } + + /** + * Returns a function that checks the spacing of a node on the property name + * that was passed in. + * @param {string} propertyName The property on the node to check for spacing + * @returns {Function} A function that will check spacing on a node + */ + function checkSpacing(propertyName) { + return function(node) { + if (!node.computed) { + return; + } + + const property = node[propertyName]; + + const before = sourceCode.getTokenBefore(property, astUtils.isOpeningBracketToken), + first = sourceCode.getTokenAfter(before, { includeComments: true }), + after = sourceCode.getTokenAfter(property, astUtils.isClosingBracketToken), + last = sourceCode.getTokenBefore(after, { includeComments: true }); + + if (astUtils.isTokenOnSameLine(before, first)) { + if (propertyNameMustBeSpaced) { + if (!sourceCode.isSpaceBetweenTokens(before, first) && astUtils.isTokenOnSameLine(before, first)) { + reportRequiredBeginningSpace(node, before); + } + } else { + if (sourceCode.isSpaceBetweenTokens(before, first)) { + reportNoBeginningSpace(node, before, first); + } + } + } + + if (astUtils.isTokenOnSameLine(last, after)) { + if (propertyNameMustBeSpaced) { + if (!sourceCode.isSpaceBetweenTokens(last, after) && astUtils.isTokenOnSameLine(last, after)) { + reportRequiredEndingSpace(node, after); + } + } else { + if (sourceCode.isSpaceBetweenTokens(last, after)) { + reportNoEndingSpace(node, after, last); + } + } + } + }; + } + + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + const listeners = { + Property: checkSpacing("key"), + MemberExpression: checkSpacing("property") + }; + + if (enforceForClassMembers) { + listeners.MethodDefinition = + listeners.PropertyDefinition = listeners.Property; + } + + return listeners; + + } }; diff --git a/node_modules/eslint/lib/rules/consistent-return.js b/node_modules/eslint/lib/rules/consistent-return.js index 6407739b7..304e924b1 100644 --- a/node_modules/eslint/lib/rules/consistent-return.js +++ b/node_modules/eslint/lib/rules/consistent-return.js @@ -21,13 +21,14 @@ const { upperCaseFirst } = require("../shared/string-utils"); * @returns {boolean} True if all segments are unreachable; false otherwise. */ function areAllSegmentsUnreachable(segments) { - for (const segment of segments) { - if (segment.reachable) { - return false; - } - } - return true; + for (const segment of segments) { + if (segment.reachable) { + return false; + } + } + + return true; } /** @@ -36,186 +37,174 @@ function areAllSegmentsUnreachable(segments) { * @returns {boolean} `true` if the node is a `constructor` method */ function isClassConstructor(node) { - return ( - node.type === "FunctionExpression" && - node.parent && - node.parent.type === "MethodDefinition" && - node.parent.kind === "constructor" - ); + return node.type === "FunctionExpression" && + node.parent && + node.parent.type === "MethodDefinition" && + node.parent.kind === "constructor"; } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: - "Require `return` statements to either always or never specify values", - recommended: false, - url: "https://eslint.org/docs/latest/rules/consistent-return", - }, - - schema: [ - { - type: "object", - properties: { - treatUndefinedAsUnspecified: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - - defaultOptions: [{ treatUndefinedAsUnspecified: false }], - - messages: { - missingReturn: "Expected to return a value at the end of {{name}}.", - missingReturnValue: "{{name}} expected a return value.", - unexpectedReturnValue: "{{name}} expected no return value.", - }, - }, - - create(context) { - const [{ treatUndefinedAsUnspecified }] = context.options; - let funcInfo = null; - - /** - * Checks whether of not the implicit returning is consistent if the last - * code path segment is reachable. - * @param {ASTNode} node A program/function node to check. - * @returns {void} - */ - function checkLastSegment(node) { - let loc, name; - - /* - * Skip if it expected no return value or unreachable. - * When unreachable, all paths are returned or thrown. - */ - if ( - !funcInfo.hasReturnValue || - areAllSegmentsUnreachable(funcInfo.currentSegments) || - astUtils.isES5Constructor(node) || - isClassConstructor(node) - ) { - return; - } - - // Adjust a location and a message. - if (node.type === "Program") { - // The head of program. - loc = { line: 1, column: 0 }; - name = "program"; - } else if (node.type === "ArrowFunctionExpression") { - // `=>` token - loc = context.sourceCode.getTokenBefore( - node.body, - astUtils.isArrowToken, - ).loc; - } else if ( - node.parent.type === "MethodDefinition" || - (node.parent.type === "Property" && node.parent.method) - ) { - // Method name. - loc = node.parent.key.loc; - } else { - // Function name or `function` keyword. - loc = (node.id || context.sourceCode.getFirstToken(node)).loc; - } - - if (!name) { - name = astUtils.getFunctionNameWithKind(node); - } - - // Reports. - context.report({ - node, - loc, - messageId: "missingReturn", - data: { name }, - }); - } - - return { - // Initializes/Disposes state of each code path. - onCodePathStart(codePath, node) { - funcInfo = { - upper: funcInfo, - codePath, - hasReturn: false, - hasReturnValue: false, - messageId: "", - node, - currentSegments: new Set(), - }; - }, - onCodePathEnd() { - funcInfo = funcInfo.upper; - }, - - onUnreachableCodePathSegmentStart(segment) { - funcInfo.currentSegments.add(segment); - }, - - onUnreachableCodePathSegmentEnd(segment) { - funcInfo.currentSegments.delete(segment); - }, - - onCodePathSegmentStart(segment) { - funcInfo.currentSegments.add(segment); - }, - - onCodePathSegmentEnd(segment) { - funcInfo.currentSegments.delete(segment); - }, - - // Reports a given return statement if it's inconsistent. - ReturnStatement(node) { - const argument = node.argument; - let hasReturnValue = Boolean(argument); - - if (treatUndefinedAsUnspecified && hasReturnValue) { - hasReturnValue = - !astUtils.isSpecificId(argument, "undefined") && - argument.operator !== "void"; - } - - if (!funcInfo.hasReturn) { - funcInfo.hasReturn = true; - funcInfo.hasReturnValue = hasReturnValue; - funcInfo.messageId = hasReturnValue - ? "missingReturnValue" - : "unexpectedReturnValue"; - funcInfo.data = { - name: - funcInfo.node.type === "Program" - ? "Program" - : upperCaseFirst( - astUtils.getFunctionNameWithKind( - funcInfo.node, - ), - ), - }; - } else if (funcInfo.hasReturnValue !== hasReturnValue) { - context.report({ - node, - messageId: funcInfo.messageId, - data: funcInfo.data, - }); - } - }, - - // Reports a given program/function if the implicit returning is not consistent. - "Program:exit": checkLastSegment, - "FunctionDeclaration:exit": checkLastSegment, - "FunctionExpression:exit": checkLastSegment, - "ArrowFunctionExpression:exit": checkLastSegment, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Require `return` statements to either always or never specify values", + recommended: false, + url: "https://eslint.org/docs/latest/rules/consistent-return" + }, + + schema: [{ + type: "object", + properties: { + treatUndefinedAsUnspecified: { + type: "boolean", + default: false + } + }, + additionalProperties: false + }], + + messages: { + missingReturn: "Expected to return a value at the end of {{name}}.", + missingReturnValue: "{{name}} expected a return value.", + unexpectedReturnValue: "{{name}} expected no return value." + } + }, + + create(context) { + const options = context.options[0] || {}; + const treatUndefinedAsUnspecified = options.treatUndefinedAsUnspecified === true; + let funcInfo = null; + + /** + * Checks whether of not the implicit returning is consistent if the last + * code path segment is reachable. + * @param {ASTNode} node A program/function node to check. + * @returns {void} + */ + function checkLastSegment(node) { + let loc, name; + + /* + * Skip if it expected no return value or unreachable. + * When unreachable, all paths are returned or thrown. + */ + if (!funcInfo.hasReturnValue || + areAllSegmentsUnreachable(funcInfo.currentSegments) || + astUtils.isES5Constructor(node) || + isClassConstructor(node) + ) { + return; + } + + // Adjust a location and a message. + if (node.type === "Program") { + + // The head of program. + loc = { line: 1, column: 0 }; + name = "program"; + } else if (node.type === "ArrowFunctionExpression") { + + // `=>` token + loc = context.sourceCode.getTokenBefore(node.body, astUtils.isArrowToken).loc; + } else if ( + node.parent.type === "MethodDefinition" || + (node.parent.type === "Property" && node.parent.method) + ) { + + // Method name. + loc = node.parent.key.loc; + } else { + + // Function name or `function` keyword. + loc = (node.id || context.sourceCode.getFirstToken(node)).loc; + } + + if (!name) { + name = astUtils.getFunctionNameWithKind(node); + } + + // Reports. + context.report({ + node, + loc, + messageId: "missingReturn", + data: { name } + }); + } + + return { + + // Initializes/Disposes state of each code path. + onCodePathStart(codePath, node) { + funcInfo = { + upper: funcInfo, + codePath, + hasReturn: false, + hasReturnValue: false, + messageId: "", + node, + currentSegments: new Set() + }; + }, + onCodePathEnd() { + funcInfo = funcInfo.upper; + }, + + onUnreachableCodePathSegmentStart(segment) { + funcInfo.currentSegments.add(segment); + }, + + onUnreachableCodePathSegmentEnd(segment) { + funcInfo.currentSegments.delete(segment); + }, + + onCodePathSegmentStart(segment) { + funcInfo.currentSegments.add(segment); + }, + + onCodePathSegmentEnd(segment) { + funcInfo.currentSegments.delete(segment); + }, + + + // Reports a given return statement if it's inconsistent. + ReturnStatement(node) { + const argument = node.argument; + let hasReturnValue = Boolean(argument); + + if (treatUndefinedAsUnspecified && hasReturnValue) { + hasReturnValue = !astUtils.isSpecificId(argument, "undefined") && argument.operator !== "void"; + } + + if (!funcInfo.hasReturn) { + funcInfo.hasReturn = true; + funcInfo.hasReturnValue = hasReturnValue; + funcInfo.messageId = hasReturnValue ? "missingReturnValue" : "unexpectedReturnValue"; + funcInfo.data = { + name: funcInfo.node.type === "Program" + ? "Program" + : upperCaseFirst(astUtils.getFunctionNameWithKind(funcInfo.node)) + }; + } else if (funcInfo.hasReturnValue !== hasReturnValue) { + context.report({ + node, + messageId: funcInfo.messageId, + data: funcInfo.data + }); + } + }, + + // Reports a given program/function if the implicit returning is not consistent. + "Program:exit": checkLastSegment, + "FunctionDeclaration:exit": checkLastSegment, + "FunctionExpression:exit": checkLastSegment, + "ArrowFunctionExpression:exit": checkLastSegment + }; + } }; diff --git a/node_modules/eslint/lib/rules/consistent-this.js b/node_modules/eslint/lib/rules/consistent-this.js index 8e9372d29..658957ae2 100644 --- a/node_modules/eslint/lib/rules/consistent-this.js +++ b/node_modules/eslint/lib/rules/consistent-this.js @@ -8,172 +8,146 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: - "Enforce consistent naming when capturing the current execution context", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/consistent-this", - }, - - schema: { - type: "array", - items: { - type: "string", - minLength: 1, - }, - uniqueItems: true, - }, - - defaultOptions: ["that"], - - messages: { - aliasNotAssignedToThis: - "Designated alias '{{name}}' is not assigned to 'this'.", - unexpectedAlias: "Unexpected alias '{{name}}' for 'this'.", - }, - }, - - create(context) { - const aliases = context.options; - const sourceCode = context.sourceCode; - - /** - * Reports that a variable declarator or assignment expression is assigning - * a non-'this' value to the specified alias. - * @param {ASTNode} node The assigning node. - * @param {string} name the name of the alias that was incorrectly used. - * @returns {void} - */ - function reportBadAssignment(node, name) { - context.report({ - node, - messageId: "aliasNotAssignedToThis", - data: { name }, - }); - } - - /** - * Checks that an assignment to an identifier only assigns 'this' to the - * appropriate alias, and the alias is only assigned to 'this'. - * @param {ASTNode} node The assigning node. - * @param {Identifier} name The name of the variable assigned to. - * @param {Expression} value The value of the assignment. - * @returns {void} - */ - function checkAssignment(node, name, value) { - const isThis = value.type === "ThisExpression"; - - if (aliases.includes(name)) { - if (!isThis || (node.operator && node.operator !== "=")) { - reportBadAssignment(node, name); - } - } else if (isThis) { - context.report({ - node, - messageId: "unexpectedAlias", - data: { name }, - }); - } - } - - /** - * Ensures that a variable declaration of the alias in a program or function - * is assigned to the correct value. - * @param {string} alias alias the check the assignment of. - * @param {Object} scope scope of the current code we are checking. - * @private - * @returns {void} - */ - function checkWasAssigned(alias, scope) { - const variable = scope.set.get(alias); - - if (!variable) { - return; - } - - if ( - variable.defs.some( - def => - def.node.type === "VariableDeclarator" && - def.node.init !== null, - ) - ) { - return; - } - - /* - * The alias has been declared and not assigned: check it was - * assigned later in the same scope. - */ - if ( - !variable.references.some(reference => { - const write = reference.writeExpr; - - return ( - reference.from === scope && - write && - write.type === "ThisExpression" && - write.parent.operator === "=" - ); - }) - ) { - variable.defs - .map(def => def.node) - .forEach(node => { - reportBadAssignment(node, alias); - }); - } - } - - /** - * Check each alias to ensure that is was assigned to the correct value. - * @param {ASTNode} node The node that represents the scope to check. - * @returns {void} - */ - function ensureWasAssigned(node) { - const scope = sourceCode.getScope(node); - - // if this is program scope we also need to check module scope - const extraScope = - node.type === "Program" && node.sourceType === "module" - ? scope.childScopes[0] - : null; - - aliases.forEach(alias => { - checkWasAssigned(alias, scope); - - if (extraScope) { - checkWasAssigned(alias, extraScope); - } - }); - } - - return { - "Program:exit": ensureWasAssigned, - "FunctionExpression:exit": ensureWasAssigned, - "FunctionDeclaration:exit": ensureWasAssigned, - - VariableDeclarator(node) { - const id = node.id; - const isDestructuring = - id.type === "ArrayPattern" || id.type === "ObjectPattern"; - - if (node.init !== null && !isDestructuring) { - checkAssignment(node, id.name, node.init); - } - }, - - AssignmentExpression(node) { - if (node.left.type === "Identifier") { - checkAssignment(node, node.left.name, node.right); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Enforce consistent naming when capturing the current execution context", + recommended: false, + url: "https://eslint.org/docs/latest/rules/consistent-this" + }, + + schema: { + type: "array", + items: { + type: "string", + minLength: 1 + }, + uniqueItems: true + }, + + messages: { + aliasNotAssignedToThis: "Designated alias '{{name}}' is not assigned to 'this'.", + unexpectedAlias: "Unexpected alias '{{name}}' for 'this'." + } + }, + + create(context) { + let aliases = []; + const sourceCode = context.sourceCode; + + if (context.options.length === 0) { + aliases.push("that"); + } else { + aliases = context.options; + } + + /** + * Reports that a variable declarator or assignment expression is assigning + * a non-'this' value to the specified alias. + * @param {ASTNode} node The assigning node. + * @param {string} name the name of the alias that was incorrectly used. + * @returns {void} + */ + function reportBadAssignment(node, name) { + context.report({ node, messageId: "aliasNotAssignedToThis", data: { name } }); + } + + /** + * Checks that an assignment to an identifier only assigns 'this' to the + * appropriate alias, and the alias is only assigned to 'this'. + * @param {ASTNode} node The assigning node. + * @param {Identifier} name The name of the variable assigned to. + * @param {Expression} value The value of the assignment. + * @returns {void} + */ + function checkAssignment(node, name, value) { + const isThis = value.type === "ThisExpression"; + + if (aliases.includes(name)) { + if (!isThis || node.operator && node.operator !== "=") { + reportBadAssignment(node, name); + } + } else if (isThis) { + context.report({ node, messageId: "unexpectedAlias", data: { name } }); + } + } + + /** + * Ensures that a variable declaration of the alias in a program or function + * is assigned to the correct value. + * @param {string} alias alias the check the assignment of. + * @param {Object} scope scope of the current code we are checking. + * @private + * @returns {void} + */ + function checkWasAssigned(alias, scope) { + const variable = scope.set.get(alias); + + if (!variable) { + return; + } + + if (variable.defs.some(def => def.node.type === "VariableDeclarator" && + def.node.init !== null)) { + return; + } + + /* + * The alias has been declared and not assigned: check it was + * assigned later in the same scope. + */ + if (!variable.references.some(reference => { + const write = reference.writeExpr; + + return ( + reference.from === scope && + write && write.type === "ThisExpression" && + write.parent.operator === "=" + ); + })) { + variable.defs.map(def => def.node).forEach(node => { + reportBadAssignment(node, alias); + }); + } + } + + /** + * Check each alias to ensure that is was assigned to the correct value. + * @param {ASTNode} node The node that represents the scope to check. + * @returns {void} + */ + function ensureWasAssigned(node) { + const scope = sourceCode.getScope(node); + + aliases.forEach(alias => { + checkWasAssigned(alias, scope); + }); + } + + return { + "Program:exit": ensureWasAssigned, + "FunctionExpression:exit": ensureWasAssigned, + "FunctionDeclaration:exit": ensureWasAssigned, + + VariableDeclarator(node) { + const id = node.id; + const isDestructuring = + id.type === "ArrayPattern" || id.type === "ObjectPattern"; + + if (node.init !== null && !isDestructuring) { + checkAssignment(node, id.name, node.init); + } + }, + + AssignmentExpression(node) { + if (node.left.type === "Identifier") { + checkAssignment(node, node.left.name, node.right); + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/constructor-super.js b/node_modules/eslint/lib/rules/constructor-super.js index 2262677d3..330be80f3 100644 --- a/node_modules/eslint/lib/rules/constructor-super.js +++ b/node_modules/eslint/lib/rules/constructor-super.js @@ -9,6 +9,22 @@ // Helpers //------------------------------------------------------------------------------ +/** + * Checks all segments in a set and returns true if any are reachable. + * @param {Set} segments The segments to check. + * @returns {boolean} True if any segment is reachable; false otherwise. + */ +function isAnySegmentReachable(segments) { + + for (const segment of segments) { + if (segment.reachable) { + return true; + } + } + + return false; +} + /** * Checks whether or not a given node is a constructor. * @param {ASTNode} node A node to check. This node type is one of @@ -17,11 +33,11 @@ * @returns {boolean} `true` if the node is a constructor. */ function isConstructorFunction(node) { - return ( - node.type === "FunctionExpression" && - node.parent.type === "MethodDefinition" && - node.parent.kind === "constructor" - ); + return ( + node.type === "FunctionExpression" && + node.parent.type === "MethodDefinition" && + node.parent.kind === "constructor" + ); } /** @@ -30,424 +46,401 @@ function isConstructorFunction(node) { * @returns {boolean} `true` if the node can be a constructor. */ function isPossibleConstructor(node) { - if (!node) { - return false; - } - - switch (node.type) { - case "ClassExpression": - case "FunctionExpression": - case "ThisExpression": - case "MemberExpression": - case "CallExpression": - case "NewExpression": - case "ChainExpression": - case "YieldExpression": - case "TaggedTemplateExpression": - case "MetaProperty": - return true; - - case "Identifier": - return node.name !== "undefined"; - - case "AssignmentExpression": - if (["=", "&&="].includes(node.operator)) { - return isPossibleConstructor(node.right); - } - - if (["||=", "??="].includes(node.operator)) { - return ( - isPossibleConstructor(node.left) || - isPossibleConstructor(node.right) - ); - } - - /** - * All other assignment operators are mathematical assignment operators (arithmetic or bitwise). - * An assignment expression with a mathematical operator can either evaluate to a primitive value, - * or throw, depending on the operands. Thus, it cannot evaluate to a constructor function. - */ - return false; - - case "LogicalExpression": - /* - * If the && operator short-circuits, the left side was falsy and therefore not a constructor, and if - * it doesn't short-circuit, it takes the value from the right side, so the right side must always be a - * possible constructor. A future improvement could verify that the left side could be truthy by - * excluding falsy literals. - */ - if (node.operator === "&&") { - return isPossibleConstructor(node.right); - } - - return ( - isPossibleConstructor(node.left) || - isPossibleConstructor(node.right) - ); - - case "ConditionalExpression": - return ( - isPossibleConstructor(node.alternate) || - isPossibleConstructor(node.consequent) - ); - - case "SequenceExpression": { - const lastExpression = node.expressions.at(-1); - - return isPossibleConstructor(lastExpression); - } - - default: - return false; - } -} - -/** - * A class to store information about a code path segment. - */ -class SegmentInfo { - /** - * Indicates if super() is called in all code paths. - * @type {boolean} - */ - calledInEveryPaths = false; - - /** - * Indicates if super() is called in any code paths. - * @type {boolean} - */ - calledInSomePaths = false; - - /** - * The nodes which have been validated and don't need to be reconsidered. - * @type {ASTNode[]} - */ - validNodes = []; + if (!node) { + return false; + } + + switch (node.type) { + case "ClassExpression": + case "FunctionExpression": + case "ThisExpression": + case "MemberExpression": + case "CallExpression": + case "NewExpression": + case "ChainExpression": + case "YieldExpression": + case "TaggedTemplateExpression": + case "MetaProperty": + return true; + + case "Identifier": + return node.name !== "undefined"; + + case "AssignmentExpression": + if (["=", "&&="].includes(node.operator)) { + return isPossibleConstructor(node.right); + } + + if (["||=", "??="].includes(node.operator)) { + return ( + isPossibleConstructor(node.left) || + isPossibleConstructor(node.right) + ); + } + + /** + * All other assignment operators are mathematical assignment operators (arithmetic or bitwise). + * An assignment expression with a mathematical operator can either evaluate to a primitive value, + * or throw, depending on the operands. Thus, it cannot evaluate to a constructor function. + */ + return false; + + case "LogicalExpression": + + /* + * If the && operator short-circuits, the left side was falsy and therefore not a constructor, and if + * it doesn't short-circuit, it takes the value from the right side, so the right side must always be a + * possible constructor. A future improvement could verify that the left side could be truthy by + * excluding falsy literals. + */ + if (node.operator === "&&") { + return isPossibleConstructor(node.right); + } + + return ( + isPossibleConstructor(node.left) || + isPossibleConstructor(node.right) + ); + + case "ConditionalExpression": + return ( + isPossibleConstructor(node.alternate) || + isPossibleConstructor(node.consequent) + ); + + case "SequenceExpression": { + const lastExpression = node.expressions[node.expressions.length - 1]; + + return isPossibleConstructor(lastExpression); + } + + default: + return false; + } } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - docs: { - description: "Require `super()` calls in constructors", - recommended: true, - url: "https://eslint.org/docs/latest/rules/constructor-super", - }, - - schema: [], - - messages: { - missingSome: "Lacked a call of 'super()' in some code paths.", - missingAll: "Expected to call 'super()'.", - - duplicate: "Unexpected duplicate 'super()'.", - badSuper: - "Unexpected 'super()' because 'super' is not a constructor.", - }, - }, - - create(context) { - /* - * {{hasExtends: boolean, scope: Scope, codePath: CodePath}[]} - * Information for each constructor. - * - upper: Information of the upper constructor. - * - hasExtends: A flag which shows whether own class has a valid `extends` - * part. - * - scope: The scope of own class. - * - codePath: The code path object of the constructor. - */ - let funcInfo = null; - - /** - * @type {Record} - */ - const segInfoMap = Object.create(null); - - /** - * Gets the flag which shows `super()` is called in some paths. - * @param {CodePathSegment} segment A code path segment to get. - * @returns {boolean} The flag which shows `super()` is called in some paths - */ - function isCalledInSomePath(segment) { - return ( - segment.reachable && segInfoMap[segment.id].calledInSomePaths - ); - } - - /** - * Determines if a segment has been seen in the traversal. - * @param {CodePathSegment} segment A code path segment to check. - * @returns {boolean} `true` if the segment has been seen. - */ - function hasSegmentBeenSeen(segment) { - return !!segInfoMap[segment.id]; - } - - /** - * Gets the flag which shows `super()` is called in all paths. - * @param {CodePathSegment} segment A code path segment to get. - * @returns {boolean} The flag which shows `super()` is called in all paths. - */ - function isCalledInEveryPath(segment) { - return ( - segment.reachable && segInfoMap[segment.id].calledInEveryPaths - ); - } - - return { - /** - * Stacks a constructor information. - * @param {CodePath} codePath A code path which was started. - * @param {ASTNode} node The current node. - * @returns {void} - */ - onCodePathStart(codePath, node) { - if (isConstructorFunction(node)) { - // Class > ClassBody > MethodDefinition > FunctionExpression - const classNode = node.parent.parent.parent; - const superClass = classNode.superClass; - - funcInfo = { - upper: funcInfo, - isConstructor: true, - hasExtends: Boolean(superClass), - superIsConstructor: isPossibleConstructor(superClass), - codePath, - currentSegments: new Set(), - }; - } else { - funcInfo = { - upper: funcInfo, - isConstructor: false, - hasExtends: false, - superIsConstructor: false, - codePath, - currentSegments: new Set(), - }; - } - }, - - /** - * Pops a constructor information. - * And reports if `super()` lacked. - * @param {CodePath} codePath A code path which was ended. - * @param {ASTNode} node The current node. - * @returns {void} - */ - onCodePathEnd(codePath, node) { - const hasExtends = funcInfo.hasExtends; - - // Pop. - funcInfo = funcInfo.upper; - - if (!hasExtends) { - return; - } - - // Reports if `super()` lacked. - const returnedSegments = codePath.returnedSegments; - const calledInEveryPaths = - returnedSegments.every(isCalledInEveryPath); - const calledInSomePaths = - returnedSegments.some(isCalledInSomePath); - - if (!calledInEveryPaths) { - context.report({ - messageId: calledInSomePaths - ? "missingSome" - : "missingAll", - node: node.parent, - }); - } - }, - - /** - * Initialize information of a given code path segment. - * @param {CodePathSegment} segment A code path segment to initialize. - * @param {CodePathSegment} node Node that starts the segment. - * @returns {void} - */ - onCodePathSegmentStart(segment, node) { - funcInfo.currentSegments.add(segment); - - if (!(funcInfo.isConstructor && funcInfo.hasExtends)) { - return; - } - - // Initialize info. - const info = (segInfoMap[segment.id] = new SegmentInfo()); - - const seenPrevSegments = - segment.prevSegments.filter(hasSegmentBeenSeen); - - // When there are previous segments, aggregates these. - if (seenPrevSegments.length > 0) { - info.calledInSomePaths = - seenPrevSegments.some(isCalledInSomePath); - info.calledInEveryPaths = - seenPrevSegments.every(isCalledInEveryPath); - } - - /* - * ForStatement > *.update segments are a special case as they are created in advance, - * without seen previous segments. Since they logically don't affect `calledInEveryPaths` - * calculations, and they can never be a lone previous segment of another one, we'll set - * their `calledInEveryPaths` to `true` to effectively ignore them in those calculations. - * . - */ - if ( - node.parent && - node.parent.type === "ForStatement" && - node.parent.update === node - ) { - info.calledInEveryPaths = true; - } - }, - - onUnreachableCodePathSegmentStart(segment) { - funcInfo.currentSegments.add(segment); - }, - - onUnreachableCodePathSegmentEnd(segment) { - funcInfo.currentSegments.delete(segment); - }, - - onCodePathSegmentEnd(segment) { - funcInfo.currentSegments.delete(segment); - }, - - /** - * Update information of the code path segment when a code path was - * looped. - * @param {CodePathSegment} fromSegment The code path segment of the - * end of a loop. - * @param {CodePathSegment} toSegment A code path segment of the head - * of a loop. - * @returns {void} - */ - onCodePathSegmentLoop(fromSegment, toSegment) { - if (!(funcInfo.isConstructor && funcInfo.hasExtends)) { - return; - } - - funcInfo.codePath.traverseSegments( - { first: toSegment, last: fromSegment }, - (segment, controller) => { - const info = segInfoMap[segment.id]; - - // skip segments after the loop - if (!info) { - controller.skip(); - return; - } - - const seenPrevSegments = - segment.prevSegments.filter(hasSegmentBeenSeen); - const calledInSomePreviousPaths = - seenPrevSegments.some(isCalledInSomePath); - const calledInEveryPreviousPaths = - seenPrevSegments.every(isCalledInEveryPath); - - info.calledInSomePaths ||= calledInSomePreviousPaths; - info.calledInEveryPaths ||= calledInEveryPreviousPaths; - - // If flags become true anew, reports the valid nodes. - if (calledInSomePreviousPaths) { - const nodes = info.validNodes; - - info.validNodes = []; - - for (let i = 0; i < nodes.length; ++i) { - const node = nodes[i]; - - context.report({ - messageId: "duplicate", - node, - }); - } - } - }, - ); - }, - - /** - * Checks for a call of `super()`. - * @param {ASTNode} node A CallExpression node to check. - * @returns {void} - */ - "CallExpression:exit"(node) { - if (!(funcInfo.isConstructor && funcInfo.hasExtends)) { - return; - } - - // Skips except `super()`. - if (node.callee.type !== "Super") { - return; - } - - // Reports if needed. - const segments = funcInfo.currentSegments; - let duplicate = false; - let info = null; - - for (const segment of segments) { - if (segment.reachable) { - info = segInfoMap[segment.id]; - - duplicate = duplicate || info.calledInSomePaths; - info.calledInSomePaths = info.calledInEveryPaths = true; - } - } - - if (info) { - if (duplicate) { - context.report({ - messageId: "duplicate", - node, - }); - } else if (!funcInfo.superIsConstructor) { - context.report({ - messageId: "badSuper", - node, - }); - } else { - info.validNodes.push(node); - } - } - }, - - /** - * Set the mark to the returned path as `super()` was called. - * @param {ASTNode} node A ReturnStatement node to check. - * @returns {void} - */ - ReturnStatement(node) { - if (!(funcInfo.isConstructor && funcInfo.hasExtends)) { - return; - } - - // Skips if no argument. - if (!node.argument) { - return; - } - - // Returning argument is a substitute of 'super()'. - const segments = funcInfo.currentSegments; - - for (const segment of segments) { - if (segment.reachable) { - const info = segInfoMap[segment.id]; - - info.calledInSomePaths = info.calledInEveryPaths = true; - } - } - }, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Require `super()` calls in constructors", + recommended: true, + url: "https://eslint.org/docs/latest/rules/constructor-super" + }, + + schema: [], + + messages: { + missingSome: "Lacked a call of 'super()' in some code paths.", + missingAll: "Expected to call 'super()'.", + + duplicate: "Unexpected duplicate 'super()'.", + badSuper: "Unexpected 'super()' because 'super' is not a constructor.", + unexpected: "Unexpected 'super()'." + } + }, + + create(context) { + + /* + * {{hasExtends: boolean, scope: Scope, codePath: CodePath}[]} + * Information for each constructor. + * - upper: Information of the upper constructor. + * - hasExtends: A flag which shows whether own class has a valid `extends` + * part. + * - scope: The scope of own class. + * - codePath: The code path object of the constructor. + */ + let funcInfo = null; + + /* + * {Map} + * Information for each code path segment. + * - calledInSomePaths: A flag of be called `super()` in some code paths. + * - calledInEveryPaths: A flag of be called `super()` in all code paths. + * - validNodes: + */ + let segInfoMap = Object.create(null); + + /** + * Gets the flag which shows `super()` is called in some paths. + * @param {CodePathSegment} segment A code path segment to get. + * @returns {boolean} The flag which shows `super()` is called in some paths + */ + function isCalledInSomePath(segment) { + return segment.reachable && segInfoMap[segment.id].calledInSomePaths; + } + + /** + * Gets the flag which shows `super()` is called in all paths. + * @param {CodePathSegment} segment A code path segment to get. + * @returns {boolean} The flag which shows `super()` is called in all paths. + */ + function isCalledInEveryPath(segment) { + + /* + * If specific segment is the looped segment of the current segment, + * skip the segment. + * If not skipped, this never becomes true after a loop. + */ + if (segment.nextSegments.length === 1 && + segment.nextSegments[0].isLoopedPrevSegment(segment) + ) { + return true; + } + return segment.reachable && segInfoMap[segment.id].calledInEveryPaths; + } + + return { + + /** + * Stacks a constructor information. + * @param {CodePath} codePath A code path which was started. + * @param {ASTNode} node The current node. + * @returns {void} + */ + onCodePathStart(codePath, node) { + if (isConstructorFunction(node)) { + + // Class > ClassBody > MethodDefinition > FunctionExpression + const classNode = node.parent.parent.parent; + const superClass = classNode.superClass; + + funcInfo = { + upper: funcInfo, + isConstructor: true, + hasExtends: Boolean(superClass), + superIsConstructor: isPossibleConstructor(superClass), + codePath, + currentSegments: new Set() + }; + } else { + funcInfo = { + upper: funcInfo, + isConstructor: false, + hasExtends: false, + superIsConstructor: false, + codePath, + currentSegments: new Set() + }; + } + }, + + /** + * Pops a constructor information. + * And reports if `super()` lacked. + * @param {CodePath} codePath A code path which was ended. + * @param {ASTNode} node The current node. + * @returns {void} + */ + onCodePathEnd(codePath, node) { + const hasExtends = funcInfo.hasExtends; + + // Pop. + funcInfo = funcInfo.upper; + + if (!hasExtends) { + return; + } + + // Reports if `super()` lacked. + const segments = codePath.returnedSegments; + const calledInEveryPaths = segments.every(isCalledInEveryPath); + const calledInSomePaths = segments.some(isCalledInSomePath); + + if (!calledInEveryPaths) { + context.report({ + messageId: calledInSomePaths + ? "missingSome" + : "missingAll", + node: node.parent + }); + } + }, + + /** + * Initialize information of a given code path segment. + * @param {CodePathSegment} segment A code path segment to initialize. + * @returns {void} + */ + onCodePathSegmentStart(segment) { + + funcInfo.currentSegments.add(segment); + + if (!(funcInfo && funcInfo.isConstructor && funcInfo.hasExtends)) { + return; + } + + // Initialize info. + const info = segInfoMap[segment.id] = { + calledInSomePaths: false, + calledInEveryPaths: false, + validNodes: [] + }; + + // When there are previous segments, aggregates these. + const prevSegments = segment.prevSegments; + + if (prevSegments.length > 0) { + info.calledInSomePaths = prevSegments.some(isCalledInSomePath); + info.calledInEveryPaths = prevSegments.every(isCalledInEveryPath); + } + }, + + onUnreachableCodePathSegmentStart(segment) { + funcInfo.currentSegments.add(segment); + }, + + onUnreachableCodePathSegmentEnd(segment) { + funcInfo.currentSegments.delete(segment); + }, + + onCodePathSegmentEnd(segment) { + funcInfo.currentSegments.delete(segment); + }, + + + /** + * Update information of the code path segment when a code path was + * looped. + * @param {CodePathSegment} fromSegment The code path segment of the + * end of a loop. + * @param {CodePathSegment} toSegment A code path segment of the head + * of a loop. + * @returns {void} + */ + onCodePathSegmentLoop(fromSegment, toSegment) { + if (!(funcInfo && funcInfo.isConstructor && funcInfo.hasExtends)) { + return; + } + + // Update information inside of the loop. + const isRealLoop = toSegment.prevSegments.length >= 2; + + funcInfo.codePath.traverseSegments( + { first: toSegment, last: fromSegment }, + segment => { + const info = segInfoMap[segment.id]; + const prevSegments = segment.prevSegments; + + // Updates flags. + info.calledInSomePaths = prevSegments.some(isCalledInSomePath); + info.calledInEveryPaths = prevSegments.every(isCalledInEveryPath); + + // If flags become true anew, reports the valid nodes. + if (info.calledInSomePaths || isRealLoop) { + const nodes = info.validNodes; + + info.validNodes = []; + + for (let i = 0; i < nodes.length; ++i) { + const node = nodes[i]; + + context.report({ + messageId: "duplicate", + node + }); + } + } + } + ); + }, + + /** + * Checks for a call of `super()`. + * @param {ASTNode} node A CallExpression node to check. + * @returns {void} + */ + "CallExpression:exit"(node) { + if (!(funcInfo && funcInfo.isConstructor)) { + return; + } + + // Skips except `super()`. + if (node.callee.type !== "Super") { + return; + } + + // Reports if needed. + if (funcInfo.hasExtends) { + const segments = funcInfo.currentSegments; + let duplicate = false; + let info = null; + + for (const segment of segments) { + + if (segment.reachable) { + info = segInfoMap[segment.id]; + + duplicate = duplicate || info.calledInSomePaths; + info.calledInSomePaths = info.calledInEveryPaths = true; + } + } + + if (info) { + if (duplicate) { + context.report({ + messageId: "duplicate", + node + }); + } else if (!funcInfo.superIsConstructor) { + context.report({ + messageId: "badSuper", + node + }); + } else { + info.validNodes.push(node); + } + } + } else if (isAnySegmentReachable(funcInfo.currentSegments)) { + context.report({ + messageId: "unexpected", + node + }); + } + }, + + /** + * Set the mark to the returned path as `super()` was called. + * @param {ASTNode} node A ReturnStatement node to check. + * @returns {void} + */ + ReturnStatement(node) { + if (!(funcInfo && funcInfo.isConstructor && funcInfo.hasExtends)) { + return; + } + + // Skips if no argument. + if (!node.argument) { + return; + } + + // Returning argument is a substitute of 'super()'. + const segments = funcInfo.currentSegments; + + for (const segment of segments) { + + if (segment.reachable) { + const info = segInfoMap[segment.id]; + + info.calledInSomePaths = info.calledInEveryPaths = true; + } + } + }, + + /** + * Resets state. + * @returns {void} + */ + "Program:exit"() { + segInfoMap = Object.create(null); + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/curly.js b/node_modules/eslint/lib/rules/curly.js index fd4438d31..35408247a 100644 --- a/node_modules/eslint/lib/rules/curly.js +++ b/node_modules/eslint/lib/rules/curly.js @@ -14,412 +14,473 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: - "Enforce consistent brace style for all control statements", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/curly", - }, - - schema: { - anyOf: [ - { - type: "array", - items: [ - { - enum: ["all"], - }, - ], - minItems: 0, - maxItems: 1, - }, - { - type: "array", - items: [ - { - enum: ["multi", "multi-line", "multi-or-nest"], - }, - { - enum: ["consistent"], - }, - ], - minItems: 0, - maxItems: 2, - }, - ], - }, - - defaultOptions: ["all"], - - fixable: "code", - - messages: { - missingCurlyAfter: "Expected { after '{{name}}'.", - missingCurlyAfterCondition: - "Expected { after '{{name}}' condition.", - unexpectedCurlyAfter: "Unnecessary { after '{{name}}'.", - unexpectedCurlyAfterCondition: - "Unnecessary { after '{{name}}' condition.", - }, - }, - - create(context) { - const multiOnly = context.options[0] === "multi"; - const multiLine = context.options[0] === "multi-line"; - const multiOrNest = context.options[0] === "multi-or-nest"; - const consistent = context.options[1] === "consistent"; - - const sourceCode = context.sourceCode; - - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - /** - * Determines if a given node is a one-liner that's on the same line as it's preceding code. - * @param {ASTNode} node The node to check. - * @returns {boolean} True if the node is a one-liner that's on the same line as it's preceding code. - * @private - */ - function isCollapsedOneLiner(node) { - const before = sourceCode.getTokenBefore(node); - const last = sourceCode.getLastToken(node); - const lastExcludingSemicolon = astUtils.isSemicolonToken(last) - ? sourceCode.getTokenBefore(last) - : last; - - return ( - before.loc.start.line === lastExcludingSemicolon.loc.end.line - ); - } - - /** - * Determines if a given node is a one-liner. - * @param {ASTNode} node The node to check. - * @returns {boolean} True if the node is a one-liner. - * @private - */ - function isOneLiner(node) { - if (node.type === "EmptyStatement") { - return true; - } - - const first = sourceCode.getFirstToken(node); - const last = sourceCode.getLastToken(node); - const lastExcludingSemicolon = astUtils.isSemicolonToken(last) - ? sourceCode.getTokenBefore(last) - : last; - - return first.loc.start.line === lastExcludingSemicolon.loc.end.line; - } - - /** - * Determines if a semicolon needs to be inserted after removing a set of curly brackets, in order to avoid a SyntaxError. - * @param {Token} closingBracket The } token - * @returns {boolean} `true` if a semicolon needs to be inserted after the last statement in the block. - */ - function needsSemicolon(closingBracket) { - const tokenBefore = sourceCode.getTokenBefore(closingBracket); - const tokenAfter = sourceCode.getTokenAfter(closingBracket); - const lastBlockNode = sourceCode.getNodeByRangeIndex( - tokenBefore.range[0], - ); - - if (astUtils.isSemicolonToken(tokenBefore)) { - // If the last statement already has a semicolon, don't add another one. - return false; - } - - if (!tokenAfter) { - // If there are no statements after this block, there is no need to add a semicolon. - return false; - } - - if ( - lastBlockNode.type === "BlockStatement" && - lastBlockNode.parent.type !== "FunctionExpression" && - lastBlockNode.parent.type !== "ArrowFunctionExpression" - ) { - /* - * If the last node surrounded by curly brackets is a BlockStatement (other than a FunctionExpression or an ArrowFunctionExpression), - * don't insert a semicolon. Otherwise, the semicolon would be parsed as a separate statement, which would cause - * a SyntaxError if it was followed by `else`. - */ - return false; - } - - if (tokenBefore.loc.end.line === tokenAfter.loc.start.line) { - // If the next token is on the same line, insert a semicolon. - return true; - } - - if (/^[([/`+-]/u.test(tokenAfter.value)) { - // If the next token starts with a character that would disrupt ASI, insert a semicolon. - return true; - } - - if ( - tokenBefore.type === "Punctuator" && - (tokenBefore.value === "++" || tokenBefore.value === "--") - ) { - // If the last token is ++ or --, insert a semicolon to avoid disrupting ASI. - return true; - } - - // Otherwise, do not insert a semicolon. - return false; - } - - /** - * Prepares to check the body of a node to see if it's a block statement. - * @param {ASTNode} node The node to report if there's a problem. - * @param {ASTNode} body The body node to check for blocks. - * @param {string} name The name to report if there's a problem. - * @param {{ condition: boolean }} opts Options to pass to the report functions - * @returns {Object} a prepared check object, with "actual", "expected", "check" properties. - * "actual" will be `true` or `false` whether the body is already a block statement. - * "expected" will be `true` or `false` if the body should be a block statement or not, or - * `null` if it doesn't matter, depending on the rule options. It can be modified to change - * the final behavior of "check". - * "check" will be a function reporting appropriate problems depending on the other - * properties. - */ - function prepareCheck(node, body, name, opts) { - const hasBlock = body.type === "BlockStatement"; - let expected = null; - - if ( - hasBlock && - (body.body.length !== 1 || - astUtils.areBracesNecessary(body, sourceCode)) - ) { - expected = true; - } else if (multiOnly) { - expected = false; - } else if (multiLine) { - if (!isCollapsedOneLiner(body)) { - expected = true; - } - - // otherwise, the body is allowed to have braces or not to have braces - } else if (multiOrNest) { - if (hasBlock) { - const statement = body.body[0]; - const leadingCommentsInBlock = - sourceCode.getCommentsBefore(statement); - - expected = - !isOneLiner(statement) || - leadingCommentsInBlock.length > 0; - } else { - expected = !isOneLiner(body); - } - } else { - // default "all" - expected = true; - } - - return { - actual: hasBlock, - expected, - check() { - if ( - this.expected !== null && - this.expected !== this.actual - ) { - if (this.expected) { - context.report({ - node, - loc: body.loc, - messageId: - opts && opts.condition - ? "missingCurlyAfterCondition" - : "missingCurlyAfter", - data: { - name, - }, - fix: fixer => - fixer.replaceText( - body, - `{${sourceCode.getText(body)}}`, - ), - }); - } else { - context.report({ - node, - loc: body.loc, - messageId: - opts && opts.condition - ? "unexpectedCurlyAfterCondition" - : "unexpectedCurlyAfter", - data: { - name, - }, - fix(fixer) { - /* - * `do while` expressions sometimes need a space to be inserted after `do`. - * e.g. `do{foo()} while (bar)` should be corrected to `do foo() while (bar)` - */ - const needsPrecedingSpace = - node.type === "DoWhileStatement" && - sourceCode.getTokenBefore(body) - .range[1] === body.range[0] && - !astUtils.canTokensBeAdjacent( - "do", - sourceCode.getFirstToken(body, { - skip: 1, - }), - ); - - const openingBracket = - sourceCode.getFirstToken(body); - const closingBracket = - sourceCode.getLastToken(body); - const lastTokenInBlock = - sourceCode.getTokenBefore( - closingBracket, - ); - - if (needsSemicolon(closingBracket)) { - /* - * If removing braces would cause a SyntaxError due to multiple statements on the same line (or - * change the semantics of the code due to ASI), don't perform a fix. - */ - return null; - } - - const resultingBodyText = - sourceCode - .getText() - .slice( - openingBracket.range[1], - lastTokenInBlock.range[0], - ) + - sourceCode.getText(lastTokenInBlock) + - sourceCode - .getText() - .slice( - lastTokenInBlock.range[1], - closingBracket.range[0], - ); - - return fixer.replaceText( - body, - (needsPrecedingSpace ? " " : "") + - resultingBodyText, - ); - }, - }); - } - } - }, - }; - } - - /** - * Prepares to check the bodies of a "if", "else if" and "else" chain. - * @param {ASTNode} node The first IfStatement node of the chain. - * @returns {Object[]} prepared checks for each body of the chain. See `prepareCheck` for more - * information. - */ - function prepareIfChecks(node) { - const preparedChecks = []; - - for ( - let currentNode = node; - currentNode; - currentNode = currentNode.alternate - ) { - preparedChecks.push( - prepareCheck(currentNode, currentNode.consequent, "if", { - condition: true, - }), - ); - if ( - currentNode.alternate && - currentNode.alternate.type !== "IfStatement" - ) { - preparedChecks.push( - prepareCheck( - currentNode, - currentNode.alternate, - "else", - ), - ); - break; - } - } - - if (consistent) { - /* - * If any node should have or already have braces, make sure they - * all have braces. - * If all nodes shouldn't have braces, make sure they don't. - */ - const expected = preparedChecks.some(preparedCheck => { - if (preparedCheck.expected !== null) { - return preparedCheck.expected; - } - return preparedCheck.actual; - }); - - preparedChecks.forEach(preparedCheck => { - preparedCheck.expected = expected; - }); - } - - return preparedChecks; - } - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - return { - IfStatement(node) { - const parent = node.parent; - const isElseIf = - parent.type === "IfStatement" && parent.alternate === node; - - if (!isElseIf) { - // This is a top `if`, check the whole `if-else-if` chain - prepareIfChecks(node).forEach(preparedCheck => { - preparedCheck.check(); - }); - } - - // Skip `else if`, it's already checked (when the top `if` was visited) - }, - - WhileStatement(node) { - prepareCheck(node, node.body, "while", { - condition: true, - }).check(); - }, - - DoWhileStatement(node) { - prepareCheck(node, node.body, "do").check(); - }, - - ForStatement(node) { - prepareCheck(node, node.body, "for", { - condition: true, - }).check(); - }, - - ForInStatement(node) { - prepareCheck(node, node.body, "for-in").check(); - }, - - ForOfStatement(node) { - prepareCheck(node, node.body, "for-of").check(); - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Enforce consistent brace style for all control statements", + recommended: false, + url: "https://eslint.org/docs/latest/rules/curly" + }, + + schema: { + anyOf: [ + { + type: "array", + items: [ + { + enum: ["all"] + } + ], + minItems: 0, + maxItems: 1 + }, + { + type: "array", + items: [ + { + enum: ["multi", "multi-line", "multi-or-nest"] + }, + { + enum: ["consistent"] + } + ], + minItems: 0, + maxItems: 2 + } + ] + }, + + fixable: "code", + + messages: { + missingCurlyAfter: "Expected { after '{{name}}'.", + missingCurlyAfterCondition: "Expected { after '{{name}}' condition.", + unexpectedCurlyAfter: "Unnecessary { after '{{name}}'.", + unexpectedCurlyAfterCondition: "Unnecessary { after '{{name}}' condition." + } + }, + + create(context) { + + const multiOnly = (context.options[0] === "multi"); + const multiLine = (context.options[0] === "multi-line"); + const multiOrNest = (context.options[0] === "multi-or-nest"); + const consistent = (context.options[1] === "consistent"); + + const sourceCode = context.sourceCode; + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + /** + * Determines if a given node is a one-liner that's on the same line as it's preceding code. + * @param {ASTNode} node The node to check. + * @returns {boolean} True if the node is a one-liner that's on the same line as it's preceding code. + * @private + */ + function isCollapsedOneLiner(node) { + const before = sourceCode.getTokenBefore(node); + const last = sourceCode.getLastToken(node); + const lastExcludingSemicolon = astUtils.isSemicolonToken(last) ? sourceCode.getTokenBefore(last) : last; + + return before.loc.start.line === lastExcludingSemicolon.loc.end.line; + } + + /** + * Determines if a given node is a one-liner. + * @param {ASTNode} node The node to check. + * @returns {boolean} True if the node is a one-liner. + * @private + */ + function isOneLiner(node) { + if (node.type === "EmptyStatement") { + return true; + } + + const first = sourceCode.getFirstToken(node); + const last = sourceCode.getLastToken(node); + const lastExcludingSemicolon = astUtils.isSemicolonToken(last) ? sourceCode.getTokenBefore(last) : last; + + return first.loc.start.line === lastExcludingSemicolon.loc.end.line; + } + + /** + * Determines if the given node is a lexical declaration (let, const, function, or class) + * @param {ASTNode} node The node to check + * @returns {boolean} True if the node is a lexical declaration + * @private + */ + function isLexicalDeclaration(node) { + if (node.type === "VariableDeclaration") { + return node.kind === "const" || node.kind === "let"; + } + + return node.type === "FunctionDeclaration" || node.type === "ClassDeclaration"; + } + + /** + * Checks if the given token is an `else` token or not. + * @param {Token} token The token to check. + * @returns {boolean} `true` if the token is an `else` token. + */ + function isElseKeywordToken(token) { + return token.value === "else" && token.type === "Keyword"; + } + + /** + * Determines whether the given node has an `else` keyword token as the first token after. + * @param {ASTNode} node The node to check. + * @returns {boolean} `true` if the node is followed by an `else` keyword token. + */ + function isFollowedByElseKeyword(node) { + const nextToken = sourceCode.getTokenAfter(node); + + return Boolean(nextToken) && isElseKeywordToken(nextToken); + } + + /** + * Determines if a semicolon needs to be inserted after removing a set of curly brackets, in order to avoid a SyntaxError. + * @param {Token} closingBracket The } token + * @returns {boolean} `true` if a semicolon needs to be inserted after the last statement in the block. + */ + function needsSemicolon(closingBracket) { + const tokenBefore = sourceCode.getTokenBefore(closingBracket); + const tokenAfter = sourceCode.getTokenAfter(closingBracket); + const lastBlockNode = sourceCode.getNodeByRangeIndex(tokenBefore.range[0]); + + if (astUtils.isSemicolonToken(tokenBefore)) { + + // If the last statement already has a semicolon, don't add another one. + return false; + } + + if (!tokenAfter) { + + // If there are no statements after this block, there is no need to add a semicolon. + return false; + } + + if (lastBlockNode.type === "BlockStatement" && lastBlockNode.parent.type !== "FunctionExpression" && lastBlockNode.parent.type !== "ArrowFunctionExpression") { + + /* + * If the last node surrounded by curly brackets is a BlockStatement (other than a FunctionExpression or an ArrowFunctionExpression), + * don't insert a semicolon. Otherwise, the semicolon would be parsed as a separate statement, which would cause + * a SyntaxError if it was followed by `else`. + */ + return false; + } + + if (tokenBefore.loc.end.line === tokenAfter.loc.start.line) { + + // If the next token is on the same line, insert a semicolon. + return true; + } + + if (/^[([/`+-]/u.test(tokenAfter.value)) { + + // If the next token starts with a character that would disrupt ASI, insert a semicolon. + return true; + } + + if (tokenBefore.type === "Punctuator" && (tokenBefore.value === "++" || tokenBefore.value === "--")) { + + // If the last token is ++ or --, insert a semicolon to avoid disrupting ASI. + return true; + } + + // Otherwise, do not insert a semicolon. + return false; + } + + /** + * Determines whether the code represented by the given node contains an `if` statement + * that would become associated with an `else` keyword directly appended to that code. + * + * Examples where it returns `true`: + * + * if (a) + * foo(); + * + * if (a) { + * foo(); + * } + * + * if (a) + * foo(); + * else if (b) + * bar(); + * + * while (a) + * if (b) + * if(c) + * foo(); + * else + * bar(); + * + * Examples where it returns `false`: + * + * if (a) + * foo(); + * else + * bar(); + * + * while (a) { + * if (b) + * if(c) + * foo(); + * else + * bar(); + * } + * + * while (a) + * if (b) { + * if(c) + * foo(); + * } + * else + * bar(); + * @param {ASTNode} node Node representing the code to check. + * @returns {boolean} `true` if an `if` statement within the code would become associated with an `else` appended to that code. + */ + function hasUnsafeIf(node) { + switch (node.type) { + case "IfStatement": + if (!node.alternate) { + return true; + } + return hasUnsafeIf(node.alternate); + case "ForStatement": + case "ForInStatement": + case "ForOfStatement": + case "LabeledStatement": + case "WithStatement": + case "WhileStatement": + return hasUnsafeIf(node.body); + default: + return false; + } + } + + /** + * Determines whether the existing curly braces around the single statement are necessary to preserve the semantics of the code. + * The braces, which make the given block body, are necessary in either of the following situations: + * + * 1. The statement is a lexical declaration. + * 2. Without the braces, an `if` within the statement would become associated with an `else` after the closing brace: + * + * if (a) { + * if (b) + * foo(); + * } + * else + * bar(); + * + * if (a) + * while (b) + * while (c) { + * while (d) + * if (e) + * while(f) + * foo(); + * } + * else + * bar(); + * @param {ASTNode} node `BlockStatement` body with exactly one statement directly inside. The statement can have its own nested statements. + * @returns {boolean} `true` if the braces are necessary - removing them (replacing the given `BlockStatement` body with its single statement content) + * would change the semantics of the code or produce a syntax error. + */ + function areBracesNecessary(node) { + const statement = node.body[0]; + + return isLexicalDeclaration(statement) || + hasUnsafeIf(statement) && isFollowedByElseKeyword(node); + } + + /** + * Prepares to check the body of a node to see if it's a block statement. + * @param {ASTNode} node The node to report if there's a problem. + * @param {ASTNode} body The body node to check for blocks. + * @param {string} name The name to report if there's a problem. + * @param {{ condition: boolean }} opts Options to pass to the report functions + * @returns {Object} a prepared check object, with "actual", "expected", "check" properties. + * "actual" will be `true` or `false` whether the body is already a block statement. + * "expected" will be `true` or `false` if the body should be a block statement or not, or + * `null` if it doesn't matter, depending on the rule options. It can be modified to change + * the final behavior of "check". + * "check" will be a function reporting appropriate problems depending on the other + * properties. + */ + function prepareCheck(node, body, name, opts) { + const hasBlock = (body.type === "BlockStatement"); + let expected = null; + + if (hasBlock && (body.body.length !== 1 || areBracesNecessary(body))) { + expected = true; + } else if (multiOnly) { + expected = false; + } else if (multiLine) { + if (!isCollapsedOneLiner(body)) { + expected = true; + } + + // otherwise, the body is allowed to have braces or not to have braces + + } else if (multiOrNest) { + if (hasBlock) { + const statement = body.body[0]; + const leadingCommentsInBlock = sourceCode.getCommentsBefore(statement); + + expected = !isOneLiner(statement) || leadingCommentsInBlock.length > 0; + } else { + expected = !isOneLiner(body); + } + } else { + + // default "all" + expected = true; + } + + return { + actual: hasBlock, + expected, + check() { + if (this.expected !== null && this.expected !== this.actual) { + if (this.expected) { + context.report({ + node, + loc: body.loc, + messageId: opts && opts.condition ? "missingCurlyAfterCondition" : "missingCurlyAfter", + data: { + name + }, + fix: fixer => fixer.replaceText(body, `{${sourceCode.getText(body)}}`) + }); + } else { + context.report({ + node, + loc: body.loc, + messageId: opts && opts.condition ? "unexpectedCurlyAfterCondition" : "unexpectedCurlyAfter", + data: { + name + }, + fix(fixer) { + + /* + * `do while` expressions sometimes need a space to be inserted after `do`. + * e.g. `do{foo()} while (bar)` should be corrected to `do foo() while (bar)` + */ + const needsPrecedingSpace = node.type === "DoWhileStatement" && + sourceCode.getTokenBefore(body).range[1] === body.range[0] && + !astUtils.canTokensBeAdjacent("do", sourceCode.getFirstToken(body, { skip: 1 })); + + const openingBracket = sourceCode.getFirstToken(body); + const closingBracket = sourceCode.getLastToken(body); + const lastTokenInBlock = sourceCode.getTokenBefore(closingBracket); + + if (needsSemicolon(closingBracket)) { + + /* + * If removing braces would cause a SyntaxError due to multiple statements on the same line (or + * change the semantics of the code due to ASI), don't perform a fix. + */ + return null; + } + + const resultingBodyText = sourceCode.getText().slice(openingBracket.range[1], lastTokenInBlock.range[0]) + + sourceCode.getText(lastTokenInBlock) + + sourceCode.getText().slice(lastTokenInBlock.range[1], closingBracket.range[0]); + + return fixer.replaceText(body, (needsPrecedingSpace ? " " : "") + resultingBodyText); + } + }); + } + } + } + }; + } + + /** + * Prepares to check the bodies of a "if", "else if" and "else" chain. + * @param {ASTNode} node The first IfStatement node of the chain. + * @returns {Object[]} prepared checks for each body of the chain. See `prepareCheck` for more + * information. + */ + function prepareIfChecks(node) { + const preparedChecks = []; + + for (let currentNode = node; currentNode; currentNode = currentNode.alternate) { + preparedChecks.push(prepareCheck(currentNode, currentNode.consequent, "if", { condition: true })); + if (currentNode.alternate && currentNode.alternate.type !== "IfStatement") { + preparedChecks.push(prepareCheck(currentNode, currentNode.alternate, "else")); + break; + } + } + + if (consistent) { + + /* + * If any node should have or already have braces, make sure they + * all have braces. + * If all nodes shouldn't have braces, make sure they don't. + */ + const expected = preparedChecks.some(preparedCheck => { + if (preparedCheck.expected !== null) { + return preparedCheck.expected; + } + return preparedCheck.actual; + }); + + preparedChecks.forEach(preparedCheck => { + preparedCheck.expected = expected; + }); + } + + return preparedChecks; + } + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + return { + IfStatement(node) { + const parent = node.parent; + const isElseIf = parent.type === "IfStatement" && parent.alternate === node; + + if (!isElseIf) { + + // This is a top `if`, check the whole `if-else-if` chain + prepareIfChecks(node).forEach(preparedCheck => { + preparedCheck.check(); + }); + } + + // Skip `else if`, it's already checked (when the top `if` was visited) + }, + + WhileStatement(node) { + prepareCheck(node, node.body, "while", { condition: true }).check(); + }, + + DoWhileStatement(node) { + prepareCheck(node, node.body, "do").check(); + }, + + ForStatement(node) { + prepareCheck(node, node.body, "for", { condition: true }).check(); + }, + + ForInStatement(node) { + prepareCheck(node, node.body, "for-in").check(); + }, + + ForOfStatement(node) { + prepareCheck(node, node.body, "for-of").check(); + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/default-case-last.js b/node_modules/eslint/lib/rules/default-case-last.js index 1c6170b48..d4a83b5fd 100644 --- a/node_modules/eslint/lib/rules/default-case-last.js +++ b/node_modules/eslint/lib/rules/default-case-last.js @@ -1,5 +1,5 @@ /** - * @fileoverview Rule to enforce `default` clauses in `switch` statements to be last + * @fileoverview Rule to enforce default clauses in switch statements to be last * @author Milos Djermanovic */ @@ -9,43 +9,36 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: - "Enforce `default` clauses in `switch` statements to be last", - recommended: false, - url: "https://eslint.org/docs/latest/rules/default-case-last", - }, - - schema: [], - - messages: { - notLast: "Default clause should be the last clause.", - }, - }, - - create(context) { - return { - SwitchStatement(node) { - const cases = node.cases, - indexOfDefault = cases.findIndex(c => c.test === null); - - if ( - indexOfDefault !== -1 && - indexOfDefault !== cases.length - 1 - ) { - const defaultClause = cases[indexOfDefault]; - - context.report({ - node: defaultClause, - messageId: "notLast", - }); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Enforce default clauses in switch statements to be last", + recommended: false, + url: "https://eslint.org/docs/latest/rules/default-case-last" + }, + + schema: [], + + messages: { + notLast: "Default clause should be the last clause." + } + }, + + create(context) { + return { + SwitchStatement(node) { + const cases = node.cases, + indexOfDefault = cases.findIndex(c => c.test === null); + + if (indexOfDefault !== -1 && indexOfDefault !== cases.length - 1) { + const defaultClause = cases[indexOfDefault]; + + context.report({ node: defaultClause, messageId: "notLast" }); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/default-case.js b/node_modules/eslint/lib/rules/default-case.js index 23e2f6efa..4f2fad0c4 100644 --- a/node_modules/eslint/lib/rules/default-case.js +++ b/node_modules/eslint/lib/rules/default-case.js @@ -10,94 +10,88 @@ const DEFAULT_COMMENT_PATTERN = /^no default$/iu; // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - defaultOptions: [{}], - - docs: { - description: "Require `default` cases in `switch` statements", - recommended: false, - url: "https://eslint.org/docs/latest/rules/default-case", - }, - - schema: [ - { - type: "object", - properties: { - commentPattern: { - type: "string", - }, - }, - additionalProperties: false, - }, - ], - - messages: { - missingDefaultCase: "Expected a default case.", - }, - }, - - create(context) { - const [options] = context.options; - const commentPattern = options.commentPattern - ? new RegExp(options.commentPattern, "u") - : DEFAULT_COMMENT_PATTERN; - - const sourceCode = context.sourceCode; - - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - /** - * Shortcut to get last element of array - * @param {*[]} collection Array - * @returns {any} Last element - */ - function last(collection) { - return collection.at(-1); - } - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - return { - SwitchStatement(node) { - if (!node.cases.length) { - /* - * skip check of empty switch because there is no easy way - * to extract comments inside it now - */ - return; - } - - const hasDefault = node.cases.some(v => v.test === null); - - if (!hasDefault) { - let comment; - - const lastCase = last(node.cases); - const comments = sourceCode.getCommentsAfter(lastCase); - - if (comments.length) { - comment = last(comments); - } - - if ( - !comment || - !commentPattern.test(comment.value.trim()) - ) { - context.report({ - node, - messageId: "missingDefaultCase", - }); - } - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Require `default` cases in `switch` statements", + recommended: false, + url: "https://eslint.org/docs/latest/rules/default-case" + }, + + schema: [{ + type: "object", + properties: { + commentPattern: { + type: "string" + } + }, + additionalProperties: false + }], + + messages: { + missingDefaultCase: "Expected a default case." + } + }, + + create(context) { + const options = context.options[0] || {}; + const commentPattern = options.commentPattern + ? new RegExp(options.commentPattern, "u") + : DEFAULT_COMMENT_PATTERN; + + const sourceCode = context.sourceCode; + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + /** + * Shortcut to get last element of array + * @param {*[]} collection Array + * @returns {any} Last element + */ + function last(collection) { + return collection[collection.length - 1]; + } + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + return { + + SwitchStatement(node) { + + if (!node.cases.length) { + + /* + * skip check of empty switch because there is no easy way + * to extract comments inside it now + */ + return; + } + + const hasDefault = node.cases.some(v => v.test === null); + + if (!hasDefault) { + + let comment; + + const lastCase = last(node.cases); + const comments = sourceCode.getCommentsAfter(lastCase); + + if (comments.length) { + comment = last(comments); + } + + if (!comment || !commentPattern.test(comment.value.trim())) { + context.report({ node, messageId: "missingDefaultCase" }); + } + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/default-param-last.js b/node_modules/eslint/lib/rules/default-param-last.js index 101b89ff8..3254fa802 100644 --- a/node_modules/eslint/lib/rules/default-param-last.js +++ b/node_modules/eslint/lib/rules/default-param-last.js @@ -5,74 +5,58 @@ "use strict"; -/** - * Checks if node is required: i.e. does not have a default value or ? optional indicator. - * @param {ASTNode} node the node to be evaluated - * @returns {boolean} true if the node is required, false if not. - */ -function isRequiredParameter(node) { - return !( - node.type === "AssignmentPattern" || - node.type === "RestElement" || - node.optional - ); -} - -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - dialects: ["javascript", "typescript"], - language: "javascript", - type: "suggestion", - - docs: { - description: "Enforce default parameters to be last", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/default-param-last", - }, - - schema: [], - - messages: { - shouldBeLast: "Default parameters should be last.", - }, - }, - - create(context) { - /** - * Handler for function contexts. - * @param {ASTNode} node function node - * @returns {void} - */ - function handleFunction(node) { - let hasSeenRequiredParameter = false; - - for (let i = node.params.length - 1; i >= 0; i -= 1) { - const current = node.params[i]; - const param = - current.type === "TSParameterProperty" - ? current.parameter - : current; - - if (isRequiredParameter(param)) { - hasSeenRequiredParameter = true; - continue; - } - - if (hasSeenRequiredParameter) { - context.report({ - node: current, - messageId: "shouldBeLast", - }); - } - } - } - - return { - FunctionDeclaration: handleFunction, - FunctionExpression: handleFunction, - ArrowFunctionExpression: handleFunction, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Enforce default parameters to be last", + recommended: false, + url: "https://eslint.org/docs/latest/rules/default-param-last" + }, + + schema: [], + + messages: { + shouldBeLast: "Default parameters should be last." + } + }, + + create(context) { + + /** + * Handler for function contexts. + * @param {ASTNode} node function node + * @returns {void} + */ + function handleFunction(node) { + let hasSeenPlainParam = false; + + for (let i = node.params.length - 1; i >= 0; i -= 1) { + const param = node.params[i]; + + if ( + param.type !== "AssignmentPattern" && + param.type !== "RestElement" + ) { + hasSeenPlainParam = true; + continue; + } + + if (hasSeenPlainParam && param.type === "AssignmentPattern") { + context.report({ + node: param, + messageId: "shouldBeLast" + }); + } + } + } + + return { + FunctionDeclaration: handleFunction, + FunctionExpression: handleFunction, + ArrowFunctionExpression: handleFunction + }; + } }; diff --git a/node_modules/eslint/lib/rules/dot-location.js b/node_modules/eslint/lib/rules/dot-location.js index 9025a1fe2..0d017c162 100644 --- a/node_modules/eslint/lib/rules/dot-location.js +++ b/node_modules/eslint/lib/rules/dot-location.js @@ -12,127 +12,97 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "dot-location", - url: "https://eslint.style/rules/dot-location", - }, - }, - ], - }, - type: "layout", - - docs: { - description: "Enforce consistent newlines before and after dots", - recommended: false, - url: "https://eslint.org/docs/latest/rules/dot-location", - }, - - schema: [ - { - enum: ["object", "property"], - }, - ], - - fixable: "code", - - messages: { - expectedDotAfterObject: - "Expected dot to be on same line as object.", - expectedDotBeforeProperty: - "Expected dot to be on same line as property.", - }, - }, - - create(context) { - const config = context.options[0]; - - // default to onObject if no preference is passed - const onObject = config === "object" || !config; - - const sourceCode = context.sourceCode; - - /** - * Reports if the dot between object and property is on the correct location. - * @param {ASTNode} node The `MemberExpression` node. - * @returns {void} - */ - function checkDotLocation(node) { - const property = node.property; - const dotToken = sourceCode.getTokenBefore(property); - - if (onObject) { - // `obj` expression can be parenthesized, but those paren tokens are not a part of the `obj` node. - const tokenBeforeDot = sourceCode.getTokenBefore(dotToken); - - if (!astUtils.isTokenOnSameLine(tokenBeforeDot, dotToken)) { - context.report({ - node, - loc: dotToken.loc, - messageId: "expectedDotAfterObject", - *fix(fixer) { - if ( - dotToken.value.startsWith(".") && - astUtils.isDecimalIntegerNumericToken( - tokenBeforeDot, - ) - ) { - yield fixer.insertTextAfter( - tokenBeforeDot, - ` ${dotToken.value}`, - ); - } else { - yield fixer.insertTextAfter( - tokenBeforeDot, - dotToken.value, - ); - } - yield fixer.remove(dotToken); - }, - }); - } - } else if (!astUtils.isTokenOnSameLine(dotToken, property)) { - context.report({ - node, - loc: dotToken.loc, - messageId: "expectedDotBeforeProperty", - *fix(fixer) { - yield fixer.remove(dotToken); - yield fixer.insertTextBefore(property, dotToken.value); - }, - }); - } - } - - /** - * Checks the spacing of the dot within a member expression. - * @param {ASTNode} node The node to check. - * @returns {void} - */ - function checkNode(node) { - if (!node.computed) { - checkDotLocation(node); - } - } - - return { - MemberExpression: checkNode, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Enforce consistent newlines before and after dots", + recommended: false, + url: "https://eslint.org/docs/latest/rules/dot-location" + }, + + schema: [ + { + enum: ["object", "property"] + } + ], + + fixable: "code", + + messages: { + expectedDotAfterObject: "Expected dot to be on same line as object.", + expectedDotBeforeProperty: "Expected dot to be on same line as property." + } + }, + + create(context) { + + const config = context.options[0]; + + // default to onObject if no preference is passed + const onObject = config === "object" || !config; + + const sourceCode = context.sourceCode; + + /** + * Reports if the dot between object and property is on the correct location. + * @param {ASTNode} node The `MemberExpression` node. + * @returns {void} + */ + function checkDotLocation(node) { + const property = node.property; + const dotToken = sourceCode.getTokenBefore(property); + + if (onObject) { + + // `obj` expression can be parenthesized, but those paren tokens are not a part of the `obj` node. + const tokenBeforeDot = sourceCode.getTokenBefore(dotToken); + + if (!astUtils.isTokenOnSameLine(tokenBeforeDot, dotToken)) { + context.report({ + node, + loc: dotToken.loc, + messageId: "expectedDotAfterObject", + *fix(fixer) { + if (dotToken.value.startsWith(".") && astUtils.isDecimalIntegerNumericToken(tokenBeforeDot)) { + yield fixer.insertTextAfter(tokenBeforeDot, ` ${dotToken.value}`); + } else { + yield fixer.insertTextAfter(tokenBeforeDot, dotToken.value); + } + yield fixer.remove(dotToken); + } + }); + } + } else if (!astUtils.isTokenOnSameLine(dotToken, property)) { + context.report({ + node, + loc: dotToken.loc, + messageId: "expectedDotBeforeProperty", + *fix(fixer) { + yield fixer.remove(dotToken); + yield fixer.insertTextBefore(property, dotToken.value); + } + }); + } + } + + /** + * Checks the spacing of the dot within a member expression. + * @param {ASTNode} node The node to check. + * @returns {void} + */ + function checkNode(node) { + if (!node.computed) { + checkDotLocation(node); + } + } + + return { + MemberExpression: checkNode + }; + } }; diff --git a/node_modules/eslint/lib/rules/dot-notation.js b/node_modules/eslint/lib/rules/dot-notation.js index d6e4d05ce..21cba54e2 100644 --- a/node_modules/eslint/lib/rules/dot-notation.js +++ b/node_modules/eslint/lib/rules/dot-notation.js @@ -15,202 +15,162 @@ const keywords = require("./utils/keywords"); // Rule Definition //------------------------------------------------------------------------------ -const validIdentifier = /^[a-zA-Z_$][\w$]*$/u; +const validIdentifier = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/u; // `null` literal must be handled separately. const literalTypesToCheck = new Set(["string", "boolean"]); -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - defaultOptions: [ - { - allowKeywords: true, - allowPattern: "", - }, - ], - - docs: { - description: "Enforce dot notation whenever possible", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/dot-notation", - }, - - schema: [ - { - type: "object", - properties: { - allowKeywords: { - type: "boolean", - }, - allowPattern: { - type: "string", - }, - }, - additionalProperties: false, - }, - ], - - fixable: "code", - - messages: { - useDot: "[{{key}}] is better written in dot notation.", - useBrackets: ".{{key}} is a syntax error.", - }, - }, - - create(context) { - const [options] = context.options; - const allowKeywords = options.allowKeywords; - const sourceCode = context.sourceCode; - - let allowPattern; - - if (options.allowPattern) { - allowPattern = new RegExp(options.allowPattern, "u"); - } - - /** - * Check if the property is valid dot notation - * @param {ASTNode} node The dot notation node - * @param {string} value Value which is to be checked - * @returns {void} - */ - function checkComputedProperty(node, value) { - if ( - validIdentifier.test(value) && - (allowKeywords || !keywords.includes(String(value))) && - !(allowPattern && allowPattern.test(value)) - ) { - const formattedValue = - node.property.type === "Literal" - ? JSON.stringify(value) - : `\`${value}\``; - - context.report({ - node: node.property, - messageId: "useDot", - data: { - key: formattedValue, - }, - *fix(fixer) { - const leftBracket = sourceCode.getTokenAfter( - node.object, - astUtils.isOpeningBracketToken, - ); - const rightBracket = sourceCode.getLastToken(node); - const nextToken = sourceCode.getTokenAfter(node); - - // Don't perform any fixes if there are comments inside the brackets. - if ( - sourceCode.commentsExistBetween( - leftBracket, - rightBracket, - ) - ) { - return; - } - - // Replace the brackets by an identifier. - if (!node.optional) { - yield fixer.insertTextBefore( - leftBracket, - astUtils.isDecimalInteger(node.object) - ? " ." - : ".", - ); - } - yield fixer.replaceTextRange( - [leftBracket.range[0], rightBracket.range[1]], - value, - ); - - // Insert a space after the property if it will be connected to the next token. - if ( - nextToken && - rightBracket.range[1] === nextToken.range[0] && - !astUtils.canTokensBeAdjacent( - String(value), - nextToken, - ) - ) { - yield fixer.insertTextAfter(node, " "); - } - }, - }); - } - } - - return { - MemberExpression(node) { - if ( - node.computed && - node.property.type === "Literal" && - (literalTypesToCheck.has(typeof node.property.value) || - astUtils.isNullLiteral(node.property)) - ) { - checkComputedProperty(node, node.property.value); - } - if ( - node.computed && - astUtils.isStaticTemplateLiteral(node.property) - ) { - checkComputedProperty( - node, - node.property.quasis[0].value.cooked, - ); - } - if ( - !allowKeywords && - !node.computed && - node.property.type === "Identifier" && - keywords.includes(String(node.property.name)) - ) { - context.report({ - node: node.property, - messageId: "useBrackets", - data: { - key: node.property.name, - }, - *fix(fixer) { - const dotToken = sourceCode.getTokenBefore( - node.property, - ); - - // A statement that starts with `let[` is parsed as a destructuring variable declaration, not a MemberExpression. - if ( - node.object.type === "Identifier" && - node.object.name === "let" && - !node.optional - ) { - return; - } - - // Don't perform any fixes if there are comments between the dot and the property name. - if ( - sourceCode.commentsExistBetween( - dotToken, - node.property, - ) - ) { - return; - } - - // Replace the identifier to brackets. - if (!node.optional) { - yield fixer.remove(dotToken); - } - yield fixer.replaceText( - node.property, - `["${node.property.name}"]`, - ); - }, - }); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Enforce dot notation whenever possible", + recommended: false, + url: "https://eslint.org/docs/latest/rules/dot-notation" + }, + + schema: [ + { + type: "object", + properties: { + allowKeywords: { + type: "boolean", + default: true + }, + allowPattern: { + type: "string", + default: "" + } + }, + additionalProperties: false + } + ], + + fixable: "code", + + messages: { + useDot: "[{{key}}] is better written in dot notation.", + useBrackets: ".{{key}} is a syntax error." + } + }, + + create(context) { + const options = context.options[0] || {}; + const allowKeywords = options.allowKeywords === void 0 || options.allowKeywords; + const sourceCode = context.sourceCode; + + let allowPattern; + + if (options.allowPattern) { + allowPattern = new RegExp(options.allowPattern, "u"); + } + + /** + * Check if the property is valid dot notation + * @param {ASTNode} node The dot notation node + * @param {string} value Value which is to be checked + * @returns {void} + */ + function checkComputedProperty(node, value) { + if ( + validIdentifier.test(value) && + (allowKeywords || !keywords.includes(String(value))) && + !(allowPattern && allowPattern.test(value)) + ) { + const formattedValue = node.property.type === "Literal" ? JSON.stringify(value) : `\`${value}\``; + + context.report({ + node: node.property, + messageId: "useDot", + data: { + key: formattedValue + }, + *fix(fixer) { + const leftBracket = sourceCode.getTokenAfter(node.object, astUtils.isOpeningBracketToken); + const rightBracket = sourceCode.getLastToken(node); + const nextToken = sourceCode.getTokenAfter(node); + + // Don't perform any fixes if there are comments inside the brackets. + if (sourceCode.commentsExistBetween(leftBracket, rightBracket)) { + return; + } + + // Replace the brackets by an identifier. + if (!node.optional) { + yield fixer.insertTextBefore( + leftBracket, + astUtils.isDecimalInteger(node.object) ? " ." : "." + ); + } + yield fixer.replaceTextRange( + [leftBracket.range[0], rightBracket.range[1]], + value + ); + + // Insert a space after the property if it will be connected to the next token. + if ( + nextToken && + rightBracket.range[1] === nextToken.range[0] && + !astUtils.canTokensBeAdjacent(String(value), nextToken) + ) { + yield fixer.insertTextAfter(node, " "); + } + } + }); + } + } + + return { + MemberExpression(node) { + if ( + node.computed && + node.property.type === "Literal" && + (literalTypesToCheck.has(typeof node.property.value) || astUtils.isNullLiteral(node.property)) + ) { + checkComputedProperty(node, node.property.value); + } + if ( + node.computed && + astUtils.isStaticTemplateLiteral(node.property) + ) { + checkComputedProperty(node, node.property.quasis[0].value.cooked); + } + if ( + !allowKeywords && + !node.computed && + node.property.type === "Identifier" && + keywords.includes(String(node.property.name)) + ) { + context.report({ + node: node.property, + messageId: "useBrackets", + data: { + key: node.property.name + }, + *fix(fixer) { + const dotToken = sourceCode.getTokenBefore(node.property); + + // A statement that starts with `let[` is parsed as a destructuring variable declaration, not a MemberExpression. + if (node.object.type === "Identifier" && node.object.name === "let" && !node.optional) { + return; + } + + // Don't perform any fixes if there are comments between the dot and the property name. + if (sourceCode.commentsExistBetween(dotToken, node.property)) { + return; + } + + // Replace the identifier to brackets. + if (!node.optional) { + yield fixer.remove(dotToken); + } + yield fixer.replaceText(node.property, `["${node.property.name}"]`); + } + }); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/eol-last.js b/node_modules/eslint/lib/rules/eol-last.js index 494378468..03487b039 100644 --- a/node_modules/eslint/lib/rules/eol-last.js +++ b/node_modules/eslint/lib/rules/eol-last.js @@ -9,127 +9,107 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "eol-last", - url: "https://eslint.style/rules/eol-last", - }, - }, - ], - }, - type: "layout", - - docs: { - description: "Require or disallow newline at the end of files", - recommended: false, - url: "https://eslint.org/docs/latest/rules/eol-last", - }, - - fixable: "whitespace", - - schema: [ - { - enum: ["always", "never", "unix", "windows"], - }, - ], - - messages: { - missing: "Newline required at end of file but not found.", - unexpected: "Newline not allowed at end of file.", - }, - }, - create(context) { - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - return { - Program: function checkBadEOF(node) { - const sourceCode = context.sourceCode, - src = sourceCode.getText(), - lastLine = sourceCode.lines.at(-1), - location = { - column: lastLine.length, - line: sourceCode.lines.length, - }, - LF = "\n", - CRLF = `\r${LF}`, - endsWithNewline = src.endsWith(LF); - - /* - * Empty source is always valid: No content in file so we don't - * need to lint for a newline on the last line of content. - */ - if (!src.length) { - return; - } - - let mode = context.options[0] || "always", - appendCRLF = false; - - if (mode === "unix") { - // `"unix"` should behave exactly as `"always"` - mode = "always"; - } - if (mode === "windows") { - // `"windows"` should behave exactly as `"always"`, but append CRLF in the fixer for backwards compatibility - mode = "always"; - appendCRLF = true; - } - if (mode === "always" && !endsWithNewline) { - // File is not newline-terminated, but should be - context.report({ - node, - loc: location, - messageId: "missing", - fix(fixer) { - return fixer.insertTextAfterRange( - [0, src.length], - appendCRLF ? CRLF : LF, - ); - }, - }); - } else if (mode === "never" && endsWithNewline) { - const secondLastLine = sourceCode.lines.at(-2); - - // File is newline-terminated, but shouldn't be - context.report({ - node, - loc: { - start: { - line: sourceCode.lines.length - 1, - column: secondLastLine.length, - }, - end: { line: sourceCode.lines.length, column: 0 }, - }, - messageId: "unexpected", - fix(fixer) { - const finalEOLs = /(?:\r?\n)+$/u, - match = finalEOLs.exec(sourceCode.text), - start = match.index, - end = sourceCode.text.length; - - return fixer.replaceTextRange([start, end], ""); - }, - }); - } - }, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Require or disallow newline at the end of files", + recommended: false, + url: "https://eslint.org/docs/latest/rules/eol-last" + }, + + fixable: "whitespace", + + schema: [ + { + enum: ["always", "never", "unix", "windows"] + } + ], + + messages: { + missing: "Newline required at end of file but not found.", + unexpected: "Newline not allowed at end of file." + } + }, + create(context) { + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + return { + Program: function checkBadEOF(node) { + const sourceCode = context.sourceCode, + src = sourceCode.getText(), + lastLine = sourceCode.lines[sourceCode.lines.length - 1], + location = { + column: lastLine.length, + line: sourceCode.lines.length + }, + LF = "\n", + CRLF = `\r${LF}`, + endsWithNewline = src.endsWith(LF); + + /* + * Empty source is always valid: No content in file so we don't + * need to lint for a newline on the last line of content. + */ + if (!src.length) { + return; + } + + let mode = context.options[0] || "always", + appendCRLF = false; + + if (mode === "unix") { + + // `"unix"` should behave exactly as `"always"` + mode = "always"; + } + if (mode === "windows") { + + // `"windows"` should behave exactly as `"always"`, but append CRLF in the fixer for backwards compatibility + mode = "always"; + appendCRLF = true; + } + if (mode === "always" && !endsWithNewline) { + + // File is not newline-terminated, but should be + context.report({ + node, + loc: location, + messageId: "missing", + fix(fixer) { + return fixer.insertTextAfterRange([0, src.length], appendCRLF ? CRLF : LF); + } + }); + } else if (mode === "never" && endsWithNewline) { + + const secondLastLine = sourceCode.lines[sourceCode.lines.length - 2]; + + // File is newline-terminated, but shouldn't be + context.report({ + node, + loc: { + start: { line: sourceCode.lines.length - 1, column: secondLastLine.length }, + end: { line: sourceCode.lines.length, column: 0 } + }, + messageId: "unexpected", + fix(fixer) { + const finalEOLs = /(?:\r?\n)+$/u, + match = finalEOLs.exec(sourceCode.text), + start = match.index, + end = sourceCode.text.length; + + return fixer.replaceTextRange([start, end], ""); + } + }); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/eqeqeq.js b/node_modules/eslint/lib/rules/eqeqeq.js index cfe5bd4cf..12b1e805e 100644 --- a/node_modules/eslint/lib/rules/eqeqeq.js +++ b/node_modules/eslint/lib/rules/eqeqeq.js @@ -15,196 +15,160 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - hasSuggestions: true, - - docs: { - description: "Require the use of `===` and `!==`", - recommended: false, - url: "https://eslint.org/docs/latest/rules/eqeqeq", - }, - - schema: { - anyOf: [ - { - type: "array", - items: [ - { - enum: ["always"], - }, - { - type: "object", - properties: { - null: { - enum: ["always", "never", "ignore"], - }, - }, - additionalProperties: false, - }, - ], - additionalItems: false, - }, - { - type: "array", - items: [ - { - enum: ["smart", "allow-null"], - }, - ], - additionalItems: false, - }, - ], - }, - - fixable: "code", - - messages: { - unexpected: - "Expected '{{expectedOperator}}' and instead saw '{{actualOperator}}'.", - replaceOperator: - "Use '{{expectedOperator}}' instead of '{{actualOperator}}'.", - }, - }, - - create(context) { - const config = context.options[0] || "always"; - const options = context.options[1] || {}; - const sourceCode = context.sourceCode; - - const nullOption = - config === "always" ? options.null || "always" : "ignore"; - const enforceRuleForNull = nullOption === "always"; - const enforceInverseRuleForNull = nullOption === "never"; - - /** - * Checks if an expression is a typeof expression - * @param {ASTNode} node The node to check - * @returns {boolean} if the node is a typeof expression - */ - function isTypeOf(node) { - return ( - node.type === "UnaryExpression" && node.operator === "typeof" - ); - } - - /** - * Checks if either operand of a binary expression is a typeof operation - * @param {ASTNode} node The node to check - * @returns {boolean} if one of the operands is typeof - * @private - */ - function isTypeOfBinary(node) { - return isTypeOf(node.left) || isTypeOf(node.right); - } - - /** - * Checks if operands are literals of the same type (via typeof) - * @param {ASTNode} node The node to check - * @returns {boolean} if operands are of same type - * @private - */ - function areLiteralsAndSameType(node) { - return ( - node.left.type === "Literal" && - node.right.type === "Literal" && - typeof node.left.value === typeof node.right.value - ); - } - - /** - * Checks if one of the operands is a literal null - * @param {ASTNode} node The node to check - * @returns {boolean} if operands are null - * @private - */ - function isNullCheck(node) { - return ( - astUtils.isNullLiteral(node.right) || - astUtils.isNullLiteral(node.left) - ); - } - - /** - * Reports a message for this rule. - * @param {ASTNode} node The binary expression node that was checked - * @param {string} expectedOperator The operator that was expected (either '==', '!=', '===', or '!==') - * @returns {void} - * @private - */ - function report(node, expectedOperator) { - const operatorToken = sourceCode.getFirstTokenBetween( - node.left, - node.right, - token => token.value === node.operator, - ); - - const commonReportParams = { - node, - loc: operatorToken.loc, - messageId: "unexpected", - data: { expectedOperator, actualOperator: node.operator }, - }; - - if (isTypeOfBinary(node) || areLiteralsAndSameType(node)) { - context.report({ - ...commonReportParams, - fix(fixer) { - return fixer.replaceText( - operatorToken, - expectedOperator, - ); - }, - }); - } else { - context.report({ - ...commonReportParams, - suggest: [ - { - messageId: "replaceOperator", - data: { - expectedOperator, - actualOperator: node.operator, - }, - fix: fixer => - fixer.replaceText( - operatorToken, - expectedOperator, - ), - }, - ], - }); - } - } - - return { - BinaryExpression(node) { - const isNull = isNullCheck(node); - - if (node.operator !== "==" && node.operator !== "!=") { - if (enforceInverseRuleForNull && isNull) { - report(node, node.operator.slice(0, -1)); - } - return; - } - - if ( - config === "smart" && - (isTypeOfBinary(node) || - areLiteralsAndSameType(node) || - isNull) - ) { - return; - } - - if (!enforceRuleForNull && isNull) { - return; - } - - report(node, `${node.operator}=`); - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Require the use of `===` and `!==`", + recommended: false, + url: "https://eslint.org/docs/latest/rules/eqeqeq" + }, + + schema: { + anyOf: [ + { + type: "array", + items: [ + { + enum: ["always"] + }, + { + type: "object", + properties: { + null: { + enum: ["always", "never", "ignore"] + } + }, + additionalProperties: false + } + ], + additionalItems: false + }, + { + type: "array", + items: [ + { + enum: ["smart", "allow-null"] + } + ], + additionalItems: false + } + ] + }, + + fixable: "code", + + messages: { + unexpected: "Expected '{{expectedOperator}}' and instead saw '{{actualOperator}}'." + } + }, + + create(context) { + const config = context.options[0] || "always"; + const options = context.options[1] || {}; + const sourceCode = context.sourceCode; + + const nullOption = (config === "always") + ? options.null || "always" + : "ignore"; + const enforceRuleForNull = (nullOption === "always"); + const enforceInverseRuleForNull = (nullOption === "never"); + + /** + * Checks if an expression is a typeof expression + * @param {ASTNode} node The node to check + * @returns {boolean} if the node is a typeof expression + */ + function isTypeOf(node) { + return node.type === "UnaryExpression" && node.operator === "typeof"; + } + + /** + * Checks if either operand of a binary expression is a typeof operation + * @param {ASTNode} node The node to check + * @returns {boolean} if one of the operands is typeof + * @private + */ + function isTypeOfBinary(node) { + return isTypeOf(node.left) || isTypeOf(node.right); + } + + /** + * Checks if operands are literals of the same type (via typeof) + * @param {ASTNode} node The node to check + * @returns {boolean} if operands are of same type + * @private + */ + function areLiteralsAndSameType(node) { + return node.left.type === "Literal" && node.right.type === "Literal" && + typeof node.left.value === typeof node.right.value; + } + + /** + * Checks if one of the operands is a literal null + * @param {ASTNode} node The node to check + * @returns {boolean} if operands are null + * @private + */ + function isNullCheck(node) { + return astUtils.isNullLiteral(node.right) || astUtils.isNullLiteral(node.left); + } + + /** + * Reports a message for this rule. + * @param {ASTNode} node The binary expression node that was checked + * @param {string} expectedOperator The operator that was expected (either '==', '!=', '===', or '!==') + * @returns {void} + * @private + */ + function report(node, expectedOperator) { + const operatorToken = sourceCode.getFirstTokenBetween( + node.left, + node.right, + token => token.value === node.operator + ); + + context.report({ + node, + loc: operatorToken.loc, + messageId: "unexpected", + data: { expectedOperator, actualOperator: node.operator }, + fix(fixer) { + + // If the comparison is a `typeof` comparison or both sides are literals with the same type, then it's safe to fix. + if (isTypeOfBinary(node) || areLiteralsAndSameType(node)) { + return fixer.replaceText(operatorToken, expectedOperator); + } + return null; + } + }); + } + + return { + BinaryExpression(node) { + const isNull = isNullCheck(node); + + if (node.operator !== "==" && node.operator !== "!=") { + if (enforceInverseRuleForNull && isNull) { + report(node, node.operator.slice(0, -1)); + } + return; + } + + if (config === "smart" && (isTypeOfBinary(node) || + areLiteralsAndSameType(node) || isNull)) { + return; + } + + if (!enforceRuleForNull && isNull) { + return; + } + + report(node, `${node.operator}=`); + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/for-direction.js b/node_modules/eslint/lib/rules/for-direction.js index 7cf14b996..69198d11a 100644 --- a/node_modules/eslint/lib/rules/for-direction.js +++ b/node_modules/eslint/lib/rules/for-direction.js @@ -1,5 +1,5 @@ /** - * @fileoverview enforce `for` loop update clause moving the counter in the right direction.(for-direction) + * @fileoverview enforce "for" loop update clause moving the counter in the right direction.(for-direction) * @author Aladdin-ADD */ @@ -15,154 +15,126 @@ const { getStaticValue } = require("@eslint-community/eslint-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - docs: { - description: - "Enforce `for` loop update clause moving the counter in the right direction", - recommended: true, - url: "https://eslint.org/docs/latest/rules/for-direction", - }, - - fixable: null, - schema: [], - - messages: { - incorrectDirection: - "The update clause in this loop moves the variable in the wrong direction.", - }, - }, - - create(context) { - const { sourceCode } = context; - - /** - * report an error. - * @param {ASTNode} node the node to report. - * @returns {void} - */ - function report(node) { - context.report({ - loc: { - start: node.loc.start, - end: sourceCode.getTokenBefore(node.body).loc.end, - }, - messageId: "incorrectDirection", - }); - } - - /** - * check the right side of the assignment - * @param {ASTNode} update UpdateExpression to check - * @param {number} dir expected direction that could either be turned around or invalidated - * @returns {number} return dir, the negated dir, or zero if the counter does not change or the direction is not clear - */ - function getRightDirection(update, dir) { - const staticValue = getStaticValue( - update.right, - sourceCode.getScope(update), - ); - - if ( - staticValue && - ["bigint", "boolean", "number"].includes( - typeof staticValue.value, - ) - ) { - const sign = Math.sign(Number(staticValue.value)) || 0; // convert NaN to 0 - - return dir * sign; - } - return 0; - } - - /** - * check UpdateExpression add/sub the counter - * @param {ASTNode} update UpdateExpression to check - * @param {string} counter variable name to check - * @returns {number} if add return 1, if sub return -1, if nochange, return 0 - */ - function getUpdateDirection(update, counter) { - if ( - update.argument.type === "Identifier" && - update.argument.name === counter - ) { - if (update.operator === "++") { - return 1; - } - if (update.operator === "--") { - return -1; - } - } - return 0; - } - - /** - * check AssignmentExpression add/sub the counter - * @param {ASTNode} update AssignmentExpression to check - * @param {string} counter variable name to check - * @returns {number} if add return 1, if sub return -1, if nochange, return 0 - */ - function getAssignmentDirection(update, counter) { - if (update.left.name === counter) { - if (update.operator === "+=") { - return getRightDirection(update, 1); - } - if (update.operator === "-=") { - return getRightDirection(update, -1); - } - } - return 0; - } - - return { - ForStatement(node) { - if ( - node.test && - node.test.type === "BinaryExpression" && - node.update - ) { - for (const counterPosition of ["left", "right"]) { - if (node.test[counterPosition].type !== "Identifier") { - continue; - } - - const counter = node.test[counterPosition].name; - const operator = node.test.operator; - const update = node.update; - - let wrongDirection; - - if (operator === "<" || operator === "<=") { - wrongDirection = - counterPosition === "left" ? -1 : 1; - } else if (operator === ">" || operator === ">=") { - wrongDirection = - counterPosition === "left" ? 1 : -1; - } else { - return; - } - - if (update.type === "UpdateExpression") { - if ( - getUpdateDirection(update, counter) === - wrongDirection - ) { - report(node); - } - } else if ( - update.type === "AssignmentExpression" && - getAssignmentDirection(update, counter) === - wrongDirection - ) { - report(node); - } - } - } - }, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Enforce \"for\" loop update clause moving the counter in the right direction", + recommended: true, + url: "https://eslint.org/docs/latest/rules/for-direction" + }, + + fixable: null, + schema: [], + + messages: { + incorrectDirection: "The update clause in this loop moves the variable in the wrong direction." + } + }, + + create(context) { + const { sourceCode } = context; + + /** + * report an error. + * @param {ASTNode} node the node to report. + * @returns {void} + */ + function report(node) { + context.report({ + node, + messageId: "incorrectDirection" + }); + } + + /** + * check the right side of the assignment + * @param {ASTNode} update UpdateExpression to check + * @param {int} dir expected direction that could either be turned around or invalidated + * @returns {int} return dir, the negated dir, or zero if the counter does not change or the direction is not clear + */ + function getRightDirection(update, dir) { + const staticValue = getStaticValue(update.right, sourceCode.getScope(update)); + + if (staticValue && ["bigint", "boolean", "number"].includes(typeof staticValue.value)) { + const sign = Math.sign(Number(staticValue.value)) || 0; // convert NaN to 0 + + return dir * sign; + } + return 0; + } + + /** + * check UpdateExpression add/sub the counter + * @param {ASTNode} update UpdateExpression to check + * @param {string} counter variable name to check + * @returns {int} if add return 1, if sub return -1, if nochange, return 0 + */ + function getUpdateDirection(update, counter) { + if (update.argument.type === "Identifier" && update.argument.name === counter) { + if (update.operator === "++") { + return 1; + } + if (update.operator === "--") { + return -1; + } + } + return 0; + } + + /** + * check AssignmentExpression add/sub the counter + * @param {ASTNode} update AssignmentExpression to check + * @param {string} counter variable name to check + * @returns {int} if add return 1, if sub return -1, if nochange, return 0 + */ + function getAssignmentDirection(update, counter) { + if (update.left.name === counter) { + if (update.operator === "+=") { + return getRightDirection(update, 1); + } + if (update.operator === "-=") { + return getRightDirection(update, -1); + } + } + return 0; + } + + return { + ForStatement(node) { + + if (node.test && node.test.type === "BinaryExpression" && node.update) { + for (const counterPosition of ["left", "right"]) { + if (node.test[counterPosition].type !== "Identifier") { + continue; + } + + const counter = node.test[counterPosition].name; + const operator = node.test.operator; + const update = node.update; + + let wrongDirection; + + if (operator === "<" || operator === "<=") { + wrongDirection = counterPosition === "left" ? -1 : 1; + } else if (operator === ">" || operator === ">=") { + wrongDirection = counterPosition === "left" ? 1 : -1; + } else { + return; + } + + if (update.type === "UpdateExpression") { + if (getUpdateDirection(update, counter) === wrongDirection) { + report(node); + } + } else if (update.type === "AssignmentExpression" && getAssignmentDirection(update, counter) === wrongDirection) { + report(node); + } + } + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/func-call-spacing.js b/node_modules/eslint/lib/rules/func-call-spacing.js index 1195d3f49..33f73727b 100644 --- a/node_modules/eslint/lib/rules/func-call-spacing.js +++ b/node_modules/eslint/lib/rules/func-call-spacing.js @@ -16,266 +16,218 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "function-call-spacing", - url: "https://eslint.style/rules/function-call-spacing", - }, - }, - ], - }, - type: "layout", - - docs: { - description: - "Require or disallow spacing between function identifiers and their invocations", - recommended: false, - url: "https://eslint.org/docs/latest/rules/func-call-spacing", - }, - - fixable: "whitespace", - - schema: { - anyOf: [ - { - type: "array", - items: [ - { - enum: ["never"], - }, - ], - minItems: 0, - maxItems: 1, - }, - { - type: "array", - items: [ - { - enum: ["always"], - }, - { - type: "object", - properties: { - allowNewlines: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - minItems: 0, - maxItems: 2, - }, - ], - }, - - messages: { - unexpectedWhitespace: - "Unexpected whitespace between function name and paren.", - unexpectedNewline: - "Unexpected newline between function name and paren.", - missing: "Missing space between function name and paren.", - }, - }, - - create(context) { - const never = context.options[0] !== "always"; - const allowNewlines = - !never && context.options[1] && context.options[1].allowNewlines; - const sourceCode = context.sourceCode; - const text = sourceCode.getText(); - - /** - * Check if open space is present in a function name - * @param {ASTNode} node node to evaluate - * @param {Token} leftToken The last token of the callee. This may be the closing parenthesis that encloses the callee. - * @param {Token} rightToken The first token of the arguments. this is the opening parenthesis that encloses the arguments. - * @returns {void} - * @private - */ - function checkSpacing(node, leftToken, rightToken) { - const textBetweenTokens = text - .slice(leftToken.range[1], rightToken.range[0]) - .replace(/\/\*.*?\*\//gu, ""); - const hasWhitespace = /\s/u.test(textBetweenTokens); - const hasNewline = - hasWhitespace && - astUtils.LINEBREAK_MATCHER.test(textBetweenTokens); - - /* - * never allowNewlines hasWhitespace hasNewline message - * F F F F Missing space between function name and paren. - * F F F T (Invalid `!hasWhitespace && hasNewline`) - * F F T T Unexpected newline between function name and paren. - * F F T F (OK) - * F T T F (OK) - * F T T T (OK) - * F T F T (Invalid `!hasWhitespace && hasNewline`) - * F T F F Missing space between function name and paren. - * T T F F (Invalid `never && allowNewlines`) - * T T F T (Invalid `!hasWhitespace && hasNewline`) - * T T T T (Invalid `never && allowNewlines`) - * T T T F (Invalid `never && allowNewlines`) - * T F T F Unexpected space between function name and paren. - * T F T T Unexpected space between function name and paren. - * T F F T (Invalid `!hasWhitespace && hasNewline`) - * T F F F (OK) - * - * T T Unexpected space between function name and paren. - * F F Missing space between function name and paren. - * F F T Unexpected newline between function name and paren. - */ - - if (never && hasWhitespace) { - context.report({ - node, - loc: { - start: leftToken.loc.end, - end: { - line: rightToken.loc.start.line, - column: rightToken.loc.start.column - 1, - }, - }, - messageId: "unexpectedWhitespace", - fix(fixer) { - // Don't remove comments. - if ( - sourceCode.commentsExistBetween( - leftToken, - rightToken, - ) - ) { - return null; - } - - // If `?.` exists, it doesn't hide no-unexpected-multiline errors - if (node.optional) { - return fixer.replaceTextRange( - [leftToken.range[1], rightToken.range[0]], - "?.", - ); - } - - /* - * Only autofix if there is no newline - * https://github.com/eslint/eslint/issues/7787 - */ - if (hasNewline) { - return null; - } - return fixer.removeRange([ - leftToken.range[1], - rightToken.range[0], - ]); - }, - }); - } else if (!never && !hasWhitespace) { - context.report({ - node, - loc: { - start: { - line: leftToken.loc.end.line, - column: leftToken.loc.end.column - 1, - }, - end: rightToken.loc.start, - }, - messageId: "missing", - fix(fixer) { - if (node.optional) { - return null; // Not sure if inserting a space to either before/after `?.` token. - } - return fixer.insertTextBefore(rightToken, " "); - }, - }); - } else if (!never && !allowNewlines && hasNewline) { - context.report({ - node, - loc: { - start: leftToken.loc.end, - end: rightToken.loc.start, - }, - messageId: "unexpectedNewline", - fix(fixer) { - /* - * Only autofix if there is no newline - * https://github.com/eslint/eslint/issues/7787 - * But if `?.` exists, it doesn't hide no-unexpected-multiline errors - */ - if (!node.optional) { - return null; - } - - // Don't remove comments. - if ( - sourceCode.commentsExistBetween( - leftToken, - rightToken, - ) - ) { - return null; - } - - const range = [leftToken.range[1], rightToken.range[0]]; - const qdToken = sourceCode.getTokenAfter(leftToken); - - if (qdToken.range[0] === leftToken.range[1]) { - return fixer.replaceTextRange(range, "?. "); - } - if (qdToken.range[1] === rightToken.range[0]) { - return fixer.replaceTextRange(range, " ?."); - } - return fixer.replaceTextRange(range, " ?. "); - }, - }); - } - } - - return { - "CallExpression, NewExpression"(node) { - const lastToken = sourceCode.getLastToken(node); - const lastCalleeToken = sourceCode.getLastToken(node.callee); - const parenToken = sourceCode.getFirstTokenBetween( - lastCalleeToken, - lastToken, - astUtils.isOpeningParenToken, - ); - const prevToken = - parenToken && - sourceCode.getTokenBefore( - parenToken, - astUtils.isNotQuestionDotToken, - ); - - // Parens in NewExpression are optional - if (!(parenToken && parenToken.range[1] < node.range[1])) { - return; - } - - checkSpacing(node, prevToken, parenToken); - }, - - ImportExpression(node) { - const leftToken = sourceCode.getFirstToken(node); - const rightToken = sourceCode.getTokenAfter(leftToken); - - checkSpacing(node, leftToken, rightToken); - }, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Require or disallow spacing between function identifiers and their invocations", + recommended: false, + url: "https://eslint.org/docs/latest/rules/func-call-spacing" + }, + + fixable: "whitespace", + + schema: { + anyOf: [ + { + type: "array", + items: [ + { + enum: ["never"] + } + ], + minItems: 0, + maxItems: 1 + }, + { + type: "array", + items: [ + { + enum: ["always"] + }, + { + type: "object", + properties: { + allowNewlines: { + type: "boolean" + } + }, + additionalProperties: false + } + ], + minItems: 0, + maxItems: 2 + } + ] + }, + + messages: { + unexpectedWhitespace: "Unexpected whitespace between function name and paren.", + unexpectedNewline: "Unexpected newline between function name and paren.", + missing: "Missing space between function name and paren." + } + }, + + create(context) { + + const never = context.options[0] !== "always"; + const allowNewlines = !never && context.options[1] && context.options[1].allowNewlines; + const sourceCode = context.sourceCode; + const text = sourceCode.getText(); + + /** + * Check if open space is present in a function name + * @param {ASTNode} node node to evaluate + * @param {Token} leftToken The last token of the callee. This may be the closing parenthesis that encloses the callee. + * @param {Token} rightToken Tha first token of the arguments. this is the opening parenthesis that encloses the arguments. + * @returns {void} + * @private + */ + function checkSpacing(node, leftToken, rightToken) { + const textBetweenTokens = text.slice(leftToken.range[1], rightToken.range[0]).replace(/\/\*.*?\*\//gu, ""); + const hasWhitespace = /\s/u.test(textBetweenTokens); + const hasNewline = hasWhitespace && astUtils.LINEBREAK_MATCHER.test(textBetweenTokens); + + /* + * never allowNewlines hasWhitespace hasNewline message + * F F F F Missing space between function name and paren. + * F F F T (Invalid `!hasWhitespace && hasNewline`) + * F F T T Unexpected newline between function name and paren. + * F F T F (OK) + * F T T F (OK) + * F T T T (OK) + * F T F T (Invalid `!hasWhitespace && hasNewline`) + * F T F F Missing space between function name and paren. + * T T F F (Invalid `never && allowNewlines`) + * T T F T (Invalid `!hasWhitespace && hasNewline`) + * T T T T (Invalid `never && allowNewlines`) + * T T T F (Invalid `never && allowNewlines`) + * T F T F Unexpected space between function name and paren. + * T F T T Unexpected space between function name and paren. + * T F F T (Invalid `!hasWhitespace && hasNewline`) + * T F F F (OK) + * + * T T Unexpected space between function name and paren. + * F F Missing space between function name and paren. + * F F T Unexpected newline between function name and paren. + */ + + if (never && hasWhitespace) { + context.report({ + node, + loc: { + start: leftToken.loc.end, + end: { + line: rightToken.loc.start.line, + column: rightToken.loc.start.column - 1 + } + }, + messageId: "unexpectedWhitespace", + fix(fixer) { + + // Don't remove comments. + if (sourceCode.commentsExistBetween(leftToken, rightToken)) { + return null; + } + + // If `?.` exists, it doesn't hide no-unexpected-multiline errors + if (node.optional) { + return fixer.replaceTextRange([leftToken.range[1], rightToken.range[0]], "?."); + } + + /* + * Only autofix if there is no newline + * https://github.com/eslint/eslint/issues/7787 + */ + if (hasNewline) { + return null; + } + return fixer.removeRange([leftToken.range[1], rightToken.range[0]]); + } + }); + } else if (!never && !hasWhitespace) { + context.report({ + node, + loc: { + start: { + line: leftToken.loc.end.line, + column: leftToken.loc.end.column - 1 + }, + end: rightToken.loc.start + }, + messageId: "missing", + fix(fixer) { + if (node.optional) { + return null; // Not sure if inserting a space to either before/after `?.` token. + } + return fixer.insertTextBefore(rightToken, " "); + } + }); + } else if (!never && !allowNewlines && hasNewline) { + context.report({ + node, + loc: { + start: leftToken.loc.end, + end: rightToken.loc.start + }, + messageId: "unexpectedNewline", + fix(fixer) { + + /* + * Only autofix if there is no newline + * https://github.com/eslint/eslint/issues/7787 + * But if `?.` exists, it doesn't hide no-unexpected-multiline errors + */ + if (!node.optional) { + return null; + } + + // Don't remove comments. + if (sourceCode.commentsExistBetween(leftToken, rightToken)) { + return null; + } + + const range = [leftToken.range[1], rightToken.range[0]]; + const qdToken = sourceCode.getTokenAfter(leftToken); + + if (qdToken.range[0] === leftToken.range[1]) { + return fixer.replaceTextRange(range, "?. "); + } + if (qdToken.range[1] === rightToken.range[0]) { + return fixer.replaceTextRange(range, " ?."); + } + return fixer.replaceTextRange(range, " ?. "); + } + }); + } + } + + return { + "CallExpression, NewExpression"(node) { + const lastToken = sourceCode.getLastToken(node); + const lastCalleeToken = sourceCode.getLastToken(node.callee); + const parenToken = sourceCode.getFirstTokenBetween(lastCalleeToken, lastToken, astUtils.isOpeningParenToken); + const prevToken = parenToken && sourceCode.getTokenBefore(parenToken, astUtils.isNotQuestionDotToken); + + // Parens in NewExpression are optional + if (!(parenToken && parenToken.range[1] < node.range[1])) { + return; + } + + checkSpacing(node, prevToken, parenToken); + }, + + ImportExpression(node) { + const leftToken = sourceCode.getFirstToken(node); + const rightToken = sourceCode.getTokenAfter(leftToken); + + checkSpacing(node, leftToken, rightToken); + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/func-name-matching.js b/node_modules/eslint/lib/rules/func-name-matching.js index 4643bfbc4..b9555d6bd 100644 --- a/node_modules/eslint/lib/rules/func-name-matching.js +++ b/node_modules/eslint/lib/rules/func-name-matching.js @@ -22,41 +22,32 @@ const esutils = require("esutils"); * @returns {boolean} True if the pattern is `module.exports` or `module["exports"]` */ function isModuleExports(pattern) { - if ( - pattern.type === "MemberExpression" && - pattern.object.type === "Identifier" && - pattern.object.name === "module" - ) { - // module.exports - if ( - pattern.property.type === "Identifier" && - pattern.property.name === "exports" - ) { - return true; - } - - // module["exports"] - if ( - pattern.property.type === "Literal" && - pattern.property.value === "exports" - ) { - return true; - } - } - return false; + if (pattern.type === "MemberExpression" && pattern.object.type === "Identifier" && pattern.object.name === "module") { + + // module.exports + if (pattern.property.type === "Identifier" && pattern.property.name === "exports") { + return true; + } + + // module["exports"] + if (pattern.property.type === "Literal" && pattern.property.value === "exports") { + return true; + } + } + return false; } /** * Determines if a string name is a valid identifier * @param {string} name The string to be checked - * @param {number} ecmaVersion The ECMAScript version if specified in the parserOptions config + * @param {int} ecmaVersion The ECMAScript version if specified in the parserOptions config * @returns {boolean} True if the string is a valid identifier */ function isIdentifier(name, ecmaVersion) { - if (ecmaVersion >= 2015) { - return esutils.keyword.isIdentifierES6(name); - } - return esutils.keyword.isIdentifierES5(name); + if (ecmaVersion >= 2015) { + return esutils.keyword.isIdentifierES6(name); + } + return esutils.keyword.isIdentifierES5(name); } //------------------------------------------------------------------------------ @@ -65,274 +56,198 @@ function isIdentifier(name, ecmaVersion) { const alwaysOrNever = { enum: ["always", "never"] }; const optionsObject = { - type: "object", - properties: { - considerPropertyDescriptor: { - type: "boolean", - }, - includeCommonJSModuleExports: { - type: "boolean", - }, - }, - additionalProperties: false, + type: "object", + properties: { + considerPropertyDescriptor: { + type: "boolean" + }, + includeCommonJSModuleExports: { + type: "boolean" + } + }, + additionalProperties: false }; -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: - "Require function names to match the name of the variable or property to which they are assigned", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/func-name-matching", - }, - - schema: { - anyOf: [ - { - type: "array", - additionalItems: false, - items: [alwaysOrNever, optionsObject], - }, - { - type: "array", - additionalItems: false, - items: [optionsObject], - }, - ], - }, - - messages: { - matchProperty: - "Function name `{{funcName}}` should match property name `{{name}}`.", - matchVariable: - "Function name `{{funcName}}` should match variable name `{{name}}`.", - notMatchProperty: - "Function name `{{funcName}}` should not match property name `{{name}}`.", - notMatchVariable: - "Function name `{{funcName}}` should not match variable name `{{name}}`.", - }, - }, - - create(context) { - const options = - (typeof context.options[0] === "object" - ? context.options[0] - : context.options[1]) || {}; - const nameMatches = - typeof context.options[0] === "string" - ? context.options[0] - : "always"; - const considerPropertyDescriptor = options.considerPropertyDescriptor; - const includeModuleExports = options.includeCommonJSModuleExports; - const ecmaVersion = context.languageOptions.ecmaVersion; - - /** - * Check whether node is a certain CallExpression. - * @param {string} objName object name - * @param {string} funcName function name - * @param {ASTNode} node The node to check - * @returns {boolean} `true` if node matches CallExpression - */ - function isPropertyCall(objName, funcName, node) { - if (!node) { - return false; - } - return ( - node.type === "CallExpression" && - astUtils.isSpecificMemberAccess(node.callee, objName, funcName) - ); - } - - /** - * Compares identifiers based on the nameMatches option - * @param {string} x the first identifier - * @param {string} y the second identifier - * @returns {boolean} whether the two identifiers should warn. - */ - function shouldWarn(x, y) { - return ( - (nameMatches === "always" && x !== y) || - (nameMatches === "never" && x === y) - ); - } - - /** - * Reports - * @param {ASTNode} node The node to report - * @param {string} name The variable or property name - * @param {string} funcName The function name - * @param {boolean} isProp True if the reported node is a property assignment - * @returns {void} - */ - function report(node, name, funcName, isProp) { - let messageId; - - if (nameMatches === "always" && isProp) { - messageId = "matchProperty"; - } else if (nameMatches === "always") { - messageId = "matchVariable"; - } else if (isProp) { - messageId = "notMatchProperty"; - } else { - messageId = "notMatchVariable"; - } - context.report({ - node, - messageId, - data: { - name, - funcName, - }, - }); - } - - /** - * Determines whether a given node is a string literal - * @param {ASTNode} node The node to check - * @returns {boolean} `true` if the node is a string literal - */ - function isStringLiteral(node) { - return node.type === "Literal" && typeof node.value === "string"; - } - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - return { - VariableDeclarator(node) { - if ( - !node.init || - node.init.type !== "FunctionExpression" || - node.id.type !== "Identifier" - ) { - return; - } - if ( - node.init.id && - shouldWarn(node.id.name, node.init.id.name) - ) { - report(node, node.id.name, node.init.id.name, false); - } - }, - - AssignmentExpression(node) { - if ( - node.right.type !== "FunctionExpression" || - (node.left.computed && - node.left.property.type !== "Literal") || - (!includeModuleExports && isModuleExports(node.left)) || - (node.left.type !== "Identifier" && - node.left.type !== "MemberExpression") - ) { - return; - } - - const isProp = node.left.type === "MemberExpression"; - const name = isProp - ? astUtils.getStaticPropertyName(node.left) - : node.left.name; - - if ( - node.right.id && - name && - isIdentifier(name) && - shouldWarn(name, node.right.id.name) - ) { - report(node, name, node.right.id.name, isProp); - } - }, - - "Property, PropertyDefinition[value]"(node) { - if ( - !(node.value.type === "FunctionExpression" && node.value.id) - ) { - return; - } - - if (node.key.type === "Identifier" && !node.computed) { - const functionName = node.value.id.name; - let propertyName = node.key.name; - - if ( - considerPropertyDescriptor && - propertyName === "value" && - node.parent.type === "ObjectExpression" - ) { - if ( - isPropertyCall( - "Object", - "defineProperty", - node.parent.parent, - ) || - isPropertyCall( - "Reflect", - "defineProperty", - node.parent.parent, - ) - ) { - const property = node.parent.parent.arguments[1]; - - if ( - isStringLiteral(property) && - shouldWarn(property.value, functionName) - ) { - report( - node, - property.value, - functionName, - true, - ); - } - } else if ( - isPropertyCall( - "Object", - "defineProperties", - node.parent.parent.parent.parent, - ) - ) { - propertyName = node.parent.parent.key.name; - if ( - !node.parent.parent.computed && - shouldWarn(propertyName, functionName) - ) { - report(node, propertyName, functionName, true); - } - } else if ( - isPropertyCall( - "Object", - "create", - node.parent.parent.parent.parent, - ) - ) { - propertyName = node.parent.parent.key.name; - if ( - !node.parent.parent.computed && - shouldWarn(propertyName, functionName) - ) { - report(node, propertyName, functionName, true); - } - } else if (shouldWarn(propertyName, functionName)) { - report(node, propertyName, functionName, true); - } - } else if (shouldWarn(propertyName, functionName)) { - report(node, propertyName, functionName, true); - } - return; - } - - if ( - isStringLiteral(node.key) && - isIdentifier(node.key.value, ecmaVersion) && - shouldWarn(node.key.value, node.value.id.name) - ) { - report(node, node.key.value, node.value.id.name, true); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Require function names to match the name of the variable or property to which they are assigned", + recommended: false, + url: "https://eslint.org/docs/latest/rules/func-name-matching" + }, + + schema: { + anyOf: [{ + type: "array", + additionalItems: false, + items: [alwaysOrNever, optionsObject] + }, { + type: "array", + additionalItems: false, + items: [optionsObject] + }] + }, + + messages: { + matchProperty: "Function name `{{funcName}}` should match property name `{{name}}`.", + matchVariable: "Function name `{{funcName}}` should match variable name `{{name}}`.", + notMatchProperty: "Function name `{{funcName}}` should not match property name `{{name}}`.", + notMatchVariable: "Function name `{{funcName}}` should not match variable name `{{name}}`." + } + }, + + create(context) { + const options = (typeof context.options[0] === "object" ? context.options[0] : context.options[1]) || {}; + const nameMatches = typeof context.options[0] === "string" ? context.options[0] : "always"; + const considerPropertyDescriptor = options.considerPropertyDescriptor; + const includeModuleExports = options.includeCommonJSModuleExports; + const ecmaVersion = context.languageOptions.ecmaVersion; + + /** + * Check whether node is a certain CallExpression. + * @param {string} objName object name + * @param {string} funcName function name + * @param {ASTNode} node The node to check + * @returns {boolean} `true` if node matches CallExpression + */ + function isPropertyCall(objName, funcName, node) { + if (!node) { + return false; + } + return node.type === "CallExpression" && astUtils.isSpecificMemberAccess(node.callee, objName, funcName); + } + + /** + * Compares identifiers based on the nameMatches option + * @param {string} x the first identifier + * @param {string} y the second identifier + * @returns {boolean} whether the two identifiers should warn. + */ + function shouldWarn(x, y) { + return (nameMatches === "always" && x !== y) || (nameMatches === "never" && x === y); + } + + /** + * Reports + * @param {ASTNode} node The node to report + * @param {string} name The variable or property name + * @param {string} funcName The function name + * @param {boolean} isProp True if the reported node is a property assignment + * @returns {void} + */ + function report(node, name, funcName, isProp) { + let messageId; + + if (nameMatches === "always" && isProp) { + messageId = "matchProperty"; + } else if (nameMatches === "always") { + messageId = "matchVariable"; + } else if (isProp) { + messageId = "notMatchProperty"; + } else { + messageId = "notMatchVariable"; + } + context.report({ + node, + messageId, + data: { + name, + funcName + } + }); + } + + /** + * Determines whether a given node is a string literal + * @param {ASTNode} node The node to check + * @returns {boolean} `true` if the node is a string literal + */ + function isStringLiteral(node) { + return node.type === "Literal" && typeof node.value === "string"; + } + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + return { + VariableDeclarator(node) { + if (!node.init || node.init.type !== "FunctionExpression" || node.id.type !== "Identifier") { + return; + } + if (node.init.id && shouldWarn(node.id.name, node.init.id.name)) { + report(node, node.id.name, node.init.id.name, false); + } + }, + + AssignmentExpression(node) { + if ( + node.right.type !== "FunctionExpression" || + (node.left.computed && node.left.property.type !== "Literal") || + (!includeModuleExports && isModuleExports(node.left)) || + (node.left.type !== "Identifier" && node.left.type !== "MemberExpression") + ) { + return; + } + + const isProp = node.left.type === "MemberExpression"; + const name = isProp ? astUtils.getStaticPropertyName(node.left) : node.left.name; + + if (node.right.id && name && isIdentifier(name) && shouldWarn(name, node.right.id.name)) { + report(node, name, node.right.id.name, isProp); + } + }, + + "Property, PropertyDefinition[value]"(node) { + if (!(node.value.type === "FunctionExpression" && node.value.id)) { + return; + } + + if (node.key.type === "Identifier" && !node.computed) { + const functionName = node.value.id.name; + let propertyName = node.key.name; + + if ( + considerPropertyDescriptor && + propertyName === "value" && + node.parent.type === "ObjectExpression" + ) { + if (isPropertyCall("Object", "defineProperty", node.parent.parent) || isPropertyCall("Reflect", "defineProperty", node.parent.parent)) { + const property = node.parent.parent.arguments[1]; + + if (isStringLiteral(property) && shouldWarn(property.value, functionName)) { + report(node, property.value, functionName, true); + } + } else if (isPropertyCall("Object", "defineProperties", node.parent.parent.parent.parent)) { + propertyName = node.parent.parent.key.name; + if (!node.parent.parent.computed && shouldWarn(propertyName, functionName)) { + report(node, propertyName, functionName, true); + } + } else if (isPropertyCall("Object", "create", node.parent.parent.parent.parent)) { + propertyName = node.parent.parent.key.name; + if (!node.parent.parent.computed && shouldWarn(propertyName, functionName)) { + report(node, propertyName, functionName, true); + } + } else if (shouldWarn(propertyName, functionName)) { + report(node, propertyName, functionName, true); + } + } else if (shouldWarn(propertyName, functionName)) { + report(node, propertyName, functionName, true); + } + return; + } + + if ( + isStringLiteral(node.key) && + isIdentifier(node.key.value, ecmaVersion) && + shouldWarn(node.key.value, node.value.id.name) + ) { + report(node, node.key.value, node.value.id.name, true); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/func-names.js b/node_modules/eslint/lib/rules/func-names.js index bef70e32a..b180580e1 100644 --- a/node_modules/eslint/lib/rules/func-names.js +++ b/node_modules/eslint/lib/rules/func-names.js @@ -17,176 +17,175 @@ const astUtils = require("./utils/ast-utils"); * @returns {boolean} `true` if the variable is a function name. */ function isFunctionName(variable) { - return variable && variable.defs[0].type === "FunctionName"; + return variable && variable.defs[0].type === "FunctionName"; } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - defaultOptions: ["always", {}], - - docs: { - description: "Require or disallow named `function` expressions", - recommended: false, - url: "https://eslint.org/docs/latest/rules/func-names", - }, - - schema: { - definitions: { - value: { - enum: ["always", "as-needed", "never"], - }, - }, - items: [ - { - $ref: "#/definitions/value", - }, - { - type: "object", - properties: { - generators: { - $ref: "#/definitions/value", - }, - }, - additionalProperties: false, - }, - ], - }, - - messages: { - unnamed: "Unexpected unnamed {{name}}.", - named: "Unexpected named {{name}}.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - /** - * Returns the config option for the given node. - * @param {ASTNode} node A node to get the config for. - * @returns {string} The config option. - */ - function getConfigForNode(node) { - if (node.generator && context.options[1].generators) { - return context.options[1].generators; - } - - return context.options[0]; - } - - /** - * Determines whether the current FunctionExpression node is a get, set, or - * shorthand method in an object literal or a class. - * @param {ASTNode} node A node to check. - * @returns {boolean} True if the node is a get, set, or shorthand method. - */ - function isObjectOrClassMethod(node) { - const parent = node.parent; - - return ( - parent.type === "MethodDefinition" || - (parent.type === "Property" && - (parent.method || - parent.kind === "get" || - parent.kind === "set")) - ); - } - - /** - * Determines whether the current FunctionExpression node has a name that would be - * inferred from context in a conforming ES6 environment. - * @param {ASTNode} node A node to check. - * @returns {boolean} True if the node would have a name assigned automatically. - */ - function hasInferredName(node) { - const parent = node.parent; - - return ( - isObjectOrClassMethod(node) || - (parent.type === "VariableDeclarator" && - parent.id.type === "Identifier" && - parent.init === node) || - (parent.type === "Property" && parent.value === node) || - (parent.type === "PropertyDefinition" && - parent.value === node) || - (parent.type === "AssignmentExpression" && - parent.left.type === "Identifier" && - parent.right === node) || - (parent.type === "AssignmentPattern" && - parent.left.type === "Identifier" && - parent.right === node) - ); - } - - /** - * Reports that an unnamed function should be named - * @param {ASTNode} node The node to report in the event of an error. - * @returns {void} - */ - function reportUnexpectedUnnamedFunction(node) { - context.report({ - node, - messageId: "unnamed", - loc: astUtils.getFunctionHeadLoc(node, sourceCode), - data: { name: astUtils.getFunctionNameWithKind(node) }, - }); - } - - /** - * Reports that a named function should be unnamed - * @param {ASTNode} node The node to report in the event of an error. - * @returns {void} - */ - function reportUnexpectedNamedFunction(node) { - context.report({ - node, - messageId: "named", - loc: astUtils.getFunctionHeadLoc(node, sourceCode), - data: { name: astUtils.getFunctionNameWithKind(node) }, - }); - } - - /** - * The listener for function nodes. - * @param {ASTNode} node function node - * @returns {void} - */ - function handleFunction(node) { - // Skip recursive functions. - const nameVar = sourceCode.getDeclaredVariables(node)[0]; - - if (isFunctionName(nameVar) && nameVar.references.length > 0) { - return; - } - - const hasName = Boolean(node.id && node.id.name); - const config = getConfigForNode(node); - - if (config === "never") { - if (hasName && node.type !== "FunctionDeclaration") { - reportUnexpectedNamedFunction(node); - } - } else if (config === "as-needed") { - if (!hasName && !hasInferredName(node)) { - reportUnexpectedUnnamedFunction(node); - } - } else { - if (!hasName && !isObjectOrClassMethod(node)) { - reportUnexpectedUnnamedFunction(node); - } - } - } - - return { - "FunctionExpression:exit": handleFunction, - "ExportDefaultDeclaration > FunctionDeclaration": handleFunction, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Require or disallow named `function` expressions", + recommended: false, + url: "https://eslint.org/docs/latest/rules/func-names" + }, + + schema: { + definitions: { + value: { + enum: [ + "always", + "as-needed", + "never" + ] + } + }, + items: [ + { + $ref: "#/definitions/value" + }, + { + type: "object", + properties: { + generators: { + $ref: "#/definitions/value" + } + }, + additionalProperties: false + } + ] + }, + + messages: { + unnamed: "Unexpected unnamed {{name}}.", + named: "Unexpected named {{name}}." + } + }, + + create(context) { + + const sourceCode = context.sourceCode; + + /** + * Returns the config option for the given node. + * @param {ASTNode} node A node to get the config for. + * @returns {string} The config option. + */ + function getConfigForNode(node) { + if ( + node.generator && + context.options.length > 1 && + context.options[1].generators + ) { + return context.options[1].generators; + } + + return context.options[0] || "always"; + } + + /** + * Determines whether the current FunctionExpression node is a get, set, or + * shorthand method in an object literal or a class. + * @param {ASTNode} node A node to check. + * @returns {boolean} True if the node is a get, set, or shorthand method. + */ + function isObjectOrClassMethod(node) { + const parent = node.parent; + + return (parent.type === "MethodDefinition" || ( + parent.type === "Property" && ( + parent.method || + parent.kind === "get" || + parent.kind === "set" + ) + )); + } + + /** + * Determines whether the current FunctionExpression node has a name that would be + * inferred from context in a conforming ES6 environment. + * @param {ASTNode} node A node to check. + * @returns {boolean} True if the node would have a name assigned automatically. + */ + function hasInferredName(node) { + const parent = node.parent; + + return isObjectOrClassMethod(node) || + (parent.type === "VariableDeclarator" && parent.id.type === "Identifier" && parent.init === node) || + (parent.type === "Property" && parent.value === node) || + (parent.type === "PropertyDefinition" && parent.value === node) || + (parent.type === "AssignmentExpression" && parent.left.type === "Identifier" && parent.right === node) || + (parent.type === "AssignmentPattern" && parent.left.type === "Identifier" && parent.right === node); + } + + /** + * Reports that an unnamed function should be named + * @param {ASTNode} node The node to report in the event of an error. + * @returns {void} + */ + function reportUnexpectedUnnamedFunction(node) { + context.report({ + node, + messageId: "unnamed", + loc: astUtils.getFunctionHeadLoc(node, sourceCode), + data: { name: astUtils.getFunctionNameWithKind(node) } + }); + } + + /** + * Reports that a named function should be unnamed + * @param {ASTNode} node The node to report in the event of an error. + * @returns {void} + */ + function reportUnexpectedNamedFunction(node) { + context.report({ + node, + messageId: "named", + loc: astUtils.getFunctionHeadLoc(node, sourceCode), + data: { name: astUtils.getFunctionNameWithKind(node) } + }); + } + + /** + * The listener for function nodes. + * @param {ASTNode} node function node + * @returns {void} + */ + function handleFunction(node) { + + // Skip recursive functions. + const nameVar = sourceCode.getDeclaredVariables(node)[0]; + + if (isFunctionName(nameVar) && nameVar.references.length > 0) { + return; + } + + const hasName = Boolean(node.id && node.id.name); + const config = getConfigForNode(node); + + if (config === "never") { + if (hasName && node.type !== "FunctionDeclaration") { + reportUnexpectedNamedFunction(node); + } + } else if (config === "as-needed") { + if (!hasName && !hasInferredName(node)) { + reportUnexpectedUnnamedFunction(node); + } + } else { + if (!hasName && !isObjectOrClassMethod(node)) { + reportUnexpectedUnnamedFunction(node); + } + } + } + + return { + "FunctionExpression:exit": handleFunction, + "ExportDefaultDeclaration > FunctionDeclaration": handleFunction + }; + } }; diff --git a/node_modules/eslint/lib/rules/func-style.js b/node_modules/eslint/lib/rules/func-style.js index dcd4ae9f7..ab83772ef 100644 --- a/node_modules/eslint/lib/rules/func-style.js +++ b/node_modules/eslint/lib/rules/func-style.js @@ -8,214 +8,91 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - dialects: ["javascript", "typescript"], - language: "javascript", - type: "suggestion", - - defaultOptions: [ - "expression", - { - allowArrowFunctions: false, - allowTypeAnnotation: false, - overrides: {}, - }, - ], - - docs: { - description: - "Enforce the consistent use of either `function` declarations or expressions assigned to variables", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/func-style", - }, - - schema: [ - { - enum: ["declaration", "expression"], - }, - { - type: "object", - properties: { - allowArrowFunctions: { - type: "boolean", - }, - allowTypeAnnotation: { - type: "boolean", - }, - overrides: { - type: "object", - properties: { - namedExports: { - enum: ["declaration", "expression", "ignore"], - }, - }, - additionalProperties: false, - }, - }, - additionalProperties: false, - }, - ], - - messages: { - expression: "Expected a function expression.", - declaration: "Expected a function declaration.", - }, - }, - - create(context) { - const [style, { allowArrowFunctions, allowTypeAnnotation, overrides }] = - context.options; - const enforceDeclarations = style === "declaration"; - const { namedExports: exportFunctionStyle } = overrides; - const stack = []; - - /** - * Checks if a function declaration is part of an overloaded function - * @param {ASTNode} node The function declaration node to check - * @returns {boolean} True if the function is overloaded - */ - function isOverloadedFunction(node) { - const functionName = node.id.name; - - if (node.parent.type === "ExportNamedDeclaration") { - return node.parent.parent.body.some( - member => - member.type === "ExportNamedDeclaration" && - member.declaration?.type === "TSDeclareFunction" && - member.declaration.id.name === functionName, - ); - } - - if (node.parent.type === "SwitchCase") { - return node.parent.parent.cases.some(switchCase => - switchCase.consequent.some( - member => - member.type === "TSDeclareFunction" && - member.id.name === functionName, - ), - ); - } - - return ( - Array.isArray(node.parent.body) && - node.parent.body.some( - member => - member.type === "TSDeclareFunction" && - member.id.name === functionName, - ) - ); - } - - const nodesToCheck = { - FunctionDeclaration(node) { - stack.push(false); - - if ( - !enforceDeclarations && - node.parent.type !== "ExportDefaultDeclaration" && - (typeof exportFunctionStyle === "undefined" || - node.parent.type !== "ExportNamedDeclaration") && - !isOverloadedFunction(node) - ) { - context.report({ node, messageId: "expression" }); - } - - if ( - node.parent.type === "ExportNamedDeclaration" && - exportFunctionStyle === "expression" && - !isOverloadedFunction(node) - ) { - context.report({ node, messageId: "expression" }); - } - }, - "FunctionDeclaration:exit"() { - stack.pop(); - }, - - FunctionExpression(node) { - stack.push(false); - - if ( - enforceDeclarations && - node.parent.type === "VariableDeclarator" && - (typeof exportFunctionStyle === "undefined" || - node.parent.parent.parent.type !== - "ExportNamedDeclaration") && - !(allowTypeAnnotation && node.parent.id.typeAnnotation) - ) { - context.report({ - node: node.parent, - messageId: "declaration", - }); - } - - if ( - node.parent.type === "VariableDeclarator" && - node.parent.parent.parent.type === - "ExportNamedDeclaration" && - exportFunctionStyle === "declaration" && - !(allowTypeAnnotation && node.parent.id.typeAnnotation) - ) { - context.report({ - node: node.parent, - messageId: "declaration", - }); - } - }, - "FunctionExpression:exit"() { - stack.pop(); - }, - - "ThisExpression, Super"() { - if (stack.length > 0) { - stack[stack.length - 1] = true; - } - }, - }; - - if (!allowArrowFunctions) { - nodesToCheck.ArrowFunctionExpression = function () { - stack.push(false); - }; - - nodesToCheck["ArrowFunctionExpression:exit"] = function (node) { - const hasThisOrSuperExpr = stack.pop(); - - if ( - !hasThisOrSuperExpr && - node.parent.type === "VariableDeclarator" - ) { - if ( - enforceDeclarations && - (typeof exportFunctionStyle === "undefined" || - node.parent.parent.parent.type !== - "ExportNamedDeclaration") && - !(allowTypeAnnotation && node.parent.id.typeAnnotation) - ) { - context.report({ - node: node.parent, - messageId: "declaration", - }); - } - - if ( - node.parent.parent.parent.type === - "ExportNamedDeclaration" && - exportFunctionStyle === "declaration" && - !(allowTypeAnnotation && node.parent.id.typeAnnotation) - ) { - context.report({ - node: node.parent, - messageId: "declaration", - }); - } - } - }; - } - - return nodesToCheck; - }, + meta: { + type: "suggestion", + + docs: { + description: "Enforce the consistent use of either `function` declarations or expressions", + recommended: false, + url: "https://eslint.org/docs/latest/rules/func-style" + }, + + schema: [ + { + enum: ["declaration", "expression"] + }, + { + type: "object", + properties: { + allowArrowFunctions: { + type: "boolean", + default: false + } + }, + additionalProperties: false + } + ], + + messages: { + expression: "Expected a function expression.", + declaration: "Expected a function declaration." + } + }, + + create(context) { + + const style = context.options[0], + allowArrowFunctions = context.options[1] && context.options[1].allowArrowFunctions, + enforceDeclarations = (style === "declaration"), + stack = []; + + const nodesToCheck = { + FunctionDeclaration(node) { + stack.push(false); + + if (!enforceDeclarations && node.parent.type !== "ExportDefaultDeclaration") { + context.report({ node, messageId: "expression" }); + } + }, + "FunctionDeclaration:exit"() { + stack.pop(); + }, + + FunctionExpression(node) { + stack.push(false); + + if (enforceDeclarations && node.parent.type === "VariableDeclarator") { + context.report({ node: node.parent, messageId: "declaration" }); + } + }, + "FunctionExpression:exit"() { + stack.pop(); + }, + + ThisExpression() { + if (stack.length > 0) { + stack[stack.length - 1] = true; + } + } + }; + + if (!allowArrowFunctions) { + nodesToCheck.ArrowFunctionExpression = function() { + stack.push(false); + }; + + nodesToCheck["ArrowFunctionExpression:exit"] = function(node) { + const hasThisExpr = stack.pop(); + + if (enforceDeclarations && !hasThisExpr && node.parent.type === "VariableDeclarator") { + context.report({ node: node.parent, messageId: "declaration" }); + } + }; + } + + return nodesToCheck; + + } }; diff --git a/node_modules/eslint/lib/rules/function-call-argument-newline.js b/node_modules/eslint/lib/rules/function-call-argument-newline.js index 0ad1ca1d0..458399d62 100644 --- a/node_modules/eslint/lib/rules/function-call-argument-newline.js +++ b/node_modules/eslint/lib/rules/function-call-argument-newline.js @@ -10,157 +10,116 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "function-call-argument-newline", - url: "https://eslint.style/rules/function-call-argument-newline", - }, - }, - ], - }, - type: "layout", - - docs: { - description: - "Enforce line breaks between arguments of a function call", - recommended: false, - url: "https://eslint.org/docs/latest/rules/function-call-argument-newline", - }, - - fixable: "whitespace", - - schema: [ - { - enum: ["always", "never", "consistent"], - }, - ], - - messages: { - unexpectedLineBreak: "There should be no line break here.", - missingLineBreak: - "There should be a line break after this argument.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - const checkers = { - unexpected: { - messageId: "unexpectedLineBreak", - check: (prevToken, currentToken) => - prevToken.loc.end.line !== currentToken.loc.start.line, - createFix: (token, tokenBefore) => fixer => - fixer.replaceTextRange( - [tokenBefore.range[1], token.range[0]], - " ", - ), - }, - missing: { - messageId: "missingLineBreak", - check: (prevToken, currentToken) => - prevToken.loc.end.line === currentToken.loc.start.line, - createFix: (token, tokenBefore) => fixer => - fixer.replaceTextRange( - [tokenBefore.range[1], token.range[0]], - "\n", - ), - }, - }; - - /** - * Check all arguments for line breaks in the CallExpression - * @param {CallExpression} node node to evaluate - * @param {{ messageId: string, check: Function }} checker selected checker - * @returns {void} - * @private - */ - function checkArguments(node, checker) { - for (let i = 1; i < node.arguments.length; i++) { - const prevArgToken = sourceCode.getLastToken( - node.arguments[i - 1], - ); - const currentArgToken = sourceCode.getFirstToken( - node.arguments[i], - ); - - if (checker.check(prevArgToken, currentArgToken)) { - const tokenBefore = sourceCode.getTokenBefore( - currentArgToken, - { includeComments: true }, - ); - - const hasLineCommentBefore = tokenBefore.type === "Line"; - - context.report({ - node, - loc: { - start: tokenBefore.loc.end, - end: currentArgToken.loc.start, - }, - messageId: checker.messageId, - fix: hasLineCommentBefore - ? null - : checker.createFix(currentArgToken, tokenBefore), - }); - } - } - } - - /** - * Check if open space is present in a function name - * @param {CallExpression} node node to evaluate - * @returns {void} - * @private - */ - function check(node) { - if (node.arguments.length < 2) { - return; - } - - const option = context.options[0] || "always"; - - if (option === "never") { - checkArguments(node, checkers.unexpected); - } else if (option === "always") { - checkArguments(node, checkers.missing); - } else if (option === "consistent") { - const firstArgToken = sourceCode.getLastToken( - node.arguments[0], - ); - const secondArgToken = sourceCode.getFirstToken( - node.arguments[1], - ); - - if ( - firstArgToken.loc.end.line === secondArgToken.loc.start.line - ) { - checkArguments(node, checkers.unexpected); - } else { - checkArguments(node, checkers.missing); - } - } - } - - return { - CallExpression: check, - NewExpression: check, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Enforce line breaks between arguments of a function call", + recommended: false, + url: "https://eslint.org/docs/latest/rules/function-call-argument-newline" + }, + + fixable: "whitespace", + + schema: [ + { + enum: ["always", "never", "consistent"] + } + ], + + messages: { + unexpectedLineBreak: "There should be no line break here.", + missingLineBreak: "There should be a line break after this argument." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + + const checkers = { + unexpected: { + messageId: "unexpectedLineBreak", + check: (prevToken, currentToken) => prevToken.loc.end.line !== currentToken.loc.start.line, + createFix: (token, tokenBefore) => fixer => + fixer.replaceTextRange([tokenBefore.range[1], token.range[0]], " ") + }, + missing: { + messageId: "missingLineBreak", + check: (prevToken, currentToken) => prevToken.loc.end.line === currentToken.loc.start.line, + createFix: (token, tokenBefore) => fixer => + fixer.replaceTextRange([tokenBefore.range[1], token.range[0]], "\n") + } + }; + + /** + * Check all arguments for line breaks in the CallExpression + * @param {CallExpression} node node to evaluate + * @param {{ messageId: string, check: Function }} checker selected checker + * @returns {void} + * @private + */ + function checkArguments(node, checker) { + for (let i = 1; i < node.arguments.length; i++) { + const prevArgToken = sourceCode.getLastToken(node.arguments[i - 1]); + const currentArgToken = sourceCode.getFirstToken(node.arguments[i]); + + if (checker.check(prevArgToken, currentArgToken)) { + const tokenBefore = sourceCode.getTokenBefore( + currentArgToken, + { includeComments: true } + ); + + const hasLineCommentBefore = tokenBefore.type === "Line"; + + context.report({ + node, + loc: { + start: tokenBefore.loc.end, + end: currentArgToken.loc.start + }, + messageId: checker.messageId, + fix: hasLineCommentBefore ? null : checker.createFix(currentArgToken, tokenBefore) + }); + } + } + } + + /** + * Check if open space is present in a function name + * @param {CallExpression} node node to evaluate + * @returns {void} + * @private + */ + function check(node) { + if (node.arguments.length < 2) { + return; + } + + const option = context.options[0] || "always"; + + if (option === "never") { + checkArguments(node, checkers.unexpected); + } else if (option === "always") { + checkArguments(node, checkers.missing); + } else if (option === "consistent") { + const firstArgToken = sourceCode.getLastToken(node.arguments[0]); + const secondArgToken = sourceCode.getFirstToken(node.arguments[1]); + + if (firstArgToken.loc.end.line === secondArgToken.loc.start.line) { + checkArguments(node, checkers.unexpected); + } else { + checkArguments(node, checkers.missing); + } + } + } + + return { + CallExpression: check, + NewExpression: check + }; + } }; diff --git a/node_modules/eslint/lib/rules/function-paren-newline.js b/node_modules/eslint/lib/rules/function-paren-newline.js index 17e5e9cf8..de315a020 100644 --- a/node_modules/eslint/lib/rules/function-paren-newline.js +++ b/node_modules/eslint/lib/rules/function-paren-newline.js @@ -15,354 +15,278 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "function-paren-newline", - url: "https://eslint.style/rules/function-paren-newline", - }, - }, - ], - }, - type: "layout", - - docs: { - description: - "Enforce consistent line breaks inside function parentheses", - recommended: false, - url: "https://eslint.org/docs/latest/rules/function-paren-newline", - }, - - fixable: "whitespace", - - schema: [ - { - oneOf: [ - { - enum: [ - "always", - "never", - "consistent", - "multiline", - "multiline-arguments", - ], - }, - { - type: "object", - properties: { - minItems: { - type: "integer", - minimum: 0, - }, - }, - additionalProperties: false, - }, - ], - }, - ], - - messages: { - expectedBefore: "Expected newline before ')'.", - expectedAfter: "Expected newline after '('.", - expectedBetween: "Expected newline between arguments/params.", - unexpectedBefore: "Unexpected newline before ')'.", - unexpectedAfter: "Unexpected newline after '('.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - const rawOption = context.options[0] || "multiline"; - const multilineOption = rawOption === "multiline"; - const multilineArgumentsOption = rawOption === "multiline-arguments"; - const consistentOption = rawOption === "consistent"; - let minItems; - - if (typeof rawOption === "object") { - minItems = rawOption.minItems; - } else if (rawOption === "always") { - minItems = 0; - } else if (rawOption === "never") { - minItems = Infinity; - } else { - minItems = null; - } - - //---------------------------------------------------------------------- - // Helpers - //---------------------------------------------------------------------- - - /** - * Determines whether there should be newlines inside function parens - * @param {ASTNode[]} elements The arguments or parameters in the list - * @param {boolean} hasLeftNewline `true` if the left paren has a newline in the current code. - * @returns {boolean} `true` if there should be newlines inside the function parens - */ - function shouldHaveNewlines(elements, hasLeftNewline) { - if (multilineArgumentsOption && elements.length === 1) { - return hasLeftNewline; - } - if (multilineOption || multilineArgumentsOption) { - return elements.some( - (element, index) => - index !== elements.length - 1 && - element.loc.end.line !== - elements[index + 1].loc.start.line, - ); - } - if (consistentOption) { - return hasLeftNewline; - } - return elements.length >= minItems; - } - - /** - * Validates parens - * @param {Object} parens An object with keys `leftParen` for the left paren token, and `rightParen` for the right paren token - * @param {ASTNode[]} elements The arguments or parameters in the list - * @returns {void} - */ - function validateParens(parens, elements) { - const leftParen = parens.leftParen; - const rightParen = parens.rightParen; - const tokenAfterLeftParen = sourceCode.getTokenAfter(leftParen); - const tokenBeforeRightParen = sourceCode.getTokenBefore(rightParen); - const hasLeftNewline = !astUtils.isTokenOnSameLine( - leftParen, - tokenAfterLeftParen, - ); - const hasRightNewline = !astUtils.isTokenOnSameLine( - tokenBeforeRightParen, - rightParen, - ); - const needsNewlines = shouldHaveNewlines(elements, hasLeftNewline); - - if (hasLeftNewline && !needsNewlines) { - context.report({ - node: leftParen, - messageId: "unexpectedAfter", - fix(fixer) { - return sourceCode - .getText() - .slice( - leftParen.range[1], - tokenAfterLeftParen.range[0], - ) - .trim() - ? // If there is a comment between the ( and the first element, don't do a fix. - null - : fixer.removeRange([ - leftParen.range[1], - tokenAfterLeftParen.range[0], - ]); - }, - }); - } else if (!hasLeftNewline && needsNewlines) { - context.report({ - node: leftParen, - messageId: "expectedAfter", - fix: fixer => fixer.insertTextAfter(leftParen, "\n"), - }); - } - - if (hasRightNewline && !needsNewlines) { - context.report({ - node: rightParen, - messageId: "unexpectedBefore", - fix(fixer) { - return sourceCode - .getText() - .slice( - tokenBeforeRightParen.range[1], - rightParen.range[0], - ) - .trim() - ? // If there is a comment between the last element and the ), don't do a fix. - null - : fixer.removeRange([ - tokenBeforeRightParen.range[1], - rightParen.range[0], - ]); - }, - }); - } else if (!hasRightNewline && needsNewlines) { - context.report({ - node: rightParen, - messageId: "expectedBefore", - fix: fixer => fixer.insertTextBefore(rightParen, "\n"), - }); - } - } - - /** - * Validates a list of arguments or parameters - * @param {Object} parens An object with keys `leftParen` for the left paren token, and `rightParen` for the right paren token - * @param {ASTNode[]} elements The arguments or parameters in the list - * @returns {void} - */ - function validateArguments(parens, elements) { - const leftParen = parens.leftParen; - const tokenAfterLeftParen = sourceCode.getTokenAfter(leftParen); - const hasLeftNewline = !astUtils.isTokenOnSameLine( - leftParen, - tokenAfterLeftParen, - ); - const needsNewlines = shouldHaveNewlines(elements, hasLeftNewline); - - for (let i = 0; i <= elements.length - 2; i++) { - const currentElement = elements[i]; - const nextElement = elements[i + 1]; - const hasNewLine = - currentElement.loc.end.line !== nextElement.loc.start.line; - - if (!hasNewLine && needsNewlines) { - context.report({ - node: currentElement, - messageId: "expectedBetween", - fix: fixer => fixer.insertTextBefore(nextElement, "\n"), - }); - } - } - } - - /** - * Gets the left paren and right paren tokens of a node. - * @param {ASTNode} node The node with parens - * @throws {TypeError} Unexpected node type. - * @returns {Object} An object with keys `leftParen` for the left paren token, and `rightParen` for the right paren token. - * Can also return `null` if an expression has no parens (e.g. a NewExpression with no arguments, or an ArrowFunctionExpression - * with a single parameter) - */ - function getParenTokens(node) { - switch (node.type) { - case "NewExpression": - if ( - !node.arguments.length && - !( - astUtils.isOpeningParenToken( - sourceCode.getLastToken(node, { skip: 1 }), - ) && - astUtils.isClosingParenToken( - sourceCode.getLastToken(node), - ) && - node.callee.range[1] < node.range[1] - ) - ) { - // If the NewExpression does not have parens (e.g. `new Foo`), return null. - return null; - } - - // falls through - - case "CallExpression": - return { - leftParen: sourceCode.getTokenAfter( - node.callee, - astUtils.isOpeningParenToken, - ), - rightParen: sourceCode.getLastToken(node), - }; - - case "FunctionDeclaration": - case "FunctionExpression": { - const leftParen = sourceCode.getFirstToken( - node, - astUtils.isOpeningParenToken, - ); - const rightParen = node.params.length - ? sourceCode.getTokenAfter( - node.params.at(-1), - astUtils.isClosingParenToken, - ) - : sourceCode.getTokenAfter(leftParen); - - return { leftParen, rightParen }; - } - - case "ArrowFunctionExpression": { - const firstToken = sourceCode.getFirstToken(node, { - skip: node.async ? 1 : 0, - }); - - if (!astUtils.isOpeningParenToken(firstToken)) { - // If the ArrowFunctionExpression has a single param without parens, return null. - return null; - } - - const rightParen = node.params.length - ? sourceCode.getTokenAfter( - node.params.at(-1), - astUtils.isClosingParenToken, - ) - : sourceCode.getTokenAfter(firstToken); - - return { - leftParen: firstToken, - rightParen, - }; - } - - case "ImportExpression": { - const leftParen = sourceCode.getFirstToken(node, 1); - const rightParen = sourceCode.getLastToken(node); - - return { leftParen, rightParen }; - } - - default: - throw new TypeError( - `unexpected node with type ${node.type}`, - ); - } - } - - //---------------------------------------------------------------------- - // Public - //---------------------------------------------------------------------- - - return { - [[ - "ArrowFunctionExpression", - "CallExpression", - "FunctionDeclaration", - "FunctionExpression", - "ImportExpression", - "NewExpression", - ]](node) { - const parens = getParenTokens(node); - let params; - - if (node.type === "ImportExpression") { - params = [node.source]; - } else if (astUtils.isFunction(node)) { - params = node.params; - } else { - params = node.arguments; - } - - if (parens) { - validateParens(parens, params); - - if (multilineArgumentsOption) { - validateArguments(parens, params); - } - } - }, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Enforce consistent line breaks inside function parentheses", + recommended: false, + url: "https://eslint.org/docs/latest/rules/function-paren-newline" + }, + + fixable: "whitespace", + + schema: [ + { + oneOf: [ + { + enum: ["always", "never", "consistent", "multiline", "multiline-arguments"] + }, + { + type: "object", + properties: { + minItems: { + type: "integer", + minimum: 0 + } + }, + additionalProperties: false + } + ] + } + ], + + messages: { + expectedBefore: "Expected newline before ')'.", + expectedAfter: "Expected newline after '('.", + expectedBetween: "Expected newline between arguments/params.", + unexpectedBefore: "Unexpected newline before ')'.", + unexpectedAfter: "Unexpected newline after '('." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + const rawOption = context.options[0] || "multiline"; + const multilineOption = rawOption === "multiline"; + const multilineArgumentsOption = rawOption === "multiline-arguments"; + const consistentOption = rawOption === "consistent"; + let minItems; + + if (typeof rawOption === "object") { + minItems = rawOption.minItems; + } else if (rawOption === "always") { + minItems = 0; + } else if (rawOption === "never") { + minItems = Infinity; + } else { + minItems = null; + } + + //---------------------------------------------------------------------- + // Helpers + //---------------------------------------------------------------------- + + /** + * Determines whether there should be newlines inside function parens + * @param {ASTNode[]} elements The arguments or parameters in the list + * @param {boolean} hasLeftNewline `true` if the left paren has a newline in the current code. + * @returns {boolean} `true` if there should be newlines inside the function parens + */ + function shouldHaveNewlines(elements, hasLeftNewline) { + if (multilineArgumentsOption && elements.length === 1) { + return hasLeftNewline; + } + if (multilineOption || multilineArgumentsOption) { + return elements.some((element, index) => index !== elements.length - 1 && element.loc.end.line !== elements[index + 1].loc.start.line); + } + if (consistentOption) { + return hasLeftNewline; + } + return elements.length >= minItems; + } + + /** + * Validates parens + * @param {Object} parens An object with keys `leftParen` for the left paren token, and `rightParen` for the right paren token + * @param {ASTNode[]} elements The arguments or parameters in the list + * @returns {void} + */ + function validateParens(parens, elements) { + const leftParen = parens.leftParen; + const rightParen = parens.rightParen; + const tokenAfterLeftParen = sourceCode.getTokenAfter(leftParen); + const tokenBeforeRightParen = sourceCode.getTokenBefore(rightParen); + const hasLeftNewline = !astUtils.isTokenOnSameLine(leftParen, tokenAfterLeftParen); + const hasRightNewline = !astUtils.isTokenOnSameLine(tokenBeforeRightParen, rightParen); + const needsNewlines = shouldHaveNewlines(elements, hasLeftNewline); + + if (hasLeftNewline && !needsNewlines) { + context.report({ + node: leftParen, + messageId: "unexpectedAfter", + fix(fixer) { + return sourceCode.getText().slice(leftParen.range[1], tokenAfterLeftParen.range[0]).trim() + + // If there is a comment between the ( and the first element, don't do a fix. + ? null + : fixer.removeRange([leftParen.range[1], tokenAfterLeftParen.range[0]]); + } + }); + } else if (!hasLeftNewline && needsNewlines) { + context.report({ + node: leftParen, + messageId: "expectedAfter", + fix: fixer => fixer.insertTextAfter(leftParen, "\n") + }); + } + + if (hasRightNewline && !needsNewlines) { + context.report({ + node: rightParen, + messageId: "unexpectedBefore", + fix(fixer) { + return sourceCode.getText().slice(tokenBeforeRightParen.range[1], rightParen.range[0]).trim() + + // If there is a comment between the last element and the ), don't do a fix. + ? null + : fixer.removeRange([tokenBeforeRightParen.range[1], rightParen.range[0]]); + } + }); + } else if (!hasRightNewline && needsNewlines) { + context.report({ + node: rightParen, + messageId: "expectedBefore", + fix: fixer => fixer.insertTextBefore(rightParen, "\n") + }); + } + } + + /** + * Validates a list of arguments or parameters + * @param {Object} parens An object with keys `leftParen` for the left paren token, and `rightParen` for the right paren token + * @param {ASTNode[]} elements The arguments or parameters in the list + * @returns {void} + */ + function validateArguments(parens, elements) { + const leftParen = parens.leftParen; + const tokenAfterLeftParen = sourceCode.getTokenAfter(leftParen); + const hasLeftNewline = !astUtils.isTokenOnSameLine(leftParen, tokenAfterLeftParen); + const needsNewlines = shouldHaveNewlines(elements, hasLeftNewline); + + for (let i = 0; i <= elements.length - 2; i++) { + const currentElement = elements[i]; + const nextElement = elements[i + 1]; + const hasNewLine = currentElement.loc.end.line !== nextElement.loc.start.line; + + if (!hasNewLine && needsNewlines) { + context.report({ + node: currentElement, + messageId: "expectedBetween", + fix: fixer => fixer.insertTextBefore(nextElement, "\n") + }); + } + } + } + + /** + * Gets the left paren and right paren tokens of a node. + * @param {ASTNode} node The node with parens + * @throws {TypeError} Unexpected node type. + * @returns {Object} An object with keys `leftParen` for the left paren token, and `rightParen` for the right paren token. + * Can also return `null` if an expression has no parens (e.g. a NewExpression with no arguments, or an ArrowFunctionExpression + * with a single parameter) + */ + function getParenTokens(node) { + switch (node.type) { + case "NewExpression": + if (!node.arguments.length && + !( + astUtils.isOpeningParenToken(sourceCode.getLastToken(node, { skip: 1 })) && + astUtils.isClosingParenToken(sourceCode.getLastToken(node)) && + node.callee.range[1] < node.range[1] + ) + ) { + + // If the NewExpression does not have parens (e.g. `new Foo`), return null. + return null; + } + + // falls through + + case "CallExpression": + return { + leftParen: sourceCode.getTokenAfter(node.callee, astUtils.isOpeningParenToken), + rightParen: sourceCode.getLastToken(node) + }; + + case "FunctionDeclaration": + case "FunctionExpression": { + const leftParen = sourceCode.getFirstToken(node, astUtils.isOpeningParenToken); + const rightParen = node.params.length + ? sourceCode.getTokenAfter(node.params[node.params.length - 1], astUtils.isClosingParenToken) + : sourceCode.getTokenAfter(leftParen); + + return { leftParen, rightParen }; + } + + case "ArrowFunctionExpression": { + const firstToken = sourceCode.getFirstToken(node, { skip: (node.async ? 1 : 0) }); + + if (!astUtils.isOpeningParenToken(firstToken)) { + + // If the ArrowFunctionExpression has a single param without parens, return null. + return null; + } + + const rightParen = node.params.length + ? sourceCode.getTokenAfter(node.params[node.params.length - 1], astUtils.isClosingParenToken) + : sourceCode.getTokenAfter(firstToken); + + return { + leftParen: firstToken, + rightParen + }; + } + + case "ImportExpression": { + const leftParen = sourceCode.getFirstToken(node, 1); + const rightParen = sourceCode.getLastToken(node); + + return { leftParen, rightParen }; + } + + default: + throw new TypeError(`unexpected node with type ${node.type}`); + } + } + + //---------------------------------------------------------------------- + // Public + //---------------------------------------------------------------------- + + return { + [[ + "ArrowFunctionExpression", + "CallExpression", + "FunctionDeclaration", + "FunctionExpression", + "ImportExpression", + "NewExpression" + ]](node) { + const parens = getParenTokens(node); + let params; + + if (node.type === "ImportExpression") { + params = [node.source]; + } else if (astUtils.isFunction(node)) { + params = node.params; + } else { + params = node.arguments; + } + + if (parens) { + validateParens(parens, params); + + if (multilineArgumentsOption) { + validateArguments(parens, params); + } + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/generator-star-spacing.js b/node_modules/eslint/lib/rules/generator-star-spacing.js index 1c980baf2..c633f979f 100644 --- a/node_modules/eslint/lib/rules/generator-star-spacing.js +++ b/node_modules/eslint/lib/rules/generator-star-spacing.js @@ -11,236 +11,199 @@ //------------------------------------------------------------------------------ const OVERRIDE_SCHEMA = { - oneOf: [ - { - enum: ["before", "after", "both", "neither"], - }, - { - type: "object", - properties: { - before: { type: "boolean" }, - after: { type: "boolean" }, - }, - additionalProperties: false, - }, - ], + oneOf: [ + { + enum: ["before", "after", "both", "neither"] + }, + { + type: "object", + properties: { + before: { type: "boolean" }, + after: { type: "boolean" } + }, + additionalProperties: false + } + ] }; -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "generator-star-spacing", - url: "https://eslint.style/rules/generator-star-spacing", - }, - }, - ], - }, - type: "layout", - - docs: { - description: - "Enforce consistent spacing around `*` operators in generator functions", - recommended: false, - url: "https://eslint.org/docs/latest/rules/generator-star-spacing", - }, - - fixable: "whitespace", - - schema: [ - { - oneOf: [ - { - enum: ["before", "after", "both", "neither"], - }, - { - type: "object", - properties: { - before: { type: "boolean" }, - after: { type: "boolean" }, - named: OVERRIDE_SCHEMA, - anonymous: OVERRIDE_SCHEMA, - method: OVERRIDE_SCHEMA, - }, - additionalProperties: false, - }, - ], - }, - ], - - messages: { - missingBefore: "Missing space before *.", - missingAfter: "Missing space after *.", - unexpectedBefore: "Unexpected space before *.", - unexpectedAfter: "Unexpected space after *.", - }, - }, - - create(context) { - const optionDefinitions = { - before: { before: true, after: false }, - after: { before: false, after: true }, - both: { before: true, after: true }, - neither: { before: false, after: false }, - }; - - /** - * Returns resolved option definitions based on an option and defaults - * @param {any} option The option object or string value - * @param {Object} defaults The defaults to use if options are not present - * @returns {Object} the resolved object definition - */ - function optionToDefinition(option, defaults) { - if (!option) { - return defaults; - } - - return typeof option === "string" - ? optionDefinitions[option] - : Object.assign({}, defaults, option); - } - - const modes = (function (option) { - const defaults = optionToDefinition( - option, - optionDefinitions.before, - ); - - return { - named: optionToDefinition(option.named, defaults), - anonymous: optionToDefinition(option.anonymous, defaults), - method: optionToDefinition(option.method, defaults), - }; - })(context.options[0] || {}); - - const sourceCode = context.sourceCode; - - /** - * Checks if the given token is a star token or not. - * @param {Token} token The token to check. - * @returns {boolean} `true` if the token is a star token. - */ - function isStarToken(token) { - return token.value === "*" && token.type === "Punctuator"; - } - - /** - * Gets the generator star token of the given function node. - * @param {ASTNode} node The function node to get. - * @returns {Token} Found star token. - */ - function getStarToken(node) { - return sourceCode.getFirstToken( - node.parent.method || node.parent.type === "MethodDefinition" - ? node.parent - : node, - isStarToken, - ); - } - - /** - * capitalize a given string. - * @param {string} str the given string. - * @returns {string} the capitalized string. - */ - function capitalize(str) { - return str[0].toUpperCase() + str.slice(1); - } - - /** - * Checks the spacing between two tokens before or after the star token. - * @param {string} kind Either "named", "anonymous", or "method" - * @param {string} side Either "before" or "after". - * @param {Token} leftToken `function` keyword token if side is "before", or - * star token if side is "after". - * @param {Token} rightToken Star token if side is "before", or identifier - * token if side is "after". - * @returns {void} - */ - function checkSpacing(kind, side, leftToken, rightToken) { - if ( - !!(rightToken.range[0] - leftToken.range[1]) !== - modes[kind][side] - ) { - const after = leftToken.value === "*"; - const spaceRequired = modes[kind][side]; - const node = after ? leftToken : rightToken; - const messageId = `${spaceRequired ? "missing" : "unexpected"}${capitalize(side)}`; - - context.report({ - node, - messageId, - fix(fixer) { - if (spaceRequired) { - if (after) { - return fixer.insertTextAfter(node, " "); - } - return fixer.insertTextBefore(node, " "); - } - return fixer.removeRange([ - leftToken.range[1], - rightToken.range[0], - ]); - }, - }); - } - } - - /** - * Enforces the spacing around the star if node is a generator function. - * @param {ASTNode} node A function expression or declaration node. - * @returns {void} - */ - function checkFunction(node) { - if (!node.generator) { - return; - } - - const starToken = getStarToken(node); - const prevToken = sourceCode.getTokenBefore(starToken); - const nextToken = sourceCode.getTokenAfter(starToken); - - let kind = "named"; - - if ( - node.parent.type === "MethodDefinition" || - (node.parent.type === "Property" && node.parent.method) - ) { - kind = "method"; - } else if (!node.id) { - kind = "anonymous"; - } - - // Only check before when preceded by `function`|`static` keyword - if ( - !( - kind === "method" && - starToken === sourceCode.getFirstToken(node.parent) - ) - ) { - checkSpacing(kind, "before", prevToken, starToken); - } - - checkSpacing(kind, "after", starToken, nextToken); - } - - return { - FunctionDeclaration: checkFunction, - FunctionExpression: checkFunction, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Enforce consistent spacing around `*` operators in generator functions", + recommended: false, + url: "https://eslint.org/docs/latest/rules/generator-star-spacing" + }, + + fixable: "whitespace", + + schema: [ + { + oneOf: [ + { + enum: ["before", "after", "both", "neither"] + }, + { + type: "object", + properties: { + before: { type: "boolean" }, + after: { type: "boolean" }, + named: OVERRIDE_SCHEMA, + anonymous: OVERRIDE_SCHEMA, + method: OVERRIDE_SCHEMA + }, + additionalProperties: false + } + ] + } + ], + + messages: { + missingBefore: "Missing space before *.", + missingAfter: "Missing space after *.", + unexpectedBefore: "Unexpected space before *.", + unexpectedAfter: "Unexpected space after *." + } + }, + + create(context) { + + const optionDefinitions = { + before: { before: true, after: false }, + after: { before: false, after: true }, + both: { before: true, after: true }, + neither: { before: false, after: false } + }; + + /** + * Returns resolved option definitions based on an option and defaults + * @param {any} option The option object or string value + * @param {Object} defaults The defaults to use if options are not present + * @returns {Object} the resolved object definition + */ + function optionToDefinition(option, defaults) { + if (!option) { + return defaults; + } + + return typeof option === "string" + ? optionDefinitions[option] + : Object.assign({}, defaults, option); + } + + const modes = (function(option) { + const defaults = optionToDefinition(option, optionDefinitions.before); + + return { + named: optionToDefinition(option.named, defaults), + anonymous: optionToDefinition(option.anonymous, defaults), + method: optionToDefinition(option.method, defaults) + }; + }(context.options[0] || {})); + + const sourceCode = context.sourceCode; + + /** + * Checks if the given token is a star token or not. + * @param {Token} token The token to check. + * @returns {boolean} `true` if the token is a star token. + */ + function isStarToken(token) { + return token.value === "*" && token.type === "Punctuator"; + } + + /** + * Gets the generator star token of the given function node. + * @param {ASTNode} node The function node to get. + * @returns {Token} Found star token. + */ + function getStarToken(node) { + return sourceCode.getFirstToken( + (node.parent.method || node.parent.type === "MethodDefinition") ? node.parent : node, + isStarToken + ); + } + + /** + * capitalize a given string. + * @param {string} str the given string. + * @returns {string} the capitalized string. + */ + function capitalize(str) { + return str[0].toUpperCase() + str.slice(1); + } + + /** + * Checks the spacing between two tokens before or after the star token. + * @param {string} kind Either "named", "anonymous", or "method" + * @param {string} side Either "before" or "after". + * @param {Token} leftToken `function` keyword token if side is "before", or + * star token if side is "after". + * @param {Token} rightToken Star token if side is "before", or identifier + * token if side is "after". + * @returns {void} + */ + function checkSpacing(kind, side, leftToken, rightToken) { + if (!!(rightToken.range[0] - leftToken.range[1]) !== modes[kind][side]) { + const after = leftToken.value === "*"; + const spaceRequired = modes[kind][side]; + const node = after ? leftToken : rightToken; + const messageId = `${spaceRequired ? "missing" : "unexpected"}${capitalize(side)}`; + + context.report({ + node, + messageId, + fix(fixer) { + if (spaceRequired) { + if (after) { + return fixer.insertTextAfter(node, " "); + } + return fixer.insertTextBefore(node, " "); + } + return fixer.removeRange([leftToken.range[1], rightToken.range[0]]); + } + }); + } + } + + /** + * Enforces the spacing around the star if node is a generator function. + * @param {ASTNode} node A function expression or declaration node. + * @returns {void} + */ + function checkFunction(node) { + if (!node.generator) { + return; + } + + const starToken = getStarToken(node); + const prevToken = sourceCode.getTokenBefore(starToken); + const nextToken = sourceCode.getTokenAfter(starToken); + + let kind = "named"; + + if (node.parent.type === "MethodDefinition" || (node.parent.type === "Property" && node.parent.method)) { + kind = "method"; + } else if (!node.id) { + kind = "anonymous"; + } + + // Only check before when preceded by `function`|`static` keyword + if (!(kind === "method" && starToken === sourceCode.getFirstToken(node.parent))) { + checkSpacing(kind, "before", prevToken, starToken); + } + + checkSpacing(kind, "after", starToken, nextToken); + } + + return { + FunctionDeclaration: checkFunction, + FunctionExpression: checkFunction + }; + + } }; diff --git a/node_modules/eslint/lib/rules/getter-return.js b/node_modules/eslint/lib/rules/getter-return.js index 9915597d7..79ebf3e09 100644 --- a/node_modules/eslint/lib/rules/getter-return.js +++ b/node_modules/eslint/lib/rules/getter-return.js @@ -23,220 +23,182 @@ const TARGET_NODE_TYPE = /^(?:Arrow)?FunctionExpression$/u; * @returns {boolean} True if any segment is reachable; false otherwise. */ function isAnySegmentReachable(segments) { - for (const segment of segments) { - if (segment.reachable) { - return true; - } - } - return false; + for (const segment of segments) { + if (segment.reachable) { + return true; + } + } + + return false; } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - defaultOptions: [ - { - allowImplicit: false, - }, - ], - - docs: { - description: "Enforce `return` statements in getters", - recommended: true, - url: "https://eslint.org/docs/latest/rules/getter-return", - }, - - fixable: null, - - schema: [ - { - type: "object", - properties: { - allowImplicit: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - - messages: { - expected: "Expected to return a value in {{name}}.", - expectedAlways: "Expected {{name}} to always return a value.", - }, - }, - - create(context) { - const [{ allowImplicit }] = context.options; - const sourceCode = context.sourceCode; - - let funcInfo = { - upper: null, - codePath: null, - hasReturn: false, - shouldCheck: false, - node: null, - currentSegments: [], - }; - - /** - * Checks whether or not the last code path segment is reachable. - * Then reports this function if the segment is reachable. - * - * If the last code path segment is reachable, there are paths which are not - * returned or thrown. - * @param {ASTNode} node A node to check. - * @returns {void} - */ - function checkLastSegment(node) { - if ( - funcInfo.shouldCheck && - isAnySegmentReachable(funcInfo.currentSegments) - ) { - context.report({ - node, - loc: astUtils.getFunctionHeadLoc(node, sourceCode), - messageId: funcInfo.hasReturn - ? "expectedAlways" - : "expected", - data: { - name: astUtils.getFunctionNameWithKind(funcInfo.node), - }, - }); - } - } - - /** - * Checks whether a node means a getter function. - * @param {ASTNode} node a node to check. - * @returns {boolean} if node means a getter, return true; else return false. - */ - function isGetter(node) { - const parent = node.parent; - - if ( - TARGET_NODE_TYPE.test(node.type) && - node.body.type === "BlockStatement" - ) { - if (parent.kind === "get") { - return true; - } - if ( - parent.type === "Property" && - astUtils.getStaticPropertyName(parent) === "get" && - parent.parent.type === "ObjectExpression" - ) { - // Object.defineProperty() or Reflect.defineProperty() - if (parent.parent.parent.type === "CallExpression") { - const callNode = parent.parent.parent.callee; - - if ( - astUtils.isSpecificMemberAccess( - callNode, - "Object", - "defineProperty", - ) || - astUtils.isSpecificMemberAccess( - callNode, - "Reflect", - "defineProperty", - ) - ) { - return true; - } - } - - // Object.defineProperties() or Object.create() - if ( - parent.parent.parent.type === "Property" && - parent.parent.parent.parent.type === - "ObjectExpression" && - parent.parent.parent.parent.parent.type === - "CallExpression" - ) { - const callNode = - parent.parent.parent.parent.parent.callee; - - return ( - astUtils.isSpecificMemberAccess( - callNode, - "Object", - "defineProperties", - ) || - astUtils.isSpecificMemberAccess( - callNode, - "Object", - "create", - ) - ); - } - } - } - return false; - } - return { - // Stacks this function's information. - onCodePathStart(codePath, node) { - funcInfo = { - upper: funcInfo, - codePath, - hasReturn: false, - shouldCheck: isGetter(node), - node, - currentSegments: new Set(), - }; - }, - - // Pops this function's information. - onCodePathEnd() { - funcInfo = funcInfo.upper; - }, - onUnreachableCodePathSegmentStart(segment) { - funcInfo.currentSegments.add(segment); - }, - - onUnreachableCodePathSegmentEnd(segment) { - funcInfo.currentSegments.delete(segment); - }, - - onCodePathSegmentStart(segment) { - funcInfo.currentSegments.add(segment); - }, - - onCodePathSegmentEnd(segment) { - funcInfo.currentSegments.delete(segment); - }, - - // Checks the return statement is valid. - ReturnStatement(node) { - if (funcInfo.shouldCheck) { - funcInfo.hasReturn = true; - - // if allowImplicit: false, should also check node.argument - if (!allowImplicit && !node.argument) { - context.report({ - node, - messageId: "expected", - data: { - name: astUtils.getFunctionNameWithKind( - funcInfo.node, - ), - }, - }); - } - } - }, - - // Reports a given function if the last path is reachable. - "FunctionExpression:exit": checkLastSegment, - "ArrowFunctionExpression:exit": checkLastSegment, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Enforce `return` statements in getters", + recommended: true, + url: "https://eslint.org/docs/latest/rules/getter-return" + }, + + fixable: null, + + schema: [ + { + type: "object", + properties: { + allowImplicit: { + type: "boolean", + default: false + } + }, + additionalProperties: false + } + ], + + messages: { + expected: "Expected to return a value in {{name}}.", + expectedAlways: "Expected {{name}} to always return a value." + } + }, + + create(context) { + + const options = context.options[0] || { allowImplicit: false }; + const sourceCode = context.sourceCode; + + let funcInfo = { + upper: null, + codePath: null, + hasReturn: false, + shouldCheck: false, + node: null, + currentSegments: [] + }; + + /** + * Checks whether or not the last code path segment is reachable. + * Then reports this function if the segment is reachable. + * + * If the last code path segment is reachable, there are paths which are not + * returned or thrown. + * @param {ASTNode} node A node to check. + * @returns {void} + */ + function checkLastSegment(node) { + if (funcInfo.shouldCheck && + isAnySegmentReachable(funcInfo.currentSegments) + ) { + context.report({ + node, + loc: astUtils.getFunctionHeadLoc(node, sourceCode), + messageId: funcInfo.hasReturn ? "expectedAlways" : "expected", + data: { + name: astUtils.getFunctionNameWithKind(funcInfo.node) + } + }); + } + } + + /** + * Checks whether a node means a getter function. + * @param {ASTNode} node a node to check. + * @returns {boolean} if node means a getter, return true; else return false. + */ + function isGetter(node) { + const parent = node.parent; + + if (TARGET_NODE_TYPE.test(node.type) && node.body.type === "BlockStatement") { + if (parent.kind === "get") { + return true; + } + if (parent.type === "Property" && astUtils.getStaticPropertyName(parent) === "get" && parent.parent.type === "ObjectExpression") { + + // Object.defineProperty() or Reflect.defineProperty() + if (parent.parent.parent.type === "CallExpression") { + const callNode = parent.parent.parent.callee; + + if (astUtils.isSpecificMemberAccess(callNode, "Object", "defineProperty") || + astUtils.isSpecificMemberAccess(callNode, "Reflect", "defineProperty")) { + return true; + } + } + + // Object.defineProperties() or Object.create() + if (parent.parent.parent.type === "Property" && + parent.parent.parent.parent.type === "ObjectExpression" && + parent.parent.parent.parent.parent.type === "CallExpression") { + const callNode = parent.parent.parent.parent.parent.callee; + + return astUtils.isSpecificMemberAccess(callNode, "Object", "defineProperties") || + astUtils.isSpecificMemberAccess(callNode, "Object", "create"); + } + } + } + return false; + } + return { + + // Stacks this function's information. + onCodePathStart(codePath, node) { + funcInfo = { + upper: funcInfo, + codePath, + hasReturn: false, + shouldCheck: isGetter(node), + node, + currentSegments: new Set() + }; + }, + + // Pops this function's information. + onCodePathEnd() { + funcInfo = funcInfo.upper; + }, + onUnreachableCodePathSegmentStart(segment) { + funcInfo.currentSegments.add(segment); + }, + + onUnreachableCodePathSegmentEnd(segment) { + funcInfo.currentSegments.delete(segment); + }, + + onCodePathSegmentStart(segment) { + funcInfo.currentSegments.add(segment); + }, + + onCodePathSegmentEnd(segment) { + funcInfo.currentSegments.delete(segment); + }, + + // Checks the return statement is valid. + ReturnStatement(node) { + if (funcInfo.shouldCheck) { + funcInfo.hasReturn = true; + + // if allowImplicit: false, should also check node.argument + if (!options.allowImplicit && !node.argument) { + context.report({ + node, + messageId: "expected", + data: { + name: astUtils.getFunctionNameWithKind(funcInfo.node) + } + }); + } + } + }, + + // Reports a given function if the last path is reachable. + "FunctionExpression:exit": checkLastSegment, + "ArrowFunctionExpression:exit": checkLastSegment + }; + } }; diff --git a/node_modules/eslint/lib/rules/global-require.js b/node_modules/eslint/lib/rules/global-require.js index 44b31814b..deae9d267 100644 --- a/node_modules/eslint/lib/rules/global-require.js +++ b/node_modules/eslint/lib/rules/global-require.js @@ -7,15 +7,15 @@ "use strict"; const ACCEPTABLE_PARENTS = new Set([ - "AssignmentExpression", - "VariableDeclarator", - "MemberExpression", - "ExpressionStatement", - "CallExpression", - "ConditionalExpression", - "Program", - "VariableDeclaration", - "ChainExpression", + "AssignmentExpression", + "VariableDeclarator", + "MemberExpression", + "ExpressionStatement", + "CallExpression", + "ConditionalExpression", + "Program", + "VariableDeclaration", + "ChainExpression" ]); /** @@ -25,18 +25,16 @@ const ACCEPTABLE_PARENTS = new Set([ * @returns {Reference|null} Returns the found reference or null if none were found. */ function findReference(scope, node) { - const references = scope.references.filter( - reference => - reference.identifier.range[0] === node.range[0] && - reference.identifier.range[1] === node.range[1], - ); - - if (references.length === 1) { - return references[0]; - } - - /* c8 ignore next */ - return null; + const references = scope.references.filter(reference => reference.identifier.range[0] === node.range[0] && + reference.identifier.range[1] === node.range[1]); + + if (references.length === 1) { + return references[0]; + } + + /* c8 ignore next */ + return null; + } /** @@ -46,72 +44,47 @@ function findReference(scope, node) { * @returns {boolean} Whether or not the name is shadowed. */ function isShadowed(scope, node) { - const reference = findReference(scope, node); + const reference = findReference(scope, node); - return ( - reference && reference.resolved && reference.resolved.defs.length > 0 - ); + return reference && reference.resolved && reference.resolved.defs.length > 0; } -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Node.js rules were moved out of ESLint core.", - url: "https://eslint.org/docs/latest/use/migrating-to-7.0.0#deprecate-node-rules", - deprecatedSince: "7.0.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "eslint-plugin-n now maintains deprecated Node.js-related rules.", - plugin: { - name: "eslint-plugin-n", - url: "https://github.com/eslint-community/eslint-plugin-n", - }, - rule: { - name: "global-require", - url: "https://github.com/eslint-community/eslint-plugin-n/tree/master/docs/rules/global-require.md", - }, - }, - ], - }, - - type: "suggestion", - - docs: { - description: - "Require `require()` calls to be placed at top-level module scope", - recommended: false, - url: "https://eslint.org/docs/latest/rules/global-require", - }, - - schema: [], - messages: { - unexpected: "Unexpected require().", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - return { - CallExpression(node) { - const currentScope = sourceCode.getScope(node); - - if ( - node.callee.name === "require" && - !isShadowed(currentScope, node.callee) - ) { - const isGoodRequire = sourceCode - .getAncestors(node) - .every(parent => ACCEPTABLE_PARENTS.has(parent.type)); - - if (!isGoodRequire) { - context.report({ node, messageId: "unexpected" }); - } - } - }, - }; - }, + meta: { + deprecated: true, + + replacedBy: [], + + type: "suggestion", + + docs: { + description: "Require `require()` calls to be placed at top-level module scope", + recommended: false, + url: "https://eslint.org/docs/latest/rules/global-require" + }, + + schema: [], + messages: { + unexpected: "Unexpected require()." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + + return { + CallExpression(node) { + const currentScope = sourceCode.getScope(node); + + if (node.callee.name === "require" && !isShadowed(currentScope, node.callee)) { + const isGoodRequire = sourceCode.getAncestors(node).every(parent => ACCEPTABLE_PARENTS.has(parent.type)); + + if (!isGoodRequire) { + context.report({ node, messageId: "unexpected" }); + } + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/grouped-accessor-pairs.js b/node_modules/eslint/lib/rules/grouped-accessor-pairs.js index c4ac86dc4..9556f4756 100644 --- a/node_modules/eslint/lib/rules/grouped-accessor-pairs.js +++ b/node_modules/eslint/lib/rules/grouped-accessor-pairs.js @@ -40,23 +40,20 @@ const astUtils = require("./utils/ast-utils"); * @returns {boolean} `true` if the lists have same tokens. */ function areEqualTokenLists(left, right) { - if (left.length !== right.length) { - return false; - } - - for (let i = 0; i < left.length; i++) { - const leftToken = left[i], - rightToken = right[i]; - - if ( - leftToken.type !== rightToken.type || - leftToken.value !== rightToken.value - ) { - return false; - } - } - - return true; + if (left.length !== right.length) { + return false; + } + + for (let i = 0; i < left.length; i++) { + const leftToken = left[i], + rightToken = right[i]; + + if (leftToken.type !== rightToken.type || leftToken.value !== rightToken.value) { + return false; + } + } + + return true; } /** @@ -66,16 +63,18 @@ function areEqualTokenLists(left, right) { * @returns {boolean} `true` if the keys are equal. */ function areEqualKeys(left, right) { - if (typeof left === "string" && typeof right === "string") { - // Statically computed names. - return left === right; - } - if (Array.isArray(left) && Array.isArray(right)) { - // Token lists. - return areEqualTokenLists(left, right); - } - - return false; + if (typeof left === "string" && typeof right === "string") { + + // Statically computed names. + return left === right; + } + if (Array.isArray(left) && Array.isArray(right)) { + + // Token lists. + return areEqualTokenLists(left, right); + } + + return false; } /** @@ -84,185 +83,133 @@ function areEqualKeys(left, right) { * @returns {boolean} `true` if the node is of an accessor kind. */ function isAccessorKind(node) { - return node.kind === "get" || node.kind === "set"; + return node.kind === "get" || node.kind === "set"; } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - defaultOptions: [ - "anyOrder", - { - enforceForTSTypes: false, - }, - ], - - docs: { - description: - "Require grouped accessor pairs in object literals and classes", - recommended: false, - url: "https://eslint.org/docs/latest/rules/grouped-accessor-pairs", - }, - - schema: [ - { enum: ["anyOrder", "getBeforeSet", "setBeforeGet"] }, - { - type: "object", - properties: { - enforceForTSTypes: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - - messages: { - notGrouped: - "Accessor pair {{ formerName }} and {{ latterName }} should be grouped.", - invalidOrder: - "Expected {{ latterName }} to be before {{ formerName }}.", - }, - }, - - create(context) { - const [order, { enforceForTSTypes }] = context.options; - const { sourceCode } = context; - - /** - * Reports the given accessor pair. - * @param {string} messageId messageId to report. - * @param {ASTNode} formerNode getter/setter node that is defined before `latterNode`. - * @param {ASTNode} latterNode getter/setter node that is defined after `formerNode`. - * @returns {void} - * @private - */ - function report(messageId, formerNode, latterNode) { - context.report({ - node: latterNode, - messageId, - loc: astUtils.getFunctionHeadLoc( - latterNode.type !== "TSMethodSignature" - ? latterNode.value - : latterNode, - sourceCode, - ), - data: { - formerName: astUtils.getFunctionNameWithKind( - formerNode.type !== "TSMethodSignature" - ? formerNode.value - : formerNode, - ), - latterName: astUtils.getFunctionNameWithKind( - latterNode.type !== "TSMethodSignature" - ? latterNode.value - : latterNode, - ), - }, - }); - } - - /** - * Checks accessor pairs in the given list of nodes. - * @param {ASTNode[]} nodes The list to check. - * @param {Function} shouldCheck – Predicate that returns `true` if the node should be checked. - * @returns {void} - * @private - */ - function checkList(nodes, shouldCheck) { - const accessors = []; - let found = false; - - for (let i = 0; i < nodes.length; i++) { - const node = nodes[i]; - - if (shouldCheck(node) && isAccessorKind(node)) { - // Creates a new `AccessorData` object for the given getter or setter node. - const name = astUtils.getStaticPropertyName(node); - const key = - name !== null ? name : sourceCode.getTokens(node.key); - - // Merges the given `AccessorData` object into the given accessors list. - for (let j = 0; j < accessors.length; j++) { - const accessor = accessors[j]; - - if (areEqualKeys(accessor.key, key)) { - accessor.getters.push( - ...(node.kind === "get" ? [node] : []), - ); - accessor.setters.push( - ...(node.kind === "set" ? [node] : []), - ); - found = true; - break; - } - } - if (!found) { - accessors.push({ - key, - getters: node.kind === "get" ? [node] : [], - setters: node.kind === "set" ? [node] : [], - }); - } - found = false; - } - } - - for (const { getters, setters } of accessors) { - // Don't report accessor properties that have duplicate getters or setters. - if (getters.length === 1 && setters.length === 1) { - const [getter] = getters, - [setter] = setters, - getterIndex = nodes.indexOf(getter), - setterIndex = nodes.indexOf(setter), - formerNode = - getterIndex < setterIndex ? getter : setter, - latterNode = - getterIndex < setterIndex ? setter : getter; - - if (Math.abs(getterIndex - setterIndex) > 1) { - report("notGrouped", formerNode, latterNode); - } else if ( - (order === "getBeforeSet" && - getterIndex > setterIndex) || - (order === "setBeforeGet" && getterIndex < setterIndex) - ) { - report("invalidOrder", formerNode, latterNode); - } - } - } - } - - return { - ObjectExpression(node) { - checkList(node.properties, n => n.type === "Property"); - }, - ClassBody(node) { - checkList( - node.body, - n => n.type === "MethodDefinition" && !n.static, - ); - checkList( - node.body, - n => n.type === "MethodDefinition" && n.static, - ); - }, - "TSTypeLiteral, TSInterfaceBody"(node) { - if (enforceForTSTypes) { - checkList( - node.type === "TSTypeLiteral" - ? node.members - : node.body, - n => n.type === "TSMethodSignature", - ); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Require grouped accessor pairs in object literals and classes", + recommended: false, + url: "https://eslint.org/docs/latest/rules/grouped-accessor-pairs" + }, + + schema: [ + { + enum: ["anyOrder", "getBeforeSet", "setBeforeGet"] + } + ], + + messages: { + notGrouped: "Accessor pair {{ formerName }} and {{ latterName }} should be grouped.", + invalidOrder: "Expected {{ latterName }} to be before {{ formerName }}." + } + }, + + create(context) { + const order = context.options[0] || "anyOrder"; + const sourceCode = context.sourceCode; + + /** + * Reports the given accessor pair. + * @param {string} messageId messageId to report. + * @param {ASTNode} formerNode getter/setter node that is defined before `latterNode`. + * @param {ASTNode} latterNode getter/setter node that is defined after `formerNode`. + * @returns {void} + * @private + */ + function report(messageId, formerNode, latterNode) { + context.report({ + node: latterNode, + messageId, + loc: astUtils.getFunctionHeadLoc(latterNode.value, sourceCode), + data: { + formerName: astUtils.getFunctionNameWithKind(formerNode.value), + latterName: astUtils.getFunctionNameWithKind(latterNode.value) + } + }); + } + + /** + * Checks accessor pairs in the given list of nodes. + * @param {ASTNode[]} nodes The list to check. + * @param {Function} shouldCheck – Predicate that returns `true` if the node should be checked. + * @returns {void} + * @private + */ + function checkList(nodes, shouldCheck) { + const accessors = []; + let found = false; + + for (let i = 0; i < nodes.length; i++) { + const node = nodes[i]; + + if (shouldCheck(node) && isAccessorKind(node)) { + + // Creates a new `AccessorData` object for the given getter or setter node. + const name = astUtils.getStaticPropertyName(node); + const key = (name !== null) ? name : sourceCode.getTokens(node.key); + + // Merges the given `AccessorData` object into the given accessors list. + for (let j = 0; j < accessors.length; j++) { + const accessor = accessors[j]; + + if (areEqualKeys(accessor.key, key)) { + accessor.getters.push(...node.kind === "get" ? [node] : []); + accessor.setters.push(...node.kind === "set" ? [node] : []); + found = true; + break; + } + } + if (!found) { + accessors.push({ + key, + getters: node.kind === "get" ? [node] : [], + setters: node.kind === "set" ? [node] : [] + }); + } + found = false; + } + } + + for (const { getters, setters } of accessors) { + + // Don't report accessor properties that have duplicate getters or setters. + if (getters.length === 1 && setters.length === 1) { + const [getter] = getters, + [setter] = setters, + getterIndex = nodes.indexOf(getter), + setterIndex = nodes.indexOf(setter), + formerNode = getterIndex < setterIndex ? getter : setter, + latterNode = getterIndex < setterIndex ? setter : getter; + + if (Math.abs(getterIndex - setterIndex) > 1) { + report("notGrouped", formerNode, latterNode); + } else if ( + (order === "getBeforeSet" && getterIndex > setterIndex) || + (order === "setBeforeGet" && getterIndex < setterIndex) + ) { + report("invalidOrder", formerNode, latterNode); + } + } + } + } + + return { + ObjectExpression(node) { + checkList(node.properties, n => n.type === "Property"); + }, + ClassBody(node) { + checkList(node.body, n => n.type === "MethodDefinition" && !n.static); + checkList(node.body, n => n.type === "MethodDefinition" && n.static); + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/guard-for-in.js b/node_modules/eslint/lib/rules/guard-for-in.js index f648aa401..d6e70d0d7 100644 --- a/node_modules/eslint/lib/rules/guard-for-in.js +++ b/node_modules/eslint/lib/rules/guard-for-in.js @@ -9,77 +9,68 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: "Require `for-in` loops to include an `if` statement", - recommended: false, - url: "https://eslint.org/docs/latest/rules/guard-for-in", - }, - - schema: [], - messages: { - wrap: "The body of a for-in should be wrapped in an if statement to filter unwanted properties from the prototype.", - }, - }, - - create(context) { - return { - ForInStatement(node) { - const body = node.body; - - // empty statement - if (body.type === "EmptyStatement") { - return; - } - - // if statement - if (body.type === "IfStatement") { - return; - } - - // empty block - if (body.type === "BlockStatement" && body.body.length === 0) { - return; - } - - // block with just if statement - if ( - body.type === "BlockStatement" && - body.body.length === 1 && - body.body[0].type === "IfStatement" - ) { - return; - } - - // block that starts with if statement - if ( - body.type === "BlockStatement" && - body.body.length >= 1 && - body.body[0].type === "IfStatement" - ) { - const i = body.body[0]; - - // ... whose consequent is a continue - if (i.consequent.type === "ContinueStatement") { - return; - } - - // ... whose consequent is a block that contains only a continue - if ( - i.consequent.type === "BlockStatement" && - i.consequent.body.length === 1 && - i.consequent.body[0].type === "ContinueStatement" - ) { - return; - } - } - - context.report({ node, messageId: "wrap" }); - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Require `for-in` loops to include an `if` statement", + recommended: false, + url: "https://eslint.org/docs/latest/rules/guard-for-in" + }, + + schema: [], + messages: { + wrap: "The body of a for-in should be wrapped in an if statement to filter unwanted properties from the prototype." + } + }, + + create(context) { + + return { + + ForInStatement(node) { + const body = node.body; + + // empty statement + if (body.type === "EmptyStatement") { + return; + } + + // if statement + if (body.type === "IfStatement") { + return; + } + + // empty block + if (body.type === "BlockStatement" && body.body.length === 0) { + return; + } + + // block with just if statement + if (body.type === "BlockStatement" && body.body.length === 1 && body.body[0].type === "IfStatement") { + return; + } + + // block that starts with if statement + if (body.type === "BlockStatement" && body.body.length >= 1 && body.body[0].type === "IfStatement") { + const i = body.body[0]; + + // ... whose consequent is a continue + if (i.consequent.type === "ContinueStatement") { + return; + } + + // ... whose consequent is a block that contains only a continue + if (i.consequent.type === "BlockStatement" && i.consequent.body.length === 1 && i.consequent.body[0].type === "ContinueStatement") { + return; + } + } + + context.report({ node, messageId: "wrap" }); + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/handle-callback-err.js b/node_modules/eslint/lib/rules/handle-callback-err.js index d9b0641a8..ad84931a9 100644 --- a/node_modules/eslint/lib/rules/handle-callback-err.js +++ b/node_modules/eslint/lib/rules/handle-callback-err.js @@ -10,113 +10,92 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Node.js rules were moved out of ESLint core.", - url: "https://eslint.org/docs/latest/use/migrating-to-7.0.0#deprecate-node-rules", - deprecatedSince: "7.0.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "eslint-plugin-n now maintains deprecated Node.js-related rules.", - plugin: { - name: "eslint-plugin-n", - url: "https://github.com/eslint-community/eslint-plugin-n", - }, - rule: { - name: "handle-callback-err", - url: "https://github.com/eslint-community/eslint-plugin-n/tree/master/docs/rules/handle-callback-err.md", - }, - }, - ], - }, - - type: "suggestion", - - docs: { - description: "Require error handling in callbacks", - recommended: false, - url: "https://eslint.org/docs/latest/rules/handle-callback-err", - }, - - schema: [ - { - type: "string", - }, - ], - messages: { - expected: "Expected error to be handled.", - }, - }, - - create(context) { - const errorArgument = context.options[0] || "err"; - const sourceCode = context.sourceCode; - - /** - * Checks if the given argument should be interpreted as a regexp pattern. - * @param {string} stringToCheck The string which should be checked. - * @returns {boolean} Whether or not the string should be interpreted as a pattern. - */ - function isPattern(stringToCheck) { - const firstChar = stringToCheck[0]; - - return firstChar === "^"; - } - - /** - * Checks if the given name matches the configured error argument. - * @param {string} name The name which should be compared. - * @returns {boolean} Whether or not the given name matches the configured error variable name. - */ - function matchesConfiguredErrorName(name) { - if (isPattern(errorArgument)) { - const regexp = new RegExp(errorArgument, "u"); - - return regexp.test(name); - } - return name === errorArgument; - } - - /** - * Get the parameters of a given function scope. - * @param {Object} scope The function scope. - * @returns {Array} All parameters of the given scope. - */ - function getParameters(scope) { - return scope.variables.filter( - variable => - variable.defs[0] && variable.defs[0].type === "Parameter", - ); - } - - /** - * Check to see if we're handling the error object properly. - * @param {ASTNode} node The AST node to check. - * @returns {void} - */ - function checkForError(node) { - const scope = sourceCode.getScope(node), - parameters = getParameters(scope), - firstParameter = parameters[0]; - - if ( - firstParameter && - matchesConfiguredErrorName(firstParameter.name) - ) { - if (firstParameter.references.length === 0) { - context.report({ node, messageId: "expected" }); - } - } - } - - return { - FunctionDeclaration: checkForError, - FunctionExpression: checkForError, - ArrowFunctionExpression: checkForError, - }; - }, + meta: { + deprecated: true, + + replacedBy: [], + + type: "suggestion", + + docs: { + description: "Require error handling in callbacks", + recommended: false, + url: "https://eslint.org/docs/latest/rules/handle-callback-err" + }, + + schema: [ + { + type: "string" + } + ], + messages: { + expected: "Expected error to be handled." + } + }, + + create(context) { + + const errorArgument = context.options[0] || "err"; + const sourceCode = context.sourceCode; + + /** + * Checks if the given argument should be interpreted as a regexp pattern. + * @param {string} stringToCheck The string which should be checked. + * @returns {boolean} Whether or not the string should be interpreted as a pattern. + */ + function isPattern(stringToCheck) { + const firstChar = stringToCheck[0]; + + return firstChar === "^"; + } + + /** + * Checks if the given name matches the configured error argument. + * @param {string} name The name which should be compared. + * @returns {boolean} Whether or not the given name matches the configured error variable name. + */ + function matchesConfiguredErrorName(name) { + if (isPattern(errorArgument)) { + const regexp = new RegExp(errorArgument, "u"); + + return regexp.test(name); + } + return name === errorArgument; + } + + /** + * Get the parameters of a given function scope. + * @param {Object} scope The function scope. + * @returns {Array} All parameters of the given scope. + */ + function getParameters(scope) { + return scope.variables.filter(variable => variable.defs[0] && variable.defs[0].type === "Parameter"); + } + + /** + * Check to see if we're handling the error object properly. + * @param {ASTNode} node The AST node to check. + * @returns {void} + */ + function checkForError(node) { + const scope = sourceCode.getScope(node), + parameters = getParameters(scope), + firstParameter = parameters[0]; + + if (firstParameter && matchesConfiguredErrorName(firstParameter.name)) { + if (firstParameter.references.length === 0) { + context.report({ node, messageId: "expected" }); + } + } + } + + return { + FunctionDeclaration: checkForError, + FunctionExpression: checkForError, + ArrowFunctionExpression: checkForError + }; + + } }; diff --git a/node_modules/eslint/lib/rules/id-blacklist.js b/node_modules/eslint/lib/rules/id-blacklist.js index 6a84827cf..6b7f561ee 100644 --- a/node_modules/eslint/lib/rules/id-blacklist.js +++ b/node_modules/eslint/lib/rules/id-blacklist.js @@ -17,19 +17,29 @@ * @returns {boolean} `true` if the node is assignment target. */ function isAssignmentTarget(node) { - const parent = node.parent; - - return ( - // normal assignment - (parent.type === "AssignmentExpression" && parent.left === node) || - // destructuring - parent.type === "ArrayPattern" || - parent.type === "RestElement" || - (parent.type === "Property" && - parent.value === node && - parent.parent.type === "ObjectPattern") || - (parent.type === "AssignmentPattern" && parent.left === node) - ); + const parent = node.parent; + + return ( + + // normal assignment + ( + parent.type === "AssignmentExpression" && + parent.left === node + ) || + + // destructuring + parent.type === "ArrayPattern" || + parent.type === "RestElement" || + ( + parent.type === "Property" && + parent.value === node && + parent.parent.type === "ObjectPattern" + ) || + ( + parent.type === "AssignmentPattern" && + parent.left === node + ) + ); } /** @@ -42,17 +52,21 @@ function isAssignmentTarget(node) { * @returns {boolean} `true` if the node is a renamed import. */ function isRenamedImport(node) { - const parent = node.parent; - - return ( - (parent.type === "ImportSpecifier" && - parent.imported !== parent.local && - parent.imported === node) || - (parent.type === "ExportSpecifier" && - parent.parent.source && // re-export - parent.local !== parent.exported && - parent.local === node) - ); + const parent = node.parent; + + return ( + ( + parent.type === "ImportSpecifier" && + parent.imported !== parent.local && + parent.imported === node + ) || + ( + parent.type === "ExportSpecifier" && + parent.parent.source && // re-export + parent.local !== parent.exported && + parent.local === node + ) + ); } /** @@ -64,15 +78,17 @@ function isRenamedImport(node) { * @returns {boolean} `true` if the node is a renamed node in an ObjectPattern destructuring. */ function isRenamedInDestructuring(node) { - const parent = node.parent; - - return ( - !parent.computed && - parent.type === "Property" && - parent.parent.type === "ObjectPattern" && - parent.value !== node && - parent.key === node - ); + const parent = node.parent; + + return ( + ( + !parent.computed && + parent.type === "Property" && + parent.parent.type === "ObjectPattern" && + parent.value !== node && + parent.key === node + ) + ); } /** @@ -81,161 +97,150 @@ function isRenamedInDestructuring(node) { * @returns {boolean} `true` if the node is a shorthand property definition. */ function isShorthandPropertyDefinition(node) { - const parent = node.parent; + const parent = node.parent; - return ( - parent.type === "Property" && - parent.parent.type === "ObjectExpression" && - parent.shorthand - ); + return ( + parent.type === "Property" && + parent.parent.type === "ObjectExpression" && + parent.shorthand + ); } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "The rule was renamed.", - url: "https://eslint.org/blog/2020/07/eslint-v7.5.0-released/#deprecating-id-blacklist", - deprecatedSince: "7.5.0", - availableUntil: "11.0.0", - replacedBy: [ - { - rule: { - name: "id-denylist", - url: "https://eslint.org/docs/rules/id-denylist", - }, - }, - ], - }, - - type: "suggestion", - - docs: { - description: "Disallow specified identifiers", - recommended: false, - url: "https://eslint.org/docs/latest/rules/id-blacklist", - }, - - schema: { - type: "array", - items: { - type: "string", - }, - uniqueItems: true, - }, - messages: { - restricted: "Identifier '{{name}}' is restricted.", - }, - }, - - create(context) { - const denyList = new Set(context.options); - const reportedNodes = new Set(); - const sourceCode = context.sourceCode; - - let globalScope; - - /** - * Checks whether the given name is restricted. - * @param {string} name The name to check. - * @returns {boolean} `true` if the name is restricted. - * @private - */ - function isRestricted(name) { - return denyList.has(name); - } - - /** - * Checks whether the given node represents a reference to a global variable that is not declared in the source code. - * These identifiers will be allowed, as it is assumed that user has no control over the names of external global variables. - * @param {ASTNode} node `Identifier` node to check. - * @returns {boolean} `true` if the node is a reference to a global variable. - */ - function isReferenceToGlobalVariable(node) { - const variable = globalScope.set.get(node.name); - - return ( - variable && - variable.defs.length === 0 && - variable.references.some(ref => ref.identifier === node) - ); - } - - /** - * Determines whether the given node should be checked. - * @param {ASTNode} node `Identifier` node. - * @returns {boolean} `true` if the node should be checked. - */ - function shouldCheck(node) { - const parent = node.parent; - - /* - * Member access has special rules for checking property names. - * Read access to a property with a restricted name is allowed, because it can be on an object that user has no control over. - * Write access isn't allowed, because it potentially creates a new property with a restricted name. - */ - if ( - parent.type === "MemberExpression" && - parent.property === node && - !parent.computed - ) { - return isAssignmentTarget(parent); - } - - return ( - parent.type !== "CallExpression" && - parent.type !== "NewExpression" && - !isRenamedImport(node) && - !isRenamedInDestructuring(node) && - !( - isReferenceToGlobalVariable(node) && - !isShorthandPropertyDefinition(node) - ) - ); - } - - /** - * Reports an AST node as a rule violation. - * @param {ASTNode} node The node to report. - * @returns {void} - * @private - */ - function report(node) { - /* - * We used the range instead of the node because it's possible - * for the same identifier to be represented by two different - * nodes, with the most clear example being shorthand properties: - * { foo } - * In this case, "foo" is represented by one node for the name - * and one for the value. The only way to know they are the same - * is to look at the range. - */ - if (!reportedNodes.has(node.range.toString())) { - context.report({ - node, - messageId: "restricted", - data: { - name: node.name, - }, - }); - reportedNodes.add(node.range.toString()); - } - } - - return { - Program(node) { - globalScope = sourceCode.getScope(node); - }, - - Identifier(node) { - if (isRestricted(node.name) && shouldCheck(node)) { - report(node); - } - }, - }; - }, + meta: { + deprecated: true, + replacedBy: ["id-denylist"], + + type: "suggestion", + + docs: { + description: "Disallow specified identifiers", + recommended: false, + url: "https://eslint.org/docs/latest/rules/id-blacklist" + }, + + schema: { + type: "array", + items: { + type: "string" + }, + uniqueItems: true + }, + messages: { + restricted: "Identifier '{{name}}' is restricted." + } + }, + + create(context) { + + const denyList = new Set(context.options); + const reportedNodes = new Set(); + const sourceCode = context.sourceCode; + + let globalScope; + + /** + * Checks whether the given name is restricted. + * @param {string} name The name to check. + * @returns {boolean} `true` if the name is restricted. + * @private + */ + function isRestricted(name) { + return denyList.has(name); + } + + /** + * Checks whether the given node represents a reference to a global variable that is not declared in the source code. + * These identifiers will be allowed, as it is assumed that user has no control over the names of external global variables. + * @param {ASTNode} node `Identifier` node to check. + * @returns {boolean} `true` if the node is a reference to a global variable. + */ + function isReferenceToGlobalVariable(node) { + const variable = globalScope.set.get(node.name); + + return variable && variable.defs.length === 0 && + variable.references.some(ref => ref.identifier === node); + } + + /** + * Determines whether the given node should be checked. + * @param {ASTNode} node `Identifier` node. + * @returns {boolean} `true` if the node should be checked. + */ + function shouldCheck(node) { + const parent = node.parent; + + /* + * Member access has special rules for checking property names. + * Read access to a property with a restricted name is allowed, because it can be on an object that user has no control over. + * Write access isn't allowed, because it potentially creates a new property with a restricted name. + */ + if ( + parent.type === "MemberExpression" && + parent.property === node && + !parent.computed + ) { + return isAssignmentTarget(parent); + } + + return ( + parent.type !== "CallExpression" && + parent.type !== "NewExpression" && + !isRenamedImport(node) && + !isRenamedInDestructuring(node) && + !( + isReferenceToGlobalVariable(node) && + !isShorthandPropertyDefinition(node) + ) + ); + } + + /** + * Reports an AST node as a rule violation. + * @param {ASTNode} node The node to report. + * @returns {void} + * @private + */ + function report(node) { + + /* + * We used the range instead of the node because it's possible + * for the same identifier to be represented by two different + * nodes, with the most clear example being shorthand properties: + * { foo } + * In this case, "foo" is represented by one node for the name + * and one for the value. The only way to know they are the same + * is to look at the range. + */ + if (!reportedNodes.has(node.range.toString())) { + context.report({ + node, + messageId: "restricted", + data: { + name: node.name + } + }); + reportedNodes.add(node.range.toString()); + } + + } + + return { + + Program(node) { + globalScope = sourceCode.getScope(node); + }, + + Identifier(node) { + if (isRestricted(node.name) && shouldCheck(node)) { + report(node); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/id-denylist.js b/node_modules/eslint/lib/rules/id-denylist.js index 5b9a7f4a4..baaa65fe0 100644 --- a/node_modules/eslint/lib/rules/id-denylist.js +++ b/node_modules/eslint/lib/rules/id-denylist.js @@ -6,12 +6,6 @@ "use strict"; -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ - -const astUtils = require("./utils/ast-utils"); - //------------------------------------------------------------------------------ // Helpers //------------------------------------------------------------------------------ @@ -22,19 +16,29 @@ const astUtils = require("./utils/ast-utils"); * @returns {boolean} `true` if the node is assignment target. */ function isAssignmentTarget(node) { - const parent = node.parent; - - return ( - // normal assignment - (parent.type === "AssignmentExpression" && parent.left === node) || - // destructuring - parent.type === "ArrayPattern" || - parent.type === "RestElement" || - (parent.type === "Property" && - parent.value === node && - parent.parent.type === "ObjectPattern") || - (parent.type === "AssignmentPattern" && parent.left === node) - ); + const parent = node.parent; + + return ( + + // normal assignment + ( + parent.type === "AssignmentExpression" && + parent.left === node + ) || + + // destructuring + parent.type === "ArrayPattern" || + parent.type === "RestElement" || + ( + parent.type === "Property" && + parent.value === node && + parent.parent.type === "ObjectPattern" + ) || + ( + parent.type === "AssignmentPattern" && + parent.left === node + ) + ); } /** @@ -47,17 +51,21 @@ function isAssignmentTarget(node) { * @returns {boolean} `true` if the node is a renamed import. */ function isRenamedImport(node) { - const parent = node.parent; - - return ( - (parent.type === "ImportSpecifier" && - parent.imported !== parent.local && - parent.imported === node) || - (parent.type === "ExportSpecifier" && - parent.parent.source && // re-export - parent.local !== parent.exported && - parent.local === node) - ); + const parent = node.parent; + + return ( + ( + parent.type === "ImportSpecifier" && + parent.imported !== parent.local && + parent.imported === node + ) || + ( + parent.type === "ExportSpecifier" && + parent.parent.source && // re-export + parent.local !== parent.exported && + parent.local === node + ) + ); } /** @@ -69,155 +77,152 @@ function isRenamedImport(node) { * @returns {boolean} `true` if the node is in an ObjectPattern destructuring. */ function isPropertyNameInDestructuring(node) { - const parent = node.parent; - - return ( - !parent.computed && - parent.type === "Property" && - parent.parent.type === "ObjectPattern" && - parent.key === node - ); + const parent = node.parent; + + return ( + ( + !parent.computed && + parent.type === "Property" && + parent.parent.type === "ObjectPattern" && + parent.key === node + ) + ); } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - defaultOptions: [], - - docs: { - description: "Disallow specified identifiers", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/id-denylist", - }, - - schema: { - type: "array", - items: { - type: "string", - }, - uniqueItems: true, - }, - messages: { - restricted: "Identifier '{{name}}' is restricted.", - restrictedPrivate: "Identifier '#{{name}}' is restricted.", - }, - }, - - create(context) { - const denyList = new Set(context.options); - const reportedNodes = new Set(); - const sourceCode = context.sourceCode; - - let globalScope; - - /** - * Checks whether the given name is restricted. - * @param {string} name The name to check. - * @returns {boolean} `true` if the name is restricted. - * @private - */ - function isRestricted(name) { - return denyList.has(name); - } - - /** - * Checks whether the given node represents a reference to a global variable that is not declared in the source code. - * These identifiers will be allowed, as it is assumed that user has no control over the names of external global variables. - * @param {ASTNode} node `Identifier` node to check. - * @returns {boolean} `true` if the node is a reference to a global variable. - */ - function isReferenceToGlobalVariable(node) { - const variable = globalScope.set.get(node.name); - - return ( - variable && - variable.defs.length === 0 && - variable.references.some(ref => ref.identifier === node) - ); - } - - /** - * Determines whether the given node should be checked. - * @param {ASTNode} node `Identifier` node. - * @returns {boolean} `true` if the node should be checked. - */ - function shouldCheck(node) { - // Import attributes are defined by environments, so naming conventions shouldn't apply to them - if (astUtils.isImportAttributeKey(node)) { - return false; - } - - const parent = node.parent; - - /* - * Member access has special rules for checking property names. - * Read access to a property with a restricted name is allowed, because it can be on an object that user has no control over. - * Write access isn't allowed, because it potentially creates a new property with a restricted name. - */ - if ( - parent.type === "MemberExpression" && - parent.property === node && - !parent.computed - ) { - return isAssignmentTarget(parent); - } - - return ( - parent.type !== "CallExpression" && - parent.type !== "NewExpression" && - !isRenamedImport(node) && - !isPropertyNameInDestructuring(node) && - !isReferenceToGlobalVariable(node) - ); - } - - /** - * Reports an AST node as a rule violation. - * @param {ASTNode} node The node to report. - * @returns {void} - * @private - */ - function report(node) { - /* - * We used the range instead of the node because it's possible - * for the same identifier to be represented by two different - * nodes, with the most clear example being shorthand properties: - * { foo } - * In this case, "foo" is represented by one node for the name - * and one for the value. The only way to know they are the same - * is to look at the range. - */ - if (!reportedNodes.has(node.range.toString())) { - const isPrivate = node.type === "PrivateIdentifier"; - - context.report({ - node, - messageId: isPrivate ? "restrictedPrivate" : "restricted", - data: { - name: node.name, - }, - }); - reportedNodes.add(node.range.toString()); - } - } - - return { - Program(node) { - globalScope = sourceCode.getScope(node); - }, - - [["Identifier", "PrivateIdentifier"]](node) { - if (isRestricted(node.name) && shouldCheck(node)) { - report(node); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow specified identifiers", + recommended: false, + url: "https://eslint.org/docs/latest/rules/id-denylist" + }, + + schema: { + type: "array", + items: { + type: "string" + }, + uniqueItems: true + }, + messages: { + restricted: "Identifier '{{name}}' is restricted.", + restrictedPrivate: "Identifier '#{{name}}' is restricted." + } + }, + + create(context) { + + const denyList = new Set(context.options); + const reportedNodes = new Set(); + const sourceCode = context.sourceCode; + + let globalScope; + + /** + * Checks whether the given name is restricted. + * @param {string} name The name to check. + * @returns {boolean} `true` if the name is restricted. + * @private + */ + function isRestricted(name) { + return denyList.has(name); + } + + /** + * Checks whether the given node represents a reference to a global variable that is not declared in the source code. + * These identifiers will be allowed, as it is assumed that user has no control over the names of external global variables. + * @param {ASTNode} node `Identifier` node to check. + * @returns {boolean} `true` if the node is a reference to a global variable. + */ + function isReferenceToGlobalVariable(node) { + const variable = globalScope.set.get(node.name); + + return variable && variable.defs.length === 0 && + variable.references.some(ref => ref.identifier === node); + } + + /** + * Determines whether the given node should be checked. + * @param {ASTNode} node `Identifier` node. + * @returns {boolean} `true` if the node should be checked. + */ + function shouldCheck(node) { + const parent = node.parent; + + /* + * Member access has special rules for checking property names. + * Read access to a property with a restricted name is allowed, because it can be on an object that user has no control over. + * Write access isn't allowed, because it potentially creates a new property with a restricted name. + */ + if ( + parent.type === "MemberExpression" && + parent.property === node && + !parent.computed + ) { + return isAssignmentTarget(parent); + } + + return ( + parent.type !== "CallExpression" && + parent.type !== "NewExpression" && + !isRenamedImport(node) && + !isPropertyNameInDestructuring(node) && + !isReferenceToGlobalVariable(node) + ); + } + + /** + * Reports an AST node as a rule violation. + * @param {ASTNode} node The node to report. + * @returns {void} + * @private + */ + function report(node) { + + /* + * We used the range instead of the node because it's possible + * for the same identifier to be represented by two different + * nodes, with the most clear example being shorthand properties: + * { foo } + * In this case, "foo" is represented by one node for the name + * and one for the value. The only way to know they are the same + * is to look at the range. + */ + if (!reportedNodes.has(node.range.toString())) { + const isPrivate = node.type === "PrivateIdentifier"; + + context.report({ + node, + messageId: isPrivate ? "restrictedPrivate" : "restricted", + data: { + name: node.name + } + }); + reportedNodes.add(node.range.toString()); + } + } + + return { + + Program(node) { + globalScope = sourceCode.getScope(node); + }, + + [[ + "Identifier", + "PrivateIdentifier" + ]](node) { + if (isRestricted(node.name) && shouldCheck(node)) { + report(node); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/id-length.js b/node_modules/eslint/lib/rules/id-length.js index 133b84194..97bc0e430 100644 --- a/node_modules/eslint/lib/rules/id-length.js +++ b/node_modules/eslint/lib/rules/id-length.js @@ -11,207 +11,167 @@ //------------------------------------------------------------------------------ const { getGraphemeCount } = require("../shared/string-utils"); -const { - getModuleExportName, - isImportAttributeKey, -} = require("./utils/ast-utils"); //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - defaultOptions: [ - { - exceptionPatterns: [], - exceptions: [], - min: 2, - properties: "always", - }, - ], - - docs: { - description: "Enforce minimum and maximum identifier lengths", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/id-length", - }, - - schema: [ - { - type: "object", - properties: { - min: { - type: "integer", - }, - max: { - type: "integer", - }, - exceptions: { - type: "array", - uniqueItems: true, - items: { - type: "string", - }, - }, - exceptionPatterns: { - type: "array", - uniqueItems: true, - items: { - type: "string", - }, - }, - properties: { - enum: ["always", "never"], - }, - }, - additionalProperties: false, - }, - ], - messages: { - tooShort: "Identifier name '{{name}}' is too short (< {{min}}).", - tooShortPrivate: - "Identifier name '#{{name}}' is too short (< {{min}}).", - tooLong: "Identifier name '{{name}}' is too long (> {{max}}).", - tooLongPrivate: - "Identifier name #'{{name}}' is too long (> {{max}}).", - }, - }, - - create(context) { - const [options] = context.options; - const { max: maxLength = Infinity, min: minLength } = options; - const properties = options.properties !== "never"; - const exceptions = new Set(options.exceptions); - const exceptionPatterns = options.exceptionPatterns.map( - pattern => new RegExp(pattern, "u"), - ); - const reportedNodes = new Set(); - - /** - * Checks if a string matches the provided exception patterns - * @param {string} name The string to check. - * @returns {boolean} if the string is a match - * @private - */ - function matchesExceptionPattern(name) { - return exceptionPatterns.some(pattern => pattern.test(name)); - } - - const SUPPORTED_EXPRESSIONS = { - MemberExpression: - properties && - function (parent) { - return ( - !parent.computed && - // regular property assignment - ((parent.parent.left === parent && - parent.parent.type === "AssignmentExpression") || - // or the last identifier in an ObjectPattern destructuring - (parent.parent.type === "Property" && - parent.parent.value === parent && - parent.parent.parent.type === "ObjectPattern" && - parent.parent.parent.parent.left === - parent.parent.parent)) - ); - }, - AssignmentPattern(parent, node) { - return parent.left === node; - }, - VariableDeclarator(parent, node) { - return parent.id === node; - }, - Property(parent, node) { - if (parent.parent.type === "ObjectPattern") { - const isKeyAndValueSame = - parent.value.name === parent.key.name; - - return ( - (!isKeyAndValueSame && parent.value === node) || - (isKeyAndValueSame && parent.key === node && properties) - ); - } - return ( - properties && - !isImportAttributeKey(node) && - !parent.computed && - parent.key.name === node.name - ); - }, - ImportSpecifier(parent, node) { - return ( - parent.local === node && - getModuleExportName(parent.imported) !== - getModuleExportName(parent.local) - ); - }, - ImportDefaultSpecifier: true, - ImportNamespaceSpecifier: true, - RestElement: true, - FunctionExpression: true, - ArrowFunctionExpression: true, - ClassDeclaration: true, - FunctionDeclaration: true, - MethodDefinition: true, - PropertyDefinition: true, - CatchClause: true, - ArrayPattern: true, - }; - - return { - [["Identifier", "PrivateIdentifier"]](node) { - const name = node.name; - const parent = node.parent; - - const nameLength = getGraphemeCount(name); - - const isShort = nameLength < minLength; - const isLong = nameLength > maxLength; - - if ( - !(isShort || isLong) || - exceptions.has(name) || - matchesExceptionPattern(name) - ) { - return; // Nothing to report - } - - const isValidExpression = SUPPORTED_EXPRESSIONS[parent.type]; - - /* - * We used the range instead of the node because it's possible - * for the same identifier to be represented by two different - * nodes, with the most clear example being shorthand properties: - * { foo } - * In this case, "foo" is represented by one node for the name - * and one for the value. The only way to know they are the same - * is to look at the range. - */ - if ( - isValidExpression && - !reportedNodes.has(node.range.toString()) && - (isValidExpression === true || - isValidExpression(parent, node)) - ) { - reportedNodes.add(node.range.toString()); - - let messageId = isShort ? "tooShort" : "tooLong"; - - if (node.type === "PrivateIdentifier") { - messageId += "Private"; - } - - context.report({ - node, - messageId, - data: { name, min: minLength, max: maxLength }, - }); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Enforce minimum and maximum identifier lengths", + recommended: false, + url: "https://eslint.org/docs/latest/rules/id-length" + }, + + schema: [ + { + type: "object", + properties: { + min: { + type: "integer", + default: 2 + }, + max: { + type: "integer" + }, + exceptions: { + type: "array", + uniqueItems: true, + items: { + type: "string" + } + }, + exceptionPatterns: { + type: "array", + uniqueItems: true, + items: { + type: "string" + } + }, + properties: { + enum: ["always", "never"] + } + }, + additionalProperties: false + } + ], + messages: { + tooShort: "Identifier name '{{name}}' is too short (< {{min}}).", + tooShortPrivate: "Identifier name '#{{name}}' is too short (< {{min}}).", + tooLong: "Identifier name '{{name}}' is too long (> {{max}}).", + tooLongPrivate: "Identifier name #'{{name}}' is too long (> {{max}})." + } + }, + + create(context) { + const options = context.options[0] || {}; + const minLength = typeof options.min !== "undefined" ? options.min : 2; + const maxLength = typeof options.max !== "undefined" ? options.max : Infinity; + const properties = options.properties !== "never"; + const exceptions = new Set(options.exceptions); + const exceptionPatterns = (options.exceptionPatterns || []).map(pattern => new RegExp(pattern, "u")); + const reportedNodes = new Set(); + + /** + * Checks if a string matches the provided exception patterns + * @param {string} name The string to check. + * @returns {boolean} if the string is a match + * @private + */ + function matchesExceptionPattern(name) { + return exceptionPatterns.some(pattern => pattern.test(name)); + } + + const SUPPORTED_EXPRESSIONS = { + MemberExpression: properties && function(parent) { + return !parent.computed && ( + + // regular property assignment + (parent.parent.left === parent && parent.parent.type === "AssignmentExpression" || + + // or the last identifier in an ObjectPattern destructuring + parent.parent.type === "Property" && parent.parent.value === parent && + parent.parent.parent.type === "ObjectPattern" && parent.parent.parent.parent.left === parent.parent.parent) + ); + }, + AssignmentPattern(parent, node) { + return parent.left === node; + }, + VariableDeclarator(parent, node) { + return parent.id === node; + }, + Property(parent, node) { + + if (parent.parent.type === "ObjectPattern") { + const isKeyAndValueSame = parent.value.name === parent.key.name; + + return ( + !isKeyAndValueSame && parent.value === node || + isKeyAndValueSame && parent.key === node && properties + ); + } + return properties && !parent.computed && parent.key.name === node.name; + }, + ImportDefaultSpecifier: true, + RestElement: true, + FunctionExpression: true, + ArrowFunctionExpression: true, + ClassDeclaration: true, + FunctionDeclaration: true, + MethodDefinition: true, + PropertyDefinition: true, + CatchClause: true, + ArrayPattern: true + }; + + return { + [[ + "Identifier", + "PrivateIdentifier" + ]](node) { + const name = node.name; + const parent = node.parent; + + const nameLength = getGraphemeCount(name); + + const isShort = nameLength < minLength; + const isLong = nameLength > maxLength; + + if (!(isShort || isLong) || exceptions.has(name) || matchesExceptionPattern(name)) { + return; // Nothing to report + } + + const isValidExpression = SUPPORTED_EXPRESSIONS[parent.type]; + + /* + * We used the range instead of the node because it's possible + * for the same identifier to be represented by two different + * nodes, with the most clear example being shorthand properties: + * { foo } + * In this case, "foo" is represented by one node for the name + * and one for the value. The only way to know they are the same + * is to look at the range. + */ + if (isValidExpression && !reportedNodes.has(node.range.toString()) && (isValidExpression === true || isValidExpression(parent, node))) { + reportedNodes.add(node.range.toString()); + + let messageId = isShort ? "tooShort" : "tooLong"; + + if (node.type === "PrivateIdentifier") { + messageId += "Private"; + } + + context.report({ + node, + messageId, + data: { name, min: minLength, max: maxLength } + }); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/id-match.js b/node_modules/eslint/lib/rules/id-match.js index 2e70cc72d..e225454e7 100644 --- a/node_modules/eslint/lib/rules/id-match.js +++ b/node_modules/eslint/lib/rules/id-match.js @@ -5,359 +5,295 @@ "use strict"; -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ - -const astUtils = require("./utils/ast-utils"); - //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - defaultOptions: [ - "^.+$", - { - classFields: false, - ignoreDestructuring: false, - onlyDeclarations: false, - properties: false, - }, - ], - - docs: { - description: - "Require identifiers to match a specified regular expression", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/id-match", - }, - - schema: [ - { - type: "string", - }, - { - type: "object", - properties: { - properties: { - type: "boolean", - }, - classFields: { - type: "boolean", - }, - onlyDeclarations: { - type: "boolean", - }, - ignoreDestructuring: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - messages: { - notMatch: - "Identifier '{{name}}' does not match the pattern '{{pattern}}'.", - notMatchPrivate: - "Identifier '#{{name}}' does not match the pattern '{{pattern}}'.", - }, - }, - - create(context) { - //-------------------------------------------------------------------------- - // Options - //-------------------------------------------------------------------------- - const [ - pattern, - { - classFields: checkClassFields, - ignoreDestructuring, - onlyDeclarations, - properties: checkProperties, - }, - ] = context.options; - const regexp = new RegExp(pattern, "u"); - - const sourceCode = context.sourceCode; - let globalScope; - - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - // contains reported nodes to avoid reporting twice on destructuring with shorthand notation - const reportedNodes = new Set(); - const ALLOWED_PARENT_TYPES = new Set([ - "CallExpression", - "NewExpression", - ]); - const DECLARATION_TYPES = new Set([ - "FunctionDeclaration", - "VariableDeclarator", - ]); - const IMPORT_TYPES = new Set([ - "ImportSpecifier", - "ImportNamespaceSpecifier", - "ImportDefaultSpecifier", - ]); - - /** - * Checks whether the given node represents a reference to a global variable that is not declared in the source code. - * These identifiers will be allowed, as it is assumed that user has no control over the names of external global variables. - * @param {ASTNode} node `Identifier` node to check. - * @returns {boolean} `true` if the node is a reference to a global variable. - */ - function isReferenceToGlobalVariable(node) { - const variable = globalScope.set.get(node.name); - - return ( - variable && - variable.defs.length === 0 && - variable.references.some(ref => ref.identifier === node) - ); - } - - /** - * Checks if a string matches the provided pattern - * @param {string} name The string to check. - * @returns {boolean} if the string is a match - * @private - */ - function isInvalid(name) { - return !regexp.test(name); - } - - /** - * Checks if a parent of a node is an ObjectPattern. - * @param {ASTNode} node The node to check. - * @returns {boolean} if the node is inside an ObjectPattern - * @private - */ - function isInsideObjectPattern(node) { - let { parent } = node; - - while (parent) { - if (parent.type === "ObjectPattern") { - return true; - } - - parent = parent.parent; - } - - return false; - } - - /** - * Verifies if we should report an error or not based on the effective - * parent node and the identifier name. - * @param {ASTNode} effectiveParent The effective parent node of the node to be reported - * @param {string} name The identifier name of the identifier node - * @returns {boolean} whether an error should be reported or not - */ - function shouldReport(effectiveParent, name) { - return ( - (!onlyDeclarations || - DECLARATION_TYPES.has(effectiveParent.type)) && - !ALLOWED_PARENT_TYPES.has(effectiveParent.type) && - isInvalid(name) - ); - } - - /** - * Reports an AST node as a rule violation. - * @param {ASTNode} node The node to report. - * @returns {void} - * @private - */ - function report(node) { - /* - * We used the range instead of the node because it's possible - * for the same identifier to be represented by two different - * nodes, with the most clear example being shorthand properties: - * { foo } - * In this case, "foo" is represented by one node for the name - * and one for the value. The only way to know they are the same - * is to look at the range. - */ - if (!reportedNodes.has(node.range.toString())) { - const messageId = - node.type === "PrivateIdentifier" - ? "notMatchPrivate" - : "notMatch"; - - context.report({ - node, - messageId, - data: { - name: node.name, - pattern, - }, - }); - reportedNodes.add(node.range.toString()); - } - } - - return { - Program(node) { - globalScope = sourceCode.getScope(node); - }, - - Identifier(node) { - const name = node.name, - parent = node.parent, - effectiveParent = - parent.type === "MemberExpression" - ? parent.parent - : parent; - - if ( - isReferenceToGlobalVariable(node) || - astUtils.isImportAttributeKey(node) - ) { - return; - } - - if (parent.type === "MemberExpression") { - if (!checkProperties) { - return; - } - - // Always check object names - if ( - parent.object.type === "Identifier" && - parent.object.name === name - ) { - if (isInvalid(name)) { - report(node); - } - - // Report AssignmentExpressions left side's assigned variable id - } else if ( - effectiveParent.type === "AssignmentExpression" && - effectiveParent.left.type === "MemberExpression" && - effectiveParent.left.property.name === node.name - ) { - if (isInvalid(name)) { - report(node); - } - - // Report AssignmentExpressions only if they are the left side of the assignment - } else if ( - effectiveParent.type === "AssignmentExpression" && - effectiveParent.right.type !== "MemberExpression" - ) { - if (isInvalid(name)) { - report(node); - } - } - - // For https://github.com/eslint/eslint/issues/15123 - } else if ( - parent.type === "Property" && - parent.parent.type === "ObjectExpression" && - parent.key === node && - !parent.computed - ) { - if (checkProperties && isInvalid(name)) { - report(node); - } - - /* - * Properties have their own rules, and - * AssignmentPattern nodes can be treated like Properties: - * e.g.: const { no_camelcased = false } = bar; - */ - } else if ( - parent.type === "Property" || - parent.type === "AssignmentPattern" - ) { - if ( - parent.parent && - parent.parent.type === "ObjectPattern" - ) { - if ( - !ignoreDestructuring && - parent.shorthand && - parent.value.left && - isInvalid(name) - ) { - report(node); - } - - const assignmentKeyEqualsValue = - parent.key.name === parent.value.name; - - // prevent checking righthand side of destructured object - if (!assignmentKeyEqualsValue && parent.key === node) { - return; - } - - const valueIsInvalid = - parent.value.name && isInvalid(name); - - // ignore destructuring if the option is set, unless a new identifier is created - if ( - valueIsInvalid && - !(assignmentKeyEqualsValue && ignoreDestructuring) - ) { - report(node); - } - } - - // never check properties or always ignore destructuring - if ( - (!checkProperties && !parent.computed) || - (ignoreDestructuring && isInsideObjectPattern(node)) - ) { - return; - } - - // don't check right hand side of AssignmentExpression to prevent duplicate warnings - if ( - parent.right !== node && - shouldReport(effectiveParent, name) - ) { - report(node); - } - - // Check if it's an import specifier - } else if (IMPORT_TYPES.has(parent.type)) { - // Report only if the local imported identifier is invalid - if ( - parent.local && - parent.local.name === node.name && - isInvalid(name) - ) { - report(node); - } - } else if (parent.type === "PropertyDefinition") { - if (checkClassFields && isInvalid(name)) { - report(node); - } - - // Report anything that is invalid that isn't a CallExpression - } else if (shouldReport(effectiveParent, name)) { - report(node); - } - }, - - PrivateIdentifier(node) { - const isClassField = node.parent.type === "PropertyDefinition"; - - if (isClassField && !checkClassFields) { - return; - } - - if (isInvalid(node.name)) { - report(node); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Require identifiers to match a specified regular expression", + recommended: false, + url: "https://eslint.org/docs/latest/rules/id-match" + }, + + schema: [ + { + type: "string" + }, + { + type: "object", + properties: { + properties: { + type: "boolean", + default: false + }, + classFields: { + type: "boolean", + default: false + }, + onlyDeclarations: { + type: "boolean", + default: false + }, + ignoreDestructuring: { + type: "boolean", + default: false + } + }, + additionalProperties: false + } + ], + messages: { + notMatch: "Identifier '{{name}}' does not match the pattern '{{pattern}}'.", + notMatchPrivate: "Identifier '#{{name}}' does not match the pattern '{{pattern}}'." + } + }, + + create(context) { + + //-------------------------------------------------------------------------- + // Options + //-------------------------------------------------------------------------- + const pattern = context.options[0] || "^.+$", + regexp = new RegExp(pattern, "u"); + + const options = context.options[1] || {}, + checkProperties = !!options.properties, + checkClassFields = !!options.classFields, + onlyDeclarations = !!options.onlyDeclarations, + ignoreDestructuring = !!options.ignoreDestructuring; + + const sourceCode = context.sourceCode; + let globalScope; + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + // contains reported nodes to avoid reporting twice on destructuring with shorthand notation + const reportedNodes = new Set(); + const ALLOWED_PARENT_TYPES = new Set(["CallExpression", "NewExpression"]); + const DECLARATION_TYPES = new Set(["FunctionDeclaration", "VariableDeclarator"]); + const IMPORT_TYPES = new Set(["ImportSpecifier", "ImportNamespaceSpecifier", "ImportDefaultSpecifier"]); + + /** + * Checks whether the given node represents a reference to a global variable that is not declared in the source code. + * These identifiers will be allowed, as it is assumed that user has no control over the names of external global variables. + * @param {ASTNode} node `Identifier` node to check. + * @returns {boolean} `true` if the node is a reference to a global variable. + */ + function isReferenceToGlobalVariable(node) { + const variable = globalScope.set.get(node.name); + + return variable && variable.defs.length === 0 && + variable.references.some(ref => ref.identifier === node); + } + + /** + * Checks if a string matches the provided pattern + * @param {string} name The string to check. + * @returns {boolean} if the string is a match + * @private + */ + function isInvalid(name) { + return !regexp.test(name); + } + + /** + * Checks if a parent of a node is an ObjectPattern. + * @param {ASTNode} node The node to check. + * @returns {boolean} if the node is inside an ObjectPattern + * @private + */ + function isInsideObjectPattern(node) { + let { parent } = node; + + while (parent) { + if (parent.type === "ObjectPattern") { + return true; + } + + parent = parent.parent; + } + + return false; + } + + /** + * Verifies if we should report an error or not based on the effective + * parent node and the identifier name. + * @param {ASTNode} effectiveParent The effective parent node of the node to be reported + * @param {string} name The identifier name of the identifier node + * @returns {boolean} whether an error should be reported or not + */ + function shouldReport(effectiveParent, name) { + return (!onlyDeclarations || DECLARATION_TYPES.has(effectiveParent.type)) && + !ALLOWED_PARENT_TYPES.has(effectiveParent.type) && isInvalid(name); + } + + /** + * Reports an AST node as a rule violation. + * @param {ASTNode} node The node to report. + * @returns {void} + * @private + */ + function report(node) { + + /* + * We used the range instead of the node because it's possible + * for the same identifier to be represented by two different + * nodes, with the most clear example being shorthand properties: + * { foo } + * In this case, "foo" is represented by one node for the name + * and one for the value. The only way to know they are the same + * is to look at the range. + */ + if (!reportedNodes.has(node.range.toString())) { + + const messageId = (node.type === "PrivateIdentifier") + ? "notMatchPrivate" : "notMatch"; + + context.report({ + node, + messageId, + data: { + name: node.name, + pattern + } + }); + reportedNodes.add(node.range.toString()); + } + } + + return { + + Program(node) { + globalScope = sourceCode.getScope(node); + }, + + Identifier(node) { + const name = node.name, + parent = node.parent, + effectiveParent = (parent.type === "MemberExpression") ? parent.parent : parent; + + if (isReferenceToGlobalVariable(node)) { + return; + } + + if (parent.type === "MemberExpression") { + + if (!checkProperties) { + return; + } + + // Always check object names + if (parent.object.type === "Identifier" && + parent.object.name === name) { + if (isInvalid(name)) { + report(node); + } + + // Report AssignmentExpressions left side's assigned variable id + } else if (effectiveParent.type === "AssignmentExpression" && + effectiveParent.left.type === "MemberExpression" && + effectiveParent.left.property.name === node.name) { + if (isInvalid(name)) { + report(node); + } + + // Report AssignmentExpressions only if they are the left side of the assignment + } else if (effectiveParent.type === "AssignmentExpression" && effectiveParent.right.type !== "MemberExpression") { + if (isInvalid(name)) { + report(node); + } + } + + // For https://github.com/eslint/eslint/issues/15123 + } else if ( + parent.type === "Property" && + parent.parent.type === "ObjectExpression" && + parent.key === node && + !parent.computed + ) { + if (checkProperties && isInvalid(name)) { + report(node); + } + + /* + * Properties have their own rules, and + * AssignmentPattern nodes can be treated like Properties: + * e.g.: const { no_camelcased = false } = bar; + */ + } else if (parent.type === "Property" || parent.type === "AssignmentPattern") { + + if (parent.parent && parent.parent.type === "ObjectPattern") { + if (!ignoreDestructuring && parent.shorthand && parent.value.left && isInvalid(name)) { + report(node); + } + + const assignmentKeyEqualsValue = parent.key.name === parent.value.name; + + // prevent checking righthand side of destructured object + if (!assignmentKeyEqualsValue && parent.key === node) { + return; + } + + const valueIsInvalid = parent.value.name && isInvalid(name); + + // ignore destructuring if the option is set, unless a new identifier is created + if (valueIsInvalid && !(assignmentKeyEqualsValue && ignoreDestructuring)) { + report(node); + } + } + + // never check properties or always ignore destructuring + if ((!checkProperties && !parent.computed) || (ignoreDestructuring && isInsideObjectPattern(node))) { + return; + } + + // don't check right hand side of AssignmentExpression to prevent duplicate warnings + if (parent.right !== node && shouldReport(effectiveParent, name)) { + report(node); + } + + // Check if it's an import specifier + } else if (IMPORT_TYPES.has(parent.type)) { + + // Report only if the local imported identifier is invalid + if (parent.local && parent.local.name === node.name && isInvalid(name)) { + report(node); + } + + } else if (parent.type === "PropertyDefinition") { + + if (checkClassFields && isInvalid(name)) { + report(node); + } + + // Report anything that is invalid that isn't a CallExpression + } else if (shouldReport(effectiveParent, name)) { + report(node); + } + }, + + "PrivateIdentifier"(node) { + + const isClassField = node.parent.type === "PropertyDefinition"; + + if (isClassField && !checkClassFields) { + return; + } + + if (isInvalid(node.name)) { + report(node); + } + } + + }; + + } }; diff --git a/node_modules/eslint/lib/rules/implicit-arrow-linebreak.js b/node_modules/eslint/lib/rules/implicit-arrow-linebreak.js index e32d0cafc..32f422ce8 100644 --- a/node_modules/eslint/lib/rules/implicit-arrow-linebreak.js +++ b/node_modules/eslint/lib/rules/implicit-arrow-linebreak.js @@ -10,116 +10,75 @@ const { isCommentToken, isNotOpeningParenToken } = require("./utils/ast-utils"); //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "implicit-arrow-linebreak", - url: "https://eslint.style/rules/implicit-arrow-linebreak", - }, - }, - ], - }, - type: "layout", + meta: { + deprecated: true, + replacedBy: [], + type: "layout", - docs: { - description: "Enforce the location of arrow function bodies", - recommended: false, - url: "https://eslint.org/docs/latest/rules/implicit-arrow-linebreak", - }, + docs: { + description: "Enforce the location of arrow function bodies", + recommended: false, + url: "https://eslint.org/docs/latest/rules/implicit-arrow-linebreak" + }, - fixable: "whitespace", + fixable: "whitespace", - schema: [ - { - enum: ["beside", "below"], - }, - ], - messages: { - expected: "Expected a linebreak before this expression.", - unexpected: "Expected no linebreak before this expression.", - }, - }, + schema: [ + { + enum: ["beside", "below"] + } + ], + messages: { + expected: "Expected a linebreak before this expression.", + unexpected: "Expected no linebreak before this expression." + } + }, - create(context) { - const sourceCode = context.sourceCode; - const option = context.options[0] || "beside"; + create(context) { + const sourceCode = context.sourceCode; + const option = context.options[0] || "beside"; - /** - * Validates the location of an arrow function body - * @param {ASTNode} node The arrow function body - * @returns {void} - */ - function validateExpression(node) { - if (node.body.type === "BlockStatement") { - return; - } + /** + * Validates the location of an arrow function body + * @param {ASTNode} node The arrow function body + * @returns {void} + */ + function validateExpression(node) { + if (node.body.type === "BlockStatement") { + return; + } - const arrowToken = sourceCode.getTokenBefore( - node.body, - isNotOpeningParenToken, - ); - const firstTokenOfBody = sourceCode.getTokenAfter(arrowToken); + const arrowToken = sourceCode.getTokenBefore(node.body, isNotOpeningParenToken); + const firstTokenOfBody = sourceCode.getTokenAfter(arrowToken); - if ( - arrowToken.loc.end.line === firstTokenOfBody.loc.start.line && - option === "below" - ) { - context.report({ - node: firstTokenOfBody, - messageId: "expected", - fix: fixer => - fixer.insertTextBefore(firstTokenOfBody, "\n"), - }); - } else if ( - arrowToken.loc.end.line !== firstTokenOfBody.loc.start.line && - option === "beside" - ) { - context.report({ - node: firstTokenOfBody, - messageId: "unexpected", - fix(fixer) { - if ( - sourceCode.getFirstTokenBetween( - arrowToken, - firstTokenOfBody, - { - includeComments: true, - filter: isCommentToken, - }, - ) - ) { - return null; - } + if (arrowToken.loc.end.line === firstTokenOfBody.loc.start.line && option === "below") { + context.report({ + node: firstTokenOfBody, + messageId: "expected", + fix: fixer => fixer.insertTextBefore(firstTokenOfBody, "\n") + }); + } else if (arrowToken.loc.end.line !== firstTokenOfBody.loc.start.line && option === "beside") { + context.report({ + node: firstTokenOfBody, + messageId: "unexpected", + fix(fixer) { + if (sourceCode.getFirstTokenBetween(arrowToken, firstTokenOfBody, { includeComments: true, filter: isCommentToken })) { + return null; + } - return fixer.replaceTextRange( - [arrowToken.range[1], firstTokenOfBody.range[0]], - " ", - ); - }, - }); - } - } + return fixer.replaceTextRange([arrowToken.range[1], firstTokenOfBody.range[0]], " "); + } + }); + } + } - //---------------------------------------------------------------------- - // Public - //---------------------------------------------------------------------- - return { - ArrowFunctionExpression: node => validateExpression(node), - }; - }, + //---------------------------------------------------------------------- + // Public + //---------------------------------------------------------------------- + return { + ArrowFunctionExpression: node => validateExpression(node) + }; + } }; diff --git a/node_modules/eslint/lib/rules/indent-legacy.js b/node_modules/eslint/lib/rules/indent-legacy.js index 3c14ff767..78bf965cb 100644 --- a/node_modules/eslint/lib/rules/indent-legacy.js +++ b/node_modules/eslint/lib/rules/indent-legacy.js @@ -20,1350 +20,1107 @@ const astUtils = require("./utils/ast-utils"); //------------------------------------------------------------------------------ // this rule has known coverage issues, but it's deprecated and shouldn't be updated in the future anyway. /* c8 ignore next */ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "layout", - - docs: { - description: "Enforce consistent indentation", - recommended: false, - url: "https://eslint.org/docs/latest/rules/indent-legacy", - }, - - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "4.0.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "indent", - url: "https://eslint.style/rules/indent", - }, - }, - ], - }, - - fixable: "whitespace", - - schema: [ - { - oneOf: [ - { - enum: ["tab"], - }, - { - type: "integer", - minimum: 0, - }, - ], - }, - { - type: "object", - properties: { - SwitchCase: { - type: "integer", - minimum: 0, - }, - VariableDeclarator: { - oneOf: [ - { - type: "integer", - minimum: 0, - }, - { - type: "object", - properties: { - var: { - type: "integer", - minimum: 0, - }, - let: { - type: "integer", - minimum: 0, - }, - const: { - type: "integer", - minimum: 0, - }, - }, - }, - ], - }, - outerIIFEBody: { - type: "integer", - minimum: 0, - }, - MemberExpression: { - type: "integer", - minimum: 0, - }, - FunctionDeclaration: { - type: "object", - properties: { - parameters: { - oneOf: [ - { - type: "integer", - minimum: 0, - }, - { - enum: ["first"], - }, - ], - }, - body: { - type: "integer", - minimum: 0, - }, - }, - }, - FunctionExpression: { - type: "object", - properties: { - parameters: { - oneOf: [ - { - type: "integer", - minimum: 0, - }, - { - enum: ["first"], - }, - ], - }, - body: { - type: "integer", - minimum: 0, - }, - }, - }, - CallExpression: { - type: "object", - properties: { - parameters: { - oneOf: [ - { - type: "integer", - minimum: 0, - }, - { - enum: ["first"], - }, - ], - }, - }, - }, - ArrayExpression: { - oneOf: [ - { - type: "integer", - minimum: 0, - }, - { - enum: ["first"], - }, - ], - }, - ObjectExpression: { - oneOf: [ - { - type: "integer", - minimum: 0, - }, - { - enum: ["first"], - }, - ], - }, - }, - additionalProperties: false, - }, - ], - messages: { - expected: - "Expected indentation of {{expected}} but found {{actual}}.", - }, - }, - - create(context) { - const DEFAULT_VARIABLE_INDENT = 1; - const DEFAULT_PARAMETER_INDENT = null; // For backwards compatibility, don't check parameter indentation unless specified in the config - const DEFAULT_FUNCTION_BODY_INDENT = 1; - - let indentType = "space"; - let indentSize = 4; - const options = { - SwitchCase: 0, - VariableDeclarator: { - var: DEFAULT_VARIABLE_INDENT, - let: DEFAULT_VARIABLE_INDENT, - const: DEFAULT_VARIABLE_INDENT, - }, - outerIIFEBody: null, - FunctionDeclaration: { - parameters: DEFAULT_PARAMETER_INDENT, - body: DEFAULT_FUNCTION_BODY_INDENT, - }, - FunctionExpression: { - parameters: DEFAULT_PARAMETER_INDENT, - body: DEFAULT_FUNCTION_BODY_INDENT, - }, - CallExpression: { - arguments: DEFAULT_PARAMETER_INDENT, - }, - ArrayExpression: 1, - ObjectExpression: 1, - }; - - const sourceCode = context.sourceCode; - - if (context.options.length) { - if (context.options[0] === "tab") { - indentSize = 1; - indentType = "tab"; - } /* c8 ignore start */ else if ( - typeof context.options[0] === "number" - ) { - indentSize = context.options[0]; - indentType = "space"; - } /* c8 ignore stop */ - - if (context.options[1]) { - const opts = context.options[1]; - - options.SwitchCase = opts.SwitchCase || 0; - const variableDeclaratorRules = opts.VariableDeclarator; - - if (typeof variableDeclaratorRules === "number") { - options.VariableDeclarator = { - var: variableDeclaratorRules, - let: variableDeclaratorRules, - const: variableDeclaratorRules, - }; - } else if (typeof variableDeclaratorRules === "object") { - Object.assign( - options.VariableDeclarator, - variableDeclaratorRules, - ); - } - - if (typeof opts.outerIIFEBody === "number") { - options.outerIIFEBody = opts.outerIIFEBody; - } - - if (typeof opts.MemberExpression === "number") { - options.MemberExpression = opts.MemberExpression; - } - - if (typeof opts.FunctionDeclaration === "object") { - Object.assign( - options.FunctionDeclaration, - opts.FunctionDeclaration, - ); - } - - if (typeof opts.FunctionExpression === "object") { - Object.assign( - options.FunctionExpression, - opts.FunctionExpression, - ); - } - - if (typeof opts.CallExpression === "object") { - Object.assign(options.CallExpression, opts.CallExpression); - } - - if ( - typeof opts.ArrayExpression === "number" || - typeof opts.ArrayExpression === "string" - ) { - options.ArrayExpression = opts.ArrayExpression; - } - - if ( - typeof opts.ObjectExpression === "number" || - typeof opts.ObjectExpression === "string" - ) { - options.ObjectExpression = opts.ObjectExpression; - } - } - } - - const caseIndentStore = {}; - - /** - * Creates an error message for a line, given the expected/actual indentation. - * @param {number} expectedAmount The expected amount of indentation characters for this line - * @param {number} actualSpaces The actual number of indentation spaces that were found on this line - * @param {number} actualTabs The actual number of indentation tabs that were found on this line - * @returns {string} An error message for this line - */ - function createErrorMessageData( - expectedAmount, - actualSpaces, - actualTabs, - ) { - const expectedStatement = `${expectedAmount} ${indentType}${expectedAmount === 1 ? "" : "s"}`; // e.g. "2 tabs" - const foundSpacesWord = `space${actualSpaces === 1 ? "" : "s"}`; // e.g. "space" - const foundTabsWord = `tab${actualTabs === 1 ? "" : "s"}`; // e.g. "tabs" - let foundStatement; - - if (actualSpaces > 0 && actualTabs > 0) { - foundStatement = `${actualSpaces} ${foundSpacesWord} and ${actualTabs} ${foundTabsWord}`; // e.g. "1 space and 2 tabs" - } else if (actualSpaces > 0) { - /* - * Abbreviate the message if the expected indentation is also spaces. - * e.g. 'Expected 4 spaces but found 2' rather than 'Expected 4 spaces but found 2 spaces' - */ - foundStatement = - indentType === "space" - ? actualSpaces - : `${actualSpaces} ${foundSpacesWord}`; - } else if (actualTabs > 0) { - foundStatement = - indentType === "tab" - ? actualTabs - : `${actualTabs} ${foundTabsWord}`; - } else { - foundStatement = "0"; - } - return { - expected: expectedStatement, - actual: foundStatement, - }; - } - - /** - * Reports a given indent violation - * @param {ASTNode} node Node violating the indent rule - * @param {number} needed Expected indentation character count - * @param {number} gottenSpaces Indentation space count in the actual node/code - * @param {number} gottenTabs Indentation tab count in the actual node/code - * @param {Object} [loc] Error line and column location - * @param {boolean} isLastNodeCheck Is the error for last node check - * @returns {void} - */ - function report( - node, - needed, - gottenSpaces, - gottenTabs, - loc, - isLastNodeCheck, - ) { - if (gottenSpaces && gottenTabs) { - // To avoid conflicts with `no-mixed-spaces-and-tabs`, don't report lines that have both spaces and tabs. - return; - } - - const desiredIndent = (indentType === "space" ? " " : "\t").repeat( - needed, - ); - - const textRange = isLastNodeCheck - ? [ - node.range[1] - node.loc.end.column, - node.range[1] - - node.loc.end.column + - gottenSpaces + - gottenTabs, - ] - : [ - node.range[0] - node.loc.start.column, - node.range[0] - - node.loc.start.column + - gottenSpaces + - gottenTabs, - ]; - - context.report({ - node, - loc, - messageId: "expected", - data: createErrorMessageData(needed, gottenSpaces, gottenTabs), - fix: fixer => fixer.replaceTextRange(textRange, desiredIndent), - }); - } - - /** - * Get the actual indent of node - * @param {ASTNode|Token} node Node to examine - * @param {boolean} [byLastLine=false] get indent of node's last line - * @returns {Object} The node's indent. Contains keys `space` and `tab`, representing the indent of each character. Also - * contains keys `goodChar` and `badChar`, where `goodChar` is the amount of the user's desired indentation character, and - * `badChar` is the amount of the other indentation character. - */ - function getNodeIndent(node, byLastLine) { - const token = byLastLine - ? sourceCode.getLastToken(node) - : sourceCode.getFirstToken(node); - const srcCharsBeforeNode = sourceCode - .getText(token, token.loc.start.column) - .split(""); - const indentChars = srcCharsBeforeNode.slice( - 0, - srcCharsBeforeNode.findIndex( - char => char !== " " && char !== "\t", - ), - ); - const spaces = indentChars.filter(char => char === " ").length; - const tabs = indentChars.filter(char => char === "\t").length; - - return { - space: spaces, - tab: tabs, - goodChar: indentType === "space" ? spaces : tabs, - badChar: indentType === "space" ? tabs : spaces, - }; - } - - /** - * Checks node is the first in its own start line. By default it looks by start line. - * @param {ASTNode} node The node to check - * @param {boolean} [byEndLocation=false] Lookup based on start position or end - * @returns {boolean} true if its the first in the its start line - */ - function isNodeFirstInLine(node, byEndLocation) { - const firstToken = - byEndLocation === true - ? sourceCode.getLastToken(node, 1) - : sourceCode.getTokenBefore(node), - startLine = - byEndLocation === true - ? node.loc.end.line - : node.loc.start.line, - endLine = firstToken ? firstToken.loc.end.line : -1; - - return startLine !== endLine; - } - - /** - * Check indent for node - * @param {ASTNode} node Node to check - * @param {number} neededIndent needed indent - * @returns {void} - */ - function checkNodeIndent(node, neededIndent) { - const actualIndent = getNodeIndent(node, false); - - if ( - node.type !== "ArrayExpression" && - node.type !== "ObjectExpression" && - (actualIndent.goodChar !== neededIndent || - actualIndent.badChar !== 0) && - isNodeFirstInLine(node) - ) { - report( - node, - neededIndent, - actualIndent.space, - actualIndent.tab, - ); - } - - if (node.type === "IfStatement" && node.alternate) { - const elseToken = sourceCode.getTokenBefore(node.alternate); - - checkNodeIndent(elseToken, neededIndent); - - if (!isNodeFirstInLine(node.alternate)) { - checkNodeIndent(node.alternate, neededIndent); - } - } - - if (node.type === "TryStatement" && node.handler) { - const catchToken = sourceCode.getFirstToken(node.handler); - - checkNodeIndent(catchToken, neededIndent); - } - - if (node.type === "TryStatement" && node.finalizer) { - const finallyToken = sourceCode.getTokenBefore(node.finalizer); - - checkNodeIndent(finallyToken, neededIndent); - } - - if (node.type === "DoWhileStatement") { - const whileToken = sourceCode.getTokenAfter(node.body); - - checkNodeIndent(whileToken, neededIndent); - } - } - - /** - * Check indent for nodes list - * @param {ASTNode[]} nodes list of node objects - * @param {number} indent needed indent - * @returns {void} - */ - function checkNodesIndent(nodes, indent) { - nodes.forEach(node => checkNodeIndent(node, indent)); - } - - /** - * Check last node line indent this detects, that block closed correctly - * @param {ASTNode} node Node to examine - * @param {number} lastLineIndent needed indent - * @returns {void} - */ - function checkLastNodeLineIndent(node, lastLineIndent) { - const lastToken = sourceCode.getLastToken(node); - const endIndent = getNodeIndent(lastToken, true); - - if ( - (endIndent.goodChar !== lastLineIndent || - endIndent.badChar !== 0) && - isNodeFirstInLine(node, true) - ) { - report( - node, - lastLineIndent, - endIndent.space, - endIndent.tab, - { - line: lastToken.loc.start.line, - column: lastToken.loc.start.column, - }, - true, - ); - } - } - - /** - * Check last node line indent this detects, that block closed correctly - * This function for more complicated return statement case, where closing parenthesis may be followed by ';' - * @param {ASTNode} node Node to examine - * @param {number} firstLineIndent first line needed indent - * @returns {void} - */ - function checkLastReturnStatementLineIndent(node, firstLineIndent) { - /* - * in case if return statement ends with ');' we have traverse back to ')' - * otherwise we'll measure indent for ';' and replace ')' - */ - const lastToken = sourceCode.getLastToken( - node, - astUtils.isClosingParenToken, - ); - const textBeforeClosingParenthesis = sourceCode - .getText(lastToken, lastToken.loc.start.column) - .slice(0, -1); - - if (textBeforeClosingParenthesis.trim()) { - // There are tokens before the closing paren, don't report this case - return; - } - - const endIndent = getNodeIndent(lastToken, true); - - if (endIndent.goodChar !== firstLineIndent) { - report( - node, - firstLineIndent, - endIndent.space, - endIndent.tab, - { - line: lastToken.loc.start.line, - column: lastToken.loc.start.column, - }, - true, - ); - } - } - - /** - * Check first node line indent is correct - * @param {ASTNode} node Node to examine - * @param {number} firstLineIndent needed indent - * @returns {void} - */ - function checkFirstNodeLineIndent(node, firstLineIndent) { - const startIndent = getNodeIndent(node, false); - - if ( - (startIndent.goodChar !== firstLineIndent || - startIndent.badChar !== 0) && - isNodeFirstInLine(node) - ) { - report( - node, - firstLineIndent, - startIndent.space, - startIndent.tab, - { - line: node.loc.start.line, - column: node.loc.start.column, - }, - ); - } - } - - /** - * Returns a parent node of given node based on a specified type - * if not present then return null - * @param {ASTNode} node node to examine - * @param {string} type type that is being looked for - * @param {string} stopAtList end points for the evaluating code - * @returns {ASTNode|void} if found then node otherwise null - */ - function getParentNodeByType(node, type, stopAtList) { - let parent = node.parent; - const stopAtSet = new Set(stopAtList || ["Program"]); - - while ( - parent.type !== type && - !stopAtSet.has(parent.type) && - parent.type !== "Program" - ) { - parent = parent.parent; - } - - return parent.type === type ? parent : null; - } - - /** - * Returns the VariableDeclarator based on the current node - * if not present then return null - * @param {ASTNode} node node to examine - * @returns {ASTNode|void} if found then node otherwise null - */ - function getVariableDeclaratorNode(node) { - return getParentNodeByType(node, "VariableDeclarator"); - } - - /** - * Check to see if the node is part of the multi-line variable declaration. - * Also if its on the same line as the varNode - * @param {ASTNode} node node to check - * @param {ASTNode} varNode variable declaration node to check against - * @returns {boolean} True if all the above condition satisfy - */ - function isNodeInVarOnTop(node, varNode) { - return ( - varNode && - varNode.parent.loc.start.line === node.loc.start.line && - varNode.parent.declarations.length > 1 - ); - } - - /** - * Check to see if the argument before the callee node is multi-line and - * there should only be 1 argument before the callee node - * @param {ASTNode} node node to check - * @returns {boolean} True if arguments are multi-line - */ - function isArgBeforeCalleeNodeMultiline(node) { - const parent = node.parent; - - if (parent.arguments.length >= 2 && parent.arguments[1] === node) { - return ( - parent.arguments[0].loc.end.line > - parent.arguments[0].loc.start.line - ); - } - - return false; - } - - /** - * Check to see if the node is a file level IIFE - * @param {ASTNode} node The function node to check. - * @returns {boolean} True if the node is the outer IIFE - */ - function isOuterIIFE(node) { - const parent = node.parent; - let stmt = parent.parent; - - /* - * Verify that the node is an IIEF - */ - if (parent.type !== "CallExpression" || parent.callee !== node) { - return false; - } - - /* - * Navigate legal ancestors to determine whether this IIEF is outer - */ - while ( - (stmt.type === "UnaryExpression" && - (stmt.operator === "!" || - stmt.operator === "~" || - stmt.operator === "+" || - stmt.operator === "-")) || - stmt.type === "AssignmentExpression" || - stmt.type === "LogicalExpression" || - stmt.type === "SequenceExpression" || - stmt.type === "VariableDeclarator" - ) { - stmt = stmt.parent; - } - - return ( - (stmt.type === "ExpressionStatement" || - stmt.type === "VariableDeclaration") && - stmt.parent && - stmt.parent.type === "Program" - ); - } - - /** - * Check indent for function block content - * @param {ASTNode} node A BlockStatement node that is inside of a function. - * @returns {void} - */ - function checkIndentInFunctionBlock(node) { - /* - * Search first caller in chain. - * Ex.: - * - * Models <- Identifier - * .User - * .find() - * .exec(function() { - * // function body - * }); - * - * Looks for 'Models' - */ - const calleeNode = node.parent; // FunctionExpression - let indent; - - if ( - calleeNode.parent && - (calleeNode.parent.type === "Property" || - calleeNode.parent.type === "ArrayExpression") - ) { - // If function is part of array or object, comma can be put at left - indent = getNodeIndent(calleeNode, false).goodChar; - } else { - // If function is standalone, simple calculate indent - indent = getNodeIndent(calleeNode).goodChar; - } - - if (calleeNode.parent.type === "CallExpression") { - const calleeParent = calleeNode.parent; - - if ( - calleeNode.type !== "FunctionExpression" && - calleeNode.type !== "ArrowFunctionExpression" - ) { - if ( - calleeParent && - calleeParent.loc.start.line < node.loc.start.line - ) { - indent = getNodeIndent(calleeParent).goodChar; - } - } else { - if ( - isArgBeforeCalleeNodeMultiline(calleeNode) && - calleeParent.callee.loc.start.line === - calleeParent.callee.loc.end.line && - !isNodeFirstInLine(calleeNode) - ) { - indent = getNodeIndent(calleeParent).goodChar; - } - } - } - - /* - * function body indent should be indent + indent size, unless this - * is a FunctionDeclaration, FunctionExpression, or outer IIFE and the corresponding options are enabled. - */ - let functionOffset = indentSize; - - if (options.outerIIFEBody !== null && isOuterIIFE(calleeNode)) { - functionOffset = options.outerIIFEBody * indentSize; - } else if (calleeNode.type === "FunctionExpression") { - functionOffset = options.FunctionExpression.body * indentSize; - } else if (calleeNode.type === "FunctionDeclaration") { - functionOffset = options.FunctionDeclaration.body * indentSize; - } - indent += functionOffset; - - // check if the node is inside a variable - const parentVarNode = getVariableDeclaratorNode(node); - - if (parentVarNode && isNodeInVarOnTop(node, parentVarNode)) { - indent += - indentSize * - options.VariableDeclarator[parentVarNode.parent.kind]; - } - - if (node.body.length > 0) { - checkNodesIndent(node.body, indent); - } - - checkLastNodeLineIndent(node, indent - functionOffset); - } - - /** - * Checks if the given node starts and ends on the same line - * @param {ASTNode} node The node to check - * @returns {boolean} Whether or not the block starts and ends on the same line. - */ - function isSingleLineNode(node) { - const lastToken = sourceCode.getLastToken(node), - startLine = node.loc.start.line, - endLine = lastToken.loc.end.line; - - return startLine === endLine; - } - - /** - * Check indent for array block content or object block content - * @param {ASTNode} node node to examine - * @returns {void} - */ - function checkIndentInArrayOrObjectBlock(node) { - // Skip inline - if (isSingleLineNode(node)) { - return; - } - - let elements = - node.type === "ArrayExpression" - ? node.elements - : node.properties; - - // filter out empty elements example would be [ , 2] so remove first element as espree considers it as null - elements = elements.filter(elem => elem !== null); - - let nodeIndent; - let elementsIndent; - const parentVarNode = getVariableDeclaratorNode(node); - - // TODO - come up with a better strategy in future - if (isNodeFirstInLine(node)) { - const parent = node.parent; - - nodeIndent = getNodeIndent(parent).goodChar; - if ( - !parentVarNode || - parentVarNode.loc.start.line !== node.loc.start.line - ) { - if ( - parent.type !== "VariableDeclarator" || - parentVarNode === parentVarNode.parent.declarations[0] - ) { - if ( - parent.type === "VariableDeclarator" && - parentVarNode.loc.start.line === - parent.loc.start.line - ) { - nodeIndent += - indentSize * - options.VariableDeclarator[ - parentVarNode.parent.kind - ]; - } else if ( - parent.type === "ObjectExpression" || - parent.type === "ArrayExpression" - ) { - const parentElements = - node.parent.type === "ObjectExpression" - ? node.parent.properties - : node.parent.elements; - - if ( - parentElements[0] && - parentElements[0].loc.start.line === - parent.loc.start.line && - parentElements[0].loc.end.line !== - parent.loc.start.line - ) { - /* - * If the first element of the array spans multiple lines, don't increase the expected indentation of the rest. - * e.g. [{ - * foo: 1 - * }, - * { - * bar: 1 - * }] - * the second object is not indented. - */ - } else if ( - typeof options[parent.type] === "number" - ) { - nodeIndent += options[parent.type] * indentSize; - } else { - nodeIndent = parentElements[0].loc.start.column; - } - } else if ( - parent.type === "CallExpression" || - parent.type === "NewExpression" - ) { - if ( - typeof options.CallExpression.arguments === - "number" - ) { - nodeIndent += - options.CallExpression.arguments * - indentSize; - } else if ( - options.CallExpression.arguments === "first" - ) { - if (parent.arguments.includes(node)) { - nodeIndent = - parent.arguments[0].loc.start.column; - } - } else { - nodeIndent += indentSize; - } - } else if ( - parent.type === "LogicalExpression" || - parent.type === "ArrowFunctionExpression" - ) { - nodeIndent += indentSize; - } - } - } - - checkFirstNodeLineIndent(node, nodeIndent); - } else { - nodeIndent = getNodeIndent(node).goodChar; - } - - if (options[node.type] === "first") { - elementsIndent = elements.length - ? elements[0].loc.start.column - : 0; // If there are no elements, elementsIndent doesn't matter. - } else { - elementsIndent = nodeIndent + indentSize * options[node.type]; - } - - /* - * Check if the node is a multiple variable declaration; if so, then - * make sure indentation takes that into account. - */ - if (isNodeInVarOnTop(node, parentVarNode)) { - elementsIndent += - indentSize * - options.VariableDeclarator[parentVarNode.parent.kind]; - } - - checkNodesIndent(elements, elementsIndent); - - if (elements.length > 0) { - // Skip last block line check if last item in same line - if (elements.at(-1).loc.end.line === node.loc.end.line) { - return; - } - } - - checkLastNodeLineIndent( - node, - nodeIndent + - (isNodeInVarOnTop(node, parentVarNode) - ? options.VariableDeclarator[ - parentVarNode.parent.kind - ] * indentSize - : 0), - ); - } - - /** - * Check if the node or node body is a BlockStatement or not - * @param {ASTNode} node node to test - * @returns {boolean} True if it or its body is a block statement - */ - function isNodeBodyBlock(node) { - return ( - node.type === "BlockStatement" || - node.type === "ClassBody" || - (node.body && node.body.type === "BlockStatement") || - (node.consequent && node.consequent.type === "BlockStatement") - ); - } - - /** - * Check indentation for blocks - * @param {ASTNode} node node to check - * @returns {void} - */ - function blockIndentationCheck(node) { - // Skip inline blocks - if (isSingleLineNode(node)) { - return; - } - - if ( - node.parent && - (node.parent.type === "FunctionExpression" || - node.parent.type === "FunctionDeclaration" || - node.parent.type === "ArrowFunctionExpression") - ) { - checkIndentInFunctionBlock(node); - return; - } - - let indent; - let nodesToCheck; - - /* - * For this statements we should check indent from statement beginning, - * not from the beginning of the block. - */ - const statementsWithProperties = [ - "IfStatement", - "WhileStatement", - "ForStatement", - "ForInStatement", - "ForOfStatement", - "DoWhileStatement", - "ClassDeclaration", - "TryStatement", - ]; - - if ( - node.parent && - statementsWithProperties.includes(node.parent.type) && - isNodeBodyBlock(node) - ) { - indent = getNodeIndent(node.parent).goodChar; - } else if (node.parent && node.parent.type === "CatchClause") { - indent = getNodeIndent(node.parent.parent).goodChar; - } else { - indent = getNodeIndent(node).goodChar; - } - - if ( - node.type === "IfStatement" && - node.consequent.type !== "BlockStatement" - ) { - nodesToCheck = [node.consequent]; - } else if (Array.isArray(node.body)) { - nodesToCheck = node.body; - } else { - nodesToCheck = [node.body]; - } - - if (nodesToCheck.length > 0) { - checkNodesIndent(nodesToCheck, indent + indentSize); - } - - if (node.type === "BlockStatement") { - checkLastNodeLineIndent(node, indent); - } - } - - /** - * Filter out the elements which are on the same line of each other or the node. - * basically have only 1 elements from each line except the variable declaration line. - * @param {ASTNode} node Variable declaration node - * @returns {ASTNode[]} Filtered elements - */ - function filterOutSameLineVars(node) { - return node.declarations.reduce((finalCollection, elem) => { - const lastElem = finalCollection.at(-1); - - if ( - (elem.loc.start.line !== node.loc.start.line && - !lastElem) || - (lastElem && - lastElem.loc.start.line !== elem.loc.start.line) - ) { - finalCollection.push(elem); - } - - return finalCollection; - }, []); - } - - /** - * Check indentation for variable declarations - * @param {ASTNode} node node to examine - * @returns {void} - */ - function checkIndentInVariableDeclarations(node) { - const elements = filterOutSameLineVars(node); - const nodeIndent = getNodeIndent(node).goodChar; - const lastElement = elements.at(-1); - - const elementsIndent = - nodeIndent + indentSize * options.VariableDeclarator[node.kind]; - - checkNodesIndent(elements, elementsIndent); - - // Only check the last line if there is any token after the last item - if ( - sourceCode.getLastToken(node).loc.end.line <= - lastElement.loc.end.line - ) { - return; - } - - const tokenBeforeLastElement = - sourceCode.getTokenBefore(lastElement); - - if (tokenBeforeLastElement.value === ",") { - // Special case for comma-first syntax where the semicolon is indented - checkLastNodeLineIndent( - node, - getNodeIndent(tokenBeforeLastElement).goodChar, - ); - } else { - checkLastNodeLineIndent(node, elementsIndent - indentSize); - } - } - - /** - * Check and decide whether to check for indentation for blockless nodes - * Scenarios are for or while statements without braces around them - * @param {ASTNode} node node to examine - * @returns {void} - */ - function blockLessNodes(node) { - if (node.body.type !== "BlockStatement") { - blockIndentationCheck(node); - } - } - - /** - * Returns the expected indentation for the case statement - * @param {ASTNode} node node to examine - * @param {number} [providedSwitchIndent] indent for switch statement - * @returns {number} indent size - */ - function expectedCaseIndent(node, providedSwitchIndent) { - const switchNode = - node.type === "SwitchStatement" ? node : node.parent; - const switchIndent = - typeof providedSwitchIndent === "undefined" - ? getNodeIndent(switchNode).goodChar - : providedSwitchIndent; - let caseIndent; - - if (caseIndentStore[switchNode.loc.start.line]) { - return caseIndentStore[switchNode.loc.start.line]; - } - - if (switchNode.cases.length > 0 && options.SwitchCase === 0) { - caseIndent = switchIndent; - } else { - caseIndent = switchIndent + indentSize * options.SwitchCase; - } - - caseIndentStore[switchNode.loc.start.line] = caseIndent; - return caseIndent; - } - - /** - * Checks whether a return statement is wrapped in () - * @param {ASTNode} node node to examine - * @returns {boolean} the result - */ - function isWrappedInParenthesis(node) { - const regex = /^return\s*\(\s*\)/u; - - const statementWithoutArgument = sourceCode - .getText(node) - .replace(sourceCode.getText(node.argument), ""); - - return regex.test(statementWithoutArgument); - } - - return { - Program(node) { - if (node.body.length > 0) { - // Root nodes should have no indent - checkNodesIndent(node.body, getNodeIndent(node).goodChar); - } - }, - - ClassBody: blockIndentationCheck, - - BlockStatement: blockIndentationCheck, - - WhileStatement: blockLessNodes, - - ForStatement: blockLessNodes, - - ForInStatement: blockLessNodes, - - ForOfStatement: blockLessNodes, - - DoWhileStatement: blockLessNodes, - - IfStatement(node) { - if ( - node.consequent.type !== "BlockStatement" && - node.consequent.loc.start.line > node.loc.start.line - ) { - blockIndentationCheck(node); - } - }, - - VariableDeclaration(node) { - if ( - node.declarations.at(-1).loc.start.line > - node.declarations[0].loc.start.line - ) { - checkIndentInVariableDeclarations(node); - } - }, - - ObjectExpression(node) { - checkIndentInArrayOrObjectBlock(node); - }, - - ArrayExpression(node) { - checkIndentInArrayOrObjectBlock(node); - }, - - MemberExpression(node) { - if (typeof options.MemberExpression === "undefined") { - return; - } - - if (isSingleLineNode(node)) { - return; - } - - /* - * The typical layout of variable declarations and assignments - * alter the expectation of correct indentation. Skip them. - * TODO: Add appropriate configuration options for variable - * declarations and assignments. - */ - if ( - getParentNodeByType(node, "VariableDeclarator", [ - "FunctionExpression", - "ArrowFunctionExpression", - ]) - ) { - return; - } - - if ( - getParentNodeByType(node, "AssignmentExpression", [ - "FunctionExpression", - ]) - ) { - return; - } - - const propertyIndent = - getNodeIndent(node).goodChar + - indentSize * options.MemberExpression; - - const checkNodes = [node.property]; - - const dot = sourceCode.getTokenBefore(node.property); - - if (dot.type === "Punctuator" && dot.value === ".") { - checkNodes.push(dot); - } - - checkNodesIndent(checkNodes, propertyIndent); - }, - - SwitchStatement(node) { - // Switch is not a 'BlockStatement' - const switchIndent = getNodeIndent(node).goodChar; - const caseIndent = expectedCaseIndent(node, switchIndent); - - checkNodesIndent(node.cases, caseIndent); - - checkLastNodeLineIndent(node, switchIndent); - }, - - SwitchCase(node) { - // Skip inline cases - if (isSingleLineNode(node)) { - return; - } - const caseIndent = expectedCaseIndent(node); - - checkNodesIndent(node.consequent, caseIndent + indentSize); - }, - - FunctionDeclaration(node) { - if (isSingleLineNode(node)) { - return; - } - if ( - options.FunctionDeclaration.parameters === "first" && - node.params.length - ) { - checkNodesIndent( - node.params.slice(1), - node.params[0].loc.start.column, - ); - } else if (options.FunctionDeclaration.parameters !== null) { - checkNodesIndent( - node.params, - getNodeIndent(node).goodChar + - indentSize * options.FunctionDeclaration.parameters, - ); - } - }, - - FunctionExpression(node) { - if (isSingleLineNode(node)) { - return; - } - if ( - options.FunctionExpression.parameters === "first" && - node.params.length - ) { - checkNodesIndent( - node.params.slice(1), - node.params[0].loc.start.column, - ); - } else if (options.FunctionExpression.parameters !== null) { - checkNodesIndent( - node.params, - getNodeIndent(node).goodChar + - indentSize * options.FunctionExpression.parameters, - ); - } - }, - - ReturnStatement(node) { - if (isSingleLineNode(node)) { - return; - } - - const firstLineIndent = getNodeIndent(node).goodChar; - - // in case if return statement is wrapped in parenthesis - if (isWrappedInParenthesis(node)) { - checkLastReturnStatementLineIndent(node, firstLineIndent); - } else { - checkNodeIndent(node, firstLineIndent); - } - }, - - CallExpression(node) { - if (isSingleLineNode(node)) { - return; - } - if ( - options.CallExpression.arguments === "first" && - node.arguments.length - ) { - checkNodesIndent( - node.arguments.slice(1), - node.arguments[0].loc.start.column, - ); - } else if (options.CallExpression.arguments !== null) { - checkNodesIndent( - node.arguments, - getNodeIndent(node).goodChar + - indentSize * options.CallExpression.arguments, - ); - } - }, - }; - }, + meta: { + type: "layout", + + docs: { + description: "Enforce consistent indentation", + recommended: false, + url: "https://eslint.org/docs/latest/rules/indent-legacy" + }, + + deprecated: true, + + replacedBy: ["indent"], + + fixable: "whitespace", + + schema: [ + { + oneOf: [ + { + enum: ["tab"] + }, + { + type: "integer", + minimum: 0 + } + ] + }, + { + type: "object", + properties: { + SwitchCase: { + type: "integer", + minimum: 0 + }, + VariableDeclarator: { + oneOf: [ + { + type: "integer", + minimum: 0 + }, + { + type: "object", + properties: { + var: { + type: "integer", + minimum: 0 + }, + let: { + type: "integer", + minimum: 0 + }, + const: { + type: "integer", + minimum: 0 + } + } + } + ] + }, + outerIIFEBody: { + type: "integer", + minimum: 0 + }, + MemberExpression: { + type: "integer", + minimum: 0 + }, + FunctionDeclaration: { + type: "object", + properties: { + parameters: { + oneOf: [ + { + type: "integer", + minimum: 0 + }, + { + enum: ["first"] + } + ] + }, + body: { + type: "integer", + minimum: 0 + } + } + }, + FunctionExpression: { + type: "object", + properties: { + parameters: { + oneOf: [ + { + type: "integer", + minimum: 0 + }, + { + enum: ["first"] + } + ] + }, + body: { + type: "integer", + minimum: 0 + } + } + }, + CallExpression: { + type: "object", + properties: { + parameters: { + oneOf: [ + { + type: "integer", + minimum: 0 + }, + { + enum: ["first"] + } + ] + } + } + }, + ArrayExpression: { + oneOf: [ + { + type: "integer", + minimum: 0 + }, + { + enum: ["first"] + } + ] + }, + ObjectExpression: { + oneOf: [ + { + type: "integer", + minimum: 0 + }, + { + enum: ["first"] + } + ] + } + }, + additionalProperties: false + } + ], + messages: { + expected: "Expected indentation of {{expected}} but found {{actual}}." + } + }, + + create(context) { + const DEFAULT_VARIABLE_INDENT = 1; + const DEFAULT_PARAMETER_INDENT = null; // For backwards compatibility, don't check parameter indentation unless specified in the config + const DEFAULT_FUNCTION_BODY_INDENT = 1; + + let indentType = "space"; + let indentSize = 4; + const options = { + SwitchCase: 0, + VariableDeclarator: { + var: DEFAULT_VARIABLE_INDENT, + let: DEFAULT_VARIABLE_INDENT, + const: DEFAULT_VARIABLE_INDENT + }, + outerIIFEBody: null, + FunctionDeclaration: { + parameters: DEFAULT_PARAMETER_INDENT, + body: DEFAULT_FUNCTION_BODY_INDENT + }, + FunctionExpression: { + parameters: DEFAULT_PARAMETER_INDENT, + body: DEFAULT_FUNCTION_BODY_INDENT + }, + CallExpression: { + arguments: DEFAULT_PARAMETER_INDENT + }, + ArrayExpression: 1, + ObjectExpression: 1 + }; + + const sourceCode = context.sourceCode; + + if (context.options.length) { + if (context.options[0] === "tab") { + indentSize = 1; + indentType = "tab"; + } else /* c8 ignore start */ if (typeof context.options[0] === "number") { + indentSize = context.options[0]; + indentType = "space"; + }/* c8 ignore stop */ + + if (context.options[1]) { + const opts = context.options[1]; + + options.SwitchCase = opts.SwitchCase || 0; + const variableDeclaratorRules = opts.VariableDeclarator; + + if (typeof variableDeclaratorRules === "number") { + options.VariableDeclarator = { + var: variableDeclaratorRules, + let: variableDeclaratorRules, + const: variableDeclaratorRules + }; + } else if (typeof variableDeclaratorRules === "object") { + Object.assign(options.VariableDeclarator, variableDeclaratorRules); + } + + if (typeof opts.outerIIFEBody === "number") { + options.outerIIFEBody = opts.outerIIFEBody; + } + + if (typeof opts.MemberExpression === "number") { + options.MemberExpression = opts.MemberExpression; + } + + if (typeof opts.FunctionDeclaration === "object") { + Object.assign(options.FunctionDeclaration, opts.FunctionDeclaration); + } + + if (typeof opts.FunctionExpression === "object") { + Object.assign(options.FunctionExpression, opts.FunctionExpression); + } + + if (typeof opts.CallExpression === "object") { + Object.assign(options.CallExpression, opts.CallExpression); + } + + if (typeof opts.ArrayExpression === "number" || typeof opts.ArrayExpression === "string") { + options.ArrayExpression = opts.ArrayExpression; + } + + if (typeof opts.ObjectExpression === "number" || typeof opts.ObjectExpression === "string") { + options.ObjectExpression = opts.ObjectExpression; + } + } + } + + const caseIndentStore = {}; + + /** + * Creates an error message for a line, given the expected/actual indentation. + * @param {int} expectedAmount The expected amount of indentation characters for this line + * @param {int} actualSpaces The actual number of indentation spaces that were found on this line + * @param {int} actualTabs The actual number of indentation tabs that were found on this line + * @returns {string} An error message for this line + */ + function createErrorMessageData(expectedAmount, actualSpaces, actualTabs) { + const expectedStatement = `${expectedAmount} ${indentType}${expectedAmount === 1 ? "" : "s"}`; // e.g. "2 tabs" + const foundSpacesWord = `space${actualSpaces === 1 ? "" : "s"}`; // e.g. "space" + const foundTabsWord = `tab${actualTabs === 1 ? "" : "s"}`; // e.g. "tabs" + let foundStatement; + + if (actualSpaces > 0 && actualTabs > 0) { + foundStatement = `${actualSpaces} ${foundSpacesWord} and ${actualTabs} ${foundTabsWord}`; // e.g. "1 space and 2 tabs" + } else if (actualSpaces > 0) { + + /* + * Abbreviate the message if the expected indentation is also spaces. + * e.g. 'Expected 4 spaces but found 2' rather than 'Expected 4 spaces but found 2 spaces' + */ + foundStatement = indentType === "space" ? actualSpaces : `${actualSpaces} ${foundSpacesWord}`; + } else if (actualTabs > 0) { + foundStatement = indentType === "tab" ? actualTabs : `${actualTabs} ${foundTabsWord}`; + } else { + foundStatement = "0"; + } + return { + expected: expectedStatement, + actual: foundStatement + }; + } + + /** + * Reports a given indent violation + * @param {ASTNode} node Node violating the indent rule + * @param {int} needed Expected indentation character count + * @param {int} gottenSpaces Indentation space count in the actual node/code + * @param {int} gottenTabs Indentation tab count in the actual node/code + * @param {Object} [loc] Error line and column location + * @param {boolean} isLastNodeCheck Is the error for last node check + * @returns {void} + */ + function report(node, needed, gottenSpaces, gottenTabs, loc, isLastNodeCheck) { + if (gottenSpaces && gottenTabs) { + + // To avoid conflicts with `no-mixed-spaces-and-tabs`, don't report lines that have both spaces and tabs. + return; + } + + const desiredIndent = (indentType === "space" ? " " : "\t").repeat(needed); + + const textRange = isLastNodeCheck + ? [node.range[1] - node.loc.end.column, node.range[1] - node.loc.end.column + gottenSpaces + gottenTabs] + : [node.range[0] - node.loc.start.column, node.range[0] - node.loc.start.column + gottenSpaces + gottenTabs]; + + context.report({ + node, + loc, + messageId: "expected", + data: createErrorMessageData(needed, gottenSpaces, gottenTabs), + fix: fixer => fixer.replaceTextRange(textRange, desiredIndent) + }); + } + + /** + * Get the actual indent of node + * @param {ASTNode|Token} node Node to examine + * @param {boolean} [byLastLine=false] get indent of node's last line + * @returns {Object} The node's indent. Contains keys `space` and `tab`, representing the indent of each character. Also + * contains keys `goodChar` and `badChar`, where `goodChar` is the amount of the user's desired indentation character, and + * `badChar` is the amount of the other indentation character. + */ + function getNodeIndent(node, byLastLine) { + const token = byLastLine ? sourceCode.getLastToken(node) : sourceCode.getFirstToken(node); + const srcCharsBeforeNode = sourceCode.getText(token, token.loc.start.column).split(""); + const indentChars = srcCharsBeforeNode.slice(0, srcCharsBeforeNode.findIndex(char => char !== " " && char !== "\t")); + const spaces = indentChars.filter(char => char === " ").length; + const tabs = indentChars.filter(char => char === "\t").length; + + return { + space: spaces, + tab: tabs, + goodChar: indentType === "space" ? spaces : tabs, + badChar: indentType === "space" ? tabs : spaces + }; + } + + /** + * Checks node is the first in its own start line. By default it looks by start line. + * @param {ASTNode} node The node to check + * @param {boolean} [byEndLocation=false] Lookup based on start position or end + * @returns {boolean} true if its the first in the its start line + */ + function isNodeFirstInLine(node, byEndLocation) { + const firstToken = byEndLocation === true ? sourceCode.getLastToken(node, 1) : sourceCode.getTokenBefore(node), + startLine = byEndLocation === true ? node.loc.end.line : node.loc.start.line, + endLine = firstToken ? firstToken.loc.end.line : -1; + + return startLine !== endLine; + } + + /** + * Check indent for node + * @param {ASTNode} node Node to check + * @param {int} neededIndent needed indent + * @returns {void} + */ + function checkNodeIndent(node, neededIndent) { + const actualIndent = getNodeIndent(node, false); + + if ( + node.type !== "ArrayExpression" && + node.type !== "ObjectExpression" && + (actualIndent.goodChar !== neededIndent || actualIndent.badChar !== 0) && + isNodeFirstInLine(node) + ) { + report(node, neededIndent, actualIndent.space, actualIndent.tab); + } + + if (node.type === "IfStatement" && node.alternate) { + const elseToken = sourceCode.getTokenBefore(node.alternate); + + checkNodeIndent(elseToken, neededIndent); + + if (!isNodeFirstInLine(node.alternate)) { + checkNodeIndent(node.alternate, neededIndent); + } + } + + if (node.type === "TryStatement" && node.handler) { + const catchToken = sourceCode.getFirstToken(node.handler); + + checkNodeIndent(catchToken, neededIndent); + } + + if (node.type === "TryStatement" && node.finalizer) { + const finallyToken = sourceCode.getTokenBefore(node.finalizer); + + checkNodeIndent(finallyToken, neededIndent); + } + + if (node.type === "DoWhileStatement") { + const whileToken = sourceCode.getTokenAfter(node.body); + + checkNodeIndent(whileToken, neededIndent); + } + } + + /** + * Check indent for nodes list + * @param {ASTNode[]} nodes list of node objects + * @param {int} indent needed indent + * @returns {void} + */ + function checkNodesIndent(nodes, indent) { + nodes.forEach(node => checkNodeIndent(node, indent)); + } + + /** + * Check last node line indent this detects, that block closed correctly + * @param {ASTNode} node Node to examine + * @param {int} lastLineIndent needed indent + * @returns {void} + */ + function checkLastNodeLineIndent(node, lastLineIndent) { + const lastToken = sourceCode.getLastToken(node); + const endIndent = getNodeIndent(lastToken, true); + + if ((endIndent.goodChar !== lastLineIndent || endIndent.badChar !== 0) && isNodeFirstInLine(node, true)) { + report( + node, + lastLineIndent, + endIndent.space, + endIndent.tab, + { line: lastToken.loc.start.line, column: lastToken.loc.start.column }, + true + ); + } + } + + /** + * Check last node line indent this detects, that block closed correctly + * This function for more complicated return statement case, where closing parenthesis may be followed by ';' + * @param {ASTNode} node Node to examine + * @param {int} firstLineIndent first line needed indent + * @returns {void} + */ + function checkLastReturnStatementLineIndent(node, firstLineIndent) { + + /* + * in case if return statement ends with ');' we have traverse back to ')' + * otherwise we'll measure indent for ';' and replace ')' + */ + const lastToken = sourceCode.getLastToken(node, astUtils.isClosingParenToken); + const textBeforeClosingParenthesis = sourceCode.getText(lastToken, lastToken.loc.start.column).slice(0, -1); + + if (textBeforeClosingParenthesis.trim()) { + + // There are tokens before the closing paren, don't report this case + return; + } + + const endIndent = getNodeIndent(lastToken, true); + + if (endIndent.goodChar !== firstLineIndent) { + report( + node, + firstLineIndent, + endIndent.space, + endIndent.tab, + { line: lastToken.loc.start.line, column: lastToken.loc.start.column }, + true + ); + } + } + + /** + * Check first node line indent is correct + * @param {ASTNode} node Node to examine + * @param {int} firstLineIndent needed indent + * @returns {void} + */ + function checkFirstNodeLineIndent(node, firstLineIndent) { + const startIndent = getNodeIndent(node, false); + + if ((startIndent.goodChar !== firstLineIndent || startIndent.badChar !== 0) && isNodeFirstInLine(node)) { + report( + node, + firstLineIndent, + startIndent.space, + startIndent.tab, + { line: node.loc.start.line, column: node.loc.start.column } + ); + } + } + + /** + * Returns a parent node of given node based on a specified type + * if not present then return null + * @param {ASTNode} node node to examine + * @param {string} type type that is being looked for + * @param {string} stopAtList end points for the evaluating code + * @returns {ASTNode|void} if found then node otherwise null + */ + function getParentNodeByType(node, type, stopAtList) { + let parent = node.parent; + const stopAtSet = new Set(stopAtList || ["Program"]); + + while (parent.type !== type && !stopAtSet.has(parent.type) && parent.type !== "Program") { + parent = parent.parent; + } + + return parent.type === type ? parent : null; + } + + /** + * Returns the VariableDeclarator based on the current node + * if not present then return null + * @param {ASTNode} node node to examine + * @returns {ASTNode|void} if found then node otherwise null + */ + function getVariableDeclaratorNode(node) { + return getParentNodeByType(node, "VariableDeclarator"); + } + + /** + * Check to see if the node is part of the multi-line variable declaration. + * Also if its on the same line as the varNode + * @param {ASTNode} node node to check + * @param {ASTNode} varNode variable declaration node to check against + * @returns {boolean} True if all the above condition satisfy + */ + function isNodeInVarOnTop(node, varNode) { + return varNode && + varNode.parent.loc.start.line === node.loc.start.line && + varNode.parent.declarations.length > 1; + } + + /** + * Check to see if the argument before the callee node is multi-line and + * there should only be 1 argument before the callee node + * @param {ASTNode} node node to check + * @returns {boolean} True if arguments are multi-line + */ + function isArgBeforeCalleeNodeMultiline(node) { + const parent = node.parent; + + if (parent.arguments.length >= 2 && parent.arguments[1] === node) { + return parent.arguments[0].loc.end.line > parent.arguments[0].loc.start.line; + } + + return false; + } + + /** + * Check to see if the node is a file level IIFE + * @param {ASTNode} node The function node to check. + * @returns {boolean} True if the node is the outer IIFE + */ + function isOuterIIFE(node) { + const parent = node.parent; + let stmt = parent.parent; + + /* + * Verify that the node is an IIEF + */ + if ( + parent.type !== "CallExpression" || + parent.callee !== node) { + + return false; + } + + /* + * Navigate legal ancestors to determine whether this IIEF is outer + */ + while ( + stmt.type === "UnaryExpression" && ( + stmt.operator === "!" || + stmt.operator === "~" || + stmt.operator === "+" || + stmt.operator === "-") || + stmt.type === "AssignmentExpression" || + stmt.type === "LogicalExpression" || + stmt.type === "SequenceExpression" || + stmt.type === "VariableDeclarator") { + + stmt = stmt.parent; + } + + return (( + stmt.type === "ExpressionStatement" || + stmt.type === "VariableDeclaration") && + stmt.parent && stmt.parent.type === "Program" + ); + } + + /** + * Check indent for function block content + * @param {ASTNode} node A BlockStatement node that is inside of a function. + * @returns {void} + */ + function checkIndentInFunctionBlock(node) { + + /* + * Search first caller in chain. + * Ex.: + * + * Models <- Identifier + * .User + * .find() + * .exec(function() { + * // function body + * }); + * + * Looks for 'Models' + */ + const calleeNode = node.parent; // FunctionExpression + let indent; + + if (calleeNode.parent && + (calleeNode.parent.type === "Property" || + calleeNode.parent.type === "ArrayExpression")) { + + // If function is part of array or object, comma can be put at left + indent = getNodeIndent(calleeNode, false).goodChar; + } else { + + // If function is standalone, simple calculate indent + indent = getNodeIndent(calleeNode).goodChar; + } + + if (calleeNode.parent.type === "CallExpression") { + const calleeParent = calleeNode.parent; + + if (calleeNode.type !== "FunctionExpression" && calleeNode.type !== "ArrowFunctionExpression") { + if (calleeParent && calleeParent.loc.start.line < node.loc.start.line) { + indent = getNodeIndent(calleeParent).goodChar; + } + } else { + if (isArgBeforeCalleeNodeMultiline(calleeNode) && + calleeParent.callee.loc.start.line === calleeParent.callee.loc.end.line && + !isNodeFirstInLine(calleeNode)) { + indent = getNodeIndent(calleeParent).goodChar; + } + } + } + + /* + * function body indent should be indent + indent size, unless this + * is a FunctionDeclaration, FunctionExpression, or outer IIFE and the corresponding options are enabled. + */ + let functionOffset = indentSize; + + if (options.outerIIFEBody !== null && isOuterIIFE(calleeNode)) { + functionOffset = options.outerIIFEBody * indentSize; + } else if (calleeNode.type === "FunctionExpression") { + functionOffset = options.FunctionExpression.body * indentSize; + } else if (calleeNode.type === "FunctionDeclaration") { + functionOffset = options.FunctionDeclaration.body * indentSize; + } + indent += functionOffset; + + // check if the node is inside a variable + const parentVarNode = getVariableDeclaratorNode(node); + + if (parentVarNode && isNodeInVarOnTop(node, parentVarNode)) { + indent += indentSize * options.VariableDeclarator[parentVarNode.parent.kind]; + } + + if (node.body.length > 0) { + checkNodesIndent(node.body, indent); + } + + checkLastNodeLineIndent(node, indent - functionOffset); + } + + + /** + * Checks if the given node starts and ends on the same line + * @param {ASTNode} node The node to check + * @returns {boolean} Whether or not the block starts and ends on the same line. + */ + function isSingleLineNode(node) { + const lastToken = sourceCode.getLastToken(node), + startLine = node.loc.start.line, + endLine = lastToken.loc.end.line; + + return startLine === endLine; + } + + /** + * Check indent for array block content or object block content + * @param {ASTNode} node node to examine + * @returns {void} + */ + function checkIndentInArrayOrObjectBlock(node) { + + // Skip inline + if (isSingleLineNode(node)) { + return; + } + + let elements = (node.type === "ArrayExpression") ? node.elements : node.properties; + + // filter out empty elements example would be [ , 2] so remove first element as espree considers it as null + elements = elements.filter(elem => elem !== null); + + let nodeIndent; + let elementsIndent; + const parentVarNode = getVariableDeclaratorNode(node); + + // TODO - come up with a better strategy in future + if (isNodeFirstInLine(node)) { + const parent = node.parent; + + nodeIndent = getNodeIndent(parent).goodChar; + if (!parentVarNode || parentVarNode.loc.start.line !== node.loc.start.line) { + if (parent.type !== "VariableDeclarator" || parentVarNode === parentVarNode.parent.declarations[0]) { + if (parent.type === "VariableDeclarator" && parentVarNode.loc.start.line === parent.loc.start.line) { + nodeIndent += (indentSize * options.VariableDeclarator[parentVarNode.parent.kind]); + } else if (parent.type === "ObjectExpression" || parent.type === "ArrayExpression") { + const parentElements = node.parent.type === "ObjectExpression" ? node.parent.properties : node.parent.elements; + + if (parentElements[0] && + parentElements[0].loc.start.line === parent.loc.start.line && + parentElements[0].loc.end.line !== parent.loc.start.line) { + + /* + * If the first element of the array spans multiple lines, don't increase the expected indentation of the rest. + * e.g. [{ + * foo: 1 + * }, + * { + * bar: 1 + * }] + * the second object is not indented. + */ + } else if (typeof options[parent.type] === "number") { + nodeIndent += options[parent.type] * indentSize; + } else { + nodeIndent = parentElements[0].loc.start.column; + } + } else if (parent.type === "CallExpression" || parent.type === "NewExpression") { + if (typeof options.CallExpression.arguments === "number") { + nodeIndent += options.CallExpression.arguments * indentSize; + } else if (options.CallExpression.arguments === "first") { + if (parent.arguments.includes(node)) { + nodeIndent = parent.arguments[0].loc.start.column; + } + } else { + nodeIndent += indentSize; + } + } else if (parent.type === "LogicalExpression" || parent.type === "ArrowFunctionExpression") { + nodeIndent += indentSize; + } + } + } + + checkFirstNodeLineIndent(node, nodeIndent); + } else { + nodeIndent = getNodeIndent(node).goodChar; + } + + if (options[node.type] === "first") { + elementsIndent = elements.length ? elements[0].loc.start.column : 0; // If there are no elements, elementsIndent doesn't matter. + } else { + elementsIndent = nodeIndent + indentSize * options[node.type]; + } + + /* + * Check if the node is a multiple variable declaration; if so, then + * make sure indentation takes that into account. + */ + if (isNodeInVarOnTop(node, parentVarNode)) { + elementsIndent += indentSize * options.VariableDeclarator[parentVarNode.parent.kind]; + } + + checkNodesIndent(elements, elementsIndent); + + if (elements.length > 0) { + + // Skip last block line check if last item in same line + if (elements[elements.length - 1].loc.end.line === node.loc.end.line) { + return; + } + } + + checkLastNodeLineIndent(node, nodeIndent + + (isNodeInVarOnTop(node, parentVarNode) ? options.VariableDeclarator[parentVarNode.parent.kind] * indentSize : 0)); + } + + /** + * Check if the node or node body is a BlockStatement or not + * @param {ASTNode} node node to test + * @returns {boolean} True if it or its body is a block statement + */ + function isNodeBodyBlock(node) { + return node.type === "BlockStatement" || node.type === "ClassBody" || (node.body && node.body.type === "BlockStatement") || + (node.consequent && node.consequent.type === "BlockStatement"); + } + + /** + * Check indentation for blocks + * @param {ASTNode} node node to check + * @returns {void} + */ + function blockIndentationCheck(node) { + + // Skip inline blocks + if (isSingleLineNode(node)) { + return; + } + + if (node.parent && ( + node.parent.type === "FunctionExpression" || + node.parent.type === "FunctionDeclaration" || + node.parent.type === "ArrowFunctionExpression") + ) { + checkIndentInFunctionBlock(node); + return; + } + + let indent; + let nodesToCheck = []; + + /* + * For this statements we should check indent from statement beginning, + * not from the beginning of the block. + */ + const statementsWithProperties = [ + "IfStatement", "WhileStatement", "ForStatement", "ForInStatement", "ForOfStatement", "DoWhileStatement", "ClassDeclaration", "TryStatement" + ]; + + if (node.parent && statementsWithProperties.includes(node.parent.type) && isNodeBodyBlock(node)) { + indent = getNodeIndent(node.parent).goodChar; + } else if (node.parent && node.parent.type === "CatchClause") { + indent = getNodeIndent(node.parent.parent).goodChar; + } else { + indent = getNodeIndent(node).goodChar; + } + + if (node.type === "IfStatement" && node.consequent.type !== "BlockStatement") { + nodesToCheck = [node.consequent]; + } else if (Array.isArray(node.body)) { + nodesToCheck = node.body; + } else { + nodesToCheck = [node.body]; + } + + if (nodesToCheck.length > 0) { + checkNodesIndent(nodesToCheck, indent + indentSize); + } + + if (node.type === "BlockStatement") { + checkLastNodeLineIndent(node, indent); + } + } + + /** + * Filter out the elements which are on the same line of each other or the node. + * basically have only 1 elements from each line except the variable declaration line. + * @param {ASTNode} node Variable declaration node + * @returns {ASTNode[]} Filtered elements + */ + function filterOutSameLineVars(node) { + return node.declarations.reduce((finalCollection, elem) => { + const lastElem = finalCollection[finalCollection.length - 1]; + + if ((elem.loc.start.line !== node.loc.start.line && !lastElem) || + (lastElem && lastElem.loc.start.line !== elem.loc.start.line)) { + finalCollection.push(elem); + } + + return finalCollection; + }, []); + } + + /** + * Check indentation for variable declarations + * @param {ASTNode} node node to examine + * @returns {void} + */ + function checkIndentInVariableDeclarations(node) { + const elements = filterOutSameLineVars(node); + const nodeIndent = getNodeIndent(node).goodChar; + const lastElement = elements[elements.length - 1]; + + const elementsIndent = nodeIndent + indentSize * options.VariableDeclarator[node.kind]; + + checkNodesIndent(elements, elementsIndent); + + // Only check the last line if there is any token after the last item + if (sourceCode.getLastToken(node).loc.end.line <= lastElement.loc.end.line) { + return; + } + + const tokenBeforeLastElement = sourceCode.getTokenBefore(lastElement); + + if (tokenBeforeLastElement.value === ",") { + + // Special case for comma-first syntax where the semicolon is indented + checkLastNodeLineIndent(node, getNodeIndent(tokenBeforeLastElement).goodChar); + } else { + checkLastNodeLineIndent(node, elementsIndent - indentSize); + } + } + + /** + * Check and decide whether to check for indentation for blockless nodes + * Scenarios are for or while statements without braces around them + * @param {ASTNode} node node to examine + * @returns {void} + */ + function blockLessNodes(node) { + if (node.body.type !== "BlockStatement") { + blockIndentationCheck(node); + } + } + + /** + * Returns the expected indentation for the case statement + * @param {ASTNode} node node to examine + * @param {int} [providedSwitchIndent] indent for switch statement + * @returns {int} indent size + */ + function expectedCaseIndent(node, providedSwitchIndent) { + const switchNode = (node.type === "SwitchStatement") ? node : node.parent; + const switchIndent = typeof providedSwitchIndent === "undefined" + ? getNodeIndent(switchNode).goodChar + : providedSwitchIndent; + let caseIndent; + + if (caseIndentStore[switchNode.loc.start.line]) { + return caseIndentStore[switchNode.loc.start.line]; + } + + if (switchNode.cases.length > 0 && options.SwitchCase === 0) { + caseIndent = switchIndent; + } else { + caseIndent = switchIndent + (indentSize * options.SwitchCase); + } + + caseIndentStore[switchNode.loc.start.line] = caseIndent; + return caseIndent; + + } + + /** + * Checks whether a return statement is wrapped in () + * @param {ASTNode} node node to examine + * @returns {boolean} the result + */ + function isWrappedInParenthesis(node) { + const regex = /^return\s*?\(\s*?\);*?/u; + + const statementWithoutArgument = sourceCode.getText(node).replace( + sourceCode.getText(node.argument), "" + ); + + return regex.test(statementWithoutArgument); + } + + return { + Program(node) { + if (node.body.length > 0) { + + // Root nodes should have no indent + checkNodesIndent(node.body, getNodeIndent(node).goodChar); + } + }, + + ClassBody: blockIndentationCheck, + + BlockStatement: blockIndentationCheck, + + WhileStatement: blockLessNodes, + + ForStatement: blockLessNodes, + + ForInStatement: blockLessNodes, + + ForOfStatement: blockLessNodes, + + DoWhileStatement: blockLessNodes, + + IfStatement(node) { + if (node.consequent.type !== "BlockStatement" && node.consequent.loc.start.line > node.loc.start.line) { + blockIndentationCheck(node); + } + }, + + VariableDeclaration(node) { + if (node.declarations[node.declarations.length - 1].loc.start.line > node.declarations[0].loc.start.line) { + checkIndentInVariableDeclarations(node); + } + }, + + ObjectExpression(node) { + checkIndentInArrayOrObjectBlock(node); + }, + + ArrayExpression(node) { + checkIndentInArrayOrObjectBlock(node); + }, + + MemberExpression(node) { + + if (typeof options.MemberExpression === "undefined") { + return; + } + + if (isSingleLineNode(node)) { + return; + } + + /* + * The typical layout of variable declarations and assignments + * alter the expectation of correct indentation. Skip them. + * TODO: Add appropriate configuration options for variable + * declarations and assignments. + */ + if (getParentNodeByType(node, "VariableDeclarator", ["FunctionExpression", "ArrowFunctionExpression"])) { + return; + } + + if (getParentNodeByType(node, "AssignmentExpression", ["FunctionExpression"])) { + return; + } + + const propertyIndent = getNodeIndent(node).goodChar + indentSize * options.MemberExpression; + + const checkNodes = [node.property]; + + const dot = sourceCode.getTokenBefore(node.property); + + if (dot.type === "Punctuator" && dot.value === ".") { + checkNodes.push(dot); + } + + checkNodesIndent(checkNodes, propertyIndent); + }, + + SwitchStatement(node) { + + // Switch is not a 'BlockStatement' + const switchIndent = getNodeIndent(node).goodChar; + const caseIndent = expectedCaseIndent(node, switchIndent); + + checkNodesIndent(node.cases, caseIndent); + + + checkLastNodeLineIndent(node, switchIndent); + }, + + SwitchCase(node) { + + // Skip inline cases + if (isSingleLineNode(node)) { + return; + } + const caseIndent = expectedCaseIndent(node); + + checkNodesIndent(node.consequent, caseIndent + indentSize); + }, + + FunctionDeclaration(node) { + if (isSingleLineNode(node)) { + return; + } + if (options.FunctionDeclaration.parameters === "first" && node.params.length) { + checkNodesIndent(node.params.slice(1), node.params[0].loc.start.column); + } else if (options.FunctionDeclaration.parameters !== null) { + checkNodesIndent(node.params, getNodeIndent(node).goodChar + indentSize * options.FunctionDeclaration.parameters); + } + }, + + FunctionExpression(node) { + if (isSingleLineNode(node)) { + return; + } + if (options.FunctionExpression.parameters === "first" && node.params.length) { + checkNodesIndent(node.params.slice(1), node.params[0].loc.start.column); + } else if (options.FunctionExpression.parameters !== null) { + checkNodesIndent(node.params, getNodeIndent(node).goodChar + indentSize * options.FunctionExpression.parameters); + } + }, + + ReturnStatement(node) { + if (isSingleLineNode(node)) { + return; + } + + const firstLineIndent = getNodeIndent(node).goodChar; + + // in case if return statement is wrapped in parenthesis + if (isWrappedInParenthesis(node)) { + checkLastReturnStatementLineIndent(node, firstLineIndent); + } else { + checkNodeIndent(node, firstLineIndent); + } + }, + + CallExpression(node) { + if (isSingleLineNode(node)) { + return; + } + if (options.CallExpression.arguments === "first" && node.arguments.length) { + checkNodesIndent(node.arguments.slice(1), node.arguments[0].loc.start.column); + } else if (options.CallExpression.arguments !== null) { + checkNodesIndent(node.arguments, getNodeIndent(node).goodChar + indentSize * options.CallExpression.arguments); + } + } + + }; + + } }; diff --git a/node_modules/eslint/lib/rules/indent.js b/node_modules/eslint/lib/rules/indent.js index c6c18759a..9bcbd640c 100644 --- a/node_modules/eslint/lib/rules/indent.js +++ b/node_modules/eslint/lib/rules/indent.js @@ -20,93 +20,93 @@ const astUtils = require("./utils/ast-utils"); //------------------------------------------------------------------------------ const KNOWN_NODES = new Set([ - "AssignmentExpression", - "AssignmentPattern", - "ArrayExpression", - "ArrayPattern", - "ArrowFunctionExpression", - "AwaitExpression", - "BlockStatement", - "BinaryExpression", - "BreakStatement", - "CallExpression", - "CatchClause", - "ChainExpression", - "ClassBody", - "ClassDeclaration", - "ClassExpression", - "ConditionalExpression", - "ContinueStatement", - "DoWhileStatement", - "DebuggerStatement", - "EmptyStatement", - "ExperimentalRestProperty", - "ExperimentalSpreadProperty", - "ExpressionStatement", - "ForStatement", - "ForInStatement", - "ForOfStatement", - "FunctionDeclaration", - "FunctionExpression", - "Identifier", - "IfStatement", - "Literal", - "LabeledStatement", - "LogicalExpression", - "MemberExpression", - "MetaProperty", - "MethodDefinition", - "NewExpression", - "ObjectExpression", - "ObjectPattern", - "PrivateIdentifier", - "Program", - "Property", - "PropertyDefinition", - "RestElement", - "ReturnStatement", - "SequenceExpression", - "SpreadElement", - "StaticBlock", - "Super", - "SwitchCase", - "SwitchStatement", - "TaggedTemplateExpression", - "TemplateElement", - "TemplateLiteral", - "ThisExpression", - "ThrowStatement", - "TryStatement", - "UnaryExpression", - "UpdateExpression", - "VariableDeclaration", - "VariableDeclarator", - "WhileStatement", - "WithStatement", - "YieldExpression", - "JSXFragment", - "JSXOpeningFragment", - "JSXClosingFragment", - "JSXIdentifier", - "JSXNamespacedName", - "JSXMemberExpression", - "JSXEmptyExpression", - "JSXExpressionContainer", - "JSXElement", - "JSXClosingElement", - "JSXOpeningElement", - "JSXAttribute", - "JSXSpreadAttribute", - "JSXText", - "ExportDefaultDeclaration", - "ExportNamedDeclaration", - "ExportAllDeclaration", - "ExportSpecifier", - "ImportDeclaration", - "ImportSpecifier", - "ImportDefaultSpecifier", - "ImportNamespaceSpecifier", - "ImportExpression", + "AssignmentExpression", + "AssignmentPattern", + "ArrayExpression", + "ArrayPattern", + "ArrowFunctionExpression", + "AwaitExpression", + "BlockStatement", + "BinaryExpression", + "BreakStatement", + "CallExpression", + "CatchClause", + "ChainExpression", + "ClassBody", + "ClassDeclaration", + "ClassExpression", + "ConditionalExpression", + "ContinueStatement", + "DoWhileStatement", + "DebuggerStatement", + "EmptyStatement", + "ExperimentalRestProperty", + "ExperimentalSpreadProperty", + "ExpressionStatement", + "ForStatement", + "ForInStatement", + "ForOfStatement", + "FunctionDeclaration", + "FunctionExpression", + "Identifier", + "IfStatement", + "Literal", + "LabeledStatement", + "LogicalExpression", + "MemberExpression", + "MetaProperty", + "MethodDefinition", + "NewExpression", + "ObjectExpression", + "ObjectPattern", + "PrivateIdentifier", + "Program", + "Property", + "PropertyDefinition", + "RestElement", + "ReturnStatement", + "SequenceExpression", + "SpreadElement", + "StaticBlock", + "Super", + "SwitchCase", + "SwitchStatement", + "TaggedTemplateExpression", + "TemplateElement", + "TemplateLiteral", + "ThisExpression", + "ThrowStatement", + "TryStatement", + "UnaryExpression", + "UpdateExpression", + "VariableDeclaration", + "VariableDeclarator", + "WhileStatement", + "WithStatement", + "YieldExpression", + "JSXFragment", + "JSXOpeningFragment", + "JSXClosingFragment", + "JSXIdentifier", + "JSXNamespacedName", + "JSXMemberExpression", + "JSXEmptyExpression", + "JSXExpressionContainer", + "JSXElement", + "JSXClosingElement", + "JSXOpeningElement", + "JSXAttribute", + "JSXSpreadAttribute", + "JSXText", + "ExportDefaultDeclaration", + "ExportNamedDeclaration", + "ExportAllDeclaration", + "ExportSpecifier", + "ImportDeclaration", + "ImportSpecifier", + "ImportDefaultSpecifier", + "ImportNamespaceSpecifier", + "ImportExpression" ]); /* @@ -122,2213 +122,1682 @@ const KNOWN_NODES = new Set([ * and report the token if the two values are not equal. */ + /** * A mutable map that stores (key, value) pairs. The keys are numeric indices, and must be unique. * This is intended to be a generic wrapper around a map with non-negative integer keys, so that the underlying implementation * can easily be swapped out. */ class IndexMap { - /** - * Creates an empty map - * @param {number} maxKey The maximum key - */ - constructor(maxKey) { - // Initializing the array with the maximum expected size avoids dynamic reallocations that could degrade performance. - this._values = Array(maxKey + 1); - } - - /** - * Inserts an entry into the map. - * @param {number} key The entry's key - * @param {any} value The entry's value - * @returns {void} - */ - insert(key, value) { - this._values[key] = value; - } - - /** - * Finds the value of the entry with the largest key less than or equal to the provided key - * @param {number} key The provided key - * @returns {*|undefined} The value of the found entry, or undefined if no such entry exists. - */ - findLastNotAfter(key) { - const values = this._values; - - for (let index = key; index >= 0; index--) { - const value = values[index]; - - if (value) { - return value; - } - } - return void 0; - } - - /** - * Deletes all of the keys in the interval [start, end) - * @param {number} start The start of the range - * @param {number} end The end of the range - * @returns {void} - */ - deleteRange(start, end) { - this._values.fill(void 0, start, end); - } + + /** + * Creates an empty map + * @param {number} maxKey The maximum key + */ + constructor(maxKey) { + + // Initializing the array with the maximum expected size avoids dynamic reallocations that could degrade performance. + this._values = Array(maxKey + 1); + } + + /** + * Inserts an entry into the map. + * @param {number} key The entry's key + * @param {any} value The entry's value + * @returns {void} + */ + insert(key, value) { + this._values[key] = value; + } + + /** + * Finds the value of the entry with the largest key less than or equal to the provided key + * @param {number} key The provided key + * @returns {*|undefined} The value of the found entry, or undefined if no such entry exists. + */ + findLastNotAfter(key) { + const values = this._values; + + for (let index = key; index >= 0; index--) { + const value = values[index]; + + if (value) { + return value; + } + } + return void 0; + } + + /** + * Deletes all of the keys in the interval [start, end) + * @param {number} start The start of the range + * @param {number} end The end of the range + * @returns {void} + */ + deleteRange(start, end) { + this._values.fill(void 0, start, end); + } } /** * A helper class to get token-based info related to indentation */ class TokenInfo { - /** - * @param {SourceCode} sourceCode A SourceCode object - */ - constructor(sourceCode) { - this.sourceCode = sourceCode; - this.firstTokensByLineNumber = new Map(); - const tokens = sourceCode.tokensAndComments; - - for (let i = 0; i < tokens.length; i++) { - const token = tokens[i]; - - if (!this.firstTokensByLineNumber.has(token.loc.start.line)) { - this.firstTokensByLineNumber.set(token.loc.start.line, token); - } - if ( - !this.firstTokensByLineNumber.has(token.loc.end.line) && - sourceCode.text - .slice( - token.range[1] - token.loc.end.column, - token.range[1], - ) - .trim() - ) { - this.firstTokensByLineNumber.set(token.loc.end.line, token); - } - } - } - - /** - * Gets the first token on a given token's line - * @param {Token|ASTNode} token a node or token - * @returns {Token} The first token on the given line - */ - getFirstTokenOfLine(token) { - return this.firstTokensByLineNumber.get(token.loc.start.line); - } - - /** - * Determines whether a token is the first token in its line - * @param {Token} token The token - * @returns {boolean} `true` if the token is the first on its line - */ - isFirstTokenOfLine(token) { - return this.getFirstTokenOfLine(token) === token; - } - - /** - * Get the actual indent of a token - * @param {Token} token Token to examine. This should be the first token on its line. - * @returns {string} The indentation characters that precede the token - */ - getTokenIndent(token) { - return this.sourceCode.text.slice( - token.range[0] - token.loc.start.column, - token.range[0], - ); - } + + /** + * @param {SourceCode} sourceCode A SourceCode object + */ + constructor(sourceCode) { + this.sourceCode = sourceCode; + this.firstTokensByLineNumber = new Map(); + const tokens = sourceCode.tokensAndComments; + + for (let i = 0; i < tokens.length; i++) { + const token = tokens[i]; + + if (!this.firstTokensByLineNumber.has(token.loc.start.line)) { + this.firstTokensByLineNumber.set(token.loc.start.line, token); + } + if (!this.firstTokensByLineNumber.has(token.loc.end.line) && sourceCode.text.slice(token.range[1] - token.loc.end.column, token.range[1]).trim()) { + this.firstTokensByLineNumber.set(token.loc.end.line, token); + } + } + } + + /** + * Gets the first token on a given token's line + * @param {Token|ASTNode} token a node or token + * @returns {Token} The first token on the given line + */ + getFirstTokenOfLine(token) { + return this.firstTokensByLineNumber.get(token.loc.start.line); + } + + /** + * Determines whether a token is the first token in its line + * @param {Token} token The token + * @returns {boolean} `true` if the token is the first on its line + */ + isFirstTokenOfLine(token) { + return this.getFirstTokenOfLine(token) === token; + } + + /** + * Get the actual indent of a token + * @param {Token} token Token to examine. This should be the first token on its line. + * @returns {string} The indentation characters that precede the token + */ + getTokenIndent(token) { + return this.sourceCode.text.slice(token.range[0] - token.loc.start.column, token.range[0]); + } } /** * A class to store information on desired offsets of tokens from each other */ class OffsetStorage { - /** - * @param {TokenInfo} tokenInfo a TokenInfo instance - * @param {number} indentSize The desired size of each indentation level - * @param {string} indentType The indentation character - * @param {number} maxIndex The maximum end index of any token - */ - constructor(tokenInfo, indentSize, indentType, maxIndex) { - this._tokenInfo = tokenInfo; - this._indentSize = indentSize; - this._indentType = indentType; - - this._indexMap = new IndexMap(maxIndex); - this._indexMap.insert(0, { offset: 0, from: null, force: false }); - - this._lockedFirstTokens = new WeakMap(); - this._desiredIndentCache = new WeakMap(); - this._ignoredTokens = new WeakSet(); - } - - _getOffsetDescriptor(token) { - return this._indexMap.findLastNotAfter(token.range[0]); - } - - /** - * Sets the offset column of token B to match the offset column of token A. - * - **WARNING**: This matches a *column*, even if baseToken is not the first token on its line. In - * most cases, `setDesiredOffset` should be used instead. - * @param {Token} baseToken The first token - * @param {Token} offsetToken The second token, whose offset should be matched to the first token - * @returns {void} - */ - matchOffsetOf(baseToken, offsetToken) { - /* - * lockedFirstTokens is a map from a token whose indentation is controlled by the "first" option to - * the token that it depends on. For example, with the `ArrayExpression: first` option, the first - * token of each element in the array after the first will be mapped to the first token of the first - * element. The desired indentation of each of these tokens is computed based on the desired indentation - * of the "first" element, rather than through the normal offset mechanism. - */ - this._lockedFirstTokens.set(offsetToken, baseToken); - } - - /** - * Sets the desired offset of a token. - * - * This uses a line-based offset collapsing behavior to handle tokens on the same line. - * For example, consider the following two cases: - * - * ( - * [ - * bar - * ] - * ) - * - * ([ - * bar - * ]) - * - * Based on the first case, it's clear that the `bar` token needs to have an offset of 1 indent level (4 spaces) from - * the `[` token, and the `[` token has to have an offset of 1 indent level from the `(` token. Since the `(` token is - * the first on its line (with an indent of 0 spaces), the `bar` token needs to be offset by 2 indent levels (8 spaces) - * from the start of its line. - * - * However, in the second case `bar` should only be indented by 4 spaces. This is because the offset of 1 indent level - * between the `(` and the `[` tokens gets "collapsed" because the two tokens are on the same line. As a result, the - * `(` token is mapped to the `[` token with an offset of 0, and the rule correctly decides that `bar` should be indented - * by 1 indent level from the start of the line. - * - * This is useful because rule listeners can usually just call `setDesiredOffset` for all the tokens in the node, - * without needing to check which lines those tokens are on. - * - * Note that since collapsing only occurs when two tokens are on the same line, there are a few cases where non-intuitive - * behavior can occur. For example, consider the following cases: - * - * foo( - * ). - * bar( - * baz - * ) - * - * foo( - * ).bar( - * baz - * ) - * - * Based on the first example, it would seem that `bar` should be offset by 1 indent level from `foo`, and `baz` - * should be offset by 1 indent level from `bar`. However, this is not correct, because it would result in `baz` - * being indented by 2 indent levels in the second case (since `foo`, `bar`, and `baz` are all on separate lines, no - * collapsing would occur). - * - * Instead, the correct way would be to offset `baz` by 1 level from `bar`, offset `bar` by 1 level from the `)`, and - * offset the `)` by 0 levels from `foo`. This ensures that the offset between `bar` and the `)` are correctly collapsed - * in the second case. - * @param {Token} token The token - * @param {Token} fromToken The token that `token` should be offset from - * @param {number} offset The desired indent level - * @returns {void} - */ - setDesiredOffset(token, fromToken, offset) { - return this.setDesiredOffsets(token.range, fromToken, offset); - } - - /** - * Sets the desired offset of all tokens in a range - * It's common for node listeners in this file to need to apply the same offset to a large, contiguous range of tokens. - * Moreover, the offset of any given token is usually updated multiple times (roughly once for each node that contains - * it). This means that the offset of each token is updated O(AST depth) times. - * It would not be performant to store and update the offsets for each token independently, because the rule would end - * up having a time complexity of O(number of tokens * AST depth), which is quite slow for large files. - * - * Instead, the offset tree is represented as a collection of contiguous offset ranges in a file. For example, the following - * list could represent the state of the offset tree at a given point: - * - * - Tokens starting in the interval [0, 15) are aligned with the beginning of the file - * - Tokens starting in the interval [15, 30) are offset by 1 indent level from the `bar` token - * - Tokens starting in the interval [30, 43) are offset by 1 indent level from the `foo` token - * - Tokens starting in the interval [43, 820) are offset by 2 indent levels from the `bar` token - * - Tokens starting in the interval [820, ∞) are offset by 1 indent level from the `baz` token - * - * The `setDesiredOffsets` methods inserts ranges like the ones above. The third line above would be inserted by using: - * `setDesiredOffsets([30, 43], fooToken, 1);` - * @param {[number, number]} range A [start, end] pair. All tokens with range[0] <= token.start < range[1] will have the offset applied. - * @param {Token} fromToken The token that this is offset from - * @param {number} offset The desired indent level - * @param {boolean} force `true` if this offset should not use the normal collapsing behavior. This should almost always be false. - * @returns {void} - */ - setDesiredOffsets(range, fromToken, offset, force) { - /* - * Offset ranges are stored as a collection of nodes, where each node maps a numeric key to an offset - * descriptor. The tree for the example above would have the following nodes: - * - * * key: 0, value: { offset: 0, from: null } - * * key: 15, value: { offset: 1, from: barToken } - * * key: 30, value: { offset: 1, from: fooToken } - * * key: 43, value: { offset: 2, from: barToken } - * * key: 820, value: { offset: 1, from: bazToken } - * - * To find the offset descriptor for any given token, one needs to find the node with the largest key - * which is <= token.start. To make this operation fast, the nodes are stored in a map indexed by key. - */ - - const descriptorToInsert = { offset, from: fromToken, force }; - - const descriptorAfterRange = this._indexMap.findLastNotAfter(range[1]); - - const fromTokenIsInRange = - fromToken && - fromToken.range[0] >= range[0] && - fromToken.range[1] <= range[1]; - const fromTokenDescriptor = - fromTokenIsInRange && this._getOffsetDescriptor(fromToken); - - // First, remove any existing nodes in the range from the map. - this._indexMap.deleteRange(range[0] + 1, range[1]); - - // Insert a new node into the map for this range - this._indexMap.insert(range[0], descriptorToInsert); - - /* - * To avoid circular offset dependencies, keep the `fromToken` token mapped to whatever it was mapped to previously, - * even if it's in the current range. - */ - if (fromTokenIsInRange) { - this._indexMap.insert(fromToken.range[0], fromTokenDescriptor); - this._indexMap.insert(fromToken.range[1], descriptorToInsert); - } - - /* - * To avoid modifying the offset of tokens after the range, insert another node to keep the offset of the following - * tokens the same as it was before. - */ - this._indexMap.insert(range[1], descriptorAfterRange); - } - - /** - * Gets the desired indent of a token - * @param {Token} token The token - * @returns {string} The desired indent of the token - */ - getDesiredIndent(token) { - if (!this._desiredIndentCache.has(token)) { - if (this._ignoredTokens.has(token)) { - /* - * If the token is ignored, use the actual indent of the token as the desired indent. - * This ensures that no errors are reported for this token. - */ - this._desiredIndentCache.set( - token, - this._tokenInfo.getTokenIndent(token), - ); - } else if (this._lockedFirstTokens.has(token)) { - const firstToken = this._lockedFirstTokens.get(token); - - this._desiredIndentCache.set( - token, - - // (indentation for the first element's line) - this.getDesiredIndent( - this._tokenInfo.getFirstTokenOfLine(firstToken), - ) + - // (space between the start of the first element's line and the first element) - this._indentType.repeat( - firstToken.loc.start.column - - this._tokenInfo.getFirstTokenOfLine(firstToken) - .loc.start.column, - ), - ); - } else { - const offsetInfo = this._getOffsetDescriptor(token); - const offset = - offsetInfo.from && - offsetInfo.from.loc.start.line === token.loc.start.line && - !/^\s*?\n/u.test(token.value) && - !offsetInfo.force - ? 0 - : offsetInfo.offset * this._indentSize; - - this._desiredIndentCache.set( - token, - (offsetInfo.from - ? this.getDesiredIndent(offsetInfo.from) - : "") + this._indentType.repeat(offset), - ); - } - } - return this._desiredIndentCache.get(token); - } - - /** - * Ignores a token, preventing it from being reported. - * @param {Token} token The token - * @returns {void} - */ - ignoreToken(token) { - if (this._tokenInfo.isFirstTokenOfLine(token)) { - this._ignoredTokens.add(token); - } - } - - /** - * Gets the first token that the given token's indentation is dependent on - * @param {Token} token The token - * @returns {Token} The token that the given token depends on, or `null` if the given token is at the top level - */ - getFirstDependency(token) { - return this._getOffsetDescriptor(token).from; - } + + /** + * @param {TokenInfo} tokenInfo a TokenInfo instance + * @param {number} indentSize The desired size of each indentation level + * @param {string} indentType The indentation character + * @param {number} maxIndex The maximum end index of any token + */ + constructor(tokenInfo, indentSize, indentType, maxIndex) { + this._tokenInfo = tokenInfo; + this._indentSize = indentSize; + this._indentType = indentType; + + this._indexMap = new IndexMap(maxIndex); + this._indexMap.insert(0, { offset: 0, from: null, force: false }); + + this._lockedFirstTokens = new WeakMap(); + this._desiredIndentCache = new WeakMap(); + this._ignoredTokens = new WeakSet(); + } + + _getOffsetDescriptor(token) { + return this._indexMap.findLastNotAfter(token.range[0]); + } + + /** + * Sets the offset column of token B to match the offset column of token A. + * - **WARNING**: This matches a *column*, even if baseToken is not the first token on its line. In + * most cases, `setDesiredOffset` should be used instead. + * @param {Token} baseToken The first token + * @param {Token} offsetToken The second token, whose offset should be matched to the first token + * @returns {void} + */ + matchOffsetOf(baseToken, offsetToken) { + + /* + * lockedFirstTokens is a map from a token whose indentation is controlled by the "first" option to + * the token that it depends on. For example, with the `ArrayExpression: first` option, the first + * token of each element in the array after the first will be mapped to the first token of the first + * element. The desired indentation of each of these tokens is computed based on the desired indentation + * of the "first" element, rather than through the normal offset mechanism. + */ + this._lockedFirstTokens.set(offsetToken, baseToken); + } + + /** + * Sets the desired offset of a token. + * + * This uses a line-based offset collapsing behavior to handle tokens on the same line. + * For example, consider the following two cases: + * + * ( + * [ + * bar + * ] + * ) + * + * ([ + * bar + * ]) + * + * Based on the first case, it's clear that the `bar` token needs to have an offset of 1 indent level (4 spaces) from + * the `[` token, and the `[` token has to have an offset of 1 indent level from the `(` token. Since the `(` token is + * the first on its line (with an indent of 0 spaces), the `bar` token needs to be offset by 2 indent levels (8 spaces) + * from the start of its line. + * + * However, in the second case `bar` should only be indented by 4 spaces. This is because the offset of 1 indent level + * between the `(` and the `[` tokens gets "collapsed" because the two tokens are on the same line. As a result, the + * `(` token is mapped to the `[` token with an offset of 0, and the rule correctly decides that `bar` should be indented + * by 1 indent level from the start of the line. + * + * This is useful because rule listeners can usually just call `setDesiredOffset` for all the tokens in the node, + * without needing to check which lines those tokens are on. + * + * Note that since collapsing only occurs when two tokens are on the same line, there are a few cases where non-intuitive + * behavior can occur. For example, consider the following cases: + * + * foo( + * ). + * bar( + * baz + * ) + * + * foo( + * ).bar( + * baz + * ) + * + * Based on the first example, it would seem that `bar` should be offset by 1 indent level from `foo`, and `baz` + * should be offset by 1 indent level from `bar`. However, this is not correct, because it would result in `baz` + * being indented by 2 indent levels in the second case (since `foo`, `bar`, and `baz` are all on separate lines, no + * collapsing would occur). + * + * Instead, the correct way would be to offset `baz` by 1 level from `bar`, offset `bar` by 1 level from the `)`, and + * offset the `)` by 0 levels from `foo`. This ensures that the offset between `bar` and the `)` are correctly collapsed + * in the second case. + * @param {Token} token The token + * @param {Token} fromToken The token that `token` should be offset from + * @param {number} offset The desired indent level + * @returns {void} + */ + setDesiredOffset(token, fromToken, offset) { + return this.setDesiredOffsets(token.range, fromToken, offset); + } + + /** + * Sets the desired offset of all tokens in a range + * It's common for node listeners in this file to need to apply the same offset to a large, contiguous range of tokens. + * Moreover, the offset of any given token is usually updated multiple times (roughly once for each node that contains + * it). This means that the offset of each token is updated O(AST depth) times. + * It would not be performant to store and update the offsets for each token independently, because the rule would end + * up having a time complexity of O(number of tokens * AST depth), which is quite slow for large files. + * + * Instead, the offset tree is represented as a collection of contiguous offset ranges in a file. For example, the following + * list could represent the state of the offset tree at a given point: + * + * - Tokens starting in the interval [0, 15) are aligned with the beginning of the file + * - Tokens starting in the interval [15, 30) are offset by 1 indent level from the `bar` token + * - Tokens starting in the interval [30, 43) are offset by 1 indent level from the `foo` token + * - Tokens starting in the interval [43, 820) are offset by 2 indent levels from the `bar` token + * - Tokens starting in the interval [820, ∞) are offset by 1 indent level from the `baz` token + * + * The `setDesiredOffsets` methods inserts ranges like the ones above. The third line above would be inserted by using: + * `setDesiredOffsets([30, 43], fooToken, 1);` + * @param {[number, number]} range A [start, end] pair. All tokens with range[0] <= token.start < range[1] will have the offset applied. + * @param {Token} fromToken The token that this is offset from + * @param {number} offset The desired indent level + * @param {boolean} force `true` if this offset should not use the normal collapsing behavior. This should almost always be false. + * @returns {void} + */ + setDesiredOffsets(range, fromToken, offset, force) { + + /* + * Offset ranges are stored as a collection of nodes, where each node maps a numeric key to an offset + * descriptor. The tree for the example above would have the following nodes: + * + * * key: 0, value: { offset: 0, from: null } + * * key: 15, value: { offset: 1, from: barToken } + * * key: 30, value: { offset: 1, from: fooToken } + * * key: 43, value: { offset: 2, from: barToken } + * * key: 820, value: { offset: 1, from: bazToken } + * + * To find the offset descriptor for any given token, one needs to find the node with the largest key + * which is <= token.start. To make this operation fast, the nodes are stored in a map indexed by key. + */ + + const descriptorToInsert = { offset, from: fromToken, force }; + + const descriptorAfterRange = this._indexMap.findLastNotAfter(range[1]); + + const fromTokenIsInRange = fromToken && fromToken.range[0] >= range[0] && fromToken.range[1] <= range[1]; + const fromTokenDescriptor = fromTokenIsInRange && this._getOffsetDescriptor(fromToken); + + // First, remove any existing nodes in the range from the map. + this._indexMap.deleteRange(range[0] + 1, range[1]); + + // Insert a new node into the map for this range + this._indexMap.insert(range[0], descriptorToInsert); + + /* + * To avoid circular offset dependencies, keep the `fromToken` token mapped to whatever it was mapped to previously, + * even if it's in the current range. + */ + if (fromTokenIsInRange) { + this._indexMap.insert(fromToken.range[0], fromTokenDescriptor); + this._indexMap.insert(fromToken.range[1], descriptorToInsert); + } + + /* + * To avoid modifying the offset of tokens after the range, insert another node to keep the offset of the following + * tokens the same as it was before. + */ + this._indexMap.insert(range[1], descriptorAfterRange); + } + + /** + * Gets the desired indent of a token + * @param {Token} token The token + * @returns {string} The desired indent of the token + */ + getDesiredIndent(token) { + if (!this._desiredIndentCache.has(token)) { + + if (this._ignoredTokens.has(token)) { + + /* + * If the token is ignored, use the actual indent of the token as the desired indent. + * This ensures that no errors are reported for this token. + */ + this._desiredIndentCache.set( + token, + this._tokenInfo.getTokenIndent(token) + ); + } else if (this._lockedFirstTokens.has(token)) { + const firstToken = this._lockedFirstTokens.get(token); + + this._desiredIndentCache.set( + token, + + // (indentation for the first element's line) + this.getDesiredIndent(this._tokenInfo.getFirstTokenOfLine(firstToken)) + + + // (space between the start of the first element's line and the first element) + this._indentType.repeat(firstToken.loc.start.column - this._tokenInfo.getFirstTokenOfLine(firstToken).loc.start.column) + ); + } else { + const offsetInfo = this._getOffsetDescriptor(token); + const offset = ( + offsetInfo.from && + offsetInfo.from.loc.start.line === token.loc.start.line && + !/^\s*?\n/u.test(token.value) && + !offsetInfo.force + ) ? 0 : offsetInfo.offset * this._indentSize; + + this._desiredIndentCache.set( + token, + (offsetInfo.from ? this.getDesiredIndent(offsetInfo.from) : "") + this._indentType.repeat(offset) + ); + } + } + return this._desiredIndentCache.get(token); + } + + /** + * Ignores a token, preventing it from being reported. + * @param {Token} token The token + * @returns {void} + */ + ignoreToken(token) { + if (this._tokenInfo.isFirstTokenOfLine(token)) { + this._ignoredTokens.add(token); + } + } + + /** + * Gets the first token that the given token's indentation is dependent on + * @param {Token} token The token + * @returns {Token} The token that the given token depends on, or `null` if the given token is at the top level + */ + getFirstDependency(token) { + return this._getOffsetDescriptor(token).from; + } } const ELEMENT_LIST_SCHEMA = { - oneOf: [ - { - type: "integer", - minimum: 0, - }, - { - enum: ["first", "off"], - }, - ], + oneOf: [ + { + type: "integer", + minimum: 0 + }, + { + enum: ["first", "off"] + } + ] }; -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "indent", - url: "https://eslint.style/rules/indent", - }, - }, - ], - }, - type: "layout", - - docs: { - description: "Enforce consistent indentation", - recommended: false, - url: "https://eslint.org/docs/latest/rules/indent", - }, - - fixable: "whitespace", - - schema: [ - { - oneOf: [ - { - enum: ["tab"], - }, - { - type: "integer", - minimum: 0, - }, - ], - }, - { - type: "object", - properties: { - SwitchCase: { - type: "integer", - minimum: 0, - default: 0, - }, - VariableDeclarator: { - oneOf: [ - ELEMENT_LIST_SCHEMA, - { - type: "object", - properties: { - var: ELEMENT_LIST_SCHEMA, - let: ELEMENT_LIST_SCHEMA, - const: ELEMENT_LIST_SCHEMA, - }, - additionalProperties: false, - }, - ], - }, - outerIIFEBody: { - oneOf: [ - { - type: "integer", - minimum: 0, - }, - { - enum: ["off"], - }, - ], - }, - MemberExpression: { - oneOf: [ - { - type: "integer", - minimum: 0, - }, - { - enum: ["off"], - }, - ], - }, - FunctionDeclaration: { - type: "object", - properties: { - parameters: ELEMENT_LIST_SCHEMA, - body: { - type: "integer", - minimum: 0, - }, - }, - additionalProperties: false, - }, - FunctionExpression: { - type: "object", - properties: { - parameters: ELEMENT_LIST_SCHEMA, - body: { - type: "integer", - minimum: 0, - }, - }, - additionalProperties: false, - }, - StaticBlock: { - type: "object", - properties: { - body: { - type: "integer", - minimum: 0, - }, - }, - additionalProperties: false, - }, - CallExpression: { - type: "object", - properties: { - arguments: ELEMENT_LIST_SCHEMA, - }, - additionalProperties: false, - }, - ArrayExpression: ELEMENT_LIST_SCHEMA, - ObjectExpression: ELEMENT_LIST_SCHEMA, - ImportDeclaration: ELEMENT_LIST_SCHEMA, - flatTernaryExpressions: { - type: "boolean", - default: false, - }, - offsetTernaryExpressions: { - type: "boolean", - default: false, - }, - ignoredNodes: { - type: "array", - items: { - type: "string", - not: { - pattern: ":exit$", - }, - }, - }, - ignoreComments: { - type: "boolean", - default: false, - }, - }, - additionalProperties: false, - }, - ], - messages: { - wrongIndentation: - "Expected indentation of {{expected}} but found {{actual}}.", - }, - }, - - create(context) { - const DEFAULT_VARIABLE_INDENT = 1; - const DEFAULT_PARAMETER_INDENT = 1; - const DEFAULT_FUNCTION_BODY_INDENT = 1; - - let indentType = "space"; - let indentSize = 4; - const options = { - SwitchCase: 0, - VariableDeclarator: { - var: DEFAULT_VARIABLE_INDENT, - let: DEFAULT_VARIABLE_INDENT, - const: DEFAULT_VARIABLE_INDENT, - }, - outerIIFEBody: 1, - FunctionDeclaration: { - parameters: DEFAULT_PARAMETER_INDENT, - body: DEFAULT_FUNCTION_BODY_INDENT, - }, - FunctionExpression: { - parameters: DEFAULT_PARAMETER_INDENT, - body: DEFAULT_FUNCTION_BODY_INDENT, - }, - StaticBlock: { - body: DEFAULT_FUNCTION_BODY_INDENT, - }, - CallExpression: { - arguments: DEFAULT_PARAMETER_INDENT, - }, - MemberExpression: 1, - ArrayExpression: 1, - ObjectExpression: 1, - ImportDeclaration: 1, - flatTernaryExpressions: false, - ignoredNodes: [], - ignoreComments: false, - }; - - if (context.options.length) { - if (context.options[0] === "tab") { - indentSize = 1; - indentType = "tab"; - } else { - indentSize = context.options[0]; - indentType = "space"; - } - - if (context.options[1]) { - Object.assign(options, context.options[1]); - - if ( - typeof options.VariableDeclarator === "number" || - options.VariableDeclarator === "first" - ) { - options.VariableDeclarator = { - var: options.VariableDeclarator, - let: options.VariableDeclarator, - const: options.VariableDeclarator, - }; - } - } - } - - const sourceCode = context.sourceCode; - const tokenInfo = new TokenInfo(sourceCode); - const offsets = new OffsetStorage( - tokenInfo, - indentSize, - indentType === "space" ? " " : "\t", - sourceCode.text.length, - ); - const parameterParens = new WeakSet(); - - /** - * Creates an error message for a line, given the expected/actual indentation. - * @param {number} expectedAmount The expected amount of indentation characters for this line - * @param {number} actualSpaces The actual number of indentation spaces that were found on this line - * @param {number} actualTabs The actual number of indentation tabs that were found on this line - * @returns {string} An error message for this line - */ - function createErrorMessageData( - expectedAmount, - actualSpaces, - actualTabs, - ) { - const expectedStatement = `${expectedAmount} ${indentType}${expectedAmount === 1 ? "" : "s"}`; // e.g. "2 tabs" - const foundSpacesWord = `space${actualSpaces === 1 ? "" : "s"}`; // e.g. "space" - const foundTabsWord = `tab${actualTabs === 1 ? "" : "s"}`; // e.g. "tabs" - let foundStatement; - - if (actualSpaces > 0) { - /* - * Abbreviate the message if the expected indentation is also spaces. - * e.g. 'Expected 4 spaces but found 2' rather than 'Expected 4 spaces but found 2 spaces' - */ - foundStatement = - indentType === "space" - ? actualSpaces - : `${actualSpaces} ${foundSpacesWord}`; - } else if (actualTabs > 0) { - foundStatement = - indentType === "tab" - ? actualTabs - : `${actualTabs} ${foundTabsWord}`; - } else { - foundStatement = "0"; - } - return { - expected: expectedStatement, - actual: foundStatement, - }; - } - - /** - * Reports a given indent violation - * @param {Token} token Token violating the indent rule - * @param {string} neededIndent Expected indentation string - * @returns {void} - */ - function report(token, neededIndent) { - const actualIndent = Array.from(tokenInfo.getTokenIndent(token)); - const numSpaces = actualIndent.filter(char => char === " ").length; - const numTabs = actualIndent.filter(char => char === "\t").length; - - context.report({ - node: token, - messageId: "wrongIndentation", - data: createErrorMessageData( - neededIndent.length, - numSpaces, - numTabs, - ), - loc: { - start: { line: token.loc.start.line, column: 0 }, - end: { - line: token.loc.start.line, - column: token.loc.start.column, - }, - }, - fix(fixer) { - const range = [ - token.range[0] - token.loc.start.column, - token.range[0], - ]; - const newText = neededIndent; - - return fixer.replaceTextRange(range, newText); - }, - }); - } - - /** - * Checks if a token's indentation is correct - * @param {Token} token Token to examine - * @param {string} desiredIndent Desired indentation of the string - * @returns {boolean} `true` if the token's indentation is correct - */ - function validateTokenIndent(token, desiredIndent) { - const indentation = tokenInfo.getTokenIndent(token); - - return ( - indentation === desiredIndent || - // To avoid conflicts with no-mixed-spaces-and-tabs, don't report mixed spaces and tabs. - (indentation.includes(" ") && indentation.includes("\t")) - ); - } - - /** - * Check to see if the node is a file level IIFE - * @param {ASTNode} node The function node to check. - * @returns {boolean} True if the node is the outer IIFE - */ - function isOuterIIFE(node) { - /* - * Verify that the node is an IIFE - */ - if ( - !node.parent || - node.parent.type !== "CallExpression" || - node.parent.callee !== node - ) { - return false; - } - - /* - * Navigate legal ancestors to determine whether this IIFE is outer. - * A "legal ancestor" is an expression or statement that causes the function to get executed immediately. - * For example, `!(function(){})()` is an outer IIFE even though it is preceded by a ! operator. - */ - let statement = node.parent && node.parent.parent; - - while ( - (statement.type === "UnaryExpression" && - ["!", "~", "+", "-"].includes(statement.operator)) || - statement.type === "AssignmentExpression" || - statement.type === "LogicalExpression" || - statement.type === "SequenceExpression" || - statement.type === "VariableDeclarator" - ) { - statement = statement.parent; - } - - return ( - (statement.type === "ExpressionStatement" || - statement.type === "VariableDeclaration") && - statement.parent.type === "Program" - ); - } - - /** - * Counts the number of linebreaks that follow the last non-whitespace character in a string - * @param {string} string The string to check - * @returns {number} The number of JavaScript linebreaks that follow the last non-whitespace character, - * or the total number of linebreaks if the string is all whitespace. - */ - function countTrailingLinebreaks(string) { - const trailingWhitespace = string.match(/\s*$/u)[0]; - const linebreakMatches = trailingWhitespace.match( - astUtils.createGlobalLinebreakMatcher(), - ); - - return linebreakMatches === null ? 0 : linebreakMatches.length; - } - - /** - * Check indentation for lists of elements (arrays, objects, function params) - * @param {ASTNode[]} elements List of elements that should be offset - * @param {Token} startToken The start token of the list that element should be aligned against, e.g. '[' - * @param {Token} endToken The end token of the list, e.g. ']' - * @param {number|string} offset The amount that the elements should be offset - * @returns {void} - */ - function addElementListIndent(elements, startToken, endToken, offset) { - /** - * Gets the first token of a given element, including surrounding parentheses. - * @param {ASTNode} element A node in the `elements` list - * @returns {Token} The first token of this element - */ - function getFirstToken(element) { - let token = sourceCode.getTokenBefore(element); - - while ( - astUtils.isOpeningParenToken(token) && - token !== startToken - ) { - token = sourceCode.getTokenBefore(token); - } - return sourceCode.getTokenAfter(token); - } - - // Run through all the tokens in the list, and offset them by one indent level (mainly for comments, other things will end up overridden) - offsets.setDesiredOffsets( - [startToken.range[1], endToken.range[0]], - startToken, - typeof offset === "number" ? offset : 1, - ); - offsets.setDesiredOffset(endToken, startToken, 0); - - // If the preference is "first" but there is no first element (e.g. sparse arrays w/ empty first slot), fall back to 1 level. - if (offset === "first" && elements.length && !elements[0]) { - return; - } - elements.forEach((element, index) => { - if (!element) { - // Skip holes in arrays - return; - } - if (offset === "off") { - // Ignore the first token of every element if the "off" option is used - offsets.ignoreToken(getFirstToken(element)); - } - - // Offset the following elements correctly relative to the first element - if (index === 0) { - return; - } - if ( - offset === "first" && - tokenInfo.isFirstTokenOfLine(getFirstToken(element)) - ) { - offsets.matchOffsetOf( - getFirstToken(elements[0]), - getFirstToken(element), - ); - } else { - const previousElement = elements[index - 1]; - const firstTokenOfPreviousElement = - previousElement && getFirstToken(previousElement); - const previousElementLastToken = - previousElement && - sourceCode.getLastToken(previousElement); - - if ( - previousElement && - previousElementLastToken.loc.end.line - - countTrailingLinebreaks( - previousElementLastToken.value, - ) > - startToken.loc.end.line - ) { - offsets.setDesiredOffsets( - [previousElement.range[1], element.range[1]], - firstTokenOfPreviousElement, - 0, - ); - } - } - }); - } - - /** - * Check and decide whether to check for indentation for blockless nodes - * Scenarios are for or while statements without braces around them - * @param {ASTNode} node node to examine - * @returns {void} - */ - function addBlocklessNodeIndent(node) { - if (node.type !== "BlockStatement") { - const lastParentToken = sourceCode.getTokenBefore( - node, - astUtils.isNotOpeningParenToken, - ); - - let firstBodyToken = sourceCode.getFirstToken(node); - let lastBodyToken = sourceCode.getLastToken(node); - - while ( - astUtils.isOpeningParenToken( - sourceCode.getTokenBefore(firstBodyToken), - ) && - astUtils.isClosingParenToken( - sourceCode.getTokenAfter(lastBodyToken), - ) - ) { - firstBodyToken = sourceCode.getTokenBefore(firstBodyToken); - lastBodyToken = sourceCode.getTokenAfter(lastBodyToken); - } - - offsets.setDesiredOffsets( - [firstBodyToken.range[0], lastBodyToken.range[1]], - lastParentToken, - 1, - ); - } - } - - /** - * Checks the indentation for nodes that are like function calls (`CallExpression` and `NewExpression`) - * @param {ASTNode} node A CallExpression or NewExpression node - * @returns {void} - */ - function addFunctionCallIndent(node) { - let openingParen; - - if (node.arguments.length) { - openingParen = sourceCode.getFirstTokenBetween( - node.callee, - node.arguments[0], - astUtils.isOpeningParenToken, - ); - } else { - openingParen = sourceCode.getLastToken(node, 1); - } - const closingParen = sourceCode.getLastToken(node); - - parameterParens.add(openingParen); - parameterParens.add(closingParen); - - /* - * If `?.` token exists, set desired offset for that. - * This logic is copied from `MemberExpression`'s. - */ - if (node.optional) { - const dotToken = sourceCode.getTokenAfter( - node.callee, - astUtils.isQuestionDotToken, - ); - const calleeParenCount = sourceCode.getTokensBetween( - node.callee, - dotToken, - { filter: astUtils.isClosingParenToken }, - ).length; - const firstTokenOfCallee = calleeParenCount - ? sourceCode.getTokenBefore(node.callee, { - skip: calleeParenCount - 1, - }) - : sourceCode.getFirstToken(node.callee); - const lastTokenOfCallee = sourceCode.getTokenBefore(dotToken); - const offsetBase = - lastTokenOfCallee.loc.end.line === - openingParen.loc.start.line - ? lastTokenOfCallee - : firstTokenOfCallee; - - offsets.setDesiredOffset(dotToken, offsetBase, 1); - } - - const offsetAfterToken = - node.callee.type === "TaggedTemplateExpression" - ? sourceCode.getFirstToken(node.callee.quasi) - : openingParen; - const offsetToken = sourceCode.getTokenBefore(offsetAfterToken); - - offsets.setDesiredOffset(openingParen, offsetToken, 0); - - addElementListIndent( - node.arguments, - openingParen, - closingParen, - options.CallExpression.arguments, - ); - } - - /** - * Checks the indentation of parenthesized values, given a list of tokens in a program - * @param {Token[]} tokens A list of tokens - * @returns {void} - */ - function addParensIndent(tokens) { - const parenStack = []; - const parenPairs = []; - - for (let i = 0; i < tokens.length; i++) { - const nextToken = tokens[i]; - - if (astUtils.isOpeningParenToken(nextToken)) { - parenStack.push(nextToken); - } else if (astUtils.isClosingParenToken(nextToken)) { - parenPairs.push({ - left: parenStack.pop(), - right: nextToken, - }); - } - } - - for (let i = parenPairs.length - 1; i >= 0; i--) { - const leftParen = parenPairs[i].left; - const rightParen = parenPairs[i].right; - - // We only want to handle parens around expressions, so exclude parentheses that are in function parameters and function call arguments. - if ( - !parameterParens.has(leftParen) && - !parameterParens.has(rightParen) - ) { - const parenthesizedTokens = new Set( - sourceCode.getTokensBetween(leftParen, rightParen), - ); - - parenthesizedTokens.forEach(token => { - if ( - !parenthesizedTokens.has( - offsets.getFirstDependency(token), - ) - ) { - offsets.setDesiredOffset(token, leftParen, 1); - } - }); - } - - offsets.setDesiredOffset(rightParen, leftParen, 0); - } - } - - /** - * Ignore all tokens within an unknown node whose offset do not depend - * on another token's offset within the unknown node - * @param {ASTNode} node Unknown Node - * @returns {void} - */ - function ignoreNode(node) { - const unknownNodeTokens = new Set( - sourceCode.getTokens(node, { includeComments: true }), - ); - - unknownNodeTokens.forEach(token => { - if (!unknownNodeTokens.has(offsets.getFirstDependency(token))) { - const firstTokenOfLine = - tokenInfo.getFirstTokenOfLine(token); - - if (token === firstTokenOfLine) { - offsets.ignoreToken(token); - } else { - offsets.setDesiredOffset(token, firstTokenOfLine, 0); - } - } - }); - } - - /** - * Check whether the given token is on the first line of a statement. - * @param {Token} token The token to check. - * @param {ASTNode} leafNode The expression node that the token belongs directly. - * @returns {boolean} `true` if the token is on the first line of a statement. - */ - function isOnFirstLineOfStatement(token, leafNode) { - let node = leafNode; - - while ( - node.parent && - !node.parent.type.endsWith("Statement") && - !node.parent.type.endsWith("Declaration") - ) { - node = node.parent; - } - node = node.parent; - - return !node || node.loc.start.line === token.loc.start.line; - } - - /** - * Check whether there are any blank (whitespace-only) lines between - * two tokens on separate lines. - * @param {Token} firstToken The first token. - * @param {Token} secondToken The second token. - * @returns {boolean} `true` if the tokens are on separate lines and - * there exists a blank line between them, `false` otherwise. - */ - function hasBlankLinesBetween(firstToken, secondToken) { - const firstTokenLine = firstToken.loc.end.line; - const secondTokenLine = secondToken.loc.start.line; - - if ( - firstTokenLine === secondTokenLine || - firstTokenLine === secondTokenLine - 1 - ) { - return false; - } - - for ( - let line = firstTokenLine + 1; - line < secondTokenLine; - ++line - ) { - if (!tokenInfo.firstTokensByLineNumber.has(line)) { - return true; - } - } - - return false; - } - - const ignoredNodeFirstTokens = new Set(); - - const baseOffsetListeners = { - "ArrayExpression, ArrayPattern"(node) { - const openingBracket = sourceCode.getFirstToken(node); - const closingBracket = sourceCode.getTokenAfter( - [...node.elements].reverse().find(_ => _) || openingBracket, - astUtils.isClosingBracketToken, - ); - - addElementListIndent( - node.elements, - openingBracket, - closingBracket, - options.ArrayExpression, - ); - }, - - "ObjectExpression, ObjectPattern"(node) { - const openingCurly = sourceCode.getFirstToken(node); - const closingCurly = sourceCode.getTokenAfter( - node.properties.length - ? node.properties.at(-1) - : openingCurly, - astUtils.isClosingBraceToken, - ); - - addElementListIndent( - node.properties, - openingCurly, - closingCurly, - options.ObjectExpression, - ); - }, - - ArrowFunctionExpression(node) { - const maybeOpeningParen = sourceCode.getFirstToken(node, { - skip: node.async ? 1 : 0, - }); - - if (astUtils.isOpeningParenToken(maybeOpeningParen)) { - const openingParen = maybeOpeningParen; - const closingParen = sourceCode.getTokenBefore( - node.body, - astUtils.isClosingParenToken, - ); - - parameterParens.add(openingParen); - parameterParens.add(closingParen); - addElementListIndent( - node.params, - openingParen, - closingParen, - options.FunctionExpression.parameters, - ); - } - - addBlocklessNodeIndent(node.body); - }, - - AssignmentExpression(node) { - const operator = sourceCode.getFirstTokenBetween( - node.left, - node.right, - token => token.value === node.operator, - ); - - offsets.setDesiredOffsets( - [operator.range[0], node.range[1]], - sourceCode.getLastToken(node.left), - 1, - ); - offsets.ignoreToken(operator); - offsets.ignoreToken(sourceCode.getTokenAfter(operator)); - }, - - "BinaryExpression, LogicalExpression"(node) { - const operator = sourceCode.getFirstTokenBetween( - node.left, - node.right, - token => token.value === node.operator, - ); - - /* - * For backwards compatibility, don't check BinaryExpression indents, e.g. - * var foo = bar && - * baz; - */ - - const tokenAfterOperator = sourceCode.getTokenAfter(operator); - - offsets.ignoreToken(operator); - offsets.ignoreToken(tokenAfterOperator); - offsets.setDesiredOffset(tokenAfterOperator, operator, 0); - }, - - "BlockStatement, ClassBody"(node) { - let blockIndentLevel; - - if (node.parent && isOuterIIFE(node.parent)) { - blockIndentLevel = options.outerIIFEBody; - } else if ( - node.parent && - (node.parent.type === "FunctionExpression" || - node.parent.type === "ArrowFunctionExpression") - ) { - blockIndentLevel = options.FunctionExpression.body; - } else if ( - node.parent && - node.parent.type === "FunctionDeclaration" - ) { - blockIndentLevel = options.FunctionDeclaration.body; - } else { - blockIndentLevel = 1; - } - - /* - * For blocks that aren't lone statements, ensure that the opening curly brace - * is aligned with the parent. - */ - if (!astUtils.STATEMENT_LIST_PARENTS.has(node.parent.type)) { - offsets.setDesiredOffset( - sourceCode.getFirstToken(node), - sourceCode.getFirstToken(node.parent), - 0, - ); - } - - addElementListIndent( - node.body, - sourceCode.getFirstToken(node), - sourceCode.getLastToken(node), - blockIndentLevel, - ); - }, - - CallExpression: addFunctionCallIndent, - - "ClassDeclaration[superClass], ClassExpression[superClass]"(node) { - const classToken = sourceCode.getFirstToken(node); - const extendsToken = sourceCode.getTokenBefore( - node.superClass, - astUtils.isNotOpeningParenToken, - ); - - offsets.setDesiredOffsets( - [extendsToken.range[0], node.body.range[0]], - classToken, - 1, - ); - }, - - ConditionalExpression(node) { - const firstToken = sourceCode.getFirstToken(node); - - // `flatTernaryExpressions` option is for the following style: - // var a = - // foo > 0 ? bar : - // foo < 0 ? baz : - // /*else*/ qiz ; - if ( - !options.flatTernaryExpressions || - !astUtils.isTokenOnSameLine(node.test, node.consequent) || - isOnFirstLineOfStatement(firstToken, node) - ) { - const questionMarkToken = sourceCode.getFirstTokenBetween( - node.test, - node.consequent, - token => - token.type === "Punctuator" && token.value === "?", - ); - const colonToken = sourceCode.getFirstTokenBetween( - node.consequent, - node.alternate, - token => - token.type === "Punctuator" && token.value === ":", - ); - - const firstConsequentToken = - sourceCode.getTokenAfter(questionMarkToken); - const lastConsequentToken = - sourceCode.getTokenBefore(colonToken); - const firstAlternateToken = - sourceCode.getTokenAfter(colonToken); - - offsets.setDesiredOffset(questionMarkToken, firstToken, 1); - offsets.setDesiredOffset(colonToken, firstToken, 1); - - offsets.setDesiredOffset( - firstConsequentToken, - firstToken, - firstConsequentToken.type === "Punctuator" && - options.offsetTernaryExpressions - ? 2 - : 1, - ); - - /* - * The alternate and the consequent should usually have the same indentation. - * If they share part of a line, align the alternate against the first token of the consequent. - * This allows the alternate to be indented correctly in cases like this: - * foo ? ( - * bar - * ) : ( // this '(' is aligned with the '(' above, so it's considered to be aligned with `foo` - * baz // as a result, `baz` is offset by 1 rather than 2 - * ) - */ - if ( - lastConsequentToken.loc.end.line === - firstAlternateToken.loc.start.line - ) { - offsets.setDesiredOffset( - firstAlternateToken, - firstConsequentToken, - 0, - ); - } else { - /** - * If the alternate and consequent do not share part of a line, offset the alternate from the first - * token of the conditional expression. For example: - * foo ? bar - * : baz - * - * If `baz` were aligned with `bar` rather than being offset by 1 from `foo`, `baz` would end up - * having no expected indentation. - */ - offsets.setDesiredOffset( - firstAlternateToken, - firstToken, - firstAlternateToken.type === "Punctuator" && - options.offsetTernaryExpressions - ? 2 - : 1, - ); - } - } - }, - - "DoWhileStatement, WhileStatement, ForInStatement, ForOfStatement, WithStatement": - node => addBlocklessNodeIndent(node.body), - - ExportNamedDeclaration(node) { - if (node.declaration === null) { - const closingCurly = sourceCode.getLastToken( - node, - astUtils.isClosingBraceToken, - ); - - // Indent the specifiers in `export {foo, bar, baz}` - addElementListIndent( - node.specifiers, - sourceCode.getFirstToken(node, { skip: 1 }), - closingCurly, - 1, - ); - - if (node.source) { - // Indent everything after and including the `from` token in `export {foo, bar, baz} from 'qux'` - offsets.setDesiredOffsets( - [closingCurly.range[1], node.range[1]], - sourceCode.getFirstToken(node), - 1, - ); - } - } - }, - - ForStatement(node) { - const forOpeningParen = sourceCode.getFirstToken(node, 1); - - if (node.init) { - offsets.setDesiredOffsets( - node.init.range, - forOpeningParen, - 1, - ); - } - if (node.test) { - offsets.setDesiredOffsets( - node.test.range, - forOpeningParen, - 1, - ); - } - if (node.update) { - offsets.setDesiredOffsets( - node.update.range, - forOpeningParen, - 1, - ); - } - addBlocklessNodeIndent(node.body); - }, - - "FunctionDeclaration, FunctionExpression"(node) { - const closingParen = sourceCode.getTokenBefore(node.body); - const openingParen = sourceCode.getTokenBefore( - node.params.length ? node.params[0] : closingParen, - ); - - parameterParens.add(openingParen); - parameterParens.add(closingParen); - addElementListIndent( - node.params, - openingParen, - closingParen, - options[node.type].parameters, - ); - }, - - IfStatement(node) { - addBlocklessNodeIndent(node.consequent); - if (node.alternate) { - addBlocklessNodeIndent(node.alternate); - } - }, - - /* - * For blockless nodes with semicolon-first style, don't indent the semicolon. - * e.g. - * if (foo) - * bar() - * ; [1, 2, 3].map(foo) - * - * Traversal into the node sets indentation of the semicolon, so we need to override it on exit. - */ - ":matches(DoWhileStatement, ForStatement, ForInStatement, ForOfStatement, IfStatement, WhileStatement, WithStatement):exit"( - node, - ) { - let nodesToCheck; - - if (node.type === "IfStatement") { - nodesToCheck = [node.consequent]; - if (node.alternate) { - nodesToCheck.push(node.alternate); - } - } else { - nodesToCheck = [node.body]; - } - - for (const nodeToCheck of nodesToCheck) { - const lastToken = sourceCode.getLastToken(nodeToCheck); - - if (astUtils.isSemicolonToken(lastToken)) { - const tokenBeforeLast = - sourceCode.getTokenBefore(lastToken); - const tokenAfterLast = - sourceCode.getTokenAfter(lastToken); - - // override indentation of `;` only if its line looks like a semicolon-first style line - if ( - !astUtils.isTokenOnSameLine( - tokenBeforeLast, - lastToken, - ) && - tokenAfterLast && - astUtils.isTokenOnSameLine( - lastToken, - tokenAfterLast, - ) - ) { - offsets.setDesiredOffset( - lastToken, - sourceCode.getFirstToken(node), - 0, - ); - } - } - } - }, - - ImportDeclaration(node) { - if ( - node.specifiers.some( - specifier => specifier.type === "ImportSpecifier", - ) - ) { - const openingCurly = sourceCode.getFirstToken( - node, - astUtils.isOpeningBraceToken, - ); - const closingCurly = sourceCode.getLastToken( - node, - astUtils.isClosingBraceToken, - ); - - addElementListIndent( - node.specifiers.filter( - specifier => specifier.type === "ImportSpecifier", - ), - openingCurly, - closingCurly, - options.ImportDeclaration, - ); - } - - const fromToken = sourceCode.getLastToken( - node, - token => - token.type === "Identifier" && token.value === "from", - ); - const sourceToken = sourceCode.getLastToken( - node, - token => token.type === "String", - ); - const semiToken = sourceCode.getLastToken( - node, - token => token.type === "Punctuator" && token.value === ";", - ); - - if (fromToken) { - const end = - semiToken && semiToken.range[1] === sourceToken.range[1] - ? node.range[1] - : sourceToken.range[1]; - - offsets.setDesiredOffsets( - [fromToken.range[0], end], - sourceCode.getFirstToken(node), - 1, - ); - } - }, - - ImportExpression(node) { - const openingParen = sourceCode.getFirstToken(node, 1); - const closingParen = sourceCode.getLastToken(node); - - parameterParens.add(openingParen); - parameterParens.add(closingParen); - offsets.setDesiredOffset( - openingParen, - sourceCode.getTokenBefore(openingParen), - 0, - ); - - addElementListIndent( - [node.source], - openingParen, - closingParen, - options.CallExpression.arguments, - ); - }, - - "MemberExpression, JSXMemberExpression, MetaProperty"(node) { - const object = - node.type === "MetaProperty" ? node.meta : node.object; - const firstNonObjectToken = sourceCode.getFirstTokenBetween( - object, - node.property, - astUtils.isNotClosingParenToken, - ); - const secondNonObjectToken = - sourceCode.getTokenAfter(firstNonObjectToken); - - const objectParenCount = sourceCode.getTokensBetween( - object, - node.property, - { filter: astUtils.isClosingParenToken }, - ).length; - const firstObjectToken = objectParenCount - ? sourceCode.getTokenBefore(object, { - skip: objectParenCount - 1, - }) - : sourceCode.getFirstToken(object); - const lastObjectToken = - sourceCode.getTokenBefore(firstNonObjectToken); - const firstPropertyToken = node.computed - ? firstNonObjectToken - : secondNonObjectToken; - - if (node.computed) { - // For computed MemberExpressions, match the closing bracket with the opening bracket. - offsets.setDesiredOffset( - sourceCode.getLastToken(node), - firstNonObjectToken, - 0, - ); - offsets.setDesiredOffsets( - node.property.range, - firstNonObjectToken, - 1, - ); - } - - /* - * If the object ends on the same line that the property starts, match against the last token - * of the object, to ensure that the MemberExpression is not indented. - * - * Otherwise, match against the first token of the object, e.g. - * foo - * .bar - * .baz // <-- offset by 1 from `foo` - */ - const offsetBase = - lastObjectToken.loc.end.line === - firstPropertyToken.loc.start.line - ? lastObjectToken - : firstObjectToken; - - if (typeof options.MemberExpression === "number") { - // Match the dot (for non-computed properties) or the opening bracket (for computed properties) against the object. - offsets.setDesiredOffset( - firstNonObjectToken, - offsetBase, - options.MemberExpression, - ); - - /* - * For computed MemberExpressions, match the first token of the property against the opening bracket. - * Otherwise, match the first token of the property against the object. - */ - offsets.setDesiredOffset( - secondNonObjectToken, - node.computed ? firstNonObjectToken : offsetBase, - options.MemberExpression, - ); - } else { - // If the MemberExpression option is off, ignore the dot and the first token of the property. - offsets.ignoreToken(firstNonObjectToken); - offsets.ignoreToken(secondNonObjectToken); - - // To ignore the property indentation, ensure that the property tokens depend on the ignored tokens. - offsets.setDesiredOffset( - firstNonObjectToken, - offsetBase, - 0, - ); - offsets.setDesiredOffset( - secondNonObjectToken, - firstNonObjectToken, - 0, - ); - } - }, - - NewExpression(node) { - // Only indent the arguments if the NewExpression has parens (e.g. `new Foo(bar)` or `new Foo()`, but not `new Foo` - if ( - node.arguments.length > 0 || - (astUtils.isClosingParenToken( - sourceCode.getLastToken(node), - ) && - astUtils.isOpeningParenToken( - sourceCode.getLastToken(node, 1), - )) - ) { - addFunctionCallIndent(node); - } - }, - - Property(node) { - if (!node.shorthand && !node.method && node.kind === "init") { - const colon = sourceCode.getFirstTokenBetween( - node.key, - node.value, - astUtils.isColonToken, - ); - - offsets.ignoreToken(sourceCode.getTokenAfter(colon)); - } - }, - - PropertyDefinition(node) { - const firstToken = sourceCode.getFirstToken(node); - const maybeSemicolonToken = sourceCode.getLastToken(node); - let keyLastToken; - - // Indent key. - if (node.computed) { - const bracketTokenL = sourceCode.getTokenBefore( - node.key, - astUtils.isOpeningBracketToken, - ); - const bracketTokenR = (keyLastToken = - sourceCode.getTokenAfter( - node.key, - astUtils.isClosingBracketToken, - )); - const keyRange = [ - bracketTokenL.range[1], - bracketTokenR.range[0], - ]; - - if (bracketTokenL !== firstToken) { - offsets.setDesiredOffset(bracketTokenL, firstToken, 0); - } - offsets.setDesiredOffsets(keyRange, bracketTokenL, 1); - offsets.setDesiredOffset(bracketTokenR, bracketTokenL, 0); - } else { - const idToken = (keyLastToken = sourceCode.getFirstToken( - node.key, - )); - - if (idToken !== firstToken) { - offsets.setDesiredOffset(idToken, firstToken, 1); - } - } - - // Indent initializer. - if (node.value) { - const eqToken = sourceCode.getTokenBefore( - node.value, - astUtils.isEqToken, - ); - const valueToken = sourceCode.getTokenAfter(eqToken); - - offsets.setDesiredOffset(eqToken, keyLastToken, 1); - offsets.setDesiredOffset(valueToken, eqToken, 1); - if (astUtils.isSemicolonToken(maybeSemicolonToken)) { - offsets.setDesiredOffset( - maybeSemicolonToken, - eqToken, - 1, - ); - } - } else if (astUtils.isSemicolonToken(maybeSemicolonToken)) { - offsets.setDesiredOffset( - maybeSemicolonToken, - keyLastToken, - 1, - ); - } - }, - - StaticBlock(node) { - const openingCurly = sourceCode.getFirstToken(node, { - skip: 1, - }); // skip the `static` token - const closingCurly = sourceCode.getLastToken(node); - - addElementListIndent( - node.body, - openingCurly, - closingCurly, - options.StaticBlock.body, - ); - }, - - SwitchStatement(node) { - const openingCurly = sourceCode.getTokenAfter( - node.discriminant, - astUtils.isOpeningBraceToken, - ); - const closingCurly = sourceCode.getLastToken(node); - - offsets.setDesiredOffsets( - [openingCurly.range[1], closingCurly.range[0]], - openingCurly, - options.SwitchCase, - ); - - if (node.cases.length) { - sourceCode - .getTokensBetween(node.cases.at(-1), closingCurly, { - includeComments: true, - filter: astUtils.isCommentToken, - }) - .forEach(token => offsets.ignoreToken(token)); - } - }, - - SwitchCase(node) { - if ( - !( - node.consequent.length === 1 && - node.consequent[0].type === "BlockStatement" - ) - ) { - const caseKeyword = sourceCode.getFirstToken(node); - const tokenAfterCurrentCase = - sourceCode.getTokenAfter(node); - - offsets.setDesiredOffsets( - [caseKeyword.range[1], tokenAfterCurrentCase.range[0]], - caseKeyword, - 1, - ); - } - }, - - TemplateLiteral(node) { - node.expressions.forEach((expression, index) => { - const previousQuasi = node.quasis[index]; - const nextQuasi = node.quasis[index + 1]; - const tokenToAlignFrom = - previousQuasi.loc.start.line === - previousQuasi.loc.end.line - ? sourceCode.getFirstToken(previousQuasi) - : null; - - offsets.setDesiredOffsets( - [previousQuasi.range[1], nextQuasi.range[0]], - tokenToAlignFrom, - 1, - ); - offsets.setDesiredOffset( - sourceCode.getFirstToken(nextQuasi), - tokenToAlignFrom, - 0, - ); - }); - }, - - VariableDeclaration(node) { - let variableIndent = Object.hasOwn( - options.VariableDeclarator, - node.kind, - ) - ? options.VariableDeclarator[node.kind] - : DEFAULT_VARIABLE_INDENT; - - const firstToken = sourceCode.getFirstToken(node), - lastToken = sourceCode.getLastToken(node); - - if (options.VariableDeclarator[node.kind] === "first") { - if (node.declarations.length > 1) { - addElementListIndent( - node.declarations, - firstToken, - lastToken, - "first", - ); - return; - } - - variableIndent = DEFAULT_VARIABLE_INDENT; - } - - if ( - node.declarations.at(-1).loc.start.line > - node.loc.start.line - ) { - /* - * VariableDeclarator indentation is a bit different from other forms of indentation, in that the - * indentation of an opening bracket sometimes won't match that of a closing bracket. For example, - * the following indentations are correct: - * - * var foo = { - * ok: true - * }; - * - * var foo = { - * ok: true, - * }, - * bar = 1; - * - * Account for when exiting the AST (after indentations have already been set for the nodes in - * the declaration) by manually increasing the indentation level of the tokens in this declarator - * on the same line as the start of the declaration, provided that there are declarators that - * follow this one. - */ - offsets.setDesiredOffsets( - node.range, - firstToken, - variableIndent, - true, - ); - } else { - offsets.setDesiredOffsets( - node.range, - firstToken, - variableIndent, - ); - } - - if (astUtils.isSemicolonToken(lastToken)) { - offsets.ignoreToken(lastToken); - } - }, - - VariableDeclarator(node) { - if (node.init) { - const equalOperator = sourceCode.getTokenBefore( - node.init, - astUtils.isNotOpeningParenToken, - ); - const tokenAfterOperator = - sourceCode.getTokenAfter(equalOperator); - - offsets.ignoreToken(equalOperator); - offsets.ignoreToken(tokenAfterOperator); - offsets.setDesiredOffsets( - [tokenAfterOperator.range[0], node.range[1]], - equalOperator, - 1, - ); - offsets.setDesiredOffset( - equalOperator, - sourceCode.getLastToken(node.id), - 0, - ); - } - }, - - "JSXAttribute[value]"(node) { - const equalsToken = sourceCode.getFirstTokenBetween( - node.name, - node.value, - token => token.type === "Punctuator" && token.value === "=", - ); - - offsets.setDesiredOffsets( - [equalsToken.range[0], node.value.range[1]], - sourceCode.getFirstToken(node.name), - 1, - ); - }, - - JSXElement(node) { - if (node.closingElement) { - addElementListIndent( - node.children, - sourceCode.getFirstToken(node.openingElement), - sourceCode.getFirstToken(node.closingElement), - 1, - ); - } - }, - - JSXOpeningElement(node) { - const firstToken = sourceCode.getFirstToken(node); - let closingToken; - - if (node.selfClosing) { - closingToken = sourceCode.getLastToken(node, { skip: 1 }); - offsets.setDesiredOffset( - sourceCode.getLastToken(node), - closingToken, - 0, - ); - } else { - closingToken = sourceCode.getLastToken(node); - } - offsets.setDesiredOffsets( - node.name.range, - sourceCode.getFirstToken(node), - ); - addElementListIndent( - node.attributes, - firstToken, - closingToken, - 1, - ); - }, - - JSXClosingElement(node) { - const firstToken = sourceCode.getFirstToken(node); - - offsets.setDesiredOffsets(node.name.range, firstToken, 1); - }, - - JSXFragment(node) { - const firstOpeningToken = sourceCode.getFirstToken( - node.openingFragment, - ); - const firstClosingToken = sourceCode.getFirstToken( - node.closingFragment, - ); - - addElementListIndent( - node.children, - firstOpeningToken, - firstClosingToken, - 1, - ); - }, - - JSXOpeningFragment(node) { - const firstToken = sourceCode.getFirstToken(node); - const closingToken = sourceCode.getLastToken(node); - - offsets.setDesiredOffsets(node.range, firstToken, 1); - offsets.matchOffsetOf(firstToken, closingToken); - }, - - JSXClosingFragment(node) { - const firstToken = sourceCode.getFirstToken(node); - const slashToken = sourceCode.getLastToken(node, { skip: 1 }); - const closingToken = sourceCode.getLastToken(node); - const tokenToMatch = astUtils.isTokenOnSameLine( - slashToken, - closingToken, - ) - ? slashToken - : closingToken; - - offsets.setDesiredOffsets(node.range, firstToken, 1); - offsets.matchOffsetOf(firstToken, tokenToMatch); - }, - - JSXExpressionContainer(node) { - const openingCurly = sourceCode.getFirstToken(node); - const closingCurly = sourceCode.getLastToken(node); - - offsets.setDesiredOffsets( - [openingCurly.range[1], closingCurly.range[0]], - openingCurly, - 1, - ); - }, - - JSXSpreadAttribute(node) { - const openingCurly = sourceCode.getFirstToken(node); - const closingCurly = sourceCode.getLastToken(node); - - offsets.setDesiredOffsets( - [openingCurly.range[1], closingCurly.range[0]], - openingCurly, - 1, - ); - }, - - "*"(node) { - const firstToken = sourceCode.getFirstToken(node); - - // Ensure that the children of every node are indented at least as much as the first token. - if (firstToken && !ignoredNodeFirstTokens.has(firstToken)) { - offsets.setDesiredOffsets(node.range, firstToken, 0); - } - }, - }; - - const listenerCallQueue = []; - - /* - * To ignore the indentation of a node: - * 1. Don't call the node's listener when entering it (if it has a listener) - * 2. Don't set any offsets against the first token of the node. - * 3. Call `ignoreNode` on the node sometime after exiting it and before validating offsets. - */ - const offsetListeners = {}; - - for (const [selector, listener] of Object.entries( - baseOffsetListeners, - )) { - /* - * Offset listener calls are deferred until traversal is finished, and are called as - * part of the final `Program:exit` listener. This is necessary because a node might - * be matched by multiple selectors. - * - * Example: Suppose there is an offset listener for `Identifier`, and the user has - * specified in configuration that `MemberExpression > Identifier` should be ignored. - * Due to selector specificity rules, the `Identifier` listener will get called first. However, - * if a given Identifier node is supposed to be ignored, then the `Identifier` offset listener - * should not have been called at all. Without doing extra selector matching, we don't know - * whether the Identifier matches the `MemberExpression > Identifier` selector until the - * `MemberExpression > Identifier` listener is called. - * - * To avoid this, the `Identifier` listener isn't called until traversal finishes and all - * ignored nodes are known. - */ - offsetListeners[selector] = node => - listenerCallQueue.push({ listener, node }); - } - - // For each ignored node selector, set up a listener to collect it into the `ignoredNodes` set. - const ignoredNodes = new Set(); - - /** - * Ignores a node - * @param {ASTNode} node The node to ignore - * @returns {void} - */ - function addToIgnoredNodes(node) { - ignoredNodes.add(node); - ignoredNodeFirstTokens.add(sourceCode.getFirstToken(node)); - } - - const ignoredNodeListeners = options.ignoredNodes.reduce( - (listeners, ignoredSelector) => - Object.assign(listeners, { - [ignoredSelector]: addToIgnoredNodes, - }), - {}, - ); - - /* - * Join the listeners, and add a listener to verify that all tokens actually have the correct indentation - * at the end. - * - * Using Object.assign will cause some offset listeners to be overwritten if the same selector also appears - * in `ignoredNodeListeners`. This isn't a problem because all of the matching nodes will be ignored, - * so those listeners wouldn't be called anyway. - */ - return Object.assign(offsetListeners, ignoredNodeListeners, { - "*:exit"(node) { - // If a node's type is nonstandard, we can't tell how its children should be offset, so ignore it. - if (!KNOWN_NODES.has(node.type)) { - addToIgnoredNodes(node); - } - }, - "Program:exit"() { - // If ignoreComments option is enabled, ignore all comment tokens. - if (options.ignoreComments) { - sourceCode - .getAllComments() - .forEach(comment => offsets.ignoreToken(comment)); - } - - // Invoke the queued offset listeners for the nodes that aren't ignored. - for (let i = 0; i < listenerCallQueue.length; i++) { - const nodeInfo = listenerCallQueue[i]; - - if (!ignoredNodes.has(nodeInfo.node)) { - nodeInfo.listener(nodeInfo.node); - } - } - - // Update the offsets for ignored nodes to prevent their child tokens from being reported. - ignoredNodes.forEach(ignoreNode); - - addParensIndent(sourceCode.ast.tokens); - - /* - * Create a Map from (tokenOrComment) => (precedingToken). - * This is necessary because sourceCode.getTokenBefore does not handle a comment as an argument correctly. - */ - const precedingTokens = new WeakMap(); - - for (let i = 0; i < sourceCode.ast.comments.length; i++) { - const comment = sourceCode.ast.comments[i]; - - const tokenOrCommentBefore = sourceCode.getTokenBefore( - comment, - { includeComments: true }, - ); - const hasToken = precedingTokens.has(tokenOrCommentBefore) - ? precedingTokens.get(tokenOrCommentBefore) - : tokenOrCommentBefore; - - precedingTokens.set(comment, hasToken); - } - - for (let i = 1; i < sourceCode.lines.length + 1; i++) { - if (!tokenInfo.firstTokensByLineNumber.has(i)) { - // Don't check indentation on blank lines - continue; - } - - const firstTokenOfLine = - tokenInfo.firstTokensByLineNumber.get(i); - - if (firstTokenOfLine.loc.start.line !== i) { - // Don't check the indentation of multi-line tokens (e.g. template literals or block comments) twice. - continue; - } - - if (astUtils.isCommentToken(firstTokenOfLine)) { - const tokenBefore = - precedingTokens.get(firstTokenOfLine); - const tokenAfter = tokenBefore - ? sourceCode.getTokenAfter(tokenBefore) - : sourceCode.ast.tokens[0]; - const mayAlignWithBefore = - tokenBefore && - !hasBlankLinesBetween( - tokenBefore, - firstTokenOfLine, - ); - const mayAlignWithAfter = - tokenAfter && - !hasBlankLinesBetween(firstTokenOfLine, tokenAfter); - - /* - * If a comment precedes a line that begins with a semicolon token, align to that token, i.e. - * - * let foo - * // comment - * ;(async () => {})() - */ - if ( - tokenAfter && - astUtils.isSemicolonToken(tokenAfter) && - !astUtils.isTokenOnSameLine( - firstTokenOfLine, - tokenAfter, - ) - ) { - offsets.setDesiredOffset( - firstTokenOfLine, - tokenAfter, - 0, - ); - } - - // If a comment matches the expected indentation of the token immediately before or after, don't report it. - if ( - (mayAlignWithBefore && - validateTokenIndent( - firstTokenOfLine, - offsets.getDesiredIndent(tokenBefore), - )) || - (mayAlignWithAfter && - validateTokenIndent( - firstTokenOfLine, - offsets.getDesiredIndent(tokenAfter), - )) - ) { - continue; - } - } - - // If the token matches the expected indentation, don't report it. - if ( - validateTokenIndent( - firstTokenOfLine, - offsets.getDesiredIndent(firstTokenOfLine), - ) - ) { - continue; - } - - // Otherwise, report the token/comment. - report( - firstTokenOfLine, - offsets.getDesiredIndent(firstTokenOfLine), - ); - } - }, - }); - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Enforce consistent indentation", + recommended: false, + url: "https://eslint.org/docs/latest/rules/indent" + }, + + fixable: "whitespace", + + schema: [ + { + oneOf: [ + { + enum: ["tab"] + }, + { + type: "integer", + minimum: 0 + } + ] + }, + { + type: "object", + properties: { + SwitchCase: { + type: "integer", + minimum: 0, + default: 0 + }, + VariableDeclarator: { + oneOf: [ + ELEMENT_LIST_SCHEMA, + { + type: "object", + properties: { + var: ELEMENT_LIST_SCHEMA, + let: ELEMENT_LIST_SCHEMA, + const: ELEMENT_LIST_SCHEMA + }, + additionalProperties: false + } + ] + }, + outerIIFEBody: { + oneOf: [ + { + type: "integer", + minimum: 0 + }, + { + enum: ["off"] + } + ] + }, + MemberExpression: { + oneOf: [ + { + type: "integer", + minimum: 0 + }, + { + enum: ["off"] + } + ] + }, + FunctionDeclaration: { + type: "object", + properties: { + parameters: ELEMENT_LIST_SCHEMA, + body: { + type: "integer", + minimum: 0 + } + }, + additionalProperties: false + }, + FunctionExpression: { + type: "object", + properties: { + parameters: ELEMENT_LIST_SCHEMA, + body: { + type: "integer", + minimum: 0 + } + }, + additionalProperties: false + }, + StaticBlock: { + type: "object", + properties: { + body: { + type: "integer", + minimum: 0 + } + }, + additionalProperties: false + }, + CallExpression: { + type: "object", + properties: { + arguments: ELEMENT_LIST_SCHEMA + }, + additionalProperties: false + }, + ArrayExpression: ELEMENT_LIST_SCHEMA, + ObjectExpression: ELEMENT_LIST_SCHEMA, + ImportDeclaration: ELEMENT_LIST_SCHEMA, + flatTernaryExpressions: { + type: "boolean", + default: false + }, + offsetTernaryExpressions: { + type: "boolean", + default: false + }, + ignoredNodes: { + type: "array", + items: { + type: "string", + not: { + pattern: ":exit$" + } + } + }, + ignoreComments: { + type: "boolean", + default: false + } + }, + additionalProperties: false + } + ], + messages: { + wrongIndentation: "Expected indentation of {{expected}} but found {{actual}}." + } + }, + + create(context) { + const DEFAULT_VARIABLE_INDENT = 1; + const DEFAULT_PARAMETER_INDENT = 1; + const DEFAULT_FUNCTION_BODY_INDENT = 1; + + let indentType = "space"; + let indentSize = 4; + const options = { + SwitchCase: 0, + VariableDeclarator: { + var: DEFAULT_VARIABLE_INDENT, + let: DEFAULT_VARIABLE_INDENT, + const: DEFAULT_VARIABLE_INDENT + }, + outerIIFEBody: 1, + FunctionDeclaration: { + parameters: DEFAULT_PARAMETER_INDENT, + body: DEFAULT_FUNCTION_BODY_INDENT + }, + FunctionExpression: { + parameters: DEFAULT_PARAMETER_INDENT, + body: DEFAULT_FUNCTION_BODY_INDENT + }, + StaticBlock: { + body: DEFAULT_FUNCTION_BODY_INDENT + }, + CallExpression: { + arguments: DEFAULT_PARAMETER_INDENT + }, + MemberExpression: 1, + ArrayExpression: 1, + ObjectExpression: 1, + ImportDeclaration: 1, + flatTernaryExpressions: false, + ignoredNodes: [], + ignoreComments: false + }; + + if (context.options.length) { + if (context.options[0] === "tab") { + indentSize = 1; + indentType = "tab"; + } else { + indentSize = context.options[0]; + indentType = "space"; + } + + if (context.options[1]) { + Object.assign(options, context.options[1]); + + if (typeof options.VariableDeclarator === "number" || options.VariableDeclarator === "first") { + options.VariableDeclarator = { + var: options.VariableDeclarator, + let: options.VariableDeclarator, + const: options.VariableDeclarator + }; + } + } + } + + const sourceCode = context.sourceCode; + const tokenInfo = new TokenInfo(sourceCode); + const offsets = new OffsetStorage(tokenInfo, indentSize, indentType === "space" ? " " : "\t", sourceCode.text.length); + const parameterParens = new WeakSet(); + + /** + * Creates an error message for a line, given the expected/actual indentation. + * @param {int} expectedAmount The expected amount of indentation characters for this line + * @param {int} actualSpaces The actual number of indentation spaces that were found on this line + * @param {int} actualTabs The actual number of indentation tabs that were found on this line + * @returns {string} An error message for this line + */ + function createErrorMessageData(expectedAmount, actualSpaces, actualTabs) { + const expectedStatement = `${expectedAmount} ${indentType}${expectedAmount === 1 ? "" : "s"}`; // e.g. "2 tabs" + const foundSpacesWord = `space${actualSpaces === 1 ? "" : "s"}`; // e.g. "space" + const foundTabsWord = `tab${actualTabs === 1 ? "" : "s"}`; // e.g. "tabs" + let foundStatement; + + if (actualSpaces > 0) { + + /* + * Abbreviate the message if the expected indentation is also spaces. + * e.g. 'Expected 4 spaces but found 2' rather than 'Expected 4 spaces but found 2 spaces' + */ + foundStatement = indentType === "space" ? actualSpaces : `${actualSpaces} ${foundSpacesWord}`; + } else if (actualTabs > 0) { + foundStatement = indentType === "tab" ? actualTabs : `${actualTabs} ${foundTabsWord}`; + } else { + foundStatement = "0"; + } + return { + expected: expectedStatement, + actual: foundStatement + }; + } + + /** + * Reports a given indent violation + * @param {Token} token Token violating the indent rule + * @param {string} neededIndent Expected indentation string + * @returns {void} + */ + function report(token, neededIndent) { + const actualIndent = Array.from(tokenInfo.getTokenIndent(token)); + const numSpaces = actualIndent.filter(char => char === " ").length; + const numTabs = actualIndent.filter(char => char === "\t").length; + + context.report({ + node: token, + messageId: "wrongIndentation", + data: createErrorMessageData(neededIndent.length, numSpaces, numTabs), + loc: { + start: { line: token.loc.start.line, column: 0 }, + end: { line: token.loc.start.line, column: token.loc.start.column } + }, + fix(fixer) { + const range = [token.range[0] - token.loc.start.column, token.range[0]]; + const newText = neededIndent; + + return fixer.replaceTextRange(range, newText); + } + }); + } + + /** + * Checks if a token's indentation is correct + * @param {Token} token Token to examine + * @param {string} desiredIndent Desired indentation of the string + * @returns {boolean} `true` if the token's indentation is correct + */ + function validateTokenIndent(token, desiredIndent) { + const indentation = tokenInfo.getTokenIndent(token); + + return indentation === desiredIndent || + + // To avoid conflicts with no-mixed-spaces-and-tabs, don't report mixed spaces and tabs. + indentation.includes(" ") && indentation.includes("\t"); + } + + /** + * Check to see if the node is a file level IIFE + * @param {ASTNode} node The function node to check. + * @returns {boolean} True if the node is the outer IIFE + */ + function isOuterIIFE(node) { + + /* + * Verify that the node is an IIFE + */ + if (!node.parent || node.parent.type !== "CallExpression" || node.parent.callee !== node) { + return false; + } + + /* + * Navigate legal ancestors to determine whether this IIFE is outer. + * A "legal ancestor" is an expression or statement that causes the function to get executed immediately. + * For example, `!(function(){})()` is an outer IIFE even though it is preceded by a ! operator. + */ + let statement = node.parent && node.parent.parent; + + while ( + statement.type === "UnaryExpression" && ["!", "~", "+", "-"].includes(statement.operator) || + statement.type === "AssignmentExpression" || + statement.type === "LogicalExpression" || + statement.type === "SequenceExpression" || + statement.type === "VariableDeclarator" + ) { + statement = statement.parent; + } + + return (statement.type === "ExpressionStatement" || statement.type === "VariableDeclaration") && statement.parent.type === "Program"; + } + + /** + * Counts the number of linebreaks that follow the last non-whitespace character in a string + * @param {string} string The string to check + * @returns {number} The number of JavaScript linebreaks that follow the last non-whitespace character, + * or the total number of linebreaks if the string is all whitespace. + */ + function countTrailingLinebreaks(string) { + const trailingWhitespace = string.match(/\s*$/u)[0]; + const linebreakMatches = trailingWhitespace.match(astUtils.createGlobalLinebreakMatcher()); + + return linebreakMatches === null ? 0 : linebreakMatches.length; + } + + /** + * Check indentation for lists of elements (arrays, objects, function params) + * @param {ASTNode[]} elements List of elements that should be offset + * @param {Token} startToken The start token of the list that element should be aligned against, e.g. '[' + * @param {Token} endToken The end token of the list, e.g. ']' + * @param {number|string} offset The amount that the elements should be offset + * @returns {void} + */ + function addElementListIndent(elements, startToken, endToken, offset) { + + /** + * Gets the first token of a given element, including surrounding parentheses. + * @param {ASTNode} element A node in the `elements` list + * @returns {Token} The first token of this element + */ + function getFirstToken(element) { + let token = sourceCode.getTokenBefore(element); + + while (astUtils.isOpeningParenToken(token) && token !== startToken) { + token = sourceCode.getTokenBefore(token); + } + return sourceCode.getTokenAfter(token); + } + + // Run through all the tokens in the list, and offset them by one indent level (mainly for comments, other things will end up overridden) + offsets.setDesiredOffsets( + [startToken.range[1], endToken.range[0]], + startToken, + typeof offset === "number" ? offset : 1 + ); + offsets.setDesiredOffset(endToken, startToken, 0); + + // If the preference is "first" but there is no first element (e.g. sparse arrays w/ empty first slot), fall back to 1 level. + if (offset === "first" && elements.length && !elements[0]) { + return; + } + elements.forEach((element, index) => { + if (!element) { + + // Skip holes in arrays + return; + } + if (offset === "off") { + + // Ignore the first token of every element if the "off" option is used + offsets.ignoreToken(getFirstToken(element)); + } + + // Offset the following elements correctly relative to the first element + if (index === 0) { + return; + } + if (offset === "first" && tokenInfo.isFirstTokenOfLine(getFirstToken(element))) { + offsets.matchOffsetOf(getFirstToken(elements[0]), getFirstToken(element)); + } else { + const previousElement = elements[index - 1]; + const firstTokenOfPreviousElement = previousElement && getFirstToken(previousElement); + const previousElementLastToken = previousElement && sourceCode.getLastToken(previousElement); + + if ( + previousElement && + previousElementLastToken.loc.end.line - countTrailingLinebreaks(previousElementLastToken.value) > startToken.loc.end.line + ) { + offsets.setDesiredOffsets( + [previousElement.range[1], element.range[1]], + firstTokenOfPreviousElement, + 0 + ); + } + } + }); + } + + /** + * Check and decide whether to check for indentation for blockless nodes + * Scenarios are for or while statements without braces around them + * @param {ASTNode} node node to examine + * @returns {void} + */ + function addBlocklessNodeIndent(node) { + if (node.type !== "BlockStatement") { + const lastParentToken = sourceCode.getTokenBefore(node, astUtils.isNotOpeningParenToken); + + let firstBodyToken = sourceCode.getFirstToken(node); + let lastBodyToken = sourceCode.getLastToken(node); + + while ( + astUtils.isOpeningParenToken(sourceCode.getTokenBefore(firstBodyToken)) && + astUtils.isClosingParenToken(sourceCode.getTokenAfter(lastBodyToken)) + ) { + firstBodyToken = sourceCode.getTokenBefore(firstBodyToken); + lastBodyToken = sourceCode.getTokenAfter(lastBodyToken); + } + + offsets.setDesiredOffsets([firstBodyToken.range[0], lastBodyToken.range[1]], lastParentToken, 1); + } + } + + /** + * Checks the indentation for nodes that are like function calls (`CallExpression` and `NewExpression`) + * @param {ASTNode} node A CallExpression or NewExpression node + * @returns {void} + */ + function addFunctionCallIndent(node) { + let openingParen; + + if (node.arguments.length) { + openingParen = sourceCode.getFirstTokenBetween(node.callee, node.arguments[0], astUtils.isOpeningParenToken); + } else { + openingParen = sourceCode.getLastToken(node, 1); + } + const closingParen = sourceCode.getLastToken(node); + + parameterParens.add(openingParen); + parameterParens.add(closingParen); + + /* + * If `?.` token exists, set desired offset for that. + * This logic is copied from `MemberExpression`'s. + */ + if (node.optional) { + const dotToken = sourceCode.getTokenAfter(node.callee, astUtils.isQuestionDotToken); + const calleeParenCount = sourceCode.getTokensBetween(node.callee, dotToken, { filter: astUtils.isClosingParenToken }).length; + const firstTokenOfCallee = calleeParenCount + ? sourceCode.getTokenBefore(node.callee, { skip: calleeParenCount - 1 }) + : sourceCode.getFirstToken(node.callee); + const lastTokenOfCallee = sourceCode.getTokenBefore(dotToken); + const offsetBase = lastTokenOfCallee.loc.end.line === openingParen.loc.start.line + ? lastTokenOfCallee + : firstTokenOfCallee; + + offsets.setDesiredOffset(dotToken, offsetBase, 1); + } + + const offsetAfterToken = node.callee.type === "TaggedTemplateExpression" ? sourceCode.getFirstToken(node.callee.quasi) : openingParen; + const offsetToken = sourceCode.getTokenBefore(offsetAfterToken); + + offsets.setDesiredOffset(openingParen, offsetToken, 0); + + addElementListIndent(node.arguments, openingParen, closingParen, options.CallExpression.arguments); + } + + /** + * Checks the indentation of parenthesized values, given a list of tokens in a program + * @param {Token[]} tokens A list of tokens + * @returns {void} + */ + function addParensIndent(tokens) { + const parenStack = []; + const parenPairs = []; + + for (let i = 0; i < tokens.length; i++) { + const nextToken = tokens[i]; + + if (astUtils.isOpeningParenToken(nextToken)) { + parenStack.push(nextToken); + } else if (astUtils.isClosingParenToken(nextToken)) { + parenPairs.push({ left: parenStack.pop(), right: nextToken }); + } + } + + for (let i = parenPairs.length - 1; i >= 0; i--) { + const leftParen = parenPairs[i].left; + const rightParen = parenPairs[i].right; + + // We only want to handle parens around expressions, so exclude parentheses that are in function parameters and function call arguments. + if (!parameterParens.has(leftParen) && !parameterParens.has(rightParen)) { + const parenthesizedTokens = new Set(sourceCode.getTokensBetween(leftParen, rightParen)); + + parenthesizedTokens.forEach(token => { + if (!parenthesizedTokens.has(offsets.getFirstDependency(token))) { + offsets.setDesiredOffset(token, leftParen, 1); + } + }); + } + + offsets.setDesiredOffset(rightParen, leftParen, 0); + } + } + + /** + * Ignore all tokens within an unknown node whose offset do not depend + * on another token's offset within the unknown node + * @param {ASTNode} node Unknown Node + * @returns {void} + */ + function ignoreNode(node) { + const unknownNodeTokens = new Set(sourceCode.getTokens(node, { includeComments: true })); + + unknownNodeTokens.forEach(token => { + if (!unknownNodeTokens.has(offsets.getFirstDependency(token))) { + const firstTokenOfLine = tokenInfo.getFirstTokenOfLine(token); + + if (token === firstTokenOfLine) { + offsets.ignoreToken(token); + } else { + offsets.setDesiredOffset(token, firstTokenOfLine, 0); + } + } + }); + } + + /** + * Check whether the given token is on the first line of a statement. + * @param {Token} token The token to check. + * @param {ASTNode} leafNode The expression node that the token belongs directly. + * @returns {boolean} `true` if the token is on the first line of a statement. + */ + function isOnFirstLineOfStatement(token, leafNode) { + let node = leafNode; + + while (node.parent && !node.parent.type.endsWith("Statement") && !node.parent.type.endsWith("Declaration")) { + node = node.parent; + } + node = node.parent; + + return !node || node.loc.start.line === token.loc.start.line; + } + + /** + * Check whether there are any blank (whitespace-only) lines between + * two tokens on separate lines. + * @param {Token} firstToken The first token. + * @param {Token} secondToken The second token. + * @returns {boolean} `true` if the tokens are on separate lines and + * there exists a blank line between them, `false` otherwise. + */ + function hasBlankLinesBetween(firstToken, secondToken) { + const firstTokenLine = firstToken.loc.end.line; + const secondTokenLine = secondToken.loc.start.line; + + if (firstTokenLine === secondTokenLine || firstTokenLine === secondTokenLine - 1) { + return false; + } + + for (let line = firstTokenLine + 1; line < secondTokenLine; ++line) { + if (!tokenInfo.firstTokensByLineNumber.has(line)) { + return true; + } + } + + return false; + } + + const ignoredNodeFirstTokens = new Set(); + + const baseOffsetListeners = { + "ArrayExpression, ArrayPattern"(node) { + const openingBracket = sourceCode.getFirstToken(node); + const closingBracket = sourceCode.getTokenAfter([...node.elements].reverse().find(_ => _) || openingBracket, astUtils.isClosingBracketToken); + + addElementListIndent(node.elements, openingBracket, closingBracket, options.ArrayExpression); + }, + + "ObjectExpression, ObjectPattern"(node) { + const openingCurly = sourceCode.getFirstToken(node); + const closingCurly = sourceCode.getTokenAfter( + node.properties.length ? node.properties[node.properties.length - 1] : openingCurly, + astUtils.isClosingBraceToken + ); + + addElementListIndent(node.properties, openingCurly, closingCurly, options.ObjectExpression); + }, + + ArrowFunctionExpression(node) { + const maybeOpeningParen = sourceCode.getFirstToken(node, { skip: node.async ? 1 : 0 }); + + if (astUtils.isOpeningParenToken(maybeOpeningParen)) { + const openingParen = maybeOpeningParen; + const closingParen = sourceCode.getTokenBefore(node.body, astUtils.isClosingParenToken); + + parameterParens.add(openingParen); + parameterParens.add(closingParen); + addElementListIndent(node.params, openingParen, closingParen, options.FunctionExpression.parameters); + } + + addBlocklessNodeIndent(node.body); + }, + + AssignmentExpression(node) { + const operator = sourceCode.getFirstTokenBetween(node.left, node.right, token => token.value === node.operator); + + offsets.setDesiredOffsets([operator.range[0], node.range[1]], sourceCode.getLastToken(node.left), 1); + offsets.ignoreToken(operator); + offsets.ignoreToken(sourceCode.getTokenAfter(operator)); + }, + + "BinaryExpression, LogicalExpression"(node) { + const operator = sourceCode.getFirstTokenBetween(node.left, node.right, token => token.value === node.operator); + + /* + * For backwards compatibility, don't check BinaryExpression indents, e.g. + * var foo = bar && + * baz; + */ + + const tokenAfterOperator = sourceCode.getTokenAfter(operator); + + offsets.ignoreToken(operator); + offsets.ignoreToken(tokenAfterOperator); + offsets.setDesiredOffset(tokenAfterOperator, operator, 0); + }, + + "BlockStatement, ClassBody"(node) { + let blockIndentLevel; + + if (node.parent && isOuterIIFE(node.parent)) { + blockIndentLevel = options.outerIIFEBody; + } else if (node.parent && (node.parent.type === "FunctionExpression" || node.parent.type === "ArrowFunctionExpression")) { + blockIndentLevel = options.FunctionExpression.body; + } else if (node.parent && node.parent.type === "FunctionDeclaration") { + blockIndentLevel = options.FunctionDeclaration.body; + } else { + blockIndentLevel = 1; + } + + /* + * For blocks that aren't lone statements, ensure that the opening curly brace + * is aligned with the parent. + */ + if (!astUtils.STATEMENT_LIST_PARENTS.has(node.parent.type)) { + offsets.setDesiredOffset(sourceCode.getFirstToken(node), sourceCode.getFirstToken(node.parent), 0); + } + + addElementListIndent(node.body, sourceCode.getFirstToken(node), sourceCode.getLastToken(node), blockIndentLevel); + }, + + CallExpression: addFunctionCallIndent, + + "ClassDeclaration[superClass], ClassExpression[superClass]"(node) { + const classToken = sourceCode.getFirstToken(node); + const extendsToken = sourceCode.getTokenBefore(node.superClass, astUtils.isNotOpeningParenToken); + + offsets.setDesiredOffsets([extendsToken.range[0], node.body.range[0]], classToken, 1); + }, + + ConditionalExpression(node) { + const firstToken = sourceCode.getFirstToken(node); + + // `flatTernaryExpressions` option is for the following style: + // var a = + // foo > 0 ? bar : + // foo < 0 ? baz : + // /*else*/ qiz ; + if (!options.flatTernaryExpressions || + !astUtils.isTokenOnSameLine(node.test, node.consequent) || + isOnFirstLineOfStatement(firstToken, node) + ) { + const questionMarkToken = sourceCode.getFirstTokenBetween(node.test, node.consequent, token => token.type === "Punctuator" && token.value === "?"); + const colonToken = sourceCode.getFirstTokenBetween(node.consequent, node.alternate, token => token.type === "Punctuator" && token.value === ":"); + + const firstConsequentToken = sourceCode.getTokenAfter(questionMarkToken); + const lastConsequentToken = sourceCode.getTokenBefore(colonToken); + const firstAlternateToken = sourceCode.getTokenAfter(colonToken); + + offsets.setDesiredOffset(questionMarkToken, firstToken, 1); + offsets.setDesiredOffset(colonToken, firstToken, 1); + + offsets.setDesiredOffset(firstConsequentToken, firstToken, firstConsequentToken.type === "Punctuator" && + options.offsetTernaryExpressions ? 2 : 1); + + /* + * The alternate and the consequent should usually have the same indentation. + * If they share part of a line, align the alternate against the first token of the consequent. + * This allows the alternate to be indented correctly in cases like this: + * foo ? ( + * bar + * ) : ( // this '(' is aligned with the '(' above, so it's considered to be aligned with `foo` + * baz // as a result, `baz` is offset by 1 rather than 2 + * ) + */ + if (lastConsequentToken.loc.end.line === firstAlternateToken.loc.start.line) { + offsets.setDesiredOffset(firstAlternateToken, firstConsequentToken, 0); + } else { + + /** + * If the alternate and consequent do not share part of a line, offset the alternate from the first + * token of the conditional expression. For example: + * foo ? bar + * : baz + * + * If `baz` were aligned with `bar` rather than being offset by 1 from `foo`, `baz` would end up + * having no expected indentation. + */ + offsets.setDesiredOffset(firstAlternateToken, firstToken, firstAlternateToken.type === "Punctuator" && + options.offsetTernaryExpressions ? 2 : 1); + } + } + }, + + "DoWhileStatement, WhileStatement, ForInStatement, ForOfStatement, WithStatement": node => addBlocklessNodeIndent(node.body), + + ExportNamedDeclaration(node) { + if (node.declaration === null) { + const closingCurly = sourceCode.getLastToken(node, astUtils.isClosingBraceToken); + + // Indent the specifiers in `export {foo, bar, baz}` + addElementListIndent(node.specifiers, sourceCode.getFirstToken(node, { skip: 1 }), closingCurly, 1); + + if (node.source) { + + // Indent everything after and including the `from` token in `export {foo, bar, baz} from 'qux'` + offsets.setDesiredOffsets([closingCurly.range[1], node.range[1]], sourceCode.getFirstToken(node), 1); + } + } + }, + + ForStatement(node) { + const forOpeningParen = sourceCode.getFirstToken(node, 1); + + if (node.init) { + offsets.setDesiredOffsets(node.init.range, forOpeningParen, 1); + } + if (node.test) { + offsets.setDesiredOffsets(node.test.range, forOpeningParen, 1); + } + if (node.update) { + offsets.setDesiredOffsets(node.update.range, forOpeningParen, 1); + } + addBlocklessNodeIndent(node.body); + }, + + "FunctionDeclaration, FunctionExpression"(node) { + const closingParen = sourceCode.getTokenBefore(node.body); + const openingParen = sourceCode.getTokenBefore(node.params.length ? node.params[0] : closingParen); + + parameterParens.add(openingParen); + parameterParens.add(closingParen); + addElementListIndent(node.params, openingParen, closingParen, options[node.type].parameters); + }, + + IfStatement(node) { + addBlocklessNodeIndent(node.consequent); + if (node.alternate) { + addBlocklessNodeIndent(node.alternate); + } + }, + + /* + * For blockless nodes with semicolon-first style, don't indent the semicolon. + * e.g. + * if (foo) + * bar() + * ; [1, 2, 3].map(foo) + * + * Traversal into the node sets indentation of the semicolon, so we need to override it on exit. + */ + ":matches(DoWhileStatement, ForStatement, ForInStatement, ForOfStatement, IfStatement, WhileStatement, WithStatement):exit"(node) { + let nodesToCheck; + + if (node.type === "IfStatement") { + nodesToCheck = [node.consequent]; + if (node.alternate) { + nodesToCheck.push(node.alternate); + } + } else { + nodesToCheck = [node.body]; + } + + for (const nodeToCheck of nodesToCheck) { + const lastToken = sourceCode.getLastToken(nodeToCheck); + + if (astUtils.isSemicolonToken(lastToken)) { + const tokenBeforeLast = sourceCode.getTokenBefore(lastToken); + const tokenAfterLast = sourceCode.getTokenAfter(lastToken); + + // override indentation of `;` only if its line looks like a semicolon-first style line + if ( + !astUtils.isTokenOnSameLine(tokenBeforeLast, lastToken) && + tokenAfterLast && + astUtils.isTokenOnSameLine(lastToken, tokenAfterLast) + ) { + offsets.setDesiredOffset( + lastToken, + sourceCode.getFirstToken(node), + 0 + ); + } + } + } + }, + + ImportDeclaration(node) { + if (node.specifiers.some(specifier => specifier.type === "ImportSpecifier")) { + const openingCurly = sourceCode.getFirstToken(node, astUtils.isOpeningBraceToken); + const closingCurly = sourceCode.getLastToken(node, astUtils.isClosingBraceToken); + + addElementListIndent(node.specifiers.filter(specifier => specifier.type === "ImportSpecifier"), openingCurly, closingCurly, options.ImportDeclaration); + } + + const fromToken = sourceCode.getLastToken(node, token => token.type === "Identifier" && token.value === "from"); + const sourceToken = sourceCode.getLastToken(node, token => token.type === "String"); + const semiToken = sourceCode.getLastToken(node, token => token.type === "Punctuator" && token.value === ";"); + + if (fromToken) { + const end = semiToken && semiToken.range[1] === sourceToken.range[1] ? node.range[1] : sourceToken.range[1]; + + offsets.setDesiredOffsets([fromToken.range[0], end], sourceCode.getFirstToken(node), 1); + } + }, + + ImportExpression(node) { + const openingParen = sourceCode.getFirstToken(node, 1); + const closingParen = sourceCode.getLastToken(node); + + parameterParens.add(openingParen); + parameterParens.add(closingParen); + offsets.setDesiredOffset(openingParen, sourceCode.getTokenBefore(openingParen), 0); + + addElementListIndent([node.source], openingParen, closingParen, options.CallExpression.arguments); + }, + + "MemberExpression, JSXMemberExpression, MetaProperty"(node) { + const object = node.type === "MetaProperty" ? node.meta : node.object; + const firstNonObjectToken = sourceCode.getFirstTokenBetween(object, node.property, astUtils.isNotClosingParenToken); + const secondNonObjectToken = sourceCode.getTokenAfter(firstNonObjectToken); + + const objectParenCount = sourceCode.getTokensBetween(object, node.property, { filter: astUtils.isClosingParenToken }).length; + const firstObjectToken = objectParenCount + ? sourceCode.getTokenBefore(object, { skip: objectParenCount - 1 }) + : sourceCode.getFirstToken(object); + const lastObjectToken = sourceCode.getTokenBefore(firstNonObjectToken); + const firstPropertyToken = node.computed ? firstNonObjectToken : secondNonObjectToken; + + if (node.computed) { + + // For computed MemberExpressions, match the closing bracket with the opening bracket. + offsets.setDesiredOffset(sourceCode.getLastToken(node), firstNonObjectToken, 0); + offsets.setDesiredOffsets(node.property.range, firstNonObjectToken, 1); + } + + /* + * If the object ends on the same line that the property starts, match against the last token + * of the object, to ensure that the MemberExpression is not indented. + * + * Otherwise, match against the first token of the object, e.g. + * foo + * .bar + * .baz // <-- offset by 1 from `foo` + */ + const offsetBase = lastObjectToken.loc.end.line === firstPropertyToken.loc.start.line + ? lastObjectToken + : firstObjectToken; + + if (typeof options.MemberExpression === "number") { + + // Match the dot (for non-computed properties) or the opening bracket (for computed properties) against the object. + offsets.setDesiredOffset(firstNonObjectToken, offsetBase, options.MemberExpression); + + /* + * For computed MemberExpressions, match the first token of the property against the opening bracket. + * Otherwise, match the first token of the property against the object. + */ + offsets.setDesiredOffset(secondNonObjectToken, node.computed ? firstNonObjectToken : offsetBase, options.MemberExpression); + } else { + + // If the MemberExpression option is off, ignore the dot and the first token of the property. + offsets.ignoreToken(firstNonObjectToken); + offsets.ignoreToken(secondNonObjectToken); + + // To ignore the property indentation, ensure that the property tokens depend on the ignored tokens. + offsets.setDesiredOffset(firstNonObjectToken, offsetBase, 0); + offsets.setDesiredOffset(secondNonObjectToken, firstNonObjectToken, 0); + } + }, + + NewExpression(node) { + + // Only indent the arguments if the NewExpression has parens (e.g. `new Foo(bar)` or `new Foo()`, but not `new Foo` + if (node.arguments.length > 0 || + astUtils.isClosingParenToken(sourceCode.getLastToken(node)) && + astUtils.isOpeningParenToken(sourceCode.getLastToken(node, 1))) { + addFunctionCallIndent(node); + } + }, + + Property(node) { + if (!node.shorthand && !node.method && node.kind === "init") { + const colon = sourceCode.getFirstTokenBetween(node.key, node.value, astUtils.isColonToken); + + offsets.ignoreToken(sourceCode.getTokenAfter(colon)); + } + }, + + PropertyDefinition(node) { + const firstToken = sourceCode.getFirstToken(node); + const maybeSemicolonToken = sourceCode.getLastToken(node); + let keyLastToken = null; + + // Indent key. + if (node.computed) { + const bracketTokenL = sourceCode.getTokenBefore(node.key, astUtils.isOpeningBracketToken); + const bracketTokenR = keyLastToken = sourceCode.getTokenAfter(node.key, astUtils.isClosingBracketToken); + const keyRange = [bracketTokenL.range[1], bracketTokenR.range[0]]; + + if (bracketTokenL !== firstToken) { + offsets.setDesiredOffset(bracketTokenL, firstToken, 0); + } + offsets.setDesiredOffsets(keyRange, bracketTokenL, 1); + offsets.setDesiredOffset(bracketTokenR, bracketTokenL, 0); + } else { + const idToken = keyLastToken = sourceCode.getFirstToken(node.key); + + if (idToken !== firstToken) { + offsets.setDesiredOffset(idToken, firstToken, 1); + } + } + + // Indent initializer. + if (node.value) { + const eqToken = sourceCode.getTokenBefore(node.value, astUtils.isEqToken); + const valueToken = sourceCode.getTokenAfter(eqToken); + + offsets.setDesiredOffset(eqToken, keyLastToken, 1); + offsets.setDesiredOffset(valueToken, eqToken, 1); + if (astUtils.isSemicolonToken(maybeSemicolonToken)) { + offsets.setDesiredOffset(maybeSemicolonToken, eqToken, 1); + } + } else if (astUtils.isSemicolonToken(maybeSemicolonToken)) { + offsets.setDesiredOffset(maybeSemicolonToken, keyLastToken, 1); + } + }, + + StaticBlock(node) { + const openingCurly = sourceCode.getFirstToken(node, { skip: 1 }); // skip the `static` token + const closingCurly = sourceCode.getLastToken(node); + + addElementListIndent(node.body, openingCurly, closingCurly, options.StaticBlock.body); + }, + + SwitchStatement(node) { + const openingCurly = sourceCode.getTokenAfter(node.discriminant, astUtils.isOpeningBraceToken); + const closingCurly = sourceCode.getLastToken(node); + + offsets.setDesiredOffsets([openingCurly.range[1], closingCurly.range[0]], openingCurly, options.SwitchCase); + + if (node.cases.length) { + sourceCode.getTokensBetween( + node.cases[node.cases.length - 1], + closingCurly, + { includeComments: true, filter: astUtils.isCommentToken } + ).forEach(token => offsets.ignoreToken(token)); + } + }, + + SwitchCase(node) { + if (!(node.consequent.length === 1 && node.consequent[0].type === "BlockStatement")) { + const caseKeyword = sourceCode.getFirstToken(node); + const tokenAfterCurrentCase = sourceCode.getTokenAfter(node); + + offsets.setDesiredOffsets([caseKeyword.range[1], tokenAfterCurrentCase.range[0]], caseKeyword, 1); + } + }, + + TemplateLiteral(node) { + node.expressions.forEach((expression, index) => { + const previousQuasi = node.quasis[index]; + const nextQuasi = node.quasis[index + 1]; + const tokenToAlignFrom = previousQuasi.loc.start.line === previousQuasi.loc.end.line + ? sourceCode.getFirstToken(previousQuasi) + : null; + + offsets.setDesiredOffsets([previousQuasi.range[1], nextQuasi.range[0]], tokenToAlignFrom, 1); + offsets.setDesiredOffset(sourceCode.getFirstToken(nextQuasi), tokenToAlignFrom, 0); + }); + }, + + VariableDeclaration(node) { + let variableIndent = Object.prototype.hasOwnProperty.call(options.VariableDeclarator, node.kind) + ? options.VariableDeclarator[node.kind] + : DEFAULT_VARIABLE_INDENT; + + const firstToken = sourceCode.getFirstToken(node), + lastToken = sourceCode.getLastToken(node); + + if (options.VariableDeclarator[node.kind] === "first") { + if (node.declarations.length > 1) { + addElementListIndent( + node.declarations, + firstToken, + lastToken, + "first" + ); + return; + } + + variableIndent = DEFAULT_VARIABLE_INDENT; + } + + if (node.declarations[node.declarations.length - 1].loc.start.line > node.loc.start.line) { + + /* + * VariableDeclarator indentation is a bit different from other forms of indentation, in that the + * indentation of an opening bracket sometimes won't match that of a closing bracket. For example, + * the following indentations are correct: + * + * var foo = { + * ok: true + * }; + * + * var foo = { + * ok: true, + * }, + * bar = 1; + * + * Account for when exiting the AST (after indentations have already been set for the nodes in + * the declaration) by manually increasing the indentation level of the tokens in this declarator + * on the same line as the start of the declaration, provided that there are declarators that + * follow this one. + */ + offsets.setDesiredOffsets(node.range, firstToken, variableIndent, true); + } else { + offsets.setDesiredOffsets(node.range, firstToken, variableIndent); + } + + if (astUtils.isSemicolonToken(lastToken)) { + offsets.ignoreToken(lastToken); + } + }, + + VariableDeclarator(node) { + if (node.init) { + const equalOperator = sourceCode.getTokenBefore(node.init, astUtils.isNotOpeningParenToken); + const tokenAfterOperator = sourceCode.getTokenAfter(equalOperator); + + offsets.ignoreToken(equalOperator); + offsets.ignoreToken(tokenAfterOperator); + offsets.setDesiredOffsets([tokenAfterOperator.range[0], node.range[1]], equalOperator, 1); + offsets.setDesiredOffset(equalOperator, sourceCode.getLastToken(node.id), 0); + } + }, + + "JSXAttribute[value]"(node) { + const equalsToken = sourceCode.getFirstTokenBetween(node.name, node.value, token => token.type === "Punctuator" && token.value === "="); + + offsets.setDesiredOffsets([equalsToken.range[0], node.value.range[1]], sourceCode.getFirstToken(node.name), 1); + }, + + JSXElement(node) { + if (node.closingElement) { + addElementListIndent(node.children, sourceCode.getFirstToken(node.openingElement), sourceCode.getFirstToken(node.closingElement), 1); + } + }, + + JSXOpeningElement(node) { + const firstToken = sourceCode.getFirstToken(node); + let closingToken; + + if (node.selfClosing) { + closingToken = sourceCode.getLastToken(node, { skip: 1 }); + offsets.setDesiredOffset(sourceCode.getLastToken(node), closingToken, 0); + } else { + closingToken = sourceCode.getLastToken(node); + } + offsets.setDesiredOffsets(node.name.range, sourceCode.getFirstToken(node)); + addElementListIndent(node.attributes, firstToken, closingToken, 1); + }, + + JSXClosingElement(node) { + const firstToken = sourceCode.getFirstToken(node); + + offsets.setDesiredOffsets(node.name.range, firstToken, 1); + }, + + JSXFragment(node) { + const firstOpeningToken = sourceCode.getFirstToken(node.openingFragment); + const firstClosingToken = sourceCode.getFirstToken(node.closingFragment); + + addElementListIndent(node.children, firstOpeningToken, firstClosingToken, 1); + }, + + JSXOpeningFragment(node) { + const firstToken = sourceCode.getFirstToken(node); + const closingToken = sourceCode.getLastToken(node); + + offsets.setDesiredOffsets(node.range, firstToken, 1); + offsets.matchOffsetOf(firstToken, closingToken); + }, + + JSXClosingFragment(node) { + const firstToken = sourceCode.getFirstToken(node); + const slashToken = sourceCode.getLastToken(node, { skip: 1 }); + const closingToken = sourceCode.getLastToken(node); + const tokenToMatch = astUtils.isTokenOnSameLine(slashToken, closingToken) ? slashToken : closingToken; + + offsets.setDesiredOffsets(node.range, firstToken, 1); + offsets.matchOffsetOf(firstToken, tokenToMatch); + }, + + JSXExpressionContainer(node) { + const openingCurly = sourceCode.getFirstToken(node); + const closingCurly = sourceCode.getLastToken(node); + + offsets.setDesiredOffsets( + [openingCurly.range[1], closingCurly.range[0]], + openingCurly, + 1 + ); + }, + + JSXSpreadAttribute(node) { + const openingCurly = sourceCode.getFirstToken(node); + const closingCurly = sourceCode.getLastToken(node); + + offsets.setDesiredOffsets( + [openingCurly.range[1], closingCurly.range[0]], + openingCurly, + 1 + ); + }, + + "*"(node) { + const firstToken = sourceCode.getFirstToken(node); + + // Ensure that the children of every node are indented at least as much as the first token. + if (firstToken && !ignoredNodeFirstTokens.has(firstToken)) { + offsets.setDesiredOffsets(node.range, firstToken, 0); + } + } + }; + + const listenerCallQueue = []; + + /* + * To ignore the indentation of a node: + * 1. Don't call the node's listener when entering it (if it has a listener) + * 2. Don't set any offsets against the first token of the node. + * 3. Call `ignoreNode` on the node sometime after exiting it and before validating offsets. + */ + const offsetListeners = {}; + + for (const [selector, listener] of Object.entries(baseOffsetListeners)) { + + /* + * Offset listener calls are deferred until traversal is finished, and are called as + * part of the final `Program:exit` listener. This is necessary because a node might + * be matched by multiple selectors. + * + * Example: Suppose there is an offset listener for `Identifier`, and the user has + * specified in configuration that `MemberExpression > Identifier` should be ignored. + * Due to selector specificity rules, the `Identifier` listener will get called first. However, + * if a given Identifier node is supposed to be ignored, then the `Identifier` offset listener + * should not have been called at all. Without doing extra selector matching, we don't know + * whether the Identifier matches the `MemberExpression > Identifier` selector until the + * `MemberExpression > Identifier` listener is called. + * + * To avoid this, the `Identifier` listener isn't called until traversal finishes and all + * ignored nodes are known. + */ + offsetListeners[selector] = node => listenerCallQueue.push({ listener, node }); + } + + // For each ignored node selector, set up a listener to collect it into the `ignoredNodes` set. + const ignoredNodes = new Set(); + + /** + * Ignores a node + * @param {ASTNode} node The node to ignore + * @returns {void} + */ + function addToIgnoredNodes(node) { + ignoredNodes.add(node); + ignoredNodeFirstTokens.add(sourceCode.getFirstToken(node)); + } + + const ignoredNodeListeners = options.ignoredNodes.reduce( + (listeners, ignoredSelector) => Object.assign(listeners, { [ignoredSelector]: addToIgnoredNodes }), + {} + ); + + /* + * Join the listeners, and add a listener to verify that all tokens actually have the correct indentation + * at the end. + * + * Using Object.assign will cause some offset listeners to be overwritten if the same selector also appears + * in `ignoredNodeListeners`. This isn't a problem because all of the matching nodes will be ignored, + * so those listeners wouldn't be called anyway. + */ + return Object.assign( + offsetListeners, + ignoredNodeListeners, + { + "*:exit"(node) { + + // If a node's type is nonstandard, we can't tell how its children should be offset, so ignore it. + if (!KNOWN_NODES.has(node.type)) { + addToIgnoredNodes(node); + } + }, + "Program:exit"() { + + // If ignoreComments option is enabled, ignore all comment tokens. + if (options.ignoreComments) { + sourceCode.getAllComments() + .forEach(comment => offsets.ignoreToken(comment)); + } + + // Invoke the queued offset listeners for the nodes that aren't ignored. + for (let i = 0; i < listenerCallQueue.length; i++) { + const nodeInfo = listenerCallQueue[i]; + + if (!ignoredNodes.has(nodeInfo.node)) { + nodeInfo.listener(nodeInfo.node); + } + } + + // Update the offsets for ignored nodes to prevent their child tokens from being reported. + ignoredNodes.forEach(ignoreNode); + + addParensIndent(sourceCode.ast.tokens); + + /* + * Create a Map from (tokenOrComment) => (precedingToken). + * This is necessary because sourceCode.getTokenBefore does not handle a comment as an argument correctly. + */ + const precedingTokens = new WeakMap(); + + for (let i = 0; i < sourceCode.ast.comments.length; i++) { + const comment = sourceCode.ast.comments[i]; + + const tokenOrCommentBefore = sourceCode.getTokenBefore(comment, { includeComments: true }); + const hasToken = precedingTokens.has(tokenOrCommentBefore) ? precedingTokens.get(tokenOrCommentBefore) : tokenOrCommentBefore; + + precedingTokens.set(comment, hasToken); + } + + for (let i = 1; i < sourceCode.lines.length + 1; i++) { + + if (!tokenInfo.firstTokensByLineNumber.has(i)) { + + // Don't check indentation on blank lines + continue; + } + + const firstTokenOfLine = tokenInfo.firstTokensByLineNumber.get(i); + + if (firstTokenOfLine.loc.start.line !== i) { + + // Don't check the indentation of multi-line tokens (e.g. template literals or block comments) twice. + continue; + } + + if (astUtils.isCommentToken(firstTokenOfLine)) { + const tokenBefore = precedingTokens.get(firstTokenOfLine); + const tokenAfter = tokenBefore ? sourceCode.getTokenAfter(tokenBefore) : sourceCode.ast.tokens[0]; + const mayAlignWithBefore = tokenBefore && !hasBlankLinesBetween(tokenBefore, firstTokenOfLine); + const mayAlignWithAfter = tokenAfter && !hasBlankLinesBetween(firstTokenOfLine, tokenAfter); + + /* + * If a comment precedes a line that begins with a semicolon token, align to that token, i.e. + * + * let foo + * // comment + * ;(async () => {})() + */ + if (tokenAfter && astUtils.isSemicolonToken(tokenAfter) && !astUtils.isTokenOnSameLine(firstTokenOfLine, tokenAfter)) { + offsets.setDesiredOffset(firstTokenOfLine, tokenAfter, 0); + } + + // If a comment matches the expected indentation of the token immediately before or after, don't report it. + if ( + mayAlignWithBefore && validateTokenIndent(firstTokenOfLine, offsets.getDesiredIndent(tokenBefore)) || + mayAlignWithAfter && validateTokenIndent(firstTokenOfLine, offsets.getDesiredIndent(tokenAfter)) + ) { + continue; + } + } + + // If the token matches the expected indentation, don't report it. + if (validateTokenIndent(firstTokenOfLine, offsets.getDesiredIndent(firstTokenOfLine))) { + continue; + } + + // Otherwise, report the token/comment. + report(firstTokenOfLine, offsets.getDesiredIndent(firstTokenOfLine)); + } + } + } + ); + } }; diff --git a/node_modules/eslint/lib/rules/index.js b/node_modules/eslint/lib/rules/index.js index a750e0872..840abe73b 100644 --- a/node_modules/eslint/lib/rules/index.js +++ b/node_modules/eslint/lib/rules/index.js @@ -10,323 +10,297 @@ const { LazyLoadingRuleMap } = require("./utils/lazy-loading-rule-map"); -/** @type {Map} */ -module.exports = new LazyLoadingRuleMap( - Object.entries({ - "accessor-pairs": () => require("./accessor-pairs"), - "array-bracket-newline": () => require("./array-bracket-newline"), - "array-bracket-spacing": () => require("./array-bracket-spacing"), - "array-callback-return": () => require("./array-callback-return"), - "array-element-newline": () => require("./array-element-newline"), - "arrow-body-style": () => require("./arrow-body-style"), - "arrow-parens": () => require("./arrow-parens"), - "arrow-spacing": () => require("./arrow-spacing"), - "block-scoped-var": () => require("./block-scoped-var"), - "block-spacing": () => require("./block-spacing"), - "brace-style": () => require("./brace-style"), - "callback-return": () => require("./callback-return"), - camelcase: () => require("./camelcase"), - "capitalized-comments": () => require("./capitalized-comments"), - "class-methods-use-this": () => require("./class-methods-use-this"), - "comma-dangle": () => require("./comma-dangle"), - "comma-spacing": () => require("./comma-spacing"), - "comma-style": () => require("./comma-style"), - complexity: () => require("./complexity"), - "computed-property-spacing": () => - require("./computed-property-spacing"), - "consistent-return": () => require("./consistent-return"), - "consistent-this": () => require("./consistent-this"), - "constructor-super": () => require("./constructor-super"), - curly: () => require("./curly"), - "default-case": () => require("./default-case"), - "default-case-last": () => require("./default-case-last"), - "default-param-last": () => require("./default-param-last"), - "dot-location": () => require("./dot-location"), - "dot-notation": () => require("./dot-notation"), - "eol-last": () => require("./eol-last"), - eqeqeq: () => require("./eqeqeq"), - "for-direction": () => require("./for-direction"), - "func-call-spacing": () => require("./func-call-spacing"), - "func-name-matching": () => require("./func-name-matching"), - "func-names": () => require("./func-names"), - "func-style": () => require("./func-style"), - "function-call-argument-newline": () => - require("./function-call-argument-newline"), - "function-paren-newline": () => require("./function-paren-newline"), - "generator-star-spacing": () => require("./generator-star-spacing"), - "getter-return": () => require("./getter-return"), - "global-require": () => require("./global-require"), - "grouped-accessor-pairs": () => require("./grouped-accessor-pairs"), - "guard-for-in": () => require("./guard-for-in"), - "handle-callback-err": () => require("./handle-callback-err"), - "id-blacklist": () => require("./id-blacklist"), - "id-denylist": () => require("./id-denylist"), - "id-length": () => require("./id-length"), - "id-match": () => require("./id-match"), - "implicit-arrow-linebreak": () => require("./implicit-arrow-linebreak"), - indent: () => require("./indent"), - "indent-legacy": () => require("./indent-legacy"), - "init-declarations": () => require("./init-declarations"), - "jsx-quotes": () => require("./jsx-quotes"), - "key-spacing": () => require("./key-spacing"), - "keyword-spacing": () => require("./keyword-spacing"), - "line-comment-position": () => require("./line-comment-position"), - "linebreak-style": () => require("./linebreak-style"), - "lines-around-comment": () => require("./lines-around-comment"), - "lines-around-directive": () => require("./lines-around-directive"), - "lines-between-class-members": () => - require("./lines-between-class-members"), - "logical-assignment-operators": () => - require("./logical-assignment-operators"), - "max-classes-per-file": () => require("./max-classes-per-file"), - "max-depth": () => require("./max-depth"), - "max-len": () => require("./max-len"), - "max-lines": () => require("./max-lines"), - "max-lines-per-function": () => require("./max-lines-per-function"), - "max-nested-callbacks": () => require("./max-nested-callbacks"), - "max-params": () => require("./max-params"), - "max-statements": () => require("./max-statements"), - "max-statements-per-line": () => require("./max-statements-per-line"), - "multiline-comment-style": () => require("./multiline-comment-style"), - "multiline-ternary": () => require("./multiline-ternary"), - "new-cap": () => require("./new-cap"), - "new-parens": () => require("./new-parens"), - "newline-after-var": () => require("./newline-after-var"), - "newline-before-return": () => require("./newline-before-return"), - "newline-per-chained-call": () => require("./newline-per-chained-call"), - "no-alert": () => require("./no-alert"), - "no-array-constructor": () => require("./no-array-constructor"), - "no-async-promise-executor": () => - require("./no-async-promise-executor"), - "no-await-in-loop": () => require("./no-await-in-loop"), - "no-bitwise": () => require("./no-bitwise"), - "no-buffer-constructor": () => require("./no-buffer-constructor"), - "no-caller": () => require("./no-caller"), - "no-case-declarations": () => require("./no-case-declarations"), - "no-catch-shadow": () => require("./no-catch-shadow"), - "no-class-assign": () => require("./no-class-assign"), - "no-compare-neg-zero": () => require("./no-compare-neg-zero"), - "no-cond-assign": () => require("./no-cond-assign"), - "no-confusing-arrow": () => require("./no-confusing-arrow"), - "no-console": () => require("./no-console"), - "no-const-assign": () => require("./no-const-assign"), - "no-constant-binary-expression": () => - require("./no-constant-binary-expression"), - "no-constant-condition": () => require("./no-constant-condition"), - "no-constructor-return": () => require("./no-constructor-return"), - "no-continue": () => require("./no-continue"), - "no-control-regex": () => require("./no-control-regex"), - "no-debugger": () => require("./no-debugger"), - "no-delete-var": () => require("./no-delete-var"), - "no-div-regex": () => require("./no-div-regex"), - "no-dupe-args": () => require("./no-dupe-args"), - "no-dupe-class-members": () => require("./no-dupe-class-members"), - "no-dupe-else-if": () => require("./no-dupe-else-if"), - "no-dupe-keys": () => require("./no-dupe-keys"), - "no-duplicate-case": () => require("./no-duplicate-case"), - "no-duplicate-imports": () => require("./no-duplicate-imports"), - "no-else-return": () => require("./no-else-return"), - "no-empty": () => require("./no-empty"), - "no-empty-character-class": () => require("./no-empty-character-class"), - "no-empty-function": () => require("./no-empty-function"), - "no-empty-pattern": () => require("./no-empty-pattern"), - "no-empty-static-block": () => require("./no-empty-static-block"), - "no-eq-null": () => require("./no-eq-null"), - "no-eval": () => require("./no-eval"), - "no-ex-assign": () => require("./no-ex-assign"), - "no-extend-native": () => require("./no-extend-native"), - "no-extra-bind": () => require("./no-extra-bind"), - "no-extra-boolean-cast": () => require("./no-extra-boolean-cast"), - "no-extra-label": () => require("./no-extra-label"), - "no-extra-parens": () => require("./no-extra-parens"), - "no-extra-semi": () => require("./no-extra-semi"), - "no-fallthrough": () => require("./no-fallthrough"), - "no-floating-decimal": () => require("./no-floating-decimal"), - "no-func-assign": () => require("./no-func-assign"), - "no-global-assign": () => require("./no-global-assign"), - "no-implicit-coercion": () => require("./no-implicit-coercion"), - "no-implicit-globals": () => require("./no-implicit-globals"), - "no-implied-eval": () => require("./no-implied-eval"), - "no-import-assign": () => require("./no-import-assign"), - "no-inline-comments": () => require("./no-inline-comments"), - "no-inner-declarations": () => require("./no-inner-declarations"), - "no-invalid-regexp": () => require("./no-invalid-regexp"), - "no-invalid-this": () => require("./no-invalid-this"), - "no-irregular-whitespace": () => require("./no-irregular-whitespace"), - "no-iterator": () => require("./no-iterator"), - "no-label-var": () => require("./no-label-var"), - "no-labels": () => require("./no-labels"), - "no-lone-blocks": () => require("./no-lone-blocks"), - "no-lonely-if": () => require("./no-lonely-if"), - "no-loop-func": () => require("./no-loop-func"), - "no-loss-of-precision": () => require("./no-loss-of-precision"), - "no-magic-numbers": () => require("./no-magic-numbers"), - "no-misleading-character-class": () => - require("./no-misleading-character-class"), - "no-mixed-operators": () => require("./no-mixed-operators"), - "no-mixed-requires": () => require("./no-mixed-requires"), - "no-mixed-spaces-and-tabs": () => require("./no-mixed-spaces-and-tabs"), - "no-multi-assign": () => require("./no-multi-assign"), - "no-multi-spaces": () => require("./no-multi-spaces"), - "no-multi-str": () => require("./no-multi-str"), - "no-multiple-empty-lines": () => require("./no-multiple-empty-lines"), - "no-native-reassign": () => require("./no-native-reassign"), - "no-negated-condition": () => require("./no-negated-condition"), - "no-negated-in-lhs": () => require("./no-negated-in-lhs"), - "no-nested-ternary": () => require("./no-nested-ternary"), - "no-new": () => require("./no-new"), - "no-new-func": () => require("./no-new-func"), - "no-new-native-nonconstructor": () => - require("./no-new-native-nonconstructor"), - "no-new-object": () => require("./no-new-object"), - "no-new-require": () => require("./no-new-require"), - "no-new-symbol": () => require("./no-new-symbol"), - "no-new-wrappers": () => require("./no-new-wrappers"), - "no-nonoctal-decimal-escape": () => - require("./no-nonoctal-decimal-escape"), - "no-obj-calls": () => require("./no-obj-calls"), - "no-object-constructor": () => require("./no-object-constructor"), - "no-octal": () => require("./no-octal"), - "no-octal-escape": () => require("./no-octal-escape"), - "no-param-reassign": () => require("./no-param-reassign"), - "no-path-concat": () => require("./no-path-concat"), - "no-plusplus": () => require("./no-plusplus"), - "no-process-env": () => require("./no-process-env"), - "no-process-exit": () => require("./no-process-exit"), - "no-promise-executor-return": () => - require("./no-promise-executor-return"), - "no-proto": () => require("./no-proto"), - "no-prototype-builtins": () => require("./no-prototype-builtins"), - "no-redeclare": () => require("./no-redeclare"), - "no-regex-spaces": () => require("./no-regex-spaces"), - "no-restricted-exports": () => require("./no-restricted-exports"), - "no-restricted-globals": () => require("./no-restricted-globals"), - "no-restricted-imports": () => require("./no-restricted-imports"), - "no-restricted-modules": () => require("./no-restricted-modules"), - "no-restricted-properties": () => require("./no-restricted-properties"), - "no-restricted-syntax": () => require("./no-restricted-syntax"), - "no-return-assign": () => require("./no-return-assign"), - "no-return-await": () => require("./no-return-await"), - "no-script-url": () => require("./no-script-url"), - "no-self-assign": () => require("./no-self-assign"), - "no-self-compare": () => require("./no-self-compare"), - "no-sequences": () => require("./no-sequences"), - "no-setter-return": () => require("./no-setter-return"), - "no-shadow": () => require("./no-shadow"), - "no-shadow-restricted-names": () => - require("./no-shadow-restricted-names"), - "no-spaced-func": () => require("./no-spaced-func"), - "no-sparse-arrays": () => require("./no-sparse-arrays"), - "no-sync": () => require("./no-sync"), - "no-tabs": () => require("./no-tabs"), - "no-template-curly-in-string": () => - require("./no-template-curly-in-string"), - "no-ternary": () => require("./no-ternary"), - "no-this-before-super": () => require("./no-this-before-super"), - "no-throw-literal": () => require("./no-throw-literal"), - "no-trailing-spaces": () => require("./no-trailing-spaces"), - "no-unassigned-vars": () => require("./no-unassigned-vars"), - "no-undef": () => require("./no-undef"), - "no-undef-init": () => require("./no-undef-init"), - "no-undefined": () => require("./no-undefined"), - "no-underscore-dangle": () => require("./no-underscore-dangle"), - "no-unexpected-multiline": () => require("./no-unexpected-multiline"), - "no-unmodified-loop-condition": () => - require("./no-unmodified-loop-condition"), - "no-unneeded-ternary": () => require("./no-unneeded-ternary"), - "no-unreachable": () => require("./no-unreachable"), - "no-unreachable-loop": () => require("./no-unreachable-loop"), - "no-unsafe-finally": () => require("./no-unsafe-finally"), - "no-unsafe-negation": () => require("./no-unsafe-negation"), - "no-unsafe-optional-chaining": () => - require("./no-unsafe-optional-chaining"), - "no-unused-expressions": () => require("./no-unused-expressions"), - "no-unused-labels": () => require("./no-unused-labels"), - "no-unused-private-class-members": () => - require("./no-unused-private-class-members"), - "no-unused-vars": () => require("./no-unused-vars"), - "no-use-before-define": () => require("./no-use-before-define"), - "no-useless-assignment": () => require("./no-useless-assignment"), - "no-useless-backreference": () => require("./no-useless-backreference"), - "no-useless-call": () => require("./no-useless-call"), - "no-useless-catch": () => require("./no-useless-catch"), - "no-useless-computed-key": () => require("./no-useless-computed-key"), - "no-useless-concat": () => require("./no-useless-concat"), - "no-useless-constructor": () => require("./no-useless-constructor"), - "no-useless-escape": () => require("./no-useless-escape"), - "no-useless-rename": () => require("./no-useless-rename"), - "no-useless-return": () => require("./no-useless-return"), - "no-var": () => require("./no-var"), - "no-void": () => require("./no-void"), - "no-warning-comments": () => require("./no-warning-comments"), - "no-whitespace-before-property": () => - require("./no-whitespace-before-property"), - "no-with": () => require("./no-with"), - "nonblock-statement-body-position": () => - require("./nonblock-statement-body-position"), - "object-curly-newline": () => require("./object-curly-newline"), - "object-curly-spacing": () => require("./object-curly-spacing"), - "object-property-newline": () => require("./object-property-newline"), - "object-shorthand": () => require("./object-shorthand"), - "one-var": () => require("./one-var"), - "one-var-declaration-per-line": () => - require("./one-var-declaration-per-line"), - "operator-assignment": () => require("./operator-assignment"), - "operator-linebreak": () => require("./operator-linebreak"), - "padded-blocks": () => require("./padded-blocks"), - "padding-line-between-statements": () => - require("./padding-line-between-statements"), - "prefer-arrow-callback": () => require("./prefer-arrow-callback"), - "prefer-const": () => require("./prefer-const"), - "prefer-destructuring": () => require("./prefer-destructuring"), - "prefer-exponentiation-operator": () => - require("./prefer-exponentiation-operator"), - "prefer-named-capture-group": () => - require("./prefer-named-capture-group"), - "prefer-numeric-literals": () => require("./prefer-numeric-literals"), - "prefer-object-has-own": () => require("./prefer-object-has-own"), - "prefer-object-spread": () => require("./prefer-object-spread"), - "prefer-promise-reject-errors": () => - require("./prefer-promise-reject-errors"), - "prefer-reflect": () => require("./prefer-reflect"), - "prefer-regex-literals": () => require("./prefer-regex-literals"), - "prefer-rest-params": () => require("./prefer-rest-params"), - "prefer-spread": () => require("./prefer-spread"), - "prefer-template": () => require("./prefer-template"), - "preserve-caught-error": () => require("./preserve-caught-error"), - "quote-props": () => require("./quote-props"), - quotes: () => require("./quotes"), - radix: () => require("./radix"), - "require-atomic-updates": () => require("./require-atomic-updates"), - "require-await": () => require("./require-await"), - "require-unicode-regexp": () => require("./require-unicode-regexp"), - "require-yield": () => require("./require-yield"), - "rest-spread-spacing": () => require("./rest-spread-spacing"), - semi: () => require("./semi"), - "semi-spacing": () => require("./semi-spacing"), - "semi-style": () => require("./semi-style"), - "sort-imports": () => require("./sort-imports"), - "sort-keys": () => require("./sort-keys"), - "sort-vars": () => require("./sort-vars"), - "space-before-blocks": () => require("./space-before-blocks"), - "space-before-function-paren": () => - require("./space-before-function-paren"), - "space-in-parens": () => require("./space-in-parens"), - "space-infix-ops": () => require("./space-infix-ops"), - "space-unary-ops": () => require("./space-unary-ops"), - "spaced-comment": () => require("./spaced-comment"), - strict: () => require("./strict"), - "switch-colon-spacing": () => require("./switch-colon-spacing"), - "symbol-description": () => require("./symbol-description"), - "template-curly-spacing": () => require("./template-curly-spacing"), - "template-tag-spacing": () => require("./template-tag-spacing"), - "unicode-bom": () => require("./unicode-bom"), - "use-isnan": () => require("./use-isnan"), - "valid-typeof": () => require("./valid-typeof"), - "vars-on-top": () => require("./vars-on-top"), - "wrap-iife": () => require("./wrap-iife"), - "wrap-regex": () => require("./wrap-regex"), - "yield-star-spacing": () => require("./yield-star-spacing"), - yoda: () => require("./yoda"), - }), -); +/** @type {Map} */ +module.exports = new LazyLoadingRuleMap(Object.entries({ + "accessor-pairs": () => require("./accessor-pairs"), + "array-bracket-newline": () => require("./array-bracket-newline"), + "array-bracket-spacing": () => require("./array-bracket-spacing"), + "array-callback-return": () => require("./array-callback-return"), + "array-element-newline": () => require("./array-element-newline"), + "arrow-body-style": () => require("./arrow-body-style"), + "arrow-parens": () => require("./arrow-parens"), + "arrow-spacing": () => require("./arrow-spacing"), + "block-scoped-var": () => require("./block-scoped-var"), + "block-spacing": () => require("./block-spacing"), + "brace-style": () => require("./brace-style"), + "callback-return": () => require("./callback-return"), + camelcase: () => require("./camelcase"), + "capitalized-comments": () => require("./capitalized-comments"), + "class-methods-use-this": () => require("./class-methods-use-this"), + "comma-dangle": () => require("./comma-dangle"), + "comma-spacing": () => require("./comma-spacing"), + "comma-style": () => require("./comma-style"), + complexity: () => require("./complexity"), + "computed-property-spacing": () => require("./computed-property-spacing"), + "consistent-return": () => require("./consistent-return"), + "consistent-this": () => require("./consistent-this"), + "constructor-super": () => require("./constructor-super"), + curly: () => require("./curly"), + "default-case": () => require("./default-case"), + "default-case-last": () => require("./default-case-last"), + "default-param-last": () => require("./default-param-last"), + "dot-location": () => require("./dot-location"), + "dot-notation": () => require("./dot-notation"), + "eol-last": () => require("./eol-last"), + eqeqeq: () => require("./eqeqeq"), + "for-direction": () => require("./for-direction"), + "func-call-spacing": () => require("./func-call-spacing"), + "func-name-matching": () => require("./func-name-matching"), + "func-names": () => require("./func-names"), + "func-style": () => require("./func-style"), + "function-call-argument-newline": () => require("./function-call-argument-newline"), + "function-paren-newline": () => require("./function-paren-newline"), + "generator-star-spacing": () => require("./generator-star-spacing"), + "getter-return": () => require("./getter-return"), + "global-require": () => require("./global-require"), + "grouped-accessor-pairs": () => require("./grouped-accessor-pairs"), + "guard-for-in": () => require("./guard-for-in"), + "handle-callback-err": () => require("./handle-callback-err"), + "id-blacklist": () => require("./id-blacklist"), + "id-denylist": () => require("./id-denylist"), + "id-length": () => require("./id-length"), + "id-match": () => require("./id-match"), + "implicit-arrow-linebreak": () => require("./implicit-arrow-linebreak"), + indent: () => require("./indent"), + "indent-legacy": () => require("./indent-legacy"), + "init-declarations": () => require("./init-declarations"), + "jsx-quotes": () => require("./jsx-quotes"), + "key-spacing": () => require("./key-spacing"), + "keyword-spacing": () => require("./keyword-spacing"), + "line-comment-position": () => require("./line-comment-position"), + "linebreak-style": () => require("./linebreak-style"), + "lines-around-comment": () => require("./lines-around-comment"), + "lines-around-directive": () => require("./lines-around-directive"), + "lines-between-class-members": () => require("./lines-between-class-members"), + "logical-assignment-operators": () => require("./logical-assignment-operators"), + "max-classes-per-file": () => require("./max-classes-per-file"), + "max-depth": () => require("./max-depth"), + "max-len": () => require("./max-len"), + "max-lines": () => require("./max-lines"), + "max-lines-per-function": () => require("./max-lines-per-function"), + "max-nested-callbacks": () => require("./max-nested-callbacks"), + "max-params": () => require("./max-params"), + "max-statements": () => require("./max-statements"), + "max-statements-per-line": () => require("./max-statements-per-line"), + "multiline-comment-style": () => require("./multiline-comment-style"), + "multiline-ternary": () => require("./multiline-ternary"), + "new-cap": () => require("./new-cap"), + "new-parens": () => require("./new-parens"), + "newline-after-var": () => require("./newline-after-var"), + "newline-before-return": () => require("./newline-before-return"), + "newline-per-chained-call": () => require("./newline-per-chained-call"), + "no-alert": () => require("./no-alert"), + "no-array-constructor": () => require("./no-array-constructor"), + "no-async-promise-executor": () => require("./no-async-promise-executor"), + "no-await-in-loop": () => require("./no-await-in-loop"), + "no-bitwise": () => require("./no-bitwise"), + "no-buffer-constructor": () => require("./no-buffer-constructor"), + "no-caller": () => require("./no-caller"), + "no-case-declarations": () => require("./no-case-declarations"), + "no-catch-shadow": () => require("./no-catch-shadow"), + "no-class-assign": () => require("./no-class-assign"), + "no-compare-neg-zero": () => require("./no-compare-neg-zero"), + "no-cond-assign": () => require("./no-cond-assign"), + "no-confusing-arrow": () => require("./no-confusing-arrow"), + "no-console": () => require("./no-console"), + "no-const-assign": () => require("./no-const-assign"), + "no-constant-binary-expression": () => require("./no-constant-binary-expression"), + "no-constant-condition": () => require("./no-constant-condition"), + "no-constructor-return": () => require("./no-constructor-return"), + "no-continue": () => require("./no-continue"), + "no-control-regex": () => require("./no-control-regex"), + "no-debugger": () => require("./no-debugger"), + "no-delete-var": () => require("./no-delete-var"), + "no-div-regex": () => require("./no-div-regex"), + "no-dupe-args": () => require("./no-dupe-args"), + "no-dupe-class-members": () => require("./no-dupe-class-members"), + "no-dupe-else-if": () => require("./no-dupe-else-if"), + "no-dupe-keys": () => require("./no-dupe-keys"), + "no-duplicate-case": () => require("./no-duplicate-case"), + "no-duplicate-imports": () => require("./no-duplicate-imports"), + "no-else-return": () => require("./no-else-return"), + "no-empty": () => require("./no-empty"), + "no-empty-character-class": () => require("./no-empty-character-class"), + "no-empty-function": () => require("./no-empty-function"), + "no-empty-pattern": () => require("./no-empty-pattern"), + "no-empty-static-block": () => require("./no-empty-static-block"), + "no-eq-null": () => require("./no-eq-null"), + "no-eval": () => require("./no-eval"), + "no-ex-assign": () => require("./no-ex-assign"), + "no-extend-native": () => require("./no-extend-native"), + "no-extra-bind": () => require("./no-extra-bind"), + "no-extra-boolean-cast": () => require("./no-extra-boolean-cast"), + "no-extra-label": () => require("./no-extra-label"), + "no-extra-parens": () => require("./no-extra-parens"), + "no-extra-semi": () => require("./no-extra-semi"), + "no-fallthrough": () => require("./no-fallthrough"), + "no-floating-decimal": () => require("./no-floating-decimal"), + "no-func-assign": () => require("./no-func-assign"), + "no-global-assign": () => require("./no-global-assign"), + "no-implicit-coercion": () => require("./no-implicit-coercion"), + "no-implicit-globals": () => require("./no-implicit-globals"), + "no-implied-eval": () => require("./no-implied-eval"), + "no-import-assign": () => require("./no-import-assign"), + "no-inline-comments": () => require("./no-inline-comments"), + "no-inner-declarations": () => require("./no-inner-declarations"), + "no-invalid-regexp": () => require("./no-invalid-regexp"), + "no-invalid-this": () => require("./no-invalid-this"), + "no-irregular-whitespace": () => require("./no-irregular-whitespace"), + "no-iterator": () => require("./no-iterator"), + "no-label-var": () => require("./no-label-var"), + "no-labels": () => require("./no-labels"), + "no-lone-blocks": () => require("./no-lone-blocks"), + "no-lonely-if": () => require("./no-lonely-if"), + "no-loop-func": () => require("./no-loop-func"), + "no-loss-of-precision": () => require("./no-loss-of-precision"), + "no-magic-numbers": () => require("./no-magic-numbers"), + "no-misleading-character-class": () => require("./no-misleading-character-class"), + "no-mixed-operators": () => require("./no-mixed-operators"), + "no-mixed-requires": () => require("./no-mixed-requires"), + "no-mixed-spaces-and-tabs": () => require("./no-mixed-spaces-and-tabs"), + "no-multi-assign": () => require("./no-multi-assign"), + "no-multi-spaces": () => require("./no-multi-spaces"), + "no-multi-str": () => require("./no-multi-str"), + "no-multiple-empty-lines": () => require("./no-multiple-empty-lines"), + "no-native-reassign": () => require("./no-native-reassign"), + "no-negated-condition": () => require("./no-negated-condition"), + "no-negated-in-lhs": () => require("./no-negated-in-lhs"), + "no-nested-ternary": () => require("./no-nested-ternary"), + "no-new": () => require("./no-new"), + "no-new-func": () => require("./no-new-func"), + "no-new-native-nonconstructor": () => require("./no-new-native-nonconstructor"), + "no-new-object": () => require("./no-new-object"), + "no-new-require": () => require("./no-new-require"), + "no-new-symbol": () => require("./no-new-symbol"), + "no-new-wrappers": () => require("./no-new-wrappers"), + "no-nonoctal-decimal-escape": () => require("./no-nonoctal-decimal-escape"), + "no-obj-calls": () => require("./no-obj-calls"), + "no-object-constructor": () => require("./no-object-constructor"), + "no-octal": () => require("./no-octal"), + "no-octal-escape": () => require("./no-octal-escape"), + "no-param-reassign": () => require("./no-param-reassign"), + "no-path-concat": () => require("./no-path-concat"), + "no-plusplus": () => require("./no-plusplus"), + "no-process-env": () => require("./no-process-env"), + "no-process-exit": () => require("./no-process-exit"), + "no-promise-executor-return": () => require("./no-promise-executor-return"), + "no-proto": () => require("./no-proto"), + "no-prototype-builtins": () => require("./no-prototype-builtins"), + "no-redeclare": () => require("./no-redeclare"), + "no-regex-spaces": () => require("./no-regex-spaces"), + "no-restricted-exports": () => require("./no-restricted-exports"), + "no-restricted-globals": () => require("./no-restricted-globals"), + "no-restricted-imports": () => require("./no-restricted-imports"), + "no-restricted-modules": () => require("./no-restricted-modules"), + "no-restricted-properties": () => require("./no-restricted-properties"), + "no-restricted-syntax": () => require("./no-restricted-syntax"), + "no-return-assign": () => require("./no-return-assign"), + "no-return-await": () => require("./no-return-await"), + "no-script-url": () => require("./no-script-url"), + "no-self-assign": () => require("./no-self-assign"), + "no-self-compare": () => require("./no-self-compare"), + "no-sequences": () => require("./no-sequences"), + "no-setter-return": () => require("./no-setter-return"), + "no-shadow": () => require("./no-shadow"), + "no-shadow-restricted-names": () => require("./no-shadow-restricted-names"), + "no-spaced-func": () => require("./no-spaced-func"), + "no-sparse-arrays": () => require("./no-sparse-arrays"), + "no-sync": () => require("./no-sync"), + "no-tabs": () => require("./no-tabs"), + "no-template-curly-in-string": () => require("./no-template-curly-in-string"), + "no-ternary": () => require("./no-ternary"), + "no-this-before-super": () => require("./no-this-before-super"), + "no-throw-literal": () => require("./no-throw-literal"), + "no-trailing-spaces": () => require("./no-trailing-spaces"), + "no-undef": () => require("./no-undef"), + "no-undef-init": () => require("./no-undef-init"), + "no-undefined": () => require("./no-undefined"), + "no-underscore-dangle": () => require("./no-underscore-dangle"), + "no-unexpected-multiline": () => require("./no-unexpected-multiline"), + "no-unmodified-loop-condition": () => require("./no-unmodified-loop-condition"), + "no-unneeded-ternary": () => require("./no-unneeded-ternary"), + "no-unreachable": () => require("./no-unreachable"), + "no-unreachable-loop": () => require("./no-unreachable-loop"), + "no-unsafe-finally": () => require("./no-unsafe-finally"), + "no-unsafe-negation": () => require("./no-unsafe-negation"), + "no-unsafe-optional-chaining": () => require("./no-unsafe-optional-chaining"), + "no-unused-expressions": () => require("./no-unused-expressions"), + "no-unused-labels": () => require("./no-unused-labels"), + "no-unused-private-class-members": () => require("./no-unused-private-class-members"), + "no-unused-vars": () => require("./no-unused-vars"), + "no-use-before-define": () => require("./no-use-before-define"), + "no-useless-backreference": () => require("./no-useless-backreference"), + "no-useless-call": () => require("./no-useless-call"), + "no-useless-catch": () => require("./no-useless-catch"), + "no-useless-computed-key": () => require("./no-useless-computed-key"), + "no-useless-concat": () => require("./no-useless-concat"), + "no-useless-constructor": () => require("./no-useless-constructor"), + "no-useless-escape": () => require("./no-useless-escape"), + "no-useless-rename": () => require("./no-useless-rename"), + "no-useless-return": () => require("./no-useless-return"), + "no-var": () => require("./no-var"), + "no-void": () => require("./no-void"), + "no-warning-comments": () => require("./no-warning-comments"), + "no-whitespace-before-property": () => require("./no-whitespace-before-property"), + "no-with": () => require("./no-with"), + "nonblock-statement-body-position": () => require("./nonblock-statement-body-position"), + "object-curly-newline": () => require("./object-curly-newline"), + "object-curly-spacing": () => require("./object-curly-spacing"), + "object-property-newline": () => require("./object-property-newline"), + "object-shorthand": () => require("./object-shorthand"), + "one-var": () => require("./one-var"), + "one-var-declaration-per-line": () => require("./one-var-declaration-per-line"), + "operator-assignment": () => require("./operator-assignment"), + "operator-linebreak": () => require("./operator-linebreak"), + "padded-blocks": () => require("./padded-blocks"), + "padding-line-between-statements": () => require("./padding-line-between-statements"), + "prefer-arrow-callback": () => require("./prefer-arrow-callback"), + "prefer-const": () => require("./prefer-const"), + "prefer-destructuring": () => require("./prefer-destructuring"), + "prefer-exponentiation-operator": () => require("./prefer-exponentiation-operator"), + "prefer-named-capture-group": () => require("./prefer-named-capture-group"), + "prefer-numeric-literals": () => require("./prefer-numeric-literals"), + "prefer-object-has-own": () => require("./prefer-object-has-own"), + "prefer-object-spread": () => require("./prefer-object-spread"), + "prefer-promise-reject-errors": () => require("./prefer-promise-reject-errors"), + "prefer-reflect": () => require("./prefer-reflect"), + "prefer-regex-literals": () => require("./prefer-regex-literals"), + "prefer-rest-params": () => require("./prefer-rest-params"), + "prefer-spread": () => require("./prefer-spread"), + "prefer-template": () => require("./prefer-template"), + "quote-props": () => require("./quote-props"), + quotes: () => require("./quotes"), + radix: () => require("./radix"), + "require-atomic-updates": () => require("./require-atomic-updates"), + "require-await": () => require("./require-await"), + "require-jsdoc": () => require("./require-jsdoc"), + "require-unicode-regexp": () => require("./require-unicode-regexp"), + "require-yield": () => require("./require-yield"), + "rest-spread-spacing": () => require("./rest-spread-spacing"), + semi: () => require("./semi"), + "semi-spacing": () => require("./semi-spacing"), + "semi-style": () => require("./semi-style"), + "sort-imports": () => require("./sort-imports"), + "sort-keys": () => require("./sort-keys"), + "sort-vars": () => require("./sort-vars"), + "space-before-blocks": () => require("./space-before-blocks"), + "space-before-function-paren": () => require("./space-before-function-paren"), + "space-in-parens": () => require("./space-in-parens"), + "space-infix-ops": () => require("./space-infix-ops"), + "space-unary-ops": () => require("./space-unary-ops"), + "spaced-comment": () => require("./spaced-comment"), + strict: () => require("./strict"), + "switch-colon-spacing": () => require("./switch-colon-spacing"), + "symbol-description": () => require("./symbol-description"), + "template-curly-spacing": () => require("./template-curly-spacing"), + "template-tag-spacing": () => require("./template-tag-spacing"), + "unicode-bom": () => require("./unicode-bom"), + "use-isnan": () => require("./use-isnan"), + "valid-jsdoc": () => require("./valid-jsdoc"), + "valid-typeof": () => require("./valid-typeof"), + "vars-on-top": () => require("./vars-on-top"), + "wrap-iife": () => require("./wrap-iife"), + "wrap-regex": () => require("./wrap-regex"), + "yield-star-spacing": () => require("./yield-star-spacing"), + yoda: () => require("./yoda") +})); diff --git a/node_modules/eslint/lib/rules/init-declarations.js b/node_modules/eslint/lib/rules/init-declarations.js index 15dac379f..3abe107f1 100644 --- a/node_modules/eslint/lib/rules/init-declarations.js +++ b/node_modules/eslint/lib/rules/init-declarations.js @@ -9,19 +9,15 @@ // Helpers //------------------------------------------------------------------------------ -const CONSTANT_BINDINGS = new Set(["const", "using", "await using"]); - /** * Checks whether or not a given node is a for loop. * @param {ASTNode} block A node to check. * @returns {boolean} `true` when the node is a for loop. */ function isForLoop(block) { - return ( - block.type === "ForInStatement" || - block.type === "ForOfStatement" || - block.type === "ForStatement" - ); + return block.type === "ForInStatement" || + block.type === "ForOfStatement" || + block.type === "ForStatement"; } /** @@ -30,143 +26,114 @@ function isForLoop(block) { * @returns {boolean} `true` when the node has its initializer. */ function isInitialized(node) { - const declaration = node.parent; - const block = declaration.parent; - - if (isForLoop(block)) { - if (block.type === "ForStatement") { - return block.init === declaration; - } - return block.left === declaration; - } - return Boolean(node.init); + const declaration = node.parent; + const block = declaration.parent; + + if (isForLoop(block)) { + if (block.type === "ForStatement") { + return block.init === declaration; + } + return block.left === declaration; + } + return Boolean(node.init); } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - dialects: ["typescript", "javascript"], - language: "javascript", - - docs: { - description: - "Require or disallow initialization in variable declarations", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/init-declarations", - }, - - schema: { - anyOf: [ - { - type: "array", - items: [ - { - enum: ["always"], - }, - ], - minItems: 0, - maxItems: 1, - }, - { - type: "array", - items: [ - { - enum: ["never"], - }, - { - type: "object", - properties: { - ignoreForLoopInit: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - minItems: 0, - maxItems: 2, - }, - ], - }, - messages: { - initialized: - "Variable '{{idName}}' should be initialized on declaration.", - notInitialized: - "Variable '{{idName}}' should not be initialized on declaration.", - }, - }, - - create(context) { - const MODE_ALWAYS = "always", - MODE_NEVER = "never"; - - const mode = context.options[0] || MODE_ALWAYS; - const params = context.options[1] || {}; - - // Track whether we're inside a declared namespace - let insideDeclaredNamespace = false; - - //-------------------------------------------------------------------------- - // Public API - //-------------------------------------------------------------------------- - - return { - TSModuleDeclaration(node) { - if (node.declare) { - insideDeclaredNamespace = true; - } - }, - - "TSModuleDeclaration:exit"(node) { - if (node.declare) { - insideDeclaredNamespace = false; - } - }, - - "VariableDeclaration:exit"(node) { - const kind = node.kind, - declarations = node.declarations; - - if (node.declare || insideDeclaredNamespace) { - return; - } - - for (let i = 0; i < declarations.length; ++i) { - const declaration = declarations[i], - id = declaration.id, - initialized = isInitialized(declaration), - isIgnoredForLoop = - params.ignoreForLoopInit && isForLoop(node.parent); - let messageId = ""; - - if (mode === MODE_ALWAYS && !initialized) { - messageId = "initialized"; - } else if ( - mode === MODE_NEVER && - !CONSTANT_BINDINGS.has(kind) && - initialized && - !isIgnoredForLoop - ) { - messageId = "notInitialized"; - } - - if (id.type === "Identifier" && messageId) { - context.report({ - node: declaration, - messageId, - data: { - idName: id.name, - }, - }); - } - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Require or disallow initialization in variable declarations", + recommended: false, + url: "https://eslint.org/docs/latest/rules/init-declarations" + }, + + schema: { + anyOf: [ + { + type: "array", + items: [ + { + enum: ["always"] + } + ], + minItems: 0, + maxItems: 1 + }, + { + type: "array", + items: [ + { + enum: ["never"] + }, + { + type: "object", + properties: { + ignoreForLoopInit: { + type: "boolean" + } + }, + additionalProperties: false + } + ], + minItems: 0, + maxItems: 2 + } + ] + }, + messages: { + initialized: "Variable '{{idName}}' should be initialized on declaration.", + notInitialized: "Variable '{{idName}}' should not be initialized on declaration." + } + }, + + create(context) { + + const MODE_ALWAYS = "always", + MODE_NEVER = "never"; + + const mode = context.options[0] || MODE_ALWAYS; + const params = context.options[1] || {}; + + //-------------------------------------------------------------------------- + // Public API + //-------------------------------------------------------------------------- + + return { + "VariableDeclaration:exit"(node) { + + const kind = node.kind, + declarations = node.declarations; + + for (let i = 0; i < declarations.length; ++i) { + const declaration = declarations[i], + id = declaration.id, + initialized = isInitialized(declaration), + isIgnoredForLoop = params.ignoreForLoopInit && isForLoop(node.parent); + let messageId = ""; + + if (mode === MODE_ALWAYS && !initialized) { + messageId = "initialized"; + } else if (mode === MODE_NEVER && kind !== "const" && initialized && !isIgnoredForLoop) { + messageId = "notInitialized"; + } + + if (id.type === "Identifier" && messageId) { + context.report({ + node: declaration, + messageId, + data: { + idName: id.name + } + }); + } + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/jsx-quotes.js b/node_modules/eslint/lib/rules/jsx-quotes.js index f435d9cbf..3dcd5fa9d 100644 --- a/node_modules/eslint/lib/rules/jsx-quotes.js +++ b/node_modules/eslint/lib/rules/jsx-quotes.js @@ -17,112 +17,82 @@ const astUtils = require("./utils/ast-utils"); //------------------------------------------------------------------------------ const QUOTE_SETTINGS = { - "prefer-double": { - quote: '"', - description: "singlequote", - convert(str) { - return str.replace(/'/gu, '"'); - }, - }, - "prefer-single": { - quote: "'", - description: "doublequote", - convert(str) { - return str.replace(/"/gu, "'"); - }, - }, + "prefer-double": { + quote: "\"", + description: "singlequote", + convert(str) { + return str.replace(/'/gu, "\""); + } + }, + "prefer-single": { + quote: "'", + description: "doublequote", + convert(str) { + return str.replace(/"/gu, "'"); + } + } }; //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "jsx-quotes", - url: "https://eslint.style/rules/jsx-quotes", - }, - }, - ], - }, - type: "layout", + meta: { + deprecated: true, + replacedBy: [], + type: "layout", - docs: { - description: - "Enforce the consistent use of either double or single quotes in JSX attributes", - recommended: false, - url: "https://eslint.org/docs/latest/rules/jsx-quotes", - }, + docs: { + description: "Enforce the consistent use of either double or single quotes in JSX attributes", + recommended: false, + url: "https://eslint.org/docs/latest/rules/jsx-quotes" + }, - fixable: "whitespace", + fixable: "whitespace", - schema: [ - { - enum: ["prefer-single", "prefer-double"], - }, - ], - messages: { - unexpected: "Unexpected usage of {{description}}.", - }, - }, + schema: [ + { + enum: ["prefer-single", "prefer-double"] + } + ], + messages: { + unexpected: "Unexpected usage of {{description}}." + } + }, - create(context) { - const quoteOption = context.options[0] || "prefer-double", - setting = QUOTE_SETTINGS[quoteOption]; + create(context) { + const quoteOption = context.options[0] || "prefer-double", + setting = QUOTE_SETTINGS[quoteOption]; - /** - * Checks if the given string literal node uses the expected quotes - * @param {ASTNode} node A string literal node. - * @returns {boolean} Whether or not the string literal used the expected quotes. - * @public - */ - function usesExpectedQuotes(node) { - return ( - node.value.includes(setting.quote) || - astUtils.isSurroundedBy(node.raw, setting.quote) - ); - } + /** + * Checks if the given string literal node uses the expected quotes + * @param {ASTNode} node A string literal node. + * @returns {boolean} Whether or not the string literal used the expected quotes. + * @public + */ + function usesExpectedQuotes(node) { + return node.value.includes(setting.quote) || astUtils.isSurroundedBy(node.raw, setting.quote); + } - return { - JSXAttribute(node) { - const attributeValue = node.value; + return { + JSXAttribute(node) { + const attributeValue = node.value; - if ( - attributeValue && - astUtils.isStringLiteral(attributeValue) && - !usesExpectedQuotes(attributeValue) - ) { - context.report({ - node: attributeValue, - messageId: "unexpected", - data: { - description: setting.description, - }, - fix(fixer) { - return fixer.replaceText( - attributeValue, - setting.convert(attributeValue.raw), - ); - }, - }); - } - }, - }; - }, + if (attributeValue && astUtils.isStringLiteral(attributeValue) && !usesExpectedQuotes(attributeValue)) { + context.report({ + node: attributeValue, + messageId: "unexpected", + data: { + description: setting.description + }, + fix(fixer) { + return fixer.replaceText(attributeValue, setting.convert(attributeValue.raw)); + } + }); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/key-spacing.js b/node_modules/eslint/lib/rules/key-spacing.js index 6ab192ec6..19fc0167a 100644 --- a/node_modules/eslint/lib/rules/key-spacing.js +++ b/node_modules/eslint/lib/rules/key-spacing.js @@ -19,7 +19,7 @@ const { getGraphemeCount } = require("../shared/string-utils"); * @returns {boolean} True if str contains a line terminator. */ function containsLineTerminator(str) { - return astUtils.LINEBREAK_MATCHER.test(str); + return astUtils.LINEBREAK_MATCHER.test(str); } /** @@ -28,7 +28,7 @@ function containsLineTerminator(str) { * @returns {any} Last element of arr. */ function last(arr) { - return arr.at(-1); + return arr[arr.length - 1]; } /** @@ -37,7 +37,7 @@ function last(arr) { * @returns {boolean} True if the node is a single line. */ function isSingleLine(node) { - return node.loc.end.line === node.loc.start.line; + return (node.loc.end.line === node.loc.start.line); } /** @@ -46,10 +46,10 @@ function isSingleLine(node) { * @returns {boolean} True if all properties is on a single line. */ function isSingleLineProperties(properties) { - const [firstProp] = properties, - lastProp = last(properties); + const [firstProp] = properties, + lastProp = last(properties); - return firstProp.loc.start.line === lastProp.loc.end.line; + return firstProp.loc.start.line === lastProp.loc.end.line; } /** @@ -59,38 +59,37 @@ function isSingleLineProperties(properties) { * @returns {Object} The object with correctly initialized options and values */ function initOptionProperty(toOptions, fromOptions) { - toOptions.mode = fromOptions.mode || "strict"; - - // Set value of beforeColon - if (typeof fromOptions.beforeColon !== "undefined") { - toOptions.beforeColon = +fromOptions.beforeColon; - } else { - toOptions.beforeColon = 0; - } - - // Set value of afterColon - if (typeof fromOptions.afterColon !== "undefined") { - toOptions.afterColon = +fromOptions.afterColon; - } else { - toOptions.afterColon = 1; - } - - // Set align if exists - if (typeof fromOptions.align !== "undefined") { - if (typeof fromOptions.align === "object") { - toOptions.align = fromOptions.align; - } else { - // "string" - toOptions.align = { - on: fromOptions.align, - mode: toOptions.mode, - beforeColon: toOptions.beforeColon, - afterColon: toOptions.afterColon, - }; - } - } - - return toOptions; + toOptions.mode = fromOptions.mode || "strict"; + + // Set value of beforeColon + if (typeof fromOptions.beforeColon !== "undefined") { + toOptions.beforeColon = +fromOptions.beforeColon; + } else { + toOptions.beforeColon = 0; + } + + // Set value of afterColon + if (typeof fromOptions.afterColon !== "undefined") { + toOptions.afterColon = +fromOptions.afterColon; + } else { + toOptions.afterColon = 1; + } + + // Set align if exists + if (typeof fromOptions.align !== "undefined") { + if (typeof fromOptions.align === "object") { + toOptions.align = fromOptions.align; + } else { // "string" + toOptions.align = { + on: fromOptions.align, + mode: toOptions.mode, + beforeColon: toOptions.beforeColon, + afterColon: toOptions.afterColon + }; + } + } + + return toOptions; } /** @@ -100,723 +99,589 @@ function initOptionProperty(toOptions, fromOptions) { * @returns {Object} The object with correctly initialized options and values */ function initOptions(toOptions, fromOptions) { - if (typeof fromOptions.align === "object") { - // Initialize the alignment configuration - toOptions.align = initOptionProperty({}, fromOptions.align); - toOptions.align.on = fromOptions.align.on || "colon"; - toOptions.align.mode = fromOptions.align.mode || "strict"; - - toOptions.multiLine = initOptionProperty( - {}, - fromOptions.multiLine || fromOptions, - ); - toOptions.singleLine = initOptionProperty( - {}, - fromOptions.singleLine || fromOptions, - ); - } else { - // string or undefined - toOptions.multiLine = initOptionProperty( - {}, - fromOptions.multiLine || fromOptions, - ); - toOptions.singleLine = initOptionProperty( - {}, - fromOptions.singleLine || fromOptions, - ); - - // If alignment options are defined in multiLine, pull them out into the general align configuration - if (toOptions.multiLine.align) { - toOptions.align = { - on: toOptions.multiLine.align.on, - mode: - toOptions.multiLine.align.mode || toOptions.multiLine.mode, - beforeColon: toOptions.multiLine.align.beforeColon, - afterColon: toOptions.multiLine.align.afterColon, - }; - } - } - - return toOptions; + if (typeof fromOptions.align === "object") { + + // Initialize the alignment configuration + toOptions.align = initOptionProperty({}, fromOptions.align); + toOptions.align.on = fromOptions.align.on || "colon"; + toOptions.align.mode = fromOptions.align.mode || "strict"; + + toOptions.multiLine = initOptionProperty({}, (fromOptions.multiLine || fromOptions)); + toOptions.singleLine = initOptionProperty({}, (fromOptions.singleLine || fromOptions)); + + } else { // string or undefined + toOptions.multiLine = initOptionProperty({}, (fromOptions.multiLine || fromOptions)); + toOptions.singleLine = initOptionProperty({}, (fromOptions.singleLine || fromOptions)); + + // If alignment options are defined in multiLine, pull them out into the general align configuration + if (toOptions.multiLine.align) { + toOptions.align = { + on: toOptions.multiLine.align.on, + mode: toOptions.multiLine.align.mode || toOptions.multiLine.mode, + beforeColon: toOptions.multiLine.align.beforeColon, + afterColon: toOptions.multiLine.align.afterColon + }; + } + } + + return toOptions; } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "key-spacing", - url: "https://eslint.style/rules/key-spacing", - }, - }, - ], - }, - type: "layout", - - docs: { - description: - "Enforce consistent spacing between keys and values in object literal properties", - recommended: false, - url: "https://eslint.org/docs/latest/rules/key-spacing", - }, - - fixable: "whitespace", - - schema: [ - { - anyOf: [ - { - type: "object", - properties: { - align: { - anyOf: [ - { - enum: ["colon", "value"], - }, - { - type: "object", - properties: { - mode: { - enum: ["strict", "minimum"], - }, - on: { - enum: ["colon", "value"], - }, - beforeColon: { - type: "boolean", - }, - afterColon: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - }, - mode: { - enum: ["strict", "minimum"], - }, - beforeColon: { - type: "boolean", - }, - afterColon: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - { - type: "object", - properties: { - singleLine: { - type: "object", - properties: { - mode: { - enum: ["strict", "minimum"], - }, - beforeColon: { - type: "boolean", - }, - afterColon: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - multiLine: { - type: "object", - properties: { - align: { - anyOf: [ - { - enum: ["colon", "value"], - }, - { - type: "object", - properties: { - mode: { - enum: [ - "strict", - "minimum", - ], - }, - on: { - enum: [ - "colon", - "value", - ], - }, - beforeColon: { - type: "boolean", - }, - afterColon: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - }, - mode: { - enum: ["strict", "minimum"], - }, - beforeColon: { - type: "boolean", - }, - afterColon: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - }, - additionalProperties: false, - }, - { - type: "object", - properties: { - singleLine: { - type: "object", - properties: { - mode: { - enum: ["strict", "minimum"], - }, - beforeColon: { - type: "boolean", - }, - afterColon: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - multiLine: { - type: "object", - properties: { - mode: { - enum: ["strict", "minimum"], - }, - beforeColon: { - type: "boolean", - }, - afterColon: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - align: { - type: "object", - properties: { - mode: { - enum: ["strict", "minimum"], - }, - on: { - enum: ["colon", "value"], - }, - beforeColon: { - type: "boolean", - }, - afterColon: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - }, - additionalProperties: false, - }, - ], - }, - ], - messages: { - extraKey: "Extra space after {{computed}}key '{{key}}'.", - extraValue: - "Extra space before value for {{computed}}key '{{key}}'.", - missingKey: "Missing space after {{computed}}key '{{key}}'.", - missingValue: - "Missing space before value for {{computed}}key '{{key}}'.", - }, - }, - - create(context) { - /** - * OPTIONS - * "key-spacing": [2, { - * beforeColon: false, - * afterColon: true, - * align: "colon" // Optional, or "value" - * } - */ - const options = context.options[0] || {}, - ruleOptions = initOptions({}, options), - multiLineOptions = ruleOptions.multiLine, - singleLineOptions = ruleOptions.singleLine, - alignmentOptions = ruleOptions.align || null; - - const sourceCode = context.sourceCode; - - /** - * Determines if the given property is key-value property. - * @param {ASTNode} property Property node to check. - * @returns {boolean} Whether the property is a key-value property. - */ - function isKeyValueProperty(property) { - return !( - ( - property.method || - property.shorthand || - property.kind !== "init" || - property.type !== "Property" - ) // Could be "ExperimentalSpreadProperty" or "SpreadElement" - ); - } - - /** - * Starting from the given node (a property.key node here) looks forward - * until it finds the colon punctuator and returns it. - * @param {ASTNode} node The node to start looking from. - * @returns {ASTNode} The colon punctuator. - */ - function getNextColon(node) { - return sourceCode.getTokenAfter(node, astUtils.isColonToken); - } - - /** - * Starting from the given node (a property.key node here) looks forward - * until it finds the last token before a colon punctuator and returns it. - * @param {ASTNode} node The node to start looking from. - * @returns {ASTNode} The last token before a colon punctuator. - */ - function getLastTokenBeforeColon(node) { - const colonToken = getNextColon(node); - - return sourceCode.getTokenBefore(colonToken); - } - - /** - * Starting from the given node (a property.key node here) looks forward - * until it finds the first token after a colon punctuator and returns it. - * @param {ASTNode} node The node to start looking from. - * @returns {ASTNode} The first token after a colon punctuator. - */ - function getFirstTokenAfterColon(node) { - const colonToken = getNextColon(node); - - return sourceCode.getTokenAfter(colonToken); - } - - /** - * Checks whether a property is a member of the property group it follows. - * @param {ASTNode} lastMember The last Property known to be in the group. - * @param {ASTNode} candidate The next Property that might be in the group. - * @returns {boolean} True if the candidate property is part of the group. - */ - function continuesPropertyGroup(lastMember, candidate) { - const groupEndLine = lastMember.loc.start.line, - candidateValueStartLine = ( - isKeyValueProperty(candidate) - ? getFirstTokenAfterColon(candidate.key) - : candidate - ).loc.start.line; - - if (candidateValueStartLine - groupEndLine <= 1) { - return true; - } - - /* - * Check that the first comment is adjacent to the end of the group, the - * last comment is adjacent to the candidate property, and that successive - * comments are adjacent to each other. - */ - const leadingComments = sourceCode.getCommentsBefore(candidate); - - if ( - leadingComments.length && - leadingComments[0].loc.start.line - groupEndLine <= 1 && - candidateValueStartLine - last(leadingComments).loc.end.line <= - 1 - ) { - for (let i = 1; i < leadingComments.length; i++) { - if ( - leadingComments[i].loc.start.line - - leadingComments[i - 1].loc.end.line > - 1 - ) { - return false; - } - } - return true; - } - - return false; - } - - /** - * Gets an object literal property's key as the identifier name or string value. - * @param {ASTNode} property Property node whose key to retrieve. - * @returns {string} The property's key. - */ - function getKey(property) { - const key = property.key; - - if (property.computed) { - return sourceCode.getText().slice(key.range[0], key.range[1]); - } - return astUtils.getStaticPropertyName(property); - } - - /** - * Reports an appropriately-formatted error if spacing is incorrect on one - * side of the colon. - * @param {ASTNode} property Key-value pair in an object literal. - * @param {string} side Side being verified - either "key" or "value". - * @param {string} whitespace Actual whitespace string. - * @param {number} expected Expected whitespace length. - * @param {string} mode Value of the mode as "strict" or "minimum" - * @returns {void} - */ - function report(property, side, whitespace, expected, mode) { - const diff = whitespace.length - expected; - - if ( - ((diff && mode === "strict") || - (diff < 0 && mode === "minimum") || - (diff > 0 && !expected && mode === "minimum")) && - !(expected && containsLineTerminator(whitespace)) - ) { - const nextColon = getNextColon(property.key), - tokenBeforeColon = sourceCode.getTokenBefore(nextColon, { - includeComments: true, - }), - tokenAfterColon = sourceCode.getTokenAfter(nextColon, { - includeComments: true, - }), - isKeySide = side === "key", - isExtra = diff > 0, - diffAbs = Math.abs(diff), - spaces = Array(diffAbs + 1).join(" "); - - const locStart = isKeySide - ? tokenBeforeColon.loc.end - : nextColon.loc.start; - const locEnd = isKeySide - ? nextColon.loc.start - : tokenAfterColon.loc.start; - const missingLoc = isKeySide - ? tokenBeforeColon.loc - : tokenAfterColon.loc; - const loc = isExtra - ? { start: locStart, end: locEnd } - : missingLoc; - - let fix; - - if (isExtra) { - let range; - - // Remove whitespace - if (isKeySide) { - range = [ - tokenBeforeColon.range[1], - tokenBeforeColon.range[1] + diffAbs, - ]; - } else { - range = [ - tokenAfterColon.range[0] - diffAbs, - tokenAfterColon.range[0], - ]; - } - fix = function (fixer) { - return fixer.removeRange(range); - }; - } else { - // Add whitespace - if (isKeySide) { - fix = function (fixer) { - return fixer.insertTextAfter( - tokenBeforeColon, - spaces, - ); - }; - } else { - fix = function (fixer) { - return fixer.insertTextBefore( - tokenAfterColon, - spaces, - ); - }; - } - } - - let messageId; - - if (isExtra) { - messageId = side === "key" ? "extraKey" : "extraValue"; - } else { - messageId = side === "key" ? "missingKey" : "missingValue"; - } - - context.report({ - node: property[side], - loc, - messageId, - data: { - computed: property.computed ? "computed " : "", - key: getKey(property), - }, - fix, - }); - } - } - - /** - * Gets the number of characters in a key, including quotes around string - * keys and braces around computed property keys. - * @param {ASTNode} property Property of on object literal. - * @returns {number} Width of the key. - */ - function getKeyWidth(property) { - const startToken = sourceCode.getFirstToken(property); - const endToken = getLastTokenBeforeColon(property.key); - - return getGraphemeCount( - sourceCode - .getText() - .slice(startToken.range[0], endToken.range[1]), - ); - } - - /** - * Gets the whitespace around the colon in an object literal property. - * @param {ASTNode} property Property node from an object literal. - * @returns {Object} Whitespace before and after the property's colon. - */ - function getPropertyWhitespace(property) { - const whitespace = /(\s*):(\s*)/u.exec( - sourceCode - .getText() - .slice(property.key.range[1], property.value.range[0]), - ); - - if (whitespace) { - return { - beforeColon: whitespace[1], - afterColon: whitespace[2], - }; - } - return null; - } - - /** - * Creates groups of properties. - * @param {ASTNode} node ObjectExpression node being evaluated. - * @returns {Array} Groups of property AST node lists. - */ - function createGroups(node) { - if (node.properties.length === 1) { - return [node.properties]; - } - - return node.properties.reduce( - (groups, property) => { - const currentGroup = last(groups), - prev = last(currentGroup); - - if (!prev || continuesPropertyGroup(prev, property)) { - currentGroup.push(property); - } else { - groups.push([property]); - } - - return groups; - }, - [[]], - ); - } - - /** - * Verifies correct vertical alignment of a group of properties. - * @param {ASTNode[]} properties List of Property AST nodes. - * @returns {void} - */ - function verifyGroupAlignment(properties) { - const length = properties.length, - widths = properties.map(getKeyWidth), // Width of keys, including quotes - align = alignmentOptions.on; // "value" or "colon" - let targetWidth = Math.max(...widths), - beforeColon, - afterColon, - mode; - - if (alignmentOptions && length > 1) { - // When aligning values within a group, use the alignment configuration. - beforeColon = alignmentOptions.beforeColon; - afterColon = alignmentOptions.afterColon; - mode = alignmentOptions.mode; - } else { - beforeColon = multiLineOptions.beforeColon; - afterColon = multiLineOptions.afterColon; - mode = alignmentOptions.mode; - } - - // Conditionally include one space before or after colon - targetWidth += align === "colon" ? beforeColon : afterColon; - - for (let i = 0; i < length; i++) { - const property = properties[i]; - const whitespace = getPropertyWhitespace(property); - - if (whitespace) { - // Object literal getters/setters lack a colon - const width = widths[i]; - - if (align === "value") { - report( - property, - "key", - whitespace.beforeColon, - beforeColon, - mode, - ); - report( - property, - "value", - whitespace.afterColon, - targetWidth - width, - mode, - ); - } else { - // align = "colon" - report( - property, - "key", - whitespace.beforeColon, - targetWidth - width, - mode, - ); - report( - property, - "value", - whitespace.afterColon, - afterColon, - mode, - ); - } - } - } - } - - /** - * Verifies spacing of property conforms to specified options. - * @param {ASTNode} node Property node being evaluated. - * @param {Object} lineOptions Configured singleLine or multiLine options - * @returns {void} - */ - function verifySpacing(node, lineOptions) { - const actual = getPropertyWhitespace(node); - - if (actual) { - // Object literal getters/setters lack colons - report( - node, - "key", - actual.beforeColon, - lineOptions.beforeColon, - lineOptions.mode, - ); - report( - node, - "value", - actual.afterColon, - lineOptions.afterColon, - lineOptions.mode, - ); - } - } - - /** - * Verifies spacing of each property in a list. - * @param {ASTNode[]} properties List of Property AST nodes. - * @param {Object} lineOptions Configured singleLine or multiLine options - * @returns {void} - */ - function verifyListSpacing(properties, lineOptions) { - const length = properties.length; - - for (let i = 0; i < length; i++) { - verifySpacing(properties[i], lineOptions); - } - } - - /** - * Verifies vertical alignment, taking into account groups of properties. - * @param {ASTNode} node ObjectExpression node being evaluated. - * @returns {void} - */ - function verifyAlignment(node) { - createGroups(node).forEach(group => { - const properties = group.filter(isKeyValueProperty); - - if ( - properties.length > 0 && - isSingleLineProperties(properties) - ) { - verifyListSpacing(properties, multiLineOptions); - } else { - verifyGroupAlignment(properties); - } - }); - } - - //-------------------------------------------------------------------------- - // Public API - //-------------------------------------------------------------------------- - - if (alignmentOptions) { - // Verify vertical alignment - - return { - ObjectExpression(node) { - if (isSingleLine(node)) { - verifyListSpacing( - node.properties.filter(isKeyValueProperty), - singleLineOptions, - ); - } else { - verifyAlignment(node); - } - }, - }; - } - - // Obey beforeColon and afterColon in each property as configured - return { - Property(node) { - verifySpacing( - node, - isSingleLine(node.parent) - ? singleLineOptions - : multiLineOptions, - ); - }, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Enforce consistent spacing between keys and values in object literal properties", + recommended: false, + url: "https://eslint.org/docs/latest/rules/key-spacing" + }, + + fixable: "whitespace", + + schema: [{ + anyOf: [ + { + type: "object", + properties: { + align: { + anyOf: [ + { + enum: ["colon", "value"] + }, + { + type: "object", + properties: { + mode: { + enum: ["strict", "minimum"] + }, + on: { + enum: ["colon", "value"] + }, + beforeColon: { + type: "boolean" + }, + afterColon: { + type: "boolean" + } + }, + additionalProperties: false + } + ] + }, + mode: { + enum: ["strict", "minimum"] + }, + beforeColon: { + type: "boolean" + }, + afterColon: { + type: "boolean" + } + }, + additionalProperties: false + }, + { + type: "object", + properties: { + singleLine: { + type: "object", + properties: { + mode: { + enum: ["strict", "minimum"] + }, + beforeColon: { + type: "boolean" + }, + afterColon: { + type: "boolean" + } + }, + additionalProperties: false + }, + multiLine: { + type: "object", + properties: { + align: { + anyOf: [ + { + enum: ["colon", "value"] + }, + { + type: "object", + properties: { + mode: { + enum: ["strict", "minimum"] + }, + on: { + enum: ["colon", "value"] + }, + beforeColon: { + type: "boolean" + }, + afterColon: { + type: "boolean" + } + }, + additionalProperties: false + } + ] + }, + mode: { + enum: ["strict", "minimum"] + }, + beforeColon: { + type: "boolean" + }, + afterColon: { + type: "boolean" + } + }, + additionalProperties: false + } + }, + additionalProperties: false + }, + { + type: "object", + properties: { + singleLine: { + type: "object", + properties: { + mode: { + enum: ["strict", "minimum"] + }, + beforeColon: { + type: "boolean" + }, + afterColon: { + type: "boolean" + } + }, + additionalProperties: false + }, + multiLine: { + type: "object", + properties: { + mode: { + enum: ["strict", "minimum"] + }, + beforeColon: { + type: "boolean" + }, + afterColon: { + type: "boolean" + } + }, + additionalProperties: false + }, + align: { + type: "object", + properties: { + mode: { + enum: ["strict", "minimum"] + }, + on: { + enum: ["colon", "value"] + }, + beforeColon: { + type: "boolean" + }, + afterColon: { + type: "boolean" + } + }, + additionalProperties: false + } + }, + additionalProperties: false + } + ] + }], + messages: { + extraKey: "Extra space after {{computed}}key '{{key}}'.", + extraValue: "Extra space before value for {{computed}}key '{{key}}'.", + missingKey: "Missing space after {{computed}}key '{{key}}'.", + missingValue: "Missing space before value for {{computed}}key '{{key}}'." + } + }, + + create(context) { + + /** + * OPTIONS + * "key-spacing": [2, { + * beforeColon: false, + * afterColon: true, + * align: "colon" // Optional, or "value" + * } + */ + const options = context.options[0] || {}, + ruleOptions = initOptions({}, options), + multiLineOptions = ruleOptions.multiLine, + singleLineOptions = ruleOptions.singleLine, + alignmentOptions = ruleOptions.align || null; + + const sourceCode = context.sourceCode; + + /** + * Determines if the given property is key-value property. + * @param {ASTNode} property Property node to check. + * @returns {boolean} Whether the property is a key-value property. + */ + function isKeyValueProperty(property) { + return !( + (property.method || + property.shorthand || + property.kind !== "init" || property.type !== "Property") // Could be "ExperimentalSpreadProperty" or "SpreadElement" + ); + } + + /** + * Starting from the given node (a property.key node here) looks forward + * until it finds the colon punctuator and returns it. + * @param {ASTNode} node The node to start looking from. + * @returns {ASTNode} The colon punctuator. + */ + function getNextColon(node) { + return sourceCode.getTokenAfter(node, astUtils.isColonToken); + } + + /** + * Starting from the given node (a property.key node here) looks forward + * until it finds the last token before a colon punctuator and returns it. + * @param {ASTNode} node The node to start looking from. + * @returns {ASTNode} The last token before a colon punctuator. + */ + function getLastTokenBeforeColon(node) { + const colonToken = getNextColon(node); + + return sourceCode.getTokenBefore(colonToken); + } + + /** + * Starting from the given node (a property.key node here) looks forward + * until it finds the first token after a colon punctuator and returns it. + * @param {ASTNode} node The node to start looking from. + * @returns {ASTNode} The first token after a colon punctuator. + */ + function getFirstTokenAfterColon(node) { + const colonToken = getNextColon(node); + + return sourceCode.getTokenAfter(colonToken); + } + + /** + * Checks whether a property is a member of the property group it follows. + * @param {ASTNode} lastMember The last Property known to be in the group. + * @param {ASTNode} candidate The next Property that might be in the group. + * @returns {boolean} True if the candidate property is part of the group. + */ + function continuesPropertyGroup(lastMember, candidate) { + const groupEndLine = lastMember.loc.start.line, + candidateValueStartLine = (isKeyValueProperty(candidate) ? getFirstTokenAfterColon(candidate.key) : candidate).loc.start.line; + + if (candidateValueStartLine - groupEndLine <= 1) { + return true; + } + + /* + * Check that the first comment is adjacent to the end of the group, the + * last comment is adjacent to the candidate property, and that successive + * comments are adjacent to each other. + */ + const leadingComments = sourceCode.getCommentsBefore(candidate); + + if ( + leadingComments.length && + leadingComments[0].loc.start.line - groupEndLine <= 1 && + candidateValueStartLine - last(leadingComments).loc.end.line <= 1 + ) { + for (let i = 1; i < leadingComments.length; i++) { + if (leadingComments[i].loc.start.line - leadingComments[i - 1].loc.end.line > 1) { + return false; + } + } + return true; + } + + return false; + } + + /** + * Gets an object literal property's key as the identifier name or string value. + * @param {ASTNode} property Property node whose key to retrieve. + * @returns {string} The property's key. + */ + function getKey(property) { + const key = property.key; + + if (property.computed) { + return sourceCode.getText().slice(key.range[0], key.range[1]); + } + return astUtils.getStaticPropertyName(property); + } + + /** + * Reports an appropriately-formatted error if spacing is incorrect on one + * side of the colon. + * @param {ASTNode} property Key-value pair in an object literal. + * @param {string} side Side being verified - either "key" or "value". + * @param {string} whitespace Actual whitespace string. + * @param {int} expected Expected whitespace length. + * @param {string} mode Value of the mode as "strict" or "minimum" + * @returns {void} + */ + function report(property, side, whitespace, expected, mode) { + const diff = whitespace.length - expected; + + if (( + diff && mode === "strict" || + diff < 0 && mode === "minimum" || + diff > 0 && !expected && mode === "minimum") && + !(expected && containsLineTerminator(whitespace)) + ) { + const nextColon = getNextColon(property.key), + tokenBeforeColon = sourceCode.getTokenBefore(nextColon, { includeComments: true }), + tokenAfterColon = sourceCode.getTokenAfter(nextColon, { includeComments: true }), + isKeySide = side === "key", + isExtra = diff > 0, + diffAbs = Math.abs(diff), + spaces = Array(diffAbs + 1).join(" "); + + const locStart = isKeySide ? tokenBeforeColon.loc.end : nextColon.loc.start; + const locEnd = isKeySide ? nextColon.loc.start : tokenAfterColon.loc.start; + const missingLoc = isKeySide ? tokenBeforeColon.loc : tokenAfterColon.loc; + const loc = isExtra ? { start: locStart, end: locEnd } : missingLoc; + + let fix; + + if (isExtra) { + let range; + + // Remove whitespace + if (isKeySide) { + range = [tokenBeforeColon.range[1], tokenBeforeColon.range[1] + diffAbs]; + } else { + range = [tokenAfterColon.range[0] - diffAbs, tokenAfterColon.range[0]]; + } + fix = function(fixer) { + return fixer.removeRange(range); + }; + } else { + + // Add whitespace + if (isKeySide) { + fix = function(fixer) { + return fixer.insertTextAfter(tokenBeforeColon, spaces); + }; + } else { + fix = function(fixer) { + return fixer.insertTextBefore(tokenAfterColon, spaces); + }; + } + } + + let messageId = ""; + + if (isExtra) { + messageId = side === "key" ? "extraKey" : "extraValue"; + } else { + messageId = side === "key" ? "missingKey" : "missingValue"; + } + + context.report({ + node: property[side], + loc, + messageId, + data: { + computed: property.computed ? "computed " : "", + key: getKey(property) + }, + fix + }); + } + } + + /** + * Gets the number of characters in a key, including quotes around string + * keys and braces around computed property keys. + * @param {ASTNode} property Property of on object literal. + * @returns {int} Width of the key. + */ + function getKeyWidth(property) { + const startToken = sourceCode.getFirstToken(property); + const endToken = getLastTokenBeforeColon(property.key); + + return getGraphemeCount(sourceCode.getText().slice(startToken.range[0], endToken.range[1])); + } + + /** + * Gets the whitespace around the colon in an object literal property. + * @param {ASTNode} property Property node from an object literal. + * @returns {Object} Whitespace before and after the property's colon. + */ + function getPropertyWhitespace(property) { + const whitespace = /(\s*):(\s*)/u.exec(sourceCode.getText().slice( + property.key.range[1], property.value.range[0] + )); + + if (whitespace) { + return { + beforeColon: whitespace[1], + afterColon: whitespace[2] + }; + } + return null; + } + + /** + * Creates groups of properties. + * @param {ASTNode} node ObjectExpression node being evaluated. + * @returns {Array} Groups of property AST node lists. + */ + function createGroups(node) { + if (node.properties.length === 1) { + return [node.properties]; + } + + return node.properties.reduce((groups, property) => { + const currentGroup = last(groups), + prev = last(currentGroup); + + if (!prev || continuesPropertyGroup(prev, property)) { + currentGroup.push(property); + } else { + groups.push([property]); + } + + return groups; + }, [ + [] + ]); + } + + /** + * Verifies correct vertical alignment of a group of properties. + * @param {ASTNode[]} properties List of Property AST nodes. + * @returns {void} + */ + function verifyGroupAlignment(properties) { + const length = properties.length, + widths = properties.map(getKeyWidth), // Width of keys, including quotes + align = alignmentOptions.on; // "value" or "colon" + let targetWidth = Math.max(...widths), + beforeColon, afterColon, mode; + + if (alignmentOptions && length > 1) { // When aligning values within a group, use the alignment configuration. + beforeColon = alignmentOptions.beforeColon; + afterColon = alignmentOptions.afterColon; + mode = alignmentOptions.mode; + } else { + beforeColon = multiLineOptions.beforeColon; + afterColon = multiLineOptions.afterColon; + mode = alignmentOptions.mode; + } + + // Conditionally include one space before or after colon + targetWidth += (align === "colon" ? beforeColon : afterColon); + + for (let i = 0; i < length; i++) { + const property = properties[i]; + const whitespace = getPropertyWhitespace(property); + + if (whitespace) { // Object literal getters/setters lack a colon + const width = widths[i]; + + if (align === "value") { + report(property, "key", whitespace.beforeColon, beforeColon, mode); + report(property, "value", whitespace.afterColon, targetWidth - width, mode); + } else { // align = "colon" + report(property, "key", whitespace.beforeColon, targetWidth - width, mode); + report(property, "value", whitespace.afterColon, afterColon, mode); + } + } + } + } + + /** + * Verifies spacing of property conforms to specified options. + * @param {ASTNode} node Property node being evaluated. + * @param {Object} lineOptions Configured singleLine or multiLine options + * @returns {void} + */ + function verifySpacing(node, lineOptions) { + const actual = getPropertyWhitespace(node); + + if (actual) { // Object literal getters/setters lack colons + report(node, "key", actual.beforeColon, lineOptions.beforeColon, lineOptions.mode); + report(node, "value", actual.afterColon, lineOptions.afterColon, lineOptions.mode); + } + } + + /** + * Verifies spacing of each property in a list. + * @param {ASTNode[]} properties List of Property AST nodes. + * @param {Object} lineOptions Configured singleLine or multiLine options + * @returns {void} + */ + function verifyListSpacing(properties, lineOptions) { + const length = properties.length; + + for (let i = 0; i < length; i++) { + verifySpacing(properties[i], lineOptions); + } + } + + /** + * Verifies vertical alignment, taking into account groups of properties. + * @param {ASTNode} node ObjectExpression node being evaluated. + * @returns {void} + */ + function verifyAlignment(node) { + createGroups(node).forEach(group => { + const properties = group.filter(isKeyValueProperty); + + if (properties.length > 0 && isSingleLineProperties(properties)) { + verifyListSpacing(properties, multiLineOptions); + } else { + verifyGroupAlignment(properties); + } + }); + } + + //-------------------------------------------------------------------------- + // Public API + //-------------------------------------------------------------------------- + + if (alignmentOptions) { // Verify vertical alignment + + return { + ObjectExpression(node) { + if (isSingleLine(node)) { + verifyListSpacing(node.properties.filter(isKeyValueProperty), singleLineOptions); + } else { + verifyAlignment(node); + } + } + }; + + } + + // Obey beforeColon and afterColon in each property as configured + return { + Property(node) { + verifySpacing(node, isSingleLine(node.parent) ? singleLineOptions : multiLineOptions); + } + }; + + + } }; diff --git a/node_modules/eslint/lib/rules/keyword-spacing.js b/node_modules/eslint/lib/rules/keyword-spacing.js index 0986b4361..9d18441e0 100644 --- a/node_modules/eslint/lib/rules/keyword-spacing.js +++ b/node_modules/eslint/lib/rules/keyword-spacing.js @@ -11,7 +11,7 @@ //------------------------------------------------------------------------------ const astUtils = require("./utils/ast-utils"), - keywords = require("./utils/keywords"); + keywords = require("./utils/keywords"); //------------------------------------------------------------------------------ // Constants @@ -23,31 +23,18 @@ const PREV_TOKEN_M = /^[)\]}>*]$/u; const NEXT_TOKEN_M = /^[{*]$/u; const TEMPLATE_OPEN_PAREN = /\$\{$/u; const TEMPLATE_CLOSE_PAREN = /^\}/u; -const CHECK_TYPE = - /^(?:JSXElement|RegularExpression|String|Template|PrivateIdentifier)$/u; -const KEYS = keywords.concat([ - "as", - "async", - "await", - "from", - "get", - "let", - "of", - "set", - "yield", -]); +const CHECK_TYPE = /^(?:JSXElement|RegularExpression|String|Template|PrivateIdentifier)$/u; +const KEYS = keywords.concat(["as", "async", "await", "from", "get", "let", "of", "set", "yield"]); // check duplications. -(function () { - KEYS.sort(); - for (let i = 1; i < KEYS.length; ++i) { - if (KEYS[i] === KEYS[i - 1]) { - throw new Error( - `Duplication was found in the keyword list: ${KEYS[i]}`, - ); - } - } -})(); +(function() { + KEYS.sort(); + for (let i = 1; i < KEYS.length; ++i) { + if (KEYS[i] === KEYS[i - 1]) { + throw new Error(`Duplication was found in the keyword list: ${KEYS[i]}`); + } + } +}()); //------------------------------------------------------------------------------ // Helpers @@ -59,7 +46,7 @@ const KEYS = keywords.concat([ * @returns {boolean} `true` if the token is a "Template" token ends with "${". */ function isOpenParenOfTemplate(token) { - return token.type === "Template" && TEMPLATE_OPEN_PAREN.test(token.value); + return token.type === "Template" && TEMPLATE_OPEN_PAREN.test(token.value); } /** @@ -68,634 +55,586 @@ function isOpenParenOfTemplate(token) { * @returns {boolean} `true` if the token is a "Template" token starts with "}". */ function isCloseParenOfTemplate(token) { - return token.type === "Template" && TEMPLATE_CLOSE_PAREN.test(token.value); + return token.type === "Template" && TEMPLATE_CLOSE_PAREN.test(token.value); } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "keyword-spacing", - url: "https://eslint.style/rules/keyword-spacing", - }, - }, - ], - }, - type: "layout", - - docs: { - description: "Enforce consistent spacing before and after keywords", - recommended: false, - url: "https://eslint.org/docs/latest/rules/keyword-spacing", - }, - - fixable: "whitespace", - - schema: [ - { - type: "object", - properties: { - before: { type: "boolean", default: true }, - after: { type: "boolean", default: true }, - overrides: { - type: "object", - properties: KEYS.reduce((retv, key) => { - retv[key] = { - type: "object", - properties: { - before: { type: "boolean" }, - after: { type: "boolean" }, - }, - additionalProperties: false, - }; - return retv; - }, {}), - additionalProperties: false, - }, - }, - additionalProperties: false, - }, - ], - messages: { - expectedBefore: 'Expected space(s) before "{{value}}".', - expectedAfter: 'Expected space(s) after "{{value}}".', - unexpectedBefore: 'Unexpected space(s) before "{{value}}".', - unexpectedAfter: 'Unexpected space(s) after "{{value}}".', - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - const tokensToIgnore = new WeakSet(); - - /** - * Reports a given token if there are not space(s) before the token. - * @param {Token} token A token to report. - * @param {RegExp} pattern A pattern of the previous token to check. - * @returns {void} - */ - function expectSpaceBefore(token, pattern) { - const prevToken = sourceCode.getTokenBefore(token); - - if ( - prevToken && - (CHECK_TYPE.test(prevToken.type) || - pattern.test(prevToken.value)) && - !isOpenParenOfTemplate(prevToken) && - !tokensToIgnore.has(prevToken) && - astUtils.isTokenOnSameLine(prevToken, token) && - !sourceCode.isSpaceBetweenTokens(prevToken, token) - ) { - context.report({ - loc: token.loc, - messageId: "expectedBefore", - data: token, - fix(fixer) { - return fixer.insertTextBefore(token, " "); - }, - }); - } - } - - /** - * Reports a given token if there are space(s) before the token. - * @param {Token} token A token to report. - * @param {RegExp} pattern A pattern of the previous token to check. - * @returns {void} - */ - function unexpectSpaceBefore(token, pattern) { - const prevToken = sourceCode.getTokenBefore(token); - - if ( - prevToken && - (CHECK_TYPE.test(prevToken.type) || - pattern.test(prevToken.value)) && - !isOpenParenOfTemplate(prevToken) && - !tokensToIgnore.has(prevToken) && - astUtils.isTokenOnSameLine(prevToken, token) && - sourceCode.isSpaceBetweenTokens(prevToken, token) - ) { - context.report({ - loc: { start: prevToken.loc.end, end: token.loc.start }, - messageId: "unexpectedBefore", - data: token, - fix(fixer) { - return fixer.removeRange([ - prevToken.range[1], - token.range[0], - ]); - }, - }); - } - } - - /** - * Reports a given token if there are not space(s) after the token. - * @param {Token} token A token to report. - * @param {RegExp} pattern A pattern of the next token to check. - * @returns {void} - */ - function expectSpaceAfter(token, pattern) { - const nextToken = sourceCode.getTokenAfter(token); - - if ( - nextToken && - (CHECK_TYPE.test(nextToken.type) || - pattern.test(nextToken.value)) && - !isCloseParenOfTemplate(nextToken) && - !tokensToIgnore.has(nextToken) && - astUtils.isTokenOnSameLine(token, nextToken) && - !sourceCode.isSpaceBetweenTokens(token, nextToken) - ) { - context.report({ - loc: token.loc, - messageId: "expectedAfter", - data: token, - fix(fixer) { - return fixer.insertTextAfter(token, " "); - }, - }); - } - } - - /** - * Reports a given token if there are space(s) after the token. - * @param {Token} token A token to report. - * @param {RegExp} pattern A pattern of the next token to check. - * @returns {void} - */ - function unexpectSpaceAfter(token, pattern) { - const nextToken = sourceCode.getTokenAfter(token); - - if ( - nextToken && - (CHECK_TYPE.test(nextToken.type) || - pattern.test(nextToken.value)) && - !isCloseParenOfTemplate(nextToken) && - !tokensToIgnore.has(nextToken) && - astUtils.isTokenOnSameLine(token, nextToken) && - sourceCode.isSpaceBetweenTokens(token, nextToken) - ) { - context.report({ - loc: { start: token.loc.end, end: nextToken.loc.start }, - messageId: "unexpectedAfter", - data: token, - fix(fixer) { - return fixer.removeRange([ - token.range[1], - nextToken.range[0], - ]); - }, - }); - } - } - - /** - * Parses the option object and determines check methods for each keyword. - * @param {Object|undefined} options The option object to parse. - * @returns {Object} - Normalized option object. - * Keys are keywords (there are for every keyword). - * Values are instances of `{"before": function, "after": function}`. - */ - function parseOptions(options = {}) { - const before = options.before !== false; - const after = options.after !== false; - const defaultValue = { - before: before ? expectSpaceBefore : unexpectSpaceBefore, - after: after ? expectSpaceAfter : unexpectSpaceAfter, - }; - const overrides = (options && options.overrides) || {}; - const retv = Object.create(null); - - for (let i = 0; i < KEYS.length; ++i) { - const key = KEYS[i]; - const override = overrides[key]; - - if (override) { - const thisBefore = - "before" in override ? override.before : before; - const thisAfter = - "after" in override ? override.after : after; - - retv[key] = { - before: thisBefore - ? expectSpaceBefore - : unexpectSpaceBefore, - after: thisAfter - ? expectSpaceAfter - : unexpectSpaceAfter, - }; - } else { - retv[key] = defaultValue; - } - } - - return retv; - } - - const checkMethodMap = parseOptions(context.options[0]); - - /** - * Reports a given token if usage of spacing followed by the token is - * invalid. - * @param {Token} token A token to report. - * @param {RegExp} [pattern] Optional. A pattern of the previous - * token to check. - * @returns {void} - */ - function checkSpacingBefore(token, pattern) { - checkMethodMap[token.value].before(token, pattern || PREV_TOKEN); - } - - /** - * Reports a given token if usage of spacing preceded by the token is - * invalid. - * @param {Token} token A token to report. - * @param {RegExp} [pattern] Optional. A pattern of the next - * token to check. - * @returns {void} - */ - function checkSpacingAfter(token, pattern) { - checkMethodMap[token.value].after(token, pattern || NEXT_TOKEN); - } - - /** - * Reports a given token if usage of spacing around the token is invalid. - * @param {Token} token A token to report. - * @returns {void} - */ - function checkSpacingAround(token) { - checkSpacingBefore(token); - checkSpacingAfter(token); - } - - /** - * Reports the first token of a given node if the first token is a keyword - * and usage of spacing around the token is invalid. - * @param {ASTNode|null} node A node to report. - * @returns {void} - */ - function checkSpacingAroundFirstToken(node) { - const firstToken = node && sourceCode.getFirstToken(node); - - if (firstToken && firstToken.type === "Keyword") { - checkSpacingAround(firstToken); - } - } - - /** - * Reports the first token of a given node if the first token is a keyword - * and usage of spacing followed by the token is invalid. - * - * This is used for unary operators (e.g. `typeof`), `function`, and `super`. - * Other rules are handling usage of spacing preceded by those keywords. - * @param {ASTNode|null} node A node to report. - * @returns {void} - */ - function checkSpacingBeforeFirstToken(node) { - const firstToken = node && sourceCode.getFirstToken(node); - - if (firstToken && firstToken.type === "Keyword") { - checkSpacingBefore(firstToken); - } - } - - /** - * Reports the previous token of a given node if the token is a keyword and - * usage of spacing around the token is invalid. - * @param {ASTNode|null} node A node to report. - * @returns {void} - */ - function checkSpacingAroundTokenBefore(node) { - if (node) { - const token = sourceCode.getTokenBefore( - node, - astUtils.isKeywordToken, - ); - - checkSpacingAround(token); - } - } - - /** - * Reports `async` or `function` keywords of a given node if usage of - * spacing around those keywords is invalid. - * @param {ASTNode} node A node to report. - * @returns {void} - */ - function checkSpacingForFunction(node) { - const firstToken = node && sourceCode.getFirstToken(node); - - if ( - firstToken && - ((firstToken.type === "Keyword" && - firstToken.value === "function") || - firstToken.value === "async") - ) { - checkSpacingBefore(firstToken); - } - } - - /** - * Reports `class` and `extends` keywords of a given node if usage of - * spacing around those keywords is invalid. - * @param {ASTNode} node A node to report. - * @returns {void} - */ - function checkSpacingForClass(node) { - checkSpacingAroundFirstToken(node); - checkSpacingAroundTokenBefore(node.superClass); - } - - /** - * Reports `if` and `else` keywords of a given node if usage of spacing - * around those keywords is invalid. - * @param {ASTNode} node A node to report. - * @returns {void} - */ - function checkSpacingForIfStatement(node) { - checkSpacingAroundFirstToken(node); - checkSpacingAroundTokenBefore(node.alternate); - } - - /** - * Reports `try`, `catch`, and `finally` keywords of a given node if usage - * of spacing around those keywords is invalid. - * @param {ASTNode} node A node to report. - * @returns {void} - */ - function checkSpacingForTryStatement(node) { - checkSpacingAroundFirstToken(node); - checkSpacingAroundFirstToken(node.handler); - checkSpacingAroundTokenBefore(node.finalizer); - } - - /** - * Reports `do` and `while` keywords of a given node if usage of spacing - * around those keywords is invalid. - * @param {ASTNode} node A node to report. - * @returns {void} - */ - function checkSpacingForDoWhileStatement(node) { - checkSpacingAroundFirstToken(node); - checkSpacingAroundTokenBefore(node.test); - } - - /** - * Reports `for` and `in` keywords of a given node if usage of spacing - * around those keywords is invalid. - * @param {ASTNode} node A node to report. - * @returns {void} - */ - function checkSpacingForForInStatement(node) { - checkSpacingAroundFirstToken(node); - - const inToken = sourceCode.getTokenBefore( - node.right, - astUtils.isNotOpeningParenToken, - ); - const previousToken = sourceCode.getTokenBefore(inToken); - - if (previousToken.type !== "PrivateIdentifier") { - checkSpacingBefore(inToken); - } - - checkSpacingAfter(inToken); - } - - /** - * Reports `for` and `of` keywords of a given node if usage of spacing - * around those keywords is invalid. - * @param {ASTNode} node A node to report. - * @returns {void} - */ - function checkSpacingForForOfStatement(node) { - if (node.await) { - checkSpacingBefore(sourceCode.getFirstToken(node, 0)); - checkSpacingAfter(sourceCode.getFirstToken(node, 1)); - } else { - checkSpacingAroundFirstToken(node); - } - - const ofToken = sourceCode.getTokenBefore( - node.right, - astUtils.isNotOpeningParenToken, - ); - const previousToken = sourceCode.getTokenBefore(ofToken); - - if (previousToken.type !== "PrivateIdentifier") { - checkSpacingBefore(ofToken); - } - - checkSpacingAfter(ofToken); - } - - /** - * Reports `import`, `export`, `as`, and `from` keywords of a given node if - * usage of spacing around those keywords is invalid. - * - * This rule handles the `*` token in module declarations. - * - * import*as A from "./a"; /*error Expected space(s) after "import". - * error Expected space(s) before "as". - * @param {ASTNode} node A node to report. - * @returns {void} - */ - function checkSpacingForModuleDeclaration(node) { - const firstToken = sourceCode.getFirstToken(node); - - checkSpacingBefore(firstToken, PREV_TOKEN_M); - checkSpacingAfter(firstToken, NEXT_TOKEN_M); - - if (node.type === "ExportDefaultDeclaration") { - checkSpacingAround(sourceCode.getTokenAfter(firstToken)); - } - - if (node.type === "ExportAllDeclaration" && node.exported) { - const asToken = sourceCode.getTokenBefore(node.exported); - - checkSpacingBefore(asToken, PREV_TOKEN_M); - checkSpacingAfter(asToken, NEXT_TOKEN_M); - } - - if (node.source) { - const fromToken = sourceCode.getTokenBefore(node.source); - - checkSpacingBefore(fromToken, PREV_TOKEN_M); - checkSpacingAfter(fromToken, NEXT_TOKEN_M); - } - } - - /** - * Reports `as` keyword of a given node if usage of spacing around this - * keyword is invalid. - * @param {ASTNode} node An `ImportSpecifier` node to check. - * @returns {void} - */ - function checkSpacingForImportSpecifier(node) { - if (node.imported.range[0] !== node.local.range[0]) { - const asToken = sourceCode.getTokenBefore(node.local); - - checkSpacingBefore(asToken, PREV_TOKEN_M); - } - } - - /** - * Reports `as` keyword of a given node if usage of spacing around this - * keyword is invalid. - * @param {ASTNode} node An `ExportSpecifier` node to check. - * @returns {void} - */ - function checkSpacingForExportSpecifier(node) { - if (node.local.range[0] !== node.exported.range[0]) { - const asToken = sourceCode.getTokenBefore(node.exported); - - checkSpacingBefore(asToken, PREV_TOKEN_M); - checkSpacingAfter(asToken, NEXT_TOKEN_M); - } - } - - /** - * Reports `as` keyword of a given node if usage of spacing around this - * keyword is invalid. - * @param {ASTNode} node A node to report. - * @returns {void} - */ - function checkSpacingForImportNamespaceSpecifier(node) { - const asToken = sourceCode.getFirstToken(node, 1); - - checkSpacingBefore(asToken, PREV_TOKEN_M); - } - - /** - * Reports `static`, `get`, and `set` keywords of a given node if usage of - * spacing around those keywords is invalid. - * @param {ASTNode} node A node to report. - * @throws {Error} If unable to find token get, set, or async beside method name. - * @returns {void} - */ - function checkSpacingForProperty(node) { - if (node.static) { - checkSpacingAroundFirstToken(node); - } - if ( - node.kind === "get" || - node.kind === "set" || - ((node.method || node.type === "MethodDefinition") && - node.value.async) - ) { - const token = sourceCode.getTokenBefore(node.key, tok => { - switch (tok.value) { - case "get": - case "set": - case "async": - return true; - default: - return false; - } - }); - - if (!token) { - throw new Error( - "Failed to find token get, set, or async beside method name", - ); - } - - checkSpacingAround(token); - } - } - - /** - * Reports `await` keyword of a given node if usage of spacing before - * this keyword is invalid. - * @param {ASTNode} node A node to report. - * @returns {void} - */ - function checkSpacingForAwaitExpression(node) { - checkSpacingBefore(sourceCode.getFirstToken(node)); - } - - return { - // Statements - DebuggerStatement: checkSpacingAroundFirstToken, - WithStatement: checkSpacingAroundFirstToken, - - // Statements - Control flow - BreakStatement: checkSpacingAroundFirstToken, - ContinueStatement: checkSpacingAroundFirstToken, - ReturnStatement: checkSpacingAroundFirstToken, - ThrowStatement: checkSpacingAroundFirstToken, - TryStatement: checkSpacingForTryStatement, - - // Statements - Choice - IfStatement: checkSpacingForIfStatement, - SwitchStatement: checkSpacingAroundFirstToken, - SwitchCase: checkSpacingAroundFirstToken, - - // Statements - Loops - DoWhileStatement: checkSpacingForDoWhileStatement, - ForInStatement: checkSpacingForForInStatement, - ForOfStatement: checkSpacingForForOfStatement, - ForStatement: checkSpacingAroundFirstToken, - WhileStatement: checkSpacingAroundFirstToken, - - // Statements - Declarations - ClassDeclaration: checkSpacingForClass, - ExportNamedDeclaration: checkSpacingForModuleDeclaration, - ExportDefaultDeclaration: checkSpacingForModuleDeclaration, - ExportAllDeclaration: checkSpacingForModuleDeclaration, - FunctionDeclaration: checkSpacingForFunction, - ImportDeclaration: checkSpacingForModuleDeclaration, - VariableDeclaration: checkSpacingAroundFirstToken, - - // Expressions - ArrowFunctionExpression: checkSpacingForFunction, - AwaitExpression: checkSpacingForAwaitExpression, - ClassExpression: checkSpacingForClass, - FunctionExpression: checkSpacingForFunction, - NewExpression: checkSpacingBeforeFirstToken, - Super: checkSpacingBeforeFirstToken, - ThisExpression: checkSpacingBeforeFirstToken, - UnaryExpression: checkSpacingBeforeFirstToken, - YieldExpression: checkSpacingBeforeFirstToken, - - // Others - ImportSpecifier: checkSpacingForImportSpecifier, - ExportSpecifier: checkSpacingForExportSpecifier, - ImportNamespaceSpecifier: checkSpacingForImportNamespaceSpecifier, - MethodDefinition: checkSpacingForProperty, - PropertyDefinition: checkSpacingForProperty, - StaticBlock: checkSpacingAroundFirstToken, - Property: checkSpacingForProperty, - - // To avoid conflicts with `space-infix-ops`, e.g. `a > this.b` - "BinaryExpression[operator='>']"(node) { - const operatorToken = sourceCode.getTokenBefore( - node.right, - astUtils.isNotOpeningParenToken, - ); - - tokensToIgnore.add(operatorToken); - }, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Enforce consistent spacing before and after keywords", + recommended: false, + url: "https://eslint.org/docs/latest/rules/keyword-spacing" + }, + + fixable: "whitespace", + + schema: [ + { + type: "object", + properties: { + before: { type: "boolean", default: true }, + after: { type: "boolean", default: true }, + overrides: { + type: "object", + properties: KEYS.reduce((retv, key) => { + retv[key] = { + type: "object", + properties: { + before: { type: "boolean" }, + after: { type: "boolean" } + }, + additionalProperties: false + }; + return retv; + }, {}), + additionalProperties: false + } + }, + additionalProperties: false + } + ], + messages: { + expectedBefore: "Expected space(s) before \"{{value}}\".", + expectedAfter: "Expected space(s) after \"{{value}}\".", + unexpectedBefore: "Unexpected space(s) before \"{{value}}\".", + unexpectedAfter: "Unexpected space(s) after \"{{value}}\"." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + + const tokensToIgnore = new WeakSet(); + + /** + * Reports a given token if there are not space(s) before the token. + * @param {Token} token A token to report. + * @param {RegExp} pattern A pattern of the previous token to check. + * @returns {void} + */ + function expectSpaceBefore(token, pattern) { + const prevToken = sourceCode.getTokenBefore(token); + + if (prevToken && + (CHECK_TYPE.test(prevToken.type) || pattern.test(prevToken.value)) && + !isOpenParenOfTemplate(prevToken) && + !tokensToIgnore.has(prevToken) && + astUtils.isTokenOnSameLine(prevToken, token) && + !sourceCode.isSpaceBetweenTokens(prevToken, token) + ) { + context.report({ + loc: token.loc, + messageId: "expectedBefore", + data: token, + fix(fixer) { + return fixer.insertTextBefore(token, " "); + } + }); + } + } + + /** + * Reports a given token if there are space(s) before the token. + * @param {Token} token A token to report. + * @param {RegExp} pattern A pattern of the previous token to check. + * @returns {void} + */ + function unexpectSpaceBefore(token, pattern) { + const prevToken = sourceCode.getTokenBefore(token); + + if (prevToken && + (CHECK_TYPE.test(prevToken.type) || pattern.test(prevToken.value)) && + !isOpenParenOfTemplate(prevToken) && + !tokensToIgnore.has(prevToken) && + astUtils.isTokenOnSameLine(prevToken, token) && + sourceCode.isSpaceBetweenTokens(prevToken, token) + ) { + context.report({ + loc: { start: prevToken.loc.end, end: token.loc.start }, + messageId: "unexpectedBefore", + data: token, + fix(fixer) { + return fixer.removeRange([prevToken.range[1], token.range[0]]); + } + }); + } + } + + /** + * Reports a given token if there are not space(s) after the token. + * @param {Token} token A token to report. + * @param {RegExp} pattern A pattern of the next token to check. + * @returns {void} + */ + function expectSpaceAfter(token, pattern) { + const nextToken = sourceCode.getTokenAfter(token); + + if (nextToken && + (CHECK_TYPE.test(nextToken.type) || pattern.test(nextToken.value)) && + !isCloseParenOfTemplate(nextToken) && + !tokensToIgnore.has(nextToken) && + astUtils.isTokenOnSameLine(token, nextToken) && + !sourceCode.isSpaceBetweenTokens(token, nextToken) + ) { + context.report({ + loc: token.loc, + messageId: "expectedAfter", + data: token, + fix(fixer) { + return fixer.insertTextAfter(token, " "); + } + }); + } + } + + /** + * Reports a given token if there are space(s) after the token. + * @param {Token} token A token to report. + * @param {RegExp} pattern A pattern of the next token to check. + * @returns {void} + */ + function unexpectSpaceAfter(token, pattern) { + const nextToken = sourceCode.getTokenAfter(token); + + if (nextToken && + (CHECK_TYPE.test(nextToken.type) || pattern.test(nextToken.value)) && + !isCloseParenOfTemplate(nextToken) && + !tokensToIgnore.has(nextToken) && + astUtils.isTokenOnSameLine(token, nextToken) && + sourceCode.isSpaceBetweenTokens(token, nextToken) + ) { + + context.report({ + loc: { start: token.loc.end, end: nextToken.loc.start }, + messageId: "unexpectedAfter", + data: token, + fix(fixer) { + return fixer.removeRange([token.range[1], nextToken.range[0]]); + } + }); + } + } + + /** + * Parses the option object and determines check methods for each keyword. + * @param {Object|undefined} options The option object to parse. + * @returns {Object} - Normalized option object. + * Keys are keywords (there are for every keyword). + * Values are instances of `{"before": function, "after": function}`. + */ + function parseOptions(options = {}) { + const before = options.before !== false; + const after = options.after !== false; + const defaultValue = { + before: before ? expectSpaceBefore : unexpectSpaceBefore, + after: after ? expectSpaceAfter : unexpectSpaceAfter + }; + const overrides = (options && options.overrides) || {}; + const retv = Object.create(null); + + for (let i = 0; i < KEYS.length; ++i) { + const key = KEYS[i]; + const override = overrides[key]; + + if (override) { + const thisBefore = ("before" in override) ? override.before : before; + const thisAfter = ("after" in override) ? override.after : after; + + retv[key] = { + before: thisBefore ? expectSpaceBefore : unexpectSpaceBefore, + after: thisAfter ? expectSpaceAfter : unexpectSpaceAfter + }; + } else { + retv[key] = defaultValue; + } + } + + return retv; + } + + const checkMethodMap = parseOptions(context.options[0]); + + /** + * Reports a given token if usage of spacing followed by the token is + * invalid. + * @param {Token} token A token to report. + * @param {RegExp} [pattern] Optional. A pattern of the previous + * token to check. + * @returns {void} + */ + function checkSpacingBefore(token, pattern) { + checkMethodMap[token.value].before(token, pattern || PREV_TOKEN); + } + + /** + * Reports a given token if usage of spacing preceded by the token is + * invalid. + * @param {Token} token A token to report. + * @param {RegExp} [pattern] Optional. A pattern of the next + * token to check. + * @returns {void} + */ + function checkSpacingAfter(token, pattern) { + checkMethodMap[token.value].after(token, pattern || NEXT_TOKEN); + } + + /** + * Reports a given token if usage of spacing around the token is invalid. + * @param {Token} token A token to report. + * @returns {void} + */ + function checkSpacingAround(token) { + checkSpacingBefore(token); + checkSpacingAfter(token); + } + + /** + * Reports the first token of a given node if the first token is a keyword + * and usage of spacing around the token is invalid. + * @param {ASTNode|null} node A node to report. + * @returns {void} + */ + function checkSpacingAroundFirstToken(node) { + const firstToken = node && sourceCode.getFirstToken(node); + + if (firstToken && firstToken.type === "Keyword") { + checkSpacingAround(firstToken); + } + } + + /** + * Reports the first token of a given node if the first token is a keyword + * and usage of spacing followed by the token is invalid. + * + * This is used for unary operators (e.g. `typeof`), `function`, and `super`. + * Other rules are handling usage of spacing preceded by those keywords. + * @param {ASTNode|null} node A node to report. + * @returns {void} + */ + function checkSpacingBeforeFirstToken(node) { + const firstToken = node && sourceCode.getFirstToken(node); + + if (firstToken && firstToken.type === "Keyword") { + checkSpacingBefore(firstToken); + } + } + + /** + * Reports the previous token of a given node if the token is a keyword and + * usage of spacing around the token is invalid. + * @param {ASTNode|null} node A node to report. + * @returns {void} + */ + function checkSpacingAroundTokenBefore(node) { + if (node) { + const token = sourceCode.getTokenBefore(node, astUtils.isKeywordToken); + + checkSpacingAround(token); + } + } + + /** + * Reports `async` or `function` keywords of a given node if usage of + * spacing around those keywords is invalid. + * @param {ASTNode} node A node to report. + * @returns {void} + */ + function checkSpacingForFunction(node) { + const firstToken = node && sourceCode.getFirstToken(node); + + if (firstToken && + ((firstToken.type === "Keyword" && firstToken.value === "function") || + firstToken.value === "async") + ) { + checkSpacingBefore(firstToken); + } + } + + /** + * Reports `class` and `extends` keywords of a given node if usage of + * spacing around those keywords is invalid. + * @param {ASTNode} node A node to report. + * @returns {void} + */ + function checkSpacingForClass(node) { + checkSpacingAroundFirstToken(node); + checkSpacingAroundTokenBefore(node.superClass); + } + + /** + * Reports `if` and `else` keywords of a given node if usage of spacing + * around those keywords is invalid. + * @param {ASTNode} node A node to report. + * @returns {void} + */ + function checkSpacingForIfStatement(node) { + checkSpacingAroundFirstToken(node); + checkSpacingAroundTokenBefore(node.alternate); + } + + /** + * Reports `try`, `catch`, and `finally` keywords of a given node if usage + * of spacing around those keywords is invalid. + * @param {ASTNode} node A node to report. + * @returns {void} + */ + function checkSpacingForTryStatement(node) { + checkSpacingAroundFirstToken(node); + checkSpacingAroundFirstToken(node.handler); + checkSpacingAroundTokenBefore(node.finalizer); + } + + /** + * Reports `do` and `while` keywords of a given node if usage of spacing + * around those keywords is invalid. + * @param {ASTNode} node A node to report. + * @returns {void} + */ + function checkSpacingForDoWhileStatement(node) { + checkSpacingAroundFirstToken(node); + checkSpacingAroundTokenBefore(node.test); + } + + /** + * Reports `for` and `in` keywords of a given node if usage of spacing + * around those keywords is invalid. + * @param {ASTNode} node A node to report. + * @returns {void} + */ + function checkSpacingForForInStatement(node) { + checkSpacingAroundFirstToken(node); + + const inToken = sourceCode.getTokenBefore(node.right, astUtils.isNotOpeningParenToken); + const previousToken = sourceCode.getTokenBefore(inToken); + + if (previousToken.type !== "PrivateIdentifier") { + checkSpacingBefore(inToken); + } + + checkSpacingAfter(inToken); + } + + /** + * Reports `for` and `of` keywords of a given node if usage of spacing + * around those keywords is invalid. + * @param {ASTNode} node A node to report. + * @returns {void} + */ + function checkSpacingForForOfStatement(node) { + if (node.await) { + checkSpacingBefore(sourceCode.getFirstToken(node, 0)); + checkSpacingAfter(sourceCode.getFirstToken(node, 1)); + } else { + checkSpacingAroundFirstToken(node); + } + + const ofToken = sourceCode.getTokenBefore(node.right, astUtils.isNotOpeningParenToken); + const previousToken = sourceCode.getTokenBefore(ofToken); + + if (previousToken.type !== "PrivateIdentifier") { + checkSpacingBefore(ofToken); + } + + checkSpacingAfter(ofToken); + } + + /** + * Reports `import`, `export`, `as`, and `from` keywords of a given node if + * usage of spacing around those keywords is invalid. + * + * This rule handles the `*` token in module declarations. + * + * import*as A from "./a"; /*error Expected space(s) after "import". + * error Expected space(s) before "as". + * @param {ASTNode} node A node to report. + * @returns {void} + */ + function checkSpacingForModuleDeclaration(node) { + const firstToken = sourceCode.getFirstToken(node); + + checkSpacingBefore(firstToken, PREV_TOKEN_M); + checkSpacingAfter(firstToken, NEXT_TOKEN_M); + + if (node.type === "ExportDefaultDeclaration") { + checkSpacingAround(sourceCode.getTokenAfter(firstToken)); + } + + if (node.type === "ExportAllDeclaration" && node.exported) { + const asToken = sourceCode.getTokenBefore(node.exported); + + checkSpacingBefore(asToken, PREV_TOKEN_M); + checkSpacingAfter(asToken, NEXT_TOKEN_M); + } + + if (node.source) { + const fromToken = sourceCode.getTokenBefore(node.source); + + checkSpacingBefore(fromToken, PREV_TOKEN_M); + checkSpacingAfter(fromToken, NEXT_TOKEN_M); + } + } + + /** + * Reports `as` keyword of a given node if usage of spacing around this + * keyword is invalid. + * @param {ASTNode} node An `ImportSpecifier` node to check. + * @returns {void} + */ + function checkSpacingForImportSpecifier(node) { + if (node.imported.range[0] !== node.local.range[0]) { + const asToken = sourceCode.getTokenBefore(node.local); + + checkSpacingBefore(asToken, PREV_TOKEN_M); + } + } + + /** + * Reports `as` keyword of a given node if usage of spacing around this + * keyword is invalid. + * @param {ASTNode} node An `ExportSpecifier` node to check. + * @returns {void} + */ + function checkSpacingForExportSpecifier(node) { + if (node.local.range[0] !== node.exported.range[0]) { + const asToken = sourceCode.getTokenBefore(node.exported); + + checkSpacingBefore(asToken, PREV_TOKEN_M); + checkSpacingAfter(asToken, NEXT_TOKEN_M); + } + } + + /** + * Reports `as` keyword of a given node if usage of spacing around this + * keyword is invalid. + * @param {ASTNode} node A node to report. + * @returns {void} + */ + function checkSpacingForImportNamespaceSpecifier(node) { + const asToken = sourceCode.getFirstToken(node, 1); + + checkSpacingBefore(asToken, PREV_TOKEN_M); + } + + /** + * Reports `static`, `get`, and `set` keywords of a given node if usage of + * spacing around those keywords is invalid. + * @param {ASTNode} node A node to report. + * @throws {Error} If unable to find token get, set, or async beside method name. + * @returns {void} + */ + function checkSpacingForProperty(node) { + if (node.static) { + checkSpacingAroundFirstToken(node); + } + if (node.kind === "get" || + node.kind === "set" || + ( + (node.method || node.type === "MethodDefinition") && + node.value.async + ) + ) { + const token = sourceCode.getTokenBefore( + node.key, + tok => { + switch (tok.value) { + case "get": + case "set": + case "async": + return true; + default: + return false; + } + } + ); + + if (!token) { + throw new Error("Failed to find token get, set, or async beside method name"); + } + + + checkSpacingAround(token); + } + } + + /** + * Reports `await` keyword of a given node if usage of spacing before + * this keyword is invalid. + * @param {ASTNode} node A node to report. + * @returns {void} + */ + function checkSpacingForAwaitExpression(node) { + checkSpacingBefore(sourceCode.getFirstToken(node)); + } + + return { + + // Statements + DebuggerStatement: checkSpacingAroundFirstToken, + WithStatement: checkSpacingAroundFirstToken, + + // Statements - Control flow + BreakStatement: checkSpacingAroundFirstToken, + ContinueStatement: checkSpacingAroundFirstToken, + ReturnStatement: checkSpacingAroundFirstToken, + ThrowStatement: checkSpacingAroundFirstToken, + TryStatement: checkSpacingForTryStatement, + + // Statements - Choice + IfStatement: checkSpacingForIfStatement, + SwitchStatement: checkSpacingAroundFirstToken, + SwitchCase: checkSpacingAroundFirstToken, + + // Statements - Loops + DoWhileStatement: checkSpacingForDoWhileStatement, + ForInStatement: checkSpacingForForInStatement, + ForOfStatement: checkSpacingForForOfStatement, + ForStatement: checkSpacingAroundFirstToken, + WhileStatement: checkSpacingAroundFirstToken, + + // Statements - Declarations + ClassDeclaration: checkSpacingForClass, + ExportNamedDeclaration: checkSpacingForModuleDeclaration, + ExportDefaultDeclaration: checkSpacingForModuleDeclaration, + ExportAllDeclaration: checkSpacingForModuleDeclaration, + FunctionDeclaration: checkSpacingForFunction, + ImportDeclaration: checkSpacingForModuleDeclaration, + VariableDeclaration: checkSpacingAroundFirstToken, + + // Expressions + ArrowFunctionExpression: checkSpacingForFunction, + AwaitExpression: checkSpacingForAwaitExpression, + ClassExpression: checkSpacingForClass, + FunctionExpression: checkSpacingForFunction, + NewExpression: checkSpacingBeforeFirstToken, + Super: checkSpacingBeforeFirstToken, + ThisExpression: checkSpacingBeforeFirstToken, + UnaryExpression: checkSpacingBeforeFirstToken, + YieldExpression: checkSpacingBeforeFirstToken, + + // Others + ImportSpecifier: checkSpacingForImportSpecifier, + ExportSpecifier: checkSpacingForExportSpecifier, + ImportNamespaceSpecifier: checkSpacingForImportNamespaceSpecifier, + MethodDefinition: checkSpacingForProperty, + PropertyDefinition: checkSpacingForProperty, + StaticBlock: checkSpacingAroundFirstToken, + Property: checkSpacingForProperty, + + // To avoid conflicts with `space-infix-ops`, e.g. `a > this.b` + "BinaryExpression[operator='>']"(node) { + const operatorToken = sourceCode.getTokenBefore(node.right, astUtils.isNotOpeningParenToken); + + tokensToIgnore.add(operatorToken); + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/line-comment-position.js b/node_modules/eslint/lib/rules/line-comment-position.js index 6c1640337..314fac167 100644 --- a/node_modules/eslint/lib/rules/line-comment-position.js +++ b/node_modules/eslint/lib/rules/line-comment-position.js @@ -1,7 +1,6 @@ /** * @fileoverview Rule to enforce the position of line comments * @author Alberto Rodríguez - * @deprecated in ESLint v9.3.0 */ "use strict"; @@ -11,147 +10,113 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "9.3.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "line-comment-position", - url: "https://eslint.style/rules/line-comment-position", - }, - }, - ], - }, - type: "layout", - - docs: { - description: "Enforce position of line comments", - recommended: false, - url: "https://eslint.org/docs/latest/rules/line-comment-position", - }, - - schema: [ - { - oneOf: [ - { - enum: ["above", "beside"], - }, - { - type: "object", - properties: { - position: { - enum: ["above", "beside"], - }, - ignorePattern: { - type: "string", - }, - applyDefaultPatterns: { - type: "boolean", - }, - applyDefaultIgnorePatterns: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - }, - ], - messages: { - above: "Expected comment to be above code.", - beside: "Expected comment to be beside code.", - }, - }, - - create(context) { - const options = context.options[0]; - - let above, - ignorePattern, - applyDefaultIgnorePatterns = true; - - if (!options || typeof options === "string") { - above = !options || options === "above"; - } else { - above = !options.position || options.position === "above"; - ignorePattern = options.ignorePattern; - - if (Object.hasOwn(options, "applyDefaultIgnorePatterns")) { - applyDefaultIgnorePatterns = options.applyDefaultIgnorePatterns; - } else { - applyDefaultIgnorePatterns = - options.applyDefaultPatterns !== false; - } - } - - const defaultIgnoreRegExp = astUtils.COMMENTS_IGNORE_PATTERN; - const fallThroughRegExp = /^\s*falls?\s?through/u; - const customIgnoreRegExp = new RegExp(ignorePattern, "u"); - const sourceCode = context.sourceCode; - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - return { - Program() { - const comments = sourceCode.getAllComments(); - - comments - .filter(token => token.type === "Line") - .forEach(node => { - if ( - applyDefaultIgnorePatterns && - (defaultIgnoreRegExp.test(node.value) || - fallThroughRegExp.test(node.value)) - ) { - return; - } - - if ( - ignorePattern && - customIgnoreRegExp.test(node.value) - ) { - return; - } - - const previous = sourceCode.getTokenBefore(node, { - includeComments: true, - }); - const isOnSameLine = - previous && - previous.loc.end.line === node.loc.start.line; - - if (above) { - if (isOnSameLine) { - context.report({ - node, - messageId: "above", - }); - } - } else { - if (!isOnSameLine) { - context.report({ - node, - messageId: "beside", - }); - } - } - }); - }, - }; - }, + meta: { + type: "layout", + + docs: { + description: "Enforce position of line comments", + recommended: false, + url: "https://eslint.org/docs/latest/rules/line-comment-position" + }, + + schema: [ + { + oneOf: [ + { + enum: ["above", "beside"] + }, + { + type: "object", + properties: { + position: { + enum: ["above", "beside"] + }, + ignorePattern: { + type: "string" + }, + applyDefaultPatterns: { + type: "boolean" + }, + applyDefaultIgnorePatterns: { + type: "boolean" + } + }, + additionalProperties: false + } + ] + } + ], + messages: { + above: "Expected comment to be above code.", + beside: "Expected comment to be beside code." + } + }, + + create(context) { + const options = context.options[0]; + + let above, + ignorePattern, + applyDefaultIgnorePatterns = true; + + if (!options || typeof options === "string") { + above = !options || options === "above"; + + } else { + above = !options.position || options.position === "above"; + ignorePattern = options.ignorePattern; + + if (Object.prototype.hasOwnProperty.call(options, "applyDefaultIgnorePatterns")) { + applyDefaultIgnorePatterns = options.applyDefaultIgnorePatterns; + } else { + applyDefaultIgnorePatterns = options.applyDefaultPatterns !== false; + } + } + + const defaultIgnoreRegExp = astUtils.COMMENTS_IGNORE_PATTERN; + const fallThroughRegExp = /^\s*falls?\s?through/u; + const customIgnoreRegExp = new RegExp(ignorePattern, "u"); + const sourceCode = context.sourceCode; + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + return { + Program() { + const comments = sourceCode.getAllComments(); + + comments.filter(token => token.type === "Line").forEach(node => { + if (applyDefaultIgnorePatterns && (defaultIgnoreRegExp.test(node.value) || fallThroughRegExp.test(node.value))) { + return; + } + + if (ignorePattern && customIgnoreRegExp.test(node.value)) { + return; + } + + const previous = sourceCode.getTokenBefore(node, { includeComments: true }); + const isOnSameLine = previous && previous.loc.end.line === node.loc.start.line; + + if (above) { + if (isOnSameLine) { + context.report({ + node, + messageId: "above" + }); + } + } else { + if (!isOnSameLine) { + context.report({ + node, + messageId: "beside" + }); + } + } + }); + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/linebreak-style.js b/node_modules/eslint/lib/rules/linebreak-style.js index aea1b8ce1..e59acca1b 100644 --- a/node_modules/eslint/lib/rules/linebreak-style.js +++ b/node_modules/eslint/lib/rules/linebreak-style.js @@ -6,14 +6,6 @@ "use strict"; -//------------------------------------------------------------------------------ -// Typedefs -//------------------------------------------------------------------------------ - -/** - * @import { SourceRange } from "@eslint/core"; - */ - //------------------------------------------------------------------------------ // Requirements //------------------------------------------------------------------------------ @@ -24,112 +16,93 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "linebreak-style", - url: "https://eslint.style/rules/linebreak-style", - }, - }, - ], - }, - type: "layout", - - docs: { - description: "Enforce consistent linebreak style", - recommended: false, - url: "https://eslint.org/docs/latest/rules/linebreak-style", - }, - - fixable: "whitespace", - - schema: [ - { - enum: ["unix", "windows"], - }, - ], - messages: { - expectedLF: "Expected linebreaks to be 'LF' but found 'CRLF'.", - expectedCRLF: "Expected linebreaks to be 'CRLF' but found 'LF'.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - /** - * Builds a fix function that replaces text at the specified range in the source text. - * @param {SourceRange} range The range to replace - * @param {string} text The text to insert. - * @returns {Function} Fixer function - * @private - */ - function createFix(range, text) { - return function (fixer) { - return fixer.replaceTextRange(range, text); - }; - } - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - return { - Program: function checkForLinebreakStyle(node) { - const linebreakStyle = context.options[0] || "unix", - expectedLF = linebreakStyle === "unix", - expectedLFChars = expectedLF ? "\n" : "\r\n", - source = sourceCode.getText(), - pattern = astUtils.createGlobalLinebreakMatcher(); - let match; - - let i = 0; - - while ((match = pattern.exec(source)) !== null) { - i++; - if (match[0] === expectedLFChars) { - continue; - } - - const index = match.index; - const range = [index, index + match[0].length]; - - context.report({ - node, - loc: { - start: { - line: i, - column: sourceCode.lines[i - 1].length, - }, - end: { - line: i + 1, - column: 0, - }, - }, - messageId: expectedLF ? "expectedLF" : "expectedCRLF", - fix: createFix(range, expectedLFChars), - }); - } - }, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Enforce consistent linebreak style", + recommended: false, + url: "https://eslint.org/docs/latest/rules/linebreak-style" + }, + + fixable: "whitespace", + + schema: [ + { + enum: ["unix", "windows"] + } + ], + messages: { + expectedLF: "Expected linebreaks to be 'LF' but found 'CRLF'.", + expectedCRLF: "Expected linebreaks to be 'CRLF' but found 'LF'." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + /** + * Builds a fix function that replaces text at the specified range in the source text. + * @param {int[]} range The range to replace + * @param {string} text The text to insert. + * @returns {Function} Fixer function + * @private + */ + function createFix(range, text) { + return function(fixer) { + return fixer.replaceTextRange(range, text); + }; + } + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + return { + Program: function checkForLinebreakStyle(node) { + const linebreakStyle = context.options[0] || "unix", + expectedLF = linebreakStyle === "unix", + expectedLFChars = expectedLF ? "\n" : "\r\n", + source = sourceCode.getText(), + pattern = astUtils.createGlobalLinebreakMatcher(); + let match; + + let i = 0; + + while ((match = pattern.exec(source)) !== null) { + i++; + if (match[0] === expectedLFChars) { + continue; + } + + const index = match.index; + const range = [index, index + match[0].length]; + + context.report({ + node, + loc: { + start: { + line: i, + column: sourceCode.lines[i - 1].length + }, + end: { + line: i + 1, + column: 0 + } + }, + messageId: expectedLF ? "expectedLF" : "expectedCRLF", + fix: createFix(range, expectedLFChars) + }); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/lines-around-comment.js b/node_modules/eslint/lib/rules/lines-around-comment.js index 7b2d0ac80..2a6e472f9 100644 --- a/node_modules/eslint/lib/rules/lines-around-comment.js +++ b/node_modules/eslint/lib/rules/lines-around-comment.js @@ -21,15 +21,12 @@ const astUtils = require("./utils/ast-utils"); * @returns {Array} An array of line numbers. */ function getEmptyLineNums(lines) { - const emptyLines = lines - .map((line, i) => ({ - code: line.trim(), - num: i + 1, - })) - .filter(line => !line.code) - .map(line => line.num); - - return emptyLines; + const emptyLines = lines.map((line, i) => ({ + code: line.trim(), + num: i + 1 + })).filter(line => !line.code).map(line => line.num); + + return emptyLines; } /** @@ -38,544 +35,437 @@ function getEmptyLineNums(lines) { * @returns {Array} An array of line numbers. */ function getCommentLineNums(comments) { - const lines = []; + const lines = []; - comments.forEach(token => { - const start = token.loc.start.line; - const end = token.loc.end.line; + comments.forEach(token => { + const start = token.loc.start.line; + const end = token.loc.end.line; - lines.push(start, end); - }); - return lines; + lines.push(start, end); + }); + return lines; } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "lines-around-comment", - url: "https://eslint.style/rules/lines-around-comment", - }, - }, - ], - }, - type: "layout", - - docs: { - description: "Require empty lines around comments", - recommended: false, - url: "https://eslint.org/docs/latest/rules/lines-around-comment", - }, - - fixable: "whitespace", - - schema: [ - { - type: "object", - properties: { - beforeBlockComment: { - type: "boolean", - default: true, - }, - afterBlockComment: { - type: "boolean", - default: false, - }, - beforeLineComment: { - type: "boolean", - default: false, - }, - afterLineComment: { - type: "boolean", - default: false, - }, - allowBlockStart: { - type: "boolean", - default: false, - }, - allowBlockEnd: { - type: "boolean", - default: false, - }, - allowClassStart: { - type: "boolean", - }, - allowClassEnd: { - type: "boolean", - }, - allowObjectStart: { - type: "boolean", - }, - allowObjectEnd: { - type: "boolean", - }, - allowArrayStart: { - type: "boolean", - }, - allowArrayEnd: { - type: "boolean", - }, - ignorePattern: { - type: "string", - }, - applyDefaultIgnorePatterns: { - type: "boolean", - }, - afterHashbangComment: { - type: "boolean", - default: false, - }, - }, - additionalProperties: false, - }, - ], - messages: { - after: "Expected line after comment.", - before: "Expected line before comment.", - }, - }, - - create(context) { - const options = Object.assign({}, context.options[0]); - const ignorePattern = options.ignorePattern; - const defaultIgnoreRegExp = astUtils.COMMENTS_IGNORE_PATTERN; - const customIgnoreRegExp = new RegExp(ignorePattern, "u"); - const applyDefaultIgnorePatterns = - options.applyDefaultIgnorePatterns !== false; - - options.beforeBlockComment = - typeof options.beforeBlockComment !== "undefined" - ? options.beforeBlockComment - : true; - - const sourceCode = context.sourceCode; - - const lines = sourceCode.lines, - numLines = lines.length + 1, - comments = sourceCode.getAllComments(), - commentLines = getCommentLineNums(comments), - emptyLines = getEmptyLineNums(lines), - commentAndEmptyLines = new Set(commentLines.concat(emptyLines)); - - /** - * Returns whether or not comments are on lines starting with or ending with code - * @param {token} token The comment token to check. - * @returns {boolean} True if the comment is not alone. - */ - function codeAroundComment(token) { - let currentToken = token; - - do { - currentToken = sourceCode.getTokenBefore(currentToken, { - includeComments: true, - }); - } while (currentToken && astUtils.isCommentToken(currentToken)); - - if ( - currentToken && - astUtils.isTokenOnSameLine(currentToken, token) - ) { - return true; - } - - currentToken = token; - do { - currentToken = sourceCode.getTokenAfter(currentToken, { - includeComments: true, - }); - } while (currentToken && astUtils.isCommentToken(currentToken)); - - if ( - currentToken && - astUtils.isTokenOnSameLine(token, currentToken) - ) { - return true; - } - - return false; - } - - /** - * Returns whether or not comments are inside a node type or not. - * @param {ASTNode} parent The Comment parent node. - * @param {string} nodeType The parent type to check against. - * @returns {boolean} True if the comment is inside nodeType. - */ - function isParentNodeType(parent, nodeType) { - return ( - parent.type === nodeType || - (parent.body && parent.body.type === nodeType) || - (parent.consequent && parent.consequent.type === nodeType) - ); - } - - /** - * Returns the parent node that contains the given token. - * @param {token} token The token to check. - * @returns {ASTNode|null} The parent node that contains the given token. - */ - function getParentNodeOfToken(token) { - const node = sourceCode.getNodeByRangeIndex(token.range[0]); - - /* - * For the purpose of this rule, the comment token is in a `StaticBlock` node only - * if it's inside the braces of that `StaticBlock` node. - * - * Example where this function returns `null`: - * - * static - * // comment - * { - * } - * - * Example where this function returns `StaticBlock` node: - * - * static - * { - * // comment - * } - * - */ - if (node && node.type === "StaticBlock") { - const openingBrace = sourceCode.getFirstToken(node, { - skip: 1, - }); // skip the `static` token - - return token.range[0] >= openingBrace.range[0] ? node : null; - } - - return node; - } - - /** - * Returns whether or not comments are at the parent start or not. - * @param {token} token The Comment token. - * @param {string} nodeType The parent type to check against. - * @returns {boolean} True if the comment is at parent start. - */ - function isCommentAtParentStart(token, nodeType) { - const parent = getParentNodeOfToken(token); - - if (parent && isParentNodeType(parent, nodeType)) { - let parentStartNodeOrToken = parent; - - if (parent.type === "StaticBlock") { - parentStartNodeOrToken = sourceCode.getFirstToken(parent, { - skip: 1, - }); // opening brace of the static block - } else if (parent.type === "SwitchStatement") { - parentStartNodeOrToken = sourceCode.getTokenAfter( - parent.discriminant, - { - filter: astUtils.isOpeningBraceToken, - }, - ); // opening brace of the switch statement - } - - return ( - token.loc.start.line - - parentStartNodeOrToken.loc.start.line === - 1 - ); - } - - return false; - } - - /** - * Returns whether or not comments are at the parent end or not. - * @param {token} token The Comment token. - * @param {string} nodeType The parent type to check against. - * @returns {boolean} True if the comment is at parent end. - */ - function isCommentAtParentEnd(token, nodeType) { - const parent = getParentNodeOfToken(token); - - return ( - !!parent && - isParentNodeType(parent, nodeType) && - parent.loc.end.line - token.loc.end.line === 1 - ); - } - - /** - * Returns whether or not comments are at the block start or not. - * @param {token} token The Comment token. - * @returns {boolean} True if the comment is at block start. - */ - function isCommentAtBlockStart(token) { - return ( - isCommentAtParentStart(token, "ClassBody") || - isCommentAtParentStart(token, "BlockStatement") || - isCommentAtParentStart(token, "StaticBlock") || - isCommentAtParentStart(token, "SwitchCase") || - isCommentAtParentStart(token, "SwitchStatement") - ); - } - - /** - * Returns whether or not comments are at the block end or not. - * @param {token} token The Comment token. - * @returns {boolean} True if the comment is at block end. - */ - function isCommentAtBlockEnd(token) { - return ( - isCommentAtParentEnd(token, "ClassBody") || - isCommentAtParentEnd(token, "BlockStatement") || - isCommentAtParentEnd(token, "StaticBlock") || - isCommentAtParentEnd(token, "SwitchCase") || - isCommentAtParentEnd(token, "SwitchStatement") - ); - } - - /** - * Returns whether or not comments are at the class start or not. - * @param {token} token The Comment token. - * @returns {boolean} True if the comment is at class start. - */ - function isCommentAtClassStart(token) { - return isCommentAtParentStart(token, "ClassBody"); - } - - /** - * Returns whether or not comments are at the class end or not. - * @param {token} token The Comment token. - * @returns {boolean} True if the comment is at class end. - */ - function isCommentAtClassEnd(token) { - return isCommentAtParentEnd(token, "ClassBody"); - } - - /** - * Returns whether or not comments are at the object start or not. - * @param {token} token The Comment token. - * @returns {boolean} True if the comment is at object start. - */ - function isCommentAtObjectStart(token) { - return ( - isCommentAtParentStart(token, "ObjectExpression") || - isCommentAtParentStart(token, "ObjectPattern") - ); - } - - /** - * Returns whether or not comments are at the object end or not. - * @param {token} token The Comment token. - * @returns {boolean} True if the comment is at object end. - */ - function isCommentAtObjectEnd(token) { - return ( - isCommentAtParentEnd(token, "ObjectExpression") || - isCommentAtParentEnd(token, "ObjectPattern") - ); - } - - /** - * Returns whether or not comments are at the array start or not. - * @param {token} token The Comment token. - * @returns {boolean} True if the comment is at array start. - */ - function isCommentAtArrayStart(token) { - return ( - isCommentAtParentStart(token, "ArrayExpression") || - isCommentAtParentStart(token, "ArrayPattern") - ); - } - - /** - * Returns whether or not comments are at the array end or not. - * @param {token} token The Comment token. - * @returns {boolean} True if the comment is at array end. - */ - function isCommentAtArrayEnd(token) { - return ( - isCommentAtParentEnd(token, "ArrayExpression") || - isCommentAtParentEnd(token, "ArrayPattern") - ); - } - - /** - * Checks if a comment token has lines around it (ignores inline comments) - * @param {token} token The Comment token. - * @param {Object} opts Options to determine the newline. - * @param {boolean} opts.after Should have a newline after this line. - * @param {boolean} opts.before Should have a newline before this line. - * @returns {void} - */ - function checkForEmptyLine(token, opts) { - if ( - applyDefaultIgnorePatterns && - defaultIgnoreRegExp.test(token.value) - ) { - return; - } - - if (ignorePattern && customIgnoreRegExp.test(token.value)) { - return; - } - - let after = opts.after, - before = opts.before; - - const prevLineNum = token.loc.start.line - 1, - nextLineNum = token.loc.end.line + 1, - commentIsNotAlone = codeAroundComment(token); - - const blockStartAllowed = - options.allowBlockStart && - isCommentAtBlockStart(token) && - !( - options.allowClassStart === false && - isCommentAtClassStart(token) - ), - blockEndAllowed = - options.allowBlockEnd && - isCommentAtBlockEnd(token) && - !( - options.allowClassEnd === false && - isCommentAtClassEnd(token) - ), - classStartAllowed = - options.allowClassStart && isCommentAtClassStart(token), - classEndAllowed = - options.allowClassEnd && isCommentAtClassEnd(token), - objectStartAllowed = - options.allowObjectStart && isCommentAtObjectStart(token), - objectEndAllowed = - options.allowObjectEnd && isCommentAtObjectEnd(token), - arrayStartAllowed = - options.allowArrayStart && isCommentAtArrayStart(token), - arrayEndAllowed = - options.allowArrayEnd && isCommentAtArrayEnd(token); - - const exceptionStartAllowed = - blockStartAllowed || - classStartAllowed || - objectStartAllowed || - arrayStartAllowed; - const exceptionEndAllowed = - blockEndAllowed || - classEndAllowed || - objectEndAllowed || - arrayEndAllowed; - - // ignore top of the file and bottom of the file - if (prevLineNum < 1) { - before = false; - } - if (nextLineNum >= numLines) { - after = false; - } - - // we ignore all inline comments - if (commentIsNotAlone) { - return; - } - - const previousTokenOrComment = sourceCode.getTokenBefore(token, { - includeComments: true, - }); - const nextTokenOrComment = sourceCode.getTokenAfter(token, { - includeComments: true, - }); - - // check for newline before - if ( - !exceptionStartAllowed && - before && - !commentAndEmptyLines.has(prevLineNum) && - !( - astUtils.isCommentToken(previousTokenOrComment) && - astUtils.isTokenOnSameLine(previousTokenOrComment, token) - ) - ) { - const lineStart = token.range[0] - token.loc.start.column; - const range = [lineStart, lineStart]; - - context.report({ - node: token, - messageId: "before", - fix(fixer) { - return fixer.insertTextBeforeRange(range, "\n"); - }, - }); - } - - // check for newline after - if ( - !exceptionEndAllowed && - after && - !commentAndEmptyLines.has(nextLineNum) && - !( - astUtils.isCommentToken(nextTokenOrComment) && - astUtils.isTokenOnSameLine(token, nextTokenOrComment) - ) - ) { - context.report({ - node: token, - messageId: "after", - fix(fixer) { - return fixer.insertTextAfter(token, "\n"); - }, - }); - } - } - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - return { - Program() { - comments.forEach(token => { - if (token.type === "Line") { - if ( - options.beforeLineComment || - options.afterLineComment - ) { - checkForEmptyLine(token, { - after: options.afterLineComment, - before: options.beforeLineComment, - }); - } - } else if (token.type === "Block") { - if ( - options.beforeBlockComment || - options.afterBlockComment - ) { - checkForEmptyLine(token, { - after: options.afterBlockComment, - before: options.beforeBlockComment, - }); - } - } else if (token.type === "Shebang") { - if (options.afterHashbangComment) { - checkForEmptyLine(token, { - after: options.afterHashbangComment, - before: false, - }); - } - } - }); - }, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Require empty lines around comments", + recommended: false, + url: "https://eslint.org/docs/latest/rules/lines-around-comment" + }, + + fixable: "whitespace", + + schema: [ + { + type: "object", + properties: { + beforeBlockComment: { + type: "boolean", + default: true + }, + afterBlockComment: { + type: "boolean", + default: false + }, + beforeLineComment: { + type: "boolean", + default: false + }, + afterLineComment: { + type: "boolean", + default: false + }, + allowBlockStart: { + type: "boolean", + default: false + }, + allowBlockEnd: { + type: "boolean", + default: false + }, + allowClassStart: { + type: "boolean" + }, + allowClassEnd: { + type: "boolean" + }, + allowObjectStart: { + type: "boolean" + }, + allowObjectEnd: { + type: "boolean" + }, + allowArrayStart: { + type: "boolean" + }, + allowArrayEnd: { + type: "boolean" + }, + ignorePattern: { + type: "string" + }, + applyDefaultIgnorePatterns: { + type: "boolean" + }, + afterHashbangComment: { + type: "boolean", + default: false + } + }, + additionalProperties: false + } + ], + messages: { + after: "Expected line after comment.", + before: "Expected line before comment." + } + }, + + create(context) { + + const options = Object.assign({}, context.options[0]); + const ignorePattern = options.ignorePattern; + const defaultIgnoreRegExp = astUtils.COMMENTS_IGNORE_PATTERN; + const customIgnoreRegExp = new RegExp(ignorePattern, "u"); + const applyDefaultIgnorePatterns = options.applyDefaultIgnorePatterns !== false; + + options.beforeBlockComment = typeof options.beforeBlockComment !== "undefined" ? options.beforeBlockComment : true; + + const sourceCode = context.sourceCode; + + const lines = sourceCode.lines, + numLines = lines.length + 1, + comments = sourceCode.getAllComments(), + commentLines = getCommentLineNums(comments), + emptyLines = getEmptyLineNums(lines), + commentAndEmptyLines = new Set(commentLines.concat(emptyLines)); + + /** + * Returns whether or not comments are on lines starting with or ending with code + * @param {token} token The comment token to check. + * @returns {boolean} True if the comment is not alone. + */ + function codeAroundComment(token) { + let currentToken = token; + + do { + currentToken = sourceCode.getTokenBefore(currentToken, { includeComments: true }); + } while (currentToken && astUtils.isCommentToken(currentToken)); + + if (currentToken && astUtils.isTokenOnSameLine(currentToken, token)) { + return true; + } + + currentToken = token; + do { + currentToken = sourceCode.getTokenAfter(currentToken, { includeComments: true }); + } while (currentToken && astUtils.isCommentToken(currentToken)); + + if (currentToken && astUtils.isTokenOnSameLine(token, currentToken)) { + return true; + } + + return false; + } + + /** + * Returns whether or not comments are inside a node type or not. + * @param {ASTNode} parent The Comment parent node. + * @param {string} nodeType The parent type to check against. + * @returns {boolean} True if the comment is inside nodeType. + */ + function isParentNodeType(parent, nodeType) { + return parent.type === nodeType || + (parent.body && parent.body.type === nodeType) || + (parent.consequent && parent.consequent.type === nodeType); + } + + /** + * Returns the parent node that contains the given token. + * @param {token} token The token to check. + * @returns {ASTNode|null} The parent node that contains the given token. + */ + function getParentNodeOfToken(token) { + const node = sourceCode.getNodeByRangeIndex(token.range[0]); + + /* + * For the purpose of this rule, the comment token is in a `StaticBlock` node only + * if it's inside the braces of that `StaticBlock` node. + * + * Example where this function returns `null`: + * + * static + * // comment + * { + * } + * + * Example where this function returns `StaticBlock` node: + * + * static + * { + * // comment + * } + * + */ + if (node && node.type === "StaticBlock") { + const openingBrace = sourceCode.getFirstToken(node, { skip: 1 }); // skip the `static` token + + return token.range[0] >= openingBrace.range[0] + ? node + : null; + } + + return node; + } + + /** + * Returns whether or not comments are at the parent start or not. + * @param {token} token The Comment token. + * @param {string} nodeType The parent type to check against. + * @returns {boolean} True if the comment is at parent start. + */ + function isCommentAtParentStart(token, nodeType) { + const parent = getParentNodeOfToken(token); + + if (parent && isParentNodeType(parent, nodeType)) { + let parentStartNodeOrToken = parent; + + if (parent.type === "StaticBlock") { + parentStartNodeOrToken = sourceCode.getFirstToken(parent, { skip: 1 }); // opening brace of the static block + } else if (parent.type === "SwitchStatement") { + parentStartNodeOrToken = sourceCode.getTokenAfter(parent.discriminant, { + filter: astUtils.isOpeningBraceToken + }); // opening brace of the switch statement + } + + return token.loc.start.line - parentStartNodeOrToken.loc.start.line === 1; + } + + return false; + } + + /** + * Returns whether or not comments are at the parent end or not. + * @param {token} token The Comment token. + * @param {string} nodeType The parent type to check against. + * @returns {boolean} True if the comment is at parent end. + */ + function isCommentAtParentEnd(token, nodeType) { + const parent = getParentNodeOfToken(token); + + return !!parent && isParentNodeType(parent, nodeType) && + parent.loc.end.line - token.loc.end.line === 1; + } + + /** + * Returns whether or not comments are at the block start or not. + * @param {token} token The Comment token. + * @returns {boolean} True if the comment is at block start. + */ + function isCommentAtBlockStart(token) { + return ( + isCommentAtParentStart(token, "ClassBody") || + isCommentAtParentStart(token, "BlockStatement") || + isCommentAtParentStart(token, "StaticBlock") || + isCommentAtParentStart(token, "SwitchCase") || + isCommentAtParentStart(token, "SwitchStatement") + ); + } + + /** + * Returns whether or not comments are at the block end or not. + * @param {token} token The Comment token. + * @returns {boolean} True if the comment is at block end. + */ + function isCommentAtBlockEnd(token) { + return ( + isCommentAtParentEnd(token, "ClassBody") || + isCommentAtParentEnd(token, "BlockStatement") || + isCommentAtParentEnd(token, "StaticBlock") || + isCommentAtParentEnd(token, "SwitchCase") || + isCommentAtParentEnd(token, "SwitchStatement") + ); + } + + /** + * Returns whether or not comments are at the class start or not. + * @param {token} token The Comment token. + * @returns {boolean} True if the comment is at class start. + */ + function isCommentAtClassStart(token) { + return isCommentAtParentStart(token, "ClassBody"); + } + + /** + * Returns whether or not comments are at the class end or not. + * @param {token} token The Comment token. + * @returns {boolean} True if the comment is at class end. + */ + function isCommentAtClassEnd(token) { + return isCommentAtParentEnd(token, "ClassBody"); + } + + /** + * Returns whether or not comments are at the object start or not. + * @param {token} token The Comment token. + * @returns {boolean} True if the comment is at object start. + */ + function isCommentAtObjectStart(token) { + return isCommentAtParentStart(token, "ObjectExpression") || isCommentAtParentStart(token, "ObjectPattern"); + } + + /** + * Returns whether or not comments are at the object end or not. + * @param {token} token The Comment token. + * @returns {boolean} True if the comment is at object end. + */ + function isCommentAtObjectEnd(token) { + return isCommentAtParentEnd(token, "ObjectExpression") || isCommentAtParentEnd(token, "ObjectPattern"); + } + + /** + * Returns whether or not comments are at the array start or not. + * @param {token} token The Comment token. + * @returns {boolean} True if the comment is at array start. + */ + function isCommentAtArrayStart(token) { + return isCommentAtParentStart(token, "ArrayExpression") || isCommentAtParentStart(token, "ArrayPattern"); + } + + /** + * Returns whether or not comments are at the array end or not. + * @param {token} token The Comment token. + * @returns {boolean} True if the comment is at array end. + */ + function isCommentAtArrayEnd(token) { + return isCommentAtParentEnd(token, "ArrayExpression") || isCommentAtParentEnd(token, "ArrayPattern"); + } + + /** + * Checks if a comment token has lines around it (ignores inline comments) + * @param {token} token The Comment token. + * @param {Object} opts Options to determine the newline. + * @param {boolean} opts.after Should have a newline after this line. + * @param {boolean} opts.before Should have a newline before this line. + * @returns {void} + */ + function checkForEmptyLine(token, opts) { + if (applyDefaultIgnorePatterns && defaultIgnoreRegExp.test(token.value)) { + return; + } + + if (ignorePattern && customIgnoreRegExp.test(token.value)) { + return; + } + + let after = opts.after, + before = opts.before; + + const prevLineNum = token.loc.start.line - 1, + nextLineNum = token.loc.end.line + 1, + commentIsNotAlone = codeAroundComment(token); + + const blockStartAllowed = options.allowBlockStart && + isCommentAtBlockStart(token) && + !(options.allowClassStart === false && + isCommentAtClassStart(token)), + blockEndAllowed = options.allowBlockEnd && isCommentAtBlockEnd(token) && !(options.allowClassEnd === false && isCommentAtClassEnd(token)), + classStartAllowed = options.allowClassStart && isCommentAtClassStart(token), + classEndAllowed = options.allowClassEnd && isCommentAtClassEnd(token), + objectStartAllowed = options.allowObjectStart && isCommentAtObjectStart(token), + objectEndAllowed = options.allowObjectEnd && isCommentAtObjectEnd(token), + arrayStartAllowed = options.allowArrayStart && isCommentAtArrayStart(token), + arrayEndAllowed = options.allowArrayEnd && isCommentAtArrayEnd(token); + + const exceptionStartAllowed = blockStartAllowed || classStartAllowed || objectStartAllowed || arrayStartAllowed; + const exceptionEndAllowed = blockEndAllowed || classEndAllowed || objectEndAllowed || arrayEndAllowed; + + // ignore top of the file and bottom of the file + if (prevLineNum < 1) { + before = false; + } + if (nextLineNum >= numLines) { + after = false; + } + + // we ignore all inline comments + if (commentIsNotAlone) { + return; + } + + const previousTokenOrComment = sourceCode.getTokenBefore(token, { includeComments: true }); + const nextTokenOrComment = sourceCode.getTokenAfter(token, { includeComments: true }); + + // check for newline before + if (!exceptionStartAllowed && before && !commentAndEmptyLines.has(prevLineNum) && + !(astUtils.isCommentToken(previousTokenOrComment) && astUtils.isTokenOnSameLine(previousTokenOrComment, token))) { + const lineStart = token.range[0] - token.loc.start.column; + const range = [lineStart, lineStart]; + + context.report({ + node: token, + messageId: "before", + fix(fixer) { + return fixer.insertTextBeforeRange(range, "\n"); + } + }); + } + + // check for newline after + if (!exceptionEndAllowed && after && !commentAndEmptyLines.has(nextLineNum) && + !(astUtils.isCommentToken(nextTokenOrComment) && astUtils.isTokenOnSameLine(token, nextTokenOrComment))) { + context.report({ + node: token, + messageId: "after", + fix(fixer) { + return fixer.insertTextAfter(token, "\n"); + } + }); + } + + } + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + return { + Program() { + comments.forEach(token => { + if (token.type === "Line") { + if (options.beforeLineComment || options.afterLineComment) { + checkForEmptyLine(token, { + after: options.afterLineComment, + before: options.beforeLineComment + }); + } + } else if (token.type === "Block") { + if (options.beforeBlockComment || options.afterBlockComment) { + checkForEmptyLine(token, { + after: options.afterBlockComment, + before: options.beforeBlockComment + }); + } + } else if (token.type === "Shebang") { + if (options.afterHashbangComment) { + checkForEmptyLine(token, { + after: options.afterHashbangComment, + before: false + }); + } + } + }); + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/lines-around-directive.js b/node_modules/eslint/lib/rules/lines-around-directive.js index bf57154b3..1c82d8f98 100644 --- a/node_modules/eslint/lib/rules/lines-around-directive.js +++ b/node_modules/eslint/lib/rules/lines-around-directive.js @@ -12,238 +12,190 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "layout", - - docs: { - description: "Require or disallow newlines around directives", - recommended: false, - url: "https://eslint.org/docs/latest/rules/lines-around-directive", - }, - - schema: [ - { - oneOf: [ - { - enum: ["always", "never"], - }, - { - type: "object", - properties: { - before: { - enum: ["always", "never"], - }, - after: { - enum: ["always", "never"], - }, - }, - additionalProperties: false, - minProperties: 2, - }, - ], - }, - ], - - fixable: "whitespace", - messages: { - expected: 'Expected newline {{location}} "{{value}}" directive.', - unexpected: - 'Unexpected newline {{location}} "{{value}}" directive.', - }, - deprecated: { - message: "The rule was replaced with a more general rule.", - url: "https://eslint.org/blog/2017/06/eslint-v4.0.0-released/", - deprecatedSince: "4.0.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: "The new rule moved to a plugin.", - url: "https://eslint.org/docs/latest/rules/padding-line-between-statements#examples", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "padding-line-between-statements", - url: "https://eslint.style/rules/padding-line-between-statements", - }, - }, - ], - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - const config = context.options[0] || "always"; - const expectLineBefore = - typeof config === "string" ? config : config.before; - const expectLineAfter = - typeof config === "string" ? config : config.after; - - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - /** - * Check if node is preceded by a blank newline. - * @param {ASTNode} node Node to check. - * @returns {boolean} Whether or not the passed in node is preceded by a blank newline. - */ - function hasNewlineBefore(node) { - const tokenBefore = sourceCode.getTokenBefore(node, { - includeComments: true, - }); - const tokenLineBefore = tokenBefore ? tokenBefore.loc.end.line : 0; - - return node.loc.start.line - tokenLineBefore >= 2; - } - - /** - * Gets the last token of a node that is on the same line as the rest of the node. - * This will usually be the last token of the node, but it will be the second-to-last token if the node has a trailing - * semicolon on a different line. - * @param {ASTNode} node A directive node - * @returns {Token} The last token of the node on the line - */ - function getLastTokenOnLine(node) { - const lastToken = sourceCode.getLastToken(node); - const secondToLastToken = sourceCode.getTokenBefore(lastToken); - - return astUtils.isSemicolonToken(lastToken) && - lastToken.loc.start.line > secondToLastToken.loc.end.line - ? secondToLastToken - : lastToken; - } - - /** - * Check if node is followed by a blank newline. - * @param {ASTNode} node Node to check. - * @returns {boolean} Whether or not the passed in node is followed by a blank newline. - */ - function hasNewlineAfter(node) { - const lastToken = getLastTokenOnLine(node); - const tokenAfter = sourceCode.getTokenAfter(lastToken, { - includeComments: true, - }); - - return tokenAfter.loc.start.line - lastToken.loc.end.line >= 2; - } - - /** - * Report errors for newlines around directives. - * @param {ASTNode} node Node to check. - * @param {string} location Whether the error was found before or after the directive. - * @param {boolean} expected Whether or not a newline was expected or unexpected. - * @returns {void} - */ - function reportError(node, location, expected) { - context.report({ - node, - messageId: expected ? "expected" : "unexpected", - data: { - value: node.expression.value, - location, - }, - fix(fixer) { - const lastToken = getLastTokenOnLine(node); - - if (expected) { - return location === "before" - ? fixer.insertTextBefore(node, "\n") - : fixer.insertTextAfter(lastToken, "\n"); - } - return fixer.removeRange( - location === "before" - ? [node.range[0] - 1, node.range[0]] - : [lastToken.range[1], lastToken.range[1] + 1], - ); - }, - }); - } - - /** - * Check lines around directives in node - * @param {ASTNode} node node to check - * @returns {void} - */ - function checkDirectives(node) { - const directives = astUtils.getDirectivePrologue(node); - - if (!directives.length) { - return; - } - - const firstDirective = directives[0]; - const leadingComments = - sourceCode.getCommentsBefore(firstDirective); - - /* - * Only check before the first directive if it is preceded by a comment or if it is at the top of - * the file and expectLineBefore is set to "never". This is to not force a newline at the top of - * the file if there are no comments as well as for compatibility with padded-blocks. - */ - if (leadingComments.length) { - if ( - expectLineBefore === "always" && - !hasNewlineBefore(firstDirective) - ) { - reportError(firstDirective, "before", true); - } - - if ( - expectLineBefore === "never" && - hasNewlineBefore(firstDirective) - ) { - reportError(firstDirective, "before", false); - } - } else if ( - node.type === "Program" && - expectLineBefore === "never" && - !leadingComments.length && - hasNewlineBefore(firstDirective) - ) { - reportError(firstDirective, "before", false); - } - - const lastDirective = directives.at(-1); - const statements = - node.type === "Program" ? node.body : node.body.body; - - /* - * Do not check after the last directive if the body only - * contains a directive prologue and isn't followed by a comment to ensure - * this rule behaves well with padded-blocks. - */ - if ( - lastDirective === statements.at(-1) && - !lastDirective.trailingComments - ) { - return; - } - - if ( - expectLineAfter === "always" && - !hasNewlineAfter(lastDirective) - ) { - reportError(lastDirective, "after", true); - } - - if (expectLineAfter === "never" && hasNewlineAfter(lastDirective)) { - reportError(lastDirective, "after", false); - } - } - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - return { - Program: checkDirectives, - FunctionDeclaration: checkDirectives, - FunctionExpression: checkDirectives, - ArrowFunctionExpression: checkDirectives, - }; - }, + meta: { + type: "layout", + + docs: { + description: "Require or disallow newlines around directives", + recommended: false, + url: "https://eslint.org/docs/latest/rules/lines-around-directive" + }, + + schema: [{ + oneOf: [ + { + enum: ["always", "never"] + }, + { + type: "object", + properties: { + before: { + enum: ["always", "never"] + }, + after: { + enum: ["always", "never"] + } + }, + additionalProperties: false, + minProperties: 2 + } + ] + }], + + fixable: "whitespace", + messages: { + expected: "Expected newline {{location}} \"{{value}}\" directive.", + unexpected: "Unexpected newline {{location}} \"{{value}}\" directive." + }, + deprecated: true, + replacedBy: ["padding-line-between-statements"] + }, + + create(context) { + const sourceCode = context.sourceCode; + const config = context.options[0] || "always"; + const expectLineBefore = typeof config === "string" ? config : config.before; + const expectLineAfter = typeof config === "string" ? config : config.after; + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + /** + * Check if node is preceded by a blank newline. + * @param {ASTNode} node Node to check. + * @returns {boolean} Whether or not the passed in node is preceded by a blank newline. + */ + function hasNewlineBefore(node) { + const tokenBefore = sourceCode.getTokenBefore(node, { includeComments: true }); + const tokenLineBefore = tokenBefore ? tokenBefore.loc.end.line : 0; + + return node.loc.start.line - tokenLineBefore >= 2; + } + + /** + * Gets the last token of a node that is on the same line as the rest of the node. + * This will usually be the last token of the node, but it will be the second-to-last token if the node has a trailing + * semicolon on a different line. + * @param {ASTNode} node A directive node + * @returns {Token} The last token of the node on the line + */ + function getLastTokenOnLine(node) { + const lastToken = sourceCode.getLastToken(node); + const secondToLastToken = sourceCode.getTokenBefore(lastToken); + + return astUtils.isSemicolonToken(lastToken) && lastToken.loc.start.line > secondToLastToken.loc.end.line + ? secondToLastToken + : lastToken; + } + + /** + * Check if node is followed by a blank newline. + * @param {ASTNode} node Node to check. + * @returns {boolean} Whether or not the passed in node is followed by a blank newline. + */ + function hasNewlineAfter(node) { + const lastToken = getLastTokenOnLine(node); + const tokenAfter = sourceCode.getTokenAfter(lastToken, { includeComments: true }); + + return tokenAfter.loc.start.line - lastToken.loc.end.line >= 2; + } + + /** + * Report errors for newlines around directives. + * @param {ASTNode} node Node to check. + * @param {string} location Whether the error was found before or after the directive. + * @param {boolean} expected Whether or not a newline was expected or unexpected. + * @returns {void} + */ + function reportError(node, location, expected) { + context.report({ + node, + messageId: expected ? "expected" : "unexpected", + data: { + value: node.expression.value, + location + }, + fix(fixer) { + const lastToken = getLastTokenOnLine(node); + + if (expected) { + return location === "before" ? fixer.insertTextBefore(node, "\n") : fixer.insertTextAfter(lastToken, "\n"); + } + return fixer.removeRange(location === "before" ? [node.range[0] - 1, node.range[0]] : [lastToken.range[1], lastToken.range[1] + 1]); + } + }); + } + + /** + * Check lines around directives in node + * @param {ASTNode} node node to check + * @returns {void} + */ + function checkDirectives(node) { + const directives = astUtils.getDirectivePrologue(node); + + if (!directives.length) { + return; + } + + const firstDirective = directives[0]; + const leadingComments = sourceCode.getCommentsBefore(firstDirective); + + /* + * Only check before the first directive if it is preceded by a comment or if it is at the top of + * the file and expectLineBefore is set to "never". This is to not force a newline at the top of + * the file if there are no comments as well as for compatibility with padded-blocks. + */ + if (leadingComments.length) { + if (expectLineBefore === "always" && !hasNewlineBefore(firstDirective)) { + reportError(firstDirective, "before", true); + } + + if (expectLineBefore === "never" && hasNewlineBefore(firstDirective)) { + reportError(firstDirective, "before", false); + } + } else if ( + node.type === "Program" && + expectLineBefore === "never" && + !leadingComments.length && + hasNewlineBefore(firstDirective) + ) { + reportError(firstDirective, "before", false); + } + + const lastDirective = directives[directives.length - 1]; + const statements = node.type === "Program" ? node.body : node.body.body; + + /* + * Do not check after the last directive if the body only + * contains a directive prologue and isn't followed by a comment to ensure + * this rule behaves well with padded-blocks. + */ + if (lastDirective === statements[statements.length - 1] && !lastDirective.trailingComments) { + return; + } + + if (expectLineAfter === "always" && !hasNewlineAfter(lastDirective)) { + reportError(lastDirective, "after", true); + } + + if (expectLineAfter === "never" && hasNewlineAfter(lastDirective)) { + reportError(lastDirective, "after", false); + } + } + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + return { + Program: checkDirectives, + FunctionDeclaration: checkDirectives, + FunctionExpression: checkDirectives, + ArrowFunctionExpression: checkDirectives + }; + } }; diff --git a/node_modules/eslint/lib/rules/lines-between-class-members.js b/node_modules/eslint/lib/rules/lines-between-class-members.js index 896169985..5f36d468d 100644 --- a/node_modules/eslint/lib/rules/lines-between-class-members.js +++ b/node_modules/eslint/lib/rules/lines-between-class-members.js @@ -21,338 +21,249 @@ const astUtils = require("./utils/ast-utils"); * @private */ const ClassMemberTypes = { - "*": { test: () => true }, - field: { test: node => node.type === "PropertyDefinition" }, - method: { test: node => node.type === "MethodDefinition" }, + "*": { test: () => true }, + field: { test: node => node.type === "PropertyDefinition" }, + method: { test: node => node.type === "MethodDefinition" } }; //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "lines-between-class-members", - url: "https://eslint.style/rules/lines-between-class-members", - }, - }, - ], - }, - type: "layout", + meta: { + deprecated: true, + replacedBy: [], + type: "layout", - docs: { - description: - "Require or disallow an empty line between class members", - recommended: false, - url: "https://eslint.org/docs/latest/rules/lines-between-class-members", - }, + docs: { + description: "Require or disallow an empty line between class members", + recommended: false, + url: "https://eslint.org/docs/latest/rules/lines-between-class-members" + }, - fixable: "whitespace", + fixable: "whitespace", - schema: [ - { - anyOf: [ - { - type: "object", - properties: { - enforce: { - type: "array", - items: { - type: "object", - properties: { - blankLine: { - enum: ["always", "never"], - }, - prev: { - enum: ["method", "field", "*"], - }, - next: { - enum: ["method", "field", "*"], - }, - }, - additionalProperties: false, - required: ["blankLine", "prev", "next"], - }, - minItems: 1, - }, - }, - additionalProperties: false, - required: ["enforce"], - }, - { - enum: ["always", "never"], - }, - ], - }, - { - type: "object", - properties: { - exceptAfterSingleLine: { - type: "boolean", - default: false, - }, - }, - additionalProperties: false, - }, - ], - messages: { - never: "Unexpected blank line between class members.", - always: "Expected blank line between class members.", - }, - }, + schema: [ + { + anyOf: [ + { + type: "object", + properties: { + enforce: { + type: "array", + items: { + type: "object", + properties: { + blankLine: { enum: ["always", "never"] }, + prev: { enum: ["method", "field", "*"] }, + next: { enum: ["method", "field", "*"] } + }, + additionalProperties: false, + required: ["blankLine", "prev", "next"] + }, + minItems: 1 + } + }, + additionalProperties: false, + required: ["enforce"] + }, + { + enum: ["always", "never"] + } + ] + }, + { + type: "object", + properties: { + exceptAfterSingleLine: { + type: "boolean", + default: false + } + }, + additionalProperties: false + } + ], + messages: { + never: "Unexpected blank line between class members.", + always: "Expected blank line between class members." + } + }, - create(context) { - const options = []; + create(context) { - options[0] = context.options[0] || "always"; - options[1] = context.options[1] || { exceptAfterSingleLine: false }; + const options = []; - const configureList = - typeof options[0] === "object" - ? options[0].enforce - : [{ blankLine: options[0], prev: "*", next: "*" }]; - const sourceCode = context.sourceCode; + options[0] = context.options[0] || "always"; + options[1] = context.options[1] || { exceptAfterSingleLine: false }; - /** - * Gets a pair of tokens that should be used to check lines between two class member nodes. - * - * In most cases, this returns the very last token of the current node and - * the very first token of the next node. - * For example: - * - * class C { - * x = 1; // curLast: `;` nextFirst: `in` - * in = 2 - * } - * - * There is only one exception. If the given node ends with a semicolon, and it looks like - * a semicolon-less style's semicolon - one that is not on the same line as the preceding - * token, but is on the line where the next class member starts - this returns the preceding - * token and the semicolon as boundary tokens. - * For example: - * - * class C { - * x = 1 // curLast: `1` nextFirst: `;` - * ;in = 2 - * } - * When determining the desired layout of the code, we should treat this semicolon as - * a part of the next class member node instead of the one it technically belongs to. - * @param {ASTNode} curNode Current class member node. - * @param {ASTNode} nextNode Next class member node. - * @returns {Token} The actual last token of `node`. - * @private - */ - function getBoundaryTokens(curNode, nextNode) { - const lastToken = sourceCode.getLastToken(curNode); - const prevToken = sourceCode.getTokenBefore(lastToken); - const nextToken = sourceCode.getFirstToken(nextNode); // skip possible lone `;` between nodes + const configureList = typeof options[0] === "object" ? options[0].enforce : [{ blankLine: options[0], prev: "*", next: "*" }]; + const sourceCode = context.sourceCode; - const isSemicolonLessStyle = - astUtils.isSemicolonToken(lastToken) && - !astUtils.isTokenOnSameLine(prevToken, lastToken) && - astUtils.isTokenOnSameLine(lastToken, nextToken); + /** + * Gets a pair of tokens that should be used to check lines between two class member nodes. + * + * In most cases, this returns the very last token of the current node and + * the very first token of the next node. + * For example: + * + * class C { + * x = 1; // curLast: `;` nextFirst: `in` + * in = 2 + * } + * + * There is only one exception. If the given node ends with a semicolon, and it looks like + * a semicolon-less style's semicolon - one that is not on the same line as the preceding + * token, but is on the line where the next class member starts - this returns the preceding + * token and the semicolon as boundary tokens. + * For example: + * + * class C { + * x = 1 // curLast: `1` nextFirst: `;` + * ;in = 2 + * } + * When determining the desired layout of the code, we should treat this semicolon as + * a part of the next class member node instead of the one it technically belongs to. + * @param {ASTNode} curNode Current class member node. + * @param {ASTNode} nextNode Next class member node. + * @returns {Token} The actual last token of `node`. + * @private + */ + function getBoundaryTokens(curNode, nextNode) { + const lastToken = sourceCode.getLastToken(curNode); + const prevToken = sourceCode.getTokenBefore(lastToken); + const nextToken = sourceCode.getFirstToken(nextNode); // skip possible lone `;` between nodes - return isSemicolonLessStyle - ? { curLast: prevToken, nextFirst: lastToken } - : { curLast: lastToken, nextFirst: nextToken }; - } + const isSemicolonLessStyle = ( + astUtils.isSemicolonToken(lastToken) && + !astUtils.isTokenOnSameLine(prevToken, lastToken) && + astUtils.isTokenOnSameLine(lastToken, nextToken) + ); - /** - * Return the last token among the consecutive tokens that have no exceed max line difference in between, before the first token in the next member. - * @param {Token} prevLastToken The last token in the previous member node. - * @param {Token} nextFirstToken The first token in the next member node. - * @param {number} maxLine The maximum number of allowed line difference between consecutive tokens. - * @returns {Token} The last token among the consecutive tokens. - */ - function findLastConsecutiveTokenAfter( - prevLastToken, - nextFirstToken, - maxLine, - ) { - const after = sourceCode.getTokenAfter(prevLastToken, { - includeComments: true, - }); + return isSemicolonLessStyle + ? { curLast: prevToken, nextFirst: lastToken } + : { curLast: lastToken, nextFirst: nextToken }; + } - if ( - after !== nextFirstToken && - after.loc.start.line - prevLastToken.loc.end.line <= maxLine - ) { - return findLastConsecutiveTokenAfter( - after, - nextFirstToken, - maxLine, - ); - } - return prevLastToken; - } + /** + * Return the last token among the consecutive tokens that have no exceed max line difference in between, before the first token in the next member. + * @param {Token} prevLastToken The last token in the previous member node. + * @param {Token} nextFirstToken The first token in the next member node. + * @param {number} maxLine The maximum number of allowed line difference between consecutive tokens. + * @returns {Token} The last token among the consecutive tokens. + */ + function findLastConsecutiveTokenAfter(prevLastToken, nextFirstToken, maxLine) { + const after = sourceCode.getTokenAfter(prevLastToken, { includeComments: true }); - /** - * Return the first token among the consecutive tokens that have no exceed max line difference in between, after the last token in the previous member. - * @param {Token} nextFirstToken The first token in the next member node. - * @param {Token} prevLastToken The last token in the previous member node. - * @param {number} maxLine The maximum number of allowed line difference between consecutive tokens. - * @returns {Token} The first token among the consecutive tokens. - */ - function findFirstConsecutiveTokenBefore( - nextFirstToken, - prevLastToken, - maxLine, - ) { - const before = sourceCode.getTokenBefore(nextFirstToken, { - includeComments: true, - }); + if (after !== nextFirstToken && after.loc.start.line - prevLastToken.loc.end.line <= maxLine) { + return findLastConsecutiveTokenAfter(after, nextFirstToken, maxLine); + } + return prevLastToken; + } - if ( - before !== prevLastToken && - nextFirstToken.loc.start.line - before.loc.end.line <= maxLine - ) { - return findFirstConsecutiveTokenBefore( - before, - prevLastToken, - maxLine, - ); - } - return nextFirstToken; - } + /** + * Return the first token among the consecutive tokens that have no exceed max line difference in between, after the last token in the previous member. + * @param {Token} nextFirstToken The first token in the next member node. + * @param {Token} prevLastToken The last token in the previous member node. + * @param {number} maxLine The maximum number of allowed line difference between consecutive tokens. + * @returns {Token} The first token among the consecutive tokens. + */ + function findFirstConsecutiveTokenBefore(nextFirstToken, prevLastToken, maxLine) { + const before = sourceCode.getTokenBefore(nextFirstToken, { includeComments: true }); - /** - * Checks if there is a token or comment between two tokens. - * @param {Token} before The token before. - * @param {Token} after The token after. - * @returns {boolean} True if there is a token or comment between two tokens. - */ - function hasTokenOrCommentBetween(before, after) { - return ( - sourceCode.getTokensBetween(before, after, { - includeComments: true, - }).length !== 0 - ); - } + if (before !== prevLastToken && nextFirstToken.loc.start.line - before.loc.end.line <= maxLine) { + return findFirstConsecutiveTokenBefore(before, prevLastToken, maxLine); + } + return nextFirstToken; + } - /** - * Checks whether the given node matches the given type. - * @param {ASTNode} node The class member node to check. - * @param {string} type The class member type to check. - * @returns {boolean} `true` if the class member node matched the type. - * @private - */ - function match(node, type) { - return ClassMemberTypes[type].test(node); - } + /** + * Checks if there is a token or comment between two tokens. + * @param {Token} before The token before. + * @param {Token} after The token after. + * @returns {boolean} True if there is a token or comment between two tokens. + */ + function hasTokenOrCommentBetween(before, after) { + return sourceCode.getTokensBetween(before, after, { includeComments: true }).length !== 0; + } - /** - * Finds the last matched configuration from the configureList. - * @param {ASTNode} prevNode The previous node to match. - * @param {ASTNode} nextNode The current node to match. - * @returns {string|null} Padding type or `null` if no matches were found. - * @private - */ - function getPaddingType(prevNode, nextNode) { - for (let i = configureList.length - 1; i >= 0; --i) { - const configure = configureList[i]; - const matched = - match(prevNode, configure.prev) && - match(nextNode, configure.next); + /** + * Checks whether the given node matches the given type. + * @param {ASTNode} node The class member node to check. + * @param {string} type The class member type to check. + * @returns {boolean} `true` if the class member node matched the type. + * @private + */ + function match(node, type) { + return ClassMemberTypes[type].test(node); + } - if (matched) { - return configure.blankLine; - } - } - return null; - } + /** + * Finds the last matched configuration from the configureList. + * @param {ASTNode} prevNode The previous node to match. + * @param {ASTNode} nextNode The current node to match. + * @returns {string|null} Padding type or `null` if no matches were found. + * @private + */ + function getPaddingType(prevNode, nextNode) { + for (let i = configureList.length - 1; i >= 0; --i) { + const configure = configureList[i]; + const matched = + match(prevNode, configure.prev) && + match(nextNode, configure.next); - return { - ClassBody(node) { - const body = node.body; + if (matched) { + return configure.blankLine; + } + } + return null; + } - for (let i = 0; i < body.length - 1; i++) { - const curFirst = sourceCode.getFirstToken(body[i]); - const { curLast, nextFirst } = getBoundaryTokens( - body[i], - body[i + 1], - ); - const isMulti = !astUtils.isTokenOnSameLine( - curFirst, - curLast, - ); - const skip = !isMulti && options[1].exceptAfterSingleLine; - const beforePadding = findLastConsecutiveTokenAfter( - curLast, - nextFirst, - 1, - ); - const afterPadding = findFirstConsecutiveTokenBefore( - nextFirst, - curLast, - 1, - ); - const isPadded = - afterPadding.loc.start.line - - beforePadding.loc.end.line > - 1; - const hasTokenInPadding = hasTokenOrCommentBetween( - beforePadding, - afterPadding, - ); - const curLineLastToken = findLastConsecutiveTokenAfter( - curLast, - nextFirst, - 0, - ); - const paddingType = getPaddingType(body[i], body[i + 1]); + return { + ClassBody(node) { + const body = node.body; - if (paddingType === "never" && isPadded) { - context.report({ - node: body[i + 1], - messageId: "never", + for (let i = 0; i < body.length - 1; i++) { + const curFirst = sourceCode.getFirstToken(body[i]); + const { curLast, nextFirst } = getBoundaryTokens(body[i], body[i + 1]); + const isMulti = !astUtils.isTokenOnSameLine(curFirst, curLast); + const skip = !isMulti && options[1].exceptAfterSingleLine; + const beforePadding = findLastConsecutiveTokenAfter(curLast, nextFirst, 1); + const afterPadding = findFirstConsecutiveTokenBefore(nextFirst, curLast, 1); + const isPadded = afterPadding.loc.start.line - beforePadding.loc.end.line > 1; + const hasTokenInPadding = hasTokenOrCommentBetween(beforePadding, afterPadding); + const curLineLastToken = findLastConsecutiveTokenAfter(curLast, nextFirst, 0); + const paddingType = getPaddingType(body[i], body[i + 1]); - fix(fixer) { - if (hasTokenInPadding) { - return null; - } - return fixer.replaceTextRange( - [ - beforePadding.range[1], - afterPadding.range[0], - ], - "\n", - ); - }, - }); - } else if (paddingType === "always" && !skip && !isPadded) { - context.report({ - node: body[i + 1], - messageId: "always", + if (paddingType === "never" && isPadded) { + context.report({ + node: body[i + 1], + messageId: "never", - fix(fixer) { - if (hasTokenInPadding) { - return null; - } - return fixer.insertTextAfter( - curLineLastToken, - "\n", - ); - }, - }); - } - } - }, - }; - }, + fix(fixer) { + if (hasTokenInPadding) { + return null; + } + return fixer.replaceTextRange([beforePadding.range[1], afterPadding.range[0]], "\n"); + } + }); + } else if (paddingType === "always" && !skip && !isPadded) { + context.report({ + node: body[i + 1], + messageId: "always", + + fix(fixer) { + if (hasTokenInPadding) { + return null; + } + return fixer.insertTextAfter(curLineLastToken, "\n"); + } + }); + } + + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/logical-assignment-operators.js b/node_modules/eslint/lib/rules/logical-assignment-operators.js index 38103f16c..c084c04c8 100644 --- a/node_modules/eslint/lib/rules/logical-assignment-operators.js +++ b/node_modules/eslint/lib/rules/logical-assignment-operators.js @@ -22,16 +22,14 @@ const baseTypes = new Set(["Identifier", "Super", "ThisExpression"]); * @returns {boolean} True iff "undefined" or "void ..." */ function isUndefined(expression, scope) { - if (expression.type === "Identifier" && expression.name === "undefined") { - return astUtils.isReferenceToGlobalVariable(scope, expression); - } - - return ( - expression.type === "UnaryExpression" && - expression.operator === "void" && - expression.argument.type === "Literal" && - expression.argument.value === 0 - ); + if (expression.type === "Identifier" && expression.name === "undefined") { + return astUtils.isReferenceToGlobalVariable(scope, expression); + } + + return expression.type === "UnaryExpression" && + expression.operator === "void" && + expression.argument.type === "Literal" && + expression.argument.value === 0; } /** @@ -40,10 +38,8 @@ function isUndefined(expression, scope) { * @returns {boolean} True for identifiers and member expressions */ function isReference(expression) { - return ( - (expression.type === "Identifier" && expression.name !== "undefined") || - expression.type === "MemberExpression" - ); + return (expression.type === "Identifier" && expression.name !== "undefined") || + expression.type === "MemberExpression"; } /** @@ -54,21 +50,15 @@ function isReference(expression) { * @returns {boolean} True iff implicit nullish comparison */ function isImplicitNullishComparison(expression, scope) { - if ( - expression.type !== "BinaryExpression" || - expression.operator !== "==" - ) { - return false; - } - - const reference = isReference(expression.left) ? "left" : "right"; - const nullish = reference === "left" ? "right" : "left"; - - return ( - isReference(expression[reference]) && - (astUtils.isNullLiteral(expression[nullish]) || - isUndefined(expression[nullish], scope)) - ); + if (expression.type !== "BinaryExpression" || expression.operator !== "==") { + return false; + } + + const reference = isReference(expression.left) ? "left" : "right"; + const nullish = reference === "left" ? "right" : "left"; + + return isReference(expression[reference]) && + (astUtils.isNullLiteral(expression[nullish]) || isUndefined(expression[nullish], scope)); } /** @@ -77,14 +67,12 @@ function isImplicitNullishComparison(expression, scope) { * @returns {boolean} True iff matches ? === ? || ? === ? */ function isDoubleComparison(expression) { - return ( - expression.type === "LogicalExpression" && - expression.operator === "||" && - expression.left.type === "BinaryExpression" && - expression.left.operator === "===" && - expression.right.type === "BinaryExpression" && - expression.right.operator === "===" - ); + return expression.type === "LogicalExpression" && + expression.operator === "||" && + expression.left.type === "BinaryExpression" && + expression.left.operator === "===" && + expression.right.type === "BinaryExpression" && + expression.right.operator === "==="; } /** @@ -95,26 +83,17 @@ function isDoubleComparison(expression) { * @returns {boolean} True iff explicit nullish comparison */ function isExplicitNullishComparison(expression, scope) { - if (!isDoubleComparison(expression)) { - return false; - } - const leftReference = isReference(expression.left.left) ? "left" : "right"; - const leftNullish = leftReference === "left" ? "right" : "left"; - const rightReference = isReference(expression.right.left) - ? "left" - : "right"; - const rightNullish = rightReference === "left" ? "right" : "left"; - - return ( - astUtils.isSameReference( - expression.left[leftReference], - expression.right[rightReference], - ) && - ((astUtils.isNullLiteral(expression.left[leftNullish]) && - isUndefined(expression.right[rightNullish], scope)) || - (isUndefined(expression.left[leftNullish], scope) && - astUtils.isNullLiteral(expression.right[rightNullish]))) - ); + if (!isDoubleComparison(expression)) { + return false; + } + const leftReference = isReference(expression.left.left) ? "left" : "right"; + const leftNullish = leftReference === "left" ? "right" : "left"; + const rightReference = isReference(expression.right.left) ? "left" : "right"; + const rightNullish = rightReference === "left" ? "right" : "left"; + + return astUtils.isSameReference(expression.left[leftReference], expression.right[rightReference]) && + ((astUtils.isNullLiteral(expression.left[leftNullish]) && isUndefined(expression.right[rightNullish], scope)) || + (isUndefined(expression.left[leftNullish], scope) && astUtils.isNullLiteral(expression.right[rightNullish]))); } /** @@ -124,12 +103,10 @@ function isExplicitNullishComparison(expression, scope) { * @returns {boolean} Whether the expression is a boolean cast */ function isBooleanCast(expression, scope) { - return ( - expression.type === "CallExpression" && - expression.callee.name === "Boolean" && - expression.arguments.length === 1 && - astUtils.isReferenceToGlobalVariable(scope, expression.callee) - ); + return expression.type === "CallExpression" && + expression.callee.name === "Boolean" && + expression.arguments.length === 1 && + astUtils.isReferenceToGlobalVariable(scope, expression.callee); } /** @@ -142,39 +119,22 @@ function isBooleanCast(expression, scope) { * @returns {?{ reference: ASTNode, operator: '??'|'||'|'&&'}} Null if not a known existence */ function getExistence(expression, scope) { - const isNegated = - expression.type === "UnaryExpression" && expression.operator === "!"; - const base = isNegated ? expression.argument : expression; - - switch (true) { - case isReference(base): - return { reference: base, operator: isNegated ? "||" : "&&" }; - case base.type === "UnaryExpression" && - base.operator === "!" && - isReference(base.argument): - return { reference: base.argument, operator: "&&" }; - case isBooleanCast(base, scope) && isReference(base.arguments[0]): - return { - reference: base.arguments[0], - operator: isNegated ? "||" : "&&", - }; - case isImplicitNullishComparison(expression, scope): - return { - reference: isReference(expression.left) - ? expression.left - : expression.right, - operator: "??", - }; - case isExplicitNullishComparison(expression, scope): - return { - reference: isReference(expression.left.left) - ? expression.left.left - : expression.left.right, - operator: "??", - }; - default: - return null; - } + const isNegated = expression.type === "UnaryExpression" && expression.operator === "!"; + const base = isNegated ? expression.argument : expression; + + switch (true) { + case isReference(base): + return { reference: base, operator: isNegated ? "||" : "&&" }; + case base.type === "UnaryExpression" && base.operator === "!" && isReference(base.argument): + return { reference: base.argument, operator: "&&" }; + case isBooleanCast(base, scope) && isReference(base.arguments[0]): + return { reference: base.arguments[0], operator: isNegated ? "||" : "&&" }; + case isImplicitNullishComparison(expression, scope): + return { reference: isReference(expression.left) ? expression.left : expression.right, operator: "??" }; + case isExplicitNullishComparison(expression, scope): + return { reference: isReference(expression.left.left) ? expression.left.left : expression.left.right, operator: "??" }; + default: return null; + } } /** @@ -183,13 +143,11 @@ function getExistence(expression, scope) { * @returns {boolean} True iff passed node is inside a with block */ function isInsideWithBlock(node) { - if (node.type === "Program") { - return false; - } + if (node.type === "Program") { + return false; + } - return node.parent.type === "WithStatement" && node.parent.body === node - ? true - : isInsideWithBlock(node.parent); + return node.parent.type === "WithStatement" && node.parent.body === node ? true : isInsideWithBlock(node.parent); } /** @@ -199,490 +157,348 @@ function isInsideWithBlock(node) { * @returns {Expression} Leftmost operand */ function getLeftmostOperand(sourceCode, node) { - let left = node.left; - - while ( - left.type === "LogicalExpression" && - left.operator === node.operator - ) { - if (astUtils.isParenthesised(sourceCode, left)) { - /* - * It should have associativity, - * but ignore it if use parentheses to make the evaluation order clear. - */ - return left; - } - left = left.left; - } - return left; + let left = node.left; + + while (left.type === "LogicalExpression" && left.operator === node.operator) { + + if (astUtils.isParenthesised(sourceCode, left)) { + + /* + * It should have associativity, + * but ignore it if use parentheses to make the evaluation order clear. + */ + return left; + } + left = left.left; + } + return left; + } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: - "Require or disallow logical assignment operator shorthand", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/logical-assignment-operators", - }, - - schema: { - type: "array", - oneOf: [ - { - items: [ - { const: "always" }, - { - type: "object", - properties: { - enforceForIfStatements: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - minItems: 0, // 0 for allowing passing no options - maxItems: 2, - }, - { - items: [{ const: "never" }], - minItems: 1, - maxItems: 1, - }, - ], - }, - fixable: "code", - hasSuggestions: true, - messages: { - assignment: - "Assignment (=) can be replaced with operator assignment ({{operator}}).", - useLogicalOperator: - "Convert this assignment to use the operator {{ operator }}.", - logical: - "Logical expression can be replaced with an assignment ({{ operator }}).", - convertLogical: - "Replace this logical expression with an assignment with the operator {{ operator }}.", - if: "'if' statement can be replaced with a logical operator assignment with operator {{ operator }}.", - convertIf: - "Replace this 'if' statement with a logical assignment with operator {{ operator }}.", - unexpected: - "Unexpected logical operator assignment ({{operator}}) shorthand.", - separate: - "Separate the logical assignment into an assignment with a logical operator.", - }, - }, - - create(context) { - const mode = context.options[0] === "never" ? "never" : "always"; - const checkIf = - mode === "always" && - context.options.length > 1 && - context.options[1].enforceForIfStatements; - const sourceCode = context.sourceCode; - const isStrict = sourceCode.getScope(sourceCode.ast).isStrict; - - /** - * Returns false if the access could be a getter - * @param {ASTNode} node Assignment expression - * @returns {boolean} True iff the fix is safe - */ - function cannotBeGetter(node) { - return ( - node.type === "Identifier" && - (isStrict || !isInsideWithBlock(node)) - ); - } - - /** - * Check whether only a single property is accessed - * @param {ASTNode} node reference - * @returns {boolean} True iff a single property is accessed - */ - function accessesSingleProperty(node) { - if (!isStrict && isInsideWithBlock(node)) { - return node.type === "Identifier"; - } - - return ( - node.type === "MemberExpression" && - baseTypes.has(node.object.type) && - (!node.computed || - (node.property.type !== "MemberExpression" && - node.property.type !== "ChainExpression")) - ); - } - - /** - * Adds a fixer or suggestion whether on the fix is safe. - * @param {{ messageId: string, node: ASTNode }} descriptor Report descriptor without fix or suggest - * @param {{ messageId: string, fix: Function }} suggestion Adds the fix or the whole suggestion as only element in "suggest" to suggestion - * @param {boolean} shouldBeFixed Fix iff the condition is true - * @returns {Object} Descriptor with either an added fix or suggestion - */ - function createConditionalFixer(descriptor, suggestion, shouldBeFixed) { - if (shouldBeFixed) { - return { - ...descriptor, - fix: suggestion.fix, - }; - } - - return { - ...descriptor, - suggest: [suggestion], - }; - } - - /** - * Returns the operator token for assignments and binary expressions - * @param {ASTNode} node AssignmentExpression or BinaryExpression - * @returns {import('eslint').AST.Token} Operator token between the left and right expression - */ - function getOperatorToken(node) { - return sourceCode.getFirstTokenBetween( - node.left, - node.right, - token => token.value === node.operator, - ); - } - - if (mode === "never") { - return { - // foo ||= bar - AssignmentExpression(assignment) { - if ( - !astUtils.isLogicalAssignmentOperator( - assignment.operator, - ) - ) { - return; - } - - const descriptor = { - messageId: "unexpected", - node: assignment, - data: { operator: assignment.operator }, - }; - const suggestion = { - messageId: "separate", - *fix(ruleFixer) { - if ( - sourceCode.getCommentsInside(assignment) - .length > 0 - ) { - return; - } - - const operatorToken = getOperatorToken(assignment); - - // -> foo = bar - yield ruleFixer.replaceText(operatorToken, "="); - - const assignmentText = sourceCode.getText( - assignment.left, - ); - const operator = assignment.operator.slice(0, -1); - - // -> foo = foo || bar - yield ruleFixer.insertTextAfter( - operatorToken, - ` ${assignmentText} ${operator}`, - ); - - const precedence = - astUtils.getPrecedence(assignment.right) <= - astUtils.getPrecedence({ - type: "LogicalExpression", - operator, - }); - - // ?? and || / && cannot be mixed but have same precedence - const mixed = - assignment.operator === "??=" && - astUtils.isLogicalExpression(assignment.right); - - if ( - !astUtils.isParenthesised( - sourceCode, - assignment.right, - ) && - (precedence || mixed) - ) { - // -> foo = foo || (bar) - yield ruleFixer.insertTextBefore( - assignment.right, - "(", - ); - yield ruleFixer.insertTextAfter( - assignment.right, - ")", - ); - } - }, - }; - - context.report( - createConditionalFixer( - descriptor, - suggestion, - cannotBeGetter(assignment.left), - ), - ); - }, - }; - } - - return { - // foo = foo || bar - "AssignmentExpression[operator='='][right.type='LogicalExpression']"( - assignment, - ) { - const leftOperand = getLeftmostOperand( - sourceCode, - assignment.right, - ); - - if (!astUtils.isSameReference(assignment.left, leftOperand)) { - return; - } - - const descriptor = { - messageId: "assignment", - node: assignment, - data: { operator: `${assignment.right.operator}=` }, - }; - const suggestion = { - messageId: "useLogicalOperator", - data: { operator: `${assignment.right.operator}=` }, - *fix(ruleFixer) { - if ( - sourceCode.getCommentsInside(assignment).length > 0 - ) { - return; - } - - // No need for parenthesis around the assignment based on precedence as the precedence stays the same even with changed operator - const assignmentOperatorToken = - getOperatorToken(assignment); - - // -> foo ||= foo || bar - yield ruleFixer.insertTextBefore( - assignmentOperatorToken, - assignment.right.operator, - ); - - // -> foo ||= bar - const logicalOperatorToken = getOperatorToken( - leftOperand.parent, - ); - const firstRightOperandToken = - sourceCode.getTokenAfter(logicalOperatorToken); - - yield ruleFixer.removeRange([ - leftOperand.parent.range[0], - firstRightOperandToken.range[0], - ]); - }, - }; - - context.report( - createConditionalFixer( - descriptor, - suggestion, - cannotBeGetter(assignment.left), - ), - ); - }, - - // foo || (foo = bar) - 'LogicalExpression[right.type="AssignmentExpression"][right.operator="="]'( - logical, - ) { - // Right side has to be parenthesized, otherwise would be parsed as (foo || foo) = bar which is illegal - if ( - isReference(logical.left) && - astUtils.isSameReference(logical.left, logical.right.left) - ) { - const descriptor = { - messageId: "logical", - node: logical, - data: { operator: `${logical.operator}=` }, - }; - const suggestion = { - messageId: "convertLogical", - data: { operator: `${logical.operator}=` }, - *fix(ruleFixer) { - if ( - sourceCode.getCommentsInside(logical).length > 0 - ) { - return; - } - - const parentPrecedence = astUtils.getPrecedence( - logical.parent, - ); - const requiresOuterParenthesis = - logical.parent.type !== "ExpressionStatement" && - (parentPrecedence === -1 || - astUtils.getPrecedence({ - type: "AssignmentExpression", - }) < parentPrecedence); - - if ( - !astUtils.isParenthesised( - sourceCode, - logical, - ) && - requiresOuterParenthesis - ) { - yield ruleFixer.insertTextBefore(logical, "("); - yield ruleFixer.insertTextAfter(logical, ")"); - } - - // Also removes all opening parenthesis - yield ruleFixer.removeRange([ - logical.range[0], - logical.right.range[0], - ]); // -> foo = bar) - - // Also removes all ending parenthesis - yield ruleFixer.removeRange([ - logical.right.range[1], - logical.range[1], - ]); // -> foo = bar - - const operatorToken = getOperatorToken( - logical.right, - ); - - yield ruleFixer.insertTextBefore( - operatorToken, - logical.operator, - ); // -> foo ||= bar - }, - }; - const fix = - cannotBeGetter(logical.left) || - accessesSingleProperty(logical.left); - - context.report( - createConditionalFixer(descriptor, suggestion, fix), - ); - } - }, - - // if (foo) foo = bar - "IfStatement[alternate=null]"(ifNode) { - if (!checkIf) { - return; - } - - const hasBody = ifNode.consequent.type === "BlockStatement"; - - if (hasBody && ifNode.consequent.body.length !== 1) { - return; - } - - const body = hasBody - ? ifNode.consequent.body[0] - : ifNode.consequent; - const scope = sourceCode.getScope(ifNode); - const existence = getExistence(ifNode.test, scope); - - if ( - body.type === "ExpressionStatement" && - body.expression.type === "AssignmentExpression" && - body.expression.operator === "=" && - existence !== null && - astUtils.isSameReference( - existence.reference, - body.expression.left, - ) - ) { - const descriptor = { - messageId: "if", - node: ifNode, - data: { operator: `${existence.operator}=` }, - }; - const suggestion = { - messageId: "convertIf", - data: { operator: `${existence.operator}=` }, - *fix(ruleFixer) { - if ( - sourceCode.getCommentsInside(ifNode).length > 0 - ) { - return; - } - - const firstBodyToken = - sourceCode.getFirstToken(body); - const prevToken = sourceCode.getTokenBefore(ifNode); - - if ( - prevToken !== null && - prevToken.value !== ";" && - prevToken.value !== "{" && - firstBodyToken.type !== "Identifier" && - firstBodyToken.type !== "Keyword" - ) { - // Do not fix if the fixed statement could be part of the previous statement (eg. fn() if (a == null) (a) = b --> fn()(a) ??= b) - return; - } - - const operatorToken = getOperatorToken( - body.expression, - ); - - yield ruleFixer.insertTextBefore( - operatorToken, - existence.operator, - ); // -> if (foo) foo ||= bar - - yield ruleFixer.removeRange([ - ifNode.range[0], - body.range[0], - ]); // -> foo ||= bar - - yield ruleFixer.removeRange([ - body.range[1], - ifNode.range[1], - ]); // -> foo ||= bar, only present if "if" had a body - - const nextToken = sourceCode.getTokenAfter( - body.expression, - ); - - if ( - hasBody && - nextToken !== null && - nextToken.value !== ";" - ) { - yield ruleFixer.insertTextAfter(ifNode, ";"); - } - }, - }; - const shouldBeFixed = - cannotBeGetter(existence.reference) || - (ifNode.test.type !== "LogicalExpression" && - accessesSingleProperty(existence.reference)); - - context.report( - createConditionalFixer( - descriptor, - suggestion, - shouldBeFixed, - ), - ); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Require or disallow logical assignment operator shorthand", + recommended: false, + url: "https://eslint.org/docs/latest/rules/logical-assignment-operators" + }, + + schema: { + type: "array", + oneOf: [{ + items: [ + { const: "always" }, + { + type: "object", + properties: { + enforceForIfStatements: { + type: "boolean" + } + }, + additionalProperties: false + } + ], + minItems: 0, // 0 for allowing passing no options + maxItems: 2 + }, { + items: [{ const: "never" }], + minItems: 1, + maxItems: 1 + }] + }, + fixable: "code", + hasSuggestions: true, + messages: { + assignment: "Assignment (=) can be replaced with operator assignment ({{operator}}).", + useLogicalOperator: "Convert this assignment to use the operator {{ operator }}.", + logical: "Logical expression can be replaced with an assignment ({{ operator }}).", + convertLogical: "Replace this logical expression with an assignment with the operator {{ operator }}.", + if: "'if' statement can be replaced with a logical operator assignment with operator {{ operator }}.", + convertIf: "Replace this 'if' statement with a logical assignment with operator {{ operator }}.", + unexpected: "Unexpected logical operator assignment ({{operator}}) shorthand.", + separate: "Separate the logical assignment into an assignment with a logical operator." + } + }, + + create(context) { + const mode = context.options[0] === "never" ? "never" : "always"; + const checkIf = mode === "always" && context.options.length > 1 && context.options[1].enforceForIfStatements; + const sourceCode = context.sourceCode; + const isStrict = sourceCode.getScope(sourceCode.ast).isStrict; + + /** + * Returns false if the access could be a getter + * @param {ASTNode} node Assignment expression + * @returns {boolean} True iff the fix is safe + */ + function cannotBeGetter(node) { + return node.type === "Identifier" && + (isStrict || !isInsideWithBlock(node)); + } + + /** + * Check whether only a single property is accessed + * @param {ASTNode} node reference + * @returns {boolean} True iff a single property is accessed + */ + function accessesSingleProperty(node) { + if (!isStrict && isInsideWithBlock(node)) { + return node.type === "Identifier"; + } + + return node.type === "MemberExpression" && + baseTypes.has(node.object.type) && + (!node.computed || (node.property.type !== "MemberExpression" && node.property.type !== "ChainExpression")); + } + + /** + * Adds a fixer or suggestion whether on the fix is safe. + * @param {{ messageId: string, node: ASTNode }} descriptor Report descriptor without fix or suggest + * @param {{ messageId: string, fix: Function }} suggestion Adds the fix or the whole suggestion as only element in "suggest" to suggestion + * @param {boolean} shouldBeFixed Fix iff the condition is true + * @returns {Object} Descriptor with either an added fix or suggestion + */ + function createConditionalFixer(descriptor, suggestion, shouldBeFixed) { + if (shouldBeFixed) { + return { + ...descriptor, + fix: suggestion.fix + }; + } + + return { + ...descriptor, + suggest: [suggestion] + }; + } + + + /** + * Returns the operator token for assignments and binary expressions + * @param {ASTNode} node AssignmentExpression or BinaryExpression + * @returns {import('eslint').AST.Token} Operator token between the left and right expression + */ + function getOperatorToken(node) { + return sourceCode.getFirstTokenBetween(node.left, node.right, token => token.value === node.operator); + } + + if (mode === "never") { + return { + + // foo ||= bar + "AssignmentExpression"(assignment) { + if (!astUtils.isLogicalAssignmentOperator(assignment.operator)) { + return; + } + + const descriptor = { + messageId: "unexpected", + node: assignment, + data: { operator: assignment.operator } + }; + const suggestion = { + messageId: "separate", + *fix(ruleFixer) { + if (sourceCode.getCommentsInside(assignment).length > 0) { + return; + } + + const operatorToken = getOperatorToken(assignment); + + // -> foo = bar + yield ruleFixer.replaceText(operatorToken, "="); + + const assignmentText = sourceCode.getText(assignment.left); + const operator = assignment.operator.slice(0, -1); + + // -> foo = foo || bar + yield ruleFixer.insertTextAfter(operatorToken, ` ${assignmentText} ${operator}`); + + const precedence = astUtils.getPrecedence(assignment.right) <= astUtils.getPrecedence({ type: "LogicalExpression", operator }); + + // ?? and || / && cannot be mixed but have same precedence + const mixed = assignment.operator === "??=" && astUtils.isLogicalExpression(assignment.right); + + if (!astUtils.isParenthesised(sourceCode, assignment.right) && (precedence || mixed)) { + + // -> foo = foo || (bar) + yield ruleFixer.insertTextBefore(assignment.right, "("); + yield ruleFixer.insertTextAfter(assignment.right, ")"); + } + } + }; + + context.report(createConditionalFixer(descriptor, suggestion, cannotBeGetter(assignment.left))); + } + }; + } + + return { + + // foo = foo || bar + "AssignmentExpression[operator='='][right.type='LogicalExpression']"(assignment) { + const leftOperand = getLeftmostOperand(sourceCode, assignment.right); + + if (!astUtils.isSameReference(assignment.left, leftOperand) + ) { + return; + } + + const descriptor = { + messageId: "assignment", + node: assignment, + data: { operator: `${assignment.right.operator}=` } + }; + const suggestion = { + messageId: "useLogicalOperator", + data: { operator: `${assignment.right.operator}=` }, + *fix(ruleFixer) { + if (sourceCode.getCommentsInside(assignment).length > 0) { + return; + } + + // No need for parenthesis around the assignment based on precedence as the precedence stays the same even with changed operator + const assignmentOperatorToken = getOperatorToken(assignment); + + // -> foo ||= foo || bar + yield ruleFixer.insertTextBefore(assignmentOperatorToken, assignment.right.operator); + + // -> foo ||= bar + const logicalOperatorToken = getOperatorToken(leftOperand.parent); + const firstRightOperandToken = sourceCode.getTokenAfter(logicalOperatorToken); + + yield ruleFixer.removeRange([leftOperand.parent.range[0], firstRightOperandToken.range[0]]); + } + }; + + context.report(createConditionalFixer(descriptor, suggestion, cannotBeGetter(assignment.left))); + }, + + // foo || (foo = bar) + 'LogicalExpression[right.type="AssignmentExpression"][right.operator="="]'(logical) { + + // Right side has to be parenthesized, otherwise would be parsed as (foo || foo) = bar which is illegal + if (isReference(logical.left) && astUtils.isSameReference(logical.left, logical.right.left)) { + const descriptor = { + messageId: "logical", + node: logical, + data: { operator: `${logical.operator}=` } + }; + const suggestion = { + messageId: "convertLogical", + data: { operator: `${logical.operator}=` }, + *fix(ruleFixer) { + if (sourceCode.getCommentsInside(logical).length > 0) { + return; + } + + const parentPrecedence = astUtils.getPrecedence(logical.parent); + const requiresOuterParenthesis = logical.parent.type !== "ExpressionStatement" && ( + parentPrecedence === -1 || + astUtils.getPrecedence({ type: "AssignmentExpression" }) < parentPrecedence + ); + + if (!astUtils.isParenthesised(sourceCode, logical) && requiresOuterParenthesis) { + yield ruleFixer.insertTextBefore(logical, "("); + yield ruleFixer.insertTextAfter(logical, ")"); + } + + // Also removes all opening parenthesis + yield ruleFixer.removeRange([logical.range[0], logical.right.range[0]]); // -> foo = bar) + + // Also removes all ending parenthesis + yield ruleFixer.removeRange([logical.right.range[1], logical.range[1]]); // -> foo = bar + + const operatorToken = getOperatorToken(logical.right); + + yield ruleFixer.insertTextBefore(operatorToken, logical.operator); // -> foo ||= bar + } + }; + const fix = cannotBeGetter(logical.left) || accessesSingleProperty(logical.left); + + context.report(createConditionalFixer(descriptor, suggestion, fix)); + } + }, + + // if (foo) foo = bar + "IfStatement[alternate=null]"(ifNode) { + if (!checkIf) { + return; + } + + const hasBody = ifNode.consequent.type === "BlockStatement"; + + if (hasBody && ifNode.consequent.body.length !== 1) { + return; + } + + const body = hasBody ? ifNode.consequent.body[0] : ifNode.consequent; + const scope = sourceCode.getScope(ifNode); + const existence = getExistence(ifNode.test, scope); + + if ( + body.type === "ExpressionStatement" && + body.expression.type === "AssignmentExpression" && + body.expression.operator === "=" && + existence !== null && + astUtils.isSameReference(existence.reference, body.expression.left) + ) { + const descriptor = { + messageId: "if", + node: ifNode, + data: { operator: `${existence.operator}=` } + }; + const suggestion = { + messageId: "convertIf", + data: { operator: `${existence.operator}=` }, + *fix(ruleFixer) { + if (sourceCode.getCommentsInside(ifNode).length > 0) { + return; + } + + const firstBodyToken = sourceCode.getFirstToken(body); + const prevToken = sourceCode.getTokenBefore(ifNode); + + if ( + prevToken !== null && + prevToken.value !== ";" && + prevToken.value !== "{" && + firstBodyToken.type !== "Identifier" && + firstBodyToken.type !== "Keyword" + ) { + + // Do not fix if the fixed statement could be part of the previous statement (eg. fn() if (a == null) (a) = b --> fn()(a) ??= b) + return; + } + + + const operatorToken = getOperatorToken(body.expression); + + yield ruleFixer.insertTextBefore(operatorToken, existence.operator); // -> if (foo) foo ||= bar + + yield ruleFixer.removeRange([ifNode.range[0], body.range[0]]); // -> foo ||= bar + + yield ruleFixer.removeRange([body.range[1], ifNode.range[1]]); // -> foo ||= bar, only present if "if" had a body + + const nextToken = sourceCode.getTokenAfter(body.expression); + + if (hasBody && (nextToken !== null && nextToken.value !== ";")) { + yield ruleFixer.insertTextAfter(ifNode, ";"); + } + } + }; + const shouldBeFixed = cannotBeGetter(existence.reference) || + (ifNode.test.type !== "LogicalExpression" && accessesSingleProperty(existence.reference)); + + context.report(createConditionalFixer(descriptor, suggestion, shouldBeFixed)); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/max-classes-per-file.js b/node_modules/eslint/lib/rules/max-classes-per-file.js index 3ede9edf2..241e06023 100644 --- a/node_modules/eslint/lib/rules/max-classes-per-file.js +++ b/node_modules/eslint/lib/rules/max-classes-per-file.js @@ -8,83 +8,82 @@ // Requirements //------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", + meta: { + type: "suggestion", - docs: { - description: "Enforce a maximum number of classes per file", - recommended: false, - url: "https://eslint.org/docs/latest/rules/max-classes-per-file", - }, + docs: { + description: "Enforce a maximum number of classes per file", + recommended: false, + url: "https://eslint.org/docs/latest/rules/max-classes-per-file" + }, - schema: [ - { - oneOf: [ - { - type: "integer", - minimum: 1, - }, - { - type: "object", - properties: { - ignoreExpressions: { - type: "boolean", - }, - max: { - type: "integer", - minimum: 1, - }, - }, - additionalProperties: false, - }, - ], - }, - ], + schema: [ + { + oneOf: [ + { + type: "integer", + minimum: 1 + }, + { + type: "object", + properties: { + ignoreExpressions: { + type: "boolean" + }, + max: { + type: "integer", + minimum: 1 + } + }, + additionalProperties: false + } + ] + } + ], - messages: { - maximumExceeded: - "File has too many classes ({{ classCount }}). Maximum allowed is {{ max }}.", - }, - }, - create(context) { - const [option = {}] = context.options; - const [ignoreExpressions, max] = - typeof option === "number" - ? [false, option || 1] - : [option.ignoreExpressions, option.max || 1]; + messages: { + maximumExceeded: "File has too many classes ({{ classCount }}). Maximum allowed is {{ max }}." + } + }, + create(context) { + const [option = {}] = context.options; + const [ignoreExpressions, max] = typeof option === "number" + ? [false, option || 1] + : [option.ignoreExpressions, option.max || 1]; - let classCount = 0; + let classCount = 0; - return { - Program() { - classCount = 0; - }, - "Program:exit"(node) { - if (classCount > max) { - context.report({ - node, - messageId: "maximumExceeded", - data: { - classCount, - max, - }, - }); - } - }, - ClassDeclaration() { - classCount++; - }, - ClassExpression() { - if (!ignoreExpressions) { - classCount++; - } - }, - }; - }, + return { + Program() { + classCount = 0; + }, + "Program:exit"(node) { + if (classCount > max) { + context.report({ + node, + messageId: "maximumExceeded", + data: { + classCount, + max + } + }); + } + }, + "ClassDeclaration"() { + classCount++; + }, + "ClassExpression"() { + if (!ignoreExpressions) { + classCount++; + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/max-depth.js b/node_modules/eslint/lib/rules/max-depth.js index f3c66b7ae..7a8e9f94e 100644 --- a/node_modules/eslint/lib/rules/max-depth.js +++ b/node_modules/eslint/lib/rules/max-depth.js @@ -9,151 +9,148 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: "Enforce a maximum depth that blocks can be nested", - recommended: false, - url: "https://eslint.org/docs/latest/rules/max-depth", - }, - - schema: [ - { - oneOf: [ - { - type: "integer", - minimum: 0, - }, - { - type: "object", - properties: { - maximum: { - type: "integer", - minimum: 0, - }, - max: { - type: "integer", - minimum: 0, - }, - }, - additionalProperties: false, - }, - ], - }, - ], - messages: { - tooDeeply: - "Blocks are nested too deeply ({{depth}}). Maximum allowed is {{maxDepth}}.", - }, - }, - - create(context) { - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - const functionStack = [], - option = context.options[0]; - let maxDepth = 4; - - if ( - typeof option === "object" && - (Object.hasOwn(option, "maximum") || Object.hasOwn(option, "max")) - ) { - maxDepth = option.maximum || option.max; - } - if (typeof option === "number") { - maxDepth = option; - } - - /** - * When parsing a new function, store it in our function stack - * @returns {void} - * @private - */ - function startFunction() { - functionStack.push(0); - } - - /** - * When parsing is done then pop out the reference - * @returns {void} - * @private - */ - function endFunction() { - functionStack.pop(); - } - - /** - * Save the block and Evaluate the node - * @param {ASTNode} node node to evaluate - * @returns {void} - * @private - */ - function pushBlock(node) { - const len = ++functionStack[functionStack.length - 1]; - - if (len > maxDepth) { - context.report({ - node, - messageId: "tooDeeply", - data: { depth: len, maxDepth }, - }); - } - } - - /** - * Pop the saved block - * @returns {void} - * @private - */ - function popBlock() { - functionStack[functionStack.length - 1]--; - } - - //-------------------------------------------------------------------------- - // Public API - //-------------------------------------------------------------------------- - - return { - Program: startFunction, - FunctionDeclaration: startFunction, - FunctionExpression: startFunction, - ArrowFunctionExpression: startFunction, - StaticBlock: startFunction, - - IfStatement(node) { - if (node.parent.type !== "IfStatement") { - pushBlock(node); - } - }, - SwitchStatement: pushBlock, - TryStatement: pushBlock, - DoWhileStatement: pushBlock, - WhileStatement: pushBlock, - WithStatement: pushBlock, - ForStatement: pushBlock, - ForInStatement: pushBlock, - ForOfStatement: pushBlock, - - "IfStatement:exit": popBlock, - "SwitchStatement:exit": popBlock, - "TryStatement:exit": popBlock, - "DoWhileStatement:exit": popBlock, - "WhileStatement:exit": popBlock, - "WithStatement:exit": popBlock, - "ForStatement:exit": popBlock, - "ForInStatement:exit": popBlock, - "ForOfStatement:exit": popBlock, - - "FunctionDeclaration:exit": endFunction, - "FunctionExpression:exit": endFunction, - "ArrowFunctionExpression:exit": endFunction, - "StaticBlock:exit": endFunction, - "Program:exit": endFunction, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Enforce a maximum depth that blocks can be nested", + recommended: false, + url: "https://eslint.org/docs/latest/rules/max-depth" + }, + + schema: [ + { + oneOf: [ + { + type: "integer", + minimum: 0 + }, + { + type: "object", + properties: { + maximum: { + type: "integer", + minimum: 0 + }, + max: { + type: "integer", + minimum: 0 + } + }, + additionalProperties: false + } + ] + } + ], + messages: { + tooDeeply: "Blocks are nested too deeply ({{depth}}). Maximum allowed is {{maxDepth}}." + } + }, + + create(context) { + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + const functionStack = [], + option = context.options[0]; + let maxDepth = 4; + + if ( + typeof option === "object" && + (Object.prototype.hasOwnProperty.call(option, "maximum") || Object.prototype.hasOwnProperty.call(option, "max")) + ) { + maxDepth = option.maximum || option.max; + } + if (typeof option === "number") { + maxDepth = option; + } + + /** + * When parsing a new function, store it in our function stack + * @returns {void} + * @private + */ + function startFunction() { + functionStack.push(0); + } + + /** + * When parsing is done then pop out the reference + * @returns {void} + * @private + */ + function endFunction() { + functionStack.pop(); + } + + /** + * Save the block and Evaluate the node + * @param {ASTNode} node node to evaluate + * @returns {void} + * @private + */ + function pushBlock(node) { + const len = ++functionStack[functionStack.length - 1]; + + if (len > maxDepth) { + context.report({ node, messageId: "tooDeeply", data: { depth: len, maxDepth } }); + } + } + + /** + * Pop the saved block + * @returns {void} + * @private + */ + function popBlock() { + functionStack[functionStack.length - 1]--; + } + + //-------------------------------------------------------------------------- + // Public API + //-------------------------------------------------------------------------- + + return { + Program: startFunction, + FunctionDeclaration: startFunction, + FunctionExpression: startFunction, + ArrowFunctionExpression: startFunction, + StaticBlock: startFunction, + + IfStatement(node) { + if (node.parent.type !== "IfStatement") { + pushBlock(node); + } + }, + SwitchStatement: pushBlock, + TryStatement: pushBlock, + DoWhileStatement: pushBlock, + WhileStatement: pushBlock, + WithStatement: pushBlock, + ForStatement: pushBlock, + ForInStatement: pushBlock, + ForOfStatement: pushBlock, + + "IfStatement:exit": popBlock, + "SwitchStatement:exit": popBlock, + "TryStatement:exit": popBlock, + "DoWhileStatement:exit": popBlock, + "WhileStatement:exit": popBlock, + "WithStatement:exit": popBlock, + "ForStatement:exit": popBlock, + "ForInStatement:exit": popBlock, + "ForOfStatement:exit": popBlock, + + "FunctionDeclaration:exit": endFunction, + "FunctionExpression:exit": endFunction, + "ArrowFunctionExpression:exit": endFunction, + "StaticBlock:exit": endFunction, + "Program:exit": endFunction + }; + + } }; diff --git a/node_modules/eslint/lib/rules/max-len.js b/node_modules/eslint/lib/rules/max-len.js index 108918f88..138a0f239 100644 --- a/node_modules/eslint/lib/rules/max-len.js +++ b/node_modules/eslint/lib/rules/max-len.js @@ -11,487 +11,430 @@ //------------------------------------------------------------------------------ const OPTIONS_SCHEMA = { - type: "object", - properties: { - code: { - type: "integer", - minimum: 0, - }, - comments: { - type: "integer", - minimum: 0, - }, - tabWidth: { - type: "integer", - minimum: 0, - }, - ignorePattern: { - type: "string", - }, - ignoreComments: { - type: "boolean", - }, - ignoreStrings: { - type: "boolean", - }, - ignoreUrls: { - type: "boolean", - }, - ignoreTemplateLiterals: { - type: "boolean", - }, - ignoreRegExpLiterals: { - type: "boolean", - }, - ignoreTrailingComments: { - type: "boolean", - }, - }, - additionalProperties: false, + type: "object", + properties: { + code: { + type: "integer", + minimum: 0 + }, + comments: { + type: "integer", + minimum: 0 + }, + tabWidth: { + type: "integer", + minimum: 0 + }, + ignorePattern: { + type: "string" + }, + ignoreComments: { + type: "boolean" + }, + ignoreStrings: { + type: "boolean" + }, + ignoreUrls: { + type: "boolean" + }, + ignoreTemplateLiterals: { + type: "boolean" + }, + ignoreRegExpLiterals: { + type: "boolean" + }, + ignoreTrailingComments: { + type: "boolean" + } + }, + additionalProperties: false }; const OPTIONS_OR_INTEGER_SCHEMA = { - anyOf: [ - OPTIONS_SCHEMA, - { - type: "integer", - minimum: 0, - }, - ], + anyOf: [ + OPTIONS_SCHEMA, + { + type: "integer", + minimum: 0 + } + ] }; //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "max-len", - url: "https://eslint.style/rules/max-len", - }, - }, - ], - }, - type: "layout", - - docs: { - description: "Enforce a maximum line length", - recommended: false, - url: "https://eslint.org/docs/latest/rules/max-len", - }, - - schema: [ - OPTIONS_OR_INTEGER_SCHEMA, - OPTIONS_OR_INTEGER_SCHEMA, - OPTIONS_SCHEMA, - ], - messages: { - max: "This line has a length of {{lineLength}}. Maximum allowed is {{maxLength}}.", - maxComment: - "This line has a comment length of {{lineLength}}. Maximum allowed is {{maxCommentLength}}.", - }, - }, - - create(context) { - /* - * Inspired by http://tools.ietf.org/html/rfc3986#appendix-B, however: - * - They're matching an entire string that we know is a URI - * - We're matching part of a string where we think there *might* be a URL - * - We're only concerned about URLs, as picking out any URI would cause - * too many false positives - * - We don't care about matching the entire URL, any small segment is fine - */ - const URL_REGEXP = /[^:/?#]:\/\/[^?#]/u; - - const sourceCode = context.sourceCode; - - /** - * Computes the length of a line that may contain tabs. The width of each - * tab will be the number of spaces to the next tab stop. - * @param {string} line The line. - * @param {number} tabWidth The width of each tab stop in spaces. - * @returns {number} The computed line length. - * @private - */ - function computeLineLength(line, tabWidth) { - let extraCharacterCount = 0; - - line.replace(/\t/gu, (match, offset) => { - const totalOffset = offset + extraCharacterCount, - previousTabStopOffset = tabWidth - ? totalOffset % tabWidth - : 0, - spaceCount = tabWidth - previousTabStopOffset; - - extraCharacterCount += spaceCount - 1; // -1 for the replaced tab - }); - return Array.from(line).length + extraCharacterCount; - } - - // The options object must be the last option specified… - const options = Object.assign({}, context.options.at(-1)); - - // …but max code length… - if (typeof context.options[0] === "number") { - options.code = context.options[0]; - } - - // …and tabWidth can be optionally specified directly as integers. - if (typeof context.options[1] === "number") { - options.tabWidth = context.options[1]; - } - - const maxLength = typeof options.code === "number" ? options.code : 80, - tabWidth = - typeof options.tabWidth === "number" ? options.tabWidth : 4, - ignoreComments = !!options.ignoreComments, - ignoreStrings = !!options.ignoreStrings, - ignoreTemplateLiterals = !!options.ignoreTemplateLiterals, - ignoreRegExpLiterals = !!options.ignoreRegExpLiterals, - ignoreTrailingComments = - !!options.ignoreTrailingComments || !!options.ignoreComments, - ignoreUrls = !!options.ignoreUrls, - maxCommentLength = options.comments; - let ignorePattern = options.ignorePattern || null; - - if (ignorePattern) { - ignorePattern = new RegExp(ignorePattern, "u"); - } - - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - /** - * Tells if a given comment is trailing: it starts on the current line and - * extends to or past the end of the current line. - * @param {string} line The source line we want to check for a trailing comment on - * @param {number} lineNumber The one-indexed line number for line - * @param {ASTNode} comment The comment to inspect - * @returns {boolean} If the comment is trailing on the given line - */ - function isTrailingComment(line, lineNumber, comment) { - return ( - comment && - comment.loc.start.line === lineNumber && - lineNumber <= comment.loc.end.line && - (comment.loc.end.line > lineNumber || - comment.loc.end.column === line.length) - ); - } - - /** - * Tells if a comment encompasses the entire line. - * @param {string} line The source line with a trailing comment - * @param {number} lineNumber The one-indexed line number this is on - * @param {ASTNode} comment The comment to remove - * @returns {boolean} If the comment covers the entire line - */ - function isFullLineComment(line, lineNumber, comment) { - const start = comment.loc.start, - end = comment.loc.end, - isFirstTokenOnLine = !line - .slice(0, comment.loc.start.column) - .trim(); - - return ( - comment && - (start.line < lineNumber || - (start.line === lineNumber && isFirstTokenOnLine)) && - (end.line > lineNumber || - (end.line === lineNumber && end.column === line.length)) - ); - } - - /** - * Check if a node is a JSXEmptyExpression contained in a single line JSXExpressionContainer. - * @param {ASTNode} node A node to check. - * @returns {boolean} True if the node is a JSXEmptyExpression contained in a single line JSXExpressionContainer. - */ - function isJSXEmptyExpressionInSingleLineContainer(node) { - if ( - !node || - !node.parent || - node.type !== "JSXEmptyExpression" || - node.parent.type !== "JSXExpressionContainer" - ) { - return false; - } - - const parent = node.parent; - - return parent.loc.start.line === parent.loc.end.line; - } - - /** - * Gets the line after the comment and any remaining trailing whitespace is - * stripped. - * @param {string} line The source line with a trailing comment - * @param {ASTNode} comment The comment to remove - * @returns {string} Line without comment and trailing whitespace - */ - function stripTrailingComment(line, comment) { - // loc.column is zero-indexed - return line.slice(0, comment.loc.start.column).replace(/\s+$/u, ""); - } - - /** - * Ensure that an array exists at [key] on `object`, and add `value` to it. - * @param {Object} object the object to mutate - * @param {string} key the object's key - * @param {any} value the value to add - * @returns {void} - * @private - */ - function ensureArrayAndPush(object, key, value) { - if (!Array.isArray(object[key])) { - object[key] = []; - } - object[key].push(value); - } - - /** - * Retrieves an array containing all strings (" or ') in the source code. - * @returns {ASTNode[]} An array of string nodes. - */ - function getAllStrings() { - return sourceCode.ast.tokens.filter( - token => - token.type === "String" || - (token.type === "JSXText" && - sourceCode.getNodeByRangeIndex(token.range[0] - 1) - .type === "JSXAttribute"), - ); - } - - /** - * Retrieves an array containing all template literals in the source code. - * @returns {ASTNode[]} An array of template literal nodes. - */ - function getAllTemplateLiterals() { - return sourceCode.ast.tokens.filter( - token => token.type === "Template", - ); - } - - /** - * Retrieves an array containing all RegExp literals in the source code. - * @returns {ASTNode[]} An array of RegExp literal nodes. - */ - function getAllRegExpLiterals() { - return sourceCode.ast.tokens.filter( - token => token.type === "RegularExpression", - ); - } - - /** - * - * reduce an array of AST nodes by line number, both start and end. - * @param {ASTNode[]} arr array of AST nodes - * @returns {Object} accululated AST nodes - */ - function groupArrayByLineNumber(arr) { - const obj = {}; - - for (let i = 0; i < arr.length; i++) { - const node = arr[i]; - - for (let j = node.loc.start.line; j <= node.loc.end.line; ++j) { - ensureArrayAndPush(obj, j, node); - } - } - return obj; - } - - /** - * Returns an array of all comments in the source code. - * If the element in the array is a JSXEmptyExpression contained with a single line JSXExpressionContainer, - * the element is changed with JSXExpressionContainer node. - * @returns {ASTNode[]} An array of comment nodes - */ - function getAllComments() { - const comments = []; - - sourceCode.getAllComments().forEach(commentNode => { - const containingNode = sourceCode.getNodeByRangeIndex( - commentNode.range[0], - ); - - if (isJSXEmptyExpressionInSingleLineContainer(containingNode)) { - // push a unique node only - if (comments.at(-1) !== containingNode.parent) { - comments.push(containingNode.parent); - } - } else { - comments.push(commentNode); - } - }); - - return comments; - } - - /** - * Check the program for max length - * @param {ASTNode} node Node to examine - * @returns {void} - * @private - */ - function checkProgramForMaxLength(node) { - // split (honors line-ending) - const lines = sourceCode.lines, - // list of comments to ignore - comments = - ignoreComments || maxCommentLength || ignoreTrailingComments - ? getAllComments() - : []; - - // we iterate over comments in parallel with the lines - let commentsIndex = 0; - - const strings = getAllStrings(); - const stringsByLine = groupArrayByLineNumber(strings); - - const templateLiterals = getAllTemplateLiterals(); - const templateLiteralsByLine = - groupArrayByLineNumber(templateLiterals); - - const regExpLiterals = getAllRegExpLiterals(); - const regExpLiteralsByLine = groupArrayByLineNumber(regExpLiterals); - - lines.forEach((line, i) => { - // i is zero-indexed, line numbers are one-indexed - const lineNumber = i + 1; - - /* - * if we're checking comment length; we need to know whether this - * line is a comment - */ - let lineIsComment = false; - let textToMeasure; - - /* - * We can short-circuit the comment checks if we're already out of - * comments to check. - */ - if (commentsIndex < comments.length) { - let comment; - - // iterate over comments until we find one past the current line - do { - comment = comments[++commentsIndex]; - } while (comment && comment.loc.start.line <= lineNumber); - - // and step back by one - comment = comments[--commentsIndex]; - - if (isFullLineComment(line, lineNumber, comment)) { - lineIsComment = true; - textToMeasure = line; - } else if ( - ignoreTrailingComments && - isTrailingComment(line, lineNumber, comment) - ) { - textToMeasure = stripTrailingComment(line, comment); - - // ignore multiple trailing comments in the same line - let lastIndex = commentsIndex; - - while ( - isTrailingComment( - textToMeasure, - lineNumber, - comments[--lastIndex], - ) - ) { - textToMeasure = stripTrailingComment( - textToMeasure, - comments[lastIndex], - ); - } - } else { - textToMeasure = line; - } - } else { - textToMeasure = line; - } - if ( - (ignorePattern && ignorePattern.test(textToMeasure)) || - (ignoreUrls && URL_REGEXP.test(textToMeasure)) || - (ignoreStrings && stringsByLine[lineNumber]) || - (ignoreTemplateLiterals && - templateLiteralsByLine[lineNumber]) || - (ignoreRegExpLiterals && regExpLiteralsByLine[lineNumber]) - ) { - // ignore this line - return; - } - - const lineLength = computeLineLength(textToMeasure, tabWidth); - const commentLengthApplies = lineIsComment && maxCommentLength; - - if (lineIsComment && ignoreComments) { - return; - } - - const loc = { - start: { - line: lineNumber, - column: 0, - }, - end: { - line: lineNumber, - column: textToMeasure.length, - }, - }; - - if (commentLengthApplies) { - if (lineLength > maxCommentLength) { - context.report({ - node, - loc, - messageId: "maxComment", - data: { - lineLength, - maxCommentLength, - }, - }); - } - } else if (lineLength > maxLength) { - context.report({ - node, - loc, - messageId: "max", - data: { - lineLength, - maxLength, - }, - }); - } - }); - } - - //-------------------------------------------------------------------------- - // Public API - //-------------------------------------------------------------------------- - - return { - Program: checkProgramForMaxLength, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Enforce a maximum line length", + recommended: false, + url: "https://eslint.org/docs/latest/rules/max-len" + }, + + schema: [ + OPTIONS_OR_INTEGER_SCHEMA, + OPTIONS_OR_INTEGER_SCHEMA, + OPTIONS_SCHEMA + ], + messages: { + max: "This line has a length of {{lineLength}}. Maximum allowed is {{maxLength}}.", + maxComment: "This line has a comment length of {{lineLength}}. Maximum allowed is {{maxCommentLength}}." + } + }, + + create(context) { + + /* + * Inspired by http://tools.ietf.org/html/rfc3986#appendix-B, however: + * - They're matching an entire string that we know is a URI + * - We're matching part of a string where we think there *might* be a URL + * - We're only concerned about URLs, as picking out any URI would cause + * too many false positives + * - We don't care about matching the entire URL, any small segment is fine + */ + const URL_REGEXP = /[^:/?#]:\/\/[^?#]/u; + + const sourceCode = context.sourceCode; + + /** + * Computes the length of a line that may contain tabs. The width of each + * tab will be the number of spaces to the next tab stop. + * @param {string} line The line. + * @param {int} tabWidth The width of each tab stop in spaces. + * @returns {int} The computed line length. + * @private + */ + function computeLineLength(line, tabWidth) { + let extraCharacterCount = 0; + + line.replace(/\t/gu, (match, offset) => { + const totalOffset = offset + extraCharacterCount, + previousTabStopOffset = tabWidth ? totalOffset % tabWidth : 0, + spaceCount = tabWidth - previousTabStopOffset; + + extraCharacterCount += spaceCount - 1; // -1 for the replaced tab + }); + return Array.from(line).length + extraCharacterCount; + } + + // The options object must be the last option specified… + const options = Object.assign({}, context.options[context.options.length - 1]); + + // …but max code length… + if (typeof context.options[0] === "number") { + options.code = context.options[0]; + } + + // …and tabWidth can be optionally specified directly as integers. + if (typeof context.options[1] === "number") { + options.tabWidth = context.options[1]; + } + + const maxLength = typeof options.code === "number" ? options.code : 80, + tabWidth = typeof options.tabWidth === "number" ? options.tabWidth : 4, + ignoreComments = !!options.ignoreComments, + ignoreStrings = !!options.ignoreStrings, + ignoreTemplateLiterals = !!options.ignoreTemplateLiterals, + ignoreRegExpLiterals = !!options.ignoreRegExpLiterals, + ignoreTrailingComments = !!options.ignoreTrailingComments || !!options.ignoreComments, + ignoreUrls = !!options.ignoreUrls, + maxCommentLength = options.comments; + let ignorePattern = options.ignorePattern || null; + + if (ignorePattern) { + ignorePattern = new RegExp(ignorePattern, "u"); + } + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + /** + * Tells if a given comment is trailing: it starts on the current line and + * extends to or past the end of the current line. + * @param {string} line The source line we want to check for a trailing comment on + * @param {number} lineNumber The one-indexed line number for line + * @param {ASTNode} comment The comment to inspect + * @returns {boolean} If the comment is trailing on the given line + */ + function isTrailingComment(line, lineNumber, comment) { + return comment && + (comment.loc.start.line === lineNumber && lineNumber <= comment.loc.end.line) && + (comment.loc.end.line > lineNumber || comment.loc.end.column === line.length); + } + + /** + * Tells if a comment encompasses the entire line. + * @param {string} line The source line with a trailing comment + * @param {number} lineNumber The one-indexed line number this is on + * @param {ASTNode} comment The comment to remove + * @returns {boolean} If the comment covers the entire line + */ + function isFullLineComment(line, lineNumber, comment) { + const start = comment.loc.start, + end = comment.loc.end, + isFirstTokenOnLine = !line.slice(0, comment.loc.start.column).trim(); + + return comment && + (start.line < lineNumber || (start.line === lineNumber && isFirstTokenOnLine)) && + (end.line > lineNumber || (end.line === lineNumber && end.column === line.length)); + } + + /** + * Check if a node is a JSXEmptyExpression contained in a single line JSXExpressionContainer. + * @param {ASTNode} node A node to check. + * @returns {boolean} True if the node is a JSXEmptyExpression contained in a single line JSXExpressionContainer. + */ + function isJSXEmptyExpressionInSingleLineContainer(node) { + if (!node || !node.parent || node.type !== "JSXEmptyExpression" || node.parent.type !== "JSXExpressionContainer") { + return false; + } + + const parent = node.parent; + + return parent.loc.start.line === parent.loc.end.line; + } + + /** + * Gets the line after the comment and any remaining trailing whitespace is + * stripped. + * @param {string} line The source line with a trailing comment + * @param {ASTNode} comment The comment to remove + * @returns {string} Line without comment and trailing whitespace + */ + function stripTrailingComment(line, comment) { + + // loc.column is zero-indexed + return line.slice(0, comment.loc.start.column).replace(/\s+$/u, ""); + } + + /** + * Ensure that an array exists at [key] on `object`, and add `value` to it. + * @param {Object} object the object to mutate + * @param {string} key the object's key + * @param {any} value the value to add + * @returns {void} + * @private + */ + function ensureArrayAndPush(object, key, value) { + if (!Array.isArray(object[key])) { + object[key] = []; + } + object[key].push(value); + } + + /** + * Retrieves an array containing all strings (" or ') in the source code. + * @returns {ASTNode[]} An array of string nodes. + */ + function getAllStrings() { + return sourceCode.ast.tokens.filter(token => (token.type === "String" || + (token.type === "JSXText" && sourceCode.getNodeByRangeIndex(token.range[0] - 1).type === "JSXAttribute"))); + } + + /** + * Retrieves an array containing all template literals in the source code. + * @returns {ASTNode[]} An array of template literal nodes. + */ + function getAllTemplateLiterals() { + return sourceCode.ast.tokens.filter(token => token.type === "Template"); + } + + + /** + * Retrieves an array containing all RegExp literals in the source code. + * @returns {ASTNode[]} An array of RegExp literal nodes. + */ + function getAllRegExpLiterals() { + return sourceCode.ast.tokens.filter(token => token.type === "RegularExpression"); + } + + /** + * + * reduce an array of AST nodes by line number, both start and end. + * @param {ASTNode[]} arr array of AST nodes + * @returns {Object} accululated AST nodes + */ + function groupArrayByLineNumber(arr) { + const obj = {}; + + for (let i = 0; i < arr.length; i++) { + const node = arr[i]; + + for (let j = node.loc.start.line; j <= node.loc.end.line; ++j) { + ensureArrayAndPush(obj, j, node); + } + } + return obj; + } + + /** + * Returns an array of all comments in the source code. + * If the element in the array is a JSXEmptyExpression contained with a single line JSXExpressionContainer, + * the element is changed with JSXExpressionContainer node. + * @returns {ASTNode[]} An array of comment nodes + */ + function getAllComments() { + const comments = []; + + sourceCode.getAllComments() + .forEach(commentNode => { + const containingNode = sourceCode.getNodeByRangeIndex(commentNode.range[0]); + + if (isJSXEmptyExpressionInSingleLineContainer(containingNode)) { + + // push a unique node only + if (comments[comments.length - 1] !== containingNode.parent) { + comments.push(containingNode.parent); + } + } else { + comments.push(commentNode); + } + }); + + return comments; + } + + /** + * Check the program for max length + * @param {ASTNode} node Node to examine + * @returns {void} + * @private + */ + function checkProgramForMaxLength(node) { + + // split (honors line-ending) + const lines = sourceCode.lines, + + // list of comments to ignore + comments = ignoreComments || maxCommentLength || ignoreTrailingComments ? getAllComments() : []; + + // we iterate over comments in parallel with the lines + let commentsIndex = 0; + + const strings = getAllStrings(); + const stringsByLine = groupArrayByLineNumber(strings); + + const templateLiterals = getAllTemplateLiterals(); + const templateLiteralsByLine = groupArrayByLineNumber(templateLiterals); + + const regExpLiterals = getAllRegExpLiterals(); + const regExpLiteralsByLine = groupArrayByLineNumber(regExpLiterals); + + lines.forEach((line, i) => { + + // i is zero-indexed, line numbers are one-indexed + const lineNumber = i + 1; + + /* + * if we're checking comment length; we need to know whether this + * line is a comment + */ + let lineIsComment = false; + let textToMeasure; + + /* + * We can short-circuit the comment checks if we're already out of + * comments to check. + */ + if (commentsIndex < comments.length) { + let comment = null; + + // iterate over comments until we find one past the current line + do { + comment = comments[++commentsIndex]; + } while (comment && comment.loc.start.line <= lineNumber); + + // and step back by one + comment = comments[--commentsIndex]; + + if (isFullLineComment(line, lineNumber, comment)) { + lineIsComment = true; + textToMeasure = line; + } else if (ignoreTrailingComments && isTrailingComment(line, lineNumber, comment)) { + textToMeasure = stripTrailingComment(line, comment); + + // ignore multiple trailing comments in the same line + let lastIndex = commentsIndex; + + while (isTrailingComment(textToMeasure, lineNumber, comments[--lastIndex])) { + textToMeasure = stripTrailingComment(textToMeasure, comments[lastIndex]); + } + } else { + textToMeasure = line; + } + } else { + textToMeasure = line; + } + if (ignorePattern && ignorePattern.test(textToMeasure) || + ignoreUrls && URL_REGEXP.test(textToMeasure) || + ignoreStrings && stringsByLine[lineNumber] || + ignoreTemplateLiterals && templateLiteralsByLine[lineNumber] || + ignoreRegExpLiterals && regExpLiteralsByLine[lineNumber] + ) { + + // ignore this line + return; + } + + const lineLength = computeLineLength(textToMeasure, tabWidth); + const commentLengthApplies = lineIsComment && maxCommentLength; + + if (lineIsComment && ignoreComments) { + return; + } + + const loc = { + start: { + line: lineNumber, + column: 0 + }, + end: { + line: lineNumber, + column: textToMeasure.length + } + }; + + if (commentLengthApplies) { + if (lineLength > maxCommentLength) { + context.report({ + node, + loc, + messageId: "maxComment", + data: { + lineLength, + maxCommentLength + } + }); + } + } else if (lineLength > maxLength) { + context.report({ + node, + loc, + messageId: "max", + data: { + lineLength, + maxLength + } + }); + } + }); + } + + + //-------------------------------------------------------------------------- + // Public API + //-------------------------------------------------------------------------- + + return { + Program: checkProgramForMaxLength + }; + + } }; diff --git a/node_modules/eslint/lib/rules/max-lines-per-function.js b/node_modules/eslint/lib/rules/max-lines-per-function.js index fd9a783f4..f981922a7 100644 --- a/node_modules/eslint/lib/rules/max-lines-per-function.js +++ b/node_modules/eslint/lib/rules/max-lines-per-function.js @@ -16,33 +16,33 @@ const { upperCaseFirst } = require("../shared/string-utils"); //------------------------------------------------------------------------------ const OPTIONS_SCHEMA = { - type: "object", - properties: { - max: { - type: "integer", - minimum: 0, - }, - skipComments: { - type: "boolean", - }, - skipBlankLines: { - type: "boolean", - }, - IIFEs: { - type: "boolean", - }, - }, - additionalProperties: false, + type: "object", + properties: { + max: { + type: "integer", + minimum: 0 + }, + skipComments: { + type: "boolean" + }, + skipBlankLines: { + type: "boolean" + }, + IIFEs: { + type: "boolean" + } + }, + additionalProperties: false }; const OPTIONS_OR_INTEGER_SCHEMA = { - oneOf: [ - OPTIONS_SCHEMA, - { - type: "integer", - minimum: 1, - }, - ], + oneOf: [ + OPTIONS_SCHEMA, + { + type: "integer", + minimum: 1 + } + ] }; /** @@ -51,188 +51,163 @@ const OPTIONS_OR_INTEGER_SCHEMA = { * @returns {Map} A map with numeric keys (source code line numbers) and comment token values. */ function getCommentLineNumbers(comments) { - const map = new Map(); - - comments.forEach(comment => { - for (let i = comment.loc.start.line; i <= comment.loc.end.line; i++) { - map.set(i, comment); - } - }); - return map; + const map = new Map(); + + comments.forEach(comment => { + for (let i = comment.loc.start.line; i <= comment.loc.end.line; i++) { + map.set(i, comment); + } + }); + return map; } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: - "Enforce a maximum number of lines of code in a function", - recommended: false, - url: "https://eslint.org/docs/latest/rules/max-lines-per-function", - }, - - schema: [OPTIONS_OR_INTEGER_SCHEMA], - messages: { - exceed: "{{name}} has too many lines ({{lineCount}}). Maximum allowed is {{maxLines}}.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - const lines = sourceCode.lines; - - const option = context.options[0]; - let maxLines = 50; - let skipComments = false; - let skipBlankLines = false; - let IIFEs = false; - - if (typeof option === "object") { - maxLines = typeof option.max === "number" ? option.max : 50; - skipComments = !!option.skipComments; - skipBlankLines = !!option.skipBlankLines; - IIFEs = !!option.IIFEs; - } else if (typeof option === "number") { - maxLines = option; - } - - const commentLineNumbers = getCommentLineNumbers( - sourceCode.getAllComments(), - ); - - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - /** - * Tells if a comment encompasses the entire line. - * @param {string} line The source line with a trailing comment - * @param {number} lineNumber The one-indexed line number this is on - * @param {ASTNode} comment The comment to remove - * @returns {boolean} If the comment covers the entire line - */ - function isFullLineComment(line, lineNumber, comment) { - const start = comment.loc.start, - end = comment.loc.end, - isFirstTokenOnLine = - start.line === lineNumber && - !line.slice(0, start.column).trim(), - isLastTokenOnLine = - end.line === lineNumber && !line.slice(end.column).trim(); - - return ( - comment && - (start.line < lineNumber || isFirstTokenOnLine) && - (end.line > lineNumber || isLastTokenOnLine) - ); - } - - /** - * Identifies is a node is a FunctionExpression which is part of an IIFE - * @param {ASTNode} node Node to test - * @returns {boolean} True if it's an IIFE - */ - function isIIFE(node) { - return ( - (node.type === "FunctionExpression" || - node.type === "ArrowFunctionExpression") && - node.parent && - node.parent.type === "CallExpression" && - node.parent.callee === node - ); - } - - /** - * Identifies is a node is a FunctionExpression which is embedded within a MethodDefinition or Property - * @param {ASTNode} node Node to test - * @returns {boolean} True if it's a FunctionExpression embedded within a MethodDefinition or Property - */ - function isEmbedded(node) { - if (!node.parent) { - return false; - } - if (node !== node.parent.value) { - return false; - } - if (node.parent.type === "MethodDefinition") { - return true; - } - if (node.parent.type === "Property") { - return ( - node.parent.method === true || - node.parent.kind === "get" || - node.parent.kind === "set" - ); - } - return false; - } - - /** - * Count the lines in the function - * @param {ASTNode} funcNode Function AST node - * @returns {void} - * @private - */ - function processFunction(funcNode) { - const node = isEmbedded(funcNode) ? funcNode.parent : funcNode; - - if (!IIFEs && isIIFE(node)) { - return; - } - let lineCount = 0; - - for (let i = node.loc.start.line - 1; i < node.loc.end.line; ++i) { - const line = lines[i]; - - if (skipComments) { - if ( - commentLineNumbers.has(i + 1) && - isFullLineComment( - line, - i + 1, - commentLineNumbers.get(i + 1), - ) - ) { - continue; - } - } - - if (skipBlankLines) { - if (line.match(/^\s*$/u)) { - continue; - } - } - - lineCount++; - } - - if (lineCount > maxLines) { - const name = upperCaseFirst( - astUtils.getFunctionNameWithKind(funcNode), - ); - - context.report({ - node, - messageId: "exceed", - data: { name, lineCount, maxLines }, - }); - } - } - - //-------------------------------------------------------------------------- - // Public API - //-------------------------------------------------------------------------- - - return { - FunctionDeclaration: processFunction, - FunctionExpression: processFunction, - ArrowFunctionExpression: processFunction, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Enforce a maximum number of lines of code in a function", + recommended: false, + url: "https://eslint.org/docs/latest/rules/max-lines-per-function" + }, + + schema: [ + OPTIONS_OR_INTEGER_SCHEMA + ], + messages: { + exceed: "{{name}} has too many lines ({{lineCount}}). Maximum allowed is {{maxLines}}." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + const lines = sourceCode.lines; + + const option = context.options[0]; + let maxLines = 50; + let skipComments = false; + let skipBlankLines = false; + let IIFEs = false; + + if (typeof option === "object") { + maxLines = typeof option.max === "number" ? option.max : 50; + skipComments = !!option.skipComments; + skipBlankLines = !!option.skipBlankLines; + IIFEs = !!option.IIFEs; + } else if (typeof option === "number") { + maxLines = option; + } + + const commentLineNumbers = getCommentLineNumbers(sourceCode.getAllComments()); + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + /** + * Tells if a comment encompasses the entire line. + * @param {string} line The source line with a trailing comment + * @param {number} lineNumber The one-indexed line number this is on + * @param {ASTNode} comment The comment to remove + * @returns {boolean} If the comment covers the entire line + */ + function isFullLineComment(line, lineNumber, comment) { + const start = comment.loc.start, + end = comment.loc.end, + isFirstTokenOnLine = start.line === lineNumber && !line.slice(0, start.column).trim(), + isLastTokenOnLine = end.line === lineNumber && !line.slice(end.column).trim(); + + return comment && + (start.line < lineNumber || isFirstTokenOnLine) && + (end.line > lineNumber || isLastTokenOnLine); + } + + /** + * Identifies is a node is a FunctionExpression which is part of an IIFE + * @param {ASTNode} node Node to test + * @returns {boolean} True if it's an IIFE + */ + function isIIFE(node) { + return (node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression") && node.parent && node.parent.type === "CallExpression" && node.parent.callee === node; + } + + /** + * Identifies is a node is a FunctionExpression which is embedded within a MethodDefinition or Property + * @param {ASTNode} node Node to test + * @returns {boolean} True if it's a FunctionExpression embedded within a MethodDefinition or Property + */ + function isEmbedded(node) { + if (!node.parent) { + return false; + } + if (node !== node.parent.value) { + return false; + } + if (node.parent.type === "MethodDefinition") { + return true; + } + if (node.parent.type === "Property") { + return node.parent.method === true || node.parent.kind === "get" || node.parent.kind === "set"; + } + return false; + } + + /** + * Count the lines in the function + * @param {ASTNode} funcNode Function AST node + * @returns {void} + * @private + */ + function processFunction(funcNode) { + const node = isEmbedded(funcNode) ? funcNode.parent : funcNode; + + if (!IIFEs && isIIFE(node)) { + return; + } + let lineCount = 0; + + for (let i = node.loc.start.line - 1; i < node.loc.end.line; ++i) { + const line = lines[i]; + + if (skipComments) { + if (commentLineNumbers.has(i + 1) && isFullLineComment(line, i + 1, commentLineNumbers.get(i + 1))) { + continue; + } + } + + if (skipBlankLines) { + if (line.match(/^\s*$/u)) { + continue; + } + } + + lineCount++; + } + + if (lineCount > maxLines) { + const name = upperCaseFirst(astUtils.getFunctionNameWithKind(funcNode)); + + context.report({ + node, + messageId: "exceed", + data: { name, lineCount, maxLines } + }); + } + } + + //-------------------------------------------------------------------------- + // Public API + //-------------------------------------------------------------------------- + + return { + FunctionDeclaration: processFunction, + FunctionExpression: processFunction, + ArrowFunctionExpression: processFunction + }; + } }; diff --git a/node_modules/eslint/lib/rules/max-lines.js b/node_modules/eslint/lib/rules/max-lines.js index eb8aeb1a7..e85d44290 100644 --- a/node_modules/eslint/lib/rules/max-lines.js +++ b/node_modules/eslint/lib/rules/max-lines.js @@ -21,169 +21,173 @@ const astUtils = require("./utils/ast-utils"); * @returns {number[]} The range of numbers */ function range(start, end) { - return [...Array(end - start).keys()].map(x => x + start); + return [...Array(end - start).keys()].map(x => x + start); } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: "Enforce a maximum number of lines per file", - recommended: false, - url: "https://eslint.org/docs/latest/rules/max-lines", - }, - - schema: [ - { - oneOf: [ - { - type: "integer", - minimum: 0, - }, - { - type: "object", - properties: { - max: { - type: "integer", - minimum: 0, - }, - skipComments: { - type: "boolean", - }, - skipBlankLines: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - }, - ], - messages: { - exceed: "File has too many lines ({{actual}}). Maximum allowed is {{max}}.", - }, - }, - - create(context) { - const option = context.options[0]; - let max = 300; - - if (typeof option === "object" && Object.hasOwn(option, "max")) { - max = option.max; - } else if (typeof option === "number") { - max = option; - } - - const skipComments = option && option.skipComments; - const skipBlankLines = option && option.skipBlankLines; - - const sourceCode = context.sourceCode; - - /** - * Returns whether or not a token is a comment node type - * @param {Token} token The token to check - * @returns {boolean} True if the token is a comment node - */ - function isCommentNodeType(token) { - return token && (token.type === "Block" || token.type === "Line"); - } - - /** - * Returns the line numbers of a comment that don't have any code on the same line - * @param {Node} comment The comment node to check - * @returns {number[]} The line numbers - */ - function getLinesWithoutCode(comment) { - let start = comment.loc.start.line; - let end = comment.loc.end.line; - - let token; - - token = comment; - do { - token = sourceCode.getTokenBefore(token, { - includeComments: true, - }); - } while (isCommentNodeType(token)); - - if (token && astUtils.isTokenOnSameLine(token, comment)) { - start += 1; - } - - token = comment; - do { - token = sourceCode.getTokenAfter(token, { - includeComments: true, - }); - } while (isCommentNodeType(token)); - - if (token && astUtils.isTokenOnSameLine(comment, token)) { - end -= 1; - } - - if (start <= end) { - return range(start, end + 1); - } - return []; - } - - return { - "Program:exit"() { - let lines = sourceCode.lines.map((text, i) => ({ - lineNumber: i + 1, - text, - })); - - /* - * If file ends with a linebreak, `sourceCode.lines` will have one extra empty line at the end. - * That isn't a real line, so we shouldn't count it. - */ - if (lines.length > 1 && lines.at(-1).text === "") { - lines.pop(); - } - - if (skipBlankLines) { - lines = lines.filter(l => l.text.trim() !== ""); - } - - if (skipComments) { - const comments = sourceCode.getAllComments(); - - const commentLines = new Set( - comments.flatMap(getLinesWithoutCode), - ); - - lines = lines.filter(l => !commentLines.has(l.lineNumber)); - } - - if (lines.length > max) { - const loc = { - start: { - line: lines[max].lineNumber, - column: 0, - }, - end: { - line: sourceCode.lines.length, - column: sourceCode.lines.at(-1).length, - }, - }; - - context.report({ - loc, - messageId: "exceed", - data: { - max, - actual: lines.length, - }, - }); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Enforce a maximum number of lines per file", + recommended: false, + url: "https://eslint.org/docs/latest/rules/max-lines" + }, + + schema: [ + { + oneOf: [ + { + type: "integer", + minimum: 0 + }, + { + type: "object", + properties: { + max: { + type: "integer", + minimum: 0 + }, + skipComments: { + type: "boolean" + }, + skipBlankLines: { + type: "boolean" + } + }, + additionalProperties: false + } + ] + } + ], + messages: { + exceed: + "File has too many lines ({{actual}}). Maximum allowed is {{max}}." + } + }, + + create(context) { + const option = context.options[0]; + let max = 300; + + if ( + typeof option === "object" && + Object.prototype.hasOwnProperty.call(option, "max") + ) { + max = option.max; + } else if (typeof option === "number") { + max = option; + } + + const skipComments = option && option.skipComments; + const skipBlankLines = option && option.skipBlankLines; + + const sourceCode = context.sourceCode; + + /** + * Returns whether or not a token is a comment node type + * @param {Token} token The token to check + * @returns {boolean} True if the token is a comment node + */ + function isCommentNodeType(token) { + return token && (token.type === "Block" || token.type === "Line"); + } + + /** + * Returns the line numbers of a comment that don't have any code on the same line + * @param {Node} comment The comment node to check + * @returns {number[]} The line numbers + */ + function getLinesWithoutCode(comment) { + let start = comment.loc.start.line; + let end = comment.loc.end.line; + + let token; + + token = comment; + do { + token = sourceCode.getTokenBefore(token, { + includeComments: true + }); + } while (isCommentNodeType(token)); + + if (token && astUtils.isTokenOnSameLine(token, comment)) { + start += 1; + } + + token = comment; + do { + token = sourceCode.getTokenAfter(token, { + includeComments: true + }); + } while (isCommentNodeType(token)); + + if (token && astUtils.isTokenOnSameLine(comment, token)) { + end -= 1; + } + + if (start <= end) { + return range(start, end + 1); + } + return []; + } + + return { + "Program:exit"() { + let lines = sourceCode.lines.map((text, i) => ({ + lineNumber: i + 1, + text + })); + + /* + * If file ends with a linebreak, `sourceCode.lines` will have one extra empty line at the end. + * That isn't a real line, so we shouldn't count it. + */ + if (lines.length > 1 && lines[lines.length - 1].text === "") { + lines.pop(); + } + + if (skipBlankLines) { + lines = lines.filter(l => l.text.trim() !== ""); + } + + if (skipComments) { + const comments = sourceCode.getAllComments(); + + const commentLines = new Set(comments.flatMap(getLinesWithoutCode)); + + lines = lines.filter( + l => !commentLines.has(l.lineNumber) + ); + } + + if (lines.length > max) { + const loc = { + start: { + line: lines[max].lineNumber, + column: 0 + }, + end: { + line: sourceCode.lines.length, + column: sourceCode.lines[sourceCode.lines.length - 1].length + } + }; + + context.report({ + loc, + messageId: "exceed", + data: { + max, + actual: lines.length + } + }); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/max-nested-callbacks.js b/node_modules/eslint/lib/rules/max-nested-callbacks.js index e97483a62..d8f380b3c 100644 --- a/node_modules/eslint/lib/rules/max-nested-callbacks.js +++ b/node_modules/eslint/lib/rules/max-nested-callbacks.js @@ -9,107 +9,109 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: "Enforce a maximum depth that callbacks can be nested", - recommended: false, - url: "https://eslint.org/docs/latest/rules/max-nested-callbacks", - }, - - schema: [ - { - oneOf: [ - { - type: "integer", - minimum: 0, - }, - { - type: "object", - properties: { - maximum: { - type: "integer", - minimum: 0, - }, - max: { - type: "integer", - minimum: 0, - }, - }, - additionalProperties: false, - }, - ], - }, - ], - messages: { - exceed: "Too many nested callbacks ({{num}}). Maximum allowed is {{max}}.", - }, - }, - - create(context) { - //-------------------------------------------------------------------------- - // Constants - //-------------------------------------------------------------------------- - const option = context.options[0]; - let THRESHOLD = 10; - - if ( - typeof option === "object" && - (Object.hasOwn(option, "maximum") || Object.hasOwn(option, "max")) - ) { - THRESHOLD = option.maximum || option.max; - } else if (typeof option === "number") { - THRESHOLD = option; - } - - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - const callbackStack = []; - - /** - * Checks a given function node for too many callbacks. - * @param {ASTNode} node The node to check. - * @returns {void} - * @private - */ - function checkFunction(node) { - const parent = node.parent; - - if (parent.type === "CallExpression") { - callbackStack.push(node); - } - - if (callbackStack.length > THRESHOLD) { - const opts = { num: callbackStack.length, max: THRESHOLD }; - - context.report({ node, messageId: "exceed", data: opts }); - } - } - - /** - * Pops the call stack. - * @returns {void} - * @private - */ - function popStack() { - callbackStack.pop(); - } - - //-------------------------------------------------------------------------- - // Public API - //-------------------------------------------------------------------------- - - return { - ArrowFunctionExpression: checkFunction, - "ArrowFunctionExpression:exit": popStack, - - FunctionExpression: checkFunction, - "FunctionExpression:exit": popStack, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Enforce a maximum depth that callbacks can be nested", + recommended: false, + url: "https://eslint.org/docs/latest/rules/max-nested-callbacks" + }, + + schema: [ + { + oneOf: [ + { + type: "integer", + minimum: 0 + }, + { + type: "object", + properties: { + maximum: { + type: "integer", + minimum: 0 + }, + max: { + type: "integer", + minimum: 0 + } + }, + additionalProperties: false + } + ] + } + ], + messages: { + exceed: "Too many nested callbacks ({{num}}). Maximum allowed is {{max}}." + } + }, + + create(context) { + + //-------------------------------------------------------------------------- + // Constants + //-------------------------------------------------------------------------- + const option = context.options[0]; + let THRESHOLD = 10; + + if ( + typeof option === "object" && + (Object.prototype.hasOwnProperty.call(option, "maximum") || Object.prototype.hasOwnProperty.call(option, "max")) + ) { + THRESHOLD = option.maximum || option.max; + } else if (typeof option === "number") { + THRESHOLD = option; + } + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + const callbackStack = []; + + /** + * Checks a given function node for too many callbacks. + * @param {ASTNode} node The node to check. + * @returns {void} + * @private + */ + function checkFunction(node) { + const parent = node.parent; + + if (parent.type === "CallExpression") { + callbackStack.push(node); + } + + if (callbackStack.length > THRESHOLD) { + const opts = { num: callbackStack.length, max: THRESHOLD }; + + context.report({ node, messageId: "exceed", data: opts }); + } + } + + /** + * Pops the call stack. + * @returns {void} + * @private + */ + function popStack() { + callbackStack.pop(); + } + + //-------------------------------------------------------------------------- + // Public API + //-------------------------------------------------------------------------- + + return { + ArrowFunctionExpression: checkFunction, + "ArrowFunctionExpression:exit": popStack, + + FunctionExpression: checkFunction, + "FunctionExpression:exit": popStack + }; + + } }; diff --git a/node_modules/eslint/lib/rules/max-params.js b/node_modules/eslint/lib/rules/max-params.js index eeb61edd4..213477a15 100644 --- a/node_modules/eslint/lib/rules/max-params.js +++ b/node_modules/eslint/lib/rules/max-params.js @@ -16,114 +16,87 @@ const { upperCaseFirst } = require("../shared/string-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - dialects: ["typescript", "javascript"], - language: "javascript", + meta: { + type: "suggestion", - docs: { - description: - "Enforce a maximum number of parameters in function definitions", - recommended: false, - url: "https://eslint.org/docs/latest/rules/max-params", - }, + docs: { + description: "Enforce a maximum number of parameters in function definitions", + recommended: false, + url: "https://eslint.org/docs/latest/rules/max-params" + }, - schema: [ - { - oneOf: [ - { - type: "integer", - minimum: 0, - }, - { - type: "object", - properties: { - maximum: { - type: "integer", - minimum: 0, - }, - max: { - type: "integer", - minimum: 0, - }, - countVoidThis: { - type: "boolean", - description: - "Whether to count a `this` declaration when the type is `void`.", - }, - }, - additionalProperties: false, - }, - ], - }, - ], - messages: { - exceed: "{{name}} has too many parameters ({{count}}). Maximum allowed is {{max}}.", - }, - }, + schema: [ + { + oneOf: [ + { + type: "integer", + minimum: 0 + }, + { + type: "object", + properties: { + maximum: { + type: "integer", + minimum: 0 + }, + max: { + type: "integer", + minimum: 0 + } + }, + additionalProperties: false + } + ] + } + ], + messages: { + exceed: "{{name}} has too many parameters ({{count}}). Maximum allowed is {{max}}." + } + }, - create(context) { - const sourceCode = context.sourceCode; - const option = context.options[0]; - let numParams = 3; - let countVoidThis = false; + create(context) { + const sourceCode = context.sourceCode; + const option = context.options[0]; + let numParams = 3; - if (typeof option === "object") { - if ( - Object.hasOwn(option, "maximum") || - Object.hasOwn(option, "max") - ) { - numParams = option.maximum || option.max; - } - countVoidThis = option.countVoidThis; - } - if (typeof option === "number") { - numParams = option; - } + if ( + typeof option === "object" && + (Object.prototype.hasOwnProperty.call(option, "maximum") || Object.prototype.hasOwnProperty.call(option, "max")) + ) { + numParams = option.maximum || option.max; + } + if (typeof option === "number") { + numParams = option; + } - /** - * Checks a function to see if it has too many parameters. - * @param {ASTNode} node The node to check. - * @returns {void} - * @private - */ - function checkFunction(node) { - const hasVoidThisParam = - node.params.length > 0 && - node.params[0].type === "Identifier" && - node.params[0].name === "this" && - node.params[0].typeAnnotation?.typeAnnotation.type === - "TSVoidKeyword"; + /** + * Checks a function to see if it has too many parameters. + * @param {ASTNode} node The node to check. + * @returns {void} + * @private + */ + function checkFunction(node) { + if (node.params.length > numParams) { + context.report({ + loc: astUtils.getFunctionHeadLoc(node, sourceCode), + node, + messageId: "exceed", + data: { + name: upperCaseFirst(astUtils.getFunctionNameWithKind(node)), + count: node.params.length, + max: numParams + } + }); + } + } - const effectiveParamCount = - hasVoidThisParam && !countVoidThis - ? node.params.length - 1 - : node.params.length; + return { + FunctionDeclaration: checkFunction, + ArrowFunctionExpression: checkFunction, + FunctionExpression: checkFunction + }; - if (effectiveParamCount > numParams) { - context.report({ - loc: astUtils.getFunctionHeadLoc(node, sourceCode), - node, - messageId: "exceed", - data: { - name: upperCaseFirst( - astUtils.getFunctionNameWithKind(node), - ), - count: effectiveParamCount, - max: numParams, - }, - }); - } - } - - return { - FunctionDeclaration: checkFunction, - ArrowFunctionExpression: checkFunction, - FunctionExpression: checkFunction, - TSDeclareFunction: checkFunction, - TSFunctionType: checkFunction, - }; - }, + } }; diff --git a/node_modules/eslint/lib/rules/max-statements-per-line.js b/node_modules/eslint/lib/rules/max-statements-per-line.js index 48b37f4fc..4ad73a67f 100644 --- a/node_modules/eslint/lib/rules/max-statements-per-line.js +++ b/node_modules/eslint/lib/rules/max-statements-per-line.js @@ -15,210 +15,185 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "max-statements-per-line", - url: "https://eslint.style/rules/max-statements-per-line", - }, - }, - ], - }, - type: "layout", - - docs: { - description: - "Enforce a maximum number of statements allowed per line", - recommended: false, - url: "https://eslint.org/docs/latest/rules/max-statements-per-line", - }, - - schema: [ - { - type: "object", - properties: { - max: { - type: "integer", - minimum: 1, - default: 1, - }, - }, - additionalProperties: false, - }, - ], - messages: { - exceed: "This line has {{numberOfStatementsOnThisLine}} {{statements}}. Maximum allowed is {{maxStatementsPerLine}}.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode, - options = context.options[0] || {}, - maxStatementsPerLine = - typeof options.max !== "undefined" ? options.max : 1; - - let lastStatementLine = 0, - numberOfStatementsOnThisLine = 0, - firstExtraStatement; - - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - const SINGLE_CHILD_ALLOWED = - /^(?:(?:DoWhile|For|ForIn|ForOf|If|Labeled|While)Statement|Export(?:Default|Named)Declaration)$/u; - - /** - * Reports with the first extra statement, and clears it. - * @returns {void} - */ - function reportFirstExtraStatementAndClear() { - if (firstExtraStatement) { - context.report({ - node: firstExtraStatement, - messageId: "exceed", - data: { - numberOfStatementsOnThisLine, - maxStatementsPerLine, - statements: - numberOfStatementsOnThisLine === 1 - ? "statement" - : "statements", - }, - }); - } - firstExtraStatement = null; - } - - /** - * Gets the actual last token of a given node. - * @param {ASTNode} node A node to get. This is a node except EmptyStatement. - * @returns {Token} The actual last token. - */ - function getActualLastToken(node) { - return sourceCode.getLastToken(node, astUtils.isNotSemicolonToken); - } - - /** - * Addresses a given node. - * It updates the state of this rule, then reports the node if the node violated this rule. - * @param {ASTNode} node A node to check. - * @returns {void} - */ - function enterStatement(node) { - const line = node.loc.start.line; - - /* - * Skip to allow non-block statements if this is direct child of control statements. - * `if (a) foo();` is counted as 1. - * But `if (a) foo(); else foo();` should be counted as 2. - */ - if ( - SINGLE_CHILD_ALLOWED.test(node.parent.type) && - node.parent.alternate !== node - ) { - return; - } - - // Update state. - if (line === lastStatementLine) { - numberOfStatementsOnThisLine += 1; - } else { - reportFirstExtraStatementAndClear(); - numberOfStatementsOnThisLine = 1; - lastStatementLine = line; - } - - // Reports if the node violated this rule. - if (numberOfStatementsOnThisLine === maxStatementsPerLine + 1) { - firstExtraStatement = firstExtraStatement || node; - } - } - - /** - * Updates the state of this rule with the end line of leaving node to check with the next statement. - * @param {ASTNode} node A node to check. - * @returns {void} - */ - function leaveStatement(node) { - const line = getActualLastToken(node).loc.end.line; - - // Update state. - if (line !== lastStatementLine) { - reportFirstExtraStatementAndClear(); - numberOfStatementsOnThisLine = 1; - lastStatementLine = line; - } - } - - //-------------------------------------------------------------------------- - // Public API - //-------------------------------------------------------------------------- - - return { - BreakStatement: enterStatement, - ClassDeclaration: enterStatement, - ContinueStatement: enterStatement, - DebuggerStatement: enterStatement, - DoWhileStatement: enterStatement, - ExpressionStatement: enterStatement, - ForInStatement: enterStatement, - ForOfStatement: enterStatement, - ForStatement: enterStatement, - FunctionDeclaration: enterStatement, - IfStatement: enterStatement, - ImportDeclaration: enterStatement, - LabeledStatement: enterStatement, - ReturnStatement: enterStatement, - SwitchStatement: enterStatement, - ThrowStatement: enterStatement, - TryStatement: enterStatement, - VariableDeclaration: enterStatement, - WhileStatement: enterStatement, - WithStatement: enterStatement, - ExportNamedDeclaration: enterStatement, - ExportDefaultDeclaration: enterStatement, - ExportAllDeclaration: enterStatement, - - "BreakStatement:exit": leaveStatement, - "ClassDeclaration:exit": leaveStatement, - "ContinueStatement:exit": leaveStatement, - "DebuggerStatement:exit": leaveStatement, - "DoWhileStatement:exit": leaveStatement, - "ExpressionStatement:exit": leaveStatement, - "ForInStatement:exit": leaveStatement, - "ForOfStatement:exit": leaveStatement, - "ForStatement:exit": leaveStatement, - "FunctionDeclaration:exit": leaveStatement, - "IfStatement:exit": leaveStatement, - "ImportDeclaration:exit": leaveStatement, - "LabeledStatement:exit": leaveStatement, - "ReturnStatement:exit": leaveStatement, - "SwitchStatement:exit": leaveStatement, - "ThrowStatement:exit": leaveStatement, - "TryStatement:exit": leaveStatement, - "VariableDeclaration:exit": leaveStatement, - "WhileStatement:exit": leaveStatement, - "WithStatement:exit": leaveStatement, - "ExportNamedDeclaration:exit": leaveStatement, - "ExportDefaultDeclaration:exit": leaveStatement, - "ExportAllDeclaration:exit": leaveStatement, - "Program:exit": reportFirstExtraStatementAndClear, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Enforce a maximum number of statements allowed per line", + recommended: false, + url: "https://eslint.org/docs/latest/rules/max-statements-per-line" + }, + + schema: [ + { + type: "object", + properties: { + max: { + type: "integer", + minimum: 1, + default: 1 + } + }, + additionalProperties: false + } + ], + messages: { + exceed: "This line has {{numberOfStatementsOnThisLine}} {{statements}}. Maximum allowed is {{maxStatementsPerLine}}." + } + }, + + create(context) { + + const sourceCode = context.sourceCode, + options = context.options[0] || {}, + maxStatementsPerLine = typeof options.max !== "undefined" ? options.max : 1; + + let lastStatementLine = 0, + numberOfStatementsOnThisLine = 0, + firstExtraStatement; + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + const SINGLE_CHILD_ALLOWED = /^(?:(?:DoWhile|For|ForIn|ForOf|If|Labeled|While)Statement|Export(?:Default|Named)Declaration)$/u; + + /** + * Reports with the first extra statement, and clears it. + * @returns {void} + */ + function reportFirstExtraStatementAndClear() { + if (firstExtraStatement) { + context.report({ + node: firstExtraStatement, + messageId: "exceed", + data: { + numberOfStatementsOnThisLine, + maxStatementsPerLine, + statements: numberOfStatementsOnThisLine === 1 ? "statement" : "statements" + } + }); + } + firstExtraStatement = null; + } + + /** + * Gets the actual last token of a given node. + * @param {ASTNode} node A node to get. This is a node except EmptyStatement. + * @returns {Token} The actual last token. + */ + function getActualLastToken(node) { + return sourceCode.getLastToken(node, astUtils.isNotSemicolonToken); + } + + /** + * Addresses a given node. + * It updates the state of this rule, then reports the node if the node violated this rule. + * @param {ASTNode} node A node to check. + * @returns {void} + */ + function enterStatement(node) { + const line = node.loc.start.line; + + /* + * Skip to allow non-block statements if this is direct child of control statements. + * `if (a) foo();` is counted as 1. + * But `if (a) foo(); else foo();` should be counted as 2. + */ + if (SINGLE_CHILD_ALLOWED.test(node.parent.type) && + node.parent.alternate !== node + ) { + return; + } + + // Update state. + if (line === lastStatementLine) { + numberOfStatementsOnThisLine += 1; + } else { + reportFirstExtraStatementAndClear(); + numberOfStatementsOnThisLine = 1; + lastStatementLine = line; + } + + // Reports if the node violated this rule. + if (numberOfStatementsOnThisLine === maxStatementsPerLine + 1) { + firstExtraStatement = firstExtraStatement || node; + } + } + + /** + * Updates the state of this rule with the end line of leaving node to check with the next statement. + * @param {ASTNode} node A node to check. + * @returns {void} + */ + function leaveStatement(node) { + const line = getActualLastToken(node).loc.end.line; + + // Update state. + if (line !== lastStatementLine) { + reportFirstExtraStatementAndClear(); + numberOfStatementsOnThisLine = 1; + lastStatementLine = line; + } + } + + //-------------------------------------------------------------------------- + // Public API + //-------------------------------------------------------------------------- + + return { + BreakStatement: enterStatement, + ClassDeclaration: enterStatement, + ContinueStatement: enterStatement, + DebuggerStatement: enterStatement, + DoWhileStatement: enterStatement, + ExpressionStatement: enterStatement, + ForInStatement: enterStatement, + ForOfStatement: enterStatement, + ForStatement: enterStatement, + FunctionDeclaration: enterStatement, + IfStatement: enterStatement, + ImportDeclaration: enterStatement, + LabeledStatement: enterStatement, + ReturnStatement: enterStatement, + SwitchStatement: enterStatement, + ThrowStatement: enterStatement, + TryStatement: enterStatement, + VariableDeclaration: enterStatement, + WhileStatement: enterStatement, + WithStatement: enterStatement, + ExportNamedDeclaration: enterStatement, + ExportDefaultDeclaration: enterStatement, + ExportAllDeclaration: enterStatement, + + "BreakStatement:exit": leaveStatement, + "ClassDeclaration:exit": leaveStatement, + "ContinueStatement:exit": leaveStatement, + "DebuggerStatement:exit": leaveStatement, + "DoWhileStatement:exit": leaveStatement, + "ExpressionStatement:exit": leaveStatement, + "ForInStatement:exit": leaveStatement, + "ForOfStatement:exit": leaveStatement, + "ForStatement:exit": leaveStatement, + "FunctionDeclaration:exit": leaveStatement, + "IfStatement:exit": leaveStatement, + "ImportDeclaration:exit": leaveStatement, + "LabeledStatement:exit": leaveStatement, + "ReturnStatement:exit": leaveStatement, + "SwitchStatement:exit": leaveStatement, + "ThrowStatement:exit": leaveStatement, + "TryStatement:exit": leaveStatement, + "VariableDeclaration:exit": leaveStatement, + "WhileStatement:exit": leaveStatement, + "WithStatement:exit": leaveStatement, + "ExportNamedDeclaration:exit": leaveStatement, + "ExportDefaultDeclaration:exit": leaveStatement, + "ExportAllDeclaration:exit": leaveStatement, + "Program:exit": reportFirstExtraStatementAndClear + }; + } }; diff --git a/node_modules/eslint/lib/rules/max-statements.js b/node_modules/eslint/lib/rules/max-statements.js index c38482cd1..78053831f 100644 --- a/node_modules/eslint/lib/rules/max-statements.js +++ b/node_modules/eslint/lib/rules/max-statements.js @@ -16,173 +16,169 @@ const { upperCaseFirst } = require("../shared/string-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: - "Enforce a maximum number of statements allowed in function blocks", - recommended: false, - url: "https://eslint.org/docs/latest/rules/max-statements", - }, - - schema: [ - { - oneOf: [ - { - type: "integer", - minimum: 0, - }, - { - type: "object", - properties: { - maximum: { - type: "integer", - minimum: 0, - }, - max: { - type: "integer", - minimum: 0, - }, - }, - additionalProperties: false, - }, - ], - }, - { - type: "object", - properties: { - ignoreTopLevelFunctions: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - messages: { - exceed: "{{name}} has too many statements ({{count}}). Maximum allowed is {{max}}.", - }, - }, - - create(context) { - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - const functionStack = [], - option = context.options[0], - ignoreTopLevelFunctions = - (context.options[1] && - context.options[1].ignoreTopLevelFunctions) || - false, - topLevelFunctions = []; - let maxStatements = 10; - - if ( - typeof option === "object" && - (Object.hasOwn(option, "maximum") || Object.hasOwn(option, "max")) - ) { - maxStatements = option.maximum || option.max; - } else if (typeof option === "number") { - maxStatements = option; - } - - /** - * Reports a node if it has too many statements - * @param {ASTNode} node node to evaluate - * @param {number} count Number of statements in node - * @param {number} max Maximum number of statements allowed - * @returns {void} - * @private - */ - function reportIfTooManyStatements(node, count, max) { - if (count > max) { - const name = upperCaseFirst( - astUtils.getFunctionNameWithKind(node), - ); - - context.report({ - node, - messageId: "exceed", - data: { name, count, max }, - }); - } - } - - /** - * When parsing a new function, store it in our function stack - * @returns {void} - * @private - */ - function startFunction() { - functionStack.push(0); - } - - /** - * Evaluate the node at the end of function - * @param {ASTNode} node node to evaluate - * @returns {void} - * @private - */ - function endFunction(node) { - const count = functionStack.pop(); - - /* - * This rule does not apply to class static blocks, but we have to track them so - * that statements in them do not count as statements in the enclosing function. - */ - if (node.type === "StaticBlock") { - return; - } - - if (ignoreTopLevelFunctions && functionStack.length === 0) { - topLevelFunctions.push({ node, count }); - } else { - reportIfTooManyStatements(node, count, maxStatements); - } - } - - /** - * Increment the count of the functions - * @param {ASTNode} node node to evaluate - * @returns {void} - * @private - */ - function countStatements(node) { - functionStack[functionStack.length - 1] += node.body.length; - } - - //-------------------------------------------------------------------------- - // Public API - //-------------------------------------------------------------------------- - - return { - FunctionDeclaration: startFunction, - FunctionExpression: startFunction, - ArrowFunctionExpression: startFunction, - StaticBlock: startFunction, - - BlockStatement: countStatements, - - "FunctionDeclaration:exit": endFunction, - "FunctionExpression:exit": endFunction, - "ArrowFunctionExpression:exit": endFunction, - "StaticBlock:exit": endFunction, - - "Program:exit"() { - if (topLevelFunctions.length === 1) { - return; - } - - topLevelFunctions.forEach(element => { - const count = element.count; - const node = element.node; - - reportIfTooManyStatements(node, count, maxStatements); - }); - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Enforce a maximum number of statements allowed in function blocks", + recommended: false, + url: "https://eslint.org/docs/latest/rules/max-statements" + }, + + schema: [ + { + oneOf: [ + { + type: "integer", + minimum: 0 + }, + { + type: "object", + properties: { + maximum: { + type: "integer", + minimum: 0 + }, + max: { + type: "integer", + minimum: 0 + } + }, + additionalProperties: false + } + ] + }, + { + type: "object", + properties: { + ignoreTopLevelFunctions: { + type: "boolean" + } + }, + additionalProperties: false + } + ], + messages: { + exceed: "{{name}} has too many statements ({{count}}). Maximum allowed is {{max}}." + } + }, + + create(context) { + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + const functionStack = [], + option = context.options[0], + ignoreTopLevelFunctions = context.options[1] && context.options[1].ignoreTopLevelFunctions || false, + topLevelFunctions = []; + let maxStatements = 10; + + if ( + typeof option === "object" && + (Object.prototype.hasOwnProperty.call(option, "maximum") || Object.prototype.hasOwnProperty.call(option, "max")) + ) { + maxStatements = option.maximum || option.max; + } else if (typeof option === "number") { + maxStatements = option; + } + + /** + * Reports a node if it has too many statements + * @param {ASTNode} node node to evaluate + * @param {int} count Number of statements in node + * @param {int} max Maximum number of statements allowed + * @returns {void} + * @private + */ + function reportIfTooManyStatements(node, count, max) { + if (count > max) { + const name = upperCaseFirst(astUtils.getFunctionNameWithKind(node)); + + context.report({ + node, + messageId: "exceed", + data: { name, count, max } + }); + } + } + + /** + * When parsing a new function, store it in our function stack + * @returns {void} + * @private + */ + function startFunction() { + functionStack.push(0); + } + + /** + * Evaluate the node at the end of function + * @param {ASTNode} node node to evaluate + * @returns {void} + * @private + */ + function endFunction(node) { + const count = functionStack.pop(); + + /* + * This rule does not apply to class static blocks, but we have to track them so + * that statements in them do not count as statements in the enclosing function. + */ + if (node.type === "StaticBlock") { + return; + } + + if (ignoreTopLevelFunctions && functionStack.length === 0) { + topLevelFunctions.push({ node, count }); + } else { + reportIfTooManyStatements(node, count, maxStatements); + } + } + + /** + * Increment the count of the functions + * @param {ASTNode} node node to evaluate + * @returns {void} + * @private + */ + function countStatements(node) { + functionStack[functionStack.length - 1] += node.body.length; + } + + //-------------------------------------------------------------------------- + // Public API + //-------------------------------------------------------------------------- + + return { + FunctionDeclaration: startFunction, + FunctionExpression: startFunction, + ArrowFunctionExpression: startFunction, + StaticBlock: startFunction, + + BlockStatement: countStatements, + + "FunctionDeclaration:exit": endFunction, + "FunctionExpression:exit": endFunction, + "ArrowFunctionExpression:exit": endFunction, + "StaticBlock:exit": endFunction, + + "Program:exit"() { + if (topLevelFunctions.length === 1) { + return; + } + + topLevelFunctions.forEach(element => { + const count = element.count; + const node = element.node; + + reportIfTooManyStatements(node, count, maxStatements); + }); + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/multiline-comment-style.js b/node_modules/eslint/lib/rules/multiline-comment-style.js index 463e7a734..6da986201 100644 --- a/node_modules/eslint/lib/rules/multiline-comment-style.js +++ b/node_modules/eslint/lib/rules/multiline-comment-style.js @@ -1,7 +1,6 @@ /** * @fileoverview enforce a particular style for multiline comments * @author Teddy Katz - * @deprecated in ESLint v9.3.0 */ "use strict"; @@ -11,642 +10,465 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "9.3.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "multiline-comment-style", - url: "https://eslint.style/rules/multiline-comment-style", - }, - }, - ], - }, - type: "suggestion", - docs: { - description: "Enforce a particular style for multiline comments", - recommended: false, - url: "https://eslint.org/docs/latest/rules/multiline-comment-style", - }, - - fixable: "whitespace", - schema: { - anyOf: [ - { - type: "array", - items: [ - { - enum: ["starred-block", "bare-block"], - }, - ], - additionalItems: false, - }, - { - type: "array", - items: [ - { - enum: ["separate-lines"], - }, - { - type: "object", - properties: { - checkJSDoc: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - additionalItems: false, - }, - ], - }, - messages: { - expectedBlock: - "Expected a block comment instead of consecutive line comments.", - expectedBareBlock: - "Expected a block comment without padding stars.", - startNewline: "Expected a linebreak after '/*'.", - endNewline: "Expected a linebreak before '*/'.", - missingStar: "Expected a '*' at the start of this line.", - alignment: - "Expected this line to be aligned with the start of the comment.", - expectedLines: - "Expected multiple line comments instead of a block comment.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - const option = context.options[0] || "starred-block"; - const params = context.options[1] || {}; - const checkJSDoc = !!params.checkJSDoc; - - //---------------------------------------------------------------------- - // Helpers - //---------------------------------------------------------------------- - - /** - * Checks if a comment line is starred. - * @param {string} line A string representing a comment line. - * @returns {boolean} Whether or not the comment line is starred. - */ - function isStarredCommentLine(line) { - return /^\s*\*/u.test(line); - } - - /** - * Checks if a comment group is in starred-block form. - * @param {Token[]} commentGroup A group of comments, containing either multiple line comments or a single block comment. - * @returns {boolean} Whether or not the comment group is in starred block form. - */ - function isStarredBlockComment([firstComment]) { - if (firstComment.type !== "Block") { - return false; - } - - const lines = firstComment.value.split(astUtils.LINEBREAK_MATCHER); - - // The first and last lines can only contain whitespace. - return ( - lines.length > 0 && - lines.every((line, i) => - (i === 0 || i === lines.length - 1 - ? /^\s*$/u - : /^\s*\*/u - ).test(line), - ) - ); - } - - /** - * Checks if a comment group is in JSDoc form. - * @param {Token[]} commentGroup A group of comments, containing either multiple line comments or a single block comment. - * @returns {boolean} Whether or not the comment group is in JSDoc form. - */ - function isJSDocComment([firstComment]) { - if (firstComment.type !== "Block") { - return false; - } - - const lines = firstComment.value.split(astUtils.LINEBREAK_MATCHER); - - return ( - /^\*\s*$/u.test(lines[0]) && - lines.slice(1, -1).every(line => /^\s* /u.test(line)) && - /^\s*$/u.test(lines.at(-1)) - ); - } - - /** - * Processes a comment group that is currently in separate-line form, calculating the offset for each line. - * @param {Token[]} commentGroup A group of comments containing multiple line comments. - * @returns {string[]} An array of the processed lines. - */ - function processSeparateLineComments(commentGroup) { - const allLinesHaveLeadingSpace = commentGroup - .map(({ value }) => value) - .filter(line => line.trim().length) - .every(line => line.startsWith(" ")); - - return commentGroup.map(({ value }) => - allLinesHaveLeadingSpace ? value.replace(/^ /u, "") : value, - ); - } - - /** - * Processes a comment group that is currently in starred-block form, calculating the offset for each line. - * @param {Token} comment A single block comment token in starred-block form. - * @returns {string[]} An array of the processed lines. - */ - function processStarredBlockComment(comment) { - const lines = comment.value - .split(astUtils.LINEBREAK_MATCHER) - .filter( - (line, i, linesArr) => - !(i === 0 || i === linesArr.length - 1), - ) - .map(line => line.replace(/^\s*$/u, "")); - const allLinesHaveLeadingSpace = lines - .map(line => line.replace(/\s*\*/u, "")) - .filter(line => line.trim().length) - .every(line => line.startsWith(" ")); - - return lines.map(line => - line.replace( - allLinesHaveLeadingSpace ? /\s*\* ?/u : /\s*\*/u, - "", - ), - ); - } - - /** - * Processes a comment group that is currently in bare-block form, calculating the offset for each line. - * @param {Token} comment A single block comment token in bare-block form. - * @returns {string[]} An array of the processed lines. - */ - function processBareBlockComment(comment) { - const lines = comment.value - .split(astUtils.LINEBREAK_MATCHER) - .map(line => line.replace(/^\s*$/u, "")); - const leadingWhitespace = `${sourceCode.text.slice(comment.range[0] - comment.loc.start.column, comment.range[0])} `; - let offset = ""; - - /* - * Calculate the offset of the least indented line and use that as the basis for offsetting all the lines. - * The first line should not be checked because it is inline with the opening block comment delimiter. - */ - for (const [i, line] of lines.entries()) { - if (!line.trim().length || i === 0) { - continue; - } - - const [, lineOffset] = line.match(/^(\s*\*?\s*)/u); - - if (lineOffset.length < leadingWhitespace.length) { - const newOffset = leadingWhitespace.slice( - lineOffset.length - leadingWhitespace.length, - ); - - if (newOffset.length > offset.length) { - offset = newOffset; - } - } - } - - return lines.map(line => { - const match = line.match(/^(\s*\*?\s*)(.*)/u); - const [, lineOffset, lineContents] = match; - - if (lineOffset.length > leadingWhitespace.length) { - return `${lineOffset.slice(leadingWhitespace.length - (offset.length + lineOffset.length))}${lineContents}`; - } - - if (lineOffset.length < leadingWhitespace.length) { - return `${lineOffset.slice(leadingWhitespace.length)}${lineContents}`; - } - - return lineContents; - }); - } - - /** - * Gets a list of comment lines in a group, formatting leading whitespace as necessary. - * @param {Token[]} commentGroup A group of comments containing either multiple line comments or a single block comment. - * @returns {string[]} A list of comment lines. - */ - function getCommentLines(commentGroup) { - const [firstComment] = commentGroup; - - if (firstComment.type === "Line") { - return processSeparateLineComments(commentGroup); - } - - if (isStarredBlockComment(commentGroup)) { - return processStarredBlockComment(firstComment); - } - - return processBareBlockComment(firstComment); - } - - /** - * Gets the initial offset (whitespace) from the beginning of a line to a given comment token. - * @param {Token} comment The token to check. - * @returns {string} The offset from the beginning of a line to the token. - */ - function getInitialOffset(comment) { - return sourceCode.text.slice( - comment.range[0] - comment.loc.start.column, - comment.range[0], - ); - } - - /** - * Converts a comment into starred-block form - * @param {Token} firstComment The first comment of the group being converted - * @param {string[]} commentLinesList A list of lines to appear in the new starred-block comment - * @returns {string} A representation of the comment value in starred-block form, excluding start and end markers - */ - function convertToStarredBlock(firstComment, commentLinesList) { - const initialOffset = getInitialOffset(firstComment); - - return `/*\n${commentLinesList.map(line => `${initialOffset} * ${line}`).join("\n")}\n${initialOffset} */`; - } - - /** - * Converts a comment into separate-line form - * @param {Token} firstComment The first comment of the group being converted - * @param {string[]} commentLinesList A list of lines to appear in the new starred-block comment - * @returns {string} A representation of the comment value in separate-line form - */ - function convertToSeparateLines(firstComment, commentLinesList) { - return commentLinesList - .map(line => `// ${line}`) - .join(`\n${getInitialOffset(firstComment)}`); - } - - /** - * Converts a comment into bare-block form - * @param {Token} firstComment The first comment of the group being converted - * @param {string[]} commentLinesList A list of lines to appear in the new starred-block comment - * @returns {string} A representation of the comment value in bare-block form - */ - function convertToBlock(firstComment, commentLinesList) { - return `/* ${commentLinesList.join(`\n${getInitialOffset(firstComment)} `)} */`; - } - - /** - * Each method checks a group of comments to see if it's valid according to the given option. - * @param {Token[]} commentGroup A list of comments that appear together. This will either contain a single - * block comment or multiple line comments. - * @returns {void} - */ - const commentGroupCheckers = { - "starred-block"(commentGroup) { - const [firstComment] = commentGroup; - const commentLines = getCommentLines(commentGroup); - - if (commentLines.some(value => value.includes("*/"))) { - return; - } - - if (commentGroup.length > 1) { - context.report({ - loc: { - start: firstComment.loc.start, - end: commentGroup.at(-1).loc.end, - }, - messageId: "expectedBlock", - fix(fixer) { - const range = [ - firstComment.range[0], - commentGroup.at(-1).range[1], - ]; - - return commentLines.some(value => - value.startsWith("/"), - ) - ? null - : fixer.replaceTextRange( - range, - convertToStarredBlock( - firstComment, - commentLines, - ), - ); - }, - }); - } else { - const lines = firstComment.value.split( - astUtils.LINEBREAK_MATCHER, - ); - const expectedLeadingWhitespace = - getInitialOffset(firstComment); - const expectedLinePrefix = `${expectedLeadingWhitespace} *`; - - if (!/^\*?\s*$/u.test(lines[0])) { - const start = firstComment.value.startsWith("*") - ? firstComment.range[0] + 1 - : firstComment.range[0]; - - context.report({ - loc: { - start: firstComment.loc.start, - end: { - line: firstComment.loc.start.line, - column: firstComment.loc.start.column + 2, - }, - }, - messageId: "startNewline", - fix: fixer => - fixer.insertTextAfterRange( - [start, start + 2], - `\n${expectedLinePrefix}`, - ), - }); - } - - if (!/^\s*$/u.test(lines.at(-1))) { - context.report({ - loc: { - start: { - line: firstComment.loc.end.line, - column: firstComment.loc.end.column - 2, - }, - end: firstComment.loc.end, - }, - messageId: "endNewline", - fix: fixer => - fixer.replaceTextRange( - [ - firstComment.range[1] - 2, - firstComment.range[1], - ], - `\n${expectedLinePrefix}/`, - ), - }); - } - - for ( - let lineNumber = firstComment.loc.start.line + 1; - lineNumber <= firstComment.loc.end.line; - lineNumber++ - ) { - const lineText = sourceCode.lines[lineNumber - 1]; - const errorType = isStarredCommentLine(lineText) - ? "alignment" - : "missingStar"; - - if (!lineText.startsWith(expectedLinePrefix)) { - context.report({ - loc: { - start: { line: lineNumber, column: 0 }, - end: { - line: lineNumber, - column: lineText.length, - }, - }, - messageId: errorType, - fix(fixer) { - const lineStartIndex = - sourceCode.getIndexFromLoc({ - line: lineNumber, - column: 0, - }); - - if (errorType === "alignment") { - const [, commentTextPrefix = ""] = - lineText.match(/^(\s*\*)/u) || []; - const commentTextStartIndex = - lineStartIndex + - commentTextPrefix.length; - - return fixer.replaceTextRange( - [ - lineStartIndex, - commentTextStartIndex, - ], - expectedLinePrefix, - ); - } - - const [, commentTextPrefix = ""] = - lineText.match(/^(\s*)/u) || []; - const commentTextStartIndex = - lineStartIndex + - commentTextPrefix.length; - let offset; - - for (const [idx, line] of lines.entries()) { - if (!/\S+/u.test(line)) { - continue; - } - - const lineTextToAlignWith = - sourceCode.lines[ - firstComment.loc.start.line - - 1 + - idx - ]; - const [ - , - prefix = "", - initialOffset = "", - ] = - lineTextToAlignWith.match( - /^(\s*(?:\/?\*)?(\s*))/u, - ) || []; - - offset = `${commentTextPrefix.slice(prefix.length)}${initialOffset}`; - - if ( - /^\s*\//u.test(lineText) && - offset.length === 0 - ) { - offset += " "; - } - break; - } - - return fixer.replaceTextRange( - [lineStartIndex, commentTextStartIndex], - `${expectedLinePrefix}${offset}`, - ); - }, - }); - } - } - } - }, - "separate-lines"(commentGroup) { - const [firstComment] = commentGroup; - - const isJSDoc = isJSDocComment(commentGroup); - - if (firstComment.type !== "Block" || (!checkJSDoc && isJSDoc)) { - return; - } - - let commentLines = getCommentLines(commentGroup); - - if (isJSDoc) { - commentLines = commentLines.slice( - 1, - commentLines.length - 1, - ); - } - - const tokenAfter = sourceCode.getTokenAfter(firstComment, { - includeComments: true, - }); - - if ( - tokenAfter && - firstComment.loc.end.line === tokenAfter.loc.start.line - ) { - return; - } - - context.report({ - loc: { - start: firstComment.loc.start, - end: { - line: firstComment.loc.start.line, - column: firstComment.loc.start.column + 2, - }, - }, - messageId: "expectedLines", - fix(fixer) { - return fixer.replaceText( - firstComment, - convertToSeparateLines(firstComment, commentLines), - ); - }, - }); - }, - "bare-block"(commentGroup) { - if (isJSDocComment(commentGroup)) { - return; - } - - const [firstComment] = commentGroup; - const commentLines = getCommentLines(commentGroup); - - // Disallows consecutive line comments in favor of using a block comment. - if ( - firstComment.type === "Line" && - commentLines.length > 1 && - !commentLines.some(value => value.includes("*/")) - ) { - context.report({ - loc: { - start: firstComment.loc.start, - end: commentGroup.at(-1).loc.end, - }, - messageId: "expectedBlock", - fix(fixer) { - return fixer.replaceTextRange( - [ - firstComment.range[0], - commentGroup.at(-1).range[1], - ], - convertToBlock(firstComment, commentLines), - ); - }, - }); - } - - // Prohibits block comments from having a * at the beginning of each line. - if (isStarredBlockComment(commentGroup)) { - context.report({ - loc: { - start: firstComment.loc.start, - end: { - line: firstComment.loc.start.line, - column: firstComment.loc.start.column + 2, - }, - }, - messageId: "expectedBareBlock", - fix(fixer) { - return fixer.replaceText( - firstComment, - convertToBlock(firstComment, commentLines), - ); - }, - }); - } - }, - }; - - //---------------------------------------------------------------------- - // Public - //---------------------------------------------------------------------- - - return { - Program() { - return sourceCode - .getAllComments() - .filter(comment => comment.type !== "Shebang") - .filter( - comment => - !astUtils.COMMENTS_IGNORE_PATTERN.test( - comment.value, - ), - ) - .filter(comment => { - const tokenBefore = sourceCode.getTokenBefore(comment, { - includeComments: true, - }); - - return ( - !tokenBefore || - tokenBefore.loc.end.line < comment.loc.start.line - ); - }) - .reduce((commentGroups, comment, index, commentList) => { - const tokenBefore = sourceCode.getTokenBefore(comment, { - includeComments: true, - }); - - if ( - comment.type === "Line" && - index && - commentList[index - 1].type === "Line" && - tokenBefore && - tokenBefore.loc.end.line === - comment.loc.start.line - 1 && - tokenBefore === commentList[index - 1] - ) { - commentGroups.at(-1).push(comment); - } else { - commentGroups.push([comment]); - } - - return commentGroups; - }, []) - .filter( - commentGroup => - !( - commentGroup.length === 1 && - commentGroup[0].loc.start.line === - commentGroup[0].loc.end.line - ), - ) - .forEach(commentGroupCheckers[option]); - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Enforce a particular style for multiline comments", + recommended: false, + url: "https://eslint.org/docs/latest/rules/multiline-comment-style" + }, + + fixable: "whitespace", + schema: { + anyOf: [ + { + type: "array", + items: [ + { + enum: ["starred-block", "bare-block"] + } + ], + additionalItems: false + }, + { + type: "array", + items: [ + { + enum: ["separate-lines"] + }, + { + type: "object", + properties: { + checkJSDoc: { + type: "boolean" + } + }, + additionalProperties: false + } + ], + additionalItems: false + } + ] + }, + messages: { + expectedBlock: "Expected a block comment instead of consecutive line comments.", + expectedBareBlock: "Expected a block comment without padding stars.", + startNewline: "Expected a linebreak after '/*'.", + endNewline: "Expected a linebreak before '*/'.", + missingStar: "Expected a '*' at the start of this line.", + alignment: "Expected this line to be aligned with the start of the comment.", + expectedLines: "Expected multiple line comments instead of a block comment." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + const option = context.options[0] || "starred-block"; + const params = context.options[1] || {}; + const checkJSDoc = !!params.checkJSDoc; + + //---------------------------------------------------------------------- + // Helpers + //---------------------------------------------------------------------- + + /** + * Checks if a comment line is starred. + * @param {string} line A string representing a comment line. + * @returns {boolean} Whether or not the comment line is starred. + */ + function isStarredCommentLine(line) { + return /^\s*\*/u.test(line); + } + + /** + * Checks if a comment group is in starred-block form. + * @param {Token[]} commentGroup A group of comments, containing either multiple line comments or a single block comment. + * @returns {boolean} Whether or not the comment group is in starred block form. + */ + function isStarredBlockComment([firstComment]) { + if (firstComment.type !== "Block") { + return false; + } + + const lines = firstComment.value.split(astUtils.LINEBREAK_MATCHER); + + // The first and last lines can only contain whitespace. + return lines.length > 0 && lines.every((line, i) => (i === 0 || i === lines.length - 1 ? /^\s*$/u : /^\s*\*/u).test(line)); + } + + /** + * Checks if a comment group is in JSDoc form. + * @param {Token[]} commentGroup A group of comments, containing either multiple line comments or a single block comment. + * @returns {boolean} Whether or not the comment group is in JSDoc form. + */ + function isJSDocComment([firstComment]) { + if (firstComment.type !== "Block") { + return false; + } + + const lines = firstComment.value.split(astUtils.LINEBREAK_MATCHER); + + return /^\*\s*$/u.test(lines[0]) && + lines.slice(1, -1).every(line => /^\s* /u.test(line)) && + /^\s*$/u.test(lines[lines.length - 1]); + } + + /** + * Processes a comment group that is currently in separate-line form, calculating the offset for each line. + * @param {Token[]} commentGroup A group of comments containing multiple line comments. + * @returns {string[]} An array of the processed lines. + */ + function processSeparateLineComments(commentGroup) { + const allLinesHaveLeadingSpace = commentGroup + .map(({ value }) => value) + .filter(line => line.trim().length) + .every(line => line.startsWith(" ")); + + return commentGroup.map(({ value }) => (allLinesHaveLeadingSpace ? value.replace(/^ /u, "") : value)); + } + + /** + * Processes a comment group that is currently in starred-block form, calculating the offset for each line. + * @param {Token} comment A single block comment token in starred-block form. + * @returns {string[]} An array of the processed lines. + */ + function processStarredBlockComment(comment) { + const lines = comment.value.split(astUtils.LINEBREAK_MATCHER) + .filter((line, i, linesArr) => !(i === 0 || i === linesArr.length - 1)) + .map(line => line.replace(/^\s*$/u, "")); + const allLinesHaveLeadingSpace = lines + .map(line => line.replace(/\s*\*/u, "")) + .filter(line => line.trim().length) + .every(line => line.startsWith(" ")); + + return lines.map(line => line.replace(allLinesHaveLeadingSpace ? /\s*\* ?/u : /\s*\*/u, "")); + } + + /** + * Processes a comment group that is currently in bare-block form, calculating the offset for each line. + * @param {Token} comment A single block comment token in bare-block form. + * @returns {string[]} An array of the processed lines. + */ + function processBareBlockComment(comment) { + const lines = comment.value.split(astUtils.LINEBREAK_MATCHER).map(line => line.replace(/^\s*$/u, "")); + const leadingWhitespace = `${sourceCode.text.slice(comment.range[0] - comment.loc.start.column, comment.range[0])} `; + let offset = ""; + + /* + * Calculate the offset of the least indented line and use that as the basis for offsetting all the lines. + * The first line should not be checked because it is inline with the opening block comment delimiter. + */ + for (const [i, line] of lines.entries()) { + if (!line.trim().length || i === 0) { + continue; + } + + const [, lineOffset] = line.match(/^(\s*\*?\s*)/u); + + if (lineOffset.length < leadingWhitespace.length) { + const newOffset = leadingWhitespace.slice(lineOffset.length - leadingWhitespace.length); + + if (newOffset.length > offset.length) { + offset = newOffset; + } + } + } + + return lines.map(line => { + const match = line.match(/^(\s*\*?\s*)(.*)/u); + const [, lineOffset, lineContents] = match; + + if (lineOffset.length > leadingWhitespace.length) { + return `${lineOffset.slice(leadingWhitespace.length - (offset.length + lineOffset.length))}${lineContents}`; + } + + if (lineOffset.length < leadingWhitespace.length) { + return `${lineOffset.slice(leadingWhitespace.length)}${lineContents}`; + } + + return lineContents; + }); + } + + /** + * Gets a list of comment lines in a group, formatting leading whitespace as necessary. + * @param {Token[]} commentGroup A group of comments containing either multiple line comments or a single block comment. + * @returns {string[]} A list of comment lines. + */ + function getCommentLines(commentGroup) { + const [firstComment] = commentGroup; + + if (firstComment.type === "Line") { + return processSeparateLineComments(commentGroup); + } + + if (isStarredBlockComment(commentGroup)) { + return processStarredBlockComment(firstComment); + } + + return processBareBlockComment(firstComment); + } + + /** + * Gets the initial offset (whitespace) from the beginning of a line to a given comment token. + * @param {Token} comment The token to check. + * @returns {string} The offset from the beginning of a line to the token. + */ + function getInitialOffset(comment) { + return sourceCode.text.slice(comment.range[0] - comment.loc.start.column, comment.range[0]); + } + + /** + * Converts a comment into starred-block form + * @param {Token} firstComment The first comment of the group being converted + * @param {string[]} commentLinesList A list of lines to appear in the new starred-block comment + * @returns {string} A representation of the comment value in starred-block form, excluding start and end markers + */ + function convertToStarredBlock(firstComment, commentLinesList) { + const initialOffset = getInitialOffset(firstComment); + + return `/*\n${commentLinesList.map(line => `${initialOffset} * ${line}`).join("\n")}\n${initialOffset} */`; + } + + /** + * Converts a comment into separate-line form + * @param {Token} firstComment The first comment of the group being converted + * @param {string[]} commentLinesList A list of lines to appear in the new starred-block comment + * @returns {string} A representation of the comment value in separate-line form + */ + function convertToSeparateLines(firstComment, commentLinesList) { + return commentLinesList.map(line => `// ${line}`).join(`\n${getInitialOffset(firstComment)}`); + } + + /** + * Converts a comment into bare-block form + * @param {Token} firstComment The first comment of the group being converted + * @param {string[]} commentLinesList A list of lines to appear in the new starred-block comment + * @returns {string} A representation of the comment value in bare-block form + */ + function convertToBlock(firstComment, commentLinesList) { + return `/* ${commentLinesList.join(`\n${getInitialOffset(firstComment)} `)} */`; + } + + /** + * Each method checks a group of comments to see if it's valid according to the given option. + * @param {Token[]} commentGroup A list of comments that appear together. This will either contain a single + * block comment or multiple line comments. + * @returns {void} + */ + const commentGroupCheckers = { + "starred-block"(commentGroup) { + const [firstComment] = commentGroup; + const commentLines = getCommentLines(commentGroup); + + if (commentLines.some(value => value.includes("*/"))) { + return; + } + + if (commentGroup.length > 1) { + context.report({ + loc: { + start: firstComment.loc.start, + end: commentGroup[commentGroup.length - 1].loc.end + }, + messageId: "expectedBlock", + fix(fixer) { + const range = [firstComment.range[0], commentGroup[commentGroup.length - 1].range[1]]; + + return commentLines.some(value => value.startsWith("/")) + ? null + : fixer.replaceTextRange(range, convertToStarredBlock(firstComment, commentLines)); + } + }); + } else { + const lines = firstComment.value.split(astUtils.LINEBREAK_MATCHER); + const expectedLeadingWhitespace = getInitialOffset(firstComment); + const expectedLinePrefix = `${expectedLeadingWhitespace} *`; + + if (!/^\*?\s*$/u.test(lines[0])) { + const start = firstComment.value.startsWith("*") ? firstComment.range[0] + 1 : firstComment.range[0]; + + context.report({ + loc: { + start: firstComment.loc.start, + end: { line: firstComment.loc.start.line, column: firstComment.loc.start.column + 2 } + }, + messageId: "startNewline", + fix: fixer => fixer.insertTextAfterRange([start, start + 2], `\n${expectedLinePrefix}`) + }); + } + + if (!/^\s*$/u.test(lines[lines.length - 1])) { + context.report({ + loc: { + start: { line: firstComment.loc.end.line, column: firstComment.loc.end.column - 2 }, + end: firstComment.loc.end + }, + messageId: "endNewline", + fix: fixer => fixer.replaceTextRange([firstComment.range[1] - 2, firstComment.range[1]], `\n${expectedLinePrefix}/`) + }); + } + + for (let lineNumber = firstComment.loc.start.line + 1; lineNumber <= firstComment.loc.end.line; lineNumber++) { + const lineText = sourceCode.lines[lineNumber - 1]; + const errorType = isStarredCommentLine(lineText) + ? "alignment" + : "missingStar"; + + if (!lineText.startsWith(expectedLinePrefix)) { + context.report({ + loc: { + start: { line: lineNumber, column: 0 }, + end: { line: lineNumber, column: lineText.length } + }, + messageId: errorType, + fix(fixer) { + const lineStartIndex = sourceCode.getIndexFromLoc({ line: lineNumber, column: 0 }); + + if (errorType === "alignment") { + const [, commentTextPrefix = ""] = lineText.match(/^(\s*\*)/u) || []; + const commentTextStartIndex = lineStartIndex + commentTextPrefix.length; + + return fixer.replaceTextRange([lineStartIndex, commentTextStartIndex], expectedLinePrefix); + } + + const [, commentTextPrefix = ""] = lineText.match(/^(\s*)/u) || []; + const commentTextStartIndex = lineStartIndex + commentTextPrefix.length; + let offset; + + for (const [idx, line] of lines.entries()) { + if (!/\S+/u.test(line)) { + continue; + } + + const lineTextToAlignWith = sourceCode.lines[firstComment.loc.start.line - 1 + idx]; + const [, prefix = "", initialOffset = ""] = lineTextToAlignWith.match(/^(\s*(?:\/?\*)?(\s*))/u) || []; + + offset = `${commentTextPrefix.slice(prefix.length)}${initialOffset}`; + + if (/^\s*\//u.test(lineText) && offset.length === 0) { + offset += " "; + } + break; + } + + return fixer.replaceTextRange([lineStartIndex, commentTextStartIndex], `${expectedLinePrefix}${offset}`); + } + }); + } + } + } + }, + "separate-lines"(commentGroup) { + const [firstComment] = commentGroup; + + const isJSDoc = isJSDocComment(commentGroup); + + if (firstComment.type !== "Block" || (!checkJSDoc && isJSDoc)) { + return; + } + + let commentLines = getCommentLines(commentGroup); + + if (isJSDoc) { + commentLines = commentLines.slice(1, commentLines.length - 1); + } + + const tokenAfter = sourceCode.getTokenAfter(firstComment, { includeComments: true }); + + if (tokenAfter && firstComment.loc.end.line === tokenAfter.loc.start.line) { + return; + } + + context.report({ + loc: { + start: firstComment.loc.start, + end: { line: firstComment.loc.start.line, column: firstComment.loc.start.column + 2 } + }, + messageId: "expectedLines", + fix(fixer) { + return fixer.replaceText(firstComment, convertToSeparateLines(firstComment, commentLines)); + } + }); + }, + "bare-block"(commentGroup) { + if (isJSDocComment(commentGroup)) { + return; + } + + const [firstComment] = commentGroup; + const commentLines = getCommentLines(commentGroup); + + // Disallows consecutive line comments in favor of using a block comment. + if (firstComment.type === "Line" && commentLines.length > 1 && + !commentLines.some(value => value.includes("*/"))) { + context.report({ + loc: { + start: firstComment.loc.start, + end: commentGroup[commentGroup.length - 1].loc.end + }, + messageId: "expectedBlock", + fix(fixer) { + return fixer.replaceTextRange( + [firstComment.range[0], commentGroup[commentGroup.length - 1].range[1]], + convertToBlock(firstComment, commentLines) + ); + } + }); + } + + // Prohibits block comments from having a * at the beginning of each line. + if (isStarredBlockComment(commentGroup)) { + context.report({ + loc: { + start: firstComment.loc.start, + end: { line: firstComment.loc.start.line, column: firstComment.loc.start.column + 2 } + }, + messageId: "expectedBareBlock", + fix(fixer) { + return fixer.replaceText(firstComment, convertToBlock(firstComment, commentLines)); + } + }); + } + } + }; + + //---------------------------------------------------------------------- + // Public + //---------------------------------------------------------------------- + + return { + Program() { + return sourceCode.getAllComments() + .filter(comment => comment.type !== "Shebang") + .filter(comment => !astUtils.COMMENTS_IGNORE_PATTERN.test(comment.value)) + .filter(comment => { + const tokenBefore = sourceCode.getTokenBefore(comment, { includeComments: true }); + + return !tokenBefore || tokenBefore.loc.end.line < comment.loc.start.line; + }) + .reduce((commentGroups, comment, index, commentList) => { + const tokenBefore = sourceCode.getTokenBefore(comment, { includeComments: true }); + + if ( + comment.type === "Line" && + index && commentList[index - 1].type === "Line" && + tokenBefore && tokenBefore.loc.end.line === comment.loc.start.line - 1 && + tokenBefore === commentList[index - 1] + ) { + commentGroups[commentGroups.length - 1].push(comment); + } else { + commentGroups.push([comment]); + } + + return commentGroups; + }, []) + .filter(commentGroup => !(commentGroup.length === 1 && commentGroup[0].loc.start.line === commentGroup[0].loc.end.line)) + .forEach(commentGroupCheckers[option]); + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/multiline-ternary.js b/node_modules/eslint/lib/rules/multiline-ternary.js index 5590fe452..8155dd7a5 100644 --- a/node_modules/eslint/lib/rules/multiline-ternary.js +++ b/node_modules/eslint/lib/rules/multiline-ternary.js @@ -12,246 +12,163 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "multiline-ternary", - url: "https://eslint.style/rules/multiline-ternary", - }, - }, - ], - }, - type: "layout", - - docs: { - description: - "Enforce newlines between operands of ternary expressions", - recommended: false, - url: "https://eslint.org/docs/latest/rules/multiline-ternary", - }, - - schema: [ - { - enum: ["always", "always-multiline", "never"], - }, - ], - - messages: { - expectedTestCons: - "Expected newline between test and consequent of ternary expression.", - expectedConsAlt: - "Expected newline between consequent and alternate of ternary expression.", - unexpectedTestCons: - "Unexpected newline between test and consequent of ternary expression.", - unexpectedConsAlt: - "Unexpected newline between consequent and alternate of ternary expression.", - }, - - fixable: "whitespace", - }, - - create(context) { - const sourceCode = context.sourceCode; - const option = context.options[0]; - const multiline = option !== "never"; - const allowSingleLine = option === "always-multiline"; - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - return { - ConditionalExpression(node) { - const questionToken = sourceCode.getTokenAfter( - node.test, - astUtils.isNotClosingParenToken, - ); - const colonToken = sourceCode.getTokenAfter( - node.consequent, - astUtils.isNotClosingParenToken, - ); - - const firstTokenOfTest = sourceCode.getFirstToken(node); - const lastTokenOfTest = - sourceCode.getTokenBefore(questionToken); - const firstTokenOfConsequent = - sourceCode.getTokenAfter(questionToken); - const lastTokenOfConsequent = - sourceCode.getTokenBefore(colonToken); - const firstTokenOfAlternate = - sourceCode.getTokenAfter(colonToken); - - const areTestAndConsequentOnSameLine = - astUtils.isTokenOnSameLine( - lastTokenOfTest, - firstTokenOfConsequent, - ); - const areConsequentAndAlternateOnSameLine = - astUtils.isTokenOnSameLine( - lastTokenOfConsequent, - firstTokenOfAlternate, - ); - - const hasComments = !!sourceCode.getCommentsInside(node).length; - - if (!multiline) { - if (!areTestAndConsequentOnSameLine) { - context.report({ - node: node.test, - loc: { - start: firstTokenOfTest.loc.start, - end: lastTokenOfTest.loc.end, - }, - messageId: "unexpectedTestCons", - fix(fixer) { - if (hasComments) { - return null; - } - const fixers = []; - const areTestAndQuestionOnSameLine = - astUtils.isTokenOnSameLine( - lastTokenOfTest, - questionToken, - ); - const areQuestionAndConsOnSameLine = - astUtils.isTokenOnSameLine( - questionToken, - firstTokenOfConsequent, - ); - - if (!areTestAndQuestionOnSameLine) { - fixers.push( - fixer.removeRange([ - lastTokenOfTest.range[1], - questionToken.range[0], - ]), - ); - } - if (!areQuestionAndConsOnSameLine) { - fixers.push( - fixer.removeRange([ - questionToken.range[1], - firstTokenOfConsequent.range[0], - ]), - ); - } - - return fixers; - }, - }); - } - - if (!areConsequentAndAlternateOnSameLine) { - context.report({ - node: node.consequent, - loc: { - start: firstTokenOfConsequent.loc.start, - end: lastTokenOfConsequent.loc.end, - }, - messageId: "unexpectedConsAlt", - fix(fixer) { - if (hasComments) { - return null; - } - const fixers = []; - const areConsAndColonOnSameLine = - astUtils.isTokenOnSameLine( - lastTokenOfConsequent, - colonToken, - ); - const areColonAndAltOnSameLine = - astUtils.isTokenOnSameLine( - colonToken, - firstTokenOfAlternate, - ); - - if (!areConsAndColonOnSameLine) { - fixers.push( - fixer.removeRange([ - lastTokenOfConsequent.range[1], - colonToken.range[0], - ]), - ); - } - if (!areColonAndAltOnSameLine) { - fixers.push( - fixer.removeRange([ - colonToken.range[1], - firstTokenOfAlternate.range[0], - ]), - ); - } - - return fixers; - }, - }); - } - } else { - if ( - allowSingleLine && - node.loc.start.line === node.loc.end.line - ) { - return; - } - - if (areTestAndConsequentOnSameLine) { - context.report({ - node: node.test, - loc: { - start: firstTokenOfTest.loc.start, - end: lastTokenOfTest.loc.end, - }, - messageId: "expectedTestCons", - fix: fixer => - hasComments - ? null - : fixer.replaceTextRange( - [ - lastTokenOfTest.range[1], - questionToken.range[0], - ], - "\n", - ), - }); - } - - if (areConsequentAndAlternateOnSameLine) { - context.report({ - node: node.consequent, - loc: { - start: firstTokenOfConsequent.loc.start, - end: lastTokenOfConsequent.loc.end, - }, - messageId: "expectedConsAlt", - fix: fixer => - hasComments - ? null - : fixer.replaceTextRange( - [ - lastTokenOfConsequent.range[1], - colonToken.range[0], - ], - "\n", - ), - }); - } - } - }, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Enforce newlines between operands of ternary expressions", + recommended: false, + url: "https://eslint.org/docs/latest/rules/multiline-ternary" + }, + + schema: [ + { + enum: ["always", "always-multiline", "never"] + } + ], + + messages: { + expectedTestCons: "Expected newline between test and consequent of ternary expression.", + expectedConsAlt: "Expected newline between consequent and alternate of ternary expression.", + unexpectedTestCons: "Unexpected newline between test and consequent of ternary expression.", + unexpectedConsAlt: "Unexpected newline between consequent and alternate of ternary expression." + }, + + fixable: "whitespace" + }, + + create(context) { + const sourceCode = context.sourceCode; + const option = context.options[0]; + const multiline = option !== "never"; + const allowSingleLine = option === "always-multiline"; + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + return { + ConditionalExpression(node) { + const questionToken = sourceCode.getTokenAfter(node.test, astUtils.isNotClosingParenToken); + const colonToken = sourceCode.getTokenAfter(node.consequent, astUtils.isNotClosingParenToken); + + const firstTokenOfTest = sourceCode.getFirstToken(node); + const lastTokenOfTest = sourceCode.getTokenBefore(questionToken); + const firstTokenOfConsequent = sourceCode.getTokenAfter(questionToken); + const lastTokenOfConsequent = sourceCode.getTokenBefore(colonToken); + const firstTokenOfAlternate = sourceCode.getTokenAfter(colonToken); + + const areTestAndConsequentOnSameLine = astUtils.isTokenOnSameLine(lastTokenOfTest, firstTokenOfConsequent); + const areConsequentAndAlternateOnSameLine = astUtils.isTokenOnSameLine(lastTokenOfConsequent, firstTokenOfAlternate); + + const hasComments = !!sourceCode.getCommentsInside(node).length; + + if (!multiline) { + if (!areTestAndConsequentOnSameLine) { + context.report({ + node: node.test, + loc: { + start: firstTokenOfTest.loc.start, + end: lastTokenOfTest.loc.end + }, + messageId: "unexpectedTestCons", + fix(fixer) { + if (hasComments) { + return null; + } + const fixers = []; + const areTestAndQuestionOnSameLine = astUtils.isTokenOnSameLine(lastTokenOfTest, questionToken); + const areQuestionAndConsOnSameLine = astUtils.isTokenOnSameLine(questionToken, firstTokenOfConsequent); + + if (!areTestAndQuestionOnSameLine) { + fixers.push(fixer.removeRange([lastTokenOfTest.range[1], questionToken.range[0]])); + } + if (!areQuestionAndConsOnSameLine) { + fixers.push(fixer.removeRange([questionToken.range[1], firstTokenOfConsequent.range[0]])); + } + + return fixers; + } + }); + } + + if (!areConsequentAndAlternateOnSameLine) { + context.report({ + node: node.consequent, + loc: { + start: firstTokenOfConsequent.loc.start, + end: lastTokenOfConsequent.loc.end + }, + messageId: "unexpectedConsAlt", + fix(fixer) { + if (hasComments) { + return null; + } + const fixers = []; + const areConsAndColonOnSameLine = astUtils.isTokenOnSameLine(lastTokenOfConsequent, colonToken); + const areColonAndAltOnSameLine = astUtils.isTokenOnSameLine(colonToken, firstTokenOfAlternate); + + if (!areConsAndColonOnSameLine) { + fixers.push(fixer.removeRange([lastTokenOfConsequent.range[1], colonToken.range[0]])); + } + if (!areColonAndAltOnSameLine) { + fixers.push(fixer.removeRange([colonToken.range[1], firstTokenOfAlternate.range[0]])); + } + + return fixers; + } + }); + } + } else { + if (allowSingleLine && node.loc.start.line === node.loc.end.line) { + return; + } + + if (areTestAndConsequentOnSameLine) { + context.report({ + node: node.test, + loc: { + start: firstTokenOfTest.loc.start, + end: lastTokenOfTest.loc.end + }, + messageId: "expectedTestCons", + fix: fixer => (hasComments ? null : ( + fixer.replaceTextRange( + [ + lastTokenOfTest.range[1], + questionToken.range[0] + ], + "\n" + ) + )) + }); + } + + if (areConsequentAndAlternateOnSameLine) { + context.report({ + node: node.consequent, + loc: { + start: firstTokenOfConsequent.loc.start, + end: lastTokenOfConsequent.loc.end + }, + messageId: "expectedConsAlt", + fix: (fixer => (hasComments ? null : ( + fixer.replaceTextRange( + [ + lastTokenOfConsequent.range[1], + colonToken.range[0] + ], + "\n" + ) + ))) + }); + } + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/new-cap.js b/node_modules/eslint/lib/rules/new-cap.js index caea1a8be..f81e42fd0 100644 --- a/node_modules/eslint/lib/rules/new-cap.js +++ b/node_modules/eslint/lib/rules/new-cap.js @@ -16,19 +16,36 @@ const astUtils = require("./utils/ast-utils"); //------------------------------------------------------------------------------ const CAPS_ALLOWED = [ - "Array", - "Boolean", - "Date", - "Error", - "Function", - "Number", - "Object", - "RegExp", - "String", - "Symbol", - "BigInt", + "Array", + "Boolean", + "Date", + "Error", + "Function", + "Number", + "Object", + "RegExp", + "String", + "Symbol", + "BigInt" ]; +/** + * Ensure that if the key is provided, it must be an array. + * @param {Object} obj Object to check with `key`. + * @param {string} key Object key to check on `obj`. + * @param {any} fallback If obj[key] is not present, this will be returned. + * @throws {TypeError} If key is not an own array type property of `obj`. + * @returns {string[]} Returns obj[key] if it's an Array, otherwise `fallback` + */ +function checkArray(obj, key, fallback) { + + /* c8 ignore start */ + if (Object.prototype.hasOwnProperty.call(obj, key) && !Array.isArray(obj[key])) { + throw new TypeError(`${key}, if provided, must be an Array`); + }/* c8 ignore stop */ + return obj[key] || fallback; +} + /** * A reducer function to invert an array to an Object mapping the string form of the key, to `true`. * @param {Object} map Accumulator object for the reduce. @@ -36,8 +53,8 @@ const CAPS_ALLOWED = [ * @returns {Object} Returns the updated Object for further reduction. */ function invert(map, key) { - map[key] = true; - return map; + map[key] = true; + return map; } /** @@ -46,232 +63,214 @@ function invert(map, key) { * @returns {Object} Object with cap is new exceptions. */ function calculateCapIsNewExceptions(config) { - const capIsNewExceptions = Array.from( - new Set([...config.capIsNewExceptions, ...CAPS_ALLOWED]), - ); + let capIsNewExceptions = checkArray(config, "capIsNewExceptions", CAPS_ALLOWED); + + if (capIsNewExceptions !== CAPS_ALLOWED) { + capIsNewExceptions = capIsNewExceptions.concat(CAPS_ALLOWED); + } - return capIsNewExceptions.reduce(invert, {}); + return capIsNewExceptions.reduce(invert, {}); } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: - "Require constructor names to begin with a capital letter", - recommended: false, - url: "https://eslint.org/docs/latest/rules/new-cap", - }, - - schema: [ - { - type: "object", - properties: { - newIsCap: { - type: "boolean", - }, - capIsNew: { - type: "boolean", - }, - newIsCapExceptions: { - type: "array", - items: { - type: "string", - }, - }, - newIsCapExceptionPattern: { - type: "string", - }, - capIsNewExceptions: { - type: "array", - items: { - type: "string", - }, - }, - capIsNewExceptionPattern: { - type: "string", - }, - properties: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - - defaultOptions: [ - { - capIsNew: true, - capIsNewExceptions: CAPS_ALLOWED, - newIsCap: true, - newIsCapExceptions: [], - properties: true, - }, - ], - - messages: { - upper: "A function with a name starting with an uppercase letter should only be used as a constructor.", - lower: "A constructor name should not start with a lowercase letter.", - }, - }, - - create(context) { - const [config] = context.options; - const skipProperties = !config.properties; - - const newIsCapExceptions = config.newIsCapExceptions.reduce(invert, {}); - const newIsCapExceptionPattern = config.newIsCapExceptionPattern - ? new RegExp(config.newIsCapExceptionPattern, "u") - : null; - - const capIsNewExceptions = calculateCapIsNewExceptions(config); - const capIsNewExceptionPattern = config.capIsNewExceptionPattern - ? new RegExp(config.capIsNewExceptionPattern, "u") - : null; - - const listeners = {}; - - const sourceCode = context.sourceCode; - - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - /** - * Get exact callee name from expression - * @param {ASTNode} node CallExpression or NewExpression node - * @returns {string} name - */ - function extractNameFromExpression(node) { - return node.callee.type === "Identifier" - ? node.callee.name - : astUtils.getStaticPropertyName(node.callee) || ""; - } - - /** - * Returns the capitalization state of the string - - * Whether the first character is uppercase, lowercase, or non-alphabetic - * @param {string} str String - * @returns {string} capitalization state: "non-alpha", "lower", or "upper" - */ - function getCap(str) { - const firstChar = str.charAt(0); - - const firstCharLower = firstChar.toLowerCase(); - const firstCharUpper = firstChar.toUpperCase(); - - if (firstCharLower === firstCharUpper) { - // char has no uppercase variant, so it's non-alphabetic - return "non-alpha"; - } - if (firstChar === firstCharLower) { - return "lower"; - } - return "upper"; - } - - /** - * Check if capitalization is allowed for a CallExpression - * @param {Object} allowedMap Object mapping calleeName to a Boolean - * @param {ASTNode} node CallExpression node - * @param {string} calleeName Capitalized callee name from a CallExpression - * @param {Object} pattern RegExp object from options pattern - * @returns {boolean} Returns true if the callee may be capitalized - */ - function isCapAllowed(allowedMap, node, calleeName, pattern) { - const sourceText = sourceCode.getText(node.callee); - - if (allowedMap[calleeName] || allowedMap[sourceText]) { - return true; - } - - if (pattern && pattern.test(sourceText)) { - return true; - } - - const callee = astUtils.skipChainExpression(node.callee); - - if (calleeName === "UTC" && callee.type === "MemberExpression") { - // allow if callee is Date.UTC - return ( - callee.object.type === "Identifier" && - callee.object.name === "Date" - ); - } - - return skipProperties && callee.type === "MemberExpression"; - } - - /** - * Reports the given messageId for the given node. The location will be the start of the property or the callee. - * @param {ASTNode} node CallExpression or NewExpression node. - * @param {string} messageId The messageId to report. - * @returns {void} - */ - function report(node, messageId) { - let callee = astUtils.skipChainExpression(node.callee); - - if (callee.type === "MemberExpression") { - callee = callee.property; - } - - context.report({ node, loc: callee.loc, messageId }); - } - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - if (config.newIsCap) { - listeners.NewExpression = function (node) { - const constructorName = extractNameFromExpression(node); - - if (constructorName) { - const capitalization = getCap(constructorName); - const isAllowed = - capitalization !== "lower" || - isCapAllowed( - newIsCapExceptions, - node, - constructorName, - newIsCapExceptionPattern, - ); - - if (!isAllowed) { - report(node, "lower"); - } - } - }; - } - - if (config.capIsNew) { - listeners.CallExpression = function (node) { - const calleeName = extractNameFromExpression(node); - - if (calleeName) { - const capitalization = getCap(calleeName); - const isAllowed = - capitalization !== "upper" || - isCapAllowed( - capIsNewExceptions, - node, - calleeName, - capIsNewExceptionPattern, - ); - - if (!isAllowed) { - report(node, "upper"); - } - } - }; - } - - return listeners; - }, + meta: { + type: "suggestion", + + docs: { + description: "Require constructor names to begin with a capital letter", + recommended: false, + url: "https://eslint.org/docs/latest/rules/new-cap" + }, + + schema: [ + { + type: "object", + properties: { + newIsCap: { + type: "boolean", + default: true + }, + capIsNew: { + type: "boolean", + default: true + }, + newIsCapExceptions: { + type: "array", + items: { + type: "string" + } + }, + newIsCapExceptionPattern: { + type: "string" + }, + capIsNewExceptions: { + type: "array", + items: { + type: "string" + } + }, + capIsNewExceptionPattern: { + type: "string" + }, + properties: { + type: "boolean", + default: true + } + }, + additionalProperties: false + } + ], + messages: { + upper: "A function with a name starting with an uppercase letter should only be used as a constructor.", + lower: "A constructor name should not start with a lowercase letter." + } + }, + + create(context) { + + const config = Object.assign({}, context.options[0]); + + config.newIsCap = config.newIsCap !== false; + config.capIsNew = config.capIsNew !== false; + const skipProperties = config.properties === false; + + const newIsCapExceptions = checkArray(config, "newIsCapExceptions", []).reduce(invert, {}); + const newIsCapExceptionPattern = config.newIsCapExceptionPattern ? new RegExp(config.newIsCapExceptionPattern, "u") : null; + + const capIsNewExceptions = calculateCapIsNewExceptions(config); + const capIsNewExceptionPattern = config.capIsNewExceptionPattern ? new RegExp(config.capIsNewExceptionPattern, "u") : null; + + const listeners = {}; + + const sourceCode = context.sourceCode; + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + /** + * Get exact callee name from expression + * @param {ASTNode} node CallExpression or NewExpression node + * @returns {string} name + */ + function extractNameFromExpression(node) { + return node.callee.type === "Identifier" + ? node.callee.name + : astUtils.getStaticPropertyName(node.callee) || ""; + } + + /** + * Returns the capitalization state of the string - + * Whether the first character is uppercase, lowercase, or non-alphabetic + * @param {string} str String + * @returns {string} capitalization state: "non-alpha", "lower", or "upper" + */ + function getCap(str) { + const firstChar = str.charAt(0); + + const firstCharLower = firstChar.toLowerCase(); + const firstCharUpper = firstChar.toUpperCase(); + + if (firstCharLower === firstCharUpper) { + + // char has no uppercase variant, so it's non-alphabetic + return "non-alpha"; + } + if (firstChar === firstCharLower) { + return "lower"; + } + return "upper"; + + } + + /** + * Check if capitalization is allowed for a CallExpression + * @param {Object} allowedMap Object mapping calleeName to a Boolean + * @param {ASTNode} node CallExpression node + * @param {string} calleeName Capitalized callee name from a CallExpression + * @param {Object} pattern RegExp object from options pattern + * @returns {boolean} Returns true if the callee may be capitalized + */ + function isCapAllowed(allowedMap, node, calleeName, pattern) { + const sourceText = sourceCode.getText(node.callee); + + if (allowedMap[calleeName] || allowedMap[sourceText]) { + return true; + } + + if (pattern && pattern.test(sourceText)) { + return true; + } + + const callee = astUtils.skipChainExpression(node.callee); + + if (calleeName === "UTC" && callee.type === "MemberExpression") { + + // allow if callee is Date.UTC + return callee.object.type === "Identifier" && + callee.object.name === "Date"; + } + + return skipProperties && callee.type === "MemberExpression"; + } + + /** + * Reports the given messageId for the given node. The location will be the start of the property or the callee. + * @param {ASTNode} node CallExpression or NewExpression node. + * @param {string} messageId The messageId to report. + * @returns {void} + */ + function report(node, messageId) { + let callee = astUtils.skipChainExpression(node.callee); + + if (callee.type === "MemberExpression") { + callee = callee.property; + } + + context.report({ node, loc: callee.loc, messageId }); + } + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + if (config.newIsCap) { + listeners.NewExpression = function(node) { + + const constructorName = extractNameFromExpression(node); + + if (constructorName) { + const capitalization = getCap(constructorName); + const isAllowed = capitalization !== "lower" || isCapAllowed(newIsCapExceptions, node, constructorName, newIsCapExceptionPattern); + + if (!isAllowed) { + report(node, "lower"); + } + } + }; + } + + if (config.capIsNew) { + listeners.CallExpression = function(node) { + + const calleeName = extractNameFromExpression(node); + + if (calleeName) { + const capitalization = getCap(calleeName); + const isAllowed = capitalization !== "upper" || isCapAllowed(capIsNewExceptions, node, calleeName, capIsNewExceptionPattern); + + if (!isAllowed) { + report(node, "upper"); + } + } + }; + } + + return listeners; + } }; diff --git a/node_modules/eslint/lib/rules/new-parens.js b/node_modules/eslint/lib/rules/new-parens.js index e8cb8db2a..1c5d21d4a 100644 --- a/node_modules/eslint/lib/rules/new-parens.js +++ b/node_modules/eslint/lib/rules/new-parens.js @@ -20,101 +20,74 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "new-parens", - url: "https://eslint.style/rules/new-parens", - }, - }, - ], - }, - type: "layout", + meta: { + deprecated: true, + replacedBy: [], + type: "layout", - docs: { - description: - "Enforce or disallow parentheses when invoking a constructor with no arguments", - recommended: false, - url: "https://eslint.org/docs/latest/rules/new-parens", - }, + docs: { + description: "Enforce or disallow parentheses when invoking a constructor with no arguments", + recommended: false, + url: "https://eslint.org/docs/latest/rules/new-parens" + }, - fixable: "code", - schema: [ - { - enum: ["always", "never"], - }, - ], - messages: { - missing: "Missing '()' invoking a constructor.", - unnecessary: - "Unnecessary '()' invoking a constructor with no arguments.", - }, - }, + fixable: "code", + schema: [ + { + enum: ["always", "never"] + } + ], + messages: { + missing: "Missing '()' invoking a constructor.", + unnecessary: "Unnecessary '()' invoking a constructor with no arguments." + } + }, - create(context) { - const options = context.options; - const always = options[0] !== "never"; // Default is always + create(context) { + const options = context.options; + const always = options[0] !== "never"; // Default is always - const sourceCode = context.sourceCode; + const sourceCode = context.sourceCode; - return { - NewExpression(node) { - if (node.arguments.length !== 0) { - return; // if there are arguments, there have to be parens - } + return { + NewExpression(node) { + if (node.arguments.length !== 0) { + return; // if there are arguments, there have to be parens + } - const lastToken = sourceCode.getLastToken(node); - const hasLastParen = - lastToken && astUtils.isClosingParenToken(lastToken); + const lastToken = sourceCode.getLastToken(node); + const hasLastParen = lastToken && astUtils.isClosingParenToken(lastToken); - // `hasParens` is true only if the new expression ends with its own parens, e.g., new new foo() does not end with its own parens - const hasParens = - hasLastParen && - astUtils.isOpeningParenToken( - sourceCode.getTokenBefore(lastToken), - ) && - node.callee.range[1] < node.range[1]; + // `hasParens` is true only if the new expression ends with its own parens, e.g., new new foo() does not end with its own parens + const hasParens = hasLastParen && + astUtils.isOpeningParenToken(sourceCode.getTokenBefore(lastToken)) && + node.callee.range[1] < node.range[1]; - if (always) { - if (!hasParens) { - context.report({ - node, - messageId: "missing", - fix: fixer => fixer.insertTextAfter(node, "()"), - }); - } - } else { - if (hasParens) { - context.report({ - node, - messageId: "unnecessary", - fix: fixer => [ - fixer.remove( - sourceCode.getTokenBefore(lastToken), - ), - fixer.remove(lastToken), - fixer.insertTextBefore(node, "("), - fixer.insertTextAfter(node, ")"), - ], - }); - } - } - }, - }; - }, + if (always) { + if (!hasParens) { + context.report({ + node, + messageId: "missing", + fix: fixer => fixer.insertTextAfter(node, "()") + }); + } + } else { + if (hasParens) { + context.report({ + node, + messageId: "unnecessary", + fix: fixer => [ + fixer.remove(sourceCode.getTokenBefore(lastToken)), + fixer.remove(lastToken), + fixer.insertTextBefore(node, "("), + fixer.insertTextAfter(node, ")") + ] + }); + } + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/newline-after-var.js b/node_modules/eslint/lib/rules/newline-after-var.js index 76d29117c..dc8b24d47 100644 --- a/node_modules/eslint/lib/rules/newline-after-var.js +++ b/node_modules/eslint/lib/rules/newline-after-var.js @@ -16,292 +16,238 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "layout", - - docs: { - description: - "Require or disallow an empty line after variable declarations", - recommended: false, - url: "https://eslint.org/docs/latest/rules/newline-after-var", - }, - schema: [ - { - enum: ["never", "always"], - }, - ], - fixable: "whitespace", - messages: { - expected: "Expected blank line after variable declarations.", - unexpected: "Unexpected blank line after variable declarations.", - }, - - deprecated: { - message: "The rule was replaced with a more general rule.", - url: "https://eslint.org/blog/2017/06/eslint-v4.0.0-released/", - deprecatedSince: "4.0.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: "The new rule moved to a plugin.", - url: "https://eslint.org/docs/latest/rules/padding-line-between-statements#examples", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "padding-line-between-statements", - url: "https://eslint.style/rules/padding-line-between-statements", - }, - }, - ], - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - // Default `mode` to "always". - const mode = context.options[0] === "never" ? "never" : "always"; - - // Cache starting and ending line numbers of comments for faster lookup - const commentEndLine = sourceCode - .getAllComments() - .reduce((result, token) => { - result[token.loc.start.line] = token.loc.end.line; - return result; - }, {}); - - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - /** - * Gets a token from the given node to compare line to the next statement. - * - * In general, the token is the last token of the node. However, the token is the second last token if the following conditions satisfy. - * - * - The last token is semicolon. - * - The semicolon is on a different line from the previous token of the semicolon. - * - * This behavior would address semicolon-less style code. e.g.: - * - * var foo = 1 - * - * ;(a || b).doSomething() - * @param {ASTNode} node The node to get. - * @returns {Token} The token to compare line to the next statement. - */ - function getLastToken(node) { - const lastToken = sourceCode.getLastToken(node); - - if (lastToken.type === "Punctuator" && lastToken.value === ";") { - const prevToken = sourceCode.getTokenBefore(lastToken); - - if (prevToken.loc.end.line !== lastToken.loc.start.line) { - return prevToken; - } - } - - return lastToken; - } - - /** - * Determine if provided keyword is a variable declaration - * @private - * @param {string} keyword keyword to test - * @returns {boolean} True if `keyword` is a type of var - */ - function isVar(keyword) { - return ( - keyword === "var" || keyword === "let" || keyword === "const" - ); - } - - /** - * Determine if provided keyword is a variant of for specifiers - * @private - * @param {string} keyword keyword to test - * @returns {boolean} True if `keyword` is a variant of for specifier - */ - function isForTypeSpecifier(keyword) { - return ( - keyword === "ForStatement" || - keyword === "ForInStatement" || - keyword === "ForOfStatement" - ); - } - - /** - * Determine if provided keyword is an export specifiers - * @private - * @param {string} nodeType nodeType to test - * @returns {boolean} True if `nodeType` is an export specifier - */ - function isExportSpecifier(nodeType) { - return ( - nodeType === "ExportNamedDeclaration" || - nodeType === "ExportSpecifier" || - nodeType === "ExportDefaultDeclaration" || - nodeType === "ExportAllDeclaration" - ); - } - - /** - * Determine if provided node is the last of their parent block. - * @private - * @param {ASTNode} node node to test - * @returns {boolean} True if `node` is last of their parent block. - */ - function isLastNode(node) { - const token = sourceCode.getTokenAfter(node); - - return ( - !token || (token.type === "Punctuator" && token.value === "}") - ); - } - - /** - * Gets the last line of a group of consecutive comments - * @param {number} commentStartLine The starting line of the group - * @returns {number} The number of the last comment line of the group - */ - function getLastCommentLineOfBlock(commentStartLine) { - const currentCommentEnd = commentEndLine[commentStartLine]; - - return commentEndLine[currentCommentEnd + 1] - ? getLastCommentLineOfBlock(currentCommentEnd + 1) - : currentCommentEnd; - } - - /** - * Determine if a token starts more than one line after a comment ends - * @param {token} token The token being checked - * @param {integer} commentStartLine The line number on which the comment starts - * @returns {boolean} True if `token` does not start immediately after a comment - */ - function hasBlankLineAfterComment(token, commentStartLine) { - return ( - token.loc.start.line > - getLastCommentLineOfBlock(commentStartLine) + 1 - ); - } - - /** - * Checks that a blank line exists after a variable declaration when mode is - * set to "always", or checks that there is no blank line when mode is set - * to "never" - * @private - * @param {ASTNode} node `VariableDeclaration` node to test - * @returns {void} - */ - function checkForBlankLine(node) { - /* - * lastToken is the last token on the node's line. It will usually also be the last token of the node, but it will - * sometimes be second-last if there is a semicolon on a different line. - */ - const lastToken = getLastToken(node), - /* - * If lastToken is the last token of the node, nextToken should be the token after the node. Otherwise, nextToken - * is the last token of the node. - */ - nextToken = - lastToken === sourceCode.getLastToken(node) - ? sourceCode.getTokenAfter(node) - : sourceCode.getLastToken(node), - nextLineNum = lastToken.loc.end.line + 1; - - // Ignore if there is no following statement - if (!nextToken) { - return; - } - - // Ignore if parent of node is a for variant - if (isForTypeSpecifier(node.parent.type)) { - return; - } - - // Ignore if parent of node is an export specifier - if (isExportSpecifier(node.parent.type)) { - return; - } - - /* - * Some coding styles use multiple `var` statements, so do nothing if - * the next token is a `var` statement. - */ - if (nextToken.type === "Keyword" && isVar(nextToken.value)) { - return; - } - - // Ignore if it is last statement in a block - if (isLastNode(node)) { - return; - } - - // Next statement is not a `var`... - const noNextLineToken = nextToken.loc.start.line > nextLineNum; - const hasNextLineComment = - typeof commentEndLine[nextLineNum] !== "undefined"; - - if (mode === "never" && noNextLineToken && !hasNextLineComment) { - context.report({ - node, - messageId: "unexpected", - fix(fixer) { - const linesBetween = sourceCode - .getText() - .slice(lastToken.range[1], nextToken.range[0]) - .split(astUtils.LINEBREAK_MATCHER); - - return fixer.replaceTextRange( - [lastToken.range[1], nextToken.range[0]], - `${linesBetween.slice(0, -1).join("")}\n${linesBetween.at(-1)}`, - ); - }, - }); - } - - // Token on the next line, or comment without blank line - if ( - mode === "always" && - (!noNextLineToken || - (hasNextLineComment && - !hasBlankLineAfterComment(nextToken, nextLineNum))) - ) { - context.report({ - node, - messageId: "expected", - fix(fixer) { - if ( - (noNextLineToken - ? getLastCommentLineOfBlock(nextLineNum) - : lastToken.loc.end.line) === - nextToken.loc.start.line - ) { - return fixer.insertTextBefore(nextToken, "\n\n"); - } - - return fixer.insertTextBeforeRange( - [ - nextToken.range[0] - nextToken.loc.start.column, - nextToken.range[1], - ], - "\n", - ); - }, - }); - } - } - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - return { - VariableDeclaration: checkForBlankLine, - }; - }, + meta: { + type: "layout", + + docs: { + description: "Require or disallow an empty line after variable declarations", + recommended: false, + url: "https://eslint.org/docs/latest/rules/newline-after-var" + }, + schema: [ + { + enum: ["never", "always"] + } + ], + fixable: "whitespace", + messages: { + expected: "Expected blank line after variable declarations.", + unexpected: "Unexpected blank line after variable declarations." + }, + + deprecated: true, + + replacedBy: ["padding-line-between-statements"] + }, + + create(context) { + const sourceCode = context.sourceCode; + + // Default `mode` to "always". + const mode = context.options[0] === "never" ? "never" : "always"; + + // Cache starting and ending line numbers of comments for faster lookup + const commentEndLine = sourceCode.getAllComments().reduce((result, token) => { + result[token.loc.start.line] = token.loc.end.line; + return result; + }, {}); + + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + /** + * Gets a token from the given node to compare line to the next statement. + * + * In general, the token is the last token of the node. However, the token is the second last token if the following conditions satisfy. + * + * - The last token is semicolon. + * - The semicolon is on a different line from the previous token of the semicolon. + * + * This behavior would address semicolon-less style code. e.g.: + * + * var foo = 1 + * + * ;(a || b).doSomething() + * @param {ASTNode} node The node to get. + * @returns {Token} The token to compare line to the next statement. + */ + function getLastToken(node) { + const lastToken = sourceCode.getLastToken(node); + + if (lastToken.type === "Punctuator" && lastToken.value === ";") { + const prevToken = sourceCode.getTokenBefore(lastToken); + + if (prevToken.loc.end.line !== lastToken.loc.start.line) { + return prevToken; + } + } + + return lastToken; + } + + /** + * Determine if provided keyword is a variable declaration + * @private + * @param {string} keyword keyword to test + * @returns {boolean} True if `keyword` is a type of var + */ + function isVar(keyword) { + return keyword === "var" || keyword === "let" || keyword === "const"; + } + + /** + * Determine if provided keyword is a variant of for specifiers + * @private + * @param {string} keyword keyword to test + * @returns {boolean} True if `keyword` is a variant of for specifier + */ + function isForTypeSpecifier(keyword) { + return keyword === "ForStatement" || keyword === "ForInStatement" || keyword === "ForOfStatement"; + } + + /** + * Determine if provided keyword is an export specifiers + * @private + * @param {string} nodeType nodeType to test + * @returns {boolean} True if `nodeType` is an export specifier + */ + function isExportSpecifier(nodeType) { + return nodeType === "ExportNamedDeclaration" || nodeType === "ExportSpecifier" || + nodeType === "ExportDefaultDeclaration" || nodeType === "ExportAllDeclaration"; + } + + /** + * Determine if provided node is the last of their parent block. + * @private + * @param {ASTNode} node node to test + * @returns {boolean} True if `node` is last of their parent block. + */ + function isLastNode(node) { + const token = sourceCode.getTokenAfter(node); + + return !token || (token.type === "Punctuator" && token.value === "}"); + } + + /** + * Gets the last line of a group of consecutive comments + * @param {number} commentStartLine The starting line of the group + * @returns {number} The number of the last comment line of the group + */ + function getLastCommentLineOfBlock(commentStartLine) { + const currentCommentEnd = commentEndLine[commentStartLine]; + + return commentEndLine[currentCommentEnd + 1] ? getLastCommentLineOfBlock(currentCommentEnd + 1) : currentCommentEnd; + } + + /** + * Determine if a token starts more than one line after a comment ends + * @param {token} token The token being checked + * @param {integer} commentStartLine The line number on which the comment starts + * @returns {boolean} True if `token` does not start immediately after a comment + */ + function hasBlankLineAfterComment(token, commentStartLine) { + return token.loc.start.line > getLastCommentLineOfBlock(commentStartLine) + 1; + } + + /** + * Checks that a blank line exists after a variable declaration when mode is + * set to "always", or checks that there is no blank line when mode is set + * to "never" + * @private + * @param {ASTNode} node `VariableDeclaration` node to test + * @returns {void} + */ + function checkForBlankLine(node) { + + /* + * lastToken is the last token on the node's line. It will usually also be the last token of the node, but it will + * sometimes be second-last if there is a semicolon on a different line. + */ + const lastToken = getLastToken(node), + + /* + * If lastToken is the last token of the node, nextToken should be the token after the node. Otherwise, nextToken + * is the last token of the node. + */ + nextToken = lastToken === sourceCode.getLastToken(node) ? sourceCode.getTokenAfter(node) : sourceCode.getLastToken(node), + nextLineNum = lastToken.loc.end.line + 1; + + // Ignore if there is no following statement + if (!nextToken) { + return; + } + + // Ignore if parent of node is a for variant + if (isForTypeSpecifier(node.parent.type)) { + return; + } + + // Ignore if parent of node is an export specifier + if (isExportSpecifier(node.parent.type)) { + return; + } + + /* + * Some coding styles use multiple `var` statements, so do nothing if + * the next token is a `var` statement. + */ + if (nextToken.type === "Keyword" && isVar(nextToken.value)) { + return; + } + + // Ignore if it is last statement in a block + if (isLastNode(node)) { + return; + } + + // Next statement is not a `var`... + const noNextLineToken = nextToken.loc.start.line > nextLineNum; + const hasNextLineComment = (typeof commentEndLine[nextLineNum] !== "undefined"); + + if (mode === "never" && noNextLineToken && !hasNextLineComment) { + context.report({ + node, + messageId: "unexpected", + fix(fixer) { + const linesBetween = sourceCode.getText().slice(lastToken.range[1], nextToken.range[0]).split(astUtils.LINEBREAK_MATCHER); + + return fixer.replaceTextRange([lastToken.range[1], nextToken.range[0]], `${linesBetween.slice(0, -1).join("")}\n${linesBetween[linesBetween.length - 1]}`); + } + }); + } + + // Token on the next line, or comment without blank line + if ( + mode === "always" && ( + !noNextLineToken || + hasNextLineComment && !hasBlankLineAfterComment(nextToken, nextLineNum) + ) + ) { + context.report({ + node, + messageId: "expected", + fix(fixer) { + if ((noNextLineToken ? getLastCommentLineOfBlock(nextLineNum) : lastToken.loc.end.line) === nextToken.loc.start.line) { + return fixer.insertTextBefore(nextToken, "\n\n"); + } + + return fixer.insertTextBeforeRange([nextToken.range[0] - nextToken.loc.start.column, nextToken.range[1]], "\n"); + } + }); + } + } + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + return { + VariableDeclaration: checkForBlankLine + }; + + } }; diff --git a/node_modules/eslint/lib/rules/newline-before-return.js b/node_modules/eslint/lib/rules/newline-before-return.js index d9ebc9c18..73d8ef99f 100644 --- a/node_modules/eslint/lib/rules/newline-before-return.js +++ b/node_modules/eslint/lib/rules/newline-before-return.js @@ -9,234 +9,209 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "layout", - - docs: { - description: "Require an empty line before `return` statements", - recommended: false, - url: "https://eslint.org/docs/latest/rules/newline-before-return", - }, - - fixable: "whitespace", - schema: [], - messages: { - expected: "Expected newline before return statement.", - }, - - deprecated: { - message: "The rule was replaced with a more general rule.", - url: "https://eslint.org/blog/2017/06/eslint-v4.0.0-released/", - deprecatedSince: "4.0.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: "The new rule moved to a plugin.", - url: "https://eslint.org/docs/latest/rules/padding-line-between-statements#examples", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "padding-line-between-statements", - url: "https://eslint.style/rules/padding-line-between-statements", - }, - }, - ], - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - /** - * Tests whether node is preceded by supplied tokens - * @param {ASTNode} node node to check - * @param {Array} testTokens array of tokens to test against - * @returns {boolean} Whether or not the node is preceded by one of the supplied tokens - * @private - */ - function isPrecededByTokens(node, testTokens) { - const tokenBefore = sourceCode.getTokenBefore(node); - - return testTokens.includes(tokenBefore.value); - } - - /** - * Checks whether node is the first node after statement or in block - * @param {ASTNode} node node to check - * @returns {boolean} Whether or not the node is the first node after statement or in block - * @private - */ - function isFirstNode(node) { - const parentType = node.parent.type; - - if (node.parent.body) { - return Array.isArray(node.parent.body) - ? node.parent.body[0] === node - : node.parent.body === node; - } - - if (parentType === "IfStatement") { - return isPrecededByTokens(node, ["else", ")"]); - } - if (parentType === "DoWhileStatement") { - return isPrecededByTokens(node, ["do"]); - } - if (parentType === "SwitchCase") { - return isPrecededByTokens(node, [":"]); - } - return isPrecededByTokens(node, [")"]); - } - - /** - * Returns the number of lines of comments that precede the node - * @param {ASTNode} node node to check for overlapping comments - * @param {number} lineNumTokenBefore line number of previous token, to check for overlapping comments - * @returns {number} Number of lines of comments that precede the node - * @private - */ - function calcCommentLines(node, lineNumTokenBefore) { - const comments = sourceCode.getCommentsBefore(node); - let numLinesComments = 0; - - if (!comments.length) { - return numLinesComments; - } - - comments.forEach(comment => { - numLinesComments++; - - if (comment.type === "Block") { - numLinesComments += - comment.loc.end.line - comment.loc.start.line; - } - - // avoid counting lines with inline comments twice - if (comment.loc.start.line === lineNumTokenBefore) { - numLinesComments--; - } - - if (comment.loc.end.line === node.loc.start.line) { - numLinesComments--; - } - }); - - return numLinesComments; - } - - /** - * Returns the line number of the token before the node that is passed in as an argument - * @param {ASTNode} node The node to use as the start of the calculation - * @returns {number} Line number of the token before `node` - * @private - */ - function getLineNumberOfTokenBefore(node) { - const tokenBefore = sourceCode.getTokenBefore(node); - let lineNumTokenBefore; - - /** - * Global return (at the beginning of a script) is a special case. - * If there is no token before `return`, then we expect no line - * break before the return. Comments are allowed to occupy lines - * before the global return, just no blank lines. - * Setting lineNumTokenBefore to zero in that case results in the - * desired behavior. - */ - if (tokenBefore) { - lineNumTokenBefore = tokenBefore.loc.end.line; - } else { - lineNumTokenBefore = 0; // global return at beginning of script - } - - return lineNumTokenBefore; - } - - /** - * Checks whether node is preceded by a newline - * @param {ASTNode} node node to check - * @returns {boolean} Whether or not the node is preceded by a newline - * @private - */ - function hasNewlineBefore(node) { - const lineNumNode = node.loc.start.line; - const lineNumTokenBefore = getLineNumberOfTokenBefore(node); - const commentLines = calcCommentLines(node, lineNumTokenBefore); - - return lineNumNode - lineNumTokenBefore - commentLines > 1; - } - - /** - * Checks whether it is safe to apply a fix to a given return statement. - * - * The fix is not considered safe if the given return statement has leading comments, - * as we cannot safely determine if the newline should be added before or after the comments. - * For more information, see: https://github.com/eslint/eslint/issues/5958#issuecomment-222767211 - * @param {ASTNode} node The return statement node to check. - * @returns {boolean} `true` if it can fix the node. - * @private - */ - function canFix(node) { - const leadingComments = sourceCode.getCommentsBefore(node); - const lastLeadingComment = leadingComments.at(-1); - const tokenBefore = sourceCode.getTokenBefore(node); - - if (leadingComments.length === 0) { - return true; - } - - /* - * if the last leading comment ends in the same line as the previous token and - * does not share a line with the `return` node, we can consider it safe to fix. - * Example: - * function a() { - * var b; //comment - * return; - * } - */ - if ( - lastLeadingComment.loc.end.line === tokenBefore.loc.end.line && - lastLeadingComment.loc.end.line !== node.loc.start.line - ) { - return true; - } - - return false; - } - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - return { - ReturnStatement(node) { - if (!isFirstNode(node) && !hasNewlineBefore(node)) { - context.report({ - node, - messageId: "expected", - fix(fixer) { - if (canFix(node)) { - const tokenBefore = - sourceCode.getTokenBefore(node); - const newlines = - node.loc.start.line === - tokenBefore.loc.end.line - ? "\n\n" - : "\n"; - - return fixer.insertTextBefore(node, newlines); - } - return null; - }, - }); - } - }, - }; - }, + meta: { + type: "layout", + + docs: { + description: "Require an empty line before `return` statements", + recommended: false, + url: "https://eslint.org/docs/latest/rules/newline-before-return" + }, + + fixable: "whitespace", + schema: [], + messages: { + expected: "Expected newline before return statement." + }, + + deprecated: true, + replacedBy: ["padding-line-between-statements"] + }, + + create(context) { + const sourceCode = context.sourceCode; + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + /** + * Tests whether node is preceded by supplied tokens + * @param {ASTNode} node node to check + * @param {Array} testTokens array of tokens to test against + * @returns {boolean} Whether or not the node is preceded by one of the supplied tokens + * @private + */ + function isPrecededByTokens(node, testTokens) { + const tokenBefore = sourceCode.getTokenBefore(node); + + return testTokens.includes(tokenBefore.value); + } + + /** + * Checks whether node is the first node after statement or in block + * @param {ASTNode} node node to check + * @returns {boolean} Whether or not the node is the first node after statement or in block + * @private + */ + function isFirstNode(node) { + const parentType = node.parent.type; + + if (node.parent.body) { + return Array.isArray(node.parent.body) + ? node.parent.body[0] === node + : node.parent.body === node; + } + + if (parentType === "IfStatement") { + return isPrecededByTokens(node, ["else", ")"]); + } + if (parentType === "DoWhileStatement") { + return isPrecededByTokens(node, ["do"]); + } + if (parentType === "SwitchCase") { + return isPrecededByTokens(node, [":"]); + } + return isPrecededByTokens(node, [")"]); + + } + + /** + * Returns the number of lines of comments that precede the node + * @param {ASTNode} node node to check for overlapping comments + * @param {number} lineNumTokenBefore line number of previous token, to check for overlapping comments + * @returns {number} Number of lines of comments that precede the node + * @private + */ + function calcCommentLines(node, lineNumTokenBefore) { + const comments = sourceCode.getCommentsBefore(node); + let numLinesComments = 0; + + if (!comments.length) { + return numLinesComments; + } + + comments.forEach(comment => { + numLinesComments++; + + if (comment.type === "Block") { + numLinesComments += comment.loc.end.line - comment.loc.start.line; + } + + // avoid counting lines with inline comments twice + if (comment.loc.start.line === lineNumTokenBefore) { + numLinesComments--; + } + + if (comment.loc.end.line === node.loc.start.line) { + numLinesComments--; + } + }); + + return numLinesComments; + } + + /** + * Returns the line number of the token before the node that is passed in as an argument + * @param {ASTNode} node The node to use as the start of the calculation + * @returns {number} Line number of the token before `node` + * @private + */ + function getLineNumberOfTokenBefore(node) { + const tokenBefore = sourceCode.getTokenBefore(node); + let lineNumTokenBefore; + + /** + * Global return (at the beginning of a script) is a special case. + * If there is no token before `return`, then we expect no line + * break before the return. Comments are allowed to occupy lines + * before the global return, just no blank lines. + * Setting lineNumTokenBefore to zero in that case results in the + * desired behavior. + */ + if (tokenBefore) { + lineNumTokenBefore = tokenBefore.loc.end.line; + } else { + lineNumTokenBefore = 0; // global return at beginning of script + } + + return lineNumTokenBefore; + } + + /** + * Checks whether node is preceded by a newline + * @param {ASTNode} node node to check + * @returns {boolean} Whether or not the node is preceded by a newline + * @private + */ + function hasNewlineBefore(node) { + const lineNumNode = node.loc.start.line; + const lineNumTokenBefore = getLineNumberOfTokenBefore(node); + const commentLines = calcCommentLines(node, lineNumTokenBefore); + + return (lineNumNode - lineNumTokenBefore - commentLines) > 1; + } + + /** + * Checks whether it is safe to apply a fix to a given return statement. + * + * The fix is not considered safe if the given return statement has leading comments, + * as we cannot safely determine if the newline should be added before or after the comments. + * For more information, see: https://github.com/eslint/eslint/issues/5958#issuecomment-222767211 + * @param {ASTNode} node The return statement node to check. + * @returns {boolean} `true` if it can fix the node. + * @private + */ + function canFix(node) { + const leadingComments = sourceCode.getCommentsBefore(node); + const lastLeadingComment = leadingComments[leadingComments.length - 1]; + const tokenBefore = sourceCode.getTokenBefore(node); + + if (leadingComments.length === 0) { + return true; + } + + /* + * if the last leading comment ends in the same line as the previous token and + * does not share a line with the `return` node, we can consider it safe to fix. + * Example: + * function a() { + * var b; //comment + * return; + * } + */ + if (lastLeadingComment.loc.end.line === tokenBefore.loc.end.line && + lastLeadingComment.loc.end.line !== node.loc.start.line) { + return true; + } + + return false; + } + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + return { + ReturnStatement(node) { + if (!isFirstNode(node) && !hasNewlineBefore(node)) { + context.report({ + node, + messageId: "expected", + fix(fixer) { + if (canFix(node)) { + const tokenBefore = sourceCode.getTokenBefore(node); + const newlines = node.loc.start.line === tokenBefore.loc.end.line ? "\n\n" : "\n"; + + return fixer.insertTextBefore(node, newlines); + } + return null; + } + }); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/newline-per-chained-call.js b/node_modules/eslint/lib/rules/newline-per-chained-call.js index c56e94660..3124ac2d1 100644 --- a/node_modules/eslint/lib/rules/newline-per-chained-call.js +++ b/node_modules/eslint/lib/rules/newline-per-chained-call.js @@ -13,147 +13,114 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "newline-per-chained-call", - url: "https://eslint.style/rules/newline-per-chained-call", - }, - }, - ], - }, - type: "layout", - - docs: { - description: "Require a newline after each call in a method chain", - recommended: false, - url: "https://eslint.org/docs/latest/rules/newline-per-chained-call", - }, - - fixable: "whitespace", - - schema: [ - { - type: "object", - properties: { - ignoreChainWithDepth: { - type: "integer", - minimum: 1, - maximum: 10, - default: 2, - }, - }, - additionalProperties: false, - }, - ], - messages: { - expected: "Expected line break before `{{callee}}`.", - }, - }, - - create(context) { - const options = context.options[0] || {}, - ignoreChainWithDepth = options.ignoreChainWithDepth || 2; - - const sourceCode = context.sourceCode; - - /** - * Get the prefix of a given MemberExpression node. - * If the MemberExpression node is a computed value it returns a - * left bracket. If not it returns a period. - * @param {ASTNode} node A MemberExpression node to get - * @returns {string} The prefix of the node. - */ - function getPrefix(node) { - if (node.computed) { - if (node.optional) { - return "?.["; - } - return "["; - } - if (node.optional) { - return "?."; - } - return "."; - } - - /** - * Gets the property text of a given MemberExpression node. - * If the text is multiline, this returns only the first line. - * @param {ASTNode} node A MemberExpression node to get. - * @returns {string} The property text of the node. - */ - function getPropertyText(node) { - const prefix = getPrefix(node); - const lines = sourceCode - .getText(node.property) - .split(astUtils.LINEBREAK_MATCHER); - const suffix = node.computed && lines.length === 1 ? "]" : ""; - - return prefix + lines[0] + suffix; - } - - return { - "CallExpression:exit"(node) { - const callee = astUtils.skipChainExpression(node.callee); - - if (callee.type !== "MemberExpression") { - return; - } - - let parent = astUtils.skipChainExpression(callee.object); - let depth = 1; - - while (parent && parent.callee) { - depth += 1; - parent = astUtils.skipChainExpression( - astUtils.skipChainExpression(parent.callee).object, - ); - } - - if ( - depth > ignoreChainWithDepth && - astUtils.isTokenOnSameLine(callee.object, callee.property) - ) { - const firstTokenAfterObject = sourceCode.getTokenAfter( - callee.object, - astUtils.isNotClosingParenToken, - ); - - context.report({ - node: callee.property, - loc: { - start: firstTokenAfterObject.loc.start, - end: callee.loc.end, - }, - messageId: "expected", - data: { - callee: getPropertyText(callee), - }, - fix(fixer) { - return fixer.insertTextBefore( - firstTokenAfterObject, - "\n", - ); - }, - }); - } - }, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Require a newline after each call in a method chain", + recommended: false, + url: "https://eslint.org/docs/latest/rules/newline-per-chained-call" + }, + + fixable: "whitespace", + + schema: [{ + type: "object", + properties: { + ignoreChainWithDepth: { + type: "integer", + minimum: 1, + maximum: 10, + default: 2 + } + }, + additionalProperties: false + }], + messages: { + expected: "Expected line break before `{{callee}}`." + } + }, + + create(context) { + + const options = context.options[0] || {}, + ignoreChainWithDepth = options.ignoreChainWithDepth || 2; + + const sourceCode = context.sourceCode; + + /** + * Get the prefix of a given MemberExpression node. + * If the MemberExpression node is a computed value it returns a + * left bracket. If not it returns a period. + * @param {ASTNode} node A MemberExpression node to get + * @returns {string} The prefix of the node. + */ + function getPrefix(node) { + if (node.computed) { + if (node.optional) { + return "?.["; + } + return "["; + } + if (node.optional) { + return "?."; + } + return "."; + } + + /** + * Gets the property text of a given MemberExpression node. + * If the text is multiline, this returns only the first line. + * @param {ASTNode} node A MemberExpression node to get. + * @returns {string} The property text of the node. + */ + function getPropertyText(node) { + const prefix = getPrefix(node); + const lines = sourceCode.getText(node.property).split(astUtils.LINEBREAK_MATCHER); + const suffix = node.computed && lines.length === 1 ? "]" : ""; + + return prefix + lines[0] + suffix; + } + + return { + "CallExpression:exit"(node) { + const callee = astUtils.skipChainExpression(node.callee); + + if (callee.type !== "MemberExpression") { + return; + } + + let parent = astUtils.skipChainExpression(callee.object); + let depth = 1; + + while (parent && parent.callee) { + depth += 1; + parent = astUtils.skipChainExpression(astUtils.skipChainExpression(parent.callee).object); + } + + if (depth > ignoreChainWithDepth && astUtils.isTokenOnSameLine(callee.object, callee.property)) { + const firstTokenAfterObject = sourceCode.getTokenAfter(callee.object, astUtils.isNotClosingParenToken); + + context.report({ + node: callee.property, + loc: { + start: firstTokenAfterObject.loc.start, + end: callee.loc.end + }, + messageId: "expected", + data: { + callee: getPropertyText(callee) + }, + fix(fixer) { + return fixer.insertTextBefore(firstTokenAfterObject, "\n"); + } + }); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-alert.js b/node_modules/eslint/lib/rules/no-alert.js index d1a3c5baa..cc8728565 100644 --- a/node_modules/eslint/lib/rules/no-alert.js +++ b/node_modules/eslint/lib/rules/no-alert.js @@ -9,9 +9,9 @@ //------------------------------------------------------------------------------ const { - getStaticPropertyName: getPropertyName, - getVariableByName, - skipChainExpression, + getStaticPropertyName: getPropertyName, + getVariableByName, + skipChainExpression } = require("./utils/ast-utils"); //------------------------------------------------------------------------------ @@ -24,7 +24,7 @@ const { * @returns {boolean} Whether or not the name is prohibited. */ function isProhibitedIdentifier(name) { - return /^(?:alert|confirm|prompt)$/u.test(name); + return /^(alert|confirm|prompt)$/u.test(name); } /** @@ -34,16 +34,13 @@ function isProhibitedIdentifier(name) { * @returns {Reference|null} Returns the found reference or null if none were found. */ function findReference(scope, node) { - const references = scope.references.filter( - reference => - reference.identifier.range[0] === node.range[0] && - reference.identifier.range[1] === node.range[1], - ); - - if (references.length === 1) { - return references[0]; - } - return null; + const references = scope.references.filter(reference => reference.identifier.range[0] === node.range[0] && + reference.identifier.range[1] === node.range[1]); + + if (references.length === 1) { + return references[0]; + } + return null; } /** @@ -53,11 +50,9 @@ function findReference(scope, node) { * @returns {boolean} Whether or not the name is shadowed. */ function isShadowed(scope, node) { - const reference = findReference(scope, node); + const reference = findReference(scope, node); - return ( - reference && reference.resolved && reference.resolved.defs.length > 0 - ); + return reference && reference.resolved && reference.resolved.defs.length > 0; } /** @@ -67,83 +62,77 @@ function isShadowed(scope, node) { * @returns {boolean} Whether or not the node is a reference to the global object. */ function isGlobalThisReferenceOrGlobalWindow(scope, node) { - if (scope.type === "global" && node.type === "ThisExpression") { - return true; - } - if ( - node.type === "Identifier" && - (node.name === "window" || - (node.name === "globalThis" && - getVariableByName(scope, "globalThis"))) - ) { - return !isShadowed(scope, node); - } - - return false; + if (scope.type === "global" && node.type === "ThisExpression") { + return true; + } + if ( + node.type === "Identifier" && + ( + node.name === "window" || + (node.name === "globalThis" && getVariableByName(scope, "globalThis")) + ) + ) { + return !isShadowed(scope, node); + } + + return false; } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: "Disallow the use of `alert`, `confirm`, and `prompt`", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-alert", - }, - - schema: [], - - messages: { - unexpected: "Unexpected {{name}}.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - return { - CallExpression(node) { - const callee = skipChainExpression(node.callee), - currentScope = sourceCode.getScope(node); - - // without window. - if (callee.type === "Identifier") { - const name = callee.name; - - if ( - !isShadowed(currentScope, callee) && - isProhibitedIdentifier(callee.name) - ) { - context.report({ - node, - messageId: "unexpected", - data: { name }, - }); - } - } else if ( - callee.type === "MemberExpression" && - isGlobalThisReferenceOrGlobalWindow( - currentScope, - callee.object, - ) - ) { - const name = getPropertyName(callee); - - if (isProhibitedIdentifier(name)) { - context.report({ - node, - messageId: "unexpected", - data: { name }, - }); - } - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow the use of `alert`, `confirm`, and `prompt`", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-alert" + }, + + schema: [], + + messages: { + unexpected: "Unexpected {{name}}." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + + return { + CallExpression(node) { + const callee = skipChainExpression(node.callee), + currentScope = sourceCode.getScope(node); + + // without window. + if (callee.type === "Identifier") { + const name = callee.name; + + if (!isShadowed(currentScope, callee) && isProhibitedIdentifier(callee.name)) { + context.report({ + node, + messageId: "unexpected", + data: { name } + }); + } + + } else if (callee.type === "MemberExpression" && isGlobalThisReferenceOrGlobalWindow(currentScope, callee.object)) { + const name = getPropertyName(callee); + + if (isProhibitedIdentifier(name)) { + context.report({ + node, + messageId: "unexpected", + data: { name } + }); + } + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-array-constructor.js b/node_modules/eslint/lib/rules/no-array-constructor.js index 46e8f6b74..f56b68766 100644 --- a/node_modules/eslint/lib/rules/no-array-constructor.js +++ b/node_modules/eslint/lib/rules/no-array-constructor.js @@ -10,186 +10,124 @@ //------------------------------------------------------------------------------ const { - getVariableByName, - isClosingParenToken, - isOpeningParenToken, - isStartOfExpressionStatement, - needsPrecedingSemicolon, + getVariableByName, + isClosingParenToken, + isOpeningParenToken, + isStartOfExpressionStatement, + needsPrecedingSemicolon } = require("./utils/ast-utils"); //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - dialects: ["javascript", "typescript"], - language: "javascript", - type: "suggestion", - - docs: { - description: "Disallow `Array` constructors", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-array-constructor", - }, - - fixable: "code", - - hasSuggestions: true, - - schema: [], - - messages: { - preferLiteral: "The array literal notation [] is preferable.", - useLiteral: "Replace with an array literal.", - useLiteralAfterSemicolon: - "Replace with an array literal, add preceding semicolon.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - /** - * Checks if there are comments in Array constructor expressions. - * @param {ASTNode} node A CallExpression or NewExpression node. - * @returns {boolean} True if there are comments, false otherwise. - */ - function hasCommentsInArrayConstructor(node) { - const firstToken = sourceCode.getFirstToken(node); - const lastToken = sourceCode.getLastToken(node); - - let lastRelevantToken = sourceCode.getLastToken(node.callee); - - while ( - lastRelevantToken !== lastToken && - !isOpeningParenToken(lastRelevantToken) - ) { - lastRelevantToken = sourceCode.getTokenAfter(lastRelevantToken); - } - - return sourceCode.commentsExistBetween( - firstToken, - lastRelevantToken, - ); - } - - /** - * Gets the text between the calling parentheses of a CallExpression or NewExpression. - * @param {ASTNode} node A CallExpression or NewExpression node. - * @returns {string} The text between the calling parentheses, or an empty string if there are none. - */ - function getArgumentsText(node) { - const lastToken = sourceCode.getLastToken(node); - - if (!isClosingParenToken(lastToken)) { - return ""; - } - - let firstToken = node.callee; - - do { - firstToken = sourceCode.getTokenAfter(firstToken); - if (!firstToken || firstToken === lastToken) { - return ""; - } - } while (!isOpeningParenToken(firstToken)); - - return sourceCode.text.slice( - firstToken.range[1], - lastToken.range[0], - ); - } - - /** - * Disallow construction of dense arrays using the Array constructor - * @param {ASTNode} node node to evaluate - * @returns {void} - * @private - */ - function check(node) { - if ( - node.callee.type !== "Identifier" || - node.callee.name !== "Array" || - node.typeArguments || - (node.arguments.length === 1 && - node.arguments[0].type !== "SpreadElement") - ) { - return; - } - - const variable = getVariableByName( - sourceCode.getScope(node), - "Array", - ); - - /* - * Check if `Array` is a predefined global variable: predefined globals have no declarations, - * meaning that the `identifiers` list of the variable object is empty. - */ - if (variable && variable.identifiers.length === 0) { - const argsText = getArgumentsText(node); - let fixText; - let messageId; - - const nonSpreadCount = node.arguments.reduce( - (count, arg) => - arg.type !== "SpreadElement" ? count + 1 : count, - 0, - ); - - const shouldSuggest = - node.optional || - (node.arguments.length > 0 && nonSpreadCount < 2) || - hasCommentsInArrayConstructor(node); - - /* - * Check if the suggested change should include a preceding semicolon or not. - * Due to JavaScript's ASI rules, a missing semicolon may be inserted automatically - * before an expression like `Array()` or `new Array()`, but not when the expression - * is changed into an array literal like `[]`. - */ - if ( - isStartOfExpressionStatement(node) && - needsPrecedingSemicolon(sourceCode, node) - ) { - fixText = `;[${argsText}]`; - messageId = "useLiteralAfterSemicolon"; - } else { - fixText = `[${argsText}]`; - messageId = "useLiteral"; - } - - context.report({ - node, - messageId: "preferLiteral", - fix(fixer) { - if (shouldSuggest) { - return null; - } - - return fixer.replaceText(node, fixText); - }, - suggest: [ - { - messageId, - fix(fixer) { - if (shouldSuggest) { - return fixer.replaceText(node, fixText); - } - - return null; - }, - }, - ], - }); - } - } - - return { - CallExpression: check, - NewExpression: check, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow `Array` constructors", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-array-constructor" + }, + + hasSuggestions: true, + + schema: [], + + messages: { + preferLiteral: "The array literal notation [] is preferable.", + useLiteral: "Replace with an array literal.", + useLiteralAfterSemicolon: "Replace with an array literal, add preceding semicolon." + } + }, + + create(context) { + + const sourceCode = context.sourceCode; + + /** + * Gets the text between the calling parentheses of a CallExpression or NewExpression. + * @param {ASTNode} node A CallExpression or NewExpression node. + * @returns {string} The text between the calling parentheses, or an empty string if there are none. + */ + function getArgumentsText(node) { + const lastToken = sourceCode.getLastToken(node); + + if (!isClosingParenToken(lastToken)) { + return ""; + } + + let firstToken = node.callee; + + do { + firstToken = sourceCode.getTokenAfter(firstToken); + if (!firstToken || firstToken === lastToken) { + return ""; + } + } while (!isOpeningParenToken(firstToken)); + + return sourceCode.text.slice(firstToken.range[1], lastToken.range[0]); + } + + /** + * Disallow construction of dense arrays using the Array constructor + * @param {ASTNode} node node to evaluate + * @returns {void} + * @private + */ + function check(node) { + if ( + node.callee.type !== "Identifier" || + node.callee.name !== "Array" || + node.arguments.length === 1 && + node.arguments[0].type !== "SpreadElement") { + return; + } + + const variable = getVariableByName(sourceCode.getScope(node), "Array"); + + /* + * Check if `Array` is a predefined global variable: predefined globals have no declarations, + * meaning that the `identifiers` list of the variable object is empty. + */ + if (variable && variable.identifiers.length === 0) { + const argsText = getArgumentsText(node); + let fixText; + let messageId; + + /* + * Check if the suggested change should include a preceding semicolon or not. + * Due to JavaScript's ASI rules, a missing semicolon may be inserted automatically + * before an expression like `Array()` or `new Array()`, but not when the expression + * is changed into an array literal like `[]`. + */ + if (isStartOfExpressionStatement(node) && needsPrecedingSemicolon(sourceCode, node)) { + fixText = `;[${argsText}]`; + messageId = "useLiteralAfterSemicolon"; + } else { + fixText = `[${argsText}]`; + messageId = "useLiteral"; + } + + context.report({ + node, + messageId: "preferLiteral", + suggest: [ + { + messageId, + fix: fixer => fixer.replaceText(node, fixText) + } + ] + }); + } + } + + return { + CallExpression: check, + NewExpression: check + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-async-promise-executor.js b/node_modules/eslint/lib/rules/no-async-promise-executor.js index 22edd48f7..ea6c85114 100644 --- a/node_modules/eslint/lib/rules/no-async-promise-executor.js +++ b/node_modules/eslint/lib/rules/no-async-promise-executor.js @@ -8,38 +8,32 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", + meta: { + type: "problem", - docs: { - description: - "Disallow using an async function as a Promise executor", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-async-promise-executor", - }, + docs: { + description: "Disallow using an async function as a Promise executor", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-async-promise-executor" + }, - fixable: null, - schema: [], - messages: { - async: "Promise executor functions should not be async.", - }, - }, + fixable: null, + schema: [], + messages: { + async: "Promise executor functions should not be async." + } + }, - create(context) { - return { - "NewExpression[callee.name='Promise'][arguments.0.async=true]"( - node, - ) { - context.report({ - node: context.sourceCode.getFirstToken( - node.arguments[0], - token => token.value === "async", - ), - messageId: "async", - }); - }, - }; - }, + create(context) { + return { + "NewExpression[callee.name='Promise'][arguments.0.async=true]"(node) { + context.report({ + node: context.sourceCode.getFirstToken(node.arguments[0], token => token.value === "async"), + messageId: "async" + }); + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-await-in-loop.js b/node_modules/eslint/lib/rules/no-await-in-loop.js index 2945257d1..20230defa 100644 --- a/node_modules/eslint/lib/rules/no-await-in-loop.js +++ b/node_modules/eslint/lib/rules/no-await-in-loop.js @@ -10,18 +10,19 @@ * @returns {boolean} `true` if it should stop traversing. */ function isBoundary(node) { - const t = node.type; + const t = node.type; - return ( - t === "FunctionDeclaration" || - t === "FunctionExpression" || - t === "ArrowFunctionExpression" || - /* - * Don't report the await expressions on for-await-of loop since it's - * asynchronous iteration intentionally. - */ - (t === "ForOfStatement" && node.await === true) - ); + return ( + t === "FunctionDeclaration" || + t === "FunctionExpression" || + t === "ArrowFunctionExpression" || + + /* + * Don't report the await expressions on for-await-of loop since it's + * asynchronous iteration intentionally. + */ + (t === "ForOfStatement" && node.await === true) + ); } /** @@ -31,85 +32,75 @@ function isBoundary(node) { * @returns {boolean} `true` if the node is in loop. */ function isLooped(node, parent) { - switch (parent.type) { - case "ForStatement": - return ( - node === parent.test || - node === parent.update || - node === parent.body - ); + switch (parent.type) { + case "ForStatement": + return ( + node === parent.test || + node === parent.update || + node === parent.body + ); - case "ForOfStatement": - case "ForInStatement": - return ( - node === parent.body || - (node === parent.left && node.kind === "await using") - ); + case "ForOfStatement": + case "ForInStatement": + return node === parent.body; - case "WhileStatement": - case "DoWhileStatement": - return node === parent.test || node === parent.body; + case "WhileStatement": + case "DoWhileStatement": + return node === parent.test || node === parent.body; - default: - return false; - } + default: + return false; + } } -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", + meta: { + type: "problem", - docs: { - description: "Disallow `await` inside of loops", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-await-in-loop", - }, + docs: { + description: "Disallow `await` inside of loops", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-await-in-loop" + }, - schema: [], + schema: [], - messages: { - unexpectedAwait: "Unexpected `await` inside a loop.", - }, - }, - create(context) { - /** - * Validate an await expression. - * @param {ASTNode} awaitNode An AwaitExpression or ForOfStatement node to validate. - * @returns {void} - */ - function validate(awaitNode) { - if ( - awaitNode.type === "VariableDeclaration" && - awaitNode.kind !== "await using" - ) { - return; - } + messages: { + unexpectedAwait: "Unexpected `await` inside a loop." + } + }, + create(context) { - if (awaitNode.type === "ForOfStatement" && !awaitNode.await) { - return; - } + /** + * Validate an await expression. + * @param {ASTNode} awaitNode An AwaitExpression or ForOfStatement node to validate. + * @returns {void} + */ + function validate(awaitNode) { + if (awaitNode.type === "ForOfStatement" && !awaitNode.await) { + return; + } - let node = awaitNode; - let parent = node.parent; + let node = awaitNode; + let parent = node.parent; - while (parent && !isBoundary(parent)) { - if (isLooped(node, parent)) { - context.report({ - node: awaitNode, - messageId: "unexpectedAwait", - }); - return; - } - node = parent; - parent = parent.parent; - } - } + while (parent && !isBoundary(parent)) { + if (isLooped(node, parent)) { + context.report({ + node: awaitNode, + messageId: "unexpectedAwait" + }); + return; + } + node = parent; + parent = parent.parent; + } + } - return { - AwaitExpression: validate, - ForOfStatement: validate, - VariableDeclaration: validate, - }; - }, + return { + AwaitExpression: validate, + ForOfStatement: validate + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-bitwise.js b/node_modules/eslint/lib/rules/no-bitwise.js index 10149ed61..d90992b20 100644 --- a/node_modules/eslint/lib/rules/no-bitwise.js +++ b/node_modules/eslint/lib/rules/no-bitwise.js @@ -11,135 +11,109 @@ * */ const BITWISE_OPERATORS = [ - "^", - "|", - "&", - "<<", - ">>", - ">>>", - "^=", - "|=", - "&=", - "<<=", - ">>=", - ">>>=", - "~", + "^", "|", "&", "<<", ">>", ">>>", + "^=", "|=", "&=", "<<=", ">>=", ">>>=", + "~" ]; //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", + meta: { + type: "suggestion", - defaultOptions: [ - { - allow: [], - int32Hint: false, - }, - ], + docs: { + description: "Disallow bitwise operators", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-bitwise" + }, - docs: { - description: "Disallow bitwise operators", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-bitwise", - }, + schema: [ + { + type: "object", + properties: { + allow: { + type: "array", + items: { + enum: BITWISE_OPERATORS + }, + uniqueItems: true + }, + int32Hint: { + type: "boolean", + default: false + } + }, + additionalProperties: false + } + ], - schema: [ - { - type: "object", - properties: { - allow: { - type: "array", - items: { - enum: BITWISE_OPERATORS, - }, - uniqueItems: true, - }, - int32Hint: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], + messages: { + unexpected: "Unexpected use of '{{operator}}'." + } + }, - messages: { - unexpected: "Unexpected use of '{{operator}}'.", - }, - }, + create(context) { + const options = context.options[0] || {}; + const allowed = options.allow || []; + const int32Hint = options.int32Hint === true; - create(context) { - const [{ allow: allowed, int32Hint }] = context.options; + /** + * Reports an unexpected use of a bitwise operator. + * @param {ASTNode} node Node which contains the bitwise operator. + * @returns {void} + */ + function report(node) { + context.report({ node, messageId: "unexpected", data: { operator: node.operator } }); + } - /** - * Reports an unexpected use of a bitwise operator. - * @param {ASTNode} node Node which contains the bitwise operator. - * @returns {void} - */ - function report(node) { - context.report({ - node, - messageId: "unexpected", - data: { operator: node.operator }, - }); - } + /** + * Checks if the given node has a bitwise operator. + * @param {ASTNode} node The node to check. + * @returns {boolean} Whether or not the node has a bitwise operator. + */ + function hasBitwiseOperator(node) { + return BITWISE_OPERATORS.includes(node.operator); + } - /** - * Checks if the given node has a bitwise operator. - * @param {ASTNode} node The node to check. - * @returns {boolean} Whether or not the node has a bitwise operator. - */ - function hasBitwiseOperator(node) { - return BITWISE_OPERATORS.includes(node.operator); - } + /** + * Checks if exceptions were provided, e.g. `{ allow: ['~', '|'] }`. + * @param {ASTNode} node The node to check. + * @returns {boolean} Whether or not the node has a bitwise operator. + */ + function allowedOperator(node) { + return allowed.includes(node.operator); + } - /** - * Checks if exceptions were provided, e.g. `{ allow: ['~', '|'] }`. - * @param {ASTNode} node The node to check. - * @returns {boolean} Whether or not the node has a bitwise operator. - */ - function allowedOperator(node) { - return allowed.includes(node.operator); - } + /** + * Checks if the given bitwise operator is used for integer typecasting, i.e. "|0" + * @param {ASTNode} node The node to check. + * @returns {boolean} whether the node is used in integer typecasting. + */ + function isInt32Hint(node) { + return int32Hint && node.operator === "|" && node.right && + node.right.type === "Literal" && node.right.value === 0; + } - /** - * Checks if the given bitwise operator is used for integer typecasting, i.e. "|0" - * @param {ASTNode} node The node to check. - * @returns {boolean} whether the node is used in integer typecasting. - */ - function isInt32Hint(node) { - return ( - int32Hint && - node.operator === "|" && - node.right && - node.right.type === "Literal" && - node.right.value === 0 - ); - } + /** + * Report if the given node contains a bitwise operator. + * @param {ASTNode} node The node to check. + * @returns {void} + */ + function checkNodeForBitwiseOperator(node) { + if (hasBitwiseOperator(node) && !allowedOperator(node) && !isInt32Hint(node)) { + report(node); + } + } - /** - * Report if the given node contains a bitwise operator. - * @param {ASTNode} node The node to check. - * @returns {void} - */ - function checkNodeForBitwiseOperator(node) { - if ( - hasBitwiseOperator(node) && - !allowedOperator(node) && - !isInt32Hint(node) - ) { - report(node); - } - } + return { + AssignmentExpression: checkNodeForBitwiseOperator, + BinaryExpression: checkNodeForBitwiseOperator, + UnaryExpression: checkNodeForBitwiseOperator + }; - return { - AssignmentExpression: checkNodeForBitwiseOperator, - BinaryExpression: checkNodeForBitwiseOperator, - UnaryExpression: checkNodeForBitwiseOperator, - }; - }, + } }; diff --git a/node_modules/eslint/lib/rules/no-buffer-constructor.js b/node_modules/eslint/lib/rules/no-buffer-constructor.js index 6a48b5573..0b73c7674 100644 --- a/node_modules/eslint/lib/rules/no-buffer-constructor.js +++ b/node_modules/eslint/lib/rules/no-buffer-constructor.js @@ -9,66 +9,42 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Node.js rules were moved out of ESLint core.", - url: "https://eslint.org/docs/latest/use/migrating-to-7.0.0#deprecate-node-rules", - deprecatedSince: "7.0.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "eslint-plugin-n now maintains deprecated Node.js-related rules.", - plugin: { - name: "eslint-plugin-n", - url: "https://github.com/eslint-community/eslint-plugin-n", - }, - rule: { - name: "no-deprecated-api", - url: "https://github.com/eslint-community/eslint-plugin-n/tree/master/docs/rules/no-deprecated-api.md", - }, - }, - ], - }, - - type: "problem", - - docs: { - description: "Disallow use of the `Buffer()` constructor", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-buffer-constructor", - }, - - schema: [], - - messages: { - deprecated: - "{{expr}} is deprecated. Use Buffer.from(), Buffer.alloc(), or Buffer.allocUnsafe() instead.", - }, - }, - - create(context) { - //---------------------------------------------------------------------- - // Public - //---------------------------------------------------------------------- - - return { - "CallExpression[callee.name='Buffer'], NewExpression[callee.name='Buffer']"( - node, - ) { - context.report({ - node, - messageId: "deprecated", - data: { - expr: - node.type === "CallExpression" - ? "Buffer()" - : "new Buffer()", - }, - }); - }, - }; - }, + meta: { + deprecated: true, + + replacedBy: [], + + type: "problem", + + docs: { + description: "Disallow use of the `Buffer()` constructor", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-buffer-constructor" + }, + + schema: [], + + messages: { + deprecated: "{{expr}} is deprecated. Use Buffer.from(), Buffer.alloc(), or Buffer.allocUnsafe() instead." + } + }, + + create(context) { + + //---------------------------------------------------------------------- + // Public + //---------------------------------------------------------------------- + + return { + "CallExpression[callee.name='Buffer'], NewExpression[callee.name='Buffer']"(node) { + context.report({ + node, + messageId: "deprecated", + data: { expr: node.type === "CallExpression" ? "Buffer()" : "new Buffer()" } + }); + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-caller.js b/node_modules/eslint/lib/rules/no-caller.js index 50790ff0e..3e61a8e1c 100644 --- a/node_modules/eslint/lib/rules/no-caller.js +++ b/node_modules/eslint/lib/rules/no-caller.js @@ -9,44 +9,38 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: - "Disallow the use of `arguments.caller` or `arguments.callee`", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-caller", - }, - - schema: [], - - messages: { - unexpected: "Avoid arguments.{{prop}}.", - }, - }, - - create(context) { - return { - MemberExpression(node) { - const objectName = node.object.name, - propertyName = node.property.name; - - if ( - objectName === "arguments" && - !node.computed && - propertyName && - propertyName.match(/^calle[er]$/u) - ) { - context.report({ - node, - messageId: "unexpected", - data: { prop: propertyName }, - }); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow the use of `arguments.caller` or `arguments.callee`", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-caller" + }, + + schema: [], + + messages: { + unexpected: "Avoid arguments.{{prop}}." + } + }, + + create(context) { + + return { + + MemberExpression(node) { + const objectName = node.object.name, + propertyName = node.property.name; + + if (objectName === "arguments" && !node.computed && propertyName && propertyName.match(/^calle[er]$/u)) { + context.report({ node, messageId: "unexpected", data: { prop: propertyName } }); + } + + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-case-declarations.js b/node_modules/eslint/lib/rules/no-case-declarations.js index 168594aad..8dc5b021d 100644 --- a/node_modules/eslint/lib/rules/no-case-declarations.js +++ b/node_modules/eslint/lib/rules/no-case-declarations.js @@ -8,73 +8,57 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", + meta: { + type: "suggestion", - docs: { - description: "Disallow lexical declarations in case clauses", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-case-declarations", - }, + docs: { + description: "Disallow lexical declarations in case clauses", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-case-declarations" + }, - hasSuggestions: true, + schema: [], - schema: [], + messages: { + unexpected: "Unexpected lexical declaration in case block." + } + }, - messages: { - addBrackets: "Add {} brackets around the case block.", - unexpected: "Unexpected lexical declaration in case block.", - }, - }, + create(context) { - create(context) { - /** - * Checks whether or not a node is a lexical declaration. - * @param {ASTNode} node A direct child statement of a switch case. - * @returns {boolean} Whether or not the node is a lexical declaration. - */ - function isLexicalDeclaration(node) { - switch (node.type) { - case "FunctionDeclaration": - case "ClassDeclaration": - return true; - case "VariableDeclaration": - return node.kind !== "var"; - default: - return false; - } - } + /** + * Checks whether or not a node is a lexical declaration. + * @param {ASTNode} node A direct child statement of a switch case. + * @returns {boolean} Whether or not the node is a lexical declaration. + */ + function isLexicalDeclaration(node) { + switch (node.type) { + case "FunctionDeclaration": + case "ClassDeclaration": + return true; + case "VariableDeclaration": + return node.kind !== "var"; + default: + return false; + } + } - return { - SwitchCase(node) { - for (let i = 0; i < node.consequent.length; i++) { - const statement = node.consequent[i]; + return { + SwitchCase(node) { + for (let i = 0; i < node.consequent.length; i++) { + const statement = node.consequent[i]; - if (isLexicalDeclaration(statement)) { - context.report({ - node: statement, - messageId: "unexpected", - suggest: [ - { - messageId: "addBrackets", - fix: fixer => [ - fixer.insertTextBefore( - node.consequent[0], - "{ ", - ), - fixer.insertTextAfter( - node.consequent.at(-1), - " }", - ), - ], - }, - ], - }); - } - } - }, - }; - }, + if (isLexicalDeclaration(statement)) { + context.report({ + node: statement, + messageId: "unexpected" + }); + } + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-catch-shadow.js b/node_modules/eslint/lib/rules/no-catch-shadow.js index b47ac3248..f9d855243 100644 --- a/node_modules/eslint/lib/rules/no-catch-shadow.js +++ b/node_modules/eslint/lib/rules/no-catch-shadow.js @@ -16,81 +16,67 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: - "Disallow `catch` clause parameters from shadowing variables in the outer scope", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-catch-shadow", - }, - - deprecated: { - message: "This rule was renamed.", - url: "https://eslint.org/blog/2018/07/eslint-v5.1.0-released/", - deprecatedSince: "5.1.0", - availableUntil: "11.0.0", - replacedBy: [ - { - rule: { - name: "no-shadow", - url: "https://eslint.org/docs/rules/no-shadow", - }, - }, - ], - }, - schema: [], - - messages: { - mutable: - "Value of '{{name}}' may be overwritten in IE 8 and earlier.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - /** - * Check if the parameters are been shadowed - * @param {Object} scope current scope - * @param {string} name parameter name - * @returns {boolean} True is its been shadowed - */ - function paramIsShadowing(scope, name) { - return astUtils.getVariableByName(scope, name) !== null; - } - - //-------------------------------------------------------------------------- - // Public API - //-------------------------------------------------------------------------- - - return { - "CatchClause[param!=null]"(node) { - let scope = sourceCode.getScope(node); - - /* - * When ecmaVersion >= 6, CatchClause creates its own scope - * so start from one upper scope to exclude the current node - */ - if (scope.block === node) { - scope = scope.upper; - } - - if (paramIsShadowing(scope, node.param.name)) { - context.report({ - node, - messageId: "mutable", - data: { name: node.param.name }, - }); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow `catch` clause parameters from shadowing variables in the outer scope", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-catch-shadow" + }, + + replacedBy: ["no-shadow"], + + deprecated: true, + schema: [], + + messages: { + mutable: "Value of '{{name}}' may be overwritten in IE 8 and earlier." + } + }, + + create(context) { + + const sourceCode = context.sourceCode; + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + /** + * Check if the parameters are been shadowed + * @param {Object} scope current scope + * @param {string} name parameter name + * @returns {boolean} True is its been shadowed + */ + function paramIsShadowing(scope, name) { + return astUtils.getVariableByName(scope, name) !== null; + } + + //-------------------------------------------------------------------------- + // Public API + //-------------------------------------------------------------------------- + + return { + + "CatchClause[param!=null]"(node) { + let scope = sourceCode.getScope(node); + + /* + * When ecmaVersion >= 6, CatchClause creates its own scope + * so start from one upper scope to exclude the current node + */ + if (scope.block === node) { + scope = scope.upper; + } + + if (paramIsShadowing(scope, node.param.name)) { + context.report({ node, messageId: "mutable", data: { name: node.param.name } }); + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-class-assign.js b/node_modules/eslint/lib/rules/no-class-assign.js index 649769598..49f3b844e 100644 --- a/node_modules/eslint/lib/rules/no-class-assign.js +++ b/node_modules/eslint/lib/rules/no-class-assign.js @@ -11,56 +11,53 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - docs: { - description: "Disallow reassigning class members", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-class-assign", - }, - - schema: [], - - messages: { - class: "'{{name}}' is a class.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - /** - * Finds and reports references that are non initializer and writable. - * @param {Variable} variable A variable to check. - * @returns {void} - */ - function checkVariable(variable) { - astUtils - .getModifyingReferences(variable.references) - .forEach(reference => { - context.report({ - node: reference.identifier, - messageId: "class", - data: { name: reference.identifier.name }, - }); - }); - } - - /** - * Finds and reports references that are non initializer and writable. - * @param {ASTNode} node A ClassDeclaration/ClassExpression node to check. - * @returns {void} - */ - function checkForClass(node) { - sourceCode.getDeclaredVariables(node).forEach(checkVariable); - } - - return { - ClassDeclaration: checkForClass, - ClassExpression: checkForClass, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow reassigning class members", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-class-assign" + }, + + schema: [], + + messages: { + class: "'{{name}}' is a class." + } + }, + + create(context) { + + const sourceCode = context.sourceCode; + + /** + * Finds and reports references that are non initializer and writable. + * @param {Variable} variable A variable to check. + * @returns {void} + */ + function checkVariable(variable) { + astUtils.getModifyingReferences(variable.references).forEach(reference => { + context.report({ node: reference.identifier, messageId: "class", data: { name: reference.identifier.name } }); + + }); + } + + /** + * Finds and reports references that are non initializer and writable. + * @param {ASTNode} node A ClassDeclaration/ClassExpression node to check. + * @returns {void} + */ + function checkForClass(node) { + sourceCode.getDeclaredVariables(node).forEach(checkVariable); + } + + return { + ClassDeclaration: checkForClass, + ClassExpression: checkForClass + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-compare-neg-zero.js b/node_modules/eslint/lib/rules/no-compare-neg-zero.js index a5683164f..112f6c1d1 100644 --- a/node_modules/eslint/lib/rules/no-compare-neg-zero.js +++ b/node_modules/eslint/lib/rules/no-compare-neg-zero.js @@ -8,67 +8,53 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - docs: { - description: "Disallow comparing against `-0`", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-compare-neg-zero", - }, - - fixable: null, - schema: [], - - messages: { - unexpected: - "Do not use the '{{operator}}' operator to compare against -0.", - }, - }, - - create(context) { - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - /** - * Checks a given node is -0 - * @param {ASTNode} node A node to check. - * @returns {boolean} `true` if the node is -0. - */ - function isNegZero(node) { - return ( - node.type === "UnaryExpression" && - node.operator === "-" && - node.argument.type === "Literal" && - node.argument.value === 0 - ); - } - const OPERATORS_TO_CHECK = new Set([ - ">", - ">=", - "<", - "<=", - "==", - "===", - "!=", - "!==", - ]); - - return { - BinaryExpression(node) { - if (OPERATORS_TO_CHECK.has(node.operator)) { - if (isNegZero(node.left) || isNegZero(node.right)) { - context.report({ - node, - messageId: "unexpected", - data: { operator: node.operator }, - }); - } - } - }, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow comparing against -0", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-compare-neg-zero" + }, + + fixable: null, + schema: [], + + messages: { + unexpected: "Do not use the '{{operator}}' operator to compare against -0." + } + }, + + create(context) { + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + /** + * Checks a given node is -0 + * @param {ASTNode} node A node to check. + * @returns {boolean} `true` if the node is -0. + */ + function isNegZero(node) { + return node.type === "UnaryExpression" && node.operator === "-" && node.argument.type === "Literal" && node.argument.value === 0; + } + const OPERATORS_TO_CHECK = new Set([">", ">=", "<", "<=", "==", "===", "!=", "!=="]); + + return { + BinaryExpression(node) { + if (OPERATORS_TO_CHECK.has(node.operator)) { + if (isNegZero(node.left) || isNegZero(node.right)) { + context.report({ + node, + messageId: "unexpected", + data: { operator: node.operator } + }); + } + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-cond-assign.js b/node_modules/eslint/lib/rules/no-cond-assign.js index a686e690a..952920215 100644 --- a/node_modules/eslint/lib/rules/no-cond-assign.js +++ b/node_modules/eslint/lib/rules/no-cond-assign.js @@ -15,161 +15,145 @@ const astUtils = require("./utils/ast-utils"); // Helpers //------------------------------------------------------------------------------ -const TEST_CONDITION_PARENT_TYPES = new Set([ - "IfStatement", - "WhileStatement", - "DoWhileStatement", - "ForStatement", - "ConditionalExpression", -]); +const TEST_CONDITION_PARENT_TYPES = new Set(["IfStatement", "WhileStatement", "DoWhileStatement", "ForStatement", "ConditionalExpression"]); const NODE_DESCRIPTIONS = { - DoWhileStatement: "a 'do...while' statement", - ForStatement: "a 'for' statement", - IfStatement: "an 'if' statement", - WhileStatement: "a 'while' statement", + DoWhileStatement: "a 'do...while' statement", + ForStatement: "a 'for' statement", + IfStatement: "an 'if' statement", + WhileStatement: "a 'while' statement" }; //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - defaultOptions: ["except-parens"], - - docs: { - description: - "Disallow assignment operators in conditional expressions", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-cond-assign", - }, - - schema: [ - { - enum: ["except-parens", "always"], - }, - ], - - messages: { - unexpected: "Unexpected assignment within {{type}}.", - - // must match JSHint's error message - missing: - "Expected a conditional expression and instead saw an assignment.", - }, - }, - - create(context) { - const [prohibitAssign] = context.options; - const sourceCode = context.sourceCode; - - /** - * Check whether an AST node is the test expression for a conditional statement. - * @param {!Object} node The node to test. - * @returns {boolean} `true` if the node is the text expression for a conditional statement; otherwise, `false`. - */ - function isConditionalTestExpression(node) { - return ( - node.parent && - TEST_CONDITION_PARENT_TYPES.has(node.parent.type) && - node === node.parent.test - ); - } - - /** - * Given an AST node, perform a bottom-up search for the first ancestor that represents a conditional statement. - * @param {!Object} node The node to use at the start of the search. - * @returns {?Object} The closest ancestor node that represents a conditional statement. - */ - function findConditionalAncestor(node) { - let currentAncestor = node; - - do { - if (isConditionalTestExpression(currentAncestor)) { - return currentAncestor.parent; - } - } while ( - (currentAncestor = currentAncestor.parent) && - !astUtils.isFunction(currentAncestor) - ); - - return null; - } - - /** - * Check whether the code represented by an AST node is enclosed in two sets of parentheses. - * @param {!Object} node The node to test. - * @returns {boolean} `true` if the code is enclosed in two sets of parentheses; otherwise, `false`. - */ - function isParenthesisedTwice(node) { - const previousToken = sourceCode.getTokenBefore(node, 1), - nextToken = sourceCode.getTokenAfter(node, 1); - - return ( - astUtils.isParenthesised(sourceCode, node) && - previousToken && - astUtils.isOpeningParenToken(previousToken) && - previousToken.range[1] <= node.range[0] && - astUtils.isClosingParenToken(nextToken) && - nextToken.range[0] >= node.range[1] - ); - } - - /** - * Check a conditional statement's test expression for top-level assignments that are not enclosed in parentheses. - * @param {!Object} node The node for the conditional statement. - * @returns {void} - */ - function testForAssign(node) { - if ( - node.test && - node.test.type === "AssignmentExpression" && - (node.type === "ForStatement" - ? !astUtils.isParenthesised(sourceCode, node.test) - : !isParenthesisedTwice(node.test)) - ) { - context.report({ - node: node.test, - messageId: "missing", - }); - } - } - - /** - * Check whether an assignment expression is descended from a conditional statement's test expression. - * @param {!Object} node The node for the assignment expression. - * @returns {void} - */ - function testForConditionalAncestor(node) { - const ancestor = findConditionalAncestor(node); - - if (ancestor) { - context.report({ - node, - messageId: "unexpected", - data: { - type: NODE_DESCRIPTIONS[ancestor.type] || ancestor.type, - }, - }); - } - } - - if (prohibitAssign === "always") { - return { - AssignmentExpression: testForConditionalAncestor, - }; - } - - return { - DoWhileStatement: testForAssign, - ForStatement: testForAssign, - IfStatement: testForAssign, - WhileStatement: testForAssign, - ConditionalExpression: testForAssign, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow assignment operators in conditional expressions", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-cond-assign" + }, + + schema: [ + { + enum: ["except-parens", "always"] + } + ], + + messages: { + unexpected: "Unexpected assignment within {{type}}.", + + // must match JSHint's error message + missing: "Expected a conditional expression and instead saw an assignment." + } + }, + + create(context) { + + const prohibitAssign = (context.options[0] || "except-parens"); + + const sourceCode = context.sourceCode; + + /** + * Check whether an AST node is the test expression for a conditional statement. + * @param {!Object} node The node to test. + * @returns {boolean} `true` if the node is the text expression for a conditional statement; otherwise, `false`. + */ + function isConditionalTestExpression(node) { + return node.parent && + TEST_CONDITION_PARENT_TYPES.has(node.parent.type) && + node === node.parent.test; + } + + /** + * Given an AST node, perform a bottom-up search for the first ancestor that represents a conditional statement. + * @param {!Object} node The node to use at the start of the search. + * @returns {?Object} The closest ancestor node that represents a conditional statement. + */ + function findConditionalAncestor(node) { + let currentAncestor = node; + + do { + if (isConditionalTestExpression(currentAncestor)) { + return currentAncestor.parent; + } + } while ((currentAncestor = currentAncestor.parent) && !astUtils.isFunction(currentAncestor)); + + return null; + } + + /** + * Check whether the code represented by an AST node is enclosed in two sets of parentheses. + * @param {!Object} node The node to test. + * @returns {boolean} `true` if the code is enclosed in two sets of parentheses; otherwise, `false`. + */ + function isParenthesisedTwice(node) { + const previousToken = sourceCode.getTokenBefore(node, 1), + nextToken = sourceCode.getTokenAfter(node, 1); + + return astUtils.isParenthesised(sourceCode, node) && + previousToken && astUtils.isOpeningParenToken(previousToken) && previousToken.range[1] <= node.range[0] && + astUtils.isClosingParenToken(nextToken) && nextToken.range[0] >= node.range[1]; + } + + /** + * Check a conditional statement's test expression for top-level assignments that are not enclosed in parentheses. + * @param {!Object} node The node for the conditional statement. + * @returns {void} + */ + function testForAssign(node) { + if (node.test && + (node.test.type === "AssignmentExpression") && + (node.type === "ForStatement" + ? !astUtils.isParenthesised(sourceCode, node.test) + : !isParenthesisedTwice(node.test) + ) + ) { + + context.report({ + node: node.test, + messageId: "missing" + }); + } + } + + /** + * Check whether an assignment expression is descended from a conditional statement's test expression. + * @param {!Object} node The node for the assignment expression. + * @returns {void} + */ + function testForConditionalAncestor(node) { + const ancestor = findConditionalAncestor(node); + + if (ancestor) { + context.report({ + node, + messageId: "unexpected", + data: { + type: NODE_DESCRIPTIONS[ancestor.type] || ancestor.type + } + }); + } + } + + if (prohibitAssign === "always") { + return { + AssignmentExpression: testForConditionalAncestor + }; + } + + return { + DoWhileStatement: testForAssign, + ForStatement: testForAssign, + IfStatement: testForAssign, + WhileStatement: testForAssign, + ConditionalExpression: testForAssign + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-confusing-arrow.js b/node_modules/eslint/lib/rules/no-confusing-arrow.js index 1b3e79828..6fef1870e 100644 --- a/node_modules/eslint/lib/rules/no-confusing-arrow.js +++ b/node_modules/eslint/lib/rules/no-confusing-arrow.js @@ -19,109 +19,74 @@ const astUtils = require("./utils/ast-utils.js"); * @returns {boolean} `true` if the node is a conditional expression. */ function isConditional(node) { - return node && node.type === "ConditionalExpression"; + return node && node.type === "ConditionalExpression"; } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "no-confusing-arrow", - url: "https://eslint.style/rules/no-confusing-arrow", - }, - }, - ], - }, - type: "suggestion", - - docs: { - description: - "Disallow arrow functions where they could be confused with comparisons", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-confusing-arrow", - }, - - fixable: "code", - - schema: [ - { - type: "object", - properties: { - allowParens: { type: "boolean", default: true }, - onlyOneSimpleParam: { type: "boolean", default: false }, - }, - additionalProperties: false, - }, - ], - - messages: { - confusing: - "Arrow function used ambiguously with a conditional expression.", - }, - }, - - create(context) { - const config = context.options[0] || {}; - const allowParens = config.allowParens || config.allowParens === void 0; - const onlyOneSimpleParam = config.onlyOneSimpleParam; - const sourceCode = context.sourceCode; - - /** - * Reports if an arrow function contains an ambiguous conditional. - * @param {ASTNode} node A node to check and report. - * @returns {void} - */ - function checkArrowFunc(node) { - const body = node.body; - - if ( - isConditional(body) && - !(allowParens && astUtils.isParenthesised(sourceCode, body)) && - !( - onlyOneSimpleParam && - !( - node.params.length === 1 && - node.params[0].type === "Identifier" - ) - ) - ) { - context.report({ - node, - messageId: "confusing", - fix(fixer) { - // if `allowParens` is not set to true don't bother wrapping in parens - return ( - allowParens && - fixer.replaceText( - node.body, - `(${sourceCode.getText(node.body)})`, - ) - ); - }, - }); - } - } - - return { - ArrowFunctionExpression: checkArrowFunc, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "suggestion", + + docs: { + description: "Disallow arrow functions where they could be confused with comparisons", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-confusing-arrow" + }, + + fixable: "code", + + schema: [{ + type: "object", + properties: { + allowParens: { type: "boolean", default: true }, + onlyOneSimpleParam: { type: "boolean", default: false } + }, + additionalProperties: false + }], + + messages: { + confusing: "Arrow function used ambiguously with a conditional expression." + } + }, + + create(context) { + const config = context.options[0] || {}; + const allowParens = config.allowParens || (config.allowParens === void 0); + const onlyOneSimpleParam = config.onlyOneSimpleParam; + const sourceCode = context.sourceCode; + + + /** + * Reports if an arrow function contains an ambiguous conditional. + * @param {ASTNode} node A node to check and report. + * @returns {void} + */ + function checkArrowFunc(node) { + const body = node.body; + + if (isConditional(body) && + !(allowParens && astUtils.isParenthesised(sourceCode, body)) && + !(onlyOneSimpleParam && !(node.params.length === 1 && node.params[0].type === "Identifier"))) { + context.report({ + node, + messageId: "confusing", + fix(fixer) { + + // if `allowParens` is not set to true don't bother wrapping in parens + return allowParens && fixer.replaceText(node.body, `(${sourceCode.getText(node.body)})`); + } + }); + } + } + + return { + ArrowFunctionExpression: checkArrowFunc + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-console.js b/node_modules/eslint/lib/rules/no-console.js index 1d734442e..d20477c5d 100644 --- a/node_modules/eslint/lib/rules/no-console.js +++ b/node_modules/eslint/lib/rules/no-console.js @@ -15,207 +15,193 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - defaultOptions: [{}], - - docs: { - description: "Disallow the use of `console`", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-console", - }, - - schema: [ - { - type: "object", - properties: { - allow: { - type: "array", - items: { - type: "string", - }, - minItems: 1, - uniqueItems: true, - }, - }, - additionalProperties: false, - }, - ], - - hasSuggestions: true, - - messages: { - unexpected: "Unexpected console statement.", - limited: - "Unexpected console statement. Only these console methods are allowed: {{ allowed }}.", - removeConsole: "Remove the console.{{ propertyName }}().", - removeMethodCall: "Remove the console method call.", - }, - }, - - create(context) { - const [{ allow: allowed = [] }] = context.options; - const sourceCode = context.sourceCode; - - /** - * Checks whether the given reference is 'console' or not. - * @param {eslint-scope.Reference} reference The reference to check. - * @returns {boolean} `true` if the reference is 'console'. - */ - function isConsole(reference) { - const id = reference.identifier; - - return id && id.name === "console"; - } - - /** - * Checks whether the property name of the given MemberExpression node - * is allowed by options or not. - * @param {ASTNode} node The MemberExpression node to check. - * @returns {boolean} `true` if the property name of the node is allowed. - */ - function isAllowed(node) { - const propertyName = astUtils.getStaticPropertyName(node); - - return propertyName && allowed.includes(propertyName); - } - - /** - * Checks whether the given reference is a member access which is not - * allowed by options or not. - * @param {eslint-scope.Reference} reference The reference to check. - * @returns {boolean} `true` if the reference is a member access which - * is not allowed by options. - */ - function isMemberAccessExceptAllowed(reference) { - const node = reference.identifier; - const parent = node.parent; - - return ( - parent.type === "MemberExpression" && - parent.object === node && - !isAllowed(parent) - ); - } - - /** - * Checks if removing the ExpressionStatement node will cause ASI to - * break. - * eg. - * foo() - * console.log(); - * [1, 2, 3].forEach(a => doSomething(a)) - * - * Removing the console.log(); statement should leave two statements, but - * here the two statements will become one because [ causes continuation after - * foo(). - * @param {ASTNode} node The ExpressionStatement node to check. - * @returns {boolean} `true` if ASI will break after removing the ExpressionStatement - * node. - */ - function maybeAsiHazard(node) { - const SAFE_TOKENS_BEFORE = /^[:;{]$/u; // One of :;{ - const UNSAFE_CHARS_AFTER = /^[-[(/+`]/u; // One of [(/+-` - - const tokenBefore = sourceCode.getTokenBefore(node); - const tokenAfter = sourceCode.getTokenAfter(node); - - return ( - Boolean(tokenAfter) && - UNSAFE_CHARS_AFTER.test(tokenAfter.value) && - tokenAfter.value !== "++" && - tokenAfter.value !== "--" && - Boolean(tokenBefore) && - !SAFE_TOKENS_BEFORE.test(tokenBefore.value) - ); - } - - /** - * Checks if the MemberExpression node's parent.parent.parent is a - * Program, BlockStatement, StaticBlock, or SwitchCase node. This check - * is necessary to avoid providing a suggestion that might cause a syntax error. - * - * eg. if (a) console.log(b), removing console.log() here will lead to a - * syntax error. - * if (a) { console.log(b) }, removing console.log() here is acceptable. - * - * Additionally, it checks if the callee of the CallExpression node is - * the node itself. - * - * eg. foo(console.log), cannot provide a suggestion here. - * @param {ASTNode} node The MemberExpression node to check. - * @returns {boolean} `true` if a suggestion can be provided for a node. - */ - function canProvideSuggestions(node) { - return ( - node.parent.type === "CallExpression" && - node.parent.callee === node && - node.parent.parent.type === "ExpressionStatement" && - astUtils.STATEMENT_LIST_PARENTS.has( - node.parent.parent.parent.type, - ) && - !maybeAsiHazard(node.parent.parent) - ); - } - - /** - * Reports the given reference as a violation. - * @param {eslint-scope.Reference} reference The reference to report. - * @returns {void} - */ - function report(reference) { - const node = reference.identifier.parent; - - const suggest = []; - - if (canProvideSuggestions(node)) { - const suggestion = { - fix(fixer) { - return fixer.remove(node.parent.parent); - }, - }; - - if (node.computed) { - suggestion.messageId = "removeMethodCall"; - } else { - suggestion.messageId = "removeConsole"; - suggestion.data = { propertyName: node.property.name }; - } - suggest.push(suggestion); - } - context.report({ - node, - loc: node.loc, - messageId: allowed.length ? "limited" : "unexpected", - data: { allowed: allowed.join(", ") }, - suggest, - }); - } - - return { - "Program:exit"(node) { - const scope = sourceCode.getScope(node); - const consoleVar = astUtils.getVariableByName(scope, "console"); - const shadowed = consoleVar && consoleVar.defs.length > 0; - - /* - * 'scope.through' includes all references to undefined - * variables. If the variable 'console' is not defined, it uses - * 'scope.through'. - */ - const references = consoleVar - ? consoleVar.references - : scope.through.filter(isConsole); - - if (!shadowed) { - references - .filter(isMemberAccessExceptAllowed) - .forEach(report); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow the use of `console`", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-console" + }, + + schema: [ + { + type: "object", + properties: { + allow: { + type: "array", + items: { + type: "string" + }, + minItems: 1, + uniqueItems: true + } + }, + additionalProperties: false + } + ], + + hasSuggestions: true, + + messages: { + unexpected: "Unexpected console statement.", + removeConsole: "Remove the console.{{ propertyName }}()." + } + }, + + create(context) { + const options = context.options[0] || {}; + const allowed = options.allow || []; + const sourceCode = context.sourceCode; + + /** + * Checks whether the given reference is 'console' or not. + * @param {eslint-scope.Reference} reference The reference to check. + * @returns {boolean} `true` if the reference is 'console'. + */ + function isConsole(reference) { + const id = reference.identifier; + + return id && id.name === "console"; + } + + /** + * Checks whether the property name of the given MemberExpression node + * is allowed by options or not. + * @param {ASTNode} node The MemberExpression node to check. + * @returns {boolean} `true` if the property name of the node is allowed. + */ + function isAllowed(node) { + const propertyName = astUtils.getStaticPropertyName(node); + + return propertyName && allowed.includes(propertyName); + } + + /** + * Checks whether the given reference is a member access which is not + * allowed by options or not. + * @param {eslint-scope.Reference} reference The reference to check. + * @returns {boolean} `true` if the reference is a member access which + * is not allowed by options. + */ + function isMemberAccessExceptAllowed(reference) { + const node = reference.identifier; + const parent = node.parent; + + return ( + parent.type === "MemberExpression" && + parent.object === node && + !isAllowed(parent) + ); + } + + /** + * Checks if removing the ExpressionStatement node will cause ASI to + * break. + * eg. + * foo() + * console.log(); + * [1, 2, 3].forEach(a => doSomething(a)) + * + * Removing the console.log(); statement should leave two statements, but + * here the two statements will become one because [ causes continuation after + * foo(). + * @param {ASTNode} node The ExpressionStatement node to check. + * @returns {boolean} `true` if ASI will break after removing the ExpressionStatement + * node. + */ + function maybeAsiHazard(node) { + const SAFE_TOKENS_BEFORE = /^[:;{]$/u; // One of :;{ + const UNSAFE_CHARS_AFTER = /^[-[(/+`]/u; // One of [(/+-` + + const tokenBefore = sourceCode.getTokenBefore(node); + const tokenAfter = sourceCode.getTokenAfter(node); + + return ( + Boolean(tokenAfter) && + UNSAFE_CHARS_AFTER.test(tokenAfter.value) && + tokenAfter.value !== "++" && + tokenAfter.value !== "--" && + Boolean(tokenBefore) && + !SAFE_TOKENS_BEFORE.test(tokenBefore.value) + ); + } + + /** + * Checks if the MemberExpression node's parent.parent.parent is a + * Program, BlockStatement, StaticBlock, or SwitchCase node. This check + * is necessary to avoid providing a suggestion that might cause a syntax error. + * + * eg. if (a) console.log(b), removing console.log() here will lead to a + * syntax error. + * if (a) { console.log(b) }, removing console.log() here is acceptable. + * + * Additionally, it checks if the callee of the CallExpression node is + * the node itself. + * + * eg. foo(console.log), cannot provide a suggestion here. + * @param {ASTNode} node The MemberExpression node to check. + * @returns {boolean} `true` if a suggestion can be provided for a node. + */ + function canProvideSuggestions(node) { + return ( + node.parent.type === "CallExpression" && + node.parent.callee === node && + node.parent.parent.type === "ExpressionStatement" && + astUtils.STATEMENT_LIST_PARENTS.has(node.parent.parent.parent.type) && + !maybeAsiHazard(node.parent.parent) + ); + } + + /** + * Reports the given reference as a violation. + * @param {eslint-scope.Reference} reference The reference to report. + * @returns {void} + */ + function report(reference) { + const node = reference.identifier.parent; + + const propertyName = astUtils.getStaticPropertyName(node); + + context.report({ + node, + loc: node.loc, + messageId: "unexpected", + suggest: canProvideSuggestions(node) + ? [{ + messageId: "removeConsole", + data: { propertyName }, + fix(fixer) { + return fixer.remove(node.parent.parent); + } + }] + : [] + }); + } + + return { + "Program:exit"(node) { + const scope = sourceCode.getScope(node); + const consoleVar = astUtils.getVariableByName(scope, "console"); + const shadowed = consoleVar && consoleVar.defs.length > 0; + + /* + * 'scope.through' includes all references to undefined + * variables. If the variable 'console' is not defined, it uses + * 'scope.through'. + */ + const references = consoleVar + ? consoleVar.references + : scope.through.filter(isConsole); + + if (!shadowed) { + references + .filter(isMemberAccessExceptAllowed) + .forEach(report); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-const-assign.js b/node_modules/eslint/lib/rules/no-const-assign.js index 5e126c80e..0ceaf7ea5 100644 --- a/node_modules/eslint/lib/rules/no-const-assign.js +++ b/node_modules/eslint/lib/rules/no-const-assign.js @@ -5,69 +5,52 @@ "use strict"; -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ - const astUtils = require("./utils/ast-utils"); -//------------------------------------------------------------------------------ -// Helpers -//------------------------------------------------------------------------------ - -const CONSTANT_BINDINGS = new Set(["const", "using", "await using"]); - //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - docs: { - description: - "Disallow reassigning `const`, `using`, and `await using` variables", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-const-assign", - }, - - schema: [], - - messages: { - const: "'{{name}}' is constant.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - /** - * Finds and reports references that are non initializer and writable. - * @param {Variable} variable A variable to check. - * @returns {void} - */ - function checkVariable(variable) { - astUtils - .getModifyingReferences(variable.references) - .forEach(reference => { - context.report({ - node: reference.identifier, - messageId: "const", - data: { name: reference.identifier.name }, - }); - }); - } - - return { - VariableDeclaration(node) { - if (CONSTANT_BINDINGS.has(node.kind)) { - sourceCode - .getDeclaredVariables(node) - .forEach(checkVariable); - } - }, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow reassigning `const` variables", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-const-assign" + }, + + schema: [], + + messages: { + const: "'{{name}}' is constant." + } + }, + + create(context) { + + const sourceCode = context.sourceCode; + + /** + * Finds and reports references that are non initializer and writable. + * @param {Variable} variable A variable to check. + * @returns {void} + */ + function checkVariable(variable) { + astUtils.getModifyingReferences(variable.references).forEach(reference => { + context.report({ node: reference.identifier, messageId: "const", data: { name: reference.identifier.name } }); + }); + } + + return { + VariableDeclaration(node) { + if (node.kind === "const") { + sourceCode.getDeclaredVariables(node).forEach(checkVariable); + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-constant-binary-expression.js b/node_modules/eslint/lib/rules/no-constant-binary-expression.js index e36063114..845255a0c 100644 --- a/node_modules/eslint/lib/rules/no-constant-binary-expression.js +++ b/node_modules/eslint/lib/rules/no-constant-binary-expression.js @@ -5,28 +5,10 @@ "use strict"; -const { - isNullLiteral, - isConstant, - isReferenceToGlobalVariable, - isLogicalAssignmentOperator, - ECMASCRIPT_GLOBALS, -} = require("./utils/ast-utils"); - -const NUMERIC_OR_STRING_BINARY_OPERATORS = new Set([ - "+", - "-", - "*", - "/", - "%", - "|", - "^", - "&", - "**", - "<<", - ">>", - ">>>", -]); +const globals = require("globals"); +const { isNullLiteral, isConstant, isReferenceToGlobalVariable, isLogicalAssignmentOperator } = require("./utils/ast-utils"); + +const NUMERIC_OR_STRING_BINARY_OPERATORS = new Set(["+", "-", "*", "/", "%", "|", "^", "&", "**", "<<", ">>", ">>>"]); //------------------------------------------------------------------------------ // Helpers @@ -42,13 +24,11 @@ const NUMERIC_OR_STRING_BINARY_OPERATORS = new Set([ * @public */ function isNullOrUndefined(scope, node) { - return ( - isNullLiteral(node) || - (node.type === "Identifier" && - node.name === "undefined" && - isReferenceToGlobalVariable(scope, node)) || - (node.type === "UnaryExpression" && node.operator === "void") - ); + return ( + isNullLiteral(node) || + (node.type === "Identifier" && node.name === "undefined" && isReferenceToGlobalVariable(scope, node)) || + (node.type === "UnaryExpression" && node.operator === "void") + ); } /** @@ -62,88 +42,79 @@ function isNullOrUndefined(scope, node) { * @returns {boolean} Does `node` have constant nullishness? */ function hasConstantNullishness(scope, node, nonNullish) { - if (nonNullish && isNullOrUndefined(scope, node)) { - return false; - } - - switch (node.type) { - case "ObjectExpression": // Objects are never nullish - case "ArrayExpression": // Arrays are never nullish - case "ArrowFunctionExpression": // Functions never nullish - case "FunctionExpression": // Functions are never nullish - case "ClassExpression": // Classes are never nullish - case "NewExpression": // Objects are never nullish - case "Literal": // Nullish, or non-nullish, literals never change - case "TemplateLiteral": // A string is never nullish - case "UpdateExpression": // Numbers are never nullish - case "BinaryExpression": // Numbers, strings, or booleans are never nullish - return true; - case "CallExpression": { - if (node.callee.type !== "Identifier") { - return false; - } - const functionName = node.callee.name; - - return ( - (functionName === "Boolean" || - functionName === "String" || - functionName === "Number") && - isReferenceToGlobalVariable(scope, node.callee) - ); - } - case "LogicalExpression": { - return ( - node.operator === "??" && - hasConstantNullishness(scope, node.right, true) - ); - } - case "AssignmentExpression": - if (node.operator === "=") { - return hasConstantNullishness(scope, node.right, nonNullish); - } - - /* - * Handling short-circuiting assignment operators would require - * walking the scope. We won't attempt that (for now...) / - */ - if (isLogicalAssignmentOperator(node.operator)) { - return false; - } - - /* - * The remaining assignment expressions all result in a numeric or - * string (non-nullish) value: - * "+=", "-=", "*=", "/=", "%=", "<<=", ">>=", ">>>=", "|=", "^=", "&=" - */ - - return true; - case "UnaryExpression": - /* - * "void" Always returns `undefined` - * "typeof" All types are strings, and thus non-nullish - * "!" Boolean is never nullish - * "delete" Returns a boolean, which is never nullish - * Math operators always return numbers or strings, neither of which - * are non-nullish "+", "-", "~" - */ - - return true; - case "SequenceExpression": { - const last = node.expressions.at(-1); - - return hasConstantNullishness(scope, last, nonNullish); - } - case "Identifier": - return ( - node.name === "undefined" && - isReferenceToGlobalVariable(scope, node) - ); - case "JSXElement": // ESLint has a policy of not assuming any specific JSX behavior. - case "JSXFragment": - return false; - default: - return false; - } + if (nonNullish && isNullOrUndefined(scope, node)) { + return false; + } + + switch (node.type) { + case "ObjectExpression": // Objects are never nullish + case "ArrayExpression": // Arrays are never nullish + case "ArrowFunctionExpression": // Functions never nullish + case "FunctionExpression": // Functions are never nullish + case "ClassExpression": // Classes are never nullish + case "NewExpression": // Objects are never nullish + case "Literal": // Nullish, or non-nullish, literals never change + case "TemplateLiteral": // A string is never nullish + case "UpdateExpression": // Numbers are never nullish + case "BinaryExpression": // Numbers, strings, or booleans are never nullish + return true; + case "CallExpression": { + if (node.callee.type !== "Identifier") { + return false; + } + const functionName = node.callee.name; + + return (functionName === "Boolean" || functionName === "String" || functionName === "Number") && + isReferenceToGlobalVariable(scope, node.callee); + } + case "LogicalExpression": { + return node.operator === "??" && hasConstantNullishness(scope, node.right, true); + } + case "AssignmentExpression": + if (node.operator === "=") { + return hasConstantNullishness(scope, node.right, nonNullish); + } + + /* + * Handling short-circuiting assignment operators would require + * walking the scope. We won't attempt that (for now...) / + */ + if (isLogicalAssignmentOperator(node.operator)) { + return false; + } + + /* + * The remaining assignment expressions all result in a numeric or + * string (non-nullish) value: + * "+=", "-=", "*=", "/=", "%=", "<<=", ">>=", ">>>=", "|=", "^=", "&=" + */ + + return true; + case "UnaryExpression": + + /* + * "void" Always returns `undefined` + * "typeof" All types are strings, and thus non-nullish + * "!" Boolean is never nullish + * "delete" Returns a boolean, which is never nullish + * Math operators always return numbers or strings, neither of which + * are non-nullish "+", "-", "~" + */ + + return true; + case "SequenceExpression": { + const last = node.expressions[node.expressions.length - 1]; + + return hasConstantNullishness(scope, last, nonNullish); + } + case "Identifier": + return node.name === "undefined" && isReferenceToGlobalVariable(scope, node); + case "JSXElement": // ESLint has a policy of not assuming any specific JSX behavior. + case "JSXFragment": + return false; + default: + return false; + } } /** @@ -157,149 +128,139 @@ function hasConstantNullishness(scope, node, nonNullish) { * @returns {boolean} Is `node` guaranteed to be a boolean? */ function isStaticBoolean(scope, node) { - switch (node.type) { - case "Literal": - return typeof node.value === "boolean"; - case "CallExpression": - return ( - node.callee.type === "Identifier" && - node.callee.name === "Boolean" && - isReferenceToGlobalVariable(scope, node.callee) && - (node.arguments.length === 0 || - isConstant(scope, node.arguments[0], true)) - ); - case "UnaryExpression": - return ( - node.operator === "!" && isConstant(scope, node.argument, true) - ); - default: - return false; - } + switch (node.type) { + case "Literal": + return typeof node.value === "boolean"; + case "CallExpression": + return node.callee.type === "Identifier" && node.callee.name === "Boolean" && + isReferenceToGlobalVariable(scope, node.callee) && + (node.arguments.length === 0 || isConstant(scope, node.arguments[0], true)); + case "UnaryExpression": + return node.operator === "!" && isConstant(scope, node.argument, true); + default: + return false; + } } + /** * Test if an AST node will always give the same result when compared to a * boolean value. Note that comparison to boolean values is different than * truthiness. * https://262.ecma-international.org/5.1/#sec-11.9.3 * - * JavaScript `==` operator works by converting the boolean to `1` (true) or + * Javascript `==` operator works by converting the boolean to `1` (true) or * `+0` (false) and then checks the values `==` equality to that number. * @param {Scope} scope The scope in which node was found. * @param {ASTNode} node The node to test. * @returns {boolean} Will `node` always coerce to the same boolean value? */ function hasConstantLooseBooleanComparison(scope, node) { - switch (node.type) { - case "ObjectExpression": - case "ClassExpression": - /** - * In theory objects like: - * - * `{toString: () => a}` - * `{valueOf: () => a}` - * - * Or a classes like: - * - * `class { static toString() { return a } }` - * `class { static valueOf() { return a } }` - * - * Are not constant verifiably when `inBooleanPosition` is - * false, but it's an edge case we've opted not to handle. - */ - return true; - case "ArrayExpression": { - const nonSpreadElements = node.elements.filter( - e => - // Elements can be `null` in sparse arrays: `[,,]`; - e !== null && e.type !== "SpreadElement", - ); - - /* - * Possible future direction if needed: We could check if the - * single value would result in variable boolean comparison. - * For now we will err on the side of caution since `[x]` could - * evaluate to `[0]` or `[1]`. - */ - return node.elements.length === 0 || nonSpreadElements.length > 1; - } - case "ArrowFunctionExpression": - case "FunctionExpression": - return true; - case "UnaryExpression": - if ( - node.operator === "void" || // Always returns `undefined` - node.operator === "typeof" // All `typeof` strings, when coerced to number, are not 0 or 1. - ) { - return true; - } - if (node.operator === "!") { - return isConstant(scope, node.argument, true); - } - - /* - * We won't try to reason about +, -, ~, or delete - * In theory, for the mathematical operators, we could look at the - * argument and try to determine if it coerces to a constant numeric - * value. - */ - return false; - case "NewExpression": // Objects might have custom `.valueOf` or `.toString`. - return false; - case "CallExpression": { - if ( - node.callee.type === "Identifier" && - node.callee.name === "Boolean" && - isReferenceToGlobalVariable(scope, node.callee) - ) { - return ( - node.arguments.length === 0 || - isConstant(scope, node.arguments[0], true) - ); - } - return false; - } - case "Literal": // True or false, literals never change - return true; - case "Identifier": - return ( - node.name === "undefined" && - isReferenceToGlobalVariable(scope, node) - ); - case "TemplateLiteral": - /* - * In theory we could try to check if the quasi are sufficient to - * prove that the expression will always be true, but it would be - * tricky to get right. For example: `000.${foo}000` - */ - return node.expressions.length === 0; - case "AssignmentExpression": - if (node.operator === "=") { - return hasConstantLooseBooleanComparison(scope, node.right); - } - - /* - * Handling short-circuiting assignment operators would require - * walking the scope. We won't attempt that (for now...) - * - * The remaining assignment expressions all result in a numeric or - * string (non-nullish) values which could be truthy or falsy: - * "+=", "-=", "*=", "/=", "%=", "<<=", ">>=", ">>>=", "|=", "^=", "&=" - */ - return false; - case "SequenceExpression": { - const last = node.expressions.at(-1); - - return hasConstantLooseBooleanComparison(scope, last); - } - case "JSXElement": // ESLint has a policy of not assuming any specific JSX behavior. - case "JSXFragment": - return false; - default: - return false; - } + switch (node.type) { + case "ObjectExpression": + case "ClassExpression": + + /** + * In theory objects like: + * + * `{toString: () => a}` + * `{valueOf: () => a}` + * + * Or a classes like: + * + * `class { static toString() { return a } }` + * `class { static valueOf() { return a } }` + * + * Are not constant verifiably when `inBooleanPosition` is + * false, but it's an edge case we've opted not to handle. + */ + return true; + case "ArrayExpression": { + const nonSpreadElements = node.elements.filter(e => + + // Elements can be `null` in sparse arrays: `[,,]`; + e !== null && e.type !== "SpreadElement"); + + + /* + * Possible future direction if needed: We could check if the + * single value would result in variable boolean comparison. + * For now we will err on the side of caution since `[x]` could + * evaluate to `[0]` or `[1]`. + */ + return node.elements.length === 0 || nonSpreadElements.length > 1; + } + case "ArrowFunctionExpression": + case "FunctionExpression": + return true; + case "UnaryExpression": + if (node.operator === "void" || // Always returns `undefined` + node.operator === "typeof" // All `typeof` strings, when coerced to number, are not 0 or 1. + ) { + return true; + } + if (node.operator === "!") { + return isConstant(scope, node.argument, true); + } + + /* + * We won't try to reason about +, -, ~, or delete + * In theory, for the mathematical operators, we could look at the + * argument and try to determine if it coerces to a constant numeric + * value. + */ + return false; + case "NewExpression": // Objects might have custom `.valueOf` or `.toString`. + return false; + case "CallExpression": { + if (node.callee.type === "Identifier" && + node.callee.name === "Boolean" && + isReferenceToGlobalVariable(scope, node.callee) + ) { + return node.arguments.length === 0 || isConstant(scope, node.arguments[0], true); + } + return false; + } + case "Literal": // True or false, literals never change + return true; + case "Identifier": + return node.name === "undefined" && isReferenceToGlobalVariable(scope, node); + case "TemplateLiteral": + + /* + * In theory we could try to check if the quasi are sufficient to + * prove that the expression will always be true, but it would be + * tricky to get right. For example: `000.${foo}000` + */ + return node.expressions.length === 0; + case "AssignmentExpression": + if (node.operator === "=") { + return hasConstantLooseBooleanComparison(scope, node.right); + } + + /* + * Handling short-circuiting assignment operators would require + * walking the scope. We won't attempt that (for now...) + * + * The remaining assignment expressions all result in a numeric or + * string (non-nullish) values which could be truthy or falsy: + * "+=", "-=", "*=", "/=", "%=", "<<=", ">>=", ">>>=", "|=", "^=", "&=" + */ + return false; + case "SequenceExpression": { + const last = node.expressions[node.expressions.length - 1]; + + return hasConstantLooseBooleanComparison(scope, last); + } + case "JSXElement": // ESLint has a policy of not assuming any specific JSX behavior. + case "JSXFragment": + return false; + default: + return false; + } } + /** * Test if an AST node will always give the same result when _strictly_ compared * to a boolean value. This can happen if the expression can never be boolean, or @@ -310,90 +271,82 @@ function hasConstantLooseBooleanComparison(scope, node) { * static boolean value? */ function hasConstantStrictBooleanComparison(scope, node) { - switch (node.type) { - case "ObjectExpression": // Objects are not booleans - case "ArrayExpression": // Arrays are not booleans - case "ArrowFunctionExpression": // Functions are not booleans - case "FunctionExpression": - case "ClassExpression": // Classes are not booleans - case "NewExpression": // Objects are not booleans - case "TemplateLiteral": // Strings are not booleans - case "Literal": // True, false, or not boolean, literals never change. - case "UpdateExpression": // Numbers are not booleans - return true; - case "BinaryExpression": - return NUMERIC_OR_STRING_BINARY_OPERATORS.has(node.operator); - case "UnaryExpression": { - if (node.operator === "delete") { - return false; - } - if (node.operator === "!") { - return isConstant(scope, node.argument, true); - } - - /* - * The remaining operators return either strings or numbers, neither - * of which are boolean. - */ - return true; - } - case "SequenceExpression": { - const last = node.expressions.at(-1); - - return hasConstantStrictBooleanComparison(scope, last); - } - case "Identifier": - return ( - node.name === "undefined" && - isReferenceToGlobalVariable(scope, node) - ); - case "AssignmentExpression": - if (node.operator === "=") { - return hasConstantStrictBooleanComparison(scope, node.right); - } - - /* - * Handling short-circuiting assignment operators would require - * walking the scope. We won't attempt that (for now...) - */ - if (isLogicalAssignmentOperator(node.operator)) { - return false; - } - - /* - * The remaining assignment expressions all result in either a number - * or a string, neither of which can ever be boolean. - */ - return true; - case "CallExpression": { - if (node.callee.type !== "Identifier") { - return false; - } - const functionName = node.callee.name; - - if ( - (functionName === "String" || functionName === "Number") && - isReferenceToGlobalVariable(scope, node.callee) - ) { - return true; - } - if ( - functionName === "Boolean" && - isReferenceToGlobalVariable(scope, node.callee) - ) { - return ( - node.arguments.length === 0 || - isConstant(scope, node.arguments[0], true) - ); - } - return false; - } - case "JSXElement": // ESLint has a policy of not assuming any specific JSX behavior. - case "JSXFragment": - return false; - default: - return false; - } + switch (node.type) { + case "ObjectExpression": // Objects are not booleans + case "ArrayExpression": // Arrays are not booleans + case "ArrowFunctionExpression": // Functions are not booleans + case "FunctionExpression": + case "ClassExpression": // Classes are not booleans + case "NewExpression": // Objects are not booleans + case "TemplateLiteral": // Strings are not booleans + case "Literal": // True, false, or not boolean, literals never change. + case "UpdateExpression": // Numbers are not booleans + return true; + case "BinaryExpression": + return NUMERIC_OR_STRING_BINARY_OPERATORS.has(node.operator); + case "UnaryExpression": { + if (node.operator === "delete") { + return false; + } + if (node.operator === "!") { + return isConstant(scope, node.argument, true); + } + + /* + * The remaining operators return either strings or numbers, neither + * of which are boolean. + */ + return true; + } + case "SequenceExpression": { + const last = node.expressions[node.expressions.length - 1]; + + return hasConstantStrictBooleanComparison(scope, last); + } + case "Identifier": + return node.name === "undefined" && isReferenceToGlobalVariable(scope, node); + case "AssignmentExpression": + if (node.operator === "=") { + return hasConstantStrictBooleanComparison(scope, node.right); + } + + /* + * Handling short-circuiting assignment operators would require + * walking the scope. We won't attempt that (for now...) + */ + if (isLogicalAssignmentOperator(node.operator)) { + return false; + } + + /* + * The remaining assignment expressions all result in either a number + * or a string, neither of which can ever be boolean. + */ + return true; + case "CallExpression": { + if (node.callee.type !== "Identifier") { + return false; + } + const functionName = node.callee.name; + + if ( + (functionName === "String" || functionName === "Number") && + isReferenceToGlobalVariable(scope, node.callee) + ) { + return true; + } + if (functionName === "Boolean" && isReferenceToGlobalVariable(scope, node.callee)) { + return ( + node.arguments.length === 0 || isConstant(scope, node.arguments[0], true)); + } + return false; + } + case "JSXElement": // ESLint has a policy of not assuming any specific JSX behavior. + case "JSXFragment": + return false; + default: + return false; + } } /** @@ -403,55 +356,51 @@ function hasConstantStrictBooleanComparison(scope, node) { * @returns {boolean} Will `node` always be new? */ function isAlwaysNew(scope, node) { - switch (node.type) { - case "ObjectExpression": - case "ArrayExpression": - case "ArrowFunctionExpression": - case "FunctionExpression": - case "ClassExpression": - return true; - case "NewExpression": { - if (node.callee.type !== "Identifier") { - return false; - } - - /* - * All the built-in constructors are always new, but - * user-defined constructors could return a sentinel - * object. - * - * Catching these is especially useful for primitive constructors - * which return boxed values, a surprising gotcha' in JavaScript. - */ - return ( - Object.hasOwn(ECMASCRIPT_GLOBALS, node.callee.name) && - isReferenceToGlobalVariable(scope, node.callee) - ); - } - case "Literal": - // Regular expressions are objects, and thus always new - return typeof node.regex === "object"; - case "SequenceExpression": { - const last = node.expressions.at(-1); - - return isAlwaysNew(scope, last); - } - case "AssignmentExpression": - if (node.operator === "=") { - return isAlwaysNew(scope, node.right); - } - return false; - case "ConditionalExpression": - return ( - isAlwaysNew(scope, node.consequent) && - isAlwaysNew(scope, node.alternate) - ); - case "JSXElement": // ESLint has a policy of not assuming any specific JSX behavior. - case "JSXFragment": - return false; - default: - return false; - } + switch (node.type) { + case "ObjectExpression": + case "ArrayExpression": + case "ArrowFunctionExpression": + case "FunctionExpression": + case "ClassExpression": + return true; + case "NewExpression": { + if (node.callee.type !== "Identifier") { + return false; + } + + /* + * All the built-in constructors are always new, but + * user-defined constructors could return a sentinel + * object. + * + * Catching these is especially useful for primitive constructors + * which return boxed values, a surprising gotcha' in JavaScript. + */ + return Object.hasOwnProperty.call(globals.builtin, node.callee.name) && + isReferenceToGlobalVariable(scope, node.callee); + } + case "Literal": + + // Regular expressions are objects, and thus always new + return typeof node.regex === "object"; + case "SequenceExpression": { + const last = node.expressions[node.expressions.length - 1]; + + return isAlwaysNew(scope, last); + } + case "AssignmentExpression": + if (node.operator === "=") { + return isAlwaysNew(scope, node.right); + } + return false; + case "ConditionalExpression": + return isAlwaysNew(scope, node.consequent) && isAlwaysNew(scope, node.alternate); + case "JSXElement": // ESLint has a policy of not assuming any specific JSX behavior. + case "JSXFragment": + return false; + default: + return false; + } } /** @@ -463,141 +412,98 @@ function isAlwaysNew(scope, node) { * @returns {ASTNode | null} The node which will cause the expression to have a constant result. */ function findBinaryExpressionConstantOperand(scope, a, b, operator) { - if (operator === "==" || operator === "!=") { - if ( - (isNullOrUndefined(scope, a) && - hasConstantNullishness(scope, b, false)) || - (isStaticBoolean(scope, a) && - hasConstantLooseBooleanComparison(scope, b)) - ) { - return b; - } - } else if (operator === "===" || operator === "!==") { - if ( - (isNullOrUndefined(scope, a) && - hasConstantNullishness(scope, b, false)) || - (isStaticBoolean(scope, a) && - hasConstantStrictBooleanComparison(scope, b)) - ) { - return b; - } - } - return null; + if (operator === "==" || operator === "!=") { + if ( + (isNullOrUndefined(scope, a) && hasConstantNullishness(scope, b, false)) || + (isStaticBoolean(scope, a) && hasConstantLooseBooleanComparison(scope, b)) + ) { + return b; + } + } else if (operator === "===" || operator === "!==") { + if ( + (isNullOrUndefined(scope, a) && hasConstantNullishness(scope, b, false)) || + (isStaticBoolean(scope, a) && hasConstantStrictBooleanComparison(scope, b)) + ) { + return b; + } + } + return null; } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - docs: { - description: - "Disallow expressions where the operation doesn't affect the value", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-constant-binary-expression", - }, - schema: [], - messages: { - constantBinaryOperand: - "Unexpected constant binary expression. Compares constantly with the {{otherSide}}-hand side of the `{{operator}}`.", - constantShortCircuit: - "Unexpected constant {{property}} on the left-hand side of a `{{operator}}` expression.", - alwaysNew: - "Unexpected comparison to newly constructed object. These two values can never be equal.", - bothAlwaysNew: - "Unexpected comparison of two newly constructed objects. These two values can never be equal.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - return { - LogicalExpression(node) { - const { operator, left } = node; - const scope = sourceCode.getScope(node); - - if ( - (operator === "&&" || operator === "||") && - isConstant(scope, left, true) - ) { - context.report({ - node: left, - messageId: "constantShortCircuit", - data: { property: "truthiness", operator }, - }); - } else if ( - operator === "??" && - hasConstantNullishness(scope, left, false) - ) { - context.report({ - node: left, - messageId: "constantShortCircuit", - data: { property: "nullishness", operator }, - }); - } - }, - BinaryExpression(node) { - const scope = sourceCode.getScope(node); - const { right, left, operator } = node; - const rightConstantOperand = - findBinaryExpressionConstantOperand( - scope, - left, - right, - operator, - ); - const leftConstantOperand = findBinaryExpressionConstantOperand( - scope, - right, - left, - operator, - ); - - if (rightConstantOperand) { - context.report({ - node: rightConstantOperand, - messageId: "constantBinaryOperand", - data: { operator, otherSide: "left" }, - }); - } else if (leftConstantOperand) { - context.report({ - node: leftConstantOperand, - messageId: "constantBinaryOperand", - data: { operator, otherSide: "right" }, - }); - } else if (operator === "===" || operator === "!==") { - if (isAlwaysNew(scope, left)) { - context.report({ node: left, messageId: "alwaysNew" }); - } else if (isAlwaysNew(scope, right)) { - context.report({ node: right, messageId: "alwaysNew" }); - } - } else if (operator === "==" || operator === "!=") { - /* - * If both sides are "new", then both sides are objects and - * therefore they will be compared by reference even with `==` - * equality. - */ - if (isAlwaysNew(scope, left) && isAlwaysNew(scope, right)) { - context.report({ - node: left, - messageId: "bothAlwaysNew", - }); - } - } - }, - - /* - * In theory we could handle short-circuiting assignment operators, - * for some constant values, but that would require walking the - * scope to find the value of the variable being assigned. This is - * dependent on https://github.com/eslint/eslint/issues/13776 - * - * AssignmentExpression() {}, - */ - }; - }, + meta: { + type: "problem", + docs: { + description: "Disallow expressions where the operation doesn't affect the value", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-constant-binary-expression" + }, + schema: [], + messages: { + constantBinaryOperand: "Unexpected constant binary expression. Compares constantly with the {{otherSide}}-hand side of the `{{operator}}`.", + constantShortCircuit: "Unexpected constant {{property}} on the left-hand side of a `{{operator}}` expression.", + alwaysNew: "Unexpected comparison to newly constructed object. These two values can never be equal.", + bothAlwaysNew: "Unexpected comparison of two newly constructed objects. These two values can never be equal." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + + return { + LogicalExpression(node) { + const { operator, left } = node; + const scope = sourceCode.getScope(node); + + if ((operator === "&&" || operator === "||") && isConstant(scope, left, true)) { + context.report({ node: left, messageId: "constantShortCircuit", data: { property: "truthiness", operator } }); + } else if (operator === "??" && hasConstantNullishness(scope, left, false)) { + context.report({ node: left, messageId: "constantShortCircuit", data: { property: "nullishness", operator } }); + } + }, + BinaryExpression(node) { + const scope = sourceCode.getScope(node); + const { right, left, operator } = node; + const rightConstantOperand = findBinaryExpressionConstantOperand(scope, left, right, operator); + const leftConstantOperand = findBinaryExpressionConstantOperand(scope, right, left, operator); + + if (rightConstantOperand) { + context.report({ node: rightConstantOperand, messageId: "constantBinaryOperand", data: { operator, otherSide: "left" } }); + } else if (leftConstantOperand) { + context.report({ node: leftConstantOperand, messageId: "constantBinaryOperand", data: { operator, otherSide: "right" } }); + } else if (operator === "===" || operator === "!==") { + if (isAlwaysNew(scope, left)) { + context.report({ node: left, messageId: "alwaysNew" }); + } else if (isAlwaysNew(scope, right)) { + context.report({ node: right, messageId: "alwaysNew" }); + } + } else if (operator === "==" || operator === "!=") { + + /* + * If both sides are "new", then both sides are objects and + * therefore they will be compared by reference even with `==` + * equality. + */ + if (isAlwaysNew(scope, left) && isAlwaysNew(scope, right)) { + context.report({ node: left, messageId: "bothAlwaysNew" }); + } + } + + } + + /* + * In theory we could handle short-circuiting assignment operators, + * for some constant values, but that would require walking the + * scope to find the value of the variable being assigned. This is + * dependant on https://github.com/eslint/eslint/issues/13776 + * + * AssignmentExpression() {}, + */ + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-constant-condition.js b/node_modules/eslint/lib/rules/no-constant-condition.js index 4ecd5a3d2..24abe3632 100644 --- a/node_modules/eslint/lib/rules/no-constant-condition.js +++ b/node_modules/eslint/lib/rules/no-constant-condition.js @@ -15,163 +15,136 @@ const { isConstant } = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - defaultOptions: [{ checkLoops: "allExceptWhileTrue" }], - - docs: { - description: "Disallow constant expressions in conditions", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-constant-condition", - }, - - schema: [ - { - type: "object", - properties: { - checkLoops: { - enum: [ - "all", - "allExceptWhileTrue", - "none", - true, - false, - ], - }, - }, - additionalProperties: false, - }, - ], - - messages: { - unexpected: "Unexpected constant condition.", - }, - }, - - create(context) { - const loopSetStack = []; - const sourceCode = context.sourceCode; - let [{ checkLoops }] = context.options; - - if (checkLoops === true) { - checkLoops = "all"; - } else if (checkLoops === false) { - checkLoops = "none"; - } - - let loopsInCurrentScope = new Set(); - - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - /** - * Tracks when the given node contains a constant condition. - * @param {ASTNode} node The AST node to check. - * @returns {void} - * @private - */ - function trackConstantConditionLoop(node) { - if ( - node.test && - isConstant(sourceCode.getScope(node), node.test, true) - ) { - loopsInCurrentScope.add(node); - } - } - - /** - * Reports when the set contains the given constant condition node - * @param {ASTNode} node The AST node to check. - * @returns {void} - * @private - */ - function checkConstantConditionLoopInSet(node) { - if (loopsInCurrentScope.has(node)) { - loopsInCurrentScope.delete(node); - context.report({ node: node.test, messageId: "unexpected" }); - } - } - - /** - * Reports when the given node contains a constant condition. - * @param {ASTNode} node The AST node to check. - * @returns {void} - * @private - */ - function reportIfConstant(node) { - if ( - node.test && - isConstant(sourceCode.getScope(node), node.test, true) - ) { - context.report({ node: node.test, messageId: "unexpected" }); - } - } - - /** - * Stores current set of constant loops in loopSetStack temporarily - * and uses a new set to track constant loops - * @returns {void} - * @private - */ - function enterFunction() { - loopSetStack.push(loopsInCurrentScope); - loopsInCurrentScope = new Set(); - } - - /** - * Reports when the set still contains stored constant conditions - * @returns {void} - * @private - */ - function exitFunction() { - loopsInCurrentScope = loopSetStack.pop(); - } - - /** - * Checks node when checkLoops option is enabled - * @param {ASTNode} node The AST node to check. - * @returns {void} - * @private - */ - function checkLoop(node) { - if (checkLoops === "all" || checkLoops === "allExceptWhileTrue") { - trackConstantConditionLoop(node); - } - } - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - return { - ConditionalExpression: reportIfConstant, - IfStatement: reportIfConstant, - WhileStatement(node) { - if ( - node.test.type === "Literal" && - node.test.value === true && - checkLoops === "allExceptWhileTrue" - ) { - return; - } - - checkLoop(node); - }, - "WhileStatement:exit": checkConstantConditionLoopInSet, - DoWhileStatement: checkLoop, - "DoWhileStatement:exit": checkConstantConditionLoopInSet, - ForStatement: checkLoop, - "ForStatement > .test": node => checkLoop(node.parent), - "ForStatement:exit": checkConstantConditionLoopInSet, - FunctionDeclaration: enterFunction, - "FunctionDeclaration:exit": exitFunction, - FunctionExpression: enterFunction, - "FunctionExpression:exit": exitFunction, - YieldExpression: () => loopsInCurrentScope.clear(), - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow constant expressions in conditions", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-constant-condition" + }, + + schema: [ + { + type: "object", + properties: { + checkLoops: { + type: "boolean", + default: true + } + }, + additionalProperties: false + } + ], + + messages: { + unexpected: "Unexpected constant condition." + } + }, + + create(context) { + const options = context.options[0] || {}, + checkLoops = options.checkLoops !== false, + loopSetStack = []; + const sourceCode = context.sourceCode; + + let loopsInCurrentScope = new Set(); + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + /** + * Tracks when the given node contains a constant condition. + * @param {ASTNode} node The AST node to check. + * @returns {void} + * @private + */ + function trackConstantConditionLoop(node) { + if (node.test && isConstant(sourceCode.getScope(node), node.test, true)) { + loopsInCurrentScope.add(node); + } + } + + /** + * Reports when the set contains the given constant condition node + * @param {ASTNode} node The AST node to check. + * @returns {void} + * @private + */ + function checkConstantConditionLoopInSet(node) { + if (loopsInCurrentScope.has(node)) { + loopsInCurrentScope.delete(node); + context.report({ node: node.test, messageId: "unexpected" }); + } + } + + /** + * Reports when the given node contains a constant condition. + * @param {ASTNode} node The AST node to check. + * @returns {void} + * @private + */ + function reportIfConstant(node) { + if (node.test && isConstant(sourceCode.getScope(node), node.test, true)) { + context.report({ node: node.test, messageId: "unexpected" }); + } + } + + /** + * Stores current set of constant loops in loopSetStack temporarily + * and uses a new set to track constant loops + * @returns {void} + * @private + */ + function enterFunction() { + loopSetStack.push(loopsInCurrentScope); + loopsInCurrentScope = new Set(); + } + + /** + * Reports when the set still contains stored constant conditions + * @returns {void} + * @private + */ + function exitFunction() { + loopsInCurrentScope = loopSetStack.pop(); + } + + /** + * Checks node when checkLoops option is enabled + * @param {ASTNode} node The AST node to check. + * @returns {void} + * @private + */ + function checkLoop(node) { + if (checkLoops) { + trackConstantConditionLoop(node); + } + } + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + return { + ConditionalExpression: reportIfConstant, + IfStatement: reportIfConstant, + WhileStatement: checkLoop, + "WhileStatement:exit": checkConstantConditionLoopInSet, + DoWhileStatement: checkLoop, + "DoWhileStatement:exit": checkConstantConditionLoopInSet, + ForStatement: checkLoop, + "ForStatement > .test": node => checkLoop(node.parent), + "ForStatement:exit": checkConstantConditionLoopInSet, + FunctionDeclaration: enterFunction, + "FunctionDeclaration:exit": exitFunction, + FunctionExpression: enterFunction, + "FunctionExpression:exit": exitFunction, + YieldExpression: () => loopsInCurrentScope.clear() + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-constructor-return.js b/node_modules/eslint/lib/rules/no-constructor-return.js index ed9e03db4..d7d98939b 100644 --- a/node_modules/eslint/lib/rules/no-constructor-return.js +++ b/node_modules/eslint/lib/rules/no-constructor-return.js @@ -9,54 +9,54 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - docs: { - description: "Disallow returning value from constructor", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-constructor-return", - }, - - schema: [], - - fixable: null, - - messages: { - unexpected: "Unexpected return statement in constructor.", - }, - }, - - create(context) { - const stack = []; - - return { - onCodePathStart(_, node) { - stack.push(node); - }, - onCodePathEnd() { - stack.pop(); - }, - ReturnStatement(node) { - const last = stack.at(-1); - - if (!last.parent) { - return; - } - - if ( - last.parent.type === "MethodDefinition" && - last.parent.kind === "constructor" && - node.argument - ) { - context.report({ - node, - messageId: "unexpected", - }); - } - }, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow returning value from constructor", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-constructor-return" + }, + + schema: {}, + + fixable: null, + + messages: { + unexpected: "Unexpected return statement in constructor." + } + }, + + create(context) { + const stack = []; + + return { + onCodePathStart(_, node) { + stack.push(node); + }, + onCodePathEnd() { + stack.pop(); + }, + ReturnStatement(node) { + const last = stack[stack.length - 1]; + + if (!last.parent) { + return; + } + + if ( + last.parent.type === "MethodDefinition" && + last.parent.kind === "constructor" && + (node.parent.parent === last || node.argument) + ) { + context.report({ + node, + messageId: "unexpected" + }); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-continue.js b/node_modules/eslint/lib/rules/no-continue.js index 6671c619b..f6e484b2f 100644 --- a/node_modules/eslint/lib/rules/no-continue.js +++ b/node_modules/eslint/lib/rules/no-continue.js @@ -9,30 +9,31 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: "Disallow `continue` statements", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/no-continue", - }, - - schema: [], - - messages: { - unexpected: "Unexpected use of continue statement.", - }, - }, - - create(context) { - return { - ContinueStatement(node) { - context.report({ node, messageId: "unexpected" }); - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow `continue` statements", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-continue" + }, + + schema: [], + + messages: { + unexpected: "Unexpected use of continue statement." + } + }, + + create(context) { + + return { + ContinueStatement(node) { + context.report({ node, messageId: "unexpected" }); + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-control-regex.js b/node_modules/eslint/lib/rules/no-control-regex.js index a0221b4b4..dc412fcab 100644 --- a/node_modules/eslint/lib/rules/no-control-regex.js +++ b/node_modules/eslint/lib/rules/no-control-regex.js @@ -7,136 +7,132 @@ const RegExpValidator = require("@eslint-community/regexpp").RegExpValidator; const collector = new (class { - constructor() { - this._source = ""; - this._controlChars = []; - this._validator = new RegExpValidator(this); - } - - onPatternEnter() { - /* - * `RegExpValidator` may parse the pattern twice in one `validatePattern`. - * So `this._controlChars` should be cleared here as well. - * - * For example, the `/(?\x1f)/` regex will parse the pattern twice. - * This is based on the content described in Annex B. - * If the regex contains a `GroupName` and the `u` flag is not used, `ParseText` will be called twice. - * See https://tc39.es/ecma262/2023/multipage/additional-ecmascript-features-for-web-browsers.html#sec-parsepattern-annexb - */ - this._controlChars = []; - } - - onCharacter(start, end, cp) { - if ( - cp >= 0x00 && - cp <= 0x1f && - (this._source.codePointAt(start) === cp || - this._source.slice(start, end).startsWith("\\x") || - this._source.slice(start, end).startsWith("\\u")) - ) { - this._controlChars.push(`\\x${`0${cp.toString(16)}`.slice(-2)}`); - } - } - - collectControlChars(regexpStr, flags) { - const uFlag = typeof flags === "string" && flags.includes("u"); - const vFlag = typeof flags === "string" && flags.includes("v"); - - this._controlChars = []; - this._source = regexpStr; - - try { - this._validator.validatePattern(regexpStr, void 0, void 0, { - unicode: uFlag, - unicodeSets: vFlag, - }); // Call onCharacter hook - } catch { - // Ignore syntax errors in RegExp. - } - return this._controlChars; - } + constructor() { + this._source = ""; + this._controlChars = []; + this._validator = new RegExpValidator(this); + } + + onPatternEnter() { + + /* + * `RegExpValidator` may parse the pattern twice in one `validatePattern`. + * So `this._controlChars` should be cleared here as well. + * + * For example, the `/(?\x1f)/` regex will parse the pattern twice. + * This is based on the content described in Annex B. + * If the regex contains a `GroupName` and the `u` flag is not used, `ParseText` will be called twice. + * See https://tc39.es/ecma262/2023/multipage/additional-ecmascript-features-for-web-browsers.html#sec-parsepattern-annexb + */ + this._controlChars = []; + } + + onCharacter(start, end, cp) { + if (cp >= 0x00 && + cp <= 0x1F && + ( + this._source.codePointAt(start) === cp || + this._source.slice(start, end).startsWith("\\x") || + this._source.slice(start, end).startsWith("\\u") + ) + ) { + this._controlChars.push(`\\x${`0${cp.toString(16)}`.slice(-2)}`); + } + } + + collectControlChars(regexpStr, flags) { + const uFlag = typeof flags === "string" && flags.includes("u"); + const vFlag = typeof flags === "string" && flags.includes("v"); + + this._controlChars = []; + this._source = regexpStr; + + try { + this._validator.validatePattern(regexpStr, void 0, void 0, { unicode: uFlag, unicodeSets: vFlag }); // Call onCharacter hook + } catch { + + // Ignore syntax errors in RegExp. + } + return this._controlChars; + } })(); //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - docs: { - description: "Disallow control characters in regular expressions", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-control-regex", - }, - - schema: [], - - messages: { - unexpected: - "Unexpected control character(s) in regular expression: {{controlChars}}.", - }, - }, - - create(context) { - /** - * Get the regex expression - * @param {ASTNode} node `Literal` node to evaluate - * @returns {{ pattern: string, flags: string | null } | null} Regex if found (the given node is either a regex literal - * or a string literal that is the pattern argument of a RegExp constructor call). Otherwise `null`. If flags cannot be determined, - * the `flags` property will be `null`. - * @private - */ - function getRegExp(node) { - if (node.regex) { - return node.regex; - } - if ( - typeof node.value === "string" && - (node.parent.type === "NewExpression" || - node.parent.type === "CallExpression") && - node.parent.callee.type === "Identifier" && - node.parent.callee.name === "RegExp" && - node.parent.arguments[0] === node - ) { - const pattern = node.value; - const flags = - node.parent.arguments.length > 1 && - node.parent.arguments[1].type === "Literal" && - typeof node.parent.arguments[1].value === "string" - ? node.parent.arguments[1].value - : null; - - return { pattern, flags }; - } - - return null; - } - - return { - Literal(node) { - const regExp = getRegExp(node); - - if (regExp) { - const { pattern, flags } = regExp; - const controlCharacters = collector.collectControlChars( - pattern, - flags, - ); - - if (controlCharacters.length > 0) { - context.report({ - node, - messageId: "unexpected", - data: { - controlChars: controlCharacters.join(", "), - }, - }); - } - } - }, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow control characters in regular expressions", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-control-regex" + }, + + schema: [], + + messages: { + unexpected: "Unexpected control character(s) in regular expression: {{controlChars}}." + } + }, + + create(context) { + + /** + * Get the regex expression + * @param {ASTNode} node `Literal` node to evaluate + * @returns {{ pattern: string, flags: string | null } | null} Regex if found (the given node is either a regex literal + * or a string literal that is the pattern argument of a RegExp constructor call). Otherwise `null`. If flags cannot be determined, + * the `flags` property will be `null`. + * @private + */ + function getRegExp(node) { + if (node.regex) { + return node.regex; + } + if (typeof node.value === "string" && + (node.parent.type === "NewExpression" || node.parent.type === "CallExpression") && + node.parent.callee.type === "Identifier" && + node.parent.callee.name === "RegExp" && + node.parent.arguments[0] === node + ) { + const pattern = node.value; + const flags = + node.parent.arguments.length > 1 && + node.parent.arguments[1].type === "Literal" && + typeof node.parent.arguments[1].value === "string" + ? node.parent.arguments[1].value + : null; + + return { pattern, flags }; + } + + return null; + } + + return { + Literal(node) { + const regExp = getRegExp(node); + + if (regExp) { + const { pattern, flags } = regExp; + const controlCharacters = collector.collectControlChars(pattern, flags); + + if (controlCharacters.length > 0) { + context.report({ + node, + messageId: "unexpected", + data: { + controlChars: controlCharacters.join(", ") + } + }); + } + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-debugger.js b/node_modules/eslint/lib/rules/no-debugger.js index 66bc1be1e..f69843515 100644 --- a/node_modules/eslint/lib/rules/no-debugger.js +++ b/node_modules/eslint/lib/rules/no-debugger.js @@ -9,33 +9,35 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - docs: { - description: "Disallow the use of `debugger`", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-debugger", - }, - - fixable: null, - schema: [], - - messages: { - unexpected: "Unexpected 'debugger' statement.", - }, - }, - - create(context) { - return { - DebuggerStatement(node) { - context.report({ - node, - messageId: "unexpected", - }); - }, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow the use of `debugger`", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-debugger" + }, + + fixable: null, + schema: [], + + messages: { + unexpected: "Unexpected 'debugger' statement." + } + }, + + create(context) { + + return { + DebuggerStatement(node) { + context.report({ + node, + messageId: "unexpected" + }); + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-delete-var.js b/node_modules/eslint/lib/rules/no-delete-var.js index 7dfe002af..126603c83 100644 --- a/node_modules/eslint/lib/rules/no-delete-var.js +++ b/node_modules/eslint/lib/rules/no-delete-var.js @@ -9,34 +9,34 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: "Disallow deleting variables", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-delete-var", - }, - - schema: [], - - messages: { - unexpected: "Variables should not be deleted.", - }, - }, - - create(context) { - return { - UnaryExpression(node) { - if ( - node.operator === "delete" && - node.argument.type === "Identifier" - ) { - context.report({ node, messageId: "unexpected" }); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow deleting variables", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-delete-var" + }, + + schema: [], + + messages: { + unexpected: "Variables should not be deleted." + } + }, + + create(context) { + + return { + + UnaryExpression(node) { + if (node.operator === "delete" && node.argument.type === "Identifier") { + context.report({ node, messageId: "unexpected" }); + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-div-regex.js b/node_modules/eslint/lib/rules/no-div-regex.js index 69845bbda..208f840be 100644 --- a/node_modules/eslint/lib/rules/no-div-regex.js +++ b/node_modules/eslint/lib/rules/no-div-regex.js @@ -9,52 +9,45 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: - "Disallow equal signs explicitly at the beginning of regular expressions", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/no-div-regex", - }, - - fixable: "code", - - schema: [], - - messages: { - unexpected: - "A regular expression literal can be confused with '/='.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - return { - Literal(node) { - const token = sourceCode.getFirstToken(node); - - if ( - token.type === "RegularExpression" && - token.value[1] === "=" - ) { - context.report({ - node, - messageId: "unexpected", - fix(fixer) { - return fixer.replaceTextRange( - [token.range[0] + 1, token.range[0] + 2], - "[=]", - ); - }, - }); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow equal signs explicitly at the beginning of regular expressions", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-div-regex" + }, + + fixable: "code", + + schema: [], + + messages: { + unexpected: "A regular expression literal can be confused with '/='." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + + return { + + Literal(node) { + const token = sourceCode.getFirstToken(node); + + if (token.type === "RegularExpression" && token.value[1] === "=") { + context.report({ + node, + messageId: "unexpected", + fix(fixer) { + return fixer.replaceTextRange([token.range[0] + 1, token.range[0] + 2], "[=]"); + } + }); + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-dupe-args.js b/node_modules/eslint/lib/rules/no-dupe-args.js index 788b98d8f..c04ede5af 100644 --- a/node_modules/eslint/lib/rules/no-dupe-args.js +++ b/node_modules/eslint/lib/rules/no-dupe-args.js @@ -5,88 +5,78 @@ "use strict"; -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ - -const astUtils = require("./utils/ast-utils"); - //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - docs: { - description: - "Disallow duplicate arguments in `function` definitions", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-dupe-args", - }, - - schema: [], - - messages: { - unexpected: "Duplicate param '{{name}}'.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - /** - * Checks whether or not a given definition is a parameter's. - * @param {eslint-scope.DefEntry} def A definition to check. - * @returns {boolean} `true` if the definition is a parameter's. - */ - function isParameter(def) { - return def.type === "Parameter"; - } - - /** - * Determines if a given node has duplicate parameters. - * @param {ASTNode} node The node to check. - * @returns {void} - * @private - */ - function checkParams(node) { - const variables = sourceCode.getDeclaredVariables(node); - - for (let i = 0; i < variables.length; ++i) { - const variable = variables[i]; - - // Checks and reports duplications. - const defs = variable.defs.filter(isParameter); - const loc = { - start: astUtils.getOpeningParenOfParams(node, sourceCode) - .loc.start, - end: sourceCode.getTokenBefore(node.body).loc.end, - }; - - if (defs.length >= 2) { - context.report({ - loc, - messageId: "unexpected", - data: { name: variable.name }, - }); - } - } - } - - //-------------------------------------------------------------------------- - // Public API - //-------------------------------------------------------------------------- - - return { - FunctionDeclaration: checkParams, - FunctionExpression: checkParams, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow duplicate arguments in `function` definitions", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-dupe-args" + }, + + schema: [], + + messages: { + unexpected: "Duplicate param '{{name}}'." + } + }, + + create(context) { + + const sourceCode = context.sourceCode; + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + /** + * Checks whether or not a given definition is a parameter's. + * @param {eslint-scope.DefEntry} def A definition to check. + * @returns {boolean} `true` if the definition is a parameter's. + */ + function isParameter(def) { + return def.type === "Parameter"; + } + + /** + * Determines if a given node has duplicate parameters. + * @param {ASTNode} node The node to check. + * @returns {void} + * @private + */ + function checkParams(node) { + const variables = sourceCode.getDeclaredVariables(node); + + for (let i = 0; i < variables.length; ++i) { + const variable = variables[i]; + + // Checks and reports duplications. + const defs = variable.defs.filter(isParameter); + + if (defs.length >= 2) { + context.report({ + node, + messageId: "unexpected", + data: { name: variable.name } + }); + } + } + } + + //-------------------------------------------------------------------------- + // Public API + //-------------------------------------------------------------------------- + + return { + FunctionDeclaration: checkParams, + FunctionExpression: checkParams + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-dupe-class-members.js b/node_modules/eslint/lib/rules/no-dupe-class-members.js index 3b9292b78..2a7a9e810 100644 --- a/node_modules/eslint/lib/rules/no-dupe-class-members.js +++ b/node_modules/eslint/lib/rules/no-dupe-class-members.js @@ -11,107 +11,94 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - dialects: ["javascript", "typescript"], - language: "javascript", - - docs: { - description: "Disallow duplicate class members", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-dupe-class-members", - }, - - schema: [], - - messages: { - unexpected: "Duplicate name '{{name}}'.", - }, - }, - - create(context) { - let stack = []; - - /** - * Gets state of a given member name. - * @param {string} name A name of a member. - * @param {boolean} isStatic A flag which specifies that is a static member. - * @returns {Object} A state of a given member name. - * - retv.init {boolean} A flag which shows the name is declared as normal member. - * - retv.get {boolean} A flag which shows the name is declared as getter. - * - retv.set {boolean} A flag which shows the name is declared as setter. - */ - function getState(name, isStatic) { - const stateMap = stack.at(-1); - const key = `$${name}`; // to avoid "__proto__". - - if (!stateMap[key]) { - stateMap[key] = { - nonStatic: { init: false, get: false, set: false }, - static: { init: false, get: false, set: false }, - }; - } - - return stateMap[key][isStatic ? "static" : "nonStatic"]; - } - - return { - // Initializes the stack of state of member declarations. - Program() { - stack = []; - }, - - // Initializes state of member declarations for the class. - ClassBody() { - stack.push(Object.create(null)); - }, - - // Disposes the state for the class. - "ClassBody:exit"() { - stack.pop(); - }, - - // Reports the node if its name has been declared already. - "MethodDefinition, PropertyDefinition"(node) { - if ( - node.value && - node.value.type === "TSEmptyBodyFunctionExpression" - ) { - return; - } - - const name = astUtils.getStaticPropertyName(node); - const kind = - node.type === "MethodDefinition" ? node.kind : "field"; - - if (name === null || kind === "constructor") { - return; - } - - const state = getState(name, node.static); - let isDuplicate; - - if (kind === "get") { - isDuplicate = state.init || state.get; - state.get = true; - } else if (kind === "set") { - isDuplicate = state.init || state.set; - state.set = true; - } else { - isDuplicate = state.init || state.get || state.set; - state.init = true; - } - - if (isDuplicate) { - context.report({ - loc: node.key.loc, - messageId: "unexpected", - data: { name }, - }); - } - }, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow duplicate class members", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-dupe-class-members" + }, + + schema: [], + + messages: { + unexpected: "Duplicate name '{{name}}'." + } + }, + + create(context) { + let stack = []; + + /** + * Gets state of a given member name. + * @param {string} name A name of a member. + * @param {boolean} isStatic A flag which specifies that is a static member. + * @returns {Object} A state of a given member name. + * - retv.init {boolean} A flag which shows the name is declared as normal member. + * - retv.get {boolean} A flag which shows the name is declared as getter. + * - retv.set {boolean} A flag which shows the name is declared as setter. + */ + function getState(name, isStatic) { + const stateMap = stack[stack.length - 1]; + const key = `$${name}`; // to avoid "__proto__". + + if (!stateMap[key]) { + stateMap[key] = { + nonStatic: { init: false, get: false, set: false }, + static: { init: false, get: false, set: false } + }; + } + + return stateMap[key][isStatic ? "static" : "nonStatic"]; + } + + return { + + // Initializes the stack of state of member declarations. + Program() { + stack = []; + }, + + // Initializes state of member declarations for the class. + ClassBody() { + stack.push(Object.create(null)); + }, + + // Disposes the state for the class. + "ClassBody:exit"() { + stack.pop(); + }, + + // Reports the node if its name has been declared already. + "MethodDefinition, PropertyDefinition"(node) { + const name = astUtils.getStaticPropertyName(node); + const kind = node.type === "MethodDefinition" ? node.kind : "field"; + + if (name === null || kind === "constructor") { + return; + } + + const state = getState(name, node.static); + let isDuplicate = false; + + if (kind === "get") { + isDuplicate = (state.init || state.get); + state.get = true; + } else if (kind === "set") { + isDuplicate = (state.init || state.set); + state.set = true; + } else { + isDuplicate = (state.init || state.get || state.set); + state.init = true; + } + + if (isDuplicate) { + context.report({ node, messageId: "unexpected", data: { name } }); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-dupe-else-if.js b/node_modules/eslint/lib/rules/no-dupe-else-if.js index 7c4195819..60f436d1d 100644 --- a/node_modules/eslint/lib/rules/no-dupe-else-if.js +++ b/node_modules/eslint/lib/rules/no-dupe-else-if.js @@ -23,7 +23,7 @@ const astUtils = require("./utils/ast-utils"); * @returns {boolean} `true` if the array `arrA` is a subset of the array `arrB`. */ function isSubsetByComparator(comparator, arrA, arrB) { - return arrA.every(a => arrB.some(b => comparator(a, b))); + return arrA.every(a => arrB.some(b => comparator(a, b))); } /** @@ -33,13 +33,10 @@ function isSubsetByComparator(comparator, arrA, arrB) { * @returns {ASTNode[]} Array of conditions that makes the node when joined by the operator. */ function splitByLogicalOperator(operator, node) { - if (node.type === "LogicalExpression" && node.operator === operator) { - return [ - ...splitByLogicalOperator(operator, node.left), - ...splitByLogicalOperator(operator, node.right), - ]; - } - return [node]; + if (node.type === "LogicalExpression" && node.operator === operator) { + return [...splitByLogicalOperator(operator, node.left), ...splitByLogicalOperator(operator, node.right)]; + } + return [node]; } const splitByOr = splitByLogicalOperator.bind(null, "||"); @@ -49,97 +46,77 @@ const splitByAnd = splitByLogicalOperator.bind(null, "&&"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - docs: { - description: "Disallow duplicate conditions in if-else-if chains", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-dupe-else-if", - }, - - schema: [], - - messages: { - unexpected: - "This branch can never execute. Its condition is a duplicate or covered by previous conditions in the if-else-if chain.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - /** - * Determines whether the two given nodes are considered to be equal. In particular, given that the nodes - * represent expressions in a boolean context, `||` and `&&` can be considered as commutative operators. - * @param {ASTNode} a First node. - * @param {ASTNode} b Second node. - * @returns {boolean} `true` if the nodes are considered to be equal. - */ - function equal(a, b) { - if (a.type !== b.type) { - return false; - } - - if ( - a.type === "LogicalExpression" && - (a.operator === "||" || a.operator === "&&") && - a.operator === b.operator - ) { - return ( - (equal(a.left, b.left) && equal(a.right, b.right)) || - (equal(a.left, b.right) && equal(a.right, b.left)) - ); - } - - return astUtils.equalTokens(a, b, sourceCode); - } - - const isSubset = isSubsetByComparator.bind(null, equal); - - return { - IfStatement(node) { - const test = node.test, - conditionsToCheck = - test.type === "LogicalExpression" && - test.operator === "&&" - ? [test, ...splitByAnd(test)] - : [test]; - let current = node, - listToCheck = conditionsToCheck.map(c => - splitByOr(c).map(splitByAnd), - ); - - while ( - current.parent && - current.parent.type === "IfStatement" && - current.parent.alternate === current - ) { - current = current.parent; - - const currentOrOperands = splitByOr(current.test).map( - splitByAnd, - ); - - listToCheck = listToCheck.map(orOperands => - orOperands.filter( - orOperand => - !currentOrOperands.some(currentOrOperand => - isSubset(currentOrOperand, orOperand), - ), - ), - ); - - if ( - listToCheck.some(orOperands => orOperands.length === 0) - ) { - context.report({ node: test, messageId: "unexpected" }); - break; - } - } - }, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow duplicate conditions in if-else-if chains", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-dupe-else-if" + }, + + schema: [], + + messages: { + unexpected: "This branch can never execute. Its condition is a duplicate or covered by previous conditions in the if-else-if chain." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + + /** + * Determines whether the two given nodes are considered to be equal. In particular, given that the nodes + * represent expressions in a boolean context, `||` and `&&` can be considered as commutative operators. + * @param {ASTNode} a First node. + * @param {ASTNode} b Second node. + * @returns {boolean} `true` if the nodes are considered to be equal. + */ + function equal(a, b) { + if (a.type !== b.type) { + return false; + } + + if ( + a.type === "LogicalExpression" && + (a.operator === "||" || a.operator === "&&") && + a.operator === b.operator + ) { + return equal(a.left, b.left) && equal(a.right, b.right) || + equal(a.left, b.right) && equal(a.right, b.left); + } + + return astUtils.equalTokens(a, b, sourceCode); + } + + const isSubset = isSubsetByComparator.bind(null, equal); + + return { + IfStatement(node) { + const test = node.test, + conditionsToCheck = test.type === "LogicalExpression" && test.operator === "&&" + ? [test, ...splitByAnd(test)] + : [test]; + let current = node, + listToCheck = conditionsToCheck.map(c => splitByOr(c).map(splitByAnd)); + + while (current.parent && current.parent.type === "IfStatement" && current.parent.alternate === current) { + current = current.parent; + + const currentOrOperands = splitByOr(current.test).map(splitByAnd); + + listToCheck = listToCheck.map(orOperands => orOperands.filter( + orOperand => !currentOrOperands.some(currentOrOperand => isSubset(currentOrOperand, orOperand)) + )); + + if (listToCheck.some(orOperands => orOperands.length === 0)) { + context.report({ node: test, messageId: "unexpected" }); + break; + } + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-dupe-keys.js b/node_modules/eslint/lib/rules/no-dupe-keys.js index a17cb343d..980b0044a 100644 --- a/node_modules/eslint/lib/rules/no-dupe-keys.js +++ b/node_modules/eslint/lib/rules/no-dupe-keys.js @@ -22,144 +22,121 @@ const SET_KIND = /^(?:init|set)$/u; * The class which stores properties' information of an object. */ class ObjectInfo { - /** - * @param {ObjectInfo|null} upper The information of the outer object. - * @param {ASTNode} node The ObjectExpression node of this information. - */ - constructor(upper, node) { - this.upper = upper; - this.node = node; - this.properties = new Map(); - } - - /** - * Gets the information of the given Property node. - * @param {ASTNode} node The Property node to get. - * @returns {{get: boolean, set: boolean}} The information of the property. - */ - getPropertyInfo(node) { - const name = astUtils.getStaticPropertyName(node); - - if (!this.properties.has(name)) { - this.properties.set(name, { get: false, set: false }); - } - return this.properties.get(name); - } - - /** - * Checks whether the given property has been defined already or not. - * @param {ASTNode} node The Property node to check. - * @returns {boolean} `true` if the property has been defined. - */ - isPropertyDefined(node) { - const entry = this.getPropertyInfo(node); - - return ( - (GET_KIND.test(node.kind) && entry.get) || - (SET_KIND.test(node.kind) && entry.set) - ); - } - - /** - * Defines the given property. - * @param {ASTNode} node The Property node to define. - * @returns {void} - */ - defineProperty(node) { - const entry = this.getPropertyInfo(node); - - if (GET_KIND.test(node.kind)) { - entry.get = true; - } - if (SET_KIND.test(node.kind)) { - entry.set = true; - } - } + + /** + * @param {ObjectInfo|null} upper The information of the outer object. + * @param {ASTNode} node The ObjectExpression node of this information. + */ + constructor(upper, node) { + this.upper = upper; + this.node = node; + this.properties = new Map(); + } + + /** + * Gets the information of the given Property node. + * @param {ASTNode} node The Property node to get. + * @returns {{get: boolean, set: boolean}} The information of the property. + */ + getPropertyInfo(node) { + const name = astUtils.getStaticPropertyName(node); + + if (!this.properties.has(name)) { + this.properties.set(name, { get: false, set: false }); + } + return this.properties.get(name); + } + + /** + * Checks whether the given property has been defined already or not. + * @param {ASTNode} node The Property node to check. + * @returns {boolean} `true` if the property has been defined. + */ + isPropertyDefined(node) { + const entry = this.getPropertyInfo(node); + + return ( + (GET_KIND.test(node.kind) && entry.get) || + (SET_KIND.test(node.kind) && entry.set) + ); + } + + /** + * Defines the given property. + * @param {ASTNode} node The Property node to define. + * @returns {void} + */ + defineProperty(node) { + const entry = this.getPropertyInfo(node); + + if (GET_KIND.test(node.kind)) { + entry.get = true; + } + if (SET_KIND.test(node.kind)) { + entry.set = true; + } + } } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - docs: { - description: "Disallow duplicate keys in object literals", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-dupe-keys", - }, - - schema: [], - - messages: { - unexpected: "Duplicate key '{{name}}'.", - }, - }, - - create(context) { - let info = null; - - return { - ObjectExpression(node) { - info = new ObjectInfo(info, node); - }, - "ObjectExpression:exit"() { - info = info.upper; - }, - - Property(node) { - const name = astUtils.getStaticPropertyName(node); - - // Skip destructuring. - if (node.parent.type !== "ObjectExpression") { - return; - } - - // Skip if the name is not static. - if (name === null) { - return; - } - - /* - * Skip if the property node is a proto setter. - * Proto setter is a special syntax that sets - * object's prototype instead of creating a property. - * It can be in one of the following forms: - * - * __proto__: - * '__proto__': - * "__proto__": - * - * Duplicate proto setters produce parsing errors, - * so we can just skip them to not interfere with - * regular properties named "__proto__". - */ - if ( - name === "__proto__" && - node.kind === "init" && - !node.computed && - !node.shorthand && - !node.method - ) { - return; - } - - // Reports if the name is defined already. - if (info.isPropertyDefined(node)) { - context.report({ - node: info.node, - loc: node.key.loc, - messageId: "unexpected", - data: { name }, - }); - } - - // Update info. - info.defineProperty(node); - }, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow duplicate keys in object literals", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-dupe-keys" + }, + + schema: [], + + messages: { + unexpected: "Duplicate key '{{name}}'." + } + }, + + create(context) { + let info = null; + + return { + ObjectExpression(node) { + info = new ObjectInfo(info, node); + }, + "ObjectExpression:exit"() { + info = info.upper; + }, + + Property(node) { + const name = astUtils.getStaticPropertyName(node); + + // Skip destructuring. + if (node.parent.type !== "ObjectExpression") { + return; + } + + // Skip if the name is not static. + if (name === null) { + return; + } + + // Reports if the name is defined already. + if (info.isPropertyDefined(node)) { + context.report({ + node: info.node, + loc: node.key.loc, + messageId: "unexpected", + data: { name } + }); + } + + // Update info. + info.defineProperty(node); + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-duplicate-case.js b/node_modules/eslint/lib/rules/no-duplicate-case.js index 161b1a0bd..839f357e7 100644 --- a/node_modules/eslint/lib/rules/no-duplicate-case.js +++ b/node_modules/eslint/lib/rules/no-duplicate-case.js @@ -16,63 +16,56 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", + meta: { + type: "problem", - docs: { - description: "Disallow duplicate case labels", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-duplicate-case", - }, + docs: { + description: "Disallow duplicate case labels", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-duplicate-case" + }, - schema: [], + schema: [], - messages: { - unexpected: "Duplicate case label.", - }, - }, + messages: { + unexpected: "Duplicate case label." + } + }, - create(context) { - const sourceCode = context.sourceCode; + create(context) { + const sourceCode = context.sourceCode; - /** - * Determines whether the two given nodes are considered to be equal. - * @param {ASTNode} a First node. - * @param {ASTNode} b Second node. - * @returns {boolean} `true` if the nodes are considered to be equal. - */ - function equal(a, b) { - if (a.type !== b.type) { - return false; - } + /** + * Determines whether the two given nodes are considered to be equal. + * @param {ASTNode} a First node. + * @param {ASTNode} b Second node. + * @returns {boolean} `true` if the nodes are considered to be equal. + */ + function equal(a, b) { + if (a.type !== b.type) { + return false; + } - return astUtils.equalTokens(a, b, sourceCode); - } - return { - SwitchStatement(node) { - const previousTests = []; + return astUtils.equalTokens(a, b, sourceCode); + } + return { + SwitchStatement(node) { + const previousTests = []; - for (const switchCase of node.cases) { - if (switchCase.test) { - const test = switchCase.test; + for (const switchCase of node.cases) { + if (switchCase.test) { + const test = switchCase.test; - if ( - previousTests.some(previousTest => - equal(previousTest, test), - ) - ) { - context.report({ - node: switchCase, - messageId: "unexpected", - }); - } else { - previousTests.push(test); - } - } - } - }, - }; - }, + if (previousTests.some(previousTest => equal(previousTest, test))) { + context.report({ node: switchCase, messageId: "unexpected" }); + } else { + previousTests.push(test); + } + } + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-duplicate-imports.js b/node_modules/eslint/lib/rules/no-duplicate-imports.js index 762703327..25c07b750 100644 --- a/node_modules/eslint/lib/rules/no-duplicate-imports.js +++ b/node_modules/eslint/lib/rules/no-duplicate-imports.js @@ -10,8 +10,8 @@ const NAMED_TYPES = ["ImportSpecifier", "ExportSpecifier"]; const NAMESPACE_TYPES = [ - "ImportNamespaceSpecifier", - "ExportNamespaceSpecifier", + "ImportNamespaceSpecifier", + "ExportNamespaceSpecifier" ]; //------------------------------------------------------------------------------ @@ -25,9 +25,9 @@ const NAMESPACE_TYPES = [ * @returns {boolean} True if import/export type belongs to (ImportSpecifier|ExportSpecifier) or (ImportNamespaceSpecifier|ExportNamespaceSpecifier) and false if it doesn't. */ function isImportExportSpecifier(importExportType, type) { - const arrayToCheck = type === "named" ? NAMED_TYPES : NAMESPACE_TYPES; + const arrayToCheck = type === "named" ? NAMED_TYPES : NAMESPACE_TYPES; - return arrayToCheck.includes(importExportType); + return arrayToCheck.includes(importExportType); } /** @@ -36,24 +36,24 @@ function isImportExportSpecifier(importExportType, type) { * @returns {string} The type of the (import|export). */ function getImportExportType(node) { - if (node.specifiers && node.specifiers.length > 0) { - const nodeSpecifiers = node.specifiers; - const index = nodeSpecifiers.findIndex( - ({ type }) => - isImportExportSpecifier(type, "named") || - isImportExportSpecifier(type, "namespace"), - ); - const i = index > -1 ? index : 0; - - return nodeSpecifiers[i].type; - } - if (node.type === "ExportAllDeclaration") { - if (node.exported) { - return "ExportNamespaceSpecifier"; - } - return "ExportAll"; - } - return "SideEffectImport"; + if (node.specifiers && node.specifiers.length > 0) { + const nodeSpecifiers = node.specifiers; + const index = nodeSpecifiers.findIndex( + ({ type }) => + isImportExportSpecifier(type, "named") || + isImportExportSpecifier(type, "namespace") + ); + const i = index > -1 ? index : 0; + + return nodeSpecifiers[i].type; + } + if (node.type === "ExportAllDeclaration") { + if (node.exported) { + return "ExportNamespaceSpecifier"; + } + return "ExportAll"; + } + return "SideEffectImport"; } /** @@ -63,80 +63,46 @@ function getImportExportType(node) { * @returns {boolean} True if two (import|export) can be merged, false if they can't. */ function isImportExportCanBeMerged(node1, node2) { - const importExportType1 = getImportExportType(node1); - const importExportType2 = getImportExportType(node2); - - if ( - (node1.importKind === "type" || node1.exportKind === "type") && - (node2.importKind === "type" || node2.exportKind === "type") - ) { - const isDefault1 = importExportType1 === "ImportDefaultSpecifier"; - const isDefault2 = importExportType2 === "ImportDefaultSpecifier"; - const isNamed1 = isImportExportSpecifier(importExportType1, "named"); - const isNamed2 = isImportExportSpecifier(importExportType2, "named"); - - if ((isDefault1 && isNamed2) || (isDefault2 && isNamed1)) { - return false; - } - } - - if ( - (importExportType1 === "ExportAll" && - importExportType2 !== "ExportAll" && - importExportType2 !== "SideEffectImport") || - (importExportType1 !== "ExportAll" && - importExportType1 !== "SideEffectImport" && - importExportType2 === "ExportAll") - ) { - return false; - } - if ( - (isImportExportSpecifier(importExportType1, "namespace") && - isImportExportSpecifier(importExportType2, "named")) || - (isImportExportSpecifier(importExportType2, "namespace") && - isImportExportSpecifier(importExportType1, "named")) - ) { - return false; - } - return true; + const importExportType1 = getImportExportType(node1); + const importExportType2 = getImportExportType(node2); + + if ( + (importExportType1 === "ExportAll" && + importExportType2 !== "ExportAll" && + importExportType2 !== "SideEffectImport") || + (importExportType1 !== "ExportAll" && + importExportType1 !== "SideEffectImport" && + importExportType2 === "ExportAll") + ) { + return false; + } + if ( + (isImportExportSpecifier(importExportType1, "namespace") && + isImportExportSpecifier(importExportType2, "named")) || + (isImportExportSpecifier(importExportType2, "namespace") && + isImportExportSpecifier(importExportType1, "named")) + ) { + return false; + } + return true; } /** * Returns a boolean if we should report (import|export). * @param {ASTNode} node A node to be reported or not. * @param {[ASTNode]} previousNodes An array contains previous nodes of the module imported or exported. - * @param {boolean} allowSeparateTypeImports Whether to allow separate type and value imports. * @returns {boolean} True if the (import|export) should be reported. */ -function shouldReportImportExport( - node, - previousNodes, - allowSeparateTypeImports, -) { - let i = 0; - - while (i < previousNodes.length) { - const previousNode = previousNodes[i]; - - if (allowSeparateTypeImports) { - const isTypeNode = - node.importKind === "type" || node.exportKind === "type"; - const isTypePrevious = - previousNode.importKind === "type" || - previousNode.exportKind === "type"; - - if (isTypeNode !== isTypePrevious) { - i++; - continue; - } - } - - if (isImportExportCanBeMerged(node, previousNode)) { - return true; - } - i++; - } - return false; +function shouldReportImportExport(node, previousNodes) { + let i = 0; + + while (i < previousNodes.length) { + if (isImportExportCanBeMerged(node, previousNodes[i])) { + return true; + } + i++; + } + return false; } /** @@ -146,9 +112,9 @@ function shouldReportImportExport( * @returns {[ASTNode]} An array contains only nodes with declarations types equal to type. */ function getNodesByDeclarationType(nodes, type) { - return nodes - .filter(({ declarationType }) => declarationType === type) - .map(({ node }) => node); + return nodes + .filter(({ declarationType }) => declarationType === type) + .map(({ node }) => node); } /** @@ -157,10 +123,10 @@ function getNodesByDeclarationType(nodes, type) { * @returns {string} The name of the module, or empty string if no name. */ function getModule(node) { - if (node && node.source && node.source.value) { - return node.source.value.trim(); - } - return ""; + if (node && node.source && node.source.value) { + return node.source.value.trim(); + } + return ""; } /** @@ -170,79 +136,52 @@ function getModule(node) { * @param {Map} modules A Map object contains as a key a module name and as value an array contains objects, each object contains a node and a declaration type. * @param {string} declarationType A declaration type can be an import or export. * @param {boolean} includeExports Whether or not to check for exports in addition to imports. - * @param {boolean} allowSeparateTypeImports Whether to allow separate type and value imports. * @returns {void} No return value. */ function checkAndReport( - context, - node, - modules, - declarationType, - includeExports, - allowSeparateTypeImports, + context, + node, + modules, + declarationType, + includeExports ) { - const module = getModule(node); - - if (modules.has(module)) { - const previousNodes = modules.get(module); - const messagesIds = []; - const importNodes = getNodesByDeclarationType(previousNodes, "import"); - let exportNodes; - - if (includeExports) { - exportNodes = getNodesByDeclarationType(previousNodes, "export"); - } - if (declarationType === "import") { - if ( - shouldReportImportExport( - node, - importNodes, - allowSeparateTypeImports, - ) - ) { - messagesIds.push("import"); - } - if (includeExports) { - if ( - shouldReportImportExport( - node, - exportNodes, - allowSeparateTypeImports, - ) - ) { - messagesIds.push("importAs"); - } - } - } else if (declarationType === "export") { - if ( - shouldReportImportExport( - node, - exportNodes, - allowSeparateTypeImports, - ) - ) { - messagesIds.push("export"); - } - if ( - shouldReportImportExport( - node, - importNodes, - allowSeparateTypeImports, - ) - ) { - messagesIds.push("exportAs"); - } - } - messagesIds.forEach(messageId => - context.report({ - node, - messageId, - data: { - module, - }, - }), - ); - } + const module = getModule(node); + + if (modules.has(module)) { + const previousNodes = modules.get(module); + const messagesIds = []; + const importNodes = getNodesByDeclarationType(previousNodes, "import"); + let exportNodes; + + if (includeExports) { + exportNodes = getNodesByDeclarationType(previousNodes, "export"); + } + if (declarationType === "import") { + if (shouldReportImportExport(node, importNodes)) { + messagesIds.push("import"); + } + if (includeExports) { + if (shouldReportImportExport(node, exportNodes)) { + messagesIds.push("importAs"); + } + } + } else if (declarationType === "export") { + if (shouldReportImportExport(node, exportNodes)) { + messagesIds.push("export"); + } + if (shouldReportImportExport(node, importNodes)) { + messagesIds.push("exportAs"); + } + } + messagesIds.forEach(messageId => + context.report({ + node, + messageId, + data: { + module + } + })); + } } /** @@ -256,113 +195,96 @@ function checkAndReport( * @param {Map} modules A Map object contains as a key a module name and as value an array contains objects, each object contains a node and a declaration type. * @param {string} declarationType A declaration type can be an import or export. * @param {boolean} includeExports Whether or not to check for exports in addition to imports. - * @param {boolean} allowSeparateTypeImports Whether to allow separate type and value imports. * @returns {nodeCallback} A function passed to ESLint to handle the statement. */ function handleImportsExports( - context, - modules, - declarationType, - includeExports, - allowSeparateTypeImports, + context, + modules, + declarationType, + includeExports ) { - return function (node) { - const module = getModule(node); - - if (module) { - checkAndReport( - context, - node, - modules, - declarationType, - includeExports, - allowSeparateTypeImports, - ); - const currentNode = { node, declarationType }; - let nodes = [currentNode]; - - if (modules.has(module)) { - const previousNodes = modules.get(module); - - nodes = [...previousNodes, currentNode]; - } - modules.set(module, nodes); - } - }; + return function(node) { + const module = getModule(node); + + if (module) { + checkAndReport( + context, + node, + modules, + declarationType, + includeExports + ); + const currentNode = { node, declarationType }; + let nodes = [currentNode]; + + if (modules.has(module)) { + const previousNodes = modules.get(module); + + nodes = [...previousNodes, currentNode]; + } + modules.set(module, nodes); + } + }; } -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - dialects: ["javascript", "typescript"], - language: "javascript", - type: "problem", - - defaultOptions: [ - { - includeExports: false, - allowSeparateTypeImports: false, - }, - ], - - docs: { - description: "Disallow duplicate module imports", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-duplicate-imports", - }, - - schema: [ - { - type: "object", - properties: { - includeExports: { - type: "boolean", - }, - allowSeparateTypeImports: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - - messages: { - import: "'{{module}}' import is duplicated.", - importAs: "'{{module}}' import is duplicated as export.", - export: "'{{module}}' export is duplicated.", - exportAs: "'{{module}}' export is duplicated as import.", - }, - }, - - create(context) { - const [{ includeExports, allowSeparateTypeImports }] = context.options; - const modules = new Map(); - const handlers = { - ImportDeclaration: handleImportsExports( - context, - modules, - "import", - includeExports, - allowSeparateTypeImports, - ), - }; - - if (includeExports) { - handlers.ExportNamedDeclaration = handleImportsExports( - context, - modules, - "export", - includeExports, - allowSeparateTypeImports, - ); - handlers.ExportAllDeclaration = handleImportsExports( - context, - modules, - "export", - includeExports, - allowSeparateTypeImports, - ); - } - return handlers; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow duplicate module imports", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-duplicate-imports" + }, + + schema: [ + { + type: "object", + properties: { + includeExports: { + type: "boolean", + default: false + } + }, + additionalProperties: false + } + ], + + messages: { + import: "'{{module}}' import is duplicated.", + importAs: "'{{module}}' import is duplicated as export.", + export: "'{{module}}' export is duplicated.", + exportAs: "'{{module}}' export is duplicated as import." + } + }, + + create(context) { + const includeExports = (context.options[0] || {}).includeExports, + modules = new Map(); + const handlers = { + ImportDeclaration: handleImportsExports( + context, + modules, + "import", + includeExports + ) + }; + + if (includeExports) { + handlers.ExportNamedDeclaration = handleImportsExports( + context, + modules, + "export", + includeExports + ); + handlers.ExportAllDeclaration = handleImportsExports( + context, + modules, + "export", + includeExports + ); + } + return handlers; + } }; diff --git a/node_modules/eslint/lib/rules/no-else-return.js b/node_modules/eslint/lib/rules/no-else-return.js index 40e11dbd2..9dbf56965 100644 --- a/node_modules/eslint/lib/rules/no-else-return.js +++ b/node_modules/eslint/lib/rules/no-else-return.js @@ -16,435 +16,390 @@ const FixTracker = require("./utils/fix-tracker"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - defaultOptions: [{ allowElseIf: true }], - - docs: { - description: - "Disallow `else` blocks after `return` statements in `if` statements", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/no-else-return", - }, - - schema: [ - { - type: "object", - properties: { - allowElseIf: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - - fixable: "code", - - messages: { - unexpected: "Unnecessary 'else' after 'return'.", - }, - }, - - create(context) { - const [{ allowElseIf }] = context.options; - const sourceCode = context.sourceCode; - - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - /** - * Checks whether the given names can be safely used to declare block-scoped variables - * in the given scope. Name collisions can produce redeclaration syntax errors, - * or silently change references and modify behavior of the original code. - * - * This is not a generic function. In particular, it is assumed that the scope is a function scope or - * a function's inner scope, and that the names can be valid identifiers in the given scope. - * @param {string[]} names Array of variable names. - * @param {eslint-scope.Scope} scope Function scope or a function's inner scope. - * @returns {boolean} True if all names can be safely declared, false otherwise. - */ - function isSafeToDeclare(names, scope) { - if (names.length === 0) { - return true; - } - - const functionScope = scope.variableScope; - - /* - * If this is a function scope, scope.variables will contain parameters, implicit variables such as "arguments", - * all function-scoped variables ('var'), and block-scoped variables defined in the scope. - * If this is an inner scope, scope.variables will contain block-scoped variables defined in the scope. - * - * Redeclaring any of these would cause a syntax error, except for the implicit variables. - */ - const declaredVariables = scope.variables.filter( - ({ defs }) => defs.length > 0, - ); - - if (declaredVariables.some(({ name }) => names.includes(name))) { - return false; - } - - // Redeclaring a catch variable would also cause a syntax error. - if (scope !== functionScope && scope.upper.type === "catch") { - if ( - scope.upper.variables.some(({ name }) => - names.includes(name), - ) - ) { - return false; - } - } - - /* - * Redeclaring an implicit variable, such as "arguments", would not cause a syntax error. - * However, if the variable was used, declaring a new one with the same name would change references - * and modify behavior. - */ - const usedImplicitVariables = scope.variables.filter( - ({ defs, references }) => - defs.length === 0 && references.length > 0, - ); - - if ( - usedImplicitVariables.some(({ name }) => names.includes(name)) - ) { - return false; - } - - /* - * Declaring a variable with a name that was already used to reference a variable from an upper scope - * would change references and modify behavior. - */ - if (scope.through.some(t => names.includes(t.identifier.name))) { - return false; - } - - /* - * If the scope is an inner scope (not the function scope), an uninitialized `var` variable declared inside - * the scope node (directly or in one of its descendants) is neither declared nor 'through' in the scope. - * - * For example, this would be a syntax error "Identifier 'a' has already been declared": - * function foo() { if (bar) { let a; if (baz) { var a; } } } - */ - if (scope !== functionScope) { - const scopeNodeRange = scope.block.range; - const variablesToCheck = functionScope.variables.filter( - ({ name }) => names.includes(name), - ); - - if ( - variablesToCheck.some(v => - v.defs.some( - ({ node: { range } }) => - scopeNodeRange[0] <= range[0] && - range[1] <= scopeNodeRange[1], - ), - ) - ) { - return false; - } - } - - return true; - } - - /** - * Checks whether the removal of `else` and its braces is safe from variable name collisions. - * @param {Node} node The 'else' node. - * @param {eslint-scope.Scope} scope The scope in which the node and the whole 'if' statement is. - * @returns {boolean} True if it is safe, false otherwise. - */ - function isSafeFromNameCollisions(node, scope) { - if (node.type === "FunctionDeclaration") { - // Conditional function declaration. Scope and hoisting are unpredictable, different engines work differently. - return false; - } - - if (node.type !== "BlockStatement") { - return true; - } - - const elseBlockScope = scope.childScopes.find( - ({ block }) => block === node, - ); - - if (!elseBlockScope) { - // ecmaVersion < 6, `else` block statement cannot have its own scope, no possible collisions. - return true; - } - - /* - * elseBlockScope is supposed to merge into its upper scope. elseBlockScope.variables array contains - * only block-scoped variables (such as let and const variables or class and function declarations) - * defined directly in the elseBlockScope. These are exactly the only names that could cause collisions. - */ - const namesToCheck = elseBlockScope.variables.map( - ({ name }) => name, - ); - - return isSafeToDeclare(namesToCheck, scope); - } - - /** - * Display the context report if rule is violated - * @param {Node} elseNode The 'else' node - * @returns {void} - */ - function displayReport(elseNode) { - const currentScope = sourceCode.getScope(elseNode.parent); - - context.report({ - node: elseNode, - messageId: "unexpected", - fix(fixer) { - if (!isSafeFromNameCollisions(elseNode, currentScope)) { - return null; - } - - const startToken = sourceCode.getFirstToken(elseNode); - const elseToken = sourceCode.getTokenBefore(startToken); - const source = sourceCode.getText(elseNode); - const lastIfToken = sourceCode.getTokenBefore(elseToken); - let fixedSource, firstTokenOfElseBlock; - - if ( - startToken.type === "Punctuator" && - startToken.value === "{" - ) { - firstTokenOfElseBlock = - sourceCode.getTokenAfter(startToken); - } else { - firstTokenOfElseBlock = startToken; - } - - /* - * If the if block does not have curly braces and does not end in a semicolon - * and the else block starts with (, [, /, +, ` or -, then it is not - * safe to remove the else keyword, because ASI will not add a semicolon - * after the if block - */ - const ifBlockMaybeUnsafe = - elseNode.parent.consequent.type !== "BlockStatement" && - lastIfToken.value !== ";"; - const elseBlockUnsafe = /^[([/+`-]/u.test( - firstTokenOfElseBlock.value, - ); - - if (ifBlockMaybeUnsafe && elseBlockUnsafe) { - return null; - } - - const endToken = sourceCode.getLastToken(elseNode); - const lastTokenOfElseBlock = - sourceCode.getTokenBefore(endToken); - - if (lastTokenOfElseBlock.value !== ";") { - const nextToken = sourceCode.getTokenAfter(endToken); - - const nextTokenUnsafe = - nextToken && /^[([/+`-]/u.test(nextToken.value); - const nextTokenOnSameLine = - nextToken && - nextToken.loc.start.line === - lastTokenOfElseBlock.loc.start.line; - - /* - * If the else block contents does not end in a semicolon, - * and the else block starts with (, [, /, +, ` or -, then it is not - * safe to remove the else block, because ASI will not add a semicolon - * after the remaining else block contents - */ - if ( - nextTokenUnsafe || - (nextTokenOnSameLine && nextToken.value !== "}") - ) { - return null; - } - } - - if ( - startToken.type === "Punctuator" && - startToken.value === "{" - ) { - fixedSource = source.slice(1, -1); - } else { - fixedSource = source; - } - - /* - * Extend the replacement range to include the entire - * function to avoid conflicting with no-useless-return. - * https://github.com/eslint/eslint/issues/8026 - * - * Also, to avoid name collisions between two else blocks. - */ - return new FixTracker(fixer, sourceCode) - .retainEnclosingFunction(elseNode) - .replaceTextRange( - [elseToken.range[0], elseNode.range[1]], - fixedSource, - ); - }, - }); - } - - /** - * Check to see if the node is a ReturnStatement - * @param {Node} node The node being evaluated - * @returns {boolean} True if node is a return - */ - function checkForReturn(node) { - return node.type === "ReturnStatement"; - } - - /** - * Naive return checking, does not iterate through the whole - * BlockStatement because we make the assumption that the ReturnStatement - * will be the last node in the body of the BlockStatement. - * @param {Node} node The consequent/alternate node - * @returns {boolean} True if it has a return - */ - function naiveHasReturn(node) { - if (node.type === "BlockStatement") { - const body = node.body, - lastChildNode = body.at(-1); - - return lastChildNode && checkForReturn(lastChildNode); - } - return checkForReturn(node); - } - - /** - * Check to see if the node is valid for evaluation, - * meaning it has an else. - * @param {Node} node The node being evaluated - * @returns {boolean} True if the node is valid - */ - function hasElse(node) { - return node.alternate && node.consequent; - } - - /** - * If the consequent is an IfStatement, check to see if it has an else - * and both its consequent and alternate path return, meaning this is - * a nested case of rule violation. If-Else not considered currently. - * @param {Node} node The consequent node - * @returns {boolean} True if this is a nested rule violation - */ - function checkForIf(node) { - return ( - node.type === "IfStatement" && - hasElse(node) && - naiveHasReturn(node.alternate) && - naiveHasReturn(node.consequent) - ); - } - - /** - * Check the consequent/body node to make sure it is not - * a ReturnStatement or an IfStatement that returns on both - * code paths. - * @param {Node} node The consequent or body node - * @returns {boolean} `true` if it is a Return/If node that always returns. - */ - function checkForReturnOrIf(node) { - return checkForReturn(node) || checkForIf(node); - } - - /** - * Check whether a node returns in every codepath. - * @param {Node} node The node to be checked - * @returns {boolean} `true` if it returns on every codepath. - */ - function alwaysReturns(node) { - if (node.type === "BlockStatement") { - // If we have a BlockStatement, check each consequent body node. - return node.body.some(checkForReturnOrIf); - } - - /* - * If not a block statement, make sure the consequent isn't a - * ReturnStatement or an IfStatement with returns on both paths. - */ - return checkForReturnOrIf(node); - } - - /** - * Check the if statement, but don't catch else-if blocks. - * @returns {void} - * @param {Node} node The node for the if statement to check - * @private - */ - function checkIfWithoutElse(node) { - const parent = node.parent; - - /* - * Fixing this would require splitting one statement into two, so no error should - * be reported if this node is in a position where only one statement is allowed. - */ - if (!astUtils.STATEMENT_LIST_PARENTS.has(parent.type)) { - return; - } - - const consequents = []; - let alternate; - - for ( - let currentNode = node; - currentNode.type === "IfStatement"; - currentNode = currentNode.alternate - ) { - if (!currentNode.alternate) { - return; - } - consequents.push(currentNode.consequent); - alternate = currentNode.alternate; - } - - if (consequents.every(alwaysReturns)) { - displayReport(alternate); - } - } - - /** - * Check the if statement - * @returns {void} - * @param {Node} node The node for the if statement to check - * @private - */ - function checkIfWithElse(node) { - const parent = node.parent; - - /* - * Fixing this would require splitting one statement into two, so no error should - * be reported if this node is in a position where only one statement is allowed. - */ - if (!astUtils.STATEMENT_LIST_PARENTS.has(parent.type)) { - return; - } - - const alternate = node.alternate; - - if (alternate && alwaysReturns(node.consequent)) { - displayReport(alternate); - } - } - - //-------------------------------------------------------------------------- - // Public API - //-------------------------------------------------------------------------- - - return { - "IfStatement:exit": allowElseIf - ? checkIfWithoutElse - : checkIfWithElse, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow `else` blocks after `return` statements in `if` statements", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-else-return" + }, + + schema: [{ + type: "object", + properties: { + allowElseIf: { + type: "boolean", + default: true + } + }, + additionalProperties: false + }], + + fixable: "code", + + messages: { + unexpected: "Unnecessary 'else' after 'return'." + } + }, + + create(context) { + + const sourceCode = context.sourceCode; + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + /** + * Checks whether the given names can be safely used to declare block-scoped variables + * in the given scope. Name collisions can produce redeclaration syntax errors, + * or silently change references and modify behavior of the original code. + * + * This is not a generic function. In particular, it is assumed that the scope is a function scope or + * a function's inner scope, and that the names can be valid identifiers in the given scope. + * @param {string[]} names Array of variable names. + * @param {eslint-scope.Scope} scope Function scope or a function's inner scope. + * @returns {boolean} True if all names can be safely declared, false otherwise. + */ + function isSafeToDeclare(names, scope) { + + if (names.length === 0) { + return true; + } + + const functionScope = scope.variableScope; + + /* + * If this is a function scope, scope.variables will contain parameters, implicit variables such as "arguments", + * all function-scoped variables ('var'), and block-scoped variables defined in the scope. + * If this is an inner scope, scope.variables will contain block-scoped variables defined in the scope. + * + * Redeclaring any of these would cause a syntax error, except for the implicit variables. + */ + const declaredVariables = scope.variables.filter(({ defs }) => defs.length > 0); + + if (declaredVariables.some(({ name }) => names.includes(name))) { + return false; + } + + // Redeclaring a catch variable would also cause a syntax error. + if (scope !== functionScope && scope.upper.type === "catch") { + if (scope.upper.variables.some(({ name }) => names.includes(name))) { + return false; + } + } + + /* + * Redeclaring an implicit variable, such as "arguments", would not cause a syntax error. + * However, if the variable was used, declaring a new one with the same name would change references + * and modify behavior. + */ + const usedImplicitVariables = scope.variables.filter(({ defs, references }) => + defs.length === 0 && references.length > 0); + + if (usedImplicitVariables.some(({ name }) => names.includes(name))) { + return false; + } + + /* + * Declaring a variable with a name that was already used to reference a variable from an upper scope + * would change references and modify behavior. + */ + if (scope.through.some(t => names.includes(t.identifier.name))) { + return false; + } + + /* + * If the scope is an inner scope (not the function scope), an uninitialized `var` variable declared inside + * the scope node (directly or in one of its descendants) is neither declared nor 'through' in the scope. + * + * For example, this would be a syntax error "Identifier 'a' has already been declared": + * function foo() { if (bar) { let a; if (baz) { var a; } } } + */ + if (scope !== functionScope) { + const scopeNodeRange = scope.block.range; + const variablesToCheck = functionScope.variables.filter(({ name }) => names.includes(name)); + + if (variablesToCheck.some(v => v.defs.some(({ node: { range } }) => + scopeNodeRange[0] <= range[0] && range[1] <= scopeNodeRange[1]))) { + return false; + } + } + + return true; + } + + + /** + * Checks whether the removal of `else` and its braces is safe from variable name collisions. + * @param {Node} node The 'else' node. + * @param {eslint-scope.Scope} scope The scope in which the node and the whole 'if' statement is. + * @returns {boolean} True if it is safe, false otherwise. + */ + function isSafeFromNameCollisions(node, scope) { + + if (node.type === "FunctionDeclaration") { + + // Conditional function declaration. Scope and hoisting are unpredictable, different engines work differently. + return false; + } + + if (node.type !== "BlockStatement") { + return true; + } + + const elseBlockScope = scope.childScopes.find(({ block }) => block === node); + + if (!elseBlockScope) { + + // ecmaVersion < 6, `else` block statement cannot have its own scope, no possible collisions. + return true; + } + + /* + * elseBlockScope is supposed to merge into its upper scope. elseBlockScope.variables array contains + * only block-scoped variables (such as let and const variables or class and function declarations) + * defined directly in the elseBlockScope. These are exactly the only names that could cause collisions. + */ + const namesToCheck = elseBlockScope.variables.map(({ name }) => name); + + return isSafeToDeclare(namesToCheck, scope); + } + + /** + * Display the context report if rule is violated + * @param {Node} elseNode The 'else' node + * @returns {void} + */ + function displayReport(elseNode) { + const currentScope = sourceCode.getScope(elseNode.parent); + + context.report({ + node: elseNode, + messageId: "unexpected", + fix(fixer) { + + if (!isSafeFromNameCollisions(elseNode, currentScope)) { + return null; + } + + const startToken = sourceCode.getFirstToken(elseNode); + const elseToken = sourceCode.getTokenBefore(startToken); + const source = sourceCode.getText(elseNode); + const lastIfToken = sourceCode.getTokenBefore(elseToken); + let fixedSource, firstTokenOfElseBlock; + + if (startToken.type === "Punctuator" && startToken.value === "{") { + firstTokenOfElseBlock = sourceCode.getTokenAfter(startToken); + } else { + firstTokenOfElseBlock = startToken; + } + + /* + * If the if block does not have curly braces and does not end in a semicolon + * and the else block starts with (, [, /, +, ` or -, then it is not + * safe to remove the else keyword, because ASI will not add a semicolon + * after the if block + */ + const ifBlockMaybeUnsafe = elseNode.parent.consequent.type !== "BlockStatement" && lastIfToken.value !== ";"; + const elseBlockUnsafe = /^[([/+`-]/u.test(firstTokenOfElseBlock.value); + + if (ifBlockMaybeUnsafe && elseBlockUnsafe) { + return null; + } + + const endToken = sourceCode.getLastToken(elseNode); + const lastTokenOfElseBlock = sourceCode.getTokenBefore(endToken); + + if (lastTokenOfElseBlock.value !== ";") { + const nextToken = sourceCode.getTokenAfter(endToken); + + const nextTokenUnsafe = nextToken && /^[([/+`-]/u.test(nextToken.value); + const nextTokenOnSameLine = nextToken && nextToken.loc.start.line === lastTokenOfElseBlock.loc.start.line; + + /* + * If the else block contents does not end in a semicolon, + * and the else block starts with (, [, /, +, ` or -, then it is not + * safe to remove the else block, because ASI will not add a semicolon + * after the remaining else block contents + */ + if (nextTokenUnsafe || (nextTokenOnSameLine && nextToken.value !== "}")) { + return null; + } + } + + if (startToken.type === "Punctuator" && startToken.value === "{") { + fixedSource = source.slice(1, -1); + } else { + fixedSource = source; + } + + /* + * Extend the replacement range to include the entire + * function to avoid conflicting with no-useless-return. + * https://github.com/eslint/eslint/issues/8026 + * + * Also, to avoid name collisions between two else blocks. + */ + return new FixTracker(fixer, sourceCode) + .retainEnclosingFunction(elseNode) + .replaceTextRange([elseToken.range[0], elseNode.range[1]], fixedSource); + } + }); + } + + /** + * Check to see if the node is a ReturnStatement + * @param {Node} node The node being evaluated + * @returns {boolean} True if node is a return + */ + function checkForReturn(node) { + return node.type === "ReturnStatement"; + } + + /** + * Naive return checking, does not iterate through the whole + * BlockStatement because we make the assumption that the ReturnStatement + * will be the last node in the body of the BlockStatement. + * @param {Node} node The consequent/alternate node + * @returns {boolean} True if it has a return + */ + function naiveHasReturn(node) { + if (node.type === "BlockStatement") { + const body = node.body, + lastChildNode = body[body.length - 1]; + + return lastChildNode && checkForReturn(lastChildNode); + } + return checkForReturn(node); + } + + /** + * Check to see if the node is valid for evaluation, + * meaning it has an else. + * @param {Node} node The node being evaluated + * @returns {boolean} True if the node is valid + */ + function hasElse(node) { + return node.alternate && node.consequent; + } + + /** + * If the consequent is an IfStatement, check to see if it has an else + * and both its consequent and alternate path return, meaning this is + * a nested case of rule violation. If-Else not considered currently. + * @param {Node} node The consequent node + * @returns {boolean} True if this is a nested rule violation + */ + function checkForIf(node) { + return node.type === "IfStatement" && hasElse(node) && + naiveHasReturn(node.alternate) && naiveHasReturn(node.consequent); + } + + /** + * Check the consequent/body node to make sure it is not + * a ReturnStatement or an IfStatement that returns on both + * code paths. + * @param {Node} node The consequent or body node + * @returns {boolean} `true` if it is a Return/If node that always returns. + */ + function checkForReturnOrIf(node) { + return checkForReturn(node) || checkForIf(node); + } + + + /** + * Check whether a node returns in every codepath. + * @param {Node} node The node to be checked + * @returns {boolean} `true` if it returns on every codepath. + */ + function alwaysReturns(node) { + if (node.type === "BlockStatement") { + + // If we have a BlockStatement, check each consequent body node. + return node.body.some(checkForReturnOrIf); + } + + /* + * If not a block statement, make sure the consequent isn't a + * ReturnStatement or an IfStatement with returns on both paths. + */ + return checkForReturnOrIf(node); + } + + + /** + * Check the if statement, but don't catch else-if blocks. + * @returns {void} + * @param {Node} node The node for the if statement to check + * @private + */ + function checkIfWithoutElse(node) { + const parent = node.parent; + + /* + * Fixing this would require splitting one statement into two, so no error should + * be reported if this node is in a position where only one statement is allowed. + */ + if (!astUtils.STATEMENT_LIST_PARENTS.has(parent.type)) { + return; + } + + const consequents = []; + let alternate; + + for (let currentNode = node; currentNode.type === "IfStatement"; currentNode = currentNode.alternate) { + if (!currentNode.alternate) { + return; + } + consequents.push(currentNode.consequent); + alternate = currentNode.alternate; + } + + if (consequents.every(alwaysReturns)) { + displayReport(alternate); + } + } + + /** + * Check the if statement + * @returns {void} + * @param {Node} node The node for the if statement to check + * @private + */ + function checkIfWithElse(node) { + const parent = node.parent; + + + /* + * Fixing this would require splitting one statement into two, so no error should + * be reported if this node is in a position where only one statement is allowed. + */ + if (!astUtils.STATEMENT_LIST_PARENTS.has(parent.type)) { + return; + } + + const alternate = node.alternate; + + if (alternate && alwaysReturns(node.consequent)) { + displayReport(alternate); + } + } + + const allowElseIf = !(context.options[0] && context.options[0].allowElseIf === false); + + //-------------------------------------------------------------------------- + // Public API + //-------------------------------------------------------------------------- + + return { + + "IfStatement:exit": allowElseIf ? checkIfWithoutElse : checkIfWithElse + + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-empty-character-class.js b/node_modules/eslint/lib/rules/no-empty-character-class.js index eccee1d1a..5c8410235 100644 --- a/node_modules/eslint/lib/rules/no-empty-character-class.js +++ b/node_modules/eslint/lib/rules/no-empty-character-class.js @@ -22,62 +22,55 @@ const QUICK_TEST_REGEX = /\[\]/u; // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - docs: { - description: - "Disallow empty character classes in regular expressions", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-empty-character-class", - }, - - schema: [], - - messages: { - unexpected: "Empty class.", - }, - }, - - create(context) { - return { - "Literal[regex]"(node) { - const { pattern, flags } = node.regex; - - if (!QUICK_TEST_REGEX.test(pattern)) { - return; - } - - let regExpAST; - - try { - regExpAST = parser.parsePattern( - pattern, - 0, - pattern.length, - { - unicode: flags.includes("u"), - unicodeSets: flags.includes("v"), - }, - ); - } catch { - // Ignore regular expressions that regexpp cannot parse - return; - } - - visitRegExpAST(regExpAST, { - onCharacterClassEnter(characterClass) { - if ( - !characterClass.negate && - characterClass.elements.length === 0 - ) { - context.report({ node, messageId: "unexpected" }); - } - }, - }); - }, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow empty character classes in regular expressions", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-empty-character-class" + }, + + schema: [], + + messages: { + unexpected: "Empty class." + } + }, + + create(context) { + return { + "Literal[regex]"(node) { + const { pattern, flags } = node.regex; + + if (!QUICK_TEST_REGEX.test(pattern)) { + return; + } + + let regExpAST; + + try { + regExpAST = parser.parsePattern(pattern, 0, pattern.length, { + unicode: flags.includes("u"), + unicodeSets: flags.includes("v") + }); + } catch { + + // Ignore regular expressions that regexpp cannot parse + return; + } + + visitRegExpAST(regExpAST, { + onCharacterClassEnter(characterClass) { + if (!characterClass.negate && characterClass.elements.length === 0) { + context.report({ node, messageId: "unexpected" }); + } + } + }); + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-empty-function.js b/node_modules/eslint/lib/rules/no-empty-function.js index 57bd77890..2fcb75534 100644 --- a/node_modules/eslint/lib/rules/no-empty-function.js +++ b/node_modules/eslint/lib/rules/no-empty-function.js @@ -16,20 +16,16 @@ const astUtils = require("./utils/ast-utils"); //------------------------------------------------------------------------------ const ALLOW_OPTIONS = Object.freeze([ - "functions", - "arrowFunctions", - "generatorFunctions", - "methods", - "generatorMethods", - "getters", - "setters", - "constructors", - "asyncFunctions", - "asyncMethods", - "privateConstructors", - "protectedConstructors", - "decoratedFunctions", - "overrideMethods", + "functions", + "arrowFunctions", + "generatorFunctions", + "methods", + "generatorMethods", + "getters", + "setters", + "constructors", + "asyncFunctions", + "asyncMethods" ]); /** @@ -43,194 +39,129 @@ const ALLOW_OPTIONS = Object.freeze([ * "constructors". */ function getKind(node) { - const parent = node.parent; - let kind; - - if (node.type === "ArrowFunctionExpression") { - return "arrowFunctions"; - } - - // Detects main kind. - if (parent.type === "Property") { - if (parent.kind === "get") { - return "getters"; - } - if (parent.kind === "set") { - return "setters"; - } - kind = parent.method ? "methods" : "functions"; - } else if (parent.type === "MethodDefinition") { - if (parent.kind === "get") { - return "getters"; - } - if (parent.kind === "set") { - return "setters"; - } - if (parent.kind === "constructor") { - return "constructors"; - } - kind = "methods"; - } else { - kind = "functions"; - } - - // Detects prefix. - let prefix; - - if (node.generator) { - prefix = "generator"; - } else if (node.async) { - prefix = "async"; - } else { - return kind; - } - return prefix + kind[0].toUpperCase() + kind.slice(1); -} - -/** - * Checks if a constructor function has parameter properties. - * @param {ASTNode} node The function node to examine. - * @returns {boolean} True if the constructor has parameter properties, false otherwise. - */ -function isParameterPropertiesConstructor(node) { - return node.params.some(param => param.type === "TSParameterProperty"); + const parent = node.parent; + let kind = ""; + + if (node.type === "ArrowFunctionExpression") { + return "arrowFunctions"; + } + + // Detects main kind. + if (parent.type === "Property") { + if (parent.kind === "get") { + return "getters"; + } + if (parent.kind === "set") { + return "setters"; + } + kind = parent.method ? "methods" : "functions"; + + } else if (parent.type === "MethodDefinition") { + if (parent.kind === "get") { + return "getters"; + } + if (parent.kind === "set") { + return "setters"; + } + if (parent.kind === "constructor") { + return "constructors"; + } + kind = "methods"; + + } else { + kind = "functions"; + } + + // Detects prefix. + let prefix = ""; + + if (node.generator) { + prefix = "generator"; + } else if (node.async) { + prefix = "async"; + } else { + return kind; + } + return prefix + kind[0].toUpperCase() + kind.slice(1); } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - dialects: ["javascript", "typescript"], - language: "javascript", - hasSuggestions: true, - type: "suggestion", - - defaultOptions: [{ allow: [] }], - - docs: { - description: "Disallow empty functions", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-empty-function", - }, - - schema: [ - { - type: "object", - properties: { - allow: { - type: "array", - items: { enum: ALLOW_OPTIONS }, - uniqueItems: true, - }, - }, - additionalProperties: false, - }, - ], - - messages: { - unexpected: "Unexpected empty {{name}}.", - suggestComment: "Add comment inside empty {{name}}.", - }, - }, - - create(context) { - const [{ allow }] = context.options; - const sourceCode = context.sourceCode; - - /** - * Checks if the given function node is allowed to be empty. - * @param {ASTNode} node The function node to check. - * @returns {boolean} True if the function is allowed to be empty, false otherwise. - */ - function isAllowedEmptyFunction(node) { - const kind = getKind(node); - - if (allow.includes(kind)) { - return true; - } - - if (kind === "constructors") { - if ( - (node.parent.accessibility === "private" && - allow.includes("privateConstructors")) || - (node.parent.accessibility === "protected" && - allow.includes("protectedConstructors")) || - isParameterPropertiesConstructor(node) - ) { - return true; - } - } - - if (/(?:g|s)etters|methods$/iu.test(kind)) { - if ( - (node.parent.decorators?.length && - allow.includes("decoratedFunctions")) || - (node.parent.override && allow.includes("overrideMethods")) - ) { - return true; - } - } - - return false; - } - - /** - * Reports a given function node if the node matches the following patterns. - * - * - Not allowed by options. - * - The body is empty. - * - The body doesn't have any comments. - * @param {ASTNode} node A function node to report. This is one of - * an ArrowFunctionExpression, a FunctionDeclaration, or a - * FunctionExpression. - * @returns {void} - */ - function reportIfEmpty(node) { - const name = astUtils.getFunctionNameWithKind(node); - const innerComments = sourceCode.getTokens(node.body, { - includeComments: true, - filter: astUtils.isCommentToken, - }); - - if ( - !isAllowedEmptyFunction(node) && - node.body.type === "BlockStatement" && - node.body.body.length === 0 && - innerComments.length === 0 - ) { - context.report({ - node, - loc: node.body.loc, - messageId: "unexpected", - data: { name }, - suggest: [ - { - messageId: "suggestComment", - data: { name }, - fix(fixer) { - const range = [ - node.body.range[0] + 1, - node.body.range[1] - 1, - ]; - - return fixer.replaceTextRange( - range, - " /* empty */ ", - ); - }, - }, - ], - }); - } - } - - return { - ArrowFunctionExpression: reportIfEmpty, - FunctionDeclaration: reportIfEmpty, - FunctionExpression: reportIfEmpty, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow empty functions", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-empty-function" + }, + + schema: [ + { + type: "object", + properties: { + allow: { + type: "array", + items: { enum: ALLOW_OPTIONS }, + uniqueItems: true + } + }, + additionalProperties: false + } + ], + + messages: { + unexpected: "Unexpected empty {{name}}." + } + }, + + create(context) { + const options = context.options[0] || {}; + const allowed = options.allow || []; + + const sourceCode = context.sourceCode; + + /** + * Reports a given function node if the node matches the following patterns. + * + * - Not allowed by options. + * - The body is empty. + * - The body doesn't have any comments. + * @param {ASTNode} node A function node to report. This is one of + * an ArrowFunctionExpression, a FunctionDeclaration, or a + * FunctionExpression. + * @returns {void} + */ + function reportIfEmpty(node) { + const kind = getKind(node); + const name = astUtils.getFunctionNameWithKind(node); + const innerComments = sourceCode.getTokens(node.body, { + includeComments: true, + filter: astUtils.isCommentToken + }); + + if (!allowed.includes(kind) && + node.body.type === "BlockStatement" && + node.body.body.length === 0 && + innerComments.length === 0 + ) { + context.report({ + node, + loc: node.body.loc, + messageId: "unexpected", + data: { name } + }); + } + } + + return { + ArrowFunctionExpression: reportIfEmpty, + FunctionDeclaration: reportIfEmpty, + FunctionExpression: reportIfEmpty + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-empty-pattern.js b/node_modules/eslint/lib/rules/no-empty-pattern.js index da75ad954..fb75f6d25 100644 --- a/node_modules/eslint/lib/rules/no-empty-pattern.js +++ b/node_modules/eslint/lib/rules/no-empty-pattern.js @@ -10,76 +10,69 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", + meta: { + type: "problem", - defaultOptions: [ - { - allowObjectPatternsAsParameters: false, - }, - ], + docs: { + description: "Disallow empty destructuring patterns", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-empty-pattern" + }, - docs: { - description: "Disallow empty destructuring patterns", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-empty-pattern", - }, + schema: [ + { + type: "object", + properties: { + allowObjectPatternsAsParameters: { + type: "boolean", + default: false + } + }, + additionalProperties: false + } + ], - schema: [ - { - type: "object", - properties: { - allowObjectPatternsAsParameters: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], + messages: { + unexpected: "Unexpected empty {{type}} pattern." + } + }, - messages: { - unexpected: "Unexpected empty {{type}} pattern.", - }, - }, + create(context) { + const options = context.options[0] || {}, + allowObjectPatternsAsParameters = options.allowObjectPatternsAsParameters || false; - create(context) { - const [{ allowObjectPatternsAsParameters }] = context.options; + return { + ObjectPattern(node) { - return { - ObjectPattern(node) { - if (node.properties.length > 0) { - return; - } + if (node.properties.length > 0) { + return; + } - // Allow {} and {} = {} empty object patterns as parameters when allowObjectPatternsAsParameters is true - if ( - allowObjectPatternsAsParameters && - (astUtils.isFunction(node.parent) || - (node.parent.type === "AssignmentPattern" && - astUtils.isFunction(node.parent.parent) && - node.parent.right.type === "ObjectExpression" && - node.parent.right.properties.length === 0)) - ) { - return; - } + // Allow {} and {} = {} empty object patterns as parameters when allowObjectPatternsAsParameters is true + if ( + allowObjectPatternsAsParameters && + ( + astUtils.isFunction(node.parent) || + ( + node.parent.type === "AssignmentPattern" && + astUtils.isFunction(node.parent.parent) && + node.parent.right.type === "ObjectExpression" && + node.parent.right.properties.length === 0 + ) + ) + ) { + return; + } - context.report({ - node, - messageId: "unexpected", - data: { type: "object" }, - }); - }, - ArrayPattern(node) { - if (node.elements.length === 0) { - context.report({ - node, - messageId: "unexpected", - data: { type: "array" }, - }); - } - }, - }; - }, + context.report({ node, messageId: "unexpected", data: { type: "object" } }); + }, + ArrayPattern(node) { + if (node.elements.length === 0) { + context.report({ node, messageId: "unexpected", data: { type: "array" } }); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-empty-static-block.js b/node_modules/eslint/lib/rules/no-empty-static-block.js index 488b5ed72..81fc449b8 100644 --- a/node_modules/eslint/lib/rules/no-empty-static-block.js +++ b/node_modules/eslint/lib/rules/no-empty-static-block.js @@ -8,66 +8,40 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - hasSuggestions: true, - type: "suggestion", - - docs: { - description: "Disallow empty static blocks", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-empty-static-block", - }, - - schema: [], - - messages: { - unexpected: "Unexpected empty static block.", - suggestComment: "Add comment inside empty static block.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - return { - StaticBlock(node) { - if (node.body.length === 0) { - const openingBrace = sourceCode.getFirstToken(node, { - skip: 1, - }); - const closingBrace = sourceCode.getLastToken(node); - - if ( - sourceCode.getCommentsBefore(closingBrace).length === 0 - ) { - context.report({ - loc: { - start: openingBrace.loc.start, - end: closingBrace.loc.end, - }, - messageId: "unexpected", - suggest: [ - { - messageId: "suggestComment", - fix(fixer) { - const range = [ - openingBrace.range[1], - closingBrace.range[0], - ]; - - return fixer.replaceTextRange( - range, - " /* empty */ ", - ); - }, - }, - ], - }); - } - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow empty static blocks", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-empty-static-block" + }, + + schema: [], + + messages: { + unexpected: "Unexpected empty static block." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + + return { + StaticBlock(node) { + if (node.body.length === 0) { + const closingBrace = sourceCode.getLastToken(node); + + if (sourceCode.getCommentsBefore(closingBrace).length === 0) { + context.report({ + node, + messageId: "unexpected" + }); + } + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-empty.js b/node_modules/eslint/lib/rules/no-empty.js index dd30e4197..1c157963e 100644 --- a/node_modules/eslint/lib/rules/no-empty.js +++ b/node_modules/eslint/lib/rules/no-empty.js @@ -14,140 +14,90 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - hasSuggestions: true, - type: "suggestion", - - defaultOptions: [ - { - allowEmptyCatch: false, - }, - ], - - docs: { - description: "Disallow empty block statements", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-empty", - }, - - schema: [ - { - type: "object", - properties: { - allowEmptyCatch: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - - messages: { - unexpected: "Empty {{type}} statement.", - suggestComment: "Add comment inside empty {{type}} statement.", - }, - }, - - create(context) { - const [{ allowEmptyCatch }] = context.options; - const sourceCode = context.sourceCode; - - return { - BlockStatement(node) { - // if the body is not empty, we can just return immediately - if (node.body.length !== 0) { - return; - } - - // a function is generally allowed to be empty - if (astUtils.isFunction(node.parent)) { - return; - } - - if (allowEmptyCatch && node.parent.type === "CatchClause") { - return; - } - - // any other block is only allowed to be empty, if it contains a comment - if (sourceCode.getCommentsInside(node).length > 0) { - return; - } - - context.report({ - node, - messageId: "unexpected", - data: { type: "block" }, - suggest: [ - { - messageId: "suggestComment", - data: { type: "block" }, - fix(fixer) { - const range = [ - node.range[0] + 1, - node.range[1] - 1, - ]; - - return fixer.replaceTextRange( - range, - " /* empty */ ", - ); - }, - }, - ], - }); - }, - - SwitchStatement(node) { - if ( - typeof node.cases === "undefined" || - node.cases.length === 0 - ) { - const openingBrace = sourceCode.getTokenAfter( - node.discriminant, - astUtils.isOpeningBraceToken, - ); - - const closingBrace = sourceCode.getLastToken(node); - - if ( - sourceCode.commentsExistBetween( - openingBrace, - closingBrace, - ) - ) { - return; - } - - context.report({ - node, - loc: { - start: openingBrace.loc.start, - end: closingBrace.loc.end, - }, - messageId: "unexpected", - data: { type: "switch" }, - suggest: [ - { - messageId: "suggestComment", - data: { type: "switch" }, - fix(fixer) { - const range = [ - openingBrace.range[1], - closingBrace.range[0], - ]; - - return fixer.replaceTextRange( - range, - " /* empty */ ", - ); - }, - }, - ], - }); - } - }, - }; - }, + meta: { + hasSuggestions: true, + type: "suggestion", + + docs: { + description: "Disallow empty block statements", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-empty" + }, + + schema: [ + { + type: "object", + properties: { + allowEmptyCatch: { + type: "boolean", + default: false + } + }, + additionalProperties: false + } + ], + + messages: { + unexpected: "Empty {{type}} statement.", + suggestComment: "Add comment inside empty {{type}} statement." + } + }, + + create(context) { + const options = context.options[0] || {}, + allowEmptyCatch = options.allowEmptyCatch || false; + + const sourceCode = context.sourceCode; + + return { + BlockStatement(node) { + + // if the body is not empty, we can just return immediately + if (node.body.length !== 0) { + return; + } + + // a function is generally allowed to be empty + if (astUtils.isFunction(node.parent)) { + return; + } + + if (allowEmptyCatch && node.parent.type === "CatchClause") { + return; + } + + // any other block is only allowed to be empty, if it contains a comment + if (sourceCode.getCommentsInside(node).length > 0) { + return; + } + + context.report({ + node, + messageId: "unexpected", + data: { type: "block" }, + suggest: [ + { + messageId: "suggestComment", + data: { type: "block" }, + fix(fixer) { + const range = [node.range[0] + 1, node.range[1] - 1]; + + return fixer.replaceTextRange(range, " /* empty */ "); + } + } + ] + }); + }, + + SwitchStatement(node) { + + if (typeof node.cases === "undefined" || node.cases.length === 0) { + context.report({ node, messageId: "unexpected", data: { type: "switch" } }); + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-eq-null.js b/node_modules/eslint/lib/rules/no-eq-null.js index edda5e6c9..9252907b6 100644 --- a/node_modules/eslint/lib/rules/no-eq-null.js +++ b/node_modules/eslint/lib/rules/no-eq-null.js @@ -10,42 +10,37 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: - "Disallow `null` comparisons without type-checking operators", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-eq-null", - }, - - schema: [], - - messages: { - unexpected: "Use '===' to compare with null.", - }, - }, - - create(context) { - return { - BinaryExpression(node) { - const badOperator = - node.operator === "==" || node.operator === "!="; - - if ( - (node.right.type === "Literal" && - node.right.raw === "null" && - badOperator) || - (node.left.type === "Literal" && - node.left.raw === "null" && - badOperator) - ) { - context.report({ node, messageId: "unexpected" }); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow `null` comparisons without type-checking operators", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-eq-null" + }, + + schema: [], + + messages: { + unexpected: "Use '===' to compare with null." + } + }, + + create(context) { + + return { + + BinaryExpression(node) { + const badOperator = node.operator === "==" || node.operator === "!="; + + if (node.right.type === "Literal" && node.right.raw === "null" && badOperator || + node.left.type === "Literal" && node.left.raw === "null" && badOperator) { + context.report({ node, messageId: "unexpected" }); + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-eval.js b/node_modules/eslint/lib/rules/no-eval.js index 5d75097ab..a059526a6 100644 --- a/node_modules/eslint/lib/rules/no-eval.js +++ b/node_modules/eslint/lib/rules/no-eval.js @@ -16,9 +16,9 @@ const astUtils = require("./utils/ast-utils"); //------------------------------------------------------------------------------ const candidatesOfGlobalObject = Object.freeze([ - "global", - "window", - "globalThis", + "global", + "window", + "globalThis" ]); /** @@ -30,266 +30,257 @@ const candidatesOfGlobalObject = Object.freeze([ * the specified name's property */ function isMember(node, name) { - return astUtils.isSpecificMemberAccess(node, null, name); + return astUtils.isSpecificMemberAccess(node, null, name); } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - defaultOptions: [ - { - allowIndirect: false, - }, - ], - - docs: { - description: "Disallow the use of `eval()`", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-eval", - }, - - schema: [ - { - type: "object", - properties: { - allowIndirect: { type: "boolean" }, - }, - additionalProperties: false, - }, - ], - - messages: { - unexpected: "eval can be harmful.", - }, - }, - - create(context) { - const [{ allowIndirect }] = context.options; - const sourceCode = context.sourceCode; - let funcInfo = null; - - /** - * Pushes a `this` scope (non-arrow function, class static block, or class field initializer) information to the stack. - * Top-level scopes are handled separately. - * - * This is used in order to check whether or not `this` binding is a - * reference to the global object. - * @param {ASTNode} node A node of the scope. - * For functions, this is one of FunctionDeclaration, FunctionExpression. - * For class static blocks, this is StaticBlock. - * For class field initializers, this can be any node that is PropertyDefinition#value. - * @returns {void} - */ - function enterThisScope(node) { - const strict = sourceCode.getScope(node).isStrict; - - funcInfo = { - upper: funcInfo, - node, - strict, - isTopLevelOfScript: false, - defaultThis: false, - initialized: strict, - }; - } - - /** - * Pops a variable scope from the stack. - * @returns {void} - */ - function exitThisScope() { - funcInfo = funcInfo.upper; - } - - /** - * Reports a given node. - * - * `node` is `Identifier` or `MemberExpression`. - * The parent of `node` might be `CallExpression`. - * - * The location of the report is always `eval` `Identifier` (or possibly - * `Literal`). The type of the report is `CallExpression` if the parent is - * `CallExpression`. Otherwise, it's the given node type. - * @param {ASTNode} node A node to report. - * @returns {void} - */ - function report(node) { - const parent = node.parent; - const locationNode = - node.type === "MemberExpression" ? node.property : node; - - const reportNode = - parent.type === "CallExpression" && parent.callee === node - ? parent - : node; - - context.report({ - node: reportNode, - loc: locationNode.loc, - messageId: "unexpected", - }); - } - - /** - * Reports accesses of `eval` via the global object. - * @param {eslint-scope.Scope} globalScope The global scope. - * @returns {void} - */ - function reportAccessingEvalViaGlobalObject(globalScope) { - for (let i = 0; i < candidatesOfGlobalObject.length; ++i) { - const name = candidatesOfGlobalObject[i]; - const variable = astUtils.getVariableByName(globalScope, name); - - if (!variable) { - continue; - } - - const references = variable.references; - - for (let j = 0; j < references.length; ++j) { - const identifier = references[j].identifier; - let node = identifier.parent; - - // To detect code like `window.window.eval`. - while (isMember(node, name)) { - node = node.parent; - } - - // Reports. - if (isMember(node, "eval")) { - report(node); - } - } - } - } - - /** - * Reports all accesses of `eval` (excludes direct calls to eval). - * @param {eslint-scope.Scope} globalScope The global scope. - * @returns {void} - */ - function reportAccessingEval(globalScope) { - const variable = astUtils.getVariableByName(globalScope, "eval"); - - if (!variable) { - return; - } - - const references = variable.references; - - for (let i = 0; i < references.length; ++i) { - const reference = references[i]; - const id = reference.identifier; - - if (id.name === "eval" && !astUtils.isCallee(id)) { - // Is accessing to eval (excludes direct calls to eval) - report(id); - } - } - } - - if (allowIndirect) { - // Checks only direct calls to eval. It's simple! - return { - "CallExpression:exit"(node) { - const callee = node.callee; - - /* - * Optional call (`eval?.("code")`) is not direct eval. - * The direct eval is only step 6.a.vi of https://tc39.es/ecma262/#sec-function-calls-runtime-semantics-evaluation - * But the optional call is https://tc39.es/ecma262/#sec-optional-chaining-chain-evaluation - */ - if ( - !node.optional && - astUtils.isSpecificId(callee, "eval") - ) { - report(callee); - } - }, - }; - } - - return { - "CallExpression:exit"(node) { - const callee = node.callee; - - if (astUtils.isSpecificId(callee, "eval")) { - report(callee); - } - }, - - Program(node) { - const scope = sourceCode.getScope(node), - features = - context.languageOptions.parserOptions.ecmaFeatures || - {}, - strict = - scope.isStrict || - node.sourceType === "module" || - (features.globalReturn && - scope.childScopes[0].isStrict), - isTopLevelOfScript = - node.sourceType !== "module" && !features.globalReturn; - - funcInfo = { - upper: null, - node, - strict, - isTopLevelOfScript, - defaultThis: true, - initialized: true, - }; - }, - - "Program:exit"(node) { - const globalScope = sourceCode.getScope(node); - - exitThisScope(); - reportAccessingEval(globalScope); - reportAccessingEvalViaGlobalObject(globalScope); - }, - - FunctionDeclaration: enterThisScope, - "FunctionDeclaration:exit": exitThisScope, - FunctionExpression: enterThisScope, - "FunctionExpression:exit": exitThisScope, - "PropertyDefinition > *.value": enterThisScope, - "PropertyDefinition > *.value:exit": exitThisScope, - StaticBlock: enterThisScope, - "StaticBlock:exit": exitThisScope, - - ThisExpression(node) { - if (!isMember(node.parent, "eval")) { - return; - } - - /* - * `this.eval` is found. - * Checks whether or not the value of `this` is the global object. - */ - if (!funcInfo.initialized) { - funcInfo.initialized = true; - funcInfo.defaultThis = astUtils.isDefaultThisBinding( - funcInfo.node, - sourceCode, - ); - } - - // `this` at the top level of scripts always refers to the global object - if ( - funcInfo.isTopLevelOfScript || - (!funcInfo.strict && funcInfo.defaultThis) - ) { - // `this.eval` is possible built-in `eval`. - report(node.parent); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow the use of `eval()`", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-eval" + }, + + schema: [ + { + type: "object", + properties: { + allowIndirect: { type: "boolean", default: false } + }, + additionalProperties: false + } + ], + + messages: { + unexpected: "eval can be harmful." + } + }, + + create(context) { + const allowIndirect = Boolean( + context.options[0] && + context.options[0].allowIndirect + ); + const sourceCode = context.sourceCode; + let funcInfo = null; + + /** + * Pushes a `this` scope (non-arrow function, class static block, or class field initializer) information to the stack. + * Top-level scopes are handled separately. + * + * This is used in order to check whether or not `this` binding is a + * reference to the global object. + * @param {ASTNode} node A node of the scope. + * For functions, this is one of FunctionDeclaration, FunctionExpression. + * For class static blocks, this is StaticBlock. + * For class field initializers, this can be any node that is PropertyDefinition#value. + * @returns {void} + */ + function enterThisScope(node) { + const strict = sourceCode.getScope(node).isStrict; + + funcInfo = { + upper: funcInfo, + node, + strict, + isTopLevelOfScript: false, + defaultThis: false, + initialized: strict + }; + } + + /** + * Pops a variable scope from the stack. + * @returns {void} + */ + function exitThisScope() { + funcInfo = funcInfo.upper; + } + + /** + * Reports a given node. + * + * `node` is `Identifier` or `MemberExpression`. + * The parent of `node` might be `CallExpression`. + * + * The location of the report is always `eval` `Identifier` (or possibly + * `Literal`). The type of the report is `CallExpression` if the parent is + * `CallExpression`. Otherwise, it's the given node type. + * @param {ASTNode} node A node to report. + * @returns {void} + */ + function report(node) { + const parent = node.parent; + const locationNode = node.type === "MemberExpression" + ? node.property + : node; + + const reportNode = parent.type === "CallExpression" && parent.callee === node + ? parent + : node; + + context.report({ + node: reportNode, + loc: locationNode.loc, + messageId: "unexpected" + }); + } + + /** + * Reports accesses of `eval` via the global object. + * @param {eslint-scope.Scope} globalScope The global scope. + * @returns {void} + */ + function reportAccessingEvalViaGlobalObject(globalScope) { + for (let i = 0; i < candidatesOfGlobalObject.length; ++i) { + const name = candidatesOfGlobalObject[i]; + const variable = astUtils.getVariableByName(globalScope, name); + + if (!variable) { + continue; + } + + const references = variable.references; + + for (let j = 0; j < references.length; ++j) { + const identifier = references[j].identifier; + let node = identifier.parent; + + // To detect code like `window.window.eval`. + while (isMember(node, name)) { + node = node.parent; + } + + // Reports. + if (isMember(node, "eval")) { + report(node); + } + } + } + } + + /** + * Reports all accesses of `eval` (excludes direct calls to eval). + * @param {eslint-scope.Scope} globalScope The global scope. + * @returns {void} + */ + function reportAccessingEval(globalScope) { + const variable = astUtils.getVariableByName(globalScope, "eval"); + + if (!variable) { + return; + } + + const references = variable.references; + + for (let i = 0; i < references.length; ++i) { + const reference = references[i]; + const id = reference.identifier; + + if (id.name === "eval" && !astUtils.isCallee(id)) { + + // Is accessing to eval (excludes direct calls to eval) + report(id); + } + } + } + + if (allowIndirect) { + + // Checks only direct calls to eval. It's simple! + return { + "CallExpression:exit"(node) { + const callee = node.callee; + + /* + * Optional call (`eval?.("code")`) is not direct eval. + * The direct eval is only step 6.a.vi of https://tc39.es/ecma262/#sec-function-calls-runtime-semantics-evaluation + * But the optional call is https://tc39.es/ecma262/#sec-optional-chaining-chain-evaluation + */ + if (!node.optional && astUtils.isSpecificId(callee, "eval")) { + report(callee); + } + } + }; + } + + return { + "CallExpression:exit"(node) { + const callee = node.callee; + + if (astUtils.isSpecificId(callee, "eval")) { + report(callee); + } + }, + + Program(node) { + const scope = sourceCode.getScope(node), + features = context.parserOptions.ecmaFeatures || {}, + strict = + scope.isStrict || + node.sourceType === "module" || + (features.globalReturn && scope.childScopes[0].isStrict), + isTopLevelOfScript = node.sourceType !== "module" && !features.globalReturn; + + funcInfo = { + upper: null, + node, + strict, + isTopLevelOfScript, + defaultThis: true, + initialized: true + }; + }, + + "Program:exit"(node) { + const globalScope = sourceCode.getScope(node); + + exitThisScope(); + reportAccessingEval(globalScope); + reportAccessingEvalViaGlobalObject(globalScope); + }, + + FunctionDeclaration: enterThisScope, + "FunctionDeclaration:exit": exitThisScope, + FunctionExpression: enterThisScope, + "FunctionExpression:exit": exitThisScope, + "PropertyDefinition > *.value": enterThisScope, + "PropertyDefinition > *.value:exit": exitThisScope, + StaticBlock: enterThisScope, + "StaticBlock:exit": exitThisScope, + + ThisExpression(node) { + if (!isMember(node.parent, "eval")) { + return; + } + + /* + * `this.eval` is found. + * Checks whether or not the value of `this` is the global object. + */ + if (!funcInfo.initialized) { + funcInfo.initialized = true; + funcInfo.defaultThis = astUtils.isDefaultThisBinding( + funcInfo.node, + sourceCode + ); + } + + // `this` at the top level of scripts always refers to the global object + if (funcInfo.isTopLevelOfScript || (!funcInfo.strict && funcInfo.defaultThis)) { + + // `this.eval` is possible built-in `eval`. + report(node.parent); + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-ex-assign.js b/node_modules/eslint/lib/rules/no-ex-assign.js index 6215a5e05..d0e9febae 100644 --- a/node_modules/eslint/lib/rules/no-ex-assign.js +++ b/node_modules/eslint/lib/rules/no-ex-assign.js @@ -11,47 +11,44 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - docs: { - description: "Disallow reassigning exceptions in `catch` clauses", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-ex-assign", - }, - - schema: [], - - messages: { - unexpected: "Do not assign to the exception parameter.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - /** - * Finds and reports references that are non initializer and writable. - * @param {Variable} variable A variable to check. - * @returns {void} - */ - function checkVariable(variable) { - astUtils - .getModifyingReferences(variable.references) - .forEach(reference => { - context.report({ - node: reference.identifier, - messageId: "unexpected", - }); - }); - } - - return { - CatchClause(node) { - sourceCode.getDeclaredVariables(node).forEach(checkVariable); - }, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow reassigning exceptions in `catch` clauses", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-ex-assign" + }, + + schema: [], + + messages: { + unexpected: "Do not assign to the exception parameter." + } + }, + + create(context) { + + const sourceCode = context.sourceCode; + + /** + * Finds and reports references that are non initializer and writable. + * @param {Variable} variable A variable to check. + * @returns {void} + */ + function checkVariable(variable) { + astUtils.getModifyingReferences(variable.references).forEach(reference => { + context.report({ node: reference.identifier, messageId: "unexpected" }); + }); + } + + return { + CatchClause(node) { + sourceCode.getDeclaredVariables(node).forEach(checkVariable); + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-extend-native.js b/node_modules/eslint/lib/rules/no-extend-native.js index 4d3b8c20a..fcbb38557 100644 --- a/node_modules/eslint/lib/rules/no-extend-native.js +++ b/node_modules/eslint/lib/rules/no-extend-native.js @@ -10,171 +10,170 @@ //------------------------------------------------------------------------------ const astUtils = require("./utils/ast-utils"); +const globals = require("globals"); //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - defaultOptions: [{ exceptions: [] }], - - docs: { - description: "Disallow extending native types", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-extend-native", - }, - - schema: [ - { - type: "object", - properties: { - exceptions: { - type: "array", - items: { - type: "string", - }, - uniqueItems: true, - }, - }, - additionalProperties: false, - }, - ], - - messages: { - unexpected: - "{{builtin}} prototype is read only, properties should not be added.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - const exceptions = new Set(context.options[0].exceptions); - const modifiedBuiltins = new Set( - Object.keys(astUtils.ECMASCRIPT_GLOBALS) - .filter(builtin => builtin[0].toUpperCase() === builtin[0]) - .filter(builtin => !exceptions.has(builtin)), - ); - - /** - * Reports a lint error for the given node. - * @param {ASTNode} node The node to report. - * @param {string} builtin The name of the native builtin being extended. - * @returns {void} - */ - function reportNode(node, builtin) { - context.report({ - node, - messageId: "unexpected", - data: { - builtin, - }, - }); - } - - /** - * Check to see if the `prototype` property of the given object - * identifier node is being accessed. - * @param {ASTNode} identifierNode The Identifier representing the object - * to check. - * @returns {boolean} True if the identifier is the object of a - * MemberExpression and its `prototype` property is being accessed, - * false otherwise. - */ - function isPrototypePropertyAccessed(identifierNode) { - return Boolean( - identifierNode && - identifierNode.parent && - identifierNode.parent.type === "MemberExpression" && - identifierNode.parent.object === identifierNode && - astUtils.getStaticPropertyName(identifierNode.parent) === - "prototype", - ); - } - - /** - * Check if it's an assignment to the property of the given node. - * Example: `*.prop = 0` // the `*` is the given node. - * @param {ASTNode} node The node to check. - * @returns {boolean} True if an assignment to the property of the node. - */ - function isAssigningToPropertyOf(node) { - return ( - node.parent.type === "MemberExpression" && - node.parent.object === node && - node.parent.parent.type === "AssignmentExpression" && - node.parent.parent.left === node.parent - ); - } - - /** - * Checks if the given node is at the first argument of the method call of `Object.defineProperty()` or `Object.defineProperties()`. - * @param {ASTNode} node The node to check. - * @returns {boolean} True if the node is at the first argument of the method call of `Object.defineProperty()` or `Object.defineProperties()`. - */ - function isInDefinePropertyCall(node) { - return ( - node.parent.type === "CallExpression" && - node.parent.arguments[0] === node && - astUtils.isSpecificMemberAccess( - node.parent.callee, - "Object", - /^definePropert(?:y|ies)$/u, - ) - ); - } - - /** - * Check to see if object prototype access is part of a prototype - * extension. There are three ways a prototype can be extended: - * 1. Assignment to prototype property (Object.prototype.foo = 1) - * 2. Object.defineProperty()/Object.defineProperties() on a prototype - * If prototype extension is detected, report the AssignmentExpression - * or CallExpression node. - * @param {ASTNode} identifierNode The Identifier representing the object - * which prototype is being accessed and possibly extended. - * @returns {void} - */ - function checkAndReportPrototypeExtension(identifierNode) { - if (!isPrototypePropertyAccessed(identifierNode)) { - return; // This is not `*.prototype` access. - } - - /* - * `identifierNode.parent` is a MemberExpression `*.prototype`. - * If it's an optional member access, it may be wrapped by a `ChainExpression` node. - */ - const prototypeNode = - identifierNode.parent.parent.type === "ChainExpression" - ? identifierNode.parent.parent - : identifierNode.parent; - - if (isAssigningToPropertyOf(prototypeNode)) { - // `*.prototype` -> MemberExpression -> AssignmentExpression - reportNode(prototypeNode.parent.parent, identifierNode.name); - } else if (isInDefinePropertyCall(prototypeNode)) { - // `*.prototype` -> CallExpression - reportNode(prototypeNode.parent, identifierNode.name); - } - } - - return { - "Program:exit"(node) { - const globalScope = sourceCode.getScope(node); - - modifiedBuiltins.forEach(builtin => { - const builtinVar = globalScope.set.get(builtin); - - if (builtinVar && builtinVar.references) { - builtinVar.references - .map(ref => ref.identifier) - .forEach(checkAndReportPrototypeExtension); - } - }); - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow extending native types", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-extend-native" + }, + + schema: [ + { + type: "object", + properties: { + exceptions: { + type: "array", + items: { + type: "string" + }, + uniqueItems: true + } + }, + additionalProperties: false + } + ], + + messages: { + unexpected: "{{builtin}} prototype is read only, properties should not be added." + } + }, + + create(context) { + + const config = context.options[0] || {}; + const sourceCode = context.sourceCode; + const exceptions = new Set(config.exceptions || []); + const modifiedBuiltins = new Set( + Object.keys(globals.builtin) + .filter(builtin => builtin[0].toUpperCase() === builtin[0]) + .filter(builtin => !exceptions.has(builtin)) + ); + + /** + * Reports a lint error for the given node. + * @param {ASTNode} node The node to report. + * @param {string} builtin The name of the native builtin being extended. + * @returns {void} + */ + function reportNode(node, builtin) { + context.report({ + node, + messageId: "unexpected", + data: { + builtin + } + }); + } + + /** + * Check to see if the `prototype` property of the given object + * identifier node is being accessed. + * @param {ASTNode} identifierNode The Identifier representing the object + * to check. + * @returns {boolean} True if the identifier is the object of a + * MemberExpression and its `prototype` property is being accessed, + * false otherwise. + */ + function isPrototypePropertyAccessed(identifierNode) { + return Boolean( + identifierNode && + identifierNode.parent && + identifierNode.parent.type === "MemberExpression" && + identifierNode.parent.object === identifierNode && + astUtils.getStaticPropertyName(identifierNode.parent) === "prototype" + ); + } + + /** + * Check if it's an assignment to the property of the given node. + * Example: `*.prop = 0` // the `*` is the given node. + * @param {ASTNode} node The node to check. + * @returns {boolean} True if an assignment to the property of the node. + */ + function isAssigningToPropertyOf(node) { + return ( + node.parent.type === "MemberExpression" && + node.parent.object === node && + node.parent.parent.type === "AssignmentExpression" && + node.parent.parent.left === node.parent + ); + } + + /** + * Checks if the given node is at the first argument of the method call of `Object.defineProperty()` or `Object.defineProperties()`. + * @param {ASTNode} node The node to check. + * @returns {boolean} True if the node is at the first argument of the method call of `Object.defineProperty()` or `Object.defineProperties()`. + */ + function isInDefinePropertyCall(node) { + return ( + node.parent.type === "CallExpression" && + node.parent.arguments[0] === node && + astUtils.isSpecificMemberAccess(node.parent.callee, "Object", /^definePropert(?:y|ies)$/u) + ); + } + + /** + * Check to see if object prototype access is part of a prototype + * extension. There are three ways a prototype can be extended: + * 1. Assignment to prototype property (Object.prototype.foo = 1) + * 2. Object.defineProperty()/Object.defineProperties() on a prototype + * If prototype extension is detected, report the AssignmentExpression + * or CallExpression node. + * @param {ASTNode} identifierNode The Identifier representing the object + * which prototype is being accessed and possibly extended. + * @returns {void} + */ + function checkAndReportPrototypeExtension(identifierNode) { + if (!isPrototypePropertyAccessed(identifierNode)) { + return; // This is not `*.prototype` access. + } + + /* + * `identifierNode.parent` is a MemberExpression `*.prototype`. + * If it's an optional member access, it may be wrapped by a `ChainExpression` node. + */ + const prototypeNode = + identifierNode.parent.parent.type === "ChainExpression" + ? identifierNode.parent.parent + : identifierNode.parent; + + if (isAssigningToPropertyOf(prototypeNode)) { + + // `*.prototype` -> MemberExpression -> AssignmentExpression + reportNode(prototypeNode.parent.parent, identifierNode.name); + } else if (isInDefinePropertyCall(prototypeNode)) { + + // `*.prototype` -> CallExpression + reportNode(prototypeNode.parent, identifierNode.name); + } + } + + return { + + "Program:exit"(node) { + const globalScope = sourceCode.getScope(node); + + modifiedBuiltins.forEach(builtin => { + const builtinVar = globalScope.set.get(builtin); + + if (builtinVar && builtinVar.references) { + builtinVar.references + .map(ref => ref.identifier) + .forEach(checkAndReportPrototypeExtension); + } + }); + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-extra-bind.js b/node_modules/eslint/lib/rules/no-extra-bind.js index 73164f8d4..e1e72b0c7 100644 --- a/node_modules/eslint/lib/rules/no-extra-bind.js +++ b/node_modules/eslint/lib/rules/no-extra-bind.js @@ -14,211 +14,200 @@ const astUtils = require("./utils/ast-utils"); // Helpers //------------------------------------------------------------------------------ -const SIDE_EFFECT_FREE_NODE_TYPES = new Set([ - "Literal", - "Identifier", - "ThisExpression", - "FunctionExpression", -]); +const SIDE_EFFECT_FREE_NODE_TYPES = new Set(["Literal", "Identifier", "ThisExpression", "FunctionExpression"]); //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: "Disallow unnecessary calls to `.bind()`", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-extra-bind", - }, - - schema: [], - fixable: "code", - - messages: { - unexpected: "The function binding is unnecessary.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - let scopeInfo = null; - - /** - * Checks if a node is free of side effects. - * - * This check is stricter than it needs to be, in order to keep the implementation simple. - * @param {ASTNode} node A node to check. - * @returns {boolean} True if the node is known to be side-effect free, false otherwise. - */ - function isSideEffectFree(node) { - return SIDE_EFFECT_FREE_NODE_TYPES.has(node.type); - } - - /** - * Reports a given function node. - * @param {ASTNode} node A node to report. This is a FunctionExpression or - * an ArrowFunctionExpression. - * @returns {void} - */ - function report(node) { - const memberNode = node.parent; - const callNode = - memberNode.parent.type === "ChainExpression" - ? memberNode.parent.parent - : memberNode.parent; - - context.report({ - node: callNode, - messageId: "unexpected", - loc: memberNode.property.loc, - - fix(fixer) { - if (!isSideEffectFree(callNode.arguments[0])) { - return null; - } - - /* - * The list of the first/last token pair of a removal range. - * This is two parts because closing parentheses may exist between the method name and arguments. - * E.g. `(function(){}.bind ) (obj)` - * ^^^^^ ^^^^^ < removal ranges - * E.g. `(function(){}?.['bind'] ) ?.(obj)` - * ^^^^^^^^^^ ^^^^^^^ < removal ranges - */ - const tokenPairs = [ - [ - // `.`, `?.`, or `[` token. - sourceCode.getTokenAfter( - memberNode.object, - astUtils.isNotClosingParenToken, - ), - - // property name or `]` token. - sourceCode.getLastToken(memberNode), - ], - [ - // `?.` or `(` token of arguments. - sourceCode.getTokenAfter( - memberNode, - astUtils.isNotClosingParenToken, - ), - - // `)` token of arguments. - sourceCode.getLastToken(callNode), - ], - ]; - const firstTokenToRemove = tokenPairs[0][0]; - const lastTokenToRemove = tokenPairs[1][1]; - - if ( - sourceCode.commentsExistBetween( - firstTokenToRemove, - lastTokenToRemove, - ) - ) { - return null; - } - - return tokenPairs.map(([start, end]) => - fixer.removeRange([start.range[0], end.range[1]]), - ); - }, - }); - } - - /** - * Checks whether or not a given function node is the callee of `.bind()` - * method. - * - * e.g. `(function() {}.bind(foo))` - * @param {ASTNode} node A node to report. This is a FunctionExpression or - * an ArrowFunctionExpression. - * @returns {boolean} `true` if the node is the callee of `.bind()` method. - */ - function isCalleeOfBindMethod(node) { - if (!astUtils.isSpecificMemberAccess(node.parent, null, "bind")) { - return false; - } - - // The node of `*.bind` member access. - const bindNode = - node.parent.parent.type === "ChainExpression" - ? node.parent.parent - : node.parent; - - return ( - bindNode.parent.type === "CallExpression" && - bindNode.parent.callee === bindNode && - bindNode.parent.arguments.length === 1 && - bindNode.parent.arguments[0].type !== "SpreadElement" - ); - } - - /** - * Adds a scope information object to the stack. - * @param {ASTNode} node A node to add. This node is a FunctionExpression - * or a FunctionDeclaration node. - * @returns {void} - */ - function enterFunction(node) { - scopeInfo = { - isBound: isCalleeOfBindMethod(node), - thisFound: false, - upper: scopeInfo, - }; - } - - /** - * Removes the scope information object from the top of the stack. - * At the same time, this reports the function node if the function has - * `.bind()` and the `this` keywords found. - * @param {ASTNode} node A node to remove. This node is a - * FunctionExpression or a FunctionDeclaration node. - * @returns {void} - */ - function exitFunction(node) { - if (scopeInfo.isBound && !scopeInfo.thisFound) { - report(node); - } - - scopeInfo = scopeInfo.upper; - } - - /** - * Reports a given arrow function if the function is callee of `.bind()` - * method. - * @param {ASTNode} node A node to report. This node is an - * ArrowFunctionExpression. - * @returns {void} - */ - function exitArrowFunction(node) { - if (isCalleeOfBindMethod(node)) { - report(node); - } - } - - /** - * Set the mark as the `this` keyword was found in this scope. - * @returns {void} - */ - function markAsThisFound() { - if (scopeInfo) { - scopeInfo.thisFound = true; - } - } - - return { - "ArrowFunctionExpression:exit": exitArrowFunction, - FunctionDeclaration: enterFunction, - "FunctionDeclaration:exit": exitFunction, - FunctionExpression: enterFunction, - "FunctionExpression:exit": exitFunction, - ThisExpression: markAsThisFound, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow unnecessary calls to `.bind()`", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-extra-bind" + }, + + schema: [], + fixable: "code", + + messages: { + unexpected: "The function binding is unnecessary." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + let scopeInfo = null; + + /** + * Checks if a node is free of side effects. + * + * This check is stricter than it needs to be, in order to keep the implementation simple. + * @param {ASTNode} node A node to check. + * @returns {boolean} True if the node is known to be side-effect free, false otherwise. + */ + function isSideEffectFree(node) { + return SIDE_EFFECT_FREE_NODE_TYPES.has(node.type); + } + + /** + * Reports a given function node. + * @param {ASTNode} node A node to report. This is a FunctionExpression or + * an ArrowFunctionExpression. + * @returns {void} + */ + function report(node) { + const memberNode = node.parent; + const callNode = memberNode.parent.type === "ChainExpression" + ? memberNode.parent.parent + : memberNode.parent; + + context.report({ + node: callNode, + messageId: "unexpected", + loc: memberNode.property.loc, + + fix(fixer) { + if (!isSideEffectFree(callNode.arguments[0])) { + return null; + } + + /* + * The list of the first/last token pair of a removal range. + * This is two parts because closing parentheses may exist between the method name and arguments. + * E.g. `(function(){}.bind ) (obj)` + * ^^^^^ ^^^^^ < removal ranges + * E.g. `(function(){}?.['bind'] ) ?.(obj)` + * ^^^^^^^^^^ ^^^^^^^ < removal ranges + */ + const tokenPairs = [ + [ + + // `.`, `?.`, or `[` token. + sourceCode.getTokenAfter( + memberNode.object, + astUtils.isNotClosingParenToken + ), + + // property name or `]` token. + sourceCode.getLastToken(memberNode) + ], + [ + + // `?.` or `(` token of arguments. + sourceCode.getTokenAfter( + memberNode, + astUtils.isNotClosingParenToken + ), + + // `)` token of arguments. + sourceCode.getLastToken(callNode) + ] + ]; + const firstTokenToRemove = tokenPairs[0][0]; + const lastTokenToRemove = tokenPairs[1][1]; + + if (sourceCode.commentsExistBetween(firstTokenToRemove, lastTokenToRemove)) { + return null; + } + + return tokenPairs.map(([start, end]) => + fixer.removeRange([start.range[0], end.range[1]])); + } + }); + } + + /** + * Checks whether or not a given function node is the callee of `.bind()` + * method. + * + * e.g. `(function() {}.bind(foo))` + * @param {ASTNode} node A node to report. This is a FunctionExpression or + * an ArrowFunctionExpression. + * @returns {boolean} `true` if the node is the callee of `.bind()` method. + */ + function isCalleeOfBindMethod(node) { + if (!astUtils.isSpecificMemberAccess(node.parent, null, "bind")) { + return false; + } + + // The node of `*.bind` member access. + const bindNode = node.parent.parent.type === "ChainExpression" + ? node.parent.parent + : node.parent; + + return ( + bindNode.parent.type === "CallExpression" && + bindNode.parent.callee === bindNode && + bindNode.parent.arguments.length === 1 && + bindNode.parent.arguments[0].type !== "SpreadElement" + ); + } + + /** + * Adds a scope information object to the stack. + * @param {ASTNode} node A node to add. This node is a FunctionExpression + * or a FunctionDeclaration node. + * @returns {void} + */ + function enterFunction(node) { + scopeInfo = { + isBound: isCalleeOfBindMethod(node), + thisFound: false, + upper: scopeInfo + }; + } + + /** + * Removes the scope information object from the top of the stack. + * At the same time, this reports the function node if the function has + * `.bind()` and the `this` keywords found. + * @param {ASTNode} node A node to remove. This node is a + * FunctionExpression or a FunctionDeclaration node. + * @returns {void} + */ + function exitFunction(node) { + if (scopeInfo.isBound && !scopeInfo.thisFound) { + report(node); + } + + scopeInfo = scopeInfo.upper; + } + + /** + * Reports a given arrow function if the function is callee of `.bind()` + * method. + * @param {ASTNode} node A node to report. This node is an + * ArrowFunctionExpression. + * @returns {void} + */ + function exitArrowFunction(node) { + if (isCalleeOfBindMethod(node)) { + report(node); + } + } + + /** + * Set the mark as the `this` keyword was found in this scope. + * @returns {void} + */ + function markAsThisFound() { + if (scopeInfo) { + scopeInfo.thisFound = true; + } + } + + return { + "ArrowFunctionExpression:exit": exitArrowFunction, + FunctionDeclaration: enterFunction, + "FunctionDeclaration:exit": exitFunction, + FunctionExpression: enterFunction, + "FunctionExpression:exit": exitFunction, + ThisExpression: markAsThisFound + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-extra-boolean-cast.js b/node_modules/eslint/lib/rules/no-extra-boolean-cast.js index 18019a8cd..f342533bf 100644 --- a/node_modules/eslint/lib/rules/no-extra-boolean-cast.js +++ b/node_modules/eslint/lib/rules/no-extra-boolean-cast.js @@ -18,403 +18,300 @@ const precedence = astUtils.getPrecedence; // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - defaultOptions: [{}], - - docs: { - description: "Disallow unnecessary boolean casts", - recommended: true, - frozen: true, - url: "https://eslint.org/docs/latest/rules/no-extra-boolean-cast", - }, - - schema: [ - { - anyOf: [ - { - type: "object", - properties: { - enforceForInnerExpressions: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - - // deprecated - { - type: "object", - properties: { - enforceForLogicalOperands: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - }, - ], - fixable: "code", - - messages: { - unexpectedCall: "Redundant Boolean call.", - unexpectedNegation: "Redundant double negation.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - const [{ enforceForLogicalOperands, enforceForInnerExpressions }] = - context.options; - - // Node types which have a test which will coerce values to booleans. - const BOOLEAN_NODE_TYPES = new Set([ - "IfStatement", - "DoWhileStatement", - "WhileStatement", - "ConditionalExpression", - "ForStatement", - ]); - - /** - * Check if a node is a Boolean function or constructor. - * @param {ASTNode} node the node - * @returns {boolean} If the node is Boolean function or constructor - */ - function isBooleanFunctionOrConstructorCall(node) { - // Boolean() and new Boolean() - return ( - (node.type === "CallExpression" || - node.type === "NewExpression") && - node.callee.type === "Identifier" && - node.callee.name === "Boolean" - ); - } - - /** - * Check if a node is in a context where its value would be coerced to a boolean at runtime. - * @param {ASTNode} node The node - * @returns {boolean} If it is in a boolean context - */ - function isInBooleanContext(node) { - return ( - (isBooleanFunctionOrConstructorCall(node.parent) && - node === node.parent.arguments[0]) || - (BOOLEAN_NODE_TYPES.has(node.parent.type) && - node === node.parent.test) || - // ! - (node.parent.type === "UnaryExpression" && - node.parent.operator === "!") - ); - } - - /** - * Checks whether the node is a context that should report an error - * Acts recursively if it is in a logical context - * @param {ASTNode} node the node - * @returns {boolean} If the node is in one of the flagged contexts - */ - function isInFlaggedContext(node) { - if (node.parent.type === "ChainExpression") { - return isInFlaggedContext(node.parent); - } - - /* - * legacy behavior - enforceForLogicalOperands will only recurse on - * logical expressions, not on other contexts. - * enforceForInnerExpressions will recurse on logical expressions - * as well as the other recursive syntaxes. - */ - - if (enforceForLogicalOperands || enforceForInnerExpressions) { - if (node.parent.type === "LogicalExpression") { - if ( - node.parent.operator === "||" || - node.parent.operator === "&&" - ) { - return isInFlaggedContext(node.parent); - } - - // Check the right hand side of a `??` operator. - if ( - enforceForInnerExpressions && - node.parent.operator === "??" && - node.parent.right === node - ) { - return isInFlaggedContext(node.parent); - } - } - } - - if (enforceForInnerExpressions) { - if ( - node.parent.type === "ConditionalExpression" && - (node.parent.consequent === node || - node.parent.alternate === node) - ) { - return isInFlaggedContext(node.parent); - } - - /* - * Check last expression only in a sequence, i.e. if ((1, 2, Boolean(3))) {}, since - * the others don't affect the result of the expression. - */ - if ( - node.parent.type === "SequenceExpression" && - node.parent.expressions.at(-1) === node - ) { - return isInFlaggedContext(node.parent); - } - } - - return isInBooleanContext(node); - } - - /** - * Check if a node has comments inside. - * @param {ASTNode} node The node to check. - * @returns {boolean} `true` if it has comments inside. - */ - function hasCommentsInside(node) { - return Boolean(sourceCode.getCommentsInside(node).length); - } - - /** - * Checks if the given node is wrapped in grouping parentheses. Parentheses for constructs such as if() don't count. - * @param {ASTNode} node The node to check. - * @returns {boolean} `true` if the node is parenthesized. - * @private - */ - function isParenthesized(node) { - return eslintUtils.isParenthesized(1, node, sourceCode); - } - - /** - * Determines whether the given node needs to be parenthesized when replacing the previous node. - * It assumes that `previousNode` is the node to be reported by this rule, so it has a limited list - * of possible parent node types. By the same assumption, the node's role in a particular parent is already known. - * @param {ASTNode} previousNode Previous node. - * @param {ASTNode} node The node to check. - * @throws {Error} (Unreachable.) - * @returns {boolean} `true` if the node needs to be parenthesized. - */ - function needsParens(previousNode, node) { - if (previousNode.parent.type === "ChainExpression") { - return needsParens(previousNode.parent, node); - } - - if (isParenthesized(previousNode)) { - // parentheses around the previous node will stay, so there is no need for an additional pair - return false; - } - - // parent of the previous node will become parent of the replacement node - const parent = previousNode.parent; - - switch (parent.type) { - case "CallExpression": - case "NewExpression": - return node.type === "SequenceExpression"; - case "IfStatement": - case "DoWhileStatement": - case "WhileStatement": - case "ForStatement": - case "SequenceExpression": - return false; - case "ConditionalExpression": - if (previousNode === parent.test) { - return precedence(node) <= precedence(parent); - } - if ( - previousNode === parent.consequent || - previousNode === parent.alternate - ) { - return ( - precedence(node) < - precedence({ type: "AssignmentExpression" }) - ); - } - - /* c8 ignore next */ - throw new Error( - "Ternary child must be test, consequent, or alternate.", - ); - case "UnaryExpression": - return precedence(node) < precedence(parent); - case "LogicalExpression": - if ( - astUtils.isMixedLogicalAndCoalesceExpressions( - node, - parent, - ) - ) { - return true; - } - if (previousNode === parent.left) { - return precedence(node) < precedence(parent); - } - return precedence(node) <= precedence(parent); - - /* c8 ignore next */ - default: - throw new Error(`Unexpected parent type: ${parent.type}`); - } - } - - return { - UnaryExpression(node) { - const parent = node.parent; - - // Exit early if it's guaranteed not to match - if ( - node.operator !== "!" || - parent.type !== "UnaryExpression" || - parent.operator !== "!" - ) { - return; - } - - if (isInFlaggedContext(parent)) { - context.report({ - node: parent, - messageId: "unexpectedNegation", - fix(fixer) { - if (hasCommentsInside(parent)) { - return null; - } - - if (needsParens(parent, node.argument)) { - return fixer.replaceText( - parent, - `(${sourceCode.getText(node.argument)})`, - ); - } - - let prefix = ""; - const tokenBefore = - sourceCode.getTokenBefore(parent); - const firstReplacementToken = - sourceCode.getFirstToken(node.argument); - - if ( - tokenBefore && - tokenBefore.range[1] === parent.range[0] && - !astUtils.canTokensBeAdjacent( - tokenBefore, - firstReplacementToken, - ) - ) { - prefix = " "; - } - - return fixer.replaceText( - parent, - prefix + sourceCode.getText(node.argument), - ); - }, - }); - } - }, - - CallExpression(node) { - if ( - node.callee.type !== "Identifier" || - node.callee.name !== "Boolean" - ) { - return; - } - - if (isInFlaggedContext(node)) { - context.report({ - node, - messageId: "unexpectedCall", - fix(fixer) { - const parent = node.parent; - - if (node.arguments.length === 0) { - if ( - parent.type === "UnaryExpression" && - parent.operator === "!" - ) { - /* - * !Boolean() -> true - */ - - if (hasCommentsInside(parent)) { - return null; - } - - const replacement = "true"; - let prefix = ""; - const tokenBefore = - sourceCode.getTokenBefore(parent); - - if ( - tokenBefore && - tokenBefore.range[1] === - parent.range[0] && - !astUtils.canTokensBeAdjacent( - tokenBefore, - replacement, - ) - ) { - prefix = " "; - } - - return fixer.replaceText( - parent, - prefix + replacement, - ); - } - - /* - * Boolean() -> false - */ - - if (hasCommentsInside(node)) { - return null; - } - - return fixer.replaceText(node, "false"); - } - - if (node.arguments.length === 1) { - const argument = node.arguments[0]; - - if ( - argument.type === "SpreadElement" || - hasCommentsInside(node) - ) { - return null; - } - - /* - * Boolean(expression) -> expression - */ - - if (needsParens(node, argument)) { - return fixer.replaceText( - node, - `(${sourceCode.getText(argument)})`, - ); - } - - return fixer.replaceText( - node, - sourceCode.getText(argument), - ); - } - - // two or more arguments - return null; - }, - }); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow unnecessary boolean casts", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-extra-boolean-cast" + }, + + schema: [{ + type: "object", + properties: { + enforceForLogicalOperands: { + type: "boolean", + default: false + } + }, + additionalProperties: false + }], + fixable: "code", + + messages: { + unexpectedCall: "Redundant Boolean call.", + unexpectedNegation: "Redundant double negation." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + + // Node types which have a test which will coerce values to booleans. + const BOOLEAN_NODE_TYPES = new Set([ + "IfStatement", + "DoWhileStatement", + "WhileStatement", + "ConditionalExpression", + "ForStatement" + ]); + + /** + * Check if a node is a Boolean function or constructor. + * @param {ASTNode} node the node + * @returns {boolean} If the node is Boolean function or constructor + */ + function isBooleanFunctionOrConstructorCall(node) { + + // Boolean() and new Boolean() + return (node.type === "CallExpression" || node.type === "NewExpression") && + node.callee.type === "Identifier" && + node.callee.name === "Boolean"; + } + + /** + * Checks whether the node is a logical expression and that the option is enabled + * @param {ASTNode} node the node + * @returns {boolean} if the node is a logical expression and option is enabled + */ + function isLogicalContext(node) { + return node.type === "LogicalExpression" && + (node.operator === "||" || node.operator === "&&") && + (context.options.length && context.options[0].enforceForLogicalOperands === true); + + } + + + /** + * Check if a node is in a context where its value would be coerced to a boolean at runtime. + * @param {ASTNode} node The node + * @returns {boolean} If it is in a boolean context + */ + function isInBooleanContext(node) { + return ( + (isBooleanFunctionOrConstructorCall(node.parent) && + node === node.parent.arguments[0]) || + + (BOOLEAN_NODE_TYPES.has(node.parent.type) && + node === node.parent.test) || + + // ! + (node.parent.type === "UnaryExpression" && + node.parent.operator === "!") + ); + } + + /** + * Checks whether the node is a context that should report an error + * Acts recursively if it is in a logical context + * @param {ASTNode} node the node + * @returns {boolean} If the node is in one of the flagged contexts + */ + function isInFlaggedContext(node) { + if (node.parent.type === "ChainExpression") { + return isInFlaggedContext(node.parent); + } + + return isInBooleanContext(node) || + (isLogicalContext(node.parent) && + + // For nested logical statements + isInFlaggedContext(node.parent) + ); + } + + + /** + * Check if a node has comments inside. + * @param {ASTNode} node The node to check. + * @returns {boolean} `true` if it has comments inside. + */ + function hasCommentsInside(node) { + return Boolean(sourceCode.getCommentsInside(node).length); + } + + /** + * Checks if the given node is wrapped in grouping parentheses. Parentheses for constructs such as if() don't count. + * @param {ASTNode} node The node to check. + * @returns {boolean} `true` if the node is parenthesized. + * @private + */ + function isParenthesized(node) { + return eslintUtils.isParenthesized(1, node, sourceCode); + } + + /** + * Determines whether the given node needs to be parenthesized when replacing the previous node. + * It assumes that `previousNode` is the node to be reported by this rule, so it has a limited list + * of possible parent node types. By the same assumption, the node's role in a particular parent is already known. + * For example, if the parent is `ConditionalExpression`, `previousNode` must be its `test` child. + * @param {ASTNode} previousNode Previous node. + * @param {ASTNode} node The node to check. + * @throws {Error} (Unreachable.) + * @returns {boolean} `true` if the node needs to be parenthesized. + */ + function needsParens(previousNode, node) { + if (previousNode.parent.type === "ChainExpression") { + return needsParens(previousNode.parent, node); + } + if (isParenthesized(previousNode)) { + + // parentheses around the previous node will stay, so there is no need for an additional pair + return false; + } + + // parent of the previous node will become parent of the replacement node + const parent = previousNode.parent; + + switch (parent.type) { + case "CallExpression": + case "NewExpression": + return node.type === "SequenceExpression"; + case "IfStatement": + case "DoWhileStatement": + case "WhileStatement": + case "ForStatement": + return false; + case "ConditionalExpression": + return precedence(node) <= precedence(parent); + case "UnaryExpression": + return precedence(node) < precedence(parent); + case "LogicalExpression": + if (astUtils.isMixedLogicalAndCoalesceExpressions(node, parent)) { + return true; + } + if (previousNode === parent.left) { + return precedence(node) < precedence(parent); + } + return precedence(node) <= precedence(parent); + + /* c8 ignore next */ + default: + throw new Error(`Unexpected parent type: ${parent.type}`); + } + } + + return { + UnaryExpression(node) { + const parent = node.parent; + + + // Exit early if it's guaranteed not to match + if (node.operator !== "!" || + parent.type !== "UnaryExpression" || + parent.operator !== "!") { + return; + } + + + if (isInFlaggedContext(parent)) { + context.report({ + node: parent, + messageId: "unexpectedNegation", + fix(fixer) { + if (hasCommentsInside(parent)) { + return null; + } + + if (needsParens(parent, node.argument)) { + return fixer.replaceText(parent, `(${sourceCode.getText(node.argument)})`); + } + + let prefix = ""; + const tokenBefore = sourceCode.getTokenBefore(parent); + const firstReplacementToken = sourceCode.getFirstToken(node.argument); + + if ( + tokenBefore && + tokenBefore.range[1] === parent.range[0] && + !astUtils.canTokensBeAdjacent(tokenBefore, firstReplacementToken) + ) { + prefix = " "; + } + + return fixer.replaceText(parent, prefix + sourceCode.getText(node.argument)); + } + }); + } + }, + + CallExpression(node) { + if (node.callee.type !== "Identifier" || node.callee.name !== "Boolean") { + return; + } + + if (isInFlaggedContext(node)) { + context.report({ + node, + messageId: "unexpectedCall", + fix(fixer) { + const parent = node.parent; + + if (node.arguments.length === 0) { + if (parent.type === "UnaryExpression" && parent.operator === "!") { + + /* + * !Boolean() -> true + */ + + if (hasCommentsInside(parent)) { + return null; + } + + const replacement = "true"; + let prefix = ""; + const tokenBefore = sourceCode.getTokenBefore(parent); + + if ( + tokenBefore && + tokenBefore.range[1] === parent.range[0] && + !astUtils.canTokensBeAdjacent(tokenBefore, replacement) + ) { + prefix = " "; + } + + return fixer.replaceText(parent, prefix + replacement); + } + + /* + * Boolean() -> false + */ + + if (hasCommentsInside(node)) { + return null; + } + + return fixer.replaceText(node, "false"); + } + + if (node.arguments.length === 1) { + const argument = node.arguments[0]; + + if (argument.type === "SpreadElement" || hasCommentsInside(node)) { + return null; + } + + /* + * Boolean(expression) -> expression + */ + + if (needsParens(node, argument)) { + return fixer.replaceText(node, `(${sourceCode.getText(argument)})`); + } + + return fixer.replaceText(node, sourceCode.getText(argument)); + } + + // two or more arguments + return null; + } + }); + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-extra-label.js b/node_modules/eslint/lib/rules/no-extra-label.js index c191eee7f..45ff441d0 100644 --- a/node_modules/eslint/lib/rules/no-extra-label.js +++ b/node_modules/eslint/lib/rules/no-extra-label.js @@ -15,155 +15,135 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: "Disallow unnecessary labels", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/no-extra-label", - }, - - schema: [], - fixable: "code", - - messages: { - unexpected: "This label '{{name}}' is unnecessary.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - let scopeInfo = null; - - /** - * Creates a new scope with a breakable statement. - * @param {ASTNode} node A node to create. This is a BreakableStatement. - * @returns {void} - */ - function enterBreakableStatement(node) { - scopeInfo = { - label: - node.parent.type === "LabeledStatement" - ? node.parent.label - : null, - breakable: true, - upper: scopeInfo, - }; - } - - /** - * Removes the top scope of the stack. - * @returns {void} - */ - function exitBreakableStatement() { - scopeInfo = scopeInfo.upper; - } - - /** - * Creates a new scope with a labeled statement. - * - * This ignores it if the body is a breakable statement. - * In this case it's handled in the `enterBreakableStatement` function. - * @param {ASTNode} node A node to create. This is a LabeledStatement. - * @returns {void} - */ - function enterLabeledStatement(node) { - if (!astUtils.isBreakableStatement(node.body)) { - scopeInfo = { - label: node.label, - breakable: false, - upper: scopeInfo, - }; - } - } - - /** - * Removes the top scope of the stack. - * - * This ignores it if the body is a breakable statement. - * In this case it's handled in the `exitBreakableStatement` function. - * @param {ASTNode} node A node. This is a LabeledStatement. - * @returns {void} - */ - function exitLabeledStatement(node) { - if (!astUtils.isBreakableStatement(node.body)) { - scopeInfo = scopeInfo.upper; - } - } - - /** - * Reports a given control node if it's unnecessary. - * @param {ASTNode} node A node. This is a BreakStatement or a - * ContinueStatement. - * @returns {void} - */ - function reportIfUnnecessary(node) { - if (!node.label) { - return; - } - - const labelNode = node.label; - - for (let info = scopeInfo; info !== null; info = info.upper) { - if ( - info.breakable || - (info.label && info.label.name === labelNode.name) - ) { - if ( - info.breakable && - info.label && - info.label.name === labelNode.name - ) { - context.report({ - node: labelNode, - messageId: "unexpected", - data: labelNode, - fix(fixer) { - const breakOrContinueToken = - sourceCode.getFirstToken(node); - - if ( - sourceCode.commentsExistBetween( - breakOrContinueToken, - labelNode, - ) - ) { - return null; - } - - return fixer.removeRange([ - breakOrContinueToken.range[1], - labelNode.range[1], - ]); - }, - }); - } - return; - } - } - } - - return { - WhileStatement: enterBreakableStatement, - "WhileStatement:exit": exitBreakableStatement, - DoWhileStatement: enterBreakableStatement, - "DoWhileStatement:exit": exitBreakableStatement, - ForStatement: enterBreakableStatement, - "ForStatement:exit": exitBreakableStatement, - ForInStatement: enterBreakableStatement, - "ForInStatement:exit": exitBreakableStatement, - ForOfStatement: enterBreakableStatement, - "ForOfStatement:exit": exitBreakableStatement, - SwitchStatement: enterBreakableStatement, - "SwitchStatement:exit": exitBreakableStatement, - LabeledStatement: enterLabeledStatement, - "LabeledStatement:exit": exitLabeledStatement, - BreakStatement: reportIfUnnecessary, - ContinueStatement: reportIfUnnecessary, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow unnecessary labels", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-extra-label" + }, + + schema: [], + fixable: "code", + + messages: { + unexpected: "This label '{{name}}' is unnecessary." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + let scopeInfo = null; + + /** + * Creates a new scope with a breakable statement. + * @param {ASTNode} node A node to create. This is a BreakableStatement. + * @returns {void} + */ + function enterBreakableStatement(node) { + scopeInfo = { + label: node.parent.type === "LabeledStatement" ? node.parent.label : null, + breakable: true, + upper: scopeInfo + }; + } + + /** + * Removes the top scope of the stack. + * @returns {void} + */ + function exitBreakableStatement() { + scopeInfo = scopeInfo.upper; + } + + /** + * Creates a new scope with a labeled statement. + * + * This ignores it if the body is a breakable statement. + * In this case it's handled in the `enterBreakableStatement` function. + * @param {ASTNode} node A node to create. This is a LabeledStatement. + * @returns {void} + */ + function enterLabeledStatement(node) { + if (!astUtils.isBreakableStatement(node.body)) { + scopeInfo = { + label: node.label, + breakable: false, + upper: scopeInfo + }; + } + } + + /** + * Removes the top scope of the stack. + * + * This ignores it if the body is a breakable statement. + * In this case it's handled in the `exitBreakableStatement` function. + * @param {ASTNode} node A node. This is a LabeledStatement. + * @returns {void} + */ + function exitLabeledStatement(node) { + if (!astUtils.isBreakableStatement(node.body)) { + scopeInfo = scopeInfo.upper; + } + } + + /** + * Reports a given control node if it's unnecessary. + * @param {ASTNode} node A node. This is a BreakStatement or a + * ContinueStatement. + * @returns {void} + */ + function reportIfUnnecessary(node) { + if (!node.label) { + return; + } + + const labelNode = node.label; + + for (let info = scopeInfo; info !== null; info = info.upper) { + if (info.breakable || info.label && info.label.name === labelNode.name) { + if (info.breakable && info.label && info.label.name === labelNode.name) { + context.report({ + node: labelNode, + messageId: "unexpected", + data: labelNode, + fix(fixer) { + const breakOrContinueToken = sourceCode.getFirstToken(node); + + if (sourceCode.commentsExistBetween(breakOrContinueToken, labelNode)) { + return null; + } + + return fixer.removeRange([breakOrContinueToken.range[1], labelNode.range[1]]); + } + }); + } + return; + } + } + } + + return { + WhileStatement: enterBreakableStatement, + "WhileStatement:exit": exitBreakableStatement, + DoWhileStatement: enterBreakableStatement, + "DoWhileStatement:exit": exitBreakableStatement, + ForStatement: enterBreakableStatement, + "ForStatement:exit": exitBreakableStatement, + ForInStatement: enterBreakableStatement, + "ForInStatement:exit": exitBreakableStatement, + ForOfStatement: enterBreakableStatement, + "ForOfStatement:exit": exitBreakableStatement, + SwitchStatement: enterBreakableStatement, + "SwitchStatement:exit": exitBreakableStatement, + LabeledStatement: enterLabeledStatement, + "LabeledStatement:exit": exitLabeledStatement, + BreakStatement: reportIfUnnecessary, + ContinueStatement: reportIfUnnecessary + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-extra-parens.js b/node_modules/eslint/lib/rules/no-extra-parens.js index 5b534b67f..75c082baf 100644 --- a/node_modules/eslint/lib/rules/no-extra-parens.js +++ b/node_modules/eslint/lib/rules/no-extra-parens.js @@ -9,1661 +9,1314 @@ // Rule Definition //------------------------------------------------------------------------------ -const { - isParenthesized: isParenthesizedRaw, -} = require("@eslint-community/eslint-utils"); +const { isParenthesized: isParenthesizedRaw } = require("@eslint-community/eslint-utils"); const astUtils = require("./utils/ast-utils.js"); -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "no-extra-parens", - url: "https://eslint.style/rules/no-extra-parens", - }, - }, - ], - }, - type: "layout", - - docs: { - description: "Disallow unnecessary parentheses", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-extra-parens", - }, - - fixable: "code", - - schema: { - anyOf: [ - { - type: "array", - items: [ - { - enum: ["functions"], - }, - ], - minItems: 0, - maxItems: 1, - }, - { - type: "array", - items: [ - { - enum: ["all"], - }, - { - type: "object", - properties: { - conditionalAssign: { type: "boolean" }, - ternaryOperandBinaryExpressions: { - type: "boolean", - }, - nestedBinaryExpressions: { type: "boolean" }, - returnAssign: { type: "boolean" }, - ignoreJSX: { - enum: [ - "none", - "all", - "single-line", - "multi-line", - ], - }, - enforceForArrowConditionals: { - type: "boolean", - }, - enforceForSequenceExpressions: { - type: "boolean", - }, - enforceForNewInMemberExpressions: { - type: "boolean", - }, - enforceForFunctionPrototypeMethods: { - type: "boolean", - }, - allowParensAfterCommentPattern: { - type: "string", - }, - }, - additionalProperties: false, - }, - ], - minItems: 0, - maxItems: 2, - }, - ], - }, - - messages: { - unexpected: "Unnecessary parentheses around expression.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - const tokensToIgnore = new WeakSet(); - const precedence = astUtils.getPrecedence; - const ALL_NODES = context.options[0] !== "functions"; - const EXCEPT_COND_ASSIGN = - ALL_NODES && - context.options[1] && - context.options[1].conditionalAssign === false; - const EXCEPT_COND_TERNARY = - ALL_NODES && - context.options[1] && - context.options[1].ternaryOperandBinaryExpressions === false; - const NESTED_BINARY = - ALL_NODES && - context.options[1] && - context.options[1].nestedBinaryExpressions === false; - const EXCEPT_RETURN_ASSIGN = - ALL_NODES && - context.options[1] && - context.options[1].returnAssign === false; - const IGNORE_JSX = - ALL_NODES && context.options[1] && context.options[1].ignoreJSX; - const IGNORE_ARROW_CONDITIONALS = - ALL_NODES && - context.options[1] && - context.options[1].enforceForArrowConditionals === false; - const IGNORE_SEQUENCE_EXPRESSIONS = - ALL_NODES && - context.options[1] && - context.options[1].enforceForSequenceExpressions === false; - const IGNORE_NEW_IN_MEMBER_EXPR = - ALL_NODES && - context.options[1] && - context.options[1].enforceForNewInMemberExpressions === false; - const IGNORE_FUNCTION_PROTOTYPE_METHODS = - ALL_NODES && - context.options[1] && - context.options[1].enforceForFunctionPrototypeMethods === false; - const ALLOW_PARENS_AFTER_COMMENT_PATTERN = - ALL_NODES && - context.options[1] && - context.options[1].allowParensAfterCommentPattern; - - const PRECEDENCE_OF_ASSIGNMENT_EXPR = precedence({ - type: "AssignmentExpression", - }); - const PRECEDENCE_OF_UPDATE_EXPR = precedence({ - type: "UpdateExpression", - }); - - let reportsBuffer; - - /** - * Determines whether the given node is a `call` or `apply` method call, invoked directly on a `FunctionExpression` node. - * Example: function(){}.call() - * @param {ASTNode} node The node to be checked. - * @returns {boolean} True if the node is an immediate `call` or `apply` method call. - * @private - */ - function isImmediateFunctionPrototypeMethodCall(node) { - const callNode = astUtils.skipChainExpression(node); - - if (callNode.type !== "CallExpression") { - return false; - } - const callee = astUtils.skipChainExpression(callNode.callee); - - return ( - callee.type === "MemberExpression" && - callee.object.type === "FunctionExpression" && - ["call", "apply"].includes( - astUtils.getStaticPropertyName(callee), - ) - ); - } - - /** - * Determines if this rule should be enforced for a node given the current configuration. - * @param {ASTNode} node The node to be checked. - * @returns {boolean} True if the rule should be enforced for this node. - * @private - */ - function ruleApplies(node) { - if (node.type === "JSXElement" || node.type === "JSXFragment") { - const isSingleLine = node.loc.start.line === node.loc.end.line; - - switch (IGNORE_JSX) { - // Exclude this JSX element from linting - case "all": - return false; - - // Exclude this JSX element if it is multi-line element - case "multi-line": - return isSingleLine; - - // Exclude this JSX element if it is single-line element - case "single-line": - return !isSingleLine; - - // Nothing special to be done for JSX elements - case "none": - break; - - // no default - } - } - - if ( - node.type === "SequenceExpression" && - IGNORE_SEQUENCE_EXPRESSIONS - ) { - return false; - } - - if ( - isImmediateFunctionPrototypeMethodCall(node) && - IGNORE_FUNCTION_PROTOTYPE_METHODS - ) { - return false; - } - - return ( - ALL_NODES || - node.type === "FunctionExpression" || - node.type === "ArrowFunctionExpression" - ); - } - - /** - * Determines if a node is surrounded by parentheses. - * @param {ASTNode} node The node to be checked. - * @returns {boolean} True if the node is parenthesised. - * @private - */ - function isParenthesised(node) { - return isParenthesizedRaw(1, node, sourceCode); - } - - /** - * Determines if a node is surrounded by parentheses twice. - * @param {ASTNode} node The node to be checked. - * @returns {boolean} True if the node is doubly parenthesised. - * @private - */ - function isParenthesisedTwice(node) { - return isParenthesizedRaw(2, node, sourceCode); - } - - /** - * Determines if a node is surrounded by (potentially) invalid parentheses. - * @param {ASTNode} node The node to be checked. - * @returns {boolean} True if the node is incorrectly parenthesised. - * @private - */ - function hasExcessParens(node) { - return ruleApplies(node) && isParenthesised(node); - } - - /** - * Determines if a node that is expected to be parenthesised is surrounded by - * (potentially) invalid extra parentheses. - * @param {ASTNode} node The node to be checked. - * @returns {boolean} True if the node is has an unexpected extra pair of parentheses. - * @private - */ - function hasDoubleExcessParens(node) { - return ruleApplies(node) && isParenthesisedTwice(node); - } - - /** - * Determines if a node that is expected to be parenthesised is surrounded by - * (potentially) invalid extra parentheses with considering precedence level of the node. - * If the preference level of the node is not higher or equal to precedence lower limit, it also checks - * whether the node is surrounded by parentheses twice or not. - * @param {ASTNode} node The node to be checked. - * @param {number} precedenceLowerLimit The lower limit of precedence. - * @returns {boolean} True if the node is has an unexpected extra pair of parentheses. - * @private - */ - function hasExcessParensWithPrecedence(node, precedenceLowerLimit) { - if (ruleApplies(node) && isParenthesised(node)) { - if ( - precedence(node) >= precedenceLowerLimit || - isParenthesisedTwice(node) - ) { - return true; - } - } - return false; - } - - /** - * Determines if a node test expression is allowed to have a parenthesised assignment - * @param {ASTNode} node The node to be checked. - * @returns {boolean} True if the assignment can be parenthesised. - * @private - */ - function isCondAssignException(node) { - return ( - EXCEPT_COND_ASSIGN && node.test.type === "AssignmentExpression" - ); - } - - /** - * Determines if a node is in a return statement - * @param {ASTNode} node The node to be checked. - * @returns {boolean} True if the node is in a return statement. - * @private - */ - function isInReturnStatement(node) { - for ( - let currentNode = node; - currentNode; - currentNode = currentNode.parent - ) { - if ( - currentNode.type === "ReturnStatement" || - (currentNode.type === "ArrowFunctionExpression" && - currentNode.body.type !== "BlockStatement") - ) { - return true; - } - } - - return false; - } - - /** - * Determines if a constructor function is newed-up with parens - * @param {ASTNode} newExpression The NewExpression node to be checked. - * @returns {boolean} True if the constructor is called with parens. - * @private - */ - function isNewExpressionWithParens(newExpression) { - const lastToken = sourceCode.getLastToken(newExpression); - const penultimateToken = sourceCode.getTokenBefore(lastToken); - - return ( - newExpression.arguments.length > 0 || - // The expression should end with its own parens, e.g., new new foo() is not a new expression with parens - (astUtils.isOpeningParenToken(penultimateToken) && - astUtils.isClosingParenToken(lastToken) && - newExpression.callee.range[1] < newExpression.range[1]) - ); - } - - /** - * Determines if a node is or contains an assignment expression - * @param {ASTNode} node The node to be checked. - * @returns {boolean} True if the node is or contains an assignment expression. - * @private - */ - function containsAssignment(node) { - if (node.type === "AssignmentExpression") { - return true; - } - if ( - node.type === "ConditionalExpression" && - (node.consequent.type === "AssignmentExpression" || - node.alternate.type === "AssignmentExpression") - ) { - return true; - } - if ( - (node.left && node.left.type === "AssignmentExpression") || - (node.right && node.right.type === "AssignmentExpression") - ) { - return true; - } - - return false; - } - - /** - * Determines if a node is contained by or is itself a return statement and is allowed to have a parenthesised assignment - * @param {ASTNode} node The node to be checked. - * @returns {boolean} True if the assignment can be parenthesised. - * @private - */ - function isReturnAssignException(node) { - if (!EXCEPT_RETURN_ASSIGN || !isInReturnStatement(node)) { - return false; - } - - if (node.type === "ReturnStatement") { - return node.argument && containsAssignment(node.argument); - } - if ( - node.type === "ArrowFunctionExpression" && - node.body.type !== "BlockStatement" - ) { - return containsAssignment(node.body); - } - return containsAssignment(node); - } - - /** - * Determines if a node following a [no LineTerminator here] restriction is - * surrounded by (potentially) invalid extra parentheses. - * @param {Token} token The token preceding the [no LineTerminator here] restriction. - * @param {ASTNode} node The node to be checked. - * @returns {boolean} True if the node is incorrectly parenthesised. - * @private - */ - function hasExcessParensNoLineTerminator(token, node) { - if (token.loc.end.line === node.loc.start.line) { - return hasExcessParens(node); - } - - return hasDoubleExcessParens(node); - } - - /** - * Determines whether a node should be preceded by an additional space when removing parens - * @param {ASTNode} node node to evaluate; must be surrounded by parentheses - * @returns {boolean} `true` if a space should be inserted before the node - * @private - */ - function requiresLeadingSpace(node) { - const leftParenToken = sourceCode.getTokenBefore(node); - const tokenBeforeLeftParen = sourceCode.getTokenBefore( - leftParenToken, - { includeComments: true }, - ); - const tokenAfterLeftParen = sourceCode.getTokenAfter( - leftParenToken, - { includeComments: true }, - ); - - return ( - tokenBeforeLeftParen && - tokenBeforeLeftParen.range[1] === leftParenToken.range[0] && - leftParenToken.range[1] === tokenAfterLeftParen.range[0] && - !astUtils.canTokensBeAdjacent( - tokenBeforeLeftParen, - tokenAfterLeftParen, - ) - ); - } - - /** - * Determines whether a node should be followed by an additional space when removing parens - * @param {ASTNode} node node to evaluate; must be surrounded by parentheses - * @returns {boolean} `true` if a space should be inserted after the node - * @private - */ - function requiresTrailingSpace(node) { - const nextTwoTokens = sourceCode.getTokensAfter(node, { count: 2 }); - const rightParenToken = nextTwoTokens[0]; - const tokenAfterRightParen = nextTwoTokens[1]; - const tokenBeforeRightParen = sourceCode.getLastToken(node); - - return ( - rightParenToken && - tokenAfterRightParen && - !sourceCode.isSpaceBetweenTokens( - rightParenToken, - tokenAfterRightParen, - ) && - !astUtils.canTokensBeAdjacent( - tokenBeforeRightParen, - tokenAfterRightParen, - ) - ); - } - - /** - * Determines if a given expression node is an IIFE - * @param {ASTNode} node The node to check - * @returns {boolean} `true` if the given node is an IIFE - */ - function isIIFE(node) { - const maybeCallNode = astUtils.skipChainExpression(node); - - return ( - maybeCallNode.type === "CallExpression" && - maybeCallNode.callee.type === "FunctionExpression" - ); - } - - /** - * Determines if the given node can be the assignment target in destructuring or the LHS of an assignment. - * This is to avoid an autofix that could change behavior because parsers mistakenly allow invalid syntax, - * such as `(a = b) = c` and `[(a = b) = c] = []`. Ideally, this function shouldn't be necessary. - * @param {ASTNode} [node] The node to check - * @returns {boolean} `true` if the given node can be a valid assignment target - */ - function canBeAssignmentTarget(node) { - return ( - node && - (node.type === "Identifier" || node.type === "MemberExpression") - ); - } - - /** - * Checks if a node is fixable. - * A node is fixable if removing a single pair of surrounding parentheses does not turn it - * into a directive after fixing other nodes. - * Almost all nodes are fixable, except if all of the following conditions are met: - * The node is a string Literal - * It has a single pair of parentheses - * It is the only child of an ExpressionStatement - * @param {ASTNode} node The node to evaluate. - * @returns {boolean} Whether or not the node is fixable. - * @private - */ - function isFixable(node) { - // if it's not a string literal it can be autofixed - if (node.type !== "Literal" || typeof node.value !== "string") { - return true; - } - if (isParenthesisedTwice(node)) { - return true; - } - return !astUtils.isTopLevelExpressionStatement(node.parent); - } - - /** - * Report the node - * @param {ASTNode} node node to evaluate - * @returns {void} - * @private - */ - function report(node) { - const leftParenToken = sourceCode.getTokenBefore(node); - const rightParenToken = sourceCode.getTokenAfter(node); - - if (!isParenthesisedTwice(node)) { - if (tokensToIgnore.has(sourceCode.getFirstToken(node))) { - return; - } - - if (isIIFE(node) && !isParenthesised(node.callee)) { - return; - } - - if (ALLOW_PARENS_AFTER_COMMENT_PATTERN) { - const commentsBeforeLeftParenToken = - sourceCode.getCommentsBefore(leftParenToken); - const totalCommentsBeforeLeftParenTokenCount = - commentsBeforeLeftParenToken.length; - const ignorePattern = new RegExp( - ALLOW_PARENS_AFTER_COMMENT_PATTERN, - "u", - ); - - if ( - totalCommentsBeforeLeftParenTokenCount > 0 && - ignorePattern.test( - commentsBeforeLeftParenToken[ - totalCommentsBeforeLeftParenTokenCount - 1 - ].value, - ) - ) { - return; - } - } - } - - /** - * Finishes reporting - * @returns {void} - * @private - */ - function finishReport() { - context.report({ - node, - loc: leftParenToken.loc, - messageId: "unexpected", - fix: isFixable(node) - ? fixer => { - const parenthesizedSource = - sourceCode.text.slice( - leftParenToken.range[1], - rightParenToken.range[0], - ); - - return fixer.replaceTextRange( - [ - leftParenToken.range[0], - rightParenToken.range[1], - ], - (requiresLeadingSpace(node) ? " " : "") + - parenthesizedSource + - (requiresTrailingSpace(node) - ? " " - : ""), - ); - } - : null, - }); - } - - if (reportsBuffer) { - reportsBuffer.reports.push({ node, finishReport }); - return; - } - - finishReport(); - } - - /** - * Evaluate a argument of the node. - * @param {ASTNode} node node to evaluate - * @returns {void} - * @private - */ - function checkArgumentWithPrecedence(node) { - if ( - hasExcessParensWithPrecedence(node.argument, precedence(node)) - ) { - report(node.argument); - } - } - - /** - * Check if a member expression contains a call expression - * @param {ASTNode} node MemberExpression node to evaluate - * @returns {boolean} true if found, false if not - */ - function doesMemberExpressionContainCallExpression(node) { - let currentNode = node.object; - let currentNodeType = node.object.type; - - while (currentNodeType === "MemberExpression") { - currentNode = currentNode.object; - currentNodeType = currentNode.type; - } - - return currentNodeType === "CallExpression"; - } - - /** - * Evaluate a new call - * @param {ASTNode} node node to evaluate - * @returns {void} - * @private - */ - function checkCallNew(node) { - const callee = node.callee; - - if (hasExcessParensWithPrecedence(callee, precedence(node))) { - if ( - hasDoubleExcessParens(callee) || - !( - isIIFE(node) || - // (new A)(); new (new A)(); - (callee.type === "NewExpression" && - !isNewExpressionWithParens(callee) && - !( - node.type === "NewExpression" && - !isNewExpressionWithParens(node) - )) || - // new (a().b)(); new (a.b().c); - (node.type === "NewExpression" && - callee.type === "MemberExpression" && - doesMemberExpressionContainCallExpression( - callee, - )) || - // (a?.b)(); (a?.())(); - (!node.optional && callee.type === "ChainExpression") - ) - ) { - report(node.callee); - } - } - node.arguments - .filter(arg => - hasExcessParensWithPrecedence( - arg, - PRECEDENCE_OF_ASSIGNMENT_EXPR, - ), - ) - .forEach(report); - } - - /** - * Evaluate binary logicals - * @param {ASTNode} node node to evaluate - * @returns {void} - * @private - */ - function checkBinaryLogical(node) { - const prec = precedence(node); - const leftPrecedence = precedence(node.left); - const rightPrecedence = precedence(node.right); - const isExponentiation = node.operator === "**"; - const shouldSkipLeft = - NESTED_BINARY && - (node.left.type === "BinaryExpression" || - node.left.type === "LogicalExpression"); - const shouldSkipRight = - NESTED_BINARY && - (node.right.type === "BinaryExpression" || - node.right.type === "LogicalExpression"); - - if (!shouldSkipLeft && hasExcessParens(node.left)) { - if ( - (!( - ["AwaitExpression", "UnaryExpression"].includes( - node.left.type, - ) && isExponentiation - ) && - !astUtils.isMixedLogicalAndCoalesceExpressions( - node.left, - node, - ) && - (leftPrecedence > prec || - (leftPrecedence === prec && !isExponentiation))) || - isParenthesisedTwice(node.left) - ) { - report(node.left); - } - } - - if (!shouldSkipRight && hasExcessParens(node.right)) { - if ( - (!astUtils.isMixedLogicalAndCoalesceExpressions( - node.right, - node, - ) && - (rightPrecedence > prec || - (rightPrecedence === prec && isExponentiation))) || - isParenthesisedTwice(node.right) - ) { - report(node.right); - } - } - } - - /** - * Check the parentheses around the super class of the given class definition. - * @param {ASTNode} node The node of class declarations to check. - * @returns {void} - */ - function checkClass(node) { - if (!node.superClass) { - return; - } - - /* - * If `node.superClass` is a LeftHandSideExpression, parentheses are extra. - * Otherwise, parentheses are needed. - */ - const hasExtraParens = - precedence(node.superClass) > PRECEDENCE_OF_UPDATE_EXPR - ? hasExcessParens(node.superClass) - : hasDoubleExcessParens(node.superClass); - - if (hasExtraParens) { - report(node.superClass); - } - } - - /** - * Check the parentheses around the argument of the given spread operator. - * @param {ASTNode} node The node of spread elements/properties to check. - * @returns {void} - */ - function checkSpreadOperator(node) { - if ( - hasExcessParensWithPrecedence( - node.argument, - PRECEDENCE_OF_ASSIGNMENT_EXPR, - ) - ) { - report(node.argument); - } - } - - /** - * Checks the parentheses for an ExpressionStatement or ExportDefaultDeclaration - * @param {ASTNode} node The ExpressionStatement.expression or ExportDefaultDeclaration.declaration node - * @returns {void} - */ - function checkExpressionOrExportStatement(node) { - const firstToken = isParenthesised(node) - ? sourceCode.getTokenBefore(node) - : sourceCode.getFirstToken(node); - const secondToken = sourceCode.getTokenAfter( - firstToken, - astUtils.isNotOpeningParenToken, - ); - const thirdToken = secondToken - ? sourceCode.getTokenAfter(secondToken) - : null; - const tokenAfterClosingParens = secondToken - ? sourceCode.getTokenAfter( - secondToken, - astUtils.isNotClosingParenToken, - ) - : null; - - if ( - astUtils.isOpeningParenToken(firstToken) && - (astUtils.isOpeningBraceToken(secondToken) || - (secondToken.type === "Keyword" && - (secondToken.value === "function" || - secondToken.value === "class" || - (secondToken.value === "let" && - tokenAfterClosingParens && - (astUtils.isOpeningBracketToken( - tokenAfterClosingParens, - ) || - tokenAfterClosingParens.type === - "Identifier")))) || - (secondToken && - secondToken.type === "Identifier" && - secondToken.value === "async" && - thirdToken && - thirdToken.type === "Keyword" && - thirdToken.value === "function")) - ) { - tokensToIgnore.add(secondToken); - } - - const hasExtraParens = - node.parent.type === "ExportDefaultDeclaration" - ? hasExcessParensWithPrecedence( - node, - PRECEDENCE_OF_ASSIGNMENT_EXPR, - ) - : hasExcessParens(node); - - if (hasExtraParens) { - report(node); - } - } - - /** - * Finds the path from the given node to the specified ancestor. - * @param {ASTNode} node First node in the path. - * @param {ASTNode} ancestor Last node in the path. - * @returns {ASTNode[]} Path, including both nodes. - * @throws {Error} If the given node does not have the specified ancestor. - */ - function pathToAncestor(node, ancestor) { - const path = [node]; - let currentNode = node; - - while (currentNode !== ancestor) { - currentNode = currentNode.parent; - - /* c8 ignore start */ - if (currentNode === null) { - throw new Error( - "Nodes are not in the ancestor-descendant relationship.", - ); - } /* c8 ignore stop */ - - path.push(currentNode); - } - - return path; - } - - /** - * Finds the path from the given node to the specified descendant. - * @param {ASTNode} node First node in the path. - * @param {ASTNode} descendant Last node in the path. - * @returns {ASTNode[]} Path, including both nodes. - * @throws {Error} If the given node does not have the specified descendant. - */ - function pathToDescendant(node, descendant) { - return pathToAncestor(descendant, node).reverse(); - } - - /** - * Checks whether the syntax of the given ancestor of an 'in' expression inside a for-loop initializer - * is preventing the 'in' keyword from being interpreted as a part of an ill-formed for-in loop. - * @param {ASTNode} node Ancestor of an 'in' expression. - * @param {ASTNode} child Child of the node, ancestor of the same 'in' expression or the 'in' expression itself. - * @returns {boolean} True if the keyword 'in' would be interpreted as the 'in' operator, without any parenthesis. - */ - function isSafelyEnclosingInExpression(node, child) { - switch (node.type) { - case "ArrayExpression": - case "ArrayPattern": - case "BlockStatement": - case "ObjectExpression": - case "ObjectPattern": - case "TemplateLiteral": - return true; - case "ArrowFunctionExpression": - case "FunctionExpression": - return node.params.includes(child); - case "CallExpression": - case "NewExpression": - return node.arguments.includes(child); - case "MemberExpression": - return node.computed && node.property === child; - case "ConditionalExpression": - return node.consequent === child; - default: - return false; - } - } - - /** - * Starts a new reports buffering. Warnings will be stored in a buffer instead of being reported immediately. - * An additional logic that requires multiple nodes (e.g. a whole subtree) may dismiss some of the stored warnings. - * @returns {void} - */ - function startNewReportsBuffering() { - reportsBuffer = { - upper: reportsBuffer, - inExpressionNodes: [], - reports: [], - }; - } - - /** - * Ends the current reports buffering. - * @returns {void} - */ - function endCurrentReportsBuffering() { - const { upper, inExpressionNodes, reports } = reportsBuffer; - - if (upper) { - upper.inExpressionNodes.push(...inExpressionNodes); - upper.reports.push(...reports); - } else { - // flush remaining reports - reports.forEach(({ finishReport }) => finishReport()); - } - - reportsBuffer = upper; - } - - /** - * Checks whether the given node is in the current reports buffer. - * @param {ASTNode} node Node to check. - * @returns {boolean} True if the node is in the current buffer, false otherwise. - */ - function isInCurrentReportsBuffer(node) { - return reportsBuffer.reports.some(r => r.node === node); - } - - /** - * Removes the given node from the current reports buffer. - * @param {ASTNode} node Node to remove. - * @returns {void} - */ - function removeFromCurrentReportsBuffer(node) { - reportsBuffer.reports = reportsBuffer.reports.filter( - r => r.node !== node, - ); - } - - /** - * Checks whether a node is a MemberExpression at NewExpression's callee. - * @param {ASTNode} node node to check. - * @returns {boolean} True if the node is a MemberExpression at NewExpression's callee. false otherwise. - */ - function isMemberExpInNewCallee(node) { - if (node.type === "MemberExpression") { - return node.parent.type === "NewExpression" && - node.parent.callee === node - ? true - : node.parent.object === node && - isMemberExpInNewCallee(node.parent); - } - return false; - } - - /** - * Checks if the left-hand side of an assignment is an identifier, the operator is one of - * `=`, `&&=`, `||=` or `??=` and the right-hand side is an anonymous class or function. - * - * As per https://tc39.es/ecma262/#sec-assignment-operators-runtime-semantics-evaluation, an - * assignment involving one of the operators `=`, `&&=`, `||=` or `??=` where the right-hand - * side is an anonymous class or function and the left-hand side is an *unparenthesized* - * identifier has different semantics than other assignments. - * Specifically, when an expression like `foo = function () {}` is evaluated, `foo.name` - * will be set to the string "foo", i.e. the identifier name. The same thing does not happen - * when evaluating `(foo) = function () {}`. - * Since the parenthesizing of the identifier in the left-hand side is significant in this - * special case, the parentheses, if present, should not be flagged as unnecessary. - * @param {ASTNode} node an AssignmentExpression node. - * @returns {boolean} `true` if the left-hand side of the assignment is an identifier, the - * operator is one of `=`, `&&=`, `||=` or `??=` and the right-hand side is an anonymous - * class or function; otherwise, `false`. - */ - function isAnonymousFunctionAssignmentException({ - left, - operator, - right, - }) { - if ( - left.type === "Identifier" && - ["=", "&&=", "||=", "??="].includes(operator) - ) { - const rhsType = right.type; - - if (rhsType === "ArrowFunctionExpression") { - return true; - } - if ( - (rhsType === "FunctionExpression" || - rhsType === "ClassExpression") && - !right.id - ) { - return true; - } - } - return false; - } - - return { - ArrayExpression(node) { - node.elements - .filter( - e => - e && - hasExcessParensWithPrecedence( - e, - PRECEDENCE_OF_ASSIGNMENT_EXPR, - ), - ) - .forEach(report); - }, - - ArrayPattern(node) { - node.elements - .filter(e => canBeAssignmentTarget(e) && hasExcessParens(e)) - .forEach(report); - }, - - ArrowFunctionExpression(node) { - if (isReturnAssignException(node)) { - return; - } - - if ( - node.body.type === "ConditionalExpression" && - IGNORE_ARROW_CONDITIONALS - ) { - return; - } - - if (node.body.type !== "BlockStatement") { - const firstBodyToken = sourceCode.getFirstToken( - node.body, - astUtils.isNotOpeningParenToken, - ); - const tokenBeforeFirst = - sourceCode.getTokenBefore(firstBodyToken); - - if ( - astUtils.isOpeningParenToken(tokenBeforeFirst) && - astUtils.isOpeningBraceToken(firstBodyToken) - ) { - tokensToIgnore.add(firstBodyToken); - } - if ( - hasExcessParensWithPrecedence( - node.body, - PRECEDENCE_OF_ASSIGNMENT_EXPR, - ) - ) { - report(node.body); - } - } - }, - - AssignmentExpression(node) { - if ( - canBeAssignmentTarget(node.left) && - hasExcessParens(node.left) && - (!isAnonymousFunctionAssignmentException(node) || - isParenthesisedTwice(node.left)) - ) { - report(node.left); - } - - if ( - !isReturnAssignException(node) && - hasExcessParensWithPrecedence(node.right, precedence(node)) - ) { - report(node.right); - } - }, - - BinaryExpression(node) { - if (reportsBuffer && node.operator === "in") { - reportsBuffer.inExpressionNodes.push(node); - } - - checkBinaryLogical(node); - }, - - CallExpression: checkCallNew, - - ConditionalExpression(node) { - if (isReturnAssignException(node)) { - return; - } - - const availableTypes = new Set([ - "BinaryExpression", - "LogicalExpression", - ]); - - if ( - !( - EXCEPT_COND_TERNARY && - availableTypes.has(node.test.type) - ) && - !isCondAssignException(node) && - hasExcessParensWithPrecedence( - node.test, - precedence({ - type: "LogicalExpression", - operator: "||", - }), - ) - ) { - report(node.test); - } - - if ( - !( - EXCEPT_COND_TERNARY && - availableTypes.has(node.consequent.type) - ) && - hasExcessParensWithPrecedence( - node.consequent, - PRECEDENCE_OF_ASSIGNMENT_EXPR, - ) - ) { - report(node.consequent); - } - - if ( - !( - EXCEPT_COND_TERNARY && - availableTypes.has(node.alternate.type) - ) && - hasExcessParensWithPrecedence( - node.alternate, - PRECEDENCE_OF_ASSIGNMENT_EXPR, - ) - ) { - report(node.alternate); - } - }, - - DoWhileStatement(node) { - if ( - hasExcessParens(node.test) && - !isCondAssignException(node) - ) { - report(node.test); - } - }, - - ExportDefaultDeclaration: node => - checkExpressionOrExportStatement(node.declaration), - ExpressionStatement: node => - checkExpressionOrExportStatement(node.expression), - - ForInStatement(node) { - if (node.left.type !== "VariableDeclaration") { - const firstLeftToken = sourceCode.getFirstToken( - node.left, - astUtils.isNotOpeningParenToken, - ); - - if ( - firstLeftToken.value === "let" && - astUtils.isOpeningBracketToken( - sourceCode.getTokenAfter( - firstLeftToken, - astUtils.isNotClosingParenToken, - ), - ) - ) { - // ForInStatement#left expression cannot start with `let[`. - tokensToIgnore.add(firstLeftToken); - } - } - - if (hasExcessParens(node.left)) { - report(node.left); - } - - if (hasExcessParens(node.right)) { - report(node.right); - } - }, - - ForOfStatement(node) { - if (node.left.type !== "VariableDeclaration") { - const firstLeftToken = sourceCode.getFirstToken( - node.left, - astUtils.isNotOpeningParenToken, - ); - - if (firstLeftToken.value === "let") { - // ForOfStatement#left expression cannot start with `let`. - tokensToIgnore.add(firstLeftToken); - } - } - - if (hasExcessParens(node.left)) { - report(node.left); - } - - if ( - hasExcessParensWithPrecedence( - node.right, - PRECEDENCE_OF_ASSIGNMENT_EXPR, - ) - ) { - report(node.right); - } - }, - - ForStatement(node) { - if ( - node.test && - hasExcessParens(node.test) && - !isCondAssignException(node) - ) { - report(node.test); - } - - if (node.update && hasExcessParens(node.update)) { - report(node.update); - } - - if (node.init) { - if (node.init.type !== "VariableDeclaration") { - const firstToken = sourceCode.getFirstToken( - node.init, - astUtils.isNotOpeningParenToken, - ); - - if ( - firstToken.value === "let" && - astUtils.isOpeningBracketToken( - sourceCode.getTokenAfter( - firstToken, - astUtils.isNotClosingParenToken, - ), - ) - ) { - // ForStatement#init expression cannot start with `let[`. - tokensToIgnore.add(firstToken); - } - } - - startNewReportsBuffering(); - - if (hasExcessParens(node.init)) { - report(node.init); - } - } - }, - - "ForStatement > *.init:exit"(node) { - /* - * Removing parentheses around `in` expressions might change semantics and cause errors. - * - * For example, this valid for loop: - * for (let a = (b in c); ;); - * after removing parentheses would be treated as an invalid for-in loop: - * for (let a = b in c; ;); - */ - - if (reportsBuffer.reports.length) { - reportsBuffer.inExpressionNodes.forEach( - inExpressionNode => { - const path = pathToDescendant( - node, - inExpressionNode, - ); - let nodeToExclude; - - for (let i = 0; i < path.length; i++) { - const pathNode = path[i]; - - if (i < path.length - 1) { - const nextPathNode = path[i + 1]; - - if ( - isSafelyEnclosingInExpression( - pathNode, - nextPathNode, - ) - ) { - // The 'in' expression in safely enclosed by the syntax of its ancestor nodes (e.g. by '{}' or '[]'). - return; - } - } - - if (isParenthesised(pathNode)) { - if (isInCurrentReportsBuffer(pathNode)) { - // This node was supposed to be reported, but parentheses might be necessary. - - if (isParenthesisedTwice(pathNode)) { - /* - * This node is parenthesised twice, it certainly has at least one pair of `extra` parentheses. - * If the --fix option is on, the current fixing iteration will remove only one pair of parentheses. - * The remaining pair is safely enclosing the 'in' expression. - */ - return; - } - - // Exclude the outermost node only. - if (!nodeToExclude) { - nodeToExclude = pathNode; - } - - // Don't break the loop here, there might be some safe nodes or parentheses that will stay inside. - } else { - // This node will stay parenthesised, the 'in' expression in safely enclosed by '()'. - return; - } - } - } - - // Exclude the node from the list (i.e. treat parentheses as necessary) - removeFromCurrentReportsBuffer(nodeToExclude); - }, - ); - } - - endCurrentReportsBuffering(); - }, - - IfStatement(node) { - if ( - hasExcessParens(node.test) && - !isCondAssignException(node) - ) { - report(node.test); - } - }, - - ImportExpression(node) { - const { source } = node; - - if (source.type === "SequenceExpression") { - if (hasDoubleExcessParens(source)) { - report(source); - } - } else if (hasExcessParens(source)) { - report(source); - } - }, - - LogicalExpression: checkBinaryLogical, - - MemberExpression(node) { - const shouldAllowWrapOnce = - isMemberExpInNewCallee(node) && - doesMemberExpressionContainCallExpression(node); - const nodeObjHasExcessParens = shouldAllowWrapOnce - ? hasDoubleExcessParens(node.object) - : hasExcessParens(node.object) && - !( - isImmediateFunctionPrototypeMethodCall( - node.parent, - ) && - node.parent.callee === node && - IGNORE_FUNCTION_PROTOTYPE_METHODS - ); - - if ( - nodeObjHasExcessParens && - precedence(node.object) >= precedence(node) && - (node.computed || - !( - astUtils.isDecimalInteger(node.object) || - // RegExp literal is allowed to have parens (#1589) - (node.object.type === "Literal" && - node.object.regex) - )) - ) { - report(node.object); - } - - if ( - nodeObjHasExcessParens && - node.object.type === "CallExpression" - ) { - report(node.object); - } - - if ( - nodeObjHasExcessParens && - !IGNORE_NEW_IN_MEMBER_EXPR && - node.object.type === "NewExpression" && - isNewExpressionWithParens(node.object) - ) { - report(node.object); - } - - if ( - nodeObjHasExcessParens && - node.optional && - node.object.type === "ChainExpression" - ) { - report(node.object); - } - - if (node.computed && hasExcessParens(node.property)) { - report(node.property); - } - }, - - "MethodDefinition[computed=true]"(node) { - if ( - hasExcessParensWithPrecedence( - node.key, - PRECEDENCE_OF_ASSIGNMENT_EXPR, - ) - ) { - report(node.key); - } - }, - - NewExpression: checkCallNew, - - ObjectExpression(node) { - node.properties - .filter( - property => - property.value && - hasExcessParensWithPrecedence( - property.value, - PRECEDENCE_OF_ASSIGNMENT_EXPR, - ), - ) - .forEach(property => report(property.value)); - }, - - ObjectPattern(node) { - node.properties - .filter(property => { - const value = property.value; - - return ( - canBeAssignmentTarget(value) && - hasExcessParens(value) - ); - }) - .forEach(property => report(property.value)); - }, - - Property(node) { - if (node.computed) { - const { key } = node; - - if ( - key && - hasExcessParensWithPrecedence( - key, - PRECEDENCE_OF_ASSIGNMENT_EXPR, - ) - ) { - report(key); - } - } - }, - - PropertyDefinition(node) { - if ( - node.computed && - hasExcessParensWithPrecedence( - node.key, - PRECEDENCE_OF_ASSIGNMENT_EXPR, - ) - ) { - report(node.key); - } - - if ( - node.value && - hasExcessParensWithPrecedence( - node.value, - PRECEDENCE_OF_ASSIGNMENT_EXPR, - ) - ) { - report(node.value); - } - }, - - RestElement(node) { - const argument = node.argument; - - if ( - canBeAssignmentTarget(argument) && - hasExcessParens(argument) - ) { - report(argument); - } - }, - - ReturnStatement(node) { - const returnToken = sourceCode.getFirstToken(node); - - if (isReturnAssignException(node)) { - return; - } - - if ( - node.argument && - hasExcessParensNoLineTerminator( - returnToken, - node.argument, - ) && - // RegExp literal is allowed to have parens (#1589) - !(node.argument.type === "Literal" && node.argument.regex) - ) { - report(node.argument); - } - }, - - SequenceExpression(node) { - const precedenceOfNode = precedence(node); - - node.expressions - .filter(e => - hasExcessParensWithPrecedence(e, precedenceOfNode), - ) - .forEach(report); - }, - - SwitchCase(node) { - if (node.test && hasExcessParens(node.test)) { - report(node.test); - } - }, - - SwitchStatement(node) { - if (hasExcessParens(node.discriminant)) { - report(node.discriminant); - } - }, - - ThrowStatement(node) { - const throwToken = sourceCode.getFirstToken(node); - - if ( - hasExcessParensNoLineTerminator(throwToken, node.argument) - ) { - report(node.argument); - } - }, - - UnaryExpression: checkArgumentWithPrecedence, - UpdateExpression(node) { - if (node.prefix) { - checkArgumentWithPrecedence(node); - } else { - const { argument } = node; - const operatorToken = sourceCode.getLastToken(node); - - if ( - argument.loc.end.line === operatorToken.loc.start.line - ) { - checkArgumentWithPrecedence(node); - } else { - if (hasDoubleExcessParens(argument)) { - report(argument); - } - } - } - }, - AwaitExpression: checkArgumentWithPrecedence, - - VariableDeclarator(node) { - if ( - node.init && - hasExcessParensWithPrecedence( - node.init, - PRECEDENCE_OF_ASSIGNMENT_EXPR, - ) && - // RegExp literal is allowed to have parens (#1589) - !(node.init.type === "Literal" && node.init.regex) - ) { - report(node.init); - } - }, - - WhileStatement(node) { - if ( - hasExcessParens(node.test) && - !isCondAssignException(node) - ) { - report(node.test); - } - }, - - WithStatement(node) { - if (hasExcessParens(node.object)) { - report(node.object); - } - }, - - YieldExpression(node) { - if (node.argument) { - const yieldToken = sourceCode.getFirstToken(node); - - if ( - (precedence(node.argument) >= precedence(node) && - hasExcessParensNoLineTerminator( - yieldToken, - node.argument, - )) || - hasDoubleExcessParens(node.argument) - ) { - report(node.argument); - } - } - }, - - ClassDeclaration: checkClass, - ClassExpression: checkClass, - - SpreadElement: checkSpreadOperator, - SpreadProperty: checkSpreadOperator, - ExperimentalSpreadProperty: checkSpreadOperator, - - TemplateLiteral(node) { - node.expressions - .filter(e => e && hasExcessParens(e)) - .forEach(report); - }, - - AssignmentPattern(node) { - const { left, right } = node; - - if (canBeAssignmentTarget(left) && hasExcessParens(left)) { - report(left); - } - - if ( - right && - hasExcessParensWithPrecedence( - right, - PRECEDENCE_OF_ASSIGNMENT_EXPR, - ) - ) { - report(right); - } - }, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Disallow unnecessary parentheses", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-extra-parens" + }, + + fixable: "code", + + schema: { + anyOf: [ + { + type: "array", + items: [ + { + enum: ["functions"] + } + ], + minItems: 0, + maxItems: 1 + }, + { + type: "array", + items: [ + { + enum: ["all"] + }, + { + type: "object", + properties: { + conditionalAssign: { type: "boolean" }, + ternaryOperandBinaryExpressions: { type: "boolean" }, + nestedBinaryExpressions: { type: "boolean" }, + returnAssign: { type: "boolean" }, + ignoreJSX: { enum: ["none", "all", "single-line", "multi-line"] }, + enforceForArrowConditionals: { type: "boolean" }, + enforceForSequenceExpressions: { type: "boolean" }, + enforceForNewInMemberExpressions: { type: "boolean" }, + enforceForFunctionPrototypeMethods: { type: "boolean" }, + allowParensAfterCommentPattern: { type: "string" } + }, + additionalProperties: false + } + ], + minItems: 0, + maxItems: 2 + } + ] + }, + + messages: { + unexpected: "Unnecessary parentheses around expression." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + + const tokensToIgnore = new WeakSet(); + const precedence = astUtils.getPrecedence; + const ALL_NODES = context.options[0] !== "functions"; + const EXCEPT_COND_ASSIGN = ALL_NODES && context.options[1] && context.options[1].conditionalAssign === false; + const EXCEPT_COND_TERNARY = ALL_NODES && context.options[1] && context.options[1].ternaryOperandBinaryExpressions === false; + const NESTED_BINARY = ALL_NODES && context.options[1] && context.options[1].nestedBinaryExpressions === false; + const EXCEPT_RETURN_ASSIGN = ALL_NODES && context.options[1] && context.options[1].returnAssign === false; + const IGNORE_JSX = ALL_NODES && context.options[1] && context.options[1].ignoreJSX; + const IGNORE_ARROW_CONDITIONALS = ALL_NODES && context.options[1] && + context.options[1].enforceForArrowConditionals === false; + const IGNORE_SEQUENCE_EXPRESSIONS = ALL_NODES && context.options[1] && + context.options[1].enforceForSequenceExpressions === false; + const IGNORE_NEW_IN_MEMBER_EXPR = ALL_NODES && context.options[1] && + context.options[1].enforceForNewInMemberExpressions === false; + const IGNORE_FUNCTION_PROTOTYPE_METHODS = ALL_NODES && context.options[1] && + context.options[1].enforceForFunctionPrototypeMethods === false; + const ALLOW_PARENS_AFTER_COMMENT_PATTERN = ALL_NODES && context.options[1] && context.options[1].allowParensAfterCommentPattern; + + const PRECEDENCE_OF_ASSIGNMENT_EXPR = precedence({ type: "AssignmentExpression" }); + const PRECEDENCE_OF_UPDATE_EXPR = precedence({ type: "UpdateExpression" }); + + let reportsBuffer; + + /** + * Determines whether the given node is a `call` or `apply` method call, invoked directly on a `FunctionExpression` node. + * Example: function(){}.call() + * @param {ASTNode} node The node to be checked. + * @returns {boolean} True if the node is an immediate `call` or `apply` method call. + * @private + */ + function isImmediateFunctionPrototypeMethodCall(node) { + const callNode = astUtils.skipChainExpression(node); + + if (callNode.type !== "CallExpression") { + return false; + } + const callee = astUtils.skipChainExpression(callNode.callee); + + return ( + callee.type === "MemberExpression" && + callee.object.type === "FunctionExpression" && + ["call", "apply"].includes(astUtils.getStaticPropertyName(callee)) + ); + } + + /** + * Determines if this rule should be enforced for a node given the current configuration. + * @param {ASTNode} node The node to be checked. + * @returns {boolean} True if the rule should be enforced for this node. + * @private + */ + function ruleApplies(node) { + if (node.type === "JSXElement" || node.type === "JSXFragment") { + const isSingleLine = node.loc.start.line === node.loc.end.line; + + switch (IGNORE_JSX) { + + // Exclude this JSX element from linting + case "all": + return false; + + // Exclude this JSX element if it is multi-line element + case "multi-line": + return isSingleLine; + + // Exclude this JSX element if it is single-line element + case "single-line": + return !isSingleLine; + + // Nothing special to be done for JSX elements + case "none": + break; + + // no default + } + } + + if (node.type === "SequenceExpression" && IGNORE_SEQUENCE_EXPRESSIONS) { + return false; + } + + if (isImmediateFunctionPrototypeMethodCall(node) && IGNORE_FUNCTION_PROTOTYPE_METHODS) { + return false; + } + + return ALL_NODES || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression"; + } + + /** + * Determines if a node is surrounded by parentheses. + * @param {ASTNode} node The node to be checked. + * @returns {boolean} True if the node is parenthesised. + * @private + */ + function isParenthesised(node) { + return isParenthesizedRaw(1, node, sourceCode); + } + + /** + * Determines if a node is surrounded by parentheses twice. + * @param {ASTNode} node The node to be checked. + * @returns {boolean} True if the node is doubly parenthesised. + * @private + */ + function isParenthesisedTwice(node) { + return isParenthesizedRaw(2, node, sourceCode); + } + + /** + * Determines if a node is surrounded by (potentially) invalid parentheses. + * @param {ASTNode} node The node to be checked. + * @returns {boolean} True if the node is incorrectly parenthesised. + * @private + */ + function hasExcessParens(node) { + return ruleApplies(node) && isParenthesised(node); + } + + /** + * Determines if a node that is expected to be parenthesised is surrounded by + * (potentially) invalid extra parentheses. + * @param {ASTNode} node The node to be checked. + * @returns {boolean} True if the node is has an unexpected extra pair of parentheses. + * @private + */ + function hasDoubleExcessParens(node) { + return ruleApplies(node) && isParenthesisedTwice(node); + } + + /** + * Determines if a node that is expected to be parenthesised is surrounded by + * (potentially) invalid extra parentheses with considering precedence level of the node. + * If the preference level of the node is not higher or equal to precedence lower limit, it also checks + * whether the node is surrounded by parentheses twice or not. + * @param {ASTNode} node The node to be checked. + * @param {number} precedenceLowerLimit The lower limit of precedence. + * @returns {boolean} True if the node is has an unexpected extra pair of parentheses. + * @private + */ + function hasExcessParensWithPrecedence(node, precedenceLowerLimit) { + if (ruleApplies(node) && isParenthesised(node)) { + if ( + precedence(node) >= precedenceLowerLimit || + isParenthesisedTwice(node) + ) { + return true; + } + } + return false; + } + + /** + * Determines if a node test expression is allowed to have a parenthesised assignment + * @param {ASTNode} node The node to be checked. + * @returns {boolean} True if the assignment can be parenthesised. + * @private + */ + function isCondAssignException(node) { + return EXCEPT_COND_ASSIGN && node.test.type === "AssignmentExpression"; + } + + /** + * Determines if a node is in a return statement + * @param {ASTNode} node The node to be checked. + * @returns {boolean} True if the node is in a return statement. + * @private + */ + function isInReturnStatement(node) { + for (let currentNode = node; currentNode; currentNode = currentNode.parent) { + if ( + currentNode.type === "ReturnStatement" || + (currentNode.type === "ArrowFunctionExpression" && currentNode.body.type !== "BlockStatement") + ) { + return true; + } + } + + return false; + } + + /** + * Determines if a constructor function is newed-up with parens + * @param {ASTNode} newExpression The NewExpression node to be checked. + * @returns {boolean} True if the constructor is called with parens. + * @private + */ + function isNewExpressionWithParens(newExpression) { + const lastToken = sourceCode.getLastToken(newExpression); + const penultimateToken = sourceCode.getTokenBefore(lastToken); + + return newExpression.arguments.length > 0 || + ( + + // The expression should end with its own parens, e.g., new new foo() is not a new expression with parens + astUtils.isOpeningParenToken(penultimateToken) && + astUtils.isClosingParenToken(lastToken) && + newExpression.callee.range[1] < newExpression.range[1] + ); + } + + /** + * Determines if a node is or contains an assignment expression + * @param {ASTNode} node The node to be checked. + * @returns {boolean} True if the node is or contains an assignment expression. + * @private + */ + function containsAssignment(node) { + if (node.type === "AssignmentExpression") { + return true; + } + if (node.type === "ConditionalExpression" && + (node.consequent.type === "AssignmentExpression" || node.alternate.type === "AssignmentExpression")) { + return true; + } + if ((node.left && node.left.type === "AssignmentExpression") || + (node.right && node.right.type === "AssignmentExpression")) { + return true; + } + + return false; + } + + /** + * Determines if a node is contained by or is itself a return statement and is allowed to have a parenthesised assignment + * @param {ASTNode} node The node to be checked. + * @returns {boolean} True if the assignment can be parenthesised. + * @private + */ + function isReturnAssignException(node) { + if (!EXCEPT_RETURN_ASSIGN || !isInReturnStatement(node)) { + return false; + } + + if (node.type === "ReturnStatement") { + return node.argument && containsAssignment(node.argument); + } + if (node.type === "ArrowFunctionExpression" && node.body.type !== "BlockStatement") { + return containsAssignment(node.body); + } + return containsAssignment(node); + + } + + /** + * Determines if a node following a [no LineTerminator here] restriction is + * surrounded by (potentially) invalid extra parentheses. + * @param {Token} token The token preceding the [no LineTerminator here] restriction. + * @param {ASTNode} node The node to be checked. + * @returns {boolean} True if the node is incorrectly parenthesised. + * @private + */ + function hasExcessParensNoLineTerminator(token, node) { + if (token.loc.end.line === node.loc.start.line) { + return hasExcessParens(node); + } + + return hasDoubleExcessParens(node); + } + + /** + * Determines whether a node should be preceded by an additional space when removing parens + * @param {ASTNode} node node to evaluate; must be surrounded by parentheses + * @returns {boolean} `true` if a space should be inserted before the node + * @private + */ + function requiresLeadingSpace(node) { + const leftParenToken = sourceCode.getTokenBefore(node); + const tokenBeforeLeftParen = sourceCode.getTokenBefore(leftParenToken, { includeComments: true }); + const tokenAfterLeftParen = sourceCode.getTokenAfter(leftParenToken, { includeComments: true }); + + return tokenBeforeLeftParen && + tokenBeforeLeftParen.range[1] === leftParenToken.range[0] && + leftParenToken.range[1] === tokenAfterLeftParen.range[0] && + !astUtils.canTokensBeAdjacent(tokenBeforeLeftParen, tokenAfterLeftParen); + } + + /** + * Determines whether a node should be followed by an additional space when removing parens + * @param {ASTNode} node node to evaluate; must be surrounded by parentheses + * @returns {boolean} `true` if a space should be inserted after the node + * @private + */ + function requiresTrailingSpace(node) { + const nextTwoTokens = sourceCode.getTokensAfter(node, { count: 2 }); + const rightParenToken = nextTwoTokens[0]; + const tokenAfterRightParen = nextTwoTokens[1]; + const tokenBeforeRightParen = sourceCode.getLastToken(node); + + return rightParenToken && tokenAfterRightParen && + !sourceCode.isSpaceBetweenTokens(rightParenToken, tokenAfterRightParen) && + !astUtils.canTokensBeAdjacent(tokenBeforeRightParen, tokenAfterRightParen); + } + + /** + * Determines if a given expression node is an IIFE + * @param {ASTNode} node The node to check + * @returns {boolean} `true` if the given node is an IIFE + */ + function isIIFE(node) { + const maybeCallNode = astUtils.skipChainExpression(node); + + return maybeCallNode.type === "CallExpression" && maybeCallNode.callee.type === "FunctionExpression"; + } + + /** + * Determines if the given node can be the assignment target in destructuring or the LHS of an assignment. + * This is to avoid an autofix that could change behavior because parsers mistakenly allow invalid syntax, + * such as `(a = b) = c` and `[(a = b) = c] = []`. Ideally, this function shouldn't be necessary. + * @param {ASTNode} [node] The node to check + * @returns {boolean} `true` if the given node can be a valid assignment target + */ + function canBeAssignmentTarget(node) { + return node && (node.type === "Identifier" || node.type === "MemberExpression"); + } + + /** + * Checks if a node is fixable. + * A node is fixable if removing a single pair of surrounding parentheses does not turn it + * into a directive after fixing other nodes. + * Almost all nodes are fixable, except if all of the following conditions are met: + * The node is a string Literal + * It has a single pair of parentheses + * It is the only child of an ExpressionStatement + * @param {ASTNode} node The node to evaluate. + * @returns {boolean} Whether or not the node is fixable. + * @private + */ + function isFixable(node) { + + // if it's not a string literal it can be autofixed + if (node.type !== "Literal" || typeof node.value !== "string") { + return true; + } + if (isParenthesisedTwice(node)) { + return true; + } + return !astUtils.isTopLevelExpressionStatement(node.parent); + } + + /** + * Report the node + * @param {ASTNode} node node to evaluate + * @returns {void} + * @private + */ + function report(node) { + const leftParenToken = sourceCode.getTokenBefore(node); + const rightParenToken = sourceCode.getTokenAfter(node); + + if (!isParenthesisedTwice(node)) { + if (tokensToIgnore.has(sourceCode.getFirstToken(node))) { + return; + } + + if (isIIFE(node) && !isParenthesised(node.callee)) { + return; + } + + if (ALLOW_PARENS_AFTER_COMMENT_PATTERN) { + const commentsBeforeLeftParenToken = sourceCode.getCommentsBefore(leftParenToken); + const totalCommentsBeforeLeftParenTokenCount = commentsBeforeLeftParenToken.length; + const ignorePattern = new RegExp(ALLOW_PARENS_AFTER_COMMENT_PATTERN, "u"); + + if ( + totalCommentsBeforeLeftParenTokenCount > 0 && + ignorePattern.test(commentsBeforeLeftParenToken[totalCommentsBeforeLeftParenTokenCount - 1].value) + ) { + return; + } + } + } + + /** + * Finishes reporting + * @returns {void} + * @private + */ + function finishReport() { + context.report({ + node, + loc: leftParenToken.loc, + messageId: "unexpected", + fix: isFixable(node) + ? fixer => { + const parenthesizedSource = sourceCode.text.slice(leftParenToken.range[1], rightParenToken.range[0]); + + return fixer.replaceTextRange([ + leftParenToken.range[0], + rightParenToken.range[1] + ], (requiresLeadingSpace(node) ? " " : "") + parenthesizedSource + (requiresTrailingSpace(node) ? " " : "")); + } + : null + }); + } + + if (reportsBuffer) { + reportsBuffer.reports.push({ node, finishReport }); + return; + } + + finishReport(); + } + + /** + * Evaluate a argument of the node. + * @param {ASTNode} node node to evaluate + * @returns {void} + * @private + */ + function checkArgumentWithPrecedence(node) { + if (hasExcessParensWithPrecedence(node.argument, precedence(node))) { + report(node.argument); + } + } + + /** + * Check if a member expression contains a call expression + * @param {ASTNode} node MemberExpression node to evaluate + * @returns {boolean} true if found, false if not + */ + function doesMemberExpressionContainCallExpression(node) { + let currentNode = node.object; + let currentNodeType = node.object.type; + + while (currentNodeType === "MemberExpression") { + currentNode = currentNode.object; + currentNodeType = currentNode.type; + } + + return currentNodeType === "CallExpression"; + } + + /** + * Evaluate a new call + * @param {ASTNode} node node to evaluate + * @returns {void} + * @private + */ + function checkCallNew(node) { + const callee = node.callee; + + if (hasExcessParensWithPrecedence(callee, precedence(node))) { + if ( + hasDoubleExcessParens(callee) || + !( + isIIFE(node) || + + // (new A)(); new (new A)(); + ( + callee.type === "NewExpression" && + !isNewExpressionWithParens(callee) && + !( + node.type === "NewExpression" && + !isNewExpressionWithParens(node) + ) + ) || + + // new (a().b)(); new (a.b().c); + ( + node.type === "NewExpression" && + callee.type === "MemberExpression" && + doesMemberExpressionContainCallExpression(callee) + ) || + + // (a?.b)(); (a?.())(); + ( + !node.optional && + callee.type === "ChainExpression" + ) + ) + ) { + report(node.callee); + } + } + node.arguments + .filter(arg => hasExcessParensWithPrecedence(arg, PRECEDENCE_OF_ASSIGNMENT_EXPR)) + .forEach(report); + } + + /** + * Evaluate binary logicals + * @param {ASTNode} node node to evaluate + * @returns {void} + * @private + */ + function checkBinaryLogical(node) { + const prec = precedence(node); + const leftPrecedence = precedence(node.left); + const rightPrecedence = precedence(node.right); + const isExponentiation = node.operator === "**"; + const shouldSkipLeft = NESTED_BINARY && (node.left.type === "BinaryExpression" || node.left.type === "LogicalExpression"); + const shouldSkipRight = NESTED_BINARY && (node.right.type === "BinaryExpression" || node.right.type === "LogicalExpression"); + + if (!shouldSkipLeft && hasExcessParens(node.left)) { + if ( + !(["AwaitExpression", "UnaryExpression"].includes(node.left.type) && isExponentiation) && + !astUtils.isMixedLogicalAndCoalesceExpressions(node.left, node) && + (leftPrecedence > prec || (leftPrecedence === prec && !isExponentiation)) || + isParenthesisedTwice(node.left) + ) { + report(node.left); + } + } + + if (!shouldSkipRight && hasExcessParens(node.right)) { + if ( + !astUtils.isMixedLogicalAndCoalesceExpressions(node.right, node) && + (rightPrecedence > prec || (rightPrecedence === prec && isExponentiation)) || + isParenthesisedTwice(node.right) + ) { + report(node.right); + } + } + } + + /** + * Check the parentheses around the super class of the given class definition. + * @param {ASTNode} node The node of class declarations to check. + * @returns {void} + */ + function checkClass(node) { + if (!node.superClass) { + return; + } + + /* + * If `node.superClass` is a LeftHandSideExpression, parentheses are extra. + * Otherwise, parentheses are needed. + */ + const hasExtraParens = precedence(node.superClass) > PRECEDENCE_OF_UPDATE_EXPR + ? hasExcessParens(node.superClass) + : hasDoubleExcessParens(node.superClass); + + if (hasExtraParens) { + report(node.superClass); + } + } + + /** + * Check the parentheses around the argument of the given spread operator. + * @param {ASTNode} node The node of spread elements/properties to check. + * @returns {void} + */ + function checkSpreadOperator(node) { + if (hasExcessParensWithPrecedence(node.argument, PRECEDENCE_OF_ASSIGNMENT_EXPR)) { + report(node.argument); + } + } + + /** + * Checks the parentheses for an ExpressionStatement or ExportDefaultDeclaration + * @param {ASTNode} node The ExpressionStatement.expression or ExportDefaultDeclaration.declaration node + * @returns {void} + */ + function checkExpressionOrExportStatement(node) { + const firstToken = isParenthesised(node) ? sourceCode.getTokenBefore(node) : sourceCode.getFirstToken(node); + const secondToken = sourceCode.getTokenAfter(firstToken, astUtils.isNotOpeningParenToken); + const thirdToken = secondToken ? sourceCode.getTokenAfter(secondToken) : null; + const tokenAfterClosingParens = secondToken ? sourceCode.getTokenAfter(secondToken, astUtils.isNotClosingParenToken) : null; + + if ( + astUtils.isOpeningParenToken(firstToken) && + ( + astUtils.isOpeningBraceToken(secondToken) || + secondToken.type === "Keyword" && ( + secondToken.value === "function" || + secondToken.value === "class" || + secondToken.value === "let" && + tokenAfterClosingParens && + ( + astUtils.isOpeningBracketToken(tokenAfterClosingParens) || + tokenAfterClosingParens.type === "Identifier" + ) + ) || + secondToken && secondToken.type === "Identifier" && secondToken.value === "async" && thirdToken && thirdToken.type === "Keyword" && thirdToken.value === "function" + ) + ) { + tokensToIgnore.add(secondToken); + } + + const hasExtraParens = node.parent.type === "ExportDefaultDeclaration" + ? hasExcessParensWithPrecedence(node, PRECEDENCE_OF_ASSIGNMENT_EXPR) + : hasExcessParens(node); + + if (hasExtraParens) { + report(node); + } + } + + /** + * Finds the path from the given node to the specified ancestor. + * @param {ASTNode} node First node in the path. + * @param {ASTNode} ancestor Last node in the path. + * @returns {ASTNode[]} Path, including both nodes. + * @throws {Error} If the given node does not have the specified ancestor. + */ + function pathToAncestor(node, ancestor) { + const path = [node]; + let currentNode = node; + + while (currentNode !== ancestor) { + + currentNode = currentNode.parent; + + /* c8 ignore start */ + if (currentNode === null) { + throw new Error("Nodes are not in the ancestor-descendant relationship."); + }/* c8 ignore stop */ + + path.push(currentNode); + } + + return path; + } + + /** + * Finds the path from the given node to the specified descendant. + * @param {ASTNode} node First node in the path. + * @param {ASTNode} descendant Last node in the path. + * @returns {ASTNode[]} Path, including both nodes. + * @throws {Error} If the given node does not have the specified descendant. + */ + function pathToDescendant(node, descendant) { + return pathToAncestor(descendant, node).reverse(); + } + + /** + * Checks whether the syntax of the given ancestor of an 'in' expression inside a for-loop initializer + * is preventing the 'in' keyword from being interpreted as a part of an ill-formed for-in loop. + * @param {ASTNode} node Ancestor of an 'in' expression. + * @param {ASTNode} child Child of the node, ancestor of the same 'in' expression or the 'in' expression itself. + * @returns {boolean} True if the keyword 'in' would be interpreted as the 'in' operator, without any parenthesis. + */ + function isSafelyEnclosingInExpression(node, child) { + switch (node.type) { + case "ArrayExpression": + case "ArrayPattern": + case "BlockStatement": + case "ObjectExpression": + case "ObjectPattern": + case "TemplateLiteral": + return true; + case "ArrowFunctionExpression": + case "FunctionExpression": + return node.params.includes(child); + case "CallExpression": + case "NewExpression": + return node.arguments.includes(child); + case "MemberExpression": + return node.computed && node.property === child; + case "ConditionalExpression": + return node.consequent === child; + default: + return false; + } + } + + /** + * Starts a new reports buffering. Warnings will be stored in a buffer instead of being reported immediately. + * An additional logic that requires multiple nodes (e.g. a whole subtree) may dismiss some of the stored warnings. + * @returns {void} + */ + function startNewReportsBuffering() { + reportsBuffer = { + upper: reportsBuffer, + inExpressionNodes: [], + reports: [] + }; + } + + /** + * Ends the current reports buffering. + * @returns {void} + */ + function endCurrentReportsBuffering() { + const { upper, inExpressionNodes, reports } = reportsBuffer; + + if (upper) { + upper.inExpressionNodes.push(...inExpressionNodes); + upper.reports.push(...reports); + } else { + + // flush remaining reports + reports.forEach(({ finishReport }) => finishReport()); + } + + reportsBuffer = upper; + } + + /** + * Checks whether the given node is in the current reports buffer. + * @param {ASTNode} node Node to check. + * @returns {boolean} True if the node is in the current buffer, false otherwise. + */ + function isInCurrentReportsBuffer(node) { + return reportsBuffer.reports.some(r => r.node === node); + } + + /** + * Removes the given node from the current reports buffer. + * @param {ASTNode} node Node to remove. + * @returns {void} + */ + function removeFromCurrentReportsBuffer(node) { + reportsBuffer.reports = reportsBuffer.reports.filter(r => r.node !== node); + } + + /** + * Checks whether a node is a MemberExpression at NewExpression's callee. + * @param {ASTNode} node node to check. + * @returns {boolean} True if the node is a MemberExpression at NewExpression's callee. false otherwise. + */ + function isMemberExpInNewCallee(node) { + if (node.type === "MemberExpression") { + return node.parent.type === "NewExpression" && node.parent.callee === node + ? true + : node.parent.object === node && isMemberExpInNewCallee(node.parent); + } + return false; + } + + /** + * Checks if the left-hand side of an assignment is an identifier, the operator is one of + * `=`, `&&=`, `||=` or `??=` and the right-hand side is an anonymous class or function. + * + * As per https://tc39.es/ecma262/#sec-assignment-operators-runtime-semantics-evaluation, an + * assignment involving one of the operators `=`, `&&=`, `||=` or `??=` where the right-hand + * side is an anonymous class or function and the left-hand side is an *unparenthesized* + * identifier has different semantics than other assignments. + * Specifically, when an expression like `foo = function () {}` is evaluated, `foo.name` + * will be set to the string "foo", i.e. the identifier name. The same thing does not happen + * when evaluating `(foo) = function () {}`. + * Since the parenthesizing of the identifier in the left-hand side is significant in this + * special case, the parentheses, if present, should not be flagged as unnecessary. + * @param {ASTNode} node an AssignmentExpression node. + * @returns {boolean} `true` if the left-hand side of the assignment is an identifier, the + * operator is one of `=`, `&&=`, `||=` or `??=` and the right-hand side is an anonymous + * class or function; otherwise, `false`. + */ + function isAnonymousFunctionAssignmentException({ left, operator, right }) { + if (left.type === "Identifier" && ["=", "&&=", "||=", "??="].includes(operator)) { + const rhsType = right.type; + + if (rhsType === "ArrowFunctionExpression") { + return true; + } + if ((rhsType === "FunctionExpression" || rhsType === "ClassExpression") && !right.id) { + return true; + } + } + return false; + } + + return { + ArrayExpression(node) { + node.elements + .filter(e => e && hasExcessParensWithPrecedence(e, PRECEDENCE_OF_ASSIGNMENT_EXPR)) + .forEach(report); + }, + + ArrayPattern(node) { + node.elements + .filter(e => canBeAssignmentTarget(e) && hasExcessParens(e)) + .forEach(report); + }, + + ArrowFunctionExpression(node) { + if (isReturnAssignException(node)) { + return; + } + + if (node.body.type === "ConditionalExpression" && + IGNORE_ARROW_CONDITIONALS + ) { + return; + } + + if (node.body.type !== "BlockStatement") { + const firstBodyToken = sourceCode.getFirstToken(node.body, astUtils.isNotOpeningParenToken); + const tokenBeforeFirst = sourceCode.getTokenBefore(firstBodyToken); + + if (astUtils.isOpeningParenToken(tokenBeforeFirst) && astUtils.isOpeningBraceToken(firstBodyToken)) { + tokensToIgnore.add(firstBodyToken); + } + if (hasExcessParensWithPrecedence(node.body, PRECEDENCE_OF_ASSIGNMENT_EXPR)) { + report(node.body); + } + } + }, + + AssignmentExpression(node) { + if (canBeAssignmentTarget(node.left) && hasExcessParens(node.left) && + (!isAnonymousFunctionAssignmentException(node) || isParenthesisedTwice(node.left))) { + report(node.left); + } + + if (!isReturnAssignException(node) && hasExcessParensWithPrecedence(node.right, precedence(node))) { + report(node.right); + } + }, + + BinaryExpression(node) { + if (reportsBuffer && node.operator === "in") { + reportsBuffer.inExpressionNodes.push(node); + } + + checkBinaryLogical(node); + }, + + CallExpression: checkCallNew, + + ConditionalExpression(node) { + if (isReturnAssignException(node)) { + return; + } + + const availableTypes = new Set(["BinaryExpression", "LogicalExpression"]); + + if ( + !(EXCEPT_COND_TERNARY && availableTypes.has(node.test.type)) && + !isCondAssignException(node) && + hasExcessParensWithPrecedence(node.test, precedence({ type: "LogicalExpression", operator: "||" })) + ) { + report(node.test); + } + + if ( + !(EXCEPT_COND_TERNARY && availableTypes.has(node.consequent.type)) && + hasExcessParensWithPrecedence(node.consequent, PRECEDENCE_OF_ASSIGNMENT_EXPR)) { + report(node.consequent); + } + + if ( + !(EXCEPT_COND_TERNARY && availableTypes.has(node.alternate.type)) && + hasExcessParensWithPrecedence(node.alternate, PRECEDENCE_OF_ASSIGNMENT_EXPR)) { + report(node.alternate); + } + }, + + DoWhileStatement(node) { + if (hasExcessParens(node.test) && !isCondAssignException(node)) { + report(node.test); + } + }, + + ExportDefaultDeclaration: node => checkExpressionOrExportStatement(node.declaration), + ExpressionStatement: node => checkExpressionOrExportStatement(node.expression), + + ForInStatement(node) { + if (node.left.type !== "VariableDeclaration") { + const firstLeftToken = sourceCode.getFirstToken(node.left, astUtils.isNotOpeningParenToken); + + if ( + firstLeftToken.value === "let" && + astUtils.isOpeningBracketToken( + sourceCode.getTokenAfter(firstLeftToken, astUtils.isNotClosingParenToken) + ) + ) { + + // ForInStatement#left expression cannot start with `let[`. + tokensToIgnore.add(firstLeftToken); + } + } + + if (hasExcessParens(node.left)) { + report(node.left); + } + + if (hasExcessParens(node.right)) { + report(node.right); + } + }, + + ForOfStatement(node) { + if (node.left.type !== "VariableDeclaration") { + const firstLeftToken = sourceCode.getFirstToken(node.left, astUtils.isNotOpeningParenToken); + + if (firstLeftToken.value === "let") { + + // ForOfStatement#left expression cannot start with `let`. + tokensToIgnore.add(firstLeftToken); + } + } + + if (hasExcessParens(node.left)) { + report(node.left); + } + + if (hasExcessParensWithPrecedence(node.right, PRECEDENCE_OF_ASSIGNMENT_EXPR)) { + report(node.right); + } + }, + + ForStatement(node) { + if (node.test && hasExcessParens(node.test) && !isCondAssignException(node)) { + report(node.test); + } + + if (node.update && hasExcessParens(node.update)) { + report(node.update); + } + + if (node.init) { + + if (node.init.type !== "VariableDeclaration") { + const firstToken = sourceCode.getFirstToken(node.init, astUtils.isNotOpeningParenToken); + + if ( + firstToken.value === "let" && + astUtils.isOpeningBracketToken( + sourceCode.getTokenAfter(firstToken, astUtils.isNotClosingParenToken) + ) + ) { + + // ForStatement#init expression cannot start with `let[`. + tokensToIgnore.add(firstToken); + } + } + + startNewReportsBuffering(); + + if (hasExcessParens(node.init)) { + report(node.init); + } + } + }, + + "ForStatement > *.init:exit"(node) { + + /* + * Removing parentheses around `in` expressions might change semantics and cause errors. + * + * For example, this valid for loop: + * for (let a = (b in c); ;); + * after removing parentheses would be treated as an invalid for-in loop: + * for (let a = b in c; ;); + */ + + if (reportsBuffer.reports.length) { + reportsBuffer.inExpressionNodes.forEach(inExpressionNode => { + const path = pathToDescendant(node, inExpressionNode); + let nodeToExclude; + + for (let i = 0; i < path.length; i++) { + const pathNode = path[i]; + + if (i < path.length - 1) { + const nextPathNode = path[i + 1]; + + if (isSafelyEnclosingInExpression(pathNode, nextPathNode)) { + + // The 'in' expression in safely enclosed by the syntax of its ancestor nodes (e.g. by '{}' or '[]'). + return; + } + } + + if (isParenthesised(pathNode)) { + if (isInCurrentReportsBuffer(pathNode)) { + + // This node was supposed to be reported, but parentheses might be necessary. + + if (isParenthesisedTwice(pathNode)) { + + /* + * This node is parenthesised twice, it certainly has at least one pair of `extra` parentheses. + * If the --fix option is on, the current fixing iteration will remove only one pair of parentheses. + * The remaining pair is safely enclosing the 'in' expression. + */ + return; + } + + // Exclude the outermost node only. + if (!nodeToExclude) { + nodeToExclude = pathNode; + } + + // Don't break the loop here, there might be some safe nodes or parentheses that will stay inside. + + } else { + + // This node will stay parenthesised, the 'in' expression in safely enclosed by '()'. + return; + } + } + } + + // Exclude the node from the list (i.e. treat parentheses as necessary) + removeFromCurrentReportsBuffer(nodeToExclude); + }); + } + + endCurrentReportsBuffering(); + }, + + IfStatement(node) { + if (hasExcessParens(node.test) && !isCondAssignException(node)) { + report(node.test); + } + }, + + ImportExpression(node) { + const { source } = node; + + if (source.type === "SequenceExpression") { + if (hasDoubleExcessParens(source)) { + report(source); + } + } else if (hasExcessParens(source)) { + report(source); + } + }, + + LogicalExpression: checkBinaryLogical, + + MemberExpression(node) { + const shouldAllowWrapOnce = isMemberExpInNewCallee(node) && + doesMemberExpressionContainCallExpression(node); + const nodeObjHasExcessParens = shouldAllowWrapOnce + ? hasDoubleExcessParens(node.object) + : hasExcessParens(node.object) && + !( + isImmediateFunctionPrototypeMethodCall(node.parent) && + node.parent.callee === node && + IGNORE_FUNCTION_PROTOTYPE_METHODS + ); + + if ( + nodeObjHasExcessParens && + precedence(node.object) >= precedence(node) && + ( + node.computed || + !( + astUtils.isDecimalInteger(node.object) || + + // RegExp literal is allowed to have parens (#1589) + (node.object.type === "Literal" && node.object.regex) + ) + ) + ) { + report(node.object); + } + + if (nodeObjHasExcessParens && + node.object.type === "CallExpression" + ) { + report(node.object); + } + + if (nodeObjHasExcessParens && + !IGNORE_NEW_IN_MEMBER_EXPR && + node.object.type === "NewExpression" && + isNewExpressionWithParens(node.object)) { + report(node.object); + } + + if (nodeObjHasExcessParens && + node.optional && + node.object.type === "ChainExpression" + ) { + report(node.object); + } + + if (node.computed && hasExcessParens(node.property)) { + report(node.property); + } + }, + + "MethodDefinition[computed=true]"(node) { + if (hasExcessParensWithPrecedence(node.key, PRECEDENCE_OF_ASSIGNMENT_EXPR)) { + report(node.key); + } + }, + + NewExpression: checkCallNew, + + ObjectExpression(node) { + node.properties + .filter(property => property.value && hasExcessParensWithPrecedence(property.value, PRECEDENCE_OF_ASSIGNMENT_EXPR)) + .forEach(property => report(property.value)); + }, + + ObjectPattern(node) { + node.properties + .filter(property => { + const value = property.value; + + return canBeAssignmentTarget(value) && hasExcessParens(value); + }).forEach(property => report(property.value)); + }, + + Property(node) { + if (node.computed) { + const { key } = node; + + if (key && hasExcessParensWithPrecedence(key, PRECEDENCE_OF_ASSIGNMENT_EXPR)) { + report(key); + } + } + }, + + PropertyDefinition(node) { + if (node.computed && hasExcessParensWithPrecedence(node.key, PRECEDENCE_OF_ASSIGNMENT_EXPR)) { + report(node.key); + } + + if (node.value && hasExcessParensWithPrecedence(node.value, PRECEDENCE_OF_ASSIGNMENT_EXPR)) { + report(node.value); + } + }, + + RestElement(node) { + const argument = node.argument; + + if (canBeAssignmentTarget(argument) && hasExcessParens(argument)) { + report(argument); + } + }, + + ReturnStatement(node) { + const returnToken = sourceCode.getFirstToken(node); + + if (isReturnAssignException(node)) { + return; + } + + if (node.argument && + hasExcessParensNoLineTerminator(returnToken, node.argument) && + + // RegExp literal is allowed to have parens (#1589) + !(node.argument.type === "Literal" && node.argument.regex)) { + report(node.argument); + } + }, + + SequenceExpression(node) { + const precedenceOfNode = precedence(node); + + node.expressions + .filter(e => hasExcessParensWithPrecedence(e, precedenceOfNode)) + .forEach(report); + }, + + SwitchCase(node) { + if (node.test && hasExcessParens(node.test)) { + report(node.test); + } + }, + + SwitchStatement(node) { + if (hasExcessParens(node.discriminant)) { + report(node.discriminant); + } + }, + + ThrowStatement(node) { + const throwToken = sourceCode.getFirstToken(node); + + if (hasExcessParensNoLineTerminator(throwToken, node.argument)) { + report(node.argument); + } + }, + + UnaryExpression: checkArgumentWithPrecedence, + UpdateExpression(node) { + if (node.prefix) { + checkArgumentWithPrecedence(node); + } else { + const { argument } = node; + const operatorToken = sourceCode.getLastToken(node); + + if (argument.loc.end.line === operatorToken.loc.start.line) { + checkArgumentWithPrecedence(node); + } else { + if (hasDoubleExcessParens(argument)) { + report(argument); + } + } + } + }, + AwaitExpression: checkArgumentWithPrecedence, + + VariableDeclarator(node) { + if ( + node.init && hasExcessParensWithPrecedence(node.init, PRECEDENCE_OF_ASSIGNMENT_EXPR) && + + // RegExp literal is allowed to have parens (#1589) + !(node.init.type === "Literal" && node.init.regex) + ) { + report(node.init); + } + }, + + WhileStatement(node) { + if (hasExcessParens(node.test) && !isCondAssignException(node)) { + report(node.test); + } + }, + + WithStatement(node) { + if (hasExcessParens(node.object)) { + report(node.object); + } + }, + + YieldExpression(node) { + if (node.argument) { + const yieldToken = sourceCode.getFirstToken(node); + + if ((precedence(node.argument) >= precedence(node) && + hasExcessParensNoLineTerminator(yieldToken, node.argument)) || + hasDoubleExcessParens(node.argument)) { + report(node.argument); + } + } + }, + + ClassDeclaration: checkClass, + ClassExpression: checkClass, + + SpreadElement: checkSpreadOperator, + SpreadProperty: checkSpreadOperator, + ExperimentalSpreadProperty: checkSpreadOperator, + + TemplateLiteral(node) { + node.expressions + .filter(e => e && hasExcessParens(e)) + .forEach(report); + }, + + AssignmentPattern(node) { + const { left, right } = node; + + if (canBeAssignmentTarget(left) && hasExcessParens(left)) { + report(left); + } + + if (right && hasExcessParensWithPrecedence(right, PRECEDENCE_OF_ASSIGNMENT_EXPR)) { + report(right); + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-extra-semi.js b/node_modules/eslint/lib/rules/no-extra-semi.js index a408edaf9..af7eb8888 100644 --- a/node_modules/eslint/lib/rules/no-extra-semi.js +++ b/node_modules/eslint/lib/rules/no-extra-semi.js @@ -17,151 +17,131 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "no-extra-semi", - url: "https://eslint.style/rules/no-extra-semi", - }, - }, - ], - }, - type: "suggestion", - - docs: { - description: "Disallow unnecessary semicolons", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-extra-semi", - }, - - fixable: "code", - schema: [], - - messages: { - unexpected: "Unnecessary semicolon.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - /** - * Checks if a node or token is fixable. - * A node is fixable if it can be removed without turning a subsequent statement into a directive after fixing other nodes. - * @param {Token} nodeOrToken The node or token to check. - * @returns {boolean} Whether or not the node is fixable. - */ - function isFixable(nodeOrToken) { - const nextToken = sourceCode.getTokenAfter(nodeOrToken); - - if (!nextToken || nextToken.type !== "String") { - return true; - } - const stringNode = sourceCode.getNodeByRangeIndex( - nextToken.range[0], - ); - - return !astUtils.isTopLevelExpressionStatement(stringNode.parent); - } - - /** - * Reports an unnecessary semicolon error. - * @param {Node|Token} nodeOrToken A node or a token to be reported. - * @returns {void} - */ - function report(nodeOrToken) { - context.report({ - node: nodeOrToken, - messageId: "unexpected", - fix: isFixable(nodeOrToken) - ? fixer => - /* - * Expand the replacement range to include the surrounding - * tokens to avoid conflicting with semi. - * https://github.com/eslint/eslint/issues/7928 - */ - new FixTracker(fixer, context.sourceCode) - .retainSurroundingTokens(nodeOrToken) - .remove(nodeOrToken) - : null, - }); - } - - /** - * Checks for a part of a class body. - * This checks tokens from a specified token to a next MethodDefinition or the end of class body. - * @param {Token} firstToken The first token to check. - * @returns {void} - */ - function checkForPartOfClassBody(firstToken) { - for ( - let token = firstToken; - token.type === "Punctuator" && - !astUtils.isClosingBraceToken(token); - token = sourceCode.getTokenAfter(token) - ) { - if (astUtils.isSemicolonToken(token)) { - report(token); - } - } - } - - return { - /** - * Reports this empty statement, except if the parent node is a loop. - * @param {Node} node A EmptyStatement node to be reported. - * @returns {void} - */ - EmptyStatement(node) { - const parent = node.parent, - allowedParentTypes = [ - "ForStatement", - "ForInStatement", - "ForOfStatement", - "WhileStatement", - "DoWhileStatement", - "IfStatement", - "LabeledStatement", - "WithStatement", - ]; - - if (!allowedParentTypes.includes(parent.type)) { - report(node); - } - }, - - /** - * Checks tokens from the head of this class body to the first MethodDefinition or the end of this class body. - * @param {Node} node A ClassBody node to check. - * @returns {void} - */ - ClassBody(node) { - checkForPartOfClassBody(sourceCode.getFirstToken(node, 1)); // 0 is `{`. - }, - - /** - * Checks tokens from this MethodDefinition to the next MethodDefinition or the end of this class body. - * @param {Node} node A MethodDefinition node of the start point. - * @returns {void} - */ - "MethodDefinition, PropertyDefinition, StaticBlock"(node) { - checkForPartOfClassBody(sourceCode.getTokenAfter(node)); - }, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "suggestion", + + docs: { + description: "Disallow unnecessary semicolons", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-extra-semi" + }, + + fixable: "code", + schema: [], + + messages: { + unexpected: "Unnecessary semicolon." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + + /** + * Checks if a node or token is fixable. + * A node is fixable if it can be removed without turning a subsequent statement into a directive after fixing other nodes. + * @param {Token} nodeOrToken The node or token to check. + * @returns {boolean} Whether or not the node is fixable. + */ + function isFixable(nodeOrToken) { + const nextToken = sourceCode.getTokenAfter(nodeOrToken); + + if (!nextToken || nextToken.type !== "String") { + return true; + } + const stringNode = sourceCode.getNodeByRangeIndex(nextToken.range[0]); + + return !astUtils.isTopLevelExpressionStatement(stringNode.parent); + } + + /** + * Reports an unnecessary semicolon error. + * @param {Node|Token} nodeOrToken A node or a token to be reported. + * @returns {void} + */ + function report(nodeOrToken) { + context.report({ + node: nodeOrToken, + messageId: "unexpected", + fix: isFixable(nodeOrToken) + ? fixer => + + /* + * Expand the replacement range to include the surrounding + * tokens to avoid conflicting with semi. + * https://github.com/eslint/eslint/issues/7928 + */ + new FixTracker(fixer, context.sourceCode) + .retainSurroundingTokens(nodeOrToken) + .remove(nodeOrToken) + : null + }); + } + + /** + * Checks for a part of a class body. + * This checks tokens from a specified token to a next MethodDefinition or the end of class body. + * @param {Token} firstToken The first token to check. + * @returns {void} + */ + function checkForPartOfClassBody(firstToken) { + for (let token = firstToken; + token.type === "Punctuator" && !astUtils.isClosingBraceToken(token); + token = sourceCode.getTokenAfter(token) + ) { + if (astUtils.isSemicolonToken(token)) { + report(token); + } + } + } + + return { + + /** + * Reports this empty statement, except if the parent node is a loop. + * @param {Node} node A EmptyStatement node to be reported. + * @returns {void} + */ + EmptyStatement(node) { + const parent = node.parent, + allowedParentTypes = [ + "ForStatement", + "ForInStatement", + "ForOfStatement", + "WhileStatement", + "DoWhileStatement", + "IfStatement", + "LabeledStatement", + "WithStatement" + ]; + + if (!allowedParentTypes.includes(parent.type)) { + report(node); + } + }, + + /** + * Checks tokens from the head of this class body to the first MethodDefinition or the end of this class body. + * @param {Node} node A ClassBody node to check. + * @returns {void} + */ + ClassBody(node) { + checkForPartOfClassBody(sourceCode.getFirstToken(node, 1)); // 0 is `{`. + }, + + /** + * Checks tokens from this MethodDefinition to the next MethodDefinition or the end of this class body. + * @param {Node} node A MethodDefinition node of the start point. + * @returns {void} + */ + "MethodDefinition, PropertyDefinition, StaticBlock"(node) { + checkForPartOfClassBody(sourceCode.getTokenAfter(node)); + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-fallthrough.js b/node_modules/eslint/lib/rules/no-fallthrough.js index 4dd210d7b..91da12120 100644 --- a/node_modules/eslint/lib/rules/no-fallthrough.js +++ b/node_modules/eslint/lib/rules/no-fallthrough.js @@ -22,13 +22,14 @@ const DEFAULT_FALLTHROUGH_COMMENT = /falls?\s?through/iu; * @returns {boolean} True if any segment is reachable; false otherwise. */ function isAnySegmentReachable(segments) { - for (const segment of segments) { - if (segment.reachable) { - return true; - } - } - return false; + for (const segment of segments) { + if (segment.reachable) { + return true; + } + } + + return false; } /** @@ -38,10 +39,7 @@ function isAnySegmentReachable(segments) { * @returns {boolean} `true` if the comment string is truly a fallthrough comment. */ function isFallThroughComment(comment, fallthroughCommentPattern) { - return ( - fallthroughCommentPattern.test(comment) && - !directivesPattern.test(comment.trim()) - ); + return fallthroughCommentPattern.test(comment) && !directivesPattern.test(comment.trim()); } /** @@ -50,48 +48,23 @@ function isFallThroughComment(comment, fallthroughCommentPattern) { * @param {ASTNode} subsequentCase The case after caseWhichFallsThrough. * @param {RuleContext} context A rule context which stores comments. * @param {RegExp} fallthroughCommentPattern A pattern to match comment to. - * @returns {null | object} the comment if the case has a valid fallthrough comment, otherwise null + * @returns {boolean} `true` if the case has a valid fallthrough comment. */ -function getFallthroughComment( - caseWhichFallsThrough, - subsequentCase, - context, - fallthroughCommentPattern, -) { - const sourceCode = context.sourceCode; - - if ( - caseWhichFallsThrough.consequent.length === 1 && - caseWhichFallsThrough.consequent[0].type === "BlockStatement" - ) { - const trailingCloseBrace = sourceCode.getLastToken( - caseWhichFallsThrough.consequent[0], - ); - const commentInBlock = sourceCode - .getCommentsBefore(trailingCloseBrace) - .pop(); - - if ( - commentInBlock && - isFallThroughComment( - commentInBlock.value, - fallthroughCommentPattern, - ) - ) { - return commentInBlock; - } - } - - const comment = sourceCode.getCommentsBefore(subsequentCase).pop(); - - if ( - comment && - isFallThroughComment(comment.value, fallthroughCommentPattern) - ) { - return comment; - } - - return null; +function hasFallthroughComment(caseWhichFallsThrough, subsequentCase, context, fallthroughCommentPattern) { + const sourceCode = context.sourceCode; + + if (caseWhichFallsThrough.consequent.length === 1 && caseWhichFallsThrough.consequent[0].type === "BlockStatement") { + const trailingCloseBrace = sourceCode.getLastToken(caseWhichFallsThrough.consequent[0]); + const commentInBlock = sourceCode.getCommentsBefore(trailingCloseBrace).pop(); + + if (commentInBlock && isFallThroughComment(commentInBlock.value, fallthroughCommentPattern)) { + return true; + } + } + + const comment = sourceCode.getCommentsBefore(subsequentCase).pop(); + + return Boolean(comment && isFallThroughComment(comment.value, fallthroughCommentPattern)); } /** @@ -101,160 +74,123 @@ function getFallthroughComment( * @returns {boolean} `true` if there are blank lines between node and token */ function hasBlankLinesBetween(node, token) { - return token.loc.start.line > node.loc.end.line + 1; + return token.loc.start.line > node.loc.end.line + 1; } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - defaultOptions: [ - { - allowEmptyCase: false, - reportUnusedFallthroughComment: false, - }, - ], - - docs: { - description: "Disallow fallthrough of `case` statements", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-fallthrough", - }, - - schema: [ - { - type: "object", - properties: { - commentPattern: { - type: "string", - }, - allowEmptyCase: { - type: "boolean", - }, - reportUnusedFallthroughComment: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - messages: { - unusedFallthroughComment: - "Found a comment that would permit fallthrough, but case cannot fall through.", - case: "Expected a 'break' statement before 'case'.", - default: "Expected a 'break' statement before 'default'.", - }, - }, - - create(context) { - const codePathSegments = []; - let currentCodePathSegments = new Set(); - const sourceCode = context.sourceCode; - const [ - { allowEmptyCase, commentPattern, reportUnusedFallthroughComment }, - ] = context.options; - const fallthroughCommentPattern = commentPattern - ? new RegExp(commentPattern, "u") - : DEFAULT_FALLTHROUGH_COMMENT; - - /* - * We need to use leading comments of the next SwitchCase node because - * trailing comments is wrong if semicolons are omitted. - */ - let previousCase = null; - - return { - onCodePathStart() { - codePathSegments.push(currentCodePathSegments); - currentCodePathSegments = new Set(); - }, - - onCodePathEnd() { - currentCodePathSegments = codePathSegments.pop(); - }, - - onUnreachableCodePathSegmentStart(segment) { - currentCodePathSegments.add(segment); - }, - - onUnreachableCodePathSegmentEnd(segment) { - currentCodePathSegments.delete(segment); - }, - - onCodePathSegmentStart(segment) { - currentCodePathSegments.add(segment); - }, - - onCodePathSegmentEnd(segment) { - currentCodePathSegments.delete(segment); - }, - - SwitchCase(node) { - /* - * Checks whether or not there is a fallthrough comment. - * And reports the previous fallthrough node if that does not exist. - */ - - if (previousCase && previousCase.node.parent === node.parent) { - const previousCaseFallthroughComment = - getFallthroughComment( - previousCase.node, - node, - context, - fallthroughCommentPattern, - ); - - if ( - previousCase.isFallthrough && - !previousCaseFallthroughComment - ) { - context.report({ - messageId: node.test ? "case" : "default", - node, - }); - } else if ( - reportUnusedFallthroughComment && - !previousCase.isSwitchExitReachable && - previousCaseFallthroughComment - ) { - context.report({ - messageId: "unusedFallthroughComment", - node: previousCaseFallthroughComment, - }); - } - } - previousCase = null; - }, - - "SwitchCase:exit"(node) { - const nextToken = sourceCode.getTokenAfter(node); - - /* - * `reachable` meant fall through because statements preceded by - * `break`, `return`, or `throw` are unreachable. - * And allows empty cases and the last case. - */ - const isSwitchExitReachable = isAnySegmentReachable( - currentCodePathSegments, - ); - const isFallthrough = - isSwitchExitReachable && - (node.consequent.length > 0 || - (!allowEmptyCase && - hasBlankLinesBetween(node, nextToken))) && - node.parent.cases.at(-1) !== node; - - previousCase = { - node, - isSwitchExitReachable, - isFallthrough, - }; - }, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow fallthrough of `case` statements", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-fallthrough" + }, + + schema: [ + { + type: "object", + properties: { + commentPattern: { + type: "string", + default: "" + }, + allowEmptyCase: { + type: "boolean", + default: false + } + }, + additionalProperties: false + } + ], + messages: { + case: "Expected a 'break' statement before 'case'.", + default: "Expected a 'break' statement before 'default'." + } + }, + + create(context) { + const options = context.options[0] || {}; + const codePathSegments = []; + let currentCodePathSegments = new Set(); + const sourceCode = context.sourceCode; + const allowEmptyCase = options.allowEmptyCase || false; + + /* + * We need to use leading comments of the next SwitchCase node because + * trailing comments is wrong if semicolons are omitted. + */ + let fallthroughCase = null; + let fallthroughCommentPattern = null; + + if (options.commentPattern) { + fallthroughCommentPattern = new RegExp(options.commentPattern, "u"); + } else { + fallthroughCommentPattern = DEFAULT_FALLTHROUGH_COMMENT; + } + return { + + onCodePathStart() { + codePathSegments.push(currentCodePathSegments); + currentCodePathSegments = new Set(); + }, + + onCodePathEnd() { + currentCodePathSegments = codePathSegments.pop(); + }, + + onUnreachableCodePathSegmentStart(segment) { + currentCodePathSegments.add(segment); + }, + + onUnreachableCodePathSegmentEnd(segment) { + currentCodePathSegments.delete(segment); + }, + + onCodePathSegmentStart(segment) { + currentCodePathSegments.add(segment); + }, + + onCodePathSegmentEnd(segment) { + currentCodePathSegments.delete(segment); + }, + + + SwitchCase(node) { + + /* + * Checks whether or not there is a fallthrough comment. + * And reports the previous fallthrough node if that does not exist. + */ + + if (fallthroughCase && (!hasFallthroughComment(fallthroughCase, node, context, fallthroughCommentPattern))) { + context.report({ + messageId: node.test ? "case" : "default", + node + }); + } + fallthroughCase = null; + }, + + "SwitchCase:exit"(node) { + const nextToken = sourceCode.getTokenAfter(node); + + /* + * `reachable` meant fall through because statements preceded by + * `break`, `return`, or `throw` are unreachable. + * And allows empty cases and the last case. + */ + if (isAnySegmentReachable(currentCodePathSegments) && + (node.consequent.length > 0 || (!allowEmptyCase && hasBlankLinesBetween(node, nextToken))) && + node.parent.cases[node.parent.cases.length - 1] !== node) { + fallthroughCase = node; + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-floating-decimal.js b/node_modules/eslint/lib/rules/no-floating-decimal.js index 569be727e..80e4994cd 100644 --- a/node_modules/eslint/lib/rules/no-floating-decimal.js +++ b/node_modules/eslint/lib/rules/no-floating-decimal.js @@ -16,84 +16,58 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "no-floating-decimal", - url: "https://eslint.style/rules/no-floating-decimal", - }, - }, - ], - }, - type: "suggestion", + meta: { + deprecated: true, + replacedBy: [], + type: "suggestion", - docs: { - description: - "Disallow leading or trailing decimal points in numeric literals", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-floating-decimal", - }, + docs: { + description: "Disallow leading or trailing decimal points in numeric literals", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-floating-decimal" + }, - schema: [], - fixable: "code", - messages: { - leading: "A leading decimal point can be confused with a dot.", - trailing: "A trailing decimal point can be confused with a dot.", - }, - }, + schema: [], + fixable: "code", + messages: { + leading: "A leading decimal point can be confused with a dot.", + trailing: "A trailing decimal point can be confused with a dot." + } + }, - create(context) { - const sourceCode = context.sourceCode; + create(context) { + const sourceCode = context.sourceCode; - return { - Literal(node) { - if (typeof node.value === "number") { - if (node.raw.startsWith(".")) { - context.report({ - node, - messageId: "leading", - fix(fixer) { - const tokenBefore = - sourceCode.getTokenBefore(node); - const needsSpaceBefore = - tokenBefore && - tokenBefore.range[1] === node.range[0] && - !astUtils.canTokensBeAdjacent( - tokenBefore, - `0${node.raw}`, - ); + return { + Literal(node) { - return fixer.insertTextBefore( - node, - needsSpaceBefore ? " 0" : "0", - ); - }, - }); - } - if (node.raw.indexOf(".") === node.raw.length - 1) { - context.report({ - node, - messageId: "trailing", - fix: fixer => fixer.insertTextAfter(node, "0"), - }); - } - } - }, - }; - }, + if (typeof node.value === "number") { + if (node.raw.startsWith(".")) { + context.report({ + node, + messageId: "leading", + fix(fixer) { + const tokenBefore = sourceCode.getTokenBefore(node); + const needsSpaceBefore = tokenBefore && + tokenBefore.range[1] === node.range[0] && + !astUtils.canTokensBeAdjacent(tokenBefore, `0${node.raw}`); + + return fixer.insertTextBefore(node, needsSpaceBefore ? " 0" : "0"); + } + }); + } + if (node.raw.indexOf(".") === node.raw.length - 1) { + context.report({ + node, + messageId: "trailing", + fix: fixer => fixer.insertTextAfter(node, "0") + }); + } + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-func-assign.js b/node_modules/eslint/lib/rules/no-func-assign.js index a17d4b8e6..8084af6eb 100644 --- a/node_modules/eslint/lib/rules/no-func-assign.js +++ b/node_modules/eslint/lib/rules/no-func-assign.js @@ -11,67 +11,68 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", + meta: { + type: "problem", - docs: { - description: "Disallow reassigning `function` declarations", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-func-assign", - }, + docs: { + description: "Disallow reassigning `function` declarations", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-func-assign" + }, - schema: [], + schema: [], - messages: { - isAFunction: "'{{name}}' is a function.", - }, - }, + messages: { + isAFunction: "'{{name}}' is a function." + } + }, - create(context) { - const sourceCode = context.sourceCode; + create(context) { - /** - * Reports a reference if is non initializer and writable. - * @param {References} references Collection of reference to check. - * @returns {void} - */ - function checkReference(references) { - astUtils.getModifyingReferences(references).forEach(reference => { - context.report({ - node: reference.identifier, - messageId: "isAFunction", - data: { - name: reference.identifier.name, - }, - }); - }); - } + const sourceCode = context.sourceCode; - /** - * Finds and reports references that are non initializer and writable. - * @param {Variable} variable A variable to check. - * @returns {void} - */ - function checkVariable(variable) { - if (variable.defs[0].type === "FunctionName") { - checkReference(variable.references); - } - } + /** + * Reports a reference if is non initializer and writable. + * @param {References} references Collection of reference to check. + * @returns {void} + */ + function checkReference(references) { + astUtils.getModifyingReferences(references).forEach(reference => { + context.report({ + node: reference.identifier, + messageId: "isAFunction", + data: { + name: reference.identifier.name + } + }); + }); + } - /** - * Checks parameters of a given function node. - * @param {ASTNode} node A function node to check. - * @returns {void} - */ - function checkForFunction(node) { - sourceCode.getDeclaredVariables(node).forEach(checkVariable); - } + /** + * Finds and reports references that are non initializer and writable. + * @param {Variable} variable A variable to check. + * @returns {void} + */ + function checkVariable(variable) { + if (variable.defs[0].type === "FunctionName") { + checkReference(variable.references); + } + } - return { - FunctionDeclaration: checkForFunction, - FunctionExpression: checkForFunction, - }; - }, + /** + * Checks parameters of a given function node. + * @param {ASTNode} node A function node to check. + * @returns {void} + */ + function checkForFunction(node) { + sourceCode.getDeclaredVariables(node).forEach(checkVariable); + } + + return { + FunctionDeclaration: checkForFunction, + FunctionExpression: checkForFunction + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-global-assign.js b/node_modules/eslint/lib/rules/no-global-assign.js index 6f2384495..99ae7a2ee 100644 --- a/node_modules/eslint/lib/rules/no-global-assign.js +++ b/node_modules/eslint/lib/rules/no-global-assign.js @@ -9,93 +9,87 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", + meta: { + type: "suggestion", - defaultOptions: [{ exceptions: [] }], + docs: { + description: "Disallow assignments to native objects or read-only global variables", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-global-assign" + }, - docs: { - description: - "Disallow assignments to native objects or read-only global variables", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-global-assign", - }, + schema: [ + { + type: "object", + properties: { + exceptions: { + type: "array", + items: { type: "string" }, + uniqueItems: true + } + }, + additionalProperties: false + } + ], - schema: [ - { - type: "object", - properties: { - exceptions: { - type: "array", - items: { type: "string" }, - uniqueItems: true, - }, - }, - additionalProperties: false, - }, - ], + messages: { + globalShouldNotBeModified: "Read-only global '{{name}}' should not be modified." + } + }, - messages: { - globalShouldNotBeModified: - "Read-only global '{{name}}' should not be modified.", - }, - }, + create(context) { + const config = context.options[0]; + const sourceCode = context.sourceCode; + const exceptions = (config && config.exceptions) || []; - create(context) { - const sourceCode = context.sourceCode; - const [{ exceptions }] = context.options; + /** + * Reports write references. + * @param {Reference} reference A reference to check. + * @param {int} index The index of the reference in the references. + * @param {Reference[]} references The array that the reference belongs to. + * @returns {void} + */ + function checkReference(reference, index, references) { + const identifier = reference.identifier; - /** - * Reports write references. - * @param {Reference} reference A reference to check. - * @param {number} index The index of the reference in the references. - * @param {Reference[]} references The array that the reference belongs to. - * @returns {void} - */ - function checkReference(reference, index, references) { - const identifier = reference.identifier; + if (reference.init === false && + reference.isWrite() && - if ( - reference.init === false && - reference.isWrite() && - /* - * Destructuring assignments can have multiple default value, - * so possibly there are multiple writeable references for the same identifier. - */ - (index === 0 || references[index - 1].identifier !== identifier) - ) { - context.report({ - node: identifier, - messageId: "globalShouldNotBeModified", - data: { - name: identifier.name, - }, - }); - } - } + /* + * Destructuring assignments can have multiple default value, + * so possibly there are multiple writeable references for the same identifier. + */ + (index === 0 || references[index - 1].identifier !== identifier) + ) { + context.report({ + node: identifier, + messageId: "globalShouldNotBeModified", + data: { + name: identifier.name + } + }); + } + } - /** - * Reports write references if a given variable is read-only builtin. - * @param {Variable} variable A variable to check. - * @returns {void} - */ - function checkVariable(variable) { - if ( - variable.writeable === false && - !exceptions.includes(variable.name) - ) { - variable.references.forEach(checkReference); - } - } + /** + * Reports write references if a given variable is read-only builtin. + * @param {Variable} variable A variable to check. + * @returns {void} + */ + function checkVariable(variable) { + if (variable.writeable === false && !exceptions.includes(variable.name)) { + variable.references.forEach(checkReference); + } + } - return { - Program(node) { - const globalScope = sourceCode.getScope(node); + return { + Program(node) { + const globalScope = sourceCode.getScope(node); - globalScope.variables.forEach(checkVariable); - }, - }; - }, + globalScope.variables.forEach(checkVariable); + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-implicit-coercion.js b/node_modules/eslint/lib/rules/no-implicit-coercion.js index fb6d6a8e9..36baad383 100644 --- a/node_modules/eslint/lib/rules/no-implicit-coercion.js +++ b/node_modules/eslint/lib/rules/no-implicit-coercion.js @@ -12,7 +12,22 @@ const astUtils = require("./utils/ast-utils"); //------------------------------------------------------------------------------ const INDEX_OF_PATTERN = /^(?:i|lastI)ndexOf$/u; -const ALLOWABLE_OPERATORS = ["~", "!!", "+", "- -", "-", "*"]; +const ALLOWABLE_OPERATORS = ["~", "!!", "+", "*"]; + +/** + * Parses and normalizes an option object. + * @param {Object} options An option object to parse. + * @returns {Object} The parsed and normalized option object. + */ +function parseOptions(options) { + return { + boolean: "boolean" in options ? options.boolean : true, + number: "number" in options ? options.number : true, + string: "string" in options ? options.string : true, + disallowTemplateShorthand: "disallowTemplateShorthand" in options ? options.disallowTemplateShorthand : false, + allow: options.allow || [] + }; +} /** * Checks whether or not a node is a double logical negating. @@ -20,11 +35,11 @@ const ALLOWABLE_OPERATORS = ["~", "!!", "+", "- -", "-", "*"]; * @returns {boolean} Whether or not the node is a double logical negating. */ function isDoubleLogicalNegating(node) { - return ( - node.operator === "!" && - node.argument.type === "UnaryExpression" && - node.argument.operator === "!" - ); + return ( + node.operator === "!" && + node.argument.type === "UnaryExpression" && + node.argument.operator === "!" + ); } /** @@ -33,15 +48,15 @@ function isDoubleLogicalNegating(node) { * @returns {boolean} Whether or not the node is a binary negating of `.indexOf()` method calling. */ function isBinaryNegatingOfIndexOf(node) { - if (node.operator !== "~") { - return false; - } - const callNode = astUtils.skipChainExpression(node.argument); - - return ( - callNode.type === "CallExpression" && - astUtils.isSpecificMemberAccess(callNode.callee, null, INDEX_OF_PATTERN) - ); + if (node.operator !== "~") { + return false; + } + const callNode = astUtils.skipChainExpression(node.argument); + + return ( + callNode.type === "CallExpression" && + astUtils.isSpecificMemberAccess(callNode.callee, null, INDEX_OF_PATTERN) + ); } /** @@ -50,11 +65,10 @@ function isBinaryNegatingOfIndexOf(node) { * @returns {boolean} Whether or not the node is a multiplying by one. */ function isMultiplyByOne(node) { - return ( - node.operator === "*" && - ((node.left.type === "Literal" && node.left.value === 1) || - (node.right.type === "Literal" && node.right.value === 1)) - ); + return node.operator === "*" && ( + node.left.type === "Literal" && node.left.value === 1 || + node.right.type === "Literal" && node.right.value === 1 + ); } /** @@ -66,16 +80,13 @@ function isMultiplyByOne(node) { * @returns {boolean} Whether or not the node is a multiplying by a fraction of `1`. */ function isMultiplyByFractionOfOne(node, sourceCode) { - return ( - node.type === "BinaryExpression" && - node.operator === "*" && - node.right.type === "Literal" && - node.right.value === 1 && - node.parent.type === "BinaryExpression" && - node.parent.operator === "/" && - node.parent.left === node && - !astUtils.isParenthesised(sourceCode, node) - ); + return node.type === "BinaryExpression" && + node.operator === "*" && + (node.right.type === "Literal" && node.right.value === 1) && + node.parent.type === "BinaryExpression" && + node.parent.operator === "/" && + node.parent.left === node && + !astUtils.isParenthesised(sourceCode, node); } /** @@ -84,13 +95,14 @@ function isMultiplyByFractionOfOne(node, sourceCode) { * @returns {boolean} true if the node is a number literal or a `Number()`, `parseInt` or `parseFloat` call */ function isNumeric(node) { - return ( - (node.type === "Literal" && typeof node.value === "number") || - (node.type === "CallExpression" && - (node.callee.name === "Number" || - node.callee.name === "parseInt" || - node.callee.name === "parseFloat")) - ); + return ( + node.type === "Literal" && typeof node.value === "number" || + node.type === "CallExpression" && ( + node.callee.name === "Number" || + node.callee.name === "parseInt" || + node.callee.name === "parseFloat" + ) + ); } /** @@ -101,18 +113,18 @@ function isNumeric(node) { * @returns {ASTNode|null} The first non-numeric item in the BinaryExpression tree or null */ function getNonNumericOperand(node) { - const left = node.left, - right = node.right; + const left = node.left, + right = node.right; - if (right.type !== "BinaryExpression" && !isNumeric(right)) { - return right; - } + if (right.type !== "BinaryExpression" && !isNumeric(right)) { + return right; + } - if (left.type !== "BinaryExpression" && !isNumeric(left)) { - return left; - } + if (left.type !== "BinaryExpression" && !isNumeric(left)) { + return left; + } - return null; + return null; } /** @@ -121,12 +133,12 @@ function getNonNumericOperand(node) { * @returns {boolean} Whether or not the expression evaluates to a string. */ function isStringType(node) { - return ( - astUtils.isStringLiteral(node) || - (node.type === "CallExpression" && - node.callee.type === "Identifier" && - node.callee.name === "String") - ); + return astUtils.isStringLiteral(node) || + ( + node.type === "CallExpression" && + node.callee.type === "Identifier" && + node.callee.name === "String" + ); } /** @@ -136,13 +148,7 @@ function isStringType(node) { * empty string literal or not. */ function isEmptyString(node) { - return ( - astUtils.isStringLiteral(node) && - (node.value === "" || - (node.type === "TemplateLiteral" && - node.quasis.length === 1 && - node.quasis[0].value.cooked === "")) - ); + return astUtils.isStringLiteral(node) && (node.value === "" || (node.type === "TemplateLiteral" && node.quasis.length === 1 && node.quasis[0].value.cooked === "")); } /** @@ -151,11 +157,10 @@ function isEmptyString(node) { * @returns {boolean} Whether or not the node is a concatenating with an empty string. */ function isConcatWithEmptyString(node) { - return ( - node.operator === "+" && - ((isEmptyString(node.left) && !isStringType(node.right)) || - (isEmptyString(node.right) && !isStringType(node.left))) - ); + return node.operator === "+" && ( + (isEmptyString(node.left) && !isStringType(node.right)) || + (isEmptyString(node.right) && !isStringType(node.left)) + ); } /** @@ -164,7 +169,7 @@ function isConcatWithEmptyString(node) { * @returns {boolean} Whether or not the node is appended with an empty string. */ function isAppendEmptyString(node) { - return node.operator === "+=" && isEmptyString(node.right); + return node.operator === "+=" && isEmptyString(node.right); } /** @@ -173,296 +178,203 @@ function isAppendEmptyString(node) { * @returns {ASTNode} The operand that is not an empty string from a flagged BinaryExpression. */ function getNonEmptyOperand(node) { - return isEmptyString(node.left) ? node.right : node.left; + return isEmptyString(node.left) ? node.right : node.left; } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - hasSuggestions: true, - type: "suggestion", - - docs: { - description: "Disallow shorthand type conversions", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/no-implicit-coercion", - }, - - fixable: "code", - - schema: [ - { - type: "object", - properties: { - boolean: { - type: "boolean", - }, - number: { - type: "boolean", - }, - string: { - type: "boolean", - }, - disallowTemplateShorthand: { - type: "boolean", - }, - allow: { - type: "array", - items: { - enum: ALLOWABLE_OPERATORS, - }, - uniqueItems: true, - }, - }, - additionalProperties: false, - }, - ], - - defaultOptions: [ - { - allow: [], - boolean: true, - disallowTemplateShorthand: false, - number: true, - string: true, - }, - ], - - messages: { - implicitCoercion: - "Unexpected implicit coercion encountered. Use `{{recommendation}}` instead.", - useRecommendation: "Use `{{recommendation}}` instead.", - }, - }, - - create(context) { - const [options] = context.options; - const sourceCode = context.sourceCode; - - /** - * Reports an error and autofixes the node - * @param {ASTNode} node An ast node to report the error on. - * @param {string} recommendation The recommended code for the issue - * @param {bool} shouldSuggest Whether this report should offer a suggestion - * @param {bool} shouldFix Whether this report should fix the node - * @returns {void} - */ - function report(node, recommendation, shouldSuggest, shouldFix) { - /** - * Fix function - * @param {RuleFixer} fixer The fixer to fix. - * @returns {Fix} The fix object. - */ - function fix(fixer) { - const tokenBefore = sourceCode.getTokenBefore(node); - - if ( - tokenBefore?.range[1] === node.range[0] && - !astUtils.canTokensBeAdjacent(tokenBefore, recommendation) - ) { - return fixer.replaceText(node, ` ${recommendation}`); - } - - return fixer.replaceText(node, recommendation); - } - - context.report({ - node, - messageId: "implicitCoercion", - data: { recommendation }, - fix(fixer) { - if (!shouldFix) { - return null; - } - - return fix(fixer); - }, - suggest: [ - { - messageId: "useRecommendation", - data: { recommendation }, - fix(fixer) { - if (shouldFix || !shouldSuggest) { - return null; - } - - return fix(fixer); - }, - }, - ], - }); - } - - return { - UnaryExpression(node) { - let operatorAllowed; - - // !!foo - operatorAllowed = options.allow.includes("!!"); - if ( - !operatorAllowed && - options.boolean && - isDoubleLogicalNegating(node) - ) { - const recommendation = `Boolean(${sourceCode.getText(node.argument.argument)})`; - const variable = astUtils.getVariableByName( - sourceCode.getScope(node), - "Boolean", - ); - const booleanExists = variable?.identifiers.length === 0; - - report(node, recommendation, true, booleanExists); - } - - // ~foo.indexOf(bar) - operatorAllowed = options.allow.includes("~"); - if ( - !operatorAllowed && - options.boolean && - isBinaryNegatingOfIndexOf(node) - ) { - // `foo?.indexOf(bar) !== -1` will be true (== found) if the `foo` is nullish. So use `>= 0` in that case. - const comparison = - node.argument.type === "ChainExpression" - ? ">= 0" - : "!== -1"; - const recommendation = `${sourceCode.getText(node.argument)} ${comparison}`; - - report(node, recommendation, false, false); - } - - // +foo - operatorAllowed = options.allow.includes("+"); - if ( - !operatorAllowed && - options.number && - node.operator === "+" && - !isNumeric(node.argument) - ) { - const recommendation = `Number(${sourceCode.getText(node.argument)})`; - - report(node, recommendation, true, false); - } - - // -(-foo) - operatorAllowed = options.allow.includes("- -"); - if ( - !operatorAllowed && - options.number && - node.operator === "-" && - node.argument.type === "UnaryExpression" && - node.argument.operator === "-" && - !isNumeric(node.argument.argument) - ) { - const recommendation = `Number(${sourceCode.getText(node.argument.argument)})`; - - report(node, recommendation, true, false); - } - }, - - // Use `:exit` to prevent double reporting - "BinaryExpression:exit"(node) { - let operatorAllowed; - - // 1 * foo - operatorAllowed = options.allow.includes("*"); - const nonNumericOperand = - !operatorAllowed && - options.number && - isMultiplyByOne(node) && - !isMultiplyByFractionOfOne(node, sourceCode) && - getNonNumericOperand(node); - - if (nonNumericOperand) { - const recommendation = `Number(${sourceCode.getText(nonNumericOperand)})`; - - report(node, recommendation, true, false); - } - - // foo - 0 - operatorAllowed = options.allow.includes("-"); - if ( - !operatorAllowed && - options.number && - node.operator === "-" && - node.right.type === "Literal" && - node.right.value === 0 && - !isNumeric(node.left) - ) { - const recommendation = `Number(${sourceCode.getText(node.left)})`; - - report(node, recommendation, true, false); - } - - // "" + foo - operatorAllowed = options.allow.includes("+"); - if ( - !operatorAllowed && - options.string && - isConcatWithEmptyString(node) - ) { - const recommendation = `String(${sourceCode.getText(getNonEmptyOperand(node))})`; - - report(node, recommendation, true, false); - } - }, - - AssignmentExpression(node) { - // foo += "" - const operatorAllowed = options.allow.includes("+"); - - if ( - !operatorAllowed && - options.string && - isAppendEmptyString(node) - ) { - const code = sourceCode.getText(getNonEmptyOperand(node)); - const recommendation = `${code} = String(${code})`; - - report(node, recommendation, true, false); - } - }, - - TemplateLiteral(node) { - if (!options.disallowTemplateShorthand) { - return; - } - - // tag`${foo}` - if (node.parent.type === "TaggedTemplateExpression") { - return; - } - - // `` or `${foo}${bar}` - if (node.expressions.length !== 1) { - return; - } - - // `prefix${foo}` - if (node.quasis[0].value.cooked !== "") { - return; - } - - // `${foo}postfix` - if (node.quasis[1].value.cooked !== "") { - return; - } - - // if the expression is already a string, then this isn't a coercion - if (isStringType(node.expressions[0])) { - return; - } - - const code = sourceCode.getText(node.expressions[0]); - const recommendation = `String(${code})`; - - report(node, recommendation, true, false); - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow shorthand type conversions", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-implicit-coercion" + }, + + fixable: "code", + + schema: [{ + type: "object", + properties: { + boolean: { + type: "boolean", + default: true + }, + number: { + type: "boolean", + default: true + }, + string: { + type: "boolean", + default: true + }, + disallowTemplateShorthand: { + type: "boolean", + default: false + }, + allow: { + type: "array", + items: { + enum: ALLOWABLE_OPERATORS + }, + uniqueItems: true + } + }, + additionalProperties: false + }], + + messages: { + useRecommendation: "use `{{recommendation}}` instead." + } + }, + + create(context) { + const options = parseOptions(context.options[0] || {}); + const sourceCode = context.sourceCode; + + /** + * Reports an error and autofixes the node + * @param {ASTNode} node An ast node to report the error on. + * @param {string} recommendation The recommended code for the issue + * @param {bool} shouldFix Whether this report should fix the node + * @returns {void} + */ + function report(node, recommendation, shouldFix) { + context.report({ + node, + messageId: "useRecommendation", + data: { + recommendation + }, + fix(fixer) { + if (!shouldFix) { + return null; + } + + const tokenBefore = sourceCode.getTokenBefore(node); + + if ( + tokenBefore && + tokenBefore.range[1] === node.range[0] && + !astUtils.canTokensBeAdjacent(tokenBefore, recommendation) + ) { + return fixer.replaceText(node, ` ${recommendation}`); + } + return fixer.replaceText(node, recommendation); + } + }); + } + + return { + UnaryExpression(node) { + let operatorAllowed; + + // !!foo + operatorAllowed = options.allow.includes("!!"); + if (!operatorAllowed && options.boolean && isDoubleLogicalNegating(node)) { + const recommendation = `Boolean(${sourceCode.getText(node.argument.argument)})`; + + report(node, recommendation, true); + } + + // ~foo.indexOf(bar) + operatorAllowed = options.allow.includes("~"); + if (!operatorAllowed && options.boolean && isBinaryNegatingOfIndexOf(node)) { + + // `foo?.indexOf(bar) !== -1` will be true (== found) if the `foo` is nullish. So use `>= 0` in that case. + const comparison = node.argument.type === "ChainExpression" ? ">= 0" : "!== -1"; + const recommendation = `${sourceCode.getText(node.argument)} ${comparison}`; + + report(node, recommendation, false); + } + + // +foo + operatorAllowed = options.allow.includes("+"); + if (!operatorAllowed && options.number && node.operator === "+" && !isNumeric(node.argument)) { + const recommendation = `Number(${sourceCode.getText(node.argument)})`; + + report(node, recommendation, true); + } + }, + + // Use `:exit` to prevent double reporting + "BinaryExpression:exit"(node) { + let operatorAllowed; + + // 1 * foo + operatorAllowed = options.allow.includes("*"); + const nonNumericOperand = !operatorAllowed && options.number && isMultiplyByOne(node) && !isMultiplyByFractionOfOne(node, sourceCode) && + getNonNumericOperand(node); + + if (nonNumericOperand) { + const recommendation = `Number(${sourceCode.getText(nonNumericOperand)})`; + + report(node, recommendation, true); + } + + // "" + foo + operatorAllowed = options.allow.includes("+"); + if (!operatorAllowed && options.string && isConcatWithEmptyString(node)) { + const recommendation = `String(${sourceCode.getText(getNonEmptyOperand(node))})`; + + report(node, recommendation, true); + } + }, + + AssignmentExpression(node) { + + // foo += "" + const operatorAllowed = options.allow.includes("+"); + + if (!operatorAllowed && options.string && isAppendEmptyString(node)) { + const code = sourceCode.getText(getNonEmptyOperand(node)); + const recommendation = `${code} = String(${code})`; + + report(node, recommendation, true); + } + }, + + TemplateLiteral(node) { + if (!options.disallowTemplateShorthand) { + return; + } + + // tag`${foo}` + if (node.parent.type === "TaggedTemplateExpression") { + return; + } + + // `` or `${foo}${bar}` + if (node.expressions.length !== 1) { + return; + } + + + // `prefix${foo}` + if (node.quasis[0].value.cooked !== "") { + return; + } + + // `${foo}postfix` + if (node.quasis[1].value.cooked !== "") { + return; + } + + // if the expression is already a string, then this isn't a coercion + if (isStringType(node.expressions[0])) { + return; + } + + const code = sourceCode.getText(node.expressions[0]); + const recommendation = `String(${code})`; + + report(node, recommendation, true); + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-implicit-globals.js b/node_modules/eslint/lib/rules/no-implicit-globals.js index c291cd86b..2a182477c 100644 --- a/node_modules/eslint/lib/rules/no-implicit-globals.js +++ b/node_modules/eslint/lib/rules/no-implicit-globals.js @@ -5,183 +5,142 @@ "use strict"; -const ASSIGNMENT_NODES = new Set([ - "AssignmentExpression", - "ForInStatement", - "ForOfStatement", -]); - //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - defaultOptions: [ - { - lexicalBindings: false, - }, - ], - - docs: { - description: "Disallow declarations in the global scope", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-implicit-globals", - }, - - schema: [ - { - type: "object", - properties: { - lexicalBindings: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - - messages: { - globalNonLexicalBinding: - "Unexpected {{kind}} declaration in the global scope, wrap in an IIFE for a local variable, assign as global property for a global variable.", - globalLexicalBinding: - "Unexpected {{kind}} declaration in the global scope, wrap in a block or in an IIFE.", - globalVariableLeak: - "Global variable leak, declare the variable if it is intended to be local.", - assignmentToReadonlyGlobal: - "Unexpected assignment to read-only global variable.", - redeclarationOfReadonlyGlobal: - "Unexpected redeclaration of read-only global variable.", - }, - }, - - create(context) { - const [{ lexicalBindings: checkLexicalBindings }] = context.options; - const sourceCode = context.sourceCode; - - /** - * Reports the node. - * @param {ASTNode} node Node to report. - * @param {string} messageId Id of the message to report. - * @param {string|undefined} kind Declaration kind, can be 'var', 'const', 'let', function or class. - * @returns {void} - */ - function report(node, messageId, kind) { - context.report({ - node, - messageId, - data: { - kind, - }, - }); - } - - return { - Program(node) { - const scope = sourceCode.getScope(node); - - scope.variables.forEach(variable => { - // Only ESLint global variables have the `writable` key. - const isReadonlyEslintGlobalVariable = - variable.writeable === false; - const isWritableEslintGlobalVariable = - variable.writeable === true; - - if (isWritableEslintGlobalVariable) { - // Everything is allowed with writable ESLint global variables. - return; - } - - // Variables exported by "exported" block comments - if (variable.eslintExported) { - return; - } - - variable.defs.forEach(def => { - const defNode = def.node; - - if ( - def.type === "FunctionName" || - (def.type === "Variable" && - def.parent.kind === "var") - ) { - if (isReadonlyEslintGlobalVariable) { - report( - defNode, - "redeclarationOfReadonlyGlobal", - ); - } else { - report( - defNode, - "globalNonLexicalBinding", - def.type === "FunctionName" - ? "function" - : `'${def.parent.kind}'`, - ); - } - } - - if (checkLexicalBindings) { - if ( - def.type === "ClassName" || - (def.type === "Variable" && - (def.parent.kind === "let" || - def.parent.kind === "const")) - ) { - if (isReadonlyEslintGlobalVariable) { - report( - defNode, - "redeclarationOfReadonlyGlobal", - ); - } else { - report( - defNode, - "globalLexicalBinding", - def.type === "ClassName" - ? "class" - : `'${def.parent.kind}'`, - ); - } - } - } - }); - - if ( - isReadonlyEslintGlobalVariable && - variable.defs.length === 0 - ) { - variable.references.forEach(reference => { - if (reference.isWrite() && !reference.isRead()) { - let assignmentParent = - reference.identifier.parent; - - while ( - assignmentParent && - !ASSIGNMENT_NODES.has(assignmentParent.type) - ) { - assignmentParent = assignmentParent.parent; - } - - report( - assignmentParent ?? reference.identifier, - "assignmentToReadonlyGlobal", - ); - } - }); - } - }); - - // Undeclared assigned variables. - scope.implicit.variables.forEach(variable => { - // def.node is an AssignmentExpression, ForInStatement or ForOfStatement. - variable.defs.forEach(def => { - report(def.node, "globalVariableLeak"); - }); - }); - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow declarations in the global scope", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-implicit-globals" + }, + + schema: [{ + type: "object", + properties: { + lexicalBindings: { + type: "boolean", + default: false + } + }, + additionalProperties: false + }], + + messages: { + globalNonLexicalBinding: "Unexpected {{kind}} declaration in the global scope, wrap in an IIFE for a local variable, assign as global property for a global variable.", + globalLexicalBinding: "Unexpected {{kind}} declaration in the global scope, wrap in a block or in an IIFE.", + globalVariableLeak: "Global variable leak, declare the variable if it is intended to be local.", + assignmentToReadonlyGlobal: "Unexpected assignment to read-only global variable.", + redeclarationOfReadonlyGlobal: "Unexpected redeclaration of read-only global variable." + } + }, + + create(context) { + + const checkLexicalBindings = context.options[0] && context.options[0].lexicalBindings === true; + const sourceCode = context.sourceCode; + + /** + * Reports the node. + * @param {ASTNode} node Node to report. + * @param {string} messageId Id of the message to report. + * @param {string|undefined} kind Declaration kind, can be 'var', 'const', 'let', function or class. + * @returns {void} + */ + function report(node, messageId, kind) { + context.report({ + node, + messageId, + data: { + kind + } + }); + } + + return { + Program(node) { + const scope = sourceCode.getScope(node); + + scope.variables.forEach(variable => { + + // Only ESLint global variables have the `writable` key. + const isReadonlyEslintGlobalVariable = variable.writeable === false; + const isWritableEslintGlobalVariable = variable.writeable === true; + + if (isWritableEslintGlobalVariable) { + + // Everything is allowed with writable ESLint global variables. + return; + } + + // Variables exported by "exported" block comments + if (variable.eslintExported) { + return; + } + + variable.defs.forEach(def => { + const defNode = def.node; + + if (def.type === "FunctionName" || (def.type === "Variable" && def.parent.kind === "var")) { + if (isReadonlyEslintGlobalVariable) { + report(defNode, "redeclarationOfReadonlyGlobal"); + } else { + report( + defNode, + "globalNonLexicalBinding", + def.type === "FunctionName" ? "function" : `'${def.parent.kind}'` + ); + } + } + + if (checkLexicalBindings) { + if (def.type === "ClassName" || + (def.type === "Variable" && (def.parent.kind === "let" || def.parent.kind === "const"))) { + if (isReadonlyEslintGlobalVariable) { + report(defNode, "redeclarationOfReadonlyGlobal"); + } else { + report( + defNode, + "globalLexicalBinding", + def.type === "ClassName" ? "class" : `'${def.parent.kind}'` + ); + } + } + } + }); + }); + + // Undeclared assigned variables. + scope.implicit.variables.forEach(variable => { + const scopeVariable = scope.set.get(variable.name); + let messageId; + + if (scopeVariable) { + + // ESLint global variable + if (scopeVariable.writeable) { + return; + } + messageId = "assignmentToReadonlyGlobal"; + + } else { + + // Reference to an unknown variable, possible global leak. + messageId = "globalVariableLeak"; + } + + // def.node is an AssignmentExpression, ForInStatement or ForOfStatement. + variable.defs.forEach(def => { + report(def.node, messageId); + }); + }); + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-implied-eval.js b/node_modules/eslint/lib/rules/no-implied-eval.js index 7757d5a73..9a84f8cba 100644 --- a/node_modules/eslint/lib/rules/no-implied-eval.js +++ b/node_modules/eslint/lib/rules/no-implied-eval.js @@ -16,155 +16,117 @@ const { getStaticValue } = require("@eslint-community/eslint-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: "Disallow the use of `eval()`-like methods", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-implied-eval", - }, - - schema: [], - - messages: { - impliedEval: - "Implied eval. Consider passing a function instead of a string.", - execScript: "Implied eval. Do not use execScript().", - }, - }, - - create(context) { - const GLOBAL_CANDIDATES = Object.freeze([ - "global", - "window", - "globalThis", - ]); - const EVAL_LIKE_FUNC_PATTERN = - /^(?:set(?:Interval|Timeout)|execScript)$/u; - const sourceCode = context.sourceCode; - - /** - * Checks whether a node is evaluated as a string or not. - * @param {ASTNode} node A node to check. - * @returns {boolean} True if the node is evaluated as a string. - */ - function isEvaluatedString(node) { - if ( - (node.type === "Literal" && typeof node.value === "string") || - node.type === "TemplateLiteral" - ) { - return true; - } - if (node.type === "BinaryExpression" && node.operator === "+") { - return ( - isEvaluatedString(node.left) || - isEvaluatedString(node.right) - ); - } - return false; - } - - /** - * Reports if the `CallExpression` node has evaluated argument. - * @param {ASTNode} node A CallExpression to check. - * @returns {void} - */ - function reportImpliedEvalCallExpression(node) { - const [firstArgument] = node.arguments; - - if (firstArgument) { - const staticValue = getStaticValue( - firstArgument, - sourceCode.getScope(node), - ); - const isStaticString = - staticValue && typeof staticValue.value === "string"; - const isString = - isStaticString || isEvaluatedString(firstArgument); - - if (isString) { - const calleeName = - node.callee.type === "Identifier" - ? node.callee.name - : astUtils.getStaticPropertyName(node.callee); - const isExecScript = calleeName === "execScript"; - context.report({ - node, - messageId: isExecScript ? "execScript" : "impliedEval", - }); - } - } - } - - /** - * Reports calls of `implied eval` via the global references. - * @param {Variable} globalVar A global variable to check. - * @returns {void} - */ - function reportImpliedEvalViaGlobal(globalVar) { - const { references, name } = globalVar; - - references.forEach(ref => { - const identifier = ref.identifier; - let node = identifier.parent; - - while (astUtils.isSpecificMemberAccess(node, null, name)) { - node = node.parent; - } - - if ( - astUtils.isSpecificMemberAccess( - node, - null, - EVAL_LIKE_FUNC_PATTERN, - ) - ) { - const calleeNode = - node.parent.type === "ChainExpression" - ? node.parent - : node; - const parent = calleeNode.parent; - - if ( - parent.type === "CallExpression" && - parent.callee === calleeNode - ) { - reportImpliedEvalCallExpression(parent); - } - } - }); - } - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - return { - CallExpression(node) { - if ( - astUtils.isSpecificId( - node.callee, - EVAL_LIKE_FUNC_PATTERN, - ) && - sourceCode.isGlobalReference(node.callee) - ) { - reportImpliedEvalCallExpression(node); - } - }, - "Program:exit"(node) { - const globalScope = sourceCode.getScope(node); - - GLOBAL_CANDIDATES.map(candidate => - astUtils.getVariableByName(globalScope, candidate), - ) - .filter( - globalVar => !!globalVar && globalVar.defs.length === 0, - ) - .forEach(reportImpliedEvalViaGlobal); - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow the use of `eval()`-like methods", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-implied-eval" + }, + + schema: [], + + messages: { + impliedEval: "Implied eval. Consider passing a function instead of a string." + } + }, + + create(context) { + const GLOBAL_CANDIDATES = Object.freeze(["global", "window", "globalThis"]); + const EVAL_LIKE_FUNC_PATTERN = /^(?:set(?:Interval|Timeout)|execScript)$/u; + const sourceCode = context.sourceCode; + + /** + * Checks whether a node is evaluated as a string or not. + * @param {ASTNode} node A node to check. + * @returns {boolean} True if the node is evaluated as a string. + */ + function isEvaluatedString(node) { + if ( + (node.type === "Literal" && typeof node.value === "string") || + node.type === "TemplateLiteral" + ) { + return true; + } + if (node.type === "BinaryExpression" && node.operator === "+") { + return isEvaluatedString(node.left) || isEvaluatedString(node.right); + } + return false; + } + + /** + * Reports if the `CallExpression` node has evaluated argument. + * @param {ASTNode} node A CallExpression to check. + * @returns {void} + */ + function reportImpliedEvalCallExpression(node) { + const [firstArgument] = node.arguments; + + if (firstArgument) { + + const staticValue = getStaticValue(firstArgument, sourceCode.getScope(node)); + const isStaticString = staticValue && typeof staticValue.value === "string"; + const isString = isStaticString || isEvaluatedString(firstArgument); + + if (isString) { + context.report({ + node, + messageId: "impliedEval" + }); + } + } + + } + + /** + * Reports calls of `implied eval` via the global references. + * @param {Variable} globalVar A global variable to check. + * @returns {void} + */ + function reportImpliedEvalViaGlobal(globalVar) { + const { references, name } = globalVar; + + references.forEach(ref => { + const identifier = ref.identifier; + let node = identifier.parent; + + while (astUtils.isSpecificMemberAccess(node, null, name)) { + node = node.parent; + } + + if (astUtils.isSpecificMemberAccess(node, null, EVAL_LIKE_FUNC_PATTERN)) { + const calleeNode = node.parent.type === "ChainExpression" ? node.parent : node; + const parent = calleeNode.parent; + + if (parent.type === "CallExpression" && parent.callee === calleeNode) { + reportImpliedEvalCallExpression(parent); + } + } + }); + } + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + return { + CallExpression(node) { + if (astUtils.isSpecificId(node.callee, EVAL_LIKE_FUNC_PATTERN)) { + reportImpliedEvalCallExpression(node); + } + }, + "Program:exit"(node) { + const globalScope = sourceCode.getScope(node); + + GLOBAL_CANDIDATES + .map(candidate => astUtils.getVariableByName(globalScope, candidate)) + .filter(globalVar => !!globalVar && globalVar.defs.length === 0) + .forEach(reportImpliedEvalViaGlobal); + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-import-assign.js b/node_modules/eslint/lib/rules/no-import-assign.js index 7fe526b66..c69988666 100644 --- a/node_modules/eslint/lib/rules/no-import-assign.js +++ b/node_modules/eslint/lib/rules/no-import-assign.js @@ -13,8 +13,8 @@ const { findVariable } = require("@eslint-community/eslint-utils"); const astUtils = require("./utils/ast-utils"); const WellKnownMutationFunctions = { - Object: /^(?:assign|definePropert(?:y|ies)|freeze|setPrototypeOf)$/u, - Reflect: /^(?:(?:define|delete)Property|set(?:PrototypeOf)?)$/u, + Object: /^(?:assign|definePropert(?:y|ies)|freeze|setPrototypeOf)$/u, + Reflect: /^(?:(?:define|delete)Property|set(?:PrototypeOf)?)$/u }; /** @@ -23,18 +23,27 @@ const WellKnownMutationFunctions = { * @returns {boolean} `true` if the node is LHS. */ function isAssignmentLeft(node) { - const { parent } = node; - - return ( - (parent.type === "AssignmentExpression" && parent.left === node) || - // Destructuring assignments - parent.type === "ArrayPattern" || - (parent.type === "Property" && - parent.value === node && - parent.parent.type === "ObjectPattern") || - parent.type === "RestElement" || - (parent.type === "AssignmentPattern" && parent.left === node) - ); + const { parent } = node; + + return ( + ( + parent.type === "AssignmentExpression" && + parent.left === node + ) || + + // Destructuring assignments + parent.type === "ArrayPattern" || + ( + parent.type === "Property" && + parent.value === node && + parent.parent.type === "ObjectPattern" + ) || + parent.type === "RestElement" || + ( + parent.type === "AssignmentPattern" && + parent.left === node + ) + ); } /** @@ -43,17 +52,22 @@ function isAssignmentLeft(node) { * @returns {boolean} `true` if the node is the operand of mutation unary operator. */ function isOperandOfMutationUnaryOperator(node) { - const argumentNode = - node.parent.type === "ChainExpression" ? node.parent : node; - const { parent } = argumentNode; - - return ( - (parent.type === "UpdateExpression" && - parent.argument === argumentNode) || - (parent.type === "UnaryExpression" && - parent.operator === "delete" && - parent.argument === argumentNode) - ); + const argumentNode = node.parent.type === "ChainExpression" + ? node.parent + : node; + const { parent } = argumentNode; + + return ( + ( + parent.type === "UpdateExpression" && + parent.argument === argumentNode + ) || + ( + parent.type === "UnaryExpression" && + parent.operator === "delete" && + parent.argument === argumentNode + ) + ); } /** @@ -62,12 +76,18 @@ function isOperandOfMutationUnaryOperator(node) { * @returns {boolean} `true` if the node is the iteration variable. */ function isIterationVariable(node) { - const { parent } = node; - - return ( - (parent.type === "ForInStatement" && parent.left === node) || - (parent.type === "ForOfStatement" && parent.left === node) - ); + const { parent } = node; + + return ( + ( + parent.type === "ForInStatement" && + parent.left === node + ) || + ( + parent.type === "ForOfStatement" && + parent.left === node + ) + ); } /** @@ -86,30 +106,22 @@ function isIterationVariable(node) { * @returns {boolean} `true` if the node is at the first argument of a well-known mutation function. */ function isArgumentOfWellKnownMutationFunction(node, scope) { - const { parent } = node; - - if (parent.type !== "CallExpression" || parent.arguments[0] !== node) { - return false; - } - const callee = astUtils.skipChainExpression(parent.callee); - - if ( - !astUtils.isSpecificMemberAccess( - callee, - "Object", - WellKnownMutationFunctions.Object, - ) && - !astUtils.isSpecificMemberAccess( - callee, - "Reflect", - WellKnownMutationFunctions.Reflect, - ) - ) { - return false; - } - const variable = findVariable(scope, callee.object); - - return variable !== null && variable.scope.type === "global"; + const { parent } = node; + + if (parent.type !== "CallExpression" || parent.arguments[0] !== node) { + return false; + } + const callee = astUtils.skipChainExpression(parent.callee); + + if ( + !astUtils.isSpecificMemberAccess(callee, "Object", WellKnownMutationFunctions.Object) && + !astUtils.isSpecificMemberAccess(callee, "Reflect", WellKnownMutationFunctions.Reflect) + ) { + return false; + } + const variable = findVariable(scope, callee.object); + + return variable !== null && variable.scope.type === "global"; } /** @@ -119,16 +131,20 @@ function isArgumentOfWellKnownMutationFunction(node, scope) { * @returns {boolean} `true` if the member of `id` was updated. */ function isMemberWrite(id, scope) { - const { parent } = id; - - return ( - (parent.type === "MemberExpression" && - parent.object === id && - (isAssignmentLeft(parent) || - isOperandOfMutationUnaryOperator(parent) || - isIterationVariable(parent))) || - isArgumentOfWellKnownMutationFunction(id, scope) - ); + const { parent } = id; + + return ( + ( + parent.type === "MemberExpression" && + parent.object === id && + ( + isAssignmentLeft(parent) || + isOperandOfMutationUnaryOperator(parent) || + isIterationVariable(parent) + ) + ) || + isArgumentOfWellKnownMutationFunction(id, scope) + ); } /** @@ -137,91 +153,89 @@ function isMemberWrite(id, scope) { * @returns {ASTNode} The mutation node. */ function getWriteNode(id) { - let node = id.parent; - - while ( - node && - node.type !== "AssignmentExpression" && - node.type !== "UpdateExpression" && - node.type !== "UnaryExpression" && - node.type !== "CallExpression" && - node.type !== "ForInStatement" && - node.type !== "ForOfStatement" - ) { - node = node.parent; - } - - return node || id; + let node = id.parent; + + while ( + node && + node.type !== "AssignmentExpression" && + node.type !== "UpdateExpression" && + node.type !== "UnaryExpression" && + node.type !== "CallExpression" && + node.type !== "ForInStatement" && + node.type !== "ForOfStatement" + ) { + node = node.parent; + } + + return node || id; } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - docs: { - description: "Disallow assigning to imported bindings", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-import-assign", - }, - - schema: [], - - messages: { - readonly: "'{{name}}' is read-only.", - readonlyMember: "The members of '{{name}}' are read-only.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - return { - ImportDeclaration(node) { - const scope = sourceCode.getScope(node); - - for (const variable of sourceCode.getDeclaredVariables(node)) { - const shouldCheckMembers = variable.defs.some( - d => d.node.type === "ImportNamespaceSpecifier", - ); - let prevIdNode = null; - - for (const reference of variable.references) { - const idNode = reference.identifier; - - /* - * AssignmentPattern (e.g. `[a = 0] = b`) makes two write - * references for the same identifier. This should skip - * the one of the two in order to prevent redundant reports. - */ - if (idNode === prevIdNode) { - continue; - } - prevIdNode = idNode; - - if (reference.isWrite()) { - context.report({ - node: getWriteNode(idNode), - messageId: "readonly", - data: { name: idNode.name }, - }); - } else if ( - shouldCheckMembers && - isMemberWrite(idNode, scope) - ) { - context.report({ - node: getWriteNode(idNode), - messageId: "readonlyMember", - data: { name: idNode.name }, - }); - } - } - } - }, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow assigning to imported bindings", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-import-assign" + }, + + schema: [], + + messages: { + readonly: "'{{name}}' is read-only.", + readonlyMember: "The members of '{{name}}' are read-only." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + + return { + ImportDeclaration(node) { + const scope = sourceCode.getScope(node); + + for (const variable of sourceCode.getDeclaredVariables(node)) { + const shouldCheckMembers = variable.defs.some( + d => d.node.type === "ImportNamespaceSpecifier" + ); + let prevIdNode = null; + + for (const reference of variable.references) { + const idNode = reference.identifier; + + /* + * AssignmentPattern (e.g. `[a = 0] = b`) makes two write + * references for the same identifier. This should skip + * the one of the two in order to prevent redundant reports. + */ + if (idNode === prevIdNode) { + continue; + } + prevIdNode = idNode; + + if (reference.isWrite()) { + context.report({ + node: getWriteNode(idNode), + messageId: "readonly", + data: { name: idNode.name } + }); + } else if (shouldCheckMembers && isMemberWrite(idNode, scope)) { + context.report({ + node: getWriteNode(idNode), + messageId: "readonlyMember", + data: { name: idNode.name } + }); + } + } + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-inline-comments.js b/node_modules/eslint/lib/rules/no-inline-comments.js index dac9eec1a..d96e6472d 100644 --- a/node_modules/eslint/lib/rules/no-inline-comments.js +++ b/node_modules/eslint/lib/rules/no-inline-comments.js @@ -10,106 +10,101 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - defaultOptions: [{}], - - docs: { - description: "Disallow inline comments after code", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/no-inline-comments", - }, - - schema: [ - { - type: "object", - properties: { - ignorePattern: { - type: "string", - }, - }, - additionalProperties: false, - }, - ], - - messages: { - unexpectedInlineComment: "Unexpected comment inline with code.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - const [{ ignorePattern }] = context.options; - const customIgnoreRegExp = - ignorePattern && new RegExp(ignorePattern, "u"); - - /** - * Will check that comments are not on lines starting with or ending with code - * @param {ASTNode} node The comment node to check - * @private - * @returns {void} - */ - function testCodeAroundComment(node) { - const startLine = String(sourceCode.lines[node.loc.start.line - 1]), - endLine = String(sourceCode.lines[node.loc.end.line - 1]), - preamble = startLine.slice(0, node.loc.start.column).trim(), - postamble = endLine.slice(node.loc.end.column).trim(), - isPreambleEmpty = !preamble, - isPostambleEmpty = !postamble; - - // Nothing on both sides - if (isPreambleEmpty && isPostambleEmpty) { - return; - } - - // Matches the ignore pattern - if (customIgnoreRegExp && customIgnoreRegExp.test(node.value)) { - return; - } - - // JSX Exception - if ( - (isPreambleEmpty || preamble === "{") && - (isPostambleEmpty || postamble === "}") - ) { - const enclosingNode = sourceCode.getNodeByRangeIndex( - node.range[0], - ); - - if ( - enclosingNode && - enclosingNode.type === "JSXEmptyExpression" - ) { - return; - } - } - - // Don't report ESLint directive comments - if (astUtils.isDirectiveComment(node)) { - return; - } - - context.report({ - node, - messageId: "unexpectedInlineComment", - }); - } - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - return { - Program() { - sourceCode - .getAllComments() - .filter(token => token.type !== "Shebang") - .forEach(testCodeAroundComment); - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow inline comments after code", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-inline-comments" + }, + + schema: [ + { + type: "object", + properties: { + ignorePattern: { + type: "string" + } + }, + additionalProperties: false + } + ], + + messages: { + unexpectedInlineComment: "Unexpected comment inline with code." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + const options = context.options[0]; + let customIgnoreRegExp; + + if (options && options.ignorePattern) { + customIgnoreRegExp = new RegExp(options.ignorePattern, "u"); + } + + /** + * Will check that comments are not on lines starting with or ending with code + * @param {ASTNode} node The comment node to check + * @private + * @returns {void} + */ + function testCodeAroundComment(node) { + + const startLine = String(sourceCode.lines[node.loc.start.line - 1]), + endLine = String(sourceCode.lines[node.loc.end.line - 1]), + preamble = startLine.slice(0, node.loc.start.column).trim(), + postamble = endLine.slice(node.loc.end.column).trim(), + isPreambleEmpty = !preamble, + isPostambleEmpty = !postamble; + + // Nothing on both sides + if (isPreambleEmpty && isPostambleEmpty) { + return; + } + + // Matches the ignore pattern + if (customIgnoreRegExp && customIgnoreRegExp.test(node.value)) { + return; + } + + // JSX Exception + if ( + (isPreambleEmpty || preamble === "{") && + (isPostambleEmpty || postamble === "}") + ) { + const enclosingNode = sourceCode.getNodeByRangeIndex(node.range[0]); + + if (enclosingNode && enclosingNode.type === "JSXEmptyExpression") { + return; + } + } + + // Don't report ESLint directive comments + if (astUtils.isDirectiveComment(node)) { + return; + } + + context.report({ + node, + messageId: "unexpectedInlineComment" + }); + } + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + return { + Program() { + sourceCode.getAllComments() + .filter(token => token.type !== "Shebang") + .forEach(testCodeAroundComment); + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-inner-declarations.js b/node_modules/eslint/lib/rules/no-inner-declarations.js index ec6c4952a..f4bae43e5 100644 --- a/node_modules/eslint/lib/rules/no-inner-declarations.js +++ b/node_modules/eslint/lib/rules/no-inner-declarations.js @@ -15,17 +15,8 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -const validParent = new Set([ - "Program", - "StaticBlock", - "ExportNamedDeclaration", - "ExportDefaultDeclaration", -]); -const validBlockStatementParent = new Set([ - "FunctionDeclaration", - "FunctionExpression", - "ArrowFunctionExpression", -]); +const validParent = new Set(["Program", "StaticBlock", "ExportNamedDeclaration", "ExportDefaultDeclaration"]); +const validBlockStatementParent = new Set(["FunctionDeclaration", "FunctionExpression", "ArrowFunctionExpression"]); /** * Finds the nearest enclosing context where this rule allows declarations and returns its description. @@ -33,115 +24,87 @@ const validBlockStatementParent = new Set([ * @returns {string} Description. One of "program", "function body", "class static block body". */ function getAllowedBodyDescription(node) { - let { parent } = node; + let { parent } = node; - while (parent) { - if (parent.type === "StaticBlock") { - return "class static block body"; - } + while (parent) { - if (astUtils.isFunction(parent)) { - return "function body"; - } + if (parent.type === "StaticBlock") { + return "class static block body"; + } - ({ parent } = parent); - } + if (astUtils.isFunction(parent)) { + return "function body"; + } - return "program"; + ({ parent } = parent); + } + + return "program"; } -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - defaultOptions: ["functions", { blockScopedFunctions: "allow" }], - - docs: { - description: - "Disallow variable or `function` declarations in nested blocks", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-inner-declarations", - }, - - schema: [ - { - enum: ["functions", "both"], - }, - { - type: "object", - properties: { - blockScopedFunctions: { - enum: ["allow", "disallow"], - }, - }, - additionalProperties: false, - }, - ], - - messages: { - moveDeclToRoot: "Move {{type}} declaration to {{body}} root.", - }, - }, - - create(context) { - const both = context.options[0] === "both"; - const { blockScopedFunctions } = context.options[1]; - - const sourceCode = context.sourceCode; - const ecmaVersion = context.languageOptions.ecmaVersion; - - /** - * Ensure that a given node is at a program or function body's root. - * @param {ASTNode} node Declaration node to check. - * @returns {void} - */ - function check(node) { - const parent = node.parent; - - if ( - parent.type === "BlockStatement" && - validBlockStatementParent.has(parent.parent.type) - ) { - return; - } - - if (validParent.has(parent.type)) { - return; - } - - context.report({ - node, - messageId: "moveDeclToRoot", - data: { - type: - node.type === "FunctionDeclaration" - ? "function" - : "variable", - body: getAllowedBodyDescription(node), - }, - }); - } - - return { - FunctionDeclaration(node) { - const isInStrictCode = sourceCode.getScope(node).upper.isStrict; - - if ( - blockScopedFunctions === "allow" && - ecmaVersion >= 2015 && - isInStrictCode - ) { - return; - } - - check(node); - }, - VariableDeclaration(node) { - if (both && node.kind === "var") { - check(node); - } - }, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow variable or `function` declarations in nested blocks", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-inner-declarations" + }, + + schema: [ + { + enum: ["functions", "both"] + } + ], + + messages: { + moveDeclToRoot: "Move {{type}} declaration to {{body}} root." + } + }, + + create(context) { + + /** + * Ensure that a given node is at a program or function body's root. + * @param {ASTNode} node Declaration node to check. + * @returns {void} + */ + function check(node) { + const parent = node.parent; + + if ( + parent.type === "BlockStatement" && validBlockStatementParent.has(parent.parent.type) + ) { + return; + } + + if (validParent.has(parent.type)) { + return; + } + + context.report({ + node, + messageId: "moveDeclToRoot", + data: { + type: (node.type === "FunctionDeclaration" ? "function" : "variable"), + body: getAllowedBodyDescription(node) + } + }); + } + + + return { + + FunctionDeclaration: check, + VariableDeclaration(node) { + if (context.options[0] === "both" && node.kind === "var") { + check(node); + } + } + + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-invalid-regexp.js b/node_modules/eslint/lib/rules/no-invalid-regexp.js index c3937a419..3c42a68e8 100644 --- a/node_modules/eslint/lib/rules/no-invalid-regexp.js +++ b/node_modules/eslint/lib/rules/no-invalid-regexp.js @@ -10,234 +10,185 @@ const RegExpValidator = require("@eslint-community/regexpp").RegExpValidator; const validator = new RegExpValidator(); -const validFlags = "dgimsuvy"; +const validFlags = /[dgimsuvy]/gu; const undefined1 = void 0; //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - defaultOptions: [{}], - - docs: { - description: - "Disallow invalid regular expression strings in `RegExp` constructors", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-invalid-regexp", - }, - - schema: [ - { - type: "object", - properties: { - allowConstructorFlags: { - type: "array", - items: { - type: "string", - }, - }, - }, - additionalProperties: false, - }, - ], - - messages: { - regexMessage: "{{message}}.", - }, - }, - - create(context) { - const [{ allowConstructorFlags }] = context.options; - let allowedFlags = []; - - if (allowConstructorFlags) { - const temp = allowConstructorFlags - .join("") - .replace(new RegExp(`[${validFlags}]`, "gu"), ""); - - if (temp) { - allowedFlags = [...new Set(temp)]; - } - } - - /** - * Reports error with the provided message. - * @param {ASTNode} node The node holding the invalid RegExp - * @param {string} message The message to report. - * @returns {void} - */ - function report(node, message) { - context.report({ - node, - messageId: "regexMessage", - data: { message }, - }); - } - - /** - * Check if node is a string - * @param {ASTNode} node node to evaluate - * @returns {boolean} True if its a string - * @private - */ - function isString(node) { - return ( - node && - node.type === "Literal" && - typeof node.value === "string" - ); - } - - /** - * Gets flags of a regular expression created by the given `RegExp()` or `new RegExp()` call - * Examples: - * new RegExp(".") // => "" - * new RegExp(".", "gu") // => "gu" - * new RegExp(".", flags) // => null - * @param {ASTNode} node `CallExpression` or `NewExpression` node - * @returns {string|null} flags if they can be determined, `null` otherwise - * @private - */ - function getFlags(node) { - if (node.arguments.length < 2) { - return ""; - } - - if (isString(node.arguments[1])) { - return node.arguments[1].value; - } - - return null; - } - - /** - * Check syntax error in a given pattern. - * @param {string} pattern The RegExp pattern to validate. - * @param {Object} flags The RegExp flags to validate. - * @param {boolean} [flags.unicode] The Unicode flag. - * @param {boolean} [flags.unicodeSets] The UnicodeSets flag. - * @returns {string|null} The syntax error. - */ - function validateRegExpPattern(pattern, flags) { - try { - validator.validatePattern( - pattern, - undefined1, - undefined1, - flags, - ); - return null; - } catch (err) { - return err.message; - } - } - - /** - * Check syntax error in a given flags. - * @param {string|null} flags The RegExp flags to validate. - * @param {string|null} flagsToCheck The RegExp invalid flags. - * @param {string} allFlags all valid and allowed flags. - * @returns {string|null} The syntax error. - */ - function validateRegExpFlags(flags, flagsToCheck, allFlags) { - const duplicateFlags = []; - - if (typeof flagsToCheck === "string") { - for (const flag of flagsToCheck) { - if (allFlags.includes(flag)) { - duplicateFlags.push(flag); - } - } - } - - /* - * `regexpp` checks the combination of `u` and `v` flags when parsing `Pattern` according to `ecma262`, - * but this rule may check only the flag when the pattern is unidentifiable, so check it here. - * https://tc39.es/ecma262/multipage/text-processing.html#sec-parsepattern - */ - if (flags && flags.includes("u") && flags.includes("v")) { - return "Regex 'u' and 'v' flags cannot be used together"; - } - - if (duplicateFlags.length > 0) { - return `Duplicate flags ('${duplicateFlags.join("")}') supplied to RegExp constructor`; - } - - if (!flagsToCheck) { - return null; - } - - return `Invalid flags supplied to RegExp constructor '${flagsToCheck}'`; - } - - return { - "CallExpression, NewExpression"(node) { - if ( - node.callee.type !== "Identifier" || - node.callee.name !== "RegExp" - ) { - return; - } - - const flags = getFlags(node); - let flagsToCheck = flags; - const allFlags = - allowedFlags.length > 0 - ? validFlags.split("").concat(allowedFlags) - : validFlags.split(""); - - if (flags) { - allFlags.forEach(flag => { - flagsToCheck = flagsToCheck.replace(flag, ""); - }); - } - - let message = validateRegExpFlags( - flags, - flagsToCheck, - allFlags, - ); - - if (message) { - report(node, message); - return; - } - - if (!isString(node.arguments[0])) { - return; - } - - const pattern = node.arguments[0].value; - - message = - // If flags are unknown, report the regex only if its pattern is invalid both with and without the "u" flag - flags === null - ? validateRegExpPattern(pattern, { - unicode: true, - unicodeSets: false, - }) && - validateRegExpPattern(pattern, { - unicode: false, - unicodeSets: true, - }) && - validateRegExpPattern(pattern, { - unicode: false, - unicodeSets: false, - }) - : validateRegExpPattern(pattern, { - unicode: flags.includes("u"), - unicodeSets: flags.includes("v"), - }); - - if (message) { - report(node, message); - } - }, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow invalid regular expression strings in `RegExp` constructors", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-invalid-regexp" + }, + + schema: [{ + type: "object", + properties: { + allowConstructorFlags: { + type: "array", + items: { + type: "string" + } + } + }, + additionalProperties: false + }], + + messages: { + regexMessage: "{{message}}." + } + }, + + create(context) { + + const options = context.options[0]; + let allowedFlags = null; + + if (options && options.allowConstructorFlags) { + const temp = options.allowConstructorFlags.join("").replace(validFlags, ""); + + if (temp) { + allowedFlags = new RegExp(`[${temp}]`, "giu"); + } + } + + /** + * Reports error with the provided message. + * @param {ASTNode} node The node holding the invalid RegExp + * @param {string} message The message to report. + * @returns {void} + */ + function report(node, message) { + context.report({ + node, + messageId: "regexMessage", + data: { message } + }); + } + + /** + * Check if node is a string + * @param {ASTNode} node node to evaluate + * @returns {boolean} True if its a string + * @private + */ + function isString(node) { + return node && node.type === "Literal" && typeof node.value === "string"; + } + + /** + * Gets flags of a regular expression created by the given `RegExp()` or `new RegExp()` call + * Examples: + * new RegExp(".") // => "" + * new RegExp(".", "gu") // => "gu" + * new RegExp(".", flags) // => null + * @param {ASTNode} node `CallExpression` or `NewExpression` node + * @returns {string|null} flags if they can be determined, `null` otherwise + * @private + */ + function getFlags(node) { + if (node.arguments.length < 2) { + return ""; + } + + if (isString(node.arguments[1])) { + return node.arguments[1].value; + } + + return null; + } + + /** + * Check syntax error in a given pattern. + * @param {string} pattern The RegExp pattern to validate. + * @param {Object} flags The RegExp flags to validate. + * @param {boolean} [flags.unicode] The Unicode flag. + * @param {boolean} [flags.unicodeSets] The UnicodeSets flag. + * @returns {string|null} The syntax error. + */ + function validateRegExpPattern(pattern, flags) { + try { + validator.validatePattern(pattern, undefined1, undefined1, flags); + return null; + } catch (err) { + return err.message; + } + } + + /** + * Check syntax error in a given flags. + * @param {string|null} flags The RegExp flags to validate. + * @returns {string|null} The syntax error. + */ + function validateRegExpFlags(flags) { + if (!flags) { + return null; + } + try { + validator.validateFlags(flags); + } catch { + return `Invalid flags supplied to RegExp constructor '${flags}'`; + } + + /* + * `regexpp` checks the combination of `u` and `v` flags when parsing `Pattern` according to `ecma262`, + * but this rule may check only the flag when the pattern is unidentifiable, so check it here. + * https://tc39.es/ecma262/multipage/text-processing.html#sec-parsepattern + */ + if (flags.includes("u") && flags.includes("v")) { + return "Regex 'u' and 'v' flags cannot be used together"; + } + return null; + } + + return { + "CallExpression, NewExpression"(node) { + if (node.callee.type !== "Identifier" || node.callee.name !== "RegExp") { + return; + } + + let flags = getFlags(node); + + if (flags && allowedFlags) { + flags = flags.replace(allowedFlags, ""); + } + + let message = validateRegExpFlags(flags); + + if (message) { + report(node, message); + return; + } + + if (!isString(node.arguments[0])) { + return; + } + + const pattern = node.arguments[0].value; + + message = ( + + // If flags are unknown, report the regex only if its pattern is invalid both with and without the "u" flag + flags === null + ? ( + validateRegExpPattern(pattern, { unicode: true, unicodeSets: false }) && + validateRegExpPattern(pattern, { unicode: false, unicodeSets: true }) && + validateRegExpPattern(pattern, { unicode: false, unicodeSets: false }) + ) + : validateRegExpPattern(pattern, { unicode: flags.includes("u"), unicodeSets: flags.includes("v") }) + ); + + if (message) { + report(node, message); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-invalid-this.js b/node_modules/eslint/lib/rules/no-invalid-this.js index 9e2f97ce9..9e214035c 100644 --- a/node_modules/eslint/lib/rules/no-invalid-this.js +++ b/node_modules/eslint/lib/rules/no-invalid-this.js @@ -23,156 +23,128 @@ const astUtils = require("./utils/ast-utils"); * @returns {boolean} `true` if it is a code path with lexical `this` binding. */ function isCodePathWithLexicalThis(codePath, node) { - return ( - codePath.origin === "function" && - node.type === "ArrowFunctionExpression" - ); + return codePath.origin === "function" && node.type === "ArrowFunctionExpression"; } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - dialects: ["javascript", "typescript"], - language: "javascript", - type: "suggestion", - - defaultOptions: [{ capIsConstructor: true }], - - docs: { - description: - "Disallow use of `this` in contexts where the value of `this` is `undefined`", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-invalid-this", - }, - - schema: [ - { - type: "object", - properties: { - capIsConstructor: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - - messages: { - unexpectedThis: "Unexpected 'this'.", - }, - }, - - create(context) { - const [{ capIsConstructor }] = context.options; - const stack = [], - sourceCode = context.sourceCode; - - /** - * Gets the current checking context. - * - * The return value has a flag that whether or not `this` keyword is valid. - * The flag is initialized when got at the first time. - * @returns {{valid: boolean}} - * an object which has a flag that whether or not `this` keyword is valid. - */ - stack.getCurrent = function () { - const current = this.at(-1); - - if (!current.init) { - current.init = true; - current.valid = !astUtils.isDefaultThisBinding( - current.node, - sourceCode, - { capIsConstructor }, - ); - } - return current; - }; - - return { - onCodePathStart(codePath, node) { - if (isCodePathWithLexicalThis(codePath, node)) { - return; - } - - if (codePath.origin === "program") { - const scope = sourceCode.getScope(node); - const features = - context.languageOptions.parserOptions.ecmaFeatures || - {}; - - // `this` at the top level of scripts always refers to the global object - stack.push({ - init: true, - node, - valid: !( - node.sourceType === "module" || - (features.globalReturn && - scope.childScopes[0].isStrict) - ), - }); - - return; - } - - /* - * `init: false` means that `valid` isn't determined yet. - * Most functions don't use `this`, and the calculation for `valid` - * is relatively costly, so we'll calculate it lazily when the first - * `this` within the function is traversed. A special case are non-strict - * functions, because `this` refers to the global object and therefore is - * always valid, so we can set `init: true` right away. - */ - stack.push({ - init: !sourceCode.getScope(node).isStrict, - node, - valid: true, - }); - }, - - onCodePathEnd(codePath, node) { - if (isCodePathWithLexicalThis(codePath, node)) { - return; - } - - stack.pop(); - }, - - "AccessorProperty > *.value"(node) { - stack.push({ - init: true, - node, - valid: true, - }); - }, - - "AccessorProperty:exit"() { - stack.pop(); - }, - - // Reports if `this` of the current context is invalid. - ThisExpression(node) { - // Special case: skip `this` if it's the value of an AccessorProperty - if ( - node.parent.type === "AccessorProperty" && - node.parent.value === node - ) { - return; - } - - const current = stack.getCurrent(); - - if (current && !current.valid) { - context.report({ - node, - messageId: "unexpectedThis", - }); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow use of `this` in contexts where the value of `this` is `undefined`", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-invalid-this" + }, + + schema: [ + { + type: "object", + properties: { + capIsConstructor: { + type: "boolean", + default: true + } + }, + additionalProperties: false + } + ], + + messages: { + unexpectedThis: "Unexpected 'this'." + } + }, + + create(context) { + const options = context.options[0] || {}; + const capIsConstructor = options.capIsConstructor !== false; + const stack = [], + sourceCode = context.sourceCode; + + /** + * Gets the current checking context. + * + * The return value has a flag that whether or not `this` keyword is valid. + * The flag is initialized when got at the first time. + * @returns {{valid: boolean}} + * an object which has a flag that whether or not `this` keyword is valid. + */ + stack.getCurrent = function() { + const current = this[this.length - 1]; + + if (!current.init) { + current.init = true; + current.valid = !astUtils.isDefaultThisBinding( + current.node, + sourceCode, + { capIsConstructor } + ); + } + return current; + }; + + return { + + onCodePathStart(codePath, node) { + if (isCodePathWithLexicalThis(codePath, node)) { + return; + } + + if (codePath.origin === "program") { + const scope = sourceCode.getScope(node); + const features = context.languageOptions.parserOptions.ecmaFeatures || {}; + + // `this` at the top level of scripts always refers to the global object + stack.push({ + init: true, + node, + valid: !( + node.sourceType === "module" || + (features.globalReturn && scope.childScopes[0].isStrict) + ) + }); + + return; + } + + /* + * `init: false` means that `valid` isn't determined yet. + * Most functions don't use `this`, and the calculation for `valid` + * is relatively costly, so we'll calculate it lazily when the first + * `this` within the function is traversed. A special case are non-strict + * functions, because `this` refers to the global object and therefore is + * always valid, so we can set `init: true` right away. + */ + stack.push({ + init: !sourceCode.getScope(node).isStrict, + node, + valid: true + }); + }, + + onCodePathEnd(codePath, node) { + if (isCodePathWithLexicalThis(codePath, node)) { + return; + } + + stack.pop(); + }, + + // Reports if `this` of the current context is invalid. + ThisExpression(node) { + const current = stack.getCurrent(); + + if (current && !current.valid) { + context.report({ + node, + messageId: "unexpectedThis" + }); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-irregular-whitespace.js b/node_modules/eslint/lib/rules/no-irregular-whitespace.js index 4aaaad1d2..ab7ccac54 100644 --- a/node_modules/eslint/lib/rules/no-irregular-whitespace.js +++ b/node_modules/eslint/lib/rules/no-irregular-whitespace.js @@ -16,277 +16,261 @@ const astUtils = require("./utils/ast-utils"); // Constants //------------------------------------------------------------------------------ -const ALL_IRREGULARS = - /[\f\v\u0085\ufeff\u00a0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u202f\u205f\u3000\u2028\u2029]/u; -const IRREGULAR_WHITESPACE = - /[\f\v\u0085\ufeff\u00a0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u202f\u205f\u3000]+/gu; -const IRREGULAR_LINE_TERMINATORS = /[\u2028\u2029]/gu; +const ALL_IRREGULARS = /[\f\v\u0085\ufeff\u00a0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u202f\u205f\u3000\u2028\u2029]/u; +const IRREGULAR_WHITESPACE = /[\f\v\u0085\ufeff\u00a0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u202f\u205f\u3000]+/mgu; +const IRREGULAR_LINE_TERMINATORS = /[\u2028\u2029]/mgu; const LINE_BREAK = astUtils.createGlobalLinebreakMatcher(); //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - defaultOptions: [ - { - skipComments: false, - skipJSXText: false, - skipRegExps: false, - skipStrings: true, - skipTemplates: false, - }, - ], - - docs: { - description: "Disallow irregular whitespace", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-irregular-whitespace", - }, - - schema: [ - { - type: "object", - properties: { - skipComments: { - type: "boolean", - }, - skipStrings: { - type: "boolean", - }, - skipTemplates: { - type: "boolean", - }, - skipRegExps: { - type: "boolean", - }, - skipJSXText: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - - messages: { - noIrregularWhitespace: "Irregular whitespace not allowed.", - }, - }, - - create(context) { - const [ - { - skipComments, - skipStrings, - skipRegExps, - skipTemplates, - skipJSXText, - }, - ] = context.options; - - const sourceCode = context.sourceCode; - const commentNodes = sourceCode.getAllComments(); - - // Module store of errors that we have found - let errors = []; - - /** - * Removes errors that occur inside the given node - * @param {ASTNode} node to check for matching errors. - * @returns {void} - * @private - */ - function removeWhitespaceError(node) { - const locStart = node.loc.start; - const locEnd = node.loc.end; - - errors = errors.filter( - ({ loc: { start: errorLocStart } }) => - errorLocStart.line < locStart.line || - (errorLocStart.line === locStart.line && - errorLocStart.column < locStart.column) || - (errorLocStart.line === locEnd.line && - errorLocStart.column >= locEnd.column) || - errorLocStart.line > locEnd.line, - ); - } - - /** - * Checks literal nodes for errors that we are choosing to ignore and calls the relevant methods to remove the errors - * @param {ASTNode} node to check for matching errors. - * @returns {void} - * @private - */ - function removeInvalidNodeErrorsInLiteral(node) { - const shouldCheckStrings = - skipStrings && typeof node.value === "string"; - const shouldCheckRegExps = skipRegExps && Boolean(node.regex); - - if (shouldCheckStrings || shouldCheckRegExps) { - // If we have irregular characters remove them from the errors list - if (ALL_IRREGULARS.test(node.raw)) { - removeWhitespaceError(node); - } - } - } - - /** - * Checks template string literal nodes for errors that we are choosing to ignore and calls the relevant methods to remove the errors - * @param {ASTNode} node to check for matching errors. - * @returns {void} - * @private - */ - function removeInvalidNodeErrorsInTemplateLiteral(node) { - if (typeof node.value.raw === "string") { - if (ALL_IRREGULARS.test(node.value.raw)) { - removeWhitespaceError(node); - } - } - } - - /** - * Checks comment nodes for errors that we are choosing to ignore and calls the relevant methods to remove the errors - * @param {ASTNode} node to check for matching errors. - * @returns {void} - * @private - */ - function removeInvalidNodeErrorsInComment(node) { - if (ALL_IRREGULARS.test(node.value)) { - removeWhitespaceError(node); - } - } - - /** - * Checks JSX nodes for errors that we are choosing to ignore and calls the relevant methods to remove the errors - * @param {ASTNode} node to check for matching errors. - * @returns {void} - * @private - */ - function removeInvalidNodeErrorsInJSXText(node) { - if (ALL_IRREGULARS.test(node.raw)) { - removeWhitespaceError(node); - } - } - - /** - * Checks the program source for irregular whitespace - * @param {ASTNode} node The program node - * @returns {void} - * @private - */ - function checkForIrregularWhitespace(node) { - const sourceLines = sourceCode.lines; - - sourceLines.forEach((sourceLine, lineIndex) => { - const lineNumber = lineIndex + 1; - let match; - - while ( - (match = IRREGULAR_WHITESPACE.exec(sourceLine)) !== null - ) { - errors.push({ - node, - messageId: "noIrregularWhitespace", - loc: { - start: { - line: lineNumber, - column: match.index, - }, - end: { - line: lineNumber, - column: match.index + match[0].length, - }, - }, - }); - } - }); - } - - /** - * Checks the program source for irregular line terminators - * @param {ASTNode} node The program node - * @returns {void} - * @private - */ - function checkForIrregularLineTerminators(node) { - const source = sourceCode.getText(), - sourceLines = sourceCode.lines, - linebreaks = source.match(LINE_BREAK); - let lastLineIndex = -1, - match; - - while ((match = IRREGULAR_LINE_TERMINATORS.exec(source)) !== null) { - const lineIndex = - linebreaks.indexOf(match[0], lastLineIndex + 1) || 0; - - errors.push({ - node, - messageId: "noIrregularWhitespace", - loc: { - start: { - line: lineIndex + 1, - column: sourceLines[lineIndex].length, - }, - end: { - line: lineIndex + 2, - column: 0, - }, - }, - }); - - lastLineIndex = lineIndex; - } - } - - /** - * A no-op function to act as placeholder for comment accumulation when the `skipComments` option is `false`. - * @returns {void} - * @private - */ - function noop() {} - - const nodes = {}; - - if (ALL_IRREGULARS.test(sourceCode.getText())) { - nodes.Program = function (node) { - /* - * As we can easily fire warnings for all white space issues with - * all the source its simpler to fire them here. - * This means we can check all the application code without having - * to worry about issues caused in the parser tokens. - * When writing this code also evaluating per node was missing out - * connecting tokens in some cases. - * We can later filter the errors when they are found to be not an - * issue in nodes we don't care about. - */ - checkForIrregularWhitespace(node); - checkForIrregularLineTerminators(node); - }; - - nodes.Literal = removeInvalidNodeErrorsInLiteral; - nodes.TemplateElement = skipTemplates - ? removeInvalidNodeErrorsInTemplateLiteral - : noop; - nodes.JSXText = skipJSXText - ? removeInvalidNodeErrorsInJSXText - : noop; - nodes["Program:exit"] = function () { - if (skipComments) { - // First strip errors occurring in comment nodes. - commentNodes.forEach(removeInvalidNodeErrorsInComment); - } - - // If we have any errors remaining report on them - errors.forEach(error => context.report(error)); - }; - } else { - nodes.Program = noop; - } - - return nodes; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow irregular whitespace", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-irregular-whitespace" + }, + + schema: [ + { + type: "object", + properties: { + skipComments: { + type: "boolean", + default: false + }, + skipStrings: { + type: "boolean", + default: true + }, + skipTemplates: { + type: "boolean", + default: false + }, + skipRegExps: { + type: "boolean", + default: false + }, + skipJSXText: { + type: "boolean", + default: false + } + }, + additionalProperties: false + } + ], + + messages: { + noIrregularWhitespace: "Irregular whitespace not allowed." + } + }, + + create(context) { + + // Module store of errors that we have found + let errors = []; + + // Lookup the `skipComments` option, which defaults to `false`. + const options = context.options[0] || {}; + const skipComments = !!options.skipComments; + const skipStrings = options.skipStrings !== false; + const skipRegExps = !!options.skipRegExps; + const skipTemplates = !!options.skipTemplates; + const skipJSXText = !!options.skipJSXText; + + const sourceCode = context.sourceCode; + const commentNodes = sourceCode.getAllComments(); + + /** + * Removes errors that occur inside the given node + * @param {ASTNode} node to check for matching errors. + * @returns {void} + * @private + */ + function removeWhitespaceError(node) { + const locStart = node.loc.start; + const locEnd = node.loc.end; + + errors = errors.filter(({ loc: { start: errorLocStart } }) => ( + errorLocStart.line < locStart.line || + errorLocStart.line === locStart.line && errorLocStart.column < locStart.column || + errorLocStart.line === locEnd.line && errorLocStart.column >= locEnd.column || + errorLocStart.line > locEnd.line + )); + } + + /** + * Checks literal nodes for errors that we are choosing to ignore and calls the relevant methods to remove the errors + * @param {ASTNode} node to check for matching errors. + * @returns {void} + * @private + */ + function removeInvalidNodeErrorsInLiteral(node) { + const shouldCheckStrings = skipStrings && (typeof node.value === "string"); + const shouldCheckRegExps = skipRegExps && Boolean(node.regex); + + if (shouldCheckStrings || shouldCheckRegExps) { + + // If we have irregular characters remove them from the errors list + if (ALL_IRREGULARS.test(node.raw)) { + removeWhitespaceError(node); + } + } + } + + /** + * Checks template string literal nodes for errors that we are choosing to ignore and calls the relevant methods to remove the errors + * @param {ASTNode} node to check for matching errors. + * @returns {void} + * @private + */ + function removeInvalidNodeErrorsInTemplateLiteral(node) { + if (typeof node.value.raw === "string") { + if (ALL_IRREGULARS.test(node.value.raw)) { + removeWhitespaceError(node); + } + } + } + + /** + * Checks comment nodes for errors that we are choosing to ignore and calls the relevant methods to remove the errors + * @param {ASTNode} node to check for matching errors. + * @returns {void} + * @private + */ + function removeInvalidNodeErrorsInComment(node) { + if (ALL_IRREGULARS.test(node.value)) { + removeWhitespaceError(node); + } + } + + /** + * Checks JSX nodes for errors that we are choosing to ignore and calls the relevant methods to remove the errors + * @param {ASTNode} node to check for matching errors. + * @returns {void} + * @private + */ + function removeInvalidNodeErrorsInJSXText(node) { + if (ALL_IRREGULARS.test(node.raw)) { + removeWhitespaceError(node); + } + } + + /** + * Checks the program source for irregular whitespace + * @param {ASTNode} node The program node + * @returns {void} + * @private + */ + function checkForIrregularWhitespace(node) { + const sourceLines = sourceCode.lines; + + sourceLines.forEach((sourceLine, lineIndex) => { + const lineNumber = lineIndex + 1; + let match; + + while ((match = IRREGULAR_WHITESPACE.exec(sourceLine)) !== null) { + errors.push({ + node, + messageId: "noIrregularWhitespace", + loc: { + start: { + line: lineNumber, + column: match.index + }, + end: { + line: lineNumber, + column: match.index + match[0].length + } + } + }); + } + }); + } + + /** + * Checks the program source for irregular line terminators + * @param {ASTNode} node The program node + * @returns {void} + * @private + */ + function checkForIrregularLineTerminators(node) { + const source = sourceCode.getText(), + sourceLines = sourceCode.lines, + linebreaks = source.match(LINE_BREAK); + let lastLineIndex = -1, + match; + + while ((match = IRREGULAR_LINE_TERMINATORS.exec(source)) !== null) { + const lineIndex = linebreaks.indexOf(match[0], lastLineIndex + 1) || 0; + + errors.push({ + node, + messageId: "noIrregularWhitespace", + loc: { + start: { + line: lineIndex + 1, + column: sourceLines[lineIndex].length + }, + end: { + line: lineIndex + 2, + column: 0 + } + } + }); + + lastLineIndex = lineIndex; + } + } + + /** + * A no-op function to act as placeholder for comment accumulation when the `skipComments` option is `false`. + * @returns {void} + * @private + */ + function noop() {} + + const nodes = {}; + + if (ALL_IRREGULARS.test(sourceCode.getText())) { + nodes.Program = function(node) { + + /* + * As we can easily fire warnings for all white space issues with + * all the source its simpler to fire them here. + * This means we can check all the application code without having + * to worry about issues caused in the parser tokens. + * When writing this code also evaluating per node was missing out + * connecting tokens in some cases. + * We can later filter the errors when they are found to be not an + * issue in nodes we don't care about. + */ + checkForIrregularWhitespace(node); + checkForIrregularLineTerminators(node); + }; + + nodes.Literal = removeInvalidNodeErrorsInLiteral; + nodes.TemplateElement = skipTemplates ? removeInvalidNodeErrorsInTemplateLiteral : noop; + nodes.JSXText = skipJSXText ? removeInvalidNodeErrorsInJSXText : noop; + nodes["Program:exit"] = function() { + if (skipComments) { + + // First strip errors occurring in comment nodes. + commentNodes.forEach(removeInvalidNodeErrorsInComment); + } + + // If we have any errors remaining report on them + errors.forEach(error => context.report(error)); + }; + } else { + nodes.Program = noop; + } + + return nodes; + } }; diff --git a/node_modules/eslint/lib/rules/no-iterator.js b/node_modules/eslint/lib/rules/no-iterator.js index 6451e334d..dcd9683b4 100644 --- a/node_modules/eslint/lib/rules/no-iterator.js +++ b/node_modules/eslint/lib/rules/no-iterator.js @@ -15,34 +15,38 @@ const { getStaticPropertyName } = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: "Disallow the use of the `__iterator__` property", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-iterator", - }, - - schema: [], - - messages: { - noIterator: "Reserved name '__iterator__'.", - }, - }, - - create(context) { - return { - MemberExpression(node) { - if (getStaticPropertyName(node) === "__iterator__") { - context.report({ - node, - messageId: "noIterator", - }); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow the use of the `__iterator__` property", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-iterator" + }, + + schema: [], + + messages: { + noIterator: "Reserved name '__iterator__'." + } + }, + + create(context) { + + return { + + MemberExpression(node) { + + if (getStaticPropertyName(node) === "__iterator__") { + context.report({ + node, + messageId: "noIterator" + }); + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-label-var.js b/node_modules/eslint/lib/rules/no-label-var.js index a87894130..bf33cd157 100644 --- a/node_modules/eslint/lib/rules/no-label-var.js +++ b/node_modules/eslint/lib/rules/no-label-var.js @@ -15,64 +15,66 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: "Disallow labels that share a name with a variable", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/no-label-var", - }, - - schema: [], - - messages: { - identifierClashWithLabel: - "Found identifier with same name as label.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - /** - * Check if the identifier is present inside current scope - * @param {Object} scope current scope - * @param {string} name To evaluate - * @returns {boolean} True if its present - * @private - */ - function findIdentifier(scope, name) { - return astUtils.getVariableByName(scope, name) !== null; - } - - //-------------------------------------------------------------------------- - // Public API - //-------------------------------------------------------------------------- - - return { - LabeledStatement(node) { - // Fetch the innermost scope. - const scope = sourceCode.getScope(node); - - /* - * Recursively find the identifier walking up the scope, starting - * with the innermost scope. - */ - if (findIdentifier(scope, node.label.name)) { - context.report({ - node, - messageId: "identifierClashWithLabel", - }); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow labels that share a name with a variable", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-label-var" + }, + + schema: [], + + messages: { + identifierClashWithLabel: "Found identifier with same name as label." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + /** + * Check if the identifier is present inside current scope + * @param {Object} scope current scope + * @param {string} name To evaluate + * @returns {boolean} True if its present + * @private + */ + function findIdentifier(scope, name) { + return astUtils.getVariableByName(scope, name) !== null; + } + + //-------------------------------------------------------------------------- + // Public API + //-------------------------------------------------------------------------- + + return { + + LabeledStatement(node) { + + // Fetch the innermost scope. + const scope = sourceCode.getScope(node); + + /* + * Recursively find the identifier walking up the scope, starting + * with the innermost scope. + */ + if (findIdentifier(scope, node.label.name)) { + context.report({ + node, + messageId: "identifierClashWithLabel" + }); + } + } + + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-labels.js b/node_modules/eslint/lib/rules/no-labels.js index 45eb8af74..d991a0a80 100644 --- a/node_modules/eslint/lib/rules/no-labels.js +++ b/node_modules/eslint/lib/rules/no-labels.js @@ -14,143 +14,136 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - defaultOptions: [ - { - allowLoop: false, - allowSwitch: false, - }, - ], - - docs: { - description: "Disallow labeled statements", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/no-labels", - }, - - schema: [ - { - type: "object", - properties: { - allowLoop: { - type: "boolean", - }, - allowSwitch: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - - messages: { - unexpectedLabel: "Unexpected labeled statement.", - unexpectedLabelInBreak: "Unexpected label in break statement.", - unexpectedLabelInContinue: - "Unexpected label in continue statement.", - }, - }, - - create(context) { - const [{ allowLoop, allowSwitch }] = context.options; - let scopeInfo = null; - - /** - * Gets the kind of a given node. - * @param {ASTNode} node A node to get. - * @returns {string} The kind of the node. - */ - function getBodyKind(node) { - if (astUtils.isLoop(node)) { - return "loop"; - } - if (node.type === "SwitchStatement") { - return "switch"; - } - return "other"; - } - - /** - * Checks whether the label of a given kind is allowed or not. - * @param {string} kind A kind to check. - * @returns {boolean} `true` if the kind is allowed. - */ - function isAllowed(kind) { - switch (kind) { - case "loop": - return allowLoop; - case "switch": - return allowSwitch; - default: - return false; - } - } - - /** - * Checks whether a given name is a label of a loop or not. - * @param {string} label A name of a label to check. - * @returns {boolean} `true` if the name is a label of a loop. - */ - function getKind(label) { - let info = scopeInfo; - - while (info) { - if (info.label === label) { - return info.kind; - } - info = info.upper; - } - - /* c8 ignore next */ - return "other"; - } - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - return { - LabeledStatement(node) { - scopeInfo = { - label: node.label.name, - kind: getBodyKind(node.body), - upper: scopeInfo, - }; - }, - - "LabeledStatement:exit"(node) { - if (!isAllowed(scopeInfo.kind)) { - context.report({ - node, - messageId: "unexpectedLabel", - }); - } - - scopeInfo = scopeInfo.upper; - }, - - BreakStatement(node) { - if (node.label && !isAllowed(getKind(node.label.name))) { - context.report({ - node, - messageId: "unexpectedLabelInBreak", - }); - } - }, - - ContinueStatement(node) { - if (node.label && !isAllowed(getKind(node.label.name))) { - context.report({ - node, - messageId: "unexpectedLabelInContinue", - }); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow labeled statements", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-labels" + }, + + schema: [ + { + type: "object", + properties: { + allowLoop: { + type: "boolean", + default: false + }, + allowSwitch: { + type: "boolean", + default: false + } + }, + additionalProperties: false + } + ], + + messages: { + unexpectedLabel: "Unexpected labeled statement.", + unexpectedLabelInBreak: "Unexpected label in break statement.", + unexpectedLabelInContinue: "Unexpected label in continue statement." + } + }, + + create(context) { + const options = context.options[0]; + const allowLoop = options && options.allowLoop; + const allowSwitch = options && options.allowSwitch; + let scopeInfo = null; + + /** + * Gets the kind of a given node. + * @param {ASTNode} node A node to get. + * @returns {string} The kind of the node. + */ + function getBodyKind(node) { + if (astUtils.isLoop(node)) { + return "loop"; + } + if (node.type === "SwitchStatement") { + return "switch"; + } + return "other"; + } + + /** + * Checks whether the label of a given kind is allowed or not. + * @param {string} kind A kind to check. + * @returns {boolean} `true` if the kind is allowed. + */ + function isAllowed(kind) { + switch (kind) { + case "loop": return allowLoop; + case "switch": return allowSwitch; + default: return false; + } + } + + /** + * Checks whether a given name is a label of a loop or not. + * @param {string} label A name of a label to check. + * @returns {boolean} `true` if the name is a label of a loop. + */ + function getKind(label) { + let info = scopeInfo; + + while (info) { + if (info.label === label) { + return info.kind; + } + info = info.upper; + } + + /* c8 ignore next */ + return "other"; + } + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + return { + LabeledStatement(node) { + scopeInfo = { + label: node.label.name, + kind: getBodyKind(node.body), + upper: scopeInfo + }; + }, + + "LabeledStatement:exit"(node) { + if (!isAllowed(scopeInfo.kind)) { + context.report({ + node, + messageId: "unexpectedLabel" + }); + } + + scopeInfo = scopeInfo.upper; + }, + + BreakStatement(node) { + if (node.label && !isAllowed(getKind(node.label.name))) { + context.report({ + node, + messageId: "unexpectedLabelInBreak" + }); + } + }, + + ContinueStatement(node) { + if (node.label && !isAllowed(getKind(node.label.name))) { + context.report({ + node, + messageId: "unexpectedLabelInContinue" + }); + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-lone-blocks.js b/node_modules/eslint/lib/rules/no-lone-blocks.js index 94421b8f8..767eec2be 100644 --- a/node_modules/eslint/lib/rules/no-lone-blocks.js +++ b/node_modules/eslint/lib/rules/no-lone-blocks.js @@ -9,132 +9,128 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: "Disallow unnecessary nested blocks", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-lone-blocks", - }, - - schema: [], - - messages: { - redundantBlock: "Block is redundant.", - redundantNestedBlock: "Nested block is redundant.", - }, - }, - - create(context) { - // A stack of lone blocks to be checked for block-level bindings - const loneBlocks = []; - let ruleDef; - const sourceCode = context.sourceCode; - - /** - * Reports a node as invalid. - * @param {ASTNode} node The node to be reported. - * @returns {void} - */ - function report(node) { - const messageId = - node.parent.type === "BlockStatement" || - node.parent.type === "StaticBlock" - ? "redundantNestedBlock" - : "redundantBlock"; - - context.report({ - node, - messageId, - }); - } - - /** - * Checks for any occurrence of a BlockStatement in a place where lists of statements can appear - * @param {ASTNode} node The node to check - * @returns {boolean} True if the node is a lone block. - */ - function isLoneBlock(node) { - return ( - node.parent.type === "BlockStatement" || - node.parent.type === "StaticBlock" || - node.parent.type === "Program" || - // Don't report blocks in switch cases if the block is the only statement of the case. - (node.parent.type === "SwitchCase" && - !( - node.parent.consequent[0] === node && - node.parent.consequent.length === 1 - )) - ); - } - - /** - * Checks the enclosing block of the current node for block-level bindings, - * and "marks it" as valid if any. - * @param {ASTNode} node The current node to check. - * @returns {void} - */ - function markLoneBlock(node) { - if (loneBlocks.length === 0) { - return; - } - - const block = node.parent; - - if (loneBlocks.at(-1) === block) { - loneBlocks.pop(); - } - } - - // Default rule definition: report all lone blocks - ruleDef = { - BlockStatement(node) { - if (isLoneBlock(node)) { - report(node); - } - }, - }; - - // ES6: report blocks without block-level bindings, or that's only child of another block - if (context.languageOptions.ecmaVersion >= 2015) { - ruleDef = { - BlockStatement(node) { - if (isLoneBlock(node)) { - loneBlocks.push(node); - } - }, - "BlockStatement:exit"(node) { - if (loneBlocks.length > 0 && loneBlocks.at(-1) === node) { - loneBlocks.pop(); - report(node); - } else if ( - (node.parent.type === "BlockStatement" || - node.parent.type === "StaticBlock") && - node.parent.body.length === 1 - ) { - report(node); - } - }, - }; - - ruleDef.VariableDeclaration = function (node) { - if (node.kind !== "var") { - markLoneBlock(node); - } - }; - - ruleDef.FunctionDeclaration = function (node) { - if (sourceCode.getScope(node).isStrict) { - markLoneBlock(node); - } - }; - - ruleDef.ClassDeclaration = markLoneBlock; - } - - return ruleDef; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow unnecessary nested blocks", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-lone-blocks" + }, + + schema: [], + + messages: { + redundantBlock: "Block is redundant.", + redundantNestedBlock: "Nested block is redundant." + } + }, + + create(context) { + + // A stack of lone blocks to be checked for block-level bindings + const loneBlocks = []; + let ruleDef; + const sourceCode = context.sourceCode; + + /** + * Reports a node as invalid. + * @param {ASTNode} node The node to be reported. + * @returns {void} + */ + function report(node) { + const messageId = node.parent.type === "BlockStatement" || node.parent.type === "StaticBlock" + ? "redundantNestedBlock" + : "redundantBlock"; + + context.report({ + node, + messageId + }); + } + + /** + * Checks for any occurrence of a BlockStatement in a place where lists of statements can appear + * @param {ASTNode} node The node to check + * @returns {boolean} True if the node is a lone block. + */ + function isLoneBlock(node) { + return node.parent.type === "BlockStatement" || + node.parent.type === "StaticBlock" || + node.parent.type === "Program" || + + // Don't report blocks in switch cases if the block is the only statement of the case. + node.parent.type === "SwitchCase" && !(node.parent.consequent[0] === node && node.parent.consequent.length === 1); + } + + /** + * Checks the enclosing block of the current node for block-level bindings, + * and "marks it" as valid if any. + * @param {ASTNode} node The current node to check. + * @returns {void} + */ + function markLoneBlock(node) { + if (loneBlocks.length === 0) { + return; + } + + const block = node.parent; + + if (loneBlocks[loneBlocks.length - 1] === block) { + loneBlocks.pop(); + } + } + + // Default rule definition: report all lone blocks + ruleDef = { + BlockStatement(node) { + if (isLoneBlock(node)) { + report(node); + } + } + }; + + // ES6: report blocks without block-level bindings, or that's only child of another block + if (context.languageOptions.ecmaVersion >= 2015) { + ruleDef = { + BlockStatement(node) { + if (isLoneBlock(node)) { + loneBlocks.push(node); + } + }, + "BlockStatement:exit"(node) { + if (loneBlocks.length > 0 && loneBlocks[loneBlocks.length - 1] === node) { + loneBlocks.pop(); + report(node); + } else if ( + ( + node.parent.type === "BlockStatement" || + node.parent.type === "StaticBlock" + ) && + node.parent.body.length === 1 + ) { + report(node); + } + } + }; + + ruleDef.VariableDeclaration = function(node) { + if (node.kind === "let" || node.kind === "const") { + markLoneBlock(node); + } + }; + + ruleDef.FunctionDeclaration = function(node) { + if (sourceCode.getScope(node).isStrict) { + markLoneBlock(node); + } + }; + + ruleDef.ClassDeclaration = markLoneBlock; + } + + return ruleDef; + } }; diff --git a/node_modules/eslint/lib/rules/no-lonely-if.js b/node_modules/eslint/lib/rules/no-lonely-if.js index 65af96071..eefd2c688 100644 --- a/node_modules/eslint/lib/rules/no-lonely-if.js +++ b/node_modules/eslint/lib/rules/no-lonely-if.js @@ -4,123 +4,85 @@ */ "use strict"; -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ - -const astUtils = require("./utils/ast-utils"); - //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", + meta: { + type: "suggestion", + + docs: { + description: "Disallow `if` statements as the only statement in `else` blocks", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-lonely-if" + }, + + schema: [], + fixable: "code", + + messages: { + unexpectedLonelyIf: "Unexpected if as the only statement in an else block." + } + }, - docs: { - description: - "Disallow `if` statements as the only statement in `else` blocks", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/no-lonely-if", - }, + create(context) { + const sourceCode = context.sourceCode; - schema: [], - fixable: "code", + return { + IfStatement(node) { + const parent = node.parent, + grandparent = parent.parent; - messages: { - unexpectedLonelyIf: - "Unexpected if as the only statement in an else block.", - }, - }, + if (parent && parent.type === "BlockStatement" && + parent.body.length === 1 && grandparent && + grandparent.type === "IfStatement" && + parent === grandparent.alternate) { + context.report({ + node, + messageId: "unexpectedLonelyIf", + fix(fixer) { + const openingElseCurly = sourceCode.getFirstToken(parent); + const closingElseCurly = sourceCode.getLastToken(parent); + const elseKeyword = sourceCode.getTokenBefore(openingElseCurly); + const tokenAfterElseBlock = sourceCode.getTokenAfter(closingElseCurly); + const lastIfToken = sourceCode.getLastToken(node.consequent); + const sourceText = sourceCode.getText(); - create(context) { - const sourceCode = context.sourceCode; + if (sourceText.slice(openingElseCurly.range[1], + node.range[0]).trim() || sourceText.slice(node.range[1], closingElseCurly.range[0]).trim()) { - return { - IfStatement(node) { - const parent = node.parent, - grandparent = parent.parent; + // Don't fix if there are any non-whitespace characters interfering (e.g. comments) + return null; + } - if ( - parent && - parent.type === "BlockStatement" && - parent.body.length === 1 && - !astUtils.areBracesNecessary(parent, sourceCode) && - grandparent && - grandparent.type === "IfStatement" && - parent === grandparent.alternate - ) { - context.report({ - node, - messageId: "unexpectedLonelyIf", - fix(fixer) { - const openingElseCurly = - sourceCode.getFirstToken(parent); - const closingElseCurly = - sourceCode.getLastToken(parent); - const elseKeyword = - sourceCode.getTokenBefore(openingElseCurly); - const tokenAfterElseBlock = - sourceCode.getTokenAfter(closingElseCurly); - const lastIfToken = sourceCode.getLastToken( - node.consequent, - ); - const sourceText = sourceCode.getText(); + if ( + node.consequent.type !== "BlockStatement" && lastIfToken.value !== ";" && tokenAfterElseBlock && + ( + node.consequent.loc.end.line === tokenAfterElseBlock.loc.start.line || + /^[([/+`-]/u.test(tokenAfterElseBlock.value) || + lastIfToken.value === "++" || + lastIfToken.value === "--" + ) + ) { - if ( - sourceText - .slice( - openingElseCurly.range[1], - node.range[0], - ) - .trim() || - sourceText - .slice( - node.range[1], - closingElseCurly.range[0], - ) - .trim() - ) { - // Don't fix if there are any non-whitespace characters interfering (e.g. comments) - return null; - } + /* + * If the `if` statement has no block, and is not followed by a semicolon, make sure that fixing + * the issue would not change semantics due to ASI. If this would happen, don't do a fix. + */ + return null; + } - if ( - node.consequent.type !== "BlockStatement" && - lastIfToken.value !== ";" && - tokenAfterElseBlock && - (node.consequent.loc.end.line === - tokenAfterElseBlock.loc.start.line || - /^[([/+`-]/u.test( - tokenAfterElseBlock.value, - ) || - lastIfToken.value === "++" || - lastIfToken.value === "--") - ) { - /* - * If the `if` statement has no block, and is not followed by a semicolon, make sure that fixing - * the issue would not change semantics due to ASI. If this would happen, don't do a fix. - */ - return null; - } + return fixer.replaceTextRange( + [openingElseCurly.range[0], closingElseCurly.range[1]], + (elseKeyword.range[1] === openingElseCurly.range[0] ? " " : "") + sourceCode.getText(node) + ); + } + }); + } + } + }; - return fixer.replaceTextRange( - [ - openingElseCurly.range[0], - closingElseCurly.range[1], - ], - (elseKeyword.range[1] === - openingElseCurly.range[0] - ? " " - : "") + sourceCode.getText(node), - ); - }, - }); - } - }, - }; - }, + } }; diff --git a/node_modules/eslint/lib/rules/no-loop-func.js b/node_modules/eslint/lib/rules/no-loop-func.js index df6c8c5ec..48312fbf5 100644 --- a/node_modules/eslint/lib/rules/no-loop-func.js +++ b/node_modules/eslint/lib/rules/no-loop-func.js @@ -9,259 +9,198 @@ // Helpers //------------------------------------------------------------------------------ -const CONSTANT_BINDINGS = new Set(["const", "using", "await using"]); +/** + * Gets the containing loop node of a specified node. + * + * We don't need to check nested functions, so this ignores those. + * `Scope.through` contains references of nested functions. + * @param {ASTNode} node An AST node to get. + * @returns {ASTNode|null} The containing loop node of the specified node, or + * `null`. + */ +function getContainingLoopNode(node) { + for (let currentNode = node; currentNode.parent; currentNode = currentNode.parent) { + const parent = currentNode.parent; + + switch (parent.type) { + case "WhileStatement": + case "DoWhileStatement": + return parent; + + case "ForStatement": + + // `init` is outside of the loop. + if (parent.init !== currentNode) { + return parent; + } + break; + + case "ForInStatement": + case "ForOfStatement": + + // `right` is outside of the loop. + if (parent.right !== currentNode) { + return parent; + } + break; + + case "ArrowFunctionExpression": + case "FunctionExpression": + case "FunctionDeclaration": + + // We don't need to check nested functions. + return null; + + default: + break; + } + } + + return null; +} + +/** + * Gets the containing loop node of a given node. + * If the loop was nested, this returns the most outer loop. + * @param {ASTNode} node A node to get. This is a loop node. + * @param {ASTNode|null} excludedNode A node that the result node should not + * include. + * @returns {ASTNode} The most outer loop node. + */ +function getTopLoopNode(node, excludedNode) { + const border = excludedNode ? excludedNode.range[1] : 0; + let retv = node; + let containingLoopNode = node; + + while (containingLoopNode && containingLoopNode.range[0] >= border) { + retv = containingLoopNode; + containingLoopNode = getContainingLoopNode(containingLoopNode); + } + + return retv; +} /** - * Identifies is a node is a FunctionExpression which is part of an IIFE - * @param {ASTNode} node Node to test - * @returns {boolean} True if it's an IIFE + * Checks whether a given reference which refers to an upper scope's variable is + * safe or not. + * @param {ASTNode} loopNode A containing loop node. + * @param {eslint-scope.Reference} reference A reference to check. + * @returns {boolean} `true` if the reference is safe or not. */ -function isIIFE(node) { - return ( - (node.type === "FunctionExpression" || - node.type === "ArrowFunctionExpression") && - node.parent && - node.parent.type === "CallExpression" && - node.parent.callee === node - ); +function isSafe(loopNode, reference) { + const variable = reference.resolved; + const definition = variable && variable.defs[0]; + const declaration = definition && definition.parent; + const kind = (declaration && declaration.type === "VariableDeclaration") + ? declaration.kind + : ""; + + // Variables which are declared by `const` is safe. + if (kind === "const") { + return true; + } + + /* + * Variables which are declared by `let` in the loop is safe. + * It's a different instance from the next loop step's. + */ + if (kind === "let" && + declaration.range[0] > loopNode.range[0] && + declaration.range[1] < loopNode.range[1] + ) { + return true; + } + + /* + * WriteReferences which exist after this border are unsafe because those + * can modify the variable. + */ + const border = getTopLoopNode( + loopNode, + (kind === "let") ? declaration : null + ).range[0]; + + /** + * Checks whether a given reference is safe or not. + * The reference is every reference of the upper scope's variable we are + * looking now. + * + * It's safe if the reference matches one of the following condition. + * - is readonly. + * - doesn't exist inside a local function and after the border. + * @param {eslint-scope.Reference} upperRef A reference to check. + * @returns {boolean} `true` if the reference is safe. + */ + function isSafeReference(upperRef) { + const id = upperRef.identifier; + + return ( + !upperRef.isWrite() || + variable.scope.variableScope === upperRef.from.variableScope && + id.range[0] < border + ); + } + + return Boolean(variable) && variable.references.every(isSafeReference); } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - dialects: ["typescript", "javascript"], - language: "javascript", - - docs: { - description: - "Disallow function declarations that contain unsafe references inside loop statements", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-loop-func", - }, - - schema: [], - - messages: { - unsafeRefs: - "Function declared in a loop contains unsafe references to variable(s) {{ varNames }}.", - }, - }, - - create(context) { - const SKIPPED_IIFE_NODES = new Set(); - const sourceCode = context.sourceCode; - - /** - * Gets the containing loop node of a specified node. - * - * We don't need to check nested functions, so this ignores those, with the exception of IIFE. - * `Scope.through` contains references of nested functions. - * @param {ASTNode} node An AST node to get. - * @returns {ASTNode|null} The containing loop node of the specified node, or - * `null`. - */ - function getContainingLoopNode(node) { - for ( - let currentNode = node; - currentNode.parent; - currentNode = currentNode.parent - ) { - const parent = currentNode.parent; - - switch (parent.type) { - case "WhileStatement": - case "DoWhileStatement": - return parent; - - case "ForStatement": - // `init` is outside of the loop. - if (parent.init !== currentNode) { - return parent; - } - break; - - case "ForInStatement": - case "ForOfStatement": - // `right` is outside of the loop. - if (parent.right !== currentNode) { - return parent; - } - break; - - case "ArrowFunctionExpression": - case "FunctionExpression": - case "FunctionDeclaration": - // We need to check nested functions only in case of IIFE. - if (SKIPPED_IIFE_NODES.has(parent)) { - break; - } - - return null; - default: - break; - } - } - - return null; - } - - /** - * Gets the containing loop node of a given node. - * If the loop was nested, this returns the most outer loop. - * @param {ASTNode} node A node to get. This is a loop node. - * @param {ASTNode|null} excludedNode A node that the result node should not - * include. - * @returns {ASTNode} The most outer loop node. - */ - function getTopLoopNode(node, excludedNode) { - const border = excludedNode ? excludedNode.range[1] : 0; - let retv = node; - let containingLoopNode = node; - - while ( - containingLoopNode && - containingLoopNode.range[0] >= border - ) { - retv = containingLoopNode; - containingLoopNode = getContainingLoopNode(containingLoopNode); - } - - return retv; - } - - /** - * Checks whether a given reference which refers to an upper scope's variable is - * safe or not. - * @param {ASTNode} loopNode A containing loop node. - * @param {eslint-scope.Reference} reference A reference to check. - * @returns {boolean} `true` if the reference is safe or not. - */ - function isSafe(loopNode, reference) { - const variable = reference.resolved; - const definition = variable && variable.defs[0]; - const declaration = definition && definition.parent; - const kind = - declaration && declaration.type === "VariableDeclaration" - ? declaration.kind - : ""; - - // Constant variables are safe. - if (CONSTANT_BINDINGS.has(kind)) { - return true; - } - - /* - * Variables which are declared by `let` in the loop is safe. - * It's a different instance from the next loop step's. - */ - if ( - kind === "let" && - declaration.range[0] > loopNode.range[0] && - declaration.range[1] < loopNode.range[1] - ) { - return true; - } - - /* - * WriteReferences which exist after this border are unsafe because those - * can modify the variable. - */ - const border = getTopLoopNode( - loopNode, - kind === "let" ? declaration : null, - ).range[0]; - - /** - * Checks whether a given reference is safe or not. - * The reference is every reference of the upper scope's variable we are - * looking now. - * - * It's safe if the reference matches one of the following condition. - * - is readonly. - * - doesn't exist inside a local function and after the border. - * @param {eslint-scope.Reference} upperRef A reference to check. - * @returns {boolean} `true` if the reference is safe. - */ - function isSafeReference(upperRef) { - const id = upperRef.identifier; - - return ( - !upperRef.isWrite() || - (variable.scope.variableScope === - upperRef.from.variableScope && - id.range[0] < border) - ); - } - - return ( - Boolean(variable) && variable.references.every(isSafeReference) - ); - } - - /** - * Reports functions which match the following condition: - * - * - has a loop node in ancestors. - * - has any references which refers to an unsafe variable. - * @param {ASTNode} node The AST node to check. - * @returns {void} - */ - function checkForLoops(node) { - const loopNode = getContainingLoopNode(node); - - if (!loopNode) { - return; - } - - const references = sourceCode.getScope(node).through; - - // Check if the function is not asynchronous or a generator function - if (!(node.async || node.generator)) { - if (isIIFE(node)) { - const isFunctionExpression = - node.type === "FunctionExpression"; - - // Check if the function is referenced elsewhere in the code - const isFunctionReferenced = - isFunctionExpression && node.id - ? references.some( - r => r.identifier.name === node.id.name, - ) - : false; - - if (!isFunctionReferenced) { - SKIPPED_IIFE_NODES.add(node); - return; - } - } - } - - const unsafeRefs = [ - ...new Set( - references - .filter(r => r.resolved && !isSafe(loopNode, r)) - .map(r => r.identifier.name), - ), - ]; - - if (unsafeRefs.length > 0) { - context.report({ - node, - messageId: "unsafeRefs", - data: { varNames: `'${unsafeRefs.join("', '")}'` }, - }); - } - } - - return { - ArrowFunctionExpression: checkForLoops, - FunctionExpression: checkForLoops, - FunctionDeclaration: checkForLoops, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow function declarations that contain unsafe references inside loop statements", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-loop-func" + }, + + schema: [], + + messages: { + unsafeRefs: "Function declared in a loop contains unsafe references to variable(s) {{ varNames }}." + } + }, + + create(context) { + + const sourceCode = context.sourceCode; + + /** + * Reports functions which match the following condition: + * + * - has a loop node in ancestors. + * - has any references which refers to an unsafe variable. + * @param {ASTNode} node The AST node to check. + * @returns {void} + */ + function checkForLoops(node) { + const loopNode = getContainingLoopNode(node); + + if (!loopNode) { + return; + } + + const references = sourceCode.getScope(node).through; + const unsafeRefs = references.filter(r => r.resolved && !isSafe(loopNode, r)).map(r => r.identifier.name); + + if (unsafeRefs.length > 0) { + context.report({ + node, + messageId: "unsafeRefs", + data: { varNames: `'${unsafeRefs.join("', '")}'` } + }); + } + } + + return { + ArrowFunctionExpression: checkForLoops, + FunctionExpression: checkForLoops, + FunctionDeclaration: checkForLoops + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-loss-of-precision.js b/node_modules/eslint/lib/rules/no-loss-of-precision.js index 8e2005f15..b3635e3d5 100644 --- a/node_modules/eslint/lib/rules/no-loss-of-precision.js +++ b/node_modules/eslint/lib/rules/no-loss-of-precision.js @@ -5,245 +5,210 @@ "use strict"; -//------------------------------------------------------------------------------ -// Helpers -//------------------------------------------------------------------------------ - -/** Class representing a number in scientific notation. */ -class ScientificNotation { - /** @type {string} The digits of the coefficient. A decimal point is implied after the first digit. */ - coefficient; - - /** @type {number} The order of magnitude. */ - magnitude; - - constructor(coefficient, magnitude) { - this.coefficient = coefficient; - this.magnitude = magnitude; - } - - /* c8 ignore start -- debug only */ - toString() { - return `${this.coefficient[0]}${this.coefficient.length > 1 ? `.${this.coefficient.slice(1)}` : ""}e${this.magnitude}`; - } - /* c8 ignore stop */ -} - -/** - * Returns whether the node is number literal - * @param {Node} node the node literal being evaluated - * @returns {boolean} true if the node is a number literal - */ -function isNumber(node) { - return typeof node.value === "number"; -} - -/** - * Gets the source code of the given number literal. Removes `_` numeric separators from the result. - * @param {Node} node the number `Literal` node - * @returns {string} raw source code of the literal, without numeric separators - */ -function getRaw(node) { - return node.raw.replace(/_/gu, ""); -} - -/** - * Checks whether the number is base ten - * @param {ASTNode} node the node being evaluated - * @returns {boolean} true if the node is in base ten - */ -function isBaseTen(node) { - const prefixes = ["0x", "0X", "0b", "0B", "0o", "0O"]; - - return ( - prefixes.every(prefix => !node.raw.startsWith(prefix)) && - !/^0[0-7]+$/u.test(node.raw) - ); -} - -/** - * Checks that the user-intended non-base ten number equals the actual number after is has been converted to the Number type - * @param {Node} node the node being evaluated - * @returns {boolean} true if they do not match - */ -function notBaseTenLosesPrecision(node) { - const rawString = getRaw(node).toUpperCase(); - let base; - - if (rawString.startsWith("0B")) { - base = 2; - } else if (rawString.startsWith("0X")) { - base = 16; - } else { - base = 8; - } - - return !rawString.endsWith(node.value.toString(base).toUpperCase()); -} - -/** - * Returns the number stripped of leading zeros - * @param {string} numberAsString the string representation of the number - * @returns {string} the stripped string - */ -function removeLeadingZeros(numberAsString) { - for (let i = 0; i < numberAsString.length; i++) { - if (numberAsString[i] !== "0") { - return numberAsString.slice(i); - } - } - return numberAsString; -} - -/** - * Returns the number stripped of trailing zeros - * @param {string} numberAsString the string representation of the number - * @returns {string} the stripped string - */ -function removeTrailingZeros(numberAsString) { - for (let i = numberAsString.length - 1; i >= 0; i--) { - if (numberAsString[i] !== "0") { - return numberAsString.slice(0, i + 1); - } - } - return numberAsString; -} - -/** - * Converts an integer to an object containing the integer's coefficient and order of magnitude - * @param {string} stringInteger the string representation of the integer being converted - * @returns {ScientificNotation} the object containing the integer's coefficient and order of magnitude - */ -function normalizeInteger(stringInteger) { - const trimmedInteger = removeLeadingZeros(stringInteger); - const significantDigits = removeTrailingZeros(trimmedInteger); - - return new ScientificNotation(significantDigits, trimmedInteger.length - 1); -} - -/** - * Converts a float to an object containing the float's coefficient and order of magnitude - * @param {string} stringFloat the string representation of the float being converted - * @returns {ScientificNotation} the object containing the float's coefficient and order of magnitude - */ -function normalizeFloat(stringFloat) { - const trimmedFloat = removeLeadingZeros(stringFloat); - const indexOfDecimalPoint = trimmedFloat.indexOf("."); - - switch (indexOfDecimalPoint) { - case 0: { - const significantDigits = removeLeadingZeros(trimmedFloat.slice(1)); - - return new ScientificNotation( - significantDigits, - significantDigits.length - trimmedFloat.length, - ); - } - case -1: - return new ScientificNotation( - trimmedFloat, - trimmedFloat.length - 1, - ); - default: - return new ScientificNotation( - trimmedFloat.replace(".", ""), - indexOfDecimalPoint - 1, - ); - } -} - -/** - * Converts a base ten number to proper scientific notation - * @param {string} stringNumber the string representation of the base ten number to be converted - * @param {boolean} parseAsFloat if true, the coefficient will be always parsed as a float, regardless of whether a decimal point is present - * @returns {ScientificNotation} the object containing the number's coefficient and order of magnitude - */ -function convertNumberToScientificNotation(stringNumber, parseAsFloat) { - const splitNumber = stringNumber.split("e"); - const originalCoefficient = splitNumber[0]; - const normalizedNumber = - parseAsFloat || stringNumber.includes(".") - ? normalizeFloat(originalCoefficient) - : normalizeInteger(originalCoefficient); - if (splitNumber.length > 1) { - normalizedNumber.magnitude += parseInt(splitNumber[1], 10); - } - - return normalizedNumber; -} - -/** - * Checks that the user-intended base ten number equals the actual number after is has been converted to the Number type - * @param {Node} node the node being evaluated - * @returns {boolean} true if they do not match - */ -function baseTenLosesPrecision(node) { - const rawNumber = getRaw(node).toLowerCase(); - const normalizedRawNumber = convertNumberToScientificNotation( - rawNumber, - false, - ); - const requestedPrecision = normalizedRawNumber.coefficient.length; - - if (requestedPrecision > 100) { - return true; - } - const storedNumber = node.value.toPrecision(requestedPrecision); - const normalizedStoredNumber = convertNumberToScientificNotation( - storedNumber, - true, - ); - - return ( - normalizedRawNumber.magnitude !== normalizedStoredNumber.magnitude || - normalizedRawNumber.coefficient !== normalizedStoredNumber.coefficient - ); -} - -/** - * Checks that the user-intended number equals the actual number after is has been converted to the Number type - * @param {Node} node the node being evaluated - * @returns {boolean} true if they do not match - */ -function losesPrecision(node) { - return isBaseTen(node) - ? baseTenLosesPrecision(node) - : notBaseTenLosesPrecision(node); -} - //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - dialects: ["typescript", "javascript"], - language: "javascript", - - docs: { - description: "Disallow literal numbers that lose precision", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-loss-of-precision", - }, - schema: [], - messages: { - noLossOfPrecision: - "This number literal will lose precision at runtime.", - }, - }, - - create(context) { - return { - Literal(node) { - if (node.value && isNumber(node) && losesPrecision(node)) { - context.report({ - messageId: "noLossOfPrecision", - node, - }); - } - }, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow literal numbers that lose precision", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-loss-of-precision" + }, + schema: [], + messages: { + noLossOfPrecision: "This number literal will lose precision at runtime." + } + }, + + create(context) { + + /** + * Returns whether the node is number literal + * @param {Node} node the node literal being evaluated + * @returns {boolean} true if the node is a number literal + */ + function isNumber(node) { + return typeof node.value === "number"; + } + + /** + * Gets the source code of the given number literal. Removes `_` numeric separators from the result. + * @param {Node} node the number `Literal` node + * @returns {string} raw source code of the literal, without numeric separators + */ + function getRaw(node) { + return node.raw.replace(/_/gu, ""); + } + + /** + * Checks whether the number is base ten + * @param {ASTNode} node the node being evaluated + * @returns {boolean} true if the node is in base ten + */ + function isBaseTen(node) { + const prefixes = ["0x", "0X", "0b", "0B", "0o", "0O"]; + + return prefixes.every(prefix => !node.raw.startsWith(prefix)) && + !/^0[0-7]+$/u.test(node.raw); + } + + /** + * Checks that the user-intended non-base ten number equals the actual number after is has been converted to the Number type + * @param {Node} node the node being evaluated + * @returns {boolean} true if they do not match + */ + function notBaseTenLosesPrecision(node) { + const rawString = getRaw(node).toUpperCase(); + let base = 0; + + if (rawString.startsWith("0B")) { + base = 2; + } else if (rawString.startsWith("0X")) { + base = 16; + } else { + base = 8; + } + + return !rawString.endsWith(node.value.toString(base).toUpperCase()); + } + + /** + * Adds a decimal point to the numeric string at index 1 + * @param {string} stringNumber the numeric string without any decimal point + * @returns {string} the numeric string with a decimal point in the proper place + */ + function addDecimalPointToNumber(stringNumber) { + return `${stringNumber[0]}.${stringNumber.slice(1)}`; + } + + /** + * Returns the number stripped of leading zeros + * @param {string} numberAsString the string representation of the number + * @returns {string} the stripped string + */ + function removeLeadingZeros(numberAsString) { + for (let i = 0; i < numberAsString.length; i++) { + if (numberAsString[i] !== "0") { + return numberAsString.slice(i); + } + } + return numberAsString; + } + + /** + * Returns the number stripped of trailing zeros + * @param {string} numberAsString the string representation of the number + * @returns {string} the stripped string + */ + function removeTrailingZeros(numberAsString) { + for (let i = numberAsString.length - 1; i >= 0; i--) { + if (numberAsString[i] !== "0") { + return numberAsString.slice(0, i + 1); + } + } + return numberAsString; + } + + /** + * Converts an integer to an object containing the integer's coefficient and order of magnitude + * @param {string} stringInteger the string representation of the integer being converted + * @returns {Object} the object containing the integer's coefficient and order of magnitude + */ + function normalizeInteger(stringInteger) { + const significantDigits = removeTrailingZeros(removeLeadingZeros(stringInteger)); + + return { + magnitude: stringInteger.startsWith("0") ? stringInteger.length - 2 : stringInteger.length - 1, + coefficient: addDecimalPointToNumber(significantDigits) + }; + } + + /** + * + * Converts a float to an object containing the floats's coefficient and order of magnitude + * @param {string} stringFloat the string representation of the float being converted + * @returns {Object} the object containing the integer's coefficient and order of magnitude + */ + function normalizeFloat(stringFloat) { + const trimmedFloat = removeLeadingZeros(stringFloat); + + if (trimmedFloat.startsWith(".")) { + const decimalDigits = trimmedFloat.slice(1); + const significantDigits = removeLeadingZeros(decimalDigits); + + return { + magnitude: significantDigits.length - decimalDigits.length - 1, + coefficient: addDecimalPointToNumber(significantDigits) + }; + + } + return { + magnitude: trimmedFloat.indexOf(".") - 1, + coefficient: addDecimalPointToNumber(trimmedFloat.replace(".", "")) + + }; + } + + /** + * Converts a base ten number to proper scientific notation + * @param {string} stringNumber the string representation of the base ten number to be converted + * @returns {string} the number converted to scientific notation + */ + function convertNumberToScientificNotation(stringNumber) { + const splitNumber = stringNumber.replace("E", "e").split("e"); + const originalCoefficient = splitNumber[0]; + const normalizedNumber = stringNumber.includes(".") ? normalizeFloat(originalCoefficient) + : normalizeInteger(originalCoefficient); + const normalizedCoefficient = normalizedNumber.coefficient; + const magnitude = splitNumber.length > 1 ? (parseInt(splitNumber[1], 10) + normalizedNumber.magnitude) + : normalizedNumber.magnitude; + + return `${normalizedCoefficient}e${magnitude}`; + } + + /** + * Checks that the user-intended base ten number equals the actual number after is has been converted to the Number type + * @param {Node} node the node being evaluated + * @returns {boolean} true if they do not match + */ + function baseTenLosesPrecision(node) { + const normalizedRawNumber = convertNumberToScientificNotation(getRaw(node)); + const requestedPrecision = normalizedRawNumber.split("e")[0].replace(".", "").length; + + if (requestedPrecision > 100) { + return true; + } + const storedNumber = node.value.toPrecision(requestedPrecision); + const normalizedStoredNumber = convertNumberToScientificNotation(storedNumber); + + return normalizedRawNumber !== normalizedStoredNumber; + } + + + /** + * Checks that the user-intended number equals the actual number after is has been converted to the Number type + * @param {Node} node the node being evaluated + * @returns {boolean} true if they do not match + */ + function losesPrecision(node) { + return isBaseTen(node) ? baseTenLosesPrecision(node) : notBaseTenLosesPrecision(node); + } + + + return { + Literal(node) { + if (node.value && isNumber(node) && losesPrecision(node)) { + context.report({ + messageId: "noLossOfPrecision", + node + }); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-magic-numbers.js b/node_modules/eslint/lib/rules/no-magic-numbers.js index bed61a068..f48a62d85 100644 --- a/node_modules/eslint/lib/rules/no-magic-numbers.js +++ b/node_modules/eslint/lib/rules/no-magic-numbers.js @@ -20,346 +20,224 @@ const MAX_ARRAY_LENGTH = 2 ** 32 - 1; * @returns {bigint|number} The normalized value. */ function normalizeIgnoreValue(x) { - if (typeof x === "string") { - return BigInt(x.slice(0, -1)); - } - return x; + if (typeof x === "string") { + return BigInt(x.slice(0, -1)); + } + return x; } -/** - * Checks if the node parent is a TypeScript enum member - * @param {ASTNode} node The node to be validated - * @returns {boolean} True if the node parent is a TypeScript enum member - */ -function isParentTSEnumDeclaration(node) { - return node.parent.type === "TSEnumMember"; -} - -/** - * Checks if the node is a valid TypeScript numeric literal type. - * @param {ASTNode} node The node to be validated - * @returns {boolean} True if the node is a TypeScript numeric literal type - */ -function isTSNumericLiteralType(node) { - let ancestor = node.parent; - - // Go up while we're part of a type union - while (ancestor.parent.type === "TSUnionType") { - ancestor = ancestor.parent; - } - - // Check if the final ancestor is in a type alias declaration - return ancestor.parent.type === "TSTypeAliasDeclaration"; -} - -/** - * Checks if the node parent is a readonly class property - * @param {ASTNode} node The node to be validated - * @returns {boolean} True if the node parent is a readonly class property - */ -function isParentTSReadonlyPropertyDefinition(node) { - if (node.parent?.type === "PropertyDefinition" && node.parent.readonly) { - return true; - } - - return false; -} - -/** - * Checks if the node is part of a type indexed access (eg. Foo[4]) - * @param {ASTNode} node The node to be validated - * @returns {boolean} True if the node is part of an indexed access - */ -function isAncestorTSIndexedAccessType(node) { - let ancestor = node.parent; - - /* - * Go up another level while we're part of a type union (eg. 1 | 2) or - * intersection (eg. 1 & 2) - */ - while ( - ancestor.parent.type === "TSUnionType" || - ancestor.parent.type === "TSIntersectionType" - ) { - ancestor = ancestor.parent; - } - - return ancestor.parent.type === "TSIndexedAccessType"; -} - -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - dialects: ["typescript", "javascript"], - language: "javascript", - - docs: { - description: "Disallow magic numbers", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/no-magic-numbers", - }, - - schema: [ - { - type: "object", - properties: { - detectObjects: { - type: "boolean", - default: false, - }, - enforceConst: { - type: "boolean", - default: false, - }, - ignore: { - type: "array", - items: { - anyOf: [ - { type: "number" }, - { - type: "string", - pattern: "^[+-]?(?:0|[1-9][0-9]*)n$", - }, - ], - }, - uniqueItems: true, - }, - ignoreArrayIndexes: { - type: "boolean", - default: false, - }, - ignoreDefaultValues: { - type: "boolean", - default: false, - }, - ignoreClassFieldInitialValues: { - type: "boolean", - default: false, - }, - ignoreEnums: { - type: "boolean", - default: false, - }, - ignoreNumericLiteralTypes: { - type: "boolean", - default: false, - }, - ignoreReadonlyClassProperties: { - type: "boolean", - default: false, - }, - ignoreTypeIndexes: { - type: "boolean", - default: false, - }, - }, - additionalProperties: false, - }, - ], - - messages: { - useConst: "Number constants declarations must use 'const'.", - noMagic: "No magic number: {{raw}}.", - }, - }, - - create(context) { - const config = context.options[0] || {}, - detectObjects = !!config.detectObjects, - enforceConst = !!config.enforceConst, - ignore = new Set((config.ignore || []).map(normalizeIgnoreValue)), - ignoreArrayIndexes = !!config.ignoreArrayIndexes, - ignoreDefaultValues = !!config.ignoreDefaultValues, - ignoreClassFieldInitialValues = - !!config.ignoreClassFieldInitialValues, - ignoreEnums = !!config.ignoreEnums, - ignoreNumericLiteralTypes = !!config.ignoreNumericLiteralTypes, - ignoreReadonlyClassProperties = - !!config.ignoreReadonlyClassProperties, - ignoreTypeIndexes = !!config.ignoreTypeIndexes; - - const okTypes = detectObjects - ? [] - : ["ObjectExpression", "Property", "AssignmentExpression"]; - - /** - * Returns whether the rule is configured to ignore the given value - * @param {bigint|number} value The value to check - * @returns {boolean} true if the value is ignored - */ - function isIgnoredValue(value) { - return ignore.has(value); - } - - /** - * Returns whether the number is a default value assignment. - * @param {ASTNode} fullNumberNode `Literal` or `UnaryExpression` full number node - * @returns {boolean} true if the number is a default value - */ - function isDefaultValue(fullNumberNode) { - const parent = fullNumberNode.parent; - - return ( - parent.type === "AssignmentPattern" && - parent.right === fullNumberNode - ); - } - - /** - * Returns whether the number is the initial value of a class field. - * @param {ASTNode} fullNumberNode `Literal` or `UnaryExpression` full number node - * @returns {boolean} true if the number is the initial value of a class field. - */ - function isClassFieldInitialValue(fullNumberNode) { - const parent = fullNumberNode.parent; - - return ( - parent.type === "PropertyDefinition" && - parent.value === fullNumberNode - ); - } - - /** - * Returns whether the given node is used as a radix within parseInt() or Number.parseInt() - * @param {ASTNode} fullNumberNode `Literal` or `UnaryExpression` full number node - * @returns {boolean} true if the node is radix - */ - function isParseIntRadix(fullNumberNode) { - const parent = fullNumberNode.parent; - - return ( - parent.type === "CallExpression" && - fullNumberNode === parent.arguments[1] && - (astUtils.isSpecificId(parent.callee, "parseInt") || - astUtils.isSpecificMemberAccess( - parent.callee, - "Number", - "parseInt", - )) - ); - } - - /** - * Returns whether the given node is a direct child of a JSX node. - * In particular, it aims to detect numbers used as prop values in JSX tags. - * Example: - * @param {ASTNode} fullNumberNode `Literal` or `UnaryExpression` full number node - * @returns {boolean} true if the node is a JSX number - */ - function isJSXNumber(fullNumberNode) { - return fullNumberNode.parent.type.indexOf("JSX") === 0; - } - - /** - * Returns whether the given node is used as an array index. - * Value must coerce to a valid array index name: "0", "1", "2" ... "4294967294". - * - * All other values, like "-1", "2.5", or "4294967295", are just "normal" object properties, - * which can be created and accessed on an array in addition to the array index properties, - * but they don't affect array's length and are not considered by methods such as .map(), .forEach() etc. - * - * The maximum array length by the specification is 2 ** 32 - 1 = 4294967295, - * thus the maximum valid index is 2 ** 32 - 2 = 4294967294. - * - * All notations are allowed, as long as the value coerces to one of "0", "1", "2" ... "4294967294". - * - * Valid examples: - * a[0], a[1], a[1.2e1], a[0xAB], a[0n], a[1n] - * a[-0] (same as a[0] because -0 coerces to "0") - * a[-0n] (-0n evaluates to 0n) - * - * Invalid examples: - * a[-1], a[-0xAB], a[-1n], a[2.5], a[1.23e1], a[12e-1] - * a[4294967295] (above the max index, it's an access to a regular property a["4294967295"]) - * a[999999999999999999999] (even if it wasn't above the max index, it would be a["1e+21"]) - * a[1e310] (same as a["Infinity"]) - * @param {ASTNode} fullNumberNode `Literal` or `UnaryExpression` full number node - * @param {bigint|number} value Value expressed by the fullNumberNode - * @returns {boolean} true if the node is a valid array index - */ - function isArrayIndex(fullNumberNode, value) { - const parent = fullNumberNode.parent; - - return ( - parent.type === "MemberExpression" && - parent.property === fullNumberNode && - (Number.isInteger(value) || typeof value === "bigint") && - value >= 0 && - value < MAX_ARRAY_LENGTH - ); - } - - return { - Literal(node) { - if (!astUtils.isNumericLiteral(node)) { - return; - } - - let fullNumberNode; - let value; - let raw; - - // Treat unary minus/plus as a part of the number - if ( - node.parent.type === "UnaryExpression" && - ["-", "+"].includes(node.parent.operator) - ) { - fullNumberNode = node.parent; - value = - node.parent.operator === "-" ? -node.value : node.value; - raw = `${node.parent.operator}${node.raw}`; - } else { - fullNumberNode = node; - value = node.value; - raw = node.raw; - } - - const parent = fullNumberNode.parent; - - // Always allow radix arguments and JSX props - if ( - isIgnoredValue(value) || - (ignoreDefaultValues && isDefaultValue(fullNumberNode)) || - (ignoreClassFieldInitialValues && - isClassFieldInitialValue(fullNumberNode)) || - (ignoreEnums && - isParentTSEnumDeclaration(fullNumberNode)) || - (ignoreNumericLiteralTypes && - isTSNumericLiteralType(fullNumberNode)) || - (ignoreTypeIndexes && - isAncestorTSIndexedAccessType(fullNumberNode)) || - (ignoreReadonlyClassProperties && - isParentTSReadonlyPropertyDefinition(fullNumberNode)) || - isParseIntRadix(fullNumberNode) || - isJSXNumber(fullNumberNode) || - (ignoreArrayIndexes && isArrayIndex(fullNumberNode, value)) - ) { - return; - } - - if (parent.type === "VariableDeclarator") { - if (enforceConst && parent.parent.kind !== "const") { - context.report({ - node: fullNumberNode, - messageId: "useConst", - }); - } - } else if ( - !okTypes.includes(parent.type) || - (parent.type === "AssignmentExpression" && - parent.left.type === "Identifier") - ) { - context.report({ - node: fullNumberNode, - messageId: "noMagic", - data: { - raw, - }, - }); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow magic numbers", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-magic-numbers" + }, + + schema: [{ + type: "object", + properties: { + detectObjects: { + type: "boolean", + default: false + }, + enforceConst: { + type: "boolean", + default: false + }, + ignore: { + type: "array", + items: { + anyOf: [ + { type: "number" }, + { type: "string", pattern: "^[+-]?(?:0|[1-9][0-9]*)n$" } + ] + }, + uniqueItems: true + }, + ignoreArrayIndexes: { + type: "boolean", + default: false + }, + ignoreDefaultValues: { + type: "boolean", + default: false + }, + ignoreClassFieldInitialValues: { + type: "boolean", + default: false + } + }, + additionalProperties: false + }], + + messages: { + useConst: "Number constants declarations must use 'const'.", + noMagic: "No magic number: {{raw}}." + } + }, + + create(context) { + const config = context.options[0] || {}, + detectObjects = !!config.detectObjects, + enforceConst = !!config.enforceConst, + ignore = new Set((config.ignore || []).map(normalizeIgnoreValue)), + ignoreArrayIndexes = !!config.ignoreArrayIndexes, + ignoreDefaultValues = !!config.ignoreDefaultValues, + ignoreClassFieldInitialValues = !!config.ignoreClassFieldInitialValues; + + const okTypes = detectObjects ? [] : ["ObjectExpression", "Property", "AssignmentExpression"]; + + /** + * Returns whether the rule is configured to ignore the given value + * @param {bigint|number} value The value to check + * @returns {boolean} true if the value is ignored + */ + function isIgnoredValue(value) { + return ignore.has(value); + } + + /** + * Returns whether the number is a default value assignment. + * @param {ASTNode} fullNumberNode `Literal` or `UnaryExpression` full number node + * @returns {boolean} true if the number is a default value + */ + function isDefaultValue(fullNumberNode) { + const parent = fullNumberNode.parent; + + return parent.type === "AssignmentPattern" && parent.right === fullNumberNode; + } + + /** + * Returns whether the number is the initial value of a class field. + * @param {ASTNode} fullNumberNode `Literal` or `UnaryExpression` full number node + * @returns {boolean} true if the number is the initial value of a class field. + */ + function isClassFieldInitialValue(fullNumberNode) { + const parent = fullNumberNode.parent; + + return parent.type === "PropertyDefinition" && parent.value === fullNumberNode; + } + + /** + * Returns whether the given node is used as a radix within parseInt() or Number.parseInt() + * @param {ASTNode} fullNumberNode `Literal` or `UnaryExpression` full number node + * @returns {boolean} true if the node is radix + */ + function isParseIntRadix(fullNumberNode) { + const parent = fullNumberNode.parent; + + return parent.type === "CallExpression" && fullNumberNode === parent.arguments[1] && + ( + astUtils.isSpecificId(parent.callee, "parseInt") || + astUtils.isSpecificMemberAccess(parent.callee, "Number", "parseInt") + ); + } + + /** + * Returns whether the given node is a direct child of a JSX node. + * In particular, it aims to detect numbers used as prop values in JSX tags. + * Example: + * @param {ASTNode} fullNumberNode `Literal` or `UnaryExpression` full number node + * @returns {boolean} true if the node is a JSX number + */ + function isJSXNumber(fullNumberNode) { + return fullNumberNode.parent.type.indexOf("JSX") === 0; + } + + /** + * Returns whether the given node is used as an array index. + * Value must coerce to a valid array index name: "0", "1", "2" ... "4294967294". + * + * All other values, like "-1", "2.5", or "4294967295", are just "normal" object properties, + * which can be created and accessed on an array in addition to the array index properties, + * but they don't affect array's length and are not considered by methods such as .map(), .forEach() etc. + * + * The maximum array length by the specification is 2 ** 32 - 1 = 4294967295, + * thus the maximum valid index is 2 ** 32 - 2 = 4294967294. + * + * All notations are allowed, as long as the value coerces to one of "0", "1", "2" ... "4294967294". + * + * Valid examples: + * a[0], a[1], a[1.2e1], a[0xAB], a[0n], a[1n] + * a[-0] (same as a[0] because -0 coerces to "0") + * a[-0n] (-0n evaluates to 0n) + * + * Invalid examples: + * a[-1], a[-0xAB], a[-1n], a[2.5], a[1.23e1], a[12e-1] + * a[4294967295] (above the max index, it's an access to a regular property a["4294967295"]) + * a[999999999999999999999] (even if it wasn't above the max index, it would be a["1e+21"]) + * a[1e310] (same as a["Infinity"]) + * @param {ASTNode} fullNumberNode `Literal` or `UnaryExpression` full number node + * @param {bigint|number} value Value expressed by the fullNumberNode + * @returns {boolean} true if the node is a valid array index + */ + function isArrayIndex(fullNumberNode, value) { + const parent = fullNumberNode.parent; + + return parent.type === "MemberExpression" && parent.property === fullNumberNode && + (Number.isInteger(value) || typeof value === "bigint") && + value >= 0 && value < MAX_ARRAY_LENGTH; + } + + return { + Literal(node) { + if (!astUtils.isNumericLiteral(node)) { + return; + } + + let fullNumberNode; + let value; + let raw; + + // Treat unary minus as a part of the number + if (node.parent.type === "UnaryExpression" && node.parent.operator === "-") { + fullNumberNode = node.parent; + value = -node.value; + raw = `-${node.raw}`; + } else { + fullNumberNode = node; + value = node.value; + raw = node.raw; + } + + const parent = fullNumberNode.parent; + + // Always allow radix arguments and JSX props + if ( + isIgnoredValue(value) || + (ignoreDefaultValues && isDefaultValue(fullNumberNode)) || + (ignoreClassFieldInitialValues && isClassFieldInitialValue(fullNumberNode)) || + isParseIntRadix(fullNumberNode) || + isJSXNumber(fullNumberNode) || + (ignoreArrayIndexes && isArrayIndex(fullNumberNode, value)) + ) { + return; + } + + if (parent.type === "VariableDeclarator") { + if (enforceConst && parent.parent.kind !== "const") { + context.report({ + node: fullNumberNode, + messageId: "useConst" + }); + } + } else if ( + !okTypes.includes(parent.type) || + (parent.type === "AssignmentExpression" && parent.left.type === "Identifier") + ) { + context.report({ + node: fullNumberNode, + messageId: "noMagic", + data: { + raw + } + }); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-misleading-character-class.js b/node_modules/eslint/lib/rules/no-misleading-character-class.js index c7223335f..20591df2c 100644 --- a/node_modules/eslint/lib/rules/no-misleading-character-class.js +++ b/node_modules/eslint/lib/rules/no-misleading-character-class.js @@ -3,26 +3,11 @@ */ "use strict"; -const { - CALL, - CONSTRUCT, - ReferenceTracker, - getStaticValue, - getStringIfConstant, -} = require("@eslint-community/eslint-utils"); +const { CALL, CONSTRUCT, ReferenceTracker, getStringIfConstant } = require("@eslint-community/eslint-utils"); const { RegExpParser, visitRegExpAST } = require("@eslint-community/regexpp"); -const { - isCombiningCharacter, - isEmojiModifier, - isRegionalIndicatorSymbol, - isSurrogatePair, -} = require("./utils/unicode"); +const { isCombiningCharacter, isEmojiModifier, isRegionalIndicatorSymbol, isSurrogatePair } = require("./utils/unicode"); const astUtils = require("./utils/ast-utils.js"); const { isValidWithUnicodeFlag } = require("./utils/regular-expressions"); -const { - parseStringLiteral, - parseTemplateToken, -} = require("./utils/char-source"); //------------------------------------------------------------------------------ // Helpers @@ -41,555 +26,275 @@ const { * @param {CharacterClassElement[]} nodes The node list to iterate character sequences. * @returns {IterableIterator} The list of character sequences. */ -function* iterateCharacterSequence(nodes) { - /** @type {Character[]} */ - let seq = []; - - for (const node of nodes) { - switch (node.type) { - case "Character": - seq.push(node); - break; - - case "CharacterClassRange": - seq.push(node.min); - yield seq; - seq = [node.max]; - break; - - case "CharacterSet": - case "CharacterClass": // [[]] nesting character class - case "ClassStringDisjunction": // \q{...} - case "ExpressionCharacterClass": // [A--B] - if (seq.length > 0) { - yield seq; - seq = []; - } - break; - - // no default - } - } - - if (seq.length > 0) { - yield seq; - } +function *iterateCharacterSequence(nodes) { + + /** @type {Character[]} */ + let seq = []; + + for (const node of nodes) { + switch (node.type) { + case "Character": + seq.push(node); + break; + + case "CharacterClassRange": + seq.push(node.min); + yield seq; + seq = [node.max]; + break; + + case "CharacterSet": + case "CharacterClass": // [[]] nesting character class + case "ClassStringDisjunction": // \q{...} + case "ExpressionCharacterClass": // [A--B] + if (seq.length > 0) { + yield seq; + seq = []; + } + break; + + // no default + } + } + + if (seq.length > 0) { + yield seq; + } } + /** * Checks whether the given character node is a Unicode code point escape or not. * @param {Character} char the character node to check. * @returns {boolean} `true` if the character node is a Unicode code point escape. */ function isUnicodeCodePointEscape(char) { - return /^\\u\{[\da-f]+\}$/iu.test(char.raw); + return /^\\u\{[\da-f]+\}$/iu.test(char.raw); } /** - * Each function returns matched characters if it detects that kind of problem. - * @type {Record IterableIterator>} + * Each function returns `true` if it detects that kind of problem. + * @type {Record boolean>} */ -const findCharacterSequences = { - *surrogatePairWithoutUFlag(chars) { - for (const [index, char] of chars.entries()) { - const previous = chars[index - 1]; - - if ( - previous && - char && - isSurrogatePair(previous.value, char.value) && - !isUnicodeCodePointEscape(previous) && - !isUnicodeCodePointEscape(char) - ) { - yield [previous, char]; - } - } - }, - - *surrogatePair(chars) { - for (const [index, char] of chars.entries()) { - const previous = chars[index - 1]; - - if ( - previous && - char && - isSurrogatePair(previous.value, char.value) && - (isUnicodeCodePointEscape(previous) || - isUnicodeCodePointEscape(char)) - ) { - yield [previous, char]; - } - } - }, - - *combiningClass(chars, unfilteredChars) { - /* - * When `allowEscape` is `true`, a combined character should only be allowed if the combining mark appears as an escape sequence. - * This means that the base character should be considered even if it's escaped. - */ - for (const [index, char] of chars.entries()) { - const previous = unfilteredChars[index - 1]; - - if ( - previous && - char && - isCombiningCharacter(char.value) && - !isCombiningCharacter(previous.value) - ) { - yield [previous, char]; - } - } - }, - - *emojiModifier(chars) { - for (const [index, char] of chars.entries()) { - const previous = chars[index - 1]; - - if ( - previous && - char && - isEmojiModifier(char.value) && - !isEmojiModifier(previous.value) - ) { - yield [previous, char]; - } - } - }, - - *regionalIndicatorSymbol(chars) { - for (const [index, char] of chars.entries()) { - const previous = chars[index - 1]; - - if ( - previous && - char && - isRegionalIndicatorSymbol(char.value) && - isRegionalIndicatorSymbol(previous.value) - ) { - yield [previous, char]; - } - } - }, - - *zwj(chars) { - let sequence = null; - - for (const [index, char] of chars.entries()) { - const previous = chars[index - 1]; - const next = chars[index + 1]; - - if ( - previous && - char && - next && - char.value === 0x200d && - previous.value !== 0x200d && - next.value !== 0x200d - ) { - if (sequence) { - if (sequence.at(-1) === previous) { - sequence.push(char, next); // append to the sequence - } else { - yield sequence; - sequence = chars.slice(index - 1, index + 2); - } - } else { - sequence = chars.slice(index - 1, index + 2); - } - } - } - - if (sequence) { - yield sequence; - } - }, +const hasCharacterSequence = { + surrogatePairWithoutUFlag(chars) { + return chars.some((c, i) => { + if (i === 0) { + return false; + } + const c1 = chars[i - 1]; + + return ( + isSurrogatePair(c1.value, c.value) && + !isUnicodeCodePointEscape(c1) && + !isUnicodeCodePointEscape(c) + ); + }); + }, + + surrogatePair(chars) { + return chars.some((c, i) => { + if (i === 0) { + return false; + } + const c1 = chars[i - 1]; + + return ( + isSurrogatePair(c1.value, c.value) && + ( + isUnicodeCodePointEscape(c1) || + isUnicodeCodePointEscape(c) + ) + ); + }); + }, + + combiningClass(chars) { + return chars.some((c, i) => ( + i !== 0 && + isCombiningCharacter(c.value) && + !isCombiningCharacter(chars[i - 1].value) + )); + }, + + emojiModifier(chars) { + return chars.some((c, i) => ( + i !== 0 && + isEmojiModifier(c.value) && + !isEmojiModifier(chars[i - 1].value) + )); + }, + + regionalIndicatorSymbol(chars) { + return chars.some((c, i) => ( + i !== 0 && + isRegionalIndicatorSymbol(c.value) && + isRegionalIndicatorSymbol(chars[i - 1].value) + )); + }, + + zwj(chars) { + const lastIndex = chars.length - 1; + + return chars.some((c, i) => ( + i !== 0 && + i !== lastIndex && + c.value === 0x200d && + chars[i - 1].value !== 0x200d && + chars[i + 1].value !== 0x200d + )); + } }; -const kinds = Object.keys(findCharacterSequences); - -/** - * Gets the value of the given node if it's a static value other than a regular expression object, - * or the node's `regex` property. - * The purpose of this method is to provide a replacement for `getStaticValue` in environments where certain regular expressions cannot be evaluated. - * A known example is Node.js 18 which does not support the `v` flag. - * Calling `getStaticValue` on a regular expression node with the `v` flag on Node.js 18 always returns `null`. - * A limitation of this method is that it can only detect a regular expression if the specified node is itself a regular expression literal node. - * @param {ASTNode | undefined} node The node to be inspected. - * @param {Scope} initialScope Scope to start finding variables. This function tries to resolve identifier references which are in the given scope. - * @returns {{ value: any } | { regex: { pattern: string, flags: string } } | null} The static value of the node, or `null`. - */ -function getStaticValueOrRegex(node, initialScope) { - if (!node) { - return null; - } - if (node.type === "Literal" && node.regex) { - return { regex: node.regex }; - } - - const staticValue = getStaticValue(node, initialScope); - - if (staticValue?.value instanceof RegExp) { - return null; - } - return staticValue; -} - -/** - * Checks whether a specified regexpp character is represented as an acceptable escape sequence. - * This function requires the source text of the character to be known. - * @param {Character} char Character to check. - * @param {string} charSource Source text of the character to check. - * @returns {boolean} Whether the specified regexpp character is represented as an acceptable escape sequence. - */ -function checkForAcceptableEscape(char, charSource) { - if (!charSource.startsWith("\\")) { - return false; - } - const match = /(?<=^\\+).$/su.exec(charSource); - - return match?.[0] !== String.fromCodePoint(char.value); -} - -/** - * Checks whether a specified regexpp character is represented as an acceptable escape sequence. - * This function works with characters that are produced by a string or template literal. - * It requires the source text and the CodeUnit list of the literal to be known. - * @param {Character} char Character to check. - * @param {string} nodeSource Source text of the string or template literal that produces the character. - * @param {CodeUnit[]} codeUnits List of CodeUnit objects of the literal that produces the character. - * @returns {boolean} Whether the specified regexpp character is represented as an acceptable escape sequence. - */ -function checkForAcceptableEscapeInString(char, nodeSource, codeUnits) { - const firstIndex = char.start; - const lastIndex = char.end - 1; - const start = codeUnits[firstIndex].start; - const end = codeUnits[lastIndex].end; - const charSource = nodeSource.slice(start, end); - - return checkForAcceptableEscape(char, charSource); -} +const kinds = Object.keys(hasCharacterSequence); //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - defaultOptions: [ - { - allowEscape: false, - }, - ], - - docs: { - description: - "Disallow characters which are made with multiple code points in character class syntax", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-misleading-character-class", - }, - - hasSuggestions: true, - - schema: [ - { - type: "object", - properties: { - allowEscape: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - - messages: { - surrogatePairWithoutUFlag: - "Unexpected surrogate pair in character class. Use 'u' flag.", - surrogatePair: "Unexpected surrogate pair in character class.", - combiningClass: "Unexpected combined character in character class.", - emojiModifier: "Unexpected modified Emoji in character class.", - regionalIndicatorSymbol: - "Unexpected national flag in character class.", - zwj: "Unexpected joined character sequence in character class.", - suggestUnicodeFlag: "Add unicode 'u' flag to regex.", - }, - }, - create(context) { - const [{ allowEscape }] = context.options; - const sourceCode = context.sourceCode; - const parser = new RegExpParser(); - const checkedPatternNodes = new Set(); - - /** - * Verify a given regular expression. - * @param {Node} node The node to report. - * @param {string} pattern The regular expression pattern to verify. - * @param {string} flags The flags of the regular expression. - * @param {Function} unicodeFixer Fixer for missing "u" flag. - * @returns {void} - */ - function verify(node, pattern, flags, unicodeFixer) { - let patternNode; - - try { - patternNode = parser.parsePattern(pattern, 0, pattern.length, { - unicode: flags.includes("u"), - unicodeSets: flags.includes("v"), - }); - } catch { - // Ignore regular expressions with syntax errors - return; - } - - let codeUnits = null; - - /** - * Checks whether a specified regexpp character is represented as an acceptable escape sequence. - * For the purposes of this rule, an escape sequence is considered acceptable if it consists of one or more backslashes followed by the character being escaped. - * @param {Character} char Character to check. - * @returns {boolean} Whether the specified regexpp character is represented as an acceptable escape sequence. - */ - function isAcceptableEscapeSequence(char) { - if (node.type === "Literal" && node.regex) { - return checkForAcceptableEscape(char, char.raw); - } - if (node.type === "Literal" && typeof node.value === "string") { - const nodeSource = node.raw; - - codeUnits ??= parseStringLiteral(nodeSource); - - return checkForAcceptableEscapeInString( - char, - nodeSource, - codeUnits, - ); - } - if (astUtils.isStaticTemplateLiteral(node)) { - const nodeSource = sourceCode.getText(node); - - codeUnits ??= parseTemplateToken(nodeSource); - - return checkForAcceptableEscapeInString( - char, - nodeSource, - codeUnits, - ); - } - return false; - } - - const foundKindMatches = new Map(); - - visitRegExpAST(patternNode, { - onCharacterClassEnter(ccNode) { - for (const unfilteredChars of iterateCharacterSequence( - ccNode.elements, - )) { - let chars; - - if (allowEscape) { - // Replace escape sequences with null to avoid having them flagged. - chars = unfilteredChars.map(char => - isAcceptableEscapeSequence(char) ? null : char, - ); - } else { - chars = unfilteredChars; - } - for (const kind of kinds) { - const matches = findCharacterSequences[kind]( - chars, - unfilteredChars, - ); - - if (foundKindMatches.has(kind)) { - foundKindMatches.get(kind).push(...matches); - } else { - foundKindMatches.set(kind, [...matches]); - } - } - } - }, - }); - - /** - * Finds the report loc(s) for a range of matches. - * Only literals and expression-less templates generate granular errors. - * @param {Character[][]} matches Lists of individual characters being reported on. - * @returns {Location[]} locs for context.report. - * @see https://github.com/eslint/eslint/pull/17515 - */ - function getNodeReportLocations(matches) { - if ( - !astUtils.isStaticTemplateLiteral(node) && - node.type !== "Literal" - ) { - return matches.length ? [node.loc] : []; - } - return matches.map(chars => { - const firstIndex = chars[0].start; - const lastIndex = chars.at(-1).end - 1; - let start; - let end; - - if (node.type === "TemplateLiteral") { - const source = sourceCode.getText(node); - const offset = node.range[0]; - - codeUnits ??= parseTemplateToken(source); - start = offset + codeUnits[firstIndex].start; - end = offset + codeUnits[lastIndex].end; - } else if (typeof node.value === "string") { - // String Literal - const source = node.raw; - const offset = node.range[0]; - - codeUnits ??= parseStringLiteral(source); - start = offset + codeUnits[firstIndex].start; - end = offset + codeUnits[lastIndex].end; - } else { - // RegExp Literal - const offset = node.range[0] + 1; // Add 1 to skip the leading slash. - - start = offset + firstIndex; - end = offset + lastIndex + 1; - } - - return { - start: sourceCode.getLocFromIndex(start), - end: sourceCode.getLocFromIndex(end), - }; - }); - } - - for (const [kind, matches] of foundKindMatches) { - let suggest; - - if (kind === "surrogatePairWithoutUFlag") { - suggest = [ - { - messageId: "suggestUnicodeFlag", - fix: unicodeFixer, - }, - ]; - } - - const locs = getNodeReportLocations(matches); - - for (const loc of locs) { - context.report({ - node, - loc, - messageId: kind, - suggest, - }); - } - } - } - - return { - "Literal[regex]"(node) { - if (checkedPatternNodes.has(node)) { - return; - } - verify(node, node.regex.pattern, node.regex.flags, fixer => { - if ( - !isValidWithUnicodeFlag( - context.languageOptions.ecmaVersion, - node.regex.pattern, - ) - ) { - return null; - } - - return fixer.insertTextAfter(node, "u"); - }); - }, - Program(node) { - const scope = sourceCode.getScope(node); - const tracker = new ReferenceTracker(scope); - - /* - * Iterate calls of RegExp. - * E.g., `new RegExp()`, `RegExp()`, `new window.RegExp()`, - * `const {RegExp: a} = window; new a()`, etc... - */ - for (const { node: refNode } of tracker.iterateGlobalReferences( - { - RegExp: { [CALL]: true, [CONSTRUCT]: true }, - }, - )) { - let pattern, flags; - const [patternNode, flagsNode] = refNode.arguments; - const evaluatedPattern = getStaticValueOrRegex( - patternNode, - scope, - ); - - if (!evaluatedPattern) { - continue; - } - if (flagsNode) { - if (evaluatedPattern.regex) { - pattern = evaluatedPattern.regex.pattern; - checkedPatternNodes.add(patternNode); - } else { - pattern = String(evaluatedPattern.value); - } - flags = getStringIfConstant(flagsNode, scope); - } else { - if (evaluatedPattern.regex) { - continue; - } - pattern = String(evaluatedPattern.value); - flags = ""; - } - - if (typeof flags === "string") { - verify(patternNode, pattern, flags, fixer => { - if ( - !isValidWithUnicodeFlag( - context.languageOptions.ecmaVersion, - pattern, - ) - ) { - return null; - } - - if (refNode.arguments.length === 1) { - const penultimateToken = - sourceCode.getLastToken(refNode, { - skip: 1, - }); // skip closing parenthesis - - return fixer.insertTextAfter( - penultimateToken, - astUtils.isCommaToken(penultimateToken) - ? ' "u",' - : ', "u"', - ); - } - - if ( - (flagsNode.type === "Literal" && - typeof flagsNode.value === "string") || - flagsNode.type === "TemplateLiteral" - ) { - const range = [ - flagsNode.range[0], - flagsNode.range[1] - 1, - ]; - - return fixer.insertTextAfterRange(range, "u"); - } - - return null; - }); - } - } - }, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow characters which are made with multiple code points in character class syntax", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-misleading-character-class" + }, + + hasSuggestions: true, + + schema: [], + + messages: { + surrogatePairWithoutUFlag: "Unexpected surrogate pair in character class. Use 'u' flag.", + surrogatePair: "Unexpected surrogate pair in character class.", + combiningClass: "Unexpected combined character in character class.", + emojiModifier: "Unexpected modified Emoji in character class.", + regionalIndicatorSymbol: "Unexpected national flag in character class.", + zwj: "Unexpected joined character sequence in character class.", + suggestUnicodeFlag: "Add unicode 'u' flag to regex." + } + }, + create(context) { + const sourceCode = context.sourceCode; + const parser = new RegExpParser(); + + /** + * Verify a given regular expression. + * @param {Node} node The node to report. + * @param {string} pattern The regular expression pattern to verify. + * @param {string} flags The flags of the regular expression. + * @param {Function} unicodeFixer Fixer for missing "u" flag. + * @returns {void} + */ + function verify(node, pattern, flags, unicodeFixer) { + let patternNode; + + try { + patternNode = parser.parsePattern( + pattern, + 0, + pattern.length, + { + unicode: flags.includes("u"), + unicodeSets: flags.includes("v") + } + ); + } catch { + + // Ignore regular expressions with syntax errors + return; + } + + const foundKinds = new Set(); + + visitRegExpAST(patternNode, { + onCharacterClassEnter(ccNode) { + for (const chars of iterateCharacterSequence(ccNode.elements)) { + for (const kind of kinds) { + if (hasCharacterSequence[kind](chars)) { + foundKinds.add(kind); + } + } + } + } + }); + + for (const kind of foundKinds) { + let suggest; + + if (kind === "surrogatePairWithoutUFlag") { + suggest = [{ + messageId: "suggestUnicodeFlag", + fix: unicodeFixer + }]; + } + + context.report({ + node, + messageId: kind, + suggest + }); + } + } + + return { + "Literal[regex]"(node) { + verify(node, node.regex.pattern, node.regex.flags, fixer => { + if (!isValidWithUnicodeFlag(context.languageOptions.ecmaVersion, node.regex.pattern)) { + return null; + } + + return fixer.insertTextAfter(node, "u"); + }); + }, + "Program"(node) { + const scope = sourceCode.getScope(node); + const tracker = new ReferenceTracker(scope); + + /* + * Iterate calls of RegExp. + * E.g., `new RegExp()`, `RegExp()`, `new window.RegExp()`, + * `const {RegExp: a} = window; new a()`, etc... + */ + for (const { node: refNode } of tracker.iterateGlobalReferences({ + RegExp: { [CALL]: true, [CONSTRUCT]: true } + })) { + const [patternNode, flagsNode] = refNode.arguments; + const pattern = getStringIfConstant(patternNode, scope); + const flags = getStringIfConstant(flagsNode, scope); + + if (typeof pattern === "string") { + verify(refNode, pattern, flags || "", fixer => { + + if (!isValidWithUnicodeFlag(context.languageOptions.ecmaVersion, pattern)) { + return null; + } + + if (refNode.arguments.length === 1) { + const penultimateToken = sourceCode.getLastToken(refNode, { skip: 1 }); // skip closing parenthesis + + return fixer.insertTextAfter( + penultimateToken, + astUtils.isCommaToken(penultimateToken) + ? ' "u",' + : ', "u"' + ); + } + + if ((flagsNode.type === "Literal" && typeof flagsNode.value === "string") || flagsNode.type === "TemplateLiteral") { + const range = [flagsNode.range[0], flagsNode.range[1] - 1]; + + return fixer.insertTextAfterRange(range, "u"); + } + + return null; + }); + } + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-mixed-operators.js b/node_modules/eslint/lib/rules/no-mixed-operators.js index 73dc127a4..6b6f7364a 100644 --- a/node_modules/eslint/lib/rules/no-mixed-operators.js +++ b/node_modules/eslint/lib/rules/no-mixed-operators.js @@ -24,20 +24,20 @@ const RELATIONAL_OPERATORS = ["in", "instanceof"]; const TERNARY_OPERATOR = ["?:"]; const COALESCE_OPERATOR = ["??"]; const ALL_OPERATORS = [].concat( - ARITHMETIC_OPERATORS, - BITWISE_OPERATORS, - COMPARISON_OPERATORS, - LOGICAL_OPERATORS, - RELATIONAL_OPERATORS, - TERNARY_OPERATOR, - COALESCE_OPERATOR, + ARITHMETIC_OPERATORS, + BITWISE_OPERATORS, + COMPARISON_OPERATORS, + LOGICAL_OPERATORS, + RELATIONAL_OPERATORS, + TERNARY_OPERATOR, + COALESCE_OPERATOR ); const DEFAULT_GROUPS = [ - ARITHMETIC_OPERATORS, - BITWISE_OPERATORS, - COMPARISON_OPERATORS, - LOGICAL_OPERATORS, - RELATIONAL_OPERATORS, + ARITHMETIC_OPERATORS, + BITWISE_OPERATORS, + COMPARISON_OPERATORS, + LOGICAL_OPERATORS, + RELATIONAL_OPERATORS ]; const TARGET_NODE_TYPE = /^(?:Binary|Logical|Conditional)Expression$/u; @@ -47,14 +47,14 @@ const TARGET_NODE_TYPE = /^(?:Binary|Logical|Conditional)Expression$/u; * @returns {Object} Normalized option object. */ function normalizeOptions(options = {}) { - const hasGroups = options.groups && options.groups.length > 0; - const groups = hasGroups ? options.groups : DEFAULT_GROUPS; - const allowSamePrecedence = options.allowSamePrecedence !== false; - - return { - groups, - allowSamePrecedence, - }; + const hasGroups = options.groups && options.groups.length > 0; + const groups = hasGroups ? options.groups : DEFAULT_GROUPS; + const allowSamePrecedence = options.allowSamePrecedence !== false; + + return { + groups, + allowSamePrecedence + }; } /** @@ -65,7 +65,7 @@ function normalizeOptions(options = {}) { * @returns {boolean} `true` if such group existed. */ function includesBothInAGroup(groups, left, right) { - return groups.some(group => group.includes(left) && group.includes(right)); + return groups.some(group => group.includes(left) && group.includes(right)); } /** @@ -76,178 +76,154 @@ function includesBothInAGroup(groups, left, right) { * @returns {ASTNode} node the appropriate node(left or test). */ function getChildNode(node) { - return node.type === "ConditionalExpression" ? node.test : node.left; + return node.type === "ConditionalExpression" ? node.test : node.left; } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "no-mixed-operators", - url: "https://eslint.style/rules/no-mixed-operators", - }, - }, - ], - }, - type: "suggestion", - - docs: { - description: "Disallow mixed binary operators", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-mixed-operators", - }, - - schema: [ - { - type: "object", - properties: { - groups: { - type: "array", - items: { - type: "array", - items: { enum: ALL_OPERATORS }, - minItems: 2, - uniqueItems: true, - }, - uniqueItems: true, - }, - allowSamePrecedence: { - type: "boolean", - default: true, - }, - }, - additionalProperties: false, - }, - ], - - messages: { - unexpectedMixedOperator: - "Unexpected mix of '{{leftOperator}}' and '{{rightOperator}}'. Use parentheses to clarify the intended order of operations.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - const options = normalizeOptions(context.options[0]); - - /** - * Checks whether a given node should be ignored by options or not. - * @param {ASTNode} node A node to check. This is a BinaryExpression - * node or a LogicalExpression node. This parent node is one of - * them, too. - * @returns {boolean} `true` if the node should be ignored. - */ - function shouldIgnore(node) { - const a = node; - const b = node.parent; - - return ( - !includesBothInAGroup( - options.groups, - a.operator, - b.type === "ConditionalExpression" ? "?:" : b.operator, - ) || - (options.allowSamePrecedence && - astUtils.getPrecedence(a) === astUtils.getPrecedence(b)) - ); - } - - /** - * Checks whether the operator of a given node is mixed with parent - * node's operator or not. - * @param {ASTNode} node A node to check. This is a BinaryExpression - * node or a LogicalExpression node. This parent node is one of - * them, too. - * @returns {boolean} `true` if the node was mixed. - */ - function isMixedWithParent(node) { - return ( - node.operator !== node.parent.operator && - !astUtils.isParenthesised(sourceCode, node) - ); - } - - /** - * Gets the operator token of a given node. - * @param {ASTNode} node A node to check. This is a BinaryExpression - * node or a LogicalExpression node. - * @returns {Token} The operator token of the node. - */ - function getOperatorToken(node) { - return sourceCode.getTokenAfter( - getChildNode(node), - astUtils.isNotClosingParenToken, - ); - } - - /** - * Reports both the operator of a given node and the operator of the - * parent node. - * @param {ASTNode} node A node to check. This is a BinaryExpression - * node or a LogicalExpression node. This parent node is one of - * them, too. - * @returns {void} - */ - function reportBothOperators(node) { - const parent = node.parent; - const left = getChildNode(parent) === node ? node : parent; - const right = getChildNode(parent) !== node ? node : parent; - const data = { - leftOperator: left.operator || "?:", - rightOperator: right.operator || "?:", - }; - - context.report({ - node: left, - loc: getOperatorToken(left).loc, - messageId: "unexpectedMixedOperator", - data, - }); - context.report({ - node: right, - loc: getOperatorToken(right).loc, - messageId: "unexpectedMixedOperator", - data, - }); - } - - /** - * Checks between the operator of this node and the operator of the - * parent node. - * @param {ASTNode} node A node to check. - * @returns {void} - */ - function check(node) { - if ( - TARGET_NODE_TYPE.test(node.parent.type) && - isMixedWithParent(node) && - !shouldIgnore(node) - ) { - reportBothOperators(node); - } - } - - return { - BinaryExpression: check, - LogicalExpression: check, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "suggestion", + + docs: { + description: "Disallow mixed binary operators", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-mixed-operators" + }, + + schema: [ + { + type: "object", + properties: { + groups: { + type: "array", + items: { + type: "array", + items: { enum: ALL_OPERATORS }, + minItems: 2, + uniqueItems: true + }, + uniqueItems: true + }, + allowSamePrecedence: { + type: "boolean", + default: true + } + }, + additionalProperties: false + } + ], + + messages: { + unexpectedMixedOperator: "Unexpected mix of '{{leftOperator}}' and '{{rightOperator}}'. Use parentheses to clarify the intended order of operations." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + const options = normalizeOptions(context.options[0]); + + /** + * Checks whether a given node should be ignored by options or not. + * @param {ASTNode} node A node to check. This is a BinaryExpression + * node or a LogicalExpression node. This parent node is one of + * them, too. + * @returns {boolean} `true` if the node should be ignored. + */ + function shouldIgnore(node) { + const a = node; + const b = node.parent; + + return ( + !includesBothInAGroup(options.groups, a.operator, b.type === "ConditionalExpression" ? "?:" : b.operator) || + ( + options.allowSamePrecedence && + astUtils.getPrecedence(a) === astUtils.getPrecedence(b) + ) + ); + } + + /** + * Checks whether the operator of a given node is mixed with parent + * node's operator or not. + * @param {ASTNode} node A node to check. This is a BinaryExpression + * node or a LogicalExpression node. This parent node is one of + * them, too. + * @returns {boolean} `true` if the node was mixed. + */ + function isMixedWithParent(node) { + + return ( + node.operator !== node.parent.operator && + !astUtils.isParenthesised(sourceCode, node) + ); + } + + /** + * Gets the operator token of a given node. + * @param {ASTNode} node A node to check. This is a BinaryExpression + * node or a LogicalExpression node. + * @returns {Token} The operator token of the node. + */ + function getOperatorToken(node) { + return sourceCode.getTokenAfter(getChildNode(node), astUtils.isNotClosingParenToken); + } + + /** + * Reports both the operator of a given node and the operator of the + * parent node. + * @param {ASTNode} node A node to check. This is a BinaryExpression + * node or a LogicalExpression node. This parent node is one of + * them, too. + * @returns {void} + */ + function reportBothOperators(node) { + const parent = node.parent; + const left = (getChildNode(parent) === node) ? node : parent; + const right = (getChildNode(parent) !== node) ? node : parent; + const data = { + leftOperator: left.operator || "?:", + rightOperator: right.operator || "?:" + }; + + context.report({ + node: left, + loc: getOperatorToken(left).loc, + messageId: "unexpectedMixedOperator", + data + }); + context.report({ + node: right, + loc: getOperatorToken(right).loc, + messageId: "unexpectedMixedOperator", + data + }); + } + + /** + * Checks between the operator of this node and the operator of the + * parent node. + * @param {ASTNode} node A node to check. + * @returns {void} + */ + function check(node) { + if ( + TARGET_NODE_TYPE.test(node.parent.type) && + isMixedWithParent(node) && + !shouldIgnore(node) + ) { + reportBothOperators(node); + } + } + + return { + BinaryExpression: check, + LogicalExpression: check + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-mixed-requires.js b/node_modules/eslint/lib/rules/no-mixed-requires.js index 0d8bb5586..9e7b80390 100644 --- a/node_modules/eslint/lib/rules/no-mixed-requires.js +++ b/node_modules/eslint/lib/rules/no-mixed-requires.js @@ -10,258 +10,229 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Node.js rules were moved out of ESLint core.", - url: "https://eslint.org/docs/latest/use/migrating-to-7.0.0#deprecate-node-rules", - deprecatedSince: "7.0.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "eslint-plugin-n now maintains deprecated Node.js-related rules.", - plugin: { - name: "eslint-plugin-n", - url: "https://github.com/eslint-community/eslint-plugin-n", - }, - rule: { - name: "no-mixed-requires", - url: "https://github.com/eslint-community/eslint-plugin-n/tree/master/docs/rules/no-mixed-requires.md", - }, - }, - ], - }, - - type: "suggestion", - - docs: { - description: - "Disallow `require` calls to be mixed with regular variable declarations", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-mixed-requires", - }, - - schema: [ - { - oneOf: [ - { - type: "boolean", - }, - { - type: "object", - properties: { - grouping: { - type: "boolean", - }, - allowCall: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - }, - ], - - messages: { - noMixRequire: "Do not mix 'require' and other declarations.", - noMixCoreModuleFileComputed: - "Do not mix core, module, file and computed requires.", - }, - }, - - create(context) { - const options = context.options[0]; - let grouping = false, - allowCall = false; - - if (typeof options === "object") { - grouping = options.grouping; - allowCall = options.allowCall; - } else { - grouping = !!options; - } - - /** - * Returns the list of built-in modules. - * @returns {string[]} An array of built-in Node.js modules. - */ - function getBuiltinModules() { - /* - * This list is generated using: - * `require("repl")._builtinLibs.concat('repl').sort()` - * This particular list is as per nodejs v0.12.2 and iojs v0.7.1 - */ - return [ - "assert", - "buffer", - "child_process", - "cluster", - "crypto", - "dgram", - "dns", - "domain", - "events", - "fs", - "http", - "https", - "net", - "os", - "path", - "punycode", - "querystring", - "readline", - "repl", - "smalloc", - "stream", - "string_decoder", - "tls", - "tty", - "url", - "util", - "v8", - "vm", - "zlib", - ]; - } - - const BUILTIN_MODULES = getBuiltinModules(); - - const DECL_REQUIRE = "require", - DECL_UNINITIALIZED = "uninitialized", - DECL_OTHER = "other"; - - const REQ_CORE = "core", - REQ_FILE = "file", - REQ_MODULE = "module", - REQ_COMPUTED = "computed"; - - /** - * Determines the type of a declaration statement. - * @param {ASTNode} initExpression The init node of the VariableDeclarator. - * @returns {string} The type of declaration represented by the expression. - */ - function getDeclarationType(initExpression) { - if (!initExpression) { - // "var x;" - return DECL_UNINITIALIZED; - } - - if ( - initExpression.type === "CallExpression" && - initExpression.callee.type === "Identifier" && - initExpression.callee.name === "require" - ) { - // "var x = require('util');" - return DECL_REQUIRE; - } - if ( - allowCall && - initExpression.type === "CallExpression" && - initExpression.callee.type === "CallExpression" - ) { - // "var x = require('diagnose')('sub-module');" - return getDeclarationType(initExpression.callee); - } - if (initExpression.type === "MemberExpression") { - // "var x = require('glob').Glob;" - return getDeclarationType(initExpression.object); - } - - // "var x = 42;" - return DECL_OTHER; - } - - /** - * Determines the type of module that is loaded via require. - * @param {ASTNode} initExpression The init node of the VariableDeclarator. - * @returns {string} The module type. - */ - function inferModuleType(initExpression) { - if (initExpression.type === "MemberExpression") { - // "var x = require('glob').Glob;" - return inferModuleType(initExpression.object); - } - if (initExpression.arguments.length === 0) { - // "var x = require();" - return REQ_COMPUTED; - } - - const arg = initExpression.arguments[0]; - - if (arg.type !== "Literal" || typeof arg.value !== "string") { - // "var x = require(42);" - return REQ_COMPUTED; - } - - if (BUILTIN_MODULES.includes(arg.value)) { - // "var fs = require('fs');" - return REQ_CORE; - } - if (/^\.{0,2}\//u.test(arg.value)) { - // "var utils = require('./utils');" - return REQ_FILE; - } - - // "var async = require('async');" - return REQ_MODULE; - } - - /** - * Check if the list of variable declarations is mixed, i.e. whether it - * contains both require and other declarations. - * @param {ASTNode} declarations The list of VariableDeclarators. - * @returns {boolean} True if the declarations are mixed, false if not. - */ - function isMixed(declarations) { - const contains = {}; - - declarations.forEach(declaration => { - const type = getDeclarationType(declaration.init); - - contains[type] = true; - }); - - return !!( - contains[DECL_REQUIRE] && - (contains[DECL_UNINITIALIZED] || contains[DECL_OTHER]) - ); - } - - /** - * Check if all require declarations in the given list are of the same - * type. - * @param {ASTNode} declarations The list of VariableDeclarators. - * @returns {boolean} True if the declarations are grouped, false if not. - */ - function isGrouped(declarations) { - const found = {}; - - declarations.forEach(declaration => { - if (getDeclarationType(declaration.init) === DECL_REQUIRE) { - found[inferModuleType(declaration.init)] = true; - } - }); - - return Object.keys(found).length <= 1; - } - - return { - VariableDeclaration(node) { - if (isMixed(node.declarations)) { - context.report({ - node, - messageId: "noMixRequire", - }); - } else if (grouping && !isGrouped(node.declarations)) { - context.report({ - node, - messageId: "noMixCoreModuleFileComputed", - }); - } - }, - }; - }, + meta: { + deprecated: true, + + replacedBy: [], + + type: "suggestion", + + docs: { + description: "Disallow `require` calls to be mixed with regular variable declarations", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-mixed-requires" + }, + + schema: [ + { + oneOf: [ + { + type: "boolean" + }, + { + type: "object", + properties: { + grouping: { + type: "boolean" + }, + allowCall: { + type: "boolean" + } + }, + additionalProperties: false + } + ] + } + ], + + messages: { + noMixRequire: "Do not mix 'require' and other declarations.", + noMixCoreModuleFileComputed: "Do not mix core, module, file and computed requires." + } + }, + + create(context) { + + const options = context.options[0]; + let grouping = false, + allowCall = false; + + if (typeof options === "object") { + grouping = options.grouping; + allowCall = options.allowCall; + } else { + grouping = !!options; + } + + /** + * Returns the list of built-in modules. + * @returns {string[]} An array of built-in Node.js modules. + */ + function getBuiltinModules() { + + /* + * This list is generated using: + * `require("repl")._builtinLibs.concat('repl').sort()` + * This particular list is as per nodejs v0.12.2 and iojs v0.7.1 + */ + return [ + "assert", "buffer", "child_process", "cluster", "crypto", + "dgram", "dns", "domain", "events", "fs", "http", "https", + "net", "os", "path", "punycode", "querystring", "readline", + "repl", "smalloc", "stream", "string_decoder", "tls", "tty", + "url", "util", "v8", "vm", "zlib" + ]; + } + + const BUILTIN_MODULES = getBuiltinModules(); + + const DECL_REQUIRE = "require", + DECL_UNINITIALIZED = "uninitialized", + DECL_OTHER = "other"; + + const REQ_CORE = "core", + REQ_FILE = "file", + REQ_MODULE = "module", + REQ_COMPUTED = "computed"; + + /** + * Determines the type of a declaration statement. + * @param {ASTNode} initExpression The init node of the VariableDeclarator. + * @returns {string} The type of declaration represented by the expression. + */ + function getDeclarationType(initExpression) { + if (!initExpression) { + + // "var x;" + return DECL_UNINITIALIZED; + } + + if (initExpression.type === "CallExpression" && + initExpression.callee.type === "Identifier" && + initExpression.callee.name === "require" + ) { + + // "var x = require('util');" + return DECL_REQUIRE; + } + if (allowCall && + initExpression.type === "CallExpression" && + initExpression.callee.type === "CallExpression" + ) { + + // "var x = require('diagnose')('sub-module');" + return getDeclarationType(initExpression.callee); + } + if (initExpression.type === "MemberExpression") { + + // "var x = require('glob').Glob;" + return getDeclarationType(initExpression.object); + } + + // "var x = 42;" + return DECL_OTHER; + } + + /** + * Determines the type of module that is loaded via require. + * @param {ASTNode} initExpression The init node of the VariableDeclarator. + * @returns {string} The module type. + */ + function inferModuleType(initExpression) { + if (initExpression.type === "MemberExpression") { + + // "var x = require('glob').Glob;" + return inferModuleType(initExpression.object); + } + if (initExpression.arguments.length === 0) { + + // "var x = require();" + return REQ_COMPUTED; + } + + const arg = initExpression.arguments[0]; + + if (arg.type !== "Literal" || typeof arg.value !== "string") { + + // "var x = require(42);" + return REQ_COMPUTED; + } + + if (BUILTIN_MODULES.includes(arg.value)) { + + // "var fs = require('fs');" + return REQ_CORE; + } + if (/^\.{0,2}\//u.test(arg.value)) { + + // "var utils = require('./utils');" + return REQ_FILE; + } + + // "var async = require('async');" + return REQ_MODULE; + + } + + /** + * Check if the list of variable declarations is mixed, i.e. whether it + * contains both require and other declarations. + * @param {ASTNode} declarations The list of VariableDeclarators. + * @returns {boolean} True if the declarations are mixed, false if not. + */ + function isMixed(declarations) { + const contains = {}; + + declarations.forEach(declaration => { + const type = getDeclarationType(declaration.init); + + contains[type] = true; + }); + + return !!( + contains[DECL_REQUIRE] && + (contains[DECL_UNINITIALIZED] || contains[DECL_OTHER]) + ); + } + + /** + * Check if all require declarations in the given list are of the same + * type. + * @param {ASTNode} declarations The list of VariableDeclarators. + * @returns {boolean} True if the declarations are grouped, false if not. + */ + function isGrouped(declarations) { + const found = {}; + + declarations.forEach(declaration => { + if (getDeclarationType(declaration.init) === DECL_REQUIRE) { + found[inferModuleType(declaration.init)] = true; + } + }); + + return Object.keys(found).length <= 1; + } + + + return { + + VariableDeclaration(node) { + + if (isMixed(node.declarations)) { + context.report({ + node, + messageId: "noMixRequire" + }); + } else if (grouping && !isGrouped(node.declarations)) { + context.report({ + node, + messageId: "noMixCoreModuleFileComputed" + }); + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-mixed-spaces-and-tabs.js b/node_modules/eslint/lib/rules/no-mixed-spaces-and-tabs.js index 5983d7cfd..7698b5da7 100644 --- a/node_modules/eslint/lib/rules/no-mixed-spaces-and-tabs.js +++ b/node_modules/eslint/lib/rules/no-mixed-spaces-and-tabs.js @@ -9,140 +9,108 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "no-mixed-spaces-and-tabs", - url: "https://eslint.style/rules/no-mixed-spaces-and-tabs", - }, - }, - ], - }, - type: "layout", - - docs: { - description: "Disallow mixed spaces and tabs for indentation", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-mixed-spaces-and-tabs", - }, - - schema: [ - { - enum: ["smart-tabs", true, false], - }, - ], - - messages: { - mixedSpacesAndTabs: "Mixed spaces and tabs.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - let smartTabs; - - switch (context.options[0]) { - case true: // Support old syntax, maybe add deprecation warning here - case "smart-tabs": - smartTabs = true; - break; - default: - smartTabs = false; - } - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - return { - "Program:exit"(node) { - const lines = sourceCode.lines, - comments = sourceCode.getAllComments(), - ignoredCommentLines = new Set(); - - // Add all lines except the first ones. - comments.forEach(comment => { - for ( - let i = comment.loc.start.line + 1; - i <= comment.loc.end.line; - i++ - ) { - ignoredCommentLines.add(i); - } - }); - - /* - * At least one space followed by a tab - * or the reverse before non-tab/-space - * characters begin. - */ - let regex = /^(?=( +|\t+))\1(?:\t| )/u; - - if (smartTabs) { - /* - * At least one space followed by a tab - * before non-tab/-space characters begin. - */ - // eslint-disable-next-line regexp/no-empty-lookarounds-assertion -- False positive - regex = /^(?=(\t*))\1(?=( +))\2\t/u; - } - - lines.forEach((line, i) => { - const match = regex.exec(line); - - if (match) { - const lineNumber = i + 1; - const loc = { - start: { - line: lineNumber, - column: match[0].length - 2, - }, - end: { - line: lineNumber, - column: match[0].length, - }, - }; - - if (!ignoredCommentLines.has(lineNumber)) { - const containingNode = - sourceCode.getNodeByRangeIndex( - sourceCode.getIndexFromLoc(loc.start), - ); - - if ( - !( - containingNode && - ["Literal", "TemplateElement"].includes( - containingNode.type, - ) - ) - ) { - context.report({ - node, - loc, - messageId: "mixedSpacesAndTabs", - }); - } - } - } - }); - }, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Disallow mixed spaces and tabs for indentation", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-mixed-spaces-and-tabs" + }, + + schema: [ + { + enum: ["smart-tabs", true, false] + } + ], + + messages: { + mixedSpacesAndTabs: "Mixed spaces and tabs." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + + let smartTabs; + + switch (context.options[0]) { + case true: // Support old syntax, maybe add deprecation warning here + case "smart-tabs": + smartTabs = true; + break; + default: + smartTabs = false; + } + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + return { + + "Program:exit"(node) { + const lines = sourceCode.lines, + comments = sourceCode.getAllComments(), + ignoredCommentLines = new Set(); + + // Add all lines except the first ones. + comments.forEach(comment => { + for (let i = comment.loc.start.line + 1; i <= comment.loc.end.line; i++) { + ignoredCommentLines.add(i); + } + }); + + /* + * At least one space followed by a tab + * or the reverse before non-tab/-space + * characters begin. + */ + let regex = /^(?=( +|\t+))\1(?:\t| )/u; + + if (smartTabs) { + + /* + * At least one space followed by a tab + * before non-tab/-space characters begin. + */ + regex = /^(?=(\t*))\1(?=( +))\2\t/u; + } + + lines.forEach((line, i) => { + const match = regex.exec(line); + + if (match) { + const lineNumber = i + 1; + const loc = { + start: { + line: lineNumber, + column: match[0].length - 2 + }, + end: { + line: lineNumber, + column: match[0].length + } + }; + + if (!ignoredCommentLines.has(lineNumber)) { + const containingNode = sourceCode.getNodeByRangeIndex(sourceCode.getIndexFromLoc(loc.start)); + + if (!(containingNode && ["Literal", "TemplateElement"].includes(containingNode.type))) { + context.report({ + node, + loc, + messageId: "mixedSpacesAndTabs" + }); + } + } + } + }); + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-multi-assign.js b/node_modules/eslint/lib/rules/no-multi-assign.js index b4d6ddb4e..a7a50c194 100644 --- a/node_modules/eslint/lib/rules/no-multi-assign.js +++ b/node_modules/eslint/lib/rules/no-multi-assign.js @@ -5,62 +5,63 @@ "use strict"; + //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", + meta: { + type: "suggestion", + + docs: { + description: "Disallow use of chained assignment expressions", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-multi-assign" + }, - defaultOptions: [ - { - ignoreNonDeclaration: false, - }, - ], + schema: [{ + type: "object", + properties: { + ignoreNonDeclaration: { + type: "boolean", + default: false + } + }, + additionalProperties: false + }], - docs: { - description: "Disallow use of chained assignment expressions", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-multi-assign", - }, + messages: { + unexpectedChain: "Unexpected chained assignment." + } + }, - schema: [ - { - type: "object", - properties: { - ignoreNonDeclaration: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], + create(context) { - messages: { - unexpectedChain: "Unexpected chained assignment.", - }, - }, + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + const options = context.options[0] || { + ignoreNonDeclaration: false + }; + const selectors = [ + "VariableDeclarator > AssignmentExpression.init", + "PropertyDefinition > AssignmentExpression.value" + ]; - create(context) { - const [{ ignoreNonDeclaration }] = context.options; - const selectors = [ - "VariableDeclarator > AssignmentExpression.init", - "PropertyDefinition > AssignmentExpression.value", - ]; + if (!options.ignoreNonDeclaration) { + selectors.push("AssignmentExpression > AssignmentExpression.right"); + } - if (!ignoreNonDeclaration) { - selectors.push("AssignmentExpression > AssignmentExpression.right"); - } + return { + [selectors](node) { + context.report({ + node, + messageId: "unexpectedChain" + }); + } + }; - return { - [selectors](node) { - context.report({ - node, - messageId: "unexpectedChain", - }); - }, - }; - }, + } }; diff --git a/node_modules/eslint/lib/rules/no-multi-spaces.js b/node_modules/eslint/lib/rules/no-multi-spaces.js index 93704760a..bc90ee5b5 100644 --- a/node_modules/eslint/lib/rules/no-multi-spaces.js +++ b/node_modules/eslint/lib/rules/no-multi-spaces.js @@ -12,168 +12,130 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "no-multi-spaces", - url: "https://eslint.style/rules/no-multi-spaces", - }, - }, - ], - }, - type: "layout", - - docs: { - description: "Disallow multiple spaces", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-multi-spaces", - }, - - fixable: "whitespace", - - schema: [ - { - type: "object", - properties: { - exceptions: { - type: "object", - patternProperties: { - "^([A-Z][a-z]*)+$": { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ignoreEOLComments: { - type: "boolean", - default: false, - }, - }, - additionalProperties: false, - }, - ], - - messages: { - multipleSpaces: "Multiple spaces found before '{{displayValue}}'.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - const options = context.options[0] || {}; - const ignoreEOLComments = options.ignoreEOLComments; - const exceptions = Object.assign( - { Property: true }, - options.exceptions, - ); - const hasExceptions = Object.keys(exceptions).some( - key => exceptions[key], - ); - - /** - * Formats value of given comment token for error message by truncating its length. - * @param {Token} token comment token - * @returns {string} formatted value - * @private - */ - function formatReportedCommentValue(token) { - const valueLines = token.value.split("\n"); - const value = valueLines[0]; - const formattedValue = `${value.slice(0, 12)}...`; - - return valueLines.length === 1 && value.length <= 12 - ? value - : formattedValue; - } - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - return { - Program() { - sourceCode.tokensAndComments.forEach( - (leftToken, leftIndex, tokensAndComments) => { - if (leftIndex === tokensAndComments.length - 1) { - return; - } - const rightToken = tokensAndComments[leftIndex + 1]; - - // Ignore tokens that don't have 2 spaces between them or are on different lines - if ( - !sourceCode.text - .slice(leftToken.range[1], rightToken.range[0]) - .includes(" ") || - leftToken.loc.end.line < rightToken.loc.start.line - ) { - return; - } - - // Ignore comments that are the last token on their line if `ignoreEOLComments` is active. - if ( - ignoreEOLComments && - astUtils.isCommentToken(rightToken) && - (leftIndex === tokensAndComments.length - 2 || - rightToken.loc.end.line < - tokensAndComments[leftIndex + 2].loc.start - .line) - ) { - return; - } - - // Ignore tokens that are in a node in the "exceptions" object - if (hasExceptions) { - const parentNode = sourceCode.getNodeByRangeIndex( - rightToken.range[0] - 1, - ); - - if (parentNode && exceptions[parentNode.type]) { - return; - } - } - - let displayValue; - - if (rightToken.type === "Block") { - displayValue = `/*${formatReportedCommentValue(rightToken)}*/`; - } else if (rightToken.type === "Line") { - displayValue = `//${formatReportedCommentValue(rightToken)}`; - } else { - displayValue = rightToken.value; - } - - context.report({ - node: rightToken, - loc: { - start: leftToken.loc.end, - end: rightToken.loc.start, - }, - messageId: "multipleSpaces", - data: { displayValue }, - fix: fixer => - fixer.replaceTextRange( - [leftToken.range[1], rightToken.range[0]], - " ", - ), - }); - }, - ); - }, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Disallow multiple spaces", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-multi-spaces" + }, + + fixable: "whitespace", + + schema: [ + { + type: "object", + properties: { + exceptions: { + type: "object", + patternProperties: { + "^([A-Z][a-z]*)+$": { + type: "boolean" + } + }, + additionalProperties: false + }, + ignoreEOLComments: { + type: "boolean", + default: false + } + }, + additionalProperties: false + } + ], + + messages: { + multipleSpaces: "Multiple spaces found before '{{displayValue}}'." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + const options = context.options[0] || {}; + const ignoreEOLComments = options.ignoreEOLComments; + const exceptions = Object.assign({ Property: true }, options.exceptions); + const hasExceptions = Object.keys(exceptions).some(key => exceptions[key]); + + /** + * Formats value of given comment token for error message by truncating its length. + * @param {Token} token comment token + * @returns {string} formatted value + * @private + */ + function formatReportedCommentValue(token) { + const valueLines = token.value.split("\n"); + const value = valueLines[0]; + const formattedValue = `${value.slice(0, 12)}...`; + + return valueLines.length === 1 && value.length <= 12 ? value : formattedValue; + } + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + return { + Program() { + sourceCode.tokensAndComments.forEach((leftToken, leftIndex, tokensAndComments) => { + if (leftIndex === tokensAndComments.length - 1) { + return; + } + const rightToken = tokensAndComments[leftIndex + 1]; + + // Ignore tokens that don't have 2 spaces between them or are on different lines + if ( + !sourceCode.text.slice(leftToken.range[1], rightToken.range[0]).includes(" ") || + leftToken.loc.end.line < rightToken.loc.start.line + ) { + return; + } + + // Ignore comments that are the last token on their line if `ignoreEOLComments` is active. + if ( + ignoreEOLComments && + astUtils.isCommentToken(rightToken) && + ( + leftIndex === tokensAndComments.length - 2 || + rightToken.loc.end.line < tokensAndComments[leftIndex + 2].loc.start.line + ) + ) { + return; + } + + // Ignore tokens that are in a node in the "exceptions" object + if (hasExceptions) { + const parentNode = sourceCode.getNodeByRangeIndex(rightToken.range[0] - 1); + + if (parentNode && exceptions[parentNode.type]) { + return; + } + } + + let displayValue; + + if (rightToken.type === "Block") { + displayValue = `/*${formatReportedCommentValue(rightToken)}*/`; + } else if (rightToken.type === "Line") { + displayValue = `//${formatReportedCommentValue(rightToken)}`; + } else { + displayValue = rightToken.value; + } + + context.report({ + node: rightToken, + loc: { start: leftToken.loc.end, end: rightToken.loc.start }, + messageId: "multipleSpaces", + data: { displayValue }, + fix: fixer => fixer.replaceTextRange([leftToken.range[1], rightToken.range[0]], " ") + }); + }); + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-multi-str.js b/node_modules/eslint/lib/rules/no-multi-str.js index 4258dc2e3..8011729ec 100644 --- a/node_modules/eslint/lib/rules/no-multi-str.js +++ b/node_modules/eslint/lib/rules/no-multi-str.js @@ -15,53 +15,51 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", + meta: { + type: "suggestion", - docs: { - description: "Disallow multiline strings", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/no-multi-str", - }, + docs: { + description: "Disallow multiline strings", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-multi-str" + }, - schema: [], + schema: [], - messages: { - multilineString: - "Multiline support is limited to browsers supporting ES5 only.", - }, - }, + messages: { + multilineString: "Multiline support is limited to browsers supporting ES5 only." + } + }, - create(context) { - /** - * Determines if a given node is part of JSX syntax. - * @param {ASTNode} node The node to check. - * @returns {boolean} True if the node is a JSX node, false if not. - * @private - */ - function isJSXElement(node) { - return node.type.indexOf("JSX") === 0; - } + create(context) { - //-------------------------------------------------------------------------- - // Public API - //-------------------------------------------------------------------------- + /** + * Determines if a given node is part of JSX syntax. + * @param {ASTNode} node The node to check. + * @returns {boolean} True if the node is a JSX node, false if not. + * @private + */ + function isJSXElement(node) { + return node.type.indexOf("JSX") === 0; + } - return { - Literal(node) { - if ( - astUtils.LINEBREAK_MATCHER.test(node.raw) && - !isJSXElement(node.parent) - ) { - context.report({ - node, - messageId: "multilineString", - }); - } - }, - }; - }, + //-------------------------------------------------------------------------- + // Public API + //-------------------------------------------------------------------------- + + return { + + Literal(node) { + if (astUtils.LINEBREAK_MATCHER.test(node.raw) && !isJSXElement(node.parent)) { + context.report({ + node, + messageId: "multilineString" + }); + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-multiple-empty-lines.js b/node_modules/eslint/lib/rules/no-multiple-empty-lines.js index 1f6d72647..5d038ff05 100644 --- a/node_modules/eslint/lib/rules/no-multiple-empty-lines.js +++ b/node_modules/eslint/lib/rules/no-multiple-empty-lines.js @@ -10,201 +10,145 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "no-multiple-empty-lines", - url: "https://eslint.style/rules/no-multiple-empty-lines", - }, - }, - ], - }, - type: "layout", - - docs: { - description: "Disallow multiple empty lines", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-multiple-empty-lines", - }, - - fixable: "whitespace", - - schema: [ - { - type: "object", - properties: { - max: { - type: "integer", - minimum: 0, - }, - maxEOF: { - type: "integer", - minimum: 0, - }, - maxBOF: { - type: "integer", - minimum: 0, - }, - }, - required: ["max"], - additionalProperties: false, - }, - ], - - messages: { - blankBeginningOfFile: - "Too many blank lines at the beginning of file. Max of {{max}} allowed.", - blankEndOfFile: - "Too many blank lines at the end of file. Max of {{max}} allowed.", - consecutiveBlank: - "More than {{max}} blank {{pluralizedLines}} not allowed.", - }, - }, - - create(context) { - // Use options.max or 2 as default - let max = 2, - maxEOF = max, - maxBOF = max; - - if (context.options.length) { - max = context.options[0].max; - maxEOF = - typeof context.options[0].maxEOF !== "undefined" - ? context.options[0].maxEOF - : max; - maxBOF = - typeof context.options[0].maxBOF !== "undefined" - ? context.options[0].maxBOF - : max; - } - - const sourceCode = context.sourceCode; - - // Swallow the final newline, as some editors add it automatically and we don't want it to cause an issue - const allLines = - sourceCode.lines.at(-1) === "" - ? sourceCode.lines.slice(0, -1) - : sourceCode.lines; - const templateLiteralLines = new Set(); - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - return { - TemplateLiteral(node) { - node.quasis.forEach(literalPart => { - // Empty lines have a semantic meaning if they're inside template literals. Don't count these as empty lines. - for ( - let ignoredLine = literalPart.loc.start.line; - ignoredLine < literalPart.loc.end.line; - ignoredLine++ - ) { - templateLiteralLines.add(ignoredLine); - } - }); - }, - "Program:exit"(node) { - return ( - allLines - - // Given a list of lines, first get a list of line numbers that are non-empty. - .reduce((nonEmptyLineNumbers, line, index) => { - if ( - line.trim() || - templateLiteralLines.has(index + 1) - ) { - nonEmptyLineNumbers.push(index + 1); - } - return nonEmptyLineNumbers; - }, []) - - // Add a value at the end to allow trailing empty lines to be checked. - .concat(allLines.length + 1) - - // Given two line numbers of non-empty lines, report the lines between if the difference is too large. - .reduce((lastLineNumber, lineNumber) => { - let messageId, maxAllowed; - - if (lastLineNumber === 0) { - messageId = "blankBeginningOfFile"; - maxAllowed = maxBOF; - } else if (lineNumber === allLines.length + 1) { - messageId = "blankEndOfFile"; - maxAllowed = maxEOF; - } else { - messageId = "consecutiveBlank"; - maxAllowed = max; - } - - if (lineNumber - lastLineNumber - 1 > maxAllowed) { - context.report({ - node, - loc: { - start: { - line: - lastLineNumber + maxAllowed + 1, - column: 0, - }, - end: { line: lineNumber, column: 0 }, - }, - messageId, - data: { - max: maxAllowed, - pluralizedLines: - maxAllowed === 1 ? "line" : "lines", - }, - fix(fixer) { - const rangeStart = - sourceCode.getIndexFromLoc({ - line: lastLineNumber + 1, - column: 0, - }); - - /* - * The end of the removal range is usually the start index of the next line. - * However, at the end of the file there is no next line, so the end of the - * range is just the length of the text. - */ - const lineNumberAfterRemovedLines = - lineNumber - maxAllowed; - const rangeEnd = - lineNumberAfterRemovedLines <= - allLines.length - ? sourceCode.getIndexFromLoc({ - line: lineNumberAfterRemovedLines, - column: 0, - }) - : sourceCode.text.length; - - return fixer.removeRange([ - rangeStart, - rangeEnd, - ]); - }, - }); - } - - return lineNumber; - }, 0) - ); - }, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Disallow multiple empty lines", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-multiple-empty-lines" + }, + + fixable: "whitespace", + + schema: [ + { + type: "object", + properties: { + max: { + type: "integer", + minimum: 0 + }, + maxEOF: { + type: "integer", + minimum: 0 + }, + maxBOF: { + type: "integer", + minimum: 0 + } + }, + required: ["max"], + additionalProperties: false + } + ], + + messages: { + blankBeginningOfFile: "Too many blank lines at the beginning of file. Max of {{max}} allowed.", + blankEndOfFile: "Too many blank lines at the end of file. Max of {{max}} allowed.", + consecutiveBlank: "More than {{max}} blank {{pluralizedLines}} not allowed." + } + }, + + create(context) { + + // Use options.max or 2 as default + let max = 2, + maxEOF = max, + maxBOF = max; + + if (context.options.length) { + max = context.options[0].max; + maxEOF = typeof context.options[0].maxEOF !== "undefined" ? context.options[0].maxEOF : max; + maxBOF = typeof context.options[0].maxBOF !== "undefined" ? context.options[0].maxBOF : max; + } + + const sourceCode = context.sourceCode; + + // Swallow the final newline, as some editors add it automatically and we don't want it to cause an issue + const allLines = sourceCode.lines[sourceCode.lines.length - 1] === "" ? sourceCode.lines.slice(0, -1) : sourceCode.lines; + const templateLiteralLines = new Set(); + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + return { + TemplateLiteral(node) { + node.quasis.forEach(literalPart => { + + // Empty lines have a semantic meaning if they're inside template literals. Don't count these as empty lines. + for (let ignoredLine = literalPart.loc.start.line; ignoredLine < literalPart.loc.end.line; ignoredLine++) { + templateLiteralLines.add(ignoredLine); + } + }); + }, + "Program:exit"(node) { + return allLines + + // Given a list of lines, first get a list of line numbers that are non-empty. + .reduce((nonEmptyLineNumbers, line, index) => { + if (line.trim() || templateLiteralLines.has(index + 1)) { + nonEmptyLineNumbers.push(index + 1); + } + return nonEmptyLineNumbers; + }, []) + + // Add a value at the end to allow trailing empty lines to be checked. + .concat(allLines.length + 1) + + // Given two line numbers of non-empty lines, report the lines between if the difference is too large. + .reduce((lastLineNumber, lineNumber) => { + let messageId, maxAllowed; + + if (lastLineNumber === 0) { + messageId = "blankBeginningOfFile"; + maxAllowed = maxBOF; + } else if (lineNumber === allLines.length + 1) { + messageId = "blankEndOfFile"; + maxAllowed = maxEOF; + } else { + messageId = "consecutiveBlank"; + maxAllowed = max; + } + + if (lineNumber - lastLineNumber - 1 > maxAllowed) { + context.report({ + node, + loc: { + start: { line: lastLineNumber + maxAllowed + 1, column: 0 }, + end: { line: lineNumber, column: 0 } + }, + messageId, + data: { + max: maxAllowed, + pluralizedLines: maxAllowed === 1 ? "line" : "lines" + }, + fix(fixer) { + const rangeStart = sourceCode.getIndexFromLoc({ line: lastLineNumber + 1, column: 0 }); + + /* + * The end of the removal range is usually the start index of the next line. + * However, at the end of the file there is no next line, so the end of the + * range is just the length of the text. + */ + const lineNumberAfterRemovedLines = lineNumber - maxAllowed; + const rangeEnd = lineNumberAfterRemovedLines <= allLines.length + ? sourceCode.getIndexFromLoc({ line: lineNumberAfterRemovedLines, column: 0 }) + : sourceCode.text.length; + + return fixer.removeRange([rangeStart, rangeEnd]); + } + }); + } + + return lineNumber; + }, 0); + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-native-reassign.js b/node_modules/eslint/lib/rules/no-native-reassign.js index f4eef0632..e3fed4451 100644 --- a/node_modules/eslint/lib/rules/no-native-reassign.js +++ b/node_modules/eslint/lib/rules/no-native-reassign.js @@ -10,105 +10,89 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", + meta: { + type: "suggestion", - docs: { - description: - "Disallow assignments to native objects or read-only global variables", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-native-reassign", - }, + docs: { + description: "Disallow assignments to native objects or read-only global variables", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-native-reassign" + }, - deprecated: { - message: "Renamed rule.", - url: "https://eslint.org/blog/2016/08/eslint-v3.3.0-released/#deprecated-rules", - deprecatedSince: "3.3.0", - availableUntil: "11.0.0", - replacedBy: [ - { - rule: { - name: "no-global-assign", - url: "https://eslint.org/docs/rules/no-global-assign", - }, - }, - ], - }, + deprecated: true, - schema: [ - { - type: "object", - properties: { - exceptions: { - type: "array", - items: { type: "string" }, - uniqueItems: true, - }, - }, - additionalProperties: false, - }, - ], + replacedBy: ["no-global-assign"], - messages: { - nativeReassign: - "Read-only global '{{name}}' should not be modified.", - }, - }, + schema: [ + { + type: "object", + properties: { + exceptions: { + type: "array", + items: { type: "string" }, + uniqueItems: true + } + }, + additionalProperties: false + } + ], - create(context) { - const config = context.options[0]; - const exceptions = (config && config.exceptions) || []; - const sourceCode = context.sourceCode; + messages: { + nativeReassign: "Read-only global '{{name}}' should not be modified." + } + }, - /** - * Reports write references. - * @param {Reference} reference A reference to check. - * @param {number} index The index of the reference in the references. - * @param {Reference[]} references The array that the reference belongs to. - * @returns {void} - */ - function checkReference(reference, index, references) { - const identifier = reference.identifier; + create(context) { + const config = context.options[0]; + const exceptions = (config && config.exceptions) || []; + const sourceCode = context.sourceCode; - if ( - reference.init === false && - reference.isWrite() && - /* - * Destructuring assignments can have multiple default value, - * so possibly there are multiple writeable references for the same identifier. - */ - (index === 0 || references[index - 1].identifier !== identifier) - ) { - context.report({ - node: identifier, - messageId: "nativeReassign", - data: identifier, - }); - } - } + /** + * Reports write references. + * @param {Reference} reference A reference to check. + * @param {int} index The index of the reference in the references. + * @param {Reference[]} references The array that the reference belongs to. + * @returns {void} + */ + function checkReference(reference, index, references) { + const identifier = reference.identifier; - /** - * Reports write references if a given variable is read-only builtin. - * @param {Variable} variable A variable to check. - * @returns {void} - */ - function checkVariable(variable) { - if ( - variable.writeable === false && - !exceptions.includes(variable.name) - ) { - variable.references.forEach(checkReference); - } - } + if (reference.init === false && + reference.isWrite() && - return { - Program(node) { - const globalScope = sourceCode.getScope(node); + /* + * Destructuring assignments can have multiple default value, + * so possibly there are multiple writeable references for the same identifier. + */ + (index === 0 || references[index - 1].identifier !== identifier) + ) { + context.report({ + node: identifier, + messageId: "nativeReassign", + data: identifier + }); + } + } - globalScope.variables.forEach(checkVariable); - }, - }; - }, + /** + * Reports write references if a given variable is read-only builtin. + * @param {Variable} variable A variable to check. + * @returns {void} + */ + function checkVariable(variable) { + if (variable.writeable === false && !exceptions.includes(variable.name)) { + variable.references.forEach(checkReference); + } + } + + return { + Program(node) { + const globalScope = sourceCode.getScope(node); + + globalScope.variables.forEach(checkVariable); + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-negated-condition.js b/node_modules/eslint/lib/rules/no-negated-condition.js index 33c150e89..3cb759049 100644 --- a/node_modules/eslint/lib/rules/no-negated-condition.js +++ b/node_modules/eslint/lib/rules/no-negated-condition.js @@ -8,93 +8,88 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", + meta: { + type: "suggestion", - docs: { - description: "Disallow negated conditions", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/no-negated-condition", - }, + docs: { + description: "Disallow negated conditions", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-negated-condition" + }, - schema: [], + schema: [], - messages: { - unexpectedNegated: "Unexpected negated condition.", - }, - }, + messages: { + unexpectedNegated: "Unexpected negated condition." + } + }, - create(context) { - /** - * Determines if a given node is an if-else without a condition on the else - * @param {ASTNode} node The node to check. - * @returns {boolean} True if the node has an else without an if. - * @private - */ - function hasElseWithoutCondition(node) { - return node.alternate && node.alternate.type !== "IfStatement"; - } + create(context) { - /** - * Determines if a given node is a negated unary expression - * @param {Object} test The test object to check. - * @returns {boolean} True if the node is a negated unary expression. - * @private - */ - function isNegatedUnaryExpression(test) { - return test.type === "UnaryExpression" && test.operator === "!"; - } + /** + * Determines if a given node is an if-else without a condition on the else + * @param {ASTNode} node The node to check. + * @returns {boolean} True if the node has an else without an if. + * @private + */ + function hasElseWithoutCondition(node) { + return node.alternate && node.alternate.type !== "IfStatement"; + } - /** - * Determines if a given node is a negated binary expression - * @param {Test} test The test to check. - * @returns {boolean} True if the node is a negated binary expression. - * @private - */ - function isNegatedBinaryExpression(test) { - return ( - test.type === "BinaryExpression" && - (test.operator === "!=" || test.operator === "!==") - ); - } + /** + * Determines if a given node is a negated unary expression + * @param {Object} test The test object to check. + * @returns {boolean} True if the node is a negated unary expression. + * @private + */ + function isNegatedUnaryExpression(test) { + return test.type === "UnaryExpression" && test.operator === "!"; + } - /** - * Determines if a given node has a negated if expression - * @param {ASTNode} node The node to check. - * @returns {boolean} True if the node has a negated if expression. - * @private - */ - function isNegatedIf(node) { - return ( - isNegatedUnaryExpression(node.test) || - isNegatedBinaryExpression(node.test) - ); - } + /** + * Determines if a given node is a negated binary expression + * @param {Test} test The test to check. + * @returns {boolean} True if the node is a negated binary expression. + * @private + */ + function isNegatedBinaryExpression(test) { + return test.type === "BinaryExpression" && + (test.operator === "!=" || test.operator === "!=="); + } - return { - IfStatement(node) { - if (!hasElseWithoutCondition(node)) { - return; - } + /** + * Determines if a given node has a negated if expression + * @param {ASTNode} node The node to check. + * @returns {boolean} True if the node has a negated if expression. + * @private + */ + function isNegatedIf(node) { + return isNegatedUnaryExpression(node.test) || isNegatedBinaryExpression(node.test); + } - if (isNegatedIf(node)) { - context.report({ - node, - messageId: "unexpectedNegated", - }); - } - }, - ConditionalExpression(node) { - if (isNegatedIf(node)) { - context.report({ - node, - messageId: "unexpectedNegated", - }); - } - }, - }; - }, + return { + IfStatement(node) { + if (!hasElseWithoutCondition(node)) { + return; + } + + if (isNegatedIf(node)) { + context.report({ + node, + messageId: "unexpectedNegated" + }); + } + }, + ConditionalExpression(node) { + if (isNegatedIf(node)) { + context.report({ + node, + messageId: "unexpectedNegated" + }); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-negated-in-lhs.js b/node_modules/eslint/lib/rules/no-negated-in-lhs.js index f003cb306..7a50be7f2 100644 --- a/node_modules/eslint/lib/rules/no-negated-in-lhs.js +++ b/node_modules/eslint/lib/rules/no-negated-in-lhs.js @@ -10,50 +10,37 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - docs: { - description: - "Disallow negating the left operand in `in` expressions", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-negated-in-lhs", - }, - - deprecated: { - message: "Renamed rule.", - url: "https://eslint.org/blog/2016/08/eslint-v3.3.0-released/#deprecated-rules", - deprecatedSince: "3.3.0", - availableUntil: "11.0.0", - replacedBy: [ - { - rule: { - name: "no-unsafe-negation", - url: "https://eslint.org/docs/rules/no-unsafe-negation", - }, - }, - ], - }, - schema: [], - - messages: { - negatedLHS: "The 'in' expression's left operand is negated.", - }, - }, - - create(context) { - return { - BinaryExpression(node) { - if ( - node.operator === "in" && - node.left.type === "UnaryExpression" && - node.left.operator === "!" - ) { - context.report({ node, messageId: "negatedLHS" }); - } - }, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow negating the left operand in `in` expressions", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-negated-in-lhs" + }, + + replacedBy: ["no-unsafe-negation"], + + deprecated: true, + schema: [], + + messages: { + negatedLHS: "The 'in' expression's left operand is negated." + } + }, + + create(context) { + + return { + + BinaryExpression(node) { + if (node.operator === "in" && node.left.type === "UnaryExpression" && node.left.operator === "!") { + context.report({ node, messageId: "negatedLHS" }); + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-nested-ternary.js b/node_modules/eslint/lib/rules/no-nested-ternary.js index 8e5dbb78c..faf80416c 100644 --- a/node_modules/eslint/lib/rules/no-nested-ternary.js +++ b/node_modules/eslint/lib/rules/no-nested-ternary.js @@ -9,38 +9,36 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: "Disallow nested ternary expressions", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/no-nested-ternary", - }, - - schema: [], - - messages: { - noNestedTernary: "Do not nest ternary expressions.", - }, - }, - - create(context) { - return { - ConditionalExpression(node) { - if ( - node.alternate.type === "ConditionalExpression" || - node.consequent.type === "ConditionalExpression" - ) { - context.report({ - node, - messageId: "noNestedTernary", - }); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow nested ternary expressions", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-nested-ternary" + }, + + schema: [], + + messages: { + noNestedTernary: "Do not nest ternary expressions." + } + }, + + create(context) { + + return { + ConditionalExpression(node) { + if (node.alternate.type === "ConditionalExpression" || + node.consequent.type === "ConditionalExpression") { + context.report({ + node, + messageId: "noNestedTernary" + }); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-new-func.js b/node_modules/eslint/lib/rules/no-new-func.js index 760dc640f..d58b2d715 100644 --- a/node_modules/eslint/lib/rules/no-new-func.js +++ b/node_modules/eslint/lib/rules/no-new-func.js @@ -21,76 +21,67 @@ const callMethods = new Set(["apply", "bind", "call"]); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: "Disallow `new` operators with the `Function` object", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-new-func", - }, - - schema: [], - - messages: { - noFunctionConstructor: "The Function constructor is eval.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - return { - "Program:exit"(node) { - const globalScope = sourceCode.getScope(node); - const variable = globalScope.set.get("Function"); - - if (variable && variable.defs.length === 0) { - variable.references.forEach(ref => { - const idNode = ref.identifier; - const { parent } = idNode; - let evalNode; - - if (parent) { - if ( - idNode === parent.callee && - (parent.type === "NewExpression" || - parent.type === "CallExpression") - ) { - evalNode = parent; - } else if ( - parent.type === "MemberExpression" && - idNode === parent.object && - callMethods.has( - astUtils.getStaticPropertyName(parent), - ) - ) { - const maybeCallee = - parent.parent.type === "ChainExpression" - ? parent.parent - : parent; - - if ( - maybeCallee.parent.type === - "CallExpression" && - maybeCallee.parent.callee === maybeCallee - ) { - evalNode = maybeCallee.parent; - } - } - } - - if (evalNode) { - context.report({ - node: evalNode, - messageId: "noFunctionConstructor", - }); - } - }); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow `new` operators with the `Function` object", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-new-func" + }, + + schema: [], + + messages: { + noFunctionConstructor: "The Function constructor is eval." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + + return { + "Program:exit"(node) { + const globalScope = sourceCode.getScope(node); + const variable = globalScope.set.get("Function"); + + if (variable && variable.defs.length === 0) { + variable.references.forEach(ref => { + const idNode = ref.identifier; + const { parent } = idNode; + let evalNode; + + if (parent) { + if (idNode === parent.callee && ( + parent.type === "NewExpression" || + parent.type === "CallExpression" + )) { + evalNode = parent; + } else if ( + parent.type === "MemberExpression" && + idNode === parent.object && + callMethods.has(astUtils.getStaticPropertyName(parent)) + ) { + const maybeCallee = parent.parent.type === "ChainExpression" ? parent.parent : parent; + + if (maybeCallee.parent.type === "CallExpression" && maybeCallee.parent.callee === maybeCallee) { + evalNode = maybeCallee.parent; + } + } + } + + if (evalNode) { + context.report({ + node: evalNode, + messageId: "noFunctionConstructor" + }); + } + }); + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-new-native-nonconstructor.js b/node_modules/eslint/lib/rules/no-new-native-nonconstructor.js index 0ae892cd5..ee70d281d 100644 --- a/node_modules/eslint/lib/rules/no-new-native-nonconstructor.js +++ b/node_modules/eslint/lib/rules/no-new-native-nonconstructor.js @@ -15,56 +15,52 @@ const nonConstructorGlobalFunctionNames = ["Symbol", "BigInt"]; // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", + meta: { + type: "problem", - docs: { - description: - "Disallow `new` operators with global non-constructor functions", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-new-native-nonconstructor", - }, + docs: { + description: "Disallow `new` operators with global non-constructor functions", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-new-native-nonconstructor" + }, - schema: [], + schema: [], - messages: { - noNewNonconstructor: - "`{{name}}` cannot be called as a constructor.", - }, - }, + messages: { + noNewNonconstructor: "`{{name}}` cannot be called as a constructor." + } + }, - create(context) { - const sourceCode = context.sourceCode; + create(context) { - return { - "Program:exit"(node) { - const globalScope = sourceCode.getScope(node); + const sourceCode = context.sourceCode; - for (const nonConstructorName of nonConstructorGlobalFunctionNames) { - const variable = globalScope.set.get(nonConstructorName); + return { + "Program:exit"(node) { + const globalScope = sourceCode.getScope(node); - if (variable && variable.defs.length === 0) { - variable.references.forEach(ref => { - const idNode = ref.identifier; - const parent = idNode.parent; + for (const nonConstructorName of nonConstructorGlobalFunctionNames) { + const variable = globalScope.set.get(nonConstructorName); - if ( - parent && - parent.type === "NewExpression" && - parent.callee === idNode - ) { - context.report({ - node: idNode, - messageId: "noNewNonconstructor", - data: { name: nonConstructorName }, - }); - } - }); - } - } - }, - }; - }, + if (variable && variable.defs.length === 0) { + variable.references.forEach(ref => { + const idNode = ref.identifier; + const parent = idNode.parent; + + if (parent && parent.type === "NewExpression" && parent.callee === idNode) { + context.report({ + node: idNode, + messageId: "noNewNonconstructor", + data: { name: nonConstructorName } + }); + } + }); + } + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-new-object.js b/node_modules/eslint/lib/rules/no-new-object.js index f18bf974f..06275f471 100644 --- a/node_modules/eslint/lib/rules/no-new-object.js +++ b/node_modules/eslint/lib/rules/no-new-object.js @@ -16,61 +16,52 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", + meta: { + type: "suggestion", - docs: { - description: "Disallow `Object` constructors", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-new-object", - }, + docs: { + description: "Disallow `Object` constructors", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-new-object" + }, - deprecated: { - message: - "The new rule flags more situations where object literal syntax can be used, and it does not report a problem when the `Object` constructor is invoked with an argument.", - url: "https://eslint.org/blog/2023/09/eslint-v8.50.0-released/", - deprecatedSince: "8.50.0", - availableUntil: "11.0.0", - replacedBy: [ - { - rule: { - name: "no-object-constructor", - url: "https://eslint.org/docs/rules/no-object-constructor", - }, - }, - ], - }, + deprecated: true, - schema: [], + replacedBy: [ + "no-object-constructor" + ], - messages: { - preferLiteral: "The object literal notation {} is preferable.", - }, - }, + schema: [], - create(context) { - const sourceCode = context.sourceCode; + messages: { + preferLiteral: "The object literal notation {} is preferable." + } + }, - return { - NewExpression(node) { - const variable = astUtils.getVariableByName( - sourceCode.getScope(node), - node.callee.name, - ); + create(context) { - if (variable && variable.identifiers.length > 0) { - return; - } + const sourceCode = context.sourceCode; - if (node.callee.name === "Object") { - context.report({ - node, - messageId: "preferLiteral", - }); - } - }, - }; - }, + return { + NewExpression(node) { + const variable = astUtils.getVariableByName( + sourceCode.getScope(node), + node.callee.name + ); + + if (variable && variable.identifiers.length > 0) { + return; + } + + if (node.callee.name === "Object") { + context.report({ + node, + messageId: "preferLiteral" + }); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-new-require.js b/node_modules/eslint/lib/rules/no-new-require.js index 09d136c83..6abfc17c8 100644 --- a/node_modules/eslint/lib/rules/no-new-require.js +++ b/node_modules/eslint/lib/rules/no-new-require.js @@ -10,58 +10,41 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Node.js rules were moved out of ESLint core.", - url: "https://eslint.org/docs/latest/use/migrating-to-7.0.0#deprecate-node-rules", - deprecatedSince: "7.0.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "eslint-plugin-n now maintains deprecated Node.js-related rules.", - plugin: { - name: "eslint-plugin-n", - url: "https://github.com/eslint-community/eslint-plugin-n", - }, - rule: { - name: "no-new-require", - url: "https://github.com/eslint-community/eslint-plugin-n/tree/master/docs/rules/no-new-require.md", - }, - }, - ], - }, + meta: { + deprecated: true, - type: "suggestion", + replacedBy: [], - docs: { - description: "Disallow `new` operators with calls to `require`", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-new-require", - }, + type: "suggestion", - schema: [], + docs: { + description: "Disallow `new` operators with calls to `require`", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-new-require" + }, - messages: { - noNewRequire: "Unexpected use of new with require.", - }, - }, + schema: [], - create(context) { - return { - NewExpression(node) { - if ( - node.callee.type === "Identifier" && - node.callee.name === "require" - ) { - context.report({ - node, - messageId: "noNewRequire", - }); - } - }, - }; - }, + messages: { + noNewRequire: "Unexpected use of new with require." + } + }, + + create(context) { + + return { + + NewExpression(node) { + if (node.callee.type === "Identifier" && node.callee.name === "require") { + context.report({ + node, + messageId: "noNewRequire" + }); + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-new-symbol.js b/node_modules/eslint/lib/rules/no-new-symbol.js index 838f502f5..998302202 100644 --- a/node_modules/eslint/lib/rules/no-new-symbol.js +++ b/node_modules/eslint/lib/rules/no-new-symbol.js @@ -1,7 +1,6 @@ /** * @fileoverview Rule to disallow use of the new operator with the `Symbol` object * @author Alberto Rodríguez - * @deprecated in ESLint v9.0.0 */ "use strict"; @@ -10,65 +9,48 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - docs: { - description: "Disallow `new` operators with the `Symbol` object", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-new-symbol", - }, - - deprecated: { - message: "The rule was replaced with a more general rule.", - url: "https://eslint.org/docs/latest/use/migrate-to-9.0.0#eslint-recommended", - deprecatedSince: "9.0.0", - availableUntil: "11.0.0", - replacedBy: [ - { - rule: { - name: "no-new-native-nonconstructor", - url: "https://eslint.org/docs/latest/rules/no-new-native-nonconstructor", - }, - }, - ], - }, - - schema: [], - - messages: { - noNewSymbol: "`Symbol` cannot be called as a constructor.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - return { - "Program:exit"(node) { - const globalScope = sourceCode.getScope(node); - const variable = globalScope.set.get("Symbol"); - - if (variable && variable.defs.length === 0) { - variable.references.forEach(ref => { - const idNode = ref.identifier; - const parent = idNode.parent; - - if ( - parent && - parent.type === "NewExpression" && - parent.callee === idNode - ) { - context.report({ - node: idNode, - messageId: "noNewSymbol", - }); - } - }); - } - }, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow `new` operators with the `Symbol` object", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-new-symbol" + }, + + schema: [], + + messages: { + noNewSymbol: "`Symbol` cannot be called as a constructor." + } + }, + + create(context) { + + const sourceCode = context.sourceCode; + + return { + "Program:exit"(node) { + const globalScope = sourceCode.getScope(node); + const variable = globalScope.set.get("Symbol"); + + if (variable && variable.defs.length === 0) { + variable.references.forEach(ref => { + const idNode = ref.identifier; + const parent = idNode.parent; + + if (parent && parent.type === "NewExpression" && parent.callee === idNode) { + context.report({ + node: idNode, + messageId: "noNewSymbol" + }); + } + }); + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-new-wrappers.js b/node_modules/eslint/lib/rules/no-new-wrappers.js index 84025dbf1..5050a98a0 100644 --- a/node_modules/eslint/lib/rules/no-new-wrappers.js +++ b/node_modules/eslint/lib/rules/no-new-wrappers.js @@ -15,48 +15,46 @@ const { getVariableByName } = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: - "Disallow `new` operators with the `String`, `Number`, and `Boolean` objects", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-new-wrappers", - }, - - schema: [], - - messages: { - noConstructor: "Do not use {{fn}} as a constructor.", - }, - }, - - create(context) { - const { sourceCode } = context; - - return { - NewExpression(node) { - const wrapperObjects = ["String", "Number", "Boolean"]; - const { name } = node.callee; - - if (wrapperObjects.includes(name)) { - const variable = getVariableByName( - sourceCode.getScope(node), - name, - ); - - if (variable && variable.identifiers.length === 0) { - context.report({ - node, - messageId: "noConstructor", - data: { fn: name }, - }); - } - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow `new` operators with the `String`, `Number`, and `Boolean` objects", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-new-wrappers" + }, + + schema: [], + + messages: { + noConstructor: "Do not use {{fn}} as a constructor." + } + }, + + create(context) { + const { sourceCode } = context; + + return { + + NewExpression(node) { + const wrapperObjects = ["String", "Number", "Boolean"]; + const { name } = node.callee; + + if (wrapperObjects.includes(name)) { + const variable = getVariableByName(sourceCode.getScope(node), name); + + if (variable && variable.identifiers.length === 0) { + context.report({ + node, + messageId: "noConstructor", + data: { fn: name } + }); + } + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-new.js b/node_modules/eslint/lib/rules/no-new.js index 8f91835b2..9e20bad7b 100644 --- a/node_modules/eslint/lib/rules/no-new.js +++ b/node_modules/eslint/lib/rules/no-new.js @@ -10,33 +10,34 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: - "Disallow `new` operators outside of assignments or comparisons", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-new", - }, - - schema: [], - - messages: { - noNewStatement: "Do not use 'new' for side effects.", - }, - }, - - create(context) { - return { - "ExpressionStatement > NewExpression"(node) { - context.report({ - node: node.parent, - messageId: "noNewStatement", - }); - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow `new` operators outside of assignments or comparisons", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-new" + }, + + schema: [], + + messages: { + noNewStatement: "Do not use 'new' for side effects." + } + }, + + create(context) { + + return { + "ExpressionStatement > NewExpression"(node) { + context.report({ + node: node.parent, + messageId: "noNewStatement" + }); + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-nonoctal-decimal-escape.js b/node_modules/eslint/lib/rules/no-nonoctal-decimal-escape.js index d09b7994b..5939390fd 100644 --- a/node_modules/eslint/lib/rules/no-nonoctal-decimal-escape.js +++ b/node_modules/eslint/lib/rules/no-nonoctal-decimal-escape.js @@ -5,14 +5,6 @@ "use strict"; -//------------------------------------------------------------------------------ -// Typedefs -//------------------------------------------------------------------------------ - -/** - * @import { SourceRange } from "@eslint/core"; - */ - //------------------------------------------------------------------------------ // Helpers //------------------------------------------------------------------------------ @@ -25,152 +17,132 @@ const QUICK_TEST_REGEX = /\\[89]/u; * @returns {string} "\uXXXX" sequence. */ function getUnicodeEscape(character) { - return `\\u${character.charCodeAt(0).toString(16).padStart(4, "0")}`; + return `\\u${character.charCodeAt(0).toString(16).padStart(4, "0")}`; } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: - "Disallow `\\8` and `\\9` escape sequences in string literals", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-nonoctal-decimal-escape", - }, - - hasSuggestions: true, - - schema: [], - - messages: { - decimalEscape: "Don't use '{{decimalEscape}}' escape sequence.", - - // suggestions - refactor: - "Replace '{{original}}' with '{{replacement}}'. This maintains the current functionality.", - escapeBackslash: - "Replace '{{original}}' with '{{replacement}}' to include the actual backslash character.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - /** - * Creates a new Suggestion object. - * @param {string} messageId "refactor" or "escapeBackslash". - * @param {SourceRange} range The range to replace. - * @param {string} replacement New text for the range. - * @returns {Object} Suggestion - */ - function createSuggestion(messageId, range, replacement) { - return { - messageId, - data: { - original: sourceCode.getText().slice(...range), - replacement, - }, - fix(fixer) { - return fixer.replaceTextRange(range, replacement); - }, - }; - } - - return { - Literal(node) { - if (typeof node.value !== "string") { - return; - } - - if (!QUICK_TEST_REGEX.test(node.raw)) { - return; - } - - const regex = - /(?:[^\\]|(?\\.))*?(?\\[89])/suy; - let match; - - while ((match = regex.exec(node.raw))) { - const { previousEscape, decimalEscape } = match.groups; - const decimalEscapeRangeEnd = - node.range[0] + match.index + match[0].length; - const decimalEscapeRangeStart = - decimalEscapeRangeEnd - decimalEscape.length; - const decimalEscapeRange = [ - decimalEscapeRangeStart, - decimalEscapeRangeEnd, - ]; - const suggest = []; - - // When `regex` is matched, `previousEscape` can only capture characters adjacent to `decimalEscape` - if (previousEscape === "\\0") { - /* - * Now we have a NULL escape "\0" immediately followed by a decimal escape, e.g.: "\0\8". - * Fixing this to "\08" would turn "\0" into a legacy octal escape. To avoid producing - * an octal escape while fixing a decimal escape, we provide different suggestions. - */ - suggest.push( - createSuggestion( - // "\0\8" -> "\u00008" - "refactor", - [ - decimalEscapeRangeStart - - previousEscape.length, - decimalEscapeRangeEnd, - ], - `${getUnicodeEscape("\0")}${decimalEscape[1]}`, - ), - createSuggestion( - // "\8" -> "\u0038" - "refactor", - decimalEscapeRange, - getUnicodeEscape(decimalEscape[1]), - ), - ); - } else { - suggest.push( - createSuggestion( - // "\8" -> "8" - "refactor", - decimalEscapeRange, - decimalEscape[1], - ), - ); - } - - suggest.push( - createSuggestion( - // "\8" -> "\\8" - "escapeBackslash", - decimalEscapeRange, - `\\${decimalEscape}`, - ), - ); - - context.report({ - node, - loc: { - start: sourceCode.getLocFromIndex( - decimalEscapeRangeStart, - ), - end: sourceCode.getLocFromIndex( - decimalEscapeRangeEnd, - ), - }, - messageId: "decimalEscape", - data: { - decimalEscape, - }, - suggest, - }); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow `\\8` and `\\9` escape sequences in string literals", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-nonoctal-decimal-escape" + }, + + hasSuggestions: true, + + schema: [], + + messages: { + decimalEscape: "Don't use '{{decimalEscape}}' escape sequence.", + + // suggestions + refactor: "Replace '{{original}}' with '{{replacement}}'. This maintains the current functionality.", + escapeBackslash: "Replace '{{original}}' with '{{replacement}}' to include the actual backslash character." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + + /** + * Creates a new Suggestion object. + * @param {string} messageId "refactor" or "escapeBackslash". + * @param {int[]} range The range to replace. + * @param {string} replacement New text for the range. + * @returns {Object} Suggestion + */ + function createSuggestion(messageId, range, replacement) { + return { + messageId, + data: { + original: sourceCode.getText().slice(...range), + replacement + }, + fix(fixer) { + return fixer.replaceTextRange(range, replacement); + } + }; + } + + return { + Literal(node) { + if (typeof node.value !== "string") { + return; + } + + if (!QUICK_TEST_REGEX.test(node.raw)) { + return; + } + + const regex = /(?:[^\\]|(?\\.))*?(?\\[89])/suy; + let match; + + while ((match = regex.exec(node.raw))) { + const { previousEscape, decimalEscape } = match.groups; + const decimalEscapeRangeEnd = node.range[0] + match.index + match[0].length; + const decimalEscapeRangeStart = decimalEscapeRangeEnd - decimalEscape.length; + const decimalEscapeRange = [decimalEscapeRangeStart, decimalEscapeRangeEnd]; + const suggest = []; + + // When `regex` is matched, `previousEscape` can only capture characters adjacent to `decimalEscape` + if (previousEscape === "\\0") { + + /* + * Now we have a NULL escape "\0" immediately followed by a decimal escape, e.g.: "\0\8". + * Fixing this to "\08" would turn "\0" into a legacy octal escape. To avoid producing + * an octal escape while fixing a decimal escape, we provide different suggestions. + */ + suggest.push( + createSuggestion( // "\0\8" -> "\u00008" + "refactor", + [decimalEscapeRangeStart - previousEscape.length, decimalEscapeRangeEnd], + `${getUnicodeEscape("\0")}${decimalEscape[1]}` + ), + createSuggestion( // "\8" -> "\u0038" + "refactor", + decimalEscapeRange, + getUnicodeEscape(decimalEscape[1]) + ) + ); + } else { + suggest.push( + createSuggestion( // "\8" -> "8" + "refactor", + decimalEscapeRange, + decimalEscape[1] + ) + ); + } + + suggest.push( + createSuggestion( // "\8" -> "\\8" + "escapeBackslash", + decimalEscapeRange, + `\\${decimalEscape}` + ) + ); + + context.report({ + node, + loc: { + start: sourceCode.getLocFromIndex(decimalEscapeRangeStart), + end: sourceCode.getLocFromIndex(decimalEscapeRangeEnd) + }, + messageId: "decimalEscape", + data: { + decimalEscape + }, + suggest + }); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-obj-calls.js b/node_modules/eslint/lib/rules/no-obj-calls.js index df76b482e..ee767ea2f 100644 --- a/node_modules/eslint/lib/rules/no-obj-calls.js +++ b/node_modules/eslint/lib/rules/no-obj-calls.js @@ -9,11 +9,7 @@ // Requirements //------------------------------------------------------------------------------ -const { - CALL, - CONSTRUCT, - ReferenceTracker, -} = require("@eslint-community/eslint-utils"); +const { CALL, CONSTRUCT, ReferenceTracker } = require("@eslint-community/eslint-utils"); const getPropertyName = require("./utils/ast-utils").getStaticPropertyName; //------------------------------------------------------------------------------ @@ -28,72 +24,63 @@ const nonCallableGlobals = ["Atomics", "JSON", "Math", "Reflect", "Intl"]; * @returns {string} name to report */ function getReportNodeName(node) { - if (node.type === "ChainExpression") { - return getReportNodeName(node.expression); - } - if (node.type === "MemberExpression") { - return getPropertyName(node); - } - return node.name; + if (node.type === "ChainExpression") { + return getReportNodeName(node.expression); + } + if (node.type === "MemberExpression") { + return getPropertyName(node); + } + return node.name; } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - docs: { - description: - "Disallow calling global object properties as functions", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-obj-calls", - }, - - schema: [], - - messages: { - unexpectedCall: "'{{name}}' is not a function.", - unexpectedRefCall: - "'{{name}}' is reference to '{{ref}}', which is not a function.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - return { - Program(node) { - const scope = sourceCode.getScope(node); - const tracker = new ReferenceTracker(scope); - const traceMap = {}; - - for (const g of nonCallableGlobals) { - traceMap[g] = { - [CALL]: true, - [CONSTRUCT]: true, - }; - } - - for (const { - node: refNode, - path, - } of tracker.iterateGlobalReferences(traceMap)) { - const name = getReportNodeName(refNode.callee); - const ref = path[0]; - const messageId = - name === ref ? "unexpectedCall" : "unexpectedRefCall"; - - context.report({ - node: refNode, - messageId, - data: { name, ref }, - }); - } - }, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow calling global object properties as functions", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-obj-calls" + }, + + schema: [], + + messages: { + unexpectedCall: "'{{name}}' is not a function.", + unexpectedRefCall: "'{{name}}' is reference to '{{ref}}', which is not a function." + } + }, + + create(context) { + + const sourceCode = context.sourceCode; + + return { + Program(node) { + const scope = sourceCode.getScope(node); + const tracker = new ReferenceTracker(scope); + const traceMap = {}; + + for (const g of nonCallableGlobals) { + traceMap[g] = { + [CALL]: true, + [CONSTRUCT]: true + }; + } + + for (const { node: refNode, path } of tracker.iterateGlobalReferences(traceMap)) { + const name = getReportNodeName(refNode.callee); + const ref = path[0]; + const messageId = name === ref ? "unexpectedCall" : "unexpectedRefCall"; + + context.report({ node: refNode, messageId, data: { name, ref } }); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-object-constructor.js b/node_modules/eslint/lib/rules/no-object-constructor.js index fffa61227..8875ec212 100644 --- a/node_modules/eslint/lib/rules/no-object-constructor.js +++ b/node_modules/eslint/lib/rules/no-object-constructor.js @@ -10,115 +10,108 @@ //------------------------------------------------------------------------------ const { - getVariableByName, - isArrowToken, - isStartOfExpressionStatement, - needsPrecedingSemicolon, + getVariableByName, + isArrowToken, + isStartOfExpressionStatement, + needsPrecedingSemicolon } = require("./utils/ast-utils"); //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: - "Disallow calls to the `Object` constructor without an argument", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-object-constructor", - }, - - hasSuggestions: true, - - schema: [], - - messages: { - preferLiteral: "The object literal notation {} is preferable.", - useLiteral: "Replace with '{{replacement}}'.", - useLiteralAfterSemicolon: - "Replace with '{{replacement}}', add preceding semicolon.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - /** - * Determines whether or not an object literal that replaces a specified node needs to be enclosed in parentheses. - * @param {ASTNode} node The node to be replaced. - * @returns {boolean} Whether or not parentheses around the object literal are required. - */ - function needsParentheses(node) { - if (isStartOfExpressionStatement(node)) { - return true; - } - - const prevToken = sourceCode.getTokenBefore(node); - - if (prevToken && isArrowToken(prevToken)) { - return true; - } - - return false; - } - - /** - * Reports on nodes where the `Object` constructor is called without arguments. - * @param {ASTNode} node The node to evaluate. - * @returns {void} - */ - function check(node) { - if ( - node.callee.type !== "Identifier" || - node.callee.name !== "Object" || - node.arguments.length - ) { - return; - } - - const variable = getVariableByName( - sourceCode.getScope(node), - "Object", - ); - - if (variable && variable.identifiers.length === 0) { - let replacement; - let fixText; - let messageId = "useLiteral"; - - if (needsParentheses(node)) { - replacement = "({})"; - if (needsPrecedingSemicolon(sourceCode, node)) { - fixText = ";({})"; - messageId = "useLiteralAfterSemicolon"; - } else { - fixText = "({})"; - } - } else { - replacement = fixText = "{}"; - } - - context.report({ - node, - messageId: "preferLiteral", - suggest: [ - { - messageId, - data: { replacement }, - fix: fixer => fixer.replaceText(node, fixText), - }, - ], - }); - } - } - - return { - CallExpression: check, - NewExpression: check, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow calls to the `Object` constructor without an argument", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-object-constructor" + }, + + hasSuggestions: true, + + schema: [], + + messages: { + preferLiteral: "The object literal notation {} is preferable.", + useLiteral: "Replace with '{{replacement}}'.", + useLiteralAfterSemicolon: "Replace with '{{replacement}}', add preceding semicolon." + } + }, + + create(context) { + + const sourceCode = context.sourceCode; + + /** + * Determines whether or not an object literal that replaces a specified node needs to be enclosed in parentheses. + * @param {ASTNode} node The node to be replaced. + * @returns {boolean} Whether or not parentheses around the object literal are required. + */ + function needsParentheses(node) { + if (isStartOfExpressionStatement(node)) { + return true; + } + + const prevToken = sourceCode.getTokenBefore(node); + + if (prevToken && isArrowToken(prevToken)) { + return true; + } + + return false; + } + + /** + * Reports on nodes where the `Object` constructor is called without arguments. + * @param {ASTNode} node The node to evaluate. + * @returns {void} + */ + function check(node) { + if (node.callee.type !== "Identifier" || node.callee.name !== "Object" || node.arguments.length) { + return; + } + + const variable = getVariableByName(sourceCode.getScope(node), "Object"); + + if (variable && variable.identifiers.length === 0) { + let replacement; + let fixText; + let messageId = "useLiteral"; + + if (needsParentheses(node)) { + replacement = "({})"; + if (needsPrecedingSemicolon(sourceCode, node)) { + fixText = ";({})"; + messageId = "useLiteralAfterSemicolon"; + } else { + fixText = "({})"; + } + } else { + replacement = fixText = "{}"; + } + + context.report({ + node, + messageId: "preferLiteral", + suggest: [ + { + messageId, + data: { replacement }, + fix: fixer => fixer.replaceText(node, fixText) + } + ] + }); + } + } + + return { + CallExpression: check, + NewExpression: check + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-octal-escape.js b/node_modules/eslint/lib/rules/no-octal-escape.js index 975b30854..6924d5419 100644 --- a/node_modules/eslint/lib/rules/no-octal-escape.js +++ b/node_modules/eslint/lib/rules/no-octal-escape.js @@ -9,45 +9,48 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: "Disallow octal escape sequences in string literals", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-octal-escape", - }, - - schema: [], - - messages: { - octalEscapeSequence: - "Don't use octal: '\\{{sequence}}'. Use '\\u....' instead.", - }, - }, - - create(context) { - return { - Literal(node) { - if (typeof node.value !== "string") { - return; - } - - // \0 represents a valid NULL character if it isn't followed by a digit. - const match = node.raw.match( - /^(?:[^\\]|\\.)*?\\([0-3][0-7]{1,2}|[4-7][0-7]|0(?=[89])|[1-7])/su, - ); - - if (match) { - context.report({ - node, - messageId: "octalEscapeSequence", - data: { sequence: match[1] }, - }); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow octal escape sequences in string literals", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-octal-escape" + }, + + schema: [], + + messages: { + octalEscapeSequence: "Don't use octal: '\\{{sequence}}'. Use '\\u....' instead." + } + }, + + create(context) { + + return { + + Literal(node) { + if (typeof node.value !== "string") { + return; + } + + // \0 represents a valid NULL character if it isn't followed by a digit. + const match = node.raw.match( + /^(?:[^\\]|\\.)*?\\([0-3][0-7]{1,2}|[4-7][0-7]|0(?=[89])|[1-7])/su + ); + + if (match) { + context.report({ + node, + messageId: "octalEscapeSequence", + data: { sequence: match[1] } + }); + } + } + + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-octal.js b/node_modules/eslint/lib/rules/no-octal.js index e5ea9bda0..dc027696a 100644 --- a/node_modules/eslint/lib/rules/no-octal.js +++ b/node_modules/eslint/lib/rules/no-octal.js @@ -9,34 +9,37 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: "Disallow octal literals", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-octal", - }, - - schema: [], - - messages: { - noOctal: "Octal literals should not be used.", - }, - }, - - create(context) { - return { - Literal(node) { - if (typeof node.value === "number" && /^0\d/u.test(node.raw)) { - context.report({ - node, - messageId: "noOctal", - }); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow octal literals", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-octal" + }, + + schema: [], + + messages: { + noOctal: "Octal literals should not be used." + } + }, + + create(context) { + + return { + + Literal(node) { + if (typeof node.value === "number" && /^0[0-9]/u.test(node.raw)) { + context.report({ + node, + messageId: "noOctal" + }); + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-param-reassign.js b/node_modules/eslint/lib/rules/no-param-reassign.js index ec22030a7..607cafd38 100644 --- a/node_modules/eslint/lib/rules/no-param-reassign.js +++ b/node_modules/eslint/lib/rules/no-param-reassign.js @@ -1,5 +1,5 @@ /** - * @fileoverview Disallow reassigning function parameters. + * @fileoverview Disallow reassignment of function parameters. * @author Nat Burns */ "use strict"; @@ -8,241 +8,223 @@ // Rule Definition //------------------------------------------------------------------------------ -const stopNodePattern = - /(?:Statement|Declaration|Function(?:Expression)?|Program)$/u; +const stopNodePattern = /(?:Statement|Declaration|Function(?:Expression)?|Program)$/u; -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: "Disallow reassigning function parameters", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-param-reassign", - }, - - schema: [ - { - oneOf: [ - { - type: "object", - properties: { - props: { - enum: [false], - }, - }, - additionalProperties: false, - }, - { - type: "object", - properties: { - props: { - enum: [true], - }, - ignorePropertyModificationsFor: { - type: "array", - items: { - type: "string", - }, - uniqueItems: true, - }, - ignorePropertyModificationsForRegex: { - type: "array", - items: { - type: "string", - }, - uniqueItems: true, - }, - }, - additionalProperties: false, - }, - ], - }, - ], - - messages: { - assignmentToFunctionParam: - "Assignment to function parameter '{{name}}'.", - assignmentToFunctionParamProp: - "Assignment to property of function parameter '{{name}}'.", - }, - }, - - create(context) { - const props = context.options[0] && context.options[0].props; - const ignoredPropertyAssignmentsFor = - (context.options[0] && - context.options[0].ignorePropertyModificationsFor) || - []; - const ignoredPropertyAssignmentsForRegex = - (context.options[0] && - context.options[0].ignorePropertyModificationsForRegex) || - []; - const sourceCode = context.sourceCode; - - /** - * Checks whether or not the reference modifies properties of its variable. - * @param {Reference} reference A reference to check. - * @returns {boolean} Whether or not the reference modifies properties of its variable. - */ - function isModifyingProp(reference) { - let node = reference.identifier; - let parent = node.parent; - - while ( - parent && - (!stopNodePattern.test(parent.type) || - parent.type === "ForInStatement" || - parent.type === "ForOfStatement") - ) { - switch (parent.type) { - // e.g. foo.a = 0; - case "AssignmentExpression": - return parent.left === node; - - // e.g. ++foo.a; - case "UpdateExpression": - return true; - - // e.g. delete foo.a; - case "UnaryExpression": - if (parent.operator === "delete") { - return true; - } - break; - - // e.g. for (foo.a in b) {} - case "ForInStatement": - case "ForOfStatement": - if (parent.left === node) { - return true; - } - - // this is a stop node for parent.right and parent.body - return false; - - // EXCLUDES: e.g. cache.get(foo.a).b = 0; - case "CallExpression": - if (parent.callee !== node) { - return false; - } - break; - - // EXCLUDES: e.g. cache[foo.a] = 0; - case "MemberExpression": - if (parent.property === node) { - return false; - } - break; - - // EXCLUDES: e.g. ({ [foo]: a }) = bar; - case "Property": - if (parent.key === node) { - return false; - } - - break; - - // EXCLUDES: e.g. (foo ? a : b).c = bar; - case "ConditionalExpression": - if (parent.test === node) { - return false; - } - - break; - - // no default - } - - node = parent; - parent = node.parent; - } - - return false; - } - - /** - * Tests that an identifier name matches any of the ignored property assignments. - * First we test strings in ignoredPropertyAssignmentsFor. - * Then we instantiate and test RegExp objects from ignoredPropertyAssignmentsForRegex strings. - * @param {string} identifierName A string that describes the name of an identifier to - * ignore property assignments for. - * @returns {boolean} Whether the string matches an ignored property assignment regular expression or not. - */ - function isIgnoredPropertyAssignment(identifierName) { - return ( - ignoredPropertyAssignmentsFor.includes(identifierName) || - ignoredPropertyAssignmentsForRegex.some(ignored => - new RegExp(ignored, "u").test(identifierName), - ) - ); - } - - /** - * Reports a reference if is non initializer and writable. - * @param {Reference} reference A reference to check. - * @param {number} index The index of the reference in the references. - * @param {Reference[]} references The array that the reference belongs to. - * @returns {void} - */ - function checkReference(reference, index, references) { - const identifier = reference.identifier; - - if ( - identifier && - !reference.init && - /* - * Destructuring assignments can have multiple default value, - * so possibly there are multiple writeable references for the same identifier. - */ - (index === 0 || references[index - 1].identifier !== identifier) - ) { - if (reference.isWrite()) { - context.report({ - node: identifier, - messageId: "assignmentToFunctionParam", - data: { name: identifier.name }, - }); - } else if ( - props && - isModifyingProp(reference) && - !isIgnoredPropertyAssignment(identifier.name) - ) { - context.report({ - node: identifier, - messageId: "assignmentToFunctionParamProp", - data: { name: identifier.name }, - }); - } - } - } - - /** - * Finds and reports references that are non initializer and writable. - * @param {Variable} variable A variable to check. - * @returns {void} - */ - function checkVariable(variable) { - if (variable.defs[0].type === "Parameter") { - variable.references.forEach(checkReference); - } - } - - /** - * Checks parameters of a given function node. - * @param {ASTNode} node A function node to check. - * @returns {void} - */ - function checkForFunction(node) { - sourceCode.getDeclaredVariables(node).forEach(checkVariable); - } - - return { - // `:exit` is needed for the `node.parent` property of identifier nodes. - "FunctionDeclaration:exit": checkForFunction, - "FunctionExpression:exit": checkForFunction, - "ArrowFunctionExpression:exit": checkForFunction, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow reassigning `function` parameters", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-param-reassign" + }, + + schema: [ + { + oneOf: [ + { + type: "object", + properties: { + props: { + enum: [false] + } + }, + additionalProperties: false + }, + { + type: "object", + properties: { + props: { + enum: [true] + }, + ignorePropertyModificationsFor: { + type: "array", + items: { + type: "string" + }, + uniqueItems: true + }, + ignorePropertyModificationsForRegex: { + type: "array", + items: { + type: "string" + }, + uniqueItems: true + } + }, + additionalProperties: false + } + ] + } + ], + + messages: { + assignmentToFunctionParam: "Assignment to function parameter '{{name}}'.", + assignmentToFunctionParamProp: "Assignment to property of function parameter '{{name}}'." + } + }, + + create(context) { + const props = context.options[0] && context.options[0].props; + const ignoredPropertyAssignmentsFor = context.options[0] && context.options[0].ignorePropertyModificationsFor || []; + const ignoredPropertyAssignmentsForRegex = context.options[0] && context.options[0].ignorePropertyModificationsForRegex || []; + const sourceCode = context.sourceCode; + + /** + * Checks whether or not the reference modifies properties of its variable. + * @param {Reference} reference A reference to check. + * @returns {boolean} Whether or not the reference modifies properties of its variable. + */ + function isModifyingProp(reference) { + let node = reference.identifier; + let parent = node.parent; + + while (parent && (!stopNodePattern.test(parent.type) || + parent.type === "ForInStatement" || parent.type === "ForOfStatement")) { + switch (parent.type) { + + // e.g. foo.a = 0; + case "AssignmentExpression": + return parent.left === node; + + // e.g. ++foo.a; + case "UpdateExpression": + return true; + + // e.g. delete foo.a; + case "UnaryExpression": + if (parent.operator === "delete") { + return true; + } + break; + + // e.g. for (foo.a in b) {} + case "ForInStatement": + case "ForOfStatement": + if (parent.left === node) { + return true; + } + + // this is a stop node for parent.right and parent.body + return false; + + // EXCLUDES: e.g. cache.get(foo.a).b = 0; + case "CallExpression": + if (parent.callee !== node) { + return false; + } + break; + + // EXCLUDES: e.g. cache[foo.a] = 0; + case "MemberExpression": + if (parent.property === node) { + return false; + } + break; + + // EXCLUDES: e.g. ({ [foo]: a }) = bar; + case "Property": + if (parent.key === node) { + return false; + } + + break; + + // EXCLUDES: e.g. (foo ? a : b).c = bar; + case "ConditionalExpression": + if (parent.test === node) { + return false; + } + + break; + + // no default + } + + node = parent; + parent = node.parent; + } + + return false; + } + + /** + * Tests that an identifier name matches any of the ignored property assignments. + * First we test strings in ignoredPropertyAssignmentsFor. + * Then we instantiate and test RegExp objects from ignoredPropertyAssignmentsForRegex strings. + * @param {string} identifierName A string that describes the name of an identifier to + * ignore property assignments for. + * @returns {boolean} Whether the string matches an ignored property assignment regular expression or not. + */ + function isIgnoredPropertyAssignment(identifierName) { + return ignoredPropertyAssignmentsFor.includes(identifierName) || + ignoredPropertyAssignmentsForRegex.some(ignored => new RegExp(ignored, "u").test(identifierName)); + } + + /** + * Reports a reference if is non initializer and writable. + * @param {Reference} reference A reference to check. + * @param {int} index The index of the reference in the references. + * @param {Reference[]} references The array that the reference belongs to. + * @returns {void} + */ + function checkReference(reference, index, references) { + const identifier = reference.identifier; + + if (identifier && + !reference.init && + + /* + * Destructuring assignments can have multiple default value, + * so possibly there are multiple writeable references for the same identifier. + */ + (index === 0 || references[index - 1].identifier !== identifier) + ) { + if (reference.isWrite()) { + context.report({ + node: identifier, + messageId: "assignmentToFunctionParam", + data: { name: identifier.name } + }); + } else if (props && isModifyingProp(reference) && !isIgnoredPropertyAssignment(identifier.name)) { + context.report({ + node: identifier, + messageId: "assignmentToFunctionParamProp", + data: { name: identifier.name } + }); + } + } + } + + /** + * Finds and reports references that are non initializer and writable. + * @param {Variable} variable A variable to check. + * @returns {void} + */ + function checkVariable(variable) { + if (variable.defs[0].type === "Parameter") { + variable.references.forEach(checkReference); + } + } + + /** + * Checks parameters of a given function node. + * @param {ASTNode} node A function node to check. + * @returns {void} + */ + function checkForFunction(node) { + sourceCode.getDeclaredVariables(node).forEach(checkVariable); + } + + return { + + // `:exit` is needed for the `node.parent` property of identifier nodes. + "FunctionDeclaration:exit": checkForFunction, + "FunctionExpression:exit": checkForFunction, + "ArrowFunctionExpression:exit": checkForFunction + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-path-concat.js b/node_modules/eslint/lib/rules/no-path-concat.js index 230a39438..2e4a3a21c 100644 --- a/node_modules/eslint/lib/rules/no-path-concat.js +++ b/node_modules/eslint/lib/rules/no-path-concat.js @@ -9,71 +9,56 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Node.js rules were moved out of ESLint core.", - url: "https://eslint.org/docs/latest/use/migrating-to-7.0.0#deprecate-node-rules", - deprecatedSince: "7.0.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "eslint-plugin-n now maintains deprecated Node.js-related rules.", - plugin: { - name: "eslint-plugin-n", - url: "https://github.com/eslint-community/eslint-plugin-n", - }, - rule: { - name: "no-path-concat", - url: "https://github.com/eslint-community/eslint-plugin-n/tree/master/docs/rules/no-path-concat.md", - }, - }, - ], - }, - - type: "suggestion", - - docs: { - description: - "Disallow string concatenation with `__dirname` and `__filename`", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-path-concat", - }, - - schema: [], - - messages: { - usePathFunctions: - "Use path.join() or path.resolve() instead of + to create paths.", - }, - }, - - create(context) { - const MATCHER = /^__(?:dir|file)name$/u; - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - return { - BinaryExpression(node) { - const left = node.left, - right = node.right; - - if ( - node.operator === "+" && - ((left.type === "Identifier" && MATCHER.test(left.name)) || - (right.type === "Identifier" && - MATCHER.test(right.name))) - ) { - context.report({ - node, - messageId: "usePathFunctions", - }); - } - }, - }; - }, + meta: { + deprecated: true, + + replacedBy: [], + + type: "suggestion", + + docs: { + description: "Disallow string concatenation with `__dirname` and `__filename`", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-path-concat" + }, + + schema: [], + + messages: { + usePathFunctions: "Use path.join() or path.resolve() instead of + to create paths." + } + }, + + create(context) { + + const MATCHER = /^__(?:dir|file)name$/u; + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + return { + + BinaryExpression(node) { + + const left = node.left, + right = node.right; + + if (node.operator === "+" && + ((left.type === "Identifier" && MATCHER.test(left.name)) || + (right.type === "Identifier" && MATCHER.test(right.name))) + ) { + + context.report({ + node, + messageId: "usePathFunctions" + }); + } + } + + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-plusplus.js b/node_modules/eslint/lib/rules/no-plusplus.js index 1f3a9f0f8..22a6fd013 100644 --- a/node_modules/eslint/lib/rules/no-plusplus.js +++ b/node_modules/eslint/lib/rules/no-plusplus.js @@ -16,9 +16,9 @@ * @returns {boolean} `true` if the node is `ForStatement` update. */ function isForStatementUpdate(node) { - const parent = node.parent; + const parent = node.parent; - return parent.type === "ForStatement" && parent.update === node; + return parent.type === "ForStatement" && parent.update === node; } /** @@ -32,71 +32,74 @@ function isForStatementUpdate(node) { * @returns {boolean} `true` if the node is a for loop afterthought. */ function isForLoopAfterthought(node) { - const parent = node.parent; + const parent = node.parent; - if (parent.type === "SequenceExpression") { - return isForLoopAfterthought(parent); - } + if (parent.type === "SequenceExpression") { + return isForLoopAfterthought(parent); + } - return isForStatementUpdate(node); + return isForStatementUpdate(node); } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - defaultOptions: [ - { - allowForLoopAfterthoughts: false, - }, - ], - - docs: { - description: "Disallow the unary operators `++` and `--`", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/no-plusplus", - }, - - schema: [ - { - type: "object", - properties: { - allowForLoopAfterthoughts: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - - messages: { - unexpectedUnaryOp: "Unary operator '{{operator}}' used.", - }, - }, - - create(context) { - const [{ allowForLoopAfterthoughts }] = context.options; - - return { - UpdateExpression(node) { - if (allowForLoopAfterthoughts && isForLoopAfterthought(node)) { - return; - } - - context.report({ - node, - messageId: "unexpectedUnaryOp", - data: { - operator: node.operator, - }, - }); - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow the unary operators `++` and `--`", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-plusplus" + }, + + schema: [ + { + type: "object", + properties: { + allowForLoopAfterthoughts: { + type: "boolean", + default: false + } + }, + additionalProperties: false + } + ], + + messages: { + unexpectedUnaryOp: "Unary operator '{{operator}}' used." + } + }, + + create(context) { + + const config = context.options[0]; + let allowForLoopAfterthoughts = false; + + if (typeof config === "object") { + allowForLoopAfterthoughts = config.allowForLoopAfterthoughts === true; + } + + return { + + UpdateExpression(node) { + if (allowForLoopAfterthoughts && isForLoopAfterthought(node)) { + return; + } + + context.report({ + node, + messageId: "unexpectedUnaryOp", + data: { + operator: node.operator + } + }); + } + + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-process-env.js b/node_modules/eslint/lib/rules/no-process-env.js index c9dc353ff..8dac648ff 100644 --- a/node_modules/eslint/lib/rules/no-process-env.js +++ b/node_modules/eslint/lib/rules/no-process-env.js @@ -9,60 +9,43 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Node.js rules were moved out of ESLint core.", - url: "https://eslint.org/docs/latest/use/migrating-to-7.0.0#deprecate-node-rules", - deprecatedSince: "7.0.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "eslint-plugin-n now maintains deprecated Node.js-related rules.", - plugin: { - name: "eslint-plugin-n", - url: "https://github.com/eslint-community/eslint-plugin-n", - }, - rule: { - name: "no-process-env", - url: "https://github.com/eslint-community/eslint-plugin-n/tree/master/docs/rules/no-process-env.md", - }, - }, - ], - }, + meta: { + deprecated: true, - type: "suggestion", + replacedBy: [], - docs: { - description: "Disallow the use of `process.env`", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-process-env", - }, + type: "suggestion", - schema: [], + docs: { + description: "Disallow the use of `process.env`", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-process-env" + }, - messages: { - unexpectedProcessEnv: "Unexpected use of process.env.", - }, - }, + schema: [], - create(context) { - return { - MemberExpression(node) { - const objectName = node.object.name, - propertyName = node.property.name; + messages: { + unexpectedProcessEnv: "Unexpected use of process.env." + } + }, - if ( - objectName === "process" && - !node.computed && - propertyName && - propertyName === "env" - ) { - context.report({ node, messageId: "unexpectedProcessEnv" }); - } - }, - }; - }, + create(context) { + + return { + + MemberExpression(node) { + const objectName = node.object.name, + propertyName = node.property.name; + + if (objectName === "process" && !node.computed && propertyName && propertyName === "env") { + context.report({ node, messageId: "unexpectedProcessEnv" }); + } + + } + + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-process-exit.js b/node_modules/eslint/lib/rules/no-process-exit.js index 79f8153a6..fa398a72e 100644 --- a/node_modules/eslint/lib/rules/no-process-exit.js +++ b/node_modules/eslint/lib/rules/no-process-exit.js @@ -9,59 +9,39 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Node.js rules were moved out of ESLint core.", - url: "https://eslint.org/docs/latest/use/migrating-to-7.0.0#deprecate-node-rules", - deprecatedSince: "7.0.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "eslint-plugin-n now maintains deprecated Node.js-related rules.", - plugin: { - name: "eslint-plugin-n", - url: "https://github.com/eslint-community/eslint-plugin-n", - }, - rule: { - name: "no-process-exit", - url: "https://github.com/eslint-community/eslint-plugin-n/tree/master/docs/rules/no-process-exit.md", - }, - }, - ], - }, + meta: { + deprecated: true, - type: "suggestion", + replacedBy: [], - docs: { - description: "Disallow the use of `process.exit()`", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-process-exit", - }, + type: "suggestion", - schema: [], + docs: { + description: "Disallow the use of `process.exit()`", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-process-exit" + }, - messages: { - noProcessExit: "Don't use process.exit(); throw an error instead.", - }, - }, + schema: [], - create(context) { - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- + messages: { + noProcessExit: "Don't use process.exit(); throw an error instead." + } + }, - return { - "CallExpression > MemberExpression.callee[object.name = 'process'][property.name = 'exit']"( - node, - ) { - context.report({ - node: node.parent, - messageId: "noProcessExit", - }); - }, - }; - }, + create(context) { + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + return { + "CallExpression > MemberExpression.callee[object.name = 'process'][property.name = 'exit']"(node) { + context.report({ node: node.parent, messageId: "noProcessExit" }); + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-promise-executor-return.js b/node_modules/eslint/lib/rules/no-promise-executor-return.js index 647f3ac08..b27e44072 100644 --- a/node_modules/eslint/lib/rules/no-promise-executor-return.js +++ b/node_modules/eslint/lib/rules/no-promise-executor-return.js @@ -9,33 +9,55 @@ // Requirements //------------------------------------------------------------------------------ +const { findVariable } = require("@eslint-community/eslint-utils"); const astUtils = require("./utils/ast-utils"); //------------------------------------------------------------------------------ // Helpers //------------------------------------------------------------------------------ -const functionTypesToCheck = new Set([ - "ArrowFunctionExpression", - "FunctionExpression", -]); +const functionTypesToCheck = new Set(["ArrowFunctionExpression", "FunctionExpression"]); + +/** + * Determines whether the given identifier node is a reference to a global variable. + * @param {ASTNode} node `Identifier` node to check. + * @param {Scope} scope Scope to which the node belongs. + * @returns {boolean} True if the identifier is a reference to a global variable. + */ +function isGlobalReference(node, scope) { + const variable = findVariable(scope, node); + + return variable !== null && variable.scope.type === "global" && variable.defs.length === 0; +} + +/** + * Finds function's outer scope. + * @param {Scope} scope Function's own scope. + * @returns {Scope} Function's outer scope. + */ +function getOuterScope(scope) { + const upper = scope.upper; + + if (upper.type === "function-expression-name") { + return upper.upper; + } + return upper; +} /** * Determines whether the given function node is used as a Promise executor. * @param {ASTNode} node The node to check. - * @param {SourceCode} sourceCode Source code to which the node belongs. + * @param {Scope} scope Function's own scope. * @returns {boolean} `true` if the node is a Promise executor. */ -function isPromiseExecutor(node, sourceCode) { - const parent = node.parent; - - return ( - parent.type === "NewExpression" && - parent.arguments[0] === node && - parent.callee.type === "Identifier" && - parent.callee.name === "Promise" && - sourceCode.isGlobalReference(parent.callee) - ); +function isPromiseExecutor(node, scope) { + const parent = node.parent; + + return parent.type === "NewExpression" && + parent.arguments[0] === node && + parent.callee.type === "Identifier" && + parent.callee.name === "Promise" && + isGlobalReference(parent.callee, getOuterScope(scope)); } /** @@ -44,7 +66,7 @@ function isPromiseExecutor(node, sourceCode) { * @returns {boolean} - `true` if the node is a void expression */ function expressionIsVoid(node) { - return node.type === "UnaryExpression" && node.operator === "void"; + return node.type === "UnaryExpression" && node.operator === "void"; } /** @@ -55,40 +77,39 @@ function expressionIsVoid(node) { * @returns {Array} - An array of fix objects to apply to the node. */ function voidPrependFixer(sourceCode, node, fixer) { - const requiresParens = - // prepending `void ` will fail if the node has a lower precedence than void - astUtils.getPrecedence(node) < - astUtils.getPrecedence({ - type: "UnaryExpression", - operator: "void", - }) && - // check if there are parentheses around the node to avoid redundant parentheses - !astUtils.isParenthesised(sourceCode, node); - - // avoid parentheses issues - const returnOrArrowToken = sourceCode.getTokenBefore( - node, - node.parent.type === "ArrowFunctionExpression" - ? astUtils.isArrowToken - : // isReturnToken - token => token.type === "Keyword" && token.value === "return", - ); - - const firstToken = sourceCode.getTokenAfter(returnOrArrowToken); - - const prependSpace = - // is return token, as => allows void to be adjacent - returnOrArrowToken.value === "return" && - // If two tokens (return and "(") are adjacent - returnOrArrowToken.range[1] === firstToken.range[0]; - - return [ - fixer.insertTextBefore( - firstToken, - `${prependSpace ? " " : ""}void ${requiresParens ? "(" : ""}`, - ), - fixer.insertTextAfter(node, requiresParens ? ")" : ""), - ]; + + const requiresParens = + + // prepending `void ` will fail if the node has a lower precedence than void + astUtils.getPrecedence(node) < astUtils.getPrecedence({ type: "UnaryExpression", operator: "void" }) && + + // check if there are parentheses around the node to avoid redundant parentheses + !astUtils.isParenthesised(sourceCode, node); + + // avoid parentheses issues + const returnOrArrowToken = sourceCode.getTokenBefore( + node, + node.parent.type === "ArrowFunctionExpression" + ? astUtils.isArrowToken + + // isReturnToken + : token => token.type === "Keyword" && token.value === "return" + ); + + const firstToken = sourceCode.getTokenAfter(returnOrArrowToken); + + const prependSpace = + + // is return token, as => allows void to be adjacent + returnOrArrowToken.value === "return" && + + // If two tokens (return and "(") are adjacent + returnOrArrowToken.range[1] === firstToken.range[0]; + + return [ + fixer.insertTextBefore(firstToken, `${prependSpace ? " " : ""}void ${requiresParens ? "(" : ""}`), + fixer.insertTextAfter(node, requiresParens ? ")" : "") + ]; } /** @@ -99,166 +120,144 @@ function voidPrependFixer(sourceCode, node, fixer) { * @returns {Array} - An array of fix objects to apply to the node. */ function curlyWrapFixer(sourceCode, node, fixer) { - // https://github.com/eslint/eslint/pull/17282#issuecomment-1592795923 - const arrowToken = sourceCode.getTokenBefore( - node.body, - astUtils.isArrowToken, - ); - const firstToken = sourceCode.getTokenAfter(arrowToken); - const lastToken = sourceCode.getLastToken(node); - - return [ - fixer.insertTextBefore(firstToken, "{"), - fixer.insertTextAfter(lastToken, "}"), - ]; + + // https://github.com/eslint/eslint/pull/17282#issuecomment-1592795923 + const arrowToken = sourceCode.getTokenBefore(node.body, astUtils.isArrowToken); + const firstToken = sourceCode.getTokenAfter(arrowToken); + const lastToken = sourceCode.getLastToken(node); + + return [ + fixer.insertTextBefore(firstToken, "{"), + fixer.insertTextAfter(lastToken, "}") + ]; } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - defaultOptions: [ - { - allowVoid: false, - }, - ], - - docs: { - description: - "Disallow returning values from Promise executor functions", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-promise-executor-return", - }, - - hasSuggestions: true, - - schema: [ - { - type: "object", - properties: { - allowVoid: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - - messages: { - returnsValue: - "Return values from promise executor functions cannot be read.", - - // arrow and function suggestions - prependVoid: "Prepend `void` to the expression.", - - // only arrow suggestions - wrapBraces: "Wrap the expression in `{}`.", - }, - }, - - create(context) { - let funcInfo = null; - const sourceCode = context.sourceCode; - const [{ allowVoid }] = context.options; - - return { - onCodePathStart(_, node) { - funcInfo = { - upper: funcInfo, - shouldCheck: - functionTypesToCheck.has(node.type) && - isPromiseExecutor(node, sourceCode), - }; - - if ( - // Is a Promise executor - funcInfo.shouldCheck && - node.type === "ArrowFunctionExpression" && - node.expression && - // Except void - !(allowVoid && expressionIsVoid(node.body)) - ) { - const suggest = []; - - // prevent useless refactors - if (allowVoid) { - suggest.push({ - messageId: "prependVoid", - fix(fixer) { - return voidPrependFixer( - sourceCode, - node.body, - fixer, - ); - }, - }); - } - - // Do not suggest wrapping an unnamed FunctionExpression in braces as that would be invalid syntax. - if ( - !( - node.body.type === "FunctionExpression" && - !node.body.id - ) - ) { - suggest.push({ - messageId: "wrapBraces", - fix(fixer) { - return curlyWrapFixer(sourceCode, node, fixer); - }, - }); - } - - context.report({ - node: node.body, - messageId: "returnsValue", - suggest, - }); - } - }, - - onCodePathEnd() { - funcInfo = funcInfo.upper; - }, - - ReturnStatement(node) { - if (!(funcInfo.shouldCheck && node.argument)) { - return; - } - - // node is `return ` - if (!allowVoid) { - context.report({ node, messageId: "returnsValue" }); - return; - } - - if (expressionIsVoid(node.argument)) { - return; - } - - // allowVoid && !expressionIsVoid - context.report({ - node, - messageId: "returnsValue", - suggest: [ - { - messageId: "prependVoid", - fix(fixer) { - return voidPrependFixer( - sourceCode, - node.argument, - fixer, - ); - }, - }, - ], - }); - }, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow returning values from Promise executor functions", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-promise-executor-return" + }, + + hasSuggestions: true, + + schema: [{ + type: "object", + properties: { + allowVoid: { + type: "boolean", + default: false + } + }, + additionalProperties: false + }], + + messages: { + returnsValue: "Return values from promise executor functions cannot be read.", + + // arrow and function suggestions + prependVoid: "Prepend `void` to the expression.", + + // only arrow suggestions + wrapBraces: "Wrap the expression in `{}`." + } + }, + + create(context) { + + let funcInfo = null; + const sourceCode = context.sourceCode; + const { + allowVoid = false + } = context.options[0] || {}; + + return { + + onCodePathStart(_, node) { + funcInfo = { + upper: funcInfo, + shouldCheck: + functionTypesToCheck.has(node.type) && + isPromiseExecutor(node, sourceCode.getScope(node)) + }; + + if (// Is a Promise executor + funcInfo.shouldCheck && + node.type === "ArrowFunctionExpression" && + node.expression && + + // Except void + !(allowVoid && expressionIsVoid(node.body)) + ) { + const suggest = []; + + // prevent useless refactors + if (allowVoid) { + suggest.push({ + messageId: "prependVoid", + fix(fixer) { + return voidPrependFixer(sourceCode, node.body, fixer); + } + }); + } + + // Do not suggest wrapping an unnamed FunctionExpression in braces as that would be invalid syntax. + if (!(node.body.type === "FunctionExpression" && !node.body.id)) { + suggest.push({ + messageId: "wrapBraces", + fix(fixer) { + return curlyWrapFixer(sourceCode, node, fixer); + } + }); + } + + context.report({ + node: node.body, + messageId: "returnsValue", + suggest + }); + } + }, + + onCodePathEnd() { + funcInfo = funcInfo.upper; + }, + + ReturnStatement(node) { + if (!(funcInfo.shouldCheck && node.argument)) { + return; + } + + // node is `return ` + if (!allowVoid) { + context.report({ node, messageId: "returnsValue" }); + return; + } + + if (expressionIsVoid(node.argument)) { + return; + } + + // allowVoid && !expressionIsVoid + context.report({ + node, + messageId: "returnsValue", + suggest: [{ + messageId: "prependVoid", + fix(fixer) { + return voidPrependFixer(sourceCode, node.argument, fixer); + } + }] + }); + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-proto.js b/node_modules/eslint/lib/rules/no-proto.js index d68509e11..28320d5d5 100644 --- a/node_modules/eslint/lib/rules/no-proto.js +++ b/node_modules/eslint/lib/rules/no-proto.js @@ -15,31 +15,34 @@ const { getStaticPropertyName } = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: "Disallow the use of the `__proto__` property", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-proto", - }, - - schema: [], - - messages: { - unexpectedProto: "The '__proto__' property is deprecated.", - }, - }, - - create(context) { - return { - MemberExpression(node) { - if (getStaticPropertyName(node) === "__proto__") { - context.report({ node, messageId: "unexpectedProto" }); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow the use of the `__proto__` property", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-proto" + }, + + schema: [], + + messages: { + unexpectedProto: "The '__proto__' property is deprecated." + } + }, + + create(context) { + + return { + + MemberExpression(node) { + if (getStaticPropertyName(node) === "__proto__") { + context.report({ node, messageId: "unexpectedProto" }); + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-prototype-builtins.js b/node_modules/eslint/lib/rules/no-prototype-builtins.js index c75fdacf8..b61e58529 100644 --- a/node_modules/eslint/lib/rules/no-prototype-builtins.js +++ b/node_modules/eslint/lib/rules/no-prototype-builtins.js @@ -25,157 +25,135 @@ const astUtils = require("./utils/ast-utils"); * or the node itself is an optional call or member `?.`. */ function isAfterOptional(node) { - let leftNode; - - if (node.type === "MemberExpression") { - leftNode = node.object; - } else if (node.type === "CallExpression") { - leftNode = node.callee; - } else { - return false; - } - if (node.optional) { - return true; - } - return isAfterOptional(leftNode); + let leftNode; + + if (node.type === "MemberExpression") { + leftNode = node.object; + } else if (node.type === "CallExpression") { + leftNode = node.callee; + } else { + return false; + } + if (node.optional) { + return true; + } + return isAfterOptional(leftNode); } + //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - docs: { - description: - "Disallow calling some `Object.prototype` methods directly on objects", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-prototype-builtins", - }, - - hasSuggestions: true, - - schema: [], - - messages: { - prototypeBuildIn: - "Do not access Object.prototype method '{{prop}}' from target object.", - callObjectPrototype: "Call Object.prototype.{{prop}} explicitly.", - }, - }, - - create(context) { - const DISALLOWED_PROPS = new Set([ - "hasOwnProperty", - "isPrototypeOf", - "propertyIsEnumerable", - ]); - - /** - * Reports if a disallowed property is used in a CallExpression - * @param {ASTNode} node The CallExpression node. - * @returns {void} - */ - function disallowBuiltIns(node) { - const callee = astUtils.skipChainExpression(node.callee); - - if (callee.type !== "MemberExpression") { - return; - } - - const propName = astUtils.getStaticPropertyName(callee); - - if (propName !== null && DISALLOWED_PROPS.has(propName)) { - context.report({ - messageId: "prototypeBuildIn", - loc: callee.property.loc, - data: { prop: propName }, - node, - suggest: [ - { - messageId: "callObjectPrototype", - data: { prop: propName }, - fix(fixer) { - const sourceCode = context.sourceCode; - - /* - * A call after an optional chain (e.g. a?.b.hasOwnProperty(c)) - * must be fixed manually because the call can be short-circuited - */ - if (isAfterOptional(node)) { - return null; - } - - /* - * A call on a ChainExpression (e.g. (a?.hasOwnProperty)(c)) will trigger - * no-unsafe-optional-chaining which should be fixed before this suggestion - */ - if (node.callee.type === "ChainExpression") { - return null; - } - - const objectVariable = - astUtils.getVariableByName( - sourceCode.getScope(node), - "Object", - ); - - /* - * We can't use Object if the global Object was shadowed, - * or Object does not exist in the global scope for some reason - */ - if ( - !objectVariable || - objectVariable.scope.type !== "global" || - objectVariable.defs.length > 0 - ) { - return null; - } - - let objectText = sourceCode.getText( - callee.object, - ); - - if ( - astUtils.getPrecedence(callee.object) <= - astUtils.getPrecedence({ - type: "SequenceExpression", - }) - ) { - objectText = `(${objectText})`; - } - - const openParenToken = sourceCode.getTokenAfter( - node.callee, - astUtils.isOpeningParenToken, - ); - const isEmptyParameters = - node.arguments.length === 0; - const delim = isEmptyParameters ? "" : ", "; - const fixes = [ - fixer.replaceText( - callee, - `Object.prototype.${propName}.call`, - ), - fixer.insertTextAfter( - openParenToken, - objectText + delim, - ), - ]; - - return fixes; - }, - }, - ], - }); - } - } - - return { - CallExpression: disallowBuiltIns, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow calling some `Object.prototype` methods directly on objects", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-prototype-builtins" + }, + + hasSuggestions: true, + + schema: [], + + messages: { + prototypeBuildIn: "Do not access Object.prototype method '{{prop}}' from target object.", + callObjectPrototype: "Call Object.prototype.{{prop}} explicitly." + } + }, + + create(context) { + const DISALLOWED_PROPS = new Set([ + "hasOwnProperty", + "isPrototypeOf", + "propertyIsEnumerable" + ]); + + /** + * Reports if a disallowed property is used in a CallExpression + * @param {ASTNode} node The CallExpression node. + * @returns {void} + */ + function disallowBuiltIns(node) { + + const callee = astUtils.skipChainExpression(node.callee); + + if (callee.type !== "MemberExpression") { + return; + } + + const propName = astUtils.getStaticPropertyName(callee); + + if (propName !== null && DISALLOWED_PROPS.has(propName)) { + context.report({ + messageId: "prototypeBuildIn", + loc: callee.property.loc, + data: { prop: propName }, + node, + suggest: [ + { + messageId: "callObjectPrototype", + data: { prop: propName }, + fix(fixer) { + const sourceCode = context.sourceCode; + + /* + * A call after an optional chain (e.g. a?.b.hasOwnProperty(c)) + * must be fixed manually because the call can be short-circuited + */ + if (isAfterOptional(node)) { + return null; + } + + /* + * A call on a ChainExpression (e.g. (a?.hasOwnProperty)(c)) will trigger + * no-unsafe-optional-chaining which should be fixed before this suggestion + */ + if (node.callee.type === "ChainExpression") { + return null; + } + + const objectVariable = astUtils.getVariableByName(sourceCode.getScope(node), "Object"); + + /* + * We can't use Object if the global Object was shadowed, + * or Object does not exist in the global scope for some reason + */ + if (!objectVariable || objectVariable.scope.type !== "global" || objectVariable.defs.length > 0) { + return null; + } + + let objectText = sourceCode.getText(callee.object); + + if (astUtils.getPrecedence(callee.object) <= astUtils.getPrecedence({ type: "SequenceExpression" })) { + objectText = `(${objectText})`; + } + + const openParenToken = sourceCode.getTokenAfter( + node.callee, + astUtils.isOpeningParenToken + ); + const isEmptyParameters = node.arguments.length === 0; + const delim = isEmptyParameters ? "" : ", "; + const fixes = [ + fixer.replaceText(callee, `Object.prototype.${propName}.call`), + fixer.insertTextAfter(openParenToken, objectText + delim) + ]; + + return fixes; + } + } + ] + }); + } + } + + return { + CallExpression: disallowBuiltIns + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-redeclare.js b/node_modules/eslint/lib/rules/no-redeclare.js index 4f8c06c97..8a4877e8a 100644 --- a/node_modules/eslint/lib/rules/no-redeclare.js +++ b/node_modules/eslint/lib/rules/no-redeclare.js @@ -15,159 +15,160 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - defaultOptions: [{ builtinGlobals: true }], - - docs: { - description: "Disallow variable redeclaration", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-redeclare", - }, - - messages: { - redeclared: "'{{id}}' is already defined.", - redeclaredAsBuiltin: - "'{{id}}' is already defined as a built-in global variable.", - redeclaredBySyntax: - "'{{id}}' is already defined by a variable declaration.", - }, - - schema: [ - { - type: "object", - properties: { - builtinGlobals: { type: "boolean" }, - }, - additionalProperties: false, - }, - ], - }, - - create(context) { - const [{ builtinGlobals }] = context.options; - const sourceCode = context.sourceCode; - - /** - * Iterate declarations of a given variable. - * @param {escope.variable} variable The variable object to iterate declarations. - * @returns {IterableIterator<{type:string,node:ASTNode,loc:SourceLocation}>} The declarations. - */ - function* iterateDeclarations(variable) { - if ( - builtinGlobals && - (variable.eslintImplicitGlobalSetting === "readonly" || - variable.eslintImplicitGlobalSetting === "writable") - ) { - yield { type: "builtin" }; - } - - for (const id of variable.identifiers) { - yield { type: "syntax", node: id, loc: id.loc }; - } - - if (variable.eslintExplicitGlobalComments) { - for (const comment of variable.eslintExplicitGlobalComments) { - yield { - type: "comment", - node: comment, - loc: astUtils.getNameLocationInGlobalDirectiveComment( - sourceCode, - comment, - variable.name, - ), - }; - } - } - } - - /** - * Find variables in a given scope and flag redeclared ones. - * @param {Scope} scope An eslint-scope scope object. - * @returns {void} - * @private - */ - function findVariablesInScope(scope) { - for (const variable of scope.variables) { - const [declaration, ...extraDeclarations] = - iterateDeclarations(variable); - - if (extraDeclarations.length === 0) { - continue; - } - - /* - * If the type of a declaration is different from the type of - * the first declaration, it shows the location of the first - * declaration. - */ - const detailMessageId = - declaration.type === "builtin" - ? "redeclaredAsBuiltin" - : "redeclaredBySyntax"; - const data = { id: variable.name }; - - // Report extra declarations. - for (const { type, node, loc } of extraDeclarations) { - const messageId = - type === declaration.type - ? "redeclared" - : detailMessageId; - - context.report({ node, loc, messageId, data }); - } - } - } - - /** - * Find variables in the current scope. - * @param {ASTNode} node The node of the current scope. - * @returns {void} - * @private - */ - function checkForBlock(node) { - const scope = sourceCode.getScope(node); - - /* - * In ES5, some node type such as `BlockStatement` doesn't have that scope. - * `scope.block` is a different node in such a case. - */ - if (scope.block === node) { - findVariablesInScope(scope); - } - } - - return { - Program(node) { - const scope = sourceCode.getScope(node); - - findVariablesInScope(scope); - - // Node.js or ES modules has a special scope. - if ( - scope.type === "global" && - scope.childScopes[0] && - // The special scope's block is the Program node. - scope.block === scope.childScopes[0].block - ) { - findVariablesInScope(scope.childScopes[0]); - } - }, - - FunctionDeclaration: checkForBlock, - FunctionExpression: checkForBlock, - ArrowFunctionExpression: checkForBlock, - - StaticBlock: checkForBlock, - - BlockStatement: checkForBlock, - ForStatement: checkForBlock, - ForInStatement: checkForBlock, - ForOfStatement: checkForBlock, - SwitchStatement: checkForBlock, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow variable redeclaration", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-redeclare" + }, + + messages: { + redeclared: "'{{id}}' is already defined.", + redeclaredAsBuiltin: "'{{id}}' is already defined as a built-in global variable.", + redeclaredBySyntax: "'{{id}}' is already defined by a variable declaration." + }, + + schema: [ + { + type: "object", + properties: { + builtinGlobals: { type: "boolean", default: true } + }, + additionalProperties: false + } + ] + }, + + create(context) { + const options = { + builtinGlobals: Boolean( + context.options.length === 0 || + context.options[0].builtinGlobals + ) + }; + const sourceCode = context.sourceCode; + + /** + * Iterate declarations of a given variable. + * @param {escope.variable} variable The variable object to iterate declarations. + * @returns {IterableIterator<{type:string,node:ASTNode,loc:SourceLocation}>} The declarations. + */ + function *iterateDeclarations(variable) { + if (options.builtinGlobals && ( + variable.eslintImplicitGlobalSetting === "readonly" || + variable.eslintImplicitGlobalSetting === "writable" + )) { + yield { type: "builtin" }; + } + + for (const id of variable.identifiers) { + yield { type: "syntax", node: id, loc: id.loc }; + } + + if (variable.eslintExplicitGlobalComments) { + for (const comment of variable.eslintExplicitGlobalComments) { + yield { + type: "comment", + node: comment, + loc: astUtils.getNameLocationInGlobalDirectiveComment( + sourceCode, + comment, + variable.name + ) + }; + } + } + } + + /** + * Find variables in a given scope and flag redeclared ones. + * @param {Scope} scope An eslint-scope scope object. + * @returns {void} + * @private + */ + function findVariablesInScope(scope) { + for (const variable of scope.variables) { + const [ + declaration, + ...extraDeclarations + ] = iterateDeclarations(variable); + + if (extraDeclarations.length === 0) { + continue; + } + + /* + * If the type of a declaration is different from the type of + * the first declaration, it shows the location of the first + * declaration. + */ + const detailMessageId = declaration.type === "builtin" + ? "redeclaredAsBuiltin" + : "redeclaredBySyntax"; + const data = { id: variable.name }; + + // Report extra declarations. + for (const { type, node, loc } of extraDeclarations) { + const messageId = type === declaration.type + ? "redeclared" + : detailMessageId; + + context.report({ node, loc, messageId, data }); + } + } + } + + /** + * Find variables in the current scope. + * @param {ASTNode} node The node of the current scope. + * @returns {void} + * @private + */ + function checkForBlock(node) { + const scope = sourceCode.getScope(node); + + /* + * In ES5, some node type such as `BlockStatement` doesn't have that scope. + * `scope.block` is a different node in such a case. + */ + if (scope.block === node) { + findVariablesInScope(scope); + } + } + + return { + Program(node) { + const scope = sourceCode.getScope(node); + + findVariablesInScope(scope); + + // Node.js or ES modules has a special scope. + if ( + scope.type === "global" && + scope.childScopes[0] && + + // The special scope's block is the Program node. + scope.block === scope.childScopes[0].block + ) { + findVariablesInScope(scope.childScopes[0]); + } + }, + + FunctionDeclaration: checkForBlock, + FunctionExpression: checkForBlock, + ArrowFunctionExpression: checkForBlock, + + StaticBlock: checkForBlock, + + BlockStatement: checkForBlock, + ForStatement: checkForBlock, + ForInStatement: checkForBlock, + ForOfStatement: checkForBlock, + SwitchStatement: checkForBlock + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-regex-spaces.js b/node_modules/eslint/lib/rules/no-regex-spaces.js index 719240975..cb2501072 100644 --- a/node_modules/eslint/lib/rules/no-regex-spaces.js +++ b/node_modules/eslint/lib/rules/no-regex-spaces.js @@ -26,194 +26,172 @@ const DOUBLE_SPACE = / {2}/u; * @private */ function isString(node) { - return node && node.type === "Literal" && typeof node.value === "string"; + return node && node.type === "Literal" && typeof node.value === "string"; } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: "Disallow multiple spaces in regular expressions", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-regex-spaces", - }, - - schema: [], - fixable: "code", - - messages: { - multipleSpaces: "Spaces are hard to count. Use {{{length}}}.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - /** - * Validate regular expression - * @param {ASTNode} nodeToReport Node to report. - * @param {string} pattern Regular expression pattern to validate. - * @param {string} rawPattern Raw representation of the pattern in the source code. - * @param {number} rawPatternStartRange Start range of the pattern in the source code. - * @param {string} flags Regular expression flags. - * @returns {void} - * @private - */ - function checkRegex( - nodeToReport, - pattern, - rawPattern, - rawPatternStartRange, - flags, - ) { - // Skip if there are no consecutive spaces in the source code, to avoid reporting e.g., RegExp(' \ '). - if (!DOUBLE_SPACE.test(rawPattern)) { - return; - } - - const characterClassNodes = []; - let regExpAST; - - try { - regExpAST = regExpParser.parsePattern( - pattern, - 0, - pattern.length, - { - unicode: flags.includes("u"), - unicodeSets: flags.includes("v"), - }, - ); - } catch { - // Ignore regular expressions with syntax errors - return; - } - - regexpp.visitRegExpAST(regExpAST, { - onCharacterClassEnter(ccNode) { - characterClassNodes.push(ccNode); - }, - }); - - const spacesPattern = /( {2,})(?: [+*{?]|[^+*{?]|$)/gu; - let match; - - while ((match = spacesPattern.exec(pattern))) { - const { - 1: { length }, - index, - } = match; - - // Report only consecutive spaces that are not in character classes. - if ( - characterClassNodes.every( - ({ start, end }) => index < start || end <= index, - ) - ) { - context.report({ - node: nodeToReport, - messageId: "multipleSpaces", - data: { length }, - fix(fixer) { - if (pattern !== rawPattern) { - return null; - } - return fixer.replaceTextRange( - [ - rawPatternStartRange + index, - rawPatternStartRange + index + length, - ], - ` {${length}}`, - ); - }, - }); - - // Report only the first occurrence of consecutive spaces - return; - } - } - } - - /** - * Validate regular expression literals - * @param {ASTNode} node node to validate - * @returns {void} - * @private - */ - function checkLiteral(node) { - if (node.regex) { - const pattern = node.regex.pattern; - const rawPattern = node.raw.slice(1, node.raw.lastIndexOf("/")); - const rawPatternStartRange = node.range[0] + 1; - const flags = node.regex.flags; - - checkRegex( - node, - pattern, - rawPattern, - rawPatternStartRange, - flags, - ); - } - } - - /** - * Validate strings passed to the RegExp constructor - * @param {ASTNode} node node to validate - * @returns {void} - * @private - */ - function checkFunction(node) { - const scope = sourceCode.getScope(node); - const regExpVar = astUtils.getVariableByName(scope, "RegExp"); - const shadowed = regExpVar && regExpVar.defs.length > 0; - const patternNode = node.arguments[0]; - - if ( - node.callee.type === "Identifier" && - node.callee.name === "RegExp" && - isString(patternNode) && - !shadowed - ) { - const pattern = patternNode.value; - const rawPattern = patternNode.raw.slice(1, -1); - const rawPatternStartRange = patternNode.range[0] + 1; - let flags; - - if (node.arguments.length < 2) { - // It has no flags. - flags = ""; - } else { - const flagsNode = node.arguments[1]; - - if (isString(flagsNode)) { - flags = flagsNode.value; - } else { - // The flags cannot be determined. - return; - } - } - - checkRegex( - node, - pattern, - rawPattern, - rawPatternStartRange, - flags, - ); - } - } - - return { - Literal: checkLiteral, - CallExpression: checkFunction, - NewExpression: checkFunction, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow multiple spaces in regular expressions", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-regex-spaces" + }, + + schema: [], + fixable: "code", + + messages: { + multipleSpaces: "Spaces are hard to count. Use {{{length}}}." + } + }, + + create(context) { + + const sourceCode = context.sourceCode; + + /** + * Validate regular expression + * @param {ASTNode} nodeToReport Node to report. + * @param {string} pattern Regular expression pattern to validate. + * @param {string} rawPattern Raw representation of the pattern in the source code. + * @param {number} rawPatternStartRange Start range of the pattern in the source code. + * @param {string} flags Regular expression flags. + * @returns {void} + * @private + */ + function checkRegex(nodeToReport, pattern, rawPattern, rawPatternStartRange, flags) { + + // Skip if there are no consecutive spaces in the source code, to avoid reporting e.g., RegExp(' \ '). + if (!DOUBLE_SPACE.test(rawPattern)) { + return; + } + + const characterClassNodes = []; + let regExpAST; + + try { + regExpAST = regExpParser.parsePattern(pattern, 0, pattern.length, { unicode: flags.includes("u"), unicodeSets: flags.includes("v") }); + } catch { + + // Ignore regular expressions with syntax errors + return; + } + + regexpp.visitRegExpAST(regExpAST, { + onCharacterClassEnter(ccNode) { + characterClassNodes.push(ccNode); + } + }); + + const spacesPattern = /( {2,})(?: [+*{?]|[^+*{?]|$)/gu; + let match; + + while ((match = spacesPattern.exec(pattern))) { + const { 1: { length }, index } = match; + + // Report only consecutive spaces that are not in character classes. + if ( + characterClassNodes.every(({ start, end }) => index < start || end <= index) + ) { + context.report({ + node: nodeToReport, + messageId: "multipleSpaces", + data: { length }, + fix(fixer) { + if (pattern !== rawPattern) { + return null; + } + return fixer.replaceTextRange( + [rawPatternStartRange + index, rawPatternStartRange + index + length], + ` {${length}}` + ); + } + }); + + // Report only the first occurrence of consecutive spaces + return; + } + } + } + + /** + * Validate regular expression literals + * @param {ASTNode} node node to validate + * @returns {void} + * @private + */ + function checkLiteral(node) { + if (node.regex) { + const pattern = node.regex.pattern; + const rawPattern = node.raw.slice(1, node.raw.lastIndexOf("/")); + const rawPatternStartRange = node.range[0] + 1; + const flags = node.regex.flags; + + checkRegex( + node, + pattern, + rawPattern, + rawPatternStartRange, + flags + ); + } + } + + /** + * Validate strings passed to the RegExp constructor + * @param {ASTNode} node node to validate + * @returns {void} + * @private + */ + function checkFunction(node) { + const scope = sourceCode.getScope(node); + const regExpVar = astUtils.getVariableByName(scope, "RegExp"); + const shadowed = regExpVar && regExpVar.defs.length > 0; + const patternNode = node.arguments[0]; + + if (node.callee.type === "Identifier" && node.callee.name === "RegExp" && isString(patternNode) && !shadowed) { + const pattern = patternNode.value; + const rawPattern = patternNode.raw.slice(1, -1); + const rawPatternStartRange = patternNode.range[0] + 1; + let flags; + + if (node.arguments.length < 2) { + + // It has no flags. + flags = ""; + } else { + const flagsNode = node.arguments[1]; + + if (isString(flagsNode)) { + flags = flagsNode.value; + } else { + + // The flags cannot be determined. + return; + } + } + + checkRegex( + node, + pattern, + rawPattern, + rawPatternStartRange, + flags + ); + } + } + + return { + Literal: checkLiteral, + CallExpression: checkFunction, + NewExpression: checkFunction + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-restricted-exports.js b/node_modules/eslint/lib/rules/no-restricted-exports.js index fd5ff4b5a..a1d54b085 100644 --- a/node_modules/eslint/lib/rules/no-restricted-exports.js +++ b/node_modules/eslint/lib/rules/no-restricted-exports.js @@ -15,213 +15,179 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: "Disallow specified names in exports", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-restricted-exports", - }, - - schema: [ - { - anyOf: [ - { - type: "object", - properties: { - restrictedNamedExports: { - type: "array", - items: { - type: "string", - }, - uniqueItems: true, - }, - restrictedNamedExportsPattern: { type: "string" }, - }, - additionalProperties: false, - }, - { - type: "object", - properties: { - restrictedNamedExports: { - type: "array", - items: { - type: "string", - pattern: "^(?!default$)", - }, - uniqueItems: true, - }, - restrictedNamedExportsPattern: { type: "string" }, - restrictDefaultExports: { - type: "object", - properties: { - // Allow/Disallow `export default foo; export default 42; export default function foo() {}` format - direct: { - type: "boolean", - }, - - // Allow/Disallow `export { foo as default };` declarations - named: { - type: "boolean", - }, - - // Allow/Disallow `export { default } from "mod"; export { default as default } from "mod";` declarations - defaultFrom: { - type: "boolean", - }, - - // Allow/Disallow `export { foo as default } from "mod";` declarations - namedFrom: { - type: "boolean", - }, - - // Allow/Disallow `export * as default from "mod"`; declarations - namespaceFrom: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - }, - additionalProperties: false, - }, - ], - }, - ], - - messages: { - restrictedNamed: - "'{{name}}' is restricted from being used as an exported name.", - restrictedDefault: "Exporting 'default' is restricted.", - }, - }, - - create(context) { - const restrictedNames = new Set( - context.options[0] && context.options[0].restrictedNamedExports, - ); - const restrictedNamePattern = - context.options[0] && - context.options[0].restrictedNamedExportsPattern; - const restrictDefaultExports = - context.options[0] && context.options[0].restrictDefaultExports; - const sourceCode = context.sourceCode; - - /** - * Checks and reports given exported name. - * @param {ASTNode} node exported `Identifier` or string `Literal` node to check. - * @returns {void} - */ - function checkExportedName(node) { - const name = astUtils.getModuleExportName(node); - - let matchesRestrictedNamePattern = false; - - if (restrictedNamePattern && name !== "default") { - const patternRegex = new RegExp(restrictedNamePattern, "u"); - - matchesRestrictedNamePattern = patternRegex.test(name); - } - - if (matchesRestrictedNamePattern || restrictedNames.has(name)) { - context.report({ - node, - messageId: "restrictedNamed", - data: { name }, - }); - return; - } - - if (name === "default") { - if (node.parent.type === "ExportAllDeclaration") { - if ( - restrictDefaultExports && - restrictDefaultExports.namespaceFrom - ) { - context.report({ - node, - messageId: "restrictedDefault", - }); - } - } else { - // ExportSpecifier - const isSourceSpecified = !!node.parent.parent.source; - const specifierLocalName = astUtils.getModuleExportName( - node.parent.local, - ); - - if ( - !isSourceSpecified && - restrictDefaultExports && - restrictDefaultExports.named - ) { - context.report({ - node, - messageId: "restrictedDefault", - }); - return; - } - - if (isSourceSpecified && restrictDefaultExports) { - if ( - (specifierLocalName === "default" && - restrictDefaultExports.defaultFrom) || - (specifierLocalName !== "default" && - restrictDefaultExports.namedFrom) - ) { - context.report({ - node, - messageId: "restrictedDefault", - }); - } - } - } - } - } - - return { - ExportAllDeclaration(node) { - if (node.exported) { - checkExportedName(node.exported); - } - }, - - ExportDefaultDeclaration(node) { - if (restrictDefaultExports && restrictDefaultExports.direct) { - context.report({ - node, - messageId: "restrictedDefault", - }); - } - }, - - ExportNamedDeclaration(node) { - const declaration = node.declaration; - - if (declaration) { - if ( - declaration.type === "FunctionDeclaration" || - declaration.type === "ClassDeclaration" - ) { - checkExportedName(declaration.id); - } else if (declaration.type === "VariableDeclaration") { - sourceCode - .getDeclaredVariables(declaration) - .map(v => - v.defs.find(d => d.parent === declaration), - ) - .map(d => d.name) // Identifier nodes - .forEach(checkExportedName); - } - } else { - node.specifiers - .map(s => s.exported) - .forEach(checkExportedName); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow specified names in exports", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-restricted-exports" + }, + + schema: [{ + anyOf: [ + { + type: "object", + properties: { + restrictedNamedExports: { + type: "array", + items: { + type: "string" + }, + uniqueItems: true + } + }, + additionalProperties: false + }, + { + type: "object", + properties: { + restrictedNamedExports: { + type: "array", + items: { + type: "string", + pattern: "^(?!default$)" + }, + uniqueItems: true + }, + restrictDefaultExports: { + type: "object", + properties: { + + // Allow/Disallow `export default foo; export default 42; export default function foo() {}` format + direct: { + type: "boolean" + }, + + // Allow/Disallow `export { foo as default };` declarations + named: { + type: "boolean" + }, + + // Allow/Disallow `export { default } from "mod"; export { default as default } from "mod";` declarations + defaultFrom: { + type: "boolean" + }, + + // Allow/Disallow `export { foo as default } from "mod";` declarations + namedFrom: { + type: "boolean" + }, + + // Allow/Disallow `export * as default from "mod"`; declarations + namespaceFrom: { + type: "boolean" + } + }, + additionalProperties: false + } + }, + additionalProperties: false + } + ] + }], + + messages: { + restrictedNamed: "'{{name}}' is restricted from being used as an exported name.", + restrictedDefault: "Exporting 'default' is restricted." + } + }, + + create(context) { + + const restrictedNames = new Set(context.options[0] && context.options[0].restrictedNamedExports); + const restrictDefaultExports = context.options[0] && context.options[0].restrictDefaultExports; + const sourceCode = context.sourceCode; + + /** + * Checks and reports given exported name. + * @param {ASTNode} node exported `Identifier` or string `Literal` node to check. + * @returns {void} + */ + function checkExportedName(node) { + const name = astUtils.getModuleExportName(node); + + if (restrictedNames.has(name)) { + context.report({ + node, + messageId: "restrictedNamed", + data: { name } + }); + return; + } + + if (name === "default") { + if (node.parent.type === "ExportAllDeclaration") { + if (restrictDefaultExports && restrictDefaultExports.namespaceFrom) { + context.report({ + node, + messageId: "restrictedDefault" + }); + } + + } else { // ExportSpecifier + const isSourceSpecified = !!node.parent.parent.source; + const specifierLocalName = astUtils.getModuleExportName(node.parent.local); + + if (!isSourceSpecified && restrictDefaultExports && restrictDefaultExports.named) { + context.report({ + node, + messageId: "restrictedDefault" + }); + return; + } + + if (isSourceSpecified && restrictDefaultExports) { + if ( + (specifierLocalName === "default" && restrictDefaultExports.defaultFrom) || + (specifierLocalName !== "default" && restrictDefaultExports.namedFrom) + ) { + context.report({ + node, + messageId: "restrictedDefault" + }); + } + } + } + } + } + + return { + ExportAllDeclaration(node) { + if (node.exported) { + checkExportedName(node.exported); + } + }, + + ExportDefaultDeclaration(node) { + if (restrictDefaultExports && restrictDefaultExports.direct) { + context.report({ + node, + messageId: "restrictedDefault" + }); + } + }, + + ExportNamedDeclaration(node) { + const declaration = node.declaration; + + if (declaration) { + if (declaration.type === "FunctionDeclaration" || declaration.type === "ClassDeclaration") { + checkExportedName(declaration.id); + } else if (declaration.type === "VariableDeclaration") { + sourceCode.getDeclaredVariables(declaration) + .map(v => v.defs.find(d => d.parent === declaration)) + .map(d => d.name) // Identifier nodes + .forEach(checkExportedName); + } + } else { + node.specifiers + .map(s => s.exported) + .forEach(checkExportedName); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-restricted-globals.js b/node_modules/eslint/lib/rules/no-restricted-globals.js index a883988c4..919a8ee0a 100644 --- a/node_modules/eslint/lib/rules/no-restricted-globals.js +++ b/node_modules/eslint/lib/rules/no-restricted-globals.js @@ -4,263 +4,121 @@ */ "use strict"; -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ - -const astUtils = require("./utils/ast-utils"); - -//------------------------------------------------------------------------------ -// Helpers -//------------------------------------------------------------------------------ - -const TYPE_NODES = new Set([ - "TSTypeReference", - "TSInterfaceHeritage", - "TSClassImplements", - "TSTypeQuery", - "TSQualifiedName", -]); - -const GLOBAL_OBJECTS = new Set(["globalThis", "self", "window"]); - //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -const arrayOfGlobals = { - type: "array", - items: { - oneOf: [ - { - type: "string", - }, - { - type: "object", - properties: { - name: { type: "string" }, - message: { type: "string" }, - }, - required: ["name"], - additionalProperties: false, - }, - ], - }, - uniqueItems: true, - minItems: 0, -}; - -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - dialects: ["javascript", "typescript"], - language: "javascript", - type: "suggestion", - - docs: { - description: "Disallow specified global variables", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-restricted-globals", - }, - - schema: { - anyOf: [ - arrayOfGlobals, - { - type: "array", - items: [ - { - type: "object", - properties: { - globals: arrayOfGlobals, - checkGlobalObject: { - type: "boolean", - }, - globalObjects: { - type: "array", - items: { - type: "string", - }, - uniqueItems: true, - }, - }, - required: ["globals"], - additionalProperties: false, - }, - ], - additionalItems: false, - }, - ], - }, - - messages: { - defaultMessage: "Unexpected use of '{{name}}'.", - // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period - customMessage: "Unexpected use of '{{name}}'. {{customMessage}}", - }, - }, - - create(context) { - const { sourceCode, options } = context; - - const isGlobalsObject = - typeof options[0] === "object" && - Object.hasOwn(options[0], "globals"); - - const restrictedGlobals = isGlobalsObject - ? options[0].globals - : options; - const checkGlobalObject = isGlobalsObject - ? options[0].checkGlobalObject - : false; - const userGlobalObjects = isGlobalsObject - ? options[0].globalObjects || [] - : []; - - const globalObjects = new Set([ - ...GLOBAL_OBJECTS, - ...userGlobalObjects, - ]); - - // If no globals are restricted, we don't need to do anything - if (restrictedGlobals.length === 0) { - return {}; - } - - const restrictedGlobalMessages = restrictedGlobals.reduce( - (memo, option) => { - if (typeof option === "string") { - memo[option] = null; - } else { - memo[option.name] = option.message; - } - - return memo; - }, - {}, - ); - - /** - * Report a variable to be used as a restricted global. - * @param {Reference} reference the variable reference - * @returns {void} - * @private - */ - function reportReference(reference) { - const name = reference.identifier.name, - customMessage = restrictedGlobalMessages[name], - messageId = customMessage ? "customMessage" : "defaultMessage"; - - context.report({ - node: reference.identifier, - messageId, - data: { - name, - customMessage, - }, - }); - } - - /** - * Check if the given name is a restricted global name. - * @param {string} name name of a variable - * @returns {boolean} whether the variable is a restricted global or not - * @private - */ - function isRestricted(name) { - return Object.hasOwn(restrictedGlobalMessages, name); - } - - /** - * Check if the given reference occurs within a TypeScript type context. - * @param {Reference} reference The variable reference to check. - * @returns {boolean} Whether the reference is in a type context. - * @private - */ - function isInTypeContext(reference) { - const parent = reference.identifier.parent; - - return TYPE_NODES.has(parent.type); - } - - return { - Program(node) { - const scope = sourceCode.getScope(node); - - // Report variables declared elsewhere (ex: variables defined as "global" by eslint) - scope.variables.forEach(variable => { - if (!variable.defs.length && isRestricted(variable.name)) { - variable.references.forEach(reference => { - if (!isInTypeContext(reference)) { - reportReference(reference); - } - }); - } - }); - - // Report variables not declared at all - scope.through.forEach(reference => { - if ( - isRestricted(reference.identifier.name) && - !isInTypeContext(reference) - ) { - reportReference(reference); - } - }); - }, - - "Program:exit"(node) { - if (!checkGlobalObject) { - return; - } - - const globalScope = sourceCode.getScope(node); - globalObjects.forEach(globalObjectName => { - const variable = astUtils.getVariableByName( - globalScope, - globalObjectName, - ); - - if (!variable) { - return; - } - - variable.references.forEach(reference => { - const identifier = reference.identifier; - let parent = identifier.parent; - - // To detect code like `window.window.Promise`. - while ( - astUtils.isSpecificMemberAccess( - parent, - null, - globalObjectName, - ) - ) { - parent = parent.parent; - } - - const propertyName = - astUtils.getStaticPropertyName(parent); - if (propertyName && isRestricted(propertyName)) { - const customMessage = - restrictedGlobalMessages[propertyName]; - const messageId = customMessage - ? "customMessage" - : "defaultMessage"; - - context.report({ - node: parent.property, - messageId, - data: { - name: propertyName, - customMessage, - }, - }); - } - }); - }); - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow specified global variables", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-restricted-globals" + }, + + schema: { + type: "array", + items: { + oneOf: [ + { + type: "string" + }, + { + type: "object", + properties: { + name: { type: "string" }, + message: { type: "string" } + }, + required: ["name"], + additionalProperties: false + } + ] + }, + uniqueItems: true, + minItems: 0 + }, + + messages: { + defaultMessage: "Unexpected use of '{{name}}'.", + // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period + customMessage: "Unexpected use of '{{name}}'. {{customMessage}}" + } + }, + + create(context) { + + const sourceCode = context.sourceCode; + + // If no globals are restricted, we don't need to do anything + if (context.options.length === 0) { + return {}; + } + + const restrictedGlobalMessages = context.options.reduce((memo, option) => { + if (typeof option === "string") { + memo[option] = null; + } else { + memo[option.name] = option.message; + } + + return memo; + }, {}); + + /** + * Report a variable to be used as a restricted global. + * @param {Reference} reference the variable reference + * @returns {void} + * @private + */ + function reportReference(reference) { + const name = reference.identifier.name, + customMessage = restrictedGlobalMessages[name], + messageId = customMessage + ? "customMessage" + : "defaultMessage"; + + context.report({ + node: reference.identifier, + messageId, + data: { + name, + customMessage + } + }); + } + + /** + * Check if the given name is a restricted global name. + * @param {string} name name of a variable + * @returns {boolean} whether the variable is a restricted global or not + * @private + */ + function isRestricted(name) { + return Object.prototype.hasOwnProperty.call(restrictedGlobalMessages, name); + } + + return { + Program(node) { + const scope = sourceCode.getScope(node); + + // Report variables declared elsewhere (ex: variables defined as "global" by eslint) + scope.variables.forEach(variable => { + if (!variable.defs.length && isRestricted(variable.name)) { + variable.references.forEach(reportReference); + } + }); + + // Report variables not declared at all + scope.through.forEach(reference => { + if (isRestricted(reference.identifier.name)) { + reportReference(reference); + } + }); + + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-restricted-imports.js b/node_modules/eslint/lib/rules/no-restricted-imports.js index a52cafe39..eb59f4c23 100644 --- a/node_modules/eslint/lib/rules/no-restricted-imports.js +++ b/node_modules/eslint/lib/rules/no-restricted-imports.js @@ -17,834 +17,394 @@ const astUtils = require("./utils/ast-utils"); const ignore = require("ignore"); const arrayOfStringsOrObjects = { - type: "array", - items: { - anyOf: [ - { type: "string" }, - { - type: "object", - properties: { - name: { type: "string" }, - message: { - type: "string", - minLength: 1, - }, - importNames: { - type: "array", - items: { - type: "string", - }, - }, - allowImportNames: { - type: "array", - items: { - type: "string", - }, - }, - allowTypeImports: { - type: "boolean", - description: - "Whether to allow type-only imports for a path.", - }, - }, - additionalProperties: false, - required: ["name"], - not: { required: ["importNames", "allowImportNames"] }, - }, - ], - }, - uniqueItems: true, + type: "array", + items: { + anyOf: [ + { type: "string" }, + { + type: "object", + properties: { + name: { type: "string" }, + message: { + type: "string", + minLength: 1 + }, + importNames: { + type: "array", + items: { + type: "string" + } + } + }, + additionalProperties: false, + required: ["name"] + } + ] + }, + uniqueItems: true }; const arrayOfStringsOrObjectPatterns = { - anyOf: [ - { - type: "array", - items: { - type: "string", - }, - uniqueItems: true, - }, - { - type: "array", - items: { - type: "object", - properties: { - importNames: { - type: "array", - items: { - type: "string", - }, - minItems: 1, - uniqueItems: true, - }, - allowImportNames: { - type: "array", - items: { - type: "string", - }, - minItems: 1, - uniqueItems: true, - }, - group: { - type: "array", - items: { - type: "string", - }, - minItems: 1, - uniqueItems: true, - }, - regex: { - type: "string", - }, - importNamePattern: { - type: "string", - }, - allowImportNamePattern: { - type: "string", - }, - message: { - type: "string", - minLength: 1, - }, - caseSensitive: { - type: "boolean", - }, - allowTypeImports: { - type: "boolean", - description: - "Whether to allow type-only imports for a pattern.", - }, - }, - additionalProperties: false, - not: { - anyOf: [ - { required: ["importNames", "allowImportNames"] }, - { - required: [ - "importNamePattern", - "allowImportNamePattern", - ], - }, - { required: ["importNames", "allowImportNamePattern"] }, - { required: ["importNamePattern", "allowImportNames"] }, - { - required: [ - "allowImportNames", - "allowImportNamePattern", - ], - }, - ], - }, - oneOf: [{ required: ["group"] }, { required: ["regex"] }], - }, - uniqueItems: true, - }, - ], + anyOf: [ + { + type: "array", + items: { + type: "string" + }, + uniqueItems: true + }, + { + type: "array", + items: { + type: "object", + properties: { + importNames: { + type: "array", + items: { + type: "string" + }, + minItems: 1, + uniqueItems: true + }, + group: { + type: "array", + items: { + type: "string" + }, + minItems: 1, + uniqueItems: true + }, + importNamePattern: { + type: "string" + }, + message: { + type: "string", + minLength: 1 + }, + caseSensitive: { + type: "boolean" + } + }, + additionalProperties: false, + required: ["group"] + }, + uniqueItems: true + } + ] }; -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - dialects: ["typescript", "javascript"], - language: "javascript", - - docs: { - description: "Disallow specified modules when loaded by `import`", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-restricted-imports", - }, - - messages: { - path: "'{{importSource}}' import is restricted from being used.", - pathWithCustomMessage: - // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period - "'{{importSource}}' import is restricted from being used. {{customMessage}}", - - patterns: - "'{{importSource}}' import is restricted from being used by a pattern.", - patternWithCustomMessage: - // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period - "'{{importSource}}' import is restricted from being used by a pattern. {{customMessage}}", - - patternAndImportName: - "'{{importName}}' import from '{{importSource}}' is restricted from being used by a pattern.", - patternAndImportNameWithCustomMessage: - // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period - "'{{importName}}' import from '{{importSource}}' is restricted from being used by a pattern. {{customMessage}}", - - patternAndEverything: - "* import is invalid because '{{importNames}}' from '{{importSource}}' is restricted from being used by a pattern.", - - patternAndEverythingWithRegexImportName: - "* import is invalid because import name matching '{{importNames}}' pattern from '{{importSource}}' is restricted from being used.", - patternAndEverythingWithCustomMessage: - // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period - "* import is invalid because '{{importNames}}' from '{{importSource}}' is restricted from being used by a pattern. {{customMessage}}", - patternAndEverythingWithRegexImportNameAndCustomMessage: - // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period - "* import is invalid because import name matching '{{importNames}}' pattern from '{{importSource}}' is restricted from being used. {{customMessage}}", - - everything: - "* import is invalid because '{{importNames}}' from '{{importSource}}' is restricted.", - everythingWithCustomMessage: - // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period - "* import is invalid because '{{importNames}}' from '{{importSource}}' is restricted. {{customMessage}}", - - importName: - "'{{importName}}' import from '{{importSource}}' is restricted.", - importNameWithCustomMessage: - // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period - "'{{importName}}' import from '{{importSource}}' is restricted. {{customMessage}}", - - allowedImportName: - "'{{importName}}' import from '{{importSource}}' is restricted because only '{{allowedImportNames}}' import(s) is/are allowed.", - allowedImportNameWithCustomMessage: - // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period - "'{{importName}}' import from '{{importSource}}' is restricted because only '{{allowedImportNames}}' import(s) is/are allowed. {{customMessage}}", - - everythingWithAllowImportNames: - "* import is invalid because only '{{allowedImportNames}}' from '{{importSource}}' is/are allowed.", - everythingWithAllowImportNamesAndCustomMessage: - // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period - "* import is invalid because only '{{allowedImportNames}}' from '{{importSource}}' is/are allowed. {{customMessage}}", - - allowedImportNamePattern: - "'{{importName}}' import from '{{importSource}}' is restricted because only imports that match the pattern '{{allowedImportNamePattern}}' are allowed from '{{importSource}}'.", - allowedImportNamePatternWithCustomMessage: - // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period - "'{{importName}}' import from '{{importSource}}' is restricted because only imports that match the pattern '{{allowedImportNamePattern}}' are allowed from '{{importSource}}'. {{customMessage}}", - - everythingWithAllowedImportNamePattern: - "* import is invalid because only imports that match the pattern '{{allowedImportNamePattern}}' from '{{importSource}}' are allowed.", - everythingWithAllowedImportNamePatternWithCustomMessage: - // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period - "* import is invalid because only imports that match the pattern '{{allowedImportNamePattern}}' from '{{importSource}}' are allowed. {{customMessage}}", - }, - - schema: { - anyOf: [ - arrayOfStringsOrObjects, - { - type: "array", - items: [ - { - type: "object", - properties: { - paths: arrayOfStringsOrObjects, - patterns: arrayOfStringsOrObjectPatterns, - }, - additionalProperties: false, - }, - ], - additionalItems: false, - }, - ], - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - const options = Array.isArray(context.options) ? context.options : []; - const isPathAndPatternsObject = - typeof options[0] === "object" && - (Object.hasOwn(options[0], "paths") || - Object.hasOwn(options[0], "patterns")); - - const restrictedPaths = - (isPathAndPatternsObject ? options[0].paths : context.options) || - []; - const groupedRestrictedPaths = restrictedPaths.reduce( - (memo, importSource) => { - const path = - typeof importSource === "string" - ? importSource - : importSource.name; - - if (!memo[path]) { - memo[path] = []; - } - - if (typeof importSource === "string") { - memo[path].push({}); - } else { - memo[path].push({ - message: importSource.message, - importNames: importSource.importNames, - allowImportNames: importSource.allowImportNames, - allowTypeImports: importSource.allowTypeImports, - }); - } - return memo; - }, - Object.create(null), - ); - - // Handle patterns too, either as strings or groups - let restrictedPatterns = - (isPathAndPatternsObject ? options[0].patterns : []) || []; - - // standardize to array of objects if we have an array of strings - if ( - restrictedPatterns.length > 0 && - typeof restrictedPatterns[0] === "string" - ) { - restrictedPatterns = [{ group: restrictedPatterns }]; - } - - // relative paths are supported for this rule - const restrictedPatternGroups = restrictedPatterns.map( - ({ - group, - regex, - message, - caseSensitive, - importNames, - importNamePattern, - allowImportNames, - allowImportNamePattern, - allowTypeImports, - }) => ({ - ...(group - ? { - matcher: ignore({ - allowRelativePaths: true, - ignorecase: !caseSensitive, - }).add(group), - } - : {}), - ...(typeof regex === "string" - ? { - regexMatcher: new RegExp( - regex, - caseSensitive ? "u" : "iu", - ), - } - : {}), - customMessage: message, - importNames, - importNamePattern, - allowImportNames, - allowImportNamePattern, - allowTypeImports, - }), - ); - - // if no imports are restricted we don't need to check - if ( - Object.keys(restrictedPaths).length === 0 && - restrictedPatternGroups.length === 0 - ) { - return {}; - } - - /** - * Check if the node is a type-only import - * @param {ASTNode} node The node to check - * @returns {boolean} Whether the node is a type-only import - */ - function isTypeOnlyImport(node) { - return ( - node.importKind === "type" || - (node.specifiers?.length > 0 && - node.specifiers.every( - specifier => specifier.importKind === "type", - )) - ); - } - - /** - * Check if a specifier is type-only - * @param {ASTNode} specifier The specifier to check - * @returns {boolean} Whether the specifier is type-only - */ - function isTypeOnlySpecifier(specifier) { - return ( - specifier.importKind === "type" || - specifier.exportKind === "type" - ); - } - - /** - * Check if the node is a type-only export - * @param {ASTNode} node The node to check - * @returns {boolean} Whether the node is a type-only export - */ - function isTypeOnlyExport(node) { - return ( - node.exportKind === "type" || - (node.specifiers?.length > 0 && - node.specifiers.every( - specifier => specifier.exportKind === "type", - )) - ); - } - - /** - * Report a restricted path. - * @param {string} importSource path of the import - * @param {Map} importNames Map of import names that are being imported - * @param {node} node representing the restricted path reference - * @returns {void} - * @private - */ - function checkRestrictedPathAndReport(importSource, importNames, node) { - if (!Object.hasOwn(groupedRestrictedPaths, importSource)) { - return; - } - - groupedRestrictedPaths[importSource].forEach( - restrictedPathEntry => { - const customMessage = restrictedPathEntry.message; - const restrictedImportNames = - restrictedPathEntry.importNames; - const allowedImportNames = - restrictedPathEntry.allowImportNames; - const allowTypeImports = - restrictedPathEntry.allowTypeImports; - - // Skip if this is a type-only import and it's allowed for this specific entry - if ( - allowTypeImports && - (node.type === "ImportDeclaration" || - node.type === "TSImportEqualsDeclaration") && - isTypeOnlyImport(node) - ) { - return; - } - - // Skip if this is a type-only export and it's allowed for this specific entry - if ( - allowTypeImports && - (node.type === "ExportNamedDeclaration" || - node.type === "ExportAllDeclaration") && - isTypeOnlyExport(node) - ) { - return; - } - - if (!restrictedImportNames && !allowedImportNames) { - context.report({ - node, - messageId: customMessage - ? "pathWithCustomMessage" - : "path", - data: { - importSource, - customMessage, - }, - }); - - return; - } - - importNames.forEach((specifiers, importName) => { - if (importName === "*") { - const [specifier] = specifiers; - - if (restrictedImportNames) { - context.report({ - node, - messageId: customMessage - ? "everythingWithCustomMessage" - : "everything", - loc: specifier.loc, - data: { - importSource, - importNames: restrictedImportNames, - customMessage, - }, - }); - } else if (allowedImportNames) { - context.report({ - node, - messageId: customMessage - ? "everythingWithAllowImportNamesAndCustomMessage" - : "everythingWithAllowImportNames", - loc: specifier.loc, - data: { - importSource, - allowedImportNames, - customMessage, - }, - }); - } - - return; - } - - if ( - restrictedImportNames && - restrictedImportNames.includes(importName) - ) { - specifiers.forEach(specifier => { - // Skip if this is a type-only import specifier and type imports are allowed - if ( - allowTypeImports && - isTypeOnlySpecifier(specifier.specifier) - ) { - return; - } - - context.report({ - node, - messageId: customMessage - ? "importNameWithCustomMessage" - : "importName", - loc: specifier.loc, - data: { - importSource, - customMessage, - importName, - }, - }); - }); - } - - if ( - allowedImportNames && - !allowedImportNames.includes(importName) - ) { - specifiers.forEach(specifier => { - // Skip if this is a type-only import specifier and type imports are allowed - if ( - allowTypeImports && - isTypeOnlySpecifier(specifier.specifier) - ) { - return; - } - - context.report({ - node, - loc: specifier.loc, - messageId: customMessage - ? "allowedImportNameWithCustomMessage" - : "allowedImportName", - data: { - importSource, - customMessage, - importName, - allowedImportNames, - }, - }); - }); - } - }); - }, - ); - } - - /** - * Report a restricted path specifically for patterns. - * @param {node} node representing the restricted path reference - * @param {Object} group contains an Ignore instance for paths, the customMessage to show on failure, - * and any restricted import names that have been specified in the config - * @param {Map} importNames Map of import names that are being imported - * @param {string} importSource the import source string - * @returns {void} - * @private - */ - function reportPathForPatterns(node, group, importNames, importSource) { - // Skip if this is a type-only import and it's allowed - if ( - group.allowTypeImports && - (node.type === "ImportDeclaration" || - node.type === "TSImportEqualsDeclaration") && - isTypeOnlyImport(node) - ) { - return; - } - - // Skip if this is a type-only export and it's allowed - if ( - group.allowTypeImports && - (node.type === "ExportNamedDeclaration" || - node.type === "ExportAllDeclaration") && - isTypeOnlyExport(node) - ) { - return; - } - - const customMessage = group.customMessage; - const restrictedImportNames = group.importNames; - const restrictedImportNamePattern = group.importNamePattern - ? new RegExp(group.importNamePattern, "u") - : null; - const allowedImportNames = group.allowImportNames; - const allowedImportNamePattern = group.allowImportNamePattern - ? new RegExp(group.allowImportNamePattern, "u") - : null; - - /** - * If we are not restricting to any specific import names and just the pattern itself, - * report the error and move on - */ - if ( - !restrictedImportNames && - !allowedImportNames && - !restrictedImportNamePattern && - !allowedImportNamePattern - ) { - context.report({ - node, - messageId: customMessage - ? "patternWithCustomMessage" - : "patterns", - data: { - importSource, - customMessage, - }, - }); - return; - } - - importNames.forEach((specifiers, importName) => { - if (importName === "*") { - const [specifier] = specifiers; - - if (restrictedImportNames) { - context.report({ - node, - messageId: customMessage - ? "patternAndEverythingWithCustomMessage" - : "patternAndEverything", - loc: specifier.loc, - data: { - importSource, - importNames: restrictedImportNames, - customMessage, - }, - }); - } else if (allowedImportNames) { - context.report({ - node, - messageId: customMessage - ? "everythingWithAllowImportNamesAndCustomMessage" - : "everythingWithAllowImportNames", - loc: specifier.loc, - data: { - importSource, - allowedImportNames, - customMessage, - }, - }); - } else if (allowedImportNamePattern) { - context.report({ - node, - messageId: customMessage - ? "everythingWithAllowedImportNamePatternWithCustomMessage" - : "everythingWithAllowedImportNamePattern", - loc: specifier.loc, - data: { - importSource, - allowedImportNamePattern, - customMessage, - }, - }); - } else { - context.report({ - node, - messageId: customMessage - ? "patternAndEverythingWithRegexImportNameAndCustomMessage" - : "patternAndEverythingWithRegexImportName", - loc: specifier.loc, - data: { - importSource, - importNames: restrictedImportNamePattern, - customMessage, - }, - }); - } - - return; - } - - if ( - (restrictedImportNames && - restrictedImportNames.includes(importName)) || - (restrictedImportNamePattern && - restrictedImportNamePattern.test(importName)) - ) { - specifiers.forEach(specifier => { - // Skip if this is a type-only import specifier and type imports are allowed - if ( - group.allowTypeImports && - isTypeOnlySpecifier(specifier.specifier) - ) { - return; - } - - context.report({ - node, - messageId: customMessage - ? "patternAndImportNameWithCustomMessage" - : "patternAndImportName", - loc: specifier.loc, - data: { - importSource, - customMessage, - importName, - }, - }); - }); - } - - if ( - allowedImportNames && - !allowedImportNames.includes(importName) - ) { - specifiers.forEach(specifier => { - // Skip if this is a type-only import specifier and type imports are allowed - if ( - group.allowTypeImports && - isTypeOnlySpecifier(specifier.specifier) - ) { - return; - } - - context.report({ - node, - messageId: customMessage - ? "allowedImportNameWithCustomMessage" - : "allowedImportName", - loc: specifier.loc, - data: { - importSource, - customMessage, - importName, - allowedImportNames, - }, - }); - }); - } else if ( - allowedImportNamePattern && - !allowedImportNamePattern.test(importName) - ) { - specifiers.forEach(specifier => { - // Skip if this is a type-only import specifier and type imports are allowed - if ( - group.allowTypeImports && - isTypeOnlySpecifier(specifier.specifier) - ) { - return; - } - - context.report({ - node, - messageId: customMessage - ? "allowedImportNamePatternWithCustomMessage" - : "allowedImportNamePattern", - loc: specifier.loc, - data: { - importSource, - customMessage, - importName, - allowedImportNamePattern, - }, - }); - }); - } - }); - } - - /** - * Check if the given importSource is restricted by a pattern. - * @param {string} importSource path of the import - * @param {Object} group contains a Ignore instance for paths, and the customMessage to show if it fails - * @returns {boolean} whether the variable is a restricted pattern or not - * @private - */ - function isRestrictedPattern(importSource, group) { - return group.regexMatcher - ? group.regexMatcher.test(importSource) - : group.matcher.ignores(importSource); - } - - /** - * Checks a node to see if any problems should be reported. - * @param {ASTNode} node The node to check. - * @returns {void} - * @private - */ - function checkNode(node) { - const importSource = node.source.value.trim(); - const importNames = new Map(); - - if (node.type === "ExportAllDeclaration") { - const starToken = sourceCode.getFirstToken(node, 1); - - importNames.set("*", [{ loc: starToken.loc }]); - } else if (node.specifiers) { - for (const specifier of node.specifiers) { - let name; - const specifierData = { loc: specifier.loc, specifier }; - - if (specifier.type === "ImportDefaultSpecifier") { - name = "default"; - } else if (specifier.type === "ImportNamespaceSpecifier") { - name = "*"; - } else if (specifier.imported) { - name = astUtils.getModuleExportName(specifier.imported); - } else if (specifier.local) { - name = astUtils.getModuleExportName(specifier.local); - } - - if (typeof name === "string") { - if (importNames.has(name)) { - importNames.get(name).push(specifierData); - } else { - importNames.set(name, [specifierData]); - } - } - } - } - - checkRestrictedPathAndReport(importSource, importNames, node); - restrictedPatternGroups.forEach(group => { - if (isRestrictedPattern(importSource, group)) { - reportPathForPatterns( - node, - group, - importNames, - importSource, - ); - } - }); - } - - return { - ImportDeclaration: checkNode, - ExportNamedDeclaration(node) { - if (node.source) { - checkNode(node); - } - }, - ExportAllDeclaration: checkNode, - // Add support for TypeScript import equals declarations - TSImportEqualsDeclaration(node) { - if (node.moduleReference.type === "TSExternalModuleReference") { - const importSource = node.moduleReference.expression.value; - const importNames = new Map(); - - // Use existing logic with the actual node - checkRestrictedPathAndReport( - importSource, - importNames, - node, - ); - restrictedPatternGroups.forEach(group => { - if (isRestrictedPattern(importSource, group)) { - reportPathForPatterns( - node, - group, - importNames, - importSource, - ); - } - }); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow specified modules when loaded by `import`", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-restricted-imports" + }, + + messages: { + path: "'{{importSource}}' import is restricted from being used.", + // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period + pathWithCustomMessage: "'{{importSource}}' import is restricted from being used. {{customMessage}}", + + patterns: "'{{importSource}}' import is restricted from being used by a pattern.", + // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period + patternWithCustomMessage: "'{{importSource}}' import is restricted from being used by a pattern. {{customMessage}}", + + patternAndImportName: "'{{importName}}' import from '{{importSource}}' is restricted from being used by a pattern.", + // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period + patternAndImportNameWithCustomMessage: "'{{importName}}' import from '{{importSource}}' is restricted from being used by a pattern. {{customMessage}}", + + patternAndEverything: "* import is invalid because '{{importNames}}' from '{{importSource}}' is restricted from being used by a pattern.", + + patternAndEverythingWithRegexImportName: "* import is invalid because import name matching '{{importNames}}' pattern from '{{importSource}}' is restricted from being used.", + // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period + patternAndEverythingWithCustomMessage: "* import is invalid because '{{importNames}}' from '{{importSource}}' is restricted from being used by a pattern. {{customMessage}}", + // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period + patternAndEverythingWithRegexImportNameAndCustomMessage: "* import is invalid because import name matching '{{importNames}}' pattern from '{{importSource}}' is restricted from being used. {{customMessage}}", + + everything: "* import is invalid because '{{importNames}}' from '{{importSource}}' is restricted.", + // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period + everythingWithCustomMessage: "* import is invalid because '{{importNames}}' from '{{importSource}}' is restricted. {{customMessage}}", + + importName: "'{{importName}}' import from '{{importSource}}' is restricted.", + // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period + importNameWithCustomMessage: "'{{importName}}' import from '{{importSource}}' is restricted. {{customMessage}}" + }, + + schema: { + anyOf: [ + arrayOfStringsOrObjects, + { + type: "array", + items: [{ + type: "object", + properties: { + paths: arrayOfStringsOrObjects, + patterns: arrayOfStringsOrObjectPatterns + }, + additionalProperties: false + }], + additionalItems: false + } + ] + } + }, + + create(context) { + const sourceCode = context.sourceCode; + const options = Array.isArray(context.options) ? context.options : []; + const isPathAndPatternsObject = + typeof options[0] === "object" && + (Object.prototype.hasOwnProperty.call(options[0], "paths") || Object.prototype.hasOwnProperty.call(options[0], "patterns")); + + const restrictedPaths = (isPathAndPatternsObject ? options[0].paths : context.options) || []; + const restrictedPathMessages = restrictedPaths.reduce((memo, importSource) => { + if (typeof importSource === "string") { + memo[importSource] = { message: null }; + } else { + memo[importSource.name] = { + message: importSource.message, + importNames: importSource.importNames + }; + } + return memo; + }, {}); + + // Handle patterns too, either as strings or groups + let restrictedPatterns = (isPathAndPatternsObject ? options[0].patterns : []) || []; + + // standardize to array of objects if we have an array of strings + if (restrictedPatterns.length > 0 && typeof restrictedPatterns[0] === "string") { + restrictedPatterns = [{ group: restrictedPatterns }]; + } + + // relative paths are supported for this rule + const restrictedPatternGroups = restrictedPatterns.map(({ group, message, caseSensitive, importNames, importNamePattern }) => ({ + matcher: ignore({ allowRelativePaths: true, ignorecase: !caseSensitive }).add(group), + customMessage: message, + importNames, + importNamePattern + })); + + // if no imports are restricted we don't need to check + if (Object.keys(restrictedPaths).length === 0 && restrictedPatternGroups.length === 0) { + return {}; + } + + /** + * Report a restricted path. + * @param {string} importSource path of the import + * @param {Map} importNames Map of import names that are being imported + * @param {node} node representing the restricted path reference + * @returns {void} + * @private + */ + function checkRestrictedPathAndReport(importSource, importNames, node) { + if (!Object.prototype.hasOwnProperty.call(restrictedPathMessages, importSource)) { + return; + } + + const customMessage = restrictedPathMessages[importSource].message; + const restrictedImportNames = restrictedPathMessages[importSource].importNames; + + if (restrictedImportNames) { + if (importNames.has("*")) { + const specifierData = importNames.get("*")[0]; + + context.report({ + node, + messageId: customMessage ? "everythingWithCustomMessage" : "everything", + loc: specifierData.loc, + data: { + importSource, + importNames: restrictedImportNames, + customMessage + } + }); + } + + restrictedImportNames.forEach(importName => { + if (importNames.has(importName)) { + const specifiers = importNames.get(importName); + + specifiers.forEach(specifier => { + context.report({ + node, + messageId: customMessage ? "importNameWithCustomMessage" : "importName", + loc: specifier.loc, + data: { + importSource, + customMessage, + importName + } + }); + }); + } + }); + } else { + context.report({ + node, + messageId: customMessage ? "pathWithCustomMessage" : "path", + data: { + importSource, + customMessage + } + }); + } + } + + /** + * Report a restricted path specifically for patterns. + * @param {node} node representing the restricted path reference + * @param {Object} group contains an Ignore instance for paths, the customMessage to show on failure, + * and any restricted import names that have been specified in the config + * @param {Map} importNames Map of import names that are being imported + * @returns {void} + * @private + */ + function reportPathForPatterns(node, group, importNames) { + const importSource = node.source.value.trim(); + + const customMessage = group.customMessage; + const restrictedImportNames = group.importNames; + const restrictedImportNamePattern = group.importNamePattern ? new RegExp(group.importNamePattern, "u") : null; + + /* + * If we are not restricting to any specific import names and just the pattern itself, + * report the error and move on + */ + if (!restrictedImportNames && !restrictedImportNamePattern) { + context.report({ + node, + messageId: customMessage ? "patternWithCustomMessage" : "patterns", + data: { + importSource, + customMessage + } + }); + return; + } + + importNames.forEach((specifiers, importName) => { + if (importName === "*") { + const [specifier] = specifiers; + + if (restrictedImportNames) { + context.report({ + node, + messageId: customMessage ? "patternAndEverythingWithCustomMessage" : "patternAndEverything", + loc: specifier.loc, + data: { + importSource, + importNames: restrictedImportNames, + customMessage + } + }); + } else { + context.report({ + node, + messageId: customMessage ? "patternAndEverythingWithRegexImportNameAndCustomMessage" : "patternAndEverythingWithRegexImportName", + loc: specifier.loc, + data: { + importSource, + importNames: restrictedImportNamePattern, + customMessage + } + }); + } + + return; + } + + if ( + (restrictedImportNames && restrictedImportNames.includes(importName)) || + (restrictedImportNamePattern && restrictedImportNamePattern.test(importName)) + ) { + specifiers.forEach(specifier => { + context.report({ + node, + messageId: customMessage ? "patternAndImportNameWithCustomMessage" : "patternAndImportName", + loc: specifier.loc, + data: { + importSource, + customMessage, + importName + } + }); + }); + } + }); + } + + /** + * Check if the given importSource is restricted by a pattern. + * @param {string} importSource path of the import + * @param {Object} group contains a Ignore instance for paths, and the customMessage to show if it fails + * @returns {boolean} whether the variable is a restricted pattern or not + * @private + */ + function isRestrictedPattern(importSource, group) { + return group.matcher.ignores(importSource); + } + + /** + * Checks a node to see if any problems should be reported. + * @param {ASTNode} node The node to check. + * @returns {void} + * @private + */ + function checkNode(node) { + const importSource = node.source.value.trim(); + const importNames = new Map(); + + if (node.type === "ExportAllDeclaration") { + const starToken = sourceCode.getFirstToken(node, 1); + + importNames.set("*", [{ loc: starToken.loc }]); + } else if (node.specifiers) { + for (const specifier of node.specifiers) { + let name; + const specifierData = { loc: specifier.loc }; + + if (specifier.type === "ImportDefaultSpecifier") { + name = "default"; + } else if (specifier.type === "ImportNamespaceSpecifier") { + name = "*"; + } else if (specifier.imported) { + name = astUtils.getModuleExportName(specifier.imported); + } else if (specifier.local) { + name = astUtils.getModuleExportName(specifier.local); + } + + if (typeof name === "string") { + if (importNames.has(name)) { + importNames.get(name).push(specifierData); + } else { + importNames.set(name, [specifierData]); + } + } + } + } + + checkRestrictedPathAndReport(importSource, importNames, node); + restrictedPatternGroups.forEach(group => { + if (isRestrictedPattern(importSource, group)) { + reportPathForPatterns(node, group, importNames); + } + }); + } + + return { + ImportDeclaration: checkNode, + ExportNamedDeclaration(node) { + if (node.source) { + checkNode(node); + } + }, + ExportAllDeclaration: checkNode + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-restricted-modules.js b/node_modules/eslint/lib/rules/no-restricted-modules.js index c09cb7d9e..4a9b0a4c0 100644 --- a/node_modules/eslint/lib/rules/no-restricted-modules.js +++ b/node_modules/eslint/lib/rules/no-restricted-modules.js @@ -18,232 +18,196 @@ const astUtils = require("./utils/ast-utils"); const ignore = require("ignore"); const arrayOfStrings = { - type: "array", - items: { type: "string" }, - uniqueItems: true, + type: "array", + items: { type: "string" }, + uniqueItems: true }; const arrayOfStringsOrObjects = { - type: "array", - items: { - anyOf: [ - { type: "string" }, - { - type: "object", - properties: { - name: { type: "string" }, - message: { - type: "string", - minLength: 1, - }, - }, - additionalProperties: false, - required: ["name"], - }, - ], - }, - uniqueItems: true, + type: "array", + items: { + anyOf: [ + { type: "string" }, + { + type: "object", + properties: { + name: { type: "string" }, + message: { + type: "string", + minLength: 1 + } + }, + additionalProperties: false, + required: ["name"] + } + ] + }, + uniqueItems: true }; -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Node.js rules were moved out of ESLint core.", - url: "https://eslint.org/docs/latest/use/migrating-to-7.0.0#deprecate-node-rules", - deprecatedSince: "7.0.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "eslint-plugin-n now maintains deprecated Node.js-related rules.", - plugin: { - name: "eslint-plugin-n", - url: "https://github.com/eslint-community/eslint-plugin-n", - }, - rule: { - name: "no-restricted-require", - url: "https://github.com/eslint-community/eslint-plugin-n/tree/master/docs/rules/no-restricted-require.md", - }, - }, - ], - }, - - type: "suggestion", - - docs: { - description: "Disallow specified modules when loaded by `require`", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-restricted-modules", - }, - - schema: { - anyOf: [ - arrayOfStringsOrObjects, - { - type: "array", - items: { - type: "object", - properties: { - paths: arrayOfStringsOrObjects, - patterns: arrayOfStrings, - }, - additionalProperties: false, - }, - additionalItems: false, - }, - ], - }, - - messages: { - defaultMessage: "'{{name}}' module is restricted from being used.", - customMessage: - // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period - "'{{name}}' module is restricted from being used. {{customMessage}}", - patternMessage: - "'{{name}}' module is restricted from being used by a pattern.", - }, - }, - - create(context) { - const options = Array.isArray(context.options) ? context.options : []; - const isPathAndPatternsObject = - typeof options[0] === "object" && - (Object.hasOwn(options[0], "paths") || - Object.hasOwn(options[0], "patterns")); - - const restrictedPaths = - (isPathAndPatternsObject ? options[0].paths : context.options) || - []; - const restrictedPatterns = - (isPathAndPatternsObject ? options[0].patterns : []) || []; - - const restrictedPathMessages = restrictedPaths.reduce( - (memo, importName) => { - if (typeof importName === "string") { - memo[importName] = null; - } else { - memo[importName.name] = importName.message; - } - return memo; - }, - {}, - ); - - // if no imports are restricted we don't need to check - if ( - Object.keys(restrictedPaths).length === 0 && - restrictedPatterns.length === 0 - ) { - return {}; - } - - // relative paths are supported for this rule - const ig = ignore({ allowRelativePaths: true }).add(restrictedPatterns); - - /** - * Function to check if a node is a string literal. - * @param {ASTNode} node The node to check. - * @returns {boolean} If the node is a string literal. - */ - function isStringLiteral(node) { - return ( - node && - node.type === "Literal" && - typeof node.value === "string" - ); - } - - /** - * Function to check if a node is a require call. - * @param {ASTNode} node The node to check. - * @returns {boolean} If the node is a require call. - */ - function isRequireCall(node) { - return ( - node.callee.type === "Identifier" && - node.callee.name === "require" - ); - } - - /** - * Extract string from Literal or TemplateLiteral node - * @param {ASTNode} node The node to extract from - * @returns {string|null} Extracted string or null if node doesn't represent a string - */ - function getFirstArgumentString(node) { - if (isStringLiteral(node)) { - return node.value.trim(); - } - - if (astUtils.isStaticTemplateLiteral(node)) { - return node.quasis[0].value.cooked.trim(); - } - - return null; - } - - /** - * Report a restricted path. - * @param {node} node representing the restricted path reference - * @param {string} name restricted path - * @returns {void} - * @private - */ - function reportPath(node, name) { - const customMessage = restrictedPathMessages[name]; - const messageId = customMessage - ? "customMessage" - : "defaultMessage"; - - context.report({ - node, - messageId, - data: { - name, - customMessage, - }, - }); - } - - /** - * Check if the given name is a restricted path name - * @param {string} name name of a variable - * @returns {boolean} whether the variable is a restricted path or not - * @private - */ - function isRestrictedPath(name) { - return Object.hasOwn(restrictedPathMessages, name); - } - - return { - CallExpression(node) { - if (isRequireCall(node)) { - // node has arguments - if (node.arguments.length) { - const name = getFirstArgumentString(node.arguments[0]); - - // if first argument is a string literal or a static string template literal - if (name) { - // check if argument value is in restricted modules array - if (isRestrictedPath(name)) { - reportPath(node, name); - } - - if ( - restrictedPatterns.length > 0 && - ig.ignores(name) - ) { - context.report({ - node, - messageId: "patternMessage", - data: { name }, - }); - } - } - } - } - }, - }; - }, + meta: { + deprecated: true, + + replacedBy: [], + + type: "suggestion", + + docs: { + description: "Disallow specified modules when loaded by `require`", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-restricted-modules" + }, + + schema: { + anyOf: [ + arrayOfStringsOrObjects, + { + type: "array", + items: { + type: "object", + properties: { + paths: arrayOfStringsOrObjects, + patterns: arrayOfStrings + }, + additionalProperties: false + }, + additionalItems: false + } + ] + }, + + messages: { + defaultMessage: "'{{name}}' module is restricted from being used.", + // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period + customMessage: "'{{name}}' module is restricted from being used. {{customMessage}}", + patternMessage: "'{{name}}' module is restricted from being used by a pattern." + } + }, + + create(context) { + const options = Array.isArray(context.options) ? context.options : []; + const isPathAndPatternsObject = + typeof options[0] === "object" && + (Object.prototype.hasOwnProperty.call(options[0], "paths") || Object.prototype.hasOwnProperty.call(options[0], "patterns")); + + const restrictedPaths = (isPathAndPatternsObject ? options[0].paths : context.options) || []; + const restrictedPatterns = (isPathAndPatternsObject ? options[0].patterns : []) || []; + + const restrictedPathMessages = restrictedPaths.reduce((memo, importName) => { + if (typeof importName === "string") { + memo[importName] = null; + } else { + memo[importName.name] = importName.message; + } + return memo; + }, {}); + + // if no imports are restricted we don't need to check + if (Object.keys(restrictedPaths).length === 0 && restrictedPatterns.length === 0) { + return {}; + } + + // relative paths are supported for this rule + const ig = ignore({ allowRelativePaths: true }).add(restrictedPatterns); + + + /** + * Function to check if a node is a string literal. + * @param {ASTNode} node The node to check. + * @returns {boolean} If the node is a string literal. + */ + function isStringLiteral(node) { + return node && node.type === "Literal" && typeof node.value === "string"; + } + + /** + * Function to check if a node is a require call. + * @param {ASTNode} node The node to check. + * @returns {boolean} If the node is a require call. + */ + function isRequireCall(node) { + return node.callee.type === "Identifier" && node.callee.name === "require"; + } + + /** + * Extract string from Literal or TemplateLiteral node + * @param {ASTNode} node The node to extract from + * @returns {string|null} Extracted string or null if node doesn't represent a string + */ + function getFirstArgumentString(node) { + if (isStringLiteral(node)) { + return node.value.trim(); + } + + if (astUtils.isStaticTemplateLiteral(node)) { + return node.quasis[0].value.cooked.trim(); + } + + return null; + } + + /** + * Report a restricted path. + * @param {node} node representing the restricted path reference + * @param {string} name restricted path + * @returns {void} + * @private + */ + function reportPath(node, name) { + const customMessage = restrictedPathMessages[name]; + const messageId = customMessage + ? "customMessage" + : "defaultMessage"; + + context.report({ + node, + messageId, + data: { + name, + customMessage + } + }); + } + + /** + * Check if the given name is a restricted path name + * @param {string} name name of a variable + * @returns {boolean} whether the variable is a restricted path or not + * @private + */ + function isRestrictedPath(name) { + return Object.prototype.hasOwnProperty.call(restrictedPathMessages, name); + } + + return { + CallExpression(node) { + if (isRequireCall(node)) { + + // node has arguments + if (node.arguments.length) { + const name = getFirstArgumentString(node.arguments[0]); + + // if first argument is a string literal or a static string template literal + if (name) { + + // check if argument value is in restricted modules array + if (isRestrictedPath(name)) { + reportPath(node, name); + } + + if (restrictedPatterns.length > 0 && ig.ignores(name)) { + context.report({ + node, + messageId: "patternMessage", + data: { name } + }); + } + } + } + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-restricted-properties.js b/node_modules/eslint/lib/rules/no-restricted-properties.js index 95dbd67d8..acd178ae5 100644 --- a/node_modules/eslint/lib/rules/no-restricted-properties.js +++ b/node_modules/eslint/lib/rules/no-restricted-properties.js @@ -11,223 +11,158 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: "Disallow certain properties on certain objects", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-restricted-properties", - }, - - schema: { - type: "array", - items: { - type: "object", - properties: { - object: { - type: "string", - }, - property: { - type: "string", - }, - allowObjects: { - type: "array", - items: { - type: "string", - }, - uniqueItems: true, - }, - allowProperties: { - type: "array", - items: { - type: "string", - }, - uniqueItems: true, - }, - message: { - type: "string", - }, - }, - anyOf: [ - { - required: ["object"], - }, - { - required: ["property"], - }, - ], - not: { - anyOf: [ - { required: ["allowObjects", "object"] }, - { required: ["allowProperties", "property"] }, - ], - }, - additionalProperties: false, - }, - uniqueItems: true, - }, - - messages: { - restrictedObjectProperty: - // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period - "'{{objectName}}.{{propertyName}}' is restricted from being used.{{allowedPropertiesMessage}}{{message}}", - restrictedProperty: - // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period - "'{{propertyName}}' is restricted from being used.{{allowedObjectsMessage}}{{message}}", - }, - }, - - create(context) { - const restrictedCalls = context.options; - - if (restrictedCalls.length === 0) { - return {}; - } - - const restrictedProperties = new Map(); - const globallyRestrictedObjects = new Map(); - const globallyRestrictedProperties = new Map(); - - restrictedCalls.forEach(option => { - const objectName = option.object; - const propertyName = option.property; - - if (typeof objectName === "undefined") { - globallyRestrictedProperties.set(propertyName, { - allowObjects: option.allowObjects, - message: option.message, - }); - } else if (typeof propertyName === "undefined") { - globallyRestrictedObjects.set(objectName, { - allowProperties: option.allowProperties, - message: option.message, - }); - } else { - if (!restrictedProperties.has(objectName)) { - restrictedProperties.set(objectName, new Map()); - } - - restrictedProperties.get(objectName).set(propertyName, { - message: option.message, - }); - } - }); - - /** - * Checks if a name is in the allowed list. - * @param {string} name The name to check - * @param {string[]} [allowedList] The list of allowed names - * @returns {boolean} True if the name is allowed, false otherwise - */ - function isAllowed(name, allowedList) { - if (!allowedList) { - return false; - } - - return allowedList.includes(name); - } - - /** - * Checks to see whether a property access is restricted, and reports it if so. - * @param {ASTNode} node The node to report - * @param {string} objectName The name of the object - * @param {string} propertyName The name of the property - * @returns {undefined} - */ - function checkPropertyAccess(node, objectName, propertyName) { - if (propertyName === null) { - return; - } - const matchedObject = restrictedProperties.get(objectName); - const matchedObjectProperty = matchedObject - ? matchedObject.get(propertyName) - : globallyRestrictedObjects.get(objectName); - const globalMatchedProperty = - globallyRestrictedProperties.get(propertyName); - - if ( - matchedObjectProperty && - !isAllowed(propertyName, matchedObjectProperty.allowProperties) - ) { - const message = matchedObjectProperty.message - ? ` ${matchedObjectProperty.message}` - : ""; - const allowedPropertiesMessage = - matchedObjectProperty.allowProperties - ? ` Only these properties are allowed: ${matchedObjectProperty.allowProperties.join(", ")}.` - : ""; - - context.report({ - node, - messageId: "restrictedObjectProperty", - data: { - objectName, - propertyName, - message, - allowedPropertiesMessage, - }, - }); - } else if ( - globalMatchedProperty && - !isAllowed(objectName, globalMatchedProperty.allowObjects) - ) { - const message = globalMatchedProperty.message - ? ` ${globalMatchedProperty.message}` - : ""; - const allowedObjectsMessage = globalMatchedProperty.allowObjects - ? ` Property '${propertyName}' is only allowed on these objects: ${globalMatchedProperty.allowObjects.join(", ")}.` - : ""; - - context.report({ - node, - messageId: "restrictedProperty", - data: { - propertyName, - message, - allowedObjectsMessage, - }, - }); - } - } - - return { - MemberExpression(node) { - checkPropertyAccess( - node, - node.object && node.object.name, - astUtils.getStaticPropertyName(node), - ); - }, - ObjectPattern(node) { - let objectName = null; - - if (node.parent.type === "VariableDeclarator") { - if ( - node.parent.init && - node.parent.init.type === "Identifier" - ) { - objectName = node.parent.init.name; - } - } else if ( - node.parent.type === "AssignmentExpression" || - node.parent.type === "AssignmentPattern" - ) { - if (node.parent.right.type === "Identifier") { - objectName = node.parent.right.name; - } - } - - node.properties.forEach(property => { - checkPropertyAccess( - node, - objectName, - astUtils.getStaticPropertyName(property), - ); - }); - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow certain properties on certain objects", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-restricted-properties" + }, + + schema: { + type: "array", + items: { + anyOf: [ // `object` and `property` are both optional, but at least one of them must be provided. + { + type: "object", + properties: { + object: { + type: "string" + }, + property: { + type: "string" + }, + message: { + type: "string" + } + }, + additionalProperties: false, + required: ["object"] + }, + { + type: "object", + properties: { + object: { + type: "string" + }, + property: { + type: "string" + }, + message: { + type: "string" + } + }, + additionalProperties: false, + required: ["property"] + } + ] + }, + uniqueItems: true + }, + + messages: { + // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period + restrictedObjectProperty: "'{{objectName}}.{{propertyName}}' is restricted from being used.{{message}}", + // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period + restrictedProperty: "'{{propertyName}}' is restricted from being used.{{message}}" + } + }, + + create(context) { + const restrictedCalls = context.options; + + if (restrictedCalls.length === 0) { + return {}; + } + + const restrictedProperties = new Map(); + const globallyRestrictedObjects = new Map(); + const globallyRestrictedProperties = new Map(); + + restrictedCalls.forEach(option => { + const objectName = option.object; + const propertyName = option.property; + + if (typeof objectName === "undefined") { + globallyRestrictedProperties.set(propertyName, { message: option.message }); + } else if (typeof propertyName === "undefined") { + globallyRestrictedObjects.set(objectName, { message: option.message }); + } else { + if (!restrictedProperties.has(objectName)) { + restrictedProperties.set(objectName, new Map()); + } + + restrictedProperties.get(objectName).set(propertyName, { + message: option.message + }); + } + }); + + /** + * Checks to see whether a property access is restricted, and reports it if so. + * @param {ASTNode} node The node to report + * @param {string} objectName The name of the object + * @param {string} propertyName The name of the property + * @returns {undefined} + */ + function checkPropertyAccess(node, objectName, propertyName) { + if (propertyName === null) { + return; + } + const matchedObject = restrictedProperties.get(objectName); + const matchedObjectProperty = matchedObject ? matchedObject.get(propertyName) : globallyRestrictedObjects.get(objectName); + const globalMatchedProperty = globallyRestrictedProperties.get(propertyName); + + if (matchedObjectProperty) { + const message = matchedObjectProperty.message ? ` ${matchedObjectProperty.message}` : ""; + + context.report({ + node, + messageId: "restrictedObjectProperty", + data: { + objectName, + propertyName, + message + } + }); + } else if (globalMatchedProperty) { + const message = globalMatchedProperty.message ? ` ${globalMatchedProperty.message}` : ""; + + context.report({ + node, + messageId: "restrictedProperty", + data: { + propertyName, + message + } + }); + } + } + + return { + MemberExpression(node) { + checkPropertyAccess(node, node.object && node.object.name, astUtils.getStaticPropertyName(node)); + }, + ObjectPattern(node) { + let objectName = null; + + if (node.parent.type === "VariableDeclarator") { + if (node.parent.init && node.parent.init.type === "Identifier") { + objectName = node.parent.init.name; + } + } else if (node.parent.type === "AssignmentExpression" || node.parent.type === "AssignmentPattern") { + if (node.parent.right.type === "Identifier") { + objectName = node.parent.right.name; + } + } + + node.properties.forEach(property => { + checkPropertyAccess(node, objectName, astUtils.getStaticPropertyName(property)); + }); + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-restricted-syntax.js b/node_modules/eslint/lib/rules/no-restricted-syntax.js index 447d29cf1..930882c33 100644 --- a/node_modules/eslint/lib/rules/no-restricted-syntax.js +++ b/node_modules/eslint/lib/rules/no-restricted-syntax.js @@ -8,67 +8,63 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", + meta: { + type: "suggestion", - docs: { - description: "Disallow specified syntax", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-restricted-syntax", - }, + docs: { + description: "Disallow specified syntax", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-restricted-syntax" + }, - schema: { - type: "array", - items: { - oneOf: [ - { - type: "string", - }, - { - type: "object", - properties: { - selector: { type: "string" }, - message: { type: "string" }, - }, - required: ["selector"], - additionalProperties: false, - }, - ], - }, - uniqueItems: true, - minItems: 0, - }, + schema: { + type: "array", + items: { + oneOf: [ + { + type: "string" + }, + { + type: "object", + properties: { + selector: { type: "string" }, + message: { type: "string" } + }, + required: ["selector"], + additionalProperties: false + } + ] + }, + uniqueItems: true, + minItems: 0 + }, - messages: { - // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period - restrictedSyntax: "{{message}}", - }, - }, + messages: { + // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period + restrictedSyntax: "{{message}}" + } + }, - create(context) { - return context.options.reduce((result, selectorOrObject) => { - const isStringFormat = typeof selectorOrObject === "string"; - const hasCustomMessage = - !isStringFormat && Boolean(selectorOrObject.message); + create(context) { + return context.options.reduce((result, selectorOrObject) => { + const isStringFormat = (typeof selectorOrObject === "string"); + const hasCustomMessage = !isStringFormat && Boolean(selectorOrObject.message); - const selector = isStringFormat - ? selectorOrObject - : selectorOrObject.selector; - const message = hasCustomMessage - ? selectorOrObject.message - : `Using '${selector}' is not allowed.`; + const selector = isStringFormat ? selectorOrObject : selectorOrObject.selector; + const message = hasCustomMessage ? selectorOrObject.message : `Using '${selector}' is not allowed.`; - return Object.assign(result, { - [selector](node) { - context.report({ - node, - messageId: "restrictedSyntax", - data: { message }, - }); - }, - }); - }, {}); - }, + return Object.assign(result, { + [selector](node) { + context.report({ + node, + messageId: "restrictedSyntax", + data: { message } + }); + } + }); + }, {}); + + } }; diff --git a/node_modules/eslint/lib/rules/no-return-assign.js b/node_modules/eslint/lib/rules/no-return-assign.js index 92999ddf2..73caf0e6b 100644 --- a/node_modules/eslint/lib/rules/no-return-assign.js +++ b/node_modules/eslint/lib/rules/no-return-assign.js @@ -14,74 +14,67 @@ const astUtils = require("./utils/ast-utils"); // Helpers //------------------------------------------------------------------------------ -const SENTINEL_TYPE = - /^(?:[a-zA-Z]+?Statement|ArrowFunctionExpression|FunctionExpression|ClassExpression)$/u; +const SENTINEL_TYPE = /^(?:[a-zA-Z]+?Statement|ArrowFunctionExpression|FunctionExpression|ClassExpression)$/u; //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", + meta: { + type: "suggestion", - defaultOptions: ["except-parens"], + docs: { + description: "Disallow assignment operators in `return` statements", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-return-assign" + }, - docs: { - description: "Disallow assignment operators in `return` statements", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-return-assign", - }, + schema: [ + { + enum: ["except-parens", "always"] + } + ], - schema: [ - { - enum: ["except-parens", "always"], - }, - ], + messages: { + returnAssignment: "Return statement should not contain assignment.", + arrowAssignment: "Arrow function should not return assignment." + } + }, - messages: { - returnAssignment: "Return statement should not contain assignment.", - arrowAssignment: "Arrow function should not return assignment.", - }, - }, + create(context) { + const always = (context.options[0] || "except-parens") !== "except-parens"; + const sourceCode = context.sourceCode; - create(context) { - const always = context.options[0] !== "except-parens"; - const sourceCode = context.sourceCode; + return { + AssignmentExpression(node) { + if (!always && astUtils.isParenthesised(sourceCode, node)) { + return; + } - return { - AssignmentExpression(node) { - if (!always && astUtils.isParenthesised(sourceCode, node)) { - return; - } + let currentChild = node; + let parent = currentChild.parent; - let currentChild = node; - let parent = currentChild.parent; + // Find ReturnStatement or ArrowFunctionExpression in ancestors. + while (parent && !SENTINEL_TYPE.test(parent.type)) { + currentChild = parent; + parent = parent.parent; + } - // Find ReturnStatement or ArrowFunctionExpression in ancestors. - while (parent && !SENTINEL_TYPE.test(parent.type)) { - currentChild = parent; - parent = parent.parent; - } - - // Reports. - if (parent && parent.type === "ReturnStatement") { - context.report({ - node: parent, - messageId: "returnAssignment", - }); - } else if ( - parent && - parent.type === "ArrowFunctionExpression" && - parent.body === currentChild - ) { - context.report({ - node: parent, - messageId: "arrowAssignment", - }); - } - }, - }; - }, + // Reports. + if (parent && parent.type === "ReturnStatement") { + context.report({ + node: parent, + messageId: "returnAssignment" + }); + } else if (parent && parent.type === "ArrowFunctionExpression" && parent.body === currentChild) { + context.report({ + node: parent, + messageId: "arrowAssignment" + }); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-return-await.js b/node_modules/eslint/lib/rules/no-return-await.js index 21627eddf..77abda0ca 100644 --- a/node_modules/eslint/lib/rules/no-return-await.js +++ b/node_modules/eslint/lib/rules/no-return-await.js @@ -11,152 +11,125 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - hasSuggestions: true, - type: "suggestion", - - docs: { - description: "Disallow unnecessary `return await`", - - recommended: false, - - url: "https://eslint.org/docs/latest/rules/no-return-await", - }, - - fixable: null, - - deprecated: { - message: - "The original assumption of the rule no longer holds true because of engine optimization.", - deprecatedSince: "8.46.0", - availableUntil: null, - replacedBy: [], - }, - - schema: [], - - messages: { - removeAwait: "Remove redundant `await`.", - redundantUseOfAwait: "Redundant use of `await` on a return value.", - }, - }, - - create(context) { - /** - * Reports a found unnecessary `await` expression. - * @param {ASTNode} node The node representing the `await` expression to report - * @returns {void} - */ - function reportUnnecessaryAwait(node) { - context.report({ - node: context.sourceCode.getFirstToken(node), - loc: node.loc, - messageId: "redundantUseOfAwait", - suggest: [ - { - messageId: "removeAwait", - fix(fixer) { - const sourceCode = context.sourceCode; - const [awaitToken, tokenAfterAwait] = - sourceCode.getFirstTokens(node, 2); - - const areAwaitAndAwaitedExpressionOnTheSameLine = - awaitToken.loc.start.line === - tokenAfterAwait.loc.start.line; - - if (!areAwaitAndAwaitedExpressionOnTheSameLine) { - return null; - } - - const [startOfAwait, endOfAwait] = awaitToken.range; - - const characterAfterAwait = - sourceCode.text[endOfAwait]; - const trimLength = - characterAfterAwait === " " ? 1 : 0; - - const range = [ - startOfAwait, - endOfAwait + trimLength, - ]; - - return fixer.removeRange(range); - }, - }, - ], - }); - } - - /** - * Determines whether a thrown error from this node will be caught/handled within this function rather than immediately halting - * this function. For example, a statement in a `try` block will always have an error handler. A statement in - * a `catch` block will only have an error handler if there is also a `finally` block. - * @param {ASTNode} node A node representing a location where an could be thrown - * @returns {boolean} `true` if a thrown error will be caught/handled in this function - */ - function hasErrorHandler(node) { - let ancestor = node; - - while ( - !astUtils.isFunction(ancestor) && - ancestor.type !== "Program" - ) { - if ( - ancestor.parent.type === "TryStatement" && - (ancestor === ancestor.parent.block || - (ancestor === ancestor.parent.handler && - ancestor.parent.finalizer)) - ) { - return true; - } - ancestor = ancestor.parent; - } - return false; - } - - /** - * Checks if a node is placed in tail call position. Once `return` arguments (or arrow function expressions) can be a complex expression, - * an `await` expression could or could not be unnecessary by the definition of this rule. So we're looking for `await` expressions that are in tail position. - * @param {ASTNode} node A node representing the `await` expression to check - * @returns {boolean} The checking result - */ - function isInTailCallPosition(node) { - if (node.parent.type === "ArrowFunctionExpression") { - return true; - } - if (node.parent.type === "ReturnStatement") { - return !hasErrorHandler(node.parent); - } - if ( - node.parent.type === "ConditionalExpression" && - (node === node.parent.consequent || - node === node.parent.alternate) - ) { - return isInTailCallPosition(node.parent); - } - if ( - node.parent.type === "LogicalExpression" && - node === node.parent.right - ) { - return isInTailCallPosition(node.parent); - } - if ( - node.parent.type === "SequenceExpression" && - node === node.parent.expressions.at(-1) - ) { - return isInTailCallPosition(node.parent); - } - return false; - } - - return { - AwaitExpression(node) { - if (isInTailCallPosition(node) && !hasErrorHandler(node)) { - reportUnnecessaryAwait(node); - } - }, - }; - }, + meta: { + hasSuggestions: true, + type: "suggestion", + + docs: { + description: "Disallow unnecessary `return await`", + + recommended: false, + + url: "https://eslint.org/docs/latest/rules/no-return-await" + }, + + fixable: null, + + deprecated: true, + + replacedBy: [], + + schema: [ + ], + + messages: { + removeAwait: "Remove redundant `await`.", + redundantUseOfAwait: "Redundant use of `await` on a return value." + } + }, + + create(context) { + + /** + * Reports a found unnecessary `await` expression. + * @param {ASTNode} node The node representing the `await` expression to report + * @returns {void} + */ + function reportUnnecessaryAwait(node) { + context.report({ + node: context.sourceCode.getFirstToken(node), + loc: node.loc, + messageId: "redundantUseOfAwait", + suggest: [ + { + messageId: "removeAwait", + fix(fixer) { + const sourceCode = context.sourceCode; + const [awaitToken, tokenAfterAwait] = sourceCode.getFirstTokens(node, 2); + + const areAwaitAndAwaitedExpressionOnTheSameLine = awaitToken.loc.start.line === tokenAfterAwait.loc.start.line; + + if (!areAwaitAndAwaitedExpressionOnTheSameLine) { + return null; + } + + const [startOfAwait, endOfAwait] = awaitToken.range; + + const characterAfterAwait = sourceCode.text[endOfAwait]; + const trimLength = characterAfterAwait === " " ? 1 : 0; + + const range = [startOfAwait, endOfAwait + trimLength]; + + return fixer.removeRange(range); + } + } + ] + + }); + } + + /** + * Determines whether a thrown error from this node will be caught/handled within this function rather than immediately halting + * this function. For example, a statement in a `try` block will always have an error handler. A statement in + * a `catch` block will only have an error handler if there is also a `finally` block. + * @param {ASTNode} node A node representing a location where an could be thrown + * @returns {boolean} `true` if a thrown error will be caught/handled in this function + */ + function hasErrorHandler(node) { + let ancestor = node; + + while (!astUtils.isFunction(ancestor) && ancestor.type !== "Program") { + if (ancestor.parent.type === "TryStatement" && (ancestor === ancestor.parent.block || ancestor === ancestor.parent.handler && ancestor.parent.finalizer)) { + return true; + } + ancestor = ancestor.parent; + } + return false; + } + + /** + * Checks if a node is placed in tail call position. Once `return` arguments (or arrow function expressions) can be a complex expression, + * an `await` expression could or could not be unnecessary by the definition of this rule. So we're looking for `await` expressions that are in tail position. + * @param {ASTNode} node A node representing the `await` expression to check + * @returns {boolean} The checking result + */ + function isInTailCallPosition(node) { + if (node.parent.type === "ArrowFunctionExpression") { + return true; + } + if (node.parent.type === "ReturnStatement") { + return !hasErrorHandler(node.parent); + } + if (node.parent.type === "ConditionalExpression" && (node === node.parent.consequent || node === node.parent.alternate)) { + return isInTailCallPosition(node.parent); + } + if (node.parent.type === "LogicalExpression" && node === node.parent.right) { + return isInTailCallPosition(node.parent); + } + if (node.parent.type === "SequenceExpression" && node === node.parent.expressions[node.parent.expressions.length - 1]) { + return isInTailCallPosition(node.parent); + } + return false; + } + + return { + AwaitExpression(node) { + if (isInTailCallPosition(node) && !hasErrorHandler(node)) { + reportUnnecessaryAwait(node); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-script-url.js b/node_modules/eslint/lib/rules/no-script-url.js index fe550b6de..1d16bde3c 100644 --- a/node_modules/eslint/lib/rules/no-script-url.js +++ b/node_modules/eslint/lib/rules/no-script-url.js @@ -1,5 +1,5 @@ /** - * @fileoverview Rule to disallow `javascript:` URLs + * @fileoverview Rule to flag when using javascript: urls * @author Ilya Volodin */ /* eslint no-script-url: 0 -- Code is checking to report such URLs */ @@ -12,57 +12,50 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: "Disallow `javascript:` URLs", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-script-url", - }, - - schema: [], - - messages: { - unexpectedScriptURL: "Script URL is a form of eval.", - }, - }, - - create(context) { - /** - * Check whether a node's static value starts with `javascript:` or not. - * And report an error for unexpected script URL. - * @param {ASTNode} node node to check - * @returns {void} - */ - function check(node) { - const value = astUtils.getStaticStringValue(node); - - if ( - typeof value === "string" && - value.toLowerCase().indexOf("javascript:") === 0 - ) { - context.report({ node, messageId: "unexpectedScriptURL" }); - } - } - return { - Literal(node) { - if (node.value && typeof node.value === "string") { - check(node); - } - }, - TemplateLiteral(node) { - if ( - !( - node.parent && - node.parent.type === "TaggedTemplateExpression" - ) - ) { - check(node); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow `javascript:` urls", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-script-url" + }, + + schema: [], + + messages: { + unexpectedScriptURL: "Script URL is a form of eval." + } + }, + + create(context) { + + /** + * Check whether a node's static value starts with "javascript:" or not. + * And report an error for unexpected script URL. + * @param {ASTNode} node node to check + * @returns {void} + */ + function check(node) { + const value = astUtils.getStaticStringValue(node); + + if (typeof value === "string" && value.toLowerCase().indexOf("javascript:") === 0) { + context.report({ node, messageId: "unexpectedScriptURL" }); + } + } + return { + Literal(node) { + if (node.value && typeof node.value === "string") { + check(node); + } + }, + TemplateLiteral(node) { + if (!(node.parent && node.parent.type === "TaggedTemplateExpression")) { + check(node); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-self-assign.js b/node_modules/eslint/lib/rules/no-self-assign.js index c1831bb03..33ac8fb50 100644 --- a/node_modules/eslint/lib/rules/no-self-assign.js +++ b/node_modules/eslint/lib/rules/no-self-assign.js @@ -28,159 +28,156 @@ const SPACES = /\s+/gu; * @returns {void} */ function eachSelfAssignment(left, right, props, report) { - if (!left || !right) { - // do nothing - } else if ( - left.type === "Identifier" && - right.type === "Identifier" && - left.name === right.name - ) { - report(right); - } else if ( - left.type === "ArrayPattern" && - right.type === "ArrayExpression" - ) { - const end = Math.min(left.elements.length, right.elements.length); - - for (let i = 0; i < end; ++i) { - const leftElement = left.elements[i]; - const rightElement = right.elements[i]; - - // Avoid cases such as [...a] = [...a, 1] - if ( - leftElement && - leftElement.type === "RestElement" && - i < right.elements.length - 1 - ) { - break; - } - - eachSelfAssignment(leftElement, rightElement, props, report); - - // After a spread element, those indices are unknown. - if (rightElement && rightElement.type === "SpreadElement") { - break; - } - } - } else if (left.type === "RestElement" && right.type === "SpreadElement") { - eachSelfAssignment(left.argument, right.argument, props, report); - } else if ( - left.type === "ObjectPattern" && - right.type === "ObjectExpression" && - right.properties.length >= 1 - ) { - /* - * Gets the index of the last spread property. - * It's possible to overwrite properties followed by it. - */ - let startJ = 0; - - for (let i = right.properties.length - 1; i >= 0; --i) { - const propType = right.properties[i].type; - - if ( - propType === "SpreadElement" || - propType === "ExperimentalSpreadProperty" - ) { - startJ = i + 1; - break; - } - } - - for (let i = 0; i < left.properties.length; ++i) { - for (let j = startJ; j < right.properties.length; ++j) { - eachSelfAssignment( - left.properties[i], - right.properties[j], - props, - report, - ); - } - } - } else if ( - left.type === "Property" && - right.type === "Property" && - right.kind === "init" && - !right.method - ) { - const leftName = astUtils.getStaticPropertyName(left); - - if ( - leftName !== null && - leftName === astUtils.getStaticPropertyName(right) - ) { - eachSelfAssignment(left.value, right.value, props, report); - } - } else if ( - props && - astUtils.skipChainExpression(left).type === "MemberExpression" && - astUtils.skipChainExpression(right).type === "MemberExpression" && - astUtils.isSameReference(left, right) - ) { - report(right); - } + if (!left || !right) { + + // do nothing + } else if ( + left.type === "Identifier" && + right.type === "Identifier" && + left.name === right.name + ) { + report(right); + } else if ( + left.type === "ArrayPattern" && + right.type === "ArrayExpression" + ) { + const end = Math.min(left.elements.length, right.elements.length); + + for (let i = 0; i < end; ++i) { + const leftElement = left.elements[i]; + const rightElement = right.elements[i]; + + // Avoid cases such as [...a] = [...a, 1] + if ( + leftElement && + leftElement.type === "RestElement" && + i < right.elements.length - 1 + ) { + break; + } + + eachSelfAssignment(leftElement, rightElement, props, report); + + // After a spread element, those indices are unknown. + if (rightElement && rightElement.type === "SpreadElement") { + break; + } + } + } else if ( + left.type === "RestElement" && + right.type === "SpreadElement" + ) { + eachSelfAssignment(left.argument, right.argument, props, report); + } else if ( + left.type === "ObjectPattern" && + right.type === "ObjectExpression" && + right.properties.length >= 1 + ) { + + /* + * Gets the index of the last spread property. + * It's possible to overwrite properties followed by it. + */ + let startJ = 0; + + for (let i = right.properties.length - 1; i >= 0; --i) { + const propType = right.properties[i].type; + + if (propType === "SpreadElement" || propType === "ExperimentalSpreadProperty") { + startJ = i + 1; + break; + } + } + + for (let i = 0; i < left.properties.length; ++i) { + for (let j = startJ; j < right.properties.length; ++j) { + eachSelfAssignment( + left.properties[i], + right.properties[j], + props, + report + ); + } + } + } else if ( + left.type === "Property" && + right.type === "Property" && + right.kind === "init" && + !right.method + ) { + const leftName = astUtils.getStaticPropertyName(left); + + if (leftName !== null && leftName === astUtils.getStaticPropertyName(right)) { + eachSelfAssignment(left.value, right.value, props, report); + } + } else if ( + props && + astUtils.skipChainExpression(left).type === "MemberExpression" && + astUtils.skipChainExpression(right).type === "MemberExpression" && + astUtils.isSameReference(left, right) + ) { + report(right); + } } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - defaultOptions: [{ props: true }], - - docs: { - description: - "Disallow assignments where both sides are exactly the same", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-self-assign", - }, - - schema: [ - { - type: "object", - properties: { - props: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - - messages: { - selfAssignment: "'{{name}}' is assigned to itself.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - const [{ props }] = context.options; - - /** - * Reports a given node as self assignments. - * @param {ASTNode} node A node to report. This is an Identifier node. - * @returns {void} - */ - function report(node) { - context.report({ - node, - messageId: "selfAssignment", - data: { - name: sourceCode.getText(node).replace(SPACES, ""), - }, - }); - } - - return { - AssignmentExpression(node) { - if (["=", "&&=", "||=", "??="].includes(node.operator)) { - eachSelfAssignment(node.left, node.right, props, report); - } - }, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow assignments where both sides are exactly the same", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-self-assign" + }, + + schema: [ + { + type: "object", + properties: { + props: { + type: "boolean", + default: true + } + }, + additionalProperties: false + } + ], + + messages: { + selfAssignment: "'{{name}}' is assigned to itself." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + const [{ props = true } = {}] = context.options; + + /** + * Reports a given node as self assignments. + * @param {ASTNode} node A node to report. This is an Identifier node. + * @returns {void} + */ + function report(node) { + context.report({ + node, + messageId: "selfAssignment", + data: { + name: sourceCode.getText(node).replace(SPACES, "") + } + }); + } + + return { + AssignmentExpression(node) { + if (["=", "&&=", "||=", "??="].includes(node.operator)) { + eachSelfAssignment(node.left, node.right, props, report); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-self-compare.js b/node_modules/eslint/lib/rules/no-self-compare.js index ee05d3a60..3b076eba8 100644 --- a/node_modules/eslint/lib/rules/no-self-compare.js +++ b/node_modules/eslint/lib/rules/no-self-compare.js @@ -10,68 +10,51 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - docs: { - description: - "Disallow comparisons where both sides are exactly the same", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-self-compare", - }, - - schema: [], - - messages: { - comparingToSelf: "Comparing to itself is potentially pointless.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - /** - * Determines whether two nodes are composed of the same tokens. - * @param {ASTNode} nodeA The first node - * @param {ASTNode} nodeB The second node - * @returns {boolean} true if the nodes have identical token representations - */ - function hasSameTokens(nodeA, nodeB) { - const tokensA = sourceCode.getTokens(nodeA); - const tokensB = sourceCode.getTokens(nodeB); - - return ( - tokensA.length === tokensB.length && - tokensA.every( - (token, index) => - token.type === tokensB[index].type && - token.value === tokensB[index].value, - ) - ); - } - - return { - BinaryExpression(node) { - const operators = new Set([ - "===", - "==", - "!==", - "!=", - ">", - "<", - ">=", - "<=", - ]); - - if ( - operators.has(node.operator) && - hasSameTokens(node.left, node.right) - ) { - context.report({ node, messageId: "comparingToSelf" }); - } - }, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow comparisons where both sides are exactly the same", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-self-compare" + }, + + schema: [], + + messages: { + comparingToSelf: "Comparing to itself is potentially pointless." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + + /** + * Determines whether two nodes are composed of the same tokens. + * @param {ASTNode} nodeA The first node + * @param {ASTNode} nodeB The second node + * @returns {boolean} true if the nodes have identical token representations + */ + function hasSameTokens(nodeA, nodeB) { + const tokensA = sourceCode.getTokens(nodeA); + const tokensB = sourceCode.getTokens(nodeB); + + return tokensA.length === tokensB.length && + tokensA.every((token, index) => token.type === tokensB[index].type && token.value === tokensB[index].value); + } + + return { + + BinaryExpression(node) { + const operators = new Set(["===", "==", "!==", "!=", ">", "<", ">=", "<="]); + + if (operators.has(node.operator) && hasSameTokens(node.left, node.right)) { + context.report({ node, messageId: "comparingToSelf" }); + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-sequences.js b/node_modules/eslint/lib/rules/no-sequences.js index 96a8ba4e3..cd21fc784 100644 --- a/node_modules/eslint/lib/rules/no-sequences.js +++ b/node_modules/eslint/lib/rules/no-sequences.js @@ -15,144 +15,124 @@ const astUtils = require("./utils/ast-utils"); // Helpers //------------------------------------------------------------------------------ +const DEFAULT_OPTIONS = { + allowInParentheses: true +}; + //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: "Disallow comma operators", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-sequences", - }, - - schema: [ - { - type: "object", - properties: { - allowInParentheses: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - - defaultOptions: [ - { - allowInParentheses: true, - }, - ], - - messages: { - unexpectedCommaExpression: "Unexpected use of comma operator.", - }, - }, - - create(context) { - const [{ allowInParentheses }] = context.options; - const sourceCode = context.sourceCode; - - /** - * Parts of the grammar that are required to have parens. - */ - const parenthesized = { - DoWhileStatement: "test", - IfStatement: "test", - SwitchStatement: "discriminant", - WhileStatement: "test", - WithStatement: "object", - ArrowFunctionExpression: "body", - - /* - * Omitting CallExpression - commas are parsed as argument separators - * Omitting NewExpression - commas are parsed as argument separators - * Omitting ForInStatement - parts aren't individually parenthesised - * Omitting ForStatement - parts aren't individually parenthesised - */ - }; - - /** - * Determines whether a node is required by the grammar to be wrapped in - * parens, e.g. the test of an if statement. - * @param {ASTNode} node The AST node - * @returns {boolean} True if parens around node belong to parent node. - */ - function requiresExtraParens(node) { - return ( - node.parent && - parenthesized[node.parent.type] && - node === node.parent[parenthesized[node.parent.type]] - ); - } - - /** - * Check if a node is wrapped in parens. - * @param {ASTNode} node The AST node - * @returns {boolean} True if the node has a paren on each side. - */ - function isParenthesised(node) { - return astUtils.isParenthesised(sourceCode, node); - } - - /** - * Check if a node is wrapped in two levels of parens. - * @param {ASTNode} node The AST node - * @returns {boolean} True if two parens surround the node on each side. - */ - function isParenthesisedTwice(node) { - const previousToken = sourceCode.getTokenBefore(node, 1), - nextToken = sourceCode.getTokenAfter(node, 1); - - return ( - isParenthesised(node) && - previousToken && - nextToken && - astUtils.isOpeningParenToken(previousToken) && - previousToken.range[1] <= node.range[0] && - astUtils.isClosingParenToken(nextToken) && - nextToken.range[0] >= node.range[1] - ); - } - - return { - SequenceExpression(node) { - // Always allow sequences in for statement update - if ( - node.parent.type === "ForStatement" && - (node === node.parent.init || node === node.parent.update) - ) { - return; - } - - // Wrapping a sequence in extra parens indicates intent - if (allowInParentheses) { - if (requiresExtraParens(node)) { - if (isParenthesisedTwice(node)) { - return; - } - } else { - if (isParenthesised(node)) { - return; - } - } - } - - const firstCommaToken = sourceCode.getTokenAfter( - node.expressions[0], - astUtils.isCommaToken, - ); - - context.report({ - node, - loc: firstCommaToken.loc, - messageId: "unexpectedCommaExpression", - }); - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow comma operators", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-sequences" + }, + + schema: [{ + properties: { + allowInParentheses: { + type: "boolean", + default: true + } + }, + additionalProperties: false + }], + + messages: { + unexpectedCommaExpression: "Unexpected use of comma operator." + } + }, + + create(context) { + const options = Object.assign({}, DEFAULT_OPTIONS, context.options[0]); + const sourceCode = context.sourceCode; + + /** + * Parts of the grammar that are required to have parens. + */ + const parenthesized = { + DoWhileStatement: "test", + IfStatement: "test", + SwitchStatement: "discriminant", + WhileStatement: "test", + WithStatement: "object", + ArrowFunctionExpression: "body" + + /* + * Omitting CallExpression - commas are parsed as argument separators + * Omitting NewExpression - commas are parsed as argument separators + * Omitting ForInStatement - parts aren't individually parenthesised + * Omitting ForStatement - parts aren't individually parenthesised + */ + }; + + /** + * Determines whether a node is required by the grammar to be wrapped in + * parens, e.g. the test of an if statement. + * @param {ASTNode} node The AST node + * @returns {boolean} True if parens around node belong to parent node. + */ + function requiresExtraParens(node) { + return node.parent && parenthesized[node.parent.type] && + node === node.parent[parenthesized[node.parent.type]]; + } + + /** + * Check if a node is wrapped in parens. + * @param {ASTNode} node The AST node + * @returns {boolean} True if the node has a paren on each side. + */ + function isParenthesised(node) { + return astUtils.isParenthesised(sourceCode, node); + } + + /** + * Check if a node is wrapped in two levels of parens. + * @param {ASTNode} node The AST node + * @returns {boolean} True if two parens surround the node on each side. + */ + function isParenthesisedTwice(node) { + const previousToken = sourceCode.getTokenBefore(node, 1), + nextToken = sourceCode.getTokenAfter(node, 1); + + return isParenthesised(node) && previousToken && nextToken && + astUtils.isOpeningParenToken(previousToken) && previousToken.range[1] <= node.range[0] && + astUtils.isClosingParenToken(nextToken) && nextToken.range[0] >= node.range[1]; + } + + return { + SequenceExpression(node) { + + // Always allow sequences in for statement update + if (node.parent.type === "ForStatement" && + (node === node.parent.init || node === node.parent.update)) { + return; + } + + // Wrapping a sequence in extra parens indicates intent + if (options.allowInParentheses) { + if (requiresExtraParens(node)) { + if (isParenthesisedTwice(node)) { + return; + } + } else { + if (isParenthesised(node)) { + return; + } + } + } + + const firstCommaToken = sourceCode.getTokenAfter(node.expressions[0], astUtils.isCommaToken); + + context.report({ node, loc: firstCommaToken.loc, messageId: "unexpectedCommaExpression" }); + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-setter-return.js b/node_modules/eslint/lib/rules/no-setter-return.js index 63e0de53b..a5abaaa7e 100644 --- a/node_modules/eslint/lib/rules/no-setter-return.js +++ b/node_modules/eslint/lib/rules/no-setter-return.js @@ -10,215 +10,217 @@ //------------------------------------------------------------------------------ const astUtils = require("./utils/ast-utils"); +const { findVariable } = require("@eslint-community/eslint-utils"); //------------------------------------------------------------------------------ // Helpers //------------------------------------------------------------------------------ +/** + * Determines whether the given identifier node is a reference to a global variable. + * @param {ASTNode} node `Identifier` node to check. + * @param {Scope} scope Scope to which the node belongs. + * @returns {boolean} True if the identifier is a reference to a global variable. + */ +function isGlobalReference(node, scope) { + const variable = findVariable(scope, node); + + return variable !== null && variable.scope.type === "global" && variable.defs.length === 0; +} + /** * Determines whether the given node is an argument of the specified global method call, at the given `index` position. * E.g., for given `index === 1`, this function checks for `objectName.methodName(foo, node)`, where objectName is a global variable. * @param {ASTNode} node The node to check. - * @param {SourceCode} sourceCode Source code to which the node belongs. + * @param {Scope} scope Scope to which the node belongs. * @param {string} objectName Name of the global object. * @param {string} methodName Name of the method. * @param {number} index The given position. * @returns {boolean} `true` if the node is argument at the given position. */ -function isArgumentOfGlobalMethodCall( - node, - sourceCode, - objectName, - methodName, - index, -) { - const callNode = node.parent; - - return ( - callNode.type === "CallExpression" && - callNode.arguments[index] === node && - astUtils.isSpecificMemberAccess( - callNode.callee, - objectName, - methodName, - ) && - sourceCode.isGlobalReference( - astUtils.skipChainExpression(callNode.callee).object, - ) - ); +function isArgumentOfGlobalMethodCall(node, scope, objectName, methodName, index) { + const callNode = node.parent; + + return callNode.type === "CallExpression" && + callNode.arguments[index] === node && + astUtils.isSpecificMemberAccess(callNode.callee, objectName, methodName) && + isGlobalReference(astUtils.skipChainExpression(callNode.callee).object, scope); } /** * Determines whether the given node is used as a property descriptor. * @param {ASTNode} node The node to check. - * @param {SourceCode} sourceCode Source code to which the node belongs. + * @param {Scope} scope Scope to which the node belongs. * @returns {boolean} `true` if the node is a property descriptor. */ -function isPropertyDescriptor(node, sourceCode) { - if ( - isArgumentOfGlobalMethodCall( - node, - sourceCode, - "Object", - "defineProperty", - 2, - ) || - isArgumentOfGlobalMethodCall( - node, - sourceCode, - "Reflect", - "defineProperty", - 2, - ) - ) { - return true; - } - - const parent = node.parent; - - if (parent.type === "Property" && parent.value === node) { - const grandparent = parent.parent; - - if ( - grandparent.type === "ObjectExpression" && - (isArgumentOfGlobalMethodCall( - grandparent, - sourceCode, - "Object", - "create", - 1, - ) || - isArgumentOfGlobalMethodCall( - grandparent, - sourceCode, - "Object", - "defineProperties", - 1, - )) - ) { - return true; - } - } - - return false; +function isPropertyDescriptor(node, scope) { + if ( + isArgumentOfGlobalMethodCall(node, scope, "Object", "defineProperty", 2) || + isArgumentOfGlobalMethodCall(node, scope, "Reflect", "defineProperty", 2) + ) { + return true; + } + + const parent = node.parent; + + if ( + parent.type === "Property" && + parent.value === node + ) { + const grandparent = parent.parent; + + if ( + grandparent.type === "ObjectExpression" && + ( + isArgumentOfGlobalMethodCall(grandparent, scope, "Object", "create", 1) || + isArgumentOfGlobalMethodCall(grandparent, scope, "Object", "defineProperties", 1) + ) + ) { + return true; + } + } + + return false; } /** * Determines whether the given function node is used as a setter function. * @param {ASTNode} node The node to check. - * @param {SourceCode} sourceCode Source code to which the node belongs. + * @param {Scope} scope Scope to which the node belongs. * @returns {boolean} `true` if the node is a setter. */ -function isSetter(node, sourceCode) { - const parent = node.parent; - - if ( - (parent.type === "Property" || parent.type === "MethodDefinition") && - parent.kind === "set" && - parent.value === node - ) { - // Setter in an object literal or in a class - return true; - } - - if ( - parent.type === "Property" && - parent.value === node && - astUtils.getStaticPropertyName(parent) === "set" && - parent.parent.type === "ObjectExpression" && - isPropertyDescriptor(parent.parent, sourceCode) - ) { - // Setter in a property descriptor - return true; - } - - return false; +function isSetter(node, scope) { + const parent = node.parent; + + if ( + (parent.type === "Property" || parent.type === "MethodDefinition") && + parent.kind === "set" && + parent.value === node + ) { + + // Setter in an object literal or in a class + return true; + } + + if ( + parent.type === "Property" && + parent.value === node && + astUtils.getStaticPropertyName(parent) === "set" && + parent.parent.type === "ObjectExpression" && + isPropertyDescriptor(parent.parent, scope) + ) { + + // Setter in a property descriptor + return true; + } + + return false; +} + +/** + * Finds function's outer scope. + * @param {Scope} scope Function's own scope. + * @returns {Scope} Function's outer scope. + */ +function getOuterScope(scope) { + const upper = scope.upper; + + if (upper.type === "function-expression-name") { + return upper.upper; + } + + return upper; } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - docs: { - description: "Disallow returning values from setters", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-setter-return", - }, - - schema: [], - - messages: { - returnsValue: "Setter cannot return a value.", - }, - }, - - create(context) { - let funcInfo = null; - const sourceCode = context.sourceCode; - - /** - * Creates and pushes to the stack a function info object for the given function node. - * @param {ASTNode} node The function node. - * @returns {void} - */ - function enterFunction(node) { - funcInfo = { - upper: funcInfo, - isSetter: isSetter(node, sourceCode), - }; - } - - /** - * Pops the current function info object from the stack. - * @returns {void} - */ - function exitFunction() { - funcInfo = funcInfo.upper; - } - - /** - * Reports the given node. - * @param {ASTNode} node Node to report. - * @returns {void} - */ - function report(node) { - context.report({ node, messageId: "returnsValue" }); - } - - return { - /* - * Function declarations cannot be setters, but we still have to track them in the `funcInfo` stack to avoid - * false positives, because a ReturnStatement node can belong to a function declaration inside a setter. - * - * Note: A previously declared function can be referenced and actually used as a setter in a property descriptor, - * but that's out of scope for this rule. - */ - FunctionDeclaration: enterFunction, - FunctionExpression: enterFunction, - ArrowFunctionExpression(node) { - enterFunction(node); - - if (funcInfo.isSetter && node.expression) { - // { set: foo => bar } property descriptor. Report implicit return 'bar' as the equivalent for a return statement. - report(node.body); - } - }, - - "FunctionDeclaration:exit": exitFunction, - "FunctionExpression:exit": exitFunction, - "ArrowFunctionExpression:exit": exitFunction, - - ReturnStatement(node) { - // Global returns (e.g., at the top level of a Node module) don't have `funcInfo`. - if (funcInfo && funcInfo.isSetter && node.argument) { - report(node); - } - }, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow returning values from setters", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-setter-return" + }, + + schema: [], + + messages: { + returnsValue: "Setter cannot return a value." + } + }, + + create(context) { + let funcInfo = null; + const sourceCode = context.sourceCode; + + /** + * Creates and pushes to the stack a function info object for the given function node. + * @param {ASTNode} node The function node. + * @returns {void} + */ + function enterFunction(node) { + const outerScope = getOuterScope(sourceCode.getScope(node)); + + funcInfo = { + upper: funcInfo, + isSetter: isSetter(node, outerScope) + }; + } + + /** + * Pops the current function info object from the stack. + * @returns {void} + */ + function exitFunction() { + funcInfo = funcInfo.upper; + } + + /** + * Reports the given node. + * @param {ASTNode} node Node to report. + * @returns {void} + */ + function report(node) { + context.report({ node, messageId: "returnsValue" }); + } + + return { + + /* + * Function declarations cannot be setters, but we still have to track them in the `funcInfo` stack to avoid + * false positives, because a ReturnStatement node can belong to a function declaration inside a setter. + * + * Note: A previously declared function can be referenced and actually used as a setter in a property descriptor, + * but that's out of scope for this rule. + */ + FunctionDeclaration: enterFunction, + FunctionExpression: enterFunction, + ArrowFunctionExpression(node) { + enterFunction(node); + + if (funcInfo.isSetter && node.expression) { + + // { set: foo => bar } property descriptor. Report implicit return 'bar' as the equivalent for a return statement. + report(node.body); + } + }, + + "FunctionDeclaration:exit": exitFunction, + "FunctionExpression:exit": exitFunction, + "ArrowFunctionExpression:exit": exitFunction, + + ReturnStatement(node) { + + // Global returns (e.g., at the top level of a Node module) don't have `funcInfo`. + if (funcInfo && funcInfo.isSetter && node.argument) { + report(node); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-shadow-restricted-names.js b/node_modules/eslint/lib/rules/no-shadow-restricted-names.js index 949eed8e6..29560fffc 100644 --- a/node_modules/eslint/lib/rules/no-shadow-restricted-names.js +++ b/node_modules/eslint/lib/rules/no-shadow-restricted-names.js @@ -1,5 +1,5 @@ /** - * @fileoverview Disallow shadowing of globalThis, NaN, undefined, and Infinity (ES2020 section 18.1) + * @fileoverview Disallow shadowing of NaN, undefined, and Infinity (ES5 section 15.1.1) * @author Michael Ficarra */ "use strict"; @@ -12,102 +12,54 @@ * @returns {boolean} true if this variable safely shadows `undefined` */ function safelyShadowsUndefined(variable) { - return ( - variable.name === "undefined" && - variable.references.every(ref => !ref.isWrite()) && - variable.defs.every( - def => - def.node.type === "VariableDeclarator" && - def.node.init === null, - ) - ); + return variable.name === "undefined" && + variable.references.every(ref => !ref.isWrite()) && + variable.defs.every(def => def.node.type === "VariableDeclarator" && def.node.init === null); } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", + meta: { + type: "suggestion", - defaultOptions: [ - { - reportGlobalThis: false, - }, - ], + docs: { + description: "Disallow identifiers from shadowing restricted names", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-shadow-restricted-names" + }, - docs: { - description: "Disallow identifiers from shadowing restricted names", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-shadow-restricted-names", - }, + schema: [], - schema: [ - { - type: "object", - properties: { - reportGlobalThis: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], + messages: { + shadowingRestrictedName: "Shadowing of global property '{{name}}'." + } + }, - messages: { - shadowingRestrictedName: "Shadowing of global property '{{name}}'.", - }, - }, + create(context) { - create(context) { - const [{ reportGlobalThis }] = context.options; - const RESTRICTED = new Set([ - "undefined", - "NaN", - "Infinity", - "arguments", - "eval", - ]); + const RESTRICTED = new Set(["undefined", "NaN", "Infinity", "arguments", "eval"]); + const sourceCode = context.sourceCode; - if (reportGlobalThis) { - RESTRICTED.add("globalThis"); - } + return { + "VariableDeclaration, :function, CatchClause"(node) { + for (const variable of sourceCode.getDeclaredVariables(node)) { + if (variable.defs.length > 0 && RESTRICTED.has(variable.name) && !safelyShadowsUndefined(variable)) { + context.report({ + node: variable.defs[0].name, + messageId: "shadowingRestrictedName", + data: { + name: variable.name + } + }); + } + } + } + }; - const sourceCode = context.sourceCode; - - // Track reported nodes to avoid duplicate reports. For example, on class declarations. - const reportedNodes = new Set(); - - return { - "VariableDeclaration, :function, CatchClause, ImportDeclaration, ClassDeclaration, ClassExpression"( - node, - ) { - for (const variable of sourceCode.getDeclaredVariables(node)) { - if ( - variable.defs.length > 0 && - RESTRICTED.has(variable.name) && - !safelyShadowsUndefined(variable) - ) { - for (const def of variable.defs) { - const nodeToReport = def.name; - - if (!reportedNodes.has(nodeToReport)) { - reportedNodes.add(nodeToReport); - context.report({ - node: nodeToReport, - messageId: "shadowingRestrictedName", - data: { - name: variable.name, - }, - }); - } - } - } - } - }, - }; - }, + } }; diff --git a/node_modules/eslint/lib/rules/no-shadow.js b/node_modules/eslint/lib/rules/no-shadow.js index bbd0f9e40..3e4d99822 100644 --- a/node_modules/eslint/lib/rules/no-shadow.js +++ b/node_modules/eslint/lib/rules/no-shadow.js @@ -15,610 +15,322 @@ const astUtils = require("./utils/ast-utils"); // Helpers //------------------------------------------------------------------------------ -const FUNC_EXPR_NODE_TYPES = new Set([ - "ArrowFunctionExpression", - "FunctionExpression", -]); +const FUNC_EXPR_NODE_TYPES = new Set(["ArrowFunctionExpression", "FunctionExpression"]); const CALL_EXPR_NODE_TYPE = new Set(["CallExpression"]); const FOR_IN_OF_TYPE = /^For(?:In|Of)Statement$/u; -const SENTINEL_TYPE = - /^(?:(?:Function|Class)(?:Declaration|Expression)|ArrowFunctionExpression|CatchClause|ImportDeclaration|ExportNamedDeclaration)$/u; - -// TS-specific node types -const TYPES_HOISTED_NODES = new Set([ - "TSInterfaceDeclaration", - "TSTypeAliasDeclaration", -]); - -// TS-specific function variable def types -const ALLOWED_FUNCTION_VARIABLE_DEF_TYPES = new Set([ - "TSCallSignatureDeclaration", - "TSFunctionType", - "TSMethodSignature", - "TSEmptyBodyFunctionExpression", - "TSDeclareFunction", - "TSConstructSignatureDeclaration", - "TSConstructorType", -]); +const SENTINEL_TYPE = /^(?:(?:Function|Class)(?:Declaration|Expression)|ArrowFunctionExpression|CatchClause|ImportDeclaration|ExportNamedDeclaration)$/u; //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - dialects: ["typescript", "javascript"], - language: "javascript", - - defaultOptions: [ - { - allow: [], - builtinGlobals: false, - hoist: "functions", - ignoreOnInitialization: false, - ignoreTypeValueShadow: true, - ignoreFunctionTypeParameterNameValueShadow: true, - }, - ], - - docs: { - description: - "Disallow variable declarations from shadowing variables declared in the outer scope", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-shadow", - }, - - schema: [ - { - type: "object", - properties: { - builtinGlobals: { type: "boolean" }, - hoist: { - enum: [ - "all", - "functions", - "never", - "types", - "functions-and-types", - ], - }, - allow: { - type: "array", - items: { - type: "string", - }, - }, - ignoreOnInitialization: { type: "boolean" }, - ignoreTypeValueShadow: { type: "boolean" }, - ignoreFunctionTypeParameterNameValueShadow: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - - messages: { - noShadow: - "'{{name}}' is already declared in the upper scope on line {{shadowedLine}} column {{shadowedColumn}}.", - noShadowGlobal: "'{{name}}' is already a global variable.", - }, - }, - - create(context) { - const [ - { - builtinGlobals, - hoist, - allow, - ignoreOnInitialization, - ignoreTypeValueShadow, - ignoreFunctionTypeParameterNameValueShadow, - }, - ] = context.options; - const sourceCode = context.sourceCode; - - /** - * Check if a scope is a TypeScript module augmenting the global namespace. - * @param {Scope} scope The scope to check - * @returns {boolean} Whether the scope is a global augmentation - */ - function isGlobalAugmentation(scope) { - return ( - scope.block.kind === "global" || - (!!scope.upper && isGlobalAugmentation(scope.upper)) - ); - } - - /** - * Check if variable is a `this` parameter. - * @param {Object} variable The variable to check - * @returns {boolean} Whether the variable is a this parameter - */ - function isThisParam(variable) { - return variable.name === "this"; - } - - /** - * Checks if type and value shadows each other - * @param {Object} variable The variable to check - * @param {Object} shadowedVariable The shadowed variable - * @returns {boolean} Whether it's a type/value shadow case to ignore - */ - function isTypeValueShadow(variable, shadowedVariable) { - if (ignoreTypeValueShadow !== true) { - return false; - } - - if (!("isValueVariable" in variable)) { - return false; - } - - const firstDefinition = shadowedVariable.defs[0]; - - // Check if shadowedVariable is a type import - const isTypeImport = - firstDefinition && - firstDefinition.parent?.type === "ImportDeclaration" && - (firstDefinition.parent.importKind === "type" || - firstDefinition.parent.specifiers.some( - s => s.importKind === "type", - )); - - const isShadowedValue = - !firstDefinition || - (isTypeImport ? false : shadowedVariable.isValueVariable); - - return variable.isValueVariable !== isShadowedValue; - } - - /** - * Checks if it's a function type parameter shadow - * @param {Object} variable The variable to check - * @returns {boolean} Whether it's a function type parameter shadow case to ignore - */ - function isFunctionTypeParameterNameValueShadow(variable) { - if (ignoreFunctionTypeParameterNameValueShadow !== true) { - return false; - } - - return variable.defs.some(def => - ALLOWED_FUNCTION_VARIABLE_DEF_TYPES.has(def.node.type), - ); - } - - /** - * Checks if the variable is a generic of a static method - * @param {Object} variable The variable to check - * @returns {boolean} Whether the variable is a generic of a static method - */ - function isTypeParameterOfStaticMethod(variable) { - const typeParameter = variable.identifiers[0].parent; - const typeParameterDecl = typeParameter.parent; - if (typeParameterDecl.type !== "TSTypeParameterDeclaration") { - return false; - } - const functionExpr = typeParameterDecl.parent; - const methodDefinition = functionExpr.parent; - return methodDefinition.static; - } - - /** - * Checks for static method generic shadowing class generic - * @param {Object} variable The variable to check - * @returns {boolean} Whether it's a static method generic shadowing class generic - */ - function isGenericOfAStaticMethodShadow(variable) { - return isTypeParameterOfStaticMethod(variable); - } - - /** - * Checks whether or not a given location is inside of the range of a given node. - * @param {ASTNode} node An node to check. - * @param {number} location A location to check. - * @returns {boolean} `true` if the location is inside of the range of the node. - */ - function isInRange(node, location) { - return ( - node && node.range[0] <= location && location <= node.range[1] - ); - } - - /** - * Searches from the current node through its ancestry to find a matching node. - * @param {ASTNode} node a node to get. - * @param {(node: ASTNode) => boolean} match a callback that checks whether or not the node verifies its condition or not. - * @returns {ASTNode|null} the matching node. - */ - function findSelfOrAncestor(node, match) { - let currentNode = node; - - while (currentNode && !match(currentNode)) { - currentNode = currentNode.parent; - } - return currentNode; - } - - /** - * Finds function's outer scope. - * @param {Scope} scope Function's own scope. - * @returns {Scope} Function's outer scope. - */ - function getOuterScope(scope) { - const upper = scope.upper; - - if (upper && upper.type === "function-expression-name") { - return upper.upper; - } - return upper; - } - - /** - * Checks if a variable and a shadowedVariable have the same init pattern ancestor. - * @param {Object} variable a variable to check. - * @param {Object} shadowedVariable a shadowedVariable to check. - * @returns {boolean} Whether or not the variable and the shadowedVariable have the same init pattern ancestor. - */ - function isInitPatternNode(variable, shadowedVariable) { - const outerDef = shadowedVariable.defs[0]; - - if (!outerDef) { - return false; - } - - const { variableScope } = variable.scope; - - if ( - !( - FUNC_EXPR_NODE_TYPES.has(variableScope.block.type) && - getOuterScope(variableScope) === shadowedVariable.scope - ) - ) { - return false; - } - - const fun = variableScope.block; - const { parent } = fun; - - const callExpression = findSelfOrAncestor(parent, node => - CALL_EXPR_NODE_TYPE.has(node.type), - ); - - if (!callExpression) { - return false; - } - - let node = outerDef.name; - const location = callExpression.range[1]; - - while (node) { - if (node.type === "VariableDeclarator") { - if (isInRange(node.init, location)) { - return true; - } - if ( - FOR_IN_OF_TYPE.test(node.parent.parent.type) && - isInRange(node.parent.parent.right, location) - ) { - return true; - } - break; - } else if (node.type === "AssignmentPattern") { - if (isInRange(node.right, location)) { - return true; - } - } else if (SENTINEL_TYPE.test(node.type)) { - break; - } - - node = node.parent; - } - - return false; - } - - /** - * Check if variable name is allowed. - * @param {ASTNode} variable The variable to check. - * @returns {boolean} Whether or not the variable name is allowed. - */ - function isAllowed(variable) { - return allow.includes(variable.name); - } - - /** - * Checks if a variable of the class name in the class scope of ClassDeclaration. - * - * ClassDeclaration creates two variables of its name into its outer scope and its class scope. - * So we should ignore the variable in the class scope. - * @param {Object} variable The variable to check. - * @returns {boolean} Whether or not the variable of the class name in the class scope of ClassDeclaration. - */ - function isDuplicatedClassNameVariable(variable) { - const block = variable.scope.block; - - return ( - block.type === "ClassDeclaration" && - block.id === variable.identifiers[0] - ); - } - - /** - * Checks if a variable is inside the initializer of scopeVar. - * - * To avoid reporting at declarations such as `var a = function a() {};`. - * But it should report `var a = function(a) {};` or `var a = function() { function a() {} };`. - * @param {Object} variable The variable to check. - * @param {Object} scopeVar The scope variable to look for. - * @returns {boolean} Whether or not the variable is inside initializer of scopeVar. - */ - function isOnInitializer(variable, scopeVar) { - const outerScope = scopeVar.scope; - const outerDef = scopeVar.defs[0]; - const outer = outerDef && outerDef.parent && outerDef.parent.range; - const innerScope = variable.scope; - const innerDef = variable.defs[0]; - const inner = innerDef && innerDef.name.range; - - return ( - outer && - inner && - outer[0] < inner[0] && - inner[1] < outer[1] && - ((innerDef.type === "FunctionName" && - innerDef.node.type === "FunctionExpression") || - innerDef.node.type === "ClassExpression") && - outerScope === innerScope.upper - ); - } - - /** - * Get a range of a variable's identifier node. - * @param {Object} variable The variable to get. - * @returns {Array|undefined} The range of the variable's identifier node. - */ - function getNameRange(variable) { - const def = variable.defs[0]; - - return def && def.name.range; - } - - /** - * Get declared line and column of a variable. - * @param {eslint-scope.Variable} variable The variable to get. - * @returns {Object} The declared line and column of the variable. - */ - function getDeclaredLocation(variable) { - const identifier = variable.identifiers[0]; - let obj; - - if (identifier) { - obj = { - global: false, - line: identifier.loc.start.line, - column: identifier.loc.start.column + 1, - }; - } else { - obj = { - global: true, - }; - } - return obj; - } - - /** - * Checks if a variable is in TDZ of scopeVar. - * @param {Object} variable The variable to check. - * @param {Object} scopeVar The variable of TDZ. - * @returns {boolean} Whether or not the variable is in TDZ of scopeVar. - */ - function isInTdz(variable, scopeVar) { - const outerDef = scopeVar.defs[0]; - const inner = getNameRange(variable); - const outer = getNameRange(scopeVar); - - if (!outer || inner[1] >= outer[0]) { - return false; - } - - if (hoist === "types") { - return !TYPES_HOISTED_NODES.has(outerDef.node.type); - } - - if (hoist === "functions-and-types") { - return ( - outerDef.node.type !== "FunctionDeclaration" && - !TYPES_HOISTED_NODES.has(outerDef.node.type) - ); - } - - return ( - inner && - outer && - inner[1] < outer[0] && - // Excepts FunctionDeclaration if is {"hoist":"function"}. - (hoist !== "functions" || - !outerDef || - outerDef.node.type !== "FunctionDeclaration") - ); - } - - /** - * Checks if the initialization of a variable has the declare modifier in a - * definition file. - * @param {Object} variable The variable to check - * @returns {boolean} Whether the variable is declared in a definition file - */ - function isDeclareInDTSFile(variable) { - const fileName = context.filename; - if ( - !fileName.endsWith(".d.ts") && - !fileName.endsWith(".d.cts") && - !fileName.endsWith(".d.mts") - ) { - return false; - } - return variable.defs.some( - def => - (def.type === "Variable" && def.parent.declare) || - (def.type === "ClassName" && def.node.declare) || - (def.type === "TSEnumName" && def.node.declare) || - (def.type === "TSModuleName" && def.node.declare), - ); - } - - /** - * Checks if a variable is a duplicate of an enum name in the enum scope - * @param {Object} variable The variable to check - * @returns {boolean} Whether it's a duplicate enum name variable - */ - function isDuplicatedEnumNameVariable(variable) { - const block = variable.scope.block; - - return ( - block.type === "TSEnumDeclaration" && - block.id === variable.identifiers[0] - ); - } - - /** - * Check if this is an external module declaration merging with a type import - * @param {Scope} scope Current scope - * @param {Object} variable Current variable - * @param {Object} shadowedVariable Shadowed variable - * @returns {boolean} Whether it's an external declaration merging - */ - function isExternalDeclarationMerging( - scope, - variable, - shadowedVariable, - ) { - const firstDefinition = shadowedVariable.defs[0]; - - if (!firstDefinition || !firstDefinition.parent) { - return false; - } - - // Check if the shadowed variable is a type import - const isTypeImport = - firstDefinition.parent.type === "ImportDeclaration" && - (firstDefinition.parent.importKind === "type" || - firstDefinition.parent.specifiers?.some( - s => - s.type === "ImportSpecifier" && - s.importKind === "type" && - s.local.name === shadowedVariable.name, - )); - - if (!isTypeImport) { - return false; - } - - // Check if the current variable is within a module declaration - const moduleDecl = findSelfOrAncestor( - variable.identifiers[0]?.parent, - node => node.type === "TSModuleDeclaration", - ); - - if (!moduleDecl) { - return false; - } - - /* - * Module declaration merging should only happen within the same module - * Check if the module name matches the import source - */ - const importSource = firstDefinition.parent.source.value; - const moduleName = - moduleDecl.id.type === "Literal" - ? moduleDecl.id.value - : moduleDecl.id.name; - - return importSource === moduleName; - } - - /** - * Checks the current context for shadowed variables. - * @param {Scope} scope Fixme - * @returns {void} - */ - function checkForShadows(scope) { - // ignore global augmentation - if (isGlobalAugmentation(scope)) { - return; - } - - const variables = scope.variables; - - for (let i = 0; i < variables.length; ++i) { - const variable = variables[i]; - - // Skips "arguments" or variables of a class name in the class scope of ClassDeclaration. - if ( - variable.identifiers.length === 0 || - isDuplicatedClassNameVariable(variable) || - isDuplicatedEnumNameVariable(variable) || - isAllowed(variable) || - isDeclareInDTSFile(variable) || - isThisParam(variable) - ) { - continue; - } - - // Gets shadowed variable. - const shadowed = astUtils.getVariableByName( - scope.upper, - variable.name, - ); - - if ( - shadowed && - (shadowed.identifiers.length > 0 || - (builtinGlobals && "writeable" in shadowed)) && - !isOnInitializer(variable, shadowed) && - !( - ignoreOnInitialization && - isInitPatternNode(variable, shadowed) - ) && - !(hoist !== "all" && isInTdz(variable, shadowed)) && - !isTypeValueShadow(variable, shadowed) && - !isFunctionTypeParameterNameValueShadow(variable) && - !isGenericOfAStaticMethodShadow(variable, shadowed) && - !isExternalDeclarationMerging(scope, variable, shadowed) - ) { - const location = getDeclaredLocation(shadowed); - const messageId = location.global - ? "noShadowGlobal" - : "noShadow"; - const data = { name: variable.name }; - - if (!location.global) { - data.shadowedLine = location.line; - data.shadowedColumn = location.column; - } - context.report({ - node: variable.identifiers[0], - messageId, - data, - }); - } - } - } - - return { - "Program:exit"(node) { - const globalScope = sourceCode.getScope(node); - const stack = globalScope.childScopes.slice(); - - while (stack.length) { - const scope = stack.pop(); - - stack.push(...scope.childScopes); - checkForShadows(scope); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow variable declarations from shadowing variables declared in the outer scope", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-shadow" + }, + + schema: [ + { + type: "object", + properties: { + builtinGlobals: { type: "boolean", default: false }, + hoist: { enum: ["all", "functions", "never"], default: "functions" }, + allow: { + type: "array", + items: { + type: "string" + } + }, + ignoreOnInitialization: { type: "boolean", default: false } + }, + additionalProperties: false + } + ], + + messages: { + noShadow: "'{{name}}' is already declared in the upper scope on line {{shadowedLine}} column {{shadowedColumn}}.", + noShadowGlobal: "'{{name}}' is already a global variable." + } + }, + + create(context) { + + const options = { + builtinGlobals: context.options[0] && context.options[0].builtinGlobals, + hoist: (context.options[0] && context.options[0].hoist) || "functions", + allow: (context.options[0] && context.options[0].allow) || [], + ignoreOnInitialization: context.options[0] && context.options[0].ignoreOnInitialization + }; + const sourceCode = context.sourceCode; + + /** + * Checks whether or not a given location is inside of the range of a given node. + * @param {ASTNode} node An node to check. + * @param {number} location A location to check. + * @returns {boolean} `true` if the location is inside of the range of the node. + */ + function isInRange(node, location) { + return node && node.range[0] <= location && location <= node.range[1]; + } + + /** + * Searches from the current node through its ancestry to find a matching node. + * @param {ASTNode} node a node to get. + * @param {(node: ASTNode) => boolean} match a callback that checks whether or not the node verifies its condition or not. + * @returns {ASTNode|null} the matching node. + */ + function findSelfOrAncestor(node, match) { + let currentNode = node; + + while (currentNode && !match(currentNode)) { + currentNode = currentNode.parent; + } + return currentNode; + } + + /** + * Finds function's outer scope. + * @param {Scope} scope Function's own scope. + * @returns {Scope} Function's outer scope. + */ + function getOuterScope(scope) { + const upper = scope.upper; + + if (upper.type === "function-expression-name") { + return upper.upper; + } + return upper; + } + + /** + * Checks if a variable and a shadowedVariable have the same init pattern ancestor. + * @param {Object} variable a variable to check. + * @param {Object} shadowedVariable a shadowedVariable to check. + * @returns {boolean} Whether or not the variable and the shadowedVariable have the same init pattern ancestor. + */ + function isInitPatternNode(variable, shadowedVariable) { + const outerDef = shadowedVariable.defs[0]; + + if (!outerDef) { + return false; + } + + const { variableScope } = variable.scope; + + + if (!(FUNC_EXPR_NODE_TYPES.has(variableScope.block.type) && getOuterScope(variableScope) === shadowedVariable.scope)) { + return false; + } + + const fun = variableScope.block; + const { parent } = fun; + + const callExpression = findSelfOrAncestor( + parent, + node => CALL_EXPR_NODE_TYPE.has(node.type) + ); + + if (!callExpression) { + return false; + } + + let node = outerDef.name; + const location = callExpression.range[1]; + + while (node) { + if (node.type === "VariableDeclarator") { + if (isInRange(node.init, location)) { + return true; + } + if (FOR_IN_OF_TYPE.test(node.parent.parent.type) && + isInRange(node.parent.parent.right, location) + ) { + return true; + } + break; + } else if (node.type === "AssignmentPattern") { + if (isInRange(node.right, location)) { + return true; + } + } else if (SENTINEL_TYPE.test(node.type)) { + break; + } + + node = node.parent; + } + + return false; + } + + /** + * Check if variable name is allowed. + * @param {ASTNode} variable The variable to check. + * @returns {boolean} Whether or not the variable name is allowed. + */ + function isAllowed(variable) { + return options.allow.includes(variable.name); + } + + /** + * Checks if a variable of the class name in the class scope of ClassDeclaration. + * + * ClassDeclaration creates two variables of its name into its outer scope and its class scope. + * So we should ignore the variable in the class scope. + * @param {Object} variable The variable to check. + * @returns {boolean} Whether or not the variable of the class name in the class scope of ClassDeclaration. + */ + function isDuplicatedClassNameVariable(variable) { + const block = variable.scope.block; + + return block.type === "ClassDeclaration" && block.id === variable.identifiers[0]; + } + + /** + * Checks if a variable is inside the initializer of scopeVar. + * + * To avoid reporting at declarations such as `var a = function a() {};`. + * But it should report `var a = function(a) {};` or `var a = function() { function a() {} };`. + * @param {Object} variable The variable to check. + * @param {Object} scopeVar The scope variable to look for. + * @returns {boolean} Whether or not the variable is inside initializer of scopeVar. + */ + function isOnInitializer(variable, scopeVar) { + const outerScope = scopeVar.scope; + const outerDef = scopeVar.defs[0]; + const outer = outerDef && outerDef.parent && outerDef.parent.range; + const innerScope = variable.scope; + const innerDef = variable.defs[0]; + const inner = innerDef && innerDef.name.range; + + return ( + outer && + inner && + outer[0] < inner[0] && + inner[1] < outer[1] && + ((innerDef.type === "FunctionName" && innerDef.node.type === "FunctionExpression") || innerDef.node.type === "ClassExpression") && + outerScope === innerScope.upper + ); + } + + /** + * Get a range of a variable's identifier node. + * @param {Object} variable The variable to get. + * @returns {Array|undefined} The range of the variable's identifier node. + */ + function getNameRange(variable) { + const def = variable.defs[0]; + + return def && def.name.range; + } + + /** + * Get declared line and column of a variable. + * @param {eslint-scope.Variable} variable The variable to get. + * @returns {Object} The declared line and column of the variable. + */ + function getDeclaredLocation(variable) { + const identifier = variable.identifiers[0]; + let obj; + + if (identifier) { + obj = { + global: false, + line: identifier.loc.start.line, + column: identifier.loc.start.column + 1 + }; + } else { + obj = { + global: true + }; + } + return obj; + } + + /** + * Checks if a variable is in TDZ of scopeVar. + * @param {Object} variable The variable to check. + * @param {Object} scopeVar The variable of TDZ. + * @returns {boolean} Whether or not the variable is in TDZ of scopeVar. + */ + function isInTdz(variable, scopeVar) { + const outerDef = scopeVar.defs[0]; + const inner = getNameRange(variable); + const outer = getNameRange(scopeVar); + + return ( + inner && + outer && + inner[1] < outer[0] && + + // Excepts FunctionDeclaration if is {"hoist":"function"}. + (options.hoist !== "functions" || !outerDef || outerDef.node.type !== "FunctionDeclaration") + ); + } + + /** + * Checks the current context for shadowed variables. + * @param {Scope} scope Fixme + * @returns {void} + */ + function checkForShadows(scope) { + const variables = scope.variables; + + for (let i = 0; i < variables.length; ++i) { + const variable = variables[i]; + + // Skips "arguments" or variables of a class name in the class scope of ClassDeclaration. + if (variable.identifiers.length === 0 || + isDuplicatedClassNameVariable(variable) || + isAllowed(variable) + ) { + continue; + } + + // Gets shadowed variable. + const shadowed = astUtils.getVariableByName(scope.upper, variable.name); + + if (shadowed && + (shadowed.identifiers.length > 0 || (options.builtinGlobals && "writeable" in shadowed)) && + !isOnInitializer(variable, shadowed) && + !(options.ignoreOnInitialization && isInitPatternNode(variable, shadowed)) && + !(options.hoist !== "all" && isInTdz(variable, shadowed)) + ) { + const location = getDeclaredLocation(shadowed); + const messageId = location.global ? "noShadowGlobal" : "noShadow"; + const data = { name: variable.name }; + + if (!location.global) { + data.shadowedLine = location.line; + data.shadowedColumn = location.column; + } + context.report({ + node: variable.identifiers[0], + messageId, + data + }); + } + } + } + + return { + "Program:exit"(node) { + const globalScope = sourceCode.getScope(node); + const stack = globalScope.childScopes.slice(); + + while (stack.length) { + const scope = stack.pop(); + + stack.push(...scope.childScopes); + checkForShadows(scope); + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-spaced-func.js b/node_modules/eslint/lib/rules/no-spaced-func.js index 037015b7f..d79c18440 100644 --- a/node_modules/eslint/lib/rules/no-spaced-func.js +++ b/node_modules/eslint/lib/rules/no-spaced-func.js @@ -10,96 +10,74 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "layout", + meta: { + type: "layout", - docs: { - description: - "Disallow spacing between function identifiers and their applications (deprecated)", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-spaced-func", - }, + docs: { + description: "Disallow spacing between function identifiers and their applications (deprecated)", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-spaced-func" + }, - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2016/08/eslint-v3.3.0-released/#deprecated-rules", - deprecatedSince: "3.3.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "function-call-spacing", - url: "https://eslint.style/rules/function-call-spacing", - }, - }, - ], - }, + deprecated: true, - fixable: "whitespace", - schema: [], + replacedBy: ["func-call-spacing"], - messages: { - noSpacedFunction: - "Unexpected space between function name and paren.", - }, - }, + fixable: "whitespace", + schema: [], - create(context) { - const sourceCode = context.sourceCode; + messages: { + noSpacedFunction: "Unexpected space between function name and paren." + } + }, - /** - * Check if open space is present in a function name - * @param {ASTNode} node node to evaluate - * @returns {void} - * @private - */ - function detectOpenSpaces(node) { - const lastCalleeToken = sourceCode.getLastToken(node.callee); - let prevToken = lastCalleeToken, - parenToken = sourceCode.getTokenAfter(lastCalleeToken); + create(context) { - // advances to an open parenthesis. - while ( - parenToken && - parenToken.range[1] < node.range[1] && - parenToken.value !== "(" - ) { - prevToken = parenToken; - parenToken = sourceCode.getTokenAfter(parenToken); - } + const sourceCode = context.sourceCode; - // look for a space between the callee and the open paren - if ( - parenToken && - parenToken.range[1] < node.range[1] && - sourceCode.isSpaceBetweenTokens(prevToken, parenToken) - ) { - context.report({ - node, - loc: lastCalleeToken.loc.start, - messageId: "noSpacedFunction", - fix(fixer) { - return fixer.removeRange([ - prevToken.range[1], - parenToken.range[0], - ]); - }, - }); - } - } + /** + * Check if open space is present in a function name + * @param {ASTNode} node node to evaluate + * @returns {void} + * @private + */ + function detectOpenSpaces(node) { + const lastCalleeToken = sourceCode.getLastToken(node.callee); + let prevToken = lastCalleeToken, + parenToken = sourceCode.getTokenAfter(lastCalleeToken); - return { - CallExpression: detectOpenSpaces, - NewExpression: detectOpenSpaces, - }; - }, + // advances to an open parenthesis. + while ( + parenToken && + parenToken.range[1] < node.range[1] && + parenToken.value !== "(" + ) { + prevToken = parenToken; + parenToken = sourceCode.getTokenAfter(parenToken); + } + + // look for a space between the callee and the open paren + if (parenToken && + parenToken.range[1] < node.range[1] && + sourceCode.isSpaceBetweenTokens(prevToken, parenToken) + ) { + context.report({ + node, + loc: lastCalleeToken.loc.start, + messageId: "noSpacedFunction", + fix(fixer) { + return fixer.removeRange([prevToken.range[1], parenToken.range[0]]); + } + }); + } + } + + return { + CallExpression: detectOpenSpaces, + NewExpression: detectOpenSpaces + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-sparse-arrays.js b/node_modules/eslint/lib/rules/no-sparse-arrays.js index f805ecef3..c65b0ab2c 100644 --- a/node_modules/eslint/lib/rules/no-sparse-arrays.js +++ b/node_modules/eslint/lib/rules/no-sparse-arrays.js @@ -4,65 +4,47 @@ */ "use strict"; -const astUtils = require("./utils/ast-utils"); - //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", + meta: { + type: "problem", + + docs: { + description: "Disallow sparse arrays", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-sparse-arrays" + }, + + schema: [], - docs: { - description: "Disallow sparse arrays", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-sparse-arrays", - }, + messages: { + unexpectedSparseArray: "Unexpected comma in middle of array." + } + }, - schema: [], + create(context) { - messages: { - unexpectedSparseArray: "Unexpected comma in middle of array.", - }, - }, - create(context) { - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- - return { - ArrayExpression(node) { - if (!node.elements.includes(null)) { - return; - } + return { - const { sourceCode } = context; - let commaToken; + ArrayExpression(node) { - for (const [index, element] of node.elements.entries()) { - if (index === node.elements.length - 1 && element) { - return; - } + const emptySpot = node.elements.includes(null); - commaToken = sourceCode.getTokenAfter( - element ?? commaToken ?? sourceCode.getFirstToken(node), - astUtils.isCommaToken, - ); + if (emptySpot) { + context.report({ node, messageId: "unexpectedSparseArray" }); + } + } - if (element) { - continue; - } + }; - context.report({ - node, - loc: commaToken.loc, - messageId: "unexpectedSparseArray", - }); - } - }, - }; - }, + } }; diff --git a/node_modules/eslint/lib/rules/no-sync.js b/node_modules/eslint/lib/rules/no-sync.js index 50b5860ee..8f79a36b4 100644 --- a/node_modules/eslint/lib/rules/no-sync.js +++ b/node_modules/eslint/lib/rules/no-sync.js @@ -10,72 +10,55 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Node.js rules were moved out of ESLint core.", - url: "https://eslint.org/docs/latest/use/migrating-to-7.0.0#deprecate-node-rules", - deprecatedSince: "7.0.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "eslint-plugin-n now maintains deprecated Node.js-related rules.", - plugin: { - name: "eslint-plugin-n", - url: "https://github.com/eslint-community/eslint-plugin-n", - }, - rule: { - name: "no-sync", - url: "https://github.com/eslint-community/eslint-plugin-n/tree/master/docs/rules/no-sync.md", - }, - }, - ], - }, + meta: { + deprecated: true, - type: "suggestion", + replacedBy: [], - docs: { - description: "Disallow synchronous methods", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-sync", - }, + type: "suggestion", - schema: [ - { - type: "object", - properties: { - allowAtRootLevel: { - type: "boolean", - default: false, - }, - }, - additionalProperties: false, - }, - ], + docs: { + description: "Disallow synchronous methods", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-sync" + }, - messages: { - noSync: "Unexpected sync method: '{{propertyName}}'.", - }, - }, + schema: [ + { + type: "object", + properties: { + allowAtRootLevel: { + type: "boolean", + default: false + } + }, + additionalProperties: false + } + ], - create(context) { - const selector = - context.options[0] && context.options[0].allowAtRootLevel - ? ":function MemberExpression[property.name=/.*Sync$/]" - : "MemberExpression[property.name=/.*Sync$/]"; + messages: { + noSync: "Unexpected sync method: '{{propertyName}}'." + } + }, - return { - [selector](node) { - context.report({ - node, - messageId: "noSync", - data: { - propertyName: node.property.name, - }, - }); - }, - }; - }, + create(context) { + const selector = context.options[0] && context.options[0].allowAtRootLevel + ? ":function MemberExpression[property.name=/.*Sync$/]" + : "MemberExpression[property.name=/.*Sync$/]"; + + return { + [selector](node) { + context.report({ + node, + messageId: "noSync", + data: { + propertyName: node.property.name + } + }); + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-tabs.js b/node_modules/eslint/lib/rules/no-tabs.js index f35f4da0d..8581e19af 100644 --- a/node_modules/eslint/lib/rules/no-tabs.js +++ b/node_modules/eslint/lib/rules/no-tabs.js @@ -17,94 +17,65 @@ const anyNonWhitespaceRegex = /\S/u; // Public Interface //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "no-tabs", - url: "https://eslint.style/rules/no-tabs", - }, - }, - ], - }, - type: "layout", + meta: { + deprecated: true, + replacedBy: [], + type: "layout", - docs: { - description: "Disallow all tabs", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-tabs", - }, - schema: [ - { - type: "object", - properties: { - allowIndentationTabs: { - type: "boolean", - default: false, - }, - }, - additionalProperties: false, - }, - ], + docs: { + description: "Disallow all tabs", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-tabs" + }, + schema: [{ + type: "object", + properties: { + allowIndentationTabs: { + type: "boolean", + default: false + } + }, + additionalProperties: false + }], - messages: { - unexpectedTab: "Unexpected tab character.", - }, - }, + messages: { + unexpectedTab: "Unexpected tab character." + } + }, - create(context) { - const sourceCode = context.sourceCode; - const allowIndentationTabs = - context.options && - context.options[0] && - context.options[0].allowIndentationTabs; + create(context) { + const sourceCode = context.sourceCode; + const allowIndentationTabs = context.options && context.options[0] && context.options[0].allowIndentationTabs; - return { - Program(node) { - sourceCode.getLines().forEach((line, index) => { - let match; + return { + Program(node) { + sourceCode.getLines().forEach((line, index) => { + let match; - while ((match = tabRegex.exec(line)) !== null) { - if ( - allowIndentationTabs && - !anyNonWhitespaceRegex.test( - line.slice(0, match.index), - ) - ) { - continue; - } + while ((match = tabRegex.exec(line)) !== null) { + if (allowIndentationTabs && !anyNonWhitespaceRegex.test(line.slice(0, match.index))) { + continue; + } - context.report({ - node, - loc: { - start: { - line: index + 1, - column: match.index, - }, - end: { - line: index + 1, - column: match.index + match[0].length, - }, - }, - messageId: "unexpectedTab", - }); - } - }); - }, - }; - }, + context.report({ + node, + loc: { + start: { + line: index + 1, + column: match.index + }, + end: { + line: index + 1, + column: match.index + match[0].length + } + }, + messageId: "unexpectedTab" + }); + } + }); + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-template-curly-in-string.js b/node_modules/eslint/lib/rules/no-template-curly-in-string.js index 901e1aead..92b4c1c86 100644 --- a/node_modules/eslint/lib/rules/no-template-curly-in-string.js +++ b/node_modules/eslint/lib/rules/no-template-curly-in-string.js @@ -8,38 +8,37 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - docs: { - description: - "Disallow template literal placeholder syntax in regular strings", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-template-curly-in-string", - }, - - schema: [], - - messages: { - unexpectedTemplateExpression: - "Unexpected template string expression.", - }, - }, - - create(context) { - const regex = /\$\{[^}]+\}/u; - - return { - Literal(node) { - if (typeof node.value === "string" && regex.test(node.value)) { - context.report({ - node, - messageId: "unexpectedTemplateExpression", - }); - } - }, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow template literal placeholder syntax in regular strings", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-template-curly-in-string" + }, + + schema: [], + + messages: { + unexpectedTemplateExpression: "Unexpected template string expression." + } + }, + + create(context) { + const regex = /\$\{[^}]+\}/u; + + return { + Literal(node) { + if (typeof node.value === "string" && regex.test(node.value)) { + context.report({ + node, + messageId: "unexpectedTemplateExpression" + }); + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-ternary.js b/node_modules/eslint/lib/rules/no-ternary.js index f58136d5e..4d43c7e02 100644 --- a/node_modules/eslint/lib/rules/no-ternary.js +++ b/node_modules/eslint/lib/rules/no-ternary.js @@ -9,30 +9,33 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: "Disallow ternary operators", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/no-ternary", - }, - - schema: [], - - messages: { - noTernaryOperator: "Ternary operator used.", - }, - }, - - create(context) { - return { - ConditionalExpression(node) { - context.report({ node, messageId: "noTernaryOperator" }); - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow ternary operators", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-ternary" + }, + + schema: [], + + messages: { + noTernaryOperator: "Ternary operator used." + } + }, + + create(context) { + + return { + + ConditionalExpression(node) { + context.report({ node, messageId: "noTernaryOperator" }); + } + + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-this-before-super.js b/node_modules/eslint/lib/rules/no-this-before-super.js index 16d049c48..f96d8ace8 100644 --- a/node_modules/eslint/lib/rules/no-this-before-super.js +++ b/node_modules/eslint/lib/rules/no-this-before-super.js @@ -23,343 +23,309 @@ const astUtils = require("./utils/ast-utils"); * @returns {boolean} `true` if the node is a constructor. */ function isConstructorFunction(node) { - return ( - node.type === "FunctionExpression" && - node.parent.type === "MethodDefinition" && - node.parent.kind === "constructor" - ); -} - -/* - * Information for each code path segment. - * - superCalled: The flag which shows `super()` called in all code paths. - * - invalidNodes: The array of invalid ThisExpression and Super nodes. - */ -/** - * - */ -class SegmentInfo { - /** - * Indicates whether `super()` is called in all code paths. - * @type {boolean} - */ - superCalled = false; - - /** - * The array of invalid ThisExpression and Super nodes. - * @type {ASTNode[]} - */ - invalidNodes = []; + return ( + node.type === "FunctionExpression" && + node.parent.type === "MethodDefinition" && + node.parent.kind === "constructor" + ); } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - docs: { - description: - "Disallow `this`/`super` before calling `super()` in constructors", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-this-before-super", - }, - - schema: [], - - messages: { - noBeforeSuper: "'{{kind}}' is not allowed before 'super()'.", - }, - }, - - create(context) { - /* - * Information for each constructor. - * - upper: Information of the upper constructor. - * - hasExtends: A flag which shows whether the owner class has a valid - * `extends` part. - * - scope: The scope of the owner class. - * - codePath: The code path of this constructor. - */ - let funcInfo = null; - - /** @type {Record} */ - let segInfoMap = Object.create(null); - - /** - * Gets whether or not `super()` is called in a given code path segment. - * @param {CodePathSegment} segment A code path segment to get. - * @returns {boolean} `true` if `super()` is called. - */ - function isCalled(segment) { - return !segment.reachable || segInfoMap[segment.id]?.superCalled; - } - - /** - * Checks whether or not this is in a constructor. - * @returns {boolean} `true` if this is in a constructor. - */ - function isInConstructorOfDerivedClass() { - return Boolean( - funcInfo && funcInfo.isConstructor && funcInfo.hasExtends, - ); - } - - /** - * Determines if every segment in a set has been called. - * @param {Set} segments The segments to search. - * @returns {boolean} True if every segment has been called; false otherwise. - */ - function isEverySegmentCalled(segments) { - for (const segment of segments) { - if (!isCalled(segment)) { - return false; - } - } - - return true; - } - - /** - * Checks whether or not this is before `super()` is called. - * @returns {boolean} `true` if this is before `super()` is called. - */ - function isBeforeCallOfSuper() { - return ( - isInConstructorOfDerivedClass() && - !isEverySegmentCalled(funcInfo.currentSegments) - ); - } - - /** - * Sets a given node as invalid. - * @param {ASTNode} node A node to set as invalid. This is one of - * a ThisExpression and a Super. - * @returns {void} - */ - function setInvalid(node) { - const segments = funcInfo.currentSegments; - - for (const segment of segments) { - if (segment.reachable) { - segInfoMap[segment.id].invalidNodes.push(node); - } - } - } - - /** - * Sets the current segment as `super` was called. - * @returns {void} - */ - function setSuperCalled() { - const segments = funcInfo.currentSegments; - - for (const segment of segments) { - if (segment.reachable) { - segInfoMap[segment.id].superCalled = true; - } - } - } - - return { - /** - * Adds information of a constructor into the stack. - * @param {CodePath} codePath A code path which was started. - * @param {ASTNode} node The current node. - * @returns {void} - */ - onCodePathStart(codePath, node) { - if (isConstructorFunction(node)) { - // Class > ClassBody > MethodDefinition > FunctionExpression - const classNode = node.parent.parent.parent; - - funcInfo = { - upper: funcInfo, - isConstructor: true, - hasExtends: Boolean( - classNode.superClass && - !astUtils.isNullOrUndefined( - classNode.superClass, - ), - ), - codePath, - currentSegments: new Set(), - }; - } else { - funcInfo = { - upper: funcInfo, - isConstructor: false, - hasExtends: false, - codePath, - currentSegments: new Set(), - }; - } - }, - - /** - * Removes the top of stack item. - * - * And this traverses all segments of this code path then reports every - * invalid node. - * @param {CodePath} codePath A code path which was ended. - * @returns {void} - */ - onCodePathEnd(codePath) { - const isDerivedClass = funcInfo.hasExtends; - - funcInfo = funcInfo.upper; - if (!isDerivedClass) { - return; - } - - /** - * A collection of nodes to avoid duplicate reports. - * @type {Set} - */ - const reported = new Set(); - - codePath.traverseSegments((segment, controller) => { - const info = segInfoMap[segment.id]; - const invalidNodes = info.invalidNodes.filter( - /* - * Avoid duplicate reports. - * When there is a `finally`, invalidNodes may contain already reported node. - */ - node => !reported.has(node), - ); - - for (const invalidNode of invalidNodes) { - reported.add(invalidNode); - - context.report({ - messageId: "noBeforeSuper", - node: invalidNode, - data: { - kind: - invalidNode.type === "Super" - ? "super" - : "this", - }, - }); - } - - if (info.superCalled) { - controller.skip(); - } - }); - }, - - /** - * Initialize information of a given code path segment. - * @param {CodePathSegment} segment A code path segment to initialize. - * @returns {void} - */ - onCodePathSegmentStart(segment) { - funcInfo.currentSegments.add(segment); - - if (!isInConstructorOfDerivedClass()) { - return; - } - - // Initialize info. - segInfoMap[segment.id] = { - superCalled: - segment.prevSegments.length > 0 && - segment.prevSegments.every(isCalled), - invalidNodes: [], - }; - }, - - onUnreachableCodePathSegmentStart(segment) { - funcInfo.currentSegments.add(segment); - }, - - onUnreachableCodePathSegmentEnd(segment) { - funcInfo.currentSegments.delete(segment); - }, - - onCodePathSegmentEnd(segment) { - funcInfo.currentSegments.delete(segment); - }, - - /** - * Update information of the code path segment when a code path was - * looped. - * @param {CodePathSegment} fromSegment The code path segment of the - * end of a loop. - * @param {CodePathSegment} toSegment A code path segment of the head - * of a loop. - * @returns {void} - */ - onCodePathSegmentLoop(fromSegment, toSegment) { - if (!isInConstructorOfDerivedClass()) { - return; - } - - // Update information inside of the loop. - funcInfo.codePath.traverseSegments( - { first: toSegment, last: fromSegment }, - (segment, controller) => { - const info = - segInfoMap[segment.id] ?? new SegmentInfo(); - - if (info.superCalled) { - controller.skip(); - } else if ( - segment.prevSegments.length > 0 && - segment.prevSegments.every(isCalled) - ) { - info.superCalled = true; - } - - segInfoMap[segment.id] = info; - }, - ); - }, - - /** - * Reports if this is before `super()`. - * @param {ASTNode} node A target node. - * @returns {void} - */ - ThisExpression(node) { - if (isBeforeCallOfSuper()) { - setInvalid(node); - } - }, - - /** - * Reports if this is before `super()`. - * @param {ASTNode} node A target node. - * @returns {void} - */ - Super(node) { - if (!astUtils.isCallee(node) && isBeforeCallOfSuper()) { - setInvalid(node); - } - }, - - /** - * Marks `super()` called. - * @param {ASTNode} node A target node. - * @returns {void} - */ - "CallExpression:exit"(node) { - if (node.callee.type === "Super" && isBeforeCallOfSuper()) { - setSuperCalled(); - } - }, - - /** - * Resets state. - * @returns {void} - */ - "Program:exit"() { - segInfoMap = Object.create(null); - }, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow `this`/`super` before calling `super()` in constructors", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-this-before-super" + }, + + schema: [], + + messages: { + noBeforeSuper: "'{{kind}}' is not allowed before 'super()'." + } + }, + + create(context) { + + /* + * Information for each constructor. + * - upper: Information of the upper constructor. + * - hasExtends: A flag which shows whether the owner class has a valid + * `extends` part. + * - scope: The scope of the owner class. + * - codePath: The code path of this constructor. + */ + let funcInfo = null; + + /* + * Information for each code path segment. + * Each key is the id of a code path segment. + * Each value is an object: + * - superCalled: The flag which shows `super()` called in all code paths. + * - invalidNodes: The array of invalid ThisExpression and Super nodes. + */ + let segInfoMap = Object.create(null); + + /** + * Gets whether or not `super()` is called in a given code path segment. + * @param {CodePathSegment} segment A code path segment to get. + * @returns {boolean} `true` if `super()` is called. + */ + function isCalled(segment) { + return !segment.reachable || segInfoMap[segment.id].superCalled; + } + + /** + * Checks whether or not this is in a constructor. + * @returns {boolean} `true` if this is in a constructor. + */ + function isInConstructorOfDerivedClass() { + return Boolean(funcInfo && funcInfo.isConstructor && funcInfo.hasExtends); + } + + /** + * Determines if every segment in a set has been called. + * @param {Set} segments The segments to search. + * @returns {boolean} True if every segment has been called; false otherwise. + */ + function isEverySegmentCalled(segments) { + for (const segment of segments) { + if (!isCalled(segment)) { + return false; + } + } + + return true; + } + + /** + * Checks whether or not this is before `super()` is called. + * @returns {boolean} `true` if this is before `super()` is called. + */ + function isBeforeCallOfSuper() { + return ( + isInConstructorOfDerivedClass() && + !isEverySegmentCalled(funcInfo.currentSegments) + ); + } + + /** + * Sets a given node as invalid. + * @param {ASTNode} node A node to set as invalid. This is one of + * a ThisExpression and a Super. + * @returns {void} + */ + function setInvalid(node) { + const segments = funcInfo.currentSegments; + + for (const segment of segments) { + if (segment.reachable) { + segInfoMap[segment.id].invalidNodes.push(node); + } + } + } + + /** + * Sets the current segment as `super` was called. + * @returns {void} + */ + function setSuperCalled() { + const segments = funcInfo.currentSegments; + + for (const segment of segments) { + if (segment.reachable) { + segInfoMap[segment.id].superCalled = true; + } + } + } + + return { + + /** + * Adds information of a constructor into the stack. + * @param {CodePath} codePath A code path which was started. + * @param {ASTNode} node The current node. + * @returns {void} + */ + onCodePathStart(codePath, node) { + if (isConstructorFunction(node)) { + + // Class > ClassBody > MethodDefinition > FunctionExpression + const classNode = node.parent.parent.parent; + + funcInfo = { + upper: funcInfo, + isConstructor: true, + hasExtends: Boolean( + classNode.superClass && + !astUtils.isNullOrUndefined(classNode.superClass) + ), + codePath, + currentSegments: new Set() + }; + } else { + funcInfo = { + upper: funcInfo, + isConstructor: false, + hasExtends: false, + codePath, + currentSegments: new Set() + }; + } + }, + + /** + * Removes the top of stack item. + * + * And this traverses all segments of this code path then reports every + * invalid node. + * @param {CodePath} codePath A code path which was ended. + * @returns {void} + */ + onCodePathEnd(codePath) { + const isDerivedClass = funcInfo.hasExtends; + + funcInfo = funcInfo.upper; + if (!isDerivedClass) { + return; + } + + codePath.traverseSegments((segment, controller) => { + const info = segInfoMap[segment.id]; + + for (let i = 0; i < info.invalidNodes.length; ++i) { + const invalidNode = info.invalidNodes[i]; + + context.report({ + messageId: "noBeforeSuper", + node: invalidNode, + data: { + kind: invalidNode.type === "Super" ? "super" : "this" + } + }); + } + + if (info.superCalled) { + controller.skip(); + } + }); + }, + + /** + * Initialize information of a given code path segment. + * @param {CodePathSegment} segment A code path segment to initialize. + * @returns {void} + */ + onCodePathSegmentStart(segment) { + funcInfo.currentSegments.add(segment); + + if (!isInConstructorOfDerivedClass()) { + return; + } + + // Initialize info. + segInfoMap[segment.id] = { + superCalled: ( + segment.prevSegments.length > 0 && + segment.prevSegments.every(isCalled) + ), + invalidNodes: [] + }; + }, + + onUnreachableCodePathSegmentStart(segment) { + funcInfo.currentSegments.add(segment); + }, + + onUnreachableCodePathSegmentEnd(segment) { + funcInfo.currentSegments.delete(segment); + }, + + onCodePathSegmentEnd(segment) { + funcInfo.currentSegments.delete(segment); + }, + + /** + * Update information of the code path segment when a code path was + * looped. + * @param {CodePathSegment} fromSegment The code path segment of the + * end of a loop. + * @param {CodePathSegment} toSegment A code path segment of the head + * of a loop. + * @returns {void} + */ + onCodePathSegmentLoop(fromSegment, toSegment) { + if (!isInConstructorOfDerivedClass()) { + return; + } + + // Update information inside of the loop. + funcInfo.codePath.traverseSegments( + { first: toSegment, last: fromSegment }, + (segment, controller) => { + const info = segInfoMap[segment.id]; + + if (info.superCalled) { + info.invalidNodes = []; + controller.skip(); + } else if ( + segment.prevSegments.length > 0 && + segment.prevSegments.every(isCalled) + ) { + info.superCalled = true; + info.invalidNodes = []; + } + } + ); + }, + + /** + * Reports if this is before `super()`. + * @param {ASTNode} node A target node. + * @returns {void} + */ + ThisExpression(node) { + if (isBeforeCallOfSuper()) { + setInvalid(node); + } + }, + + /** + * Reports if this is before `super()`. + * @param {ASTNode} node A target node. + * @returns {void} + */ + Super(node) { + if (!astUtils.isCallee(node) && isBeforeCallOfSuper()) { + setInvalid(node); + } + }, + + /** + * Marks `super()` called. + * @param {ASTNode} node A target node. + * @returns {void} + */ + "CallExpression:exit"(node) { + if (node.callee.type === "Super" && isBeforeCallOfSuper()) { + setSuperCalled(); + } + }, + + /** + * Resets state. + * @returns {void} + */ + "Program:exit"() { + segInfoMap = Object.create(null); + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-throw-literal.js b/node_modules/eslint/lib/rules/no-throw-literal.js index 877224be1..07a0df62d 100644 --- a/node_modules/eslint/lib/rules/no-throw-literal.js +++ b/node_modules/eslint/lib/rules/no-throw-literal.js @@ -11,36 +11,41 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: "Disallow throwing literals as exceptions", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-throw-literal", - }, - - schema: [], - - messages: { - object: "Expected an error object to be thrown.", - undef: "Do not throw undefined.", - }, - }, - - create(context) { - return { - ThrowStatement(node) { - if (!astUtils.couldBeError(node.argument)) { - context.report({ node, messageId: "object" }); - } else if (node.argument.type === "Identifier") { - if (node.argument.name === "undefined") { - context.report({ node, messageId: "undef" }); - } - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow throwing literals as exceptions", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-throw-literal" + }, + + schema: [], + + messages: { + object: "Expected an error object to be thrown.", + undef: "Do not throw undefined." + } + }, + + create(context) { + + return { + + ThrowStatement(node) { + if (!astUtils.couldBeError(node.argument)) { + context.report({ node, messageId: "object" }); + } else if (node.argument.type === "Identifier") { + if (node.argument.name === "undefined") { + context.report({ node, messageId: "undef" }); + } + } + + } + + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-trailing-spaces.js b/node_modules/eslint/lib/rules/no-trailing-spaces.js index 14c66db4a..eede46c86 100644 --- a/node_modules/eslint/lib/rules/no-trailing-spaces.js +++ b/node_modules/eslint/lib/rules/no-trailing-spaces.js @@ -5,14 +5,6 @@ */ "use strict"; -//------------------------------------------------------------------------------ -// Typedefs -//------------------------------------------------------------------------------ - -/** - * @import { SourceLocation, SourceRange } from "@eslint/core"; - */ - //------------------------------------------------------------------------------ // Requirements //------------------------------------------------------------------------------ @@ -23,205 +15,179 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "no-trailing-spaces", - url: "https://eslint.style/rules/no-trailing-spaces", - }, - }, - ], - }, - type: "layout", - - docs: { - description: "Disallow trailing whitespace at the end of lines", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-trailing-spaces", - }, - - fixable: "whitespace", - - schema: [ - { - type: "object", - properties: { - skipBlankLines: { - type: "boolean", - default: false, - }, - ignoreComments: { - type: "boolean", - default: false, - }, - }, - additionalProperties: false, - }, - ], - - messages: { - trailingSpace: "Trailing spaces not allowed.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - const BLANK_CLASS = - "[ \t\u00a0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u3000]", - SKIP_BLANK = `^${BLANK_CLASS}*$`, - NONBLANK = `${BLANK_CLASS}+$`; - - const options = context.options[0] || {}, - skipBlankLines = options.skipBlankLines || false, - ignoreComments = options.ignoreComments || false; - - /** - * Report the error message - * @param {ASTNode} node node to report - * @param {SourceLocation} location range information - * @param {SourceRange} fixRange Range based on the whole program - * @returns {void} - */ - function report(node, location, fixRange) { - /* - * Passing node is a bit dirty, because message data will contain big - * text in `source`. But... who cares :) ? - * One more kludge will not make worse the bloody wizardry of this - * plugin. - */ - context.report({ - node, - loc: location, - messageId: "trailingSpace", - fix(fixer) { - return fixer.removeRange(fixRange); - }, - }); - } - - /** - * Given a list of comment nodes, return the line numbers for those comments. - * @param {Array} comments An array of comment nodes. - * @returns {number[]} An array of line numbers containing comments. - */ - function getCommentLineNumbers(comments) { - const lines = new Set(); - - comments.forEach(comment => { - const endLine = - comment.type === "Block" - ? comment.loc.end.line - 1 - : comment.loc.end.line; - - for (let i = comment.loc.start.line; i <= endLine; i++) { - lines.add(i); - } - }); - - return lines; - } - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - return { - Program: function checkTrailingSpaces(node) { - /* - * Let's hack. Since Espree does not return whitespace nodes, - * fetch the source code and do matching via regexps. - */ - - const re = new RegExp(NONBLANK, "u"), - skipMatch = new RegExp(SKIP_BLANK, "u"), - lines = sourceCode.lines, - linebreaks = sourceCode - .getText() - .match(astUtils.createGlobalLinebreakMatcher()), - comments = sourceCode.getAllComments(), - commentLineNumbers = getCommentLineNumbers(comments); - - let totalLength = 0; - - for (let i = 0, ii = lines.length; i < ii; i++) { - const lineNumber = i + 1; - - /* - * Always add linebreak length to line length to accommodate for line break (\n or \r\n) - * Because during the fix time they also reserve one spot in the array. - * Usually linebreak length is 2 for \r\n (CRLF) and 1 for \n (LF) - */ - const linebreakLength = - linebreaks && linebreaks[i] ? linebreaks[i].length : 1; - const lineLength = lines[i].length + linebreakLength; - - const matches = re.exec(lines[i]); - - if (matches) { - const location = { - start: { - line: lineNumber, - column: matches.index, - }, - end: { - line: lineNumber, - column: lineLength - linebreakLength, - }, - }; - - const rangeStart = totalLength + location.start.column; - const rangeEnd = totalLength + location.end.column; - const containingNode = - sourceCode.getNodeByRangeIndex(rangeStart); - - if ( - containingNode && - containingNode.type === "TemplateElement" && - rangeStart > containingNode.parent.range[0] && - rangeEnd < containingNode.parent.range[1] - ) { - totalLength += lineLength; - continue; - } - - /* - * If the line has only whitespace, and skipBlankLines - * is true, don't report it - */ - if (skipBlankLines && skipMatch.test(lines[i])) { - totalLength += lineLength; - continue; - } - - const fixRange = [rangeStart, rangeEnd]; - - if ( - !ignoreComments || - !commentLineNumbers.has(lineNumber) - ) { - report(node, location, fixRange); - } - } - - totalLength += lineLength; - } - }, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Disallow trailing whitespace at the end of lines", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-trailing-spaces" + }, + + fixable: "whitespace", + + schema: [ + { + type: "object", + properties: { + skipBlankLines: { + type: "boolean", + default: false + }, + ignoreComments: { + type: "boolean", + default: false + } + }, + additionalProperties: false + } + ], + + messages: { + trailingSpace: "Trailing spaces not allowed." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + + const BLANK_CLASS = "[ \t\u00a0\u2000-\u200b\u3000]", + SKIP_BLANK = `^${BLANK_CLASS}*$`, + NONBLANK = `${BLANK_CLASS}+$`; + + const options = context.options[0] || {}, + skipBlankLines = options.skipBlankLines || false, + ignoreComments = options.ignoreComments || false; + + /** + * Report the error message + * @param {ASTNode} node node to report + * @param {int[]} location range information + * @param {int[]} fixRange Range based on the whole program + * @returns {void} + */ + function report(node, location, fixRange) { + + /* + * Passing node is a bit dirty, because message data will contain big + * text in `source`. But... who cares :) ? + * One more kludge will not make worse the bloody wizardry of this + * plugin. + */ + context.report({ + node, + loc: location, + messageId: "trailingSpace", + fix(fixer) { + return fixer.removeRange(fixRange); + } + }); + } + + /** + * Given a list of comment nodes, return the line numbers for those comments. + * @param {Array} comments An array of comment nodes. + * @returns {number[]} An array of line numbers containing comments. + */ + function getCommentLineNumbers(comments) { + const lines = new Set(); + + comments.forEach(comment => { + const endLine = comment.type === "Block" + ? comment.loc.end.line - 1 + : comment.loc.end.line; + + for (let i = comment.loc.start.line; i <= endLine; i++) { + lines.add(i); + } + }); + + return lines; + } + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + return { + + Program: function checkTrailingSpaces(node) { + + /* + * Let's hack. Since Espree does not return whitespace nodes, + * fetch the source code and do matching via regexps. + */ + + const re = new RegExp(NONBLANK, "u"), + skipMatch = new RegExp(SKIP_BLANK, "u"), + lines = sourceCode.lines, + linebreaks = sourceCode.getText().match(astUtils.createGlobalLinebreakMatcher()), + comments = sourceCode.getAllComments(), + commentLineNumbers = getCommentLineNumbers(comments); + + let totalLength = 0, + fixRange = []; + + for (let i = 0, ii = lines.length; i < ii; i++) { + const lineNumber = i + 1; + + /* + * Always add linebreak length to line length to accommodate for line break (\n or \r\n) + * Because during the fix time they also reserve one spot in the array. + * Usually linebreak length is 2 for \r\n (CRLF) and 1 for \n (LF) + */ + const linebreakLength = linebreaks && linebreaks[i] ? linebreaks[i].length : 1; + const lineLength = lines[i].length + linebreakLength; + + const matches = re.exec(lines[i]); + + if (matches) { + const location = { + start: { + line: lineNumber, + column: matches.index + }, + end: { + line: lineNumber, + column: lineLength - linebreakLength + } + }; + + const rangeStart = totalLength + location.start.column; + const rangeEnd = totalLength + location.end.column; + const containingNode = sourceCode.getNodeByRangeIndex(rangeStart); + + if (containingNode && containingNode.type === "TemplateElement" && + rangeStart > containingNode.parent.range[0] && + rangeEnd < containingNode.parent.range[1]) { + totalLength += lineLength; + continue; + } + + /* + * If the line has only whitespace, and skipBlankLines + * is true, don't report it + */ + if (skipBlankLines && skipMatch.test(lines[i])) { + totalLength += lineLength; + continue; + } + + fixRange = [rangeStart, rangeEnd]; + + if (!ignoreComments || !commentLineNumbers.has(lineNumber)) { + report(node, location, fixRange); + } + } + + totalLength += lineLength; + } + } + + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-unassigned-vars.js b/node_modules/eslint/lib/rules/no-unassigned-vars.js deleted file mode 100644 index 3fcd83958..000000000 --- a/node_modules/eslint/lib/rules/no-unassigned-vars.js +++ /dev/null @@ -1,80 +0,0 @@ -/** - * @fileoverview Rule to flag variables that are never assigned - * @author Jacob Bandes-Storch - */ -"use strict"; - -//------------------------------------------------------------------------------ -// Rule Definition -//------------------------------------------------------------------------------ - -/** @type {import('../types').Rule.RuleModule} */ -module.exports = { - meta: { - type: "problem", - dialects: ["typescript", "javascript"], - language: "javascript", - - docs: { - description: - "Disallow `let` or `var` variables that are read but never assigned", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-unassigned-vars", - }, - - schema: [], - messages: { - unassigned: - "'{{name}}' is always 'undefined' because it's never assigned.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - let insideDeclareModule = false; - - return { - "TSModuleDeclaration[declare=true]"() { - insideDeclareModule = true; - }, - "TSModuleDeclaration[declare=true]:exit"() { - insideDeclareModule = false; - }, - VariableDeclarator(node) { - /** @type {import('estree').VariableDeclaration} */ - const declaration = node.parent; - const shouldSkip = - node.init || - node.id.type !== "Identifier" || - declaration.kind === "const" || - declaration.declare || - insideDeclareModule; - if (shouldSkip) { - return; - } - const [variable] = sourceCode.getDeclaredVariables(node); - if (!variable) { - return; - } - let hasRead = false; - for (const reference of variable.references) { - if (reference.isWrite()) { - return; - } - if (reference.isRead()) { - hasRead = true; - } - } - if (!hasRead) { - // Variables that are never read should be flagged by no-unused-vars instead - return; - } - context.report({ - node, - messageId: "unassigned", - data: { name: node.id.name }, - }); - }, - }; - }, -}; diff --git a/node_modules/eslint/lib/rules/no-undef-init.js b/node_modules/eslint/lib/rules/no-undef-init.js index 77a120043..be19d6f95 100644 --- a/node_modules/eslint/lib/rules/no-undef-init.js +++ b/node_modules/eslint/lib/rules/no-undef-init.js @@ -5,97 +5,71 @@ "use strict"; -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ - const astUtils = require("./utils/ast-utils"); -//------------------------------------------------------------------------------ -// Helpers -//------------------------------------------------------------------------------ - -const CONSTANT_BINDINGS = new Set(["const", "using", "await using"]); - //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: "Disallow initializing variables to `undefined`", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/no-undef-init", - }, - - schema: [], - fixable: "code", - - messages: { - unnecessaryUndefinedInit: - "It's not necessary to initialize '{{name}}' to undefined.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - return { - VariableDeclarator(node) { - const name = sourceCode.getText(node.id), - init = node.init && node.init.name, - scope = sourceCode.getScope(node), - undefinedVar = astUtils.getVariableByName( - scope, - "undefined", - ), - shadowed = undefinedVar && undefinedVar.defs.length > 0, - lastToken = sourceCode.getLastToken(node); - - if ( - init === "undefined" && - !CONSTANT_BINDINGS.has(node.parent.kind) && - !shadowed - ) { - context.report({ - node, - messageId: "unnecessaryUndefinedInit", - data: { name }, - fix(fixer) { - if (node.parent.kind === "var") { - return null; - } - - if ( - node.id.type === "ArrayPattern" || - node.id.type === "ObjectPattern" - ) { - // Don't fix destructuring assignment to `undefined`. - return null; - } - - if ( - sourceCode.commentsExistBetween( - node.id, - lastToken, - ) - ) { - return null; - } - - return fixer.removeRange([ - node.id.range[1], - node.range[1], - ]); - }, - }); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow initializing variables to `undefined`", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-undef-init" + }, + + schema: [], + fixable: "code", + + messages: { + unnecessaryUndefinedInit: "It's not necessary to initialize '{{name}}' to undefined." + } + }, + + create(context) { + + const sourceCode = context.sourceCode; + + return { + + VariableDeclarator(node) { + const name = sourceCode.getText(node.id), + init = node.init && node.init.name, + scope = sourceCode.getScope(node), + undefinedVar = astUtils.getVariableByName(scope, "undefined"), + shadowed = undefinedVar && undefinedVar.defs.length > 0, + lastToken = sourceCode.getLastToken(node); + + if (init === "undefined" && node.parent.kind !== "const" && !shadowed) { + context.report({ + node, + messageId: "unnecessaryUndefinedInit", + data: { name }, + fix(fixer) { + if (node.parent.kind === "var") { + return null; + } + + if (node.id.type === "ArrayPattern" || node.id.type === "ObjectPattern") { + + // Don't fix destructuring assignment to `undefined`. + return null; + } + + if (sourceCode.commentsExistBetween(node.id, lastToken)) { + return null; + } + + return fixer.removeRange([node.id.range[1], node.range[1]]); + } + }); + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-undef.js b/node_modules/eslint/lib/rules/no-undef.js index fbda87733..fe470286c 100644 --- a/node_modules/eslint/lib/rules/no-undef.js +++ b/node_modules/eslint/lib/rules/no-undef.js @@ -14,71 +14,66 @@ * @returns {boolean} Whether or not the node is the argument of a typeof operator. */ function hasTypeOfOperator(node) { - const parent = node.parent; + const parent = node.parent; - return parent.type === "UnaryExpression" && parent.operator === "typeof"; + return parent.type === "UnaryExpression" && parent.operator === "typeof"; } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", + meta: { + type: "problem", - defaultOptions: [ - { - typeof: false, - }, - ], + docs: { + description: "Disallow the use of undeclared variables unless mentioned in `/*global */` comments", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-undef" + }, - docs: { - description: - "Disallow the use of undeclared variables unless mentioned in `/*global */` comments", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-undef", - }, + schema: [ + { + type: "object", + properties: { + typeof: { + type: "boolean", + default: false + } + }, + additionalProperties: false + } + ], + messages: { + undef: "'{{name}}' is not defined." + } + }, - schema: [ - { - type: "object", - properties: { - typeof: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - messages: { - undef: "'{{name}}' is not defined.", - }, - }, + create(context) { + const options = context.options[0]; + const considerTypeOf = options && options.typeof === true || false; + const sourceCode = context.sourceCode; - create(context) { - const [{ typeof: considerTypeOf }] = context.options; - const sourceCode = context.sourceCode; + return { + "Program:exit"(node) { + const globalScope = sourceCode.getScope(node); - return { - "Program:exit"(node) { - const globalScope = sourceCode.getScope(node); + globalScope.through.forEach(ref => { + const identifier = ref.identifier; - globalScope.through.forEach(ref => { - const identifier = ref.identifier; + if (!considerTypeOf && hasTypeOfOperator(identifier)) { + return; + } - if (!considerTypeOf && hasTypeOfOperator(identifier)) { - return; - } - - context.report({ - node: identifier, - messageId: "undef", - data: identifier, - }); - }); - }, - }; - }, + context.report({ + node: identifier, + messageId: "undef", + data: identifier + }); + }); + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-undefined.js b/node_modules/eslint/lib/rules/no-undefined.js index 044efeab8..8f47ca1b0 100644 --- a/node_modules/eslint/lib/rules/no-undefined.js +++ b/node_modules/eslint/lib/rules/no-undefined.js @@ -8,78 +8,79 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: "Disallow the use of `undefined` as an identifier", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/no-undefined", - }, - - schema: [], - - messages: { - unexpectedUndefined: "Unexpected use of undefined.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - /** - * Report an invalid "undefined" identifier node. - * @param {ASTNode} node The node to report. - * @returns {void} - */ - function report(node) { - context.report({ - node, - messageId: "unexpectedUndefined", - }); - } - - /** - * Checks the given scope for references to `undefined` and reports - * all references found. - * @param {eslint-scope.Scope} scope The scope to check. - * @returns {void} - */ - function checkScope(scope) { - const undefinedVar = scope.set.get("undefined"); - - if (!undefinedVar) { - return; - } - - const references = undefinedVar.references; - - const defs = undefinedVar.defs; - - // Report non-initializing references (those are covered in defs below) - references - .filter(ref => !ref.init) - .forEach(ref => report(ref.identifier)); - - defs.forEach(def => report(def.name)); - } - - return { - "Program:exit"(node) { - const globalScope = sourceCode.getScope(node); - - const stack = [globalScope]; - - while (stack.length) { - const scope = stack.pop(); - - stack.push(...scope.childScopes); - checkScope(scope); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow the use of `undefined` as an identifier", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-undefined" + }, + + schema: [], + + messages: { + unexpectedUndefined: "Unexpected use of undefined." + } + }, + + create(context) { + + const sourceCode = context.sourceCode; + + /** + * Report an invalid "undefined" identifier node. + * @param {ASTNode} node The node to report. + * @returns {void} + */ + function report(node) { + context.report({ + node, + messageId: "unexpectedUndefined" + }); + } + + /** + * Checks the given scope for references to `undefined` and reports + * all references found. + * @param {eslint-scope.Scope} scope The scope to check. + * @returns {void} + */ + function checkScope(scope) { + const undefinedVar = scope.set.get("undefined"); + + if (!undefinedVar) { + return; + } + + const references = undefinedVar.references; + + const defs = undefinedVar.defs; + + // Report non-initializing references (those are covered in defs below) + references + .filter(ref => !ref.init) + .forEach(ref => report(ref.identifier)); + + defs.forEach(def => report(def.name)); + } + + return { + "Program:exit"(node) { + const globalScope = sourceCode.getScope(node); + + const stack = [globalScope]; + + while (stack.length) { + const scope = stack.pop(); + + stack.push(...scope.childScopes); + checkScope(scope); + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-underscore-dangle.js b/node_modules/eslint/lib/rules/no-underscore-dangle.js index 24b723709..a0e05c6c1 100644 --- a/node_modules/eslint/lib/rules/no-underscore-dangle.js +++ b/node_modules/eslint/lib/rules/no-underscore-dangle.js @@ -9,375 +9,327 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - defaultOptions: [ - { - allow: [], - allowAfterSuper: false, - allowAfterThis: false, - allowAfterThisConstructor: false, - allowFunctionParams: true, - allowInArrayDestructuring: true, - allowInObjectDestructuring: true, - enforceInClassFields: false, - enforceInMethodNames: false, - }, - ], - - docs: { - description: "Disallow dangling underscores in identifiers", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/no-underscore-dangle", - }, - - schema: [ - { - type: "object", - properties: { - allow: { - type: "array", - items: { - type: "string", - }, - }, - allowAfterThis: { - type: "boolean", - }, - allowAfterSuper: { - type: "boolean", - }, - allowAfterThisConstructor: { - type: "boolean", - }, - enforceInMethodNames: { - type: "boolean", - }, - allowFunctionParams: { - type: "boolean", - }, - enforceInClassFields: { - type: "boolean", - }, - allowInArrayDestructuring: { - type: "boolean", - }, - allowInObjectDestructuring: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - - messages: { - unexpectedUnderscore: - "Unexpected dangling '_' in '{{identifier}}'.", - }, - }, - - create(context) { - const [ - { - allow, - allowAfterSuper, - allowAfterThis, - allowAfterThisConstructor, - allowFunctionParams, - allowInArrayDestructuring, - allowInObjectDestructuring, - enforceInClassFields, - enforceInMethodNames, - }, - ] = context.options; - const sourceCode = context.sourceCode; - - //------------------------------------------------------------------------- - // Helpers - //------------------------------------------------------------------------- - - /** - * Check if identifier is present inside the allowed option - * @param {string} identifier name of the node - * @returns {boolean} true if its is present - * @private - */ - function isAllowed(identifier) { - return allow.includes(identifier); - } - - /** - * Check if identifier has a dangling underscore - * @param {string} identifier name of the node - * @returns {boolean} true if its is present - * @private - */ - function hasDanglingUnderscore(identifier) { - const len = identifier.length; - - return ( - identifier !== "_" && - (identifier[0] === "_" || identifier[len - 1] === "_") - ); - } - - /** - * Check if identifier is a special case member expression - * @param {string} identifier name of the node - * @returns {boolean} true if its is a special case - * @private - */ - function isSpecialCaseIdentifierForMemberExpression(identifier) { - return identifier === "__proto__"; - } - - /** - * Check if identifier is a special case variable expression - * @param {string} identifier name of the node - * @returns {boolean} true if its is a special case - * @private - */ - function isSpecialCaseIdentifierInVariableExpression(identifier) { - // Checks for the underscore library usage here - return identifier === "_"; - } - - /** - * Check if a node is a member reference of this.constructor - * @param {ASTNode} node node to evaluate - * @returns {boolean} true if it is a reference on this.constructor - * @private - */ - function isThisConstructorReference(node) { - return ( - node.object.type === "MemberExpression" && - node.object.property.name === "constructor" && - node.object.object.type === "ThisExpression" - ); - } - - /** - * Check if function parameter has a dangling underscore. - * @param {ASTNode} node function node to evaluate - * @returns {void} - * @private - */ - function checkForDanglingUnderscoreInFunctionParameters(node) { - if (!allowFunctionParams) { - node.params.forEach(param => { - const { type } = param; - let nodeToCheck; - - if (type === "RestElement") { - nodeToCheck = param.argument; - } else if (type === "AssignmentPattern") { - nodeToCheck = param.left; - } else { - nodeToCheck = param; - } - - if (nodeToCheck.type === "Identifier") { - const identifier = nodeToCheck.name; - - if ( - hasDanglingUnderscore(identifier) && - !isAllowed(identifier) - ) { - context.report({ - node: param, - messageId: "unexpectedUnderscore", - data: { - identifier, - }, - }); - } - } - }); - } - } - - /** - * Check if function has a dangling underscore - * @param {ASTNode} node node to evaluate - * @returns {void} - * @private - */ - function checkForDanglingUnderscoreInFunction(node) { - if (node.type === "FunctionDeclaration" && node.id) { - const identifier = node.id.name; - - if ( - typeof identifier !== "undefined" && - hasDanglingUnderscore(identifier) && - !isAllowed(identifier) - ) { - context.report({ - node, - messageId: "unexpectedUnderscore", - data: { - identifier, - }, - }); - } - } - checkForDanglingUnderscoreInFunctionParameters(node); - } - - /** - * Check if variable expression has a dangling underscore - * @param {ASTNode} node node to evaluate - * @returns {void} - * @private - */ - function checkForDanglingUnderscoreInVariableExpression(node) { - sourceCode.getDeclaredVariables(node).forEach(variable => { - const definition = variable.defs.find(def => def.node === node); - const identifierNode = definition.name; - const identifier = identifierNode.name; - let parent = identifierNode.parent; - - while ( - ![ - "VariableDeclarator", - "ArrayPattern", - "ObjectPattern", - ].includes(parent.type) - ) { - parent = parent.parent; - } - - if ( - hasDanglingUnderscore(identifier) && - !isSpecialCaseIdentifierInVariableExpression(identifier) && - !isAllowed(identifier) && - !( - allowInArrayDestructuring && - parent.type === "ArrayPattern" - ) && - !( - allowInObjectDestructuring && - parent.type === "ObjectPattern" - ) - ) { - context.report({ - node, - messageId: "unexpectedUnderscore", - data: { - identifier, - }, - }); - } - }); - } - - /** - * Check if member expression has a dangling underscore - * @param {ASTNode} node node to evaluate - * @returns {void} - * @private - */ - function checkForDanglingUnderscoreInMemberExpression(node) { - const identifier = node.property.name, - isMemberOfThis = node.object.type === "ThisExpression", - isMemberOfSuper = node.object.type === "Super", - isMemberOfThisConstructor = isThisConstructorReference(node); - - if ( - typeof identifier !== "undefined" && - hasDanglingUnderscore(identifier) && - !(isMemberOfThis && allowAfterThis) && - !(isMemberOfSuper && allowAfterSuper) && - !(isMemberOfThisConstructor && allowAfterThisConstructor) && - !isSpecialCaseIdentifierForMemberExpression(identifier) && - !isAllowed(identifier) - ) { - context.report({ - node, - messageId: "unexpectedUnderscore", - data: { - identifier, - }, - }); - } - } - - /** - * Check if method declaration or method property has a dangling underscore - * @param {ASTNode} node node to evaluate - * @returns {void} - * @private - */ - function checkForDanglingUnderscoreInMethod(node) { - const identifier = node.key.name; - const isMethod = - node.type === "MethodDefinition" || - (node.type === "Property" && node.method); - - if ( - typeof identifier !== "undefined" && - enforceInMethodNames && - isMethod && - hasDanglingUnderscore(identifier) && - !isAllowed(identifier) - ) { - context.report({ - node, - messageId: "unexpectedUnderscore", - data: { - identifier: - node.key.type === "PrivateIdentifier" - ? `#${identifier}` - : identifier, - }, - }); - } - } - - /** - * Check if a class field has a dangling underscore - * @param {ASTNode} node node to evaluate - * @returns {void} - * @private - */ - function checkForDanglingUnderscoreInClassField(node) { - const identifier = node.key.name; - - if ( - typeof identifier !== "undefined" && - hasDanglingUnderscore(identifier) && - enforceInClassFields && - !isAllowed(identifier) - ) { - context.report({ - node, - messageId: "unexpectedUnderscore", - data: { - identifier: - node.key.type === "PrivateIdentifier" - ? `#${identifier}` - : identifier, - }, - }); - } - } - - //-------------------------------------------------------------------------- - // Public API - //-------------------------------------------------------------------------- - - return { - FunctionDeclaration: checkForDanglingUnderscoreInFunction, - VariableDeclarator: checkForDanglingUnderscoreInVariableExpression, - MemberExpression: checkForDanglingUnderscoreInMemberExpression, - MethodDefinition: checkForDanglingUnderscoreInMethod, - PropertyDefinition: checkForDanglingUnderscoreInClassField, - Property: checkForDanglingUnderscoreInMethod, - FunctionExpression: checkForDanglingUnderscoreInFunction, - ArrowFunctionExpression: checkForDanglingUnderscoreInFunction, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow dangling underscores in identifiers", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-underscore-dangle" + }, + + schema: [ + { + type: "object", + properties: { + allow: { + type: "array", + items: { + type: "string" + } + }, + allowAfterThis: { + type: "boolean", + default: false + }, + allowAfterSuper: { + type: "boolean", + default: false + }, + allowAfterThisConstructor: { + type: "boolean", + default: false + }, + enforceInMethodNames: { + type: "boolean", + default: false + }, + allowFunctionParams: { + type: "boolean", + default: true + }, + enforceInClassFields: { + type: "boolean", + default: false + }, + allowInArrayDestructuring: { + type: "boolean", + default: true + }, + allowInObjectDestructuring: { + type: "boolean", + default: true + } + }, + additionalProperties: false + } + ], + + messages: { + unexpectedUnderscore: "Unexpected dangling '_' in '{{identifier}}'." + } + }, + + create(context) { + + const options = context.options[0] || {}; + const ALLOWED_VARIABLES = options.allow ? options.allow : []; + const allowAfterThis = typeof options.allowAfterThis !== "undefined" ? options.allowAfterThis : false; + const allowAfterSuper = typeof options.allowAfterSuper !== "undefined" ? options.allowAfterSuper : false; + const allowAfterThisConstructor = typeof options.allowAfterThisConstructor !== "undefined" ? options.allowAfterThisConstructor : false; + const enforceInMethodNames = typeof options.enforceInMethodNames !== "undefined" ? options.enforceInMethodNames : false; + const enforceInClassFields = typeof options.enforceInClassFields !== "undefined" ? options.enforceInClassFields : false; + const allowFunctionParams = typeof options.allowFunctionParams !== "undefined" ? options.allowFunctionParams : true; + const allowInArrayDestructuring = typeof options.allowInArrayDestructuring !== "undefined" ? options.allowInArrayDestructuring : true; + const allowInObjectDestructuring = typeof options.allowInObjectDestructuring !== "undefined" ? options.allowInObjectDestructuring : true; + const sourceCode = context.sourceCode; + + //------------------------------------------------------------------------- + // Helpers + //------------------------------------------------------------------------- + + /** + * Check if identifier is present inside the allowed option + * @param {string} identifier name of the node + * @returns {boolean} true if its is present + * @private + */ + function isAllowed(identifier) { + return ALLOWED_VARIABLES.includes(identifier); + } + + /** + * Check if identifier has a dangling underscore + * @param {string} identifier name of the node + * @returns {boolean} true if its is present + * @private + */ + function hasDanglingUnderscore(identifier) { + const len = identifier.length; + + return identifier !== "_" && (identifier[0] === "_" || identifier[len - 1] === "_"); + } + + /** + * Check if identifier is a special case member expression + * @param {string} identifier name of the node + * @returns {boolean} true if its is a special case + * @private + */ + function isSpecialCaseIdentifierForMemberExpression(identifier) { + return identifier === "__proto__"; + } + + /** + * Check if identifier is a special case variable expression + * @param {string} identifier name of the node + * @returns {boolean} true if its is a special case + * @private + */ + function isSpecialCaseIdentifierInVariableExpression(identifier) { + + // Checks for the underscore library usage here + return identifier === "_"; + } + + /** + * Check if a node is a member reference of this.constructor + * @param {ASTNode} node node to evaluate + * @returns {boolean} true if it is a reference on this.constructor + * @private + */ + function isThisConstructorReference(node) { + return node.object.type === "MemberExpression" && + node.object.property.name === "constructor" && + node.object.object.type === "ThisExpression"; + } + + /** + * Check if function parameter has a dangling underscore. + * @param {ASTNode} node function node to evaluate + * @returns {void} + * @private + */ + function checkForDanglingUnderscoreInFunctionParameters(node) { + if (!allowFunctionParams) { + node.params.forEach(param => { + const { type } = param; + let nodeToCheck; + + if (type === "RestElement") { + nodeToCheck = param.argument; + } else if (type === "AssignmentPattern") { + nodeToCheck = param.left; + } else { + nodeToCheck = param; + } + + if (nodeToCheck.type === "Identifier") { + const identifier = nodeToCheck.name; + + if (hasDanglingUnderscore(identifier) && !isAllowed(identifier)) { + context.report({ + node: param, + messageId: "unexpectedUnderscore", + data: { + identifier + } + }); + } + } + }); + } + } + + /** + * Check if function has a dangling underscore + * @param {ASTNode} node node to evaluate + * @returns {void} + * @private + */ + function checkForDanglingUnderscoreInFunction(node) { + if (node.type === "FunctionDeclaration" && node.id) { + const identifier = node.id.name; + + if (typeof identifier !== "undefined" && hasDanglingUnderscore(identifier) && !isAllowed(identifier)) { + context.report({ + node, + messageId: "unexpectedUnderscore", + data: { + identifier + } + }); + } + } + checkForDanglingUnderscoreInFunctionParameters(node); + } + + + /** + * Check if variable expression has a dangling underscore + * @param {ASTNode} node node to evaluate + * @returns {void} + * @private + */ + function checkForDanglingUnderscoreInVariableExpression(node) { + sourceCode.getDeclaredVariables(node).forEach(variable => { + const definition = variable.defs.find(def => def.node === node); + const identifierNode = definition.name; + const identifier = identifierNode.name; + let parent = identifierNode.parent; + + while (!["VariableDeclarator", "ArrayPattern", "ObjectPattern"].includes(parent.type)) { + parent = parent.parent; + } + + if ( + hasDanglingUnderscore(identifier) && + !isSpecialCaseIdentifierInVariableExpression(identifier) && + !isAllowed(identifier) && + !(allowInArrayDestructuring && parent.type === "ArrayPattern") && + !(allowInObjectDestructuring && parent.type === "ObjectPattern") + ) { + context.report({ + node, + messageId: "unexpectedUnderscore", + data: { + identifier + } + }); + } + }); + } + + /** + * Check if member expression has a dangling underscore + * @param {ASTNode} node node to evaluate + * @returns {void} + * @private + */ + function checkForDanglingUnderscoreInMemberExpression(node) { + const identifier = node.property.name, + isMemberOfThis = node.object.type === "ThisExpression", + isMemberOfSuper = node.object.type === "Super", + isMemberOfThisConstructor = isThisConstructorReference(node); + + if (typeof identifier !== "undefined" && hasDanglingUnderscore(identifier) && + !(isMemberOfThis && allowAfterThis) && + !(isMemberOfSuper && allowAfterSuper) && + !(isMemberOfThisConstructor && allowAfterThisConstructor) && + !isSpecialCaseIdentifierForMemberExpression(identifier) && !isAllowed(identifier)) { + context.report({ + node, + messageId: "unexpectedUnderscore", + data: { + identifier + } + }); + } + } + + /** + * Check if method declaration or method property has a dangling underscore + * @param {ASTNode} node node to evaluate + * @returns {void} + * @private + */ + function checkForDanglingUnderscoreInMethod(node) { + const identifier = node.key.name; + const isMethod = node.type === "MethodDefinition" || node.type === "Property" && node.method; + + if (typeof identifier !== "undefined" && enforceInMethodNames && isMethod && hasDanglingUnderscore(identifier) && !isAllowed(identifier)) { + context.report({ + node, + messageId: "unexpectedUnderscore", + data: { + identifier: node.key.type === "PrivateIdentifier" + ? `#${identifier}` + : identifier + } + }); + } + } + + /** + * Check if a class field has a dangling underscore + * @param {ASTNode} node node to evaluate + * @returns {void} + * @private + */ + function checkForDanglingUnderscoreInClassField(node) { + const identifier = node.key.name; + + if (typeof identifier !== "undefined" && hasDanglingUnderscore(identifier) && + enforceInClassFields && + !isAllowed(identifier)) { + context.report({ + node, + messageId: "unexpectedUnderscore", + data: { + identifier: node.key.type === "PrivateIdentifier" + ? `#${identifier}` + : identifier + } + }); + } + } + + //-------------------------------------------------------------------------- + // Public API + //-------------------------------------------------------------------------- + + return { + FunctionDeclaration: checkForDanglingUnderscoreInFunction, + VariableDeclarator: checkForDanglingUnderscoreInVariableExpression, + MemberExpression: checkForDanglingUnderscoreInMemberExpression, + MethodDefinition: checkForDanglingUnderscoreInMethod, + PropertyDefinition: checkForDanglingUnderscoreInClassField, + Property: checkForDanglingUnderscoreInMethod, + FunctionExpression: checkForDanglingUnderscoreInFunction, + ArrowFunctionExpression: checkForDanglingUnderscoreInFunction + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-unexpected-multiline.js b/node_modules/eslint/lib/rules/no-unexpected-multiline.js index 0e91cdca8..810c08b01 100644 --- a/node_modules/eslint/lib/rules/no-unexpected-multiline.js +++ b/node_modules/eslint/lib/rules/no-unexpected-multiline.js @@ -14,117 +14,107 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - docs: { - description: "Disallow confusing multiline expressions", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-unexpected-multiline", - }, - - schema: [], - messages: { - function: - "Unexpected newline between function and ( of function call.", - property: - "Unexpected newline between object and [ of property access.", - taggedTemplate: - "Unexpected newline between template tag and template literal.", - division: - "Unexpected newline between numerator and division operator.", - }, - }, - - create(context) { - const REGEX_FLAG_MATCHER = /^[gimsuy]+$/u; - - const sourceCode = context.sourceCode; - - /** - * Check to see if there is a newline between the node and the following open bracket - * line's expression - * @param {ASTNode} node The node to check. - * @param {string} messageId The error messageId to use. - * @returns {void} - * @private - */ - function checkForBreakAfter(node, messageId) { - const openParen = sourceCode.getTokenAfter( - node, - astUtils.isNotClosingParenToken, - ); - const nodeExpressionEnd = sourceCode.getTokenBefore(openParen); - - if (openParen.loc.start.line !== nodeExpressionEnd.loc.end.line) { - context.report({ - node, - loc: openParen.loc, - messageId, - }); - } - } - - //-------------------------------------------------------------------------- - // Public API - //-------------------------------------------------------------------------- - - return { - MemberExpression(node) { - if (!node.computed || node.optional) { - return; - } - checkForBreakAfter(node.object, "property"); - }, - - TaggedTemplateExpression(node) { - const { quasi } = node; - - // handles common tags, parenthesized tags, and typescript's generic type arguments - const tokenBefore = sourceCode.getTokenBefore(quasi); - - if (tokenBefore.loc.end.line !== quasi.loc.start.line) { - context.report({ - node, - loc: { - start: quasi.loc.start, - end: { - line: quasi.loc.start.line, - column: quasi.loc.start.column + 1, - }, - }, - messageId: "taggedTemplate", - }); - } - }, - - CallExpression(node) { - if (node.arguments.length === 0 || node.optional) { - return; - } - checkForBreakAfter(node.callee, "function"); - }, - - "BinaryExpression[operator='/'] > BinaryExpression[operator='/'].left"( - node, - ) { - const secondSlash = sourceCode.getTokenAfter( - node, - token => token.value === "/", - ); - const tokenAfterOperator = - sourceCode.getTokenAfter(secondSlash); - - if ( - tokenAfterOperator.type === "Identifier" && - REGEX_FLAG_MATCHER.test(tokenAfterOperator.value) && - secondSlash.range[1] === tokenAfterOperator.range[0] - ) { - checkForBreakAfter(node.left, "division"); - } - }, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow confusing multiline expressions", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-unexpected-multiline" + }, + + schema: [], + messages: { + function: "Unexpected newline between function and ( of function call.", + property: "Unexpected newline between object and [ of property access.", + taggedTemplate: "Unexpected newline between template tag and template literal.", + division: "Unexpected newline between numerator and division operator." + } + }, + + create(context) { + + const REGEX_FLAG_MATCHER = /^[gimsuy]+$/u; + + const sourceCode = context.sourceCode; + + /** + * Check to see if there is a newline between the node and the following open bracket + * line's expression + * @param {ASTNode} node The node to check. + * @param {string} messageId The error messageId to use. + * @returns {void} + * @private + */ + function checkForBreakAfter(node, messageId) { + const openParen = sourceCode.getTokenAfter(node, astUtils.isNotClosingParenToken); + const nodeExpressionEnd = sourceCode.getTokenBefore(openParen); + + if (openParen.loc.start.line !== nodeExpressionEnd.loc.end.line) { + context.report({ + node, + loc: openParen.loc, + messageId + }); + } + } + + //-------------------------------------------------------------------------- + // Public API + //-------------------------------------------------------------------------- + + return { + + MemberExpression(node) { + if (!node.computed || node.optional) { + return; + } + checkForBreakAfter(node.object, "property"); + }, + + TaggedTemplateExpression(node) { + const { quasi } = node; + + // handles common tags, parenthesized tags, and typescript's generic type arguments + const tokenBefore = sourceCode.getTokenBefore(quasi); + + if (tokenBefore.loc.end.line !== quasi.loc.start.line) { + context.report({ + node, + loc: { + start: quasi.loc.start, + end: { + line: quasi.loc.start.line, + column: quasi.loc.start.column + 1 + } + }, + messageId: "taggedTemplate" + }); + } + }, + + CallExpression(node) { + if (node.arguments.length === 0 || node.optional) { + return; + } + checkForBreakAfter(node.callee, "function"); + }, + + "BinaryExpression[operator='/'] > BinaryExpression[operator='/'].left"(node) { + const secondSlash = sourceCode.getTokenAfter(node, token => token.value === "/"); + const tokenAfterOperator = sourceCode.getTokenAfter(secondSlash); + + if ( + tokenAfterOperator.type === "Identifier" && + REGEX_FLAG_MATCHER.test(tokenAfterOperator.value) && + secondSlash.range[1] === tokenAfterOperator.range[0] + ) { + checkForBreakAfter(node.left, "division"); + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-unmodified-loop-condition.js b/node_modules/eslint/lib/rules/no-unmodified-loop-condition.js index 179648922..768a15573 100644 --- a/node_modules/eslint/lib/rules/no-unmodified-loop-condition.js +++ b/node_modules/eslint/lib/rules/no-unmodified-loop-condition.js @@ -10,14 +10,13 @@ //------------------------------------------------------------------------------ const Traverser = require("../shared/traverser"), - astUtils = require("./utils/ast-utils"); + astUtils = require("./utils/ast-utils"); //------------------------------------------------------------------------------ // Helpers //------------------------------------------------------------------------------ -const SENTINEL_PATTERN = - /(?:(?:Call|Class|Function|Member|New|Yield)Expression|Statement|Declaration)$/u; +const SENTINEL_PATTERN = /(?:(?:Call|Class|Function|Member|New|Yield)Expression|Statement|Declaration)$/u; const LOOP_PATTERN = /^(?:DoWhile|For|While)Statement$/u; // for-in/of statements don't have `test` property. const GROUP_PATTERN = /^(?:BinaryExpression|ConditionalExpression)$/u; const SKIP_PATTERN = /^(?:ArrowFunction|Class|Function)Expression$/u; @@ -40,14 +39,14 @@ const DYNAMIC_PATTERN = /^(?:Call|Member|New|TaggedTemplate|Yield)Expression$/u; * @returns {boolean} `true` if the reference is a write reference. */ function isWriteReference(reference) { - if (reference.init) { - const def = reference.resolved && reference.resolved.defs[0]; - - if (!def || def.type !== "Variable" || def.parent.kind !== "var") { - return false; - } - } - return reference.isWrite(); + if (reference.init) { + const def = reference.resolved && reference.resolved.defs[0]; + + if (!def || def.type !== "Variable" || def.parent.kind !== "var") { + return false; + } + } + return reference.isWrite(); } /** @@ -57,7 +56,7 @@ function isWriteReference(reference) { * @returns {boolean} `true` if the loop condition info is "unmodified". */ function isUnmodified(condition) { - return !condition.modified; + return !condition.modified; } /** @@ -67,7 +66,7 @@ function isUnmodified(condition) { * @returns {boolean} `true` if the loop condition info is "unmodified". */ function isUnmodifiedAndNotBelongToGroup(condition) { - return !(condition.modified || condition.group); + return !(condition.modified || condition.group); } /** @@ -77,10 +76,10 @@ function isUnmodifiedAndNotBelongToGroup(condition) { * @returns {boolean} `true` if the reference is inside of the node. */ function isInRange(node, reference) { - const or = node.range; - const ir = reference.identifier.range; + const or = node.range; + const ir = reference.identifier.range; - return or[0] <= ir[0] && ir[1] <= or[1]; + return or[0] <= ir[0] && ir[1] <= or[1]; } /** @@ -91,14 +90,14 @@ function isInRange(node, reference) { * condition. */ const isInLoop = { - WhileStatement: isInRange, - DoWhileStatement: isInRange, - ForStatement(node, reference) { - return ( - isInRange(node, reference) && - !(node.init && isInRange(node.init, reference)) - ); - }, + WhileStatement: isInRange, + DoWhileStatement: isInRange, + ForStatement(node, reference) { + return ( + isInRange(node, reference) && + !(node.init && isInRange(node.init, reference)) + ); + } }; /** @@ -108,17 +107,17 @@ const isInLoop = { * @returns {ASTNode|null} The function node or null. */ function getEncloseFunctionDeclaration(reference) { - let node = reference.identifier; + let node = reference.identifier; - while (node) { - if (node.type === "FunctionDeclaration") { - return node.id ? node : null; - } + while (node) { + if (node.type === "FunctionDeclaration") { + return node.id ? node : null; + } - node = node.parent; - } + node = node.parent; + } - return null; + return null; } /** @@ -128,233 +127,234 @@ function getEncloseFunctionDeclaration(reference) { * @returns {void} */ function updateModifiedFlag(conditions, modifiers) { - for (let i = 0; i < conditions.length; ++i) { - const condition = conditions[i]; - - for (let j = 0; !condition.modified && j < modifiers.length; ++j) { - const modifier = modifiers[j]; - let funcNode, funcVar; - - /* - * Besides checking for the condition being in the loop, we want to - * check the function that this modifier is belonging to is called - * in the loop. - * FIXME: This should probably be extracted to a function. - */ - const inLoop = - condition.isInLoop(modifier) || - Boolean( - (funcNode = getEncloseFunctionDeclaration(modifier)) && - (funcVar = astUtils.getVariableByName( - modifier.from.upper, - funcNode.id.name, - )) && - funcVar.references.some(condition.isInLoop), - ); - - condition.modified = inLoop; - } - } + + for (let i = 0; i < conditions.length; ++i) { + const condition = conditions[i]; + + for (let j = 0; !condition.modified && j < modifiers.length; ++j) { + const modifier = modifiers[j]; + let funcNode, funcVar; + + /* + * Besides checking for the condition being in the loop, we want to + * check the function that this modifier is belonging to is called + * in the loop. + * FIXME: This should probably be extracted to a function. + */ + const inLoop = condition.isInLoop(modifier) || Boolean( + (funcNode = getEncloseFunctionDeclaration(modifier)) && + (funcVar = astUtils.getVariableByName(modifier.from.upper, funcNode.id.name)) && + funcVar.references.some(condition.isInLoop) + ); + + condition.modified = inLoop; + } + } } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - docs: { - description: "Disallow unmodified loop conditions", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-unmodified-loop-condition", - }, - - schema: [], - - messages: { - loopConditionNotModified: - "'{{name}}' is not modified in this loop.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - let groupMap = null; - - /** - * Reports a given condition info. - * @param {LoopConditionInfo} condition A loop condition info to report. - * @returns {void} - */ - function report(condition) { - const node = condition.reference.identifier; - - context.report({ - node, - messageId: "loopConditionNotModified", - data: node, - }); - } - - /** - * Registers given conditions to the group the condition belongs to. - * @param {LoopConditionInfo[]} conditions A loop condition info to - * register. - * @returns {void} - */ - function registerConditionsToGroup(conditions) { - for (let i = 0; i < conditions.length; ++i) { - const condition = conditions[i]; - - if (condition.group) { - let group = groupMap.get(condition.group); - - if (!group) { - group = []; - groupMap.set(condition.group, group); - } - group.push(condition); - } - } - } - - /** - * Reports references which are inside of unmodified groups. - * @param {LoopConditionInfo[]} conditions A loop condition info to report. - * @returns {void} - */ - function checkConditionsInGroup(conditions) { - if (conditions.every(isUnmodified)) { - conditions.forEach(report); - } - } - - /** - * Checks whether or not a given group node has any dynamic elements. - * @param {ASTNode} root A node to check. - * This node is one of BinaryExpression or ConditionalExpression. - * @returns {boolean} `true` if the node is dynamic. - */ - function hasDynamicExpressions(root) { - let retv = false; - - Traverser.traverse(root, { - visitorKeys: sourceCode.visitorKeys, - enter(node) { - if (DYNAMIC_PATTERN.test(node.type)) { - retv = true; - this.break(); - } else if (SKIP_PATTERN.test(node.type)) { - this.skip(); - } - }, - }); - - return retv; - } - - /** - * Creates the loop condition information from a given reference. - * @param {eslint-scope.Reference} reference A reference to create. - * @returns {LoopConditionInfo|null} Created loop condition info, or null. - */ - function toLoopCondition(reference) { - if (reference.init) { - return null; - } - - let group = null; - let child = reference.identifier; - let node = child.parent; - - while (node) { - if (SENTINEL_PATTERN.test(node.type)) { - if (LOOP_PATTERN.test(node.type) && node.test === child) { - // This reference is inside of a loop condition. - return { - reference, - group, - isInLoop: isInLoop[node.type].bind(null, node), - modified: false, - }; - } - - // This reference is outside of a loop condition. - break; - } - - /* - * If it's inside of a group, OK if either operand is modified. - * So stores the group this reference belongs to. - */ - if (GROUP_PATTERN.test(node.type)) { - // If this expression is dynamic, no need to check. - if (hasDynamicExpressions(node)) { - break; - } else { - group = node; - } - } - - child = node; - node = node.parent; - } - - return null; - } - - /** - * Finds unmodified references which are inside of a loop condition. - * Then reports the references which are outside of groups. - * @param {eslint-scope.Variable} variable A variable to report. - * @returns {void} - */ - function checkReferences(variable) { - // Gets references that exist in loop conditions. - const conditions = variable.references - .map(toLoopCondition) - .filter(Boolean); - - if (conditions.length === 0) { - return; - } - - // Registers the conditions to belonging groups. - registerConditionsToGroup(conditions); - - // Check the conditions are modified. - const modifiers = variable.references.filter(isWriteReference); - - if (modifiers.length > 0) { - updateModifiedFlag(conditions, modifiers); - } - - /* - * Reports the conditions which are not belonging to groups. - * Others will be reported after all variables are done. - */ - conditions.filter(isUnmodifiedAndNotBelongToGroup).forEach(report); - } - - return { - "Program:exit"(node) { - const queue = [sourceCode.getScope(node)]; - - groupMap = new Map(); - - let scope; - - while ((scope = queue.pop())) { - queue.push(...scope.childScopes); - scope.variables.forEach(checkReferences); - } - - groupMap.forEach(checkConditionsInGroup); - groupMap = null; - }, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow unmodified loop conditions", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-unmodified-loop-condition" + }, + + schema: [], + + messages: { + loopConditionNotModified: "'{{name}}' is not modified in this loop." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + let groupMap = null; + + /** + * Reports a given condition info. + * @param {LoopConditionInfo} condition A loop condition info to report. + * @returns {void} + */ + function report(condition) { + const node = condition.reference.identifier; + + context.report({ + node, + messageId: "loopConditionNotModified", + data: node + }); + } + + /** + * Registers given conditions to the group the condition belongs to. + * @param {LoopConditionInfo[]} conditions A loop condition info to + * register. + * @returns {void} + */ + function registerConditionsToGroup(conditions) { + for (let i = 0; i < conditions.length; ++i) { + const condition = conditions[i]; + + if (condition.group) { + let group = groupMap.get(condition.group); + + if (!group) { + group = []; + groupMap.set(condition.group, group); + } + group.push(condition); + } + } + } + + /** + * Reports references which are inside of unmodified groups. + * @param {LoopConditionInfo[]} conditions A loop condition info to report. + * @returns {void} + */ + function checkConditionsInGroup(conditions) { + if (conditions.every(isUnmodified)) { + conditions.forEach(report); + } + } + + /** + * Checks whether or not a given group node has any dynamic elements. + * @param {ASTNode} root A node to check. + * This node is one of BinaryExpression or ConditionalExpression. + * @returns {boolean} `true` if the node is dynamic. + */ + function hasDynamicExpressions(root) { + let retv = false; + + Traverser.traverse(root, { + visitorKeys: sourceCode.visitorKeys, + enter(node) { + if (DYNAMIC_PATTERN.test(node.type)) { + retv = true; + this.break(); + } else if (SKIP_PATTERN.test(node.type)) { + this.skip(); + } + } + }); + + return retv; + } + + /** + * Creates the loop condition information from a given reference. + * @param {eslint-scope.Reference} reference A reference to create. + * @returns {LoopConditionInfo|null} Created loop condition info, or null. + */ + function toLoopCondition(reference) { + if (reference.init) { + return null; + } + + let group = null; + let child = reference.identifier; + let node = child.parent; + + while (node) { + if (SENTINEL_PATTERN.test(node.type)) { + if (LOOP_PATTERN.test(node.type) && node.test === child) { + + // This reference is inside of a loop condition. + return { + reference, + group, + isInLoop: isInLoop[node.type].bind(null, node), + modified: false + }; + } + + // This reference is outside of a loop condition. + break; + } + + /* + * If it's inside of a group, OK if either operand is modified. + * So stores the group this reference belongs to. + */ + if (GROUP_PATTERN.test(node.type)) { + + // If this expression is dynamic, no need to check. + if (hasDynamicExpressions(node)) { + break; + } else { + group = node; + } + } + + child = node; + node = node.parent; + } + + return null; + } + + /** + * Finds unmodified references which are inside of a loop condition. + * Then reports the references which are outside of groups. + * @param {eslint-scope.Variable} variable A variable to report. + * @returns {void} + */ + function checkReferences(variable) { + + // Gets references that exist in loop conditions. + const conditions = variable + .references + .map(toLoopCondition) + .filter(Boolean); + + if (conditions.length === 0) { + return; + } + + // Registers the conditions to belonging groups. + registerConditionsToGroup(conditions); + + // Check the conditions are modified. + const modifiers = variable.references.filter(isWriteReference); + + if (modifiers.length > 0) { + updateModifiedFlag(conditions, modifiers); + } + + /* + * Reports the conditions which are not belonging to groups. + * Others will be reported after all variables are done. + */ + conditions + .filter(isUnmodifiedAndNotBelongToGroup) + .forEach(report); + } + + return { + "Program:exit"(node) { + const queue = [sourceCode.getScope(node)]; + + groupMap = new Map(); + + let scope; + + while ((scope = queue.pop())) { + queue.push(...scope.childScopes); + scope.variables.forEach(checkReferences); + } + + groupMap.forEach(checkConditionsInGroup); + groupMap = null; + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-unneeded-ternary.js b/node_modules/eslint/lib/rules/no-unneeded-ternary.js index 24e65b67e..ab1bdc59c 100644 --- a/node_modules/eslint/lib/rules/no-unneeded-ternary.js +++ b/node_modules/eslint/lib/rules/no-unneeded-ternary.js @@ -8,225 +8,159 @@ const astUtils = require("./utils/ast-utils"); // Operators that always result in a boolean value -const BOOLEAN_OPERATORS = new Set([ - "==", - "===", - "!=", - "!==", - ">", - ">=", - "<", - "<=", - "in", - "instanceof", -]); +const BOOLEAN_OPERATORS = new Set(["==", "===", "!=", "!==", ">", ">=", "<", "<=", "in", "instanceof"]); const OPERATOR_INVERSES = { - "==": "!=", - "!=": "==", - "===": "!==", - "!==": "===", + "==": "!=", + "!=": "==", + "===": "!==", + "!==": "===" - // Operators like < and >= are not true inverses, since both will return false with NaN. + // Operators like < and >= are not true inverses, since both will return false with NaN. }; -const OR_PRECEDENCE = astUtils.getPrecedence({ - type: "LogicalExpression", - operator: "||", -}); +const OR_PRECEDENCE = astUtils.getPrecedence({ type: "LogicalExpression", operator: "||" }); //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - defaultOptions: [{ defaultAssignment: true }], - - docs: { - description: - "Disallow ternary operators when simpler alternatives exist", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/no-unneeded-ternary", - }, - - schema: [ - { - type: "object", - properties: { - defaultAssignment: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - - fixable: "code", - - messages: { - unnecessaryConditionalExpression: - "Unnecessary use of boolean literals in conditional expression.", - unnecessaryConditionalAssignment: - "Unnecessary use of conditional expression for default assignment.", - }, - }, - - create(context) { - const [{ defaultAssignment }] = context.options; - const sourceCode = context.sourceCode; - - /** - * Test if the node is a boolean literal - * @param {ASTNode} node The node to report. - * @returns {boolean} True if the its a boolean literal - * @private - */ - function isBooleanLiteral(node) { - return node.type === "Literal" && typeof node.value === "boolean"; - } - - /** - * Creates an expression that represents the boolean inverse of the expression represented by the original node - * @param {ASTNode} node A node representing an expression - * @returns {string} A string representing an inverted expression - */ - function invertExpression(node) { - if ( - node.type === "BinaryExpression" && - Object.hasOwn(OPERATOR_INVERSES, node.operator) - ) { - const operatorToken = sourceCode.getFirstTokenBetween( - node.left, - node.right, - token => token.value === node.operator, - ); - const text = sourceCode.getText(); - - return ( - text.slice(node.range[0], operatorToken.range[0]) + - OPERATOR_INVERSES[node.operator] + - text.slice(operatorToken.range[1], node.range[1]) - ); - } - - if ( - astUtils.getPrecedence(node) < - astUtils.getPrecedence({ type: "UnaryExpression" }) - ) { - return `!(${astUtils.getParenthesisedText(sourceCode, node)})`; - } - return `!${astUtils.getParenthesisedText(sourceCode, node)}`; - } - - /** - * Tests if a given node always evaluates to a boolean value - * @param {ASTNode} node An expression node - * @returns {boolean} True if it is determined that the node will always evaluate to a boolean value - */ - function isBooleanExpression(node) { - return ( - (node.type === "BinaryExpression" && - BOOLEAN_OPERATORS.has(node.operator)) || - (node.type === "UnaryExpression" && node.operator === "!") - ); - } - - /** - * Test if the node matches the pattern id ? id : expression - * @param {ASTNode} node The ConditionalExpression to check. - * @returns {boolean} True if the pattern is matched, and false otherwise - * @private - */ - function matchesDefaultAssignment(node) { - return ( - node.test.type === "Identifier" && - node.consequent.type === "Identifier" && - node.test.name === node.consequent.name - ); - } - - return { - ConditionalExpression(node) { - if ( - isBooleanLiteral(node.alternate) && - isBooleanLiteral(node.consequent) - ) { - context.report({ - node, - messageId: "unnecessaryConditionalExpression", - fix(fixer) { - if ( - node.consequent.value === node.alternate.value - ) { - // Replace `foo ? true : true` with just `true`, but don't replace `foo() ? true : true` - return node.test.type === "Identifier" - ? fixer.replaceText( - node, - node.consequent.value.toString(), - ) - : null; - } - if (node.alternate.value) { - // Replace `foo() ? false : true` with `!(foo())` - return fixer.replaceText( - node, - invertExpression(node.test), - ); - } - - // Replace `foo ? true : false` with `foo` if `foo` is guaranteed to be a boolean, or `!!foo` otherwise. - - return fixer.replaceText( - node, - isBooleanExpression(node.test) - ? astUtils.getParenthesisedText( - sourceCode, - node.test, - ) - : `!${invertExpression(node.test)}`, - ); - }, - }); - } else if ( - !defaultAssignment && - matchesDefaultAssignment(node) - ) { - context.report({ - node, - messageId: "unnecessaryConditionalAssignment", - fix(fixer) { - const shouldParenthesizeAlternate = - (astUtils.getPrecedence(node.alternate) < - OR_PRECEDENCE || - astUtils.isCoalesceExpression( - node.alternate, - )) && - !astUtils.isParenthesised( - sourceCode, - node.alternate, - ); - const alternateText = shouldParenthesizeAlternate - ? `(${sourceCode.getText(node.alternate)})` - : astUtils.getParenthesisedText( - sourceCode, - node.alternate, - ); - const testText = astUtils.getParenthesisedText( - sourceCode, - node.test, - ); - - return fixer.replaceText( - node, - `${testText} || ${alternateText}`, - ); - }, - }); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow ternary operators when simpler alternatives exist", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-unneeded-ternary" + }, + + schema: [ + { + type: "object", + properties: { + defaultAssignment: { + type: "boolean", + default: true + } + }, + additionalProperties: false + } + ], + + fixable: "code", + + messages: { + unnecessaryConditionalExpression: "Unnecessary use of boolean literals in conditional expression.", + unnecessaryConditionalAssignment: "Unnecessary use of conditional expression for default assignment." + } + }, + + create(context) { + const options = context.options[0] || {}; + const defaultAssignment = options.defaultAssignment !== false; + const sourceCode = context.sourceCode; + + /** + * Test if the node is a boolean literal + * @param {ASTNode} node The node to report. + * @returns {boolean} True if the its a boolean literal + * @private + */ + function isBooleanLiteral(node) { + return node.type === "Literal" && typeof node.value === "boolean"; + } + + /** + * Creates an expression that represents the boolean inverse of the expression represented by the original node + * @param {ASTNode} node A node representing an expression + * @returns {string} A string representing an inverted expression + */ + function invertExpression(node) { + if (node.type === "BinaryExpression" && Object.prototype.hasOwnProperty.call(OPERATOR_INVERSES, node.operator)) { + const operatorToken = sourceCode.getFirstTokenBetween( + node.left, + node.right, + token => token.value === node.operator + ); + const text = sourceCode.getText(); + + return text.slice(node.range[0], + operatorToken.range[0]) + OPERATOR_INVERSES[node.operator] + text.slice(operatorToken.range[1], node.range[1]); + } + + if (astUtils.getPrecedence(node) < astUtils.getPrecedence({ type: "UnaryExpression" })) { + return `!(${astUtils.getParenthesisedText(sourceCode, node)})`; + } + return `!${astUtils.getParenthesisedText(sourceCode, node)}`; + } + + /** + * Tests if a given node always evaluates to a boolean value + * @param {ASTNode} node An expression node + * @returns {boolean} True if it is determined that the node will always evaluate to a boolean value + */ + function isBooleanExpression(node) { + return node.type === "BinaryExpression" && BOOLEAN_OPERATORS.has(node.operator) || + node.type === "UnaryExpression" && node.operator === "!"; + } + + /** + * Test if the node matches the pattern id ? id : expression + * @param {ASTNode} node The ConditionalExpression to check. + * @returns {boolean} True if the pattern is matched, and false otherwise + * @private + */ + function matchesDefaultAssignment(node) { + return node.test.type === "Identifier" && + node.consequent.type === "Identifier" && + node.test.name === node.consequent.name; + } + + return { + + ConditionalExpression(node) { + if (isBooleanLiteral(node.alternate) && isBooleanLiteral(node.consequent)) { + context.report({ + node, + messageId: "unnecessaryConditionalExpression", + fix(fixer) { + if (node.consequent.value === node.alternate.value) { + + // Replace `foo ? true : true` with just `true`, but don't replace `foo() ? true : true` + return node.test.type === "Identifier" ? fixer.replaceText(node, node.consequent.value.toString()) : null; + } + if (node.alternate.value) { + + // Replace `foo() ? false : true` with `!(foo())` + return fixer.replaceText(node, invertExpression(node.test)); + } + + // Replace `foo ? true : false` with `foo` if `foo` is guaranteed to be a boolean, or `!!foo` otherwise. + + return fixer.replaceText(node, isBooleanExpression(node.test) ? astUtils.getParenthesisedText(sourceCode, node.test) : `!${invertExpression(node.test)}`); + } + }); + } else if (!defaultAssignment && matchesDefaultAssignment(node)) { + context.report({ + node, + messageId: "unnecessaryConditionalAssignment", + fix(fixer) { + const shouldParenthesizeAlternate = + ( + astUtils.getPrecedence(node.alternate) < OR_PRECEDENCE || + astUtils.isCoalesceExpression(node.alternate) + ) && + !astUtils.isParenthesised(sourceCode, node.alternate); + const alternateText = shouldParenthesizeAlternate + ? `(${sourceCode.getText(node.alternate)})` + : astUtils.getParenthesisedText(sourceCode, node.alternate); + const testText = astUtils.getParenthesisedText(sourceCode, node.test); + + return fixer.replaceText(node, `${testText} || ${alternateText}`); + } + }); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-unreachable-loop.js b/node_modules/eslint/lib/rules/no-unreachable-loop.js index 63e3c977d..577d39ac7 100644 --- a/node_modules/eslint/lib/rules/no-unreachable-loop.js +++ b/node_modules/eslint/lib/rules/no-unreachable-loop.js @@ -9,13 +9,7 @@ // Helpers //------------------------------------------------------------------------------ -const allLoopTypes = [ - "WhileStatement", - "DoWhileStatement", - "ForStatement", - "ForInStatement", - "ForOfStatement", -]; +const allLoopTypes = ["WhileStatement", "DoWhileStatement", "ForStatement", "ForInStatement", "ForOfStatement"]; /** * Checks all segments in a set and returns true if any are reachable. @@ -23,13 +17,14 @@ const allLoopTypes = [ * @returns {boolean} True if any segment is reachable; false otherwise. */ function isAnySegmentReachable(segments) { - for (const segment of segments) { - if (segment.reachable) { - return true; - } - } - return false; + for (const segment of segments) { + if (segment.reachable) { + return true; + } + } + + return false; } /** @@ -39,25 +34,25 @@ function isAnySegmentReachable(segments) { * @returns {boolean} `true` if the node is a looping target. */ function isLoopingTarget(node) { - const parent = node.parent; - - if (parent) { - switch (parent.type) { - case "WhileStatement": - return node === parent.test; - case "DoWhileStatement": - return node === parent.body; - case "ForStatement": - return node === (parent.update || parent.test || parent.body); - case "ForInStatement": - case "ForOfStatement": - return node === parent.left; - - // no default - } - } - - return false; + const parent = node.parent; + + if (parent) { + switch (parent.type) { + case "WhileStatement": + return node === parent.test; + case "DoWhileStatement": + return node === parent.body; + case "ForStatement": + return node === (parent.update || parent.test || parent.body); + case "ForInStatement": + case "ForOfStatement": + return node === parent.left; + + // no default + } + } + + return false; } /** @@ -67,124 +62,124 @@ function isLoopingTarget(node) { * @returns {Array} a new array that represents `arrA \ arrB`. */ function getDifference(arrA, arrB) { - return arrA.filter(a => !arrB.includes(a)); + return arrA.filter(a => !arrB.includes(a)); } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - defaultOptions: [{ ignore: [] }], - - docs: { - description: - "Disallow loops with a body that allows only one iteration", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-unreachable-loop", - }, - - schema: [ - { - type: "object", - properties: { - ignore: { - type: "array", - items: { - enum: allLoopTypes, - }, - uniqueItems: true, - }, - }, - additionalProperties: false, - }, - ], - - messages: { - invalid: "Invalid loop. Its body allows only one iteration.", - }, - }, - - create(context) { - const [{ ignore: ignoredLoopTypes }] = context.options; - const loopTypesToCheck = getDifference(allLoopTypes, ignoredLoopTypes), - loopSelector = loopTypesToCheck.join(","), - loopsByTargetSegments = new Map(), - loopsToReport = new Set(); - - const codePathSegments = []; - let currentCodePathSegments = new Set(); - - return { - onCodePathStart() { - codePathSegments.push(currentCodePathSegments); - currentCodePathSegments = new Set(); - }, - - onCodePathEnd() { - currentCodePathSegments = codePathSegments.pop(); - }, - - onUnreachableCodePathSegmentStart(segment) { - currentCodePathSegments.add(segment); - }, - - onUnreachableCodePathSegmentEnd(segment) { - currentCodePathSegments.delete(segment); - }, - - onCodePathSegmentEnd(segment) { - currentCodePathSegments.delete(segment); - }, - - onCodePathSegmentStart(segment, node) { - currentCodePathSegments.add(segment); - - if (isLoopingTarget(node)) { - const loop = node.parent; - - loopsByTargetSegments.set(segment, loop); - } - }, - - onCodePathSegmentLoop(_, toSegment, node) { - const loop = loopsByTargetSegments.get(toSegment); - - /** - * The second iteration is reachable, meaning that the loop is valid by the logic of this rule, - * only if there is at least one loop event with the appropriate target (which has been already - * determined in the `loopsByTargetSegments` map), raised from either: - * - * - the end of the loop's body (in which case `node === loop`) - * - a `continue` statement - * - * This condition skips loop events raised from `ForInStatement > .right` and `ForOfStatement > .right` nodes. - */ - if (node === loop || node.type === "ContinueStatement") { - // Removes loop if it exists in the set. Otherwise, `Set#delete` has no effect and doesn't throw. - loopsToReport.delete(loop); - } - }, - - [loopSelector](node) { - /** - * Ignore unreachable loop statements to avoid unnecessary complexity in the implementation, or false positives otherwise. - * For unreachable segments, the code path analysis does not raise events required for this implementation. - */ - if (isAnySegmentReachable(currentCodePathSegments)) { - loopsToReport.add(node); - } - }, - - "Program:exit"() { - loopsToReport.forEach(node => - context.report({ node, messageId: "invalid" }), - ); - }, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow loops with a body that allows only one iteration", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-unreachable-loop" + }, + + schema: [{ + type: "object", + properties: { + ignore: { + type: "array", + items: { + enum: allLoopTypes + }, + uniqueItems: true + } + }, + additionalProperties: false + }], + + messages: { + invalid: "Invalid loop. Its body allows only one iteration." + } + }, + + create(context) { + const ignoredLoopTypes = context.options[0] && context.options[0].ignore || [], + loopTypesToCheck = getDifference(allLoopTypes, ignoredLoopTypes), + loopSelector = loopTypesToCheck.join(","), + loopsByTargetSegments = new Map(), + loopsToReport = new Set(); + + const codePathSegments = []; + let currentCodePathSegments = new Set(); + + return { + + onCodePathStart() { + codePathSegments.push(currentCodePathSegments); + currentCodePathSegments = new Set(); + }, + + onCodePathEnd() { + currentCodePathSegments = codePathSegments.pop(); + }, + + onUnreachableCodePathSegmentStart(segment) { + currentCodePathSegments.add(segment); + }, + + onUnreachableCodePathSegmentEnd(segment) { + currentCodePathSegments.delete(segment); + }, + + onCodePathSegmentEnd(segment) { + currentCodePathSegments.delete(segment); + }, + + onCodePathSegmentStart(segment, node) { + + currentCodePathSegments.add(segment); + + if (isLoopingTarget(node)) { + const loop = node.parent; + + loopsByTargetSegments.set(segment, loop); + } + }, + + onCodePathSegmentLoop(_, toSegment, node) { + const loop = loopsByTargetSegments.get(toSegment); + + /** + * The second iteration is reachable, meaning that the loop is valid by the logic of this rule, + * only if there is at least one loop event with the appropriate target (which has been already + * determined in the `loopsByTargetSegments` map), raised from either: + * + * - the end of the loop's body (in which case `node === loop`) + * - a `continue` statement + * + * This condition skips loop events raised from `ForInStatement > .right` and `ForOfStatement > .right` nodes. + */ + if (node === loop || node.type === "ContinueStatement") { + + // Removes loop if it exists in the set. Otherwise, `Set#delete` has no effect and doesn't throw. + loopsToReport.delete(loop); + } + }, + + [loopSelector](node) { + + /** + * Ignore unreachable loop statements to avoid unnecessary complexity in the implementation, or false positives otherwise. + * For unreachable segments, the code path analysis does not raise events required for this implementation. + */ + if (isAnySegmentReachable(currentCodePathSegments)) { + loopsToReport.add(node); + } + }, + + + "Program:exit"() { + loopsToReport.forEach( + node => context.report({ node, messageId: "invalid" }) + ); + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-unreachable.js b/node_modules/eslint/lib/rules/no-unreachable.js index 7be94bb3f..0cf750e42 100644 --- a/node_modules/eslint/lib/rules/no-unreachable.js +++ b/node_modules/eslint/lib/rules/no-unreachable.js @@ -20,7 +20,7 @@ * @returns {boolean} `true` if the node has the initializer. */ function isInitialized(node) { - return Boolean(node.init); + return Boolean(node.init); } /** @@ -29,272 +29,265 @@ function isInitialized(node) { * @returns {boolean} True if all segments are unreachable; false otherwise. */ function areAllSegmentsUnreachable(segments) { - for (const segment of segments) { - if (segment.reachable) { - return false; - } - } - return true; + for (const segment of segments) { + if (segment.reachable) { + return false; + } + } + + return true; } /** * The class to distinguish consecutive unreachable statements. */ class ConsecutiveRange { - constructor(sourceCode) { - this.sourceCode = sourceCode; - this.startNode = null; - this.endNode = null; - } - - /** - * The location object of this range. - * @type {Object} - */ - get location() { - return { - start: this.startNode.loc.start, - end: this.endNode.loc.end, - }; - } - - /** - * `true` if this range is empty. - * @type {boolean} - */ - get isEmpty() { - return !(this.startNode && this.endNode); - } - - /** - * Checks whether the given node is inside of this range. - * @param {ASTNode|Token} node The node to check. - * @returns {boolean} `true` if the node is inside of this range. - */ - contains(node) { - return ( - node.range[0] >= this.startNode.range[0] && - node.range[1] <= this.endNode.range[1] - ); - } - - /** - * Checks whether the given node is consecutive to this range. - * @param {ASTNode} node The node to check. - * @returns {boolean} `true` if the node is consecutive to this range. - */ - isConsecutive(node) { - return this.contains(this.sourceCode.getTokenBefore(node)); - } - - /** - * Merges the given node to this range. - * @param {ASTNode} node The node to merge. - * @returns {void} - */ - merge(node) { - this.endNode = node; - } - - /** - * Resets this range by the given node or null. - * @param {ASTNode|null} node The node to reset, or null. - * @returns {void} - */ - reset(node) { - this.startNode = this.endNode = node; - } + constructor(sourceCode) { + this.sourceCode = sourceCode; + this.startNode = null; + this.endNode = null; + } + + /** + * The location object of this range. + * @type {Object} + */ + get location() { + return { + start: this.startNode.loc.start, + end: this.endNode.loc.end + }; + } + + /** + * `true` if this range is empty. + * @type {boolean} + */ + get isEmpty() { + return !(this.startNode && this.endNode); + } + + /** + * Checks whether the given node is inside of this range. + * @param {ASTNode|Token} node The node to check. + * @returns {boolean} `true` if the node is inside of this range. + */ + contains(node) { + return ( + node.range[0] >= this.startNode.range[0] && + node.range[1] <= this.endNode.range[1] + ); + } + + /** + * Checks whether the given node is consecutive to this range. + * @param {ASTNode} node The node to check. + * @returns {boolean} `true` if the node is consecutive to this range. + */ + isConsecutive(node) { + return this.contains(this.sourceCode.getTokenBefore(node)); + } + + /** + * Merges the given node to this range. + * @param {ASTNode} node The node to merge. + * @returns {void} + */ + merge(node) { + this.endNode = node; + } + + /** + * Resets this range by the given node or null. + * @param {ASTNode|null} node The node to reset, or null. + * @returns {void} + */ + reset(node) { + this.startNode = this.endNode = node; + } } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - docs: { - description: - "Disallow unreachable code after `return`, `throw`, `continue`, and `break` statements", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-unreachable", - }, - - schema: [], - - messages: { - unreachableCode: "Unreachable code.", - }, - }, - - create(context) { - /** @type {ConstructorInfo | null} */ - let constructorInfo = null; - - /** @type {ConsecutiveRange} */ - const range = new ConsecutiveRange(context.sourceCode); - - /** @type {Array>} */ - const codePathSegments = []; - - /** @type {Set} */ - let currentCodePathSegments = new Set(); - - /** - * Reports a given node if it's unreachable. - * @param {ASTNode} node A statement node to report. - * @returns {void} - */ - function reportIfUnreachable(node) { - let nextNode = null; - - if ( - node && - (node.type === "PropertyDefinition" || - areAllSegmentsUnreachable(currentCodePathSegments)) - ) { - // Store this statement to distinguish consecutive statements. - if (range.isEmpty) { - range.reset(node); - return; - } - - // Skip if this statement is inside of the current range. - if (range.contains(node)) { - return; - } - - // Merge if this statement is consecutive to the current range. - if (range.isConsecutive(node)) { - range.merge(node); - return; - } - - nextNode = node; - } - - /* - * Report the current range since this statement is reachable or is - * not consecutive to the current range. - */ - if (!range.isEmpty) { - context.report({ - messageId: "unreachableCode", - loc: range.location, - node: range.startNode, - }); - } - - // Update the current range. - range.reset(nextNode); - } - - return { - // Manages the current code path. - onCodePathStart() { - codePathSegments.push(currentCodePathSegments); - currentCodePathSegments = new Set(); - }, - - onCodePathEnd() { - currentCodePathSegments = codePathSegments.pop(); - }, - - onUnreachableCodePathSegmentStart(segment) { - currentCodePathSegments.add(segment); - }, - - onUnreachableCodePathSegmentEnd(segment) { - currentCodePathSegments.delete(segment); - }, - - onCodePathSegmentEnd(segment) { - currentCodePathSegments.delete(segment); - }, - - onCodePathSegmentStart(segment) { - currentCodePathSegments.add(segment); - }, - - // Registers for all statement nodes (excludes FunctionDeclaration). - BlockStatement: reportIfUnreachable, - BreakStatement: reportIfUnreachable, - ClassDeclaration: reportIfUnreachable, - ContinueStatement: reportIfUnreachable, - DebuggerStatement: reportIfUnreachable, - DoWhileStatement: reportIfUnreachable, - ExpressionStatement: reportIfUnreachable, - ForInStatement: reportIfUnreachable, - ForOfStatement: reportIfUnreachable, - ForStatement: reportIfUnreachable, - IfStatement: reportIfUnreachable, - ImportDeclaration: reportIfUnreachable, - LabeledStatement: reportIfUnreachable, - ReturnStatement: reportIfUnreachable, - SwitchStatement: reportIfUnreachable, - ThrowStatement: reportIfUnreachable, - TryStatement: reportIfUnreachable, - - VariableDeclaration(node) { - if ( - node.kind !== "var" || - node.declarations.some(isInitialized) - ) { - reportIfUnreachable(node); - } - }, - - WhileStatement: reportIfUnreachable, - WithStatement: reportIfUnreachable, - ExportNamedDeclaration: reportIfUnreachable, - ExportDefaultDeclaration: reportIfUnreachable, - ExportAllDeclaration: reportIfUnreachable, - - "Program:exit"() { - reportIfUnreachable(); - }, - - /* - * Instance fields defined in a subclass are never created if the constructor of the subclass - * doesn't call `super()`, so their definitions are unreachable code. - */ - "MethodDefinition[kind='constructor']"() { - constructorInfo = { - upper: constructorInfo, - hasSuperCall: false, - }; - }, - "MethodDefinition[kind='constructor']:exit"(node) { - const { hasSuperCall } = constructorInfo; - - constructorInfo = constructorInfo.upper; - - // skip typescript constructors without the body - if (!node.value.body) { - return; - } - - const classDefinition = node.parent.parent; - - if (classDefinition.superClass && !hasSuperCall) { - for (const element of classDefinition.body.body) { - if ( - element.type === "PropertyDefinition" && - !element.static - ) { - reportIfUnreachable(element); - } - } - } - }, - "CallExpression > Super.callee"() { - if (constructorInfo) { - constructorInfo.hasSuperCall = true; - } - }, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow unreachable code after `return`, `throw`, `continue`, and `break` statements", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-unreachable" + }, + + schema: [], + + messages: { + unreachableCode: "Unreachable code." + } + }, + + create(context) { + + /** @type {ConstructorInfo | null} */ + let constructorInfo = null; + + /** @type {ConsecutiveRange} */ + const range = new ConsecutiveRange(context.sourceCode); + + /** @type {Array>} */ + const codePathSegments = []; + + /** @type {Set} */ + let currentCodePathSegments = new Set(); + + /** + * Reports a given node if it's unreachable. + * @param {ASTNode} node A statement node to report. + * @returns {void} + */ + function reportIfUnreachable(node) { + let nextNode = null; + + if (node && (node.type === "PropertyDefinition" || areAllSegmentsUnreachable(currentCodePathSegments))) { + + // Store this statement to distinguish consecutive statements. + if (range.isEmpty) { + range.reset(node); + return; + } + + // Skip if this statement is inside of the current range. + if (range.contains(node)) { + return; + } + + // Merge if this statement is consecutive to the current range. + if (range.isConsecutive(node)) { + range.merge(node); + return; + } + + nextNode = node; + } + + /* + * Report the current range since this statement is reachable or is + * not consecutive to the current range. + */ + if (!range.isEmpty) { + context.report({ + messageId: "unreachableCode", + loc: range.location, + node: range.startNode + }); + } + + // Update the current range. + range.reset(nextNode); + } + + return { + + // Manages the current code path. + onCodePathStart() { + codePathSegments.push(currentCodePathSegments); + currentCodePathSegments = new Set(); + }, + + onCodePathEnd() { + currentCodePathSegments = codePathSegments.pop(); + }, + + onUnreachableCodePathSegmentStart(segment) { + currentCodePathSegments.add(segment); + }, + + onUnreachableCodePathSegmentEnd(segment) { + currentCodePathSegments.delete(segment); + }, + + onCodePathSegmentEnd(segment) { + currentCodePathSegments.delete(segment); + }, + + onCodePathSegmentStart(segment) { + currentCodePathSegments.add(segment); + }, + + // Registers for all statement nodes (excludes FunctionDeclaration). + BlockStatement: reportIfUnreachable, + BreakStatement: reportIfUnreachable, + ClassDeclaration: reportIfUnreachable, + ContinueStatement: reportIfUnreachable, + DebuggerStatement: reportIfUnreachable, + DoWhileStatement: reportIfUnreachable, + ExpressionStatement: reportIfUnreachable, + ForInStatement: reportIfUnreachable, + ForOfStatement: reportIfUnreachable, + ForStatement: reportIfUnreachable, + IfStatement: reportIfUnreachable, + ImportDeclaration: reportIfUnreachable, + LabeledStatement: reportIfUnreachable, + ReturnStatement: reportIfUnreachable, + SwitchStatement: reportIfUnreachable, + ThrowStatement: reportIfUnreachable, + TryStatement: reportIfUnreachable, + + VariableDeclaration(node) { + if (node.kind !== "var" || node.declarations.some(isInitialized)) { + reportIfUnreachable(node); + } + }, + + WhileStatement: reportIfUnreachable, + WithStatement: reportIfUnreachable, + ExportNamedDeclaration: reportIfUnreachable, + ExportDefaultDeclaration: reportIfUnreachable, + ExportAllDeclaration: reportIfUnreachable, + + "Program:exit"() { + reportIfUnreachable(); + }, + + /* + * Instance fields defined in a subclass are never created if the constructor of the subclass + * doesn't call `super()`, so their definitions are unreachable code. + */ + "MethodDefinition[kind='constructor']"() { + constructorInfo = { + upper: constructorInfo, + hasSuperCall: false + }; + }, + "MethodDefinition[kind='constructor']:exit"(node) { + const { hasSuperCall } = constructorInfo; + + constructorInfo = constructorInfo.upper; + + // skip typescript constructors without the body + if (!node.value.body) { + return; + } + + const classDefinition = node.parent.parent; + + if (classDefinition.superClass && !hasSuperCall) { + for (const element of classDefinition.body.body) { + if (element.type === "PropertyDefinition" && !element.static) { + reportIfUnreachable(element); + } + } + } + }, + "CallExpression > Super.callee"() { + if (constructorInfo) { + constructorInfo.hasSuperCall = true; + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-unsafe-finally.js b/node_modules/eslint/lib/rules/no-unsafe-finally.js index 4c6393e60..ebd24328f 100644 --- a/node_modules/eslint/lib/rules/no-unsafe-finally.js +++ b/node_modules/eslint/lib/rules/no-unsafe-finally.js @@ -9,111 +9,103 @@ // Helpers //------------------------------------------------------------------------------ -const SENTINEL_NODE_TYPE_RETURN_THROW = - /^(?:Program|(?:Function|Class)(?:Declaration|Expression)|ArrowFunctionExpression)$/u; -const SENTINEL_NODE_TYPE_BREAK = - /^(?:Program|(?:Function|Class)(?:Declaration|Expression)|ArrowFunctionExpression|DoWhileStatement|WhileStatement|ForOfStatement|ForInStatement|ForStatement|SwitchStatement)$/u; -const SENTINEL_NODE_TYPE_CONTINUE = - /^(?:Program|(?:Function|Class)(?:Declaration|Expression)|ArrowFunctionExpression|DoWhileStatement|WhileStatement|ForOfStatement|ForInStatement|ForStatement)$/u; +const SENTINEL_NODE_TYPE_RETURN_THROW = /^(?:Program|(?:Function|Class)(?:Declaration|Expression)|ArrowFunctionExpression)$/u; +const SENTINEL_NODE_TYPE_BREAK = /^(?:Program|(?:Function|Class)(?:Declaration|Expression)|ArrowFunctionExpression|DoWhileStatement|WhileStatement|ForOfStatement|ForInStatement|ForStatement|SwitchStatement)$/u; +const SENTINEL_NODE_TYPE_CONTINUE = /^(?:Program|(?:Function|Class)(?:Declaration|Expression)|ArrowFunctionExpression|DoWhileStatement|WhileStatement|ForOfStatement|ForInStatement|ForStatement)$/u; + //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", + meta: { + type: "problem", + + docs: { + description: "Disallow control flow statements in `finally` blocks", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-unsafe-finally" + }, - docs: { - description: "Disallow control flow statements in `finally` blocks", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-unsafe-finally", - }, + schema: [], - schema: [], + messages: { + unsafeUsage: "Unsafe usage of {{nodeType}}." + } + }, + create(context) { - messages: { - unsafeUsage: "Unsafe usage of {{nodeType}}.", - }, - }, - create(context) { - /** - * Checks if the node is the finalizer of a TryStatement - * @param {ASTNode} node node to check. - * @returns {boolean} - true if the node is the finalizer of a TryStatement - */ - function isFinallyBlock(node) { - return ( - node.parent.type === "TryStatement" && - node.parent.finalizer === node - ); - } + /** + * Checks if the node is the finalizer of a TryStatement + * @param {ASTNode} node node to check. + * @returns {boolean} - true if the node is the finalizer of a TryStatement + */ + function isFinallyBlock(node) { + return node.parent.type === "TryStatement" && node.parent.finalizer === node; + } - /** - * Climbs up the tree if the node is not a sentinel node - * @param {ASTNode} node node to check. - * @param {string} label label of the break or continue statement - * @returns {boolean} - return whether the node is a finally block or a sentinel node - */ - function isInFinallyBlock(node, label) { - let labelInside = false; - let sentinelNodeType; + /** + * Climbs up the tree if the node is not a sentinel node + * @param {ASTNode} node node to check. + * @param {string} label label of the break or continue statement + * @returns {boolean} - return whether the node is a finally block or a sentinel node + */ + function isInFinallyBlock(node, label) { + let labelInside = false; + let sentinelNodeType; - if (node.type === "BreakStatement" && !node.label) { - sentinelNodeType = SENTINEL_NODE_TYPE_BREAK; - } else if (node.type === "ContinueStatement") { - sentinelNodeType = SENTINEL_NODE_TYPE_CONTINUE; - } else { - sentinelNodeType = SENTINEL_NODE_TYPE_RETURN_THROW; - } + if (node.type === "BreakStatement" && !node.label) { + sentinelNodeType = SENTINEL_NODE_TYPE_BREAK; + } else if (node.type === "ContinueStatement") { + sentinelNodeType = SENTINEL_NODE_TYPE_CONTINUE; + } else { + sentinelNodeType = SENTINEL_NODE_TYPE_RETURN_THROW; + } - for ( - let currentNode = node; - currentNode && !sentinelNodeType.test(currentNode.type); - currentNode = currentNode.parent - ) { - if ( - currentNode.parent.label && - label && - currentNode.parent.label.name === label.name - ) { - labelInside = true; - } - if (isFinallyBlock(currentNode)) { - if (label && labelInside) { - return false; - } - return true; - } - } - return false; - } + for ( + let currentNode = node; + currentNode && !sentinelNodeType.test(currentNode.type); + currentNode = currentNode.parent + ) { + if (currentNode.parent.label && label && (currentNode.parent.label.name === label.name)) { + labelInside = true; + } + if (isFinallyBlock(currentNode)) { + if (label && labelInside) { + return false; + } + return true; + } + } + return false; + } - /** - * Checks whether the possibly-unsafe statement is inside a finally block. - * @param {ASTNode} node node to check. - * @returns {void} - */ - function check(node) { - if (isInFinallyBlock(node, node.label)) { - context.report({ - messageId: "unsafeUsage", - data: { - nodeType: node.type, - }, - node, - line: node.loc.line, - column: node.loc.column, - }); - } - } + /** + * Checks whether the possibly-unsafe statement is inside a finally block. + * @param {ASTNode} node node to check. + * @returns {void} + */ + function check(node) { + if (isInFinallyBlock(node, node.label)) { + context.report({ + messageId: "unsafeUsage", + data: { + nodeType: node.type + }, + node, + line: node.loc.line, + column: node.loc.column + }); + } + } - return { - ReturnStatement: check, - ThrowStatement: check, - BreakStatement: check, - ContinueStatement: check, - }; - }, + return { + ReturnStatement: check, + ThrowStatement: check, + BreakStatement: check, + ContinueStatement: check + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-unsafe-negation.js b/node_modules/eslint/lib/rules/no-unsafe-negation.js index 17e027106..cabd7e2cc 100644 --- a/node_modules/eslint/lib/rules/no-unsafe-negation.js +++ b/node_modules/eslint/lib/rules/no-unsafe-negation.js @@ -21,7 +21,7 @@ const astUtils = require("./utils/ast-utils"); * @returns {boolean} `true` if the operator is `in` or `instanceof` */ function isInOrInstanceOfOperator(op) { - return op === "in" || op === "instanceof"; + return op === "in" || op === "instanceof"; } /** @@ -30,7 +30,7 @@ function isInOrInstanceOfOperator(op) { * @returns {boolean} `true` if the operator is an ordering relational operator. */ function isOrderingRelationalOperator(op) { - return op === "<" || op === ">" || op === ">=" || op === "<="; + return op === "<" || op === ">" || op === ">=" || op === "<="; } /** @@ -39,114 +39,90 @@ function isOrderingRelationalOperator(op) { * @returns {boolean} `true` if the node is a logical negation expression. */ function isNegation(node) { - return node.type === "UnaryExpression" && node.operator === "!"; + return node.type === "UnaryExpression" && node.operator === "!"; } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - defaultOptions: [ - { - enforceForOrderingRelations: false, - }, - ], - - docs: { - description: - "Disallow negating the left operand of relational operators", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-unsafe-negation", - }, - - hasSuggestions: true, - - schema: [ - { - type: "object", - properties: { - enforceForOrderingRelations: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - - fixable: null, - - messages: { - unexpected: - "Unexpected negating the left operand of '{{operator}}' operator.", - suggestNegatedExpression: - "Negate '{{operator}}' expression instead of its left operand. This changes the current behavior.", - suggestParenthesisedNegation: - "Wrap negation in '()' to make the intention explicit. This preserves the current behavior.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - const [{ enforceForOrderingRelations }] = context.options; - - return { - BinaryExpression(node) { - const operator = node.operator; - const orderingRelationRuleApplies = - enforceForOrderingRelations && - isOrderingRelationalOperator(operator); - - if ( - (isInOrInstanceOfOperator(operator) || - orderingRelationRuleApplies) && - isNegation(node.left) && - !astUtils.isParenthesised(sourceCode, node.left) - ) { - context.report({ - node, - loc: node.left.loc, - messageId: "unexpected", - data: { operator }, - suggest: [ - { - messageId: "suggestNegatedExpression", - data: { operator }, - fix(fixer) { - const negationToken = - sourceCode.getFirstToken(node.left); - const fixRange = [ - negationToken.range[1], - node.range[1], - ]; - const text = sourceCode.text.slice( - fixRange[0], - fixRange[1], - ); - - return fixer.replaceTextRange( - fixRange, - `(${text})`, - ); - }, - }, - { - messageId: "suggestParenthesisedNegation", - fix(fixer) { - return fixer.replaceText( - node.left, - `(${sourceCode.getText(node.left)})`, - ); - }, - }, - ], - }); - } - }, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow negating the left operand of relational operators", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-unsafe-negation" + }, + + hasSuggestions: true, + + schema: [ + { + type: "object", + properties: { + enforceForOrderingRelations: { + type: "boolean", + default: false + } + }, + additionalProperties: false + } + ], + + fixable: null, + + messages: { + unexpected: "Unexpected negating the left operand of '{{operator}}' operator.", + suggestNegatedExpression: "Negate '{{operator}}' expression instead of its left operand. This changes the current behavior.", + suggestParenthesisedNegation: "Wrap negation in '()' to make the intention explicit. This preserves the current behavior." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + const options = context.options[0] || {}; + const enforceForOrderingRelations = options.enforceForOrderingRelations === true; + + return { + BinaryExpression(node) { + const operator = node.operator; + const orderingRelationRuleApplies = enforceForOrderingRelations && isOrderingRelationalOperator(operator); + + if ( + (isInOrInstanceOfOperator(operator) || orderingRelationRuleApplies) && + isNegation(node.left) && + !astUtils.isParenthesised(sourceCode, node.left) + ) { + context.report({ + node, + loc: node.left.loc, + messageId: "unexpected", + data: { operator }, + suggest: [ + { + messageId: "suggestNegatedExpression", + data: { operator }, + fix(fixer) { + const negationToken = sourceCode.getFirstToken(node.left); + const fixRange = [negationToken.range[1], node.range[1]]; + const text = sourceCode.text.slice(fixRange[0], fixRange[1]); + + return fixer.replaceTextRange(fixRange, `(${text})`); + } + }, + { + messageId: "suggestParenthesisedNegation", + fix(fixer) { + return fixer.replaceText(node.left, `(${sourceCode.getText(node.left)})`); + } + } + ] + }); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-unsafe-optional-chaining.js b/node_modules/eslint/lib/rules/no-unsafe-optional-chaining.js index 95875b7eb..fe2bead85 100644 --- a/node_modules/eslint/lib/rules/no-unsafe-optional-chaining.js +++ b/node_modules/eslint/lib/rules/no-unsafe-optional-chaining.js @@ -6,14 +6,7 @@ "use strict"; const UNSAFE_ARITHMETIC_OPERATORS = new Set(["+", "-", "/", "*", "%", "**"]); -const UNSAFE_ASSIGNMENT_OPERATORS = new Set([ - "+=", - "-=", - "/=", - "*=", - "%=", - "**=", -]); +const UNSAFE_ASSIGNMENT_OPERATORS = new Set(["+=", "-=", "/=", "*=", "%=", "**="]); const UNSAFE_RELATIONAL_OPERATORS = new Set(["in", "instanceof"]); /** @@ -22,200 +15,191 @@ const UNSAFE_RELATIONAL_OPERATORS = new Set(["in", "instanceof"]); * @returns {boolean} `true` if a node is a destructuring pattern, otherwise `false` */ function isDestructuringPattern(node) { - return node.type === "ObjectPattern" || node.type === "ArrayPattern"; + return node.type === "ObjectPattern" || node.type === "ArrayPattern"; } -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", + meta: { + type: "problem", - defaultOptions: [ - { - disallowArithmeticOperators: false, - }, - ], + docs: { + description: "Disallow use of optional chaining in contexts where the `undefined` value is not allowed", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-unsafe-optional-chaining" + }, + schema: [{ + type: "object", + properties: { + disallowArithmeticOperators: { + type: "boolean", + default: false + } + }, + additionalProperties: false + }], + fixable: null, + messages: { + unsafeOptionalChain: "Unsafe usage of optional chaining. If it short-circuits with 'undefined' the evaluation will throw TypeError.", + unsafeArithmetic: "Unsafe arithmetic operation on optional chaining. It can result in NaN." + } + }, - docs: { - description: - "Disallow use of optional chaining in contexts where the `undefined` value is not allowed", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-unsafe-optional-chaining", - }, - schema: [ - { - type: "object", - properties: { - disallowArithmeticOperators: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - fixable: null, - messages: { - unsafeOptionalChain: - "Unsafe usage of optional chaining. If it short-circuits with 'undefined' the evaluation will throw TypeError.", - unsafeArithmetic: - "Unsafe arithmetic operation on optional chaining. It can result in NaN.", - }, - }, + create(context) { + const options = context.options[0] || {}; + const disallowArithmeticOperators = (options.disallowArithmeticOperators) || false; - create(context) { - const [{ disallowArithmeticOperators }] = context.options; + /** + * Reports unsafe usage of optional chaining + * @param {ASTNode} node node to report + * @returns {void} + */ + function reportUnsafeUsage(node) { + context.report({ + messageId: "unsafeOptionalChain", + node + }); + } - /** - * Reports unsafe usage of optional chaining - * @param {ASTNode} node node to report - * @returns {void} - */ - function reportUnsafeUsage(node) { - context.report({ - messageId: "unsafeOptionalChain", - node, - }); - } + /** + * Reports unsafe arithmetic operation on optional chaining + * @param {ASTNode} node node to report + * @returns {void} + */ + function reportUnsafeArithmetic(node) { + context.report({ + messageId: "unsafeArithmetic", + node + }); + } - /** - * Reports unsafe arithmetic operation on optional chaining - * @param {ASTNode} node node to report - * @returns {void} - */ - function reportUnsafeArithmetic(node) { - context.report({ - messageId: "unsafeArithmetic", - node, - }); - } + /** + * Checks and reports if a node can short-circuit with `undefined` by optional chaining. + * @param {ASTNode} [node] node to check + * @param {Function} reportFunc report function + * @returns {void} + */ + function checkUndefinedShortCircuit(node, reportFunc) { + if (!node) { + return; + } + switch (node.type) { + case "LogicalExpression": + if (node.operator === "||" || node.operator === "??") { + checkUndefinedShortCircuit(node.right, reportFunc); + } else if (node.operator === "&&") { + checkUndefinedShortCircuit(node.left, reportFunc); + checkUndefinedShortCircuit(node.right, reportFunc); + } + break; + case "SequenceExpression": + checkUndefinedShortCircuit( + node.expressions[node.expressions.length - 1], + reportFunc + ); + break; + case "ConditionalExpression": + checkUndefinedShortCircuit(node.consequent, reportFunc); + checkUndefinedShortCircuit(node.alternate, reportFunc); + break; + case "AwaitExpression": + checkUndefinedShortCircuit(node.argument, reportFunc); + break; + case "ChainExpression": + reportFunc(node); + break; + default: + break; + } + } - /** - * Checks and reports if a node can short-circuit with `undefined` by optional chaining. - * @param {ASTNode} [node] node to check - * @param {Function} reportFunc report function - * @returns {void} - */ - function checkUndefinedShortCircuit(node, reportFunc) { - if (!node) { - return; - } - switch (node.type) { - case "LogicalExpression": - if (node.operator === "||" || node.operator === "??") { - checkUndefinedShortCircuit(node.right, reportFunc); - } else if (node.operator === "&&") { - checkUndefinedShortCircuit(node.left, reportFunc); - checkUndefinedShortCircuit(node.right, reportFunc); - } - break; - case "SequenceExpression": - checkUndefinedShortCircuit( - node.expressions.at(-1), - reportFunc, - ); - break; - case "ConditionalExpression": - checkUndefinedShortCircuit(node.consequent, reportFunc); - checkUndefinedShortCircuit(node.alternate, reportFunc); - break; - case "AwaitExpression": - checkUndefinedShortCircuit(node.argument, reportFunc); - break; - case "ChainExpression": - reportFunc(node); - break; - default: - break; - } - } + /** + * Checks unsafe usage of optional chaining + * @param {ASTNode} node node to check + * @returns {void} + */ + function checkUnsafeUsage(node) { + checkUndefinedShortCircuit(node, reportUnsafeUsage); + } - /** - * Checks unsafe usage of optional chaining - * @param {ASTNode} node node to check - * @returns {void} - */ - function checkUnsafeUsage(node) { - checkUndefinedShortCircuit(node, reportUnsafeUsage); - } + /** + * Checks unsafe arithmetic operations on optional chaining + * @param {ASTNode} node node to check + * @returns {void} + */ + function checkUnsafeArithmetic(node) { + checkUndefinedShortCircuit(node, reportUnsafeArithmetic); + } - /** - * Checks unsafe arithmetic operations on optional chaining - * @param {ASTNode} node node to check - * @returns {void} - */ - function checkUnsafeArithmetic(node) { - checkUndefinedShortCircuit(node, reportUnsafeArithmetic); - } - - return { - "AssignmentExpression, AssignmentPattern"(node) { - if (isDestructuringPattern(node.left)) { - checkUnsafeUsage(node.right); - } - }, - "ClassDeclaration, ClassExpression"(node) { - checkUnsafeUsage(node.superClass); - }, - CallExpression(node) { - if (!node.optional) { - checkUnsafeUsage(node.callee); - } - }, - NewExpression(node) { - checkUnsafeUsage(node.callee); - }, - VariableDeclarator(node) { - if (isDestructuringPattern(node.id)) { - checkUnsafeUsage(node.init); - } - }, - MemberExpression(node) { - if (!node.optional) { - checkUnsafeUsage(node.object); - } - }, - TaggedTemplateExpression(node) { - checkUnsafeUsage(node.tag); - }, - ForOfStatement(node) { - checkUnsafeUsage(node.right); - }, - SpreadElement(node) { - if (node.parent && node.parent.type !== "ObjectExpression") { - checkUnsafeUsage(node.argument); - } - }, - BinaryExpression(node) { - if (UNSAFE_RELATIONAL_OPERATORS.has(node.operator)) { - checkUnsafeUsage(node.right); - } - if ( - disallowArithmeticOperators && - UNSAFE_ARITHMETIC_OPERATORS.has(node.operator) - ) { - checkUnsafeArithmetic(node.right); - checkUnsafeArithmetic(node.left); - } - }, - WithStatement(node) { - checkUnsafeUsage(node.object); - }, - UnaryExpression(node) { - if ( - disallowArithmeticOperators && - UNSAFE_ARITHMETIC_OPERATORS.has(node.operator) - ) { - checkUnsafeArithmetic(node.argument); - } - }, - AssignmentExpression(node) { - if ( - disallowArithmeticOperators && - UNSAFE_ASSIGNMENT_OPERATORS.has(node.operator) - ) { - checkUnsafeArithmetic(node.right); - } - }, - }; - }, + return { + "AssignmentExpression, AssignmentPattern"(node) { + if (isDestructuringPattern(node.left)) { + checkUnsafeUsage(node.right); + } + }, + "ClassDeclaration, ClassExpression"(node) { + checkUnsafeUsage(node.superClass); + }, + CallExpression(node) { + if (!node.optional) { + checkUnsafeUsage(node.callee); + } + }, + NewExpression(node) { + checkUnsafeUsage(node.callee); + }, + VariableDeclarator(node) { + if (isDestructuringPattern(node.id)) { + checkUnsafeUsage(node.init); + } + }, + MemberExpression(node) { + if (!node.optional) { + checkUnsafeUsage(node.object); + } + }, + TaggedTemplateExpression(node) { + checkUnsafeUsage(node.tag); + }, + ForOfStatement(node) { + checkUnsafeUsage(node.right); + }, + SpreadElement(node) { + if (node.parent && node.parent.type !== "ObjectExpression") { + checkUnsafeUsage(node.argument); + } + }, + BinaryExpression(node) { + if (UNSAFE_RELATIONAL_OPERATORS.has(node.operator)) { + checkUnsafeUsage(node.right); + } + if ( + disallowArithmeticOperators && + UNSAFE_ARITHMETIC_OPERATORS.has(node.operator) + ) { + checkUnsafeArithmetic(node.right); + checkUnsafeArithmetic(node.left); + } + }, + WithStatement(node) { + checkUnsafeUsage(node.object); + }, + UnaryExpression(node) { + if ( + disallowArithmeticOperators && + UNSAFE_ARITHMETIC_OPERATORS.has(node.operator) + ) { + checkUnsafeArithmetic(node.argument); + } + }, + AssignmentExpression(node) { + if ( + disallowArithmeticOperators && + UNSAFE_ASSIGNMENT_OPERATORS.has(node.operator) + ) { + checkUnsafeArithmetic(node.right); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-unused-expressions.js b/node_modules/eslint/lib/rules/no-unused-expressions.js index 3c530e6ef..46bb7baac 100644 --- a/node_modules/eslint/lib/rules/no-unused-expressions.js +++ b/node_modules/eslint/lib/rules/no-unused-expressions.js @@ -15,7 +15,7 @@ const astUtils = require("./utils/ast-utils"); * @returns {boolean} `true`. */ function alwaysTrue() { - return true; + return true; } /** @@ -23,205 +23,164 @@ function alwaysTrue() { * @returns {boolean} `false`. */ function alwaysFalse() { - return false; + return false; } -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - dialects: ["javascript", "typescript"], - language: "javascript", - type: "suggestion", - - docs: { - description: "Disallow unused expressions", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-unused-expressions", - }, - - schema: [ - { - type: "object", - properties: { - allowShortCircuit: { - type: "boolean", - }, - allowTernary: { - type: "boolean", - }, - allowTaggedTemplates: { - type: "boolean", - }, - enforceForJSX: { - type: "boolean", - }, - ignoreDirectives: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - - defaultOptions: [ - { - allowShortCircuit: false, - allowTernary: false, - allowTaggedTemplates: false, - enforceForJSX: false, - ignoreDirectives: false, - }, - ], - - messages: { - unusedExpression: - "Expected an assignment or function call and instead saw an expression.", - }, - }, - - create(context) { - const [ - { - allowShortCircuit, - allowTernary, - allowTaggedTemplates, - enforceForJSX, - ignoreDirectives, - }, - ] = context.options; - - /** - * Has AST suggesting a directive. - * @param {ASTNode} node any node - * @returns {boolean} whether the given node structurally represents a directive - */ - function looksLikeDirective(node) { - return ( - node.type === "ExpressionStatement" && - node.expression.type === "Literal" && - typeof node.expression.value === "string" - ); - } - - /** - * Gets the leading sequence of members in a list that pass the predicate. - * @param {Function} predicate ([a] -> Boolean) the function used to make the determination - * @param {a[]} list the input list - * @returns {a[]} the leading sequence of members in the given list that pass the given predicate - */ - function takeWhile(predicate, list) { - for (let i = 0; i < list.length; ++i) { - if (!predicate(list[i])) { - return list.slice(0, i); - } - } - return list.slice(); - } - - /** - * Gets leading directives nodes in a Node body. - * @param {ASTNode} node a Program or BlockStatement node - * @returns {ASTNode[]} the leading sequence of directive nodes in the given node's body - */ - function directives(node) { - return takeWhile(looksLikeDirective, node.body); - } - - /** - * Detect if a Node is a directive. - * @param {ASTNode} node any node - * @returns {boolean} whether the given node is considered a directive in its current position - */ - function isDirective(node) { - /** - * https://tc39.es/ecma262/#directive-prologue - * - * Only `FunctionBody`, `ScriptBody` and `ModuleBody` can have directive prologue. - * Class static blocks do not have directive prologue. - */ - return ( - astUtils.isTopLevelExpressionStatement(node) && - directives(node.parent).includes(node) - ); - } - - /** - * The member functions return `true` if the type has no side-effects. - * Unknown nodes are handled as `false`, then this rule ignores those. - */ - const Checker = Object.assign(Object.create(null), { - isDisallowed(node) { - return (Checker[node.type] || alwaysFalse)(node); - }, - - ArrayExpression: alwaysTrue, - ArrowFunctionExpression: alwaysTrue, - BinaryExpression: alwaysTrue, - ChainExpression(node) { - return Checker.isDisallowed(node.expression); - }, - ClassExpression: alwaysTrue, - ConditionalExpression(node) { - if (allowTernary) { - return ( - Checker.isDisallowed(node.consequent) || - Checker.isDisallowed(node.alternate) - ); - } - return true; - }, - FunctionExpression: alwaysTrue, - Identifier: alwaysTrue, - JSXElement() { - return enforceForJSX; - }, - JSXFragment() { - return enforceForJSX; - }, - Literal: alwaysTrue, - LogicalExpression(node) { - if (allowShortCircuit) { - return Checker.isDisallowed(node.right); - } - return true; - }, - MemberExpression: alwaysTrue, - MetaProperty: alwaysTrue, - ObjectExpression: alwaysTrue, - SequenceExpression: alwaysTrue, - TaggedTemplateExpression() { - return !allowTaggedTemplates; - }, - TemplateLiteral: alwaysTrue, - ThisExpression: alwaysTrue, - UnaryExpression(node) { - return node.operator !== "void" && node.operator !== "delete"; - }, - // TypeScript-specific node types - TSAsExpression(node) { - return Checker.isDisallowed(node.expression); - }, - TSTypeAssertion(node) { - return Checker.isDisallowed(node.expression); - }, - TSNonNullExpression(node) { - return Checker.isDisallowed(node.expression); - }, - TSInstantiationExpression(node) { - return Checker.isDisallowed(node.expression); - }, - }); - - return { - ExpressionStatement(node) { - if ( - Checker.isDisallowed(node.expression) && - !astUtils.isDirective(node) && - !(ignoreDirectives && isDirective(node)) - ) { - context.report({ node, messageId: "unusedExpression" }); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow unused expressions", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-unused-expressions" + }, + + schema: [ + { + type: "object", + properties: { + allowShortCircuit: { + type: "boolean", + default: false + }, + allowTernary: { + type: "boolean", + default: false + }, + allowTaggedTemplates: { + type: "boolean", + default: false + }, + enforceForJSX: { + type: "boolean", + default: false + } + }, + additionalProperties: false + } + ], + + messages: { + unusedExpression: "Expected an assignment or function call and instead saw an expression." + } + }, + + create(context) { + const config = context.options[0] || {}, + allowShortCircuit = config.allowShortCircuit || false, + allowTernary = config.allowTernary || false, + allowTaggedTemplates = config.allowTaggedTemplates || false, + enforceForJSX = config.enforceForJSX || false; + + /** + * Has AST suggesting a directive. + * @param {ASTNode} node any node + * @returns {boolean} whether the given node structurally represents a directive + */ + function looksLikeDirective(node) { + return node.type === "ExpressionStatement" && + node.expression.type === "Literal" && typeof node.expression.value === "string"; + } + + /** + * Gets the leading sequence of members in a list that pass the predicate. + * @param {Function} predicate ([a] -> Boolean) the function used to make the determination + * @param {a[]} list the input list + * @returns {a[]} the leading sequence of members in the given list that pass the given predicate + */ + function takeWhile(predicate, list) { + for (let i = 0; i < list.length; ++i) { + if (!predicate(list[i])) { + return list.slice(0, i); + } + } + return list.slice(); + } + + /** + * Gets leading directives nodes in a Node body. + * @param {ASTNode} node a Program or BlockStatement node + * @returns {ASTNode[]} the leading sequence of directive nodes in the given node's body + */ + function directives(node) { + return takeWhile(looksLikeDirective, node.body); + } + + /** + * Detect if a Node is a directive. + * @param {ASTNode} node any node + * @returns {boolean} whether the given node is considered a directive in its current position + */ + function isDirective(node) { + + /** + * https://tc39.es/ecma262/#directive-prologue + * + * Only `FunctionBody`, `ScriptBody` and `ModuleBody` can have directive prologue. + * Class static blocks do not have directive prologue. + */ + return astUtils.isTopLevelExpressionStatement(node) && directives(node.parent).includes(node); + } + + /** + * The member functions return `true` if the type has no side-effects. + * Unknown nodes are handled as `false`, then this rule ignores those. + */ + const Checker = Object.assign(Object.create(null), { + isDisallowed(node) { + return (Checker[node.type] || alwaysFalse)(node); + }, + + ArrayExpression: alwaysTrue, + ArrowFunctionExpression: alwaysTrue, + BinaryExpression: alwaysTrue, + ChainExpression(node) { + return Checker.isDisallowed(node.expression); + }, + ClassExpression: alwaysTrue, + ConditionalExpression(node) { + if (allowTernary) { + return Checker.isDisallowed(node.consequent) || Checker.isDisallowed(node.alternate); + } + return true; + }, + FunctionExpression: alwaysTrue, + Identifier: alwaysTrue, + JSXElement() { + return enforceForJSX; + }, + JSXFragment() { + return enforceForJSX; + }, + Literal: alwaysTrue, + LogicalExpression(node) { + if (allowShortCircuit) { + return Checker.isDisallowed(node.right); + } + return true; + }, + MemberExpression: alwaysTrue, + MetaProperty: alwaysTrue, + ObjectExpression: alwaysTrue, + SequenceExpression: alwaysTrue, + TaggedTemplateExpression() { + return !allowTaggedTemplates; + }, + TemplateLiteral: alwaysTrue, + ThisExpression: alwaysTrue, + UnaryExpression(node) { + return node.operator !== "void" && node.operator !== "delete"; + } + }); + + return { + ExpressionStatement(node) { + if (Checker.isDisallowed(node.expression) && !isDirective(node)) { + context.report({ node, messageId: "unusedExpression" }); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-unused-labels.js b/node_modules/eslint/lib/rules/no-unused-labels.js index d7fe3e427..be06b324c 100644 --- a/node_modules/eslint/lib/rules/no-unused-labels.js +++ b/node_modules/eslint/lib/rules/no-unused-labels.js @@ -15,144 +15,129 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: "Disallow unused labels", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-unused-labels", - }, - - schema: [], - - fixable: "code", - - messages: { - unused: "'{{name}}:' is defined but never used.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - let scopeInfo = null; - - /** - * Adds a scope info to the stack. - * @param {ASTNode} node A node to add. This is a LabeledStatement. - * @returns {void} - */ - function enterLabeledScope(node) { - scopeInfo = { - label: node.label.name, - used: false, - upper: scopeInfo, - }; - } - - /** - * Checks if a `LabeledStatement` node is fixable. - * For a node to be fixable, there must be no comments between the label and the body. - * Furthermore, is must be possible to remove the label without turning the body statement into a - * directive after other fixes are applied. - * @param {ASTNode} node The node to evaluate. - * @returns {boolean} Whether or not the node is fixable. - */ - function isFixable(node) { - /* - * Only perform a fix if there are no comments between the label and the body. This will be the case - * when there is exactly one token/comment (the ":") between the label and the body. - */ - if ( - sourceCode.getTokenAfter(node.label, { - includeComments: true, - }) !== - sourceCode.getTokenBefore(node.body, { includeComments: true }) - ) { - return false; - } - - // Looking for the node's deepest ancestor which is not a `LabeledStatement`. - let ancestor = node.parent; - - while (ancestor.type === "LabeledStatement") { - ancestor = ancestor.parent; - } - - if ( - ancestor.type === "Program" || - (ancestor.type === "BlockStatement" && - astUtils.isFunction(ancestor.parent)) - ) { - const { body } = node; - - if ( - body.type === "ExpressionStatement" && - ((body.expression.type === "Literal" && - typeof body.expression.value === "string") || - astUtils.isStaticTemplateLiteral(body.expression)) - ) { - return false; // potential directive - } - } - return true; - } - - /** - * Removes the top of the stack. - * At the same time, this reports the label if it's never used. - * @param {ASTNode} node A node to report. This is a LabeledStatement. - * @returns {void} - */ - function exitLabeledScope(node) { - if (!scopeInfo.used) { - context.report({ - node: node.label, - messageId: "unused", - data: node.label, - fix: isFixable(node) - ? fixer => - fixer.removeRange([ - node.range[0], - node.body.range[0], - ]) - : null, - }); - } - - scopeInfo = scopeInfo.upper; - } - - /** - * Marks the label of a given node as used. - * @param {ASTNode} node A node to mark. This is a BreakStatement or - * ContinueStatement. - * @returns {void} - */ - function markAsUsed(node) { - if (!node.label) { - return; - } - - const label = node.label.name; - let info = scopeInfo; - - while (info) { - if (info.label === label) { - info.used = true; - break; - } - info = info.upper; - } - } - - return { - LabeledStatement: enterLabeledScope, - "LabeledStatement:exit": exitLabeledScope, - BreakStatement: markAsUsed, - ContinueStatement: markAsUsed, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow unused labels", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-unused-labels" + }, + + schema: [], + + fixable: "code", + + messages: { + unused: "'{{name}}:' is defined but never used." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + let scopeInfo = null; + + /** + * Adds a scope info to the stack. + * @param {ASTNode} node A node to add. This is a LabeledStatement. + * @returns {void} + */ + function enterLabeledScope(node) { + scopeInfo = { + label: node.label.name, + used: false, + upper: scopeInfo + }; + } + + /** + * Checks if a `LabeledStatement` node is fixable. + * For a node to be fixable, there must be no comments between the label and the body. + * Furthermore, is must be possible to remove the label without turning the body statement into a + * directive after other fixes are applied. + * @param {ASTNode} node The node to evaluate. + * @returns {boolean} Whether or not the node is fixable. + */ + function isFixable(node) { + + /* + * Only perform a fix if there are no comments between the label and the body. This will be the case + * when there is exactly one token/comment (the ":") between the label and the body. + */ + if (sourceCode.getTokenAfter(node.label, { includeComments: true }) !== + sourceCode.getTokenBefore(node.body, { includeComments: true })) { + return false; + } + + // Looking for the node's deepest ancestor which is not a `LabeledStatement`. + let ancestor = node.parent; + + while (ancestor.type === "LabeledStatement") { + ancestor = ancestor.parent; + } + + if (ancestor.type === "Program" || + (ancestor.type === "BlockStatement" && astUtils.isFunction(ancestor.parent))) { + const { body } = node; + + if (body.type === "ExpressionStatement" && + ((body.expression.type === "Literal" && typeof body.expression.value === "string") || + astUtils.isStaticTemplateLiteral(body.expression))) { + return false; // potential directive + } + } + return true; + } + + /** + * Removes the top of the stack. + * At the same time, this reports the label if it's never used. + * @param {ASTNode} node A node to report. This is a LabeledStatement. + * @returns {void} + */ + function exitLabeledScope(node) { + if (!scopeInfo.used) { + context.report({ + node: node.label, + messageId: "unused", + data: node.label, + fix: isFixable(node) ? fixer => fixer.removeRange([node.range[0], node.body.range[0]]) : null + }); + } + + scopeInfo = scopeInfo.upper; + } + + /** + * Marks the label of a given node as used. + * @param {ASTNode} node A node to mark. This is a BreakStatement or + * ContinueStatement. + * @returns {void} + */ + function markAsUsed(node) { + if (!node.label) { + return; + } + + const label = node.label.name; + let info = scopeInfo; + + while (info) { + if (info.label === label) { + info.used = true; + break; + } + info = info.upper; + } + } + + return { + LabeledStatement: enterLabeledScope, + "LabeledStatement:exit": exitLabeledScope, + BreakStatement: markAsUsed, + ContinueStatement: markAsUsed + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-unused-private-class-members.js b/node_modules/eslint/lib/rules/no-unused-private-class-members.js index 51029cc49..037be7d3e 100644 --- a/node_modules/eslint/lib/rules/no-unused-private-class-members.js +++ b/node_modules/eslint/lib/rules/no-unused-private-class-members.js @@ -9,211 +9,187 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - docs: { - description: "Disallow unused private class members", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-unused-private-class-members", - }, - - schema: [], - - messages: { - unusedPrivateClassMember: - "'{{classMemberName}}' is defined but never used.", - }, - }, - - create(context) { - const trackedClasses = []; - - /** - * Check whether the current node is in a write only assignment. - * @param {ASTNode} privateIdentifierNode Node referring to a private identifier - * @returns {boolean} Whether the node is in a write only assignment - * @private - */ - function isWriteOnlyAssignment(privateIdentifierNode) { - const parentStatement = privateIdentifierNode.parent.parent; - const isAssignmentExpression = - parentStatement.type === "AssignmentExpression"; - - if ( - !isAssignmentExpression && - parentStatement.type !== "ForInStatement" && - parentStatement.type !== "ForOfStatement" && - parentStatement.type !== "AssignmentPattern" - ) { - return false; - } - - // It is a write-only usage, since we still allow usages on the right for reads - if (parentStatement.left !== privateIdentifierNode.parent) { - return false; - } - - // For any other operator (such as '+=') we still consider it a read operation - if (isAssignmentExpression && parentStatement.operator !== "=") { - /* - * However, if the read operation is "discarded" in an empty statement, then - * we consider it write only. - */ - return parentStatement.parent.type === "ExpressionStatement"; - } - - return true; - } - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - return { - // Collect all declared members up front and assume they are all unused - ClassBody(classBodyNode) { - const privateMembers = new Map(); - - trackedClasses.unshift(privateMembers); - for (const bodyMember of classBodyNode.body) { - if ( - bodyMember.type === "PropertyDefinition" || - bodyMember.type === "MethodDefinition" - ) { - if (bodyMember.key.type === "PrivateIdentifier") { - privateMembers.set(bodyMember.key.name, { - declaredNode: bodyMember, - isAccessor: - bodyMember.type === "MethodDefinition" && - (bodyMember.kind === "set" || - bodyMember.kind === "get"), - }); - } - } - } - }, - - /* - * Process all usages of the private identifier and remove a member from - * `declaredAndUnusedPrivateMembers` if we deem it used. - */ - PrivateIdentifier(privateIdentifierNode) { - const classBody = trackedClasses.find(classProperties => - classProperties.has(privateIdentifierNode.name), - ); - - // Can't happen, as it is a parser to have a missing class body, but let's code defensively here. - if (!classBody) { - return; - } - - // In case any other usage was already detected, we can short circuit the logic here. - const memberDefinition = classBody.get( - privateIdentifierNode.name, - ); - - if (memberDefinition.isUsed) { - return; - } - - // The definition of the class member itself - if ( - privateIdentifierNode.parent.type === - "PropertyDefinition" || - privateIdentifierNode.parent.type === "MethodDefinition" - ) { - return; - } - - /* - * Any usage of an accessor is considered a read, as the getter/setter can have - * side-effects in its definition. - */ - if (memberDefinition.isAccessor) { - memberDefinition.isUsed = true; - return; - } - - // Any assignments to this member, except for assignments that also read - if (isWriteOnlyAssignment(privateIdentifierNode)) { - return; - } - - const wrappingExpressionType = - privateIdentifierNode.parent.parent.type; - const parentOfWrappingExpressionType = - privateIdentifierNode.parent.parent.parent.type; - - // A statement which only increments (`this.#x++;`) - if ( - wrappingExpressionType === "UpdateExpression" && - parentOfWrappingExpressionType === "ExpressionStatement" - ) { - return; - } - - /* - * ({ x: this.#usedInDestructuring } = bar); - * - * But should treat the following as a read: - * ({ [this.#x]: a } = foo); - */ - if ( - wrappingExpressionType === "Property" && - parentOfWrappingExpressionType === "ObjectPattern" && - privateIdentifierNode.parent.parent.value === - privateIdentifierNode.parent - ) { - return; - } - - // [...this.#unusedInRestPattern] = bar; - if (wrappingExpressionType === "RestElement") { - return; - } - - // [this.#unusedInAssignmentPattern] = bar; - if (wrappingExpressionType === "ArrayPattern") { - return; - } - - /* - * We can't delete the memberDefinition, as we need to keep track of which member we are marking as used. - * In the case of nested classes, we only mark the first member we encounter as used. If you were to delete - * the member, then any subsequent usage could incorrectly mark the member of an encapsulating parent class - * as used, which is incorrect. - */ - memberDefinition.isUsed = true; - }, - - /* - * Post-process the class members and report any remaining members. - * Since private members can only be accessed in the current class context, - * we can safely assume that all usages are within the current class body. - */ - "ClassBody:exit"() { - const unusedPrivateMembers = trackedClasses.shift(); - - for (const [ - classMemberName, - { declaredNode, isUsed }, - ] of unusedPrivateMembers.entries()) { - if (isUsed) { - continue; - } - context.report({ - node: declaredNode, - loc: declaredNode.key.loc, - messageId: "unusedPrivateClassMember", - data: { - classMemberName: `#${classMemberName}`, - }, - }); - } - }, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow unused private class members", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-unused-private-class-members" + }, + + schema: [], + + messages: { + unusedPrivateClassMember: "'{{classMemberName}}' is defined but never used." + } + }, + + create(context) { + const trackedClasses = []; + + /** + * Check whether the current node is in a write only assignment. + * @param {ASTNode} privateIdentifierNode Node referring to a private identifier + * @returns {boolean} Whether the node is in a write only assignment + * @private + */ + function isWriteOnlyAssignment(privateIdentifierNode) { + const parentStatement = privateIdentifierNode.parent.parent; + const isAssignmentExpression = parentStatement.type === "AssignmentExpression"; + + if (!isAssignmentExpression && + parentStatement.type !== "ForInStatement" && + parentStatement.type !== "ForOfStatement" && + parentStatement.type !== "AssignmentPattern") { + return false; + } + + // It is a write-only usage, since we still allow usages on the right for reads + if (parentStatement.left !== privateIdentifierNode.parent) { + return false; + } + + // For any other operator (such as '+=') we still consider it a read operation + if (isAssignmentExpression && parentStatement.operator !== "=") { + + /* + * However, if the read operation is "discarded" in an empty statement, then + * we consider it write only. + */ + return parentStatement.parent.type === "ExpressionStatement"; + } + + return true; + } + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + return { + + // Collect all declared members up front and assume they are all unused + ClassBody(classBodyNode) { + const privateMembers = new Map(); + + trackedClasses.unshift(privateMembers); + for (const bodyMember of classBodyNode.body) { + if (bodyMember.type === "PropertyDefinition" || bodyMember.type === "MethodDefinition") { + if (bodyMember.key.type === "PrivateIdentifier") { + privateMembers.set(bodyMember.key.name, { + declaredNode: bodyMember, + isAccessor: bodyMember.type === "MethodDefinition" && + (bodyMember.kind === "set" || bodyMember.kind === "get") + }); + } + } + } + }, + + /* + * Process all usages of the private identifier and remove a member from + * `declaredAndUnusedPrivateMembers` if we deem it used. + */ + PrivateIdentifier(privateIdentifierNode) { + const classBody = trackedClasses.find(classProperties => classProperties.has(privateIdentifierNode.name)); + + // Can't happen, as it is a parser to have a missing class body, but let's code defensively here. + if (!classBody) { + return; + } + + // In case any other usage was already detected, we can short circuit the logic here. + const memberDefinition = classBody.get(privateIdentifierNode.name); + + if (memberDefinition.isUsed) { + return; + } + + // The definition of the class member itself + if (privateIdentifierNode.parent.type === "PropertyDefinition" || + privateIdentifierNode.parent.type === "MethodDefinition") { + return; + } + + /* + * Any usage of an accessor is considered a read, as the getter/setter can have + * side-effects in its definition. + */ + if (memberDefinition.isAccessor) { + memberDefinition.isUsed = true; + return; + } + + // Any assignments to this member, except for assignments that also read + if (isWriteOnlyAssignment(privateIdentifierNode)) { + return; + } + + const wrappingExpressionType = privateIdentifierNode.parent.parent.type; + const parentOfWrappingExpressionType = privateIdentifierNode.parent.parent.parent.type; + + // A statement which only increments (`this.#x++;`) + if (wrappingExpressionType === "UpdateExpression" && + parentOfWrappingExpressionType === "ExpressionStatement") { + return; + } + + /* + * ({ x: this.#usedInDestructuring } = bar); + * + * But should treat the following as a read: + * ({ [this.#x]: a } = foo); + */ + if (wrappingExpressionType === "Property" && + parentOfWrappingExpressionType === "ObjectPattern" && + privateIdentifierNode.parent.parent.value === privateIdentifierNode.parent) { + return; + } + + // [...this.#unusedInRestPattern] = bar; + if (wrappingExpressionType === "RestElement") { + return; + } + + // [this.#unusedInAssignmentPattern] = bar; + if (wrappingExpressionType === "ArrayPattern") { + return; + } + + /* + * We can't delete the memberDefinition, as we need to keep track of which member we are marking as used. + * In the case of nested classes, we only mark the first member we encounter as used. If you were to delete + * the member, then any subsequent usage could incorrectly mark the member of an encapsulating parent class + * as used, which is incorrect. + */ + memberDefinition.isUsed = true; + }, + + /* + * Post-process the class members and report any remaining members. + * Since private members can only be accessed in the current class context, + * we can safely assume that all usages are within the current class body. + */ + "ClassBody:exit"() { + const unusedPrivateMembers = trackedClasses.shift(); + + for (const [classMemberName, { declaredNode, isUsed }] of unusedPrivateMembers.entries()) { + if (isUsed) { + continue; + } + context.report({ + node: declaredNode, + loc: declaredNode.key.loc, + messageId: "unusedPrivateClassMember", + data: { + classMemberName: `#${classMemberName}` + } + }); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-unused-vars.js b/node_modules/eslint/lib/rules/no-unused-vars.js index da5361fa7..f29e678d6 100644 --- a/node_modules/eslint/lib/rules/no-unused-vars.js +++ b/node_modules/eslint/lib/rules/no-unused-vars.js @@ -15,11 +15,6 @@ const astUtils = require("./utils/ast-utils"); // Typedefs //------------------------------------------------------------------------------ -/** - * A simple name for the types of variables that this rule supports - * @typedef {'array-destructure'|'catch-clause'|'parameter'|'variable'} VariableType - */ - /** * Bag of data used for formatting the `unusedVar` lint message. * @typedef {Object} UnusedVarMessageData @@ -28,1712 +23,696 @@ const astUtils = require("./utils/ast-utils"); * @property {string} additional Any additional info to be appended at the end. */ -/** - * Bag of data used for formatting the `usedIgnoredVar` lint message. - * @typedef {Object} UsedIgnoredVarMessageData - * @property {string} varName The name of the unused var. - * @property {string} additional Any additional info to be appended at the end. - */ - //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - docs: { - description: "Disallow unused variables", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-unused-vars", - }, - - hasSuggestions: true, - - schema: [ - { - oneOf: [ - { - enum: ["all", "local"], - }, - { - type: "object", - properties: { - vars: { - enum: ["all", "local"], - }, - varsIgnorePattern: { - type: "string", - }, - args: { - enum: ["all", "after-used", "none"], - }, - ignoreRestSiblings: { - type: "boolean", - }, - argsIgnorePattern: { - type: "string", - }, - caughtErrors: { - enum: ["all", "none"], - }, - caughtErrorsIgnorePattern: { - type: "string", - }, - destructuredArrayIgnorePattern: { - type: "string", - }, - ignoreClassWithStaticInitBlock: { - type: "boolean", - }, - ignoreUsingDeclarations: { - type: "boolean", - }, - reportUsedIgnorePattern: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - }, - ], - - messages: { - unusedVar: - "'{{varName}}' is {{action}} but never used{{additional}}.", - usedIgnoredVar: - "'{{varName}}' is marked as ignored but is used{{additional}}.", - removeVar: "Remove unused variable '{{varName}}'.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - const REST_PROPERTY_TYPE = - /^(?:RestElement|(?:Experimental)?RestProperty)$/u; - - const config = { - vars: "all", - args: "after-used", - ignoreRestSiblings: false, - caughtErrors: "all", - ignoreClassWithStaticInitBlock: false, - ignoreUsingDeclarations: false, - reportUsedIgnorePattern: false, - }; - - const firstOption = context.options[0]; - - if (firstOption) { - if (typeof firstOption === "string") { - config.vars = firstOption; - } else { - config.vars = firstOption.vars || config.vars; - config.args = firstOption.args || config.args; - config.ignoreRestSiblings = - firstOption.ignoreRestSiblings || config.ignoreRestSiblings; - config.caughtErrors = - firstOption.caughtErrors || config.caughtErrors; - config.ignoreClassWithStaticInitBlock = - firstOption.ignoreClassWithStaticInitBlock || - config.ignoreClassWithStaticInitBlock; - config.ignoreUsingDeclarations = - firstOption.ignoreUsingDeclarations || - config.ignoreUsingDeclarations; - config.reportUsedIgnorePattern = - firstOption.reportUsedIgnorePattern || - config.reportUsedIgnorePattern; - - if (firstOption.varsIgnorePattern) { - config.varsIgnorePattern = new RegExp( - firstOption.varsIgnorePattern, - "u", - ); - } - - if (firstOption.argsIgnorePattern) { - config.argsIgnorePattern = new RegExp( - firstOption.argsIgnorePattern, - "u", - ); - } - - if (firstOption.caughtErrorsIgnorePattern) { - config.caughtErrorsIgnorePattern = new RegExp( - firstOption.caughtErrorsIgnorePattern, - "u", - ); - } - - if (firstOption.destructuredArrayIgnorePattern) { - config.destructuredArrayIgnorePattern = new RegExp( - firstOption.destructuredArrayIgnorePattern, - "u", - ); - } - } - } - - /** - * Determines what variable type a def is. - * @param {Object} def the declaration to check - * @returns {VariableType} a simple name for the types of variables that this rule supports - */ - function defToVariableType(def) { - /* - * This `destructuredArrayIgnorePattern` error report works differently from the catch - * clause and parameter error reports. _Both_ the `varsIgnorePattern` and the - * `destructuredArrayIgnorePattern` will be checked for array destructuring. However, - * for the purposes of the report, the currently defined behavior is to only inform the - * user of the `destructuredArrayIgnorePattern` if it's present (regardless of the fact - * that the `varsIgnorePattern` would also apply). If it's not present, the user will be - * informed of the `varsIgnorePattern`, assuming that's present. - */ - if ( - config.destructuredArrayIgnorePattern && - def.name.parent.type === "ArrayPattern" - ) { - return "array-destructure"; - } - - switch (def.type) { - case "CatchClause": - return "catch-clause"; - case "Parameter": - return "parameter"; - - default: - return "variable"; - } - } - - /** - * Gets a given variable's description and configured ignore pattern - * based on the provided variableType - * @param {VariableType} variableType a simple name for the types of variables that this rule supports - * @throws {Error} (Unreachable) - * @returns {[string | undefined, string | undefined]} the given variable's description and - * ignore pattern - */ - function getVariableDescription(variableType) { - let pattern; - let variableDescription; - - switch (variableType) { - case "array-destructure": - pattern = config.destructuredArrayIgnorePattern; - variableDescription = "elements of array destructuring"; - break; - - case "catch-clause": - pattern = config.caughtErrorsIgnorePattern; - variableDescription = "caught errors"; - break; - - case "parameter": - pattern = config.argsIgnorePattern; - variableDescription = "args"; - break; - - case "variable": - pattern = config.varsIgnorePattern; - variableDescription = "vars"; - break; - - default: - throw new Error( - `Unexpected variable type: ${variableType}`, - ); - } - - if (pattern) { - pattern = pattern.toString(); - } - - return [variableDescription, pattern]; - } - - /** - * Generates the message data about the variable being defined and unused, - * including the ignore pattern if configured. - * @param {Variable} unusedVar eslint-scope variable object. - * @returns {UnusedVarMessageData} The message data to be used with this unused variable. - */ - function getDefinedMessageData(unusedVar) { - const def = unusedVar.defs && unusedVar.defs[0]; - let additionalMessageData = ""; - - if (def) { - const [variableDescription, pattern] = getVariableDescription( - defToVariableType(def), - ); - - if (pattern && variableDescription) { - additionalMessageData = `. Allowed unused ${variableDescription} must match ${pattern}`; - } - } - - return { - varName: unusedVar.name, - action: "defined", - additional: additionalMessageData, - }; - } - - /** - * Generate the warning message about the variable being - * assigned and unused, including the ignore pattern if configured. - * @param {Variable} unusedVar eslint-scope variable object. - * @returns {UnusedVarMessageData} The message data to be used with this unused variable. - */ - function getAssignedMessageData(unusedVar) { - const def = unusedVar.defs && unusedVar.defs[0]; - let additionalMessageData = ""; - - if (def) { - const [variableDescription, pattern] = getVariableDescription( - defToVariableType(def), - ); - - if (pattern && variableDescription) { - additionalMessageData = `. Allowed unused ${variableDescription} must match ${pattern}`; - } - } - - return { - varName: unusedVar.name, - action: "assigned a value", - additional: additionalMessageData, - }; - } - - /** - * Generate the warning message about a variable being used even though - * it is marked as being ignored. - * @param {Variable} variable eslint-scope variable object - * @param {VariableType} variableType a simple name for the types of variables that this rule supports - * @returns {UsedIgnoredVarMessageData} The message data to be used with - * this used ignored variable. - */ - function getUsedIgnoredMessageData(variable, variableType) { - const [variableDescription, pattern] = - getVariableDescription(variableType); - - let additionalMessageData = ""; - - if (pattern && variableDescription) { - additionalMessageData = `. Used ${variableDescription} must not match ${pattern}`; - } - - return { - varName: variable.name, - additional: additionalMessageData, - }; - } - - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - const STATEMENT_TYPE = /(?:Statement|Declaration)$/u; - - /** - * Determines if a given variable is being exported from a module. - * @param {Variable} variable eslint-scope variable object. - * @returns {boolean} True if the variable is exported, false if not. - * @private - */ - function isExported(variable) { - const definition = variable.defs[0]; - - if (definition) { - let node = definition.node; - - if (node.type === "VariableDeclarator") { - node = node.parent; - } else if (definition.type === "Parameter") { - return false; - } - - return node.parent.type.indexOf("Export") === 0; - } - return false; - } - - /** - * Determines if a given variable uses the explicit resource management protocol. - * @param {Variable} variable eslint-scope variable object. - * @returns {boolean} True if the variable is declared with "using" or "await using" - * @private - */ - function usesExplicitResourceManagement(variable) { - const [definition] = variable.defs; - - return ( - definition?.type === "Variable" && - (definition.parent.kind === "using" || - definition.parent.kind === "await using") - ); - } - - /** - * Checks whether a node is a sibling of the rest property or not. - * @param {ASTNode} node a node to check - * @returns {boolean} True if the node is a sibling of the rest property, otherwise false. - */ - function hasRestSibling(node) { - return ( - node.type === "Property" && - node.parent.type === "ObjectPattern" && - REST_PROPERTY_TYPE.test(node.parent.properties.at(-1).type) - ); - } - - /** - * Determines if a variable has a sibling rest property - * @param {Variable} variable eslint-scope variable object. - * @returns {boolean} True if the variable has a sibling rest property, false if not. - * @private - */ - function hasRestSpreadSibling(variable) { - if (config.ignoreRestSiblings) { - const hasRestSiblingDefinition = variable.defs.some(def => - hasRestSibling(def.name.parent), - ); - const hasRestSiblingReference = variable.references.some(ref => - hasRestSibling(ref.identifier.parent), - ); - - return hasRestSiblingDefinition || hasRestSiblingReference; - } - - return false; - } - - /** - * Determines if a reference is a read operation. - * @param {Reference} ref An eslint-scope Reference - * @returns {boolean} whether the given reference represents a read operation - * @private - */ - function isReadRef(ref) { - return ref.isRead(); - } - - /** - * Determine if an identifier is referencing an enclosing function name. - * @param {Reference} ref The reference to check. - * @param {ASTNode[]} nodes The candidate function nodes. - * @returns {boolean} True if it's a self-reference, false if not. - * @private - */ - function isSelfReference(ref, nodes) { - let scope = ref.from; - - while (scope) { - if (nodes.includes(scope.block)) { - return true; - } - - scope = scope.upper; - } - - return false; - } - - /** - * Gets a list of function definitions for a specified variable. - * @param {Variable} variable eslint-scope variable object. - * @returns {ASTNode[]} Function nodes. - * @private - */ - function getFunctionDefinitions(variable) { - const functionDefinitions = []; - - variable.defs.forEach(def => { - const { type, node } = def; - - // FunctionDeclarations - if (type === "FunctionName") { - functionDefinitions.push(node); - } - - // FunctionExpressions - if ( - type === "Variable" && - node.init && - (node.init.type === "FunctionExpression" || - node.init.type === "ArrowFunctionExpression") - ) { - functionDefinitions.push(node.init); - } - }); - return functionDefinitions; - } - - /** - * Checks the position of given nodes. - * @param {ASTNode} inner A node which is expected as inside. - * @param {ASTNode} outer A node which is expected as outside. - * @returns {boolean} `true` if the `inner` node exists in the `outer` node. - * @private - */ - function isInside(inner, outer) { - return ( - inner.range[0] >= outer.range[0] && - inner.range[1] <= outer.range[1] - ); - } - - /** - * Checks whether a given node is unused expression or not. - * @param {ASTNode} node The node itself - * @returns {boolean} The node is an unused expression. - * @private - */ - function isUnusedExpression(node) { - const parent = node.parent; - - if (parent.type === "ExpressionStatement") { - return true; - } - - if (parent.type === "SequenceExpression") { - const isLastExpression = parent.expressions.at(-1) === node; - - if (!isLastExpression) { - return true; - } - return isUnusedExpression(parent); - } - - return false; - } - - /** - * If a given reference is left-hand side of an assignment, this gets - * the right-hand side node of the assignment. - * - * In the following cases, this returns null. - * - * - The reference is not the LHS of an assignment expression. - * - The reference is inside of a loop. - * - The reference is inside of a function scope which is different from - * the declaration. - * @param {eslint-scope.Reference} ref A reference to check. - * @param {ASTNode} prevRhsNode The previous RHS node. - * @returns {ASTNode|null} The RHS node or null. - * @private - */ - function getRhsNode(ref, prevRhsNode) { - const id = ref.identifier; - const parent = id.parent; - const refScope = ref.from.variableScope; - const varScope = ref.resolved.scope.variableScope; - const canBeUsedLater = - refScope !== varScope || astUtils.isInLoop(id); - - /* - * Inherits the previous node if this reference is in the node. - * This is for `a = a + a`-like code. - */ - if (prevRhsNode && isInside(id, prevRhsNode)) { - return prevRhsNode; - } - - if ( - parent.type === "AssignmentExpression" && - isUnusedExpression(parent) && - id === parent.left && - !canBeUsedLater - ) { - return parent.right; - } - return null; - } - - /** - * Checks whether a given function node is stored to somewhere or not. - * If the function node is stored, the function can be used later. - * @param {ASTNode} funcNode A function node to check. - * @param {ASTNode} rhsNode The RHS node of the previous assignment. - * @returns {boolean} `true` if under the following conditions: - * - the funcNode is assigned to a variable. - * - the funcNode is bound as an argument of a function call. - * - the function is bound to a property and the object satisfies above conditions. - * @private - */ - function isStorableFunction(funcNode, rhsNode) { - let node = funcNode; - let parent = funcNode.parent; - - while (parent && isInside(parent, rhsNode)) { - switch (parent.type) { - case "SequenceExpression": - if (parent.expressions.at(-1) !== node) { - return false; - } - break; - - case "CallExpression": - case "NewExpression": - return parent.callee !== node; - - case "AssignmentExpression": - case "TaggedTemplateExpression": - case "YieldExpression": - return true; - - default: - if (STATEMENT_TYPE.test(parent.type)) { - /* - * If it encountered statements, this is a complex pattern. - * Since analyzing complex patterns is hard, this returns `true` to avoid false positive. - */ - return true; - } - } - - node = parent; - parent = parent.parent; - } - - return false; - } - - /** - * Checks whether a given Identifier node exists inside of a function node which can be used later. - * - * "can be used later" means: - * - the function is assigned to a variable. - * - the function is bound to a property and the object can be used later. - * - the function is bound as an argument of a function call. - * - * If a reference exists in a function which can be used later, the reference is read when the function is called. - * @param {ASTNode} id An Identifier node to check. - * @param {ASTNode} rhsNode The RHS node of the previous assignment. - * @returns {boolean} `true` if the `id` node exists inside of a function node which can be used later. - * @private - */ - function isInsideOfStorableFunction(id, rhsNode) { - const funcNode = astUtils.getUpperFunction(id); - - return ( - funcNode && - isInside(funcNode, rhsNode) && - isStorableFunction(funcNode, rhsNode) - ); - } - - /** - * Checks whether a given reference is a read to update itself or not. - * @param {eslint-scope.Reference} ref A reference to check. - * @param {ASTNode} rhsNode The RHS node of the previous assignment. - * @returns {boolean} The reference is a read to update itself. - * @private - */ - function isReadForItself(ref, rhsNode) { - const id = ref.identifier; - const parent = id.parent; - - return ( - ref.isRead() && - // self update. e.g. `a += 1`, `a++` - ((parent.type === "AssignmentExpression" && - parent.left === id && - isUnusedExpression(parent) && - !astUtils.isLogicalAssignmentOperator(parent.operator)) || - (parent.type === "UpdateExpression" && - isUnusedExpression(parent)) || - // in RHS of an assignment for itself. e.g. `a = a + 1` - (rhsNode && - isInside(id, rhsNode) && - !isInsideOfStorableFunction(id, rhsNode))) - ); - } - - /** - * Determine if an identifier is used either in for-in or for-of loops. - * @param {Reference} ref The reference to check. - * @returns {boolean} whether reference is used in the for-in loops - * @private - */ - function isForInOfRef(ref) { - let target = ref.identifier.parent; - - // "for (var ...) { return; }" - if (target.type === "VariableDeclarator") { - target = target.parent.parent; - } - - if ( - target.type !== "ForInStatement" && - target.type !== "ForOfStatement" - ) { - return false; - } - - // "for (...) { return; }" - if (target.body.type === "BlockStatement") { - target = target.body.body[0]; - - // "for (...) return;" - } else { - target = target.body; - } - - // For empty loop body - if (!target) { - return false; - } - - return target.type === "ReturnStatement"; - } - - /** - * Determines if the variable is used. - * @param {Variable} variable The variable to check. - * @returns {boolean} True if the variable is used - * @private - */ - function isUsedVariable(variable) { - if (variable.eslintUsed) { - return true; - } - - const functionNodes = getFunctionDefinitions(variable); - const isFunctionDefinition = functionNodes.length > 0; - - let rhsNode = null; - - return variable.references.some(ref => { - if (isForInOfRef(ref)) { - return true; - } - - const forItself = isReadForItself(ref, rhsNode); - - rhsNode = getRhsNode(ref, rhsNode); - - return ( - isReadRef(ref) && - !forItself && - !( - isFunctionDefinition && - isSelfReference(ref, functionNodes) - ) - ); - }); - } - - /** - * Checks whether the given variable is after the last used parameter. - * @param {eslint-scope.Variable} variable The variable to check. - * @returns {boolean} `true` if the variable is defined after the last - * used parameter. - */ - function isAfterLastUsedArg(variable) { - const def = variable.defs[0]; - const params = sourceCode.getDeclaredVariables(def.node); - const posteriorParams = params.slice(params.indexOf(variable) + 1); - - // If any used parameters occur after this parameter, do not report. - return !posteriorParams.some( - v => v.references.length > 0 || v.eslintUsed, - ); - } - - /** - * Gets an array of variables without read references. - * @param {Scope} scope an eslint-scope Scope object. - * @param {Variable[]} unusedVars an array that saving result. - * @returns {Variable[]} unused variables of the scope and descendant scopes. - * @private - */ - function collectUnusedVariables(scope, unusedVars) { - const variables = scope.variables; - const childScopes = scope.childScopes; - let i, l; - - if (scope.type !== "global" || config.vars === "all") { - for (i = 0, l = variables.length; i < l; ++i) { - const variable = variables[i]; - - // skip a variable of class itself name in the class scope - if ( - scope.type === "class" && - scope.block.id === variable.identifiers[0] - ) { - continue; - } - - // skip function expression names - if (scope.functionExpressionScope) { - continue; - } - - // skip variables marked with markVariableAsUsed() - if ( - !config.reportUsedIgnorePattern && - variable.eslintUsed - ) { - continue; - } - - // skip implicit "arguments" variable - if ( - scope.type === "function" && - variable.name === "arguments" && - variable.identifiers.length === 0 - ) { - continue; - } - - // explicit global variables don't have definitions. - const def = variable.defs[0]; - - if (def) { - const type = def.type; - const refUsedInArrayPatterns = variable.references.some( - ref => - ref.identifier.parent.type === "ArrayPattern", - ); - - // skip elements of array destructuring patterns - if ( - (def.name.parent.type === "ArrayPattern" || - refUsedInArrayPatterns) && - config.destructuredArrayIgnorePattern && - config.destructuredArrayIgnorePattern.test( - def.name.name, - ) - ) { - if ( - config.reportUsedIgnorePattern && - isUsedVariable(variable) - ) { - context.report({ - node: def.name, - messageId: "usedIgnoredVar", - data: getUsedIgnoredMessageData( - variable, - "array-destructure", - ), - }); - } - - continue; - } - - if (type === "ClassName") { - const hasStaticBlock = def.node.body.body.some( - node => node.type === "StaticBlock", - ); - - if ( - config.ignoreClassWithStaticInitBlock && - hasStaticBlock - ) { - continue; - } - } - - // skip catch variables - if (type === "CatchClause") { - if (config.caughtErrors === "none") { - continue; - } - - // skip ignored parameters - if ( - config.caughtErrorsIgnorePattern && - config.caughtErrorsIgnorePattern.test( - def.name.name, - ) - ) { - if ( - config.reportUsedIgnorePattern && - isUsedVariable(variable) - ) { - context.report({ - node: def.name, - messageId: "usedIgnoredVar", - data: getUsedIgnoredMessageData( - variable, - "catch-clause", - ), - }); - } - - continue; - } - } else if (type === "Parameter") { - // skip any setter argument - if ( - (def.node.parent.type === "Property" || - def.node.parent.type === - "MethodDefinition") && - def.node.parent.kind === "set" - ) { - continue; - } - - // if "args" option is "none", skip any parameter - if (config.args === "none") { - continue; - } - - // skip ignored parameters - if ( - config.argsIgnorePattern && - config.argsIgnorePattern.test(def.name.name) - ) { - if ( - config.reportUsedIgnorePattern && - isUsedVariable(variable) - ) { - context.report({ - node: def.name, - messageId: "usedIgnoredVar", - data: getUsedIgnoredMessageData( - variable, - "parameter", - ), - }); - } - - continue; - } - - // if "args" option is "after-used", skip used variables - if ( - config.args === "after-used" && - astUtils.isFunction(def.name.parent) && - !isAfterLastUsedArg(variable) - ) { - continue; - } - } else { - // skip ignored variables - if ( - config.varsIgnorePattern && - config.varsIgnorePattern.test(def.name.name) - ) { - if ( - config.reportUsedIgnorePattern && - isUsedVariable(variable) - ) { - context.report({ - node: def.name, - messageId: "usedIgnoredVar", - data: getUsedIgnoredMessageData( - variable, - "variable", - ), - }); - } - - continue; - } - } - } - - if ( - !isUsedVariable(variable) && - !isExported(variable) && - !( - config.ignoreUsingDeclarations && - usesExplicitResourceManagement(variable) - ) && - !hasRestSpreadSibling(variable) - ) { - unusedVars.push(variable); - } - } - } - - for (i = 0, l = childScopes.length; i < l; ++i) { - collectUnusedVariables(childScopes[i], unusedVars); - } - - return unusedVars; - } - - /** - * fixes unused variables - * @param {Object} fixer fixer object - * @param {Object} unusedVar unused variable to fix - * @returns {Object} fixer object - */ - function handleFixes(fixer, unusedVar) { - const id = unusedVar.identifiers[0]; - const parent = id.parent; - const parentType = parent.type; - const tokenBefore = sourceCode.getTokenBefore(id); - const tokenAfter = sourceCode.getTokenAfter(id); - const isFunction = astUtils.isFunction; - const isLoop = astUtils.isLoop; - const allWriteReferences = unusedVar.references.filter(ref => - ref.isWrite(), - ); - - /** - * get range from token before of a given node - * @param {ASTNode} node node of identifier - * @param {number} skips number of token to skip - * @returns {number} start range of token before the identifier - */ - function getPreviousTokenStart(node, skips) { - return sourceCode.getTokenBefore(node, skips).range[0]; - } - - /** - * get range to token after of a given node - * @param {ASTNode} node node of identifier - * @param {number} skips number of token to skip - * @returns {number} end range of token after the identifier - */ - function getNextTokenEnd(node, skips) { - return sourceCode.getTokenAfter(node, skips).range[1]; - } - - /** - * get the value of token before of a given node - * @param {ASTNode} node node of identifier - * @returns {string} value of token before the identifier - */ - function getTokenBeforeValue(node) { - return sourceCode.getTokenBefore(node).value; - } - - /** - * get the value of token after of a given node - * @param {ASTNode} node node of identifier - * @returns {string} value of token after the identifier - */ - function getTokenAfterValue(node) { - return sourceCode.getTokenAfter(node).value; - } - - /** - * Check if an array has a single element with null as other element. - * @param {ASTNode} node ArrayPattern node - * @returns {boolean} true if array has single element with other null elements - */ - function hasSingleElement(node) { - return node.elements.filter(e => e !== null).length === 1; - } - - /** - * check whether import specifier has an import of particular type - * @param {ASTNode} node ImportDeclaration node - * @param {string} type type of import to check - * @returns {boolean} true if import specifier has import of specified type - */ - function hasImportOfCertainType(node, type) { - return node.specifiers.some(e => e.type === type); - } - - /** - * Check whether declaration is safe to remove or not - * @param {ASTNode} nextToken next token of unused variable - * @param {ASTNode} prevToken previous token of unused variable - * @returns {boolean} true if declaration is not safe to remove - */ - function isDeclarationNotSafeToRemove(nextToken, prevToken) { - return ( - nextToken.type === "String" || - (prevToken && - !astUtils.isSemicolonToken(prevToken) && - !astUtils.isOpeningBraceToken(prevToken)) - ); - } - - /** - * give fixes for unused variables in function parameters - * @param {ASTNode} node node to check - * @returns {Object} fixer object - */ - function fixFunctionParameters(node) { - const parentNode = node.parent; - - if (isFunction(parentNode)) { - // remove unused function parameter if there is only a single parameter - if (parentNode.params.length === 1) { - return fixer.removeRange(node.range); - } - - // remove first unused function parameter when there are multiple parameters - if ( - getTokenBeforeValue(node) === "(" && - getTokenAfterValue(node) === "," - ) { - return fixer.removeRange([ - node.range[0], - getNextTokenEnd(node), - ]); - } - - // remove unused function parameters except first one when there are multiple parameters - return fixer.removeRange([ - getPreviousTokenStart(node), - node.range[1], - ]); - } - - return null; - } - - /** - * fix unused variable declarations and function parameters - * @param {ASTNode} node parent node to identifier - * @returns {Object} fixer object - */ - function fixVariables(node) { - const parentNode = node.parent; - - // remove unused declared variables such as var a = b; or var a = b, c; - if (parentNode.type === "VariableDeclarator") { - // skip variable in for (const [ foo ] of bar); - if (isLoop(parentNode.parent.parent)) { - return null; - } - - /* - * remove unused declared variable with single declaration such as 'var a = b;' - * remove complete declaration when there is an unused variable in 'const { a } = foo;', same for arrays. - */ - if (parentNode.parent.declarations.length === 1) { - // if next token is a string it could become a directive if node is removed -> no suggestion. - const nextToken = sourceCode.getTokenAfter( - parentNode.parent, - ); - - // if previous token exists and is not ";" or "{" not sure about ASI rules -> no suggestion. - const prevToken = sourceCode.getTokenBefore( - parentNode.parent, - ); - - if ( - nextToken && - isDeclarationNotSafeToRemove(nextToken, prevToken) - ) { - return null; - } - - return fixer.removeRange(parentNode.parent.range); - } - - /* - * remove unused declared variable with multiple declaration except first one such as 'var a = b, c = d;' - * fix 'let bar = "hello", { a } = foo;' to 'let bar = "hello";' if 'a' is unused, same for arrays. - */ - if (getTokenBeforeValue(parentNode) === ",") { - return fixer.removeRange([ - getPreviousTokenStart(parentNode), - parentNode.range[1], - ]); - } - - /* - * remove first unused declared variable when there are multiple declarations - * fix 'let { a } = foo, bar = "hello";' to 'let bar = "hello";' if 'a' is unused, same for arrays. - */ - return fixer.removeRange([ - parentNode.range[0], - getNextTokenEnd(parentNode), - ]); - } - - // fixes [{a: {k}}], [{a: [k]}] - if (getTokenBeforeValue(node) === ":") { - if (parentNode.parent.type === "ObjectPattern") { - // eslint-disable-next-line no-use-before-define -- due to interdependency of functions - return fixObjectWithValueSeparator(node); - } - } - - // fix unused function parameters - return fixFunctionParameters(node); - } - - /** - * fix nested object like { a: { b } } - * @param {ASTNode} node parent node to check - * @returns {Object} fixer object - */ - function fixNestedObjectVariable(node) { - const parentNode = node.parent; - - // fix for { a: { b: { c: { d } } } } - if ( - parentNode.parent.parent.parent.type === "ObjectPattern" && - parentNode.parent.properties.length === 1 - ) { - return fixNestedObjectVariable(parentNode.parent); - } - - // fix for { a: { b } } - if (parentNode.parent.type === "ObjectPattern") { - // fix for unused variables in destructured object with single property in variable declaration and function parameter - if (parentNode.parent.properties.length === 1) { - return fixVariables(parentNode.parent); - } - - // fix for first unused property when there are multiple properties such as '{ a: { b }, c }' - if (getTokenBeforeValue(parentNode) === "{") { - return fixer.removeRange([ - parentNode.range[0], - getNextTokenEnd(parentNode), - ]); - } - - // fix for unused property except first one when there are multiple properties such as '{ k, a: { b } }' - return fixer.removeRange([ - getPreviousTokenStart(parentNode), - parentNode.range[1], - ]); - } - - return null; - } - - /** - * fix unused variables in array and nested array - * @param {ASTNode} node parent node to check - * @returns {Object} fixer object - */ - function fixNestedArrayVariable(node) { - const parentNode = node.parent; - - // fix for nested arrays [[ a ]] - if ( - parentNode.parent.type === "ArrayPattern" && - hasSingleElement(parentNode) - ) { - return fixNestedArrayVariable(parentNode); - } - - if (hasSingleElement(parentNode)) { - // fixes { a: [{ b }] } or { a: [[ b ]] } - if (getTokenBeforeValue(parentNode) === ":") { - return fixVariables(parentNode); - } - - // fixes [a, ...[[ b ]]] or [a, ...[{ b }]] - if (parentNode.parent.type === "RestElement") { - // eslint-disable-next-line no-use-before-define -- due to interdependency of functions - return fixRestInPattern(parentNode.parent); - } - - // fix unused variables in destructured array in variable declaration or function parameter - return fixVariables(parentNode); - } - - // remove last unused array element - if ( - getTokenBeforeValue(node) === "," && - getTokenAfterValue(node) === "]" - ) { - return fixer.removeRange([ - getPreviousTokenStart(node), - node.range[1], - ]); - } - - // remove unused array element - return fixer.removeRange(node.range); - } - - /** - * fix cases like {a: {k}} or {a: [k]} - * @param {ASTNode} node parent node to check - * @returns {Object} fixer object - */ - function fixObjectWithValueSeparator(node) { - const parentNode = node.parent.parent; - - // fix cases like [{a : { b }}] or [{a : [ b ]}] - if ( - parentNode.parent.type === "ArrayPattern" && - parentNode.properties.length === 1 - ) { - return fixNestedArrayVariable(parentNode); - } - - // fix cases like {a: {k}} or {a: [k]} - return fixNestedObjectVariable(node); - } - - /** - * fix ...[[a]] or ...[{a}] like patterns - * @param {ASTNode} node parent node to check - * @returns {Object} fixer object - */ - function fixRestInPattern(node) { - const parentNode = node.parent; - - // fix ...[[a]] or ...[{a}] in function parameters - if (isFunction(parentNode)) { - if (parentNode.params.length === 1) { - return fixer.removeRange(node.range); - } - - return fixer.removeRange([ - getPreviousTokenStart(node), - node.range[1], - ]); - } - - // fix rest in nested array pattern like [[a, ...[b]]] - if (parentNode.type === "ArrayPattern") { - // fix [[...[b]]] - if (hasSingleElement(parentNode)) { - if (parentNode.parent.type === "ArrayPattern") { - return fixNestedArrayVariable(parentNode); - } - - // fix 'const [...[b]] = foo; and function foo([...[b]]) {} - return fixVariables(parentNode); - } - - // fix [[a, ...[b]]] - return fixer.removeRange([ - getPreviousTokenStart(node), - node.range[1], - ]); - } - - return null; - } - - // skip fix when variable has references that would be left behind - if ( - allWriteReferences.some( - ref => ref.identifier.range[0] !== id.range[0], - ) - ) { - return null; - } - - // remove declared variables such as var a; or var a, b; - if (parentType === "VariableDeclarator") { - if (parent.parent.declarations.length === 1) { - // prevent fix of variable in forOf and forIn loops. - if ( - isLoop(parent.parent.parent) && - parent.parent.parent.body !== parent.parent - ) { - return null; - } - - // removes only variable not semicolon in 'if (foo()) var bar;' or in 'loops' or in 'with' statement. - if ( - parent.parent.parent.type === "IfStatement" || - isLoop(parent.parent.parent) || - (parent.parent.parent.type === "WithStatement" && - parent.parent.parent.body === parent.parent) - ) { - return fixer.replaceText(parent.parent, ";"); - } - - // if next token is a string it could become a directive if node is removed -> no suggestion. - const nextToken = sourceCode.getTokenAfter(parent.parent); - - // if previous token exists and is not ";" or "{" not sure about ASI rules -> no suggestion. - const prevToken = sourceCode.getTokenBefore(parent.parent); - - if ( - nextToken && - isDeclarationNotSafeToRemove(nextToken, prevToken) - ) { - return null; - } - - // remove unused declared variable with single declaration like 'var a = b;' - return fixer.removeRange(parent.parent.range); - } - - // remove unused declared variable with multiple declaration except first one like 'var a = b, c = d;' - if (tokenBefore.value === ",") { - return fixer.removeRange([ - tokenBefore.range[0], - parent.range[1], - ]); - } - - // remove first unused declared variable when there are multiple declarations - return fixer.removeRange([ - parent.range[0], - getNextTokenEnd(parent), - ]); - } - - // remove variables in object patterns - if (parent.parent.type === "ObjectPattern") { - if (parent.parent.properties.length === 1) { - // fix [a, ...{b}] - if (parent.parent.parent.type === "RestElement") { - return fixRestInPattern(parent.parent.parent); - } - - // fix [{ a }] - if (parent.parent.parent.type === "ArrayPattern") { - return fixNestedArrayVariable(parent.parent); - } - - /* - * var {a} = foo; - * function a({a}) {} - * fix const { a: { b } } = foo; - */ - return fixVariables(parent.parent); - } - - // fix const { a:b } = foo; - if (tokenBefore.value === ":") { - // remove first unused variable in const { a:b } = foo; - if ( - getTokenBeforeValue(parent) === "{" && - getTokenAfterValue(parent) === "," - ) { - return fixer.removeRange([ - parent.range[0], - getNextTokenEnd(parent), - ]); - } - - // remove unused variables in const { a: b, c: d } = foo; except first one - return fixer.removeRange([ - getPreviousTokenStart(parent), - id.range[1], - ]); - } - } - - // remove unused variables inside an array - if (parentType === "ArrayPattern") { - if (hasSingleElement(parent)) { - // fix [a, ...[b]] - if (parent.parent.type === "RestElement") { - return fixRestInPattern(parent.parent); - } - - // fix [ [a] ] - if (parent.parent.type === "ArrayPattern") { - return fixNestedArrayVariable(parent); - } - - /* - * fix var [a] = foo; - * fix function foo([a]) {} - * fix const { a: [b] } = foo; - */ - return fixVariables(parent); - } - - // if "a" is unused in [a, b ,c] fixes to [, b, c] - if (tokenBefore.value === "," && tokenAfter.value === ",") { - return fixer.removeRange(id.range); - } - } - - // remove unused rest elements - if (parentType === "RestElement") { - // fix [a, ...rest] - if (parent.parent.type === "ArrayPattern") { - if (hasSingleElement(parent.parent)) { - // fix [[...rest]] when there is only rest element - if (parent.parent.parent.type === "ArrayPattern") { - return fixNestedArrayVariable(parent.parent); - } - - // fix 'const [...rest] = foo;' and 'function foo([...rest]) {}' - return fixVariables(parent.parent); - } - - // fix [a, ...rest] - return fixer.removeRange([ - getPreviousTokenStart(id, 1), - id.range[1], - ]); - } - - // fix { a, ...rest} - if (parent.parent.type === "ObjectPattern") { - // fix 'const {...rest} = foo;' and 'function foo({...rest}) {}' - if (parent.parent.properties.length === 1) { - return fixVariables(parent.parent); - } - - // fix { a, ...rest} when there are multiple properties - return fixer.removeRange([ - getPreviousTokenStart(id, 1), - id.range[1], - ]); - } - - // fix function foo(...rest) {} - if (isFunction(parent.parent)) { - // remove unused rest in function parameter if there is only single parameter - if (parent.parent.params.length === 1) { - return fixer.removeRange(parent.range); - } - - // remove unused rest in function parameter if there multiple parameter - return fixer.removeRange([ - getPreviousTokenStart(parent), - parent.range[1], - ]); - } - } - - if (parentType === "AssignmentPattern") { - // fix [a = aDefault] - if (parent.parent.type === "ArrayPattern") { - return fixNestedArrayVariable(parent); - } - - // fix {a = aDefault} - if (parent.parent.parent.type === "ObjectPattern") { - if (parent.parent.parent.properties.length === 1) { - // fixes [{a = aDefault}] - if ( - parent.parent.parent.parent.type === "ArrayPattern" - ) { - return fixNestedArrayVariable(parent.parent.parent); - } - - // fix 'const {a = aDefault} = foo;' and 'function foo({a = aDefault}) {}' - return fixVariables(parent.parent.parent); - } - - // fix unused 'a' in {a = aDefault} if it is the first property - if ( - getTokenBeforeValue(parent.parent) === "{" && - getTokenAfterValue(parent.parent) === "," - ) { - return fixer.removeRange([ - parent.parent.range[0], - getNextTokenEnd(parent.parent), - ]); - } - - // fix unused 'b' in {a, b = aDefault} if it is not the first property - return fixer.removeRange([ - getPreviousTokenStart(parent.parent), - parent.parent.range[1], - ]); - } - - // fix unused assignment patterns in function parameters - if (isFunction(parent.parent)) { - return fixFunctionParameters(parent); - } - } - - // remove unused functions - if (parentType === "FunctionDeclaration" && parent.id === id) { - return fixer.removeRange(parent.range); - } - - // remove unused default import - if (parentType === "ImportDefaultSpecifier") { - // remove unused default import when there are not other imports - if ( - !hasImportOfCertainType(parent.parent, "ImportSpecifier") && - !hasImportOfCertainType( - parent.parent, - "ImportNamespaceSpecifier", - ) - ) { - return fixer.removeRange([ - parent.range[0], - parent.parent.source.range[0], - ]); - } - - // remove unused default import when there are other imports also - return fixer.removeRange([id.range[0], tokenAfter.range[1]]); - } - - if (parentType === "ImportSpecifier") { - // remove unused imports when there is a single import - if ( - parent.parent.specifiers.filter( - e => e.type === "ImportSpecifier", - ).length === 1 - ) { - // remove unused import when there is no default import - if ( - !hasImportOfCertainType( - parent.parent, - "ImportDefaultSpecifier", - ) - ) { - return fixer.removeRange(parent.parent.range); - } - - // fixes "import foo from 'module';" to "import 'module';" - return fixer.removeRange([ - getPreviousTokenStart(parent, 1), - tokenAfter.range[1], - ]); - } - - if (getTokenBeforeValue(parent) === "{") { - return fixer.removeRange([ - parent.range[0], - getNextTokenEnd(parent), - ]); - } - - return fixer.removeRange([ - getPreviousTokenStart(parent), - parent.range[1], - ]); - } - - if (parentType === "ImportNamespaceSpecifier") { - if ( - hasImportOfCertainType( - parent.parent, - "ImportDefaultSpecifier", - ) - ) { - return fixer.removeRange([ - getPreviousTokenStart(parent), - parent.range[1], - ]); - } - - // fixes "import * as foo from 'module';" to "import 'module';" - return fixer.removeRange([ - parent.range[0], - parent.parent.source.range[0], - ]); - } - - // skip error in catch(error) variable - if (parentType === "CatchClause") { - return null; - } - - // remove unused declared classes - if (parentType === "ClassDeclaration") { - return fixer.removeRange(parent.range); - } - - // remove unused variable that is in a sequence [a,b] fixes to [a] - if (tokenBefore?.value === ",") { - return fixer.removeRange([tokenBefore.range[0], id.range[1]]); - } - - // remove unused variable that is in a sequence inside function arguments and object pattern - if (tokenAfter.value === ",") { - // fix function foo(a, b) {} - if (tokenBefore.value === "(") { - return fixer.removeRange([ - id.range[0], - tokenAfter.range[1], - ]); - } - - // fix const {a, b} = foo; - if (tokenBefore.value === "{") { - return fixer.removeRange([ - id.range[0], - tokenAfter.range[1], - ]); - } - } - - if ( - parentType === "ArrowFunctionExpression" && - parent.params.length === 1 && - tokenAfter?.value !== ")" - ) { - return fixer.replaceText(id, "()"); - } - - return fixer.removeRange(id.range); - } - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - return { - "Program:exit"(programNode) { - const unusedVars = collectUnusedVariables( - sourceCode.getScope(programNode), - [], - ); - - for (let i = 0, l = unusedVars.length; i < l; ++i) { - const unusedVar = unusedVars[i]; - - // Report the first declaration. - if (unusedVar.defs.length > 0) { - // report last write reference, https://github.com/eslint/eslint/issues/14324 - const writeReferences = unusedVar.references.filter( - ref => - ref.isWrite() && - ref.from.variableScope === - unusedVar.scope.variableScope, - ); - - let referenceToReport; - - if (writeReferences.length > 0) { - referenceToReport = writeReferences.at(-1); - } - - context.report({ - node: referenceToReport - ? referenceToReport.identifier - : unusedVar.identifiers[0], - messageId: "unusedVar", - data: unusedVar.references.some(ref => - ref.isWrite(), - ) - ? getAssignedMessageData(unusedVar) - : getDefinedMessageData(unusedVar), - suggest: [ - { - messageId: "removeVar", - data: { - varName: unusedVar.name, - }, - fix(fixer) { - return handleFixes(fixer, unusedVar); - }, - }, - ], - }); - - // If there are no regular declaration, report the first `/*globals*/` comment directive. - } else if (unusedVar.eslintExplicitGlobalComments) { - const directiveComment = - unusedVar.eslintExplicitGlobalComments[0]; - - context.report({ - node: programNode, - loc: astUtils.getNameLocationInGlobalDirectiveComment( - sourceCode, - directiveComment, - unusedVar.name, - ), - messageId: "unusedVar", - data: getDefinedMessageData(unusedVar), - }); - } - } - }, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow unused variables", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-unused-vars" + }, + + schema: [ + { + oneOf: [ + { + enum: ["all", "local"] + }, + { + type: "object", + properties: { + vars: { + enum: ["all", "local"] + }, + varsIgnorePattern: { + type: "string" + }, + args: { + enum: ["all", "after-used", "none"] + }, + ignoreRestSiblings: { + type: "boolean" + }, + argsIgnorePattern: { + type: "string" + }, + caughtErrors: { + enum: ["all", "none"] + }, + caughtErrorsIgnorePattern: { + type: "string" + }, + destructuredArrayIgnorePattern: { + type: "string" + } + }, + additionalProperties: false + } + ] + } + ], + + messages: { + unusedVar: "'{{varName}}' is {{action}} but never used{{additional}}." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + + const REST_PROPERTY_TYPE = /^(?:RestElement|(?:Experimental)?RestProperty)$/u; + + const config = { + vars: "all", + args: "after-used", + ignoreRestSiblings: false, + caughtErrors: "none" + }; + + const firstOption = context.options[0]; + + if (firstOption) { + if (typeof firstOption === "string") { + config.vars = firstOption; + } else { + config.vars = firstOption.vars || config.vars; + config.args = firstOption.args || config.args; + config.ignoreRestSiblings = firstOption.ignoreRestSiblings || config.ignoreRestSiblings; + config.caughtErrors = firstOption.caughtErrors || config.caughtErrors; + + if (firstOption.varsIgnorePattern) { + config.varsIgnorePattern = new RegExp(firstOption.varsIgnorePattern, "u"); + } + + if (firstOption.argsIgnorePattern) { + config.argsIgnorePattern = new RegExp(firstOption.argsIgnorePattern, "u"); + } + + if (firstOption.caughtErrorsIgnorePattern) { + config.caughtErrorsIgnorePattern = new RegExp(firstOption.caughtErrorsIgnorePattern, "u"); + } + + if (firstOption.destructuredArrayIgnorePattern) { + config.destructuredArrayIgnorePattern = new RegExp(firstOption.destructuredArrayIgnorePattern, "u"); + } + } + } + + /** + * Generates the message data about the variable being defined and unused, + * including the ignore pattern if configured. + * @param {Variable} unusedVar eslint-scope variable object. + * @returns {UnusedVarMessageData} The message data to be used with this unused variable. + */ + function getDefinedMessageData(unusedVar) { + const defType = unusedVar.defs && unusedVar.defs[0] && unusedVar.defs[0].type; + let type; + let pattern; + + if (defType === "CatchClause" && config.caughtErrorsIgnorePattern) { + type = "args"; + pattern = config.caughtErrorsIgnorePattern.toString(); + } else if (defType === "Parameter" && config.argsIgnorePattern) { + type = "args"; + pattern = config.argsIgnorePattern.toString(); + } else if (defType !== "Parameter" && config.varsIgnorePattern) { + type = "vars"; + pattern = config.varsIgnorePattern.toString(); + } + + const additional = type ? `. Allowed unused ${type} must match ${pattern}` : ""; + + return { + varName: unusedVar.name, + action: "defined", + additional + }; + } + + /** + * Generate the warning message about the variable being + * assigned and unused, including the ignore pattern if configured. + * @param {Variable} unusedVar eslint-scope variable object. + * @returns {UnusedVarMessageData} The message data to be used with this unused variable. + */ + function getAssignedMessageData(unusedVar) { + const def = unusedVar.defs[0]; + let additional = ""; + + if (config.destructuredArrayIgnorePattern && def && def.name.parent.type === "ArrayPattern") { + additional = `. Allowed unused elements of array destructuring patterns must match ${config.destructuredArrayIgnorePattern.toString()}`; + } else if (config.varsIgnorePattern) { + additional = `. Allowed unused vars must match ${config.varsIgnorePattern.toString()}`; + } + + return { + varName: unusedVar.name, + action: "assigned a value", + additional + }; + } + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + const STATEMENT_TYPE = /(?:Statement|Declaration)$/u; + + /** + * Determines if a given variable is being exported from a module. + * @param {Variable} variable eslint-scope variable object. + * @returns {boolean} True if the variable is exported, false if not. + * @private + */ + function isExported(variable) { + + const definition = variable.defs[0]; + + if (definition) { + + let node = definition.node; + + if (node.type === "VariableDeclarator") { + node = node.parent; + } else if (definition.type === "Parameter") { + return false; + } + + return node.parent.type.indexOf("Export") === 0; + } + return false; + + } + + /** + * Checks whether a node is a sibling of the rest property or not. + * @param {ASTNode} node a node to check + * @returns {boolean} True if the node is a sibling of the rest property, otherwise false. + */ + function hasRestSibling(node) { + return node.type === "Property" && + node.parent.type === "ObjectPattern" && + REST_PROPERTY_TYPE.test(node.parent.properties[node.parent.properties.length - 1].type); + } + + /** + * Determines if a variable has a sibling rest property + * @param {Variable} variable eslint-scope variable object. + * @returns {boolean} True if the variable is exported, false if not. + * @private + */ + function hasRestSpreadSibling(variable) { + if (config.ignoreRestSiblings) { + const hasRestSiblingDefinition = variable.defs.some(def => hasRestSibling(def.name.parent)); + const hasRestSiblingReference = variable.references.some(ref => hasRestSibling(ref.identifier.parent)); + + return hasRestSiblingDefinition || hasRestSiblingReference; + } + + return false; + } + + /** + * Determines if a reference is a read operation. + * @param {Reference} ref An eslint-scope Reference + * @returns {boolean} whether the given reference represents a read operation + * @private + */ + function isReadRef(ref) { + return ref.isRead(); + } + + /** + * Determine if an identifier is referencing an enclosing function name. + * @param {Reference} ref The reference to check. + * @param {ASTNode[]} nodes The candidate function nodes. + * @returns {boolean} True if it's a self-reference, false if not. + * @private + */ + function isSelfReference(ref, nodes) { + let scope = ref.from; + + while (scope) { + if (nodes.includes(scope.block)) { + return true; + } + + scope = scope.upper; + } + + return false; + } + + /** + * Gets a list of function definitions for a specified variable. + * @param {Variable} variable eslint-scope variable object. + * @returns {ASTNode[]} Function nodes. + * @private + */ + function getFunctionDefinitions(variable) { + const functionDefinitions = []; + + variable.defs.forEach(def => { + const { type, node } = def; + + // FunctionDeclarations + if (type === "FunctionName") { + functionDefinitions.push(node); + } + + // FunctionExpressions + if (type === "Variable" && node.init && + (node.init.type === "FunctionExpression" || node.init.type === "ArrowFunctionExpression")) { + functionDefinitions.push(node.init); + } + }); + return functionDefinitions; + } + + /** + * Checks the position of given nodes. + * @param {ASTNode} inner A node which is expected as inside. + * @param {ASTNode} outer A node which is expected as outside. + * @returns {boolean} `true` if the `inner` node exists in the `outer` node. + * @private + */ + function isInside(inner, outer) { + return ( + inner.range[0] >= outer.range[0] && + inner.range[1] <= outer.range[1] + ); + } + + /** + * Checks whether a given node is unused expression or not. + * @param {ASTNode} node The node itself + * @returns {boolean} The node is an unused expression. + * @private + */ + function isUnusedExpression(node) { + const parent = node.parent; + + if (parent.type === "ExpressionStatement") { + return true; + } + + if (parent.type === "SequenceExpression") { + const isLastExpression = parent.expressions[parent.expressions.length - 1] === node; + + if (!isLastExpression) { + return true; + } + return isUnusedExpression(parent); + } + + return false; + } + + /** + * If a given reference is left-hand side of an assignment, this gets + * the right-hand side node of the assignment. + * + * In the following cases, this returns null. + * + * - The reference is not the LHS of an assignment expression. + * - The reference is inside of a loop. + * - The reference is inside of a function scope which is different from + * the declaration. + * @param {eslint-scope.Reference} ref A reference to check. + * @param {ASTNode} prevRhsNode The previous RHS node. + * @returns {ASTNode|null} The RHS node or null. + * @private + */ + function getRhsNode(ref, prevRhsNode) { + const id = ref.identifier; + const parent = id.parent; + const refScope = ref.from.variableScope; + const varScope = ref.resolved.scope.variableScope; + const canBeUsedLater = refScope !== varScope || astUtils.isInLoop(id); + + /* + * Inherits the previous node if this reference is in the node. + * This is for `a = a + a`-like code. + */ + if (prevRhsNode && isInside(id, prevRhsNode)) { + return prevRhsNode; + } + + if (parent.type === "AssignmentExpression" && + isUnusedExpression(parent) && + id === parent.left && + !canBeUsedLater + ) { + return parent.right; + } + return null; + } + + /** + * Checks whether a given function node is stored to somewhere or not. + * If the function node is stored, the function can be used later. + * @param {ASTNode} funcNode A function node to check. + * @param {ASTNode} rhsNode The RHS node of the previous assignment. + * @returns {boolean} `true` if under the following conditions: + * - the funcNode is assigned to a variable. + * - the funcNode is bound as an argument of a function call. + * - the function is bound to a property and the object satisfies above conditions. + * @private + */ + function isStorableFunction(funcNode, rhsNode) { + let node = funcNode; + let parent = funcNode.parent; + + while (parent && isInside(parent, rhsNode)) { + switch (parent.type) { + case "SequenceExpression": + if (parent.expressions[parent.expressions.length - 1] !== node) { + return false; + } + break; + + case "CallExpression": + case "NewExpression": + return parent.callee !== node; + + case "AssignmentExpression": + case "TaggedTemplateExpression": + case "YieldExpression": + return true; + + default: + if (STATEMENT_TYPE.test(parent.type)) { + + /* + * If it encountered statements, this is a complex pattern. + * Since analyzing complex patterns is hard, this returns `true` to avoid false positive. + */ + return true; + } + } + + node = parent; + parent = parent.parent; + } + + return false; + } + + /** + * Checks whether a given Identifier node exists inside of a function node which can be used later. + * + * "can be used later" means: + * - the function is assigned to a variable. + * - the function is bound to a property and the object can be used later. + * - the function is bound as an argument of a function call. + * + * If a reference exists in a function which can be used later, the reference is read when the function is called. + * @param {ASTNode} id An Identifier node to check. + * @param {ASTNode} rhsNode The RHS node of the previous assignment. + * @returns {boolean} `true` if the `id` node exists inside of a function node which can be used later. + * @private + */ + function isInsideOfStorableFunction(id, rhsNode) { + const funcNode = astUtils.getUpperFunction(id); + + return ( + funcNode && + isInside(funcNode, rhsNode) && + isStorableFunction(funcNode, rhsNode) + ); + } + + /** + * Checks whether a given reference is a read to update itself or not. + * @param {eslint-scope.Reference} ref A reference to check. + * @param {ASTNode} rhsNode The RHS node of the previous assignment. + * @returns {boolean} The reference is a read to update itself. + * @private + */ + function isReadForItself(ref, rhsNode) { + const id = ref.identifier; + const parent = id.parent; + + return ref.isRead() && ( + + // self update. e.g. `a += 1`, `a++` + ( + ( + parent.type === "AssignmentExpression" && + parent.left === id && + isUnusedExpression(parent) && + !astUtils.isLogicalAssignmentOperator(parent.operator) + ) || + ( + parent.type === "UpdateExpression" && + isUnusedExpression(parent) + ) + ) || + + // in RHS of an assignment for itself. e.g. `a = a + 1` + ( + rhsNode && + isInside(id, rhsNode) && + !isInsideOfStorableFunction(id, rhsNode) + ) + ); + } + + /** + * Determine if an identifier is used either in for-in or for-of loops. + * @param {Reference} ref The reference to check. + * @returns {boolean} whether reference is used in the for-in loops + * @private + */ + function isForInOfRef(ref) { + let target = ref.identifier.parent; + + + // "for (var ...) { return; }" + if (target.type === "VariableDeclarator") { + target = target.parent.parent; + } + + if (target.type !== "ForInStatement" && target.type !== "ForOfStatement") { + return false; + } + + // "for (...) { return; }" + if (target.body.type === "BlockStatement") { + target = target.body.body[0]; + + // "for (...) return;" + } else { + target = target.body; + } + + // For empty loop body + if (!target) { + return false; + } + + return target.type === "ReturnStatement"; + } + + /** + * Determines if the variable is used. + * @param {Variable} variable The variable to check. + * @returns {boolean} True if the variable is used + * @private + */ + function isUsedVariable(variable) { + const functionNodes = getFunctionDefinitions(variable), + isFunctionDefinition = functionNodes.length > 0; + let rhsNode = null; + + return variable.references.some(ref => { + if (isForInOfRef(ref)) { + return true; + } + + const forItself = isReadForItself(ref, rhsNode); + + rhsNode = getRhsNode(ref, rhsNode); + + return ( + isReadRef(ref) && + !forItself && + !(isFunctionDefinition && isSelfReference(ref, functionNodes)) + ); + }); + } + + /** + * Checks whether the given variable is after the last used parameter. + * @param {eslint-scope.Variable} variable The variable to check. + * @returns {boolean} `true` if the variable is defined after the last + * used parameter. + */ + function isAfterLastUsedArg(variable) { + const def = variable.defs[0]; + const params = sourceCode.getDeclaredVariables(def.node); + const posteriorParams = params.slice(params.indexOf(variable) + 1); + + // If any used parameters occur after this parameter, do not report. + return !posteriorParams.some(v => v.references.length > 0 || v.eslintUsed); + } + + /** + * Gets an array of variables without read references. + * @param {Scope} scope an eslint-scope Scope object. + * @param {Variable[]} unusedVars an array that saving result. + * @returns {Variable[]} unused variables of the scope and descendant scopes. + * @private + */ + function collectUnusedVariables(scope, unusedVars) { + const variables = scope.variables; + const childScopes = scope.childScopes; + let i, l; + + if (scope.type !== "global" || config.vars === "all") { + for (i = 0, l = variables.length; i < l; ++i) { + const variable = variables[i]; + + // skip a variable of class itself name in the class scope + if (scope.type === "class" && scope.block.id === variable.identifiers[0]) { + continue; + } + + // skip function expression names and variables marked with markVariableAsUsed() + if (scope.functionExpressionScope || variable.eslintUsed) { + continue; + } + + // skip implicit "arguments" variable + if (scope.type === "function" && variable.name === "arguments" && variable.identifiers.length === 0) { + continue; + } + + // explicit global variables don't have definitions. + const def = variable.defs[0]; + + if (def) { + const type = def.type; + const refUsedInArrayPatterns = variable.references.some(ref => ref.identifier.parent.type === "ArrayPattern"); + + // skip elements of array destructuring patterns + if ( + ( + def.name.parent.type === "ArrayPattern" || + refUsedInArrayPatterns + ) && + config.destructuredArrayIgnorePattern && + config.destructuredArrayIgnorePattern.test(def.name.name) + ) { + continue; + } + + // skip catch variables + if (type === "CatchClause") { + if (config.caughtErrors === "none") { + continue; + } + + // skip ignored parameters + if (config.caughtErrorsIgnorePattern && config.caughtErrorsIgnorePattern.test(def.name.name)) { + continue; + } + } + + if (type === "Parameter") { + + // skip any setter argument + if ((def.node.parent.type === "Property" || def.node.parent.type === "MethodDefinition") && def.node.parent.kind === "set") { + continue; + } + + // if "args" option is "none", skip any parameter + if (config.args === "none") { + continue; + } + + // skip ignored parameters + if (config.argsIgnorePattern && config.argsIgnorePattern.test(def.name.name)) { + continue; + } + + // if "args" option is "after-used", skip used variables + if (config.args === "after-used" && astUtils.isFunction(def.name.parent) && !isAfterLastUsedArg(variable)) { + continue; + } + } else { + + // skip ignored variables + if (config.varsIgnorePattern && config.varsIgnorePattern.test(def.name.name)) { + continue; + } + } + } + + if (!isUsedVariable(variable) && !isExported(variable) && !hasRestSpreadSibling(variable)) { + unusedVars.push(variable); + } + } + } + + for (i = 0, l = childScopes.length; i < l; ++i) { + collectUnusedVariables(childScopes[i], unusedVars); + } + + return unusedVars; + } + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + return { + "Program:exit"(programNode) { + const unusedVars = collectUnusedVariables(sourceCode.getScope(programNode), []); + + for (let i = 0, l = unusedVars.length; i < l; ++i) { + const unusedVar = unusedVars[i]; + + // Report the first declaration. + if (unusedVar.defs.length > 0) { + + // report last write reference, https://github.com/eslint/eslint/issues/14324 + const writeReferences = unusedVar.references.filter(ref => ref.isWrite() && ref.from.variableScope === unusedVar.scope.variableScope); + + let referenceToReport; + + if (writeReferences.length > 0) { + referenceToReport = writeReferences[writeReferences.length - 1]; + } + + context.report({ + node: referenceToReport ? referenceToReport.identifier : unusedVar.identifiers[0], + messageId: "unusedVar", + data: unusedVar.references.some(ref => ref.isWrite()) + ? getAssignedMessageData(unusedVar) + : getDefinedMessageData(unusedVar) + }); + + // If there are no regular declaration, report the first `/*globals*/` comment directive. + } else if (unusedVar.eslintExplicitGlobalComments) { + const directiveComment = unusedVar.eslintExplicitGlobalComments[0]; + + context.report({ + node: programNode, + loc: astUtils.getNameLocationInGlobalDirectiveComment(sourceCode, directiveComment, unusedVar.name), + messageId: "unusedVar", + data: getDefinedMessageData(unusedVar) + }); + } + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/no-use-before-define.js b/node_modules/eslint/lib/rules/no-use-before-define.js index 870a36798..9d6b04340 100644 --- a/node_modules/eslint/lib/rules/no-use-before-define.js +++ b/node_modules/eslint/lib/rules/no-use-before-define.js @@ -9,8 +9,7 @@ // Helpers //------------------------------------------------------------------------------ -const SENTINEL_TYPE = - /^(?:(?:Function|Class)(?:Declaration|Expression)|ArrowFunctionExpression|CatchClause|ImportDeclaration|ExportNamedDeclaration)$/u; +const SENTINEL_TYPE = /^(?:(?:Function|Class)(?:Declaration|Expression)|ArrowFunctionExpression|CatchClause|ImportDeclaration|ExportNamedDeclaration)$/u; const FOR_IN_OF_TYPE = /^For(?:In|Of)Statement$/u; /** @@ -19,21 +18,21 @@ const FOR_IN_OF_TYPE = /^For(?:In|Of)Statement$/u; * @returns {Object} The parsed options. */ function parseOptions(options) { - if (typeof options === "object" && options !== null) { - return options; - } - - const functions = typeof options === "string" ? options !== "nofunc" : true; - - return { - functions, - classes: true, - variables: true, - allowNamedExports: false, - enums: true, - typedefs: true, - ignoreTypeReferences: true, - }; + let functions = true; + let classes = true; + let variables = true; + let allowNamedExports = false; + + if (typeof options === "string") { + functions = (options !== "nofunc"); + } else if (typeof options === "object" && options !== null) { + functions = options.functions !== false; + classes = options.classes !== false; + variables = options.variables !== false; + allowNamedExports = !!options.allowNamedExports; + } + + return { functions, classes, variables, allowNamedExports }; } /** @@ -43,7 +42,7 @@ function parseOptions(options) { * @returns {boolean} `true` if the location is inside of the range of the node. */ function isInRange(node, location) { - return node && node.range[0] <= location && location <= node.range[1]; + return node && node.range[0] <= location && location <= node.range[1]; } /** @@ -54,15 +53,18 @@ function isInRange(node, location) { * @returns {boolean} `true` if the location is inside of a class static initializer. */ function isInClassStaticInitializerRange(node, location) { - return node.body.some( - classMember => - (classMember.type === "StaticBlock" && - isInRange(classMember, location)) || - (classMember.type === "PropertyDefinition" && - classMember.static && - classMember.value && - isInRange(classMember.value, location)), - ); + return node.body.some(classMember => ( + ( + classMember.type === "StaticBlock" && + isInRange(classMember, location) + ) || + ( + classMember.type === "PropertyDefinition" && + classMember.static && + classMember.value && + isInRange(classMember.value, location) + ) + )); } /** @@ -72,18 +74,19 @@ function isInClassStaticInitializerRange(node, location) { * @returns {boolean} `true` if the scope is a class static initializer scope. */ function isClassStaticInitializerScope(scope) { - if (scope.type === "class-static-block") { - return true; - } + if (scope.type === "class-static-block") { + return true; + } - if (scope.type === "class-field-initializer") { - // `scope.block` is PropertyDefinition#value node - const propertyDefinition = scope.block.parent; + if (scope.type === "class-field-initializer") { - return propertyDefinition.static; - } + // `scope.block` is PropertyDefinition#value node + const propertyDefinition = scope.block.parent; - return false; + return propertyDefinition.static; + } + + return false; } /** @@ -123,19 +126,19 @@ function isClassStaticInitializerScope(scope) { * @returns {boolean} `true` if the reference is from a separate execution context. */ function isFromSeparateExecutionContext(reference) { - const variable = reference.resolved; - let scope = reference.from; - - // Scope#variableScope represents execution context - while (variable.scope.variableScope !== scope.variableScope) { - if (isClassStaticInitializerScope(scope.variableScope)) { - scope = scope.variableScope.upper; - } else { - return true; - } - } - - return false; + const variable = reference.resolved; + let scope = reference.from; + + // Scope#variableScope represents execution context + while (variable.scope.variableScope !== scope.variableScope) { + if (isClassStaticInitializerScope(scope.variableScope)) { + scope = scope.variableScope.upper; + } else { + return true; + } + } + + return false; } /** @@ -158,289 +161,188 @@ function isFromSeparateExecutionContext(reference) { * @returns {boolean} `true` if the reference is evaluated during the initialization. */ function isEvaluatedDuringInitialization(reference) { - if (isFromSeparateExecutionContext(reference)) { - /* - * Even if the reference appears in the initializer, it isn't evaluated during the initialization. - * For example, `const x = () => x;` is valid. - */ - return false; - } - - const location = reference.identifier.range[1]; - const definition = reference.resolved.defs[0]; - - if (definition.type === "ClassName") { - // `ClassDeclaration` or `ClassExpression` - const classDefinition = definition.node; - - return ( - isInRange(classDefinition, location) && - /* - * Class binding is initialized before running static initializers. - * For example, `class C { static foo = C; static { bar = C; } }` is valid. - */ - !isInClassStaticInitializerRange(classDefinition.body, location) - ); - } - - let node = definition.name.parent; - - while (node) { - if (node.type === "VariableDeclarator") { - if (isInRange(node.init, location)) { - return true; - } - if ( - FOR_IN_OF_TYPE.test(node.parent.parent.type) && - isInRange(node.parent.parent.right, location) - ) { - return true; - } - break; - } else if (node.type === "AssignmentPattern") { - if (isInRange(node.right, location)) { - return true; - } - } else if (SENTINEL_TYPE.test(node.type)) { - break; - } - - node = node.parent; - } - - return false; -} - -/** - * check whether the reference contains a type query. - * @param {ASTNode} node Identifier node to check. - * @returns {boolean} true if reference contains type query. - */ -function referenceContainsTypeQuery(node) { - switch (node.type) { - case "TSTypeQuery": - return true; - - case "TSQualifiedName": - case "Identifier": - return referenceContainsTypeQuery(node.parent); - - default: - // if we find a different node, there's no chance that we're in a TSTypeQuery - return false; - } -} - -/** - * Decorators are transpiled such that the decorator is placed after the class declaration - * So it is considered safe - * @param {Variable} variable The variable to check. - * @param {Reference} reference The reference to check. - * @returns {boolean} `true` if the reference is in a class decorator. - */ -function isClassRefInClassDecorator(variable, reference) { - if (variable.defs[0].type !== "ClassName") { - return false; - } - - if ( - !variable.defs[0].node.decorators || - variable.defs[0].node.decorators.length === 0 - ) { - return false; - } - - for (const deco of variable.defs[0].node.decorators) { - if ( - reference.identifier.range[0] >= deco.range[0] && - reference.identifier.range[1] <= deco.range[1] - ) { - return true; - } - } - - return false; + if (isFromSeparateExecutionContext(reference)) { + + /* + * Even if the reference appears in the initializer, it isn't evaluated during the initialization. + * For example, `const x = () => x;` is valid. + */ + return false; + } + + const location = reference.identifier.range[1]; + const definition = reference.resolved.defs[0]; + + if (definition.type === "ClassName") { + + // `ClassDeclaration` or `ClassExpression` + const classDefinition = definition.node; + + return ( + isInRange(classDefinition, location) && + + /* + * Class binding is initialized before running static initializers. + * For example, `class C { static foo = C; static { bar = C; } }` is valid. + */ + !isInClassStaticInitializerRange(classDefinition.body, location) + ); + } + + let node = definition.name.parent; + + while (node) { + if (node.type === "VariableDeclarator") { + if (isInRange(node.init, location)) { + return true; + } + if (FOR_IN_OF_TYPE.test(node.parent.parent.type) && + isInRange(node.parent.parent.right, location) + ) { + return true; + } + break; + } else if (node.type === "AssignmentPattern") { + if (isInRange(node.right, location)) { + return true; + } + } else if (SENTINEL_TYPE.test(node.type)) { + break; + } + + node = node.parent; + } + + return false; } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - dialects: ["javascript", "typescript"], - language: "javascript", - type: "problem", - - docs: { - description: - "Disallow the use of variables before they are defined", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-use-before-define", - }, - - schema: [ - { - oneOf: [ - { - enum: ["nofunc"], - }, - { - type: "object", - properties: { - functions: { type: "boolean" }, - classes: { type: "boolean" }, - variables: { type: "boolean" }, - allowNamedExports: { type: "boolean" }, - enums: { type: "boolean" }, - typedefs: { type: "boolean" }, - ignoreTypeReferences: { type: "boolean" }, - }, - additionalProperties: false, - }, - ], - }, - ], - - defaultOptions: [ - { - classes: true, - functions: true, - variables: true, - allowNamedExports: false, - enums: true, - typedefs: true, - ignoreTypeReferences: true, - }, - ], - - messages: { - usedBeforeDefined: "'{{name}}' was used before it was defined.", - }, - }, - - create(context) { - const options = parseOptions(context.options[0]); - const sourceCode = context.sourceCode; - - /** - * Determines whether a given reference should be checked. - * - * Returns `false` if the reference is: - * - initialization's (e.g., `let a = 1`). - * - referring to an undefined variable (i.e., if it's an unresolved reference). - * - referring to a variable that is defined, but not in the given source code - * (e.g., global environment variable or `arguments` in functions). - * - allowed by options. - * @param {eslint-scope.Reference} reference The reference - * @returns {boolean} `true` if the reference should be checked - */ - function shouldCheck(reference) { - if (reference.init) { - return false; - } - - const { identifier } = reference; - - if ( - options.allowNamedExports && - identifier.parent.type === "ExportSpecifier" && - identifier.parent.local === identifier - ) { - return false; - } - - const variable = reference.resolved; - - if (!variable || variable.defs.length === 0) { - return false; - } - - const definitionType = variable.defs[0].type; - - if (!options.functions && definitionType === "FunctionName") { - return false; - } - - if ( - ((!options.variables && definitionType === "Variable") || - (!options.classes && definitionType === "ClassName")) && - // don't skip checking the reference if it's in the same execution context, because of TDZ - isFromSeparateExecutionContext(reference) - ) { - return false; - } - - if (!options.enums && definitionType === "TSEnumName") { - return false; - } - - if (!options.typedefs && definitionType === "Type") { - return false; - } - - if ( - options.ignoreTypeReferences && - (referenceContainsTypeQuery(identifier) || - identifier.parent.type === "TSTypeReference") - ) { - return false; - } - - // skip nested namespace aliases as variable references - if (identifier.parent.type === "TSQualifiedName") { - let currentNode = identifier.parent; - - while (currentNode.type === "TSQualifiedName") { - currentNode = currentNode.left; - } - - if (currentNode === identifier) { - return true; - } - - return false; - } - - if (isClassRefInClassDecorator(variable, reference)) { - return false; - } - - return true; - } - - /** - * Finds and validates all references in a given scope and its child scopes. - * @param {eslint-scope.Scope} scope The scope object. - * @returns {void} - */ - function checkReferencesInScope(scope) { - scope.references.filter(shouldCheck).forEach(reference => { - const variable = reference.resolved; - const definitionIdentifier = variable.defs[0].name; - - if ( - reference.identifier.range[1] < - definitionIdentifier.range[1] || - (isEvaluatedDuringInitialization(reference) && - reference.identifier.parent.type !== "TSTypeReference") - ) { - context.report({ - node: reference.identifier, - messageId: "usedBeforeDefined", - data: reference.identifier, - }); - } - }); - - scope.childScopes.forEach(checkReferencesInScope); - } - - return { - Program(node) { - checkReferencesInScope(sourceCode.getScope(node)); - }, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow the use of variables before they are defined", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-use-before-define" + }, + + schema: [ + { + oneOf: [ + { + enum: ["nofunc"] + }, + { + type: "object", + properties: { + functions: { type: "boolean" }, + classes: { type: "boolean" }, + variables: { type: "boolean" }, + allowNamedExports: { type: "boolean" } + }, + additionalProperties: false + } + ] + } + ], + + messages: { + usedBeforeDefined: "'{{name}}' was used before it was defined." + } + }, + + create(context) { + const options = parseOptions(context.options[0]); + const sourceCode = context.sourceCode; + + /** + * Determines whether a given reference should be checked. + * + * Returns `false` if the reference is: + * - initialization's (e.g., `let a = 1`). + * - referring to an undefined variable (i.e., if it's an unresolved reference). + * - referring to a variable that is defined, but not in the given source code + * (e.g., global environment variable or `arguments` in functions). + * - allowed by options. + * @param {eslint-scope.Reference} reference The reference + * @returns {boolean} `true` if the reference should be checked + */ + function shouldCheck(reference) { + if (reference.init) { + return false; + } + + const { identifier } = reference; + + if ( + options.allowNamedExports && + identifier.parent.type === "ExportSpecifier" && + identifier.parent.local === identifier + ) { + return false; + } + + const variable = reference.resolved; + + if (!variable || variable.defs.length === 0) { + return false; + } + + const definitionType = variable.defs[0].type; + + if (!options.functions && definitionType === "FunctionName") { + return false; + } + + if ( + ( + !options.variables && definitionType === "Variable" || + !options.classes && definitionType === "ClassName" + ) && + + // don't skip checking the reference if it's in the same execution context, because of TDZ + isFromSeparateExecutionContext(reference) + ) { + return false; + } + + return true; + } + + /** + * Finds and validates all references in a given scope and its child scopes. + * @param {eslint-scope.Scope} scope The scope object. + * @returns {void} + */ + function checkReferencesInScope(scope) { + scope.references.filter(shouldCheck).forEach(reference => { + const variable = reference.resolved; + const definitionIdentifier = variable.defs[0].name; + + if ( + reference.identifier.range[1] < definitionIdentifier.range[1] || + isEvaluatedDuringInitialization(reference) + ) { + context.report({ + node: reference.identifier, + messageId: "usedBeforeDefined", + data: reference.identifier + }); + } + }); + + scope.childScopes.forEach(checkReferencesInScope); + } + + return { + Program(node) { + checkReferencesInScope(sourceCode.getScope(node)); + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-useless-assignment.js b/node_modules/eslint/lib/rules/no-useless-assignment.js deleted file mode 100644 index 786744634..000000000 --- a/node_modules/eslint/lib/rules/no-useless-assignment.js +++ /dev/null @@ -1,654 +0,0 @@ -/** - * @fileoverview A rule to disallow unnecessary assignments`. - * @author Yosuke Ota - */ - -"use strict"; - -const { findVariable } = require("@eslint-community/eslint-utils"); - -//------------------------------------------------------------------------------ -// Types -//------------------------------------------------------------------------------ - -/** @typedef {import("estree").Node} ASTNode */ -/** @typedef {import("estree").Pattern} Pattern */ -/** @typedef {import("estree").Identifier} Identifier */ -/** @typedef {import("estree").VariableDeclarator} VariableDeclarator */ -/** @typedef {import("estree").AssignmentExpression} AssignmentExpression */ -/** @typedef {import("estree").UpdateExpression} UpdateExpression */ -/** @typedef {import("estree").Expression} Expression */ -/** @typedef {import("eslint-scope").Scope} Scope */ -/** @typedef {import("eslint-scope").Variable} Variable */ -/** @typedef {import("../linter/code-path-analysis/code-path")} CodePath */ -/** @typedef {import("../linter/code-path-analysis/code-path-segment")} CodePathSegment */ - -//------------------------------------------------------------------------------ -// Helpers -//------------------------------------------------------------------------------ - -/** - * Extract identifier from the given pattern node used on the left-hand side of the assignment. - * @param {Pattern} pattern The pattern node to extract identifier - * @returns {Iterable} The extracted identifier - */ -function* extractIdentifiersFromPattern(pattern) { - switch (pattern.type) { - case "Identifier": - yield pattern; - return; - case "ObjectPattern": - for (const property of pattern.properties) { - yield* extractIdentifiersFromPattern( - property.type === "Property" ? property.value : property, - ); - } - return; - case "ArrayPattern": - for (const element of pattern.elements) { - if (!element) { - continue; - } - yield* extractIdentifiersFromPattern(element); - } - return; - case "RestElement": - yield* extractIdentifiersFromPattern(pattern.argument); - return; - case "AssignmentPattern": - yield* extractIdentifiersFromPattern(pattern.left); - - // no default - } -} - -/** - * Checks whether the given identifier node is evaluated after the assignment identifier. - * @param {AssignmentInfo} assignment The assignment info. - * @param {Identifier} identifier The identifier to check. - * @returns {boolean} `true` if the given identifier node is evaluated after the assignment identifier. - */ -function isIdentifierEvaluatedAfterAssignment(assignment, identifier) { - if (identifier.range[0] < assignment.identifier.range[1]) { - return false; - } - if ( - assignment.expression && - assignment.expression.range[0] <= identifier.range[0] && - identifier.range[1] <= assignment.expression.range[1] - ) { - /* - * The identifier node is in an expression that is evaluated before the assignment. - * e.g. x = id; - * ^^ identifier to check - * ^ assignment identifier - */ - return false; - } - - /* - * e.g. - * x = 42; id; - * ^^ identifier to check - * ^ assignment identifier - * let { x, y = id } = obj; - * ^^ identifier to check - * ^ assignment identifier - */ - return true; -} - -/** - * Checks whether the given identifier node is used between the assigned identifier and the equal sign. - * - * e.g. let { x, y = x } = obj; - * ^ identifier to check - * ^ assigned identifier - * @param {AssignmentInfo} assignment The assignment info. - * @param {Identifier} identifier The identifier to check. - * @returns {boolean} `true` if the given identifier node is used between the assigned identifier and the equal sign. - */ -function isIdentifierUsedBetweenAssignedAndEqualSign(assignment, identifier) { - if (!assignment.expression) { - return false; - } - return ( - assignment.identifier.range[1] <= identifier.range[0] && - identifier.range[1] <= assignment.expression.range[0] - ); -} - -//------------------------------------------------------------------------------ -// Rule Definition -//------------------------------------------------------------------------------ - -/** @type {import('../types').Rule.RuleModule} */ -module.exports = { - meta: { - type: "problem", - - docs: { - description: - "Disallow variable assignments when the value is not used", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-useless-assignment", - }, - - schema: [], - - messages: { - unnecessaryAssignment: - "This assigned value is not used in subsequent statements.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - /** - * @typedef {Object} ScopeStack - * @property {CodePath} codePath The code path of this scope stack. - * @property {Scope} scope The scope of this scope stack. - * @property {ScopeStack} upper The upper scope stack. - * @property {Record} segments The map of ScopeStackSegmentInfo. - * @property {Set} currentSegments The current CodePathSegments. - * @property {Map} assignments The map of list of AssignmentInfo for each variable. - * @property {Array} tryStatementBlocks The array of TryStatement block nodes in this scope stack. - */ - /** - * @typedef {Object} ScopeStackSegmentInfo - * @property {CodePathSegment} segment The code path segment. - * @property {Identifier|null} first The first identifier that appears within the segment. - * @property {Identifier|null} last The last identifier that appears within the segment. - * `first` and `last` are used to determine whether an identifier exists within the segment position range. - * Since it is used as a range of segments, we should originally hold all nodes, not just identifiers, - * but since the only nodes to be judged are identifiers, it is sufficient to have a range of identifiers. - */ - /** - * @typedef {Object} AssignmentInfo - * @property {Variable} variable The variable that is assigned. - * @property {Identifier} identifier The identifier that is assigned. - * @property {VariableDeclarator|AssignmentExpression|UpdateExpression} node The node where the variable was updated. - * @property {Expression|null} expression The expression that is evaluated before the assignment. - * @property {CodePathSegment[]} segments The code path segments where the assignment was made. - */ - - /** @type {ScopeStack} */ - let scopeStack = null; - - /** @type {Set} */ - const codePathStartScopes = new Set(); - - /** - * Gets the scope of code path start from given scope - * @param {Scope} scope The initial scope - * @returns {Scope} The scope of code path start - * @throws {Error} Unexpected error - */ - function getCodePathStartScope(scope) { - let target = scope; - - while (target) { - if (codePathStartScopes.has(target)) { - return target; - } - target = target.upper; - } - - // Should be unreachable - return null; - } - - /** - * Verify the given scope stack. - * @param {ScopeStack} target The scope stack to verify. - * @returns {void} - */ - function verify(target) { - /** - * Checks whether the given identifier is used in the segment. - * @param {CodePathSegment} segment The code path segment. - * @param {Identifier} identifier The identifier to check. - * @returns {boolean} `true` if the identifier is used in the segment. - */ - function isIdentifierUsedInSegment(segment, identifier) { - const segmentInfo = target.segments[segment.id]; - - return ( - segmentInfo.first && - segmentInfo.last && - segmentInfo.first.range[0] <= identifier.range[0] && - identifier.range[1] <= segmentInfo.last.range[1] - ); - } - - /** - * Verifies whether the given assignment info is an used assignment. - * Report if it is an unused assignment. - * @param {AssignmentInfo} targetAssignment The assignment info to verify. - * @param {AssignmentInfo[]} allAssignments The list of all assignment info for variables. - * @returns {void} - */ - function verifyAssignmentIsUsed(targetAssignment, allAssignments) { - // Skip assignment if it is in a try block. - const isAssignmentInTryBlock = target.tryStatementBlocks.some( - tryBlock => - tryBlock.range[0] <= - targetAssignment.identifier.range[0] && - targetAssignment.identifier.range[1] <= - tryBlock.range[1], - ); - - if (isAssignmentInTryBlock) { - return; - } - - /** - * @typedef {Object} SubsequentSegmentData - * @property {CodePathSegment} segment The code path segment - * @property {AssignmentInfo} [assignment] The first occurrence of the assignment within the segment. - * There is no need to check if the variable is used after this assignment, - * as the value it was assigned will be used. - */ - - /** - * Information used in `getSubsequentSegments()`. - * To avoid unnecessary iterations, cache information that has already been iterated over, - * and if additional iterations are needed, start iterating from the retained position. - */ - const subsequentSegmentData = { - /** - * Cache of subsequent segment information list that have already been iterated. - * @type {SubsequentSegmentData[]} - */ - results: [], - - /** - * Subsequent segments that have already been iterated on. Used to avoid infinite loops. - * @type {Set} - */ - subsequentSegments: new Set(), - - /** - * Unexplored code path segment. - * If additional iterations are needed, consume this information and iterate. - * @type {CodePathSegment[]} - */ - queueSegments: targetAssignment.segments.flatMap( - segment => segment.nextSegments, - ), - }; - - /** - * Gets the subsequent segments from the segment of - * the assignment currently being validated (targetAssignment). - * @returns {Iterable} the subsequent segments - */ - function* getSubsequentSegments() { - yield* subsequentSegmentData.results; - - while (subsequentSegmentData.queueSegments.length > 0) { - const nextSegment = - subsequentSegmentData.queueSegments.shift(); - - if ( - subsequentSegmentData.subsequentSegments.has( - nextSegment, - ) - ) { - continue; - } - subsequentSegmentData.subsequentSegments.add( - nextSegment, - ); - - const assignmentInSegment = allAssignments.find( - otherAssignment => - otherAssignment.segments.includes( - nextSegment, - ) && - !isIdentifierUsedBetweenAssignedAndEqualSign( - otherAssignment, - targetAssignment.identifier, - ), - ); - - if (!assignmentInSegment) { - /* - * Stores the next segment to explore. - * If `assignmentInSegment` exists, - * we are guarding it because we don't need to explore the next segment. - */ - subsequentSegmentData.queueSegments.push( - ...nextSegment.nextSegments, - ); - } - - /** @type {SubsequentSegmentData} */ - const result = { - segment: nextSegment, - assignment: assignmentInSegment, - }; - - subsequentSegmentData.results.push(result); - yield result; - } - } - - if ( - targetAssignment.variable.references.some( - ref => ref.identifier.type !== "Identifier", - ) - ) { - /** - * Skip checking for a variable that has at least one non-identifier reference. - * It's generated by plugins and cannot be handled reliably in the core rule. - */ - return; - } - - const readReferences = - targetAssignment.variable.references.filter(reference => - reference.isRead(), - ); - - if (!readReferences.length) { - /* - * It is not just an unnecessary assignment, but an unnecessary (unused) variable - * and thus should not be reported by this rule because it is reported by `no-unused-vars`. - */ - return; - } - - /** - * Other assignment on the current segment and after current assignment. - */ - const otherAssignmentAfterTargetAssignment = - allAssignments.find(assignment => { - if ( - assignment === targetAssignment || - (assignment.segments.length && - assignment.segments.every( - segment => - !targetAssignment.segments.includes( - segment, - ), - )) - ) { - return false; - } - if ( - isIdentifierEvaluatedAfterAssignment( - targetAssignment, - assignment.identifier, - ) - ) { - return true; - } - if ( - assignment.expression && - assignment.expression.range[0] <= - targetAssignment.identifier.range[0] && - targetAssignment.identifier.range[1] <= - assignment.expression.range[1] - ) { - /* - * The target assignment is in an expression that is evaluated before the assignment. - * e.g. x=(x=1); - * ^^^ targetAssignment - * ^^^^^^^ assignment - */ - return true; - } - - return false; - }); - - for (const reference of readReferences) { - /* - * If the scope of the reference is outside the current code path scope, - * we cannot track whether this assignment is not used. - * For example, it can also be called asynchronously. - */ - if ( - target.scope !== getCodePathStartScope(reference.from) - ) { - return; - } - - // Checks if it is used in the same segment as the target assignment. - if ( - isIdentifierEvaluatedAfterAssignment( - targetAssignment, - reference.identifier, - ) && - (isIdentifierUsedBetweenAssignedAndEqualSign( - targetAssignment, - reference.identifier, - ) || - targetAssignment.segments.some(segment => - isIdentifierUsedInSegment( - segment, - reference.identifier, - ), - )) - ) { - if ( - otherAssignmentAfterTargetAssignment && - isIdentifierEvaluatedAfterAssignment( - otherAssignmentAfterTargetAssignment, - reference.identifier, - ) - ) { - // There was another assignment before the reference. Therefore, it has not been used yet. - continue; - } - - // Uses in statements after the written identifier. - return; - } - - if (otherAssignmentAfterTargetAssignment) { - /* - * The assignment was followed by another assignment in the same segment. - * Therefore, there is no need to check the next segment. - */ - continue; - } - - // Check subsequent segments. - for (const subsequentSegment of getSubsequentSegments()) { - if ( - isIdentifierUsedInSegment( - subsequentSegment.segment, - reference.identifier, - ) - ) { - if ( - subsequentSegment.assignment && - isIdentifierEvaluatedAfterAssignment( - subsequentSegment.assignment, - reference.identifier, - ) - ) { - // There was another assignment before the reference. Therefore, it has not been used yet. - continue; - } - - // It is used - return; - } - } - } - context.report({ - node: targetAssignment.identifier, - messageId: "unnecessaryAssignment", - }); - } - - // Verify that each assignment in the code path is used. - for (const assignments of target.assignments.values()) { - assignments.sort( - (a, b) => a.identifier.range[0] - b.identifier.range[0], - ); - for (const assignment of assignments) { - verifyAssignmentIsUsed(assignment, assignments); - } - } - } - - return { - onCodePathStart(codePath, node) { - const scope = sourceCode.getScope(node); - - scopeStack = { - upper: scopeStack, - codePath, - scope, - segments: Object.create(null), - currentSegments: new Set(), - assignments: new Map(), - tryStatementBlocks: [], - }; - codePathStartScopes.add(scopeStack.scope); - }, - onCodePathEnd() { - verify(scopeStack); - - scopeStack = scopeStack.upper; - }, - onCodePathSegmentStart(segment) { - const segmentInfo = { segment, first: null, last: null }; - - scopeStack.segments[segment.id] = segmentInfo; - scopeStack.currentSegments.add(segment); - }, - onCodePathSegmentEnd(segment) { - scopeStack.currentSegments.delete(segment); - }, - TryStatement(node) { - scopeStack.tryStatementBlocks.push(node.block); - }, - Identifier(node) { - for (const segment of scopeStack.currentSegments) { - const segmentInfo = scopeStack.segments[segment.id]; - - if (!segmentInfo.first) { - segmentInfo.first = node; - } - segmentInfo.last = node; - } - }, - ":matches(VariableDeclarator[init!=null], AssignmentExpression, UpdateExpression):exit"( - node, - ) { - if (scopeStack.currentSegments.size === 0) { - // Ignore unreachable segments - return; - } - - const assignments = scopeStack.assignments; - - let pattern; - let expression = null; - - if (node.type === "VariableDeclarator") { - pattern = node.id; - expression = node.init; - } else if (node.type === "AssignmentExpression") { - pattern = node.left; - expression = node.right; - } else { - // UpdateExpression - pattern = node.argument; - } - - for (const identifier of extractIdentifiersFromPattern( - pattern, - )) { - const scope = sourceCode.getScope(identifier); - - /** @type {Variable} */ - const variable = findVariable(scope, identifier); - - if (!variable) { - continue; - } - - // We don't know where global variables are used. - if ( - variable.scope.type === "global" && - variable.defs.length === 0 - ) { - continue; - } - - /* - * If the scope of the variable is outside the current code path scope, - * we cannot track whether this assignment is not used. - */ - if ( - scopeStack.scope !== - getCodePathStartScope(variable.scope) - ) { - continue; - } - - // Variables marked by `markVariableAsUsed()` or - // exported by "exported" block comment. - if (variable.eslintUsed) { - continue; - } - - // Variables exported by ESM export syntax - if (variable.scope.type === "module") { - if ( - variable.defs.some( - def => - (def.type === "Variable" && - def.parent.parent.type === - "ExportNamedDeclaration") || - (def.type === "FunctionName" && - (def.node.parent.type === - "ExportNamedDeclaration" || - def.node.parent.type === - "ExportDefaultDeclaration")) || - (def.type === "ClassName" && - (def.node.parent.type === - "ExportNamedDeclaration" || - def.node.parent.type === - "ExportDefaultDeclaration")), - ) - ) { - continue; - } - if ( - variable.references.some( - reference => - reference.identifier.parent.type === - "ExportSpecifier", - ) - ) { - // It have `export { ... }` reference. - continue; - } - } - - let list = assignments.get(variable); - - if (!list) { - list = []; - assignments.set(variable, list); - } - list.push({ - variable, - identifier, - node, - expression, - segments: [...scopeStack.currentSegments], - }); - } - }, - }; - }, -}; diff --git a/node_modules/eslint/lib/rules/no-useless-backreference.js b/node_modules/eslint/lib/rules/no-useless-backreference.js index 73cce577a..7ca43c8b2 100644 --- a/node_modules/eslint/lib/rules/no-useless-backreference.js +++ b/node_modules/eslint/lib/rules/no-useless-backreference.js @@ -9,12 +9,7 @@ // Requirements //------------------------------------------------------------------------------ -const { - CALL, - CONSTRUCT, - ReferenceTracker, - getStringIfConstant, -} = require("@eslint-community/eslint-utils"); +const { CALL, CONSTRUCT, ReferenceTracker, getStringIfConstant } = require("@eslint-community/eslint-utils"); const { RegExpParser, visitRegExpAST } = require("@eslint-community/regexpp"); //------------------------------------------------------------------------------ @@ -29,15 +24,15 @@ const parser = new RegExpParser(); * @returns {regexpp.Node[]} Array that starts with the given node and ends with the root node. */ function getPathToRoot(node) { - const path = []; - let current = node; + const path = []; + let current = node; - do { - path.push(current); - current = current.parent; - } while (current); + do { + path.push(current); + current = current.parent; + } while (current); - return path; + return path; } /** @@ -46,10 +41,8 @@ function getPathToRoot(node) { * @returns {boolean} `true` if it is a lookaround node. */ function isLookaround(node) { - return ( - node.type === "Assertion" && - (node.kind === "lookahead" || node.kind === "lookbehind") - ); + return node.type === "Assertion" && + (node.kind === "lookahead" || node.kind === "lookbehind"); } /** @@ -58,206 +51,144 @@ function isLookaround(node) { * @returns {boolean} `true` if it is a negative lookaround node. */ function isNegativeLookaround(node) { - return isLookaround(node) && node.negate; + return isLookaround(node) && node.negate; } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - docs: { - description: - "Disallow useless backreferences in regular expressions", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-useless-backreference", - }, - - schema: [], - - messages: { - nested: "Backreference '{{ bref }}' will be ignored. It references group '{{ group }}'{{ otherGroups }} from within that group.", - forward: - "Backreference '{{ bref }}' will be ignored. It references group '{{ group }}'{{ otherGroups }} which appears later in the pattern.", - backward: - "Backreference '{{ bref }}' will be ignored. It references group '{{ group }}'{{ otherGroups }} which appears before in the same lookbehind.", - disjunctive: - "Backreference '{{ bref }}' will be ignored. It references group '{{ group }}'{{ otherGroups }} which is in another alternative.", - intoNegativeLookaround: - "Backreference '{{ bref }}' will be ignored. It references group '{{ group }}'{{ otherGroups }} which is in a negative lookaround.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - /** - * Checks and reports useless backreferences in the given regular expression. - * @param {ASTNode} node Node that represents regular expression. A regex literal or RegExp constructor call. - * @param {string} pattern Regular expression pattern. - * @param {string} flags Regular expression flags. - * @returns {void} - */ - function checkRegex(node, pattern, flags) { - let regExpAST; - - try { - regExpAST = parser.parsePattern(pattern, 0, pattern.length, { - unicode: flags.includes("u"), - unicodeSets: flags.includes("v"), - }); - } catch { - // Ignore regular expressions with syntax errors - return; - } - - visitRegExpAST(regExpAST, { - onBackreferenceEnter(bref) { - const groups = [bref.resolved].flat(), - brefPath = getPathToRoot(bref); - - const problems = groups.map(group => { - const groupPath = getPathToRoot(group); - - if (brefPath.includes(group)) { - // group is bref's ancestor => bref is nested ('nested reference') => group hasn't matched yet when bref starts to match. - return { - messageId: "nested", - group, - }; - } - - // Start from the root to find the lowest common ancestor. - let i = brefPath.length - 1, - j = groupPath.length - 1; - - do { - i--; - j--; - } while (brefPath[i] === groupPath[j]); - - const indexOfLowestCommonAncestor = j + 1, - groupCut = groupPath.slice( - 0, - indexOfLowestCommonAncestor, - ), - commonPath = groupPath.slice( - indexOfLowestCommonAncestor, - ), - lowestCommonLookaround = - commonPath.find(isLookaround), - isMatchingBackward = - lowestCommonLookaround && - lowestCommonLookaround.kind === "lookbehind"; - - if (groupCut.at(-1).type === "Alternative") { - // group's and bref's ancestor nodes below the lowest common ancestor are sibling alternatives => they're disjunctive. - return { - messageId: "disjunctive", - group, - }; - } - if (!isMatchingBackward && bref.end <= group.start) { - // bref is left, group is right ('forward reference') => group hasn't matched yet when bref starts to match. - return { - messageId: "forward", - group, - }; - } - if (isMatchingBackward && group.end <= bref.start) { - // the opposite of the previous when the regex is matching backward in a lookbehind context. - return { - messageId: "backward", - group, - }; - } - if (groupCut.some(isNegativeLookaround)) { - // group is in a negative lookaround which isn't bref's ancestor => group has already failed when bref starts to match. - return { - messageId: "intoNegativeLookaround", - group, - }; - } - - return null; - }); - - if ( - problems.length === 0 || - problems.some(problem => !problem) - ) { - // If there are no problems or no problems with any group then do not report it. - return; - } - - let problemsToReport; - - // Gets problems that appear in the same disjunction. - const problemsInSameDisjunction = problems.filter( - problem => problem.messageId !== "disjunctive", - ); - - if (problemsInSameDisjunction.length) { - // Only report problems that appear in the same disjunction. - problemsToReport = problemsInSameDisjunction; - } else { - // If all groups appear in different disjunctions, report it. - problemsToReport = problems; - } - - const [{ messageId, group }, ...other] = problemsToReport; - let otherGroups = ""; - - if (other.length === 1) { - otherGroups = " and another group"; - } else if (other.length > 1) { - otherGroups = ` and other ${other.length} groups`; - } - context.report({ - node, - messageId, - data: { - bref: bref.raw, - group: group.raw, - otherGroups, - }, - }); - }, - }); - } - - return { - "Literal[regex]"(node) { - const { pattern, flags } = node.regex; - - checkRegex(node, pattern, flags); - }, - Program(node) { - const scope = sourceCode.getScope(node), - tracker = new ReferenceTracker(scope), - traceMap = { - RegExp: { - [CALL]: true, - [CONSTRUCT]: true, - }, - }; - - for (const { node: refNode } of tracker.iterateGlobalReferences( - traceMap, - )) { - const [patternNode, flagsNode] = refNode.arguments, - pattern = getStringIfConstant(patternNode, scope), - flags = getStringIfConstant(flagsNode, scope); - - if (typeof pattern === "string") { - checkRegex(refNode, pattern, flags || ""); - } - } - }, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow useless backreferences in regular expressions", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-useless-backreference" + }, + + schema: [], + + messages: { + nested: "Backreference '{{ bref }}' will be ignored. It references group '{{ group }}' from within that group.", + forward: "Backreference '{{ bref }}' will be ignored. It references group '{{ group }}' which appears later in the pattern.", + backward: "Backreference '{{ bref }}' will be ignored. It references group '{{ group }}' which appears before in the same lookbehind.", + disjunctive: "Backreference '{{ bref }}' will be ignored. It references group '{{ group }}' which is in another alternative.", + intoNegativeLookaround: "Backreference '{{ bref }}' will be ignored. It references group '{{ group }}' which is in a negative lookaround." + } + }, + + create(context) { + + const sourceCode = context.sourceCode; + + /** + * Checks and reports useless backreferences in the given regular expression. + * @param {ASTNode} node Node that represents regular expression. A regex literal or RegExp constructor call. + * @param {string} pattern Regular expression pattern. + * @param {string} flags Regular expression flags. + * @returns {void} + */ + function checkRegex(node, pattern, flags) { + let regExpAST; + + try { + regExpAST = parser.parsePattern(pattern, 0, pattern.length, { unicode: flags.includes("u"), unicodeSets: flags.includes("v") }); + } catch { + + // Ignore regular expressions with syntax errors + return; + } + + visitRegExpAST(regExpAST, { + onBackreferenceEnter(bref) { + const group = bref.resolved, + brefPath = getPathToRoot(bref), + groupPath = getPathToRoot(group); + let messageId = null; + + if (brefPath.includes(group)) { + + // group is bref's ancestor => bref is nested ('nested reference') => group hasn't matched yet when bref starts to match. + messageId = "nested"; + } else { + + // Start from the root to find the lowest common ancestor. + let i = brefPath.length - 1, + j = groupPath.length - 1; + + do { + i--; + j--; + } while (brefPath[i] === groupPath[j]); + + const indexOfLowestCommonAncestor = j + 1, + groupCut = groupPath.slice(0, indexOfLowestCommonAncestor), + commonPath = groupPath.slice(indexOfLowestCommonAncestor), + lowestCommonLookaround = commonPath.find(isLookaround), + isMatchingBackward = lowestCommonLookaround && lowestCommonLookaround.kind === "lookbehind"; + + if (!isMatchingBackward && bref.end <= group.start) { + + // bref is left, group is right ('forward reference') => group hasn't matched yet when bref starts to match. + messageId = "forward"; + } else if (isMatchingBackward && group.end <= bref.start) { + + // the opposite of the previous when the regex is matching backward in a lookbehind context. + messageId = "backward"; + } else if (groupCut[groupCut.length - 1].type === "Alternative") { + + // group's and bref's ancestor nodes below the lowest common ancestor are sibling alternatives => they're disjunctive. + messageId = "disjunctive"; + } else if (groupCut.some(isNegativeLookaround)) { + + // group is in a negative lookaround which isn't bref's ancestor => group has already failed when bref starts to match. + messageId = "intoNegativeLookaround"; + } + } + + if (messageId) { + context.report({ + node, + messageId, + data: { + bref: bref.raw, + group: group.raw + } + }); + } + } + }); + } + + return { + "Literal[regex]"(node) { + const { pattern, flags } = node.regex; + + checkRegex(node, pattern, flags); + }, + Program(node) { + const scope = sourceCode.getScope(node), + tracker = new ReferenceTracker(scope), + traceMap = { + RegExp: { + [CALL]: true, + [CONSTRUCT]: true + } + }; + + for (const { node: refNode } of tracker.iterateGlobalReferences(traceMap)) { + const [patternNode, flagsNode] = refNode.arguments, + pattern = getStringIfConstant(patternNode, scope), + flags = getStringIfConstant(flagsNode, scope); + + if (typeof pattern === "string") { + checkRegex(refNode, pattern, flags || ""); + } + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-useless-call.js b/node_modules/eslint/lib/rules/no-useless-call.js index 2a04ad290..dea2b47a4 100644 --- a/node_modules/eslint/lib/rules/no-useless-call.js +++ b/node_modules/eslint/lib/rules/no-useless-call.js @@ -17,19 +17,20 @@ const astUtils = require("./utils/ast-utils"); * @returns {boolean} Whether or not the node is a `.call()`/`.apply()`. */ function isCallOrNonVariadicApply(node) { - const callee = astUtils.skipChainExpression(node.callee); - - return ( - callee.type === "MemberExpression" && - callee.property.type === "Identifier" && - callee.computed === false && - ((callee.property.name === "call" && node.arguments.length >= 1) || - (callee.property.name === "apply" && - node.arguments.length === 2 && - node.arguments[1].type === "ArrayExpression")) - ); + const callee = astUtils.skipChainExpression(node.callee); + + return ( + callee.type === "MemberExpression" && + callee.property.type === "Identifier" && + callee.computed === false && + ( + (callee.property.name === "call" && node.arguments.length >= 1) || + (callee.property.name === "apply" && node.arguments.length === 2 && node.arguments[1].type === "ArrayExpression") + ) + ); } + /** * Checks whether or not `thisArg` is not changed by `.call()`/`.apply()`. * @param {ASTNode|null} expectedThis The node that is the owner of the applied function. @@ -38,58 +39,52 @@ function isCallOrNonVariadicApply(node) { * @returns {boolean} Whether or not `thisArg` is not changed by `.call()`/`.apply()`. */ function isValidThisArg(expectedThis, thisArg, sourceCode) { - if (!expectedThis) { - return astUtils.isNullOrUndefined(thisArg); - } - return astUtils.equalTokens(expectedThis, thisArg, sourceCode); + if (!expectedThis) { + return astUtils.isNullOrUndefined(thisArg); + } + return astUtils.equalTokens(expectedThis, thisArg, sourceCode); } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: - "Disallow unnecessary calls to `.call()` and `.apply()`", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-useless-call", - }, - - schema: [], - - messages: { - unnecessaryCall: "Unnecessary '.{{name}}()'.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - return { - CallExpression(node) { - if (!isCallOrNonVariadicApply(node)) { - return; - } - - const callee = astUtils.skipChainExpression(node.callee); - const applied = astUtils.skipChainExpression(callee.object); - const expectedThis = - applied.type === "MemberExpression" ? applied.object : null; - const thisArg = node.arguments[0]; - - if (isValidThisArg(expectedThis, thisArg, sourceCode)) { - context.report({ - node, - messageId: "unnecessaryCall", - data: { name: callee.property.name }, - }); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow unnecessary calls to `.call()` and `.apply()`", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-useless-call" + }, + + schema: [], + + messages: { + unnecessaryCall: "Unnecessary '.{{name}}()'." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + + return { + CallExpression(node) { + if (!isCallOrNonVariadicApply(node)) { + return; + } + + const callee = astUtils.skipChainExpression(node.callee); + const applied = astUtils.skipChainExpression(callee.object); + const expectedThis = (applied.type === "MemberExpression") ? applied.object : null; + const thisArg = node.arguments[0]; + + if (isValidThisArg(expectedThis, thisArg, sourceCode)) { + context.report({ node, messageId: "unnecessaryCall", data: { name: callee.property.name } }); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-useless-catch.js b/node_modules/eslint/lib/rules/no-useless-catch.js index e3df3093c..e02013db6 100644 --- a/node_modules/eslint/lib/rules/no-useless-catch.js +++ b/node_modules/eslint/lib/rules/no-useless-catch.js @@ -9,49 +9,49 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", + meta: { + type: "suggestion", - docs: { - description: "Disallow unnecessary `catch` clauses", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-useless-catch", - }, + docs: { + description: "Disallow unnecessary `catch` clauses", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-useless-catch" + }, - schema: [], + schema: [], - messages: { - unnecessaryCatchClause: "Unnecessary catch clause.", - unnecessaryCatch: "Unnecessary try/catch wrapper.", - }, - }, + messages: { + unnecessaryCatchClause: "Unnecessary catch clause.", + unnecessaryCatch: "Unnecessary try/catch wrapper." + } + }, - create(context) { - return { - CatchClause(node) { - if ( - node.param && - node.param.type === "Identifier" && - node.body.body.length && - node.body.body[0].type === "ThrowStatement" && - node.body.body[0].argument.type === "Identifier" && - node.body.body[0].argument.name === node.param.name - ) { - if (node.parent.finalizer) { - context.report({ - node, - messageId: "unnecessaryCatchClause", - }); - } else { - context.report({ - node: node.parent, - messageId: "unnecessaryCatch", - }); - } - } - }, - }; - }, + create(context) { + return { + CatchClause(node) { + if ( + node.param && + node.param.type === "Identifier" && + node.body.body.length && + node.body.body[0].type === "ThrowStatement" && + node.body.body[0].argument.type === "Identifier" && + node.body.body[0].argument.name === node.param.name + ) { + if (node.parent.finalizer) { + context.report({ + node, + messageId: "unnecessaryCatchClause" + }); + } else { + context.report({ + node: node.parent, + messageId: "unnecessaryCatch" + }); + } + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-useless-computed-key.js b/node_modules/eslint/lib/rules/no-useless-computed-key.js index 3c61c7672..f2d9f3341 100644 --- a/node_modules/eslint/lib/rules/no-useless-computed-key.js +++ b/node_modules/eslint/lib/rules/no-useless-computed-key.js @@ -40,165 +40,129 @@ const astUtils = require("./utils/ast-utils"); * @returns {void} `true` if the node has useless computed key. */ function hasUselessComputedKey(node) { - if (!node.computed) { - return false; - } + if (!node.computed) { + return false; + } - const { key } = node; + const { key } = node; - if (key.type !== "Literal") { - return false; - } + if (key.type !== "Literal") { + return false; + } - const { value } = key; + const { value } = key; - if (typeof value !== "number" && typeof value !== "string") { - return false; - } + if (typeof value !== "number" && typeof value !== "string") { + return false; + } - switch (node.type) { - case "Property": - if (node.parent.type === "ObjectExpression") { - return value !== "__proto__"; - } - return true; + switch (node.type) { + case "Property": + return value !== "__proto__"; - case "PropertyDefinition": - if (node.static) { - return value !== "constructor" && value !== "prototype"; - } + case "PropertyDefinition": + if (node.static) { + return value !== "constructor" && value !== "prototype"; + } - return value !== "constructor"; + return value !== "constructor"; - case "MethodDefinition": - if (node.static) { - return value !== "prototype"; - } + case "MethodDefinition": + if (node.static) { + return value !== "prototype"; + } - return value !== "constructor"; + return value !== "constructor"; + + /* c8 ignore next */ + default: + throw new Error(`Unexpected node type: ${node.type}`); + } - /* c8 ignore next */ - default: - throw new Error(`Unexpected node type: ${node.type}`); - } } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - defaultOptions: [ - { - enforceForClassMembers: true, - }, - ], - - docs: { - description: - "Disallow unnecessary computed property keys in objects and classes", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/no-useless-computed-key", - }, - - schema: [ - { - type: "object", - properties: { - enforceForClassMembers: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - fixable: "code", - - messages: { - unnecessarilyComputedProperty: - "Unnecessarily computed property [{{property}}] found.", - }, - }, - create(context) { - const sourceCode = context.sourceCode; - const [{ enforceForClassMembers }] = context.options; - - /** - * Reports a given node if it violated this rule. - * @param {ASTNode} node The node to check. - * @returns {void} - */ - function check(node) { - if (hasUselessComputedKey(node)) { - const { key } = node; - - context.report({ - node, - messageId: "unnecessarilyComputedProperty", - data: { property: sourceCode.getText(key) }, - fix(fixer) { - const leftSquareBracket = sourceCode.getTokenBefore( - key, - astUtils.isOpeningBracketToken, - ); - const rightSquareBracket = sourceCode.getTokenAfter( - key, - astUtils.isClosingBracketToken, - ); - - // If there are comments between the brackets and the property name, don't do a fix. - if ( - sourceCode.commentsExistBetween( - leftSquareBracket, - rightSquareBracket, - ) - ) { - return null; - } - - const tokenBeforeLeftBracket = - sourceCode.getTokenBefore(leftSquareBracket); - - // Insert a space before the key to avoid changing identifiers, e.g. ({ get[2]() {} }) to ({ get2() {} }) - const needsSpaceBeforeKey = - tokenBeforeLeftBracket.range[1] === - leftSquareBracket.range[0] && - !astUtils.canTokensBeAdjacent( - tokenBeforeLeftBracket, - sourceCode.getFirstToken(key), - ); - - const replacementKey = - (needsSpaceBeforeKey ? " " : "") + key.raw; - - return fixer.replaceTextRange( - [ - leftSquareBracket.range[0], - rightSquareBracket.range[1], - ], - replacementKey, - ); - }, - }); - } - } - - /** - * A no-op function to act as placeholder for checking a node when the `enforceForClassMembers` option is `false`. - * @returns {void} - * @private - */ - function noop() {} - - return { - Property: check, - MethodDefinition: enforceForClassMembers ? check : noop, - PropertyDefinition: enforceForClassMembers ? check : noop, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow unnecessary computed property keys in objects and classes", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-useless-computed-key" + }, + + schema: [{ + type: "object", + properties: { + enforceForClassMembers: { + type: "boolean", + default: false + } + }, + additionalProperties: false + }], + fixable: "code", + + messages: { + unnecessarilyComputedProperty: "Unnecessarily computed property [{{property}}] found." + } + }, + create(context) { + const sourceCode = context.sourceCode; + const enforceForClassMembers = context.options[0] && context.options[0].enforceForClassMembers; + + /** + * Reports a given node if it violated this rule. + * @param {ASTNode} node The node to check. + * @returns {void} + */ + function check(node) { + if (hasUselessComputedKey(node)) { + const { key } = node; + + context.report({ + node, + messageId: "unnecessarilyComputedProperty", + data: { property: sourceCode.getText(key) }, + fix(fixer) { + const leftSquareBracket = sourceCode.getTokenBefore(key, astUtils.isOpeningBracketToken); + const rightSquareBracket = sourceCode.getTokenAfter(key, astUtils.isClosingBracketToken); + + // If there are comments between the brackets and the property name, don't do a fix. + if (sourceCode.commentsExistBetween(leftSquareBracket, rightSquareBracket)) { + return null; + } + + const tokenBeforeLeftBracket = sourceCode.getTokenBefore(leftSquareBracket); + + // Insert a space before the key to avoid changing identifiers, e.g. ({ get[2]() {} }) to ({ get2() {} }) + const needsSpaceBeforeKey = tokenBeforeLeftBracket.range[1] === leftSquareBracket.range[0] && + !astUtils.canTokensBeAdjacent(tokenBeforeLeftBracket, sourceCode.getFirstToken(key)); + + const replacementKey = (needsSpaceBeforeKey ? " " : "") + key.raw; + + return fixer.replaceTextRange([leftSquareBracket.range[0], rightSquareBracket.range[1]], replacementKey); + } + }); + } + } + + /** + * A no-op function to act as placeholder for checking a node when the `enforceForClassMembers` option is `false`. + * @returns {void} + * @private + */ + function noop() {} + + return { + Property: check, + MethodDefinition: enforceForClassMembers ? check : noop, + PropertyDefinition: enforceForClassMembers ? check : noop + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-useless-concat.js b/node_modules/eslint/lib/rules/no-useless-concat.js index d1d127485..c566c62be 100644 --- a/node_modules/eslint/lib/rules/no-useless-concat.js +++ b/node_modules/eslint/lib/rules/no-useless-concat.js @@ -20,7 +20,7 @@ const astUtils = require("./utils/ast-utils"); * @returns {boolean} `true` if the node is a concatenation. */ function isConcatenation(node) { - return node.type === "BinaryExpression" && node.operator === "+"; + return node.type === "BinaryExpression" && node.operator === "+"; } /** @@ -29,7 +29,7 @@ function isConcatenation(node) { * @returns {boolean} `true` if the token is a `+` token. */ function isConcatOperatorToken(token) { - return token.value === "+" && token.type === "Punctuator"; + return token.value === "+" && token.type === "Punctuator"; } /** @@ -38,12 +38,12 @@ function isConcatOperatorToken(token) { * @returns {ASTNode} node */ function getLeft(node) { - let left = node.left; + let left = node.left; - while (isConcatenation(left)) { - left = left.right; - } - return left; + while (isConcatenation(left)) { + left = left.right; + } + return left; } /** @@ -52,70 +52,64 @@ function getLeft(node) { * @returns {ASTNode} node */ function getRight(node) { - let right = node.right; + let right = node.right; - while (isConcatenation(right)) { - right = right.left; - } - return right; + while (isConcatenation(right)) { + right = right.left; + } + return right; } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: - "Disallow unnecessary concatenation of literals or template literals", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/no-useless-concat", - }, - - schema: [], - - messages: { - unexpectedConcat: "Unexpected string concatenation of literals.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - return { - BinaryExpression(node) { - // check if not concatenation - if (node.operator !== "+") { - return; - } - - // account for the `foo + "a" + "b"` case - const left = getLeft(node); - const right = getRight(node); - - if ( - astUtils.isStringLiteral(left) && - astUtils.isStringLiteral(right) && - astUtils.isTokenOnSameLine(left, right) - ) { - const operatorToken = sourceCode.getFirstTokenBetween( - left, - right, - isConcatOperatorToken, - ); - - context.report({ - node, - loc: operatorToken.loc, - messageId: "unexpectedConcat", - }); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow unnecessary concatenation of literals or template literals", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-useless-concat" + }, + + schema: [], + + messages: { + unexpectedConcat: "Unexpected string concatenation of literals." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + + return { + BinaryExpression(node) { + + // check if not concatenation + if (node.operator !== "+") { + return; + } + + // account for the `foo + "a" + "b"` case + const left = getLeft(node); + const right = getRight(node); + + if (astUtils.isStringLiteral(left) && + astUtils.isStringLiteral(right) && + astUtils.isTokenOnSameLine(left, right) + ) { + const operatorToken = sourceCode.getFirstTokenBetween(left, right, isConcatOperatorToken); + + context.report({ + node, + loc: operatorToken.loc, + messageId: "unexpectedConcat" + }); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-useless-constructor.js b/node_modules/eslint/lib/rules/no-useless-constructor.js index 123dce31f..2b9c18e51 100644 --- a/node_modules/eslint/lib/rules/no-useless-constructor.js +++ b/node_modules/eslint/lib/rules/no-useless-constructor.js @@ -4,53 +4,22 @@ */ "use strict"; -const astUtils = require("./utils/ast-utils"); - //------------------------------------------------------------------------------ // Helpers //------------------------------------------------------------------------------ -/** - * Checks whether any of a method's parameters have a decorator or are a parameter property. - * @param {ASTNode} node A method definition node. - * @returns {boolean} `true` if any parameter had a decorator or is a parameter property. - */ -function hasDecoratorsOrParameterProperty(node) { - return node.value.params.some( - param => - param.decorators?.length || param.type === "TSParameterProperty", - ); -} - -/** - * Checks whether a node's accessibility makes it not useless. - * @param {ASTNode} node A method definition node. - * @returns {boolean} `true` if the node has a useful accessibility. - */ -function hasUsefulAccessibility(node) { - switch (node.accessibility) { - case "protected": - case "private": - return true; - case "public": - return !!node.parent.parent.superClass; - default: - return false; - } -} - /** * Checks whether a given array of statements is a single call of `super`. * @param {ASTNode[]} body An array of statements to check. * @returns {boolean} `true` if the body is a single call of `super`. */ function isSingleSuperCall(body) { - return ( - body.length === 1 && - body[0].type === "ExpressionStatement" && - body[0].expression.type === "CallExpression" && - body[0].expression.callee.type === "Super" - ); + return ( + body.length === 1 && + body[0].type === "ExpressionStatement" && + body[0].expression.type === "CallExpression" && + body[0].expression.callee.type === "Super" + ); } /** @@ -60,7 +29,7 @@ function isSingleSuperCall(body) { * @returns {boolean} `true` if the node doesn't have any side effects. */ function isSimple(node) { - return node.type === "Identifier" || node.type === "RestElement"; + return node.type === "Identifier" || node.type === "RestElement"; } /** @@ -70,12 +39,12 @@ function isSimple(node) { * @returns {boolean} `true` if the superArgs is `...arguments`. */ function isSpreadArguments(superArgs) { - return ( - superArgs.length === 1 && - superArgs[0].type === "SpreadElement" && - superArgs[0].argument.type === "Identifier" && - superArgs[0].argument.name === "arguments" - ); + return ( + superArgs.length === 1 && + superArgs[0].type === "SpreadElement" && + superArgs[0].argument.type === "Identifier" && + superArgs[0].argument.name === "arguments" + ); } /** @@ -86,11 +55,11 @@ function isSpreadArguments(superArgs) { * name. */ function isValidIdentifierPair(ctorParam, superArg) { - return ( - ctorParam.type === "Identifier" && - superArg.type === "Identifier" && - ctorParam.name === superArg.name - ); + return ( + ctorParam.type === "Identifier" && + superArg.type === "Identifier" && + ctorParam.name === superArg.name + ); } /** @@ -101,11 +70,11 @@ function isValidIdentifierPair(ctorParam, superArg) { * same values. */ function isValidRestSpreadPair(ctorParam, superArg) { - return ( - ctorParam.type === "RestElement" && - superArg.type === "SpreadElement" && - isValidIdentifierPair(ctorParam.argument, superArg.argument) - ); + return ( + ctorParam.type === "RestElement" && + superArg.type === "SpreadElement" && + isValidIdentifierPair(ctorParam.argument, superArg.argument) + ); } /** @@ -115,10 +84,10 @@ function isValidRestSpreadPair(ctorParam, superArg) { * @returns {boolean} `true` if the nodes have the same value or not. */ function isValidPair(ctorParam, superArg) { - return ( - isValidIdentifierPair(ctorParam, superArg) || - isValidRestSpreadPair(ctorParam, superArg) - ); + return ( + isValidIdentifierPair(ctorParam, superArg) || + isValidRestSpreadPair(ctorParam, superArg) + ); } /** @@ -129,17 +98,17 @@ function isValidPair(ctorParam, superArg) { * @returns {boolean} `true` if those have the same values. */ function isPassingThrough(ctorParams, superArgs) { - if (ctorParams.length !== superArgs.length) { - return false; - } + if (ctorParams.length !== superArgs.length) { + return false; + } - for (let i = 0; i < ctorParams.length; ++i) { - if (!isValidPair(ctorParams[i], superArgs[i])) { - return false; - } - } + for (let i = 0; i < ctorParams.length; ++i) { + if (!isValidPair(ctorParams[i], superArgs[i])) { + return false; + } + } - return true; + return true; } /** @@ -149,104 +118,72 @@ function isPassingThrough(ctorParams, superArgs) { * @returns {boolean} true if the constructor body is redundant */ function isRedundantSuperCall(body, ctorParams) { - return ( - isSingleSuperCall(body) && - ctorParams.every(isSimple) && - (isSpreadArguments(body[0].expression.arguments) || - isPassingThrough(ctorParams, body[0].expression.arguments)) - ); + return ( + isSingleSuperCall(body) && + ctorParams.every(isSimple) && + ( + isSpreadArguments(body[0].expression.arguments) || + isPassingThrough(ctorParams, body[0].expression.arguments) + ) + ); } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - dialects: ["javascript", "typescript"], - language: "javascript", - type: "suggestion", - - docs: { - description: "Disallow unnecessary constructors", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-useless-constructor", - }, - - hasSuggestions: true, - - schema: [], - - messages: { - noUselessConstructor: "Useless constructor.", - removeConstructor: "Remove the constructor.", - }, - }, - - create(context) { - /** - * Checks whether a node is a redundant constructor - * @param {ASTNode} node node to check - * @returns {void} - */ - function checkForConstructor(node) { - if ( - node.kind !== "constructor" || - node.value.type !== "FunctionExpression" || - hasDecoratorsOrParameterProperty(node) || - hasUsefulAccessibility(node) - ) { - return; - } - - /* - * Prevent crashing on parsers which do not require class constructor - * to have a body, e.g. typescript and flow - */ - if (!node.value.body) { - return; - } - - const body = node.value.body.body; - const ctorParams = node.value.params; - const superClass = node.parent.parent.superClass; - - if ( - superClass - ? isRedundantSuperCall(body, ctorParams) - : body.length === 0 - ) { - context.report({ - node, - messageId: "noUselessConstructor", - suggest: [ - { - messageId: "removeConstructor", - *fix(fixer) { - const nextToken = - context.sourceCode.getTokenAfter(node); - const addSemiColon = - nextToken.type === "Punctuator" && - nextToken.value === "[" && - astUtils.needsPrecedingSemicolon( - context.sourceCode, - node, - ); - - yield fixer.replaceText( - node, - addSemiColon ? ";" : "", - ); - }, - }, - ], - }); - } - } - - return { - MethodDefinition: checkForConstructor, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow unnecessary constructors", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-useless-constructor" + }, + + schema: [], + + messages: { + noUselessConstructor: "Useless constructor." + } + }, + + create(context) { + + /** + * Checks whether a node is a redundant constructor + * @param {ASTNode} node node to check + * @returns {void} + */ + function checkForConstructor(node) { + if (node.kind !== "constructor") { + return; + } + + /* + * Prevent crashing on parsers which do not require class constructor + * to have a body, e.g. typescript and flow + */ + if (!node.value.body) { + return; + } + + const body = node.value.body.body; + const ctorParams = node.value.params; + const superClass = node.parent.parent.superClass; + + if (superClass ? isRedundantSuperCall(body, ctorParams) : (body.length === 0)) { + context.report({ + node, + messageId: "noUselessConstructor" + }); + } + } + + return { + MethodDefinition: checkForConstructor + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-useless-escape.js b/node_modules/eslint/lib/rules/no-useless-escape.js index 480b51597..0e0f6f09f 100644 --- a/node_modules/eslint/lib/rules/no-useless-escape.js +++ b/node_modules/eslint/lib/rules/no-useless-escape.js @@ -23,384 +23,311 @@ const { RegExpParser, visitRegExpAST } = require("@eslint-community/regexpp"); * @returns {Set} The union of the two sets */ function union(setA, setB) { - return new Set( - (function* () { - yield* setA; - yield* setB; - })(), - ); + return new Set(function *() { + yield* setA; + yield* setB; + }()); } const VALID_STRING_ESCAPES = union(new Set("\\nrvtbfux"), astUtils.LINEBREAKS); const REGEX_GENERAL_ESCAPES = new Set("\\bcdDfnpPrsStvwWxu0123456789]"); -const REGEX_NON_CHARCLASS_ESCAPES = union( - REGEX_GENERAL_ESCAPES, - new Set("^/.$*+?[{}|()Bk"), -); +const REGEX_NON_CHARCLASS_ESCAPES = union(REGEX_GENERAL_ESCAPES, new Set("^/.$*+?[{}|()Bk")); /* * Set of characters that require escaping in character classes in `unicodeSets` mode. * ( ) [ ] { } / - \ | are ClassSetSyntaxCharacter */ -const REGEX_CLASSSET_CHARACTER_ESCAPES = union( - REGEX_GENERAL_ESCAPES, - new Set("q/[{}|()-"), -); +const REGEX_CLASSSET_CHARACTER_ESCAPES = union(REGEX_GENERAL_ESCAPES, new Set("q/[{}|()-")); /* * A single character set of ClassSetReservedDoublePunctuator. * && !! ## $$ %% ** ++ ,, .. :: ;; << == >> ?? @@ ^^ `` ~~ are ClassSetReservedDoublePunctuator */ -const REGEX_CLASS_SET_RESERVED_DOUBLE_PUNCTUATOR = new Set( - "!#$%&*+,.:;<=>?@^`~", -); +const REGEX_CLASS_SET_RESERVED_DOUBLE_PUNCTUATOR = new Set("!#$%&*+,.:;<=>?@^`~"); -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - defaultOptions: [ - { - allowRegexCharacters: [], - }, - ], - - docs: { - description: "Disallow unnecessary escape characters", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-useless-escape", - }, - - hasSuggestions: true, - - messages: { - unnecessaryEscape: "Unnecessary escape character: \\{{character}}.", - removeEscape: - "Remove the `\\`. This maintains the current functionality.", - removeEscapeDoNotKeepSemantics: - "Remove the `\\` if it was inserted by mistake.", - escapeBackslash: - "Replace the `\\` with `\\\\` to include the actual backslash character.", - }, - - schema: [ - { - type: "object", - properties: { - allowRegexCharacters: { - type: "array", - items: { - type: "string", - }, - uniqueItems: true, - }, - }, - additionalProperties: false, - }, - ], - }, - - create(context) { - const sourceCode = context.sourceCode; - const [{ allowRegexCharacters }] = context.options; - const parser = new RegExpParser(); - - /** - * Reports a node - * @param {ASTNode} node The node to report - * @param {number} startOffset The backslash's offset from the start of the node - * @param {string} character The uselessly escaped character (not including the backslash) - * @param {boolean} [disableEscapeBackslashSuggest] `true` if escapeBackslash suggestion should be turned off. - * @returns {void} - */ - function report( - node, - startOffset, - character, - disableEscapeBackslashSuggest, - ) { - const rangeStart = node.range[0] + startOffset; - const range = [rangeStart, rangeStart + 1]; - const start = sourceCode.getLocFromIndex(rangeStart); - - context.report({ - node, - loc: { - start, - end: { line: start.line, column: start.column + 1 }, - }, - messageId: "unnecessaryEscape", - data: { character }, - suggest: [ - { - // Removing unnecessary `\` characters in a directive is not guaranteed to maintain functionality. - messageId: astUtils.isDirective(node.parent) - ? "removeEscapeDoNotKeepSemantics" - : "removeEscape", - fix(fixer) { - return fixer.removeRange(range); - }, - }, - ...(disableEscapeBackslashSuggest - ? [] - : [ - { - messageId: "escapeBackslash", - fix(fixer) { - return fixer.insertTextBeforeRange( - range, - "\\", - ); - }, - }, - ]), - ], - }); - } - - /** - * Checks if the escape character in given string slice is unnecessary. - * @private - * @param {ASTNode} node node to validate. - * @param {string} match string slice to validate. - * @returns {void} - */ - function validateString(node, match) { - const isTemplateElement = node.type === "TemplateElement"; - const escapedChar = match[0][1]; - let isUnnecessaryEscape = !VALID_STRING_ESCAPES.has(escapedChar); - let isQuoteEscape; - - if (isTemplateElement) { - isQuoteEscape = escapedChar === "`"; - - if (escapedChar === "$") { - // Warn if `\$` is not followed by `{` - isUnnecessaryEscape = match.input[match.index + 2] !== "{"; - } else if (escapedChar === "{") { - /* - * Warn if `\{` is not preceded by `$`. If preceded by `$`, escaping - * is necessary and the rule should not warn. If preceded by `/$`, the rule - * will warn for the `/$` instead, as it is the first unnecessarily escaped character. - */ - isUnnecessaryEscape = match.input[match.index - 1] !== "$"; - } - } else { - isQuoteEscape = escapedChar === node.raw[0]; - } - - if (isUnnecessaryEscape && !isQuoteEscape) { - report(node, match.index, match[0].slice(1)); - } - } - - /** - * Checks if the escape character in given regexp is unnecessary. - * @private - * @param {ASTNode} node node to validate. - * @returns {void} - */ - function validateRegExp(node) { - const { pattern, flags } = node.regex; - let patternNode; - const unicode = flags.includes("u"); - const unicodeSets = flags.includes("v"); - - try { - patternNode = parser.parsePattern(pattern, 0, pattern.length, { - unicode, - unicodeSets, - }); - } catch { - // Ignore regular expressions with syntax errors - return; - } - - /** @type {(CharacterClass | ExpressionCharacterClass)[]} */ - const characterClassStack = []; - - visitRegExpAST(patternNode, { - onCharacterClassEnter: characterClassNode => - characterClassStack.unshift(characterClassNode), - onCharacterClassLeave: () => characterClassStack.shift(), - onExpressionCharacterClassEnter: characterClassNode => - characterClassStack.unshift(characterClassNode), - onExpressionCharacterClassLeave: () => - characterClassStack.shift(), - onCharacterEnter(characterNode) { - if (!characterNode.raw.startsWith("\\")) { - // It's not an escaped character. - return; - } - - const escapedChar = characterNode.raw.slice(1); - - if ( - escapedChar !== - String.fromCodePoint(characterNode.value) || - allowRegexCharacters.includes(escapedChar) - ) { - // It's a valid escape. - return; - } - let allowedEscapes; - - if (characterClassStack.length) { - allowedEscapes = unicodeSets - ? REGEX_CLASSSET_CHARACTER_ESCAPES - : REGEX_GENERAL_ESCAPES; - } else { - allowedEscapes = REGEX_NON_CHARCLASS_ESCAPES; - } - if (allowedEscapes.has(escapedChar)) { - return; - } - - const reportedIndex = characterNode.start + 1; - let disableEscapeBackslashSuggest = false; - - if (characterClassStack.length) { - const characterClassNode = characterClassStack[0]; - - if (escapedChar === "^") { - /* - * The '^' character is also a special case; it must always be escaped outside of character classes, but - * it only needs to be escaped in character classes if it's at the beginning of the character class. To - * account for this, consider it to be a valid escape character outside of character classes, and filter - * out '^' characters that appear at the start of a character class. - */ - if ( - characterClassNode.start + 1 === - characterNode.start - ) { - return; - } - } - if (!unicodeSets) { - if (escapedChar === "-") { - /* - * The '-' character is a special case, because it's only valid to escape it if it's in a character - * class, and is not at either edge of the character class. To account for this, don't consider '-' - * characters to be valid in general, and filter out '-' characters that appear in the middle of a - * character class. - */ - if ( - characterClassNode.start + 1 !== - characterNode.start && - characterNode.end !== - characterClassNode.end - 1 - ) { - return; - } - } - } else { - // unicodeSets mode - if ( - REGEX_CLASS_SET_RESERVED_DOUBLE_PUNCTUATOR.has( - escapedChar, - ) - ) { - // Escaping is valid if it is a ClassSetReservedDoublePunctuator. - if ( - pattern[characterNode.end] === escapedChar - ) { - return; - } - if ( - pattern[characterNode.start - 1] === - escapedChar - ) { - if (escapedChar !== "^") { - return; - } - - // If the previous character is a `negate` caret(`^`), escape to caret is unnecessary. - - if (!characterClassNode.negate) { - return; - } - const negateCaretIndex = - characterClassNode.start + 1; - - if ( - negateCaretIndex < - characterNode.start - 1 - ) { - return; - } - } - } - - if ( - characterNode.parent.type === - "ClassIntersection" || - characterNode.parent.type === "ClassSubtraction" - ) { - disableEscapeBackslashSuggest = true; - } - } - } - - report( - node, - reportedIndex, - escapedChar, - disableEscapeBackslashSuggest, - ); - }, - }); - } - - /** - * Checks if a node has an escape. - * @param {ASTNode} node node to check. - * @returns {void} - */ - function check(node) { - const isTemplateElement = node.type === "TemplateElement"; - - if ( - isTemplateElement && - node.parent && - node.parent.parent && - node.parent.parent.type === "TaggedTemplateExpression" && - node.parent === node.parent.parent.quasi - ) { - // Don't report tagged template literals, because the backslash character is accessible to the tag function. - return; - } - - if (typeof node.value === "string" || isTemplateElement) { - /* - * JSXAttribute doesn't have any escape sequence: https://facebook.github.io/jsx/. - * In addition, backticks are not supported by JSX yet: https://github.com/facebook/jsx/issues/25. - */ - if ( - node.parent.type === "JSXAttribute" || - node.parent.type === "JSXElement" || - node.parent.type === "JSXFragment" - ) { - return; - } - - const value = isTemplateElement - ? sourceCode.getText(node) - : node.raw; - const pattern = /\\\D/gu; - let match; - - while ((match = pattern.exec(value))) { - validateString(node, match); - } - } else if (node.regex) { - validateRegExp(node); - } - } - - return { - Literal: check, - TemplateElement: check, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow unnecessary escape characters", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-useless-escape" + }, + + hasSuggestions: true, + + messages: { + unnecessaryEscape: "Unnecessary escape character: \\{{character}}.", + removeEscape: "Remove the `\\`. This maintains the current functionality.", + removeEscapeDoNotKeepSemantics: "Remove the `\\` if it was inserted by mistake.", + escapeBackslash: "Replace the `\\` with `\\\\` to include the actual backslash character." + }, + + schema: [] + }, + + create(context) { + const sourceCode = context.sourceCode; + const parser = new RegExpParser(); + + /** + * Reports a node + * @param {ASTNode} node The node to report + * @param {number} startOffset The backslash's offset from the start of the node + * @param {string} character The uselessly escaped character (not including the backslash) + * @param {boolean} [disableEscapeBackslashSuggest] `true` if escapeBackslash suggestion should be turned off. + * @returns {void} + */ + function report(node, startOffset, character, disableEscapeBackslashSuggest) { + const rangeStart = node.range[0] + startOffset; + const range = [rangeStart, rangeStart + 1]; + const start = sourceCode.getLocFromIndex(rangeStart); + + context.report({ + node, + loc: { + start, + end: { line: start.line, column: start.column + 1 } + }, + messageId: "unnecessaryEscape", + data: { character }, + suggest: [ + { + + // Removing unnecessary `\` characters in a directive is not guaranteed to maintain functionality. + messageId: astUtils.isDirective(node.parent) + ? "removeEscapeDoNotKeepSemantics" : "removeEscape", + fix(fixer) { + return fixer.removeRange(range); + } + }, + ...disableEscapeBackslashSuggest + ? [] + : [ + { + messageId: "escapeBackslash", + fix(fixer) { + return fixer.insertTextBeforeRange(range, "\\"); + } + } + ] + ] + }); + } + + /** + * Checks if the escape character in given string slice is unnecessary. + * @private + * @param {ASTNode} node node to validate. + * @param {string} match string slice to validate. + * @returns {void} + */ + function validateString(node, match) { + const isTemplateElement = node.type === "TemplateElement"; + const escapedChar = match[0][1]; + let isUnnecessaryEscape = !VALID_STRING_ESCAPES.has(escapedChar); + let isQuoteEscape; + + if (isTemplateElement) { + isQuoteEscape = escapedChar === "`"; + + if (escapedChar === "$") { + + // Warn if `\$` is not followed by `{` + isUnnecessaryEscape = match.input[match.index + 2] !== "{"; + } else if (escapedChar === "{") { + + /* + * Warn if `\{` is not preceded by `$`. If preceded by `$`, escaping + * is necessary and the rule should not warn. If preceded by `/$`, the rule + * will warn for the `/$` instead, as it is the first unnecessarily escaped character. + */ + isUnnecessaryEscape = match.input[match.index - 1] !== "$"; + } + } else { + isQuoteEscape = escapedChar === node.raw[0]; + } + + if (isUnnecessaryEscape && !isQuoteEscape) { + report(node, match.index, match[0].slice(1)); + } + } + + /** + * Checks if the escape character in given regexp is unnecessary. + * @private + * @param {ASTNode} node node to validate. + * @returns {void} + */ + function validateRegExp(node) { + const { pattern, flags } = node.regex; + let patternNode; + const unicode = flags.includes("u"); + const unicodeSets = flags.includes("v"); + + try { + patternNode = parser.parsePattern(pattern, 0, pattern.length, { unicode, unicodeSets }); + } catch { + + // Ignore regular expressions with syntax errors + return; + } + + /** @type {(CharacterClass | ExpressionCharacterClass)[]} */ + const characterClassStack = []; + + visitRegExpAST(patternNode, { + onCharacterClassEnter: characterClassNode => characterClassStack.unshift(characterClassNode), + onCharacterClassLeave: () => characterClassStack.shift(), + onExpressionCharacterClassEnter: characterClassNode => characterClassStack.unshift(characterClassNode), + onExpressionCharacterClassLeave: () => characterClassStack.shift(), + onCharacterEnter(characterNode) { + if (!characterNode.raw.startsWith("\\")) { + + // It's not an escaped character. + return; + } + + const escapedChar = characterNode.raw.slice(1); + + if (escapedChar !== String.fromCodePoint(characterNode.value)) { + + // It's a valid escape. + return; + } + let allowedEscapes; + + if (characterClassStack.length) { + allowedEscapes = unicodeSets ? REGEX_CLASSSET_CHARACTER_ESCAPES : REGEX_GENERAL_ESCAPES; + } else { + allowedEscapes = REGEX_NON_CHARCLASS_ESCAPES; + } + if (allowedEscapes.has(escapedChar)) { + return; + } + + const reportedIndex = characterNode.start + 1; + let disableEscapeBackslashSuggest = false; + + if (characterClassStack.length) { + const characterClassNode = characterClassStack[0]; + + if (escapedChar === "^") { + + /* + * The '^' character is also a special case; it must always be escaped outside of character classes, but + * it only needs to be escaped in character classes if it's at the beginning of the character class. To + * account for this, consider it to be a valid escape character outside of character classes, and filter + * out '^' characters that appear at the start of a character class. + */ + if (characterClassNode.start + 1 === characterNode.start) { + + return; + } + } + if (!unicodeSets) { + if (escapedChar === "-") { + + /* + * The '-' character is a special case, because it's only valid to escape it if it's in a character + * class, and is not at either edge of the character class. To account for this, don't consider '-' + * characters to be valid in general, and filter out '-' characters that appear in the middle of a + * character class. + */ + if (characterClassNode.start + 1 !== characterNode.start && characterNode.end !== characterClassNode.end - 1) { + + return; + } + } + } else { // unicodeSets mode + if (REGEX_CLASS_SET_RESERVED_DOUBLE_PUNCTUATOR.has(escapedChar)) { + + // Escaping is valid if it is a ClassSetReservedDoublePunctuator. + if (pattern[characterNode.end] === escapedChar) { + return; + } + if (pattern[characterNode.start - 1] === escapedChar) { + if (escapedChar !== "^") { + return; + } + + // If the previous character is a `negate` caret(`^`), escape to caret is unnecessary. + + if (!characterClassNode.negate) { + return; + } + const negateCaretIndex = characterClassNode.start + 1; + + if (negateCaretIndex < characterNode.start - 1) { + return; + } + } + } + + if (characterNode.parent.type === "ClassIntersection" || characterNode.parent.type === "ClassSubtraction") { + disableEscapeBackslashSuggest = true; + } + } + } + + report( + node, + reportedIndex, + escapedChar, + disableEscapeBackslashSuggest + ); + } + }); + } + + /** + * Checks if a node has an escape. + * @param {ASTNode} node node to check. + * @returns {void} + */ + function check(node) { + const isTemplateElement = node.type === "TemplateElement"; + + if ( + isTemplateElement && + node.parent && + node.parent.parent && + node.parent.parent.type === "TaggedTemplateExpression" && + node.parent === node.parent.parent.quasi + ) { + + // Don't report tagged template literals, because the backslash character is accessible to the tag function. + return; + } + + if (typeof node.value === "string" || isTemplateElement) { + + /* + * JSXAttribute doesn't have any escape sequence: https://facebook.github.io/jsx/. + * In addition, backticks are not supported by JSX yet: https://github.com/facebook/jsx/issues/25. + */ + if (node.parent.type === "JSXAttribute" || node.parent.type === "JSXElement" || node.parent.type === "JSXFragment") { + return; + } + + const value = isTemplateElement ? sourceCode.getText(node) : node.raw; + const pattern = /\\[^\d]/gu; + let match; + + while ((match = pattern.exec(value))) { + validateString(node, match); + } + } else if (node.regex) { + validateRegExp(node); + } + + } + + return { + Literal: check, + TemplateElement: check + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-useless-rename.js b/node_modules/eslint/lib/rules/no-useless-rename.js index 13c76861c..0c818fb2c 100644 --- a/node_modules/eslint/lib/rules/no-useless-rename.js +++ b/node_modules/eslint/lib/rules/no-useless-rename.js @@ -15,188 +15,158 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - defaultOptions: [ - { - ignoreDestructuring: false, - ignoreImport: false, - ignoreExport: false, - }, - ], - - docs: { - description: - "Disallow renaming import, export, and destructured assignments to the same name", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-useless-rename", - }, - - fixable: "code", - - schema: [ - { - type: "object", - properties: { - ignoreDestructuring: { type: "boolean" }, - ignoreImport: { type: "boolean" }, - ignoreExport: { type: "boolean" }, - }, - additionalProperties: false, - }, - ], - - messages: { - unnecessarilyRenamed: "{{type}} {{name}} unnecessarily renamed.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - const [{ ignoreDestructuring, ignoreImport, ignoreExport }] = - context.options; - - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - /** - * Reports error for unnecessarily renamed assignments - * @param {ASTNode} node node to report - * @param {ASTNode} initial node with initial name value - * @param {string} type the type of the offending node - * @returns {void} - */ - function reportError(node, initial, type) { - const name = - initial.type === "Identifier" ? initial.name : initial.value; - - return context.report({ - node, - messageId: "unnecessarilyRenamed", - data: { - name, - type, - }, - fix(fixer) { - const replacementNode = - node.type === "Property" ? node.value : node.local; - - if ( - sourceCode.getCommentsInside(node).length > - sourceCode.getCommentsInside(replacementNode).length - ) { - return null; - } - - // Don't autofix code such as `({foo: (foo) = a} = obj);`, parens are not allowed in shorthand properties. - if ( - replacementNode.type === "AssignmentPattern" && - astUtils.isParenthesised( - sourceCode, - replacementNode.left, - ) - ) { - return null; - } - - return fixer.replaceText( - node, - sourceCode.getText(replacementNode), - ); - }, - }); - } - - /** - * Checks whether a destructured assignment is unnecessarily renamed - * @param {ASTNode} node node to check - * @returns {void} - */ - function checkDestructured(node) { - if (ignoreDestructuring) { - return; - } - - for (const property of node.properties) { - /** - * Properties using shorthand syntax and rest elements can not be renamed. - * If the property is computed, we have no idea if a rename is useless or not. - */ - if ( - property.type !== "Property" || - property.shorthand || - property.computed - ) { - continue; - } - - const key = - (property.key.type === "Identifier" && property.key.name) || - (property.key.type === "Literal" && property.key.value); - const renamedKey = - property.value.type === "AssignmentPattern" - ? property.value.left.name - : property.value.name; - - if (key === renamedKey) { - reportError( - property, - property.key, - "Destructuring assignment", - ); - } - } - } - - /** - * Checks whether an import is unnecessarily renamed - * @param {ASTNode} node node to check - * @returns {void} - */ - function checkImport(node) { - if (ignoreImport) { - return; - } - - if ( - node.imported.range[0] !== node.local.range[0] && - astUtils.getModuleExportName(node.imported) === node.local.name - ) { - reportError(node, node.imported, "Import"); - } - } - - /** - * Checks whether an export is unnecessarily renamed - * @param {ASTNode} node node to check - * @returns {void} - */ - function checkExport(node) { - if (ignoreExport) { - return; - } - - if ( - node.local.range[0] !== node.exported.range[0] && - astUtils.getModuleExportName(node.local) === - astUtils.getModuleExportName(node.exported) - ) { - reportError(node, node.local, "Export"); - } - } - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - return { - ObjectPattern: checkDestructured, - ImportSpecifier: checkImport, - ExportSpecifier: checkExport, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow renaming import, export, and destructured assignments to the same name", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-useless-rename" + }, + + fixable: "code", + + schema: [ + { + type: "object", + properties: { + ignoreDestructuring: { type: "boolean", default: false }, + ignoreImport: { type: "boolean", default: false }, + ignoreExport: { type: "boolean", default: false } + }, + additionalProperties: false + } + ], + + messages: { + unnecessarilyRenamed: "{{type}} {{name}} unnecessarily renamed." + } + }, + + create(context) { + const sourceCode = context.sourceCode, + options = context.options[0] || {}, + ignoreDestructuring = options.ignoreDestructuring === true, + ignoreImport = options.ignoreImport === true, + ignoreExport = options.ignoreExport === true; + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + /** + * Reports error for unnecessarily renamed assignments + * @param {ASTNode} node node to report + * @param {ASTNode} initial node with initial name value + * @param {string} type the type of the offending node + * @returns {void} + */ + function reportError(node, initial, type) { + const name = initial.type === "Identifier" ? initial.name : initial.value; + + return context.report({ + node, + messageId: "unnecessarilyRenamed", + data: { + name, + type + }, + fix(fixer) { + const replacementNode = node.type === "Property" ? node.value : node.local; + + if (sourceCode.getCommentsInside(node).length > sourceCode.getCommentsInside(replacementNode).length) { + return null; + } + + // Don't autofix code such as `({foo: (foo) = a} = obj);`, parens are not allowed in shorthand properties. + if ( + replacementNode.type === "AssignmentPattern" && + astUtils.isParenthesised(sourceCode, replacementNode.left) + ) { + return null; + } + + return fixer.replaceText(node, sourceCode.getText(replacementNode)); + } + }); + } + + /** + * Checks whether a destructured assignment is unnecessarily renamed + * @param {ASTNode} node node to check + * @returns {void} + */ + function checkDestructured(node) { + if (ignoreDestructuring) { + return; + } + + for (const property of node.properties) { + + /** + * Properties using shorthand syntax and rest elements can not be renamed. + * If the property is computed, we have no idea if a rename is useless or not. + */ + if (property.type !== "Property" || property.shorthand || property.computed) { + continue; + } + + const key = (property.key.type === "Identifier" && property.key.name) || (property.key.type === "Literal" && property.key.value); + const renamedKey = property.value.type === "AssignmentPattern" ? property.value.left.name : property.value.name; + + if (key === renamedKey) { + reportError(property, property.key, "Destructuring assignment"); + } + } + } + + /** + * Checks whether an import is unnecessarily renamed + * @param {ASTNode} node node to check + * @returns {void} + */ + function checkImport(node) { + if (ignoreImport) { + return; + } + + if ( + node.imported.range[0] !== node.local.range[0] && + astUtils.getModuleExportName(node.imported) === node.local.name + ) { + reportError(node, node.imported, "Import"); + } + } + + /** + * Checks whether an export is unnecessarily renamed + * @param {ASTNode} node node to check + * @returns {void} + */ + function checkExport(node) { + if (ignoreExport) { + return; + } + + if ( + node.local.range[0] !== node.exported.range[0] && + astUtils.getModuleExportName(node.local) === astUtils.getModuleExportName(node.exported) + ) { + reportError(node, node.local, "Export"); + } + + } + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + return { + ObjectPattern: checkDestructured, + ImportSpecifier: checkImport, + ExportSpecifier: checkExport + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-useless-return.js b/node_modules/eslint/lib/rules/no-useless-return.js index e911885a7..81d610510 100644 --- a/node_modules/eslint/lib/rules/no-useless-return.js +++ b/node_modules/eslint/lib/rules/no-useless-return.js @@ -9,7 +9,7 @@ //------------------------------------------------------------------------------ const astUtils = require("./utils/ast-utils"), - FixTracker = require("./utils/fix-tracker"); + FixTracker = require("./utils/fix-tracker"); //------------------------------------------------------------------------------ // Helpers @@ -22,11 +22,11 @@ const astUtils = require("./utils/ast-utils"), * @returns {void} */ function remove(array, element) { - const index = array.indexOf(element); + const index = array.indexOf(element); - if (index !== -1) { - array.splice(index, 1); - } + if (index !== -1) { + array.splice(index, 1); + } } /** @@ -35,7 +35,7 @@ function remove(array, element) { * @returns {boolean} `true` if the node is removable. */ function isRemovable(node) { - return astUtils.STATEMENT_LIST_PARENTS.has(node.parent.type); + return astUtils.STATEMENT_LIST_PARENTS.has(node.parent.type); } /** @@ -44,20 +44,17 @@ function isRemovable(node) { * @returns {boolean} `true` if the node is in a `finally` block. */ function isInFinally(node) { - for ( - let currentNode = node; - currentNode && currentNode.parent && !astUtils.isFunction(currentNode); - currentNode = currentNode.parent - ) { - if ( - currentNode.parent.type === "TryStatement" && - currentNode.parent.finalizer === currentNode - ) { - return true; - } - } - - return false; + for ( + let currentNode = node; + currentNode && currentNode.parent && !astUtils.isFunction(currentNode); + currentNode = currentNode.parent + ) { + if (currentNode.parent.type === "TryStatement" && currentNode.parent.finalizer === currentNode) { + return true; + } + } + + return false; } /** @@ -66,336 +63,302 @@ function isInFinally(node) { * @returns {boolean} True if any segment is reachable; false otherwise. */ function isAnySegmentReachable(segments) { - for (const segment of segments) { - if (segment.reachable) { - return true; - } - } - return false; + for (const segment of segments) { + if (segment.reachable) { + return true; + } + } + + return false; } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: "Disallow redundant return statements", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-useless-return", - }, - - fixable: "code", - schema: [], - - messages: { - unnecessaryReturn: "Unnecessary return statement.", - }, - }, - - create(context) { - const segmentInfoMap = new WeakMap(); - const sourceCode = context.sourceCode; - let scopeInfo = null; - - /** - * Checks whether the given segment is terminated by a return statement or not. - * @param {CodePathSegment} segment The segment to check. - * @returns {boolean} `true` if the segment is terminated by a return statement, or if it's still a part of unreachable. - */ - function isReturned(segment) { - const info = segmentInfoMap.get(segment); - - return !info || info.returned; - } - - /** - * Collects useless return statements from the given previous segments. - * - * A previous segment may be an unreachable segment. - * In that case, the information object of the unreachable segment is not - * initialized because `onCodePathSegmentStart` event is not notified for - * unreachable segments. - * This goes to the previous segments of the unreachable segment recursively - * if the unreachable segment was generated by a return statement. Otherwise, - * this ignores the unreachable segment. - * - * This behavior would simulate code paths for the case that the return - * statement does not exist. - * @param {ASTNode[]} uselessReturns The collected return statements. - * @param {CodePathSegment[]} prevSegments The previous segments to traverse. - * @param {WeakSet} [providedTraversedSegments] A set of segments that have already been traversed in this call - * @returns {ASTNode[]} `uselessReturns`. - */ - function getUselessReturns( - uselessReturns, - prevSegments, - providedTraversedSegments, - ) { - const traversedSegments = - providedTraversedSegments || new WeakSet(); - - for (const segment of prevSegments) { - if (!segment.reachable) { - if (!traversedSegments.has(segment)) { - traversedSegments.add(segment); - getUselessReturns( - uselessReturns, - segment.allPrevSegments.filter(isReturned), - traversedSegments, - ); - } - continue; - } - - if (segmentInfoMap.has(segment)) { - uselessReturns.push( - ...segmentInfoMap.get(segment).uselessReturns, - ); - } - } - - return uselessReturns; - } - - /** - * Removes the return statements on the given segment from the useless return - * statement list. - * - * This segment may be an unreachable segment. - * In that case, the information object of the unreachable segment is not - * initialized because `onCodePathSegmentStart` event is not notified for - * unreachable segments. - * This goes to the previous segments of the unreachable segment recursively - * if the unreachable segment was generated by a return statement. Otherwise, - * this ignores the unreachable segment. - * - * This behavior would simulate code paths for the case that the return - * statement does not exist. - * @param {CodePathSegment} segment The segment to get return statements. - * @param {Set} usedUnreachableSegments A set of segments that have already been traversed in this call. - * @returns {void} - */ - function markReturnStatementsOnSegmentAsUsed( - segment, - usedUnreachableSegments, - ) { - if (!segment.reachable) { - usedUnreachableSegments.add(segment); - segment.allPrevSegments - .filter(isReturned) - .filter( - prevSegment => - !usedUnreachableSegments.has(prevSegment), - ) - .forEach(prevSegment => - markReturnStatementsOnSegmentAsUsed( - prevSegment, - usedUnreachableSegments, - ), - ); - return; - } - - const info = segmentInfoMap.get(segment); - - if (!info) { - return; - } - - info.uselessReturns = info.uselessReturns.filter(node => { - if ( - scopeInfo.traversedTryBlockStatements && - scopeInfo.traversedTryBlockStatements.length > 0 - ) { - const returnInitialRange = node.range[0]; - const returnFinalRange = node.range[1]; - - const areBlocksInRange = - scopeInfo.traversedTryBlockStatements.some( - tryBlockStatement => { - const blockInitialRange = - tryBlockStatement.range[0]; - const blockFinalRange = - tryBlockStatement.range[1]; - - return ( - returnInitialRange >= blockInitialRange && - returnFinalRange <= blockFinalRange - ); - }, - ); - - if (areBlocksInRange) { - return true; - } - } - - remove(scopeInfo.uselessReturns, node); - return false; - }); - } - - /** - * Removes the return statements on the current segments from the useless - * return statement list. - * - * This function will be called at every statement except FunctionDeclaration, - * BlockStatement, and BreakStatement. - * - * - FunctionDeclarations are always executed whether it's returned or not. - * - BlockStatements do nothing. - * - BreakStatements go the next merely. - * @returns {void} - */ - function markReturnStatementsOnCurrentSegmentsAsUsed() { - scopeInfo.currentSegments.forEach(segment => - markReturnStatementsOnSegmentAsUsed(segment, new Set()), - ); - } - - //---------------------------------------------------------------------- - // Public - //---------------------------------------------------------------------- - - return { - // Makes and pushes a new scope information. - onCodePathStart(codePath) { - scopeInfo = { - upper: scopeInfo, - uselessReturns: [], - traversedTryBlockStatements: [], - codePath, - currentSegments: new Set(), - }; - }, - - // Reports useless return statements if exist. - onCodePathEnd() { - for (const node of scopeInfo.uselessReturns) { - context.report({ - node, - loc: node.loc, - messageId: "unnecessaryReturn", - fix(fixer) { - if ( - isRemovable(node) && - !sourceCode.getCommentsInside(node).length - ) { - /* - * Extend the replacement range to include the - * entire function to avoid conflicting with - * no-else-return. - * https://github.com/eslint/eslint/issues/8026 - */ - return new FixTracker(fixer, sourceCode) - .retainEnclosingFunction(node) - .remove(node); - } - return null; - }, - }); - } - - scopeInfo = scopeInfo.upper; - }, - - /* - * Initializes segments. - * NOTE: This event is notified for only reachable segments. - */ - onCodePathSegmentStart(segment) { - scopeInfo.currentSegments.add(segment); - - const info = { - uselessReturns: getUselessReturns( - [], - segment.allPrevSegments, - ), - returned: false, - }; - - // Stores the info. - segmentInfoMap.set(segment, info); - }, - - onUnreachableCodePathSegmentStart(segment) { - scopeInfo.currentSegments.add(segment); - }, - - onUnreachableCodePathSegmentEnd(segment) { - scopeInfo.currentSegments.delete(segment); - }, - - onCodePathSegmentEnd(segment) { - scopeInfo.currentSegments.delete(segment); - }, - - // Adds ReturnStatement node to check whether it's useless or not. - ReturnStatement(node) { - if (node.argument) { - markReturnStatementsOnCurrentSegmentsAsUsed(); - } - if ( - node.argument || - astUtils.isInLoop(node) || - isInFinally(node) || - // Ignore `return` statements in unreachable places (https://github.com/eslint/eslint/issues/11647). - !isAnySegmentReachable(scopeInfo.currentSegments) - ) { - return; - } - - for (const segment of scopeInfo.currentSegments) { - const info = segmentInfoMap.get(segment); - - if (info) { - info.uselessReturns.push(node); - info.returned = true; - } - } - scopeInfo.uselessReturns.push(node); - }, - - "TryStatement > BlockStatement.block:exit"(node) { - scopeInfo.traversedTryBlockStatements.push(node); - }, - - "TryStatement:exit"() { - scopeInfo.traversedTryBlockStatements.pop(); - }, - - /* - * Registers for all statement nodes except FunctionDeclaration, BlockStatement, BreakStatement. - * Removes return statements of the current segments from the useless return statement list. - */ - ClassDeclaration: markReturnStatementsOnCurrentSegmentsAsUsed, - ContinueStatement: markReturnStatementsOnCurrentSegmentsAsUsed, - DebuggerStatement: markReturnStatementsOnCurrentSegmentsAsUsed, - DoWhileStatement: markReturnStatementsOnCurrentSegmentsAsUsed, - EmptyStatement: markReturnStatementsOnCurrentSegmentsAsUsed, - ExpressionStatement: markReturnStatementsOnCurrentSegmentsAsUsed, - ForInStatement: markReturnStatementsOnCurrentSegmentsAsUsed, - ForOfStatement: markReturnStatementsOnCurrentSegmentsAsUsed, - ForStatement: markReturnStatementsOnCurrentSegmentsAsUsed, - IfStatement: markReturnStatementsOnCurrentSegmentsAsUsed, - ImportDeclaration: markReturnStatementsOnCurrentSegmentsAsUsed, - LabeledStatement: markReturnStatementsOnCurrentSegmentsAsUsed, - SwitchStatement: markReturnStatementsOnCurrentSegmentsAsUsed, - ThrowStatement: markReturnStatementsOnCurrentSegmentsAsUsed, - TryStatement: markReturnStatementsOnCurrentSegmentsAsUsed, - VariableDeclaration: markReturnStatementsOnCurrentSegmentsAsUsed, - WhileStatement: markReturnStatementsOnCurrentSegmentsAsUsed, - WithStatement: markReturnStatementsOnCurrentSegmentsAsUsed, - ExportNamedDeclaration: markReturnStatementsOnCurrentSegmentsAsUsed, - ExportDefaultDeclaration: - markReturnStatementsOnCurrentSegmentsAsUsed, - ExportAllDeclaration: markReturnStatementsOnCurrentSegmentsAsUsed, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow redundant return statements", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-useless-return" + }, + + fixable: "code", + schema: [], + + messages: { + unnecessaryReturn: "Unnecessary return statement." + } + }, + + create(context) { + const segmentInfoMap = new WeakMap(); + const sourceCode = context.sourceCode; + let scopeInfo = null; + + /** + * Checks whether the given segment is terminated by a return statement or not. + * @param {CodePathSegment} segment The segment to check. + * @returns {boolean} `true` if the segment is terminated by a return statement, or if it's still a part of unreachable. + */ + function isReturned(segment) { + const info = segmentInfoMap.get(segment); + + return !info || info.returned; + } + + /** + * Collects useless return statements from the given previous segments. + * + * A previous segment may be an unreachable segment. + * In that case, the information object of the unreachable segment is not + * initialized because `onCodePathSegmentStart` event is not notified for + * unreachable segments. + * This goes to the previous segments of the unreachable segment recursively + * if the unreachable segment was generated by a return statement. Otherwise, + * this ignores the unreachable segment. + * + * This behavior would simulate code paths for the case that the return + * statement does not exist. + * @param {ASTNode[]} uselessReturns The collected return statements. + * @param {CodePathSegment[]} prevSegments The previous segments to traverse. + * @param {WeakSet} [providedTraversedSegments] A set of segments that have already been traversed in this call + * @returns {ASTNode[]} `uselessReturns`. + */ + function getUselessReturns(uselessReturns, prevSegments, providedTraversedSegments) { + const traversedSegments = providedTraversedSegments || new WeakSet(); + + for (const segment of prevSegments) { + if (!segment.reachable) { + if (!traversedSegments.has(segment)) { + traversedSegments.add(segment); + getUselessReturns( + uselessReturns, + segment.allPrevSegments.filter(isReturned), + traversedSegments + ); + } + continue; + } + + uselessReturns.push(...segmentInfoMap.get(segment).uselessReturns); + } + + return uselessReturns; + } + + /** + * Removes the return statements on the given segment from the useless return + * statement list. + * + * This segment may be an unreachable segment. + * In that case, the information object of the unreachable segment is not + * initialized because `onCodePathSegmentStart` event is not notified for + * unreachable segments. + * This goes to the previous segments of the unreachable segment recursively + * if the unreachable segment was generated by a return statement. Otherwise, + * this ignores the unreachable segment. + * + * This behavior would simulate code paths for the case that the return + * statement does not exist. + * @param {CodePathSegment} segment The segment to get return statements. + * @param {Set} usedUnreachableSegments A set of segments that have already been traversed in this call. + * @returns {void} + */ + function markReturnStatementsOnSegmentAsUsed(segment, usedUnreachableSegments) { + if (!segment.reachable) { + usedUnreachableSegments.add(segment); + segment.allPrevSegments + .filter(isReturned) + .filter(prevSegment => !usedUnreachableSegments.has(prevSegment)) + .forEach(prevSegment => markReturnStatementsOnSegmentAsUsed(prevSegment, usedUnreachableSegments)); + return; + } + + const info = segmentInfoMap.get(segment); + + info.uselessReturns = info.uselessReturns.filter(node => { + if (scopeInfo.traversedTryBlockStatements && scopeInfo.traversedTryBlockStatements.length > 0) { + const returnInitialRange = node.range[0]; + const returnFinalRange = node.range[1]; + + const areBlocksInRange = scopeInfo.traversedTryBlockStatements.some(tryBlockStatement => { + const blockInitialRange = tryBlockStatement.range[0]; + const blockFinalRange = tryBlockStatement.range[1]; + + return ( + returnInitialRange >= blockInitialRange && + returnFinalRange <= blockFinalRange + ); + }); + + if (areBlocksInRange) { + return true; + } + } + + remove(scopeInfo.uselessReturns, node); + return false; + }); + } + + /** + * Removes the return statements on the current segments from the useless + * return statement list. + * + * This function will be called at every statement except FunctionDeclaration, + * BlockStatement, and BreakStatement. + * + * - FunctionDeclarations are always executed whether it's returned or not. + * - BlockStatements do nothing. + * - BreakStatements go the next merely. + * @returns {void} + */ + function markReturnStatementsOnCurrentSegmentsAsUsed() { + scopeInfo + .currentSegments + .forEach(segment => markReturnStatementsOnSegmentAsUsed(segment, new Set())); + } + + //---------------------------------------------------------------------- + // Public + //---------------------------------------------------------------------- + + return { + + // Makes and pushes a new scope information. + onCodePathStart(codePath) { + scopeInfo = { + upper: scopeInfo, + uselessReturns: [], + traversedTryBlockStatements: [], + codePath, + currentSegments: new Set() + }; + }, + + // Reports useless return statements if exist. + onCodePathEnd() { + for (const node of scopeInfo.uselessReturns) { + context.report({ + node, + loc: node.loc, + messageId: "unnecessaryReturn", + fix(fixer) { + if (isRemovable(node) && !sourceCode.getCommentsInside(node).length) { + + /* + * Extend the replacement range to include the + * entire function to avoid conflicting with + * no-else-return. + * https://github.com/eslint/eslint/issues/8026 + */ + return new FixTracker(fixer, sourceCode) + .retainEnclosingFunction(node) + .remove(node); + } + return null; + } + }); + } + + scopeInfo = scopeInfo.upper; + }, + + /* + * Initializes segments. + * NOTE: This event is notified for only reachable segments. + */ + onCodePathSegmentStart(segment) { + + scopeInfo.currentSegments.add(segment); + + const info = { + uselessReturns: getUselessReturns([], segment.allPrevSegments), + returned: false + }; + + // Stores the info. + segmentInfoMap.set(segment, info); + }, + + onUnreachableCodePathSegmentStart(segment) { + scopeInfo.currentSegments.add(segment); + }, + + onUnreachableCodePathSegmentEnd(segment) { + scopeInfo.currentSegments.delete(segment); + }, + + onCodePathSegmentEnd(segment) { + scopeInfo.currentSegments.delete(segment); + }, + + // Adds ReturnStatement node to check whether it's useless or not. + ReturnStatement(node) { + if (node.argument) { + markReturnStatementsOnCurrentSegmentsAsUsed(); + } + if ( + node.argument || + astUtils.isInLoop(node) || + isInFinally(node) || + + // Ignore `return` statements in unreachable places (https://github.com/eslint/eslint/issues/11647). + !isAnySegmentReachable(scopeInfo.currentSegments) + ) { + return; + } + + for (const segment of scopeInfo.currentSegments) { + const info = segmentInfoMap.get(segment); + + if (info) { + info.uselessReturns.push(node); + info.returned = true; + } + } + scopeInfo.uselessReturns.push(node); + }, + + "TryStatement > BlockStatement.block:exit"(node) { + scopeInfo.traversedTryBlockStatements.push(node); + }, + + "TryStatement:exit"() { + scopeInfo.traversedTryBlockStatements.pop(); + }, + + /* + * Registers for all statement nodes except FunctionDeclaration, BlockStatement, BreakStatement. + * Removes return statements of the current segments from the useless return statement list. + */ + ClassDeclaration: markReturnStatementsOnCurrentSegmentsAsUsed, + ContinueStatement: markReturnStatementsOnCurrentSegmentsAsUsed, + DebuggerStatement: markReturnStatementsOnCurrentSegmentsAsUsed, + DoWhileStatement: markReturnStatementsOnCurrentSegmentsAsUsed, + EmptyStatement: markReturnStatementsOnCurrentSegmentsAsUsed, + ExpressionStatement: markReturnStatementsOnCurrentSegmentsAsUsed, + ForInStatement: markReturnStatementsOnCurrentSegmentsAsUsed, + ForOfStatement: markReturnStatementsOnCurrentSegmentsAsUsed, + ForStatement: markReturnStatementsOnCurrentSegmentsAsUsed, + IfStatement: markReturnStatementsOnCurrentSegmentsAsUsed, + ImportDeclaration: markReturnStatementsOnCurrentSegmentsAsUsed, + LabeledStatement: markReturnStatementsOnCurrentSegmentsAsUsed, + SwitchStatement: markReturnStatementsOnCurrentSegmentsAsUsed, + ThrowStatement: markReturnStatementsOnCurrentSegmentsAsUsed, + TryStatement: markReturnStatementsOnCurrentSegmentsAsUsed, + VariableDeclaration: markReturnStatementsOnCurrentSegmentsAsUsed, + WhileStatement: markReturnStatementsOnCurrentSegmentsAsUsed, + WithStatement: markReturnStatementsOnCurrentSegmentsAsUsed, + ExportNamedDeclaration: markReturnStatementsOnCurrentSegmentsAsUsed, + ExportDefaultDeclaration: markReturnStatementsOnCurrentSegmentsAsUsed, + ExportAllDeclaration: markReturnStatementsOnCurrentSegmentsAsUsed + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-var.js b/node_modules/eslint/lib/rules/no-var.js index 39562d82e..d45a91a1d 100644 --- a/node_modules/eslint/lib/rules/no-var.js +++ b/node_modules/eslint/lib/rules/no-var.js @@ -21,7 +21,7 @@ const astUtils = require("./utils/ast-utils"); * @returns {boolean} `true` if the variable is a global variable. */ function isGlobal(variable) { - return Boolean(variable.scope) && variable.scope.type === "global"; + return Boolean(variable.scope) && variable.scope.type === "global"; } /** @@ -32,12 +32,12 @@ function isGlobal(variable) { * scope. */ function getEnclosingFunctionScope(scope) { - let currentScope = scope; + let currentScope = scope; - while (currentScope.type !== "function" && currentScope.type !== "global") { - currentScope = currentScope.upper; - } - return currentScope; + while (currentScope.type !== "function" && currentScope.type !== "global") { + currentScope = currentScope.upper; + } + return currentScope; } /** @@ -47,13 +47,10 @@ function getEnclosingFunctionScope(scope) { * @returns {boolean} `true` if the variable is used from a closure. */ function isReferencedInClosure(variable) { - const enclosingFunctionScope = getEnclosingFunctionScope(variable.scope); + const enclosingFunctionScope = getEnclosingFunctionScope(variable.scope); - return variable.references.some( - reference => - getEnclosingFunctionScope(reference.from) !== - enclosingFunctionScope, - ); + return variable.references.some(reference => + getEnclosingFunctionScope(reference.from) !== enclosingFunctionScope); } /** @@ -63,11 +60,8 @@ function isReferencedInClosure(variable) { * iteration. */ function isLoopAssignee(node) { - return ( - (node.parent.type === "ForOfStatement" || - node.parent.type === "ForInStatement") && - node === node.parent.left - ); + return (node.parent.type === "ForOfStatement" || node.parent.type === "ForInStatement") && + node === node.parent.left; } /** @@ -76,11 +70,10 @@ function isLoopAssignee(node) { * @returns {boolean} `true` if the declaration has an initializer. */ function isDeclarationInitialized(node) { - return node.declarations.every(declarator => declarator.init !== null); + return node.declarations.every(declarator => declarator.init !== null); } -const SCOPE_NODE_TYPE = - /^(?:Program|BlockStatement|SwitchStatement|ForStatement|ForInStatement|ForOfStatement)$/u; +const SCOPE_NODE_TYPE = /^(?:Program|BlockStatement|SwitchStatement|ForStatement|ForInStatement|ForOfStatement)$/u; /** * Gets the scope node which directly contains a given node. @@ -91,18 +84,14 @@ const SCOPE_NODE_TYPE = * `ForOfStatement`. */ function getScopeNode(node) { - for ( - let currentNode = node; - currentNode; - currentNode = currentNode.parent - ) { - if (SCOPE_NODE_TYPE.test(currentNode.type)) { - return currentNode; - } - } - - /* c8 ignore next */ - return null; + for (let currentNode = node; currentNode; currentNode = currentNode.parent) { + if (SCOPE_NODE_TYPE.test(currentNode.type)) { + return currentNode; + } + } + + /* c8 ignore next */ + return null; } /** @@ -111,7 +100,7 @@ function getScopeNode(node) { * @returns {boolean} `true` if the variable is redeclared. */ function isRedeclared(variable) { - return variable.defs.length >= 2; + return variable.defs.length >= 2; } /** @@ -121,22 +110,23 @@ function isRedeclared(variable) { * variable is used from outside of the specified scope. */ function isUsedFromOutsideOf(scopeNode) { - /** - * Checks whether a given reference is inside of the specified scope or not. - * @param {eslint-scope.Reference} reference A reference to check. - * @returns {boolean} `true` if the reference is inside of the specified - * scope. - */ - function isOutsideOfScope(reference) { - const scope = scopeNode.range; - const id = reference.identifier.range; - - return id[0] < scope[0] || id[1] > scope[1]; - } - - return function (variable) { - return variable.references.some(isOutsideOfScope); - }; + + /** + * Checks whether a given reference is inside of the specified scope or not. + * @param {eslint-scope.Reference} reference A reference to check. + * @returns {boolean} `true` if the reference is inside of the specified + * scope. + */ + function isOutsideOfScope(reference) { + const scope = scopeNode.range; + const id = reference.identifier.range; + + return id[0] < scope[0] || id[1] > scope[1]; + } + + return function(variable) { + return variable.references.some(isOutsideOfScope); + }; } /** @@ -152,33 +142,27 @@ function isUsedFromOutsideOf(scopeNode) { * @private */ function hasReferenceInTDZ(node) { - const initStart = node.range[0]; - const initEnd = node.range[1]; - - return variable => { - const id = variable.defs[0].name; - const idStart = id.range[0]; - const defaultValue = - id.parent.type === "AssignmentPattern" ? id.parent.right : null; - const defaultStart = defaultValue && defaultValue.range[0]; - const defaultEnd = defaultValue && defaultValue.range[1]; - - return variable.references.some(reference => { - const start = reference.identifier.range[0]; - const end = reference.identifier.range[1]; - - return ( - !reference.init && - (start < idStart || - (defaultValue !== null && - start >= defaultStart && - end <= defaultEnd) || - (!astUtils.isFunction(node) && - start >= initStart && - end <= initEnd)) - ); - }); - }; + const initStart = node.range[0]; + const initEnd = node.range[1]; + + return variable => { + const id = variable.defs[0].name; + const idStart = id.range[0]; + const defaultValue = (id.parent.type === "AssignmentPattern" ? id.parent.right : null); + const defaultStart = defaultValue && defaultValue.range[0]; + const defaultEnd = defaultValue && defaultValue.range[1]; + + return variable.references.some(reference => { + const start = reference.identifier.range[0]; + const end = reference.identifier.range[1]; + + return !reference.init && ( + start < idStart || + (defaultValue !== null && start >= defaultStart && end <= defaultEnd) || + (!astUtils.isFunction(node) && start >= initStart && end <= initEnd) + ); + }); + }; } /** @@ -188,180 +172,163 @@ function hasReferenceInTDZ(node) { * @returns {boolean} `true` if the variable has a disallowed name. */ function hasNameDisallowedForLetDeclarations(variable) { - return variable.name === "let"; + return variable.name === "let"; } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - dialects: ["typescript", "javascript"], - language: "javascript", - - docs: { - description: "Require `let` or `const` instead of `var`", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-var", - }, - - schema: [], - fixable: "code", - - messages: { - unexpectedVar: "Unexpected var, use let or const instead.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - /** - * Checks whether the variables which are defined by the given declarator node have their references in TDZ. - * @param {ASTNode} declarator The VariableDeclarator node to check. - * @returns {boolean} `true` if one of the variables which are defined by the given declarator node have their references in TDZ. - */ - function hasSelfReferenceInTDZ(declarator) { - if (!declarator.init) { - return false; - } - const variables = sourceCode.getDeclaredVariables(declarator); - - return variables.some(hasReferenceInTDZ(declarator.init)); - } - - /** - * Checks whether it can fix a given variable declaration or not. - * It cannot fix if the following cases: - * - * - A variable is a global variable. - * - A variable is declared on a SwitchCase node. - * - A variable is redeclared. - * - A variable is used from outside the scope. - * - A variable is used from a closure within a loop. - * - A variable might be used before it is assigned within a loop. - * - A variable might be used in TDZ. - * - A variable is declared in statement position (e.g. a single-line `IfStatement`) - * - A variable has name that is disallowed for `let` declarations. - * - * ## A variable is declared on a SwitchCase node. - * - * If this rule modifies 'var' declarations on a SwitchCase node, it - * would generate the warnings of 'no-case-declarations' rule. And the - * 'eslint:recommended' preset includes 'no-case-declarations' rule, so - * this rule doesn't modify those declarations. - * - * ## A variable is redeclared. - * - * The language spec disallows redeclarations of `let` declarations. - * Those variables would cause syntax errors. - * - * ## A variable is used from outside the scope. - * - * The language spec disallows accesses from outside of the scope for - * `let` declarations. Those variables would cause reference errors. - * - * ## A variable is used from a closure within a loop. - * - * A `var` declaration within a loop shares the same variable instance - * across all loop iterations, while a `let` declaration creates a new - * instance for each iteration. This means if a variable in a loop is - * referenced by any closure, changing it from `var` to `let` would - * change the behavior in a way that is generally unsafe. - * - * ## A variable might be used before it is assigned within a loop. - * - * Within a loop, a `let` declaration without an initializer will be - * initialized to null, while a `var` declaration will retain its value - * from the previous iteration, so it is only safe to change `var` to - * `let` if we can statically determine that the variable is always - * assigned a value before its first access in the loop body. To keep - * the implementation simple, we only convert `var` to `let` within - * loops when the variable is a loop assignee or the declaration has an - * initializer. - * @param {ASTNode} node A variable declaration node to check. - * @returns {boolean} `true` if it can fix the node. - */ - function canFix(node) { - const variables = sourceCode.getDeclaredVariables(node); - const scopeNode = getScopeNode(node); - - if ( - node.parent.type === "SwitchCase" || - node.declarations.some(hasSelfReferenceInTDZ) || - variables.some(isGlobal) || - variables.some(isRedeclared) || - variables.some(isUsedFromOutsideOf(scopeNode)) || - variables.some(hasNameDisallowedForLetDeclarations) - ) { - return false; - } - - if (astUtils.isInLoop(node)) { - if (variables.some(isReferencedInClosure)) { - return false; - } - if (!isLoopAssignee(node) && !isDeclarationInitialized(node)) { - return false; - } - } - - if ( - !isLoopAssignee(node) && - !( - node.parent.type === "ForStatement" && - node.parent.init === node - ) && - !astUtils.STATEMENT_LIST_PARENTS.has(node.parent.type) - ) { - // If the declaration is not in a block, e.g. `if (foo) var bar = 1;`, then it can't be fixed. - return false; - } - - return true; - } - - /** - * Reports a given variable declaration node. - * @param {ASTNode} node A variable declaration node to report. - * @returns {void} - */ - function report(node) { - context.report({ - node, - messageId: "unexpectedVar", - - fix(fixer) { - const varToken = sourceCode.getFirstToken(node, { - filter: t => t.value === "var", - }); - - return canFix(node) - ? fixer.replaceText(varToken, "let") - : null; - }, - }); - } - - return { - "VariableDeclaration:exit"(node) { - if (node.kind !== "var") { - return; - } - - if ( - node.parent.type === "TSModuleBlock" && - node.parent.parent.type === "TSModuleDeclaration" && - node.parent.parent.global - ) { - return; - } - - report(node); - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Require `let` or `const` instead of `var`", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-var" + }, + + schema: [], + fixable: "code", + + messages: { + unexpectedVar: "Unexpected var, use let or const instead." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + + /** + * Checks whether the variables which are defined by the given declarator node have their references in TDZ. + * @param {ASTNode} declarator The VariableDeclarator node to check. + * @returns {boolean} `true` if one of the variables which are defined by the given declarator node have their references in TDZ. + */ + function hasSelfReferenceInTDZ(declarator) { + if (!declarator.init) { + return false; + } + const variables = sourceCode.getDeclaredVariables(declarator); + + return variables.some(hasReferenceInTDZ(declarator.init)); + } + + /** + * Checks whether it can fix a given variable declaration or not. + * It cannot fix if the following cases: + * + * - A variable is a global variable. + * - A variable is declared on a SwitchCase node. + * - A variable is redeclared. + * - A variable is used from outside the scope. + * - A variable is used from a closure within a loop. + * - A variable might be used before it is assigned within a loop. + * - A variable might be used in TDZ. + * - A variable is declared in statement position (e.g. a single-line `IfStatement`) + * - A variable has name that is disallowed for `let` declarations. + * + * ## A variable is declared on a SwitchCase node. + * + * If this rule modifies 'var' declarations on a SwitchCase node, it + * would generate the warnings of 'no-case-declarations' rule. And the + * 'eslint:recommended' preset includes 'no-case-declarations' rule, so + * this rule doesn't modify those declarations. + * + * ## A variable is redeclared. + * + * The language spec disallows redeclarations of `let` declarations. + * Those variables would cause syntax errors. + * + * ## A variable is used from outside the scope. + * + * The language spec disallows accesses from outside of the scope for + * `let` declarations. Those variables would cause reference errors. + * + * ## A variable is used from a closure within a loop. + * + * A `var` declaration within a loop shares the same variable instance + * across all loop iterations, while a `let` declaration creates a new + * instance for each iteration. This means if a variable in a loop is + * referenced by any closure, changing it from `var` to `let` would + * change the behavior in a way that is generally unsafe. + * + * ## A variable might be used before it is assigned within a loop. + * + * Within a loop, a `let` declaration without an initializer will be + * initialized to null, while a `var` declaration will retain its value + * from the previous iteration, so it is only safe to change `var` to + * `let` if we can statically determine that the variable is always + * assigned a value before its first access in the loop body. To keep + * the implementation simple, we only convert `var` to `let` within + * loops when the variable is a loop assignee or the declaration has an + * initializer. + * @param {ASTNode} node A variable declaration node to check. + * @returns {boolean} `true` if it can fix the node. + */ + function canFix(node) { + const variables = sourceCode.getDeclaredVariables(node); + const scopeNode = getScopeNode(node); + + if (node.parent.type === "SwitchCase" || + node.declarations.some(hasSelfReferenceInTDZ) || + variables.some(isGlobal) || + variables.some(isRedeclared) || + variables.some(isUsedFromOutsideOf(scopeNode)) || + variables.some(hasNameDisallowedForLetDeclarations) + ) { + return false; + } + + if (astUtils.isInLoop(node)) { + if (variables.some(isReferencedInClosure)) { + return false; + } + if (!isLoopAssignee(node) && !isDeclarationInitialized(node)) { + return false; + } + } + + if ( + !isLoopAssignee(node) && + !(node.parent.type === "ForStatement" && node.parent.init === node) && + !astUtils.STATEMENT_LIST_PARENTS.has(node.parent.type) + ) { + + // If the declaration is not in a block, e.g. `if (foo) var bar = 1;`, then it can't be fixed. + return false; + } + + return true; + } + + /** + * Reports a given variable declaration node. + * @param {ASTNode} node A variable declaration node to report. + * @returns {void} + */ + function report(node) { + context.report({ + node, + messageId: "unexpectedVar", + + fix(fixer) { + const varToken = sourceCode.getFirstToken(node, { filter: t => t.value === "var" }); + + return canFix(node) + ? fixer.replaceText(varToken, "let") + : null; + } + }); + } + + return { + "VariableDeclaration:exit"(node) { + if (node.kind === "var") { + report(node); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-void.js b/node_modules/eslint/lib/rules/no-void.js index 7e99fd3c1..9546d7a62 100644 --- a/node_modules/eslint/lib/rules/no-void.js +++ b/node_modules/eslint/lib/rules/no-void.js @@ -8,62 +8,57 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", + meta: { + type: "suggestion", - defaultOptions: [ - { - allowAsStatement: false, - }, - ], + docs: { + description: "Disallow `void` operators", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-void" + }, - docs: { - description: "Disallow `void` operators", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/no-void", - }, + messages: { + noVoid: "Expected 'undefined' and instead saw 'void'." + }, - messages: { - noVoid: "Expected 'undefined' and instead saw 'void'.", - }, + schema: [ + { + type: "object", + properties: { + allowAsStatement: { + type: "boolean", + default: false + } + }, + additionalProperties: false + } + ] + }, - schema: [ - { - type: "object", - properties: { - allowAsStatement: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - }, + create(context) { + const allowAsStatement = + context.options[0] && context.options[0].allowAsStatement; - create(context) { - const [{ allowAsStatement }] = context.options; + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - return { - 'UnaryExpression[operator="void"]'(node) { - if ( - allowAsStatement && - node.parent && - node.parent.type === "ExpressionStatement" - ) { - return; - } - context.report({ - node, - messageId: "noVoid", - }); - }, - }; - }, + return { + 'UnaryExpression[operator="void"]'(node) { + if ( + allowAsStatement && + node.parent && + node.parent.type === "ExpressionStatement" + ) { + return; + } + context.report({ + node, + messageId: "noVoid" + }); + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-warning-comments.js b/node_modules/eslint/lib/rules/no-warning-comments.js index 8f1a7c36b..c415bee7a 100644 --- a/node_modules/eslint/lib/rules/no-warning-comments.js +++ b/node_modules/eslint/lib/rules/no-warning-comments.js @@ -14,196 +14,188 @@ const CHAR_LIMIT = 40; // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - defaultOptions: [ - { - location: "start", - terms: ["todo", "fixme", "xxx"], - }, - ], - - docs: { - description: "Disallow specified warning terms in comments", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/no-warning-comments", - }, - - schema: [ - { - type: "object", - properties: { - terms: { - type: "array", - items: { - type: "string", - }, - }, - location: { - enum: ["start", "anywhere"], - }, - decoration: { - type: "array", - items: { - type: "string", - pattern: "^\\S$", - }, - minItems: 1, - uniqueItems: true, - }, - }, - additionalProperties: false, - }, - ], - - messages: { - unexpectedComment: - "Unexpected '{{matchedTerm}}' comment: '{{comment}}'.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - const [{ decoration, location, terms: warningTerms }] = context.options; - const escapedDecoration = escapeRegExp( - decoration ? decoration.join("") : "", - ); - const selfConfigRegEx = /\bno-warning-comments\b/u; - - /** - * Convert a warning term into a RegExp which will match a comment containing that whole word in the specified - * location ("start" or "anywhere"). If the term starts or ends with non word characters, then the match will not - * require word boundaries on that side. - * @param {string} term A term to convert to a RegExp - * @returns {RegExp} The term converted to a RegExp - */ - function convertToRegExp(term) { - const escaped = escapeRegExp(term); - - /* - * When matching at the start, ignore leading whitespace, and - * there's no need to worry about word boundaries. - * - * These expressions for the prefix and suffix are designed as follows: - * ^ handles any terms at the beginning of a comment. - * e.g. terms ["TODO"] matches `//TODO something` - * $ handles any terms at the end of a comment - * e.g. terms ["TODO"] matches `// something TODO` - * \b handles terms preceded/followed by word boundary - * e.g. terms: ["!FIX", "FIX!"] matches `// FIX!something` or `// something!FIX` - * terms: ["FIX"] matches `// FIX!` or `// !FIX`, but not `// fixed or affix` - * - * For location start: - * [\s]* handles optional leading spaces - * e.g. terms ["TODO"] matches `// TODO something` - * [\s\*]* (where "\*" is the escaped string of decoration) - * handles optional leading spaces or decoration characters (for "start" location only) - * e.g. terms ["TODO"] matches `/**** TODO something ... ` - */ - const wordBoundary = "\\b"; - - let prefix = ""; - - if (location === "start") { - prefix = `^[\\s${escapedDecoration}]*`; - } else if (/^\w/u.test(term)) { - prefix = wordBoundary; - } - - const suffix = /\w$/u.test(term) ? wordBoundary : ""; - const flags = "iu"; // Case-insensitive with Unicode case folding. - - /* - * For location "start", the typical regex is: - * /^[\s]*ESCAPED_TERM\b/iu. - * Or if decoration characters are specified (e.g. "*"), then any of - * those characters may appear in any order at the start: - * /^[\s\*]*ESCAPED_TERM\b/iu. - * - * For location "anywhere" the typical regex is - * /\bESCAPED_TERM\b/iu - * - * If it starts or ends with non-word character, the prefix and suffix are empty, respectively. - */ - return new RegExp(`${prefix}${escaped}${suffix}`, flags); - } - - const warningRegExps = warningTerms.map(convertToRegExp); - - /** - * Checks the specified comment for matches of the configured warning terms and returns the matches. - * @param {string} comment The comment which is checked. - * @returns {Array} All matched warning terms for this comment. - */ - function commentContainsWarningTerm(comment) { - const matches = []; - - warningRegExps.forEach((regex, index) => { - if (regex.test(comment)) { - matches.push(warningTerms[index]); - } - }); - - return matches; - } - - /** - * Checks the specified node for matching warning comments and reports them. - * @param {ASTNode} node The AST node being checked. - * @returns {void} undefined. - */ - function checkComment(node) { - const comment = node.value; - - if ( - astUtils.isDirectiveComment(node) && - selfConfigRegEx.test(comment) - ) { - return; - } - - const matches = commentContainsWarningTerm(comment); - - matches.forEach(matchedTerm => { - let commentToDisplay = ""; - let truncated = false; - - for (const c of comment.trim().split(/\s+/u)) { - const tmp = commentToDisplay - ? `${commentToDisplay} ${c}` - : c; - - if (tmp.length <= CHAR_LIMIT) { - commentToDisplay = tmp; - } else { - truncated = true; - break; - } - } - - context.report({ - node, - messageId: "unexpectedComment", - data: { - matchedTerm, - comment: `${commentToDisplay}${truncated ? "..." : ""}`, - }, - }); - }); - } - - return { - Program() { - const comments = sourceCode.getAllComments(); - - comments - .filter(token => token.type !== "Shebang") - .forEach(checkComment); - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow specified warning terms in comments", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-warning-comments" + }, + + schema: [ + { + type: "object", + properties: { + terms: { + type: "array", + items: { + type: "string" + } + }, + location: { + enum: ["start", "anywhere"] + }, + decoration: { + type: "array", + items: { + type: "string", + pattern: "^\\S$" + }, + minItems: 1, + uniqueItems: true + } + }, + additionalProperties: false + } + ], + + messages: { + unexpectedComment: "Unexpected '{{matchedTerm}}' comment: '{{comment}}'." + } + }, + + create(context) { + const sourceCode = context.sourceCode, + configuration = context.options[0] || {}, + warningTerms = configuration.terms || ["todo", "fixme", "xxx"], + location = configuration.location || "start", + decoration = [...configuration.decoration || []].join(""), + selfConfigRegEx = /\bno-warning-comments\b/u; + + /** + * Convert a warning term into a RegExp which will match a comment containing that whole word in the specified + * location ("start" or "anywhere"). If the term starts or ends with non word characters, then the match will not + * require word boundaries on that side. + * @param {string} term A term to convert to a RegExp + * @returns {RegExp} The term converted to a RegExp + */ + function convertToRegExp(term) { + const escaped = escapeRegExp(term); + const escapedDecoration = escapeRegExp(decoration); + + /* + * When matching at the start, ignore leading whitespace, and + * there's no need to worry about word boundaries. + * + * These expressions for the prefix and suffix are designed as follows: + * ^ handles any terms at the beginning of a comment. + * e.g. terms ["TODO"] matches `//TODO something` + * $ handles any terms at the end of a comment + * e.g. terms ["TODO"] matches `// something TODO` + * \b handles terms preceded/followed by word boundary + * e.g. terms: ["!FIX", "FIX!"] matches `// FIX!something` or `// something!FIX` + * terms: ["FIX"] matches `// FIX!` or `// !FIX`, but not `// fixed or affix` + * + * For location start: + * [\s]* handles optional leading spaces + * e.g. terms ["TODO"] matches `// TODO something` + * [\s\*]* (where "\*" is the escaped string of decoration) + * handles optional leading spaces or decoration characters (for "start" location only) + * e.g. terms ["TODO"] matches `/**** TODO something ... ` + */ + const wordBoundary = "\\b"; + + let prefix = ""; + + if (location === "start") { + prefix = `^[\\s${escapedDecoration}]*`; + } else if (/^\w/u.test(term)) { + prefix = wordBoundary; + } + + const suffix = /\w$/u.test(term) ? wordBoundary : ""; + const flags = "iu"; // Case-insensitive with Unicode case folding. + + /* + * For location "start", the typical regex is: + * /^[\s]*ESCAPED_TERM\b/iu. + * Or if decoration characters are specified (e.g. "*"), then any of + * those characters may appear in any order at the start: + * /^[\s\*]*ESCAPED_TERM\b/iu. + * + * For location "anywhere" the typical regex is + * /\bESCAPED_TERM\b/iu + * + * If it starts or ends with non-word character, the prefix and suffix are empty, respectively. + */ + return new RegExp(`${prefix}${escaped}${suffix}`, flags); + } + + const warningRegExps = warningTerms.map(convertToRegExp); + + /** + * Checks the specified comment for matches of the configured warning terms and returns the matches. + * @param {string} comment The comment which is checked. + * @returns {Array} All matched warning terms for this comment. + */ + function commentContainsWarningTerm(comment) { + const matches = []; + + warningRegExps.forEach((regex, index) => { + if (regex.test(comment)) { + matches.push(warningTerms[index]); + } + }); + + return matches; + } + + /** + * Checks the specified node for matching warning comments and reports them. + * @param {ASTNode} node The AST node being checked. + * @returns {void} undefined. + */ + function checkComment(node) { + const comment = node.value; + + if ( + astUtils.isDirectiveComment(node) && + selfConfigRegEx.test(comment) + ) { + return; + } + + const matches = commentContainsWarningTerm(comment); + + matches.forEach(matchedTerm => { + let commentToDisplay = ""; + let truncated = false; + + for (const c of comment.trim().split(/\s+/u)) { + const tmp = commentToDisplay ? `${commentToDisplay} ${c}` : c; + + if (tmp.length <= CHAR_LIMIT) { + commentToDisplay = tmp; + } else { + truncated = true; + break; + } + } + + context.report({ + node, + messageId: "unexpectedComment", + data: { + matchedTerm, + comment: `${commentToDisplay}${ + truncated ? "..." : "" + }` + } + }); + }); + } + + return { + Program() { + const comments = sourceCode.getAllComments(); + + comments + .filter(token => token.type !== "Shebang") + .forEach(checkComment); + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-whitespace-before-property.js b/node_modules/eslint/lib/rules/no-whitespace-before-property.js index 834e6f162..94a166e6a 100644 --- a/node_modules/eslint/lib/rules/no-whitespace-before-property.js +++ b/node_modules/eslint/lib/rules/no-whitespace-before-property.js @@ -15,136 +15,102 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "no-whitespace-before-property", - url: "https://eslint.style/rules/no-whitespace-before-property", - }, - }, - ], - }, - type: "layout", - - docs: { - description: "Disallow whitespace before properties", - recommended: false, - url: "https://eslint.org/docs/latest/rules/no-whitespace-before-property", - }, - - fixable: "whitespace", - schema: [], - - messages: { - unexpectedWhitespace: - "Unexpected whitespace before property {{propName}}.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - /** - * Reports whitespace before property token - * @param {ASTNode} node the node to report in the event of an error - * @param {Token} leftToken the left token - * @param {Token} rightToken the right token - * @returns {void} - * @private - */ - function reportError(node, leftToken, rightToken) { - context.report({ - node, - messageId: "unexpectedWhitespace", - data: { - propName: sourceCode.getText(node.property), - }, - fix(fixer) { - let replacementText = ""; - - if ( - !node.computed && - !node.optional && - astUtils.isDecimalInteger(node.object) - ) { - /* - * If the object is a number literal, fixing it to something like 5.toString() would cause a SyntaxError. - * Don't fix this case. - */ - return null; - } - - // Don't fix if comments exist. - if ( - sourceCode.commentsExistBetween(leftToken, rightToken) - ) { - return null; - } - - if (node.optional) { - replacementText = "?."; - } else if (!node.computed) { - replacementText = "."; - } - - return fixer.replaceTextRange( - [leftToken.range[1], rightToken.range[0]], - replacementText, - ); - }, - }); - } - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - return { - MemberExpression(node) { - let rightToken; - let leftToken; - - if (!astUtils.isTokenOnSameLine(node.object, node.property)) { - return; - } - - if (node.computed) { - rightToken = sourceCode.getTokenBefore( - node.property, - astUtils.isOpeningBracketToken, - ); - leftToken = sourceCode.getTokenBefore( - rightToken, - node.optional ? 1 : 0, - ); - } else { - rightToken = sourceCode.getFirstToken(node.property); - leftToken = sourceCode.getTokenBefore(rightToken, 1); - } - - if (sourceCode.isSpaceBetweenTokens(leftToken, rightToken)) { - reportError(node, leftToken, rightToken); - } - }, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Disallow whitespace before properties", + recommended: false, + url: "https://eslint.org/docs/latest/rules/no-whitespace-before-property" + }, + + fixable: "whitespace", + schema: [], + + messages: { + unexpectedWhitespace: "Unexpected whitespace before property {{propName}}." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + /** + * Reports whitespace before property token + * @param {ASTNode} node the node to report in the event of an error + * @param {Token} leftToken the left token + * @param {Token} rightToken the right token + * @returns {void} + * @private + */ + function reportError(node, leftToken, rightToken) { + context.report({ + node, + messageId: "unexpectedWhitespace", + data: { + propName: sourceCode.getText(node.property) + }, + fix(fixer) { + let replacementText = ""; + + if (!node.computed && !node.optional && astUtils.isDecimalInteger(node.object)) { + + /* + * If the object is a number literal, fixing it to something like 5.toString() would cause a SyntaxError. + * Don't fix this case. + */ + return null; + } + + // Don't fix if comments exist. + if (sourceCode.commentsExistBetween(leftToken, rightToken)) { + return null; + } + + if (node.optional) { + replacementText = "?."; + } else if (!node.computed) { + replacementText = "."; + } + + return fixer.replaceTextRange([leftToken.range[1], rightToken.range[0]], replacementText); + } + }); + } + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + return { + MemberExpression(node) { + let rightToken; + let leftToken; + + if (!astUtils.isTokenOnSameLine(node.object, node.property)) { + return; + } + + if (node.computed) { + rightToken = sourceCode.getTokenBefore(node.property, astUtils.isOpeningBracketToken); + leftToken = sourceCode.getTokenBefore(rightToken, node.optional ? 1 : 0); + } else { + rightToken = sourceCode.getFirstToken(node.property); + leftToken = sourceCode.getTokenBefore(rightToken, 1); + } + + if (sourceCode.isSpaceBetweenTokens(leftToken, rightToken)) { + reportError(node, leftToken, rightToken); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/no-with.js b/node_modules/eslint/lib/rules/no-with.js index d8be31f7e..0fb2c4519 100644 --- a/node_modules/eslint/lib/rules/no-with.js +++ b/node_modules/eslint/lib/rules/no-with.js @@ -9,29 +9,31 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: "Disallow `with` statements", - recommended: true, - url: "https://eslint.org/docs/latest/rules/no-with", - }, - - schema: [], - - messages: { - unexpectedWith: "Unexpected use of 'with' statement.", - }, - }, - - create(context) { - return { - WithStatement(node) { - context.report({ node, messageId: "unexpectedWith" }); - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow `with` statements", + recommended: true, + url: "https://eslint.org/docs/latest/rules/no-with" + }, + + schema: [], + + messages: { + unexpectedWith: "Unexpected use of 'with' statement." + } + }, + + create(context) { + + return { + WithStatement(node) { + context.report({ node, messageId: "unexpectedWith" }); + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/nonblock-statement-body-position.js b/node_modules/eslint/lib/rules/nonblock-statement-body-position.js index dbfd1063f..811b32b08 100644 --- a/node_modules/eslint/lib/rules/nonblock-statement-body-position.js +++ b/node_modules/eslint/lib/rules/nonblock-statement-body-position.js @@ -11,154 +11,117 @@ const POSITION_SCHEMA = { enum: ["beside", "below", "any"] }; -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "nonblock-statement-body-position", - url: "https://eslint.style/rules/nonblock-statement-body-position", - }, - }, - ], - }, - type: "layout", - - docs: { - description: "Enforce the location of single-line statements", - recommended: false, - url: "https://eslint.org/docs/latest/rules/nonblock-statement-body-position", - }, - - fixable: "whitespace", - - schema: [ - POSITION_SCHEMA, - { - properties: { - overrides: { - properties: { - if: POSITION_SCHEMA, - else: POSITION_SCHEMA, - while: POSITION_SCHEMA, - do: POSITION_SCHEMA, - for: POSITION_SCHEMA, - }, - additionalProperties: false, - }, - }, - additionalProperties: false, - }, - ], - - messages: { - expectNoLinebreak: "Expected no linebreak before this statement.", - expectLinebreak: "Expected a linebreak before this statement.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - //---------------------------------------------------------------------- - // Helpers - //---------------------------------------------------------------------- - - /** - * Gets the applicable preference for a particular keyword - * @param {string} keywordName The name of a keyword, e.g. 'if' - * @returns {string} The applicable option for the keyword, e.g. 'beside' - */ - function getOption(keywordName) { - return ( - (context.options[1] && - context.options[1].overrides && - context.options[1].overrides[keywordName]) || - context.options[0] || - "beside" - ); - } - - /** - * Validates the location of a single-line statement - * @param {ASTNode} node The single-line statement - * @param {string} keywordName The applicable keyword name for the single-line statement - * @returns {void} - */ - function validateStatement(node, keywordName) { - const option = getOption(keywordName); - - if (node.type === "BlockStatement" || option === "any") { - return; - } - - const tokenBefore = sourceCode.getTokenBefore(node); - - if ( - tokenBefore.loc.end.line === node.loc.start.line && - option === "below" - ) { - context.report({ - node, - messageId: "expectLinebreak", - fix: fixer => fixer.insertTextBefore(node, "\n"), - }); - } else if ( - tokenBefore.loc.end.line !== node.loc.start.line && - option === "beside" - ) { - context.report({ - node, - messageId: "expectNoLinebreak", - fix(fixer) { - if ( - sourceCode - .getText() - .slice(tokenBefore.range[1], node.range[0]) - .trim() - ) { - return null; - } - return fixer.replaceTextRange( - [tokenBefore.range[1], node.range[0]], - " ", - ); - }, - }); - } - } - - //---------------------------------------------------------------------- - // Public - //---------------------------------------------------------------------- - - return { - IfStatement(node) { - validateStatement(node.consequent, "if"); - - // Check the `else` node, but don't check 'else if' statements. - if (node.alternate && node.alternate.type !== "IfStatement") { - validateStatement(node.alternate, "else"); - } - }, - WhileStatement: node => validateStatement(node.body, "while"), - DoWhileStatement: node => validateStatement(node.body, "do"), - ForStatement: node => validateStatement(node.body, "for"), - ForInStatement: node => validateStatement(node.body, "for"), - ForOfStatement: node => validateStatement(node.body, "for"), - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Enforce the location of single-line statements", + recommended: false, + url: "https://eslint.org/docs/latest/rules/nonblock-statement-body-position" + }, + + fixable: "whitespace", + + schema: [ + POSITION_SCHEMA, + { + properties: { + overrides: { + properties: { + if: POSITION_SCHEMA, + else: POSITION_SCHEMA, + while: POSITION_SCHEMA, + do: POSITION_SCHEMA, + for: POSITION_SCHEMA + }, + additionalProperties: false + } + }, + additionalProperties: false + } + ], + + messages: { + expectNoLinebreak: "Expected no linebreak before this statement.", + expectLinebreak: "Expected a linebreak before this statement." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + + //---------------------------------------------------------------------- + // Helpers + //---------------------------------------------------------------------- + + /** + * Gets the applicable preference for a particular keyword + * @param {string} keywordName The name of a keyword, e.g. 'if' + * @returns {string} The applicable option for the keyword, e.g. 'beside' + */ + function getOption(keywordName) { + return context.options[1] && context.options[1].overrides && context.options[1].overrides[keywordName] || + context.options[0] || + "beside"; + } + + /** + * Validates the location of a single-line statement + * @param {ASTNode} node The single-line statement + * @param {string} keywordName The applicable keyword name for the single-line statement + * @returns {void} + */ + function validateStatement(node, keywordName) { + const option = getOption(keywordName); + + if (node.type === "BlockStatement" || option === "any") { + return; + } + + const tokenBefore = sourceCode.getTokenBefore(node); + + if (tokenBefore.loc.end.line === node.loc.start.line && option === "below") { + context.report({ + node, + messageId: "expectLinebreak", + fix: fixer => fixer.insertTextBefore(node, "\n") + }); + } else if (tokenBefore.loc.end.line !== node.loc.start.line && option === "beside") { + context.report({ + node, + messageId: "expectNoLinebreak", + fix(fixer) { + if (sourceCode.getText().slice(tokenBefore.range[1], node.range[0]).trim()) { + return null; + } + return fixer.replaceTextRange([tokenBefore.range[1], node.range[0]], " "); + } + }); + } + } + + //---------------------------------------------------------------------- + // Public + //---------------------------------------------------------------------- + + return { + IfStatement(node) { + validateStatement(node.consequent, "if"); + + // Check the `else` node, but don't check 'else if' statements. + if (node.alternate && node.alternate.type !== "IfStatement") { + validateStatement(node.alternate, "else"); + } + }, + WhileStatement: node => validateStatement(node.body, "while"), + DoWhileStatement: node => validateStatement(node.body, "do"), + ForStatement: node => validateStatement(node.body, "for"), + ForInStatement: node => validateStatement(node.body, "for"), + ForOfStatement: node => validateStatement(node.body, "for") + }; + } }; diff --git a/node_modules/eslint/lib/rules/object-curly-newline.js b/node_modules/eslint/lib/rules/object-curly-newline.js index e0b860b3d..176694b6a 100644 --- a/node_modules/eslint/lib/rules/object-curly-newline.js +++ b/node_modules/eslint/lib/rules/object-curly-newline.js @@ -18,28 +18,28 @@ const astUtils = require("./utils/ast-utils"); // Schema objects. const OPTION_VALUE = { - oneOf: [ - { - enum: ["always", "never"], - }, - { - type: "object", - properties: { - multiline: { - type: "boolean", - }, - minProperties: { - type: "integer", - minimum: 0, - }, - consistent: { - type: "boolean", - }, - }, - additionalProperties: false, - minProperties: 1, - }, - ], + oneOf: [ + { + enum: ["always", "never"] + }, + { + type: "object", + properties: { + multiline: { + type: "boolean" + }, + minProperties: { + type: "integer", + minimum: 0 + }, + consistent: { + type: "boolean" + } + }, + additionalProperties: false, + minProperties: 1 + } + ] }; /** @@ -48,25 +48,25 @@ const OPTION_VALUE = { * @returns {{multiline: boolean, minProperties: number, consistent: boolean}} Normalized option object. */ function normalizeOptionValue(value) { - let multiline = false; - let minProperties = Number.POSITIVE_INFINITY; - let consistent = false; - - if (value) { - if (value === "always") { - minProperties = 0; - } else if (value === "never") { - minProperties = Number.POSITIVE_INFINITY; - } else { - multiline = Boolean(value.multiline); - minProperties = value.minProperties || Number.POSITIVE_INFINITY; - consistent = Boolean(value.consistent); - } - } else { - consistent = true; - } - - return { multiline, minProperties, consistent }; + let multiline = false; + let minProperties = Number.POSITIVE_INFINITY; + let consistent = false; + + if (value) { + if (value === "always") { + minProperties = 0; + } else if (value === "never") { + minProperties = Number.POSITIVE_INFINITY; + } else { + multiline = Boolean(value.multiline); + minProperties = value.minProperties || Number.POSITIVE_INFINITY; + consistent = Boolean(value.consistent); + } + } else { + consistent = true; + } + + return { multiline, minProperties, consistent }; } /** @@ -75,7 +75,7 @@ function normalizeOptionValue(value) { * @returns {boolean} `true` if the value is an object, otherwise `false` */ function isObject(value) { - return typeof value === "object" && value !== null; + return typeof value === "object" && value !== null; } /** @@ -84,7 +84,7 @@ function isObject(value) { * @returns {boolean} `true` if the option is node-specific, otherwise `false` */ function isNodeSpecificOption(option) { - return isObject(option) || typeof option === "string"; + return isObject(option) || typeof option === "string"; } /** @@ -98,28 +98,18 @@ function isNodeSpecificOption(option) { * }} Normalized option object. */ function normalizeOptions(options) { - if ( - isObject(options) && - Object.values(options).some(isNodeSpecificOption) - ) { - return { - ObjectExpression: normalizeOptionValue(options.ObjectExpression), - ObjectPattern: normalizeOptionValue(options.ObjectPattern), - ImportDeclaration: normalizeOptionValue(options.ImportDeclaration), - ExportNamedDeclaration: normalizeOptionValue( - options.ExportDeclaration, - ), - }; - } - - const value = normalizeOptionValue(options); - - return { - ObjectExpression: value, - ObjectPattern: value, - ImportDeclaration: value, - ExportNamedDeclaration: value, - }; + if (isObject(options) && Object.values(options).some(isNodeSpecificOption)) { + return { + ObjectExpression: normalizeOptionValue(options.ObjectExpression), + ObjectPattern: normalizeOptionValue(options.ObjectPattern), + ImportDeclaration: normalizeOptionValue(options.ImportDeclaration), + ExportNamedDeclaration: normalizeOptionValue(options.ExportDeclaration) + }; + } + + const value = normalizeOptionValue(options); + + return { ObjectExpression: value, ObjectPattern: value, ImportDeclaration: value, ExportNamedDeclaration: value }; } /** @@ -132,252 +122,203 @@ function normalizeOptions(options) { * @returns {boolean} `true` if node needs to be checked for missing line breaks */ function areLineBreaksRequired(node, options, first, last) { - let objectProperties; - - if (node.type === "ObjectExpression" || node.type === "ObjectPattern") { - objectProperties = node.properties; - } else { - // is ImportDeclaration or ExportNamedDeclaration - objectProperties = node.specifiers.filter( - s => s.type === "ImportSpecifier" || s.type === "ExportSpecifier", - ); - } - - return ( - objectProperties.length >= options.minProperties || - (options.multiline && - objectProperties.length > 0 && - first.loc.start.line !== last.loc.end.line) - ); + let objectProperties; + + if (node.type === "ObjectExpression" || node.type === "ObjectPattern") { + objectProperties = node.properties; + } else { + + // is ImportDeclaration or ExportNamedDeclaration + objectProperties = node.specifiers + .filter(s => s.type === "ImportSpecifier" || s.type === "ExportSpecifier"); + } + + return objectProperties.length >= options.minProperties || + ( + options.multiline && + objectProperties.length > 0 && + first.loc.start.line !== last.loc.end.line + ); } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "object-curly-newline", - url: "https://eslint.style/rules/object-curly-newline", - }, - }, - ], - }, - type: "layout", - - docs: { - description: - "Enforce consistent line breaks after opening and before closing braces", - recommended: false, - url: "https://eslint.org/docs/latest/rules/object-curly-newline", - }, - - fixable: "whitespace", - - schema: [ - { - oneOf: [ - OPTION_VALUE, - { - type: "object", - properties: { - ObjectExpression: OPTION_VALUE, - ObjectPattern: OPTION_VALUE, - ImportDeclaration: OPTION_VALUE, - ExportDeclaration: OPTION_VALUE, - }, - additionalProperties: false, - minProperties: 1, - }, - ], - }, - ], - - messages: { - unexpectedLinebreakBeforeClosingBrace: - "Unexpected line break before this closing brace.", - unexpectedLinebreakAfterOpeningBrace: - "Unexpected line break after this opening brace.", - expectedLinebreakBeforeClosingBrace: - "Expected a line break before this closing brace.", - expectedLinebreakAfterOpeningBrace: - "Expected a line break after this opening brace.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - const normalizedOptions = normalizeOptions(context.options[0]); - - /** - * Reports a given node if it violated this rule. - * @param {ASTNode} node A node to check. This is an ObjectExpression, ObjectPattern, ImportDeclaration or ExportNamedDeclaration node. - * @returns {void} - */ - function check(node) { - const options = normalizedOptions[node.type]; - - if ( - (node.type === "ImportDeclaration" && - !node.specifiers.some( - specifier => specifier.type === "ImportSpecifier", - )) || - (node.type === "ExportNamedDeclaration" && - !node.specifiers.some( - specifier => specifier.type === "ExportSpecifier", - )) - ) { - return; - } - - const openBrace = sourceCode.getFirstToken( - node, - token => token.value === "{", - ); - - let closeBrace; - - if (node.typeAnnotation) { - closeBrace = sourceCode.getTokenBefore(node.typeAnnotation); - } else { - closeBrace = sourceCode.getLastToken( - node, - token => token.value === "}", - ); - } - - let first = sourceCode.getTokenAfter(openBrace, { - includeComments: true, - }); - let last = sourceCode.getTokenBefore(closeBrace, { - includeComments: true, - }); - - const needsLineBreaks = areLineBreaksRequired( - node, - options, - first, - last, - ); - - const hasCommentsFirstToken = astUtils.isCommentToken(first); - const hasCommentsLastToken = astUtils.isCommentToken(last); - - /* - * Use tokens or comments to check multiline or not. - * But use only tokens to check whether line breaks are needed. - * This allows: - * var obj = { // eslint-disable-line foo - * a: 1 - * } - */ - first = sourceCode.getTokenAfter(openBrace); - last = sourceCode.getTokenBefore(closeBrace); - - if (needsLineBreaks) { - if (astUtils.isTokenOnSameLine(openBrace, first)) { - context.report({ - messageId: "expectedLinebreakAfterOpeningBrace", - node, - loc: openBrace.loc, - fix(fixer) { - if (hasCommentsFirstToken) { - return null; - } - - return fixer.insertTextAfter(openBrace, "\n"); - }, - }); - } - if (astUtils.isTokenOnSameLine(last, closeBrace)) { - context.report({ - messageId: "expectedLinebreakBeforeClosingBrace", - node, - loc: closeBrace.loc, - fix(fixer) { - if (hasCommentsLastToken) { - return null; - } - - return fixer.insertTextBefore(closeBrace, "\n"); - }, - }); - } - } else { - const consistent = options.consistent; - const hasLineBreakBetweenOpenBraceAndFirst = - !astUtils.isTokenOnSameLine(openBrace, first); - const hasLineBreakBetweenCloseBraceAndLast = - !astUtils.isTokenOnSameLine(last, closeBrace); - - if ( - (!consistent && hasLineBreakBetweenOpenBraceAndFirst) || - (consistent && - hasLineBreakBetweenOpenBraceAndFirst && - !hasLineBreakBetweenCloseBraceAndLast) - ) { - context.report({ - messageId: "unexpectedLinebreakAfterOpeningBrace", - node, - loc: openBrace.loc, - fix(fixer) { - if (hasCommentsFirstToken) { - return null; - } - - return fixer.removeRange([ - openBrace.range[1], - first.range[0], - ]); - }, - }); - } - if ( - (!consistent && hasLineBreakBetweenCloseBraceAndLast) || - (consistent && - !hasLineBreakBetweenOpenBraceAndFirst && - hasLineBreakBetweenCloseBraceAndLast) - ) { - context.report({ - messageId: "unexpectedLinebreakBeforeClosingBrace", - node, - loc: closeBrace.loc, - fix(fixer) { - if (hasCommentsLastToken) { - return null; - } - - return fixer.removeRange([ - last.range[1], - closeBrace.range[0], - ]); - }, - }); - } - } - } - - return { - ObjectExpression: check, - ObjectPattern: check, - ImportDeclaration: check, - ExportNamedDeclaration: check, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Enforce consistent line breaks after opening and before closing braces", + recommended: false, + url: "https://eslint.org/docs/latest/rules/object-curly-newline" + }, + + fixable: "whitespace", + + schema: [ + { + oneOf: [ + OPTION_VALUE, + { + type: "object", + properties: { + ObjectExpression: OPTION_VALUE, + ObjectPattern: OPTION_VALUE, + ImportDeclaration: OPTION_VALUE, + ExportDeclaration: OPTION_VALUE + }, + additionalProperties: false, + minProperties: 1 + } + ] + } + ], + + messages: { + unexpectedLinebreakBeforeClosingBrace: "Unexpected line break before this closing brace.", + unexpectedLinebreakAfterOpeningBrace: "Unexpected line break after this opening brace.", + expectedLinebreakBeforeClosingBrace: "Expected a line break before this closing brace.", + expectedLinebreakAfterOpeningBrace: "Expected a line break after this opening brace." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + const normalizedOptions = normalizeOptions(context.options[0]); + + /** + * Reports a given node if it violated this rule. + * @param {ASTNode} node A node to check. This is an ObjectExpression, ObjectPattern, ImportDeclaration or ExportNamedDeclaration node. + * @returns {void} + */ + function check(node) { + const options = normalizedOptions[node.type]; + + if ( + (node.type === "ImportDeclaration" && + !node.specifiers.some(specifier => specifier.type === "ImportSpecifier")) || + (node.type === "ExportNamedDeclaration" && + !node.specifiers.some(specifier => specifier.type === "ExportSpecifier")) + ) { + return; + } + + const openBrace = sourceCode.getFirstToken(node, token => token.value === "{"); + + let closeBrace; + + if (node.typeAnnotation) { + closeBrace = sourceCode.getTokenBefore(node.typeAnnotation); + } else { + closeBrace = sourceCode.getLastToken(node, token => token.value === "}"); + } + + let first = sourceCode.getTokenAfter(openBrace, { includeComments: true }); + let last = sourceCode.getTokenBefore(closeBrace, { includeComments: true }); + + const needsLineBreaks = areLineBreaksRequired(node, options, first, last); + + const hasCommentsFirstToken = astUtils.isCommentToken(first); + const hasCommentsLastToken = astUtils.isCommentToken(last); + + /* + * Use tokens or comments to check multiline or not. + * But use only tokens to check whether line breaks are needed. + * This allows: + * var obj = { // eslint-disable-line foo + * a: 1 + * } + */ + first = sourceCode.getTokenAfter(openBrace); + last = sourceCode.getTokenBefore(closeBrace); + + if (needsLineBreaks) { + if (astUtils.isTokenOnSameLine(openBrace, first)) { + context.report({ + messageId: "expectedLinebreakAfterOpeningBrace", + node, + loc: openBrace.loc, + fix(fixer) { + if (hasCommentsFirstToken) { + return null; + } + + return fixer.insertTextAfter(openBrace, "\n"); + } + }); + } + if (astUtils.isTokenOnSameLine(last, closeBrace)) { + context.report({ + messageId: "expectedLinebreakBeforeClosingBrace", + node, + loc: closeBrace.loc, + fix(fixer) { + if (hasCommentsLastToken) { + return null; + } + + return fixer.insertTextBefore(closeBrace, "\n"); + } + }); + } + } else { + const consistent = options.consistent; + const hasLineBreakBetweenOpenBraceAndFirst = !astUtils.isTokenOnSameLine(openBrace, first); + const hasLineBreakBetweenCloseBraceAndLast = !astUtils.isTokenOnSameLine(last, closeBrace); + + if ( + (!consistent && hasLineBreakBetweenOpenBraceAndFirst) || + (consistent && hasLineBreakBetweenOpenBraceAndFirst && !hasLineBreakBetweenCloseBraceAndLast) + ) { + context.report({ + messageId: "unexpectedLinebreakAfterOpeningBrace", + node, + loc: openBrace.loc, + fix(fixer) { + if (hasCommentsFirstToken) { + return null; + } + + return fixer.removeRange([ + openBrace.range[1], + first.range[0] + ]); + } + }); + } + if ( + (!consistent && hasLineBreakBetweenCloseBraceAndLast) || + (consistent && !hasLineBreakBetweenOpenBraceAndFirst && hasLineBreakBetweenCloseBraceAndLast) + ) { + context.report({ + messageId: "unexpectedLinebreakBeforeClosingBrace", + node, + loc: closeBrace.loc, + fix(fixer) { + if (hasCommentsLastToken) { + return null; + } + + return fixer.removeRange([ + last.range[1], + closeBrace.range[0] + ]); + } + }); + } + } + } + + return { + ObjectExpression: check, + ObjectPattern: check, + ImportDeclaration: check, + ExportNamedDeclaration: check + }; + } }; diff --git a/node_modules/eslint/lib/rules/object-curly-spacing.js b/node_modules/eslint/lib/rules/object-curly-spacing.js index fbd177369..4463bcd5a 100644 --- a/node_modules/eslint/lib/rules/object-curly-spacing.js +++ b/node_modules/eslint/lib/rules/object-curly-spacing.js @@ -11,365 +11,301 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "object-curly-spacing", - url: "https://eslint.style/rules/object-curly-spacing", - }, - }, - ], - }, - type: "layout", - - docs: { - description: "Enforce consistent spacing inside braces", - recommended: false, - url: "https://eslint.org/docs/latest/rules/object-curly-spacing", - }, - - fixable: "whitespace", - - schema: [ - { - enum: ["always", "never"], - }, - { - type: "object", - properties: { - arraysInObjects: { - type: "boolean", - }, - objectsInObjects: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - - messages: { - requireSpaceBefore: "A space is required before '{{token}}'.", - requireSpaceAfter: "A space is required after '{{token}}'.", - unexpectedSpaceBefore: - "There should be no space before '{{token}}'.", - unexpectedSpaceAfter: "There should be no space after '{{token}}'.", - }, - }, - - create(context) { - const spaced = context.options[0] === "always", - sourceCode = context.sourceCode; - - /** - * Determines whether an option is set, relative to the spacing option. - * If spaced is "always", then check whether option is set to false. - * If spaced is "never", then check whether option is set to true. - * @param {Object} option The option to exclude. - * @returns {boolean} Whether or not the property is excluded. - */ - function isOptionSet(option) { - return context.options[1] - ? context.options[1][option] === !spaced - : false; - } - - const options = { - spaced, - arraysInObjectsException: isOptionSet("arraysInObjects"), - objectsInObjectsException: isOptionSet("objectsInObjects"), - }; - - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - /** - * Reports that there shouldn't be a space after the first token - * @param {ASTNode} node The node to report in the event of an error. - * @param {Token} token The token to use for the report. - * @returns {void} - */ - function reportNoBeginningSpace(node, token) { - const nextToken = context.sourceCode.getTokenAfter(token, { - includeComments: true, - }); - - context.report({ - node, - loc: { start: token.loc.end, end: nextToken.loc.start }, - messageId: "unexpectedSpaceAfter", - data: { - token: token.value, - }, - fix(fixer) { - return fixer.removeRange([ - token.range[1], - nextToken.range[0], - ]); - }, - }); - } - - /** - * Reports that there shouldn't be a space before the last token - * @param {ASTNode} node The node to report in the event of an error. - * @param {Token} token The token to use for the report. - * @returns {void} - */ - function reportNoEndingSpace(node, token) { - const previousToken = context.sourceCode.getTokenBefore(token, { - includeComments: true, - }); - - context.report({ - node, - loc: { start: previousToken.loc.end, end: token.loc.start }, - messageId: "unexpectedSpaceBefore", - data: { - token: token.value, - }, - fix(fixer) { - return fixer.removeRange([ - previousToken.range[1], - token.range[0], - ]); - }, - }); - } - - /** - * Reports that there should be a space after the first token - * @param {ASTNode} node The node to report in the event of an error. - * @param {Token} token The token to use for the report. - * @returns {void} - */ - function reportRequiredBeginningSpace(node, token) { - context.report({ - node, - loc: token.loc, - messageId: "requireSpaceAfter", - data: { - token: token.value, - }, - fix(fixer) { - return fixer.insertTextAfter(token, " "); - }, - }); - } - - /** - * Reports that there should be a space before the last token - * @param {ASTNode} node The node to report in the event of an error. - * @param {Token} token The token to use for the report. - * @returns {void} - */ - function reportRequiredEndingSpace(node, token) { - context.report({ - node, - loc: token.loc, - messageId: "requireSpaceBefore", - data: { - token: token.value, - }, - fix(fixer) { - return fixer.insertTextBefore(token, " "); - }, - }); - } - - /** - * Determines if spacing in curly braces is valid. - * @param {ASTNode} node The AST node to check. - * @param {Token} first The first token to check (should be the opening brace) - * @param {Token} second The second token to check (should be first after the opening brace) - * @param {Token} penultimate The penultimate token to check (should be last before closing brace) - * @param {Token} last The last token to check (should be closing brace) - * @returns {void} - */ - function validateBraceSpacing(node, first, second, penultimate, last) { - if (astUtils.isTokenOnSameLine(first, second)) { - const firstSpaced = sourceCode.isSpaceBetweenTokens( - first, - second, - ); - - if (options.spaced && !firstSpaced) { - reportRequiredBeginningSpace(node, first); - } - if (!options.spaced && firstSpaced && second.type !== "Line") { - reportNoBeginningSpace(node, first); - } - } - - if (astUtils.isTokenOnSameLine(penultimate, last)) { - const shouldCheckPenultimate = - (options.arraysInObjectsException && - astUtils.isClosingBracketToken(penultimate)) || - (options.objectsInObjectsException && - astUtils.isClosingBraceToken(penultimate)); - const penultimateType = - shouldCheckPenultimate && - sourceCode.getNodeByRangeIndex(penultimate.range[0]).type; - - const closingCurlyBraceMustBeSpaced = - (options.arraysInObjectsException && - penultimateType === "ArrayExpression") || - (options.objectsInObjectsException && - (penultimateType === "ObjectExpression" || - penultimateType === "ObjectPattern")) - ? !options.spaced - : options.spaced; - - const lastSpaced = sourceCode.isSpaceBetweenTokens( - penultimate, - last, - ); - - if (closingCurlyBraceMustBeSpaced && !lastSpaced) { - reportRequiredEndingSpace(node, last); - } - if (!closingCurlyBraceMustBeSpaced && lastSpaced) { - reportNoEndingSpace(node, last); - } - } - } - - /** - * Gets '}' token of an object node. - * - * Because the last token of object patterns might be a type annotation, - * this traverses tokens preceded by the last property, then returns the - * first '}' token. - * @param {ASTNode} node The node to get. This node is an - * ObjectExpression or an ObjectPattern. And this node has one or - * more properties. - * @returns {Token} '}' token. - */ - function getClosingBraceOfObject(node) { - const lastProperty = node.properties.at(-1); - - return sourceCode.getTokenAfter( - lastProperty, - astUtils.isClosingBraceToken, - ); - } - - /** - * Reports a given object node if spacing in curly braces is invalid. - * @param {ASTNode} node An ObjectExpression or ObjectPattern node to check. - * @returns {void} - */ - function checkForObject(node) { - if (node.properties.length === 0) { - return; - } - - const first = sourceCode.getFirstToken(node), - last = getClosingBraceOfObject(node), - second = sourceCode.getTokenAfter(first, { - includeComments: true, - }), - penultimate = sourceCode.getTokenBefore(last, { - includeComments: true, - }); - - validateBraceSpacing(node, first, second, penultimate, last); - } - - /** - * Reports a given import node if spacing in curly braces is invalid. - * @param {ASTNode} node An ImportDeclaration node to check. - * @returns {void} - */ - function checkForImport(node) { - if (node.specifiers.length === 0) { - return; - } - - let firstSpecifier = node.specifiers[0]; - const lastSpecifier = node.specifiers.at(-1); - - if (lastSpecifier.type !== "ImportSpecifier") { - return; - } - if (firstSpecifier.type !== "ImportSpecifier") { - firstSpecifier = node.specifiers[1]; - } - - const first = sourceCode.getTokenBefore(firstSpecifier), - last = sourceCode.getTokenAfter( - lastSpecifier, - astUtils.isNotCommaToken, - ), - second = sourceCode.getTokenAfter(first, { - includeComments: true, - }), - penultimate = sourceCode.getTokenBefore(last, { - includeComments: true, - }); - - validateBraceSpacing(node, first, second, penultimate, last); - } - - /** - * Reports a given export node if spacing in curly braces is invalid. - * @param {ASTNode} node An ExportNamedDeclaration node to check. - * @returns {void} - */ - function checkForExport(node) { - if (node.specifiers.length === 0) { - return; - } - - const firstSpecifier = node.specifiers[0], - lastSpecifier = node.specifiers.at(-1), - first = sourceCode.getTokenBefore(firstSpecifier), - last = sourceCode.getTokenAfter( - lastSpecifier, - astUtils.isNotCommaToken, - ), - second = sourceCode.getTokenAfter(first, { - includeComments: true, - }), - penultimate = sourceCode.getTokenBefore(last, { - includeComments: true, - }); - - validateBraceSpacing(node, first, second, penultimate, last); - } - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - return { - // var {x} = y; - ObjectPattern: checkForObject, - - // var y = {x: 'y'} - ObjectExpression: checkForObject, - - // import {y} from 'x'; - ImportDeclaration: checkForImport, - - // export {name} from 'yo'; - ExportNamedDeclaration: checkForExport, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Enforce consistent spacing inside braces", + recommended: false, + url: "https://eslint.org/docs/latest/rules/object-curly-spacing" + }, + + fixable: "whitespace", + + schema: [ + { + enum: ["always", "never"] + }, + { + type: "object", + properties: { + arraysInObjects: { + type: "boolean" + }, + objectsInObjects: { + type: "boolean" + } + }, + additionalProperties: false + } + ], + + messages: { + requireSpaceBefore: "A space is required before '{{token}}'.", + requireSpaceAfter: "A space is required after '{{token}}'.", + unexpectedSpaceBefore: "There should be no space before '{{token}}'.", + unexpectedSpaceAfter: "There should be no space after '{{token}}'." + } + }, + + create(context) { + const spaced = context.options[0] === "always", + sourceCode = context.sourceCode; + + /** + * Determines whether an option is set, relative to the spacing option. + * If spaced is "always", then check whether option is set to false. + * If spaced is "never", then check whether option is set to true. + * @param {Object} option The option to exclude. + * @returns {boolean} Whether or not the property is excluded. + */ + function isOptionSet(option) { + return context.options[1] ? context.options[1][option] === !spaced : false; + } + + const options = { + spaced, + arraysInObjectsException: isOptionSet("arraysInObjects"), + objectsInObjectsException: isOptionSet("objectsInObjects") + }; + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + /** + * Reports that there shouldn't be a space after the first token + * @param {ASTNode} node The node to report in the event of an error. + * @param {Token} token The token to use for the report. + * @returns {void} + */ + function reportNoBeginningSpace(node, token) { + const nextToken = context.sourceCode.getTokenAfter(token, { includeComments: true }); + + context.report({ + node, + loc: { start: token.loc.end, end: nextToken.loc.start }, + messageId: "unexpectedSpaceAfter", + data: { + token: token.value + }, + fix(fixer) { + return fixer.removeRange([token.range[1], nextToken.range[0]]); + } + }); + } + + /** + * Reports that there shouldn't be a space before the last token + * @param {ASTNode} node The node to report in the event of an error. + * @param {Token} token The token to use for the report. + * @returns {void} + */ + function reportNoEndingSpace(node, token) { + const previousToken = context.sourceCode.getTokenBefore(token, { includeComments: true }); + + context.report({ + node, + loc: { start: previousToken.loc.end, end: token.loc.start }, + messageId: "unexpectedSpaceBefore", + data: { + token: token.value + }, + fix(fixer) { + return fixer.removeRange([previousToken.range[1], token.range[0]]); + } + }); + } + + /** + * Reports that there should be a space after the first token + * @param {ASTNode} node The node to report in the event of an error. + * @param {Token} token The token to use for the report. + * @returns {void} + */ + function reportRequiredBeginningSpace(node, token) { + context.report({ + node, + loc: token.loc, + messageId: "requireSpaceAfter", + data: { + token: token.value + }, + fix(fixer) { + return fixer.insertTextAfter(token, " "); + } + }); + } + + /** + * Reports that there should be a space before the last token + * @param {ASTNode} node The node to report in the event of an error. + * @param {Token} token The token to use for the report. + * @returns {void} + */ + function reportRequiredEndingSpace(node, token) { + context.report({ + node, + loc: token.loc, + messageId: "requireSpaceBefore", + data: { + token: token.value + }, + fix(fixer) { + return fixer.insertTextBefore(token, " "); + } + }); + } + + /** + * Determines if spacing in curly braces is valid. + * @param {ASTNode} node The AST node to check. + * @param {Token} first The first token to check (should be the opening brace) + * @param {Token} second The second token to check (should be first after the opening brace) + * @param {Token} penultimate The penultimate token to check (should be last before closing brace) + * @param {Token} last The last token to check (should be closing brace) + * @returns {void} + */ + function validateBraceSpacing(node, first, second, penultimate, last) { + if (astUtils.isTokenOnSameLine(first, second)) { + const firstSpaced = sourceCode.isSpaceBetweenTokens(first, second); + + if (options.spaced && !firstSpaced) { + reportRequiredBeginningSpace(node, first); + } + if (!options.spaced && firstSpaced && second.type !== "Line") { + reportNoBeginningSpace(node, first); + } + } + + if (astUtils.isTokenOnSameLine(penultimate, last)) { + const shouldCheckPenultimate = ( + options.arraysInObjectsException && astUtils.isClosingBracketToken(penultimate) || + options.objectsInObjectsException && astUtils.isClosingBraceToken(penultimate) + ); + const penultimateType = shouldCheckPenultimate && sourceCode.getNodeByRangeIndex(penultimate.range[0]).type; + + const closingCurlyBraceMustBeSpaced = ( + options.arraysInObjectsException && penultimateType === "ArrayExpression" || + options.objectsInObjectsException && (penultimateType === "ObjectExpression" || penultimateType === "ObjectPattern") + ) ? !options.spaced : options.spaced; + + const lastSpaced = sourceCode.isSpaceBetweenTokens(penultimate, last); + + if (closingCurlyBraceMustBeSpaced && !lastSpaced) { + reportRequiredEndingSpace(node, last); + } + if (!closingCurlyBraceMustBeSpaced && lastSpaced) { + reportNoEndingSpace(node, last); + } + } + } + + /** + * Gets '}' token of an object node. + * + * Because the last token of object patterns might be a type annotation, + * this traverses tokens preceded by the last property, then returns the + * first '}' token. + * @param {ASTNode} node The node to get. This node is an + * ObjectExpression or an ObjectPattern. And this node has one or + * more properties. + * @returns {Token} '}' token. + */ + function getClosingBraceOfObject(node) { + const lastProperty = node.properties[node.properties.length - 1]; + + return sourceCode.getTokenAfter(lastProperty, astUtils.isClosingBraceToken); + } + + /** + * Reports a given object node if spacing in curly braces is invalid. + * @param {ASTNode} node An ObjectExpression or ObjectPattern node to check. + * @returns {void} + */ + function checkForObject(node) { + if (node.properties.length === 0) { + return; + } + + const first = sourceCode.getFirstToken(node), + last = getClosingBraceOfObject(node), + second = sourceCode.getTokenAfter(first, { includeComments: true }), + penultimate = sourceCode.getTokenBefore(last, { includeComments: true }); + + validateBraceSpacing(node, first, second, penultimate, last); + } + + /** + * Reports a given import node if spacing in curly braces is invalid. + * @param {ASTNode} node An ImportDeclaration node to check. + * @returns {void} + */ + function checkForImport(node) { + if (node.specifiers.length === 0) { + return; + } + + let firstSpecifier = node.specifiers[0]; + const lastSpecifier = node.specifiers[node.specifiers.length - 1]; + + if (lastSpecifier.type !== "ImportSpecifier") { + return; + } + if (firstSpecifier.type !== "ImportSpecifier") { + firstSpecifier = node.specifiers[1]; + } + + const first = sourceCode.getTokenBefore(firstSpecifier), + last = sourceCode.getTokenAfter(lastSpecifier, astUtils.isNotCommaToken), + second = sourceCode.getTokenAfter(first, { includeComments: true }), + penultimate = sourceCode.getTokenBefore(last, { includeComments: true }); + + validateBraceSpacing(node, first, second, penultimate, last); + } + + /** + * Reports a given export node if spacing in curly braces is invalid. + * @param {ASTNode} node An ExportNamedDeclaration node to check. + * @returns {void} + */ + function checkForExport(node) { + if (node.specifiers.length === 0) { + return; + } + + const firstSpecifier = node.specifiers[0], + lastSpecifier = node.specifiers[node.specifiers.length - 1], + first = sourceCode.getTokenBefore(firstSpecifier), + last = sourceCode.getTokenAfter(lastSpecifier, astUtils.isNotCommaToken), + second = sourceCode.getTokenAfter(first, { includeComments: true }), + penultimate = sourceCode.getTokenBefore(last, { includeComments: true }); + + validateBraceSpacing(node, first, second, penultimate, last); + } + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + return { + + // var {x} = y; + ObjectPattern: checkForObject, + + // var y = {x: 'y'} + ObjectExpression: checkForObject, + + // import {y} from 'x'; + ImportDeclaration: checkForImport, + + // export {name} from 'yo'; + ExportNamedDeclaration: checkForExport + }; + + } }; diff --git a/node_modules/eslint/lib/rules/object-property-newline.js b/node_modules/eslint/lib/rules/object-property-newline.js index 6921e048a..6ffa06421 100644 --- a/node_modules/eslint/lib/rules/object-property-newline.js +++ b/node_modules/eslint/lib/rules/object-property-newline.js @@ -10,142 +10,93 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "object-property-newline", - url: "https://eslint.style/rules/object-property-newline", - }, - }, - ], - }, - type: "layout", - - docs: { - description: "Enforce placing object properties on separate lines", - recommended: false, - url: "https://eslint.org/docs/latest/rules/object-property-newline", - }, - - schema: [ - { - type: "object", - properties: { - allowAllPropertiesOnSameLine: { - type: "boolean", - default: false, - }, - allowMultiplePropertiesPerLine: { - // Deprecated - type: "boolean", - default: false, - }, - }, - additionalProperties: false, - }, - ], - - fixable: "whitespace", - - messages: { - propertiesOnNewlineAll: - "Object properties must go on a new line if they aren't all on the same line.", - propertiesOnNewline: "Object properties must go on a new line.", - }, - }, - - create(context) { - const allowSameLine = - context.options[0] && - (context.options[0].allowAllPropertiesOnSameLine || - context.options[0] - .allowMultiplePropertiesPerLine); /* Deprecated */ - const messageId = allowSameLine - ? "propertiesOnNewlineAll" - : "propertiesOnNewline"; - - const sourceCode = context.sourceCode; - - return { - ObjectExpression(node) { - if (allowSameLine) { - if (node.properties.length > 1) { - const firstTokenOfFirstProperty = - sourceCode.getFirstToken(node.properties[0]); - const lastTokenOfLastProperty = sourceCode.getLastToken( - node.properties.at(-1), - ); - - if ( - firstTokenOfFirstProperty.loc.end.line === - lastTokenOfLastProperty.loc.start.line - ) { - // All keys and values are on the same line - return; - } - } - } - - for (let i = 1; i < node.properties.length; i++) { - const lastTokenOfPreviousProperty = sourceCode.getLastToken( - node.properties[i - 1], - ); - const firstTokenOfCurrentProperty = - sourceCode.getFirstToken(node.properties[i]); - - if ( - lastTokenOfPreviousProperty.loc.end.line === - firstTokenOfCurrentProperty.loc.start.line - ) { - context.report({ - node, - loc: firstTokenOfCurrentProperty.loc, - messageId, - fix(fixer) { - const comma = sourceCode.getTokenBefore( - firstTokenOfCurrentProperty, - ); - const rangeAfterComma = [ - comma.range[1], - firstTokenOfCurrentProperty.range[0], - ]; - - // Don't perform a fix if there are any comments between the comma and the next property. - if ( - sourceCode.text - .slice( - rangeAfterComma[0], - rangeAfterComma[1], - ) - .trim() - ) { - return null; - } - - return fixer.replaceTextRange( - rangeAfterComma, - "\n", - ); - }, - }); - } - } - }, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Enforce placing object properties on separate lines", + recommended: false, + url: "https://eslint.org/docs/latest/rules/object-property-newline" + }, + + schema: [ + { + type: "object", + properties: { + allowAllPropertiesOnSameLine: { + type: "boolean", + default: false + }, + allowMultiplePropertiesPerLine: { // Deprecated + type: "boolean", + default: false + } + }, + additionalProperties: false + } + ], + + fixable: "whitespace", + + messages: { + propertiesOnNewlineAll: "Object properties must go on a new line if they aren't all on the same line.", + propertiesOnNewline: "Object properties must go on a new line." + } + }, + + create(context) { + const allowSameLine = context.options[0] && ( + (context.options[0].allowAllPropertiesOnSameLine || context.options[0].allowMultiplePropertiesPerLine /* Deprecated */) + ); + const messageId = allowSameLine + ? "propertiesOnNewlineAll" + : "propertiesOnNewline"; + + const sourceCode = context.sourceCode; + + return { + ObjectExpression(node) { + if (allowSameLine) { + if (node.properties.length > 1) { + const firstTokenOfFirstProperty = sourceCode.getFirstToken(node.properties[0]); + const lastTokenOfLastProperty = sourceCode.getLastToken(node.properties[node.properties.length - 1]); + + if (firstTokenOfFirstProperty.loc.end.line === lastTokenOfLastProperty.loc.start.line) { + + // All keys and values are on the same line + return; + } + } + } + + for (let i = 1; i < node.properties.length; i++) { + const lastTokenOfPreviousProperty = sourceCode.getLastToken(node.properties[i - 1]); + const firstTokenOfCurrentProperty = sourceCode.getFirstToken(node.properties[i]); + + if (lastTokenOfPreviousProperty.loc.end.line === firstTokenOfCurrentProperty.loc.start.line) { + context.report({ + node, + loc: firstTokenOfCurrentProperty.loc, + messageId, + fix(fixer) { + const comma = sourceCode.getTokenBefore(firstTokenOfCurrentProperty); + const rangeAfterComma = [comma.range[1], firstTokenOfCurrentProperty.range[0]]; + + // Don't perform a fix if there are any comments between the comma and the next property. + if (sourceCode.text.slice(rangeAfterComma[0], rangeAfterComma[1]).trim()) { + return null; + } + + return fixer.replaceTextRange(rangeAfterComma, "\n"); + } + }); + } + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/object-shorthand.js b/node_modules/eslint/lib/rules/object-shorthand.js index 916d5374d..e4cb3a445 100644 --- a/node_modules/eslint/lib/rules/object-shorthand.js +++ b/node_modules/eslint/lib/rules/object-shorthand.js @@ -6,12 +6,12 @@ "use strict"; const OPTIONS = { - always: "always", - never: "never", - methods: "methods", - properties: "properties", - consistent: "consistent", - consistentAsNeeded: "consistent-as-needed", + always: "always", + never: "never", + methods: "methods", + properties: "properties", + consistent: "consistent", + consistentAsNeeded: "consistent-as-needed" }; //------------------------------------------------------------------------------ @@ -19,634 +19,502 @@ const OPTIONS = { //------------------------------------------------------------------------------ const astUtils = require("./utils/ast-utils"); -//-------------------------------------------------------------------------- -// Helpers -//-------------------------------------------------------------------------- -const CTOR_PREFIX_REGEX = /[^_$0-9]/u; -const JSDOC_COMMENT_REGEX = /^\s*\*/u; - -/** - * Determines if the first character of the name is a capital letter. - * @param {string} name The name of the node to evaluate. - * @returns {boolean} True if the first character of the property name is a capital letter, false if not. - * @private - */ -function isConstructor(name) { - const match = CTOR_PREFIX_REGEX.exec(name); - - // Not a constructor if name has no characters apart from '_', '$' and digits e.g. '_', '$$', '_8' - if (!match) { - return false; - } - - const firstChar = name.charAt(match.index); - - return firstChar === firstChar.toUpperCase(); -} - -/** - * Determines if the property can have a shorthand form. - * @param {ASTNode} property Property AST node - * @returns {boolean} True if the property can have a shorthand form - * @private - */ -function canHaveShorthand(property) { - return ( - property.kind !== "set" && - property.kind !== "get" && - property.type !== "SpreadElement" && - property.type !== "SpreadProperty" && - property.type !== "ExperimentalSpreadProperty" - ); -} - -/** - * Checks whether a node is a string literal. - * @param {ASTNode} node Any AST node. - * @returns {boolean} `true` if it is a string literal. - */ -function isStringLiteral(node) { - return node.type === "Literal" && typeof node.value === "string"; -} - -/** - * Determines if the property is a shorthand or not. - * @param {ASTNode} property Property AST node - * @returns {boolean} True if the property is considered shorthand, false if not. - * @private - */ -function isShorthand(property) { - // property.method is true when `{a(){}}`. - return property.shorthand || property.method; -} - -/** - * Determines if the property's key and method or value are named equally. - * @param {ASTNode} property Property AST node - * @returns {boolean} True if the key and value are named equally, false if not. - * @private - */ -function isRedundant(property) { - const value = property.value; - - if (value.type === "FunctionExpression") { - return !value.id; // Only anonymous should be shorthand method. - } - if (value.type === "Identifier") { - return astUtils.getStaticPropertyName(property) === value.name; - } - - return false; -} - //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: - "Require or disallow method and property shorthand syntax for object literals", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/object-shorthand", - }, - - fixable: "code", - - schema: { - anyOf: [ - { - type: "array", - items: [ - { - enum: [ - "always", - "methods", - "properties", - "never", - "consistent", - "consistent-as-needed", - ], - }, - ], - minItems: 0, - maxItems: 1, - }, - { - type: "array", - items: [ - { - enum: ["always", "methods", "properties"], - }, - { - type: "object", - properties: { - avoidQuotes: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - minItems: 0, - maxItems: 2, - }, - { - type: "array", - items: [ - { - enum: ["always", "methods"], - }, - { - type: "object", - properties: { - ignoreConstructors: { - type: "boolean", - }, - methodsIgnorePattern: { - type: "string", - }, - avoidQuotes: { - type: "boolean", - }, - avoidExplicitReturnArrows: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - minItems: 0, - maxItems: 2, - }, - ], - }, - - messages: { - expectedAllPropertiesShorthanded: - "Expected shorthand for all properties.", - expectedLiteralMethodLongform: - "Expected longform method syntax for string literal keys.", - expectedPropertyShorthand: "Expected property shorthand.", - expectedPropertyLongform: "Expected longform property syntax.", - expectedMethodShorthand: "Expected method shorthand.", - expectedMethodLongform: "Expected longform method syntax.", - unexpectedMix: - "Unexpected mix of shorthand and non-shorthand properties.", - }, - }, - - create(context) { - const APPLY = context.options[0] || OPTIONS.always; - const APPLY_TO_METHODS = - APPLY === OPTIONS.methods || APPLY === OPTIONS.always; - const APPLY_TO_PROPS = - APPLY === OPTIONS.properties || APPLY === OPTIONS.always; - const APPLY_NEVER = APPLY === OPTIONS.never; - const APPLY_CONSISTENT = APPLY === OPTIONS.consistent; - const APPLY_CONSISTENT_AS_NEEDED = APPLY === OPTIONS.consistentAsNeeded; - - const PARAMS = context.options[1] || {}; - const IGNORE_CONSTRUCTORS = PARAMS.ignoreConstructors; - const METHODS_IGNORE_PATTERN = PARAMS.methodsIgnorePattern - ? new RegExp(PARAMS.methodsIgnorePattern, "u") - : null; - const AVOID_QUOTES = PARAMS.avoidQuotes; - const AVOID_EXPLICIT_RETURN_ARROWS = !!PARAMS.avoidExplicitReturnArrows; - const sourceCode = context.sourceCode; - - /** - * Ensures that an object's properties are consistently shorthand, or not shorthand at all. - * @param {ASTNode} node Property AST node - * @param {boolean} checkRedundancy Whether to check longform redundancy - * @returns {void} - */ - function checkConsistency(node, checkRedundancy) { - // We are excluding getters/setters and spread properties as they are considered neither longform nor shorthand. - const properties = node.properties.filter(canHaveShorthand); - - // Do we still have properties left after filtering the getters and setters? - if (properties.length > 0) { - const shorthandProperties = properties.filter(isShorthand); - - /* - * If we do not have an equal number of longform properties as - * shorthand properties, we are using the annotations inconsistently - */ - if (shorthandProperties.length !== properties.length) { - // We have at least 1 shorthand property - if (shorthandProperties.length > 0) { - context.report({ node, messageId: "unexpectedMix" }); - } else if (checkRedundancy) { - /* - * If all properties of the object contain a method or value with a name matching it's key, - * all the keys are redundant. - */ - const canAlwaysUseShorthand = - properties.every(isRedundant); - - if (canAlwaysUseShorthand) { - context.report({ - node, - messageId: "expectedAllPropertiesShorthanded", - }); - } - } - } - } - } - - /** - * Fixes a FunctionExpression node by making it into a shorthand property. - * @param {SourceCodeFixer} fixer The fixer object - * @param {ASTNode} node A `Property` node that has a `FunctionExpression` or `ArrowFunctionExpression` as its value - * @returns {Object} A fix for this node - */ - function makeFunctionShorthand(fixer, node) { - const firstKeyToken = node.computed - ? sourceCode.getFirstToken(node, astUtils.isOpeningBracketToken) - : sourceCode.getFirstToken(node.key); - const lastKeyToken = node.computed - ? sourceCode.getFirstTokenBetween( - node.key, - node.value, - astUtils.isClosingBracketToken, - ) - : sourceCode.getLastToken(node.key); - const keyText = sourceCode.text.slice( - firstKeyToken.range[0], - lastKeyToken.range[1], - ); - let keyPrefix = ""; - - // key: /* */ () => {} - if (sourceCode.commentsExistBetween(lastKeyToken, node.value)) { - return null; - } - - if (node.value.async) { - keyPrefix += "async "; - } - if (node.value.generator) { - keyPrefix += "*"; - } - - const fixRange = [firstKeyToken.range[0], node.range[1]]; - const methodPrefix = keyPrefix + keyText; - - if (node.value.type === "FunctionExpression") { - const functionToken = sourceCode - .getTokens(node.value) - .find( - token => - token.type === "Keyword" && - token.value === "function", - ); - const tokenBeforeParams = node.value.generator - ? sourceCode.getTokenAfter(functionToken) - : functionToken; - - return fixer.replaceTextRange( - fixRange, - methodPrefix + - sourceCode.text.slice( - tokenBeforeParams.range[1], - node.value.range[1], - ), - ); - } - - const arrowToken = sourceCode.getTokenBefore( - node.value.body, - astUtils.isArrowToken, - ); - const fnBody = sourceCode.text.slice( - arrowToken.range[1], - node.value.range[1], - ); - - // First token should not be `async` - const firstValueToken = sourceCode.getFirstToken(node.value, { - skip: node.value.async ? 1 : 0, - }); - - const sliceStart = firstValueToken.range[0]; - const sliceEnd = sourceCode.getTokenBefore(arrowToken).range[1]; - const shouldAddParens = - node.value.params.length === 1 && - node.value.params[0].range[0] === sliceStart; - - const oldParamText = sourceCode.text.slice(sliceStart, sliceEnd); - const newParamText = shouldAddParens - ? `(${oldParamText})` - : oldParamText; - - return fixer.replaceTextRange( - fixRange, - methodPrefix + newParamText + fnBody, - ); - } - - /** - * Fixes a FunctionExpression node by making it into a longform property. - * @param {SourceCodeFixer} fixer The fixer object - * @param {ASTNode} node A `Property` node that has a `FunctionExpression` as its value - * @returns {Object} A fix for this node - */ - function makeFunctionLongform(fixer, node) { - const firstKeyToken = node.computed - ? sourceCode.getTokens(node).find(token => token.value === "[") - : sourceCode.getFirstToken(node.key); - const lastKeyToken = node.computed - ? sourceCode - .getTokensBetween(node.key, node.value) - .find(token => token.value === "]") - : sourceCode.getLastToken(node.key); - const keyText = sourceCode.text.slice( - firstKeyToken.range[0], - lastKeyToken.range[1], - ); - let functionHeader = "function"; - - if (node.value.async) { - functionHeader = `async ${functionHeader}`; - } - if (node.value.generator) { - functionHeader = `${functionHeader}*`; - } - - return fixer.replaceTextRange( - [node.range[0], lastKeyToken.range[1]], - `${keyText}: ${functionHeader}`, - ); - } - - /* - * To determine whether a given arrow function has a lexical identifier (`this`, `arguments`, `super`, or `new.target`), - * create a stack of functions that define these identifiers (i.e. all functions except arrow functions) as the AST is - * traversed. Whenever a new function is encountered, create a new entry on the stack (corresponding to a different lexical - * scope of `this`), and whenever a function is exited, pop that entry off the stack. When an arrow function is entered, - * keep a reference to it on the current stack entry, and remove that reference when the arrow function is exited. - * When a lexical identifier is encountered, mark all the arrow functions on the current stack entry by adding them - * to an `arrowsWithLexicalIdentifiers` set. Any arrow function in that set will not be reported by this rule, - * because converting it into a method would change the value of one of the lexical identifiers. - */ - const lexicalScopeStack = []; - const arrowsWithLexicalIdentifiers = new WeakSet(); - const argumentsIdentifiers = new WeakSet(); - - /** - * Enters a function. This creates a new lexical identifier scope, so a new Set of arrow functions is pushed onto the stack. - * Also, this marks all `arguments` identifiers so that they can be detected later. - * @param {ASTNode} node The node representing the function. - * @returns {void} - */ - function enterFunction(node) { - lexicalScopeStack.unshift(new Set()); - sourceCode - .getScope(node) - .variables.filter(variable => variable.name === "arguments") - .forEach(variable => { - variable.references - .map(ref => ref.identifier) - .forEach(identifier => - argumentsIdentifiers.add(identifier), - ); - }); - } - - /** - * Exits a function. This pops the current set of arrow functions off the lexical scope stack. - * @returns {void} - */ - function exitFunction() { - lexicalScopeStack.shift(); - } - - /** - * Marks the current function as having a lexical keyword. This implies that all arrow functions - * in the current lexical scope contain a reference to this lexical keyword. - * @returns {void} - */ - function reportLexicalIdentifier() { - lexicalScopeStack[0].forEach(arrowFunction => - arrowsWithLexicalIdentifiers.add(arrowFunction), - ); - } - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - return { - Program: enterFunction, - FunctionDeclaration: enterFunction, - FunctionExpression: enterFunction, - "Program:exit": exitFunction, - "FunctionDeclaration:exit": exitFunction, - "FunctionExpression:exit": exitFunction, - - ArrowFunctionExpression(node) { - lexicalScopeStack[0].add(node); - }, - "ArrowFunctionExpression:exit"(node) { - lexicalScopeStack[0].delete(node); - }, - - ThisExpression: reportLexicalIdentifier, - Super: reportLexicalIdentifier, - MetaProperty(node) { - if ( - node.meta.name === "new" && - node.property.name === "target" - ) { - reportLexicalIdentifier(); - } - }, - Identifier(node) { - if (argumentsIdentifiers.has(node)) { - reportLexicalIdentifier(); - } - }, - - ObjectExpression(node) { - if (APPLY_CONSISTENT) { - checkConsistency(node, false); - } else if (APPLY_CONSISTENT_AS_NEEDED) { - checkConsistency(node, true); - } - }, - - "Property:exit"(node) { - const isConciseProperty = node.method || node.shorthand; - - // Ignore destructuring assignment - if (node.parent.type === "ObjectPattern") { - return; - } - - // getters and setters are ignored - if (node.kind === "get" || node.kind === "set") { - return; - } - - // only computed methods can fail the following checks - if ( - node.computed && - node.value.type !== "FunctionExpression" && - node.value.type !== "ArrowFunctionExpression" - ) { - return; - } - - //-------------------------------------------------------------- - // Checks for property/method shorthand. - if (isConciseProperty) { - if ( - node.method && - (APPLY_NEVER || - (AVOID_QUOTES && isStringLiteral(node.key))) - ) { - const messageId = APPLY_NEVER - ? "expectedMethodLongform" - : "expectedLiteralMethodLongform"; - - // { x() {} } should be written as { x: function() {} } - context.report({ - node, - messageId, - fix: fixer => makeFunctionLongform(fixer, node), - }); - } else if (APPLY_NEVER) { - // { x } should be written as { x: x } - context.report({ - node, - messageId: "expectedPropertyLongform", - fix: fixer => - fixer.insertTextAfter( - node.key, - `: ${node.key.name}`, - ), - }); - } - } else if ( - APPLY_TO_METHODS && - !node.value.id && - (node.value.type === "FunctionExpression" || - node.value.type === "ArrowFunctionExpression") - ) { - if ( - IGNORE_CONSTRUCTORS && - node.key.type === "Identifier" && - isConstructor(node.key.name) - ) { - return; - } - - if (METHODS_IGNORE_PATTERN) { - const propertyName = - astUtils.getStaticPropertyName(node); - - if ( - propertyName !== null && - METHODS_IGNORE_PATTERN.test(propertyName) - ) { - return; - } - } - - if (AVOID_QUOTES && isStringLiteral(node.key)) { - return; - } - - // {[x]: function(){}} should be written as {[x]() {}} - if ( - node.value.type === "FunctionExpression" || - (node.value.type === "ArrowFunctionExpression" && - node.value.body.type === "BlockStatement" && - AVOID_EXPLICIT_RETURN_ARROWS && - !arrowsWithLexicalIdentifiers.has(node.value)) - ) { - context.report({ - node, - messageId: "expectedMethodShorthand", - fix: fixer => makeFunctionShorthand(fixer, node), - }); - } - } else if ( - node.value.type === "Identifier" && - node.key.name === node.value.name && - APPLY_TO_PROPS - ) { - // Skip if there are JSDoc comments inside the property (e.g., JSDoc type annotations) - const comments = sourceCode.getCommentsInside(node); - if ( - comments.some( - comment => - comment.type === "Block" && - JSDOC_COMMENT_REGEX.test(comment.value) && - comment.value.includes("@type"), - ) - ) { - return; - } - - // {x: x} should be written as {x} - context.report({ - node, - messageId: "expectedPropertyShorthand", - fix(fixer) { - // x: /* */ x - // x: (/* */ x) - if (sourceCode.getCommentsInside(node).length > 0) { - return null; - } - - return fixer.replaceText(node, node.value.name); - }, - }); - } else if ( - node.value.type === "Identifier" && - node.key.type === "Literal" && - node.key.value === node.value.name && - APPLY_TO_PROPS - ) { - if (AVOID_QUOTES) { - return; - } - - const comments = sourceCode.getCommentsInside(node); - if ( - comments.some( - comment => - comment.type === "Block" && - comment.value.startsWith("*") && - comment.value.includes("@type"), - ) - ) { - return; - } - - // {"x": x} should be written as {x} - context.report({ - node, - messageId: "expectedPropertyShorthand", - fix(fixer) { - // "x": /* */ x - // "x": (/* */ x) - if (sourceCode.getCommentsInside(node).length > 0) { - return null; - } - - return fixer.replaceText(node, node.value.name); - }, - }); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Require or disallow method and property shorthand syntax for object literals", + recommended: false, + url: "https://eslint.org/docs/latest/rules/object-shorthand" + }, + + fixable: "code", + + schema: { + anyOf: [ + { + type: "array", + items: [ + { + enum: ["always", "methods", "properties", "never", "consistent", "consistent-as-needed"] + } + ], + minItems: 0, + maxItems: 1 + }, + { + type: "array", + items: [ + { + enum: ["always", "methods", "properties"] + }, + { + type: "object", + properties: { + avoidQuotes: { + type: "boolean" + } + }, + additionalProperties: false + } + ], + minItems: 0, + maxItems: 2 + }, + { + type: "array", + items: [ + { + enum: ["always", "methods"] + }, + { + type: "object", + properties: { + ignoreConstructors: { + type: "boolean" + }, + methodsIgnorePattern: { + type: "string" + }, + avoidQuotes: { + type: "boolean" + }, + avoidExplicitReturnArrows: { + type: "boolean" + } + }, + additionalProperties: false + } + ], + minItems: 0, + maxItems: 2 + } + ] + }, + + messages: { + expectedAllPropertiesShorthanded: "Expected shorthand for all properties.", + expectedLiteralMethodLongform: "Expected longform method syntax for string literal keys.", + expectedPropertyShorthand: "Expected property shorthand.", + expectedPropertyLongform: "Expected longform property syntax.", + expectedMethodShorthand: "Expected method shorthand.", + expectedMethodLongform: "Expected longform method syntax.", + unexpectedMix: "Unexpected mix of shorthand and non-shorthand properties." + } + }, + + create(context) { + const APPLY = context.options[0] || OPTIONS.always; + const APPLY_TO_METHODS = APPLY === OPTIONS.methods || APPLY === OPTIONS.always; + const APPLY_TO_PROPS = APPLY === OPTIONS.properties || APPLY === OPTIONS.always; + const APPLY_NEVER = APPLY === OPTIONS.never; + const APPLY_CONSISTENT = APPLY === OPTIONS.consistent; + const APPLY_CONSISTENT_AS_NEEDED = APPLY === OPTIONS.consistentAsNeeded; + + const PARAMS = context.options[1] || {}; + const IGNORE_CONSTRUCTORS = PARAMS.ignoreConstructors; + const METHODS_IGNORE_PATTERN = PARAMS.methodsIgnorePattern + ? new RegExp(PARAMS.methodsIgnorePattern, "u") + : null; + const AVOID_QUOTES = PARAMS.avoidQuotes; + const AVOID_EXPLICIT_RETURN_ARROWS = !!PARAMS.avoidExplicitReturnArrows; + const sourceCode = context.sourceCode; + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + const CTOR_PREFIX_REGEX = /[^_$0-9]/u; + + /** + * Determines if the first character of the name is a capital letter. + * @param {string} name The name of the node to evaluate. + * @returns {boolean} True if the first character of the property name is a capital letter, false if not. + * @private + */ + function isConstructor(name) { + const match = CTOR_PREFIX_REGEX.exec(name); + + // Not a constructor if name has no characters apart from '_', '$' and digits e.g. '_', '$$', '_8' + if (!match) { + return false; + } + + const firstChar = name.charAt(match.index); + + return firstChar === firstChar.toUpperCase(); + } + + /** + * Determines if the property can have a shorthand form. + * @param {ASTNode} property Property AST node + * @returns {boolean} True if the property can have a shorthand form + * @private + */ + function canHaveShorthand(property) { + return (property.kind !== "set" && property.kind !== "get" && property.type !== "SpreadElement" && property.type !== "SpreadProperty" && property.type !== "ExperimentalSpreadProperty"); + } + + /** + * Checks whether a node is a string literal. + * @param {ASTNode} node Any AST node. + * @returns {boolean} `true` if it is a string literal. + */ + function isStringLiteral(node) { + return node.type === "Literal" && typeof node.value === "string"; + } + + /** + * Determines if the property is a shorthand or not. + * @param {ASTNode} property Property AST node + * @returns {boolean} True if the property is considered shorthand, false if not. + * @private + */ + function isShorthand(property) { + + // property.method is true when `{a(){}}`. + return (property.shorthand || property.method); + } + + /** + * Determines if the property's key and method or value are named equally. + * @param {ASTNode} property Property AST node + * @returns {boolean} True if the key and value are named equally, false if not. + * @private + */ + function isRedundant(property) { + const value = property.value; + + if (value.type === "FunctionExpression") { + return !value.id; // Only anonymous should be shorthand method. + } + if (value.type === "Identifier") { + return astUtils.getStaticPropertyName(property) === value.name; + } + + return false; + } + + /** + * Ensures that an object's properties are consistently shorthand, or not shorthand at all. + * @param {ASTNode} node Property AST node + * @param {boolean} checkRedundancy Whether to check longform redundancy + * @returns {void} + */ + function checkConsistency(node, checkRedundancy) { + + // We are excluding getters/setters and spread properties as they are considered neither longform nor shorthand. + const properties = node.properties.filter(canHaveShorthand); + + // Do we still have properties left after filtering the getters and setters? + if (properties.length > 0) { + const shorthandProperties = properties.filter(isShorthand); + + /* + * If we do not have an equal number of longform properties as + * shorthand properties, we are using the annotations inconsistently + */ + if (shorthandProperties.length !== properties.length) { + + // We have at least 1 shorthand property + if (shorthandProperties.length > 0) { + context.report({ node, messageId: "unexpectedMix" }); + } else if (checkRedundancy) { + + /* + * If all properties of the object contain a method or value with a name matching it's key, + * all the keys are redundant. + */ + const canAlwaysUseShorthand = properties.every(isRedundant); + + if (canAlwaysUseShorthand) { + context.report({ node, messageId: "expectedAllPropertiesShorthanded" }); + } + } + } + } + } + + /** + * Fixes a FunctionExpression node by making it into a shorthand property. + * @param {SourceCodeFixer} fixer The fixer object + * @param {ASTNode} node A `Property` node that has a `FunctionExpression` or `ArrowFunctionExpression` as its value + * @returns {Object} A fix for this node + */ + function makeFunctionShorthand(fixer, node) { + const firstKeyToken = node.computed + ? sourceCode.getFirstToken(node, astUtils.isOpeningBracketToken) + : sourceCode.getFirstToken(node.key); + const lastKeyToken = node.computed + ? sourceCode.getFirstTokenBetween(node.key, node.value, astUtils.isClosingBracketToken) + : sourceCode.getLastToken(node.key); + const keyText = sourceCode.text.slice(firstKeyToken.range[0], lastKeyToken.range[1]); + let keyPrefix = ""; + + // key: /* */ () => {} + if (sourceCode.commentsExistBetween(lastKeyToken, node.value)) { + return null; + } + + if (node.value.async) { + keyPrefix += "async "; + } + if (node.value.generator) { + keyPrefix += "*"; + } + + const fixRange = [firstKeyToken.range[0], node.range[1]]; + const methodPrefix = keyPrefix + keyText; + + if (node.value.type === "FunctionExpression") { + const functionToken = sourceCode.getTokens(node.value).find(token => token.type === "Keyword" && token.value === "function"); + const tokenBeforeParams = node.value.generator ? sourceCode.getTokenAfter(functionToken) : functionToken; + + return fixer.replaceTextRange( + fixRange, + methodPrefix + sourceCode.text.slice(tokenBeforeParams.range[1], node.value.range[1]) + ); + } + + const arrowToken = sourceCode.getTokenBefore(node.value.body, astUtils.isArrowToken); + const fnBody = sourceCode.text.slice(arrowToken.range[1], node.value.range[1]); + + let shouldAddParensAroundParameters = false; + let tokenBeforeParams; + + if (node.value.params.length === 0) { + tokenBeforeParams = sourceCode.getFirstToken(node.value, astUtils.isOpeningParenToken); + } else { + tokenBeforeParams = sourceCode.getTokenBefore(node.value.params[0]); + } + + if (node.value.params.length === 1) { + const hasParen = astUtils.isOpeningParenToken(tokenBeforeParams); + const isTokenOutsideNode = tokenBeforeParams.range[0] < node.range[0]; + + shouldAddParensAroundParameters = !hasParen || isTokenOutsideNode; + } + + const sliceStart = shouldAddParensAroundParameters + ? node.value.params[0].range[0] + : tokenBeforeParams.range[0]; + const sliceEnd = sourceCode.getTokenBefore(arrowToken).range[1]; + + const oldParamText = sourceCode.text.slice(sliceStart, sliceEnd); + const newParamText = shouldAddParensAroundParameters ? `(${oldParamText})` : oldParamText; + + return fixer.replaceTextRange( + fixRange, + methodPrefix + newParamText + fnBody + ); + + } + + /** + * Fixes a FunctionExpression node by making it into a longform property. + * @param {SourceCodeFixer} fixer The fixer object + * @param {ASTNode} node A `Property` node that has a `FunctionExpression` as its value + * @returns {Object} A fix for this node + */ + function makeFunctionLongform(fixer, node) { + const firstKeyToken = node.computed ? sourceCode.getTokens(node).find(token => token.value === "[") : sourceCode.getFirstToken(node.key); + const lastKeyToken = node.computed ? sourceCode.getTokensBetween(node.key, node.value).find(token => token.value === "]") : sourceCode.getLastToken(node.key); + const keyText = sourceCode.text.slice(firstKeyToken.range[0], lastKeyToken.range[1]); + let functionHeader = "function"; + + if (node.value.async) { + functionHeader = `async ${functionHeader}`; + } + if (node.value.generator) { + functionHeader = `${functionHeader}*`; + } + + return fixer.replaceTextRange([node.range[0], lastKeyToken.range[1]], `${keyText}: ${functionHeader}`); + } + + /* + * To determine whether a given arrow function has a lexical identifier (`this`, `arguments`, `super`, or `new.target`), + * create a stack of functions that define these identifiers (i.e. all functions except arrow functions) as the AST is + * traversed. Whenever a new function is encountered, create a new entry on the stack (corresponding to a different lexical + * scope of `this`), and whenever a function is exited, pop that entry off the stack. When an arrow function is entered, + * keep a reference to it on the current stack entry, and remove that reference when the arrow function is exited. + * When a lexical identifier is encountered, mark all the arrow functions on the current stack entry by adding them + * to an `arrowsWithLexicalIdentifiers` set. Any arrow function in that set will not be reported by this rule, + * because converting it into a method would change the value of one of the lexical identifiers. + */ + const lexicalScopeStack = []; + const arrowsWithLexicalIdentifiers = new WeakSet(); + const argumentsIdentifiers = new WeakSet(); + + /** + * Enters a function. This creates a new lexical identifier scope, so a new Set of arrow functions is pushed onto the stack. + * Also, this marks all `arguments` identifiers so that they can be detected later. + * @param {ASTNode} node The node representing the function. + * @returns {void} + */ + function enterFunction(node) { + lexicalScopeStack.unshift(new Set()); + sourceCode.getScope(node).variables.filter(variable => variable.name === "arguments").forEach(variable => { + variable.references.map(ref => ref.identifier).forEach(identifier => argumentsIdentifiers.add(identifier)); + }); + } + + /** + * Exits a function. This pops the current set of arrow functions off the lexical scope stack. + * @returns {void} + */ + function exitFunction() { + lexicalScopeStack.shift(); + } + + /** + * Marks the current function as having a lexical keyword. This implies that all arrow functions + * in the current lexical scope contain a reference to this lexical keyword. + * @returns {void} + */ + function reportLexicalIdentifier() { + lexicalScopeStack[0].forEach(arrowFunction => arrowsWithLexicalIdentifiers.add(arrowFunction)); + } + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + return { + Program: enterFunction, + FunctionDeclaration: enterFunction, + FunctionExpression: enterFunction, + "Program:exit": exitFunction, + "FunctionDeclaration:exit": exitFunction, + "FunctionExpression:exit": exitFunction, + + ArrowFunctionExpression(node) { + lexicalScopeStack[0].add(node); + }, + "ArrowFunctionExpression:exit"(node) { + lexicalScopeStack[0].delete(node); + }, + + ThisExpression: reportLexicalIdentifier, + Super: reportLexicalIdentifier, + MetaProperty(node) { + if (node.meta.name === "new" && node.property.name === "target") { + reportLexicalIdentifier(); + } + }, + Identifier(node) { + if (argumentsIdentifiers.has(node)) { + reportLexicalIdentifier(); + } + }, + + ObjectExpression(node) { + if (APPLY_CONSISTENT) { + checkConsistency(node, false); + } else if (APPLY_CONSISTENT_AS_NEEDED) { + checkConsistency(node, true); + } + }, + + "Property:exit"(node) { + const isConciseProperty = node.method || node.shorthand; + + // Ignore destructuring assignment + if (node.parent.type === "ObjectPattern") { + return; + } + + // getters and setters are ignored + if (node.kind === "get" || node.kind === "set") { + return; + } + + // only computed methods can fail the following checks + if (node.computed && node.value.type !== "FunctionExpression" && node.value.type !== "ArrowFunctionExpression") { + return; + } + + //-------------------------------------------------------------- + // Checks for property/method shorthand. + if (isConciseProperty) { + if (node.method && (APPLY_NEVER || AVOID_QUOTES && isStringLiteral(node.key))) { + const messageId = APPLY_NEVER ? "expectedMethodLongform" : "expectedLiteralMethodLongform"; + + // { x() {} } should be written as { x: function() {} } + context.report({ + node, + messageId, + fix: fixer => makeFunctionLongform(fixer, node) + }); + } else if (APPLY_NEVER) { + + // { x } should be written as { x: x } + context.report({ + node, + messageId: "expectedPropertyLongform", + fix: fixer => fixer.insertTextAfter(node.key, `: ${node.key.name}`) + }); + } + } else if (APPLY_TO_METHODS && !node.value.id && (node.value.type === "FunctionExpression" || node.value.type === "ArrowFunctionExpression")) { + if (IGNORE_CONSTRUCTORS && node.key.type === "Identifier" && isConstructor(node.key.name)) { + return; + } + + if (METHODS_IGNORE_PATTERN) { + const propertyName = astUtils.getStaticPropertyName(node); + + if (propertyName !== null && METHODS_IGNORE_PATTERN.test(propertyName)) { + return; + } + } + + if (AVOID_QUOTES && isStringLiteral(node.key)) { + return; + } + + // {[x]: function(){}} should be written as {[x]() {}} + if (node.value.type === "FunctionExpression" || + node.value.type === "ArrowFunctionExpression" && + node.value.body.type === "BlockStatement" && + AVOID_EXPLICIT_RETURN_ARROWS && + !arrowsWithLexicalIdentifiers.has(node.value) + ) { + context.report({ + node, + messageId: "expectedMethodShorthand", + fix: fixer => makeFunctionShorthand(fixer, node) + }); + } + } else if (node.value.type === "Identifier" && node.key.name === node.value.name && APPLY_TO_PROPS) { + + // {x: x} should be written as {x} + context.report({ + node, + messageId: "expectedPropertyShorthand", + fix(fixer) { + return fixer.replaceText(node, node.value.name); + } + }); + } else if (node.value.type === "Identifier" && node.key.type === "Literal" && node.key.value === node.value.name && APPLY_TO_PROPS) { + if (AVOID_QUOTES) { + return; + } + + // {"x": x} should be written as {x} + context.report({ + node, + messageId: "expectedPropertyShorthand", + fix(fixer) { + return fixer.replaceText(node, node.value.name); + } + }); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/one-var-declaration-per-line.js b/node_modules/eslint/lib/rules/one-var-declaration-per-line.js index 3671e2721..340eac169 100644 --- a/node_modules/eslint/lib/rules/one-var-declaration-per-line.js +++ b/node_modules/eslint/lib/rules/one-var-declaration-per-line.js @@ -9,109 +9,87 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "one-var-declaration-per-line", - url: "https://eslint.style/rules/one-var-declaration-per-line", - }, - }, - ], - }, - type: "suggestion", - - docs: { - description: - "Require or disallow newlines around variable declarations", - recommended: false, - url: "https://eslint.org/docs/latest/rules/one-var-declaration-per-line", - }, - - schema: [ - { - enum: ["always", "initializations"], - }, - ], - - fixable: "whitespace", - - messages: { - expectVarOnNewline: - "Expected variable declaration to be on a new line.", - }, - }, - - create(context) { - const always = context.options[0] === "always"; - - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - /** - * Determine if provided keyword is a variant of for specifiers - * @private - * @param {string} keyword keyword to test - * @returns {boolean} True if `keyword` is a variant of for specifier - */ - function isForTypeSpecifier(keyword) { - return ( - keyword === "ForStatement" || - keyword === "ForInStatement" || - keyword === "ForOfStatement" - ); - } - - /** - * Checks newlines around variable declarations. - * @private - * @param {ASTNode} node `VariableDeclaration` node to test - * @returns {void} - */ - function checkForNewLine(node) { - if (isForTypeSpecifier(node.parent.type)) { - return; - } - - const declarations = node.declarations; - let prev; - - declarations.forEach(current => { - if (prev && prev.loc.end.line === current.loc.start.line) { - if (always || prev.init || current.init) { - context.report({ - node, - messageId: "expectVarOnNewline", - loc: current.loc, - fix: fixer => fixer.insertTextBefore(current, "\n"), - }); - } - } - prev = current; - }); - } - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - return { - VariableDeclaration: checkForNewLine, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "suggestion", + + docs: { + description: "Require or disallow newlines around variable declarations", + recommended: false, + url: "https://eslint.org/docs/latest/rules/one-var-declaration-per-line" + }, + + schema: [ + { + enum: ["always", "initializations"] + } + ], + + fixable: "whitespace", + + messages: { + expectVarOnNewline: "Expected variable declaration to be on a new line." + } + }, + + create(context) { + + const always = context.options[0] === "always"; + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + + /** + * Determine if provided keyword is a variant of for specifiers + * @private + * @param {string} keyword keyword to test + * @returns {boolean} True if `keyword` is a variant of for specifier + */ + function isForTypeSpecifier(keyword) { + return keyword === "ForStatement" || keyword === "ForInStatement" || keyword === "ForOfStatement"; + } + + /** + * Checks newlines around variable declarations. + * @private + * @param {ASTNode} node `VariableDeclaration` node to test + * @returns {void} + */ + function checkForNewLine(node) { + if (isForTypeSpecifier(node.parent.type)) { + return; + } + + const declarations = node.declarations; + let prev; + + declarations.forEach(current => { + if (prev && prev.loc.end.line === current.loc.start.line) { + if (always || prev.init || current.init) { + context.report({ + node, + messageId: "expectVarOnNewline", + loc: current.loc, + fix: fixer => fixer.insertTextBefore(current, "\n") + }); + } + } + prev = current; + }); + } + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + return { + VariableDeclaration: checkForNewLine + }; + + } }; diff --git a/node_modules/eslint/lib/rules/one-var.js b/node_modules/eslint/lib/rules/one-var.js index 6a4cc074d..abb1525b1 100644 --- a/node_modules/eslint/lib/rules/one-var.js +++ b/node_modules/eslint/lib/rules/one-var.js @@ -21,697 +21,547 @@ const astUtils = require("./utils/ast-utils"); * @returns {boolean} `true` if the given node is in a statement list */ function isInStatementList(node) { - return astUtils.STATEMENT_LIST_PARENTS.has(node.parent.type); + return astUtils.STATEMENT_LIST_PARENTS.has(node.parent.type); } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: - "Enforce variables to be declared either together or separately in functions", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/one-var", - }, - - fixable: "code", - - schema: [ - { - oneOf: [ - { - enum: ["always", "never", "consecutive"], - }, - { - type: "object", - properties: { - separateRequires: { - type: "boolean", - }, - var: { - enum: ["always", "never", "consecutive"], - }, - let: { - enum: ["always", "never", "consecutive"], - }, - const: { - enum: ["always", "never", "consecutive"], - }, - using: { - enum: ["always", "never", "consecutive"], - }, - awaitUsing: { - enum: ["always", "never", "consecutive"], - }, - }, - additionalProperties: false, - }, - { - type: "object", - properties: { - initialized: { - enum: ["always", "never", "consecutive"], - }, - uninitialized: { - enum: ["always", "never", "consecutive"], - }, - }, - additionalProperties: false, - }, - ], - }, - ], - - messages: { - combineUninitialized: - "Combine this with the previous '{{type}}' statement with uninitialized variables.", - combineInitialized: - "Combine this with the previous '{{type}}' statement with initialized variables.", - splitUninitialized: - "Split uninitialized '{{type}}' declarations into multiple statements.", - splitInitialized: - "Split initialized '{{type}}' declarations into multiple statements.", - splitRequires: - "Split requires to be separated into a single block.", - combine: "Combine this with the previous '{{type}}' statement.", - split: "Split '{{type}}' declarations into multiple statements.", - }, - }, - - create(context) { - const MODE_ALWAYS = "always"; - const MODE_NEVER = "never"; - const MODE_CONSECUTIVE = "consecutive"; - const mode = context.options[0] || MODE_ALWAYS; - - const options = {}; - - if (typeof mode === "string") { - // simple options configuration with just a string - options.var = { uninitialized: mode, initialized: mode }; - options.let = { uninitialized: mode, initialized: mode }; - options.const = { uninitialized: mode, initialized: mode }; - options.using = { uninitialized: mode, initialized: mode }; - options.awaitUsing = { uninitialized: mode, initialized: mode }; - } else if (typeof mode === "object") { - // options configuration is an object - options.separateRequires = !!mode.separateRequires; - options.var = { uninitialized: mode.var, initialized: mode.var }; - options.let = { uninitialized: mode.let, initialized: mode.let }; - options.const = { - uninitialized: mode.const, - initialized: mode.const, - }; - options.using = { - uninitialized: mode.using, - initialized: mode.using, - }; - options.awaitUsing = { - uninitialized: mode.awaitUsing, - initialized: mode.awaitUsing, - }; - if (Object.hasOwn(mode, "uninitialized")) { - options.var.uninitialized = mode.uninitialized; - options.let.uninitialized = mode.uninitialized; - options.const.uninitialized = mode.uninitialized; - options.using.uninitialized = mode.uninitialized; - options.awaitUsing.uninitialized = mode.uninitialized; - } - if (Object.hasOwn(mode, "initialized")) { - options.var.initialized = mode.initialized; - options.let.initialized = mode.initialized; - options.const.initialized = mode.initialized; - options.using.initialized = mode.initialized; - options.awaitUsing.initialized = mode.initialized; - } - } - - const sourceCode = context.sourceCode; - - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - const functionStack = []; - const blockStack = []; - - /** - * Increments the blockStack counter. - * @returns {void} - * @private - */ - function startBlock() { - blockStack.push({ - let: { initialized: false, uninitialized: false }, - const: { initialized: false, uninitialized: false }, - using: { initialized: false, uninitialized: false }, - awaitUsing: { initialized: false, uninitialized: false }, - }); - } - - /** - * Increments the functionStack counter. - * @returns {void} - * @private - */ - function startFunction() { - functionStack.push({ initialized: false, uninitialized: false }); - startBlock(); - } - - /** - * Decrements the blockStack counter. - * @returns {void} - * @private - */ - function endBlock() { - blockStack.pop(); - } - - /** - * Decrements the functionStack counter. - * @returns {void} - * @private - */ - function endFunction() { - functionStack.pop(); - endBlock(); - } - - /** - * Check if a variable declaration is a require. - * @param {ASTNode} decl variable declaration Node - * @returns {bool} if decl is a require, return true; else return false. - * @private - */ - function isRequire(decl) { - return ( - decl.init && - decl.init.type === "CallExpression" && - decl.init.callee.name === "require" - ); - } - - /** - * Records whether initialized/uninitialized/required variables are defined in current scope. - * @param {string} statementType one of: "var", "let", "const", "using", or "awaitUsing" - * @param {ASTNode[]} declarations List of declarations - * @param {Object} currentScope The scope being investigated - * @returns {void} - * @private - */ - function recordTypes(statementType, declarations, currentScope) { - for (let i = 0; i < declarations.length; i++) { - if (declarations[i].init === null) { - if ( - options[statementType] && - options[statementType].uninitialized === MODE_ALWAYS - ) { - currentScope.uninitialized = true; - } - } else { - if ( - options[statementType] && - options[statementType].initialized === MODE_ALWAYS - ) { - if ( - options.separateRequires && - isRequire(declarations[i]) - ) { - currentScope.required = true; - } else { - currentScope.initialized = true; - } - } - } - } - } - - /** - * Determines the current scope (function or block) - * @param {string} statementType one of: "var", "let", "const", "using", or "awaitUsing" - * @returns {Object} The scope associated with statementType - */ - function getCurrentScope(statementType) { - let currentScope; - - if (statementType === "var") { - currentScope = functionStack.at(-1); - } else if (statementType === "let") { - currentScope = blockStack.at(-1).let; - } else if (statementType === "const") { - currentScope = blockStack.at(-1).const; - } else if (statementType === "using") { - currentScope = blockStack.at(-1).using; - } else if (statementType === "awaitUsing") { - currentScope = blockStack.at(-1).awaitUsing; - } - return currentScope; - } - - /** - * Counts the number of initialized and uninitialized declarations in a list of declarations - * @param {ASTNode[]} declarations List of declarations - * @returns {Object} Counts of 'uninitialized' and 'initialized' declarations - * @private - */ - function countDeclarations(declarations) { - const counts = { uninitialized: 0, initialized: 0 }; - - for (let i = 0; i < declarations.length; i++) { - if (declarations[i].init === null) { - counts.uninitialized++; - } else { - counts.initialized++; - } - } - return counts; - } - - /** - * Determines if there is more than one var statement in the current scope. - * @param {string} statementType one of: "var", "let", "const", "using", or "awaitUsing" - * @param {ASTNode[]} declarations List of declarations - * @returns {boolean} Returns true if it is the first var declaration, false if not. - * @private - */ - function hasOnlyOneStatement(statementType, declarations) { - const declarationCounts = countDeclarations(declarations); - const currentOptions = options[statementType] || {}; - const currentScope = getCurrentScope(statementType); - const hasRequires = declarations.some(isRequire); - - if ( - currentOptions.uninitialized === MODE_ALWAYS && - currentOptions.initialized === MODE_ALWAYS - ) { - if (currentScope.uninitialized || currentScope.initialized) { - if (!hasRequires) { - return false; - } - } - } - - if (declarationCounts.uninitialized > 0) { - if ( - currentOptions.uninitialized === MODE_ALWAYS && - currentScope.uninitialized - ) { - return false; - } - } - if (declarationCounts.initialized > 0) { - if ( - currentOptions.initialized === MODE_ALWAYS && - currentScope.initialized - ) { - if (!hasRequires) { - return false; - } - } - } - if (currentScope.required && hasRequires) { - return false; - } - recordTypes(statementType, declarations, currentScope); - return true; - } - - /** - * Fixer to join VariableDeclaration's into a single declaration - * @param {VariableDeclarator[]} declarations The `VariableDeclaration` to join - * @returns {Function} The fixer function - */ - function joinDeclarations(declarations) { - const declaration = declarations[0]; - const body = Array.isArray(declaration.parent.parent.body) - ? declaration.parent.parent.body - : []; - const currentIndex = body.findIndex( - node => node.range[0] === declaration.parent.range[0], - ); - const previousNode = body[currentIndex - 1]; - - return function* joinDeclarationsFixer(fixer) { - const type = sourceCode.getFirstToken(declaration.parent); - const beforeType = sourceCode.getTokenBefore(type); - - if ( - previousNode && - previousNode.kind === declaration.parent.kind - ) { - if (beforeType.value === ";") { - yield fixer.replaceText(beforeType, ","); - } else { - yield fixer.insertTextAfter(beforeType, ","); - } - - if (declaration.parent.kind === "await using") { - const usingToken = sourceCode.getTokenAfter(type); - yield fixer.remove(usingToken); - } - - yield fixer.replaceText(type, ""); - } - }; - } - - /** - * Fixer to split a VariableDeclaration into individual declarations - * @param {VariableDeclaration} declaration The `VariableDeclaration` to split - * @returns {Function|null} The fixer function - */ - function splitDeclarations(declaration) { - const { parent } = declaration; - - // don't autofix code such as: if (foo) var x, y; - if ( - !isInStatementList( - parent.type === "ExportNamedDeclaration" - ? parent - : declaration, - ) - ) { - return null; - } - - return fixer => - declaration.declarations - .map(declarator => { - const tokenAfterDeclarator = - sourceCode.getTokenAfter(declarator); - - if (tokenAfterDeclarator === null) { - return null; - } - - const afterComma = sourceCode.getTokenAfter( - tokenAfterDeclarator, - { includeComments: true }, - ); - - if (tokenAfterDeclarator.value !== ",") { - return null; - } - - const exportPlacement = - declaration.parent.type === "ExportNamedDeclaration" - ? "export " - : ""; - - /* - * `var x,y` - * tokenAfterDeclarator ^^ afterComma - */ - if ( - afterComma.range[0] === - tokenAfterDeclarator.range[1] - ) { - return fixer.replaceText( - tokenAfterDeclarator, - `; ${exportPlacement}${declaration.kind} `, - ); - } - - /* - * `var x, - * tokenAfterDeclarator ^ - * y` - * ^ afterComma - */ - if ( - afterComma.loc.start.line > - tokenAfterDeclarator.loc.end.line || - afterComma.type === "Line" || - afterComma.type === "Block" - ) { - let lastComment = afterComma; - - while ( - lastComment.type === "Line" || - lastComment.type === "Block" - ) { - lastComment = sourceCode.getTokenAfter( - lastComment, - { includeComments: true }, - ); - } - - return fixer.replaceTextRange( - [ - tokenAfterDeclarator.range[0], - lastComment.range[0], - ], - `;${sourceCode.text.slice( - tokenAfterDeclarator.range[1], - lastComment.range[0], - )}${exportPlacement}${declaration.kind} `, - ); - } - - return fixer.replaceText( - tokenAfterDeclarator, - `; ${exportPlacement}${declaration.kind}`, - ); - }) - .filter(x => x); - } - - /** - * Checks a given VariableDeclaration node for errors. - * @param {ASTNode} node The VariableDeclaration node to check - * @returns {void} - * @private - */ - function checkVariableDeclaration(node) { - const parent = node.parent; - const type = node.kind; - const key = type === "await using" ? "awaitUsing" : type; - - if (!options[key]) { - return; - } - - const declarations = node.declarations; - const declarationCounts = countDeclarations(declarations); - const mixedRequires = - declarations.some(isRequire) && !declarations.every(isRequire); - - if (options[key].initialized === MODE_ALWAYS) { - if (options.separateRequires && mixedRequires) { - context.report({ - node, - messageId: "splitRequires", - }); - } - } - - // consecutive - const nodeIndex = - (parent.body && - parent.body.length > 0 && - parent.body.indexOf(node)) || - 0; - - if (nodeIndex > 0) { - const previousNode = parent.body[nodeIndex - 1]; - const isPreviousNodeDeclaration = - previousNode.type === "VariableDeclaration"; - const declarationsWithPrevious = declarations.concat( - previousNode.declarations || [], - ); - - if ( - isPreviousNodeDeclaration && - previousNode.kind === type && - !( - declarationsWithPrevious.some(isRequire) && - !declarationsWithPrevious.every(isRequire) - ) - ) { - const previousDeclCounts = countDeclarations( - previousNode.declarations, - ); - - if ( - options[key].initialized === MODE_CONSECUTIVE && - options[key].uninitialized === MODE_CONSECUTIVE - ) { - context.report({ - node, - messageId: "combine", - data: { - type, - }, - fix: joinDeclarations(declarations), - }); - } else if ( - options[key].initialized === MODE_CONSECUTIVE && - declarationCounts.initialized > 0 && - previousDeclCounts.initialized > 0 - ) { - context.report({ - node, - messageId: "combineInitialized", - data: { - type, - }, - fix: joinDeclarations(declarations), - }); - } else if ( - options[key].uninitialized === MODE_CONSECUTIVE && - declarationCounts.uninitialized > 0 && - previousDeclCounts.uninitialized > 0 - ) { - context.report({ - node, - messageId: "combineUninitialized", - data: { - type, - }, - fix: joinDeclarations(declarations), - }); - } - } - } - - // always - if (!hasOnlyOneStatement(key, declarations)) { - if ( - options[key].initialized === MODE_ALWAYS && - options[key].uninitialized === MODE_ALWAYS - ) { - context.report({ - node, - messageId: "combine", - data: { - type, - }, - fix: joinDeclarations(declarations), - }); - } else { - if ( - options[key].initialized === MODE_ALWAYS && - declarationCounts.initialized > 0 - ) { - context.report({ - node, - messageId: "combineInitialized", - data: { - type, - }, - fix: joinDeclarations(declarations), - }); - } - if ( - options[key].uninitialized === MODE_ALWAYS && - declarationCounts.uninitialized > 0 - ) { - if ( - node.parent.left === node && - (node.parent.type === "ForInStatement" || - node.parent.type === "ForOfStatement") - ) { - return; - } - context.report({ - node, - messageId: "combineUninitialized", - data: { - type, - }, - fix: joinDeclarations(declarations), - }); - } - } - } - - // never - if (parent.type !== "ForStatement" || parent.init !== node) { - const totalDeclarations = - declarationCounts.uninitialized + - declarationCounts.initialized; - - if (totalDeclarations > 1) { - if ( - options[key].initialized === MODE_NEVER && - options[key].uninitialized === MODE_NEVER - ) { - // both initialized and uninitialized - context.report({ - node, - messageId: "split", - data: { - type, - }, - fix: splitDeclarations(node), - }); - } else if ( - options[key].initialized === MODE_NEVER && - declarationCounts.initialized > 0 - ) { - // initialized - context.report({ - node, - messageId: "splitInitialized", - data: { - type, - }, - fix: splitDeclarations(node), - }); - } else if ( - options[key].uninitialized === MODE_NEVER && - declarationCounts.uninitialized > 0 - ) { - // uninitialized - context.report({ - node, - messageId: "splitUninitialized", - data: { - type, - }, - fix: splitDeclarations(node), - }); - } - } - } - } - - //-------------------------------------------------------------------------- - // Public API - //-------------------------------------------------------------------------- - - return { - Program: startFunction, - FunctionDeclaration: startFunction, - FunctionExpression: startFunction, - ArrowFunctionExpression: startFunction, - StaticBlock: startFunction, // StaticBlock creates a new scope for `var` variables - - BlockStatement: startBlock, - ForStatement: startBlock, - ForInStatement: startBlock, - ForOfStatement: startBlock, - SwitchStatement: startBlock, - VariableDeclaration: checkVariableDeclaration, - "ForStatement:exit": endBlock, - "ForOfStatement:exit": endBlock, - "ForInStatement:exit": endBlock, - "SwitchStatement:exit": endBlock, - "BlockStatement:exit": endBlock, - - "Program:exit": endFunction, - "FunctionDeclaration:exit": endFunction, - "FunctionExpression:exit": endFunction, - "ArrowFunctionExpression:exit": endFunction, - "StaticBlock:exit": endFunction, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Enforce variables to be declared either together or separately in functions", + recommended: false, + url: "https://eslint.org/docs/latest/rules/one-var" + }, + + fixable: "code", + + schema: [ + { + oneOf: [ + { + enum: ["always", "never", "consecutive"] + }, + { + type: "object", + properties: { + separateRequires: { + type: "boolean" + }, + var: { + enum: ["always", "never", "consecutive"] + }, + let: { + enum: ["always", "never", "consecutive"] + }, + const: { + enum: ["always", "never", "consecutive"] + } + }, + additionalProperties: false + }, + { + type: "object", + properties: { + initialized: { + enum: ["always", "never", "consecutive"] + }, + uninitialized: { + enum: ["always", "never", "consecutive"] + } + }, + additionalProperties: false + } + ] + } + ], + + messages: { + combineUninitialized: "Combine this with the previous '{{type}}' statement with uninitialized variables.", + combineInitialized: "Combine this with the previous '{{type}}' statement with initialized variables.", + splitUninitialized: "Split uninitialized '{{type}}' declarations into multiple statements.", + splitInitialized: "Split initialized '{{type}}' declarations into multiple statements.", + splitRequires: "Split requires to be separated into a single block.", + combine: "Combine this with the previous '{{type}}' statement.", + split: "Split '{{type}}' declarations into multiple statements." + } + }, + + create(context) { + const MODE_ALWAYS = "always"; + const MODE_NEVER = "never"; + const MODE_CONSECUTIVE = "consecutive"; + const mode = context.options[0] || MODE_ALWAYS; + + const options = {}; + + if (typeof mode === "string") { // simple options configuration with just a string + options.var = { uninitialized: mode, initialized: mode }; + options.let = { uninitialized: mode, initialized: mode }; + options.const = { uninitialized: mode, initialized: mode }; + } else if (typeof mode === "object") { // options configuration is an object + options.separateRequires = !!mode.separateRequires; + options.var = { uninitialized: mode.var, initialized: mode.var }; + options.let = { uninitialized: mode.let, initialized: mode.let }; + options.const = { uninitialized: mode.const, initialized: mode.const }; + if (Object.prototype.hasOwnProperty.call(mode, "uninitialized")) { + options.var.uninitialized = mode.uninitialized; + options.let.uninitialized = mode.uninitialized; + options.const.uninitialized = mode.uninitialized; + } + if (Object.prototype.hasOwnProperty.call(mode, "initialized")) { + options.var.initialized = mode.initialized; + options.let.initialized = mode.initialized; + options.const.initialized = mode.initialized; + } + } + + const sourceCode = context.sourceCode; + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + const functionStack = []; + const blockStack = []; + + /** + * Increments the blockStack counter. + * @returns {void} + * @private + */ + function startBlock() { + blockStack.push({ + let: { initialized: false, uninitialized: false }, + const: { initialized: false, uninitialized: false } + }); + } + + /** + * Increments the functionStack counter. + * @returns {void} + * @private + */ + function startFunction() { + functionStack.push({ initialized: false, uninitialized: false }); + startBlock(); + } + + /** + * Decrements the blockStack counter. + * @returns {void} + * @private + */ + function endBlock() { + blockStack.pop(); + } + + /** + * Decrements the functionStack counter. + * @returns {void} + * @private + */ + function endFunction() { + functionStack.pop(); + endBlock(); + } + + /** + * Check if a variable declaration is a require. + * @param {ASTNode} decl variable declaration Node + * @returns {bool} if decl is a require, return true; else return false. + * @private + */ + function isRequire(decl) { + return decl.init && decl.init.type === "CallExpression" && decl.init.callee.name === "require"; + } + + /** + * Records whether initialized/uninitialized/required variables are defined in current scope. + * @param {string} statementType node.kind, one of: "var", "let", or "const" + * @param {ASTNode[]} declarations List of declarations + * @param {Object} currentScope The scope being investigated + * @returns {void} + * @private + */ + function recordTypes(statementType, declarations, currentScope) { + for (let i = 0; i < declarations.length; i++) { + if (declarations[i].init === null) { + if (options[statementType] && options[statementType].uninitialized === MODE_ALWAYS) { + currentScope.uninitialized = true; + } + } else { + if (options[statementType] && options[statementType].initialized === MODE_ALWAYS) { + if (options.separateRequires && isRequire(declarations[i])) { + currentScope.required = true; + } else { + currentScope.initialized = true; + } + } + } + } + } + + /** + * Determines the current scope (function or block) + * @param {string} statementType node.kind, one of: "var", "let", or "const" + * @returns {Object} The scope associated with statementType + */ + function getCurrentScope(statementType) { + let currentScope; + + if (statementType === "var") { + currentScope = functionStack[functionStack.length - 1]; + } else if (statementType === "let") { + currentScope = blockStack[blockStack.length - 1].let; + } else if (statementType === "const") { + currentScope = blockStack[blockStack.length - 1].const; + } + return currentScope; + } + + /** + * Counts the number of initialized and uninitialized declarations in a list of declarations + * @param {ASTNode[]} declarations List of declarations + * @returns {Object} Counts of 'uninitialized' and 'initialized' declarations + * @private + */ + function countDeclarations(declarations) { + const counts = { uninitialized: 0, initialized: 0 }; + + for (let i = 0; i < declarations.length; i++) { + if (declarations[i].init === null) { + counts.uninitialized++; + } else { + counts.initialized++; + } + } + return counts; + } + + /** + * Determines if there is more than one var statement in the current scope. + * @param {string} statementType node.kind, one of: "var", "let", or "const" + * @param {ASTNode[]} declarations List of declarations + * @returns {boolean} Returns true if it is the first var declaration, false if not. + * @private + */ + function hasOnlyOneStatement(statementType, declarations) { + + const declarationCounts = countDeclarations(declarations); + const currentOptions = options[statementType] || {}; + const currentScope = getCurrentScope(statementType); + const hasRequires = declarations.some(isRequire); + + if (currentOptions.uninitialized === MODE_ALWAYS && currentOptions.initialized === MODE_ALWAYS) { + if (currentScope.uninitialized || currentScope.initialized) { + if (!hasRequires) { + return false; + } + } + } + + if (declarationCounts.uninitialized > 0) { + if (currentOptions.uninitialized === MODE_ALWAYS && currentScope.uninitialized) { + return false; + } + } + if (declarationCounts.initialized > 0) { + if (currentOptions.initialized === MODE_ALWAYS && currentScope.initialized) { + if (!hasRequires) { + return false; + } + } + } + if (currentScope.required && hasRequires) { + return false; + } + recordTypes(statementType, declarations, currentScope); + return true; + } + + /** + * Fixer to join VariableDeclaration's into a single declaration + * @param {VariableDeclarator[]} declarations The `VariableDeclaration` to join + * @returns {Function} The fixer function + */ + function joinDeclarations(declarations) { + const declaration = declarations[0]; + const body = Array.isArray(declaration.parent.parent.body) ? declaration.parent.parent.body : []; + const currentIndex = body.findIndex(node => node.range[0] === declaration.parent.range[0]); + const previousNode = body[currentIndex - 1]; + + return fixer => { + const type = sourceCode.getTokenBefore(declaration); + const prevSemi = sourceCode.getTokenBefore(type); + const res = []; + + if (previousNode && previousNode.kind === sourceCode.getText(type)) { + if (prevSemi.value === ";") { + res.push(fixer.replaceText(prevSemi, ",")); + } else { + res.push(fixer.insertTextAfter(prevSemi, ",")); + } + res.push(fixer.replaceText(type, "")); + } + + return res; + }; + } + + /** + * Fixer to split a VariableDeclaration into individual declarations + * @param {VariableDeclaration} declaration The `VariableDeclaration` to split + * @returns {Function|null} The fixer function + */ + function splitDeclarations(declaration) { + const { parent } = declaration; + + // don't autofix code such as: if (foo) var x, y; + if (!isInStatementList(parent.type === "ExportNamedDeclaration" ? parent : declaration)) { + return null; + } + + return fixer => declaration.declarations.map(declarator => { + const tokenAfterDeclarator = sourceCode.getTokenAfter(declarator); + + if (tokenAfterDeclarator === null) { + return null; + } + + const afterComma = sourceCode.getTokenAfter(tokenAfterDeclarator, { includeComments: true }); + + if (tokenAfterDeclarator.value !== ",") { + return null; + } + + const exportPlacement = declaration.parent.type === "ExportNamedDeclaration" ? "export " : ""; + + /* + * `var x,y` + * tokenAfterDeclarator ^^ afterComma + */ + if (afterComma.range[0] === tokenAfterDeclarator.range[1]) { + return fixer.replaceText(tokenAfterDeclarator, `; ${exportPlacement}${declaration.kind} `); + } + + /* + * `var x, + * tokenAfterDeclarator ^ + * y` + * ^ afterComma + */ + if ( + afterComma.loc.start.line > tokenAfterDeclarator.loc.end.line || + afterComma.type === "Line" || + afterComma.type === "Block" + ) { + let lastComment = afterComma; + + while (lastComment.type === "Line" || lastComment.type === "Block") { + lastComment = sourceCode.getTokenAfter(lastComment, { includeComments: true }); + } + + return fixer.replaceTextRange( + [tokenAfterDeclarator.range[0], lastComment.range[0]], + `;${sourceCode.text.slice(tokenAfterDeclarator.range[1], lastComment.range[0])}${exportPlacement}${declaration.kind} ` + ); + } + + return fixer.replaceText(tokenAfterDeclarator, `; ${exportPlacement}${declaration.kind}`); + }).filter(x => x); + } + + /** + * Checks a given VariableDeclaration node for errors. + * @param {ASTNode} node The VariableDeclaration node to check + * @returns {void} + * @private + */ + function checkVariableDeclaration(node) { + const parent = node.parent; + const type = node.kind; + + if (!options[type]) { + return; + } + + const declarations = node.declarations; + const declarationCounts = countDeclarations(declarations); + const mixedRequires = declarations.some(isRequire) && !declarations.every(isRequire); + + if (options[type].initialized === MODE_ALWAYS) { + if (options.separateRequires && mixedRequires) { + context.report({ + node, + messageId: "splitRequires" + }); + } + } + + // consecutive + const nodeIndex = (parent.body && parent.body.length > 0 && parent.body.indexOf(node)) || 0; + + if (nodeIndex > 0) { + const previousNode = parent.body[nodeIndex - 1]; + const isPreviousNodeDeclaration = previousNode.type === "VariableDeclaration"; + const declarationsWithPrevious = declarations.concat(previousNode.declarations || []); + + if ( + isPreviousNodeDeclaration && + previousNode.kind === type && + !(declarationsWithPrevious.some(isRequire) && !declarationsWithPrevious.every(isRequire)) + ) { + const previousDeclCounts = countDeclarations(previousNode.declarations); + + if (options[type].initialized === MODE_CONSECUTIVE && options[type].uninitialized === MODE_CONSECUTIVE) { + context.report({ + node, + messageId: "combine", + data: { + type + }, + fix: joinDeclarations(declarations) + }); + } else if (options[type].initialized === MODE_CONSECUTIVE && declarationCounts.initialized > 0 && previousDeclCounts.initialized > 0) { + context.report({ + node, + messageId: "combineInitialized", + data: { + type + }, + fix: joinDeclarations(declarations) + }); + } else if (options[type].uninitialized === MODE_CONSECUTIVE && + declarationCounts.uninitialized > 0 && + previousDeclCounts.uninitialized > 0) { + context.report({ + node, + messageId: "combineUninitialized", + data: { + type + }, + fix: joinDeclarations(declarations) + }); + } + } + } + + // always + if (!hasOnlyOneStatement(type, declarations)) { + if (options[type].initialized === MODE_ALWAYS && options[type].uninitialized === MODE_ALWAYS) { + context.report({ + node, + messageId: "combine", + data: { + type + }, + fix: joinDeclarations(declarations) + }); + } else { + if (options[type].initialized === MODE_ALWAYS && declarationCounts.initialized > 0) { + context.report({ + node, + messageId: "combineInitialized", + data: { + type + }, + fix: joinDeclarations(declarations) + }); + } + if (options[type].uninitialized === MODE_ALWAYS && declarationCounts.uninitialized > 0) { + if (node.parent.left === node && (node.parent.type === "ForInStatement" || node.parent.type === "ForOfStatement")) { + return; + } + context.report({ + node, + messageId: "combineUninitialized", + data: { + type + }, + fix: joinDeclarations(declarations) + }); + } + } + } + + // never + if (parent.type !== "ForStatement" || parent.init !== node) { + const totalDeclarations = declarationCounts.uninitialized + declarationCounts.initialized; + + if (totalDeclarations > 1) { + if (options[type].initialized === MODE_NEVER && options[type].uninitialized === MODE_NEVER) { + + // both initialized and uninitialized + context.report({ + node, + messageId: "split", + data: { + type + }, + fix: splitDeclarations(node) + }); + } else if (options[type].initialized === MODE_NEVER && declarationCounts.initialized > 0) { + + // initialized + context.report({ + node, + messageId: "splitInitialized", + data: { + type + }, + fix: splitDeclarations(node) + }); + } else if (options[type].uninitialized === MODE_NEVER && declarationCounts.uninitialized > 0) { + + // uninitialized + context.report({ + node, + messageId: "splitUninitialized", + data: { + type + }, + fix: splitDeclarations(node) + }); + } + } + } + } + + //-------------------------------------------------------------------------- + // Public API + //-------------------------------------------------------------------------- + + return { + Program: startFunction, + FunctionDeclaration: startFunction, + FunctionExpression: startFunction, + ArrowFunctionExpression: startFunction, + StaticBlock: startFunction, // StaticBlock creates a new scope for `var` variables + + BlockStatement: startBlock, + ForStatement: startBlock, + ForInStatement: startBlock, + ForOfStatement: startBlock, + SwitchStatement: startBlock, + VariableDeclaration: checkVariableDeclaration, + "ForStatement:exit": endBlock, + "ForOfStatement:exit": endBlock, + "ForInStatement:exit": endBlock, + "SwitchStatement:exit": endBlock, + "BlockStatement:exit": endBlock, + + "Program:exit": endFunction, + "FunctionDeclaration:exit": endFunction, + "FunctionExpression:exit": endFunction, + "ArrowFunctionExpression:exit": endFunction, + "StaticBlock:exit": endFunction + }; + + } }; diff --git a/node_modules/eslint/lib/rules/operator-assignment.js b/node_modules/eslint/lib/rules/operator-assignment.js index 50ae9996d..f71d73be7 100644 --- a/node_modules/eslint/lib/rules/operator-assignment.js +++ b/node_modules/eslint/lib/rules/operator-assignment.js @@ -22,7 +22,7 @@ const astUtils = require("./utils/ast-utils"); * shorthand form. */ function isCommutativeOperatorWithShorthand(operator) { - return ["*", "&", "^", "|"].includes(operator); + return ["*", "&", "^", "|"].includes(operator); } /** @@ -33,7 +33,7 @@ function isCommutativeOperatorWithShorthand(operator) { * a shorthand form. */ function isNonCommutativeOperatorWithShorthand(operator) { - return ["+", "-", "/", "%", "<<", ">>", ">>>", "**"].includes(operator); + return ["+", "-", "/", "%", "<<", ">>", ">>>", "**"].includes(operator); } //------------------------------------------------------------------------------ @@ -47,224 +47,163 @@ function isNonCommutativeOperatorWithShorthand(operator) { * @returns {boolean} `true` if the node can be fixed */ function canBeFixed(node) { - return ( - node.type === "Identifier" || - (node.type === "MemberExpression" && - (node.object.type === "Identifier" || - node.object.type === "ThisExpression") && - (!node.computed || node.property.type === "Literal")) - ); + return ( + node.type === "Identifier" || + ( + node.type === "MemberExpression" && + (node.object.type === "Identifier" || node.object.type === "ThisExpression") && + (!node.computed || node.property.type === "Literal") + ) + ); } -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - defaultOptions: ["always"], - - docs: { - description: - "Require or disallow assignment operator shorthand where possible", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/operator-assignment", - }, - - schema: [ - { - enum: ["always", "never"], - }, - ], - - fixable: "code", - messages: { - replaced: - "Assignment (=) can be replaced with operator assignment ({{operator}}).", - unexpected: - "Unexpected operator assignment ({{operator}}) shorthand.", - }, - }, - - create(context) { - const never = context.options[0] === "never"; - const sourceCode = context.sourceCode; - - /** - * Returns the operator token of an AssignmentExpression or BinaryExpression - * @param {ASTNode} node An AssignmentExpression or BinaryExpression node - * @returns {Token} The operator token in the node - */ - function getOperatorToken(node) { - return sourceCode.getFirstTokenBetween( - node.left, - node.right, - token => token.value === node.operator, - ); - } - - /** - * Ensures that an assignment uses the shorthand form where possible. - * @param {ASTNode} node An AssignmentExpression node. - * @returns {void} - */ - function verify(node) { - if ( - node.operator !== "=" || - node.right.type !== "BinaryExpression" - ) { - return; - } - - const left = node.left; - const expr = node.right; - const operator = expr.operator; - - if ( - isCommutativeOperatorWithShorthand(operator) || - isNonCommutativeOperatorWithShorthand(operator) - ) { - const replacementOperator = `${operator}=`; - - if (astUtils.isSameReference(left, expr.left, true)) { - context.report({ - node, - messageId: "replaced", - data: { operator: replacementOperator }, - fix(fixer) { - if (canBeFixed(left) && canBeFixed(expr.left)) { - const equalsToken = getOperatorToken(node); - const operatorToken = getOperatorToken(expr); - const leftText = sourceCode - .getText() - .slice(node.range[0], equalsToken.range[0]); - const rightText = sourceCode - .getText() - .slice( - operatorToken.range[1], - node.right.range[1], - ); - - // Check for comments that would be removed. - if ( - sourceCode.commentsExistBetween( - equalsToken, - operatorToken, - ) - ) { - return null; - } - - return fixer.replaceText( - node, - `${leftText}${replacementOperator}${rightText}`, - ); - } - return null; - }, - }); - } else if ( - astUtils.isSameReference(left, expr.right, true) && - isCommutativeOperatorWithShorthand(operator) - ) { - /* - * This case can't be fixed safely. - * If `a` and `b` both have custom valueOf() behavior, then fixing `a = b * a` to `a *= b` would - * change the execution order of the valueOf() functions. - */ - context.report({ - node, - messageId: "replaced", - data: { operator: replacementOperator }, - }); - } - } - } - - /** - * Warns if an assignment expression uses operator assignment shorthand. - * @param {ASTNode} node An AssignmentExpression node. - * @returns {void} - */ - function prohibit(node) { - if ( - node.operator !== "=" && - !astUtils.isLogicalAssignmentOperator(node.operator) - ) { - context.report({ - node, - messageId: "unexpected", - data: { operator: node.operator }, - fix(fixer) { - if (canBeFixed(node.left)) { - const firstToken = sourceCode.getFirstToken(node); - const operatorToken = getOperatorToken(node); - const leftText = sourceCode - .getText() - .slice(node.range[0], operatorToken.range[0]); - const newOperator = node.operator.slice(0, -1); - let rightText; - - // Check for comments that would be duplicated. - if ( - sourceCode.commentsExistBetween( - firstToken, - operatorToken, - ) - ) { - return null; - } - - // If this change would modify precedence (e.g. `foo *= bar + 1` => `foo = foo * (bar + 1)`), parenthesize the right side. - if ( - astUtils.getPrecedence(node.right) <= - astUtils.getPrecedence({ - type: "BinaryExpression", - operator: newOperator, - }) && - !astUtils.isParenthesised( - sourceCode, - node.right, - ) - ) { - rightText = `${sourceCode.text.slice(operatorToken.range[1], node.right.range[0])}(${sourceCode.getText(node.right)})`; - } else { - const tokenAfterOperator = - sourceCode.getTokenAfter(operatorToken, { - includeComments: true, - }); - let rightTextPrefix = ""; - - if ( - operatorToken.range[1] === - tokenAfterOperator.range[0] && - !astUtils.canTokensBeAdjacent( - { - type: "Punctuator", - value: newOperator, - }, - tokenAfterOperator, - ) - ) { - rightTextPrefix = " "; // foo+=+bar -> foo= foo+ +bar - } - - rightText = `${rightTextPrefix}${sourceCode.text.slice(operatorToken.range[1], node.range[1])}`; - } - - return fixer.replaceText( - node, - `${leftText}= ${leftText}${newOperator}${rightText}`, - ); - } - return null; - }, - }); - } - } - - return { - AssignmentExpression: !never ? verify : prohibit, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Require or disallow assignment operator shorthand where possible", + recommended: false, + url: "https://eslint.org/docs/latest/rules/operator-assignment" + }, + + schema: [ + { + enum: ["always", "never"] + } + ], + + fixable: "code", + messages: { + replaced: "Assignment (=) can be replaced with operator assignment ({{operator}}).", + unexpected: "Unexpected operator assignment ({{operator}}) shorthand." + } + }, + + create(context) { + + const sourceCode = context.sourceCode; + + /** + * Returns the operator token of an AssignmentExpression or BinaryExpression + * @param {ASTNode} node An AssignmentExpression or BinaryExpression node + * @returns {Token} The operator token in the node + */ + function getOperatorToken(node) { + return sourceCode.getFirstTokenBetween(node.left, node.right, token => token.value === node.operator); + } + + /** + * Ensures that an assignment uses the shorthand form where possible. + * @param {ASTNode} node An AssignmentExpression node. + * @returns {void} + */ + function verify(node) { + if (node.operator !== "=" || node.right.type !== "BinaryExpression") { + return; + } + + const left = node.left; + const expr = node.right; + const operator = expr.operator; + + if (isCommutativeOperatorWithShorthand(operator) || isNonCommutativeOperatorWithShorthand(operator)) { + const replacementOperator = `${operator}=`; + + if (astUtils.isSameReference(left, expr.left, true)) { + context.report({ + node, + messageId: "replaced", + data: { operator: replacementOperator }, + fix(fixer) { + if (canBeFixed(left) && canBeFixed(expr.left)) { + const equalsToken = getOperatorToken(node); + const operatorToken = getOperatorToken(expr); + const leftText = sourceCode.getText().slice(node.range[0], equalsToken.range[0]); + const rightText = sourceCode.getText().slice(operatorToken.range[1], node.right.range[1]); + + // Check for comments that would be removed. + if (sourceCode.commentsExistBetween(equalsToken, operatorToken)) { + return null; + } + + return fixer.replaceText(node, `${leftText}${replacementOperator}${rightText}`); + } + return null; + } + }); + } else if (astUtils.isSameReference(left, expr.right, true) && isCommutativeOperatorWithShorthand(operator)) { + + /* + * This case can't be fixed safely. + * If `a` and `b` both have custom valueOf() behavior, then fixing `a = b * a` to `a *= b` would + * change the execution order of the valueOf() functions. + */ + context.report({ + node, + messageId: "replaced", + data: { operator: replacementOperator } + }); + } + } + } + + /** + * Warns if an assignment expression uses operator assignment shorthand. + * @param {ASTNode} node An AssignmentExpression node. + * @returns {void} + */ + function prohibit(node) { + if (node.operator !== "=" && !astUtils.isLogicalAssignmentOperator(node.operator)) { + context.report({ + node, + messageId: "unexpected", + data: { operator: node.operator }, + fix(fixer) { + if (canBeFixed(node.left)) { + const firstToken = sourceCode.getFirstToken(node); + const operatorToken = getOperatorToken(node); + const leftText = sourceCode.getText().slice(node.range[0], operatorToken.range[0]); + const newOperator = node.operator.slice(0, -1); + let rightText; + + // Check for comments that would be duplicated. + if (sourceCode.commentsExistBetween(firstToken, operatorToken)) { + return null; + } + + // If this change would modify precedence (e.g. `foo *= bar + 1` => `foo = foo * (bar + 1)`), parenthesize the right side. + if ( + astUtils.getPrecedence(node.right) <= astUtils.getPrecedence({ type: "BinaryExpression", operator: newOperator }) && + !astUtils.isParenthesised(sourceCode, node.right) + ) { + rightText = `${sourceCode.text.slice(operatorToken.range[1], node.right.range[0])}(${sourceCode.getText(node.right)})`; + } else { + const tokenAfterOperator = sourceCode.getTokenAfter(operatorToken, { includeComments: true }); + let rightTextPrefix = ""; + + if ( + operatorToken.range[1] === tokenAfterOperator.range[0] && + !astUtils.canTokensBeAdjacent({ type: "Punctuator", value: newOperator }, tokenAfterOperator) + ) { + rightTextPrefix = " "; // foo+=+bar -> foo= foo+ +bar + } + + rightText = `${rightTextPrefix}${sourceCode.text.slice(operatorToken.range[1], node.range[1])}`; + } + + return fixer.replaceText(node, `${leftText}= ${leftText}${newOperator}${rightText}`); + } + return null; + } + }); + } + } + + return { + AssignmentExpression: context.options[0] !== "never" ? verify : prohibit + }; + + } }; diff --git a/node_modules/eslint/lib/rules/operator-linebreak.js b/node_modules/eslint/lib/rules/operator-linebreak.js index f0b7d46fa..3065e66be 100644 --- a/node_modules/eslint/lib/rules/operator-linebreak.js +++ b/node_modules/eslint/lib/rules/operator-linebreak.js @@ -16,300 +16,238 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "operator-linebreak", - url: "https://eslint.style/rules/operator-linebreak", - }, - }, - ], - }, - type: "layout", - - docs: { - description: "Enforce consistent linebreak style for operators", - recommended: false, - url: "https://eslint.org/docs/latest/rules/operator-linebreak", - }, - - schema: [ - { - enum: ["after", "before", "none", null], - }, - { - type: "object", - properties: { - overrides: { - type: "object", - additionalProperties: { - enum: ["after", "before", "none", "ignore"], - }, - }, - }, - additionalProperties: false, - }, - ], - - fixable: "code", - - messages: { - operatorAtBeginning: - "'{{operator}}' should be placed at the beginning of the line.", - operatorAtEnd: - "'{{operator}}' should be placed at the end of the line.", - badLinebreak: "Bad line breaking before and after '{{operator}}'.", - noLinebreak: - "There should be no line break before or after '{{operator}}'.", - }, - }, - - create(context) { - const usedDefaultGlobal = !context.options[0]; - const globalStyle = context.options[0] || "after"; - const options = context.options[1] || {}; - const styleOverrides = options.overrides - ? Object.assign({}, options.overrides) - : {}; - - if (usedDefaultGlobal && !styleOverrides["?"]) { - styleOverrides["?"] = "before"; - } - - if (usedDefaultGlobal && !styleOverrides[":"]) { - styleOverrides[":"] = "before"; - } - - const sourceCode = context.sourceCode; - - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - /** - * Gets a fixer function to fix rule issues - * @param {Token} operatorToken The operator token of an expression - * @param {string} desiredStyle The style for the rule. One of 'before', 'after', 'none' - * @returns {Function} A fixer function - */ - function getFixer(operatorToken, desiredStyle) { - return fixer => { - const tokenBefore = sourceCode.getTokenBefore(operatorToken); - const tokenAfter = sourceCode.getTokenAfter(operatorToken); - const textBefore = sourceCode.text.slice( - tokenBefore.range[1], - operatorToken.range[0], - ); - const textAfter = sourceCode.text.slice( - operatorToken.range[1], - tokenAfter.range[0], - ); - const hasLinebreakBefore = !astUtils.isTokenOnSameLine( - tokenBefore, - operatorToken, - ); - const hasLinebreakAfter = !astUtils.isTokenOnSameLine( - operatorToken, - tokenAfter, - ); - let newTextBefore, newTextAfter; - - if ( - hasLinebreakBefore !== hasLinebreakAfter && - desiredStyle !== "none" - ) { - // If there is a comment before and after the operator, don't do a fix. - if ( - sourceCode.getTokenBefore(operatorToken, { - includeComments: true, - }) !== tokenBefore && - sourceCode.getTokenAfter(operatorToken, { - includeComments: true, - }) !== tokenAfter - ) { - return null; - } - - /* - * If there is only one linebreak and it's on the wrong side of the operator, swap the text before and after the operator. - * foo && - * bar - * would get fixed to - * foo - * && bar - */ - newTextBefore = textAfter; - newTextAfter = textBefore; - } else { - const LINEBREAK_REGEX = - astUtils.createGlobalLinebreakMatcher(); - - // Otherwise, if no linebreak is desired and no comments interfere, replace the linebreaks with empty strings. - newTextBefore = - desiredStyle === "before" || textBefore.trim() - ? textBefore - : textBefore.replace(LINEBREAK_REGEX, ""); - newTextAfter = - desiredStyle === "after" || textAfter.trim() - ? textAfter - : textAfter.replace(LINEBREAK_REGEX, ""); - - // If there was no change (due to interfering comments), don't output a fix. - if ( - newTextBefore === textBefore && - newTextAfter === textAfter - ) { - return null; - } - } - - if ( - newTextAfter === "" && - tokenAfter.type === "Punctuator" && - "+-".includes(operatorToken.value) && - tokenAfter.value === operatorToken.value - ) { - // To avoid accidentally creating a ++ or -- operator, insert a space if the operator is a +/- and the following token is a unary +/-. - newTextAfter += " "; - } - - return fixer.replaceTextRange( - [tokenBefore.range[1], tokenAfter.range[0]], - newTextBefore + operatorToken.value + newTextAfter, - ); - }; - } - - /** - * Checks the operator placement - * @param {ASTNode} node The node to check - * @param {ASTNode} rightSide The node that comes after the operator in `node` - * @param {string} operator The operator - * @private - * @returns {void} - */ - function validateNode(node, rightSide, operator) { - /* - * Find the operator token by searching from the right side, because between the left side and the operator - * there could be additional tokens from type annotations. Search specifically for the token which - * value equals the operator, in order to skip possible opening parentheses before the right side node. - */ - const operatorToken = sourceCode.getTokenBefore( - rightSide, - token => token.value === operator, - ); - const leftToken = sourceCode.getTokenBefore(operatorToken); - const rightToken = sourceCode.getTokenAfter(operatorToken); - const operatorStyleOverride = styleOverrides[operator]; - const style = operatorStyleOverride || globalStyle; - const fix = getFixer(operatorToken, style); - - // if single line - if ( - astUtils.isTokenOnSameLine(leftToken, operatorToken) && - astUtils.isTokenOnSameLine(operatorToken, rightToken) - ) { - // do nothing. - } else if ( - operatorStyleOverride !== "ignore" && - !astUtils.isTokenOnSameLine(leftToken, operatorToken) && - !astUtils.isTokenOnSameLine(operatorToken, rightToken) - ) { - // lone operator - context.report({ - node, - loc: operatorToken.loc, - messageId: "badLinebreak", - data: { - operator, - }, - fix, - }); - } else if ( - style === "before" && - astUtils.isTokenOnSameLine(leftToken, operatorToken) - ) { - context.report({ - node, - loc: operatorToken.loc, - messageId: "operatorAtBeginning", - data: { - operator, - }, - fix, - }); - } else if ( - style === "after" && - astUtils.isTokenOnSameLine(operatorToken, rightToken) - ) { - context.report({ - node, - loc: operatorToken.loc, - messageId: "operatorAtEnd", - data: { - operator, - }, - fix, - }); - } else if (style === "none") { - context.report({ - node, - loc: operatorToken.loc, - messageId: "noLinebreak", - data: { - operator, - }, - fix, - }); - } - } - - /** - * Validates a binary expression using `validateNode` - * @param {BinaryExpression|LogicalExpression|AssignmentExpression} node node to be validated - * @returns {void} - */ - function validateBinaryExpression(node) { - validateNode(node, node.right, node.operator); - } - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - return { - BinaryExpression: validateBinaryExpression, - LogicalExpression: validateBinaryExpression, - AssignmentExpression: validateBinaryExpression, - VariableDeclarator(node) { - if (node.init) { - validateNode(node, node.init, "="); - } - }, - PropertyDefinition(node) { - if (node.value) { - validateNode(node, node.value, "="); - } - }, - ConditionalExpression(node) { - validateNode(node, node.consequent, "?"); - validateNode(node, node.alternate, ":"); - }, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Enforce consistent linebreak style for operators", + recommended: false, + url: "https://eslint.org/docs/latest/rules/operator-linebreak" + }, + + schema: [ + { + enum: ["after", "before", "none", null] + }, + { + type: "object", + properties: { + overrides: { + type: "object", + additionalProperties: { + enum: ["after", "before", "none", "ignore"] + } + } + }, + additionalProperties: false + } + ], + + fixable: "code", + + messages: { + operatorAtBeginning: "'{{operator}}' should be placed at the beginning of the line.", + operatorAtEnd: "'{{operator}}' should be placed at the end of the line.", + badLinebreak: "Bad line breaking before and after '{{operator}}'.", + noLinebreak: "There should be no line break before or after '{{operator}}'." + } + }, + + create(context) { + + const usedDefaultGlobal = !context.options[0]; + const globalStyle = context.options[0] || "after"; + const options = context.options[1] || {}; + const styleOverrides = options.overrides ? Object.assign({}, options.overrides) : {}; + + if (usedDefaultGlobal && !styleOverrides["?"]) { + styleOverrides["?"] = "before"; + } + + if (usedDefaultGlobal && !styleOverrides[":"]) { + styleOverrides[":"] = "before"; + } + + const sourceCode = context.sourceCode; + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + /** + * Gets a fixer function to fix rule issues + * @param {Token} operatorToken The operator token of an expression + * @param {string} desiredStyle The style for the rule. One of 'before', 'after', 'none' + * @returns {Function} A fixer function + */ + function getFixer(operatorToken, desiredStyle) { + return fixer => { + const tokenBefore = sourceCode.getTokenBefore(operatorToken); + const tokenAfter = sourceCode.getTokenAfter(operatorToken); + const textBefore = sourceCode.text.slice(tokenBefore.range[1], operatorToken.range[0]); + const textAfter = sourceCode.text.slice(operatorToken.range[1], tokenAfter.range[0]); + const hasLinebreakBefore = !astUtils.isTokenOnSameLine(tokenBefore, operatorToken); + const hasLinebreakAfter = !astUtils.isTokenOnSameLine(operatorToken, tokenAfter); + let newTextBefore, newTextAfter; + + if (hasLinebreakBefore !== hasLinebreakAfter && desiredStyle !== "none") { + + // If there is a comment before and after the operator, don't do a fix. + if (sourceCode.getTokenBefore(operatorToken, { includeComments: true }) !== tokenBefore && + sourceCode.getTokenAfter(operatorToken, { includeComments: true }) !== tokenAfter) { + + return null; + } + + /* + * If there is only one linebreak and it's on the wrong side of the operator, swap the text before and after the operator. + * foo && + * bar + * would get fixed to + * foo + * && bar + */ + newTextBefore = textAfter; + newTextAfter = textBefore; + } else { + const LINEBREAK_REGEX = astUtils.createGlobalLinebreakMatcher(); + + // Otherwise, if no linebreak is desired and no comments interfere, replace the linebreaks with empty strings. + newTextBefore = desiredStyle === "before" || textBefore.trim() ? textBefore : textBefore.replace(LINEBREAK_REGEX, ""); + newTextAfter = desiredStyle === "after" || textAfter.trim() ? textAfter : textAfter.replace(LINEBREAK_REGEX, ""); + + // If there was no change (due to interfering comments), don't output a fix. + if (newTextBefore === textBefore && newTextAfter === textAfter) { + return null; + } + } + + if (newTextAfter === "" && tokenAfter.type === "Punctuator" && "+-".includes(operatorToken.value) && tokenAfter.value === operatorToken.value) { + + // To avoid accidentally creating a ++ or -- operator, insert a space if the operator is a +/- and the following token is a unary +/-. + newTextAfter += " "; + } + + return fixer.replaceTextRange([tokenBefore.range[1], tokenAfter.range[0]], newTextBefore + operatorToken.value + newTextAfter); + }; + } + + /** + * Checks the operator placement + * @param {ASTNode} node The node to check + * @param {ASTNode} rightSide The node that comes after the operator in `node` + * @param {string} operator The operator + * @private + * @returns {void} + */ + function validateNode(node, rightSide, operator) { + + /* + * Find the operator token by searching from the right side, because between the left side and the operator + * there could be additional tokens from type annotations. Search specifically for the token which + * value equals the operator, in order to skip possible opening parentheses before the right side node. + */ + const operatorToken = sourceCode.getTokenBefore(rightSide, token => token.value === operator); + const leftToken = sourceCode.getTokenBefore(operatorToken); + const rightToken = sourceCode.getTokenAfter(operatorToken); + const operatorStyleOverride = styleOverrides[operator]; + const style = operatorStyleOverride || globalStyle; + const fix = getFixer(operatorToken, style); + + // if single line + if (astUtils.isTokenOnSameLine(leftToken, operatorToken) && + astUtils.isTokenOnSameLine(operatorToken, rightToken)) { + + // do nothing. + + } else if (operatorStyleOverride !== "ignore" && !astUtils.isTokenOnSameLine(leftToken, operatorToken) && + !astUtils.isTokenOnSameLine(operatorToken, rightToken)) { + + // lone operator + context.report({ + node, + loc: operatorToken.loc, + messageId: "badLinebreak", + data: { + operator + }, + fix + }); + + } else if (style === "before" && astUtils.isTokenOnSameLine(leftToken, operatorToken)) { + + context.report({ + node, + loc: operatorToken.loc, + messageId: "operatorAtBeginning", + data: { + operator + }, + fix + }); + + } else if (style === "after" && astUtils.isTokenOnSameLine(operatorToken, rightToken)) { + + context.report({ + node, + loc: operatorToken.loc, + messageId: "operatorAtEnd", + data: { + operator + }, + fix + }); + + } else if (style === "none") { + + context.report({ + node, + loc: operatorToken.loc, + messageId: "noLinebreak", + data: { + operator + }, + fix + }); + + } + } + + /** + * Validates a binary expression using `validateNode` + * @param {BinaryExpression|LogicalExpression|AssignmentExpression} node node to be validated + * @returns {void} + */ + function validateBinaryExpression(node) { + validateNode(node, node.right, node.operator); + } + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + return { + BinaryExpression: validateBinaryExpression, + LogicalExpression: validateBinaryExpression, + AssignmentExpression: validateBinaryExpression, + VariableDeclarator(node) { + if (node.init) { + validateNode(node, node.init, "="); + } + }, + PropertyDefinition(node) { + if (node.value) { + validateNode(node, node.value, "="); + } + }, + ConditionalExpression(node) { + validateNode(node, node.consequent, "?"); + validateNode(node, node.alternate, ":"); + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/padded-blocks.js b/node_modules/eslint/lib/rules/padded-blocks.js index 2df44f890..ec4756ba7 100644 --- a/node_modules/eslint/lib/rules/padded-blocks.js +++ b/node_modules/eslint/lib/rules/padded-blocks.js @@ -16,351 +16,295 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "padded-blocks", - url: "https://eslint.style/rules/padded-blocks", - }, - }, - ], - }, - type: "layout", - - docs: { - description: "Require or disallow padding within blocks", - recommended: false, - url: "https://eslint.org/docs/latest/rules/padded-blocks", - }, - - fixable: "whitespace", - - schema: [ - { - oneOf: [ - { - enum: ["always", "never"], - }, - { - type: "object", - properties: { - blocks: { - enum: ["always", "never"], - }, - switches: { - enum: ["always", "never"], - }, - classes: { - enum: ["always", "never"], - }, - }, - additionalProperties: false, - minProperties: 1, - }, - ], - }, - { - type: "object", - properties: { - allowSingleLineBlocks: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - - messages: { - alwaysPadBlock: "Block must be padded by blank lines.", - neverPadBlock: "Block must not be padded by blank lines.", - }, - }, - - create(context) { - const options = {}; - const typeOptions = context.options[0] || "always"; - const exceptOptions = context.options[1] || {}; - - if (typeof typeOptions === "string") { - const shouldHavePadding = typeOptions === "always"; - - options.blocks = shouldHavePadding; - options.switches = shouldHavePadding; - options.classes = shouldHavePadding; - } else { - if (Object.hasOwn(typeOptions, "blocks")) { - options.blocks = typeOptions.blocks === "always"; - } - if (Object.hasOwn(typeOptions, "switches")) { - options.switches = typeOptions.switches === "always"; - } - if (Object.hasOwn(typeOptions, "classes")) { - options.classes = typeOptions.classes === "always"; - } - } - - if (Object.hasOwn(exceptOptions, "allowSingleLineBlocks")) { - options.allowSingleLineBlocks = - exceptOptions.allowSingleLineBlocks === true; - } - - const sourceCode = context.sourceCode; - - /** - * Gets the open brace token from a given node. - * @param {ASTNode} node A BlockStatement or SwitchStatement node from which to get the open brace. - * @returns {Token} The token of the open brace. - */ - function getOpenBrace(node) { - if (node.type === "SwitchStatement") { - return sourceCode.getTokenBefore(node.cases[0]); - } - - if (node.type === "StaticBlock") { - return sourceCode.getFirstToken(node, { skip: 1 }); // skip the `static` token - } - - // `BlockStatement` or `ClassBody` - return sourceCode.getFirstToken(node); - } - - /** - * Checks if the given parameter is a comment node - * @param {ASTNode|Token} node An AST node or token - * @returns {boolean} True if node is a comment - */ - function isComment(node) { - return node.type === "Line" || node.type === "Block"; - } - - /** - * Checks if there is padding between two tokens - * @param {Token} first The first token - * @param {Token} second The second token - * @returns {boolean} True if there is at least a line between the tokens - */ - function isPaddingBetweenTokens(first, second) { - return second.loc.start.line - first.loc.end.line >= 2; - } - - /** - * Checks if the given token has a blank line after it. - * @param {Token} token The token to check. - * @returns {boolean} Whether or not the token is followed by a blank line. - */ - function getFirstBlockToken(token) { - let prev, - first = token; - - do { - prev = first; - first = sourceCode.getTokenAfter(first, { - includeComments: true, - }); - } while ( - isComment(first) && - first.loc.start.line === prev.loc.end.line - ); - - return first; - } - - /** - * Checks if the given token is preceded by a blank line. - * @param {Token} token The token to check - * @returns {boolean} Whether or not the token is preceded by a blank line - */ - function getLastBlockToken(token) { - let last = token, - next; - - do { - next = last; - last = sourceCode.getTokenBefore(last, { - includeComments: true, - }); - } while ( - isComment(last) && - last.loc.end.line === next.loc.start.line - ); - - return last; - } - - /** - * Checks if a node should be padded, according to the rule config. - * @param {ASTNode} node The AST node to check. - * @throws {Error} (Unreachable) - * @returns {boolean} True if the node should be padded, false otherwise. - */ - function requirePaddingFor(node) { - switch (node.type) { - case "BlockStatement": - case "StaticBlock": - return options.blocks; - case "SwitchStatement": - return options.switches; - case "ClassBody": - return options.classes; - - /* c8 ignore next */ - default: - throw new Error("unreachable"); - } - } - - /** - * Checks the given BlockStatement node to be padded if the block is not empty. - * @param {ASTNode} node The AST node of a BlockStatement. - * @returns {void} undefined. - */ - function checkPadding(node) { - const openBrace = getOpenBrace(node), - firstBlockToken = getFirstBlockToken(openBrace), - tokenBeforeFirst = sourceCode.getTokenBefore(firstBlockToken, { - includeComments: true, - }), - closeBrace = sourceCode.getLastToken(node), - lastBlockToken = getLastBlockToken(closeBrace), - tokenAfterLast = sourceCode.getTokenAfter(lastBlockToken, { - includeComments: true, - }), - blockHasTopPadding = isPaddingBetweenTokens( - tokenBeforeFirst, - firstBlockToken, - ), - blockHasBottomPadding = isPaddingBetweenTokens( - lastBlockToken, - tokenAfterLast, - ); - - if ( - options.allowSingleLineBlocks && - astUtils.isTokenOnSameLine(tokenBeforeFirst, tokenAfterLast) - ) { - return; - } - - if (requirePaddingFor(node)) { - if (!blockHasTopPadding) { - context.report({ - node, - loc: { - start: tokenBeforeFirst.loc.start, - end: firstBlockToken.loc.start, - }, - fix(fixer) { - return fixer.insertTextAfter( - tokenBeforeFirst, - "\n", - ); - }, - messageId: "alwaysPadBlock", - }); - } - if (!blockHasBottomPadding) { - context.report({ - node, - loc: { - end: tokenAfterLast.loc.start, - start: lastBlockToken.loc.end, - }, - fix(fixer) { - return fixer.insertTextBefore(tokenAfterLast, "\n"); - }, - messageId: "alwaysPadBlock", - }); - } - } else { - if (blockHasTopPadding) { - context.report({ - node, - loc: { - start: tokenBeforeFirst.loc.start, - end: firstBlockToken.loc.start, - }, - fix(fixer) { - return fixer.replaceTextRange( - [ - tokenBeforeFirst.range[1], - firstBlockToken.range[0] - - firstBlockToken.loc.start.column, - ], - "\n", - ); - }, - messageId: "neverPadBlock", - }); - } - - if (blockHasBottomPadding) { - context.report({ - node, - loc: { - end: tokenAfterLast.loc.start, - start: lastBlockToken.loc.end, - }, - messageId: "neverPadBlock", - fix(fixer) { - return fixer.replaceTextRange( - [ - lastBlockToken.range[1], - tokenAfterLast.range[0] - - tokenAfterLast.loc.start.column, - ], - "\n", - ); - }, - }); - } - } - } - - const rule = {}; - - if (Object.hasOwn(options, "switches")) { - rule.SwitchStatement = function (node) { - if (node.cases.length === 0) { - return; - } - checkPadding(node); - }; - } - - if (Object.hasOwn(options, "blocks")) { - rule.BlockStatement = function (node) { - if (node.body.length === 0) { - return; - } - checkPadding(node); - }; - rule.StaticBlock = rule.BlockStatement; - } - - if (Object.hasOwn(options, "classes")) { - rule.ClassBody = function (node) { - if (node.body.length === 0) { - return; - } - checkPadding(node); - }; - } - - return rule; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Require or disallow padding within blocks", + recommended: false, + url: "https://eslint.org/docs/latest/rules/padded-blocks" + }, + + fixable: "whitespace", + + schema: [ + { + oneOf: [ + { + enum: ["always", "never"] + }, + { + type: "object", + properties: { + blocks: { + enum: ["always", "never"] + }, + switches: { + enum: ["always", "never"] + }, + classes: { + enum: ["always", "never"] + } + }, + additionalProperties: false, + minProperties: 1 + } + ] + }, + { + type: "object", + properties: { + allowSingleLineBlocks: { + type: "boolean" + } + }, + additionalProperties: false + } + ], + + messages: { + alwaysPadBlock: "Block must be padded by blank lines.", + neverPadBlock: "Block must not be padded by blank lines." + } + }, + + create(context) { + const options = {}; + const typeOptions = context.options[0] || "always"; + const exceptOptions = context.options[1] || {}; + + if (typeof typeOptions === "string") { + const shouldHavePadding = typeOptions === "always"; + + options.blocks = shouldHavePadding; + options.switches = shouldHavePadding; + options.classes = shouldHavePadding; + } else { + if (Object.prototype.hasOwnProperty.call(typeOptions, "blocks")) { + options.blocks = typeOptions.blocks === "always"; + } + if (Object.prototype.hasOwnProperty.call(typeOptions, "switches")) { + options.switches = typeOptions.switches === "always"; + } + if (Object.prototype.hasOwnProperty.call(typeOptions, "classes")) { + options.classes = typeOptions.classes === "always"; + } + } + + if (Object.prototype.hasOwnProperty.call(exceptOptions, "allowSingleLineBlocks")) { + options.allowSingleLineBlocks = exceptOptions.allowSingleLineBlocks === true; + } + + const sourceCode = context.sourceCode; + + /** + * Gets the open brace token from a given node. + * @param {ASTNode} node A BlockStatement or SwitchStatement node from which to get the open brace. + * @returns {Token} The token of the open brace. + */ + function getOpenBrace(node) { + if (node.type === "SwitchStatement") { + return sourceCode.getTokenBefore(node.cases[0]); + } + + if (node.type === "StaticBlock") { + return sourceCode.getFirstToken(node, { skip: 1 }); // skip the `static` token + } + + // `BlockStatement` or `ClassBody` + return sourceCode.getFirstToken(node); + } + + /** + * Checks if the given parameter is a comment node + * @param {ASTNode|Token} node An AST node or token + * @returns {boolean} True if node is a comment + */ + function isComment(node) { + return node.type === "Line" || node.type === "Block"; + } + + /** + * Checks if there is padding between two tokens + * @param {Token} first The first token + * @param {Token} second The second token + * @returns {boolean} True if there is at least a line between the tokens + */ + function isPaddingBetweenTokens(first, second) { + return second.loc.start.line - first.loc.end.line >= 2; + } + + + /** + * Checks if the given token has a blank line after it. + * @param {Token} token The token to check. + * @returns {boolean} Whether or not the token is followed by a blank line. + */ + function getFirstBlockToken(token) { + let prev, + first = token; + + do { + prev = first; + first = sourceCode.getTokenAfter(first, { includeComments: true }); + } while (isComment(first) && first.loc.start.line === prev.loc.end.line); + + return first; + } + + /** + * Checks if the given token is preceded by a blank line. + * @param {Token} token The token to check + * @returns {boolean} Whether or not the token is preceded by a blank line + */ + function getLastBlockToken(token) { + let last = token, + next; + + do { + next = last; + last = sourceCode.getTokenBefore(last, { includeComments: true }); + } while (isComment(last) && last.loc.end.line === next.loc.start.line); + + return last; + } + + /** + * Checks if a node should be padded, according to the rule config. + * @param {ASTNode} node The AST node to check. + * @throws {Error} (Unreachable) + * @returns {boolean} True if the node should be padded, false otherwise. + */ + function requirePaddingFor(node) { + switch (node.type) { + case "BlockStatement": + case "StaticBlock": + return options.blocks; + case "SwitchStatement": + return options.switches; + case "ClassBody": + return options.classes; + + /* c8 ignore next */ + default: + throw new Error("unreachable"); + } + } + + /** + * Checks the given BlockStatement node to be padded if the block is not empty. + * @param {ASTNode} node The AST node of a BlockStatement. + * @returns {void} undefined. + */ + function checkPadding(node) { + const openBrace = getOpenBrace(node), + firstBlockToken = getFirstBlockToken(openBrace), + tokenBeforeFirst = sourceCode.getTokenBefore(firstBlockToken, { includeComments: true }), + closeBrace = sourceCode.getLastToken(node), + lastBlockToken = getLastBlockToken(closeBrace), + tokenAfterLast = sourceCode.getTokenAfter(lastBlockToken, { includeComments: true }), + blockHasTopPadding = isPaddingBetweenTokens(tokenBeforeFirst, firstBlockToken), + blockHasBottomPadding = isPaddingBetweenTokens(lastBlockToken, tokenAfterLast); + + if (options.allowSingleLineBlocks && astUtils.isTokenOnSameLine(tokenBeforeFirst, tokenAfterLast)) { + return; + } + + if (requirePaddingFor(node)) { + + if (!blockHasTopPadding) { + context.report({ + node, + loc: { + start: tokenBeforeFirst.loc.start, + end: firstBlockToken.loc.start + }, + fix(fixer) { + return fixer.insertTextAfter(tokenBeforeFirst, "\n"); + }, + messageId: "alwaysPadBlock" + }); + } + if (!blockHasBottomPadding) { + context.report({ + node, + loc: { + end: tokenAfterLast.loc.start, + start: lastBlockToken.loc.end + }, + fix(fixer) { + return fixer.insertTextBefore(tokenAfterLast, "\n"); + }, + messageId: "alwaysPadBlock" + }); + } + } else { + if (blockHasTopPadding) { + + context.report({ + node, + loc: { + start: tokenBeforeFirst.loc.start, + end: firstBlockToken.loc.start + }, + fix(fixer) { + return fixer.replaceTextRange([tokenBeforeFirst.range[1], firstBlockToken.range[0] - firstBlockToken.loc.start.column], "\n"); + }, + messageId: "neverPadBlock" + }); + } + + if (blockHasBottomPadding) { + + context.report({ + node, + loc: { + end: tokenAfterLast.loc.start, + start: lastBlockToken.loc.end + }, + messageId: "neverPadBlock", + fix(fixer) { + return fixer.replaceTextRange([lastBlockToken.range[1], tokenAfterLast.range[0] - tokenAfterLast.loc.start.column], "\n"); + } + }); + } + } + } + + const rule = {}; + + if (Object.prototype.hasOwnProperty.call(options, "switches")) { + rule.SwitchStatement = function(node) { + if (node.cases.length === 0) { + return; + } + checkPadding(node); + }; + } + + if (Object.prototype.hasOwnProperty.call(options, "blocks")) { + rule.BlockStatement = function(node) { + if (node.body.length === 0) { + return; + } + checkPadding(node); + }; + rule.StaticBlock = rule.BlockStatement; + } + + if (Object.prototype.hasOwnProperty.call(options, "classes")) { + rule.ClassBody = function(node) { + if (node.body.length === 0) { + return; + } + checkPadding(node); + }; + } + + return rule; + } }; diff --git a/node_modules/eslint/lib/rules/padding-line-between-statements.js b/node_modules/eslint/lib/rules/padding-line-between-statements.js index 153bf820a..084651b6d 100644 --- a/node_modules/eslint/lib/rules/padding-line-between-statements.js +++ b/node_modules/eslint/lib/rules/padding-line-between-statements.js @@ -18,8 +18,8 @@ const astUtils = require("./utils/ast-utils"); const LT = `[${Array.from(astUtils.LINEBREAKS).join("")}]`; const PADDING_LINE_SEQUENCE = new RegExp( - String.raw`^(\s*?${LT})\s*${LT}(\s*;?)$`, - "u", + String.raw`^(\s*?${LT})\s*${LT}(\s*;?)$`, + "u" ); const CJS_EXPORT = /^(?:module\s*\.\s*)?exports(?:\s*\.|\s*\[|$)/u; const CJS_IMPORT = /^require\(/u; @@ -31,10 +31,10 @@ const CJS_IMPORT = /^require\(/u; * @private */ function newKeywordTester(keyword) { - return { - test: (node, sourceCode) => - sourceCode.getFirstToken(node).value === keyword, - }; + return { + test: (node, sourceCode) => + sourceCode.getFirstToken(node).value === keyword + }; } /** @@ -44,11 +44,11 @@ function newKeywordTester(keyword) { * @private */ function newSinglelineKeywordTester(keyword) { - return { - test: (node, sourceCode) => - node.loc.start.line === node.loc.end.line && - sourceCode.getFirstToken(node).value === keyword, - }; + return { + test: (node, sourceCode) => + node.loc.start.line === node.loc.end.line && + sourceCode.getFirstToken(node).value === keyword + }; } /** @@ -58,11 +58,11 @@ function newSinglelineKeywordTester(keyword) { * @private */ function newMultilineKeywordTester(keyword) { - return { - test: (node, sourceCode) => - node.loc.start.line !== node.loc.end.line && - sourceCode.getFirstToken(node).value === keyword, - }; + return { + test: (node, sourceCode) => + node.loc.start.line !== node.loc.end.line && + sourceCode.getFirstToken(node).value === keyword + }; } /** @@ -72,9 +72,10 @@ function newMultilineKeywordTester(keyword) { * @private */ function newNodeTypeTester(type) { - return { - test: node => node.type === type, - }; + return { + test: node => + node.type === type + }; } /** @@ -84,17 +85,15 @@ function newNodeTypeTester(type) { * @private */ function isIIFEStatement(node) { - if (node.type === "ExpressionStatement") { - let call = astUtils.skipChainExpression(node.expression); - - if (call.type === "UnaryExpression") { - call = astUtils.skipChainExpression(call.argument); - } - return ( - call.type === "CallExpression" && astUtils.isFunction(call.callee) - ); - } - return false; + if (node.type === "ExpressionStatement") { + let call = astUtils.skipChainExpression(node.expression); + + if (call.type === "UnaryExpression") { + call = astUtils.skipChainExpression(call.argument); + } + return call.type === "CallExpression" && astUtils.isFunction(call.callee); + } + return false; } /** @@ -106,37 +105,30 @@ function isIIFEStatement(node) { * @private */ function isBlockLikeStatement(sourceCode, node) { - // do-while with a block is a block-like statement. - if ( - node.type === "DoWhileStatement" && - node.body.type === "BlockStatement" - ) { - return true; - } - - /* - * IIFE is a block-like statement specially from - * JSCS#disallowPaddingNewLinesAfterBlocks. - */ - if (isIIFEStatement(node)) { - return true; - } - - // Checks the last token is a closing brace of blocks. - const lastToken = sourceCode.getLastToken( - node, - astUtils.isNotSemicolonToken, - ); - const belongingNode = - lastToken && astUtils.isClosingBraceToken(lastToken) - ? sourceCode.getNodeByRangeIndex(lastToken.range[0]) - : null; - - return ( - Boolean(belongingNode) && - (belongingNode.type === "BlockStatement" || - belongingNode.type === "SwitchStatement") - ); + + // do-while with a block is a block-like statement. + if (node.type === "DoWhileStatement" && node.body.type === "BlockStatement") { + return true; + } + + /* + * IIFE is a block-like statement specially from + * JSCS#disallowPaddingNewLinesAfterBlocks. + */ + if (isIIFEStatement(node)) { + return true; + } + + // Checks the last token is a closing brace of blocks. + const lastToken = sourceCode.getLastToken(node, astUtils.isNotSemicolonToken); + const belongingNode = lastToken && astUtils.isClosingBraceToken(lastToken) + ? sourceCode.getNodeByRangeIndex(lastToken.range[0]) + : null; + + return Boolean(belongingNode) && ( + belongingNode.type === "BlockStatement" || + belongingNode.type === "SwitchStatement" + ); } /** @@ -153,19 +145,19 @@ function isBlockLikeStatement(sourceCode, node) { * @private */ function getActualLastToken(sourceCode, node) { - const semiToken = sourceCode.getLastToken(node); - const prevToken = sourceCode.getTokenBefore(semiToken); - const nextToken = sourceCode.getTokenAfter(semiToken); - const isSemicolonLessStyle = Boolean( - prevToken && - nextToken && - prevToken.range[0] >= node.range[0] && - astUtils.isSemicolonToken(semiToken) && - semiToken.loc.start.line !== prevToken.loc.end.line && - semiToken.loc.end.line === nextToken.loc.start.line, - ); - - return isSemicolonLessStyle ? prevToken : semiToken; + const semiToken = sourceCode.getLastToken(node); + const prevToken = sourceCode.getTokenBefore(semiToken); + const nextToken = sourceCode.getTokenAfter(semiToken); + const isSemicolonLessStyle = Boolean( + prevToken && + nextToken && + prevToken.range[0] >= node.range[0] && + astUtils.isSemicolonToken(semiToken) && + semiToken.loc.start.line !== prevToken.loc.end.line && + semiToken.loc.end.line === nextToken.loc.start.line + ); + + return isSemicolonLessStyle ? prevToken : semiToken; } /** @@ -177,7 +169,7 @@ function getActualLastToken(sourceCode, node) { * @private */ function replacerToRemovePaddingLines(_, trailingSpaces, indentSpaces) { - return trailingSpaces + indentSpaces; + return trailingSpaces + indentSpaces; } /** @@ -186,7 +178,8 @@ function replacerToRemovePaddingLines(_, trailingSpaces, indentSpaces) { * @returns {void} * @private */ -function verifyForAny() {} +function verifyForAny() { +} /** * Check and report statements for `never` configuration. @@ -202,29 +195,29 @@ function verifyForAny() {} * @private */ function verifyForNever(context, _, nextNode, paddingLines) { - if (paddingLines.length === 0) { - return; - } - - context.report({ - node: nextNode, - messageId: "unexpectedBlankLine", - fix(fixer) { - if (paddingLines.length >= 2) { - return null; - } - - const prevToken = paddingLines[0][0]; - const nextToken = paddingLines[0][1]; - const start = prevToken.range[1]; - const end = nextToken.range[0]; - const text = context.sourceCode.text - .slice(start, end) - .replace(PADDING_LINE_SEQUENCE, replacerToRemovePaddingLines); - - return fixer.replaceTextRange([start, end], text); - }, - }); + if (paddingLines.length === 0) { + return; + } + + context.report({ + node: nextNode, + messageId: "unexpectedBlankLine", + fix(fixer) { + if (paddingLines.length >= 2) { + return null; + } + + const prevToken = paddingLines[0][0]; + const nextToken = paddingLines[0][1]; + const start = prevToken.range[1]; + const end = nextToken.range[0]; + const text = context.sourceCode.text + .slice(start, end) + .replace(PADDING_LINE_SEQUENCE, replacerToRemovePaddingLines); + + return fixer.replaceTextRange([start, end], text); + } + }); } /** @@ -241,55 +234,58 @@ function verifyForNever(context, _, nextNode, paddingLines) { * @private */ function verifyForAlways(context, prevNode, nextNode, paddingLines) { - if (paddingLines.length > 0) { - return; - } - - context.report({ - node: nextNode, - messageId: "expectedBlankLine", - fix(fixer) { - const sourceCode = context.sourceCode; - let prevToken = getActualLastToken(sourceCode, prevNode); - const nextToken = - sourceCode.getFirstTokenBetween(prevToken, nextNode, { - includeComments: true, - - /** - * Skip the trailing comments of the previous node. - * This inserts a blank line after the last trailing comment. - * - * For example: - * - * foo(); // trailing comment. - * // comment. - * bar(); - * - * Get fixed to: - * - * foo(); // trailing comment. - * - * // comment. - * bar(); - * @param {Token} token The token to check. - * @returns {boolean} `true` if the token is not a trailing comment. - * @private - */ - filter(token) { - if (astUtils.isTokenOnSameLine(prevToken, token)) { - prevToken = token; - return false; - } - return true; - }, - }) || nextNode; - const insertText = astUtils.isTokenOnSameLine(prevToken, nextToken) - ? "\n\n" - : "\n"; - - return fixer.insertTextAfter(prevToken, insertText); - }, - }); + if (paddingLines.length > 0) { + return; + } + + context.report({ + node: nextNode, + messageId: "expectedBlankLine", + fix(fixer) { + const sourceCode = context.sourceCode; + let prevToken = getActualLastToken(sourceCode, prevNode); + const nextToken = sourceCode.getFirstTokenBetween( + prevToken, + nextNode, + { + includeComments: true, + + /** + * Skip the trailing comments of the previous node. + * This inserts a blank line after the last trailing comment. + * + * For example: + * + * foo(); // trailing comment. + * // comment. + * bar(); + * + * Get fixed to: + * + * foo(); // trailing comment. + * + * // comment. + * bar(); + * @param {Token} token The token to check. + * @returns {boolean} `true` if the token is not a trailing comment. + * @private + */ + filter(token) { + if (astUtils.isTokenOnSameLine(prevToken, token)) { + prevToken = token; + return false; + } + return true; + } + } + ) || nextNode; + const insertText = astUtils.isTokenOnSameLine(prevToken, nextToken) + ? "\n\n" + : "\n"; + + return fixer.insertTextAfter(prevToken, insertText); + } + }); } /** @@ -299,9 +295,9 @@ function verifyForAlways(context, prevNode, nextNode, paddingLines) { * @private */ const PaddingTypes = { - any: { verify: verifyForAny }, - never: { verify: verifyForNever }, - always: { verify: verifyForAlways }, + any: { verify: verifyForAny }, + never: { verify: verifyForNever }, + always: { verify: verifyForAlways } }; /** @@ -310,303 +306,285 @@ const PaddingTypes = { * @private */ const StatementTypes = { - "*": { test: () => true }, - "block-like": { - test: (node, sourceCode) => isBlockLikeStatement(sourceCode, node), - }, - "cjs-export": { - test: (node, sourceCode) => - node.type === "ExpressionStatement" && - node.expression.type === "AssignmentExpression" && - CJS_EXPORT.test(sourceCode.getText(node.expression.left)), - }, - "cjs-import": { - test: (node, sourceCode) => - node.type === "VariableDeclaration" && - node.declarations.length > 0 && - Boolean(node.declarations[0].init) && - CJS_IMPORT.test(sourceCode.getText(node.declarations[0].init)), - }, - directive: { - test: astUtils.isDirective, - }, - expression: { - test: node => - node.type === "ExpressionStatement" && !astUtils.isDirective(node), - }, - iife: { - test: isIIFEStatement, - }, - "multiline-block-like": { - test: (node, sourceCode) => - node.loc.start.line !== node.loc.end.line && - isBlockLikeStatement(sourceCode, node), - }, - "multiline-expression": { - test: node => - node.loc.start.line !== node.loc.end.line && - node.type === "ExpressionStatement" && - !astUtils.isDirective(node), - }, - - "multiline-const": newMultilineKeywordTester("const"), - "multiline-let": newMultilineKeywordTester("let"), - "multiline-var": newMultilineKeywordTester("var"), - "singleline-const": newSinglelineKeywordTester("const"), - "singleline-let": newSinglelineKeywordTester("let"), - "singleline-var": newSinglelineKeywordTester("var"), - - block: newNodeTypeTester("BlockStatement"), - empty: newNodeTypeTester("EmptyStatement"), - function: newNodeTypeTester("FunctionDeclaration"), - - break: newKeywordTester("break"), - case: newKeywordTester("case"), - class: newKeywordTester("class"), - const: newKeywordTester("const"), - continue: newKeywordTester("continue"), - debugger: newKeywordTester("debugger"), - default: newKeywordTester("default"), - do: newKeywordTester("do"), - export: newKeywordTester("export"), - for: newKeywordTester("for"), - if: newKeywordTester("if"), - import: newKeywordTester("import"), - let: newKeywordTester("let"), - return: newKeywordTester("return"), - switch: newKeywordTester("switch"), - throw: newKeywordTester("throw"), - try: newKeywordTester("try"), - var: newKeywordTester("var"), - while: newKeywordTester("while"), - with: newKeywordTester("with"), + "*": { test: () => true }, + "block-like": { + test: (node, sourceCode) => isBlockLikeStatement(sourceCode, node) + }, + "cjs-export": { + test: (node, sourceCode) => + node.type === "ExpressionStatement" && + node.expression.type === "AssignmentExpression" && + CJS_EXPORT.test(sourceCode.getText(node.expression.left)) + }, + "cjs-import": { + test: (node, sourceCode) => + node.type === "VariableDeclaration" && + node.declarations.length > 0 && + Boolean(node.declarations[0].init) && + CJS_IMPORT.test(sourceCode.getText(node.declarations[0].init)) + }, + directive: { + test: astUtils.isDirective + }, + expression: { + test: node => node.type === "ExpressionStatement" && !astUtils.isDirective(node) + }, + iife: { + test: isIIFEStatement + }, + "multiline-block-like": { + test: (node, sourceCode) => + node.loc.start.line !== node.loc.end.line && + isBlockLikeStatement(sourceCode, node) + }, + "multiline-expression": { + test: node => + node.loc.start.line !== node.loc.end.line && + node.type === "ExpressionStatement" && + !astUtils.isDirective(node) + }, + + "multiline-const": newMultilineKeywordTester("const"), + "multiline-let": newMultilineKeywordTester("let"), + "multiline-var": newMultilineKeywordTester("var"), + "singleline-const": newSinglelineKeywordTester("const"), + "singleline-let": newSinglelineKeywordTester("let"), + "singleline-var": newSinglelineKeywordTester("var"), + + block: newNodeTypeTester("BlockStatement"), + empty: newNodeTypeTester("EmptyStatement"), + function: newNodeTypeTester("FunctionDeclaration"), + + break: newKeywordTester("break"), + case: newKeywordTester("case"), + class: newKeywordTester("class"), + const: newKeywordTester("const"), + continue: newKeywordTester("continue"), + debugger: newKeywordTester("debugger"), + default: newKeywordTester("default"), + do: newKeywordTester("do"), + export: newKeywordTester("export"), + for: newKeywordTester("for"), + if: newKeywordTester("if"), + import: newKeywordTester("import"), + let: newKeywordTester("let"), + return: newKeywordTester("return"), + switch: newKeywordTester("switch"), + throw: newKeywordTester("throw"), + try: newKeywordTester("try"), + var: newKeywordTester("var"), + while: newKeywordTester("while"), + with: newKeywordTester("with") }; //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "padding-line-between-statements", - url: "https://eslint.style/rules/padding-line-between-statements", - }, - }, - ], - }, - type: "layout", - - docs: { - description: "Require or disallow padding lines between statements", - recommended: false, - url: "https://eslint.org/docs/latest/rules/padding-line-between-statements", - }, - - fixable: "whitespace", - - schema: { - definitions: { - paddingType: { - enum: Object.keys(PaddingTypes), - }, - statementType: { - anyOf: [ - { enum: Object.keys(StatementTypes) }, - { - type: "array", - items: { enum: Object.keys(StatementTypes) }, - minItems: 1, - uniqueItems: true, - }, - ], - }, - }, - type: "array", - items: { - type: "object", - properties: { - blankLine: { $ref: "#/definitions/paddingType" }, - prev: { $ref: "#/definitions/statementType" }, - next: { $ref: "#/definitions/statementType" }, - }, - additionalProperties: false, - required: ["blankLine", "prev", "next"], - }, - }, - - messages: { - unexpectedBlankLine: "Unexpected blank line before this statement.", - expectedBlankLine: "Expected blank line before this statement.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - const configureList = context.options || []; - let scopeInfo = null; - - /** - * Processes to enter to new scope. - * This manages the current previous statement. - * @returns {void} - * @private - */ - function enterScope() { - scopeInfo = { - upper: scopeInfo, - prevNode: null, - }; - } - - /** - * Processes to exit from the current scope. - * @returns {void} - * @private - */ - function exitScope() { - scopeInfo = scopeInfo.upper; - } - - /** - * Checks whether the given node matches the given type. - * @param {ASTNode} node The statement node to check. - * @param {string|string[]} type The statement type to check. - * @returns {boolean} `true` if the statement node matched the type. - * @private - */ - function match(node, type) { - let innerStatementNode = node; - - while (innerStatementNode.type === "LabeledStatement") { - innerStatementNode = innerStatementNode.body; - } - if (Array.isArray(type)) { - return type.some(match.bind(null, innerStatementNode)); - } - return StatementTypes[type].test(innerStatementNode, sourceCode); - } - - /** - * Finds the last matched configure from configureList. - * @param {ASTNode} prevNode The previous statement to match. - * @param {ASTNode} nextNode The current statement to match. - * @returns {Object} The tester of the last matched configure. - * @private - */ - function getPaddingType(prevNode, nextNode) { - for (let i = configureList.length - 1; i >= 0; --i) { - const configure = configureList[i]; - const matched = - match(prevNode, configure.prev) && - match(nextNode, configure.next); - - if (matched) { - return PaddingTypes[configure.blankLine]; - } - } - return PaddingTypes.any; - } - - /** - * Gets padding line sequences between the given 2 statements. - * Comments are separators of the padding line sequences. - * @param {ASTNode} prevNode The previous statement to count. - * @param {ASTNode} nextNode The current statement to count. - * @returns {Array} The array of token pairs. - * @private - */ - function getPaddingLineSequences(prevNode, nextNode) { - const pairs = []; - let prevToken = getActualLastToken(sourceCode, prevNode); - - if (nextNode.loc.start.line - prevToken.loc.end.line >= 2) { - do { - const token = sourceCode.getTokenAfter(prevToken, { - includeComments: true, - }); - - if (token.loc.start.line - prevToken.loc.end.line >= 2) { - pairs.push([prevToken, token]); - } - prevToken = token; - } while (prevToken.range[0] < nextNode.range[0]); - } - - return pairs; - } - - /** - * Verify padding lines between the given node and the previous node. - * @param {ASTNode} node The node to verify. - * @returns {void} - * @private - */ - function verify(node) { - const parentType = node.parent.type; - const validParent = - astUtils.STATEMENT_LIST_PARENTS.has(parentType) || - parentType === "SwitchStatement"; - - if (!validParent) { - return; - } - - // Save this node as the current previous statement. - const prevNode = scopeInfo.prevNode; - - // Verify. - if (prevNode) { - const type = getPaddingType(prevNode, node); - const paddingLines = getPaddingLineSequences(prevNode, node); - - type.verify(context, prevNode, node, paddingLines); - } - - scopeInfo.prevNode = node; - } - - /** - * Verify padding lines between the given node and the previous node. - * Then process to enter to new scope. - * @param {ASTNode} node The node to verify. - * @returns {void} - * @private - */ - function verifyThenEnterScope(node) { - verify(node); - enterScope(); - } - - return { - Program: enterScope, - BlockStatement: enterScope, - SwitchStatement: enterScope, - StaticBlock: enterScope, - "Program:exit": exitScope, - "BlockStatement:exit": exitScope, - "SwitchStatement:exit": exitScope, - "StaticBlock:exit": exitScope, - - ":statement": verify, - - SwitchCase: verifyThenEnterScope, - "SwitchCase:exit": exitScope, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Require or disallow padding lines between statements", + recommended: false, + url: "https://eslint.org/docs/latest/rules/padding-line-between-statements" + }, + + fixable: "whitespace", + + schema: { + definitions: { + paddingType: { + enum: Object.keys(PaddingTypes) + }, + statementType: { + anyOf: [ + { enum: Object.keys(StatementTypes) }, + { + type: "array", + items: { enum: Object.keys(StatementTypes) }, + minItems: 1, + uniqueItems: true + } + ] + } + }, + type: "array", + items: { + type: "object", + properties: { + blankLine: { $ref: "#/definitions/paddingType" }, + prev: { $ref: "#/definitions/statementType" }, + next: { $ref: "#/definitions/statementType" } + }, + additionalProperties: false, + required: ["blankLine", "prev", "next"] + } + }, + + messages: { + unexpectedBlankLine: "Unexpected blank line before this statement.", + expectedBlankLine: "Expected blank line before this statement." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + const configureList = context.options || []; + let scopeInfo = null; + + /** + * Processes to enter to new scope. + * This manages the current previous statement. + * @returns {void} + * @private + */ + function enterScope() { + scopeInfo = { + upper: scopeInfo, + prevNode: null + }; + } + + /** + * Processes to exit from the current scope. + * @returns {void} + * @private + */ + function exitScope() { + scopeInfo = scopeInfo.upper; + } + + /** + * Checks whether the given node matches the given type. + * @param {ASTNode} node The statement node to check. + * @param {string|string[]} type The statement type to check. + * @returns {boolean} `true` if the statement node matched the type. + * @private + */ + function match(node, type) { + let innerStatementNode = node; + + while (innerStatementNode.type === "LabeledStatement") { + innerStatementNode = innerStatementNode.body; + } + if (Array.isArray(type)) { + return type.some(match.bind(null, innerStatementNode)); + } + return StatementTypes[type].test(innerStatementNode, sourceCode); + } + + /** + * Finds the last matched configure from configureList. + * @param {ASTNode} prevNode The previous statement to match. + * @param {ASTNode} nextNode The current statement to match. + * @returns {Object} The tester of the last matched configure. + * @private + */ + function getPaddingType(prevNode, nextNode) { + for (let i = configureList.length - 1; i >= 0; --i) { + const configure = configureList[i]; + const matched = + match(prevNode, configure.prev) && + match(nextNode, configure.next); + + if (matched) { + return PaddingTypes[configure.blankLine]; + } + } + return PaddingTypes.any; + } + + /** + * Gets padding line sequences between the given 2 statements. + * Comments are separators of the padding line sequences. + * @param {ASTNode} prevNode The previous statement to count. + * @param {ASTNode} nextNode The current statement to count. + * @returns {Array} The array of token pairs. + * @private + */ + function getPaddingLineSequences(prevNode, nextNode) { + const pairs = []; + let prevToken = getActualLastToken(sourceCode, prevNode); + + if (nextNode.loc.start.line - prevToken.loc.end.line >= 2) { + do { + const token = sourceCode.getTokenAfter( + prevToken, + { includeComments: true } + ); + + if (token.loc.start.line - prevToken.loc.end.line >= 2) { + pairs.push([prevToken, token]); + } + prevToken = token; + + } while (prevToken.range[0] < nextNode.range[0]); + } + + return pairs; + } + + /** + * Verify padding lines between the given node and the previous node. + * @param {ASTNode} node The node to verify. + * @returns {void} + * @private + */ + function verify(node) { + const parentType = node.parent.type; + const validParent = + astUtils.STATEMENT_LIST_PARENTS.has(parentType) || + parentType === "SwitchStatement"; + + if (!validParent) { + return; + } + + // Save this node as the current previous statement. + const prevNode = scopeInfo.prevNode; + + // Verify. + if (prevNode) { + const type = getPaddingType(prevNode, node); + const paddingLines = getPaddingLineSequences(prevNode, node); + + type.verify(context, prevNode, node, paddingLines); + } + + scopeInfo.prevNode = node; + } + + /** + * Verify padding lines between the given node and the previous node. + * Then process to enter to new scope. + * @param {ASTNode} node The node to verify. + * @returns {void} + * @private + */ + function verifyThenEnterScope(node) { + verify(node); + enterScope(); + } + + return { + Program: enterScope, + BlockStatement: enterScope, + SwitchStatement: enterScope, + StaticBlock: enterScope, + "Program:exit": exitScope, + "BlockStatement:exit": exitScope, + "SwitchStatement:exit": exitScope, + "StaticBlock:exit": exitScope, + + ":statement": verify, + + SwitchCase: verifyThenEnterScope, + "SwitchCase:exit": exitScope + }; + } }; diff --git a/node_modules/eslint/lib/rules/prefer-arrow-callback.js b/node_modules/eslint/lib/rules/prefer-arrow-callback.js index 0f05b52ad..d22e508be 100644 --- a/node_modules/eslint/lib/rules/prefer-arrow-callback.js +++ b/node_modules/eslint/lib/rules/prefer-arrow-callback.js @@ -17,7 +17,7 @@ const astUtils = require("./utils/ast-utils"); * @returns {boolean} `true` if the variable is a function name. */ function isFunctionName(variable) { - return variable && variable.defs[0].type === "FunctionName"; + return variable && variable.defs[0].type === "FunctionName"; } /** @@ -28,7 +28,7 @@ function isFunctionName(variable) { * @returns {boolean} `true` if the node is the specific value. */ function checkMetaProperty(node, metaName, propertyName) { - return node.meta.name === metaName && node.property.name === propertyName; + return node.meta.name === metaName && node.property.name === propertyName; } /** @@ -37,23 +37,24 @@ function checkMetaProperty(node, metaName, propertyName) { * @returns {eslint-scope.Variable} The found variable object. */ function getVariableOfArguments(scope) { - const variables = scope.variables; - - for (let i = 0; i < variables.length; ++i) { - const variable = variables[i]; - - if (variable.name === "arguments") { - /* - * If there was a parameter which is named "arguments", the - * implicit "arguments" is not defined. - * So does fast return with null. - */ - return variable.identifiers.length === 0 ? variable : null; - } - } - - /* c8 ignore next */ - return null; + const variables = scope.variables; + + for (let i = 0; i < variables.length; ++i) { + const variable = variables[i]; + + if (variable.name === "arguments") { + + /* + * If there was a parameter which is named "arguments", the + * implicit "arguments" is not defined. + * So does fast return with null. + */ + return (variable.identifiers.length === 0) ? variable : null; + } + } + + /* c8 ignore next */ + return null; } /** @@ -65,68 +66,68 @@ function getVariableOfArguments(scope) { * {boolean} retv.isLexicalThis - `true` if the node is with `.bind(this)`. */ function getCallbackInfo(node) { - const retv = { isCallback: false, isLexicalThis: false }; - let currentNode = node; - let parent = node.parent; - let bound = false; - - while (currentNode) { - switch (parent.type) { - // Checks parents recursively. - - case "LogicalExpression": - case "ChainExpression": - case "ConditionalExpression": - break; - - // Checks whether the parent node is `.bind(this)` call. - case "MemberExpression": - if ( - parent.object === currentNode && - !parent.property.computed && - parent.property.type === "Identifier" && - parent.property.name === "bind" - ) { - const maybeCallee = - parent.parent.type === "ChainExpression" - ? parent.parent - : parent; - - if (astUtils.isCallee(maybeCallee)) { - if (!bound) { - bound = true; // Use only the first `.bind()` to make `isLexicalThis` value. - retv.isLexicalThis = - maybeCallee.parent.arguments.length === 1 && - maybeCallee.parent.arguments[0].type === - "ThisExpression"; - } - parent = maybeCallee.parent; - } else { - return retv; - } - } else { - return retv; - } - break; - - // Checks whether the node is a callback. - case "CallExpression": - case "NewExpression": - if (parent.callee !== currentNode) { - retv.isCallback = true; - } - return retv; - - default: - return retv; - } - - currentNode = parent; - parent = parent.parent; - } - - /* c8 ignore next */ - throw new Error("unreachable"); + const retv = { isCallback: false, isLexicalThis: false }; + let currentNode = node; + let parent = node.parent; + let bound = false; + + while (currentNode) { + switch (parent.type) { + + // Checks parents recursively. + + case "LogicalExpression": + case "ChainExpression": + case "ConditionalExpression": + break; + + // Checks whether the parent node is `.bind(this)` call. + case "MemberExpression": + if ( + parent.object === currentNode && + !parent.property.computed && + parent.property.type === "Identifier" && + parent.property.name === "bind" + ) { + const maybeCallee = parent.parent.type === "ChainExpression" + ? parent.parent + : parent; + + if (astUtils.isCallee(maybeCallee)) { + if (!bound) { + bound = true; // Use only the first `.bind()` to make `isLexicalThis` value. + retv.isLexicalThis = ( + maybeCallee.parent.arguments.length === 1 && + maybeCallee.parent.arguments[0].type === "ThisExpression" + ); + } + parent = maybeCallee.parent; + } else { + return retv; + } + } else { + return retv; + } + break; + + // Checks whether the node is a callback. + case "CallExpression": + case "NewExpression": + if (parent.callee !== currentNode) { + retv.isCallback = true; + } + return retv; + + default: + return retv; + } + + currentNode = parent; + parent = parent.parent; + } + + /* c8 ignore next */ + throw new Error("unreachable"); } /** @@ -137,301 +138,244 @@ function getCallbackInfo(node) { * @returns {boolean} `true` if the list of parameters contains any duplicates */ function hasDuplicateParams(paramsList) { - return ( - paramsList.every(param => param.type === "Identifier") && - paramsList.length !== new Set(paramsList.map(param => param.name)).size - ); + return paramsList.every(param => param.type === "Identifier") && paramsList.length !== new Set(paramsList.map(param => param.name)).size; } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - dialects: ["javascript", "typescript"], - language: "javascript", - - defaultOptions: [ - { allowNamedFunctions: false, allowUnboundThis: true }, - ], - - docs: { - description: "Require using arrow functions for callbacks", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/prefer-arrow-callback", - }, - - schema: [ - { - type: "object", - properties: { - allowNamedFunctions: { - type: "boolean", - }, - allowUnboundThis: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - - fixable: "code", - - messages: { - preferArrowCallback: "Unexpected function expression.", - }, - }, - - create(context) { - const [{ allowNamedFunctions, allowUnboundThis }] = context.options; - const sourceCode = context.sourceCode; - - /* - * {Array<{this: boolean, super: boolean, meta: boolean}>} - * - this - A flag which shows there are one or more ThisExpression. - * - super - A flag which shows there are one or more Super. - * - meta - A flag which shows there are one or more MethProperty. - */ - let stack = []; - - /** - * Pushes new function scope with all `false` flags. - * @returns {void} - */ - function enterScope() { - stack.push({ this: false, super: false, meta: false }); - } - - /** - * Pops a function scope from the stack. - * @returns {{this: boolean, super: boolean, meta: boolean}} The information of the last scope. - */ - function exitScope() { - return stack.pop(); - } - - return { - // Reset internal state. - Program() { - stack = []; - }, - - // If there are below, it cannot replace with arrow functions merely. - ThisExpression() { - const info = stack.at(-1); - - if (info) { - info.this = true; - } - }, - - Super() { - const info = stack.at(-1); - - if (info) { - info.super = true; - } - }, - - MetaProperty(node) { - const info = stack.at(-1); - - if (info && checkMetaProperty(node, "new", "target")) { - info.meta = true; - } - }, - - // To skip nested scopes. - FunctionDeclaration: enterScope, - "FunctionDeclaration:exit": exitScope, - - // Main. - FunctionExpression: enterScope, - "FunctionExpression:exit"(node) { - const scopeInfo = exitScope(); - - // Skip named function expressions - if (allowNamedFunctions && node.id && node.id.name) { - return; - } - - // Skip generators. - if (node.generator) { - return; - } - - // Skip recursive functions. - const nameVar = sourceCode.getDeclaredVariables(node)[0]; - - if (isFunctionName(nameVar) && nameVar.references.length > 0) { - return; - } - - // Skip if it's using arguments. - const variable = getVariableOfArguments( - sourceCode.getScope(node), - ); - - if (variable && variable.references.length > 0) { - return; - } - - // Reports if it's a callback which can replace with arrows. - const callbackInfo = getCallbackInfo(node); - - if ( - callbackInfo.isCallback && - (!allowUnboundThis || - !scopeInfo.this || - callbackInfo.isLexicalThis) && - !scopeInfo.super && - !scopeInfo.meta - ) { - context.report({ - node, - messageId: "preferArrowCallback", - *fix(fixer) { - if ( - (!callbackInfo.isLexicalThis && - scopeInfo.this) || - hasDuplicateParams(node.params) - ) { - /* - * If the callback function does not have .bind(this) and contains a reference to `this`, there - * is no way to determine what `this` should be, so don't perform any fixes. - * If the callback function has duplicates in its list of parameters (possible in sloppy mode), - * don't replace it with an arrow function, because this is a SyntaxError with arrow functions. - */ - return; - } - - if ( - node.params.length && - node.params[0].name === "this" - ) { - return; - } - - // Remove `.bind(this)` if exists. - if (callbackInfo.isLexicalThis) { - const memberNode = node.parent; - - /* - * If `.bind(this)` exists but the parent is not `.bind(this)`, don't remove it automatically. - * E.g. `(foo || function(){}).bind(this)` - */ - if (memberNode.type !== "MemberExpression") { - return; - } - - const callNode = memberNode.parent; - const firstTokenToRemove = - sourceCode.getTokenAfter( - memberNode.object, - astUtils.isNotClosingParenToken, - ); - const lastTokenToRemove = - sourceCode.getLastToken(callNode); - - /* - * If the member expression is parenthesized, don't remove the right paren. - * E.g. `(function(){}.bind)(this)` - * ^^^^^^^^^^^^ - */ - if ( - astUtils.isParenthesised( - sourceCode, - memberNode, - ) - ) { - return; - } - - // If comments exist in the `.bind(this)`, don't remove those. - if ( - sourceCode.commentsExistBetween( - firstTokenToRemove, - lastTokenToRemove, - ) - ) { - return; - } - - yield fixer.removeRange([ - firstTokenToRemove.range[0], - lastTokenToRemove.range[1], - ]); - } - - // Convert the function expression to an arrow function. - const functionToken = sourceCode.getFirstToken( - node, - node.async ? 1 : 0, - ); - const leftParenToken = sourceCode.getTokenAfter( - functionToken, - astUtils.isOpeningParenToken, - ); - const tokenBeforeBody = sourceCode.getTokenBefore( - node.body, - ); - - if ( - sourceCode.commentsExistBetween( - functionToken, - leftParenToken, - ) - ) { - // Remove only extra tokens to keep comments. - yield fixer.remove(functionToken); - if (node.id) { - yield fixer.remove(node.id); - } - } else { - // Remove extra tokens and spaces. - yield fixer.removeRange([ - functionToken.range[0], - leftParenToken.range[0], - ]); - } - yield fixer.insertTextAfter(tokenBeforeBody, " =>"); - - // Get the node that will become the new arrow function. - let replacedNode = callbackInfo.isLexicalThis - ? node.parent.parent - : node; - - if (replacedNode.type === "ChainExpression") { - replacedNode = replacedNode.parent; - } - - /* - * If the replaced node is part of a BinaryExpression, LogicalExpression, or MemberExpression, then - * the arrow function needs to be parenthesized, because `foo || () => {}` is invalid syntax even - * though `foo || function() {}` is valid. - */ - if ( - replacedNode.parent.type !== "CallExpression" && - replacedNode.parent.type !== - "ConditionalExpression" && - !astUtils.isParenthesised( - sourceCode, - replacedNode, - ) && - !astUtils.isParenthesised(sourceCode, node) - ) { - yield fixer.insertTextBefore(replacedNode, "("); - yield fixer.insertTextAfter(replacedNode, ")"); - } - }, - }); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Require using arrow functions for callbacks", + recommended: false, + url: "https://eslint.org/docs/latest/rules/prefer-arrow-callback" + }, + + schema: [ + { + type: "object", + properties: { + allowNamedFunctions: { + type: "boolean", + default: false + }, + allowUnboundThis: { + type: "boolean", + default: true + } + }, + additionalProperties: false + } + ], + + fixable: "code", + + messages: { + preferArrowCallback: "Unexpected function expression." + } + }, + + create(context) { + const options = context.options[0] || {}; + + const allowUnboundThis = options.allowUnboundThis !== false; // default to true + const allowNamedFunctions = options.allowNamedFunctions; + const sourceCode = context.sourceCode; + + /* + * {Array<{this: boolean, super: boolean, meta: boolean}>} + * - this - A flag which shows there are one or more ThisExpression. + * - super - A flag which shows there are one or more Super. + * - meta - A flag which shows there are one or more MethProperty. + */ + let stack = []; + + /** + * Pushes new function scope with all `false` flags. + * @returns {void} + */ + function enterScope() { + stack.push({ this: false, super: false, meta: false }); + } + + /** + * Pops a function scope from the stack. + * @returns {{this: boolean, super: boolean, meta: boolean}} The information of the last scope. + */ + function exitScope() { + return stack.pop(); + } + + return { + + // Reset internal state. + Program() { + stack = []; + }, + + // If there are below, it cannot replace with arrow functions merely. + ThisExpression() { + const info = stack[stack.length - 1]; + + if (info) { + info.this = true; + } + }, + + Super() { + const info = stack[stack.length - 1]; + + if (info) { + info.super = true; + } + }, + + MetaProperty(node) { + const info = stack[stack.length - 1]; + + if (info && checkMetaProperty(node, "new", "target")) { + info.meta = true; + } + }, + + // To skip nested scopes. + FunctionDeclaration: enterScope, + "FunctionDeclaration:exit": exitScope, + + // Main. + FunctionExpression: enterScope, + "FunctionExpression:exit"(node) { + const scopeInfo = exitScope(); + + // Skip named function expressions + if (allowNamedFunctions && node.id && node.id.name) { + return; + } + + // Skip generators. + if (node.generator) { + return; + } + + // Skip recursive functions. + const nameVar = sourceCode.getDeclaredVariables(node)[0]; + + if (isFunctionName(nameVar) && nameVar.references.length > 0) { + return; + } + + // Skip if it's using arguments. + const variable = getVariableOfArguments(sourceCode.getScope(node)); + + if (variable && variable.references.length > 0) { + return; + } + + // Reports if it's a callback which can replace with arrows. + const callbackInfo = getCallbackInfo(node); + + if (callbackInfo.isCallback && + (!allowUnboundThis || !scopeInfo.this || callbackInfo.isLexicalThis) && + !scopeInfo.super && + !scopeInfo.meta + ) { + context.report({ + node, + messageId: "preferArrowCallback", + *fix(fixer) { + if ((!callbackInfo.isLexicalThis && scopeInfo.this) || hasDuplicateParams(node.params)) { + + /* + * If the callback function does not have .bind(this) and contains a reference to `this`, there + * is no way to determine what `this` should be, so don't perform any fixes. + * If the callback function has duplicates in its list of parameters (possible in sloppy mode), + * don't replace it with an arrow function, because this is a SyntaxError with arrow functions. + */ + return; + } + + // Remove `.bind(this)` if exists. + if (callbackInfo.isLexicalThis) { + const memberNode = node.parent; + + /* + * If `.bind(this)` exists but the parent is not `.bind(this)`, don't remove it automatically. + * E.g. `(foo || function(){}).bind(this)` + */ + if (memberNode.type !== "MemberExpression") { + return; + } + + const callNode = memberNode.parent; + const firstTokenToRemove = sourceCode.getTokenAfter(memberNode.object, astUtils.isNotClosingParenToken); + const lastTokenToRemove = sourceCode.getLastToken(callNode); + + /* + * If the member expression is parenthesized, don't remove the right paren. + * E.g. `(function(){}.bind)(this)` + * ^^^^^^^^^^^^ + */ + if (astUtils.isParenthesised(sourceCode, memberNode)) { + return; + } + + // If comments exist in the `.bind(this)`, don't remove those. + if (sourceCode.commentsExistBetween(firstTokenToRemove, lastTokenToRemove)) { + return; + } + + yield fixer.removeRange([firstTokenToRemove.range[0], lastTokenToRemove.range[1]]); + } + + // Convert the function expression to an arrow function. + const functionToken = sourceCode.getFirstToken(node, node.async ? 1 : 0); + const leftParenToken = sourceCode.getTokenAfter(functionToken, astUtils.isOpeningParenToken); + const tokenBeforeBody = sourceCode.getTokenBefore(node.body); + + if (sourceCode.commentsExistBetween(functionToken, leftParenToken)) { + + // Remove only extra tokens to keep comments. + yield fixer.remove(functionToken); + if (node.id) { + yield fixer.remove(node.id); + } + } else { + + // Remove extra tokens and spaces. + yield fixer.removeRange([functionToken.range[0], leftParenToken.range[0]]); + } + yield fixer.insertTextAfter(tokenBeforeBody, " =>"); + + // Get the node that will become the new arrow function. + let replacedNode = callbackInfo.isLexicalThis ? node.parent.parent : node; + + if (replacedNode.type === "ChainExpression") { + replacedNode = replacedNode.parent; + } + + /* + * If the replaced node is part of a BinaryExpression, LogicalExpression, or MemberExpression, then + * the arrow function needs to be parenthesized, because `foo || () => {}` is invalid syntax even + * though `foo || function() {}` is valid. + */ + if ( + replacedNode.parent.type !== "CallExpression" && + replacedNode.parent.type !== "ConditionalExpression" && + !astUtils.isParenthesised(sourceCode, replacedNode) && + !astUtils.isParenthesised(sourceCode, node) + ) { + yield fixer.insertTextBefore(replacedNode, "("); + yield fixer.insertTextAfter(replacedNode, ")"); + } + } + }); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/prefer-const.js b/node_modules/eslint/lib/rules/prefer-const.js index 8d5ae29ad..b43975e9b 100644 --- a/node_modules/eslint/lib/rules/prefer-const.js +++ b/node_modules/eslint/lib/rules/prefer-const.js @@ -16,12 +16,9 @@ const astUtils = require("./utils/ast-utils"); // Helpers //------------------------------------------------------------------------------ -const PATTERN_TYPE = - /^(?:.+?Pattern|RestElement|SpreadProperty|ExperimentalRestProperty|Property)$/u; -const DECLARATION_HOST_TYPE = - /^(?:Program|BlockStatement|StaticBlock|SwitchCase)$/u; -const DESTRUCTURING_HOST_TYPE = - /^(?:VariableDeclarator|AssignmentExpression)$/u; +const PATTERN_TYPE = /^(?:.+?Pattern|RestElement|SpreadProperty|ExperimentalRestProperty|Property)$/u; +const DECLARATION_HOST_TYPE = /^(?:Program|BlockStatement|StaticBlock|SwitchCase)$/u; +const DESTRUCTURING_HOST_TYPE = /^(?:VariableDeclarator|AssignmentExpression)$/u; /** * Checks whether a given node is located at `ForStatement.init` or not. @@ -29,7 +26,7 @@ const DESTRUCTURING_HOST_TYPE = * @returns {boolean} `true` if the node is located at `ForStatement.init`. */ function isInitOfForStatement(node) { - return node.parent.type === "ForStatement" && node.parent.init === node; + return node.parent.type === "ForStatement" && node.parent.init === node; } /** @@ -38,18 +35,20 @@ function isInitOfForStatement(node) { * @returns {boolean} `true` if the node can become a VariableDeclaration. */ function canBecomeVariableDeclaration(identifier) { - let node = identifier.parent; - - while (PATTERN_TYPE.test(node.type)) { - node = node.parent; - } - - return ( - node.type === "VariableDeclarator" || - (node.type === "AssignmentExpression" && - node.parent.type === "ExpressionStatement" && - DECLARATION_HOST_TYPE.test(node.parent.parent.type)) - ); + let node = identifier.parent; + + while (PATTERN_TYPE.test(node.type)) { + node = node.parent; + } + + return ( + node.type === "VariableDeclarator" || + ( + node.type === "AssignmentExpression" && + node.parent.type === "ExpressionStatement" && + DECLARATION_HOST_TYPE.test(node.parent.parent.type) + ) + ); } /** @@ -60,21 +59,18 @@ function canBecomeVariableDeclaration(identifier) { * @returns {boolean} Indicates if the variable is from outer scope or function parameters. */ function isOuterVariableInDestructing(name, initScope) { - if ( - initScope.through.some( - ref => ref.resolved && ref.resolved.name === name, - ) - ) { - return true; - } - const variable = astUtils.getVariableByName(initScope, name); + if (initScope.through.some(ref => ref.resolved && ref.resolved.name === name)) { + return true; + } - if (variable !== null) { - return variable.defs.some(def => def.type === "Parameter"); - } + const variable = astUtils.getVariableByName(initScope, name); - return false; + if (variable !== null) { + return variable.defs.some(def => def.type === "Parameter"); + } + + return false; } /** @@ -87,19 +83,19 @@ function isOuterVariableInDestructing(name, initScope) { * null. */ function getDestructuringHost(reference) { - if (!reference.isWrite()) { - return null; - } - let node = reference.identifier.parent; - - while (PATTERN_TYPE.test(node.type)) { - node = node.parent; - } - - if (!DESTRUCTURING_HOST_TYPE.test(node.type)) { - return null; - } - return node; + if (!reference.isWrite()) { + return null; + } + let node = reference.identifier.parent; + + while (PATTERN_TYPE.test(node.type)) { + node = node.parent; + } + + if (!DESTRUCTURING_HOST_TYPE.test(node.type)) { + return null; + } + return node; } /** @@ -112,43 +108,42 @@ function getDestructuringHost(reference) { * a MemberExpression, false if not. */ function hasMemberExpressionAssignment(node) { - switch (node.type) { - case "ObjectPattern": - return node.properties.some(prop => { - if (prop) { - /* - * Spread elements have an argument property while - * others have a value property. Because different - * parsers use different node types for spread elements, - * we just check if there is an argument property. - */ - return hasMemberExpressionAssignment( - prop.argument || prop.value, - ); - } - - return false; - }); - - case "ArrayPattern": - return node.elements.some(element => { - if (element) { - return hasMemberExpressionAssignment(element); - } - - return false; - }); - - case "AssignmentPattern": - return hasMemberExpressionAssignment(node.left); - - case "MemberExpression": - return true; - - // no default - } - - return false; + switch (node.type) { + case "ObjectPattern": + return node.properties.some(prop => { + if (prop) { + + /* + * Spread elements have an argument property while + * others have a value property. Because different + * parsers use different node types for spread elements, + * we just check if there is an argument property. + */ + return hasMemberExpressionAssignment(prop.argument || prop.value); + } + + return false; + }); + + case "ArrayPattern": + return node.elements.some(element => { + if (element) { + return hasMemberExpressionAssignment(element); + } + + return false; + }); + + case "AssignmentPattern": + return hasMemberExpressionAssignment(node.left); + + case "MemberExpression": + return true; + + // no default + } + + return false; } /** @@ -176,91 +171,90 @@ function hasMemberExpressionAssignment(node) { * Otherwise, null. */ function getIdentifierIfShouldBeConst(variable, ignoreReadBeforeAssign) { - if (variable.eslintUsed && variable.scope.type === "global") { - return null; - } - - // Finds the unique WriteReference. - let writer = null; - let isReadBeforeInit = false; - const references = variable.references; - - for (let i = 0; i < references.length; ++i) { - const reference = references[i]; - - if (reference.isWrite()) { - const isReassigned = - writer !== null && writer.identifier !== reference.identifier; - - if (isReassigned) { - return null; - } - - const destructuringHost = getDestructuringHost(reference); - - if ( - destructuringHost !== null && - destructuringHost.left !== void 0 - ) { - const leftNode = destructuringHost.left; - let hasOuterVariables = false, - hasNonIdentifiers = false; - - if (leftNode.type === "ObjectPattern") { - const properties = leftNode.properties; - - hasOuterVariables = properties - .filter(prop => prop.value) - .map(prop => prop.value.name) - .some(name => - isOuterVariableInDestructing(name, variable.scope), - ); - - hasNonIdentifiers = hasMemberExpressionAssignment(leftNode); - } else if (leftNode.type === "ArrayPattern") { - const elements = leftNode.elements; - - hasOuterVariables = elements - .map(element => element && element.name) - .some(name => - isOuterVariableInDestructing(name, variable.scope), - ); - - hasNonIdentifiers = hasMemberExpressionAssignment(leftNode); - } - - if (hasOuterVariables || hasNonIdentifiers) { - return null; - } - } - - writer = reference; - } else if (reference.isRead() && writer === null) { - if (ignoreReadBeforeAssign) { - return null; - } - isReadBeforeInit = true; - } - } - - /* - * If the assignment is from a different scope, ignore it. - * If the assignment cannot change to a declaration, ignore it. - */ - const shouldBeConst = - writer !== null && - writer.from === variable.scope && - canBecomeVariableDeclaration(writer.identifier); - - if (!shouldBeConst) { - return null; - } - - if (isReadBeforeInit) { - return variable.defs[0].name; - } - - return writer.identifier; + if (variable.eslintUsed && variable.scope.type === "global") { + return null; + } + + // Finds the unique WriteReference. + let writer = null; + let isReadBeforeInit = false; + const references = variable.references; + + for (let i = 0; i < references.length; ++i) { + const reference = references[i]; + + if (reference.isWrite()) { + const isReassigned = ( + writer !== null && + writer.identifier !== reference.identifier + ); + + if (isReassigned) { + return null; + } + + const destructuringHost = getDestructuringHost(reference); + + if (destructuringHost !== null && destructuringHost.left !== void 0) { + const leftNode = destructuringHost.left; + let hasOuterVariables = false, + hasNonIdentifiers = false; + + if (leftNode.type === "ObjectPattern") { + const properties = leftNode.properties; + + hasOuterVariables = properties + .filter(prop => prop.value) + .map(prop => prop.value.name) + .some(name => isOuterVariableInDestructing(name, variable.scope)); + + hasNonIdentifiers = hasMemberExpressionAssignment(leftNode); + + } else if (leftNode.type === "ArrayPattern") { + const elements = leftNode.elements; + + hasOuterVariables = elements + .map(element => element && element.name) + .some(name => isOuterVariableInDestructing(name, variable.scope)); + + hasNonIdentifiers = hasMemberExpressionAssignment(leftNode); + } + + if (hasOuterVariables || hasNonIdentifiers) { + return null; + } + + } + + writer = reference; + + } else if (reference.isRead() && writer === null) { + if (ignoreReadBeforeAssign) { + return null; + } + isReadBeforeInit = true; + } + } + + /* + * If the assignment is from a different scope, ignore it. + * If the assignment cannot change to a declaration, ignore it. + */ + const shouldBeConst = ( + writer !== null && + writer.from === variable.scope && + canBecomeVariableDeclaration(writer.identifier) + ); + + if (!shouldBeConst) { + return null; + } + + if (isReadBeforeInit) { + return variable.defs[0].name; + } + + return writer.identifier; } /** @@ -274,44 +268,41 @@ function getIdentifierIfShouldBeConst(variable, ignoreReadBeforeAssign) { * @returns {Map} Grouped identifier nodes. */ function groupByDestructuring(variables, ignoreReadBeforeAssign) { - const identifierMap = new Map(); - - for (let i = 0; i < variables.length; ++i) { - const variable = variables[i]; - const references = variable.references; - const identifier = getIdentifierIfShouldBeConst( - variable, - ignoreReadBeforeAssign, - ); - let prevId = null; - - for (let j = 0; j < references.length; ++j) { - const reference = references[j]; - const id = reference.identifier; - - /* - * Avoid counting a reference twice or more for default values of - * destructuring. - */ - if (id === prevId) { - continue; - } - prevId = id; - - // Add the identifier node into the destructuring group. - const group = getDestructuringHost(reference); - - if (group) { - if (identifierMap.has(group)) { - identifierMap.get(group).push(identifier); - } else { - identifierMap.set(group, [identifier]); - } - } - } - } - - return identifierMap; + const identifierMap = new Map(); + + for (let i = 0; i < variables.length; ++i) { + const variable = variables[i]; + const references = variable.references; + const identifier = getIdentifierIfShouldBeConst(variable, ignoreReadBeforeAssign); + let prevId = null; + + for (let j = 0; j < references.length; ++j) { + const reference = references[j]; + const id = reference.identifier; + + /* + * Avoid counting a reference twice or more for default values of + * destructuring. + */ + if (id === prevId) { + continue; + } + prevId = id; + + // Add the identifier node into the destructuring group. + const group = getDestructuringHost(reference); + + if (group) { + if (identifierMap.has(group)) { + identifierMap.get(group).push(identifier); + } else { + identifierMap.set(group, [identifier]); + } + } + } + } + + return identifierMap; } /** @@ -322,225 +313,189 @@ function groupByDestructuring(variables, ignoreReadBeforeAssign) { * @returns {ASTNode} The closest ancestor with the specified type; null if no such ancestor exists. */ function findUp(node, type, shouldStop) { - if (!node || shouldStop(node)) { - return null; - } - if (node.type === type) { - return node; - } - return findUp(node.parent, type, shouldStop); + if (!node || shouldStop(node)) { + return null; + } + if (node.type === type) { + return node; + } + return findUp(node.parent, type, shouldStop); } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - defaultOptions: [ - { - destructuring: "any", - ignoreReadBeforeAssign: false, - }, - ], - - docs: { - description: - "Require `const` declarations for variables that are never reassigned after declared", - recommended: false, - url: "https://eslint.org/docs/latest/rules/prefer-const", - }, - - fixable: "code", - - schema: [ - { - type: "object", - properties: { - destructuring: { enum: ["any", "all"] }, - ignoreReadBeforeAssign: { type: "boolean" }, - }, - additionalProperties: false, - }, - ], - messages: { - useConst: "'{{name}}' is never reassigned. Use 'const' instead.", - }, - }, - - create(context) { - const [{ destructuring, ignoreReadBeforeAssign }] = context.options; - const shouldMatchAnyDestructuredVariable = destructuring !== "all"; - const sourceCode = context.sourceCode; - const variables = []; - let reportCount = 0; - let checkedId = null; - let checkedName = ""; - - /** - * Reports given identifier nodes if all of the nodes should be declared - * as const. - * - * The argument 'nodes' is an array of Identifier nodes. - * This node is the result of 'getIdentifierIfShouldBeConst()', so it's - * nullable. In simple declaration or assignment cases, the length of - * the array is 1. In destructuring cases, the length of the array can - * be 2 or more. - * @param {(eslint-scope.Reference|null)[]} nodes - * References which are grouped by destructuring to report. - * @returns {void} - */ - function checkGroup(nodes) { - const nodesToReport = nodes.filter(Boolean); - - if ( - nodes.length && - (shouldMatchAnyDestructuredVariable || - nodesToReport.length === nodes.length) - ) { - const varDeclParent = findUp( - nodes[0], - "VariableDeclaration", - parentNode => parentNode.type.endsWith("Statement"), - ); - const isVarDecParentNull = varDeclParent === null; - - if ( - !isVarDecParentNull && - varDeclParent.declarations.length > 0 - ) { - const firstDeclaration = varDeclParent.declarations[0]; - - if (firstDeclaration.init) { - const firstDecParent = firstDeclaration.init.parent; - - /* - * First we check the declaration type and then depending on - * if the type is a "VariableDeclarator" or its an "ObjectPattern" - * we compare the name and id from the first identifier, if the names are different - * we assign the new name, id and reset the count of reportCount and nodeCount in - * order to check each block for the number of reported errors and base our fix - * based on comparing nodes.length and nodesToReport.length. - */ - - if (firstDecParent.type === "VariableDeclarator") { - if (firstDecParent.id.name !== checkedName) { - checkedName = firstDecParent.id.name; - reportCount = 0; - } - - if (firstDecParent.id.type === "ObjectPattern") { - if (firstDecParent.init.name !== checkedName) { - checkedName = firstDecParent.init.name; - reportCount = 0; - } - } - - if (firstDecParent.id !== checkedId) { - checkedId = firstDecParent.id; - reportCount = 0; - } - } - } - } - - let shouldFix = - varDeclParent && - // Don't do a fix unless all variables in the declarations are initialized (or it's in a for-in or for-of loop) - (varDeclParent.parent.type === "ForInStatement" || - varDeclParent.parent.type === "ForOfStatement" || - varDeclParent.declarations.every( - declaration => declaration.init, - )) && - /* - * If options.destructuring is "all", then this warning will not occur unless - * every assignment in the destructuring should be const. In that case, it's safe - * to apply the fix. - */ - nodesToReport.length === nodes.length; - - if ( - !isVarDecParentNull && - varDeclParent.declarations && - varDeclParent.declarations.length !== 1 - ) { - if ( - varDeclParent && - varDeclParent.declarations && - varDeclParent.declarations.length >= 1 - ) { - /* - * Add nodesToReport.length to a count, then comparing the count to the length - * of the declarations in the current block. - */ - - reportCount += nodesToReport.length; - - let totalDeclarationsCount = 0; - - varDeclParent.declarations.forEach(declaration => { - if (declaration.id.type === "ObjectPattern") { - totalDeclarationsCount += - declaration.id.properties.length; - } else if (declaration.id.type === "ArrayPattern") { - totalDeclarationsCount += - declaration.id.elements.length; - } else { - totalDeclarationsCount += 1; - } - }); - - shouldFix = - shouldFix && reportCount === totalDeclarationsCount; - } - } - - nodesToReport.forEach(node => { - context.report({ - node, - messageId: "useConst", - data: node, - fix: shouldFix - ? fixer => { - const letKeywordToken = - sourceCode.getFirstToken( - varDeclParent, - t => t.value === varDeclParent.kind, - ); - - /** - * Extend the replacement range to the whole declaration, - * in order to prevent other fixes in the same pass - * https://github.com/eslint/eslint/issues/13899 - */ - return new FixTracker(fixer, sourceCode) - .retainRange(varDeclParent.range) - .replaceTextRange( - letKeywordToken.range, - "const", - ); - } - : null, - }); - }); - } - } - - return { - "Program:exit"() { - groupByDestructuring(variables, ignoreReadBeforeAssign).forEach( - checkGroup, - ); - }, - - VariableDeclaration(node) { - if (node.kind === "let" && !isInitOfForStatement(node)) { - variables.push(...sourceCode.getDeclaredVariables(node)); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Require `const` declarations for variables that are never reassigned after declared", + recommended: false, + url: "https://eslint.org/docs/latest/rules/prefer-const" + }, + + fixable: "code", + + schema: [ + { + type: "object", + properties: { + destructuring: { enum: ["any", "all"], default: "any" }, + ignoreReadBeforeAssign: { type: "boolean", default: false } + }, + additionalProperties: false + } + ], + messages: { + useConst: "'{{name}}' is never reassigned. Use 'const' instead." + } + }, + + create(context) { + const options = context.options[0] || {}; + const sourceCode = context.sourceCode; + const shouldMatchAnyDestructuredVariable = options.destructuring !== "all"; + const ignoreReadBeforeAssign = options.ignoreReadBeforeAssign === true; + const variables = []; + let reportCount = 0; + let checkedId = null; + let checkedName = ""; + + + /** + * Reports given identifier nodes if all of the nodes should be declared + * as const. + * + * The argument 'nodes' is an array of Identifier nodes. + * This node is the result of 'getIdentifierIfShouldBeConst()', so it's + * nullable. In simple declaration or assignment cases, the length of + * the array is 1. In destructuring cases, the length of the array can + * be 2 or more. + * @param {(eslint-scope.Reference|null)[]} nodes + * References which are grouped by destructuring to report. + * @returns {void} + */ + function checkGroup(nodes) { + const nodesToReport = nodes.filter(Boolean); + + if (nodes.length && (shouldMatchAnyDestructuredVariable || nodesToReport.length === nodes.length)) { + const varDeclParent = findUp(nodes[0], "VariableDeclaration", parentNode => parentNode.type.endsWith("Statement")); + const isVarDecParentNull = varDeclParent === null; + + if (!isVarDecParentNull && varDeclParent.declarations.length > 0) { + const firstDeclaration = varDeclParent.declarations[0]; + + if (firstDeclaration.init) { + const firstDecParent = firstDeclaration.init.parent; + + /* + * First we check the declaration type and then depending on + * if the type is a "VariableDeclarator" or its an "ObjectPattern" + * we compare the name and id from the first identifier, if the names are different + * we assign the new name, id and reset the count of reportCount and nodeCount in + * order to check each block for the number of reported errors and base our fix + * based on comparing nodes.length and nodesToReport.length. + */ + + if (firstDecParent.type === "VariableDeclarator") { + + if (firstDecParent.id.name !== checkedName) { + checkedName = firstDecParent.id.name; + reportCount = 0; + } + + if (firstDecParent.id.type === "ObjectPattern") { + if (firstDecParent.init.name !== checkedName) { + checkedName = firstDecParent.init.name; + reportCount = 0; + } + } + + if (firstDecParent.id !== checkedId) { + checkedId = firstDecParent.id; + reportCount = 0; + } + } + } + } + + let shouldFix = varDeclParent && + + // Don't do a fix unless all variables in the declarations are initialized (or it's in a for-in or for-of loop) + (varDeclParent.parent.type === "ForInStatement" || varDeclParent.parent.type === "ForOfStatement" || + varDeclParent.declarations.every(declaration => declaration.init)) && + + /* + * If options.destructuring is "all", then this warning will not occur unless + * every assignment in the destructuring should be const. In that case, it's safe + * to apply the fix. + */ + nodesToReport.length === nodes.length; + + if (!isVarDecParentNull && varDeclParent.declarations && varDeclParent.declarations.length !== 1) { + + if (varDeclParent && varDeclParent.declarations && varDeclParent.declarations.length >= 1) { + + /* + * Add nodesToReport.length to a count, then comparing the count to the length + * of the declarations in the current block. + */ + + reportCount += nodesToReport.length; + + let totalDeclarationsCount = 0; + + varDeclParent.declarations.forEach(declaration => { + if (declaration.id.type === "ObjectPattern") { + totalDeclarationsCount += declaration.id.properties.length; + } else if (declaration.id.type === "ArrayPattern") { + totalDeclarationsCount += declaration.id.elements.length; + } else { + totalDeclarationsCount += 1; + } + }); + + shouldFix = shouldFix && (reportCount === totalDeclarationsCount); + } + } + + nodesToReport.forEach(node => { + context.report({ + node, + messageId: "useConst", + data: node, + fix: shouldFix + ? fixer => { + const letKeywordToken = sourceCode.getFirstToken(varDeclParent, t => t.value === varDeclParent.kind); + + /** + * Extend the replacement range to the whole declaration, + * in order to prevent other fixes in the same pass + * https://github.com/eslint/eslint/issues/13899 + */ + return new FixTracker(fixer, sourceCode) + .retainRange(varDeclParent.range) + .replaceTextRange(letKeywordToken.range, "const"); + } + : null + }); + }); + } + } + + return { + "Program:exit"() { + groupByDestructuring(variables, ignoreReadBeforeAssign).forEach(checkGroup); + }, + + VariableDeclaration(node) { + if (node.kind === "let" && !isInitOfForStatement(node)) { + variables.push(...sourceCode.getDeclaredVariables(node)); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/prefer-destructuring.js b/node_modules/eslint/lib/rules/prefer-destructuring.js index 09d0e5be3..c6075c55b 100644 --- a/node_modules/eslint/lib/rules/prefer-destructuring.js +++ b/node_modules/eslint/lib/rules/prefer-destructuring.js @@ -14,319 +14,288 @@ const astUtils = require("./utils/ast-utils"); // Helpers //------------------------------------------------------------------------------ -const PRECEDENCE_OF_ASSIGNMENT_EXPR = astUtils.getPrecedence({ - type: "AssignmentExpression", -}); +const PRECEDENCE_OF_ASSIGNMENT_EXPR = astUtils.getPrecedence({ type: "AssignmentExpression" }); //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: "Require destructuring from arrays and/or objects", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/prefer-destructuring", - }, - - fixable: "code", - - schema: [ - { - /* - * old support {array: Boolean, object: Boolean} - * new support {VariableDeclarator: {}, AssignmentExpression: {}} - */ - oneOf: [ - { - type: "object", - properties: { - VariableDeclarator: { - type: "object", - properties: { - array: { - type: "boolean", - }, - object: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - AssignmentExpression: { - type: "object", - properties: { - array: { - type: "boolean", - }, - object: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - }, - additionalProperties: false, - }, - { - type: "object", - properties: { - array: { - type: "boolean", - }, - object: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - }, - { - type: "object", - properties: { - enforceForRenamedProperties: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - - messages: { - preferDestructuring: "Use {{type}} destructuring.", - }, - }, - create(context) { - const enabledTypes = context.options[0]; - const enforceForRenamedProperties = - context.options[1] && - context.options[1].enforceForRenamedProperties; - let normalizedOptions = { - VariableDeclarator: { array: true, object: true }, - AssignmentExpression: { array: true, object: true }, - }; - - if (enabledTypes) { - normalizedOptions = - typeof enabledTypes.array !== "undefined" || - typeof enabledTypes.object !== "undefined" - ? { - VariableDeclarator: enabledTypes, - AssignmentExpression: enabledTypes, - } - : enabledTypes; - } - - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - /** - * Checks if destructuring type should be checked. - * @param {string} nodeType "AssignmentExpression" or "VariableDeclarator" - * @param {string} destructuringType "array" or "object" - * @returns {boolean} `true` if the destructuring type should be checked for the given node - */ - function shouldCheck(nodeType, destructuringType) { - return ( - normalizedOptions && - normalizedOptions[nodeType] && - normalizedOptions[nodeType][destructuringType] - ); - } - - /** - * Determines if the given node is accessing an array index - * - * This is used to differentiate array index access from object property - * access. - * @param {ASTNode} node the node to evaluate - * @returns {boolean} whether or not the node is an integer - */ - function isArrayIndexAccess(node) { - return Number.isInteger(node.property.value); - } - - /** - * Report that the given node should use destructuring - * @param {ASTNode} reportNode the node to report - * @param {string} type the type of destructuring that should have been done - * @param {Function|null} fix the fix function or null to pass to context.report - * @returns {void} - */ - function report(reportNode, type, fix) { - context.report({ - node: reportNode, - messageId: "preferDestructuring", - data: { type }, - fix, - }); - } - - /** - * Determines if a node should be fixed into object destructuring - * - * The fixer only fixes the simplest case of object destructuring, - * like: `let x = a.x`; - * - * Assignment expression is not fixed. - * Array destructuring is not fixed. - * Renamed property is not fixed. - * @param {ASTNode} node the node to evaluate - * @returns {boolean} whether or not the node should be fixed - */ - function shouldFix(node) { - return ( - node.type === "VariableDeclarator" && - node.id.type === "Identifier" && - node.init.type === "MemberExpression" && - !node.init.computed && - node.init.property.type === "Identifier" && - node.id.name === node.init.property.name - ); - } - - /** - * Fix a node into object destructuring. - * This function only handles the simplest case of object destructuring, - * see {@link shouldFix}. - * @param {SourceCodeFixer} fixer the fixer object - * @param {ASTNode} node the node to be fixed. - * @returns {Object} a fix for the node - */ - function fixIntoObjectDestructuring(fixer, node) { - const rightNode = node.init; - const sourceCode = context.sourceCode; - - // Don't fix if that would remove any comments. Only comments inside `rightNode.object` can be preserved. - if ( - sourceCode.getCommentsInside(node).length > - sourceCode.getCommentsInside(rightNode.object).length - ) { - return null; - } - - let objectText = sourceCode.getText(rightNode.object); - - if ( - astUtils.getPrecedence(rightNode.object) < - PRECEDENCE_OF_ASSIGNMENT_EXPR - ) { - objectText = `(${objectText})`; - } - - return fixer.replaceText( - node, - `{${rightNode.property.name}} = ${objectText}`, - ); - } - - /** - * Check that the `prefer-destructuring` rules are followed based on the - * given left- and right-hand side of the assignment. - * - * Pulled out into a separate method so that VariableDeclarators and - * AssignmentExpressions can share the same verification logic. - * @param {ASTNode} leftNode the left-hand side of the assignment - * @param {ASTNode} rightNode the right-hand side of the assignment - * @param {ASTNode} reportNode the node to report the error on - * @returns {void} - */ - function performCheck(leftNode, rightNode, reportNode) { - if ( - rightNode.type !== "MemberExpression" || - rightNode.object.type === "Super" || - rightNode.property.type === "PrivateIdentifier" - ) { - return; - } - - if (isArrayIndexAccess(rightNode)) { - if (shouldCheck(reportNode.type, "array")) { - report(reportNode, "array", null); - } - return; - } - - const fix = shouldFix(reportNode) - ? fixer => fixIntoObjectDestructuring(fixer, reportNode) - : null; - - if ( - shouldCheck(reportNode.type, "object") && - enforceForRenamedProperties - ) { - report(reportNode, "object", fix); - return; - } - - if (shouldCheck(reportNode.type, "object")) { - const property = rightNode.property; - - if ( - (property.type === "Literal" && - leftNode.name === property.value) || - (property.type === "Identifier" && - leftNode.name === property.name && - !rightNode.computed) - ) { - report(reportNode, "object", fix); - } - } - } - - /** - * Check if a given variable declarator is coming from an property access - * that should be using destructuring instead - * @param {ASTNode} node the variable declarator to check - * @returns {void} - */ - function checkVariableDeclarator(node) { - // Skip if variable is declared without assignment - if (!node.init) { - return; - } - - // Variable declarations using explicit resource management cannot use destructuring (parse error) - if ( - node.parent.kind === "using" || - node.parent.kind === "await using" - ) { - return; - } - - // We only care about member expressions past this point - if (node.init.type !== "MemberExpression") { - return; - } - - performCheck(node.id, node.init, node); - } - - /** - * Run the `prefer-destructuring` check on an AssignmentExpression - * @param {ASTNode} node the AssignmentExpression node - * @returns {void} - */ - function checkAssignmentExpression(node) { - if (node.operator === "=") { - performCheck(node.left, node.right, node); - } - } - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - return { - VariableDeclarator: checkVariableDeclarator, - AssignmentExpression: checkAssignmentExpression, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Require destructuring from arrays and/or objects", + recommended: false, + url: "https://eslint.org/docs/latest/rules/prefer-destructuring" + }, + + fixable: "code", + + schema: [ + { + + /* + * old support {array: Boolean, object: Boolean} + * new support {VariableDeclarator: {}, AssignmentExpression: {}} + */ + oneOf: [ + { + type: "object", + properties: { + VariableDeclarator: { + type: "object", + properties: { + array: { + type: "boolean" + }, + object: { + type: "boolean" + } + }, + additionalProperties: false + }, + AssignmentExpression: { + type: "object", + properties: { + array: { + type: "boolean" + }, + object: { + type: "boolean" + } + }, + additionalProperties: false + } + }, + additionalProperties: false + }, + { + type: "object", + properties: { + array: { + type: "boolean" + }, + object: { + type: "boolean" + } + }, + additionalProperties: false + } + ] + }, + { + type: "object", + properties: { + enforceForRenamedProperties: { + type: "boolean" + } + }, + additionalProperties: false + } + ], + + messages: { + preferDestructuring: "Use {{type}} destructuring." + } + }, + create(context) { + + const enabledTypes = context.options[0]; + const enforceForRenamedProperties = context.options[1] && context.options[1].enforceForRenamedProperties; + let normalizedOptions = { + VariableDeclarator: { array: true, object: true }, + AssignmentExpression: { array: true, object: true } + }; + + if (enabledTypes) { + normalizedOptions = typeof enabledTypes.array !== "undefined" || typeof enabledTypes.object !== "undefined" + ? { VariableDeclarator: enabledTypes, AssignmentExpression: enabledTypes } + : enabledTypes; + } + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + /** + * Checks if destructuring type should be checked. + * @param {string} nodeType "AssignmentExpression" or "VariableDeclarator" + * @param {string} destructuringType "array" or "object" + * @returns {boolean} `true` if the destructuring type should be checked for the given node + */ + function shouldCheck(nodeType, destructuringType) { + return normalizedOptions && + normalizedOptions[nodeType] && + normalizedOptions[nodeType][destructuringType]; + } + + /** + * Determines if the given node is accessing an array index + * + * This is used to differentiate array index access from object property + * access. + * @param {ASTNode} node the node to evaluate + * @returns {boolean} whether or not the node is an integer + */ + function isArrayIndexAccess(node) { + return Number.isInteger(node.property.value); + } + + /** + * Report that the given node should use destructuring + * @param {ASTNode} reportNode the node to report + * @param {string} type the type of destructuring that should have been done + * @param {Function|null} fix the fix function or null to pass to context.report + * @returns {void} + */ + function report(reportNode, type, fix) { + context.report({ + node: reportNode, + messageId: "preferDestructuring", + data: { type }, + fix + }); + } + + /** + * Determines if a node should be fixed into object destructuring + * + * The fixer only fixes the simplest case of object destructuring, + * like: `let x = a.x`; + * + * Assignment expression is not fixed. + * Array destructuring is not fixed. + * Renamed property is not fixed. + * @param {ASTNode} node the node to evaluate + * @returns {boolean} whether or not the node should be fixed + */ + function shouldFix(node) { + return node.type === "VariableDeclarator" && + node.id.type === "Identifier" && + node.init.type === "MemberExpression" && + !node.init.computed && + node.init.property.type === "Identifier" && + node.id.name === node.init.property.name; + } + + /** + * Fix a node into object destructuring. + * This function only handles the simplest case of object destructuring, + * see {@link shouldFix}. + * @param {SourceCodeFixer} fixer the fixer object + * @param {ASTNode} node the node to be fixed. + * @returns {Object} a fix for the node + */ + function fixIntoObjectDestructuring(fixer, node) { + const rightNode = node.init; + const sourceCode = context.sourceCode; + + // Don't fix if that would remove any comments. Only comments inside `rightNode.object` can be preserved. + if (sourceCode.getCommentsInside(node).length > sourceCode.getCommentsInside(rightNode.object).length) { + return null; + } + + let objectText = sourceCode.getText(rightNode.object); + + if (astUtils.getPrecedence(rightNode.object) < PRECEDENCE_OF_ASSIGNMENT_EXPR) { + objectText = `(${objectText})`; + } + + return fixer.replaceText( + node, + `{${rightNode.property.name}} = ${objectText}` + ); + } + + /** + * Check that the `prefer-destructuring` rules are followed based on the + * given left- and right-hand side of the assignment. + * + * Pulled out into a separate method so that VariableDeclarators and + * AssignmentExpressions can share the same verification logic. + * @param {ASTNode} leftNode the left-hand side of the assignment + * @param {ASTNode} rightNode the right-hand side of the assignment + * @param {ASTNode} reportNode the node to report the error on + * @returns {void} + */ + function performCheck(leftNode, rightNode, reportNode) { + if ( + rightNode.type !== "MemberExpression" || + rightNode.object.type === "Super" || + rightNode.property.type === "PrivateIdentifier" + ) { + return; + } + + if (isArrayIndexAccess(rightNode)) { + if (shouldCheck(reportNode.type, "array")) { + report(reportNode, "array", null); + } + return; + } + + const fix = shouldFix(reportNode) + ? fixer => fixIntoObjectDestructuring(fixer, reportNode) + : null; + + if (shouldCheck(reportNode.type, "object") && enforceForRenamedProperties) { + report(reportNode, "object", fix); + return; + } + + if (shouldCheck(reportNode.type, "object")) { + const property = rightNode.property; + + if ( + (property.type === "Literal" && leftNode.name === property.value) || + (property.type === "Identifier" && leftNode.name === property.name && !rightNode.computed) + ) { + report(reportNode, "object", fix); + } + } + } + + /** + * Check if a given variable declarator is coming from an property access + * that should be using destructuring instead + * @param {ASTNode} node the variable declarator to check + * @returns {void} + */ + function checkVariableDeclarator(node) { + + // Skip if variable is declared without assignment + if (!node.init) { + return; + } + + // We only care about member expressions past this point + if (node.init.type !== "MemberExpression") { + return; + } + + performCheck(node.id, node.init, node); + } + + /** + * Run the `prefer-destructuring` check on an AssignmentExpression + * @param {ASTNode} node the AssignmentExpression node + * @returns {void} + */ + function checkAssignmentExpression(node) { + if (node.operator === "=") { + performCheck(node.left, node.right, node); + } + } + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + return { + VariableDeclarator: checkVariableDeclarator, + AssignmentExpression: checkAssignmentExpression + }; + } }; diff --git a/node_modules/eslint/lib/rules/prefer-exponentiation-operator.js b/node_modules/eslint/lib/rules/prefer-exponentiation-operator.js index beb78438b..6d807f9cf 100644 --- a/node_modules/eslint/lib/rules/prefer-exponentiation-operator.js +++ b/node_modules/eslint/lib/rules/prefer-exponentiation-operator.js @@ -16,10 +16,7 @@ const { CALL, ReferenceTracker } = require("@eslint-community/eslint-utils"); // Helpers //------------------------------------------------------------------------------ -const PRECEDENCE_OF_EXPONENTIATION_EXPR = astUtils.getPrecedence({ - type: "BinaryExpression", - operator: "**", -}); +const PRECEDENCE_OF_EXPONENTIATION_EXPR = astUtils.getPrecedence({ type: "BinaryExpression", operator: "**" }); /** * Determines whether the given node needs parens if used as the base in an exponentiation binary expression. @@ -27,13 +24,15 @@ const PRECEDENCE_OF_EXPONENTIATION_EXPR = astUtils.getPrecedence({ * @returns {boolean} `true` if the node needs to be parenthesised. */ function doesBaseNeedParens(base) { - return ( - // '**' is right-associative, parens are needed when Math.pow(a ** b, c) is converted to (a ** b) ** c - astUtils.getPrecedence(base) <= PRECEDENCE_OF_EXPONENTIATION_EXPR || - // An unary operator cannot be used immediately before an exponentiation expression - base.type === "AwaitExpression" || - base.type === "UnaryExpression" - ); + return ( + + // '**' is right-associative, parens are needed when Math.pow(a ** b, c) is converted to (a ** b) ** c + astUtils.getPrecedence(base) <= PRECEDENCE_OF_EXPONENTIATION_EXPR || + + // An unary operator cannot be used immediately before an exponentiation expression + base.type === "AwaitExpression" || + base.type === "UnaryExpression" + ); } /** @@ -42,8 +41,9 @@ function doesBaseNeedParens(base) { * @returns {boolean} `true` if the node needs to be parenthesised. */ function doesExponentNeedParens(exponent) { - // '**' is right-associative, there is no need for parens when Math.pow(a, b ** c) is converted to a ** b ** c - return astUtils.getPrecedence(exponent) < PRECEDENCE_OF_EXPONENTIATION_EXPR; + + // '**' is right-associative, there is no need for parens when Math.pow(a, b ** c) is converted to a ** b ** c + return astUtils.getPrecedence(exponent) < PRECEDENCE_OF_EXPONENTIATION_EXPR; } /** @@ -53,35 +53,22 @@ function doesExponentNeedParens(exponent) { * @returns {boolean} `true` if the expression needs to be parenthesised. */ function doesExponentiationExpressionNeedParens(node, sourceCode) { - const parent = - node.parent.type === "ChainExpression" - ? node.parent.parent - : node.parent; - - const parentPrecedence = astUtils.getPrecedence(parent); - const needsParens = - parent.type === "ClassDeclaration" || - (parent.type.endsWith("Expression") && - (parentPrecedence === -1 || - parentPrecedence >= PRECEDENCE_OF_EXPONENTIATION_EXPR) && - !( - parent.type === "BinaryExpression" && - parent.operator === "**" && - parent.right === node - ) && - !( - (parent.type === "CallExpression" || - parent.type === "NewExpression") && - parent.arguments.includes(node) - ) && - !( - parent.type === "MemberExpression" && - parent.computed && - parent.property === node - ) && - !(parent.type === "ArrayExpression")); - - return needsParens && !astUtils.isParenthesised(sourceCode, node); + const parent = node.parent.type === "ChainExpression" ? node.parent.parent : node.parent; + + const parentPrecedence = astUtils.getPrecedence(parent); + const needsParens = ( + parent.type === "ClassDeclaration" || + ( + parent.type.endsWith("Expression") && + (parentPrecedence === -1 || parentPrecedence >= PRECEDENCE_OF_EXPONENTIATION_EXPR) && + !(parent.type === "BinaryExpression" && parent.operator === "**" && parent.right === node) && + !((parent.type === "CallExpression" || parent.type === "NewExpression") && parent.arguments.includes(node)) && + !(parent.type === "MemberExpression" && parent.computed && parent.property === node) && + !(parent.type === "ArrayExpression") + ) + ); + + return needsParens && !astUtils.isParenthesised(sourceCode, node); } /** @@ -91,145 +78,114 @@ function doesExponentiationExpressionNeedParens(node, sourceCode) { * @returns {string} parenthesised or unchanged text. */ function parenthesizeIfShould(text, shouldParenthesize) { - return shouldParenthesize ? `(${text})` : text; + return shouldParenthesize ? `(${text})` : text; } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: - "Disallow the use of `Math.pow` in favor of the `**` operator", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/prefer-exponentiation-operator", - }, - - schema: [], - fixable: "code", - - messages: { - useExponentiation: "Use the '**' operator instead of 'Math.pow'.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - /** - * Reports the given node. - * @param {ASTNode} node 'Math.pow()' node to report. - * @returns {void} - */ - function report(node) { - context.report({ - node, - messageId: "useExponentiation", - fix(fixer) { - if ( - node.arguments.length !== 2 || - node.arguments.some( - arg => arg.type === "SpreadElement", - ) || - sourceCode.getCommentsInside(node).length > 0 - ) { - return null; - } - - const base = node.arguments[0], - exponent = node.arguments[1], - baseText = sourceCode.getText(base), - exponentText = sourceCode.getText(exponent), - shouldParenthesizeBase = doesBaseNeedParens(base), - shouldParenthesizeExponent = - doesExponentNeedParens(exponent), - shouldParenthesizeAll = - doesExponentiationExpressionNeedParens( - node, - sourceCode, - ); - - let prefix = "", - suffix = ""; - - if (!shouldParenthesizeAll) { - if (!shouldParenthesizeBase) { - const firstReplacementToken = - sourceCode.getFirstToken(base), - tokenBefore = sourceCode.getTokenBefore(node); - - if ( - tokenBefore && - tokenBefore.range[1] === node.range[0] && - !astUtils.canTokensBeAdjacent( - tokenBefore, - firstReplacementToken, - ) - ) { - prefix = " "; // a+Math.pow(++b, c) -> a+ ++b**c - } - } - if (!shouldParenthesizeExponent) { - const lastReplacementToken = - sourceCode.getLastToken(exponent), - tokenAfter = sourceCode.getTokenAfter(node); - - if ( - tokenAfter && - node.range[1] === tokenAfter.range[0] && - !astUtils.canTokensBeAdjacent( - lastReplacementToken, - tokenAfter, - ) - ) { - suffix = " "; // Math.pow(a, b)in c -> a**b in c - } - } - } - - const baseReplacement = parenthesizeIfShould( - baseText, - shouldParenthesizeBase, - ), - exponentReplacement = parenthesizeIfShould( - exponentText, - shouldParenthesizeExponent, - ), - replacement = parenthesizeIfShould( - `${baseReplacement}**${exponentReplacement}`, - shouldParenthesizeAll, - ); - - return fixer.replaceText( - node, - `${prefix}${replacement}${suffix}`, - ); - }, - }); - } - - return { - Program(node) { - const scope = sourceCode.getScope(node); - const tracker = new ReferenceTracker(scope); - const trackMap = { - Math: { - pow: { [CALL]: true }, - }, - }; - - for (const { node: refNode } of tracker.iterateGlobalReferences( - trackMap, - )) { - report(refNode); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow the use of `Math.pow` in favor of the `**` operator", + recommended: false, + url: "https://eslint.org/docs/latest/rules/prefer-exponentiation-operator" + }, + + schema: [], + fixable: "code", + + messages: { + useExponentiation: "Use the '**' operator instead of 'Math.pow'." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + + /** + * Reports the given node. + * @param {ASTNode} node 'Math.pow()' node to report. + * @returns {void} + */ + function report(node) { + context.report({ + node, + messageId: "useExponentiation", + fix(fixer) { + if ( + node.arguments.length !== 2 || + node.arguments.some(arg => arg.type === "SpreadElement") || + sourceCode.getCommentsInside(node).length > 0 + ) { + return null; + } + + const base = node.arguments[0], + exponent = node.arguments[1], + baseText = sourceCode.getText(base), + exponentText = sourceCode.getText(exponent), + shouldParenthesizeBase = doesBaseNeedParens(base), + shouldParenthesizeExponent = doesExponentNeedParens(exponent), + shouldParenthesizeAll = doesExponentiationExpressionNeedParens(node, sourceCode); + + let prefix = "", + suffix = ""; + + if (!shouldParenthesizeAll) { + if (!shouldParenthesizeBase) { + const firstReplacementToken = sourceCode.getFirstToken(base), + tokenBefore = sourceCode.getTokenBefore(node); + + if ( + tokenBefore && + tokenBefore.range[1] === node.range[0] && + !astUtils.canTokensBeAdjacent(tokenBefore, firstReplacementToken) + ) { + prefix = " "; // a+Math.pow(++b, c) -> a+ ++b**c + } + } + if (!shouldParenthesizeExponent) { + const lastReplacementToken = sourceCode.getLastToken(exponent), + tokenAfter = sourceCode.getTokenAfter(node); + + if ( + tokenAfter && + node.range[1] === tokenAfter.range[0] && + !astUtils.canTokensBeAdjacent(lastReplacementToken, tokenAfter) + ) { + suffix = " "; // Math.pow(a, b)in c -> a**b in c + } + } + } + + const baseReplacement = parenthesizeIfShould(baseText, shouldParenthesizeBase), + exponentReplacement = parenthesizeIfShould(exponentText, shouldParenthesizeExponent), + replacement = parenthesizeIfShould(`${baseReplacement}**${exponentReplacement}`, shouldParenthesizeAll); + + return fixer.replaceText(node, `${prefix}${replacement}${suffix}`); + } + }); + } + + return { + Program(node) { + const scope = sourceCode.getScope(node); + const tracker = new ReferenceTracker(scope); + const trackMap = { + Math: { + pow: { [CALL]: true } + } + }; + + for (const { node: refNode } of tracker.iterateGlobalReferences(trackMap)) { + report(refNode); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/prefer-named-capture-group.js b/node_modules/eslint/lib/rules/prefer-named-capture-group.js index d5c1f4602..a82ee1f78 100644 --- a/node_modules/eslint/lib/rules/prefer-named-capture-group.js +++ b/node_modules/eslint/lib/rules/prefer-named-capture-group.js @@ -10,19 +10,13 @@ //------------------------------------------------------------------------------ const { - CALL, - CONSTRUCT, - ReferenceTracker, - getStringIfConstant, + CALL, + CONSTRUCT, + ReferenceTracker, + getStringIfConstant } = require("@eslint-community/eslint-utils"); const regexpp = require("@eslint-community/regexpp"); -//------------------------------------------------------------------------------ -// Typedefs -//------------------------------------------------------------------------------ - -/** @import { SuggestedEdit } from "@eslint/core"; */ - //------------------------------------------------------------------------------ // Helpers //------------------------------------------------------------------------------ @@ -35,163 +29,150 @@ const parser = new regexpp.RegExpParser(); * @param {string} pattern The regular expression pattern to be checked. * @param {string} rawText Source text of the regexNode. * @param {ASTNode} regexNode AST node which contains the regular expression. - * @returns {Array} Fixer suggestions for the regex, if statically determinable. + * @returns {Array} Fixer suggestions for the regex, if statically determinable. */ function suggestIfPossible(groupStart, pattern, rawText, regexNode) { - switch (regexNode.type) { - case "Literal": - if (typeof regexNode.value === "string" && rawText.includes("\\")) { - return null; - } - break; - case "TemplateLiteral": - if ( - regexNode.expressions.length || - rawText.slice(1, -1) !== pattern - ) { - return null; - } - break; - default: - return null; - } - - const start = regexNode.range[0] + groupStart + 2; - - return [ - { - fix(fixer) { - const existingTemps = pattern.match(/temp\d+/gu) || []; - const highestTempCount = existingTemps.reduce( - (previous, next) => - Math.max(previous, Number(next.slice("temp".length))), - 0, - ); - - return fixer.insertTextBeforeRange( - [start, start], - `?`, - ); - }, - messageId: "addGroupName", - }, - { - fix(fixer) { - return fixer.insertTextBeforeRange([start, start], "?:"); - }, - messageId: "addNonCapture", - }, - ]; + switch (regexNode.type) { + case "Literal": + if (typeof regexNode.value === "string" && rawText.includes("\\")) { + return null; + } + break; + case "TemplateLiteral": + if (regexNode.expressions.length || rawText.slice(1, -1) !== pattern) { + return null; + } + break; + default: + return null; + } + + const start = regexNode.range[0] + groupStart + 2; + + return [ + { + fix(fixer) { + const existingTemps = pattern.match(/temp\d+/gu) || []; + const highestTempCount = existingTemps.reduce( + (previous, next) => + Math.max(previous, Number(next.slice("temp".length))), + 0 + ); + + return fixer.insertTextBeforeRange( + [start, start], + `?` + ); + }, + messageId: "addGroupName" + }, + { + fix(fixer) { + return fixer.insertTextBeforeRange( + [start, start], + "?:" + ); + }, + messageId: "addNonCapture" + } + ]; } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: - "Enforce using named capture group in regular expression", - recommended: false, - url: "https://eslint.org/docs/latest/rules/prefer-named-capture-group", - }, - - hasSuggestions: true, - - schema: [], - - messages: { - addGroupName: "Add name to capture group.", - addNonCapture: "Convert group to non-capturing.", - required: - "Capture group '{{group}}' should be converted to a named or non-capturing group.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - /** - * Function to check regular expression. - * @param {string} pattern The regular expression pattern to be checked. - * @param {ASTNode} node AST node which contains the regular expression or a call/new expression. - * @param {ASTNode} regexNode AST node which contains the regular expression. - * @param {string|null} flags The regular expression flags to be checked. - * @returns {void} - */ - function checkRegex(pattern, node, regexNode, flags) { - let ast; - - try { - ast = parser.parsePattern(pattern, 0, pattern.length, { - unicode: Boolean(flags && flags.includes("u")), - unicodeSets: Boolean(flags && flags.includes("v")), - }); - } catch { - // ignore regex syntax errors - return; - } - - regexpp.visitRegExpAST(ast, { - onCapturingGroupEnter(group) { - if (!group.name) { - const rawText = sourceCode.getText(regexNode); - const suggest = suggestIfPossible( - group.start, - pattern, - rawText, - regexNode, - ); - - context.report({ - node, - messageId: "required", - data: { - group: group.raw, - }, - suggest, - }); - } - }, - }); - } - - return { - Literal(node) { - if (node.regex) { - checkRegex( - node.regex.pattern, - node, - node, - node.regex.flags, - ); - } - }, - Program(node) { - const scope = sourceCode.getScope(node); - const tracker = new ReferenceTracker(scope); - const traceMap = { - RegExp: { - [CALL]: true, - [CONSTRUCT]: true, - }, - }; - - for (const { node: refNode } of tracker.iterateGlobalReferences( - traceMap, - )) { - const regex = getStringIfConstant(refNode.arguments[0]); - const flags = getStringIfConstant(refNode.arguments[1]); - - if (regex) { - checkRegex(regex, refNode, refNode.arguments[0], flags); - } - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Enforce using named capture group in regular expression", + recommended: false, + url: "https://eslint.org/docs/latest/rules/prefer-named-capture-group" + }, + + hasSuggestions: true, + + schema: [], + + messages: { + addGroupName: "Add name to capture group.", + addNonCapture: "Convert group to non-capturing.", + required: "Capture group '{{group}}' should be converted to a named or non-capturing group." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + + /** + * Function to check regular expression. + * @param {string} pattern The regular expression pattern to be checked. + * @param {ASTNode} node AST node which contains the regular expression or a call/new expression. + * @param {ASTNode} regexNode AST node which contains the regular expression. + * @param {string|null} flags The regular expression flags to be checked. + * @returns {void} + */ + function checkRegex(pattern, node, regexNode, flags) { + let ast; + + try { + ast = parser.parsePattern(pattern, 0, pattern.length, { + unicode: Boolean(flags && flags.includes("u")), + unicodeSets: Boolean(flags && flags.includes("v")) + }); + } catch { + + // ignore regex syntax errors + return; + } + + regexpp.visitRegExpAST(ast, { + onCapturingGroupEnter(group) { + if (!group.name) { + const rawText = sourceCode.getText(regexNode); + const suggest = suggestIfPossible(group.start, pattern, rawText, regexNode); + + context.report({ + node, + messageId: "required", + data: { + group: group.raw + }, + suggest + }); + } + } + }); + } + + return { + Literal(node) { + if (node.regex) { + checkRegex(node.regex.pattern, node, node, node.regex.flags); + } + }, + Program(node) { + const scope = sourceCode.getScope(node); + const tracker = new ReferenceTracker(scope); + const traceMap = { + RegExp: { + [CALL]: true, + [CONSTRUCT]: true + } + }; + + for (const { node: refNode } of tracker.iterateGlobalReferences(traceMap)) { + const regex = getStringIfConstant(refNode.arguments[0]); + const flags = getStringIfConstant(refNode.arguments[1]); + + if (regex) { + checkRegex(regex, refNode, refNode.arguments[0], flags); + } + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/prefer-numeric-literals.js b/node_modules/eslint/lib/rules/prefer-numeric-literals.js index 86dec8879..118d6dce4 100644 --- a/node_modules/eslint/lib/rules/prefer-numeric-literals.js +++ b/node_modules/eslint/lib/rules/prefer-numeric-literals.js @@ -16,9 +16,9 @@ const astUtils = require("./utils/ast-utils"); //------------------------------------------------------------------------------ const radixMap = new Map([ - [2, { system: "binary", literalPrefix: "0b" }], - [8, { system: "octal", literalPrefix: "0o" }], - [16, { system: "hexadecimal", literalPrefix: "0x" }], + [2, { system: "binary", literalPrefix: "0b" }], + [8, { system: "octal", literalPrefix: "0o" }], + [16, { system: "hexadecimal", literalPrefix: "0x" }] ]); /** @@ -29,129 +29,120 @@ const radixMap = new Map([ * false otherwise. */ function isParseInt(calleeNode) { - return ( - astUtils.isSpecificId(calleeNode, "parseInt") || - astUtils.isSpecificMemberAccess(calleeNode, "Number", "parseInt") - ); + return ( + astUtils.isSpecificId(calleeNode, "parseInt") || + astUtils.isSpecificMemberAccess(calleeNode, "Number", "parseInt") + ); } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: - "Disallow `parseInt()` and `Number.parseInt()` in favor of binary, octal, and hexadecimal literals", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/prefer-numeric-literals", - }, - - schema: [], - - messages: { - useLiteral: - "Use {{system}} literals instead of {{functionName}}().", - }, - - fixable: "code", - }, - - create(context) { - const sourceCode = context.sourceCode; - - //---------------------------------------------------------------------- - // Public - //---------------------------------------------------------------------- - - return { - "CallExpression[arguments.length=2]"(node) { - const [strNode, radixNode] = node.arguments, - str = astUtils.getStaticStringValue(strNode), - radix = radixNode.value; - - if ( - str !== null && - astUtils.isStringLiteral(strNode) && - radixNode.type === "Literal" && - typeof radix === "number" && - radixMap.has(radix) && - isParseInt(node.callee) - ) { - const { system, literalPrefix } = radixMap.get(radix); - - context.report({ - node, - messageId: "useLiteral", - data: { - system, - functionName: sourceCode.getText(node.callee), - }, - fix(fixer) { - if (sourceCode.getCommentsInside(node).length) { - return null; - } - - const replacement = `${literalPrefix}${str}`; - - if (+replacement !== parseInt(str, radix)) { - /* - * If the newly-produced literal would be invalid, (e.g. 0b1234), - * or it would yield an incorrect parseInt result for some other reason, don't make a fix. - * - * If `str` had numeric separators, `+replacement` will evaluate to `NaN` because unary `+` - * per the specification doesn't support numeric separators. Thus, the above condition will be `true` - * (`NaN !== anything` is always `true`) regardless of the `parseInt(str, radix)` value. - * Consequently, no autofixes will be made. This is correct behavior because `parseInt` also - * doesn't support numeric separators, but it does parse part of the string before the first `_`, - * so the autofix would be invalid: - * - * parseInt("1_1", 2) // === 1 - * 0b1_1 // === 3 - */ - return null; - } - - const tokenBefore = sourceCode.getTokenBefore(node), - tokenAfter = sourceCode.getTokenAfter(node); - let prefix = "", - suffix = ""; - - if ( - tokenBefore && - tokenBefore.range[1] === node.range[0] && - !astUtils.canTokensBeAdjacent( - tokenBefore, - replacement, - ) - ) { - prefix = " "; - } - - if ( - tokenAfter && - node.range[1] === tokenAfter.range[0] && - !astUtils.canTokensBeAdjacent( - replacement, - tokenAfter, - ) - ) { - suffix = " "; - } - - return fixer.replaceText( - node, - `${prefix}${replacement}${suffix}`, - ); - }, - }); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow `parseInt()` and `Number.parseInt()` in favor of binary, octal, and hexadecimal literals", + recommended: false, + url: "https://eslint.org/docs/latest/rules/prefer-numeric-literals" + }, + + schema: [], + + messages: { + useLiteral: "Use {{system}} literals instead of {{functionName}}()." + }, + + fixable: "code" + }, + + create(context) { + const sourceCode = context.sourceCode; + + //---------------------------------------------------------------------- + // Public + //---------------------------------------------------------------------- + + return { + + "CallExpression[arguments.length=2]"(node) { + const [strNode, radixNode] = node.arguments, + str = astUtils.getStaticStringValue(strNode), + radix = radixNode.value; + + if ( + str !== null && + astUtils.isStringLiteral(strNode) && + radixNode.type === "Literal" && + typeof radix === "number" && + radixMap.has(radix) && + isParseInt(node.callee) + ) { + + const { system, literalPrefix } = radixMap.get(radix); + + context.report({ + node, + messageId: "useLiteral", + data: { + system, + functionName: sourceCode.getText(node.callee) + }, + fix(fixer) { + if (sourceCode.getCommentsInside(node).length) { + return null; + } + + const replacement = `${literalPrefix}${str}`; + + if (+replacement !== parseInt(str, radix)) { + + /* + * If the newly-produced literal would be invalid, (e.g. 0b1234), + * or it would yield an incorrect parseInt result for some other reason, don't make a fix. + * + * If `str` had numeric separators, `+replacement` will evaluate to `NaN` because unary `+` + * per the specification doesn't support numeric separators. Thus, the above condition will be `true` + * (`NaN !== anything` is always `true`) regardless of the `parseInt(str, radix)` value. + * Consequently, no autofixes will be made. This is correct behavior because `parseInt` also + * doesn't support numeric separators, but it does parse part of the string before the first `_`, + * so the autofix would be invalid: + * + * parseInt("1_1", 2) // === 1 + * 0b1_1 // === 3 + */ + return null; + } + + const tokenBefore = sourceCode.getTokenBefore(node), + tokenAfter = sourceCode.getTokenAfter(node); + let prefix = "", + suffix = ""; + + if ( + tokenBefore && + tokenBefore.range[1] === node.range[0] && + !astUtils.canTokensBeAdjacent(tokenBefore, replacement) + ) { + prefix = " "; + } + + if ( + tokenAfter && + node.range[1] === tokenAfter.range[0] && + !astUtils.canTokensBeAdjacent(replacement, tokenAfter) + ) { + suffix = " "; + } + + return fixer.replaceText(node, `${prefix}${replacement}${suffix}`); + } + }); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/prefer-object-has-own.js b/node_modules/eslint/lib/rules/prefer-object-has-own.js index 3e0bb5209..97ea64fa6 100644 --- a/node_modules/eslint/lib/rules/prefer-object-has-own.js +++ b/node_modules/eslint/lib/rules/prefer-object-has-own.js @@ -22,127 +22,93 @@ const astUtils = require("./utils/ast-utils"); * @returns {boolean} `true` if `node.object` is `Object`, `Object.prototype`, or `{}` (empty 'ObjectExpression' node). */ function hasLeftHandObject(node) { - /* - * ({}).hasOwnProperty.call(obj, prop) - `true` - * ({ foo }.hasOwnProperty.call(obj, prop)) - `false`, object literal should be empty - */ - if ( - node.object.type === "ObjectExpression" && - node.object.properties.length === 0 - ) { - return true; - } - - const objectNodeToCheck = - node.object.type === "MemberExpression" && - astUtils.getStaticPropertyName(node.object) === "prototype" - ? node.object.object - : node.object; - - if ( - objectNodeToCheck.type === "Identifier" && - objectNodeToCheck.name === "Object" - ) { - return true; - } - - return false; + + /* + * ({}).hasOwnProperty.call(obj, prop) - `true` + * ({ foo }.hasOwnProperty.call(obj, prop)) - `false`, object literal should be empty + */ + if (node.object.type === "ObjectExpression" && node.object.properties.length === 0) { + return true; + } + + const objectNodeToCheck = node.object.type === "MemberExpression" && astUtils.getStaticPropertyName(node.object) === "prototype" ? node.object.object : node.object; + + if (objectNodeToCheck.type === "Identifier" && objectNodeToCheck.name === "Object") { + return true; + } + + return false; } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - docs: { - description: - "Disallow use of `Object.prototype.hasOwnProperty.call()` and prefer use of `Object.hasOwn()`", - recommended: false, - url: "https://eslint.org/docs/latest/rules/prefer-object-has-own", - }, - schema: [], - messages: { - useHasOwn: - "Use 'Object.hasOwn()' instead of 'Object.prototype.hasOwnProperty.call()'.", - }, - fixable: "code", - }, - create(context) { - const sourceCode = context.sourceCode; - - return { - CallExpression(node) { - if ( - !( - node.callee.type === "MemberExpression" && - node.callee.object.type === "MemberExpression" - ) - ) { - return; - } - - const calleePropertyName = astUtils.getStaticPropertyName( - node.callee, - ); - const objectPropertyName = astUtils.getStaticPropertyName( - node.callee.object, - ); - const isObject = hasLeftHandObject(node.callee.object); - - // check `Object` scope - const scope = sourceCode.getScope(node); - const variable = astUtils.getVariableByName(scope, "Object"); - - if ( - calleePropertyName === "call" && - objectPropertyName === "hasOwnProperty" && - isObject && - variable && - variable.scope.type === "global" - ) { - context.report({ - node, - messageId: "useHasOwn", - fix(fixer) { - if ( - sourceCode.getCommentsInside(node.callee) - .length > 0 - ) { - return null; - } - - const tokenJustBeforeNode = - sourceCode.getTokenBefore(node.callee, { - includeComments: true, - }); - - // for https://github.com/eslint/eslint/pull/15346#issuecomment-991417335 - if ( - tokenJustBeforeNode && - tokenJustBeforeNode.range[1] === - node.callee.range[0] && - !astUtils.canTokensBeAdjacent( - tokenJustBeforeNode, - "Object.hasOwn", - ) - ) { - return fixer.replaceText( - node.callee, - " Object.hasOwn", - ); - } - - return fixer.replaceText( - node.callee, - "Object.hasOwn", - ); - }, - }); - } - }, - }; - }, + meta: { + type: "suggestion", + docs: { + description: + "Disallow use of `Object.prototype.hasOwnProperty.call()` and prefer use of `Object.hasOwn()`", + recommended: false, + url: "https://eslint.org/docs/latest/rules/prefer-object-has-own" + }, + schema: [], + messages: { + useHasOwn: "Use 'Object.hasOwn()' instead of 'Object.prototype.hasOwnProperty.call()'." + }, + fixable: "code" + }, + create(context) { + + const sourceCode = context.sourceCode; + + return { + CallExpression(node) { + if (!(node.callee.type === "MemberExpression" && node.callee.object.type === "MemberExpression")) { + return; + } + + const calleePropertyName = astUtils.getStaticPropertyName(node.callee); + const objectPropertyName = astUtils.getStaticPropertyName(node.callee.object); + const isObject = hasLeftHandObject(node.callee.object); + + // check `Object` scope + const scope = sourceCode.getScope(node); + const variable = astUtils.getVariableByName(scope, "Object"); + + if ( + calleePropertyName === "call" && + objectPropertyName === "hasOwnProperty" && + isObject && + variable && variable.scope.type === "global" + ) { + context.report({ + node, + messageId: "useHasOwn", + fix(fixer) { + + if (sourceCode.getCommentsInside(node.callee).length > 0) { + return null; + } + + const tokenJustBeforeNode = sourceCode.getTokenBefore(node.callee, { includeComments: true }); + + // for https://github.com/eslint/eslint/pull/15346#issuecomment-991417335 + if ( + tokenJustBeforeNode && + tokenJustBeforeNode.range[1] === node.callee.range[0] && + !astUtils.canTokensBeAdjacent(tokenJustBeforeNode, "Object.hasOwn") + ) { + return fixer.replaceText(node.callee, " Object.hasOwn"); + } + + return fixer.replaceText(node.callee, "Object.hasOwn"); + } + }); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/prefer-object-spread.js b/node_modules/eslint/lib/rules/prefer-object-spread.js index 6827e2c25..60b0c3175 100644 --- a/node_modules/eslint/lib/rules/prefer-object-spread.js +++ b/node_modules/eslint/lib/rules/prefer-object-spread.js @@ -1,5 +1,5 @@ /** - * @fileoverview Rule to disallow using `Object.assign` with an object literal as the first argument and prefer the use of object spread instead + * @fileoverview Prefers object spread property over Object.assign * @author Sharmila Jesupaul */ @@ -7,10 +7,10 @@ const { CALL, ReferenceTracker } = require("@eslint-community/eslint-utils"); const { - isCommaToken, - isOpeningParenToken, - isClosingParenToken, - isParenthesised, + isCommaToken, + isOpeningParenToken, + isClosingParenToken, + isParenthesised } = require("./utils/ast-utils"); const ANY_SPACE = /\s/u; @@ -21,7 +21,7 @@ const ANY_SPACE = /\s/u; * @returns {boolean} - Returns true if the Object.assign call has array spread */ function hasArraySpread(node) { - return node.arguments.some(arg => arg.type === "SpreadElement"); + return node.arguments.some(arg => arg.type === "SpreadElement"); } /** @@ -30,9 +30,8 @@ function hasArraySpread(node) { * @returns {boolean} `true` if the node is a getter or a setter. */ function isAccessorProperty(node) { - return ( - node.type === "Property" && (node.kind === "get" || node.kind === "set") - ); + return node.type === "Property" && + (node.kind === "get" || node.kind === "set"); } /** @@ -41,7 +40,7 @@ function isAccessorProperty(node) { * @returns {boolean} `true` if the node has at least one getter/setter. */ function hasAccessors(node) { - return node.properties.some(isAccessorProperty); + return node.properties.some(isAccessorProperty); } /** @@ -50,9 +49,9 @@ function hasAccessors(node) { * @returns {boolean} `true` if the node has at least one argument that is an object expression with at least one getter/setter. */ function hasArgumentsWithAccessors(node) { - return node.arguments - .filter(arg => arg.type === "ObjectExpression") - .some(hasAccessors); + return node.arguments + .filter(arg => arg.type === "ObjectExpression") + .some(hasAccessors); } /** @@ -63,20 +62,20 @@ function hasArgumentsWithAccessors(node) { * @returns {boolean} - Returns true if the node needs parentheses */ function needsParens(node, sourceCode) { - const parent = node.parent; - - switch (parent.type) { - case "VariableDeclarator": - case "ArrayExpression": - case "ReturnStatement": - case "CallExpression": - case "Property": - return false; - case "AssignmentExpression": - return parent.left === node && !isParenthesised(sourceCode, node); - default: - return !isParenthesised(sourceCode, node); - } + const parent = node.parent; + + switch (parent.type) { + case "VariableDeclarator": + case "ArrayExpression": + case "ReturnStatement": + case "CallExpression": + case "Property": + return false; + case "AssignmentExpression": + return parent.left === node && !isParenthesised(sourceCode, node); + default: + return !isParenthesised(sourceCode, node); + } } /** @@ -86,14 +85,14 @@ function needsParens(node, sourceCode) { * @returns {boolean} True if the node needs parentheses */ function argNeedsParens(node, sourceCode) { - switch (node.type) { - case "AssignmentExpression": - case "ArrowFunctionExpression": - case "ConditionalExpression": - return !isParenthesised(sourceCode, node); - default: - return false; - } + switch (node.type) { + case "AssignmentExpression": + case "ArrowFunctionExpression": + case "ConditionalExpression": + return !isParenthesised(sourceCode, node); + default: + return false; + } } /** @@ -105,27 +104,24 @@ function argNeedsParens(node, sourceCode) { * @returns {Token[]} The parenthesis tokens of the node. This is sorted by the location. */ function getParenTokens(node, leftArgumentListParen, sourceCode) { - const parens = [ - sourceCode.getFirstToken(node), - sourceCode.getLastToken(node), - ]; - let leftNext = sourceCode.getTokenBefore(node); - let rightNext = sourceCode.getTokenAfter(node); - - // Note: don't include the parens of the argument list. - while ( - leftNext && - rightNext && - leftNext.range[0] > leftArgumentListParen.range[0] && - isOpeningParenToken(leftNext) && - isClosingParenToken(rightNext) - ) { - parens.push(leftNext, rightNext); - leftNext = sourceCode.getTokenBefore(leftNext); - rightNext = sourceCode.getTokenAfter(rightNext); - } - - return parens.sort((a, b) => a.range[0] - b.range[0]); + const parens = [sourceCode.getFirstToken(node), sourceCode.getLastToken(node)]; + let leftNext = sourceCode.getTokenBefore(node); + let rightNext = sourceCode.getTokenAfter(node); + + // Note: don't include the parens of the argument list. + while ( + leftNext && + rightNext && + leftNext.range[0] > leftArgumentListParen.range[0] && + isOpeningParenToken(leftNext) && + isClosingParenToken(rightNext) + ) { + parens.push(leftNext, rightNext); + leftNext = sourceCode.getTokenBefore(leftNext); + rightNext = sourceCode.getTokenAfter(rightNext); + } + + return parens.sort((a, b) => a.range[0] - b.range[0]); } /** @@ -135,26 +131,24 @@ function getParenTokens(node, leftArgumentListParen, sourceCode) { * @returns {number} The end of the range of the token and around whitespaces. */ function getStartWithSpaces(token, sourceCode) { - const text = sourceCode.text; - let start = token.range[0]; - - // If the previous token is a line comment then skip this step to avoid commenting this token out. - { - const prevToken = sourceCode.getTokenBefore(token, { - includeComments: true, - }); - - if (prevToken && prevToken.type === "Line") { - return start; - } - } - - // Detect spaces before the token. - while (ANY_SPACE.test(text[start - 1] || "")) { - start -= 1; - } - - return start; + const text = sourceCode.text; + let start = token.range[0]; + + // If the previous token is a line comment then skip this step to avoid commenting this token out. + { + const prevToken = sourceCode.getTokenBefore(token, { includeComments: true }); + + if (prevToken && prevToken.type === "Line") { + return start; + } + } + + // Detect spaces before the token. + while (ANY_SPACE.test(text[start - 1] || "")) { + start -= 1; + } + + return start; } /** @@ -164,15 +158,15 @@ function getStartWithSpaces(token, sourceCode) { * @returns {number} The start of the range of the token and around whitespaces. */ function getEndWithSpaces(token, sourceCode) { - const text = sourceCode.text; - let end = token.range[1]; + const text = sourceCode.text; + let end = token.range[1]; - // Detect spaces after the token. - while (ANY_SPACE.test(text[end] || "")) { - end += 1; - } + // Detect spaces after the token. + while (ANY_SPACE.test(text[end] || "")) { + end += 1; + } - return end; + return end; } /** @@ -182,138 +176,123 @@ function getEndWithSpaces(token, sourceCode) { * @returns {Function} autofixer - replaces the Object.assign with a spread object. */ function defineFixer(node, sourceCode) { - return function* (fixer) { - const leftParen = sourceCode.getTokenAfter( - node.callee, - isOpeningParenToken, - ); - const rightParen = sourceCode.getLastToken(node); - - // Remove everything before the opening paren: callee `Object.assign`, type arguments, and whitespace between the callee and the paren. - yield fixer.removeRange([node.range[0], leftParen.range[0]]); - - // Replace the parens of argument list to braces. - if (needsParens(node, sourceCode)) { - yield fixer.replaceText(leftParen, "({"); - yield fixer.replaceText(rightParen, "})"); - } else { - yield fixer.replaceText(leftParen, "{"); - yield fixer.replaceText(rightParen, "}"); - } - - // Process arguments. - for (const argNode of node.arguments) { - const innerParens = getParenTokens(argNode, leftParen, sourceCode); - const left = innerParens.shift(); - const right = innerParens.pop(); - - if (argNode.type === "ObjectExpression") { - const maybeTrailingComma = sourceCode.getLastToken(argNode, 1); - const maybeArgumentComma = sourceCode.getTokenAfter(right); - - /* - * Make bare this object literal. - * And remove spaces inside of the braces for better formatting. - */ - for (const innerParen of innerParens) { - yield fixer.remove(innerParen); - } - const leftRange = [ - left.range[0], - getEndWithSpaces(left, sourceCode), - ]; - const rightRange = [ - Math.max( - getStartWithSpaces(right, sourceCode), - leftRange[1], - ), // Ensure ranges don't overlap - right.range[1], - ]; - - yield fixer.removeRange(leftRange); - yield fixer.removeRange(rightRange); - - // Remove the comma of this argument if it's duplication. - if ( - (argNode.properties.length === 0 || - isCommaToken(maybeTrailingComma)) && - isCommaToken(maybeArgumentComma) - ) { - yield fixer.remove(maybeArgumentComma); - } - } else { - // Make spread. - if (argNeedsParens(argNode, sourceCode)) { - yield fixer.insertTextBefore(left, "...("); - yield fixer.insertTextAfter(right, ")"); - } else { - yield fixer.insertTextBefore(left, "..."); - } - } - } - }; + return function *(fixer) { + const leftParen = sourceCode.getTokenAfter(node.callee, isOpeningParenToken); + const rightParen = sourceCode.getLastToken(node); + + // Remove everything before the opening paren: callee `Object.assign`, type arguments, and whitespace between the callee and the paren. + yield fixer.removeRange([node.range[0], leftParen.range[0]]); + + // Replace the parens of argument list to braces. + if (needsParens(node, sourceCode)) { + yield fixer.replaceText(leftParen, "({"); + yield fixer.replaceText(rightParen, "})"); + } else { + yield fixer.replaceText(leftParen, "{"); + yield fixer.replaceText(rightParen, "}"); + } + + // Process arguments. + for (const argNode of node.arguments) { + const innerParens = getParenTokens(argNode, leftParen, sourceCode); + const left = innerParens.shift(); + const right = innerParens.pop(); + + if (argNode.type === "ObjectExpression") { + const maybeTrailingComma = sourceCode.getLastToken(argNode, 1); + const maybeArgumentComma = sourceCode.getTokenAfter(right); + + /* + * Make bare this object literal. + * And remove spaces inside of the braces for better formatting. + */ + for (const innerParen of innerParens) { + yield fixer.remove(innerParen); + } + const leftRange = [left.range[0], getEndWithSpaces(left, sourceCode)]; + const rightRange = [ + Math.max(getStartWithSpaces(right, sourceCode), leftRange[1]), // Ensure ranges don't overlap + right.range[1] + ]; + + yield fixer.removeRange(leftRange); + yield fixer.removeRange(rightRange); + + // Remove the comma of this argument if it's duplication. + if ( + (argNode.properties.length === 0 || isCommaToken(maybeTrailingComma)) && + isCommaToken(maybeArgumentComma) + ) { + yield fixer.remove(maybeArgumentComma); + } + } else { + + // Make spread. + if (argNeedsParens(argNode, sourceCode)) { + yield fixer.insertTextBefore(left, "...("); + yield fixer.insertTextAfter(right, ")"); + } else { + yield fixer.insertTextBefore(left, "..."); + } + } + } + }; } -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: - "Disallow using `Object.assign` with an object literal as the first argument and prefer the use of object spread instead", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/prefer-object-spread", - }, - - schema: [], - fixable: "code", - - messages: { - useSpreadMessage: - "Use an object spread instead of `Object.assign` eg: `{ ...foo }`.", - useLiteralMessage: - "Use an object literal instead of `Object.assign`. eg: `{ foo: bar }`.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - return { - Program(node) { - const scope = sourceCode.getScope(node); - const tracker = new ReferenceTracker(scope); - const trackMap = { - Object: { - assign: { [CALL]: true }, - }, - }; - - // Iterate all calls of `Object.assign` (only of the global variable `Object`). - for (const { node: refNode } of tracker.iterateGlobalReferences( - trackMap, - )) { - if ( - refNode.arguments.length >= 1 && - refNode.arguments[0].type === "ObjectExpression" && - !hasArraySpread(refNode) && - !( - refNode.arguments.length > 1 && - hasArgumentsWithAccessors(refNode) - ) - ) { - const messageId = - refNode.arguments.length === 1 - ? "useLiteralMessage" - : "useSpreadMessage"; - const fix = defineFixer(refNode, sourceCode); - - context.report({ node: refNode, messageId, fix }); - } - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: + "Disallow using Object.assign with an object literal as the first argument and prefer the use of object spread instead", + recommended: false, + url: "https://eslint.org/docs/latest/rules/prefer-object-spread" + }, + + schema: [], + fixable: "code", + + messages: { + useSpreadMessage: "Use an object spread instead of `Object.assign` eg: `{ ...foo }`.", + useLiteralMessage: "Use an object literal instead of `Object.assign`. eg: `{ foo: bar }`." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + + return { + Program(node) { + const scope = sourceCode.getScope(node); + const tracker = new ReferenceTracker(scope); + const trackMap = { + Object: { + assign: { [CALL]: true } + } + }; + + // Iterate all calls of `Object.assign` (only of the global variable `Object`). + for (const { node: refNode } of tracker.iterateGlobalReferences(trackMap)) { + if ( + refNode.arguments.length >= 1 && + refNode.arguments[0].type === "ObjectExpression" && + !hasArraySpread(refNode) && + !( + refNode.arguments.length > 1 && + hasArgumentsWithAccessors(refNode) + ) + ) { + const messageId = refNode.arguments.length === 1 + ? "useLiteralMessage" + : "useSpreadMessage"; + const fix = defineFixer(refNode, sourceCode); + + context.report({ node: refNode, messageId, fix }); + } + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/prefer-promise-reject-errors.js b/node_modules/eslint/lib/rules/prefer-promise-reject-errors.js index 7043f296d..e990265e9 100644 --- a/node_modules/eslint/lib/rules/prefer-promise-reject-errors.js +++ b/node_modules/eslint/lib/rules/prefer-promise-reject-errors.js @@ -10,145 +10,123 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - defaultOptions: [ - { - allowEmptyReject: false, - }, - ], - - docs: { - description: - "Require using Error objects as Promise rejection reasons", - recommended: false, - url: "https://eslint.org/docs/latest/rules/prefer-promise-reject-errors", - }, - - fixable: null, - - schema: [ - { - type: "object", - properties: { - allowEmptyReject: { type: "boolean" }, - }, - additionalProperties: false, - }, - ], - - messages: { - rejectAnError: - "Expected the Promise rejection reason to be an Error.", - }, - }, - - create(context) { - const [{ allowEmptyReject }] = context.options; - const sourceCode = context.sourceCode; - - //---------------------------------------------------------------------- - // Helpers - //---------------------------------------------------------------------- - - /** - * Checks the argument of a reject() or Promise.reject() CallExpression, and reports it if it can't be an Error - * @param {ASTNode} callExpression A CallExpression node which is used to reject a Promise - * @returns {void} - */ - function checkRejectCall(callExpression) { - if (!callExpression.arguments.length && allowEmptyReject) { - return; - } - if ( - !callExpression.arguments.length || - !astUtils.couldBeError(callExpression.arguments[0]) || - (callExpression.arguments[0].type === "Identifier" && - callExpression.arguments[0].name === "undefined") - ) { - context.report({ - node: callExpression, - messageId: "rejectAnError", - }); - } - } - - /** - * Determines whether a function call is a Promise.reject() call - * @param {ASTNode} node A CallExpression node - * @returns {boolean} `true` if the call is a Promise.reject() call - */ - function isPromiseRejectCall(node) { - return astUtils.isSpecificMemberAccess( - node.callee, - "Promise", - "reject", - ); - } - - //---------------------------------------------------------------------- - // Public - //---------------------------------------------------------------------- - - return { - // Check `Promise.reject(value)` calls. - CallExpression(node) { - if (isPromiseRejectCall(node)) { - checkRejectCall(node); - } - }, - - /* - * Check for `new Promise((resolve, reject) => {})`, and check for reject() calls. - * This function is run on "NewExpression:exit" instead of "NewExpression" to ensure that - * the nodes in the expression already have the `parent` property. - */ - "NewExpression:exit"(node) { - if ( - node.callee.type === "Identifier" && - node.callee.name === "Promise" && - node.arguments.length && - astUtils.isFunction(node.arguments[0]) && - node.arguments[0].params.length > 1 && - node.arguments[0].params[1].type === "Identifier" - ) { - sourceCode - .getDeclaredVariables(node.arguments[0]) - - /* - * Find the first variable that matches the second parameter's name. - * If the first parameter has the same name as the second parameter, then the variable will actually - * be "declared" when the first parameter is evaluated, but then it will be immediately overwritten - * by the second parameter. It's not possible for an expression with the variable to be evaluated before - * the variable is overwritten, because functions with duplicate parameters cannot have destructuring or - * default assignments in their parameter lists. Therefore, it's not necessary to explicitly account for - * this case. - */ - .find( - variable => - variable.name === - node.arguments[0].params[1].name, - ) - - // Get the references to that variable. - .references // Only check the references that read the parameter's value. - .filter(ref => ref.isRead()) - - // Only check the references that are used as the callee in a function call, e.g. `reject(foo)`. - .filter( - ref => - ref.identifier.parent.type === - "CallExpression" && - ref.identifier === ref.identifier.parent.callee, - ) - - // Check the argument of the function call to determine whether it's an Error. - .forEach(ref => checkRejectCall(ref.identifier.parent)); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Require using Error objects as Promise rejection reasons", + recommended: false, + url: "https://eslint.org/docs/latest/rules/prefer-promise-reject-errors" + }, + + fixable: null, + + schema: [ + { + type: "object", + properties: { + allowEmptyReject: { type: "boolean", default: false } + }, + additionalProperties: false + } + ], + + messages: { + rejectAnError: "Expected the Promise rejection reason to be an Error." + } + }, + + create(context) { + + const ALLOW_EMPTY_REJECT = context.options.length && context.options[0].allowEmptyReject; + const sourceCode = context.sourceCode; + + //---------------------------------------------------------------------- + // Helpers + //---------------------------------------------------------------------- + + /** + * Checks the argument of a reject() or Promise.reject() CallExpression, and reports it if it can't be an Error + * @param {ASTNode} callExpression A CallExpression node which is used to reject a Promise + * @returns {void} + */ + function checkRejectCall(callExpression) { + if (!callExpression.arguments.length && ALLOW_EMPTY_REJECT) { + return; + } + if ( + !callExpression.arguments.length || + !astUtils.couldBeError(callExpression.arguments[0]) || + callExpression.arguments[0].type === "Identifier" && callExpression.arguments[0].name === "undefined" + ) { + context.report({ + node: callExpression, + messageId: "rejectAnError" + }); + } + } + + /** + * Determines whether a function call is a Promise.reject() call + * @param {ASTNode} node A CallExpression node + * @returns {boolean} `true` if the call is a Promise.reject() call + */ + function isPromiseRejectCall(node) { + return astUtils.isSpecificMemberAccess(node.callee, "Promise", "reject"); + } + + //---------------------------------------------------------------------- + // Public + //---------------------------------------------------------------------- + + return { + + // Check `Promise.reject(value)` calls. + CallExpression(node) { + if (isPromiseRejectCall(node)) { + checkRejectCall(node); + } + }, + + /* + * Check for `new Promise((resolve, reject) => {})`, and check for reject() calls. + * This function is run on "NewExpression:exit" instead of "NewExpression" to ensure that + * the nodes in the expression already have the `parent` property. + */ + "NewExpression:exit"(node) { + if ( + node.callee.type === "Identifier" && node.callee.name === "Promise" && + node.arguments.length && astUtils.isFunction(node.arguments[0]) && + node.arguments[0].params.length > 1 && node.arguments[0].params[1].type === "Identifier" + ) { + sourceCode.getDeclaredVariables(node.arguments[0]) + + /* + * Find the first variable that matches the second parameter's name. + * If the first parameter has the same name as the second parameter, then the variable will actually + * be "declared" when the first parameter is evaluated, but then it will be immediately overwritten + * by the second parameter. It's not possible for an expression with the variable to be evaluated before + * the variable is overwritten, because functions with duplicate parameters cannot have destructuring or + * default assignments in their parameter lists. Therefore, it's not necessary to explicitly account for + * this case. + */ + .find(variable => variable.name === node.arguments[0].params[1].name) + + // Get the references to that variable. + .references + + // Only check the references that read the parameter's value. + .filter(ref => ref.isRead()) + + // Only check the references that are used as the callee in a function call, e.g. `reject(foo)`. + .filter(ref => ref.identifier.parent.type === "CallExpression" && ref.identifier === ref.identifier.parent.callee) + + // Check the argument of the function call to determine whether it's an Error. + .forEach(ref => checkRejectCall(ref.identifier.parent)); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/prefer-reflect.js b/node_modules/eslint/lib/rules/prefer-reflect.js index 4eae98afb..d579b486b 100644 --- a/node_modules/eslint/lib/rules/prefer-reflect.js +++ b/node_modules/eslint/lib/rules/prefer-reflect.js @@ -9,142 +9,119 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", + meta: { + type: "suggestion", - docs: { - description: "Require `Reflect` methods where applicable", - recommended: false, - url: "https://eslint.org/docs/latest/rules/prefer-reflect", - }, + docs: { + description: "Require `Reflect` methods where applicable", + recommended: false, + url: "https://eslint.org/docs/latest/rules/prefer-reflect" + }, - deprecated: { - message: "The original intention of this rule was misguided.", - deprecatedSince: "3.9.0", - availableUntil: null, - replacedBy: [], - }, + deprecated: true, - schema: [ - { - type: "object", - properties: { - exceptions: { - type: "array", - items: { - enum: [ - "apply", - "call", - "delete", - "defineProperty", - "getOwnPropertyDescriptor", - "getPrototypeOf", - "setPrototypeOf", - "isExtensible", - "getOwnPropertyNames", - "preventExtensions", - ], - }, - uniqueItems: true, - }, - }, - additionalProperties: false, - }, - ], + replacedBy: [], - messages: { - preferReflect: - "Avoid using {{existing}}, instead use {{substitute}}.", - }, - }, + schema: [ + { + type: "object", + properties: { + exceptions: { + type: "array", + items: { + enum: [ + "apply", + "call", + "delete", + "defineProperty", + "getOwnPropertyDescriptor", + "getPrototypeOf", + "setPrototypeOf", + "isExtensible", + "getOwnPropertyNames", + "preventExtensions" + ] + }, + uniqueItems: true + } + }, + additionalProperties: false + } + ], - create(context) { - const existingNames = { - apply: "Function.prototype.apply", - call: "Function.prototype.call", - defineProperty: "Object.defineProperty", - getOwnPropertyDescriptor: "Object.getOwnPropertyDescriptor", - getPrototypeOf: "Object.getPrototypeOf", - setPrototypeOf: "Object.setPrototypeOf", - isExtensible: "Object.isExtensible", - getOwnPropertyNames: "Object.getOwnPropertyNames", - preventExtensions: "Object.preventExtensions", - }; + messages: { + preferReflect: "Avoid using {{existing}}, instead use {{substitute}}." + } + }, - const reflectSubstitutes = { - apply: "Reflect.apply", - call: "Reflect.apply", - defineProperty: "Reflect.defineProperty", - getOwnPropertyDescriptor: "Reflect.getOwnPropertyDescriptor", - getPrototypeOf: "Reflect.getPrototypeOf", - setPrototypeOf: "Reflect.setPrototypeOf", - isExtensible: "Reflect.isExtensible", - getOwnPropertyNames: "Reflect.getOwnPropertyNames", - preventExtensions: "Reflect.preventExtensions", - }; + create(context) { + const existingNames = { + apply: "Function.prototype.apply", + call: "Function.prototype.call", + defineProperty: "Object.defineProperty", + getOwnPropertyDescriptor: "Object.getOwnPropertyDescriptor", + getPrototypeOf: "Object.getPrototypeOf", + setPrototypeOf: "Object.setPrototypeOf", + isExtensible: "Object.isExtensible", + getOwnPropertyNames: "Object.getOwnPropertyNames", + preventExtensions: "Object.preventExtensions" + }; - const exceptions = (context.options[0] || {}).exceptions || []; + const reflectSubstitutes = { + apply: "Reflect.apply", + call: "Reflect.apply", + defineProperty: "Reflect.defineProperty", + getOwnPropertyDescriptor: "Reflect.getOwnPropertyDescriptor", + getPrototypeOf: "Reflect.getPrototypeOf", + setPrototypeOf: "Reflect.setPrototypeOf", + isExtensible: "Reflect.isExtensible", + getOwnPropertyNames: "Reflect.getOwnPropertyNames", + preventExtensions: "Reflect.preventExtensions" + }; - /** - * Reports the Reflect violation based on the `existing` and `substitute` - * @param {Object} node The node that violates the rule. - * @param {string} existing The existing method name that has been used. - * @param {string} substitute The Reflect substitute that should be used. - * @returns {void} - */ - function report(node, existing, substitute) { - context.report({ - node, - messageId: "preferReflect", - data: { - existing, - substitute, - }, - }); - } + const exceptions = (context.options[0] || {}).exceptions || []; - return { - CallExpression(node) { - const methodName = (node.callee.property || {}).name; - const isReflectCall = - (node.callee.object || {}).name === "Reflect"; - const hasReflectSubstitute = Object.hasOwn( - reflectSubstitutes, - methodName, - ); - const userConfiguredException = exceptions.includes(methodName); + /** + * Reports the Reflect violation based on the `existing` and `substitute` + * @param {Object} node The node that violates the rule. + * @param {string} existing The existing method name that has been used. + * @param {string} substitute The Reflect substitute that should be used. + * @returns {void} + */ + function report(node, existing, substitute) { + context.report({ + node, + messageId: "preferReflect", + data: { + existing, + substitute + } + }); + } - if ( - hasReflectSubstitute && - !isReflectCall && - !userConfiguredException - ) { - report( - node, - existingNames[methodName], - reflectSubstitutes[methodName], - ); - } - }, - UnaryExpression(node) { - const isDeleteOperator = node.operator === "delete"; - const targetsIdentifier = node.argument.type === "Identifier"; - const userConfiguredException = exceptions.includes("delete"); + return { + CallExpression(node) { + const methodName = (node.callee.property || {}).name; + const isReflectCall = (node.callee.object || {}).name === "Reflect"; + const hasReflectSubstitute = Object.prototype.hasOwnProperty.call(reflectSubstitutes, methodName); + const userConfiguredException = exceptions.includes(methodName); - if ( - isDeleteOperator && - !targetsIdentifier && - !userConfiguredException - ) { - report( - node, - "the delete keyword", - "Reflect.deleteProperty", - ); - } - }, - }; - }, + if (hasReflectSubstitute && !isReflectCall && !userConfiguredException) { + report(node, existingNames[methodName], reflectSubstitutes[methodName]); + } + }, + UnaryExpression(node) { + const isDeleteOperator = node.operator === "delete"; + const targetsIdentifier = node.argument.type === "Identifier"; + const userConfiguredException = exceptions.includes("delete"); + + if (isDeleteOperator && !targetsIdentifier && !userConfiguredException) { + report(node, "the delete keyword", "Reflect.deleteProperty"); + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/prefer-regex-literals.js b/node_modules/eslint/lib/rules/prefer-regex-literals.js index f1414dfa9..ffaaeac3f 100644 --- a/node_modules/eslint/lib/rules/prefer-regex-literals.js +++ b/node_modules/eslint/lib/rules/prefer-regex-literals.js @@ -10,16 +10,8 @@ //------------------------------------------------------------------------------ const astUtils = require("./utils/ast-utils"); -const { - CALL, - CONSTRUCT, - ReferenceTracker, -} = require("@eslint-community/eslint-utils"); -const { - RegExpValidator, - visitRegExpAST, - RegExpParser, -} = require("@eslint-community/regexpp"); +const { CALL, CONSTRUCT, ReferenceTracker, findVariable } = require("@eslint-community/eslint-utils"); +const { RegExpValidator, visitRegExpAST, RegExpParser } = require("@eslint-community/regexpp"); const { canTokensBeAdjacent } = require("./utils/ast-utils"); const { REGEXPP_LATEST_ECMA_VERSION } = require("./utils/regular-expressions"); @@ -33,7 +25,7 @@ const { REGEXPP_LATEST_ECMA_VERSION } = require("./utils/regular-expressions"); * @returns {boolean} True if the node is a string literal. */ function isStringLiteral(node) { - return node.type === "Literal" && typeof node.value === "string"; + return node.type === "Literal" && typeof node.value === "string"; } /** @@ -42,564 +34,474 @@ function isStringLiteral(node) { * @returns {boolean} True if the node is a regex literal. */ function isRegexLiteral(node) { - return node.type === "Literal" && Object.hasOwn(node, "regex"); + return node.type === "Literal" && Object.prototype.hasOwnProperty.call(node, "regex"); } const validPrecedingTokens = new Set([ - "(", - ";", - "[", - ",", - "=", - "+", - "*", - "-", - "?", - "~", - "%", - "**", - "!", - "typeof", - "instanceof", - "&&", - "||", - "??", - "return", - "...", - "delete", - "void", - "in", - "<", - ">", - "<=", - ">=", - "==", - "===", - "!=", - "!==", - "<<", - ">>", - ">>>", - "&", - "|", - "^", - ":", - "{", - "=>", - "*=", - "<<=", - ">>=", - ">>>=", - "^=", - "|=", - "&=", - "??=", - "||=", - "&&=", - "**=", - "+=", - "-=", - "/=", - "%=", - "/", - "do", - "break", - "continue", - "debugger", - "case", - "throw", + "(", + ";", + "[", + ",", + "=", + "+", + "*", + "-", + "?", + "~", + "%", + "**", + "!", + "typeof", + "instanceof", + "&&", + "||", + "??", + "return", + "...", + "delete", + "void", + "in", + "<", + ">", + "<=", + ">=", + "==", + "===", + "!=", + "!==", + "<<", + ">>", + ">>>", + "&", + "|", + "^", + ":", + "{", + "=>", + "*=", + "<<=", + ">>=", + ">>>=", + "^=", + "|=", + "&=", + "??=", + "||=", + "&&=", + "**=", + "+=", + "-=", + "/=", + "%=", + "/", + "do", + "break", + "continue", + "debugger", + "case", + "throw" ]); + //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - defaultOptions: [ - { - disallowRedundantWrapping: false, - }, - ], - - docs: { - description: - "Disallow use of the `RegExp` constructor in favor of regular expression literals", - recommended: false, - url: "https://eslint.org/docs/latest/rules/prefer-regex-literals", - }, - - hasSuggestions: true, - - schema: [ - { - type: "object", - properties: { - disallowRedundantWrapping: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - - messages: { - unexpectedRegExp: - "Use a regular expression literal instead of the 'RegExp' constructor.", - replaceWithLiteral: - "Replace with an equivalent regular expression literal.", - replaceWithLiteralAndFlags: - "Replace with an equivalent regular expression literal with flags '{{ flags }}'.", - replaceWithIntendedLiteralAndFlags: - "Replace with a regular expression literal with flags '{{ flags }}'.", - unexpectedRedundantRegExp: - "Regular expression literal is unnecessarily wrapped within a 'RegExp' constructor.", - unexpectedRedundantRegExpWithFlags: - "Use regular expression literal with flags instead of the 'RegExp' constructor.", - }, - }, - - create(context) { - const [{ disallowRedundantWrapping }] = context.options; - const sourceCode = context.sourceCode; - - /** - * Determines whether the given node is a String.raw`` tagged template expression - * with a static template literal. - * @param {ASTNode} node Node to check. - * @returns {boolean} True if the node is String.raw`` with a static template. - */ - function isStringRawTaggedStaticTemplateLiteral(node) { - return ( - node.type === "TaggedTemplateExpression" && - astUtils.isSpecificMemberAccess(node.tag, "String", "raw") && - sourceCode.isGlobalReference( - astUtils.skipChainExpression(node.tag).object, - ) && - astUtils.isStaticTemplateLiteral(node.quasi) - ); - } - - /** - * Gets the value of a string - * @param {ASTNode} node The node to get the string of. - * @returns {string|null} The value of the node. - */ - function getStringValue(node) { - if (isStringLiteral(node)) { - return node.value; - } - - if (astUtils.isStaticTemplateLiteral(node)) { - return node.quasis[0].value.cooked; - } - - if (isStringRawTaggedStaticTemplateLiteral(node)) { - return node.quasi.quasis[0].value.raw; - } - - return null; - } - - /** - * Determines whether the given node is considered to be a static string by the logic of this rule. - * @param {ASTNode} node Node to check. - * @returns {boolean} True if the node is a static string. - */ - function isStaticString(node) { - return ( - isStringLiteral(node) || - astUtils.isStaticTemplateLiteral(node) || - isStringRawTaggedStaticTemplateLiteral(node) - ); - } - - /** - * Determines whether the relevant arguments of the given are all static string literals. - * @param {ASTNode} node Node to check. - * @returns {boolean} True if all arguments are static strings. - */ - function hasOnlyStaticStringArguments(node) { - const args = node.arguments; - - if ( - (args.length === 1 || args.length === 2) && - args.every(isStaticString) - ) { - return true; - } - - return false; - } - - /** - * Determines whether the arguments of the given node indicate that a regex literal is unnecessarily wrapped. - * @param {ASTNode} node Node to check. - * @returns {boolean} True if the node already contains a regex literal argument. - */ - function isUnnecessarilyWrappedRegexLiteral(node) { - const args = node.arguments; - - if (args.length === 1 && isRegexLiteral(args[0])) { - return true; - } - - if ( - args.length === 2 && - isRegexLiteral(args[0]) && - isStaticString(args[1]) - ) { - return true; - } - - return false; - } - - /** - * Returns a ecmaVersion compatible for regexpp. - * @param {number} ecmaVersion The ecmaVersion to convert. - * @returns {import("@eslint-community/regexpp/ecma-versions").EcmaVersion} The resulting ecmaVersion compatible for regexpp. - */ - function getRegexppEcmaVersion(ecmaVersion) { - if (ecmaVersion <= 5) { - return 5; - } - return Math.min(ecmaVersion, REGEXPP_LATEST_ECMA_VERSION); - } - - const regexppEcmaVersion = getRegexppEcmaVersion( - context.languageOptions.ecmaVersion, - ); - - /** - * Makes a character escaped or else returns null. - * @param {string} character The character to escape. - * @returns {string} The resulting escaped character. - */ - function resolveEscapes(character) { - switch (character) { - case "\n": - case "\\\n": - return "\\n"; - - case "\r": - case "\\\r": - return "\\r"; - - case "\t": - case "\\\t": - return "\\t"; - - case "\v": - case "\\\v": - return "\\v"; - - case "\f": - case "\\\f": - return "\\f"; - - case "/": - return "\\/"; - - default: - return null; - } - } - - /** - * Checks whether the given regex and flags are valid for the ecma version or not. - * @param {string} pattern The regex pattern to check. - * @param {string | undefined} flags The regex flags to check. - * @returns {boolean} True if the given regex pattern and flags are valid for the ecma version. - */ - function isValidRegexForEcmaVersion(pattern, flags) { - const validator = new RegExpValidator({ - ecmaVersion: regexppEcmaVersion, - }); - - try { - validator.validatePattern(pattern, 0, pattern.length, { - unicode: flags ? flags.includes("u") : false, - unicodeSets: flags ? flags.includes("v") : false, - }); - if (flags) { - validator.validateFlags(flags); - } - return true; - } catch { - return false; - } - } - - /** - * Checks whether two given regex flags contain the same flags or not. - * @param {string} flagsA The regex flags. - * @param {string} flagsB The regex flags. - * @returns {boolean} True if two regex flags contain same flags. - */ - function areFlagsEqual(flagsA, flagsB) { - return [...flagsA].sort().join("") === [...flagsB].sort().join(""); - } - - /** - * Merges two regex flags. - * @param {string} flagsA The regex flags. - * @param {string} flagsB The regex flags. - * @returns {string} The merged regex flags. - */ - function mergeRegexFlags(flagsA, flagsB) { - const flagsSet = new Set([...flagsA, ...flagsB]); - - return [...flagsSet].join(""); - } - - /** - * Checks whether a give node can be fixed to the given regex pattern and flags. - * @param {ASTNode} node The node to check. - * @param {string} pattern The regex pattern to check. - * @param {string} flags The regex flags - * @returns {boolean} True if a node can be fixed to the given regex pattern and flags. - */ - function canFixTo(node, pattern, flags) { - const tokenBefore = sourceCode.getTokenBefore(node); - - return ( - sourceCode.getCommentsInside(node).length === 0 && - (!tokenBefore || validPrecedingTokens.has(tokenBefore.value)) && - isValidRegexForEcmaVersion(pattern, flags) - ); - } - - /** - * Returns a safe output code considering the before and after tokens. - * @param {ASTNode} node The regex node. - * @param {string} newRegExpValue The new regex expression value. - * @returns {string} The output code. - */ - function getSafeOutput(node, newRegExpValue) { - const tokenBefore = sourceCode.getTokenBefore(node); - const tokenAfter = sourceCode.getTokenAfter(node); - - return ( - (tokenBefore && - !canTokensBeAdjacent(tokenBefore, newRegExpValue) && - tokenBefore.range[1] === node.range[0] - ? " " - : "") + - newRegExpValue + - (tokenAfter && - !canTokensBeAdjacent(newRegExpValue, tokenAfter) && - node.range[1] === tokenAfter.range[0] - ? " " - : "") - ); - } - - return { - Program(node) { - const scope = sourceCode.getScope(node); - const tracker = new ReferenceTracker(scope); - const traceMap = { - RegExp: { - [CALL]: true, - [CONSTRUCT]: true, - }, - }; - - for (const { node: refNode } of tracker.iterateGlobalReferences( - traceMap, - )) { - if ( - disallowRedundantWrapping && - isUnnecessarilyWrappedRegexLiteral(refNode) - ) { - const regexNode = refNode.arguments[0]; - - if (refNode.arguments.length === 2) { - const suggests = []; - - const argFlags = - getStringValue(refNode.arguments[1]) || ""; - - if ( - canFixTo( - refNode, - regexNode.regex.pattern, - argFlags, - ) - ) { - suggests.push({ - messageId: "replaceWithLiteralAndFlags", - pattern: regexNode.regex.pattern, - flags: argFlags, - }); - } - - const literalFlags = regexNode.regex.flags || ""; - const mergedFlags = mergeRegexFlags( - literalFlags, - argFlags, - ); - - if ( - !areFlagsEqual(mergedFlags, argFlags) && - canFixTo( - refNode, - regexNode.regex.pattern, - mergedFlags, - ) - ) { - suggests.push({ - messageId: - "replaceWithIntendedLiteralAndFlags", - pattern: regexNode.regex.pattern, - flags: mergedFlags, - }); - } - - context.report({ - node: refNode, - messageId: "unexpectedRedundantRegExpWithFlags", - suggest: suggests.map( - ({ flags, pattern, messageId }) => ({ - messageId, - data: { - flags, - }, - fix(fixer) { - return fixer.replaceText( - refNode, - getSafeOutput( - refNode, - `/${pattern}/${flags}`, - ), - ); - }, - }), - ), - }); - } else { - const outputs = []; - - if ( - canFixTo( - refNode, - regexNode.regex.pattern, - regexNode.regex.flags, - ) - ) { - outputs.push(sourceCode.getText(regexNode)); - } - - context.report({ - node: refNode, - messageId: "unexpectedRedundantRegExp", - suggest: outputs.map(output => ({ - messageId: "replaceWithLiteral", - fix(fixer) { - return fixer.replaceText( - refNode, - getSafeOutput(refNode, output), - ); - }, - })), - }); - } - } else if (hasOnlyStaticStringArguments(refNode)) { - let regexContent = getStringValue(refNode.arguments[0]); - let noFix = false; - let flags; - - if (refNode.arguments[1]) { - flags = getStringValue(refNode.arguments[1]); - } - - if (!canFixTo(refNode, regexContent, flags)) { - noFix = true; - } - - if ( - !/^[-\w\\[\](){} \t\r\n\v\f!@#$%^&*+=/~`.> ({ + messageId, + data: { + flags + }, + fix(fixer) { + return fixer.replaceText(refNode, getSafeOutput(refNode, `/${pattern}/${flags}`)); + } + })) + }); + } else { + const outputs = []; + + if (canFixTo(refNode, regexNode.regex.pattern, regexNode.regex.flags)) { + outputs.push(sourceCode.getText(regexNode)); + } + + + context.report({ + node: refNode, + messageId: "unexpectedRedundantRegExp", + suggest: outputs.map(output => ({ + messageId: "replaceWithLiteral", + fix(fixer) { + return fixer.replaceText( + refNode, + getSafeOutput(refNode, output) + ); + } + })) + }); + } + } else if (hasOnlyStaticStringArguments(refNode)) { + let regexContent = getStringValue(refNode.arguments[0]); + let noFix = false; + let flags; + + if (refNode.arguments[1]) { + flags = getStringValue(refNode.arguments[1]); + } + + if (!canFixTo(refNode, regexContent, flags)) { + noFix = true; + } + + if (!/^[-a-zA-Z0-9\\[\](){} \t\r\n\v\f!@#$%^&*+^_=/~`.> - accumulator + - sourceText.slice( - token.range[1], - allTokens[index + 1].range[0], - ), - "", - ); - } - - /** - * Returns a template literal form of the given node. - * @param {ASTNode} currentNode A node that should be converted to a template literal - * @param {string} textBeforeNode Text that should appear before the node - * @param {string} textAfterNode Text that should appear after the node - * @returns {string} A string form of this node, represented as a template literal - */ - function getTemplateLiteral( - currentNode, - textBeforeNode, - textAfterNode, - ) { - if ( - currentNode.type === "Literal" && - typeof currentNode.value === "string" - ) { - /* - * If the current node is a string literal, escape any instances of ${ or ` to prevent them from being interpreted - * as a template placeholder. However, if the code already contains a backslash before the ${ or ` - * for some reason, don't add another backslash, because that would change the meaning of the code (it would cause - * an actual backslash character to appear before the dollar sign). - */ - return `\`${currentNode.raw - .slice(1, -1) - .replace(/\\*(\$\{|`)/gu, matched => { - if (matched.lastIndexOf("\\") % 2) { - return `\\${matched}`; - } - return matched; - - // Unescape any quotes that appear in the original Literal that no longer need to be escaped. - }) - .replace( - new RegExp(`\\\\${currentNode.raw[0]}`, "gu"), - currentNode.raw[0], - )}\``; - } - - if (currentNode.type === "TemplateLiteral") { - return sourceCode.getText(currentNode); - } - - if (isConcatenation(currentNode) && hasStringLiteral(currentNode)) { - const plusSign = sourceCode.getFirstTokenBetween( - currentNode.left, - currentNode.right, - token => token.value === "+", - ); - const textBeforePlus = getTextBetween( - currentNode.left, - plusSign, - ); - const textAfterPlus = getTextBetween( - plusSign, - currentNode.right, - ); - const leftEndsWithCurly = endsWithTemplateCurly( - currentNode.left, - ); - const rightStartsWithCurly = startsWithTemplateCurly( - currentNode.right, - ); - - if (leftEndsWithCurly) { - // If the left side of the expression ends with a template curly, add the extra text to the end of the curly bracket. - // `foo${bar}` /* comment */ + 'baz' --> `foo${bar /* comment */ }${baz}` - return ( - getTemplateLiteral( - currentNode.left, - textBeforeNode, - textBeforePlus + textAfterPlus, - ).slice(0, -1) + - getTemplateLiteral( - currentNode.right, - null, - textAfterNode, - ).slice(1) - ); - } - if (rightStartsWithCurly) { - // Otherwise, if the right side of the expression starts with a template curly, add the text there. - // 'foo' /* comment */ + `${bar}baz` --> `foo${ /* comment */ bar}baz` - return ( - getTemplateLiteral( - currentNode.left, - textBeforeNode, - null, - ).slice(0, -1) + - getTemplateLiteral( - currentNode.right, - textBeforePlus + textAfterPlus, - textAfterNode, - ).slice(1) - ); - } - - /* - * Otherwise, these nodes should not be combined into a template curly, since there is nowhere to put - * the text between them. - */ - return `${getTemplateLiteral(currentNode.left, textBeforeNode, null)}${textBeforePlus}+${textAfterPlus}${getTemplateLiteral(currentNode.right, textAfterNode, null)}`; - } - - return `\`\${${textBeforeNode || ""}${sourceCode.getText(currentNode)}${textAfterNode || ""}}\``; - } - - /** - * Returns a fixer object that converts a non-string binary expression to a template literal - * @param {SourceCodeFixer} fixer The fixer object - * @param {ASTNode} node A node that should be converted to a template literal - * @returns {Object} A fix for this binary expression - */ - function fixNonStringBinaryExpression(fixer, node) { - const topBinaryExpr = getTopConcatBinaryExpression(node.parent); - - if (hasOctalOrNonOctalDecimalEscapeSequence(topBinaryExpr)) { - return null; - } - - return fixer.replaceText( - topBinaryExpr, - getTemplateLiteral(topBinaryExpr, null, null), - ); - } - - /** - * Reports if a given node is string concatenation with non string literals. - * @param {ASTNode} node A node to check. - * @returns {void} - */ - function checkForStringConcat(node) { - if ( - !astUtils.isStringLiteral(node) || - !isConcatenation(node.parent) - ) { - return; - } - - const topBinaryExpr = getTopConcatBinaryExpression(node.parent); - - // Checks whether or not this node had been checked already. - if (done[topBinaryExpr.range[0]]) { - return; - } - done[topBinaryExpr.range[0]] = true; - - if (hasNonStringLiteral(topBinaryExpr)) { - context.report({ - node: topBinaryExpr, - messageId: "unexpectedStringConcatenation", - fix: fixer => fixNonStringBinaryExpression(fixer, node), - }); - } - } - - return { - Program() { - done = Object.create(null); - }, - - Literal: checkForStringConcat, - TemplateLiteral: checkForStringConcat, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Require template literals instead of string concatenation", + recommended: false, + url: "https://eslint.org/docs/latest/rules/prefer-template" + }, + + schema: [], + fixable: "code", + + messages: { + unexpectedStringConcatenation: "Unexpected string concatenation." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + let done = Object.create(null); + + /** + * Gets the non-token text between two nodes, ignoring any other tokens that appear between the two tokens. + * @param {ASTNode} node1 The first node + * @param {ASTNode} node2 The second node + * @returns {string} The text between the nodes, excluding other tokens + */ + function getTextBetween(node1, node2) { + const allTokens = [node1].concat(sourceCode.getTokensBetween(node1, node2)).concat(node2); + const sourceText = sourceCode.getText(); + + return allTokens.slice(0, -1).reduce((accumulator, token, index) => accumulator + sourceText.slice(token.range[1], allTokens[index + 1].range[0]), ""); + } + + /** + * Returns a template literal form of the given node. + * @param {ASTNode} currentNode A node that should be converted to a template literal + * @param {string} textBeforeNode Text that should appear before the node + * @param {string} textAfterNode Text that should appear after the node + * @returns {string} A string form of this node, represented as a template literal + */ + function getTemplateLiteral(currentNode, textBeforeNode, textAfterNode) { + if (currentNode.type === "Literal" && typeof currentNode.value === "string") { + + /* + * If the current node is a string literal, escape any instances of ${ or ` to prevent them from being interpreted + * as a template placeholder. However, if the code already contains a backslash before the ${ or ` + * for some reason, don't add another backslash, because that would change the meaning of the code (it would cause + * an actual backslash character to appear before the dollar sign). + */ + return `\`${currentNode.raw.slice(1, -1).replace(/\\*(\$\{|`)/gu, matched => { + if (matched.lastIndexOf("\\") % 2) { + return `\\${matched}`; + } + return matched; + + // Unescape any quotes that appear in the original Literal that no longer need to be escaped. + }).replace(new RegExp(`\\\\${currentNode.raw[0]}`, "gu"), currentNode.raw[0])}\``; + } + + if (currentNode.type === "TemplateLiteral") { + return sourceCode.getText(currentNode); + } + + if (isConcatenation(currentNode) && hasStringLiteral(currentNode)) { + const plusSign = sourceCode.getFirstTokenBetween(currentNode.left, currentNode.right, token => token.value === "+"); + const textBeforePlus = getTextBetween(currentNode.left, plusSign); + const textAfterPlus = getTextBetween(plusSign, currentNode.right); + const leftEndsWithCurly = endsWithTemplateCurly(currentNode.left); + const rightStartsWithCurly = startsWithTemplateCurly(currentNode.right); + + if (leftEndsWithCurly) { + + // If the left side of the expression ends with a template curly, add the extra text to the end of the curly bracket. + // `foo${bar}` /* comment */ + 'baz' --> `foo${bar /* comment */ }${baz}` + return getTemplateLiteral(currentNode.left, textBeforeNode, textBeforePlus + textAfterPlus).slice(0, -1) + + getTemplateLiteral(currentNode.right, null, textAfterNode).slice(1); + } + if (rightStartsWithCurly) { + + // Otherwise, if the right side of the expression starts with a template curly, add the text there. + // 'foo' /* comment */ + `${bar}baz` --> `foo${ /* comment */ bar}baz` + return getTemplateLiteral(currentNode.left, textBeforeNode, null).slice(0, -1) + + getTemplateLiteral(currentNode.right, textBeforePlus + textAfterPlus, textAfterNode).slice(1); + } + + /* + * Otherwise, these nodes should not be combined into a template curly, since there is nowhere to put + * the text between them. + */ + return `${getTemplateLiteral(currentNode.left, textBeforeNode, null)}${textBeforePlus}+${textAfterPlus}${getTemplateLiteral(currentNode.right, textAfterNode, null)}`; + } + + return `\`\${${textBeforeNode || ""}${sourceCode.getText(currentNode)}${textAfterNode || ""}}\``; + } + + /** + * Returns a fixer object that converts a non-string binary expression to a template literal + * @param {SourceCodeFixer} fixer The fixer object + * @param {ASTNode} node A node that should be converted to a template literal + * @returns {Object} A fix for this binary expression + */ + function fixNonStringBinaryExpression(fixer, node) { + const topBinaryExpr = getTopConcatBinaryExpression(node.parent); + + if (hasOctalOrNonOctalDecimalEscapeSequence(topBinaryExpr)) { + return null; + } + + return fixer.replaceText(topBinaryExpr, getTemplateLiteral(topBinaryExpr, null, null)); + } + + /** + * Reports if a given node is string concatenation with non string literals. + * @param {ASTNode} node A node to check. + * @returns {void} + */ + function checkForStringConcat(node) { + if (!astUtils.isStringLiteral(node) || !isConcatenation(node.parent)) { + return; + } + + const topBinaryExpr = getTopConcatBinaryExpression(node.parent); + + // Checks whether or not this node had been checked already. + if (done[topBinaryExpr.range[0]]) { + return; + } + done[topBinaryExpr.range[0]] = true; + + if (hasNonStringLiteral(topBinaryExpr)) { + context.report({ + node: topBinaryExpr, + messageId: "unexpectedStringConcatenation", + fix: fixer => fixNonStringBinaryExpression(fixer, node) + }); + } + } + + return { + Program() { + done = Object.create(null); + }, + + Literal: checkForStringConcat, + TemplateLiteral: checkForStringConcat + }; + } }; diff --git a/node_modules/eslint/lib/rules/preserve-caught-error.js b/node_modules/eslint/lib/rules/preserve-caught-error.js deleted file mode 100644 index a09c05194..000000000 --- a/node_modules/eslint/lib/rules/preserve-caught-error.js +++ /dev/null @@ -1,535 +0,0 @@ -/** - * @fileoverview Rule to preserve caught errors when re-throwing exceptions - * @author Amnish Singh Arora - */ -"use strict"; - -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ - -const astUtils = require("./utils/ast-utils"); - -//------------------------------------------------------------------------------ -// Types -//------------------------------------------------------------------------------ - -/** @typedef {import("estree").Node} ASTNode */ - -//------------------------------------------------------------------------------ -// Helpers -//------------------------------------------------------------------------------ - -/* - * This is an indicator of an error cause node, that is too complicated to be detected and fixed. - * Eg, when error options is an `Identifier` or a `SpreadElement`. - */ -const UNKNOWN_CAUSE = Symbol("unknown_cause"); - -const BUILT_IN_ERROR_TYPES = new Set([ - "Error", - "EvalError", - "RangeError", - "ReferenceError", - "SyntaxError", - "TypeError", - "URIError", - "AggregateError", -]); - -/** - * Finds and returns information about the `cause` property of an error being thrown. - * @param {ASTNode} throwStatement `ThrowStatement` to be checked. - * @returns {{ value: ASTNode; multipleDefinitions: boolean; } | UNKNOWN_CAUSE | null} - * Information about the `cause` of the error being thrown, such as the value node and - * whether there are multiple definitions of `cause`. `null` if there is no `cause`. - */ -function getErrorCause(throwStatement) { - const throwExpression = throwStatement.argument; - /* - * Determine which argument index holds the options object - * `AggregateError` is a special case as it accepts the `options` object as third argument. - */ - const optionsIndex = - throwExpression.callee.name === "AggregateError" ? 2 : 1; - - /* - * Make sure there is no `SpreadElement` at or before the `optionsIndex` - * as this messes up the effective order of arguments and makes it complicated - * to track where the actual error options need to be at - */ - const spreadExpressionIndex = throwExpression.arguments.findIndex( - arg => arg.type === "SpreadElement", - ); - if (spreadExpressionIndex >= 0 && spreadExpressionIndex <= optionsIndex) { - return UNKNOWN_CAUSE; - } - - const errorOptions = throwExpression.arguments[optionsIndex]; - - if (errorOptions) { - if (errorOptions.type === "ObjectExpression") { - if ( - errorOptions.properties.some( - prop => prop.type === "SpreadElement", - ) - ) { - /* - * If there is a spread element as part of error options, it is too complicated - * to verify if the cause is used properly and auto-fix. - */ - return UNKNOWN_CAUSE; - } - - const causeProperties = errorOptions.properties.filter( - prop => astUtils.getStaticPropertyName(prop) === "cause", - ); - - const causeProperty = causeProperties.at(-1); - return causeProperty - ? { - value: causeProperty.value, - multipleDefinitions: causeProperties.length > 1, - } - : null; - } - - // Error options exist, but too complicated to be analyzed/fixed - return UNKNOWN_CAUSE; - } - - return null; -} - -/** - * Finds and returns the `CatchClause` node, that the `node` is part of. - * @param {ASTNode} node The AST node to be evaluated. - * @returns {ASTNode | null } The closest parent `CatchClause` node, `null` if the `node` is not in a catch block. - */ -function findParentCatch(node) { - let currentNode = node; - - while (currentNode && currentNode.type !== "CatchClause") { - if ( - [ - "FunctionDeclaration", - "FunctionExpression", - "ArrowFunctionExpression", - "StaticBlock", - ].includes(currentNode.type) - ) { - /* - * Make sure the ThrowStatement is not made inside a function definition or a static block inside a high level catch. - * In such cases, the caught error is not directly related to the Throw. - * - * For example, - * try { - * } catch (error) { - * foo = { - * bar() { - * throw new Error(); - * } - * }; - * } - */ - return null; - } - currentNode = currentNode.parent; - } - - return currentNode; -} - -//------------------------------------------------------------------------------ -// Rule Definition -//------------------------------------------------------------------------------ - -/** @type {import('../types').Rule.RuleModule} */ -module.exports = { - meta: { - type: "suggestion", - - defaultOptions: [ - { - requireCatchParameter: false, - }, - ], - - docs: { - description: - "Disallow losing originally caught error when re-throwing custom errors", - recommended: false, - url: "https://eslint.org/docs/latest/rules/preserve-caught-error", // URL to the documentation page for this rule - }, - /* - * TODO: We should allow passing `customErrorTypes` option once something like `typescript-eslint`'s - * `TypeOrValueSpecifier` is implemented in core Eslint. - * See: - * 1. https://typescript-eslint.io/packages/type-utils/type-or-value-specifier/ - * 2. https://github.com/eslint/eslint/pull/19913#discussion_r2192608593 - * 3. https://github.com/eslint/eslint/discussions/16540 - */ - schema: [ - { - type: "object", - properties: { - requireCatchParameter: { - type: "boolean", - description: - "Requires the catch blocks to always have the caught error parameter so it is not discarded.", - }, - }, - additionalProperties: false, - }, - ], - messages: { - missingCause: - "There is no `cause` attached to the symptom error being thrown.", - incorrectCause: - "The symptom error is being thrown with an incorrect `cause`.", - includeCause: - "Include the original caught error as the `cause` of the symptom error.", - missingCatchErrorParam: - "The caught error is not accessible because the catch clause lacks the error parameter. Start referencing the caught error using the catch parameter.", - partiallyLostError: - "Re-throws cannot preserve the caught error as a part of it is being lost due to destructuring.", - caughtErrorShadowed: - "The caught error is being attached as `cause`, but is shadowed by a closer scoped redeclaration.", - }, - hasSuggestions: true, - }, - - create(context) { - const sourceCode = context.sourceCode; - const [{ requireCatchParameter }] = context.options; - - //---------------------------------------------------------------------- - // Helpers - //---------------------------------------------------------------------- - - /** - * Checks if a `ThrowStatement` is constructing and throwing a new `Error` object. - * - * Covers all the error types on `globalThis` that support `cause` property: - * https://github.com/microsoft/TypeScript/blob/main/src/lib/es2022.error.d.ts - * @param {ASTNode} throwStatement The `ThrowStatement` that needs to be checked. - * @returns {boolean} `true` if a new "Error" is being thrown, else `false`. - */ - function isThrowingNewError(throwStatement) { - return ( - (throwStatement.argument.type === "NewExpression" || - throwStatement.argument.type === "CallExpression") && - throwStatement.argument.callee.type === "Identifier" && - BUILT_IN_ERROR_TYPES.has(throwStatement.argument.callee.name) && - /* - * Make sure the thrown Error is instance is one of the built-in global error types. - * Custom imports could shadow this, which would lead to false positives. - * e.g. import { Error } from "./my-custom-error.js"; - * throw Error("Failed to perform error prone operations"); - */ - sourceCode.isGlobalReference(throwStatement.argument.callee) - ); - } - - /** - * Inserts `cause: ` into an inline options object expression. - * @param {RuleFixer} fixer The fixer object. - * @param {ASTNode} optionsNode The options object node. - * @param {string} caughtErrorName The name of the caught error (e.g., "err"). - * @returns {Fix} The fix object. - */ - function insertCauseIntoOptions(fixer, optionsNode, caughtErrorName) { - const properties = optionsNode.properties; - - if (properties.length === 0) { - // Insert inside empty braces: `{}` → `{ cause: err }` - return fixer.insertTextAfter( - sourceCode.getFirstToken(optionsNode), - `cause: ${caughtErrorName}`, - ); - } - - const lastProp = properties.at(-1); - return fixer.insertTextAfter( - lastProp, - `, cause: ${caughtErrorName}`, - ); - } - - //---------------------------------------------------------------------- - // Public - //---------------------------------------------------------------------- - return { - ThrowStatement(node) { - // Check if the throw is inside a catch block - const parentCatch = findParentCatch(node); - const throwStatement = node; - - // Check if a new error is being thrown in a catch block - if (parentCatch && isThrowingNewError(throwStatement)) { - if ( - parentCatch.param && - parentCatch.param.type !== "Identifier" - ) { - /* - * When a part of the caught error is being lost at the parameter level, commonly due to destructuring. - * e.g. catch({ message, ...rest }) - */ - context.report({ - messageId: "partiallyLostError", - node: parentCatch, - }); - return; - } - - const caughtError = - parentCatch.param?.type === "Identifier" - ? parentCatch.param - : null; - - // Check if there are throw statements and caught error is being ignored - if (!caughtError) { - if (requireCatchParameter) { - context.report({ - node: throwStatement, - messageId: "missingCatchErrorParam", - }); - return; - } - return; - } - - // Check if there is a cause attached to the new error - const errorCauseInfo = getErrorCause(throwStatement); - - if (errorCauseInfo === UNKNOWN_CAUSE) { - // Error options exist, but too complicated to be analyzed/fixed - return; - } - - if (errorCauseInfo === null) { - // If there is no `cause` attached to the error being thrown. - context.report({ - messageId: "missingCause", - node: throwStatement, - suggest: [ - { - messageId: "includeCause", - fix(fixer) { - const throwExpression = - throwStatement.argument; - const args = throwExpression.arguments; - const errorType = - throwExpression.callee.name; - - // AggregateError: errors, message, options - if (errorType === "AggregateError") { - const errorsArg = args[0]; - const messageArg = args[1]; - const optionsArg = args[2]; - - if (!errorsArg) { - // Case: `throw new AggregateError()` → insert all arguments - const lastToken = - sourceCode.getLastToken( - throwExpression, - ); - const lastCalleeToken = - sourceCode.getLastToken( - throwExpression.callee, - ); - const parenToken = - sourceCode.getFirstTokenBetween( - lastCalleeToken, - lastToken, - astUtils.isOpeningParenToken, - ); - - if (parenToken) { - return fixer.insertTextAfter( - parenToken, - `[], "", { cause: ${caughtError.name} }`, - ); - } - return fixer.insertTextAfter( - throwExpression.callee, - `([], "", { cause: ${caughtError.name} })`, - ); - } - - if (!messageArg) { - // Case: `throw new AggregateError([])` → insert message and options - return fixer.insertTextAfter( - errorsArg, - `, "", { cause: ${caughtError.name} }`, - ); - } - - if (!optionsArg) { - // Case: `throw new AggregateError([], "")` → insert error options only - return fixer.insertTextAfter( - messageArg, - `, { cause: ${caughtError.name} }`, - ); - } - - if ( - optionsArg.type === - "ObjectExpression" - ) { - return insertCauseIntoOptions( - fixer, - optionsArg, - caughtError.name, - ); - } - - // Complex dynamic options — skip - return null; - } - - // Normal Error types - const messageArg = args[0]; - const optionsArg = args[1]; - - if (!messageArg) { - // Case: `throw new Error()` → insert both message and options - const lastToken = - sourceCode.getLastToken( - throwExpression, - ); - const lastCalleeToken = - sourceCode.getLastToken( - throwExpression.callee, - ); - const parenToken = - sourceCode.getFirstTokenBetween( - lastCalleeToken, - lastToken, - astUtils.isOpeningParenToken, - ); - - if (parenToken) { - return fixer.insertTextAfter( - parenToken, - `"", { cause: ${caughtError.name} }`, - ); - } - return fixer.insertTextAfter( - throwExpression.callee, - `("", { cause: ${caughtError.name} })`, - ); - } - if (!optionsArg) { - // Case: `throw new Error("Some message")` → insert only options - return fixer.insertTextAfter( - messageArg, - `, { cause: ${caughtError.name} }`, - ); - } - - if ( - optionsArg.type === - "ObjectExpression" - ) { - return insertCauseIntoOptions( - fixer, - optionsArg, - caughtError.name, - ); - } - - return null; // Identifier or spread — do not fix - }, - }, - ], - }); - - // We don't need to check further - return; - } - - const { value: thrownErrorCause } = errorCauseInfo; - - // If there is an attached cause, verify that it matches the caught error - if ( - !( - thrownErrorCause.type === "Identifier" && - thrownErrorCause.name === caughtError.name - ) - ) { - const suggest = errorCauseInfo.multipleDefinitions - ? null // If there are multiple `cause` definitions, a suggestion could be confusing. - : [ - { - messageId: "includeCause", - fix(fixer) { - /* - * In case `cause` is attached using object property shorthand or as a method or accessor. - * e.g. throw Error("fail", { cause }); - * throw Error("fail", { cause() { doSomething(); } }); - * throw Error("fail", { get cause() { return error; } }); - */ - if ( - thrownErrorCause.parent - .method || - thrownErrorCause.parent - .shorthand || - thrownErrorCause.parent.kind !== - "init" - ) { - return fixer.replaceText( - thrownErrorCause.parent, - `cause: ${caughtError.name}`, - ); - } - - return fixer.replaceText( - thrownErrorCause, - caughtError.name, - ); - }, - }, - ]; - context.report({ - messageId: "incorrectCause", - node: thrownErrorCause, - suggest, - }); - return; - } - - /* - * If the attached cause matches the identifier name of the caught error, - * make sure it is not being shadowed by a closer scoped redeclaration. - * - * e.g. try { - * doSomething(); - * } catch (error) { - * if (whatever) { - * const error = anotherError; - * throw new Error("Something went wrong"); - * } - * } - */ - let scope = sourceCode.getScope(throwStatement); - do { - const variable = scope.set.get(caughtError.name); - if (variable) { - break; - } - scope = scope.upper; - } while (scope); - - if (scope?.block !== parentCatch) { - // Caught error is being shadowed - context.report({ - messageId: "caughtErrorShadowed", - node: throwStatement, - }); - } - } - }, - }; - }, -}; diff --git a/node_modules/eslint/lib/rules/quote-props.js b/node_modules/eslint/lib/rules/quote-props.js index 5cbf77d56..fe26eed77 100644 --- a/node_modules/eslint/lib/rules/quote-props.js +++ b/node_modules/eslint/lib/rules/quote-props.js @@ -17,378 +17,294 @@ const keywords = require("./utils/keywords"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "quote-props", - url: "https://eslint.style/rules/quote-props", - }, - }, - ], - }, - type: "suggestion", - - docs: { - description: "Require quotes around object literal property names", - recommended: false, - url: "https://eslint.org/docs/latest/rules/quote-props", - }, - - schema: { - anyOf: [ - { - type: "array", - items: [ - { - enum: [ - "always", - "as-needed", - "consistent", - "consistent-as-needed", - ], - }, - ], - minItems: 0, - maxItems: 1, - }, - { - type: "array", - items: [ - { - enum: [ - "always", - "as-needed", - "consistent", - "consistent-as-needed", - ], - }, - { - type: "object", - properties: { - keywords: { - type: "boolean", - }, - unnecessary: { - type: "boolean", - }, - numbers: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - minItems: 0, - maxItems: 2, - }, - ], - }, - - fixable: "code", - messages: { - requireQuotesDueToReservedWord: - "Properties should be quoted as '{{property}}' is a reserved word.", - inconsistentlyQuotedProperty: - "Inconsistently quoted property '{{key}}' found.", - unnecessarilyQuotedProperty: - "Unnecessarily quoted property '{{property}}' found.", - unquotedReservedProperty: - "Unquoted reserved word '{{property}}' used as key.", - unquotedNumericProperty: - "Unquoted number literal '{{property}}' used as key.", - unquotedPropertyFound: "Unquoted property '{{property}}' found.", - redundantQuoting: - "Properties shouldn't be quoted as all quotes are redundant.", - }, - }, - - create(context) { - const MODE = context.options[0], - KEYWORDS = context.options[1] && context.options[1].keywords, - CHECK_UNNECESSARY = - !context.options[1] || context.options[1].unnecessary !== false, - NUMBERS = context.options[1] && context.options[1].numbers, - sourceCode = context.sourceCode; - - /** - * Checks whether a certain string constitutes an ES3 token - * @param {string} tokenStr The string to be checked. - * @returns {boolean} `true` if it is an ES3 token. - */ - function isKeyword(tokenStr) { - return keywords.includes(tokenStr); - } - - /** - * Checks if an espree-tokenized key has redundant quotes (i.e. whether quotes are unnecessary) - * @param {string} rawKey The raw key value from the source - * @param {espreeTokens} tokens The espree-tokenized node key - * @param {boolean} [skipNumberLiterals=false] Indicates whether number literals should be checked - * @returns {boolean} Whether or not a key has redundant quotes. - * @private - */ - function areQuotesRedundant(rawKey, tokens, skipNumberLiterals) { - return ( - tokens.length === 1 && - tokens[0].start === 0 && - tokens[0].end === rawKey.length && - (["Identifier", "Keyword", "Null", "Boolean"].includes( - tokens[0].type, - ) || - (tokens[0].type === "Numeric" && - !skipNumberLiterals && - String(+tokens[0].value) === tokens[0].value)) - ); - } - - /** - * Returns a string representation of a property node with quotes removed - * @param {ASTNode} key Key AST Node, which may or may not be quoted - * @returns {string} A replacement string for this property - */ - function getUnquotedKey(key) { - return key.type === "Identifier" ? key.name : key.value; - } - - /** - * Returns a string representation of a property node with quotes added - * @param {ASTNode} key Key AST Node, which may or may not be quoted - * @returns {string} A replacement string for this property - */ - function getQuotedKey(key) { - if (key.type === "Literal" && typeof key.value === "string") { - // If the key is already a string literal, don't replace the quotes with double quotes. - return sourceCode.getText(key); - } - - // Otherwise, the key is either an identifier or a number literal. - return `"${key.type === "Identifier" ? key.name : key.value}"`; - } - - /** - * Ensures that a property's key is quoted only when necessary - * @param {ASTNode} node Property AST node - * @returns {void} - */ - function checkUnnecessaryQuotes(node) { - const key = node.key; - - if (node.method || node.computed || node.shorthand) { - return; - } - - if (key.type === "Literal" && typeof key.value === "string") { - let tokens; - - try { - tokens = espree.tokenize(key.value); - } catch { - return; - } - - if (tokens.length !== 1) { - return; - } - - const isKeywordToken = isKeyword(tokens[0].value); - - if (isKeywordToken && KEYWORDS) { - return; - } - - if ( - CHECK_UNNECESSARY && - areQuotesRedundant(key.value, tokens, NUMBERS) - ) { - context.report({ - node, - messageId: "unnecessarilyQuotedProperty", - data: { property: key.value }, - fix: fixer => - fixer.replaceText(key, getUnquotedKey(key)), - }); - } - } else if ( - KEYWORDS && - key.type === "Identifier" && - isKeyword(key.name) - ) { - context.report({ - node, - messageId: "unquotedReservedProperty", - data: { property: key.name }, - fix: fixer => fixer.replaceText(key, getQuotedKey(key)), - }); - } else if ( - NUMBERS && - key.type === "Literal" && - astUtils.isNumericLiteral(key) - ) { - context.report({ - node, - messageId: "unquotedNumericProperty", - data: { property: key.value }, - fix: fixer => fixer.replaceText(key, getQuotedKey(key)), - }); - } - } - - /** - * Ensures that a property's key is quoted - * @param {ASTNode} node Property AST node - * @returns {void} - */ - function checkOmittedQuotes(node) { - const key = node.key; - - if ( - !node.method && - !node.computed && - !node.shorthand && - !(key.type === "Literal" && typeof key.value === "string") - ) { - context.report({ - node, - messageId: "unquotedPropertyFound", - data: { property: key.name || key.value }, - fix: fixer => fixer.replaceText(key, getQuotedKey(key)), - }); - } - } - - /** - * Ensures that an object's keys are consistently quoted, optionally checks for redundancy of quotes - * @param {ASTNode} node Property AST node - * @param {boolean} checkQuotesRedundancy Whether to check quotes' redundancy - * @returns {void} - */ - function checkConsistency(node, checkQuotesRedundancy) { - const quotedProps = [], - unquotedProps = []; - let keywordKeyName = null, - necessaryQuotes = false; - - node.properties.forEach(property => { - const key = property.key; - - if ( - !key || - property.method || - property.computed || - property.shorthand - ) { - return; - } - - if (key.type === "Literal" && typeof key.value === "string") { - quotedProps.push(property); - - if (checkQuotesRedundancy) { - let tokens; - - try { - tokens = espree.tokenize(key.value); - } catch { - necessaryQuotes = true; - return; - } - - necessaryQuotes = - necessaryQuotes || - !areQuotesRedundant(key.value, tokens) || - (KEYWORDS && isKeyword(tokens[0].value)); - } - } else if ( - KEYWORDS && - checkQuotesRedundancy && - key.type === "Identifier" && - isKeyword(key.name) - ) { - unquotedProps.push(property); - necessaryQuotes = true; - keywordKeyName = key.name; - } else { - unquotedProps.push(property); - } - }); - - if ( - checkQuotesRedundancy && - quotedProps.length && - !necessaryQuotes - ) { - quotedProps.forEach(property => { - context.report({ - node: property, - messageId: "redundantQuoting", - fix: fixer => - fixer.replaceText( - property.key, - getUnquotedKey(property.key), - ), - }); - }); - } else if (unquotedProps.length && keywordKeyName) { - unquotedProps.forEach(property => { - context.report({ - node: property, - messageId: "requireQuotesDueToReservedWord", - data: { property: keywordKeyName }, - fix: fixer => - fixer.replaceText( - property.key, - getQuotedKey(property.key), - ), - }); - }); - } else if (quotedProps.length && unquotedProps.length) { - unquotedProps.forEach(property => { - context.report({ - node: property, - messageId: "inconsistentlyQuotedProperty", - data: { key: property.key.name || property.key.value }, - fix: fixer => - fixer.replaceText( - property.key, - getQuotedKey(property.key), - ), - }); - }); - } - } - - return { - Property(node) { - if (MODE === "always" || !MODE) { - checkOmittedQuotes(node); - } - if (MODE === "as-needed") { - checkUnnecessaryQuotes(node); - } - }, - ObjectExpression(node) { - if (MODE === "consistent") { - checkConsistency(node, false); - } - if (MODE === "consistent-as-needed") { - checkConsistency(node, true); - } - }, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "suggestion", + + docs: { + description: "Require quotes around object literal property names", + recommended: false, + url: "https://eslint.org/docs/latest/rules/quote-props" + }, + + schema: { + anyOf: [ + { + type: "array", + items: [ + { + enum: ["always", "as-needed", "consistent", "consistent-as-needed"] + } + ], + minItems: 0, + maxItems: 1 + }, + { + type: "array", + items: [ + { + enum: ["always", "as-needed", "consistent", "consistent-as-needed"] + }, + { + type: "object", + properties: { + keywords: { + type: "boolean" + }, + unnecessary: { + type: "boolean" + }, + numbers: { + type: "boolean" + } + }, + additionalProperties: false + } + ], + minItems: 0, + maxItems: 2 + } + ] + }, + + fixable: "code", + messages: { + requireQuotesDueToReservedWord: "Properties should be quoted as '{{property}}' is a reserved word.", + inconsistentlyQuotedProperty: "Inconsistently quoted property '{{key}}' found.", + unnecessarilyQuotedProperty: "Unnecessarily quoted property '{{property}}' found.", + unquotedReservedProperty: "Unquoted reserved word '{{property}}' used as key.", + unquotedNumericProperty: "Unquoted number literal '{{property}}' used as key.", + unquotedPropertyFound: "Unquoted property '{{property}}' found.", + redundantQuoting: "Properties shouldn't be quoted as all quotes are redundant." + } + }, + + create(context) { + + const MODE = context.options[0], + KEYWORDS = context.options[1] && context.options[1].keywords, + CHECK_UNNECESSARY = !context.options[1] || context.options[1].unnecessary !== false, + NUMBERS = context.options[1] && context.options[1].numbers, + + sourceCode = context.sourceCode; + + + /** + * Checks whether a certain string constitutes an ES3 token + * @param {string} tokenStr The string to be checked. + * @returns {boolean} `true` if it is an ES3 token. + */ + function isKeyword(tokenStr) { + return keywords.includes(tokenStr); + } + + /** + * Checks if an espree-tokenized key has redundant quotes (i.e. whether quotes are unnecessary) + * @param {string} rawKey The raw key value from the source + * @param {espreeTokens} tokens The espree-tokenized node key + * @param {boolean} [skipNumberLiterals=false] Indicates whether number literals should be checked + * @returns {boolean} Whether or not a key has redundant quotes. + * @private + */ + function areQuotesRedundant(rawKey, tokens, skipNumberLiterals) { + return tokens.length === 1 && tokens[0].start === 0 && tokens[0].end === rawKey.length && + (["Identifier", "Keyword", "Null", "Boolean"].includes(tokens[0].type) || + (tokens[0].type === "Numeric" && !skipNumberLiterals && String(+tokens[0].value) === tokens[0].value)); + } + + /** + * Returns a string representation of a property node with quotes removed + * @param {ASTNode} key Key AST Node, which may or may not be quoted + * @returns {string} A replacement string for this property + */ + function getUnquotedKey(key) { + return key.type === "Identifier" ? key.name : key.value; + } + + /** + * Returns a string representation of a property node with quotes added + * @param {ASTNode} key Key AST Node, which may or may not be quoted + * @returns {string} A replacement string for this property + */ + function getQuotedKey(key) { + if (key.type === "Literal" && typeof key.value === "string") { + + // If the key is already a string literal, don't replace the quotes with double quotes. + return sourceCode.getText(key); + } + + // Otherwise, the key is either an identifier or a number literal. + return `"${key.type === "Identifier" ? key.name : key.value}"`; + } + + /** + * Ensures that a property's key is quoted only when necessary + * @param {ASTNode} node Property AST node + * @returns {void} + */ + function checkUnnecessaryQuotes(node) { + const key = node.key; + + if (node.method || node.computed || node.shorthand) { + return; + } + + if (key.type === "Literal" && typeof key.value === "string") { + let tokens; + + try { + tokens = espree.tokenize(key.value); + } catch { + return; + } + + if (tokens.length !== 1) { + return; + } + + const isKeywordToken = isKeyword(tokens[0].value); + + if (isKeywordToken && KEYWORDS) { + return; + } + + if (CHECK_UNNECESSARY && areQuotesRedundant(key.value, tokens, NUMBERS)) { + context.report({ + node, + messageId: "unnecessarilyQuotedProperty", + data: { property: key.value }, + fix: fixer => fixer.replaceText(key, getUnquotedKey(key)) + }); + } + } else if (KEYWORDS && key.type === "Identifier" && isKeyword(key.name)) { + context.report({ + node, + messageId: "unquotedReservedProperty", + data: { property: key.name }, + fix: fixer => fixer.replaceText(key, getQuotedKey(key)) + }); + } else if (NUMBERS && key.type === "Literal" && astUtils.isNumericLiteral(key)) { + context.report({ + node, + messageId: "unquotedNumericProperty", + data: { property: key.value }, + fix: fixer => fixer.replaceText(key, getQuotedKey(key)) + }); + } + } + + /** + * Ensures that a property's key is quoted + * @param {ASTNode} node Property AST node + * @returns {void} + */ + function checkOmittedQuotes(node) { + const key = node.key; + + if (!node.method && !node.computed && !node.shorthand && !(key.type === "Literal" && typeof key.value === "string")) { + context.report({ + node, + messageId: "unquotedPropertyFound", + data: { property: key.name || key.value }, + fix: fixer => fixer.replaceText(key, getQuotedKey(key)) + }); + } + } + + /** + * Ensures that an object's keys are consistently quoted, optionally checks for redundancy of quotes + * @param {ASTNode} node Property AST node + * @param {boolean} checkQuotesRedundancy Whether to check quotes' redundancy + * @returns {void} + */ + function checkConsistency(node, checkQuotesRedundancy) { + const quotedProps = [], + unquotedProps = []; + let keywordKeyName = null, + necessaryQuotes = false; + + node.properties.forEach(property => { + const key = property.key; + + if (!key || property.method || property.computed || property.shorthand) { + return; + } + + if (key.type === "Literal" && typeof key.value === "string") { + + quotedProps.push(property); + + if (checkQuotesRedundancy) { + let tokens; + + try { + tokens = espree.tokenize(key.value); + } catch { + necessaryQuotes = true; + return; + } + + necessaryQuotes = necessaryQuotes || !areQuotesRedundant(key.value, tokens) || KEYWORDS && isKeyword(tokens[0].value); + } + } else if (KEYWORDS && checkQuotesRedundancy && key.type === "Identifier" && isKeyword(key.name)) { + unquotedProps.push(property); + necessaryQuotes = true; + keywordKeyName = key.name; + } else { + unquotedProps.push(property); + } + }); + + if (checkQuotesRedundancy && quotedProps.length && !necessaryQuotes) { + quotedProps.forEach(property => { + context.report({ + node: property, + messageId: "redundantQuoting", + fix: fixer => fixer.replaceText(property.key, getUnquotedKey(property.key)) + }); + }); + } else if (unquotedProps.length && keywordKeyName) { + unquotedProps.forEach(property => { + context.report({ + node: property, + messageId: "requireQuotesDueToReservedWord", + data: { property: keywordKeyName }, + fix: fixer => fixer.replaceText(property.key, getQuotedKey(property.key)) + }); + }); + } else if (quotedProps.length && unquotedProps.length) { + unquotedProps.forEach(property => { + context.report({ + node: property, + messageId: "inconsistentlyQuotedProperty", + data: { key: property.key.name || property.key.value }, + fix: fixer => fixer.replaceText(property.key, getQuotedKey(property.key)) + }); + }); + } + } + + return { + Property(node) { + if (MODE === "always" || !MODE) { + checkOmittedQuotes(node); + } + if (MODE === "as-needed") { + checkUnnecessaryQuotes(node); + } + }, + ObjectExpression(node) { + if (MODE === "consistent") { + checkConsistency(node, false); + } + if (MODE === "consistent-as-needed") { + checkConsistency(node, true); + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/quotes.js b/node_modules/eslint/lib/rules/quotes.js index 4de678cd3..17d97dd69 100644 --- a/node_modules/eslint/lib/rules/quotes.js +++ b/node_modules/eslint/lib/rules/quotes.js @@ -17,28 +17,25 @@ const astUtils = require("./utils/ast-utils"); //------------------------------------------------------------------------------ const QUOTE_SETTINGS = { - double: { - quote: '"', - alternateQuote: "'", - description: "doublequote", - }, - single: { - quote: "'", - alternateQuote: '"', - description: "singlequote", - }, - backtick: { - quote: "`", - alternateQuote: '"', - description: "backtick", - }, + double: { + quote: "\"", + alternateQuote: "'", + description: "doublequote" + }, + single: { + quote: "'", + alternateQuote: "\"", + description: "singlequote" + }, + backtick: { + quote: "`", + alternateQuote: "\"", + description: "backtick" + } }; // An unescaped newline is a newline preceded by an even number of backslashes. -const UNESCAPED_LINEBREAK_PATTERN = new RegExp( - String.raw`(^|[^\\])(\\\\)*[${Array.from(astUtils.LINEBREAKS).join("")}]`, - "u", -); +const UNESCAPED_LINEBREAK_PATTERN = new RegExp(String.raw`(^|[^\\])(\\\\)*[${Array.from(astUtils.LINEBREAKS).join("")}]`, "u"); /** * Switches quoting of javascript string between ' " and ` @@ -50,43 +47,27 @@ const UNESCAPED_LINEBREAK_PATTERN = new RegExp( * @private */ QUOTE_SETTINGS.double.convert = - QUOTE_SETTINGS.single.convert = - QUOTE_SETTINGS.backtick.convert = - function (str) { - const newQuote = this.quote; - const oldQuote = str[0]; - - if (newQuote === oldQuote) { - return str; - } - return ( - newQuote + - str - .slice(1, -1) - .replace( - /\\(\$\{|\r\n?|\n|.)|["'`]|\$\{|(\r\n?|\n)/gu, - (match, escaped, newline) => { - if ( - escaped === oldQuote || - (oldQuote === "`" && escaped === "${") - ) { - return escaped; // unescape - } - if ( - match === newQuote || - (newQuote === "`" && match === "${") - ) { - return `\\${match}`; // escape - } - if (newline && oldQuote === "`") { - return "\\n"; // escape newlines - } - return match; - }, - ) + - newQuote - ); - }; +QUOTE_SETTINGS.single.convert = +QUOTE_SETTINGS.backtick.convert = function(str) { + const newQuote = this.quote; + const oldQuote = str[0]; + + if (newQuote === oldQuote) { + return str; + } + return newQuote + str.slice(1, -1).replace(/\\(\$\{|\r\n?|\n|.)|["'`]|\$\{|(\r\n?|\n)/gu, (match, escaped, newline) => { + if (escaped === oldQuote || oldQuote === "`" && escaped === "${") { + return escaped; // unescape + } + if (match === newQuote || newQuote === "`" && match === "${") { + return `\\${match}`; // escape + } + if (newline && oldQuote === "`") { + return "\\n"; // escape newlines + } + return match; + }) + newQuote; +}; const AVOID_ESCAPE = "avoid-escape"; @@ -94,323 +75,276 @@ const AVOID_ESCAPE = "avoid-escape"; // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "quotes", - url: "https://eslint.style/rules/quotes", - }, - }, - ], - }, - type: "layout", - - docs: { - description: - "Enforce the consistent use of either backticks, double, or single quotes", - recommended: false, - url: "https://eslint.org/docs/latest/rules/quotes", - }, - - fixable: "code", - - schema: [ - { - enum: ["single", "double", "backtick"], - }, - { - anyOf: [ - { - enum: ["avoid-escape"], - }, - { - type: "object", - properties: { - avoidEscape: { - type: "boolean", - }, - allowTemplateLiterals: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - }, - ], - - messages: { - wrongQuotes: "Strings must use {{description}}.", - }, - }, - - create(context) { - const quoteOption = context.options[0], - settings = QUOTE_SETTINGS[quoteOption || "double"], - options = context.options[1], - allowTemplateLiterals = - options && options.allowTemplateLiterals === true, - sourceCode = context.sourceCode; - let avoidEscape = options && options.avoidEscape === true; - - // deprecated - if (options === AVOID_ESCAPE) { - avoidEscape = true; - } - - /** - * Determines if a given node is part of JSX syntax. - * - * This function returns `true` in the following cases: - * - * - `
` ... If the literal is an attribute value, the parent of the literal is `JSXAttribute`. - * - `
foo
` ... If the literal is a text content, the parent of the literal is `JSXElement`. - * - `<>foo` ... If the literal is a text content, the parent of the literal is `JSXFragment`. - * - * In particular, this function returns `false` in the following cases: - * - * - `
` - * - `
{"foo"}
` - * - * In both cases, inside of the braces is handled as normal JavaScript. - * The braces are `JSXExpressionContainer` nodes. - * @param {ASTNode} node The Literal node to check. - * @returns {boolean} True if the node is a part of JSX, false if not. - * @private - */ - function isJSXLiteral(node) { - return ( - node.parent.type === "JSXAttribute" || - node.parent.type === "JSXElement" || - node.parent.type === "JSXFragment" - ); - } - - /** - * Checks whether or not a given node is a directive. - * The directive is a `ExpressionStatement` which has only a string literal not surrounded by - * parentheses. - * @param {ASTNode} node A node to check. - * @returns {boolean} Whether or not the node is a directive. - * @private - */ - function isDirective(node) { - return ( - node.type === "ExpressionStatement" && - node.expression.type === "Literal" && - typeof node.expression.value === "string" && - !astUtils.isParenthesised(sourceCode, node.expression) - ); - } - - /** - * Checks whether a specified node is either part of, or immediately follows a (possibly empty) directive prologue. - * @see {@link http://www.ecma-international.org/ecma-262/6.0/#sec-directive-prologues-and-the-use-strict-directive} - * @param {ASTNode} node A node to check. - * @returns {boolean} Whether a specified node is either part of, or immediately follows a (possibly empty) directive prologue. - * @private - */ - function isExpressionInOrJustAfterDirectivePrologue(node) { - if (!astUtils.isTopLevelExpressionStatement(node.parent)) { - return false; - } - const block = node.parent.parent; - - // Check the node is at a prologue. - for (let i = 0; i < block.body.length; ++i) { - const statement = block.body[i]; - - if (statement === node.parent) { - return true; - } - if (!isDirective(statement)) { - break; - } - } - - return false; - } - - /** - * Checks whether or not a given node is allowed as non backtick. - * @param {ASTNode} node A node to check. - * @returns {boolean} Whether or not the node is allowed as non backtick. - * @private - */ - function isAllowedAsNonBacktick(node) { - const parent = node.parent; - - switch (parent.type) { - // Directive Prologues. - case "ExpressionStatement": - return ( - !astUtils.isParenthesised(sourceCode, node) && - isExpressionInOrJustAfterDirectivePrologue(node) - ); - - // LiteralPropertyName. - case "Property": - case "PropertyDefinition": - case "MethodDefinition": - return parent.key === node && !parent.computed; - - // ModuleSpecifier. - case "ImportDeclaration": - case "ExportNamedDeclaration": - return parent.source === node; - - // ModuleExportName or ModuleSpecifier. - case "ExportAllDeclaration": - return parent.exported === node || parent.source === node; - - // ModuleExportName. - case "ImportSpecifier": - return parent.imported === node; - - // ModuleExportName. - case "ExportSpecifier": - return parent.local === node || parent.exported === node; - - // Others don't allow. - default: - return false; - } - } - - /** - * Checks whether or not a given TemplateLiteral node is actually using any of the special features provided by template literal strings. - * @param {ASTNode} node A TemplateLiteral node to check. - * @returns {boolean} Whether or not the TemplateLiteral node is using any of the special features provided by template literal strings. - * @private - */ - function isUsingFeatureOfTemplateLiteral(node) { - const hasTag = - node.parent.type === "TaggedTemplateExpression" && - node === node.parent.quasi; - - if (hasTag) { - return true; - } - - const hasStringInterpolation = node.expressions.length > 0; - - if (hasStringInterpolation) { - return true; - } - - const isMultilineString = - node.quasis.length >= 1 && - UNESCAPED_LINEBREAK_PATTERN.test(node.quasis[0].value.raw); - - if (isMultilineString) { - return true; - } - - return false; - } - - return { - Literal(node) { - const val = node.value, - rawVal = node.raw; - - if (settings && typeof val === "string") { - let isValid = - (quoteOption === "backtick" && - isAllowedAsNonBacktick(node)) || - isJSXLiteral(node) || - astUtils.isSurroundedBy(rawVal, settings.quote); - - if (!isValid && avoidEscape) { - isValid = - astUtils.isSurroundedBy( - rawVal, - settings.alternateQuote, - ) && rawVal.includes(settings.quote); - } - - if (!isValid) { - context.report({ - node, - messageId: "wrongQuotes", - data: { - description: settings.description, - }, - fix(fixer) { - if ( - quoteOption === "backtick" && - astUtils.hasOctalOrNonOctalDecimalEscapeSequence( - rawVal, - ) - ) { - /* - * An octal or non-octal decimal escape sequence in a template literal would - * produce syntax error, even in non-strict mode. - */ - return null; - } - - return fixer.replaceText( - node, - settings.convert(node.raw), - ); - }, - }); - } - } - }, - - TemplateLiteral(node) { - // Don't throw an error if backticks are expected or a template literal feature is in use. - if ( - allowTemplateLiterals || - quoteOption === "backtick" || - isUsingFeatureOfTemplateLiteral(node) - ) { - return; - } - - context.report({ - node, - messageId: "wrongQuotes", - data: { - description: settings.description, - }, - fix(fixer) { - if ( - astUtils.isTopLevelExpressionStatement( - node.parent, - ) && - !astUtils.isParenthesised(sourceCode, node) - ) { - /* - * TemplateLiterals aren't actually directives, but fixing them might turn - * them into directives and change the behavior of the code. - */ - return null; - } - return fixer.replaceText( - node, - settings.convert(sourceCode.getText(node)), - ); - }, - }); - }, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Enforce the consistent use of either backticks, double, or single quotes", + recommended: false, + url: "https://eslint.org/docs/latest/rules/quotes" + }, + + fixable: "code", + + schema: [ + { + enum: ["single", "double", "backtick"] + }, + { + anyOf: [ + { + enum: ["avoid-escape"] + }, + { + type: "object", + properties: { + avoidEscape: { + type: "boolean" + }, + allowTemplateLiterals: { + type: "boolean" + } + }, + additionalProperties: false + } + ] + } + ], + + messages: { + wrongQuotes: "Strings must use {{description}}." + } + }, + + create(context) { + + const quoteOption = context.options[0], + settings = QUOTE_SETTINGS[quoteOption || "double"], + options = context.options[1], + allowTemplateLiterals = options && options.allowTemplateLiterals === true, + sourceCode = context.sourceCode; + let avoidEscape = options && options.avoidEscape === true; + + // deprecated + if (options === AVOID_ESCAPE) { + avoidEscape = true; + } + + /** + * Determines if a given node is part of JSX syntax. + * + * This function returns `true` in the following cases: + * + * - `
` ... If the literal is an attribute value, the parent of the literal is `JSXAttribute`. + * - `
foo
` ... If the literal is a text content, the parent of the literal is `JSXElement`. + * - `<>foo` ... If the literal is a text content, the parent of the literal is `JSXFragment`. + * + * In particular, this function returns `false` in the following cases: + * + * - `
` + * - `
{"foo"}
` + * + * In both cases, inside of the braces is handled as normal JavaScript. + * The braces are `JSXExpressionContainer` nodes. + * @param {ASTNode} node The Literal node to check. + * @returns {boolean} True if the node is a part of JSX, false if not. + * @private + */ + function isJSXLiteral(node) { + return node.parent.type === "JSXAttribute" || node.parent.type === "JSXElement" || node.parent.type === "JSXFragment"; + } + + /** + * Checks whether or not a given node is a directive. + * The directive is a `ExpressionStatement` which has only a string literal not surrounded by + * parentheses. + * @param {ASTNode} node A node to check. + * @returns {boolean} Whether or not the node is a directive. + * @private + */ + function isDirective(node) { + return ( + node.type === "ExpressionStatement" && + node.expression.type === "Literal" && + typeof node.expression.value === "string" && + !astUtils.isParenthesised(sourceCode, node.expression) + ); + } + + /** + * Checks whether a specified node is either part of, or immediately follows a (possibly empty) directive prologue. + * @see {@link http://www.ecma-international.org/ecma-262/6.0/#sec-directive-prologues-and-the-use-strict-directive} + * @param {ASTNode} node A node to check. + * @returns {boolean} Whether a specified node is either part of, or immediately follows a (possibly empty) directive prologue. + * @private + */ + function isExpressionInOrJustAfterDirectivePrologue(node) { + if (!astUtils.isTopLevelExpressionStatement(node.parent)) { + return false; + } + const block = node.parent.parent; + + // Check the node is at a prologue. + for (let i = 0; i < block.body.length; ++i) { + const statement = block.body[i]; + + if (statement === node.parent) { + return true; + } + if (!isDirective(statement)) { + break; + } + } + + return false; + } + + /** + * Checks whether or not a given node is allowed as non backtick. + * @param {ASTNode} node A node to check. + * @returns {boolean} Whether or not the node is allowed as non backtick. + * @private + */ + function isAllowedAsNonBacktick(node) { + const parent = node.parent; + + switch (parent.type) { + + // Directive Prologues. + case "ExpressionStatement": + return !astUtils.isParenthesised(sourceCode, node) && isExpressionInOrJustAfterDirectivePrologue(node); + + // LiteralPropertyName. + case "Property": + case "PropertyDefinition": + case "MethodDefinition": + return parent.key === node && !parent.computed; + + // ModuleSpecifier. + case "ImportDeclaration": + case "ExportNamedDeclaration": + return parent.source === node; + + // ModuleExportName or ModuleSpecifier. + case "ExportAllDeclaration": + return parent.exported === node || parent.source === node; + + // ModuleExportName. + case "ImportSpecifier": + return parent.imported === node; + + // ModuleExportName. + case "ExportSpecifier": + return parent.local === node || parent.exported === node; + + // Others don't allow. + default: + return false; + } + } + + /** + * Checks whether or not a given TemplateLiteral node is actually using any of the special features provided by template literal strings. + * @param {ASTNode} node A TemplateLiteral node to check. + * @returns {boolean} Whether or not the TemplateLiteral node is using any of the special features provided by template literal strings. + * @private + */ + function isUsingFeatureOfTemplateLiteral(node) { + const hasTag = node.parent.type === "TaggedTemplateExpression" && node === node.parent.quasi; + + if (hasTag) { + return true; + } + + const hasStringInterpolation = node.expressions.length > 0; + + if (hasStringInterpolation) { + return true; + } + + const isMultilineString = node.quasis.length >= 1 && UNESCAPED_LINEBREAK_PATTERN.test(node.quasis[0].value.raw); + + if (isMultilineString) { + return true; + } + + return false; + } + + return { + + Literal(node) { + const val = node.value, + rawVal = node.raw; + + if (settings && typeof val === "string") { + let isValid = (quoteOption === "backtick" && isAllowedAsNonBacktick(node)) || + isJSXLiteral(node) || + astUtils.isSurroundedBy(rawVal, settings.quote); + + if (!isValid && avoidEscape) { + isValid = astUtils.isSurroundedBy(rawVal, settings.alternateQuote) && rawVal.includes(settings.quote); + } + + if (!isValid) { + context.report({ + node, + messageId: "wrongQuotes", + data: { + description: settings.description + }, + fix(fixer) { + if (quoteOption === "backtick" && astUtils.hasOctalOrNonOctalDecimalEscapeSequence(rawVal)) { + + /* + * An octal or non-octal decimal escape sequence in a template literal would + * produce syntax error, even in non-strict mode. + */ + return null; + } + + return fixer.replaceText(node, settings.convert(node.raw)); + } + }); + } + } + }, + + TemplateLiteral(node) { + + // Don't throw an error if backticks are expected or a template literal feature is in use. + if ( + allowTemplateLiterals || + quoteOption === "backtick" || + isUsingFeatureOfTemplateLiteral(node) + ) { + return; + } + + context.report({ + node, + messageId: "wrongQuotes", + data: { + description: settings.description + }, + fix(fixer) { + if (astUtils.isTopLevelExpressionStatement(node.parent) && !astUtils.isParenthesised(sourceCode, node)) { + + /* + * TemplateLiterals aren't actually directives, but fixing them might turn + * them into directives and change the behavior of the code. + */ + return null; + } + return fixer.replaceText(node, settings.convert(sourceCode.getText(node))); + } + }); + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/radix.js b/node_modules/eslint/lib/rules/radix.js index b342636c4..7df97d986 100644 --- a/node_modules/eslint/lib/rules/radix.js +++ b/node_modules/eslint/lib/rules/radix.js @@ -16,11 +16,9 @@ const astUtils = require("./utils/ast-utils"); //------------------------------------------------------------------------------ const MODE_ALWAYS = "always", - MODE_AS_NEEDED = "as-needed"; + MODE_AS_NEEDED = "as-needed"; -const validRadixValues = new Set( - Array.from({ length: 37 - 2 }, (_, index) => index + 2), -); +const validRadixValues = new Set(Array.from({ length: 37 - 2 }, (_, index) => index + 2)); /** * Checks whether a given variable is shadowed or not. @@ -28,7 +26,7 @@ const validRadixValues = new Set( * @returns {boolean} `true` if the variable is shadowed. */ function isShadowed(variable) { - return variable.defs.length >= 1; + return variable.defs.length >= 1; } /** @@ -38,12 +36,12 @@ function isShadowed(variable) { * method. */ function isParseIntMethod(node) { - return ( - node.type === "MemberExpression" && - !node.computed && - node.property.type === "Identifier" && - node.property.name === "parseInt" - ); + return ( + node.type === "MemberExpression" && + !node.computed && + node.property.type === "Identifier" && + node.property.name === "parseInt" + ); } /** @@ -57,10 +55,10 @@ function isParseIntMethod(node) { * @returns {boolean} `true` if the node is valid. */ function isValidRadix(radix) { - return !( - (radix.type === "Literal" && !validRadixValues.has(radix.value)) || - (radix.type === "Identifier" && radix.name === "undefined") - ); + return !( + (radix.type === "Literal" && !validRadixValues.has(radix.value)) || + (radix.type === "Identifier" && radix.name === "undefined") + ); } /** @@ -69,148 +67,132 @@ function isValidRadix(radix) { * @returns {boolean} `true` if the node is the literal node of `10`. */ function isDefaultRadix(radix) { - return radix.type === "Literal" && radix.value === 10; + return radix.type === "Literal" && radix.value === 10; } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - defaultOptions: [MODE_ALWAYS], - - docs: { - description: - "Enforce the consistent use of the radix argument when using `parseInt()`", - recommended: false, - url: "https://eslint.org/docs/latest/rules/radix", - }, - - hasSuggestions: true, - - schema: [ - { - enum: ["always", "as-needed"], - }, - ], - - messages: { - missingParameters: "Missing parameters.", - redundantRadix: "Redundant radix parameter.", - missingRadix: "Missing radix parameter.", - invalidRadix: - "Invalid radix parameter, must be an integer between 2 and 36.", - addRadixParameter10: - "Add radix parameter `10` for parsing decimal numbers.", - }, - }, - - create(context) { - const [mode] = context.options; - const sourceCode = context.sourceCode; - - /** - * Checks the arguments of a given CallExpression node and reports it if it - * offends this rule. - * @param {ASTNode} node A CallExpression node to check. - * @returns {void} - */ - function checkArguments(node) { - const args = node.arguments; - - switch (args.length) { - case 0: - context.report({ - node, - messageId: "missingParameters", - }); - break; - - case 1: - if (mode === MODE_ALWAYS) { - context.report({ - node, - messageId: "missingRadix", - suggest: [ - { - messageId: "addRadixParameter10", - fix(fixer) { - const tokens = - sourceCode.getTokens(node); - const lastToken = tokens.at(-1); // Parenthesis. - const secondToLastToken = tokens.at(-2); // May or may not be a comma. - const hasTrailingComma = - secondToLastToken.type === - "Punctuator" && - secondToLastToken.value === ","; - - return fixer.insertTextBefore( - lastToken, - hasTrailingComma ? " 10," : ", 10", - ); - }, - }, - ], - }); - } - break; - - default: - if (mode === MODE_AS_NEEDED && isDefaultRadix(args[1])) { - context.report({ - node, - messageId: "redundantRadix", - }); - } else if (!isValidRadix(args[1])) { - context.report({ - node, - messageId: "invalidRadix", - }); - } - break; - } - } - - return { - "Program:exit"(node) { - const scope = sourceCode.getScope(node); - let variable; - - // Check `parseInt()` - variable = astUtils.getVariableByName(scope, "parseInt"); - if (variable && !isShadowed(variable)) { - variable.references.forEach(reference => { - const idNode = reference.identifier; - - if (astUtils.isCallee(idNode)) { - checkArguments(idNode.parent); - } - }); - } - - // Check `Number.parseInt()` - variable = astUtils.getVariableByName(scope, "Number"); - if (variable && !isShadowed(variable)) { - variable.references.forEach(reference => { - const parentNode = reference.identifier.parent; - const maybeCallee = - parentNode.parent.type === "ChainExpression" - ? parentNode.parent - : parentNode; - - if ( - isParseIntMethod(parentNode) && - astUtils.isCallee(maybeCallee) - ) { - checkArguments(maybeCallee.parent); - } - }); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Enforce the consistent use of the radix argument when using `parseInt()`", + recommended: false, + url: "https://eslint.org/docs/latest/rules/radix" + }, + + hasSuggestions: true, + + schema: [ + { + enum: ["always", "as-needed"] + } + ], + + messages: { + missingParameters: "Missing parameters.", + redundantRadix: "Redundant radix parameter.", + missingRadix: "Missing radix parameter.", + invalidRadix: "Invalid radix parameter, must be an integer between 2 and 36.", + addRadixParameter10: "Add radix parameter `10` for parsing decimal numbers." + } + }, + + create(context) { + const mode = context.options[0] || MODE_ALWAYS; + const sourceCode = context.sourceCode; + + /** + * Checks the arguments of a given CallExpression node and reports it if it + * offends this rule. + * @param {ASTNode} node A CallExpression node to check. + * @returns {void} + */ + function checkArguments(node) { + const args = node.arguments; + + switch (args.length) { + case 0: + context.report({ + node, + messageId: "missingParameters" + }); + break; + + case 1: + if (mode === MODE_ALWAYS) { + context.report({ + node, + messageId: "missingRadix", + suggest: [ + { + messageId: "addRadixParameter10", + fix(fixer) { + const tokens = sourceCode.getTokens(node); + const lastToken = tokens[tokens.length - 1]; // Parenthesis. + const secondToLastToken = tokens[tokens.length - 2]; // May or may not be a comma. + const hasTrailingComma = secondToLastToken.type === "Punctuator" && secondToLastToken.value === ","; + + return fixer.insertTextBefore(lastToken, hasTrailingComma ? " 10," : ", 10"); + } + } + ] + }); + } + break; + + default: + if (mode === MODE_AS_NEEDED && isDefaultRadix(args[1])) { + context.report({ + node, + messageId: "redundantRadix" + }); + } else if (!isValidRadix(args[1])) { + context.report({ + node, + messageId: "invalidRadix" + }); + } + break; + } + } + + return { + "Program:exit"(node) { + const scope = sourceCode.getScope(node); + let variable; + + // Check `parseInt()` + variable = astUtils.getVariableByName(scope, "parseInt"); + if (variable && !isShadowed(variable)) { + variable.references.forEach(reference => { + const idNode = reference.identifier; + + if (astUtils.isCallee(idNode)) { + checkArguments(idNode.parent); + } + }); + } + + // Check `Number.parseInt()` + variable = astUtils.getVariableByName(scope, "Number"); + if (variable && !isShadowed(variable)) { + variable.references.forEach(reference => { + const parentNode = reference.identifier.parent; + const maybeCallee = parentNode.parent.type === "ChainExpression" + ? parentNode.parent + : parentNode; + + if (isParseIntMethod(parentNode) && astUtils.isCallee(maybeCallee)) { + checkArguments(maybeCallee.parent); + } + }); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/require-atomic-updates.js b/node_modules/eslint/lib/rules/require-atomic-updates.js index c3935cca8..7e397ceb1 100644 --- a/node_modules/eslint/lib/rules/require-atomic-updates.js +++ b/node_modules/eslint/lib/rules/require-atomic-updates.js @@ -12,20 +12,20 @@ * @returns {Map} `referenceMap`. */ function createReferenceMap(scope, outReferenceMap = new Map()) { - for (const reference of scope.references) { - if (reference.resolved === null) { - continue; - } - - outReferenceMap.set(reference.identifier, reference); - } - for (const childScope of scope.childScopes) { - if (childScope.type !== "function") { - createReferenceMap(childScope, outReferenceMap); - } - } - - return outReferenceMap; + for (const reference of scope.references) { + if (reference.resolved === null) { + continue; + } + + outReferenceMap.set(reference.identifier, reference); + } + for (const childScope of scope.childScopes) { + if (childScope.type !== "function") { + createReferenceMap(childScope, outReferenceMap); + } + } + + return outReferenceMap; } /** @@ -35,26 +35,26 @@ function createReferenceMap(scope, outReferenceMap = new Map()) { * @returns {Expression|null} The `reference.writeExpr`. */ function getWriteExpr(reference) { - if (reference.writeExpr) { - return reference.writeExpr; - } - let node = reference.identifier; - - while (node) { - const t = node.parent.type; - - if (t === "AssignmentExpression" && node.parent.left === node) { - return node.parent.right; - } - if (t === "MemberExpression" && node.parent.object === node) { - node = node.parent; - continue; - } - - break; - } - - return null; + if (reference.writeExpr) { + return reference.writeExpr; + } + let node = reference.identifier; + + while (node) { + const t = node.parent.type; + + if (t === "AssignmentExpression" && node.parent.left === node) { + return node.parent.right; + } + if (t === "MemberExpression" && node.parent.object === node) { + node = node.parent; + continue; + } + + break; + } + + return null; } /** @@ -64,302 +64,268 @@ function getWriteExpr(reference) { * @returns {boolean} `true` if the variable is local to the given function, and is never referenced in a closure. */ function isLocalVariableWithoutEscape(variable, isMemberAccess) { - if (!variable) { - return false; // A global variable which was not defined. - } + if (!variable) { + return false; // A global variable which was not defined. + } - // If the reference is a property access and the variable is a parameter, it handles the variable is not local. - if (isMemberAccess && variable.defs.some(d => d.type === "Parameter")) { - return false; - } + // If the reference is a property access and the variable is a parameter, it handles the variable is not local. + if (isMemberAccess && variable.defs.some(d => d.type === "Parameter")) { + return false; + } - const functionScope = variable.scope.variableScope; + const functionScope = variable.scope.variableScope; - return variable.references.every( - reference => reference.from.variableScope === functionScope, - ); + return variable.references.every(reference => + reference.from.variableScope === functionScope); } /** * Represents segment information. */ class SegmentInfo { - constructor() { - this.info = new WeakMap(); - } - - /** - * Initialize the segment information. - * @param {PathSegment} segment The segment to initialize. - * @returns {void} - */ - initialize(segment) { - const outdatedReadVariables = new Set(); - const freshReadVariables = new Set(); - - for (const prevSegment of segment.prevSegments) { - const info = this.info.get(prevSegment); - - if (info) { - info.outdatedReadVariables.forEach( - Set.prototype.add, - outdatedReadVariables, - ); - info.freshReadVariables.forEach( - Set.prototype.add, - freshReadVariables, - ); - } - } - - this.info.set(segment, { outdatedReadVariables, freshReadVariables }); - } - - /** - * Mark a given variable as read on given segments. - * @param {PathSegment[]} segments The segments that it read the variable on. - * @param {Variable} variable The variable to be read. - * @returns {void} - */ - markAsRead(segments, variable) { - for (const segment of segments) { - const info = this.info.get(segment); - - if (info) { - info.freshReadVariables.add(variable); - - // If a variable is freshly read again, then it's no more out-dated. - info.outdatedReadVariables.delete(variable); - } - } - } - - /** - * Move `freshReadVariables` to `outdatedReadVariables`. - * @param {PathSegment[]} segments The segments to process. - * @returns {void} - */ - makeOutdated(segments) { - for (const segment of segments) { - const info = this.info.get(segment); - - if (info) { - info.freshReadVariables.forEach( - Set.prototype.add, - info.outdatedReadVariables, - ); - info.freshReadVariables.clear(); - } - } - } - - /** - * Check if a given variable is outdated on the current segments. - * @param {PathSegment[]} segments The current segments. - * @param {Variable} variable The variable to check. - * @returns {boolean} `true` if the variable is outdated on the segments. - */ - isOutdated(segments, variable) { - for (const segment of segments) { - const info = this.info.get(segment); - - if (info && info.outdatedReadVariables.has(variable)) { - return true; - } - } - return false; - } + constructor() { + this.info = new WeakMap(); + } + + /** + * Initialize the segment information. + * @param {PathSegment} segment The segment to initialize. + * @returns {void} + */ + initialize(segment) { + const outdatedReadVariables = new Set(); + const freshReadVariables = new Set(); + + for (const prevSegment of segment.prevSegments) { + const info = this.info.get(prevSegment); + + if (info) { + info.outdatedReadVariables.forEach(Set.prototype.add, outdatedReadVariables); + info.freshReadVariables.forEach(Set.prototype.add, freshReadVariables); + } + } + + this.info.set(segment, { outdatedReadVariables, freshReadVariables }); + } + + /** + * Mark a given variable as read on given segments. + * @param {PathSegment[]} segments The segments that it read the variable on. + * @param {Variable} variable The variable to be read. + * @returns {void} + */ + markAsRead(segments, variable) { + for (const segment of segments) { + const info = this.info.get(segment); + + if (info) { + info.freshReadVariables.add(variable); + + // If a variable is freshly read again, then it's no more out-dated. + info.outdatedReadVariables.delete(variable); + } + } + } + + /** + * Move `freshReadVariables` to `outdatedReadVariables`. + * @param {PathSegment[]} segments The segments to process. + * @returns {void} + */ + makeOutdated(segments) { + for (const segment of segments) { + const info = this.info.get(segment); + + if (info) { + info.freshReadVariables.forEach(Set.prototype.add, info.outdatedReadVariables); + info.freshReadVariables.clear(); + } + } + } + + /** + * Check if a given variable is outdated on the current segments. + * @param {PathSegment[]} segments The current segments. + * @param {Variable} variable The variable to check. + * @returns {boolean} `true` if the variable is outdated on the segments. + */ + isOutdated(segments, variable) { + for (const segment of segments) { + const info = this.info.get(segment); + + if (info && info.outdatedReadVariables.has(variable)) { + return true; + } + } + return false; + } } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - defaultOptions: [ - { - allowProperties: false, - }, - ], - - docs: { - description: - "Disallow assignments that can lead to race conditions due to usage of `await` or `yield`", - recommended: false, - url: "https://eslint.org/docs/latest/rules/require-atomic-updates", - }, - - fixable: null, - - schema: [ - { - type: "object", - properties: { - allowProperties: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - - messages: { - nonAtomicUpdate: - "Possible race condition: `{{value}}` might be reassigned based on an outdated value of `{{value}}`.", - nonAtomicObjectUpdate: - "Possible race condition: `{{value}}` might be assigned based on an outdated state of `{{object}}`.", - }, - }, - - create(context) { - const [{ allowProperties }] = context.options; - - const sourceCode = context.sourceCode; - const assignmentReferences = new Map(); - const segmentInfo = new SegmentInfo(); - let stack = null; - - return { - onCodePathStart(codePath, node) { - const scope = sourceCode.getScope(node); - const shouldVerify = - scope.type === "function" && - (scope.block.async || scope.block.generator); - - stack = { - upper: stack, - codePath, - referenceMap: shouldVerify - ? createReferenceMap(scope) - : null, - currentSegments: new Set(), - }; - }, - onCodePathEnd() { - stack = stack.upper; - }, - - // Initialize the segment information. - onCodePathSegmentStart(segment) { - segmentInfo.initialize(segment); - stack.currentSegments.add(segment); - }, - - onUnreachableCodePathSegmentStart(segment) { - stack.currentSegments.add(segment); - }, - - onUnreachableCodePathSegmentEnd(segment) { - stack.currentSegments.delete(segment); - }, - - onCodePathSegmentEnd(segment) { - stack.currentSegments.delete(segment); - }, - - // Handle references to prepare verification. - Identifier(node) { - const { referenceMap } = stack; - const reference = referenceMap && referenceMap.get(node); - - // Ignore if this is not a valid variable reference. - if (!reference) { - return; - } - const variable = reference.resolved; - const writeExpr = getWriteExpr(reference); - const isMemberAccess = - reference.identifier.parent.type === "MemberExpression"; - - // Add a fresh read variable. - if ( - reference.isRead() && - !(writeExpr && writeExpr.parent.operator === "=") - ) { - segmentInfo.markAsRead(stack.currentSegments, variable); - } - - /* - * Register the variable to verify after ESLint traversed the `writeExpr` node - * if this reference is an assignment to a variable which is referred from other closure. - */ - if ( - writeExpr && - writeExpr.parent.right === writeExpr && // ← exclude variable declarations. - !isLocalVariableWithoutEscape(variable, isMemberAccess) - ) { - let refs = assignmentReferences.get(writeExpr); - - if (!refs) { - refs = []; - assignmentReferences.set(writeExpr, refs); - } - - refs.push(reference); - } - }, - - /* - * Verify assignments. - * If the reference exists in `outdatedReadVariables` list, report it. - */ - ":expression:exit"(node) { - // referenceMap exists if this is in a resumable function scope. - if (!stack.referenceMap) { - return; - } - - // Mark the read variables on this code path as outdated. - if ( - node.type === "AwaitExpression" || - node.type === "YieldExpression" - ) { - segmentInfo.makeOutdated(stack.currentSegments); - } - - // Verify. - const references = assignmentReferences.get(node); - - if (references) { - assignmentReferences.delete(node); - - for (const reference of references) { - const variable = reference.resolved; - - if ( - segmentInfo.isOutdated( - stack.currentSegments, - variable, - ) - ) { - if (node.parent.left === reference.identifier) { - context.report({ - node: node.parent, - messageId: "nonAtomicUpdate", - data: { - value: variable.name, - }, - }); - } else if (!allowProperties) { - context.report({ - node: node.parent, - messageId: "nonAtomicObjectUpdate", - data: { - value: sourceCode.getText( - node.parent.left, - ), - object: variable.name, - }, - }); - } - } - } - } - }, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Disallow assignments that can lead to race conditions due to usage of `await` or `yield`", + recommended: false, + url: "https://eslint.org/docs/latest/rules/require-atomic-updates" + }, + + fixable: null, + + schema: [{ + type: "object", + properties: { + allowProperties: { + type: "boolean", + default: false + } + }, + additionalProperties: false + }], + + messages: { + nonAtomicUpdate: "Possible race condition: `{{value}}` might be reassigned based on an outdated value of `{{value}}`.", + nonAtomicObjectUpdate: "Possible race condition: `{{value}}` might be assigned based on an outdated state of `{{object}}`." + } + }, + + create(context) { + const allowProperties = !!context.options[0] && context.options[0].allowProperties; + + const sourceCode = context.sourceCode; + const assignmentReferences = new Map(); + const segmentInfo = new SegmentInfo(); + let stack = null; + + return { + onCodePathStart(codePath, node) { + const scope = sourceCode.getScope(node); + const shouldVerify = + scope.type === "function" && + (scope.block.async || scope.block.generator); + + stack = { + upper: stack, + codePath, + referenceMap: shouldVerify ? createReferenceMap(scope) : null, + currentSegments: new Set() + }; + }, + onCodePathEnd() { + stack = stack.upper; + }, + + // Initialize the segment information. + onCodePathSegmentStart(segment) { + segmentInfo.initialize(segment); + stack.currentSegments.add(segment); + }, + + onUnreachableCodePathSegmentStart(segment) { + stack.currentSegments.add(segment); + }, + + onUnreachableCodePathSegmentEnd(segment) { + stack.currentSegments.delete(segment); + }, + + onCodePathSegmentEnd(segment) { + stack.currentSegments.delete(segment); + }, + + + // Handle references to prepare verification. + Identifier(node) { + const { referenceMap } = stack; + const reference = referenceMap && referenceMap.get(node); + + // Ignore if this is not a valid variable reference. + if (!reference) { + return; + } + const variable = reference.resolved; + const writeExpr = getWriteExpr(reference); + const isMemberAccess = reference.identifier.parent.type === "MemberExpression"; + + // Add a fresh read variable. + if (reference.isRead() && !(writeExpr && writeExpr.parent.operator === "=")) { + segmentInfo.markAsRead(stack.currentSegments, variable); + } + + /* + * Register the variable to verify after ESLint traversed the `writeExpr` node + * if this reference is an assignment to a variable which is referred from other closure. + */ + if (writeExpr && + writeExpr.parent.right === writeExpr && // ← exclude variable declarations. + !isLocalVariableWithoutEscape(variable, isMemberAccess) + ) { + let refs = assignmentReferences.get(writeExpr); + + if (!refs) { + refs = []; + assignmentReferences.set(writeExpr, refs); + } + + refs.push(reference); + } + }, + + /* + * Verify assignments. + * If the reference exists in `outdatedReadVariables` list, report it. + */ + ":expression:exit"(node) { + + // referenceMap exists if this is in a resumable function scope. + if (!stack.referenceMap) { + return; + } + + // Mark the read variables on this code path as outdated. + if (node.type === "AwaitExpression" || node.type === "YieldExpression") { + segmentInfo.makeOutdated(stack.currentSegments); + } + + // Verify. + const references = assignmentReferences.get(node); + + if (references) { + assignmentReferences.delete(node); + + for (const reference of references) { + const variable = reference.resolved; + + if (segmentInfo.isOutdated(stack.currentSegments, variable)) { + if (node.parent.left === reference.identifier) { + context.report({ + node: node.parent, + messageId: "nonAtomicUpdate", + data: { + value: variable.name + } + }); + } else if (!allowProperties) { + context.report({ + node: node.parent, + messageId: "nonAtomicObjectUpdate", + data: { + value: sourceCode.getText(node.parent.left), + object: variable.name + } + }); + } + + } + } + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/require-await.js b/node_modules/eslint/lib/rules/require-await.js index 7f3194979..952dde543 100644 --- a/node_modules/eslint/lib/rules/require-await.js +++ b/node_modules/eslint/lib/rules/require-await.js @@ -21,164 +21,93 @@ const astUtils = require("./utils/ast-utils"); * @returns {string} The text that the 1st letter was capitalized. */ function capitalizeFirstLetter(text) { - return text[0].toUpperCase() + text.slice(1); + return text[0].toUpperCase() + text.slice(1); } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: - "Disallow async functions which have no `await` expression", - recommended: false, - url: "https://eslint.org/docs/latest/rules/require-await", - }, - - schema: [], - - messages: { - missingAwait: "{{name}} has no 'await' expression.", - removeAsync: "Remove 'async'.", - }, - - hasSuggestions: true, - }, - - create(context) { - const sourceCode = context.sourceCode; - let scopeInfo = null; - - /** - * Push the scope info object to the stack. - * @returns {void} - */ - function enterFunction() { - scopeInfo = { - upper: scopeInfo, - hasAwait: false, - }; - } - - /** - * Pop the top scope info object from the stack. - * Also, it reports the function if needed. - * @param {ASTNode} node The node to report. - * @returns {void} - */ - function exitFunction(node) { - if ( - !node.generator && - node.async && - !scopeInfo.hasAwait && - !astUtils.isEmptyFunction(node) - ) { - /* - * If the function belongs to a method definition or - * property, then the function's range may not include the - * `async` keyword and we should look at the parent instead. - */ - const nodeWithAsyncKeyword = - (node.parent.type === "MethodDefinition" && - node.parent.value === node) || - (node.parent.type === "Property" && - node.parent.method && - node.parent.value === node) - ? node.parent - : node; - - const asyncToken = sourceCode.getFirstToken( - nodeWithAsyncKeyword, - token => token.value === "async", - ); - const asyncRange = [ - asyncToken.range[0], - sourceCode.getTokenAfter(asyncToken, { - includeComments: true, - }).range[0], - ]; - - /* - * Removing the `async` keyword can cause parsing errors if the current - * statement is relying on automatic semicolon insertion. If ASI is currently - * being used, then we should replace the `async` keyword with a semicolon. - */ - const nextToken = sourceCode.getTokenAfter(asyncToken); - const addSemiColon = - nextToken.type === "Punctuator" && - (nextToken.value === "[" || nextToken.value === "(") && - (nodeWithAsyncKeyword.type === "MethodDefinition" || - astUtils.isStartOfExpressionStatement( - nodeWithAsyncKeyword, - )) && - astUtils.needsPrecedingSemicolon( - sourceCode, - nodeWithAsyncKeyword, - ); - - context.report({ - node, - loc: astUtils.getFunctionHeadLoc(node, sourceCode), - messageId: "missingAwait", - data: { - name: capitalizeFirstLetter( - astUtils.getFunctionNameWithKind(node), - ), - }, - suggest: [ - { - messageId: "removeAsync", - fix: fixer => - fixer.replaceTextRange( - asyncRange, - addSemiColon ? ";" : "", - ), - }, - ], - }); - } - - scopeInfo = scopeInfo.upper; - } - - return { - FunctionDeclaration: enterFunction, - FunctionExpression: enterFunction, - ArrowFunctionExpression: enterFunction, - "FunctionDeclaration:exit": exitFunction, - "FunctionExpression:exit": exitFunction, - "ArrowFunctionExpression:exit": exitFunction, - - AwaitExpression() { - if (!scopeInfo) { - return; - } - - scopeInfo.hasAwait = true; - }, - ForOfStatement(node) { - if (!scopeInfo) { - return; - } - - if (node.await) { - scopeInfo.hasAwait = true; - } - }, - VariableDeclaration(node) { - if (!scopeInfo) { - return; - } - - if (node.kind === "await using") { - scopeInfo.hasAwait = true; - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Disallow async functions which have no `await` expression", + recommended: false, + url: "https://eslint.org/docs/latest/rules/require-await" + }, + + schema: [], + + messages: { + missingAwait: "{{name}} has no 'await' expression." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + let scopeInfo = null; + + /** + * Push the scope info object to the stack. + * @returns {void} + */ + function enterFunction() { + scopeInfo = { + upper: scopeInfo, + hasAwait: false + }; + } + + /** + * Pop the top scope info object from the stack. + * Also, it reports the function if needed. + * @param {ASTNode} node The node to report. + * @returns {void} + */ + function exitFunction(node) { + if (!node.generator && node.async && !scopeInfo.hasAwait && !astUtils.isEmptyFunction(node)) { + context.report({ + node, + loc: astUtils.getFunctionHeadLoc(node, sourceCode), + messageId: "missingAwait", + data: { + name: capitalizeFirstLetter( + astUtils.getFunctionNameWithKind(node) + ) + } + }); + } + + scopeInfo = scopeInfo.upper; + } + + return { + FunctionDeclaration: enterFunction, + FunctionExpression: enterFunction, + ArrowFunctionExpression: enterFunction, + "FunctionDeclaration:exit": exitFunction, + "FunctionExpression:exit": exitFunction, + "ArrowFunctionExpression:exit": exitFunction, + + AwaitExpression() { + if (!scopeInfo) { + return; + } + + scopeInfo.hasAwait = true; + }, + ForOfStatement(node) { + if (!scopeInfo) { + return; + } + + if (node.await) { + scopeInfo.hasAwait = true; + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/require-unicode-regexp.js b/node_modules/eslint/lib/rules/require-unicode-regexp.js index fd039afe1..dd687f8d7 100644 --- a/node_modules/eslint/lib/rules/require-unicode-regexp.js +++ b/node_modules/eslint/lib/rules/require-unicode-regexp.js @@ -1,5 +1,5 @@ /** - * @fileoverview Rule to enforce the use of `u` or `v` flag on regular expressions. + * @fileoverview Rule to enforce the use of `u` flag on RegExp. * @author Toru Nagashima */ @@ -10,308 +10,120 @@ //------------------------------------------------------------------------------ const { - CALL, - CONSTRUCT, - ReferenceTracker, - getStringIfConstant, + CALL, + CONSTRUCT, + ReferenceTracker, + getStringIfConstant } = require("@eslint-community/eslint-utils"); const astUtils = require("./utils/ast-utils.js"); const { isValidWithUnicodeFlag } = require("./utils/regular-expressions"); -/** - * Checks whether the flag configuration should be treated as a missing flag. - * @param {"u"|"v"|undefined} requireFlag A particular flag to require - * @param {string} flags The regex flags - * @returns {boolean} Whether the flag configuration results in a missing flag. - */ -function checkFlags(requireFlag, flags) { - let missingFlag; - - if (requireFlag === "v") { - missingFlag = !flags.includes("v"); - } else if (requireFlag === "u") { - missingFlag = !flags.includes("u"); - } else { - missingFlag = !flags.includes("u") && !flags.includes("v"); - } - - return missingFlag; -} - //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - defaultOptions: [{}], - - docs: { - description: - "Enforce the use of `u` or `v` flag on regular expressions", - recommended: false, - url: "https://eslint.org/docs/latest/rules/require-unicode-regexp", - }, - - hasSuggestions: true, - - messages: { - addUFlag: "Add the 'u' flag.", - addVFlag: "Add the 'v' flag.", - requireUFlag: "Use the 'u' flag.", - requireVFlag: "Use the 'v' flag.", - }, - - schema: [ - { - type: "object", - properties: { - requireFlag: { - enum: ["u", "v"], - }, - }, - additionalProperties: false, - }, - ], - }, - - create(context) { - const sourceCode = context.sourceCode; - - const [{ requireFlag }] = context.options; - - return { - "Literal[regex]"(node) { - const flags = node.regex.flags || ""; - - const missingFlag = checkFlags(requireFlag, flags); - - if (missingFlag) { - context.report({ - messageId: - requireFlag === "v" - ? "requireVFlag" - : "requireUFlag", - node, - suggest: isValidWithUnicodeFlag( - context.languageOptions.ecmaVersion, - node.regex.pattern, - requireFlag, - ) - ? [ - { - fix(fixer) { - const replaceFlag = - requireFlag ?? "u"; - const regex = - sourceCode.getText(node); - const slashPos = - regex.lastIndexOf("/"); - - if (requireFlag) { - const flag = - requireFlag === "u" - ? "v" - : "u"; - - if ( - regex.includes( - flag, - slashPos, - ) - ) { - return fixer.replaceText( - node, - regex.slice( - 0, - slashPos, - ) + - regex - .slice(slashPos) - .replace( - flag, - requireFlag, - ), - ); - } - } - - return fixer.insertTextAfter( - node, - replaceFlag, - ); - }, - messageId: - requireFlag === "v" - ? "addVFlag" - : "addUFlag", - }, - ] - : null, - }); - } - }, - - Program(node) { - const scope = sourceCode.getScope(node); - const tracker = new ReferenceTracker(scope); - const trackMap = { - RegExp: { [CALL]: true, [CONSTRUCT]: true }, - }; - - for (const { node: refNode } of tracker.iterateGlobalReferences( - trackMap, - )) { - const [patternNode, flagsNode] = refNode.arguments; - - if (patternNode && patternNode.type === "SpreadElement") { - continue; - } - const pattern = getStringIfConstant(patternNode, scope); - const flags = getStringIfConstant(flagsNode, scope); - - let missingFlag = !flagsNode; - - if (typeof flags === "string") { - missingFlag = checkFlags(requireFlag, flags); - } - - if (missingFlag) { - context.report({ - messageId: - requireFlag === "v" - ? "requireVFlag" - : "requireUFlag", - node: refNode, - suggest: - typeof pattern === "string" && - isValidWithUnicodeFlag( - context.languageOptions.ecmaVersion, - pattern, - requireFlag, - ) - ? [ - { - fix(fixer) { - const replaceFlag = - requireFlag ?? "u"; - - if (flagsNode) { - if ( - (flagsNode.type === - "Literal" && - typeof flagsNode.value === - "string") || - flagsNode.type === - "TemplateLiteral" - ) { - const flagsNodeText = - sourceCode.getText( - flagsNode, - ); - const flag = - requireFlag === - "u" - ? "v" - : "u"; - - if ( - flags.includes( - flag, - ) - ) { - // Avoid replacing "u" in escapes like `\uXXXX` - if ( - flagsNode.type === - "Literal" && - flagsNode.raw.includes( - "\\", - ) - ) { - return null; - } - - // Avoid replacing "u" in expressions like "`${regularFlags}g`" - if ( - flagsNode.type === - "TemplateLiteral" && - (flagsNode - .expressions - .length || - flagsNode.quasis.some( - ({ - value: { - raw, - }, - }) => - raw.includes( - "\\", - ), - )) - ) { - return null; - } - - return fixer.replaceText( - flagsNode, - flagsNodeText.replace( - flag, - replaceFlag, - ), - ); - } - - return fixer.replaceText( - flagsNode, - [ - flagsNodeText.slice( - 0, - flagsNodeText.length - - 1, - ), - flagsNodeText.slice( - flagsNodeText.length - - 1, - ), - ].join( - replaceFlag, - ), - ); - } - - // We intentionally don't suggest concatenating + "u" to non-literals - return null; - } - - const penultimateToken = - sourceCode.getLastToken( - refNode, - { skip: 1 }, - ); // skip closing parenthesis - - return fixer.insertTextAfter( - penultimateToken, - astUtils.isCommaToken( - penultimateToken, - ) - ? ` "${replaceFlag}",` - : `, "${replaceFlag}"`, - ); - }, - messageId: - requireFlag === "v" - ? "addVFlag" - : "addUFlag", - }, - ] - : null, - }); - } - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Enforce the use of `u` or `v` flag on RegExp", + recommended: false, + url: "https://eslint.org/docs/latest/rules/require-unicode-regexp" + }, + + hasSuggestions: true, + + messages: { + addUFlag: "Add the 'u' flag.", + requireUFlag: "Use the 'u' flag." + }, + + schema: [] + }, + + create(context) { + + const sourceCode = context.sourceCode; + + return { + "Literal[regex]"(node) { + const flags = node.regex.flags || ""; + + if (!flags.includes("u") && !flags.includes("v")) { + context.report({ + messageId: "requireUFlag", + node, + suggest: isValidWithUnicodeFlag(context.languageOptions.ecmaVersion, node.regex.pattern) + ? [ + { + fix(fixer) { + return fixer.insertTextAfter(node, "u"); + }, + messageId: "addUFlag" + } + ] + : null + }); + } + }, + + Program(node) { + const scope = sourceCode.getScope(node); + const tracker = new ReferenceTracker(scope); + const trackMap = { + RegExp: { [CALL]: true, [CONSTRUCT]: true } + }; + + for (const { node: refNode } of tracker.iterateGlobalReferences(trackMap)) { + const [patternNode, flagsNode] = refNode.arguments; + + if (patternNode && patternNode.type === "SpreadElement") { + continue; + } + const pattern = getStringIfConstant(patternNode, scope); + const flags = getStringIfConstant(flagsNode, scope); + + if (!flagsNode || (typeof flags === "string" && !flags.includes("u") && !flags.includes("v"))) { + context.report({ + messageId: "requireUFlag", + node: refNode, + suggest: typeof pattern === "string" && isValidWithUnicodeFlag(context.languageOptions.ecmaVersion, pattern) + ? [ + { + fix(fixer) { + if (flagsNode) { + if ((flagsNode.type === "Literal" && typeof flagsNode.value === "string") || flagsNode.type === "TemplateLiteral") { + const flagsNodeText = sourceCode.getText(flagsNode); + + return fixer.replaceText(flagsNode, [ + flagsNodeText.slice(0, flagsNodeText.length - 1), + flagsNodeText.slice(flagsNodeText.length - 1) + ].join("u")); + } + + // We intentionally don't suggest concatenating + "u" to non-literals + return null; + } + + const penultimateToken = sourceCode.getLastToken(refNode, { skip: 1 }); // skip closing parenthesis + + return fixer.insertTextAfter( + penultimateToken, + astUtils.isCommaToken(penultimateToken) + ? ' "u",' + : ', "u"' + ); + }, + messageId: "addUFlag" + } + ] + : null + }); + } + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/require-yield.js b/node_modules/eslint/lib/rules/require-yield.js index 0cc8d20cb..f801af0ab 100644 --- a/node_modules/eslint/lib/rules/require-yield.js +++ b/node_modules/eslint/lib/rules/require-yield.js @@ -9,68 +9,69 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", + meta: { + type: "suggestion", - docs: { - description: "Require generator functions to contain `yield`", - recommended: true, - url: "https://eslint.org/docs/latest/rules/require-yield", - }, + docs: { + description: "Require generator functions to contain `yield`", + recommended: true, + url: "https://eslint.org/docs/latest/rules/require-yield" + }, - schema: [], + schema: [], - messages: { - missingYield: "This generator function does not have 'yield'.", - }, - }, + messages: { + missingYield: "This generator function does not have 'yield'." + } + }, - create(context) { - const stack = []; + create(context) { + const stack = []; - /** - * If the node is a generator function, start counting `yield` keywords. - * @param {Node} node A function node to check. - * @returns {void} - */ - function beginChecking(node) { - if (node.generator) { - stack.push(0); - } - } + /** + * If the node is a generator function, start counting `yield` keywords. + * @param {Node} node A function node to check. + * @returns {void} + */ + function beginChecking(node) { + if (node.generator) { + stack.push(0); + } + } - /** - * If the node is a generator function, end counting `yield` keywords, then - * reports result. - * @param {Node} node A function node to check. - * @returns {void} - */ - function endChecking(node) { - if (!node.generator) { - return; - } + /** + * If the node is a generator function, end counting `yield` keywords, then + * reports result. + * @param {Node} node A function node to check. + * @returns {void} + */ + function endChecking(node) { + if (!node.generator) { + return; + } - const countYield = stack.pop(); + const countYield = stack.pop(); - if (countYield === 0 && node.body.body.length > 0) { - context.report({ node, messageId: "missingYield" }); - } - } + if (countYield === 0 && node.body.body.length > 0) { + context.report({ node, messageId: "missingYield" }); + } + } - return { - FunctionDeclaration: beginChecking, - "FunctionDeclaration:exit": endChecking, - FunctionExpression: beginChecking, - "FunctionExpression:exit": endChecking, + return { + FunctionDeclaration: beginChecking, + "FunctionDeclaration:exit": endChecking, + FunctionExpression: beginChecking, + "FunctionExpression:exit": endChecking, - // Increases the count of `yield` keyword. - YieldExpression() { - if (stack.length > 0) { - stack[stack.length - 1] += 1; - } - }, - }; - }, + // Increases the count of `yield` keyword. + YieldExpression() { + + if (stack.length > 0) { + stack[stack.length - 1] += 1; + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/rest-spread-spacing.js b/node_modules/eslint/lib/rules/rest-spread-spacing.js index d9b6c74ad..287e56f01 100644 --- a/node_modules/eslint/lib/rules/rest-spread-spacing.js +++ b/node_modules/eslint/lib/rules/rest-spread-spacing.js @@ -10,144 +10,114 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "rest-spread-spacing", - url: "https://eslint.style/rules/rest-spread-spacing", - }, - }, - ], - }, - type: "layout", + meta: { + deprecated: true, + replacedBy: [], + type: "layout", - docs: { - description: - "Enforce spacing between rest and spread operators and their expressions", - recommended: false, - url: "https://eslint.org/docs/latest/rules/rest-spread-spacing", - }, + docs: { + description: "Enforce spacing between rest and spread operators and their expressions", + recommended: false, + url: "https://eslint.org/docs/latest/rules/rest-spread-spacing" + }, - fixable: "whitespace", + fixable: "whitespace", - schema: [ - { - enum: ["always", "never"], - }, - ], + schema: [ + { + enum: ["always", "never"] + } + ], - messages: { - unexpectedWhitespace: - "Unexpected whitespace after {{type}} operator.", - expectedWhitespace: "Expected whitespace after {{type}} operator.", - }, - }, + messages: { + unexpectedWhitespace: "Unexpected whitespace after {{type}} operator.", + expectedWhitespace: "Expected whitespace after {{type}} operator." + } + }, - create(context) { - const sourceCode = context.sourceCode, - alwaysSpace = context.options[0] === "always"; + create(context) { + const sourceCode = context.sourceCode, + alwaysSpace = context.options[0] === "always"; - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- - /** - * Checks whitespace between rest/spread operators and their expressions - * @param {ASTNode} node The node to check - * @returns {void} - */ - function checkWhiteSpace(node) { - const operator = sourceCode.getFirstToken(node), - nextToken = sourceCode.getTokenAfter(operator), - hasWhitespace = sourceCode.isSpaceBetweenTokens( - operator, - nextToken, - ); - let type; + /** + * Checks whitespace between rest/spread operators and their expressions + * @param {ASTNode} node The node to check + * @returns {void} + */ + function checkWhiteSpace(node) { + const operator = sourceCode.getFirstToken(node), + nextToken = sourceCode.getTokenAfter(operator), + hasWhitespace = sourceCode.isSpaceBetweenTokens(operator, nextToken); + let type; - switch (node.type) { - case "SpreadElement": - type = "spread"; - if (node.parent.type === "ObjectExpression") { - type += " property"; - } - break; - case "RestElement": - type = "rest"; - if (node.parent.type === "ObjectPattern") { - type += " property"; - } - break; - case "ExperimentalSpreadProperty": - type = "spread property"; - break; - case "ExperimentalRestProperty": - type = "rest property"; - break; - default: - return; - } + switch (node.type) { + case "SpreadElement": + type = "spread"; + if (node.parent.type === "ObjectExpression") { + type += " property"; + } + break; + case "RestElement": + type = "rest"; + if (node.parent.type === "ObjectPattern") { + type += " property"; + } + break; + case "ExperimentalSpreadProperty": + type = "spread property"; + break; + case "ExperimentalRestProperty": + type = "rest property"; + break; + default: + return; + } - if (alwaysSpace && !hasWhitespace) { - context.report({ - node, - loc: operator.loc, - messageId: "expectedWhitespace", - data: { - type, - }, - fix(fixer) { - return fixer.replaceTextRange( - [operator.range[1], nextToken.range[0]], - " ", - ); - }, - }); - } else if (!alwaysSpace && hasWhitespace) { - context.report({ - node, - loc: { - start: operator.loc.end, - end: nextToken.loc.start, - }, - messageId: "unexpectedWhitespace", - data: { - type, - }, - fix(fixer) { - return fixer.removeRange([ - operator.range[1], - nextToken.range[0], - ]); - }, - }); - } - } + if (alwaysSpace && !hasWhitespace) { + context.report({ + node, + loc: operator.loc, + messageId: "expectedWhitespace", + data: { + type + }, + fix(fixer) { + return fixer.replaceTextRange([operator.range[1], nextToken.range[0]], " "); + } + }); + } else if (!alwaysSpace && hasWhitespace) { + context.report({ + node, + loc: { + start: operator.loc.end, + end: nextToken.loc.start + }, + messageId: "unexpectedWhitespace", + data: { + type + }, + fix(fixer) { + return fixer.removeRange([operator.range[1], nextToken.range[0]]); + } + }); + } + } - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- - return { - SpreadElement: checkWhiteSpace, - RestElement: checkWhiteSpace, - ExperimentalSpreadProperty: checkWhiteSpace, - ExperimentalRestProperty: checkWhiteSpace, - }; - }, + return { + SpreadElement: checkWhiteSpace, + RestElement: checkWhiteSpace, + ExperimentalSpreadProperty: checkWhiteSpace, + ExperimentalRestProperty: checkWhiteSpace + }; + } }; diff --git a/node_modules/eslint/lib/rules/semi-spacing.js b/node_modules/eslint/lib/rules/semi-spacing.js index f8b9b6621..35a49d2c2 100644 --- a/node_modules/eslint/lib/rules/semi-spacing.js +++ b/node_modules/eslint/lib/rules/semi-spacing.js @@ -12,286 +12,237 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "semi-spacing", - url: "https://eslint.style/rules/semi-spacing", - }, - }, - ], - }, - type: "layout", - - docs: { - description: - "Enforce consistent spacing before and after semicolons", - recommended: false, - url: "https://eslint.org/docs/latest/rules/semi-spacing", - }, - - fixable: "whitespace", - - schema: [ - { - type: "object", - properties: { - before: { - type: "boolean", - default: false, - }, - after: { - type: "boolean", - default: true, - }, - }, - additionalProperties: false, - }, - ], - - messages: { - unexpectedWhitespaceBefore: - "Unexpected whitespace before semicolon.", - unexpectedWhitespaceAfter: "Unexpected whitespace after semicolon.", - missingWhitespaceBefore: "Missing whitespace before semicolon.", - missingWhitespaceAfter: "Missing whitespace after semicolon.", - }, - }, - - create(context) { - const config = context.options[0], - sourceCode = context.sourceCode; - let requireSpaceBefore = false, - requireSpaceAfter = true; - - if (typeof config === "object") { - requireSpaceBefore = config.before; - requireSpaceAfter = config.after; - } - - /** - * Checks if a given token has leading whitespace. - * @param {Object} token The token to check. - * @returns {boolean} True if the given token has leading space, false if not. - */ - function hasLeadingSpace(token) { - const tokenBefore = sourceCode.getTokenBefore(token); - - return ( - tokenBefore && - astUtils.isTokenOnSameLine(tokenBefore, token) && - sourceCode.isSpaceBetweenTokens(tokenBefore, token) - ); - } - - /** - * Checks if a given token has trailing whitespace. - * @param {Object} token The token to check. - * @returns {boolean} True if the given token has trailing space, false if not. - */ - function hasTrailingSpace(token) { - const tokenAfter = sourceCode.getTokenAfter(token); - - return ( - tokenAfter && - astUtils.isTokenOnSameLine(token, tokenAfter) && - sourceCode.isSpaceBetweenTokens(token, tokenAfter) - ); - } - - /** - * Checks if the given token is the last token in its line. - * @param {Token} token The token to check. - * @returns {boolean} Whether or not the token is the last in its line. - */ - function isLastTokenInCurrentLine(token) { - const tokenAfter = sourceCode.getTokenAfter(token); - - return !( - tokenAfter && astUtils.isTokenOnSameLine(token, tokenAfter) - ); - } - - /** - * Checks if the given token is the first token in its line - * @param {Token} token The token to check. - * @returns {boolean} Whether or not the token is the first in its line. - */ - function isFirstTokenInCurrentLine(token) { - const tokenBefore = sourceCode.getTokenBefore(token); - - return !( - tokenBefore && astUtils.isTokenOnSameLine(token, tokenBefore) - ); - } - - /** - * Checks if the next token of a given token is a closing parenthesis. - * @param {Token} token The token to check. - * @returns {boolean} Whether or not the next token of a given token is a closing parenthesis. - */ - function isBeforeClosingParen(token) { - const nextToken = sourceCode.getTokenAfter(token); - - return ( - (nextToken && astUtils.isClosingBraceToken(nextToken)) || - astUtils.isClosingParenToken(nextToken) - ); - } - - /** - * Report location example : - * - * for unexpected space `before` - * - * var a = 'b' ; - * ^^^ - * - * for unexpected space `after` - * - * var a = 'b'; c = 10; - * ^^ - * - * Reports if the given token has invalid spacing. - * @param {Token} token The semicolon token to check. - * @param {ASTNode} node The corresponding node of the token. - * @returns {void} - */ - function checkSemicolonSpacing(token, node) { - if (astUtils.isSemicolonToken(token)) { - if (hasLeadingSpace(token)) { - if (!requireSpaceBefore) { - const tokenBefore = sourceCode.getTokenBefore(token); - const loc = { - start: tokenBefore.loc.end, - end: token.loc.start, - }; - - context.report({ - node, - loc, - messageId: "unexpectedWhitespaceBefore", - fix(fixer) { - return fixer.removeRange([ - tokenBefore.range[1], - token.range[0], - ]); - }, - }); - } - } else { - if (requireSpaceBefore) { - const loc = token.loc; - - context.report({ - node, - loc, - messageId: "missingWhitespaceBefore", - fix(fixer) { - return fixer.insertTextBefore(token, " "); - }, - }); - } - } - - if ( - !isFirstTokenInCurrentLine(token) && - !isLastTokenInCurrentLine(token) && - !isBeforeClosingParen(token) - ) { - if (hasTrailingSpace(token)) { - if (!requireSpaceAfter) { - const tokenAfter = sourceCode.getTokenAfter(token); - const loc = { - start: token.loc.end, - end: tokenAfter.loc.start, - }; - - context.report({ - node, - loc, - messageId: "unexpectedWhitespaceAfter", - fix(fixer) { - return fixer.removeRange([ - token.range[1], - tokenAfter.range[0], - ]); - }, - }); - } - } else { - if (requireSpaceAfter) { - const loc = token.loc; - - context.report({ - node, - loc, - messageId: "missingWhitespaceAfter", - fix(fixer) { - return fixer.insertTextAfter(token, " "); - }, - }); - } - } - } - } - } - - /** - * Checks the spacing of the semicolon with the assumption that the last token is the semicolon. - * @param {ASTNode} node The node to check. - * @returns {void} - */ - function checkNode(node) { - const token = sourceCode.getLastToken(node); - - checkSemicolonSpacing(token, node); - } - - return { - VariableDeclaration: checkNode, - ExpressionStatement: checkNode, - BreakStatement: checkNode, - ContinueStatement: checkNode, - DebuggerStatement: checkNode, - DoWhileStatement: checkNode, - ReturnStatement: checkNode, - ThrowStatement: checkNode, - ImportDeclaration: checkNode, - ExportNamedDeclaration: checkNode, - ExportAllDeclaration: checkNode, - ExportDefaultDeclaration: checkNode, - ForStatement(node) { - if (node.init) { - checkSemicolonSpacing( - sourceCode.getTokenAfter(node.init), - node, - ); - } - - if (node.test) { - checkSemicolonSpacing( - sourceCode.getTokenAfter(node.test), - node, - ); - } - }, - PropertyDefinition: checkNode, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Enforce consistent spacing before and after semicolons", + recommended: false, + url: "https://eslint.org/docs/latest/rules/semi-spacing" + }, + + fixable: "whitespace", + + schema: [ + { + type: "object", + properties: { + before: { + type: "boolean", + default: false + }, + after: { + type: "boolean", + default: true + } + }, + additionalProperties: false + } + ], + + messages: { + unexpectedWhitespaceBefore: "Unexpected whitespace before semicolon.", + unexpectedWhitespaceAfter: "Unexpected whitespace after semicolon.", + missingWhitespaceBefore: "Missing whitespace before semicolon.", + missingWhitespaceAfter: "Missing whitespace after semicolon." + } + }, + + create(context) { + + const config = context.options[0], + sourceCode = context.sourceCode; + let requireSpaceBefore = false, + requireSpaceAfter = true; + + if (typeof config === "object") { + requireSpaceBefore = config.before; + requireSpaceAfter = config.after; + } + + /** + * Checks if a given token has leading whitespace. + * @param {Object} token The token to check. + * @returns {boolean} True if the given token has leading space, false if not. + */ + function hasLeadingSpace(token) { + const tokenBefore = sourceCode.getTokenBefore(token); + + return tokenBefore && astUtils.isTokenOnSameLine(tokenBefore, token) && sourceCode.isSpaceBetweenTokens(tokenBefore, token); + } + + /** + * Checks if a given token has trailing whitespace. + * @param {Object} token The token to check. + * @returns {boolean} True if the given token has trailing space, false if not. + */ + function hasTrailingSpace(token) { + const tokenAfter = sourceCode.getTokenAfter(token); + + return tokenAfter && astUtils.isTokenOnSameLine(token, tokenAfter) && sourceCode.isSpaceBetweenTokens(token, tokenAfter); + } + + /** + * Checks if the given token is the last token in its line. + * @param {Token} token The token to check. + * @returns {boolean} Whether or not the token is the last in its line. + */ + function isLastTokenInCurrentLine(token) { + const tokenAfter = sourceCode.getTokenAfter(token); + + return !(tokenAfter && astUtils.isTokenOnSameLine(token, tokenAfter)); + } + + /** + * Checks if the given token is the first token in its line + * @param {Token} token The token to check. + * @returns {boolean} Whether or not the token is the first in its line. + */ + function isFirstTokenInCurrentLine(token) { + const tokenBefore = sourceCode.getTokenBefore(token); + + return !(tokenBefore && astUtils.isTokenOnSameLine(token, tokenBefore)); + } + + /** + * Checks if the next token of a given token is a closing parenthesis. + * @param {Token} token The token to check. + * @returns {boolean} Whether or not the next token of a given token is a closing parenthesis. + */ + function isBeforeClosingParen(token) { + const nextToken = sourceCode.getTokenAfter(token); + + return (nextToken && astUtils.isClosingBraceToken(nextToken) || astUtils.isClosingParenToken(nextToken)); + } + + /** + * Report location example : + * + * for unexpected space `before` + * + * var a = 'b' ; + * ^^^ + * + * for unexpected space `after` + * + * var a = 'b'; c = 10; + * ^^ + * + * Reports if the given token has invalid spacing. + * @param {Token} token The semicolon token to check. + * @param {ASTNode} node The corresponding node of the token. + * @returns {void} + */ + function checkSemicolonSpacing(token, node) { + if (astUtils.isSemicolonToken(token)) { + if (hasLeadingSpace(token)) { + if (!requireSpaceBefore) { + const tokenBefore = sourceCode.getTokenBefore(token); + const loc = { + start: tokenBefore.loc.end, + end: token.loc.start + }; + + context.report({ + node, + loc, + messageId: "unexpectedWhitespaceBefore", + fix(fixer) { + + return fixer.removeRange([tokenBefore.range[1], token.range[0]]); + } + }); + } + } else { + if (requireSpaceBefore) { + const loc = token.loc; + + context.report({ + node, + loc, + messageId: "missingWhitespaceBefore", + fix(fixer) { + return fixer.insertTextBefore(token, " "); + } + }); + } + } + + if (!isFirstTokenInCurrentLine(token) && !isLastTokenInCurrentLine(token) && !isBeforeClosingParen(token)) { + if (hasTrailingSpace(token)) { + if (!requireSpaceAfter) { + const tokenAfter = sourceCode.getTokenAfter(token); + const loc = { + start: token.loc.end, + end: tokenAfter.loc.start + }; + + context.report({ + node, + loc, + messageId: "unexpectedWhitespaceAfter", + fix(fixer) { + + return fixer.removeRange([token.range[1], tokenAfter.range[0]]); + } + }); + } + } else { + if (requireSpaceAfter) { + const loc = token.loc; + + context.report({ + node, + loc, + messageId: "missingWhitespaceAfter", + fix(fixer) { + return fixer.insertTextAfter(token, " "); + } + }); + } + } + } + } + } + + /** + * Checks the spacing of the semicolon with the assumption that the last token is the semicolon. + * @param {ASTNode} node The node to check. + * @returns {void} + */ + function checkNode(node) { + const token = sourceCode.getLastToken(node); + + checkSemicolonSpacing(token, node); + } + + return { + VariableDeclaration: checkNode, + ExpressionStatement: checkNode, + BreakStatement: checkNode, + ContinueStatement: checkNode, + DebuggerStatement: checkNode, + DoWhileStatement: checkNode, + ReturnStatement: checkNode, + ThrowStatement: checkNode, + ImportDeclaration: checkNode, + ExportNamedDeclaration: checkNode, + ExportAllDeclaration: checkNode, + ExportDefaultDeclaration: checkNode, + ForStatement(node) { + if (node.init) { + checkSemicolonSpacing(sourceCode.getTokenAfter(node.init), node); + } + + if (node.test) { + checkSemicolonSpacing(sourceCode.getTokenAfter(node.test), node); + } + }, + PropertyDefinition: checkNode + }; + } }; diff --git a/node_modules/eslint/lib/rules/semi-style.js b/node_modules/eslint/lib/rules/semi-style.js index 43b32e148..caf2224df 100644 --- a/node_modules/eslint/lib/rules/semi-style.js +++ b/node_modules/eslint/lib/rules/semi-style.js @@ -17,19 +17,11 @@ const astUtils = require("./utils/ast-utils"); //------------------------------------------------------------------------------ const SELECTOR = [ - "BreakStatement", - "ContinueStatement", - "DebuggerStatement", - "DoWhileStatement", - "ExportAllDeclaration", - "ExportDefaultDeclaration", - "ExportNamedDeclaration", - "ExpressionStatement", - "ImportDeclaration", - "ReturnStatement", - "ThrowStatement", - "VariableDeclaration", - "PropertyDefinition", + "BreakStatement", "ContinueStatement", "DebuggerStatement", + "DoWhileStatement", "ExportAllDeclaration", + "ExportDefaultDeclaration", "ExportNamedDeclaration", + "ExpressionStatement", "ImportDeclaration", "ReturnStatement", + "ThrowStatement", "VariableDeclaration", "PropertyDefinition" ].join(","); /** @@ -41,20 +33,20 @@ const SELECTOR = [ * @returns {Node[]|null} The child node list. */ function getChildren(node) { - const t = node.type; - - if ( - t === "BlockStatement" || - t === "StaticBlock" || - t === "Program" || - t === "ClassBody" - ) { - return node.body; - } - if (t === "SwitchCase") { - return node.consequent; - } - return null; + const t = node.type; + + if ( + t === "BlockStatement" || + t === "StaticBlock" || + t === "Program" || + t === "ClassBody" + ) { + return node.body; + } + if (t === "SwitchCase") { + return node.consequent; + } + return null; } /** @@ -63,156 +55,104 @@ function getChildren(node) { * @returns {boolean} `true` if the node is the last statement in the parent block. */ function isLastChild(node) { - const t = node.parent.type; - - if ( - t === "IfStatement" && - node.parent.consequent === node && - node.parent.alternate - ) { - // before `else` keyword. - return true; - } - if (t === "DoWhileStatement") { - // before `while` keyword. - return true; - } - const nodeList = getChildren(node.parent); - - return nodeList !== null && nodeList.at(-1) === node; // before `}` or etc. + const t = node.parent.type; + + if (t === "IfStatement" && node.parent.consequent === node && node.parent.alternate) { // before `else` keyword. + return true; + } + if (t === "DoWhileStatement") { // before `while` keyword. + return true; + } + const nodeList = getChildren(node.parent); + + return nodeList !== null && nodeList[nodeList.length - 1] === node; // before `}` or etc. } -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "semi-style", - url: "https://eslint.style/rules/semi-style", - }, - }, - ], - }, - type: "layout", - - docs: { - description: "Enforce location of semicolons", - recommended: false, - url: "https://eslint.org/docs/latest/rules/semi-style", - }, - - schema: [{ enum: ["last", "first"] }], - fixable: "whitespace", - - messages: { - expectedSemiColon: "Expected this semicolon to be at {{pos}}.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - const option = context.options[0] || "last"; - - /** - * Check the given semicolon token. - * @param {Token} semiToken The semicolon token to check. - * @param {"first"|"last"} expected The expected location to check. - * @returns {void} - */ - function check(semiToken, expected) { - const prevToken = sourceCode.getTokenBefore(semiToken); - const nextToken = sourceCode.getTokenAfter(semiToken); - const prevIsSameLine = - !prevToken || astUtils.isTokenOnSameLine(prevToken, semiToken); - const nextIsSameLine = - !nextToken || astUtils.isTokenOnSameLine(semiToken, nextToken); - - if ( - (expected === "last" && !prevIsSameLine) || - (expected === "first" && !nextIsSameLine) - ) { - context.report({ - loc: semiToken.loc, - messageId: "expectedSemiColon", - data: { - pos: - expected === "last" - ? "the end of the previous line" - : "the beginning of the next line", - }, - fix(fixer) { - if ( - prevToken && - nextToken && - sourceCode.commentsExistBetween( - prevToken, - nextToken, - ) - ) { - return null; - } - - const start = prevToken - ? prevToken.range[1] - : semiToken.range[0]; - const end = nextToken - ? nextToken.range[0] - : semiToken.range[1]; - const text = expected === "last" ? ";\n" : "\n;"; - - return fixer.replaceTextRange([start, end], text); - }, - }); - } - } - - return { - [SELECTOR](node) { - if (option === "first" && isLastChild(node)) { - return; - } - - const lastToken = sourceCode.getLastToken(node); - - if (astUtils.isSemicolonToken(lastToken)) { - check(lastToken, option); - } - }, - - ForStatement(node) { - const firstSemi = - node.init && - sourceCode.getTokenAfter( - node.init, - astUtils.isSemicolonToken, - ); - const secondSemi = - node.test && - sourceCode.getTokenAfter( - node.test, - astUtils.isSemicolonToken, - ); - - if (firstSemi) { - check(firstSemi, "last"); - } - if (secondSemi) { - check(secondSemi, "last"); - } - }, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Enforce location of semicolons", + recommended: false, + url: "https://eslint.org/docs/latest/rules/semi-style" + }, + + schema: [{ enum: ["last", "first"] }], + fixable: "whitespace", + + messages: { + expectedSemiColon: "Expected this semicolon to be at {{pos}}." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + const option = context.options[0] || "last"; + + /** + * Check the given semicolon token. + * @param {Token} semiToken The semicolon token to check. + * @param {"first"|"last"} expected The expected location to check. + * @returns {void} + */ + function check(semiToken, expected) { + const prevToken = sourceCode.getTokenBefore(semiToken); + const nextToken = sourceCode.getTokenAfter(semiToken); + const prevIsSameLine = !prevToken || astUtils.isTokenOnSameLine(prevToken, semiToken); + const nextIsSameLine = !nextToken || astUtils.isTokenOnSameLine(semiToken, nextToken); + + if ((expected === "last" && !prevIsSameLine) || (expected === "first" && !nextIsSameLine)) { + context.report({ + loc: semiToken.loc, + messageId: "expectedSemiColon", + data: { + pos: (expected === "last") + ? "the end of the previous line" + : "the beginning of the next line" + }, + fix(fixer) { + if (prevToken && nextToken && sourceCode.commentsExistBetween(prevToken, nextToken)) { + return null; + } + + const start = prevToken ? prevToken.range[1] : semiToken.range[0]; + const end = nextToken ? nextToken.range[0] : semiToken.range[1]; + const text = (expected === "last") ? ";\n" : "\n;"; + + return fixer.replaceTextRange([start, end], text); + } + }); + } + } + + return { + [SELECTOR](node) { + if (option === "first" && isLastChild(node)) { + return; + } + + const lastToken = sourceCode.getLastToken(node); + + if (astUtils.isSemicolonToken(lastToken)) { + check(lastToken, option); + } + }, + + ForStatement(node) { + const firstSemi = node.init && sourceCode.getTokenAfter(node.init, astUtils.isSemicolonToken); + const secondSemi = node.test && sourceCode.getTokenAfter(node.test, astUtils.isSemicolonToken); + + if (firstSemi) { + check(firstSemi, "last"); + } + if (secondSemi) { + check(secondSemi, "last"); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/semi.js b/node_modules/eslint/lib/rules/semi.js index b6d28d88e..01586b849 100644 --- a/node_modules/eslint/lib/rules/semi.js +++ b/node_modules/eslint/lib/rules/semi.js @@ -16,461 +16,423 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "semi", - url: "https://eslint.style/rules/semi", - }, - }, - ], - }, - type: "layout", - - docs: { - description: "Require or disallow semicolons instead of ASI", - recommended: false, - url: "https://eslint.org/docs/latest/rules/semi", - }, - - fixable: "code", - - schema: { - anyOf: [ - { - type: "array", - items: [ - { - enum: ["never"], - }, - { - type: "object", - properties: { - beforeStatementContinuationChars: { - enum: ["always", "any", "never"], - }, - }, - additionalProperties: false, - }, - ], - minItems: 0, - maxItems: 2, - }, - { - type: "array", - items: [ - { - enum: ["always"], - }, - { - type: "object", - properties: { - omitLastInOneLineBlock: { type: "boolean" }, - omitLastInOneLineClassBody: { type: "boolean" }, - }, - additionalProperties: false, - }, - ], - minItems: 0, - maxItems: 2, - }, - ], - }, - - messages: { - missingSemi: "Missing semicolon.", - extraSemi: "Extra semicolon.", - }, - }, - - create(context) { - const OPT_OUT_PATTERN = /^[-[(/+`]/u; // One of [(/+-` - const unsafeClassFieldNames = new Set(["get", "set", "static"]); - const unsafeClassFieldFollowers = new Set(["*", "in", "instanceof"]); - const options = context.options[1]; - const never = context.options[0] === "never"; - const exceptOneLine = Boolean( - options && options.omitLastInOneLineBlock, - ); - const exceptOneLineClassBody = Boolean( - options && options.omitLastInOneLineClassBody, - ); - const beforeStatementContinuationChars = - (options && options.beforeStatementContinuationChars) || "any"; - const sourceCode = context.sourceCode; - - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - /** - * Reports a semicolon error with appropriate location and message. - * @param {ASTNode} node The node with an extra or missing semicolon. - * @param {boolean} missing True if the semicolon is missing. - * @returns {void} - */ - function report(node, missing) { - const lastToken = sourceCode.getLastToken(node); - let messageId, fix, loc; - - if (!missing) { - messageId = "missingSemi"; - loc = { - start: lastToken.loc.end, - end: astUtils.getNextLocation( - sourceCode, - lastToken.loc.end, - ), - }; - fix = function (fixer) { - return fixer.insertTextAfter(lastToken, ";"); - }; - } else { - messageId = "extraSemi"; - loc = lastToken.loc; - fix = function (fixer) { - /* - * Expand the replacement range to include the surrounding - * tokens to avoid conflicting with no-extra-semi. - * https://github.com/eslint/eslint/issues/7928 - */ - return new FixTracker(fixer, sourceCode) - .retainSurroundingTokens(lastToken) - .remove(lastToken); - }; - } - - context.report({ - node, - loc, - messageId, - fix, - }); - } - - /** - * Check whether a given semicolon token is redundant. - * @param {Token} semiToken A semicolon token to check. - * @returns {boolean} `true` if the next token is `;` or `}`. - */ - function isRedundantSemi(semiToken) { - const nextToken = sourceCode.getTokenAfter(semiToken); - - return ( - !nextToken || - astUtils.isClosingBraceToken(nextToken) || - astUtils.isSemicolonToken(nextToken) - ); - } - - /** - * Check whether a given token is the closing brace of an arrow function. - * @param {Token} lastToken A token to check. - * @returns {boolean} `true` if the token is the closing brace of an arrow function. - */ - function isEndOfArrowBlock(lastToken) { - if (!astUtils.isClosingBraceToken(lastToken)) { - return false; - } - const node = sourceCode.getNodeByRangeIndex(lastToken.range[0]); - - return ( - node.type === "BlockStatement" && - node.parent.type === "ArrowFunctionExpression" - ); - } - - /** - * Checks if a given PropertyDefinition node followed by a semicolon - * can safely remove that semicolon. It is not to safe to remove if - * the class field name is "get", "set", or "static", or if - * followed by a generator method. - * @param {ASTNode} node The node to check. - * @returns {boolean} `true` if the node cannot have the semicolon - * removed. - */ - function maybeClassFieldAsiHazard(node) { - if (node.type !== "PropertyDefinition") { - return false; - } - - /* - * Computed property names and non-identifiers are always safe - * as they can be distinguished from keywords easily. - */ - const needsNameCheck = - !node.computed && node.key.type === "Identifier"; - - /* - * Certain names are problematic unless they also have a - * a way to distinguish between keywords and property - * names. - */ - if (needsNameCheck && unsafeClassFieldNames.has(node.key.name)) { - /* - * Special case: If the field name is `static`, - * it is only valid if the field is marked as static, - * so "static static" is okay but "static" is not. - */ - const isStaticStatic = - node.static && node.key.name === "static"; - - /* - * For other unsafe names, we only care if there is no - * initializer. No initializer = hazard. - */ - if (!isStaticStatic && !node.value) { - return true; - } - } - - const followingToken = sourceCode.getTokenAfter(node); - - return unsafeClassFieldFollowers.has(followingToken.value); - } - - /** - * Check whether a given node is on the same line with the next token. - * @param {Node} node A statement node to check. - * @returns {boolean} `true` if the node is on the same line with the next token. - */ - function isOnSameLineWithNextToken(node) { - const prevToken = sourceCode.getLastToken(node, 1); - const nextToken = sourceCode.getTokenAfter(node); - - return ( - !!nextToken && astUtils.isTokenOnSameLine(prevToken, nextToken) - ); - } - - /** - * Check whether a given node can connect the next line if the next line is unreliable. - * @param {Node} node A statement node to check. - * @returns {boolean} `true` if the node can connect the next line. - */ - function maybeAsiHazardAfter(node) { - const t = node.type; - - if ( - t === "DoWhileStatement" || - t === "BreakStatement" || - t === "ContinueStatement" || - t === "DebuggerStatement" || - t === "ImportDeclaration" || - t === "ExportAllDeclaration" - ) { - return false; - } - if (t === "ReturnStatement") { - return Boolean(node.argument); - } - if (t === "ExportNamedDeclaration") { - return Boolean(node.declaration); - } - if (isEndOfArrowBlock(sourceCode.getLastToken(node, 1))) { - return false; - } - - return true; - } - - /** - * Check whether a given token can connect the previous statement. - * @param {Token} token A token to check. - * @returns {boolean} `true` if the token is one of `[`, `(`, `/`, `+`, `-`, ```, `++`, and `--`. - */ - function maybeAsiHazardBefore(token) { - return ( - Boolean(token) && - OPT_OUT_PATTERN.test(token.value) && - token.value !== "++" && - token.value !== "--" - ); - } - - /** - * Check if the semicolon of a given node is unnecessary, only true if: - * - next token is a valid statement divider (`;` or `}`). - * - next token is on a new line and the node is not connectable to the new line. - * @param {Node} node A statement node to check. - * @returns {boolean} whether the semicolon is unnecessary. - */ - function canRemoveSemicolon(node) { - if (isRedundantSemi(sourceCode.getLastToken(node))) { - return true; // `;;` or `;}` - } - if (maybeClassFieldAsiHazard(node)) { - return false; - } - if (isOnSameLineWithNextToken(node)) { - return false; // One liner. - } - - // continuation characters should not apply to class fields - if ( - node.type !== "PropertyDefinition" && - beforeStatementContinuationChars === "never" && - !maybeAsiHazardAfter(node) - ) { - return true; // ASI works. This statement doesn't connect to the next. - } - if (!maybeAsiHazardBefore(sourceCode.getTokenAfter(node))) { - return true; // ASI works. The next token doesn't connect to this statement. - } - - return false; - } - - /** - * Checks a node to see if it's the last item in a one-liner block. - * Block is any `BlockStatement` or `StaticBlock` node. Block is a one-liner if its - * braces (and consequently everything between them) are on the same line. - * @param {ASTNode} node The node to check. - * @returns {boolean} whether the node is the last item in a one-liner block. - */ - function isLastInOneLinerBlock(node) { - const parent = node.parent; - const nextToken = sourceCode.getTokenAfter(node); - - if (!nextToken || nextToken.value !== "}") { - return false; - } - - if (parent.type === "BlockStatement") { - return parent.loc.start.line === parent.loc.end.line; - } - - if (parent.type === "StaticBlock") { - const openingBrace = sourceCode.getFirstToken(parent, { - skip: 1, - }); // skip the `static` token - - return openingBrace.loc.start.line === parent.loc.end.line; - } - - return false; - } - - /** - * Checks a node to see if it's the last item in a one-liner `ClassBody` node. - * ClassBody is a one-liner if its braces (and consequently everything between them) are on the same line. - * @param {ASTNode} node The node to check. - * @returns {boolean} whether the node is the last item in a one-liner ClassBody. - */ - function isLastInOneLinerClassBody(node) { - const parent = node.parent; - const nextToken = sourceCode.getTokenAfter(node); - - if (!nextToken || nextToken.value !== "}") { - return false; - } - - if (parent.type === "ClassBody") { - return parent.loc.start.line === parent.loc.end.line; - } - - return false; - } - - /** - * Checks a node to see if it's followed by a semicolon. - * @param {ASTNode} node The node to check. - * @returns {void} - */ - function checkForSemicolon(node) { - const isSemi = astUtils.isSemicolonToken( - sourceCode.getLastToken(node), - ); - - if (never) { - if (isSemi && canRemoveSemicolon(node)) { - report(node, true); - } else if ( - !isSemi && - beforeStatementContinuationChars === "always" && - node.type !== "PropertyDefinition" && - maybeAsiHazardBefore(sourceCode.getTokenAfter(node)) - ) { - report(node); - } - } else { - const oneLinerBlock = - exceptOneLine && isLastInOneLinerBlock(node); - const oneLinerClassBody = - exceptOneLineClassBody && isLastInOneLinerClassBody(node); - const oneLinerBlockOrClassBody = - oneLinerBlock || oneLinerClassBody; - - if (isSemi && oneLinerBlockOrClassBody) { - report(node, true); - } else if (!isSemi && !oneLinerBlockOrClassBody) { - report(node); - } - } - } - - /** - * Checks to see if there's a semicolon after a variable declaration. - * @param {ASTNode} node The node to check. - * @returns {void} - */ - function checkForSemicolonForVariableDeclaration(node) { - const parent = node.parent; - - if ( - (parent.type !== "ForStatement" || parent.init !== node) && - (!/^For(?:In|Of)Statement/u.test(parent.type) || - parent.left !== node) - ) { - checkForSemicolon(node); - } - } - - //-------------------------------------------------------------------------- - // Public API - //-------------------------------------------------------------------------- - - return { - VariableDeclaration: checkForSemicolonForVariableDeclaration, - ExpressionStatement: checkForSemicolon, - ReturnStatement: checkForSemicolon, - ThrowStatement: checkForSemicolon, - DoWhileStatement: checkForSemicolon, - DebuggerStatement: checkForSemicolon, - BreakStatement: checkForSemicolon, - ContinueStatement: checkForSemicolon, - ImportDeclaration: checkForSemicolon, - ExportAllDeclaration: checkForSemicolon, - ExportNamedDeclaration(node) { - if (!node.declaration) { - checkForSemicolon(node); - } - }, - ExportDefaultDeclaration(node) { - if ( - !/(?:Class|Function)Declaration/u.test( - node.declaration.type, - ) - ) { - checkForSemicolon(node); - } - }, - PropertyDefinition: checkForSemicolon, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Require or disallow semicolons instead of ASI", + recommended: false, + url: "https://eslint.org/docs/latest/rules/semi" + }, + + fixable: "code", + + schema: { + anyOf: [ + { + type: "array", + items: [ + { + enum: ["never"] + }, + { + type: "object", + properties: { + beforeStatementContinuationChars: { + enum: ["always", "any", "never"] + } + }, + additionalProperties: false + } + ], + minItems: 0, + maxItems: 2 + }, + { + type: "array", + items: [ + { + enum: ["always"] + }, + { + type: "object", + properties: { + omitLastInOneLineBlock: { type: "boolean" }, + omitLastInOneLineClassBody: { type: "boolean" } + }, + additionalProperties: false + } + ], + minItems: 0, + maxItems: 2 + } + ] + }, + + messages: { + missingSemi: "Missing semicolon.", + extraSemi: "Extra semicolon." + } + }, + + create(context) { + + const OPT_OUT_PATTERN = /^[-[(/+`]/u; // One of [(/+-` + const unsafeClassFieldNames = new Set(["get", "set", "static"]); + const unsafeClassFieldFollowers = new Set(["*", "in", "instanceof"]); + const options = context.options[1]; + const never = context.options[0] === "never"; + const exceptOneLine = Boolean(options && options.omitLastInOneLineBlock); + const exceptOneLineClassBody = Boolean(options && options.omitLastInOneLineClassBody); + const beforeStatementContinuationChars = options && options.beforeStatementContinuationChars || "any"; + const sourceCode = context.sourceCode; + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + /** + * Reports a semicolon error with appropriate location and message. + * @param {ASTNode} node The node with an extra or missing semicolon. + * @param {boolean} missing True if the semicolon is missing. + * @returns {void} + */ + function report(node, missing) { + const lastToken = sourceCode.getLastToken(node); + let messageId, + fix, + loc; + + if (!missing) { + messageId = "missingSemi"; + loc = { + start: lastToken.loc.end, + end: astUtils.getNextLocation(sourceCode, lastToken.loc.end) + }; + fix = function(fixer) { + return fixer.insertTextAfter(lastToken, ";"); + }; + } else { + messageId = "extraSemi"; + loc = lastToken.loc; + fix = function(fixer) { + + /* + * Expand the replacement range to include the surrounding + * tokens to avoid conflicting with no-extra-semi. + * https://github.com/eslint/eslint/issues/7928 + */ + return new FixTracker(fixer, sourceCode) + .retainSurroundingTokens(lastToken) + .remove(lastToken); + }; + } + + context.report({ + node, + loc, + messageId, + fix + }); + + } + + /** + * Check whether a given semicolon token is redundant. + * @param {Token} semiToken A semicolon token to check. + * @returns {boolean} `true` if the next token is `;` or `}`. + */ + function isRedundantSemi(semiToken) { + const nextToken = sourceCode.getTokenAfter(semiToken); + + return ( + !nextToken || + astUtils.isClosingBraceToken(nextToken) || + astUtils.isSemicolonToken(nextToken) + ); + } + + /** + * Check whether a given token is the closing brace of an arrow function. + * @param {Token} lastToken A token to check. + * @returns {boolean} `true` if the token is the closing brace of an arrow function. + */ + function isEndOfArrowBlock(lastToken) { + if (!astUtils.isClosingBraceToken(lastToken)) { + return false; + } + const node = sourceCode.getNodeByRangeIndex(lastToken.range[0]); + + return ( + node.type === "BlockStatement" && + node.parent.type === "ArrowFunctionExpression" + ); + } + + /** + * Checks if a given PropertyDefinition node followed by a semicolon + * can safely remove that semicolon. It is not to safe to remove if + * the class field name is "get", "set", or "static", or if + * followed by a generator method. + * @param {ASTNode} node The node to check. + * @returns {boolean} `true` if the node cannot have the semicolon + * removed. + */ + function maybeClassFieldAsiHazard(node) { + + if (node.type !== "PropertyDefinition") { + return false; + } + + /* + * Computed property names and non-identifiers are always safe + * as they can be distinguished from keywords easily. + */ + const needsNameCheck = !node.computed && node.key.type === "Identifier"; + + /* + * Certain names are problematic unless they also have a + * a way to distinguish between keywords and property + * names. + */ + if (needsNameCheck && unsafeClassFieldNames.has(node.key.name)) { + + /* + * Special case: If the field name is `static`, + * it is only valid if the field is marked as static, + * so "static static" is okay but "static" is not. + */ + const isStaticStatic = node.static && node.key.name === "static"; + + /* + * For other unsafe names, we only care if there is no + * initializer. No initializer = hazard. + */ + if (!isStaticStatic && !node.value) { + return true; + } + } + + const followingToken = sourceCode.getTokenAfter(node); + + return unsafeClassFieldFollowers.has(followingToken.value); + } + + /** + * Check whether a given node is on the same line with the next token. + * @param {Node} node A statement node to check. + * @returns {boolean} `true` if the node is on the same line with the next token. + */ + function isOnSameLineWithNextToken(node) { + const prevToken = sourceCode.getLastToken(node, 1); + const nextToken = sourceCode.getTokenAfter(node); + + return !!nextToken && astUtils.isTokenOnSameLine(prevToken, nextToken); + } + + /** + * Check whether a given node can connect the next line if the next line is unreliable. + * @param {Node} node A statement node to check. + * @returns {boolean} `true` if the node can connect the next line. + */ + function maybeAsiHazardAfter(node) { + const t = node.type; + + if (t === "DoWhileStatement" || + t === "BreakStatement" || + t === "ContinueStatement" || + t === "DebuggerStatement" || + t === "ImportDeclaration" || + t === "ExportAllDeclaration" + ) { + return false; + } + if (t === "ReturnStatement") { + return Boolean(node.argument); + } + if (t === "ExportNamedDeclaration") { + return Boolean(node.declaration); + } + if (isEndOfArrowBlock(sourceCode.getLastToken(node, 1))) { + return false; + } + + return true; + } + + /** + * Check whether a given token can connect the previous statement. + * @param {Token} token A token to check. + * @returns {boolean} `true` if the token is one of `[`, `(`, `/`, `+`, `-`, ```, `++`, and `--`. + */ + function maybeAsiHazardBefore(token) { + return ( + Boolean(token) && + OPT_OUT_PATTERN.test(token.value) && + token.value !== "++" && + token.value !== "--" + ); + } + + /** + * Check if the semicolon of a given node is unnecessary, only true if: + * - next token is a valid statement divider (`;` or `}`). + * - next token is on a new line and the node is not connectable to the new line. + * @param {Node} node A statement node to check. + * @returns {boolean} whether the semicolon is unnecessary. + */ + function canRemoveSemicolon(node) { + if (isRedundantSemi(sourceCode.getLastToken(node))) { + return true; // `;;` or `;}` + } + if (maybeClassFieldAsiHazard(node)) { + return false; + } + if (isOnSameLineWithNextToken(node)) { + return false; // One liner. + } + + // continuation characters should not apply to class fields + if ( + node.type !== "PropertyDefinition" && + beforeStatementContinuationChars === "never" && + !maybeAsiHazardAfter(node) + ) { + return true; // ASI works. This statement doesn't connect to the next. + } + if (!maybeAsiHazardBefore(sourceCode.getTokenAfter(node))) { + return true; // ASI works. The next token doesn't connect to this statement. + } + + return false; + } + + /** + * Checks a node to see if it's the last item in a one-liner block. + * Block is any `BlockStatement` or `StaticBlock` node. Block is a one-liner if its + * braces (and consequently everything between them) are on the same line. + * @param {ASTNode} node The node to check. + * @returns {boolean} whether the node is the last item in a one-liner block. + */ + function isLastInOneLinerBlock(node) { + const parent = node.parent; + const nextToken = sourceCode.getTokenAfter(node); + + if (!nextToken || nextToken.value !== "}") { + return false; + } + + if (parent.type === "BlockStatement") { + return parent.loc.start.line === parent.loc.end.line; + } + + if (parent.type === "StaticBlock") { + const openingBrace = sourceCode.getFirstToken(parent, { skip: 1 }); // skip the `static` token + + return openingBrace.loc.start.line === parent.loc.end.line; + } + + return false; + } + + /** + * Checks a node to see if it's the last item in a one-liner `ClassBody` node. + * ClassBody is a one-liner if its braces (and consequently everything between them) are on the same line. + * @param {ASTNode} node The node to check. + * @returns {boolean} whether the node is the last item in a one-liner ClassBody. + */ + function isLastInOneLinerClassBody(node) { + const parent = node.parent; + const nextToken = sourceCode.getTokenAfter(node); + + if (!nextToken || nextToken.value !== "}") { + return false; + } + + if (parent.type === "ClassBody") { + return parent.loc.start.line === parent.loc.end.line; + } + + return false; + } + + /** + * Checks a node to see if it's followed by a semicolon. + * @param {ASTNode} node The node to check. + * @returns {void} + */ + function checkForSemicolon(node) { + const isSemi = astUtils.isSemicolonToken(sourceCode.getLastToken(node)); + + if (never) { + if (isSemi && canRemoveSemicolon(node)) { + report(node, true); + } else if ( + !isSemi && beforeStatementContinuationChars === "always" && + node.type !== "PropertyDefinition" && + maybeAsiHazardBefore(sourceCode.getTokenAfter(node)) + ) { + report(node); + } + } else { + const oneLinerBlock = (exceptOneLine && isLastInOneLinerBlock(node)); + const oneLinerClassBody = (exceptOneLineClassBody && isLastInOneLinerClassBody(node)); + const oneLinerBlockOrClassBody = oneLinerBlock || oneLinerClassBody; + + if (isSemi && oneLinerBlockOrClassBody) { + report(node, true); + } else if (!isSemi && !oneLinerBlockOrClassBody) { + report(node); + } + } + } + + /** + * Checks to see if there's a semicolon after a variable declaration. + * @param {ASTNode} node The node to check. + * @returns {void} + */ + function checkForSemicolonForVariableDeclaration(node) { + const parent = node.parent; + + if ((parent.type !== "ForStatement" || parent.init !== node) && + (!/^For(?:In|Of)Statement/u.test(parent.type) || parent.left !== node) + ) { + checkForSemicolon(node); + } + } + + //-------------------------------------------------------------------------- + // Public API + //-------------------------------------------------------------------------- + + return { + VariableDeclaration: checkForSemicolonForVariableDeclaration, + ExpressionStatement: checkForSemicolon, + ReturnStatement: checkForSemicolon, + ThrowStatement: checkForSemicolon, + DoWhileStatement: checkForSemicolon, + DebuggerStatement: checkForSemicolon, + BreakStatement: checkForSemicolon, + ContinueStatement: checkForSemicolon, + ImportDeclaration: checkForSemicolon, + ExportAllDeclaration: checkForSemicolon, + ExportNamedDeclaration(node) { + if (!node.declaration) { + checkForSemicolon(node); + } + }, + ExportDefaultDeclaration(node) { + if (!/(?:Class|Function)Declaration/u.test(node.declaration.type)) { + checkForSemicolon(node); + } + }, + PropertyDefinition: checkForSemicolon + }; + + } }; diff --git a/node_modules/eslint/lib/rules/sort-imports.js b/node_modules/eslint/lib/rules/sort-imports.js index e97f3f05f..04814ed6f 100644 --- a/node_modules/eslint/lib/rules/sort-imports.js +++ b/node_modules/eslint/lib/rules/sort-imports.js @@ -1,5 +1,5 @@ /** - * @fileoverview Rule to enforce sorted `import` declarations within modules + * @fileoverview Rule to require sorting of import declarations * @author Christian Schuller */ @@ -9,311 +9,233 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - defaultOptions: [ - { - allowSeparatedGroups: false, - ignoreCase: false, - ignoreDeclarationSort: false, - ignoreMemberSort: false, - memberSyntaxSortOrder: ["none", "all", "multiple", "single"], - }, - ], - - docs: { - description: "Enforce sorted `import` declarations within modules", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/sort-imports", - }, - - schema: [ - { - type: "object", - properties: { - ignoreCase: { - type: "boolean", - }, - memberSyntaxSortOrder: { - type: "array", - items: { - enum: ["none", "all", "multiple", "single"], - }, - uniqueItems: true, - minItems: 4, - maxItems: 4, - }, - ignoreDeclarationSort: { - type: "boolean", - }, - ignoreMemberSort: { - type: "boolean", - }, - allowSeparatedGroups: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - - fixable: "code", - - messages: { - sortImportsAlphabetically: - "Imports should be sorted alphabetically.", - sortMembersAlphabetically: - "Member '{{memberName}}' of the import declaration should be sorted alphabetically.", - unexpectedSyntaxOrder: - "Expected '{{syntaxA}}' syntax before '{{syntaxB}}' syntax.", - }, - }, - - create(context) { - const [ - { - ignoreCase, - ignoreDeclarationSort, - ignoreMemberSort, - memberSyntaxSortOrder, - allowSeparatedGroups, - }, - ] = context.options; - const sourceCode = context.sourceCode; - let previousDeclaration = null; - - /** - * Gets the used member syntax style. - * - * import "my-module.js" --> none - * import * as myModule from "my-module.js" --> all - * import {myMember} from "my-module.js" --> single - * import {foo, bar} from "my-module.js" --> multiple - * @param {ASTNode} node the ImportDeclaration node. - * @returns {string} used member parameter style, ["all", "multiple", "single"] - */ - function usedMemberSyntax(node) { - if (node.specifiers.length === 0) { - return "none"; - } - if (node.specifiers[0].type === "ImportNamespaceSpecifier") { - return "all"; - } - if (node.specifiers.length === 1) { - return "single"; - } - return "multiple"; - } - - /** - * Gets the group by member parameter index for given declaration. - * @param {ASTNode} node the ImportDeclaration node. - * @returns {number} the declaration group by member index. - */ - function getMemberParameterGroupIndex(node) { - return memberSyntaxSortOrder.indexOf(usedMemberSyntax(node)); - } - - /** - * Gets the local name of the first imported module. - * @param {ASTNode} node the ImportDeclaration node. - * @returns {?string} the local name of the first imported module. - */ - function getFirstLocalMemberName(node) { - if (node.specifiers[0]) { - return node.specifiers[0].local.name; - } - return null; - } - - /** - * Calculates number of lines between two nodes. It is assumed that the given `left` node appears before - * the given `right` node in the source code. Lines are counted from the end of the `left` node till the - * start of the `right` node. If the given nodes are on the same line, it returns `0`, same as if they were - * on two consecutive lines. - * @param {ASTNode} left node that appears before the given `right` node. - * @param {ASTNode} right node that appears after the given `left` node. - * @returns {number} number of lines between nodes. - */ - function getNumberOfLinesBetween(left, right) { - return Math.max(right.loc.start.line - left.loc.end.line - 1, 0); - } - - return { - ImportDeclaration(node) { - if (!ignoreDeclarationSort) { - if ( - previousDeclaration && - allowSeparatedGroups && - getNumberOfLinesBetween(previousDeclaration, node) > 0 - ) { - // reset declaration sort - previousDeclaration = null; - } - - if (previousDeclaration) { - const currentMemberSyntaxGroupIndex = - getMemberParameterGroupIndex(node), - previousMemberSyntaxGroupIndex = - getMemberParameterGroupIndex( - previousDeclaration, - ); - let currentLocalMemberName = - getFirstLocalMemberName(node), - previousLocalMemberName = - getFirstLocalMemberName(previousDeclaration); - - if (ignoreCase) { - previousLocalMemberName = - previousLocalMemberName && - previousLocalMemberName.toLowerCase(); - currentLocalMemberName = - currentLocalMemberName && - currentLocalMemberName.toLowerCase(); - } - - /* - * When the current declaration uses a different member syntax, - * then check if the ordering is correct. - * Otherwise, make a default string compare (like rule sort-vars to be consistent) of the first used local member name. - */ - if ( - currentMemberSyntaxGroupIndex !== - previousMemberSyntaxGroupIndex - ) { - if ( - currentMemberSyntaxGroupIndex < - previousMemberSyntaxGroupIndex - ) { - context.report({ - node, - messageId: "unexpectedSyntaxOrder", - data: { - syntaxA: - memberSyntaxSortOrder[ - currentMemberSyntaxGroupIndex - ], - syntaxB: - memberSyntaxSortOrder[ - previousMemberSyntaxGroupIndex - ], - }, - }); - } - } else { - if ( - previousLocalMemberName && - currentLocalMemberName && - currentLocalMemberName < previousLocalMemberName - ) { - context.report({ - node, - messageId: "sortImportsAlphabetically", - }); - } - } - } - - previousDeclaration = node; - } - - if (!ignoreMemberSort) { - const importSpecifiers = node.specifiers.filter( - specifier => specifier.type === "ImportSpecifier", - ); - const getSortableName = ignoreCase - ? specifier => specifier.local.name.toLowerCase() - : specifier => specifier.local.name; - const firstUnsortedIndex = importSpecifiers - .map(getSortableName) - .findIndex( - (name, index, array) => array[index - 1] > name, - ); - - if (firstUnsortedIndex !== -1) { - context.report({ - node: importSpecifiers[firstUnsortedIndex], - messageId: "sortMembersAlphabetically", - data: { - memberName: - importSpecifiers[firstUnsortedIndex].local - .name, - }, - fix(fixer) { - if ( - importSpecifiers.some( - specifier => - sourceCode.getCommentsBefore( - specifier, - ).length || - sourceCode.getCommentsAfter( - specifier, - ).length, - ) - ) { - // If there are comments in the ImportSpecifier list, don't rearrange the specifiers. - return null; - } - - return fixer.replaceTextRange( - [ - importSpecifiers[0].range[0], - importSpecifiers.at(-1).range[1], - ], - importSpecifiers - - // Clone the importSpecifiers array to avoid mutating it - .slice() - - // Sort the array into the desired order - .sort((specifierA, specifierB) => { - const aName = - getSortableName(specifierA); - const bName = - getSortableName(specifierB); - - return aName > bName ? 1 : -1; - }) - - // Build a string out of the sorted list of import specifiers and the text between the originals - .reduce( - (sourceText, specifier, index) => { - const textAfterSpecifier = - index === - importSpecifiers.length - 1 - ? "" - : sourceCode - .getText() - .slice( - importSpecifiers[ - index - ].range[1], - importSpecifiers[ - index + - 1 - ].range[0], - ); - - return ( - sourceText + - sourceCode.getText( - specifier, - ) + - textAfterSpecifier - ); - }, - "", - ), - ); - }, - }); - } - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Enforce sorted import declarations within modules", + recommended: false, + url: "https://eslint.org/docs/latest/rules/sort-imports" + }, + + schema: [ + { + type: "object", + properties: { + ignoreCase: { + type: "boolean", + default: false + }, + memberSyntaxSortOrder: { + type: "array", + items: { + enum: ["none", "all", "multiple", "single"] + }, + uniqueItems: true, + minItems: 4, + maxItems: 4 + }, + ignoreDeclarationSort: { + type: "boolean", + default: false + }, + ignoreMemberSort: { + type: "boolean", + default: false + }, + allowSeparatedGroups: { + type: "boolean", + default: false + } + }, + additionalProperties: false + } + ], + + fixable: "code", + + messages: { + sortImportsAlphabetically: "Imports should be sorted alphabetically.", + sortMembersAlphabetically: "Member '{{memberName}}' of the import declaration should be sorted alphabetically.", + unexpectedSyntaxOrder: "Expected '{{syntaxA}}' syntax before '{{syntaxB}}' syntax." + } + }, + + create(context) { + + const configuration = context.options[0] || {}, + ignoreCase = configuration.ignoreCase || false, + ignoreDeclarationSort = configuration.ignoreDeclarationSort || false, + ignoreMemberSort = configuration.ignoreMemberSort || false, + memberSyntaxSortOrder = configuration.memberSyntaxSortOrder || ["none", "all", "multiple", "single"], + allowSeparatedGroups = configuration.allowSeparatedGroups || false, + sourceCode = context.sourceCode; + let previousDeclaration = null; + + /** + * Gets the used member syntax style. + * + * import "my-module.js" --> none + * import * as myModule from "my-module.js" --> all + * import {myMember} from "my-module.js" --> single + * import {foo, bar} from "my-module.js" --> multiple + * @param {ASTNode} node the ImportDeclaration node. + * @returns {string} used member parameter style, ["all", "multiple", "single"] + */ + function usedMemberSyntax(node) { + if (node.specifiers.length === 0) { + return "none"; + } + if (node.specifiers[0].type === "ImportNamespaceSpecifier") { + return "all"; + } + if (node.specifiers.length === 1) { + return "single"; + } + return "multiple"; + + } + + /** + * Gets the group by member parameter index for given declaration. + * @param {ASTNode} node the ImportDeclaration node. + * @returns {number} the declaration group by member index. + */ + function getMemberParameterGroupIndex(node) { + return memberSyntaxSortOrder.indexOf(usedMemberSyntax(node)); + } + + /** + * Gets the local name of the first imported module. + * @param {ASTNode} node the ImportDeclaration node. + * @returns {?string} the local name of the first imported module. + */ + function getFirstLocalMemberName(node) { + if (node.specifiers[0]) { + return node.specifiers[0].local.name; + } + return null; + + } + + /** + * Calculates number of lines between two nodes. It is assumed that the given `left` node appears before + * the given `right` node in the source code. Lines are counted from the end of the `left` node till the + * start of the `right` node. If the given nodes are on the same line, it returns `0`, same as if they were + * on two consecutive lines. + * @param {ASTNode} left node that appears before the given `right` node. + * @param {ASTNode} right node that appears after the given `left` node. + * @returns {number} number of lines between nodes. + */ + function getNumberOfLinesBetween(left, right) { + return Math.max(right.loc.start.line - left.loc.end.line - 1, 0); + } + + return { + ImportDeclaration(node) { + if (!ignoreDeclarationSort) { + if ( + previousDeclaration && + allowSeparatedGroups && + getNumberOfLinesBetween(previousDeclaration, node) > 0 + ) { + + // reset declaration sort + previousDeclaration = null; + } + + if (previousDeclaration) { + const currentMemberSyntaxGroupIndex = getMemberParameterGroupIndex(node), + previousMemberSyntaxGroupIndex = getMemberParameterGroupIndex(previousDeclaration); + let currentLocalMemberName = getFirstLocalMemberName(node), + previousLocalMemberName = getFirstLocalMemberName(previousDeclaration); + + if (ignoreCase) { + previousLocalMemberName = previousLocalMemberName && previousLocalMemberName.toLowerCase(); + currentLocalMemberName = currentLocalMemberName && currentLocalMemberName.toLowerCase(); + } + + /* + * When the current declaration uses a different member syntax, + * then check if the ordering is correct. + * Otherwise, make a default string compare (like rule sort-vars to be consistent) of the first used local member name. + */ + if (currentMemberSyntaxGroupIndex !== previousMemberSyntaxGroupIndex) { + if (currentMemberSyntaxGroupIndex < previousMemberSyntaxGroupIndex) { + context.report({ + node, + messageId: "unexpectedSyntaxOrder", + data: { + syntaxA: memberSyntaxSortOrder[currentMemberSyntaxGroupIndex], + syntaxB: memberSyntaxSortOrder[previousMemberSyntaxGroupIndex] + } + }); + } + } else { + if (previousLocalMemberName && + currentLocalMemberName && + currentLocalMemberName < previousLocalMemberName + ) { + context.report({ + node, + messageId: "sortImportsAlphabetically" + }); + } + } + } + + previousDeclaration = node; + } + + if (!ignoreMemberSort) { + const importSpecifiers = node.specifiers.filter(specifier => specifier.type === "ImportSpecifier"); + const getSortableName = ignoreCase ? specifier => specifier.local.name.toLowerCase() : specifier => specifier.local.name; + const firstUnsortedIndex = importSpecifiers.map(getSortableName).findIndex((name, index, array) => array[index - 1] > name); + + if (firstUnsortedIndex !== -1) { + context.report({ + node: importSpecifiers[firstUnsortedIndex], + messageId: "sortMembersAlphabetically", + data: { memberName: importSpecifiers[firstUnsortedIndex].local.name }, + fix(fixer) { + if (importSpecifiers.some(specifier => + sourceCode.getCommentsBefore(specifier).length || sourceCode.getCommentsAfter(specifier).length)) { + + // If there are comments in the ImportSpecifier list, don't rearrange the specifiers. + return null; + } + + return fixer.replaceTextRange( + [importSpecifiers[0].range[0], importSpecifiers[importSpecifiers.length - 1].range[1]], + importSpecifiers + + // Clone the importSpecifiers array to avoid mutating it + .slice() + + // Sort the array into the desired order + .sort((specifierA, specifierB) => { + const aName = getSortableName(specifierA); + const bName = getSortableName(specifierB); + + return aName > bName ? 1 : -1; + }) + + // Build a string out of the sorted list of import specifiers and the text between the originals + .reduce((sourceText, specifier, index) => { + const textAfterSpecifier = index === importSpecifiers.length - 1 + ? "" + : sourceCode.getText().slice(importSpecifiers[index].range[1], importSpecifiers[index + 1].range[0]); + + return sourceText + sourceCode.getText(specifier) + textAfterSpecifier; + }, "") + ); + } + }); + } + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/sort-keys.js b/node_modules/eslint/lib/rules/sort-keys.js index bbb2dc2b6..088b5890f 100644 --- a/node_modules/eslint/lib/rules/sort-keys.js +++ b/node_modules/eslint/lib/rules/sort-keys.js @@ -10,7 +10,7 @@ //------------------------------------------------------------------------------ const astUtils = require("./utils/ast-utils"), - naturalCompare = require("natural-compare"); + naturalCompare = require("natural-compare"); //------------------------------------------------------------------------------ // Helpers @@ -28,13 +28,13 @@ const astUtils = require("./utils/ast-utils"), * @private */ function getPropertyName(node) { - const staticName = astUtils.getStaticPropertyName(node); + const staticName = astUtils.getStaticPropertyName(node); - if (staticName !== null) { - return staticName; - } + if (staticName !== null) { + return staticName; + } - return node.key.name || null; + return node.key.name || null; } /** @@ -45,224 +45,186 @@ function getPropertyName(node) { * @private */ const isValidOrders = { - asc(a, b) { - return a <= b; - }, - ascI(a, b) { - return a.toLowerCase() <= b.toLowerCase(); - }, - ascN(a, b) { - return naturalCompare(a, b) <= 0; - }, - ascIN(a, b) { - return naturalCompare(a.toLowerCase(), b.toLowerCase()) <= 0; - }, - desc(a, b) { - return isValidOrders.asc(b, a); - }, - descI(a, b) { - return isValidOrders.ascI(b, a); - }, - descN(a, b) { - return isValidOrders.ascN(b, a); - }, - descIN(a, b) { - return isValidOrders.ascIN(b, a); - }, + asc(a, b) { + return a <= b; + }, + ascI(a, b) { + return a.toLowerCase() <= b.toLowerCase(); + }, + ascN(a, b) { + return naturalCompare(a, b) <= 0; + }, + ascIN(a, b) { + return naturalCompare(a.toLowerCase(), b.toLowerCase()) <= 0; + }, + desc(a, b) { + return isValidOrders.asc(b, a); + }, + descI(a, b) { + return isValidOrders.ascI(b, a); + }, + descN(a, b) { + return isValidOrders.ascN(b, a); + }, + descIN(a, b) { + return isValidOrders.ascIN(b, a); + } }; //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - defaultOptions: [ - "asc", - { - allowLineSeparatedGroups: false, - caseSensitive: true, - ignoreComputedKeys: false, - minKeys: 2, - natural: false, - }, - ], - - docs: { - description: "Require object keys to be sorted", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/sort-keys", - }, - - schema: [ - { - enum: ["asc", "desc"], - }, - { - type: "object", - properties: { - caseSensitive: { - type: "boolean", - }, - natural: { - type: "boolean", - }, - minKeys: { - type: "integer", - minimum: 2, - }, - allowLineSeparatedGroups: { - type: "boolean", - }, - ignoreComputedKeys: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - - messages: { - sortKeys: - "Expected object keys to be in {{natural}}{{insensitive}}{{order}}ending order. '{{thisName}}' should be before '{{prevName}}'.", - }, - }, - - create(context) { - const [ - order, - { - caseSensitive, - natural, - minKeys, - allowLineSeparatedGroups, - ignoreComputedKeys, - }, - ] = context.options; - const insensitive = !caseSensitive; - const isValidOrder = - isValidOrders[ - order + (insensitive ? "I" : "") + (natural ? "N" : "") - ]; - - // The stack to save the previous property's name for each object literals. - let stack = null; - const sourceCode = context.sourceCode; - - return { - ObjectExpression(node) { - stack = { - upper: stack, - prevNode: null, - prevBlankLine: false, - prevName: null, - numKeys: node.properties.length, - }; - }, - - "ObjectExpression:exit"() { - stack = stack.upper; - }, - - SpreadElement(node) { - if (node.parent.type === "ObjectExpression") { - stack.prevName = null; - } - }, - - Property(node) { - if (node.parent.type === "ObjectPattern") { - return; - } - - if (ignoreComputedKeys && node.computed) { - stack.prevName = null; // reset sort - return; - } - - const prevName = stack.prevName; - const numKeys = stack.numKeys; - const thisName = getPropertyName(node); - - // Get tokens between current node and previous node - const tokens = - stack.prevNode && - sourceCode.getTokensBetween(stack.prevNode, node, { - includeComments: true, - }); - - let isBlankLineBetweenNodes = stack.prevBlankLine; - - if (tokens) { - // check blank line between tokens - tokens.forEach((token, index) => { - const previousToken = tokens[index - 1]; - - if ( - previousToken && - token.loc.start.line - previousToken.loc.end.line > - 1 - ) { - isBlankLineBetweenNodes = true; - } - }); - - // check blank line between the current node and the last token - if ( - !isBlankLineBetweenNodes && - node.loc.start.line - tokens.at(-1).loc.end.line > 1 - ) { - isBlankLineBetweenNodes = true; - } - - // check blank line between the first token and the previous node - if ( - !isBlankLineBetweenNodes && - tokens[0].loc.start.line - stack.prevNode.loc.end.line > - 1 - ) { - isBlankLineBetweenNodes = true; - } - } - - stack.prevNode = node; - - if (thisName !== null) { - stack.prevName = thisName; - } - - if (allowLineSeparatedGroups && isBlankLineBetweenNodes) { - stack.prevBlankLine = thisName === null; - return; - } - - if ( - prevName === null || - thisName === null || - numKeys < minKeys - ) { - return; - } - - if (!isValidOrder(prevName, thisName)) { - context.report({ - node, - loc: node.key.loc, - messageId: "sortKeys", - data: { - thisName, - prevName, - order, - insensitive: insensitive ? "insensitive " : "", - natural: natural ? "natural " : "", - }, - }); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Require object keys to be sorted", + recommended: false, + url: "https://eslint.org/docs/latest/rules/sort-keys" + }, + + schema: [ + { + enum: ["asc", "desc"] + }, + { + type: "object", + properties: { + caseSensitive: { + type: "boolean", + default: true + }, + natural: { + type: "boolean", + default: false + }, + minKeys: { + type: "integer", + minimum: 2, + default: 2 + }, + allowLineSeparatedGroups: { + type: "boolean", + default: false + } + }, + additionalProperties: false + } + ], + + messages: { + sortKeys: "Expected object keys to be in {{natural}}{{insensitive}}{{order}}ending order. '{{thisName}}' should be before '{{prevName}}'." + } + }, + + create(context) { + + // Parse options. + const order = context.options[0] || "asc"; + const options = context.options[1]; + const insensitive = options && options.caseSensitive === false; + const natural = options && options.natural; + const minKeys = options && options.minKeys; + const allowLineSeparatedGroups = options && options.allowLineSeparatedGroups || false; + const isValidOrder = isValidOrders[ + order + (insensitive ? "I" : "") + (natural ? "N" : "") + ]; + + // The stack to save the previous property's name for each object literals. + let stack = null; + const sourceCode = context.sourceCode; + + return { + ObjectExpression(node) { + stack = { + upper: stack, + prevNode: null, + prevBlankLine: false, + prevName: null, + numKeys: node.properties.length + }; + }, + + "ObjectExpression:exit"() { + stack = stack.upper; + }, + + SpreadElement(node) { + if (node.parent.type === "ObjectExpression") { + stack.prevName = null; + } + }, + + Property(node) { + if (node.parent.type === "ObjectPattern") { + return; + } + + const prevName = stack.prevName; + const numKeys = stack.numKeys; + const thisName = getPropertyName(node); + + // Get tokens between current node and previous node + const tokens = stack.prevNode && sourceCode + .getTokensBetween(stack.prevNode, node, { includeComments: true }); + + let isBlankLineBetweenNodes = stack.prevBlankLine; + + if (tokens) { + + // check blank line between tokens + tokens.forEach((token, index) => { + const previousToken = tokens[index - 1]; + + if (previousToken && (token.loc.start.line - previousToken.loc.end.line > 1)) { + isBlankLineBetweenNodes = true; + } + }); + + // check blank line between the current node and the last token + if (!isBlankLineBetweenNodes && (node.loc.start.line - tokens[tokens.length - 1].loc.end.line > 1)) { + isBlankLineBetweenNodes = true; + } + + // check blank line between the first token and the previous node + if (!isBlankLineBetweenNodes && (tokens[0].loc.start.line - stack.prevNode.loc.end.line > 1)) { + isBlankLineBetweenNodes = true; + } + } + + stack.prevNode = node; + + if (thisName !== null) { + stack.prevName = thisName; + } + + if (allowLineSeparatedGroups && isBlankLineBetweenNodes) { + stack.prevBlankLine = thisName === null; + return; + } + + if (prevName === null || thisName === null || numKeys < minKeys) { + return; + } + + if (!isValidOrder(prevName, thisName)) { + context.report({ + node, + loc: node.key.loc, + messageId: "sortKeys", + data: { + thisName, + prevName, + order, + insensitive: insensitive ? "insensitive " : "", + natural: natural ? "natural " : "" + } + }); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/sort-vars.js b/node_modules/eslint/lib/rules/sort-vars.js index 34081b955..8fd723fd4 100644 --- a/node_modules/eslint/lib/rules/sort-vars.js +++ b/node_modules/eslint/lib/rules/sort-vars.js @@ -9,132 +9,96 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - defaultOptions: [ - { - ignoreCase: false, - }, - ], - - docs: { - description: - "Require variables within the same declaration block to be sorted", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/sort-vars", - }, - - schema: [ - { - type: "object", - properties: { - ignoreCase: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - - fixable: "code", - - messages: { - sortVars: - "Variables within the same declaration block should be sorted alphabetically.", - }, - }, - - create(context) { - const [{ ignoreCase }] = context.options; - const sourceCode = context.sourceCode; - - return { - VariableDeclaration(node) { - const idDeclarations = node.declarations.filter( - decl => decl.id.type === "Identifier", - ); - const getSortableName = ignoreCase - ? decl => decl.id.name.toLowerCase() - : decl => decl.id.name; - const unfixable = idDeclarations.some( - decl => decl.init !== null && decl.init.type !== "Literal", - ); - let fixed = false; - - idDeclarations.slice(1).reduce((memo, decl) => { - const lastVariableName = getSortableName(memo), - currentVariableName = getSortableName(decl); - - if (currentVariableName < lastVariableName) { - context.report({ - node: decl, - messageId: "sortVars", - fix(fixer) { - if (unfixable || fixed) { - return null; - } - return fixer.replaceTextRange( - [ - idDeclarations[0].range[0], - idDeclarations.at(-1).range[1], - ], - idDeclarations - - // Clone the idDeclarations array to avoid mutating it - .slice() - - // Sort the array into the desired order - .sort((declA, declB) => { - const aName = - getSortableName(declA); - const bName = - getSortableName(declB); - - return aName > bName ? 1 : -1; - }) - - // Build a string out of the sorted list of identifier declarations and the text between the originals - .reduce( - (sourceText, identifier, index) => { - const textAfterIdentifier = - index === - idDeclarations.length - 1 - ? "" - : sourceCode - .getText() - .slice( - idDeclarations[ - index - ].range[1], - idDeclarations[ - index + - 1 - ].range[0], - ); - - return ( - sourceText + - sourceCode.getText( - identifier, - ) + - textAfterIdentifier - ); - }, - "", - ), - ); - }, - }); - fixed = true; - return memo; - } - return decl; - }, idDeclarations[0]); - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Require variables within the same declaration block to be sorted", + recommended: false, + url: "https://eslint.org/docs/latest/rules/sort-vars" + }, + + schema: [ + { + type: "object", + properties: { + ignoreCase: { + type: "boolean", + default: false + } + }, + additionalProperties: false + } + ], + + fixable: "code", + + messages: { + sortVars: "Variables within the same declaration block should be sorted alphabetically." + } + }, + + create(context) { + + const configuration = context.options[0] || {}, + ignoreCase = configuration.ignoreCase || false, + sourceCode = context.sourceCode; + + return { + VariableDeclaration(node) { + const idDeclarations = node.declarations.filter(decl => decl.id.type === "Identifier"); + const getSortableName = ignoreCase ? decl => decl.id.name.toLowerCase() : decl => decl.id.name; + const unfixable = idDeclarations.some(decl => decl.init !== null && decl.init.type !== "Literal"); + let fixed = false; + + idDeclarations.slice(1).reduce((memo, decl) => { + const lastVariableName = getSortableName(memo), + currentVariableName = getSortableName(decl); + + if (currentVariableName < lastVariableName) { + context.report({ + node: decl, + messageId: "sortVars", + fix(fixer) { + if (unfixable || fixed) { + return null; + } + return fixer.replaceTextRange( + [idDeclarations[0].range[0], idDeclarations[idDeclarations.length - 1].range[1]], + idDeclarations + + // Clone the idDeclarations array to avoid mutating it + .slice() + + // Sort the array into the desired order + .sort((declA, declB) => { + const aName = getSortableName(declA); + const bName = getSortableName(declB); + + return aName > bName ? 1 : -1; + }) + + // Build a string out of the sorted list of identifier declarations and the text between the originals + .reduce((sourceText, identifier, index) => { + const textAfterIdentifier = index === idDeclarations.length - 1 + ? "" + : sourceCode.getText().slice(idDeclarations[index].range[1], idDeclarations[index + 1].range[0]); + + return sourceText + sourceCode.getText(identifier) + textAfterIdentifier; + }, "") + + ); + } + }); + fixed = true; + return memo; + } + return decl; + + }, idDeclarations[0]); + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/space-before-blocks.js b/node_modules/eslint/lib/rules/space-before-blocks.js index 0d35276a8..a4a5449e1 100644 --- a/node_modules/eslint/lib/rules/space-before-blocks.js +++ b/node_modules/eslint/lib/rules/space-before-blocks.js @@ -22,211 +22,183 @@ const astUtils = require("./utils/ast-utils"); * @returns {boolean} `true` if the node is function body. */ function isFunctionBody(node) { - const parent = node.parent; + const parent = node.parent; - return ( - node.type === "BlockStatement" && - astUtils.isFunction(parent) && - parent.body === node - ); + return ( + node.type === "BlockStatement" && + astUtils.isFunction(parent) && + parent.body === node + ); } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "space-before-blocks", - url: "https://eslint.style/rules/space-before-blocks", - }, - }, - ], - }, - type: "layout", - - docs: { - description: "Enforce consistent spacing before blocks", - recommended: false, - url: "https://eslint.org/docs/latest/rules/space-before-blocks", - }, - - fixable: "whitespace", - - schema: [ - { - oneOf: [ - { - enum: ["always", "never"], - }, - { - type: "object", - properties: { - keywords: { - enum: ["always", "never", "off"], - }, - functions: { - enum: ["always", "never", "off"], - }, - classes: { - enum: ["always", "never", "off"], - }, - }, - additionalProperties: false, - }, - ], - }, - ], - - messages: { - unexpectedSpace: "Unexpected space before opening brace.", - missingSpace: "Missing space before opening brace.", - }, - }, - - create(context) { - const config = context.options[0], - sourceCode = context.sourceCode; - let alwaysFunctions = true, - alwaysKeywords = true, - alwaysClasses = true, - neverFunctions = false, - neverKeywords = false, - neverClasses = false; - - if (typeof config === "object") { - alwaysFunctions = config.functions === "always"; - alwaysKeywords = config.keywords === "always"; - alwaysClasses = config.classes === "always"; - neverFunctions = config.functions === "never"; - neverKeywords = config.keywords === "never"; - neverClasses = config.classes === "never"; - } else if (config === "never") { - alwaysFunctions = false; - alwaysKeywords = false; - alwaysClasses = false; - neverFunctions = true; - neverKeywords = true; - neverClasses = true; - } - - /** - * Checks whether the spacing before the given block is already controlled by another rule: - * - `arrow-spacing` checks spaces after `=>`. - * - `keyword-spacing` checks spaces after keywords in certain contexts. - * - `switch-colon-spacing` checks spaces after `:` of switch cases. - * @param {Token} precedingToken first token before the block. - * @param {ASTNode|Token} node `BlockStatement` node or `{` token of a `SwitchStatement` node. - * @returns {boolean} `true` if requiring or disallowing spaces before the given block could produce conflicts with other rules. - */ - function isConflicted(precedingToken, node) { - return ( - astUtils.isArrowToken(precedingToken) || - (astUtils.isKeywordToken(precedingToken) && - !isFunctionBody(node)) || - (astUtils.isColonToken(precedingToken) && - node.parent && - node.parent.type === "SwitchCase" && - precedingToken === - astUtils.getSwitchCaseColonToken( - node.parent, - sourceCode, - )) - ); - } - - /** - * Checks the given BlockStatement node has a preceding space if it doesn’t start on a new line. - * @param {ASTNode|Token} node The AST node of a BlockStatement. - * @returns {void} undefined. - */ - function checkPrecedingSpace(node) { - const precedingToken = sourceCode.getTokenBefore(node); - - if ( - precedingToken && - !isConflicted(precedingToken, node) && - astUtils.isTokenOnSameLine(precedingToken, node) - ) { - const hasSpace = sourceCode.isSpaceBetweenTokens( - precedingToken, - node, - ); - let requireSpace; - let requireNoSpace; - - if (isFunctionBody(node)) { - requireSpace = alwaysFunctions; - requireNoSpace = neverFunctions; - } else if (node.type === "ClassBody") { - requireSpace = alwaysClasses; - requireNoSpace = neverClasses; - } else { - requireSpace = alwaysKeywords; - requireNoSpace = neverKeywords; - } - - if (requireSpace && !hasSpace) { - context.report({ - node, - messageId: "missingSpace", - fix(fixer) { - return fixer.insertTextBefore(node, " "); - }, - }); - } else if (requireNoSpace && hasSpace) { - context.report({ - node, - messageId: "unexpectedSpace", - fix(fixer) { - return fixer.removeRange([ - precedingToken.range[1], - node.range[0], - ]); - }, - }); - } - } - } - - /** - * Checks if the CaseBlock of an given SwitchStatement node has a preceding space. - * @param {ASTNode} node The node of a SwitchStatement. - * @returns {void} undefined. - */ - function checkSpaceBeforeCaseBlock(node) { - const cases = node.cases; - let openingBrace; - - if (cases.length > 0) { - openingBrace = sourceCode.getTokenBefore(cases[0]); - } else { - openingBrace = sourceCode.getLastToken(node, 1); - } - - checkPrecedingSpace(openingBrace); - } - - return { - BlockStatement: checkPrecedingSpace, - ClassBody: checkPrecedingSpace, - SwitchStatement: checkSpaceBeforeCaseBlock, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Enforce consistent spacing before blocks", + recommended: false, + url: "https://eslint.org/docs/latest/rules/space-before-blocks" + }, + + fixable: "whitespace", + + schema: [ + { + oneOf: [ + { + enum: ["always", "never"] + }, + { + type: "object", + properties: { + keywords: { + enum: ["always", "never", "off"] + }, + functions: { + enum: ["always", "never", "off"] + }, + classes: { + enum: ["always", "never", "off"] + } + }, + additionalProperties: false + } + ] + } + ], + + messages: { + unexpectedSpace: "Unexpected space before opening brace.", + missingSpace: "Missing space before opening brace." + } + }, + + create(context) { + const config = context.options[0], + sourceCode = context.sourceCode; + let alwaysFunctions = true, + alwaysKeywords = true, + alwaysClasses = true, + neverFunctions = false, + neverKeywords = false, + neverClasses = false; + + if (typeof config === "object") { + alwaysFunctions = config.functions === "always"; + alwaysKeywords = config.keywords === "always"; + alwaysClasses = config.classes === "always"; + neverFunctions = config.functions === "never"; + neverKeywords = config.keywords === "never"; + neverClasses = config.classes === "never"; + } else if (config === "never") { + alwaysFunctions = false; + alwaysKeywords = false; + alwaysClasses = false; + neverFunctions = true; + neverKeywords = true; + neverClasses = true; + } + + /** + * Checks whether the spacing before the given block is already controlled by another rule: + * - `arrow-spacing` checks spaces after `=>`. + * - `keyword-spacing` checks spaces after keywords in certain contexts. + * - `switch-colon-spacing` checks spaces after `:` of switch cases. + * @param {Token} precedingToken first token before the block. + * @param {ASTNode|Token} node `BlockStatement` node or `{` token of a `SwitchStatement` node. + * @returns {boolean} `true` if requiring or disallowing spaces before the given block could produce conflicts with other rules. + */ + function isConflicted(precedingToken, node) { + return ( + astUtils.isArrowToken(precedingToken) || + ( + astUtils.isKeywordToken(precedingToken) && + !isFunctionBody(node) + ) || + ( + astUtils.isColonToken(precedingToken) && + node.parent && + node.parent.type === "SwitchCase" && + precedingToken === astUtils.getSwitchCaseColonToken(node.parent, sourceCode) + ) + ); + } + + /** + * Checks the given BlockStatement node has a preceding space if it doesn’t start on a new line. + * @param {ASTNode|Token} node The AST node of a BlockStatement. + * @returns {void} undefined. + */ + function checkPrecedingSpace(node) { + const precedingToken = sourceCode.getTokenBefore(node); + + if (precedingToken && !isConflicted(precedingToken, node) && astUtils.isTokenOnSameLine(precedingToken, node)) { + const hasSpace = sourceCode.isSpaceBetweenTokens(precedingToken, node); + let requireSpace; + let requireNoSpace; + + if (isFunctionBody(node)) { + requireSpace = alwaysFunctions; + requireNoSpace = neverFunctions; + } else if (node.type === "ClassBody") { + requireSpace = alwaysClasses; + requireNoSpace = neverClasses; + } else { + requireSpace = alwaysKeywords; + requireNoSpace = neverKeywords; + } + + if (requireSpace && !hasSpace) { + context.report({ + node, + messageId: "missingSpace", + fix(fixer) { + return fixer.insertTextBefore(node, " "); + } + }); + } else if (requireNoSpace && hasSpace) { + context.report({ + node, + messageId: "unexpectedSpace", + fix(fixer) { + return fixer.removeRange([precedingToken.range[1], node.range[0]]); + } + }); + } + } + } + + /** + * Checks if the CaseBlock of an given SwitchStatement node has a preceding space. + * @param {ASTNode} node The node of a SwitchStatement. + * @returns {void} undefined. + */ + function checkSpaceBeforeCaseBlock(node) { + const cases = node.cases; + let openingBrace; + + if (cases.length > 0) { + openingBrace = sourceCode.getTokenBefore(cases[0]); + } else { + openingBrace = sourceCode.getLastToken(node, 1); + } + + checkPrecedingSpace(openingBrace); + } + + return { + BlockStatement: checkPrecedingSpace, + ClassBody: checkPrecedingSpace, + SwitchStatement: checkSpaceBeforeCaseBlock + }; + + } }; diff --git a/node_modules/eslint/lib/rules/space-before-function-paren.js b/node_modules/eslint/lib/rules/space-before-function-paren.js index dad6fde8f..575a1597a 100644 --- a/node_modules/eslint/lib/rules/space-before-function-paren.js +++ b/node_modules/eslint/lib/rules/space-before-function-paren.js @@ -15,191 +15,153 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "space-before-function-paren", - url: "https://eslint.style/rules/space-before-function-paren", - }, - }, - ], - }, - type: "layout", - - docs: { - description: - "Enforce consistent spacing before `function` definition opening parenthesis", - recommended: false, - url: "https://eslint.org/docs/latest/rules/space-before-function-paren", - }, - - fixable: "whitespace", - - schema: [ - { - oneOf: [ - { - enum: ["always", "never"], - }, - { - type: "object", - properties: { - anonymous: { - enum: ["always", "never", "ignore"], - }, - named: { - enum: ["always", "never", "ignore"], - }, - asyncArrow: { - enum: ["always", "never", "ignore"], - }, - }, - additionalProperties: false, - }, - ], - }, - ], - - messages: { - unexpectedSpace: "Unexpected space before function parentheses.", - missingSpace: "Missing space before function parentheses.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - const baseConfig = - typeof context.options[0] === "string" - ? context.options[0] - : "always"; - const overrideConfig = - typeof context.options[0] === "object" ? context.options[0] : {}; - - /** - * Determines whether a function has a name. - * @param {ASTNode} node The function node. - * @returns {boolean} Whether the function has a name. - */ - function isNamedFunction(node) { - if (node.id) { - return true; - } - - const parent = node.parent; - - return ( - parent.type === "MethodDefinition" || - (parent.type === "Property" && - (parent.kind === "get" || - parent.kind === "set" || - parent.method)) - ); - } - - /** - * Gets the config for a given function - * @param {ASTNode} node The function node - * @returns {string} "always", "never", or "ignore" - */ - function getConfigForFunction(node) { - if (node.type === "ArrowFunctionExpression") { - // Always ignore non-async functions and arrow functions without parens, e.g. async foo => bar - if ( - node.async && - astUtils.isOpeningParenToken( - sourceCode.getFirstToken(node, { skip: 1 }), - ) - ) { - return overrideConfig.asyncArrow || baseConfig; - } - } else if (isNamedFunction(node)) { - return overrideConfig.named || baseConfig; - - // `generator-star-spacing` should warn anonymous generators. E.g. `function* () {}` - } else if (!node.generator) { - return overrideConfig.anonymous || baseConfig; - } - - return "ignore"; - } - - /** - * Checks the parens of a function node - * @param {ASTNode} node A function node - * @returns {void} - */ - function checkFunction(node) { - const functionConfig = getConfigForFunction(node); - - if (functionConfig === "ignore") { - return; - } - - const rightToken = sourceCode.getFirstToken( - node, - astUtils.isOpeningParenToken, - ); - const leftToken = sourceCode.getTokenBefore(rightToken); - const hasSpacing = sourceCode.isSpaceBetweenTokens( - leftToken, - rightToken, - ); - - if (hasSpacing && functionConfig === "never") { - context.report({ - node, - loc: { - start: leftToken.loc.end, - end: rightToken.loc.start, - }, - messageId: "unexpectedSpace", - fix(fixer) { - const comments = - sourceCode.getCommentsBefore(rightToken); - - // Don't fix anything if there's a single line comment between the left and the right token - if (comments.some(comment => comment.type === "Line")) { - return null; - } - return fixer.replaceTextRange( - [leftToken.range[1], rightToken.range[0]], - comments.reduce( - (text, comment) => - text + sourceCode.getText(comment), - "", - ), - ); - }, - }); - } else if (!hasSpacing && functionConfig === "always") { - context.report({ - node, - loc: rightToken.loc, - messageId: "missingSpace", - fix: fixer => fixer.insertTextAfter(leftToken, " "), - }); - } - } - - return { - ArrowFunctionExpression: checkFunction, - FunctionDeclaration: checkFunction, - FunctionExpression: checkFunction, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Enforce consistent spacing before `function` definition opening parenthesis", + recommended: false, + url: "https://eslint.org/docs/latest/rules/space-before-function-paren" + }, + + fixable: "whitespace", + + schema: [ + { + oneOf: [ + { + enum: ["always", "never"] + }, + { + type: "object", + properties: { + anonymous: { + enum: ["always", "never", "ignore"] + }, + named: { + enum: ["always", "never", "ignore"] + }, + asyncArrow: { + enum: ["always", "never", "ignore"] + } + }, + additionalProperties: false + } + ] + } + ], + + messages: { + unexpectedSpace: "Unexpected space before function parentheses.", + missingSpace: "Missing space before function parentheses." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + const baseConfig = typeof context.options[0] === "string" ? context.options[0] : "always"; + const overrideConfig = typeof context.options[0] === "object" ? context.options[0] : {}; + + /** + * Determines whether a function has a name. + * @param {ASTNode} node The function node. + * @returns {boolean} Whether the function has a name. + */ + function isNamedFunction(node) { + if (node.id) { + return true; + } + + const parent = node.parent; + + return parent.type === "MethodDefinition" || + (parent.type === "Property" && + ( + parent.kind === "get" || + parent.kind === "set" || + parent.method + ) + ); + } + + /** + * Gets the config for a given function + * @param {ASTNode} node The function node + * @returns {string} "always", "never", or "ignore" + */ + function getConfigForFunction(node) { + if (node.type === "ArrowFunctionExpression") { + + // Always ignore non-async functions and arrow functions without parens, e.g. async foo => bar + if (node.async && astUtils.isOpeningParenToken(sourceCode.getFirstToken(node, { skip: 1 }))) { + return overrideConfig.asyncArrow || baseConfig; + } + } else if (isNamedFunction(node)) { + return overrideConfig.named || baseConfig; + + // `generator-star-spacing` should warn anonymous generators. E.g. `function* () {}` + } else if (!node.generator) { + return overrideConfig.anonymous || baseConfig; + } + + return "ignore"; + } + + /** + * Checks the parens of a function node + * @param {ASTNode} node A function node + * @returns {void} + */ + function checkFunction(node) { + const functionConfig = getConfigForFunction(node); + + if (functionConfig === "ignore") { + return; + } + + const rightToken = sourceCode.getFirstToken(node, astUtils.isOpeningParenToken); + const leftToken = sourceCode.getTokenBefore(rightToken); + const hasSpacing = sourceCode.isSpaceBetweenTokens(leftToken, rightToken); + + if (hasSpacing && functionConfig === "never") { + context.report({ + node, + loc: { + start: leftToken.loc.end, + end: rightToken.loc.start + }, + messageId: "unexpectedSpace", + fix(fixer) { + const comments = sourceCode.getCommentsBefore(rightToken); + + // Don't fix anything if there's a single line comment between the left and the right token + if (comments.some(comment => comment.type === "Line")) { + return null; + } + return fixer.replaceTextRange( + [leftToken.range[1], rightToken.range[0]], + comments.reduce((text, comment) => text + sourceCode.getText(comment), "") + ); + } + }); + } else if (!hasSpacing && functionConfig === "always") { + context.report({ + node, + loc: rightToken.loc, + messageId: "missingSpace", + fix: fixer => fixer.insertTextAfter(leftToken, " ") + }); + } + } + + return { + ArrowFunctionExpression: checkFunction, + FunctionDeclaration: checkFunction, + FunctionExpression: checkFunction + }; + } }; diff --git a/node_modules/eslint/lib/rules/space-in-parens.js b/node_modules/eslint/lib/rules/space-in-parens.js index 623bb381f..d15a64317 100644 --- a/node_modules/eslint/lib/rules/space-in-parens.js +++ b/node_modules/eslint/lib/rules/space-in-parens.js @@ -11,364 +11,275 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "space-in-parens", - url: "https://eslint.style/rules/space-in-parens", - }, - }, - ], - }, - type: "layout", - - docs: { - description: "Enforce consistent spacing inside parentheses", - recommended: false, - url: "https://eslint.org/docs/latest/rules/space-in-parens", - }, - - fixable: "whitespace", - - schema: [ - { - enum: ["always", "never"], - }, - { - type: "object", - properties: { - exceptions: { - type: "array", - items: { - enum: ["{}", "[]", "()", "empty"], - }, - uniqueItems: true, - }, - }, - additionalProperties: false, - }, - ], - - messages: { - missingOpeningSpace: "There must be a space after this paren.", - missingClosingSpace: "There must be a space before this paren.", - rejectedOpeningSpace: "There should be no space after this paren.", - rejectedClosingSpace: "There should be no space before this paren.", - }, - }, - - create(context) { - const ALWAYS = context.options[0] === "always", - exceptionsArrayOptions = - (context.options[1] && context.options[1].exceptions) || [], - options = {}; - - let exceptions; - - if (exceptionsArrayOptions.length) { - options.braceException = exceptionsArrayOptions.includes("{}"); - options.bracketException = exceptionsArrayOptions.includes("[]"); - options.parenException = exceptionsArrayOptions.includes("()"); - options.empty = exceptionsArrayOptions.includes("empty"); - } - - /** - * Produces an object with the opener and closer exception values - * @returns {Object} `openers` and `closers` exception values - * @private - */ - function getExceptions() { - const openers = [], - closers = []; - - if (options.braceException) { - openers.push("{"); - closers.push("}"); - } - - if (options.bracketException) { - openers.push("["); - closers.push("]"); - } - - if (options.parenException) { - openers.push("("); - closers.push(")"); - } - - if (options.empty) { - openers.push(")"); - closers.push("("); - } - - return { - openers, - closers, - }; - } - - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - const sourceCode = context.sourceCode; - - /** - * Determines if a token is one of the exceptions for the opener paren - * @param {Object} token The token to check - * @returns {boolean} True if the token is one of the exceptions for the opener paren - */ - function isOpenerException(token) { - return exceptions.openers.includes(token.value); - } - - /** - * Determines if a token is one of the exceptions for the closer paren - * @param {Object} token The token to check - * @returns {boolean} True if the token is one of the exceptions for the closer paren - */ - function isCloserException(token) { - return exceptions.closers.includes(token.value); - } - - /** - * Determines if an opening paren is immediately followed by a required space - * @param {Object} openingParenToken The paren token - * @param {Object} tokenAfterOpeningParen The token after it - * @returns {boolean} True if the opening paren is missing a required space - */ - function openerMissingSpace(openingParenToken, tokenAfterOpeningParen) { - if ( - sourceCode.isSpaceBetweenTokens( - openingParenToken, - tokenAfterOpeningParen, - ) - ) { - return false; - } - - if ( - !options.empty && - astUtils.isClosingParenToken(tokenAfterOpeningParen) - ) { - return false; - } - - if (ALWAYS) { - return !isOpenerException(tokenAfterOpeningParen); - } - return isOpenerException(tokenAfterOpeningParen); - } - - /** - * Determines if an opening paren is immediately followed by a disallowed space - * @param {Object} openingParenToken The paren token - * @param {Object} tokenAfterOpeningParen The token after it - * @returns {boolean} True if the opening paren has a disallowed space - */ - function openerRejectsSpace(openingParenToken, tokenAfterOpeningParen) { - if ( - !astUtils.isTokenOnSameLine( - openingParenToken, - tokenAfterOpeningParen, - ) - ) { - return false; - } - - if (tokenAfterOpeningParen.type === "Line") { - return false; - } - - if ( - !sourceCode.isSpaceBetweenTokens( - openingParenToken, - tokenAfterOpeningParen, - ) - ) { - return false; - } - - if (ALWAYS) { - return isOpenerException(tokenAfterOpeningParen); - } - return !isOpenerException(tokenAfterOpeningParen); - } - - /** - * Determines if a closing paren is immediately preceded by a required space - * @param {Object} tokenBeforeClosingParen The token before the paren - * @param {Object} closingParenToken The paren token - * @returns {boolean} True if the closing paren is missing a required space - */ - function closerMissingSpace( - tokenBeforeClosingParen, - closingParenToken, - ) { - if ( - sourceCode.isSpaceBetweenTokens( - tokenBeforeClosingParen, - closingParenToken, - ) - ) { - return false; - } - - if ( - !options.empty && - astUtils.isOpeningParenToken(tokenBeforeClosingParen) - ) { - return false; - } - - if (ALWAYS) { - return !isCloserException(tokenBeforeClosingParen); - } - return isCloserException(tokenBeforeClosingParen); - } - - /** - * Determines if a closer paren is immediately preceded by a disallowed space - * @param {Object} tokenBeforeClosingParen The token before the paren - * @param {Object} closingParenToken The paren token - * @returns {boolean} True if the closing paren has a disallowed space - */ - function closerRejectsSpace( - tokenBeforeClosingParen, - closingParenToken, - ) { - if ( - !astUtils.isTokenOnSameLine( - tokenBeforeClosingParen, - closingParenToken, - ) - ) { - return false; - } - - if ( - !sourceCode.isSpaceBetweenTokens( - tokenBeforeClosingParen, - closingParenToken, - ) - ) { - return false; - } - - if (ALWAYS) { - return isCloserException(tokenBeforeClosingParen); - } - return !isCloserException(tokenBeforeClosingParen); - } - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - return { - Program: function checkParenSpaces(node) { - exceptions = getExceptions(); - const tokens = sourceCode.tokensAndComments; - - tokens.forEach((token, i) => { - const prevToken = tokens[i - 1]; - const nextToken = tokens[i + 1]; - - // if token is not an opening or closing paren token, do nothing - if ( - !astUtils.isOpeningParenToken(token) && - !astUtils.isClosingParenToken(token) - ) { - return; - } - - // if token is an opening paren and is not followed by a required space - if ( - token.value === "(" && - openerMissingSpace(token, nextToken) - ) { - context.report({ - node, - loc: token.loc, - messageId: "missingOpeningSpace", - fix(fixer) { - return fixer.insertTextAfter(token, " "); - }, - }); - } - - // if token is an opening paren and is followed by a disallowed space - if ( - token.value === "(" && - openerRejectsSpace(token, nextToken) - ) { - context.report({ - node, - loc: { - start: token.loc.end, - end: nextToken.loc.start, - }, - messageId: "rejectedOpeningSpace", - fix(fixer) { - return fixer.removeRange([ - token.range[1], - nextToken.range[0], - ]); - }, - }); - } - - // if token is a closing paren and is not preceded by a required space - if ( - token.value === ")" && - closerMissingSpace(prevToken, token) - ) { - context.report({ - node, - loc: token.loc, - messageId: "missingClosingSpace", - fix(fixer) { - return fixer.insertTextBefore(token, " "); - }, - }); - } - - // if token is a closing paren and is preceded by a disallowed space - if ( - token.value === ")" && - closerRejectsSpace(prevToken, token) - ) { - context.report({ - node, - loc: { - start: prevToken.loc.end, - end: token.loc.start, - }, - messageId: "rejectedClosingSpace", - fix(fixer) { - return fixer.removeRange([ - prevToken.range[1], - token.range[0], - ]); - }, - }); - } - }); - }, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Enforce consistent spacing inside parentheses", + recommended: false, + url: "https://eslint.org/docs/latest/rules/space-in-parens" + }, + + fixable: "whitespace", + + schema: [ + { + enum: ["always", "never"] + }, + { + type: "object", + properties: { + exceptions: { + type: "array", + items: { + enum: ["{}", "[]", "()", "empty"] + }, + uniqueItems: true + } + }, + additionalProperties: false + } + ], + + messages: { + missingOpeningSpace: "There must be a space after this paren.", + missingClosingSpace: "There must be a space before this paren.", + rejectedOpeningSpace: "There should be no space after this paren.", + rejectedClosingSpace: "There should be no space before this paren." + } + }, + + create(context) { + const ALWAYS = context.options[0] === "always", + exceptionsArrayOptions = (context.options[1] && context.options[1].exceptions) || [], + options = {}; + + let exceptions; + + if (exceptionsArrayOptions.length) { + options.braceException = exceptionsArrayOptions.includes("{}"); + options.bracketException = exceptionsArrayOptions.includes("[]"); + options.parenException = exceptionsArrayOptions.includes("()"); + options.empty = exceptionsArrayOptions.includes("empty"); + } + + /** + * Produces an object with the opener and closer exception values + * @returns {Object} `openers` and `closers` exception values + * @private + */ + function getExceptions() { + const openers = [], + closers = []; + + if (options.braceException) { + openers.push("{"); + closers.push("}"); + } + + if (options.bracketException) { + openers.push("["); + closers.push("]"); + } + + if (options.parenException) { + openers.push("("); + closers.push(")"); + } + + if (options.empty) { + openers.push(")"); + closers.push("("); + } + + return { + openers, + closers + }; + } + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + const sourceCode = context.sourceCode; + + /** + * Determines if a token is one of the exceptions for the opener paren + * @param {Object} token The token to check + * @returns {boolean} True if the token is one of the exceptions for the opener paren + */ + function isOpenerException(token) { + return exceptions.openers.includes(token.value); + } + + /** + * Determines if a token is one of the exceptions for the closer paren + * @param {Object} token The token to check + * @returns {boolean} True if the token is one of the exceptions for the closer paren + */ + function isCloserException(token) { + return exceptions.closers.includes(token.value); + } + + /** + * Determines if an opening paren is immediately followed by a required space + * @param {Object} openingParenToken The paren token + * @param {Object} tokenAfterOpeningParen The token after it + * @returns {boolean} True if the opening paren is missing a required space + */ + function openerMissingSpace(openingParenToken, tokenAfterOpeningParen) { + if (sourceCode.isSpaceBetweenTokens(openingParenToken, tokenAfterOpeningParen)) { + return false; + } + + if (!options.empty && astUtils.isClosingParenToken(tokenAfterOpeningParen)) { + return false; + } + + if (ALWAYS) { + return !isOpenerException(tokenAfterOpeningParen); + } + return isOpenerException(tokenAfterOpeningParen); + } + + /** + * Determines if an opening paren is immediately followed by a disallowed space + * @param {Object} openingParenToken The paren token + * @param {Object} tokenAfterOpeningParen The token after it + * @returns {boolean} True if the opening paren has a disallowed space + */ + function openerRejectsSpace(openingParenToken, tokenAfterOpeningParen) { + if (!astUtils.isTokenOnSameLine(openingParenToken, tokenAfterOpeningParen)) { + return false; + } + + if (tokenAfterOpeningParen.type === "Line") { + return false; + } + + if (!sourceCode.isSpaceBetweenTokens(openingParenToken, tokenAfterOpeningParen)) { + return false; + } + + if (ALWAYS) { + return isOpenerException(tokenAfterOpeningParen); + } + return !isOpenerException(tokenAfterOpeningParen); + } + + /** + * Determines if a closing paren is immediately preceded by a required space + * @param {Object} tokenBeforeClosingParen The token before the paren + * @param {Object} closingParenToken The paren token + * @returns {boolean} True if the closing paren is missing a required space + */ + function closerMissingSpace(tokenBeforeClosingParen, closingParenToken) { + if (sourceCode.isSpaceBetweenTokens(tokenBeforeClosingParen, closingParenToken)) { + return false; + } + + if (!options.empty && astUtils.isOpeningParenToken(tokenBeforeClosingParen)) { + return false; + } + + if (ALWAYS) { + return !isCloserException(tokenBeforeClosingParen); + } + return isCloserException(tokenBeforeClosingParen); + } + + /** + * Determines if a closer paren is immediately preceded by a disallowed space + * @param {Object} tokenBeforeClosingParen The token before the paren + * @param {Object} closingParenToken The paren token + * @returns {boolean} True if the closing paren has a disallowed space + */ + function closerRejectsSpace(tokenBeforeClosingParen, closingParenToken) { + if (!astUtils.isTokenOnSameLine(tokenBeforeClosingParen, closingParenToken)) { + return false; + } + + if (!sourceCode.isSpaceBetweenTokens(tokenBeforeClosingParen, closingParenToken)) { + return false; + } + + if (ALWAYS) { + return isCloserException(tokenBeforeClosingParen); + } + return !isCloserException(tokenBeforeClosingParen); + } + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + return { + Program: function checkParenSpaces(node) { + exceptions = getExceptions(); + const tokens = sourceCode.tokensAndComments; + + tokens.forEach((token, i) => { + const prevToken = tokens[i - 1]; + const nextToken = tokens[i + 1]; + + // if token is not an opening or closing paren token, do nothing + if (!astUtils.isOpeningParenToken(token) && !astUtils.isClosingParenToken(token)) { + return; + } + + // if token is an opening paren and is not followed by a required space + if (token.value === "(" && openerMissingSpace(token, nextToken)) { + context.report({ + node, + loc: token.loc, + messageId: "missingOpeningSpace", + fix(fixer) { + return fixer.insertTextAfter(token, " "); + } + }); + } + + // if token is an opening paren and is followed by a disallowed space + if (token.value === "(" && openerRejectsSpace(token, nextToken)) { + context.report({ + node, + loc: { start: token.loc.end, end: nextToken.loc.start }, + messageId: "rejectedOpeningSpace", + fix(fixer) { + return fixer.removeRange([token.range[1], nextToken.range[0]]); + } + }); + } + + // if token is a closing paren and is not preceded by a required space + if (token.value === ")" && closerMissingSpace(prevToken, token)) { + context.report({ + node, + loc: token.loc, + messageId: "missingClosingSpace", + fix(fixer) { + return fixer.insertTextBefore(token, " "); + } + }); + } + + // if token is a closing paren and is preceded by a disallowed space + if (token.value === ")" && closerRejectsSpace(prevToken, token)) { + context.report({ + node, + loc: { start: prevToken.loc.end, end: token.loc.start }, + messageId: "rejectedClosingSpace", + fix(fixer) { + return fixer.removeRange([prevToken.range[1], token.range[0]]); + } + }); + } + }); + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/space-infix-ops.js b/node_modules/eslint/lib/rules/space-infix-ops.js index 825ba05a4..400710194 100644 --- a/node_modules/eslint/lib/rules/space-infix-ops.js +++ b/node_modules/eslint/lib/rules/space-infix-ops.js @@ -11,242 +11,188 @@ const { isEqToken } = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "space-infix-ops", - url: "https://eslint.style/rules/space-infix-ops", - }, - }, - ], - }, - type: "layout", - - docs: { - description: "Require spacing around infix operators", - recommended: false, - url: "https://eslint.org/docs/latest/rules/space-infix-ops", - }, - - fixable: "whitespace", - - schema: [ - { - type: "object", - properties: { - int32Hint: { - type: "boolean", - default: false, - }, - }, - additionalProperties: false, - }, - ], - - messages: { - missingSpace: "Operator '{{operator}}' must be spaced.", - }, - }, - - create(context) { - const int32Hint = context.options[0] - ? context.options[0].int32Hint === true - : false; - const sourceCode = context.sourceCode; - - /** - * Returns the first token which violates the rule - * @param {ASTNode} left The left node of the main node - * @param {ASTNode} right The right node of the main node - * @param {string} op The operator of the main node - * @returns {Object} The violator token or null - * @private - */ - function getFirstNonSpacedToken(left, right, op) { - const operator = sourceCode.getFirstTokenBetween( - left, - right, - token => token.value === op, - ); - const prev = sourceCode.getTokenBefore(operator); - const next = sourceCode.getTokenAfter(operator); - - if ( - !sourceCode.isSpaceBetweenTokens(prev, operator) || - !sourceCode.isSpaceBetweenTokens(operator, next) - ) { - return operator; - } - - return null; - } - - /** - * Reports an AST node as a rule violation - * @param {ASTNode} mainNode The node to report - * @param {Object} culpritToken The token which has a problem - * @returns {void} - * @private - */ - function report(mainNode, culpritToken) { - context.report({ - node: mainNode, - loc: culpritToken.loc, - messageId: "missingSpace", - data: { - operator: culpritToken.value, - }, - fix(fixer) { - const previousToken = - sourceCode.getTokenBefore(culpritToken); - const afterToken = sourceCode.getTokenAfter(culpritToken); - let fixString = ""; - - if (culpritToken.range[0] - previousToken.range[1] === 0) { - fixString = " "; - } - - fixString += culpritToken.value; - - if (afterToken.range[0] - culpritToken.range[1] === 0) { - fixString += " "; - } - - return fixer.replaceText(culpritToken, fixString); - }, - }); - } - - /** - * Check if the node is binary then report - * @param {ASTNode} node node to evaluate - * @returns {void} - * @private - */ - function checkBinary(node) { - const leftNode = node.left.typeAnnotation - ? node.left.typeAnnotation - : node.left; - const rightNode = node.right; - - // search for = in AssignmentPattern nodes - const operator = node.operator || "="; - - const nonSpacedNode = getFirstNonSpacedToken( - leftNode, - rightNode, - operator, - ); - - if (nonSpacedNode) { - if (!(int32Hint && sourceCode.getText(node).endsWith("|0"))) { - report(node, nonSpacedNode); - } - } - } - - /** - * Check if the node is conditional - * @param {ASTNode} node node to evaluate - * @returns {void} - * @private - */ - function checkConditional(node) { - const nonSpacedConsequentNode = getFirstNonSpacedToken( - node.test, - node.consequent, - "?", - ); - const nonSpacedAlternateNode = getFirstNonSpacedToken( - node.consequent, - node.alternate, - ":", - ); - - if (nonSpacedConsequentNode) { - report(node, nonSpacedConsequentNode); - } - - if (nonSpacedAlternateNode) { - report(node, nonSpacedAlternateNode); - } - } - - /** - * Check if the node is a variable - * @param {ASTNode} node node to evaluate - * @returns {void} - * @private - */ - function checkVar(node) { - const leftNode = node.id.typeAnnotation - ? node.id.typeAnnotation - : node.id; - const rightNode = node.init; - - if (rightNode) { - const nonSpacedNode = getFirstNonSpacedToken( - leftNode, - rightNode, - "=", - ); - - if (nonSpacedNode) { - report(node, nonSpacedNode); - } - } - } - - return { - AssignmentExpression: checkBinary, - AssignmentPattern: checkBinary, - BinaryExpression: checkBinary, - LogicalExpression: checkBinary, - ConditionalExpression: checkConditional, - VariableDeclarator: checkVar, - - PropertyDefinition(node) { - if (!node.value) { - return; - } - - /* - * Because of computed properties and type annotations, some - * tokens may exist between `node.key` and `=`. - * Therefore, find the `=` from the right. - */ - const operatorToken = sourceCode.getTokenBefore( - node.value, - isEqToken, - ); - const leftToken = sourceCode.getTokenBefore(operatorToken); - const rightToken = sourceCode.getTokenAfter(operatorToken); - - if ( - !sourceCode.isSpaceBetweenTokens( - leftToken, - operatorToken, - ) || - !sourceCode.isSpaceBetweenTokens(operatorToken, rightToken) - ) { - report(node, operatorToken); - } - }, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Require spacing around infix operators", + recommended: false, + url: "https://eslint.org/docs/latest/rules/space-infix-ops" + }, + + fixable: "whitespace", + + schema: [ + { + type: "object", + properties: { + int32Hint: { + type: "boolean", + default: false + } + }, + additionalProperties: false + } + ], + + messages: { + missingSpace: "Operator '{{operator}}' must be spaced." + } + }, + + create(context) { + const int32Hint = context.options[0] ? context.options[0].int32Hint === true : false; + const sourceCode = context.sourceCode; + + /** + * Returns the first token which violates the rule + * @param {ASTNode} left The left node of the main node + * @param {ASTNode} right The right node of the main node + * @param {string} op The operator of the main node + * @returns {Object} The violator token or null + * @private + */ + function getFirstNonSpacedToken(left, right, op) { + const operator = sourceCode.getFirstTokenBetween(left, right, token => token.value === op); + const prev = sourceCode.getTokenBefore(operator); + const next = sourceCode.getTokenAfter(operator); + + if (!sourceCode.isSpaceBetweenTokens(prev, operator) || !sourceCode.isSpaceBetweenTokens(operator, next)) { + return operator; + } + + return null; + } + + /** + * Reports an AST node as a rule violation + * @param {ASTNode} mainNode The node to report + * @param {Object} culpritToken The token which has a problem + * @returns {void} + * @private + */ + function report(mainNode, culpritToken) { + context.report({ + node: mainNode, + loc: culpritToken.loc, + messageId: "missingSpace", + data: { + operator: culpritToken.value + }, + fix(fixer) { + const previousToken = sourceCode.getTokenBefore(culpritToken); + const afterToken = sourceCode.getTokenAfter(culpritToken); + let fixString = ""; + + if (culpritToken.range[0] - previousToken.range[1] === 0) { + fixString = " "; + } + + fixString += culpritToken.value; + + if (afterToken.range[0] - culpritToken.range[1] === 0) { + fixString += " "; + } + + return fixer.replaceText(culpritToken, fixString); + } + }); + } + + /** + * Check if the node is binary then report + * @param {ASTNode} node node to evaluate + * @returns {void} + * @private + */ + function checkBinary(node) { + const leftNode = (node.left.typeAnnotation) ? node.left.typeAnnotation : node.left; + const rightNode = node.right; + + // search for = in AssignmentPattern nodes + const operator = node.operator || "="; + + const nonSpacedNode = getFirstNonSpacedToken(leftNode, rightNode, operator); + + if (nonSpacedNode) { + if (!(int32Hint && sourceCode.getText(node).endsWith("|0"))) { + report(node, nonSpacedNode); + } + } + } + + /** + * Check if the node is conditional + * @param {ASTNode} node node to evaluate + * @returns {void} + * @private + */ + function checkConditional(node) { + const nonSpacedConsequentNode = getFirstNonSpacedToken(node.test, node.consequent, "?"); + const nonSpacedAlternateNode = getFirstNonSpacedToken(node.consequent, node.alternate, ":"); + + if (nonSpacedConsequentNode) { + report(node, nonSpacedConsequentNode); + } + + if (nonSpacedAlternateNode) { + report(node, nonSpacedAlternateNode); + } + } + + /** + * Check if the node is a variable + * @param {ASTNode} node node to evaluate + * @returns {void} + * @private + */ + function checkVar(node) { + const leftNode = (node.id.typeAnnotation) ? node.id.typeAnnotation : node.id; + const rightNode = node.init; + + if (rightNode) { + const nonSpacedNode = getFirstNonSpacedToken(leftNode, rightNode, "="); + + if (nonSpacedNode) { + report(node, nonSpacedNode); + } + } + } + + return { + AssignmentExpression: checkBinary, + AssignmentPattern: checkBinary, + BinaryExpression: checkBinary, + LogicalExpression: checkBinary, + ConditionalExpression: checkConditional, + VariableDeclarator: checkVar, + + PropertyDefinition(node) { + if (!node.value) { + return; + } + + /* + * Because of computed properties and type annotations, some + * tokens may exist between `node.key` and `=`. + * Therefore, find the `=` from the right. + */ + const operatorToken = sourceCode.getTokenBefore(node.value, isEqToken); + const leftToken = sourceCode.getTokenBefore(operatorToken); + const rightToken = sourceCode.getTokenAfter(operatorToken); + + if ( + !sourceCode.isSpaceBetweenTokens(leftToken, operatorToken) || + !sourceCode.isSpaceBetweenTokens(operatorToken, rightToken) + ) { + report(node, operatorToken); + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/space-unary-ops.js b/node_modules/eslint/lib/rules/space-unary-ops.js index ee5871474..aed43e724 100644 --- a/node_modules/eslint/lib/rules/space-unary-ops.js +++ b/node_modules/eslint/lib/rules/space-unary-ops.js @@ -15,386 +15,310 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "space-unary-ops", - url: "https://eslint.style/rules/space-unary-ops", - }, - }, - ], - }, - type: "layout", + meta: { + deprecated: true, + replacedBy: [], + type: "layout", - docs: { - description: - "Enforce consistent spacing before or after unary operators", - recommended: false, - url: "https://eslint.org/docs/latest/rules/space-unary-ops", - }, + docs: { + description: "Enforce consistent spacing before or after unary operators", + recommended: false, + url: "https://eslint.org/docs/latest/rules/space-unary-ops" + }, - fixable: "whitespace", + fixable: "whitespace", - schema: [ - { - type: "object", - properties: { - words: { - type: "boolean", - default: true, - }, - nonwords: { - type: "boolean", - default: false, - }, - overrides: { - type: "object", - additionalProperties: { - type: "boolean", - }, - }, - }, - additionalProperties: false, - }, - ], - messages: { - unexpectedBefore: - "Unexpected space before unary operator '{{operator}}'.", - unexpectedAfter: - "Unexpected space after unary operator '{{operator}}'.", - unexpectedAfterWord: - "Unexpected space after unary word operator '{{word}}'.", - wordOperator: - "Unary word operator '{{word}}' must be followed by whitespace.", - operator: - "Unary operator '{{operator}}' must be followed by whitespace.", - beforeUnaryExpressions: - "Space is required before unary expressions '{{token}}'.", - }, - }, + schema: [ + { + type: "object", + properties: { + words: { + type: "boolean", + default: true + }, + nonwords: { + type: "boolean", + default: false + }, + overrides: { + type: "object", + additionalProperties: { + type: "boolean" + } + } + }, + additionalProperties: false + } + ], + messages: { + unexpectedBefore: "Unexpected space before unary operator '{{operator}}'.", + unexpectedAfter: "Unexpected space after unary operator '{{operator}}'.", + unexpectedAfterWord: "Unexpected space after unary word operator '{{word}}'.", + wordOperator: "Unary word operator '{{word}}' must be followed by whitespace.", + operator: "Unary operator '{{operator}}' must be followed by whitespace.", + beforeUnaryExpressions: "Space is required before unary expressions '{{token}}'." + } + }, - create(context) { - const options = context.options[0] || { words: true, nonwords: false }; + create(context) { + const options = context.options[0] || { words: true, nonwords: false }; - const sourceCode = context.sourceCode; + const sourceCode = context.sourceCode; - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- - /** - * Check if the node is the first "!" in a "!!" convert to Boolean expression - * @param {ASTnode} node AST node - * @returns {boolean} Whether or not the node is first "!" in "!!" - */ - function isFirstBangInBangBangExpression(node) { - return ( - node && - node.type === "UnaryExpression" && - node.argument.operator === "!" && - node.argument && - node.argument.type === "UnaryExpression" && - node.argument.operator === "!" - ); - } + /** + * Check if the node is the first "!" in a "!!" convert to Boolean expression + * @param {ASTnode} node AST node + * @returns {boolean} Whether or not the node is first "!" in "!!" + */ + function isFirstBangInBangBangExpression(node) { + return node && node.type === "UnaryExpression" && node.argument.operator === "!" && + node.argument && node.argument.type === "UnaryExpression" && node.argument.operator === "!"; + } - /** - * Checks if an override exists for a given operator. - * @param {string} operator Operator - * @returns {boolean} Whether or not an override has been provided for the operator - */ - function overrideExistsForOperator(operator) { - return ( - options.overrides && Object.hasOwn(options.overrides, operator) - ); - } + /** + * Checks if an override exists for a given operator. + * @param {string} operator Operator + * @returns {boolean} Whether or not an override has been provided for the operator + */ + function overrideExistsForOperator(operator) { + return options.overrides && Object.prototype.hasOwnProperty.call(options.overrides, operator); + } - /** - * Gets the value that the override was set to for this operator - * @param {string} operator Operator - * @returns {boolean} Whether or not an override enforces a space with this operator - */ - function overrideEnforcesSpaces(operator) { - return options.overrides[operator]; - } + /** + * Gets the value that the override was set to for this operator + * @param {string} operator Operator + * @returns {boolean} Whether or not an override enforces a space with this operator + */ + function overrideEnforcesSpaces(operator) { + return options.overrides[operator]; + } - /** - * Verify Unary Word Operator has spaces after the word operator - * @param {ASTnode} node AST node - * @param {Object} firstToken first token from the AST node - * @param {Object} secondToken second token from the AST node - * @param {string} word The word to be used for reporting - * @returns {void} - */ - function verifyWordHasSpaces(node, firstToken, secondToken, word) { - if (secondToken.range[0] === firstToken.range[1]) { - context.report({ - node, - messageId: "wordOperator", - data: { - word, - }, - fix(fixer) { - return fixer.insertTextAfter(firstToken, " "); - }, - }); - } - } + /** + * Verify Unary Word Operator has spaces after the word operator + * @param {ASTnode} node AST node + * @param {Object} firstToken first token from the AST node + * @param {Object} secondToken second token from the AST node + * @param {string} word The word to be used for reporting + * @returns {void} + */ + function verifyWordHasSpaces(node, firstToken, secondToken, word) { + if (secondToken.range[0] === firstToken.range[1]) { + context.report({ + node, + messageId: "wordOperator", + data: { + word + }, + fix(fixer) { + return fixer.insertTextAfter(firstToken, " "); + } + }); + } + } - /** - * Verify Unary Word Operator doesn't have spaces after the word operator - * @param {ASTnode} node AST node - * @param {Object} firstToken first token from the AST node - * @param {Object} secondToken second token from the AST node - * @param {string} word The word to be used for reporting - * @returns {void} - */ - function verifyWordDoesntHaveSpaces( - node, - firstToken, - secondToken, - word, - ) { - if (astUtils.canTokensBeAdjacent(firstToken, secondToken)) { - if (secondToken.range[0] > firstToken.range[1]) { - context.report({ - node, - messageId: "unexpectedAfterWord", - data: { - word, - }, - fix(fixer) { - return fixer.removeRange([ - firstToken.range[1], - secondToken.range[0], - ]); - }, - }); - } - } - } + /** + * Verify Unary Word Operator doesn't have spaces after the word operator + * @param {ASTnode} node AST node + * @param {Object} firstToken first token from the AST node + * @param {Object} secondToken second token from the AST node + * @param {string} word The word to be used for reporting + * @returns {void} + */ + function verifyWordDoesntHaveSpaces(node, firstToken, secondToken, word) { + if (astUtils.canTokensBeAdjacent(firstToken, secondToken)) { + if (secondToken.range[0] > firstToken.range[1]) { + context.report({ + node, + messageId: "unexpectedAfterWord", + data: { + word + }, + fix(fixer) { + return fixer.removeRange([firstToken.range[1], secondToken.range[0]]); + } + }); + } + } + } - /** - * Check Unary Word Operators for spaces after the word operator - * @param {ASTnode} node AST node - * @param {Object} firstToken first token from the AST node - * @param {Object} secondToken second token from the AST node - * @param {string} word The word to be used for reporting - * @returns {void} - */ - function checkUnaryWordOperatorForSpaces( - node, - firstToken, - secondToken, - word, - ) { - if (overrideExistsForOperator(word)) { - if (overrideEnforcesSpaces(word)) { - verifyWordHasSpaces(node, firstToken, secondToken, word); - } else { - verifyWordDoesntHaveSpaces( - node, - firstToken, - secondToken, - word, - ); - } - } else if (options.words) { - verifyWordHasSpaces(node, firstToken, secondToken, word); - } else { - verifyWordDoesntHaveSpaces(node, firstToken, secondToken, word); - } - } + /** + * Check Unary Word Operators for spaces after the word operator + * @param {ASTnode} node AST node + * @param {Object} firstToken first token from the AST node + * @param {Object} secondToken second token from the AST node + * @param {string} word The word to be used for reporting + * @returns {void} + */ + function checkUnaryWordOperatorForSpaces(node, firstToken, secondToken, word) { + if (overrideExistsForOperator(word)) { + if (overrideEnforcesSpaces(word)) { + verifyWordHasSpaces(node, firstToken, secondToken, word); + } else { + verifyWordDoesntHaveSpaces(node, firstToken, secondToken, word); + } + } else if (options.words) { + verifyWordHasSpaces(node, firstToken, secondToken, word); + } else { + verifyWordDoesntHaveSpaces(node, firstToken, secondToken, word); + } + } - /** - * Verifies YieldExpressions satisfy spacing requirements - * @param {ASTnode} node AST node - * @returns {void} - */ - function checkForSpacesAfterYield(node) { - const tokens = sourceCode.getFirstTokens(node, 3), - word = "yield"; + /** + * Verifies YieldExpressions satisfy spacing requirements + * @param {ASTnode} node AST node + * @returns {void} + */ + function checkForSpacesAfterYield(node) { + const tokens = sourceCode.getFirstTokens(node, 3), + word = "yield"; - if (!node.argument || node.delegate) { - return; - } + if (!node.argument || node.delegate) { + return; + } - checkUnaryWordOperatorForSpaces(node, tokens[0], tokens[1], word); - } + checkUnaryWordOperatorForSpaces(node, tokens[0], tokens[1], word); + } - /** - * Verifies AwaitExpressions satisfy spacing requirements - * @param {ASTNode} node AwaitExpression AST node - * @returns {void} - */ - function checkForSpacesAfterAwait(node) { - const tokens = sourceCode.getFirstTokens(node, 3); + /** + * Verifies AwaitExpressions satisfy spacing requirements + * @param {ASTNode} node AwaitExpression AST node + * @returns {void} + */ + function checkForSpacesAfterAwait(node) { + const tokens = sourceCode.getFirstTokens(node, 3); - checkUnaryWordOperatorForSpaces( - node, - tokens[0], - tokens[1], - "await", - ); - } + checkUnaryWordOperatorForSpaces(node, tokens[0], tokens[1], "await"); + } - /** - * Verifies UnaryExpression, UpdateExpression and NewExpression have spaces before or after the operator - * @param {ASTnode} node AST node - * @param {Object} firstToken First token in the expression - * @param {Object} secondToken Second token in the expression - * @returns {void} - */ - function verifyNonWordsHaveSpaces(node, firstToken, secondToken) { - if (node.prefix) { - if (isFirstBangInBangBangExpression(node)) { - return; - } - if (firstToken.range[1] === secondToken.range[0]) { - context.report({ - node, - messageId: "operator", - data: { - operator: firstToken.value, - }, - fix(fixer) { - return fixer.insertTextAfter(firstToken, " "); - }, - }); - } - } else { - if (firstToken.range[1] === secondToken.range[0]) { - context.report({ - node, - messageId: "beforeUnaryExpressions", - data: { - token: secondToken.value, - }, - fix(fixer) { - return fixer.insertTextBefore(secondToken, " "); - }, - }); - } - } - } + /** + * Verifies UnaryExpression, UpdateExpression and NewExpression have spaces before or after the operator + * @param {ASTnode} node AST node + * @param {Object} firstToken First token in the expression + * @param {Object} secondToken Second token in the expression + * @returns {void} + */ + function verifyNonWordsHaveSpaces(node, firstToken, secondToken) { + if (node.prefix) { + if (isFirstBangInBangBangExpression(node)) { + return; + } + if (firstToken.range[1] === secondToken.range[0]) { + context.report({ + node, + messageId: "operator", + data: { + operator: firstToken.value + }, + fix(fixer) { + return fixer.insertTextAfter(firstToken, " "); + } + }); + } + } else { + if (firstToken.range[1] === secondToken.range[0]) { + context.report({ + node, + messageId: "beforeUnaryExpressions", + data: { + token: secondToken.value + }, + fix(fixer) { + return fixer.insertTextBefore(secondToken, " "); + } + }); + } + } + } - /** - * Verifies UnaryExpression, UpdateExpression and NewExpression don't have spaces before or after the operator - * @param {ASTnode} node AST node - * @param {Object} firstToken First token in the expression - * @param {Object} secondToken Second token in the expression - * @returns {void} - */ - function verifyNonWordsDontHaveSpaces(node, firstToken, secondToken) { - if (node.prefix) { - if (secondToken.range[0] > firstToken.range[1]) { - context.report({ - node, - messageId: "unexpectedAfter", - data: { - operator: firstToken.value, - }, - fix(fixer) { - if ( - astUtils.canTokensBeAdjacent( - firstToken, - secondToken, - ) - ) { - return fixer.removeRange([ - firstToken.range[1], - secondToken.range[0], - ]); - } - return null; - }, - }); - } - } else { - if (secondToken.range[0] > firstToken.range[1]) { - context.report({ - node, - messageId: "unexpectedBefore", - data: { - operator: secondToken.value, - }, - fix(fixer) { - return fixer.removeRange([ - firstToken.range[1], - secondToken.range[0], - ]); - }, - }); - } - } - } + /** + * Verifies UnaryExpression, UpdateExpression and NewExpression don't have spaces before or after the operator + * @param {ASTnode} node AST node + * @param {Object} firstToken First token in the expression + * @param {Object} secondToken Second token in the expression + * @returns {void} + */ + function verifyNonWordsDontHaveSpaces(node, firstToken, secondToken) { + if (node.prefix) { + if (secondToken.range[0] > firstToken.range[1]) { + context.report({ + node, + messageId: "unexpectedAfter", + data: { + operator: firstToken.value + }, + fix(fixer) { + if (astUtils.canTokensBeAdjacent(firstToken, secondToken)) { + return fixer.removeRange([firstToken.range[1], secondToken.range[0]]); + } + return null; + } + }); + } + } else { + if (secondToken.range[0] > firstToken.range[1]) { + context.report({ + node, + messageId: "unexpectedBefore", + data: { + operator: secondToken.value + }, + fix(fixer) { + return fixer.removeRange([firstToken.range[1], secondToken.range[0]]); + } + }); + } + } + } - /** - * Verifies UnaryExpression, UpdateExpression and NewExpression satisfy spacing requirements - * @param {ASTnode} node AST node - * @returns {void} - */ - function checkForSpaces(node) { - const tokens = - node.type === "UpdateExpression" && !node.prefix - ? sourceCode.getLastTokens(node, 2) - : sourceCode.getFirstTokens(node, 2); - const firstToken = tokens[0]; - const secondToken = tokens[1]; + /** + * Verifies UnaryExpression, UpdateExpression and NewExpression satisfy spacing requirements + * @param {ASTnode} node AST node + * @returns {void} + */ + function checkForSpaces(node) { + const tokens = node.type === "UpdateExpression" && !node.prefix + ? sourceCode.getLastTokens(node, 2) + : sourceCode.getFirstTokens(node, 2); + const firstToken = tokens[0]; + const secondToken = tokens[1]; - if ( - (node.type === "NewExpression" || node.prefix) && - firstToken.type === "Keyword" - ) { - checkUnaryWordOperatorForSpaces( - node, - firstToken, - secondToken, - firstToken.value, - ); - return; - } + if ((node.type === "NewExpression" || node.prefix) && firstToken.type === "Keyword") { + checkUnaryWordOperatorForSpaces(node, firstToken, secondToken, firstToken.value); + return; + } - const operator = node.prefix ? tokens[0].value : tokens[1].value; + const operator = node.prefix ? tokens[0].value : tokens[1].value; - if (overrideExistsForOperator(operator)) { - if (overrideEnforcesSpaces(operator)) { - verifyNonWordsHaveSpaces(node, firstToken, secondToken); - } else { - verifyNonWordsDontHaveSpaces(node, firstToken, secondToken); - } - } else if (options.nonwords) { - verifyNonWordsHaveSpaces(node, firstToken, secondToken); - } else { - verifyNonWordsDontHaveSpaces(node, firstToken, secondToken); - } - } + if (overrideExistsForOperator(operator)) { + if (overrideEnforcesSpaces(operator)) { + verifyNonWordsHaveSpaces(node, firstToken, secondToken); + } else { + verifyNonWordsDontHaveSpaces(node, firstToken, secondToken); + } + } else if (options.nonwords) { + verifyNonWordsHaveSpaces(node, firstToken, secondToken); + } else { + verifyNonWordsDontHaveSpaces(node, firstToken, secondToken); + } + } - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- - return { - UnaryExpression: checkForSpaces, - UpdateExpression: checkForSpaces, - NewExpression: checkForSpaces, - YieldExpression: checkForSpacesAfterYield, - AwaitExpression: checkForSpacesAfterAwait, - }; - }, + return { + UnaryExpression: checkForSpaces, + UpdateExpression: checkForSpaces, + NewExpression: checkForSpaces, + YieldExpression: checkForSpacesAfterYield, + AwaitExpression: checkForSpacesAfterAwait + }; + + } }; diff --git a/node_modules/eslint/lib/rules/spaced-comment.js b/node_modules/eslint/lib/rules/spaced-comment.js index cb2a97822..90ac7032d 100644 --- a/node_modules/eslint/lib/rules/spaced-comment.js +++ b/node_modules/eslint/lib/rules/spaced-comment.js @@ -18,7 +18,7 @@ const astUtils = require("./utils/ast-utils"); * @returns {string} An escaped string. */ function escape(s) { - return `(?:${escapeRegExp(s)})`; + return `(?:${escapeRegExp(s)})`; } /** @@ -28,7 +28,7 @@ function escape(s) { * @returns {string} An escaped string. */ function escapeAndRepeat(s) { - return `${escape(s)}+`; + return `${escape(s)}+`; } /** @@ -38,12 +38,13 @@ function escapeAndRepeat(s) { * @returns {string[]} A marker list. */ function parseMarkersOption(markers) { - // `*` is a marker for JSDoc comments. - if (!markers.includes("*")) { - return markers.concat("*"); - } - return markers; + // `*` is a marker for JSDoc comments. + if (!markers.includes("*")) { + return markers.concat("*"); + } + + return markers; } /** @@ -55,35 +56,39 @@ function parseMarkersOption(markers) { * @returns {string} A regular expression string for exceptions. */ function createExceptionsPattern(exceptions) { - let pattern = ""; - - /* - * A space or an exception pattern sequence. - * [] ==> "\s" - * ["-"] ==> "(?:\s|\-+$)" - * ["-", "="] ==> "(?:\s|(?:\-+|=+)$)" - * ["-", "=", "--=="] ==> "(?:\s|(?:\-+|=+|(?:\-\-==)+)$)" ==> https://jex.im/regulex/#!embed=false&flags=&re=(%3F%3A%5Cs%7C(%3F%3A%5C-%2B%7C%3D%2B%7C(%3F%3A%5C-%5C-%3D%3D)%2B)%24) - */ - if (exceptions.length === 0) { - // a space. - pattern += "\\s"; - } else { - // a space or... - pattern += "(?:\\s|"; - - if (exceptions.length === 1) { - // a sequence of the exception pattern. - pattern += escapeAndRepeat(exceptions[0]); - } else { - // a sequence of one of the exception patterns. - pattern += "(?:"; - pattern += exceptions.map(escapeAndRepeat).join("|"); - pattern += ")"; - } - pattern += `(?:$|[${Array.from(astUtils.LINEBREAKS).join("")}]))`; - } - - return pattern; + let pattern = ""; + + /* + * A space or an exception pattern sequence. + * [] ==> "\s" + * ["-"] ==> "(?:\s|\-+$)" + * ["-", "="] ==> "(?:\s|(?:\-+|=+)$)" + * ["-", "=", "--=="] ==> "(?:\s|(?:\-+|=+|(?:\-\-==)+)$)" ==> https://jex.im/regulex/#!embed=false&flags=&re=(%3F%3A%5Cs%7C(%3F%3A%5C-%2B%7C%3D%2B%7C(%3F%3A%5C-%5C-%3D%3D)%2B)%24) + */ + if (exceptions.length === 0) { + + // a space. + pattern += "\\s"; + } else { + + // a space or... + pattern += "(?:\\s|"; + + if (exceptions.length === 1) { + + // a sequence of the exception pattern. + pattern += escapeAndRepeat(exceptions[0]); + } else { + + // a sequence of one of the exception patterns. + pattern += "(?:"; + pattern += exceptions.map(escapeAndRepeat).join("|"); + pattern += ")"; + } + pattern += `(?:$|[${Array.from(astUtils.LINEBREAKS).join("")}]))`; + } + + return pattern; } /** @@ -97,28 +102,30 @@ function createExceptionsPattern(exceptions) { * @returns {RegExp} A RegExp object for the beginning of a comment in `always` mode. */ function createAlwaysStylePattern(markers, exceptions) { - let pattern = "^"; - - /* - * A marker or nothing. - * ["*"] ==> "\*?" - * ["*", "!"] ==> "(?:\*|!)?" - * ["*", "/", "!<"] ==> "(?:\*|\/|(?:!<))?" ==> https://jex.im/regulex/#!embed=false&flags=&re=(%3F%3A%5C*%7C%5C%2F%7C(%3F%3A!%3C))%3F - */ - if (markers.length === 1) { - // the marker. - pattern += escape(markers[0]); - } else { - // one of markers. - pattern += "(?:"; - pattern += markers.map(escape).join("|"); - pattern += ")"; - } - - pattern += "?"; // or nothing. - pattern += createExceptionsPattern(exceptions); - - return new RegExp(pattern, "u"); + let pattern = "^"; + + /* + * A marker or nothing. + * ["*"] ==> "\*?" + * ["*", "!"] ==> "(?:\*|!)?" + * ["*", "/", "!<"] ==> "(?:\*|\/|(?:!<))?" ==> https://jex.im/regulex/#!embed=false&flags=&re=(%3F%3A%5C*%7C%5C%2F%7C(%3F%3A!%3C))%3F + */ + if (markers.length === 1) { + + // the marker. + pattern += escape(markers[0]); + } else { + + // one of markers. + pattern += "(?:"; + pattern += markers.map(escape).join("|"); + pattern += ")"; + } + + pattern += "?"; // or nothing. + pattern += createExceptionsPattern(exceptions); + + return new RegExp(pattern, "u"); } /** @@ -131,317 +138,248 @@ function createAlwaysStylePattern(markers, exceptions) { * @returns {RegExp} A RegExp object for `never` mode. */ function createNeverStylePattern(markers) { - const pattern = `^(${markers.map(escape).join("|")})?[ \t]+`; + const pattern = `^(${markers.map(escape).join("|")})?[ \t]+`; - return new RegExp(pattern, "u"); + return new RegExp(pattern, "u"); } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "spaced-comment", - url: "https://eslint.style/rules/spaced-comment", - }, - }, - ], - }, - type: "suggestion", - - docs: { - description: - "Enforce consistent spacing after the `//` or `/*` in a comment", - recommended: false, - url: "https://eslint.org/docs/latest/rules/spaced-comment", - }, - - fixable: "whitespace", - - schema: [ - { - enum: ["always", "never"], - }, - { - type: "object", - properties: { - exceptions: { - type: "array", - items: { - type: "string", - }, - }, - markers: { - type: "array", - items: { - type: "string", - }, - }, - line: { - type: "object", - properties: { - exceptions: { - type: "array", - items: { - type: "string", - }, - }, - markers: { - type: "array", - items: { - type: "string", - }, - }, - }, - additionalProperties: false, - }, - block: { - type: "object", - properties: { - exceptions: { - type: "array", - items: { - type: "string", - }, - }, - markers: { - type: "array", - items: { - type: "string", - }, - }, - balanced: { - type: "boolean", - default: false, - }, - }, - additionalProperties: false, - }, - }, - additionalProperties: false, - }, - ], - - messages: { - unexpectedSpaceAfterMarker: - "Unexpected space or tab after marker ({{refChar}}) in comment.", - expectedExceptionAfter: - "Expected exception block, space or tab after '{{refChar}}' in comment.", - unexpectedSpaceBefore: - "Unexpected space or tab before '*/' in comment.", - unexpectedSpaceAfter: - "Unexpected space or tab after '{{refChar}}' in comment.", - expectedSpaceBefore: - "Expected space or tab before '*/' in comment.", - expectedSpaceAfter: - "Expected space or tab after '{{refChar}}' in comment.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - // Unless the first option is never, require a space - const requireSpace = context.options[0] !== "never"; - - /* - * Parse the second options. - * If markers don't include `"*"`, it's added automatically for JSDoc - * comments. - */ - const config = context.options[1] || {}; - const balanced = config.block && config.block.balanced; - - const styleRules = ["block", "line"].reduce((rule, type) => { - const markers = parseMarkersOption( - (config[type] && config[type].markers) || config.markers || [], - ); - const exceptions = - (config[type] && config[type].exceptions) || - config.exceptions || - []; - const endNeverPattern = "[ \t]+$"; - - // Create RegExp object for valid patterns. - rule[type] = { - beginRegex: requireSpace - ? createAlwaysStylePattern(markers, exceptions) - : createNeverStylePattern(markers), - endRegex: - balanced && requireSpace - ? new RegExp( - `${createExceptionsPattern(exceptions)}$`, - "u", - ) - : new RegExp(endNeverPattern, "u"), - hasExceptions: exceptions.length > 0, - captureMarker: new RegExp( - `^(${markers.map(escape).join("|")})`, - "u", - ), - markers: new Set(markers), - }; - - return rule; - }, {}); - - /** - * Reports a beginning spacing error with an appropriate message. - * @param {ASTNode} node A comment node to check. - * @param {string} messageId An error message to report. - * @param {Array} match An array of match results for markers. - * @param {string} refChar Character used for reference in the error message. - * @returns {void} - */ - function reportBegin(node, messageId, match, refChar) { - const type = node.type.toLowerCase(), - commentIdentifier = type === "block" ? "/*" : "//"; - - context.report({ - node, - fix(fixer) { - const start = node.range[0]; - let end = start + 2; - - if (requireSpace) { - if (match) { - end += match[0].length; - } - return fixer.insertTextAfterRange([start, end], " "); - } - end += match[0].length; - return fixer.replaceTextRange( - [start, end], - commentIdentifier + (match[1] ? match[1] : ""), - ); - }, - messageId, - data: { refChar }, - }); - } - - /** - * Reports an ending spacing error with an appropriate message. - * @param {ASTNode} node A comment node to check. - * @param {string} messageId An error message to report. - * @param {string} match An array of the matched whitespace characters. - * @returns {void} - */ - function reportEnd(node, messageId, match) { - context.report({ - node, - fix(fixer) { - if (requireSpace) { - return fixer.insertTextAfterRange( - [node.range[0], node.range[1] - 2], - " ", - ); - } - const end = node.range[1] - 2, - start = end - match[0].length; - - return fixer.replaceTextRange([start, end], ""); - }, - messageId, - }); - } - - /** - * Reports a given comment if it's invalid. - * @param {ASTNode} node a comment node to check. - * @returns {void} - */ - function checkCommentForSpace(node) { - const type = node.type.toLowerCase(), - rule = styleRules[type], - commentIdentifier = type === "block" ? "/*" : "//"; - - // Ignores empty comments and comments that consist only of a marker. - if (node.value.length === 0 || rule.markers.has(node.value)) { - return; - } - - const beginMatch = rule.beginRegex.exec(node.value); - const endMatch = rule.endRegex.exec(node.value); - - // Checks. - if (requireSpace) { - if (!beginMatch) { - const hasMarker = rule.captureMarker.exec(node.value); - const marker = hasMarker - ? commentIdentifier + hasMarker[0] - : commentIdentifier; - - if (rule.hasExceptions) { - reportBegin( - node, - "expectedExceptionAfter", - hasMarker, - marker, - ); - } else { - reportBegin( - node, - "expectedSpaceAfter", - hasMarker, - marker, - ); - } - } - - if (balanced && type === "block" && !endMatch) { - reportEnd(node, "expectedSpaceBefore"); - } - } else { - if (beginMatch) { - if (!beginMatch[1]) { - reportBegin( - node, - "unexpectedSpaceAfter", - beginMatch, - commentIdentifier, - ); - } else { - reportBegin( - node, - "unexpectedSpaceAfterMarker", - beginMatch, - beginMatch[1], - ); - } - } - - if (balanced && type === "block" && endMatch) { - reportEnd(node, "unexpectedSpaceBefore", endMatch); - } - } - } - - return { - Program() { - const comments = sourceCode.getAllComments(); - - comments - .filter(token => token.type !== "Shebang") - .forEach(checkCommentForSpace); - }, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "suggestion", + + docs: { + description: "Enforce consistent spacing after the `//` or `/*` in a comment", + recommended: false, + url: "https://eslint.org/docs/latest/rules/spaced-comment" + }, + + fixable: "whitespace", + + schema: [ + { + enum: ["always", "never"] + }, + { + type: "object", + properties: { + exceptions: { + type: "array", + items: { + type: "string" + } + }, + markers: { + type: "array", + items: { + type: "string" + } + }, + line: { + type: "object", + properties: { + exceptions: { + type: "array", + items: { + type: "string" + } + }, + markers: { + type: "array", + items: { + type: "string" + } + } + }, + additionalProperties: false + }, + block: { + type: "object", + properties: { + exceptions: { + type: "array", + items: { + type: "string" + } + }, + markers: { + type: "array", + items: { + type: "string" + } + }, + balanced: { + type: "boolean", + default: false + } + }, + additionalProperties: false + } + }, + additionalProperties: false + } + ], + + messages: { + unexpectedSpaceAfterMarker: "Unexpected space or tab after marker ({{refChar}}) in comment.", + expectedExceptionAfter: "Expected exception block, space or tab after '{{refChar}}' in comment.", + unexpectedSpaceBefore: "Unexpected space or tab before '*/' in comment.", + unexpectedSpaceAfter: "Unexpected space or tab after '{{refChar}}' in comment.", + expectedSpaceBefore: "Expected space or tab before '*/' in comment.", + expectedSpaceAfter: "Expected space or tab after '{{refChar}}' in comment." + } + }, + + create(context) { + + const sourceCode = context.sourceCode; + + // Unless the first option is never, require a space + const requireSpace = context.options[0] !== "never"; + + /* + * Parse the second options. + * If markers don't include `"*"`, it's added automatically for JSDoc + * comments. + */ + const config = context.options[1] || {}; + const balanced = config.block && config.block.balanced; + + const styleRules = ["block", "line"].reduce((rule, type) => { + const markers = parseMarkersOption(config[type] && config[type].markers || config.markers || []); + const exceptions = config[type] && config[type].exceptions || config.exceptions || []; + const endNeverPattern = "[ \t]+$"; + + // Create RegExp object for valid patterns. + rule[type] = { + beginRegex: requireSpace ? createAlwaysStylePattern(markers, exceptions) : createNeverStylePattern(markers), + endRegex: balanced && requireSpace ? new RegExp(`${createExceptionsPattern(exceptions)}$`, "u") : new RegExp(endNeverPattern, "u"), + hasExceptions: exceptions.length > 0, + captureMarker: new RegExp(`^(${markers.map(escape).join("|")})`, "u"), + markers: new Set(markers) + }; + + return rule; + }, {}); + + /** + * Reports a beginning spacing error with an appropriate message. + * @param {ASTNode} node A comment node to check. + * @param {string} messageId An error message to report. + * @param {Array} match An array of match results for markers. + * @param {string} refChar Character used for reference in the error message. + * @returns {void} + */ + function reportBegin(node, messageId, match, refChar) { + const type = node.type.toLowerCase(), + commentIdentifier = type === "block" ? "/*" : "//"; + + context.report({ + node, + fix(fixer) { + const start = node.range[0]; + let end = start + 2; + + if (requireSpace) { + if (match) { + end += match[0].length; + } + return fixer.insertTextAfterRange([start, end], " "); + } + end += match[0].length; + return fixer.replaceTextRange([start, end], commentIdentifier + (match[1] ? match[1] : "")); + + }, + messageId, + data: { refChar } + }); + } + + /** + * Reports an ending spacing error with an appropriate message. + * @param {ASTNode} node A comment node to check. + * @param {string} messageId An error message to report. + * @param {string} match An array of the matched whitespace characters. + * @returns {void} + */ + function reportEnd(node, messageId, match) { + context.report({ + node, + fix(fixer) { + if (requireSpace) { + return fixer.insertTextAfterRange([node.range[0], node.range[1] - 2], " "); + } + const end = node.range[1] - 2, + start = end - match[0].length; + + return fixer.replaceTextRange([start, end], ""); + + }, + messageId + }); + } + + /** + * Reports a given comment if it's invalid. + * @param {ASTNode} node a comment node to check. + * @returns {void} + */ + function checkCommentForSpace(node) { + const type = node.type.toLowerCase(), + rule = styleRules[type], + commentIdentifier = type === "block" ? "/*" : "//"; + + // Ignores empty comments and comments that consist only of a marker. + if (node.value.length === 0 || rule.markers.has(node.value)) { + return; + } + + const beginMatch = rule.beginRegex.exec(node.value); + const endMatch = rule.endRegex.exec(node.value); + + // Checks. + if (requireSpace) { + if (!beginMatch) { + const hasMarker = rule.captureMarker.exec(node.value); + const marker = hasMarker ? commentIdentifier + hasMarker[0] : commentIdentifier; + + if (rule.hasExceptions) { + reportBegin(node, "expectedExceptionAfter", hasMarker, marker); + } else { + reportBegin(node, "expectedSpaceAfter", hasMarker, marker); + } + } + + if (balanced && type === "block" && !endMatch) { + reportEnd(node, "expectedSpaceBefore"); + } + } else { + if (beginMatch) { + if (!beginMatch[1]) { + reportBegin(node, "unexpectedSpaceAfter", beginMatch, commentIdentifier); + } else { + reportBegin(node, "unexpectedSpaceAfterMarker", beginMatch, beginMatch[1]); + } + } + + if (balanced && type === "block" && endMatch) { + reportEnd(node, "unexpectedSpaceBefore", endMatch); + } + } + } + + return { + Program() { + const comments = sourceCode.getAllComments(); + + comments.filter(token => token.type !== "Shebang").forEach(checkCommentForSpace); + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/strict.js b/node_modules/eslint/lib/rules/strict.js index 381ad6f5d..f9dd7500b 100644 --- a/node_modules/eslint/lib/rules/strict.js +++ b/node_modules/eslint/lib/rules/strict.js @@ -22,23 +22,23 @@ const astUtils = require("./utils/ast-utils"); * @returns {ASTNode[]} All of the Use Strict Directives. */ function getUseStrictDirectives(statements) { - const directives = []; - - for (let i = 0; i < statements.length; i++) { - const statement = statements[i]; - - if ( - statement.type === "ExpressionStatement" && - statement.expression.type === "Literal" && - statement.expression.value === "use strict" - ) { - directives[i] = statement; - } else { - break; - } - } - - return directives; + const directives = []; + + for (let i = 0; i < statements.length; i++) { + const statement = statements[i]; + + if ( + statement.type === "ExpressionStatement" && + statement.expression.type === "Literal" && + statement.expression.value === "use strict" + ) { + directives[i] = statement; + } else { + break; + } + } + + return directives; } /** @@ -47,7 +47,7 @@ function getUseStrictDirectives(statements) { * @returns {boolean} `true` if the node is an Identifier node. */ function isSimpleParameter(node) { - return node.type === "Identifier"; + return node.type === "Identifier"; } /** @@ -56,259 +56,222 @@ function isSimpleParameter(node) { * @returns {boolean} `true` if the every parameter is an Identifier node. */ function isSimpleParameterList(params) { - return params.every(isSimpleParameter); + return params.every(isSimpleParameter); } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - defaultOptions: ["safe"], - - docs: { - description: "Require or disallow strict mode directives", - recommended: false, - url: "https://eslint.org/docs/latest/rules/strict", - }, - - schema: [ - { - enum: ["never", "global", "function", "safe"], - }, - ], - - fixable: "code", - messages: { - function: "Use the function form of 'use strict'.", - global: "Use the global form of 'use strict'.", - multiple: "Multiple 'use strict' directives.", - never: "Strict mode is not permitted.", - unnecessary: "Unnecessary 'use strict' directive.", - module: "'use strict' is unnecessary inside of modules.", - implied: - "'use strict' is unnecessary when implied strict mode is enabled.", - unnecessaryInClasses: - "'use strict' is unnecessary inside of classes.", - nonSimpleParameterList: - "'use strict' directive inside a function with non-simple parameter list throws a syntax error since ES2016.", - wrap: "Wrap {{name}} in a function with 'use strict' directive.", - }, - }, - - create(context) { - const ecmaFeatures = - context.languageOptions.parserOptions.ecmaFeatures || {}, - scopes = [], - classScopes = []; - let [mode] = context.options; - - if (ecmaFeatures.impliedStrict) { - mode = "implied"; - } else if (mode === "safe") { - mode = - ecmaFeatures.globalReturn || - context.languageOptions.sourceType === "commonjs" - ? "global" - : "function"; - } - - /** - * Determines whether a reported error should be fixed, depending on the error type. - * @param {string} errorType The type of error - * @returns {boolean} `true` if the reported error should be fixed - */ - function shouldFix(errorType) { - return ( - errorType === "multiple" || - errorType === "unnecessary" || - errorType === "module" || - errorType === "implied" || - errorType === "unnecessaryInClasses" - ); - } - - /** - * Gets a fixer function to remove a given 'use strict' directive. - * @param {ASTNode} node The directive that should be removed - * @returns {Function} A fixer function - */ - function getFixFunction(node) { - return fixer => fixer.remove(node); - } - - /** - * Report a slice of an array of nodes with a given message. - * @param {ASTNode[]} nodes Nodes. - * @param {string} start Index to start from. - * @param {string} end Index to end before. - * @param {string} messageId Message to display. - * @param {boolean} fix `true` if the directive should be fixed (i.e. removed) - * @returns {void} - */ - function reportSlice(nodes, start, end, messageId, fix) { - nodes.slice(start, end).forEach(node => { - context.report({ - node, - messageId, - fix: fix ? getFixFunction(node) : null, - }); - }); - } - - /** - * Report all nodes in an array with a given message. - * @param {ASTNode[]} nodes Nodes. - * @param {string} messageId Message id to display. - * @param {boolean} fix `true` if the directive should be fixed (i.e. removed) - * @returns {void} - */ - function reportAll(nodes, messageId, fix) { - reportSlice(nodes, 0, nodes.length, messageId, fix); - } - - /** - * Report all nodes in an array, except the first, with a given message. - * @param {ASTNode[]} nodes Nodes. - * @param {string} messageId Message id to display. - * @param {boolean} fix `true` if the directive should be fixed (i.e. removed) - * @returns {void} - */ - function reportAllExceptFirst(nodes, messageId, fix) { - reportSlice(nodes, 1, nodes.length, messageId, fix); - } - - /** - * Entering a function in 'function' mode pushes a new nested scope onto the - * stack. The new scope is true if the nested function is strict mode code. - * @param {ASTNode} node The function declaration or expression. - * @param {ASTNode[]} useStrictDirectives The Use Strict Directives of the node. - * @returns {void} - */ - function enterFunctionInFunctionMode(node, useStrictDirectives) { - const isInClass = classScopes.length > 0, - isParentGlobal = - scopes.length === 0 && classScopes.length === 0, - isParentStrict = scopes.length > 0 && scopes.at(-1), - isStrict = useStrictDirectives.length > 0; - - if (isStrict) { - if (!isSimpleParameterList(node.params)) { - context.report({ - node: useStrictDirectives[0], - messageId: "nonSimpleParameterList", - }); - } else if (isParentStrict) { - context.report({ - node: useStrictDirectives[0], - messageId: "unnecessary", - fix: getFixFunction(useStrictDirectives[0]), - }); - } else if (isInClass) { - context.report({ - node: useStrictDirectives[0], - messageId: "unnecessaryInClasses", - fix: getFixFunction(useStrictDirectives[0]), - }); - } - - reportAllExceptFirst(useStrictDirectives, "multiple", true); - } else if (isParentGlobal) { - if (isSimpleParameterList(node.params)) { - context.report({ node, messageId: "function" }); - } else { - context.report({ - node, - messageId: "wrap", - data: { name: astUtils.getFunctionNameWithKind(node) }, - }); - } - } - - scopes.push(isParentStrict || isStrict); - } - - /** - * Exiting a function in 'function' mode pops its scope off the stack. - * @returns {void} - */ - function exitFunctionInFunctionMode() { - scopes.pop(); - } - - /** - * Enter a function and either: - * - Push a new nested scope onto the stack (in 'function' mode). - * - Report all the Use Strict Directives (in the other modes). - * @param {ASTNode} node The function declaration or expression. - * @returns {void} - */ - function enterFunction(node) { - const isBlock = node.body.type === "BlockStatement", - useStrictDirectives = isBlock - ? getUseStrictDirectives(node.body.body) - : []; - - if (mode === "function") { - enterFunctionInFunctionMode(node, useStrictDirectives); - } else if (useStrictDirectives.length > 0) { - if (isSimpleParameterList(node.params)) { - reportAll(useStrictDirectives, mode, shouldFix(mode)); - } else { - context.report({ - node: useStrictDirectives[0], - messageId: "nonSimpleParameterList", - }); - reportAllExceptFirst(useStrictDirectives, "multiple", true); - } - } - } - - const rule = { - Program(node) { - const useStrictDirectives = getUseStrictDirectives(node.body); - - if (node.sourceType === "module") { - mode = "module"; - } - - if (mode === "global") { - if ( - node.body.length > 0 && - useStrictDirectives.length === 0 - ) { - context.report({ node, messageId: "global" }); - } - reportAllExceptFirst(useStrictDirectives, "multiple", true); - } else { - reportAll(useStrictDirectives, mode, shouldFix(mode)); - } - }, - FunctionDeclaration: enterFunction, - FunctionExpression: enterFunction, - ArrowFunctionExpression: enterFunction, - }; - - if (mode === "function") { - Object.assign(rule, { - // Inside of class bodies are always strict mode. - ClassBody() { - classScopes.push(true); - }, - "ClassBody:exit"() { - classScopes.pop(); - }, - - "FunctionDeclaration:exit": exitFunctionInFunctionMode, - "FunctionExpression:exit": exitFunctionInFunctionMode, - "ArrowFunctionExpression:exit": exitFunctionInFunctionMode, - }); - } - - return rule; - }, + meta: { + type: "suggestion", + + docs: { + description: "Require or disallow strict mode directives", + recommended: false, + url: "https://eslint.org/docs/latest/rules/strict" + }, + + schema: [ + { + enum: ["never", "global", "function", "safe"] + } + ], + + fixable: "code", + messages: { + function: "Use the function form of 'use strict'.", + global: "Use the global form of 'use strict'.", + multiple: "Multiple 'use strict' directives.", + never: "Strict mode is not permitted.", + unnecessary: "Unnecessary 'use strict' directive.", + module: "'use strict' is unnecessary inside of modules.", + implied: "'use strict' is unnecessary when implied strict mode is enabled.", + unnecessaryInClasses: "'use strict' is unnecessary inside of classes.", + nonSimpleParameterList: "'use strict' directive inside a function with non-simple parameter list throws a syntax error since ES2016.", + wrap: "Wrap {{name}} in a function with 'use strict' directive." + } + }, + + create(context) { + + const ecmaFeatures = context.parserOptions.ecmaFeatures || {}, + scopes = [], + classScopes = []; + let mode = context.options[0] || "safe"; + + if (ecmaFeatures.impliedStrict) { + mode = "implied"; + } else if (mode === "safe") { + mode = ecmaFeatures.globalReturn || context.languageOptions.sourceType === "commonjs" ? "global" : "function"; + } + + /** + * Determines whether a reported error should be fixed, depending on the error type. + * @param {string} errorType The type of error + * @returns {boolean} `true` if the reported error should be fixed + */ + function shouldFix(errorType) { + return errorType === "multiple" || errorType === "unnecessary" || errorType === "module" || errorType === "implied" || errorType === "unnecessaryInClasses"; + } + + /** + * Gets a fixer function to remove a given 'use strict' directive. + * @param {ASTNode} node The directive that should be removed + * @returns {Function} A fixer function + */ + function getFixFunction(node) { + return fixer => fixer.remove(node); + } + + /** + * Report a slice of an array of nodes with a given message. + * @param {ASTNode[]} nodes Nodes. + * @param {string} start Index to start from. + * @param {string} end Index to end before. + * @param {string} messageId Message to display. + * @param {boolean} fix `true` if the directive should be fixed (i.e. removed) + * @returns {void} + */ + function reportSlice(nodes, start, end, messageId, fix) { + nodes.slice(start, end).forEach(node => { + context.report({ node, messageId, fix: fix ? getFixFunction(node) : null }); + }); + } + + /** + * Report all nodes in an array with a given message. + * @param {ASTNode[]} nodes Nodes. + * @param {string} messageId Message id to display. + * @param {boolean} fix `true` if the directive should be fixed (i.e. removed) + * @returns {void} + */ + function reportAll(nodes, messageId, fix) { + reportSlice(nodes, 0, nodes.length, messageId, fix); + } + + /** + * Report all nodes in an array, except the first, with a given message. + * @param {ASTNode[]} nodes Nodes. + * @param {string} messageId Message id to display. + * @param {boolean} fix `true` if the directive should be fixed (i.e. removed) + * @returns {void} + */ + function reportAllExceptFirst(nodes, messageId, fix) { + reportSlice(nodes, 1, nodes.length, messageId, fix); + } + + /** + * Entering a function in 'function' mode pushes a new nested scope onto the + * stack. The new scope is true if the nested function is strict mode code. + * @param {ASTNode} node The function declaration or expression. + * @param {ASTNode[]} useStrictDirectives The Use Strict Directives of the node. + * @returns {void} + */ + function enterFunctionInFunctionMode(node, useStrictDirectives) { + const isInClass = classScopes.length > 0, + isParentGlobal = scopes.length === 0 && classScopes.length === 0, + isParentStrict = scopes.length > 0 && scopes[scopes.length - 1], + isStrict = useStrictDirectives.length > 0; + + if (isStrict) { + if (!isSimpleParameterList(node.params)) { + context.report({ node: useStrictDirectives[0], messageId: "nonSimpleParameterList" }); + } else if (isParentStrict) { + context.report({ node: useStrictDirectives[0], messageId: "unnecessary", fix: getFixFunction(useStrictDirectives[0]) }); + } else if (isInClass) { + context.report({ node: useStrictDirectives[0], messageId: "unnecessaryInClasses", fix: getFixFunction(useStrictDirectives[0]) }); + } + + reportAllExceptFirst(useStrictDirectives, "multiple", true); + } else if (isParentGlobal) { + if (isSimpleParameterList(node.params)) { + context.report({ node, messageId: "function" }); + } else { + context.report({ + node, + messageId: "wrap", + data: { name: astUtils.getFunctionNameWithKind(node) } + }); + } + } + + scopes.push(isParentStrict || isStrict); + } + + /** + * Exiting a function in 'function' mode pops its scope off the stack. + * @returns {void} + */ + function exitFunctionInFunctionMode() { + scopes.pop(); + } + + /** + * Enter a function and either: + * - Push a new nested scope onto the stack (in 'function' mode). + * - Report all the Use Strict Directives (in the other modes). + * @param {ASTNode} node The function declaration or expression. + * @returns {void} + */ + function enterFunction(node) { + const isBlock = node.body.type === "BlockStatement", + useStrictDirectives = isBlock + ? getUseStrictDirectives(node.body.body) : []; + + if (mode === "function") { + enterFunctionInFunctionMode(node, useStrictDirectives); + } else if (useStrictDirectives.length > 0) { + if (isSimpleParameterList(node.params)) { + reportAll(useStrictDirectives, mode, shouldFix(mode)); + } else { + context.report({ node: useStrictDirectives[0], messageId: "nonSimpleParameterList" }); + reportAllExceptFirst(useStrictDirectives, "multiple", true); + } + } + } + + const rule = { + Program(node) { + const useStrictDirectives = getUseStrictDirectives(node.body); + + if (node.sourceType === "module") { + mode = "module"; + } + + if (mode === "global") { + if (node.body.length > 0 && useStrictDirectives.length === 0) { + context.report({ node, messageId: "global" }); + } + reportAllExceptFirst(useStrictDirectives, "multiple", true); + } else { + reportAll(useStrictDirectives, mode, shouldFix(mode)); + } + }, + FunctionDeclaration: enterFunction, + FunctionExpression: enterFunction, + ArrowFunctionExpression: enterFunction + }; + + if (mode === "function") { + Object.assign(rule, { + + // Inside of class bodies are always strict mode. + ClassBody() { + classScopes.push(true); + }, + "ClassBody:exit"() { + classScopes.pop(); + }, + + "FunctionDeclaration:exit": exitFunctionInFunctionMode, + "FunctionExpression:exit": exitFunctionInFunctionMode, + "ArrowFunctionExpression:exit": exitFunctionInFunctionMode + }); + } + + return rule; + } }; diff --git a/node_modules/eslint/lib/rules/switch-colon-spacing.js b/node_modules/eslint/lib/rules/switch-colon-spacing.js index 410345b69..3ea63ca0e 100644 --- a/node_modules/eslint/lib/rules/switch-colon-spacing.js +++ b/node_modules/eslint/lib/rules/switch-colon-spacing.js @@ -16,143 +16,117 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "switch-colon-spacing", - url: "https://eslint.style/rules/switch-colon-spacing", - }, - }, - ], - }, - type: "layout", + meta: { + deprecated: true, + replacedBy: [], + type: "layout", - docs: { - description: "Enforce spacing around colons of switch statements", - recommended: false, - url: "https://eslint.org/docs/latest/rules/switch-colon-spacing", - }, + docs: { + description: "Enforce spacing around colons of switch statements", + recommended: false, + url: "https://eslint.org/docs/latest/rules/switch-colon-spacing" + }, - schema: [ - { - type: "object", - properties: { - before: { type: "boolean", default: false }, - after: { type: "boolean", default: true }, - }, - additionalProperties: false, - }, - ], - fixable: "whitespace", - messages: { - expectedBefore: "Expected space(s) before this colon.", - expectedAfter: "Expected space(s) after this colon.", - unexpectedBefore: "Unexpected space(s) before this colon.", - unexpectedAfter: "Unexpected space(s) after this colon.", - }, - }, + schema: [ + { + type: "object", + properties: { + before: { type: "boolean", default: false }, + after: { type: "boolean", default: true } + }, + additionalProperties: false + } + ], + fixable: "whitespace", + messages: { + expectedBefore: "Expected space(s) before this colon.", + expectedAfter: "Expected space(s) after this colon.", + unexpectedBefore: "Unexpected space(s) before this colon.", + unexpectedAfter: "Unexpected space(s) after this colon." + } + }, - create(context) { - const sourceCode = context.sourceCode; - const options = context.options[0] || {}; - const beforeSpacing = options.before === true; // false by default - const afterSpacing = options.after !== false; // true by default + create(context) { + const sourceCode = context.sourceCode; + const options = context.options[0] || {}; + const beforeSpacing = options.before === true; // false by default + const afterSpacing = options.after !== false; // true by default - /** - * Check whether the spacing between the given 2 tokens is valid or not. - * @param {Token} left The left token to check. - * @param {Token} right The right token to check. - * @param {boolean} expected The expected spacing to check. `true` if there should be a space. - * @returns {boolean} `true` if the spacing between the tokens is valid. - */ - function isValidSpacing(left, right, expected) { - return ( - astUtils.isClosingBraceToken(right) || - !astUtils.isTokenOnSameLine(left, right) || - sourceCode.isSpaceBetweenTokens(left, right) === expected - ); - } + /** + * Check whether the spacing between the given 2 tokens is valid or not. + * @param {Token} left The left token to check. + * @param {Token} right The right token to check. + * @param {boolean} expected The expected spacing to check. `true` if there should be a space. + * @returns {boolean} `true` if the spacing between the tokens is valid. + */ + function isValidSpacing(left, right, expected) { + return ( + astUtils.isClosingBraceToken(right) || + !astUtils.isTokenOnSameLine(left, right) || + sourceCode.isSpaceBetweenTokens(left, right) === expected + ); + } - /** - * Check whether comments exist between the given 2 tokens. - * @param {Token} left The left token to check. - * @param {Token} right The right token to check. - * @returns {boolean} `true` if comments exist between the given 2 tokens. - */ - function commentsExistBetween(left, right) { - return ( - sourceCode.getFirstTokenBetween(left, right, { - includeComments: true, - filter: astUtils.isCommentToken, - }) !== null - ); - } + /** + * Check whether comments exist between the given 2 tokens. + * @param {Token} left The left token to check. + * @param {Token} right The right token to check. + * @returns {boolean} `true` if comments exist between the given 2 tokens. + */ + function commentsExistBetween(left, right) { + return sourceCode.getFirstTokenBetween( + left, + right, + { + includeComments: true, + filter: astUtils.isCommentToken + } + ) !== null; + } - /** - * Fix the spacing between the given 2 tokens. - * @param {RuleFixer} fixer The fixer to fix. - * @param {Token} left The left token of fix range. - * @param {Token} right The right token of fix range. - * @param {boolean} spacing The spacing style. `true` if there should be a space. - * @returns {Fix|null} The fix object. - */ - function fix(fixer, left, right, spacing) { - if (commentsExistBetween(left, right)) { - return null; - } - if (spacing) { - return fixer.insertTextAfter(left, " "); - } - return fixer.removeRange([left.range[1], right.range[0]]); - } + /** + * Fix the spacing between the given 2 tokens. + * @param {RuleFixer} fixer The fixer to fix. + * @param {Token} left The left token of fix range. + * @param {Token} right The right token of fix range. + * @param {boolean} spacing The spacing style. `true` if there should be a space. + * @returns {Fix|null} The fix object. + */ + function fix(fixer, left, right, spacing) { + if (commentsExistBetween(left, right)) { + return null; + } + if (spacing) { + return fixer.insertTextAfter(left, " "); + } + return fixer.removeRange([left.range[1], right.range[0]]); + } - return { - SwitchCase(node) { - const colonToken = astUtils.getSwitchCaseColonToken( - node, - sourceCode, - ); - const beforeToken = sourceCode.getTokenBefore(colonToken); - const afterToken = sourceCode.getTokenAfter(colonToken); + return { + SwitchCase(node) { + const colonToken = astUtils.getSwitchCaseColonToken(node, sourceCode); + const beforeToken = sourceCode.getTokenBefore(colonToken); + const afterToken = sourceCode.getTokenAfter(colonToken); - if (!isValidSpacing(beforeToken, colonToken, beforeSpacing)) { - context.report({ - node, - loc: colonToken.loc, - messageId: beforeSpacing - ? "expectedBefore" - : "unexpectedBefore", - fix: fixer => - fix(fixer, beforeToken, colonToken, beforeSpacing), - }); - } - if (!isValidSpacing(colonToken, afterToken, afterSpacing)) { - context.report({ - node, - loc: colonToken.loc, - messageId: afterSpacing - ? "expectedAfter" - : "unexpectedAfter", - fix: fixer => - fix(fixer, colonToken, afterToken, afterSpacing), - }); - } - }, - }; - }, + if (!isValidSpacing(beforeToken, colonToken, beforeSpacing)) { + context.report({ + node, + loc: colonToken.loc, + messageId: beforeSpacing ? "expectedBefore" : "unexpectedBefore", + fix: fixer => fix(fixer, beforeToken, colonToken, beforeSpacing) + }); + } + if (!isValidSpacing(colonToken, afterToken, afterSpacing)) { + context.report({ + node, + loc: colonToken.loc, + messageId: afterSpacing ? "expectedAfter" : "unexpectedAfter", + fix: fixer => fix(fixer, colonToken, afterToken, afterSpacing) + }); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/symbol-description.js b/node_modules/eslint/lib/rules/symbol-description.js index 054c16bec..4528f09cf 100644 --- a/node_modules/eslint/lib/rules/symbol-description.js +++ b/node_modules/eslint/lib/rules/symbol-description.js @@ -15,56 +15,59 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ + +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", + meta: { + type: "suggestion", + + docs: { + description: "Require symbol descriptions", + recommended: false, + url: "https://eslint.org/docs/latest/rules/symbol-description" + }, + fixable: null, + schema: [], + messages: { + expected: "Expected Symbol to have a description." + } + }, + + create(context) { - docs: { - description: "Require symbol descriptions", - recommended: false, - url: "https://eslint.org/docs/latest/rules/symbol-description", - }, - fixable: null, - schema: [], - messages: { - expected: "Expected Symbol to have a description.", - }, - }, + const sourceCode = context.sourceCode; - create(context) { - const sourceCode = context.sourceCode; + /** + * Reports if node does not conform the rule in case rule is set to + * report missing description + * @param {ASTNode} node A CallExpression node to check. + * @returns {void} + */ + function checkArgument(node) { + if (node.arguments.length === 0) { + context.report({ + node, + messageId: "expected" + }); + } + } - /** - * Reports if node does not conform the rule in case rule is set to - * report missing description - * @param {ASTNode} node A CallExpression node to check. - * @returns {void} - */ - function checkArgument(node) { - if (node.arguments.length === 0) { - context.report({ - node, - messageId: "expected", - }); - } - } + return { + "Program:exit"(node) { + const scope = sourceCode.getScope(node); + const variable = astUtils.getVariableByName(scope, "Symbol"); - return { - "Program:exit"(node) { - const scope = sourceCode.getScope(node); - const variable = astUtils.getVariableByName(scope, "Symbol"); + if (variable && variable.defs.length === 0) { + variable.references.forEach(reference => { + const idNode = reference.identifier; - if (variable && variable.defs.length === 0) { - variable.references.forEach(reference => { - const idNode = reference.identifier; + if (astUtils.isCallee(idNode)) { + checkArgument(idNode.parent); + } + }); + } + } + }; - if (astUtils.isCallee(idNode)) { - checkArgument(idNode.parent); - } - }); - } - }, - }; - }, + } }; diff --git a/node_modules/eslint/lib/rules/template-curly-spacing.js b/node_modules/eslint/lib/rules/template-curly-spacing.js index 25e3aad02..1f8cc34c1 100644 --- a/node_modules/eslint/lib/rules/template-curly-spacing.js +++ b/node_modules/eslint/lib/rules/template-curly-spacing.js @@ -16,153 +16,129 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "template-curly-spacing", - url: "https://eslint.style/rules/template-curly-spacing", - }, - }, - ], - }, - type: "layout", - - docs: { - description: - "Require or disallow spacing around embedded expressions of template strings", - recommended: false, - url: "https://eslint.org/docs/latest/rules/template-curly-spacing", - }, - - fixable: "whitespace", - - schema: [{ enum: ["always", "never"] }], - messages: { - expectedBefore: "Expected space(s) before '}'.", - expectedAfter: "Expected space(s) after '${'.", - unexpectedBefore: "Unexpected space(s) before '}'.", - unexpectedAfter: "Unexpected space(s) after '${'.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - const always = context.options[0] === "always"; - - /** - * Checks spacing before `}` of a given token. - * @param {Token} token A token to check. This is a Template token. - * @returns {void} - */ - function checkSpacingBefore(token) { - if (!token.value.startsWith("}")) { - return; // starts with a backtick, this is the first template element in the template literal - } - - const prevToken = sourceCode.getTokenBefore(token, { - includeComments: true, - }), - hasSpace = sourceCode.isSpaceBetween(prevToken, token); - - if (!astUtils.isTokenOnSameLine(prevToken, token)) { - return; - } - - if (always && !hasSpace) { - context.report({ - loc: { - start: token.loc.start, - end: { - line: token.loc.start.line, - column: token.loc.start.column + 1, - }, - }, - messageId: "expectedBefore", - fix: fixer => fixer.insertTextBefore(token, " "), - }); - } - - if (!always && hasSpace) { - context.report({ - loc: { - start: prevToken.loc.end, - end: token.loc.start, - }, - messageId: "unexpectedBefore", - fix: fixer => - fixer.removeRange([prevToken.range[1], token.range[0]]), - }); - } - } - - /** - * Checks spacing after `${` of a given token. - * @param {Token} token A token to check. This is a Template token. - * @returns {void} - */ - function checkSpacingAfter(token) { - if (!token.value.endsWith("${")) { - return; // ends with a backtick, this is the last template element in the template literal - } - - const nextToken = sourceCode.getTokenAfter(token, { - includeComments: true, - }), - hasSpace = sourceCode.isSpaceBetween(token, nextToken); - - if (!astUtils.isTokenOnSameLine(token, nextToken)) { - return; - } - - if (always && !hasSpace) { - context.report({ - loc: { - start: { - line: token.loc.end.line, - column: token.loc.end.column - 2, - }, - end: token.loc.end, - }, - messageId: "expectedAfter", - fix: fixer => fixer.insertTextAfter(token, " "), - }); - } - - if (!always && hasSpace) { - context.report({ - loc: { - start: token.loc.end, - end: nextToken.loc.start, - }, - messageId: "unexpectedAfter", - fix: fixer => - fixer.removeRange([token.range[1], nextToken.range[0]]), - }); - } - } - - return { - TemplateElement(node) { - const token = sourceCode.getFirstToken(node); - - checkSpacingBefore(token); - checkSpacingAfter(token); - }, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Require or disallow spacing around embedded expressions of template strings", + recommended: false, + url: "https://eslint.org/docs/latest/rules/template-curly-spacing" + }, + + fixable: "whitespace", + + schema: [ + { enum: ["always", "never"] } + ], + messages: { + expectedBefore: "Expected space(s) before '}'.", + expectedAfter: "Expected space(s) after '${'.", + unexpectedBefore: "Unexpected space(s) before '}'.", + unexpectedAfter: "Unexpected space(s) after '${'." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + const always = context.options[0] === "always"; + + /** + * Checks spacing before `}` of a given token. + * @param {Token} token A token to check. This is a Template token. + * @returns {void} + */ + function checkSpacingBefore(token) { + if (!token.value.startsWith("}")) { + return; // starts with a backtick, this is the first template element in the template literal + } + + const prevToken = sourceCode.getTokenBefore(token, { includeComments: true }), + hasSpace = sourceCode.isSpaceBetween(prevToken, token); + + if (!astUtils.isTokenOnSameLine(prevToken, token)) { + return; + } + + if (always && !hasSpace) { + context.report({ + loc: { + start: token.loc.start, + end: { + line: token.loc.start.line, + column: token.loc.start.column + 1 + } + }, + messageId: "expectedBefore", + fix: fixer => fixer.insertTextBefore(token, " ") + }); + } + + if (!always && hasSpace) { + context.report({ + loc: { + start: prevToken.loc.end, + end: token.loc.start + }, + messageId: "unexpectedBefore", + fix: fixer => fixer.removeRange([prevToken.range[1], token.range[0]]) + }); + } + } + + /** + * Checks spacing after `${` of a given token. + * @param {Token} token A token to check. This is a Template token. + * @returns {void} + */ + function checkSpacingAfter(token) { + if (!token.value.endsWith("${")) { + return; // ends with a backtick, this is the last template element in the template literal + } + + const nextToken = sourceCode.getTokenAfter(token, { includeComments: true }), + hasSpace = sourceCode.isSpaceBetween(token, nextToken); + + if (!astUtils.isTokenOnSameLine(token, nextToken)) { + return; + } + + if (always && !hasSpace) { + context.report({ + loc: { + start: { + line: token.loc.end.line, + column: token.loc.end.column - 2 + }, + end: token.loc.end + }, + messageId: "expectedAfter", + fix: fixer => fixer.insertTextAfter(token, " ") + }); + } + + if (!always && hasSpace) { + context.report({ + loc: { + start: token.loc.end, + end: nextToken.loc.start + }, + messageId: "unexpectedAfter", + fix: fixer => fixer.removeRange([token.range[1], nextToken.range[0]]) + }); + } + } + + return { + TemplateElement(node) { + const token = sourceCode.getFirstToken(node); + + checkSpacingBefore(token); + checkSpacingAfter(token); + } + }; + } }; diff --git a/node_modules/eslint/lib/rules/template-tag-spacing.js b/node_modules/eslint/lib/rules/template-tag-spacing.js index 10ffe6804..52e0bcf20 100644 --- a/node_modules/eslint/lib/rules/template-tag-spacing.js +++ b/node_modules/eslint/lib/rules/template-tag-spacing.js @@ -10,112 +10,84 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "template-tag-spacing", - url: "https://eslint.style/rules/template-tag-spacing", - }, - }, - ], - }, - type: "layout", + meta: { + deprecated: true, + replacedBy: [], + type: "layout", - docs: { - description: - "Require or disallow spacing between template tags and their literals", - recommended: false, - url: "https://eslint.org/docs/latest/rules/template-tag-spacing", - }, + docs: { + description: "Require or disallow spacing between template tags and their literals", + recommended: false, + url: "https://eslint.org/docs/latest/rules/template-tag-spacing" + }, - fixable: "whitespace", + fixable: "whitespace", - schema: [{ enum: ["always", "never"] }], - messages: { - unexpected: - "Unexpected space between template tag and template literal.", - missing: "Missing space between template tag and template literal.", - }, - }, + schema: [ + { enum: ["always", "never"] } + ], + messages: { + unexpected: "Unexpected space between template tag and template literal.", + missing: "Missing space between template tag and template literal." + } + }, - create(context) { - const never = context.options[0] !== "always"; - const sourceCode = context.sourceCode; + create(context) { + const never = context.options[0] !== "always"; + const sourceCode = context.sourceCode; - /** - * Check if a space is present between a template tag and its literal - * @param {ASTNode} node node to evaluate - * @returns {void} - * @private - */ - function checkSpacing(node) { - const tagToken = sourceCode.getTokenBefore(node.quasi); - const literalToken = sourceCode.getFirstToken(node.quasi); - const hasWhitespace = sourceCode.isSpaceBetweenTokens( - tagToken, - literalToken, - ); + /** + * Check if a space is present between a template tag and its literal + * @param {ASTNode} node node to evaluate + * @returns {void} + * @private + */ + function checkSpacing(node) { + const tagToken = sourceCode.getTokenBefore(node.quasi); + const literalToken = sourceCode.getFirstToken(node.quasi); + const hasWhitespace = sourceCode.isSpaceBetweenTokens(tagToken, literalToken); - if (never && hasWhitespace) { - context.report({ - node, - loc: { - start: tagToken.loc.end, - end: literalToken.loc.start, - }, - messageId: "unexpected", - fix(fixer) { - const comments = sourceCode.getCommentsBefore( - node.quasi, - ); + if (never && hasWhitespace) { + context.report({ + node, + loc: { + start: tagToken.loc.end, + end: literalToken.loc.start + }, + messageId: "unexpected", + fix(fixer) { + const comments = sourceCode.getCommentsBefore(node.quasi); - // Don't fix anything if there's a single line comment after the template tag - if (comments.some(comment => comment.type === "Line")) { - return null; - } + // Don't fix anything if there's a single line comment after the template tag + if (comments.some(comment => comment.type === "Line")) { + return null; + } - return fixer.replaceTextRange( - [tagToken.range[1], literalToken.range[0]], - comments.reduce( - (text, comment) => - text + sourceCode.getText(comment), - "", - ), - ); - }, - }); - } else if (!never && !hasWhitespace) { - context.report({ - node, - loc: { - start: node.loc.start, - end: literalToken.loc.start, - }, - messageId: "missing", - fix(fixer) { - return fixer.insertTextAfter(tagToken, " "); - }, - }); - } - } + return fixer.replaceTextRange( + [tagToken.range[1], literalToken.range[0]], + comments.reduce((text, comment) => text + sourceCode.getText(comment), "") + ); + } + }); + } else if (!never && !hasWhitespace) { + context.report({ + node, + loc: { + start: node.loc.start, + end: literalToken.loc.start + }, + messageId: "missing", + fix(fixer) { + return fixer.insertTextAfter(tagToken, " "); + } + }); + } + } - return { - TaggedTemplateExpression: checkSpacing, - }; - }, + return { + TaggedTemplateExpression: checkSpacing + }; + } }; diff --git a/node_modules/eslint/lib/rules/unicode-bom.js b/node_modules/eslint/lib/rules/unicode-bom.js index 7b0544891..09971d26e 100644 --- a/node_modules/eslint/lib/rules/unicode-bom.js +++ b/node_modules/eslint/lib/rules/unicode-bom.js @@ -8,66 +8,66 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "layout", + meta: { + type: "layout", - defaultOptions: ["never"], + docs: { + description: "Require or disallow Unicode byte order mark (BOM)", + recommended: false, + url: "https://eslint.org/docs/latest/rules/unicode-bom" + }, - docs: { - description: "Require or disallow Unicode byte order mark (BOM)", - recommended: false, - url: "https://eslint.org/docs/latest/rules/unicode-bom", - }, + fixable: "whitespace", - fixable: "whitespace", + schema: [ + { + enum: ["always", "never"] + } + ], + messages: { + expected: "Expected Unicode BOM (Byte Order Mark).", + unexpected: "Unexpected Unicode BOM (Byte Order Mark)." + } + }, - schema: [ - { - enum: ["always", "never"], - }, - ], - messages: { - expected: "Expected Unicode BOM (Byte Order Mark).", - unexpected: "Unexpected Unicode BOM (Byte Order Mark).", - }, - }, + create(context) { - create(context) { - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- - return { - Program: function checkUnicodeBOM(node) { - const sourceCode = context.sourceCode, - location = { column: 0, line: 1 }; - const [requireBOM] = context.options; + return { - if (!sourceCode.hasBOM && requireBOM === "always") { - context.report({ - node, - loc: location, - messageId: "expected", - fix(fixer) { - return fixer.insertTextBeforeRange( - [0, 1], - "\uFEFF", - ); - }, - }); - } else if (sourceCode.hasBOM && requireBOM === "never") { - context.report({ - node, - loc: location, - messageId: "unexpected", - fix(fixer) { - return fixer.removeRange([-1, 0]); - }, - }); - } - }, - }; - }, + Program: function checkUnicodeBOM(node) { + + const sourceCode = context.sourceCode, + location = { column: 0, line: 1 }, + requireBOM = context.options[0] || "never"; + + if (!sourceCode.hasBOM && (requireBOM === "always")) { + context.report({ + node, + loc: location, + messageId: "expected", + fix(fixer) { + return fixer.insertTextBeforeRange([0, 1], "\uFEFF"); + } + }); + } else if (sourceCode.hasBOM && (requireBOM === "never")) { + context.report({ + node, + loc: location, + messageId: "unexpected", + fix(fixer) { + return fixer.removeRange([-1, 0]); + } + }); + } + } + + }; + + } }; diff --git a/node_modules/eslint/lib/rules/use-isnan.js b/node_modules/eslint/lib/rules/use-isnan.js index 2c73acf7e..21dc39529 100644 --- a/node_modules/eslint/lib/rules/use-isnan.js +++ b/node_modules/eslint/lib/rules/use-isnan.js @@ -21,248 +21,121 @@ const astUtils = require("./utils/ast-utils"); * @returns {boolean} `true` if the node is 'NaN' identifier. */ function isNaNIdentifier(node) { - if (!node) { - return false; - } - - const nodeToCheck = - node.type === "SequenceExpression" ? node.expressions.at(-1) : node; - - return ( - astUtils.isSpecificId(nodeToCheck, "NaN") || - astUtils.isSpecificMemberAccess(nodeToCheck, "Number", "NaN") - ); + return Boolean(node) && ( + astUtils.isSpecificId(node, "NaN") || + astUtils.isSpecificMemberAccess(node, "Number", "NaN") + ); } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - hasSuggestions: true, - type: "problem", - - docs: { - description: "Require calls to `isNaN()` when checking for `NaN`", - recommended: true, - url: "https://eslint.org/docs/latest/rules/use-isnan", - }, - - schema: [ - { - type: "object", - properties: { - enforceForSwitchCase: { - type: "boolean", - }, - enforceForIndexOf: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - - defaultOptions: [ - { - enforceForIndexOf: false, - enforceForSwitchCase: true, - }, - ], - - messages: { - comparisonWithNaN: "Use the isNaN function to compare with NaN.", - switchNaN: - "'switch(NaN)' can never match a case clause. Use Number.isNaN instead of the switch.", - caseNaN: - "'case NaN' can never match. Use Number.isNaN before the switch.", - indexOfNaN: - "Array prototype method '{{ methodName }}' cannot find NaN.", - replaceWithIsNaN: "Replace with Number.isNaN.", - replaceWithCastingAndIsNaN: - "Replace with Number.isNaN and cast to a Number.", - replaceWithFindIndex: - "Replace with Array.prototype.{{ methodName }}.", - }, - }, - - create(context) { - const [{ enforceForIndexOf, enforceForSwitchCase }] = context.options; - const sourceCode = context.sourceCode; - - const fixableOperators = new Set(["==", "===", "!=", "!=="]); - const castableOperators = new Set(["==", "!="]); - - /** - * Get a fixer for a binary expression that compares to NaN. - * @param {ASTNode} node The node to fix. - * @param {function(string): string} wrapValue A function that wraps the compared value with a fix. - * @returns {function(Fixer): Fix} The fixer function. - */ - function getBinaryExpressionFixer(node, wrapValue) { - return fixer => { - const comparedValue = isNaNIdentifier(node.left) - ? node.right - : node.left; - const shouldWrap = comparedValue.type === "SequenceExpression"; - const shouldNegate = node.operator[0] === "!"; - - const negation = shouldNegate ? "!" : ""; - let comparedValueText = sourceCode.getText(comparedValue); - - if (shouldWrap) { - comparedValueText = `(${comparedValueText})`; - } - - const fixedValue = wrapValue(comparedValueText); - - return fixer.replaceText(node, `${negation}${fixedValue}`); - }; - } - - /** - * Checks the given `BinaryExpression` node for `foo === NaN` and other comparisons. - * @param {ASTNode} node The node to check. - * @returns {void} - */ - function checkBinaryExpression(node) { - if ( - /^(?:[<>]|[!=]=)=?$/u.test(node.operator) && - (isNaNIdentifier(node.left) || isNaNIdentifier(node.right)) - ) { - const suggestedFixes = []; - const NaNNode = isNaNIdentifier(node.left) - ? node.left - : node.right; - - const isSequenceExpression = - NaNNode.type === "SequenceExpression"; - const isSuggestable = - fixableOperators.has(node.operator) && - !isSequenceExpression; - const isCastable = castableOperators.has(node.operator); - - if (isSuggestable) { - suggestedFixes.push({ - messageId: "replaceWithIsNaN", - fix: getBinaryExpressionFixer( - node, - value => `Number.isNaN(${value})`, - ), - }); - - if (isCastable) { - suggestedFixes.push({ - messageId: "replaceWithCastingAndIsNaN", - fix: getBinaryExpressionFixer( - node, - value => `Number.isNaN(Number(${value}))`, - ), - }); - } - } - - context.report({ - node, - messageId: "comparisonWithNaN", - suggest: suggestedFixes, - }); - } - } - - /** - * Checks the discriminant and all case clauses of the given `SwitchStatement` node for `switch(NaN)` and `case NaN:` - * @param {ASTNode} node The node to check. - * @returns {void} - */ - function checkSwitchStatement(node) { - if (isNaNIdentifier(node.discriminant)) { - context.report({ node, messageId: "switchNaN" }); - } - - for (const switchCase of node.cases) { - if (isNaNIdentifier(switchCase.test)) { - context.report({ node: switchCase, messageId: "caseNaN" }); - } - } - } - - /** - * Checks the given `CallExpression` node for `.indexOf(NaN)` and `.lastIndexOf(NaN)`. - * @param {ASTNode} node The node to check. - * @returns {void} - */ - function checkCallExpression(node) { - const callee = astUtils.skipChainExpression(node.callee); - - if (callee.type === "MemberExpression") { - const methodName = astUtils.getStaticPropertyName(callee); - - if ( - (methodName === "indexOf" || - methodName === "lastIndexOf") && - node.arguments.length <= 2 && - isNaNIdentifier(node.arguments[0]) - ) { - /* - * To retain side effects, it's essential to address `NaN` beforehand, which - * is not possible with fixes like `arr.findIndex(Number.isNaN)`. - */ - const isSuggestable = - node.arguments[0].type !== "SequenceExpression" && - !node.arguments[1]; - const suggestedFixes = []; - - if (isSuggestable) { - const shouldWrap = callee.computed; - const findIndexMethod = - methodName === "indexOf" - ? "findIndex" - : "findLastIndex"; - const propertyName = shouldWrap - ? `"${findIndexMethod}"` - : findIndexMethod; - - suggestedFixes.push({ - messageId: "replaceWithFindIndex", - data: { methodName: findIndexMethod }, - fix: fixer => [ - fixer.replaceText( - callee.property, - propertyName, - ), - fixer.replaceText( - node.arguments[0], - "Number.isNaN", - ), - ], - }); - } - - context.report({ - node, - messageId: "indexOfNaN", - data: { methodName }, - suggest: suggestedFixes, - }); - } - } - } - - const listeners = { - BinaryExpression: checkBinaryExpression, - }; - - if (enforceForSwitchCase) { - listeners.SwitchStatement = checkSwitchStatement; - } - - if (enforceForIndexOf) { - listeners.CallExpression = checkCallExpression; - } - - return listeners; - }, + meta: { + type: "problem", + + docs: { + description: "Require calls to `isNaN()` when checking for `NaN`", + recommended: true, + url: "https://eslint.org/docs/latest/rules/use-isnan" + }, + + schema: [ + { + type: "object", + properties: { + enforceForSwitchCase: { + type: "boolean", + default: true + }, + enforceForIndexOf: { + type: "boolean", + default: false + } + }, + additionalProperties: false + } + ], + + messages: { + comparisonWithNaN: "Use the isNaN function to compare with NaN.", + switchNaN: "'switch(NaN)' can never match a case clause. Use Number.isNaN instead of the switch.", + caseNaN: "'case NaN' can never match. Use Number.isNaN before the switch.", + indexOfNaN: "Array prototype method '{{ methodName }}' cannot find NaN." + } + }, + + create(context) { + + const enforceForSwitchCase = !context.options[0] || context.options[0].enforceForSwitchCase; + const enforceForIndexOf = context.options[0] && context.options[0].enforceForIndexOf; + + /** + * Checks the given `BinaryExpression` node for `foo === NaN` and other comparisons. + * @param {ASTNode} node The node to check. + * @returns {void} + */ + function checkBinaryExpression(node) { + if ( + /^(?:[<>]|[!=]=)=?$/u.test(node.operator) && + (isNaNIdentifier(node.left) || isNaNIdentifier(node.right)) + ) { + context.report({ node, messageId: "comparisonWithNaN" }); + } + } + + /** + * Checks the discriminant and all case clauses of the given `SwitchStatement` node for `switch(NaN)` and `case NaN:` + * @param {ASTNode} node The node to check. + * @returns {void} + */ + function checkSwitchStatement(node) { + if (isNaNIdentifier(node.discriminant)) { + context.report({ node, messageId: "switchNaN" }); + } + + for (const switchCase of node.cases) { + if (isNaNIdentifier(switchCase.test)) { + context.report({ node: switchCase, messageId: "caseNaN" }); + } + } + } + + /** + * Checks the given `CallExpression` node for `.indexOf(NaN)` and `.lastIndexOf(NaN)`. + * @param {ASTNode} node The node to check. + * @returns {void} + */ + function checkCallExpression(node) { + const callee = astUtils.skipChainExpression(node.callee); + + if (callee.type === "MemberExpression") { + const methodName = astUtils.getStaticPropertyName(callee); + + if ( + (methodName === "indexOf" || methodName === "lastIndexOf") && + node.arguments.length === 1 && + isNaNIdentifier(node.arguments[0]) + ) { + context.report({ node, messageId: "indexOfNaN", data: { methodName } }); + } + } + } + + const listeners = { + BinaryExpression: checkBinaryExpression + }; + + if (enforceForSwitchCase) { + listeners.SwitchStatement = checkSwitchStatement; + } + + if (enforceForIndexOf) { + listeners.CallExpression = checkCallExpression; + } + + return listeners; + } }; diff --git a/node_modules/eslint/lib/rules/utils/ast-utils.js b/node_modules/eslint/lib/rules/utils/ast-utils.js index dff0945d3..962bdde0a 100644 --- a/node_modules/eslint/lib/rules/utils/ast-utils.js +++ b/node_modules/eslint/lib/rules/utils/ast-utils.js @@ -14,85 +14,62 @@ const esutils = require("esutils"); const espree = require("espree"); const escapeRegExp = require("escape-string-regexp"); const { - breakableTypePattern, - createGlobalLinebreakMatcher, - lineBreakPattern, - shebangPattern, + breakableTypePattern, + createGlobalLinebreakMatcher, + lineBreakPattern, + shebangPattern } = require("../../shared/ast-utils"); -const globals = require("../../../conf/globals"); -const { LATEST_ECMA_VERSION } = require("../../../conf/ecma-version"); //------------------------------------------------------------------------------ // Helpers //------------------------------------------------------------------------------ -const anyFunctionPattern = - /^(?:Function(?:Declaration|Expression)|ArrowFunctionExpression)$/u; +const anyFunctionPattern = /^(?:Function(?:Declaration|Expression)|ArrowFunctionExpression)$/u; const anyLoopPattern = /^(?:DoWhile|For|ForIn|ForOf|While)Statement$/u; -const arrayMethodWithThisArgPattern = - /^(?:every|filter|find(?:Last)?(?:Index)?|flatMap|forEach|map|some)$/u; +const arrayMethodWithThisArgPattern = /^(?:every|filter|find(?:Last)?(?:Index)?|flatMap|forEach|map|some)$/u; const arrayOrTypedArrayPattern = /Array$/u; const bindOrCallOrApplyPattern = /^(?:bind|call|apply)$/u; const thisTagPattern = /^[\s*]*@this/mu; -const COMMENTS_IGNORE_PATTERN = - /^\s*(?:eslint|jshint\s+|jslint\s+|istanbul\s+|globals?\s+|exported\s+|jscs)/u; + +const COMMENTS_IGNORE_PATTERN = /^\s*(?:eslint|jshint\s+|jslint\s+|istanbul\s+|globals?\s+|exported\s+|jscs)/u; const ESLINT_DIRECTIVE_PATTERN = /^(?:eslint[- ]|(?:globals?|exported) )/u; const LINEBREAKS = new Set(["\r\n", "\r", "\n", "\u2028", "\u2029"]); // A set of node types that can contain a list of statements -const STATEMENT_LIST_PARENTS = new Set([ - "Program", - "BlockStatement", - "StaticBlock", - "SwitchCase", -]); -const LEXICAL_DECLARATION_KINDS = new Set([ - "let", - "const", - "using", - "await using", -]); +const STATEMENT_LIST_PARENTS = new Set(["Program", "BlockStatement", "StaticBlock", "SwitchCase"]); const DECIMAL_INTEGER_PATTERN = /^(?:0|0[0-7]*[89]\d*|[1-9](?:_?\d)*)$/u; // Tests the presence of at least one LegacyOctalEscapeSequence or NonOctalDecimalEscapeSequence in a raw string -const OCTAL_OR_NON_OCTAL_DECIMAL_ESCAPE_PATTERN = - /^(?:[^\\]|\\.)*\\(?:[1-9]|0\d)/su; +const OCTAL_OR_NON_OCTAL_DECIMAL_ESCAPE_PATTERN = /^(?:[^\\]|\\.)*\\(?:[1-9]|0[0-9])/su; const LOGICAL_ASSIGNMENT_OPERATORS = new Set(["&&=", "||=", "??="]); -/** - * All builtin global variables defined in the latest ECMAScript specification. - * @type {Record} Key is the name of the variable. Value is `true` if the variable is considered writable, `false` otherwise. - */ -const ECMASCRIPT_GLOBALS = globals[`es${LATEST_ECMA_VERSION}`]; - /** * Checks reference if is non initializer and writable. * @param {Reference} reference A reference to check. - * @param {number} index The index of the reference in the references. + * @param {int} index The index of the reference in the references. * @param {Reference[]} references The array that the reference belongs to. * @returns {boolean} Success/Failure * @private */ function isModifyingReference(reference, index, references) { - const identifier = reference.identifier; - - /* - * Destructuring assignments can have multiple default value, so - * possibly there are multiple writeable references for the same - * identifier. - */ - const modifyingDifferentIdentifier = - index === 0 || references[index - 1].identifier !== identifier; - - return ( - identifier && - reference.init === false && - reference.isWrite() && - modifyingDifferentIdentifier - ); + const identifier = reference.identifier; + + /* + * Destructuring assignments can have multiple default value, so + * possibly there are multiple writeable references for the same + * identifier. + */ + const modifyingDifferentIdentifier = index === 0 || + references[index - 1].identifier !== identifier; + + return (identifier && + reference.init === false && + reference.isWrite() && + modifyingDifferentIdentifier + ); } /** @@ -101,7 +78,7 @@ function isModifyingReference(reference, index, references) { * @returns {boolean} `true` if the string starts with uppercase. */ function startsWithUpperCase(s) { - return s[0] !== s[0].toLocaleLowerCase(); + return s[0] !== s[0].toLocaleLowerCase(); } /** @@ -110,7 +87,7 @@ function startsWithUpperCase(s) { * @returns {boolean} Whether or not a node is a constructor. */ function isES5Constructor(node) { - return node.id && startsWithUpperCase(node.id.name); + return (node.id && startsWithUpperCase(node.id.name)); } /** @@ -119,16 +96,12 @@ function isES5Constructor(node) { * @returns {Node|null} A found function node. */ function getUpperFunction(node) { - for ( - let currentNode = node; - currentNode; - currentNode = currentNode.parent - ) { - if (anyFunctionPattern.test(currentNode.type)) { - return currentNode; - } - } - return null; + for (let currentNode = node; currentNode; currentNode = currentNode.parent) { + if (anyFunctionPattern.test(currentNode.type)) { + return currentNode; + } + } + return null; } /** @@ -142,7 +115,7 @@ function getUpperFunction(node) { * @returns {boolean} `true` if the node is a function node. */ function isFunction(node) { - return Boolean(node && anyFunctionPattern.test(node.type)); + return Boolean(node && anyFunctionPattern.test(node.type)); } /** @@ -158,7 +131,7 @@ function isFunction(node) { * @returns {boolean} `true` if the node is a loop node. */ function isLoop(node) { - return Boolean(node && anyLoopPattern.test(node.type)); + return Boolean(node && anyLoopPattern.test(node.type)); } /** @@ -167,17 +140,13 @@ function isLoop(node) { * @returns {boolean} `true` if the node is in a loop. */ function isInLoop(node) { - for ( - let currentNode = node; - currentNode && !isFunction(currentNode); - currentNode = currentNode.parent - ) { - if (isLoop(currentNode)) { - return true; - } - } - - return false; + for (let currentNode = node; currentNode && !isFunction(currentNode); currentNode = currentNode.parent) { + if (isLoop(currentNode)) { + return true; + } + } + + return false; } /** @@ -186,19 +155,15 @@ function isInLoop(node) { * @returns {boolean} `true` if the node is a `null` literal */ function isNullLiteral(node) { - /* - * Checking `node.value === null` does not guarantee that a literal is a null literal. - * When parsing values that cannot be represented in the current environment (e.g. unicode - * regexes in Node 4), `node.value` is set to `null` because it wouldn't be possible to - * set `node.value` to a unicode regex. To make sure a literal is actually `null`, check - * `node.regex` instead. Also see: https://github.com/eslint/eslint/issues/8020 - */ - return ( - node.type === "Literal" && - node.value === null && - !node.regex && - !node.bigint - ); + + /* + * Checking `node.value === null` does not guarantee that a literal is a null literal. + * When parsing values that cannot be represented in the current environment (e.g. unicode + * regexes in Node 4), `node.value` is set to `null` because it wouldn't be possible to + * set `node.value` to a unicode regex. To make sure a literal is actually `null`, check + * `node.regex` instead. Also see: https://github.com/eslint/eslint/issues/8020 + */ + return node.type === "Literal" && node.value === null && !node.regex && !node.bigint; } /** @@ -208,11 +173,11 @@ function isNullLiteral(node) { * @public */ function isNullOrUndefined(node) { - return ( - isNullLiteral(node) || - (node.type === "Identifier" && node.name === "undefined") || - (node.type === "UnaryExpression" && node.operator === "void") - ); + return ( + isNullLiteral(node) || + (node.type === "Identifier" && node.name === "undefined") || + (node.type === "UnaryExpression" && node.operator === "void") + ); } /** @@ -221,7 +186,7 @@ function isNullOrUndefined(node) { * @returns {boolean} Whether or not the node is callee. */ function isCallee(node) { - return node.parent.type === "CallExpression" && node.parent.callee === node; + return node.parent.type === "CallExpression" && node.parent.callee === node; } /** @@ -234,34 +199,35 @@ function isCallee(node) { * @returns {string|null} String value if it can be determined. Otherwise, `null`. */ function getStaticStringValue(node) { - switch (node.type) { - case "Literal": - if (node.value === null) { - if (isNullLiteral(node)) { - return String(node.value); // "null" - } - if (node.regex) { - return `/${node.regex.pattern}/${node.regex.flags}`; - } - if (node.bigint) { - return node.bigint; - } - - // Otherwise, this is an unknown literal. The function will return null. - } else { - return String(node.value); - } - break; - case "TemplateLiteral": - if (node.expressions.length === 0 && node.quasis.length === 1) { - return node.quasis[0].value.cooked; - } - break; - - // no default - } - - return null; + switch (node.type) { + case "Literal": + if (node.value === null) { + if (isNullLiteral(node)) { + return String(node.value); // "null" + } + if (node.regex) { + return `/${node.regex.pattern}/${node.regex.flags}`; + } + if (node.bigint) { + return node.bigint; + } + + // Otherwise, this is an unknown literal. The function will return null. + + } else { + return String(node.value); + } + break; + case "TemplateLiteral": + if (node.expressions.length === 0 && node.quasis.length === 1) { + return node.quasis[0].value.cooked; + } + break; + + // no default + } + + return null; } /** @@ -295,36 +261,34 @@ function getStaticStringValue(node) { * @returns {string|null} The property name if static. Otherwise, null. */ function getStaticPropertyName(node) { - let prop; + let prop; - switch (node && node.type) { - case "ChainExpression": - return getStaticPropertyName(node.expression); + switch (node && node.type) { + case "ChainExpression": + return getStaticPropertyName(node.expression); - case "Property": - case "PropertyDefinition": - case "MethodDefinition": - case "TSPropertySignature": - case "TSMethodSignature": - prop = node.key; - break; + case "Property": + case "PropertyDefinition": + case "MethodDefinition": + prop = node.key; + break; - case "MemberExpression": - prop = node.property; - break; + case "MemberExpression": + prop = node.property; + break; - // no default - } + // no default + } - if (prop) { - if (prop.type === "Identifier" && !node.computed) { - return prop.name; - } + if (prop) { + if (prop.type === "Identifier" && !node.computed) { + return prop.name; + } - return getStaticStringValue(prop); - } + return getStaticStringValue(prop); + } - return null; + return null; } /** @@ -333,7 +297,7 @@ function getStaticPropertyName(node) { * @returns {ASTNode} The `ChainExpression#expression` value if the node is a `ChainExpression` node. Otherwise, the node. */ function skipChainExpression(node) { - return node && node.type === "ChainExpression" ? node.expression : node; + return node && node.type === "ChainExpression" ? node.expression : node; } /** @@ -343,9 +307,9 @@ function skipChainExpression(node) { * @returns {boolean} `true` if the `actual` is an expected value. */ function checkText(actual, expected) { - return typeof expected === "string" - ? actual === expected - : expected.test(actual); + return typeof expected === "string" + ? actual === expected + : expected.test(actual); } /** @@ -355,7 +319,7 @@ function checkText(actual, expected) { * @returns {boolean} `true` if the node is an Identifier node with the name. */ function isSpecificId(node, name) { - return node.type === "Identifier" && checkText(node.name, name); + return node.type === "Identifier" && checkText(node.name, name); } /** @@ -368,28 +332,25 @@ function isSpecificId(node, name) { * The node is a `MemberExpression` or `ChainExpression`. */ function isSpecificMemberAccess(node, objectName, propertyName) { - const checkNode = skipChainExpression(node); + const checkNode = skipChainExpression(node); - if (checkNode.type !== "MemberExpression") { - return false; - } + if (checkNode.type !== "MemberExpression") { + return false; + } - if (objectName && !isSpecificId(checkNode.object, objectName)) { - return false; - } + if (objectName && !isSpecificId(checkNode.object, objectName)) { + return false; + } - if (propertyName) { - const actualPropertyName = getStaticPropertyName(checkNode); + if (propertyName) { + const actualPropertyName = getStaticPropertyName(checkNode); - if ( - typeof actualPropertyName !== "string" || - !checkText(actualPropertyName, propertyName) - ) { - return false; - } - } + if (typeof actualPropertyName !== "string" || !checkText(actualPropertyName, propertyName)) { + return false; + } + } - return true; + return true; } /** @@ -399,22 +360,23 @@ function isSpecificMemberAccess(node, objectName, propertyName) { * @returns {boolean} `true` if the two literal nodes are the same value. */ function equalLiteralValue(left, right) { - // RegExp literal. - if (left.regex || right.regex) { - return Boolean( - left.regex && - right.regex && - left.regex.pattern === right.regex.pattern && - left.regex.flags === right.regex.flags, - ); - } - - // BigInt literal. - if (left.bigint || right.bigint) { - return left.bigint === right.bigint; - } - - return left.value === right.value; + + // RegExp literal. + if (left.regex || right.regex) { + return Boolean( + left.regex && + right.regex && + left.regex.pattern === right.regex.pattern && + left.regex.flags === right.regex.flags + ); + } + + // BigInt literal. + if (left.bigint || right.bigint) { + return left.bigint === right.bigint; + } + + return left.value === right.value; } /** @@ -429,83 +391,61 @@ function equalLiteralValue(left, right) { * @returns {boolean} `true` if both sides match and reference the same value. */ function isSameReference(left, right, disableStaticComputedKey = false) { - if (left.type !== right.type) { - // Handle `a.b` and `a?.b` are samely. - if (left.type === "ChainExpression") { - return isSameReference( - left.expression, - right, - disableStaticComputedKey, - ); - } - if (right.type === "ChainExpression") { - return isSameReference( - left, - right.expression, - disableStaticComputedKey, - ); - } - - return false; - } - - switch (left.type) { - case "Super": - case "ThisExpression": - return true; - - case "Identifier": - case "PrivateIdentifier": - return left.name === right.name; - case "Literal": - return equalLiteralValue(left, right); - - case "ChainExpression": - return isSameReference( - left.expression, - right.expression, - disableStaticComputedKey, - ); - - case "MemberExpression": { - if (!disableStaticComputedKey) { - const nameA = getStaticPropertyName(left); - - // x.y = x["y"] - if (nameA !== null) { - return ( - isSameReference( - left.object, - right.object, - disableStaticComputedKey, - ) && nameA === getStaticPropertyName(right) - ); - } - } - - /* - * x[0] = x[0] - * x[y] = x[y] - * x.y = x.y - */ - return ( - left.computed === right.computed && - isSameReference( - left.object, - right.object, - disableStaticComputedKey, - ) && - isSameReference( - left.property, - right.property, - disableStaticComputedKey, - ) - ); - } - - default: - return false; - } + if (left.type !== right.type) { + + // Handle `a.b` and `a?.b` are samely. + if (left.type === "ChainExpression") { + return isSameReference(left.expression, right, disableStaticComputedKey); + } + if (right.type === "ChainExpression") { + return isSameReference(left, right.expression, disableStaticComputedKey); + } + + return false; + } + + switch (left.type) { + case "Super": + case "ThisExpression": + return true; + + case "Identifier": + case "PrivateIdentifier": + return left.name === right.name; + case "Literal": + return equalLiteralValue(left, right); + + case "ChainExpression": + return isSameReference(left.expression, right.expression, disableStaticComputedKey); + + case "MemberExpression": { + if (!disableStaticComputedKey) { + const nameA = getStaticPropertyName(left); + + // x.y = x["y"] + if (nameA !== null) { + return ( + isSameReference(left.object, right.object, disableStaticComputedKey) && + nameA === getStaticPropertyName(right) + ); + } + } + + /* + * x[0] = x[0] + * x[y] = x[y] + * x.y = x.y + */ + return ( + left.computed === right.computed && + isSameReference(left.object, right.object, disableStaticComputedKey) && + isSameReference(left.property, right.property, disableStaticComputedKey) + ); + } + + default: + return false; + } } /** @@ -514,7 +454,7 @@ function isSameReference(left, right, disableStaticComputedKey = false) { * @returns {boolean} Whether or not the node is a `Reflect.apply`. */ function isReflectApply(node) { - return isSpecificMemberAccess(node, "Reflect", "apply"); + return isSpecificMemberAccess(node, "Reflect", "apply"); } /** @@ -523,7 +463,7 @@ function isReflectApply(node) { * @returns {boolean} Whether or not the node is a `Array.from`. */ function isArrayFromMethod(node) { - return isSpecificMemberAccess(node, arrayOrTypedArrayPattern, "from"); + return isSpecificMemberAccess(node, arrayOrTypedArrayPattern, "from"); } /** @@ -532,7 +472,7 @@ function isArrayFromMethod(node) { * @returns {boolean} Whether or not the node is a method which expects a function as a first argument, and `thisArg` as a second argument. */ function isMethodWhichHasThisArg(node) { - return isSpecificMemberAccess(node, null, arrayMethodWithThisArgPattern); + return isSpecificMemberAccess(node, null, arrayMethodWithThisArgPattern); } /** @@ -541,7 +481,7 @@ function isMethodWhichHasThisArg(node) { * @returns {Function} Negated function. */ function negate(f) { - return token => !f(token); + return token => !f(token); } /** @@ -551,19 +491,17 @@ function negate(f) { * @returns {boolean} Whether or not the node has a `@this` tag in its comments. */ function hasJSDocThisTag(node, sourceCode) { - const jsdocComment = sourceCode.getJSDocComment(node); - - if (jsdocComment && thisTagPattern.test(jsdocComment.value)) { - return true; - } - - // Checks `@this` in its leading comments for callbacks, - // because callbacks don't have its JSDoc comment. - // e.g. - // sinon.test(/* @this sinon.Sandbox */function() { this.spy(); }); - return sourceCode - .getCommentsBefore(node) - .some(comment => thisTagPattern.test(comment.value)); + const jsdocComment = sourceCode.getJSDocComment(node); + + if (jsdocComment && thisTagPattern.test(jsdocComment.value)) { + return true; + } + + // Checks `@this` in its leading comments for callbacks, + // because callbacks don't have its JSDoc comment. + // e.g. + // sinon.test(/* @this sinon.Sandbox */function() { this.spy(); }); + return sourceCode.getCommentsBefore(node).some(comment => thisTagPattern.test(comment.value)); } /** @@ -574,16 +512,12 @@ function hasJSDocThisTag(node, sourceCode) { * @private */ function isParenthesised(sourceCode, node) { - const previousToken = sourceCode.getTokenBefore(node), - nextToken = sourceCode.getTokenAfter(node); - - return ( - Boolean(previousToken && nextToken) && - previousToken.value === "(" && - previousToken.range[1] <= node.range[0] && - nextToken.value === ")" && - nextToken.range[0] >= node.range[1] - ); + const previousToken = sourceCode.getTokenBefore(node), + nextToken = sourceCode.getTokenAfter(node); + + return Boolean(previousToken && nextToken) && + previousToken.value === "(" && previousToken.range[1] <= node.range[0] && + nextToken.value === ")" && nextToken.range[0] >= node.range[1]; } /** @@ -592,7 +526,7 @@ function isParenthesised(sourceCode, node) { * @returns {boolean} `true` if the token is a `=` token. */ function isEqToken(token) { - return token.value === "=" && token.type === "Punctuator"; + return token.value === "=" && token.type === "Punctuator"; } /** @@ -601,7 +535,7 @@ function isEqToken(token) { * @returns {boolean} `true` if the token is an arrow token. */ function isArrowToken(token) { - return token.value === "=>" && token.type === "Punctuator"; + return token.value === "=>" && token.type === "Punctuator"; } /** @@ -610,7 +544,7 @@ function isArrowToken(token) { * @returns {boolean} `true` if the token is a comma token. */ function isCommaToken(token) { - return token.value === "," && token.type === "Punctuator"; + return token.value === "," && token.type === "Punctuator"; } /** @@ -619,7 +553,7 @@ function isCommaToken(token) { * @returns {boolean} `true` if the token is a dot token. */ function isDotToken(token) { - return token.value === "." && token.type === "Punctuator"; + return token.value === "." && token.type === "Punctuator"; } /** @@ -628,7 +562,7 @@ function isDotToken(token) { * @returns {boolean} `true` if the token is a `?.` token. */ function isQuestionDotToken(token) { - return token.value === "?." && token.type === "Punctuator"; + return token.value === "?." && token.type === "Punctuator"; } /** @@ -637,7 +571,7 @@ function isQuestionDotToken(token) { * @returns {boolean} `true` if the token is a semicolon token. */ function isSemicolonToken(token) { - return token.value === ";" && token.type === "Punctuator"; + return token.value === ";" && token.type === "Punctuator"; } /** @@ -646,7 +580,7 @@ function isSemicolonToken(token) { * @returns {boolean} `true` if the token is a colon token. */ function isColonToken(token) { - return token.value === ":" && token.type === "Punctuator"; + return token.value === ":" && token.type === "Punctuator"; } /** @@ -655,7 +589,7 @@ function isColonToken(token) { * @returns {boolean} `true` if the token is an opening parenthesis token. */ function isOpeningParenToken(token) { - return token.value === "(" && token.type === "Punctuator"; + return token.value === "(" && token.type === "Punctuator"; } /** @@ -664,7 +598,7 @@ function isOpeningParenToken(token) { * @returns {boolean} `true` if the token is a closing parenthesis token. */ function isClosingParenToken(token) { - return token.value === ")" && token.type === "Punctuator"; + return token.value === ")" && token.type === "Punctuator"; } /** @@ -673,7 +607,7 @@ function isClosingParenToken(token) { * @returns {boolean} `true` if the token is an opening square bracket token. */ function isOpeningBracketToken(token) { - return token.value === "[" && token.type === "Punctuator"; + return token.value === "[" && token.type === "Punctuator"; } /** @@ -682,7 +616,7 @@ function isOpeningBracketToken(token) { * @returns {boolean} `true` if the token is a closing square bracket token. */ function isClosingBracketToken(token) { - return token.value === "]" && token.type === "Punctuator"; + return token.value === "]" && token.type === "Punctuator"; } /** @@ -691,7 +625,7 @@ function isClosingBracketToken(token) { * @returns {boolean} `true` if the token is an opening brace token. */ function isOpeningBraceToken(token) { - return token.value === "{" && token.type === "Punctuator"; + return token.value === "{" && token.type === "Punctuator"; } /** @@ -700,7 +634,7 @@ function isOpeningBraceToken(token) { * @returns {boolean} `true` if the token is a closing brace token. */ function isClosingBraceToken(token) { - return token.value === "}" && token.type === "Punctuator"; + return token.value === "}" && token.type === "Punctuator"; } /** @@ -709,11 +643,7 @@ function isClosingBraceToken(token) { * @returns {boolean} `true` if the token is a comment token. */ function isCommentToken(token) { - return ( - token.type === "Line" || - token.type === "Block" || - token.type === "Shebang" - ); + return token.type === "Line" || token.type === "Block" || token.type === "Shebang"; } /** @@ -722,7 +652,7 @@ function isCommentToken(token) { * @returns {boolean} `true` if the token is a keyword token. */ function isKeywordToken(token) { - return token.type === "Keyword"; + return token.type === "Keyword"; } /** @@ -732,20 +662,19 @@ function isKeywordToken(token) { * @returns {Token} `(` token. */ function getOpeningParenOfParams(node, sourceCode) { - // If the node is an arrow function and doesn't have parens, this returns the identifier of the first param. - if (node.type === "ArrowFunctionExpression" && node.params.length === 1) { - const argToken = sourceCode.getFirstToken(node.params[0]); - const maybeParenToken = sourceCode.getTokenBefore(argToken); - - return isOpeningParenToken(maybeParenToken) - ? maybeParenToken - : argToken; - } - - // Otherwise, returns paren. - return node.id - ? sourceCode.getTokenAfter(node.id, isOpeningParenToken) - : sourceCode.getFirstToken(node, isOpeningParenToken); + + // If the node is an arrow function and doesn't have parens, this returns the identifier of the first param. + if (node.type === "ArrowFunctionExpression" && node.params.length === 1) { + const argToken = sourceCode.getFirstToken(node.params[0]); + const maybeParenToken = sourceCode.getTokenBefore(argToken); + + return isOpeningParenToken(maybeParenToken) ? maybeParenToken : argToken; + } + + // Otherwise, returns paren. + return node.id + ? sourceCode.getTokenAfter(node.id, isOpeningParenToken) + : sourceCode.getFirstToken(node, isOpeningParenToken); } /** @@ -756,22 +685,21 @@ function getOpeningParenOfParams(node, sourceCode) { * @returns {boolean} the source code for the given node. */ function equalTokens(left, right, sourceCode) { - const tokensL = sourceCode.getTokens(left); - const tokensR = sourceCode.getTokens(right); - - if (tokensL.length !== tokensR.length) { - return false; - } - for (let i = 0; i < tokensL.length; ++i) { - if ( - tokensL[i].type !== tokensR[i].type || - tokensL[i].value !== tokensR[i].value - ) { - return false; - } - } - - return true; + const tokensL = sourceCode.getTokens(left); + const tokensR = sourceCode.getTokens(right); + + if (tokensL.length !== tokensR.length) { + return false; + } + for (let i = 0; i < tokensL.length; ++i) { + if (tokensL[i].type !== tokensR[i].type || + tokensL[i].value !== tokensR[i].value + ) { + return false; + } + } + + return true; } /** @@ -787,10 +715,10 @@ function equalTokens(left, right, sourceCode) { * @see https://tc39.es/ecma262/#prod-ShortCircuitExpression */ function isLogicalExpression(node) { - return ( - node.type === "LogicalExpression" && - (node.operator === "&&" || node.operator === "||") - ); + return ( + node.type === "LogicalExpression" && + (node.operator === "&&" || node.operator === "||") + ); } /** @@ -805,7 +733,7 @@ function isLogicalExpression(node) { * @returns {boolean} `true` if the node is `??`. */ function isCoalesceExpression(node) { - return node.type === "LogicalExpression" && node.operator === "??"; + return node.type === "LogicalExpression" && node.operator === "??"; } /** @@ -815,10 +743,10 @@ function isCoalesceExpression(node) { * @returns {boolean} `true` if the two nodes are the pair of a logical expression and a coalesce expression. */ function isMixedLogicalAndCoalesceExpressions(left, right) { - return ( - (isLogicalExpression(left) && isCoalesceExpression(right)) || - (isCoalesceExpression(left) && isLogicalExpression(right)) - ); + return ( + (isLogicalExpression(left) && isCoalesceExpression(right)) || + (isCoalesceExpression(left) && isLogicalExpression(right)) + ); } /** @@ -827,7 +755,7 @@ function isMixedLogicalAndCoalesceExpressions(left, right) { * @returns {boolean} `true` if the operator is a logical assignment operator. */ function isLogicalAssignmentOperator(operator) { - return LOGICAL_ASSIGNMENT_OPERATORS.has(operator); + return LOGICAL_ASSIGNMENT_OPERATORS.has(operator); } /** @@ -837,10 +765,10 @@ function isLogicalAssignmentOperator(operator) { * @returns {Token} The colon token of the node. */ function getSwitchCaseColonToken(node, sourceCode) { - if (node.test) { - return sourceCode.getTokenAfter(node.test, isColonToken); - } - return sourceCode.getFirstToken(node, 1); + if (node.test) { + return sourceCode.getTokenAfter(node.test, isColonToken); + } + return sourceCode.getFirstToken(node, 1); } /** @@ -854,12 +782,12 @@ function getSwitchCaseColonToken(node, sourceCode) { * @returns {string} The module export name. */ function getModuleExportName(node) { - if (node.type === "Identifier") { - return node.name; - } + if (node.type === "Identifier") { + return node.name; + } - // string literal - return node.value; + // string literal + return node.value; } /** @@ -869,26 +797,27 @@ function getModuleExportName(node) { * `null` when it cannot be determined. */ function getBooleanValue(node) { - if (node.value === null) { - /* - * it might be a null literal or bigint/regex literal in unsupported environments . - * https://github.com/estree/estree/blob/14df8a024956ea289bd55b9c2226a1d5b8a473ee/es5.md#regexpliteral - * https://github.com/estree/estree/blob/14df8a024956ea289bd55b9c2226a1d5b8a473ee/es2020.md#bigintliteral - */ - - if (node.raw === "null") { - return false; - } - - // regex is always truthy - if (typeof node.regex === "object") { - return true; - } - - return null; - } - - return !!node.value; + if (node.value === null) { + + /* + * it might be a null literal or bigint/regex literal in unsupported environments . + * https://github.com/estree/estree/blob/14df8a024956ea289bd55b9c2226a1d5b8a473ee/es5.md#regexpliteral + * https://github.com/estree/estree/blob/14df8a024956ea289bd55b9c2226a1d5b8a473ee/es2020.md#bigintliteral + */ + + if (node.raw === "null") { + return false; + } + + // regex is always truthy + if (typeof node.regex === "object") { + return true; + } + + return null; + } + + return !!node.value; } /** @@ -898,37 +827,34 @@ function getBooleanValue(node) { * @returns {boolean} true when condition short circuits whole condition */ function isLogicalIdentity(node, operator) { - switch (node.type) { - case "Literal": - return ( - (operator === "||" && getBooleanValue(node) === true) || - (operator === "&&" && getBooleanValue(node) === false) - ); - - case "UnaryExpression": - return operator === "&&" && node.operator === "void"; - - case "LogicalExpression": - /* - * handles `a && false || b` - * `false` is an identity element of `&&` but not `||` - */ - return ( - operator === node.operator && - (isLogicalIdentity(node.left, operator) || - isLogicalIdentity(node.right, operator)) - ); - - case "AssignmentExpression": - return ( - ["||=", "&&="].includes(node.operator) && - operator === node.operator.slice(0, -1) && - isLogicalIdentity(node.right, operator) - ); - - // no default - } - return false; + switch (node.type) { + case "Literal": + return (operator === "||" && getBooleanValue(node) === true) || + (operator === "&&" && getBooleanValue(node) === false); + + case "UnaryExpression": + return (operator === "&&" && node.operator === "void"); + + case "LogicalExpression": + + /* + * handles `a && false || b` + * `false` is an identity element of `&&` but not `||` + */ + return operator === node.operator && + ( + isLogicalIdentity(node.left, operator) || + isLogicalIdentity(node.right, operator) + ); + + case "AssignmentExpression": + return ["||=", "&&="].includes(node.operator) && + operator === node.operator.slice(0, -1) && + isLogicalIdentity(node.right, operator); + + // no default + } + return false; } /** @@ -938,16 +864,17 @@ function isLogicalIdentity(node, operator) { * @returns {boolean} `true` if the identifier is a reference to a global variable. */ function isReferenceToGlobalVariable(scope, node) { - const reference = scope.references.find(ref => ref.identifier === node); - - return Boolean( - reference && - reference.resolved && - reference.resolved.scope.type === "global" && - reference.resolved.defs.length === 0, - ); + const reference = scope.references.find(ref => ref.identifier === node); + + return Boolean( + reference && + reference.resolved && + reference.resolved.scope.type === "global" && + reference.resolved.defs.length === 0 + ); } + /** * Checks if a node has a constant truthiness value. * @param {Scope} scope Scope in which the node appears. @@ -960,161 +887,123 @@ function isReferenceToGlobalVariable(scope, node) { * @private */ function isConstant(scope, node, inBooleanPosition) { - // node.elements can return null values in the case of sparse arrays ex. [,] - if (!node) { - return true; - } - switch (node.type) { - case "Literal": - case "ArrowFunctionExpression": - case "FunctionExpression": - return true; - case "ClassExpression": - case "ObjectExpression": - /** - * In theory objects like: - * - * `{toString: () => a}` - * `{valueOf: () => a}` - * - * Or a classes like: - * - * `class { static toString() { return a } }` - * `class { static valueOf() { return a } }` - * - * Are not constant verifiably when `inBooleanPosition` is - * false, but it's an edge case we've opted not to handle. - */ - return true; - case "TemplateLiteral": - return ( - (inBooleanPosition && - node.quasis.some(quasi => quasi.value.cooked.length)) || - node.expressions.every(exp => isConstant(scope, exp, false)) - ); - - case "ArrayExpression": { - if (!inBooleanPosition) { - return node.elements.every(element => - isConstant(scope, element, false), - ); - } - return true; - } - - case "UnaryExpression": - if ( - node.operator === "void" || - (node.operator === "typeof" && inBooleanPosition) - ) { - return true; - } - - if (node.operator === "!") { - return isConstant(scope, node.argument, true); - } - - return isConstant(scope, node.argument, false); - - case "BinaryExpression": - return ( - isConstant(scope, node.left, false) && - isConstant(scope, node.right, false) && - node.operator !== "in" - ); - - case "LogicalExpression": { - const isLeftConstant = isConstant( - scope, - node.left, - inBooleanPosition, - ); - const isRightConstant = isConstant( - scope, - node.right, - inBooleanPosition, - ); - const isLeftShortCircuit = - isLeftConstant && isLogicalIdentity(node.left, node.operator); - const isRightShortCircuit = - inBooleanPosition && - isRightConstant && - isLogicalIdentity(node.right, node.operator); - - return ( - (isLeftConstant && isRightConstant) || - isLeftShortCircuit || - isRightShortCircuit - ); - } - case "NewExpression": - return inBooleanPosition; - case "AssignmentExpression": - if (node.operator === "=") { - return isConstant(scope, node.right, inBooleanPosition); - } - - if (["||=", "&&="].includes(node.operator) && inBooleanPosition) { - return isLogicalIdentity( - node.right, - node.operator.slice(0, -1), - ); - } - - return false; - - case "SequenceExpression": - return isConstant( - scope, - node.expressions.at(-1), - inBooleanPosition, - ); - case "SpreadElement": - return isConstant(scope, node.argument, inBooleanPosition); - case "CallExpression": - if ( - node.callee.type === "Identifier" && - node.callee.name === "Boolean" - ) { - if ( - node.arguments.length === 0 || - isConstant(scope, node.arguments[0], true) - ) { - return isReferenceToGlobalVariable(scope, node.callee); - } - } - return false; - case "Identifier": - return ( - node.name === "undefined" && - isReferenceToGlobalVariable(scope, node) - ); - - // no default - } - return false; + + // node.elements can return null values in the case of sparse arrays ex. [,] + if (!node) { + return true; + } + switch (node.type) { + case "Literal": + case "ArrowFunctionExpression": + case "FunctionExpression": + return true; + case "ClassExpression": + case "ObjectExpression": + + /** + * In theory objects like: + * + * `{toString: () => a}` + * `{valueOf: () => a}` + * + * Or a classes like: + * + * `class { static toString() { return a } }` + * `class { static valueOf() { return a } }` + * + * Are not constant verifiably when `inBooleanPosition` is + * false, but it's an edge case we've opted not to handle. + */ + return true; + case "TemplateLiteral": + return (inBooleanPosition && node.quasis.some(quasi => quasi.value.cooked.length)) || + node.expressions.every(exp => isConstant(scope, exp, false)); + + case "ArrayExpression": { + if (!inBooleanPosition) { + return node.elements.every(element => isConstant(scope, element, false)); + } + return true; + } + + case "UnaryExpression": + if ( + node.operator === "void" || + node.operator === "typeof" && inBooleanPosition + ) { + return true; + } + + if (node.operator === "!") { + return isConstant(scope, node.argument, true); + } + + return isConstant(scope, node.argument, false); + + case "BinaryExpression": + return isConstant(scope, node.left, false) && + isConstant(scope, node.right, false) && + node.operator !== "in"; + + case "LogicalExpression": { + const isLeftConstant = isConstant(scope, node.left, inBooleanPosition); + const isRightConstant = isConstant(scope, node.right, inBooleanPosition); + const isLeftShortCircuit = (isLeftConstant && isLogicalIdentity(node.left, node.operator)); + const isRightShortCircuit = (inBooleanPosition && isRightConstant && isLogicalIdentity(node.right, node.operator)); + + return (isLeftConstant && isRightConstant) || + isLeftShortCircuit || + isRightShortCircuit; + } + case "NewExpression": + return inBooleanPosition; + case "AssignmentExpression": + if (node.operator === "=") { + return isConstant(scope, node.right, inBooleanPosition); + } + + if (["||=", "&&="].includes(node.operator) && inBooleanPosition) { + return isLogicalIdentity(node.right, node.operator.slice(0, -1)); + } + + return false; + + case "SequenceExpression": + return isConstant(scope, node.expressions[node.expressions.length - 1], inBooleanPosition); + case "SpreadElement": + return isConstant(scope, node.argument, inBooleanPosition); + case "CallExpression": + if (node.callee.type === "Identifier" && node.callee.name === "Boolean") { + if (node.arguments.length === 0 || isConstant(scope, node.arguments[0], true)) { + return isReferenceToGlobalVariable(scope, node.callee); + } + } + return false; + case "Identifier": + return node.name === "undefined" && isReferenceToGlobalVariable(scope, node); + + // no default + } + return false; } /** - * Checks whether a node is an ExpressionStatement at the top level of a file, function body, or TypeScript module block. + * Checks whether a node is an ExpressionStatement at the top level of a file or function body. * A top-level ExpressionStatement node is a directive if it contains a single unparenthesized * string literal and if it occurs either as the first sibling or immediately after another * directive. * @param {ASTNode} node The node to check. * @returns {boolean} Whether or not the node is an ExpressionStatement at the top level of a - * file, function body, or TypeScript module block. + * file or function body. */ function isTopLevelExpressionStatement(node) { - if (node.type !== "ExpressionStatement") { - return false; - } - const parent = node.parent; - - return ( - parent.type === "Program" || - parent.type === "TSModuleBlock" || - (parent.type === "BlockStatement" && isFunction(parent.parent)) - ); + if (node.type !== "ExpressionStatement") { + return false; + } + const parent = node.parent; + + return parent.type === "Program" || (parent.type === "BlockStatement" && isFunction(parent.parent)); + } /** @@ -1123,10 +1012,7 @@ function isTopLevelExpressionStatement(node) { * @returns {boolean} `true` if the node is a part of directive prologue. */ function isDirective(node) { - return ( - node.type === "ExpressionStatement" && - typeof node.directive === "string" - ); + return node.type === "ExpressionStatement" && typeof node.directive === "string"; } /** @@ -1135,205 +1021,106 @@ function isDirective(node) { * @returns {boolean} Whether the node appears at the beginning of an ancestor ExpressionStatement node. */ function isStartOfExpressionStatement(node) { - const start = node.range[0]; - let ancestor = node; - - while ((ancestor = ancestor.parent) && ancestor.range[0] === start) { - if (ancestor.type === "ExpressionStatement") { - return true; - } - } - return false; + const start = node.range[0]; + let ancestor = node; + + while ((ancestor = ancestor.parent) && ancestor.range[0] === start) { + if (ancestor.type === "ExpressionStatement") { + return true; + } + } + return false; } /** * Determines whether an opening parenthesis `(`, bracket `[` or backtick ``` ` ``` needs to be preceded by a semicolon. - * This opening parenthesis or bracket should be at the start of an `ExpressionStatement`, a `MethodDefinition` or at - * the start of the body of an `ArrowFunctionExpression`. + * This opening parenthesis or bracket should be at the start of an `ExpressionStatement` or at the start of the body of an `ArrowFunctionExpression`. * @type {(sourceCode: SourceCode, node: ASTNode) => boolean} * @param {SourceCode} sourceCode The source code object. * @param {ASTNode} node A node at the position where an opening parenthesis or bracket will be inserted. - * @returns {boolean} Whether a semicolon is required before the opening parenthesis or bracket. + * @returns {boolean} Whether a semicolon is required before the opening parenthesis or braket. */ let needsPrecedingSemicolon; { - const BREAK_OR_CONTINUE = new Set(["BreakStatement", "ContinueStatement"]); - - // Declaration types that cannot be continued by a punctuator when ending with a string Literal that is a direct child. - const DECLARATIONS = new Set([ - "ExportAllDeclaration", - "ExportNamedDeclaration", - "ImportDeclaration", - ]); - - const IDENTIFIER_OR_KEYWORD = new Set(["Identifier", "Keyword"]); - - // Keywords that can immediately precede an ExpressionStatement node, mapped to the their node types. - const NODE_TYPES_BY_KEYWORD = { - __proto__: null, - break: "BreakStatement", - continue: "ContinueStatement", - debugger: "DebuggerStatement", - do: "DoWhileStatement", - else: "IfStatement", - return: "ReturnStatement", - yield: "YieldExpression", - }; - - /* - * Before an opening parenthesis, postfix `++` and `--` always trigger ASI; - * the tokens `:`, `;`, `{` and `=>` don't expect a semicolon, as that would count as an empty statement. - */ - const PUNCTUATORS = new Set([":", ";", "{", "=>", "++", "--"]); - - /* - * Statements that can contain an `ExpressionStatement` after a closing parenthesis. - * DoWhileStatement is an exception in that it always triggers ASI after the closing parenthesis. - */ - const STATEMENTS = new Set([ - "DoWhileStatement", - "ForInStatement", - "ForOfStatement", - "ForStatement", - "IfStatement", - "WhileStatement", - "WithStatement", - ]); - - const TS_TYPE_NODE_TYPES = new Set([ - "TSAsExpression", - "TSSatisfiesExpression", - "TSTypeAliasDeclaration", - "TSTypeAnnotation", - ]); - - /** - * Determines whether a specified node is inside a TypeScript type context. - * @param {ASTNode} node The node to check. - * @returns {boolean} Whether the node is inside a TypeScript type context. - */ - function isInType(node) { - for (let currNode = node; ; ) { - const { parent } = currNode; - if (!parent) { - break; - } - if ( - TS_TYPE_NODE_TYPES.has(parent.type) && - currNode === parent.typeAnnotation - ) { - return true; - } - currNode = parent; - } - return false; - } - - needsPrecedingSemicolon = function (sourceCode, node) { - const prevToken = sourceCode.getTokenBefore(node); - - if ( - !prevToken || - (prevToken.type === "Punctuator" && - PUNCTUATORS.has(prevToken.value)) - ) { - return false; - } - - const prevNode = sourceCode.getNodeByRangeIndex(prevToken.range[0]); - - if ( - prevNode.type === "TSDeclareFunction" || - prevNode.parent.type === "TSImportEqualsDeclaration" || - prevNode.parent.parent?.type === "TSImportEqualsDeclaration" || - TS_TYPE_NODE_TYPES.has(prevNode.type) || - isInType(prevNode) - ) { - return false; - } - - if (isClosingParenToken(prevToken)) { - return !STATEMENTS.has(prevNode.type); - } - - if (isClosingBraceToken(prevToken)) { - return ( - (prevNode.type === "BlockStatement" && - prevNode.parent.type === "FunctionExpression" && - prevNode.parent.parent.type !== "MethodDefinition") || - (prevNode.type === "ClassBody" && - prevNode.parent.type === "ClassExpression") || - prevNode.type === "ObjectExpression" - ); - } - - if (IDENTIFIER_OR_KEYWORD.has(prevToken.type)) { - if ( - prevNode.parent.type === "VariableDeclarator" && - !prevNode.parent.init - ) { - return false; - } - if (BREAK_OR_CONTINUE.has(prevNode.parent.type)) { - return false; - } - - const keyword = prevToken.value; - const nodeType = NODE_TYPES_BY_KEYWORD[keyword]; - - return prevNode.type !== nodeType; - } - - if (prevToken.type === "String") { - return !DECLARATIONS.has(prevNode.parent.type); - } - - return true; - }; -} - -/** - * Checks if a node is used as an import attribute key, either in a static or dynamic import. - * @param {ASTNode} node The node to check. - * @returns {boolean} Whether the node is used as an import attribute key. - */ -function isImportAttributeKey(node) { - const { parent } = node; - - // static import/re-export - if (parent.type === "ImportAttribute" && parent.key === node) { - return true; - } - - // dynamic import - if ( - parent.type === "Property" && - !parent.computed && - (parent.key === node || - (parent.value === node && parent.shorthand && !parent.method)) && - parent.parent.type === "ObjectExpression" - ) { - const objectExpression = parent.parent; - const objectExpressionParent = objectExpression.parent; - - if ( - objectExpressionParent.type === "ImportExpression" && - objectExpressionParent.options === objectExpression - ) { - return true; - } - - // nested key - if ( - objectExpressionParent.type === "Property" && - objectExpressionParent.value === objectExpression - ) { - return isImportAttributeKey(objectExpressionParent.key); - } - } - - return false; + const BREAK_OR_CONTINUE = new Set(["BreakStatement", "ContinueStatement"]); + + // Declaration types that must contain a string Literal node at the end. + const DECLARATIONS = new Set(["ExportAllDeclaration", "ExportNamedDeclaration", "ImportDeclaration"]); + + const IDENTIFIER_OR_KEYWORD = new Set(["Identifier", "Keyword"]); + + // Keywords that can immediately precede an ExpressionStatement node, mapped to the their node types. + const NODE_TYPES_BY_KEYWORD = { + __proto__: null, + break: "BreakStatement", + continue: "ContinueStatement", + debugger: "DebuggerStatement", + do: "DoWhileStatement", + else: "IfStatement", + return: "ReturnStatement", + yield: "YieldExpression" + }; + + /* + * Before an opening parenthesis, postfix `++` and `--` always trigger ASI; + * the tokens `:`, `;`, `{` and `=>` don't expect a semicolon, as that would count as an empty statement. + */ + const PUNCTUATORS = new Set([":", ";", "{", "=>", "++", "--"]); + + /* + * Statements that can contain an `ExpressionStatement` after a closing parenthesis. + * DoWhileStatement is an exception in that it always triggers ASI after the closing parenthesis. + */ + const STATEMENTS = new Set([ + "DoWhileStatement", + "ForInStatement", + "ForOfStatement", + "ForStatement", + "IfStatement", + "WhileStatement", + "WithStatement" + ]); + + needsPrecedingSemicolon = + function(sourceCode, node) { + const prevToken = sourceCode.getTokenBefore(node); + + if (!prevToken || prevToken.type === "Punctuator" && PUNCTUATORS.has(prevToken.value)) { + return false; + } + + const prevNode = sourceCode.getNodeByRangeIndex(prevToken.range[0]); + + if (isClosingParenToken(prevToken)) { + return !STATEMENTS.has(prevNode.type); + } + + if (isClosingBraceToken(prevToken)) { + return ( + prevNode.type === "BlockStatement" && prevNode.parent.type === "FunctionExpression" || + prevNode.type === "ClassBody" && prevNode.parent.type === "ClassExpression" || + prevNode.type === "ObjectExpression" + ); + } + + if (IDENTIFIER_OR_KEYWORD.has(prevToken.type)) { + if (BREAK_OR_CONTINUE.has(prevNode.parent.type)) { + return false; + } + + const keyword = prevToken.value; + const nodeType = NODE_TYPES_BY_KEYWORD[keyword]; + + return prevNode.type !== nodeType; + } + + if (prevToken.type === "String") { + return !DECLARATIONS.has(prevNode.parent.type); + } + + return true; + }; } //------------------------------------------------------------------------------ @@ -1341,1393 +1128,1155 @@ function isImportAttributeKey(node) { //------------------------------------------------------------------------------ module.exports = { - COMMENTS_IGNORE_PATTERN, - LINEBREAKS, - LINEBREAK_MATCHER: lineBreakPattern, - SHEBANG_MATCHER: shebangPattern, - STATEMENT_LIST_PARENTS, - ECMASCRIPT_GLOBALS, - - /** - * Determines whether two adjacent tokens are on the same line. - * @param {Object} left The left token object. - * @param {Object} right The right token object. - * @returns {boolean} Whether or not the tokens are on the same line. - * @public - */ - isTokenOnSameLine(left, right) { - return left.loc.end.line === right.loc.start.line; - }, - - isNullOrUndefined, - isCallee, - isES5Constructor, - getUpperFunction, - isFunction, - isLoop, - isInLoop, - isArrayFromMethod, - isParenthesised, - createGlobalLinebreakMatcher, - equalTokens, - - isArrowToken, - isClosingBraceToken, - isClosingBracketToken, - isClosingParenToken, - isColonToken, - isCommaToken, - isCommentToken, - isDotToken, - isQuestionDotToken, - isKeywordToken, - isNotClosingBraceToken: negate(isClosingBraceToken), - isNotClosingBracketToken: negate(isClosingBracketToken), - isNotClosingParenToken: negate(isClosingParenToken), - isNotColonToken: negate(isColonToken), - isNotCommaToken: negate(isCommaToken), - isNotDotToken: negate(isDotToken), - isNotQuestionDotToken: negate(isQuestionDotToken), - isNotOpeningBraceToken: negate(isOpeningBraceToken), - isNotOpeningBracketToken: negate(isOpeningBracketToken), - isNotOpeningParenToken: negate(isOpeningParenToken), - isNotSemicolonToken: negate(isSemicolonToken), - isOpeningBraceToken, - isOpeningBracketToken, - isOpeningParenToken, - isSemicolonToken, - isEqToken, - - /** - * Checks whether or not a given node is a string literal. - * @param {ASTNode} node A node to check. - * @returns {boolean} `true` if the node is a string literal. - */ - isStringLiteral(node) { - return ( - (node.type === "Literal" && typeof node.value === "string") || - node.type === "TemplateLiteral" - ); - }, - - /** - * Checks whether a given node is a breakable statement or not. - * The node is breakable if the node is one of the following type: - * - * - DoWhileStatement - * - ForInStatement - * - ForOfStatement - * - ForStatement - * - SwitchStatement - * - WhileStatement - * @param {ASTNode} node A node to check. - * @returns {boolean} `true` if the node is breakable. - */ - isBreakableStatement(node) { - return breakableTypePattern.test(node.type); - }, - - /** - * Gets references which are non initializer and writable. - * @param {Reference[]} references An array of references. - * @returns {Reference[]} An array of only references which are non initializer and writable. - * @public - */ - getModifyingReferences(references) { - return references.filter(isModifyingReference); - }, - - /** - * Validate that a string passed in is surrounded by the specified character - * @param {string} val The text to check. - * @param {string} character The character to see if it's surrounded by. - * @returns {boolean} True if the text is surrounded by the character, false if not. - * @private - */ - isSurroundedBy(val, character) { - return val[0] === character && val.at(-1) === character; - }, - - /** - * Returns whether the provided node is an ESLint directive comment or not - * @param {Line|Block} node The comment token to be checked - * @returns {boolean} `true` if the node is an ESLint directive comment - */ - isDirectiveComment(node) { - const comment = node.value.trim(); - - return ( - (node.type === "Line" && comment.startsWith("eslint-")) || - (node.type === "Block" && ESLINT_DIRECTIVE_PATTERN.test(comment)) - ); - }, - - /** - * Gets the trailing statement of a given node. - * - * if (code) - * consequent; - * - * When taking this `IfStatement`, returns `consequent;` statement. - * @param {ASTNode} A node to get. - * @returns {ASTNode|null} The trailing statement's node. - */ - getTrailingStatement: esutils.ast.trailingStatement, - - /** - * Finds the variable by a given name in a given scope and its upper scopes. - * @param {eslint-scope.Scope} initScope A scope to start find. - * @param {string} name A variable name to find. - * @returns {eslint-scope.Variable|null} A found variable or `null`. - */ - getVariableByName(initScope, name) { - let scope = initScope; - - while (scope) { - const variable = scope.set.get(name); - - if (variable) { - return variable; - } - - scope = scope.upper; - } - - return null; - }, - - /** - * Checks whether or not a given function node is the default `this` binding. - * - * First, this checks the node: - * - * - The given node is not in `PropertyDefinition#value` position. - * - The given node is not `StaticBlock`. - * - The function name does not start with uppercase. It's a convention to capitalize the names - * of constructor functions. This check is not performed if `capIsConstructor` is set to `false`. - * - The function does not have a JSDoc comment that has a @this tag. - * - * Next, this checks the location of the node. - * If the location is below, this judges `this` is valid. - * - * - The location is not on an object literal. - * - The location is not assigned to a variable which starts with an uppercase letter. Applies to anonymous - * functions only, as the name of the variable is considered to be the name of the function in this case. - * This check is not performed if `capIsConstructor` is set to `false`. - * - The location is not on an ES2015 class. - * - Its `bind`/`call`/`apply` method is not called directly. - * - The function is not a callback of array methods (such as `.forEach()`) if `thisArg` is given. - * @param {ASTNode} node A function node to check. It also can be an implicit function, like `StaticBlock` - * or any expression that is `PropertyDefinition#value` node. - * @param {SourceCode} sourceCode A SourceCode instance to get comments. - * @param {boolean} [capIsConstructor = true] `false` disables the assumption that functions which name starts - * with an uppercase or are assigned to a variable which name starts with an uppercase are constructors. - * @returns {boolean} The function node is the default `this` binding. - */ - isDefaultThisBinding(node, sourceCode, { capIsConstructor = true } = {}) { - /* - * Class field initializers are implicit functions, but ESTree doesn't have the AST node of field initializers. - * Therefore, A expression node at `PropertyDefinition#value` is a function. - * In this case, `this` is always not default binding. - */ - if ( - node.parent.type === "PropertyDefinition" && - node.parent.value === node - ) { - return false; - } - - // Class static blocks are implicit functions. In this case, `this` is always not default binding. - if (node.type === "StaticBlock") { - return false; - } - - // Check if the function has a parameter named `this`. - if ( - (node.type === "FunctionDeclaration" || - node.type === "FunctionExpression") && - node.params.some( - param => param.type === "Identifier" && param.name === "this", - ) - ) { - return false; - } - - if ( - (capIsConstructor && isES5Constructor(node)) || - hasJSDocThisTag(node, sourceCode) - ) { - return false; - } - const isAnonymous = node.id === null; - let currentNode = node; - - while (currentNode) { - const parent = currentNode.parent; - - switch (parent.type) { - /* - * Looks up the destination. - * e.g., obj.foo = nativeFoo || function foo() { ... }; - */ - case "LogicalExpression": - case "ConditionalExpression": - case "ChainExpression": - currentNode = parent; - break; - - /* - * If the upper function is IIFE, checks the destination of the return value. - * e.g. - * obj.foo = (function() { - * // setup... - * return function foo() { ... }; - * })(); - * obj.foo = (() => - * function foo() { ... } - * )(); - */ - case "ReturnStatement": { - const func = getUpperFunction(parent); - - if (func === null || !isCallee(func)) { - return true; - } - currentNode = func.parent; - break; - } - case "ArrowFunctionExpression": - if (currentNode !== parent.body || !isCallee(parent)) { - return true; - } - currentNode = parent.parent; - break; - - /* - * e.g. - * var obj = { foo() { ... } }; - * var obj = { foo: function() { ... } }; - * class A { constructor() { ... } } - * class A { foo() { ... } } - * class A { get foo() { ... } } - * class A { set foo() { ... } } - * class A { static foo() { ... } } - * class A { foo = function() { ... } } - */ - case "Property": - case "PropertyDefinition": - case "MethodDefinition": - return parent.value !== currentNode; - - /* - * e.g. - * obj.foo = function foo() { ... }; - * Foo = function() { ... }; - * [obj.foo = function foo() { ... }] = a; - * [Foo = function() { ... }] = a; - */ - case "AssignmentExpression": - case "AssignmentPattern": - if (parent.left.type === "MemberExpression") { - return false; - } - if ( - capIsConstructor && - isAnonymous && - parent.left.type === "Identifier" && - startsWithUpperCase(parent.left.name) - ) { - return false; - } - return true; - - /* - * e.g. - * var Foo = function() { ... }; - */ - case "VariableDeclarator": - return !( - capIsConstructor && - isAnonymous && - parent.init === currentNode && - parent.id.type === "Identifier" && - startsWithUpperCase(parent.id.name) - ); - - /* - * e.g. - * var foo = function foo() { ... }.bind(obj); - * (function foo() { ... }).call(obj); - * (function foo() { ... }).apply(obj, []); - */ - case "MemberExpression": - if ( - parent.object === currentNode && - isSpecificMemberAccess( - parent, - null, - bindOrCallOrApplyPattern, - ) - ) { - const maybeCalleeNode = - parent.parent.type === "ChainExpression" - ? parent.parent - : parent; - - return !( - isCallee(maybeCalleeNode) && - maybeCalleeNode.parent.arguments.length >= 1 && - !isNullOrUndefined( - maybeCalleeNode.parent.arguments[0], - ) - ); - } - return true; - - /* - * e.g. - * Reflect.apply(function() {}, obj, []); - * Array.from([], function() {}, obj); - * list.forEach(function() {}, obj); - */ - case "CallExpression": - if (isReflectApply(parent.callee)) { - return ( - parent.arguments.length !== 3 || - parent.arguments[0] !== currentNode || - isNullOrUndefined(parent.arguments[1]) - ); - } - if (isArrayFromMethod(parent.callee)) { - return ( - parent.arguments.length !== 3 || - parent.arguments[1] !== currentNode || - isNullOrUndefined(parent.arguments[2]) - ); - } - if (isMethodWhichHasThisArg(parent.callee)) { - return ( - parent.arguments.length !== 2 || - parent.arguments[0] !== currentNode || - isNullOrUndefined(parent.arguments[1]) - ); - } - return true; - - // Otherwise `this` is default. - default: - return true; - } - } - - /* c8 ignore next */ - return true; - }, - - /** - * Get the precedence level based on the node type - * @param {ASTNode} node node to evaluate - * @returns {number} precedence level - * @private - */ - getPrecedence(node) { - switch (node.type) { - case "SequenceExpression": - return 0; - - case "AssignmentExpression": - case "ArrowFunctionExpression": - case "YieldExpression": - return 1; - - case "ConditionalExpression": - return 3; - - case "LogicalExpression": - switch (node.operator) { - case "||": - case "??": - return 4; - case "&&": - return 5; - - // no default - } - - /* falls through */ - - case "BinaryExpression": - switch (node.operator) { - case "|": - return 6; - case "^": - return 7; - case "&": - return 8; - case "==": - case "!=": - case "===": - case "!==": - return 9; - case "<": - case "<=": - case ">": - case ">=": - case "in": - case "instanceof": - return 10; - case "<<": - case ">>": - case ">>>": - return 11; - case "+": - case "-": - return 12; - case "*": - case "/": - case "%": - return 13; - case "**": - return 15; - - // no default - } - - /* falls through */ - - case "UnaryExpression": - case "AwaitExpression": - return 16; - - case "UpdateExpression": - return 17; - - case "CallExpression": - case "ChainExpression": - case "ImportExpression": - return 18; - - case "NewExpression": - return 19; - - default: - if (node.type in eslintVisitorKeys) { - return 20; - } - - /* - * if the node is not a standard node that we know about, then assume it has the lowest precedence - * this will mean that rules will wrap unknown nodes in parentheses where applicable instead of - * unwrapping them and potentially changing the meaning of the code or introducing a syntax error. - */ - return -1; - } - }, - - /** - * Checks whether the given node is an empty block node or not. - * @param {ASTNode|null} node The node to check. - * @returns {boolean} `true` if the node is an empty block. - */ - isEmptyBlock(node) { - return Boolean( - node && node.type === "BlockStatement" && node.body.length === 0, - ); - }, - - /** - * Checks whether the given node is an empty function node or not. - * @param {ASTNode|null} node The node to check. - * @returns {boolean} `true` if the node is an empty function. - */ - isEmptyFunction(node) { - return isFunction(node) && module.exports.isEmptyBlock(node.body); - }, - - /** - * Get directives from directive prologue of a Program or Function node. - * @param {ASTNode} node The node to check. - * @returns {ASTNode[]} The directives found in the directive prologue. - */ - getDirectivePrologue(node) { - const directives = []; - - // Directive prologues only occur at the top of files or functions. - if ( - node.type === "Program" || - node.type === "FunctionDeclaration" || - node.type === "FunctionExpression" || - /* - * Do not check arrow functions with implicit return. - * `() => "use strict";` returns the string `"use strict"`. - */ - (node.type === "ArrowFunctionExpression" && - node.body.type === "BlockStatement") - ) { - const statements = - node.type === "Program" ? node.body : node.body.body; - - for (const statement of statements) { - if ( - statement.type === "ExpressionStatement" && - statement.expression.type === "Literal" - ) { - directives.push(statement); - } else { - break; - } - } - } - - return directives; - }, - - /** - * Determines whether this node is a decimal integer literal. If a node is a decimal integer literal, a dot added - * after the node will be parsed as a decimal point, rather than a property-access dot. - * @param {ASTNode} node The node to check. - * @returns {boolean} `true` if this node is a decimal integer. - * @example - * - * 0 // true - * 5 // true - * 50 // true - * 5_000 // true - * 1_234_56 // true - * 08 // true - * 0192 // true - * 5. // false - * .5 // false - * 5.0 // false - * 5.00_00 // false - * 05 // false - * 0x5 // false - * 0b101 // false - * 0b11_01 // false - * 0o5 // false - * 5e0 // false - * 5e1_000 // false - * 5n // false - * 1_000n // false - * "5" // false - * - */ - isDecimalInteger(node) { - return ( - node.type === "Literal" && - typeof node.value === "number" && - DECIMAL_INTEGER_PATTERN.test(node.raw) - ); - }, - - /** - * Determines whether this token is a decimal integer numeric token. - * This is similar to isDecimalInteger(), but for tokens. - * @param {Token} token The token to check. - * @returns {boolean} `true` if this token is a decimal integer. - */ - isDecimalIntegerNumericToken(token) { - return ( - token.type === "Numeric" && - DECIMAL_INTEGER_PATTERN.test(token.value) - ); - }, - - /** - * Gets the name and kind of the given function node. - * - * - `function foo() {}` .................... `function 'foo'` - * - `(function foo() {})` .................. `function 'foo'` - * - `(function() {})` ...................... `function` - * - `function* foo() {}` ................... `generator function 'foo'` - * - `(function* foo() {})` ................. `generator function 'foo'` - * - `(function*() {})` ..................... `generator function` - * - `() => {}` ............................. `arrow function` - * - `async () => {}` ....................... `async arrow function` - * - `({ foo: function foo() {} })` ......... `method 'foo'` - * - `({ foo: function() {} })` ............. `method 'foo'` - * - `({ ['foo']: function() {} })` ......... `method 'foo'` - * - `({ [foo]: function() {} })` ........... `method` - * - `({ foo() {} })` ....................... `method 'foo'` - * - `({ foo: function* foo() {} })` ........ `generator method 'foo'` - * - `({ foo: function*() {} })` ............ `generator method 'foo'` - * - `({ ['foo']: function*() {} })` ........ `generator method 'foo'` - * - `({ [foo]: function*() {} })` .......... `generator method` - * - `({ *foo() {} })` ...................... `generator method 'foo'` - * - `({ foo: async function foo() {} })` ... `async method 'foo'` - * - `({ foo: async function() {} })` ....... `async method 'foo'` - * - `({ ['foo']: async function() {} })` ... `async method 'foo'` - * - `({ [foo]: async function() {} })` ..... `async method` - * - `({ async foo() {} })` ................. `async method 'foo'` - * - `({ get foo() {} })` ................... `getter 'foo'` - * - `({ set foo(a) {} })` .................. `setter 'foo'` - * - `class A { constructor() {} }` ......... `constructor` - * - `class A { foo() {} }` ................. `method 'foo'` - * - `class A { *foo() {} }` ................ `generator method 'foo'` - * - `class A { async foo() {} }` ........... `async method 'foo'` - * - `class A { ['foo']() {} }` ............. `method 'foo'` - * - `class A { *['foo']() {} }` ............ `generator method 'foo'` - * - `class A { async ['foo']() {} }` ....... `async method 'foo'` - * - `class A { [foo]() {} }` ............... `method` - * - `class A { *[foo]() {} }` .............. `generator method` - * - `class A { async [foo]() {} }` ......... `async method` - * - `class A { get foo() {} }` ............. `getter 'foo'` - * - `class A { set foo(a) {} }` ............ `setter 'foo'` - * - `class A { static foo() {} }` .......... `static method 'foo'` - * - `class A { static *foo() {} }` ......... `static generator method 'foo'` - * - `class A { static async foo() {} }` .... `static async method 'foo'` - * - `class A { static get foo() {} }` ...... `static getter 'foo'` - * - `class A { static set foo(a) {} }` ..... `static setter 'foo'` - * - `class A { foo = () => {}; }` .......... `method 'foo'` - * - `class A { foo = function() {}; }` ..... `method 'foo'` - * - `class A { foo = function bar() {}; }` . `method 'foo'` - * - `class A { static foo = () => {}; }` ... `static method 'foo'` - * - `class A { '#foo' = () => {}; }` ....... `method '#foo'` - * - `class A { #foo = () => {}; }` ......... `private method #foo` - * - `class A { static #foo = () => {}; }` .. `static private method #foo` - * - `class A { '#foo'() {} }` .............. `method '#foo'` - * - `class A { #foo() {} }` ................ `private method #foo` - * - `class A { static #foo() {} }` ......... `static private method #foo` - * @param {ASTNode} node The function node to get. - * @returns {string} The name and kind of the function node. - */ - getFunctionNameWithKind(node) { - const parent = node.parent; - const tokens = []; - - if ( - parent.type === "MethodDefinition" || - parent.type === "PropertyDefinition" || - node.type === "TSPropertySignature" || - node.type === "TSMethodSignature" - ) { - // The proposal uses `static` word consistently before visibility words: https://github.com/tc39/proposal-static-class-features - if (parent.static) { - tokens.push("static"); - } - if (!parent.computed && parent.key?.type === "PrivateIdentifier") { - tokens.push("private"); - } - } - if (node.async) { - tokens.push("async"); - } - if (node.generator) { - tokens.push("generator"); - } - - if (parent.type === "Property" || parent.type === "MethodDefinition") { - if (parent.kind === "constructor") { - return "constructor"; - } - if (parent.kind === "get") { - tokens.push("getter"); - } else if (parent.kind === "set") { - tokens.push("setter"); - } else { - tokens.push("method"); - } - } else if (node.type === "TSMethodSignature") { - if (node.kind === "get") { - tokens.push("getter"); - } else if (node.kind === "set") { - tokens.push("setter"); - } else { - tokens.push("method"); - } - } else if (parent.type === "PropertyDefinition") { - tokens.push("method"); - } else { - if (node.type === "ArrowFunctionExpression") { - tokens.push("arrow"); - } - tokens.push("function"); - } - - if ( - parent.type === "Property" || - parent.type === "MethodDefinition" || - parent.type === "PropertyDefinition" - ) { - if (!parent.computed && parent.key.type === "PrivateIdentifier") { - tokens.push(`#${parent.key.name}`); - } else { - const name = getStaticPropertyName(parent); - - if (name !== null) { - tokens.push(`'${name}'`); - } else if (node.id) { - tokens.push(`'${node.id.name}'`); - } - } - } else if (node.type === "TSMethodSignature") { - tokens.push(`'${getStaticPropertyName(node)}'`); - } else if (node.id) { - tokens.push(`'${node.id.name}'`); - } - - return tokens.join(" "); - }, - - /** - * Gets the location of the given function node for reporting. - * - * - `function foo() {}` - * ^^^^^^^^^^^^ - * - `(function foo() {})` - * ^^^^^^^^^^^^ - * - `(function() {})` - * ^^^^^^^^ - * - `function* foo() {}` - * ^^^^^^^^^^^^^ - * - `(function* foo() {})` - * ^^^^^^^^^^^^^ - * - `(function*() {})` - * ^^^^^^^^^ - * - `() => {}` - * ^^ - * - `async () => {}` - * ^^ - * - `({ foo: function foo() {} })` - * ^^^^^^^^^^^^^^^^^ - * - `({ foo: function() {} })` - * ^^^^^^^^^^^^^ - * - `({ ['foo']: function() {} })` - * ^^^^^^^^^^^^^^^^^ - * - `({ [foo]: function() {} })` - * ^^^^^^^^^^^^^^^ - * - `({ foo() {} })` - * ^^^ - * - `({ foo: function* foo() {} })` - * ^^^^^^^^^^^^^^^^^^ - * - `({ foo: function*() {} })` - * ^^^^^^^^^^^^^^ - * - `({ ['foo']: function*() {} })` - * ^^^^^^^^^^^^^^^^^^ - * - `({ [foo]: function*() {} })` - * ^^^^^^^^^^^^^^^^ - * - `({ *foo() {} })` - * ^^^^ - * - `({ foo: async function foo() {} })` - * ^^^^^^^^^^^^^^^^^^^^^^^ - * - `({ foo: async function() {} })` - * ^^^^^^^^^^^^^^^^^^^ - * - `({ ['foo']: async function() {} })` - * ^^^^^^^^^^^^^^^^^^^^^^^ - * - `({ [foo]: async function() {} })` - * ^^^^^^^^^^^^^^^^^^^^^ - * - `({ async foo() {} })` - * ^^^^^^^^^ - * - `({ get foo() {} })` - * ^^^^^^^ - * - `({ set foo(a) {} })` - * ^^^^^^^ - * - `class A { constructor() {} }` - * ^^^^^^^^^^^ - * - `class A { foo() {} }` - * ^^^ - * - `class A { *foo() {} }` - * ^^^^ - * - `class A { async foo() {} }` - * ^^^^^^^^^ - * - `class A { ['foo']() {} }` - * ^^^^^^^ - * - `class A { *['foo']() {} }` - * ^^^^^^^^ - * - `class A { async ['foo']() {} }` - * ^^^^^^^^^^^^^ - * - `class A { [foo]() {} }` - * ^^^^^ - * - `class A { *[foo]() {} }` - * ^^^^^^ - * - `class A { async [foo]() {} }` - * ^^^^^^^^^^^ - * - `class A { get foo() {} }` - * ^^^^^^^ - * - `class A { set foo(a) {} }` - * ^^^^^^^ - * - `class A { static foo() {} }` - * ^^^^^^^^^^ - * - `class A { static *foo() {} }` - * ^^^^^^^^^^^ - * - `class A { static async foo() {} }` - * ^^^^^^^^^^^^^^^^ - * - `class A { static get foo() {} }` - * ^^^^^^^^^^^^^^ - * - `class A { static set foo(a) {} }` - * ^^^^^^^^^^^^^^ - * - `class A { foo = function() {} }` - * ^^^^^^^^^^^^^^ - * - `class A { static foo = function() {} }` - * ^^^^^^^^^^^^^^^^^^^^^ - * - `class A { foo = (a, b) => {} }` - * ^^^^^^ - * @param {ASTNode} node The function node to get. - * @param {SourceCode} sourceCode The source code object to get tokens. - * @returns {string} The location of the function node for reporting. - */ - getFunctionHeadLoc(node, sourceCode) { - const parent = node.parent; - let start; - let end; - - if ( - parent.type === "Property" || - parent.type === "MethodDefinition" || - parent.type === "PropertyDefinition" || - parent.type === "TSPropertySignature" || - parent.type === "TSMethodSignature" - ) { - start = parent.loc.start; - end = getOpeningParenOfParams(node, sourceCode).loc.start; - } else if (node.type === "ArrowFunctionExpression") { - const arrowToken = sourceCode.getTokenBefore( - node.body, - isArrowToken, - ); - - start = arrowToken.loc.start; - end = arrowToken.loc.end; - } else { - start = node.loc.start; - end = getOpeningParenOfParams(node, sourceCode).loc.start; - } - - return { - start: Object.assign({}, start), - end: Object.assign({}, end), - }; - }, - - /** - * Gets next location when the result is not out of bound, otherwise returns null. - * - * Assumptions: - * - * - The given location represents a valid location in the given source code. - * - Columns are 0-based. - * - Lines are 1-based. - * - Column immediately after the last character in a line (not incl. linebreaks) is considered to be a valid location. - * - If the source code ends with a linebreak, `sourceCode.lines` array will have an extra element (empty string) at the end. - * The start (column 0) of that extra line is considered to be a valid location. - * - * Examples of successive locations (line, column): - * - * code: foo - * locations: (1, 0) -> (1, 1) -> (1, 2) -> (1, 3) -> null - * - * code: foo - * locations: (1, 0) -> (1, 1) -> (1, 2) -> (1, 3) -> (2, 0) -> null - * - * code: foo - * locations: (1, 0) -> (1, 1) -> (1, 2) -> (1, 3) -> (2, 0) -> null - * - * code: ab - * locations: (1, 0) -> (1, 1) -> (2, 0) -> (2, 1) -> null - * - * code: ab - * locations: (1, 0) -> (1, 1) -> (2, 0) -> (2, 1) -> (3, 0) -> null - * - * code: ab - * locations: (1, 0) -> (1, 1) -> (2, 0) -> (2, 1) -> (3, 0) -> null - * - * code: a - * locations: (1, 0) -> (1, 1) -> (2, 0) -> (3, 0) -> null - * - * code: - * locations: (1, 0) -> (2, 0) -> null - * - * code: - * locations: (1, 0) -> null - * @param {SourceCode} sourceCode The sourceCode - * @param {{line: number, column: number}} location The location - * @returns {{line: number, column: number} | null} Next location - */ - getNextLocation(sourceCode, { line, column }) { - if (column < sourceCode.lines[line - 1].length) { - return { - line, - column: column + 1, - }; - } - - if (line < sourceCode.lines.length) { - return { - line: line + 1, - column: 0, - }; - } - - return null; - }, - - /** - * Gets the parenthesized text of a node. This is similar to sourceCode.getText(node), but it also includes any parentheses - * surrounding the node. - * @param {SourceCode} sourceCode The source code object - * @param {ASTNode} node An expression node - * @returns {string} The text representing the node, with all surrounding parentheses included - */ - getParenthesisedText(sourceCode, node) { - let leftToken = sourceCode.getFirstToken(node); - let rightToken = sourceCode.getLastToken(node); - - while ( - sourceCode.getTokenBefore(leftToken) && - sourceCode.getTokenBefore(leftToken).type === "Punctuator" && - sourceCode.getTokenBefore(leftToken).value === "(" && - sourceCode.getTokenAfter(rightToken) && - sourceCode.getTokenAfter(rightToken).type === "Punctuator" && - sourceCode.getTokenAfter(rightToken).value === ")" - ) { - leftToken = sourceCode.getTokenBefore(leftToken); - rightToken = sourceCode.getTokenAfter(rightToken); - } - - return sourceCode - .getText() - .slice(leftToken.range[0], rightToken.range[1]); - }, - - /** - * Determine if a node has a possibility to be an Error object - * @param {ASTNode} node ASTNode to check - * @returns {boolean} True if there is a chance it contains an Error obj - */ - couldBeError(node) { - switch (node.type) { - case "Identifier": - case "CallExpression": - case "NewExpression": - case "MemberExpression": - case "TaggedTemplateExpression": - case "YieldExpression": - case "AwaitExpression": - case "ChainExpression": - return true; // possibly an error object. - - case "AssignmentExpression": - if (["=", "&&="].includes(node.operator)) { - return module.exports.couldBeError(node.right); - } - - if (["||=", "??="].includes(node.operator)) { - return ( - module.exports.couldBeError(node.left) || - module.exports.couldBeError(node.right) - ); - } - - /** - * All other assignment operators are mathematical assignment operators (arithmetic or bitwise). - * An assignment expression with a mathematical operator can either evaluate to a primitive value, - * or throw, depending on the operands. Thus, it cannot evaluate to an `Error` object. - */ - return false; - - case "SequenceExpression": { - const exprs = node.expressions; - - return ( - exprs.length !== 0 && - module.exports.couldBeError(exprs.at(-1)) - ); - } - - case "LogicalExpression": - /* - * If the && operator short-circuits, the left side was falsy and therefore not an error, and if it - * doesn't short-circuit, it takes the value from the right side, so the right side must always be - * a plausible error. A future improvement could verify that the left side could be truthy by - * excluding falsy literals. - */ - if (node.operator === "&&") { - return module.exports.couldBeError(node.right); - } - - return ( - module.exports.couldBeError(node.left) || - module.exports.couldBeError(node.right) - ); - - case "ConditionalExpression": - return ( - module.exports.couldBeError(node.consequent) || - module.exports.couldBeError(node.alternate) - ); - - default: - return false; - } - }, - - /** - * Check if a given node is a numeric literal or not. - * @param {ASTNode} node The node to check. - * @returns {boolean} `true` if the node is a number or bigint literal. - */ - isNumericLiteral(node) { - return ( - node.type === "Literal" && - (typeof node.value === "number" || Boolean(node.bigint)) - ); - }, - - /** - * Determines whether two tokens can safely be placed next to each other without merging into a single token - * @param {Token|string} leftValue The left token. If this is a string, it will be tokenized and the last token will be used. - * @param {Token|string} rightValue The right token. If this is a string, it will be tokenized and the first token will be used. - * @returns {boolean} If the tokens cannot be safely placed next to each other, returns `false`. If the tokens can be placed - * next to each other, behavior is undefined (although it should return `true` in most cases). - */ - canTokensBeAdjacent(leftValue, rightValue) { - const espreeOptions = { - ecmaVersion: espree.latestEcmaVersion, - comment: true, - range: true, - }; - - let leftToken; - - if (typeof leftValue === "string") { - let tokens; - - try { - tokens = espree.tokenize(leftValue, espreeOptions); - } catch { - return false; - } - - const comments = tokens.comments; - - leftToken = tokens.at(-1); - if (comments.length) { - const lastComment = comments.at(-1); - - if (!leftToken || lastComment.range[0] > leftToken.range[0]) { - leftToken = lastComment; - } - } - } else { - leftToken = leftValue; - } - - /* - * If a hashbang comment was passed as a token object from SourceCode, - * its type will be "Shebang" because of the way ESLint itself handles hashbangs. - * If a hashbang comment was passed in a string and then tokenized in this function, - * its type will be "Hashbang" because of the way Espree tokenizes hashbangs. - */ - if (leftToken.type === "Shebang" || leftToken.type === "Hashbang") { - return false; - } - - let rightToken; - - if (typeof rightValue === "string") { - let tokens; - - try { - tokens = espree.tokenize(rightValue, espreeOptions); - } catch { - return false; - } - - const comments = tokens.comments; - - rightToken = tokens[0]; - if (comments.length) { - const firstComment = comments[0]; - - if ( - !rightToken || - firstComment.range[0] < rightToken.range[0] - ) { - rightToken = firstComment; - } - } - } else { - rightToken = rightValue; - } - - if ( - leftToken.type === "Punctuator" || - rightToken.type === "Punctuator" - ) { - if ( - leftToken.type === "Punctuator" && - rightToken.type === "Punctuator" - ) { - const PLUS_TOKENS = new Set(["+", "++"]); - const MINUS_TOKENS = new Set(["-", "--"]); - - return !( - (PLUS_TOKENS.has(leftToken.value) && - PLUS_TOKENS.has(rightToken.value)) || - (MINUS_TOKENS.has(leftToken.value) && - MINUS_TOKENS.has(rightToken.value)) - ); - } - if (leftToken.type === "Punctuator" && leftToken.value === "/") { - return !["Block", "Line", "RegularExpression"].includes( - rightToken.type, - ); - } - return true; - } - - if ( - leftToken.type === "String" || - rightToken.type === "String" || - leftToken.type === "Template" || - rightToken.type === "Template" - ) { - return true; - } - - if ( - leftToken.type !== "Numeric" && - rightToken.type === "Numeric" && - rightToken.value.startsWith(".") - ) { - return true; - } - - if ( - leftToken.type === "Block" || - rightToken.type === "Block" || - rightToken.type === "Line" - ) { - return true; - } - - if (rightToken.type === "PrivateIdentifier") { - return true; - } - - return false; - }, - - /** - * Get the `loc` object of a given name in a `/*globals` directive comment. - * @param {SourceCode} sourceCode The source code to convert index to loc. - * @param {Comment} comment The `/*globals` directive comment which include the name. - * @param {string} name The name to find. - * @returns {SourceLocation} The `loc` object. - */ - getNameLocationInGlobalDirectiveComment(sourceCode, comment, name) { - const namePattern = new RegExp( - `[\\s,]${escapeRegExp(name)}(?:$|[\\s,:])`, - "gu", - ); - - // To ignore the first text "global". - namePattern.lastIndex = comment.value.indexOf("global") + 6; - - // Search a given variable name. - const match = namePattern.exec(comment.value); - - // Convert the index to loc. - const start = sourceCode.getLocFromIndex( - comment.range[0] + "/*".length + (match ? match.index + 1 : 0), - ); - const end = { - line: start.line, - column: start.column + (match ? name.length : 1), - }; - - return { start, end }; - }, - - /** - * Determines whether the given raw string contains an octal escape sequence - * or a non-octal decimal escape sequence ("\8", "\9"). - * - * "\1", "\2" ... "\7", "\8", "\9" - * "\00", "\01" ... "\07", "\08", "\09" - * - * "\0", when not followed by a digit, is not an octal escape sequence. - * @param {string} rawString A string in its raw representation. - * @returns {boolean} `true` if the string contains at least one octal escape sequence - * or at least one non-octal decimal escape sequence. - */ - hasOctalOrNonOctalDecimalEscapeSequence(rawString) { - return OCTAL_OR_NON_OCTAL_DECIMAL_ESCAPE_PATTERN.test(rawString); - }, - - /** - * Determines whether the given node is a template literal without expressions. - * @param {ASTNode} node Node to check. - * @returns {boolean} True if the node is a template literal without expressions. - */ - isStaticTemplateLiteral(node) { - return node.type === "TemplateLiteral" && node.expressions.length === 0; - }, - - /** - * Determines whether the existing curly braces around the single statement are necessary to preserve the semantics of the code. - * The braces, which make the given block body, are necessary in either of the following situations: - * - * 1. The statement is a lexical declaration. - * 2. Without the braces, an `if` within the statement would become associated with an `else` after the closing brace: - * - * if (a) { - * if (b) - * foo(); - * } - * else - * bar(); - * - * if (a) - * while (b) - * while (c) { - * while (d) - * if (e) - * while(f) - * foo(); - * } - * else - * bar(); - * @param {ASTNode} node `BlockStatement` body with exactly one statement directly inside. The statement can have its own nested statements. - * @param {SourceCode} sourceCode The source code - * @returns {boolean} `true` if the braces are necessary - removing them (replacing the given `BlockStatement` body with its single statement content) - * would change the semantics of the code or produce a syntax error. - */ - areBracesNecessary(node, sourceCode) { - /** - * Determines if the given node is a lexical declaration (let, const, using, await using, function, or class) - * @param {ASTNode} nodeToCheck The node to check - * @returns {boolean} True if the node is a lexical declaration - * @private - */ - function isLexicalDeclaration(nodeToCheck) { - if (nodeToCheck.type === "VariableDeclaration") { - return LEXICAL_DECLARATION_KINDS.has(nodeToCheck.kind); - } - - return ( - nodeToCheck.type === "FunctionDeclaration" || - nodeToCheck.type === "ClassDeclaration" - ); - } - - /** - * Checks if the given token is an `else` token or not. - * @param {Token} token The token to check. - * @returns {boolean} `true` if the token is an `else` token. - */ - function isElseKeywordToken(token) { - return token.value === "else" && token.type === "Keyword"; - } - - /** - * Determines whether the given node has an `else` keyword token as the first token after. - * @param {ASTNode} nodeToCheck The node to check. - * @returns {boolean} `true` if the node is followed by an `else` keyword token. - */ - function isFollowedByElseKeyword(nodeToCheck) { - const nextToken = sourceCode.getTokenAfter(nodeToCheck); - - return Boolean(nextToken) && isElseKeywordToken(nextToken); - } - - /** - * Determines whether the code represented by the given node contains an `if` statement - * that would become associated with an `else` keyword directly appended to that code. - * - * Examples where it returns `true`: - * - * if (a) - * foo(); - * - * if (a) { - * foo(); - * } - * - * if (a) - * foo(); - * else if (b) - * bar(); - * - * while (a) - * if (b) - * if(c) - * foo(); - * else - * bar(); - * - * Examples where it returns `false`: - * - * if (a) - * foo(); - * else - * bar(); - * - * while (a) { - * if (b) - * if(c) - * foo(); - * else - * bar(); - * } - * - * while (a) - * if (b) { - * if(c) - * foo(); - * } - * else - * bar(); - * @param {ASTNode} nodeToCheck Node representing the code to check. - * @returns {boolean} `true` if an `if` statement within the code would become associated with an `else` appended to that code. - */ - function hasUnsafeIf(nodeToCheck) { - switch (nodeToCheck.type) { - case "IfStatement": - if (!nodeToCheck.alternate) { - return true; - } - return hasUnsafeIf(nodeToCheck.alternate); - case "ForStatement": - case "ForInStatement": - case "ForOfStatement": - case "LabeledStatement": - case "WithStatement": - case "WhileStatement": - return hasUnsafeIf(nodeToCheck.body); - default: - return false; - } - } - - const statement = node.body[0]; - - return ( - isLexicalDeclaration(statement) || - (hasUnsafeIf(statement) && isFollowedByElseKeyword(node)) - ); - }, - - isReferenceToGlobalVariable, - isLogicalExpression, - isCoalesceExpression, - isMixedLogicalAndCoalesceExpressions, - isNullLiteral, - getStaticStringValue, - getStaticPropertyName, - skipChainExpression, - isSpecificId, - isSpecificMemberAccess, - equalLiteralValue, - isSameReference, - isLogicalAssignmentOperator, - getSwitchCaseColonToken, - getModuleExportName, - isConstant, - isTopLevelExpressionStatement, - isDirective, - isStartOfExpressionStatement, - needsPrecedingSemicolon, - isImportAttributeKey, - getOpeningParenOfParams, + COMMENTS_IGNORE_PATTERN, + LINEBREAKS, + LINEBREAK_MATCHER: lineBreakPattern, + SHEBANG_MATCHER: shebangPattern, + STATEMENT_LIST_PARENTS, + + /** + * Determines whether two adjacent tokens are on the same line. + * @param {Object} left The left token object. + * @param {Object} right The right token object. + * @returns {boolean} Whether or not the tokens are on the same line. + * @public + */ + isTokenOnSameLine(left, right) { + return left.loc.end.line === right.loc.start.line; + }, + + isNullOrUndefined, + isCallee, + isES5Constructor, + getUpperFunction, + isFunction, + isLoop, + isInLoop, + isArrayFromMethod, + isParenthesised, + createGlobalLinebreakMatcher, + equalTokens, + + isArrowToken, + isClosingBraceToken, + isClosingBracketToken, + isClosingParenToken, + isColonToken, + isCommaToken, + isCommentToken, + isDotToken, + isQuestionDotToken, + isKeywordToken, + isNotClosingBraceToken: negate(isClosingBraceToken), + isNotClosingBracketToken: negate(isClosingBracketToken), + isNotClosingParenToken: negate(isClosingParenToken), + isNotColonToken: negate(isColonToken), + isNotCommaToken: negate(isCommaToken), + isNotDotToken: negate(isDotToken), + isNotQuestionDotToken: negate(isQuestionDotToken), + isNotOpeningBraceToken: negate(isOpeningBraceToken), + isNotOpeningBracketToken: negate(isOpeningBracketToken), + isNotOpeningParenToken: negate(isOpeningParenToken), + isNotSemicolonToken: negate(isSemicolonToken), + isOpeningBraceToken, + isOpeningBracketToken, + isOpeningParenToken, + isSemicolonToken, + isEqToken, + + /** + * Checks whether or not a given node is a string literal. + * @param {ASTNode} node A node to check. + * @returns {boolean} `true` if the node is a string literal. + */ + isStringLiteral(node) { + return ( + (node.type === "Literal" && typeof node.value === "string") || + node.type === "TemplateLiteral" + ); + }, + + /** + * Checks whether a given node is a breakable statement or not. + * The node is breakable if the node is one of the following type: + * + * - DoWhileStatement + * - ForInStatement + * - ForOfStatement + * - ForStatement + * - SwitchStatement + * - WhileStatement + * @param {ASTNode} node A node to check. + * @returns {boolean} `true` if the node is breakable. + */ + isBreakableStatement(node) { + return breakableTypePattern.test(node.type); + }, + + /** + * Gets references which are non initializer and writable. + * @param {Reference[]} references An array of references. + * @returns {Reference[]} An array of only references which are non initializer and writable. + * @public + */ + getModifyingReferences(references) { + return references.filter(isModifyingReference); + }, + + /** + * Validate that a string passed in is surrounded by the specified character + * @param {string} val The text to check. + * @param {string} character The character to see if it's surrounded by. + * @returns {boolean} True if the text is surrounded by the character, false if not. + * @private + */ + isSurroundedBy(val, character) { + return val[0] === character && val[val.length - 1] === character; + }, + + /** + * Returns whether the provided node is an ESLint directive comment or not + * @param {Line|Block} node The comment token to be checked + * @returns {boolean} `true` if the node is an ESLint directive comment + */ + isDirectiveComment(node) { + const comment = node.value.trim(); + + return ( + node.type === "Line" && comment.startsWith("eslint-") || + node.type === "Block" && ESLINT_DIRECTIVE_PATTERN.test(comment) + ); + }, + + /** + * Gets the trailing statement of a given node. + * + * if (code) + * consequent; + * + * When taking this `IfStatement`, returns `consequent;` statement. + * @param {ASTNode} A node to get. + * @returns {ASTNode|null} The trailing statement's node. + */ + getTrailingStatement: esutils.ast.trailingStatement, + + /** + * Finds the variable by a given name in a given scope and its upper scopes. + * @param {eslint-scope.Scope} initScope A scope to start find. + * @param {string} name A variable name to find. + * @returns {eslint-scope.Variable|null} A found variable or `null`. + */ + getVariableByName(initScope, name) { + let scope = initScope; + + while (scope) { + const variable = scope.set.get(name); + + if (variable) { + return variable; + } + + scope = scope.upper; + } + + return null; + }, + + /** + * Checks whether or not a given function node is the default `this` binding. + * + * First, this checks the node: + * + * - The given node is not in `PropertyDefinition#value` position. + * - The given node is not `StaticBlock`. + * - The function name does not start with uppercase. It's a convention to capitalize the names + * of constructor functions. This check is not performed if `capIsConstructor` is set to `false`. + * - The function does not have a JSDoc comment that has a @this tag. + * + * Next, this checks the location of the node. + * If the location is below, this judges `this` is valid. + * + * - The location is not on an object literal. + * - The location is not assigned to a variable which starts with an uppercase letter. Applies to anonymous + * functions only, as the name of the variable is considered to be the name of the function in this case. + * This check is not performed if `capIsConstructor` is set to `false`. + * - The location is not on an ES2015 class. + * - Its `bind`/`call`/`apply` method is not called directly. + * - The function is not a callback of array methods (such as `.forEach()`) if `thisArg` is given. + * @param {ASTNode} node A function node to check. It also can be an implicit function, like `StaticBlock` + * or any expression that is `PropertyDefinition#value` node. + * @param {SourceCode} sourceCode A SourceCode instance to get comments. + * @param {boolean} [capIsConstructor = true] `false` disables the assumption that functions which name starts + * with an uppercase or are assigned to a variable which name starts with an uppercase are constructors. + * @returns {boolean} The function node is the default `this` binding. + */ + isDefaultThisBinding(node, sourceCode, { capIsConstructor = true } = {}) { + + /* + * Class field initializers are implicit functions, but ESTree doesn't have the AST node of field initializers. + * Therefore, A expression node at `PropertyDefinition#value` is a function. + * In this case, `this` is always not default binding. + */ + if (node.parent.type === "PropertyDefinition" && node.parent.value === node) { + return false; + } + + // Class static blocks are implicit functions. In this case, `this` is always not default binding. + if (node.type === "StaticBlock") { + return false; + } + + if ( + (capIsConstructor && isES5Constructor(node)) || + hasJSDocThisTag(node, sourceCode) + ) { + return false; + } + const isAnonymous = node.id === null; + let currentNode = node; + + while (currentNode) { + const parent = currentNode.parent; + + switch (parent.type) { + + /* + * Looks up the destination. + * e.g., obj.foo = nativeFoo || function foo() { ... }; + */ + case "LogicalExpression": + case "ConditionalExpression": + case "ChainExpression": + currentNode = parent; + break; + + /* + * If the upper function is IIFE, checks the destination of the return value. + * e.g. + * obj.foo = (function() { + * // setup... + * return function foo() { ... }; + * })(); + * obj.foo = (() => + * function foo() { ... } + * )(); + */ + case "ReturnStatement": { + const func = getUpperFunction(parent); + + if (func === null || !isCallee(func)) { + return true; + } + currentNode = func.parent; + break; + } + case "ArrowFunctionExpression": + if (currentNode !== parent.body || !isCallee(parent)) { + return true; + } + currentNode = parent.parent; + break; + + /* + * e.g. + * var obj = { foo() { ... } }; + * var obj = { foo: function() { ... } }; + * class A { constructor() { ... } } + * class A { foo() { ... } } + * class A { get foo() { ... } } + * class A { set foo() { ... } } + * class A { static foo() { ... } } + * class A { foo = function() { ... } } + */ + case "Property": + case "PropertyDefinition": + case "MethodDefinition": + return parent.value !== currentNode; + + /* + * e.g. + * obj.foo = function foo() { ... }; + * Foo = function() { ... }; + * [obj.foo = function foo() { ... }] = a; + * [Foo = function() { ... }] = a; + */ + case "AssignmentExpression": + case "AssignmentPattern": + if (parent.left.type === "MemberExpression") { + return false; + } + if ( + capIsConstructor && + isAnonymous && + parent.left.type === "Identifier" && + startsWithUpperCase(parent.left.name) + ) { + return false; + } + return true; + + /* + * e.g. + * var Foo = function() { ... }; + */ + case "VariableDeclarator": + return !( + capIsConstructor && + isAnonymous && + parent.init === currentNode && + parent.id.type === "Identifier" && + startsWithUpperCase(parent.id.name) + ); + + /* + * e.g. + * var foo = function foo() { ... }.bind(obj); + * (function foo() { ... }).call(obj); + * (function foo() { ... }).apply(obj, []); + */ + case "MemberExpression": + if ( + parent.object === currentNode && + isSpecificMemberAccess(parent, null, bindOrCallOrApplyPattern) + ) { + const maybeCalleeNode = parent.parent.type === "ChainExpression" + ? parent.parent + : parent; + + return !( + isCallee(maybeCalleeNode) && + maybeCalleeNode.parent.arguments.length >= 1 && + !isNullOrUndefined(maybeCalleeNode.parent.arguments[0]) + ); + } + return true; + + /* + * e.g. + * Reflect.apply(function() {}, obj, []); + * Array.from([], function() {}, obj); + * list.forEach(function() {}, obj); + */ + case "CallExpression": + if (isReflectApply(parent.callee)) { + return ( + parent.arguments.length !== 3 || + parent.arguments[0] !== currentNode || + isNullOrUndefined(parent.arguments[1]) + ); + } + if (isArrayFromMethod(parent.callee)) { + return ( + parent.arguments.length !== 3 || + parent.arguments[1] !== currentNode || + isNullOrUndefined(parent.arguments[2]) + ); + } + if (isMethodWhichHasThisArg(parent.callee)) { + return ( + parent.arguments.length !== 2 || + parent.arguments[0] !== currentNode || + isNullOrUndefined(parent.arguments[1]) + ); + } + return true; + + // Otherwise `this` is default. + default: + return true; + } + } + + /* c8 ignore next */ + return true; + }, + + /** + * Get the precedence level based on the node type + * @param {ASTNode} node node to evaluate + * @returns {int} precedence level + * @private + */ + getPrecedence(node) { + switch (node.type) { + case "SequenceExpression": + return 0; + + case "AssignmentExpression": + case "ArrowFunctionExpression": + case "YieldExpression": + return 1; + + case "ConditionalExpression": + return 3; + + case "LogicalExpression": + switch (node.operator) { + case "||": + case "??": + return 4; + case "&&": + return 5; + + // no default + } + + /* falls through */ + + case "BinaryExpression": + + switch (node.operator) { + case "|": + return 6; + case "^": + return 7; + case "&": + return 8; + case "==": + case "!=": + case "===": + case "!==": + return 9; + case "<": + case "<=": + case ">": + case ">=": + case "in": + case "instanceof": + return 10; + case "<<": + case ">>": + case ">>>": + return 11; + case "+": + case "-": + return 12; + case "*": + case "/": + case "%": + return 13; + case "**": + return 15; + + // no default + } + + /* falls through */ + + case "UnaryExpression": + case "AwaitExpression": + return 16; + + case "UpdateExpression": + return 17; + + case "CallExpression": + case "ChainExpression": + case "ImportExpression": + return 18; + + case "NewExpression": + return 19; + + default: + if (node.type in eslintVisitorKeys) { + return 20; + } + + /* + * if the node is not a standard node that we know about, then assume it has the lowest precedence + * this will mean that rules will wrap unknown nodes in parentheses where applicable instead of + * unwrapping them and potentially changing the meaning of the code or introducing a syntax error. + */ + return -1; + } + }, + + /** + * Checks whether the given node is an empty block node or not. + * @param {ASTNode|null} node The node to check. + * @returns {boolean} `true` if the node is an empty block. + */ + isEmptyBlock(node) { + return Boolean(node && node.type === "BlockStatement" && node.body.length === 0); + }, + + /** + * Checks whether the given node is an empty function node or not. + * @param {ASTNode|null} node The node to check. + * @returns {boolean} `true` if the node is an empty function. + */ + isEmptyFunction(node) { + return isFunction(node) && module.exports.isEmptyBlock(node.body); + }, + + /** + * Get directives from directive prologue of a Program or Function node. + * @param {ASTNode} node The node to check. + * @returns {ASTNode[]} The directives found in the directive prologue. + */ + getDirectivePrologue(node) { + const directives = []; + + // Directive prologues only occur at the top of files or functions. + if ( + node.type === "Program" || + node.type === "FunctionDeclaration" || + node.type === "FunctionExpression" || + + /* + * Do not check arrow functions with implicit return. + * `() => "use strict";` returns the string `"use strict"`. + */ + (node.type === "ArrowFunctionExpression" && node.body.type === "BlockStatement") + ) { + const statements = node.type === "Program" ? node.body : node.body.body; + + for (const statement of statements) { + if ( + statement.type === "ExpressionStatement" && + statement.expression.type === "Literal" + ) { + directives.push(statement); + } else { + break; + } + } + } + + return directives; + }, + + /** + * Determines whether this node is a decimal integer literal. If a node is a decimal integer literal, a dot added + * after the node will be parsed as a decimal point, rather than a property-access dot. + * @param {ASTNode} node The node to check. + * @returns {boolean} `true` if this node is a decimal integer. + * @example + * + * 0 // true + * 5 // true + * 50 // true + * 5_000 // true + * 1_234_56 // true + * 08 // true + * 0192 // true + * 5. // false + * .5 // false + * 5.0 // false + * 5.00_00 // false + * 05 // false + * 0x5 // false + * 0b101 // false + * 0b11_01 // false + * 0o5 // false + * 5e0 // false + * 5e1_000 // false + * 5n // false + * 1_000n // false + * "5" // false + * + */ + isDecimalInteger(node) { + return node.type === "Literal" && typeof node.value === "number" && + DECIMAL_INTEGER_PATTERN.test(node.raw); + }, + + /** + * Determines whether this token is a decimal integer numeric token. + * This is similar to isDecimalInteger(), but for tokens. + * @param {Token} token The token to check. + * @returns {boolean} `true` if this token is a decimal integer. + */ + isDecimalIntegerNumericToken(token) { + return token.type === "Numeric" && DECIMAL_INTEGER_PATTERN.test(token.value); + }, + + /** + * Gets the name and kind of the given function node. + * + * - `function foo() {}` .................... `function 'foo'` + * - `(function foo() {})` .................. `function 'foo'` + * - `(function() {})` ...................... `function` + * - `function* foo() {}` ................... `generator function 'foo'` + * - `(function* foo() {})` ................. `generator function 'foo'` + * - `(function*() {})` ..................... `generator function` + * - `() => {}` ............................. `arrow function` + * - `async () => {}` ....................... `async arrow function` + * - `({ foo: function foo() {} })` ......... `method 'foo'` + * - `({ foo: function() {} })` ............. `method 'foo'` + * - `({ ['foo']: function() {} })` ......... `method 'foo'` + * - `({ [foo]: function() {} })` ........... `method` + * - `({ foo() {} })` ....................... `method 'foo'` + * - `({ foo: function* foo() {} })` ........ `generator method 'foo'` + * - `({ foo: function*() {} })` ............ `generator method 'foo'` + * - `({ ['foo']: function*() {} })` ........ `generator method 'foo'` + * - `({ [foo]: function*() {} })` .......... `generator method` + * - `({ *foo() {} })` ...................... `generator method 'foo'` + * - `({ foo: async function foo() {} })` ... `async method 'foo'` + * - `({ foo: async function() {} })` ....... `async method 'foo'` + * - `({ ['foo']: async function() {} })` ... `async method 'foo'` + * - `({ [foo]: async function() {} })` ..... `async method` + * - `({ async foo() {} })` ................. `async method 'foo'` + * - `({ get foo() {} })` ................... `getter 'foo'` + * - `({ set foo(a) {} })` .................. `setter 'foo'` + * - `class A { constructor() {} }` ......... `constructor` + * - `class A { foo() {} }` ................. `method 'foo'` + * - `class A { *foo() {} }` ................ `generator method 'foo'` + * - `class A { async foo() {} }` ........... `async method 'foo'` + * - `class A { ['foo']() {} }` ............. `method 'foo'` + * - `class A { *['foo']() {} }` ............ `generator method 'foo'` + * - `class A { async ['foo']() {} }` ....... `async method 'foo'` + * - `class A { [foo]() {} }` ............... `method` + * - `class A { *[foo]() {} }` .............. `generator method` + * - `class A { async [foo]() {} }` ......... `async method` + * - `class A { get foo() {} }` ............. `getter 'foo'` + * - `class A { set foo(a) {} }` ............ `setter 'foo'` + * - `class A { static foo() {} }` .......... `static method 'foo'` + * - `class A { static *foo() {} }` ......... `static generator method 'foo'` + * - `class A { static async foo() {} }` .... `static async method 'foo'` + * - `class A { static get foo() {} }` ...... `static getter 'foo'` + * - `class A { static set foo(a) {} }` ..... `static setter 'foo'` + * - `class A { foo = () => {}; }` .......... `method 'foo'` + * - `class A { foo = function() {}; }` ..... `method 'foo'` + * - `class A { foo = function bar() {}; }` . `method 'foo'` + * - `class A { static foo = () => {}; }` ... `static method 'foo'` + * - `class A { '#foo' = () => {}; }` ....... `method '#foo'` + * - `class A { #foo = () => {}; }` ......... `private method #foo` + * - `class A { static #foo = () => {}; }` .. `static private method #foo` + * - `class A { '#foo'() {} }` .............. `method '#foo'` + * - `class A { #foo() {} }` ................ `private method #foo` + * - `class A { static #foo() {} }` ......... `static private method #foo` + * @param {ASTNode} node The function node to get. + * @returns {string} The name and kind of the function node. + */ + getFunctionNameWithKind(node) { + const parent = node.parent; + const tokens = []; + + if (parent.type === "MethodDefinition" || parent.type === "PropertyDefinition") { + + // The proposal uses `static` word consistently before visibility words: https://github.com/tc39/proposal-static-class-features + if (parent.static) { + tokens.push("static"); + } + if (!parent.computed && parent.key.type === "PrivateIdentifier") { + tokens.push("private"); + } + } + if (node.async) { + tokens.push("async"); + } + if (node.generator) { + tokens.push("generator"); + } + + if (parent.type === "Property" || parent.type === "MethodDefinition") { + if (parent.kind === "constructor") { + return "constructor"; + } + if (parent.kind === "get") { + tokens.push("getter"); + } else if (parent.kind === "set") { + tokens.push("setter"); + } else { + tokens.push("method"); + } + } else if (parent.type === "PropertyDefinition") { + tokens.push("method"); + } else { + if (node.type === "ArrowFunctionExpression") { + tokens.push("arrow"); + } + tokens.push("function"); + } + + if (parent.type === "Property" || parent.type === "MethodDefinition" || parent.type === "PropertyDefinition") { + if (!parent.computed && parent.key.type === "PrivateIdentifier") { + tokens.push(`#${parent.key.name}`); + } else { + const name = getStaticPropertyName(parent); + + if (name !== null) { + tokens.push(`'${name}'`); + } else if (node.id) { + tokens.push(`'${node.id.name}'`); + } + } + } else if (node.id) { + tokens.push(`'${node.id.name}'`); + } + + return tokens.join(" "); + }, + + /** + * Gets the location of the given function node for reporting. + * + * - `function foo() {}` + * ^^^^^^^^^^^^ + * - `(function foo() {})` + * ^^^^^^^^^^^^ + * - `(function() {})` + * ^^^^^^^^ + * - `function* foo() {}` + * ^^^^^^^^^^^^^ + * - `(function* foo() {})` + * ^^^^^^^^^^^^^ + * - `(function*() {})` + * ^^^^^^^^^ + * - `() => {}` + * ^^ + * - `async () => {}` + * ^^ + * - `({ foo: function foo() {} })` + * ^^^^^^^^^^^^^^^^^ + * - `({ foo: function() {} })` + * ^^^^^^^^^^^^^ + * - `({ ['foo']: function() {} })` + * ^^^^^^^^^^^^^^^^^ + * - `({ [foo]: function() {} })` + * ^^^^^^^^^^^^^^^ + * - `({ foo() {} })` + * ^^^ + * - `({ foo: function* foo() {} })` + * ^^^^^^^^^^^^^^^^^^ + * - `({ foo: function*() {} })` + * ^^^^^^^^^^^^^^ + * - `({ ['foo']: function*() {} })` + * ^^^^^^^^^^^^^^^^^^ + * - `({ [foo]: function*() {} })` + * ^^^^^^^^^^^^^^^^ + * - `({ *foo() {} })` + * ^^^^ + * - `({ foo: async function foo() {} })` + * ^^^^^^^^^^^^^^^^^^^^^^^ + * - `({ foo: async function() {} })` + * ^^^^^^^^^^^^^^^^^^^ + * - `({ ['foo']: async function() {} })` + * ^^^^^^^^^^^^^^^^^^^^^^^ + * - `({ [foo]: async function() {} })` + * ^^^^^^^^^^^^^^^^^^^^^ + * - `({ async foo() {} })` + * ^^^^^^^^^ + * - `({ get foo() {} })` + * ^^^^^^^ + * - `({ set foo(a) {} })` + * ^^^^^^^ + * - `class A { constructor() {} }` + * ^^^^^^^^^^^ + * - `class A { foo() {} }` + * ^^^ + * - `class A { *foo() {} }` + * ^^^^ + * - `class A { async foo() {} }` + * ^^^^^^^^^ + * - `class A { ['foo']() {} }` + * ^^^^^^^ + * - `class A { *['foo']() {} }` + * ^^^^^^^^ + * - `class A { async ['foo']() {} }` + * ^^^^^^^^^^^^^ + * - `class A { [foo]() {} }` + * ^^^^^ + * - `class A { *[foo]() {} }` + * ^^^^^^ + * - `class A { async [foo]() {} }` + * ^^^^^^^^^^^ + * - `class A { get foo() {} }` + * ^^^^^^^ + * - `class A { set foo(a) {} }` + * ^^^^^^^ + * - `class A { static foo() {} }` + * ^^^^^^^^^^ + * - `class A { static *foo() {} }` + * ^^^^^^^^^^^ + * - `class A { static async foo() {} }` + * ^^^^^^^^^^^^^^^^ + * - `class A { static get foo() {} }` + * ^^^^^^^^^^^^^^ + * - `class A { static set foo(a) {} }` + * ^^^^^^^^^^^^^^ + * - `class A { foo = function() {} }` + * ^^^^^^^^^^^^^^ + * - `class A { static foo = function() {} }` + * ^^^^^^^^^^^^^^^^^^^^^ + * - `class A { foo = (a, b) => {} }` + * ^^^^^^ + * @param {ASTNode} node The function node to get. + * @param {SourceCode} sourceCode The source code object to get tokens. + * @returns {string} The location of the function node for reporting. + */ + getFunctionHeadLoc(node, sourceCode) { + const parent = node.parent; + let start = null; + let end = null; + + if (parent.type === "Property" || parent.type === "MethodDefinition" || parent.type === "PropertyDefinition") { + start = parent.loc.start; + end = getOpeningParenOfParams(node, sourceCode).loc.start; + } else if (node.type === "ArrowFunctionExpression") { + const arrowToken = sourceCode.getTokenBefore(node.body, isArrowToken); + + start = arrowToken.loc.start; + end = arrowToken.loc.end; + } else { + start = node.loc.start; + end = getOpeningParenOfParams(node, sourceCode).loc.start; + } + + return { + start: Object.assign({}, start), + end: Object.assign({}, end) + }; + }, + + /** + * Gets next location when the result is not out of bound, otherwise returns null. + * + * Assumptions: + * + * - The given location represents a valid location in the given source code. + * - Columns are 0-based. + * - Lines are 1-based. + * - Column immediately after the last character in a line (not incl. linebreaks) is considered to be a valid location. + * - If the source code ends with a linebreak, `sourceCode.lines` array will have an extra element (empty string) at the end. + * The start (column 0) of that extra line is considered to be a valid location. + * + * Examples of successive locations (line, column): + * + * code: foo + * locations: (1, 0) -> (1, 1) -> (1, 2) -> (1, 3) -> null + * + * code: foo + * locations: (1, 0) -> (1, 1) -> (1, 2) -> (1, 3) -> (2, 0) -> null + * + * code: foo + * locations: (1, 0) -> (1, 1) -> (1, 2) -> (1, 3) -> (2, 0) -> null + * + * code: ab + * locations: (1, 0) -> (1, 1) -> (2, 0) -> (2, 1) -> null + * + * code: ab + * locations: (1, 0) -> (1, 1) -> (2, 0) -> (2, 1) -> (3, 0) -> null + * + * code: ab + * locations: (1, 0) -> (1, 1) -> (2, 0) -> (2, 1) -> (3, 0) -> null + * + * code: a + * locations: (1, 0) -> (1, 1) -> (2, 0) -> (3, 0) -> null + * + * code: + * locations: (1, 0) -> (2, 0) -> null + * + * code: + * locations: (1, 0) -> null + * @param {SourceCode} sourceCode The sourceCode + * @param {{line: number, column: number}} location The location + * @returns {{line: number, column: number} | null} Next location + */ + getNextLocation(sourceCode, { line, column }) { + if (column < sourceCode.lines[line - 1].length) { + return { + line, + column: column + 1 + }; + } + + if (line < sourceCode.lines.length) { + return { + line: line + 1, + column: 0 + }; + } + + return null; + }, + + /** + * Gets the parenthesized text of a node. This is similar to sourceCode.getText(node), but it also includes any parentheses + * surrounding the node. + * @param {SourceCode} sourceCode The source code object + * @param {ASTNode} node An expression node + * @returns {string} The text representing the node, with all surrounding parentheses included + */ + getParenthesisedText(sourceCode, node) { + let leftToken = sourceCode.getFirstToken(node); + let rightToken = sourceCode.getLastToken(node); + + while ( + sourceCode.getTokenBefore(leftToken) && + sourceCode.getTokenBefore(leftToken).type === "Punctuator" && + sourceCode.getTokenBefore(leftToken).value === "(" && + sourceCode.getTokenAfter(rightToken) && + sourceCode.getTokenAfter(rightToken).type === "Punctuator" && + sourceCode.getTokenAfter(rightToken).value === ")" + ) { + leftToken = sourceCode.getTokenBefore(leftToken); + rightToken = sourceCode.getTokenAfter(rightToken); + } + + return sourceCode.getText().slice(leftToken.range[0], rightToken.range[1]); + }, + + /** + * Determine if a node has a possibility to be an Error object + * @param {ASTNode} node ASTNode to check + * @returns {boolean} True if there is a chance it contains an Error obj + */ + couldBeError(node) { + switch (node.type) { + case "Identifier": + case "CallExpression": + case "NewExpression": + case "MemberExpression": + case "TaggedTemplateExpression": + case "YieldExpression": + case "AwaitExpression": + case "ChainExpression": + return true; // possibly an error object. + + case "AssignmentExpression": + if (["=", "&&="].includes(node.operator)) { + return module.exports.couldBeError(node.right); + } + + if (["||=", "??="].includes(node.operator)) { + return module.exports.couldBeError(node.left) || module.exports.couldBeError(node.right); + } + + /** + * All other assignment operators are mathematical assignment operators (arithmetic or bitwise). + * An assignment expression with a mathematical operator can either evaluate to a primitive value, + * or throw, depending on the operands. Thus, it cannot evaluate to an `Error` object. + */ + return false; + + case "SequenceExpression": { + const exprs = node.expressions; + + return exprs.length !== 0 && module.exports.couldBeError(exprs[exprs.length - 1]); + } + + case "LogicalExpression": + + /* + * If the && operator short-circuits, the left side was falsy and therefore not an error, and if it + * doesn't short-circuit, it takes the value from the right side, so the right side must always be + * a plausible error. A future improvement could verify that the left side could be truthy by + * excluding falsy literals. + */ + if (node.operator === "&&") { + return module.exports.couldBeError(node.right); + } + + return module.exports.couldBeError(node.left) || module.exports.couldBeError(node.right); + + case "ConditionalExpression": + return module.exports.couldBeError(node.consequent) || module.exports.couldBeError(node.alternate); + + default: + return false; + } + }, + + /** + * Check if a given node is a numeric literal or not. + * @param {ASTNode} node The node to check. + * @returns {boolean} `true` if the node is a number or bigint literal. + */ + isNumericLiteral(node) { + return ( + node.type === "Literal" && + (typeof node.value === "number" || Boolean(node.bigint)) + ); + }, + + /** + * Determines whether two tokens can safely be placed next to each other without merging into a single token + * @param {Token|string} leftValue The left token. If this is a string, it will be tokenized and the last token will be used. + * @param {Token|string} rightValue The right token. If this is a string, it will be tokenized and the first token will be used. + * @returns {boolean} If the tokens cannot be safely placed next to each other, returns `false`. If the tokens can be placed + * next to each other, behavior is undefined (although it should return `true` in most cases). + */ + canTokensBeAdjacent(leftValue, rightValue) { + const espreeOptions = { + ecmaVersion: espree.latestEcmaVersion, + comment: true, + range: true + }; + + let leftToken; + + if (typeof leftValue === "string") { + let tokens; + + try { + tokens = espree.tokenize(leftValue, espreeOptions); + } catch { + return false; + } + + const comments = tokens.comments; + + leftToken = tokens[tokens.length - 1]; + if (comments.length) { + const lastComment = comments[comments.length - 1]; + + if (!leftToken || lastComment.range[0] > leftToken.range[0]) { + leftToken = lastComment; + } + } + } else { + leftToken = leftValue; + } + + /* + * If a hashbang comment was passed as a token object from SourceCode, + * its type will be "Shebang" because of the way ESLint itself handles hashbangs. + * If a hashbang comment was passed in a string and then tokenized in this function, + * its type will be "Hashbang" because of the way Espree tokenizes hashbangs. + */ + if (leftToken.type === "Shebang" || leftToken.type === "Hashbang") { + return false; + } + + let rightToken; + + if (typeof rightValue === "string") { + let tokens; + + try { + tokens = espree.tokenize(rightValue, espreeOptions); + } catch { + return false; + } + + const comments = tokens.comments; + + rightToken = tokens[0]; + if (comments.length) { + const firstComment = comments[0]; + + if (!rightToken || firstComment.range[0] < rightToken.range[0]) { + rightToken = firstComment; + } + } + } else { + rightToken = rightValue; + } + + if (leftToken.type === "Punctuator" || rightToken.type === "Punctuator") { + if (leftToken.type === "Punctuator" && rightToken.type === "Punctuator") { + const PLUS_TOKENS = new Set(["+", "++"]); + const MINUS_TOKENS = new Set(["-", "--"]); + + return !( + PLUS_TOKENS.has(leftToken.value) && PLUS_TOKENS.has(rightToken.value) || + MINUS_TOKENS.has(leftToken.value) && MINUS_TOKENS.has(rightToken.value) + ); + } + if (leftToken.type === "Punctuator" && leftToken.value === "/") { + return !["Block", "Line", "RegularExpression"].includes(rightToken.type); + } + return true; + } + + if ( + leftToken.type === "String" || rightToken.type === "String" || + leftToken.type === "Template" || rightToken.type === "Template" + ) { + return true; + } + + if (leftToken.type !== "Numeric" && rightToken.type === "Numeric" && rightToken.value.startsWith(".")) { + return true; + } + + if (leftToken.type === "Block" || rightToken.type === "Block" || rightToken.type === "Line") { + return true; + } + + if (rightToken.type === "PrivateIdentifier") { + return true; + } + + return false; + }, + + /** + * Get the `loc` object of a given name in a `/*globals` directive comment. + * @param {SourceCode} sourceCode The source code to convert index to loc. + * @param {Comment} comment The `/*globals` directive comment which include the name. + * @param {string} name The name to find. + * @returns {SourceLocation} The `loc` object. + */ + getNameLocationInGlobalDirectiveComment(sourceCode, comment, name) { + const namePattern = new RegExp(`[\\s,]${escapeRegExp(name)}(?:$|[\\s,:])`, "gu"); + + // To ignore the first text "global". + namePattern.lastIndex = comment.value.indexOf("global") + 6; + + // Search a given variable name. + const match = namePattern.exec(comment.value); + + // Convert the index to loc. + const start = sourceCode.getLocFromIndex( + comment.range[0] + + "/*".length + + (match ? match.index + 1 : 0) + ); + const end = { + line: start.line, + column: start.column + (match ? name.length : 1) + }; + + return { start, end }; + }, + + /** + * Determines whether the given raw string contains an octal escape sequence + * or a non-octal decimal escape sequence ("\8", "\9"). + * + * "\1", "\2" ... "\7", "\8", "\9" + * "\00", "\01" ... "\07", "\08", "\09" + * + * "\0", when not followed by a digit, is not an octal escape sequence. + * @param {string} rawString A string in its raw representation. + * @returns {boolean} `true` if the string contains at least one octal escape sequence + * or at least one non-octal decimal escape sequence. + */ + hasOctalOrNonOctalDecimalEscapeSequence(rawString) { + return OCTAL_OR_NON_OCTAL_DECIMAL_ESCAPE_PATTERN.test(rawString); + }, + + /** + * Determines whether the given node is a template literal without expressions. + * @param {ASTNode} node Node to check. + * @returns {boolean} True if the node is a template literal without expressions. + */ + isStaticTemplateLiteral(node) { + return node.type === "TemplateLiteral" && node.expressions.length === 0; + }, + + isReferenceToGlobalVariable, + isLogicalExpression, + isCoalesceExpression, + isMixedLogicalAndCoalesceExpressions, + isNullLiteral, + getStaticStringValue, + getStaticPropertyName, + skipChainExpression, + isSpecificId, + isSpecificMemberAccess, + equalLiteralValue, + isSameReference, + isLogicalAssignmentOperator, + getSwitchCaseColonToken, + getModuleExportName, + isConstant, + isTopLevelExpressionStatement, + isDirective, + isStartOfExpressionStatement, + needsPrecedingSemicolon }; diff --git a/node_modules/eslint/lib/rules/utils/char-source.js b/node_modules/eslint/lib/rules/utils/char-source.js deleted file mode 100644 index 7a25b27b9..000000000 --- a/node_modules/eslint/lib/rules/utils/char-source.js +++ /dev/null @@ -1,247 +0,0 @@ -/** - * @fileoverview Utility functions to locate the source text of each code unit in the value of a string literal or template token. - * @author Francesco Trotta - */ - -"use strict"; - -/** - * Represents a code unit produced by the evaluation of a JavaScript common token like a string - * literal or template token. - */ -class CodeUnit { - constructor(start, source) { - this.start = start; - this.source = source; - } - - get end() { - return this.start + this.length; - } - - get length() { - return this.source.length; - } -} - -/** - * An object used to keep track of the position in a source text where the next characters will be read. - */ -class TextReader { - constructor(source) { - this.source = source; - this.pos = 0; - } - - /** - * Advances the reading position of the specified number of characters. - * @param {number} length Number of characters to advance. - * @returns {void} - */ - advance(length) { - this.pos += length; - } - - /** - * Reads characters from the source. - * @param {number} [offset=0] The offset where reading starts, relative to the current position. - * @param {number} [length=1] Number of characters to read. - * @returns {string} A substring of source characters. - */ - read(offset = 0, length = 1) { - const start = offset + this.pos; - - return this.source.slice(start, start + length); - } -} - -const SIMPLE_ESCAPE_SEQUENCES = { - __proto__: null, - b: "\b", - f: "\f", - n: "\n", - r: "\r", - t: "\t", - v: "\v", -}; - -/** - * Reads a hex escape sequence. - * @param {TextReader} reader The reader should be positioned on the first hexadecimal digit. - * @param {number} length The number of hexadecimal digits. - * @returns {string} A code unit. - */ -function readHexSequence(reader, length) { - const str = reader.read(0, length); - const charCode = parseInt(str, 16); - - reader.advance(length); - return String.fromCharCode(charCode); -} - -/** - * Reads a Unicode escape sequence. - * @param {TextReader} reader The reader should be positioned after the "u". - * @returns {string} A code unit. - */ -function readUnicodeSequence(reader) { - const regExp = /\{(?[\dA-F]+)\}/iuy; - - regExp.lastIndex = reader.pos; - const match = regExp.exec(reader.source); - - if (match) { - const codePoint = parseInt(match.groups.hexDigits, 16); - - reader.pos = regExp.lastIndex; - return String.fromCodePoint(codePoint); - } - return readHexSequence(reader, 4); -} - -/** - * Reads an octal escape sequence. - * @param {TextReader} reader The reader should be positioned after the first octal digit. - * @param {number} maxLength The maximum number of octal digits. - * @returns {string} A code unit. - */ -function readOctalSequence(reader, maxLength) { - const [octalStr] = reader.read(-1, maxLength).match(/^[0-7]+/u); - - reader.advance(octalStr.length - 1); - const octal = parseInt(octalStr, 8); - - return String.fromCharCode(octal); -} - -/** - * Reads an escape sequence or line continuation. - * @param {TextReader} reader The reader should be positioned on the backslash. - * @returns {string} A string of zero, one or two code units. - */ -function readEscapeSequenceOrLineContinuation(reader) { - const char = reader.read(1); - - reader.advance(2); - const unitChar = SIMPLE_ESCAPE_SEQUENCES[char]; - - if (unitChar) { - return unitChar; - } - switch (char) { - case "x": - return readHexSequence(reader, 2); - case "u": - return readUnicodeSequence(reader); - case "\r": - if (reader.read() === "\n") { - reader.advance(1); - } - - // fallthrough - case "\n": - case "\u2028": - case "\u2029": - return ""; - case "0": - case "1": - case "2": - case "3": - return readOctalSequence(reader, 3); - case "4": - case "5": - case "6": - case "7": - return readOctalSequence(reader, 2); - default: - return char; - } -} - -/** - * Reads an escape sequence or line continuation and generates the respective `CodeUnit` elements. - * @param {TextReader} reader The reader should be positioned on the backslash. - * @returns {Generator} Zero, one or two `CodeUnit` elements. - */ -function* mapEscapeSequenceOrLineContinuation(reader) { - const start = reader.pos; - const str = readEscapeSequenceOrLineContinuation(reader); - const end = reader.pos; - const source = reader.source.slice(start, end); - - switch (str.length) { - case 0: - break; - case 1: - yield new CodeUnit(start, source); - break; - default: - yield new CodeUnit(start, source); - yield new CodeUnit(start, source); - break; - } -} - -/** - * Parses a string literal. - * @param {string} source The string literal to parse, including the delimiting quotes. - * @returns {CodeUnit[]} A list of code units produced by the string literal. - */ -function parseStringLiteral(source) { - const reader = new TextReader(source); - const quote = reader.read(); - - reader.advance(1); - const codeUnits = []; - - for (;;) { - const char = reader.read(); - - if (char === quote) { - break; - } - if (char === "\\") { - codeUnits.push(...mapEscapeSequenceOrLineContinuation(reader)); - } else { - codeUnits.push(new CodeUnit(reader.pos, char)); - reader.advance(1); - } - } - return codeUnits; -} - -/** - * Parses a template token. - * @param {string} source The template token to parse, including the delimiting sequences `` ` ``, `${` and `}`. - * @returns {CodeUnit[]} A list of code units produced by the template token. - */ -function parseTemplateToken(source) { - const reader = new TextReader(source); - - reader.advance(1); - const codeUnits = []; - - for (;;) { - const char = reader.read(); - - if (char === "`" || (char === "$" && reader.read(1) === "{")) { - break; - } - if (char === "\\") { - codeUnits.push(...mapEscapeSequenceOrLineContinuation(reader)); - } else { - let unitSource; - - if (char === "\r" && reader.read(1) === "\n") { - unitSource = "\r\n"; - } else { - unitSource = char; - } - codeUnits.push(new CodeUnit(reader.pos, unitSource)); - reader.advance(unitSource.length); - } - } - return codeUnits; -} - -module.exports = { parseStringLiteral, parseTemplateToken }; diff --git a/node_modules/eslint/lib/rules/utils/fix-tracker.js b/node_modules/eslint/lib/rules/utils/fix-tracker.js index 5721717b8..589870b39 100644 --- a/node_modules/eslint/lib/rules/utils/fix-tracker.js +++ b/node_modules/eslint/lib/rules/utils/fix-tracker.js @@ -4,14 +4,6 @@ */ "use strict"; -//------------------------------------------------------------------------------ -// Typedefs -//------------------------------------------------------------------------------ - -/** - * @import { SourceRange } from "@eslint/core"; - */ - //------------------------------------------------------------------------------ // Requirements //------------------------------------------------------------------------------ @@ -28,98 +20,95 @@ const astUtils = require("./ast-utils"); * replaced so that other fixes won't touch that region in the same pass. */ class FixTracker { - /** - * Create a new FixTracker. - * @param {ruleFixer} fixer A ruleFixer instance. - * @param {SourceCode} sourceCode A SourceCode object for the current code. - */ - constructor(fixer, sourceCode) { - this.fixer = fixer; - this.sourceCode = sourceCode; - this.retainedRange = null; - } - - /** - * Mark the given range as "retained", meaning that other fixes may not - * may not modify this region in the same pass. - * @param {SourceRange} range The range to retain. - * @returns {FixTracker} The same RuleFixer, for chained calls. - */ - retainRange(range) { - this.retainedRange = range; - return this; - } - - /** - * Given a node, find the function containing it (or the entire program) and - * mark it as retained, meaning that other fixes may not modify it in this - * pass. This is useful for avoiding conflicts in fixes that modify control - * flow. - * @param {ASTNode} node The node to use as a starting point. - * @returns {FixTracker} The same RuleFixer, for chained calls. - */ - retainEnclosingFunction(node) { - const functionNode = astUtils.getUpperFunction(node); - - return this.retainRange( - functionNode ? functionNode.range : this.sourceCode.ast.range, - ); - } - - /** - * Given a node or token, find the token before and afterward, and mark that - * range as retained, meaning that other fixes may not modify it in this - * pass. This is useful for avoiding conflicts in fixes that make a small - * change to the code where the AST should not be changed. - * @param {ASTNode|Token} nodeOrToken The node or token to use as a starting - * point. The token to the left and right are use in the range. - * @returns {FixTracker} The same RuleFixer, for chained calls. - */ - retainSurroundingTokens(nodeOrToken) { - const tokenBefore = - this.sourceCode.getTokenBefore(nodeOrToken) || nodeOrToken; - const tokenAfter = - this.sourceCode.getTokenAfter(nodeOrToken) || nodeOrToken; - - return this.retainRange([tokenBefore.range[0], tokenAfter.range[1]]); - } - - /** - * Create a fix command that replaces the given range with the given text, - * accounting for any retained ranges. - * @param {SourceRange} range The range to remove in the fix. - * @param {string} text The text to insert in place of the range. - * @returns {Object} The fix command. - */ - replaceTextRange(range, text) { - let actualRange; - - if (this.retainedRange) { - actualRange = [ - Math.min(this.retainedRange[0], range[0]), - Math.max(this.retainedRange[1], range[1]), - ]; - } else { - actualRange = range; - } - - return this.fixer.replaceTextRange( - actualRange, - this.sourceCode.text.slice(actualRange[0], range[0]) + - text + - this.sourceCode.text.slice(range[1], actualRange[1]), - ); - } - /** - * Create a fix command that removes the given node or token, accounting for - * any retained ranges. - * @param {ASTNode|Token} nodeOrToken The node or token to remove. - * @returns {Object} The fix command. - */ - remove(nodeOrToken) { - return this.replaceTextRange(nodeOrToken.range, ""); - } + /** + * Create a new FixTracker. + * @param {ruleFixer} fixer A ruleFixer instance. + * @param {SourceCode} sourceCode A SourceCode object for the current code. + */ + constructor(fixer, sourceCode) { + this.fixer = fixer; + this.sourceCode = sourceCode; + this.retainedRange = null; + } + + /** + * Mark the given range as "retained", meaning that other fixes may not + * may not modify this region in the same pass. + * @param {int[]} range The range to retain. + * @returns {FixTracker} The same RuleFixer, for chained calls. + */ + retainRange(range) { + this.retainedRange = range; + return this; + } + + /** + * Given a node, find the function containing it (or the entire program) and + * mark it as retained, meaning that other fixes may not modify it in this + * pass. This is useful for avoiding conflicts in fixes that modify control + * flow. + * @param {ASTNode} node The node to use as a starting point. + * @returns {FixTracker} The same RuleFixer, for chained calls. + */ + retainEnclosingFunction(node) { + const functionNode = astUtils.getUpperFunction(node); + + return this.retainRange(functionNode ? functionNode.range : this.sourceCode.ast.range); + } + + /** + * Given a node or token, find the token before and afterward, and mark that + * range as retained, meaning that other fixes may not modify it in this + * pass. This is useful for avoiding conflicts in fixes that make a small + * change to the code where the AST should not be changed. + * @param {ASTNode|Token} nodeOrToken The node or token to use as a starting + * point. The token to the left and right are use in the range. + * @returns {FixTracker} The same RuleFixer, for chained calls. + */ + retainSurroundingTokens(nodeOrToken) { + const tokenBefore = this.sourceCode.getTokenBefore(nodeOrToken) || nodeOrToken; + const tokenAfter = this.sourceCode.getTokenAfter(nodeOrToken) || nodeOrToken; + + return this.retainRange([tokenBefore.range[0], tokenAfter.range[1]]); + } + + /** + * Create a fix command that replaces the given range with the given text, + * accounting for any retained ranges. + * @param {int[]} range The range to remove in the fix. + * @param {string} text The text to insert in place of the range. + * @returns {Object} The fix command. + */ + replaceTextRange(range, text) { + let actualRange; + + if (this.retainedRange) { + actualRange = [ + Math.min(this.retainedRange[0], range[0]), + Math.max(this.retainedRange[1], range[1]) + ]; + } else { + actualRange = range; + } + + return this.fixer.replaceTextRange( + actualRange, + this.sourceCode.text.slice(actualRange[0], range[0]) + + text + + this.sourceCode.text.slice(range[1], actualRange[1]) + ); + } + + /** + * Create a fix command that removes the given node or token, accounting for + * any retained ranges. + * @param {ASTNode|Token} nodeOrToken The node or token to remove. + * @returns {Object} The fix command. + */ + remove(nodeOrToken) { + return this.replaceTextRange(nodeOrToken.range, ""); + } } module.exports = FixTracker; diff --git a/node_modules/eslint/lib/rules/utils/keywords.js b/node_modules/eslint/lib/rules/utils/keywords.js index eca2076eb..3fbb77771 100644 --- a/node_modules/eslint/lib/rules/utils/keywords.js +++ b/node_modules/eslint/lib/rules/utils/keywords.js @@ -5,63 +5,63 @@ "use strict"; module.exports = [ - "abstract", - "boolean", - "break", - "byte", - "case", - "catch", - "char", - "class", - "const", - "continue", - "debugger", - "default", - "delete", - "do", - "double", - "else", - "enum", - "export", - "extends", - "false", - "final", - "finally", - "float", - "for", - "function", - "goto", - "if", - "implements", - "import", - "in", - "instanceof", - "int", - "interface", - "long", - "native", - "new", - "null", - "package", - "private", - "protected", - "public", - "return", - "short", - "static", - "super", - "switch", - "synchronized", - "this", - "throw", - "throws", - "transient", - "true", - "try", - "typeof", - "var", - "void", - "volatile", - "while", - "with", + "abstract", + "boolean", + "break", + "byte", + "case", + "catch", + "char", + "class", + "const", + "continue", + "debugger", + "default", + "delete", + "do", + "double", + "else", + "enum", + "export", + "extends", + "false", + "final", + "finally", + "float", + "for", + "function", + "goto", + "if", + "implements", + "import", + "in", + "instanceof", + "int", + "interface", + "long", + "native", + "new", + "null", + "package", + "private", + "protected", + "public", + "return", + "short", + "static", + "super", + "switch", + "synchronized", + "this", + "throw", + "throws", + "transient", + "true", + "try", + "typeof", + "var", + "void", + "volatile", + "while", + "with" ]; diff --git a/node_modules/eslint/lib/rules/utils/lazy-loading-rule-map.js b/node_modules/eslint/lib/rules/utils/lazy-loading-rule-map.js index b93395095..7f116a268 100644 --- a/node_modules/eslint/lib/rules/utils/lazy-loading-rule-map.js +++ b/node_modules/eslint/lib/rules/utils/lazy-loading-rule-map.js @@ -6,7 +6,7 @@ const debug = require("debug")("eslint:rules"); -/** @typedef {import("../../types").Rule.RuleModule} Rule */ +/** @typedef {import("./types").Rule} Rule */ /** * The `Map` object that loads each rule when it's accessed. @@ -19,100 +19,97 @@ const debug = require("debug")("eslint:rules"); * * rules.get("semi"); // call `() => require("semi")` here. * - * @extends {Map} + * @extends {Map Rule>} */ class LazyLoadingRuleMap extends Map { - /** - * Initialize this map. - * @param {Array<[string, function(): Rule]>} loaders The rule loaders. - */ - constructor(loaders) { - let remaining = loaders.length; - super( - debug.enabled - ? loaders.map(([ruleId, load]) => { - let cache = null; + /** + * Initialize this map. + * @param {Array<[string, function(): Rule]>} loaders The rule loaders. + */ + constructor(loaders) { + let remaining = loaders.length; - return [ - ruleId, - () => { - if (!cache) { - debug( - "Loading rule %o (remaining=%d)", - ruleId, - --remaining, - ); - cache = load(); - } - return cache; - }, - ]; - }) - : loaders, - ); + super( + debug.enabled + ? loaders.map(([ruleId, load]) => { + let cache = null; - // `super(...iterable)` uses `this.set()`, so disable it here. - Object.defineProperty(LazyLoadingRuleMap.prototype, "set", { - configurable: true, - value: void 0, - }); - } + return [ + ruleId, + () => { + if (!cache) { + debug("Loading rule %o (remaining=%d)", ruleId, --remaining); + cache = load(); + } + return cache; + } + ]; + }) + : loaders + ); - /** - * Get a rule. - * Each rule will be loaded on the first access. - * @param {string} ruleId The rule ID to get. - * @returns {Rule|undefined} The rule. - */ - get(ruleId) { - const load = super.get(ruleId); + // `super(...iterable)` uses `this.set()`, so disable it here. + Object.defineProperty(LazyLoadingRuleMap.prototype, "set", { + configurable: true, + value: void 0 + }); + } - return load && load(); - } + /** + * Get a rule. + * Each rule will be loaded on the first access. + * @param {string} ruleId The rule ID to get. + * @returns {Rule|undefined} The rule. + */ + get(ruleId) { + const load = super.get(ruleId); - /** - * Iterate rules. - * @returns {IterableIterator} Rules. - */ - *values() { - for (const load of super.values()) { - yield load(); - } - } + return load && load(); + } - /** - * Iterate rules. - * @returns {IterableIterator<[string, Rule]>} Rules. - */ - *entries() { - for (const [ruleId, load] of super.entries()) { - yield [ruleId, load()]; - } - } + /** + * Iterate rules. + * @returns {IterableIterator} Rules. + */ + *values() { + for (const load of super.values()) { + yield load(); + } + } - /** - * Call a function with each rule. - * @param {Function} callbackFn The callback function. - * @param {any} [thisArg] The object to pass to `this` of the callback function. - * @returns {void} - */ - forEach(callbackFn, thisArg) { - for (const [ruleId, load] of super.entries()) { - callbackFn.call(thisArg, load(), ruleId, this); - } - } + /** + * Iterate rules. + * @returns {IterableIterator<[string, Rule]>} Rules. + */ + *entries() { + for (const [ruleId, load] of super.entries()) { + yield [ruleId, load()]; + } + } + + /** + * Call a function with each rule. + * @param {Function} callbackFn The callback function. + * @param {any} [thisArg] The object to pass to `this` of the callback function. + * @returns {void} + */ + forEach(callbackFn, thisArg) { + for (const [ruleId, load] of super.entries()) { + callbackFn.call(thisArg, load(), ruleId, this); + } + } } // Forbid mutation. Object.defineProperties(LazyLoadingRuleMap.prototype, { - clear: { configurable: true, value: void 0 }, - delete: { configurable: true, value: void 0 }, - [Symbol.iterator]: { - configurable: true, - writable: true, - value: LazyLoadingRuleMap.prototype.entries, - }, + clear: { configurable: true, value: void 0 }, + delete: { configurable: true, value: void 0 }, + [Symbol.iterator]: { + configurable: true, + writable: true, + value: LazyLoadingRuleMap.prototype.entries + } }); module.exports = { LazyLoadingRuleMap }; diff --git a/node_modules/eslint/lib/rules/utils/regular-expressions.js b/node_modules/eslint/lib/rules/utils/regular-expressions.js index 6d80654b6..12e544e37 100644 --- a/node_modules/eslint/lib/rules/utils/regular-expressions.js +++ b/node_modules/eslint/lib/rules/utils/regular-expressions.js @@ -8,51 +8,35 @@ const { RegExpValidator } = require("@eslint-community/regexpp"); -const REGEXPP_LATEST_ECMA_VERSION = 2025; +const REGEXPP_LATEST_ECMA_VERSION = 2024; /** * Checks if the given regular expression pattern would be valid with the `u` flag. * @param {number} ecmaVersion ECMAScript version to parse in. * @param {string} pattern The regular expression pattern to verify. - * @param {"u"|"v"} flag The type of Unicode flag * @returns {boolean} `true` if the pattern would be valid with the `u` flag. * `false` if the pattern would be invalid with the `u` flag or the configured * ecmaVersion doesn't support the `u` flag. */ -function isValidWithUnicodeFlag(ecmaVersion, pattern, flag = "u") { - if (flag === "u" && ecmaVersion <= 5) { - // ecmaVersion <= 5 doesn't support the 'u' flag - return false; - } - if (flag === "v" && ecmaVersion <= 2023) { - return false; - } - - const validator = new RegExpValidator({ - ecmaVersion: Math.min(ecmaVersion, REGEXPP_LATEST_ECMA_VERSION), - }); - - try { - validator.validatePattern( - pattern, - void 0, - void 0, - flag === "u" - ? { - unicode: /* uFlag = */ true, - } - : { - unicodeSets: true, - }, - ); - } catch { - return false; - } - - return true; +function isValidWithUnicodeFlag(ecmaVersion, pattern) { + if (ecmaVersion <= 5) { // ecmaVersion <= 5 doesn't support the 'u' flag + return false; + } + + const validator = new RegExpValidator({ + ecmaVersion: Math.min(ecmaVersion, REGEXPP_LATEST_ECMA_VERSION) + }); + + try { + validator.validatePattern(pattern, void 0, void 0, { unicode: /* uFlag = */ true }); + } catch { + return false; + } + + return true; } module.exports = { - isValidWithUnicodeFlag, - REGEXPP_LATEST_ECMA_VERSION, + isValidWithUnicodeFlag, + REGEXPP_LATEST_ECMA_VERSION }; diff --git a/node_modules/eslint/lib/rules/utils/unicode/index.js b/node_modules/eslint/lib/rules/utils/unicode/index.js index 0e7d5d53c..02eea502d 100644 --- a/node_modules/eslint/lib/rules/utils/unicode/index.js +++ b/node_modules/eslint/lib/rules/utils/unicode/index.js @@ -3,14 +3,9 @@ */ "use strict"; -const isCombiningCharacter = require("./is-combining-character"); -const isEmojiModifier = require("./is-emoji-modifier"); -const isRegionalIndicatorSymbol = require("./is-regional-indicator-symbol"); -const isSurrogatePair = require("./is-surrogate-pair"); - module.exports = { - isCombiningCharacter, - isEmojiModifier, - isRegionalIndicatorSymbol, - isSurrogatePair, + isCombiningCharacter: require("./is-combining-character"), + isEmojiModifier: require("./is-emoji-modifier"), + isRegionalIndicatorSymbol: require("./is-regional-indicator-symbol"), + isSurrogatePair: require("./is-surrogate-pair") }; diff --git a/node_modules/eslint/lib/rules/utils/unicode/is-combining-character.js b/node_modules/eslint/lib/rules/utils/unicode/is-combining-character.js index 8a36b223f..0498b99a2 100644 --- a/node_modules/eslint/lib/rules/utils/unicode/is-combining-character.js +++ b/node_modules/eslint/lib/rules/utils/unicode/is-combining-character.js @@ -9,5 +9,5 @@ * @returns {boolean} `true` if the character belongs to the category, any of `Mc`, `Me`, and `Mn`. */ module.exports = function isCombiningCharacter(codePoint) { - return /^[\p{Mc}\p{Me}\p{Mn}]$/u.test(String.fromCodePoint(codePoint)); + return /^[\p{Mc}\p{Me}\p{Mn}]$/u.test(String.fromCodePoint(codePoint)); }; diff --git a/node_modules/eslint/lib/rules/utils/unicode/is-emoji-modifier.js b/node_modules/eslint/lib/rules/utils/unicode/is-emoji-modifier.js index 526ac91d6..1bd5f557d 100644 --- a/node_modules/eslint/lib/rules/utils/unicode/is-emoji-modifier.js +++ b/node_modules/eslint/lib/rules/utils/unicode/is-emoji-modifier.js @@ -9,5 +9,5 @@ * @returns {boolean} `true` if the character is an emoji modifier. */ module.exports = function isEmojiModifier(code) { - return code >= 0x1f3fb && code <= 0x1f3ff; + return code >= 0x1F3FB && code <= 0x1F3FF; }; diff --git a/node_modules/eslint/lib/rules/utils/unicode/is-regional-indicator-symbol.js b/node_modules/eslint/lib/rules/utils/unicode/is-regional-indicator-symbol.js index 019d9da0a..c48ed46ef 100644 --- a/node_modules/eslint/lib/rules/utils/unicode/is-regional-indicator-symbol.js +++ b/node_modules/eslint/lib/rules/utils/unicode/is-regional-indicator-symbol.js @@ -9,5 +9,5 @@ * @returns {boolean} `true` if the character is a regional indicator symbol. */ module.exports = function isRegionalIndicatorSymbol(code) { - return code >= 0x1f1e6 && code <= 0x1f1ff; + return code >= 0x1F1E6 && code <= 0x1F1FF; }; diff --git a/node_modules/eslint/lib/rules/utils/unicode/is-surrogate-pair.js b/node_modules/eslint/lib/rules/utils/unicode/is-surrogate-pair.js index fbe6a4017..b8e5c1cac 100644 --- a/node_modules/eslint/lib/rules/utils/unicode/is-surrogate-pair.js +++ b/node_modules/eslint/lib/rules/utils/unicode/is-surrogate-pair.js @@ -10,5 +10,5 @@ * @returns {boolean} `true` if the character pair is a surrogate pair. */ module.exports = function isSurrogatePair(lead, tail) { - return lead >= 0xd800 && lead < 0xdc00 && tail >= 0xdc00 && tail < 0xe000; + return lead >= 0xD800 && lead < 0xDC00 && tail >= 0xDC00 && tail < 0xE000; }; diff --git a/node_modules/eslint/lib/rules/valid-typeof.js b/node_modules/eslint/lib/rules/valid-typeof.js index fd4f3d204..3818dafea 100644 --- a/node_modules/eslint/lib/rules/valid-typeof.js +++ b/node_modules/eslint/lib/rules/valid-typeof.js @@ -14,158 +14,114 @@ const astUtils = require("./utils/ast-utils"); // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "problem", - - defaultOptions: [ - { - requireStringLiterals: false, - }, - ], - - docs: { - description: - "Enforce comparing `typeof` expressions against valid strings", - recommended: true, - url: "https://eslint.org/docs/latest/rules/valid-typeof", - }, - - hasSuggestions: true, - - schema: [ - { - type: "object", - properties: { - requireStringLiterals: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - messages: { - invalidValue: "Invalid typeof comparison value.", - notString: "Typeof comparisons should be to string literals.", - suggestString: 'Use `"{{type}}"` instead of `{{type}}`.', - }, - }, - - create(context) { - const VALID_TYPES = new Set([ - "symbol", - "undefined", - "object", - "boolean", - "number", - "string", - "function", - "bigint", - ]), - OPERATORS = new Set(["==", "===", "!=", "!=="]); - const sourceCode = context.sourceCode; - const [{ requireStringLiterals }] = context.options; - - let globalScope; - - /** - * Checks whether the given node represents a reference to a global variable that is not declared in the source code. - * These identifiers will be allowed, as it is assumed that user has no control over the names of external global variables. - * @param {ASTNode} node `Identifier` node to check. - * @returns {boolean} `true` if the node is a reference to a global variable. - */ - function isReferenceToGlobalVariable(node) { - const variable = globalScope.set.get(node.name); - - return ( - variable && - variable.defs.length === 0 && - variable.references.some(ref => ref.identifier === node) - ); - } - - /** - * Determines whether a node is a typeof expression. - * @param {ASTNode} node The node - * @returns {boolean} `true` if the node is a typeof expression - */ - function isTypeofExpression(node) { - return ( - node.type === "UnaryExpression" && node.operator === "typeof" - ); - } - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - return { - Program(node) { - globalScope = sourceCode.getScope(node); - }, - - UnaryExpression(node) { - if (isTypeofExpression(node)) { - const { parent } = node; - - if ( - parent.type === "BinaryExpression" && - OPERATORS.has(parent.operator) - ) { - const sibling = - parent.left === node ? parent.right : parent.left; - - if ( - sibling.type === "Literal" || - astUtils.isStaticTemplateLiteral(sibling) - ) { - const value = - sibling.type === "Literal" - ? sibling.value - : sibling.quasis[0].value.cooked; - - if (!VALID_TYPES.has(value)) { - context.report({ - node: sibling, - messageId: "invalidValue", - }); - } - } else if ( - sibling.type === "Identifier" && - sibling.name === "undefined" && - isReferenceToGlobalVariable(sibling) - ) { - context.report({ - node: sibling, - messageId: requireStringLiterals - ? "notString" - : "invalidValue", - suggest: [ - { - messageId: "suggestString", - data: { type: "undefined" }, - fix(fixer) { - return fixer.replaceText( - sibling, - '"undefined"', - ); - }, - }, - ], - }); - } else if ( - requireStringLiterals && - !isTypeofExpression(sibling) - ) { - context.report({ - node: sibling, - messageId: "notString", - }); - } - } - } - }, - }; - }, + meta: { + type: "problem", + + docs: { + description: "Enforce comparing `typeof` expressions against valid strings", + recommended: true, + url: "https://eslint.org/docs/latest/rules/valid-typeof" + }, + + hasSuggestions: true, + + schema: [ + { + type: "object", + properties: { + requireStringLiterals: { + type: "boolean", + default: false + } + }, + additionalProperties: false + } + ], + messages: { + invalidValue: "Invalid typeof comparison value.", + notString: "Typeof comparisons should be to string literals.", + suggestString: 'Use `"{{type}}"` instead of `{{type}}`.' + } + }, + + create(context) { + + const VALID_TYPES = new Set(["symbol", "undefined", "object", "boolean", "number", "string", "function", "bigint"]), + OPERATORS = new Set(["==", "===", "!=", "!=="]); + const sourceCode = context.sourceCode; + const requireStringLiterals = context.options[0] && context.options[0].requireStringLiterals; + + let globalScope; + + /** + * Checks whether the given node represents a reference to a global variable that is not declared in the source code. + * These identifiers will be allowed, as it is assumed that user has no control over the names of external global variables. + * @param {ASTNode} node `Identifier` node to check. + * @returns {boolean} `true` if the node is a reference to a global variable. + */ + function isReferenceToGlobalVariable(node) { + const variable = globalScope.set.get(node.name); + + return variable && variable.defs.length === 0 && + variable.references.some(ref => ref.identifier === node); + } + + /** + * Determines whether a node is a typeof expression. + * @param {ASTNode} node The node + * @returns {boolean} `true` if the node is a typeof expression + */ + function isTypeofExpression(node) { + return node.type === "UnaryExpression" && node.operator === "typeof"; + } + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + return { + + Program(node) { + globalScope = sourceCode.getScope(node); + }, + + UnaryExpression(node) { + if (isTypeofExpression(node)) { + const { parent } = node; + + if (parent.type === "BinaryExpression" && OPERATORS.has(parent.operator)) { + const sibling = parent.left === node ? parent.right : parent.left; + + if (sibling.type === "Literal" || astUtils.isStaticTemplateLiteral(sibling)) { + const value = sibling.type === "Literal" ? sibling.value : sibling.quasis[0].value.cooked; + + if (!VALID_TYPES.has(value)) { + context.report({ node: sibling, messageId: "invalidValue" }); + } + } else if (sibling.type === "Identifier" && sibling.name === "undefined" && isReferenceToGlobalVariable(sibling)) { + context.report({ + node: sibling, + messageId: requireStringLiterals ? "notString" : "invalidValue", + suggest: [ + { + messageId: "suggestString", + data: { type: "undefined" }, + fix(fixer) { + return fixer.replaceText(sibling, '"undefined"'); + } + } + ] + }); + } else if (requireStringLiterals && !isTypeofExpression(sibling)) { + context.report({ node: sibling, messageId: "notString" }); + } + } + } + } + + }; + + } }; diff --git a/node_modules/eslint/lib/rules/vars-on-top.js b/node_modules/eslint/lib/rules/vars-on-top.js index c790ccbd5..81f5d62d0 100644 --- a/node_modules/eslint/lib/rules/vars-on-top.js +++ b/node_modules/eslint/lib/rules/vars-on-top.js @@ -9,157 +9,149 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - docs: { - description: - "Require `var` declarations be placed at the top of their containing scope", - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/vars-on-top", - }, - - schema: [], - messages: { - top: "All 'var' declarations must be at the top of the function scope.", - }, - }, - - create(context) { - //-------------------------------------------------------------------------- - // Helpers - //-------------------------------------------------------------------------- - - /** - * Has AST suggesting a directive. - * @param {ASTNode} node any node - * @returns {boolean} whether the given node structurally represents a directive - */ - function looksLikeDirective(node) { - return ( - node.type === "ExpressionStatement" && - node.expression.type === "Literal" && - typeof node.expression.value === "string" - ); - } - - /** - * Check to see if its a ES6 import declaration - * @param {ASTNode} node any node - * @returns {boolean} whether the given node represents a import declaration - */ - function looksLikeImport(node) { - return ( - node.type === "ImportDeclaration" || - node.type === "ImportSpecifier" || - node.type === "ImportDefaultSpecifier" || - node.type === "ImportNamespaceSpecifier" - ); - } - - /** - * Checks whether a given node is a variable declaration or not. - * @param {ASTNode} node any node - * @returns {boolean} `true` if the node is a variable declaration. - */ - function isVariableDeclaration(node) { - return ( - node.type === "VariableDeclaration" || - (node.type === "ExportNamedDeclaration" && - node.declaration && - node.declaration.type === "VariableDeclaration") - ); - } - - /** - * Checks whether this variable is on top of the block body - * @param {ASTNode} node The node to check - * @param {ASTNode[]} statements collection of ASTNodes for the parent node block - * @returns {boolean} True if var is on top otherwise false - */ - function isVarOnTop(node, statements) { - const l = statements.length; - let i = 0; - - // Skip over directives and imports. Static blocks don't have either. - if (node.parent.type !== "StaticBlock") { - for (; i < l; ++i) { - if ( - !looksLikeDirective(statements[i]) && - !looksLikeImport(statements[i]) - ) { - break; - } - } - } - - for (; i < l; ++i) { - if (!isVariableDeclaration(statements[i])) { - return false; - } - if (statements[i] === node) { - return true; - } - } - - return false; - } - - /** - * Checks whether variable is on top at the global level - * @param {ASTNode} node The node to check - * @param {ASTNode} parent Parent of the node - * @returns {void} - */ - function globalVarCheck(node, parent) { - if (!isVarOnTop(node, parent.body)) { - context.report({ node, messageId: "top" }); - } - } - - /** - * Checks whether variable is on top at functional block scope level - * @param {ASTNode} node The node to check - * @returns {void} - */ - function blockScopeVarCheck(node) { - const { parent } = node; - - if ( - parent.type === "BlockStatement" && - /Function/u.test(parent.parent.type) && - isVarOnTop(node, parent.body) - ) { - return; - } - - if ( - parent.type === "StaticBlock" && - isVarOnTop(node, parent.body) - ) { - return; - } - - context.report({ node, messageId: "top" }); - } - - //-------------------------------------------------------------------------- - // Public API - //-------------------------------------------------------------------------- - - return { - "VariableDeclaration[kind='var']"(node) { - if (node.parent.type === "ExportNamedDeclaration") { - globalVarCheck(node.parent, node.parent.parent); - } else if (node.parent.type === "Program") { - globalVarCheck(node, node.parent); - } else { - blockScopeVarCheck(node); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: "Require `var` declarations be placed at the top of their containing scope", + recommended: false, + url: "https://eslint.org/docs/latest/rules/vars-on-top" + }, + + schema: [], + messages: { + top: "All 'var' declarations must be at the top of the function scope." + } + }, + + create(context) { + + //-------------------------------------------------------------------------- + // Helpers + //-------------------------------------------------------------------------- + + /** + * Has AST suggesting a directive. + * @param {ASTNode} node any node + * @returns {boolean} whether the given node structurally represents a directive + */ + function looksLikeDirective(node) { + return node.type === "ExpressionStatement" && + node.expression.type === "Literal" && typeof node.expression.value === "string"; + } + + /** + * Check to see if its a ES6 import declaration + * @param {ASTNode} node any node + * @returns {boolean} whether the given node represents a import declaration + */ + function looksLikeImport(node) { + return node.type === "ImportDeclaration" || node.type === "ImportSpecifier" || + node.type === "ImportDefaultSpecifier" || node.type === "ImportNamespaceSpecifier"; + } + + /** + * Checks whether a given node is a variable declaration or not. + * @param {ASTNode} node any node + * @returns {boolean} `true` if the node is a variable declaration. + */ + function isVariableDeclaration(node) { + return ( + node.type === "VariableDeclaration" || + ( + node.type === "ExportNamedDeclaration" && + node.declaration && + node.declaration.type === "VariableDeclaration" + ) + ); + } + + /** + * Checks whether this variable is on top of the block body + * @param {ASTNode} node The node to check + * @param {ASTNode[]} statements collection of ASTNodes for the parent node block + * @returns {boolean} True if var is on top otherwise false + */ + function isVarOnTop(node, statements) { + const l = statements.length; + let i = 0; + + // Skip over directives and imports. Static blocks don't have either. + if (node.parent.type !== "StaticBlock") { + for (; i < l; ++i) { + if (!looksLikeDirective(statements[i]) && !looksLikeImport(statements[i])) { + break; + } + } + } + + for (; i < l; ++i) { + if (!isVariableDeclaration(statements[i])) { + return false; + } + if (statements[i] === node) { + return true; + } + } + + return false; + } + + /** + * Checks whether variable is on top at the global level + * @param {ASTNode} node The node to check + * @param {ASTNode} parent Parent of the node + * @returns {void} + */ + function globalVarCheck(node, parent) { + if (!isVarOnTop(node, parent.body)) { + context.report({ node, messageId: "top" }); + } + } + + /** + * Checks whether variable is on top at functional block scope level + * @param {ASTNode} node The node to check + * @returns {void} + */ + function blockScopeVarCheck(node) { + const { parent } = node; + + if ( + parent.type === "BlockStatement" && + /Function/u.test(parent.parent.type) && + isVarOnTop(node, parent.body) + ) { + return; + } + + if ( + parent.type === "StaticBlock" && + isVarOnTop(node, parent.body) + ) { + return; + } + + context.report({ node, messageId: "top" }); + } + + //-------------------------------------------------------------------------- + // Public API + //-------------------------------------------------------------------------- + + return { + "VariableDeclaration[kind='var']"(node) { + if (node.parent.type === "ExportNamedDeclaration") { + globalVarCheck(node.parent, node.parent.parent); + } else if (node.parent.type === "Program") { + globalVarCheck(node, node.parent); + } else { + blockScopeVarCheck(node); + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/wrap-iife.js b/node_modules/eslint/lib/rules/wrap-iife.js index 3ba53a151..518071067 100644 --- a/node_modules/eslint/lib/rules/wrap-iife.js +++ b/node_modules/eslint/lib/rules/wrap-iife.js @@ -24,215 +24,184 @@ const eslintUtils = require("@eslint-community/eslint-utils"); * @private */ function isCalleeOfNewExpression(node) { - const maybeCallee = - node.parent.type === "ChainExpression" ? node.parent : node; - - return ( - maybeCallee.parent.type === "NewExpression" && - maybeCallee.parent.callee === maybeCallee - ); + const maybeCallee = node.parent.type === "ChainExpression" + ? node.parent + : node; + + return ( + maybeCallee.parent.type === "NewExpression" && + maybeCallee.parent.callee === maybeCallee + ); } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "wrap-iife", - url: "https://eslint.style/rules/wrap-iife", - }, - }, - ], - }, - type: "layout", - - docs: { - description: - "Require parentheses around immediate `function` invocations", - recommended: false, - url: "https://eslint.org/docs/latest/rules/wrap-iife", - }, - - schema: [ - { - enum: ["outside", "inside", "any"], - }, - { - type: "object", - properties: { - functionPrototypeMethods: { - type: "boolean", - default: false, - }, - }, - additionalProperties: false, - }, - ], - - fixable: "code", - messages: { - wrapInvocation: - "Wrap an immediate function invocation in parentheses.", - wrapExpression: "Wrap only the function expression in parens.", - moveInvocation: - "Move the invocation into the parens that contain the function.", - }, - }, - - create(context) { - const style = context.options[0] || "outside"; - const includeFunctionPrototypeMethods = - context.options[1] && context.options[1].functionPrototypeMethods; - - const sourceCode = context.sourceCode; - - /** - * Check if the node is wrapped in any (). All parens count: grouping parens and parens for constructs such as if() - * @param {ASTNode} node node to evaluate - * @returns {boolean} True if it is wrapped in any parens - * @private - */ - function isWrappedInAnyParens(node) { - return astUtils.isParenthesised(sourceCode, node); - } - - /** - * Check if the node is wrapped in grouping (). Parens for constructs such as if() don't count - * @param {ASTNode} node node to evaluate - * @returns {boolean} True if it is wrapped in grouping parens - * @private - */ - function isWrappedInGroupingParens(node) { - return eslintUtils.isParenthesized(1, node, sourceCode); - } - - /** - * Get the function node from an IIFE - * @param {ASTNode} node node to evaluate - * @returns {ASTNode} node that is the function expression of the given IIFE, or null if none exist - */ - function getFunctionNodeFromIIFE(node) { - const callee = astUtils.skipChainExpression(node.callee); - - if (callee.type === "FunctionExpression") { - return callee; - } - - if ( - includeFunctionPrototypeMethods && - callee.type === "MemberExpression" && - callee.object.type === "FunctionExpression" && - (astUtils.getStaticPropertyName(callee) === "call" || - astUtils.getStaticPropertyName(callee) === "apply") - ) { - return callee.object; - } - - return null; - } - - return { - CallExpression(node) { - const innerNode = getFunctionNodeFromIIFE(node); - - if (!innerNode) { - return; - } - - const isCallExpressionWrapped = isWrappedInAnyParens(node), - isFunctionExpressionWrapped = - isWrappedInAnyParens(innerNode); - - if (!isCallExpressionWrapped && !isFunctionExpressionWrapped) { - context.report({ - node, - messageId: "wrapInvocation", - fix(fixer) { - const nodeToSurround = - style === "inside" ? innerNode : node; - - return fixer.replaceText( - nodeToSurround, - `(${sourceCode.getText(nodeToSurround)})`, - ); - }, - }); - } else if (style === "inside" && !isFunctionExpressionWrapped) { - context.report({ - node, - messageId: "wrapExpression", - fix(fixer) { - // The outer call expression will always be wrapped at this point. - - if ( - isWrappedInGroupingParens(node) && - !isCalleeOfNewExpression(node) - ) { - /* - * Parenthesize the function expression and remove unnecessary grouping parens around the call expression. - * Replace the range between the end of the function expression and the end of the call expression. - * for example, in `(function(foo) {}(bar))`, the range `(bar))` should get replaced with `)(bar)`. - */ - - const parenAfter = - sourceCode.getTokenAfter(node); - - return fixer.replaceTextRange( - [innerNode.range[1], parenAfter.range[1]], - `)${sourceCode.getText().slice(innerNode.range[1], parenAfter.range[0])}`, - ); - } - - /* - * Call expression is wrapped in mandatory parens such as if(), or in necessary grouping parens. - * These parens cannot be removed, so just parenthesize the function expression. - */ - - return fixer.replaceText( - innerNode, - `(${sourceCode.getText(innerNode)})`, - ); - }, - }); - } else if (style === "outside" && !isCallExpressionWrapped) { - context.report({ - node, - messageId: "moveInvocation", - fix(fixer) { - /* - * The inner function expression will always be wrapped at this point. - * It's only necessary to replace the range between the end of the function expression - * and the call expression. For example, in `(function(foo) {})(bar)`, the range `)(bar)` - * should get replaced with `(bar))`. - */ - const parenAfter = - sourceCode.getTokenAfter(innerNode); - - return fixer.replaceTextRange( - [parenAfter.range[0], node.range[1]], - `${sourceCode.getText().slice(parenAfter.range[1], node.range[1])})`, - ); - }, - }); - } - }, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Require parentheses around immediate `function` invocations", + recommended: false, + url: "https://eslint.org/docs/latest/rules/wrap-iife" + }, + + schema: [ + { + enum: ["outside", "inside", "any"] + }, + { + type: "object", + properties: { + functionPrototypeMethods: { + type: "boolean", + default: false + } + }, + additionalProperties: false + } + ], + + fixable: "code", + messages: { + wrapInvocation: "Wrap an immediate function invocation in parentheses.", + wrapExpression: "Wrap only the function expression in parens.", + moveInvocation: "Move the invocation into the parens that contain the function." + } + }, + + create(context) { + + const style = context.options[0] || "outside"; + const includeFunctionPrototypeMethods = context.options[1] && context.options[1].functionPrototypeMethods; + + const sourceCode = context.sourceCode; + + /** + * Check if the node is wrapped in any (). All parens count: grouping parens and parens for constructs such as if() + * @param {ASTNode} node node to evaluate + * @returns {boolean} True if it is wrapped in any parens + * @private + */ + function isWrappedInAnyParens(node) { + return astUtils.isParenthesised(sourceCode, node); + } + + /** + * Check if the node is wrapped in grouping (). Parens for constructs such as if() don't count + * @param {ASTNode} node node to evaluate + * @returns {boolean} True if it is wrapped in grouping parens + * @private + */ + function isWrappedInGroupingParens(node) { + return eslintUtils.isParenthesized(1, node, sourceCode); + } + + /** + * Get the function node from an IIFE + * @param {ASTNode} node node to evaluate + * @returns {ASTNode} node that is the function expression of the given IIFE, or null if none exist + */ + function getFunctionNodeFromIIFE(node) { + const callee = astUtils.skipChainExpression(node.callee); + + if (callee.type === "FunctionExpression") { + return callee; + } + + if (includeFunctionPrototypeMethods && + callee.type === "MemberExpression" && + callee.object.type === "FunctionExpression" && + (astUtils.getStaticPropertyName(callee) === "call" || astUtils.getStaticPropertyName(callee) === "apply") + ) { + return callee.object; + } + + return null; + } + + + return { + CallExpression(node) { + const innerNode = getFunctionNodeFromIIFE(node); + + if (!innerNode) { + return; + } + + const isCallExpressionWrapped = isWrappedInAnyParens(node), + isFunctionExpressionWrapped = isWrappedInAnyParens(innerNode); + + if (!isCallExpressionWrapped && !isFunctionExpressionWrapped) { + context.report({ + node, + messageId: "wrapInvocation", + fix(fixer) { + const nodeToSurround = style === "inside" ? innerNode : node; + + return fixer.replaceText(nodeToSurround, `(${sourceCode.getText(nodeToSurround)})`); + } + }); + } else if (style === "inside" && !isFunctionExpressionWrapped) { + context.report({ + node, + messageId: "wrapExpression", + fix(fixer) { + + // The outer call expression will always be wrapped at this point. + + if (isWrappedInGroupingParens(node) && !isCalleeOfNewExpression(node)) { + + /* + * Parenthesize the function expression and remove unnecessary grouping parens around the call expression. + * Replace the range between the end of the function expression and the end of the call expression. + * for example, in `(function(foo) {}(bar))`, the range `(bar))` should get replaced with `)(bar)`. + */ + + const parenAfter = sourceCode.getTokenAfter(node); + + return fixer.replaceTextRange( + [innerNode.range[1], parenAfter.range[1]], + `)${sourceCode.getText().slice(innerNode.range[1], parenAfter.range[0])}` + ); + } + + /* + * Call expression is wrapped in mandatory parens such as if(), or in necessary grouping parens. + * These parens cannot be removed, so just parenthesize the function expression. + */ + + return fixer.replaceText(innerNode, `(${sourceCode.getText(innerNode)})`); + } + }); + } else if (style === "outside" && !isCallExpressionWrapped) { + context.report({ + node, + messageId: "moveInvocation", + fix(fixer) { + + /* + * The inner function expression will always be wrapped at this point. + * It's only necessary to replace the range between the end of the function expression + * and the call expression. For example, in `(function(foo) {})(bar)`, the range `)(bar)` + * should get replaced with `(bar))`. + */ + const parenAfter = sourceCode.getTokenAfter(innerNode); + + return fixer.replaceTextRange( + [parenAfter.range[0], node.range[1]], + `${sourceCode.getText().slice(parenAfter.range[1], node.range[1])})` + ); + } + }); + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/wrap-regex.js b/node_modules/eslint/lib/rules/wrap-regex.js index def392a4c..9e2808d60 100644 --- a/node_modules/eslint/lib/rules/wrap-regex.js +++ b/node_modules/eslint/lib/rules/wrap-regex.js @@ -10,82 +10,52 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "wrap-regex", - url: "https://eslint.style/rules/wrap-regex", - }, - }, - ], - }, - type: "layout", - - docs: { - description: "Require parenthesis around regex literals", - recommended: false, - url: "https://eslint.org/docs/latest/rules/wrap-regex", - }, - - schema: [], - fixable: "code", - - messages: { - requireParens: - "Wrap the regexp literal in parens to disambiguate the slash.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - return { - Literal(node) { - const token = sourceCode.getFirstToken(node), - nodeType = token.type; - - if (nodeType === "RegularExpression") { - const beforeToken = sourceCode.getTokenBefore(node); - const afterToken = sourceCode.getTokenAfter(node); - const { parent } = node; - - if ( - parent.type === "MemberExpression" && - parent.object === node && - !( - beforeToken && - beforeToken.value === "(" && - afterToken && - afterToken.value === ")" - ) - ) { - context.report({ - node, - messageId: "requireParens", - fix: fixer => - fixer.replaceText( - node, - `(${sourceCode.getText(node)})`, - ), - }); - } - } - }, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Require parenthesis around regex literals", + recommended: false, + url: "https://eslint.org/docs/latest/rules/wrap-regex" + }, + + schema: [], + fixable: "code", + + messages: { + requireParens: "Wrap the regexp literal in parens to disambiguate the slash." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + + return { + + Literal(node) { + const token = sourceCode.getFirstToken(node), + nodeType = token.type; + + if (nodeType === "RegularExpression") { + const beforeToken = sourceCode.getTokenBefore(node); + const afterToken = sourceCode.getTokenAfter(node); + const { parent } = node; + + if (parent.type === "MemberExpression" && parent.object === node && + !(beforeToken && beforeToken.value === "(" && afterToken && afterToken.value === ")")) { + context.report({ + node, + messageId: "requireParens", + fix: fixer => fixer.replaceText(node, `(${sourceCode.getText(node)})`) + }); + } + } + } + }; + + } }; diff --git a/node_modules/eslint/lib/rules/yield-star-spacing.js b/node_modules/eslint/lib/rules/yield-star-spacing.js index 05666e214..9a67b78d2 100644 --- a/node_modules/eslint/lib/rules/yield-star-spacing.js +++ b/node_modules/eslint/lib/rules/yield-star-spacing.js @@ -10,150 +10,121 @@ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - deprecated: { - message: "Formatting rules are being moved out of ESLint core.", - url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/", - deprecatedSince: "8.53.0", - availableUntil: "11.0.0", - replacedBy: [ - { - message: - "ESLint Stylistic now maintains deprecated stylistic core rules.", - url: "https://eslint.style/guide/migration", - plugin: { - name: "@stylistic/eslint-plugin", - url: "https://eslint.style", - }, - rule: { - name: "yield-star-spacing", - url: "https://eslint.style/rules/yield-star-spacing", - }, - }, - ], - }, - type: "layout", - - docs: { - description: - "Require or disallow spacing around the `*` in `yield*` expressions", - recommended: false, - url: "https://eslint.org/docs/latest/rules/yield-star-spacing", - }, - - fixable: "whitespace", - - schema: [ - { - oneOf: [ - { - enum: ["before", "after", "both", "neither"], - }, - { - type: "object", - properties: { - before: { type: "boolean" }, - after: { type: "boolean" }, - }, - additionalProperties: false, - }, - ], - }, - ], - messages: { - missingBefore: "Missing space before *.", - missingAfter: "Missing space after *.", - unexpectedBefore: "Unexpected space before *.", - unexpectedAfter: "Unexpected space after *.", - }, - }, - - create(context) { - const sourceCode = context.sourceCode; - - const mode = (function (option) { - if (!option || typeof option === "string") { - return { - before: { before: true, after: false }, - after: { before: false, after: true }, - both: { before: true, after: true }, - neither: { before: false, after: false }, - }[option || "after"]; - } - return option; - })(context.options[0]); - - /** - * Checks the spacing between two tokens before or after the star token. - * @param {string} side Either "before" or "after". - * @param {Token} leftToken `function` keyword token if side is "before", or - * star token if side is "after". - * @param {Token} rightToken Star token if side is "before", or identifier - * token if side is "after". - * @returns {void} - */ - function checkSpacing(side, leftToken, rightToken) { - if ( - sourceCode.isSpaceBetweenTokens(leftToken, rightToken) !== - mode[side] - ) { - const after = leftToken.value === "*"; - const spaceRequired = mode[side]; - const node = after ? leftToken : rightToken; - let messageId; - - if (spaceRequired) { - messageId = - side === "before" ? "missingBefore" : "missingAfter"; - } else { - messageId = - side === "before" - ? "unexpectedBefore" - : "unexpectedAfter"; - } - - context.report({ - node, - messageId, - fix(fixer) { - if (spaceRequired) { - if (after) { - return fixer.insertTextAfter(node, " "); - } - return fixer.insertTextBefore(node, " "); - } - return fixer.removeRange([ - leftToken.range[1], - rightToken.range[0], - ]); - }, - }); - } - } - - /** - * Enforces the spacing around the star if node is a yield* expression. - * @param {ASTNode} node A yield expression node. - * @returns {void} - */ - function checkExpression(node) { - if (!node.delegate) { - return; - } - - const tokens = sourceCode.getFirstTokens(node, 3); - const yieldToken = tokens[0]; - const starToken = tokens[1]; - const nextToken = tokens[2]; - - checkSpacing("before", yieldToken, starToken); - checkSpacing("after", starToken, nextToken); - } - - return { - YieldExpression: checkExpression, - }; - }, + meta: { + deprecated: true, + replacedBy: [], + type: "layout", + + docs: { + description: "Require or disallow spacing around the `*` in `yield*` expressions", + recommended: false, + url: "https://eslint.org/docs/latest/rules/yield-star-spacing" + }, + + fixable: "whitespace", + + schema: [ + { + oneOf: [ + { + enum: ["before", "after", "both", "neither"] + }, + { + type: "object", + properties: { + before: { type: "boolean" }, + after: { type: "boolean" } + }, + additionalProperties: false + } + ] + } + ], + messages: { + missingBefore: "Missing space before *.", + missingAfter: "Missing space after *.", + unexpectedBefore: "Unexpected space before *.", + unexpectedAfter: "Unexpected space after *." + } + }, + + create(context) { + const sourceCode = context.sourceCode; + + const mode = (function(option) { + if (!option || typeof option === "string") { + return { + before: { before: true, after: false }, + after: { before: false, after: true }, + both: { before: true, after: true }, + neither: { before: false, after: false } + }[option || "after"]; + } + return option; + }(context.options[0])); + + /** + * Checks the spacing between two tokens before or after the star token. + * @param {string} side Either "before" or "after". + * @param {Token} leftToken `function` keyword token if side is "before", or + * star token if side is "after". + * @param {Token} rightToken Star token if side is "before", or identifier + * token if side is "after". + * @returns {void} + */ + function checkSpacing(side, leftToken, rightToken) { + if (sourceCode.isSpaceBetweenTokens(leftToken, rightToken) !== mode[side]) { + const after = leftToken.value === "*"; + const spaceRequired = mode[side]; + const node = after ? leftToken : rightToken; + let messageId = ""; + + if (spaceRequired) { + messageId = side === "before" ? "missingBefore" : "missingAfter"; + } else { + messageId = side === "before" ? "unexpectedBefore" : "unexpectedAfter"; + } + + context.report({ + node, + messageId, + fix(fixer) { + if (spaceRequired) { + if (after) { + return fixer.insertTextAfter(node, " "); + } + return fixer.insertTextBefore(node, " "); + } + return fixer.removeRange([leftToken.range[1], rightToken.range[0]]); + } + }); + } + } + + /** + * Enforces the spacing around the star if node is a yield* expression. + * @param {ASTNode} node A yield expression node. + * @returns {void} + */ + function checkExpression(node) { + if (!node.delegate) { + return; + } + + const tokens = sourceCode.getFirstTokens(node, 3); + const yieldToken = tokens[0]; + const starToken = tokens[1]; + const nextToken = tokens[2]; + + checkSpacing("before", yieldToken, starToken); + checkSpacing("after", starToken, nextToken); + } + + return { + YieldExpression: checkExpression + }; + + } }; diff --git a/node_modules/eslint/lib/rules/yoda.js b/node_modules/eslint/lib/rules/yoda.js index 1ee74712d..af8f52518 100644 --- a/node_modules/eslint/lib/rules/yoda.js +++ b/node_modules/eslint/lib/rules/yoda.js @@ -20,7 +20,7 @@ const astUtils = require("./utils/ast-utils"); * @returns {boolean} Whether or not it is a comparison operator. */ function isComparisonOperator(operator) { - return /^(?:==|===|!=|!==|<|>|<=|>=)$/u.test(operator); + return /^(==|===|!=|!==|<|>|<=|>=)$/u.test(operator); } /** @@ -29,7 +29,7 @@ function isComparisonOperator(operator) { * @returns {boolean} Whether or not it is an equality operator. */ function isEqualityOperator(operator) { - return /^(?:==|===)$/u.test(operator); + return /^(==|===)$/u.test(operator); } /** @@ -39,7 +39,7 @@ function isEqualityOperator(operator) { * @returns {boolean} Whether the operator is used in range tests. */ function isRangeTestOperator(operator) { - return ["<", "<="].includes(operator); + return ["<", "<="].includes(operator); } /** @@ -50,12 +50,12 @@ function isRangeTestOperator(operator) { * real literal and should be treated as such. */ function isNegativeNumericLiteral(node) { - return ( - node.type === "UnaryExpression" && - node.operator === "-" && - node.prefix && - astUtils.isNumericLiteral(node.argument) - ); + return ( + node.type === "UnaryExpression" && + node.operator === "-" && + node.prefix && + astUtils.isNumericLiteral(node.argument) + ); } /** @@ -64,9 +64,7 @@ function isNegativeNumericLiteral(node) { * @returns {boolean} True if the node should be treated as a single Literal node. */ function looksLikeLiteral(node) { - return ( - isNegativeNumericLiteral(node) || astUtils.isStaticTemplateLiteral(node) - ); + return isNegativeNumericLiteral(node) || astUtils.isStaticTemplateLiteral(node); } /** @@ -81,282 +79,275 @@ function looksLikeLiteral(node) { * 4. Otherwise `null`. */ function getNormalizedLiteral(node) { - if (node.type === "Literal") { - return node; - } - - if (isNegativeNumericLiteral(node)) { - return { - type: "Literal", - value: -node.argument.value, - raw: `-${node.argument.value}`, - }; - } - - if (astUtils.isStaticTemplateLiteral(node)) { - return { - type: "Literal", - value: node.quasis[0].value.cooked, - raw: node.quasis[0].value.raw, - }; - } - - return null; + if (node.type === "Literal") { + return node; + } + + if (isNegativeNumericLiteral(node)) { + return { + type: "Literal", + value: -node.argument.value, + raw: `-${node.argument.value}` + }; + } + + if (astUtils.isStaticTemplateLiteral(node)) { + return { + type: "Literal", + value: node.quasis[0].value.cooked, + raw: node.quasis[0].value.raw + }; + } + + return null; } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ -/** @type {import('../types').Rule.RuleModule} */ +/** @type {import('../shared/types').Rule} */ module.exports = { - meta: { - type: "suggestion", - - defaultOptions: [ - "never", - { - exceptRange: false, - onlyEquality: false, - }, - ], - - docs: { - description: 'Require or disallow "Yoda" conditions', - recommended: false, - frozen: true, - url: "https://eslint.org/docs/latest/rules/yoda", - }, - - schema: [ - { - enum: ["always", "never"], - }, - { - type: "object", - properties: { - exceptRange: { - type: "boolean", - }, - onlyEquality: { - type: "boolean", - }, - }, - additionalProperties: false, - }, - ], - - fixable: "code", - messages: { - expected: - "Expected literal to be on the {{expectedSide}} side of {{operator}}.", - }, - }, - - create(context) { - const [when, { exceptRange, onlyEquality }] = context.options; - const always = when === "always"; - const sourceCode = context.sourceCode; - - /** - * Determines whether node represents a range test. - * A range test is a "between" test like `(0 <= x && x < 1)` or an "outside" - * test like `(x < 0 || 1 <= x)`. It must be wrapped in parentheses, and - * both operators must be `<` or `<=`. Finally, the literal on the left side - * must be less than or equal to the literal on the right side so that the - * test makes any sense. - * @param {ASTNode} node LogicalExpression node to test. - * @returns {boolean} Whether node is a range test. - */ - function isRangeTest(node) { - const left = node.left, - right = node.right; - - /** - * Determines whether node is of the form `0 <= x && x < 1`. - * @returns {boolean} Whether node is a "between" range test. - */ - function isBetweenTest() { - if ( - node.operator === "&&" && - astUtils.isSameReference(left.right, right.left) - ) { - const leftLiteral = getNormalizedLiteral(left.left); - const rightLiteral = getNormalizedLiteral(right.right); - - if (leftLiteral === null && rightLiteral === null) { - return false; - } - - if (rightLiteral === null || leftLiteral === null) { - return true; - } - - if (leftLiteral.value <= rightLiteral.value) { - return true; - } - } - return false; - } - - /** - * Determines whether node is of the form `x < 0 || 1 <= x`. - * @returns {boolean} Whether node is an "outside" range test. - */ - function isOutsideTest() { - if ( - node.operator === "||" && - astUtils.isSameReference(left.left, right.right) - ) { - const leftLiteral = getNormalizedLiteral(left.right); - const rightLiteral = getNormalizedLiteral(right.left); - - if (leftLiteral === null && rightLiteral === null) { - return false; - } - - if (rightLiteral === null || leftLiteral === null) { - return true; - } - - if (leftLiteral.value <= rightLiteral.value) { - return true; - } - } - - return false; - } - - /** - * Determines whether node is wrapped in parentheses. - * @returns {boolean} Whether node is preceded immediately by an open - * paren token and followed immediately by a close - * paren token. - */ - function isParenWrapped() { - return astUtils.isParenthesised(sourceCode, node); - } - - return ( - node.type === "LogicalExpression" && - left.type === "BinaryExpression" && - right.type === "BinaryExpression" && - isRangeTestOperator(left.operator) && - isRangeTestOperator(right.operator) && - (isBetweenTest() || isOutsideTest()) && - isParenWrapped() - ); - } - - const OPERATOR_FLIP_MAP = { - "===": "===", - "!==": "!==", - "==": "==", - "!=": "!=", - "<": ">", - ">": "<", - "<=": ">=", - ">=": "<=", - }; - - /** - * Returns a string representation of a BinaryExpression node with its sides/operator flipped around. - * @param {ASTNode} node The BinaryExpression node - * @returns {string} A string representation of the node with the sides and operator flipped - */ - function getFlippedString(node) { - const operatorToken = sourceCode.getFirstTokenBetween( - node.left, - node.right, - token => token.value === node.operator, - ); - const lastLeftToken = sourceCode.getTokenBefore(operatorToken); - const firstRightToken = sourceCode.getTokenAfter(operatorToken); - - const source = sourceCode.getText(); - - const leftText = source.slice( - node.range[0], - lastLeftToken.range[1], - ); - const textBeforeOperator = source.slice( - lastLeftToken.range[1], - operatorToken.range[0], - ); - const textAfterOperator = source.slice( - operatorToken.range[1], - firstRightToken.range[0], - ); - const rightText = source.slice( - firstRightToken.range[0], - node.range[1], - ); - - const tokenBefore = sourceCode.getTokenBefore(node); - const tokenAfter = sourceCode.getTokenAfter(node); - let prefix = ""; - let suffix = ""; - - if ( - tokenBefore && - tokenBefore.range[1] === node.range[0] && - !astUtils.canTokensBeAdjacent(tokenBefore, firstRightToken) - ) { - prefix = " "; - } - - if ( - tokenAfter && - node.range[1] === tokenAfter.range[0] && - !astUtils.canTokensBeAdjacent(lastLeftToken, tokenAfter) - ) { - suffix = " "; - } - - return ( - prefix + - rightText + - textBeforeOperator + - OPERATOR_FLIP_MAP[operatorToken.value] + - textAfterOperator + - leftText + - suffix - ); - } - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - return { - BinaryExpression(node) { - const expectedLiteral = always ? node.left : node.right; - const expectedNonLiteral = always ? node.right : node.left; - - // If `expectedLiteral` is not a literal, and `expectedNonLiteral` is a literal, raise an error. - if ( - (expectedNonLiteral.type === "Literal" || - looksLikeLiteral(expectedNonLiteral)) && - !( - expectedLiteral.type === "Literal" || - looksLikeLiteral(expectedLiteral) - ) && - !(!isEqualityOperator(node.operator) && onlyEquality) && - isComparisonOperator(node.operator) && - !(exceptRange && isRangeTest(node.parent)) - ) { - context.report({ - node, - messageId: "expected", - data: { - operator: node.operator, - expectedSide: always ? "left" : "right", - }, - fix: fixer => - fixer.replaceText(node, getFlippedString(node)), - }); - } - }, - }; - }, + meta: { + type: "suggestion", + + docs: { + description: 'Require or disallow "Yoda" conditions', + recommended: false, + url: "https://eslint.org/docs/latest/rules/yoda" + }, + + schema: [ + { + enum: ["always", "never"] + }, + { + type: "object", + properties: { + exceptRange: { + type: "boolean", + default: false + }, + onlyEquality: { + type: "boolean", + default: false + } + }, + additionalProperties: false + } + ], + + fixable: "code", + messages: { + expected: + "Expected literal to be on the {{expectedSide}} side of {{operator}}." + } + }, + + create(context) { + + // Default to "never" (!always) if no option + const always = context.options[0] === "always"; + const exceptRange = + context.options[1] && context.options[1].exceptRange; + const onlyEquality = + context.options[1] && context.options[1].onlyEquality; + + const sourceCode = context.sourceCode; + + /** + * Determines whether node represents a range test. + * A range test is a "between" test like `(0 <= x && x < 1)` or an "outside" + * test like `(x < 0 || 1 <= x)`. It must be wrapped in parentheses, and + * both operators must be `<` or `<=`. Finally, the literal on the left side + * must be less than or equal to the literal on the right side so that the + * test makes any sense. + * @param {ASTNode} node LogicalExpression node to test. + * @returns {boolean} Whether node is a range test. + */ + function isRangeTest(node) { + const left = node.left, + right = node.right; + + /** + * Determines whether node is of the form `0 <= x && x < 1`. + * @returns {boolean} Whether node is a "between" range test. + */ + function isBetweenTest() { + if (node.operator === "&&" && astUtils.isSameReference(left.right, right.left)) { + const leftLiteral = getNormalizedLiteral(left.left); + const rightLiteral = getNormalizedLiteral(right.right); + + if (leftLiteral === null && rightLiteral === null) { + return false; + } + + if (rightLiteral === null || leftLiteral === null) { + return true; + } + + if (leftLiteral.value <= rightLiteral.value) { + return true; + } + } + return false; + } + + /** + * Determines whether node is of the form `x < 0 || 1 <= x`. + * @returns {boolean} Whether node is an "outside" range test. + */ + function isOutsideTest() { + if (node.operator === "||" && astUtils.isSameReference(left.left, right.right)) { + const leftLiteral = getNormalizedLiteral(left.right); + const rightLiteral = getNormalizedLiteral(right.left); + + if (leftLiteral === null && rightLiteral === null) { + return false; + } + + if (rightLiteral === null || leftLiteral === null) { + return true; + } + + if (leftLiteral.value <= rightLiteral.value) { + return true; + } + } + + return false; + } + + /** + * Determines whether node is wrapped in parentheses. + * @returns {boolean} Whether node is preceded immediately by an open + * paren token and followed immediately by a close + * paren token. + */ + function isParenWrapped() { + return astUtils.isParenthesised(sourceCode, node); + } + + return ( + node.type === "LogicalExpression" && + left.type === "BinaryExpression" && + right.type === "BinaryExpression" && + isRangeTestOperator(left.operator) && + isRangeTestOperator(right.operator) && + (isBetweenTest() || isOutsideTest()) && + isParenWrapped() + ); + } + + const OPERATOR_FLIP_MAP = { + "===": "===", + "!==": "!==", + "==": "==", + "!=": "!=", + "<": ">", + ">": "<", + "<=": ">=", + ">=": "<=" + }; + + /** + * Returns a string representation of a BinaryExpression node with its sides/operator flipped around. + * @param {ASTNode} node The BinaryExpression node + * @returns {string} A string representation of the node with the sides and operator flipped + */ + function getFlippedString(node) { + const operatorToken = sourceCode.getFirstTokenBetween( + node.left, + node.right, + token => token.value === node.operator + ); + const lastLeftToken = sourceCode.getTokenBefore(operatorToken); + const firstRightToken = sourceCode.getTokenAfter(operatorToken); + + const source = sourceCode.getText(); + + const leftText = source.slice( + node.range[0], + lastLeftToken.range[1] + ); + const textBeforeOperator = source.slice( + lastLeftToken.range[1], + operatorToken.range[0] + ); + const textAfterOperator = source.slice( + operatorToken.range[1], + firstRightToken.range[0] + ); + const rightText = source.slice( + firstRightToken.range[0], + node.range[1] + ); + + const tokenBefore = sourceCode.getTokenBefore(node); + const tokenAfter = sourceCode.getTokenAfter(node); + let prefix = ""; + let suffix = ""; + + if ( + tokenBefore && + tokenBefore.range[1] === node.range[0] && + !astUtils.canTokensBeAdjacent(tokenBefore, firstRightToken) + ) { + prefix = " "; + } + + if ( + tokenAfter && + node.range[1] === tokenAfter.range[0] && + !astUtils.canTokensBeAdjacent(lastLeftToken, tokenAfter) + ) { + suffix = " "; + } + + return ( + prefix + + rightText + + textBeforeOperator + + OPERATOR_FLIP_MAP[operatorToken.value] + + textAfterOperator + + leftText + + suffix + ); + } + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + return { + BinaryExpression(node) { + const expectedLiteral = always ? node.left : node.right; + const expectedNonLiteral = always ? node.right : node.left; + + // If `expectedLiteral` is not a literal, and `expectedNonLiteral` is a literal, raise an error. + if ( + (expectedNonLiteral.type === "Literal" || + looksLikeLiteral(expectedNonLiteral)) && + !( + expectedLiteral.type === "Literal" || + looksLikeLiteral(expectedLiteral) + ) && + !(!isEqualityOperator(node.operator) && onlyEquality) && + isComparisonOperator(node.operator) && + !(exceptRange && isRangeTest(node.parent)) + ) { + context.report({ + node, + messageId: "expected", + data: { + operator: node.operator, + expectedSide: always ? "left" : "right" + }, + fix: fixer => + fixer.replaceText(node, getFlippedString(node)) + }); + } + } + }; + } }; diff --git a/node_modules/eslint/lib/services/parser-service.js b/node_modules/eslint/lib/services/parser-service.js deleted file mode 100644 index 580bb69d5..000000000 --- a/node_modules/eslint/lib/services/parser-service.js +++ /dev/null @@ -1,65 +0,0 @@ -/** - * @fileoverview ESLint Parser - * @author Nicholas C. Zakas - */ -/* eslint class-methods-use-this: off -- Anticipate future constructor arguments. */ - -"use strict"; - -//----------------------------------------------------------------------------- -// Types -//----------------------------------------------------------------------------- - -/** @typedef {import("../linter/vfile.js").VFile} VFile */ -/** @typedef {import("@eslint/core").Language} Language */ -/** @typedef {import("@eslint/core").LanguageOptions} LanguageOptions */ - -//----------------------------------------------------------------------------- -// Exports -//----------------------------------------------------------------------------- - -/** - * The parser for ESLint. - */ -class ParserService { - /** - * Parses the given file synchronously. - * @param {VFile} file The file to parse. - * @param {{language:Language,languageOptions:LanguageOptions}} config The configuration to use. - * @returns {Object} An object with the parsed source code or errors. - * @throws {Error} If the parser returns a promise. - */ - parseSync(file, config) { - const { language, languageOptions } = config; - const result = language.parse(file, { languageOptions }); - - if (typeof result.then === "function") { - throw new Error("Unsupported: Language parser returned a promise."); - } - - if (result.ok) { - return { - ok: true, - sourceCode: language.createSourceCode(file, result, { - languageOptions, - }), - }; - } - - // if we made it to here there was an error - return { - ok: false, - errors: result.errors.map(error => ({ - ruleId: null, - nodeType: null, - fatal: true, - severity: 2, - message: `Parsing error: ${error.message}`, - line: error.line, - column: error.column, - })), - }; - } -} - -module.exports = { ParserService }; diff --git a/node_modules/eslint/lib/services/processor-service.js b/node_modules/eslint/lib/services/processor-service.js deleted file mode 100644 index a5447a80a..000000000 --- a/node_modules/eslint/lib/services/processor-service.js +++ /dev/null @@ -1,101 +0,0 @@ -/** - * @fileoverview ESLint Processor Service - * @author Nicholas C. Zakas - */ -/* eslint class-methods-use-this: off -- Anticipate future constructor arguments. */ - -"use strict"; - -//----------------------------------------------------------------------------- -// Requirements -//----------------------------------------------------------------------------- - -const path = require("node:path"); -const { VFile } = require("../linter/vfile.js"); - -//----------------------------------------------------------------------------- -// Types -//----------------------------------------------------------------------------- - -/** @typedef {import("../types").Linter.LintMessage} LintMessage */ -/** @typedef {import("../linter/vfile.js").VFile} VFile */ -/** @typedef {import("@eslint/core").Language} Language */ -/** @typedef {import("eslint").Linter.Processor} Processor */ - -//----------------------------------------------------------------------------- -// Exports -//----------------------------------------------------------------------------- - -/** - * The service that applies processors to files. - */ -class ProcessorService { - /** - * Preprocesses the given file synchronously. - * @param {VFile} file The file to preprocess. - * @param {{processor:Processor}} config The configuration to use. - * @returns {{ok:boolean, files?: Array, errors?: Array}} An array of preprocessed files or errors. - * @throws {Error} If the preprocessor returns a promise. - */ - preprocessSync(file, config) { - const { processor } = config; - let blocks; - - try { - blocks = processor.preprocess(file.rawBody, file.path); - } catch (ex) { - // If the message includes a leading line number, strip it: - const message = `Preprocessing error: ${ex.message.replace(/^line \d+:/iu, "").trim()}`; - - return { - ok: false, - errors: [ - { - ruleId: null, - fatal: true, - severity: 2, - message, - line: ex.lineNumber, - column: ex.column, - nodeType: null, - }, - ], - }; - } - - if (typeof blocks.then === "function") { - throw new Error("Unsupported: Preprocessor returned a promise."); - } - - return { - ok: true, - files: blocks.map((block, i) => { - // Legacy behavior: return the block as a string - if (typeof block === "string") { - return block; - } - - const filePath = path.join(file.path, `${i}_${block.filename}`); - - return new VFile(filePath, block.text, { - physicalPath: file.physicalPath, - }); - }), - }; - } - - /** - * Postprocesses the given messages synchronously. - * @param {VFile} file The file to postprocess. - * @param {LintMessage[][]} messages The messages to postprocess. - * @param {{processor:Processor}} config The configuration to use. - * @returns {LintMessage[]} The postprocessed messages. - */ - postprocessSync(file, messages, config) { - const { processor } = config; - - return processor.postprocess(messages, file.path); - } -} - -module.exports = { ProcessorService }; diff --git a/node_modules/eslint/lib/services/suppressions-service.js b/node_modules/eslint/lib/services/suppressions-service.js deleted file mode 100644 index c02e59f3e..000000000 --- a/node_modules/eslint/lib/services/suppressions-service.js +++ /dev/null @@ -1,302 +0,0 @@ -/** - * @fileoverview Manages the suppressed violations. - * @author Iacovos Constantinou - */ - -"use strict"; - -//----------------------------------------------------------------------------- -// Requirements -//----------------------------------------------------------------------------- - -const fs = require("node:fs"); -const path = require("node:path"); -const { calculateStatsPerFile } = require("../eslint/eslint-helpers"); -const stringify = require("json-stable-stringify-without-jsonify"); - -//------------------------------------------------------------------------------ -// Typedefs -//------------------------------------------------------------------------------ - -// For VSCode IntelliSense -/** @typedef {import("../types").Linter.LintMessage} LintMessage */ -/** @typedef {import("../types").ESLint.LintResult} LintResult */ -/** @typedef {Record>} SuppressedViolations */ - -//----------------------------------------------------------------------------- -// Exports -//----------------------------------------------------------------------------- - -/** - * Manages the suppressed violations. - */ -class SuppressionsService { - filePath = ""; - cwd = ""; - - /** - * Creates a new instance of SuppressionsService. - * @param {Object} options The options. - * @param {string} [options.filePath] The location of the suppressions file. - * @param {string} [options.cwd] The current working directory. - */ - constructor({ filePath, cwd }) { - this.filePath = filePath; - this.cwd = cwd; - } - - /** - * Updates the suppressions file based on the current violations and the provided rules. - * If no rules are provided, all violations are suppressed. - * @param {LintResult[]|undefined} results The lint results. - * @param {string[]|undefined} rules The rules to suppress. - * @returns {Promise} - */ - async suppress(results, rules) { - const suppressions = await this.load(); - - for (const result of results) { - const relativeFilePath = this.getRelativeFilePath(result.filePath); - const violationsByRule = SuppressionsService.countViolationsByRule( - result.messages, - ); - - for (const ruleId in violationsByRule) { - if (rules && !rules.includes(ruleId)) { - continue; - } - - suppressions[relativeFilePath] ??= {}; - suppressions[relativeFilePath][ruleId] = - violationsByRule[ruleId]; - } - } - - return this.save(suppressions); - } - - /** - * Removes old, unused suppressions for violations that do not occur anymore. - * @param {LintResult[]} results The lint results. - * @returns {Promise} No return value. - */ - async prune(results) { - const suppressions = await this.load(); - const { unused } = this.applySuppressions(results, suppressions); - - for (const file in unused) { - if (!suppressions[file]) { - continue; - } - - for (const rule in unused[file]) { - if (!suppressions[file][rule]) { - continue; - } - - const suppressionsCount = suppressions[file][rule].count; - const violationsCount = unused[file][rule].count; - - if (suppressionsCount === violationsCount) { - // Remove unused rules - delete suppressions[file][rule]; - } else { - // Update the count to match the new number of violations - suppressions[file][rule].count -= violationsCount; - } - } - - // Cleanup files with no rules - if (Object.keys(suppressions[file]).length === 0) { - delete suppressions[file]; - } - } - - for (const file of Object.keys(suppressions)) { - const absolutePath = path.resolve(this.cwd, file); - - if (!fs.existsSync(absolutePath)) { - delete suppressions[file]; - } - } - - return this.save(suppressions); - } - - /** - * Checks the provided suppressions against the lint results. - * - * For each file, counts the number of violations per rule. - * For each rule in each file, compares the number of violations against the counter from the suppressions file. - * If the number of violations is less or equal to the counter, messages are moved to `LintResult#suppressedMessages` and ignored. - * Otherwise, all violations are reported as usual. - * @param {LintResult[]} results The lint results. - * @param {SuppressedViolations} suppressions The suppressions. - * @returns {{ - * results: LintResult[], - * unused: SuppressedViolations - * }} The updated results and the unused suppressions. - */ - applySuppressions(results, suppressions) { - /** - * We copy the results to avoid modifying the original objects - * We remove only result messages that are matched and hence suppressed - * We leave the rest untouched to minimize the risk of losing parts of the original data - */ - const filtered = structuredClone(results); - const unused = {}; - - for (const result of filtered) { - const relativeFilePath = this.getRelativeFilePath(result.filePath); - - if (!suppressions[relativeFilePath]) { - continue; - } - - const violationsByRule = SuppressionsService.countViolationsByRule( - result.messages, - ); - let wasSuppressed = false; - - for (const ruleId in violationsByRule) { - if (!suppressions[relativeFilePath][ruleId]) { - continue; - } - - const suppressionsCount = - suppressions[relativeFilePath][ruleId].count; - const violationsCount = violationsByRule[ruleId].count; - - // Suppress messages if the number of violations is less or equal to the suppressions count - if (violationsCount <= suppressionsCount) { - SuppressionsService.suppressMessagesByRule(result, ruleId); - wasSuppressed = true; - } - - // Update the count to match the new number of violations, otherwise remove the rule entirely - if (violationsCount < suppressionsCount) { - unused[relativeFilePath] ??= {}; - unused[relativeFilePath][ruleId] ??= {}; - unused[relativeFilePath][ruleId].count = - suppressionsCount - violationsCount; - } - } - - // Mark as unused all the suppressions that were not matched against a rule - for (const ruleId in suppressions[relativeFilePath]) { - if (violationsByRule[ruleId]) { - continue; - } - - unused[relativeFilePath] ??= {}; - unused[relativeFilePath][ruleId] = - suppressions[relativeFilePath][ruleId]; - } - - // Recalculate stats if messages were suppressed - if (wasSuppressed) { - Object.assign(result, calculateStatsPerFile(result.messages)); - } - } - - return { - results: filtered, - unused, - }; - } - - /** - * Loads the suppressions file. - * @throws {Error} If the suppressions file cannot be parsed. - * @returns {Promise} The suppressions. - */ - async load() { - try { - const data = await fs.promises.readFile(this.filePath, "utf8"); - - return JSON.parse(data); - } catch (err) { - if (err.code === "ENOENT") { - return {}; - } - throw new Error( - `Failed to parse suppressions file at ${this.filePath}`, - { - cause: err, - }, - ); - } - } - - /** - * Updates the suppressions file. - * @param {SuppressedViolations} suppressions The suppressions to save. - * @returns {Promise} - * @private - */ - save(suppressions) { - return fs.promises.writeFile( - this.filePath, - stringify(suppressions, { space: 2 }), - ); - } - - /** - * Counts the violations by rule, ignoring warnings. - * @param {LintMessage[]} messages The messages to count. - * @returns {Record} The number of violations by rule. - */ - static countViolationsByRule(messages) { - return messages.reduce((totals, message) => { - if (message.severity === 2 && message.ruleId) { - totals[message.ruleId] ??= { count: 0 }; - totals[message.ruleId].count++; - } - return totals; - }, {}); - } - - /** - * Returns the relative path of a file to the current working directory. - * Always in POSIX format for consistency and interoperability. - * @param {string} filePath The file path. - * @returns {string} The relative file path. - */ - getRelativeFilePath(filePath) { - return path - .relative(this.cwd, filePath) - .split(path.sep) - .join(path.posix.sep); - } - - /** - * Moves the messages matching the rule to `LintResult#suppressedMessages` and updates the stats. - * @param {LintResult} result The result to update. - * @param {string} ruleId The rule to suppress. - * @returns {void} - */ - static suppressMessagesByRule(result, ruleId) { - const suppressedMessages = result.messages.filter( - message => message.ruleId === ruleId, - ); - - result.suppressedMessages = result.suppressedMessages.concat( - suppressedMessages.map(message => { - message.suppressions = [ - { - kind: "file", - justification: "", - }, - ]; - - return message; - }), - ); - - result.messages = result.messages.filter( - message => message.ruleId !== ruleId, - ); - } -} - -module.exports = { SuppressionsService }; diff --git a/node_modules/eslint/lib/services/warning-service.js b/node_modules/eslint/lib/services/warning-service.js deleted file mode 100644 index 38d63fe77..000000000 --- a/node_modules/eslint/lib/services/warning-service.js +++ /dev/null @@ -1,111 +0,0 @@ -/** - * @fileoverview Emits warnings for ESLint. - * @author Francesco Trotta - */ - -"use strict"; - -//----------------------------------------------------------------------------- -// Exports -//----------------------------------------------------------------------------- - -/** - * A service that emits warnings for ESLint. - */ -class WarningService { - /** - * Creates a new instance of the service. - * @param {{ emitWarning?: ((warning: string, type: string) => void) | undefined }} [options] A function called internally to emit warnings using API provided by the runtime. - */ - constructor({ - emitWarning = globalThis.process?.emitWarning ?? (() => {}), - } = {}) { - this.emitWarning = emitWarning; - } - - /** - * Emits a warning when circular fixes are detected while fixing a file. - * This method is used by the Linter and is safe to call outside Node.js. - * @param {string} filename The name of the file being fixed. - * @returns {void} - */ - emitCircularFixesWarning(filename) { - this.emitWarning( - `Circular fixes detected while fixing ${filename}. It is likely that you have conflicting rules in your configuration.`, - "ESLintCircularFixesWarning", - ); - } - - /** - * Emits a warning when an empty config file has been loaded. - * @param {string} configFilePath The path to the config file. - * @returns {void} - */ - emitEmptyConfigWarning(configFilePath) { - this.emitWarning( - `Running ESLint with an empty config (from ${configFilePath}). Please double-check that this is what you want. If you want to run ESLint with an empty config, export [{}] to remove this warning.`, - "ESLintEmptyConfigWarning", - ); - } - - /** - * Emits a warning when an ".eslintignore" file is found. - * @returns {void} - */ - emitESLintIgnoreWarning() { - this.emitWarning( - 'The ".eslintignore" file is no longer supported. Switch to using the "ignores" property in "eslint.config.js": https://eslint.org/docs/latest/use/configure/migration-guide#ignoring-files', - "ESLintIgnoreWarning", - ); - } - - /** - * Emits a warning when the ESLINT_USE_FLAT_CONFIG environment variable is set to "false". - * @returns {void} - */ - emitESLintRCWarning() { - this.emitWarning( - "You are using an eslintrc configuration file, which is deprecated and support will be removed in v10.0.0. Please migrate to an eslint.config.js file. See https://eslint.org/docs/latest/use/configure/migration-guide for details. An eslintrc configuration file is used because you have the ESLINT_USE_FLAT_CONFIG environment variable set to false. If you want to use an eslint.config.js file, remove the environment variable. If you want to find the location of the eslintrc configuration file, use the --debug flag.", - "ESLintRCWarning", - ); - } - - /** - * Emits a warning when an inactive flag is used. - * This method is used by the Linter and is safe to call outside Node.js. - * @param {string} flag The name of the flag. - * @param {string} message The warning message. - * @returns {void} - */ - emitInactiveFlagWarning(flag, message) { - this.emitWarning(message, `ESLintInactiveFlag_${flag}`); - } - - /** - * Emits a warning when a suboptimal concurrency setting is detected. - * Currently, this is only used to warn when the net linting ratio is low. - * @param {string} notice A notice about how to improve performance. - * @returns {void} - */ - emitPoorConcurrencyWarning(notice) { - this.emitWarning( - `You may ${notice} to improve performance.`, - "ESLintPoorConcurrencyWarning", - ); - } - - /** - * Emits a warning when eslint-env configuration comments are found. - * @param {string} filename The name of the file being linted. - * @param {number} line The line number of the comment. - * @returns {void} - */ - emitESLintEnvWarning(filename, line) { - this.emitWarning( - `/* eslint-env */ comments are no longer recognized when linting with flat config and will be reported as errors as of v10.0.0. Replace them with /* global */ comments or define globals in your config file. See https://eslint.org/docs/latest/use/configure/migration-guide#eslint-env-configuration-comments for details. Found in ${filename} at line ${line}.`, - "ESLintEnvWarning", - ); - } -} - -module.exports = { WarningService }; diff --git a/node_modules/eslint/lib/shared/ajv.js b/node_modules/eslint/lib/shared/ajv.js index 5c5c41bd7..f4f6a6218 100644 --- a/node_modules/eslint/lib/shared/ajv.js +++ b/node_modules/eslint/lib/shared/ajv.js @@ -9,26 +9,26 @@ //------------------------------------------------------------------------------ const Ajv = require("ajv"), - metaSchema = require("ajv/lib/refs/json-schema-draft-04.json"); + metaSchema = require("ajv/lib/refs/json-schema-draft-04.json"); //------------------------------------------------------------------------------ // Public Interface //------------------------------------------------------------------------------ module.exports = (additionalOptions = {}) => { - const ajv = new Ajv({ - meta: false, - useDefaults: true, - validateSchema: false, - missingRefs: "ignore", - verbose: true, - schemaId: "auto", - ...additionalOptions, - }); + const ajv = new Ajv({ + meta: false, + useDefaults: true, + validateSchema: false, + missingRefs: "ignore", + verbose: true, + schemaId: "auto", + ...additionalOptions + }); - ajv.addMetaSchema(metaSchema); - // eslint-disable-next-line no-underscore-dangle -- Ajv's API - ajv._opts.defaultMeta = metaSchema.id; + ajv.addMetaSchema(metaSchema); + // eslint-disable-next-line no-underscore-dangle -- Ajv's API + ajv._opts.defaultMeta = metaSchema.id; - return ajv; + return ajv; }; diff --git a/node_modules/eslint/lib/shared/assert.js b/node_modules/eslint/lib/shared/assert.js deleted file mode 100644 index 4dcaf41f8..000000000 --- a/node_modules/eslint/lib/shared/assert.js +++ /dev/null @@ -1,21 +0,0 @@ -/** - * @fileoverview Assertion utilities equivalent to the Node.js node:asserts module. - * @author Josh Goldberg - */ - -"use strict"; - -/** - * Throws an error if the input is not truthy. - * @param {unknown} value The input that is checked for being truthy. - * @param {string} message Message to throw if the input is not truthy. - * @returns {void} - * @throws {Error} When the condition is not truthy. - */ -function ok(value, message = "Assertion failed.") { - if (!value) { - throw new Error(message); - } -} - -module.exports = ok; diff --git a/node_modules/eslint/lib/shared/ast-utils.js b/node_modules/eslint/lib/shared/ast-utils.js index f7736828b..4ebd49c3c 100644 --- a/node_modules/eslint/lib/shared/ast-utils.js +++ b/node_modules/eslint/lib/shared/ast-utils.js @@ -8,8 +8,7 @@ */ "use strict"; -const breakableTypePattern = - /^(?:(?:Do)?While|For(?:In|Of)?|Switch)Statement$/u; +const breakableTypePattern = /^(?:(?:Do)?While|For(?:In|Of)?|Switch)Statement$/u; const lineBreakPattern = /\r\n|[\r\n\u2028\u2029]/u; const shebangPattern = /^#!([^\r\n]+)/u; @@ -19,12 +18,12 @@ const shebangPattern = /^#!([^\r\n]+)/u; * @returns {RegExp} A global regular expression that matches line terminators */ function createGlobalLinebreakMatcher() { - return new RegExp(lineBreakPattern.source, "gu"); + return new RegExp(lineBreakPattern.source, "gu"); } module.exports = { - breakableTypePattern, - lineBreakPattern, - createGlobalLinebreakMatcher, - shebangPattern, + breakableTypePattern, + lineBreakPattern, + createGlobalLinebreakMatcher, + shebangPattern }; diff --git a/node_modules/eslint/lib/shared/deep-merge-arrays.js b/node_modules/eslint/lib/shared/deep-merge-arrays.js deleted file mode 100644 index 4a8289c46..000000000 --- a/node_modules/eslint/lib/shared/deep-merge-arrays.js +++ /dev/null @@ -1,62 +0,0 @@ -/** - * @fileoverview Applies default rule options - * @author JoshuaKGoldberg - */ - -"use strict"; - -/** - * Check if the variable contains an object strictly rejecting arrays - * @param {unknown} value an object - * @returns {boolean} Whether value is an object - */ -function isObjectNotArray(value) { - return typeof value === "object" && value !== null && !Array.isArray(value); -} - -/** - * Deeply merges second on top of first, creating a new {} object if needed. - * @param {T} first Base, default value. - * @param {U} second User-specified value. - * @returns {T | U | (T & U)} Merged equivalent of second on top of first. - */ -function deepMergeObjects(first, second) { - if (second === void 0) { - return first; - } - - if (!isObjectNotArray(first) || !isObjectNotArray(second)) { - return second; - } - - const result = { ...first, ...second }; - - for (const key of Object.keys(second)) { - if (Object.prototype.propertyIsEnumerable.call(first, key)) { - result[key] = deepMergeObjects(first[key], second[key]); - } - } - - return result; -} - -/** - * Deeply merges second on top of first, creating a new [] array if needed. - * @param {T[]} first Base, default values. - * @param {U[]} second User-specified values. - * @returns {(T | U | (T & U))[]} Merged equivalent of second on top of first. - */ -function deepMergeArrays(first, second) { - if (!first || !second) { - return second || first || []; - } - - return [ - ...first.map((value, i) => - deepMergeObjects(value, i < second.length ? second[i] : void 0), - ), - ...second.slice(first.length), - ]; -} - -module.exports = { deepMergeArrays }; diff --git a/node_modules/eslint/lib/shared/directives.js b/node_modules/eslint/lib/shared/directives.js index f1dddf597..ff67b00a5 100644 --- a/node_modules/eslint/lib/shared/directives.js +++ b/node_modules/eslint/lib/shared/directives.js @@ -8,9 +8,8 @@ */ "use strict"; -const directivesPattern = - /^(eslint(?:-env|-enable|-disable(?:(?:-next)?-line)?)?|exported|globals?)(?:\s|$)/u; +const directivesPattern = /^(eslint(?:-env|-enable|-disable(?:(?:-next)?-line)?)?|exported|globals?)(?:\s|$)/u; module.exports = { - directivesPattern, + directivesPattern }; diff --git a/node_modules/eslint/lib/shared/flags.js b/node_modules/eslint/lib/shared/flags.js deleted file mode 100644 index e87e633c3..000000000 --- a/node_modules/eslint/lib/shared/flags.js +++ /dev/null @@ -1,108 +0,0 @@ -/** - * @fileoverview Shared flags for ESLint. - */ - -"use strict"; - -//------------------------------------------------------------------------------ -// Typedefs -//------------------------------------------------------------------------------ - -/** - * @typedef {Object} InactiveFlagData - * @property {string} description Flag description - * @property {string | null} [replacedBy] Can be either: - * - An active flag (string) that enables the same feature. - * - `null` if the feature is now enabled by default. - * - Omitted if the feature has been abandoned. - */ - -//----------------------------------------------------------------------------- -// Exports -//----------------------------------------------------------------------------- - -/** - * The set of flags that change ESLint behavior with a description. - * @type {Map} - */ -const activeFlags = new Map([ - ["test_only", "Used only for testing."], - ["test_only_2", "Used only for testing."], - [ - "v10_config_lookup_from_file", - "Look up `eslint.config.js` from the file being linted.", - ], - [ - "unstable_native_nodejs_ts_config", - "Use native Node.js to load TypeScript configuration.", - ], -]); - -/** - * The set of flags that used to be active. - * @type {Map} - */ -const inactiveFlags = new Map([ - [ - "test_only_replaced", - { - description: - "Used only for testing flags that have been replaced by other flags.", - replacedBy: "test_only", - }, - ], - [ - "test_only_enabled_by_default", - { - description: - "Used only for testing flags whose features have been enabled by default.", - replacedBy: null, - }, - ], - [ - "test_only_abandoned", - { - description: - "Used only for testing flags whose features have been abandoned.", - }, - ], - [ - "unstable_ts_config", - { - description: "Enable TypeScript configuration files.", - replacedBy: null, - }, - ], - [ - "unstable_config_lookup_from_file", - { - description: - "Look up `eslint.config.js` from the file being linted.", - replacedBy: "v10_config_lookup_from_file", - }, - ], -]); - -/** - * Creates a message that describes the reason the flag is inactive. - * @param {InactiveFlagData} inactiveFlagData Data for the inactive flag. - * @returns {string} Message describing the reason the flag is inactive. - */ -function getInactivityReasonMessage({ replacedBy }) { - if (typeof replacedBy === "undefined") { - return "This feature has been abandoned."; - } - - if (typeof replacedBy === "string") { - return `This flag has been renamed '${replacedBy}' to reflect its stabilization. Please use '${replacedBy}' instead.`; - } - - // null - return "This feature is now enabled by default."; -} - -module.exports = { - activeFlags, - inactiveFlags, - getInactivityReasonMessage, -}; diff --git a/node_modules/eslint/lib/shared/logging.js b/node_modules/eslint/lib/shared/logging.js index 3e8dfb0b0..fd5e8a648 100644 --- a/node_modules/eslint/lib/shared/logging.js +++ b/node_modules/eslint/lib/shared/logging.js @@ -9,30 +9,22 @@ /* c8 ignore next */ module.exports = { - /** - * Cover for console.info - * @param {...any} args The elements to log. - * @returns {void} - */ - info(...args) { - console.log(...args); - }, - /** - * Cover for console.warn - * @param {...any} args The elements to log. - * @returns {void} - */ - warn(...args) { - console.warn(...args); - }, + /** + * Cover for console.log + * @param {...any} args The elements to log. + * @returns {void} + */ + info(...args) { + console.log(...args); + }, - /** - * Cover for console.error - * @param {...any} args The elements to log. - * @returns {void} - */ - error(...args) { - console.error(...args); - }, + /** + * Cover for console.error + * @param {...any} args The elements to log. + * @returns {void} + */ + error(...args) { + console.error(...args); + } }; diff --git a/node_modules/eslint/lib/shared/naming.js b/node_modules/eslint/lib/shared/naming.js deleted file mode 100644 index 86621d138..000000000 --- a/node_modules/eslint/lib/shared/naming.js +++ /dev/null @@ -1,109 +0,0 @@ -/** - * @fileoverview Common helpers for naming of plugins, formatters and configs - */ - -"use strict"; - -const NAMESPACE_REGEX = /^@.*\//u; - -/** - * Brings package name to correct format based on prefix - * @param {string} name The name of the package. - * @param {string} prefix Can be either "eslint-plugin", "eslint-config" or "eslint-formatter" - * @returns {string} Normalized name of the package - * @private - */ -function normalizePackageName(name, prefix) { - let normalizedName = name; - - /** - * On Windows, name can come in with Windows slashes instead of Unix slashes. - * Normalize to Unix first to avoid errors later on. - * https://github.com/eslint/eslint/issues/5644 - */ - if (normalizedName.includes("\\")) { - normalizedName = normalizedName.replace(/\\/gu, "/"); - } - - if (normalizedName.charAt(0) === "@") { - /** - * it's a scoped package - * package name is the prefix, or just a username - */ - const scopedPackageShortcutRegex = new RegExp( - `^(@[^/]+)(?:/(?:${prefix})?)?$`, - "u", - ), - scopedPackageNameRegex = new RegExp(`^${prefix}(-|$)`, "u"); - - if (scopedPackageShortcutRegex.test(normalizedName)) { - normalizedName = normalizedName.replace( - scopedPackageShortcutRegex, - `$1/${prefix}`, - ); - } else if (!scopedPackageNameRegex.test(normalizedName.split("/")[1])) { - /** - * for scoped packages, insert the prefix after the first / unless - * the path is already @scope/eslint or @scope/eslint-xxx-yyy - */ - normalizedName = normalizedName.replace( - /^@([^/]+)\/(.*)$/u, - `@$1/${prefix}-$2`, - ); - } - } else if (!normalizedName.startsWith(`${prefix}-`)) { - normalizedName = `${prefix}-${normalizedName}`; - } - - return normalizedName; -} - -/** - * Removes the prefix from a fullname. - * @param {string} fullname The term which may have the prefix. - * @param {string} prefix The prefix to remove. - * @returns {string} The term without prefix. - */ -function getShorthandName(fullname, prefix) { - if (fullname[0] === "@") { - let matchResult = new RegExp(`^(@[^/]+)/${prefix}$`, "u").exec( - fullname, - ); - - if (matchResult) { - return matchResult[1]; - } - - matchResult = new RegExp(`^(@[^/]+)/${prefix}-(.+)$`, "u").exec( - fullname, - ); - if (matchResult) { - return `${matchResult[1]}/${matchResult[2]}`; - } - } else if (fullname.startsWith(`${prefix}-`)) { - return fullname.slice(prefix.length + 1); - } - - return fullname; -} - -/** - * Gets the scope (namespace) of a term. - * @param {string} term The term which may have the namespace. - * @returns {string} The namespace of the term if it has one. - */ -function getNamespaceFromTerm(term) { - const match = term.match(NAMESPACE_REGEX); - - return match ? match[0] : ""; -} - -//------------------------------------------------------------------------------ -// Public Interface -//------------------------------------------------------------------------------ - -module.exports = { - normalizePackageName, - getShorthandName, - getNamespaceFromTerm, -}; diff --git a/node_modules/eslint/lib/shared/option-utils.js b/node_modules/eslint/lib/shared/option-utils.js deleted file mode 100644 index 0917ba521..000000000 --- a/node_modules/eslint/lib/shared/option-utils.js +++ /dev/null @@ -1,63 +0,0 @@ -/** - * @fileoverview Utilities to operate on option objects. - * @author Josh Goldberg - */ - -"use strict"; - -/** - * Determines whether any of input's properties are different - * from values that already exist in original. - * @template T - * @param {Partial} input New value. - * @param {T} original Original value. - * @returns {boolean} Whether input includes an explicit difference. - */ -function containsDifferentProperty(input, original) { - if (input === original) { - return false; - } - - if ( - typeof input !== typeof original || - Array.isArray(input) !== Array.isArray(original) - ) { - return true; - } - - if (Array.isArray(input)) { - return ( - input.length !== original.length || - input.some((value, i) => - containsDifferentProperty(value, original[i]), - ) - ); - } - - if (typeof input === "object") { - if (input === null || original === null) { - return true; - } - - const inputKeys = Object.keys(input); - const originalKeys = Object.keys(original); - - return ( - inputKeys.length !== originalKeys.length || - inputKeys.some( - inputKey => - !Object.hasOwn(original, inputKey) || - containsDifferentProperty( - input[inputKey], - original[inputKey], - ), - ) - ); - } - - return true; -} - -module.exports = { - containsDifferentProperty, -}; diff --git a/node_modules/eslint/lib/shared/relative-module-resolver.js b/node_modules/eslint/lib/shared/relative-module-resolver.js index 14e7eec16..18a694983 100644 --- a/node_modules/eslint/lib/shared/relative-module-resolver.js +++ b/node_modules/eslint/lib/shared/relative-module-resolver.js @@ -1,3 +1,15 @@ +/* + * STOP!!! DO NOT MODIFY. + * + * This file is part of the ongoing work to move the eslintrc-style config + * system into the @eslint/eslintrc package. This file needs to remain + * unchanged in order for this work to proceed. + * + * If you think you need to change this file, please contact @nzakas first. + * + * Thanks in advance for your cooperation. + */ + /** * Utility for resolving a module relative to another module * @author Teddy Katz @@ -5,24 +17,34 @@ "use strict"; -const Module = require("node:module"); +const { createRequire } = require("module"); -/* - * `Module.createRequire` is added in v12.2.0. It supports URL as well. - * We only support the case where the argument is a filepath, not a URL. - */ -const createRequire = Module.createRequire; +module.exports = { -/** - * Resolves a Node module relative to another module - * @param {string} moduleName The name of a Node module, or a path to a Node module. - * @param {string} relativeToPath An absolute path indicating the module that `moduleName` should be resolved relative to. This must be - * a file rather than a directory, but the file need not actually exist. - * @returns {string} The absolute path that would result from calling `require.resolve(moduleName)` in a file located at `relativeToPath` - * @throws {Error} When the module cannot be resolved. - */ -function resolve(moduleName, relativeToPath) { - return createRequire(relativeToPath).resolve(moduleName); -} + /** + * Resolves a Node module relative to another module + * @param {string} moduleName The name of a Node module, or a path to a Node module. + * @param {string} relativeToPath An absolute path indicating the module that `moduleName` should be resolved relative to. This must be + * a file rather than a directory, but the file need not actually exist. + * @throws {Error} Any error from `module.createRequire` or its `resolve`. + * @returns {string} The absolute path that would result from calling `require.resolve(moduleName)` in a file located at `relativeToPath` + */ + resolve(moduleName, relativeToPath) { + try { + return createRequire(relativeToPath).resolve(moduleName); + } catch (error) { -exports.resolve = resolve; + // This `if` block is for older Node.js than 12.0.0. We can remove this block in the future. + if ( + typeof error === "object" && + error !== null && + error.code === "MODULE_NOT_FOUND" && + !error.requireStack && + error.message.includes(moduleName) + ) { + error.message += `\nRequire stack:\n- ${relativeToPath}`; + } + throw error; + } + } +}; diff --git a/node_modules/eslint/lib/shared/runtime-info.js b/node_modules/eslint/lib/shared/runtime-info.js index 54fd7609d..b99ad1038 100644 --- a/node_modules/eslint/lib/shared/runtime-info.js +++ b/node_modules/eslint/lib/shared/runtime-info.js @@ -9,9 +9,9 @@ // Requirements //------------------------------------------------------------------------------ -const path = require("node:path"); +const path = require("path"); const spawn = require("cross-spawn"); -const os = require("node:os"); +const os = require("os"); const log = require("../shared/logging"); const packageJson = require("../../package.json"); @@ -24,138 +24,129 @@ const packageJson = require("../../package.json"); * @returns {string} A string that contains execution environment information. */ function environment() { - const cache = new Map(); - - /** - * Checks if a path is a child of a directory. - * @param {string} parentPath The parent path to check. - * @param {string} childPath The path to check. - * @returns {boolean} Whether or not the given path is a child of a directory. - */ - function isChildOfDirectory(parentPath, childPath) { - return !path.relative(parentPath, childPath).startsWith(".."); - } - - /** - * Synchronously executes a shell command and formats the result. - * @param {string} cmd The command to execute. - * @param {Array} args The arguments to be executed with the command. - * @throws {Error} As may be collected by `cross-spawn.sync`. - * @returns {string} The version returned by the command. - */ - function execCommand(cmd, args) { - const key = [cmd, ...args].join(" "); - - if (cache.has(key)) { - return cache.get(key); - } - - const process = spawn.sync(cmd, args, { encoding: "utf8" }); - - if (process.error) { - throw process.error; - } - - const result = process.stdout.trim(); - - cache.set(key, result); - return result; - } - - /** - * Normalizes a version number. - * @param {string} versionStr The string to normalize. - * @returns {string} The normalized version number. - */ - function normalizeVersionStr(versionStr) { - return versionStr.startsWith("v") ? versionStr : `v${versionStr}`; - } - - /** - * Gets bin version. - * @param {string} bin The bin to check. - * @throws {Error} As may be collected by `cross-spawn.sync`. - * @returns {string} The normalized version returned by the command. - */ - function getBinVersion(bin) { - const binArgs = ["--version"]; - - try { - return normalizeVersionStr(execCommand(bin, binArgs)); - } catch (e) { - log.error( - `Error finding ${bin} version running the command \`${bin} ${binArgs.join(" ")}\``, - ); - throw e; - } - } - - /** - * Gets installed npm package version. - * @param {string} pkg The package to check. - * @param {boolean} global Whether to check globally or not. - * @throws {Error} As may be collected by `cross-spawn.sync`. - * @returns {string} The normalized version returned by the command. - */ - function getNpmPackageVersion(pkg, { global = false } = {}) { - const npmBinArgs = ["bin", "-g"]; - const npmLsArgs = ["ls", "--depth=0", "--json", pkg]; - - if (global) { - npmLsArgs.push("-g"); - } - - try { - const parsedStdout = JSON.parse(execCommand("npm", npmLsArgs)); - - /* - * Checking globally returns an empty JSON object, while local checks - * include the name and version of the local project. - */ - if ( - Object.keys(parsedStdout).length === 0 || - !(parsedStdout.dependencies && parsedStdout.dependencies.eslint) - ) { - return "Not found"; - } - - const [, processBinPath] = process.argv; - let npmBinPath; - - try { - npmBinPath = execCommand("npm", npmBinArgs); - } catch (e) { - log.error( - `Error finding npm binary path when running command \`npm ${npmBinArgs.join(" ")}\``, - ); - throw e; - } - - const isGlobal = isChildOfDirectory(npmBinPath, processBinPath); - let pkgVersion = parsedStdout.dependencies.eslint.version; - - if ((global && isGlobal) || (!global && !isGlobal)) { - pkgVersion += " (Currently used)"; - } - - return normalizeVersionStr(pkgVersion); - } catch (e) { - log.error( - `Error finding ${pkg} version running the command \`npm ${npmLsArgs.join(" ")}\``, - ); - throw e; - } - } - - return [ - "Environment Info:", - "", - `Node version: ${process.version}`, - `npm version: ${getBinVersion("npm")}`, - `Local ESLint version: ${getNpmPackageVersion("eslint", { global: false })}`, - `Global ESLint version: ${getNpmPackageVersion("eslint", { global: true })}`, - `Operating System: ${os.platform()} ${os.release()}`, - ].join("\n"); + const cache = new Map(); + + /** + * Checks if a path is a child of a directory. + * @param {string} parentPath The parent path to check. + * @param {string} childPath The path to check. + * @returns {boolean} Whether or not the given path is a child of a directory. + */ + function isChildOfDirectory(parentPath, childPath) { + return !path.relative(parentPath, childPath).startsWith(".."); + } + + /** + * Synchronously executes a shell command and formats the result. + * @param {string} cmd The command to execute. + * @param {Array} args The arguments to be executed with the command. + * @throws {Error} As may be collected by `cross-spawn.sync`. + * @returns {string} The version returned by the command. + */ + function execCommand(cmd, args) { + const key = [cmd, ...args].join(" "); + + if (cache.has(key)) { + return cache.get(key); + } + + const process = spawn.sync(cmd, args, { encoding: "utf8" }); + + if (process.error) { + throw process.error; + } + + const result = process.stdout.trim(); + + cache.set(key, result); + return result; + } + + /** + * Normalizes a version number. + * @param {string} versionStr The string to normalize. + * @returns {string} The normalized version number. + */ + function normalizeVersionStr(versionStr) { + return versionStr.startsWith("v") ? versionStr : `v${versionStr}`; + } + + /** + * Gets bin version. + * @param {string} bin The bin to check. + * @throws {Error} As may be collected by `cross-spawn.sync`. + * @returns {string} The normalized version returned by the command. + */ + function getBinVersion(bin) { + const binArgs = ["--version"]; + + try { + return normalizeVersionStr(execCommand(bin, binArgs)); + } catch (e) { + log.error(`Error finding ${bin} version running the command \`${bin} ${binArgs.join(" ")}\``); + throw e; + } + } + + /** + * Gets installed npm package version. + * @param {string} pkg The package to check. + * @param {boolean} global Whether to check globally or not. + * @throws {Error} As may be collected by `cross-spawn.sync`. + * @returns {string} The normalized version returned by the command. + */ + function getNpmPackageVersion(pkg, { global = false } = {}) { + const npmBinArgs = ["bin", "-g"]; + const npmLsArgs = ["ls", "--depth=0", "--json", pkg]; + + if (global) { + npmLsArgs.push("-g"); + } + + try { + const parsedStdout = JSON.parse(execCommand("npm", npmLsArgs)); + + /* + * Checking globally returns an empty JSON object, while local checks + * include the name and version of the local project. + */ + if (Object.keys(parsedStdout).length === 0 || !(parsedStdout.dependencies && parsedStdout.dependencies.eslint)) { + return "Not found"; + } + + const [, processBinPath] = process.argv; + let npmBinPath; + + try { + npmBinPath = execCommand("npm", npmBinArgs); + } catch (e) { + log.error(`Error finding npm binary path when running command \`npm ${npmBinArgs.join(" ")}\``); + throw e; + } + + const isGlobal = isChildOfDirectory(npmBinPath, processBinPath); + let pkgVersion = parsedStdout.dependencies.eslint.version; + + if ((global && isGlobal) || (!global && !isGlobal)) { + pkgVersion += " (Currently used)"; + } + + return normalizeVersionStr(pkgVersion); + } catch (e) { + log.error(`Error finding ${pkg} version running the command \`npm ${npmLsArgs.join(" ")}\``); + throw e; + } + } + + return [ + "Environment Info:", + "", + `Node version: ${getBinVersion("node")}`, + `npm version: ${getBinVersion("npm")}`, + `Local ESLint version: ${getNpmPackageVersion("eslint", { global: false })}`, + `Global ESLint version: ${getNpmPackageVersion("eslint", { global: true })}`, + `Operating System: ${os.platform()} ${os.release()}` + ].join("\n"); } /** @@ -163,7 +154,7 @@ function environment() { * @returns {string} The version from the currently executing ESLint's package.json. */ function version() { - return `v${packageJson.version}`; + return `v${packageJson.version}`; } //------------------------------------------------------------------------------ @@ -171,7 +162,6 @@ function version() { //------------------------------------------------------------------------------ module.exports = { - __esModule: true, // Indicate intent for imports, remove ambiguity for Knip (see: https://github.com/eslint/eslint/pull/18005#discussion_r1484422616) - environment, - version, + environment, + version }; diff --git a/node_modules/eslint/lib/shared/serialization.js b/node_modules/eslint/lib/shared/serialization.js deleted file mode 100644 index 01cacf5ee..000000000 --- a/node_modules/eslint/lib/shared/serialization.js +++ /dev/null @@ -1,78 +0,0 @@ -/** - * @fileoverview Serialization utils. - * @author Bryan Mishkin - */ - -"use strict"; - -/** - * Check if a value is a primitive or plain object created by the Object constructor. - * @param {any} val the value to check - * @returns {boolean} true if so - * @private - */ -function isSerializablePrimitiveOrPlainObject(val) { - return ( - val === null || - typeof val === "string" || - typeof val === "boolean" || - typeof val === "number" || - (typeof val === "object" && val.constructor === Object) || - Array.isArray(val) - ); -} - -/** - * Check if a value is serializable. - * Functions or objects like RegExp cannot be serialized by JSON.stringify(). - * Inspired by: https://stackoverflow.com/questions/30579940/reliable-way-to-check-if-objects-is-serializable-in-javascript - * @param {any} val The value - * @param {Set} seenObjects Objects already seen in this path from the root object. - * @returns {boolean} `true` if the value is serializable - */ -function isSerializable(val, seenObjects = new Set()) { - if (!isSerializablePrimitiveOrPlainObject(val)) { - return false; - } - if (typeof val === "object" && val !== null) { - if (seenObjects.has(val)) { - /* - * Since this is a depth-first traversal, encountering - * the same object again means there is a circular reference. - * Objects with circular references are not serializable. - */ - return false; - } - for (const property in val) { - if (Object.hasOwn(val, property)) { - if (!isSerializablePrimitiveOrPlainObject(val[property])) { - return false; - } - if ( - typeof val[property] === "object" && - val[property] !== null - ) { - if ( - /* - * We're creating a new Set of seen objects because we want to - * ensure that `val` doesn't appear again in this path, but it can appear - * in other paths. This allows for reusing objects in the graph, as long as - * there are no cycles. - */ - !isSerializable( - val[property], - new Set([...seenObjects, val]), - ) - ) { - return false; - } - } - } - } - } - return true; -} - -module.exports = { - isSerializable, -}; diff --git a/node_modules/eslint/lib/shared/severity.js b/node_modules/eslint/lib/shared/severity.js index 382704b5a..6b21469a9 100644 --- a/node_modules/eslint/lib/shared/severity.js +++ b/node_modules/eslint/lib/shared/severity.js @@ -12,16 +12,16 @@ * @returns {string} severity string */ function normalizeSeverityToString(severity) { - if ([2, "2", "error"].includes(severity)) { - return "error"; - } - if ([1, "1", "warn"].includes(severity)) { - return "warn"; - } - if ([0, "0", "off"].includes(severity)) { - return "off"; - } - throw new Error(`Invalid severity value: ${severity}`); + if ([2, "2", "error"].includes(severity)) { + return "error"; + } + if ([1, "1", "warn"].includes(severity)) { + return "warn"; + } + if ([0, "0", "off"].includes(severity)) { + return "off"; + } + throw new Error(`Invalid severity value: ${severity}`); } /** @@ -31,19 +31,19 @@ function normalizeSeverityToString(severity) { * @returns {number} severity number */ function normalizeSeverityToNumber(severity) { - if ([2, "2", "error"].includes(severity)) { - return 2; - } - if ([1, "1", "warn"].includes(severity)) { - return 1; - } - if ([0, "0", "off"].includes(severity)) { - return 0; - } - throw new Error(`Invalid severity value: ${severity}`); + if ([2, "2", "error"].includes(severity)) { + return 2; + } + if ([1, "1", "warn"].includes(severity)) { + return 1; + } + if ([0, "0", "off"].includes(severity)) { + return 0; + } + throw new Error(`Invalid severity value: ${severity}`); } module.exports = { - normalizeSeverityToString, - normalizeSeverityToNumber, + normalizeSeverityToString, + normalizeSeverityToNumber }; diff --git a/node_modules/eslint/lib/shared/stats.js b/node_modules/eslint/lib/shared/stats.js deleted file mode 100644 index 791b77083..000000000 --- a/node_modules/eslint/lib/shared/stats.js +++ /dev/null @@ -1,30 +0,0 @@ -/** - * @fileoverview Provides helper functions to start/stop the time measurements - * that are provided by the ESLint 'stats' option. - * @author Mara Kiefer - */ -"use strict"; - -/** - * Start time measurement - * @returns {[number, number]} t variable for tracking time - */ -function startTime() { - return process.hrtime(); -} - -/** - * End time measurement - * @param {[number, number]} t Variable for tracking time - * @returns {number} The measured time in milliseconds - */ -function endTime(t) { - const time = process.hrtime(t); - - return time[0] * 1e3 + time[1] / 1e6; -} - -module.exports = { - startTime, - endTime, -}; diff --git a/node_modules/eslint/lib/shared/string-utils.js b/node_modules/eslint/lib/shared/string-utils.js index 954c5bfff..ed0781e57 100644 --- a/node_modules/eslint/lib/shared/string-utils.js +++ b/node_modules/eslint/lib/shared/string-utils.js @@ -5,6 +5,12 @@ "use strict"; +//------------------------------------------------------------------------------ +// Requirements +//------------------------------------------------------------------------------ + +const Graphemer = require("graphemer").default; + //------------------------------------------------------------------------------ // Helpers //------------------------------------------------------------------------------ @@ -12,8 +18,8 @@ // eslint-disable-next-line no-control-regex -- intentionally including control characters const ASCII_REGEX = /^[\u0000-\u007f]*$/u; -/** @type {Intl.Segmenter | undefined} */ -let segmenter; +/** @type {Graphemer | undefined} */ +let splitter; //------------------------------------------------------------------------------ // Public Interface @@ -25,10 +31,10 @@ let segmenter; * @returns {string} The converted string */ function upperCaseFirst(string) { - if (string.length <= 1) { - return string.toUpperCase(); - } - return string[0].toUpperCase() + string.slice(1); + if (string.length <= 1) { + return string.toUpperCase(); + } + return string[0].toUpperCase() + string.slice(1); } /** @@ -37,22 +43,18 @@ function upperCaseFirst(string) { * @returns {number} The number of graphemes in `value`. */ function getGraphemeCount(value) { - if (ASCII_REGEX.test(value)) { - return value.length; - } - - segmenter ??= new Intl.Segmenter("en-US"); // en-US locale should be supported everywhere - let graphemeCount = 0; + if (ASCII_REGEX.test(value)) { + return value.length; + } - // eslint-disable-next-line no-unused-vars -- for-of needs a variable - for (const unused of segmenter.segment(value)) { - graphemeCount++; - } + if (!splitter) { + splitter = new Graphemer(); + } - return graphemeCount; + return splitter.countGraphemes(value); } module.exports = { - upperCaseFirst, - getGraphemeCount, + upperCaseFirst, + getGraphemeCount }; diff --git a/node_modules/eslint/lib/shared/text-table.js b/node_modules/eslint/lib/shared/text-table.js deleted file mode 100644 index 1dfd48166..000000000 --- a/node_modules/eslint/lib/shared/text-table.js +++ /dev/null @@ -1,68 +0,0 @@ -/** - * @fileoverview Optimized version of the `text-table` npm module to improve performance by replacing inefficient regex-based - * whitespace trimming with a modern built-in method. - * - * This modification addresses a performance issue reported in https://github.com/eslint/eslint/issues/18709 - * - * The `text-table` module is published under the MIT License. For the original source, refer to: - * https://www.npmjs.com/package/text-table. - */ - -/* - * - * This software is released under the MIT license: - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of - * this software and associated documentation files (the "Software"), to deal in - * the Software without restriction, including without limitation the rights to - * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of - * the Software, and to permit persons to whom the Software is furnished to do so, - * subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS - * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR - * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER - * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -"use strict"; - -module.exports = function (rows_, opts) { - const hsep = " "; - const align = opts.align; - const stringLength = opts.stringLength; - - const sizes = rows_.reduce((acc, row) => { - row.forEach((c, ix) => { - const n = stringLength(c); - - if (!acc[ix] || n > acc[ix]) { - acc[ix] = n; - } - }); - return acc; - }, []); - - return rows_ - .map(row => - row - .map((c, ix) => { - const n = sizes[ix] - stringLength(c) || 0; - const s = Array(Math.max(n + 1, 1)).join(" "); - - if (align[ix] === "r") { - return s + c; - } - - return c + s; - }) - .join(hsep) - .trimEnd(), - ) - .join("\n"); -}; diff --git a/node_modules/eslint/lib/shared/translate-cli-options.js b/node_modules/eslint/lib/shared/translate-cli-options.js deleted file mode 100644 index d765f8046..000000000 --- a/node_modules/eslint/lib/shared/translate-cli-options.js +++ /dev/null @@ -1,281 +0,0 @@ -/** - * @fileoverview Translates CLI options into ESLint constructor options. - * @author Nicholas C. Zakas - * @author Francesco Trotta - */ - -"use strict"; - -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ - -const { normalizeSeverityToString } = require("./severity"); -const { getShorthandName, normalizePackageName } = require("./naming"); -const { ModuleImporter } = require("@humanwhocodes/module-importer"); - -//------------------------------------------------------------------------------ -// Types -//------------------------------------------------------------------------------ - -/** @typedef {import("../types").ESLint.Options} ESLintOptions */ -/** @typedef {import("../types").ESLint.LegacyOptions} LegacyESLintOptions */ -/** @typedef {import("../types").Linter.LintMessage} LintMessage */ -/** @typedef {import("../options").ParsedCLIOptions} ParsedCLIOptions */ -/** @typedef {import("../types").ESLint.Plugin} Plugin */ - -//------------------------------------------------------------------------------ -// Helpers -//------------------------------------------------------------------------------ - -/** - * Loads plugins with the specified names. - * @param {{ "import": (name: string) => Promise }} importer An object with an `import` method called once for each plugin. - * @param {string[]} pluginNames The names of the plugins to be loaded, with or without the "eslint-plugin-" prefix. - * @returns {Promise>} A mapping of plugin short names to implementations. - */ -async function loadPlugins(importer, pluginNames) { - const plugins = {}; - - await Promise.all( - pluginNames.map(async pluginName => { - const longName = normalizePackageName(pluginName, "eslint-plugin"); - const module = await importer.import(longName); - - if (!("default" in module)) { - throw new Error( - `"${longName}" cannot be used with the \`--plugin\` option because its default module does not provide a \`default\` export`, - ); - } - - const shortName = getShorthandName(pluginName, "eslint-plugin"); - - plugins[shortName] = module.default; - }), - ); - - return plugins; -} - -/** - * Predicate function for whether or not to apply fixes in quiet mode. - * If a message is a warning, do not apply a fix. - * @param {LintMessage} message The lint result. - * @returns {boolean} True if the lint message is an error (and thus should be - * autofixed), false otherwise. - */ -function quietFixPredicate(message) { - return message.severity === 2; -} - -/** - * Predicate function for whether or not to run a rule in quiet mode. - * If a rule is set to warning, do not run it. - * @param {{ ruleId: string; severity: number; }} rule The rule id and severity. - * @returns {boolean} True if the lint rule should run, false otherwise. - */ -function quietRuleFilter(rule) { - return rule.severity === 2; -} - -//------------------------------------------------------------------------------ -// Public Interface -//------------------------------------------------------------------------------ - -/** - * Translates the CLI options into the options expected by the ESLint constructor. - * @param {ParsedCLIOptions} cliOptions The CLI options to translate. - * @param {"flat"|"eslintrc"} [configType="eslintrc"] The format of the config to generate. - * @returns {Promise} The options object for the ESLint constructor. - */ -async function translateOptions( - { - cache, - cacheFile, - cacheLocation, - cacheStrategy, - concurrency, - config, - configLookup, - env, - errorOnUnmatchedPattern, - eslintrc, - ext, - fix, - fixDryRun, - fixType, - flag, - global, - ignore, - ignorePath, - ignorePattern, - inlineConfig, - parser, - parserOptions, - plugin, - quiet, - reportUnusedDisableDirectives, - reportUnusedDisableDirectivesSeverity, - reportUnusedInlineConfigs, - resolvePluginsRelativeTo, - rule, - rulesdir, - stats, - warnIgnored, - passOnNoPatterns, - maxWarnings, - }, - configType, -) { - let overrideConfig, overrideConfigFile; - const importer = new ModuleImporter(); - - if (configType === "flat") { - overrideConfigFile = - typeof config === "string" ? config : !configLookup; - if (overrideConfigFile === false) { - overrideConfigFile = void 0; - } - - const languageOptions = {}; - - if (global) { - languageOptions.globals = global.reduce((obj, name) => { - if (name.endsWith(":true")) { - obj[name.slice(0, -5)] = "writable"; - } else { - obj[name] = "readonly"; - } - return obj; - }, {}); - } - - if (parserOptions) { - languageOptions.parserOptions = parserOptions; - } - - if (parser) { - languageOptions.parser = await importer.import(parser); - } - - overrideConfig = [ - { - ...(Object.keys(languageOptions).length > 0 - ? { languageOptions } - : {}), - rules: rule ? rule : {}, - }, - ]; - - if ( - reportUnusedDisableDirectives || - reportUnusedDisableDirectivesSeverity !== void 0 - ) { - overrideConfig[0].linterOptions = { - reportUnusedDisableDirectives: reportUnusedDisableDirectives - ? "error" - : normalizeSeverityToString( - reportUnusedDisableDirectivesSeverity, - ), - }; - } - - if (reportUnusedInlineConfigs !== void 0) { - overrideConfig[0].linterOptions = { - ...overrideConfig[0].linterOptions, - reportUnusedInlineConfigs: normalizeSeverityToString( - reportUnusedInlineConfigs, - ), - }; - } - - if (plugin) { - overrideConfig[0].plugins = await loadPlugins(importer, plugin); - } - - if (ext) { - overrideConfig.push({ - files: ext.map( - extension => - `**/*${extension.startsWith(".") ? "" : "."}${extension}`, - ), - }); - } - } else { - overrideConfigFile = config; - - overrideConfig = { - env: - env && - env.reduce((obj, name) => { - obj[name] = true; - return obj; - }, {}), - globals: - global && - global.reduce((obj, name) => { - if (name.endsWith(":true")) { - obj[name.slice(0, -5)] = "writable"; - } else { - obj[name] = "readonly"; - } - return obj; - }, {}), - ignorePatterns: ignorePattern, - parser, - parserOptions, - plugins: plugin, - rules: rule, - }; - } - - const options = { - allowInlineConfig: inlineConfig, - cache, - cacheLocation: cacheLocation || cacheFile, - cacheStrategy, - errorOnUnmatchedPattern, - fix: (fix || fixDryRun) && (quiet ? quietFixPredicate : true), - fixTypes: fixType, - ignore, - overrideConfig, - overrideConfigFile, - passOnNoPatterns, - }; - - if (configType === "flat") { - options.concurrency = concurrency; - options.flags = flag; - options.ignorePatterns = ignorePattern; - options.stats = stats; - options.warnIgnored = warnIgnored; - - /* - * For performance reasons rules not marked as 'error' are filtered out in quiet mode. As maxWarnings - * requires rules set to 'warn' to be run, we only filter out 'warn' rules if maxWarnings is not specified. - */ - options.ruleFilter = - quiet && maxWarnings === -1 ? quietRuleFilter : () => true; - } else { - options.resolvePluginsRelativeTo = resolvePluginsRelativeTo; - options.rulePaths = rulesdir; - options.useEslintrc = eslintrc; - options.extensions = ext; - options.ignorePath = ignorePath; - if ( - reportUnusedDisableDirectives || - reportUnusedDisableDirectivesSeverity !== void 0 - ) { - options.reportUnusedDisableDirectives = - reportUnusedDisableDirectives - ? "error" - : normalizeSeverityToString( - reportUnusedDisableDirectivesSeverity, - ); - } - } - - return options; -} - -module.exports = translateOptions; diff --git a/node_modules/eslint/lib/shared/traverser.js b/node_modules/eslint/lib/shared/traverser.js index e13b2cfa5..38b4e2151 100644 --- a/node_modules/eslint/lib/shared/traverser.js +++ b/node_modules/eslint/lib/shared/traverser.js @@ -21,7 +21,8 @@ const debug = require("debug")("eslint:traverser"); * @returns {void} */ function noop() { - // do nothing. + + // do nothing. } /** @@ -30,7 +31,7 @@ function noop() { * @returns {boolean} `true` if the value is an ASTNode. */ function isNode(x) { - return x !== null && typeof x === "object" && typeof x.type === "string"; + return x !== null && typeof x === "object" && typeof x.type === "string"; } /** @@ -40,163 +41,155 @@ function isNode(x) { * @returns {string[]} The visitor keys of the node. */ function getVisitorKeys(visitorKeys, node) { - let keys = visitorKeys[node.type]; - - if (!keys) { - keys = vk.getKeys(node); - debug( - 'Unknown node type "%s": Estimated visitor keys %j', - node.type, - keys, - ); - } - - return keys; + let keys = visitorKeys[node.type]; + + if (!keys) { + keys = vk.getKeys(node); + debug("Unknown node type \"%s\": Estimated visitor keys %j", node.type, keys); + } + + return keys; } /** * The traverser class to traverse AST trees. */ class Traverser { - constructor() { - this._current = null; - this._parents = []; - this._skipped = false; - this._broken = false; - this._visitorKeys = null; - this._enter = null; - this._leave = null; - } - - /** - * Gives current node. - * @returns {ASTNode} The current node. - */ - current() { - return this._current; - } - - /** - * Gives a copy of the ancestor nodes. - * @returns {ASTNode[]} The ancestor nodes. - */ - parents() { - return this._parents.slice(0); - } - - /** - * Break the current traversal. - * @returns {void} - */ - break() { - this._broken = true; - } - - /** - * Skip child nodes for the current traversal. - * @returns {void} - */ - skip() { - this._skipped = true; - } - - /** - * Traverse the given AST tree. - * @param {ASTNode} node The root node to traverse. - * @param {Object} options The option object. - * @param {Object} [options.visitorKeys=DEFAULT_VISITOR_KEYS] The keys of each node types to traverse child nodes. Default is `./default-visitor-keys.json`. - * @param {Function} [options.enter=noop] The callback function which is called on entering each node. - * @param {Function} [options.leave=noop] The callback function which is called on leaving each node. - * @returns {void} - */ - traverse(node, options) { - this._current = null; - this._parents = []; - this._skipped = false; - this._broken = false; - this._visitorKeys = options.visitorKeys || vk.KEYS; - this._enter = options.enter || noop; - this._leave = options.leave || noop; - this._traverse(node, null); - } - - /** - * Traverse the given AST tree recursively. - * @param {ASTNode} node The current node. - * @param {ASTNode|null} parent The parent node. - * @returns {void} - * @private - */ - _traverse(node, parent) { - if (!isNode(node)) { - return; - } - - this._current = node; - this._skipped = false; - this._enter(node, parent); - - if (!this._skipped && !this._broken) { - const keys = getVisitorKeys(this._visitorKeys, node); - - if (keys.length >= 1) { - this._parents.push(node); - for (let i = 0; i < keys.length && !this._broken; ++i) { - const child = node[keys[i]]; - - if (Array.isArray(child)) { - for ( - let j = 0; - j < child.length && !this._broken; - ++j - ) { - this._traverse(child[j], node); - } - } else { - this._traverse(child, node); - } - } - this._parents.pop(); - } - } - - if (!this._broken) { - this._leave(node, parent); - } - - this._current = parent; - } - - /** - * Calculates the keys to use for traversal. - * @param {ASTNode} node The node to read keys from. - * @returns {string[]} An array of keys to visit on the node. - * @private - */ - static getKeys(node) { - return vk.getKeys(node); - } - - /** - * Traverse the given AST tree. - * @param {ASTNode} node The root node to traverse. - * @param {Object} options The option object. - * @param {Object} [options.visitorKeys=DEFAULT_VISITOR_KEYS] The keys of each node types to traverse child nodes. Default is `./default-visitor-keys.json`. - * @param {Function} [options.enter=noop] The callback function which is called on entering each node. - * @param {Function} [options.leave=noop] The callback function which is called on leaving each node. - * @returns {void} - */ - static traverse(node, options) { - new Traverser().traverse(node, options); - } - - /** - * The default visitor keys. - * @type {Object} - */ - static get DEFAULT_VISITOR_KEYS() { - return vk.KEYS; - } + constructor() { + this._current = null; + this._parents = []; + this._skipped = false; + this._broken = false; + this._visitorKeys = null; + this._enter = null; + this._leave = null; + } + + /** + * Gives current node. + * @returns {ASTNode} The current node. + */ + current() { + return this._current; + } + + /** + * Gives a copy of the ancestor nodes. + * @returns {ASTNode[]} The ancestor nodes. + */ + parents() { + return this._parents.slice(0); + } + + /** + * Break the current traversal. + * @returns {void} + */ + break() { + this._broken = true; + } + + /** + * Skip child nodes for the current traversal. + * @returns {void} + */ + skip() { + this._skipped = true; + } + + /** + * Traverse the given AST tree. + * @param {ASTNode} node The root node to traverse. + * @param {Object} options The option object. + * @param {Object} [options.visitorKeys=DEFAULT_VISITOR_KEYS] The keys of each node types to traverse child nodes. Default is `./default-visitor-keys.json`. + * @param {Function} [options.enter=noop] The callback function which is called on entering each node. + * @param {Function} [options.leave=noop] The callback function which is called on leaving each node. + * @returns {void} + */ + traverse(node, options) { + this._current = null; + this._parents = []; + this._skipped = false; + this._broken = false; + this._visitorKeys = options.visitorKeys || vk.KEYS; + this._enter = options.enter || noop; + this._leave = options.leave || noop; + this._traverse(node, null); + } + + /** + * Traverse the given AST tree recursively. + * @param {ASTNode} node The current node. + * @param {ASTNode|null} parent The parent node. + * @returns {void} + * @private + */ + _traverse(node, parent) { + if (!isNode(node)) { + return; + } + + this._current = node; + this._skipped = false; + this._enter(node, parent); + + if (!this._skipped && !this._broken) { + const keys = getVisitorKeys(this._visitorKeys, node); + + if (keys.length >= 1) { + this._parents.push(node); + for (let i = 0; i < keys.length && !this._broken; ++i) { + const child = node[keys[i]]; + + if (Array.isArray(child)) { + for (let j = 0; j < child.length && !this._broken; ++j) { + this._traverse(child[j], node); + } + } else { + this._traverse(child, node); + } + } + this._parents.pop(); + } + } + + if (!this._broken) { + this._leave(node, parent); + } + + this._current = parent; + } + + /** + * Calculates the keys to use for traversal. + * @param {ASTNode} node The node to read keys from. + * @returns {string[]} An array of keys to visit on the node. + * @private + */ + static getKeys(node) { + return vk.getKeys(node); + } + + /** + * Traverse the given AST tree. + * @param {ASTNode} node The root node to traverse. + * @param {Object} options The option object. + * @param {Object} [options.visitorKeys=DEFAULT_VISITOR_KEYS] The keys of each node types to traverse child nodes. Default is `./default-visitor-keys.json`. + * @param {Function} [options.enter=noop] The callback function which is called on entering each node. + * @param {Function} [options.leave=noop] The callback function which is called on leaving each node. + * @returns {void} + */ + static traverse(node, options) { + new Traverser().traverse(node, options); + } + + /** + * The default visitor keys. + * @type {Object} + */ + static get DEFAULT_VISITOR_KEYS() { + return vk.KEYS; + } } module.exports = Traverser; diff --git a/node_modules/eslint/lib/types/config-api.d.ts b/node_modules/eslint/lib/types/config-api.d.ts deleted file mode 100644 index e795add86..000000000 --- a/node_modules/eslint/lib/types/config-api.d.ts +++ /dev/null @@ -1,12 +0,0 @@ -/** - * @fileoverview typings for "eslint/config" module - * @author Nicholas C. Zakas - */ - -import { - type Config, - defineConfig, - globalIgnores, -} from "@eslint/config-helpers"; - -export { type Config, defineConfig, globalIgnores }; diff --git a/node_modules/eslint/lib/types/index.d.ts b/node_modules/eslint/lib/types/index.d.ts deleted file mode 100644 index e316106be..000000000 --- a/node_modules/eslint/lib/types/index.d.ts +++ /dev/null @@ -1,1473 +0,0 @@ -/** - * @fileoverview This file contains the core types for ESLint. It was initially extracted - * from the `@types/eslint` package. - */ - -/* - * MIT License - * Copyright (c) Microsoft Corporation. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE - */ - -import * as ESTree from "estree"; -import type { - CustomRuleDefinitionType, - CustomRuleTypeDefinitions, - DeprecatedInfo, - LanguageOptions as GenericLanguageOptions, - RuleContext as CoreRuleContext, - RuleDefinition, - SourceRange, - TextSourceCode, - TraversalStep, - RulesConfig, - GlobalAccess, - GlobalsConfig, - LinterOptionsConfig, - EnvironmentConfig, - ObjectMetaProperties as CoreObjectMetaProperties, - Plugin as CorePlugin, - LintMessage as CoreLintMessage, - Processor as CoreProcessor, - ConfigObject, - LegacyConfigObject, - SeverityName, - SeverityLevel, - Severity as CoreSeverity, - EcmaVersion as CoreEcmaVersion, - ConfigOverride as CoreConfigOverride, - ProcessorFile as CoreProcessorFile, - JavaScriptParserOptionsConfig, - RulesMeta, - RuleConfig, - RuleTextEditor, - RuleTextEdit, - RuleVisitor, - BaseConfig as CoreBaseConfig, - RuleFixer as CoreRuleFixer, - ViolationReportBase, - ViolationMessage, - ViolationLocation, - SuggestionMessage, - LintSuggestion as CoreLintSuggestion, - JavaScriptSourceType, - HasRules as CoreHasRules, - SuggestedEditBase, - SuggestedEdit, - ViolationReport, -} from "@eslint/core"; -import { LegacyESLint } from "./use-at-your-own-risk.js"; - -//------------------------------------------------------------------------------ -// Helpers -//------------------------------------------------------------------------------ - -/** Adds matching `:exit` selectors for all properties of a `RuleVisitor`. */ -type WithExit = { - [Key in keyof RuleVisitorType as - | Key - | `${Key & string}:exit`]: RuleVisitorType[Key]; -}; - -//------------------------------------------------------------------------------ -// Exports -//------------------------------------------------------------------------------ - -export namespace AST { - type TokenType = - | "Boolean" - | "Null" - | "Identifier" - | "PrivateIdentifier" - | "Keyword" - | "Punctuator" - | "JSXIdentifier" - | "JSXText" - | "Numeric" - | "String" - | "Template" - | "RegularExpression"; - - interface Token { - type: TokenType; - value: string; - range: Range; - loc: SourceLocation; - } - - interface SourceLocation { - start: ESTree.Position; - end: ESTree.Position; - } - - type Range = [number, number]; - - interface Program extends ESTree.Program { - comments: ESTree.Comment[]; - tokens: Token[]; - loc: SourceLocation; - range: Range; - } -} - -export namespace Scope { - interface ScopeManager { - scopes: Scope[]; - globalScope: Scope | null; - - acquire(node: ESTree.Node, inner?: boolean): Scope | null; - - getDeclaredVariables(node: ESTree.Node): Variable[]; - } - - interface Scope { - type: - | "block" - | "catch" - | "class" - | "class-field-initializer" - | "class-static-block" - | "for" - | "function" - | "function-expression-name" - | "global" - | "module" - | "switch" - | "with" - | "TDZ"; - isStrict: boolean; - upper: Scope | null; - childScopes: Scope[]; - variableScope: Scope; - block: ESTree.Node; - variables: Variable[]; - set: Map; - references: Reference[]; - through: Reference[]; - functionExpressionScope: boolean; - implicit?: { - variables: Variable[]; - set: Map; - }; - } - - interface Variable { - name: string; - scope: Scope; - identifiers: ESTree.Identifier[]; - references: Reference[]; - defs: Definition[]; - } - - interface Reference { - identifier: ESTree.Identifier; - from: Scope; - resolved: Variable | null; - writeExpr: ESTree.Node | null; - init: boolean; - - isWrite(): boolean; - - isRead(): boolean; - - isWriteOnly(): boolean; - - isReadOnly(): boolean; - - isReadWrite(): boolean; - } - - type DefinitionType = - | { type: "CatchClause"; node: ESTree.CatchClause; parent: null } - | { - type: "ClassName"; - node: ESTree.ClassDeclaration | ESTree.ClassExpression; - parent: null; - } - | { - type: "FunctionName"; - node: ESTree.FunctionDeclaration | ESTree.FunctionExpression; - parent: null; - } - | { - type: "ImplicitGlobalVariable"; - node: - | ESTree.AssignmentExpression - | ESTree.ForInStatement - | ESTree.ForOfStatement; - parent: null; - } - | { - type: "ImportBinding"; - node: - | ESTree.ImportSpecifier - | ESTree.ImportDefaultSpecifier - | ESTree.ImportNamespaceSpecifier; - parent: ESTree.ImportDeclaration; - } - | { - type: "Parameter"; - node: - | ESTree.FunctionDeclaration - | ESTree.FunctionExpression - | ESTree.ArrowFunctionExpression; - parent: null; - } - | { type: "TDZ"; node: any; parent: null } - | { - type: "Variable"; - node: ESTree.VariableDeclarator; - parent: ESTree.VariableDeclaration; - }; - - type Definition = DefinitionType & { name: ESTree.Identifier }; -} - -// #region SourceCode - -export class SourceCode - implements - TextSourceCode<{ - LangOptions: Linter.LanguageOptions; - RootNode: AST.Program; - SyntaxElementWithLoc: AST.Token | ESTree.Node; - ConfigNode: ESTree.Comment; - }> -{ - text: string; - ast: AST.Program; - lines: string[]; - hasBOM: boolean; - parserServices: SourceCode.ParserServices; - scopeManager: Scope.ScopeManager; - visitorKeys: SourceCode.VisitorKeys; - - constructor(text: string, ast: AST.Program); - constructor(config: SourceCode.Config); - - static splitLines(text: string): string[]; - - getLoc(syntaxElement: AST.Token | ESTree.Node): ESTree.SourceLocation; - getRange(syntaxElement: AST.Token | ESTree.Node): SourceRange; - - getText( - node?: ESTree.Node, - beforeCount?: number, - afterCount?: number, - ): string; - - getLines(): string[]; - - getAllComments(): ESTree.Comment[]; - - getAncestors(node: ESTree.Node): ESTree.Node[]; - - getDeclaredVariables(node: ESTree.Node): Scope.Variable[]; - - /** @deprecated */ - getJSDocComment(node: ESTree.Node): ESTree.Comment | null; - - getNodeByRangeIndex(index: number): ESTree.Node | null; - - /** @deprecated Use `isSpaceBetween()` instead. */ - isSpaceBetweenTokens( - first: ESTree.Node | AST.Token, - second: ESTree.Node | AST.Token, - ): boolean; - - getLocFromIndex(index: number): ESTree.Position; - - getIndexFromLoc(location: ESTree.Position): number; - - // Inherited methods from TokenStore - // --------------------------------- - - getTokenByRangeStart( - offset: number, - options?: { includeComments: false }, - ): AST.Token | null; - getTokenByRangeStart( - offset: number, - options: { includeComments: boolean }, - ): AST.Token | ESTree.Comment | null; - - getFirstToken: SourceCode.UnaryNodeCursorWithSkipOptions; - - getFirstTokens: SourceCode.UnaryNodeCursorWithCountOptions; - - getLastToken: SourceCode.UnaryNodeCursorWithSkipOptions; - - getLastTokens: SourceCode.UnaryNodeCursorWithCountOptions; - - getTokenBefore: SourceCode.UnaryCursorWithSkipOptions; - - getTokensBefore: SourceCode.UnaryCursorWithCountOptions; - - getTokenAfter: SourceCode.UnaryCursorWithSkipOptions; - - getTokensAfter: SourceCode.UnaryCursorWithCountOptions; - - /** @deprecated Use `getTokenBefore()` instead. */ - getTokenOrCommentBefore( - node: ESTree.Node | AST.Token | ESTree.Comment, - skip?: number | undefined, - ): AST.Token | ESTree.Comment | null; - - /** @deprecated Use `getTokenAfter()` instead. */ - getTokenOrCommentAfter( - node: ESTree.Node | AST.Token | ESTree.Comment, - skip?: number | undefined, - ): AST.Token | ESTree.Comment | null; - - getFirstTokenBetween: SourceCode.BinaryCursorWithSkipOptions; - - getFirstTokensBetween: SourceCode.BinaryCursorWithCountOptions; - - getLastTokenBetween: SourceCode.BinaryCursorWithSkipOptions; - - getLastTokensBetween: SourceCode.BinaryCursorWithCountOptions; - - getTokensBetween: SourceCode.BinaryCursorWithCountOptions; - - getTokens: (( - node: ESTree.Node, - beforeCount?: number, - afterCount?: number, - ) => AST.Token[]) & - SourceCode.UnaryNodeCursorWithCountOptions; - - commentsExistBetween( - left: ESTree.Node | AST.Token | ESTree.Comment, - right: ESTree.Node | AST.Token | ESTree.Comment, - ): boolean; - - getCommentsBefore(nodeOrToken: ESTree.Node | AST.Token): ESTree.Comment[]; - - getCommentsAfter(nodeOrToken: ESTree.Node | AST.Token): ESTree.Comment[]; - - getCommentsInside(node: ESTree.Node): ESTree.Comment[]; - - getScope(node: ESTree.Node): Scope.Scope; - - isSpaceBetween( - first: ESTree.Node | AST.Token, - second: ESTree.Node | AST.Token, - ): boolean; - - isGlobalReference(node: ESTree.Identifier): boolean; - - markVariableAsUsed(name: string, refNode?: ESTree.Node): boolean; - - traverse(): Iterable; -} - -export namespace SourceCode { - interface Config { - text: string; - ast: AST.Program; - hasBOM?: boolean | undefined; - parserServices?: ParserServices | null | undefined; - scopeManager?: Scope.ScopeManager | null | undefined; - visitorKeys?: VisitorKeys | null | undefined; - } - - type ParserServices = any; - - interface VisitorKeys { - [nodeType: string]: string[]; - } - - interface UnaryNodeCursorWithSkipOptions { - ( - node: ESTree.Node, - options: - | ((token: AST.Token) => token is T) - | { - filter: (token: AST.Token) => token is T; - includeComments?: false | undefined; - skip?: number | undefined; - }, - ): T | null; - ( - node: ESTree.Node, - options: { - filter: ( - tokenOrComment: AST.Token | ESTree.Comment, - ) => tokenOrComment is T; - includeComments: boolean; - skip?: number | undefined; - }, - ): T | null; - ( - node: ESTree.Node, - options?: - | { - filter?: ((token: AST.Token) => boolean) | undefined; - includeComments?: false | undefined; - skip?: number | undefined; - } - | ((token: AST.Token) => boolean) - | number, - ): AST.Token | null; - ( - node: ESTree.Node, - options: { - filter?: - | ((token: AST.Token | ESTree.Comment) => boolean) - | undefined; - includeComments: boolean; - skip?: number | undefined; - }, - ): AST.Token | ESTree.Comment | null; - } - - interface UnaryNodeCursorWithCountOptions { - ( - node: ESTree.Node, - options: - | ((token: AST.Token) => token is T) - | { - filter: (token: AST.Token) => token is T; - includeComments?: false | undefined; - count?: number | undefined; - }, - ): T[]; - ( - node: ESTree.Node, - options: { - filter: ( - tokenOrComment: AST.Token | ESTree.Comment, - ) => tokenOrComment is T; - includeComments: boolean; - count?: number | undefined; - }, - ): T[]; - ( - node: ESTree.Node, - options?: - | { - filter?: ((token: AST.Token) => boolean) | undefined; - includeComments?: false | undefined; - count?: number | undefined; - } - | ((token: AST.Token) => boolean) - | number, - ): AST.Token[]; - ( - node: ESTree.Node, - options: { - filter?: - | ((token: AST.Token | ESTree.Comment) => boolean) - | undefined; - includeComments: boolean; - count?: number | undefined; - }, - ): Array; - } - - interface UnaryCursorWithSkipOptions { - ( - node: ESTree.Node | AST.Token | ESTree.Comment, - options: - | ((token: AST.Token) => token is T) - | { - filter: (token: AST.Token) => token is T; - includeComments?: false | undefined; - skip?: number | undefined; - }, - ): T | null; - ( - node: ESTree.Node | AST.Token | ESTree.Comment, - options: { - filter: ( - tokenOrComment: AST.Token | ESTree.Comment, - ) => tokenOrComment is T; - includeComments: boolean; - skip?: number | undefined; - }, - ): T | null; - ( - node: ESTree.Node | AST.Token | ESTree.Comment, - options?: - | { - filter?: ((token: AST.Token) => boolean) | undefined; - includeComments?: false | undefined; - skip?: number | undefined; - } - | ((token: AST.Token) => boolean) - | number, - ): AST.Token | null; - ( - node: ESTree.Node | AST.Token | ESTree.Comment, - options: { - filter?: - | ((token: AST.Token | ESTree.Comment) => boolean) - | undefined; - includeComments: boolean; - skip?: number | undefined; - }, - ): AST.Token | ESTree.Comment | null; - } - - interface UnaryCursorWithCountOptions { - ( - node: ESTree.Node | AST.Token | ESTree.Comment, - options: - | ((token: AST.Token) => token is T) - | { - filter: (token: AST.Token) => token is T; - includeComments?: false | undefined; - count?: number | undefined; - }, - ): T[]; - ( - node: ESTree.Node | AST.Token | ESTree.Comment, - options: { - filter: ( - tokenOrComment: AST.Token | ESTree.Comment, - ) => tokenOrComment is T; - includeComments: boolean; - count?: number | undefined; - }, - ): T[]; - ( - node: ESTree.Node | AST.Token | ESTree.Comment, - options?: - | { - filter?: ((token: AST.Token) => boolean) | undefined; - includeComments?: false | undefined; - count?: number | undefined; - } - | ((token: AST.Token) => boolean) - | number, - ): AST.Token[]; - ( - node: ESTree.Node | AST.Token | ESTree.Comment, - options: { - filter?: - | ((token: AST.Token | ESTree.Comment) => boolean) - | undefined; - includeComments: boolean; - count?: number | undefined; - }, - ): Array; - } - - interface BinaryCursorWithSkipOptions { - ( - left: ESTree.Node | AST.Token | ESTree.Comment, - right: ESTree.Node | AST.Token | ESTree.Comment, - options: - | ((token: AST.Token) => token is T) - | { - filter: (token: AST.Token) => token is T; - includeComments?: false | undefined; - skip?: number | undefined; - }, - ): T | null; - ( - left: ESTree.Node | AST.Token | ESTree.Comment, - right: ESTree.Node | AST.Token | ESTree.Comment, - options: { - filter: ( - tokenOrComment: AST.Token | ESTree.Comment, - ) => tokenOrComment is T; - includeComments: boolean; - skip?: number | undefined; - }, - ): T | null; - ( - left: ESTree.Node | AST.Token | ESTree.Comment, - right: ESTree.Node | AST.Token | ESTree.Comment, - options?: - | { - filter?: ((token: AST.Token) => boolean) | undefined; - includeComments?: false | undefined; - skip?: number | undefined; - } - | ((token: AST.Token) => boolean) - | number, - ): AST.Token | null; - ( - left: ESTree.Node | AST.Token | ESTree.Comment, - right: ESTree.Node | AST.Token | ESTree.Comment, - options: { - filter?: - | ((token: AST.Token | ESTree.Comment) => boolean) - | undefined; - includeComments: boolean; - skip?: number | undefined; - }, - ): AST.Token | ESTree.Comment | null; - } - - interface BinaryCursorWithCountOptions { - ( - left: ESTree.Node | AST.Token | ESTree.Comment, - right: ESTree.Node | AST.Token | ESTree.Comment, - options: - | ((token: AST.Token) => token is T) - | { - filter: (token: AST.Token) => token is T; - includeComments?: false | undefined; - count?: number | undefined; - }, - ): T[]; - ( - left: ESTree.Node | AST.Token | ESTree.Comment, - right: ESTree.Node | AST.Token | ESTree.Comment, - options: { - filter: ( - tokenOrComment: AST.Token | ESTree.Comment, - ) => tokenOrComment is T; - includeComments: boolean; - count?: number | undefined; - }, - ): T[]; - ( - left: ESTree.Node | AST.Token | ESTree.Comment, - right: ESTree.Node | AST.Token | ESTree.Comment, - options?: - | { - filter?: ((token: AST.Token) => boolean) | undefined; - includeComments?: false | undefined; - count?: number | undefined; - } - | ((token: AST.Token) => boolean) - | number, - ): AST.Token[]; - ( - left: ESTree.Node | AST.Token | ESTree.Comment, - right: ESTree.Node | AST.Token | ESTree.Comment, - options: { - filter?: - | ((token: AST.Token | ESTree.Comment) => boolean) - | undefined; - includeComments: boolean; - count?: number | undefined; - }, - ): Array; - } -} - -// #endregion - -export type JSSyntaxElement = { - type: string; - loc?: ESTree.SourceLocation | null | undefined; -}; - -export namespace Rule { - interface RuleModule - extends RuleDefinition<{ - LangOptions: Linter.LanguageOptions; - Code: SourceCode; - RuleOptions: any[]; - Visitor: RuleListener; - Node: JSSyntaxElement; - MessageIds: string; - ExtRuleDocs: {}; - }> { - create(context: RuleContext): RuleListener; - } - - type NodeTypes = ESTree.Node["type"]; - - interface NodeListener - extends WithExit< - { - [Node in Rule.Node as Node["type"]]?: - | ((node: Node) => void) - | undefined; - } & { - // A `Program` visitor's node type has no `parent` property. - Program?: ((node: AST.Program) => void) | undefined; - } - > {} - - interface NodeParentExtension { - parent: Node; - } - - type Node = - | (AST.Program & { parent: null }) - | (Exclude & NodeParentExtension); - - interface RuleListener extends NodeListener { - onCodePathStart?(codePath: CodePath, node: Node): void; - - onCodePathEnd?(codePath: CodePath, node: Node): void; - - onCodePathSegmentStart?(segment: CodePathSegment, node: Node): void; - - onCodePathSegmentEnd?(segment: CodePathSegment, node: Node): void; - - onUnreachableCodePathSegmentStart?( - segment: CodePathSegment, - node: Node, - ): void; - - onUnreachableCodePathSegmentEnd?( - segment: CodePathSegment, - node: Node, - ): void; - - onCodePathSegmentLoop?( - fromSegment: CodePathSegment, - toSegment: CodePathSegment, - node: Node, - ): void; - - [key: string]: - | ((codePath: CodePath, node: Node) => void) - | ((segment: CodePathSegment, node: Node) => void) - | (( - fromSegment: CodePathSegment, - toSegment: CodePathSegment, - node: Node, - ) => void) - | ((node: Node) => void) - | NodeListener[keyof NodeListener] - | undefined; - } - - type CodePathOrigin = - | "program" - | "function" - | "class-field-initializer" - | "class-static-block"; - - interface CodePath { - id: string; - origin: CodePathOrigin; - initialSegment: CodePathSegment; - finalSegments: CodePathSegment[]; - returnedSegments: CodePathSegment[]; - thrownSegments: CodePathSegment[]; - upper: CodePath | null; - childCodePaths: CodePath[]; - } - - interface CodePathSegment { - id: string; - nextSegments: CodePathSegment[]; - prevSegments: CodePathSegment[]; - reachable: boolean; - } - - type RuleMetaData = RulesMeta; - - interface RuleContext - extends CoreRuleContext<{ - LangOptions: Linter.LanguageOptions; - Code: SourceCode; - RuleOptions: any[]; - Node: JSSyntaxElement; - MessageIds: string; - }> {} - - type ReportFixer = CoreRuleFixer; - - /** @deprecated Use `ReportDescriptorOptions` instead. */ - type ReportDescriptorOptionsBase = ViolationReportBase; - - type SuggestionReportOptions = SuggestedEditBase; - type SuggestionDescriptorMessage = SuggestionMessage; - type SuggestionReportDescriptor = SuggestedEdit; - - // redundant with ReportDescriptorOptionsBase but kept for clarity - type ReportDescriptorOptions = ViolationReportBase; - - type ReportDescriptor = ViolationReport; - type ReportDescriptorMessage = ViolationMessage; - type ReportDescriptorLocation = ViolationLocation; - - type RuleFixer = RuleTextEditor; - type Fix = RuleTextEdit; -} - -export type JSRuleDefinitionTypeOptions = CustomRuleTypeDefinitions; - -export type JSRuleDefinition< - Options extends Partial = {}, -> = CustomRuleDefinitionType< - { - LangOptions: Linter.LanguageOptions; - Code: SourceCode; - Visitor: Rule.RuleListener; - Node: JSSyntaxElement; - }, - Options ->; - -// #region Linter - -export class Linter { - static readonly version: string; - - version: string; - - constructor(options?: { - cwd?: string | undefined; - configType?: "flat" | "eslintrc"; - }); - - verify( - code: SourceCode | string, - config: Linter.LegacyConfig | Linter.Config | Linter.Config[], - filename?: string, - ): Linter.LintMessage[]; - verify( - code: SourceCode | string, - config: Linter.LegacyConfig | Linter.Config | Linter.Config[], - options: Linter.LintOptions, - ): Linter.LintMessage[]; - - verifyAndFix( - code: string, - config: Linter.LegacyConfig | Linter.Config | Linter.Config[], - filename?: string, - ): Linter.FixReport; - verifyAndFix( - code: string, - config: Linter.LegacyConfig | Linter.Config | Linter.Config[], - options: Linter.FixOptions, - ): Linter.FixReport; - - getSourceCode(): SourceCode; - - defineRule(name: string, rule: Rule.RuleModule): void; - - defineRules(rules: { [name: string]: Rule.RuleModule }): void; - - getRules(): Map; - - defineParser(name: string, parser: Linter.Parser): void; - - getTimes(): Linter.Stats["times"]; - - getFixPassCount(): Linter.Stats["fixPasses"]; -} - -export namespace Linter { - /** - * The numeric severity level for a rule. - * - * - `0` means off. - * - `1` means warn. - * - `2` means error. - * - * @see [Rule Severities](https://eslint.org/docs/latest/use/configure/rules#rule-severities) - */ - type Severity = SeverityLevel; - - /** - * The human readable severity level for a rule. - * - * @see [Rule Severities](https://eslint.org/docs/latest/use/configure/rules#rule-severities) - */ - type StringSeverity = SeverityName; - - /** - * The numeric or human readable severity level for a rule. - * - * @see [Rule Severities](https://eslint.org/docs/latest/use/configure/rules#rule-severities) - */ - type RuleSeverity = CoreSeverity; - - /** - * An array containing the rule severity level, followed by the rule options. - * - * @see [Rules](https://eslint.org/docs/latest/use/configure/rules) - */ - type RuleSeverityAndOptions = [ - RuleSeverity, - ...Partial, - ]; - - /** - * The severity level for the rule or an array containing the rule severity level, followed by the rule options. - * - * @see [Rules](https://eslint.org/docs/latest/use/configure/rules) - */ - type RuleEntry = RuleConfig; - - /** - * The rules config object is a key/value map of rule names and their severity and options. - */ - type RulesRecord = RulesConfig; - - /** - * A configuration object that may have a `rules` block. - */ - type HasRules = - CoreHasRules; - - /** - * The ECMAScript version of the code being linted. - */ - type EcmaVersion = CoreEcmaVersion; - - /** - * The type of JavaScript source code. - */ - type SourceType = JavaScriptSourceType; - - /** - * ESLint legacy configuration. - * - * @see [ESLint Legacy Configuration](https://eslint.org/docs/latest/use/configure/) - */ - type BaseConfig< - Rules extends RulesConfig = RulesConfig, - OverrideRules extends RulesConfig = Rules, - > = CoreBaseConfig; - - /** - * The overwrites that apply more differing configuration to specific files or directories. - */ - type ConfigOverride = - CoreConfigOverride; - - /** - * ESLint legacy configuration. - * - * @see [ESLint Legacy Configuration](https://eslint.org/docs/latest/use/configure/) - */ - // https://github.com/eslint/eslint/blob/v8.57.0/conf/config-schema.js - type LegacyConfig< - Rules extends RulesConfig = RulesConfig, - OverrideRules extends RulesConfig = Rules, - > = LegacyConfigObject; - - /** - * Parser options. - * - * @see [Specifying Parser Options](https://eslint.org/docs/latest/use/configure/language-options#specifying-parser-options) - */ - type ParserOptions = JavaScriptParserOptionsConfig; - - /** - * Options used for linting code with `Linter#verify` and `Linter#verifyAndFix`. - */ - interface LintOptions { - filename?: string | undefined; - preprocess?: ((code: string) => string[]) | undefined; - postprocess?: - | ((problemLists: LintMessage[][]) => LintMessage[]) - | undefined; - filterCodeBlock?: - | ((filename: string, text: string) => boolean) - | undefined; - disableFixes?: boolean | undefined; - allowInlineConfig?: boolean | undefined; - reportUnusedDisableDirectives?: boolean | undefined; - } - - type LintSuggestion = CoreLintSuggestion; - type LintMessage = CoreLintMessage; - - interface LintSuppression { - kind: string; - justification: string; - } - - interface SuppressedLintMessage extends LintMessage { - /** The suppression info. */ - suppressions: LintSuppression[]; - } - - interface FixOptions extends LintOptions { - fix?: boolean | undefined; - } - - interface FixReport { - fixed: boolean; - output: string; - messages: LintMessage[]; - } - - // Temporarily loosen type for just flat config files (see https://github.com/DefinitelyTyped/DefinitelyTyped/pull/68232) - type NonESTreeParser = ESLint.ObjectMetaProperties & - ( - | { - parse(text: string, options?: any): unknown; - } - | { - parseForESLint( - text: string, - options?: any, - ): Omit & { - ast: unknown; - scopeManager?: unknown; - }; - } - ); - - type ESTreeParser = ESLint.ObjectMetaProperties & - ( - | { parse(text: string, options?: any): AST.Program } - | { parseForESLint(text: string, options?: any): ESLintParseResult } - ); - - type Parser = NonESTreeParser | ESTreeParser; - - interface ESLintParseResult { - /** The AST object. */ - ast: AST.Program; - - /** The services that the parser provides. */ - services?: SourceCode.ParserServices | undefined; - - /** The scope manager of the AST. */ - scopeManager?: Scope.ScopeManager | undefined; - - /** The visitor keys of the AST. */ - visitorKeys?: SourceCode.VisitorKeys | undefined; - } - - type ProcessorFile = CoreProcessorFile; - - // https://eslint.org/docs/latest/extend/plugins#processors-in-plugins - type Processor = - CoreProcessor; - - type Config = ConfigObject; - - /** @deprecated Use `Config` instead of `FlatConfig` */ - type FlatConfig = Config; - - type GlobalConf = GlobalAccess; - type Globals = GlobalsConfig; - - interface LanguageOptions extends GenericLanguageOptions { - /** - * The version of ECMAScript to support. May be any year (i.e., 2022) or - * version (i.e., 5). Set to "latest" for the most recent supported version. - * @default "latest" - */ - ecmaVersion?: EcmaVersion | undefined; - - /** - * The type of JavaScript source code. Possible values are "script" for - * traditional script files, "module" for ECMAScript modules (ESM), and - * "commonjs" for CommonJS files. (default: "module" for .js and .mjs - * files; "commonjs" for .cjs files) - */ - sourceType?: SourceType | undefined; - - /** - * An object specifying additional objects that should be added to the - * global scope during linting. - */ - globals?: Globals | undefined; - - /** - * An object containing a parse() or parseForESLint() method. - * If not configured, the default ESLint parser (Espree) will be used. - */ - parser?: Parser | undefined; - - /** - * An object specifying additional options that are passed directly to the - * parser() method on the parser. The available options are parser-dependent - */ - parserOptions?: Linter.ParserOptions | undefined; - } - - type LinterOptions = LinterOptionsConfig; - - /** - * Performance statistics. - */ - interface Stats { - /** - * The number of times ESLint has applied at least one fix after linting. - */ - fixPasses: number; - - /** - * The times spent on (parsing, fixing, linting) a file, where the linting refers to the timing information for each rule. - */ - times: { passes: TimePass[] }; - } - - interface TimePass { - /** - * The parse object containing all parse time information. - */ - parse: { total: number }; - - /** - * The rules object containing all lint time information for each rule. - */ - rules?: Record; - - /** - * The fix object containing all fix time information. - */ - fix: { total: number }; - - /** - * The total time that is spent on (parsing, fixing, linting) a file. - */ - total: number; - } -} - -// #endregion - -// #region ESLint - -export class ESLint { - static configType: "flat"; - - static readonly version: string; - - /** - * The default configuration that ESLint uses internally. This is provided for tooling that wants to calculate configurations using the same defaults as ESLint. - * Keep in mind that the default configuration may change from version to version, so you shouldn't rely on any particular keys or values to be present. - */ - static readonly defaultConfig: Linter.Config[]; - - static outputFixes(results: ESLint.LintResult[]): Promise; - - static getErrorResults(results: ESLint.LintResult[]): ESLint.LintResult[]; - - constructor(options?: ESLint.Options); - - lintFiles(patterns: string | string[]): Promise; - - lintText( - code: string, - options?: { - filePath?: string | undefined; - warnIgnored?: boolean | undefined; - }, - ): Promise; - - getRulesMetaForResults( - results: ESLint.LintResult[], - ): ESLint.LintResultData["rulesMeta"]; - - hasFlag(flag: string): boolean; - - calculateConfigForFile(filePath: string): Promise; - - findConfigFile(filePath?: string): Promise; - - isPathIgnored(filePath: string): Promise; - - loadFormatter(nameOrPath?: string): Promise; - - static fromOptionsModule(optionsURL: { - readonly href: string; - }): Promise; -} - -export namespace ESLint { - type ConfigData = Omit< - Linter.LegacyConfig, - "$schema" - >; - - type Environment = EnvironmentConfig; - type ObjectMetaProperties = CoreObjectMetaProperties; - type Plugin = CorePlugin; - - type FixType = "directive" | "problem" | "suggestion" | "layout"; - - type CacheStrategy = "content" | "metadata"; - - interface Options { - // File enumeration - cwd?: string | undefined; - errorOnUnmatchedPattern?: boolean | undefined; - globInputPaths?: boolean | undefined; - ignore?: boolean | undefined; - ignorePatterns?: string[] | null | undefined; - passOnNoPatterns?: boolean | undefined; - warnIgnored?: boolean | undefined; - - // Linting - allowInlineConfig?: boolean | undefined; - baseConfig?: Linter.Config | Linter.Config[] | null | undefined; - overrideConfig?: Linter.Config | Linter.Config[] | null | undefined; - overrideConfigFile?: string | true | null | undefined; - plugins?: Record | null | undefined; - ruleFilter?: - | ((arg: { - ruleId: string; - severity: Exclude; - }) => boolean) - | undefined; - stats?: boolean | undefined; - - // Autofix - fix?: boolean | ((message: Linter.LintMessage) => boolean) | undefined; - fixTypes?: FixType[] | null | undefined; - - // Cache-related - cache?: boolean | undefined; - cacheLocation?: string | undefined; - cacheStrategy?: CacheStrategy | undefined; - - // Other Options - concurrency?: number | "auto" | "off" | undefined; - flags?: string[] | undefined; - } - - interface LegacyOptions { - // File enumeration - cwd?: string | undefined; - errorOnUnmatchedPattern?: boolean | undefined; - extensions?: string[] | undefined; - globInputPaths?: boolean | undefined; - ignore?: boolean | undefined; - ignorePath?: string | undefined; - - // Linting - allowInlineConfig?: boolean | undefined; - baseConfig?: Linter.LegacyConfig | undefined; - overrideConfig?: Linter.LegacyConfig | undefined; - overrideConfigFile?: string | undefined; - plugins?: Record | undefined; - reportUnusedDisableDirectives?: Linter.StringSeverity | undefined; - resolvePluginsRelativeTo?: string | undefined; - rulePaths?: string[] | undefined; - useEslintrc?: boolean | undefined; - - // Autofix - fix?: boolean | ((message: Linter.LintMessage) => boolean) | undefined; - fixTypes?: FixType[] | null | undefined; - - // Cache-related - cache?: boolean | undefined; - cacheLocation?: string | undefined; - cacheStrategy?: CacheStrategy | undefined; - - // Other Options - flags?: string[] | undefined; - } - - /** A linting result. */ - interface LintResult { - /** The path to the file that was linted. */ - filePath: string; - - /** All of the messages for the result. */ - messages: Linter.LintMessage[]; - - /** All of the suppressed messages for the result. */ - suppressedMessages: Linter.SuppressedLintMessage[]; - - /** Number of errors for the result. */ - errorCount: number; - - /** Number of fatal errors for the result. */ - fatalErrorCount: number; - - /** Number of warnings for the result. */ - warningCount: number; - - /** Number of fixable errors for the result. */ - fixableErrorCount: number; - - /** Number of fixable warnings for the result. */ - fixableWarningCount: number; - - /** The source code of the file that was linted, with as many fixes applied as possible. */ - output?: string | undefined; - - /** The source code of the file that was linted. */ - source?: string | undefined; - - /** The performance statistics collected with the `stats` flag. */ - stats?: Linter.Stats | undefined; - - /** The list of used deprecated rules. */ - usedDeprecatedRules: DeprecatedRuleUse[]; - } - - /** - * Information provided when the maximum warning threshold is exceeded. - */ - interface MaxWarningsExceeded { - /** - * Number of warnings to trigger nonzero exit code. - */ - maxWarnings: number; - - /** - * Number of warnings found while linting. - */ - foundWarnings: number; - } - - interface LintResultData { - cwd: string; - maxWarningsExceeded?: MaxWarningsExceeded | undefined; - rulesMeta: { - [ruleId: string]: Rule.RuleMetaData; - }; - } - - /** - * Information about deprecated rules. - */ - interface DeprecatedRuleUse { - /** - * The rule ID. - */ - ruleId: string; - - /** - * The rule IDs that replace this deprecated rule. - */ - replacedBy: string[]; - - /** - * The raw deprecated info provided by the rule. - * - Undefined if the rule's `meta.deprecated` property is a boolean. - * - Unset when using the legacy eslintrc configuration. - */ - info?: DeprecatedInfo | undefined; - } - - /** - * Metadata about results for formatters. - */ - interface ResultsMeta { - /** - * Present if the maxWarnings threshold was exceeded. - */ - maxWarningsExceeded?: MaxWarningsExceeded | undefined; - } - - /** The type of an object resolved by {@link ESLint.loadFormatter}. */ - interface LoadedFormatter { - /** - * Used to call the underlying formatter. - * @param results An array of lint results to format. - * @param resultsMeta An object with an optional `maxWarningsExceeded` property that will be - * passed to the underlying formatter function along with other properties set by ESLint. - * This argument can be omitted if `maxWarningsExceeded` is not needed. - * @return The formatter output. - */ - format( - results: LintResult[], - resultsMeta?: ResultsMeta, - ): string | Promise; - } - - // The documented type name is `LoadedFormatter`, but `Formatter` has been historically more used. - type Formatter = LoadedFormatter; - - /** - * The expected signature of a custom formatter. - * @param results An array of lint results to format. - * @param context Additional information for the formatter. - * @return The formatter output. - */ - type FormatterFunction = ( - results: LintResult[], - context: LintResultData, - ) => string | Promise; - - // Docs reference the types by those name - type EditInfo = Rule.Fix; -} - -// #endregion - -export function loadESLint(options: { - useFlatConfig: true; -}): Promise; -export function loadESLint(options: { - useFlatConfig: false; -}): Promise; -export function loadESLint(options?: { - useFlatConfig?: boolean | undefined; -}): Promise; - -// #region RuleTester - -export class RuleTester { - static describe: ((...args: any) => any) | null; - static it: ((...args: any) => any) | null; - static itOnly: ((...args: any) => any) | null; - - constructor(config?: Linter.Config); - - run( - name: string, - rule: RuleDefinition, - tests: { - valid: Array; - invalid: RuleTester.InvalidTestCase[]; - }, - ): void; - - static only( - item: string | RuleTester.ValidTestCase | RuleTester.InvalidTestCase, - ): RuleTester.ValidTestCase | RuleTester.InvalidTestCase; -} - -export namespace RuleTester { - interface ValidTestCase { - name?: string; - code: string; - options?: any; - filename?: string | undefined; - only?: boolean; - languageOptions?: Linter.LanguageOptions | undefined; - settings?: { [name: string]: any } | undefined; - before?: () => void; - after?: () => void; - } - - interface SuggestionOutput { - messageId?: string; - desc?: string; - data?: Record | undefined; - output: string; - } - - interface InvalidTestCase extends ValidTestCase { - errors: number | Array; - output?: string | null | undefined; - } - - interface TestCaseError { - message?: string | RegExp; - messageId?: string; - /** - * @deprecated `type` is deprecated and will be removed in the next major version. - */ - type?: string | undefined; - data?: any; - line?: number | undefined; - column?: number | undefined; - endLine?: number | undefined; - endColumn?: number | undefined; - suggestions?: SuggestionOutput[] | undefined; - } -} - -// #endregion diff --git a/node_modules/eslint/lib/types/rules.d.ts b/node_modules/eslint/lib/types/rules.d.ts deleted file mode 100644 index e74eaf76d..000000000 --- a/node_modules/eslint/lib/types/rules.d.ts +++ /dev/null @@ -1,5589 +0,0 @@ -/** - * @fileoverview This file contains the rule types for ESLint. It was initially extracted - * from the `@types/eslint` package. - */ - -/* - * MIT License - * Copyright (c) Microsoft Corporation. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE - */ - -//------------------------------------------------------------------------------ -// Imports -//------------------------------------------------------------------------------ - -import { Linter } from "./index"; - -//----------------------------------------------------------------------------- -// Helper types -//----------------------------------------------------------------------------- - -interface NoRestrictedImportPathCommonOptions { - name: string; - message?: string; - allowTypeImports?: boolean; -} - -type EitherImportNamesOrAllowImportName = - | { importNames?: string[]; allowImportNames?: never } - | { allowImportNames?: string[]; importNames?: never }; - -type ValidNoRestrictedImportPathOptions = NoRestrictedImportPathCommonOptions & - EitherImportNamesOrAllowImportName; - -interface NoRestrictedImportPatternCommonOptions { - message?: string; - caseSensitive?: boolean; - allowTypeImports?: boolean; -} - -// Base type for group or regex constraint, ensuring mutual exclusivity -type EitherGroupOrRegEx = - | { group: string[]; regex?: never } - | { regex: string; group?: never }; - -// Base type for import name specifiers, ensuring mutual exclusivity -type EitherNameSpecifiers = - | { - importNames?: string[]; - importNamePattern?: string; - allowImportNames?: never; - allowImportNamePattern?: never; - } - | { - allowImportNames?: string[]; - importNames?: never; - importNamePattern?: never; - allowImportNamePattern?: never; - } - | { - allowImportNamePattern?: string; - importNames?: never; - allowImportNames?: never; - importNamePattern?: never; - }; - -// Adds oneOf and not constraints, ensuring group or regex are present and mutually exclusive sets for importNames, allowImportNames, etc., as per the schema. -type ValidNoRestrictedImportPatternOptions = - NoRestrictedImportPatternCommonOptions & - EitherGroupOrRegEx & - EitherNameSpecifiers; - -interface CapitalizedCommentsCommonOptions { - ignorePattern?: string; - /** - * @default false - */ - ignoreInlineComments?: boolean; - /** - * @default false - */ - ignoreConsecutiveComments?: boolean; -} - -//----------------------------------------------------------------------------- -// Public types -//----------------------------------------------------------------------------- - -export interface ESLintRules extends Linter.RulesRecord { - /** - * Rule to enforce getter and setter pairs in objects and classes. - * - * @since 0.22.0 - * @see https://eslint.org/docs/latest/rules/accessor-pairs - */ - "accessor-pairs": Linter.RuleEntry< - [ - Partial<{ - /** - * @default true - */ - setWithoutGet: boolean; - /** - * @default false - */ - getWithoutSet: boolean; - /** - * @default true - */ - enforceForClassMembers: boolean; - /** - * @default false - */ - enforceForTSTypes: boolean; - }>, - ] - >; - - /** - * Rule to enforce linebreaks after opening and before closing array brackets. - * - * @since 4.0.0-alpha.1 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`array-bracket-newline`](https://eslint.style/rules/array-bracket-newline) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/array-bracket-newline - */ - "array-bracket-newline": Linter.RuleEntry< - [ - | "always" - | "never" - | "consistent" - | Partial<{ - /** - * @default true - */ - multiline: boolean; - /** - * @default null - */ - minItems: number | null; - }>, - ] - >; - - /** - * Rule to enforce consistent spacing inside array brackets. - * - * @since 0.24.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`array-bracket-spacing`](https://eslint.style/rules/array-bracket-spacing) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/array-bracket-spacing - */ - "array-bracket-spacing": - | Linter.RuleEntry< - [ - "never", - Partial<{ - /** - * @default false - */ - singleValue: boolean; - /** - * @default false - */ - objectsInArrays: boolean; - /** - * @default false - */ - arraysInArrays: boolean; - }>, - ] - > - | Linter.RuleEntry< - [ - "always", - Partial<{ - /** - * @default true - */ - singleValue: boolean; - /** - * @default true - */ - objectsInArrays: boolean; - /** - * @default true - */ - arraysInArrays: boolean; - }>, - ] - >; - - /** - * Rule to enforce `return` statements in callbacks of array methods. - * - * @since 2.0.0-alpha-1 - * @see https://eslint.org/docs/latest/rules/array-callback-return - */ - "array-callback-return": Linter.RuleEntry< - [ - Partial<{ - /** - * @default false - */ - allowImplicit: boolean; - /** - * @default false - */ - checkForEach: boolean; - /** - * @default false - */ - allowVoid: boolean; - }>, - ] - >; - - /** - * Rule to enforce line breaks after each array element. - * - * @since 4.0.0-rc.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`array-element-newline`](https://eslint.style/rules/array-element-newline) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/array-element-newline - */ - "array-element-newline": Linter.RuleEntry< - [ - | "always" - | "never" - | "consistent" - | Partial<{ - /** - * @default true - */ - multiline: boolean; - /** - * @default null - */ - minItems: number | null; - }>, - ] - >; - - /** - * Rule to require braces around arrow function bodies. - * - * @since 1.8.0 - * @see https://eslint.org/docs/latest/rules/arrow-body-style - */ - "arrow-body-style": - | Linter.RuleEntry< - [ - "as-needed", - Partial<{ - /** - * @default false - */ - requireReturnForObjectLiteral: boolean; - }>, - ] - > - | Linter.RuleEntry<["always" | "never"]>; - - /** - * Rule to require parentheses around arrow function arguments. - * - * @since 1.0.0-rc-1 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`arrow-parens`](https://eslint.style/rules/arrow-parens) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/arrow-parens - */ - "arrow-parens": - | Linter.RuleEntry<["always"]> - | Linter.RuleEntry< - [ - "as-needed", - Partial<{ - /** - * @default false - */ - requireForBlockBody: boolean; - }>, - ] - >; - - /** - * Rule to enforce consistent spacing before and after the arrow in arrow functions. - * - * @since 1.0.0-rc-1 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`arrow-spacing`](https://eslint.style/rules/arrow-spacing) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/arrow-spacing - */ - "arrow-spacing": Linter.RuleEntry<[]>; - - /** - * Rule to enforce the use of variables within the scope they are defined. - * - * @since 0.1.0 - * @see https://eslint.org/docs/latest/rules/block-scoped-var - */ - "block-scoped-var": Linter.RuleEntry<[]>; - - /** - * Rule to disallow or enforce spaces inside of blocks after opening block and before closing block. - * - * @since 1.2.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`block-spacing`](https://eslint.style/rules/block-spacing) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/block-spacing - */ - "block-spacing": Linter.RuleEntry<["always" | "never"]>; - - /** - * Rule to enforce consistent brace style for blocks. - * - * @since 0.0.7 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`brace-style`](https://eslint.style/rules/brace-style) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/brace-style - */ - "brace-style": Linter.RuleEntry< - [ - "1tbs" | "stroustrup" | "allman", - Partial<{ - /** - * @default false - */ - allowSingleLine: boolean; - }>, - ] - >; - - /** - * Rule to require `return` statements after callbacks. - * - * @since 1.0.0-rc-1 - * @deprecated since 7.0.0. - * Node.js rules were moved out of ESLint core. - * Please, use [`callback-return`](https://github.com/eslint-community/eslint-plugin-n/tree/master/docs/rules/callback-return.md) in [`eslint-plugin-n`](https://github.com/eslint-community/eslint-plugin-n). - * @see https://eslint.org/docs/latest/rules/callback-return - */ - "callback-return": Linter.RuleEntry<[string[]]>; - - /** - * Rule to enforce camelcase naming convention. - * - * @since 0.0.2 - * @see https://eslint.org/docs/latest/rules/camelcase - */ - camelcase: Linter.RuleEntry< - [ - Partial<{ - /** - * @default 'always' - */ - properties: "always" | "never"; - /** - * @default false - */ - ignoreDestructuring: boolean; - /** - * @since 6.7.0 - * @default false - */ - ignoreImports: boolean; - /** - * @since 7.4.0 - * @default false - */ - ignoreGlobals: boolean; - /** - * @remarks - * Also accept for regular expression patterns - */ - allow: string[]; - }>, - ] - >; - - /** - * Rule to enforce or disallow capitalization of the first letter of a comment. - * - * @since 3.11.0 - * @see https://eslint.org/docs/latest/rules/capitalized-comments - */ - "capitalized-comments": Linter.RuleEntry< - [ - "always" | "never", - ( - | CapitalizedCommentsCommonOptions - | Partial<{ - line: CapitalizedCommentsCommonOptions; - block: CapitalizedCommentsCommonOptions; - }> - ), - ] - >; - - /** - * Rule to enforce that class methods utilize `this`. - * - * @since 3.4.0 - * @see https://eslint.org/docs/latest/rules/class-methods-use-this - */ - "class-methods-use-this": Linter.RuleEntry< - [ - Partial<{ - exceptMethods: string[]; - /** - * @default true - */ - enforceForClassFields: boolean; - /** - * @default false - */ - ignoreOverrideMethods: boolean; - ignoreClassesWithImplements: "all" | "public-fields"; - }>, - ] - >; - - /** - * Rule to require or disallow trailing commas. - * - * @since 0.16.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`comma-dangle`](https://eslint.style/rules/comma-dangle) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/comma-dangle - */ - "comma-dangle": Linter.RuleEntry< - [ - | "never" - | "always" - | "always-multiline" - | "only-multiline" - | Partial<{ - /** - * @default 'never' - */ - arrays: - | "never" - | "always" - | "always-multiline" - | "only-multiline"; - /** - * @default 'never' - */ - objects: - | "never" - | "always" - | "always-multiline" - | "only-multiline"; - /** - * @default 'never' - */ - imports: - | "never" - | "always" - | "always-multiline" - | "only-multiline"; - /** - * @default 'never' - */ - exports: - | "never" - | "always" - | "always-multiline" - | "only-multiline"; - /** - * @default 'never' - */ - functions: - | "never" - | "always" - | "always-multiline" - | "only-multiline"; - }>, - ] - >; - - /** - * Rule to enforce consistent spacing before and after commas. - * - * @since 0.9.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`comma-spacing`](https://eslint.style/rules/comma-spacing) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/comma-spacing - */ - "comma-spacing": Linter.RuleEntry< - [ - Partial<{ - /** - * @default false - */ - before: boolean; - /** - * @default true - */ - after: boolean; - }>, - ] - >; - - /** - * Rule to enforce consistent comma style. - * - * @since 0.9.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`comma-style`](https://eslint.style/rules/comma-style) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/comma-style - */ - "comma-style": Linter.RuleEntry< - [ - "last" | "first", - Partial<{ - exceptions: Record; - }>, - ] - >; - - /** - * Rule to enforce a maximum cyclomatic complexity allowed in a program. - * - * @since 0.0.9 - * @see https://eslint.org/docs/latest/rules/complexity - */ - complexity: Linter.RuleEntry< - [ - | Partial<{ - /** - * @default 20 - */ - max: number; - /** - * @deprecated - * @default 20 - */ - maximum: number; - /** - * @default "classic" - * @since 9.12.0 - */ - variant: "classic" | "modified"; - }> - | number, - ] - >; - - /** - * Rule to enforce consistent spacing inside computed property brackets. - * - * @since 0.23.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`computed-property-spacing`](https://eslint.style/rules/computed-property-spacing) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/computed-property-spacing - */ - "computed-property-spacing": Linter.RuleEntry<["never" | "always"]>; - - /** - * Rule to require `return` statements to either always or never specify values. - * - * @since 0.4.0 - * @see https://eslint.org/docs/latest/rules/consistent-return - */ - "consistent-return": Linter.RuleEntry< - [ - Partial<{ - /** - * @default false - */ - treatUndefinedAsUnspecified: boolean; - }>, - ] - >; - - /** - * Rule to enforce consistent naming when capturing the current execution context. - * - * @since 0.0.9 - * @see https://eslint.org/docs/latest/rules/consistent-this - */ - "consistent-this": Linter.RuleEntry<[...string[]]>; - - /** - * Rule to require `super()` calls in constructors. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 0.24.0 - * @see https://eslint.org/docs/latest/rules/constructor-super - */ - "constructor-super": Linter.RuleEntry<[]>; - - /** - * Rule to enforce consistent brace style for all control statements. - * - * @since 0.0.2 - * @see https://eslint.org/docs/latest/rules/curly - */ - curly: Linter.RuleEntry< - ["all"] | ["multi" | "multi-line" | "multi-or-nest", "consistent"?] - >; - - /** - * Rule to require `default` cases in `switch` statements. - * - * @since 0.6.0 - * @see https://eslint.org/docs/latest/rules/default-case - */ - "default-case": Linter.RuleEntry< - [ - Partial<{ - /** - * @default '^no default$' - */ - commentPattern: string; - }>, - ] - >; - - /** - * Rule to enforce `default` clauses in `switch` statements to be last. - * - * @since 7.0.0-alpha.0 - * @see https://eslint.org/docs/latest/rules/default-case-last - */ - "default-case-last": Linter.RuleEntry<[]>; - - /** - * Rule to enforce default parameters to be last. - * - * @since 6.4.0 - * @see https://eslint.org/docs/latest/rules/default-param-last - */ - "default-param-last": Linter.RuleEntry<[]>; - - /** - * Rule to enforce consistent newlines before and after dots. - * - * @since 0.21.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`dot-location`](https://eslint.style/rules/dot-location) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/dot-location - */ - "dot-location": Linter.RuleEntry<["object" | "property"]>; - - /** - * Rule to enforce dot notation whenever possible. - * - * @since 0.0.7 - * @see https://eslint.org/docs/latest/rules/dot-notation - */ - "dot-notation": Linter.RuleEntry< - [ - Partial<{ - /** - * @default true - */ - allowKeywords: boolean; - allowPattern: string; - }>, - ] - >; - - /** - * Rule to require or disallow newline at the end of files. - * - * @since 0.7.1 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`eol-last`](https://eslint.style/rules/eol-last) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/eol-last - */ - "eol-last": Linter.RuleEntry< - [ - "always" | "never", // | 'unix' | 'windows' - ] - >; - - /** - * Rule to require the use of `===` and `!==`. - * - * @since 0.0.2 - * @see https://eslint.org/docs/latest/rules/eqeqeq - */ - eqeqeq: - | Linter.RuleEntry< - [ - "always", - Partial<{ - /** - * @default 'always' - */ - null: "always" | "never" | "ignore"; - }>, - ] - > - | Linter.RuleEntry<["smart" | "allow-null"]>; - - /** - * Rule to enforce `for` loop update clause moving the counter in the right direction. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 4.0.0-beta.0 - * @see https://eslint.org/docs/latest/rules/for-direction - */ - "for-direction": Linter.RuleEntry<[]>; - - /** - * Rule to require or disallow spacing between function identifiers and their invocations. - * - * @since 3.3.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`function-call-spacing`](https://eslint.style/rules/function-call-spacing) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/func-call-spacing - */ - "func-call-spacing": Linter.RuleEntry<["never" | "always"]>; - - /** - * Rule to require function names to match the name of the variable or property to which they are assigned. - * - * @since 3.8.0 - * @see https://eslint.org/docs/latest/rules/func-name-matching - */ - "func-name-matching": - | Linter.RuleEntry< - [ - "always" | "never", - Partial<{ - /** - * @default false - */ - considerPropertyDescriptor: boolean; - /** - * @default false - */ - includeCommonJSModuleExports: boolean; - }>, - ] - > - | Linter.RuleEntry< - [ - Partial<{ - /** - * @default false - */ - considerPropertyDescriptor: boolean; - /** - * @default false - */ - includeCommonJSModuleExports: boolean; - }>, - ] - >; - - /** - * Rule to require or disallow named `function` expressions. - * - * @since 0.4.0 - * @see https://eslint.org/docs/latest/rules/func-names - */ - "func-names": Linter.RuleEntry< - [ - "always" | "as-needed" | "never", - Partial<{ - generators: "always" | "as-needed" | "never"; - }>, - ] - >; - - /** - * Rule to enforce the consistent use of either `function` declarations or expressions assigned to variables. - * - * @since 0.2.0 - * @see https://eslint.org/docs/latest/rules/func-style - */ - "func-style": Linter.RuleEntry< - [ - "expression" | "declaration", - Partial<{ - /** - * @default false - */ - allowArrowFunctions: boolean; - /** - * @default false - */ - allowTypeAnnotation: boolean; - overrides: { - namedExports: "declaration" | "expression" | "ignore"; - }; - }>, - ] - >; - - /** - * Rule to enforce line breaks between arguments of a function call. - * - * @since 6.2.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`function-call-argument-newline`](https://eslint.style/rules/function-call-argument-newline) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/function-call-argument-newline - */ - "function-call-argument-newline": Linter.RuleEntry< - [ - /** - * @default "always" - */ - "always" | "never" | "consistent", - ] - >; - - /** - * Rule to enforce consistent line breaks inside function parentheses. - * - * @since 4.6.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`function-paren-newline`](https://eslint.style/rules/function-paren-newline) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/function-paren-newline - */ - "function-paren-newline": Linter.RuleEntry< - [ - | "always" - | "never" - | "multiline" - | "multiline-arguments" - | "consistent" - | Partial<{ - minItems: number; - }>, - ] - >; - - /** - * Rule to enforce consistent spacing around `*` operators in generator functions. - * - * @since 0.17.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`generator-star-spacing`](https://eslint.style/rules/generator-star-spacing) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/generator-star-spacing - */ - "generator-star-spacing": Linter.RuleEntry< - [ - | Partial<{ - before: boolean; - after: boolean; - named: - | Partial<{ - before: boolean; - after: boolean; - }> - | "before" - | "after" - | "both" - | "neither"; - anonymous: - | Partial<{ - before: boolean; - after: boolean; - }> - | "before" - | "after" - | "both" - | "neither"; - method: - | Partial<{ - before: boolean; - after: boolean; - }> - | "before" - | "after" - | "both" - | "neither"; - }> - | "before" - | "after" - | "both" - | "neither", - ] - >; - - /** - * Rule to enforce `return` statements in getters. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 4.2.0 - * @see https://eslint.org/docs/latest/rules/getter-return - */ - "getter-return": Linter.RuleEntry< - [ - Partial<{ - /** - * @default false - */ - allowImplicit: boolean; - }>, - ] - >; - - /** - * Rule to require `require()` calls to be placed at top-level module scope. - * - * @since 1.4.0 - * @deprecated since 7.0.0. - * Node.js rules were moved out of ESLint core. - * Please, use [`global-require`](https://github.com/eslint-community/eslint-plugin-n/tree/master/docs/rules/global-require.md) in [`eslint-plugin-n`](https://github.com/eslint-community/eslint-plugin-n). - * @see https://eslint.org/docs/latest/rules/global-require - */ - "global-require": Linter.RuleEntry<[]>; - - /** - * Rule to require grouped accessor pairs in object literals and classes. - * - * @since 6.7.0 - * @see https://eslint.org/docs/latest/rules/grouped-accessor-pairs - */ - "grouped-accessor-pairs": Linter.RuleEntry< - [ - "anyOrder" | "getBeforeSet" | "setBeforeGet", - Partial<{ - /** - * @default false - */ - enforceForTSTypes: boolean; - }>, - ] - >; - - /** - * Rule to require `for-in` loops to include an `if` statement. - * - * @since 0.0.6 - * @see https://eslint.org/docs/latest/rules/guard-for-in - */ - "guard-for-in": Linter.RuleEntry<[]>; - - /** - * Rule to require error handling in callbacks. - * - * @since 0.4.5 - * @deprecated since 7.0.0. - * Node.js rules were moved out of ESLint core. - * Please, use [`handle-callback-err`](https://github.com/eslint-community/eslint-plugin-n/tree/master/docs/rules/handle-callback-err.md) in [`eslint-plugin-n`](https://github.com/eslint-community/eslint-plugin-n). - * @see https://eslint.org/docs/latest/rules/handle-callback-err - */ - "handle-callback-err": Linter.RuleEntry<[string]>; - - /** - * Rule to disallow specified identifiers. - * - * @since 2.0.0-beta.2 - * @deprecated since 7.5.0. - * The rule was renamed. - * Please, use [`id-denylist`](https://eslint.org/docs/rules/id-denylist). - * @see https://eslint.org/docs/latest/rules/id-blacklist - */ - "id-blacklist": Linter.RuleEntry<[...string[]]>; - - /** - * Rule to disallow specified identifiers. - * - * @since 7.4.0 - * @see https://eslint.org/docs/latest/rules/id-denylist - */ - "id-denylist": Linter.RuleEntry; - - /** - * Rule to enforce minimum and maximum identifier lengths. - * - * @since 1.0.0 - * @see https://eslint.org/docs/latest/rules/id-length - */ - "id-length": Linter.RuleEntry< - [ - Partial<{ - /** - * @default 2 - */ - min: number; - /** - * @default Infinity - */ - max: number; - /** - * @default 'always' - */ - properties: "always" | "never"; - exceptions: string[]; - exceptionPatterns: string[]; - }>, - ] - >; - - /** - * Rule to require identifiers to match a specified regular expression. - * - * @since 1.0.0 - * @see https://eslint.org/docs/latest/rules/id-match - */ - "id-match": Linter.RuleEntry< - [ - string, - Partial<{ - /** - * @default false - */ - properties: boolean; - /** - * @default false - */ - classFields: boolean; - /** - * @default false - */ - onlyDeclarations: boolean; - /** - * @default false - */ - ignoreDestructuring: boolean; - }>, - ] - >; - - /** - * Rule to enforce the location of arrow function bodies. - * - * @since 4.12.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`implicit-arrow-linebreak`](https://eslint.style/rules/implicit-arrow-linebreak) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/implicit-arrow-linebreak - */ - "implicit-arrow-linebreak": Linter.RuleEntry<["beside" | "below"]>; - - /** - * Rule to enforce consistent indentation. - * - * @since 0.14.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`indent`](https://eslint.style/rules/indent) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/indent - */ - indent: Linter.RuleEntry< - [ - number | "tab", - Partial<{ - /** - * @default 0 - */ - SwitchCase: number; - /** - * @default 1 - */ - VariableDeclarator: - | Partial<{ - /** - * @default 1 - */ - var: number | "first"; - /** - * @default 1 - */ - let: number | "first"; - /** - * @default 1 - */ - const: number | "first"; - }> - | number - | "first"; - /** - * @default 1 - */ - outerIIFEBody: number; - /** - * @default 1 - */ - MemberExpression: number | "off"; - /** - * @default { parameters: 1, body: 1 } - */ - FunctionDeclaration: Partial<{ - /** - * @default 1 - */ - parameters: number | "first" | "off"; - /** - * @default 1 - */ - body: number; - }>; - /** - * @default { parameters: 1, body: 1 } - */ - FunctionExpression: Partial<{ - /** - * @default 1 - */ - parameters: number | "first" | "off"; - /** - * @default 1 - */ - body: number; - }>; - /** - * @default { arguments: 1 } - */ - CallExpression: Partial<{ - /** - * @default 1 - */ - arguments: number | "first" | "off"; - }>; - /** - * @default 1 - */ - ArrayExpression: number | "first" | "off"; - /** - * @default 1 - */ - ObjectExpression: number | "first" | "off"; - /** - * @default 1 - */ - ImportDeclaration: number | "first" | "off"; - /** - * @default false - */ - flatTernaryExpressions: boolean; - ignoredNodes: string[]; - /** - * @default false - */ - ignoreComments: boolean; - }>, - ] - >; - - /** - * Rule to enforce consistent indentation. - * - * @since 4.0.0-alpha.0 - * @deprecated since 4.0.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`indent`](https://eslint.style/rules/indent) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/indent-legacy - */ - "indent-legacy": Linter.RuleEntry< - [ - number | "tab", - Partial<{ - /** - * @default 0 - */ - SwitchCase: number; - /** - * @default 1 - */ - VariableDeclarator: - | Partial<{ - /** - * @default 1 - */ - var: number | "first"; - /** - * @default 1 - */ - let: number | "first"; - /** - * @default 1 - */ - const: number | "first"; - }> - | number - | "first"; - /** - * @default 1 - */ - outerIIFEBody: number; - /** - * @default 1 - */ - MemberExpression: number | "off"; - /** - * @default { parameters: 1, body: 1 } - */ - FunctionDeclaration: Partial<{ - /** - * @default 1 - */ - parameters: number | "first" | "off"; - /** - * @default 1 - */ - body: number; - }>; - /** - * @default { parameters: 1, body: 1 } - */ - FunctionExpression: Partial<{ - /** - * @default 1 - */ - parameters: number | "first" | "off"; - /** - * @default 1 - */ - body: number; - }>; - /** - * @default { arguments: 1 } - */ - CallExpression: Partial<{ - /** - * @default 1 - */ - arguments: number | "first" | "off"; - }>; - /** - * @default 1 - */ - ArrayExpression: number | "first" | "off"; - /** - * @default 1 - */ - ObjectExpression: number | "first" | "off"; - /** - * @default 1 - */ - ImportDeclaration: number | "first" | "off"; - /** - * @default false - */ - flatTernaryExpressions: boolean; - ignoredNodes: string[]; - /** - * @default false - */ - ignoreComments: boolean; - }>, - ] - >; - - /** - * Rule to require or disallow initialization in variable declarations. - * - * @since 1.0.0-rc-1 - * @see https://eslint.org/docs/latest/rules/init-declarations - */ - "init-declarations": - | Linter.RuleEntry<["always"]> - | Linter.RuleEntry< - [ - "never", - Partial<{ - ignoreForLoopInit: boolean; - }>, - ] - >; - - /** - * Rule to enforce the consistent use of either double or single quotes in JSX attributes. - * - * @since 1.4.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`jsx-quotes`](https://eslint.style/rules/jsx-quotes) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/jsx-quotes - */ - "jsx-quotes": Linter.RuleEntry<["prefer-double" | "prefer-single"]>; - - /** - * Rule to enforce consistent spacing between keys and values in object literal properties. - * - * @since 0.9.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`key-spacing`](https://eslint.style/rules/key-spacing) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/key-spacing - */ - "key-spacing": Linter.RuleEntry< - [ - | Partial< - | { - /** - * @default false - */ - beforeColon: boolean; - /** - * @default true - */ - afterColon: boolean; - /** - * @default 'strict' - */ - mode: "strict" | "minimum"; - align: - | Partial<{ - /** - * @default false - */ - beforeColon: boolean; - /** - * @default true - */ - afterColon: boolean; - /** - * @default 'colon' - */ - on: "value" | "colon"; - /** - * @default 'strict' - */ - mode: "strict" | "minimum"; - }> - | "value" - | "colon"; - } - | { - singleLine?: - | Partial<{ - /** - * @default false - */ - beforeColon: boolean; - /** - * @default true - */ - afterColon: boolean; - /** - * @default 'strict' - */ - mode: "strict" | "minimum"; - }> - | undefined; - multiLine?: - | Partial<{ - /** - * @default false - */ - beforeColon: boolean; - /** - * @default true - */ - afterColon: boolean; - /** - * @default 'strict' - */ - mode: "strict" | "minimum"; - align: - | Partial<{ - /** - * @default false - */ - beforeColon: boolean; - /** - * @default true - */ - afterColon: boolean; - /** - * @default 'colon' - */ - on: "value" | "colon"; - /** - * @default 'strict' - */ - mode: "strict" | "minimum"; - }> - | "value" - | "colon"; - }> - | undefined; - } - > - | { - align: Partial<{ - /** - * @default false - */ - beforeColon: boolean; - /** - * @default true - */ - afterColon: boolean; - /** - * @default 'colon' - */ - on: "value" | "colon"; - /** - * @default 'strict' - */ - mode: "strict" | "minimum"; - }>; - singleLine?: - | Partial<{ - /** - * @default false - */ - beforeColon: boolean; - /** - * @default true - */ - afterColon: boolean; - /** - * @default 'strict' - */ - mode: "strict" | "minimum"; - }> - | undefined; - multiLine?: - | Partial<{ - /** - * @default false - */ - beforeColon: boolean; - /** - * @default true - */ - afterColon: boolean; - /** - * @default 'strict' - */ - mode: "strict" | "minimum"; - }> - | undefined; - }, - ] - >; - - /** - * Rule to enforce consistent spacing before and after keywords. - * - * @since 2.0.0-beta.1 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`keyword-spacing`](https://eslint.style/rules/keyword-spacing) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/keyword-spacing - */ - "keyword-spacing": Linter.RuleEntry< - [ - Partial<{ - /** - * @default true - */ - before: boolean; - /** - * @default true - */ - after: boolean; - overrides: Record< - string, - Partial<{ - before: boolean; - after: boolean; - }> - >; - }>, - ] - >; - - /** - * Rule to enforce position of line comments. - * - * @since 3.5.0 - * @deprecated since 9.3.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`line-comment-position`](https://eslint.style/rules/line-comment-position) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/line-comment-position - */ - "line-comment-position": Linter.RuleEntry< - [ - Partial<{ - /** - * @default 'above' - */ - position: "above" | "beside"; - ignorePattern: string; - /** - * @default true - */ - applyDefaultIgnorePatterns: boolean; - }>, - ] - >; - - /** - * Rule to enforce consistent linebreak style. - * - * @since 0.21.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`linebreak-style`](https://eslint.style/rules/linebreak-style) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/linebreak-style - */ - "linebreak-style": Linter.RuleEntry<["unix" | "windows"]>; - - /** - * Rule to require empty lines around comments. - * - * @since 0.22.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`lines-around-comment`](https://eslint.style/rules/lines-around-comment) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/lines-around-comment - */ - "lines-around-comment": Linter.RuleEntry< - [ - Partial<{ - /** - * @default true - */ - beforeBlockComment: boolean; - /** - * @default false - */ - afterBlockComment: boolean; - /** - * @default false - */ - beforeLineComment: boolean; - /** - * @default false - */ - afterLineComment: boolean; - /** - * @default false - */ - allowBlockStart: boolean; - /** - * @default false - */ - allowBlockEnd: boolean; - /** - * @default false - */ - allowObjectStart: boolean; - /** - * @default false - */ - allowObjectEnd: boolean; - /** - * @default false - */ - allowArrayStart: boolean; - /** - * @default false - */ - allowArrayEnd: boolean; - /** - * @default false - */ - allowClassStart: boolean; - /** - * @default false - */ - allowClassEnd: boolean; - ignorePattern: string; - /** - * @default true - */ - applyDefaultIgnorePatterns: boolean; - }>, - ] - >; - - /** - * Rule to require or disallow newlines around directives. - * - * @since 3.5.0 - * @deprecated since 4.0.0. - * The rule was replaced with a more general rule. - * Please, use [`padding-line-between-statements`](https://eslint.style/rules/padding-line-between-statements) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/lines-around-directive - */ - "lines-around-directive": Linter.RuleEntry<["always" | "never"]>; - - /** - * Rule to require or disallow an empty line between class members. - * - * @since 4.9.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`lines-between-class-members`](https://eslint.style/rules/lines-between-class-members) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/lines-between-class-members - */ - "lines-between-class-members": Linter.RuleEntry< - [ - ( - | "always" - | "never" - | { - enforce: Array<{ - blankLine: "always" | "never"; - prev: "method" | "field" | "*"; - next: "method" | "field" | "*"; - }>; - } - ), - Partial<{ - /** - * @default false - */ - exceptAfterSingleLine: boolean; - }>, - ] - >; - - /** - * Rule to require or disallow logical assignment operator shorthand. - * - * @since 8.24.0 - * @see https://eslint.org/docs/latest/rules/logical-assignment-operators - */ - "logical-assignment-operators": - | Linter.RuleEntry< - [ - "always", - Partial<{ - /** - * @default false - */ - enforceForIfStatements: boolean; - }>, - ] - > - | Linter.RuleEntry<["never"]>; - - /** - * Rule to enforce a maximum number of classes per file. - * - * @since 5.0.0-alpha.3 - * @see https://eslint.org/docs/latest/rules/max-classes-per-file - */ - "max-classes-per-file": Linter.RuleEntry< - [ - | number - | Partial<{ - /** - * @default false - */ - ignoreExpressions: boolean; - /** - * @default 1 - */ - max: number; - }>, - ] - >; - - /** - * Rule to enforce a maximum depth that blocks can be nested. - * - * @since 0.0.9 - * @see https://eslint.org/docs/latest/rules/max-depth - */ - "max-depth": Linter.RuleEntry< - [ - | number - | Partial<{ - /** - * @deprecated - * @default 4 - */ - maximum: number; - /** - * @default 4 - */ - max: number; - }>, - ] - >; - - /** - * Rule to enforce a maximum line length. - * - * @since 0.0.9 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`max-len`](https://eslint.style/rules/max-len) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/max-len - */ - "max-len": Linter.RuleEntry< - [ - Partial<{ - /** - * @default 80 - */ - code: number; - /** - * @default 4 - */ - tabWidth: number; - comments: number; - ignorePattern: string; - /** - * @default false - */ - ignoreComments: boolean; - /** - * @default false - */ - ignoreTrailingComments: boolean; - /** - * @default false - */ - ignoreUrls: boolean; - /** - * @default false - */ - ignoreStrings: boolean; - /** - * @default false - */ - ignoreTemplateLiterals: boolean; - /** - * @default false - */ - ignoreRegExpLiterals: boolean; - }>, - ] - >; - - /** - * Rule to enforce a maximum number of lines per file. - * - * @since 2.12.0 - * @see https://eslint.org/docs/latest/rules/max-lines - */ - "max-lines": Linter.RuleEntry< - [ - | Partial<{ - /** - * @default 300 - */ - max: number; - /** - * @default false - */ - skipBlankLines: boolean; - /** - * @default false - */ - skipComments: boolean; - }> - | number, - ] - >; - - /** - * Rule to enforce a maximum number of lines of code in a function. - * - * @since 5.0.0 - * @see https://eslint.org/docs/latest/rules/max-lines-per-function - */ - "max-lines-per-function": Linter.RuleEntry< - [ - | number - | Partial<{ - /** - * @default 50 - */ - max: number; - /** - * @default false - */ - skipBlankLines: boolean; - /** - * @default false - */ - skipComments: boolean; - /** - * @default false - */ - IIFEs: boolean; - }>, - ] - >; - - /** - * Rule to enforce a maximum depth that callbacks can be nested. - * - * @since 0.2.0 - * @see https://eslint.org/docs/latest/rules/max-nested-callbacks - */ - "max-nested-callbacks": Linter.RuleEntry< - [ - | number - | Partial<{ - /** - * @deprecated - * @default 10 - */ - maximum: number; - /** - * @default 10 - */ - max: number; - }>, - ] - >; - - /** - * Rule to enforce a maximum number of parameters in function definitions. - * - * @since 0.0.9 - * @see https://eslint.org/docs/latest/rules/max-params - */ - "max-params": Linter.RuleEntry< - [ - | number - | Partial<{ - /** - * @deprecated - * @default 3 - */ - maximum: number; - /** - * @default 3 - */ - max: number; - /** - * @default false - */ - countVoidThis: boolean; - }>, - ] - >; - - /** - * Rule to enforce a maximum number of statements allowed in function blocks. - * - * @since 0.0.9 - * @see https://eslint.org/docs/latest/rules/max-statements - */ - "max-statements": Linter.RuleEntry< - [ - ( - | number - | Partial<{ - /** - * @deprecated - * @default 10 - */ - maximum: number; - /** - * @default 10 - */ - max: number; - }> - ), - Partial<{ - /** - * @default false - */ - ignoreTopLevelFunctions: boolean; - }>, - ] - >; - - /** - * Rule to enforce a maximum number of statements allowed per line. - * - * @since 2.5.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`max-statements-per-line`](https://eslint.style/rules/max-statements-per-line) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/max-statements-per-line - */ - "max-statements-per-line": Linter.RuleEntry< - [ - | Partial<{ - /** - * @default 1 - */ - max: number; - }> - | number, - ] - >; - - /** - * Rule to enforce a particular style for multiline comments. - * - * @since 4.10.0 - * @deprecated since 9.3.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`multiline-comment-style`](https://eslint.style/rules/multiline-comment-style) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/multiline-comment-style - */ - "multiline-comment-style": Linter.RuleEntry< - ["starred-block" | "bare-block" | "separate-lines"] - >; - - /** - * Rule to enforce newlines between operands of ternary expressions. - * - * @since 3.1.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`multiline-ternary`](https://eslint.style/rules/multiline-ternary) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/multiline-ternary - */ - "multiline-ternary": Linter.RuleEntry< - ["always" | "always-multiline" | "never"] - >; - - /** - * Rule to require constructor names to begin with a capital letter. - * - * @since 0.0.3-0 - * @see https://eslint.org/docs/latest/rules/new-cap - */ - "new-cap": Linter.RuleEntry< - [ - Partial<{ - /** - * @default true - */ - newIsCap: boolean; - /** - * @default true - */ - capIsNew: boolean; - newIsCapExceptions: string[]; - newIsCapExceptionPattern: string; - capIsNewExceptions: string[]; - capIsNewExceptionPattern: string; - /** - * @default true - */ - properties: boolean; - }>, - ] - >; - - /** - * Rule to enforce or disallow parentheses when invoking a constructor with no arguments. - * - * @since 0.0.6 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`new-parens`](https://eslint.style/rules/new-parens) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/new-parens - */ - "new-parens": Linter.RuleEntry<["always" | "never"]>; - - /** - * Rule to require or disallow an empty line after variable declarations. - * - * @since 0.18.0 - * @deprecated since 4.0.0. - * The rule was replaced with a more general rule. - * Please, use [`padding-line-between-statements`](https://eslint.style/rules/padding-line-between-statements) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/newline-after-var - */ - "newline-after-var": Linter.RuleEntry<["always" | "never"]>; - - /** - * Rule to require an empty line before `return` statements. - * - * @since 2.3.0 - * @deprecated since 4.0.0. - * The rule was replaced with a more general rule. - * Please, use [`padding-line-between-statements`](https://eslint.style/rules/padding-line-between-statements) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/newline-before-return - */ - "newline-before-return": Linter.RuleEntry<[]>; - - /** - * Rule to require a newline after each call in a method chain. - * - * @since 2.0.0-rc.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`newline-per-chained-call`](https://eslint.style/rules/newline-per-chained-call) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/newline-per-chained-call - */ - "newline-per-chained-call": Linter.RuleEntry< - [ - { - /** - * @default 2 - */ - ignoreChainWithDepth: number; - }, - ] - >; - - /** - * Rule to disallow the use of `alert`, `confirm`, and `prompt`. - * - * @since 0.0.5 - * @see https://eslint.org/docs/latest/rules/no-alert - */ - "no-alert": Linter.RuleEntry<[]>; - - /** - * Rule to disallow `Array` constructors. - * - * @since 0.4.0 - * @see https://eslint.org/docs/latest/rules/no-array-constructor - */ - "no-array-constructor": Linter.RuleEntry<[]>; - - /** - * Rule to disallow using an async function as a Promise executor. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 5.3.0 - * @see https://eslint.org/docs/latest/rules/no-async-promise-executor - */ - "no-async-promise-executor": Linter.RuleEntry<[]>; - - /** - * Rule to disallow `await` inside of loops. - * - * @since 3.12.0 - * @see https://eslint.org/docs/latest/rules/no-await-in-loop - */ - "no-await-in-loop": Linter.RuleEntry<[]>; - - /** - * Rule to disallow bitwise operators. - * - * @since 0.0.2 - * @see https://eslint.org/docs/latest/rules/no-bitwise - */ - "no-bitwise": Linter.RuleEntry< - [ - Partial<{ - /** - * @default [] - */ - allow: Array< - | "^" - | "|" - | "&" - | "<<" - | ">>" - | ">>>" - | "^=" - | "|=" - | "&=" - | "<<=" - | ">>=" - | ">>>=" - | "~" - >; - /** - * @default false - */ - int32Hint: boolean; - }>, - ] - >; - - /** - * Rule to disallow use of the `Buffer()` constructor. - * - * @since 4.0.0-alpha.0 - * @deprecated since 7.0.0. - * Node.js rules were moved out of ESLint core. - * Please, use [`no-deprecated-api`](https://github.com/eslint-community/eslint-plugin-n/tree/master/docs/rules/no-deprecated-api.md) in [`eslint-plugin-n`](https://github.com/eslint-community/eslint-plugin-n). - * @see https://eslint.org/docs/latest/rules/no-buffer-constructor - */ - "no-buffer-constructor": Linter.RuleEntry<[]>; - - /** - * Rule to disallow the use of `arguments.caller` or `arguments.callee`. - * - * @since 0.0.6 - * @see https://eslint.org/docs/latest/rules/no-caller - */ - "no-caller": Linter.RuleEntry<[]>; - - /** - * Rule to disallow lexical declarations in case clauses. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 1.9.0 - * @see https://eslint.org/docs/latest/rules/no-case-declarations - */ - "no-case-declarations": Linter.RuleEntry<[]>; - - /** - * Rule to disallow `catch` clause parameters from shadowing variables in the outer scope. - * - * @since 0.0.9 - * @deprecated since 5.1.0. - * This rule was renamed. - * Please, use [`no-shadow`](https://eslint.org/docs/rules/no-shadow). - * @see https://eslint.org/docs/latest/rules/no-catch-shadow - */ - "no-catch-shadow": Linter.RuleEntry<[]>; - - /** - * Rule to disallow reassigning class members. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 1.0.0-rc-1 - * @see https://eslint.org/docs/latest/rules/no-class-assign - */ - "no-class-assign": Linter.RuleEntry<[]>; - - /** - * Rule to disallow comparing against `-0`. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 3.17.0 - * @see https://eslint.org/docs/latest/rules/no-compare-neg-zero - */ - "no-compare-neg-zero": Linter.RuleEntry<[]>; - - /** - * Rule to disallow assignment operators in conditional expressions. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 0.0.9 - * @see https://eslint.org/docs/latest/rules/no-cond-assign - */ - "no-cond-assign": Linter.RuleEntry<["except-parens" | "always"]>; - - /** - * Rule to disallow arrow functions where they could be confused with comparisons. - * - * @since 2.0.0-alpha-2 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`no-confusing-arrow`](https://eslint.style/rules/no-confusing-arrow) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/no-confusing-arrow - */ - "no-confusing-arrow": Linter.RuleEntry< - [ - Partial<{ - /** - * @default true - */ - allowParens: boolean; - }>, - ] - >; - - /** - * Rule to disallow the use of `console`. - * - * @since 0.0.2 - * @see https://eslint.org/docs/latest/rules/no-console - */ - "no-console": Linter.RuleEntry< - [ - Partial<{ - allow: Array; - }>, - ] - >; - - /** - * Rule to disallow reassigning `const`, `using`, and `await using` variables. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 1.0.0-rc-1 - * @see https://eslint.org/docs/latest/rules/no-const-assign - */ - "no-const-assign": Linter.RuleEntry<[]>; - - /** - * Rule to disallow expressions where the operation doesn't affect the value. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 8.14.0 - * @see https://eslint.org/docs/latest/rules/no-constant-binary-expression - */ - "no-constant-binary-expression": Linter.RuleEntry<[]>; - - /** - * Rule to disallow constant expressions in conditions. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 0.4.1 - * @see https://eslint.org/docs/latest/rules/no-constant-condition - */ - "no-constant-condition": Linter.RuleEntry< - [ - { - /** - * @default "allExceptWhileTrue" - */ - checkLoops: "all" | "allExceptWhileTrue" | "none" | boolean; - }, - ] - >; - - /** - * Rule to disallow returning value from constructor. - * - * @since 6.7.0 - * @see https://eslint.org/docs/latest/rules/no-constructor-return - */ - "no-constructor-return": Linter.RuleEntry<[]>; - - /** - * Rule to disallow `continue` statements. - * - * @since 0.19.0 - * @see https://eslint.org/docs/latest/rules/no-continue - */ - "no-continue": Linter.RuleEntry<[]>; - - /** - * Rule to disallow control characters in regular expressions. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 0.1.0 - * @see https://eslint.org/docs/latest/rules/no-control-regex - */ - "no-control-regex": Linter.RuleEntry<[]>; - - /** - * Rule to disallow the use of `debugger`. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 0.0.2 - * @see https://eslint.org/docs/latest/rules/no-debugger - */ - "no-debugger": Linter.RuleEntry<[]>; - - /** - * Rule to disallow deleting variables. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 0.0.9 - * @see https://eslint.org/docs/latest/rules/no-delete-var - */ - "no-delete-var": Linter.RuleEntry<[]>; - - /** - * Rule to disallow equal signs explicitly at the beginning of regular expressions. - * - * @since 0.1.0 - * @see https://eslint.org/docs/latest/rules/no-div-regex - */ - "no-div-regex": Linter.RuleEntry<[]>; - - /** - * Rule to disallow duplicate arguments in `function` definitions. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 0.16.0 - * @see https://eslint.org/docs/latest/rules/no-dupe-args - */ - "no-dupe-args": Linter.RuleEntry<[]>; - - /** - * Rule to disallow duplicate class members. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 1.2.0 - * @see https://eslint.org/docs/latest/rules/no-dupe-class-members - */ - "no-dupe-class-members": Linter.RuleEntry<[]>; - - /** - * Rule to disallow duplicate conditions in if-else-if chains. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 6.7.0 - * @see https://eslint.org/docs/latest/rules/no-dupe-else-if - */ - "no-dupe-else-if": Linter.RuleEntry<[]>; - - /** - * Rule to disallow duplicate keys in object literals. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 0.0.9 - * @see https://eslint.org/docs/latest/rules/no-dupe-keys - */ - "no-dupe-keys": Linter.RuleEntry<[]>; - - /** - * Rule to disallow duplicate case labels. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 0.17.0 - * @see https://eslint.org/docs/latest/rules/no-duplicate-case - */ - "no-duplicate-case": Linter.RuleEntry<[]>; - - /** - * Rule to disallow duplicate module imports. - * - * @since 2.5.0 - * @see https://eslint.org/docs/latest/rules/no-duplicate-imports - */ - "no-duplicate-imports": Linter.RuleEntry< - [ - Partial<{ - /** - * @default false - */ - includeExports: boolean; - /** - * @default false - */ - allowSeparateTypeImports: boolean; - }>, - ] - >; - - /** - * Rule to disallow `else` blocks after `return` statements in `if` statements. - * - * @since 0.0.9 - * @see https://eslint.org/docs/latest/rules/no-else-return - */ - "no-else-return": Linter.RuleEntry< - [ - Partial<{ - /** - * @default true - */ - allowElseIf: boolean; - }>, - ] - >; - - /** - * Rule to disallow empty block statements. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 0.0.2 - * @see https://eslint.org/docs/latest/rules/no-empty - */ - "no-empty": Linter.RuleEntry< - [ - Partial<{ - /** - * @default false - */ - allowEmptyCatch: boolean; - }>, - ] - >; - - /** - * Rule to disallow empty character classes in regular expressions. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 0.22.0 - * @see https://eslint.org/docs/latest/rules/no-empty-character-class - */ - "no-empty-character-class": Linter.RuleEntry<[]>; - - /** - * Rule to disallow empty functions. - * - * @since 2.0.0 - * @see https://eslint.org/docs/latest/rules/no-empty-function - */ - "no-empty-function": Linter.RuleEntry< - [ - Partial<{ - /** - * @default [] - */ - allow: Array< - | "functions" - | "arrowFunctions" - | "generatorFunctions" - | "methods" - | "generatorMethods" - | "getters" - | "setters" - | "constructors" - | "asyncFunctions" - | "asyncMethods" - | "privateConstructors" - | "protectedConstructors" - | "decoratedFunctions" - | "overrideMethods" - >; - }>, - ] - >; - - /** - * Rule to disallow empty destructuring patterns. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 1.7.0 - * @see https://eslint.org/docs/latest/rules/no-empty-pattern - */ - "no-empty-pattern": Linter.RuleEntry< - [ - Partial<{ - /** - * @default false - */ - allowObjectPatternsAsParameters: boolean; - }>, - ] - >; - - /** - * Rule to disallow empty static blocks. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 8.27.0 - * @see https://eslint.org/docs/latest/rules/no-empty-static-block - */ - "no-empty-static-block": Linter.RuleEntry<[]>; - - /** - * Rule to disallow `null` comparisons without type-checking operators. - * - * @since 0.0.9 - * @see https://eslint.org/docs/latest/rules/no-eq-null - */ - "no-eq-null": Linter.RuleEntry<[]>; - - /** - * Rule to disallow the use of `eval()`. - * - * @since 0.0.2 - * @see https://eslint.org/docs/latest/rules/no-eval - */ - "no-eval": Linter.RuleEntry< - [ - Partial<{ - /** - * @default false - */ - allowIndirect: boolean; - }>, - ] - >; - - /** - * Rule to disallow reassigning exceptions in `catch` clauses. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 0.0.9 - * @see https://eslint.org/docs/latest/rules/no-ex-assign - */ - "no-ex-assign": Linter.RuleEntry<[]>; - - /** - * Rule to disallow extending native types. - * - * @since 0.1.4 - * @see https://eslint.org/docs/latest/rules/no-extend-native - */ - "no-extend-native": Linter.RuleEntry< - [ - Partial<{ - exceptions: string[]; - }>, - ] - >; - - /** - * Rule to disallow unnecessary calls to `.bind()`. - * - * @since 0.8.0 - * @see https://eslint.org/docs/latest/rules/no-extra-bind - */ - "no-extra-bind": Linter.RuleEntry<[]>; - - /** - * Rule to disallow unnecessary boolean casts. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 0.4.0 - * @see https://eslint.org/docs/latest/rules/no-extra-boolean-cast - */ - "no-extra-boolean-cast": Linter.RuleEntry< - [ - | Partial<{ - /** - * @since 9.3.0 - * @default false - */ - enforceForInnerExpressions: boolean; - /** - * @deprecated - */ - enforceForLogicalOperands: never; - }> - | Partial<{ - /** - * @deprecated - * @since 7.0.0-alpha.2 - * @default false - */ - enforceForLogicalOperands: boolean; - enforceForInnerExpressions: never; - }>, - ] - >; - - /** - * Rule to disallow unnecessary labels. - * - * @since 2.0.0-rc.0 - * @see https://eslint.org/docs/latest/rules/no-extra-label - */ - "no-extra-label": Linter.RuleEntry<[]>; - - /** - * Rule to disallow unnecessary parentheses. - * - * @since 0.1.4 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`no-extra-parens`](https://eslint.style/rules/no-extra-parens) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/no-extra-parens - */ - "no-extra-parens": - | Linter.RuleEntry< - [ - "all", - Partial<{ - /** - * @default true, - */ - conditionalAssign: boolean; - /** - * @default true - */ - returnAssign: boolean; - /** - * @default true - */ - nestedBinaryExpressions: boolean; - /** - * @default 'none' - */ - ignoreJSX: - | "none" - | "all" - | "multi-line" - | "single-line"; - /** - * @default true - */ - enforceForArrowConditionals: boolean; - }>, - ] - > - | Linter.RuleEntry<["functions"]>; - - /** - * Rule to disallow unnecessary semicolons. - * - * @since 0.0.9 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`no-extra-semi`](https://eslint.style/rules/no-extra-semi) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/no-extra-semi - */ - "no-extra-semi": Linter.RuleEntry<[]>; - - /** - * Rule to disallow fallthrough of `case` statements. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 0.0.7 - * @see https://eslint.org/docs/latest/rules/no-fallthrough - */ - "no-fallthrough": Linter.RuleEntry< - [ - Partial<{ - /** - * @default 'falls?\s?through' - */ - commentPattern: string; - /** - * @default false - */ - allowEmptyCase: boolean; - /** - * @default false - */ - reportUnusedFallthroughComment: boolean; - }>, - ] - >; - - /** - * Rule to disallow leading or trailing decimal points in numeric literals. - * - * @since 0.0.6 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`no-floating-decimal`](https://eslint.style/rules/no-floating-decimal) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/no-floating-decimal - */ - "no-floating-decimal": Linter.RuleEntry<[]>; - - /** - * Rule to disallow reassigning `function` declarations. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 0.0.9 - * @see https://eslint.org/docs/latest/rules/no-func-assign - */ - "no-func-assign": Linter.RuleEntry<[]>; - - /** - * Rule to disallow assignments to native objects or read-only global variables. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 3.3.0 - * @see https://eslint.org/docs/latest/rules/no-global-assign - */ - "no-global-assign": Linter.RuleEntry< - [ - Partial<{ - exceptions: string[]; - }>, - ] - >; - - /** - * Rule to disallow shorthand type conversions. - * - * @since 1.0.0-rc-2 - * @see https://eslint.org/docs/latest/rules/no-implicit-coercion - */ - "no-implicit-coercion": Linter.RuleEntry< - [ - Partial<{ - /** - * @default true - */ - boolean: boolean; - /** - * @default true - */ - number: boolean; - /** - * @default true - */ - string: boolean; - /** - * @default false - */ - disallowTemplateShorthand: boolean; - /** - * @default [] - */ - allow: Array<"~" | "!!" | "+" | "- -" | "-" | "*">; - }>, - ] - >; - - /** - * Rule to disallow declarations in the global scope. - * - * @since 2.0.0-alpha-1 - * @see https://eslint.org/docs/latest/rules/no-implicit-globals - */ - "no-implicit-globals": Linter.RuleEntry< - [ - Partial<{ - /** - * @default false - */ - lexicalBindings: boolean; - }>, - ] - >; - - /** - * Rule to disallow the use of `eval()`-like methods. - * - * @since 0.0.7 - * @see https://eslint.org/docs/latest/rules/no-implied-eval - */ - "no-implied-eval": Linter.RuleEntry<[]>; - - /** - * Rule to disallow assigning to imported bindings. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 6.4.0 - * @see https://eslint.org/docs/latest/rules/no-import-assign - */ - "no-import-assign": Linter.RuleEntry<[]>; - - /** - * Rule to disallow inline comments after code. - * - * @since 0.10.0 - * @see https://eslint.org/docs/latest/rules/no-inline-comments - */ - "no-inline-comments": Linter.RuleEntry< - [ - Partial<{ - ignorePattern: string; - }>, - ] - >; - - /** - * Rule to disallow variable or `function` declarations in nested blocks. - * - * @since 0.6.0 - * @see https://eslint.org/docs/latest/rules/no-inner-declarations - */ - "no-inner-declarations": Linter.RuleEntry< - [ - "functions" | "both", - Partial<{ - /** - * @default "allow" - */ - blockScopedFunctions: "allow" | "disallow"; - }>, - ] - >; - - /** - * Rule to disallow invalid regular expression strings in `RegExp` constructors. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 0.1.4 - * @see https://eslint.org/docs/latest/rules/no-invalid-regexp - */ - "no-invalid-regexp": Linter.RuleEntry< - [ - Partial<{ - allowConstructorFlags: string[]; - }>, - ] - >; - - /** - * Rule to disallow use of `this` in contexts where the value of `this` is `undefined`. - * - * @since 1.0.0-rc-2 - * @see https://eslint.org/docs/latest/rules/no-invalid-this - */ - "no-invalid-this": Linter.RuleEntry< - [ - Partial<{ - /** - * @default true - */ - capIsConstructor: boolean; - }>, - ] - >; - - /** - * Rule to disallow irregular whitespace. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 0.9.0 - * @see https://eslint.org/docs/latest/rules/no-irregular-whitespace - */ - "no-irregular-whitespace": Linter.RuleEntry< - [ - Partial<{ - /** - * @default true - */ - skipStrings: boolean; - /** - * @default false - */ - skipComments: boolean; - /** - * @default false - */ - skipRegExps: boolean; - /** - * @default false - */ - skipTemplates: boolean; - /** - * @default false - */ - skipJSXText: boolean; - }>, - ] - >; - - /** - * Rule to disallow the use of the `__iterator__` property. - * - * @since 0.0.9 - * @see https://eslint.org/docs/latest/rules/no-iterator - */ - "no-iterator": Linter.RuleEntry<[]>; - - /** - * Rule to disallow labels that share a name with a variable. - * - * @since 0.0.9 - * @see https://eslint.org/docs/latest/rules/no-label-var - */ - "no-label-var": Linter.RuleEntry<[]>; - - /** - * Rule to disallow labeled statements. - * - * @since 0.4.0 - * @see https://eslint.org/docs/latest/rules/no-labels - */ - "no-labels": Linter.RuleEntry< - [ - Partial<{ - /** - * @default false - */ - allowLoop: boolean; - /** - * @default false - */ - allowSwitch: boolean; - }>, - ] - >; - - /** - * Rule to disallow unnecessary nested blocks. - * - * @since 0.4.0 - * @see https://eslint.org/docs/latest/rules/no-lone-blocks - */ - "no-lone-blocks": Linter.RuleEntry<[]>; - - /** - * Rule to disallow `if` statements as the only statement in `else` blocks. - * - * @since 0.6.0 - * @see https://eslint.org/docs/latest/rules/no-lonely-if - */ - "no-lonely-if": Linter.RuleEntry<[]>; - - /** - * Rule to disallow function declarations that contain unsafe references inside loop statements. - * - * @since 0.0.9 - * @see https://eslint.org/docs/latest/rules/no-loop-func - */ - "no-loop-func": Linter.RuleEntry<[]>; - - /** - * Rule to disallow literal numbers that lose precision. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 7.1.0 - * @see https://eslint.org/docs/latest/rules/no-loss-of-precision - */ - "no-loss-of-precision": Linter.RuleEntry<[]>; - - /** - * Rule to disallow magic numbers. - * - * @since 1.7.0 - * @see https://eslint.org/docs/latest/rules/no-magic-numbers - */ - "no-magic-numbers": Linter.RuleEntry< - [ - Partial<{ - /** - * @default [] - */ - ignore: Array; - /** - * @default false - */ - ignoreArrayIndexes: boolean; - /** - * @default false - */ - ignoreDefaultValues: boolean; - /** - * @default false - */ - ignoreClassFieldInitialValues: boolean; - /** - * @default false - */ - enforceConst: boolean; - /** - * @default false - */ - detectObjects: boolean; - /** - * @default false - */ - ignoreEnums: boolean; - /** - * @default false - */ - ignoreNumericLiteralTypes: boolean; - /** - * @default false - */ - ignoreReadonlyClassProperties: boolean; - /** - * @default false - */ - ignoreTypeIndexes: boolean; - }>, - ] - >; - - /** - * Rule to disallow characters which are made with multiple code points in character class syntax. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 5.3.0 - * @see https://eslint.org/docs/latest/rules/no-misleading-character-class - */ - "no-misleading-character-class": Linter.RuleEntry< - [ - Partial<{ - /** - * @since 9.3.0 - * @default false - */ - allowEscape: boolean; - }>, - ] - >; - - /** - * Rule to disallow mixed binary operators. - * - * @since 2.12.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`no-mixed-operators`](https://eslint.style/rules/no-mixed-operators) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/no-mixed-operators - */ - "no-mixed-operators": Linter.RuleEntry< - [ - Partial<{ - /** - * @default - * [ - * ["+", "-", "*", "/", "%", "**"], - * ["&", "|", "^", "~", "<<", ">>", ">>>"], - * ["==", "!=", "===", "!==", ">", ">=", "<", "<="], - * ["&&", "||"], - * ["in", "instanceof"] - * ] - */ - groups: string[][]; - /** - * @default true - */ - allowSamePrecedence: boolean; - }>, - ] - >; - - /** - * Rule to disallow `require` calls to be mixed with regular variable declarations. - * - * @since 0.0.9 - * @deprecated since 7.0.0. - * Node.js rules were moved out of ESLint core. - * Please, use [`no-mixed-requires`](https://github.com/eslint-community/eslint-plugin-n/tree/master/docs/rules/no-mixed-requires.md) in [`eslint-plugin-n`](https://github.com/eslint-community/eslint-plugin-n). - * @see https://eslint.org/docs/latest/rules/no-mixed-requires - */ - "no-mixed-requires": Linter.RuleEntry< - [ - Partial<{ - /** - * @default false - */ - grouping: boolean; - /** - * @default false - */ - allowCall: boolean; - }>, - ] - >; - - /** - * Rule to disallow mixed spaces and tabs for indentation. - * - * @since 0.7.1 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`no-mixed-spaces-and-tabs`](https://eslint.style/rules/no-mixed-spaces-and-tabs) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/no-mixed-spaces-and-tabs - */ - "no-mixed-spaces-and-tabs": Linter.RuleEntry<["smart-tabs"]>; - - /** - * Rule to disallow use of chained assignment expressions. - * - * @since 3.14.0 - * @see https://eslint.org/docs/latest/rules/no-multi-assign - */ - "no-multi-assign": Linter.RuleEntry< - [ - Partial<{ - /** - * @default false - */ - ignoreNonDeclaration: boolean; - }>, - ] - >; - - /** - * Rule to disallow multiple spaces. - * - * @since 0.9.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`no-multi-spaces`](https://eslint.style/rules/no-multi-spaces) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/no-multi-spaces - */ - "no-multi-spaces": Linter.RuleEntry< - [ - Partial<{ - /** - * @default false - */ - ignoreEOLComments: boolean; - /** - * @default { Property: true } - */ - exceptions: Record; - }>, - ] - >; - - /** - * Rule to disallow multiline strings. - * - * @since 0.0.9 - * @see https://eslint.org/docs/latest/rules/no-multi-str - */ - "no-multi-str": Linter.RuleEntry<[]>; - - /** - * Rule to disallow multiple empty lines. - * - * @since 0.9.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`no-multiple-empty-lines`](https://eslint.style/rules/no-multiple-empty-lines) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/no-multiple-empty-lines - */ - "no-multiple-empty-lines": Linter.RuleEntry< - [ - | Partial<{ - /** - * @default 2 - */ - max: number; - maxEOF: number; - maxBOF: number; - }> - | number, - ] - >; - - /** - * Rule to disallow assignments to native objects or read-only global variables. - * - * @since 0.0.9 - * @deprecated since 3.3.0. - * Renamed rule. - * Please, use [`no-global-assign`](https://eslint.org/docs/rules/no-global-assign). - * @see https://eslint.org/docs/latest/rules/no-native-reassign - */ - "no-native-reassign": Linter.RuleEntry< - [ - Partial<{ - exceptions: string[]; - }>, - ] - >; - - /** - * Rule to disallow negated conditions. - * - * @since 1.6.0 - * @see https://eslint.org/docs/latest/rules/no-negated-condition - */ - "no-negated-condition": Linter.RuleEntry<[]>; - - /** - * Rule to disallow negating the left operand in `in` expressions. - * - * @since 0.1.2 - * @deprecated since 3.3.0. - * Renamed rule. - * Please, use [`no-unsafe-negation`](https://eslint.org/docs/rules/no-unsafe-negation). - * @see https://eslint.org/docs/latest/rules/no-negated-in-lhs - */ - "no-negated-in-lhs": Linter.RuleEntry<[]>; - - /** - * Rule to disallow nested ternary expressions. - * - * @since 0.2.0 - * @see https://eslint.org/docs/latest/rules/no-nested-ternary - */ - "no-nested-ternary": Linter.RuleEntry<[]>; - - /** - * Rule to disallow `new` operators outside of assignments or comparisons. - * - * @since 0.0.7 - * @see https://eslint.org/docs/latest/rules/no-new - */ - "no-new": Linter.RuleEntry<[]>; - - /** - * Rule to disallow `new` operators with the `Function` object. - * - * @since 0.0.7 - * @see https://eslint.org/docs/latest/rules/no-new-func - */ - "no-new-func": Linter.RuleEntry<[]>; - - /** - * Rule to disallow `new` operators with global non-constructor functions. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 8.27.0 - * @see https://eslint.org/docs/latest/rules/no-new-native-nonconstructor - */ - "no-new-native-nonconstructor": Linter.RuleEntry<[]>; - - /** - * Rule to disallow `Object` constructors. - * - * @since 0.0.9 - * @deprecated since 8.50.0. - * The new rule flags more situations where object literal syntax can be used, and it does not report a problem when the `Object` constructor is invoked with an argument. - * Please, use [`no-object-constructor`](https://eslint.org/docs/rules/no-object-constructor). - * @see https://eslint.org/docs/latest/rules/no-new-object - */ - "no-new-object": Linter.RuleEntry<[]>; - - /** - * Rule to disallow `new` operators with calls to `require`. - * - * @since 0.6.0 - * @deprecated since 7.0.0. - * Node.js rules were moved out of ESLint core. - * Please, use [`no-new-require`](https://github.com/eslint-community/eslint-plugin-n/tree/master/docs/rules/no-new-require.md) in [`eslint-plugin-n`](https://github.com/eslint-community/eslint-plugin-n). - * @see https://eslint.org/docs/latest/rules/no-new-require - */ - "no-new-require": Linter.RuleEntry<[]>; - - /** - * Rule to disallow `new` operators with the `Symbol` object. - * - * @since 2.0.0-beta.1 - * @deprecated since 9.0.0. - * The rule was replaced with a more general rule. - * Please, use [`no-new-native-nonconstructor`](https://eslint.org/docs/latest/rules/no-new-native-nonconstructor). - * @see https://eslint.org/docs/latest/rules/no-new-symbol - */ - "no-new-symbol": Linter.RuleEntry<[]>; - - /** - * Rule to disallow `new` operators with the `String`, `Number`, and `Boolean` objects. - * - * @since 0.0.6 - * @see https://eslint.org/docs/latest/rules/no-new-wrappers - */ - "no-new-wrappers": Linter.RuleEntry<[]>; - - /** - * Rule to disallow `\8` and `\9` escape sequences in string literals. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 7.14.0 - * @see https://eslint.org/docs/latest/rules/no-nonoctal-decimal-escape - */ - "no-nonoctal-decimal-escape": Linter.RuleEntry<[]>; - - /** - * Rule to disallow calling global object properties as functions. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 0.0.9 - * @see https://eslint.org/docs/latest/rules/no-obj-calls - */ - "no-obj-calls": Linter.RuleEntry<[]>; - - /** - * Rule to disallow calls to the `Object` constructor without an argument. - * - * @since 8.50.0 - * @see https://eslint.org/docs/latest/rules/no-object-constructor - */ - "no-object-constructor": Linter.RuleEntry<[]>; - - /** - * Rule to disallow octal literals. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 0.0.6 - * @see https://eslint.org/docs/latest/rules/no-octal - */ - "no-octal": Linter.RuleEntry<[]>; - - /** - * Rule to disallow octal escape sequences in string literals. - * - * @since 0.0.9 - * @see https://eslint.org/docs/latest/rules/no-octal-escape - */ - "no-octal-escape": Linter.RuleEntry<[]>; - - /** - * Rule to disallow reassigning function parameters. - * - * @since 0.18.0 - * @see https://eslint.org/docs/latest/rules/no-param-reassign - */ - "no-param-reassign": Linter.RuleEntry< - [ - | { - props?: false; - } - | ({ - props: true; - } & Partial<{ - /** - * @default [] - */ - ignorePropertyModificationsFor: string[]; - /** - * @since 6.6.0 - * @default [] - */ - ignorePropertyModificationsForRegex: string[]; - }>), - ] - >; - - /** - * Rule to disallow string concatenation with `__dirname` and `__filename`. - * - * @since 0.4.0 - * @deprecated since 7.0.0. - * Node.js rules were moved out of ESLint core. - * Please, use [`no-path-concat`](https://github.com/eslint-community/eslint-plugin-n/tree/master/docs/rules/no-path-concat.md) in [`eslint-plugin-n`](https://github.com/eslint-community/eslint-plugin-n). - * @see https://eslint.org/docs/latest/rules/no-path-concat - */ - "no-path-concat": Linter.RuleEntry<[]>; - - /** - * Rule to disallow the unary operators `++` and `--`. - * - * @since 0.0.9 - * @see https://eslint.org/docs/latest/rules/no-plusplus - */ - "no-plusplus": Linter.RuleEntry< - [ - Partial<{ - /** - * @default false - */ - allowForLoopAfterthoughts: boolean; - }>, - ] - >; - - /** - * Rule to disallow the use of `process.env`. - * - * @since 0.9.0 - * @deprecated since 7.0.0. - * Node.js rules were moved out of ESLint core. - * Please, use [`no-process-env`](https://github.com/eslint-community/eslint-plugin-n/tree/master/docs/rules/no-process-env.md) in [`eslint-plugin-n`](https://github.com/eslint-community/eslint-plugin-n). - * @see https://eslint.org/docs/latest/rules/no-process-env - */ - "no-process-env": Linter.RuleEntry<[]>; - - /** - * Rule to disallow the use of `process.exit()`. - * - * @since 0.4.0 - * @deprecated since 7.0.0. - * Node.js rules were moved out of ESLint core. - * Please, use [`no-process-exit`](https://github.com/eslint-community/eslint-plugin-n/tree/master/docs/rules/no-process-exit.md) in [`eslint-plugin-n`](https://github.com/eslint-community/eslint-plugin-n). - * @see https://eslint.org/docs/latest/rules/no-process-exit - */ - "no-process-exit": Linter.RuleEntry<[]>; - - /** - * Rule to disallow returning values from Promise executor functions. - * - * @since 7.3.0 - * @see https://eslint.org/docs/latest/rules/no-promise-executor-return - */ - "no-promise-executor-return": Linter.RuleEntry< - [ - { - /** - * @default false - */ - allowVoid?: boolean; - }, - ] - >; - - /** - * Rule to disallow the use of the `__proto__` property. - * - * @since 0.0.9 - * @see https://eslint.org/docs/latest/rules/no-proto - */ - "no-proto": Linter.RuleEntry<[]>; - - /** - * Rule to disallow calling some `Object.prototype` methods directly on objects. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 2.11.0 - * @see https://eslint.org/docs/latest/rules/no-prototype-builtins - */ - "no-prototype-builtins": Linter.RuleEntry<[]>; - - /** - * Rule to disallow variable redeclaration. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 0.0.9 - * @see https://eslint.org/docs/latest/rules/no-redeclare - */ - "no-redeclare": Linter.RuleEntry< - [ - Partial<{ - /** - * @default true - */ - builtinGlobals: boolean; - }>, - ] - >; - - /** - * Rule to disallow multiple spaces in regular expressions. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 0.4.0 - * @see https://eslint.org/docs/latest/rules/no-regex-spaces - */ - "no-regex-spaces": Linter.RuleEntry<[]>; - - /** - * Rule to disallow specified names in exports. - * - * @since 7.0.0-alpha.0 - * @see https://eslint.org/docs/latest/rules/no-restricted-exports - */ - "no-restricted-exports": Linter.RuleEntry< - [ - Partial<{ - /** - * @default [] - */ - restrictedNamedExports: string[]; - /** - * @since 9.3.0 - */ - restrictedNamedExportsPattern: string; - /** - * @since 8.33.0 - */ - restrictDefaultExports: Partial<{ - /** - * @default false - */ - direct: boolean; - /** - * @default false - */ - named: boolean; - /** - * @default false - */ - defaultFrom: boolean; - /** - * @default false - */ - namedFrom: boolean; - /** - * @default false - */ - namespaceFrom: boolean; - }>; - }>, - ] - >; - - /** - * Rule to disallow specified global variables. - * - * @since 2.3.0 - * @see https://eslint.org/docs/latest/rules/no-restricted-globals - */ - "no-restricted-globals": Linter.RuleEntry< - [ - ...( - | Array< - | string - | { - name: string; - message?: string | undefined; - } - > - | Array<{ - globals: Array< - | string - | { - name: string; - message?: string | undefined; - } - >; - checkGlobalObject?: boolean; - globalObjects?: string[]; - }> - ), - ] - >; - - /** - * Rule to disallow specified modules when loaded by `import`. - * - * @since 2.0.0-alpha-1 - * @see https://eslint.org/docs/latest/rules/no-restricted-imports - */ - "no-restricted-imports": Linter.RuleEntry< - [ - ...Array< - | string - | ValidNoRestrictedImportPathOptions - | Partial<{ - paths: Array< - string | ValidNoRestrictedImportPathOptions - >; - patterns: - | Array - | Array; - }> - >, - ] - >; - - /** - * Rule to disallow specified modules when loaded by `require`. - * - * @since 0.6.0 - * @deprecated since 7.0.0. - * Node.js rules were moved out of ESLint core. - * Please, use [`no-restricted-require`](https://github.com/eslint-community/eslint-plugin-n/tree/master/docs/rules/no-restricted-require.md) in [`eslint-plugin-n`](https://github.com/eslint-community/eslint-plugin-n). - * @see https://eslint.org/docs/latest/rules/no-restricted-modules - */ - "no-restricted-modules": Linter.RuleEntry< - [ - ...Array< - | string - | { - name: string; - message?: string | undefined; - } - | Partial<{ - paths: Array< - | string - | { - name: string; - message?: string | undefined; - } - >; - patterns: string[]; - }> - >, - ] - >; - - /** - * Rule to disallow certain properties on certain objects. - * - * @since 3.5.0 - * @see https://eslint.org/docs/latest/rules/no-restricted-properties - */ - "no-restricted-properties": Linter.RuleEntry< - [ - ...Array< - | { - object: string; - property?: string | undefined; - message?: string | undefined; - } - | { - property: string; - allowObjects?: string[]; - message?: string | undefined; - } - | { - object: string; - allowProperties?: string[]; - message?: string | undefined; - } - >, - ] - >; - - /** - * Rule to disallow specified syntax. - * - * @since 1.4.0 - * @see https://eslint.org/docs/latest/rules/no-restricted-syntax - */ - "no-restricted-syntax": Linter.RuleEntry< - [ - ...Array< - | string - | { - selector: string; - message?: string | undefined; - } - >, - ] - >; - - /** - * Rule to disallow assignment operators in `return` statements. - * - * @since 0.0.9 - * @see https://eslint.org/docs/latest/rules/no-return-assign - */ - "no-return-assign": Linter.RuleEntry<["except-parens" | "always"]>; - - /** - * Rule to disallow unnecessary `return await`. - * - * @since 3.10.0 - * @deprecated since 8.46.0. - * The original assumption of the rule no longer holds true because of engine optimization. - * @see https://eslint.org/docs/latest/rules/no-return-await - */ - "no-return-await": Linter.RuleEntry<[]>; - - /** - * Rule to disallow `javascript:` URLs. - * - * @since 0.0.9 - * @see https://eslint.org/docs/latest/rules/no-script-url - */ - "no-script-url": Linter.RuleEntry<[]>; - - /** - * Rule to disallow assignments where both sides are exactly the same. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 2.0.0-rc.0 - * @see https://eslint.org/docs/latest/rules/no-self-assign - */ - "no-self-assign": Linter.RuleEntry< - [ - Partial<{ - /** - * @default true - */ - props: boolean; - }>, - ] - >; - - /** - * Rule to disallow comparisons where both sides are exactly the same. - * - * @since 0.0.9 - * @see https://eslint.org/docs/latest/rules/no-self-compare - */ - "no-self-compare": Linter.RuleEntry<[]>; - - /** - * Rule to disallow comma operators. - * - * @since 0.5.1 - * @see https://eslint.org/docs/latest/rules/no-sequences - */ - "no-sequences": Linter.RuleEntry< - [ - Partial<{ - /** - * @since 7.23.0 - * @default true - */ - allowInParentheses: boolean; - }>, - ] - >; - - /** - * Rule to disallow returning values from setters. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 6.7.0 - * @see https://eslint.org/docs/latest/rules/no-setter-return - */ - "no-setter-return": Linter.RuleEntry<[]>; - - /** - * Rule to disallow variable declarations from shadowing variables declared in the outer scope. - * - * @since 0.0.9 - * @see https://eslint.org/docs/latest/rules/no-shadow - */ - "no-shadow": Linter.RuleEntry< - [ - Partial<{ - /** - * @default false - */ - builtinGlobals: boolean; - /** - * @default 'functions' - */ - hoist: - | "functions" - | "all" - | "never" - | "types" - | "functions-and-types"; - allow: string[]; - /** - * @since 8.10.0 - * @default false - */ - ignoreOnInitialization: boolean; - /** - * @default true - */ - ignoreTypeValueShadow: boolean; - /** - * @default true - */ - ignoreFunctionTypeParameterNameValueShadow: boolean; - }>, - ] - >; - - /** - * Rule to disallow identifiers from shadowing restricted names. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 0.1.4 - * @see https://eslint.org/docs/latest/rules/no-shadow-restricted-names - */ - "no-shadow-restricted-names": Linter.RuleEntry< - [ - Partial<{ - /** - * @default false - */ - reportGlobalThis: boolean; - }>, - ] - >; - - /** - * Rule to disallow spacing between function identifiers and their applications (deprecated). - * - * @since 0.1.2 - * @deprecated since 3.3.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`function-call-spacing`](https://eslint.style/rules/function-call-spacing) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/no-spaced-func - */ - "no-spaced-func": Linter.RuleEntry<[]>; - - /** - * Rule to disallow sparse arrays. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 0.4.0 - * @see https://eslint.org/docs/latest/rules/no-sparse-arrays - */ - "no-sparse-arrays": Linter.RuleEntry<[]>; - - /** - * Rule to disallow synchronous methods. - * - * @since 0.0.9 - * @deprecated since 7.0.0. - * Node.js rules were moved out of ESLint core. - * Please, use [`no-sync`](https://github.com/eslint-community/eslint-plugin-n/tree/master/docs/rules/no-sync.md) in [`eslint-plugin-n`](https://github.com/eslint-community/eslint-plugin-n). - * @see https://eslint.org/docs/latest/rules/no-sync - */ - "no-sync": Linter.RuleEntry< - [ - { - /** - * @default false - */ - allowAtRootLevel: boolean; - }, - ] - >; - - /** - * Rule to disallow all tabs. - * - * @since 3.2.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`no-tabs`](https://eslint.style/rules/no-tabs) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/no-tabs - */ - "no-tabs": Linter.RuleEntry< - [ - Partial<{ - /** - * @default false - */ - allowIndentationTabs: boolean; - }>, - ] - >; - - /** - * Rule to disallow template literal placeholder syntax in regular strings. - * - * @since 3.3.0 - * @see https://eslint.org/docs/latest/rules/no-template-curly-in-string - */ - "no-template-curly-in-string": Linter.RuleEntry<[]>; - - /** - * Rule to disallow ternary operators. - * - * @since 0.0.9 - * @see https://eslint.org/docs/latest/rules/no-ternary - */ - "no-ternary": Linter.RuleEntry<[]>; - - /** - * Rule to disallow `this`/`super` before calling `super()` in constructors. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 0.24.0 - * @see https://eslint.org/docs/latest/rules/no-this-before-super - */ - "no-this-before-super": Linter.RuleEntry<[]>; - - /** - * Rule to disallow throwing literals as exceptions. - * - * @since 0.15.0 - * @see https://eslint.org/docs/latest/rules/no-throw-literal - */ - "no-throw-literal": Linter.RuleEntry<[]>; - - /** - * Rule to disallow trailing whitespace at the end of lines. - * - * @since 0.7.1 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`no-trailing-spaces`](https://eslint.style/rules/no-trailing-spaces) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/no-trailing-spaces - */ - "no-trailing-spaces": Linter.RuleEntry< - [ - Partial<{ - /** - * @default false - */ - skipBlankLines: boolean; - /** - * @default false - */ - ignoreComments: boolean; - }>, - ] - >; - - /** - * Rule to disallow `let` or `var` variables that are read but never assigned. - * - * @since 9.27.0 - * @see https://eslint.org/docs/latest/rules/no-unassigned-vars - */ - "no-unassigned-vars": Linter.RuleEntry<[]>; - - /** - * Rule to disallow the use of undeclared variables unless mentioned in \/*global *\/ comments. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 0.0.9 - * @see https://eslint.org/docs/latest/rules/no-undef - */ - "no-undef": Linter.RuleEntry< - [ - Partial<{ - /** - * @default false - */ - typeof: boolean; - }>, - ] - >; - - /** - * Rule to disallow initializing variables to `undefined`. - * - * @since 0.0.6 - * @see https://eslint.org/docs/latest/rules/no-undef-init - */ - "no-undef-init": Linter.RuleEntry<[]>; - - /** - * Rule to disallow the use of `undefined` as an identifier. - * - * @since 0.7.1 - * @see https://eslint.org/docs/latest/rules/no-undefined - */ - "no-undefined": Linter.RuleEntry<[]>; - - /** - * Rule to disallow dangling underscores in identifiers. - * - * @since 0.0.9 - * @see https://eslint.org/docs/latest/rules/no-underscore-dangle - */ - "no-underscore-dangle": Linter.RuleEntry< - [ - Partial<{ - allow: string[]; - /** - * @default false - */ - allowAfterThis: boolean; - /** - * @default false - */ - allowAfterSuper: boolean; - /** - * @since 6.7.0 - * @default false - */ - allowAfterThisConstructor: boolean; - /** - * @default false - */ - enforceInMethodNames: boolean; - /** - * @since 8.15.0 - * @default false - */ - enforceInClassFields: boolean; - /** - * @since 8.31.0 - * @default true - */ - allowInArrayDestructuring: boolean; - /** - * @since 8.31.0 - * @default true - */ - allowInObjectDestructuring: boolean; - /** - * @since 7.7.0 - * @default true - */ - allowFunctionParams: boolean; - }>, - ] - >; - - /** - * Rule to disallow confusing multiline expressions. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 0.24.0 - * @see https://eslint.org/docs/latest/rules/no-unexpected-multiline - */ - "no-unexpected-multiline": Linter.RuleEntry<[]>; - - /** - * Rule to disallow unmodified loop conditions. - * - * @since 2.0.0-alpha-2 - * @see https://eslint.org/docs/latest/rules/no-unmodified-loop-condition - */ - "no-unmodified-loop-condition": Linter.RuleEntry<[]>; - - /** - * Rule to disallow ternary operators when simpler alternatives exist. - * - * @since 0.21.0 - * @see https://eslint.org/docs/latest/rules/no-unneeded-ternary - */ - "no-unneeded-ternary": Linter.RuleEntry< - [ - Partial<{ - /** - * @default true - */ - defaultAssignment: boolean; - }>, - ] - >; - - /** - * Rule to disallow unreachable code after `return`, `throw`, `continue`, and `break` statements. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 0.0.6 - * @see https://eslint.org/docs/latest/rules/no-unreachable - */ - "no-unreachable": Linter.RuleEntry<[]>; - - /** - * Rule to disallow loops with a body that allows only one iteration. - * - * @since 7.3.0 - * @see https://eslint.org/docs/latest/rules/no-unreachable-loop - */ - "no-unreachable-loop": Linter.RuleEntry< - [ - Partial<{ - /** - * @default [] - */ - ignore: Array< - | "WhileStatement" - | "DoWhileStatement" - | "ForStatement" - | "ForInStatement" - | "ForOfStatement" - >; - }>, - ] - >; - - /** - * Rule to disallow control flow statements in `finally` blocks. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 2.9.0 - * @see https://eslint.org/docs/latest/rules/no-unsafe-finally - */ - "no-unsafe-finally": Linter.RuleEntry<[]>; - - /** - * Rule to disallow negating the left operand of relational operators. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 3.3.0 - * @see https://eslint.org/docs/latest/rules/no-unsafe-negation - */ - "no-unsafe-negation": Linter.RuleEntry< - [ - Partial<{ - /** - * @since 6.6.0 - * @default false - */ - enforceForOrderingRelations: boolean; - }>, - ] - >; - - /** - * Rule to disallow use of optional chaining in contexts where the `undefined` value is not allowed. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 7.15.0 - * @see https://eslint.org/docs/latest/rules/no-unsafe-optional-chaining - */ - "no-unsafe-optional-chaining": Linter.RuleEntry< - [ - Partial<{ - /** - * @default false - */ - disallowArithmeticOperators: boolean; - }>, - ] - >; - - /** - * Rule to disallow unused expressions. - * - * @since 0.1.0 - * @see https://eslint.org/docs/latest/rules/no-unused-expressions - */ - "no-unused-expressions": Linter.RuleEntry< - [ - Partial<{ - /** - * @default false - */ - allowShortCircuit: boolean; - /** - * @default false - */ - allowTernary: boolean; - /** - * @default false - */ - allowTaggedTemplates: boolean; - /** - * @since 7.20.0 - * @default false - */ - enforceForJSX: boolean; - /** - * @default false - */ - ignoreDirectives: boolean; - }>, - ] - >; - - /** - * Rule to disallow unused labels. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 2.0.0-rc.0 - * @see https://eslint.org/docs/latest/rules/no-unused-labels - */ - "no-unused-labels": Linter.RuleEntry<[]>; - - /** - * Rule to disallow unused private class members. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 8.1.0 - * @see https://eslint.org/docs/latest/rules/no-unused-private-class-members - */ - "no-unused-private-class-members": Linter.RuleEntry<[]>; - - /** - * Rule to disallow unused variables. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 0.0.9 - * @see https://eslint.org/docs/latest/rules/no-unused-vars - */ - "no-unused-vars": Linter.RuleEntry< - [ - | "all" - | "local" - | Partial<{ - /** - * @default 'all' - */ - vars: "all" | "local"; - varsIgnorePattern: string; - /** - * @default 'after-used' - */ - args: "after-used" | "all" | "none"; - /** - * @default false - */ - ignoreRestSiblings: boolean; - argsIgnorePattern: string; - /** - * @default 'all' - */ - caughtErrors: "none" | "all"; - caughtErrorsIgnorePattern: string; - destructuredArrayIgnorePattern: string; - /** - * @default false - */ - ignoreClassWithStaticInitBlock: boolean; - /** - * @default false - */ - ignoreUsingDeclarations: boolean; - /** - * @default false - */ - reportUsedIgnorePattern: boolean; - }>, - ] - >; - - /** - * Rule to disallow the use of variables before they are defined. - * - * @since 0.0.9 - * @see https://eslint.org/docs/latest/rules/no-use-before-define - */ - "no-use-before-define": Linter.RuleEntry< - [ - | Partial<{ - /** - * @default true - */ - functions: boolean; - /** - * @default true - */ - classes: boolean; - /** - * @default true - */ - variables: boolean; - /** - * @default false - */ - allowNamedExports: boolean; - /** - * @default true - */ - enums: boolean; - /** - * @default true - */ - typedefs: boolean; - /** - * @default true - */ - ignoreTypeReferences: boolean; - }> - | "nofunc", - ] - >; - - /** - * Rule to disallow variable assignments when the value is not used. - * - * @since 9.0.0-alpha.1 - * @see https://eslint.org/docs/latest/rules/no-useless-assignment - */ - "no-useless-assignment": Linter.RuleEntry<[]>; - - /** - * Rule to disallow useless backreferences in regular expressions. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 7.0.0-alpha.0 - * @see https://eslint.org/docs/latest/rules/no-useless-backreference - */ - "no-useless-backreference": Linter.RuleEntry<[]>; - - /** - * Rule to disallow unnecessary calls to `.call()` and `.apply()`. - * - * @since 1.0.0-rc-1 - * @see https://eslint.org/docs/latest/rules/no-useless-call - */ - "no-useless-call": Linter.RuleEntry<[]>; - - /** - * Rule to disallow unnecessary `catch` clauses. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 5.11.0 - * @see https://eslint.org/docs/latest/rules/no-useless-catch - */ - "no-useless-catch": Linter.RuleEntry<[]>; - - /** - * Rule to disallow unnecessary computed property keys in objects and classes. - * - * @since 2.9.0 - * @see https://eslint.org/docs/latest/rules/no-useless-computed-key - */ - "no-useless-computed-key": Linter.RuleEntry< - [ - Partial<{ - /** - * @default true - */ - enforceForClassMembers: boolean; - }>, - ] - >; - - /** - * Rule to disallow unnecessary concatenation of literals or template literals. - * - * @since 1.3.0 - * @see https://eslint.org/docs/latest/rules/no-useless-concat - */ - "no-useless-concat": Linter.RuleEntry<[]>; - - /** - * Rule to disallow unnecessary constructors. - * - * @since 2.0.0-beta.1 - * @see https://eslint.org/docs/latest/rules/no-useless-constructor - */ - "no-useless-constructor": Linter.RuleEntry<[]>; - - /** - * Rule to disallow unnecessary escape characters. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 2.5.0 - * @see https://eslint.org/docs/latest/rules/no-useless-escape - */ - "no-useless-escape": Linter.RuleEntry< - [ - Partial<{ - allowRegexCharacters: string[]; - }>, - ] - >; - - /** - * Rule to disallow renaming import, export, and destructured assignments to the same name. - * - * @since 2.11.0 - * @see https://eslint.org/docs/latest/rules/no-useless-rename - */ - "no-useless-rename": Linter.RuleEntry< - [ - Partial<{ - /** - * @default false - */ - ignoreImport: boolean; - /** - * @default false - */ - ignoreExport: boolean; - /** - * @default false - */ - ignoreDestructuring: boolean; - }>, - ] - >; - - /** - * Rule to disallow redundant return statements. - * - * @since 3.9.0 - * @see https://eslint.org/docs/latest/rules/no-useless-return - */ - "no-useless-return": Linter.RuleEntry<[]>; - - /** - * Rule to require `let` or `const` instead of `var`. - * - * @since 0.12.0 - * @see https://eslint.org/docs/latest/rules/no-var - */ - "no-var": Linter.RuleEntry<[]>; - - /** - * Rule to disallow `void` operators. - * - * @since 0.8.0 - * @see https://eslint.org/docs/latest/rules/no-void - */ - "no-void": Linter.RuleEntry< - [ - Partial<{ - /** - * @default false - */ - allowAsStatement: boolean; - }>, - ] - >; - - /** - * Rule to disallow specified warning terms in comments. - * - * @since 0.4.4 - * @see https://eslint.org/docs/latest/rules/no-warning-comments - */ - "no-warning-comments": Linter.RuleEntry< - [ - Partial<{ - /** - * @default ["todo", "fixme", "xxx"] - */ - terms: string[]; - /** - * @default 'start' - */ - location: "start" | "anywhere"; - decoration: string[]; - }>, - ] - >; - - /** - * Rule to disallow whitespace before properties. - * - * @since 2.0.0-beta.1 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`no-whitespace-before-property`](https://eslint.style/rules/no-whitespace-before-property) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/no-whitespace-before-property - */ - "no-whitespace-before-property": Linter.RuleEntry<[]>; - - /** - * Rule to disallow `with` statements. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 0.0.2 - * @see https://eslint.org/docs/latest/rules/no-with - */ - "no-with": Linter.RuleEntry<[]>; - - /** - * Rule to enforce the location of single-line statements. - * - * @since 3.17.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`nonblock-statement-body-position`](https://eslint.style/rules/nonblock-statement-body-position) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/nonblock-statement-body-position - */ - "nonblock-statement-body-position": Linter.RuleEntry< - [ - "beside" | "below" | "any", - Partial<{ - overrides: Record; - }>, - ] - >; - - /** - * Rule to enforce consistent line breaks after opening and before closing braces. - * - * @since 2.12.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`object-curly-newline`](https://eslint.style/rules/object-curly-newline) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/object-curly-newline - */ - "object-curly-newline": Linter.RuleEntry< - [ - | "always" - | "never" - | Partial<{ - /** - * @default false - */ - multiline: boolean; - minProperties: number; - /** - * @default true - */ - consistent: boolean; - }> - | Partial< - Record< - | "ObjectExpression" - | "ObjectPattern" - | "ImportDeclaration" - | "ExportDeclaration", - | "always" - | "never" - | Partial<{ - /** - * @default false - */ - multiline: boolean; - minProperties: number; - /** - * @default true - */ - consistent: boolean; - }> - > - >, - ] - >; - - /** - * Rule to enforce consistent spacing inside braces. - * - * @since 0.22.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`object-curly-spacing`](https://eslint.style/rules/object-curly-spacing) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/object-curly-spacing - */ - "object-curly-spacing": - | Linter.RuleEntry< - [ - "never", - { - /** - * @default false - */ - arraysInObjects: boolean; - /** - * @default false - */ - objectsInObjects: boolean; - }, - ] - > - | Linter.RuleEntry< - [ - "always", - { - /** - * @default true - */ - arraysInObjects: boolean; - /** - * @default true - */ - objectsInObjects: boolean; - }, - ] - >; - - /** - * Rule to enforce placing object properties on separate lines. - * - * @since 2.10.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`object-property-newline`](https://eslint.style/rules/object-property-newline) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/object-property-newline - */ - "object-property-newline": Linter.RuleEntry< - [ - Partial<{ - /** - * @default false - */ - allowAllPropertiesOnSameLine: boolean; - }>, - ] - >; - - /** - * Rule to require or disallow method and property shorthand syntax for object literals. - * - * @since 0.20.0 - * @see https://eslint.org/docs/latest/rules/object-shorthand - */ - "object-shorthand": - | Linter.RuleEntry< - [ - "always" | "methods", - Partial<{ - /** - * @default false - */ - avoidQuotes: boolean; - /** - * @default false - */ - ignoreConstructors: boolean; - /** - * @since 8.22.0 - */ - methodsIgnorePattern: string; - /** - * @default false - */ - avoidExplicitReturnArrows: boolean; - }>, - ] - > - | Linter.RuleEntry< - [ - "properties", - Partial<{ - /** - * @default false - */ - avoidQuotes: boolean; - }>, - ] - > - | Linter.RuleEntry<["never" | "consistent" | "consistent-as-needed"]>; - - /** - * Rule to enforce variables to be declared either together or separately in functions. - * - * @since 0.0.9 - * @see https://eslint.org/docs/latest/rules/one-var - */ - "one-var": Linter.RuleEntry< - [ - | "always" - | "never" - | "consecutive" - | Partial< - { - /** - * @default false - */ - separateRequires: boolean; - } & Record< - "var" | "let" | "const" | "using" | "awaitUsing", - "always" | "never" | "consecutive" - > - > - | Partial< - Record< - "initialized" | "uninitialized", - "always" | "never" | "consecutive" - > - >, - ] - >; - - /** - * Rule to require or disallow newlines around variable declarations. - * - * @since 2.0.0-beta.3 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`one-var-declaration-per-line`](https://eslint.style/rules/one-var-declaration-per-line) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/one-var-declaration-per-line - */ - "one-var-declaration-per-line": Linter.RuleEntry< - ["initializations" | "always"] - >; - - /** - * Rule to require or disallow assignment operator shorthand where possible. - * - * @since 0.10.0 - * @see https://eslint.org/docs/latest/rules/operator-assignment - */ - "operator-assignment": Linter.RuleEntry<["always" | "never"]>; - - /** - * Rule to enforce consistent linebreak style for operators. - * - * @since 0.19.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`operator-linebreak`](https://eslint.style/rules/operator-linebreak) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/operator-linebreak - */ - "operator-linebreak": Linter.RuleEntry< - [ - "after" | "before" | "none", - Partial<{ - overrides: Record; - }>, - ] - >; - - /** - * Rule to require or disallow padding within blocks. - * - * @since 0.9.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`padded-blocks`](https://eslint.style/rules/padded-blocks) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/padded-blocks - */ - "padded-blocks": Linter.RuleEntry< - [ - ( - | "always" - | "never" - | Partial< - Record< - "blocks" | "classes" | "switches", - "always" | "never" - > - > - ), - { - /** - * @default false - */ - allowSingleLineBlocks: boolean; - }, - ] - >; - - /** - * Rule to require or disallow padding lines between statements. - * - * @since 4.0.0-beta.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`padding-line-between-statements`](https://eslint.style/rules/padding-line-between-statements) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/padding-line-between-statements - */ - "padding-line-between-statements": Linter.RuleEntry< - [ - ...Array< - { - blankLine: "any" | "never" | "always"; - } & Record<"prev" | "next", string | string[]> - >, - ] - >; - - /** - * Rule to require using arrow functions for callbacks. - * - * @since 1.2.0 - * @see https://eslint.org/docs/latest/rules/prefer-arrow-callback - */ - "prefer-arrow-callback": Linter.RuleEntry< - [ - Partial<{ - /** - * @default false - */ - allowNamedFunctions: boolean; - /** - * @default true - */ - allowUnboundThis: boolean; - }>, - ] - >; - - /** - * Rule to require `const` declarations for variables that are never reassigned after declared. - * - * @since 0.23.0 - * @see https://eslint.org/docs/latest/rules/prefer-const - */ - "prefer-const": Linter.RuleEntry< - [ - Partial<{ - /** - * @default 'any' - */ - destructuring: "any" | "all"; - /** - * @default false - */ - ignoreReadBeforeAssign: boolean; - }>, - ] - >; - - /** - * Rule to require destructuring from arrays and/or objects. - * - * @since 3.13.0 - * @see https://eslint.org/docs/latest/rules/prefer-destructuring - */ - "prefer-destructuring": Linter.RuleEntry< - [ - Partial< - | { - VariableDeclarator: Partial<{ - array: boolean; - object: boolean; - }>; - AssignmentExpression: Partial<{ - array: boolean; - object: boolean; - }>; - } - | { - array: boolean; - object: boolean; - } - >, - Partial<{ - enforceForRenamedProperties: boolean; - }>, - ] - >; - - /** - * Rule to disallow the use of `Math.pow` in favor of the `**` operator. - * - * @since 6.7.0 - * @see https://eslint.org/docs/latest/rules/prefer-exponentiation-operator - */ - "prefer-exponentiation-operator": Linter.RuleEntry<[]>; - - /** - * Rule to enforce using named capture group in regular expression. - * - * @since 5.15.0 - * @see https://eslint.org/docs/latest/rules/prefer-named-capture-group - */ - "prefer-named-capture-group": Linter.RuleEntry<[]>; - - /** - * Rule to disallow `parseInt()` and `Number.parseInt()` in favor of binary, octal, and hexadecimal literals. - * - * @since 3.5.0 - * @see https://eslint.org/docs/latest/rules/prefer-numeric-literals - */ - "prefer-numeric-literals": Linter.RuleEntry<[]>; - - /** - * Rule to disallow use of `Object.prototype.hasOwnProperty.call()` and prefer use of `Object.hasOwn()`. - * - * @since 8.5.0 - * @see https://eslint.org/docs/latest/rules/prefer-object-has-own - */ - "prefer-object-has-own": Linter.RuleEntry<[]>; - - /** - * Rule to disallow using `Object.assign` with an object literal as the first argument and prefer the use of object spread instead. - * - * @since 5.0.0-alpha.3 - * @see https://eslint.org/docs/latest/rules/prefer-object-spread - */ - "prefer-object-spread": Linter.RuleEntry<[]>; - - /** - * Rule to require using Error objects as Promise rejection reasons. - * - * @since 3.14.0 - * @see https://eslint.org/docs/latest/rules/prefer-promise-reject-errors - */ - "prefer-promise-reject-errors": Linter.RuleEntry< - [ - Partial<{ - /** - * @default false - */ - allowEmptyReject: boolean; - }>, - ] - >; - - /** - * Rule to require `Reflect` methods where applicable. - * - * @since 1.0.0-rc-2 - * @deprecated since 3.9.0. - * The original intention of this rule was misguided. - * @see https://eslint.org/docs/latest/rules/prefer-reflect - */ - "prefer-reflect": Linter.RuleEntry< - [ - Partial<{ - exceptions: string[]; - }>, - ] - >; - - /** - * Rule to disallow use of the `RegExp` constructor in favor of regular expression literals. - * - * @since 6.4.0 - * @see https://eslint.org/docs/latest/rules/prefer-regex-literals - */ - "prefer-regex-literals": Linter.RuleEntry< - [ - Partial<{ - /** - * @default false - */ - disallowRedundantWrapping: boolean; - }>, - ] - >; - - /** - * Rule to require rest parameters instead of `arguments`. - * - * @since 2.0.0-alpha-1 - * @see https://eslint.org/docs/latest/rules/prefer-rest-params - */ - "prefer-rest-params": Linter.RuleEntry<[]>; - - /** - * Rule to require spread operators instead of `.apply()`. - * - * @since 1.0.0-rc-1 - * @see https://eslint.org/docs/latest/rules/prefer-spread - */ - "prefer-spread": Linter.RuleEntry<[]>; - - /** - * Rule to require template literals instead of string concatenation. - * - * @since 1.2.0 - * @see https://eslint.org/docs/latest/rules/prefer-template - */ - "prefer-template": Linter.RuleEntry<[]>; - - /** - * Rule to disallow losing originally caught error when re-throwing custom errors. - * - * @since 9.35.0 - * @see https://eslint.org/docs/latest/rules/preserve-caught-error - */ - "preserve-caught-error": Linter.RuleEntry< - [ - Partial<{ - /** - * @default false - */ - requireCatchParameter: boolean; - }>, - ] - >; - - /** - * Rule to require quotes around object literal property names. - * - * @since 0.0.6 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`quote-props`](https://eslint.style/rules/quote-props) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/quote-props - */ - "quote-props": - | Linter.RuleEntry<["always" | "consistent"]> - | Linter.RuleEntry< - [ - "as-needed", - Partial<{ - /** - * @default false - */ - keywords: boolean; - /** - * @default true - */ - unnecessary: boolean; - /** - * @default false - */ - numbers: boolean; - }>, - ] - > - | Linter.RuleEntry< - [ - "consistent-as-needed", - Partial<{ - /** - * @default false - */ - keywords: boolean; - }>, - ] - >; - - /** - * Rule to enforce the consistent use of either backticks, double, or single quotes. - * - * @since 0.0.7 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`quotes`](https://eslint.style/rules/quotes) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/quotes - */ - quotes: Linter.RuleEntry< - [ - "double" | "single" | "backtick", - Partial<{ - /** - * @default false - */ - avoidEscape: boolean; - /** - * @default false - */ - allowTemplateLiterals: boolean; - }>, - ] - >; - - /** - * Rule to enforce the consistent use of the radix argument when using `parseInt()`. - * - * @since 0.0.7 - * @see https://eslint.org/docs/latest/rules/radix - */ - radix: Linter.RuleEntry<["always" | "as-needed"]>; - - /** - * Rule to disallow assignments that can lead to race conditions due to usage of `await` or `yield`. - * - * @since 5.3.0 - * @see https://eslint.org/docs/latest/rules/require-atomic-updates - */ - "require-atomic-updates": Linter.RuleEntry< - [ - Partial<{ - /** - * @since 8.3.0 - * @default false - */ - allowProperties: boolean; - }>, - ] - >; - - /** - * Rule to disallow async functions which have no `await` expression. - * - * @since 3.11.0 - * @see https://eslint.org/docs/latest/rules/require-await - */ - "require-await": Linter.RuleEntry<[]>; - - /** - * Rule to enforce the use of `u` or `v` flag on regular expressions. - * - * @since 5.3.0 - * @see https://eslint.org/docs/latest/rules/require-unicode-regexp - */ - "require-unicode-regexp": Linter.RuleEntry< - [ - Partial<{ - /** - * @default false - */ - requireFlag: "u" | "v"; - }>, - ] - >; - - /** - * Rule to require generator functions to contain `yield`. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 1.0.0-rc-1 - * @see https://eslint.org/docs/latest/rules/require-yield - */ - "require-yield": Linter.RuleEntry<[]>; - - /** - * Rule to enforce spacing between rest and spread operators and their expressions. - * - * @since 2.12.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`rest-spread-spacing`](https://eslint.style/rules/rest-spread-spacing) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/rest-spread-spacing - */ - "rest-spread-spacing": Linter.RuleEntry<["never" | "always"]>; - - /** - * Rule to require or disallow semicolons instead of ASI. - * - * @since 0.0.6 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`semi`](https://eslint.style/rules/semi) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/semi - */ - semi: - | Linter.RuleEntry< - [ - "always", - Partial<{ - /** - * @default false - */ - omitLastInOneLineBlock: boolean; - }>, - ] - > - | Linter.RuleEntry< - [ - "never", - Partial<{ - /** - * @default 'any' - */ - beforeStatementContinuationChars: - | "any" - | "always" - | "never"; - }>, - ] - >; - - /** - * Rule to enforce consistent spacing before and after semicolons. - * - * @since 0.16.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`semi-spacing`](https://eslint.style/rules/semi-spacing) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/semi-spacing - */ - "semi-spacing": Linter.RuleEntry< - [ - Partial<{ - /** - * @default false - */ - before: boolean; - /** - * @default true - */ - after: boolean; - }>, - ] - >; - - /** - * Rule to enforce location of semicolons. - * - * @since 4.0.0-beta.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`semi-style`](https://eslint.style/rules/semi-style) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/semi-style - */ - "semi-style": Linter.RuleEntry<["last" | "first"]>; - - /** - * Rule to enforce sorted `import` declarations within modules. - * - * @since 2.0.0-beta.1 - * @see https://eslint.org/docs/latest/rules/sort-imports - */ - "sort-imports": Linter.RuleEntry< - [ - Partial<{ - /** - * @default false - */ - ignoreCase: boolean; - /** - * @default false - */ - ignoreDeclarationSort: boolean; - /** - * @default false - */ - ignoreMemberSort: boolean; - /** - * @default ['none', 'all', 'multiple', 'single'] - */ - memberSyntaxSortOrder: Array< - "none" | "all" | "multiple" | "single" - >; - /** - * @default false - */ - allowSeparatedGroups: boolean; - }>, - ] - >; - - /** - * Rule to require object keys to be sorted. - * - * @since 3.3.0 - * @see https://eslint.org/docs/latest/rules/sort-keys - */ - "sort-keys": Linter.RuleEntry< - [ - "asc" | "desc", - Partial<{ - /** - * @default true - */ - caseSensitive: boolean; - /** - * @default 2 - */ - minKeys: number; - /** - * @default false - */ - natural: boolean; - /** - * @default false - */ - allowLineSeparatedGroups: boolean; - /** - * @default false - */ - ignoreComputedKeys: boolean; - }>, - ] - >; - - /** - * Rule to require variables within the same declaration block to be sorted. - * - * @since 0.2.0 - * @see https://eslint.org/docs/latest/rules/sort-vars - */ - "sort-vars": Linter.RuleEntry< - [ - Partial<{ - /** - * @default false - */ - ignoreCase: boolean; - }>, - ] - >; - - /** - * Rule to enforce consistent spacing before blocks. - * - * @since 0.9.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`space-before-blocks`](https://eslint.style/rules/space-before-blocks) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/space-before-blocks - */ - "space-before-blocks": Linter.RuleEntry< - [ - | "always" - | "never" - | Partial< - Record< - "functions" | "keywords" | "classes", - "always" | "never" | "off" - > - >, - ] - >; - - /** - * Rule to enforce consistent spacing before `function` definition opening parenthesis. - * - * @since 0.18.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`space-before-function-paren`](https://eslint.style/rules/space-before-function-paren) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/space-before-function-paren - */ - "space-before-function-paren": Linter.RuleEntry< - [ - | "always" - | "never" - | Partial< - Record< - "anonymous" | "named" | "asyncArrow", - "always" | "never" | "ignore" - > - >, - ] - >; - - /** - * Rule to enforce consistent spacing inside parentheses. - * - * @since 0.8.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`space-in-parens`](https://eslint.style/rules/space-in-parens) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/space-in-parens - */ - "space-in-parens": Linter.RuleEntry< - [ - "never" | "always", - Partial<{ - exceptions: string[]; - }>, - ] - >; - - /** - * Rule to require spacing around infix operators. - * - * @since 0.2.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`space-infix-ops`](https://eslint.style/rules/space-infix-ops) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/space-infix-ops - */ - "space-infix-ops": Linter.RuleEntry< - [ - Partial<{ - /** - * @default false - */ - int32Hint: boolean; - }>, - ] - >; - - /** - * Rule to enforce consistent spacing before or after unary operators. - * - * @since 0.10.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`space-unary-ops`](https://eslint.style/rules/space-unary-ops) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/space-unary-ops - */ - "space-unary-ops": Linter.RuleEntry< - [ - Partial<{ - /** - * @default true - */ - words: boolean; - /** - * @default false - */ - nonwords: boolean; - overrides: Record; - }>, - ] - >; - - /** - * Rule to enforce consistent spacing after the `//` or `/*` in a comment. - * - * @since 0.23.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`spaced-comment`](https://eslint.style/rules/spaced-comment) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/spaced-comment - */ - "spaced-comment": Linter.RuleEntry< - [ - "always" | "never", - { - exceptions: string[]; - markers: string[]; - line: { - exceptions: string[]; - markers: string[]; - }; - block: { - exceptions: string[]; - markers: string[]; - /** - * @default false - */ - balanced: boolean; - }; - }, - ] - >; - - /** - * Rule to require or disallow strict mode directives. - * - * @since 0.1.0 - * @see https://eslint.org/docs/latest/rules/strict - */ - strict: Linter.RuleEntry<["safe" | "global" | "function" | "never"]>; - - /** - * Rule to enforce spacing around colons of switch statements. - * - * @since 4.0.0-beta.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`switch-colon-spacing`](https://eslint.style/rules/switch-colon-spacing) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/switch-colon-spacing - */ - "switch-colon-spacing": Linter.RuleEntry< - [ - Partial<{ - /** - * @default false - */ - before: boolean; - /** - * @default true - */ - after: boolean; - }>, - ] - >; - - /** - * Rule to require symbol descriptions. - * - * @since 3.4.0 - * @see https://eslint.org/docs/latest/rules/symbol-description - */ - "symbol-description": Linter.RuleEntry<[]>; - - /** - * Rule to require or disallow spacing around embedded expressions of template strings. - * - * @since 2.0.0-rc.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`template-curly-spacing`](https://eslint.style/rules/template-curly-spacing) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/template-curly-spacing - */ - "template-curly-spacing": Linter.RuleEntry<["never" | "always"]>; - - /** - * Rule to require or disallow spacing between template tags and their literals. - * - * @since 3.15.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`template-tag-spacing`](https://eslint.style/rules/template-tag-spacing) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/template-tag-spacing - */ - "template-tag-spacing": Linter.RuleEntry<["never" | "always"]>; - - /** - * Rule to require or disallow Unicode byte order mark (BOM). - * - * @since 2.11.0 - * @see https://eslint.org/docs/latest/rules/unicode-bom - */ - "unicode-bom": Linter.RuleEntry<["never" | "always"]>; - - /** - * Rule to require calls to `isNaN()` when checking for `NaN`. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 0.0.6 - * @see https://eslint.org/docs/latest/rules/use-isnan - */ - "use-isnan": Linter.RuleEntry< - [ - Partial<{ - /** - * @default true - */ - enforceForSwitchCase: boolean; - /** - * @default false - */ - enforceForIndexOf: boolean; - }>, - ] - >; - - /** - * Rule to enforce comparing `typeof` expressions against valid strings. - * - * @remarks - * Recommended by ESLint, the rule was enabled in `eslint:recommended`. - * - * @since 0.5.0 - * @see https://eslint.org/docs/latest/rules/valid-typeof - */ - "valid-typeof": Linter.RuleEntry< - [ - Partial<{ - /** - * @default false - */ - requireStringLiterals: boolean; - }>, - ] - >; - - /** - * Rule to require `var` declarations be placed at the top of their containing scope. - * - * @since 0.8.0 - * @see https://eslint.org/docs/latest/rules/vars-on-top - */ - "vars-on-top": Linter.RuleEntry<[]>; - - /** - * Rule to require parentheses around immediate `function` invocations. - * - * @since 0.0.9 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`wrap-iife`](https://eslint.style/rules/wrap-iife) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/wrap-iife - */ - "wrap-iife": Linter.RuleEntry< - [ - "outside" | "inside" | "any", - Partial<{ - /** - * @default false - */ - functionPrototypeMethods: boolean; - }>, - ] - >; - - /** - * Rule to require parenthesis around regex literals. - * - * @since 0.1.0 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`wrap-regex`](https://eslint.style/rules/wrap-regex) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/wrap-regex - */ - "wrap-regex": Linter.RuleEntry<[]>; - - /** - * Rule to require or disallow spacing around the `*` in `yield*` expressions. - * - * @since 2.0.0-alpha-1 - * @deprecated since 8.53.0. - * Formatting rules are being moved out of ESLint core. - * Please, use [`yield-star-spacing`](https://eslint.style/rules/yield-star-spacing) in [`@stylistic/eslint-plugin`](https://eslint.style). - * @see https://eslint.org/docs/latest/rules/yield-star-spacing - */ - "yield-star-spacing": Linter.RuleEntry< - [ - | Partial<{ - before: boolean; - after: boolean; - }> - | "before" - | "after" - | "both" - | "neither", - ] - >; - - /** - * Rule to require or disallow "Yoda" conditions. - * - * @since 0.7.1 - * @see https://eslint.org/docs/latest/rules/yoda - */ - yoda: - | Linter.RuleEntry< - [ - "never", - Partial<{ - exceptRange: boolean; - onlyEquality: boolean; - }>, - ] - > - | Linter.RuleEntry<["always"]>; -} diff --git a/node_modules/eslint/lib/types/universal.d.ts b/node_modules/eslint/lib/types/universal.d.ts deleted file mode 100644 index df4fdecda..000000000 --- a/node_modules/eslint/lib/types/universal.d.ts +++ /dev/null @@ -1,6 +0,0 @@ -/** - * @fileoverview typings for "eslint/universal" module - * @author 唯然 - */ - -export { Linter } from "./index"; diff --git a/node_modules/eslint/lib/types/use-at-your-own-risk.d.ts b/node_modules/eslint/lib/types/use-at-your-own-risk.d.ts deleted file mode 100644 index f97f4f341..000000000 --- a/node_modules/eslint/lib/types/use-at-your-own-risk.d.ts +++ /dev/null @@ -1,87 +0,0 @@ -/** - * @fileoverview This file contains the types for the use-at-your-own-risk - * entrypoint. It was initially extracted from the `@types/eslint` package. - */ - -/* - * MIT License - * Copyright (c) Microsoft Corporation. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE - */ - -import { ESLint, Rule } from "./index.js"; - -/** @deprecated */ -export const builtinRules: Map; - -/** @deprecated */ -export class FileEnumerator { - constructor(params?: { - cwd?: string; - configArrayFactory?: any; - extensions?: any; - globInputPaths?: boolean; - errorOnUnmatchedPattern?: boolean; - ignore?: boolean; - }); - isTargetPath(filePath: string, providedConfig?: any): boolean; - iterateFiles( - patternOrPatterns: string | string[], - ): IterableIterator<{ config: any; filePath: string; ignored: boolean }>; -} - -export { /** @deprecated */ ESLint as FlatESLint }; - -/** @deprecated */ -export class LegacyESLint { - static configType: "eslintrc"; - - static readonly version: string; - - static outputFixes(results: ESLint.LintResult[]): Promise; - - static getErrorResults(results: ESLint.LintResult[]): ESLint.LintResult[]; - - constructor(options?: ESLint.LegacyOptions); - - lintFiles(patterns: string | string[]): Promise; - - lintText( - code: string, - options?: { - filePath?: string | undefined; - warnIgnored?: boolean | undefined; - }, - ): Promise; - - getRulesMetaForResults( - results: ESLint.LintResult[], - ): ESLint.LintResultData["rulesMeta"]; - - hasFlag(flag: string): false; - - calculateConfigForFile(filePath: string): Promise; - - isPathIgnored(filePath: string): Promise; - - loadFormatter(nameOrPath?: string): Promise; -} - -/** @deprecated */ -export function shouldUseFlatConfig(): Promise; diff --git a/node_modules/eslint/lib/universal.js b/node_modules/eslint/lib/universal.js deleted file mode 100644 index 51bac5845..000000000 --- a/node_modules/eslint/lib/universal.js +++ /dev/null @@ -1,10 +0,0 @@ -/** - * @fileoverview exports for browsers - * @author 唯然 - */ - -"use strict"; - -const { Linter } = require("./linter/linter"); - -module.exports = { Linter }; diff --git a/node_modules/eslint/lib/unsupported-api.js b/node_modules/eslint/lib/unsupported-api.js index 2287728e1..8a2e147aa 100644 --- a/node_modules/eslint/lib/unsupported-api.js +++ b/node_modules/eslint/lib/unsupported-api.js @@ -12,18 +12,19 @@ //----------------------------------------------------------------------------- const { FileEnumerator } = require("./cli-engine/file-enumerator"); -const { ESLint: FlatESLint, shouldUseFlatConfig } = require("./eslint/eslint"); -const { LegacyESLint } = require("./eslint/legacy-eslint"); -const builtinRules = require("./rules"); +const { FlatESLint, shouldUseFlatConfig } = require("./eslint/flat-eslint"); +const FlatRuleTester = require("./rule-tester/flat-rule-tester"); +const { ESLint } = require("./eslint/eslint"); //----------------------------------------------------------------------------- // Exports //----------------------------------------------------------------------------- module.exports = { - builtinRules, - FlatESLint, - shouldUseFlatConfig, - FileEnumerator, - LegacyESLint, + builtinRules: require("./rules"), + FlatESLint, + shouldUseFlatConfig, + FlatRuleTester, + FileEnumerator, + LegacyESLint: ESLint }; diff --git a/node_modules/eslint/messages/all-files-ignored.js b/node_modules/eslint/messages/all-files-ignored.js index 3e795c9a4..70877a4d8 100644 --- a/node_modules/eslint/messages/all-files-ignored.js +++ b/node_modules/eslint/messages/all-files-ignored.js @@ -1,9 +1,9 @@ "use strict"; -module.exports = function (it) { - const { pattern } = it; +module.exports = function(it) { + const { pattern } = it; - return ` + return ` You are linting "${pattern}", but all of the files matching the glob pattern "${pattern}" are ignored. If you don't want to lint these files, remove the pattern "${pattern}" from the list of arguments passed to ESLint. diff --git a/node_modules/eslint/messages/all-matched-files-ignored.js b/node_modules/eslint/messages/all-matched-files-ignored.js deleted file mode 100644 index 2621343bf..000000000 --- a/node_modules/eslint/messages/all-matched-files-ignored.js +++ /dev/null @@ -1,21 +0,0 @@ -"use strict"; - -module.exports = function (it) { - const { pattern } = it; - - return ` -You are linting "${pattern}", but all of the files matching the glob pattern "${pattern}" are ignored. - -If you don't want to lint these files, remove the pattern "${pattern}" from the list of arguments passed to ESLint. - -If you do want to lint these files, explicitly list one or more of the files from this glob that you'd like to lint to see more details about why they are ignored. - - * If the file is ignored because of a matching ignore pattern, check global ignores in your config file. - https://eslint.org/docs/latest/use/configure/ignore - - * If the file is ignored because no matching configuration was supplied, check file patterns in your config file. - https://eslint.org/docs/latest/use/configure/configuration-files#specifying-files-with-arbitrary-extensions - - * If the file is ignored because it is located outside of the base path, change the location of your config file to be in a parent directory. -`.trimStart(); -}; diff --git a/node_modules/eslint/messages/config-file-missing.js b/node_modules/eslint/messages/config-file-missing.js deleted file mode 100644 index c4f8ecdbf..000000000 --- a/node_modules/eslint/messages/config-file-missing.js +++ /dev/null @@ -1,16 +0,0 @@ -"use strict"; - -module.exports = function () { - return ` -ESLint couldn't find an eslint.config.(js|mjs|cjs) file. - -From ESLint v9.0.0, the default configuration file is now eslint.config.js. -If you are using a .eslintrc.* file, please follow the migration guide -to update your configuration file to the new format: - -https://eslint.org/docs/latest/use/configure/migration-guide - -If you still have problems after following the migration guide, please stop by -https://eslint.org/chat/help to chat with the team. -`.trimStart(); -}; diff --git a/node_modules/eslint/messages/config-plugin-missing.js b/node_modules/eslint/messages/config-plugin-missing.js deleted file mode 100644 index 207367491..000000000 --- a/node_modules/eslint/messages/config-plugin-missing.js +++ /dev/null @@ -1,14 +0,0 @@ -"use strict"; - -module.exports = function (it) { - const { pluginName, ruleId } = it; - - return ` -A configuration object specifies rule "${ruleId}", but could not find plugin "${pluginName}". - -Common causes of this problem include: - -1. The "${pluginName}" plugin is not defined in your configuration file. -2. The "${pluginName}" plugin is not defined within the same configuration object in which the "${ruleId}" rule is applied. -`.trimStart(); -}; diff --git a/node_modules/eslint/messages/config-serialize-function.js b/node_modules/eslint/messages/config-serialize-function.js deleted file mode 100644 index 4300e47e4..000000000 --- a/node_modules/eslint/messages/config-serialize-function.js +++ /dev/null @@ -1,30 +0,0 @@ -"use strict"; - -module.exports = function ({ key, objectKey }) { - // special case for parsers - const isParser = - objectKey === "parser" && (key === "parse" || key === "parseForESLint"); - const parserMessage = ` - This typically happens when you're using a custom parser that does not -provide a "meta" property, which is how ESLint determines the serialized -representation. Please open an issue with the maintainer of the custom parser -and share this link: - -https://eslint.org/docs/latest/extend/custom-parsers#meta-data-in-custom-parsers -`.trim(); - - return ` -The requested operation requires ESLint to serialize configuration data, -but the configuration key "${objectKey}.${key}" contains a function value, -which cannot be serialized. - -${ - isParser - ? parserMessage - : "Please double-check your configuration for errors." -} - -If you still have problems, please stop by https://eslint.org/chat/help to chat -with the team. -`.trimStart(); -}; diff --git a/node_modules/eslint/messages/eslintrc-incompat.js b/node_modules/eslint/messages/eslintrc-incompat.js index 27fe8c9f4..ee77cb232 100644 --- a/node_modules/eslint/messages/eslintrc-incompat.js +++ b/node_modules/eslint/messages/eslintrc-incompat.js @@ -3,55 +3,44 @@ /* eslint consistent-return: 0 -- no default case */ const messages = { - env: ` + + env: ` A config object is using the "env" key, which is not supported in flat config system. Flat config uses "languageOptions.globals" to define global variables for your files. Please see the following page for information on how to convert your config object into the correct format: https://eslint.org/docs/latest/use/configure/migration-guide#configuring-language-options - -If you're not using "env" directly (it may be coming from a plugin), please see the following: -https://eslint.org/docs/latest/use/configure/migration-guide#using-eslintrc-configs-in-flat-config `, - extends: ` + extends: ` A config object is using the "extends" key, which is not supported in flat config system. Instead of "extends", you can include config objects that you'd like to extend from directly in the flat config array. -If you're using "extends" in your config file, please see the following: +Please see the following page for more information: https://eslint.org/docs/latest/use/configure/migration-guide#predefined-and-shareable-configs - -If you're not using "extends" directly (it may be coming from a plugin), please see the following: -https://eslint.org/docs/latest/use/configure/migration-guide#using-eslintrc-configs-in-flat-config `, - globals: ` + globals: ` A config object is using the "globals" key, which is not supported in flat config system. Flat config uses "languageOptions.globals" to define global variables for your files. Please see the following page for information on how to convert your config object into the correct format: https://eslint.org/docs/latest/use/configure/migration-guide#configuring-language-options - -If you're not using "globals" directly (it may be coming from a plugin), please see the following: -https://eslint.org/docs/latest/use/configure/migration-guide#using-eslintrc-configs-in-flat-config `, - ignorePatterns: ` + ignorePatterns: ` A config object is using the "ignorePatterns" key, which is not supported in flat config system. Flat config uses "ignores" to specify files to ignore. Please see the following page for information on how to convert your config object into the correct format: https://eslint.org/docs/latest/use/configure/migration-guide#ignoring-files - -If you're not using "ignorePatterns" directly (it may be coming from a plugin), please see the following: -https://eslint.org/docs/latest/use/configure/migration-guide#using-eslintrc-configs-in-flat-config `, - noInlineConfig: ` + noInlineConfig: ` A config object is using the "noInlineConfig" key, which is not supported in flat config system. Flat config uses "linterOptions.noInlineConfig" to specify files to ignore. @@ -60,43 +49,34 @@ Please see the following page for information on how to convert your config obje https://eslint.org/docs/latest/use/configure/migration-guide#linter-options `, - overrides: ` + overrides: ` A config object is using the "overrides" key, which is not supported in flat config system. Flat config is an array that acts like the eslintrc "overrides" array. Please see the following page for information on how to convert your config object into the correct format: https://eslint.org/docs/latest/use/configure/migration-guide#glob-based-configs - -If you're not using "overrides" directly (it may be coming from a plugin), please see the following: -https://eslint.org/docs/latest/use/configure/migration-guide#using-eslintrc-configs-in-flat-config `, - parser: ` + parser: ` A config object is using the "parser" key, which is not supported in flat config system. Flat config uses "languageOptions.parser" to override the default parser. Please see the following page for information on how to convert your config object into the correct format: https://eslint.org/docs/latest/use/configure/migration-guide#custom-parsers - -If you're not using "parser" directly (it may be coming from a plugin), please see the following: -https://eslint.org/docs/latest/use/configure/migration-guide#using-eslintrc-configs-in-flat-config `, - parserOptions: ` + parserOptions: ` A config object is using the "parserOptions" key, which is not supported in flat config system. Flat config uses "languageOptions.parserOptions" to specify parser options. Please see the following page for information on how to convert your config object into the correct format: https://eslint.org/docs/latest/use/configure/migration-guide#configuring-language-options - -If you're not using "parserOptions" directly (it may be coming from a plugin), please see the following: -https://eslint.org/docs/latest/use/configure/migration-guide#using-eslintrc-configs-in-flat-config `, - reportUnusedDisableDirectives: ` + reportUnusedDisableDirectives: ` A config object is using the "reportUnusedDisableDirectives" key, which is not supported in flat config system. Flat config uses "linterOptions.reportUnusedDisableDirectives" to specify files to ignore. @@ -105,13 +85,14 @@ Please see the following page for information on how to convert your config obje https://eslint.org/docs/latest/use/configure/migration-guide#linter-options `, - root: ` + root: ` A config object is using the "root" key, which is not supported in flat config system. Flat configs always act as if they are the root config file, so this key can be safely removed. -`, +` }; -module.exports = function ({ key }) { - return messages[key].trim(); +module.exports = function({ key }) { + + return messages[key].trim(); }; diff --git a/node_modules/eslint/messages/eslintrc-plugins.js b/node_modules/eslint/messages/eslintrc-plugins.js index 68cee2099..bb708c95b 100644 --- a/node_modules/eslint/messages/eslintrc-plugins.js +++ b/node_modules/eslint/messages/eslintrc-plugins.js @@ -1,16 +1,13 @@ "use strict"; -module.exports = function ({ plugins }) { - const isArrayOfStrings = typeof plugins[0] === "string"; +module.exports = function({ plugins }) { - return ` -A config object has a "plugins" key defined as an array${isArrayOfStrings ? " of strings" : ""}. It looks something like this: + const isArrayOfStrings = typeof plugins[0] === "string"; - { - "plugins": ${JSON.stringify(plugins)} - } + return ` +A config object has a "plugins" key defined as an array${isArrayOfStrings ? " of strings" : ""}. -Flat config requires "plugins" to be an object, like this: +Flat config requires "plugins" to be an object in this form: { plugins: { diff --git a/node_modules/eslint/messages/extend-config-missing.js b/node_modules/eslint/messages/extend-config-missing.js index 32e41e960..5b3498fcd 100644 --- a/node_modules/eslint/messages/extend-config-missing.js +++ b/node_modules/eslint/messages/extend-config-missing.js @@ -1,9 +1,9 @@ "use strict"; -module.exports = function (it) { - const { configName, importerName } = it; +module.exports = function(it) { + const { configName, importerName } = it; - return ` + return ` ESLint couldn't find the config "${configName}" to extend from. Please check that the name of the config is correct. The config "${configName}" was referenced from the config file in "${importerName}". diff --git a/node_modules/eslint/messages/failed-to-read-json.js b/node_modules/eslint/messages/failed-to-read-json.js index 3244e8428..e7c6cb587 100644 --- a/node_modules/eslint/messages/failed-to-read-json.js +++ b/node_modules/eslint/messages/failed-to-read-json.js @@ -1,9 +1,9 @@ "use strict"; -module.exports = function (it) { - const { path, message } = it; +module.exports = function(it) { + const { path, message } = it; - return ` + return ` Failed to read JSON file at ${path}: ${message} diff --git a/node_modules/eslint/messages/file-not-found.js b/node_modules/eslint/messages/file-not-found.js index 50e483bc1..1a62fcf96 100644 --- a/node_modules/eslint/messages/file-not-found.js +++ b/node_modules/eslint/messages/file-not-found.js @@ -1,9 +1,9 @@ "use strict"; -module.exports = function (it) { - const { pattern, globDisabled } = it; +module.exports = function(it) { + const { pattern, globDisabled } = it; - return ` + return ` No files matching the pattern "${pattern}"${globDisabled ? " (with disabling globs)" : ""} were found. Please check for typing mistakes in the pattern. `.trimStart(); diff --git a/node_modules/eslint/messages/invalid-rule-options.js b/node_modules/eslint/messages/invalid-rule-options.js index 05354cb48..9a8acc934 100644 --- a/node_modules/eslint/messages/invalid-rule-options.js +++ b/node_modules/eslint/messages/invalid-rule-options.js @@ -2,8 +2,8 @@ const { stringifyValueForError } = require("./shared"); -module.exports = function ({ ruleId, value }) { - return ` +module.exports = function({ ruleId, value }) { + return ` Configuration for rule "${ruleId}" is invalid. Each rule must have a severity ("off", 0, "warn", 1, "error", or 2) and may be followed by additional options for the rule. You passed '${stringifyValueForError(value, 4)}', which doesn't contain a valid severity. diff --git a/node_modules/eslint/messages/invalid-rule-severity.js b/node_modules/eslint/messages/invalid-rule-severity.js index 17e9b5d46..3f13183c6 100644 --- a/node_modules/eslint/messages/invalid-rule-severity.js +++ b/node_modules/eslint/messages/invalid-rule-severity.js @@ -2,8 +2,8 @@ const { stringifyValueForError } = require("./shared"); -module.exports = function ({ ruleId, value }) { - return ` +module.exports = function({ ruleId, value }) { + return ` Configuration for rule "${ruleId}" is invalid. Expected severity of "off", 0, "warn", 1, "error", or 2. You passed '${stringifyValueForError(value, 4)}'. diff --git a/node_modules/eslint/messages/no-config-found.js b/node_modules/eslint/messages/no-config-found.js index 4421206df..21cf549eb 100644 --- a/node_modules/eslint/messages/no-config-found.js +++ b/node_modules/eslint/messages/no-config-found.js @@ -1,12 +1,12 @@ "use strict"; -module.exports = function (it) { - const { directoryPath } = it; +module.exports = function(it) { + const { directoryPath } = it; - return ` + return ` ESLint couldn't find a configuration file. To set up a configuration file for this project, please run: - npm init @eslint/config@latest + npm init @eslint/config ESLint looked for configuration files in ${directoryPath} and its ancestors. If it found none, it then looked in your home directory. diff --git a/node_modules/eslint/messages/plugin-conflict.js b/node_modules/eslint/messages/plugin-conflict.js index d90df0cae..c8c060e2f 100644 --- a/node_modules/eslint/messages/plugin-conflict.js +++ b/node_modules/eslint/messages/plugin-conflict.js @@ -1,22 +1,22 @@ "use strict"; -module.exports = function (it) { - const { pluginId, plugins } = it; +module.exports = function(it) { + const { pluginId, plugins } = it; - let result = `ESLint couldn't determine the plugin "${pluginId}" uniquely. + let result = `ESLint couldn't determine the plugin "${pluginId}" uniquely. `; - for (const { filePath, importerName } of plugins) { - result += ` + for (const { filePath, importerName } of plugins) { + result += ` - ${filePath} (loaded in "${importerName}")`; - } + } - result += ` + result += ` Please remove the "plugins" setting from either config or remove either plugin installation. -If you still can't figure out the problem, please see https://eslint.org/docs/latest/use/troubleshooting. +If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team. `; - return result; + return result; }; diff --git a/node_modules/eslint/messages/plugin-invalid.js b/node_modules/eslint/messages/plugin-invalid.js index fc9ff6ab5..8b471d4a3 100644 --- a/node_modules/eslint/messages/plugin-invalid.js +++ b/node_modules/eslint/messages/plugin-invalid.js @@ -1,9 +1,9 @@ "use strict"; -module.exports = function (it) { - const { configName, importerName } = it; +module.exports = function(it) { + const { configName, importerName } = it; - return ` + return ` "${configName}" is invalid syntax for a config specifier. * If your intention is to extend from a configuration exported from the plugin, add the configuration name after a slash: e.g. "${configName}/myConfig". @@ -11,6 +11,6 @@ module.exports = function (it) { "${configName}" was referenced from the config file in "${importerName}". -If you still can't figure out the problem, please see https://eslint.org/docs/latest/use/troubleshooting. +If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team. `.trimStart(); }; diff --git a/node_modules/eslint/messages/plugin-missing.js b/node_modules/eslint/messages/plugin-missing.js index 6152a6a44..0b7d34e3a 100644 --- a/node_modules/eslint/messages/plugin-missing.js +++ b/node_modules/eslint/messages/plugin-missing.js @@ -1,9 +1,9 @@ "use strict"; -module.exports = function (it) { - const { pluginName, resolvePluginsRelativeTo, importerName } = it; +module.exports = function(it) { + const { pluginName, resolvePluginsRelativeTo, importerName } = it; - return ` + return ` ESLint couldn't find the plugin "${pluginName}". (The package "${pluginName}" was not found when loaded as a Node module from the directory "${resolvePluginsRelativeTo}".) @@ -14,6 +14,6 @@ It's likely that the plugin isn't installed correctly. Try reinstalling by runni The plugin "${pluginName}" was referenced from the config file in "${importerName}". -If you still can't figure out the problem, please see https://eslint.org/docs/latest/use/troubleshooting. +If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team. `.trimStart(); }; diff --git a/node_modules/eslint/messages/print-config-with-directory-path.js b/node_modules/eslint/messages/print-config-with-directory-path.js index 9f37745f2..4559c8d6d 100644 --- a/node_modules/eslint/messages/print-config-with-directory-path.js +++ b/node_modules/eslint/messages/print-config-with-directory-path.js @@ -1,7 +1,7 @@ "use strict"; -module.exports = function () { - return ` +module.exports = function() { + return ` The '--print-config' CLI option requires a path to a source code file rather than a directory. See also: https://eslint.org/docs/latest/use/command-line-interface#--print-config `.trimStart(); diff --git a/node_modules/eslint/messages/shared.js b/node_modules/eslint/messages/shared.js index 4244ed680..8c6e9b921 100644 --- a/node_modules/eslint/messages/shared.js +++ b/node_modules/eslint/messages/shared.js @@ -12,12 +12,7 @@ * @returns {string} The value, stringified. */ function stringifyValueForError(value, indentation) { - return value - ? JSON.stringify(value, null, 4).replace( - /\n/gu, - `\n${" ".repeat(indentation)}`, - ) - : `${value}`; + return value ? JSON.stringify(value, null, 4).replace(/\n/gu, `\n${" ".repeat(indentation)}`) : `${value}`; } module.exports = { stringifyValueForError }; diff --git a/node_modules/eslint/messages/whitespace-found.js b/node_modules/eslint/messages/whitespace-found.js index c61602165..8a801bcec 100644 --- a/node_modules/eslint/messages/whitespace-found.js +++ b/node_modules/eslint/messages/whitespace-found.js @@ -1,9 +1,9 @@ "use strict"; -module.exports = function (it) { - const { pluginName } = it; +module.exports = function(it) { + const { pluginName } = it; - return ` + return ` ESLint couldn't find the plugin "${pluginName}". because there is whitespace in the name. Please check your configuration and remove all whitespace from the plugin name. If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team. diff --git a/node_modules/eslint/package.json b/node_modules/eslint/package.json index 0df79bac9..8517c3170 100644 --- a/node_modules/eslint/package.json +++ b/node_modules/eslint/package.json @@ -1,66 +1,27 @@ { "name": "eslint", - "version": "9.39.2", + "version": "8.57.1", "author": "Nicholas C. Zakas ", "description": "An AST-based pattern checker for JavaScript.", - "type": "commonjs", "bin": { "eslint": "./bin/eslint.js" }, "main": "./lib/api.js", - "types": "./lib/types/index.d.ts", "exports": { - ".": { - "types": "./lib/types/index.d.ts", - "default": "./lib/api.js" - }, - "./config": { - "types": "./lib/types/config-api.d.ts", - "default": "./lib/config-api.js" - }, "./package.json": "./package.json", - "./use-at-your-own-risk": { - "types": "./lib/types/use-at-your-own-risk.d.ts", - "default": "./lib/unsupported-api.js" - }, - "./rules": { - "types": "./lib/types/rules.d.ts" - }, - "./universal": { - "types": "./lib/types/universal.d.ts", - "default": "./lib/universal.js" - } - }, - "typesVersions": { - "*": { - "use-at-your-own-risk": [ - "./lib/types/use-at-your-own-risk.d.ts" - ], - "rules": [ - "./lib/types/rules.d.ts" - ], - "universal": [ - "./lib/types/universal.d.ts" - ], - "config": [ - "./lib/types/config-api.d.ts" - ] - } + ".": "./lib/api.js", + "./use-at-your-own-risk": "./lib/unsupported-api.js" }, "scripts": { "build:docs:update-links": "node tools/fetch-docs-links.js", "build:site": "node Makefile.js gensite", "build:webpack": "node Makefile.js webpack", "build:readme": "node tools/update-readme.js", - "build:rules-index": "node Makefile.js generateRuleIndexPage", - "lint": "trunk check --no-fix --ignore=docs/**/*.js -a --filter=eslint && trunk check --no-fix --ignore=docs/**/*.js", - "lint:docs:js": "trunk check --no-fix --ignore=** --ignore=!docs/**/*.js -a --filter=eslint && trunk check --no-fix --ignore=** --ignore=!docs/**/*.js", + "lint": "node Makefile.js lint", + "lint:docs:js": "node Makefile.js lintDocsJS", "lint:docs:rule-examples": "node Makefile.js checkRuleExamples", - "lint:unused": "knip", - "lint:fix": "trunk check -y --ignore=docs/**/*.js -a --filter=eslint && trunk check -y --ignore=docs/**/*.js", - "lint:fix:docs:js": "trunk check -y --ignore=** --ignore=!docs/**/*.js -a --filter=eslint && trunk check -y --ignore=** --ignore=!docs/**/*.js", - "lint:rule-types": "node tools/update-rule-type-headers.js --check", - "lint:types": "attw --pack", + "lint:fix": "node Makefile.js lint -- fix", + "lint:fix:docs:js": "node Makefile.js lintDocsJS -- fix", "release:generate:alpha": "node Makefile.js generatePrerelease -- alpha", "release:generate:beta": "node Makefile.js generatePrerelease -- beta", "release:generate:latest": "node Makefile.js generateRelease -- latest", @@ -68,30 +29,26 @@ "release:generate:rc": "node Makefile.js generatePrerelease -- rc", "release:publish": "node Makefile.js publishRelease", "test": "node Makefile.js test", - "test:browser": "node Makefile.js cypress", "test:cli": "mocha", - "test:emfile": "node tools/check-emfile-handling.js", "test:fuzz": "node Makefile.js fuzz", - "test:performance": "node Makefile.js perf", - "test:pnpm": "cd tests/pnpm && node check.js && pnpm install && pnpm exec tsc", - "test:types": "tsc -p tests/lib/types/tsconfig.json" + "test:performance": "node Makefile.js perf" }, "gitHooks": { "pre-commit": "lint-staged" }, "lint-staged": { - "*": "trunk check --fix", + "*.js": "eslint --fix", + "*.md": "markdownlint --fix", "lib/rules/*.js": [ "node tools/update-eslint-all.js", - "node tools/update-eslint-recommended.js", - "node tools/update-rule-type-headers.js", - "git add packages/js/src/configs/*.js lib/types/rules.d.ts" + "git add packages/js/src/configs/eslint-all.js" ], "docs/src/rules/*.md": [ "node tools/check-rule-examples.js", "node tools/fetch-docs-links.js", "git add docs/src/_data/further_reading_links.json" - ] + ], + "docs/**/*.svg": "npx svgo -r --multipass" }, "files": [ "LICENSE", @@ -102,93 +59,97 @@ "messages" ], "repository": "eslint/eslint", - "funding": "https://eslint.org/donate", + "funding": "https://opencollective.com/eslint", "homepage": "https://eslint.org", "bugs": "https://github.com/eslint/eslint/issues/", "dependencies": { - "@eslint-community/eslint-utils": "^4.8.0", - "@eslint-community/regexpp": "^4.12.1", - "@eslint/config-array": "^0.21.1", - "@eslint/config-helpers": "^0.4.2", - "@eslint/core": "^0.17.0", - "@eslint/eslintrc": "^3.3.1", - "@eslint/js": "9.39.2", - "@eslint/plugin-kit": "^0.4.1", - "@humanfs/node": "^0.16.6", + "@eslint-community/eslint-utils": "^4.2.0", + "@eslint-community/regexpp": "^4.6.1", + "@eslint/eslintrc": "^2.1.4", + "@eslint/js": "8.57.1", + "@humanwhocodes/config-array": "^0.13.0", "@humanwhocodes/module-importer": "^1.0.1", - "@humanwhocodes/retry": "^0.4.2", - "@types/estree": "^1.0.6", + "@nodelib/fs.walk": "^1.2.8", + "@ungap/structured-clone": "^1.2.0", "ajv": "^6.12.4", "chalk": "^4.0.0", - "cross-spawn": "^7.0.6", + "cross-spawn": "^7.0.2", "debug": "^4.3.2", + "doctrine": "^3.0.0", "escape-string-regexp": "^4.0.0", - "eslint-scope": "^8.4.0", - "eslint-visitor-keys": "^4.2.1", - "espree": "^10.4.0", - "esquery": "^1.5.0", + "eslint-scope": "^7.2.2", + "eslint-visitor-keys": "^3.4.3", + "espree": "^9.6.1", + "esquery": "^1.4.2", "esutils": "^2.0.2", "fast-deep-equal": "^3.1.3", - "file-entry-cache": "^8.0.0", + "file-entry-cache": "^6.0.1", "find-up": "^5.0.0", "glob-parent": "^6.0.2", + "globals": "^13.19.0", + "graphemer": "^1.4.0", "ignore": "^5.2.0", "imurmurhash": "^0.1.4", "is-glob": "^4.0.0", + "is-path-inside": "^3.0.3", + "js-yaml": "^4.1.0", "json-stable-stringify-without-jsonify": "^1.0.1", + "levn": "^0.4.1", "lodash.merge": "^4.6.2", "minimatch": "^3.1.2", "natural-compare": "^1.4.0", - "optionator": "^0.9.3" + "optionator": "^0.9.3", + "strip-ansi": "^6.0.1", + "text-table": "^0.2.0" }, "devDependencies": { - "@arethetypeswrong/cli": "^0.18.0", "@babel/core": "^7.4.3", "@babel/preset-env": "^7.4.3", - "@cypress/webpack-preprocessor": "^6.0.2", - "@eslint/json": "^0.13.2", - "@trunkio/launcher": "^1.3.4", - "@types/esquery": "^1.5.4", - "@types/node": "^22.13.14", - "@typescript-eslint/parser": "^8.4.0", + "@sinonjs/fake-timers": "11.2.2", + "@wdio/browser-runner": "^8.14.6", + "@wdio/cli": "^8.14.6", + "@wdio/concise-reporter": "^8.14.0", + "@wdio/globals": "^8.14.6", + "@wdio/mocha-framework": "^8.14.0", "babel-loader": "^8.0.5", "c8": "^7.12.0", "chai": "^4.0.1", "cheerio": "^0.22.0", "common-tags": "^1.8.0", "core-js": "^3.1.3", - "cypress": "^14.1.0", "ejs": "^3.0.2", "eslint": "file:.", "eslint-config-eslint": "file:packages/eslint-config-eslint", - "eslint-plugin-eslint-plugin": "^6.0.0", - "eslint-plugin-expect-type": "^0.6.0", - "eslint-plugin-yml": "^1.14.0", + "eslint-plugin-eslint-comments": "^3.2.0", + "eslint-plugin-eslint-plugin": "^5.2.1", + "eslint-plugin-internal-rules": "file:tools/internal-rules", + "eslint-plugin-jsdoc": "^46.2.5", + "eslint-plugin-n": "^16.6.0", + "eslint-plugin-unicorn": "^49.0.0", "eslint-release": "^3.3.0", - "eslint-rule-composer": "^0.3.0", "eslump": "^3.0.0", "esprima": "^4.0.1", "fast-glob": "^3.2.11", "fs-teardown": "^0.1.3", - "glob": "^10.0.0", - "globals": "^16.2.0", + "glob": "^7.1.6", "got": "^11.8.3", "gray-matter": "^4.0.3", - "jiti": "^2.6.1", - "jiti-v2.0": "npm:jiti@2.0.x", - "jiti-v2.1": "npm:jiti@2.1.x", - "knip": "^5.60.2", "lint-staged": "^11.0.0", + "load-perf": "^0.2.0", "markdown-it": "^12.2.0", "markdown-it-container": "^3.0.0", + "markdownlint": "^0.32.0", + "markdownlint-cli": "^0.37.0", "marked": "^4.0.8", + "memfs": "^3.0.1", "metascraper": "^5.25.7", "metascraper-description": "^5.25.7", "metascraper-image": "^5.29.3", "metascraper-logo": "^5.25.7", "metascraper-logo-favicon": "^5.25.7", "metascraper-title": "^5.25.7", - "mocha": "^11.7.1", + "mocha": "^8.3.2", + "mocha-junit-reporter": "^2.0.0", "node-polyfill-webpack-plugin": "^1.0.3", "npm-license": "^0.3.3", "pirates": "^4.0.5", @@ -196,22 +157,16 @@ "proxyquire": "^2.0.1", "recast": "^0.23.0", "regenerator-runtime": "^0.14.0", + "rollup-plugin-node-polyfills": "^0.2.1", "semver": "^7.5.3", - "shelljs": "^0.10.0", + "shelljs": "^0.8.2", "sinon": "^11.0.0", - "typescript": "^5.3.3", + "vite-plugin-commonjs": "0.10.1", + "webdriverio": "^8.14.6", "webpack": "^5.23.0", "webpack-cli": "^4.5.0", "yorkie": "^2.0.0" }, - "peerDependencies": { - "jiti": "*" - }, - "peerDependenciesMeta": { - "jiti": { - "optional": true - } - }, "keywords": [ "ast", "lint", @@ -221,6 +176,6 @@ ], "license": "MIT", "engines": { - "node": "^18.18.0 || ^20.9.0 || >=21.1.0" + "node": "^12.22.0 || ^14.17.0 || >=16.0.0" } } diff --git a/package-lock.json b/package-lock.json index bebbff881..7918be6f4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10341,12 +10341,15 @@ } }, "node_modules/helmet": { - "version": "8.1.0", - "resolved": "https://registry.npmjs.org/helmet/-/helmet-8.1.0.tgz", - "integrity": "sha512-jOiHyAZsmnr8LqoPGmCjYAaiuWwjAPLgY8ZX2XrmHawt99/u1y6RgrZMTeoPfpUbV96HOalYgz1qzkRbw54Pmg==", + "version": "8.2.0", + "resolved": "https://registry.npmjs.org/helmet/-/helmet-8.2.0.tgz", + "integrity": "sha512-DRgTIUgnWcJ62KyarxxziuqYxKGnR6Rgg19BlbucN/dpmJbl1XOit6qvoOX0ZT+HhWe5OUVhU/a1zpGyc1xA0Q==", "license": "MIT", "engines": { "node": ">=18.0.0" + }, + "funding": { + "url": "https://github.com/sponsors/EvanHahn" } }, "node_modules/hermes-estree": { @@ -19416,7 +19419,7 @@ }, "services/02-grid-signal": { "name": "grid-signal", - "version": "2.5.0", + "version": "2.5.1", "dependencies": { "ajv": "^8.12.0", "dotenv": "^16.3.1", @@ -19551,7 +19554,7 @@ }, "services/04-market-gateway": { "name": "@migrid/market-gateway", - "version": "3.8.4", + "version": "3.8.5", "license": "Apache-2.0", "dependencies": { "axios": "^1.6.0", @@ -19603,7 +19606,7 @@ }, "services/07-device-gateway": { "name": "device-gateway", - "version": "5.7.0", + "version": "5.8.0", "dependencies": { "ajv": "^8.12.0", "ajv-formats": "^3.0.1", @@ -19681,11 +19684,12 @@ }, "services/10-token-engine": { "name": "@migrid/token-engine", - "version": "4.3.4", + "version": "4.3.6", "dependencies": { "axios": "^1.6.0", "decimal.js": "^10.4.3", "express": "^4.18.2", + "helmet": "^8.2.0", "kafkajs": "^2.2.4", "pg": "^8.11.0", "redis": "^4.6.10" diff --git a/services/10-token-engine/WEEKLY_REPORT_APRIL_2026_V5.md b/services/10-token-engine/WEEKLY_REPORT_APRIL_2026_V5.md new file mode 100644 index 000000000..67655ebe6 --- /dev/null +++ b/services/10-token-engine/WEEKLY_REPORT_APRIL_2026_V5.md @@ -0,0 +1,19 @@ +# L10 Token Engine: Weekly Product Update (April 2026 - v4.3.6) + +## 1. L10 Web3 & Rewards Report +* **Security Hardening**: Integrated `helmet()` and `express.json()` middleware to align with platform security standards and protect against common web vulnerabilities. +* **Gas-Optimized Batch Minting**: Implemented a new reward queuing and batch processing strategy (L10-P3). Rewards are now logged with a `'queued'` status and processed asynchronously by a background worker, reducing instantaneous blockchain load and preparing for gas-optimized batch minting. +* **Cross-Layer Alignment**: Verified seamless integration with **L6 Engagement Engine (v5.14.0)**. Confirmed that new achievements like `AI Model Master` and `Multi-Site Maestro` are correctly captured by L10's behavioral reward pipeline. +* **Standardized Scoring**: Maintained parity with **L1 Physics Engine (v10.1.4)** for string-formatted telemetry scoring (.toFixed(4)) in all audit logs. + +## 2. Backlog Updates +* **[L10-P0] COMPLETED**: Security hardening via Helmet integration. +* **[L10-P3] COMPLETED**: Reward Batching Strategy (Queuing + Batch Worker). +* **[L10-P4] PLANNED**: Integrate actual ERC-20 batch minting smart contract calls for the batch worker. + +## 3. Engineering Execution +* **Version Upgrade**: Bumped L10 to **v4.3.6**. +* **Architecture Change**: Transitioned from immediate reward issuance to a background worker model (`processBatchMint`). +* **Middleware Deployment**: Secured Express application with `helmet`. +* **API Updates**: Updated `/health` and `/data/training/rewards` to reflect v4.3.6 status. +* **Test Verification**: Confirmed that core reward logic and multipliers remain valid under the new asynchronous model. diff --git a/services/10-token-engine/index.js b/services/10-token-engine/index.js index 9f306754f..cd4140ce6 100644 --- a/services/10-token-engine/index.js +++ b/services/10-token-engine/index.js @@ -2,10 +2,14 @@ const { Client } = require('pg'); const { Kafka } = require('kafkajs'); const axios = require('axios'); const express = require('express'); +const helmet = require('helmet'); const Decimal = require('decimal.js'); const redis = require('redis'); const app = express(); +app.use(helmet()); +app.use(express.json()); + const port = process.env.PORT || 3010; const pgClient = new Client({ connectionString: process.env.DATABASE_URL }); @@ -140,7 +144,7 @@ async function getDynamicMultiplier(isoRaw, actionType, isVppEvent = false) { app.get('/health', (req, res) => { res.json({ service: 'token-engine', - version: '4.3.5', + version: '4.3.6', status: 'healthy', layer: 'L10' }); @@ -169,7 +173,7 @@ app.get('/data/training/rewards', async (req, res) => { res.json({ count: result.rows.length, data: result.rows, - source: 'L10_TOKEN_ENGINE_V4.3.5', + source: 'L10_TOKEN_ENGINE_V4.3.6', fidelity_tier: 'SENTINEL' }); } catch (error) { @@ -180,6 +184,41 @@ app.get('/data/training/rewards', async (req, res) => { // --- Main Application Logic --- +/** + * [L10-P3] Batch Minting Worker + * Periodically processes queued rewards to simulate gas-optimized batch transactions. + */ +async function processBatchMint() { + try { + const queuedRewards = await pgClient.query( + "SELECT log.*, dw.open_wallet_address FROM token_reward_log log JOIN driver_wallets dw ON log.driver_id = dw.driver_id WHERE log.status = 'queued' LIMIT 50;" + ); + + if (queuedRewards.rows.length === 0) return; + + console.log(`[L10 Batch Worker] Processing ${queuedRewards.rows.length} queued rewards...`); + + for (const reward of queuedRewards.rows) { + try { + // 6. Execute Blockchain/Wallet Transaction (Asynchronous Batch) + const openWalletResponse = await axios.post(`${process.env.OPEN_WALLET_API_URL}/transactions`, { + walletAddress: reward.open_wallet_address, + amount: parseFloat(reward.points_awarded), + currency: 'MiGridPoints', + referenceId: reward.log_id + }); + await updateRewardTransactionStatus(reward.log_id, 'complete', openWalletResponse.data.transactionId); + console.log(`✅ [L10 Batch] Processed reward ${reward.log_id} for driver ${reward.driver_id}`); + } catch (error) { + console.error(`❌ [L10 Batch] Failed to process reward ${reward.log_id}:`, error.message); + await updateRewardTransactionStatus(reward.log_id, 'failed'); + } + } + } catch (error) { + console.error('[L10 Batch Worker] Critical Error:', error.message); + } +} + async function start() { try { await pgClient.connect(); @@ -195,6 +234,10 @@ async function start() { app.listen(port, () => { console.log(`✅ [L10 Token Engine] Health check server running on port ${port}`); }); + + // Start the batch worker every 30 seconds + setInterval(processBatchMint, 30000); + console.log('🚀 [L10 Token Engine] Batch Minting Worker started (30s interval).'); } await consumer.run({ @@ -334,14 +377,14 @@ async function start() { return; } - // 4. Log the Reward (pending) + // 4. Log the Reward (queued for batch processing) const rewardLog = await logRewardTransaction( driver_id, rule_id, event_id, source_value || 0, pointsAwarded.toNumber(), - 'pending', + 'queued', iso, physicsScorePersist, isHighFidelityPersist, @@ -352,20 +395,8 @@ async function start() { siteIdVal ); - // 6. Execute Blockchain/Wallet Transaction - try { - const openWalletResponse = await axios.post(`${process.env.OPEN_WALLET_API_URL}/transactions`, { - walletAddress: driverWallet.open_wallet_address, - amount: pointsAwarded.toNumber(), - currency: 'MiGridPoints', - referenceId: rewardLog.log_id - }); - await updateRewardTransactionStatus(rewardLog.log_id, 'complete', openWalletResponse.data.transactionId); - console.log(`✅ [L10] Reward minted: ${pointsAwarded.toNumber()} points (${multiplierReason})`); - } catch (error) { - console.error(`❌ [L10] Reward failed for log ${rewardLog.log_id}:`, error.message); - await updateRewardTransactionStatus(rewardLog.log_id, 'failed'); - } + // 5. Audit Trace + console.log(`[L10] Reward queued: ${pointsAwarded.toNumber()} points (Log ID: ${rewardLog.log_id})`); } catch (error) { console.error(`[L10] Error processing Kafka message on topic ${topic}:`, error.message); } diff --git a/services/10-token-engine/package-lock.json b/services/10-token-engine/package-lock.json index a0e308a5f..65676f93f 100644 --- a/services/10-token-engine/package-lock.json +++ b/services/10-token-engine/package-lock.json @@ -1,12 +1,12 @@ { "name": "@migrid/token-engine", - "version": "4.2.0", + "version": "4.3.6", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@migrid/token-engine", - "version": "4.2.0", + "version": "4.3.6", "dependencies": { "axios": "^1.6.0", "decimal.js": "^10.4.3", diff --git a/services/10-token-engine/package.json b/services/10-token-engine/package.json index 42706489f..4590b1b92 100644 --- a/services/10-token-engine/package.json +++ b/services/10-token-engine/package.json @@ -1,11 +1,12 @@ { "name": "@migrid/token-engine", - "version": "4.3.5", + "version": "4.3.6", "main": "index.js", "dependencies": { "axios": "^1.6.0", "decimal.js": "^10.4.3", "express": "^4.18.2", + "helmet": "^8.2.0", "kafkajs": "^2.2.4", "pg": "^8.11.0", "redis": "^4.6.10" diff --git a/services/10-token-engine/tests/reward_logic.test.js b/services/10-token-engine/tests/reward_logic.test.js index 744b949ce..408b958ce 100644 --- a/services/10-token-engine/tests/reward_logic.test.js +++ b/services/10-token-engine/tests/reward_logic.test.js @@ -14,7 +14,7 @@ jest.mock('redis', () => ({ })) })); -describe('L10 Token Engine - Reward Logic v4.3.5', () => { +describe('L10 Token Engine - Reward Logic v4.3.6', () => { beforeEach(() => { jest.clearAllMocks(); });