Skip to content

Commit

Permalink
fix(compiler-cli): gracefully fall back if const enum cannot be passe…
Browse files Browse the repository at this point in the history
…d 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
  • Loading branch information
crisbeto authored and alxhub committed Feb 3, 2025
1 parent bae94b8 commit a971360
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
20 changes: 14 additions & 6 deletions packages/compiler-cli/src/ngtsc/hmr/src/extract_dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function extractHmrDependencies(
): {
local: {name: string; runtimeRepresentation: o.Expression}[];
external: R3HmrNamespaceDependency[];
} {
} | null {
const name = ts.isClassDeclaration(node) && node.name ? node.name.text : null;
const visitor = new PotentialTopLevelReadsVisitor();
const sourceFile = node.getSourceFile();
Expand Down Expand Up @@ -70,10 +70,13 @@ export function extractHmrDependencies(
const readName = readNode instanceof o.ReadVarExpr ? readNode.name : readNode.text;

if (readName !== name && !seenLocals.has(readName) && availableTopLevel.has(readName)) {
local.push({
name: readName,
runtimeRepresentation: getRuntimeRepresentation(readNode, reflection, evaluator),
});
const runtimeRepresentation = getRuntimeRepresentation(readNode, reflection, evaluator);

if (runtimeRepresentation === null) {
return null;
}

local.push({name: readName, runtimeRepresentation});
seenLocals.add(readName);
}
}
Expand All @@ -94,7 +97,7 @@ function getRuntimeRepresentation(
node: o.ReadVarExpr | ts.Identifier,
reflection: ReflectionHost,
evaluator: PartialEvaluator,
): o.Expression {
): o.Expression | null {
if (node instanceof o.ReadVarExpr) {
return o.variable(node.name);
}
Expand All @@ -120,6 +123,11 @@ function getRuntimeRepresentation(
quoted: false,
value: o.literal(value.resolved),
});
} else {
// TS is pretty restrictive about what values can be in a const enum so our evaluator
// should be able to handle them, however if we happen to hit such a case, we return null
// so the HMR update can be invalidated.
return null;
}
}

Expand Down
5 changes: 5 additions & 0 deletions packages/compiler-cli/src/ngtsc/hmr/src/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ export function extractHmrMetatadata(
reflection,
evaluator,
);

if (dependencies === null) {
return null;
}

const meta: R3HmrMetadata = {
type: new o.WrappedNodeExpr(clazz.name),
className: clazz.name.text,
Expand Down

0 comments on commit a971360

Please sign in to comment.