Skip to content

Commit

Permalink
refactor(eslint-plugin-esm): no-side-effect-imports will not ignore…
Browse files Browse the repository at this point in the history
… declaration files now
  • Loading branch information
zanminkian committed Dec 21, 2024
1 parent 8b468ec commit 1b7d98c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .changeset/modern-pants-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-esm": minor
---

refactor(eslint-plugin-esm): `no-side-effect-imports` will not ignore declaration files now
1 change: 1 addition & 0 deletions packages/eslint-config/src/config/ts/declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
Expand Down
27 changes: 13 additions & 14 deletions packages/eslint-plugin-esm/doc/rules/no-side-effect-imports.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
```
<!-- prettier-ignore-end -->
Original file line number Diff line number Diff line change
Expand Up @@ -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'",
Expand All @@ -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 });
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit 1b7d98c

Please sign in to comment.