@swc/nodejs-support-wasm 1.16.2 → 1.16.10-nightly-20260928.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 (4) hide show
  1. package/README.md +47 -5
  2. package/package.json +1 -1
  3. package/wasm.d.ts +19 -0
  4. package/wasm.js +49 -5
package/README.md CHANGED
@@ -10,11 +10,53 @@ syntax validation.
10
10
 
11
11
  ## API
12
12
 
13
- - `transform` and `transformSync`
14
- - `transformModuleSyntax`
15
- - `getFirstExpression`
16
- - `isValidSyntax`
17
- - `isRecoverableError`
13
+ - `transform` and `transformSync`
14
+ - `transformModuleSyntax`
15
+ - `getFirstExpression`
16
+ - `isValidSyntax`
17
+ - `isRecoverableError`
18
+ - `tokenize`
19
+ - `findTopLevelAwaits`
20
+
21
+ All source arguments accept a string or a UTF-8 `Uint8Array`.
22
+
23
+ ### Syntax highlighting
24
+
25
+ `tokenize(src)` returns `{ kind, start, end }[]` in source order. The token kinds
26
+ are `keyword`, `identifier`, `number`, `bigint`, `string`, `template`, `regexp`,
27
+ `comment`, and `punctuator`. Offsets are zero-based UTF-16 code units with an
28
+ exclusive end, so `src.slice(start, end)` extracts the token. Comments include
29
+ their delimiters; hashbangs are comments. Whitespace and EOF are omitted.
30
+ Template text includes its backticks, while `${` and `}` are punctuators and
31
+ interpolated expressions have their own tokens.
32
+
33
+ This helper parses JavaScript to distinguish regular expressions from division
34
+ and track template substitutions. On incomplete or invalid input it returns the
35
+ tokens captured before parsing stopped, without throwing a syntax error. It is
36
+ intended for REPL highlighting, not syntax validation or TypeScript tokenization.
37
+
38
+ ### Top-level await locations
39
+
40
+ `findTopLevelAwaits(src)` returns `{ line, column }[]` in source order for `await`
41
+ expressions, `for await` loops, and `await using` declarations in module scope.
42
+ Lines start at 1 and UTF-16 columns start at 0, matching Acorn. Each location is
43
+ the start of the construct (`for` for a `for await` loop). Function bodies,
44
+ including methods and arrow functions, are excluded; class heritage expressions,
45
+ computed keys, and method decorators are included. JavaScript and TypeScript are
46
+ supported, and invalid or incomplete modules return an empty array.
47
+
48
+ ### REPL imports
49
+
50
+ `transformModuleSyntax(src)` hoists imports and declares their local bindings,
51
+ so a REPL can retain them for subsequent inputs. For example, a default import
52
+ declares `const { default: fs } = await __nodeREPLDynamicImport(...)` after
53
+ validating that the export exists. The caller supplies `__nodeREPLDynamicImport`
54
+ and evaluates the output with top-level await support.
55
+
56
+ Default and named bindings capture the initial export values, matching Node's
57
+ REPL transform; namespace bindings retain the module namespace object. References
58
+ and assignments are left intact so normal JavaScript scope, call, and `const`
59
+ semantics apply before any further rewriting performed by the host REPL.
18
60
 
19
61
  ## Contributing
20
62
 
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "강동윤 <kdy1997.dev@gmail.com>"
5
5
  ],
6
6
  "description": "SWC WebAssembly helpers for Node.js",
7
- "version": "1.16.2",
7
+ "version": "1.16.10-nightly-20260928.1",
8
8
  "license": "Apache-2.0",
9
9
  "repository": {
10
10
  "type": "git",
package/wasm.d.ts CHANGED
@@ -7,8 +7,27 @@ export declare function transformModuleSyntax(src: string | Uint8Array): ModuleS
7
7
  export declare function getFirstExpression(src: string | Uint8Array, startColumn: number): string;
8
8
  export declare function isValidSyntax(src: string | Uint8Array): boolean;
9
9
  export declare function isRecoverableError(src: string | Uint8Array): boolean;
10
+ export declare function tokenize(src: string | Uint8Array): Token[];
11
+ export declare function findTopLevelAwaits(src: string | Uint8Array): TopLevelAwaitLocation[];
10
12
  export type { Options, TransformOutput };
11
13
 
14
+ export type TokenKind = "keyword" | "identifier" | "number" | "bigint" | "string" | "template" | "regexp" | "comment" | "punctuator";
15
+
16
+ export interface Token {
17
+ kind: TokenKind;
18
+ /** Zero-based UTF-16 offset, inclusive. */
19
+ start: number;
20
+ /** Zero-based UTF-16 offset, exclusive. */
21
+ end: number;
22
+ }
23
+
24
+ export interface TopLevelAwaitLocation {
25
+ /** One-based line number. */
26
+ line: number;
27
+ /** Zero-based UTF-16 column. */
28
+ column: number;
29
+ }
30
+
12
31
  export interface ModuleSyntaxTransformOutput {
13
32
  code: string;
14
33
  hadModuleSyntax: boolean;