@@ -15,6 +15,7 @@ export type Options = PluginPass & {
15
15
memo ?: Import
16
16
}
17
17
useComputed ?: Import
18
+ debug ?: boolean
18
19
memoWithChildren ?: boolean
19
20
wrapObserverOnlyIfGet ?: boolean
20
21
dataComponent ?: boolean
@@ -134,10 +135,93 @@ const useMemoVisitor: PluginObj<Options> = {
134
135
} ,
135
136
}
136
137
138
+ const debugComponentTraverse = ( p : NodePath , state : Options , component : string ) => {
139
+ if ( ! state . opts . debug ) {
140
+ return
141
+ }
142
+ const DETECT_STRING = 'component'
143
+ const REPLACE_COMMENT = 'used'
144
+ p . traverse ( {
145
+ enter ( p ) {
146
+ let innerComment = p . node . innerComments ?. filter (
147
+ ( c ) => c . type === 'CommentBlock' && c . value . trim ( ) === DETECT_STRING
148
+ ) [ 0 ]
149
+ if ( innerComment ) {
150
+ if ( p . isCallExpression ( ) ) {
151
+ p . node . arguments . push ( t . stringLiteral ( component ) )
152
+ innerComment . value = REPLACE_COMMENT
153
+ }
154
+ } else {
155
+ let trailingComment = p . node . trailingComments ?. filter (
156
+ ( c ) => c . type === 'CommentBlock' && c . value . trim ( ) === DETECT_STRING
157
+ ) [ 0 ]
158
+ if ( trailingComment ) {
159
+ if ( p . parentPath ?. isCallExpression ( ) ) {
160
+ p . parentPath . node . arguments . push ( t . stringLiteral ( component ) )
161
+ trailingComment . value = REPLACE_COMMENT
162
+ }
163
+ }
164
+ }
165
+ } ,
166
+ } )
167
+ }
168
+
137
169
// wrap max 0-1 wrapped components
138
170
const componentVisitor : PluginObj < Options > = {
139
171
visitor : {
140
172
VariableDeclaration ( p , state ) {
173
+ // p.replaceWithSourceString(p.getSource())
174
+ // let comments = state.file.ast.comments
175
+
176
+ // console.log({ comments: state.file.ast.comments })
177
+ // if (comments) {
178
+ // comments.forEach((comment) => {
179
+ // if (comment.type === 'CommentBlock') {
180
+ // if (comment.value.trim() === 'yyy') {
181
+ // comment.value = 'xxx'
182
+ // if (comment.end && comment.start) {
183
+ // comment.end = comment.start + 2
184
+ // }
185
+ // console.log('----------------------', {
186
+ // 'comment.end': comment.end,
187
+ // 'comment.start': comment.start,
188
+ // 'comment.loc': comment.loc,
189
+ // })
190
+ // if (comment.loc) {
191
+ // // comment.loc.start.column = comment.loc.start.column + 5
192
+ // // comment.loc.start.line = comment.loc.start.line +5
193
+ // comment.loc.end.column = 70
194
+ // // comment.loc.end.line = comment.loc.start.line
195
+ // }
196
+ // }
197
+ // }
198
+ // })
199
+ // }
200
+
201
+ // if (state.file.ast.comments) {
202
+ // state.file.ast.comments = state.file.ast.comments.map((comment: any) => {
203
+ // if (comment.type === 'CommentBlock') {
204
+ // console.log({ comment: comment })
205
+ // if (comment.value.trim() === 'yyy') {
206
+ // return {
207
+ // type: 'CommentBlock',
208
+ // value: 'xxx',
209
+ // start: comment.start,
210
+ // end: comment.start + 5, // 'xxx' + '/*' + '*/'
211
+ // loc: {
212
+ // start: comment.loc.start,
213
+ // end: {
214
+ // line: comment.loc.start.line,
215
+ // column: comment.loc.start.column + 5,
216
+ // },
217
+ // },
218
+ // }
219
+ // }
220
+ // }
221
+ // return comment
222
+ // })
223
+ // }
224
+
141
225
const declarations = p . get ( 'declarations' )
142
226
const declarator = declarations [ 0 ]
143
227
if ( declarator ) {
@@ -158,6 +242,7 @@ const componentVisitor: PluginObj<Options> = {
158
242
if ( t . isIdentifier ( declarator . node . id ) ) {
159
243
if ( isComponent ( declarator . node . id . name ) ) {
160
244
const componentName = declarator . node . id . name
245
+ debugComponentTraverse ( arrowFunction , state , componentName )
161
246
const wrap = ( { importName, importSource } : Import ) => {
162
247
let actualImport = state . actualImports . get ( importName )
163
248
if ( ! actualImport ) {
@@ -270,6 +355,7 @@ const plugin = (): PluginObj<Options> => {
270
355
if ( ! filterFiles ( file ) ) {
271
356
return
272
357
}
358
+
273
359
if ( ! logFinished && file . opts . filename ) {
274
360
if ( ! filesEnteredMap . get ( file . opts . filename ) ) {
275
361
filesEnteredMap . set ( file . opts . filename , true )
0 commit comments