@@ -16,6 +16,7 @@ import { visitor as migrate } from "./plugins/migrate";
16
16
import { visitor as transform } from "./plugins/transform" ;
17
17
18
18
const SOURCE_FILES = new WeakMap ( ) ;
19
+ let currentFile ;
19
20
20
21
export default ( api , markoOpts ) => {
21
22
api . assertVersion ( 7 ) ;
@@ -61,22 +62,18 @@ export default (api, markoOpts) => {
61
62
curOpts = opts ;
62
63
} ,
63
64
parserOverride ( code ) {
64
- const prevFS = taglibConfig . fs ;
65
- taglibConfig . fs = markoOpts . fileSystem ;
66
- try {
67
- const file = getMarkoFile ( code , curOpts , markoOpts ) ;
68
- const finalAst = t . cloneNode ( file . ast , true ) ;
69
- SOURCE_FILES . set ( finalAst , file ) ;
70
- return finalAst ;
71
- } finally {
72
- taglibConfig . fs = prevFS ;
73
- }
65
+ const file = getMarkoFile ( code , curOpts , markoOpts ) ;
66
+ const finalAst = t . cloneNode ( file . ast , true ) ;
67
+ SOURCE_FILES . set ( finalAst , file ) ;
68
+ return finalAst ;
74
69
} ,
75
70
pre ( file ) {
76
71
const { buildError : prevBuildError } = file . hub ;
77
72
const { buildCodeFrameError : prevCodeFrameError } = file ;
78
73
const prevFS = taglibConfig . fs ;
74
+ const prevFile = currentFile ;
79
75
taglibConfig . fs = markoOpts . fileSystem ;
76
+ currentFile = file ;
80
77
curOpts = undefined ;
81
78
try {
82
79
const { ast, metadata } = file ;
@@ -114,6 +111,7 @@ export default (api, markoOpts) => {
114
111
file . path . scope . crawl ( ) ; // Ensure all scopes are accurate for subsequent babel plugins
115
112
} finally {
116
113
taglibConfig . fs = prevFS ;
114
+ currentFile = prevFile ;
117
115
file . buildCodeFrameError = prevCodeFrameError ;
118
116
file . hub . buildError = prevBuildError ;
119
117
file . markoOpts =
@@ -147,7 +145,23 @@ export default (api, markoOpts) => {
147
145
} ;
148
146
} ;
149
147
150
- export function getMarkoFile ( code , fileOpts , markoOpts ) {
148
+ export function getFile ( ) {
149
+ if ( currentFile ) {
150
+ return currentFile ;
151
+ }
152
+
153
+ throw new Error ( "Unable to access Marko File outside of a compilation" ) ;
154
+ }
155
+
156
+ export function getProgram ( ) {
157
+ if ( currentFile ) {
158
+ return currentFile . path ;
159
+ }
160
+
161
+ throw new Error ( "Unable to access Marko Program outside of a compilation" ) ;
162
+ }
163
+
164
+ function getMarkoFile ( code , fileOpts , markoOpts ) {
151
165
const { translator } = markoOpts ;
152
166
let compileCache = markoOpts . cache . get ( translator ) ;
153
167
@@ -191,129 +205,137 @@ export function getMarkoFile(code, fileOpts, markoOpts) {
191
205
return cached . file ;
192
206
}
193
207
194
- const taglibLookup = buildLookup ( path . dirname ( filename ) , translator ) ;
195
-
196
- const file = new MarkoFile ( fileOpts , {
197
- code,
198
- ast : {
199
- type : "File" ,
200
- program : {
201
- type : "Program" ,
202
- sourceType : "module" ,
203
- body : [ ] ,
204
- directives : [ ] ,
205
- params : [ t . identifier ( "input" ) ] ,
208
+ const prevFs = taglibConfig . fs ;
209
+ const prevFile = currentFile ;
210
+ taglibConfig . fs = markoOpts . fileSystem ;
211
+
212
+ try {
213
+ const taglibLookup = buildLookup ( path . dirname ( filename ) , translator ) ;
214
+ const file = ( currentFile = new MarkoFile ( fileOpts , {
215
+ code,
216
+ ast : {
217
+ type : "File" ,
218
+ program : {
219
+ type : "Program" ,
220
+ sourceType : "module" ,
221
+ body : [ ] ,
222
+ directives : [ ] ,
223
+ params : [ t . identifier ( "input" ) ] ,
224
+ } ,
206
225
} ,
207
- } ,
208
- } ) ;
226
+ } ) ) ;
209
227
210
- const meta = ( file . metadata . marko = {
211
- id,
212
- deps : [ ] ,
213
- tags : [ ] ,
214
- watchFiles : [ ] ,
215
- diagnostics : [ ] ,
216
- } ) ;
228
+ const meta = ( file . metadata . marko = {
229
+ id,
230
+ deps : [ ] ,
231
+ tags : [ ] ,
232
+ watchFiles : [ ] ,
233
+ diagnostics : [ ] ,
234
+ } ) ;
217
235
218
- file . markoOpts = markoOpts ;
219
- file . ___taglibLookup = taglibLookup ;
220
- file . ___getMarkoFile = getMarkoFile ;
236
+ file . markoOpts = markoOpts ;
237
+ file . ___taglibLookup = taglibLookup ;
238
+ file . ___getMarkoFile = getMarkoFile ;
221
239
222
- file . ___compileStage = "parse" ;
223
- parseMarko ( file ) ;
240
+ file . ___compileStage = "parse" ;
241
+ parseMarko ( file ) ;
224
242
225
- if ( isSource ) {
226
- return file ;
227
- }
243
+ if ( isSource ) {
244
+ return file ;
245
+ }
228
246
229
- file . path . scope . crawl ( ) ; // Initialize bindings.
247
+ file . path . scope . crawl ( ) ; // Initialize bindings.
230
248
231
- const rootMigrators = [ ] ;
232
- for ( const id in taglibLookup . taglibsById ) {
233
- for ( const migrator of taglibLookup . taglibsById [ id ] . migrators ) {
234
- addPlugin ( meta , rootMigrators , migrator ) ;
249
+ const rootMigrators = [ ] ;
250
+ for ( const id in taglibLookup . taglibsById ) {
251
+ for ( const migrator of taglibLookup . taglibsById [ id ] . migrators ) {
252
+ addPlugin ( meta , rootMigrators , migrator ) ;
253
+ }
235
254
}
236
- }
237
255
238
- rootMigrators . push ( migrate ) ;
239
- file . ___compileStage = "migrate" ;
240
- traverseAll ( file , rootMigrators ) ;
241
-
242
- if ( file . ___hasParseErrors ) {
243
- if ( markoOpts . errorRecovery ) {
244
- t . traverseFast ( file . path . node , ( node ) => {
245
- if ( node . type === "MarkoParseError" ) {
246
- diagnosticError ( file . path , {
247
- label : node . label ,
248
- loc : node . errorLoc || node . loc ,
249
- } ) ;
250
- }
251
- } ) ;
252
- } else {
253
- let errors = [ ] ;
254
- t . traverseFast ( file . path . node , ( node ) => {
255
- if ( node . type === "MarkoParseError" ) {
256
- errors . push (
257
- buildCodeFrameError (
258
- file . opts . filename ,
259
- file . code ,
260
- node . errorLoc || node . loc ,
261
- node . label ,
262
- ) ,
263
- ) ;
264
- }
265
- } ) ;
256
+ rootMigrators . push ( migrate ) ;
257
+ file . ___compileStage = "migrate" ;
258
+ traverseAll ( file , rootMigrators ) ;
259
+
260
+ if ( file . ___hasParseErrors ) {
261
+ if ( markoOpts . errorRecovery ) {
262
+ t . traverseFast ( file . path . node , ( node ) => {
263
+ if ( node . type === "MarkoParseError" ) {
264
+ diagnosticError ( file . path , {
265
+ label : node . label ,
266
+ loc : node . errorLoc || node . loc ,
267
+ } ) ;
268
+ }
269
+ } ) ;
270
+ } else {
271
+ let errors = [ ] ;
272
+ t . traverseFast ( file . path . node , ( node ) => {
273
+ if ( node . type === "MarkoParseError" ) {
274
+ errors . push (
275
+ buildCodeFrameError (
276
+ file . opts . filename ,
277
+ file . code ,
278
+ node . errorLoc || node . loc ,
279
+ node . label ,
280
+ ) ,
281
+ ) ;
282
+ }
283
+ } ) ;
266
284
267
- throwAggregateError ( errors ) ;
285
+ throwAggregateError ( errors ) ;
286
+ }
268
287
}
269
- }
270
288
271
- if ( isMigrate ) {
272
- return file ;
273
- }
289
+ if ( isMigrate ) {
290
+ return file ;
291
+ }
274
292
275
- const rootTransformers = [ ] ;
276
- for ( const id in taglibLookup . taglibsById ) {
277
- for ( const transformer of taglibLookup . taglibsById [ id ] . transformers ) {
278
- addPlugin ( meta , rootTransformers , transformer ) ;
293
+ const rootTransformers = [ ] ;
294
+ for ( const id in taglibLookup . taglibsById ) {
295
+ for ( const transformer of taglibLookup . taglibsById [ id ] . transformers ) {
296
+ addPlugin ( meta , rootTransformers , transformer ) ;
297
+ }
279
298
}
280
- }
281
299
282
- rootTransformers . push ( transform ) ;
283
- if ( translator . transform ) {
284
- rootTransformers . push ( translator . transform ) ;
285
- }
286
- file . ___compileStage = "transform" ;
287
- traverseAll ( file , rootTransformers ) ;
300
+ rootTransformers . push ( transform ) ;
301
+ if ( translator . transform ) {
302
+ rootTransformers . push ( translator . transform ) ;
303
+ }
304
+ file . ___compileStage = "transform" ;
305
+ traverseAll ( file , rootTransformers ) ;
288
306
289
- for ( const taglibId in taglibLookup . taglibsById ) {
290
- const { filePath } = taglibLookup . taglibsById [ taglibId ] ;
307
+ for ( const taglibId in taglibLookup . taglibsById ) {
308
+ const { filePath } = taglibLookup . taglibsById [ taglibId ] ;
291
309
292
- if (
293
- filePath [ filePath . length - 5 ] === "." &&
294
- filePath . endsWith ( "marko.json" )
295
- ) {
296
- meta . watchFiles . push ( filePath ) ;
310
+ if (
311
+ filePath [ filePath . length - 5 ] === "." &&
312
+ filePath . endsWith ( "marko.json" )
313
+ ) {
314
+ meta . watchFiles . push ( filePath ) ;
315
+ }
297
316
}
298
- }
299
317
300
- compileCache . set ( cacheKey , {
301
- time : Date . now ( ) ,
302
- file,
303
- contentHash,
304
- } ) ;
305
-
306
- if ( translator . analyze ) {
307
- try {
308
- file . ___compileStage = "analyze" ;
309
- traverseAll ( file , translator . analyze ) ;
310
- } catch ( e ) {
311
- compileCache . delete ( cacheKey ) ;
312
- throw e ;
318
+ compileCache . set ( cacheKey , {
319
+ time : Date . now ( ) ,
320
+ file,
321
+ contentHash,
322
+ } ) ;
323
+
324
+ if ( translator . analyze ) {
325
+ try {
326
+ file . ___compileStage = "analyze" ;
327
+ traverseAll ( file , translator . analyze ) ;
328
+ } catch ( e ) {
329
+ compileCache . delete ( cacheKey ) ;
330
+ throw e ;
331
+ }
313
332
}
314
- }
315
333
316
- return file ;
334
+ return file ;
335
+ } finally {
336
+ taglibConfig . fs = prevFs ;
337
+ currentFile = prevFile ;
338
+ }
317
339
}
318
340
319
341
function shallowClone ( data ) {
0 commit comments