Skip to content

Commit

Permalink
fix: uses a fresh Set of included labels when checking effects of chi…
Browse files Browse the repository at this point in the history
…ldren
  • Loading branch information
TrickyPi committed Dec 17, 2023
1 parent 4319406 commit 0c6efc1
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 23 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ module.exports = {
}
],
'sort-keys': ['error', 'asc', { caseSensitive: false }],
'unicorn/consistent-destructuring': 'off',
'unicorn/filename-case': 'off',
'unicorn/no-array-callback-reference': 'off',
'unicorn/no-array-reduce': 'off',
Expand Down
10 changes: 4 additions & 6 deletions src/Chunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1021,16 +1021,16 @@ export default class Chunk {
facadeChunk: this.facadeChunkByModule.get(resolution),
node,
resolution
}
}
: resolution instanceof ExternalModule
? {
? {
chunk: null,
externalChunk: this.externalChunkByModule.get(resolution)!,
facadeChunk: null,
node,
resolution
}
: { chunk: null, externalChunk: null, facadeChunk: null, node, resolution }
}
: { chunk: null, externalChunk: null, facadeChunk: null, node, resolution }
);
}
}
Expand Down Expand Up @@ -1233,7 +1233,6 @@ export default class Chunk {
!renderOptions.accessedDocumentCurrentScript &&
formatsMaybeAccessDocumentCurrentScript.includes(format)
) {
// eslint-disable-next-line unicorn/consistent-destructuring
this.accessedGlobalsByScope.get(module.scope)?.delete(DOCUMENT_CURRENT_SCRIPT);
}
renderOptions.accessedDocumentCurrentScript = false;
Expand Down Expand Up @@ -1273,7 +1272,6 @@ export default class Chunk {

if (hoistedSource) magicString.prepend(hoistedSource + n + n);

// eslint-disable-next-line unicorn/consistent-destructuring
if (this.needsExportsShim) {
magicString.prepend(`${n}${cnst} ${MISSING_EXPORT_SHIM_VARIABLE}${_}=${_}void 0;${n}${n}`);
}
Expand Down
4 changes: 0 additions & 4 deletions src/ast/nodes/IfStatement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,10 @@ export default class IfStatement extends StatementBase implements DeoptimizableE
if (typeof testValue === 'symbol') {
const { brokenFlow } = context;
if (this.consequent.hasEffects(context)) return true;
// eslint-disable-next-line unicorn/consistent-destructuring
const consequentBrokenFlow = context.brokenFlow;
context.brokenFlow = brokenFlow;
if (this.alternate === null) return false;
if (this.alternate.hasEffects(context)) return true;
// eslint-disable-next-line unicorn/consistent-destructuring
context.brokenFlow = context.brokenFlow && consequentBrokenFlow;
return false;
}
Expand Down Expand Up @@ -166,13 +164,11 @@ export default class IfStatement extends StatementBase implements DeoptimizableE
let consequentBrokenFlow = false;
if (this.consequent.shouldBeIncluded(context)) {
this.consequent.include(context, false, { asSingleStatement: true });
// eslint-disable-next-line unicorn/consistent-destructuring
consequentBrokenFlow = context.brokenFlow;
context.brokenFlow = brokenFlow;
}
if (this.alternate?.shouldBeIncluded(context)) {
this.alternate.include(context, false, { asSingleStatement: true });
// eslint-disable-next-line unicorn/consistent-destructuring
context.brokenFlow = context.brokenFlow && consequentBrokenFlow;
}
}
Expand Down
23 changes: 13 additions & 10 deletions src/ast/nodes/LabeledStatement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,33 @@ export default class LabeledStatement extends StatementBase {
declare type: NodeType.tLabeledStatement;

hasEffects(context: HasEffectsContext): boolean {
const brokenFlow = context.brokenFlow;
const { brokenFlow, includedLabels } = context;
context.ignore.labels.add(this.label.name);
if (this.body.hasEffects(context)) return true;
context.ignore.labels.delete(this.label.name);
if (context.includedLabels.has(this.label.name)) {
context.includedLabels.delete(this.label.name);
context.brokenFlow = brokenFlow;
context.includedLabels = new Set<string>();
let bodyHasEffects = false;
if (this.body.hasEffects(context)) {
bodyHasEffects = true;
} else {
context.ignore.labels.delete(this.label.name);
if (context.includedLabels.has(this.label.name)) {
context.includedLabels.delete(this.label.name);
context.brokenFlow = brokenFlow;
}
}
return false;
context.includedLabels = new Set([...includedLabels, ...context.includedLabels]);
return bodyHasEffects;
}

include(context: InclusionContext, includeChildrenRecursively: IncludeChildren): void {
this.included = true;
const { brokenFlow, includedLabels } = context;
context.includedLabels = new Set<string>();
this.body.include(context, includeChildrenRecursively);
// eslint-disable-next-line unicorn/consistent-destructuring
if (includeChildrenRecursively || context.includedLabels.has(this.label.name)) {
this.label.include();
// eslint-disable-next-line unicorn/consistent-destructuring
context.includedLabels.delete(this.label.name);
context.brokenFlow = brokenFlow;
}
// eslint-disable-next-line unicorn/consistent-destructuring
context.includedLabels = new Set([...includedLabels, ...context.includedLabels]);
}

Expand Down
2 changes: 0 additions & 2 deletions src/ast/nodes/SwitchStatement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export default class SwitchStatement extends StatementBase {
let onlyHasBrokenFlow = true;
for (const switchCase of this.cases) {
if (switchCase.hasEffects(context)) return true;
// eslint-disable-next-line unicorn/consistent-destructuring
onlyHasBrokenFlow &&= context.brokenFlow && !context.hasBreak;
context.hasBreak = false;
context.brokenFlow = brokenFlow;
Expand Down Expand Up @@ -68,7 +67,6 @@ export default class SwitchStatement extends StatementBase {
}
if (isCaseIncluded) {
switchCase.include(context, includeChildrenRecursively);
// eslint-disable-next-line unicorn/consistent-destructuring
onlyHasBrokenFlow &&= context.brokenFlow && !context.hasBreak;
context.hasBreak = false;
context.brokenFlow = brokenFlow;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ outer: {
}

outer: {
inner:/* retained comment */ {
inner: /* retained comment */ {
console.log('retained');
break outer;
console.log('removed');
Expand Down Expand Up @@ -59,6 +59,22 @@ outer: {
}
}

// removed
outer: {
if (globalThis.unknown) {
break outer;
} else {
(() => {
inner: {
outer: {
break inner;
}
console.log('removed');
}
})();
}
}

function withConsequentReturn() {
outer: {
inner: {
Expand Down

0 comments on commit 0c6efc1

Please sign in to comment.