marko-widgets
Version:
<h1 align="center"> <!-- Logo --> <br/> marko-widgets@8 <br/>
65 lines (64 loc) • 1.95 kB
JavaScript
// packages/marko-widgets/src/components/layout-put/migrate.ts
import { types as t } from "@marko/compiler";
import {
diagnosticDeprecate,
diagnosticError,
getStart,
withLoc
} from "@marko/babel-utils";
import { getAttributeValue } from "@marko/compat-utils";
var migrate_default = {
exit(tag) {
const {
node,
hub: { file }
} = tag;
const into = getAttributeValue(tag, "into");
if (!into || !into.isStringLiteral()) {
diagnosticError(tag, {
label: "The <layout-put> tag requires an 'into' attribute that is a string literal."
});
tag.remove();
return;
}
const value = getAttributeValue(tag, "value");
if (node.attributes.length > (value ? 2 : 1)) {
diagnosticError(tag, {
label: "The <layout-put> tag only supports the 'into' and 'value' attributes."
});
tag.remove();
return;
}
let children = node.body.body;
if (value) {
if (children.length) {
diagnosticError(tag, {
label: "The <layout-put> tag does not support children when using the 'value' attribute."
});
tag.remove();
return;
}
children = [t.markoPlaceholder(value.node)];
}
const newName = t.stringLiteral(`@${toCamelCase(into.node.value)}`);
const nameStart = getStart(file, node.name);
if (nameStart != null) {
withLoc(file, newName, nameStart, nameStart + into.node.value.length);
}
diagnosticDeprecate(tag, {
label: 'The "<layout-put>" tag is deprecated and replaced by `<@attribute>` tags tag. See: https://github.com/marko-js/marko/wiki/Deprecation:-layout-tag',
fix() {
tag.replaceWith(t.markoTag(newName, [], t.markoTagBody(children)));
}
});
}
};
function toCamelCase(str) {
return str.replace(/-([a-z])/g, matchToUpperCase);
}
function matchToUpperCase(_, str) {
return str.toUpperCase();
}
export {
migrate_default as default
};