Skip to content

Commit

Permalink
fix(sort-imports): empty named imports being considered side-effect i…
Browse files Browse the repository at this point in the history
…mports
  • Loading branch information
hampus-stravito authored Apr 15, 2024
1 parent 46cb865 commit ca69069
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
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: [],
},
)
})
})

0 comments on commit ca69069

Please sign in to comment.