Skip to content

Commit cf3605d

Browse files
committed
Merge branch 'main' of https://github.com/microsoft/typescript-go into fix/1374
2 parents ff68b7a + 4563f53 commit cf3605d

File tree

741 files changed

+7151
-29014
lines changed

Some content is hidden

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

741 files changed

+7151
-29014
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
name: Strada to Corsa Port Expert
3+
description: A Go and TypeScript expert who can easily figure out how to port PRs from one language to another
4+
---
5+
6+
This repository is a port of `microsoft/TypeScript` from TypeScript to Go. Since the port began, the following pull request was applied to microsoft/TypeScript. An equivalent change now needs to be applied here. The user will give you a link to the PR and you will need to try to port it to this repo.
7+
8+
Instructions
9+
- Use `curl` to fetch e.g. `https://api.github.com/repos/microsoft/typescript/pulls/59767` to view the merge commit SHA
10+
- Then use `curl` to fetch e.g. `https://github.com/microsoft/TypeScript/commit/bd3d70058c30253209199cc9dfeb85e72330d79b.patch` to download the diff patch
11+
- Use Playwright MCP if you have other information from github you need, since you won't have MCP access to the TypeScript repo
12+
- Apply the edits made in that PR to this codebase, translating them from TypeScript to Go.
13+
- The change may or may not be applicable. It may have already been ported. Do not make any significant changes outside the scope of the diff. If the change cannot be applied without significant out-of-scope changes, explain why and stop working.
14+
- Tip: search for functions and identifiers from the diff to find the right location to apply edits. Some files in microsoft/TypeScript have been split into multiple.
15+
- Tip: some changes have already been ported, like changes to diagnostic message text. Tests do not need to be ported as they are imported from the submodule.
16+
- Check that the code builds by running npx hereby build in the terminal.
17+
- Run tests. It is expected that tests will fail due to baseline changes.
18+
- Run `npx hereby test` in a terminal. They should fail with messages about baseline changes.
19+
- Tip: to run a single baseline test from the submodule, run go test ./internal/testrunner -run '^TestSubmodule/NAME_OF_TEST_FILE'
20+
- Run npx hereby baseline-accept to adopt the baseline changes.
21+
- Run git diff 'testdata/**/*.diff'. If your change is correct, these diff files will be reduced or completely deleted.
22+
- Iterate until you are satisfied with your change. Commit everything, including the baseline changes in testdata, and open a PR.

_submodules/TypeScript

Submodule TypeScript updated 157 files

internal/checker/checker.go

Lines changed: 83 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5162,7 +5162,7 @@ func (c *Checker) checkImportAttributes(declaration *ast.Node) {
51625162
return
51635163
}
51645164

5165-
if c.moduleKind == core.ModuleKindNodeNext && !isImportAttributes {
5165+
if core.ModuleKindNode20 <= c.moduleKind && c.moduleKind <= core.ModuleKindNodeNext && !isImportAttributes {
51665166
c.grammarErrorOnNode(node, diagnostics.Import_assertions_have_been_replaced_by_import_attributes_Use_with_instead_of_assert)
51675167
return
51685168
}
@@ -13890,6 +13890,12 @@ func (c *Checker) getTargetOfImportEqualsDeclaration(node *ast.Node, dontResolve
1389013890
}
1389113891
immediate := c.resolveExternalModuleName(node, moduleReference, false /*ignoreErrors*/)
1389213892
resolved := c.resolveExternalModuleSymbol(immediate, false /*dontResolveAlias*/)
13893+
if resolved != nil && core.ModuleKindNode20 <= c.moduleKind && c.moduleKind <= core.ModuleKindNodeNext {
13894+
moduleExports := c.getExportOfModule(resolved, ast.InternalSymbolNameModuleExports, node, dontResolveAlias)
13895+
if moduleExports != nil {
13896+
return moduleExports
13897+
}
13898+
}
1389313899
c.markSymbolOfAliasDeclarationIfTypeOnly(node, immediate, resolved, false /*overwriteEmpty*/, nil, "")
1389413900
return resolved
1389513901
}
@@ -13959,14 +13965,42 @@ func (c *Checker) getTargetOfImportClause(node *ast.Node, dontResolveAlias bool)
1395913965
}
1396013966

1396113967
func (c *Checker) getTargetOfModuleDefault(moduleSymbol *ast.Symbol, node *ast.Node, dontResolveAlias bool) *ast.Symbol {
13968+
file := core.Find(moduleSymbol.Declarations, ast.IsSourceFile)
13969+
specifier := c.getModuleSpecifierForImportOrExport(node)
1396213970
var exportDefaultSymbol *ast.Symbol
13971+
var exportModuleDotExportsSymbol *ast.Symbol
1396313972
if isShorthandAmbientModuleSymbol(moduleSymbol) {
13964-
exportDefaultSymbol = moduleSymbol
13973+
// !!! exportDefaultSymbol = moduleSymbol
13974+
// Does nothing?
13975+
} else if file != nil && specifier != nil &&
13976+
core.ModuleKindNode20 <= c.moduleKind && c.moduleKind <= core.ModuleKindNodeNext &&
13977+
c.getEmitSyntaxForModuleSpecifierExpression(specifier) == core.ModuleKindCommonJS &&
13978+
c.program.GetImpliedNodeFormatForEmit(file.AsSourceFile()) == core.ModuleKindESNext {
13979+
exportModuleDotExportsSymbol = c.resolveExportByName(moduleSymbol, ast.InternalSymbolNameModuleExports, node, dontResolveAlias)
13980+
}
13981+
if exportModuleDotExportsSymbol != nil {
13982+
// We have a transpiled default import where the `require` resolves to an ES module with a `module.exports` named
13983+
// export. If `esModuleInterop` is enabled, this will work:
13984+
//
13985+
// const dep_1 = __importDefault(require("./dep.mjs")); // wraps like { default: require("./dep.mjs") }
13986+
// dep_1.default; // require("./dep.mjs") -> the `module.exports` export value
13987+
//
13988+
// But without `esModuleInterop`, it will be broken:
13989+
//
13990+
// const dep_1 = require("./dep.mjs"); // the `module.exports` export value (could be primitive)
13991+
// dep_1.default; // `default` property access on the `module.exports` export value
13992+
//
13993+
// We could try to resolve the 'default' property in the latter case, but it's a mistake to run in this
13994+
// environment without `esModuleInterop`, so just error.
13995+
if !c.compilerOptions.GetESModuleInterop() {
13996+
c.error(node.Name(), diagnostics.Module_0_can_only_be_default_imported_using_the_1_flag, c.symbolToString(moduleSymbol), "esModuleInterop")
13997+
return nil
13998+
}
13999+
c.markSymbolOfAliasDeclarationIfTypeOnly(node, exportModuleDotExportsSymbol /*finalTarget*/, nil /*overwriteEmpty*/, false, nil, "")
14000+
return exportModuleDotExportsSymbol
1396514001
} else {
1396614002
exportDefaultSymbol = c.resolveExportByName(moduleSymbol, ast.InternalSymbolNameDefault, node, dontResolveAlias)
1396714003
}
13968-
file := core.Find(moduleSymbol.Declarations, ast.IsSourceFile)
13969-
specifier := c.getModuleSpecifierForImportOrExport(node)
1397014004
if specifier == nil {
1397114005
return exportDefaultSymbol
1397214006
}
@@ -14948,7 +14982,11 @@ func (c *Checker) resolveESModuleSymbol(moduleSymbol *ast.Symbol, referencingLoc
1494814982
return symbol
1494914983
}
1495014984
referenceParent := referencingLocation.Parent
14951-
if ast.IsImportDeclaration(referenceParent) && ast.GetNamespaceDeclarationNode(referenceParent) != nil || ast.IsImportCall(referenceParent) {
14985+
var namespaceImport *ast.Node
14986+
if ast.IsImportDeclaration(referenceParent) {
14987+
namespaceImport = ast.GetNamespaceDeclarationNode(referenceParent)
14988+
}
14989+
if namespaceImport != nil || ast.IsImportCall(referenceParent) {
1495214990
var reference *ast.Node
1495314991
if ast.IsImportCall(referenceParent) {
1495414992
reference = referenceParent.AsCallExpression().Arguments.Nodes[0]
@@ -14960,29 +14998,51 @@ func (c *Checker) resolveESModuleSymbol(moduleSymbol *ast.Symbol, referencingLoc
1496014998
if defaultOnlyType != nil {
1496114999
return c.cloneTypeAsModuleType(symbol, defaultOnlyType, referenceParent)
1496215000
}
14963-
// !!!
14964-
// targetFile := moduleSymbol. /* ? */ declarations. /* ? */ find(isSourceFile)
14965-
// isEsmCjsRef := targetFile && c.isESMFormatImportImportingCommonjsFormatFile(c.getEmitSyntaxForModuleSpecifierExpression(reference), host.getImpliedNodeFormatForEmit(targetFile))
14966-
// if c.compilerOptions.GetESModuleInterop() || isEsmCjsRef {
14967-
// sigs := c.getSignaturesOfStructuredType(type_, SignatureKindCall)
14968-
// if !sigs || !sigs.length {
14969-
// sigs = c.getSignaturesOfStructuredType(type_, SignatureKindConstruct)
14970-
// }
14971-
// if (sigs && sigs.length) || c.getPropertyOfType(type_, ast.InternalSymbolNameDefault /*skipObjectFunctionPropertyAugment*/, true) || isEsmCjsRef {
14972-
// var moduleType *Type
14973-
// if type_.flags & TypeFlagsStructuredType {
14974-
// moduleType = c.getTypeWithSyntheticDefaultImportType(type_, symbol, moduleSymbol, reference)
14975-
// } else {
14976-
// moduleType = c.createDefaultPropertyWrapperForModule(symbol, symbol.parent)
14977-
// }
14978-
// return c.cloneTypeAsModuleType(symbol, moduleType, referenceParent)
14979-
// }
14980-
// }
15001+
15002+
targetFile := core.Find(moduleSymbol.Declarations, ast.IsSourceFile)
15003+
usageMode := c.getEmitSyntaxForModuleSpecifierExpression(reference)
15004+
var exportModuleDotExportsSymbol *ast.Symbol
15005+
if namespaceImport != nil && targetFile != nil &&
15006+
core.ModuleKindNode20 <= c.moduleKind && c.moduleKind <= core.ModuleKindNodeNext &&
15007+
usageMode == core.ModuleKindCommonJS &&
15008+
c.program.GetImpliedNodeFormatForEmit(targetFile.AsSourceFile()) == core.ModuleKindESNext {
15009+
exportModuleDotExportsSymbol = c.getExportOfModule(symbol, ast.InternalSymbolNameModuleExports, namespaceImport, dontResolveAlias)
15010+
}
15011+
if exportModuleDotExportsSymbol != nil {
15012+
if !suppressInteropError && symbol.Flags&(ast.SymbolFlagsModule|ast.SymbolFlagsVariable) == 0 {
15013+
c.error(referencingLocation, diagnostics.This_module_can_only_be_referenced_with_ECMAScript_imports_Slashexports_by_turning_on_the_0_flag_and_referencing_its_default_export, "esModuleInterop")
15014+
}
15015+
if c.compilerOptions.GetESModuleInterop() && c.hasSignatures(typ) {
15016+
return c.cloneTypeAsModuleType(exportModuleDotExportsSymbol, typ, referenceParent)
15017+
}
15018+
return exportModuleDotExportsSymbol
15019+
}
15020+
15021+
isEsmCjsRef := targetFile != nil && isESMFormatImportImportingCommonjsFormatFile(usageMode, c.program.GetImpliedNodeFormatForEmit(targetFile.AsSourceFile()))
15022+
if c.compilerOptions.GetESModuleInterop() || isEsmCjsRef {
15023+
if c.hasSignatures(typ) || c.getPropertyOfTypeEx(typ, ast.InternalSymbolNameDefault, true /*skipObjectFunctionPropertyAugment*/, false /*includeTypeOnlyMembers*/) != nil || isEsmCjsRef {
15024+
var moduleType *Type
15025+
if typ.Flags()&TypeFlagsStructuredType != 0 {
15026+
moduleType = c.getTypeWithSyntheticDefaultImportType(typ, symbol, moduleSymbol, reference)
15027+
} else {
15028+
moduleType = c.createDefaultPropertyWrapperForModule(symbol, symbol.Parent, nil)
15029+
}
15030+
return c.cloneTypeAsModuleType(symbol, moduleType, referenceParent)
15031+
}
15032+
}
1498115033
}
1498215034
}
1498315035
return symbol
1498415036
}
1498515037

15038+
func (c *Checker) hasSignatures(t *Type) bool {
15039+
return len(c.getSignaturesOfStructuredType(t, SignatureKindCall)) > 0 || len(c.getSignaturesOfStructuredType(t, SignatureKindConstruct)) > 0
15040+
}
15041+
15042+
func isESMFormatImportImportingCommonjsFormatFile(usageMode core.ResolutionMode, targetMode core.ResolutionMode) bool {
15043+
return usageMode == core.ModuleKindESNext && targetMode == core.ModuleKindCommonJS
15044+
}
15045+
1498615046
func (c *Checker) getTypeWithSyntheticDefaultOnly(t *Type, symbol *ast.Symbol, originalSymbol *ast.Symbol, moduleSpecifier *ast.Node) *Type {
1498715047
hasDefaultOnly := c.isOnlyImportableAsDefault(moduleSpecifier, nil)
1498815048
if hasDefaultOnly && t != nil && !c.isErrorType(t) {

internal/checker/grammarchecks.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1240,7 +1240,7 @@ func (c *Checker) checkGrammarForInOrForOfStatement(forInOrOfStatement *ast.ForI
12401240
c.diagnostics.Add(createDiagnosticForNode(forInOrOfStatement.AwaitModifier, diagnostics.X_for_await_loops_are_only_allowed_at_the_top_level_of_a_file_when_that_file_is_a_module_but_this_file_has_no_imports_or_exports_Consider_adding_an_empty_export_to_make_this_file_a_module))
12411241
}
12421242
switch c.moduleKind {
1243-
case core.ModuleKindNode16, core.ModuleKindNode18, core.ModuleKindNodeNext:
1243+
case core.ModuleKindNode16, core.ModuleKindNode18, core.ModuleKindNode20, core.ModuleKindNodeNext:
12441244
sourceFileMetaData := c.program.GetSourceFileMetaData(sourceFile.Path())
12451245
if sourceFileMetaData.ImpliedNodeFormat == core.ModuleKindCommonJS {
12461246
c.diagnostics.Add(createDiagnosticForNode(forInOrOfStatement.AwaitModifier, diagnostics.The_current_file_is_a_CommonJS_module_and_cannot_use_await_at_the_top_level))
@@ -1752,6 +1752,7 @@ func (c *Checker) checkGrammarAwaitOrAwaitUsing(node *ast.Node) bool {
17521752
switch c.moduleKind {
17531753
case core.ModuleKindNode16,
17541754
core.ModuleKindNode18,
1755+
core.ModuleKindNode20,
17551756
core.ModuleKindNodeNext:
17561757
sourceFileMetaData := c.program.GetSourceFileMetaData(sourceFile.Path())
17571758
if sourceFileMetaData.ImpliedNodeFormat == core.ModuleKindCommonJS {

internal/compiler/emitter.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ func getModuleTransformer(opts *transformers.TransformOptions) *transformers.Tra
6464
core.ModuleKindES2022,
6565
core.ModuleKindES2020,
6666
core.ModuleKindES2015,
67+
core.ModuleKindNode20,
6768
core.ModuleKindNode18,
6869
core.ModuleKindNode16,
6970
core.ModuleKindNodeNext,

internal/core/compileroptions.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ func (options *CompilerOptions) GetEmitScriptTarget() ScriptTarget {
190190
switch options.GetEmitModuleKind() {
191191
case ModuleKindNode16, ModuleKindNode18:
192192
return ScriptTargetES2022
193+
case ModuleKindNode20:
194+
return ScriptTargetES2023
193195
case ModuleKindNodeNext:
194196
return ScriptTargetESNext
195197
default:
@@ -212,7 +214,7 @@ func (options *CompilerOptions) GetModuleResolutionKind() ModuleResolutionKind {
212214
return options.ModuleResolution
213215
}
214216
switch options.GetEmitModuleKind() {
215-
case ModuleKindNode16, ModuleKindNode18:
217+
case ModuleKindNode16, ModuleKindNode18, ModuleKindNode20:
216218
return ModuleResolutionKindNode16
217219
case ModuleKindNodeNext:
218220
return ModuleResolutionKindNodeNext
@@ -226,7 +228,7 @@ func (options *CompilerOptions) GetEmitModuleDetectionKind() ModuleDetectionKind
226228
return options.ModuleDetection
227229
}
228230
switch options.GetEmitModuleKind() {
229-
case ModuleKindNode16, ModuleKindNodeNext:
231+
case ModuleKindNode16, ModuleKindNode20, ModuleKindNodeNext:
230232
return ModuleDetectionKindForce
231233
default:
232234
return ModuleDetectionKindAuto
@@ -254,7 +256,7 @@ func (options *CompilerOptions) GetESModuleInterop() bool {
254256
return options.ESModuleInterop == TSTrue
255257
}
256258
switch options.GetEmitModuleKind() {
257-
case ModuleKindNode16, ModuleKindNode18, ModuleKindNodeNext, ModuleKindPreserve:
259+
case ModuleKindNode16, ModuleKindNode18, ModuleKindNode20, ModuleKindNodeNext, ModuleKindPreserve:
258260
return true
259261
}
260262
return false
@@ -273,6 +275,11 @@ func (options *CompilerOptions) GetResolveJsonModule() bool {
273275
if options.ResolveJsonModule != TSUnknown {
274276
return options.ResolveJsonModule == TSTrue
275277
}
278+
switch options.GetEmitModuleKind() {
279+
// TODO in 6.0: add Node16/Node18
280+
case ModuleKindNode20, ModuleKindESNext:
281+
return true
282+
}
276283
return options.GetModuleResolutionKind() == ModuleResolutionKindBundler
277284
}
278285

internal/ls/autoimports.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1237,7 +1237,7 @@ func getUmdImportKind(importingFile *ast.SourceFile /* | FutureSourceFile */, pr
12371237
case core.ModuleKindES2015, core.ModuleKindES2020, core.ModuleKindES2022, core.ModuleKindESNext, core.ModuleKindNone, core.ModuleKindPreserve:
12381238
// Fall back to the `import * as ns` style import.
12391239
return ImportKindNamespace
1240-
case core.ModuleKindNode16, core.ModuleKindNode18, core.ModuleKindNodeNext:
1240+
case core.ModuleKindNode16, core.ModuleKindNode18, core.ModuleKindNode20, core.ModuleKindNodeNext:
12411241
if program.GetImpliedNodeFormatForEmit(importingFile) == core.ModuleKindESNext {
12421242
return ImportKindNamespace
12431243
}

testdata/baselines/reference/submodule/compiler/crashDeclareGlobalTypeofExport.types

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
=== bar.d.ts ===
44
import * as foo from './foo'
5-
>foo : Root
5+
>foo : { default: Root; }
66

77
export as namespace foo
8-
>foo : Root
8+
>foo : { default: Root; }
99

1010
export = foo;
11-
>foo : Root
11+
>foo : { default: Root; }
1212

1313
declare global {
1414
>global : typeof global

testdata/baselines/reference/submodule/compiler/crashDeclareGlobalTypeofExport.types.diff

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

testdata/baselines/reference/submodule/compiler/esModuleInteropDefaultImports.symbols

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ import a from "./a";
1919
>a : Symbol(a, Decl(b.ts, 0, 6))
2020

2121
import { default as b } from "./a";
22-
>default : Symbol(self.default, Decl(a.ts, 0, 30))
22+
>default : Symbol(mod, Decl(a.ts, 0, 30))
2323
>b : Symbol(b, Decl(b.ts, 1, 8))
2424

2525
import c, { default as d } from "./a";
2626
>c : Symbol(c, Decl(b.ts, 2, 6))
27-
>default : Symbol(self.default, Decl(a.ts, 0, 30))
27+
>default : Symbol(mod, Decl(a.ts, 0, 30))
2828
>d : Symbol(d, Decl(b.ts, 2, 11))
2929

3030
import * as self from "./b";
@@ -34,7 +34,7 @@ export { default } from "./a";
3434
>default : Symbol(self.default, Decl(b.ts, 4, 8))
3535

3636
export { default as def } from "./a";
37-
>default : Symbol(self.default, Decl(a.ts, 0, 30))
37+
>default : Symbol(mod, Decl(a.ts, 0, 30))
3838
>def : Symbol(self.def, Decl(b.ts, 5, 8))
3939

4040
a === b;

0 commit comments

Comments
 (0)