@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.
- package/CHANGELOG.md +35 -0
- package/README.md +2 -2
- package/dist/cli.js +13 -20
- package/dist/compiler/analyze.d.ts +6 -2
- package/dist/compiler/analyze.js +57 -15
- package/dist/compiler/diagnostics.d.ts +2 -0
- package/dist/compiler/diagnostics.js +3 -2
- package/dist/compiler/emit/alias.d.ts +48 -0
- package/dist/compiler/emit/alias.js +166 -0
- package/dist/compiler/emit/borsh.js +6 -3
- package/dist/compiler/emit/calls.js +21 -4
- package/dist/compiler/emit/cpi-core.d.ts +70 -0
- package/dist/compiler/emit/cpi-core.js +356 -0
- package/dist/compiler/emit/cpi.d.ts +3 -177
- package/dist/compiler/emit/cpi.js +13 -2721
- package/dist/compiler/emit/create.d.ts +39 -0
- package/dist/compiler/emit/create.js +548 -0
- package/dist/compiler/emit/function.d.ts +11 -2
- package/dist/compiler/emit/function.js +42 -7
- package/dist/compiler/emit/metaplex.d.ts +78 -0
- package/dist/compiler/emit/metaplex.js +1345 -0
- package/dist/compiler/emit/program.js +24 -4
- package/dist/compiler/emit/shared.d.ts +7 -1
- package/dist/compiler/emit/shared.js +11 -2
- package/dist/compiler/emit/statements.d.ts +0 -9
- package/dist/compiler/emit/statements.js +2 -53
- package/dist/compiler/emit/storage.d.ts +5 -20
- package/dist/compiler/emit/storage.js +40 -106
- package/dist/compiler/emit/token.d.ts +8 -0
- package/dist/compiler/emit/token.js +493 -0
- package/dist/compiler/index.js +1 -1
- package/dist/compiler/model.d.ts +10 -2
- package/dist/compiler/model.js +7 -1
- package/dist/tools/portgen.js +3 -0
- package/dist/workspace.d.ts +1 -1
- package/dist/workspace.js +3 -1
- package/docs/architecture.md +30 -2
- package/docs/audit.md +3 -2
- package/docs/case-study-multisig.md +31 -29
- package/docs/cli.md +7 -4
- package/docs/errors.md +3 -2
- package/docs/language.md +4 -3
- package/docs/migrate-from-anchor.md +3 -3
- package/docs/security-model.md +1 -1
- package/lib/typesol.d.ts +17 -6
- package/package.json +1 -1
- package/runtime/typesol_rt.h +85 -1
- package/ts-plugin/index.js +75 -39
package/ts-plugin/index.js
CHANGED
|
@@ -2,17 +2,22 @@
|
|
|
2
2
|
//
|
|
3
3
|
// programs/tsconfig.json: { "compilerOptions": { …, "plugins": [{ "name": "typesol/ts-plugin" }] } }
|
|
4
4
|
//
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
// (an unsupported construct, a
|
|
8
|
-
//
|
|
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
|
|
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
|
-
|
|
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
|
|
41
|
-
function
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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",
|
|
60
|
-
input:
|
|
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
|
-
|
|
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
|
-
|
|
76
|
+
return JSON.parse(String(e.stdout)).diagnostics;
|
|
72
77
|
} catch {
|
|
73
|
-
log(`check failed for ${
|
|
74
|
-
|
|
78
|
+
log(`check failed for ${entry}: ${e.message}`);
|
|
79
|
+
return [];
|
|
75
80
|
}
|
|
76
81
|
}
|
|
77
|
-
|
|
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
|
-
|
|
87
|
-
const
|
|
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
|
-
|
|
90
|
-
|
|
91
|
-
|
|
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
|
-
|
|
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 };
|