transpile label statements to p calls

This commit is contained in:
Felix Roos 2024-03-17 03:14:05 +01:00
parent d1c713fa29
commit 6359bbe139

View File

@ -9,7 +9,7 @@ export function transpiler(input, options = {}) {
let ast = parse(input, {
ecmaVersion: 2022,
allowAwaitOutsideFunction: true,
locations: true,
locations: false,
});
let miniLocations = [];
@ -49,6 +49,9 @@ export function transpiler(input, options = {}) {
if (isBareSamplesCall(node, parent)) {
return this.replace(withAwait(node));
}
if (isLabelStatement(node)) {
return this.replace(labelToP(node));
}
},
leave(node, parent, prop, index) {},
});
@ -132,3 +135,31 @@ function withAwait(node) {
argument: node,
};
}
function isLabelStatement(node) {
return node.type === 'LabeledStatement';
}
function labelToP(node) {
return {
type: 'ExpressionStatement',
expression: {
type: 'CallExpression',
callee: {
type: 'MemberExpression',
object: node.body.expression,
property: {
type: 'Identifier',
name: 'p',
},
},
arguments: [
{
type: 'Literal',
value: node.label.name,
raw: `'${node.label.name}'`,
},
],
},
};
}