Skip to content

Commit 22f9ee1

Browse files
crisbetoatscott
authored andcommitted
refactor(compiler): require a reference in DirectiveMeta
Requires the `DirectiveMeta` to have a `ref` so that we can find duplicates easily.
1 parent d15ceff commit 22f9ee1

3 files changed

Lines changed: 25 additions & 1 deletion

File tree

packages/compiler-cli/src/ngtsc/imports/src/references.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {Expression} from '@angular/compiler';
1010
import ts from 'typescript';
1111

1212
import {AmbientImport} from '../../reflection';
13-
import {identifierOfNode} from '../../util/src/typescript';
13+
import {getSourceFile, identifierOfNode} from '../../util/src/typescript';
1414

1515
export interface OwningModule {
1616
specifier: string;
@@ -57,6 +57,8 @@ export class Reference<T extends ts.Node = ts.Node> {
5757

5858
readonly isAmbient: boolean;
5959

60+
readonly key: string;
61+
6062
constructor(
6163
readonly node: T,
6264
bestGuessOwningModule: OwningModule | AmbientImport | null = null,
@@ -70,9 +72,18 @@ export class Reference<T extends ts.Node = ts.Node> {
7072
}
7173

7274
const id = identifierOfNode(node);
75+
const sourceFile = getSourceFile(node);
76+
7377
if (id !== null) {
7478
this.identifiers.push(id);
7579
}
80+
81+
// The source file might not be defined for synthetic nodes.
82+
if (sourceFile) {
83+
this.key = `${sourceFile.fileName}#${node.getStart()}`;
84+
} else {
85+
this.key = `${this.bestGuessOwningModule?.specifier}#${id?.text}#${id?.getStart()}#${id?.getEnd()}`;
86+
}
7687
}
7788

7889
/**

packages/compiler/src/render3/view/t2_api.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ export interface DirectiveMeta {
9898
*/
9999
name: string;
100100

101+
/** Reference to the directive declaration site. */
102+
ref: {
103+
/** Key that uniquely identifies the reference. */
104+
key: string;
105+
106+
// Normally we have some more fields here depending on where the reference originated from.
107+
};
108+
101109
/** The selector for the directive or `null` if there isn't one. */
102110
selector: string | null;
103111

packages/compiler/test/render3/view/binding_spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import {CssSelector, SelectorlessMatcher, SelectorMatcher} from '../../../src/di
1616

1717
import {findExpression} from './util';
1818

19+
let keyCounter = 0;
20+
1921
function makeDirectiveMeta(config: {
2022
name: string;
2123
selector: string | null;
@@ -28,6 +30,9 @@ function makeDirectiveMeta(config: {
2830
}): DirectiveMeta {
2931
return {
3032
name: config.name,
33+
ref: {
34+
key: `${config.name}#${keyCounter++}`,
35+
},
3136
exportAs: config.exportAs ?? null,
3237
inputs: ClassPropertyMapping.fromMappedObject(config.inputs || {}),
3338
outputs: ClassPropertyMapping.fromMappedObject(config.outputs || {}),

0 commit comments

Comments
 (0)