Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.9k
feat(eslint-plugin): [no-import-type-side-effects] add rule to warn against runtime side effects withverbatimModuleSyntax#6394
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
7 commits Select commitHold shift + click to select a range
fedea7a feat(eslint-plugin): add rule to warn against runtime side effects wi…
bradzacherfb41743 lint
bradzacher997ae3e rename rule
bradzacher903dd6a rename rule
bradzacher3162d56 Apply suggestions from code review
bradzacheraf024ee bad merge
bradzacherda7b02f fix bad change
bradzacherFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
6 changes: 6 additions & 0 deletionspackages/eslint-plugin/docs/rules/consistent-type-imports.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletionspackages/eslint-plugin/docs/rules/no-import-type-side-effects.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| --- | ||
| description: 'Enforce the use of top-level import type qualifier when an import only has specifiers with inline type qualifiers.' | ||
| --- | ||
| > 🛑 This file is source code, not the primary documentation location! 🛑 | ||
| > | ||
| > See **https://typescript-eslint.io/rules/no-import-type-side-effects** for documentation. | ||
| The [`--verbatimModuleSyntax`](https://www.typescriptlang.org/tsconfig#verbatimModuleSyntax) compiler option causes TypeScript to do simple and predictable transpilation on import declarations. | ||
| Namely, it completely removes import declarations with a top-level `type` qualifier, and it removes any import specifiers with an inline `type` qualifier. | ||
| The latter behavior does have one potentially surprising effect in that in certain cases TS can leave behind a "side effect" import at runtime: | ||
| ```ts | ||
| import { type A, type B } from 'mod'; | ||
| // is transpiled to | ||
| import {} from 'mod'; | ||
| // which is the same as | ||
| import 'mod'; | ||
| ``` | ||
| For the rare case of needing to import for side effects, this may be desirable - but for most cases you will not want to leave behind an unnecessary side effect import. | ||
| ## Examples | ||
| This rule enforces that you use a top-level `type` qualifier for imports when it only imports specifiers with an inline `type` qualifier | ||
| <!--tabs--> | ||
| ### ❌ Incorrect | ||
| ```ts | ||
| import { type A } from 'mod'; | ||
| import { type A as AA } from 'mod'; | ||
| import { type A, type B } from 'mod'; | ||
| import { type A as AA, type B as BB } from 'mod'; | ||
| ``` | ||
| ### ✅ Correct | ||
| ```ts | ||
| import type { A } from 'mod'; | ||
| import type { A as AA } from 'mod'; | ||
| import type { A, B } from 'mod'; | ||
| import type { A as AA, B as BB } from 'mod'; | ||
| import T from 'mod'; | ||
| import type T from 'mod'; | ||
| import * as T from 'mod'; | ||
| import type * as T from 'mod'; | ||
| import { T } from 'mod'; | ||
| import type { T } from 'mod'; | ||
| import { T, U } from 'mod'; | ||
| import type { T, U } from 'mod'; | ||
| import { type T, U } from 'mod'; | ||
| import { T, type U } from 'mod'; | ||
| import type T, { U } from 'mod'; | ||
| import T, { type U } from 'mod'; | ||
| ``` | ||
| ## When Not To Use It | ||
| - If you want to leave behind side effect imports, then you shouldn't use this rule. | ||
| - If you're not using TypeScript 5.0's `verbatimModuleSyntax` option, then you don't need this rule. | ||
| ## Related To | ||
| - [`consistent-type-imports`](./consistent-type-imports.md) | ||
| - [`import/consistent-type-specifier-style`](https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/consistent-type-specifier-style.md) | ||
| - [`import/no-duplicates` with `{"prefer-inline": true}`](https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-duplicates.md#inline-type-imports) |
4 changes: 2 additions & 2 deletionspackages/eslint-plugin/src/configs/all.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -55,7 +55,6 @@ export = { | ||
| 'no-dupe-class-members': 'off', | ||
| '@typescript-eslint/no-dupe-class-members': 'error', | ||
| '@typescript-eslint/no-duplicate-enum-values': 'error', | ||
bradzacher marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| '@typescript-eslint/no-dynamic-delete': 'error', | ||
| 'no-empty-function': 'off', | ||
| '@typescript-eslint/no-empty-function': 'error', | ||
| @@ -71,6 +70,7 @@ export = { | ||
| '@typescript-eslint/no-for-in-array': 'error', | ||
| 'no-implied-eval': 'off', | ||
| '@typescript-eslint/no-implied-eval': 'error', | ||
| '@typescript-eslint/no-import-type-side-effects': 'error', | ||
| '@typescript-eslint/no-inferrable-types': 'error', | ||
| 'no-invalid-this': 'off', | ||
| '@typescript-eslint/no-invalid-this': 'error', | ||
| @@ -88,7 +88,6 @@ export = { | ||
| '@typescript-eslint/no-non-null-asserted-nullish-coalescing': 'error', | ||
| '@typescript-eslint/no-non-null-asserted-optional-chain': 'error', | ||
| '@typescript-eslint/no-non-null-assertion': 'error', | ||
| 'no-redeclare': 'off', | ||
| '@typescript-eslint/no-redeclare': 'error', | ||
| '@typescript-eslint/no-redundant-type-constituents': 'error', | ||
| @@ -128,6 +127,7 @@ export = { | ||
| '@typescript-eslint/object-curly-spacing': 'error', | ||
| 'padding-line-between-statements': 'off', | ||
| '@typescript-eslint/padding-line-between-statements': 'error', | ||
| '@typescript-eslint/parameter-properties': 'error', | ||
| '@typescript-eslint/prefer-as-const': 'error', | ||
| '@typescript-eslint/prefer-enum-initializers': 'error', | ||
| '@typescript-eslint/prefer-for-of': 'error', | ||
2 changes: 1 addition & 1 deletionpackages/eslint-plugin/src/configs/strict.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
24 changes: 6 additions & 18 deletionspackages/eslint-plugin/src/rules/consistent-type-imports.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletionspackages/eslint-plugin/src/rules/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletionspackages/eslint-plugin/src/rules/no-import-type-side-effects.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import type { TSESLint, TSESTree } from '@typescript-eslint/utils'; | ||
| import { AST_NODE_TYPES } from '@typescript-eslint/utils'; | ||
| import * as util from '../util'; | ||
| type Options = []; | ||
| type MessageIds = 'useTopLevelQualifier'; | ||
| export default util.createRule<Options, MessageIds>({ | ||
| name: 'no-import-type-side-effects', | ||
| meta: { | ||
| type: 'problem', | ||
| docs: { | ||
| description: | ||
| 'Enforce the use of top-level import type qualifier when an import only has specifiers with inline type qualifiers', | ||
| recommended: false, | ||
| }, | ||
| fixable: 'code', | ||
| messages: { | ||
| useTopLevelQualifier: | ||
| 'TypeScript will only remove the inline type specifiers which will leave behind a side effect import at runtime. Convert this to a top-level type qualifier to properly remove the entire import.', | ||
| }, | ||
| schema: [], | ||
| }, | ||
| defaultOptions: [], | ||
| create(context) { | ||
| const sourceCode = context.getSourceCode(); | ||
| return { | ||
| 'ImportDeclaration[importKind!="type"]'( | ||
| node: TSESTree.ImportDeclaration, | ||
| ): void { | ||
| const specifiers: TSESTree.ImportSpecifier[] = []; | ||
| for (const specifier of node.specifiers) { | ||
| if ( | ||
| specifier.type !== AST_NODE_TYPES.ImportSpecifier || | ||
| specifier.importKind !== 'type' | ||
| ) { | ||
| return; | ||
| } | ||
| specifiers.push(specifier); | ||
| } | ||
| context.report({ | ||
| node, | ||
| messageId: 'useTopLevelQualifier', | ||
| fix(fixer) { | ||
| const fixes: TSESLint.RuleFix[] = []; | ||
| for (const specifier of specifiers) { | ||
| const qualifier = util.nullThrows( | ||
| sourceCode.getFirstToken(specifier, util.isTypeKeyword), | ||
| util.NullThrowsReasons.MissingToken( | ||
| 'type keyword', | ||
| 'import specifier', | ||
| ), | ||
| ); | ||
| fixes.push( | ||
| fixer.removeRange([ | ||
| qualifier.range[0], | ||
| specifier.imported.range[0], | ||
| ]), | ||
| ); | ||
| } | ||
| const importKeyword = util.nullThrows( | ||
| sourceCode.getFirstToken(node, util.isImportKeyword), | ||
| util.NullThrowsReasons.MissingToken('import keyword', 'import'), | ||
| ); | ||
| fixes.push(fixer.insertTextAfter(importKeyword, ' type')); | ||
| return fixes; | ||
| }, | ||
| }); | ||
| }, | ||
| }; | ||
| }, | ||
| }); |
44 changes: 44 additions & 0 deletionspackages/eslint-plugin/tests/rules/no-import-type-side-effects.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import rule from '../../src/rules/no-import-type-side-effects'; | ||
| import { RuleTester } from '../RuleTester'; | ||
| const ruleTester = new RuleTester({ | ||
| parser: '@typescript-eslint/parser', | ||
| }); | ||
| ruleTester.run('no-import-type-side-effects', rule, { | ||
| valid: [ | ||
| "import T from 'mod';", | ||
| "import * as T from 'mod';", | ||
| "import { T } from 'mod';", | ||
| "import type { T } from 'mod';", | ||
| "import type { T, U } from 'mod';", | ||
| "import { type T, U } from 'mod';", | ||
| "import { T, type U } from 'mod';", | ||
| "import type T from 'mod';", | ||
| "import type T, { U } from 'mod';", | ||
| "import T, { type U } from 'mod';", | ||
| "import type * as T from 'mod';", | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: "import { type A } from 'mod';", | ||
| output: "import type { A } from 'mod';", | ||
| errors: [{ messageId: 'useTopLevelQualifier' }], | ||
| }, | ||
| { | ||
| code: "import { type A as AA } from 'mod';", | ||
| output: "import type { A as AA } from 'mod';", | ||
| errors: [{ messageId: 'useTopLevelQualifier' }], | ||
| }, | ||
| { | ||
| code: "import { type A, type B } from 'mod';", | ||
| output: "import type { A, B } from 'mod';", | ||
| errors: [{ messageId: 'useTopLevelQualifier' }], | ||
| }, | ||
| { | ||
| code: "import { type A as AA, type B as BB } from 'mod';", | ||
| output: "import type { A as AA, B as BB } from 'mod';", | ||
| errors: [{ messageId: 'useTopLevelQualifier' }], | ||
| }, | ||
| ], | ||
| }); |
2 changes: 1 addition & 1 deletionpackages/type-utils/tests/isTypeReadonly.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletionspackages/utils/src/ast-utils/predicates.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.