@typesol/cli 0.28.0 → 0.30.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 (48) hide show
  1. package/CHANGELOG.md +35 -0
  2. package/README.md +2 -2
  3. package/dist/cli.js +13 -20
  4. package/dist/compiler/analyze.d.ts +6 -2
  5. package/dist/compiler/analyze.js +57 -15
  6. package/dist/compiler/diagnostics.d.ts +2 -0
  7. package/dist/compiler/diagnostics.js +3 -2
  8. package/dist/compiler/emit/alias.d.ts +48 -0
  9. package/dist/compiler/emit/alias.js +166 -0
  10. package/dist/compiler/emit/borsh.js +6 -3
  11. package/dist/compiler/emit/calls.js +21 -4
  12. package/dist/compiler/emit/cpi-core.d.ts +70 -0
  13. package/dist/compiler/emit/cpi-core.js +356 -0
  14. package/dist/compiler/emit/cpi.d.ts +3 -177
  15. package/dist/compiler/emit/cpi.js +13 -2721
  16. package/dist/compiler/emit/create.d.ts +39 -0
  17. package/dist/compiler/emit/create.js +548 -0
  18. package/dist/compiler/emit/function.d.ts +11 -2
  19. package/dist/compiler/emit/function.js +42 -7
  20. package/dist/compiler/emit/metaplex.d.ts +78 -0
  21. package/dist/compiler/emit/metaplex.js +1345 -0
  22. package/dist/compiler/emit/program.js +24 -4
  23. package/dist/compiler/emit/shared.d.ts +7 -1
  24. package/dist/compiler/emit/shared.js +11 -2
  25. package/dist/compiler/emit/statements.d.ts +0 -9
  26. package/dist/compiler/emit/statements.js +2 -53
  27. package/dist/compiler/emit/storage.d.ts +5 -20
  28. package/dist/compiler/emit/storage.js +40 -106
  29. package/dist/compiler/emit/token.d.ts +8 -0
  30. package/dist/compiler/emit/token.js +493 -0
  31. package/dist/compiler/index.js +1 -1
  32. package/dist/compiler/model.d.ts +10 -2
  33. package/dist/compiler/model.js +7 -1
  34. package/dist/tools/portgen.js +3 -0
  35. package/dist/workspace.d.ts +1 -1
  36. package/dist/workspace.js +3 -1
  37. package/docs/architecture.md +30 -2
  38. package/docs/audit.md +3 -2
  39. package/docs/case-study-multisig.md +31 -29
  40. package/docs/cli.md +7 -4
  41. package/docs/errors.md +3 -2
  42. package/docs/language.md +4 -3
  43. package/docs/migrate-from-anchor.md +3 -3
  44. package/docs/security-model.md +1 -1
  45. package/lib/typesol.d.ts +17 -6
  46. package/package.json +1 -1
  47. package/runtime/typesol_rt.h +85 -1
  48. package/ts-plugin/index.js +75 -39
@@ -2,17 +2,22 @@
2
2
  //
3
3
  // programs/tsconfig.json: { "compilerOptions": { …, "plugins": [{ "name": "typesol/ts-plugin" }] } }
4
4
  //
5
- // For each program entry of the project (typesol.json), once TypeScript itself reports no errors, it runs
6
- // `typesol check --json --stdin` on the editor's text (unsaved changes included) and shows what the compiler refuses
7
- // (an unsupported construct, a missing account constraint…) at its line and column. Editors load tsconfig plugins
8
- // from the workspace's TypeScript (VS Code: "TypeScript: Select TypeScript Version" → "Use Workspace Version").
5
+ // Once TypeScript itself reports no errors in a file, the program entries of the project (typesol.json) are checked
6
+ // with the TypeSOL compiler on the editor's text (unsaved changes in every open file included), and what the compiler
7
+ // refuses (an unsupported construct, a constraint that cannot hold…) is shown where it is: in the entry or in any file
8
+ // it imports. The compiler runs in the editor's process once it has loaded (an ESM import, started when the plugin
9
+ // is created) and as `typesol check --json --files` until then. Editors load tsconfig plugins from the workspace's
10
+ // TypeScript (VS Code: "TypeScript: Select TypeScript Version" → "Use Workspace Version"; `typesol init` sets that up).
9
11
  // CommonJS on purpose: tsserver loads plugins with require().
10
12
  "use strict";
11
13
  const { execFileSync } = require("node:child_process");
14
+ const { createHash } = require("node:crypto");
12
15
  const fs = require("node:fs");
13
16
  const path = require("node:path");
17
+ const { pathToFileURL } = require("node:url");
14
18
 
15
- const CLI = path.join(__dirname, "..", "dist", "cli.js");
19
+ const DIST = path.join(__dirname, "..", "dist");
20
+ const CLI = path.join(DIST, "cli.js");
16
21
  const CODE = 9900;
17
22
 
18
23
  /** The program entry files of the typesol.json at or above `dir` (absolute), or an empty set. */
@@ -35,46 +40,62 @@ function init({ typescript: ts }) {
35
40
  function create(info) {
36
41
  const ls = info.languageService;
37
42
  const log = (m) => info.project.projectService.logger.info(`[typesol] ${m}`);
38
- const cache = new Map(); // fileName -> { text, found }
43
+ /** The compiler, loaded in the background; until then each check runs as a child process. */
44
+ let compiler;
45
+ const ready = import(pathToFileURL(path.join(DIST, "compiler", "index.js")).href).then(
46
+ (m) => (compiler = m),
47
+ (e) => log(`in-process compiler unavailable (${e.message}); checking with the CLI`),
48
+ );
49
+ const cache = new Map(); // entry -> { key, found }
39
50
 
40
- /** The entry's text plus the modification times of the .ts files beside it (its imports usually live there). */
41
- function key(fileName, sf) {
42
- const dir = path.dirname(path.resolve(fileName));
43
- let stamps = "";
44
- try {
45
- for (const f of fs.readdirSync(dir).sort())
46
- if (f.endsWith(".ts") && path.join(dir, f) !== path.resolve(fileName)) stamps += `${f}:${fs.statSync(path.join(dir, f)).mtimeMs};`;
47
- } catch {
48
- stamps = "";
51
+ /** The diagnostics of one program entry for these texts: in-process when possible, else through the CLI. */
52
+ function check(entry, files) {
53
+ if (compiler) {
54
+ try {
55
+ compiler.generate(entry, { files: new Map(Object.entries(files)) });
56
+ return [];
57
+ } catch (e) {
58
+ if (e instanceof compiler.CompileError) return e.diagnostics;
59
+ log(`check failed for ${entry}: ${e.message}`);
60
+ return [];
61
+ }
49
62
  }
50
- return `${stamps}\n${sf.text}`;
51
- }
52
-
53
- function typesolDiagnostics(fileName, sf) {
54
- const k = key(fileName, sf);
55
- const hit = cache.get(fileName);
56
- if (hit && hit.key === k) return hit.found;
57
- let found = [];
58
63
  try {
59
- const out = execFileSync(process.execPath, [CLI, "check", fileName, "--json", "--stdin"], {
60
- input: sf.text,
64
+ const out = execFileSync(process.execPath, [CLI, "check", entry, "--json", "--files"], {
65
+ input: JSON.stringify({ files }),
61
66
  encoding: "utf8",
62
67
  timeout: 15000,
63
68
  // tsserver may run inside an editor's Electron: make it behave as plain Node for the child.
64
69
  env: { ...process.env, ELECTRON_RUN_AS_NODE: "1" },
65
70
  stdio: ["pipe", "pipe", "pipe"],
66
71
  });
67
- found = JSON.parse(out).diagnostics;
72
+ return JSON.parse(out).diagnostics;
68
73
  } catch (e) {
69
74
  // Exit 1 with JSON on stdout means diagnostics; anything else is a crash worth logging, not showing.
70
75
  try {
71
- found = JSON.parse(String(e.stdout)).diagnostics;
76
+ return JSON.parse(String(e.stdout)).diagnostics;
72
77
  } catch {
73
- log(`check failed for ${fileName}: ${e.message}`);
74
- found = [];
78
+ log(`check failed for ${entry}: ${e.message}`);
79
+ return [];
75
80
  }
76
81
  }
77
- cache.set(fileName, { key: k, found });
82
+ }
83
+
84
+ /** `check`, cached on the texts it reads and the modification times of the .ts files beside the entry. */
85
+ function cachedCheck(entry, files) {
86
+ const hash = createHash("sha1");
87
+ for (const f of Object.keys(files).sort()) hash.update(`${f}\0${files[f]}\0`);
88
+ const dir = path.dirname(entry);
89
+ try {
90
+ for (const f of fs.readdirSync(dir).sort()) if (f.endsWith(".ts")) hash.update(`${f}:${fs.statSync(path.join(dir, f)).mtimeMs};`);
91
+ } catch {
92
+ /* the texts alone */
93
+ }
94
+ const key = hash.digest("hex");
95
+ const hit = cache.get(entry);
96
+ if (hit && hit.key === key) return hit.found;
97
+ const found = check(entry, files);
98
+ cache.set(entry, { key, found });
78
99
  return found;
79
100
  }
80
101
 
@@ -83,27 +104,42 @@ function init({ typescript: ts }) {
83
104
  proxy.getSemanticDiagnostics = (fileName) => {
84
105
  const prior = ls.getSemanticDiagnostics(fileName);
85
106
  if (prior.some((d) => d.category === ts.DiagnosticCategory.Error)) return prior; // TypeScript's errors first
86
- if (!entriesNear(path.dirname(path.resolve(fileName))).has(path.resolve(fileName))) return prior;
87
- const sf = ls.getProgram() && ls.getProgram().getSourceFile(fileName);
107
+ const target = path.resolve(fileName);
108
+ const entries = entriesNear(path.dirname(target));
109
+ if (!entries.size) return prior;
110
+ const program = ls.getProgram();
111
+ const sf = program && program.getSourceFile(fileName);
88
112
  if (!sf) return prior;
89
- const own = typesolDiagnostics(fileName, sf)
90
- .filter((d) => path.resolve(d.file) === path.resolve(fileName))
91
- .map((d) => {
113
+ // The editor's text of the project's own files (open buffers included).
114
+ const files = {};
115
+ for (const f of program.getSourceFiles()) if (!f.isDeclarationFile && !f.fileName.includes("/node_modules/")) files[path.resolve(f.fileName)] = f.text;
116
+ // An entry is checked on its own; another file through every entry (whichever imports it reports it).
117
+ const run = entries.has(target) ? [target] : [...entries].filter((e) => fs.existsSync(e) || files[e] !== undefined);
118
+ const seen = new Set();
119
+ const own = [];
120
+ for (const entry of run)
121
+ for (const d of cachedCheck(entry, files)) {
122
+ if (path.resolve(d.file) !== target) continue;
123
+ const id = `${d.line}:${d.column}:${d.message}`;
124
+ if (seen.has(id)) continue;
125
+ seen.add(id);
92
126
  const line = Math.max(0, Math.min(d.line - 1, sf.getLineStarts().length - 1));
93
127
  const start = Math.min(sf.getPositionOfLineAndCharacter(line, 0) + Math.max(0, d.column - 1), sf.end);
94
128
  const word = /^[\w$]+/.exec(sf.text.slice(start));
95
- return {
129
+ own.push({
96
130
  file: sf,
97
131
  start,
98
- length: word ? word[0].length : 1,
132
+ length: Math.max(1, Math.min(d.length || (word ? word[0].length : 1), sf.end - start)),
99
133
  messageText: d.message,
100
134
  category: ts.DiagnosticCategory.Error,
101
135
  code: CODE,
102
136
  source: "typesol",
103
- };
104
- });
137
+ });
138
+ }
105
139
  return [...prior, ...own];
106
140
  };
141
+ // Tests (and curious hosts) can wait for the in-process compiler.
142
+ proxy.typesolReady = () => ready;
107
143
  return proxy;
108
144
  }
109
145
  return { create };