/
TryStatement.ts
54 lines (50 loc) · 1.91 KB
/
TryStatement.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { NormalizedTreeshakingOptions } from '../../rollup/types';
import { HasEffectsContext, InclusionContext } from '../ExecutionContext';
import BlockStatement from './BlockStatement';
import CatchClause from './CatchClause';
import * as NodeType from './NodeType';
import { IncludeChildren, INCLUDE_PARAMETERS, StatementBase } from './shared/Node';
export default class TryStatement extends StatementBase {
block!: BlockStatement;
finalizer!: BlockStatement | null;
handler!: CatchClause | null;
type!: NodeType.tTryStatement;
private directlyIncluded = false;
private includedLabelsAfterBlock: string[] | null = null;
hasEffects(context: HasEffectsContext): boolean {
return (
((this.context.options.treeshake as NormalizedTreeshakingOptions).tryCatchDeoptimization
? this.block.body.length > 0
: this.block.hasEffects(context)) ||
(this.finalizer !== null && this.finalizer.hasEffects(context))
);
}
include(context: InclusionContext, includeChildrenRecursively: IncludeChildren) {
const tryCatchDeoptimization = (this.context.options.treeshake as NormalizedTreeshakingOptions)
?.tryCatchDeoptimization;
const { brokenFlow } = context;
if (!this.directlyIncluded || !tryCatchDeoptimization) {
this.included = true;
this.directlyIncluded = true;
this.block.include(
context,
tryCatchDeoptimization ? INCLUDE_PARAMETERS : includeChildrenRecursively
);
if (context.includedLabels.size > 0) {
this.includedLabelsAfterBlock = [...context.includedLabels];
}
context.brokenFlow = brokenFlow;
} else if (this.includedLabelsAfterBlock) {
for (const label of this.includedLabelsAfterBlock) {
context.includedLabels.add(label);
}
}
if (this.handler !== null) {
this.handler.include(context, includeChildrenRecursively);
context.brokenFlow = brokenFlow;
}
if (this.finalizer !== null) {
this.finalizer.include(context, includeChildrenRecursively);
}
}
}