ttsc 0.7.2 → 0.8.0-dev.20260505
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/README.md +5 -5
- package/cmd/platform/main.go +86 -76
- package/cmd/ttsc/api_compile.go +122 -105
- package/cmd/ttsc/api_transform.go +57 -49
- package/cmd/ttsc/build.go +116 -103
- package/cmd/ttsc/main.go +117 -117
- package/cmd/ttsc/main_test.go +191 -191
- package/driver/host.go +9 -9
- package/driver/program.go +285 -285
- package/driver/rewrite.go +266 -219
- package/lib/compiler/internal/compileProjectInMemory.js +3 -3
- package/lib/compiler/internal/compileProjectInMemory.js.map +1 -1
- package/lib/compiler/internal/resolveEmittedJavaScript.js +2 -1
- package/lib/compiler/internal/resolveEmittedJavaScript.js.map +1 -1
- package/lib/compiler/internal/resolveTsgo.js.map +1 -1
- package/lib/compiler/internal/runBuild.js +47 -49
- package/lib/compiler/internal/runBuild.js.map +1 -1
- package/lib/compiler/internal/transformProjectInMemory.js +44 -4
- package/lib/compiler/internal/transformProjectInMemory.js.map +1 -1
- package/lib/launcher/internal/getCompilerVersionText.js +1 -2
- package/lib/launcher/internal/getCompilerVersionText.js.map +1 -1
- package/lib/launcher/internal/prepareExecution.js +6 -4
- package/lib/launcher/internal/prepareExecution.js.map +1 -1
- package/lib/launcher/internal/runTtsc.js +49 -5
- package/lib/launcher/internal/runTtsc.js.map +1 -1
- package/lib/launcher/internal/runTtsx.js +7 -2
- package/lib/launcher/internal/runTtsx.js.map +1 -1
- package/lib/plugin/internal/buildSourcePlugin.js +6 -0
- package/lib/plugin/internal/buildSourcePlugin.js.map +1 -1
- package/lib/plugin/internal/loadProjectPlugins.js +55 -2
- package/lib/plugin/internal/loadProjectPlugins.js.map +1 -1
- package/lib/structures/ITtscPlugin.d.ts +19 -3
- package/lib/structures/ITtscPlugin.js +4 -0
- package/lib/structures/ITtscPlugin.js.map +1 -1
- package/lib/structures/TtscPluginStage.d.ts +4 -10
- package/lib/structures/internal/ITtscLoadedNativePlugin.d.ts +5 -0
- package/package.json +11 -10
- package/shim/ast/lint.go +172 -171
- package/shim/ast/shim.go +84 -83
- package/shim/bundled/shim.go +5 -1
- package/shim/checker/shim.go +79 -79
- package/shim/compiler/emit_hooks.go +494 -0
- package/shim/compiler/shim.go +16 -1
- package/shim/core/shim.go +13 -10
- package/shim/diagnosticwriter/lint.go +79 -79
- package/shim/diagnosticwriter/shim.go +19 -19
- package/shim/parser/shim.go +7 -1
- package/shim/scanner/shim.go +39 -1
- package/shim/tsoptions/shim.go +51 -1
- package/shim/tspath/shim.go +72 -1
- package/shim/vfs/cachedvfs/shim.go +1 -1
- package/shim/vfs/osvfs/shim.go +1 -1
- package/shim/vfs/shim.go +4 -1
- package/src/compiler/internal/compileProjectInMemory.ts +6 -3
- package/src/compiler/internal/resolveEmittedJavaScript.ts +2 -1
- package/src/compiler/internal/resolveTsgo.ts +0 -1
- package/src/compiler/internal/runBuild.ts +58 -86
- package/src/compiler/internal/transformProjectInMemory.ts +51 -6
- package/src/launcher/internal/getCompilerVersionText.ts +1 -2
- package/src/launcher/internal/prepareExecution.ts +6 -6
- package/src/launcher/internal/runTtsc.ts +52 -5
- package/src/launcher/internal/runTtsx.ts +16 -5
- package/src/plugin/internal/buildSourcePlugin.ts +30 -2
- package/src/plugin/internal/loadProjectPlugins.ts +81 -2
- package/src/structures/ITtscPlugin.ts +30 -3
- package/src/structures/TtscPluginStage.ts +4 -10
- package/src/structures/internal/ITtscLoadedNativePlugin.ts +5 -0
- package/test/compiler.test.ts +3 -3
- package/test/platform.test.ts +1 -3
- package/test/tsgo.test.ts +15 -7
- package/tools/gen_shims/main.go +409 -409
- package/utility/host.go +707 -0
package/README.md
CHANGED
|
@@ -16,8 +16,7 @@ A `typescript-go` toolchain for compiler-powered transforms and type-safe execut
|
|
|
16
16
|
- 10x faster than `ts-node`.
|
|
17
17
|
- type checking that `tsx` does not provide.
|
|
18
18
|
- **transformer support**: compiler-powered libraries, such as `typia`.
|
|
19
|
-
|
|
20
|
-
> `ttsx` (CLI command) can run existing `typescript@6` projects.
|
|
19
|
+
- `@ttsc/lint`: lint violations as TS compile errors.
|
|
21
20
|
|
|
22
21
|
## Setup
|
|
23
22
|
|
|
@@ -41,9 +40,10 @@ npx ttsc --noEmit
|
|
|
41
40
|
npx ttsc --watch
|
|
42
41
|
```
|
|
43
42
|
|
|
44
|
-
|
|
43
|
+
Prebuild or clear cached source-plugin binaries:
|
|
45
44
|
|
|
46
45
|
```bash
|
|
46
|
+
npx ttsc prepare
|
|
47
47
|
npx ttsc clean
|
|
48
48
|
```
|
|
49
49
|
|
|
@@ -141,9 +141,9 @@ It is transformed into dedicated JavaScript:
|
|
|
141
141
|
|
|
142
142
|
`ttsc` ships a few small utility plugins in this repository.
|
|
143
143
|
|
|
144
|
-
- [`@ttsc/banner`](https://github.com/samchon/ttsc/tree/master/packages/banner): adds
|
|
144
|
+
- [`@ttsc/banner`](https://github.com/samchon/ttsc/tree/master/packages/banner): adds `@packageDocumentation` JSDoc banners to JS and declaration emit.
|
|
145
145
|
- [`@ttsc/lint`](https://github.com/samchon/ttsc/tree/master/packages/lint): type-check + lint in one compile pass, ~20x faster than `eslint` in theory.
|
|
146
|
-
- [`@ttsc/paths`](https://github.com/samchon/ttsc/tree/master/packages/paths): rewrites path aliases in
|
|
146
|
+
- [`@ttsc/paths`](https://github.com/samchon/ttsc/tree/master/packages/paths): rewrites path aliases in JS and declaration imports.
|
|
147
147
|
- [`@ttsc/strip`](https://github.com/samchon/ttsc/tree/master/packages/strip): removes configured calls and `debugger` statements.
|
|
148
148
|
|
|
149
149
|
Plugin authors should start from the [`Guide Documents`](https://github.com/samchon/ttsc/tree/master/docs).
|
package/cmd/platform/main.go
CHANGED
|
@@ -1,73 +1,83 @@
|
|
|
1
|
+
// Platform helper binary shipped by the per-OS `ttsc-*` packages.
|
|
2
|
+
//
|
|
3
|
+
// The real compiler and runner commands live in the JavaScript launchers so
|
|
4
|
+
// they can resolve the consuming project's `@typescript/native-preview` and
|
|
5
|
+
// plugin descriptors. This binary only supplies version/platform metadata and
|
|
6
|
+
// a tiny demo command for package smoke tests.
|
|
1
7
|
package main
|
|
2
8
|
|
|
3
9
|
import (
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
+
"flag"
|
|
11
|
+
"fmt"
|
|
12
|
+
"io"
|
|
13
|
+
"os"
|
|
14
|
+
"runtime"
|
|
15
|
+
"strings"
|
|
10
16
|
)
|
|
11
17
|
|
|
12
18
|
var (
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
19
|
+
// These values are overwritten by release builds through -ldflags. The
|
|
20
|
+
// defaults keep local `go run` and fixture builds deterministic.
|
|
21
|
+
version = "0.0.0-dev"
|
|
22
|
+
commit = "dev"
|
|
23
|
+
date = "unknown"
|
|
16
24
|
)
|
|
17
25
|
|
|
18
26
|
var (
|
|
19
|
-
|
|
20
|
-
|
|
27
|
+
// Package-level writers keep CLI tests simple and avoid global os.Stdout /
|
|
28
|
+
// os.Stderr patching.
|
|
29
|
+
stdout io.Writer = os.Stdout
|
|
30
|
+
stderr io.Writer = os.Stderr
|
|
21
31
|
)
|
|
22
32
|
|
|
23
33
|
func main() {
|
|
24
|
-
|
|
34
|
+
os.Exit(run(os.Args[1:]))
|
|
25
35
|
}
|
|
26
36
|
|
|
27
37
|
func run(args []string) int {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
38
|
+
if len(args) == 0 {
|
|
39
|
+
printHelp(stdout)
|
|
40
|
+
return 0
|
|
41
|
+
}
|
|
32
42
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
43
|
+
switch args[0] {
|
|
44
|
+
case "-h", "--help", "help":
|
|
45
|
+
printHelp(stdout)
|
|
46
|
+
return 0
|
|
47
|
+
case "-v", "--version", "version":
|
|
48
|
+
printVersion(stdout)
|
|
49
|
+
return 0
|
|
50
|
+
case "demo":
|
|
51
|
+
return runDemo(args[1:])
|
|
52
|
+
case "build", "check":
|
|
53
|
+
fmt.Fprintf(
|
|
54
|
+
stderr,
|
|
55
|
+
"ttsc platform helper: %s is provided by the JavaScript ttsc CLI using the consuming project's @typescript/native-preview, or by a plugin-selected native sidecar.\n",
|
|
56
|
+
args[0],
|
|
57
|
+
)
|
|
58
|
+
return 2
|
|
59
|
+
default:
|
|
60
|
+
fmt.Fprintf(stderr, "ttsc platform helper: unknown command %q\n", args[0])
|
|
61
|
+
fmt.Fprintln(stderr, `ttsc platform helper: run "ttsc --help" through the JavaScript CLI for compiler usage.`)
|
|
62
|
+
return 2
|
|
63
|
+
}
|
|
54
64
|
}
|
|
55
65
|
|
|
56
66
|
func printVersion(w io.Writer) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
+
fmt.Fprintf(
|
|
68
|
+
w,
|
|
69
|
+
"ttsc platform helper %s (commit %s, built %s, %s/%s, go %s)\n",
|
|
70
|
+
version,
|
|
71
|
+
commit,
|
|
72
|
+
date,
|
|
73
|
+
runtime.GOOS,
|
|
74
|
+
runtime.GOARCH,
|
|
75
|
+
runtime.Version(),
|
|
76
|
+
)
|
|
67
77
|
}
|
|
68
78
|
|
|
69
79
|
func printHelp(w io.Writer) {
|
|
70
|
-
|
|
80
|
+
fmt.Fprintln(w, strings.TrimSpace(`
|
|
71
81
|
ttsc platform helper.
|
|
72
82
|
|
|
73
83
|
This binary is a small compatibility helper shipped by @ttsc platform packages.
|
|
@@ -82,37 +92,37 @@ Usage:
|
|
|
82
92
|
}
|
|
83
93
|
|
|
84
94
|
func runDemo(args []string) int {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
95
|
+
fs := flag.NewFlagSet("demo", flag.ContinueOnError)
|
|
96
|
+
fs.SetOutput(stderr)
|
|
97
|
+
typ := fs.String("type", "string", "atomic type to simulate")
|
|
98
|
+
if err := fs.Parse(args); err != nil {
|
|
99
|
+
return 2
|
|
100
|
+
}
|
|
91
101
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
102
|
+
arrow, err := demoArrow(*typ)
|
|
103
|
+
if err != nil {
|
|
104
|
+
fmt.Fprintf(stderr, "ttsc platform helper demo: %v\n", err)
|
|
105
|
+
return 2
|
|
106
|
+
}
|
|
97
107
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
108
|
+
fmt.Fprintf(stdout, "// demo<%s> -> emitted by ttsc platform helper %s\n", *typ, version)
|
|
109
|
+
fmt.Fprintln(stdout, arrow)
|
|
110
|
+
return 0
|
|
101
111
|
}
|
|
102
112
|
|
|
103
113
|
func demoArrow(name string) (string, error) {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
114
|
+
switch strings.ToLower(name) {
|
|
115
|
+
case "any":
|
|
116
|
+
return "(input) => true", nil
|
|
117
|
+
case "boolean":
|
|
118
|
+
return `(input) => "boolean" === typeof input`, nil
|
|
119
|
+
case "number":
|
|
120
|
+
return `(input) => "number" === typeof input`, nil
|
|
121
|
+
case "bigint":
|
|
122
|
+
return `(input) => "bigint" === typeof input`, nil
|
|
123
|
+
case "string", "":
|
|
124
|
+
return `(input) => "string" === typeof input`, nil
|
|
125
|
+
default:
|
|
126
|
+
return "", fmt.Errorf("unknown --type value %q (want: string|number|boolean|bigint|any)", name)
|
|
127
|
+
}
|
|
118
128
|
}
|
package/cmd/ttsc/api_compile.go
CHANGED
|
@@ -1,134 +1,151 @@
|
|
|
1
|
+
// JSON API adapter for in-process-style project compilation.
|
|
2
|
+
//
|
|
3
|
+
// `api-compile` is consumed by the TypeScript wrapper when it needs a stable,
|
|
4
|
+
// machine-readable result from the native compiler host. The command always
|
|
5
|
+
// emits into memory and serializes diagnostics plus output text; it never
|
|
6
|
+
// writes generated files into the caller's project tree.
|
|
1
7
|
package main
|
|
2
8
|
|
|
3
9
|
import (
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
+
"encoding/json"
|
|
11
|
+
"flag"
|
|
12
|
+
"fmt"
|
|
13
|
+
"os"
|
|
14
|
+
"path/filepath"
|
|
15
|
+
"strings"
|
|
10
16
|
|
|
11
|
-
|
|
17
|
+
shimcompiler "github.com/microsoft/typescript-go/shim/compiler"
|
|
12
18
|
|
|
13
|
-
|
|
19
|
+
"github.com/samchon/ttsc/packages/ttsc/driver"
|
|
14
20
|
)
|
|
15
21
|
|
|
16
22
|
type apiCompileResult struct {
|
|
17
|
-
|
|
18
|
-
|
|
23
|
+
// Diagnostics is omitted only when the compiler produced no diagnostics.
|
|
24
|
+
// Output is still returned in diagnostic cases because TypeScript-Go may
|
|
25
|
+
// produce partial emit text before a build-failing error is reported.
|
|
26
|
+
Diagnostics []apiCompileDiagnostic `json:"diagnostics,omitempty"`
|
|
27
|
+
Output map[string]string `json:"output"`
|
|
19
28
|
}
|
|
20
29
|
|
|
30
|
+
// apiCompileDiagnostic mirrors the public TypeScript-side diagnostic DTO.
|
|
31
|
+
// The JSON keys intentionally use TypeScript naming (`messageText`,
|
|
32
|
+
// `character`) instead of Go naming so callers can pass the data through
|
|
33
|
+
// without remapping.
|
|
21
34
|
type apiCompileDiagnostic struct {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
35
|
+
File *string `json:"file"`
|
|
36
|
+
Category string `json:"category"`
|
|
37
|
+
Code int32 `json:"code"`
|
|
38
|
+
Start *int `json:"start,omitempty"`
|
|
39
|
+
Length *int `json:"length,omitempty"`
|
|
40
|
+
Line int `json:"line,omitempty"`
|
|
41
|
+
Character int `json:"character,omitempty"`
|
|
42
|
+
MessageText string `json:"messageText"`
|
|
30
43
|
}
|
|
31
44
|
|
|
32
45
|
func runAPICompile(args []string) int {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
46
|
+
fs := flag.NewFlagSet("api-compile", flag.ContinueOnError)
|
|
47
|
+
fs.SetOutput(stderr)
|
|
48
|
+
tsconfigPath := fs.String("tsconfig", "tsconfig.json", "path to tsconfig.json")
|
|
49
|
+
cwdOverride := fs.String("cwd", "", "override the working directory")
|
|
50
|
+
if err := fs.Parse(args); err != nil {
|
|
51
|
+
return 2
|
|
52
|
+
}
|
|
40
53
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
54
|
+
cwd, ok := resolveBuildCwd(*cwdOverride)
|
|
55
|
+
if !ok {
|
|
56
|
+
return 2
|
|
57
|
+
}
|
|
45
58
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
59
|
+
prog, diags, err := driver.LoadProgram(cwd, *tsconfigPath, driver.LoadProgramOptions{
|
|
60
|
+
ForceEmit: true,
|
|
61
|
+
})
|
|
62
|
+
if err != nil {
|
|
63
|
+
fmt.Fprintf(stderr, "ttsc api-compile: %v\n", err)
|
|
64
|
+
return 2
|
|
65
|
+
}
|
|
66
|
+
if prog != nil {
|
|
67
|
+
defer prog.Close()
|
|
68
|
+
diags = append(diags, prog.Diagnostics()...)
|
|
69
|
+
}
|
|
57
70
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
71
|
+
output := map[string]string{}
|
|
72
|
+
if prog != nil {
|
|
73
|
+
rewrites := driver.NewRewriteSet()
|
|
74
|
+
// Capture WriteFile output in a map keyed by project-relative paths. This
|
|
75
|
+
// gives the JS API a deterministic object and avoids touching outDir.
|
|
76
|
+
writeFile := shimcompiler.WriteFile(
|
|
77
|
+
func(fileName, text string, _ *shimcompiler.WriteFileData) error {
|
|
78
|
+
output[apiOutputKey(cwd, fileName)] = text
|
|
79
|
+
return nil
|
|
80
|
+
},
|
|
81
|
+
)
|
|
82
|
+
_, emitDiags, err := prog.EmitAll(rewrites, writeFile)
|
|
83
|
+
if err != nil {
|
|
84
|
+
fmt.Fprintf(stderr, "ttsc api-compile: emit failed: %v\n", err)
|
|
85
|
+
return 3
|
|
86
|
+
}
|
|
87
|
+
diags = append(diags, emitDiags...)
|
|
88
|
+
}
|
|
74
89
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
90
|
+
result := apiCompileResult{
|
|
91
|
+
Diagnostics: make([]apiCompileDiagnostic, 0, len(diags)),
|
|
92
|
+
Output: output,
|
|
93
|
+
}
|
|
94
|
+
for _, diag := range diags {
|
|
95
|
+
result.Diagnostics = append(result.Diagnostics, toAPICompileDiagnostic(diag))
|
|
96
|
+
}
|
|
82
97
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
98
|
+
data, err := json.Marshal(result)
|
|
99
|
+
if err != nil {
|
|
100
|
+
fmt.Fprintf(stderr, "ttsc api-compile: result marshal failed: %v\n", err)
|
|
101
|
+
return 3
|
|
102
|
+
}
|
|
103
|
+
fmt.Fprintln(stdout, string(data))
|
|
104
|
+
if driver.CountErrors(diags) > 0 {
|
|
105
|
+
return 2
|
|
106
|
+
}
|
|
107
|
+
return 0
|
|
93
108
|
}
|
|
94
109
|
|
|
95
110
|
func resolveBuildCwd(cwdOverride string) (string, bool) {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
111
|
+
if cwdOverride != "" {
|
|
112
|
+
return cwdOverride, true
|
|
113
|
+
}
|
|
114
|
+
cwd, err := os.Getwd()
|
|
115
|
+
if err != nil {
|
|
116
|
+
fmt.Fprintf(stderr, "ttsc: could not get working directory: %v\n", err)
|
|
117
|
+
return "", false
|
|
118
|
+
}
|
|
119
|
+
return cwd, true
|
|
105
120
|
}
|
|
106
121
|
|
|
107
122
|
func toAPICompileDiagnostic(diag driver.Diagnostic) apiCompileDiagnostic {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
123
|
+
var file *string
|
|
124
|
+
if diag.File != "" {
|
|
125
|
+
value := diag.File
|
|
126
|
+
file = &value
|
|
127
|
+
}
|
|
128
|
+
category := "error"
|
|
129
|
+
if diag.Severity == driver.SeverityWarning {
|
|
130
|
+
category = "warning"
|
|
131
|
+
}
|
|
132
|
+
return apiCompileDiagnostic{
|
|
133
|
+
File: file,
|
|
134
|
+
Category: category,
|
|
135
|
+
Code: diag.Code,
|
|
136
|
+
Start: diag.Start,
|
|
137
|
+
Length: diag.Length,
|
|
138
|
+
Line: diag.Line,
|
|
139
|
+
Character: diag.Column,
|
|
140
|
+
MessageText: diag.Message,
|
|
141
|
+
}
|
|
127
142
|
}
|
|
128
143
|
|
|
129
144
|
func apiOutputKey(cwd, fileName string) string {
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
145
|
+
// Relative keys are the API contract for files inside the project. Absolute
|
|
146
|
+
// keys are preserved for uncommon compiler outputs outside cwd.
|
|
147
|
+
if rel, err := filepath.Rel(cwd, fileName); err == nil && !strings.HasPrefix(rel, "..") {
|
|
148
|
+
return filepath.ToSlash(rel)
|
|
149
|
+
}
|
|
150
|
+
return filepath.ToSlash(fileName)
|
|
134
151
|
}
|
|
@@ -1,64 +1,72 @@
|
|
|
1
|
+
// JSON API adapter for source-level transformation results.
|
|
2
|
+
//
|
|
3
|
+
// `api-transform` returns the TypeScript source files that TypeScript-Go
|
|
4
|
+
// parsed for the owning project. It is the no-emit companion to api-compile:
|
|
5
|
+
// diagnostics are still collected, but the result surface is TypeScript text
|
|
6
|
+
// rather than emitted JavaScript.
|
|
1
7
|
package main
|
|
2
8
|
|
|
3
9
|
import (
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
10
|
+
"encoding/json"
|
|
11
|
+
"flag"
|
|
12
|
+
"fmt"
|
|
7
13
|
|
|
8
|
-
|
|
14
|
+
"github.com/samchon/ttsc/packages/ttsc/driver"
|
|
9
15
|
)
|
|
10
16
|
|
|
11
17
|
type apiTransformResult struct {
|
|
12
|
-
|
|
13
|
-
|
|
18
|
+
// TypeScript contains every non-library source file visible through the
|
|
19
|
+
// Program facade, keyed the same way api-compile keys emitted files.
|
|
20
|
+
Diagnostics []apiCompileDiagnostic `json:"diagnostics,omitempty"`
|
|
21
|
+
TypeScript map[string]string `json:"typescript"`
|
|
14
22
|
}
|
|
15
23
|
|
|
16
24
|
func runAPITransform(args []string) int {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
25
|
+
fs := flag.NewFlagSet("api-transform", flag.ContinueOnError)
|
|
26
|
+
fs.SetOutput(stderr)
|
|
27
|
+
tsconfigPath := fs.String("tsconfig", "tsconfig.json", "path to tsconfig.json")
|
|
28
|
+
cwdOverride := fs.String("cwd", "", "override the working directory")
|
|
29
|
+
if err := fs.Parse(args); err != nil {
|
|
30
|
+
return 2
|
|
31
|
+
}
|
|
24
32
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
33
|
+
cwd, ok := resolveBuildCwd(*cwdOverride)
|
|
34
|
+
if !ok {
|
|
35
|
+
return 2
|
|
36
|
+
}
|
|
29
37
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
38
|
+
prog, diags, err := driver.LoadProgram(cwd, *tsconfigPath, driver.LoadProgramOptions{
|
|
39
|
+
ForceNoEmit: true,
|
|
40
|
+
})
|
|
41
|
+
if err != nil {
|
|
42
|
+
fmt.Fprintf(stderr, "ttsc api-transform: %v\n", err)
|
|
43
|
+
return 2
|
|
44
|
+
}
|
|
45
|
+
typescript := map[string]string{}
|
|
46
|
+
if prog != nil {
|
|
47
|
+
defer prog.Close()
|
|
48
|
+
for _, file := range prog.SourceFiles() {
|
|
49
|
+
typescript[apiOutputKey(cwd, file.FileName())] = file.Text()
|
|
50
|
+
}
|
|
51
|
+
diags = append(diags, prog.Diagnostics()...)
|
|
52
|
+
}
|
|
45
53
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
54
|
+
result := apiTransformResult{
|
|
55
|
+
Diagnostics: make([]apiCompileDiagnostic, 0, len(diags)),
|
|
56
|
+
TypeScript: typescript,
|
|
57
|
+
}
|
|
58
|
+
for _, diag := range diags {
|
|
59
|
+
result.Diagnostics = append(result.Diagnostics, toAPICompileDiagnostic(diag))
|
|
60
|
+
}
|
|
53
61
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
62
|
+
data, err := json.Marshal(result)
|
|
63
|
+
if err != nil {
|
|
64
|
+
fmt.Fprintf(stderr, "ttsc api-transform: result marshal failed: %v\n", err)
|
|
65
|
+
return 3
|
|
66
|
+
}
|
|
67
|
+
fmt.Fprintln(stdout, string(data))
|
|
68
|
+
if driver.CountErrors(diags) > 0 {
|
|
69
|
+
return 2
|
|
70
|
+
}
|
|
71
|
+
return 0
|
|
64
72
|
}
|