@atscript/typescript 0.0.29 → 0.0.31

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/cli.cjs CHANGED
@@ -204,7 +204,7 @@ function wrapProp(name) {
204
204
  return name;
205
205
  }
206
206
  function escapeQuotes(str) {
207
- return str.replace(/"/g, "\\\"").replace(/\\/g, "\\\\");
207
+ return str.replace(/\\/g, "\\\\").replace(/"/g, "\\\"");
208
208
  }
209
209
 
210
210
  //#endregion
@@ -229,7 +229,7 @@ var TypeRenderer = class TypeRenderer extends BaseRenderer {
229
229
  this.writeln(" * Do not edit this file!");
230
230
  this.writeln(" */");
231
231
  this.writeln();
232
- this.writeln("import type { TAtscriptTypeObject, TAtscriptTypeComplex, TAtscriptTypeFinal, TAtscriptTypeArray, TMetadataMap, Validator, TAtscriptAnnotatedTypeConstructor, TValidatorOptions } from \"@atscript/typescript\"");
232
+ this.writeln("import type { TAtscriptTypeObject, TAtscriptTypeComplex, TAtscriptTypeFinal, TAtscriptTypeArray, TMetadataMap, Validator, TAtscriptAnnotatedTypeConstructor, TValidatorOptions } from \"@atscript/typescript/utils\"");
233
233
  }
234
234
  post() {
235
235
  this.writeln("// prettier-ignore-end");
@@ -502,7 +502,7 @@ var Validator = class {
502
502
  }
503
503
  validateTuple(def, value) {
504
504
  if (!Array.isArray(value) || value.length !== def.type.items.length) {
505
- this.error("Expected array of length " + def.type.items.length);
505
+ this.error(`Expected array of length ${def.type.items.length}`);
506
506
  return false;
507
507
  }
508
508
  let i = 0;
@@ -555,7 +555,7 @@ var Validator = class {
555
555
  const typeKeys = new Set();
556
556
  const skipList = new Set();
557
557
  if (this.opts.skipList) {
558
- const path$3 = this.stackPath.length > 1 ? this.path + "." : "";
558
+ const path$3 = this.stackPath.length > 1 ? `${this.path}.` : "";
559
559
  this.opts.skipList.forEach((item) => {
560
560
  if (item.startsWith(path$3)) {
561
561
  const key = item.slice(path$3.length);
@@ -945,7 +945,7 @@ var JsRenderer = class extends BaseRenderer {
945
945
  this.writeln("/* eslint-disable */");
946
946
  const imports = ["defineAnnotatedType as $"];
947
947
  if (!this.opts?.preRenderJsonSchema) imports.push("buildJsonSchema as $$");
948
- this.writeln(`import { ${imports.join(", ")} } from "@atscript/typescript"`);
948
+ this.writeln(`import { ${imports.join(", ")} } from "@atscript/typescript/utils"`);
949
949
  }
950
950
  post() {
951
951
  for (const node of this.postAnnotate) {
@@ -990,7 +990,7 @@ var JsRenderer = class extends BaseRenderer {
990
990
  this.writeln("static __is_atscript_annotated_type = true");
991
991
  this.writeln("static type = {}");
992
992
  this.writeln("static metadata = new Map()");
993
- if (this.opts?.jsonSchema) if (typeof this.opts.jsonSchema === "object" && this.opts.jsonSchema.preRender) {
993
+ if (this.opts?.preRenderJsonSchema) {
994
994
  const schema = JSON.stringify(buildJsonSchema(this.toAnnotatedType(node)));
995
995
  this.writeln(`static _jsonSchema = ${schema}`);
996
996
  this.writeln("static toJsonSchema() {");
package/dist/index.cjs CHANGED
@@ -1,5 +1,4 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, '__esModule', { value: true });
3
2
  //#region rolldown:runtime
4
3
  var __create = Object.create;
5
4
  var __defProp = Object.defineProperty;
@@ -202,7 +201,7 @@ function wrapProp(name) {
202
201
  return name;
203
202
  }
204
203
  function escapeQuotes(str) {
205
- return str.replace(/"/g, "\\\"").replace(/\\/g, "\\\\");
204
+ return str.replace(/\\/g, "\\\\").replace(/"/g, "\\\"");
206
205
  }
207
206
 
208
207
  //#endregion
@@ -227,7 +226,7 @@ var TypeRenderer = class TypeRenderer extends BaseRenderer {
227
226
  this.writeln(" * Do not edit this file!");
228
227
  this.writeln(" */");
229
228
  this.writeln();
230
- this.writeln("import type { TAtscriptTypeObject, TAtscriptTypeComplex, TAtscriptTypeFinal, TAtscriptTypeArray, TMetadataMap, Validator, TAtscriptAnnotatedTypeConstructor, TValidatorOptions } from \"@atscript/typescript\"");
229
+ this.writeln("import type { TAtscriptTypeObject, TAtscriptTypeComplex, TAtscriptTypeFinal, TAtscriptTypeArray, TMetadataMap, Validator, TAtscriptAnnotatedTypeConstructor, TValidatorOptions } from \"@atscript/typescript/utils\"");
231
230
  }
232
231
  post() {
233
232
  this.writeln("// prettier-ignore-end");
@@ -500,7 +499,7 @@ var Validator = class {
500
499
  }
501
500
  validateTuple(def, value) {
502
501
  if (!Array.isArray(value) || value.length !== def.type.items.length) {
503
- this.error("Expected array of length " + def.type.items.length);
502
+ this.error(`Expected array of length ${def.type.items.length}`);
504
503
  return false;
505
504
  }
506
505
  let i = 0;
@@ -553,7 +552,7 @@ var Validator = class {
553
552
  const typeKeys = new Set();
554
553
  const skipList = new Set();
555
554
  if (this.opts.skipList) {
556
- const path$2 = this.stackPath.length > 1 ? this.path + "." : "";
555
+ const path$2 = this.stackPath.length > 1 ? `${this.path}.` : "";
557
556
  this.opts.skipList.forEach((item) => {
558
557
  if (item.startsWith(path$2)) {
559
558
  const key = item.slice(path$2.length);
@@ -843,19 +842,6 @@ else this.$metadata.set(key, value);
843
842
  };
844
843
  return handle;
845
844
  }
846
- function isAnnotatedTypeOfPrimitive(t) {
847
- if (["array", "object"].includes(t.type.kind)) return false;
848
- if (!t.type.kind) return true;
849
- if ([
850
- "union",
851
- "tuple",
852
- "intersection"
853
- ].includes(t.type.kind)) {
854
- for (const item of t.type.items) if (!isAnnotatedTypeOfPrimitive(item)) return false;
855
- return true;
856
- }
857
- return false;
858
- }
859
845
 
860
846
  //#endregion
861
847
  //#region packages/typescript/src/json-schema.ts
@@ -956,7 +942,7 @@ var JsRenderer = class extends BaseRenderer {
956
942
  this.writeln("/* eslint-disable */");
957
943
  const imports = ["defineAnnotatedType as $"];
958
944
  if (!this.opts?.preRenderJsonSchema) imports.push("buildJsonSchema as $$");
959
- this.writeln(`import { ${imports.join(", ")} } from "@atscript/typescript"`);
945
+ this.writeln(`import { ${imports.join(", ")} } from "@atscript/typescript/utils"`);
960
946
  }
961
947
  post() {
962
948
  for (const node of this.postAnnotate) {
@@ -1001,7 +987,7 @@ var JsRenderer = class extends BaseRenderer {
1001
987
  this.writeln("static __is_atscript_annotated_type = true");
1002
988
  this.writeln("static type = {}");
1003
989
  this.writeln("static metadata = new Map()");
1004
- if (this.opts?.jsonSchema) if (typeof this.opts.jsonSchema === "object" && this.opts.jsonSchema.preRender) {
990
+ if (this.opts?.preRenderJsonSchema) {
1005
991
  const schema = JSON.stringify(buildJsonSchema(this.toAnnotatedType(node)));
1006
992
  this.writeln(`static _jsonSchema = ${schema}`);
1007
993
  this.writeln("static toJsonSchema() {");
@@ -1359,10 +1345,4 @@ else return t.optional ? `${t.type} | true` : t.type;
1359
1345
  var src_default = tsPlugin;
1360
1346
 
1361
1347
  //#endregion
1362
- exports.Validator = Validator
1363
- exports.ValidatorError = ValidatorError
1364
- exports.buildJsonSchema = buildJsonSchema
1365
- exports.default = src_default
1366
- exports.defineAnnotatedType = defineAnnotatedType
1367
- exports.isAnnotatedType = isAnnotatedType
1368
- exports.isAnnotatedTypeOfPrimitive = isAnnotatedTypeOfPrimitive
1348
+ module.exports = src_default;
package/dist/index.d.ts CHANGED
@@ -8,135 +8,4 @@ interface TTsPluginOptions {
8
8
  }
9
9
  declare const tsPlugin: (opts?: TTsPluginOptions) => TAtscriptPlugin;
10
10
 
11
- interface TError {
12
- path: string;
13
- message: string;
14
- details?: TError[];
15
- }
16
- type TValidatorPlugin = (ctx: TValidatorPluginContext, def: TAtscriptAnnotatedType, value: any) => boolean | undefined;
17
- interface TValidatorOptions {
18
- partial: boolean | 'deep' | ((type: TAtscriptAnnotatedType<TAtscriptTypeObject>, path: string) => boolean);
19
- replace?: (type: TAtscriptAnnotatedType, path: string) => TAtscriptAnnotatedType;
20
- plugins: TValidatorPlugin[];
21
- unknwonProps: 'strip' | 'ignore' | 'error';
22
- errorLimit: number;
23
- skipList?: Set<string>;
24
- }
25
- interface TValidatorPluginContext {
26
- opts: Validator<any>['opts'];
27
- validateAnnotatedType: Validator<any>['validateAnnotatedType'];
28
- error: Validator<any>['error'];
29
- path: Validator<any>['path'];
30
- }
31
- declare class Validator<T extends TAtscriptAnnotatedTypeConstructor> {
32
- protected readonly def: T | TAtscriptAnnotatedType<any>;
33
- protected opts: TValidatorOptions;
34
- constructor(def: T | TAtscriptAnnotatedType<any>, opts?: Partial<TValidatorOptions>);
35
- errors: TError[];
36
- protected stackErrors: TError[][];
37
- protected stackPath: string[];
38
- protected isLimitExceeded(): boolean;
39
- protected push(name: string): void;
40
- protected pop(saveErrors: boolean): TError[] | undefined;
41
- protected clear(): void;
42
- protected error(message: string, path?: string, details?: TError[]): void;
43
- protected throw(): void;
44
- validate<TT = T>(value: any, safe?: boolean): value is TT;
45
- protected validateSafe(def: TAtscriptAnnotatedType, value: any): boolean;
46
- protected get path(): string;
47
- protected validateAnnotatedType(def: TAtscriptAnnotatedType, value: any): boolean;
48
- protected validateUnion(def: TAtscriptAnnotatedType<TAtscriptTypeComplex>, value: any): boolean;
49
- protected validateIntersection(def: TAtscriptAnnotatedType<TAtscriptTypeComplex>, value: any): boolean;
50
- protected validateTuple(def: TAtscriptAnnotatedType<TAtscriptTypeComplex>, value: any): boolean;
51
- protected validateArray(def: TAtscriptAnnotatedType<TAtscriptTypeArray>, value: any): boolean;
52
- protected validateObject(def: TAtscriptAnnotatedType<TAtscriptTypeObject>, value: any): boolean;
53
- protected validatePrimitive(def: TAtscriptAnnotatedType<TAtscriptTypeFinal>, value: any): boolean;
54
- protected validateString(def: TAtscriptAnnotatedType<TAtscriptTypeFinal>, value: string): boolean;
55
- protected validateNumber(def: TAtscriptAnnotatedType<TAtscriptTypeFinal>, value: number): boolean;
56
- }
57
- declare class ValidatorError extends Error {
58
- readonly errors: TError[];
59
- name: string;
60
- constructor(errors: TError[]);
61
- }
62
-
63
- interface TAtscriptTypeComplex {
64
- kind: 'union' | 'intersection' | 'tuple';
65
- items: TAtscriptAnnotatedType[];
66
- tags: Set<AtscriptPrimitiveTags>;
67
- }
68
- interface TAtscriptTypeArray {
69
- kind: 'array';
70
- of: TAtscriptAnnotatedType;
71
- tags: Set<AtscriptPrimitiveTags>;
72
- }
73
- interface TAtscriptTypeObject<K extends string = string> {
74
- kind: 'object';
75
- props: Map<K, TAtscriptAnnotatedType>;
76
- propsPatterns: {
77
- pattern: RegExp;
78
- def: TAtscriptAnnotatedType;
79
- }[];
80
- tags: Set<AtscriptPrimitiveTags>;
81
- }
82
- interface TAtscriptTypeFinal {
83
- kind: '';
84
- /**
85
- * design type
86
- */
87
- designType: 'string' | 'number' | 'boolean' | 'undefined' | 'null' | 'object' | 'any' | 'never';
88
- /**
89
- * value for literals
90
- */
91
- value?: string | number | boolean;
92
- tags: Set<AtscriptPrimitiveTags>;
93
- }
94
- type TAtscriptTypeDef = TAtscriptTypeComplex | TAtscriptTypeFinal | TAtscriptTypeArray | TAtscriptTypeObject<string>;
95
- interface TAtscriptAnnotatedType<T = TAtscriptTypeDef> {
96
- __is_atscript_annotated_type: true;
97
- type: T;
98
- validator: <TT extends TAtscriptAnnotatedTypeConstructor>(opts?: Partial<TValidatorOptions>) => Validator<TT>;
99
- metadata: TMetadataMap<AtscriptMetadata>;
100
- optional?: boolean;
101
- }
102
- type TAtscriptAnnotatedTypeConstructor = TAtscriptAnnotatedType & (new (...args: any[]) => any);
103
- /**
104
- * Type Guard to check if a type is atscript-annotated
105
- */
106
- declare function isAnnotatedType(type: any): type is TAtscriptAnnotatedType;
107
- type TKind = '' | 'array' | 'object' | 'union' | 'intersection' | 'tuple';
108
- declare function defineAnnotatedType(_kind?: TKind, base?: any): TAnnotatedTypeHandle;
109
- /**
110
- * Atscript Metadata Map with typed setters/getters
111
- */
112
- interface TMetadataMap<O extends object> extends Map<keyof O, O[keyof O]> {
113
- get<K extends keyof O>(key: K): O[K] | undefined;
114
- set<K extends keyof O>(key: K, value: O[K]): this;
115
- }
116
- interface TAnnotatedTypeHandle {
117
- $type: TAtscriptAnnotatedType;
118
- $def: {
119
- kind: TKind;
120
- } & Omit<TAtscriptTypeComplex, 'kind'> & Omit<TAtscriptTypeFinal, 'kind'> & Omit<TAtscriptTypeArray, 'kind'> & Omit<TAtscriptTypeObject<string>, 'kind'>;
121
- $metadata: TMetadataMap<AtscriptMetadata>;
122
- _existingObject: TAtscriptAnnotatedType | undefined;
123
- tags(...tags: string[]): TAnnotatedTypeHandle;
124
- designType(value: TAtscriptTypeFinal['designType']): TAnnotatedTypeHandle;
125
- value(value: string | number | boolean): TAnnotatedTypeHandle;
126
- of(value: TAtscriptAnnotatedType): TAnnotatedTypeHandle;
127
- item(value: TAtscriptAnnotatedType): TAnnotatedTypeHandle;
128
- prop(name: string, value: TAtscriptAnnotatedType): TAnnotatedTypeHandle;
129
- propPattern(pattern: RegExp, value: TAtscriptAnnotatedType): TAnnotatedTypeHandle;
130
- optional(value?: boolean): TAnnotatedTypeHandle;
131
- copyMetadata(fromMetadata: TMetadataMap<AtscriptMetadata>): TAnnotatedTypeHandle;
132
- refTo(type: TAtscriptAnnotatedType & {
133
- name?: string;
134
- }, chain?: string[]): TAnnotatedTypeHandle;
135
- annotate(key: keyof AtscriptMetadata, value: any, asArray?: boolean): TAnnotatedTypeHandle;
136
- }
137
- declare function isAnnotatedTypeOfPrimitive(t: TAtscriptAnnotatedType): boolean;
138
-
139
- type TJsonSchema = Record<string, any>;
140
- declare function buildJsonSchema(type: TAtscriptAnnotatedType): TJsonSchema;
141
-
142
- export { type TAnnotatedTypeHandle, type TAtscriptAnnotatedType, type TAtscriptAnnotatedTypeConstructor, type TAtscriptTypeArray, type TAtscriptTypeComplex, type TAtscriptTypeDef, type TAtscriptTypeFinal, type TAtscriptTypeObject, type TJsonSchema, type TMetadataMap, type TValidatorOptions, type TValidatorPlugin, type TValidatorPluginContext, Validator, ValidatorError, buildJsonSchema, tsPlugin as default, defineAnnotatedType, isAnnotatedType, isAnnotatedTypeOfPrimitive };
11
+ export { tsPlugin as default };
package/dist/index.mjs CHANGED
@@ -177,7 +177,7 @@ function wrapProp(name) {
177
177
  return name;
178
178
  }
179
179
  function escapeQuotes(str) {
180
- return str.replace(/"/g, "\\\"").replace(/\\/g, "\\\\");
180
+ return str.replace(/\\/g, "\\\\").replace(/"/g, "\\\"");
181
181
  }
182
182
 
183
183
  //#endregion
@@ -202,7 +202,7 @@ var TypeRenderer = class TypeRenderer extends BaseRenderer {
202
202
  this.writeln(" * Do not edit this file!");
203
203
  this.writeln(" */");
204
204
  this.writeln();
205
- this.writeln("import type { TAtscriptTypeObject, TAtscriptTypeComplex, TAtscriptTypeFinal, TAtscriptTypeArray, TMetadataMap, Validator, TAtscriptAnnotatedTypeConstructor, TValidatorOptions } from \"@atscript/typescript\"");
205
+ this.writeln("import type { TAtscriptTypeObject, TAtscriptTypeComplex, TAtscriptTypeFinal, TAtscriptTypeArray, TMetadataMap, Validator, TAtscriptAnnotatedTypeConstructor, TValidatorOptions } from \"@atscript/typescript/utils\"");
206
206
  }
207
207
  post() {
208
208
  this.writeln("// prettier-ignore-end");
@@ -475,7 +475,7 @@ var Validator = class {
475
475
  }
476
476
  validateTuple(def, value) {
477
477
  if (!Array.isArray(value) || value.length !== def.type.items.length) {
478
- this.error("Expected array of length " + def.type.items.length);
478
+ this.error(`Expected array of length ${def.type.items.length}`);
479
479
  return false;
480
480
  }
481
481
  let i = 0;
@@ -528,7 +528,7 @@ var Validator = class {
528
528
  const typeKeys = new Set();
529
529
  const skipList = new Set();
530
530
  if (this.opts.skipList) {
531
- const path$1 = this.stackPath.length > 1 ? this.path + "." : "";
531
+ const path$1 = this.stackPath.length > 1 ? `${this.path}.` : "";
532
532
  this.opts.skipList.forEach((item) => {
533
533
  if (item.startsWith(path$1)) {
534
534
  const key = item.slice(path$1.length);
@@ -818,19 +818,6 @@ else this.$metadata.set(key, value);
818
818
  };
819
819
  return handle;
820
820
  }
821
- function isAnnotatedTypeOfPrimitive(t) {
822
- if (["array", "object"].includes(t.type.kind)) return false;
823
- if (!t.type.kind) return true;
824
- if ([
825
- "union",
826
- "tuple",
827
- "intersection"
828
- ].includes(t.type.kind)) {
829
- for (const item of t.type.items) if (!isAnnotatedTypeOfPrimitive(item)) return false;
830
- return true;
831
- }
832
- return false;
833
- }
834
821
 
835
822
  //#endregion
836
823
  //#region packages/typescript/src/json-schema.ts
@@ -931,7 +918,7 @@ var JsRenderer = class extends BaseRenderer {
931
918
  this.writeln("/* eslint-disable */");
932
919
  const imports = ["defineAnnotatedType as $"];
933
920
  if (!this.opts?.preRenderJsonSchema) imports.push("buildJsonSchema as $$");
934
- this.writeln(`import { ${imports.join(", ")} } from "@atscript/typescript"`);
921
+ this.writeln(`import { ${imports.join(", ")} } from "@atscript/typescript/utils"`);
935
922
  }
936
923
  post() {
937
924
  for (const node of this.postAnnotate) {
@@ -976,7 +963,7 @@ var JsRenderer = class extends BaseRenderer {
976
963
  this.writeln("static __is_atscript_annotated_type = true");
977
964
  this.writeln("static type = {}");
978
965
  this.writeln("static metadata = new Map()");
979
- if (this.opts?.jsonSchema) if (typeof this.opts.jsonSchema === "object" && this.opts.jsonSchema.preRender) {
966
+ if (this.opts?.preRenderJsonSchema) {
980
967
  const schema = JSON.stringify(buildJsonSchema(this.toAnnotatedType(node)));
981
968
  this.writeln(`static _jsonSchema = ${schema}`);
982
969
  this.writeln("static toJsonSchema() {");
@@ -1334,4 +1321,4 @@ else return t.optional ? `${t.type} | true` : t.type;
1334
1321
  var src_default = tsPlugin;
1335
1322
 
1336
1323
  //#endregion
1337
- export { Validator, ValidatorError, buildJsonSchema, src_default as default, defineAnnotatedType, isAnnotatedType, isAnnotatedTypeOfPrimitive };
1324
+ export { src_default as default };