Skip to content

Commit 5e6bd1e

Browse files
authored
Incremental port (#1322)
1 parent f0484dd commit 5e6bd1e

File tree

132 files changed

+15326
-2764
lines changed

Some content is hidden

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

132 files changed

+15326
-2764
lines changed

cmd/tsgo/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ func runMain() int {
2020
return runAPI(args[1:])
2121
}
2222
}
23-
return int(execute.CommandLine(newSystem(), nil, args))
23+
result := execute.CommandLine(newSystem(), args, false)
24+
return int(result.Status)
2425
}

internal/ast/diagnostic.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type Diagnostic struct {
2121
relatedInformation []*Diagnostic
2222
reportsUnnecessary bool
2323
reportsDeprecated bool
24+
skippedOnNoEmit bool
2425
}
2526

2627
func (d *Diagnostic) File() *SourceFile { return d.file }
@@ -35,10 +36,12 @@ func (d *Diagnostic) MessageChain() []*Diagnostic { return d.messageChain
3536
func (d *Diagnostic) RelatedInformation() []*Diagnostic { return d.relatedInformation }
3637
func (d *Diagnostic) ReportsUnnecessary() bool { return d.reportsUnnecessary }
3738
func (d *Diagnostic) ReportsDeprecated() bool { return d.reportsDeprecated }
39+
func (d *Diagnostic) SkippedOnNoEmit() bool { return d.skippedOnNoEmit }
3840

3941
func (d *Diagnostic) SetFile(file *SourceFile) { d.file = file }
4042
func (d *Diagnostic) SetLocation(loc core.TextRange) { d.loc = loc }
4143
func (d *Diagnostic) SetCategory(category diagnostics.Category) { d.category = category }
44+
func (d *Diagnostic) SetSkippedOnNoEmit() { d.skippedOnNoEmit = true }
4245

4346
func (d *Diagnostic) SetMessageChain(messageChain []*Diagnostic) *Diagnostic {
4447
d.messageChain = messageChain
@@ -69,6 +72,32 @@ func (d *Diagnostic) Clone() *Diagnostic {
6972
return &result
7073
}
7174

75+
func NewDiagnosticWith(
76+
file *SourceFile,
77+
loc core.TextRange,
78+
code int32,
79+
category diagnostics.Category,
80+
message string,
81+
messageChain []*Diagnostic,
82+
relatedInformation []*Diagnostic,
83+
reportsUnnecessary bool,
84+
reportsDeprecated bool,
85+
skippedOnNoEmit bool,
86+
) *Diagnostic {
87+
return &Diagnostic{
88+
file: file,
89+
loc: loc,
90+
code: code,
91+
category: category,
92+
message: message,
93+
messageChain: messageChain,
94+
relatedInformation: relatedInformation,
95+
reportsUnnecessary: reportsUnnecessary,
96+
reportsDeprecated: reportsDeprecated,
97+
skippedOnNoEmit: skippedOnNoEmit,
98+
}
99+
}
100+
72101
func NewDiagnostic(file *SourceFile, loc core.TextRange, message *diagnostics.Message, args ...any) *Diagnostic {
73102
return &Diagnostic{
74103
file: file,

internal/ast/utilities.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,6 +1635,10 @@ func IsModuleAugmentationExternal(node *Node) bool {
16351635
return false
16361636
}
16371637

1638+
func IsModuleWithStringLiteralName(node *Node) bool {
1639+
return IsModuleDeclaration(node) && node.Name().Kind == KindStringLiteral
1640+
}
1641+
16381642
func GetContainingClass(node *Node) *Node {
16391643
return FindAncestor(node.Parent, IsClassLike)
16401644
}

internal/checker/checker.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,8 @@ type Checker struct {
853853
packagesMap map[string]bool
854854
activeMappers []*TypeMapper
855855
activeTypeMappersCaches []map[string]*Type
856+
ambientModulesOnce sync.Once
857+
ambientModules []*ast.Symbol
856858
}
857859

858860
func NewChecker(program Program) *Checker {
@@ -2085,7 +2087,7 @@ func (c *Checker) getSymbol(symbols ast.SymbolTable, name string, meaning ast.Sy
20852087
}
20862088

20872089
func (c *Checker) CheckSourceFile(ctx context.Context, sourceFile *ast.SourceFile) {
2088-
if SkipTypeChecking(sourceFile, c.compilerOptions, c.program) {
2090+
if SkipTypeChecking(sourceFile, c.compilerOptions, c.program, false) {
20892091
return
20902092
}
20912093
c.checkSourceFile(ctx, sourceFile)
@@ -10069,7 +10071,7 @@ func (c *Checker) checkCollisionWithRequireExportsInGeneratedCode(node *ast.Node
1006910071
parent := ast.GetDeclarationContainer(node)
1007010072
if ast.IsSourceFile(parent) && ast.IsExternalOrCommonJSModule(parent.AsSourceFile()) {
1007110073
// If the declaration happens to be in external module, report error that require and exports are reserved keywords
10072-
c.error(name, diagnostics.Duplicate_identifier_0_Compiler_reserves_name_1_in_top_level_scope_of_a_module, scanner.DeclarationNameToString(name), scanner.DeclarationNameToString(name))
10074+
c.errorSkippedOnNoEmit(name, diagnostics.Duplicate_identifier_0_Compiler_reserves_name_1_in_top_level_scope_of_a_module, scanner.DeclarationNameToString(name), scanner.DeclarationNameToString(name))
1007310075
}
1007410076
}
1007510077

@@ -13323,6 +13325,12 @@ func (c *Checker) error(location *ast.Node, message *diagnostics.Message, args .
1332313325
return diagnostic
1332413326
}
1332513327

13328+
func (c *Checker) errorSkippedOnNoEmit(location *ast.Node, message *diagnostics.Message, args ...any) *ast.Diagnostic {
13329+
diagnostic := c.error(location, message, args...)
13330+
diagnostic.SetSkippedOnNoEmit()
13331+
return diagnostic
13332+
}
13333+
1332613334
func (c *Checker) errorOrSuggestion(isError bool, location *ast.Node, message *diagnostics.Message, args ...any) {
1332713335
c.addErrorOrSuggestion(isError, NewDiagnosticForNode(location, message, args...))
1332813336
}
@@ -14808,6 +14816,17 @@ func (c *Checker) tryFindAmbientModule(moduleName string, withAugmentations bool
1480814816
return symbol
1480914817
}
1481014818

14819+
func (c *Checker) GetAmbientModules() []*ast.Symbol {
14820+
c.ambientModulesOnce.Do(func() {
14821+
for sym, global := range c.globals {
14822+
if strings.HasPrefix(sym, "\"") && strings.HasSuffix(sym, "\"") {
14823+
c.ambientModules = append(c.ambientModules, global)
14824+
}
14825+
}
14826+
})
14827+
return c.ambientModules
14828+
}
14829+
1481114830
func (c *Checker) resolveExternalModuleSymbol(moduleSymbol *ast.Symbol, dontResolveAlias bool) *ast.Symbol {
1481214831
if moduleSymbol != nil {
1481314832
exportEquals := c.resolveSymbolEx(moduleSymbol.Exports[ast.InternalSymbolNameExportEquals], dontResolveAlias)

internal/checker/checker_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ foo.bar;`
3636
fs = bundled.WrapFS(fs)
3737

3838
cd := "/"
39-
host := compiler.NewCompilerHost(cd, fs, bundled.LibPath())
39+
host := compiler.NewCompilerHost(cd, fs, bundled.LibPath(), nil)
4040

4141
parsed, errors := tsoptions.GetParsedCommandLineOfConfigFile("/tsconfig.json", &core.CompilerOptions{}, host, nil)
4242
assert.Equal(t, len(errors), 0, "Expected no errors in parsed command line")
@@ -70,14 +70,14 @@ func TestCheckSrcCompiler(t *testing.T) {
7070

7171
rootPath := tspath.CombinePaths(tspath.NormalizeSlashes(repo.TypeScriptSubmodulePath), "src", "compiler")
7272

73-
host := compiler.NewCompilerHost(rootPath, fs, bundled.LibPath())
73+
host := compiler.NewCompilerHost(rootPath, fs, bundled.LibPath(), nil)
7474
parsed, errors := tsoptions.GetParsedCommandLineOfConfigFile(tspath.CombinePaths(rootPath, "tsconfig.json"), &core.CompilerOptions{}, host, nil)
7575
assert.Equal(t, len(errors), 0, "Expected no errors in parsed command line")
7676
p := compiler.NewProgram(compiler.ProgramOptions{
7777
Config: parsed,
7878
Host: host,
7979
})
80-
p.CheckSourceFiles(t.Context())
80+
p.CheckSourceFiles(t.Context(), nil)
8181
}
8282

8383
func BenchmarkNewChecker(b *testing.B) {
@@ -87,7 +87,7 @@ func BenchmarkNewChecker(b *testing.B) {
8787

8888
rootPath := tspath.CombinePaths(tspath.NormalizeSlashes(repo.TypeScriptSubmodulePath), "src", "compiler")
8989

90-
host := compiler.NewCompilerHost(rootPath, fs, bundled.LibPath())
90+
host := compiler.NewCompilerHost(rootPath, fs, bundled.LibPath(), nil)
9191
parsed, errors := tsoptions.GetParsedCommandLineOfConfigFile(tspath.CombinePaths(rootPath, "tsconfig.json"), &core.CompilerOptions{}, host, nil)
9292
assert.Equal(b, len(errors), 0, "Expected no errors in parsed command line")
9393
p := compiler.NewProgram(compiler.ProgramOptions{

internal/checker/grammarchecks.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ func (c *Checker) grammarErrorOnNode(node *ast.Node, message *diagnostics.Messag
4242
return false
4343
}
4444

45+
func (c *Checker) grammarErrorOnNodeSkippedOnNoEmit(node *ast.Node, message *diagnostics.Message, args ...any) bool {
46+
sourceFile := ast.GetSourceFileOfNode(node)
47+
if !c.hasParseDiagnostics(sourceFile) {
48+
d := NewDiagnosticForNode(node, message, args...)
49+
d.SetSkippedOnNoEmit()
50+
c.diagnostics.Add(d)
51+
return true
52+
}
53+
return false
54+
}
55+
4556
func (c *Checker) checkGrammarRegularExpressionLiteral(_ *ast.RegularExpressionLiteral) bool {
4657
// !!!
4758
// Unclear if this is needed until regular expression parsing is more thoroughly implemented.
@@ -1648,7 +1659,7 @@ func (c *Checker) checkGrammarVariableDeclaration(node *ast.VariableDeclaration)
16481659
func (c *Checker) checkGrammarForEsModuleMarkerInBindingName(name *ast.Node) bool {
16491660
if ast.IsIdentifier(name) {
16501661
if name.Text() == "__esModule" {
1651-
return c.grammarErrorOnNode(name, diagnostics.Identifier_expected_esModule_is_reserved_as_an_exported_marker_when_transforming_ECMAScript_modules)
1662+
return c.grammarErrorOnNodeSkippedOnNoEmit(name, diagnostics.Identifier_expected_esModule_is_reserved_as_an_exported_marker_when_transforming_ECMAScript_modules)
16521663
}
16531664
} else {
16541665
for _, element := range name.AsBindingPattern().Elements.Nodes {

internal/checker/symbolaccessibility.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,7 @@ func (ch *Checker) IsAnySymbolAccessible(symbols []*ast.Symbol, enclosingDeclara
105105
}
106106

107107
func hasNonGlobalAugmentationExternalModuleSymbol(declaration *ast.Node) bool {
108-
return isModuleWithStringLiteralName(declaration) || (declaration.Kind == ast.KindSourceFile && ast.IsExternalOrCommonJSModule(declaration.AsSourceFile()))
109-
}
110-
111-
func isModuleWithStringLiteralName(node *ast.Node) bool {
112-
return ast.IsModuleDeclaration(node) && node.Name().Kind == ast.KindStringLiteral
108+
return ast.IsModuleWithStringLiteralName(declaration) || (declaration.Kind == ast.KindSourceFile && ast.IsExternalOrCommonJSModule(declaration.AsSourceFile()))
113109
}
114110

115111
func getQualifiedLeftMeaning(rightMeaning ast.SymbolFlags) ast.SymbolFlags {

internal/checker/utilities.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,8 +1460,8 @@ func forEachYieldExpression(body *ast.Node, visitor func(expr *ast.Node)) {
14601460
traverse(body)
14611461
}
14621462

1463-
func SkipTypeChecking(sourceFile *ast.SourceFile, options *core.CompilerOptions, host Program) bool {
1464-
return options.NoCheck.IsTrue() ||
1463+
func SkipTypeChecking(sourceFile *ast.SourceFile, options *core.CompilerOptions, host Program, ignoreNoCheck bool) bool {
1464+
return (!ignoreNoCheck && options.NoCheck.IsTrue()) ||
14651465
options.SkipLibCheck.IsTrue() && sourceFile.IsDeclarationFile ||
14661466
options.SkipDefaultLibCheck.IsTrue() && host.IsSourceFileDefaultLibrary(sourceFile.Path()) ||
14671467
host.IsSourceFromProjectReference(sourceFile.Path()) ||

internal/collections/manytomanyset.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package collections
2+
3+
import "fmt"
4+
5+
type ManyToManySet[K comparable, V comparable] struct {
6+
keyToValueSet map[K]*Set[V]
7+
valueToKeySet map[V]*Set[K]
8+
}
9+
10+
func (m *ManyToManySet[K, V]) GetKeys(value V) (*Set[K], bool) {
11+
keys, present := m.valueToKeySet[value]
12+
return keys, present
13+
}
14+
15+
func (m *ManyToManySet[K, V]) GetValues(key K) (*Set[V], bool) {
16+
values, present := m.keyToValueSet[key]
17+
return values, present
18+
}
19+
20+
func (m *ManyToManySet[K, V]) Len() int {
21+
return len(m.keyToValueSet)
22+
}
23+
24+
func (m *ManyToManySet[K, V]) Keys() map[K]*Set[V] {
25+
return m.keyToValueSet
26+
}
27+
28+
func (m *ManyToManySet[K, V]) Set(key K, valueSet *Set[V]) {
29+
_, hasExisting := m.keyToValueSet[key]
30+
if hasExisting {
31+
panic("ManyToManySet.Set: key already exists: " + fmt.Sprintf("%v", key))
32+
}
33+
if m.keyToValueSet == nil {
34+
m.keyToValueSet = make(map[K]*Set[V])
35+
}
36+
m.keyToValueSet[key] = valueSet
37+
for value := range valueSet.Keys() {
38+
// Add to valueToKeySet
39+
keySetForValue, exists := m.valueToKeySet[value]
40+
if !exists {
41+
if m.valueToKeySet == nil {
42+
m.valueToKeySet = make(map[V]*Set[K])
43+
}
44+
keySetForValue = &Set[K]{}
45+
m.valueToKeySet[value] = keySetForValue
46+
}
47+
keySetForValue.Add(key)
48+
}
49+
}

internal/collections/set.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package collections
22

3+
import "maps"
4+
35
type Set[T comparable] struct {
46
M map[T]struct{}
57
}
@@ -48,6 +50,24 @@ func (s *Set[T]) AddIfAbsent(key T) bool {
4850
return true
4951
}
5052

53+
func (s *Set[T]) Clone() *Set[T] {
54+
if s == nil {
55+
return nil
56+
}
57+
clone := &Set[T]{M: maps.Clone(s.M)}
58+
return clone
59+
}
60+
61+
func (s *Set[T]) Equals(other *Set[T]) bool {
62+
if s == other {
63+
return true
64+
}
65+
if s == nil || other == nil {
66+
return false
67+
}
68+
return maps.Equal(s.M, other.M)
69+
}
70+
5171
func NewSetFromItems[T comparable](items ...T) *Set[T] {
5272
s := &Set[T]{}
5373
for _, item := range items {

0 commit comments

Comments
 (0)