From 1b7d98cf019501ea0cfd10cf76ed73cd63119d0b Mon Sep 17 00:00:00 2001 From: zanminkian Date: Sat, 21 Dec 2024 17:32:10 +0000 Subject: [PATCH] refactor(eslint-plugin-esm): `no-side-effect-imports` will not ignore declaration files now --- .changeset/modern-pants-burn.md | 5 ++++ .../src/config/ts/declaration.ts | 1 + .../doc/rules/no-side-effect-imports.md | 27 +++++++++---------- .../src/rules/no-side-effect-imports.spec.ts | 6 ++--- .../src/rules/no-side-effect-imports.ts | 7 ----- 5 files changed, 21 insertions(+), 25 deletions(-) create mode 100644 .changeset/modern-pants-burn.md diff --git a/.changeset/modern-pants-burn.md b/.changeset/modern-pants-burn.md new file mode 100644 index 00000000..7568851e --- /dev/null +++ b/.changeset/modern-pants-burn.md @@ -0,0 +1,5 @@ +--- +"eslint-plugin-esm": minor +--- + +refactor(eslint-plugin-esm): `no-side-effect-imports` will not ignore declaration files now diff --git a/packages/eslint-config/src/config/ts/declaration.ts b/packages/eslint-config/src/config/ts/declaration.ts index 88f32b52..8afb761f 100644 --- a/packages/eslint-config/src/config/ts/declaration.ts +++ b/packages/eslint-config/src/config/ts/declaration.ts @@ -4,6 +4,7 @@ export function getTsDeclaration() { files: ["**/*.d.{ts,cts,mts,tsx}"], rules: { "esm/no-empty-exports": "off", + "esm/no-side-effect-imports": "off", "esm/required-exports": "off", "import/no-default-export": "off", }, diff --git a/packages/eslint-plugin-esm/doc/rules/no-side-effect-imports.md b/packages/eslint-plugin-esm/doc/rules/no-side-effect-imports.md index e1f8fe08..d80bb60b 100644 --- a/packages/eslint-plugin-esm/doc/rules/no-side-effect-imports.md +++ b/packages/eslint-plugin-esm/doc/rules/no-side-effect-imports.md @@ -8,24 +8,23 @@ Side effect import is often used for polyfills and css. It's unsafe to use it. ### Fail ```ts -import 'foo' // filename: foo.ts -import './foo' // filename: foo.ts -import {} from 'foo' // filename: foo.ts -import {} from './foo' // filename: foo.ts -import './reflect-metadata' // filename: foo.ts -import './foo.module.css' // filename: foo.ts -import 'foo.module.css' // filename: foo.ts +import 'foo' +import './foo' +import {} from 'foo' +import {} from './foo' +import './reflect-metadata' +import './foo.module.css' +import 'foo.module.css' ``` ### Pass ```ts -import 'reflect-metadata' // filename: foo.ts -import {} from 'reflect-metadata' // filename: foo.ts -import 'foo.css' // filename: foo.ts -import './foo.css' // filename: foo.ts -import 'module.css' // filename: foo.ts -import {foo} from 'foo' // filename: foo.ts -import 'foo' // filename: foo.d.ts +import 'reflect-metadata' +import {} from 'reflect-metadata' +import 'foo.css' +import './foo.css' +import 'module.css' +import {foo} from 'foo' ``` diff --git a/packages/eslint-plugin-esm/src/rules/no-side-effect-imports.spec.ts b/packages/eslint-plugin-esm/src/rules/no-side-effect-imports.spec.ts index a6ed7df9..8318f470 100644 --- a/packages/eslint-plugin-esm/src/rules/no-side-effect-imports.spec.ts +++ b/packages/eslint-plugin-esm/src/rules/no-side-effect-imports.spec.ts @@ -8,9 +8,7 @@ const valid = [ "import './foo.css'", "import 'module.css'", "import {foo} from 'foo'", -] - .map((code) => ({ code, filename: "foo.ts" })) - .concat({ code: "import 'foo'", filename: "foo.d.ts" }); +]; const invalid = [ "import 'foo'", @@ -20,6 +18,6 @@ const invalid = [ "import './reflect-metadata'", "import './foo.module.css'", "import 'foo.module.css'", -].map((code) => ({ code, filename: "foo.ts" })); +]; test({ valid, invalid, ...noSideEffectImports }); diff --git a/packages/eslint-plugin-esm/src/rules/no-side-effect-imports.ts b/packages/eslint-plugin-esm/src/rules/no-side-effect-imports.ts index eebf9d9b..8a855a4a 100644 --- a/packages/eslint-plugin-esm/src/rules/no-side-effect-imports.ts +++ b/packages/eslint-plugin-esm/src/rules/no-side-effect-imports.ts @@ -20,13 +20,6 @@ export const noSideEffectImports = createRule({ message: "Side effect import is often used for polyfills and css. It's unsafe to use it.", create: (context) => { - if ( - [".d.ts", ".d.cts", ".d.mts", ".d.tsx"].some((ext) => - context.filename.endsWith(ext), - ) - ) { - return {}; - } const ignoreExps = ignores.map((ignore) => new RegExp(ignore)); return { "ImportDeclaration[specifiers.length=0]": (node: ImportDeclaration) => {