@@ -29,63 +29,78 @@ module.exports = {
2929 } ,
3030
3131 create ( context ) {
32+ const importsWithoutNameds = [ ] ;
33+
3234 return {
33- Program ( node ) {
34- node . tokens . forEach ( ( token , idx ) => {
35- const nextToken = node . tokens [ idx + 1 ] ;
35+ ImportDeclaration ( node ) {
36+ if ( ! node . specifiers . some ( x => x . type === 'ImportSpecifier' ) ) {
37+ importsWithoutNameds . push ( node ) ;
38+ }
39+ } ,
40+
41+ 'Program:exit' : function ( program ) {
42+ const importsTokens = importsWithoutNameds . map ( ( node ) => {
43+ return [ node , program . tokens . filter ( x => x . range [ 0 ] >= node . range [ 0 ] && x . range [ 1 ] <= node . range [ 1 ] ) ] ;
44+ } ) ;
45+
46+ importsTokens . forEach ( ( [ node , tokens ] ) => {
47+ tokens . forEach ( ( token ) => {
48+ const idx = program . tokens . indexOf ( token ) ;
49+ const nextToken = program . tokens [ idx + 1 ] ;
3650
37- if ( nextToken && token . value === '{' && nextToken . value === '}' ) {
38- const hasOtherIdentifiers = node . tokens . some ( ( token ) => (
39- token . type === 'Identifier'
40- && token . value !== 'from'
41- && token . value !== 'type'
42- && token . value !== 'typeof'
43- ) ) ;
51+ if ( nextToken && token . value === '{' && nextToken . value === '}' ) {
52+ const hasOtherIdentifiers = tokens . some ( ( token ) => (
53+ token . type === 'Identifier'
54+ && token . value !== 'from'
55+ && token . value !== 'type'
56+ && token . value !== 'typeof'
57+ ) ) ;
4458
45- // If it has no other identifiers it's the only thing in the import, so we can either remove the import
46- // completely or transform it in a side-effects only import
47- if ( ! hasOtherIdentifiers ) {
48- context . report ( {
49- node,
50- message : 'Unexpected empty named import block' ,
51- suggest : [
52- {
53- desc : 'Remove unused import' ,
54- fix ( fixer ) {
55- // Remove the whole import
56- return fixer . remove ( node ) ;
59+ // If it has no other identifiers it's the only thing in the import, so we can either remove the import
60+ // completely or transform it in a side-effects only import
61+ if ( ! hasOtherIdentifiers ) {
62+ context . report ( {
63+ node,
64+ message : 'Unexpected empty named import block' ,
65+ suggest : [
66+ {
67+ desc : 'Remove unused import' ,
68+ fix ( fixer ) {
69+ // Remove the whole import
70+ return fixer . remove ( node ) ;
71+ } ,
5772 } ,
58- } ,
59- {
60- desc : 'Remove empty import block' ,
61- fix ( fixer ) {
62- // Remove the empty block and the 'from' token, leaving the import only for its side
63- // effects, e.g. `import 'mod'`
64- const sourceCode = context . getSourceCode ( ) ;
65- const fromToken = node . tokens . find ( t => t . value === 'from' ) ;
66- const importToken = node . tokens . find ( t => t . value === 'import' ) ;
67- const hasSpaceAfterFrom = sourceCode . isSpaceBetween ( fromToken , sourceCode . getTokenAfter ( fromToken ) ) ;
68- const hasSpaceAfterImport = sourceCode . isSpaceBetween ( importToken , sourceCode . getTokenAfter ( fromToken ) ) ;
73+ {
74+ desc : 'Remove empty import block' ,
75+ fix ( fixer ) {
76+ // Remove the empty block and the 'from' token, leaving the import only for its side
77+ // effects, e.g. `import 'mod'`
78+ const sourceCode = context . getSourceCode ( ) ;
79+ const fromToken = program . tokens . find ( t => t . value === 'from' ) ;
80+ const importToken = program . tokens . find ( t => t . value === 'import' ) ;
81+ const hasSpaceAfterFrom = sourceCode . isSpaceBetween ( fromToken , sourceCode . getTokenAfter ( fromToken ) ) ;
82+ const hasSpaceAfterImport = sourceCode . isSpaceBetween ( importToken , sourceCode . getTokenAfter ( fromToken ) ) ;
6983
70- const [ start ] = getEmptyBlockRange ( node . tokens , idx ) ;
71- const [ , end ] = fromToken . range ;
72- const range = [ start , hasSpaceAfterFrom ? end + 1 : end ] ;
84+ const [ start ] = getEmptyBlockRange ( program . tokens , idx ) ;
85+ const [ , end ] = fromToken . range ;
86+ const range = [ start , hasSpaceAfterFrom ? end + 1 : end ] ;
7387
74- return fixer . replaceTextRange ( range , hasSpaceAfterImport ? '' : ' ' ) ;
88+ return fixer . replaceTextRange ( range , hasSpaceAfterImport ? '' : ' ' ) ;
89+ } ,
7590 } ,
91+ ] ,
92+ } ) ;
93+ } else {
94+ context . report ( {
95+ node,
96+ message : 'Unexpected empty named import block' ,
97+ fix ( fixer ) {
98+ return fixer . removeRange ( getEmptyBlockRange ( program . tokens , idx ) ) ;
7699 } ,
77- ] ,
78- } ) ;
79- } else {
80- context . report ( {
81- node,
82- message : 'Unexpected empty named import block' ,
83- fix ( fixer ) {
84- return fixer . removeRange ( getEmptyBlockRange ( node . tokens , idx ) ) ;
85- } ,
86- } ) ;
100+ } ) ;
101+ }
87102 }
88- }
103+ } ) ;
89104 } ) ;
90105 } ,
91106 } ;
0 commit comments