typia 15.4.0 → 15.5.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/README.md +7 -2
- package/lib/transform.d.ts +3 -1
- package/lib/transform.js +4 -1
- package/lib/transform.js.map +1 -1
- package/lib/transform.mjs +5 -2
- package/lib/transform.mjs.map +1 -1
- package/native/cmd/ttsc-typia/build.go +185 -125
- package/native/cmd/ttsc-typia/build_no_emit_preserves_analysis_options_test.go +50 -43
- package/native/cmd/ttsc-typia/check.go +92 -0
- package/native/cmd/ttsc-typia/check_serve.go +318 -0
- package/native/cmd/ttsc-typia/check_serve_matches_cold_check_test.go +234 -0
- package/native/cmd/ttsc-typia/check_serve_reloads_with_program_plugin_test.go +56 -0
- package/native/cmd/ttsc-typia/check_transform_leaves_program_untouched_test.go +155 -0
- package/native/cmd/ttsc-typia/main.go +3 -0
- package/native/cmd/ttsc-typia/project_transform_publishes_untouched_source_verbatim_test.go +144 -0
- package/native/cmd/ttsc-typia/transform.go +59 -16
- package/native/cmd/ttsc-typia/transform_prints_files_rewritten_outside_typia_test.go +168 -0
- package/native/cmd/ttsc-typia/typia_check_loads_one_program_test.go +125 -0
- package/native/go.mod +2 -1
- package/package.json +5 -5
- package/src/transform.ts +4 -1
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
package main
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"encoding/json"
|
|
5
|
+
"path/filepath"
|
|
6
|
+
"testing"
|
|
7
|
+
|
|
8
|
+
"github.com/samchon/ttsc/packages/ttsc/driver"
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
// TestCheckServeReloadsWithProgramPlugin verifies typia's resident check loads
|
|
12
|
+
// a fresh Program for every cycle while a linked ProgramPlugin is selected.
|
|
13
|
+
//
|
|
14
|
+
// typia's analysis reads the ASTs the linked program hooks produce, and such a
|
|
15
|
+
// hook rewrites them in place, so a kept Program would show the next cycle
|
|
16
|
+
// source an earlier cycle already rewrote.
|
|
17
|
+
//
|
|
18
|
+
// 1. Select buildObservationPlugin, a ProgramPlugin without CheckProgram, as
|
|
19
|
+
// linked manifest entry 0.
|
|
20
|
+
// 2. Answer a passing cycle, then a transform failure after an edit of a
|
|
21
|
+
// known source.
|
|
22
|
+
// 3. Require a new load and no update for each cycle, each answer equal to a
|
|
23
|
+
// cold check.
|
|
24
|
+
//
|
|
25
|
+
// @evidence contracts/testing.md#behavioral-verification check-serve cycles with a linked ProgramPlugin must report one more load and no update per cycle, never reused, and match cold `check` status and output; a kept Program fails the counters.
|
|
26
|
+
// @evidence contracts/testing.md#independent-expectations The residency rule (a mutating linked hook requires a fresh Program) fixes the counters; cold `check` of the same files is the oracle for the answers.
|
|
27
|
+
// @evidence contracts/testing.md#distinguishing-cases An edit of a known source would be an incremental update without the plugin, as TestCheckServeMatchesColdCheck asserts; here it must reload, and both a passing and a failing answer are compared.
|
|
28
|
+
// @evidence contracts/testing.md#execution-ownership The native Go runner discovers this unit test; the plugin is selected with t.Setenv and check-serve runs in a goroutine of the test process.
|
|
29
|
+
func TestCheckServeReloadsWithProgramPlugin(t *testing.T) {
|
|
30
|
+
root := checkServeProject(t)
|
|
31
|
+
main := filepath.Join(root, "src", "main.ts")
|
|
32
|
+
typiaCheckWrite(t, main, typiaCheckValidSource)
|
|
33
|
+
// buildObservationPlugin consumes this file on every application.
|
|
34
|
+
typiaCheckWrite(t, filepath.Join(root, "external.txt"), "consumed\n")
|
|
35
|
+
payload, _ := json.Marshal([]driver.PluginEntry{{Name: "observation-test", Stage: "transform", Config: map[string]any{"mode": "complete"}}})
|
|
36
|
+
t.Setenv(driver.LinkedPluginsEnv, string(payload))
|
|
37
|
+
args := []string{"--tsconfig=" + filepath.Join(root, "tsconfig.json"), "--cwd=" + root}
|
|
38
|
+
serve := startCheckServe(t, args)
|
|
39
|
+
|
|
40
|
+
first := serve.send(t, `{}`)
|
|
41
|
+
if first.Status != 0 || first.Telemetry.ProgramLoads != 1 || first.Telemetry.Reused {
|
|
42
|
+
t.Fatalf("first cycle = %#v", first)
|
|
43
|
+
}
|
|
44
|
+
checkServeAssertCold(t, args, first)
|
|
45
|
+
|
|
46
|
+
typiaCheckWrite(t, main, transformDiagnosticSource)
|
|
47
|
+
second := serve.send(t, checkServeChanged(t, main))
|
|
48
|
+
if second.Status != 3 || second.Telemetry.ProgramLoads != 2 ||
|
|
49
|
+
second.Telemetry.ProgramUpdates != 0 || second.Telemetry.Reused || !second.TypeScriptDiagnosticsReported {
|
|
50
|
+
t.Fatalf("edited cycle = %#v", second)
|
|
51
|
+
}
|
|
52
|
+
checkServeAssertCold(t, args, second)
|
|
53
|
+
if code := serve.stop(); code != 0 {
|
|
54
|
+
t.Fatalf("check-serve exited with %d after stdin closed", code)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
package main
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"bytes"
|
|
5
|
+
"os"
|
|
6
|
+
"path/filepath"
|
|
7
|
+
"reflect"
|
|
8
|
+
"sync"
|
|
9
|
+
"testing"
|
|
10
|
+
|
|
11
|
+
shimast "github.com/microsoft/typescript-go/shim/ast"
|
|
12
|
+
shimprinter "github.com/microsoft/typescript-go/shim/printer"
|
|
13
|
+
"github.com/samchon/ttsc/packages/ttsc/driver"
|
|
14
|
+
nativecontext "github.com/samchon/typia/packages/typia/native/core/context"
|
|
15
|
+
schemametadata "github.com/samchon/typia/packages/typia/native/core/schemas/metadata"
|
|
16
|
+
nativetransform "github.com/samchon/typia/packages/typia/native/transform"
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
// TestCheckTransformLeavesProgramUntouched verifies typia's analysis rewrites
|
|
20
|
+
// calls without writing into the Program's parsed ASTs.
|
|
21
|
+
//
|
|
22
|
+
// check-serve keeps a Program across watch cycles only because of this: the
|
|
23
|
+
// next cycle's UpdateProgram shares every unchanged file's tree, so a call an
|
|
24
|
+
// earlier cycle rewrote in place would be read as already transformed.
|
|
25
|
+
//
|
|
26
|
+
// 1. Load a noEmit Program whose sources nest typia calls inside a namespace,
|
|
27
|
+
// a class and a function, so their ancestors must be rebuilt.
|
|
28
|
+
// 2. Record every node's identity, kind, range, flags, parent and text.
|
|
29
|
+
// 3. Run the analysis, then the transformer directly, requiring a rewritten
|
|
30
|
+
// file in place of the parsed one.
|
|
31
|
+
// 4. Require the recorded trees unchanged, and require the same record to
|
|
32
|
+
// notice a linked-hook style in-place literal edit.
|
|
33
|
+
//
|
|
34
|
+
// @evidence contracts/testing.md#behavioral-verification analyzeTypiaProgram and nativetransform.Transform run over a loaded Program; the transformer must return a different SourceFile for the typia call while every parsed node keeps its pointer, kind, range, flags, parent and text.
|
|
35
|
+
// @evidence contracts/testing.md#independent-expectations The parse-time record taken before any transform is the oracle; the expected rewrite follows from the typia.is call, not from printed output.
|
|
36
|
+
// @evidence contracts/testing.md#distinguishing-cases A rewritten file and an untouched plain file are both recorded; the in-place literal edit is the negative twin proving the record detects mutation rather than passing vacuously.
|
|
37
|
+
// @evidence contracts/testing.md#execution-ownership The native Go runner discovers this unit test; it loads the fixture through the driver in process and starts no compiler subprocess.
|
|
38
|
+
func TestCheckTransformLeavesProgramUntouched(t *testing.T) {
|
|
39
|
+
project := checkUntouchedProject(t)
|
|
40
|
+
prog, diags, err := driver.LoadProgram(project, "tsconfig.json", driver.LoadProgramOptions{ForceNoEmit: true})
|
|
41
|
+
if err != nil || len(diags) != 0 {
|
|
42
|
+
t.Fatalf("load program: err=%v diagnostics=%#v", err, diags)
|
|
43
|
+
}
|
|
44
|
+
defer prog.Close()
|
|
45
|
+
files := map[string]*shimast.SourceFile{}
|
|
46
|
+
for _, name := range []string{"main.ts", "plain.ts"} {
|
|
47
|
+
// No linked hook is selected here, so this lookup changes nothing.
|
|
48
|
+
file := prog.SourceFile(filepath.ToSlash(filepath.Join(project, "src", name)))
|
|
49
|
+
if file == nil {
|
|
50
|
+
t.Fatalf("src/%s is not in the program", name)
|
|
51
|
+
}
|
|
52
|
+
files[name] = file
|
|
53
|
+
}
|
|
54
|
+
before := map[string][]checkUntouchedNode{}
|
|
55
|
+
for name, file := range files {
|
|
56
|
+
before[name] = checkUntouchedRecord(file)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
var errText bytes.Buffer
|
|
60
|
+
settings := buildSettings{cwd: project, tsconfig: "tsconfig.json", quiet: true, rewriteMode: "typia"}
|
|
61
|
+
if status := analyzeTypiaProgram(prog, settings, &errText, &errText); status != 0 {
|
|
62
|
+
t.Fatalf("analysis failed with %d:\n%s", status, errText.String())
|
|
63
|
+
}
|
|
64
|
+
registerTypiaDefaultLibraryClassifier(prog)
|
|
65
|
+
defer schemametadata.MetadataDefaultLibrary_release(prog.Checker)
|
|
66
|
+
rewritten := nativetransform.Transform(prog, &nativecontext.ITransformOptions{}, nativecontext.ITypiaContext_Extras{Shared: &sync.Map{}}, shimprinter.NewEmitContext())(files["main.ts"])
|
|
67
|
+
if rewritten == nil || rewritten == files["main.ts"] {
|
|
68
|
+
t.Fatal("the typia calls in src/main.ts must be rewritten into a new file")
|
|
69
|
+
}
|
|
70
|
+
for name, file := range files {
|
|
71
|
+
if after := checkUntouchedRecord(file); !reflect.DeepEqual(before[name], after) {
|
|
72
|
+
t.Fatalf("typia's analysis changed the parsed tree of src/%s", name)
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
transformLinkedEditRewrite(files["plain.ts"].AsNode())
|
|
77
|
+
if reflect.DeepEqual(before["plain.ts"], checkUntouchedRecord(files["plain.ts"])) {
|
|
78
|
+
t.Fatal("the record must notice an in-place literal edit")
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// checkUntouchedNode is what an in-place edit of one parsed node can change.
|
|
83
|
+
type checkUntouchedNode struct {
|
|
84
|
+
node *shimast.Node
|
|
85
|
+
parent *shimast.Node
|
|
86
|
+
kind shimast.Kind
|
|
87
|
+
pos int
|
|
88
|
+
end int
|
|
89
|
+
flags shimast.NodeFlags
|
|
90
|
+
text string
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
func checkUntouchedRecord(file *shimast.SourceFile) []checkUntouchedNode {
|
|
94
|
+
var nodes []checkUntouchedNode
|
|
95
|
+
var visit func(node *shimast.Node)
|
|
96
|
+
visit = func(node *shimast.Node) {
|
|
97
|
+
entry := checkUntouchedNode{node: node, parent: node.Parent, kind: node.Kind, pos: node.Pos(), end: node.End(), flags: node.Flags}
|
|
98
|
+
switch node.Kind {
|
|
99
|
+
case shimast.KindIdentifier, shimast.KindStringLiteral, shimast.KindNumericLiteral, shimast.KindNoSubstitutionTemplateLiteral:
|
|
100
|
+
entry.text = node.Text()
|
|
101
|
+
}
|
|
102
|
+
nodes = append(nodes, entry)
|
|
103
|
+
node.ForEachChild(func(child *shimast.Node) bool {
|
|
104
|
+
visit(child)
|
|
105
|
+
return false
|
|
106
|
+
})
|
|
107
|
+
}
|
|
108
|
+
visit(file.AsNode())
|
|
109
|
+
return nodes
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
func checkUntouchedProject(t *testing.T) string {
|
|
113
|
+
t.Helper()
|
|
114
|
+
project := t.TempDir()
|
|
115
|
+
if err := os.MkdirAll(filepath.Join(project, "src"), 0o755); err != nil {
|
|
116
|
+
t.Fatalf("mkdir fixture src: %v", err)
|
|
117
|
+
}
|
|
118
|
+
transformDiagnosticWriteTypiaStub(t, project)
|
|
119
|
+
files := map[string]string{
|
|
120
|
+
"tsconfig.json": `{
|
|
121
|
+
"compilerOptions": {
|
|
122
|
+
"target": "ES2022",
|
|
123
|
+
"module": "commonjs",
|
|
124
|
+
"moduleResolution": "bundler",
|
|
125
|
+
"ignoreDeprecations": "6.0",
|
|
126
|
+
"types": ["*"],
|
|
127
|
+
"strict": true,
|
|
128
|
+
"skipLibCheck": true,
|
|
129
|
+
"noEmit": true
|
|
130
|
+
},
|
|
131
|
+
"include": ["src"]
|
|
132
|
+
}
|
|
133
|
+
`,
|
|
134
|
+
filepath.Join("src", "main.ts"): `import typia from "typia";
|
|
135
|
+
export namespace Checks {
|
|
136
|
+
export class Value {
|
|
137
|
+
public static is(input: unknown): input is { value: number } {
|
|
138
|
+
return typia.is<{ value: number }>(input);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
export function named(input: unknown): input is { name: string } {
|
|
143
|
+
return typia.is<{ name: string }>(input);
|
|
144
|
+
}
|
|
145
|
+
`,
|
|
146
|
+
filepath.Join("src", "plain.ts"): `export const spec = "linked-pending";
|
|
147
|
+
`,
|
|
148
|
+
}
|
|
149
|
+
for name, text := range files {
|
|
150
|
+
if err := os.WriteFile(filepath.Join(project, name), []byte(text), 0o644); err != nil {
|
|
151
|
+
t.Fatalf("write %s: %v", name, err)
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return project
|
|
155
|
+
}
|
|
@@ -45,6 +45,8 @@ func run(args []string) int {
|
|
|
45
45
|
return runTransform(args[1:])
|
|
46
46
|
case "check":
|
|
47
47
|
return runBuild(append([]string{"--noEmit"}, args[1:]...))
|
|
48
|
+
case "check-serve":
|
|
49
|
+
return runCheckServe(os.Stdin, stdout, args[1:])
|
|
48
50
|
case "-p", "--project":
|
|
49
51
|
if len(args) < 2 {
|
|
50
52
|
fmt.Fprintln(stderr, "ttsc-typia: -p/--project requires a path argument")
|
|
@@ -79,6 +81,7 @@ Usage:
|
|
|
79
81
|
Commands:
|
|
80
82
|
build Compile every .ts in a project and emit typia-rewritten .js.
|
|
81
83
|
check Run the analyzer without emitting.
|
|
84
|
+
check-serve Answer check requests over stdin/stdout from one resident program.
|
|
82
85
|
transform Emit a single file's typia-rewritten JS.
|
|
83
86
|
demo Run a backend smoke path.
|
|
84
87
|
version Print version, build info, and platform.
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
package main
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"encoding/json"
|
|
5
|
+
"os"
|
|
6
|
+
"path/filepath"
|
|
7
|
+
"strings"
|
|
8
|
+
"testing"
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
// TestProjectTransformPublishesUntouchedSourceVerbatim verifies the transform
|
|
12
|
+
// envelope publishes a file typia did not change as its authored text, and a
|
|
13
|
+
// file typia changed with a source map back to its authored text.
|
|
14
|
+
//
|
|
15
|
+
// The consumer keeps a module only when the published text equals the text it
|
|
16
|
+
// read. A printed copy of an unchanged file differs from it in formatting, so a
|
|
17
|
+
// bundler took every module as transformed and served lines that no longer
|
|
18
|
+
// matched the file; under Bun, which ignores the map an `onLoad` returns,
|
|
19
|
+
// breakpoints broke in every module of a typia project (samchon/typia#2557).
|
|
20
|
+
//
|
|
21
|
+
// 1. Build a project whose `main.ts` calls `typia.is<Point>()`, whose
|
|
22
|
+
// `plain.ts` has no typia call and is formatted the way the printer would
|
|
23
|
+
// rewrite it, and whose `crlf.ts` has no typia call and CRLF line endings.
|
|
24
|
+
// 2. Run project transform mode and decode the JSON envelope.
|
|
25
|
+
// 3. Assert `plain.ts` and `crlf.ts` are published byte for byte as written,
|
|
26
|
+
// with no source map entry.
|
|
27
|
+
// 4. Assert `main.ts` is lowered and carries a version 3 map whose
|
|
28
|
+
// `sourcesContent` is its authored text.
|
|
29
|
+
// 5. Run single-file transform mode on `plain.ts` and assert it prints the
|
|
30
|
+
// authored text too.
|
|
31
|
+
//
|
|
32
|
+
// @evidence contracts/testing.md#behavioral-verification The decoded envelope publishes plain.ts and crlf.ts byte for byte without a map entry, lowers main.ts with a version 3 map naming main.ts, and single-file mode prints plain.ts as written.
|
|
33
|
+
// @evidence contracts/testing.md#independent-expectations The authored fixture strings are the oracle for the unchanged files and for the map's sourcesContent; the lowered main.ts is recognized by the absence of the `typia.is<Point>` call it replaces, not by output the implementation produced.
|
|
34
|
+
// @evidence contracts/testing.md#distinguishing-cases A transformed file is paired with two untransformed ones, one formatted unlike the printer's output and one with CRLF endings the printer would normalize, so a reprint fails either comparison and a missing or misattached map fails the main.ts checks.
|
|
35
|
+
// @evidence contracts/testing.md#execution-ownership The native Go runner discovers TestProjectTransformPublishesUntouchedSourceVerbatim as a unit test. Its fixture project and captured runTransform calls exercise the owning Go operation in process; no consumer installation or compiler subprocess is used.
|
|
36
|
+
func TestProjectTransformPublishesUntouchedSourceVerbatim(t *testing.T) {
|
|
37
|
+
project := projectTransformUntouchedSourceProject(t)
|
|
38
|
+
out, errText, code := ttscTypiaTestCapture(func() int {
|
|
39
|
+
return runTransform([]string{
|
|
40
|
+
"--cwd", project,
|
|
41
|
+
"--tsconfig", "tsconfig.json",
|
|
42
|
+
"--output", "ts",
|
|
43
|
+
})
|
|
44
|
+
})
|
|
45
|
+
if code != 0 {
|
|
46
|
+
t.Fatalf("project transform failed: code=%d stderr=\n%s", code, errText)
|
|
47
|
+
}
|
|
48
|
+
var envelope struct {
|
|
49
|
+
TypeScript map[string]string `json:"typescript"`
|
|
50
|
+
SourceMaps map[string]struct {
|
|
51
|
+
Version int `json:"version"`
|
|
52
|
+
File string `json:"file"`
|
|
53
|
+
Sources []string `json:"sources"`
|
|
54
|
+
SourcesContent []*string `json:"sourcesContent"`
|
|
55
|
+
Mappings string `json:"mappings"`
|
|
56
|
+
} `json:"sourceMaps"`
|
|
57
|
+
}
|
|
58
|
+
if err := json.Unmarshal([]byte(out), &envelope); err != nil {
|
|
59
|
+
t.Fatalf("decode envelope: %v\n%s", err, out)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
for key, authored := range map[string]string{
|
|
63
|
+
"src/plain.ts": projectTransformUntouchedSourcePlain,
|
|
64
|
+
"src/crlf.ts": projectTransformUntouchedSourceCRLF,
|
|
65
|
+
} {
|
|
66
|
+
if text, ok := envelope.TypeScript[key]; !ok || text != authored {
|
|
67
|
+
t.Fatalf("%s has no typia call and must be published as written:\nwant %q\ngot %q", key, authored, text)
|
|
68
|
+
}
|
|
69
|
+
if _, ok := envelope.SourceMaps[key]; ok {
|
|
70
|
+
t.Fatalf("%s is published as written and must carry no source map", key)
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
main := envelope.TypeScript["src/main.ts"]
|
|
75
|
+
if main == "" || strings.Contains(main, "typia.is<Point>") {
|
|
76
|
+
t.Fatalf("src/main.ts must be published with its typia call lowered, got:\n%s", main)
|
|
77
|
+
}
|
|
78
|
+
sourceMap, ok := envelope.SourceMaps["src/main.ts"]
|
|
79
|
+
if !ok {
|
|
80
|
+
t.Fatalf("src/main.ts was changed and must carry a source map:\n%s", out)
|
|
81
|
+
}
|
|
82
|
+
if sourceMap.Version != 3 || sourceMap.Mappings == "" || sourceMap.File != "main.ts" {
|
|
83
|
+
t.Fatalf("src/main.ts must carry a version 3 map for main.ts with mappings: %+v", sourceMap)
|
|
84
|
+
}
|
|
85
|
+
if len(sourceMap.Sources) != 1 || sourceMap.Sources[0] != "main.ts" {
|
|
86
|
+
t.Fatalf("the src/main.ts map must name main.ts as its one source: %v", sourceMap.Sources)
|
|
87
|
+
}
|
|
88
|
+
if len(sourceMap.SourcesContent) != 1 || sourceMap.SourcesContent[0] == nil || *sourceMap.SourcesContent[0] != projectTransformUntouchedSourceMain {
|
|
89
|
+
t.Fatalf("the src/main.ts map must carry the authored text as its sourcesContent: %v", sourceMap.SourcesContent)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
single, errText, code := ttscTypiaTestCapture(func() int {
|
|
93
|
+
return runTransform([]string{
|
|
94
|
+
"--cwd", project,
|
|
95
|
+
"--tsconfig", "tsconfig.json",
|
|
96
|
+
"--file", "src/plain.ts",
|
|
97
|
+
"--output", "ts",
|
|
98
|
+
})
|
|
99
|
+
})
|
|
100
|
+
if code != 0 {
|
|
101
|
+
t.Fatalf("single-file transform failed: code=%d stderr=\n%s", code, errText)
|
|
102
|
+
}
|
|
103
|
+
if single != projectTransformUntouchedSourcePlain {
|
|
104
|
+
t.Fatalf("single-file mode must print an untouched file as written:\nwant %q\ngot %q", projectTransformUntouchedSourcePlain, single)
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
func projectTransformUntouchedSourceProject(t *testing.T) string {
|
|
109
|
+
t.Helper()
|
|
110
|
+
dir := ttscTypiaTestFixtureDirectory(t, "project-transform-untouched-source-")
|
|
111
|
+
src := filepath.Join(dir, "src")
|
|
112
|
+
if err := os.MkdirAll(src, 0o755); err != nil {
|
|
113
|
+
t.Fatalf("mkdir fixture src: %v", err)
|
|
114
|
+
}
|
|
115
|
+
if err := os.WriteFile(filepath.Join(dir, "tsconfig.json"), []byte(projectDependenciesEnvelopeTSConfig), 0o644); err != nil {
|
|
116
|
+
t.Fatalf("write tsconfig: %v", err)
|
|
117
|
+
}
|
|
118
|
+
for name, body := range map[string]string{
|
|
119
|
+
"main.ts": projectTransformUntouchedSourceMain,
|
|
120
|
+
"plain.ts": projectTransformUntouchedSourcePlain,
|
|
121
|
+
"crlf.ts": projectTransformUntouchedSourceCRLF,
|
|
122
|
+
} {
|
|
123
|
+
if err := os.WriteFile(filepath.Join(src, name), []byte(body), 0o644); err != nil {
|
|
124
|
+
t.Fatalf("write %s: %v", name, err)
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return dir
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const projectTransformUntouchedSourceMain = `import typia from "typia";
|
|
131
|
+
|
|
132
|
+
import type { Point } from "./plain";
|
|
133
|
+
|
|
134
|
+
export const isPoint = (input: unknown) => typia.is<Point>(input);
|
|
135
|
+
`
|
|
136
|
+
|
|
137
|
+
const projectTransformUntouchedSourcePlain = `export interface Point { x: number; y: number }
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
export const origin: Point = {x:0,y:0};
|
|
141
|
+
export function add(a:Point,b:Point):Point{return {x:a.x+b.x,y:a.y+b.y}}
|
|
142
|
+
`
|
|
143
|
+
|
|
144
|
+
const projectTransformUntouchedSourceCRLF = "export const greeting: string = \"hello\";\r\n\r\nexport const twice = (value: string): string => value + value;\r\n"
|
|
@@ -25,7 +25,7 @@ import (
|
|
|
25
25
|
// runTransform is the source-to-source host that drives the ttsc transform
|
|
26
26
|
// stage. Unlike `build` (which emits .js), it returns
|
|
27
27
|
// transformed TypeScript: typia's per-file node transformer runs in an
|
|
28
|
-
// EmitContext and
|
|
28
|
+
// EmitContext and a changed SourceFile is printed back as TS (no JS script
|
|
29
29
|
// transformers), so injected namespace imports stay as ES imports the caller
|
|
30
30
|
// can type-strip per file. With no --file it emits a JSON envelope of every
|
|
31
31
|
// project source keyed by its cwd-relative path; with --file it prints one file.
|
|
@@ -127,7 +127,8 @@ func runTransform(args []string) int {
|
|
|
127
127
|
if kind == "js" {
|
|
128
128
|
return transformSingleToJavaScript(prog, typiaTransform, target)
|
|
129
129
|
}
|
|
130
|
-
|
|
130
|
+
text, _ := transformFileToTypeScript(prog, typiaTransform, target)
|
|
131
|
+
return text, 0
|
|
131
132
|
})
|
|
132
133
|
}
|
|
133
134
|
|
|
@@ -179,11 +180,12 @@ func runTransformSingle(
|
|
|
179
180
|
return writeSingleOutput(text, outPath)
|
|
180
181
|
}
|
|
181
182
|
|
|
182
|
-
// runTransformProject
|
|
183
|
-
// TypeScript
|
|
184
|
-
//
|
|
185
|
-
//
|
|
186
|
-
//
|
|
183
|
+
// runTransformProject publishes every non-declaration project source as
|
|
184
|
+
// TypeScript, printed with a source map where it was changed and as its
|
|
185
|
+
// authored text where it was not, and encodes the JSON envelope the ttsc host
|
|
186
|
+
// expects. Alongside the sources it reports two invalidation channels the ttsc
|
|
187
|
+
// consumer registers with the bundler so persistent caches and watch mode
|
|
188
|
+
// invalidate generated validators soundly:
|
|
187
189
|
//
|
|
188
190
|
// - `graph`, the host-owned reference graph of the loaded program (direct
|
|
189
191
|
// resolved edges, ambient globals, tsconfig extends chain) computed by the
|
|
@@ -260,7 +262,14 @@ func runTransformProject(
|
|
|
260
262
|
continue
|
|
261
263
|
}
|
|
262
264
|
collector.Begin(key)
|
|
263
|
-
|
|
265
|
+
text, sourceMap := transformFileToTypeScript(prog, typiaTransform, sf)
|
|
266
|
+
out.TypeScript[key] = text
|
|
267
|
+
if sourceMap != "" {
|
|
268
|
+
if out.SourceMaps == nil {
|
|
269
|
+
out.SourceMaps = map[string]json.RawMessage{}
|
|
270
|
+
}
|
|
271
|
+
out.SourceMaps[key] = json.RawMessage(sourceMap)
|
|
272
|
+
}
|
|
264
273
|
collector.End()
|
|
265
274
|
}
|
|
266
275
|
out.Dependencies = collector.ToJSON()
|
|
@@ -285,24 +294,54 @@ func runTransformProject(
|
|
|
285
294
|
}
|
|
286
295
|
|
|
287
296
|
// transformFileToTypeScript runs typia's node transformer on one source file in
|
|
288
|
-
// a fresh EmitContext and
|
|
289
|
-
// the
|
|
297
|
+
// a fresh EmitContext and returns the file's TypeScript with a source map back
|
|
298
|
+
// to the authored text. It deliberately skips the JS script transformers
|
|
299
|
+
// (type-erase, module-transform): the caller wants TS.
|
|
300
|
+
//
|
|
301
|
+
// A file typia left alone comes back as the parsed text itself, with no map.
|
|
302
|
+
// The transformer rebuilds a node only when it replaces something beneath it,
|
|
303
|
+
// so the original SourceFile coming back means nothing was replaced. Printing
|
|
304
|
+
// it anyway would hand the consumer the author's code in the printer's
|
|
305
|
+
// formatting, which a bundler then takes as a changed module and serves with
|
|
306
|
+
// lines that no longer match the file on disk; a host that ignores input maps,
|
|
307
|
+
// such as Bun's `onLoad`, loses breakpoints in every module (samchon/typia#2557).
|
|
308
|
+
//
|
|
309
|
+
// The parsed text is the authored text only while nothing else rewrote the
|
|
310
|
+
// file. A linked ProgramPlugin edits the tree in place without replacing the
|
|
311
|
+
// SourceFile, and a linked source preamble is inserted into the parsed text,
|
|
312
|
+
// so under either the file is printed like a changed one.
|
|
313
|
+
//
|
|
314
|
+
// A printed file carries a version 3 map back to the authored text, with that
|
|
315
|
+
// text as `sourcesContent`, so the consumer can check the map against the text
|
|
316
|
+
// it was handed before composing it. The driver moves a preamble-bearing map
|
|
317
|
+
// off the preamble; a map it cannot correct is dropped rather than published
|
|
318
|
+
// pointing at the wrong lines, and a printed text equal to the authored text
|
|
319
|
+
// needs no map.
|
|
290
320
|
func transformFileToTypeScript(
|
|
291
321
|
prog *driver.Program,
|
|
292
322
|
typiaTransform driver.PluginTransform,
|
|
293
323
|
sf *shimast.SourceFile,
|
|
294
|
-
) string {
|
|
295
|
-
options := prog.TSProgram.Options()
|
|
324
|
+
) (string, string) {
|
|
296
325
|
ec := shimprinter.NewEmitContext()
|
|
297
326
|
result := sf
|
|
298
327
|
if next := typiaTransform(ec, result); next != nil {
|
|
299
328
|
result = next
|
|
300
329
|
}
|
|
330
|
+
if result == sf && prog.SourcePreamble == "" && !prog.HasLinkedProgramPlugins() {
|
|
331
|
+
return sf.Text(), ""
|
|
332
|
+
}
|
|
301
333
|
shimast.SetParentInChildrenUnset(result.AsNode())
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
334
|
+
text, sourceMap := shimprinter.EmitSourceFileWithSourceMap(
|
|
335
|
+
shimprinter.PrinterOptions{NewLine: prog.TSProgram.Options().NewLine, InlineSources: true},
|
|
336
|
+
shimprinter.PrintHandlers{},
|
|
337
|
+
ec,
|
|
338
|
+
result,
|
|
339
|
+
)
|
|
340
|
+
authored, corrected, ok := prog.AuthoredSourceMap(sf, sourceMap)
|
|
341
|
+
if !ok || text == authored {
|
|
342
|
+
return text, ""
|
|
343
|
+
}
|
|
344
|
+
return text, corrected
|
|
306
345
|
}
|
|
307
346
|
|
|
308
347
|
// transformSingleToJavaScript emits a single file's JS through the full node-path
|
|
@@ -396,6 +435,10 @@ func defaultTransformOutput(prog *driver.Program) string {
|
|
|
396
435
|
type transformProjectOutput struct {
|
|
397
436
|
Diagnostics []transformCompilerDiagnostic `json:"diagnostics,omitempty"`
|
|
398
437
|
TypeScript map[string]string `json:"typescript"`
|
|
438
|
+
// SourceMaps maps each printed TypeScript key whose text differs from the
|
|
439
|
+
// authored text to a version 3 map back to that text, keyed like TypeScript.
|
|
440
|
+
// A file published as its authored text has no entry.
|
|
441
|
+
SourceMaps map[string]json.RawMessage `json:"sourceMaps,omitempty"`
|
|
399
442
|
// Graph is the host-owned reference graph of the loaded program produced by
|
|
400
443
|
// the ttsc driver SDK (driver.NewTransformGraph): direct resolved reference
|
|
401
444
|
// edges, ambient global-scope files, and the tsconfig extends chain, keyed
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
package main
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"encoding/json"
|
|
5
|
+
"os"
|
|
6
|
+
"path/filepath"
|
|
7
|
+
"strings"
|
|
8
|
+
"testing"
|
|
9
|
+
|
|
10
|
+
shimast "github.com/microsoft/typescript-go/shim/ast"
|
|
11
|
+
shimcore "github.com/microsoft/typescript-go/shim/core"
|
|
12
|
+
shimprinter "github.com/microsoft/typescript-go/shim/printer"
|
|
13
|
+
"github.com/samchon/ttsc/packages/ttsc/driver"
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
// The driver pairs manifest entries with registrations by position. Go runs
|
|
17
|
+
// package init functions in file-name order, so this registration follows the
|
|
18
|
+
// one in build_emit_hook_observations_test.go and answers manifest entry 1.
|
|
19
|
+
func init() { driver.RegisterPlugin(transformLinkedEditPlugin{}) }
|
|
20
|
+
|
|
21
|
+
// TestTransformPrintsFilesRewrittenOutsideTypia verifies a file typia did not
|
|
22
|
+
// change is still printed, with a map back to its authored text, when its
|
|
23
|
+
// parsed text is not that authored text.
|
|
24
|
+
//
|
|
25
|
+
// The transform host publishes an untouched file as its parsed text instead of
|
|
26
|
+
// printing it (samchon/typia#2557). That holds only while nothing else rewrote
|
|
27
|
+
// the file: a linked source preamble is inserted into the parsed text, and a
|
|
28
|
+
// linked ProgramPlugin edits the tree in place, so publishing the parsed text
|
|
29
|
+
// would leak the preamble without a map or drop the plugin's edit.
|
|
30
|
+
//
|
|
31
|
+
// 1. Load a project with a source preamble and run the TypeScript printer on a
|
|
32
|
+
// file through a transform that changes nothing.
|
|
33
|
+
// 2. Assert the published text keeps the preamble and carries a map whose
|
|
34
|
+
// `sourcesContent` is the authored text without it.
|
|
35
|
+
// 3. Run project transform mode with a linked ProgramPlugin that rewrites a
|
|
36
|
+
// string literal in a file typia does not touch.
|
|
37
|
+
// 4. Assert the envelope publishes the rewritten literal, with a map back to
|
|
38
|
+
// the authored text.
|
|
39
|
+
//
|
|
40
|
+
// @evidence contracts/testing.md#behavioral-verification The preamble file is published with the preamble and a map whose sourcesContent is the authored text, and the linked plugin's in-place rewrite reaches the decoded project envelope with its map.
|
|
41
|
+
// @evidence contracts/testing.md#independent-expectations The authored fixture strings and the literal the fixture plugin writes are the oracles; no expected text is printed by the implementation under test.
|
|
42
|
+
// @evidence contracts/testing.md#distinguishing-cases A preamble with no tree edit and a tree edit with no preamble each leave typia's result pointer unchanged, so publishing the parsed text fails the preamble map check in one case and the rewritten literal check in the other.
|
|
43
|
+
// @evidence contracts/testing.md#execution-ownership The native Go runner discovers TestTransformPrintsFilesRewrittenOutsideTypia as a unit test. It loads fixture Programs through the driver and captured runTransform calls in process, selects the linked plugin with t.Setenv, and starts no consumer or compiler subprocess.
|
|
44
|
+
func TestTransformPrintsFilesRewrittenOutsideTypia(t *testing.T) {
|
|
45
|
+
t.Run("source preamble", func(t *testing.T) {
|
|
46
|
+
project := transformRewrittenOutsideTypiaProject(t, "transform-preamble-")
|
|
47
|
+
prog, diags, err := driver.LoadProgram(project, "tsconfig.json", driver.LoadProgramOptions{
|
|
48
|
+
ForceEmit: true,
|
|
49
|
+
SourcePreamble: transformRewrittenOutsideTypiaPreamble,
|
|
50
|
+
})
|
|
51
|
+
if err != nil || len(diags) != 0 {
|
|
52
|
+
t.Fatalf("load program: err=%v diagnostics=%#v", err, diags)
|
|
53
|
+
}
|
|
54
|
+
defer prog.Close()
|
|
55
|
+
sf := prog.SourceFile(filepath.ToSlash(filepath.Join(project, "src", "plain.ts")))
|
|
56
|
+
if sf == nil || !strings.Contains(sf.Text(), transformRewrittenOutsideTypiaPreamble) {
|
|
57
|
+
t.Fatalf("src/plain.ts must be parsed with the preamble")
|
|
58
|
+
}
|
|
59
|
+
unchanged := func(_ *shimprinter.EmitContext, file *shimast.SourceFile) *shimast.SourceFile { return file }
|
|
60
|
+
text, sourceMap := transformFileToTypeScript(prog, unchanged, sf)
|
|
61
|
+
if !strings.Contains(text, "// banner") || !strings.Contains(text, "spec") {
|
|
62
|
+
t.Fatalf("the preamble-bearing file must be printed with the preamble:\n%s", text)
|
|
63
|
+
}
|
|
64
|
+
transformRewrittenOutsideTypiaAssertMap(t, sourceMap)
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
t.Run("linked program plugin", func(t *testing.T) {
|
|
68
|
+
project := transformRewrittenOutsideTypiaProject(t, "transform-linked-edit-")
|
|
69
|
+
// Manifest entry 0 answers buildObservationPlugin, which records this file.
|
|
70
|
+
if err := os.WriteFile(filepath.Join(project, "external.txt"), []byte("consumed\n"), 0o644); err != nil {
|
|
71
|
+
t.Fatalf("write external input: %v", err)
|
|
72
|
+
}
|
|
73
|
+
payload, _ := json.Marshal([]driver.PluginEntry{
|
|
74
|
+
{Name: "observation-test", Stage: "transform", Config: map[string]any{"mode": "complete"}},
|
|
75
|
+
{Name: "linked-edit", Stage: "transform", Config: map[string]any{}},
|
|
76
|
+
})
|
|
77
|
+
t.Setenv(driver.LinkedPluginsEnv, string(payload))
|
|
78
|
+
out, errText, code := ttscTypiaTestCapture(func() int {
|
|
79
|
+
return runTransform([]string{
|
|
80
|
+
"--cwd", project,
|
|
81
|
+
"--tsconfig", "tsconfig.json",
|
|
82
|
+
"--output", "ts",
|
|
83
|
+
})
|
|
84
|
+
})
|
|
85
|
+
if code != 0 {
|
|
86
|
+
t.Fatalf("project transform failed: code=%d stderr=\n%s", code, errText)
|
|
87
|
+
}
|
|
88
|
+
var envelope struct {
|
|
89
|
+
TypeScript map[string]string `json:"typescript"`
|
|
90
|
+
SourceMaps map[string]json.RawMessage `json:"sourceMaps"`
|
|
91
|
+
}
|
|
92
|
+
if err := json.Unmarshal([]byte(out), &envelope); err != nil {
|
|
93
|
+
t.Fatalf("decode envelope: %v\n%s", err, out)
|
|
94
|
+
}
|
|
95
|
+
text := envelope.TypeScript["src/plain.ts"]
|
|
96
|
+
if !strings.Contains(text, "linked-applied") || strings.Contains(text, "linked-pending") {
|
|
97
|
+
t.Fatalf("src/plain.ts must be published with the linked plugin's edit:\n%s", text)
|
|
98
|
+
}
|
|
99
|
+
transformRewrittenOutsideTypiaAssertMap(t, string(envelope.SourceMaps["src/plain.ts"]))
|
|
100
|
+
})
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
func transformRewrittenOutsideTypiaAssertMap(t *testing.T, sourceMap string) {
|
|
104
|
+
t.Helper()
|
|
105
|
+
var decoded struct {
|
|
106
|
+
Version int `json:"version"`
|
|
107
|
+
Sources []string `json:"sources"`
|
|
108
|
+
SourcesContent []*string `json:"sourcesContent"`
|
|
109
|
+
Mappings string `json:"mappings"`
|
|
110
|
+
}
|
|
111
|
+
if err := json.Unmarshal([]byte(sourceMap), &decoded); err != nil {
|
|
112
|
+
t.Fatalf("src/plain.ts must carry a source map: %v\n%s", err, sourceMap)
|
|
113
|
+
}
|
|
114
|
+
if decoded.Version != 3 || decoded.Mappings == "" || len(decoded.Sources) != 1 || decoded.Sources[0] != "plain.ts" {
|
|
115
|
+
t.Fatalf("src/plain.ts must carry a version 3 map for plain.ts: %s", sourceMap)
|
|
116
|
+
}
|
|
117
|
+
if len(decoded.SourcesContent) != 1 || decoded.SourcesContent[0] == nil || *decoded.SourcesContent[0] != transformRewrittenOutsideTypiaPlain {
|
|
118
|
+
t.Fatalf("the src/plain.ts map must carry the authored text as its sourcesContent: %s", sourceMap)
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
func transformRewrittenOutsideTypiaProject(t *testing.T, prefix string) string {
|
|
123
|
+
t.Helper()
|
|
124
|
+
dir := ttscTypiaTestFixtureDirectory(t, prefix)
|
|
125
|
+
if err := os.MkdirAll(filepath.Join(dir, "src"), 0o755); err != nil {
|
|
126
|
+
t.Fatalf("mkdir fixture src: %v", err)
|
|
127
|
+
}
|
|
128
|
+
if err := os.WriteFile(filepath.Join(dir, "tsconfig.json"), []byte(projectDependenciesEnvelopeTSConfig), 0o644); err != nil {
|
|
129
|
+
t.Fatalf("write tsconfig: %v", err)
|
|
130
|
+
}
|
|
131
|
+
if err := os.WriteFile(filepath.Join(dir, "src", "plain.ts"), []byte(transformRewrittenOutsideTypiaPlain), 0o644); err != nil {
|
|
132
|
+
t.Fatalf("write plain.ts: %v", err)
|
|
133
|
+
}
|
|
134
|
+
return dir
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const transformRewrittenOutsideTypiaPreamble = "// banner\n"
|
|
138
|
+
|
|
139
|
+
const transformRewrittenOutsideTypiaPlain = `export const spec = "linked-pending";
|
|
140
|
+
export const twice = (value: number): number => value * 2;
|
|
141
|
+
`
|
|
142
|
+
|
|
143
|
+
// transformLinkedEditPlugin is a linked ProgramPlugin shaped like @ttsc/paths:
|
|
144
|
+
// it rewrites a string literal in place, leaving every SourceFile pointer as
|
|
145
|
+
// it was.
|
|
146
|
+
type transformLinkedEditPlugin struct{}
|
|
147
|
+
|
|
148
|
+
func (transformLinkedEditPlugin) ApplyProgram(prog *driver.Program, _ driver.PluginContext) error {
|
|
149
|
+
for _, sf := range prog.SourceFiles() {
|
|
150
|
+
transformLinkedEditRewrite(sf.AsNode())
|
|
151
|
+
}
|
|
152
|
+
return nil
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
func transformLinkedEditRewrite(node *shimast.Node) {
|
|
156
|
+
if node == nil {
|
|
157
|
+
return
|
|
158
|
+
}
|
|
159
|
+
if node.Kind == shimast.KindStringLiteral && node.Text() == "linked-pending" {
|
|
160
|
+
node.AsStringLiteral().Text = "linked-applied"
|
|
161
|
+
node.Flags |= shimast.NodeFlagsSynthesized
|
|
162
|
+
node.Loc = shimcore.UndefinedTextRange()
|
|
163
|
+
}
|
|
164
|
+
node.ForEachChild(func(child *shimast.Node) bool {
|
|
165
|
+
transformLinkedEditRewrite(child)
|
|
166
|
+
return false
|
|
167
|
+
})
|
|
168
|
+
}
|