toilscript 0.1.24 → 0.1.26

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.
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "imports": {
3
- "toilscript": "https://cdn.jsdelivr.net/npm/toilscript@0.1.24/dist/toilscript.js",
4
- "toilscript/cli": "https://cdn.jsdelivr.net/npm/toilscript@0.1.24/dist/cli.js",
3
+ "toilscript": "https://cdn.jsdelivr.net/npm/toilscript@0.1.26/dist/toilscript.js",
4
+ "toilscript/cli": "https://cdn.jsdelivr.net/npm/toilscript@0.1.26/dist/cli.js",
5
5
  "binaryen": "https://cdn.jsdelivr.net/npm/binaryen@129.0.0-nightly.20260428/index.js",
6
6
  "long": "https://cdn.jsdelivr.net/npm/long@5.3.2/index.js"
7
7
  }
package/dist/web.js CHANGED
@@ -1,8 +1,8 @@
1
- var ASSEMBLYSCRIPT_VERSION = "0.1.24";
1
+ var ASSEMBLYSCRIPT_VERSION = "0.1.26";
2
2
  var ASSEMBLYSCRIPT_IMPORTMAP = {
3
3
  "imports": {
4
- "toilscript": "https://cdn.jsdelivr.net/npm/toilscript@0.1.24/dist/toilscript.js",
5
- "toilscript/cli": "https://cdn.jsdelivr.net/npm/toilscript@0.1.24/dist/cli.js",
4
+ "toilscript": "https://cdn.jsdelivr.net/npm/toilscript@0.1.26/dist/toilscript.js",
5
+ "toilscript/cli": "https://cdn.jsdelivr.net/npm/toilscript@0.1.26/dist/cli.js",
6
6
  "binaryen": "https://cdn.jsdelivr.net/npm/binaryen@129.0.0-nightly.20260428/index.js",
7
7
  "long": "https://cdn.jsdelivr.net/npm/long@5.3.2/index.js"
8
8
  }
package/package.json CHANGED
@@ -8,7 +8,7 @@
8
8
  "toilscript",
9
9
  "wasm"
10
10
  ],
11
- "version": "0.1.24",
11
+ "version": "0.1.26",
12
12
  "author": "Daniel Wirtz <dcode+assemblyscript@dcode.io>",
13
13
  "license": "Apache-2.0",
14
14
  "homepage": "https://github.com/dacely-cloud/toilscript",
package/std/ts-plugin.cjs CHANGED
@@ -33,6 +33,13 @@ const DATA_MEMBERS = new Set([
33
33
  'dataId',
34
34
  ]);
35
35
 
36
+ /**
37
+ * Decorators whose class carries the compiler-injected `@data` binary codec.
38
+ * `@user` is `@data` plus the auth-user registration (it also types
39
+ * `AuthService.getUser()`); both get the same codec members.
40
+ */
41
+ const CODEC_DECORATORS = new Set(['data', 'user']);
42
+
36
43
  /** Toil-native decorators whose presence means the compiler uses the declaration. */
37
44
  const TOIL_DECORATORS = new Set([
38
45
  'data',
@@ -48,6 +55,10 @@ const TOIL_DECORATORS = new Set([
48
55
  'head',
49
56
  'options',
50
57
  'main',
58
+ 'user',
59
+ 'auth',
60
+ 'cache',
61
+ 'ratelimit',
51
62
  'global',
52
63
  'inline',
53
64
  'external',
@@ -90,7 +101,7 @@ function init(modules) {
90
101
  function declsAreDataClass(declarations) {
91
102
  return (
92
103
  !!declarations &&
93
- declarations.some((d) => ts.isClassDeclaration(d) && declHasDecorator(d, 'data'))
104
+ declarations.some((d) => ts.isClassDeclaration(d) && declHasDecorator(d, CODEC_DECORATORS))
94
105
  );
95
106
  }
96
107
 
@@ -114,28 +125,74 @@ function init(modules) {
114
125
  );
115
126
  }
116
127
 
128
+ /** True when a declaration carries an `export` modifier. The appended codec
129
+ * augmentation must match the class's export-ness, else merging the class with
130
+ * the synthesized `interface`/`namespace` trips TS2395 ("must be all exported
131
+ * or all local"). */
132
+ function hasExportModifier(node) {
133
+ const mods =
134
+ (ts.getModifiers && ts.canHaveModifiers && ts.canHaveModifiers(node)
135
+ ? ts.getModifiers(node)
136
+ : node.modifiers) || [];
137
+ return mods.some((m) => m.kind === ts.SyntaxKind.ExportKeyword);
138
+ }
139
+
140
+ /** True when the parsed file is an ES module (has a top-level import/export), so
141
+ * a global augmentation must be wrapped in `declare global { ... }`. */
142
+ function isModuleFile(sf) {
143
+ const modsOf = (n) =>
144
+ (ts.getModifiers && ts.canHaveModifiers && ts.canHaveModifiers(n)
145
+ ? ts.getModifiers(n)
146
+ : n.modifiers) || [];
147
+ return sf.statements.some(
148
+ (s) =>
149
+ ts.isImportDeclaration(s) ||
150
+ ts.isImportEqualsDeclaration(s) ||
151
+ ts.isExportDeclaration(s) ||
152
+ ts.isExportAssignment(s) ||
153
+ modsOf(s).some((m) => m.kind === ts.SyntaxKind.ExportKeyword),
154
+ );
155
+ }
156
+
117
157
  /**
118
- * Ambient declarations to append so the editor types each `@data` class's injected
119
- * codec members. Returns "" when the file declares no `@data` class. Uses only
158
+ * Ambient declarations to append so the editor types each `@data`/`@user` class's
159
+ * injected codec members, plus the project-wide type of `AuthService.getUser()`
160
+ * (the single `@user` class). Returns "" when the file declares neither. Uses only
120
161
  * editor-visible globals (`Uint8Array`, `JSON`, `u32`), so the appended block is
121
162
  * itself error-free; any diagnostics there are filtered anyway.
122
163
  */
123
164
  function dataAugmentation(text) {
124
- if (text.indexOf('@data') < 0) return '';
165
+ if (text.indexOf('@data') < 0 && text.indexOf('@user') < 0) return '';
125
166
  let sf;
126
167
  try {
127
168
  sf = ts.createSourceFile('__toil_aug__.ts', text, ts.ScriptTarget.Latest, true);
128
169
  } catch {
129
170
  return '';
130
171
  }
172
+ const moduleScoped = isModuleFile(sf);
131
173
  let out = '';
132
174
  sf.forEachChild((node) => {
133
- if (ts.isClassDeclaration(node) && node.name && declHasDecorator(node, 'data')) {
134
- const n = node.name.text;
175
+ if (!ts.isClassDeclaration(node) || !node.name) return;
176
+ const isUser = declHasDecorator(node, 'user');
177
+ if (!declHasDecorator(node, 'data') && !isUser) return;
178
+ const n = node.name.text;
179
+ // Match the class's export-ness so the class/interface/namespace merge is all
180
+ // exported or all local (an exported class + local augmentation -> TS2395).
181
+ const exp = hasExportModifier(node) ? 'export ' : '';
182
+ // Both `@data` and `@user` classes get the compiler-injected binary codec.
183
+ out +=
184
+ `\n// toilscript: editor types for the compiler-injected ${isUser ? '@user' : '@data'} ${n} codec\n` +
185
+ `${exp}interface ${n} { encode(): Uint8Array; toJSON(): JSON; }\n` +
186
+ `${exp}declare namespace ${n} { function decode(buf: Uint8Array): ${n}; function fromJSON(v: JSON): ${n}; function dataId(): u32; }\n`;
187
+ // The single `@user` class is also the type of `AuthService.getUser()`
188
+ // everywhere: merge it into the global `__ToilAuthUser` interface that the
189
+ // generated env d.ts returns from `getUser()`. `declare global` when the
190
+ // file is a module (else a bare interface, already global in a script).
191
+ if (isUser) {
192
+ const merge = `interface __ToilAuthUser extends ${n} {}`;
135
193
  out +=
136
- `\n// toilscript: editor types for the compiler-injected @data ${n} codec\n` +
137
- `interface ${n} { encode(): Uint8Array; toJSON(): JSON; }\n` +
138
- `declare namespace ${n} { function decode(buf: Uint8Array): ${n}; function fromJSON(v: JSON): ${n}; function dataId(): u32; }\n`;
194
+ `// toilscript: editor type for AuthService.getUser() (the @user ${n})\n` +
195
+ (moduleScoped ? `declare global { ${merge} }\n` : `${merge}\n`);
139
196
  }
140
197
  });
141
198
  return out;