ttsc 0.12.3 → 0.13.0
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.
- package/cmd/platform/main.go +9 -0
- package/cmd/ttsc/api_compile.go +122 -101
- package/cmd/ttsc/api_compile_branches_test.go +55 -57
- package/cmd/ttsc/api_transform.go +67 -49
- package/cmd/ttsc/api_transform_branches_test.go +30 -32
- package/cmd/ttsc/build.go +141 -108
- package/cmd/ttsc/build_branches_test.go +95 -97
- package/cmd/ttsc/filter.go +70 -0
- package/cmd/ttsc/filter_strips_unknown_forwarded_flag_test.go +61 -0
- package/cmd/ttsc/flags_gen.go +30 -0
- package/cmd/ttsc/helpers_test.go +39 -28
- package/cmd/ttsc/main.go +111 -104
- package/cmd/ttsc/main_alias_branches_test.go +21 -23
- package/cmd/ttscserver/main.go +96 -90
- package/cmd/ttscserver/run_lsp_prefers_tsgo_flag_test.go +22 -22
- package/cmd/ttscserver/run_lsp_reports_server_error_test.go +22 -22
- package/cmd/ttscserver/run_lsp_uses_tsgo_environment_test.go +23 -23
- package/cmd/utility-host/main.go +30 -23
- package/driver/lsp.go +53 -7
- package/driver/plugins.go +88 -72
- package/driver/program.go +464 -340
- package/driver/rewrite.go +477 -380
- package/internal/lspserver/lsp_envelope.go +64 -64
- package/internal/lspserver/lsp_frame.go +60 -60
- package/internal/lspserver/lsp_plugin.go +57 -57
- package/internal/lspserver/lsp_proxy.go +265 -251
- package/internal/lspserver/lsp_server.go +155 -144
- package/lib/TtscCompiler.d.ts +7 -0
- package/lib/TtscCompiler.js +7 -0
- package/lib/TtscCompiler.js.map +1 -1
- package/lib/compiler/internal/buildNativeCompiler.d.ts +11 -1
- package/lib/compiler/internal/buildNativeCompiler.js +23 -3
- package/lib/compiler/internal/buildNativeCompiler.js.map +1 -1
- package/lib/compiler/internal/compileProjectInMemory.d.ts +9 -0
- package/lib/compiler/internal/compileProjectInMemory.js +36 -21
- package/lib/compiler/internal/compileProjectInMemory.js.map +1 -1
- package/lib/compiler/internal/paths.d.ts +27 -0
- package/lib/compiler/internal/paths.js +70 -0
- package/lib/compiler/internal/paths.js.map +1 -0
- package/lib/compiler/internal/project/readProjectConfig.d.ts +9 -1
- package/lib/compiler/internal/project/readProjectConfig.js +80 -1
- package/lib/compiler/internal/project/readProjectConfig.js.map +1 -1
- package/lib/compiler/internal/project/resolveProjectConfig.d.ts +13 -1
- package/lib/compiler/internal/project/resolveProjectConfig.js +42 -1
- package/lib/compiler/internal/project/resolveProjectConfig.js.map +1 -1
- package/lib/compiler/internal/resolveBinary.d.ts +9 -1
- package/lib/compiler/internal/resolveBinary.js +17 -1
- package/lib/compiler/internal/resolveBinary.js.map +1 -1
- package/lib/compiler/internal/resolveEmittedJavaScript.d.ts +16 -1
- package/lib/compiler/internal/resolveEmittedJavaScript.js +42 -10
- package/lib/compiler/internal/resolveEmittedJavaScript.js.map +1 -1
- package/lib/compiler/internal/resolveTsgo.d.ts +21 -1
- package/lib/compiler/internal/resolveTsgo.js +23 -1
- package/lib/compiler/internal/resolveTsgo.js.map +1 -1
- package/lib/compiler/internal/runBuild.d.ts +24 -4
- package/lib/compiler/internal/runBuild.js +355 -93
- package/lib/compiler/internal/runBuild.js.map +1 -1
- package/lib/compiler/internal/runSingleFileEmit.d.ts +10 -1
- package/lib/compiler/internal/runSingleFileEmit.js +14 -1
- package/lib/compiler/internal/runSingleFileEmit.js.map +1 -1
- package/lib/compiler/internal/sharedHostHelpers.d.ts +5 -0
- package/lib/compiler/internal/sharedHostHelpers.js +6 -2
- package/lib/compiler/internal/sharedHostHelpers.js.map +1 -1
- package/lib/compiler/internal/spawnNative.d.ts +21 -0
- package/lib/compiler/internal/spawnNative.js +59 -0
- package/lib/compiler/internal/spawnNative.js.map +1 -0
- package/lib/compiler/internal/transformProjectInMemory.d.ts +15 -1
- package/lib/compiler/internal/transformProjectInMemory.js +57 -61
- package/lib/compiler/internal/transformProjectInMemory.js.map +1 -1
- package/lib/flags/parser.d.ts +63 -0
- package/lib/flags/parser.js +254 -0
- package/lib/flags/parser.js.map +1 -0
- package/lib/flags/schema.d.ts +147 -0
- package/lib/flags/schema.js +405 -0
- package/lib/flags/schema.js.map +1 -0
- package/lib/launcher/internal/getCompilerVersionText.js +7 -0
- package/lib/launcher/internal/getCompilerVersionText.js.map +1 -1
- package/lib/launcher/internal/prepareExecution.js +35 -0
- package/lib/launcher/internal/prepareExecution.js.map +1 -1
- package/lib/launcher/internal/resolveCacheDir.d.ts +9 -0
- package/lib/launcher/internal/resolveCacheDir.js +22 -0
- package/lib/launcher/internal/resolveCacheDir.js.map +1 -0
- package/lib/launcher/internal/resolveTtscserverBinary.js +2 -0
- package/lib/launcher/internal/resolveTtscserverBinary.js.map +1 -1
- package/lib/launcher/internal/runTtsc.d.ts +10 -0
- package/lib/launcher/internal/runTtsc.js +90 -153
- package/lib/launcher/internal/runTtsc.js.map +1 -1
- package/lib/launcher/internal/runTtscserver.js +10 -35
- package/lib/launcher/internal/runTtscserver.js.map +1 -1
- package/lib/launcher/internal/runTtsx.d.ts +8 -0
- package/lib/launcher/internal/runTtsx.js +137 -79
- package/lib/launcher/internal/runTtsx.js.map +1 -1
- package/lib/plugin/internal/buildSourcePlugin.d.ts +38 -0
- package/lib/plugin/internal/buildSourcePlugin.js +40 -17
- package/lib/plugin/internal/buildSourcePlugin.js.map +1 -1
- package/lib/plugin/internal/loadProjectPlugins.d.ts +26 -0
- package/lib/plugin/internal/loadProjectPlugins.js +90 -41
- package/lib/plugin/internal/loadProjectPlugins.js.map +1 -1
- package/lib/structures/ITtscCompilerTransformation.d.ts +15 -2
- package/lib/structures/ITtscPlugin.d.ts +24 -0
- package/lib/structures/ITtscPluginCapabilities.d.ts +29 -0
- package/lib/structures/ITtscPluginCapabilities.js +3 -0
- package/lib/structures/ITtscPluginCapabilities.js.map +1 -0
- package/lib/structures/index.d.ts +1 -0
- package/lib/structures/index.js +1 -0
- package/lib/structures/index.js.map +1 -1
- package/lib/structures/internal/ITtscLoadedNativePlugin.d.ts +5 -0
- package/lib/structures/internal/TtscCommonOptions.d.ts +19 -0
- package/lib/structures/internal/TtscSingleFileEmitOptions.d.ts +0 -2
- package/package.json +9 -9
- package/shim/ast/shim.go +24 -0
- package/shim/checker/shim.go +35 -0
- package/shim/core/shim.go +17 -0
- package/shim/diagnosticwriter/shim.go +4 -0
- package/shim/lsp/shim.go +3 -3
- package/shim/printer/shim.go +20 -0
- package/src/TtscCompiler.ts +7 -0
- package/src/compiler/internal/buildNativeCompiler.ts +23 -3
- package/src/compiler/internal/compileProjectInMemory.ts +34 -25
- package/src/compiler/internal/paths.ts +68 -0
- package/src/compiler/internal/project/readProjectConfig.ts +86 -1
- package/src/compiler/internal/project/resolveProjectConfig.ts +44 -1
- package/src/compiler/internal/resolveBinary.ts +17 -1
- package/src/compiler/internal/resolveEmittedJavaScript.ts +43 -12
- package/src/compiler/internal/resolveTsgo.ts +29 -1
- package/src/compiler/internal/runBuild.ts +398 -112
- package/src/compiler/internal/runSingleFileEmit.ts +14 -1
- package/src/compiler/internal/sharedHostHelpers.ts +7 -7
- package/src/compiler/internal/spawnNative.ts +64 -0
- package/src/compiler/internal/transformProjectInMemory.ts +47 -67
- package/src/flags/parser.ts +367 -0
- package/src/flags/schema.ts +570 -0
- package/src/launcher/internal/getCompilerVersionText.ts +7 -0
- package/src/launcher/internal/prepareExecution.ts +35 -0
- package/src/launcher/internal/resolveCacheDir.ts +19 -0
- package/src/launcher/internal/resolveTtscserverBinary.ts +2 -0
- package/src/launcher/internal/runTtsc.ts +97 -143
- package/src/launcher/internal/runTtscserver.ts +9 -1
- package/src/launcher/internal/runTtsx.ts +149 -82
- package/src/plugin/internal/buildSourcePlugin.ts +40 -14
- package/src/plugin/internal/loadProjectPlugins.ts +116 -45
- package/src/structures/ITtscCompilerTransformation.ts +15 -2
- package/src/structures/ITtscPlugin.ts +26 -1
- package/src/structures/ITtscPluginCapabilities.ts +29 -0
- package/src/structures/index.ts +1 -0
- package/src/structures/internal/ITtscLoadedNativePlugin.ts +5 -0
- package/src/structures/internal/TtscCommonOptions.ts +19 -0
- package/src/structures/internal/TtscSingleFileEmitOptions.ts +0 -2
- package/tools/gen_shims/main.go +43 -8
- package/utility/flags_gen.go +30 -0
- package/utility/host.go +304 -245
|
@@ -3,6 +3,7 @@ export * from "./ITtscCompilerDiagnostic";
|
|
|
3
3
|
export * from "./ITtscCompilerResult";
|
|
4
4
|
export * from "./ITtscCompilerTransformation";
|
|
5
5
|
export * from "./ITtscPlugin";
|
|
6
|
+
export * from "./ITtscPluginCapabilities";
|
|
6
7
|
export * from "./ITtscPluginContributor";
|
|
7
8
|
export * from "./ITtscPluginFactoryContext";
|
|
8
9
|
export * from "./ITtscProjectPluginConfig";
|
package/lib/structures/index.js
CHANGED
|
@@ -19,6 +19,7 @@ __exportStar(require("./ITtscCompilerDiagnostic"), exports);
|
|
|
19
19
|
__exportStar(require("./ITtscCompilerResult"), exports);
|
|
20
20
|
__exportStar(require("./ITtscCompilerTransformation"), exports);
|
|
21
21
|
__exportStar(require("./ITtscPlugin"), exports);
|
|
22
|
+
__exportStar(require("./ITtscPluginCapabilities"), exports);
|
|
22
23
|
__exportStar(require("./ITtscPluginContributor"), exports);
|
|
23
24
|
__exportStar(require("./ITtscPluginFactoryContext"), exports);
|
|
24
25
|
__exportStar(require("./ITtscProjectPluginConfig"), exports);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/structures/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,yDAAuC;AACvC,4DAA0C;AAC1C,wDAAsC;AACtC,gEAA8C;AAC9C,gDAA8B;AAC9B,2DAAyC;AACzC,8DAA4C;AAC5C,6DAA2C;AAC3C,oDAAkC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/structures/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,yDAAuC;AACvC,4DAA0C;AAC1C,wDAAsC;AACtC,gEAA8C;AAC9C,gDAA8B;AAC9B,4DAA0C;AAC1C,2DAAyC;AACzC,8DAA4C;AAC5C,6DAA2C;AAC3C,oDAAkC"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ITtscPluginCapabilities } from "../ITtscPluginCapabilities";
|
|
1
2
|
import type { ITtscPluginContributor } from "../ITtscPluginContributor";
|
|
2
3
|
import type { ITtscProjectPluginConfig } from "../ITtscProjectPluginConfig";
|
|
3
4
|
import type { TtscPluginStage } from "../TtscPluginStage";
|
|
@@ -5,6 +6,8 @@ import type { TtscPluginStage } from "../TtscPluginStage";
|
|
|
5
6
|
export interface ITtscLoadedNativePlugin {
|
|
6
7
|
/** Executable produced by the lazy Go source build cache. */
|
|
7
8
|
binary: string;
|
|
9
|
+
/** Capability flags declared by the descriptor (see `ITtscPluginCapabilities`). */
|
|
10
|
+
capabilities?: ITtscPluginCapabilities;
|
|
8
11
|
/** Original tsconfig plugin entry passed unchanged to the sidecar. */
|
|
9
12
|
config: ITtscProjectPluginConfig;
|
|
10
13
|
/** Contributor Go sources statically linked into the binary, if any. */
|
|
@@ -13,6 +16,8 @@ export interface ITtscLoadedNativePlugin {
|
|
|
13
16
|
kind: "executable" | "linked";
|
|
14
17
|
/** Optional human label used in diagnostics and native manifests. */
|
|
15
18
|
name?: string;
|
|
19
|
+
/** Whether a check-stage plugin already emits TypeScript diagnostics. */
|
|
20
|
+
reportsTypeScriptDiagnostics?: boolean;
|
|
16
21
|
/** Go source directory selected by the plugin descriptor. */
|
|
17
22
|
source: string;
|
|
18
23
|
/** Pipeline stage where the sidecar participates. */
|
|
@@ -21,6 +21,25 @@ export interface TtscCommonOptions {
|
|
|
21
21
|
cacheDir?: string;
|
|
22
22
|
/** Normalize compiler output so diagnostics can be parsed structurally. */
|
|
23
23
|
structuredDiagnostics?: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Run TypeScript-Go single-threaded — one checker, serial parse/check/emit.
|
|
26
|
+
* Mirrors `tsgo --singleThreaded`. Useful for deterministic debugging and CI
|
|
27
|
+
* repro.
|
|
28
|
+
*/
|
|
29
|
+
singleThreaded?: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Type-checker pool size, mirroring `tsgo --checkers`. `undefined` leaves
|
|
32
|
+
* TypeScript-Go's default; ignored when `singleThreaded` is set.
|
|
33
|
+
*/
|
|
34
|
+
checkers?: number;
|
|
35
|
+
/**
|
|
36
|
+
* CLI tokens ttsc did not recognize as its own, forwarded verbatim to the
|
|
37
|
+
* underlying `tsgo` invocation. This is how a tsgo flag ttsc has no first-
|
|
38
|
+
* class option for (`--strict`, `--target es2020`, `--listFiles`, …) still
|
|
39
|
+
* reaches the compiler: ttsc owns its own flags and lets tsgo — which has the
|
|
40
|
+
* complete, arity-aware option parser — handle the rest.
|
|
41
|
+
*/
|
|
42
|
+
passthrough?: readonly string[];
|
|
24
43
|
/**
|
|
25
44
|
* Override project plugin loading for this invocation.
|
|
26
45
|
*
|
|
@@ -9,6 +9,4 @@ export interface TtscSingleFileEmitOptions extends TtscCommonOptions {
|
|
|
9
9
|
out?: string;
|
|
10
10
|
/** Suppress summary banners from ttsc/native sidecars. Defaults to `true`. */
|
|
11
11
|
quiet?: boolean;
|
|
12
|
-
/** Normalize compiler output so diagnostics can be parsed structurally. */
|
|
13
|
-
structuredDiagnostics?: boolean;
|
|
14
12
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ttsc",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"description": "General-purpose TypeScript-Go compiler, plugin host, and runtime.",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -28,17 +28,17 @@
|
|
|
28
28
|
"homepage": "https://ttsc.dev",
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@types/node": "^25.3.0",
|
|
31
|
-
"@typescript/native-preview": "7.0.0-dev.
|
|
31
|
+
"@typescript/native-preview": "7.0.0-dev.20260522.1",
|
|
32
32
|
"rimraf": "^6.1.2"
|
|
33
33
|
},
|
|
34
34
|
"optionalDependencies": {
|
|
35
|
-
"@ttsc/linux-
|
|
36
|
-
"@ttsc/
|
|
37
|
-
"@ttsc/
|
|
38
|
-
"@ttsc/
|
|
39
|
-
"@ttsc/win32-x64": "0.
|
|
40
|
-
"@ttsc/
|
|
41
|
-
"@ttsc/
|
|
35
|
+
"@ttsc/linux-arm": "0.13.0",
|
|
36
|
+
"@ttsc/linux-x64": "0.13.0",
|
|
37
|
+
"@ttsc/linux-arm64": "0.13.0",
|
|
38
|
+
"@ttsc/darwin-arm64": "0.13.0",
|
|
39
|
+
"@ttsc/win32-x64": "0.13.0",
|
|
40
|
+
"@ttsc/win32-arm64": "0.13.0",
|
|
41
|
+
"@ttsc/darwin-x64": "0.13.0"
|
|
42
42
|
},
|
|
43
43
|
"sideEffects": false,
|
|
44
44
|
"files": [
|
package/shim/ast/shim.go
CHANGED
|
@@ -177,44 +177,68 @@ const (
|
|
|
177
177
|
ModifierFlagsReadonly = innerast.ModifierFlagsReadonly
|
|
178
178
|
)
|
|
179
179
|
|
|
180
|
+
// NewNodeFactory creates an AST node factory with the supplied creation hooks.
|
|
181
|
+
// Pass a zero-value NodeFactoryHooks to get the default factory behaviour.
|
|
180
182
|
func NewNodeFactory(options NodeFactoryHooks) *NodeFactory {
|
|
181
183
|
return innerast.NewNodeFactory(options)
|
|
182
184
|
}
|
|
183
185
|
|
|
186
|
+
// NewNodeVisitor creates a visitor that calls visit for each node encountered
|
|
187
|
+
// during a tree walk, delegating node recreation to factory using hooks.
|
|
184
188
|
func NewNodeVisitor(visit func(node *Node) *Node, factory *NodeFactory, options NodeVisitorHooks) *NodeVisitor {
|
|
185
189
|
return innerast.NewNodeVisitor(visit, factory, options)
|
|
186
190
|
}
|
|
187
191
|
|
|
192
|
+
// IsFunctionLike reports whether node is any function-like construct
|
|
193
|
+
// (function declaration, arrow function, method, constructor, accessor, etc.).
|
|
188
194
|
func IsFunctionLike(node *Node) bool {
|
|
189
195
|
return innerast.IsFunctionLike(node)
|
|
190
196
|
}
|
|
191
197
|
|
|
198
|
+
// IsPropertyAssignment reports whether node is a key:value pair inside an
|
|
199
|
+
// object literal expression.
|
|
192
200
|
func IsPropertyAssignment(node *Node) bool {
|
|
193
201
|
return innerast.IsPropertyAssignment(node)
|
|
194
202
|
}
|
|
195
203
|
|
|
204
|
+
// IsPropertyDeclaration reports whether node is a class property declaration.
|
|
196
205
|
func IsPropertyDeclaration(node *Node) bool {
|
|
197
206
|
return innerast.IsPropertyDeclaration(node)
|
|
198
207
|
}
|
|
199
208
|
|
|
209
|
+
// IsModuleBlock reports whether node is the body block of a namespace or
|
|
210
|
+
// module declaration.
|
|
200
211
|
func IsModuleBlock(node *Node) bool {
|
|
201
212
|
return innerast.IsModuleBlock(node)
|
|
202
213
|
}
|
|
203
214
|
|
|
215
|
+
// IsStringLiteral reports whether node is a string literal token.
|
|
204
216
|
func IsStringLiteral(node *Node) bool {
|
|
205
217
|
return innerast.IsStringLiteral(node)
|
|
206
218
|
}
|
|
207
219
|
|
|
220
|
+
// IsPropertyAccessExpression reports whether node is a dotted member access
|
|
221
|
+
// (e.g. `a.b`).
|
|
208
222
|
func IsPropertyAccessExpression(node *Node) bool {
|
|
209
223
|
return innerast.IsPropertyAccessExpression(node)
|
|
210
224
|
}
|
|
211
225
|
|
|
226
|
+
// IsElementAccessExpression reports whether node is a bracket member access
|
|
227
|
+
// (e.g. `a[b]`).
|
|
212
228
|
func IsElementAccessExpression(node *Node) bool {
|
|
213
229
|
return innerast.IsElementAccessExpression(node)
|
|
214
230
|
}
|
|
215
231
|
|
|
232
|
+
// GetSourceFileOfNode walks up the parent chain to return the SourceFile that
|
|
233
|
+
// owns the given node. Linked from the internal package via go:linkname because
|
|
234
|
+
// the function is unexported there.
|
|
235
|
+
//
|
|
216
236
|
//go:linkname GetSourceFileOfNode github.com/microsoft/typescript-go/internal/ast.GetSourceFileOfNode
|
|
217
237
|
func GetSourceFileOfNode(node *innerast.Node) *innerast.SourceFile
|
|
218
238
|
|
|
239
|
+
// GetNodeAtPosition returns the deepest AST node whose range covers position
|
|
240
|
+
// in file. When includeJSDoc is true the search descends into JSDoc sub-trees.
|
|
241
|
+
// Linked from the internal package via go:linkname.
|
|
242
|
+
//
|
|
219
243
|
//go:linkname GetNodeAtPosition github.com/microsoft/typescript-go/internal/ast.GetNodeAtPosition
|
|
220
244
|
func GetNodeAtPosition(file *innerast.SourceFile, position int, includeJSDoc bool) *innerast.Node
|
package/shim/checker/shim.go
CHANGED
|
@@ -59,34 +59,50 @@ const (
|
|
|
59
59
|
ElementFlagsVariadic = innerchecker.ElementFlagsVariadic
|
|
60
60
|
)
|
|
61
61
|
|
|
62
|
+
// IsTupleType reports whether t is a fixed-length tuple type.
|
|
62
63
|
func IsTupleType(t *innerchecker.Type) bool {
|
|
63
64
|
return innerchecker.IsTupleType(t)
|
|
64
65
|
}
|
|
65
66
|
|
|
67
|
+
// Checker_getIndexInfosOfType returns the index signatures (string/number/symbol
|
|
68
|
+
// index infos) declared on t.
|
|
66
69
|
func Checker_getIndexInfosOfType(recv *innerchecker.Checker, t *innerchecker.Type) []*innerchecker.IndexInfo {
|
|
67
70
|
return recv.GetIndexInfosOfType(t)
|
|
68
71
|
}
|
|
69
72
|
|
|
73
|
+
// Checker_getPropertiesOfType returns the named property symbols of t. For
|
|
74
|
+
// union and intersection types this is the set of properties visible on every
|
|
75
|
+
// member.
|
|
70
76
|
func Checker_getPropertiesOfType(recv *innerchecker.Checker, t *innerchecker.Type) []*innerast.Symbol {
|
|
71
77
|
return recv.GetPropertiesOfType(t)
|
|
72
78
|
}
|
|
73
79
|
|
|
80
|
+
// Checker_getApparentProperties returns the properties visible on t after
|
|
81
|
+
// resolving primitive wrapper types (e.g. string → String).
|
|
74
82
|
func Checker_getApparentProperties(recv *innerchecker.Checker, t *innerchecker.Type) []*innerast.Symbol {
|
|
75
83
|
return recv.GetApparentProperties(t)
|
|
76
84
|
}
|
|
77
85
|
|
|
86
|
+
// Checker_getTypeArguments returns the type arguments of a generic reference
|
|
87
|
+
// type, or nil when t is not a reference.
|
|
78
88
|
func Checker_getTypeArguments(recv *innerchecker.Checker, t *innerchecker.Type) []*innerchecker.Type {
|
|
79
89
|
return recv.GetTypeArguments(t)
|
|
80
90
|
}
|
|
81
91
|
|
|
92
|
+
// Checker_getTypeOfSymbol returns the declared type of symbol, resolving
|
|
93
|
+
// aliases and following late-bound types.
|
|
82
94
|
func Checker_getTypeOfSymbol(recv *innerchecker.Checker, symbol *innerast.Symbol) *innerchecker.Type {
|
|
83
95
|
return recv.GetTypeOfSymbol(symbol)
|
|
84
96
|
}
|
|
85
97
|
|
|
98
|
+
// Checker_getTypeOfSymbolAtLocation returns the contextual type of symbol as
|
|
99
|
+
// observed at the given AST node (useful for narrowed types in control flow).
|
|
86
100
|
func Checker_getTypeOfSymbolAtLocation(recv *innerchecker.Checker, symbol *innerast.Symbol, node *innerast.Node) *innerchecker.Type {
|
|
87
101
|
return recv.GetTypeOfSymbolAtLocation(symbol, node)
|
|
88
102
|
}
|
|
89
103
|
|
|
104
|
+
// Checker_getTypeOfPropertyOfType looks up the type of the named property on t
|
|
105
|
+
// and returns nil when no such property exists.
|
|
90
106
|
func Checker_getTypeOfPropertyOfType(recv *innerchecker.Checker, t *innerchecker.Type, name string) *innerchecker.Type {
|
|
91
107
|
return recv.GetTypeOfPropertyOfType(t, name)
|
|
92
108
|
}
|
|
@@ -94,6 +110,8 @@ func Checker_getTypeOfPropertyOfType(recv *innerchecker.Checker, t *innerchecker
|
|
|
94
110
|
//go:linkname checkerGetAliasSymbolForTypeNode github.com/microsoft/typescript-go/internal/checker.(*Checker).getAliasSymbolForTypeNode
|
|
95
111
|
func checkerGetAliasSymbolForTypeNode(recv *innerchecker.Checker, node *innerast.Node) *innerast.Symbol
|
|
96
112
|
|
|
113
|
+
// Checker_getAliasSymbolForTypeNode returns the alias symbol that a type node
|
|
114
|
+
// refers to when the node is itself a type alias reference (e.g. `type Foo = ...`).
|
|
97
115
|
func Checker_getAliasSymbolForTypeNode(recv *innerchecker.Checker, node *innerast.Node) *innerast.Symbol {
|
|
98
116
|
return checkerGetAliasSymbolForTypeNode(recv, node)
|
|
99
117
|
}
|
|
@@ -101,6 +119,8 @@ func Checker_getAliasSymbolForTypeNode(recv *innerchecker.Checker, node *inneras
|
|
|
101
119
|
//go:linkname checkerGetDeclarationOfAliasSymbol github.com/microsoft/typescript-go/internal/checker.(*Checker).getDeclarationOfAliasSymbol
|
|
102
120
|
func checkerGetDeclarationOfAliasSymbol(recv *innerchecker.Checker, symbol *innerast.Symbol) *innerast.Node
|
|
103
121
|
|
|
122
|
+
// Checker_getDeclarationOfAliasSymbol resolves an import/export alias symbol to
|
|
123
|
+
// its original declaration node.
|
|
104
124
|
func Checker_getDeclarationOfAliasSymbol(recv *innerchecker.Checker, symbol *innerast.Symbol) *innerast.Node {
|
|
105
125
|
return checkerGetDeclarationOfAliasSymbol(recv, symbol)
|
|
106
126
|
}
|
|
@@ -108,6 +128,8 @@ func Checker_getDeclarationOfAliasSymbol(recv *innerchecker.Checker, symbol *inn
|
|
|
108
128
|
//go:linkname checkerGetTargetOfImportSpecifier github.com/microsoft/typescript-go/internal/checker.(*Checker).getTargetOfImportSpecifier
|
|
109
129
|
func checkerGetTargetOfImportSpecifier(recv *innerchecker.Checker, node *innerast.Node) *innerast.Symbol
|
|
110
130
|
|
|
131
|
+
// Checker_getTargetOfImportSpecifier resolves an import specifier node to the
|
|
132
|
+
// exported symbol it binds. Returns nil if recv or node is nil.
|
|
111
133
|
func Checker_getTargetOfImportSpecifier(recv *innerchecker.Checker, node *innerast.Node) *innerast.Symbol {
|
|
112
134
|
if recv == nil || node == nil {
|
|
113
135
|
return nil
|
|
@@ -115,6 +137,8 @@ func Checker_getTargetOfImportSpecifier(recv *innerchecker.Checker, node *innera
|
|
|
115
137
|
return checkerGetTargetOfImportSpecifier(recv, node)
|
|
116
138
|
}
|
|
117
139
|
|
|
140
|
+
// Checker_getAliasedSymbol follows an alias chain to its final target symbol.
|
|
141
|
+
// Returns nil if recv or symbol is nil.
|
|
118
142
|
func Checker_getAliasedSymbol(recv *innerchecker.Checker, symbol *innerast.Symbol) *innerast.Symbol {
|
|
119
143
|
if recv == nil || symbol == nil {
|
|
120
144
|
return nil
|
|
@@ -132,6 +156,11 @@ func checkerResolveEntityName(
|
|
|
132
156
|
location *innerast.Node,
|
|
133
157
|
) *innerast.Symbol
|
|
134
158
|
|
|
159
|
+
// Checker_resolveEntityName resolves a dotted entity name (identifier or
|
|
160
|
+
// qualified name) to the symbol it denotes, filtered by meaning flags.
|
|
161
|
+
// When ignoreErrors is true, resolution failures are silent. When
|
|
162
|
+
// dontResolveAlias is true, the returned symbol may still be an alias.
|
|
163
|
+
// Returns nil if recv or name is nil.
|
|
135
164
|
func Checker_resolveEntityName(
|
|
136
165
|
recv *innerchecker.Checker,
|
|
137
166
|
name *innerast.Node,
|
|
@@ -149,6 +178,9 @@ func Checker_resolveEntityName(
|
|
|
149
178
|
//go:linkname checkerGetTypeNameSymbol github.com/microsoft/typescript-go/internal/checker.getTypeNameSymbol
|
|
150
179
|
func checkerGetTypeNameSymbol(t *innerchecker.Type) *innerast.Symbol
|
|
151
180
|
|
|
181
|
+
// Type_getTypeNameSymbol returns the symbol attached to t's type name field,
|
|
182
|
+
// or nil when t has no name symbol or t is nil. Linked via go:linkname because
|
|
183
|
+
// getTypeNameSymbol is a package-level unexported function in the checker.
|
|
152
184
|
func Type_getTypeNameSymbol(t *innerchecker.Type) *innerast.Symbol {
|
|
153
185
|
if t == nil {
|
|
154
186
|
return nil
|
|
@@ -159,6 +191,7 @@ func Type_getTypeNameSymbol(t *innerchecker.Type) *innerast.Symbol {
|
|
|
159
191
|
//go:linkname checkerIsArrayType github.com/microsoft/typescript-go/internal/checker.(*Checker).isArrayType
|
|
160
192
|
func checkerIsArrayType(recv *innerchecker.Checker, t *innerchecker.Type) bool
|
|
161
193
|
|
|
194
|
+
// Checker_isArrayType reports whether t is the built-in Array<T> reference type.
|
|
162
195
|
func Checker_isArrayType(recv *innerchecker.Checker, t *innerchecker.Type) bool {
|
|
163
196
|
return checkerIsArrayType(recv, t)
|
|
164
197
|
}
|
|
@@ -166,6 +199,8 @@ func Checker_isArrayType(recv *innerchecker.Checker, t *innerchecker.Type) bool
|
|
|
166
199
|
//go:linkname checkerGetBaseTypes github.com/microsoft/typescript-go/internal/checker.(*Checker).getBaseTypes
|
|
167
200
|
func checkerGetBaseTypes(recv *innerchecker.Checker, t *innerchecker.Type) []*innerchecker.Type
|
|
168
201
|
|
|
202
|
+
// Checker_getBaseTypes returns the list of base types (from `extends` clauses)
|
|
203
|
+
// for a class or interface type. Returns nil if recv or t is nil.
|
|
169
204
|
func Checker_getBaseTypes(recv *innerchecker.Checker, t *innerchecker.Type) []*innerchecker.Type {
|
|
170
205
|
if recv == nil || t == nil {
|
|
171
206
|
return nil
|
package/shim/core/shim.go
CHANGED
|
@@ -1,17 +1,34 @@
|
|
|
1
|
+
// Package core re-exports the subset of typescript-go's internal/core types
|
|
2
|
+
// and constants that plugins and the ttsc driver need. It provides compiler
|
|
3
|
+
// options, script-kind discrimination, tri-state booleans, and text-range
|
|
4
|
+
// primitives without exposing the full internal surface.
|
|
1
5
|
package core
|
|
2
6
|
|
|
3
7
|
import innercore "github.com/microsoft/typescript-go/internal/core"
|
|
4
8
|
|
|
9
|
+
// CompilerOptions holds the parsed tsconfig compiler options passed to the
|
|
10
|
+
// TypeScript-Go program host.
|
|
5
11
|
type CompilerOptions = innercore.CompilerOptions
|
|
12
|
+
|
|
13
|
+
// Tristate is a three-valued boolean: TSFalse, TSTrue, or TSUnknown. Used by
|
|
14
|
+
// CompilerOptions fields that can be explicitly unset.
|
|
6
15
|
type Tristate = innercore.Tristate
|
|
16
|
+
|
|
17
|
+
// TextPos is a zero-based byte offset into a source file's text.
|
|
7
18
|
type TextPos = innercore.TextPos
|
|
19
|
+
|
|
20
|
+
// TextRange is a half-open [Pos, End) byte range inside a source file.
|
|
8
21
|
type TextRange = innercore.TextRange
|
|
22
|
+
|
|
23
|
+
// ScriptKind identifies the syntactic flavour of a source file.
|
|
9
24
|
type ScriptKind = innercore.ScriptKind
|
|
10
25
|
|
|
11
26
|
const (
|
|
27
|
+
// TSFalse and TSTrue are the explicit-false and explicit-true Tristate values.
|
|
12
28
|
TSFalse = innercore.TSFalse
|
|
13
29
|
TSTrue = innercore.TSTrue
|
|
14
30
|
|
|
31
|
+
// ScriptKind* constants enumerate the file flavours typescript-go recognises.
|
|
15
32
|
ScriptKindUnknown = innercore.ScriptKindUnknown
|
|
16
33
|
ScriptKindJS = innercore.ScriptKindJS
|
|
17
34
|
ScriptKindJSX = innercore.ScriptKindJSX
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
// Package diagnosticwriter re-exports the subset of typescript-go's
|
|
2
|
+
// internal/diagnosticwriter surface that the ttsc driver uses. It provides
|
|
3
|
+
// FormatASTDiagnosticsWithColorAndContext for rendering pure tsgo diagnostics
|
|
4
|
+
// and delegates the mixed lint+tsgo rendering path to the adjacent lint.go.
|
|
1
5
|
package diagnosticwriter
|
|
2
6
|
|
|
3
7
|
import (
|
package/shim/lsp/shim.go
CHANGED
|
@@ -8,10 +8,10 @@
|
|
|
8
8
|
package lsp
|
|
9
9
|
|
|
10
10
|
import (
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
"io"
|
|
12
|
+
_ "unsafe"
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
innerlsp "github.com/microsoft/typescript-go/internal/lsp"
|
|
15
15
|
)
|
|
16
16
|
|
|
17
17
|
// Server is the opaque LSP server type from tsgo.
|
package/shim/printer/shim.go
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
// Package printer re-exports the typescript-go internal/printer types and
|
|
2
|
+
// wraps the three functions that the ttsc driver and transform plugins need
|
|
3
|
+
// to emit source text from an AST. The surface is intentionally narrow: only
|
|
4
|
+
// the construction and single-file emit path are exposed.
|
|
1
5
|
package printer
|
|
2
6
|
|
|
3
7
|
import (
|
|
@@ -5,19 +9,35 @@ import (
|
|
|
5
9
|
innerprinter "github.com/microsoft/typescript-go/internal/printer"
|
|
6
10
|
)
|
|
7
11
|
|
|
12
|
+
// PrintHandlers provides optional hooks (e.g. substituteNode) called during
|
|
13
|
+
// AST emission.
|
|
8
14
|
type PrintHandlers = innerprinter.PrintHandlers
|
|
15
|
+
|
|
16
|
+
// Printer holds the stateful emitter produced by NewPrinter.
|
|
9
17
|
type Printer = innerprinter.Printer
|
|
18
|
+
|
|
19
|
+
// PrinterOptions configures newline style, source-map generation, and other
|
|
20
|
+
// emission knobs.
|
|
10
21
|
type PrinterOptions = innerprinter.PrinterOptions
|
|
22
|
+
|
|
23
|
+
// EmitContext accumulates per-emit metadata (source maps, comment positions)
|
|
24
|
+
// and must be created fresh for each emit round via NewEmitContext.
|
|
11
25
|
type EmitContext = innerprinter.EmitContext
|
|
12
26
|
|
|
27
|
+
// NewPrinter creates an emitter with the supplied options, substitution hooks,
|
|
28
|
+
// and emit context. Callers must pass the same EmitContext to all operations
|
|
29
|
+
// in a single emit round.
|
|
13
30
|
func NewPrinter(options PrinterOptions, handlers PrintHandlers, emitContext *EmitContext) *Printer {
|
|
14
31
|
return innerprinter.NewPrinter(options, handlers, emitContext)
|
|
15
32
|
}
|
|
16
33
|
|
|
34
|
+
// NewEmitContext allocates a fresh EmitContext for a new emit round.
|
|
17
35
|
func NewEmitContext() *EmitContext {
|
|
18
36
|
return innerprinter.NewEmitContext()
|
|
19
37
|
}
|
|
20
38
|
|
|
39
|
+
// EmitSourceFile renders the full source file through the printer and returns
|
|
40
|
+
// the emitted text.
|
|
21
41
|
func EmitSourceFile(p *Printer, sourceFile *ast.SourceFile) string {
|
|
22
42
|
return p.EmitSourceFile(sourceFile)
|
|
23
43
|
}
|
package/src/TtscCompiler.ts
CHANGED
|
@@ -36,6 +36,13 @@ import type { TtscBuildResult } from "./structures/internal/TtscBuildResult";
|
|
|
36
36
|
export class TtscCompiler {
|
|
37
37
|
private readonly context: ITtscCompilerContext;
|
|
38
38
|
|
|
39
|
+
/**
|
|
40
|
+
* Create a new compiler instance bound to the given project context.
|
|
41
|
+
*
|
|
42
|
+
* The context is defensively copied: mutations to the original object after
|
|
43
|
+
* construction do not affect this instance. Omit `context` (or pass `{}`) to
|
|
44
|
+
* inherit all defaults from the running process.
|
|
45
|
+
*/
|
|
39
46
|
public constructor(context: ITtscCompilerContext = {}) {
|
|
40
47
|
this.context = {
|
|
41
48
|
...context,
|
|
@@ -3,7 +3,17 @@ import path from "node:path";
|
|
|
3
3
|
|
|
4
4
|
import { buildSourcePlugin } from "../../plugin/internal/buildSourcePlugin";
|
|
5
5
|
|
|
6
|
-
/**
|
|
6
|
+
/**
|
|
7
|
+
* Build (or retrieve from cache) the native ttsc compiler host binary used by
|
|
8
|
+
* the in-memory API compilation paths (`compileProjectInMemory`,
|
|
9
|
+
* `transformProjectInMemory`).
|
|
10
|
+
*
|
|
11
|
+
* The host is the `cmd/ttsc` Go entrypoint compiled with `buildSourcePlugin`.
|
|
12
|
+
* The cache key incorporates the ttsc package version and the full `go.mod`
|
|
13
|
+
* contents so a toolchain upgrade automatically produces a fresh binary.
|
|
14
|
+
*
|
|
15
|
+
* @returns Absolute path to the compiled host executable.
|
|
16
|
+
*/
|
|
7
17
|
export function buildNativeCompiler(options: {
|
|
8
18
|
cacheBaseDir: string;
|
|
9
19
|
cacheDir?: string;
|
|
@@ -22,6 +32,10 @@ export function buildNativeCompiler(options: {
|
|
|
22
32
|
});
|
|
23
33
|
}
|
|
24
34
|
|
|
35
|
+
/**
|
|
36
|
+
* Read the `version` field from `package.json` inside `packageRoot`, falling
|
|
37
|
+
* back to `"0.0.0"` when the file is absent or malformed.
|
|
38
|
+
*/
|
|
25
39
|
function readOwnPackageVersion(packageRoot: string): string {
|
|
26
40
|
try {
|
|
27
41
|
const pkg = JSON.parse(
|
|
@@ -33,10 +47,16 @@ function readOwnPackageVersion(packageRoot: string): string {
|
|
|
33
47
|
}
|
|
34
48
|
}
|
|
35
49
|
|
|
50
|
+
/**
|
|
51
|
+
* Read the full contents of `go.mod` inside `packageRoot` and use it as a
|
|
52
|
+
* version token for the binary cache key. Returns `"unknown"` on read error.
|
|
53
|
+
*
|
|
54
|
+
* Using the entire `go.mod` ensures that any dependency bump (including
|
|
55
|
+
* indirect ones) invalidates the cached binary.
|
|
56
|
+
*/
|
|
36
57
|
function readGoModuleVersion(packageRoot: string): string {
|
|
37
58
|
try {
|
|
38
|
-
|
|
39
|
-
return goMod;
|
|
59
|
+
return fs.readFileSync(path.join(packageRoot, "go.mod"), "utf8");
|
|
40
60
|
} catch {
|
|
41
61
|
return "unknown";
|
|
42
62
|
}
|
|
@@ -9,12 +9,22 @@ import type { ITtscCompilerDiagnostic } from "../../structures/ITtscCompilerDiag
|
|
|
9
9
|
import type { ITtscParsedProjectConfig } from "../../structures/internal/ITtscParsedProjectConfig";
|
|
10
10
|
import type { TtscBuildResult } from "../../structures/internal/TtscBuildResult";
|
|
11
11
|
import { buildNativeCompiler } from "./buildNativeCompiler";
|
|
12
|
+
import { isOutsideRelativePath, packageRootDir } from "./paths";
|
|
12
13
|
import { readProjectConfig } from "./project/readProjectConfig";
|
|
13
14
|
import { runBuild } from "./runBuild";
|
|
14
15
|
|
|
15
16
|
/**
|
|
16
17
|
* Compile a project and capture emitted files without writing to the project
|
|
17
18
|
* tree.
|
|
19
|
+
*
|
|
20
|
+
* When no plugins are configured the fast path spawns the native ttsc compiler
|
|
21
|
+
* host (`cmd/ttsc api-compile`) which returns a structured JSON response
|
|
22
|
+
* containing diagnostics and an output file map. When plugins are present the
|
|
23
|
+
* slow path goes through `runBuild` into a temp directory and reads the files
|
|
24
|
+
* back from disk.
|
|
25
|
+
*
|
|
26
|
+
* @returns A map of output path → file content plus a `TtscBuildResult` with
|
|
27
|
+
* diagnostics and the exit status.
|
|
18
28
|
*/
|
|
19
29
|
export function compileProjectInMemory(options: ITtscCompilerContext): {
|
|
20
30
|
output: Record<string, string>;
|
|
@@ -64,6 +74,7 @@ export function compileProjectInMemory(options: ITtscCompilerContext): {
|
|
|
64
74
|
};
|
|
65
75
|
}
|
|
66
76
|
|
|
77
|
+
/** Return true when the project or the call-level options declare any plugins. */
|
|
67
78
|
function hasConfiguredPlugins(
|
|
68
79
|
options: ITtscCompilerContext,
|
|
69
80
|
project: ITtscParsedProjectConfig,
|
|
@@ -71,6 +82,10 @@ function hasConfiguredPlugins(
|
|
|
71
82
|
return hasProjectPluginEntries(project, options.plugins);
|
|
72
83
|
}
|
|
73
84
|
|
|
85
|
+
/**
|
|
86
|
+
* Plugin-backed compilation: emit into a temp directory via `runBuild`, then
|
|
87
|
+
* read back every file the build wrote so they can be returned as strings.
|
|
88
|
+
*/
|
|
74
89
|
function compileProjectWithPlugins(
|
|
75
90
|
options: ITtscCompilerContext,
|
|
76
91
|
cwd: string,
|
|
@@ -101,6 +116,15 @@ function compileProjectWithPlugins(
|
|
|
101
116
|
}
|
|
102
117
|
}
|
|
103
118
|
|
|
119
|
+
/**
|
|
120
|
+
* Build a function that maps a path relative to the temp output directory to
|
|
121
|
+
* the key used in the returned `output` map.
|
|
122
|
+
*
|
|
123
|
+
* When `outDir` is inside the project root the key is relative to the project
|
|
124
|
+
* root (preserving the `outDir` prefix). When `outDir` is outside the project
|
|
125
|
+
* root the key is absolute-style (`/absolute/outDir/relative`). When `outDir`
|
|
126
|
+
* is absent the key is the bare relative path.
|
|
127
|
+
*/
|
|
104
128
|
function outputKeyMapper(
|
|
105
129
|
project: ITtscParsedProjectConfig,
|
|
106
130
|
): (relativePath: string) => string {
|
|
@@ -116,14 +140,7 @@ function outputKeyMapper(
|
|
|
116
140
|
return (relativePath) => pathToKey(path.join(outDir, relativePath));
|
|
117
141
|
}
|
|
118
142
|
|
|
119
|
-
|
|
120
|
-
return (
|
|
121
|
-
relative === ".." ||
|
|
122
|
-
relative.startsWith(`..${path.sep}`) ||
|
|
123
|
-
path.isAbsolute(relative)
|
|
124
|
-
);
|
|
125
|
-
}
|
|
126
|
-
|
|
143
|
+
/** Read every file in `directory` recursively and return a `path→content` map. */
|
|
127
144
|
function readOutputDirectory(
|
|
128
145
|
directory: string,
|
|
129
146
|
keyOf: (relativePath: string) => string,
|
|
@@ -141,6 +158,7 @@ function readOutputDirectory(
|
|
|
141
158
|
return output;
|
|
142
159
|
}
|
|
143
160
|
|
|
161
|
+
/** Recursively list all files under `directory`, sorted for stable output. */
|
|
144
162
|
function listFiles(directory: string): string[] {
|
|
145
163
|
const out: string[] = [];
|
|
146
164
|
for (const entry of fs.readdirSync(directory, { withFileTypes: true })) {
|
|
@@ -154,10 +172,18 @@ function listFiles(directory: string): string[] {
|
|
|
154
172
|
return out.sort();
|
|
155
173
|
}
|
|
156
174
|
|
|
175
|
+
/** Normalise a file path to a forward-slash key suitable for the output map. */
|
|
157
176
|
function pathToKey(file: string): string {
|
|
158
177
|
return file.replace(/\\/g, "/");
|
|
159
178
|
}
|
|
160
179
|
|
|
180
|
+
/**
|
|
181
|
+
* Parse the JSON envelope written by the native compiler host to stdout.
|
|
182
|
+
*
|
|
183
|
+
* On success returns `{ diagnostics, output }`. On JSON parse failure throws a
|
|
184
|
+
* descriptive error using stderr (preferred) or stdout as context, so callers
|
|
185
|
+
* see the original compiler error rather than a generic JSON parse message.
|
|
186
|
+
*/
|
|
161
187
|
function parseNativeCompileOutput(
|
|
162
188
|
stdout: string,
|
|
163
189
|
stderr: string,
|
|
@@ -181,20 +207,3 @@ function parseNativeCompileOutput(
|
|
|
181
207
|
);
|
|
182
208
|
}
|
|
183
209
|
}
|
|
184
|
-
|
|
185
|
-
function packageRootDir(): string {
|
|
186
|
-
let current = path.resolve(__dirname);
|
|
187
|
-
while (true) {
|
|
188
|
-
if (
|
|
189
|
-
fs.existsSync(path.join(current, "package.json")) &&
|
|
190
|
-
fs.existsSync(path.join(current, "go.mod"))
|
|
191
|
-
) {
|
|
192
|
-
return fs.realpathSync.native?.(current) ?? fs.realpathSync(current);
|
|
193
|
-
}
|
|
194
|
-
const parent = path.dirname(current);
|
|
195
|
-
if (parent === current) {
|
|
196
|
-
throw new Error("ttsc: package root not found for native compiler build");
|
|
197
|
-
}
|
|
198
|
-
current = parent;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Walk up the directory tree from `__dirname` until a directory that contains
|
|
6
|
+
* both `package.json` and `go.mod` is found. This is the `packages/ttsc` root
|
|
7
|
+
* and must be passed to `buildNativeCompiler` so it can locate `cmd/ttsc`.
|
|
8
|
+
*
|
|
9
|
+
* Uses `fs.realpathSync.native` when available to resolve symlinks identically
|
|
10
|
+
* to the Go toolchain's own path resolution.
|
|
11
|
+
*
|
|
12
|
+
* Throws when the root cannot be found (i.e. the package is not in a Go
|
|
13
|
+
* workspace), which would indicate a broken installation.
|
|
14
|
+
*/
|
|
15
|
+
export function packageRootDir(): string {
|
|
16
|
+
let current = path.resolve(__dirname);
|
|
17
|
+
while (true) {
|
|
18
|
+
if (
|
|
19
|
+
fs.existsSync(path.join(current, "package.json")) &&
|
|
20
|
+
fs.existsSync(path.join(current, "go.mod"))
|
|
21
|
+
) {
|
|
22
|
+
return fs.realpathSync.native?.(current) ?? fs.realpathSync(current);
|
|
23
|
+
}
|
|
24
|
+
const parent = path.dirname(current);
|
|
25
|
+
if (parent === current) {
|
|
26
|
+
throw new Error("ttsc: package root not found for native compiler build");
|
|
27
|
+
}
|
|
28
|
+
current = parent;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Walk up the directory tree from `from` looking for a `go.mod` file. Stops
|
|
34
|
+
* after `maxDepth` hops so callers can bound the search to a known
|
|
35
|
+
* neighbourhood of the plugin source directory.
|
|
36
|
+
*
|
|
37
|
+
* Returns the absolute path to the first `go.mod` found, or `null` when no
|
|
38
|
+
* `go.mod` exists within the depth limit or filesystem root is reached first.
|
|
39
|
+
*/
|
|
40
|
+
export function findNearestGoMod(
|
|
41
|
+
from: string,
|
|
42
|
+
maxDepth: number,
|
|
43
|
+
): string | null {
|
|
44
|
+
let current = path.resolve(from);
|
|
45
|
+
let depth = 0;
|
|
46
|
+
while (true) {
|
|
47
|
+
const candidate = path.join(current, "go.mod");
|
|
48
|
+
if (fs.existsSync(candidate)) return candidate;
|
|
49
|
+
if (depth >= maxDepth) return null;
|
|
50
|
+
const parent = path.dirname(current);
|
|
51
|
+
if (parent === current) return null;
|
|
52
|
+
current = parent;
|
|
53
|
+
depth += 1;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Return `true` when a relative path escapes its base directory — i.e. starts
|
|
59
|
+
* with `..` or is an absolute path. Used by callers that need to guard against
|
|
60
|
+
* path traversal before building output keys or spawning subprocesses.
|
|
61
|
+
*/
|
|
62
|
+
export function isOutsideRelativePath(relative: string): boolean {
|
|
63
|
+
return (
|
|
64
|
+
relative === ".." ||
|
|
65
|
+
relative.startsWith(`..${path.sep}`) ||
|
|
66
|
+
path.isAbsolute(relative)
|
|
67
|
+
);
|
|
68
|
+
}
|