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 a0c0ad0
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 17 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
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 a0c0ad0

Please sign in to comment.