marko-widgets
Version:
<h1 align="center"> <!-- Logo --> <br/> marko-widgets@8 <br/>
70 lines (69 loc) • 2.29 kB
JavaScript
// packages/marko-widgets/src/components/assign/migrate.ts
import { types as t } from "@marko/compiler";
import {
diagnosticDeprecate,
diagnosticError,
getEnd,
getStart,
parseExpression
} from "@marko/babel-utils";
var migrate_default = {
exit(tag) {
if (!tag.node.attributes.length) {
diagnosticError(tag, {
label: "The <assign> tag requires a value.",
fix() {
tag.remove();
}
});
return;
}
diagnosticDeprecate(tag, {
label: 'The "<assign>" tag is deprecated. Please use "$ <js_code>" for JavaScript in the template. See: https://github.com/marko-js/marko/wiki/Deprecation:-var,-assign,-invoke-tags',
fix() {
var _a;
const { file } = tag.hub;
const statements = [];
let condition;
for (const attr of tag.get("attributes")) {
const { node } = attr;
if (t.isMarkoSpreadAttribute(node)) {
diagnosticError(attr, {
label: "Spread attributes are not supported in <assign> tags.",
fix() {
attr.remove();
}
});
continue;
}
const { name, value } = node;
if (name === "if" && ((_a = node.arguments) == null ? void 0 : _a.length)) {
condition = node.arguments.length === 1 ? node.arguments[0] : t.sequenceExpression(node.arguments);
continue;
}
const nodeStart = getStart(file, node);
const nodeEnd = getEnd(file, node);
statements.push(
t.expressionStatement(
t.isBooleanLiteral(value) && value.value === true && /^[+-]{2,}|[+-]{2,}$/.test(name) ? nodeStart != null && nodeEnd != null ? parseExpression(file, name, nodeStart, nodeEnd) : parseExpression(file, name) : t.assignmentExpression("=", t.identifier(name), value)
)
);
}
if (condition) {
tag.replaceWith(
t.markoScriptlet([
t.ifStatement(condition, t.blockStatement(statements))
])
);
} else {
tag.replaceWithMultiple(
statements.map((it) => t.markoScriptlet([it]))
);
}
}
});
}
};
export {
migrate_default as default
};