@atscript/typescript 0.1.17 → 0.1.19

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);
@@ -242,20 +242,20 @@ else {
242
242
  passed = false;
243
243
  if (this.isLimitExceeded()) return false;
244
244
  }
245
- } else if (this.opts.unknwonProps !== "ignore") {
246
- if (this.opts.unknwonProps === "error") {
245
+ } else if (this.opts.unknownProps !== "ignore") {
246
+ if (this.opts.unknownProps === "error") {
247
247
  this.push(key);
248
248
  this.error(`Unexpected property`);
249
249
  this.pop(true);
250
250
  if (this.isLimitExceeded()) return false;
251
251
  passed = false;
252
- } else if (this.opts.unknwonProps === "strip") delete value[key];
252
+ } else if (this.opts.unknownProps === "strip") delete value[key];
253
253
  }
254
254
  }
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
  }
@@ -395,7 +401,7 @@ else {
395
401
  this.stackPath = [];
396
402
  this.opts = {
397
403
  partial: false,
398
- unknwonProps: "error",
404
+ unknownProps: "error",
399
405
  errorLimit: 10,
400
406
  ...opts,
401
407
  plugins: opts?.plugins || []
@@ -565,7 +571,7 @@ function buildJsonSchema(type) {
565
571
  type: "object",
566
572
  properties
567
573
  };
568
- if (required.length) schema.required = required;
574
+ if (required.length > 0) schema.required = required;
569
575
  return schema;
570
576
  },
571
577
  array(d) {
@@ -742,28 +748,32 @@ else flatUnion.item(existing);
742
748
  }
743
749
  case "union":
744
750
  case "intersection":
745
- case "tuple":
751
+ case "tuple": {
746
752
  for (const item of def.type.items) flattenArray(item, name);
747
753
  break;
748
- case "array":
754
+ }
755
+ case "array": {
749
756
  flattenArray(def.type.of, name);
750
757
  break;
758
+ }
751
759
  default:
752
760
  }
753
761
  }
754
762
  function flattenType(def, prefix = "", inComplexTypeOrArray = false) {
755
763
  switch (def.type.kind) {
756
- case "object":
764
+ case "object": {
757
765
  addFieldToFlatMap(prefix || "", def);
758
766
  for (const [key, value] of def.type.props.entries()) {
759
767
  if (skipPhantom && isPhantomType(value)) continue;
760
768
  flattenType(value, prefix ? `${prefix}.${key}` : key, inComplexTypeOrArray);
761
769
  }
762
770
  break;
771
+ }
763
772
  case "array": {
764
773
  let typeArray = def;
765
774
  if (!inComplexTypeOrArray) {
766
775
  typeArray = defineAnnotatedType().refTo(def).copyMetadata(def.metadata).$type;
776
+ if (def.optional) typeArray.optional = def.optional;
767
777
  if (options?.topLevelArrayTag) typeArray.metadata.set(options.topLevelArrayTag, true);
768
778
  }
769
779
  addFieldToFlatMap(prefix || "", typeArray);
@@ -772,10 +782,19 @@ else flatUnion.item(existing);
772
782
  }
773
783
  case "intersection":
774
784
  case "tuple":
775
- case "union": for (const item of def.type.items) flattenType(item, prefix, true);
776
- default:
785
+ case "union": {
786
+ for (const item of def.type.items) flattenType(item, prefix, true);
777
787
  addFieldToFlatMap(prefix || "", def);
788
+ if (def.optional) {
789
+ const entry = flatMap.get(prefix || "");
790
+ if (entry) entry.optional = def.optional;
791
+ }
778
792
  break;
793
+ }
794
+ default: {
795
+ addFieldToFlatMap(prefix || "", def);
796
+ break;
797
+ }
779
798
  }
780
799
  if (prefix) options?.onField?.(prefix, def, def.metadata);
781
800
  }
package/dist/utils.d.ts CHANGED
@@ -15,7 +15,7 @@ interface TValidatorOptions {
15
15
  partial: boolean | 'deep' | ((type: TAtscriptAnnotatedType<TAtscriptTypeObject>, path: string) => boolean);
16
16
  replace?: (type: TAtscriptAnnotatedType, path: string) => TAtscriptAnnotatedType;
17
17
  plugins: TValidatorPlugin[];
18
- unknwonProps: 'strip' | 'ignore' | 'error';
18
+ unknownProps: 'strip' | 'ignore' | 'error';
19
19
  errorLimit: number;
20
20
  skipList?: Set<string>;
21
21
  }
@@ -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);
@@ -241,20 +241,20 @@ else {
241
241
  passed = false;
242
242
  if (this.isLimitExceeded()) return false;
243
243
  }
244
- } else if (this.opts.unknwonProps !== "ignore") {
245
- if (this.opts.unknwonProps === "error") {
244
+ } else if (this.opts.unknownProps !== "ignore") {
245
+ if (this.opts.unknownProps === "error") {
246
246
  this.push(key);
247
247
  this.error(`Unexpected property`);
248
248
  this.pop(true);
249
249
  if (this.isLimitExceeded()) return false;
250
250
  passed = false;
251
- } else if (this.opts.unknwonProps === "strip") delete value[key];
251
+ } else if (this.opts.unknownProps === "strip") delete value[key];
252
252
  }
253
253
  }
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
  }
@@ -394,7 +400,7 @@ else {
394
400
  this.stackPath = [];
395
401
  this.opts = {
396
402
  partial: false,
397
- unknwonProps: "error",
403
+ unknownProps: "error",
398
404
  errorLimit: 10,
399
405
  ...opts,
400
406
  plugins: opts?.plugins || []
@@ -564,7 +570,7 @@ function buildJsonSchema(type) {
564
570
  type: "object",
565
571
  properties
566
572
  };
567
- if (required.length) schema.required = required;
573
+ if (required.length > 0) schema.required = required;
568
574
  return schema;
569
575
  },
570
576
  array(d) {
@@ -741,28 +747,32 @@ else flatUnion.item(existing);
741
747
  }
742
748
  case "union":
743
749
  case "intersection":
744
- case "tuple":
750
+ case "tuple": {
745
751
  for (const item of def.type.items) flattenArray(item, name);
746
752
  break;
747
- case "array":
753
+ }
754
+ case "array": {
748
755
  flattenArray(def.type.of, name);
749
756
  break;
757
+ }
750
758
  default:
751
759
  }
752
760
  }
753
761
  function flattenType(def, prefix = "", inComplexTypeOrArray = false) {
754
762
  switch (def.type.kind) {
755
- case "object":
763
+ case "object": {
756
764
  addFieldToFlatMap(prefix || "", def);
757
765
  for (const [key, value] of def.type.props.entries()) {
758
766
  if (skipPhantom && isPhantomType(value)) continue;
759
767
  flattenType(value, prefix ? `${prefix}.${key}` : key, inComplexTypeOrArray);
760
768
  }
761
769
  break;
770
+ }
762
771
  case "array": {
763
772
  let typeArray = def;
764
773
  if (!inComplexTypeOrArray) {
765
774
  typeArray = defineAnnotatedType().refTo(def).copyMetadata(def.metadata).$type;
775
+ if (def.optional) typeArray.optional = def.optional;
766
776
  if (options?.topLevelArrayTag) typeArray.metadata.set(options.topLevelArrayTag, true);
767
777
  }
768
778
  addFieldToFlatMap(prefix || "", typeArray);
@@ -771,10 +781,19 @@ else flatUnion.item(existing);
771
781
  }
772
782
  case "intersection":
773
783
  case "tuple":
774
- case "union": for (const item of def.type.items) flattenType(item, prefix, true);
775
- default:
784
+ case "union": {
785
+ for (const item of def.type.items) flattenType(item, prefix, true);
776
786
  addFieldToFlatMap(prefix || "", def);
787
+ if (def.optional) {
788
+ const entry = flatMap.get(prefix || "");
789
+ if (entry) entry.optional = def.optional;
790
+ }
777
791
  break;
792
+ }
793
+ default: {
794
+ addFieldToFlatMap(prefix || "", def);
795
+ break;
796
+ }
778
797
  }
779
798
  if (prefix) options?.onField?.(prefix, def, def.metadata);
780
799
  }
package/package.json CHANGED
@@ -1,10 +1,43 @@
1
1
  {
2
2
  "name": "@atscript/typescript",
3
- "version": "0.1.17",
3
+ "version": "0.1.19",
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.19"
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.17"
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"