@skmtc/core 0.0.785 → 0.0.787
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/esm/dsl/ContentSettings.d.ts +164 -0
- package/esm/dsl/ContentSettings.d.ts.map +1 -1
- package/esm/dsl/ContentSettings.js +151 -0
- package/esm/dsl/Definition.d.ts +199 -0
- package/esm/dsl/Definition.d.ts.map +1 -1
- package/esm/dsl/Definition.js +189 -0
- package/esm/dsl/EntityType.d.ts +83 -0
- package/esm/dsl/EntityType.d.ts.map +1 -1
- package/esm/dsl/EntityType.js +83 -0
- package/esm/dsl/Identifier.d.ts +152 -0
- package/esm/dsl/Identifier.d.ts.map +1 -1
- package/esm/dsl/Identifier.js +152 -0
- package/esm/dsl/Import.d.ts +221 -0
- package/esm/dsl/Import.d.ts.map +1 -1
- package/esm/dsl/Import.js +202 -0
- package/esm/dsl/operation/OperationBase.d.ts +181 -1
- package/esm/dsl/operation/OperationBase.d.ts.map +1 -1
- package/esm/dsl/operation/OperationBase.js +181 -1
- package/esm/oas/array/Array.d.ts +109 -1
- package/esm/oas/array/Array.d.ts.map +1 -1
- package/esm/oas/array/Array.js +93 -1
- package/esm/oas/document/Document.d.ts +155 -1
- package/esm/oas/document/Document.d.ts.map +1 -1
- package/esm/oas/document/Document.js +141 -2
- package/esm/oas/integer/Integer.d.ts +98 -0
- package/esm/oas/integer/Integer.d.ts.map +1 -1
- package/esm/oas/integer/Integer.js +80 -0
- package/esm/oas/object/Object.d.ts +255 -10
- package/esm/oas/object/Object.d.ts.map +1 -1
- package/esm/oas/object/Object.js +229 -10
- package/esm/oas/string/String.d.ts +103 -1
- package/esm/oas/string/String.d.ts.map +1 -1
- package/esm/oas/string/String.js +87 -1
- package/esm/types/Brand.d.ts +74 -0
- package/esm/types/Brand.d.ts.map +1 -1
- package/esm/types/Brand.js +9 -0
- package/esm/types/GeneratorKeys.d.ts +336 -0
- package/esm/types/GeneratorKeys.d.ts.map +1 -1
- package/esm/types/GeneratorKeys.js +243 -0
- package/esm/types/GeneratorType.d.ts +119 -0
- package/esm/types/GeneratorType.d.ts.map +1 -1
- package/esm/types/Method.d.ts +109 -0
- package/esm/types/Method.d.ts.map +1 -1
- package/esm/types/Method.js +84 -0
- package/esm/types/RefName.d.ts +61 -0
- package/esm/types/RefName.d.ts.map +1 -1
- package/package.json +1 -1
- package/script/dsl/ContentSettings.d.ts +164 -0
- package/script/dsl/ContentSettings.d.ts.map +1 -1
- package/script/dsl/ContentSettings.js +151 -0
- package/script/dsl/Definition.d.ts +199 -0
- package/script/dsl/Definition.d.ts.map +1 -1
- package/script/dsl/Definition.js +189 -0
- package/script/dsl/EntityType.d.ts +83 -0
- package/script/dsl/EntityType.d.ts.map +1 -1
- package/script/dsl/EntityType.js +83 -0
- package/script/dsl/Identifier.d.ts +152 -0
- package/script/dsl/Identifier.d.ts.map +1 -1
- package/script/dsl/Identifier.js +152 -0
- package/script/dsl/Import.d.ts +221 -0
- package/script/dsl/Import.d.ts.map +1 -1
- package/script/dsl/Import.js +202 -0
- package/script/dsl/operation/OperationBase.d.ts +181 -1
- package/script/dsl/operation/OperationBase.d.ts.map +1 -1
- package/script/dsl/operation/OperationBase.js +181 -1
- package/script/oas/array/Array.d.ts +109 -1
- package/script/oas/array/Array.d.ts.map +1 -1
- package/script/oas/array/Array.js +93 -1
- package/script/oas/document/Document.d.ts +155 -1
- package/script/oas/document/Document.d.ts.map +1 -1
- package/script/oas/document/Document.js +141 -2
- package/script/oas/integer/Integer.d.ts +98 -0
- package/script/oas/integer/Integer.d.ts.map +1 -1
- package/script/oas/integer/Integer.js +80 -0
- package/script/oas/object/Object.d.ts +255 -10
- package/script/oas/object/Object.d.ts.map +1 -1
- package/script/oas/object/Object.js +229 -10
- package/script/oas/string/String.d.ts +103 -1
- package/script/oas/string/String.d.ts.map +1 -1
- package/script/oas/string/String.js +87 -1
- package/script/types/Brand.d.ts +74 -0
- package/script/types/Brand.d.ts.map +1 -1
- package/script/types/Brand.js +9 -0
- package/script/types/GeneratorKeys.d.ts +336 -0
- package/script/types/GeneratorKeys.d.ts.map +1 -1
- package/script/types/GeneratorKeys.js +243 -0
- package/script/types/GeneratorType.d.ts +119 -0
- package/script/types/GeneratorType.d.ts.map +1 -1
- package/script/types/Method.d.ts +109 -0
- package/script/types/Method.d.ts.map +1 -1
- package/script/types/Method.js +84 -0
- package/script/types/RefName.d.ts +61 -0
- package/script/types/RefName.d.ts.map +1 -1
package/esm/dsl/Definition.js
CHANGED
|
@@ -1,26 +1,170 @@
|
|
|
1
1
|
import { ContentBase } from './ContentBase.js';
|
|
2
2
|
import { withDescription } from '../typescript/withDescription.js';
|
|
3
|
+
/**
|
|
4
|
+
* Represents a complete code definition in the SKMTC DSL system.
|
|
5
|
+
*
|
|
6
|
+
* The `Definition` class is the primary output unit of generators, representing
|
|
7
|
+
* a complete piece of generated code (like an interface, function, or constant).
|
|
8
|
+
* It combines an identifier, generated content, and optional metadata into a
|
|
9
|
+
* formatted code definition with proper TypeScript syntax.
|
|
10
|
+
*
|
|
11
|
+
* This class automatically handles export statements, type annotations, and
|
|
12
|
+
* JSDoc comment generation, making it the building block for all generated
|
|
13
|
+
* code artifacts.
|
|
14
|
+
*
|
|
15
|
+
* ## Key Features
|
|
16
|
+
*
|
|
17
|
+
* - **Automatic Exports**: Handles export keyword based on configuration
|
|
18
|
+
* - **Type Safety**: Preserves type information through generic parameters
|
|
19
|
+
* - **JSDoc Integration**: Automatic comment generation from descriptions
|
|
20
|
+
* - **Entity Type Support**: Handles different definition types (const, type, etc.)
|
|
21
|
+
* - **Identifier Management**: Integrates with the Identifier system for naming
|
|
22
|
+
*
|
|
23
|
+
* @template V - The type of generated value (preserves generator-specific types)
|
|
24
|
+
*
|
|
25
|
+
* @example Basic interface definition
|
|
26
|
+
* ```typescript
|
|
27
|
+
* import { Definition, Identifier } from '@skmtc/core';
|
|
28
|
+
*
|
|
29
|
+
* const userInterface = new Definition({
|
|
30
|
+
* context: generateContext,
|
|
31
|
+
* identifier: Identifier.createType('User'),
|
|
32
|
+
* description: 'Represents a user in the system',
|
|
33
|
+
* value: {
|
|
34
|
+
* generatorKey: 'typescript-interfaces',
|
|
35
|
+
* content: `{
|
|
36
|
+
* id: string;
|
|
37
|
+
* name: string;
|
|
38
|
+
* email: string;
|
|
39
|
+
* }`
|
|
40
|
+
* }
|
|
41
|
+
* });
|
|
42
|
+
*
|
|
43
|
+
* console.log(userInterface.toString());
|
|
44
|
+
* //
|
|
45
|
+
* // Represents a user in the system
|
|
46
|
+
* //
|
|
47
|
+
* // export type User = {
|
|
48
|
+
* // id: string;
|
|
49
|
+
* // name: string;
|
|
50
|
+
* // email: string;
|
|
51
|
+
* // };
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* @example Function definition with parameters
|
|
55
|
+
* ```typescript
|
|
56
|
+
* const apiFunction = new Definition({
|
|
57
|
+
* context: generateContext,
|
|
58
|
+
* identifier: Identifier.createVariable('fetchUser', 'Promise<User>'),
|
|
59
|
+
* description: 'Fetches a user by ID from the API',
|
|
60
|
+
* value: {
|
|
61
|
+
* generatorKey: 'api-client',
|
|
62
|
+
* content: `async (id: string) => {
|
|
63
|
+
* const response = await fetch(\`/api/users/\${id}\`);
|
|
64
|
+
* return response.json();
|
|
65
|
+
* }`
|
|
66
|
+
* }
|
|
67
|
+
* });
|
|
68
|
+
*
|
|
69
|
+
* console.log(apiFunction.toString());
|
|
70
|
+
* //
|
|
71
|
+
* // Fetches a user by ID from the API
|
|
72
|
+
* //
|
|
73
|
+
* // export const fetchUser: Promise<User> = async (id: string) => {
|
|
74
|
+
* // const response = await fetch(`/api/users/${id}`);
|
|
75
|
+
* // return response.json();
|
|
76
|
+
* // };
|
|
77
|
+
* ```
|
|
78
|
+
*
|
|
79
|
+
* @example Non-exported definition
|
|
80
|
+
* ```typescript
|
|
81
|
+
* const helperFunction = new Definition({
|
|
82
|
+
* context: generateContext,
|
|
83
|
+
* identifier: Identifier.createVariable('validateEmail'),
|
|
84
|
+
* value: {
|
|
85
|
+
* generatorKey: 'utilities',
|
|
86
|
+
* content: `(email: string): boolean => {
|
|
87
|
+
* return /^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/.test(email);
|
|
88
|
+
* }`
|
|
89
|
+
* },
|
|
90
|
+
* noExport: true // Internal helper, not exported
|
|
91
|
+
* });
|
|
92
|
+
*
|
|
93
|
+
* console.log(helperFunction.toString());
|
|
94
|
+
* // const validateEmail = (email: string): boolean => {
|
|
95
|
+
* // return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
|
|
96
|
+
* // };
|
|
97
|
+
* ```
|
|
98
|
+
*
|
|
99
|
+
* @example Using in generator classes
|
|
100
|
+
* ```typescript
|
|
101
|
+
* import { ModelBase, Definition, Identifier } from '@skmtc/core';
|
|
102
|
+
*
|
|
103
|
+
* class TypeScriptInterface extends ModelBase {
|
|
104
|
+
* toDefinition(): Definition {
|
|
105
|
+
* const schema = this.context.getSchema(this.refName);
|
|
106
|
+
*
|
|
107
|
+
* return new Definition({
|
|
108
|
+
* context: this.context,
|
|
109
|
+
* identifier: Identifier.createType(this.refName),
|
|
110
|
+
* description: schema.description,
|
|
111
|
+
* value: {
|
|
112
|
+
* generatorKey: this.generatorKey,
|
|
113
|
+
* content: this.generateInterfaceBody(schema)
|
|
114
|
+
* }
|
|
115
|
+
* });
|
|
116
|
+
* }
|
|
117
|
+
* }
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
3
120
|
export class Definition extends ContentBase {
|
|
121
|
+
/**
|
|
122
|
+
* Creates a new Definition instance.
|
|
123
|
+
*
|
|
124
|
+
* @param args - Definition configuration
|
|
125
|
+
* @param args.context - Generation context for pipeline access
|
|
126
|
+
* @param args.identifier - The identifier (name and type info) for this definition
|
|
127
|
+
* @param args.value - The generated content value
|
|
128
|
+
* @param args.description - Optional description for JSDoc comments
|
|
129
|
+
* @param args.noExport - Whether to omit the export keyword (default: false)
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```typescript
|
|
133
|
+
* const constant = new Definition({
|
|
134
|
+
* context: generateContext,
|
|
135
|
+
* identifier: Identifier.createVariable('API_BASE_URL', 'string'),
|
|
136
|
+
* description: 'Base URL for all API requests',
|
|
137
|
+
* value: {
|
|
138
|
+
* generatorKey: 'constants',
|
|
139
|
+
* content: "'https://api.example.com'"
|
|
140
|
+
* }
|
|
141
|
+
* });
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
4
144
|
constructor({ context, identifier, value, description, noExport }) {
|
|
5
145
|
super({ context, generatorKey: value.generatorKey });
|
|
146
|
+
/** The identifier for this definition */
|
|
6
147
|
Object.defineProperty(this, "identifier", {
|
|
7
148
|
enumerable: true,
|
|
8
149
|
configurable: true,
|
|
9
150
|
writable: true,
|
|
10
151
|
value: void 0
|
|
11
152
|
});
|
|
153
|
+
/** Optional description for JSDoc comments */
|
|
12
154
|
Object.defineProperty(this, "description", {
|
|
13
155
|
enumerable: true,
|
|
14
156
|
configurable: true,
|
|
15
157
|
writable: true,
|
|
16
158
|
value: void 0
|
|
17
159
|
});
|
|
160
|
+
/** The generated value content */
|
|
18
161
|
Object.defineProperty(this, "value", {
|
|
19
162
|
enumerable: true,
|
|
20
163
|
configurable: true,
|
|
21
164
|
writable: true,
|
|
22
165
|
value: void 0
|
|
23
166
|
});
|
|
167
|
+
/** Whether to skip the export keyword */
|
|
24
168
|
Object.defineProperty(this, "noExport", {
|
|
25
169
|
enumerable: true,
|
|
26
170
|
configurable: true,
|
|
@@ -32,6 +176,51 @@ export class Definition extends ContentBase {
|
|
|
32
176
|
this.description = description;
|
|
33
177
|
this.noExport = noExport;
|
|
34
178
|
}
|
|
179
|
+
/**
|
|
180
|
+
* Generates the complete TypeScript definition code.
|
|
181
|
+
*
|
|
182
|
+
* This method produces a properly formatted TypeScript definition with:
|
|
183
|
+
* - Optional JSDoc comments from the description
|
|
184
|
+
* - Export keyword (unless noExport is true)
|
|
185
|
+
* - Entity type (const, type, function, etc.)
|
|
186
|
+
* - Identifier with optional type annotation
|
|
187
|
+
* - Generated value content
|
|
188
|
+
*
|
|
189
|
+
* The output follows TypeScript syntax conventions and can be written
|
|
190
|
+
* directly to a .ts file.
|
|
191
|
+
*
|
|
192
|
+
* @returns The complete TypeScript definition as a string
|
|
193
|
+
*
|
|
194
|
+
* @example Type definition
|
|
195
|
+
* ```typescript
|
|
196
|
+
* const typeDef = new Definition({
|
|
197
|
+
* context,
|
|
198
|
+
* identifier: Identifier.createType('Status'),
|
|
199
|
+
* description: 'Possible status values',
|
|
200
|
+
* value: { generatorKey: 'types', content: "'pending' | 'complete' | 'failed'" }
|
|
201
|
+
* });
|
|
202
|
+
*
|
|
203
|
+
* console.log(typeDef.toString());
|
|
204
|
+
* //
|
|
205
|
+
* // Possible status values
|
|
206
|
+
* //
|
|
207
|
+
* // export type Status = 'pending' | 'complete' | 'failed';
|
|
208
|
+
* ```
|
|
209
|
+
*
|
|
210
|
+
* @example Constant definition
|
|
211
|
+
* ```typescript
|
|
212
|
+
* const constDef = new Definition({
|
|
213
|
+
* context,
|
|
214
|
+
* identifier: Identifier.createVariable('DEFAULT_TIMEOUT', 'number'),
|
|
215
|
+
* value: { generatorKey: 'config', content: '5000' }
|
|
216
|
+
* });
|
|
217
|
+
*
|
|
218
|
+
* console.log(constDef.toString());
|
|
219
|
+
* // export const DEFAULT_TIMEOUT: number = 5000;
|
|
220
|
+
* ```
|
|
221
|
+
*
|
|
222
|
+
* @todo Move syntax to typescript package to enable language-agnostic use
|
|
223
|
+
*/
|
|
35
224
|
toString() {
|
|
36
225
|
const identifier = this.identifier.typeName
|
|
37
226
|
? `${this.identifier.name}: ${this.identifier.typeName}`
|
package/esm/dsl/EntityType.d.ts
CHANGED
|
@@ -1,6 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents the type of a TypeScript entity for code generation.
|
|
3
|
+
*
|
|
4
|
+
* `EntityType` encapsulates the distinction between different kinds of
|
|
5
|
+
* TypeScript entities, specifically variables (which become `const` declarations)
|
|
6
|
+
* and types (which become `type` or `interface` declarations).
|
|
7
|
+
*
|
|
8
|
+
* This class is used by the {@link Identifier} system to ensure that
|
|
9
|
+
* generated code uses the correct TypeScript keywords and syntax patterns.
|
|
10
|
+
*
|
|
11
|
+
* @example Variable entity type
|
|
12
|
+
* ```typescript
|
|
13
|
+
* import { EntityType } from '@skmtc/core';
|
|
14
|
+
*
|
|
15
|
+
* const variableEntity = new EntityType('variable');
|
|
16
|
+
* console.log(variableEntity.toString()); // 'const'
|
|
17
|
+
*
|
|
18
|
+
* // Used in code generation:
|
|
19
|
+
* // const API_URL = 'https://api.example.com';
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @example Type entity type
|
|
23
|
+
* ```typescript
|
|
24
|
+
* const typeEntity = new EntityType('type');
|
|
25
|
+
* console.log(typeEntity.toString()); // 'type'
|
|
26
|
+
*
|
|
27
|
+
* // Used in code generation:
|
|
28
|
+
* // type User = { id: string; name: string; };
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @example Integration with Identifier
|
|
32
|
+
* ```typescript
|
|
33
|
+
* import { Identifier, EntityType } from '@skmtc/core';
|
|
34
|
+
*
|
|
35
|
+
* // Create a variable identifier (uses 'const' keyword)
|
|
36
|
+
* const varId = Identifier.createVariable('API_KEY', 'string');
|
|
37
|
+
* console.log(varId.entityType.toString()); // 'const'
|
|
38
|
+
*
|
|
39
|
+
* // Create a type identifier (uses 'type' keyword)
|
|
40
|
+
* const typeId = Identifier.createType('UserStatus');
|
|
41
|
+
* console.log(typeId.entityType.toString()); // 'type'
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
1
44
|
export declare class EntityType {
|
|
45
|
+
/** The entity type discriminator */
|
|
2
46
|
type: 'variable' | 'type';
|
|
47
|
+
/**
|
|
48
|
+
* Creates a new EntityType instance.
|
|
49
|
+
*
|
|
50
|
+
* @param type - The type of entity ('variable' for const declarations, 'type' for type declarations)
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* // For generating const declarations
|
|
55
|
+
* const constEntity = new EntityType('variable');
|
|
56
|
+
*
|
|
57
|
+
* // For generating type declarations
|
|
58
|
+
* const typeEntity = new EntityType('type');
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
3
61
|
constructor(type: 'variable' | 'type');
|
|
62
|
+
/**
|
|
63
|
+
* Returns the appropriate TypeScript keyword for this entity type.
|
|
64
|
+
*
|
|
65
|
+
* This method maps the entity type to the correct TypeScript declaration
|
|
66
|
+
* keyword that should be used in generated code:
|
|
67
|
+
* - 'variable' entities become 'const' declarations
|
|
68
|
+
* - 'type' entities become 'type' declarations
|
|
69
|
+
*
|
|
70
|
+
* @returns The TypeScript keyword string ('const' or 'type')
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```typescript
|
|
74
|
+
* const varEntity = new EntityType('variable');
|
|
75
|
+
* console.log(varEntity.toString()); // 'const'
|
|
76
|
+
*
|
|
77
|
+
* const typeEntity = new EntityType('type');
|
|
78
|
+
* console.log(typeEntity.toString()); // 'type'
|
|
79
|
+
*
|
|
80
|
+
* // Used in code generation:
|
|
81
|
+
* const keyword = entityType.toString();
|
|
82
|
+
* const declaration = `${keyword} ${identifier} = ${value};`;
|
|
83
|
+
* // Results in: "const API_URL = 'https://example.com';" or
|
|
84
|
+
* // Results in: "type Status = 'active' | 'inactive';"
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
4
87
|
toString(): string;
|
|
5
88
|
}
|
|
6
89
|
//# sourceMappingURL=EntityType.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EntityType.d.ts","sourceRoot":"","sources":["../../src/dsl/EntityType.ts"],"names":[],"mappings":"AAEA,qBAAa,UAAU;IACrB,IAAI,EAAE,UAAU,GAAG,MAAM,CAAA;
|
|
1
|
+
{"version":3,"file":"EntityType.d.ts","sourceRoot":"","sources":["../../src/dsl/EntityType.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,qBAAa,UAAU;IACrB,oCAAoC;IACpC,IAAI,EAAE,UAAU,GAAG,MAAM,CAAA;IAEzB;;;;;;;;;;;;;OAaG;gBACS,IAAI,EAAE,UAAU,GAAG,MAAM;IAIrC;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;IACH,QAAQ,IAAI,MAAM;CAMnB"}
|
package/esm/dsl/EntityType.js
CHANGED
|
@@ -1,6 +1,64 @@
|
|
|
1
1
|
import { match } from 'ts-pattern';
|
|
2
|
+
/**
|
|
3
|
+
* Represents the type of a TypeScript entity for code generation.
|
|
4
|
+
*
|
|
5
|
+
* `EntityType` encapsulates the distinction between different kinds of
|
|
6
|
+
* TypeScript entities, specifically variables (which become `const` declarations)
|
|
7
|
+
* and types (which become `type` or `interface` declarations).
|
|
8
|
+
*
|
|
9
|
+
* This class is used by the {@link Identifier} system to ensure that
|
|
10
|
+
* generated code uses the correct TypeScript keywords and syntax patterns.
|
|
11
|
+
*
|
|
12
|
+
* @example Variable entity type
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import { EntityType } from '@skmtc/core';
|
|
15
|
+
*
|
|
16
|
+
* const variableEntity = new EntityType('variable');
|
|
17
|
+
* console.log(variableEntity.toString()); // 'const'
|
|
18
|
+
*
|
|
19
|
+
* // Used in code generation:
|
|
20
|
+
* // const API_URL = 'https://api.example.com';
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @example Type entity type
|
|
24
|
+
* ```typescript
|
|
25
|
+
* const typeEntity = new EntityType('type');
|
|
26
|
+
* console.log(typeEntity.toString()); // 'type'
|
|
27
|
+
*
|
|
28
|
+
* // Used in code generation:
|
|
29
|
+
* // type User = { id: string; name: string; };
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* @example Integration with Identifier
|
|
33
|
+
* ```typescript
|
|
34
|
+
* import { Identifier, EntityType } from '@skmtc/core';
|
|
35
|
+
*
|
|
36
|
+
* // Create a variable identifier (uses 'const' keyword)
|
|
37
|
+
* const varId = Identifier.createVariable('API_KEY', 'string');
|
|
38
|
+
* console.log(varId.entityType.toString()); // 'const'
|
|
39
|
+
*
|
|
40
|
+
* // Create a type identifier (uses 'type' keyword)
|
|
41
|
+
* const typeId = Identifier.createType('UserStatus');
|
|
42
|
+
* console.log(typeId.entityType.toString()); // 'type'
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
2
45
|
export class EntityType {
|
|
46
|
+
/**
|
|
47
|
+
* Creates a new EntityType instance.
|
|
48
|
+
*
|
|
49
|
+
* @param type - The type of entity ('variable' for const declarations, 'type' for type declarations)
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```typescript
|
|
53
|
+
* // For generating const declarations
|
|
54
|
+
* const constEntity = new EntityType('variable');
|
|
55
|
+
*
|
|
56
|
+
* // For generating type declarations
|
|
57
|
+
* const typeEntity = new EntityType('type');
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
3
60
|
constructor(type) {
|
|
61
|
+
/** The entity type discriminator */
|
|
4
62
|
Object.defineProperty(this, "type", {
|
|
5
63
|
enumerable: true,
|
|
6
64
|
configurable: true,
|
|
@@ -9,6 +67,31 @@ export class EntityType {
|
|
|
9
67
|
});
|
|
10
68
|
this.type = type;
|
|
11
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* Returns the appropriate TypeScript keyword for this entity type.
|
|
72
|
+
*
|
|
73
|
+
* This method maps the entity type to the correct TypeScript declaration
|
|
74
|
+
* keyword that should be used in generated code:
|
|
75
|
+
* - 'variable' entities become 'const' declarations
|
|
76
|
+
* - 'type' entities become 'type' declarations
|
|
77
|
+
*
|
|
78
|
+
* @returns The TypeScript keyword string ('const' or 'type')
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```typescript
|
|
82
|
+
* const varEntity = new EntityType('variable');
|
|
83
|
+
* console.log(varEntity.toString()); // 'const'
|
|
84
|
+
*
|
|
85
|
+
* const typeEntity = new EntityType('type');
|
|
86
|
+
* console.log(typeEntity.toString()); // 'type'
|
|
87
|
+
*
|
|
88
|
+
* // Used in code generation:
|
|
89
|
+
* const keyword = entityType.toString();
|
|
90
|
+
* const declaration = `${keyword} ${identifier} = ${value};`;
|
|
91
|
+
* // Results in: "const API_URL = 'https://example.com';" or
|
|
92
|
+
* // Results in: "type Status = 'active' | 'inactive';"
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
12
95
|
toString() {
|
|
13
96
|
return match(this.type)
|
|
14
97
|
.with('variable', () => 'const')
|
package/esm/dsl/Identifier.d.ts
CHANGED
|
@@ -1,11 +1,163 @@
|
|
|
1
1
|
import { EntityType } from './EntityType.js';
|
|
2
|
+
/**
|
|
3
|
+
* Represents a TypeScript identifier in the SKMTC DSL system.
|
|
4
|
+
*
|
|
5
|
+
* The `Identifier` class encapsulates named entities in generated code,
|
|
6
|
+
* providing type-safe creation and management of variables, types, and other
|
|
7
|
+
* identifiers. It distinguishes between different kinds of identifiers and
|
|
8
|
+
* optionally tracks type information.
|
|
9
|
+
*
|
|
10
|
+
* This class uses factory methods instead of direct construction to ensure
|
|
11
|
+
* proper entity type classification and to provide a cleaner API.
|
|
12
|
+
*
|
|
13
|
+
* ## Key Features
|
|
14
|
+
*
|
|
15
|
+
* - **Type Safety**: Distinguishes between variables, types, and other entities
|
|
16
|
+
* - **Factory Methods**: Provides semantic constructors for different identifier types
|
|
17
|
+
* - **Optional Typing**: Can associate type information with variable identifiers
|
|
18
|
+
* - **String Conversion**: Clean string representation for code generation
|
|
19
|
+
*
|
|
20
|
+
* @example Creating variable identifiers
|
|
21
|
+
* ```typescript
|
|
22
|
+
* import { Identifier } from '@skmtc/core';
|
|
23
|
+
*
|
|
24
|
+
* // Simple variable without type
|
|
25
|
+
* const userName = Identifier.createVariable('userName');
|
|
26
|
+
* console.log(userName.toString()); // 'userName'
|
|
27
|
+
*
|
|
28
|
+
* // Typed variable
|
|
29
|
+
* const userId = Identifier.createVariable('userId', 'string');
|
|
30
|
+
* console.log(userId.name); // 'userId'
|
|
31
|
+
* console.log(userId.typeName); // 'string'
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @example Creating type identifiers
|
|
35
|
+
* ```typescript
|
|
36
|
+
* // Type identifier
|
|
37
|
+
* const userType = Identifier.createType('User');
|
|
38
|
+
* console.log(userType.toString()); // 'User'
|
|
39
|
+
* console.log(userType.entityType.value); // 'type'
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* @example Using in code generation
|
|
43
|
+
* ```typescript
|
|
44
|
+
* import { Definition, Identifier } from '@skmtc/core';
|
|
45
|
+
*
|
|
46
|
+
* class ApiGenerator {
|
|
47
|
+
* generateFunction(name: string) {
|
|
48
|
+
* const funcId = Identifier.createVariable(name);
|
|
49
|
+
* const requestType = Identifier.createType('RequestType');
|
|
50
|
+
*
|
|
51
|
+
* return new Definition({
|
|
52
|
+
* name: funcId.name,
|
|
53
|
+
* content: `function ${funcId}(data: ${requestType}) {
|
|
54
|
+
* // Implementation
|
|
55
|
+
* }`
|
|
56
|
+
* });
|
|
57
|
+
* }
|
|
58
|
+
* }
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
2
61
|
export declare class Identifier {
|
|
62
|
+
/** The identifier name */
|
|
3
63
|
name: string;
|
|
64
|
+
/** The entity type (variable, type, etc.) */
|
|
4
65
|
entityType: EntityType;
|
|
66
|
+
/** Optional type name for typed variables */
|
|
5
67
|
typeName?: string;
|
|
68
|
+
/**
|
|
69
|
+
* Creates a new Identifier instance.
|
|
70
|
+
*
|
|
71
|
+
* This constructor is private to enforce the use of factory methods
|
|
72
|
+
* that provide better semantic clarity and type safety.
|
|
73
|
+
*
|
|
74
|
+
* @param args - Identifier configuration
|
|
75
|
+
*/
|
|
6
76
|
private constructor();
|
|
77
|
+
/**
|
|
78
|
+
* Creates a variable identifier with optional type information.
|
|
79
|
+
*
|
|
80
|
+
* This factory method creates an identifier for variables, constants,
|
|
81
|
+
* function parameters, and other value-based entities. Optionally
|
|
82
|
+
* associates type information for typed variables.
|
|
83
|
+
*
|
|
84
|
+
* @param name - The variable name
|
|
85
|
+
* @param typeName - Optional type name for the variable
|
|
86
|
+
* @returns A new variable Identifier instance
|
|
87
|
+
*
|
|
88
|
+
* @example Untyped variable
|
|
89
|
+
* ```typescript
|
|
90
|
+
* const count = Identifier.createVariable('count');
|
|
91
|
+
* console.log(count.name); // 'count'
|
|
92
|
+
* console.log(count.typeName); // undefined
|
|
93
|
+
* ```
|
|
94
|
+
*
|
|
95
|
+
* @example Typed variable
|
|
96
|
+
* ```typescript
|
|
97
|
+
* const userId = Identifier.createVariable('userId', 'string');
|
|
98
|
+
* console.log(userId.name); // 'userId'
|
|
99
|
+
* console.log(userId.typeName); // 'string'
|
|
100
|
+
* ```
|
|
101
|
+
*
|
|
102
|
+
* @example In function generation
|
|
103
|
+
* ```typescript
|
|
104
|
+
* const param = Identifier.createVariable('data', 'RequestData');
|
|
105
|
+
* const funcDef = `function processRequest(${param.name}: ${param.typeName}) {}`;
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
7
108
|
static createVariable(name: string, typeName?: string): Identifier;
|
|
109
|
+
/**
|
|
110
|
+
* Creates a type identifier for TypeScript types.
|
|
111
|
+
*
|
|
112
|
+
* This factory method creates an identifier for type entities like
|
|
113
|
+
* interfaces, type aliases, classes, and other type-level constructs.
|
|
114
|
+
* Type identifiers don't have associated type information since they
|
|
115
|
+
* represent types themselves.
|
|
116
|
+
*
|
|
117
|
+
* @param name - The type name
|
|
118
|
+
* @returns A new type Identifier instance
|
|
119
|
+
*
|
|
120
|
+
* @example Interface type
|
|
121
|
+
* ```typescript
|
|
122
|
+
* const userInterface = Identifier.createType('User');
|
|
123
|
+
* console.log(userInterface.name); // 'User'
|
|
124
|
+
* console.log(userInterface.entityType.value); // 'type'
|
|
125
|
+
* ```
|
|
126
|
+
*
|
|
127
|
+
* @example Type alias
|
|
128
|
+
* ```typescript
|
|
129
|
+
* const statusType = Identifier.createType('Status');
|
|
130
|
+
* const typeDef = `type ${statusType} = 'pending' | 'complete'`;
|
|
131
|
+
* ```
|
|
132
|
+
*
|
|
133
|
+
* @example Generic type
|
|
134
|
+
* ```typescript
|
|
135
|
+
* const responseType = Identifier.createType('ApiResponse');
|
|
136
|
+
* const genericDef = `interface ${responseType}<T> { data: T; success: boolean; }`;
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
8
139
|
static createType(name: string): Identifier;
|
|
140
|
+
/**
|
|
141
|
+
* Returns the string representation of the identifier.
|
|
142
|
+
*
|
|
143
|
+
* This method simply returns the identifier name, which is the most
|
|
144
|
+
* common usage when generating code. The name can be used directly
|
|
145
|
+
* in code generation contexts.
|
|
146
|
+
*
|
|
147
|
+
* @returns The identifier name as a string
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* ```typescript
|
|
151
|
+
* const variable = Identifier.createVariable('userName');
|
|
152
|
+
* const typeId = Identifier.createType('User');
|
|
153
|
+
*
|
|
154
|
+
* console.log(variable.toString()); // 'userName'
|
|
155
|
+
* console.log(typeId.toString()); // 'User'
|
|
156
|
+
*
|
|
157
|
+
* // Can be used directly in template strings
|
|
158
|
+
* const code = `const ${variable}: ${typeId} = getUserData();`;
|
|
159
|
+
* ```
|
|
160
|
+
*/
|
|
9
161
|
toString(): string;
|
|
10
162
|
}
|
|
11
163
|
//# sourceMappingURL=Identifier.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Identifier.d.ts","sourceRoot":"","sources":["../../src/dsl/Identifier.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;
|
|
1
|
+
{"version":3,"file":"Identifier.d.ts","sourceRoot":"","sources":["../../src/dsl/Identifier.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAc5C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0DG;AACH,qBAAa,UAAU;IACrB,0BAA0B;IAC1B,IAAI,EAAE,MAAM,CAAA;IAEZ,6CAA6C;IAC7C,UAAU,EAAE,UAAU,CAAA;IAEtB,6CAA6C;IAC7C,QAAQ,CAAC,EAAE,MAAM,CAAA;IAEjB;;;;;;;OAOG;IACH,OAAO;IAMP;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA8BG;IACH,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,UAAU;IAelE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA6BG;IACH,MAAM,CAAC,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU;IAO3C;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,QAAQ,IAAI,MAAM;CAGnB"}
|