ttsc 0.7.1 → 0.7.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/README.md +5 -6
  2. package/cmd/platform/main.go +86 -76
  3. package/cmd/ttsc/api_compile.go +122 -105
  4. package/cmd/ttsc/api_transform.go +57 -49
  5. package/cmd/ttsc/build.go +116 -103
  6. package/cmd/ttsc/main.go +117 -117
  7. package/cmd/ttsc/main_test.go +191 -191
  8. package/driver/host.go +9 -9
  9. package/driver/program.go +285 -285
  10. package/driver/rewrite.go +225 -219
  11. package/lib/compiler/internal/compileProjectInMemory.js +3 -3
  12. package/lib/compiler/internal/compileProjectInMemory.js.map +1 -1
  13. package/lib/compiler/internal/resolveEmittedJavaScript.js +2 -1
  14. package/lib/compiler/internal/resolveEmittedJavaScript.js.map +1 -1
  15. package/lib/compiler/internal/resolveTsgo.js.map +1 -1
  16. package/lib/compiler/internal/runBuild.js.map +1 -1
  17. package/lib/launcher/internal/getCompilerVersionText.js +1 -2
  18. package/lib/launcher/internal/getCompilerVersionText.js.map +1 -1
  19. package/lib/launcher/internal/prepareExecution.js +92 -9
  20. package/lib/launcher/internal/prepareExecution.js.map +1 -1
  21. package/lib/launcher/internal/runTtsc.js +30 -4
  22. package/lib/launcher/internal/runTtsc.js.map +1 -1
  23. package/lib/launcher/internal/runTtsx.js +7 -2
  24. package/lib/launcher/internal/runTtsx.js.map +1 -1
  25. package/lib/plugin/internal/buildSourcePlugin.js.map +1 -1
  26. package/package.json +9 -9
  27. package/shim/ast/lint.go +171 -171
  28. package/shim/ast/shim.go +83 -83
  29. package/shim/bundled/shim.go +5 -1
  30. package/shim/checker/shim.go +79 -79
  31. package/shim/compiler/shim.go +16 -1
  32. package/shim/core/shim.go +10 -10
  33. package/shim/diagnosticwriter/lint.go +79 -79
  34. package/shim/diagnosticwriter/shim.go +19 -19
  35. package/shim/parser/shim.go +7 -1
  36. package/shim/scanner/shim.go +39 -1
  37. package/shim/tsoptions/shim.go +51 -1
  38. package/shim/tspath/shim.go +72 -1
  39. package/shim/vfs/cachedvfs/shim.go +1 -1
  40. package/shim/vfs/osvfs/shim.go +1 -1
  41. package/shim/vfs/shim.go +4 -1
  42. package/src/compiler/internal/compileProjectInMemory.ts +6 -3
  43. package/src/compiler/internal/resolveEmittedJavaScript.ts +2 -1
  44. package/src/compiler/internal/resolveTsgo.ts +0 -1
  45. package/src/compiler/internal/runBuild.ts +4 -1
  46. package/src/launcher/internal/getCompilerVersionText.ts +1 -2
  47. package/src/launcher/internal/prepareExecution.ts +115 -9
  48. package/src/launcher/internal/runTtsc.ts +35 -4
  49. package/src/launcher/internal/runTtsx.ts +16 -5
  50. package/src/plugin/internal/buildSourcePlugin.ts +7 -2
  51. package/test/platform.test.ts +1 -3
  52. package/test/tsgo.test.ts +6 -1
  53. package/tools/gen_shims/main.go +409 -409
@@ -1,12 +1,14 @@
1
1
  import * as fs from "node:fs";
2
+ import * as os from "node:os";
2
3
  import * as path from "node:path";
3
4
 
4
5
  import { resolveEmittedJavaScript } from "../../compiler/internal/resolveEmittedJavaScript";
5
6
  import { runBuild } from "../../compiler/internal/runBuild";
6
- import { resolveProjectConfig } from "../../compiler/internal/project/resolveProjectConfig";
7
+ import { readProjectConfig } from "../../compiler/internal/project/readProjectConfig";
7
8
  import type { TtscCommonOptions } from "../../structures/internal/TtscCommonOptions";
8
9
 
9
10
  const PROCESS_CACHE_KEY = String(process.pid);
11
+ const MAX_VIRTUAL_PARENT_DEPTH = 3;
10
12
 
11
13
  /** Build the owning project and locate the emitted JavaScript entry for `ttsx`. */
12
14
  export function prepareExecution(
@@ -48,22 +50,45 @@ function createProjectContext(
48
50
  filename: string,
49
51
  options: NonNullable<Parameters<typeof prepareExecution>[1]>,
50
52
  ) {
51
- const tsconfig = options.project
52
- ? resolveProjectConfig({ cwd, tsconfig: path.resolve(cwd, options.project) })
53
- : resolveProjectConfig({ cwd, file: filename });
54
- const root = path.dirname(tsconfig);
53
+ const project = readProjectConfig(
54
+ options.project
55
+ ? { cwd, tsconfig: path.resolve(cwd, options.project) }
56
+ : { cwd, file: filename },
57
+ );
58
+ const tsconfig = project.path;
59
+ const root = project.root;
55
60
  const cacheDir =
56
- options.cacheDir ?? path.join(root, "node_modules", ".cache", "ttsc", "ttsx");
61
+ options.cacheDir ??
62
+ path.join(root, "node_modules", ".cache", "ttsc", "ttsx");
63
+ const processDir = path.join(cacheDir, "project", PROCESS_CACHE_KEY);
64
+ const virtualRoot = path.join(processDir, "fs");
57
65
  return {
58
66
  tsconfig,
59
67
  root,
60
68
  cacheDir,
61
- emitDir: path.join(cacheDir, "project", PROCESS_CACHE_KEY),
69
+ processDir,
70
+ virtualRoot,
71
+ emitDir: project.compilerOptions.outDir
72
+ ? virtualPath(virtualRoot, project.compilerOptions.outDir)
73
+ : virtualPath(virtualRoot, resolveRuntimeSourceRoot(project, filename)),
62
74
  built: false,
63
75
  emittedFiles: undefined as string[] | undefined,
64
76
  };
65
77
  }
66
78
 
79
+ function resolveRuntimeSourceRoot(
80
+ project: ReturnType<typeof readProjectConfig>,
81
+ filename: string,
82
+ ): string {
83
+ const rootDir = project.compilerOptions.rootDir;
84
+ if (typeof rootDir === "string") {
85
+ return path.isAbsolute(rootDir)
86
+ ? rootDir
87
+ : path.resolve(project.root, rootDir);
88
+ }
89
+ return path.dirname(filename);
90
+ }
91
+
67
92
  function buildProject(
68
93
  context: ReturnType<typeof createProjectContext>,
69
94
  options: NonNullable<Parameters<typeof prepareExecution>[1]>,
@@ -71,7 +96,8 @@ function buildProject(
71
96
  if (context.built) return;
72
97
 
73
98
  fs.mkdirSync(context.cacheDir, { recursive: true });
74
- fs.rmSync(context.emitDir, { recursive: true, force: true });
99
+ fs.rmSync(context.processDir, { recursive: true, force: true });
100
+ fs.mkdirSync(path.dirname(context.emitDir), { recursive: true });
75
101
  const result = runBuild({
76
102
  binary: options.binary,
77
103
  cwd: context.root,
@@ -84,6 +110,7 @@ function buildProject(
84
110
  tsconfig: context.tsconfig,
85
111
  });
86
112
  if (result.status === 0) {
113
+ linkVirtualProjectLayout(context);
87
114
  context.built = true;
88
115
  context.emittedFiles =
89
116
  result.emittedFiles && result.emittedFiles.length !== 0
@@ -92,7 +119,7 @@ function buildProject(
92
119
  return;
93
120
  }
94
121
 
95
- fs.rmSync(context.emitDir, { recursive: true, force: true });
122
+ fs.rmSync(context.processDir, { recursive: true, force: true });
96
123
  const detail = [
97
124
  `ttsx: project check failed for ${context.tsconfig}`,
98
125
  result.stderr || result.stdout,
@@ -102,6 +129,85 @@ function buildProject(
102
129
  throw new Error(detail);
103
130
  }
104
131
 
132
+ function linkVirtualProjectLayout(
133
+ context: ReturnType<typeof createProjectContext>,
134
+ ): void {
135
+ for (const directory of collectLinkDirectories(context.root)) {
136
+ const virtualDirectory = virtualPath(context.virtualRoot, directory);
137
+ fs.mkdirSync(virtualDirectory, { recursive: true });
138
+ for (const entry of fs.readdirSync(directory, { withFileTypes: true })) {
139
+ const realEntry = path.join(directory, entry.name);
140
+ const virtualEntry = path.join(virtualDirectory, entry.name);
141
+ if (fs.existsSync(virtualEntry)) {
142
+ continue;
143
+ }
144
+ linkVirtualEntry(realEntry, virtualEntry, entry);
145
+ }
146
+ }
147
+ }
148
+
149
+ function linkVirtualEntry(
150
+ realEntry: string,
151
+ virtualEntry: string,
152
+ entry: fs.Dirent,
153
+ ): void {
154
+ if (entry.isDirectory()) {
155
+ fs.symlinkSync(
156
+ realEntry,
157
+ virtualEntry,
158
+ process.platform === "win32" ? "junction" : undefined,
159
+ );
160
+ return;
161
+ }
162
+ if (entry.isFile()) {
163
+ try {
164
+ fs.linkSync(realEntry, virtualEntry);
165
+ } catch {
166
+ fs.copyFileSync(realEntry, virtualEntry);
167
+ }
168
+ return;
169
+ }
170
+ fs.symlinkSync(realEntry, virtualEntry);
171
+ }
172
+
173
+ function collectLinkDirectories(projectRoot: string): string[] {
174
+ const out: string[] = [];
175
+ let current = projectRoot;
176
+ for (let depth = 0; depth <= MAX_VIRTUAL_PARENT_DEPTH; depth += 1) {
177
+ out.push(current);
178
+ if (
179
+ depth > 0 &&
180
+ (fs.existsSync(path.join(current, "pnpm-workspace.yaml")) ||
181
+ fs.existsSync(path.join(current, ".git")))
182
+ ) {
183
+ break;
184
+ }
185
+ const parent = path.dirname(current);
186
+ if (parent === current || isUnsafeVirtualParent(parent)) {
187
+ break;
188
+ }
189
+ current = parent;
190
+ }
191
+ return out.reverse();
192
+ }
193
+
194
+ function isUnsafeVirtualParent(directory: string): boolean {
195
+ const resolved = path.resolve(directory);
196
+ const root = path.parse(resolved).root;
197
+ return resolved === root || resolved === path.resolve(os.tmpdir());
198
+ }
199
+
200
+ function virtualPath(root: string, absolute: string): string {
201
+ const parsed = path.parse(path.resolve(absolute));
202
+ const label =
203
+ parsed.root === path.sep
204
+ ? "posix"
205
+ : parsed.root.replace(/[^a-zA-Z0-9]+/g, "_").replace(/^_+|_+$/g, "") ||
206
+ "root";
207
+ const relative = path.relative(parsed.root, path.resolve(absolute));
208
+ return path.join(root, label, relative);
209
+ }
210
+
105
211
  function looksLikeESM(output: string): boolean {
106
212
  if (
107
213
  /\bObject\.defineProperty\(exports\b/.test(output) ||
@@ -2,6 +2,7 @@ import { spawnSync } from "node:child_process";
2
2
  import * as fs from "node:fs";
3
3
  import * as path from "node:path";
4
4
 
5
+ import { TtscCompiler } from "../../TtscCompiler";
5
6
  import { resolveBinary } from "../../compiler/internal/resolveBinary";
6
7
  import { runBuild } from "../../compiler/internal/runBuild";
7
8
  import { runSingleFileEmit } from "../../compiler/internal/runSingleFileEmit";
@@ -35,6 +36,8 @@ export function runTtsc(
35
36
  return runCompatibleBuild(rest, true);
36
37
  case "clean":
37
38
  return runClean(rest);
39
+ case "prepare":
40
+ return runPrepare(rest);
38
41
  case "demo":
39
42
  return delegateToNative(argv);
40
43
  case "-p":
@@ -82,8 +85,34 @@ function runCompatibleBuild(
82
85
  return result.status;
83
86
  }
84
87
 
88
+ function runPrepare(argv: readonly string[]): number {
89
+ const options = parseProjectArgs(argv);
90
+ const cwd = path.resolve(options.cwd ?? process.cwd());
91
+ const compiler = new TtscCompiler({
92
+ cwd,
93
+ tsconfig: options.tsconfig,
94
+ });
95
+ const prepared = compiler.prepare();
96
+ if (prepared.length === 0) {
97
+ const projectRoot = path.dirname(
98
+ resolveProjectConfig({
99
+ cwd,
100
+ tsconfig: options.tsconfig,
101
+ }),
102
+ );
103
+ process.stdout.write(
104
+ `ttsc: no source plugins found under ${formatProjectPath(cwd, projectRoot)}\n`,
105
+ );
106
+ return 0;
107
+ }
108
+ for (const target of prepared) {
109
+ process.stdout.write(`ttsc: prepared ${formatProjectPath(cwd, target)}\n`);
110
+ }
111
+ return 0;
112
+ }
113
+
85
114
  function runClean(argv: readonly string[]): number {
86
- const options = parseCleanArgs(argv);
115
+ const options = parseProjectArgs(argv);
87
116
  const cwd = path.resolve(options.cwd ?? process.cwd());
88
117
  const projectRoot = resolveCleanProjectRoot(cwd, options.tsconfig);
89
118
  const targets = [
@@ -106,7 +135,7 @@ function runClean(argv: readonly string[]): number {
106
135
  return 0;
107
136
  }
108
137
  for (const target of removed) {
109
- process.stdout.write(`ttsc: removed ${formatCleanPath(cwd, target)}\n`);
138
+ process.stdout.write(`ttsc: removed ${formatProjectPath(cwd, target)}\n`);
110
139
  }
111
140
  return 0;
112
141
  }
@@ -120,7 +149,7 @@ function resolveCleanProjectRoot(cwd: string, tsconfig?: string): string {
120
149
  }
121
150
  }
122
151
 
123
- function formatCleanPath(cwd: string, target: string): string {
152
+ function formatProjectPath(cwd: string, target: string): string {
124
153
  const relative = path.relative(cwd, target);
125
154
  if (!relative || relative.startsWith("..") || path.isAbsolute(relative)) {
126
155
  return target;
@@ -174,7 +203,7 @@ function ensureExecutable(binary: string): void {
174
203
  }
175
204
  }
176
205
 
177
- function parseCleanArgs(argv: readonly string[]) {
206
+ function parseProjectArgs(argv: readonly string[]) {
178
207
  let cwd: string | undefined;
179
208
  let tsconfig: string | undefined;
180
209
 
@@ -303,6 +332,7 @@ function printHelp(): void {
303
332
  " ttsc -p tsconfig.json",
304
333
  " ttsc --watch",
305
334
  " ttsc --noEmit",
335
+ " ttsc prepare [options]",
306
336
  " ttsc clean [options]",
307
337
  " ttsc version",
308
338
  " ttsc --help",
@@ -328,6 +358,7 @@ function printHelp(): void {
328
358
  "Compatibility aliases:",
329
359
  " ttsc build [options] Same project build lane as `ttsc [options]`.",
330
360
  " ttsc check [options] Same as `ttsc --noEmit [options]`.",
361
+ " ttsc prepare [options] Build configured source-plugin binaries into cache.",
331
362
  " ttsc clean [options] Delete local source-plugin cache directories.",
332
363
  ].join("\n"),
333
364
  );
@@ -5,7 +5,9 @@ import * as path from "node:path";
5
5
  import { getCompilerVersionText } from "./getCompilerVersionText";
6
6
  import { prepareExecution } from "./prepareExecution";
7
7
 
8
- export function runTtsx(argv: readonly string[] = process.argv.slice(2)): number {
8
+ export function runTtsx(
9
+ argv: readonly string[] = process.argv.slice(2),
10
+ ): number {
9
11
  try {
10
12
  return run(argv);
11
13
  } catch (error) {
@@ -53,8 +55,10 @@ function formatError(error: unknown): string {
53
55
  function parseCLI(argv: readonly string[]) {
54
56
  const preload: string[] = [];
55
57
  const passthroughIndex = argv.indexOf("--");
56
- const head = passthroughIndex === -1 ? [...argv] : [...argv.slice(0, passthroughIndex)];
57
- const passthrough = passthroughIndex === -1 ? [] : [...argv.slice(passthroughIndex + 1)];
58
+ const head =
59
+ passthroughIndex === -1 ? [...argv] : [...argv.slice(0, passthroughIndex)];
60
+ const passthrough =
61
+ passthroughIndex === -1 ? [] : [...argv.slice(passthroughIndex + 1)];
58
62
 
59
63
  let binary: string | undefined;
60
64
  let cacheDir: string | undefined;
@@ -151,7 +155,11 @@ function printHelp(): void {
151
155
  }
152
156
 
153
157
  function resolvePreload(cwd: string, preload: string): string {
154
- if (preload.startsWith(".") || preload.startsWith("/") || preload.includes(path.sep)) {
158
+ if (
159
+ preload.startsWith(".") ||
160
+ preload.startsWith("/") ||
161
+ preload.includes(path.sep)
162
+ ) {
155
163
  return path.resolve(cwd, preload);
156
164
  }
157
165
  return preload;
@@ -180,7 +188,10 @@ function runPreparedEntry(
180
188
  );
181
189
  }
182
190
  const args = [
183
- ...parsed.preload.flatMap((preload) => ["-r", resolvePreload(cwd, preload)]),
191
+ ...parsed.preload.flatMap((preload) => [
192
+ "-r",
193
+ resolvePreload(cwd, preload),
194
+ ]),
184
195
  execution.entryFile,
185
196
  ...parsed.passthrough,
186
197
  ];
@@ -155,7 +155,9 @@ function writeGoWork(scratchDir: string, useDirs: readonly string[]): void {
155
155
  fs.writeFileSync(path.join(scratchDir, "go.work"), goWork, "utf8");
156
156
  }
157
157
 
158
- function sourceBuildWorkspaceReplacements(useDirs: readonly string[]): string[] {
158
+ function sourceBuildWorkspaceReplacements(
159
+ useDirs: readonly string[],
160
+ ): string[] {
159
161
  const ttscRoot = useDirs.find((dir) =>
160
162
  hasModulePath(dir, "github.com/samchon/ttsc/packages/ttsc"),
161
163
  );
@@ -308,7 +310,10 @@ function walkForGoMod(dir: string, out: string[]): void {
308
310
  }
309
311
  }
310
312
 
311
- function resolvePluginCacheRoot(projectRoot: string, cacheDir?: string): string {
313
+ function resolvePluginCacheRoot(
314
+ projectRoot: string,
315
+ cacheDir?: string,
316
+ ): string {
312
317
  if (cacheDir) {
313
318
  return path.resolve(cacheDir, "plugins");
314
319
  }
@@ -1,8 +1,6 @@
1
1
  const assert = require("node:assert/strict");
2
2
  const test = require("node:test");
3
- const {
4
- resolveBinary,
5
- } = require("../lib/compiler/internal/resolveBinary.js");
3
+ const { resolveBinary } = require("../lib/compiler/internal/resolveBinary.js");
6
4
 
7
5
  test("resolveBinary prefers TTSC_BINARY absolute override", () => {
8
6
  const resolved = resolveBinary({
package/test/tsgo.test.ts CHANGED
@@ -20,7 +20,12 @@ test("resolveTsgo accepts TTSC_TSGO_BINARY as an explicit compiler", () => {
20
20
 
21
21
  test("resolveTsgo resolves the consumer native-preview platform package", () => {
22
22
  const root = fs.mkdtempSync(path.join(os.tmpdir(), "ttsc-tsgo-test-"));
23
- const nativeRoot = path.join(root, "node_modules", "@typescript", "native-preview");
23
+ const nativeRoot = path.join(
24
+ root,
25
+ "node_modules",
26
+ "@typescript",
27
+ "native-preview",
28
+ );
24
29
  const platformRoot = path.join(
25
30
  root,
26
31
  "node_modules",