Skip to content

Commit f9043e2

Browse files
atscottmmalerba
authored andcommitted
fix(compiler-cli): ensure template IDs are not reused if a source file changes (#60152)
This commit fixes a bug where nodes are reused across incremental compilations. The source file's next template ID is lost because a new source file is created but nodes may still be retained. PR Close #60152
1 parent b326f48 commit f9043e2

File tree

1 file changed

+8
-17
lines changed
  • packages/compiler-cli/src/ngtsc/typecheck/diagnostics/src

1 file changed

+8
-17
lines changed

packages/compiler-cli/src/ngtsc/typecheck/diagnostics/src/id.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,19 @@ import {DeclarationNode} from '../../../reflection';
1111

1212
import {TemplateId} from '../../api';
1313

14-
const TEMPLATE_ID = Symbol('ngTemplateId');
15-
const NEXT_TEMPLATE_ID = Symbol('ngNextTemplateId');
16-
17-
interface HasTemplateId {
18-
[TEMPLATE_ID]: TemplateId;
19-
}
14+
const TEMPLATE_ID_MAP = Symbol('ngTemplateId');
2015

2116
interface HasNextTemplateId {
22-
[NEXT_TEMPLATE_ID]: number;
17+
[TEMPLATE_ID_MAP]: Map<ts.Node, TemplateId>;
2318
}
2419

2520
export function getTemplateId(clazz: DeclarationNode): TemplateId {
26-
const node = clazz as ts.Declaration & Partial<HasTemplateId>;
27-
if (node[TEMPLATE_ID] === undefined) {
28-
node[TEMPLATE_ID] = allocateTemplateId(node.getSourceFile());
21+
const sf = clazz.getSourceFile() as ts.SourceFile & Partial<HasNextTemplateId>;
22+
if (sf[TEMPLATE_ID_MAP] === undefined) {
23+
sf[TEMPLATE_ID_MAP] = new Map();
2924
}
30-
return node[TEMPLATE_ID]!;
31-
}
32-
33-
function allocateTemplateId(sf: ts.SourceFile & Partial<HasNextTemplateId>): TemplateId {
34-
if (sf[NEXT_TEMPLATE_ID] === undefined) {
35-
sf[NEXT_TEMPLATE_ID] = 1;
25+
if (sf[TEMPLATE_ID_MAP].get(clazz) === undefined) {
26+
sf[TEMPLATE_ID_MAP].set(clazz, `tcb${sf[TEMPLATE_ID_MAP].size + 1}` as TemplateId);
3627
}
37-
return `tcb${sf[NEXT_TEMPLATE_ID]!++}` as TemplateId;
28+
return sf[TEMPLATE_ID_MAP].get(clazz)!;
3829
}

0 commit comments

Comments
 (0)