Skip to content

Commit

Permalink
fix: prefix-only builtin modules are defined as external modules in s…
Browse files Browse the repository at this point in the history
…ort-imports rule
  • Loading branch information
Wondermarin committed Sep 8, 2023
1 parent 3e11eae commit 92b7240
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
5 changes: 3 additions & 2 deletions rules/sort-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,9 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
tsPaths.some(pattern => minimatch(nodeElement.source.value, pattern))

let isCoreModule = (value: string) =>
builtinModules.includes(value) ||
builtinModules.includes(`node:${value}`)
builtinModules.includes(
value.startsWith('node:') ? value.split('node:')[1] : value,
)

if (node.importKind === 'type') {
if (node.type === 'ImportDeclaration') {
Expand Down
48 changes: 48 additions & 0 deletions test/sort-imports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3232,5 +3232,53 @@ describe(RULE_NAME, () => {
invalid: [],
},
)

ruleTester.run(
`${RULE_NAME}: defines prefix-only builtin modules as core node modules`,
rule,
{
valid: [
{
code: dedent`
import { writeFile } from 'node:fs/promises'
import { useEffect } from 'react'
`,
options: [
{
groups: ['builtin', 'external'],
},
],
},
],
invalid: [
{
code: dedent`
import { writeFile } from 'node:fs/promises'
import { useEffect } from 'react'
`,
output: dedent`
import { writeFile } from 'node:fs/promises'
import { useEffect } from 'react'
`,
options: [
{
groups: ['builtin', 'external'],
},
],
errors: [
{
messageId: 'missedSpacingBetweenImports',
data: {
left: 'node:fs/promises',
right: 'react',
},
},
],
},
],
},
)
})
})

0 comments on commit 92b7240

Please sign in to comment.