@@ -14878,6 +14878,100 @@ const installCorrect = (modules) => {
1487814878 return original(self, compressor);
1487914879 });
1488014880
14881+ // The three sets a function reserves, shared so reading the parent chain of
14882+ // every call allocates nothing.
14883+ const AWAIT_ONLY = ["await"];
14884+ const YIELD_ONLY = ["yield"];
14885+ const AWAIT_AND_YIELD = ["await", "yield"];
14886+
14887+ /**
14888+ * The names the scope around a node reserves: `await` inside an async
14889+ * function, `yield` inside a generator, both inside an async generator. A
14890+ * plain arrow reserves neither, which engines read as a fresh context for the
14891+ * name though not for the operator.
14892+ * @param {CompressorShape} compressor the compressor, for the parent chain
14893+ * @returns {string[] | undefined} the names, or undefined where none is reserved
14894+ */
14895+ const reservedAround = (compressor) => {
14896+ for (let level = 0; ; level++) {
14897+ const parent = compressor.parent(level);
14898+ if (!parent) return undefined;
14899+ // A static block is strict and awaiting: `await` is a keyword there
14900+ // whatever surrounds it, and no input can name `yield` inside one.
14901+ if (A.isClassStaticBlockNode(parent)) return AWAIT_ONLY;
14902+ if (A.isLambdaNode(parent)) {
14903+ if (parent.async) {
14904+ return parent.generator ? AWAIT_AND_YIELD : AWAIT_ONLY;
14905+ }
14906+ return parent.generator ? YIELD_ONLY : undefined;
14907+ }
14908+ }
14909+ };
14910+
14911+ /**
14912+ * Whether a function names a word where its destination reserves it. A nested
14913+ * function sets a context of its own, except that an arrow's parameters read
14914+ * the scope around it, so those answer to the destination too.
14915+ * @param {Node} lambda the function
14916+ * @param {string[]} reserved the names
14917+ * @returns {boolean} true where one of them would land where it is a keyword
14918+ */
14919+ const namesInOwnContext = (lambda, reserved) => {
14920+ let names = false;
14921+ const walker = A.createWalker((/** @type {Node} */ inner) => {
14922+ if (names) return true;
14923+ if (A.isSymbolNode(inner) && reserved.includes(inner.name)) {
14924+ names = true;
14925+ return true;
14926+ }
14927+ if (inner !== lambda && A.isLambdaNode(inner)) {
14928+ // Only an arrow's parameters read the scope around it; no body does.
14929+ if (A.isArrowNode(inner)) {
14930+ for (const parameter of inner.params) {
14931+ A.walkNode(parameter, walker);
14932+ }
14933+ }
14934+ return true;
14935+ }
14936+ });
14937+ A.walkNode(lambda, walker);
14938+ return names;
14939+ };
14940+
14941+ modules.reservedAround = reservedAround;
14942+ modules.namesInOwnContext = namesInOwnContext;
14943+
14944+ /**
14945+ * The function a call would inline, read through a reference whose value the
14946+ * compressor knows.
14947+ * @param {Node} call the call
14948+ * @returns {Node | undefined} the function, or undefined where there is none
14949+ */
14950+ const inlinedLambda = (call) => {
14951+ const { callee } = call;
14952+ if (A.isLambdaNode(callee)) return callee;
14953+ if (A.isSymbolRefNode(callee)) {
14954+ const fixed = ast.fixedValue(callee);
14955+ if (fixed && A.isLambdaNode(fixed)) return fixed;
14956+ }
14957+ return undefined;
14958+ };
14959+
14960+ // terser inlines a function naming `await` or `yield` into an async function
14961+ // or a generator, where the name is a keyword, so its output does not parse.
14962+ // Only the inline is held back, so the call still optimizes otherwise.
14963+ wrapOptimize("Call", (original) => (self, compressor) => {
14964+ const reserved = reservedAround(compressor);
14965+ if (reserved === undefined) return original(self, compressor);
14966+ const lambda = inlinedLambda(self);
14967+ if (lambda === undefined || !namesInOwnContext(lambda, reserved)) {
14968+ return original(self, compressor);
14969+ }
14970+ return withOptionsOff(compressor, ["inline"], () =>
14971+ original(self, compressor)
14972+ );
14973+ });
14974+
1488114975 /** @type {WeakMap<CompressorShape, Set<Node>>} */
1488214976 const defaultsWithEffects = new WeakMap();
1488314977
@@ -15016,9 +15110,10 @@ const installImprove = (modules) => {
1501615110 * nothing in it is the function's own, no `var`, function declaration,
1501715111 * `return`, label, directive or context, and no `eval` or `with`.
1501815112 * @param {Node} fn the function
15113+ * @param {string[] | undefined} reserved the names the caller's scope reserves
1501915114 * @returns {boolean} true when its body can stand in for the call
1502015115 */
15021- const runsAsBlock = (fn) => {
15116+ const runsAsBlock = (fn, reserved ) => {
1502215117 if (
1502315118 fn.params.length > 0 ||
1502415119 fn.id ||
@@ -15049,16 +15144,24 @@ const installImprove = (modules) => {
1504915144 if (A.isLambdaNode(node)) return true;
1505015145 })
1505115146 );
15052- return !own && (A.isArrowNode(fn) || !readsFunctionContext(fn));
15147+ // An arrow shares the `this` around it, so only a name the caller's own
15148+ // scope reserves keeps its body from running in place there.
15149+ return (
15150+ !own &&
15151+ (A.isArrowNode(fn)
15152+ ? reserved === undefined || !namesInOwnContext(fn, reserved)
15153+ : !readsFunctionContext(fn))
15154+ );
1505315155 };
1505415156
1505515157 /**
1505615158 * The function an expression calls in place, with nothing passed and its
1505715159 * value dropped, when its body runs the same as a block there.
1505815160 * @param {Node} expression an expression whose value is dropped
15161+ * @param {string[] | undefined} reserved the names the caller's scope reserves
1505915162 * @returns {Node | undefined} the function
1506015163 */
15061- const calledInPlace = (expression) => {
15164+ const calledInPlace = (expression, reserved ) => {
1506215165 let call = expression;
1506315166 // The value is dropped, so a `!` or `void` before the call reads nothing.
1506415167 while (
@@ -15076,11 +15179,20 @@ const installImprove = (modules) => {
1507615179 return undefined;
1507715180 }
1507815181 const fn = call.callee;
15079- return (A.isFunctionNode(fn) || A.isArrowNode(fn)) && runsAsBlock(fn)
15182+ return (A.isFunctionNode(fn) || A.isArrowNode(fn)) &&
15183+ runsAsBlock(fn, reserved)
1508015184 ? fn
1508115185 : undefined;
1508215186 };
1508315187
15188+ // The `correct` phase, installed just before this one, answers what a scope
15189+ // reserves and which names a body would write there; both phases hold to one
15190+ // reading of the rule.
15191+ /** @type {(walker: CompressorShape) => string[] | undefined} */
15192+ const reservedAround = modules.reservedAround;
15193+ /** @type {(lambda: Node, reserved: string[]) => boolean} */
15194+ const namesInOwnContext = modules.namesInOwnContext;
15195+
1508415196 /** @type {(node: Node) => boolean} */
1508515197 const canBeEvictedFromBlock = modules.common.can_be_evicted_from_block;
1508615198 /** @type {(orig: Node, expressions: Node[]) => Node} */
@@ -15089,16 +15201,17 @@ const installImprove = (modules) => {
1508915201 /**
1509015202 * Whether a statement calls a function in place that runs as a block there.
1509115203 * @param {Node} statement a statement whose value is dropped
15204+ * @param {string[] | undefined} reserved the names the caller's scope reserves
1509215205 * @returns {boolean} true when `runInPlace` rewrites it
1509315206 */
15094- const runsInPlace = (statement) => {
15207+ const runsInPlace = (statement, reserved ) => {
1509515208 /** @type {Node} */
1509615209 const value = statement.expression;
1509715210 if (!A.isSequenceNode(value)) {
15098- return calledInPlace(value) !== undefined;
15211+ return calledInPlace(value, reserved ) !== undefined;
1509915212 }
1510015213 for (const expression of value.expressions) {
15101- if (calledInPlace(expression) !== undefined) return true;
15214+ if (calledInPlace(expression, reserved ) !== undefined) return true;
1510215215 }
1510315216 return false;
1510415217 };
@@ -15107,9 +15220,10 @@ const installImprove = (modules) => {
1510715220 * A statement's expressions as statements, each function it calls in place
1510815221 * written as its body, or as a block where that declares something scoped.
1510915222 * @param {Node} statement a statement whose value is dropped
15223+ * @param {string[] | undefined} reserved the names the caller's scope reserves
1511015224 * @returns {Node[] | undefined} the statements, or nothing to rewrite
1511115225 */
15112- const runInPlace = (statement) => {
15226+ const runInPlace = (statement, reserved ) => {
1511315227 /** @type {Node} */
1511415228 const value = statement.expression;
1511515229 /** @type {Node[]} */
@@ -15120,7 +15234,7 @@ const installImprove = (modules) => {
1512015234 let pending = [];
1512115235 let rewritten = false;
1512215236 for (const expression of expressions) {
15123- const fn = calledInPlace(expression);
15237+ const fn = calledInPlace(expression, reserved );
1512415238 if (!fn) {
1512515239 pending.push(expression);
1512615240 continue;
@@ -15163,7 +15277,7 @@ const installImprove = (modules) => {
1516315277 if (
1516415278 callingInPlace !== compressor &&
1516515279 improvements.enabled &&
15166- runsInPlace(self)
15280+ runsInPlace(self, reservedAround(compressor) )
1516715281 ) {
1516815282 callingInPlace = compressor;
1516915283 }
@@ -15195,19 +15309,26 @@ const installImprove = (modules) => {
1519515309 callingInPlace = undefined;
1519615310 // Read the tree once, writing nothing, then rewrite only the statements
1519715311 // found, innermost first, so a body moved out already holds its own.
15198- /** @type {[Node, Node][]} */
15312+ /** @type {[Node, Node, string[] | undefined ][]} */
1519915313 const found = [];
1520015314 const walker = A.createWalker(
1520115315 (/** @type {Node} */ node, /** @type {() => void} */ descend) => {
1520215316 if (!A.isSimpleStatementNode(node)) return;
1520315317 descend();
15204- if (runsInPlace(node)) found.push([node, walker.parent()]);
15318+ // The stack is gone once the walk ends, so the names the scope
15319+ // around this statement reserves are read while it is still here.
15320+ const reserved = reservedAround(walker);
15321+ if (runsInPlace(node, reserved)) {
15322+ found.push([node, walker.parent(), reserved]);
15323+ }
1520515324 return true;
1520615325 }
1520715326 );
1520815327 walkNode(compressed, walker);
15209- for (const [statement, parent] of found) {
15210- const statements = /** @type {Node[]} */ (runInPlace(statement));
15328+ for (const [statement, parent, reserved] of found) {
15329+ const statements =
15330+ /** @type {Node[]} */
15331+ (runInPlace(statement, reserved));
1521115332 /** @type {Node[] | undefined} */
1521215333 const body = A.bodyOf(parent);
1521315334 const list = Array.isArray(body) ? body : undefined;
0 commit comments