-
Notifications
You must be signed in to change notification settings - Fork 782
Fix isPropertyDeclaredInAncestorClass function
#2376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11368,21 +11368,13 @@ func (c *Checker) isOptionalPropertyDeclaration(node *ast.Node) bool { | |
| } | ||
|
|
||
| func (c *Checker) isPropertyDeclaredInAncestorClass(prop *ast.Symbol) bool { | ||
| if prop.Parent.Flags&ast.SymbolFlagsClass == 0 { | ||
| return false | ||
| } | ||
| classType := c.getDeclaredTypeOfSymbol(prop.Parent) | ||
| for { | ||
| baseTypes := c.getBaseTypes(classType) | ||
| if len(baseTypes) == 0 { | ||
| return false | ||
| } | ||
| classType = baseTypes[0] | ||
| superProperty := c.getPropertyOfType(classType, prop.Name) | ||
| if superProperty != nil && superProperty.ValueDeclaration != nil { | ||
| return true | ||
| if prop.Parent.Flags&ast.SymbolFlagsClass != 0 { | ||
| if baseTypes := c.getBaseTypes(c.getDeclaredTypeOfSymbol(prop.Parent)); len(baseTypes) != 0 { | ||
| superProperty := c.getPropertyOfType(baseTypes[0], prop.Name) | ||
| return superProperty != nil && superProperty.ValueDeclaration != nil | ||
|
Comment on lines
+11371
to
+11374
|
||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| checkInheritedProperty.ts(7,14): error TS2729: Property 'b' is used before its initialization. | ||
|
|
||
|
|
||
| ==== checkInheritedProperty.ts (1 errors) ==== | ||
| class Base { | ||
| } | ||
|
|
||
| declare const BaseFactory: new() => Base & { c: string } | ||
|
|
||
| class Derived extends BaseFactory { | ||
| a = this.b | ||
| ~ | ||
| !!! error TS2729: Property 'b' is used before its initialization. | ||
| !!! related TS2728 checkInheritedProperty.ts:8:5: 'b' is declared here. | ||
| b = "abc" | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| //// [tests/cases/compiler/checkInheritedProperty.ts] //// | ||
|
|
||
| === checkInheritedProperty.ts === | ||
| class Base { | ||
| >Base : Symbol(Base, Decl(checkInheritedProperty.ts, 0, 0)) | ||
| } | ||
|
|
||
| declare const BaseFactory: new() => Base & { c: string } | ||
| >BaseFactory : Symbol(BaseFactory, Decl(checkInheritedProperty.ts, 3, 13)) | ||
| >Base : Symbol(Base, Decl(checkInheritedProperty.ts, 0, 0)) | ||
| >c : Symbol(c, Decl(checkInheritedProperty.ts, 3, 44)) | ||
|
|
||
| class Derived extends BaseFactory { | ||
| >Derived : Symbol(Derived, Decl(checkInheritedProperty.ts, 3, 56)) | ||
| >BaseFactory : Symbol(BaseFactory, Decl(checkInheritedProperty.ts, 3, 13)) | ||
|
|
||
| a = this.b | ||
| >a : Symbol(Derived.a, Decl(checkInheritedProperty.ts, 5, 35)) | ||
| >this.b : Symbol(Derived.b, Decl(checkInheritedProperty.ts, 6, 14)) | ||
| >this : Symbol(Derived, Decl(checkInheritedProperty.ts, 3, 56)) | ||
| >b : Symbol(Derived.b, Decl(checkInheritedProperty.ts, 6, 14)) | ||
|
|
||
| b = "abc" | ||
| >b : Symbol(Derived.b, Decl(checkInheritedProperty.ts, 6, 14)) | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| //// [tests/cases/compiler/checkInheritedProperty.ts] //// | ||
|
|
||
| === checkInheritedProperty.ts === | ||
| class Base { | ||
| >Base : Base | ||
| } | ||
|
|
||
| declare const BaseFactory: new() => Base & { c: string } | ||
| >BaseFactory : new () => Base & { c: string; } | ||
| >c : string | ||
|
|
||
| class Derived extends BaseFactory { | ||
| >Derived : Derived | ||
| >BaseFactory : Base & { c: string; } | ||
|
|
||
| a = this.b | ||
| >a : string | ||
| >this.b : string | ||
| >this : this | ||
| >b : string | ||
|
|
||
| b = "abc" | ||
| >b : string | ||
| >"abc" : "abc" | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // @strict: true | ||
| // @noEmit: true | ||
|
|
||
| class Base { | ||
| } | ||
|
|
||
| declare const BaseFactory: new() => Base & { c: string } | ||
|
|
||
| class Derived extends BaseFactory { | ||
| a = this.b | ||
| b = "abc" | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The loop here isn't necessary since
getPropertyOfTypeon the base class will get any inherited property (and not just a property in the immediate base class as the loop seems to imply).