@@ -11746,9 +11746,10 @@ const installEstree = (modules) => {
1174611746 /**
1174711747 * Turns the leading string statements of a body into directives.
1174811748 * @param {Node[]} body the statements
11749+ * @param {EXPECTED_ANY[]=} prologue the ESTree statements, whose `directive` says which are directives when no source is held
1174911750 * @returns {Node[]} the same array
1175011751 */
11751- const normalizeDirectives = (body) => {
11752+ const normalizeDirectives = (body, prologue ) => {
1175211753 for (let i = 0; i < body.length; i++) {
1175311754 const statement = /** @type {EXPECTED_ANY} */ (body[i]);
1175411755 if (
@@ -11757,7 +11758,12 @@ const installEstree = (modules) => {
1175711758 ) {
1175811759 return body;
1175911760 }
11760- const raw = directiveSource(statement.expression);
11761+ let raw = directiveSource(statement.expression);
11762+ if (raw === null && prologue !== undefined) {
11763+ const { directive, expression } = prologue[i];
11764+ if (typeof directive !== "string") return body;
11765+ raw = expression.raw || JSON.stringify(directive);
11766+ }
1176111767 // A string the source does not open a prologue with ends it: one inside
1176211768 // parentheses is an expression, whatever it spells.
1176311769 if (raw === undefined) return body;
@@ -11779,14 +11785,29 @@ const installEstree = (modules) => {
1177911785 return body;
1178011786 };
1178111787
11788+ /**
11789+ * @returns {boolean} whether the `correct` phase is on, which reads a given
11790+ * ESTree tree where terser's conversion changes or throws on it
11791+ */
11792+ const correcting = () =>
11793+ modules.corrections !== undefined && modules.corrections.enabled;
11794+
11795+ /**
11796+ * @param {EXPECTED_ANY[]} statements a body given as ESTree
11797+ * @returns {EXPECTED_ANY[] | undefined} them, when their `directive` decides
11798+ * the prologue: terser's conversion reads any leading string as one
11799+ */
11800+ const prologueFromEstree = (statements) =>
11801+ tokenAt === null && correcting() ? statements : undefined;
11802+
1178211803 /**
1178311804 * A function's block, its leading string statements read as directives.
1178411805 * @param {EXPECTED_ANY} estree the block
1178511806 * @returns {Node} terser's block
1178611807 */
1178711808 const blockFromEstree = (estree) => {
1178811809 const block = fromEstree(estree);
11789- normalizeDirectives(block.body);
11810+ normalizeDirectives(block.body, prologueFromEstree(estree.body) );
1179011811 return block;
1179111812 };
1179211813
@@ -12174,7 +12195,10 @@ const installEstree = (modules) => {
1217412195 ToplevelNode({
1217512196 startToken: startToken(estree),
1217612197 endToken: endToken(estree),
12177- body: normalizeDirectives(estree.body.map(fromEstree))
12198+ body: normalizeDirectives(
12199+ estree.body.map(fromEstree),
12200+ prologueFromEstree(estree.body)
12201+ )
1217812202 }),
1217912203 ArrayPattern: (estree) =>
1218012204 makeNode(DestructuringNode, undefined, {
@@ -12258,10 +12282,10 @@ const installEstree = (modules) => {
1225812282 FunctionExpression: (estree) => lambdaFromEstree(estree, false),
1225912283 ArrowFunctionExpression: (estree) => {
1226012284 // terser's conversion leaves an arrow's directive a statement, where its
12261- // parser reads one; only a tree we parsed ourselves is ours to fix .
12285+ // parser reads one, so the body turns sloppy: corrections read it too .
1226212286 const body =
1226312287 estree.body.type === "BlockStatement"
12264- ? tokenAt === null
12288+ ? tokenAt === null && !correcting()
1226512289 ? fromEstree(estree.body)
1226612290 : blockFromEstree(estree.body)
1226712291 : BlockStatementNode({
@@ -12650,8 +12674,12 @@ const installEstree = (modules) => {
1265012674 // WHY: terser's conversion exports every declaration as a value, where
1265112675 // its parser exports a named one as a declaration and an anonymous class
1265212676 // or function as the expression it is; `drop_unused` then reads a name
12653- // the value has not got. Only a tree we parsed ourselves is ours to fix.
12654- if (tokenAt === null || (!isClass && !isFunction)) {
12677+ // the value has not got. Only a tree we parsed ourselves is ours to fix,
12678+ // and, while correcting, an anonymous one, which terser's throws on.
12679+ if (
12680+ (tokenAt === null && (declaration.id || !correcting())) ||
12681+ (!isClass && !isFunction)
12682+ ) {
1265512683 return makeNode(ExportNode, undefined, {
1265612684 startToken: start,
1265712685 endToken: end,
@@ -13333,29 +13361,57 @@ const installEstree = (modules) => {
1333313361 defineToEstree("PrefixedTemplateString", (node) => ({
1333413362 type: "TaggedTemplateExpression",
1333513363 tag: toEstree(node.tag),
13336- quasi: toEstree (node.quasi)
13364+ quasi: templateToEstree (node.quasi, true )
1333713365 }));
13338- defineToEstree("TemplateString", (node) => {
13366+ defineToEstree("TemplateString", (node) => templateToEstree(node, false));
13367+
13368+ /**
13369+ * A template as ESTree; an untagged one's raw text is written anew from its
13370+ * value while correcting, since the compressor changes only the value.
13371+ * @param {EXPECTED_ANY} node terser's template
13372+ * @param {boolean} tagged whether a tag reads its raw text
13373+ * @returns {EXPECTED_ANY} the template literal
13374+ */
13375+ const templateToEstree = (node, tagged) => {
1333913376 /** @type {EXPECTED_ANY[]} */
1334013377 const quasis = [];
1334113378 /** @type {EXPECTED_ANY[]} */
1334213379 const expressions = [];
13343- for (let i = 0; i < node.segments.length; i++) {
13344- if (i % 2 !== 0) {
13345- expressions.push(toEstree(node.segments[i]));
13380+ // Inlining a template into another leaves two text segments in a row,
13381+ // which ESTree holds as one.
13382+ let afterText = false;
13383+ for (const segment of node.segments) {
13384+ if (segment.type !== "TemplateElement") {
13385+ expressions.push(toEstree(segment));
13386+ afterText = false;
13387+ } else if (afterText) {
13388+ const { value } = quasis[quasis.length - 1];
13389+ value.raw += segment.raw;
13390+ value.cooked =
13391+ value.cooked === null || segment.value === null
13392+ ? null
13393+ : value.cooked + segment.value;
1334613394 } else {
1334713395 quasis.push({
1334813396 type: "TemplateElement",
13349- value: {
13350- raw: node.segments[i].raw,
13351- cooked: node.segments[i].value
13352- },
13353- tail: i === node.segments.length - 1
13397+ value: { raw: segment.raw, cooked: segment.value },
13398+ tail: false
1335413399 });
13400+ afterText = true;
13401+ }
13402+ }
13403+ quasis[quasis.length - 1].tail = true;
13404+ if (!tagged && correcting()) {
13405+ for (const { value } of quasis) {
13406+ value.raw = value.cooked.replace(
13407+ /\\|`|\$\{|\r/g,
13408+ (/** @type {string} */ match) =>
13409+ match === "\r" ? "\\r" : `\\${match}`
13410+ );
1335513411 }
1335613412 }
1335713413 return { type: "TemplateLiteral", quasis, expressions };
13358- }) ;
13414+ };
1335913415 defineToEstree("Defun", (node) => ({
1336013416 type: "FunctionDeclaration",
1336113417 id: toEstree(node.id),
0 commit comments