diff --git a/.cspell.json b/.cspell.json index 4a8c0ad226b..09466a9d300 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 e4d508f6784..691f6e0eb0e 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 e42c436bd1d..dbf08d1e780 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 8f8ae10565d..d6cb97486f5 100644 --- a/docs/linting/TROUBLESHOOTING.md +++ b/docs/linting/TROUBLESHOOTING.md @@ -119,17 +119,20 @@ 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: - -```json - "overrides": [ - { - "files": ["*.ts"], - "rules": { - "no-undef": "off" - } - } - ] +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 = { + // ... 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). @@ -206,7 +209,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 5ac8f44c2c1..c085843b842 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 cc762e151c8..1ac8f50c8c2 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 9bbcb633b10..359b8178ca1 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 710adf382ea..dd5206a7e1e 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 999924414dd..c27f3028638 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 210871ab347..8faf8aab503 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 4d14797d02c..7570e6e4667 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 d968504be07..26809892e67 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 9356f6b326f..599726f50ae 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 125c093b2bc..71443e1f52e 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 8479feb728d..2a9046854a3 100644 --- a/packages/eslint-plugin/src/util/misc.ts +++ b/packages/eslint-plugin/src/util/misc.ts @@ -4,12 +4,24 @@ 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 || ''); + const lowerFileName = fileName.toLowerCase(); + for (const definitionExt of DEFINITION_EXTENSIONS) { + if (lowerFileName.endsWith(definitionExt)) { + return true; + } + } + return false; } /** diff --git a/packages/eslint-plugin/tests/util.test.ts b/packages/eslint-plugin/tests/util.test.ts index 9714ab5718c..a2f74287077 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); + }); }); }); }); diff --git a/packages/experimental-utils/package.json b/packages/experimental-utils/package.json index beaad1f279d..bf50f5d279f 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 81df0519497..018935b2ca9 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 fcb40066bf4..31abedd2599 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 365bff6648f..7fcd45f96f7 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 e45e825f30a..6d149f2e8d3 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 bd660ad1990..6945f11a984 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 e1b20f66ede..bc64cc384a9 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 f235f3dcf21..02946daa80d 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 24366e8f398..14f61aeb0d0 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 70169c66891..c4392941bb6 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 69903a5bcaf..4558cc36b55 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 70820c1d217..e3c6acf6dff 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 00000000000..80158f468f7 --- /dev/null +++ b/packages/typescript-estree/src/create-program/getScriptKind.ts @@ -0,0 +1,50 @@ +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 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: + 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 53d6fe9ee4d..770b137f212 100644 --- a/packages/typescript-estree/src/create-program/shared.ts +++ b/packages/typescript-estree/src/create-program/shared.ts @@ -72,40 +72,21 @@ 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); + + return ( + DEFINITION_EXTENSIONS.find(definitionExt => + fileName.endsWith(definitionExt), + ) ?? path.extname(fileName) + ); } function getAstFromProgram( @@ -149,7 +130,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 a922ef199cf..fda8b30032f 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 77af781a258..9763e5894b6 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 a0c952b4db5..cce73b95248 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 502ddbdea2c..06b6e8270e0 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 f7b0b512ace..555e4aeac86 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 cdb3e2847d4..555a4984174 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.26.0", diff --git a/packages/website/package.json b/packages/website/package.json index 35309c4e44d..d1c7b6536fb 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",