Skip to content

Commit c8b2fa4

Browse files
committed
fix: add ability to select enviorenment and separate bun modules
1 parent a2b88dd commit c8b2fa4

File tree

3 files changed

+159
-11
lines changed

3 files changed

+159
-11
lines changed

docs/content/rules/sort-imports.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,12 @@ You can use this to sort by the import name only, excluding the elements, when t
276276
277277
**\*Note:** Only available for `line-length` type.
278278
279+
### environment
280+
281+
<sub>(default: `'node'`)</sub>
282+
283+
This field is responsible for which modules will be counted as builtin modules. If you are using Bun, change the value to `'bun'`.
284+
279285
## Usage
280286
281287
<CodeTabs

rules/sort-imports.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ type Options<T extends string[]> = [
6060
type: 'alphabetical' | 'line-length' | 'natural'
6161
newlinesBetween: NewlinesBetweenValue
6262
groups: (Group<T>[] | Group<T>)[]
63+
environment: 'node' | 'bun'
6364
internalPattern: string[]
6465
maxLineLength?: number
6566
order: 'desc' | 'asc'
@@ -136,6 +137,11 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
136137
minimum: 0,
137138
exclusiveMinimum: true,
138139
},
140+
environment: {
141+
enum: ['node', 'bun'],
142+
default: 'node',
143+
type: 'string',
144+
},
139145
},
140146
allOf: [
141147
{
@@ -191,6 +197,7 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
191197
customGroups: { type: {}, value: {} },
192198
internalPattern: ['~/**'],
193199
type: 'alphabetical',
200+
environment: 'node',
194201
ignoreCase: true,
195202
order: 'asc',
196203
groups: [],
@@ -270,7 +277,8 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
270277
return (
271278
builtinModules.includes(
272279
value.startsWith('node:') ? value.split('node:')[1] : value,
273-
) || bunModules.includes(value)
280+
) ||
281+
(options.environment === 'bun' ? bunModules.includes(value) : false)
274282
)
275283
}
276284

test/sort-imports.test.ts

Lines changed: 144 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,6 +1158,54 @@ describe(RULE_NAME, () => {
11581158
],
11591159
},
11601160
)
1161+
1162+
ruleTester.run(`${RULE_NAME}(${type}): allows to use bun modules`, rule, {
1163+
valid: [
1164+
{
1165+
code: dedent`
1166+
import { expect } from 'bun:test'
1167+
import { a } from 'a'
1168+
`,
1169+
options: [
1170+
{
1171+
...options,
1172+
newlinesBetween: NewlinesBetweenValue.never,
1173+
groups: ['builtin', 'external', 'unknown'],
1174+
environment: 'bun',
1175+
},
1176+
],
1177+
},
1178+
],
1179+
invalid: [
1180+
{
1181+
code: dedent`
1182+
import { a } from 'a'
1183+
import { expect } from 'bun:test'
1184+
`,
1185+
output: dedent`
1186+
import { expect } from 'bun:test'
1187+
import { a } from 'a'
1188+
`,
1189+
options: [
1190+
{
1191+
...options,
1192+
newlinesBetween: NewlinesBetweenValue.never,
1193+
groups: ['builtin', 'external', 'unknown'],
1194+
environment: 'bun',
1195+
},
1196+
],
1197+
errors: [
1198+
{
1199+
messageId: 'unexpectedImportsOrder',
1200+
data: {
1201+
left: 'a',
1202+
right: 'bun:test',
1203+
},
1204+
},
1205+
],
1206+
},
1207+
],
1208+
})
11611209
})
11621210

11631211
describe(`${RULE_NAME}: sorting by natural order`, () => {
@@ -2301,6 +2349,54 @@ describe(RULE_NAME, () => {
23012349
],
23022350
},
23032351
)
2352+
2353+
ruleTester.run(`${RULE_NAME}(${type}): allows to use bun modules`, rule, {
2354+
valid: [
2355+
{
2356+
code: dedent`
2357+
import { expect } from 'bun:test'
2358+
import { a } from 'a'
2359+
`,
2360+
options: [
2361+
{
2362+
...options,
2363+
newlinesBetween: NewlinesBetweenValue.never,
2364+
groups: ['builtin', 'external', 'unknown'],
2365+
environment: 'bun',
2366+
},
2367+
],
2368+
},
2369+
],
2370+
invalid: [
2371+
{
2372+
code: dedent`
2373+
import { a } from 'a'
2374+
import { expect } from 'bun:test'
2375+
`,
2376+
output: dedent`
2377+
import { expect } from 'bun:test'
2378+
import { a } from 'a'
2379+
`,
2380+
options: [
2381+
{
2382+
...options,
2383+
newlinesBetween: NewlinesBetweenValue.never,
2384+
groups: ['builtin', 'external', 'unknown'],
2385+
environment: 'bun',
2386+
},
2387+
],
2388+
errors: [
2389+
{
2390+
messageId: 'unexpectedImportsOrder',
2391+
data: {
2392+
left: 'a',
2393+
right: 'bun:test',
2394+
},
2395+
},
2396+
],
2397+
},
2398+
],
2399+
})
23042400
})
23052401

23062402
describe(`${RULE_NAME}: sorting by line length`, () => {
@@ -3569,6 +3665,54 @@ describe(RULE_NAME, () => {
35693665
).toBe('data[0].maxLineLength should be > 0')
35703666
})
35713667
})
3668+
3669+
ruleTester.run(`${RULE_NAME}(${type}): allows to use bun modules`, rule, {
3670+
valid: [
3671+
{
3672+
code: dedent`
3673+
import { expect } from 'bun:test'
3674+
import { a } from 'a'
3675+
`,
3676+
options: [
3677+
{
3678+
...options,
3679+
newlinesBetween: NewlinesBetweenValue.never,
3680+
groups: ['builtin', 'external', 'unknown'],
3681+
environment: 'bun',
3682+
},
3683+
],
3684+
},
3685+
],
3686+
invalid: [
3687+
{
3688+
code: dedent`
3689+
import { a } from 'a'
3690+
import { expect } from 'bun:test'
3691+
`,
3692+
output: dedent`
3693+
import { expect } from 'bun:test'
3694+
import { a } from 'a'
3695+
`,
3696+
options: [
3697+
{
3698+
...options,
3699+
newlinesBetween: NewlinesBetweenValue.never,
3700+
groups: ['builtin', 'external', 'unknown'],
3701+
environment: 'bun',
3702+
},
3703+
],
3704+
errors: [
3705+
{
3706+
messageId: 'unexpectedImportsOrder',
3707+
data: {
3708+
left: 'a',
3709+
right: 'bun:test',
3710+
},
3711+
},
3712+
],
3713+
},
3714+
],
3715+
})
35723716
})
35733717

35743718
describe(`${RULE_NAME}: misc`, () => {
@@ -3643,7 +3787,6 @@ describe(RULE_NAME, () => {
36433787
valid: [
36443788
{
36453789
code: dedent`
3646-
import { expect, test } from 'bun:test'
36473790
import { writeFile } from 'node:fs/promises'
36483791
36493792
import { useEffect } from 'react'
@@ -3660,10 +3803,8 @@ describe(RULE_NAME, () => {
36603803
code: dedent`
36613804
import { writeFile } from 'node:fs/promises'
36623805
import { useEffect } from 'react'
3663-
import { expect, test } from 'bun:test'
36643806
`,
36653807
output: dedent`
3666-
import { expect, test } from 'bun:test'
36673808
import { writeFile } from 'node:fs/promises'
36683809
36693810
import { useEffect } from 'react'
@@ -3681,13 +3822,6 @@ describe(RULE_NAME, () => {
36813822
right: 'react',
36823823
},
36833824
},
3684-
{
3685-
messageId: 'unexpectedImportsOrder',
3686-
data: {
3687-
left: 'react',
3688-
right: 'bun:test',
3689-
},
3690-
},
36913825
],
36923826
},
36933827
],

0 commit comments

Comments
 (0)