@@ -63,10 +63,6 @@ type Options<T extends string[]> = [
6363 } > ,
6464]
6565
66- type ModuleDeclaration =
67- | TSESTree . TSImportEqualsDeclaration
68- | TSESTree . ImportDeclaration
69-
7066export default createEslintRule < Options < string [ ] > , MESSAGE_ID > ( {
7167 name : 'sort-imports' ,
7268 meta : {
@@ -265,7 +261,12 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
265261 /* Avoid matching on named imports without specifiers */
266262 ! / } \s * f r o m \s + / . test ( sourceCode . getText ( node ) )
267263
268- let computeGroup = ( node : ModuleDeclaration ) : Group < string [ ] > => {
264+ let computeGroup = (
265+ node :
266+ | TSESTree . TSImportEqualsDeclaration
267+ | TSESTree . VariableDeclaration
268+ | TSESTree . ImportDeclaration ,
269+ ) : Group < string [ ] > => {
269270 let isStyle = ( value : string ) =>
270271 [ '.less' , '.scss' , '.sass' , '.styl' , '.pcss' , '.css' , '.sss' ] . some (
271272 extension => value . endsWith ( extension ) ,
@@ -288,10 +289,10 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
288289
289290 let { getGroup, defineGroup, setCustomGroups } = useGroups ( options . groups )
290291
291- let isInternal = ( nodeElement : TSESTree . ImportDeclaration ) =>
292+ let isInternal = ( value : string ) =>
292293 options . internalPattern . length &&
293294 options . internalPattern . some ( pattern =>
294- minimatch ( nodeElement . source . value , pattern , {
295+ minimatch ( value , pattern , {
295296 nocomment : true ,
296297 } ) ,
297298 )
@@ -319,7 +320,7 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
319320 let isExternal = ( value : string ) =>
320321 ! ( value . startsWith ( '.' ) || value . startsWith ( '/' ) )
321322
322- if ( node . importKind === 'type' ) {
323+ if ( node . type !== 'VariableDeclaration' && node . importKind === 'type' ) {
323324 if ( node . type === 'ImportDeclaration' ) {
324325 setCustomGroups ( options . customGroups . type , node . source . value )
325326
@@ -335,7 +336,7 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
335336 defineGroup ( 'parent-type' )
336337 }
337338
338- if ( isInternal ( node ) ) {
339+ if ( isInternal ( node . source . value ) ) {
339340 defineGroup ( 'internal-type' )
340341 }
341342
@@ -351,42 +352,55 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
351352 defineGroup ( 'type' )
352353 }
353354
354- if ( node . type === 'ImportDeclaration' ) {
355- setCustomGroups ( options . customGroups . value , node . source . value )
355+ if (
356+ node . type === 'ImportDeclaration' ||
357+ node . type === 'VariableDeclaration'
358+ ) {
359+ let value =
360+ node . type === 'ImportDeclaration'
361+ ? node . source . value
362+ : (
363+ ( node . declarations [ 0 ] . init as TSESTree . CallExpression )
364+ . arguments [ 0 ] as TSESTree . Literal
365+ )
366+ . value ! . toString ( )
367+ . toString ( )
368+
369+ setCustomGroups ( options . customGroups . value , value )
356370
357- if ( isSideEffectImport ( node ) && isStyle ( node . source . value ) ) {
371+ if ( isSideEffectImport ( node ) && isStyle ( value ) ) {
358372 defineGroup ( 'side-effect-style' )
359373 }
360374
361375 if ( isSideEffectImport ( node ) ) {
362376 defineGroup ( 'side-effect' )
363377 }
364378
365- if ( isStyle ( node . source . value ) ) {
379+ if ( isStyle ( value ) ) {
366380 defineGroup ( 'style' )
367381 }
368382
369- if ( isIndex ( node . source . value ) ) {
383+ if ( isIndex ( value ) ) {
370384 defineGroup ( 'index' )
371385 }
372386
373- if ( isSibling ( node . source . value ) ) {
387+ if ( isSibling ( value ) ) {
374388 defineGroup ( 'sibling' )
375389 }
376390
377- if ( isParent ( node . source . value ) ) {
391+ if ( isParent ( value ) ) {
378392 defineGroup ( 'parent' )
379393 }
380394
381- if ( isInternal ( node ) ) {
395+ if ( isInternal ( value ) ) {
382396 defineGroup ( 'internal' )
383397 }
384398
385- if ( isCoreModule ( node . source . value ) ) {
399+ if ( isCoreModule ( value ) ) {
386400 defineGroup ( 'builtin' )
387401 }
388402
389- if ( isExternal ( node . source . value ) ) {
403+ if ( isExternal ( value ) ) {
390404 defineGroup ( 'external' )
391405 }
392406 }
@@ -398,17 +412,25 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
398412 node : TSESTree . ImportDeclaration ,
399413 ) : boolean => node . specifiers . length > 1
400414
401- let registerNode = ( node : ModuleDeclaration ) => {
415+ let registerNode = (
416+ node :
417+ | TSESTree . TSImportEqualsDeclaration
418+ | TSESTree . VariableDeclaration
419+ | TSESTree . ImportDeclaration ,
420+ ) => {
402421 let name : string
403422
404423 if ( node . type === 'ImportDeclaration' ) {
405424 name = node . source . value
406- } else {
425+ } else if ( node . type === 'TSImportEqualsDeclaration' ) {
407426 if ( node . moduleReference . type === 'TSExternalModuleReference' ) {
408427 name = `${ node . moduleReference . expression . value } `
409428 } else {
410429 name = sourceCode . text . slice ( ...node . moduleReference . range )
411430 }
431+ } else {
432+ let decl = node . declarations [ 0 ] . init as TSESTree . CallExpression
433+ name = ( decl . arguments [ 0 ] as TSESTree . Literal ) . value ! . toString ( )
412434 }
413435
414436 nodes . push ( {
@@ -428,6 +450,16 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
428450 return {
429451 TSImportEqualsDeclaration : registerNode ,
430452 ImportDeclaration : registerNode ,
453+ VariableDeclaration : node => {
454+ if (
455+ node . declarations [ 0 ] . init &&
456+ node . declarations [ 0 ] . init . type === 'CallExpression' &&
457+ node . declarations [ 0 ] . init . callee . type === 'Identifier' &&
458+ node . declarations [ 0 ] . init . callee . name === 'require'
459+ ) {
460+ registerNode ( node )
461+ }
462+ } ,
431463 'Program:exit' : ( ) => {
432464 let hasContentBetweenNodes = (
433465 left : SortingNode ,
0 commit comments