Skip to content

Fix isPropertyDeclaredInAncestorClass function - #2376

Merged
Anders Hejlsberg (ahejlsberg) merged 2 commits into
mainfrom
fix-2284
Dec 14, 2025
Merged

Fix isPropertyDeclaredInAncestorClass function#2376
Anders Hejlsberg (ahejlsberg) merged 2 commits into
mainfrom
fix-2284

Conversation

@ahejlsberg

Copy link
Copy Markdown
Member

Fixes #2284.

Copilot AI review requested due to automatic review settings December 13, 2025 23:21
return false
}
classType := c.getDeclaredTypeOfSymbol(prop.Parent)
for {

Copy link
Copy Markdown
Member Author

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 getPropertyOfType on the base class will get any inherited property (and not just a property in the immediate base class as the loop seems to imply).

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR attempts to fix issue #2284 by refactoring the isPropertyDeclaredInAncestorClass function to use a more concise implementation. However, the refactored code introduces a critical regression by only checking the immediate parent class instead of traversing the entire inheritance hierarchy.

Key changes:

  • Simplified the isPropertyDeclaredInAncestorClass function logic
  • Added test case checkInheritedProperty.ts to verify property initialization order checking
  • Added corresponding baseline files for the test

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
testdata/tests/cases/compiler/checkInheritedProperty.ts New test case checking that property 'b' used before initialization produces an error when extending a factory constructor
testdata/baselines/reference/compiler/checkInheritedProperty.types Expected type information baseline for the new test
testdata/baselines/reference/compiler/checkInheritedProperty.symbols Expected symbol information baseline for the new test
testdata/baselines/reference/compiler/checkInheritedProperty.errors.txt Expected error baseline showing TS2729 error for property used before initialization
internal/checker/checker.go Refactored isPropertyDeclaredInAncestorClass to a more concise form, but removed the loop that checks the full inheritance chain

Comment on lines +11371 to +11374
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

Copilot AI Dec 13, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The refactored implementation only checks the immediate base type and no longer traverses the entire inheritance hierarchy. The original code correctly looped through all ancestor classes to check if the property was declared in any ancestor. This change will cause incorrect behavior when a property is declared in a grandparent or higher ancestor class, as those declarations will not be detected.

The loop should be restored to properly check all ancestors in the inheritance chain, not just the immediate parent.

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong. getPropertyOfType on the immediate base type checks all inherited properties, not just those from the immediate base. Thus, the loop is unnecessary and actually causes a panic when a base type is an intersection.

@jakebailey Jake Bailey (jakebailey) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cameron (@camc314) this should allow you to revert oxc-project/tsgolint#504

@camc314

Copy link
Copy Markdown
Contributor

nice, thank you 🙏

@ahejlsberg
Anders Hejlsberg (ahejlsberg) added this pull request to the merge queue Dec 14, 2025
Merged via the queue into main with commit 6c175a0 Dec 14, 2025
29 checks passed
@ahejlsberg

Copy link
Copy Markdown
Member Author

Cameron (@camc314) When you get a chance, can you verify that this has fixed the issue for you?

@camc314

Copy link
Copy Markdown
Contributor

Yep, this has fixed the panic, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash in getBaseTypes

4 participants