ttsc 0.8.1 → 0.10.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.
Files changed (88) hide show
  1. package/README.md +39 -68
  2. package/cmd/ttsc/api_compile.go +109 -115
  3. package/cmd/ttsc/api_transform.go +54 -51
  4. package/cmd/ttsc/main.go +103 -121
  5. package/driver/program.go +30 -2
  6. package/driver/rewrite.go +126 -9
  7. package/go.mod +2 -0
  8. package/lib/TtscCompiler.js +28 -46
  9. package/lib/TtscCompiler.js.map +1 -1
  10. package/lib/compiler/internal/buildNativeCompiler.js +8 -38
  11. package/lib/compiler/internal/buildNativeCompiler.js.map +1 -1
  12. package/lib/compiler/internal/compileProjectInMemory.js +36 -63
  13. package/lib/compiler/internal/compileProjectInMemory.js.map +1 -1
  14. package/lib/compiler/internal/project/readProjectConfig.js +108 -60
  15. package/lib/compiler/internal/project/readProjectConfig.js.map +1 -1
  16. package/lib/compiler/internal/project/resolveProjectConfig.js +15 -45
  17. package/lib/compiler/internal/project/resolveProjectConfig.js.map +1 -1
  18. package/lib/compiler/internal/resolveBinary.js +10 -40
  19. package/lib/compiler/internal/resolveBinary.js.map +1 -1
  20. package/lib/compiler/internal/resolveEmittedJavaScript.js +21 -48
  21. package/lib/compiler/internal/resolveEmittedJavaScript.js.map +1 -1
  22. package/lib/compiler/internal/resolveTsgo.js +14 -44
  23. package/lib/compiler/internal/resolveTsgo.js.map +1 -1
  24. package/lib/compiler/internal/runBuild.d.ts +7 -0
  25. package/lib/compiler/internal/runBuild.js +47 -66
  26. package/lib/compiler/internal/runBuild.js.map +1 -1
  27. package/lib/compiler/internal/runSingleFileEmit.js +18 -48
  28. package/lib/compiler/internal/runSingleFileEmit.js.map +1 -1
  29. package/lib/compiler/internal/transformProjectInMemory.js +41 -75
  30. package/lib/compiler/internal/transformProjectInMemory.js.map +1 -1
  31. package/lib/launcher/internal/getCompilerVersionText.js +7 -37
  32. package/lib/launcher/internal/getCompilerVersionText.js.map +1 -1
  33. package/lib/launcher/internal/prepareExecution.js +48 -69
  34. package/lib/launcher/internal/prepareExecution.js.map +1 -1
  35. package/lib/launcher/internal/runTtsc.js +53 -64
  36. package/lib/launcher/internal/runTtsc.js.map +1 -1
  37. package/lib/launcher/internal/runTtsx.js +328 -57
  38. package/lib/launcher/internal/runTtsx.js.map +1 -1
  39. package/lib/plugin/internal/buildSourcePlugin.d.ts +8 -0
  40. package/lib/plugin/internal/buildSourcePlugin.js +287 -139
  41. package/lib/plugin/internal/buildSourcePlugin.js.map +1 -1
  42. package/lib/plugin/internal/loadProjectPlugins.d.ts +2 -0
  43. package/lib/plugin/internal/loadProjectPlugins.js +197 -9
  44. package/lib/plugin/internal/loadProjectPlugins.js.map +1 -1
  45. package/lib/structures/ITtscCompilerContext.d.ts +16 -3
  46. package/lib/structures/ITtscCompilerResult.d.ts +4 -0
  47. package/lib/structures/ITtscCompilerTransformation.d.ts +2 -0
  48. package/lib/structures/ITtscPluginFactoryContext.d.ts +6 -3
  49. package/lib/structures/ITtscProjectPluginConfig.d.ts +8 -7
  50. package/lib/structures/internal/ITtscParsedProjectConfig.d.ts +2 -0
  51. package/lib/structures/internal/ITtscProjectLocatorOptions.d.ts +2 -0
  52. package/lib/structures/internal/TtscCommonOptions.d.ts +2 -0
  53. package/package.json +9 -14
  54. package/shim/diagnosticwriter/lint.go +8 -0
  55. package/shim/printer/go.mod +5 -0
  56. package/shim/printer/shim.go +23 -0
  57. package/src/TtscCompiler.ts +20 -5
  58. package/src/compiler/internal/buildNativeCompiler.ts +2 -2
  59. package/src/compiler/internal/compileProjectInMemory.ts +21 -16
  60. package/src/compiler/internal/project/readProjectConfig.ts +134 -35
  61. package/src/compiler/internal/project/resolveProjectConfig.ts +2 -2
  62. package/src/compiler/internal/resolveBinary.ts +2 -2
  63. package/src/compiler/internal/resolveEmittedJavaScript.ts +11 -7
  64. package/src/compiler/internal/resolveTsgo.ts +2 -2
  65. package/src/compiler/internal/runBuild.ts +34 -21
  66. package/src/compiler/internal/runSingleFileEmit.ts +3 -3
  67. package/src/compiler/internal/transformProjectInMemory.ts +37 -37
  68. package/src/launcher/internal/getCompilerVersionText.ts +2 -2
  69. package/src/launcher/internal/prepareExecution.ts +14 -4
  70. package/src/launcher/internal/runTtsc.ts +30 -4
  71. package/src/launcher/internal/runTtsx.ts +377 -26
  72. package/src/plugin/internal/buildSourcePlugin.ts +321 -50
  73. package/src/plugin/internal/loadProjectPlugins.ts +266 -10
  74. package/src/structures/ITtscCompilerContext.ts +17 -3
  75. package/src/structures/ITtscCompilerResult.ts +5 -0
  76. package/src/structures/ITtscCompilerTransformation.ts +3 -0
  77. package/src/structures/ITtscPluginFactoryContext.ts +6 -3
  78. package/src/structures/ITtscProjectPluginConfig.ts +8 -7
  79. package/src/structures/internal/ITtscParsedProjectConfig.ts +2 -0
  80. package/src/structures/internal/ITtscProjectLocatorOptions.ts +2 -0
  81. package/src/structures/internal/TtscCommonOptions.ts +2 -0
  82. package/utility/host.go +622 -20
  83. package/cmd/ttsc/main_test.go +0 -288
  84. package/test/compiler.test.ts +0 -422
  85. package/test/platform.test.ts +0 -12
  86. package/test/project.test.ts +0 -197
  87. package/test/source-build.test.ts +0 -45
  88. package/test/tsgo.test.ts +0 -69
@@ -1,11 +1,13 @@
1
1
  import { spawnSync } from "node:child_process";
2
- import * as fs from "node:fs";
3
- import * as path from "node:path";
2
+ import fs from "node:fs";
3
+ import path from "node:path";
4
4
 
5
- import { loadProjectPlugins } from "../../plugin/internal/loadProjectPlugins";
5
+ import {
6
+ hasProjectPluginEntries,
7
+ loadProjectPlugins,
8
+ } from "../../plugin/internal/loadProjectPlugins";
6
9
  import type { ITtscCompilerContext } from "../../structures/ITtscCompilerContext";
7
10
  import type { ITtscCompilerDiagnostic } from "../../structures/ITtscCompilerDiagnostic";
8
- import type { ITtscProjectPluginConfig } from "../../structures/ITtscProjectPluginConfig";
9
11
  import type { ITtscLoadedNativePlugin } from "../../structures/internal/ITtscLoadedNativePlugin";
10
12
  import type { ITtscParsedProjectConfig } from "../../structures/internal/ITtscParsedProjectConfig";
11
13
  import type { TtscBuildResult } from "../../structures/internal/TtscBuildResult";
@@ -13,6 +15,7 @@ import { buildNativeCompiler } from "./buildNativeCompiler";
13
15
  import { readProjectConfig } from "./project/readProjectConfig";
14
16
  import { resolveBinary } from "./resolveBinary";
15
17
  import { resolveTsgo } from "./resolveTsgo";
18
+ import { appendBuildOutput, normalizeBuildOutput } from "./runBuild";
16
19
 
17
20
  /** Transform a project and capture TypeScript source output in memory. */
18
21
  export function transformProjectInMemory(options: ITtscCompilerContext): {
@@ -22,23 +25,20 @@ export function transformProjectInMemory(options: ITtscCompilerContext): {
22
25
  const cwd = path.resolve(options.cwd ?? process.cwd());
23
26
  const project = readProjectConfig({
24
27
  cwd,
28
+ projectRoot: options.projectRoot,
25
29
  tsconfig: options.tsconfig,
26
30
  });
27
- if (configuredPlugins(options, project).length !== 0) {
31
+ if (hasConfiguredPlugins(options, project)) {
28
32
  return transformProjectWithPlugins(options, cwd, project);
29
33
  }
30
34
  return transformProjectWithNativeHost(options, project);
31
35
  }
32
36
 
33
- function configuredPlugins(
37
+ function hasConfiguredPlugins(
34
38
  options: ITtscCompilerContext,
35
39
  project: ITtscParsedProjectConfig,
36
- ): ITtscProjectPluginConfig[] {
37
- if (options.plugins === false) {
38
- return [];
39
- }
40
- const entries = options.plugins ?? project.compilerOptions.plugins;
41
- return entries.filter((entry) => entry.enabled !== false);
40
+ ): boolean {
41
+ return hasProjectPluginEntries(project, options.plugins);
42
42
  }
43
43
 
44
44
  function transformProjectWithNativeHost(
@@ -95,6 +95,7 @@ function transformProjectWithPlugins(
95
95
  cacheDir: options.cacheDir ?? options.env?.TTSC_CACHE_DIR,
96
96
  cwd,
97
97
  entries: options.plugins,
98
+ projectRoot: options.projectRoot,
98
99
  tsconfig: project.path,
99
100
  });
100
101
  const checks = loaded.nativePlugins.filter(
@@ -116,7 +117,11 @@ function transformProjectWithPlugins(
116
117
  };
117
118
  }
118
119
  if (transformers.length === 0) {
119
- return transformProjectWithNativeHost(options, project);
120
+ const transformed = transformProjectWithNativeHost(options, project);
121
+ return {
122
+ result: appendBuildOutput(checked, transformed.result),
123
+ typescript: transformed.typescript,
124
+ };
120
125
  }
121
126
  assertTransformHostCompatibility(transformers);
122
127
 
@@ -138,13 +143,14 @@ function transformProjectWithPlugins(
138
143
  outputText(res.stdout),
139
144
  outputText(res.stderr),
140
145
  );
146
+ const result = {
147
+ diagnostics: output.diagnostics,
148
+ status: res.status ?? 1,
149
+ stdout: "",
150
+ stderr: outputText(res.stderr),
151
+ };
141
152
  return {
142
- result: {
143
- diagnostics: output.diagnostics,
144
- status: res.status ?? 1,
145
- stdout: "",
146
- stderr: outputText(res.stderr),
147
- },
153
+ result: appendBuildOutput(checked, result),
148
154
  typescript: output.typescript,
149
155
  };
150
156
  }
@@ -175,12 +181,17 @@ function runNativeChecks(
175
181
  `ttsc.transform.check: failed to spawn ${plugin.binary}: ${res.error.message}`,
176
182
  );
177
183
  }
178
- result = appendBuildResult(result, {
179
- diagnostics: [],
180
- status: res.status ?? 1,
181
- stdout: outputText(res.stdout),
182
- stderr: outputText(res.stderr),
183
- });
184
+ result = appendBuildOutput(
185
+ result,
186
+ normalizeBuildOutput(
187
+ {
188
+ status: res.status ?? 1,
189
+ stdout: outputText(res.stdout),
190
+ stderr: outputText(res.stderr),
191
+ },
192
+ project.root,
193
+ ),
194
+ );
184
195
  if (result.status !== 0) {
185
196
  return result;
186
197
  }
@@ -279,18 +290,6 @@ function readNearestPackageManifest(
279
290
  return undefined;
280
291
  }
281
292
 
282
- function appendBuildResult(
283
- left: TtscBuildResult,
284
- right: TtscBuildResult,
285
- ): TtscBuildResult {
286
- return {
287
- diagnostics: [...left.diagnostics, ...right.diagnostics],
288
- status: right.status !== 0 ? right.status : left.status,
289
- stdout: left.stdout + right.stdout,
290
- stderr: left.stderr + right.stderr,
291
- };
292
- }
293
-
294
293
  function nativePluginEnv(
295
294
  options: ITtscCompilerContext,
296
295
  projectRoot: string,
@@ -390,6 +389,7 @@ function isTextRecord(value: unknown): value is Record<string, string> {
390
389
  return (
391
390
  typeof value === "object" &&
392
391
  value !== null &&
392
+ !Array.isArray(value) &&
393
393
  Object.values(value).every((entry) => typeof entry === "string")
394
394
  );
395
395
  }
@@ -1,6 +1,6 @@
1
1
  import { spawnSync } from "node:child_process";
2
- import * as fs from "node:fs";
3
- import * as path from "node:path";
2
+ import fs from "node:fs";
3
+ import path from "node:path";
4
4
 
5
5
  import { resolveTsgo } from "../../compiler/internal/resolveTsgo";
6
6
  import type { TtscCommonOptions } from "../../structures/internal/TtscCommonOptions";
@@ -1,6 +1,6 @@
1
- import * as fs from "node:fs";
2
- import * as os from "node:os";
3
- import * as path from "node:path";
1
+ import fs from "node:fs";
2
+ import os from "node:os";
3
+ import path from "node:path";
4
4
 
5
5
  import { resolveEmittedJavaScript } from "../../compiler/internal/resolveEmittedJavaScript";
6
6
  import { runBuild } from "../../compiler/internal/runBuild";
@@ -57,8 +57,9 @@ function createProjectContext(
57
57
  );
58
58
  const tsconfig = project.path;
59
59
  const root = project.root;
60
+ const explicitCacheDir = resolveCacheDir(cwd, options.cacheDir);
60
61
  const cacheDir =
61
- options.cacheDir ??
62
+ explicitCacheDir ??
62
63
  path.join(root, "node_modules", ".cache", "ttsc", "ttsx");
63
64
  const processDir = path.join(cacheDir, "project", PROCESS_CACHE_KEY);
64
65
  const virtualRoot = path.join(processDir, "fs");
@@ -67,6 +68,7 @@ function createProjectContext(
67
68
  root,
68
69
  cacheDir,
69
70
  processDir,
71
+ pluginCacheDir: explicitCacheDir,
70
72
  virtualRoot,
71
73
  emitDir: project.compilerOptions.outDir
72
74
  ? virtualPath(virtualRoot, project.compilerOptions.outDir)
@@ -104,6 +106,7 @@ function buildProject(
104
106
  emit: true,
105
107
  env: options.env,
106
108
  forceListEmittedFiles: true,
109
+ cacheDir: context.pluginCacheDir,
107
110
  outDir: context.emitDir,
108
111
  plugins: options.plugins,
109
112
  quiet: true,
@@ -129,6 +132,13 @@ function buildProject(
129
132
  throw new Error(detail);
130
133
  }
131
134
 
135
+ function resolveCacheDir(cwd: string, cacheDir?: string): string | undefined {
136
+ if (!cacheDir) {
137
+ return undefined;
138
+ }
139
+ return path.isAbsolute(cacheDir) ? cacheDir : path.resolve(cwd, cacheDir);
140
+ }
141
+
132
142
  function linkVirtualProjectLayout(
133
143
  context: ReturnType<typeof createProjectContext>,
134
144
  ): void {
@@ -1,6 +1,6 @@
1
1
  import { spawnSync } from "node:child_process";
2
- import * as fs from "node:fs";
3
- import * as path from "node:path";
2
+ import fs from "node:fs";
3
+ import path from "node:path";
4
4
 
5
5
  import { TtscCompiler } from "../../TtscCompiler";
6
6
  import { resolveBinary } from "../../compiler/internal/resolveBinary";
@@ -72,7 +72,7 @@ function runCompatibleBuild(
72
72
  argv: readonly string[],
73
73
  checkOnly: boolean,
74
74
  ): number {
75
- const options = parseBuildArgs(argv, checkOnly);
75
+ const options = normalizeBuildOptions(parseBuildArgs(argv, checkOnly));
76
76
  if (options.watch) {
77
77
  return runWatch(options, checkOnly);
78
78
  }
@@ -85,6 +85,17 @@ function runCompatibleBuild(
85
85
  return result.status;
86
86
  }
87
87
 
88
+ function normalizeBuildOptions(
89
+ options: ReturnType<typeof parseBuildArgs>,
90
+ ): ReturnType<typeof parseBuildArgs> {
91
+ const cwd = path.resolve(options.cwd ?? process.cwd());
92
+ return {
93
+ ...options,
94
+ cacheDir: resolveCacheDir(cwd, options.cacheDir),
95
+ cwd,
96
+ };
97
+ }
98
+
88
99
  function runPrepare(argv: readonly string[]): number {
89
100
  const options = parseProjectArgs(argv);
90
101
  const cwd = path.resolve(options.cwd ?? process.cwd());
@@ -153,12 +164,20 @@ function resolveCleanProjectRoot(cwd: string, tsconfig?: string): string {
153
164
 
154
165
  function formatProjectPath(cwd: string, target: string): string {
155
166
  const relative = path.relative(cwd, target);
156
- if (!relative || relative.startsWith("..") || path.isAbsolute(relative)) {
167
+ if (!relative || isOutsideRelativePath(relative)) {
157
168
  return target;
158
169
  }
159
170
  return relative;
160
171
  }
161
172
 
173
+ function isOutsideRelativePath(relative: string): boolean {
174
+ return (
175
+ relative === ".." ||
176
+ relative.startsWith(`..${path.sep}`) ||
177
+ path.isAbsolute(relative)
178
+ );
179
+ }
180
+
162
181
  function delegateToNative(argv: readonly string[]): number {
163
182
  const bin = resolveBinary();
164
183
  if (!bin) {
@@ -498,6 +517,13 @@ function runWatch(
498
517
  return 0;
499
518
  }
500
519
 
520
+ function resolveCacheDir(cwd: string, cacheDir?: string): string | undefined {
521
+ if (!cacheDir) {
522
+ return undefined;
523
+ }
524
+ return path.isAbsolute(cacheDir) ? cacheDir : path.resolve(cwd, cacheDir);
525
+ }
526
+
501
527
  function collectWatchDirectories(root: string): string[] {
502
528
  const out: string[] = [];
503
529
  const stack: string[] = [root];