UNPKG

marko-widgets

Version:

<h1 align="center"> <!-- Logo --> <br/> marko-widgets@8 <br/>

86 lines (84 loc) 2.77 kB
// packages/marko-widgets/src/migrate/w-preserve-directives.ts import { types as t } from "@marko/compiler"; import { diagnosticDeprecate, diagnosticError } from "@marko/babel-utils"; import { getAttribute } from "@marko/compat-utils"; var w_preserve_directives_default = { MarkoAttribute(attr) { switch (attr.node.name) { case "w-preserve": renameAttribute(attr, "no-update"); break; case "w-preserve-if": renameAttribute(attr, "no-update-if"); break; case "w-preserve-body": renameAttribute(attr, "no-update-body"); break; case "w-preserve-body-if": renameAttribute(attr, "no-update-body-if"); break; case "w-preserve-attrs": { const value = attr.node.value; if (value.type !== "StringLiteral") { diagnosticError(attr, { label: `The "w-preserve-attrs" attribute must have a string literal value.` }); attr.remove(); return; } diagnosticDeprecate(attr, { label: `The "w-preserve-attrs" attribute is deprecated. Please use ":no-update" modifier instead. See: https://github.com/marko-js/marko/wiki/Deprecation:-w\u2010*-Attributes`, fix() { const tag = attr.parentPath; const seen = /* @__PURE__ */ new Set(); const preserveAttrs = value.value.split(/\s*,\s*/); attr.remove(); for (const name of preserveAttrs) { if (seen.has(name)) continue; seen.add(name); const existing = getAttribute(tag, name); if (existing) { existing.replaceWith( t.markoAttribute( name, existing.node.value, "no-update", existing.node.arguments, existing.node.default, existing.node.bound ) ); } else { tag.node.attributes.unshift( t.markoAttribute(name, t.nullLiteral(), "no-update") ); } } } }); break; } } } }; function renameAttribute(attr, name) { const { node } = attr; diagnosticDeprecate(attr, { label: `The "${node.name}" attribute is deprecated. Please use "${name}" attribute instead. See: https://github.com/marko-js/marko/wiki/Deprecation:-w\u2010*-Attributes`, fix() { attr.replaceWith( t.markoAttribute( name, node.value, node.modifier, node.arguments, node.default, node.bound ) ); } }); } export { w_preserve_directives_default };