UNPKG

marko

Version:

UI Components + streaming, async, high performance, HTML templating for Node.js and the browser.

77 lines (68 loc) 2.65 kB
"use strict";exports.__esModule = true;exports.default = _default;var _compiler = require("@marko/compiler"); var _babelUtils = require("@marko/compiler/babel-utils"); var _withPreviousLocation = _interopRequireDefault(require("../util/with-previous-location")); var _util = require("./util");function _interopRequireDefault(e) {return e && e.__esModule ? e : { default: e };} function _default(path) { const { node, hub: { file } } = path; const extra = path.node.extra || {}; const tagProperties = 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 renderBodyKey = extra.featureType === "tags" ? "content" : "renderBody"; const renderBodyIndex = foundAttrs.properties.findIndex( (prop) => prop.key && prop.key.value === renderBodyKey ); 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)); }