Skip to content

Commit 932c5c9

Browse files
committedNov 19, 2024
fix: support optional chaining imports
1 parent 21885b1 commit 932c5c9

File tree

2 files changed

+176
-3
lines changed

2 files changed

+176
-3
lines changed
 

‎rules/sort-imports.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -353,10 +353,12 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
353353
/* Avoid matching on named imports without specifiers */
354354
!/\}\s*from\s+/u.test(sourceCode.getText(node))
355355

356-
let isStyle = (value: string): boolean =>
357-
['.less', '.scss', '.sass', '.styl', '.pcss', '.css', '.sss'].some(
358-
extension => value.endsWith(extension),
356+
let isStyle = (value: string): boolean => {
357+
let [cleanedValue] = value.split('?')
358+
return ['.less', '.scss', '.sass', '.styl', '.pcss', '.css', '.sss'].some(
359+
extension => cleanedValue.endsWith(extension),
359360
)
361+
}
360362

361363
let flatGroups = new Set(options.groups.flat())
362364
let shouldRegroupSideEffectNodes = flatGroups.has('side-effect')

‎test/sort-imports.test.ts

+171
Original file line numberDiff line numberDiff line change
@@ -2308,6 +2308,63 @@ describe(ruleName, () => {
23082308
invalid: [],
23092309
},
23102310
)
2311+
2312+
ruleTester.run(
2313+
`${ruleName}(${type}): supports style imports with optional chaining`,
2314+
rule,
2315+
{
2316+
valid: [
2317+
{
2318+
code: dedent`
2319+
import b from './b.css?raw'
2320+
import c from './c.css'
2321+
2322+
import a from './a.js'
2323+
`,
2324+
options: [
2325+
{
2326+
...options,
2327+
groups: ['style', 'unknown'],
2328+
newlinesBetween: 'always',
2329+
},
2330+
],
2331+
},
2332+
],
2333+
invalid: [
2334+
{
2335+
code: dedent`
2336+
import a from './a.js'
2337+
import b from './b.css?raw'
2338+
import c from './c.css'
2339+
`,
2340+
output: dedent`
2341+
import b from './b.css?raw'
2342+
import c from './c.css'
2343+
2344+
import a from './a.js'
2345+
`,
2346+
options: [
2347+
{
2348+
...options,
2349+
groups: ['style', 'unknown'],
2350+
newlinesBetween: 'always',
2351+
},
2352+
],
2353+
errors: [
2354+
{
2355+
messageId: 'unexpectedImportsGroupOrder',
2356+
data: {
2357+
left: './a.js',
2358+
leftGroup: 'unknown',
2359+
right: './b.css?raw',
2360+
rightGroup: 'style',
2361+
},
2362+
},
2363+
],
2364+
},
2365+
],
2366+
},
2367+
)
23112368
})
23122369

23132370
describe(`${ruleName}: sorting by natural order`, () => {
@@ -3805,6 +3862,63 @@ describe(ruleName, () => {
38053862
],
38063863
},
38073864
)
3865+
3866+
ruleTester.run(
3867+
`${ruleName}(${type}): supports style imports with optional chaining`,
3868+
rule,
3869+
{
3870+
valid: [
3871+
{
3872+
code: dedent`
3873+
import b from './b.css?raw'
3874+
import c from './c.css'
3875+
3876+
import a from './a.js'
3877+
`,
3878+
options: [
3879+
{
3880+
...options,
3881+
groups: ['style', 'unknown'],
3882+
newlinesBetween: 'always',
3883+
},
3884+
],
3885+
},
3886+
],
3887+
invalid: [
3888+
{
3889+
code: dedent`
3890+
import a from './a.js'
3891+
import b from './b.css?raw'
3892+
import c from './c.css'
3893+
`,
3894+
output: dedent`
3895+
import b from './b.css?raw'
3896+
import c from './c.css'
3897+
3898+
import a from './a.js'
3899+
`,
3900+
options: [
3901+
{
3902+
...options,
3903+
groups: ['style', 'unknown'],
3904+
newlinesBetween: 'always',
3905+
},
3906+
],
3907+
errors: [
3908+
{
3909+
messageId: 'unexpectedImportsGroupOrder',
3910+
data: {
3911+
left: './a.js',
3912+
leftGroup: 'unknown',
3913+
right: './b.css?raw',
3914+
rightGroup: 'style',
3915+
},
3916+
},
3917+
],
3918+
},
3919+
],
3920+
},
3921+
)
38083922
})
38093923

38103924
describe(`${ruleName}: sorting by line length`, () => {
@@ -5374,6 +5488,63 @@ describe(ruleName, () => {
53745488
],
53755489
},
53765490
)
5491+
5492+
ruleTester.run(
5493+
`${ruleName}(${type}): supports style imports with optional chaining`,
5494+
rule,
5495+
{
5496+
valid: [
5497+
{
5498+
code: dedent`
5499+
import b from './b.css?raw'
5500+
import c from './c.css'
5501+
5502+
import a from './a.js'
5503+
`,
5504+
options: [
5505+
{
5506+
...options,
5507+
groups: ['style', 'unknown'],
5508+
newlinesBetween: 'always',
5509+
},
5510+
],
5511+
},
5512+
],
5513+
invalid: [
5514+
{
5515+
code: dedent`
5516+
import a from './a.js'
5517+
import b from './b.css?raw'
5518+
import c from './c.css'
5519+
`,
5520+
output: dedent`
5521+
import b from './b.css?raw'
5522+
import c from './c.css'
5523+
5524+
import a from './a.js'
5525+
`,
5526+
options: [
5527+
{
5528+
...options,
5529+
groups: ['style', 'unknown'],
5530+
newlinesBetween: 'always',
5531+
},
5532+
],
5533+
errors: [
5534+
{
5535+
messageId: 'unexpectedImportsGroupOrder',
5536+
data: {
5537+
left: './a.js',
5538+
leftGroup: 'unknown',
5539+
right: './b.css?raw',
5540+
rightGroup: 'style',
5541+
},
5542+
},
5543+
],
5544+
},
5545+
],
5546+
},
5547+
)
53775548
})
53785549

53795550
describe(`${ruleName}: validating group configuration`, () => {

0 commit comments

Comments
 (0)
Please sign in to comment.