unreflect 0.0.2 → 0.0.3

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/index.d.mts CHANGED
@@ -16,6 +16,7 @@ declare class ReflectionClass {
16
16
  constructor(obj: any);
17
17
  private isPrivateName;
18
18
  private getBaseName;
19
+ private stripPrivateInitializers;
19
20
  setProperty(name: string, value: any): void;
20
21
  getProperty(name: string): Promise<ReflectionProperty | undefined>;
21
22
  getPropertyValue(name: string): Promise<any>;
package/dist/index.mjs CHANGED
@@ -14,6 +14,71 @@ var ReflectionClass = class {
14
14
  getBaseName(name) {
15
15
  return name.startsWith("#") ? name.slice(1) : name;
16
16
  }
17
+ stripPrivateInitializers(source) {
18
+ const lines = source.split("\n");
19
+ const result = [];
20
+ let skipUntilBalanced = false;
21
+ let braceDepth = 0;
22
+ let bracketDepth = 0;
23
+ let parenDepth = 0;
24
+ for (const line of lines) {
25
+ const isMethod = /^\s*#\w+\s*\(/.test(line);
26
+ const fieldMatch = line.match(/^(\s*)(#\w+)/);
27
+ if (!isMethod && fieldMatch?.[1] !== void 0 && fieldMatch[2] && !skipUntilBalanced) {
28
+ result.push(fieldMatch[1] + fieldMatch[2]);
29
+ if (line.includes("=")) {
30
+ for (const ch of line) switch (ch) {
31
+ case "{":
32
+ braceDepth++;
33
+ break;
34
+ case "}":
35
+ braceDepth--;
36
+ break;
37
+ case "[":
38
+ bracketDepth++;
39
+ break;
40
+ case "]":
41
+ bracketDepth--;
42
+ break;
43
+ case "(":
44
+ parenDepth++;
45
+ break;
46
+ case ")":
47
+ parenDepth--;
48
+ break;
49
+ }
50
+ if (braceDepth > 0 || bracketDepth > 0 || parenDepth > 0) skipUntilBalanced = true;
51
+ }
52
+ continue;
53
+ }
54
+ if (skipUntilBalanced) {
55
+ for (const ch of line) switch (ch) {
56
+ case "{":
57
+ braceDepth++;
58
+ break;
59
+ case "}":
60
+ braceDepth--;
61
+ break;
62
+ case "[":
63
+ bracketDepth++;
64
+ break;
65
+ case "]":
66
+ bracketDepth--;
67
+ break;
68
+ case "(":
69
+ parenDepth++;
70
+ break;
71
+ case ")":
72
+ parenDepth--;
73
+ break;
74
+ }
75
+ if (braceDepth === 0 && bracketDepth === 0 && parenDepth === 0) skipUntilBalanced = false;
76
+ continue;
77
+ }
78
+ result.push(line);
79
+ }
80
+ return result.join("\n");
81
+ }
17
82
  setProperty(name, value) {
18
83
  const obj = this.obj;
19
84
  const isPrivate = this.isPrivateName(name);
@@ -25,7 +90,7 @@ var ReflectionClass = class {
25
90
  const proto = Object.getPrototypeOf(obj);
26
91
  const classSource = proto.constructor.toString();
27
92
  if (!classSource.includes(`#${baseName}`)) return;
28
- const modifiedSource = classSource.replace(/#(\w+)\s*=\s*[^;]+;/g, "#$1;").replace(/^(class\s+\w+)\s*\{/, `$1 { static __reflectionSet__(obj, val) { obj.#${baseName} = val; } static __reflectionGet__(obj) { return obj.#${baseName}; }`);
93
+ const modifiedSource = this.stripPrivateInitializers(classSource).replace(/^(class\s+\w+)\s*\{/, `$1 { static __reflectionSet__(obj, val) { obj.#${baseName} = val; } static __reflectionGet__(obj) { return obj.#${baseName}; }`);
29
94
  const ModifiedClass = vm.runInThisContext(`(${modifiedSource})`);
30
95
  Object.setPrototypeOf(obj, ModifiedClass.prototype);
31
96
  const originalProps = Object.getOwnPropertyDescriptors(obj);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "unreflect",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "Access and modify private class fields and methods in JavaScript",
5
5
  "repository": "KABBOUCHI/unreflect",
6
6
  "license": "MIT",