@data-vegle/rbxts-go 0.0.0-stage → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (220) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +181 -2
  3. package/README.rbxtsgo.md +169 -0
  4. package/THIRD_PARTY_NOTICES.md +745 -0
  5. package/cli/cli.go +454 -0
  6. package/compile/compile.go +388 -0
  7. package/compile/declarations.go +221 -0
  8. package/compile/incremental.go +349 -0
  9. package/compile/times.go +9 -0
  10. package/docs/plugin-api.md +243 -0
  11. package/go.mod +20 -0
  12. package/go.sum +24 -0
  13. package/internal/difftest/difftest.go +530 -0
  14. package/internal/plugintest/plugintest.go +181 -0
  15. package/luau/create.go +351 -0
  16. package/luau/doc.go +71 -0
  17. package/luau/globals.go +178 -0
  18. package/luau/jsnum.go +188 -0
  19. package/luau/kinds.go +128 -0
  20. package/luau/list.go +227 -0
  21. package/luau/nodes.go +479 -0
  22. package/luau/operators.go +46 -0
  23. package/luau/render.go +134 -0
  24. package/luau/render_expressions.go +264 -0
  25. package/luau/render_fields.go +81 -0
  26. package/luau/render_indexable.go +81 -0
  27. package/luau/render_state.go +134 -0
  28. package/luau/render_statements.go +235 -0
  29. package/luau/render_util.go +296 -0
  30. package/luau/solve_temp_ids.go +212 -0
  31. package/luau/typeguards.go +175 -0
  32. package/luau/util.go +184 -0
  33. package/luau/visit.go +138 -0
  34. package/npm/index.d.ts +3 -0
  35. package/npm/rbxts-go.sh +187 -0
  36. package/package.json +45 -5
  37. package/patches/typescript/0001-checker-exports.patch +40 -0
  38. package/patches/typescript/0002-iterable-arity.patch +15 -0
  39. package/patches/typescript/0003-context-sensitive-this.patch +15 -0
  40. package/patches/typescript/0004-printer-keep-parse-tree-parentheses.patch +26 -0
  41. package/pluginapi/host.go +31 -0
  42. package/pluginapi/pluginapi.go +153 -0
  43. package/plugins/LICENSE-rbxts-transformer-setget +18 -0
  44. package/plugins/apply.go +342 -0
  45. package/plugins/config.go +431 -0
  46. package/plugins/fallback.go +84 -0
  47. package/plugins/fixemit.go +60 -0
  48. package/plugins/native.go +271 -0
  49. package/plugins/session.go +236 -0
  50. package/plugins/setget.go +478 -0
  51. package/project/copy.go +333 -0
  52. package/project/include/Promise.lua +2068 -0
  53. package/project/include/RuntimeLib.lua +260 -0
  54. package/project/nodepath/nodepath.go +917 -0
  55. package/project/pathtranslator/pathtranslator.go +214 -0
  56. package/project/project.go +498 -0
  57. package/project/rojoresolver/json.go +248 -0
  58. package/project/rojoresolver/rojoresolver.go +559 -0
  59. package/project/shared/constants.go +83 -0
  60. package/project/shared/diagnostic.go +238 -0
  61. package/project/shared/diagnostics.go +388 -0
  62. package/project/shared/elapsed.go +14 -0
  63. package/project/shared/eol_other.go +6 -0
  64. package/project/shared/eol_windows.go +4 -0
  65. package/project/shared/kleur.go +40 -0
  66. package/project/shared/util.go +183 -0
  67. package/scripts/build-with-plugins.sh +130 -0
  68. package/scripts/setup-third-party.sh +75 -0
  69. package/transformer/add_index_diagnostics.go +21 -0
  70. package/transformer/add_one_if_array_type.go +15 -0
  71. package/transformer/array_binding_pattern_contains_hoists.go +25 -0
  72. package/transformer/assert_never.go +24 -0
  73. package/transformer/assignment.go +64 -0
  74. package/transformer/call_macros.go +70 -0
  75. package/transformer/check_variable_hoist.go +125 -0
  76. package/transformer/clean_module_name.go +28 -0
  77. package/transformer/constructor_macros.go +122 -0
  78. package/transformer/convert_to_indexable_expression.go +12 -0
  79. package/transformer/create_binary_from_operator.go +86 -0
  80. package/transformer/create_continue_statements.go +68 -0
  81. package/transformer/create_get_service.go +15 -0
  82. package/transformer/create_hoist_declaration.go +27 -0
  83. package/transformer/create_import_expression.go +220 -0
  84. package/transformer/create_string_from_literal.go +195 -0
  85. package/transformer/create_truthiness_checks.go +63 -0
  86. package/transformer/create_type_check.go +9 -0
  87. package/transformer/ensure_transform_order.go +53 -0
  88. package/transformer/expression_chain.go +27 -0
  89. package/transformer/expression_might_mutate.go +60 -0
  90. package/transformer/find_constructor.go +15 -0
  91. package/transformer/fixup_whitespace_and_decode_entities.go +377 -0
  92. package/transformer/get_accessor_for_binding_type.go +160 -0
  93. package/transformer/get_add_iterable_to_array_builder.go +363 -0
  94. package/transformer/get_assignable_value.go +15 -0
  95. package/transformer/get_constant_value_literal.go +20 -0
  96. package/transformer/get_declared_variables.go +34 -0
  97. package/transformer/get_extends_node.go +23 -0
  98. package/transformer/get_flags.go +21 -0
  99. package/transformer/get_kind_name.go +18 -0
  100. package/transformer/get_original_symbol_of_node.go +16 -0
  101. package/transformer/get_source_file_from_module_specifier.go +68 -0
  102. package/transformer/get_statements.go +12 -0
  103. package/transformer/has_multiple_definitions.go +18 -0
  104. package/transformer/hoist_functions_past_final_statement.go +43 -0
  105. package/transformer/identifier_macros.go +17 -0
  106. package/transformer/is_blocked_by_try_statement.go +19 -0
  107. package/transformer/is_method.go +104 -0
  108. package/transformer/is_symbol_mutable.go +26 -0
  109. package/transformer/is_symbol_of_value.go +11 -0
  110. package/transformer/is_used_as_statement.go +25 -0
  111. package/transformer/is_valid_method_index_without_call.go +36 -0
  112. package/transformer/macro_manager.go +92 -0
  113. package/transformer/object_accessor.go +32 -0
  114. package/transformer/offset.go +58 -0
  115. package/transformer/pointer.go +53 -0
  116. package/transformer/property_call_macros.go +1035 -0
  117. package/transformer/state.go +606 -0
  118. package/transformer/transform_array_assignment_pattern.go +65 -0
  119. package/transformer/transform_array_binding_pattern.go +49 -0
  120. package/transformer/transform_array_literal_expression.go +97 -0
  121. package/transformer/transform_await_expression.go +12 -0
  122. package/transformer/transform_binary_expression.go +221 -0
  123. package/transformer/transform_binding_name.go +25 -0
  124. package/transformer/transform_block.go +14 -0
  125. package/transformer/transform_boolean_literal.go +13 -0
  126. package/transformer/transform_break_statement.go +26 -0
  127. package/transformer/transform_call_expression.go +266 -0
  128. package/transformer/transform_class_constructor.go +125 -0
  129. package/transformer/transform_class_declaration.go +12 -0
  130. package/transformer/transform_class_expression.go +14 -0
  131. package/transformer/transform_class_like_declaration.go +355 -0
  132. package/transformer/transform_conditional_expression.go +70 -0
  133. package/transformer/transform_continue_statement.go +30 -0
  134. package/transformer/transform_decorators.go +253 -0
  135. package/transformer/transform_delete_expression.go +17 -0
  136. package/transformer/transform_do_statement.go +39 -0
  137. package/transformer/transform_element_access_expression.go +114 -0
  138. package/transformer/transform_entity_name.go +21 -0
  139. package/transformer/transform_enum_declaration.go +135 -0
  140. package/transformer/transform_export_assignment.go +57 -0
  141. package/transformer/transform_export_declaration.go +150 -0
  142. package/transformer/transform_expression.go +120 -0
  143. package/transformer/transform_expression_statement.go +71 -0
  144. package/transformer/transform_for_of_statement.go +461 -0
  145. package/transformer/transform_for_statement.go +599 -0
  146. package/transformer/transform_function_declaration.go +84 -0
  147. package/transformer/transform_function_expression.go +48 -0
  148. package/transformer/transform_identifier.go +194 -0
  149. package/transformer/transform_if_statement.go +44 -0
  150. package/transformer/transform_import_declaration.go +146 -0
  151. package/transformer/transform_import_equals_declaration.go +46 -0
  152. package/transformer/transform_import_expression.go +36 -0
  153. package/transformer/transform_initializer.go +22 -0
  154. package/transformer/transform_interpolated_string_part.go +12 -0
  155. package/transformer/transform_jsx.go +41 -0
  156. package/transformer/transform_jsx_attributes.go +110 -0
  157. package/transformer/transform_jsx_children.go +50 -0
  158. package/transformer/transform_jsx_element.go +14 -0
  159. package/transformer/transform_jsx_expression.go +20 -0
  160. package/transformer/transform_jsx_fragment.go +42 -0
  161. package/transformer/transform_jsx_self_closing_element.go +13 -0
  162. package/transformer/transform_jsx_tag_name.go +53 -0
  163. package/transformer/transform_logical.go +160 -0
  164. package/transformer/transform_logical_or_coalescing_assignment_expression.go +103 -0
  165. package/transformer/transform_method_declaration.go +102 -0
  166. package/transformer/transform_module_declaration.go +143 -0
  167. package/transformer/transform_new_expression.go +27 -0
  168. package/transformer/transform_no_substitution_template_literal.go +16 -0
  169. package/transformer/transform_numeric_literal.go +15 -0
  170. package/transformer/transform_object_assignment_pattern.go +86 -0
  171. package/transformer/transform_object_binding_pattern.go +49 -0
  172. package/transformer/transform_object_literal_expression.go +107 -0
  173. package/transformer/transform_omitted_expression.go +9 -0
  174. package/transformer/transform_optional_chain.go +312 -0
  175. package/transformer/transform_parameters.go +124 -0
  176. package/transformer/transform_parenthesized_expression.go +17 -0
  177. package/transformer/transform_property_access_expression.go +37 -0
  178. package/transformer/transform_property_declaration.go +33 -0
  179. package/transformer/transform_property_name.go +20 -0
  180. package/transformer/transform_return_statement.go +83 -0
  181. package/transformer/transform_source_file.go +230 -0
  182. package/transformer/transform_spread_element.go +35 -0
  183. package/transformer/transform_statement.go +84 -0
  184. package/transformer/transform_statement_list.go +90 -0
  185. package/transformer/transform_string_literal.go +22 -0
  186. package/transformer/transform_super_keyword.go +9 -0
  187. package/transformer/transform_switch_statement.go +145 -0
  188. package/transformer/transform_tagged_template_expression.go +34 -0
  189. package/transformer/transform_template_expression.go +35 -0
  190. package/transformer/transform_this_expression.go +33 -0
  191. package/transformer/transform_throw_statement.go +18 -0
  192. package/transformer/transform_try_statement.go +192 -0
  193. package/transformer/transform_type_expression.go +12 -0
  194. package/transformer/transform_unary_expression.go +79 -0
  195. package/transformer/transform_variable_statement.go +178 -0
  196. package/transformer/transform_void_expression.go +13 -0
  197. package/transformer/transform_while_statement.go +34 -0
  198. package/transformer/transform_writable.go +69 -0
  199. package/transformer/transform_yield_expression.go +56 -0
  200. package/transformer/type_guards.go +30 -0
  201. package/transformer/types.go +381 -0
  202. package/transformer/validate_identifier.go +17 -0
  203. package/transformer/validate_method_assignment.go +106 -0
  204. package/transformer/validate_not_any.go +37 -0
  205. package/transformer/wrap_expression_statement.go +20 -0
  206. package/transformer/wrap_return_if_lua_tuple.go +63 -0
  207. package/transformer/wrap_statements_as_generator.go +22 -0
  208. package/tsbridge/affected.go +141 -0
  209. package/tsbridge/config.go +71 -0
  210. package/tsbridge/diagnostics.go +128 -0
  211. package/tsbridge/doc.go +16 -0
  212. package/tsbridge/macros.go +207 -0
  213. package/tsbridge/macrospec.go +30 -0
  214. package/tsbridge/pool.go +142 -0
  215. package/tsbridge/project.go +137 -0
  216. package/tsbridge/reprint.go +120 -0
  217. package/tsbridge/update.go +128 -0
  218. package/watch/poll.go +126 -0
  219. package/watch/session.go +651 -0
  220. package/watch/watch.go +180 -0
@@ -0,0 +1,142 @@
1
+ package tsbridge
2
+
3
+ import (
4
+ "context"
5
+ "sync"
6
+
7
+ "github.com/microsoft/TypeScript/tsc/internal/ast"
8
+ "github.com/microsoft/TypeScript/tsc/internal/checker"
9
+ "github.com/microsoft/TypeScript/tsc/internal/printer"
10
+ )
11
+
12
+ type checkerGroup struct {
13
+ checker *checker.Checker
14
+ files []*ast.SourceFile
15
+ }
16
+
17
+ // groupByChecker groups files by the checker that owns them, in order of first appearance.
18
+ // GetTypeCheckerForFile takes no lock. Files keep their input order inside each group.
19
+ func (p *Project) groupByChecker(ctx context.Context, files []*ast.SourceFile) []*checkerGroup {
20
+ var groups []*checkerGroup
21
+ byChecker := map[*checker.Checker]*checkerGroup{}
22
+ for _, file := range files {
23
+ c, done := p.program.GetTypeCheckerForFile(ctx, file)
24
+ done()
25
+ group := byChecker[c]
26
+ if group == nil {
27
+ group = &checkerGroup{checker: c}
28
+ byChecker[c] = group
29
+ groups = append(groups, group)
30
+ }
31
+ group.files = append(group.files, file)
32
+ }
33
+ return groups
34
+ }
35
+
36
+ // Worker is the sole user of one checker. Anything keyed by ts.Type or ts.Symbol (macro tables,
37
+ // MultiTransformState, caches) belongs to one Worker, because those values mean nothing on another checker.
38
+ type Worker struct {
39
+ checker *checker.Checker
40
+ files []*ast.SourceFile
41
+ owned map[*ast.SourceFile]bool
42
+ resolver *checker.EmitResolver
43
+
44
+ macrosOnce sync.Once
45
+ macros *MacroSymbols
46
+ macrosErr error
47
+ macroSpec MacroSpec
48
+ }
49
+
50
+ // Checker returns this worker's checker. Don't pass it, or anything it returned, to another worker.
51
+ func (w *Worker) Checker() *checker.Checker { return w.checker }
52
+
53
+ // Files returns the files this worker owns, in input order.
54
+ func (w *Worker) Files() []*ast.SourceFile { return w.files }
55
+
56
+ // Macros returns this worker's macro symbol table, built on first use from the spec passed to NewWorkers.
57
+ func (w *Worker) Macros() (*MacroSymbols, error) {
58
+ w.macrosOnce.Do(func() { w.macros, w.macrosErr = NewMacroSymbols(w.checker, w.macroSpec) })
59
+ return w.macros, w.macrosErr
60
+ }
61
+
62
+ // CheckFileAndGetResolver checks file on this worker's checker, then returns that checker's emit resolver.
63
+ // It is the only way to get a resolver in the port, and it replaces ts.TypeChecker.getEmitResolver(file).
64
+ //
65
+ // Two traps live here. TS's getEmitResolver checks the file implicitly, and tsgo's NewEmitResolver doesn't:
66
+ // IsReferencedAliasDeclaration reads links that checking sets, so without the check it silently answers wrong.
67
+ // And resolver methods lock the mutex that the pool's exclusive access holds, so resolver calls under that
68
+ // lock deadlock. This panics instead, both for that and for a file owned by another checker.
69
+ func (w *Worker) CheckFileAndGetResolver(ctx context.Context, file *ast.SourceFile) *checker.EmitResolver {
70
+ if !w.owned[file] {
71
+ panic("tsbridge: " + file.FileName().AsString() + " belongs to another checker's worker")
72
+ }
73
+ if w.checker.IsMutexHeld() {
74
+ panic("tsbridge: checker mutex is held. CheckFileAndGetResolver must not run under GetTypeCheckerForFileExclusive or ForEachCheckerParallel, and diagnostics must not overlap with workers")
75
+ }
76
+ w.checker.GetDiagnostics(ctx, file)
77
+ if w.resolver == nil {
78
+ w.resolver = w.checker.NewEmitResolver(printer.NewEmitContext())
79
+ }
80
+ return w.resolver
81
+ }
82
+
83
+ // NewWorkers makes one worker per checker that owns at least one of files. With 7.1's partitioning,
84
+ // some checkers can own no files, so there can be fewer workers than checkers.
85
+ func (p *Project) NewWorkers(ctx context.Context, files []*ast.SourceFile, spec MacroSpec) []*Worker {
86
+ p.waitPrecheck()
87
+ groups := p.groupByChecker(ctx, files)
88
+ workers := make([]*Worker, len(groups))
89
+ for i, group := range groups {
90
+ owned := make(map[*ast.SourceFile]bool, len(group.files))
91
+ for _, file := range group.files {
92
+ owned[file] = true
93
+ }
94
+ workers[i] = &Worker{checker: group.checker, files: group.files, owned: owned, macroSpec: spec}
95
+ }
96
+ return workers
97
+ }
98
+
99
+ // RunWorkers runs fn for every worker, one goroutine per worker, and waits. Each worker sees all of its files
100
+ // in one fn call, so it can keep per-checker state between them. A panic in fn is re-raised here, after the
101
+ // other workers finish. Diagnostics must not run until this returns.
102
+ func (p *Project) RunWorkers(workers []*Worker, fn func(w *Worker)) {
103
+ p.waitPrecheck()
104
+ if !p.workersRunning.CompareAndSwap(false, true) {
105
+ panic("tsbridge: RunWorkers is already running")
106
+ }
107
+ defer p.workersRunning.Store(false)
108
+ var wg sync.WaitGroup
109
+ panics := make([]any, len(workers))
110
+ for i, w := range workers {
111
+ wg.Add(1)
112
+ go func() {
113
+ defer wg.Done()
114
+ defer func() { panics[i] = recover() }()
115
+ fn(w)
116
+ }()
117
+ }
118
+ wg.Wait()
119
+ for _, r := range panics {
120
+ if r != nil {
121
+ panic(r)
122
+ }
123
+ }
124
+ }
125
+
126
+ // PluginChecker returns the one checker that the native plugin pass uses for every file of the program. It is the
127
+ // pool's first checker. A checker can answer about any file of its program: the pool only decides which checker does
128
+ // the full check of which file. So this is safe as long as nothing else uses that checker in the meantime, which is
129
+ // the caller's side of the rule:
130
+ //
131
+ // - Every use of the checker, and of the types and symbols it returns, is on one goroutine.
132
+ // - No workers and no diagnostics run on this project while that goroutine works. In a build with native plugins
133
+ // nothing does at any time, because the compile uses the reprinted project, which has its own checkers.
134
+ //
135
+ // The pool makes all of its checkers on first use, so the others of this project are made and then left idle.
136
+ func (p *Project) PluginChecker(ctx context.Context) *checker.Checker {
137
+ p.waitPrecheck()
138
+ p.assertNotInWorkerPhase()
139
+ c, done := p.program.GetTypeChecker(ctx)
140
+ done()
141
+ return c
142
+ }
@@ -0,0 +1,137 @@
1
+ package tsbridge
2
+
3
+ import (
4
+ "fmt"
5
+ "os"
6
+ "path/filepath"
7
+ "sync/atomic"
8
+
9
+ "github.com/microsoft/TypeScript/tsc/internal/ast"
10
+ "github.com/microsoft/TypeScript/tsc/internal/bundled"
11
+ "github.com/microsoft/TypeScript/tsc/internal/compiler"
12
+ "github.com/microsoft/TypeScript/tsc/internal/core"
13
+ "github.com/microsoft/TypeScript/tsc/internal/execute/tsc"
14
+ "github.com/microsoft/TypeScript/tsc/internal/outputpaths"
15
+ "github.com/microsoft/TypeScript/tsc/internal/tsoptions"
16
+ "github.com/microsoft/TypeScript/tsc/internal/tspath"
17
+ "github.com/microsoft/TypeScript/tsc/internal/vfs/osvfs"
18
+ )
19
+
20
+ // DefaultCheckers matches tsgo's own default pool size.
21
+ const DefaultCheckers = 4
22
+
23
+ // ParsedConfig is a tsconfig as written, before normalization. Validate the original options on this:
24
+ // the validation in src/Project requires "moduleResolution": "Node", which normalization rewrites.
25
+ type ParsedConfig struct {
26
+ Config *tsoptions.ParsedCommandLine
27
+ Diagnostics []*ast.Diagnostic
28
+ Cwd tspath.RootedDirectoryPath
29
+ Path tspath.RootedFilePath
30
+ }
31
+
32
+ // ParseConfig reads a tsconfig. The diagnostics are the parser's, as in ts.getParsedCommandLineOfConfigFile.
33
+ func ParseConfig(tsconfigPath string) (*ParsedConfig, error) {
34
+ cwd, err := os.Getwd()
35
+ if err != nil {
36
+ return nil, err
37
+ }
38
+ rootedCwd := tspath.RootedDirectoryPathFromAbsolute(tspath.NormalizePath(filepath.ToSlash(cwd)))
39
+ path := tspath.ToRootedFilePath(filepath.ToSlash(tsconfigPath), rootedCwd)
40
+ config, diags := tsoptions.GetParsedCommandLineOfConfigFile(path, nil, nil, bundled.WrapFS(osvfs.FS()), &tsc.ExtendedConfigCache{})
41
+ if config == nil {
42
+ return nil, fmt.Errorf("could not parse %s", path)
43
+ }
44
+ return &ParsedConfig{Config: config, Diagnostics: diags, Cwd: rootedCwd, Path: path}, nil
45
+ }
46
+
47
+ // ProjectOptions configures NewProject.
48
+ type ProjectOptions struct {
49
+ // Checkers is the pool size. Zero means DefaultCheckers. tsgo caps it at the number of program files.
50
+ Checkers int
51
+ // Cache keeps parsed files between programs. Set it to use Project.Update.
52
+ Cache *SourceFileCache
53
+ }
54
+
55
+ // Project is a program built from a normalized tsconfig.
56
+ type Project struct {
57
+ config *tsoptions.ParsedCommandLine
58
+ program *compiler.Program
59
+ notes []string
60
+ cache *SourceFileCache
61
+ graphState
62
+
63
+ // workersRunning catches diagnostics overlapping with the workers, which hold no lock.
64
+ workersRunning atomic.Bool
65
+
66
+ // precheck is closed when a background check started by Precheck has finished. Nil when none was started.
67
+ precheck chan struct{}
68
+ }
69
+
70
+ // NewProject normalizes the config in place and creates the program.
71
+ func NewProject(parsed *ParsedConfig, options ProjectOptions) *Project {
72
+ config := parsed.Config
73
+ opts, notes := NormalizeOptions(config.CompilerOptions())
74
+ checkers := options.Checkers
75
+ if checkers == 0 {
76
+ checkers = DefaultCheckers
77
+ }
78
+ opts.Checkers = &checkers
79
+ config.SetCompilerOptions(opts)
80
+ // createProgramFactory passes rootNames, options, host and oldProgram, so JS's program never sees references. An import
81
+ // across projects gets the source file and TS6059, not a project reference's output.
82
+ config.ParsedConfig.ProjectReferences = nil
83
+
84
+ if options.Cache != nil {
85
+ host, done := newCycleHost(options.Cache)
86
+ defer done()
87
+ program := compiler.NewProgram(compiler.ProgramOptions{Config: config, Host: host})
88
+ options.Cache.retain(program)
89
+ return &Project{config: config, program: program, notes: notes, cache: options.Cache}
90
+ }
91
+ host := compiler.NewCachedFSCompilerHost(bundled.WrapFS(osvfs.FS()), bundled.LibPath(), &tsc.ExtendedConfigCache{}, nil, nil)
92
+ program := compiler.NewProgram(compiler.ProgramOptions{Config: config, Host: host})
93
+ return &Project{config: config, program: program, notes: notes}
94
+ }
95
+
96
+ // Program returns the underlying program. See the package comment for the calls that aren't allowed on it.
97
+ func (p *Project) Program() *compiler.Program { return p.program }
98
+
99
+ // Options returns the normalized compiler options.
100
+ func (p *Project) Options() *core.CompilerOptions { return p.config.CompilerOptions() }
101
+
102
+ // Config returns the normalized parsed config.
103
+ func (p *Project) Config() *tsoptions.ParsedCommandLine { return p.config }
104
+
105
+ // NormalizationNotes describes what NormalizeOptions changed.
106
+ func (p *Project) NormalizationNotes() []string { return p.notes }
107
+
108
+ // SourceFilesToCompile mirrors getChangedSourceFiles on a fresh build, where every file counts as changed:
109
+ // all program files except declaration files and JSON, in program order. This includes a non-declaration
110
+ // .ts file inside node_modules, because rbxtsc doesn't exclude those either.
111
+ func (p *Project) SourceFilesToCompile() []*ast.SourceFile {
112
+ var files []*ast.SourceFile
113
+ for _, file := range p.program.SourceFiles() {
114
+ if !file.IsDeclarationFile && !ast.IsJsonSourceFile(file) {
115
+ files = append(files, file)
116
+ }
117
+ }
118
+ return files
119
+ }
120
+
121
+ // CommonSourceDirectory is program.getCommonSourceDirectory() as TS 5.5.3 computes it. Don't use
122
+ // Program.CommonSourceDirectory for output paths: since TS 6 it falls back to the tsconfig's folder when rootDir is
123
+ // unset, where TS 5 uses the common folder of the emitted files. A project with only rootDirs would get different
124
+ // output paths.
125
+ func (p *Project) CommonSourceDirectory() tspath.RootedDirectoryPath {
126
+ options := p.Options()
127
+ if options.RootDir != "" || options.Composite.IsTrue() {
128
+ return p.program.CommonSourceDirectory()
129
+ }
130
+ var emitted []tspath.RootedFilePath
131
+ for _, file := range p.program.SourceFiles() {
132
+ if !file.IsDeclarationFile && !p.program.IsSourceFileFromExternalLibrary(file) {
133
+ emitted = append(emitted, file.FileName())
134
+ }
135
+ }
136
+ return outputpaths.GetComputedCommonSourceDirectory(emitted, p.program.BaseDirectory(), p.program.CaseSensitivity())
137
+ }
@@ -0,0 +1,120 @@
1
+ package tsbridge
2
+
3
+ import (
4
+ "os"
5
+
6
+ "github.com/microsoft/TypeScript/tsc/internal/ast"
7
+ "github.com/microsoft/TypeScript/tsc/internal/bundled"
8
+ "github.com/microsoft/TypeScript/tsc/internal/compiler"
9
+ "github.com/microsoft/TypeScript/tsc/internal/core"
10
+ "github.com/microsoft/TypeScript/tsc/internal/execute/tsc"
11
+ "github.com/microsoft/TypeScript/tsc/internal/parser"
12
+ "github.com/microsoft/TypeScript/tsc/internal/tspath"
13
+ "github.com/microsoft/TypeScript/tsc/internal/vfs/osvfs"
14
+ )
15
+
16
+ // reprintHost serves printed text for some files, and the previous program's parsed and bound files for the rest.
17
+ type reprintHost struct {
18
+ compiler.CompilerHost
19
+ printed map[tspath.PathKey]string
20
+ previous map[tspath.PathKey]*ast.SourceFile
21
+ }
22
+
23
+ func (h *reprintHost) GetSourceFile(options ast.SourceFileParseOptions) *ast.SourceFile {
24
+ if text, ok := h.printed[options.PathKey]; ok {
25
+ return parser.ParseSourceFile(options, text, core.EnsureScriptKindFromFileName(options.FileName))
26
+ }
27
+ if file := h.previous[options.PathKey]; file != nil {
28
+ return file
29
+ }
30
+ return h.CompilerHost.GetSourceFile(options)
31
+ }
32
+
33
+ // WithPrintedFiles makes a new project on the same config where the given files have the given text. It stands for
34
+ // data.transformerWatcher in compileFiles.ts: JS calls updateFile for each file and gets a new program from a language
35
+ // service. Every other file, such as all the declaration files, keeps its parse and binding, so those aren't redone.
36
+ //
37
+ // The result has no file cache, so it can't be used with Update.
38
+ func (p *Project) WithPrintedFiles(printed map[tspath.PathKey]string) *Project {
39
+ host := &reprintHost{
40
+ CompilerHost: compiler.NewCachedFSCompilerHost(bundled.WrapFS(osvfs.FS()), bundled.LibPath(), &tsc.ExtendedConfigCache{}, nil, nil),
41
+ printed: printed,
42
+ previous: p.program.FilesByPath(),
43
+ }
44
+ program := compiler.NewProgram(compiler.ProgramOptions{Config: p.config, Host: host})
45
+ return &Project{config: p.config, program: program, notes: p.notes}
46
+ }
47
+
48
+ // UpdatePrinted is WithPrintedFiles for a later compile: the receiver is the last printed project, source is the program
49
+ // the text came from, changed holds the text that differs from last time, and texts says which files are printed at all.
50
+ // Every other printed file keeps its parsed and bound object. full says the source changed in a way that can move imports,
51
+ // so reusing the old program's module resolution isn't safe.
52
+ func (p *Project) UpdatePrinted(source *Project, changed map[tspath.PathKey]string, texts map[tspath.PathKey]string, full bool) *Project {
53
+ sourceFiles := source.program.FilesByPath()
54
+ previous := p.program.FilesByPath()
55
+ host := &reprintHost{
56
+ CompilerHost: compiler.NewCachedFSCompilerHost(bundled.WrapFS(osvfs.FS()), bundled.LibPath(), &tsc.ExtendedConfigCache{}, nil, nil),
57
+ printed: changed,
58
+ }
59
+ // The printed program can hold files the source program lacks: the ones only printed text imports, such as a
60
+ // module that a plugin's generated code brings in. They stay as they are while the file on disk has the text they
61
+ // were parsed from. Parsing one again would give a new object for the same declaration file, which reads as a
62
+ // change to the global scope and makes every file dirty.
63
+ extras := map[tspath.PathKey]*ast.SourceFile{}
64
+ extrasChanged := false
65
+ for path, old := range previous {
66
+ if _, ok := sourceFiles[path]; ok {
67
+ continue
68
+ }
69
+ if text, err := os.ReadFile(old.FileName().AsString()); err == nil && string(text) == old.Text() {
70
+ extras[path] = old
71
+ } else {
72
+ extrasChanged = true
73
+ }
74
+ }
75
+ // Reuse needs the same files, with only printed ones different.
76
+ reusable := !full && !extrasChanged && len(sourceFiles)+len(extras) == len(previous)
77
+ if reusable {
78
+ for path, file := range sourceFiles {
79
+ old, ok := previous[path]
80
+ if _, printed := texts[path]; !ok || (!printed && old != file) {
81
+ reusable = false
82
+ break
83
+ }
84
+ }
85
+ }
86
+ host.previous = previous
87
+ if reusable {
88
+ if len(changed) == 0 {
89
+ return p
90
+ }
91
+ program := p.program
92
+ ok := true
93
+ for path := range changed {
94
+ program, _, ok = program.ReuseProgram(path, host, nil, nil)
95
+ if !ok {
96
+ break
97
+ }
98
+ }
99
+ if ok {
100
+ return &Project{config: p.config, program: program, notes: p.notes}
101
+ }
102
+ }
103
+ // Unprinted files come from the new source program, and printed ones from the old printed program.
104
+ merged := make(map[tspath.PathKey]*ast.SourceFile, len(sourceFiles))
105
+ for path, file := range sourceFiles {
106
+ if _, printed := texts[path]; printed {
107
+ if old := previous[path]; old != nil {
108
+ merged[path] = old
109
+ }
110
+ } else {
111
+ merged[path] = file
112
+ }
113
+ }
114
+ for path, old := range extras {
115
+ merged[path] = old
116
+ }
117
+ host.previous = merged
118
+ program := compiler.NewProgram(compiler.ProgramOptions{Config: source.config, Host: host})
119
+ return &Project{config: source.config, program: program, notes: source.notes}
120
+ }
@@ -0,0 +1,128 @@
1
+ package tsbridge
2
+
3
+ import (
4
+ "time"
5
+
6
+ "github.com/microsoft/TypeScript/tsc/internal/ast"
7
+ "github.com/microsoft/TypeScript/tsc/internal/bundled"
8
+ "github.com/microsoft/TypeScript/tsc/internal/collections"
9
+ "github.com/microsoft/TypeScript/tsc/internal/compiler"
10
+ "github.com/microsoft/TypeScript/tsc/internal/execute/tsc"
11
+ "github.com/microsoft/TypeScript/tsc/internal/tspath"
12
+ "github.com/microsoft/TypeScript/tsc/internal/vfs/cachedvfs"
13
+ "github.com/microsoft/TypeScript/tsc/internal/vfs/osvfs"
14
+ )
15
+
16
+ type cachedSourceFile struct {
17
+ file *ast.SourceFile
18
+ modTime time.Time
19
+ }
20
+
21
+ // SourceFileCache keeps parsed files between programs in one process, as execute/watcher.go does. An entry is good
22
+ // while the file's mtime stands, and watch mode also evicts on every event, so a save inside the clock's resolution
23
+ // can't be missed. Evict everything after a config change, because the parse options can differ.
24
+ type SourceFileCache struct {
25
+ files collections.SyncMap[tspath.PathKey, *cachedSourceFile]
26
+ }
27
+
28
+ // NewSourceFileCache makes an empty cache.
29
+ func NewSourceFileCache() *SourceFileCache { return &SourceFileCache{} }
30
+
31
+ // Evict forgets one file.
32
+ func (c *SourceFileCache) Evict(path tspath.PathKey) { c.files.Delete(path) }
33
+
34
+ // Clear forgets every file.
35
+ func (c *SourceFileCache) Clear() {
36
+ c.files.Range(func(path tspath.PathKey, _ *cachedSourceFile) bool {
37
+ c.files.Delete(path)
38
+ return true
39
+ })
40
+ }
41
+
42
+ // retain forgets every file that the program doesn't have, so deleted files don't pile up.
43
+ func (c *SourceFileCache) retain(program *compiler.Program) {
44
+ files := program.FilesByPath()
45
+ c.files.Range(func(path tspath.PathKey, _ *cachedSourceFile) bool {
46
+ if _, ok := files[path]; !ok {
47
+ c.files.Delete(path)
48
+ }
49
+ return true
50
+ })
51
+ }
52
+
53
+ // cachingHost is watchCompilerHost from execute/watcher.go, which is unexported there.
54
+ type cachingHost struct {
55
+ compiler.CompilerHost
56
+ cache *SourceFileCache
57
+ }
58
+
59
+ func (h *cachingHost) GetSourceFile(options ast.SourceFileParseOptions) *ast.SourceFile {
60
+ info := h.CompilerHost.FS().Stat(options.FileName.AsPath())
61
+ if cached, ok := h.cache.files.Load(options.PathKey); ok {
62
+ if info != nil && info.ModTime().Equal(cached.modTime) {
63
+ return cached.file
64
+ }
65
+ }
66
+ file := h.CompilerHost.GetSourceFile(options)
67
+ if file != nil && info != nil {
68
+ h.cache.files.Store(options.PathKey, &cachedSourceFile{file: file, modTime: info.ModTime()})
69
+ } else {
70
+ h.cache.files.Delete(options.PathKey)
71
+ }
72
+ return file
73
+ }
74
+
75
+ // newCycleHost makes a host for one program. The stat and read cache must not outlive the program's construction:
76
+ // keeping one across cycles would build from stale contents. So the caller gets a function that turns it off.
77
+ func newCycleHost(cache *SourceFileCache) (compiler.CompilerHost, func()) {
78
+ cached := cachedvfs.From(bundled.WrapFS(osvfs.FS()))
79
+ host := compiler.NewCompilerHost(cached, bundled.LibPath(), &tsc.ExtendedConfigCache{}, nil, nil)
80
+ return &cachingHost{CompilerHost: host, cache: cache}, cached.DisableAndClearCache
81
+ }
82
+
83
+ // Update makes the project for the next build, with changedPaths evicted from the cache. It tries
84
+ // Program.ReuseProgram, which keeps parsing, binding and module resolution for files whose imports didn't change, and
85
+ // otherwise falls back to NewProgram with the cached files. reloadFileNames says that a file was added or removed, so
86
+ // the tsconfig's patterns have to be expanded again, and it always means NewProgram. The receiver isn't changed, and
87
+ // it must not be used again: drop it so its checkers can be collected.
88
+ //
89
+ // The project must have been made with ProjectOptions.Cache.
90
+ func (p *Project) Update(changedPaths []tspath.PathKey, reloadFileNames bool) (next *Project, reused bool) {
91
+ if p.cache == nil {
92
+ panic("tsbridge: Update needs a project made with ProjectOptions.Cache")
93
+ }
94
+ for _, path := range changedPaths {
95
+ p.cache.Evict(path)
96
+ }
97
+ host, done := newCycleHost(p.cache)
98
+ defer done()
99
+
100
+ config := p.config
101
+ if reloadFileNames {
102
+ config = config.ReloadFileNamesOfParsedCommandLine(host.FS())
103
+ }
104
+ next = &Project{config: config, notes: p.notes, cache: p.cache}
105
+
106
+ if !reloadFileNames {
107
+ program := p.program
108
+ ok := true
109
+ for _, path := range changedPaths {
110
+ if _, inProgram := program.FilesByPath()[path]; !inProgram {
111
+ // Not part of the program, so no reuse can tell whether it changes module resolution.
112
+ ok = false
113
+ break
114
+ }
115
+ program, _, ok = program.ReuseProgram(path, host, nil, nil)
116
+ if !ok {
117
+ break
118
+ }
119
+ }
120
+ if ok {
121
+ next.program = program
122
+ return next, true
123
+ }
124
+ }
125
+ next.program = compiler.NewProgram(compiler.ProgramOptions{Config: config, Host: host})
126
+ p.cache.retain(next.program)
127
+ return next, false
128
+ }
package/watch/poll.go ADDED
@@ -0,0 +1,126 @@
1
+ package watch
2
+
3
+ import (
4
+ "io"
5
+ "io/fs"
6
+ "os"
7
+ "path/filepath"
8
+ "sync"
9
+ "time"
10
+
11
+ "github.com/microsoft/TypeScript/tsc/internal/execute/watchmanager"
12
+ "github.com/microsoft/TypeScript/tsc/internal/fswatch"
13
+ "github.com/microsoft/TypeScript/tsc/internal/tspath"
14
+ )
15
+
16
+ // pollingBackend is what --usePolling asks of chokidar. fswatch only has native notifications, so this walks the
17
+ // watched folders on a timer. It's for filesystems where those don't work, such as network shares.
18
+ type pollingBackend struct {
19
+ interval time.Duration
20
+ }
21
+
22
+ func newPollingBackend(interval time.Duration) *pollingBackend {
23
+ return &pollingBackend{interval: interval}
24
+ }
25
+
26
+ type polledEntry struct {
27
+ modTime time.Time
28
+ size int64
29
+ isDir bool
30
+ }
31
+
32
+ type pollWatch struct {
33
+ stop chan struct{}
34
+ done chan struct{}
35
+ once sync.Once
36
+ }
37
+
38
+ func (w *pollWatch) Close() error {
39
+ w.once.Do(func() { close(w.stop) })
40
+ <-w.done
41
+ return nil
42
+ }
43
+
44
+ func (b *pollingBackend) WatchDirectories(requests []watchmanager.WatchDirectoryRequest) ([]io.Closer, error) {
45
+ closers := make([]io.Closer, len(requests))
46
+ for i, request := range requests {
47
+ // The first snapshot is taken here so that a change made right after this returns gets seen.
48
+ previous := snapshotDirectory(request)
49
+ watch := &pollWatch{stop: make(chan struct{}), done: make(chan struct{})}
50
+ closers[i] = watch
51
+ go func() {
52
+ defer close(watch.done)
53
+ ticker := time.NewTicker(b.interval)
54
+ defer ticker.Stop()
55
+ for {
56
+ select {
57
+ case <-watch.stop:
58
+ return
59
+ case <-ticker.C:
60
+ }
61
+ current := snapshotDirectory(request)
62
+ if events := diffSnapshots(previous, current); len(events) > 0 {
63
+ request.Callback(events, nil)
64
+ }
65
+ previous = current
66
+ }
67
+ }()
68
+ }
69
+ return closers, nil
70
+ }
71
+
72
+ func snapshotDirectory(request watchmanager.WatchDirectoryRequest) map[string]polledEntry {
73
+ result := map[string]polledEntry{}
74
+ root := filepath.FromSlash(request.Dir.AsString())
75
+ _ = filepath.WalkDir(root, func(path string, entry fs.DirEntry, err error) error {
76
+ if err != nil {
77
+ if entry != nil && entry.IsDir() {
78
+ return fs.SkipDir
79
+ }
80
+ return nil
81
+ }
82
+ if path == root {
83
+ return nil
84
+ }
85
+ slashed := filepath.ToSlash(path)
86
+ if request.Ignore != nil && request.Ignore(tspath.RootedPathFromAbsolute(slashed)) {
87
+ if entry.IsDir() {
88
+ return fs.SkipDir
89
+ }
90
+ return nil
91
+ }
92
+ info, err := entry.Info()
93
+ if err != nil {
94
+ if os.IsNotExist(err) {
95
+ return nil
96
+ }
97
+ return nil
98
+ }
99
+ // A folder's mtime changes with its contents, which get their own events.
100
+ if info.IsDir() {
101
+ result[slashed] = polledEntry{isDir: true}
102
+ } else {
103
+ result[slashed] = polledEntry{modTime: info.ModTime(), size: info.Size()}
104
+ }
105
+ if entry.IsDir() && !request.Recursive {
106
+ return fs.SkipDir
107
+ }
108
+ return nil
109
+ })
110
+ return result
111
+ }
112
+
113
+ func diffSnapshots(previous, current map[string]polledEntry) []watchmanager.WatchEvent {
114
+ var events []watchmanager.WatchEvent
115
+ for path, entry := range current {
116
+ if old, ok := previous[path]; !ok || old != entry {
117
+ events = append(events, watchmanager.WatchEvent{Path: tspath.RootedPathFromAbsolute(path), Kind: fswatch.EventUpdate})
118
+ }
119
+ }
120
+ for path := range previous {
121
+ if _, ok := current[path]; !ok {
122
+ events = append(events, watchmanager.WatchEvent{Path: tspath.RootedPathFromAbsolute(path), Kind: fswatch.EventDelete})
123
+ }
124
+ }
125
+ return events
126
+ }