UNPKG

@marko/compiler

Version:
62 lines (54 loc) 1.77 kB
"use strict";exports.__esModule = true;exports.visitor = void 0;var _babelUtils = require("@marko/compiler/babel-utils"); var _babel = require("@marko/compiler/internal/babel"); var _pluginHooks = require("../util/plugin-hooks"); /** * Applies custom migrators on tags. */ const visitor = exports.visitor = { MarkoTag: { enter(path) { const migrators = getMigratorsForTag(path); const { node } = path; for (const migrator of migrators) { (0, _pluginHooks.enter)(migrator, path, _babel.types); if (path.node !== node) break; // Stop if node is replaced. } }, exit(path) { const migrators = getMigratorsForTag(path); const { node } = path; for (const migrator of migrators) { (0, _pluginHooks.exit)(migrator, path, _babel.types); if (path.node !== node) break; // Stop if node is replaced. } } } }; function getMigratorsForTag(path) { const { hub: { file } } = path; const { watchFiles } = file.metadata.marko; const tagName = path.get("name.value").node || "*"; const MIGRATOR_CACHE = file.MIGRATOR_CACHE = file.MIGRATOR_CACHE || Object.create(null); let migrators = MIGRATOR_CACHE[tagName]; if (!migrators) { migrators = MIGRATOR_CACHE[tagName] = []; const addMigrators = (tagDef) => { if (tagDef && tagDef.migrators) { for (const migrator of tagDef.migrators) { if (migrator.path) { watchFiles.push(migrator.path); } migrators.push(migrator.hook.default || migrator.hook); } } }; addMigrators((0, _babelUtils.getTagDef)(path)); if (tagName !== "*") { addMigrators((0, _babelUtils.getTagDefForTagName)(file, "*")); } } return migrators; }