@traqula/core 0.0.15 → 0.0.17

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.
@@ -7,7 +7,7 @@ export type SubTyped<Type extends PropertyKey, Subtype extends PropertyKey> = {
7
7
  type: Type;
8
8
  subType: Subtype;
9
9
  };
10
- export declare class CoreFactory {
10
+ export declare class AstCoreFactory {
11
11
  wrap<T>(val: T, loc: SourceLocation): Wrap<T>;
12
12
  isLocalized(obj: unknown): obj is Localized;
13
13
  sourceLocation(...elements: (undefined | IToken | Localized)[]): SourceLocation;
@@ -1,4 +1,4 @@
1
- export class CoreFactory {
1
+ export class AstCoreFactory {
2
2
  wrap(val, loc) {
3
3
  return { val, loc };
4
4
  }
@@ -132,4 +132,4 @@ export class CoreFactory {
132
132
  return temp.type === type && temp.subType === subType;
133
133
  }
134
134
  }
135
- //# sourceMappingURL=CoreFactory.js.map
135
+ //# sourceMappingURL=AstCoreFactory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AstCoreFactory.js","sourceRoot":"","sources":["AstCoreFactory.ts"],"names":[],"mappings":"AAiBA,MAAM,OAAO,cAAc;IAClB,IAAI,CAAI,GAAM,EAAE,GAAmB;QACxC,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC;IACtB,CAAC;IAEM,WAAW,CAAC,GAAY;QAC7B,OAAO,OAAO,GAAG,KAAK,QAAQ,IAAI,GAAG,KAAK,IAAI,IAAI,KAAK,IAAI,GAAG;YAC5D,OAAO,GAAG,CAAC,GAAG,KAAK,QAAQ,IAAI,GAAG,CAAC,GAAG,KAAK,IAAI,IAAI,oBAAoB,IAAI,GAAG,CAAC,GAAG,CAAC;IACvF,CAAC;IAEM,cAAc,CAAC,GAAG,QAA4C;QACnE,MAAM,YAAY,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,SAAS,CAAC,CAAC;QAC3D,IAAI,YAAY,CAAC,MAA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AC,GAAW;QACjD,OAAO,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,IAAI,GAAG,CAAC,kBAAkB,KAAK,cAAc,CAAC;IACjF,CAAC;IAEM,eAAe,CAAC,IAAe;QACpC,OAAO,IAAI,CAAC,2BAA2B,CAAC,IAAI,CAAC,GAAG,CAAC;YAC/C,IAAI,CAAC,gCAAgC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACpD,CAAC;IAEM,WAAW,CAAC,IAAe,EAAE,QAAoB;QACtD,IAAI,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,EAAE,CAAC;YAC/B,QAAQ,EAAE,CAAC;QACb,CAAC;IACH,CAAC;IAEM,6BAA6B,CAAC,GAAW;QAC9C,OAAO,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,IAAI,GAAG,CAAC,kBAAkB,KAAK,eAAe,CAAC;IAClF,CAAC;IAEM,QAAQ,CAAsB,GAAW,EAAE,IAAU;QAC1D,MAAM,MAAM,GAAmB,GAAG,CAAC;QACnC,OAAO,MAAM,CAAC,IAAI,KAAK,IAAI,CAAC;IAC9B,CAAC;IAEM,WAAW,CAA8C,GAAW,EAAE,IAAU,EAAE,OAAgB;QAEvG,MAAM,IAAI,GAA0C,GAAG,CAAC;QACxD,OAAO,IAAI,CAAC,IAAI,KAAK,IAAI,IAAI,IAAI,CAAC,OAAO,KAAK,OAAO,CAAC;IACxD,CAAC;CACF","sourcesContent":["import type { IToken } from 'chevrotain';\n\nimport type {\n SourceLocation,\n SourceLocationNodeAutoGenerate,\n SourceLocationNodeReplace,\n SourceLocationNoMaterialize,\n SourceLocationSource,\n SourceLocationStringReplace,\n Node,\n Localized,\n Wrap,\n} from './nodeTypings.js';\n\nexport type Typed<Type extends PropertyKey> = { type: Type };\nexport type SubTyped<Type extends PropertyKey, Subtype extends PropertyKey> = { type: Type; subType: Subtype };\n\nexport class AstCoreFactory {\n public wrap<T>(val: T, loc: SourceLocation): Wrap<T> {\n return { val, loc };\n }\n\n public isLocalized(obj: unknown): obj is Localized {\n return typeof obj === 'object' && obj !== null && 'loc' in obj &&\n typeof obj.loc === 'object' && obj.loc !== null && 'sourceLocationType' in obj.loc;\n }\n\n public sourceLocation(...elements: (undefined | IToken | Localized)[]): SourceLocation {\n const pureElements = elements.filter(x => x !== undefined);\n if (pureElements.length === 0) {\n return this.sourceLocationNoMaterialize();\n }\n const filtered = pureElements.filter(element =>\n !this.isLocalized(element) || this.isSourceLocationSource(element.loc) ||\n this.isSourceLocationStringReplace(element.loc) || this.isSourceLocationNodeReplace(element.loc));\n if (filtered.length === 0) {\n return this.gen();\n }\n const first = filtered[0];\n const last = filtered.at(-1)!;\n return {\n sourceLocationType: 'source',\n start: this.isLocalized(first) ?\n (<SourceLocationSource | SourceLocationStringReplace> first.loc).start :\n first.startOffset,\n end: this.isLocalized(last) ?\n (<SourceLocationSource | SourceLocationStringReplace> last.loc).end :\n (last.endOffset! + 1),\n };\n }\n\n public sourceLocationNoMaterialize(): SourceLocationNoMaterialize {\n return { sourceLocationType: 'noMaterialize' };\n }\n\n /**\n * Returns a copy of the argument that is not materialized\n */\n public dematerialized<T extends Node>(arg: T): T & { loc: SourceLocationNoMaterialize } {\n return { ...arg, loc: this.sourceLocationNoMaterialize() };\n }\n\n public safeObjectTransform(value: unknown, mapper: (some: object) => any): any {\n if (value && typeof value === 'object') {\n // If you wonder why this is all so hard, this is the reason. We cannot lose the methods of our Array objects\n if (Array.isArray(value)) {\n return value.map(x => this.safeObjectTransform(x, mapper));\n }\n return mapper(value);\n }\n return value;\n }\n\n public forcedAutoGenTree<T extends object>(obj: T): T {\n const copy = { ...obj };\n for (const [ key, value ] of Object.entries(copy)) {\n (<Record<string, object>> copy)[key] = this.safeObjectTransform(value, obj => this.forcedAutoGenTree(obj));\n }\n if (this.isLocalized(copy)) {\n copy.loc = this.gen();\n }\n return copy;\n }\n\n public forceMaterialized<T extends Node>(arg: T): T {\n if (this.isSourceLocationNoMaterialize(arg.loc)) {\n return this.forcedAutoGenTree(arg);\n }\n return { ...arg };\n }\n\n public isSourceLocation(loc: object): loc is SourceLocation {\n return 'sourceLocationType' in loc;\n }\n\n public sourceLocationSource(start: number, end: number): SourceLocationSource {\n return {\n sourceLocationType: 'source',\n start,\n end,\n };\n }\n\n public gen(): SourceLocationNodeAutoGenerate {\n return { sourceLocationType: 'autoGenerate' };\n }\n\n public isSourceLocationSource(loc: object): loc is SourceLocationSource {\n return this.isSourceLocation(loc) && loc.sourceLocationType === 'source';\n }\n\n public isSourceLocationStringReplace(loc: object): loc is SourceLocationStringReplace {\n return this.isSourceLocation(loc) && loc.sourceLocationType === 'stringReplace';\n }\n\n public sourceLocationNodeReplaceUnsafe(loc: SourceLocation): SourceLocationNodeReplace {\n if (this.isSourceLocationSource(loc)) {\n return this.sourceLocationNodeReplace(loc);\n }\n throw new Error('not possible');\n }\n\n public sourceLocationNodeReplace(location: SourceLocationSource): SourceLocationNodeReplace;\n public sourceLocationNodeReplace(start: number, end: number): SourceLocationNodeReplace;\n public sourceLocationNodeReplace(startOrLoc: number | SourceLocationSource, end?: number): SourceLocationNodeReplace {\n let starting;\n let ending;\n if (typeof startOrLoc === 'number') {\n starting = startOrLoc;\n ending = end!;\n } else {\n starting = startOrLoc.start;\n ending = startOrLoc.end;\n }\n return {\n sourceLocationType: 'nodeReplace',\n start: starting,\n end: ending,\n };\n }\n\n public isSourceLocationNodeReplace(loc: object): loc is SourceLocationNodeReplace {\n return this.isSourceLocation(loc) && loc.sourceLocationType === 'nodeReplace';\n }\n\n public isSourceLocationNodeAutoGenerate(loc: object): loc is SourceLocationNodeAutoGenerate {\n return this.isSourceLocation(loc) && loc.sourceLocationType === 'autoGenerate';\n }\n\n public nodeShouldPrint(node: Localized): boolean {\n return this.isSourceLocationNodeReplace(node.loc) ||\n this.isSourceLocationNodeAutoGenerate(node.loc);\n }\n\n public printFilter(node: Localized, callback: () => void): void {\n if (this.nodeShouldPrint(node)) {\n callback();\n }\n }\n\n public isSourceLocationNoMaterialize(loc: object): loc is SourceLocationNoMaterialize {\n return this.isSourceLocation(loc) && loc.sourceLocationType === 'noMaterialize';\n }\n\n public isOfType<Type extends string>(obj: object, type: Type): obj is Typed<Type> {\n const casted: { type?: any } = obj;\n return casted.type === type;\n }\n\n public isOfSubType<Type extends string, SubType extends string>(obj: object, type: Type, subType: SubType):\n obj is SubTyped<Type, SubType> {\n const temp: { type?: unknown; subType?: unknown } = obj;\n return temp.type === type && temp.subType === subType;\n }\n}\n"]}
@@ -3,35 +3,68 @@ type Safeness = 'safe' | 'unsafe';
3
3
  type SafeWrap<Safe extends Safeness, obj extends object> = Safe extends 'safe' ? {
4
4
  [key in keyof obj]: unknown;
5
5
  } : obj;
6
+ export interface VisitContext {
7
+ shortcut?: boolean;
8
+ continue?: boolean;
9
+ }
10
+ export interface SelectiveTraversalContext<Nodes> {
11
+ next?: Nodes[];
12
+ shortcut?: boolean;
13
+ }
6
14
  export declare class TransformerType<Nodes extends Pick<Node, 'type'>> {
15
+ protected clone<T>(obj: T): T;
7
16
  protected safeObjectVisit(value: unknown, mapper: (some: object) => any): unknown;
8
- transformNode<Safe extends 'safe' | 'unsafe' = 'safe'>(curObject: object, nodeCallBacks: {
17
+ /**
18
+ * Recursively transforms all objects that are not arrays. Mapper is called on deeper objects first.
19
+ * @param startObject object to start iterating from
20
+ * @param mapper mapper to transform the various objects - argument is a copy of the original
21
+ * @param preVisitor callback that is evaluated before iterating deeper.
22
+ * If continues is false, we do not iterate deeper, current object is still mapped. - default: true
23
+ * If shortcut is true, we do not iterate deeper, nor do we branch out, this mapper will be the last one called.
24
+ * - Default false
25
+ */
26
+ transformObject(startObject: object, mapper: (some: object) => any, preVisitor?: (some: object) => VisitContext): unknown;
27
+ /**
28
+ * Visitor that visits all objects. Visits deeper objects first.
29
+ */
30
+ visitObject(startObject: object, visitor: (some: object) => void, preVisitor?: (some: object) => VisitContext): void;
31
+ transformNode<Safe extends 'safe' | 'unsafe' = 'safe'>(startObject: object, nodeCallBacks: {
9
32
  [T in Nodes['type']]?: {
10
33
  transform?: (op: SafeWrap<Safe, Extract<Nodes, {
11
34
  type: T;
12
35
  }>>) => any;
13
- continue?: (op: Extract<Nodes, {
36
+ preVisitor?: (op: Extract<Nodes, {
14
37
  type: T;
15
- }>) => boolean;
38
+ }>) => VisitContext;
16
39
  };
17
40
  }): any;
18
- visitNode(curObject: object, nodeCallBacks: {
41
+ visitNode(startObject: object, nodeCallBacks: {
42
+ [T in Nodes['type']]?: {
43
+ visitor?: (op: Extract<Nodes, {
44
+ type: T;
45
+ }>) => void;
46
+ preVisitor?: (op: Extract<Nodes, {
47
+ type: T;
48
+ }>) => VisitContext;
49
+ };
50
+ }): void;
51
+ traverseNodes(currentNode: Nodes, traverse: {
19
52
  [T in Nodes['type']]?: (op: Extract<Nodes, {
20
53
  type: T;
21
- }>) => boolean;
54
+ }>) => SelectiveTraversalContext<Nodes>;
22
55
  }): void;
23
56
  }
24
57
  export declare class TransformerSubType<Nodes extends Pick<Node, 'type' | 'subType'>> extends TransformerType<Nodes> {
25
- transformNodeSpecific<Safe extends 'safe' | 'unsafe' = 'safe'>(curObject: object, nodeCallBacks: {
58
+ transformNodeSpecific<Safe extends 'safe' | 'unsafe' = 'safe'>(startObject: object, nodeCallBacks: {
26
59
  [T in Nodes['type']]?: {
27
60
  transform?: (op: SafeWrap<Safe, Extract<Nodes, {
28
61
  type: T;
29
62
  }>>) => any;
30
- continue?: (op: Extract<Nodes, {
63
+ preVisitor?: (op: Extract<Nodes, {
31
64
  type: T;
32
- }>) => boolean;
65
+ }>) => VisitContext;
33
66
  };
34
- }, nodeSpecificCallBacks?: {
67
+ }, nodeSpecificCallBacks: {
35
68
  [Type in Nodes['type']]?: {
36
69
  [SubType in Extract<Nodes, {
37
70
  type: Type;
@@ -41,21 +74,47 @@ export declare class TransformerSubType<Nodes extends Pick<Node, 'type' | 'subTy
41
74
  type: Type;
42
75
  subType: SubType;
43
76
  }>>) => any;
44
- continue?: (op: Extract<Nodes, {
77
+ preVisitor?: (op: Extract<Nodes, {
45
78
  type: Type;
46
79
  subType: SubType;
47
- }>) => boolean;
80
+ }>) => VisitContext;
48
81
  };
49
82
  };
50
83
  }): any;
51
84
  /**
52
85
  * When both nodeCallBack and NodeSpecific callBack are matched, will only look at nodeSpecifCallback
53
86
  */
54
- visitNodeSpecific(curObject: object, nodeCallBacks: {
55
- [T in Nodes['type']]?: (op: Extract<Nodes, {
56
- type: T;
57
- }>) => boolean | void;
58
- }, nodeSpecificCallBacks?: {
87
+ visitNodeSpecific(startObject: object, nodeCallBacks: {
88
+ [T in Nodes['type']]?: {
89
+ visitor?: (op: Extract<Nodes, {
90
+ type: T;
91
+ }>) => void;
92
+ preVisitor?: (op: Extract<Nodes, {
93
+ type: T;
94
+ }>) => VisitContext;
95
+ };
96
+ }, nodeSpecificCallBacks: {
97
+ [Type in Nodes['type']]?: {
98
+ [Subtype in Extract<Nodes, {
99
+ type: Type;
100
+ subType: string;
101
+ }>['subType']]?: {
102
+ visitor?: (op: Extract<Nodes, {
103
+ type: Type;
104
+ subType: Subtype;
105
+ }>) => void;
106
+ preVisitor?: (op: Extract<Nodes, {
107
+ type: Type;
108
+ subType: Subtype;
109
+ }>) => VisitContext;
110
+ };
111
+ };
112
+ }): void;
113
+ traverseSubNodes(currentNode: Nodes, traverseNode: {
114
+ [Type in Nodes['type']]?: (op: Extract<Nodes, {
115
+ type: Type;
116
+ }>) => SelectiveTraversalContext<Nodes>;
117
+ }, traverseSubNode: {
59
118
  [Type in Nodes['type']]?: {
60
119
  [Subtype in Extract<Nodes, {
61
120
  type: Type;
@@ -63,7 +122,7 @@ export declare class TransformerSubType<Nodes extends Pick<Node, 'type' | 'subTy
63
122
  }>['subType']]?: (op: Extract<Nodes, {
64
123
  type: Type;
65
124
  subType: Subtype;
66
- }>) => boolean | void;
125
+ }>) => SelectiveTraversalContext<Nodes>;
67
126
  };
68
127
  }): void;
69
128
  }
@@ -1,7 +1,11 @@
1
1
  export class TransformerType {
2
+ clone(obj) {
3
+ const newObj = Object.create(Object.getPrototypeOf(obj));
4
+ Object.defineProperties(newObj, Object.getOwnPropertyDescriptors(obj));
5
+ return newObj;
6
+ }
2
7
  safeObjectVisit(value, mapper) {
3
8
  if (value && typeof value === 'object') {
4
- // If you wonder why this is all so hard, this is the reason. We cannot lose the methods of our Array objects
5
9
  if (Array.isArray(value)) {
6
10
  return value.map(x => this.safeObjectVisit(x, mapper));
7
11
  }
@@ -9,106 +13,214 @@ export class TransformerType {
9
13
  }
10
14
  return value;
11
15
  }
12
- transformNode(curObject, nodeCallBacks) {
13
- let continueCheck;
14
- let transformation;
15
- const casted = curObject;
16
- if (casted.type) {
17
- continueCheck = nodeCallBacks[casted.type]?.continue;
18
- transformation = nodeCallBacks[casted.type]?.transform;
19
- }
20
- let shouldContinue = true;
21
- if (continueCheck) {
22
- shouldContinue = continueCheck(curObject);
23
- }
24
- if (!shouldContinue) {
16
+ /**
17
+ * Recursively transforms all objects that are not arrays. Mapper is called on deeper objects first.
18
+ * @param startObject object to start iterating from
19
+ * @param mapper mapper to transform the various objects - argument is a copy of the original
20
+ * @param preVisitor callback that is evaluated before iterating deeper.
21
+ * If continues is false, we do not iterate deeper, current object is still mapped. - default: true
22
+ * If shortcut is true, we do not iterate deeper, nor do we branch out, this mapper will be the last one called.
23
+ * - Default false
24
+ */
25
+ transformObject(startObject, mapper, preVisitor = () => ({})) {
26
+ let didShortCut = false;
27
+ const recurse = (curObject) => {
28
+ const copy = this.clone(curObject);
29
+ const context = preVisitor(copy);
30
+ didShortCut = context.shortcut ?? false;
31
+ const continues = context.continue ?? true;
32
+ if (continues && !didShortCut) {
33
+ for (const [key, value] of Object.entries(copy)) {
34
+ if (didShortCut) {
35
+ return copy;
36
+ }
37
+ copy[key] =
38
+ this.safeObjectVisit(value, obj => recurse(obj));
39
+ }
40
+ }
41
+ return mapper(copy);
42
+ };
43
+ return recurse(startObject);
44
+ }
45
+ /**
46
+ * Visitor that visits all objects. Visits deeper objects first.
47
+ */
48
+ visitObject(startObject, visitor, preVisitor = () => ({})) {
49
+ let didShortCut = false;
50
+ const recurse = (curObject) => {
51
+ const context = preVisitor(curObject);
52
+ didShortCut = context.shortcut ?? false;
53
+ const continues = context.continue ?? true;
54
+ if (continues && !didShortCut) {
55
+ for (const value of Object.values(curObject)) {
56
+ if (didShortCut) {
57
+ return;
58
+ }
59
+ this.safeObjectVisit(value, obj => recurse(obj));
60
+ }
61
+ }
62
+ visitor(curObject);
63
+ };
64
+ recurse(startObject);
65
+ }
66
+ transformNode(startObject, nodeCallBacks) {
67
+ const transformWrapper = (curObject) => {
68
+ const casted = curObject;
69
+ if (casted.type) {
70
+ const ogFunc = nodeCallBacks[casted.type]?.transform;
71
+ if (ogFunc) {
72
+ return ogFunc(casted);
73
+ }
74
+ }
25
75
  return curObject;
26
- }
27
- const copy = { ...curObject };
28
- for (const [key, value] of Object.entries(copy)) {
29
- copy[key] =
30
- this.safeObjectVisit(value, obj => this.transformNode(obj, nodeCallBacks));
31
- }
32
- if (transformation) {
33
- return transformation(copy);
34
- }
35
- return copy;
76
+ };
77
+ const preTransformWrapper = (curObject) => {
78
+ const casted = curObject;
79
+ if (casted.type) {
80
+ const ogFunc = nodeCallBacks[casted.type]?.preVisitor;
81
+ if (ogFunc) {
82
+ return ogFunc(casted);
83
+ }
84
+ }
85
+ return {};
86
+ };
87
+ return this.transformObject(startObject, transformWrapper, preTransformWrapper);
36
88
  }
37
- visitNode(curObject, nodeCallBacks) {
38
- let callback;
39
- const casted = curObject;
40
- if (casted.type) {
41
- callback = nodeCallBacks[casted.type];
42
- }
43
- let shouldIterate = true;
44
- if (callback) {
45
- shouldIterate = callback(curObject);
46
- }
47
- if (shouldIterate) {
48
- for (const value of Object.values(curObject)) {
49
- this.safeObjectVisit(value, obj => this.visitNode(obj, nodeCallBacks));
89
+ visitNode(startObject, nodeCallBacks) {
90
+ const visitWrapper = (curObject) => {
91
+ const casted = curObject;
92
+ if (casted.type) {
93
+ const ogFunc = nodeCallBacks[casted.type]?.visitor;
94
+ if (ogFunc) {
95
+ ogFunc(casted);
96
+ }
50
97
  }
51
- }
98
+ };
99
+ const preVisitWrapper = (curObject) => {
100
+ const casted = curObject;
101
+ if (casted.type) {
102
+ const ogFunc = nodeCallBacks[casted.type]?.preVisitor;
103
+ if (ogFunc) {
104
+ return ogFunc(casted);
105
+ }
106
+ }
107
+ return {};
108
+ };
109
+ this.visitObject(startObject, visitWrapper, preVisitWrapper);
110
+ }
111
+ traverseNodes(currentNode, traverse) {
112
+ let didShortCut = false;
113
+ const recurse = (curNode) => {
114
+ const traverser = traverse[curNode.type];
115
+ if (traverser) {
116
+ const { next, shortcut } = traverser(curNode);
117
+ didShortCut = shortcut ?? false;
118
+ if (!didShortCut) {
119
+ for (const node of next ?? []) {
120
+ if (didShortCut) {
121
+ return;
122
+ }
123
+ recurse(node);
124
+ }
125
+ }
126
+ }
127
+ };
128
+ recurse(currentNode);
52
129
  }
53
130
  }
54
131
  export class TransformerSubType extends TransformerType {
55
- // Public visitObjects(curObject: object, visitor: (current: object) => void): void {
56
- // for (const value of Object.values(curObject)) {
57
- // this.safeObjectVisit(value, obj => this.visitObjects(obj, visitor));
58
- // }
59
- // }
60
- transformNodeSpecific(curObject, nodeCallBacks, nodeSpecificCallBacks = {}) {
61
- let continueCheck;
62
- let transformation;
63
- const casted = curObject;
64
- if (casted.type && casted.subType) {
65
- const specific = nodeSpecificCallBacks[casted.type];
66
- if (specific) {
67
- continueCheck = specific[casted.subType]?.continue;
68
- transformation = specific[casted.subType]?.transform;
132
+ transformNodeSpecific(startObject, nodeCallBacks, nodeSpecificCallBacks) {
133
+ const transformWrapper = (curObject) => {
134
+ let ogTransform;
135
+ const casted = curObject;
136
+ if (casted.type && casted.subType) {
137
+ const specific = nodeSpecificCallBacks[casted.type];
138
+ if (specific) {
139
+ ogTransform = specific[casted.subType]?.transform;
140
+ }
141
+ if (!ogTransform) {
142
+ ogTransform = nodeCallBacks[casted.type]?.transform;
143
+ }
69
144
  }
70
- }
71
- let shouldContinue = true;
72
- if (continueCheck) {
73
- shouldContinue = continueCheck(curObject);
74
- }
75
- if (!shouldContinue) {
76
- return curObject;
77
- }
78
- const copy = { ...curObject };
79
- for (const [key, value] of Object.entries(copy)) {
80
- copy[key] =
81
- this.safeObjectVisit(value, obj => this.transformNodeSpecific(obj, nodeCallBacks, nodeSpecificCallBacks));
82
- }
83
- if (transformation) {
84
- return transformation(copy);
85
- }
86
- return copy;
145
+ return ogTransform ? ogTransform(casted) : curObject;
146
+ };
147
+ const preVisitWrapper = (curObject) => {
148
+ let ogPreVisit;
149
+ const casted = curObject;
150
+ if (casted.type && casted.subType) {
151
+ const specific = nodeSpecificCallBacks[casted.type];
152
+ if (specific) {
153
+ ogPreVisit = specific[casted.subType]?.preVisitor;
154
+ }
155
+ if (!ogPreVisit) {
156
+ ogPreVisit = nodeCallBacks[casted.type]?.preVisitor;
157
+ }
158
+ }
159
+ return ogPreVisit ? ogPreVisit(casted) : curObject;
160
+ };
161
+ return this.transformObject(startObject, transformWrapper, preVisitWrapper);
87
162
  }
88
163
  /**
89
164
  * When both nodeCallBack and NodeSpecific callBack are matched, will only look at nodeSpecifCallback
90
165
  */
91
- visitNodeSpecific(curObject, nodeCallBacks, nodeSpecificCallBacks = {}) {
92
- let callback;
93
- const casted = curObject;
94
- if (casted.type && casted.subType) {
95
- const specific = nodeSpecificCallBacks[casted.type];
96
- if (specific) {
97
- callback = specific[casted.subType];
166
+ visitNodeSpecific(startObject, nodeCallBacks, nodeSpecificCallBacks) {
167
+ const visitWrapper = (curObject) => {
168
+ let ogTransform;
169
+ const casted = curObject;
170
+ if (casted.type && casted.subType) {
171
+ const specific = nodeSpecificCallBacks[casted.type];
172
+ if (specific) {
173
+ ogTransform = specific[casted.subType]?.visitor;
174
+ }
175
+ if (!ogTransform) {
176
+ ogTransform = nodeCallBacks[casted.type]?.visitor;
177
+ }
98
178
  }
99
- }
100
- if (!callback && casted.type) {
101
- callback = nodeCallBacks[curObject.type];
102
- }
103
- let shouldIterate = true;
104
- if (callback) {
105
- shouldIterate = callback(curObject) ?? true;
106
- }
107
- if (shouldIterate) {
108
- for (const value of Object.values(curObject)) {
109
- this.safeObjectVisit(value, obj => this.visitNodeSpecific(obj, nodeCallBacks, nodeSpecificCallBacks));
179
+ if (ogTransform) {
180
+ ogTransform(casted);
110
181
  }
111
- }
182
+ };
183
+ const preVisitWrapper = (curObject) => {
184
+ let ogPreVisit;
185
+ const casted = curObject;
186
+ if (casted.type && casted.subType) {
187
+ const specific = nodeSpecificCallBacks[casted.type];
188
+ if (specific) {
189
+ ogPreVisit = specific[casted.subType]?.preVisitor;
190
+ }
191
+ if (!ogPreVisit) {
192
+ ogPreVisit = nodeCallBacks[casted.type]?.preVisitor;
193
+ }
194
+ }
195
+ return ogPreVisit ? ogPreVisit(casted) : curObject;
196
+ };
197
+ this.visitObject(startObject, visitWrapper, preVisitWrapper);
198
+ }
199
+ traverseSubNodes(currentNode, traverseNode, traverseSubNode) {
200
+ let didShortCut = false;
201
+ const recurse = (curNode) => {
202
+ let traverser;
203
+ const subObj = traverseSubNode[curNode.type];
204
+ if (subObj) {
205
+ traverser = subObj[curNode.subType];
206
+ }
207
+ if (!traverser) {
208
+ traverser = traverseNode[curNode.type];
209
+ }
210
+ if (traverser) {
211
+ const { next, shortcut } = traverser(curNode);
212
+ didShortCut = shortcut ?? false;
213
+ if (!didShortCut) {
214
+ for (const node of next ?? []) {
215
+ if (didShortCut) {
216
+ return;
217
+ }
218
+ recurse(node);
219
+ }
220
+ }
221
+ }
222
+ };
223
+ recurse(currentNode);
112
224
  }
113
225
  }
114
226
  //# sourceMappingURL=Transformers.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"Transformers.js","sourceRoot":"","sources":["Transformers.ts"],"names":[],"mappings":"AAKA,MAAM,OAAO,eAAe;IAChB,eAAe,CAAC,KAAc,EAAE,MAA6B;QACrE,IAAI,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;YACvC,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AC;QACzB,IAAI,QAAQ,EAAE,CAAC;YACb,aAAa,GAAG,QAAQ,CAAC,SAAS,CAAC,IAAI,IAAI,CAAC;QAC9C,CAAC;QACD,IAAI,aAAa,EAAE,CAAC;YAClB,KAAK,MAAM,KAAK,IAAI,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE,CAAC;gBAC7C,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,iBAAiB,CAAC,GAAG,EAAE,aAAa,EAAE,qBAAqB,CAAC,CAAC,CAAC;YACxG,CAAC;QACH,CAAC;IACH,CAAC;CACF","sourcesContent":["import type { Node } from './nodeTypings.js';\n\ntype Safeness = 'safe' | 'unsafe';\ntype SafeWrap<Safe extends Safeness, obj extends object> = Safe extends 'safe' ? {[key in keyof obj]: unknown } : obj;\n\nexport class TransformerType<Nodes extends Pick<Node, 'type'>> {\n protected safeObjectVisit(value: unknown, mapper: (some: object) => any): unknown {\n if (value && typeof value === 'object') {\n // If you wonder why this is all so hard, this is the reason. We cannot lose the methods of our Array objects\n if (Array.isArray(value)) {\n return value.map(x => this.safeObjectVisit(x, mapper));\n }\n return mapper(value);\n }\n return value;\n }\n\n public transformNode<Safe extends 'safe' | 'unsafe' = 'safe'>(\n curObject: object,\n nodeCallBacks: {[T in Nodes['type']]?: {\n transform?: (op: SafeWrap<Safe, Extract<Nodes, { type: T }>>) => any;\n continue?: (op: Extract<Nodes, { type: T }>) => boolean;\n }},\n ): any {\n let continueCheck: ((node: any) => boolean) | undefined;\n let transformation: ((node: any) => unknown) | undefined;\n const casted = <{ type?: Nodes['type'] }>curObject;\n if (casted.type) {\n continueCheck = nodeCallBacks[casted.type]?.continue;\n transformation = nodeCallBacks[casted.type]?.transform;\n }\n let shouldContinue = true;\n if (continueCheck) {\n shouldContinue = continueCheck(curObject);\n }\n if (!shouldContinue) {\n return curObject;\n }\n const copy: { type?: unknown } = { ...curObject };\n for (const [ key, value ] of Object.entries(copy)) {\n (<Record<string, unknown>> copy)[key] =\n this.safeObjectVisit(value, obj => this.transformNode(obj, nodeCallBacks));\n }\n if (transformation) {\n return transformation(copy);\n }\n return copy;\n }\n\n public visitNode(\n curObject: object,\n nodeCallBacks: {[T in Nodes['type']]?: (op: Extract<Nodes, { type: T }>) => boolean },\n ): void {\n let callback: ((node: any) => boolean) | undefined;\n const casted = <{ type?: Nodes['type'] }>curObject;\n if (casted.type) {\n callback = nodeCallBacks[casted.type];\n }\n let shouldIterate = true;\n if (callback) {\n shouldIterate = callback(curObject);\n }\n if (shouldIterate) {\n for (const value of Object.values(curObject)) {\n this.safeObjectVisit(value, obj => this.visitNode(obj, nodeCallBacks));\n }\n }\n }\n}\n\nexport class TransformerSubType<Nodes extends Pick<Node, 'type' | 'subType'>> extends TransformerType<Nodes> {\n // Public visitObjects(curObject: object, visitor: (current: object) => void): void {\n // for (const value of Object.values(curObject)) {\n // this.safeObjectVisit(value, obj => this.visitObjects(obj, visitor));\n // }\n // }\n\n public transformNodeSpecific<Safe extends 'safe' | 'unsafe' = 'safe'>(\n curObject: object,\n nodeCallBacks: {[T in Nodes['type']]?: {\n transform?: (op: SafeWrap<Safe, Extract<Nodes, { type: T }>>) => any;\n continue?: (op: Extract<Nodes, { type: T }>) => boolean;\n }},\n nodeSpecificCallBacks: {[Type in Nodes['type']]?: {\n [SubType in Extract<Nodes, { type: Type; subType: string }>['subType']]?: {\n transform?: (op: SafeWrap<Safe, Extract<Nodes, { type: Type; subType: SubType }>>) => any;\n continue?: (op: Extract<Nodes, { type: Type; subType: SubType }>) => boolean;\n }}} = {},\n ): any {\n let continueCheck: ((node: any) => boolean) | undefined;\n let transformation: ((node: any) => unknown) | undefined;\n const casted = <{ type?: Nodes['type']; subType?: string }>curObject;\n if (casted.type && casted.subType) {\n const specific = nodeSpecificCallBacks[casted.type];\n if (specific) {\n continueCheck = specific[<keyof typeof specific> casted.subType]?.continue;\n transformation = specific[<keyof typeof specific> casted.subType]?.transform;\n }\n }\n let shouldContinue = true;\n if (continueCheck) {\n shouldContinue = continueCheck(curObject);\n }\n if (!shouldContinue) {\n return curObject;\n }\n const copy: { type?: unknown } = { ...curObject };\n for (const [ key, value ] of Object.entries(copy)) {\n (<Record<string, unknown>> copy)[key] =\n this.safeObjectVisit(value, obj => this.transformNodeSpecific(obj, nodeCallBacks, nodeSpecificCallBacks));\n }\n if (transformation) {\n return transformation(copy);\n }\n return copy;\n }\n\n /**\n * When both nodeCallBack and NodeSpecific callBack are matched, will only look at nodeSpecifCallback\n */\n public visitNodeSpecific(\n curObject: object,\n nodeCallBacks: {[T in Nodes['type']]?: (op: Extract<Nodes, { type: T }>) => boolean | void },\n nodeSpecificCallBacks: {[Type in Nodes['type']]?:\n {[Subtype in Extract<Nodes, { type: Type; subType: string }>['subType']]?:\n (op: Extract<Nodes, { type: Type; subType: Subtype }>) => boolean | void }} = {},\n ): void {\n let callback: ((node: any) => boolean | void) | undefined;\n const casted = <{ type?: Nodes['type']; subType?: string }> curObject;\n if (casted.type && casted.subType) {\n const specific = nodeSpecificCallBacks[casted.type];\n if (specific) {\n callback = specific[<keyof typeof specific>casted.subType];\n }\n }\n if (!callback && casted.type) {\n callback = nodeCallBacks[(<{ type: Nodes['type'] }> curObject).type];\n }\n let shouldIterate = true;\n if (callback) {\n shouldIterate = callback(curObject) ?? true;\n }\n if (shouldIterate) {\n for (const value of Object.values(curObject)) {\n this.safeObjectVisit(value, obj => this.visitNodeSpecific(obj, nodeCallBacks, nodeSpecificCallBacks));\n }\n }\n }\n}\n"]}
1
+ {"version":3,"file":"Transformers.js","sourceRoot":"","sources":["Transformers.ts"],"names":[],"mappings":"AAeA,MAAM,OAAO,eAAe;IAChB,KAAK,CAAI,GAAM;QACvB,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC,CAAC;QACzD,MAAM,CAAC,gBAAgB,CACrB,MAAM,EACN,MAAM,CAAC,yBAAyB,CAAC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AO,CAAC,IAAI,CAAC,CAAC;YAC5D,IAAI,MAAM,EAAE,CAAC;gBACX,SAAS,GAAG,MAAM,CAAsB,OAAO,CAAC,OAAO,CAAC,CAAC;YAC3D,CAAC;YACD,IAAI,CAAC,SAAS,EAAE,CAAC;gBACf,SAAS,GAAG,YAAY,CAAgB,OAAO,CAAC,IAAI,CAAC,CAAC;YACxD,CAAC;YACD,IAAI,SAAS,EAAE,CAAC;gBACd,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,SAAS,CAAM,OAAO,CAAC,CAAC;gBACnD,WAAW,GAAG,QAAQ,IAAI,KAAK,CAAC;gBAChC,IAAI,CAAC,WAAW,EAAE,CAAC;oBACjB,KAAK,MAAM,IAAI,IAAI,IAAI,IAAI,EAAE,EAAE,CAAC;wBAC9B,IAAI,WAAW,EAAE,CAAC;4BAChB,OAAO;wBACT,CAAC;wBACD,OAAO,CAAC,IAAI,CAAC,CAAC;oBAChB,CAAC;gBACH,CAAC;YACH,CAAC;QACH,CAAC,CAAC;QAEF,OAAO,CAAC,WAAW,CAAC,CAAC;IACvB,CAAC;CACF","sourcesContent":["import type { Node } from './nodeTypings.js';\n\ntype Safeness = 'safe' | 'unsafe';\ntype SafeWrap<Safe extends Safeness, obj extends object> = Safe extends 'safe' ? {[key in keyof obj]: unknown } : obj;\n\nexport interface VisitContext {\n shortcut?: boolean;\n continue?: boolean;\n}\n\nexport interface SelectiveTraversalContext<Nodes> {\n next?: Nodes[];\n shortcut?: boolean;\n}\n\nexport class TransformerType<Nodes extends Pick<Node, 'type'>> {\n protected clone<T>(obj: T): T {\n const newObj = Object.create(Object.getPrototypeOf(obj));\n Object.defineProperties(\n newObj,\n Object.getOwnPropertyDescriptors(obj),\n );\n return newObj;\n }\n\n protected safeObjectVisit(value: unknown, mapper: (some: object) => any): unknown {\n if (value && typeof value === 'object') {\n if (Array.isArray(value)) {\n return value.map(x => this.safeObjectVisit(x, mapper));\n }\n return mapper(value);\n }\n return value;\n }\n\n /**\n * Recursively transforms all objects that are not arrays. Mapper is called on deeper objects first.\n * @param startObject object to start iterating from\n * @param mapper mapper to transform the various objects - argument is a copy of the original\n * @param preVisitor callback that is evaluated before iterating deeper.\n * If continues is false, we do not iterate deeper, current object is still mapped. - default: true\n * If shortcut is true, we do not iterate deeper, nor do we branch out, this mapper will be the last one called.\n * - Default false\n */\n public transformObject(\n startObject: object,\n mapper: (some: object) => any,\n preVisitor: (some: object) => VisitContext = () => ({}),\n ): unknown {\n let didShortCut = false;\n const recurse = (curObject: object): unknown => {\n const copy = this.clone(curObject);\n const context = preVisitor(copy);\n didShortCut = context.shortcut ?? false;\n const continues = context.continue ?? true;\n if (continues && !didShortCut) {\n for (const [ key, value ] of Object.entries(copy)) {\n if (didShortCut) {\n return copy;\n }\n (<Record<string, unknown>> copy)[key] =\n this.safeObjectVisit(value, obj => recurse(obj));\n }\n }\n return mapper(copy);\n };\n\n return recurse(startObject);\n }\n\n /**\n * Visitor that visits all objects. Visits deeper objects first.\n */\n public visitObject(\n startObject: object,\n visitor: (some: object) => void,\n preVisitor: (some: object) => VisitContext = () => ({}),\n ): void {\n let didShortCut = false;\n\n const recurse = (curObject: object): void => {\n const context = preVisitor(curObject);\n didShortCut = context.shortcut ?? false;\n const continues = context.continue ?? true;\n if (continues && !didShortCut) {\n for (const value of Object.values(curObject)) {\n if (didShortCut) {\n return;\n }\n this.safeObjectVisit(value, obj => recurse(obj));\n }\n }\n visitor(curObject);\n };\n recurse(startObject);\n }\n\n public transformNode<Safe extends 'safe' | 'unsafe' = 'safe'>(\n startObject: object,\n nodeCallBacks: {[T in Nodes['type']]?: {\n transform?: (op: SafeWrap<Safe, Extract<Nodes, { type: T }>>) => any;\n preVisitor?: (op: Extract<Nodes, { type: T }>) => VisitContext;\n }},\n ): any {\n const transformWrapper = (curObject: object): unknown => {\n const casted = <{ type?: Nodes['type'] }>curObject;\n if (casted.type) {\n const ogFunc: ((node: any) => unknown) | undefined = nodeCallBacks[casted.type]?.transform;\n if (ogFunc) {\n return ogFunc(casted);\n }\n }\n return curObject;\n };\n const preTransformWrapper = (curObject: object): VisitContext => {\n const casted = <{ type?: Nodes['type'] }>curObject;\n if (casted.type) {\n const ogFunc: ((node: any) => VisitContext) | undefined = nodeCallBacks[casted.type]?.preVisitor;\n if (ogFunc) {\n return ogFunc(casted);\n }\n }\n return {};\n };\n return this.transformObject(startObject, transformWrapper, preTransformWrapper);\n }\n\n public visitNode(\n startObject: object,\n nodeCallBacks: {[T in Nodes['type']]?: {\n visitor?: (op: Extract<Nodes, { type: T }>) => void;\n preVisitor?: (op: Extract<Nodes, { type: T }>) => VisitContext;\n }},\n ): void {\n const visitWrapper = (curObject: object): void => {\n const casted = <{ type?: Nodes['type'] }>curObject;\n if (casted.type) {\n const ogFunc: ((node: any) => unknown) | undefined = nodeCallBacks[casted.type]?.visitor;\n if (ogFunc) {\n ogFunc(casted);\n }\n }\n };\n const preVisitWrapper = (curObject: object): VisitContext => {\n const casted = <{ type?: Nodes['type'] }>curObject;\n if (casted.type) {\n const ogFunc: ((node: any) => VisitContext) | undefined = nodeCallBacks[casted.type]?.preVisitor;\n if (ogFunc) {\n return ogFunc(casted);\n }\n }\n return {};\n };\n this.visitObject(startObject, visitWrapper, preVisitWrapper);\n }\n\n public traverseNodes(\n currentNode: Nodes,\n traverse: {[T in Nodes['type']]?: (op: Extract<Nodes, { type: T }>) => SelectiveTraversalContext<Nodes> },\n ): void {\n let didShortCut = false;\n\n const recurse = (curNode: Nodes): void => {\n const traverser = traverse[<Nodes['type']>curNode.type];\n if (traverser) {\n const { next, shortcut } = traverser(<any>curNode);\n didShortCut = shortcut ?? false;\n if (!didShortCut) {\n for (const node of next ?? []) {\n if (didShortCut) {\n return;\n }\n recurse(node);\n }\n }\n }\n };\n\n recurse(currentNode);\n }\n}\n\nexport class TransformerSubType<Nodes extends Pick<Node, 'type' | 'subType'>> extends TransformerType<Nodes> {\n public transformNodeSpecific<Safe extends 'safe' | 'unsafe' = 'safe'>(\n startObject: object,\n nodeCallBacks: {[T in Nodes['type']]?: {\n transform?: (op: SafeWrap<Safe, Extract<Nodes, { type: T }>>) => any;\n preVisitor?: (op: Extract<Nodes, { type: T }>) => VisitContext;\n }},\n nodeSpecificCallBacks: {[Type in Nodes['type']]?: {\n [SubType in Extract<Nodes, { type: Type; subType: string }>['subType']]?: {\n transform?: (op: SafeWrap<Safe, Extract<Nodes, { type: Type; subType: SubType }>>) => any;\n preVisitor?: (op: Extract<Nodes, { type: Type; subType: SubType }>) => VisitContext;\n }}},\n ): any {\n const transformWrapper = (curObject: object): unknown => {\n let ogTransform: ((node: any) => unknown) | undefined;\n const casted = <{ type?: Nodes['type']; subType?: string }>curObject;\n if (casted.type && casted.subType) {\n const specific = nodeSpecificCallBacks[casted.type];\n if (specific) {\n ogTransform = specific[<keyof typeof specific> casted.subType]?.transform;\n }\n if (!ogTransform) {\n ogTransform = nodeCallBacks[casted.type]?.transform;\n }\n }\n return ogTransform ? ogTransform(casted) : curObject;\n };\n const preVisitWrapper = (curObject: object): VisitContext => {\n let ogPreVisit: ((node: any) => VisitContext) | undefined;\n const casted = <{ type?: Nodes['type']; subType?: string }>curObject;\n if (casted.type && casted.subType) {\n const specific = nodeSpecificCallBacks[casted.type];\n if (specific) {\n ogPreVisit = specific[<keyof typeof specific> casted.subType]?.preVisitor;\n }\n if (!ogPreVisit) {\n ogPreVisit = nodeCallBacks[casted.type]?.preVisitor;\n }\n }\n return ogPreVisit ? ogPreVisit(casted) : curObject;\n };\n return this.transformObject(startObject, transformWrapper, preVisitWrapper);\n }\n\n /**\n * When both nodeCallBack and NodeSpecific callBack are matched, will only look at nodeSpecifCallback\n */\n public visitNodeSpecific(\n startObject: object,\n nodeCallBacks: {[T in Nodes['type']]?: {\n visitor?: (op: Extract<Nodes, { type: T }>) => void;\n preVisitor?: (op: Extract<Nodes, { type: T }>) => VisitContext;\n }},\n nodeSpecificCallBacks: {[Type in Nodes['type']]?:\n {[Subtype in Extract<Nodes, { type: Type; subType: string }>['subType']]?: {\n visitor?: (op: Extract<Nodes, { type: Type; subType: Subtype }>) => void;\n preVisitor?: (op: Extract<Nodes, { type: Type; subType: Subtype }>) => VisitContext;\n }}},\n ): void {\n const visitWrapper = (curObject: object): void => {\n let ogTransform: ((node: any) => void) | undefined;\n const casted = <{ type?: Nodes['type']; subType?: string }>curObject;\n if (casted.type && casted.subType) {\n const specific = nodeSpecificCallBacks[casted.type];\n if (specific) {\n ogTransform = specific[<keyof typeof specific> casted.subType]?.visitor;\n }\n if (!ogTransform) {\n ogTransform = nodeCallBacks[casted.type]?.visitor;\n }\n }\n if (ogTransform) {\n ogTransform(casted);\n }\n };\n const preVisitWrapper = (curObject: object): VisitContext => {\n let ogPreVisit: ((node: any) => VisitContext) | undefined;\n const casted = <{ type?: Nodes['type']; subType?: string }>curObject;\n if (casted.type && casted.subType) {\n const specific = nodeSpecificCallBacks[casted.type];\n if (specific) {\n ogPreVisit = specific[<keyof typeof specific> casted.subType]?.preVisitor;\n }\n if (!ogPreVisit) {\n ogPreVisit = nodeCallBacks[casted.type]?.preVisitor;\n }\n }\n return ogPreVisit ? ogPreVisit(casted) : curObject;\n };\n this.visitObject(startObject, visitWrapper, preVisitWrapper);\n }\n\n public traverseSubNodes(\n currentNode: Nodes,\n traverseNode: {[Type in Nodes['type']]?:\n (op: Extract<Nodes, { type: Type }>) => SelectiveTraversalContext<Nodes> },\n traverseSubNode: {[Type in Nodes['type']]?:\n {[Subtype in Extract<Nodes, { type: Type; subType: string }>['subType']]?:\n (op: Extract<Nodes, { type: Type; subType: Subtype }>) => SelectiveTraversalContext<Nodes> }},\n ): void {\n let didShortCut = false;\n\n const recurse = (curNode: Nodes): void => {\n let traverser: ((call: any) => SelectiveTraversalContext<Nodes>) | undefined;\n const subObj = traverseSubNode[<Nodes['type']>curNode.type];\n if (subObj) {\n traverser = subObj[<keyof typeof subObj>curNode.subType];\n }\n if (!traverser) {\n traverser = traverseNode[<Nodes['type']>curNode.type];\n }\n if (traverser) {\n const { next, shortcut } = traverser(<any>curNode);\n didShortCut = shortcut ?? false;\n if (!didShortCut) {\n for (const node of next ?? []) {\n if (didShortCut) {\n return;\n }\n recurse(node);\n }\n }\n }\n };\n\n recurse(currentNode);\n }\n}\n"]}
@@ -1,9 +1,10 @@
1
- import { CoreFactory } from '../CoreFactory.js';
1
+ import { AstCoreFactory } from '../AstCoreFactory.js';
2
+ import { traqulaIndentation } from '../utils.js';
2
3
  import type { GenRuleMap } from './builderTypes.js';
3
4
  import type { RuleDefArg } from './generatorTypes.js';
4
5
  export declare class DynamicGenerator<Context, Names extends string, RuleDefs extends GenRuleMap<Names>> {
5
6
  protected rules: RuleDefs;
6
- protected readonly factory: CoreFactory;
7
+ protected readonly factory: AstCoreFactory;
7
8
  protected __context: Context | undefined;
8
9
  protected origSource: string;
9
10
  protected generatedUntil: number;
@@ -11,7 +12,9 @@ export declare class DynamicGenerator<Context, Names extends string, RuleDefs ex
11
12
  protected readonly stringBuilder: string[];
12
13
  constructor(rules: RuleDefs);
13
14
  setContext(context: Context): void;
14
- protected getSafeContext(): Context;
15
+ protected getSafeContext(): Context & {
16
+ [traqulaIndentation]?: unknown;
17
+ };
15
18
  protected readonly subrule: RuleDefArg['SUBRULE'];
16
19
  protected readonly handleLoc: RuleDefArg['HANDLE_LOC'];
17
20
  protected readonly catchup: RuleDefArg['CATCHUP'];
@@ -19,4 +22,5 @@ export declare class DynamicGenerator<Context, Names extends string, RuleDefs ex
19
22
  private readonly printWord;
20
23
  private readonly printSpaceLeft;
21
24
  private readonly printWords;
25
+ private readonly printOnEmpty;
22
26
  }