Skip to content

Commit

Permalink
feat: support taglib translate hook, support arrays in taglib hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Jul 7, 2021
1 parent 6723f2b commit e2b0e66
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 22 deletions.
60 changes: 40 additions & 20 deletions packages/compiler/src/babel-plugin/index.js
Expand Up @@ -48,7 +48,7 @@ export default (api, markoOpts) => {
setFS(markoOpts.fileSystem);
try {
const file = getMarkoFile(code, jsParseOptions, markoOpts);
const finalAst = t.cloneDeep(file.ast);
const finalAst = t.cloneNode(file.ast, true);
SOURCE_FILES.set(finalAst, file);
return finalAst;
} finally {
Expand All @@ -65,16 +65,27 @@ export default (api, markoOpts) => {

const { ast, metadata } = file;
const sourceFile = SOURCE_FILES.get(ast);
metadata.marko = shallowClone(sourceFile.metadata.marko);

const taglibLookup = sourceFile.___taglibLookup;
const rootTranslators = [];
const { buildCodeFrameError } = file;
const { buildError } = file.hub;
metadata.marko = shallowClone(sourceFile.metadata.marko);
file.buildCodeFrameError = MarkoFile.prototype.buildCodeFrameError;
file.hub.buildError = file.buildCodeFrameError.bind(file);
file.markoOpts = markoOpts;
file.___taglibLookup = sourceFile.___taglibLookup;
file.___taglibLookup = taglibLookup;
file.___getMarkoFile = getMarkoFile;
traverseAll(file, translator.translate);

for (const id in taglibLookup.taglibsById) {
addPlugin(
metadata.marko,
rootTranslators,
taglibLookup.taglibsById[id].translator
);
}

rootTranslators.push(translator.translate);
traverseAll(file, rootTranslators);
file.buildCodeFrameError = buildCodeFrameError;
file.hub.buildError = buildError;
file.markoOpts = file.___taglibLookup = file.___getMarkoFile = undefined;
Expand Down Expand Up @@ -153,9 +164,6 @@ export function getMarkoFile(code, jsParseOptions, markoOpts) {
watchFiles: []
});

const rootMigrators = [migrate];
const rootTransformers = [transform];

file.markoOpts = markoOpts;
file.___taglibLookup = taglibLookup;
file.___getMarkoFile = getMarkoFile;
Expand All @@ -175,32 +183,28 @@ export function getMarkoFile(code, jsParseOptions, markoOpts) {

file.path.scope.crawl(); // Initialize bindings.

const rootMigrators = [];
for (const id in taglibLookup.taglibsById) {
const { migrators } = taglibLookup.taglibsById[id];
for (const migrator of migrators) {
if (migrator.path) {
meta.watchFiles.push(migrator.path);
}
rootMigrators.push(migrator.hook.default || migrator.hook);
for (const migrator of taglibLookup.taglibsById[id].migrators) {
addPlugin(meta, rootMigrators, migrator);
}
}

rootMigrators.push(migrate);
traverseAll(file, rootMigrators);

if (isMigrate) {
return file;
}

const rootTransformers = [];
for (const id in taglibLookup.taglibsById) {
const { transformers } = taglibLookup.taglibsById[id];
for (const transformer of transformers) {
if (transformer.path) {
meta.watchFiles.push(transformer.path);
}
rootTransformers.push(transformer.hook.default || transformer.hook);
for (const transformer of taglibLookup.taglibsById[id].transformers) {
addPlugin(meta, rootTransformers, transformer);
}
}

rootTransformers.push(transform);
traverseAll(file, rootTransformers);

for (const taglibId in taglibLookup.taglibsById) {
Expand Down Expand Up @@ -292,6 +296,22 @@ function traverseAll(file, visitors) {
}
}

function addPlugin(meta, arr, plugin) {
if (plugin) {
const hook = plugin.hook.default || plugin.hook;

if (plugin.path) {
meta.watchFiles.push(plugin.path);
}

if (Array.isArray(hook)) {
arr.push(...hook);
} else {
arr.push(hook);
}
}
}

function unique(item, i, list) {
return list.indexOf(item) === i;
}
2 changes: 1 addition & 1 deletion packages/compiler/src/babel-plugin/plugins/migrate.js
Expand Up @@ -45,7 +45,7 @@ function getMigratorsForTag(path) {
if (migrator.path) {
watchFiles.push(migrator.path);
}
migrators.push(migrator.hook);
migrators.push(migrator.hook.default || migrator.hook);
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/src/babel-plugin/plugins/transform.js
Expand Up @@ -47,7 +47,7 @@ function getTransformersForTag(path) {
if (transformer.path) {
watchFiles.push(transformer.path);
}
transformers.push(transformer.hook);
transformers.push(transformer.hook.default || transformer.hook);
}
}
};
Expand Down
7 changes: 7 additions & 0 deletions packages/compiler/src/taglib/loader/loadTaglibFromProps.js
Expand Up @@ -312,6 +312,13 @@ class TaglibLoader {
}
}

/**
* Exposes a babel visitor to perform additional translations on the entire ast.
*/
translate(value) {
this.tag.translator = normalizeHook(this.dirname, value);
}

/**
* Allows an ID to be explicitly assigned to a taglib.
* The taglib ID is used to prevent the same taglib (even if different versions)
Expand Down

0 comments on commit e2b0e66

Please sign in to comment.