s2cfgtojson 4.3.1 → 4.5.0

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/Struct.mts CHANGED
@@ -345,7 +345,8 @@ export function createDynamicClassInstance<T extends Struct = Struct>(
345
345
  rawName: string,
346
346
  index?: number,
347
347
  ): T {
348
- const name = parseStructName(rawName) || `UnnamedStruct${index}`;
348
+ const parsedName = parseStructName(rawName) || `UnnamedStruct${index}`;
349
+ const name = makeSafeClassName(parsedName);
349
350
  return new (new Function(
350
351
  "parent",
351
352
  "Refs",
@@ -492,6 +493,59 @@ function parseStructName(name: string): string {
492
493
  .replace(/^_+/, "");
493
494
  }
494
495
 
496
+ const RESERVED_IDENTIFIERS = new Set([
497
+ "break",
498
+ "case",
499
+ "catch",
500
+ "class",
501
+ "const",
502
+ "continue",
503
+ "debugger",
504
+ "default",
505
+ "delete",
506
+ "do",
507
+ "else",
508
+ "export",
509
+ "extends",
510
+ "finally",
511
+ "for",
512
+ "function",
513
+ "if",
514
+ "import",
515
+ "in",
516
+ "instanceof",
517
+ "new",
518
+ "return",
519
+ "super",
520
+ "switch",
521
+ "this",
522
+ "throw",
523
+ "try",
524
+ "typeof",
525
+ "var",
526
+ "void",
527
+ "while",
528
+ "with",
529
+ "yield",
530
+ "enum",
531
+ "await",
532
+ "implements",
533
+ "package",
534
+ "protected",
535
+ "static",
536
+ "interface",
537
+ "private",
538
+ "public",
539
+ "let",
540
+ ]);
541
+
542
+ function makeSafeClassName(name: string): string {
543
+ if (!/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name) || RESERVED_IDENTIFIERS.has(name)) {
544
+ return `_${name}`;
545
+ }
546
+ return name;
547
+ }
548
+
495
549
  function maybeMinifyKey(key: string, minify: boolean) {
496
550
  return minify &&
497
551
  (INTERNAL_PROPS.has(key as any) || REF_INTERNAL_PROPS.has(key as any))
package/Struct.test.mts CHANGED
@@ -91,6 +91,14 @@ struct.end`,
91
91
  `DynamicClass : struct.begin\n\nstruct.end`,
92
92
  );
93
93
  });
94
+
95
+ test("reserved keyword name", () => {
96
+ const instance = createDynamicClassInstance("default");
97
+ instance.__internal__.isRoot = true;
98
+
99
+ expect(instance).toBeInstanceOf(Struct);
100
+ expect(instance.toString()).toBe(`default : struct.begin\n\nstruct.end`);
101
+ });
94
102
  });
95
103
 
96
104
  describe("fromString()", () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "s2cfgtojson",
3
- "version": "4.3.1",
3
+ "version": "4.5.0",
4
4
  "description": "Converts Stalker 2 Cfg file into POJOs",
5
5
  "keywords": [
6
6
  "stalker",
package/readme.md CHANGED
@@ -53,7 +53,7 @@ BasePhantomAttack : struct.begin {refkey=BaseAttackAbility}
53
53
  struct.end
54
54
  `;
55
55
  const parsed = Struct.fromString(configText)[0];
56
- console.log(parsed.entries.TriggeredCooldowns);
56
+ console.log(parsed.TriggeredCooldowns);
57
57
  ```
58
58
 
59
59
  ---