@@ -6,6 +6,10 @@ import resolveExportDeclaration from './resolveExportDeclaration'
6
6
// tslint:disable-next-line:no-var-requires
7
7
import recast = require( 'recast' )
8
8
9
+ function ignore ( ) : boolean {
10
+ return false
11
+ }
12
+
9
13
function isComponentDefinition ( path : NodePath ) : boolean {
10
14
return (
11
15
// export default {}
@@ -54,8 +58,9 @@ export default function resolveExportedComponent(ast: bt.File): NodePath[] {
54
58
// in extenso export default or export myvar
55
59
function exportDeclaration ( path : NodePath ) {
56
60
const definitions = resolveExportDeclaration ( path ) . reduce ( ( acc : NodePath [ ] , definition ) => {
57
- if ( isComponentDefinition ( definition ) ) {
58
- acc . push ( definition )
61
+ const realDef = resolveIdentifier ( ast , definition )
62
+ if ( realDef && isComponentDefinition ( realDef ) ) {
63
+ acc . push ( realDef )
59
64
}
60
65
return acc
61
66
} , [ ] )
@@ -67,6 +72,20 @@ export default function resolveExportedComponent(ast: bt.File): NodePath[] {
67
72
}
68
73
69
74
recast . visit ( ast . program , {
75
+ // to look only at the root we ignore all traversing
76
+ visitFunctionDeclaration : ignore ,
77
+ visitFunctionExpression : ignore ,
78
+ visitClassDeclaration : ignore ,
79
+ visitClassExpression : ignore ,
80
+ visitIfStatement : ignore ,
81
+ visitWithStatement : ignore ,
82
+ visitSwitchStatement : ignore ,
83
+ visitCatchCause : ignore ,
84
+ visitWhileStatement : ignore ,
85
+ visitDoWhileStatement : ignore ,
86
+ visitForStatement : ignore ,
87
+ visitForInStatement : ignore ,
88
+
70
89
visitDeclareExportDeclaration : exportDeclaration ,
71
90
visitExportNamedDeclaration : exportDeclaration ,
72
91
visitExportDefaultDeclaration : exportDeclaration ,
@@ -83,11 +102,12 @@ export default function resolveExportedComponent(ast: bt.File): NodePath[] {
83
102
// Resolve the value of the right hand side. It should resolve to a call
84
103
// expression, something like Vue.extend({})
85
104
const pathRight = path . get ( 'right' )
86
- if ( ! isComponentDefinition ( pathRight ) ) {
105
+ const realComp = resolveIdentifier ( ast , pathRight )
106
+ if ( ! realComp || ! isComponentDefinition ( realComp ) ) {
87
107
return false
88
108
}
89
109
90
- setComponent ( pathRight )
110
+ setComponent ( realComp )
91
111
return false
92
112
} ,
93
113
} )
@@ -105,3 +125,48 @@ function normalizeComponentPath(path: NodePath): NodePath {
105
125
}
106
126
return path
107
127
}
128
+
129
+ function resolveIdentifier ( ast : bt . File , path : NodePath ) : NodePath | null {
130
+ if ( ! bt . isIdentifier ( path . node ) ) {
131
+ return path
132
+ }
133
+
134
+ const varName = path . node . name
135
+ let comp : NodePath | null = null
136
+
137
+ recast . visit ( ast . program , {
138
+ // to look only at the root we ignore all traversing
139
+ visitFunctionDeclaration : ignore ,
140
+ visitFunctionExpression : ignore ,
141
+ visitClassExpression : ignore ,
142
+ visitIfStatement : ignore ,
143
+ visitWithStatement : ignore ,
144
+ visitSwitchStatement : ignore ,
145
+ visitCatchCause : ignore ,
146
+ visitWhileStatement : ignore ,
147
+ visitDoWhileStatement : ignore ,
148
+ visitForStatement : ignore ,
149
+ visitForInStatement : ignore ,
150
+
151
+ visitVariableDeclaration ( variablePath : NodePath < bt . VariableDeclaration > ) {
152
+ const varID = variablePath . node . declarations [ 0 ] . id
153
+ if ( ! varID || ! bt . isIdentifier ( varID ) || varID . name !== varName ) {
154
+ return false
155
+ }
156
+
157
+ comp = variablePath . get ( 'declarations' , 0 ) . get ( 'init' )
158
+ return false
159
+ } ,
160
+
161
+ visitClassDeclaration ( classPath : NodePath < bt . ClassDeclaration > ) {
162
+ const classID = classPath . node . id
163
+ if ( ! classID || ! bt . isIdentifier ( classID ) || classID . name !== varName ) {
164
+ return false
165
+ }
166
+
167
+ comp = classPath
168
+ return false
169
+ } ,
170
+ } )
171
+ return comp
172
+ }
0 commit comments