json-as 1.1.7 → 1.1.9

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.
@@ -1,5 +1,5 @@
1
1
  // Taken from https://github.com/as-pect/visitor-as/blob/master/src/simpleParser.ts
2
- import { Parser, Tokenizer, Source, SourceKind, Expression, Statement, NamespaceDeclaration, ClassDeclaration, DeclarationStatement, Range, Node } from "assemblyscript/dist/assemblyscript.js";
2
+ import { Parser, Tokenizer, Source, SourceKind, Expression, Statement, NamespaceDeclaration, ClassDeclaration, DeclarationStatement, Range, Node, NodeKind } from "assemblyscript/dist/assemblyscript.js";
3
3
  import { ASTBuilder } from "./builder.js";
4
4
 
5
5
  export class SimpleParser {
@@ -54,3 +54,69 @@ export function isStdlib(s: Source | { range: Range }): boolean {
54
54
  export function toString(node: Node): string {
55
55
  return ASTBuilder.build(node);
56
56
  }
57
+
58
+ export function replaceRef(node: Node, replacement: Node | Node[], ref: Node | Node[] | null): void {
59
+ if (!node || !ref) return;
60
+ const nodeExpr = stripExpr(node);
61
+
62
+ if (Array.isArray(ref)) {
63
+ for (let i = 0; i < ref.length; i++) {
64
+ if (stripExpr(ref[i]) === nodeExpr) {
65
+ if (Array.isArray(replacement)) ref.splice(i, 1, ...replacement);
66
+ else ref.splice(i, 1, replacement);
67
+ return; // Exit early after replacement
68
+ }
69
+ }
70
+ } else if (typeof ref === "object") {
71
+ for (const key of Object.keys(ref)) {
72
+ const current = ref[key] as Node | Node[];
73
+ if (Array.isArray(current)) {
74
+ for (let i = 0; i < current.length; i++) {
75
+ if (stripExpr(current[i]) === nodeExpr) {
76
+ if (Array.isArray(replacement)) current.splice(i, 1, ...replacement);
77
+ else current.splice(i, 1, replacement);
78
+ return;
79
+ }
80
+ }
81
+ } else if (stripExpr(current) === nodeExpr) {
82
+ ref[key] = replacement;
83
+ return;
84
+ }
85
+ }
86
+ }
87
+ }
88
+
89
+ export function cloneNode(input: Node | Node[] | null, seen = new WeakMap(), path = ""): Node | Node[] | null {
90
+ if (input === null || typeof input !== "object") return input;
91
+
92
+ if (Array.isArray(input)) {
93
+ return input.map((item, index) => cloneNode(item, seen, `${path}[${index}]`)) as Node | Node[] | null;
94
+ }
95
+
96
+ if (seen.has(input)) return seen.get(input);
97
+
98
+ const prototype = Object.getPrototypeOf(input);
99
+ const clone = Array.isArray(input) ? [] : Object.create(prototype);
100
+ seen.set(input, clone);
101
+
102
+ for (const key of Reflect.ownKeys(input)) {
103
+ const value = input[key];
104
+ const newPath = path ? `${path}.${String(key)}` : String(key);
105
+
106
+ if (newPath.endsWith(".source")) {
107
+ clone[key] = value;
108
+ } else if (value && typeof value === "object") {
109
+ clone[key] = cloneNode(value, seen, newPath);
110
+ } else {
111
+ clone[key] = value;
112
+ }
113
+ }
114
+
115
+ return clone as Node | Node[] | null;
116
+ }
117
+
118
+ export function stripExpr(node: Node): Node {
119
+ if (!node) return node;
120
+ if (node.kind == NodeKind.Expression) return node["expression"];
121
+ return node;
122
+ }