Skip to content

Commit 7110193

Browse files
committed
refactor: move file/program context apis into compiler
1 parent 225fee2 commit 7110193

26 files changed

+253
-248
lines changed

.changeset/cold-waves-find.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@marko/runtime-tags": patch
3+
---
4+
5+
Refactor to use getProgram/getFile api from compiler.

.changeset/itchy-mice-nail.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@marko/compiler": patch
3+
---
4+
5+
Expose getProgram and getFile apis for accessing the file/program of the active compilation.

packages/compiler/index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ export function getRuntimeEntryFiles(
6969
translator?: string | undefined,
7070
): string[];
7171

72+
export function getFile(): types.BabelFile;
73+
export function getProgram(): types.NodePath<types.Program>;
74+
7275
export namespace taglib {
7376
export function resolveOptionalTaglibs(
7477
taglibIds: string[],

packages/compiler/src/babel-plugin/index.js

Lines changed: 135 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { visitor as migrate } from "./plugins/migrate";
1616
import { visitor as transform } from "./plugins/transform";
1717

1818
const SOURCE_FILES = new WeakMap();
19+
let currentFile;
1920

2021
export default (api, markoOpts) => {
2122
api.assertVersion(7);
@@ -61,22 +62,18 @@ export default (api, markoOpts) => {
6162
curOpts = opts;
6263
},
6364
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;
7469
},
7570
pre(file) {
7671
const { buildError: prevBuildError } = file.hub;
7772
const { buildCodeFrameError: prevCodeFrameError } = file;
7873
const prevFS = taglibConfig.fs;
74+
const prevFile = currentFile;
7975
taglibConfig.fs = markoOpts.fileSystem;
76+
currentFile = file;
8077
curOpts = undefined;
8178
try {
8279
const { ast, metadata } = file;
@@ -114,6 +111,7 @@ export default (api, markoOpts) => {
114111
file.path.scope.crawl(); // Ensure all scopes are accurate for subsequent babel plugins
115112
} finally {
116113
taglibConfig.fs = prevFS;
114+
currentFile = prevFile;
117115
file.buildCodeFrameError = prevCodeFrameError;
118116
file.hub.buildError = prevBuildError;
119117
file.markoOpts =
@@ -147,7 +145,23 @@ export default (api, markoOpts) => {
147145
};
148146
};
149147

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) {
151165
const { translator } = markoOpts;
152166
let compileCache = markoOpts.cache.get(translator);
153167

@@ -191,129 +205,137 @@ export function getMarkoFile(code, fileOpts, markoOpts) {
191205
return cached.file;
192206
}
193207

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+
},
206225
},
207-
},
208-
});
226+
}));
209227

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+
});
217235

218-
file.markoOpts = markoOpts;
219-
file.___taglibLookup = taglibLookup;
220-
file.___getMarkoFile = getMarkoFile;
236+
file.markoOpts = markoOpts;
237+
file.___taglibLookup = taglibLookup;
238+
file.___getMarkoFile = getMarkoFile;
221239

222-
file.___compileStage = "parse";
223-
parseMarko(file);
240+
file.___compileStage = "parse";
241+
parseMarko(file);
224242

225-
if (isSource) {
226-
return file;
227-
}
243+
if (isSource) {
244+
return file;
245+
}
228246

229-
file.path.scope.crawl(); // Initialize bindings.
247+
file.path.scope.crawl(); // Initialize bindings.
230248

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+
}
235254
}
236-
}
237255

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+
});
266284

267-
throwAggregateError(errors);
285+
throwAggregateError(errors);
286+
}
268287
}
269-
}
270288

271-
if (isMigrate) {
272-
return file;
273-
}
289+
if (isMigrate) {
290+
return file;
291+
}
274292

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+
}
279298
}
280-
}
281299

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);
288306

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];
291309

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+
}
297316
}
298-
}
299317

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+
}
313332
}
314-
}
315333

316-
return file;
334+
return file;
335+
} finally {
336+
taglibConfig.fs = prevFs;
337+
currentFile = prevFile;
338+
}
317339
}
318340

319341
function shallowClone(data) {

packages/compiler/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import throwAggregateError from "./util/merge-errors";
1414
import shouldOptimize from "./util/should-optimize";
1515
import tryLoadTranslator from "./util/try-load-translator";
1616
export { taglib };
17+
export { getFile, getProgram } from "./babel-plugin";
1718

1819
const CWD = process.cwd();
1920

packages/runtime-tags/src/translator/core/html-comment.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { types as t } from "@marko/compiler";
1+
import { getProgram, types as t } from "@marko/compiler";
22
import {
33
assertNoArgs,
44
assertNoAttributes,
@@ -30,7 +30,7 @@ import { addStatement, getRegisterUID } from "../util/signals";
3030
import translateVar from "../util/translate-var";
3131
import * as walks from "../util/walks";
3232
import * as writer from "../util/writer";
33-
import { currentProgramPath, scopeIdentifier } from "../visitors/program";
33+
import { scopeIdentifier } from "../visitors/program";
3434

3535
export const kCommentTagBinding = Symbol("comment tag binding");
3636
const kGetterId = Symbol("node getter id");
@@ -116,10 +116,10 @@ export default {
116116
const references = tag.scope.getBinding(varName)!.referencePaths;
117117
let getterFnIdentifier: t.Identifier | undefined;
118118
if (getterId) {
119-
getterFnIdentifier = currentProgramPath.scope.generateUidIdentifier(
119+
getterFnIdentifier = getProgram().scope.generateUidIdentifier(
120120
`get_${varName}`,
121121
);
122-
currentProgramPath.node.body.push(
122+
getProgram().node.body.push(
123123
t.variableDeclaration("const", [
124124
t.variableDeclarator(
125125
getterFnIdentifier,

0 commit comments

Comments
 (0)