Skip to content

Commit d389ac6

Browse files
committed
Refine rewriting restrictions
1 parent 0523606 commit d389ac6

62 files changed

Lines changed: 2694 additions & 160 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎tsc/internal/checker/checker.go‎

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8489,7 +8489,7 @@ func (c *Checker) checkImportCallExpression(node *ast.Node) *Type {
84898489
if c.compilerOptions.RewriteRelativeImportExtensions.IsTrue() &&
84908490
!c.compilerOptions.DisableDynamicImportExtensionRewriting.IsTrue() &&
84918491
!ast.IsStringLiteralLike(specifier) {
8492-
c.checkExternalEmitHelpers(node, ExternalEmitHelpersRewriteRelativeImportExtension)
8492+
c.checkDynamicImportExtensionRewriting(node)
84938493
}
84948494
specifierType := c.checkExpressionCached(specifier)
84958495
var optionsType *Type
@@ -8537,6 +8537,16 @@ func (c *Checker) checkImportCallExpression(node *ast.Node) *Type {
85378537
return c.createPromiseReturnType(node, c.anyType)
85388538
}
85398539

8540+
func (c *Checker) checkDynamicImportExtensionRewriting(node *ast.Node) {
8541+
for _, rewrite := range c.program.ContentMapperExtensionRewrites() {
8542+
if !rewrite.Runtime && rewrite.Source != rewrite.Target {
8543+
c.error(node, diagnostics.Non_literal_import_paths_cannot_be_rewritten_when_a_content_mapper_uses_both_include_and_an_outputExtensions_mapping_Set_disableDynamicImportExtensionRewriting_to_disable_rewriting_of_non_literal_import_paths)
8544+
return
8545+
}
8546+
}
8547+
c.checkExternalEmitHelpers(node, ExternalEmitHelpersRewriteRelativeImportExtension)
8548+
}
8549+
85408550
/**
85418551
* Syntactically and semantically checks a call or new expression.
85428552
* @param node The call/new expression to be checked.
@@ -8547,7 +8557,7 @@ func (c *Checker) checkCallExpression(node *ast.Node, checkMode CheckMode) *Type
85478557
!c.compilerOptions.DisableDynamicImportExtensionRewriting.IsTrue() &&
85488558
ast.IsInJSFile(node) && ast.IsRequireCall(node, false /*requireStringLiteralLikeArgument*/) &&
85498559
!ast.IsStringLiteralLike(node.Arguments()[0]) {
8550-
c.checkExternalEmitHelpers(node, ExternalEmitHelpersRewriteRelativeImportExtension)
8560+
c.checkDynamicImportExtensionRewriting(node)
85518561
}
85528562
c.checkGrammarTypeArguments(node, node.TypeArgumentList())
85538563
signature := c.getResolvedSignature(node, nil /*candidatesOutArray*/, checkMode)
@@ -15572,11 +15582,17 @@ func (c *Checker) resolveExternalModule(
1557215582
}
1557315583

1557415584
if errorNode != nil && !resolvedModule.IsCustomResolution {
15575-
rewrites := c.program.ContentMapperExtensionRewrites()
15585+
allRewrites := c.program.ContentMapperExtensionRewrites()
1557615586
caseSensitivity := c.program.CaseSensitivity()
1557715587
specifier := tspath.ToModuleSpecifier(moduleReference)
15588+
rewrites := contentmapper.RewritesForImport(allRewrites, importingSourceFile.FileName(), specifier, c.program.ContentMapperExtensionForFileName, caseSensitivity)
1557815589
_, mappedSpecifier := specifier.RewriteExtension(rewrites, caseSensitivity)
15579-
checkDeclarationRewrite := c.compilerOptions.GetEmitDeclarations() && mappedSpecifier &&
15590+
resolvedRewrites := contentmapper.FilterExtensionRewrites(allRewrites, c.program.ContentMapperExtensionForFileName(sourceFile.FileName()), caseSensitivity)
15591+
mappedSource := false
15592+
if sourceFile.ContentMapper() != "" {
15593+
_, mappedSource = sourceFile.FileName().RewriteExtension(resolvedRewrites, caseSensitivity)
15594+
}
15595+
checkDeclarationRewrite := c.compilerOptions.GetEmitDeclarations() && (mappedSpecifier || mappedSource) &&
1558015596
tspath.PathIsRelative(moduleReference) && !importingSourceFile.IsDeclarationFile
1558115597
if resolvedModule.ResolvedUsingTsExtension && tspath.IsDeclarationFileName(moduleReference) {
1558215598
if ast.FindAncestor(location, ast.IsEmittableImport) != nil {
@@ -15618,15 +15634,42 @@ func (c *Checker) resolveExternalModule(
1561815634
!tspath.IsDeclarationFileName(moduleReference) &&
1561915635
(!ast.IsLiteralImportTypeNode(location) && !ast.IsPartOfTypeOnlyImportOrExportDeclaration(location) ||
1562015636
checkDeclarationRewrite) {
15621-
rewrites = contentmapper.RewritesForImport(rewrites, specifier, c.program.ContentMapperExtensionForFileName(sourceFile.FileName()), caseSensitivity)
1562215637
shouldRewrite := specifier.ShouldRewriteExtension(rewrites, caseSensitivity)
15623-
mappedSource := false
15624-
if sourceFile.ContentMapper() != "" {
15625-
_, mappedSource = sourceFile.FileName().RewriteExtension(rewrites, caseSensitivity)
15626-
}
15638+
predictedSpecifier, _ := specifier.RewriteExtension(rewrites, caseSensitivity)
15639+
resolvedSpecifier, resolvedSpecifierRewritten := specifier.RewriteExtension(resolvedRewrites, caseSensitivity)
1562715640
resolvedUsingRewritableExtension := resolvedModule.ResolvedUsingTsExtension ||
1562815641
resolvedModule.ResolvedUsingExtraExtensions && sourceFile.ContentMapper() != "" && mappedSource
15629-
if !resolvedUsingRewritableExtension && shouldRewrite {
15642+
if specifier.IsRelative() && sourceFile.ContentMapper() != "" &&
15643+
(caseSensitivity.Canonicalize(predictedSpecifier.AsString()) != caseSensitivity.Canonicalize(resolvedSpecifier.AsString()) ||
15644+
mappedSource && !resolvedSpecifierRewritten) {
15645+
hasRootDirs := len(c.compilerOptions.RootDirs) > 0
15646+
hasModuleSuffixes := len(c.compilerOptions.ModuleSuffixes) > 0
15647+
if (hasRootDirs || hasModuleSuffixes) && mappedSpecifier != mappedSource {
15648+
var message *diagnostics.Message
15649+
if hasRootDirs && hasModuleSuffixes {
15650+
message = diagnostics.This_relative_import_path_is_unsafe_to_rewrite_because_it_appears_not_to_match_the_content_mapper_s_include_but_does_after_resolution_with_rootDirs_and_moduleSuffixes
15651+
if mappedSpecifier {
15652+
message = diagnostics.This_relative_import_path_is_unsafe_to_rewrite_because_it_appears_to_match_the_content_mapper_s_include_but_does_not_after_resolution_with_rootDirs_and_moduleSuffixes
15653+
}
15654+
} else if hasRootDirs {
15655+
message = diagnostics.This_relative_import_path_is_unsafe_to_rewrite_because_it_appears_not_to_match_the_content_mapper_s_include_but_does_after_resolution_with_rootDirs
15656+
if mappedSpecifier {
15657+
message = diagnostics.This_relative_import_path_is_unsafe_to_rewrite_because_it_appears_to_match_the_content_mapper_s_include_but_does_not_after_resolution_with_rootDirs
15658+
}
15659+
} else {
15660+
message = diagnostics.This_relative_import_path_is_unsafe_to_rewrite_because_it_appears_not_to_match_the_content_mapper_s_include_but_does_after_resolution_with_moduleSuffixes
15661+
if mappedSpecifier {
15662+
message = diagnostics.This_relative_import_path_is_unsafe_to_rewrite_because_it_appears_to_match_the_content_mapper_s_include_but_does_not_after_resolution_with_moduleSuffixes
15663+
}
15664+
}
15665+
c.error(errorNode, message)
15666+
} else {
15667+
c.error(
15668+
errorNode,
15669+
diagnostics.This_relative_import_path_is_unsafe_to_rewrite_because_the_content_mapper_s_outputExtensions_mapping_depends_on_module_resolution,
15670+
)
15671+
}
15672+
} else if !resolvedUsingRewritableExtension && shouldRewrite {
1563015673
relativeToSourceFile := resolvedModule.ResolvedFileName.AsString()
1563115674
if relativePath, ok := c.program.CaseSensitivity().RelativePathFromFile(
1563215675
importingSourceFile.FileName(),
@@ -15643,7 +15686,7 @@ func (c *Checker) resolveExternalModule(
1564315686
} else if resolvedUsingRewritableExtension && !shouldRewrite && (mappedSource || c.program.SourceFileMayBeEmitted(sourceFile, false)) {
1564415687
extension := tspath.GetAnyExtensionFromPath(moduleReference, nil, tspath.CaseSensitive)
1564515688
if mappedSource {
15646-
rewrite, _ := sourceFile.FileName().GetExtensionRewrite(rewrites, caseSensitivity)
15689+
rewrite, _ := sourceFile.FileName().GetExtensionRewrite(resolvedRewrites, caseSensitivity)
1564715690
extension = rewrite.Source
1564815691
}
1564915692
c.error(
@@ -29400,7 +29443,7 @@ func (c *Checker) checkExternalEmitHelpers(location *ast.Node, helpers ExternalE
2940029443
if !c.hasSignatureWithArityGreaterThan(symbol, 4) {
2940129444
c.error(location, diagnostics.This_syntax_requires_an_imported_helper_named_1_with_2_parameters_which_is_not_compatible_with_the_one_in_0_Consider_upgrading_your_version_of_0, externalHelpersModuleNameText, name, 5)
2940229445
}
29403-
} else if helper&ExternalEmitHelpersRewriteRelativeImportExtension != 0 && len(c.program.ContentMapperExtensionRewrites()) > 0 {
29446+
} else if helper&ExternalEmitHelpersRewriteRelativeImportExtension != 0 && tspath.HasRuntimeExtensionRewrites(c.program.ContentMapperExtensionRewrites()) {
2940429447
if !c.hasSignatureWithArityGreaterThan(symbol, 3) {
2940529448
c.error(location, diagnostics.This_syntax_requires_an_imported_helper_named_1_with_2_parameters_which_is_not_compatible_with_the_one_in_0_Consider_upgrading_your_version_of_0, externalHelpersModuleNameText, name, 4)
2940629449
}

‎tsc/internal/checker/nodebuilderimpl.go‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,19 +1343,17 @@ func (b *NodeBuilderImpl) moduleSpecifierResultForSymbol(result moduleSpecifierR
13431343
if importAttributesType != nil && b.moduleSpecifierResolvesToSymbol(result.specifier, importAttributesType, symbol) {
13441344
result.importAttributesType = importAttributesType
13451345
}
1346-
result.specifier = b.rewriteContentMappedModuleSpecifier(result.specifier, ast.GetSourceFileOfModule(symbol))
1346+
result.specifier = b.rewriteContentMappedModuleSpecifier(result.specifier)
13471347
return result
13481348
}
13491349

1350-
func (b *NodeBuilderImpl) rewriteContentMappedModuleSpecifier(specifier tspath.ModuleSpecifier, targetFile *ast.SourceFile) tspath.ModuleSpecifier {
1350+
func (b *NodeBuilderImpl) rewriteContentMappedModuleSpecifier(specifier tspath.ModuleSpecifier) tspath.ModuleSpecifier {
13511351
if b.ctx.internalFlags&nodebuilder.InternalFlagsRewriteModuleSpecifiers != 0 && b.ch.compilerOptions.RewriteRelativeImportExtensions.IsTrue() {
13521352
rewrites := b.ctx.host.ContentMapperExtensionRewrites()
13531353
caseSensitivity := b.ctx.host.CaseSensitivity()
1354-
extension := ""
1355-
if targetFile != nil {
1356-
extension = b.ctx.host.ContentMapperExtensionForFileName(targetFile.FileName())
1354+
if b.ctx.enclosingFile != nil {
1355+
rewrites = contentmapper.RewritesForImport(rewrites, b.ctx.enclosingFile.FileName(), specifier, b.ctx.host.ContentMapperExtensionForFileName, caseSensitivity)
13571356
}
1358-
rewrites = contentmapper.RewritesForImport(rewrites, specifier, extension, caseSensitivity)
13591357
if specifier.ShouldRewriteExtension(rewrites, caseSensitivity) {
13601358
rewritten, _ := specifier.RewriteExtension(rewrites, caseSensitivity)
13611359
return rewritten

‎tsc/internal/checker/nodecopy.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ func (b *NodeBuilderImpl) getModuleSpecifierOverride(parent *ast.Node, lit *ast.
271271
func (b *NodeBuilderImpl) rewriteModuleSpecifier(parent *ast.Node, lit *ast.Node) *ast.Node {
272272
newName := b.getModuleSpecifierOverride(parent, lit)
273273
if len(newName) == 0 {
274-
newName = b.rewriteContentMappedModuleSpecifier(tspath.ToModuleSpecifier(lit.Text()), b.ch.getExternalModuleFileFromDeclaration(parent)).AsString()
274+
newName = b.rewriteContentMappedModuleSpecifier(tspath.ToModuleSpecifier(lit.Text())).AsString()
275275
if newName == lit.Text() {
276276
return lit
277277
}

0 commit comments

Comments
 (0)