@atscript/typescript 0.1.16 → 0.1.18

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/dist/utils.cjs CHANGED
@@ -140,7 +140,7 @@ var Validator = class {
140
140
  }
141
141
  let i = 0;
142
142
  for (const item of def.type.items) {
143
- this.push(`[${i}]`);
143
+ this.push(String(i));
144
144
  if (!this.validateSafe(item, value[i])) {
145
145
  this.pop(true);
146
146
  return false;
@@ -176,7 +176,7 @@ var Validator = class {
176
176
  let i = 0;
177
177
  let passed = true;
178
178
  for (const item of value) {
179
- this.push(`[${i}]`);
179
+ this.push(String(i));
180
180
  if (!this.validateSafe(def.type.of, item)) {
181
181
  passed = false;
182
182
  this.pop(true);
@@ -228,7 +228,7 @@ else {
228
228
  pattern,
229
229
  def: propDef
230
230
  });
231
- if (matched.length) {
231
+ if (matched.length > 0) {
232
232
  let keyPassed = false;
233
233
  for (const { def: def$1 } of matched) if (this.validateSafe(def$1, value[key])) {
234
234
  this.pop(false);
@@ -255,7 +255,7 @@ else {
255
255
  return passed;
256
256
  }
257
257
  validatePrimitive(def, value) {
258
- if (typeof def.type.value !== "undefined") {
258
+ if (def.type.value !== undefined) {
259
259
  if (value !== def.type.value) {
260
260
  this.error(`Expected ${def.type.value}, got ${value}`);
261
261
  return false;
@@ -264,40 +264,46 @@ else {
264
264
  }
265
265
  const typeOfValue = Array.isArray(value) ? "array" : typeof value;
266
266
  switch (def.type.designType) {
267
- case "never":
267
+ case "never": {
268
268
  this.error(`This type is impossible, must be an internal problem`);
269
269
  return false;
270
+ }
270
271
  case "any": return true;
271
- case "string":
272
+ case "string": {
272
273
  if (typeOfValue !== def.type.designType) {
273
274
  this.error(`Expected ${def.type.designType}, got ${typeOfValue}`);
274
275
  return false;
275
276
  }
276
277
  return this.validateString(def, value);
277
- case "number":
278
+ }
279
+ case "number": {
278
280
  if (typeOfValue !== def.type.designType) {
279
281
  this.error(`Expected ${def.type.designType}, got ${typeOfValue}`);
280
282
  return false;
281
283
  }
282
284
  return this.validateNumber(def, value);
283
- case "boolean":
285
+ }
286
+ case "boolean": {
284
287
  if (typeOfValue !== def.type.designType) {
285
288
  this.error(`Expected ${def.type.designType}, got ${typeOfValue}`);
286
289
  return false;
287
290
  }
288
291
  return this.validateBoolean(def, value);
289
- case "undefined":
292
+ }
293
+ case "undefined": {
290
294
  if (value !== undefined) {
291
295
  this.error(`Expected ${def.type.designType}, got ${typeOfValue}`);
292
296
  return false;
293
297
  }
294
298
  return true;
295
- case "null":
299
+ }
300
+ case "null": {
296
301
  if (value !== null) {
297
302
  this.error(`Expected ${def.type.designType}, got ${typeOfValue}`);
298
303
  return false;
299
304
  }
300
305
  return true;
306
+ }
301
307
  default: throw new Error(`Unknown type "${def.type.designType}"`);
302
308
  }
303
309
  }
@@ -509,7 +515,6 @@ else throw new Error(`Can't find prop ${typeName}${keys}`);
509
515
  }
510
516
  if (!newBase && keys) throw new Error(`Can't find prop ${typeName}${keys}`);
511
517
  else if (!newBase) throw new Error(`"${typeName}" is not annotated type`);
512
- for (const [k, v] of newBase.metadata.entries()) if (!metadata.has(k)) metadata.set(k, Array.isArray(v) ? [...v] : v);
513
518
  this.$type = {
514
519
  __is_atscript_annotated_type: true,
515
520
  type: newBase.type,
@@ -566,7 +571,7 @@ function buildJsonSchema(type) {
566
571
  type: "object",
567
572
  properties
568
573
  };
569
- if (required.length) schema.required = required;
574
+ if (required.length > 0) schema.required = required;
570
575
  return schema;
571
576
  },
572
577
  array(d) {
@@ -743,28 +748,32 @@ else flatUnion.item(existing);
743
748
  }
744
749
  case "union":
745
750
  case "intersection":
746
- case "tuple":
751
+ case "tuple": {
747
752
  for (const item of def.type.items) flattenArray(item, name);
748
753
  break;
749
- case "array":
754
+ }
755
+ case "array": {
750
756
  flattenArray(def.type.of, name);
751
757
  break;
758
+ }
752
759
  default:
753
760
  }
754
761
  }
755
762
  function flattenType(def, prefix = "", inComplexTypeOrArray = false) {
756
763
  switch (def.type.kind) {
757
- case "object":
764
+ case "object": {
758
765
  addFieldToFlatMap(prefix || "", def);
759
766
  for (const [key, value] of def.type.props.entries()) {
760
767
  if (skipPhantom && isPhantomType(value)) continue;
761
768
  flattenType(value, prefix ? `${prefix}.${key}` : key, inComplexTypeOrArray);
762
769
  }
763
770
  break;
771
+ }
764
772
  case "array": {
765
773
  let typeArray = def;
766
774
  if (!inComplexTypeOrArray) {
767
775
  typeArray = defineAnnotatedType().refTo(def).copyMetadata(def.metadata).$type;
776
+ if (def.optional) typeArray.optional = def.optional;
768
777
  if (options?.topLevelArrayTag) typeArray.metadata.set(options.topLevelArrayTag, true);
769
778
  }
770
779
  addFieldToFlatMap(prefix || "", typeArray);
@@ -773,10 +782,19 @@ else flatUnion.item(existing);
773
782
  }
774
783
  case "intersection":
775
784
  case "tuple":
776
- case "union": for (const item of def.type.items) flattenType(item, prefix, true);
777
- default:
785
+ case "union": {
786
+ for (const item of def.type.items) flattenType(item, prefix, true);
778
787
  addFieldToFlatMap(prefix || "", def);
788
+ if (def.optional) {
789
+ const entry = flatMap.get(prefix || "");
790
+ if (entry) entry.optional = def.optional;
791
+ }
779
792
  break;
793
+ }
794
+ default: {
795
+ addFieldToFlatMap(prefix || "", def);
796
+ break;
797
+ }
780
798
  }
781
799
  if (prefix) options?.onField?.(prefix, def, def.metadata);
782
800
  }
package/dist/utils.d.ts CHANGED
@@ -119,10 +119,10 @@ interface TAtscriptTypeArray<DataType = unknown[]> {
119
119
  interface TAtscriptTypeObject<K extends string = string, DataType = Record<K, unknown>> {
120
120
  kind: 'object';
121
121
  props: Map<K, TAtscriptAnnotatedType>;
122
- propsPatterns: {
122
+ propsPatterns: Array<{
123
123
  pattern: RegExp;
124
124
  def: TAtscriptAnnotatedType;
125
- }[];
125
+ }>;
126
126
  tags: Set<AtscriptPrimitiveTags>;
127
127
  /** @internal phantom — carries the DataType at the type level, never set at runtime */
128
128
  __dataType?: DataType;
@@ -176,7 +176,7 @@ declare function isAnnotatedType(type: any): type is TAtscriptAnnotatedType;
176
176
  * Standalone annotate function that handles both replace and append (array) strategies.
177
177
  * Used by the handle's .annotate() method and by generated mutation statements.
178
178
  */
179
- declare function annotate<K extends keyof AtscriptMetadata>(metadata: TMetadataMap<AtscriptMetadata> | undefined, key: K, value: AtscriptMetadata[K] extends (infer E)[] ? E : AtscriptMetadata[K], asArray?: boolean): void;
179
+ declare function annotate<K extends keyof AtscriptMetadata>(metadata: TMetadataMap<AtscriptMetadata> | undefined, key: K, value: AtscriptMetadata[K] extends Array<infer E> ? E : AtscriptMetadata[K], asArray?: boolean): void;
180
180
  type TKind = '' | 'array' | 'object' | 'union' | 'intersection' | 'tuple';
181
181
  /**
182
182
  * Creates a builder handle for constructing a {@link TAtscriptAnnotatedType} at runtime.
@@ -368,13 +368,13 @@ interface TSerializedTypeFinal {
368
368
  interface TSerializedTypeObject {
369
369
  kind: 'object';
370
370
  props: Record<string, TSerializedAnnotatedTypeInner>;
371
- propsPatterns: {
371
+ propsPatterns: Array<{
372
372
  pattern: {
373
373
  source: string;
374
374
  flags: string;
375
375
  };
376
376
  def: TSerializedAnnotatedTypeInner;
377
- }[];
377
+ }>;
378
378
  tags: string[];
379
379
  }
380
380
  interface TSerializedTypeArray {
package/dist/utils.mjs CHANGED
@@ -139,7 +139,7 @@ var Validator = class {
139
139
  }
140
140
  let i = 0;
141
141
  for (const item of def.type.items) {
142
- this.push(`[${i}]`);
142
+ this.push(String(i));
143
143
  if (!this.validateSafe(item, value[i])) {
144
144
  this.pop(true);
145
145
  return false;
@@ -175,7 +175,7 @@ var Validator = class {
175
175
  let i = 0;
176
176
  let passed = true;
177
177
  for (const item of value) {
178
- this.push(`[${i}]`);
178
+ this.push(String(i));
179
179
  if (!this.validateSafe(def.type.of, item)) {
180
180
  passed = false;
181
181
  this.pop(true);
@@ -227,7 +227,7 @@ else {
227
227
  pattern,
228
228
  def: propDef
229
229
  });
230
- if (matched.length) {
230
+ if (matched.length > 0) {
231
231
  let keyPassed = false;
232
232
  for (const { def: def$1 } of matched) if (this.validateSafe(def$1, value[key])) {
233
233
  this.pop(false);
@@ -254,7 +254,7 @@ else {
254
254
  return passed;
255
255
  }
256
256
  validatePrimitive(def, value) {
257
- if (typeof def.type.value !== "undefined") {
257
+ if (def.type.value !== undefined) {
258
258
  if (value !== def.type.value) {
259
259
  this.error(`Expected ${def.type.value}, got ${value}`);
260
260
  return false;
@@ -263,40 +263,46 @@ else {
263
263
  }
264
264
  const typeOfValue = Array.isArray(value) ? "array" : typeof value;
265
265
  switch (def.type.designType) {
266
- case "never":
266
+ case "never": {
267
267
  this.error(`This type is impossible, must be an internal problem`);
268
268
  return false;
269
+ }
269
270
  case "any": return true;
270
- case "string":
271
+ case "string": {
271
272
  if (typeOfValue !== def.type.designType) {
272
273
  this.error(`Expected ${def.type.designType}, got ${typeOfValue}`);
273
274
  return false;
274
275
  }
275
276
  return this.validateString(def, value);
276
- case "number":
277
+ }
278
+ case "number": {
277
279
  if (typeOfValue !== def.type.designType) {
278
280
  this.error(`Expected ${def.type.designType}, got ${typeOfValue}`);
279
281
  return false;
280
282
  }
281
283
  return this.validateNumber(def, value);
282
- case "boolean":
284
+ }
285
+ case "boolean": {
283
286
  if (typeOfValue !== def.type.designType) {
284
287
  this.error(`Expected ${def.type.designType}, got ${typeOfValue}`);
285
288
  return false;
286
289
  }
287
290
  return this.validateBoolean(def, value);
288
- case "undefined":
291
+ }
292
+ case "undefined": {
289
293
  if (value !== undefined) {
290
294
  this.error(`Expected ${def.type.designType}, got ${typeOfValue}`);
291
295
  return false;
292
296
  }
293
297
  return true;
294
- case "null":
298
+ }
299
+ case "null": {
295
300
  if (value !== null) {
296
301
  this.error(`Expected ${def.type.designType}, got ${typeOfValue}`);
297
302
  return false;
298
303
  }
299
304
  return true;
305
+ }
300
306
  default: throw new Error(`Unknown type "${def.type.designType}"`);
301
307
  }
302
308
  }
@@ -508,7 +514,6 @@ else throw new Error(`Can't find prop ${typeName}${keys}`);
508
514
  }
509
515
  if (!newBase && keys) throw new Error(`Can't find prop ${typeName}${keys}`);
510
516
  else if (!newBase) throw new Error(`"${typeName}" is not annotated type`);
511
- for (const [k, v] of newBase.metadata.entries()) if (!metadata.has(k)) metadata.set(k, Array.isArray(v) ? [...v] : v);
512
517
  this.$type = {
513
518
  __is_atscript_annotated_type: true,
514
519
  type: newBase.type,
@@ -565,7 +570,7 @@ function buildJsonSchema(type) {
565
570
  type: "object",
566
571
  properties
567
572
  };
568
- if (required.length) schema.required = required;
573
+ if (required.length > 0) schema.required = required;
569
574
  return schema;
570
575
  },
571
576
  array(d) {
@@ -742,28 +747,32 @@ else flatUnion.item(existing);
742
747
  }
743
748
  case "union":
744
749
  case "intersection":
745
- case "tuple":
750
+ case "tuple": {
746
751
  for (const item of def.type.items) flattenArray(item, name);
747
752
  break;
748
- case "array":
753
+ }
754
+ case "array": {
749
755
  flattenArray(def.type.of, name);
750
756
  break;
757
+ }
751
758
  default:
752
759
  }
753
760
  }
754
761
  function flattenType(def, prefix = "", inComplexTypeOrArray = false) {
755
762
  switch (def.type.kind) {
756
- case "object":
763
+ case "object": {
757
764
  addFieldToFlatMap(prefix || "", def);
758
765
  for (const [key, value] of def.type.props.entries()) {
759
766
  if (skipPhantom && isPhantomType(value)) continue;
760
767
  flattenType(value, prefix ? `${prefix}.${key}` : key, inComplexTypeOrArray);
761
768
  }
762
769
  break;
770
+ }
763
771
  case "array": {
764
772
  let typeArray = def;
765
773
  if (!inComplexTypeOrArray) {
766
774
  typeArray = defineAnnotatedType().refTo(def).copyMetadata(def.metadata).$type;
775
+ if (def.optional) typeArray.optional = def.optional;
767
776
  if (options?.topLevelArrayTag) typeArray.metadata.set(options.topLevelArrayTag, true);
768
777
  }
769
778
  addFieldToFlatMap(prefix || "", typeArray);
@@ -772,10 +781,19 @@ else flatUnion.item(existing);
772
781
  }
773
782
  case "intersection":
774
783
  case "tuple":
775
- case "union": for (const item of def.type.items) flattenType(item, prefix, true);
776
- default:
784
+ case "union": {
785
+ for (const item of def.type.items) flattenType(item, prefix, true);
777
786
  addFieldToFlatMap(prefix || "", def);
787
+ if (def.optional) {
788
+ const entry = flatMap.get(prefix || "");
789
+ if (entry) entry.optional = def.optional;
790
+ }
778
791
  break;
792
+ }
793
+ default: {
794
+ addFieldToFlatMap(prefix || "", def);
795
+ break;
796
+ }
779
797
  }
780
798
  if (prefix) options?.onField?.(prefix, def, def.metadata);
781
799
  }
package/package.json CHANGED
@@ -1,10 +1,43 @@
1
1
  {
2
2
  "name": "@atscript/typescript",
3
- "version": "0.1.16",
3
+ "version": "0.1.18",
4
4
  "description": "Atscript: typescript-gen support.",
5
+ "keywords": [
6
+ "annotations",
7
+ "atscript",
8
+ "typescript"
9
+ ],
10
+ "homepage": "https://github.com/moostjs/atscript/tree/main/packages/typescript#readme",
11
+ "bugs": {
12
+ "url": "https://github.com/moostjs/atscript/issues"
13
+ },
14
+ "license": "ISC",
15
+ "author": "Artem Maltsev",
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/moostjs/atscript.git",
19
+ "directory": "packages/typescript"
20
+ },
21
+ "bin": {
22
+ "asc": "./cli.cjs"
23
+ },
24
+ "files": [
25
+ "dist",
26
+ "cli.cjs"
27
+ ],
5
28
  "type": "module",
6
29
  "main": "dist/index.mjs",
7
30
  "types": "dist/index.d.ts",
31
+ "typesVersions": {
32
+ "*": {
33
+ "utils": [
34
+ "dist/utils.d.ts"
35
+ ],
36
+ "": [
37
+ "dist/index.d.ts"
38
+ ]
39
+ }
40
+ },
8
41
  "exports": {
9
42
  ".": {
10
43
  "types": "./dist/index.d.ts",
@@ -20,22 +53,15 @@
20
53
  },
21
54
  "./package.json": "./package.json"
22
55
  },
23
- "typesVersions": {
24
- "*": {
25
- "utils": [
26
- "dist/utils.d.ts"
27
- ],
28
- "": [
29
- "dist/index.d.ts"
30
- ]
31
- }
56
+ "dependencies": {
57
+ "@moostjs/event-cli": "^0.5.32",
58
+ "moost": "^0.5.32"
32
59
  },
33
- "files": [
34
- "dist",
35
- "cli.cjs"
36
- ],
37
- "bin": {
38
- "asc": "./cli.cjs"
60
+ "devDependencies": {
61
+ "vitest": "3.2.4"
62
+ },
63
+ "peerDependencies": {
64
+ "@atscript/core": "^0.1.18"
39
65
  },
40
66
  "build": [
41
67
  {},
@@ -53,32 +79,6 @@
53
79
  "dts": false
54
80
  }
55
81
  ],
56
- "keywords": [
57
- "atscript",
58
- "annotations",
59
- "typescript"
60
- ],
61
- "author": "Artem Maltsev",
62
- "repository": {
63
- "type": "git",
64
- "url": "git+https://github.com/moostjs/atscript.git",
65
- "directory": "packages/typescript"
66
- },
67
- "bugs": {
68
- "url": "https://github.com/moostjs/atscript/issues"
69
- },
70
- "homepage": "https://github.com/moostjs/atscript/tree/main/packages/typescript#readme",
71
- "license": "ISC",
72
- "peerDependencies": {
73
- "@atscript/core": "^0.1.16"
74
- },
75
- "dependencies": {
76
- "@moostjs/event-cli": "^0.5.32",
77
- "moost": "^0.5.32"
78
- },
79
- "devDependencies": {
80
- "vitest": "3.2.4"
81
- },
82
82
  "scripts": {
83
83
  "pub": "pnpm publish --access public",
84
84
  "test": "vitest"