marko-widgets
Version:
<h1 align="center"> <!-- Logo --> <br/> marko-widgets@8 <br/>
241 lines (240 loc) • 9.21 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var migrate_exports = {};
__export(migrate_exports, {
default: () => migrate_default
});
module.exports = __toCommonJS(migrate_exports);
var import_compiler = require("@marko/compiler");
var import_babel_utils = require("@marko/babel-utils");
var migrate_default = {
exit(tag) {
(0, import_babel_utils.diagnosticDeprecate)(tag, {
label: `The "<async-fragment>" tag is deprecated. Please use "<await>" instead. See: https://github.com/marko-js/marko/wiki/Deprecation:-async-fragment`,
fix() {
const { file } = tag.hub;
const thenChildren = [];
const placeholderChildren = [];
const timeoutChildren = [];
const errorChildren = [];
let varExpression;
let providerExpression;
let providerMethod;
let providerScope;
let providerArgs;
let providerArgProps;
for (const attr of tag.get("attributes")) {
if (attr.isMarkoSpreadAttribute()) {
(0, import_babel_utils.diagnosticError)(attr, {
label: "Spread attributes are not supported on <async-fragment>.",
fix() {
attr.remove();
}
});
return;
}
const { name, value } = attr.node;
switch (name) {
case "var":
varExpression = value;
break;
case "data-provider":
providerExpression = value;
break;
case "method":
providerMethod = value;
break;
case "scope":
providerScope = value;
break;
case "arg":
providerArgs = value;
break;
case "placeholder":
placeholderChildren.push(import_compiler.types.markoPlaceholder(value));
break;
case "timeout-message":
timeoutChildren.push(import_compiler.types.markoPlaceholder(value));
break;
case "error-message":
errorChildren.push(import_compiler.types.markoPlaceholder(value));
break;
default:
if (name.startsWith("arg-")) {
const prop = import_compiler.types.objectProperty(
import_compiler.types.stringLiteral(name.slice(4)),
value
);
if (providerArgProps) {
providerArgProps.push(prop);
} else {
providerArgProps = [prop];
}
break;
}
continue;
}
attr.remove();
}
if (!(varExpression && import_compiler.types.isStringLiteral(varExpression) && import_compiler.types.isValidIdentifier(varExpression.value))) {
(0, import_babel_utils.diagnosticError)(tag, {
label: 'Invalid <async-fragment> tag. "var" must be a string literal that is a valid javascript identifier. Example; <async-fragment data-provider=data.userInfo var="userInfo" />',
fix() {
tag.remove();
}
});
return;
}
if (!providerExpression) {
(0, import_babel_utils.diagnosticError)(tag, {
label: 'Invalid <async-fragment> tag. "data-provider" attribute is missing. Example; <async-fragment data-provider=data.userInfo var="userInfo" />',
fix() {
tag.remove();
}
});
return;
}
for (const child of tag.node.body.body) {
if (child.type === "MarkoTag" && child.name.type === "StringLiteral") {
switch (child.name.value) {
case "async-fragment-placeholder":
placeholderChildren.push(...child.body.body);
continue;
case "async-fragment-error":
errorChildren.push(...child.body.body);
continue;
case "async-fragment-timeout":
timeoutChildren.push(...child.body.body);
continue;
}
}
thenChildren.push(child);
}
const valueIdentifier = import_compiler.types.identifier(varExpression.value);
const varStart = (0, import_babel_utils.getStart)(file, varExpression);
const varEnd = (0, import_babel_utils.getEnd)(file, varExpression);
if (varStart != null && varEnd != null) {
(0, import_babel_utils.withLoc)(file, valueIdentifier, varStart + 1, varEnd - 1);
}
if (providerMethod) {
providerExpression = import_compiler.types.memberExpression(
providerExpression,
providerMethod,
!import_compiler.types.isIdentifier(providerMethod)
);
}
if (providerArgProps) {
if (providerArgs) {
providerArgs = import_compiler.types.objectExpression([
import_compiler.types.spreadElement(providerArgs),
...providerArgProps
]);
} else {
providerArgs = import_compiler.types.objectExpression(providerArgProps);
}
}
if (providerArgs || providerScope) {
providerExpression = import_compiler.types.callExpression(
import_compiler.types.memberExpression(providerExpression, import_compiler.types.identifier("bind")),
[providerScope || import_compiler.types.nullLiteral()].concat(providerArgs || [])
);
}
const rootAttrTags = [];
if (thenChildren.length) {
rootAttrTags.push(
import_compiler.types.markoTag(
import_compiler.types.stringLiteral("@then"),
[],
import_compiler.types.markoTagBody(thenChildren, [valueIdentifier])
)
);
}
if (placeholderChildren.length) {
rootAttrTags.push(
import_compiler.types.markoTag(
import_compiler.types.stringLiteral("@placeholder"),
[],
import_compiler.types.markoTagBody(placeholderChildren)
)
);
}
if (timeoutChildren.length) {
const timeoutConditionTag = import_compiler.types.markoTag(
import_compiler.types.stringLiteral("if"),
[],
import_compiler.types.markoTagBody(timeoutChildren),
[
import_compiler.types.binaryExpression(
"===",
import_compiler.types.memberExpression(import_compiler.types.identifier("err"), import_compiler.types.identifier("name")),
import_compiler.types.stringLiteral("TimeoutError")
)
]
);
rootAttrTags.push(
import_compiler.types.markoTag(
import_compiler.types.stringLiteral("@catch"),
[],
import_compiler.types.markoTagBody(
errorChildren.length ? [
timeoutConditionTag,
import_compiler.types.markoTag(
import_compiler.types.stringLiteral("else"),
[],
import_compiler.types.markoTagBody(errorChildren)
)
] : [timeoutConditionTag],
[import_compiler.types.identifier("err")]
)
)
);
} else if (errorChildren.length) {
rootAttrTags.push(
import_compiler.types.markoTag(
import_compiler.types.stringLiteral("@catch"),
[],
import_compiler.types.markoTagBody(errorChildren)
)
);
}
if (tag.node.attributeTags) {
tag.replaceWith(
import_compiler.types.markoTag(
import_compiler.types.stringLiteral("await"),
tag.node.attributes,
import_compiler.types.markoTagBody([]),
[providerExpression],
void 0,
rootAttrTags
)
);
} else {
tag.replaceWith(
import_compiler.types.markoTag(
import_compiler.types.stringLiteral("await"),
tag.node.attributes,
import_compiler.types.markoTagBody(rootAttrTags),
[providerExpression]
)
);
}
}
});
}
};