Skip to content

Commit a971360

Browse files
crisbetoalxhub
authored andcommitted
fix(compiler-cli): gracefully fall back if const enum cannot be passed through (#59815)
Adds some logic so that if we can't produce a runtime representation of an enum, the dev server can fall back to refreshing the page. PR Close #59815
1 parent bae94b8 commit a971360

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

packages/compiler-cli/src/ngtsc/hmr/src/extract_dependencies.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export function extractHmrDependencies(
3939
): {
4040
local: {name: string; runtimeRepresentation: o.Expression}[];
4141
external: R3HmrNamespaceDependency[];
42-
} {
42+
} | null {
4343
const name = ts.isClassDeclaration(node) && node.name ? node.name.text : null;
4444
const visitor = new PotentialTopLevelReadsVisitor();
4545
const sourceFile = node.getSourceFile();
@@ -70,10 +70,13 @@ export function extractHmrDependencies(
7070
const readName = readNode instanceof o.ReadVarExpr ? readNode.name : readNode.text;
7171

7272
if (readName !== name && !seenLocals.has(readName) && availableTopLevel.has(readName)) {
73-
local.push({
74-
name: readName,
75-
runtimeRepresentation: getRuntimeRepresentation(readNode, reflection, evaluator),
76-
});
73+
const runtimeRepresentation = getRuntimeRepresentation(readNode, reflection, evaluator);
74+
75+
if (runtimeRepresentation === null) {
76+
return null;
77+
}
78+
79+
local.push({name: readName, runtimeRepresentation});
7780
seenLocals.add(readName);
7881
}
7982
}
@@ -94,7 +97,7 @@ function getRuntimeRepresentation(
9497
node: o.ReadVarExpr | ts.Identifier,
9598
reflection: ReflectionHost,
9699
evaluator: PartialEvaluator,
97-
): o.Expression {
100+
): o.Expression | null {
98101
if (node instanceof o.ReadVarExpr) {
99102
return o.variable(node.name);
100103
}
@@ -120,6 +123,11 @@ function getRuntimeRepresentation(
120123
quoted: false,
121124
value: o.literal(value.resolved),
122125
});
126+
} else {
127+
// TS is pretty restrictive about what values can be in a const enum so our evaluator
128+
// should be able to handle them, however if we happen to hit such a case, we return null
129+
// so the HMR update can be invalidated.
130+
return null;
123131
}
124132
}
125133

packages/compiler-cli/src/ngtsc/hmr/src/metadata.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ export function extractHmrMetatadata(
6262
reflection,
6363
evaluator,
6464
);
65+
66+
if (dependencies === null) {
67+
return null;
68+
}
69+
6570
const meta: R3HmrMetadata = {
6671
type: new o.WrappedNodeExpr(clazz.name),
6772
className: clazz.name.text,

0 commit comments

Comments
 (0)