Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sort-imports): empty named imports being considered side-effect imports #129

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion rules/sort-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,10 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
let nodes: SortingNode[] = []

let isSideEffectImport = (node: TSESTree.Node) =>
node.type === 'ImportDeclaration' && node.specifiers.length === 0
node.type === 'ImportDeclaration' &&
node.specifiers.length === 0 &&
/* Avoid matching on named imports without specifiers */
!/}\s*from\s+/.test(context.sourceCode.getText(node))

let computeGroup = (node: ModuleDeclaration): Group<string[]> => {
let isStyle = (value: string) =>
Expand Down
23 changes: 23 additions & 0 deletions test/sort-imports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3984,5 +3984,28 @@ describe(RULE_NAME, () => {
],
},
)

ruleTester.run(
`${RULE_NAME}: does not consider empty named imports to be side-effects`,
rule,
{
valid: [
{
code: dedent`
import {} from 'node:os'
import { hinaAmano } from 'weathering-with-you'
import 'node:os'
`,
options: [
{
'newlines-between': NewlinesBetweenValue.never,
groups: ['builtin', 'external', 'side-effect'],
},
],
},
],
invalid: [],
},
)
})
})
Loading