Skip to content

Commit ac28acd

Browse files
CopilotRyanCavanaughjakebailey
authored
Port TypeScript PR #63054: Default types to [], support "*" wildcard (#2688)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com> Co-authored-by: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Co-authored-by: Ryan Cavanaugh <RyanCavanaugh@users.noreply.github.com>
1 parent 0d57734 commit ac28acd

172 files changed

Lines changed: 560 additions & 2289 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.

internal/checker/checker.go

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13406,21 +13406,21 @@ func (c *Checker) getCannotFindNameDiagnosticForName(node *ast.Node) *diagnostic
1340613406
case "document", "console":
1340713407
return diagnostics.Cannot_find_name_0_Do_you_need_to_change_your_target_library_Try_changing_the_lib_compiler_option_to_include_dom
1340813408
case "$":
13409-
return core.IfElse(c.compilerOptions.Types != nil,
13410-
diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_jQuery_Try_npm_i_save_dev_types_Slashjquery_and_then_add_jquery_to_the_types_field_in_your_tsconfig,
13411-
diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_jQuery_Try_npm_i_save_dev_types_Slashjquery)
13412-
case "describe", "suite", "it", "test":
13413-
return core.IfElse(c.compilerOptions.Types != nil,
13414-
diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_a_test_runner_Try_npm_i_save_dev_types_Slashjest_or_npm_i_save_dev_types_Slashmocha_and_then_add_jest_or_mocha_to_the_types_field_in_your_tsconfig,
13415-
diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_a_test_runner_Try_npm_i_save_dev_types_Slashjest_or_npm_i_save_dev_types_Slashmocha)
13416-
case "process", "require", "Buffer", "module":
13417-
return core.IfElse(c.compilerOptions.Types != nil,
13418-
diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_node_Try_npm_i_save_dev_types_Slashnode_and_then_add_node_to_the_types_field_in_your_tsconfig,
13419-
diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_node_Try_npm_i_save_dev_types_Slashnode)
13409+
return core.IfElse(c.compilerOptions.UsesWildcardTypes(),
13410+
diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_jQuery_Try_npm_i_save_dev_types_Slashjquery,
13411+
diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_jQuery_Try_npm_i_save_dev_types_Slashjquery_and_then_add_jquery_to_the_types_field_in_your_tsconfig)
13412+
case "beforeEach", "describe", "suite", "it", "test":
13413+
return core.IfElse(c.compilerOptions.UsesWildcardTypes(),
13414+
diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_a_test_runner_Try_npm_i_save_dev_types_Slashjest_or_npm_i_save_dev_types_Slashmocha,
13415+
diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_a_test_runner_Try_npm_i_save_dev_types_Slashjest_or_npm_i_save_dev_types_Slashmocha_and_then_add_jest_or_mocha_to_the_types_field_in_your_tsconfig)
13416+
case "process", "require", "Buffer", "module", "NodeJS":
13417+
return core.IfElse(c.compilerOptions.UsesWildcardTypes(),
13418+
diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_node_Try_npm_i_save_dev_types_Slashnode,
13419+
diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_node_Try_npm_i_save_dev_types_Slashnode_and_then_add_node_to_the_types_field_in_your_tsconfig)
1342013420
case "Bun":
13421-
return core.IfElse(c.compilerOptions.Types != nil,
13422-
diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_Bun_Try_npm_i_save_dev_types_Slashbun_and_then_add_bun_to_the_types_field_in_your_tsconfig,
13423-
diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_Bun_Try_npm_i_save_dev_types_Slashbun)
13421+
return core.IfElse(c.compilerOptions.UsesWildcardTypes(),
13422+
diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_Bun_Try_npm_i_save_dev_types_Slashbun,
13423+
diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_Bun_Try_npm_i_save_dev_types_Slashbun_and_then_add_bun_to_the_types_field_in_your_tsconfig)
1342413424
case "Map", "Set", "Promise", "ast.Symbol", "WeakMap", "WeakSet", "Iterator", "AsyncIterator", "SharedArrayBuffer", "Atomics", "AsyncIterable",
1342513425
"AsyncIterableIterator", "AsyncGenerator", "AsyncGeneratorFunction", "BigInt", "Reflect", "BigInt64Array", "BigUint64Array":
1342613426
return diagnostics.Cannot_find_name_0_Do_you_need_to_change_your_target_library_Try_changing_the_lib_compiler_option_to_1_or_later
@@ -14548,11 +14548,26 @@ func (c *Checker) markSymbolOfAliasDeclarationIfTypeOnly(aliasDeclaration *ast.N
1454814548
}
1454914549

1455014550
func (c *Checker) resolveExternalModuleName(location *ast.Node, moduleReferenceExpression *ast.Node, ignoreErrors bool) *ast.Symbol {
14551-
errorMessage := diagnostics.Cannot_find_module_0_or_its_corresponding_type_declarations
14551+
errorMessage := c.getCannotResolveModuleNameErrorForSpecificModule(moduleReferenceExpression)
14552+
if errorMessage == nil {
14553+
errorMessage = diagnostics.Cannot_find_module_0_or_its_corresponding_type_declarations
14554+
}
1455214555
ignoreErrors = ignoreErrors || c.compilerOptions.NoCheck.IsTrue()
1455314556
return c.resolveExternalModuleNameWorker(location, moduleReferenceExpression, core.IfElse(ignoreErrors, nil, errorMessage), ignoreErrors, false /*isForAugmentation*/)
1455414557
}
1455514558

14559+
func (c *Checker) getCannotResolveModuleNameErrorForSpecificModule(moduleName *ast.Node) *diagnostics.Message {
14560+
if ast.IsStringLiteral(moduleName) {
14561+
if core.NodeCoreModules()[moduleName.Text()] {
14562+
if c.compilerOptions.UsesWildcardTypes() {
14563+
return diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_node_Try_npm_i_save_dev_types_Slashnode
14564+
}
14565+
return diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_node_Try_npm_i_save_dev_types_Slashnode_and_then_add_node_to_the_types_field_in_your_tsconfig
14566+
}
14567+
}
14568+
return nil
14569+
}
14570+
1455614571
func (c *Checker) resolveExternalModuleNameWorker(location *ast.Node, moduleReferenceExpression *ast.Node, moduleNotFoundError *diagnostics.Message, ignoreErrors bool, isForAugmentation bool) *ast.Symbol {
1455714572
if ast.IsStringLiteralLike(moduleReferenceExpression) {
1455814573
return c.resolveExternalModule(location, moduleReferenceExpression.Text(), moduleNotFoundError, core.IfElse(!ignoreErrors, moduleReferenceExpression, nil), isForAugmentation)

internal/compiler/fileInclude.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ func (r *FileIncludeReason) computeDiagnostic(program *Program, toFileName func(
191191
}
192192
case fileIncludeKindAutomaticTypeDirectiveFile:
193193
data := r.asAutomaticTypeDirectiveFileData()
194-
if program.Options().Types != nil {
194+
if !program.Options().UsesWildcardTypes() {
195195
if data.packageId.Name != "" {
196196
return ast.NewCompilerDiagnostic(diagnostics.Entry_point_of_type_library_0_specified_in_compilerOptions_with_packageId_1, data.typeReference, data.packageId.String())
197197
} else {
@@ -277,7 +277,7 @@ func (r *FileIncludeReason) toRelatedInfo(program *Program) *ast.Diagnostic {
277277
}
278278
}
279279
case fileIncludeKindAutomaticTypeDirectiveFile:
280-
if program.Options().Types != nil {
280+
if !program.Options().UsesWildcardTypes() {
281281
data := r.asAutomaticTypeDirectiveFileData()
282282
if typesSyntax := tsoptions.GetOptionsSyntaxByArrayElementValue(program.includeProcessor.getCompilerOptionsObjectLiteralSyntax(program), "types", data.typeReference); typesSyntax != nil {
283283
return tsoptions.CreateDiagnosticForNodeInSourceFile(config.ConfigFile.SourceFile, typesSyntax.AsNode(), diagnostics.File_is_entry_point_of_type_library_specified_here)

internal/core/compileroptions.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package core
22

33
import (
44
"reflect"
5+
"slices"
56
"strings"
67

78
"github.com/microsoft/typescript-go/internal/collections"
@@ -318,6 +319,11 @@ func (options *CompilerOptions) GetEffectiveTypeRoots(currentDirectory string) (
318319
return typeRoots, false
319320
}
320321

322+
// UsesWildcardTypes returns true if this option's types array includes "*"
323+
func (options *CompilerOptions) UsesWildcardTypes() bool {
324+
return slices.Contains(options.Types, "*")
325+
}
326+
321327
func (options *CompilerOptions) GetIsolatedModules() bool {
322328
return options.IsolatedModules == TSTrue || options.VerbatimModuleSyntax == TSTrue
323329
}

internal/fourslash/_scripts/failingTests.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ TestAutoImportJsDocImport1
1717
TestAutoImportModuleNone2
1818
TestAutoImportNodeModuleSymlinkRenamed
1919
TestAutoImportNodeNextJSRequire
20-
TestAutoImportPackageJsonFilterExistingImport3
2120
TestAutoImportPackageJsonImportsCaseSensitivity
2221
TestAutoImportPackageRootPath
2322
TestAutoImportPathsNodeModules
@@ -145,8 +144,6 @@ TestCompletionsImport_reExport_wrongName
145144
TestCompletionsImport_require_addToExisting
146145
TestCompletionsImport_typeOnly
147146
TestCompletionsImport_umdDefaultNoCrash1
148-
TestCompletionsImport_umdModules1_globalAccess
149-
TestCompletionsImport_umdModules3_script
150147
TestCompletionsImport_uriStyleNodeCoreModules2
151148
TestCompletionsImport_uriStyleNodeCoreModules3
152149
TestCompletionsImport_windowsPathsProjectRelative

internal/module/resolver.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2026,11 +2026,15 @@ func ResolveConfig(moduleName string, containingFile string, host ResolutionHost
20262026
}
20272027

20282028
func GetAutomaticTypeDirectiveNames(options *core.CompilerOptions, host ResolutionHost) []string {
2029-
if options.Types != nil {
2030-
return options.Types
2029+
if !options.UsesWildcardTypes() {
2030+
if options.Types != nil {
2031+
return options.Types
2032+
}
2033+
return []string{}
20312034
}
20322035

2033-
var result []string
2036+
// Walk the primary type lookup locations
2037+
var wildcardMatches []string
20342038
typeRoots, _ := options.GetEffectiveTypeRoots(host.GetCurrentDirectory())
20352039
for _, root := range typeRoots {
20362040
if host.FS().DirectoryExists(root) {
@@ -2048,13 +2052,24 @@ func GetAutomaticTypeDirectiveNames(options *core.CompilerOptions, host Resoluti
20482052
if !isNotNeededPackage {
20492053
baseFileName := tspath.GetBaseFileName(normalized)
20502054
if !strings.HasPrefix(baseFileName, ".") {
2051-
result = append(result, baseFileName)
2055+
wildcardMatches = append(wildcardMatches, baseFileName)
20522056
}
20532057
}
20542058
}
20552059
}
20562060
}
2057-
return result
2061+
2062+
// Order potentially matters in program construction, so substitute
2063+
// in the wildcard in the position it was specified in the types array
2064+
var result []string
2065+
for _, t := range options.Types {
2066+
if t == "*" {
2067+
result = append(result, wildcardMatches...)
2068+
} else {
2069+
result = append(result, t)
2070+
}
2071+
}
2072+
return core.Deduplicate(result)
20582073
}
20592074

20602075
type ResolvedEntrypoints struct {

testdata/baselines/reference/submodule/compiler/anonymousModules.errors.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
anonymousModules.ts(1,1): error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.
1+
anonymousModules.ts(1,1): error TS2591: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node` and then add 'node' to the types field in your tsconfig.
22
anonymousModules.ts(1,8): error TS1437: Namespace must be given a name.
3-
anonymousModules.ts(4,2): error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.
3+
anonymousModules.ts(4,2): error TS2591: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node` and then add 'node' to the types field in your tsconfig.
44
anonymousModules.ts(4,9): error TS1437: Namespace must be given a name.
5-
anonymousModules.ts(10,2): error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.
5+
anonymousModules.ts(10,2): error TS2591: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node` and then add 'node' to the types field in your tsconfig.
66
anonymousModules.ts(10,9): error TS1437: Namespace must be given a name.
77

88

99
==== anonymousModules.ts (6 errors) ====
1010
module {
1111
~~~~~~
12-
!!! error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.
12+
!!! error TS2591: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node` and then add 'node' to the types field in your tsconfig.
1313
~
1414
!!! error TS1437: Namespace must be given a name.
1515
export var foo = 1;
1616

1717
module {
1818
~~~~~~
19-
!!! error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.
19+
!!! error TS2591: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node` and then add 'node' to the types field in your tsconfig.
2020
~
2121
!!! error TS1437: Namespace must be given a name.
2222
export var bar = 1;
@@ -26,7 +26,7 @@ anonymousModules.ts(10,9): error TS1437: Namespace must be given a name.
2626

2727
module {
2828
~~~~~~
29-
!!! error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.
29+
!!! error TS2591: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node` and then add 'node' to the types field in your tsconfig.
3030
~
3131
!!! error TS1437: Namespace must be given a name.
3232
var x = bar;

testdata/baselines/reference/submodule/compiler/anonymousModules.errors.txt.diff

Lines changed: 0 additions & 39 deletions
This file was deleted.

testdata/baselines/reference/submodule/compiler/constructorWithIncompleteTypeAnnotation.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
constructorWithIncompleteTypeAnnotation.ts(11,13): error TS2503: Cannot find namespace 'module'.
2-
constructorWithIncompleteTypeAnnotation.ts(11,13): error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.
2+
constructorWithIncompleteTypeAnnotation.ts(11,13): error TS2591: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node` and then add 'node' to the types field in your tsconfig.
33
constructorWithIncompleteTypeAnnotation.ts(11,19): error TS1005: ';' expected.
44
constructorWithIncompleteTypeAnnotation.ts(22,35): error TS1005: ')' expected.
55
constructorWithIncompleteTypeAnnotation.ts(22,39): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
@@ -108,7 +108,7 @@ constructorWithIncompleteTypeAnnotation.ts(261,1): error TS1128: Declaration or
108108
~~~~~~
109109
!!! error TS2503: Cannot find namespace 'module'.
110110
~~~~~~
111-
!!! error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.
111+
!!! error TS2591: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node` and then add 'node' to the types field in your tsconfig.
112112
~
113113
!!! error TS1005: ';' expected.
114114

testdata/baselines/reference/submodule/compiler/constructorWithIncompleteTypeAnnotation.errors.txt.diff

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
@@= skipped -0, +0 lines =@@
44
-error TS-1: Pre-emit (93) and post-emit (94) diagnostic counts do not match! This can indicate that a semantic _error_ was added by the emit resolver - such an error may not be reflected on the command line or in the editor, but may be captured in a baseline here!
55
constructorWithIncompleteTypeAnnotation.ts(11,13): error TS2503: Cannot find namespace 'module'.
6-
-constructorWithIncompleteTypeAnnotation.ts(11,13): error TS2591: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node` and then add 'node' to the types field in your tsconfig.
7-
+constructorWithIncompleteTypeAnnotation.ts(11,13): error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.
6+
constructorWithIncompleteTypeAnnotation.ts(11,13): error TS2591: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node` and then add 'node' to the types field in your tsconfig.
87
constructorWithIncompleteTypeAnnotation.ts(11,19): error TS1005: ';' expected.
9-
constructorWithIncompleteTypeAnnotation.ts(22,35): error TS1005: ')' expected.
10-
constructorWithIncompleteTypeAnnotation.ts(22,39): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
118
@@= skipped -42, +41 lines =@@
129
constructorWithIncompleteTypeAnnotation.ts(159,30): error TS1005: '{' expected.
1310
constructorWithIncompleteTypeAnnotation.ts(159,31): error TS2304: Cannot find name 'Property'.
@@ -36,16 +33,7 @@
3633
==== constructorWithIncompleteTypeAnnotation.ts (93 errors) ====
3734
declare module "fs" {
3835
export class File {
39-
@@= skipped -18, +15 lines =@@
40-
~~~~~~
41-
!!! error TS2503: Cannot find namespace 'module'.
42-
~~~~~~
43-
-!!! error TS2591: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node` and then add 'node' to the types field in your tsconfig.
44-
+!!! error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.
45-
~
46-
!!! error TS1005: ';' expected.
47-
48-
@@= skipped -120, +120 lines =@@
36+
@@= skipped -138, +135 lines =@@
4937
var undef = undefined;
5038

5139
var _\uD4A5\u7204\uC316\uE59F = local;

0 commit comments

Comments
 (0)