@@ -31,104 +31,17 @@ import {
3131 Range ,
3232} from '../types' ;
3333
34- type ExportDeclarationsWithFunctions = ExportNamedDeclaration | ExportDefaultDeclaration ;
35-
36- function functionDeclarationName (
37- context : PluginContext ,
38- declaration : ExportDeclarationsWithFunctions ,
39- ) : string | null {
40- // For the Declaration passed, there can be a function declaration.
41- if ( declaration . declaration && declaration . declaration . type === 'FunctionDeclaration' ) {
42- const functionDeclaration = declaration . declaration ;
43-
44- if (
45- functionDeclaration !== null &&
46- functionDeclaration . id !== null &&
47- functionDeclaration . id . name !== null
48- ) {
49- return functionDeclaration . id . name ;
50- }
51- }
52-
53- return null ;
54- }
55-
56- function classDeclarationName (
57- context : PluginContext ,
58- declaration : ExportDeclarationsWithFunctions ,
59- ) : string | null {
60- // For the Declaration passed, there can be a function declaration.
61- if ( declaration . declaration && declaration . declaration . type === 'ClassDeclaration' ) {
62- const classDeclaration = declaration . declaration ;
63-
64- if (
65- classDeclaration !== null &&
66- classDeclaration . id !== null &&
67- classDeclaration . id . name !== null
68- ) {
69- // This class declaration is the export name we need to know.
70- return classDeclaration . id . name ;
71- }
72- }
73-
74- return null ;
75- }
76-
7734export function NamedDeclaration (
7835 context : PluginContext ,
7936 declaration : ExportNamedDeclaration ,
8037) : Array < ExportDetails > {
81- const functionName = functionDeclarationName ( context , declaration ) ;
82- const className = classDeclarationName ( context , declaration ) ;
8338 const range : Range = declaration . range as Range ;
8439 const source : string | null =
8540 declaration . source && declaration . source . value && typeof declaration . source . value === 'string'
8641 ? declaration . source . value
8742 : null ;
8843
89- // TODO(KB): This logic isn't great. If something has a named declaration, lets instead use the AST to find out what it is.
90- // var Foo=function(){}export{Foo as default} => default export function
91-
92- if ( functionName !== null ) {
93- return [
94- {
95- local : functionName ,
96- exported : functionName ,
97- closureName : functionName ,
98- type : ExportClosureMapping . NAMED_FUNCTION ,
99- range,
100- source,
101- } ,
102- ] ;
103- } else if ( className !== null ) {
104- return [
105- {
106- local : className ,
107- exported : className ,
108- closureName : className ,
109- type : ExportClosureMapping . NAMED_CLASS ,
110- range,
111- source,
112- } ,
113- ] ;
114- } else if ( declaration . declaration && declaration . declaration . type === 'VariableDeclaration' ) {
115- const { declarations } = declaration . declaration ;
116- const exportDetails : Array < ExportDetails > = [ ] ;
117-
118- for ( const declarator of declarations ) {
119- if ( declarator . id . type === 'Identifier' ) {
120- exportDetails . push ( {
121- local : declarator . id . name ,
122- exported : declarator . id . name ,
123- closureName : declarator . id . name ,
124- type : ExportClosureMapping . NAMED_CONSTANT ,
125- range,
126- source,
127- } ) ;
128- }
129- }
130- return exportDetails ;
131- } else if ( declaration . specifiers ) {
44+ if ( declaration . specifiers ) {
13245 const exportDetails : Array < ExportDetails > = [ ] ;
13346
13447 for ( const specifier of declaration . specifiers ) {
@@ -155,51 +68,17 @@ export function DefaultDeclaration(
15568 const range : Range = declaration . range as Range ;
15669 const source = null ;
15770
158- switch ( declaration . declaration . type ) {
159- case 'FunctionDeclaration' :
160- const functionName = functionDeclarationName ( context , declaration ) ;
161- if ( functionName !== null ) {
162- return [
163- {
164- local : functionName ,
165- exported : functionName ,
166- closureName : functionName ,
167- type : ExportClosureMapping . NAMED_DEFAULT_FUNCTION ,
168- range,
169- source,
170- } ,
171- ] ;
172- }
173- break ;
174- case 'ClassDeclaration' :
175- const className = classDeclarationName ( context , declaration ) ;
176- if ( className !== null ) {
177- return [
178- {
179- local : className ,
180- exported : className ,
181- closureName : className ,
182- type : ExportClosureMapping . NAMED_DEFAULT_FUNCTION ,
183- range,
184- source,
185- } ,
186- ] ;
187- }
188- break ;
189- case 'Identifier' :
190- if ( declaration . declaration . name ) {
191- return [
192- {
193- local : declaration . declaration . name ,
194- exported : declaration . declaration . name ,
195- closureName : declaration . declaration . name ,
196- type : ExportClosureMapping . NAMED_DEFAULT_FUNCTION ,
197- range,
198- source,
199- } ,
200- ] ;
201- }
202- break ;
71+ if ( declaration . declaration . type === 'Identifier' && declaration . declaration . name ) {
72+ return [
73+ {
74+ local : declaration . declaration . name ,
75+ exported : declaration . declaration . name ,
76+ closureName : declaration . declaration . name ,
77+ type : ExportClosureMapping . NAMED_DEFAULT_FUNCTION ,
78+ range,
79+ source,
80+ } ,
81+ ] ;
20382 }
20483 }
20584
@@ -224,21 +103,14 @@ export function importLocalNames(
224103 context : PluginContext ,
225104 declaration : ImportDeclaration ,
226105) : Array < string > {
106+ const VALID_SPECIFIERS = [ IMPORT_SPECIFIER , IMPORT_NAMESPACE_SPECIFIER , IMPORT_DEFAULT_SPECIFIER ] ;
227107 const returnableSpecifiers : Array < string > = [ ] ;
228108
229- if ( declaration . specifiers ) {
230- declaration . specifiers . forEach ( specifier => {
231- switch ( specifier . type ) {
232- case IMPORT_SPECIFIER :
233- case IMPORT_NAMESPACE_SPECIFIER :
234- case IMPORT_DEFAULT_SPECIFIER :
235- returnableSpecifiers . push ( specifier . local . name ) ;
236- break ;
237- default :
238- break ;
239- }
240- } ) ;
241- }
109+ ( declaration . specifiers || [ ] ) . forEach ( specifier => {
110+ if ( VALID_SPECIFIERS . includes ( specifier . type ) ) {
111+ returnableSpecifiers . push ( specifier . local . name ) ;
112+ }
113+ } ) ;
242114
243115 return returnableSpecifiers ;
244116}
0 commit comments