Skip to content

Commit eb43e92

Browse files
crisbetokirjs
authored andcommitted
refactor(compiler): account for new assignment AST (#61682)
Reworks the places that were depending on `PropertyWrite` and `KeyedWrite` to account for the new AST structure. PR Close #61682
1 parent 08ee693 commit eb43e92

25 files changed

Lines changed: 102 additions & 242 deletions

File tree

‎packages/compiler-cli/src/ngtsc/indexer/src/template.ts‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
ImplicitReceiver,
1414
ParseSourceSpan,
1515
PropertyRead,
16-
PropertyWrite,
1716
TmplAstBoundAttribute,
1817
TmplAstComponent,
1918
TmplAstDirective,
@@ -145,11 +144,6 @@ class TemplateVisitor extends CombinedRecursiveAstVisitor {
145144
super.visitPropertyRead(ast, null);
146145
}
147146

148-
override visitPropertyWrite(ast: PropertyWrite) {
149-
this.visitIdentifier(ast, IdentifierKind.Property);
150-
super.visitPropertyWrite(ast, null);
151-
}
152-
153147
override visitBoundAttribute(attribute: TmplAstBoundAttribute): void {
154148
const previous = this.currentAstWithSource;
155149
this.currentAstWithSource = {
@@ -367,9 +361,9 @@ class TemplateVisitor extends CombinedRecursiveAstVisitor {
367361
// The source span of the requested AST starts at a location that is offset from the expression.
368362
let identifierStart = ast.sourceSpan.start - absoluteOffset;
369363

370-
if (ast instanceof PropertyRead || ast instanceof PropertyWrite) {
371-
// For `PropertyRead` and `PropertyWrite`, the identifier starts at the `nameSpan`, not
372-
// necessarily the `sourceSpan`.
364+
if (ast instanceof PropertyRead) {
365+
// For `PropertyRead` and the identifier starts at the `nameSpan`,
366+
// not necessarily the `sourceSpan`.
373367
identifierStart = ast.nameSpan.start - absoluteOffset;
374368
}
375369

‎packages/compiler-cli/src/ngtsc/indexer/test/template_spec.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ runInEachFileSystem(() => {
342342
});
343343
});
344344

345-
describe('generates identifiers for PropertyWrites', () => {
345+
describe('generates identifiers for property writes', () => {
346346
it('should discover property writes in bound events', () => {
347347
const template = '<div (click)="foo=bar"></div>';
348348
const refs = getTemplateIdentifiers(bind(template));

‎packages/compiler-cli/src/ngtsc/typecheck/src/completion.ts‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
ImplicitReceiver,
1313
LiteralPrimitive,
1414
PropertyRead,
15-
PropertyWrite,
1615
SafePropertyRead,
1716
TmplAstLetDeclaration,
1817
TmplAstNode,
@@ -141,16 +140,14 @@ export class CompletionEngine {
141140
};
142141
}
143142

144-
getExpressionCompletionLocation(
145-
expr: PropertyRead | PropertyWrite | SafePropertyRead,
146-
): TcbLocation | null {
143+
getExpressionCompletionLocation(expr: PropertyRead | SafePropertyRead): TcbLocation | null {
147144
if (this.expressionCompletionCache.has(expr)) {
148145
return this.expressionCompletionCache.get(expr)!;
149146
}
150147

151148
// Completion works inside property reads and method calls.
152149
let tsExpr: ts.PropertyAccessExpression | null = null;
153-
if (expr instanceof PropertyRead || expr instanceof PropertyWrite) {
150+
if (expr instanceof PropertyRead) {
154151
// Non-safe navigation operations are trivial: `foo.bar` or `foo.bar()`
155152
tsExpr = findFirstMatchingNode(this.tcb, {
156153
filter: ts.isPropertyAccessExpression,

‎packages/compiler-cli/src/ngtsc/typecheck/src/expression.ts‎

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@ import {
1919
ImplicitReceiver,
2020
Interpolation,
2121
KeyedRead,
22-
KeyedWrite,
2322
LiteralArray,
2423
LiteralMap,
2524
LiteralPrimitive,
2625
NonNullAssert,
2726
ParenthesizedExpression,
2827
PrefixNot,
2928
PropertyRead,
30-
PropertyWrite,
3129
SafeCall,
3230
SafeKeyedRead,
3331
SafePropertyRead,
@@ -72,6 +70,7 @@ const BINARY_OPS = new Map<string, ts.BinaryOperator>([
7270
['>', ts.SyntaxKind.GreaterThanToken],
7371
['<=', ts.SyntaxKind.LessThanEqualsToken],
7472
['>=', ts.SyntaxKind.GreaterThanEqualsToken],
73+
['=', ts.SyntaxKind.EqualsToken],
7574
['==', ts.SyntaxKind.EqualsEqualsToken],
7675
['===', ts.SyntaxKind.EqualsEqualsEqualsToken],
7776
['*', ts.SyntaxKind.AsteriskToken],
@@ -209,19 +208,6 @@ class AstTranslator implements AstVisitor {
209208
return node;
210209
}
211210

212-
visitKeyedWrite(ast: KeyedWrite): ts.Expression {
213-
const receiver = wrapForDiagnostics(this.translate(ast.receiver));
214-
const left = ts.factory.createElementAccessExpression(receiver, this.translate(ast.key));
215-
// TODO(joost): annotate `left` with the span of the element access, which is not currently
216-
// available on `ast`.
217-
const right = wrapForTypeChecker(this.translate(ast.value));
218-
const node = wrapForDiagnostics(
219-
ts.factory.createBinaryExpression(left, ts.SyntaxKind.EqualsToken, right),
220-
);
221-
addParseSpanInfo(node, ast.sourceSpan);
222-
return node;
223-
}
224-
225211
visitLiteralArray(ast: LiteralArray): ts.Expression {
226212
const elements = ast.expressions.map((expr) => this.translate(expr));
227213
const literal = ts.factory.createArrayLiteralExpression(elements);
@@ -305,30 +291,6 @@ class AstTranslator implements AstVisitor {
305291
return node;
306292
}
307293

308-
visitPropertyWrite(ast: PropertyWrite): ts.Expression {
309-
const receiver = wrapForDiagnostics(this.translate(ast.receiver));
310-
const left = ts.factory.createPropertyAccessExpression(receiver, ast.name);
311-
addParseSpanInfo(left, ast.nameSpan);
312-
// TypeScript reports assignment errors on the entire lvalue expression. Annotate the lvalue of
313-
// the assignment with the sourceSpan, which includes receivers, rather than nameSpan for
314-
// consistency of the diagnostic location.
315-
// a.b.c = 1
316-
// ^^^^^^^^^ sourceSpan
317-
// ^ nameSpan
318-
const leftWithPath = wrapForDiagnostics(left);
319-
addParseSpanInfo(leftWithPath, ast.sourceSpan);
320-
// The right needs to be wrapped in parens as well or we cannot accurately match its
321-
// span to just the RHS. For example, the span in `e = $event /*0,10*/` is ambiguous.
322-
// It could refer to either the whole binary expression or just the RHS.
323-
// We should instead generate `e = ($event /*0,10*/)` so we know the span 0,10 matches RHS.
324-
const right = wrapForTypeChecker(this.translate(ast.value));
325-
const node = wrapForDiagnostics(
326-
ts.factory.createBinaryExpression(leftWithPath, ts.SyntaxKind.EqualsToken, right),
327-
);
328-
addParseSpanInfo(node, ast.sourceSpan);
329-
return node;
330-
}
331-
332294
visitSafePropertyRead(ast: SafePropertyRead): ts.Expression {
333295
let node: ts.Expression;
334296
const receiver = wrapForDiagnostics(this.translate(ast.receiver));
@@ -582,9 +544,6 @@ class VeSafeLhsInferenceBugDetector implements AstVisitor {
582544
visitKeyedRead(ast: KeyedRead): boolean {
583545
return false;
584546
}
585-
visitKeyedWrite(ast: KeyedWrite): boolean {
586-
return false;
587-
}
588547
visitLiteralArray(ast: LiteralArray): boolean {
589548
return true;
590549
}
@@ -612,9 +571,6 @@ class VeSafeLhsInferenceBugDetector implements AstVisitor {
612571
visitPropertyRead(ast: PropertyRead): boolean {
613572
return false;
614573
}
615-
visitPropertyWrite(ast: PropertyWrite): boolean {
616-
return false;
617-
}
618574
visitSafePropertyRead(ast: SafePropertyRead): boolean {
619575
return false;
620576
}

‎packages/compiler-cli/src/ngtsc/typecheck/src/oob.ts‎

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
AbsoluteSourceSpan,
1111
BindingPipe,
1212
PropertyRead,
13-
PropertyWrite,
13+
AST,
1414
TmplAstBoundAttribute,
1515
TmplAstBoundEvent,
1616
TmplAstComponent,
@@ -172,11 +172,7 @@ export interface OutOfBandDiagnosticRecorder {
172172
): void;
173173

174174
/** Reports cases where users are writing to `@let` declarations. */
175-
illegalWriteToLetDeclaration(
176-
id: TypeCheckId,
177-
node: PropertyWrite,
178-
target: TmplAstLetDeclaration,
179-
): void;
175+
illegalWriteToLetDeclaration(id: TypeCheckId, node: AST, target: TmplAstLetDeclaration): void;
180176

181177
/** Reports cases where users are accessing an `@let` before it is defined.. */
182178
letUsedBeforeDefinition(id: TypeCheckId, node: PropertyRead, target: TmplAstLetDeclaration): void;
@@ -651,11 +647,7 @@ export class OutOfBandDiagnosticRecorderImpl implements OutOfBandDiagnosticRecor
651647
);
652648
}
653649

654-
illegalWriteToLetDeclaration(
655-
id: TypeCheckId,
656-
node: PropertyWrite,
657-
target: TmplAstLetDeclaration,
658-
): void {
650+
illegalWriteToLetDeclaration(id: TypeCheckId, node: AST, target: TmplAstLetDeclaration): void {
659651
const sourceSpan = this.resolver.toTemplateParseSourceSpan(id, node.sourceSpan);
660652
if (sourceSpan === null) {
661653
throw new Error(`Assertion failure: no SourceLocation found for property write.`);

‎packages/compiler-cli/src/ngtsc/typecheck/src/template_symbol_builder.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import {
1010
AST,
1111
ASTWithName,
1212
ASTWithSource,
13+
Binary,
1314
BindingPipe,
1415
ParseSourceSpan,
1516
PropertyRead,
16-
PropertyWrite,
1717
R3Identifiers,
1818
SafePropertyRead,
1919
TmplAstBoundAttribute,
@@ -800,13 +800,16 @@ export class SymbolBuilder {
800800

801801
let withSpan = expression.sourceSpan;
802802

803-
// The `name` part of a `PropertyWrite` and `ASTWithName` do not have their own
803+
// The `name` part of a property write and `ASTWithName` do not have their own
804804
// AST so there is no way to retrieve a `Symbol` for just the `name` via a specific node.
805805
// Also skipping SafePropertyReads as it breaks nullish coalescing not nullable extended diagnostic
806806
if (
807-
expression instanceof PropertyWrite ||
808-
(expression instanceof ASTWithName && !(expression instanceof SafePropertyRead))
807+
expression instanceof Binary &&
808+
expression.operation === '=' &&
809+
expression.left instanceof PropertyRead
809810
) {
811+
withSpan = expression.left.nameSpan;
812+
} else if (expression instanceof ASTWithName && !(expression instanceof SafePropertyRead)) {
810813
withSpan = expression.nameSpan;
811814
}
812815

‎packages/compiler-cli/src/ngtsc/typecheck/src/type_check_block.ts‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import {
1919
ParsedEventType,
2020
ParseSourceSpan,
2121
PropertyRead,
22-
PropertyWrite,
2322
R3Identifiers,
2423
SafeCall,
2524
SafePropertyRead,
@@ -55,6 +54,7 @@ import {
5554
TransplantedType,
5655
TmplAstComponent,
5756
TmplAstDirective,
57+
Binary,
5858
} from '@angular/compiler';
5959
import ts from 'typescript';
6060

@@ -3115,25 +3115,31 @@ class TcbExpressionTranslator {
31153115
}
31163116
}
31173117
return targetExpression;
3118-
} else if (ast instanceof PropertyWrite && ast.receiver instanceof ImplicitReceiver) {
3119-
const target = this.tcb.boundTarget.getExpressionTarget(ast);
3118+
} else if (
3119+
ast instanceof Binary &&
3120+
ast.operation === '=' &&
3121+
ast.left instanceof PropertyRead &&
3122+
ast.left.receiver instanceof ImplicitReceiver
3123+
) {
3124+
const read = ast.left;
3125+
const target = this.tcb.boundTarget.getExpressionTarget(read);
31203126
if (target === null) {
31213127
return null;
31223128
}
31233129

3124-
const targetExpression = this.getTargetNodeExpression(target, ast);
3125-
const expr = this.translate(ast.value);
3130+
const targetExpression = this.getTargetNodeExpression(target, read);
3131+
const expr = this.translate(ast.right);
31263132
const result = ts.factory.createParenthesizedExpression(
31273133
ts.factory.createBinaryExpression(targetExpression, ts.SyntaxKind.EqualsToken, expr),
31283134
);
3129-
addParseSpanInfo(result, ast.sourceSpan);
3135+
addParseSpanInfo(result, read.sourceSpan);
31303136

31313137
// Ignore diagnostics from TS produced for writes to `@let` and re-report them using
31323138
// our own infrastructure. We can't rely on the TS reporting, because it includes
31333139
// the name of the auto-generated TCB variable name.
31343140
if (target instanceof TmplAstLetDeclaration) {
31353141
markIgnoreDiagnostics(result);
3136-
this.tcb.oobRecorder.illegalWriteToLetDeclaration(this.tcb.id, ast, target);
3142+
this.tcb.oobRecorder.illegalWriteToLetDeclaration(this.tcb.id, read, target);
31373143
}
31383144

31393145
return result;

‎packages/compiler-cli/src/ngtsc/typecheck/template_semantics/src/template_semantics_checker.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
ImplicitReceiver,
1313
ParsedEventType,
1414
PropertyRead,
15-
PropertyWrite,
15+
Binary,
1616
RecursiveAstVisitor,
1717
TmplAstBoundEvent,
1818
TmplAstLetDeclaration,
@@ -76,17 +76,20 @@ class ExpressionsSemanticsVisitor extends RecursiveAstVisitor {
7676
super();
7777
}
7878

79-
override visitPropertyWrite(ast: PropertyWrite, context: TmplAstNode): void {
80-
super.visitPropertyWrite(ast, context);
81-
this.checkForIllegalWriteInEventBinding(ast, context);
79+
override visitBinary(ast: Binary, context: TmplAstNode): void {
80+
if (ast.operation === '=' && ast.left instanceof PropertyRead) {
81+
this.checkForIllegalWriteInEventBinding(ast.left, context);
82+
} else {
83+
super.visitBinary(ast, context);
84+
}
8285
}
8386

8487
override visitPropertyRead(ast: PropertyRead, context: TmplAstNode) {
8588
super.visitPropertyRead(ast, context);
8689
this.checkForIllegalWriteInTwoWayBinding(ast, context);
8790
}
8891

89-
private checkForIllegalWriteInEventBinding(ast: PropertyWrite, context: TmplAstNode) {
92+
private checkForIllegalWriteInEventBinding(ast: PropertyRead, context: TmplAstNode) {
9093
if (!(context instanceof TmplAstBoundEvent) || !(ast.receiver instanceof ImplicitReceiver)) {
9194
return;
9295
}

‎packages/compiler-cli/src/ngtsc/typecheck/test/span_comments_spec.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,14 @@ describe('type check blocks diagnostics', () => {
113113
it('should annotate property writes', () => {
114114
const TEMPLATE = `<div (click)='a.b.c = d'></div>`;
115115
expect(tcbWithSpans(TEMPLATE)).toContain(
116-
'(((((((this).a /*14,15*/) /*14,15*/).b /*16,17*/) /*14,17*/).c /*18,19*/) /*14,23*/ = (((this).d /*22,23*/) /*22,23*/)) /*14,23*/',
116+
'(((((((this).a /*14,15*/) /*14,15*/).b /*16,17*/) /*14,17*/).c /*18,19*/) /*14,21*/) = (((this).d /*22,23*/) /*22,23*/) /*14,23*/;',
117117
);
118118
});
119119

120120
it('should $event property writes', () => {
121121
const TEMPLATE = `<div (click)='a = $event'></div>`;
122122
expect(tcbWithSpans(TEMPLATE)).toContain(
123-
'(((this).a /*14,15*/) /*14,24*/ = ($event /*18,24*/)) /*14,24*/;',
123+
'(((this).a /*14,15*/) /*14,17*/) = ($event /*18,24*/) /*14,24*/;',
124124
);
125125
});
126126

@@ -134,7 +134,7 @@ describe('type check blocks diagnostics', () => {
134134
it('should annotate keyed property writes', () => {
135135
const TEMPLATE = `<div (click)="a[b] = c"></div>`;
136136
expect(tcbWithSpans(TEMPLATE)).toContain(
137-
'((((this).a /*14,15*/) /*14,15*/)[((this).b /*16,17*/) /*16,17*/] = (((this).c /*21,22*/) /*21,22*/)) /*14,22*/',
137+
'((((this).a /*14,15*/) /*14,15*/)[((this).b /*16,17*/) /*16,17*/] /*14,20*/) = (((this).c /*21,22*/) /*21,22*/) /*14,22*/;',
138138
);
139139
});
140140

‎packages/compiler-cli/src/ngtsc/typecheck/testing/index.ts‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
*/
88

99
import {
10+
AST,
1011
BindingPipe,
1112
CssSelector,
1213
ParseSourceFile,
1314
parseTemplate,
1415
ParseTemplateOptions,
1516
PropertyRead,
16-
PropertyWrite,
1717
R3TargetBinder,
1818
SelectorlessMatcher,
1919
SelectorMatcher,
@@ -1027,11 +1027,7 @@ export class NoopOobRecorder implements OutOfBandDiagnosticRecorder {
10271027
illegalForLoopTrackAccess(): void {}
10281028
inaccessibleDeferredTriggerElement(): void {}
10291029
controlFlowPreventingContentProjection(): void {}
1030-
illegalWriteToLetDeclaration(
1031-
id: TypeCheckId,
1032-
node: PropertyWrite,
1033-
target: TmplAstLetDeclaration,
1034-
): void {}
1030+
illegalWriteToLetDeclaration(id: TypeCheckId, node: AST, target: TmplAstLetDeclaration): void {}
10351031
letUsedBeforeDefinition(
10361032
id: TypeCheckId,
10371033
node: PropertyRead,

0 commit comments

Comments
 (0)