[api-extractor] (fix) Fix module resolution for some type import declarations #5288
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #5106.
Fixes #5126.
This PR makes a change to the
ExportAnalyzerwhich fixes a regression bug where type import declarations were sometimes not resolved (and then treated as external) when using amoduleResolutionother thannode.Additionally, this PR adds additional functionality to the
api-extractor-scenariosrunner script to allow scenarios to use custom compiler states. In addition to that, it adds a new scenario calledbundlerModuleResolutionthat uses this functionality in order to test this fix.Details
From what I could tell, this bug was introduced in
[email protected]as a result of updating to[email protected]. In previous versions, type import declaration nodes would always contain aStringNodein theirargumentfield, which is used to get the mode. However, in 5.7.0 and after, these import declaration nodes may now contain aLiteralNodein certain situations. As a result, it would fail theisStringLiteralLikecheck and stop resolving the modules, leading to rollup types that include imports to types that don't exist. I introduced an extra check for theseLiteralNodesthat will then extract the intendedStringNode, and this seems to fix the issue. It shouldn't introduce any regressions, as this only introduces a new code path and doesn't affect older ones.Testing
This PR adds a scenario called
bundlerModuleResolution(let me know if you have a better name) to test the fix. The default TSConfig for the scenarios is"moduleResolution": "node", so overrides the tsconfig to set"moduleResolution": "bundler". This is done with the following additional change:Currently, the
api-extractor-scenariosreuse the same compiler state, but this prevents using scenarios from testing functionality that is specific to certain tsconfig settings because that would require a new compiler state. This PR adds some new logic inrun-scenario-helpersthat creates a custom compiler state for scenarios that added a"compiler"option in theapi-extractor-overrides.jsonfile.Impacted documentation
This shouldn't impact documentation, as it is a fix for a regression.