Skip to content

Scaffold module resolution#12

Merged
andrewbranch merged 10 commits intomainfrom
module-resolution
Oct 22, 2024
Merged

Scaffold module resolution#12
andrewbranch merged 10 commits intomainfrom
module-resolution

Conversation

@andrewbranch
Copy link
Member

@andrewbranch andrewbranch commented Oct 22, 2024

This adds many of the types and utilities used by module resolution, but doesn’t yet implement the core resolution algorithms. I wanted to get a PR up as soon as possible to prevent clashes with others who might need the same types and utilities as they’re getting started.

(Note: fixed my local clone to push to my fork after opening this PR)

resolvedUsingTsExtension bool
}

func formatExtensions(extensions Extensions) string {
Copy link
Member

Choose a reason for hiding this comment

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

Eventually this would be nice to pull into a stringer like tool (https://cs.opensource.google/go/x/tools/+/refs/tags/v0.26.0:cmd/stringer/stringer.go), but that doesn't do bit ops.

}

func formatExtensions(extensions Extensions) string {
result := []string{}
Copy link
Member

Choose a reason for hiding this comment

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

If this turns out to be hot, you may want to consider doing something like:

result := make([]string, 0, bits.OnesCount(extensions))

To get the right number of entries, but I suspect this would be more efficient as a strings.Builder rather than a strings.Join anyway.

(This is premature optimization; profiling will tell us what to tweak)

Copy link
Member Author

Choose a reason for hiding this comment

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

I’m not sure the hassle of adding the commas is worth using a strings.Builder—this is only for traceResolution which shouldn’t be used in production builds. Good idea about setting the capacity though.

break
default:
panic(fmt.Sprintf("Unexpected moduleResolution: %s", moduleResolution))
panic(fmt.Sprintf("Unexpected moduleResolution: %d", moduleResolution))
Copy link
Member

Choose a reason for hiding this comment

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

Probably at some point we should just run stringer on this to generate enum stuff; I will put up a PR with an example of this since we already have enums which would benefit.

Copy link
Member

@jakebailey jakebailey left a comment

Choose a reason for hiding this comment

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

Just from a Go code perspective, this all seems good to me; we'll have to see how the Host interface stuff plays out as that will get more complicated as we introduce more of them and want to cross-assign them. Thankfully it's not that bad to "cast" (type convert) between them as they will actually be statically checked so we can "add" more methods onto a narrower type if the underlying implementation has it.


func (c *Checker) resolveExternalModuleName(location *Node, moduleReferenceExpression *Node, ignoreErrors bool) *Symbol {
isClassic := getEmitModuleResolutionKind(c.compilerOptions) == ModuleResolutionKindClassic
errorMessage := ifElse(isClassic,
Copy link
Member

Choose a reason for hiding this comment

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

At some level I wonder if we're really saving much code by omitting classic, but I guess we want it to go away anyway.

})
}

func formatMessage(message *diagnostics.Message, args ...any) string {
Copy link
Member

Choose a reason for hiding this comment

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

At some level, I wonder if this should just be a method on Message. Surely the logic for formatting args is not compiler specific.

Copy link
Member Author

Choose a reason for hiding this comment

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

I’m not going to do that right now because this uses multiple other utilities from the compiler package, and I don’t know the best way to move them to or access them in the diagnostics package.

@andrewbranch andrewbranch merged commit 0e957c8 into main Oct 22, 2024
@andrewbranch andrewbranch deleted the module-resolution branch October 22, 2024 22:40
Copilot AI mentioned this pull request Aug 19, 2025
nathanwhit added a commit to nathanwhit/typescript-go-rs that referenced this pull request Nov 25, 2025
* add import attribute type to resolved file cache key

* fmt
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.

2 participants