ttsc 0.12.2 → 0.12.4
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 +109 -101
- package/cmd/ttsc/api_compile_branches_test.go +55 -57
- package/cmd/ttsc/api_transform.go +54 -49
- package/cmd/ttsc/api_transform_branches_test.go +30 -32
- package/cmd/ttsc/build.go +111 -108
- package/cmd/ttsc/build_branches_test.go +95 -97
- 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 +362 -339
- package/driver/rewrite.go +442 -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 +24 -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 +19 -0
- package/lib/compiler/internal/runBuild.js +112 -46
- 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/launcher/internal/getCompilerVersionText.js +7 -0
- package/lib/launcher/internal/getCompilerVersionText.js.map +1 -1
- package/lib/launcher/internal/prepareExecution.js +32 -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 +101 -16
- 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 +33 -7
- 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 +32 -18
- package/lib/plugin/internal/loadProjectPlugins.js.map +1 -1
- package/lib/structures/ITtscCompilerTransformation.d.ts +15 -2
- package/lib/structures/internal/TtscSingleFileEmitOptions.d.ts +0 -2
- package/package.json +9 -9
- package/shim/ast/go.mod +9 -0
- package/shim/ast/go.sum +20 -0
- package/shim/ast/safe_text.go +86 -0
- package/shim/ast/shim.go +24 -0
- package/shim/ast/test/node_text_joins_multi_hop_qualified_name_test.go +32 -0
- package/shim/ast/test/node_text_joins_one_hop_qualified_name_test.go +31 -0
- package/shim/ast/test/node_text_returns_empty_for_nil_test.go +23 -0
- package/shim/ast/test/node_text_returns_identifier_text_test.go +27 -0
- package/shim/ast/test/node_text_skips_empty_qualified_left_test.go +30 -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 +24 -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 +110 -55
- 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/launcher/internal/getCompilerVersionText.ts +7 -0
- package/src/launcher/internal/prepareExecution.ts +32 -0
- package/src/launcher/internal/resolveCacheDir.ts +19 -0
- package/src/launcher/internal/resolveTtscserverBinary.ts +2 -0
- package/src/launcher/internal/runTtsc.ts +120 -20
- package/src/launcher/internal/runTtscserver.ts +9 -1
- package/src/launcher/internal/runTtsx.ts +33 -8
- package/src/plugin/internal/buildSourcePlugin.ts +40 -14
- package/src/plugin/internal/loadProjectPlugins.ts +42 -18
- package/src/structures/ITtscCompilerTransformation.ts +15 -2
- package/src/structures/ITtscPlugin.ts +0 -1
- package/src/structures/internal/TtscSingleFileEmitOptions.ts +0 -2
- package/tools/gen_shims/main.go +43 -8
- package/utility/host.go +294 -245
package/cmd/ttsc/main.go
CHANGED
|
@@ -12,104 +12,108 @@
|
|
|
12
12
|
package main
|
|
13
13
|
|
|
14
14
|
import (
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
"flag"
|
|
16
|
+
"fmt"
|
|
17
|
+
"io"
|
|
18
|
+
"os"
|
|
19
|
+
"runtime"
|
|
20
|
+
"strings"
|
|
21
21
|
)
|
|
22
22
|
|
|
23
23
|
// These are overridden via `-ldflags "-X main.version=... -X main.commit=..."`
|
|
24
24
|
// in CI. Sensible defaults keep local `go run ./cmd/ttsc` usable.
|
|
25
25
|
var (
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
version = "0.0.0-dev"
|
|
27
|
+
commit = "dev"
|
|
28
|
+
date = "unknown"
|
|
29
29
|
)
|
|
30
30
|
|
|
31
31
|
// stdout / stderr are package-level to keep the CLI testable.
|
|
32
32
|
var (
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
33
|
+
stdout io.Writer = os.Stdout
|
|
34
|
+
stderr io.Writer = os.Stderr
|
|
35
|
+
getwd = os.Getwd
|
|
36
36
|
)
|
|
37
37
|
|
|
38
38
|
func main() {
|
|
39
|
-
|
|
39
|
+
os.Exit(run(os.Args[1:]))
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
func run(args []string) int {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
43
|
+
if len(args) == 0 {
|
|
44
|
+
return runBuild(nil)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
switch args[0] {
|
|
48
|
+
case "-h", "--help", "help":
|
|
49
|
+
printHelp(stdout)
|
|
50
|
+
return 0
|
|
51
|
+
case "-v", "--version", "version":
|
|
52
|
+
printVersion(stdout)
|
|
53
|
+
return 0
|
|
54
|
+
case "demo":
|
|
55
|
+
return runDemo(args[1:])
|
|
56
|
+
case "build":
|
|
57
|
+
return runBuild(args[1:])
|
|
58
|
+
case "api-compile":
|
|
59
|
+
return runAPICompile(args[1:])
|
|
60
|
+
case "api-transform":
|
|
61
|
+
return runAPITransform(args[1:])
|
|
62
|
+
case "check":
|
|
63
|
+
// `ttsc check` runs the analyze pipeline without emitting JS — useful
|
|
64
|
+
// in CI and pre-commit checks that only need schema validation.
|
|
65
|
+
return runBuild(append([]string{"--noEmit"}, args[1:]...))
|
|
66
|
+
case "-p", "--project":
|
|
67
|
+
if len(args) < 2 {
|
|
68
|
+
fmt.Fprintln(stderr, "ttsc: -p/--project requires a path argument")
|
|
69
|
+
return 2
|
|
70
|
+
}
|
|
71
|
+
return runBuild(append([]string{"--tsconfig=" + args[1]}, args[2:]...))
|
|
72
|
+
default:
|
|
73
|
+
if isBuildAlias(args[0]) {
|
|
74
|
+
return runBuild(args)
|
|
75
|
+
}
|
|
76
|
+
fmt.Fprintf(stderr, "ttsc: unknown command %q\n", args[0])
|
|
77
|
+
fmt.Fprintln(stderr, `ttsc: run "ttsc --help" to see supported commands`)
|
|
78
|
+
return 2
|
|
79
|
+
}
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
+
// isBuildAlias reports whether arg should be forwarded to runBuild without
|
|
83
|
+
// requiring an explicit "build" verb. Any flag-shaped argument (leading "-")
|
|
84
|
+
// or a TypeScript-project file extension is treated as a build alias, matching
|
|
85
|
+
// the tsc/tsgo invocation convention.
|
|
82
86
|
func isBuildAlias(arg string) bool {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
87
|
+
if strings.HasPrefix(arg, "-") {
|
|
88
|
+
return true
|
|
89
|
+
}
|
|
90
|
+
switch {
|
|
91
|
+
case strings.HasSuffix(arg, ".json"),
|
|
92
|
+
strings.HasSuffix(arg, ".ts"),
|
|
93
|
+
strings.HasSuffix(arg, ".tsx"),
|
|
94
|
+
strings.HasSuffix(arg, ".mts"),
|
|
95
|
+
strings.HasSuffix(arg, ".cts"):
|
|
96
|
+
return true
|
|
97
|
+
default:
|
|
98
|
+
return false
|
|
99
|
+
}
|
|
96
100
|
}
|
|
97
101
|
|
|
98
102
|
func printVersion(w io.Writer) {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
103
|
+
fmt.Fprintf(
|
|
104
|
+
w,
|
|
105
|
+
"ttsc %s (commit %s, built %s, %s/%s, go %s)\n",
|
|
106
|
+
version,
|
|
107
|
+
commit,
|
|
108
|
+
date,
|
|
109
|
+
runtime.GOOS,
|
|
110
|
+
runtime.GOARCH,
|
|
111
|
+
runtime.Version(),
|
|
112
|
+
)
|
|
109
113
|
}
|
|
110
114
|
|
|
111
115
|
func printHelp(w io.Writer) {
|
|
112
|
-
|
|
116
|
+
fmt.Fprintln(w, strings.TrimSpace(`
|
|
113
117
|
ttsc — standalone typescript-go host.
|
|
114
118
|
|
|
115
119
|
Usage:
|
|
@@ -155,37 +159,40 @@ Integration guide (bundlers):
|
|
|
155
159
|
}
|
|
156
160
|
|
|
157
161
|
func runDemo(args []string) int {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
162
|
+
fs := flag.NewFlagSet("demo", flag.ContinueOnError)
|
|
163
|
+
fs.SetOutput(stderr)
|
|
164
|
+
typ := fs.String("type", "string", "atomic type to simulate")
|
|
165
|
+
if err := fs.Parse(args); err != nil {
|
|
166
|
+
return 2
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
arrow, err := demoArrow(*typ)
|
|
170
|
+
if err != nil {
|
|
171
|
+
fmt.Fprintf(stderr, "ttsc demo: %v\n", err)
|
|
172
|
+
return 2
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
fmt.Fprintf(stdout, "// demo<%s> -> emitted by ttsc %s\n", *typ, version)
|
|
176
|
+
fmt.Fprintln(stdout, arrow)
|
|
177
|
+
return 0
|
|
174
178
|
}
|
|
175
179
|
|
|
180
|
+
// demoArrow returns a JavaScript arrow-function expression that validates the
|
|
181
|
+
// named atomic TypeScript type at runtime. Used by the demo sub-command to
|
|
182
|
+
// produce a small synthetic emit that smoke tests can assert on.
|
|
176
183
|
func demoArrow(name string) (string, error) {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
184
|
+
switch strings.ToLower(name) {
|
|
185
|
+
case "any":
|
|
186
|
+
return "(input) => true", nil
|
|
187
|
+
case "boolean":
|
|
188
|
+
return `(input) => "boolean" === typeof input`, nil
|
|
189
|
+
case "number":
|
|
190
|
+
return `(input) => "number" === typeof input`, nil
|
|
191
|
+
case "bigint":
|
|
192
|
+
return `(input) => "bigint" === typeof input`, nil
|
|
193
|
+
case "string", "":
|
|
194
|
+
return `(input) => "string" === typeof input`, nil
|
|
195
|
+
default:
|
|
196
|
+
return "", fmt.Errorf("unknown --type value %q (want: string|number|boolean|bigint|any)", name)
|
|
197
|
+
}
|
|
191
198
|
}
|
|
@@ -2,28 +2,26 @@ package main
|
|
|
2
2
|
|
|
3
3
|
import "testing"
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
* 3. Assert both routes fail through their intended parser.
|
|
15
|
-
*/
|
|
5
|
+
// TestMainAliasBranches verifies main command aliases cover flag-shaped demo and cts project inputs.
|
|
6
|
+
//
|
|
7
|
+
// The top-level dispatcher treats extension-shaped arguments as build aliases
|
|
8
|
+
// while demo owns its own flag parser. These small branches keep compatibility
|
|
9
|
+
// with `ttsc ./tsconfig.cts`-style invocations and malformed demo flags.
|
|
10
|
+
//
|
|
11
|
+
// 1. Run demo with a missing flag value.
|
|
12
|
+
// 2. Dispatch a `.cts` argument through the build alias path.
|
|
13
|
+
// 3. Assert both routes fail through their intended parser.
|
|
16
14
|
func TestMainAliasBranches(t *testing.T) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
15
|
+
code, _, _ := captureCommand(t, func() int {
|
|
16
|
+
return run([]string{"demo", "--type"})
|
|
17
|
+
})
|
|
18
|
+
if code != 2 {
|
|
19
|
+
t.Fatalf("demo flag status mismatch: %d", code)
|
|
20
|
+
}
|
|
21
|
+
code, _, _ = captureCommand(t, func() int {
|
|
22
|
+
return run([]string{"project.cts"})
|
|
23
|
+
})
|
|
24
|
+
if code != 2 {
|
|
25
|
+
t.Fatalf("cts build alias status mismatch: %d", code)
|
|
26
|
+
}
|
|
29
27
|
}
|
package/cmd/ttscserver/main.go
CHANGED
|
@@ -12,34 +12,34 @@
|
|
|
12
12
|
package main
|
|
13
13
|
|
|
14
14
|
import (
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
15
|
+
"context"
|
|
16
|
+
"errors"
|
|
17
|
+
"flag"
|
|
18
|
+
"fmt"
|
|
19
|
+
"io"
|
|
20
|
+
"os"
|
|
21
|
+
"os/signal"
|
|
22
|
+
"runtime"
|
|
23
|
+
"strings"
|
|
24
|
+
"syscall"
|
|
25
|
+
"time"
|
|
26
|
+
|
|
27
|
+
"github.com/samchon/ttsc/packages/ttsc/internal/lspserver"
|
|
28
28
|
)
|
|
29
29
|
|
|
30
30
|
// Build metadata; overwritten via -ldflags in release builds.
|
|
31
31
|
var (
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
version = "0.0.0-dev"
|
|
33
|
+
commit = "dev"
|
|
34
|
+
date = "unknown"
|
|
35
35
|
)
|
|
36
36
|
|
|
37
37
|
// Package-level writers so command tests can capture output without
|
|
38
38
|
// patching os.Stdout / os.Stderr globally.
|
|
39
39
|
var (
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
40
|
+
stdout io.Writer = os.Stdout
|
|
41
|
+
stderr io.Writer = os.Stderr
|
|
42
|
+
stdin io.Reader = os.Stdin
|
|
43
43
|
)
|
|
44
44
|
|
|
45
45
|
// runLSPServer is the seam command tests use to substitute a fake LSP
|
|
@@ -56,89 +56,95 @@ var notifyContext = signal.NotifyContext
|
|
|
56
56
|
var getwd = os.Getwd
|
|
57
57
|
|
|
58
58
|
func main() {
|
|
59
|
-
|
|
59
|
+
os.Exit(run(os.Args[1:]))
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
+
// run dispatches top-level subcommands/flags and returns an exit code.
|
|
63
|
+
// Called by main with os.Args[1:] and overridden in tests with a synthetic
|
|
64
|
+
// argument slice to avoid spawning a real tsgo process.
|
|
62
65
|
func run(args []string) int {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
66
|
+
if len(args) == 0 {
|
|
67
|
+
printHelp(stdout)
|
|
68
|
+
return 0
|
|
69
|
+
}
|
|
70
|
+
switch args[0] {
|
|
71
|
+
case "-h", "--help", "help":
|
|
72
|
+
printHelp(stdout)
|
|
73
|
+
return 0
|
|
74
|
+
case "-v", "--version", "version":
|
|
75
|
+
printVersion(stdout)
|
|
76
|
+
return 0
|
|
77
|
+
}
|
|
78
|
+
return runLSP(args)
|
|
76
79
|
}
|
|
77
80
|
|
|
81
|
+
// runLSP parses LSP-mode flags and starts the proxy. It returns 0 on clean
|
|
82
|
+
// shutdown, 1 on a runtime error from the LSP host, and 2 on invalid
|
|
83
|
+
// invocation (missing --stdio, unresolvable cwd, unknown flags).
|
|
78
84
|
func runLSP(args []string) int {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
85
|
+
fs := flag.NewFlagSet("ttscserver", flag.ContinueOnError)
|
|
86
|
+
fs.SetOutput(stderr)
|
|
87
|
+
stdioFlag := fs.Bool("stdio", false, "communicate with the editor over stdin/stdout")
|
|
88
|
+
cwdFlag := fs.String("cwd", "", "project root (defaults to process cwd)")
|
|
89
|
+
tsgoFlag := fs.String("tsgo", "", "absolute tsgo binary path (defaults to TTSC_TSGO_BINARY)")
|
|
90
|
+
progressDelayFlag := fs.Duration("progress-delay", 250*time.Millisecond, "delay before showing tsgo's progress UI")
|
|
91
|
+
if err := fs.Parse(args); err != nil {
|
|
92
|
+
return 2
|
|
93
|
+
}
|
|
94
|
+
if !*stdioFlag {
|
|
95
|
+
fmt.Fprintln(stderr, "ttscserver: only --stdio transport is supported")
|
|
96
|
+
return 2
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
cwd := strings.TrimSpace(*cwdFlag)
|
|
100
|
+
if cwd == "" {
|
|
101
|
+
resolved, err := getwd()
|
|
102
|
+
if err != nil {
|
|
103
|
+
fmt.Fprintf(stderr, "ttscserver: could not resolve working directory: %v\n", err)
|
|
104
|
+
return 2
|
|
105
|
+
}
|
|
106
|
+
cwd = resolved
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
ctx, stop := notifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
|
110
|
+
defer stop()
|
|
111
|
+
|
|
112
|
+
tsgoBinary := strings.TrimSpace(*tsgoFlag)
|
|
113
|
+
if tsgoBinary == "" {
|
|
114
|
+
tsgoBinary = strings.TrimSpace(os.Getenv("TTSC_TSGO_BINARY"))
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
err := runLSPServer(ctx, lspserver.LSPServerOptions{
|
|
118
|
+
In: stdin,
|
|
119
|
+
Out: stdout,
|
|
120
|
+
Err: stderr,
|
|
121
|
+
Cwd: cwd,
|
|
122
|
+
TsgoBinary: tsgoBinary,
|
|
123
|
+
Source: lspserver.NullPluginSource{},
|
|
124
|
+
ProgressDelay: *progressDelayFlag,
|
|
125
|
+
})
|
|
126
|
+
if err != nil && !errors.Is(err, context.Canceled) {
|
|
127
|
+
fmt.Fprintf(stderr, "ttscserver: %v\n", err)
|
|
128
|
+
return 1
|
|
129
|
+
}
|
|
130
|
+
return 0
|
|
125
131
|
}
|
|
126
132
|
|
|
127
133
|
func printVersion(w io.Writer) {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
134
|
+
fmt.Fprintf(
|
|
135
|
+
w,
|
|
136
|
+
"ttscserver %s (commit %s, built %s, %s/%s, go %s)\n",
|
|
137
|
+
version,
|
|
138
|
+
commit,
|
|
139
|
+
date,
|
|
140
|
+
runtime.GOOS,
|
|
141
|
+
runtime.GOARCH,
|
|
142
|
+
runtime.Version(),
|
|
143
|
+
)
|
|
138
144
|
}
|
|
139
145
|
|
|
140
146
|
func printHelp(w io.Writer) {
|
|
141
|
-
|
|
147
|
+
fmt.Fprintln(w, strings.TrimSpace(`
|
|
142
148
|
ttscserver — Language Server Protocol host for ttsc.
|
|
143
149
|
|
|
144
150
|
Usage:
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
package main
|
|
2
2
|
|
|
3
3
|
import (
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
"bytes"
|
|
5
|
+
"context"
|
|
6
|
+
"testing"
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
"github.com/samchon/ttsc/packages/ttsc/internal/lspserver"
|
|
9
9
|
)
|
|
10
10
|
|
|
11
11
|
// TestRunLSPPrefersTsgoFlag verifies the native command forwards an explicit
|
|
@@ -19,24 +19,24 @@ import (
|
|
|
19
19
|
// 2. Run runLSP with --stdio, --cwd, and --tsgo.
|
|
20
20
|
// 3. Assert the captured TsgoBinary is the flag value.
|
|
21
21
|
func TestRunLSPPrefersTsgoFlag(t *testing.T) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
22
|
+
const expected = "/tmp/tsgo-test-binary"
|
|
23
|
+
prev := runLSPServer
|
|
24
|
+
var captured lspserver.LSPServerOptions
|
|
25
|
+
runLSPServer = func(_ context.Context, opts lspserver.LSPServerOptions) error {
|
|
26
|
+
captured = opts
|
|
27
|
+
return nil
|
|
28
|
+
}
|
|
29
|
+
defer func() { runLSPServer = prev }()
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
31
|
+
outBuf := &bytes.Buffer{}
|
|
32
|
+
errBuf := &bytes.Buffer{}
|
|
33
|
+
withIO(t, outBuf, errBuf, nil, func() {
|
|
34
|
+
if code := runLSP([]string{"--stdio", "--cwd", t.TempDir(), "--tsgo", expected}); code != 0 {
|
|
35
|
+
t.Fatalf("expected exit 0, got %d (stderr=%q)", code, errBuf.String())
|
|
36
|
+
}
|
|
37
|
+
})
|
|
38
38
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
39
|
+
if captured.TsgoBinary != expected {
|
|
40
|
+
t.Fatalf("expected TsgoBinary %q, got %q", expected, captured.TsgoBinary)
|
|
41
|
+
}
|
|
42
42
|
}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
package main
|
|
2
2
|
|
|
3
3
|
import (
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
"bytes"
|
|
5
|
+
"context"
|
|
6
|
+
"errors"
|
|
7
|
+
"strings"
|
|
8
|
+
"testing"
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
"github.com/samchon/ttsc/packages/ttsc/internal/lspserver"
|
|
11
11
|
)
|
|
12
12
|
|
|
13
13
|
// TestRunLSPReportsServerError pins the non-cancellation error path in
|
|
@@ -19,22 +19,22 @@ import (
|
|
|
19
19
|
// 2. Run runLSP with --stdio --cwd <tempdir>.
|
|
20
20
|
// 3. Assert exit 1 and the sentinel text on stderr.
|
|
21
21
|
func TestRunLSPReportsServerError(t *testing.T) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
22
|
+
sentinel := errors.New("lsp host blew up")
|
|
23
|
+
prev := runLSPServer
|
|
24
|
+
runLSPServer = func(_ context.Context, _ lspserver.LSPServerOptions) error {
|
|
25
|
+
return sentinel
|
|
26
|
+
}
|
|
27
|
+
defer func() { runLSPServer = prev }()
|
|
28
28
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
29
|
+
outBuf := &bytes.Buffer{}
|
|
30
|
+
errBuf := &bytes.Buffer{}
|
|
31
|
+
withIO(t, outBuf, errBuf, nil, func() {
|
|
32
|
+
if code := runLSP([]string{"--stdio", "--cwd", t.TempDir()}); code != 1 {
|
|
33
|
+
t.Fatalf("expected exit 1, got %d", code)
|
|
34
|
+
}
|
|
35
|
+
})
|
|
36
36
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
37
|
+
if !strings.Contains(errBuf.String(), sentinel.Error()) {
|
|
38
|
+
t.Fatalf("expected sentinel on stderr, got: %q", errBuf.String())
|
|
39
|
+
}
|
|
40
40
|
}
|