@@ -14,37 +14,43 @@ module.exports = function (context) {
14
14
15
15
return {
16
16
17
- 'ImportDeclaration' : function ( declaration ) {
18
- if ( declaration . specifiers . length === 0 ) return
17
+ // pick up all imports at body entry time, to properly respect hoisting
18
+ 'Program' : function ( { body } ) {
19
+ function processBodyStatement ( declaration ) {
20
+ if ( declaration . type !== 'ImportDeclaration' ) return
19
21
20
- const imports = Exports . get ( declaration . source . value , context )
21
- if ( imports == null ) return null
22
+ if ( declaration . specifiers . length === 0 ) return
22
23
23
- if ( imports . errors . length ) {
24
- imports . reportErrors ( context , declaration )
25
- return
26
- }
24
+ const imports = Exports . get ( declaration . source . value , context )
25
+ if ( imports == null ) return null
26
+
27
+ if ( imports . errors . length ) {
28
+ imports . reportErrors ( context , declaration )
29
+ return
30
+ }
27
31
28
- for ( let specifier of declaration . specifiers ) {
29
- switch ( specifier . type ) {
30
- case 'ImportNamespaceSpecifier' :
31
- if ( ! imports . hasNamed ) {
32
- context . report ( specifier ,
33
- `No exported names found in module '${ declaration . source . value } '.` )
32
+ for ( let specifier of declaration . specifiers ) {
33
+ switch ( specifier . type ) {
34
+ case 'ImportNamespaceSpecifier' :
35
+ if ( ! imports . hasNamed ) {
36
+ context . report ( specifier ,
37
+ `No exported names found in module '${ declaration . source . value } '.` )
38
+ }
39
+ namespaces . set ( specifier . local . name , imports . named )
40
+ break
41
+ case 'ImportDefaultSpecifier' :
42
+ case 'ImportSpecifier' : {
43
+ const meta = imports . named . get (
44
+ // default to 'default' for default http://i.imgur.com/nj6qAWy.jpg
45
+ specifier . imported ? specifier . imported . name : 'default' )
46
+ if ( ! meta || ! meta . namespace ) break
47
+ namespaces . set ( specifier . local . name , meta . namespace )
48
+ break
34
49
}
35
- namespaces . set ( specifier . local . name , imports . named )
36
- break
37
- case 'ImportDefaultSpecifier' :
38
- case 'ImportSpecifier' : {
39
- const meta = imports . named . get (
40
- // default to 'default' for default http://i.imgur.com/nj6qAWy.jpg
41
- specifier . imported ? specifier . imported . name : 'default' )
42
- if ( ! meta || ! meta . namespace ) break
43
- namespaces . set ( specifier . local . name , meta . namespace )
44
- break
45
50
}
46
51
}
47
52
}
53
+ body . forEach ( processBodyStatement )
48
54
} ,
49
55
50
56
// same as above, but does not add names to local map
0 commit comments