Skip to content
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

Implement containerSeemsToBeEmptyDomElement (regex free) #691

Merged
merged 6 commits into from
Mar 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -10826,7 +10826,39 @@ func (c *Checker) isPropertyAccessible(node *ast.Node, isSuper bool, isWrite boo
}

func (c *Checker) containerSeemsToBeEmptyDomElement(containingType *Type) bool {
return false // !!!
if c.compilerOptions.Lib == nil || slices.Contains(c.compilerOptions.Lib, "lib.dom.d.ts") {
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, I'm now concerned about this due to #593 (comment) showing that our config parser doesn't actually include "dom" as the output here. Maybe that PR was wrong even though it really seemed right.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see (sort of...), but removing slices.Contains(c.compilerOptions.Lib, "dom") yields no test diff, so I think perhaps this doesn't disprove that comment and this could be wrong

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I ran a quick test showing that comment to also apply for dom, just to be sure

assert.DeepEqual(t, parsedConfigFileContent.CompilerOptions().Lib, []string{"dom"})
        --- parsedConfigFileContent.CompilerOptions().Lib
        +++ →
          []string{
        -       "lib.dom.d.ts",
        +       "dom",
          }

I wouldn't say that test should be included, since we're already testing the same thing in the compiler tsconfig test

Copy link
Member

Choose a reason for hiding this comment

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

If the original code matched only "dom", then I'm just trying to figure out if this means config parsing is still wrong. Not necessarily that your PR was wrong originally.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've revert back to the Strada-like check for now, as a default until we know more there

Copy link
Member

@jakebailey jakebailey Mar 22, 2025

Choose a reason for hiding this comment

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

Okay, yeah, so this code in strada has always been broken.

I modified strada to print compilerOptions.lib when this function is called, and the tests print lines like:

[ 'lib.es2015.d.ts', 'lib.es2017.sharedmemory.d.ts' ]
[ 'lib.es5.d.ts', 'lib.dom.d.ts' ]

So !compilerOptions.lib.includes("dom") is always true. 😆

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Being a bit unsure what to do, I opened an issue here microsoft/TypeScript#61466 and re-implemented the fixed version here

return false
}
return everyContainedType(containingType, func(t *Type) bool {
if t.symbol == nil {
return false
}

name := t.symbol.Name

switch name {
case "EventTarget", "Node", "Element":
return true
}

name, ok := strings.CutPrefix(name, "HTML")
if !ok {
return false
}

name, ok = strings.CutSuffix(name, "Element")
if !ok {
return false
}

for _, r := range name {
if !stringutil.IsASCIILetter(r) {
return false
}
}

return true
}) && c.isEmptyObjectType(containingType)
}

func (c *Checker) checkAndReportErrorForExtendingInterface(errorLocation *ast.Node) bool {
Expand Down Expand Up @@ -24531,6 +24563,13 @@ func everyType(t *Type, f func(*Type) bool) bool {
return f(t)
}

func everyContainedType(t *Type, f func(*Type) bool) bool {
if t.flags&TypeFlagsUnionOrIntersection != 0 {
return core.Every(t.Types(), f)
}
return f(t)
}

func (c *Checker) filterType(t *Type, f func(*Type) bool) *Type {
if t.flags&TypeFlagsUnion != 0 {
types := t.Types()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
missingDomElements.ts(6,24): error TS2339: Property 'textContent' does not exist on type 'Element'.
missingDomElements.ts(7,28): error TS2339: Property 'textContent' does not exist on type 'HTMLElement'.
missingDomElements.ts(8,33): error TS2339: Property 'textContent' does not exist on type 'HTMLInputElement'.
missingDomElements.ts(9,47): error TS2339: Property 'textContent' does not exist on type 'EventTarget & HTMLInputElement'.
missingDomElements.ts(6,24): error TS2812: Property 'textContent' does not exist on type 'Element'. Try changing the 'lib' compiler option to include 'dom'.
missingDomElements.ts(7,28): error TS2812: Property 'textContent' does not exist on type 'HTMLElement'. Try changing the 'lib' compiler option to include 'dom'.
missingDomElements.ts(8,33): error TS2812: Property 'textContent' does not exist on type 'HTMLInputElement'. Try changing the 'lib' compiler option to include 'dom'.
missingDomElements.ts(9,47): error TS2812: Property 'textContent' does not exist on type 'EventTarget & HTMLInputElement'. Try changing the 'lib' compiler option to include 'dom'.
missingDomElements.ts(16,32): error TS2339: Property 'textContent' does not exist on type 'HTMLElementFake'.
missingDomElements.ts(17,21): error TS2339: Property 'textContent' does not exist on type 'Node'.

Expand All @@ -14,16 +14,16 @@ missingDomElements.ts(17,21): error TS2339: Property 'textContent' does not exis

({} as any as Element).textContent;
~~~~~~~~~~~
!!! error TS2339: Property 'textContent' does not exist on type 'Element'.
!!! error TS2812: Property 'textContent' does not exist on type 'Element'. Try changing the 'lib' compiler option to include 'dom'.
({} as any as HTMLElement).textContent;
~~~~~~~~~~~
!!! error TS2339: Property 'textContent' does not exist on type 'HTMLElement'.
!!! error TS2812: Property 'textContent' does not exist on type 'HTMLElement'. Try changing the 'lib' compiler option to include 'dom'.
({} as any as HTMLInputElement).textContent;
~~~~~~~~~~~
!!! error TS2339: Property 'textContent' does not exist on type 'HTMLInputElement'.
!!! error TS2812: Property 'textContent' does not exist on type 'HTMLInputElement'. Try changing the 'lib' compiler option to include 'dom'.
({} as any as EventTarget & HTMLInputElement).textContent
~~~~~~~~~~~
!!! error TS2339: Property 'textContent' does not exist on type 'EventTarget & HTMLInputElement'.
!!! error TS2812: Property 'textContent' does not exist on type 'EventTarget & HTMLInputElement'. Try changing the 'lib' compiler option to include 'dom'.

interface HTMLElementFake {}
interface Node {
Expand Down

This file was deleted.