@marko/translator-default
Version:
Translates Marko templates to the default Marko runtime.
74 lines (65 loc) • 2.54 kB
JavaScript
;var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");exports.__esModule = true;exports.default = _default;var _babelUtils = require("@marko/babel-utils");
var _compiler = require("@marko/compiler");
var _withPreviousLocation = _interopRequireDefault(require("../util/with-previous-location"));
var _util = require("./util");
function _default(path) {
const {
node,
hub: { file }
} = path;
const tagProperties = path.node.extra && path.node.extra.properties || [];
const { key, arguments: args } = node;
const foundAttrs = (0, _util.getAttrs)(path, true);
let renderBodyProp;
let attrsLen = _compiler.types.isNullLiteral(foundAttrs) ? 0 : 1;
if (node.preserveAttrs) {
tagProperties.push(
_compiler.types.objectProperty(
_compiler.types.identifier("pa"),
_compiler.types.objectExpression(
node.preserveAttrs.map((name) =>
_compiler.types.objectProperty(
_compiler.types.isValidIdentifier(name) ?
_compiler.types.identifier(name) :
_compiler.types.stringLiteral(name),
_compiler.types.numericLiteral(1)
)
)
)
)
);
}
if (_compiler.types.isObjectExpression(foundAttrs)) {
const renderBodyIndex = foundAttrs.properties.findIndex(
(prop) => prop.key && prop.key.value === "renderBody"
);
attrsLen = foundAttrs.properties.length;
if (renderBodyIndex > -1) {
renderBodyProp = foundAttrs.properties[renderBodyIndex];
foundAttrs.properties.splice(renderBodyIndex, 1);
attrsLen--;
}
}
const dynamicTagRenderCall = _compiler.types.expressionStatement(
_compiler.types.callExpression(
(0, _babelUtils.importDefault)(
file,
`marko/src/runtime/helpers/dynamic-tag.js`,
"marko_dynamic_tag"
),
[
_compiler.types.identifier("out"),
node.name,
attrsLen ? _compiler.types.arrowFunctionExpression([], foundAttrs) : _compiler.types.nullLiteral(),
renderBodyProp ? renderBodyProp.value : _compiler.types.nullLiteral(),
args && args.length ? _compiler.types.arrayExpression(args) : _compiler.types.nullLiteral(),
tagProperties.length ?
_compiler.types.objectExpression(tagProperties) :
_compiler.types.nullLiteral(),
file._componentDefIdentifier,
key,
...(0, _util.buildEventHandlerArray)(path)]
)
);
path.replaceWith((0, _withPreviousLocation.default)(dynamicTagRenderCall, node));
}