@marko/compiler
Version:
Marko template to JS compiler.
98 lines (90 loc) • 2.92 kB
JavaScript
;exports.__esModule = true;exports.assertAllowedAttributes = assertAllowedAttributes;exports.assertAttributesOrArgs = assertAttributesOrArgs;exports.assertAttributesOrSingleArg = assertAttributesOrSingleArg;exports.assertNoArgs = assertNoArgs;exports.assertNoAttributeTags = assertNoAttributeTags;exports.assertNoAttributes = assertNoAttributes;exports.assertNoParams = assertNoParams;exports.assertNoVar = assertNoVar;function assertAllowedAttributes(path, allowed) {
let i = 0;
for (const attr of path.node.attributes) {
if (attr.type === "MarkoSpreadAttribute") {
throw path.hub.buildError(
attr,
`Tag does not support spread attributes.`
);
} else if (!allowed.includes(attr.name)) {
throw path.hub.buildError(
attr,
`Tag does not support the \`${attr.name}\` attribute.`
);
}
i++;
}
}
function assertNoAttributes(path) {
const { attributes } = path.node;
if (attributes.length) {
const start = attributes[0].loc.start;
const end = attributes[attributes.length - 1].loc.end;
throw path.hub.buildError(
{ loc: { start, end } },
"Tag does not support attributes."
);
}
}
function assertNoParams(path) {
const { params } = path.node.body;
if (params.length) {
const start = params[0].loc.start;
const end = params[params.length - 1].loc.end;
throw path.hub.buildError(
{ loc: { start, end } },
"Tag does not support parameters."
);
}
}
function assertNoAttributeTags(path) {
if (path.node.attributeTags.length) {
throw path.hub.buildError(
path.node.attributeTags[0],
"Tag not support nested attribute tags."
);
}
}
function assertNoArgs(path) {
const args = path.node.arguments;
if (args && args.length) {
const start = args[0].loc.start;
const end = args[args.length - 1].loc.end;
throw path.hub.buildError(
{ loc: { start, end } },
"Tag does not support arguments."
);
}
}
function assertNoVar(path) {
if (path.node.var) {
throw path.hub.buildError(
path.node.var,
"Tag does not support a variable."
);
}
}
function assertAttributesOrArgs(path) {
const { node } = path;
const args = node.arguments;
if (args && args.length && (node.attributes.length > 0 || node.body.length)) {
const start = args[0].loc.start;
const end = args[args.length - 1].loc.end;
throw path.hub.buildError(
{ loc: { start, end } },
"Tag does not support arguments when attributes or body present."
);
}
}
function assertAttributesOrSingleArg(path) {
assertAttributesOrArgs(path);
const args = path.node.arguments;
if (args && args.length > 1) {
const start = args[1].loc.start;
const end = args[args.length - 1].loc.end;
throw path.hub.buildError(
{ loc: { start, end } },
"Tag does not support multiple arguments."
);
}
}