Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit cf53743

Browse filesBrowse files
author
Andy
authored
In isInPropertyInitializer, don't bail out at a PropertyAssignment (microsoft#18449)
1 parent be5c00f commit cf53743
Copy full SHA for cf53743

4 files changed

+48-1Lines changed: 48 additions & 1 deletion

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/compiler/checker.ts‎

Copy file name to clipboardExpand all lines: src/compiler/checker.ts
+15-1Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14753,7 +14753,7 @@ namespace ts {
1475314753
return;
1475414754
}
1475514755

14756-
if (findAncestor(node, node => node.kind === SyntaxKind.PropertyDeclaration ? true : isExpression(node) ? false : "quit") &&
14756+
if (isInPropertyInitializer(node) &&
1475714757
!isBlockScopedNameDeclaredBeforeUse(valueDeclaration, right)
1475814758
&& !isPropertyDeclaredInAncestorClass(prop)) {
1475914759
error(right, Diagnostics.Block_scoped_variable_0_used_before_its_declaration, unescapeLeadingUnderscores(right.escapedText));
@@ -14766,6 +14766,20 @@ namespace ts {
1476614766
}
1476714767
}
1476814768

14769+
function isInPropertyInitializer(node: Node): boolean {
14770+
return !!findAncestor(node, node => {
14771+
switch (node.kind) {
14772+
case SyntaxKind.PropertyDeclaration:
14773+
return true;
14774+
case SyntaxKind.PropertyAssignment:
14775+
// We might be in `a = { b: this.b }`, so keep looking. See `tests/cases/compiler/useBeforeDeclaration_propertyAssignment.ts`.
14776+
return false;
14777+
default:
14778+
return isPartOfExpression(node) ? false : "quit";
14779+
}
14780+
});
14781+
}
14782+
1476914783
/**
1477014784
* It's possible that "prop.valueDeclaration" is a local declaration, but the property was also declared in a superclass.
1477114785
* In that case we won't consider it used before its declaration, because it gets its value from the superclass' declaration.
Collapse file
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
tests/cases/compiler/useBeforeDeclaration_propertyAssignment.ts(2,27): error TS2448: Block-scoped variable 'b' used before its declaration.
2+
3+
4+
==== tests/cases/compiler/useBeforeDeclaration_propertyAssignment.ts (1 errors) ====
5+
export class C {
6+
public a = { b: this.b };
7+
~
8+
!!! error TS2448: Block-scoped variable 'b' used before its declaration.
9+
private b = 0;
10+
}
11+
Collapse file
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//// [useBeforeDeclaration_propertyAssignment.ts]
2+
export class C {
3+
public a = { b: this.b };
4+
private b = 0;
5+
}
6+
7+
8+
//// [useBeforeDeclaration_propertyAssignment.js]
9+
"use strict";
10+
exports.__esModule = true;
11+
var C = /** @class */ (function () {
12+
function C() {
13+
this.a = { b: this.b };
14+
this.b = 0;
15+
}
16+
return C;
17+
}());
18+
exports.C = C;
Collapse file
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export class C {
2+
public a = { b: this.b };
3+
private b = 0;
4+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.