@@ -3,6 +3,44 @@ import Exports from '../ExportMap';
3
3
import importDeclaration from '../importDeclaration' ;
4
4
import docsUrl from '../docsUrl' ;
5
5
6
+ function processBodyStatement ( context , namespaces , declaration ) {
7
+ if ( declaration . type !== 'ImportDeclaration' ) return ;
8
+
9
+ if ( declaration . specifiers . length === 0 ) return ;
10
+
11
+ const imports = Exports . get ( declaration . source . value , context ) ;
12
+ if ( imports == null ) return null ;
13
+
14
+ if ( imports . errors . length > 0 ) {
15
+ imports . reportErrors ( context , declaration ) ;
16
+ return ;
17
+ }
18
+
19
+ declaration . specifiers . forEach ( ( specifier ) => {
20
+ switch ( specifier . type ) {
21
+ case 'ImportNamespaceSpecifier' :
22
+ if ( ! imports . size ) {
23
+ context . report (
24
+ specifier ,
25
+ `No exported names found in module '${ declaration . source . value } '.` ,
26
+ ) ;
27
+ }
28
+ namespaces . set ( specifier . local . name , imports ) ;
29
+ break ;
30
+ case 'ImportDefaultSpecifier' :
31
+ case 'ImportSpecifier' : {
32
+ const meta = imports . get (
33
+ // default to 'default' for default https://i.imgur.com/nj6qAWy.jpg
34
+ specifier . imported ? ( specifier . imported . name || specifier . imported . value ) : 'default' ,
35
+ ) ;
36
+ if ( ! meta || ! meta . namespace ) { break ; }
37
+ namespaces . set ( specifier . local . name , meta . namespace ) ;
38
+ break ;
39
+ }
40
+ }
41
+ } ) ;
42
+ }
43
+
6
44
module . exports = {
7
45
meta : {
8
46
type : 'problem' ,
@@ -41,44 +79,7 @@ module.exports = {
41
79
return {
42
80
// pick up all imports at body entry time, to properly respect hoisting
43
81
Program ( { body } ) {
44
- function processBodyStatement ( declaration ) {
45
- if ( declaration . type !== 'ImportDeclaration' ) return ;
46
-
47
- if ( declaration . specifiers . length === 0 ) return ;
48
-
49
- const imports = Exports . get ( declaration . source . value , context ) ;
50
- if ( imports == null ) return null ;
51
-
52
- if ( imports . errors . length ) {
53
- imports . reportErrors ( context , declaration ) ;
54
- return ;
55
- }
56
-
57
- for ( const specifier of declaration . specifiers ) {
58
- switch ( specifier . type ) {
59
- case 'ImportNamespaceSpecifier' :
60
- if ( ! imports . size ) {
61
- context . report (
62
- specifier ,
63
- `No exported names found in module '${ declaration . source . value } '.` ,
64
- ) ;
65
- }
66
- namespaces . set ( specifier . local . name , imports ) ;
67
- break ;
68
- case 'ImportDefaultSpecifier' :
69
- case 'ImportSpecifier' : {
70
- const meta = imports . get (
71
- // default to 'default' for default https://i.imgur.com/nj6qAWy.jpg
72
- specifier . imported ? ( specifier . imported . name || specifier . imported . value ) : 'default' ,
73
- ) ;
74
- if ( ! meta || ! meta . namespace ) { break ; }
75
- namespaces . set ( specifier . local . name , meta . namespace ) ;
76
- break ;
77
- }
78
- }
79
- }
80
- }
81
- body . forEach ( processBodyStatement ) ;
82
+ body . forEach ( x => processBodyStatement ( context , namespaces , x ) ) ;
82
83
} ,
83
84
84
85
// same as above, but does not add names to local map
@@ -120,7 +121,6 @@ module.exports = {
120
121
const namepath = [ dereference . object . name ] ;
121
122
// while property is namespace and parent is member expression, keep validating
122
123
while ( namespace instanceof Exports && dereference . type === 'MemberExpression' ) {
123
-
124
124
if ( dereference . computed ) {
125
125
if ( ! allowComputed ) {
126
126
context . report (
@@ -147,7 +147,6 @@ module.exports = {
147
147
namespace = exported . namespace ;
148
148
dereference = dereference . parent ;
149
149
}
150
-
151
150
} ,
152
151
153
152
VariableDeclarator ( { id, init } ) {
0 commit comments