@stacksjs/zig-dtsx 0.9.10

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/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ # MIT License
2
+
3
+ Copyright (c) 2024 Open Web Foundation
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # zig-dtsx
2
+
3
+ A high-performance TypeScript declaration file (.d.ts) emitter written in Zig. This is the native companion to the TypeScript-based `@stacksjs/dtsx`, providing the same output with significantly faster execution.
4
+
5
+ ## Overview
6
+
7
+ `zig-dtsx` reimplements the dtsx declaration generation pipeline in Zig for maximum performance. It produces output identical to the TypeScript implementation and is used as a drop-in replacement when the compiled binary is available.
8
+
9
+ ## Installation
10
+
11
+ ```bash
12
+ bun add @stacksjs/zig-dtsx -d
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ### As a Library
18
+
19
+ ```typescript
20
+ import { processSource, ZIG_AVAILABLE } from '@stacksjs/zig-dtsx'
21
+
22
+ if (ZIG_AVAILABLE) {
23
+ const dtsOutput = processSource(typescriptSource, true)
24
+ console.log(dtsOutput)
25
+ }
26
+ ```
27
+
28
+ ### Building from Source
29
+
30
+ Requires [Zig](https://ziglang.org/) to be installed.
31
+
32
+ ```bash
33
+ # Build optimized release binary
34
+ bun run build:zig
35
+
36
+ # Build debug binary
37
+ bun run build:zig-debug
38
+
39
+ # Run Zig-native tests
40
+ bun run test:zig
41
+ ```
42
+
43
+ ## Architecture
44
+
45
+ The Zig implementation mirrors the TypeScript dtsx pipeline:
46
+
47
+ 1. **Scanner** - Tokenizes TypeScript source into a stream of tokens
48
+ 2. **Extractor** - Parses tokens and extracts declaration-relevant constructs (exports, types, interfaces, classes, functions, variables)
49
+ 3. **Emitter** - Produces `.d.ts` output from the extracted declarations
50
+
51
+ The Zig binary communicates with the Node/Bun runtime via FFI, allowing seamless integration with the existing dtsx ecosystem.
52
+
53
+ ## Benchmarks
54
+
55
+ ```bash
56
+ bun run benchmark
57
+ ```
58
+
59
+ ## Testing
60
+
61
+ Tests validate that the Zig emitter produces identical output to the TypeScript implementation across all shared test fixtures.
62
+
63
+ ```bash
64
+ # Run via Bun test runner
65
+ bun test
66
+
67
+ # Run Zig-native tests
68
+ zig build test
69
+ ```
70
+
71
+ ## License
72
+
73
+ MIT
package/build.zig ADDED
@@ -0,0 +1,79 @@
1
+ const std = @import("std");
2
+
3
+ pub fn build(b: *std.Build) void {
4
+ const target = b.standardTargetOptions(.{});
5
+ const optimize = b.standardOptimizeOption(.{});
6
+
7
+ // Shared library for FFI (needs libc for c_allocator)
8
+ const lib = b.addLibrary(.{
9
+ .linkage = .dynamic,
10
+ .name = "zig-dtsx",
11
+ .root_module = b.createModule(.{
12
+ .root_source_file = b.path("src/lib.zig"),
13
+ .target = target,
14
+ .optimize = optimize,
15
+ .link_libc = true,
16
+ }),
17
+ });
18
+ // Enable LTO on Linux release builds (LLD required; macOS Mach-O doesn't support LLD)
19
+ if (target.result.os.tag == .linux and optimize != .Debug) {
20
+ lib.use_lld = true;
21
+ lib.lto = .full;
22
+ }
23
+ // Strip debug symbols in release builds for smaller binary + faster loads
24
+ if (optimize != .Debug) {
25
+ lib.root_module.strip = true;
26
+ }
27
+ b.installArtifact(lib);
28
+
29
+ // CLI binary (needs libc for C stdio)
30
+ const exe = b.addExecutable(.{
31
+ .name = "zig-dtsx",
32
+ .root_module = b.createModule(.{
33
+ .root_source_file = b.path("src/main.zig"),
34
+ .target = target,
35
+ .optimize = optimize,
36
+ .link_libc = true,
37
+ }),
38
+ });
39
+ if (optimize != .Debug) {
40
+ exe.root_module.strip = true;
41
+ }
42
+ b.installArtifact(exe);
43
+
44
+ // CLI-only step (for cross-compilation without shared library)
45
+ const cli_install = b.addInstallArtifact(exe, .{});
46
+ const cli_step = b.step("cli", "Build only the CLI binary");
47
+ cli_step.dependOn(&cli_install.step);
48
+
49
+ // Run step
50
+ const run_cmd = b.addRunArtifact(exe);
51
+ run_cmd.step.dependOn(b.getInstallStep());
52
+ if (b.args) |args| {
53
+ run_cmd.addArgs(args);
54
+ }
55
+ const run_step = b.step("run", "Run the CLI");
56
+ run_step.dependOn(&run_cmd.step);
57
+
58
+ // Unit tests
59
+ const test_targets = [_][]const u8{
60
+ "src/char_utils.zig",
61
+ "src/types.zig",
62
+ "src/scanner.zig",
63
+ "src/emitter.zig",
64
+ "src/type_inference.zig",
65
+ };
66
+
67
+ const test_step = b.step("test", "Run unit tests");
68
+ for (test_targets) |test_file| {
69
+ const unit_test = b.addTest(.{
70
+ .root_module = b.createModule(.{
71
+ .root_source_file = b.path(test_file),
72
+ .target = target,
73
+ .optimize = optimize,
74
+ }),
75
+ });
76
+ const run_unit_test = b.addRunArtifact(unit_test);
77
+ test_step.dependOn(&run_unit_test.step);
78
+ }
79
+ }
package/build.zig.zon ADDED
@@ -0,0 +1,11 @@
1
+ .{
2
+ .name = .zig_dtsx,
3
+ .version = "0.1.0",
4
+ .fingerprint = 0x545e4f1c4c6ae840,
5
+ .paths = .{
6
+ "build.zig",
7
+ "build.zig.zon",
8
+ "src",
9
+ },
10
+ .dependencies = .{},
11
+ }
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@stacksjs/zig-dtsx",
3
+ "type": "module",
4
+ "version": "0.9.10",
5
+ "description": "High-performance DTS emitter written in Zig",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./dist/index.d.ts",
9
+ "import": "./src/index.ts"
10
+ }
11
+ },
12
+ "scripts": {
13
+ "build:zig": "zig build -Doptimize=ReleaseFast",
14
+ "build:zig-debug": "zig build",
15
+ "build": "bun run build:zig",
16
+ "test": "bun test",
17
+ "test:zig": "zig build test",
18
+ "benchmark": "bun run test/benchmark.ts"
19
+ },
20
+ "dependencies": {
21
+ "@stacksjs/dtsx": "0.9.10"
22
+ }
23
+ }
@@ -0,0 +1,158 @@
1
+ /// Character constants and classification functions for the TypeScript scanner.
2
+ /// Direct port of scanner.ts lines 9-49.
3
+
4
+ // Character code constants for fast comparison
5
+ pub const CH_SPACE: u8 = 32;
6
+ pub const CH_TAB: u8 = 9;
7
+ pub const CH_LF: u8 = 10;
8
+ pub const CH_CR: u8 = 13;
9
+ pub const CH_SLASH: u8 = 47;
10
+ pub const CH_STAR: u8 = 42;
11
+ pub const CH_SQUOTE: u8 = 39;
12
+ pub const CH_DQUOTE: u8 = 34;
13
+ pub const CH_BACKTICK: u8 = 96;
14
+ pub const CH_BACKSLASH: u8 = 92;
15
+ pub const CH_LBRACE: u8 = 123;
16
+ pub const CH_RBRACE: u8 = 125;
17
+ pub const CH_LPAREN: u8 = 40;
18
+ pub const CH_RPAREN: u8 = 41;
19
+ pub const CH_LBRACKET: u8 = 91;
20
+ pub const CH_RBRACKET: u8 = 93;
21
+ pub const CH_LANGLE: u8 = 60;
22
+ pub const CH_RANGLE: u8 = 62;
23
+ pub const CH_SEMI: u8 = 59;
24
+ pub const CH_COLON: u8 = 58;
25
+ pub const CH_EQUAL: u8 = 61;
26
+ pub const CH_COMMA: u8 = 44;
27
+ pub const CH_DOT: u8 = 46;
28
+ pub const CH_QUESTION: u8 = 63;
29
+ pub const CH_HASH: u8 = 35;
30
+ pub const CH_AT: u8 = 64;
31
+ pub const CH_DOLLAR: u8 = 36;
32
+ pub const CH_UNDERSCORE: u8 = 95;
33
+ pub const CH_EXCL: u8 = 33;
34
+ pub const CH_PIPE: u8 = 124;
35
+ pub const CH_AMP: u8 = 38;
36
+ pub const CH_CARET: u8 = 94;
37
+ pub const CH_TILDE: u8 = 126;
38
+ pub const CH_PLUS: u8 = 43;
39
+ pub const CH_MINUS: u8 = 45;
40
+ pub const CH_PERCENT: u8 = 37;
41
+
42
+ pub const CH_BOM: u16 = 0xFEFF;
43
+
44
+ /// Check if a character is whitespace (space, tab, LF, CR)
45
+ pub inline fn isWhitespace(ch: u8) bool {
46
+ return ch == CH_SPACE or ch == CH_TAB or ch == CH_LF or ch == CH_CR;
47
+ }
48
+
49
+ /// Check if a character can start an identifier (A-Z, a-z, _, $, or >127 for Unicode)
50
+ pub inline fn isIdentStart(ch: u8) bool {
51
+ return (ch >= 'A' and ch <= 'Z') or (ch >= 'a' and ch <= 'z') or ch == CH_UNDERSCORE or ch == CH_DOLLAR or ch > 127;
52
+ }
53
+
54
+ /// Check if a character can continue an identifier (isIdentStart + 0-9)
55
+ pub inline fn isIdentChar(ch: u8) bool {
56
+ return isIdentStart(ch) or (ch >= '0' and ch <= '9');
57
+ }
58
+
59
+ /// Check if a character is a digit (0-9)
60
+ pub inline fn isDigit(ch: u8) bool {
61
+ return ch >= '0' and ch <= '9';
62
+ }
63
+
64
+ /// Find needle in haystack starting from start position
65
+ pub inline fn indexOf(haystack: []const u8, needle: []const u8, start: usize) ?usize {
66
+ if (needle.len == 0) return start;
67
+ if (start + needle.len > haystack.len) return null;
68
+ return std.mem.indexOfPos(u8, haystack, start, needle);
69
+ }
70
+
71
+ /// Find a single byte in haystack starting from start position
72
+ pub inline fn indexOfChar(haystack: []const u8, needle: u8, start: usize) ?usize {
73
+ if (start >= haystack.len) return null;
74
+ return std.mem.indexOfScalarPos(u8, haystack, start, needle);
75
+ }
76
+
77
+ /// Slice source[start..end) with leading/trailing whitespace trimmed
78
+ pub fn sliceTrimmed(source: []const u8, start_pos: usize, end_pos: usize) []const u8 {
79
+ var s = start_pos;
80
+ var e = end_pos;
81
+ if (s >= e) return "";
82
+
83
+ // Fast path: if endpoints are already non-whitespace, skip trim loops
84
+ if (!isWhitespace(source[s]) and !isWhitespace(source[e - 1])) {
85
+ return source[s..e];
86
+ }
87
+
88
+ while (s < e and isWhitespace(source[s])) s += 1;
89
+ while (e > s and isWhitespace(source[e - 1])) e -= 1;
90
+ return source[s..e];
91
+ }
92
+
93
+ /// Check if a string starts with a given prefix
94
+ pub inline fn startsWith(str: []const u8, prefix: []const u8) bool {
95
+ if (str.len < prefix.len) return false;
96
+ return std.mem.eql(u8, str[0..prefix.len], prefix);
97
+ }
98
+
99
+ /// Check if a string ends with a given suffix
100
+ pub inline fn endsWith(str: []const u8, suffix: []const u8) bool {
101
+ if (str.len < suffix.len) return false;
102
+ return std.mem.eql(u8, str[str.len - suffix.len ..], suffix);
103
+ }
104
+
105
+ /// Check if a string contains a substring
106
+ pub inline fn contains(str: []const u8, needle: []const u8) bool {
107
+ return std.mem.indexOf(u8, str, needle) != null;
108
+ }
109
+
110
+ const std = @import("std");
111
+
112
+ // --- Tests ---
113
+ test "isWhitespace" {
114
+ try std.testing.expect(isWhitespace(' '));
115
+ try std.testing.expect(isWhitespace('\t'));
116
+ try std.testing.expect(isWhitespace('\n'));
117
+ try std.testing.expect(isWhitespace('\r'));
118
+ try std.testing.expect(!isWhitespace('a'));
119
+ try std.testing.expect(!isWhitespace('/'));
120
+ }
121
+
122
+ test "isIdentStart" {
123
+ try std.testing.expect(isIdentStart('a'));
124
+ try std.testing.expect(isIdentStart('Z'));
125
+ try std.testing.expect(isIdentStart('_'));
126
+ try std.testing.expect(isIdentStart('$'));
127
+ try std.testing.expect(isIdentStart(200)); // Unicode
128
+ try std.testing.expect(!isIdentStart('0'));
129
+ try std.testing.expect(!isIdentStart(' '));
130
+ }
131
+
132
+ test "isIdentChar" {
133
+ try std.testing.expect(isIdentChar('a'));
134
+ try std.testing.expect(isIdentChar('0'));
135
+ try std.testing.expect(isIdentChar('_'));
136
+ try std.testing.expect(!isIdentChar(' '));
137
+ try std.testing.expect(!isIdentChar(';'));
138
+ }
139
+
140
+ test "sliceTrimmed" {
141
+ const src = " hello ";
142
+ try std.testing.expectEqualStrings("hello", sliceTrimmed(src, 0, src.len));
143
+ try std.testing.expectEqualStrings("hello", sliceTrimmed(src, 2, 7));
144
+ }
145
+
146
+ test "indexOf" {
147
+ const s = "hello world";
148
+ try std.testing.expectEqual(@as(?usize, 6), indexOf(s, "world", 0));
149
+ try std.testing.expectEqual(@as(?usize, null), indexOf(s, "xyz", 0));
150
+ try std.testing.expectEqual(@as(?usize, null), indexOf(s, "world", 7));
151
+ }
152
+
153
+ test "startsWith and endsWith" {
154
+ try std.testing.expect(startsWith("export const", "export"));
155
+ try std.testing.expect(!startsWith("import", "export"));
156
+ try std.testing.expect(endsWith("hello world", "world"));
157
+ try std.testing.expect(!endsWith("hello", "world"));
158
+ }