@kattebak/typespec-enum-emitter 1.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 kattebak
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,135 @@
1
+ # TypeSpec Enum Emitter
2
+
3
+ A custom TypeSpec emitter that exports enums as JavaScript const objects with proper TypeScript type definitions.
4
+
5
+ ## Features
6
+
7
+ - Converts TypeSpec enums to JavaScript const objects
8
+ - Generates TypeScript declaration files (.d.ts)
9
+ - Supports string and numeric enum values
10
+ - Uses the TypeSpec emitter framework
11
+
12
+ ## Installation
13
+
14
+ ```bash
15
+ npm install typespec-enum-emitter
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ ### 1. Create a TypeSpec file with enums
21
+
22
+ ```typespec
23
+ import "typespec-enum-emitter";
24
+
25
+ enum Status {
26
+ Active,
27
+ Inactive,
28
+ Pending,
29
+ }
30
+
31
+ enum Priority {
32
+ Low: "low",
33
+ Medium: "medium",
34
+ High: "high",
35
+ }
36
+
37
+ enum HttpStatusCode {
38
+ OK: 200,
39
+ NotFound: 404,
40
+ InternalServerError: 500,
41
+ }
42
+ ```
43
+
44
+ ### 2. Configure tspconfig.yaml
45
+
46
+ ```yaml
47
+ emit:
48
+ - @kattebak/typespec-enum-emitter
49
+ options:
50
+ typespec-enum-emitter:
51
+ output-file: "enums.js"
52
+ ```
53
+
54
+ ### 3. Compile
55
+
56
+ ```bash
57
+ npx tsp compile .
58
+ ```
59
+
60
+ ### 4. Generated Output
61
+
62
+ **enums.js:**
63
+
64
+ ```javascript
65
+ "use strict";
66
+ Object.defineProperty(exports, "__esModule", { value: true });
67
+
68
+ export const Status = {
69
+ Active: "Active",
70
+ Inactive: "Inactive",
71
+ Pending: "Pending",
72
+ };
73
+
74
+ export const Priority = {
75
+ Low: "low",
76
+ Medium: "medium",
77
+ High: "high",
78
+ };
79
+
80
+ export const HttpStatusCode = {
81
+ OK: 200,
82
+ NotFound: 404,
83
+ InternalServerError: 500,
84
+ };
85
+ ```
86
+
87
+ **enums.d.ts:**
88
+
89
+ ```typescript
90
+ export declare const Status: {
91
+ readonly Active: "Active";
92
+ readonly Inactive: "Inactive";
93
+ readonly Pending: "Pending";
94
+ };
95
+
96
+ export declare const Priority: {
97
+ readonly Low: "low";
98
+ readonly Medium: "medium";
99
+ readonly High: "high";
100
+ };
101
+
102
+ export declare const HttpStatusCode: {
103
+ readonly OK: 200;
104
+ readonly NotFound: 404;
105
+ readonly InternalServerError: 500;
106
+ };
107
+ ```
108
+
109
+ ## Configuration Options
110
+
111
+ - `output-file`: Name of the output file (default: "enums.js")
112
+ - `output-dir`: Output directory (defaults to emitter output directory)
113
+
114
+ ## Why Use This?
115
+
116
+ TypeScript enums have some quirks:
117
+
118
+ - They generate runtime code that can be hard to tree-shake
119
+ - Const enums don't preserve values at runtime
120
+ - Regular enums can have unexpected behavior with numeric values
121
+
122
+ This emitter generates simple const objects that:
123
+
124
+ - Are fully tree-shakeable
125
+ - Have clear runtime behavior
126
+ - Work seamlessly with TypeScript's type system
127
+ - Are easy to inspect and debug
128
+
129
+ ## Implementation
130
+
131
+ Built using the TypeSpec emitter framework with:
132
+
133
+ - `@typespec/compiler` for AST traversal
134
+ - Namespace-aware enum collection
135
+ - Proper TypeScript declaration generation
@@ -0,0 +1,4 @@
1
+ import type { EmitContext } from "@typespec/compiler";
2
+ import type { EnumEmitterOptions } from "./lib.js";
3
+ export declare function $onEmit(context: EmitContext<EnumEmitterOptions>): Promise<void>;
4
+ //# sourceMappingURL=emitter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"emitter.d.ts","sourceRoot":"","sources":["../src/emitter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAQtD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAEnD,wBAAsB,OAAO,CAAC,OAAO,EAAE,WAAW,CAAC,kBAAkB,CAAC,iBAmCrE"}
@@ -0,0 +1,68 @@
1
+ import { emitFile, resolvePath, } from "@typespec/compiler";
2
+ export async function $onEmit(context) {
3
+ const enums = [];
4
+ function collectEnums(namespace) {
5
+ for (const [_, type] of namespace.enums) {
6
+ enums.push(type);
7
+ }
8
+ for (const [_, ns] of namespace.namespaces) {
9
+ collectEnums(ns);
10
+ }
11
+ }
12
+ const globalNamespace = context.program.getGlobalNamespaceType();
13
+ collectEnums(globalNamespace);
14
+ if (enums.length === 0) {
15
+ return;
16
+ }
17
+ const outputDir = context.emitterOutputDir;
18
+ const outputFile = context.options["output-file"] ?? "enums.js";
19
+ const jsContent = generateJavaScript(enums);
20
+ const tsContent = generateTypeScript(enums);
21
+ await emitFile(context.program, {
22
+ path: resolvePath(outputDir, outputFile),
23
+ content: jsContent,
24
+ });
25
+ await emitFile(context.program, {
26
+ path: resolvePath(outputDir, outputFile.replace(".js", ".d.ts")),
27
+ content: tsContent,
28
+ });
29
+ }
30
+ function generateJavaScript(enums) {
31
+ const enumDefinitions = enums
32
+ .map((enumType) => {
33
+ const members = Array.from(enumType.members.values());
34
+ const enumObject = generateEnumObject(members);
35
+ return `export const ${enumType.name} = ${enumObject};`;
36
+ })
37
+ .join("\n\n");
38
+ return `"use strict";\nObject.defineProperty(exports, "__esModule", { value: true });\n\n${enumDefinitions}\n`;
39
+ }
40
+ function generateEnumObject(members) {
41
+ const entries = members
42
+ .map((member) => {
43
+ const key = member.name;
44
+ const value = member.value ?? member.name;
45
+ const valueStr = typeof value === "string" ? `"${value}"` : value;
46
+ return `\t${key}: ${valueStr}`;
47
+ })
48
+ .join(",\n");
49
+ return `{\n${entries}\n}`;
50
+ }
51
+ function generateTypeScript(enums) {
52
+ const enumDeclarations = enums
53
+ .map((enumType) => {
54
+ const members = Array.from(enumType.members.values());
55
+ const memberTypes = members
56
+ .map((member) => {
57
+ const key = member.name;
58
+ const value = member.value ?? member.name;
59
+ const valueStr = typeof value === "string" ? `"${value}"` : value;
60
+ return `\treadonly ${key}: ${valueStr}`;
61
+ })
62
+ .join(";\n");
63
+ return `export declare const ${enumType.name}: {\n${memberTypes}\n};`;
64
+ })
65
+ .join("\n\n");
66
+ return `${enumDeclarations}\n`;
67
+ }
68
+ //# sourceMappingURL=emitter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"emitter.js","sourceRoot":"","sources":["../src/emitter.ts"],"names":[],"mappings":"AACA,OAAO,EAGN,QAAQ,EAER,WAAW,GACX,MAAM,oBAAoB,CAAC;AAG5B,MAAM,CAAC,KAAK,UAAU,OAAO,CAAC,OAAwC;IACrE,MAAM,KAAK,GAAW,EAAE,CAAC;IAEzB,SAAS,YAAY,CAAC,SAAoB;QACzC,KAAK,MAAM,CAAC,CAAC,EAAE,IAAI,CAAC,IAAI,SAAS,CAAC,KAAK,EAAE,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CAAC;QACzC,CAAC,CAAC;aACD,IAAI,CAAC,KAAK,CAAC,CAAC;QAEd,OAAO,wBAAwB,QAAQ,CAAC,IAAI,QAAQ,WAAW,MAAM,CAAC;IACvE,CAAC,CAAC;SACD,IAAI,CAAC,MAAM,CAAC,CAAC;IAEf,OAAO,GAAG,gBAAgB,IAAI,CAAC;AAChC,CAAC"}
@@ -0,0 +1,3 @@
1
+ export * from "./emitter.js";
2
+ export { $lib } from "./lib.js";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,OAAO,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from "./emitter.js";
2
+ export { $lib } from "./lib.js";
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,OAAO,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC"}
package/dist/lib.d.ts ADDED
@@ -0,0 +1,23 @@
1
+ export interface EnumEmitterOptions {
2
+ "output-dir"?: string;
3
+ "output-file"?: string;
4
+ }
5
+ export declare const $lib: {
6
+ "emitter-options-schema": {
7
+ readonly type: "object";
8
+ readonly additionalProperties: false;
9
+ readonly properties: {
10
+ readonly "output-dir": {
11
+ readonly type: "string";
12
+ readonly nullable: true;
13
+ };
14
+ readonly "output-file": {
15
+ readonly type: "string";
16
+ readonly nullable: true;
17
+ readonly default: "enums.js";
18
+ };
19
+ };
20
+ readonly required: readonly [];
21
+ };
22
+ };
23
+ //# sourceMappingURL=lib.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lib.d.ts","sourceRoot":"","sources":["../src/lib.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,kBAAkB;IAClC,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,aAAa,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,eAAO,MAAM,IAAI;;;;;;;;;;;;;;;;;CAUhB,CAAC"}
package/dist/lib.js ADDED
@@ -0,0 +1,12 @@
1
+ export const $lib = {
2
+ "emitter-options-schema": {
3
+ type: "object",
4
+ additionalProperties: false,
5
+ properties: {
6
+ "output-dir": { type: "string", nullable: true },
7
+ "output-file": { type: "string", nullable: true, default: "enums.js" },
8
+ },
9
+ required: [],
10
+ },
11
+ };
12
+ //# sourceMappingURL=lib.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lib.js","sourceRoot":"","sources":["../src/lib.ts"],"names":[],"mappings":"AAKA,MAAM,CAAC,MAAM,IAAI,GAAG;IACnB,wBAAwB,EAAE;QACzB,IAAI,EAAE,QAAQ;QACd,oBAAoB,EAAE,KAAK;QAC3B,UAAU,EAAE;YACX,YAAY,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,QAAQ,EAAE,IAAI,EAAE;YAChD,aAAa,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,QAAQ,EAAE,IAAI,EAAE,OAAO,EAAE,UAAU,EAAE;SACtE;QACD,QAAQ,EAAE,EAAE;KACH;CACV,CAAC"}
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@kattebak/typespec-enum-emitter",
3
+ "version": "1.1.0",
4
+ "description": "TypeSpec emitter for JavaScript const enums",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "publishConfig": {
8
+ "access": "public"
9
+ },
10
+ "files": [
11
+ "dist",
12
+ "tsp"
13
+ ],
14
+ "exports": {
15
+ ".": {
16
+ "types": "./dist/index.d.ts",
17
+ "default": "./dist/index.js",
18
+ "typespec": "./tsp/main.tsp"
19
+ }
20
+ },
21
+ "scripts": {
22
+ "build": "tsc",
23
+ "watch": "tsc --watch",
24
+ "test": "npm run build && npm run lint",
25
+ "fix": "npx @biomejs/biome check --write",
26
+ "lint": "npx @biomejs/biome check"
27
+ },
28
+ "devDependencies": {
29
+ "@biomejs/biome": "2.3.7",
30
+ "@typespec/compiler": "^1.4.0",
31
+ "typescript": "^5.9.3"
32
+ }
33
+ }
package/tsp/main.tsp ADDED
@@ -0,0 +1 @@
1
+ import "../dist/index.js";