ttsc 0.6.0 → 0.7.1

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 (271) hide show
  1. package/cmd/platform/main.go +1 -1
  2. package/cmd/ttsc/api_compile.go +134 -0
  3. package/cmd/ttsc/api_transform.go +64 -0
  4. package/cmd/ttsc/build.go +1 -7
  5. package/cmd/ttsc/main.go +6 -20
  6. package/driver/program.go +13 -3
  7. package/lib/TtscCompiler.d.ts +96 -0
  8. package/lib/TtscCompiler.js +294 -0
  9. package/lib/TtscCompiler.js.map +1 -0
  10. package/lib/compiler/internal/buildNativeCompiler.d.ts +6 -0
  11. package/lib/compiler/internal/buildNativeCompiler.js +72 -0
  12. package/lib/compiler/internal/buildNativeCompiler.js.map +1 -0
  13. package/lib/compiler/internal/compileProjectInMemory.d.ts +7 -0
  14. package/lib/compiler/internal/compileProjectInMemory.js +176 -0
  15. package/lib/compiler/internal/compileProjectInMemory.js.map +1 -0
  16. package/lib/compiler/internal/project/readProjectConfig.d.ts +4 -0
  17. package/lib/{project.js → compiler/internal/project/readProjectConfig.js} +12 -82
  18. package/lib/compiler/internal/project/readProjectConfig.js.map +1 -0
  19. package/lib/compiler/internal/project/resolveProjectConfig.d.ts +3 -0
  20. package/lib/compiler/internal/project/resolveProjectConfig.js +94 -0
  21. package/lib/compiler/internal/project/resolveProjectConfig.js.map +1 -0
  22. package/lib/compiler/internal/resolveBinary.d.ts +4 -0
  23. package/lib/compiler/internal/resolveBinary.js +65 -0
  24. package/lib/compiler/internal/resolveBinary.js.map +1 -0
  25. package/lib/compiler/internal/resolveEmittedJavaScript.d.ts +7 -0
  26. package/lib/compiler/internal/resolveEmittedJavaScript.js +118 -0
  27. package/lib/compiler/internal/resolveEmittedJavaScript.js.map +1 -0
  28. package/lib/compiler/internal/resolveTsgo.d.ts +20 -0
  29. package/lib/{tsgo.js → compiler/internal/resolveTsgo.js} +5 -10
  30. package/lib/compiler/internal/resolveTsgo.js.map +1 -0
  31. package/lib/compiler/internal/runBuild.d.ts +10 -0
  32. package/lib/compiler/internal/runBuild.js +480 -0
  33. package/lib/compiler/internal/runBuild.js.map +1 -0
  34. package/lib/compiler/internal/runSingleFileEmit.d.ts +3 -0
  35. package/lib/compiler/internal/runSingleFileEmit.js +102 -0
  36. package/lib/compiler/internal/runSingleFileEmit.js.map +1 -0
  37. package/lib/compiler/internal/transformProjectInMemory.d.ts +7 -0
  38. package/lib/compiler/internal/transformProjectInMemory.js +275 -0
  39. package/lib/compiler/internal/transformProjectInMemory.js.map +1 -0
  40. package/lib/index.d.ts +6 -15
  41. package/lib/index.js +6 -15
  42. package/lib/index.js.map +1 -1
  43. package/lib/launcher/internal/getCompilerVersionText.d.ts +3 -0
  44. package/lib/launcher/internal/getCompilerVersionText.js +71 -0
  45. package/lib/launcher/internal/getCompilerVersionText.js.map +1 -0
  46. package/lib/launcher/internal/prepareExecution.d.ts +10 -0
  47. package/lib/launcher/internal/prepareExecution.js +120 -0
  48. package/lib/launcher/internal/prepareExecution.js.map +1 -0
  49. package/lib/launcher/internal/runTtsc.d.ts +1 -0
  50. package/lib/{cli.js → launcher/internal/runTtsc.js} +45 -118
  51. package/lib/launcher/internal/runTtsc.js.map +1 -0
  52. package/lib/launcher/internal/runTtsx.d.ts +1 -0
  53. package/lib/{runner/cli.js → launcher/internal/runTtsx.js} +13 -35
  54. package/lib/launcher/internal/runTtsx.js.map +1 -0
  55. package/lib/launcher/ttsc.js +2 -2
  56. package/lib/launcher/ttsc.js.map +1 -1
  57. package/lib/launcher/ttsx.js +2 -2
  58. package/lib/launcher/ttsx.js.map +1 -1
  59. package/lib/plugin/internal/buildSourcePlugin.d.ts +12 -0
  60. package/lib/{source-build.js → plugin/internal/buildSourcePlugin.js} +77 -84
  61. package/lib/plugin/internal/buildSourcePlugin.js.map +1 -0
  62. package/lib/plugin/internal/loadProjectPlugins.d.ts +14 -0
  63. package/lib/{plugin.js → plugin/internal/loadProjectPlugins.js} +38 -76
  64. package/lib/plugin/internal/loadProjectPlugins.js.map +1 -0
  65. package/lib/structures/ITtscCompilerContext.d.ts +78 -0
  66. package/lib/structures/ITtscCompilerContext.js +3 -0
  67. package/lib/structures/ITtscCompilerContext.js.map +1 -0
  68. package/lib/structures/ITtscCompilerDiagnostic.d.ts +83 -0
  69. package/lib/structures/ITtscCompilerDiagnostic.js +3 -0
  70. package/lib/structures/ITtscCompilerDiagnostic.js.map +1 -0
  71. package/lib/structures/ITtscCompilerResult.d.ts +83 -0
  72. package/lib/structures/{ITtscCacheKeyInput.js → ITtscCompilerResult.js} +1 -1
  73. package/lib/structures/ITtscCompilerResult.js.map +1 -0
  74. package/lib/structures/ITtscCompilerTransformation.d.ts +35 -0
  75. package/lib/structures/ITtscCompilerTransformation.js +3 -0
  76. package/lib/structures/ITtscCompilerTransformation.js.map +1 -0
  77. package/lib/structures/ITtscPlugin.d.ts +52 -7
  78. package/lib/structures/ITtscPluginFactoryContext.d.ts +47 -0
  79. package/lib/structures/ITtscProjectPluginConfig.d.ts +41 -0
  80. package/lib/structures/TtscPluginStage.d.ts +18 -0
  81. package/lib/structures/{ITtscBuildResult.js → TtscPluginStage.js} +1 -1
  82. package/lib/structures/TtscPluginStage.js.map +1 -0
  83. package/lib/structures/index.d.ts +8 -0
  84. package/lib/structures/index.js +25 -0
  85. package/lib/structures/index.js.map +1 -0
  86. package/lib/structures/internal/ITtscLoadedNativePlugin.d.ts +13 -0
  87. package/lib/structures/internal/ITtscLoadedNativePlugin.js.map +1 -0
  88. package/lib/structures/internal/ITtscParsedProjectConfig.d.ts +15 -0
  89. package/lib/structures/internal/ITtscParsedProjectConfig.js.map +1 -0
  90. package/lib/structures/internal/ITtscProjectLocatorOptions.d.ts +9 -0
  91. package/lib/structures/internal/ITtscProjectLocatorOptions.js.map +1 -0
  92. package/lib/structures/internal/TtscBuildOptions.d.ts +18 -0
  93. package/lib/structures/{ITtscBuildOptions.js → internal/TtscBuildOptions.js} +1 -1
  94. package/lib/structures/internal/TtscBuildOptions.js.map +1 -0
  95. package/lib/structures/internal/TtscBuildResult.d.ts +14 -0
  96. package/lib/structures/{ITtscCheckOptions.js → internal/TtscBuildResult.js} +1 -1
  97. package/lib/structures/internal/TtscBuildResult.js.map +1 -0
  98. package/lib/structures/internal/TtscCommonOptions.d.ts +27 -0
  99. package/lib/structures/{ITtscNativeSource.js → internal/TtscCommonOptions.js} +1 -1
  100. package/lib/structures/internal/TtscCommonOptions.js.map +1 -0
  101. package/lib/structures/internal/TtscSingleFileEmitOptions.d.ts +14 -0
  102. package/lib/structures/internal/TtscSingleFileEmitOptions.js +3 -0
  103. package/lib/structures/internal/TtscSingleFileEmitOptions.js.map +1 -0
  104. package/package.json +9 -9
  105. package/src/TtscCompiler.ts +305 -0
  106. package/src/compiler/internal/buildNativeCompiler.ts +43 -0
  107. package/src/compiler/internal/compileProjectInMemory.ts +189 -0
  108. package/src/{project.ts → compiler/internal/project/readProjectConfig.ts} +36 -111
  109. package/src/compiler/internal/project/resolveProjectConfig.ts +67 -0
  110. package/src/compiler/internal/resolveBinary.ts +41 -0
  111. package/src/compiler/internal/resolveEmittedJavaScript.ts +102 -0
  112. package/src/{tsgo.ts → compiler/internal/resolveTsgo.ts} +20 -23
  113. package/src/compiler/internal/runBuild.ts +644 -0
  114. package/src/compiler/internal/runSingleFileEmit.ts +73 -0
  115. package/src/compiler/internal/transformProjectInMemory.ts +368 -0
  116. package/src/index.ts +6 -15
  117. package/src/launcher/internal/getCompilerVersionText.ts +44 -0
  118. package/src/launcher/internal/prepareExecution.ts +115 -0
  119. package/src/{cli.ts → launcher/internal/runTtsc.ts} +58 -140
  120. package/src/{runner/cli.ts → launcher/internal/runTtsx.ts} +21 -49
  121. package/src/launcher/ttsc.ts +2 -2
  122. package/src/launcher/ttsx.ts +2 -2
  123. package/src/{source-build.ts → plugin/internal/buildSourcePlugin.ts} +121 -115
  124. package/src/plugin/internal/loadProjectPlugins.ts +214 -0
  125. package/src/structures/ITtscCompilerContext.ts +84 -0
  126. package/src/structures/ITtscCompilerDiagnostic.ts +91 -0
  127. package/src/structures/ITtscCompilerResult.ts +94 -0
  128. package/src/structures/ITtscCompilerTransformation.ts +46 -0
  129. package/src/structures/ITtscPlugin.ts +54 -7
  130. package/src/structures/ITtscPluginFactoryContext.ts +51 -0
  131. package/src/structures/ITtscProjectPluginConfig.ts +43 -0
  132. package/src/structures/TtscPluginStage.ts +18 -0
  133. package/src/structures/index.ts +8 -0
  134. package/src/structures/internal/ITtscLoadedNativePlugin.ts +14 -0
  135. package/src/structures/internal/ITtscParsedProjectConfig.ts +16 -0
  136. package/src/structures/internal/ITtscProjectLocatorOptions.ts +9 -0
  137. package/src/structures/internal/TtscBuildOptions.ts +19 -0
  138. package/src/structures/internal/TtscBuildResult.ts +15 -0
  139. package/src/structures/internal/TtscCommonOptions.ts +28 -0
  140. package/src/structures/internal/TtscSingleFileEmitOptions.ts +15 -0
  141. package/test/compiler.test.ts +422 -0
  142. package/test/platform.test.ts +1 -45
  143. package/test/project.test.ts +3 -1
  144. package/test/source-build.test.ts +33 -220
  145. package/test/tsgo.test.ts +1 -8
  146. package/cmd/ttsc/transform.go +0 -166
  147. package/lib/api.d.ts +0 -40
  148. package/lib/api.js +0 -680
  149. package/lib/api.js.map +0 -1
  150. package/lib/cli.d.ts +0 -1
  151. package/lib/cli.js.map +0 -1
  152. package/lib/native.d.ts +0 -8
  153. package/lib/native.js +0 -42
  154. package/lib/native.js.map +0 -1
  155. package/lib/platform.d.ts +0 -52
  156. package/lib/platform.js +0 -175
  157. package/lib/platform.js.map +0 -1
  158. package/lib/plugin.d.ts +0 -13
  159. package/lib/plugin.js.map +0 -1
  160. package/lib/project.d.ts +0 -14
  161. package/lib/project.js.map +0 -1
  162. package/lib/runner/cli.d.ts +0 -1
  163. package/lib/runner/cli.js.map +0 -1
  164. package/lib/runner/register.d.ts +0 -6
  165. package/lib/runner/register.js +0 -358
  166. package/lib/runner/register.js.map +0 -1
  167. package/lib/source-build.d.ts +0 -19
  168. package/lib/source-build.js.map +0 -1
  169. package/lib/structures/ITtscBuildOptions.d.ts +0 -14
  170. package/lib/structures/ITtscBuildOptions.js.map +0 -1
  171. package/lib/structures/ITtscBuildResult.d.ts +0 -6
  172. package/lib/structures/ITtscBuildResult.js.map +0 -1
  173. package/lib/structures/ITtscBuildSourcePluginOptions.d.ts +0 -8
  174. package/lib/structures/ITtscBuildSourcePluginOptions.js +0 -3
  175. package/lib/structures/ITtscBuildSourcePluginOptions.js.map +0 -1
  176. package/lib/structures/ITtscCacheKeyInput.d.ts +0 -7
  177. package/lib/structures/ITtscCacheKeyInput.js.map +0 -1
  178. package/lib/structures/ITtscCheckOptions.d.ts +0 -2
  179. package/lib/structures/ITtscCheckOptions.js.map +0 -1
  180. package/lib/structures/ITtscCommonOptions.d.ts +0 -22
  181. package/lib/structures/ITtscCommonOptions.js +0 -3
  182. package/lib/structures/ITtscCommonOptions.js.map +0 -1
  183. package/lib/structures/ITtscLoadPluginsOptions.d.ts +0 -8
  184. package/lib/structures/ITtscLoadPluginsOptions.js +0 -3
  185. package/lib/structures/ITtscLoadPluginsOptions.js.map +0 -1
  186. package/lib/structures/ITtscLoadedNativePlugin.d.ts +0 -7
  187. package/lib/structures/ITtscLoadedNativePlugin.js.map +0 -1
  188. package/lib/structures/ITtscLoadedPlugins.d.ts +0 -11
  189. package/lib/structures/ITtscLoadedPlugins.js +0 -3
  190. package/lib/structures/ITtscLoadedPlugins.js.map +0 -1
  191. package/lib/structures/ITtscNativeBackend.d.ts +0 -10
  192. package/lib/structures/ITtscNativeBackend.js +0 -3
  193. package/lib/structures/ITtscNativeBackend.js.map +0 -1
  194. package/lib/structures/ITtscNativePluginContractVersion.d.ts +0 -1
  195. package/lib/structures/ITtscNativePluginContractVersion.js +0 -3
  196. package/lib/structures/ITtscNativePluginContractVersion.js.map +0 -1
  197. package/lib/structures/ITtscNativePluginShape.d.ts +0 -8
  198. package/lib/structures/ITtscNativePluginShape.js +0 -3
  199. package/lib/structures/ITtscNativePluginShape.js.map +0 -1
  200. package/lib/structures/ITtscNativeRewriteMode.d.ts +0 -1
  201. package/lib/structures/ITtscNativeRewriteMode.js +0 -3
  202. package/lib/structures/ITtscNativeRewriteMode.js.map +0 -1
  203. package/lib/structures/ITtscNativeSource.d.ts +0 -4
  204. package/lib/structures/ITtscNativeSource.js.map +0 -1
  205. package/lib/structures/ITtscParsedProjectConfig.d.ts +0 -9
  206. package/lib/structures/ITtscParsedProjectConfig.js.map +0 -1
  207. package/lib/structures/ITtscPluginFactory.d.ts +0 -4
  208. package/lib/structures/ITtscPluginFactory.js +0 -3
  209. package/lib/structures/ITtscPluginFactory.js.map +0 -1
  210. package/lib/structures/ITtscPluginModule.d.ts +0 -3
  211. package/lib/structures/ITtscPluginModule.js +0 -3
  212. package/lib/structures/ITtscPluginModule.js.map +0 -1
  213. package/lib/structures/ITtscPreparedExecution.d.ts +0 -5
  214. package/lib/structures/ITtscPreparedExecution.js +0 -3
  215. package/lib/structures/ITtscPreparedExecution.js.map +0 -1
  216. package/lib/structures/ITtscProjectLocatorOptions.d.ts +0 -5
  217. package/lib/structures/ITtscProjectLocatorOptions.js.map +0 -1
  218. package/lib/structures/ITtscRegisterOptions.d.ts +0 -6
  219. package/lib/structures/ITtscRegisterOptions.js +0 -3
  220. package/lib/structures/ITtscRegisterOptions.js.map +0 -1
  221. package/lib/structures/ITtscResolveGoBinaryOptions.d.ts +0 -7
  222. package/lib/structures/ITtscResolveGoBinaryOptions.js +0 -3
  223. package/lib/structures/ITtscResolveGoBinaryOptions.js.map +0 -1
  224. package/lib/structures/ITtscResolveOptions.d.ts +0 -20
  225. package/lib/structures/ITtscResolveOptions.js +0 -3
  226. package/lib/structures/ITtscResolveOptions.js.map +0 -1
  227. package/lib/structures/ITtscResolvedTsgo.d.ts +0 -8
  228. package/lib/structures/ITtscResolvedTsgo.js +0 -3
  229. package/lib/structures/ITtscResolvedTsgo.js.map +0 -1
  230. package/lib/structures/ITtscTransformOptions.d.ts +0 -12
  231. package/lib/structures/ITtscTransformOptions.js +0 -3
  232. package/lib/structures/ITtscTransformOptions.js.map +0 -1
  233. package/lib/structures/ITtscTsgoResolveOptions.d.ts +0 -14
  234. package/lib/structures/ITtscTsgoResolveOptions.js +0 -3
  235. package/lib/structures/ITtscTsgoResolveOptions.js.map +0 -1
  236. package/lib/tsgo.d.ts +0 -6
  237. package/lib/tsgo.js.map +0 -1
  238. package/src/api.ts +0 -872
  239. package/src/native.ts +0 -64
  240. package/src/platform.ts +0 -151
  241. package/src/plugin.ts +0 -253
  242. package/src/runner/register.ts +0 -406
  243. package/src/structures/ITtscBuildOptions.ts +0 -19
  244. package/src/structures/ITtscBuildResult.ts +0 -6
  245. package/src/structures/ITtscBuildSourcePluginOptions.ts +0 -9
  246. package/src/structures/ITtscCacheKeyInput.ts +0 -7
  247. package/src/structures/ITtscCheckOptions.ts +0 -3
  248. package/src/structures/ITtscCommonOptions.ts +0 -23
  249. package/src/structures/ITtscLoadPluginsOptions.ts +0 -9
  250. package/src/structures/ITtscLoadedNativePlugin.ts +0 -8
  251. package/src/structures/ITtscLoadedPlugins.ts +0 -12
  252. package/src/structures/ITtscNativeBackend.ts +0 -11
  253. package/src/structures/ITtscNativePluginContractVersion.ts +0 -1
  254. package/src/structures/ITtscNativePluginShape.ts +0 -9
  255. package/src/structures/ITtscNativeRewriteMode.ts +0 -1
  256. package/src/structures/ITtscNativeSource.ts +0 -4
  257. package/src/structures/ITtscParsedProjectConfig.ts +0 -10
  258. package/src/structures/ITtscPluginFactory.ts +0 -7
  259. package/src/structures/ITtscPluginModule.ts +0 -4
  260. package/src/structures/ITtscPreparedExecution.ts +0 -5
  261. package/src/structures/ITtscProjectLocatorOptions.ts +0 -5
  262. package/src/structures/ITtscRegisterOptions.ts +0 -7
  263. package/src/structures/ITtscResolveGoBinaryOptions.ts +0 -7
  264. package/src/structures/ITtscResolveOptions.ts +0 -20
  265. package/src/structures/ITtscResolvedTsgo.ts +0 -8
  266. package/src/structures/ITtscTransformOptions.ts +0 -13
  267. package/src/structures/ITtscTsgoResolveOptions.ts +0 -14
  268. package/test/native.test.ts +0 -103
  269. /package/lib/structures/{ITtscLoadedNativePlugin.js → internal/ITtscLoadedNativePlugin.js} +0 -0
  270. /package/lib/structures/{ITtscParsedProjectConfig.js → internal/ITtscParsedProjectConfig.js} +0 -0
  271. /package/lib/structures/{ITtscProjectLocatorOptions.js → internal/ITtscProjectLocatorOptions.js} +0 -0
@@ -0,0 +1,422 @@
1
+ const assert = require("node:assert/strict");
2
+ const fs = require("node:fs");
3
+ const os = require("node:os");
4
+ const path = require("node:path");
5
+ const test = require("node:test");
6
+
7
+ const { TtscCompiler } = require("../lib/index.js");
8
+ const { resolveTsgo } = require("../lib/compiler/internal/resolveTsgo.js");
9
+ const tsgo = resolveTsgo({ cwd: path.resolve(__dirname, "..") }).binary;
10
+
11
+ test("TtscCompiler.compile returns output without writing project files", () => {
12
+ const root = createProject();
13
+ const compiler = new TtscCompiler({
14
+ binary: tsgo,
15
+ cwd: root,
16
+ plugins: false,
17
+ });
18
+
19
+ const result = compiler.compile();
20
+
21
+ assert.equal(result.type, "success");
22
+ assert.match(result.output["dist/main.js"], /api-ok/);
23
+ assert.match(result.output["dist/main.js"], /console\.log\(\s*message\s*\)/);
24
+ assert.match(result.output["dist/main.d.ts"], /declare const message/);
25
+ assert.match(result.output["dist/main.js.map"], /"version":3/);
26
+ assert.match(result.output["dist/main.d.ts.map"], /"version":3/);
27
+ assert.equal(fs.existsSync(path.join(root, "dist")), false);
28
+ });
29
+
30
+ test("TtscCompiler can disable project plugin loading", () => {
31
+ const root = createProject({
32
+ plugins: [{ transform: "./missing-plugin.cjs" }],
33
+ });
34
+ const compiler = new TtscCompiler({
35
+ binary: tsgo,
36
+ cwd: root,
37
+ plugins: false,
38
+ });
39
+
40
+ const result = compiler.compile();
41
+
42
+ assert.equal(result.type, "success");
43
+ });
44
+
45
+ test("TtscCompiler.compile applies configured source plugins without project output", () => {
46
+ const root = createProject({
47
+ plugins: [{ transform: "./plugin.cjs" }],
48
+ source: 'export const value = goUpper("plugin");\nconsole.log(value);\n',
49
+ });
50
+ writeCompilerPlugin(root);
51
+ const compiler = new TtscCompiler({ binary: tsgo, cwd: root });
52
+
53
+ const result = compiler.compile();
54
+
55
+ assert.equal(result.type, "success");
56
+ assert.match(result.output["dist/main.js"], /PLUGIN/);
57
+ assert.equal(fs.existsSync(path.join(root, "dist")), false);
58
+ });
59
+
60
+ test("TtscCompiler.transform returns TypeScript source without project files", () => {
61
+ const root = createProject();
62
+ const compiler = new TtscCompiler({
63
+ binary: tsgo,
64
+ cwd: root,
65
+ plugins: false,
66
+ });
67
+
68
+ const result = compiler.transform();
69
+
70
+ assert.equal(result.type, "success");
71
+ assert.match(
72
+ result.typescript["src/main.ts"],
73
+ /const message: string = "api-ok"/,
74
+ );
75
+ assert.match(
76
+ result.typescript["src/main.ts"],
77
+ /console\.log\(\s*message\s*\)/,
78
+ );
79
+ assert.equal(result.typescript["dist/main.js"], undefined);
80
+ assert.equal(result.typescript["dist/main.d.ts"], undefined);
81
+ assert.equal(fs.existsSync(path.join(root, "dist")), false);
82
+ });
83
+
84
+ test("TtscCompiler.transform returns every included TypeScript source file", () => {
85
+ const root = createProject({
86
+ files: {
87
+ "src/helpers.ts":
88
+ "export const helper = (value: string): string => value.toUpperCase();\n",
89
+ "src/nested/model.ts": "export interface Model { value: string }\n",
90
+ },
91
+ source:
92
+ 'import { helper } from "./helpers";\nconst message: string = helper("api-ok");\nconsole.log(message);\n',
93
+ });
94
+ const compiler = new TtscCompiler({
95
+ binary: tsgo,
96
+ cwd: root,
97
+ plugins: false,
98
+ });
99
+
100
+ const result = compiler.transform();
101
+
102
+ assert.equal(result.type, "success");
103
+ assert.match(result.typescript["src/main.ts"], /helper\("api-ok"\)/);
104
+ assert.match(result.typescript["src/helpers.ts"], /toUpperCase/);
105
+ assert.match(result.typescript["src/nested/model.ts"], /interface Model/);
106
+ for (const key of Object.keys(result.typescript)) {
107
+ assert.equal(key.startsWith("dist/"), false);
108
+ assert.equal(/\.(?:js|cjs|mjs|d\.ts|map)$/.test(key), false);
109
+ }
110
+ assert.equal(fs.existsSync(path.join(root, "dist")), false);
111
+ });
112
+
113
+ test("TtscCompiler.transform applies configured source plugins to TypeScript output", () => {
114
+ const root = createProject({
115
+ plugins: [{ transform: "./plugin.cjs" }],
116
+ source: 'export const value = goUpper("plugin");\nconsole.log(value);\n',
117
+ });
118
+ writeCompilerPlugin(root);
119
+ const compiler = new TtscCompiler({ binary: tsgo, cwd: root });
120
+
121
+ const result = compiler.transform();
122
+
123
+ assert.equal(result.type, "success");
124
+ assert.match(
125
+ result.typescript["src/main.ts"],
126
+ /export const value = "PLUGIN"/,
127
+ );
128
+ assert.match(result.typescript["src/main.ts"], /console\.log\(value\)/);
129
+ assert.equal(result.typescript["dist/main.js"], undefined);
130
+ assert.equal(fs.existsSync(path.join(root, "dist")), false);
131
+ });
132
+
133
+ test("TtscCompiler.transform rejects plugin output that is not TypeScript source", () => {
134
+ const root = createProject({
135
+ plugins: [{ transform: "./plugin.cjs" }],
136
+ source: 'export const value = goUpper("plugin");\nconsole.log(value);\n',
137
+ });
138
+ writeBrokenTransformPlugin(root);
139
+ const compiler = new TtscCompiler({ binary: tsgo, cwd: root });
140
+
141
+ const result = compiler.transform();
142
+
143
+ assert.equal(result.type, "exception");
144
+ assert.match(result.error.message, /did not return a TypeScript source map/);
145
+ assert.equal(fs.existsSync(path.join(root, "dist")), false);
146
+ });
147
+
148
+ test("TtscCompiler.transform returns failure on compiler diagnostics", () => {
149
+ const root = createProject({
150
+ source: 'const value: number = "not-a-number";\nconsole.log(value);\n',
151
+ });
152
+ const compiler = new TtscCompiler({
153
+ binary: tsgo,
154
+ cwd: root,
155
+ plugins: false,
156
+ });
157
+
158
+ const result = compiler.transform();
159
+
160
+ assert.equal(result.type, "failure");
161
+ assert.equal(result.diagnostics[0].code, 2322);
162
+ assert.match(result.typescript["src/main.ts"], /not-a-number/);
163
+ assert.equal(fs.existsSync(path.join(root, "dist")), false);
164
+ });
165
+
166
+ test("TtscCompiler.compile returns structured diagnostics", () => {
167
+ const root = createProject({
168
+ source: 'const value: number = "not-a-number";\nconsole.log(value);\n',
169
+ });
170
+ const compiler = new TtscCompiler({
171
+ binary: tsgo,
172
+ cwd: root,
173
+ plugins: false,
174
+ });
175
+
176
+ const result = compiler.compile();
177
+
178
+ assert.equal(result.type, "failure");
179
+ assert.equal(result.diagnostics.length, 1);
180
+ assert.equal(result.diagnostics[0].category, "error");
181
+ assert.equal(result.diagnostics[0].code, 2322);
182
+ assert.equal(typeof result.diagnostics[0].start, "number");
183
+ assert.equal(typeof result.diagnostics[0].length, "number");
184
+ assert.equal(result.diagnostics[0].line, 1);
185
+ assert.equal(result.diagnostics[0].character, 7);
186
+ assert.equal(result.diagnostics[0].file.endsWith("src/main.ts"), true);
187
+ assert.match(result.diagnostics[0].messageText, /not assignable/);
188
+ assert.equal(typeof result.output, "object");
189
+ assert.equal(fs.existsSync(path.join(root, "dist")), false);
190
+ });
191
+
192
+ test("TtscCompiler.compile does not accept per-call context overrides", () => {
193
+ const root = createProject();
194
+ const other = createProject({
195
+ plugins: [{ transform: "./missing-plugin.cjs" }],
196
+ });
197
+ const compiler = new TtscCompiler({
198
+ binary: tsgo,
199
+ cwd: root,
200
+ plugins: false,
201
+ });
202
+
203
+ const result = compiler.compile({
204
+ binary: path.join(other, "missing-tsgo"),
205
+ cwd: other,
206
+ plugins: [{ transform: "./missing-plugin.cjs" }],
207
+ });
208
+
209
+ assert.equal(result.type, "success");
210
+ assert.match(result.output["dist/main.js"], /api-ok/);
211
+ assert.equal(fs.existsSync(path.join(root, "dist")), false);
212
+ assert.equal(fs.existsSync(path.join(other, "dist")), false);
213
+ });
214
+
215
+ test("TtscCompiler.prepare builds source plugins and clean removes context cache", () => {
216
+ const root = createProject({
217
+ plugins: [{ transform: "./plugin.cjs" }],
218
+ });
219
+ writeSourcePlugin(root);
220
+ const cacheDir = path.join(root, ".cache", "ttsc");
221
+ const compiler = new TtscCompiler({ binary: tsgo, cacheDir, cwd: root });
222
+
223
+ const prepared = compiler.prepare();
224
+
225
+ assert.equal(prepared.length, 1);
226
+ assert.equal(fs.existsSync(prepared[0]), true);
227
+ assert.equal(prepared[0].startsWith(path.join(cacheDir, "plugins")), true);
228
+
229
+ const removed = compiler.clean();
230
+
231
+ assert.deepEqual(removed, [cacheDir]);
232
+ assert.equal(fs.existsSync(cacheDir), false);
233
+ });
234
+
235
+ function createProject(options = {}) {
236
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), "ttsc-compiler-api-"));
237
+ fs.mkdirSync(path.join(root, "src"), { recursive: true });
238
+ fs.writeFileSync(
239
+ path.join(root, "src", "main.ts"),
240
+ options.source ??
241
+ 'const message: string = "api-ok";\nconsole.log(message);\n',
242
+ "utf8",
243
+ );
244
+ for (const [file, content] of Object.entries(options.files ?? {})) {
245
+ const location = path.join(root, file);
246
+ fs.mkdirSync(path.dirname(location), { recursive: true });
247
+ fs.writeFileSync(location, content, "utf8");
248
+ }
249
+ fs.writeFileSync(
250
+ path.join(root, "tsconfig.json"),
251
+ JSON.stringify(
252
+ {
253
+ compilerOptions: {
254
+ target: "ES2022",
255
+ module: "commonjs",
256
+ strict: true,
257
+ outDir: "dist",
258
+ declaration: true,
259
+ declarationMap: true,
260
+ rootDir: "src",
261
+ sourceMap: true,
262
+ plugins: options.plugins,
263
+ },
264
+ include: ["src"],
265
+ },
266
+ null,
267
+ 2,
268
+ ),
269
+ "utf8",
270
+ );
271
+ return root;
272
+ }
273
+
274
+ function writeSourcePlugin(root) {
275
+ fs.writeFileSync(
276
+ path.join(root, "plugin.cjs"),
277
+ 'module.exports = { name: "prepare-fixture", source: "./plugin-go" };\n',
278
+ "utf8",
279
+ );
280
+ fs.mkdirSync(path.join(root, "plugin-go"), { recursive: true });
281
+ fs.writeFileSync(
282
+ path.join(root, "plugin-go", "go.mod"),
283
+ "module example.com/preparefixture\n\ngo 1.26\n",
284
+ "utf8",
285
+ );
286
+ fs.writeFileSync(
287
+ path.join(root, "plugin-go", "main.go"),
288
+ "package main\n\nfunc main() {}\n",
289
+ "utf8",
290
+ );
291
+ }
292
+
293
+ function writeBrokenTransformPlugin(root) {
294
+ fs.writeFileSync(
295
+ path.join(root, "plugin.cjs"),
296
+ 'module.exports = { name: "broken-transform-fixture", source: "./plugin-go" };\n',
297
+ "utf8",
298
+ );
299
+ fs.mkdirSync(path.join(root, "plugin-go"), { recursive: true });
300
+ fs.writeFileSync(
301
+ path.join(root, "plugin-go", "go.mod"),
302
+ "module example.com/brokentransformfixture\n\ngo 1.26\n",
303
+ "utf8",
304
+ );
305
+ fs.writeFileSync(
306
+ path.join(root, "plugin-go", "main.go"),
307
+ [
308
+ "package main",
309
+ "",
310
+ "import (",
311
+ '\t"fmt"',
312
+ '\t"os"',
313
+ ")",
314
+ "",
315
+ "func main() {",
316
+ '\tif len(os.Args) > 1 && os.Args[1] == "transform" {',
317
+ '\t\tfmt.Println(`{"output":{"dist/main.js":"console.log(\\"wrong\\");\\n"}}`)',
318
+ "\t\treturn",
319
+ "\t}",
320
+ "}",
321
+ "",
322
+ ].join("\n"),
323
+ "utf8",
324
+ );
325
+ }
326
+
327
+ function writeCompilerPlugin(root) {
328
+ fs.writeFileSync(
329
+ path.join(root, "plugin.cjs"),
330
+ 'module.exports = { name: "compile-fixture", source: "./plugin-go" };\n',
331
+ "utf8",
332
+ );
333
+ fs.mkdirSync(path.join(root, "plugin-go"), { recursive: true });
334
+ fs.writeFileSync(
335
+ path.join(root, "plugin-go", "go.mod"),
336
+ "module example.com/compilefixture\n\ngo 1.26\n",
337
+ "utf8",
338
+ );
339
+ fs.writeFileSync(
340
+ path.join(root, "plugin-go", "main.go"),
341
+ [
342
+ "package main",
343
+ "",
344
+ "import (",
345
+ '\t"encoding/json"',
346
+ '\t"flag"',
347
+ '\t"fmt"',
348
+ '\t"os"',
349
+ '\t"path/filepath"',
350
+ '\t"strings"',
351
+ ")",
352
+ "",
353
+ "func main() { os.Exit(run(os.Args[1:])) }",
354
+ "",
355
+ "func run(args []string) int {",
356
+ "\tif len(args) == 0 { return 2 }",
357
+ "\tswitch args[0] {",
358
+ '\tcase "build":',
359
+ "\t\treturn build(args[1:])",
360
+ '\tcase "transform":',
361
+ "\t\treturn transformSource(args[1:])",
362
+ '\tcase "check", "version":',
363
+ "\t\treturn 0",
364
+ "\tdefault:",
365
+ "\t\treturn 2",
366
+ "\t}",
367
+ "}",
368
+ "",
369
+ "func build(args []string) int {",
370
+ '\tfs := flag.NewFlagSet("build", flag.ContinueOnError)',
371
+ "\tfs.SetOutput(os.Stderr)",
372
+ '\tcwd := fs.String("cwd", "", "")',
373
+ '\toutDir := fs.String("outDir", "dist", "")',
374
+ '\t_ = fs.String("tsconfig", "", "")',
375
+ '\t_ = fs.String("plugins-json", "", "")',
376
+ '\t_ = fs.Bool("emit", false, "")',
377
+ '\t_ = fs.Bool("quiet", false, "")',
378
+ '\t_ = fs.Bool("verbose", false, "")',
379
+ '\t_ = fs.Bool("noEmit", false, "")',
380
+ "\tif err := fs.Parse(args); err != nil { return 2 }",
381
+ "\troot := *cwd",
382
+ '\tif root == "" { root, _ = os.Getwd() }',
383
+ '\tinput, err := os.ReadFile(filepath.Join(root, "src", "main.ts"))',
384
+ "\tif err != nil { fmt.Fprintln(os.Stderr, err); return 2 }",
385
+ '\tvalue := "PLUGIN"',
386
+ '\tif !strings.Contains(string(input), `goUpper("plugin")`) { value = "UNKNOWN" }',
387
+ '\toutput := fmt.Sprintf("\\"use strict\\";\\nObject.defineProperty(exports, \\"__esModule\\", { value: true });\\nexports.value = void 0;\\nconst value = %q;\\nexports.value = value;\\nconsole.log(value);\\n", value)',
388
+ '\tfile := filepath.Join(*outDir, "main.js")',
389
+ '\tif !filepath.IsAbs(*outDir) { file = filepath.Join(root, *outDir, "main.js") }',
390
+ "\tif err := os.MkdirAll(filepath.Dir(file), 0o755); err != nil { fmt.Fprintln(os.Stderr, err); return 2 }",
391
+ "\tif err := os.WriteFile(file, []byte(output), 0o644); err != nil { fmt.Fprintln(os.Stderr, err); return 2 }",
392
+ "\treturn 0",
393
+ "}",
394
+ "",
395
+ "type transformResult struct {",
396
+ '\tTypeScript map[string]string `json:"typescript"`',
397
+ "}",
398
+ "",
399
+ "func transformSource(args []string) int {",
400
+ '\tfs := flag.NewFlagSet("transform", flag.ContinueOnError)',
401
+ "\tfs.SetOutput(os.Stderr)",
402
+ '\tcwd := fs.String("cwd", "", "")',
403
+ '\t_ = fs.String("tsconfig", "", "")',
404
+ '\t_ = fs.String("plugins-json", "", "")',
405
+ "\tif err := fs.Parse(args); err != nil { return 2 }",
406
+ "\troot := *cwd",
407
+ '\tif root == "" { root, _ = os.Getwd() }',
408
+ '\tinput, err := os.ReadFile(filepath.Join(root, "src", "main.ts"))',
409
+ "\tif err != nil { fmt.Fprintln(os.Stderr, err); return 2 }",
410
+ '\tvalue := "PLUGIN"',
411
+ '\tif !strings.Contains(string(input), `goUpper("plugin")`) { value = "UNKNOWN" }',
412
+ '\toutput := fmt.Sprintf("export const value = %q;\\nconsole.log(value);\\n", value)',
413
+ '\tdata, err := json.Marshal(transformResult{TypeScript: map[string]string{"src/main.ts": output}})',
414
+ "\tif err != nil { fmt.Fprintln(os.Stderr, err); return 2 }",
415
+ "\tfmt.Fprintln(os.Stdout, string(data))",
416
+ "\treturn 0",
417
+ "}",
418
+ "",
419
+ ].join("\n"),
420
+ "utf8",
421
+ );
422
+ }
@@ -1,58 +1,14 @@
1
1
  const assert = require("node:assert/strict");
2
2
  const test = require("node:test");
3
3
  const {
4
- binaryName,
5
- installHint,
6
- platformPackageRequest,
7
4
  resolveBinary,
8
- } = require("../src/platform.ts");
5
+ } = require("../lib/compiler/internal/resolveBinary.js");
9
6
 
10
7
  test("resolveBinary prefers TTSC_BINARY absolute override", () => {
11
8
  const resolved = resolveBinary({
12
9
  env: {
13
10
  TTSC_BINARY: "/tmp/custom-ttsc",
14
11
  },
15
- localBinaryLookup: () => "/tmp/local-ttsc",
16
- resolver: () => {
17
- throw new Error("resolver should not be called when TTSC_BINARY is set");
18
- },
19
12
  });
20
13
  assert.equal(resolved, "/tmp/custom-ttsc");
21
14
  });
22
-
23
- test("resolveBinary falls back to local binary when optional package is missing", () => {
24
- const resolved = resolveBinary({
25
- env: {},
26
- localBinaryLookup: () => "/tmp/local-ttsc",
27
- resolver: () => {
28
- throw new Error("optional package missing");
29
- },
30
- });
31
- assert.equal(resolved, "/tmp/local-ttsc");
32
- });
33
-
34
- test("resolveBinary returns null when only published and local binaries are missing", () => {
35
- const resolved = resolveBinary({
36
- env: {},
37
- localBinaryLookup: () => null,
38
- moduleDir: `${process.cwd()}/src`,
39
- resolver: () => {
40
- throw new Error("optional package missing");
41
- },
42
- });
43
- assert.equal(resolved, null);
44
- });
45
-
46
- test("platform helpers describe the published package contract", () => {
47
- assert.equal(binaryName({ platform: "win32" }), "ttsc.exe");
48
- assert.equal(binaryName({ platform: "linux" }), "ttsc");
49
- assert.equal(
50
- platformPackageRequest({ platform: "linux", arch: "x64" }),
51
- "@ttsc/linux-x64/bin/ttsc",
52
- );
53
-
54
- const hint = installHint({ platform: "linux", arch: "x64" });
55
- assert.match(hint, /@ttsc\/linux-x64/);
56
- assert.match(hint, /TTSC_BINARY env var/);
57
- assert.doesNotMatch(hint, /ttsc-dev\.js/);
58
- });
@@ -6,8 +6,10 @@ const test = require("node:test");
6
6
 
7
7
  const {
8
8
  readProjectConfig,
9
+ } = require("../lib/compiler/internal/project/readProjectConfig.js");
10
+ const {
9
11
  resolveProjectConfig,
10
- } = require("../src/project.ts");
12
+ } = require("../lib/compiler/internal/project/resolveProjectConfig.js");
11
13
 
12
14
  test("resolveProjectConfig canonicalizes symlinked tsconfig paths", () => {
13
15
  const root = fs.mkdtempSync(path.join(os.tmpdir(), "ttsc-project-"));