From b65470708604d91ef840bc0538a4a508117c7251 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Fri, 20 May 2022 08:33:53 -0700 Subject: [PATCH 1/8] feat: [4.7] support new extensions --- .cspell.json | 2 +- .lintstagedrc | 2 +- docs/linting/README.md | 12 +++-- docs/linting/TROUBLESHOOTING.md | 6 +-- package.json | 10 ++-- packages/ast-spec/package.json | 4 +- packages/eslint-plugin-internal/package.json | 4 +- packages/eslint-plugin-tslint/package.json | 4 +- packages/eslint-plugin/README.md | 2 - .../rules/explicit-function-return-type.md | 4 +- .../rules/explicit-member-accessibility.md | 4 +- .../rules/explicit-module-boundary-types.md | 4 +- packages/eslint-plugin/package.json | 4 +- .../src/configs/eslint-recommended.ts | 2 +- packages/eslint-plugin/src/util/misc.ts | 13 ++++- packages/experimental-utils/package.json | 4 +- packages/parser/README.md | 24 +++++---- packages/parser/package.json | 4 +- packages/parser/src/parser.ts | 9 +--- packages/scope-manager/package.json | 2 +- packages/type-utils/package.json | 4 +- packages/types/package.json | 4 +- packages/typescript-estree/README.md | 2 +- packages/typescript-estree/package.json | 4 +- .../create-program/createIsolatedProgram.ts | 9 ++-- .../create-program/createProjectProgram.ts | 12 ++++- .../src/create-program/createSourceFile.ts | 4 +- .../src/create-program/getScriptKind.ts | 49 +++++++++++++++++++ .../src/create-program/shared.ts | 44 +++++------------ packages/typescript-estree/src/index.ts | 1 + .../typescript-estree/src/parser-options.ts | 2 +- packages/typescript-estree/src/parser.ts | 6 ++- packages/utils/package.json | 4 +- packages/visitor-keys/package.json | 4 +- packages/website-eslint/package.json | 2 +- packages/website/package.json | 2 +- 36 files changed, 162 insertions(+), 111 deletions(-) create mode 100644 packages/typescript-estree/src/create-program/getScriptKind.ts diff --git a/.cspell.json b/.cspell.json index 4a8c0ad226be..09466a9d300a 100644 --- a/.cspell.json +++ b/.cspell.json @@ -114,7 +114,7 @@ ], "overrides": [ { - "filename": "**/*.{ts,js,tsx,jsx}", + "filename": "**/*.{ts,mts,cts,js,cjs,mjs,tsx,jsx}", "ignoreRegExpList": ["/@[a-z]+/", "/#(end)?region/"], "includeRegExpList": [ "/\\/\\*[\\s\\S]*?\\*\\/|([^\\\\:]|^)\\/\\/.*$/", diff --git a/.lintstagedrc b/.lintstagedrc index e4d508f67844..691f6e0eb0e5 100644 --- a/.lintstagedrc +++ b/.lintstagedrc @@ -1,5 +1,5 @@ { - "*.{ts,js,json,md}": [ + "*.{ts,mts,cts,tsx,js,mjs,cjs,jsx,json,md,css}": [ "prettier --write" ] } diff --git a/docs/linting/README.md b/docs/linting/README.md index e42c436bd1d1..dbf08d1e7808 100644 --- a/docs/linting/README.md +++ b/docs/linting/README.md @@ -85,27 +85,27 @@ With that configured, open a terminal to the root of your project, and run the f ```bash -npx eslint . --ext .js,.jsx,.ts,.tsx +npx eslint . ``` ```bash -yarn eslint . --ext .js,.jsx,.ts,.tsx +yarn eslint . ``` -That's it - ESLint will lint all `.js`, `.jsx`, `.ts`, and `.tsx` files within the current folder, and will output the results to your terminal. +That's it - ESLint will lint all TypeScript compatible files within the current folder, and will output the results to your terminal. You are also recommended to add an npm script in your package.json, so you don't have to repeat the same command every time you run ESLint. ```json title="package.json" { "scripts": { - "lint": "eslint . --ext .js,.jsx,.ts,.tsx" + "lint": "eslint ." } } ``` @@ -116,6 +116,10 @@ This way, you can invoke the `lint` script directly: npm run lint ``` +:::note +If you use non-standard file extensions, you will need to explicitly tell ESLint to lint those extensions using the [`--ext` flag](https://eslint.org/docs/user-guide/command-line-interface#--ext) +::: + You can also get results in realtime inside most IDEs via a plugin - search your IDE's extension store. ## Next Steps diff --git a/docs/linting/TROUBLESHOOTING.md b/docs/linting/TROUBLESHOOTING.md index 8f8ae10565de..f44af3713f7e 100644 --- a/docs/linting/TROUBLESHOOTING.md +++ b/docs/linting/TROUBLESHOOTING.md @@ -119,12 +119,12 @@ As of our v4.0.0 release, this also applies to types. If you use global types from a 3rd party package (i.e. anything from an `@types` package), then you will have to configure ESLint appropriately to define these global types. For example; the `JSX` namespace from `@types/react` is a global 3rd party type that you must define in your ESLint config. -Note, that for a mixed project including JavaScript and TypeScript, the `no-undef` rule (like any role) can be turned off for TypeScript files alone by adding an `overrides` section to .eslintrc.json: +Note, that for a mixed project including JavaScript and TypeScript, the `no-undef` rule (like any role) can be turned off for TypeScript files alone by adding an `overrides` section to `.eslintrc.cjs`: ```json "overrides": [ { - "files": ["*.ts"], + "files": ["*.ts", "*.mts", "*.cts", "*.tsx"], "rules": { "no-undef": "off" } @@ -206,7 +206,7 @@ Instead of using this plugin, we recommend using prettier's `--list-different` f For example, our CI is setup to run the following command automatically, which blocks PRs that have not been formatted: ```bash npm2yarn -npm run prettier --list-different \"./**/*.{ts,js,json,md}\" +npm run prettier --list-different \"./**/*.{ts,mts,cts,tsx,js,mjs,cjs,jsx,json,md,css}\" ``` ### `eslint-plugin-import` diff --git a/package.json b/package.json index 4a5754793137..64850d72e6a0 100644 --- a/package.json +++ b/package.json @@ -26,18 +26,18 @@ "check-clean-workspace-after-install": "git diff --quiet --exit-code", "check-configs": "nx run-many --target=check-configs --all --parallel", "check-docs": "nx run-many --target=check-docs --all --parallel", - "check-format": "prettier --list-different \"./**/*.{md,mdx,ts,js,tsx,jsx}\"", - "check-spelling": "cspell --config=.cspell.json \"**/*.{md,mdx,ts,js,tsx,jsx}\"", + "check-format": "prettier --list-different \"./**/*.{md,mdx,ts,mts,cts,js,cjs,mjs,tsx,jsx}\"", + "check-spelling": "cspell --config=.cspell.json \"**/*.{md,mdx,ts,mts,cts,js,cjs,mjs,tsx,jsx}\"", "clean": "lerna clean && lerna run clean", "cz": "git-cz", - "format": "prettier --write \"./**/*.{ts,tsx,js,jsx,json,md,css}\"", + "format": "prettier --write \"./**/*.{ts,mts,cts,tsx,js,mjs,cjs,jsx,json,md,css}\"", "generate-contributors": "yarn ts-node --transpile-only ./tools/generate-contributors.ts && yarn all-contributors generate", "generate-sponsors": "yarn ts-node --transpile-only ./tools/generate-sponsors.ts", "generate-website-dts": "yarn ts-node --transpile-only ./tools/generate-website-dts.ts", - "lint-fix": "eslint . --ext .js,.jsx,.ts,.tsx --fix", + "lint-fix": "eslint . --fix", "lint-markdown-fix": "yarn lint-markdown --fix", "lint-markdown": "markdownlint \"**/*.md\" --config=.markdownlint.json --ignore-path=.markdownlintignore", - "lint": "cross-env NODE_OPTIONS=\"--max-old-space-size=16384\" eslint . --ext .js,.jsx,.ts,.tsx", + "lint": "cross-env NODE_OPTIONS=\"--max-old-space-size=16384\" eslint .", "postinstall": "yarn husky install && yarn build", "pre-commit": "yarn lint-staged", "pre-push": "yarn check-format", diff --git a/packages/ast-spec/package.json b/packages/ast-spec/package.json index 1b605ffd02e8..4b41450a6c92 100644 --- a/packages/ast-spec/package.json +++ b/packages/ast-spec/package.json @@ -33,8 +33,8 @@ "clean": "tsc -b tsconfig.build.json --clean", "postclean": "rimraf dist && rimraf _ts3.4 && rimraf .rollup.cache && rimraf coverage", "clean-fixtures": "rimraf -g \"./src/**/fixtures/**/snapshots\"", - "format": "prettier --write \"./**/*.{ts,js,json,md}\" --ignore-path ../../.prettierignore", - "lint": "eslint . --ext .js,.ts --ignore-path='../../.eslintignore'", + "format": "prettier --write \"./**/*.{ts,mts,cts,tsx,js,mjs,cjs,jsx,json,md,css}\" --ignore-path ../../.prettierignore", + "lint": "eslint . --ignore-path='../../.eslintignore'", "test": "jest", "typecheck": "tsc -p tsconfig.json --noEmit" }, diff --git a/packages/eslint-plugin-internal/package.json b/packages/eslint-plugin-internal/package.json index 1329c7b60cdc..870a57deabdc 100644 --- a/packages/eslint-plugin-internal/package.json +++ b/packages/eslint-plugin-internal/package.json @@ -7,8 +7,8 @@ "build": "tsc -b tsconfig.build.json", "clean": "tsc -b tsconfig.build.json --clean", "postclean": "rimraf dist && rimraf coverage", - "format": "prettier --write \"./**/*.{ts,js,json,md}\" --ignore-path ../../.prettierignore", - "lint": "eslint . --ext .js,.ts --ignore-path='../../.eslintignore'", + "format": "prettier --write \"./**/*.{ts,mts,cts,tsx,js,mjs,cjs,jsx,json,md,css}\" --ignore-path ../../.prettierignore", + "lint": "eslint . --ignore-path='../../.eslintignore'", "test": "jest --coverage", "typecheck": "tsc -p tsconfig.json --noEmit" }, diff --git a/packages/eslint-plugin-tslint/package.json b/packages/eslint-plugin-tslint/package.json index 77ebaade61ee..26809e3dc0be 100644 --- a/packages/eslint-plugin-tslint/package.json +++ b/packages/eslint-plugin-tslint/package.json @@ -32,8 +32,8 @@ "build": "tsc -b tsconfig.build.json", "clean": "tsc -b tsconfig.build.json --clean", "postclean": "rimraf dist && rimraf coverage", - "format": "prettier --write \"./**/*.{ts,js,json,md}\" --ignore-path ../../.prettierignore", - "lint": "eslint . --ext .js,.ts --ignore-path='../../.eslintignore'", + "format": "prettier --write \"./**/*.{ts,mts,cts,tsx,js,mjs,cjs,jsx,json,md,css}\" --ignore-path ../../.prettierignore", + "lint": "eslint . --ignore-path='../../.eslintignore'", "test": "jest --coverage", "typecheck": "tsc -p tsconfig.json --noEmit" }, diff --git a/packages/eslint-plugin/README.md b/packages/eslint-plugin/README.md index 999924414dde..c27f30286380 100644 --- a/packages/eslint-plugin/README.md +++ b/packages/eslint-plugin/README.md @@ -59,8 +59,6 @@ You can also enable all the recommended rules for our plugin. Add `plugin:@types } ``` -**Note: Make sure to use `eslint --ext .js,.ts` since by [default](https://eslint.org/docs/user-guide/command-line-interface#--ext) `eslint` will only search for `.js` files.** - ### Recommended Configs You can also use [`eslint:recommended`](https://eslint.org/docs/rules/) (the set of rules which are recommended for all projects by the ESLint Team) with this plugin: diff --git a/packages/eslint-plugin/docs/rules/explicit-function-return-type.md b/packages/eslint-plugin/docs/rules/explicit-function-return-type.md index 210871ab3478..8faf8aab503a 100644 --- a/packages/eslint-plugin/docs/rules/explicit-function-return-type.md +++ b/packages/eslint-plugin/docs/rules/explicit-function-return-type.md @@ -95,7 +95,7 @@ const defaults = { ### Configuring in a mixed JS/TS codebase -If you are working on a codebase within which you lint non-TypeScript code (i.e. `.js`/`.jsx`), you should ensure that you should use [ESLint `overrides`](https://eslint.org/docs/user-guide/configuring#disabling-rules-only-for-a-group-of-files) to only enable the rule on `.ts`/`.tsx` files. If you don't, then you will get unfixable lint errors reported within `.js`/`.jsx` files. +If you are working on a codebase within which you lint non-TypeScript code (i.e. `.js`/`.mjs`/`.cjs`/`.jsx`), you should ensure that you should use [ESLint `overrides`](https://eslint.org/docs/user-guide/configuring#disabling-rules-only-for-a-group-of-files) to only enable the rule on `.ts`/`.mts`/`.cts`/`.tsx` files. If you don't, then you will get unfixable lint errors reported within `.js`/`.mjs`/`.cjs`/`.jsx` files. ```jsonc { @@ -106,7 +106,7 @@ If you are working on a codebase within which you lint non-TypeScript code (i.e. "overrides": [ { // enable the rule specifically for TypeScript files - "files": ["*.ts", "*.tsx"], + "files": ["*.ts", "*.mts", "*.cts", "*.tsx"], "rules": { "@typescript-eslint/explicit-function-return-type": ["error"] } diff --git a/packages/eslint-plugin/docs/rules/explicit-member-accessibility.md b/packages/eslint-plugin/docs/rules/explicit-member-accessibility.md index 4d14797d02c1..7570e6e4667a 100644 --- a/packages/eslint-plugin/docs/rules/explicit-member-accessibility.md +++ b/packages/eslint-plugin/docs/rules/explicit-member-accessibility.md @@ -39,7 +39,7 @@ const defaultOptions: Options = { ### Configuring in a mixed JS/TS codebase -If you are working on a codebase within which you lint non-TypeScript code (i.e. `.js`/`.jsx`), you should ensure that you should use [ESLint `overrides`](https://eslint.org/docs/user-guide/configuring#disabling-rules-only-for-a-group-of-files) to only enable the rule on `.ts`/`.tsx` files. If you don't, then you will get unfixable lint errors reported within `.js`/`.jsx` files. +If you are working on a codebase within which you lint non-TypeScript code (i.e. `.js`/`.mjs`/`.cjs`/`.jsx`), you should ensure that you should use [ESLint `overrides`](https://eslint.org/docs/user-guide/configuring#disabling-rules-only-for-a-group-of-files) to only enable the rule on `.ts`/`.mts`/`.cts`/`.tsx` files. If you don't, then you will get unfixable lint errors reported within `.js`/`.mjs`/`.cjs`/`.jsx` files. ```jsonc { @@ -50,7 +50,7 @@ If you are working on a codebase within which you lint non-TypeScript code (i.e. "overrides": [ { // enable the rule specifically for TypeScript files - "files": ["*.ts", "*.tsx"], + "files": ["*.ts", "*.mts", "*.cts", "*.tsx"], "rules": { "@typescript-eslint/explicit-member-accessibility": ["error"] } diff --git a/packages/eslint-plugin/docs/rules/explicit-module-boundary-types.md b/packages/eslint-plugin/docs/rules/explicit-module-boundary-types.md index d968504be074..26809892e678 100644 --- a/packages/eslint-plugin/docs/rules/explicit-module-boundary-types.md +++ b/packages/eslint-plugin/docs/rules/explicit-module-boundary-types.md @@ -110,7 +110,7 @@ const defaults = { ### Configuring in a mixed JS/TS codebase -If you are working on a codebase within which you lint non-TypeScript code (i.e. `.js`/`.jsx`), you should ensure that you should use [ESLint `overrides`](https://eslint.org/docs/user-guide/configuring#disabling-rules-only-for-a-group-of-files) to only enable the rule on `.ts`/`.tsx` files. If you don't, then you will get unfixable lint errors reported within `.js`/`.jsx` files. +If you are working on a codebase within which you lint non-TypeScript code (i.e. `.js`/`.mjs`/`.cjs`/`.jsx`), you should ensure that you should use [ESLint `overrides`](https://eslint.org/docs/user-guide/configuring#disabling-rules-only-for-a-group-of-files) to only enable the rule on `.ts`/`.mts`/`.cts`/`.tsx` files. If you don't, then you will get unfixable lint errors reported within `.js`/`.mjs`/`.cjs`/`.jsx` files. ```jsonc { @@ -121,7 +121,7 @@ If you are working on a codebase within which you lint non-TypeScript code (i.e. "overrides": [ { // enable the rule specifically for TypeScript files - "files": ["*.ts", "*.tsx"], + "files": ["*.ts", "*.mts", "*.cts", "*.tsx"], "rules": { "@typescript-eslint/explicit-module-boundary-types": ["error"] } diff --git a/packages/eslint-plugin/package.json b/packages/eslint-plugin/package.json index 00acf3790485..8f67e54fc915 100644 --- a/packages/eslint-plugin/package.json +++ b/packages/eslint-plugin/package.json @@ -36,10 +36,10 @@ "check-configs": "jest tests/configs.test.ts --runTestsByPath --silent --runInBand", "clean": "tsc -b tsconfig.build.json --clean", "postclean": "rimraf dist && rimraf coverage", - "format": "prettier --write \"./**/*.{ts,js,json,md}\" --ignore-path ../../.prettierignore", + "format": "prettier --write \"./**/*.{ts,mts,cts,tsx,js,mjs,cjs,jsx,json,md,css}\" --ignore-path ../../.prettierignore", "generate:configs": "../../node_modules/.bin/ts-node --files --transpile-only tools/generate-configs.ts", "generate:rules-lists": "../../node_modules/.bin/ts-node --files --transpile-only tools/generate-rules-lists.ts", - "lint": "eslint . --ext .js,.ts --ignore-path ../../.eslintignore", + "lint": "eslint . --ignore-path ../../.eslintignore", "test": "jest --coverage", "typecheck": "tsc -p tsconfig.json --noEmit" }, diff --git a/packages/eslint-plugin/src/configs/eslint-recommended.ts b/packages/eslint-plugin/src/configs/eslint-recommended.ts index 125c093b2bc2..71443e1f52ef 100644 --- a/packages/eslint-plugin/src/configs/eslint-recommended.ts +++ b/packages/eslint-plugin/src/configs/eslint-recommended.ts @@ -6,7 +6,7 @@ export = { overrides: [ { - files: ['*.ts', '*.tsx'], + files: ['*.ts', '*.tsx', '*.mts', '*.cts'], rules: { 'constructor-super': 'off', // ts(2335) & ts(2377) 'getter-return': 'off', // ts(2378) diff --git a/packages/eslint-plugin/src/util/misc.ts b/packages/eslint-plugin/src/util/misc.ts index 8479feb728dd..59a7ccd9fc46 100644 --- a/packages/eslint-plugin/src/util/misc.ts +++ b/packages/eslint-plugin/src/util/misc.ts @@ -4,12 +4,23 @@ import { AST_NODE_TYPES, TSESLint, TSESTree } from '@typescript-eslint/utils'; import { requiresQuoting } from '@typescript-eslint/type-utils'; +import * as ts from 'typescript'; +const DEFINITION_EXTENSIONS = [ + ts.Extension.Dts, + ts.Extension.Dcts, + ts.Extension.Dmts, +] as const; /** * Check if the context file name is *.d.ts or *.d.tsx */ function isDefinitionFile(fileName: string): boolean { - return /\.d\.tsx?$/i.test(fileName || ''); + for (const definitionExt of DEFINITION_EXTENSIONS) { + if (fileName.endsWith(definitionExt)) { + return true; + } + } + return false; } /** diff --git a/packages/experimental-utils/package.json b/packages/experimental-utils/package.json index b8e1d9ec230f..885b133accd0 100644 --- a/packages/experimental-utils/package.json +++ b/packages/experimental-utils/package.json @@ -33,8 +33,8 @@ "postbuild": "downlevel-dts dist _ts3.4/dist", "clean": "tsc -b tsconfig.build.json --clean", "postclean": "rimraf dist && rimraf _ts3.4 && rimraf coverage", - "format": "prettier --write \"./**/*.{ts,js,json,md}\" --ignore-path ../../.prettierignore", - "lint": "eslint . --ext .js,.ts --ignore-path='../../.eslintignore'", + "format": "prettier --write \"./**/*.{ts,mts,cts,tsx,js,mjs,cjs,jsx,json,md,css}\" --ignore-path ../../.prettierignore", + "lint": "eslint . --ignore-path='../../.eslintignore'", "typecheck": "tsc -p tsconfig.json --noEmit" }, "dependencies": { diff --git a/packages/parser/README.md b/packages/parser/README.md index 81df05194977..f026a5b4ae2e 100644 --- a/packages/parser/README.md +++ b/packages/parser/README.md @@ -78,16 +78,19 @@ Default `false`. Enable parsing JSX when `true`. More details can be found [here](https://www.typescriptlang.org/docs/handbook/jsx.html). -**NOTE:** this setting does not affect known file types (`.js`, `.jsx`, `.ts`, `.tsx`, `.json`) because the TypeScript compiler has its own internal handling for known file extensions. The exact behavior is as follows: +**NOTE:** this setting does not affect known file types (`.js`, `.mjs`, `.cjs`, `.jsx`, `.ts`, `.mts`, `.cts`, `.tsx`, `.json`) because the TypeScript compiler has its own internal handling for known file extensions. -- if `parserOptions.project` is _not_ provided: - - `.js`, `.jsx`, `.tsx` files are parsed as if this is true. - - `.ts` files are parsed as if this is false. - - unknown extensions (`.md`, `.vue`) will respect this setting. -- if `parserOptions.project` is provided (i.e. you are using rules with type information): - - `.js`, `.jsx`, `.tsx` files are parsed as if this is true. - - `.ts` files are parsed as if this is false. - - "unknown" extensions (`.md`, `.vue`) **are parsed as if this is false**. + + +The exact behavior is as follows: + +- `.js`, `.mjs`, `.cjs`, `.jsx`, `.tsx` files are always parsed as if this is `true`. +- `.ts`, `.mts`, `.cts` files are always parsed as if this is `false`. +- For "unknown" extensions (`.md`, `.vue`): + - If `parserOptions.project` is _not_ provided: + - The setting will be respected. + - If `parserOptions.project` is provided (i.e. you are using rules with type information): + - **always parsed as if this is `false`,** ### `parserOptions.ecmaFeatures.globalReturn` @@ -203,7 +206,8 @@ For example, by default it will ensure that a glob like `./**/tsconfig.json` wil Default `undefined`. This option allows you to provide one or more additional file extensions which should be considered in the TypeScript Program compilation. -The default extensions are `.ts`, `.tsx`, `.js`, and `.jsx`. Add extensions starting with `.`, followed by the file extension. E.g. for a `.vue` file use `"extraFileExtensions": [".vue"]`. +The default extensions are `['.js', '.mjs', '.cjs', '.jsx', '.ts', '.mts', '.cts', '.tsx']`. +Add extensions starting with `.`, followed by the file extension. E.g. for a `.vue` file use `"extraFileExtensions": [".vue"]`. ### `parserOptions.warnOnUnsupportedTypeScriptVersion` diff --git a/packages/parser/package.json b/packages/parser/package.json index 7b98529b475d..732235cf57ef 100644 --- a/packages/parser/package.json +++ b/packages/parser/package.json @@ -35,8 +35,8 @@ "postbuild": "downlevel-dts dist _ts3.4/dist", "clean": "tsc -b tsconfig.build.json --clean", "postclean": "rimraf dist && rimraf _ts3.4 && rimraf coverage", - "format": "prettier --write \"./**/*.{ts,js,json,md}\" --ignore-path ../../.prettierignore", - "lint": "eslint . --ext .js,.ts --ignore-path='../../.eslintignore'", + "format": "prettier --write \"./**/*.{ts,mts,cts,tsx,js,mjs,cjs,jsx,json,md,css}\" --ignore-path ../../.prettierignore", + "lint": "eslint . --ignore-path='../../.eslintignore'", "test": "jest --coverage", "typecheck": "tsc -p tsconfig.json --noEmit" }, diff --git a/packages/parser/src/parser.ts b/packages/parser/src/parser.ts index 365bff6648f8..7fcd45f96f75 100644 --- a/packages/parser/src/parser.ts +++ b/packages/parser/src/parser.ts @@ -36,7 +36,7 @@ function validateBoolean( return value; } -const LIB_FILENAME_REGEX = /lib\.(.+)\.d\.ts$/; +const LIB_FILENAME_REGEX = /lib\.(.+)\.d\.[cm]?ts$/; function getLib(compilerOptions: CompilerOptions): Lib[] { if (compilerOptions.lib) { return compilerOptions.lib.reduce((acc, lib) => { @@ -110,13 +110,6 @@ function parseForESLint( sourceType: options.sourceType, }; - if (typeof options.filePath === 'string') { - const tsx = options.filePath.endsWith('.tsx'); - if (tsx || options.filePath.endsWith('.ts')) { - parserOptions.jsx = tsx; - } - } - /** * Allow the user to suppress the warning from typescript-estree if they are using an unsupported * version of TypeScript diff --git a/packages/scope-manager/package.json b/packages/scope-manager/package.json index 90e900573251..322e9a429903 100644 --- a/packages/scope-manager/package.json +++ b/packages/scope-manager/package.json @@ -31,7 +31,7 @@ "build": "cd ../../ && nx build @typescript-eslint/scope-manager", "clean": "cd ../../ && nx clean @typescript-eslint/scope-manager", "clean-fixtures": "cd ../../ && nx clean-fixtures @typescript-eslint/scope-manager", - "format": "prettier --write \"./**/*.{ts,js,json,md}\" --ignore-path ../../.prettierignore", + "format": "prettier --write \"./**/*.{ts,mts,cts,tsx,js,mjs,cjs,jsx,json,md,css}\" --ignore-path ../../.prettierignore", "generate:lib": "cd ../../ && nx generate-lib @typescript-eslint/scope-manager", "lint": "cd ../../ && nx lint @typescript-eslint/scope-manager", "test": "cd ../../ && nx test @typescript-eslint/scope-manager --code-coverage", diff --git a/packages/type-utils/package.json b/packages/type-utils/package.json index c13af3b74489..92ebd51fc713 100644 --- a/packages/type-utils/package.json +++ b/packages/type-utils/package.json @@ -33,8 +33,8 @@ "postbuild": "downlevel-dts dist _ts3.4/dist", "clean": "tsc -b tsconfig.build.json --clean", "postclean": "rimraf dist && rimraf _ts3.4 && rimraf coverage", - "format": "prettier --write \"./**/*.{ts,js,json,md}\" --ignore-path ../../.prettierignore", - "lint": "eslint . --ext .js,.ts --ignore-path='../../.eslintignore'", + "format": "prettier --write \"./**/*.{ts,mts,cts,tsx,js,mjs,cjs,jsx,json,md,css}\" --ignore-path ../../.prettierignore", + "lint": "eslint . --ignore-path='../../.eslintignore'", "test": "jest --coverage", "typecheck": "tsc -p tsconfig.json --noEmit" }, diff --git a/packages/types/package.json b/packages/types/package.json index 2d25da527d1b..a84d9f830d4d 100644 --- a/packages/types/package.json +++ b/packages/types/package.json @@ -33,9 +33,9 @@ "postbuild": "downlevel-dts dist _ts3.4/dist", "clean": "tsc -b tsconfig.build.json --clean", "postclean": "rimraf dist && rimraf _ts3.4 && rimraf coverage", - "format": "prettier --write \"./**/*.{ts,js,json,md}\" --ignore-path ../../.prettierignore", + "format": "prettier --write \"./**/*.{ts,mts,cts,tsx,js,mjs,cjs,jsx,json,md,css}\" --ignore-path ../../.prettierignore", "generate:lib": "../../node_modules/.bin/ts-node --files --transpile-only ../scope-manager/tools/generate-lib.ts", - "lint": "eslint . --ext .js,.ts --ignore-path='../../.eslintignore'", + "lint": "eslint . --ignore-path='../../.eslintignore'", "typecheck": "tsc -p tsconfig.json --noEmit" }, "nx": { diff --git a/packages/typescript-estree/README.md b/packages/typescript-estree/README.md index f235f3dcf212..02946daa80df 100644 --- a/packages/typescript-estree/README.md +++ b/packages/typescript-estree/README.md @@ -69,7 +69,7 @@ interface ParseOptions { * Enable parsing of JSX. * For more details, see https://www.typescriptlang.org/docs/handbook/jsx.html * - * NOTE: this setting does not effect known file types (.js, .jsx, .ts, .tsx, .json) because the + * NOTE: this setting does not effect known file types (.js, .cjs, .mjs, .jsx, .ts, .mts, .cts, .tsx, .json) because the * TypeScript compiler has its own internal handling for known file extensions. * * For the exact behavior, see https://github.com/typescript-eslint/typescript-eslint/tree/main/packages/parser#parseroptionsecmafeaturesjsx diff --git a/packages/typescript-estree/package.json b/packages/typescript-estree/package.json index fd905d2fb2f0..b4f5aa25c717 100644 --- a/packages/typescript-estree/package.json +++ b/packages/typescript-estree/package.json @@ -35,8 +35,8 @@ "postbuild": "downlevel-dts dist _ts3.4/dist", "clean": "tsc -b tsconfig.build.json --clean", "postclean": "rimraf dist && rimraf _ts3.4 && rimraf coverage", - "format": "prettier --write \"./**/*.{ts,js,json,md}\" --ignore-path ../../.prettierignore", - "lint": "eslint . --ext .js,.ts --ignore-path='../../.eslintignore'", + "format": "prettier --write \"./**/*.{ts,mts,cts,tsx,js,mjs,cjs,jsx,json,md,css}\" --ignore-path ../../.prettierignore", + "lint": "eslint . --ignore-path='../../.eslintignore'", "test": "jest --coverage", "typecheck": "tsc -p tsconfig.json --noEmit" }, diff --git a/packages/typescript-estree/src/create-program/createIsolatedProgram.ts b/packages/typescript-estree/src/create-program/createIsolatedProgram.ts index 70169c66891b..c4392941bb67 100644 --- a/packages/typescript-estree/src/create-program/createIsolatedProgram.ts +++ b/packages/typescript-estree/src/create-program/createIsolatedProgram.ts @@ -1,11 +1,8 @@ import debug from 'debug'; import * as ts from 'typescript'; import { Extra } from '../parser-options'; -import { - ASTAndProgram, - createDefaultCompilerOptionsFromExtra, - getScriptKind, -} from './shared'; +import { ASTAndProgram, createDefaultCompilerOptionsFromExtra } from './shared'; +import { getScriptKind } from './getScriptKind'; const log = debug('typescript-eslint:typescript-estree:createIsolatedProgram'); @@ -47,7 +44,7 @@ function createIsolatedProgram(code: string, extra: Extra): ASTAndProgram { code, ts.ScriptTarget.Latest, /* setParentNodes */ true, - getScriptKind(extra, filename), + getScriptKind(extra.filePath, extra.jsx), ); }, readFile() { diff --git a/packages/typescript-estree/src/create-program/createProjectProgram.ts b/packages/typescript-estree/src/create-program/createProjectProgram.ts index 69903a5bcafc..4558cc36b55b 100644 --- a/packages/typescript-estree/src/create-program/createProjectProgram.ts +++ b/packages/typescript-estree/src/create-program/createProjectProgram.ts @@ -1,5 +1,6 @@ import debug from 'debug'; import path from 'path'; +import * as ts from 'typescript'; import { getProgramsForProjects } from './createWatchProgram'; import { firstDefined } from '../node-utils'; import { Extra } from '../parser-options'; @@ -7,7 +8,16 @@ import { ASTAndProgram, getAstFromProgram } from './shared'; const log = debug('typescript-eslint:typescript-estree:createProjectProgram'); -const DEFAULT_EXTRA_FILE_EXTENSIONS = ['.ts', '.tsx', '.js', '.jsx']; +const DEFAULT_EXTRA_FILE_EXTENSIONS = [ + ts.Extension.Ts, + ts.Extension.Tsx, + ts.Extension.Js, + ts.Extension.Jsx, + ts.Extension.Mjs, + ts.Extension.Mts, + ts.Extension.Cjs, + ts.Extension.Cts, +] as readonly string[]; /** * @param code The code of the file being linted diff --git a/packages/typescript-estree/src/create-program/createSourceFile.ts b/packages/typescript-estree/src/create-program/createSourceFile.ts index 70820c1d2172..e3c6acf6dffb 100644 --- a/packages/typescript-estree/src/create-program/createSourceFile.ts +++ b/packages/typescript-estree/src/create-program/createSourceFile.ts @@ -1,7 +1,7 @@ import debug from 'debug'; import * as ts from 'typescript'; import { Extra } from '../parser-options'; -import { getScriptKind } from './shared'; +import { getScriptKind } from './getScriptKind'; const log = debug('typescript-eslint:typescript-estree:createSourceFile'); @@ -17,7 +17,7 @@ function createSourceFile(code: string, extra: Extra): ts.SourceFile { code, ts.ScriptTarget.Latest, /* setParentNodes */ true, - getScriptKind(extra), + getScriptKind(extra.filePath, extra.jsx), ); } diff --git a/packages/typescript-estree/src/create-program/getScriptKind.ts b/packages/typescript-estree/src/create-program/getScriptKind.ts new file mode 100644 index 000000000000..35b77c3b3dea --- /dev/null +++ b/packages/typescript-estree/src/create-program/getScriptKind.ts @@ -0,0 +1,49 @@ +import path from 'path'; +import * as ts from 'typescript'; + +function getScriptKind(filePath: string, jsx: boolean): ts.ScriptKind { + const extension = path.extname(filePath).toLowerCase(); + // note - we respect the user's extension when it is known we could override it and force it to match their + // jsx setting, but that could create weird situations where we throw parse errors when TSC doesn't + // https://github.com/microsoft/TypeScript/blob/da00ba67ed1182ad334f7c713b8254fba174aeba/src/compiler/utilities.ts#L6948-L6968 + switch (extension) { + case ts.Extension.Js: + case ts.Extension.Cjs: + case ts.Extension.Mjs: + return ts.ScriptKind.JS; + + case ts.Extension.Jsx: + return ts.ScriptKind.JSX; + + case ts.Extension.Ts: + case ts.Extension.Cts: + case ts.Extension.Mts: + return ts.ScriptKind.TS; + + case ts.Extension.Tsx: + return ts.ScriptKind.TSX; + + case ts.Extension.Json: + return ts.ScriptKind.JSON; + + default: + // unknown extension, force typescript to ignore the file extension, and respect the user's setting + return jsx ? ts.ScriptKind.TSX : ts.ScriptKind.TS; + } +} + +function getLanguageVariant(scriptKind: ts.ScriptKind): ts.LanguageVariant { + // https://github.com/microsoft/TypeScript/blob/d6e483b8dabd8fd37c00954c3f2184bb7f1eb90c/src/compiler/utilities.ts#L6281-L6285 + switch (scriptKind) { + case ts.ScriptKind.TSX: + case ts.ScriptKind.JSX: + case ts.ScriptKind.JS: + case ts.ScriptKind.JSON: + return ts.LanguageVariant.JSX; + + default: + return ts.LanguageVariant.Standard; + } +} + +export { getScriptKind, getLanguageVariant }; diff --git a/packages/typescript-estree/src/create-program/shared.ts b/packages/typescript-estree/src/create-program/shared.ts index 53d6fe9ee4de..e202e7ff40bc 100644 --- a/packages/typescript-estree/src/create-program/shared.ts +++ b/packages/typescript-estree/src/create-program/shared.ts @@ -72,40 +72,23 @@ function canonicalDirname(p: CanonicalPath): CanonicalPath { return path.dirname(p) as CanonicalPath; } -function getScriptKind( - extra: Extra, - filePath: string = extra.filePath, -): ts.ScriptKind { - const extension = path.extname(filePath).toLowerCase(); - // note - we respect the user's extension when it is known we could override it and force it to match their - // jsx setting, but that could create weird situations where we throw parse errors when TSC doesn't - switch (extension) { - case '.ts': - return ts.ScriptKind.TS; - - case '.tsx': - return ts.ScriptKind.TSX; - - case '.js': - return ts.ScriptKind.JS; - - case '.jsx': - return ts.ScriptKind.JSX; - - case '.json': - return ts.ScriptKind.JSON; - - default: - // unknown extension, force typescript to ignore the file extension, and respect the user's setting - return extra.jsx ? ts.ScriptKind.TSX : ts.ScriptKind.TS; - } -} - +const DEFINITION_EXTENSIONS = [ + ts.Extension.Dts, + ts.Extension.Dcts, + ts.Extension.Dmts, +] as const; function getExtension(fileName: string | undefined): string | null { if (!fileName) { return null; } - return fileName.endsWith('.d.ts') ? '.d.ts' : path.extname(fileName); + + for (const definitionExt of DEFINITION_EXTENSIONS) { + if (fileName.endsWith(definitionExt)) { + return definitionExt; + } + } + + return path.extname(fileName); } function getAstFromProgram( @@ -149,7 +132,6 @@ export { createDefaultCompilerOptionsFromExtra, ensureAbsolutePath, getCanonicalFileName, - getScriptKind, getAstFromProgram, getModuleResolver, }; diff --git a/packages/typescript-estree/src/index.ts b/packages/typescript-estree/src/index.ts index a922ef199cf7..fda8b30032f5 100644 --- a/packages/typescript-estree/src/index.ts +++ b/packages/typescript-estree/src/index.ts @@ -12,6 +12,7 @@ export { simpleTraverse } from './simple-traverse'; export * from './ts-estree'; export { clearWatchCaches as clearCaches } from './create-program/createWatchProgram'; export { createProgramFromConfigFile as createProgram } from './create-program/useProvidedPrograms'; +export * from './create-program/getScriptKind'; // re-export for backwards-compat export { visitorKeys } from '@typescript-eslint/visitor-keys'; diff --git a/packages/typescript-estree/src/parser-options.ts b/packages/typescript-estree/src/parser-options.ts index 77af781a2588..9763e5894b60 100644 --- a/packages/typescript-estree/src/parser-options.ts +++ b/packages/typescript-estree/src/parser-options.ts @@ -67,7 +67,7 @@ interface ParseOptions { * Enable parsing of JSX. * For more details, see https://www.typescriptlang.org/docs/handbook/jsx.html * - * NOTE: this setting does not effect known file types (.js, .jsx, .ts, .tsx, .json) because the + * NOTE: this setting does not effect known file types (.js, .cjs, .mjs, .jsx, .ts, .mts, .cts, .tsx, .json) because the * TypeScript compiler has its own internal handling for known file extensions. * * For the exact behavior, see https://github.com/typescript-eslint/typescript-eslint/tree/main/packages/parser#parseroptionsecmafeaturesjsx diff --git a/packages/typescript-estree/src/parser.ts b/packages/typescript-estree/src/parser.ts index a0c952b4db52..cce73b952487 100644 --- a/packages/typescript-estree/src/parser.ts +++ b/packages/typescript-estree/src/parser.ts @@ -237,8 +237,10 @@ function applyParserOptionsToExtra(options: TSESTreeOptions): void { /** * Enable JSX - note the applicable file extension is still required */ - if (typeof options.jsx === 'boolean' && options.jsx) { - extra.jsx = true; + if (typeof options.jsx !== 'boolean') { + extra.jsx = false; + } else { + extra.jsx = options.jsx; } /** diff --git a/packages/utils/package.json b/packages/utils/package.json index 130428868ca1..e4fd246d2cb5 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -33,8 +33,8 @@ "postbuild": "downlevel-dts dist _ts3.4/dist", "clean": "tsc -b tsconfig.build.json --clean", "postclean": "rimraf dist && rimraf _ts3.4 && rimraf coverage", - "format": "prettier --write \"./**/*.{ts,js,json,md}\" --ignore-path ../../.prettierignore", - "lint": "eslint . --ext .js,.ts --ignore-path='../../.eslintignore'", + "format": "prettier --write \"./**/*.{ts,mts,cts,tsx,js,mjs,cjs,jsx,json,md,css}\" --ignore-path ../../.prettierignore", + "lint": "eslint . --ignore-path='../../.eslintignore'", "test": "jest --coverage", "typecheck": "tsc -p tsconfig.json --noEmit" }, diff --git a/packages/visitor-keys/package.json b/packages/visitor-keys/package.json index 0cf35c8f412c..13b8112eba73 100644 --- a/packages/visitor-keys/package.json +++ b/packages/visitor-keys/package.json @@ -32,8 +32,8 @@ "postbuild": "downlevel-dts dist _ts3.4/dist", "clean": "tsc -b tsconfig.build.json --clean", "postclean": "rimraf dist && rimraf _ts3.4 && rimraf coverage", - "format": "prettier --write \"./**/*.{ts,js,json,md}\" --ignore-path ../../.prettierignore", - "lint": "eslint . --ext .js,.ts --ignore-path='../../.eslintignore'", + "format": "prettier --write \"./**/*.{ts,mts,cts,tsx,js,mjs,cjs,jsx,json,md,css}\" --ignore-path ../../.prettierignore", + "lint": "eslint . --ignore-path='../../.eslintignore'", "test": "jest --coverage", "typecheck": "tsc -p tsconfig.json --noEmit" }, diff --git a/packages/website-eslint/package.json b/packages/website-eslint/package.json index d437e42c6c05..ea424459197b 100644 --- a/packages/website-eslint/package.json +++ b/packages/website-eslint/package.json @@ -13,7 +13,7 @@ ], "scripts": { "build": "rollup --config=rollup.config.js", - "format": "prettier --write \"./**/*.{ts,js,json,md}\" --ignore-path ../../.prettierignore" + "format": "prettier --write \"./**/*.{ts,mts,cts,tsx,js,mjs,cjs,jsx,json,md,css}\" --ignore-path ../../.prettierignore" }, "dependencies": { "@typescript-eslint/types": "5.25.0", diff --git a/packages/website/package.json b/packages/website/package.json index a0440b4cd5e5..cb745940c814 100644 --- a/packages/website/package.json +++ b/packages/website/package.json @@ -6,7 +6,7 @@ "build": "docusaurus build", "clear": "docusaurus clear", "format": "prettier --write \"./**/*.{md,mdx,ts,js,tsx,jsx}\" --ignore-path ../../.prettierignore", - "lint": "eslint . --ext .js,.ts --ignore-path ../../.eslintignore", + "lint": "eslint . --ignore-path ../../.eslintignore", "serve": "docusaurus serve", "start": "docusaurus start", "swizzle": "docusaurus swizzle", From b561a5aa1bebaf3db2c7f39ba4802651d3617ac1 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 22 May 2022 11:29:01 -0700 Subject: [PATCH 2/8] Update TROUBLESHOOTING.md --- docs/linting/TROUBLESHOOTING.md | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/docs/linting/TROUBLESHOOTING.md b/docs/linting/TROUBLESHOOTING.md index f44af3713f7e..0bf2afd19525 100644 --- a/docs/linting/TROUBLESHOOTING.md +++ b/docs/linting/TROUBLESHOOTING.md @@ -121,15 +121,18 @@ For example; the `JSX` namespace from `@types/react` is a global 3rd party type Note, that for a mixed project including JavaScript and TypeScript, the `no-undef` rule (like any role) can be turned off for TypeScript files alone by adding an `overrides` section to `.eslintrc.cjs`: -```json - "overrides": [ - { - "files": ["*.ts", "*.mts", "*.cts", "*.tsx"], - "rules": { - "no-undef": "off" - } - } - ] +```js title=".eslintrc.cjs" +module.exports ={ + // ... the rest of your config ... + overrides: [ + { + files: ['*.ts', '*.mts', '*.cts', '*.tsx'], + rules: { + 'no-undef': 'off', + }, + }, + ], +}; ``` If you choose to leave on the ESLint `no-undef` lint rule, you can [manually define the set of allowed `globals` in your ESLint config](https://eslint.org/docs/user-guide/configuring/language-options#specifying-globals), and/or you can use one of the [pre-defined environment (`env`) configurations](https://eslint.org/docs/user-guide/configuring/language-options#specifying-environments). From e34a00e8559d60571081b535174995575193dc98 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Mon, 23 May 2022 14:39:51 -0700 Subject: [PATCH 3/8] Update docs/linting/TROUBLESHOOTING.md Co-authored-by: Josh Goldberg --- docs/linting/TROUBLESHOOTING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/linting/TROUBLESHOOTING.md b/docs/linting/TROUBLESHOOTING.md index 0bf2afd19525..43912693170e 100644 --- a/docs/linting/TROUBLESHOOTING.md +++ b/docs/linting/TROUBLESHOOTING.md @@ -119,7 +119,7 @@ As of our v4.0.0 release, this also applies to types. If you use global types from a 3rd party package (i.e. anything from an `@types` package), then you will have to configure ESLint appropriately to define these global types. For example; the `JSX` namespace from `@types/react` is a global 3rd party type that you must define in your ESLint config. -Note, that for a mixed project including JavaScript and TypeScript, the `no-undef` rule (like any role) can be turned off for TypeScript files alone by adding an `overrides` section to `.eslintrc.cjs`: +Note, that for a mixed project including JavaScript and TypeScript, the `no-undef` rule (like any rule) can be turned off for TypeScript files alone by adding an `overrides` section to `.eslintrc.cjs`: ```js title=".eslintrc.cjs" module.exports ={ From d02f76c2cfc78d02037a64f0c5d994adc5f38265 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Mon, 23 May 2022 14:40:04 -0700 Subject: [PATCH 4/8] Update docs/linting/TROUBLESHOOTING.md Co-authored-by: Josh Goldberg --- docs/linting/TROUBLESHOOTING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/linting/TROUBLESHOOTING.md b/docs/linting/TROUBLESHOOTING.md index 43912693170e..d6cb97486f57 100644 --- a/docs/linting/TROUBLESHOOTING.md +++ b/docs/linting/TROUBLESHOOTING.md @@ -122,7 +122,7 @@ For example; the `JSX` namespace from `@types/react` is a global 3rd party type Note, that for a mixed project including JavaScript and TypeScript, the `no-undef` rule (like any rule) can be turned off for TypeScript files alone by adding an `overrides` section to `.eslintrc.cjs`: ```js title=".eslintrc.cjs" -module.exports ={ +module.exports = { // ... the rest of your config ... overrides: [ { From 8e3bec421c2425c7eaa83585d49ecdcb269bbeb7 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Mon, 23 May 2022 14:40:16 -0700 Subject: [PATCH 5/8] Update packages/parser/README.md Co-authored-by: Josh Goldberg --- packages/parser/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/parser/README.md b/packages/parser/README.md index f026a5b4ae2e..018935b2ca9f 100644 --- a/packages/parser/README.md +++ b/packages/parser/README.md @@ -90,7 +90,7 @@ The exact behavior is as follows: - If `parserOptions.project` is _not_ provided: - The setting will be respected. - If `parserOptions.project` is provided (i.e. you are using rules with type information): - - **always parsed as if this is `false`,** + - **always parsed as if this is `false`** ### `parserOptions.ecmaFeatures.globalReturn` From 9203072996d26b9b0d4aa89e8845e0864b0aaeb9 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Mon, 23 May 2022 14:43:24 -0700 Subject: [PATCH 6/8] Update getScriptKind.ts --- .../typescript-estree/src/create-program/getScriptKind.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/typescript-estree/src/create-program/getScriptKind.ts b/packages/typescript-estree/src/create-program/getScriptKind.ts index 35b77c3b3dea..80158f468f7b 100644 --- a/packages/typescript-estree/src/create-program/getScriptKind.ts +++ b/packages/typescript-estree/src/create-program/getScriptKind.ts @@ -3,8 +3,9 @@ import * as ts from 'typescript'; function getScriptKind(filePath: string, jsx: boolean): ts.ScriptKind { const extension = path.extname(filePath).toLowerCase(); - // note - we respect the user's extension when it is known we could override it and force it to match their - // jsx setting, but that could create weird situations where we throw parse errors when TSC doesn't + // note - we only respect the user's jsx setting for unknown extensions + // this is so that we always match TS's internal script kind logic, preventing + // weird errors due to a mismatch. // https://github.com/microsoft/TypeScript/blob/da00ba67ed1182ad334f7c713b8254fba174aeba/src/compiler/utilities.ts#L6948-L6968 switch (extension) { case ts.Extension.Js: From 2a509b55000fb6e2d042d6bfcf014a467485e382 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Tue, 24 May 2022 15:48:11 -0700 Subject: [PATCH 7/8] review fixes --- .../typescript-estree/src/create-program/shared.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/packages/typescript-estree/src/create-program/shared.ts b/packages/typescript-estree/src/create-program/shared.ts index e202e7ff40bc..770b137f212f 100644 --- a/packages/typescript-estree/src/create-program/shared.ts +++ b/packages/typescript-estree/src/create-program/shared.ts @@ -82,13 +82,11 @@ function getExtension(fileName: string | undefined): string | null { return null; } - for (const definitionExt of DEFINITION_EXTENSIONS) { - if (fileName.endsWith(definitionExt)) { - return definitionExt; - } - } - - return path.extname(fileName); + return ( + DEFINITION_EXTENSIONS.find(definitionExt => + fileName.endsWith(definitionExt), + ) ?? path.extname(fileName) + ); } function getAstFromProgram( From e2620c5cdf83a17d4f3fd2aba544f23bc8a94b97 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Tue, 24 May 2022 15:52:01 -0700 Subject: [PATCH 8/8] fix test --- packages/eslint-plugin/src/util/misc.ts | 3 ++- packages/eslint-plugin/tests/util.test.ts | 17 ++++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/packages/eslint-plugin/src/util/misc.ts b/packages/eslint-plugin/src/util/misc.ts index 59a7ccd9fc46..2a9046854a36 100644 --- a/packages/eslint-plugin/src/util/misc.ts +++ b/packages/eslint-plugin/src/util/misc.ts @@ -15,8 +15,9 @@ const DEFINITION_EXTENSIONS = [ * Check if the context file name is *.d.ts or *.d.tsx */ function isDefinitionFile(fileName: string): boolean { + const lowerFileName = fileName.toLowerCase(); for (const definitionExt of DEFINITION_EXTENSIONS) { - if (fileName.endsWith(definitionExt)) { + if (lowerFileName.endsWith(definitionExt)) { return true; } } diff --git a/packages/eslint-plugin/tests/util.test.ts b/packages/eslint-plugin/tests/util.test.ts index 9714ab5718c4..a2f742870779 100644 --- a/packages/eslint-plugin/tests/util.test.ts +++ b/packages/eslint-plugin/tests/util.test.ts @@ -1,7 +1,7 @@ import * as util from '../src/util'; describe('isDefinitionFile', () => { - it('returns false for non-definition files', () => { + describe('returns false for non-definition files', () => { const invalid = [ 'test.js', 'test.jsx', @@ -15,18 +15,25 @@ describe('isDefinitionFile', () => { 'test.tsx', 'test.TS', 'test.TSX', + // yes, it's not a definition file if it's a `.d.tsx`! + 'test.d.tsx', + 'test.D.TSX', ]; invalid.forEach(f => { - expect(util.isDefinitionFile(f)).toBe(false); + it(f, () => { + expect(util.isDefinitionFile(f)).toBe(false); + }); }); }); - it('returns true for definition files', () => { - const valid = ['test.d.ts', 'test.d.tsx', 'test.D.TS', 'test.D.TSX']; + describe('returns true for definition files', () => { + const valid = ['test.d.ts', 'test.D.TS']; valid.forEach(f => { - expect(util.isDefinitionFile(f)).toBe(true); + it(f, () => { + expect(util.isDefinitionFile(f)).toBe(true); + }); }); }); });