@@ -3,6 +3,44 @@ import Exports from '../ExportMap';
33import importDeclaration from '../importDeclaration' ;
44import docsUrl from '../docsUrl' ;
55
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+
644module . exports = {
745 meta : {
846 type : 'problem' ,
@@ -41,44 +79,7 @@ module.exports = {
4179 return {
4280 // pick up all imports at body entry time, to properly respect hoisting
4381 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 ) ) ;
8283 } ,
8384
8485 // same as above, but does not add names to local map
@@ -120,7 +121,6 @@ module.exports = {
120121 const namepath = [ dereference . object . name ] ;
121122 // while property is namespace and parent is member expression, keep validating
122123 while ( namespace instanceof Exports && dereference . type === 'MemberExpression' ) {
123-
124124 if ( dereference . computed ) {
125125 if ( ! allowComputed ) {
126126 context . report (
@@ -147,7 +147,6 @@ module.exports = {
147147 namespace = exported . namespace ;
148148 dereference = dereference . parent ;
149149 }
150-
151150 } ,
152151
153152 VariableDeclarator ( { id, init } ) {
0 commit comments