@ptolemy2002/rgx 13.9.0 → 13.11.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/README.md CHANGED
@@ -41,6 +41,9 @@ Because there is so much to document, it has been broken up into multiple files.
41
41
  - [lookahead](./docs/class/token/lookaround/lookahead.md) - The `RGXLookaheadToken` class, which represents a lookahead assertion in a regular expression.
42
42
  - [lookbehind](./docs/class/token/lookaround/lookbehind.md) - The `RGXLookbehindToken` class, which represents a lookbehind assertion in a regular expression.
43
43
 
44
+ ## Pending Breaking Changes
45
+ Breaking changes that would improve the library, but are not worth a major release by themselves, are tracked in [PENDING_BREAKING_CHANGES.md](./PENDING_BREAKING_CHANGES.md) along with the non-breaking workarounds currently in place for them. They will be made in the next major release.
46
+
44
47
  ## Peer Dependencies
45
48
  N/A
46
49
 
@@ -1,4 +1,4 @@
1
- import { RGXConvertibleToken, RGXJSONClassToken, RGXJSONValue, RGXToken, ValidRegexFlags, ValidRegexString } from "../types";
1
+ import { RGXConvertibleToken, RGXJSONClassToken, RGXJSONObjectToken, RGXJSONValue, RGXToken, ValidRegexFlags, ValidRegexString } from "../types";
2
2
  import { RGXTokenCollectionInput } from "../collection";
3
3
  import { ResolveRGXTokenOptions } from "../resolve";
4
4
  import { CloneDepth } from "@ptolemy2002/immutability-utils";
@@ -11,7 +11,7 @@ import type { RGXExclusionToken } from "./exclusion";
11
11
  export declare abstract class RGXClassToken implements RGXConvertibleToken {
12
12
  abstract toRgx(): RGXToken;
13
13
  abstract clone(depth?: CloneDepth): ThisType<this>;
14
- toJSON(): RGXJSONClassToken;
14
+ toJSON(): RGXJSONObjectToken;
15
15
  static validateJSONArgs(args: RGXJSONValue[]): boolean | string;
16
16
  static fromJSON(json: RGXJSONClassToken): RGXClassToken;
17
17
  rgxAcceptInsertion(tokens: RGXToken[], flags: ValidRegexFlags): string | boolean;
@@ -1,21 +1,24 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RGXClassToken = void 0;
4
+ const types_1 = require("../types");
4
5
  const errors_1 = require("../errors");
5
6
  const resolve_1 = require("../resolve");
6
7
  class RGXClassToken {
7
- // Subclasses are expected to override toJSON and both static methods below. They are not marked abstract
8
- // (so existing subclasses keep compiling), so they throw RGXNotImplementedError by default.
9
- // toJSON must return a JSON class token whose args, when given to the registered constructor for the
10
- // class, reconstruct an equivalent token.
8
+ // Subclasses that want lossless JSON serialization (i.e. to be part of the JSON class registry) should override
9
+ // toJSON and both static methods below. They are not marked abstract, since subclasses that are not registered
10
+ // remain serializable through the toJSON fallback. The static methods throw RGXNotImplementedError by default.
11
+ // When overridden, toJSON must return a JSON class token whose args, when given to the registered constructor for the
12
+ // class, reconstruct an equivalent token. By default, it falls back to resolving the token into a
13
+ // JSON literal token, the same way plain convertible tokens are handled by rgxTokenToJSON. This is lossy.
11
14
  toJSON() {
12
- throw new errors_1.RGXNotImplementedError(`${this.constructor.name}.toJSON()`, 'subclasses must override this method.');
15
+ return { [types_1.RGX_JSON_FLAG]: true, source: (0, resolve_1.resolveRGXToken)(this, { groupWrap: false }) };
13
16
  }
14
17
  static validateJSONArgs(args) {
15
- throw new errors_1.RGXNotImplementedError('RGXClassToken.validateJSONArgs(args)', 'subclasses must override this static method.');
18
+ throw new errors_1.RGXNotImplementedError('RGXClassToken.validateJSONArgs(args)', 'subclasses must override this static method in order to be registered in the JSON class registry.');
16
19
  }
17
20
  static fromJSON(json) {
18
- throw new errors_1.RGXNotImplementedError('RGXClassToken.fromJSON(json)', 'subclasses must override this static method.');
21
+ throw new errors_1.RGXNotImplementedError('RGXClassToken.fromJSON(json)', 'subclasses must override this static method in order to be registered in the JSON class registry.');
19
22
  }
20
23
  rgxAcceptInsertion(tokens, flags) {
21
24
  return true;
package/dist/json.js CHANGED
@@ -59,6 +59,22 @@ const collection_1 = require("./collection");
59
59
  const ExtRegExp_1 = require("./ExtRegExp");
60
60
  const resolve_1 = require("./resolve");
61
61
  const rgxJSONClasses = {};
62
+ // Maps args arrays that have already passed the validator of the class they are mapped to and are currently
63
+ // being constructed. rgxJSONClassArgs uses this to skip validating them a second time.
64
+ const validatedRGXJSONClassArgs = new WeakMap();
65
+ function constructValidatedRGXJSONClass(name, raw, args) {
66
+ validatedRGXJSONClassArgs.set(args, name);
67
+ try {
68
+ return raw.construct(args);
69
+ }
70
+ finally {
71
+ validatedRGXJSONClassArgs.delete(args);
72
+ }
73
+ }
74
+ function getRGXJSONClassRecord(name) {
75
+ assertHasRGXJSONClass(name);
76
+ return rgxJSONClasses[name];
77
+ }
62
78
  // ------------------ Registry ------------------
63
79
  function listRGXJSONClasses() {
64
80
  return Object.keys(rgxJSONClasses);
@@ -78,8 +94,17 @@ function assertNotHasRGXJSONClass(name) {
78
94
  }
79
95
  function registerRGXJSONClass(name, entry) {
80
96
  assertNotHasRGXJSONClass(name);
81
- rgxJSONClasses[name] = entry;
82
- return entry;
97
+ // Copied so that later changes to the given entry cannot bypass the guard.
98
+ const raw = { validateArgs: entry.validateArgs, construct: entry.construct };
99
+ const guarded = Object.freeze({
100
+ validateArgs: raw.validateArgs,
101
+ construct: (args) => {
102
+ assertValidRGXJSONClassArgs(name, args);
103
+ return constructValidatedRGXJSONClass(name, raw, args);
104
+ }
105
+ });
106
+ rgxJSONClasses[name] = { raw, guarded };
107
+ return guarded;
83
108
  }
84
109
  function registerRGXJSONClassToken(name, constructor) {
85
110
  return registerRGXJSONClass(name, {
@@ -88,15 +113,14 @@ function registerRGXJSONClassToken(name, constructor) {
88
113
  });
89
114
  }
90
115
  function getRGXJSONClass(name) {
91
- assertHasRGXJSONClass(name);
92
- return rgxJSONClasses[name];
116
+ return getRGXJSONClassRecord(name).guarded;
93
117
  }
94
118
  function unregisterRGXJSONClass(name) {
95
119
  assertHasRGXJSONClass(name);
96
120
  delete rgxJSONClasses[name];
97
121
  }
98
122
  function validateRGXJSONClassArgs(name, args) {
99
- const result = getRGXJSONClass(name).validateArgs(args);
123
+ const result = getRGXJSONClassRecord(name).raw.validateArgs(args);
100
124
  if (result === true)
101
125
  return true;
102
126
  if (result === false)
@@ -129,6 +153,9 @@ function assertRGXJSONClassTokenOf(json, name) {
129
153
  function rgxJSONClassArgs(json, name, validate) {
130
154
  assertRGXJSONClassTokenOf(json, name);
131
155
  const args = json.args ?? [];
156
+ // The registry already validated these args against this class before constructing.
157
+ if (validatedRGXJSONClassArgs.get(args) === name)
158
+ return args;
132
159
  const result = validate(args);
133
160
  if (result === true)
134
161
  return args;
@@ -153,13 +180,14 @@ function rgxTokenToJSON(token, { resolveConvertible = true } = {}) {
153
180
  tg.assertRGXJSONObjectToken(token);
154
181
  return token;
155
182
  }
156
- if (class_1.RGXClassToken.check(token))
183
+ if (class_1.RGXClassToken.check(token) && token.toJSON !== class_1.RGXClassToken.prototype.toJSON)
157
184
  return token.toJSON();
158
185
  if (collection_1.RGXTokenCollection.check(token))
159
186
  return (0, class_1.toRGXClassToken)(token).toJSON();
187
+ // Class tokens without a custom toJSON fall through to here, since they are also convertible tokens.
160
188
  if (tg.isRGXConvertibleToken(token, false)) {
161
189
  if (!resolveConvertible) {
162
- throw new e.RGXNotJSONSerializableError("Cannot convert token to JSON", token, "Convertible tokens that are not class tokens can only be converted by resolving them, but resolveConvertible is false.");
190
+ throw new e.RGXNotJSONSerializableError("Cannot convert token to JSON", token, "Convertible tokens that are not class tokens with a custom toJSON method can only be converted by resolving them, but resolveConvertible is false.");
163
191
  }
164
192
  return createRGXJSONLiteralToken((0, resolve_1.resolveRGXToken)(token, { groupWrap: false }));
165
193
  }
@@ -178,9 +206,8 @@ function rgxTokenFromJSON(json) {
178
206
  return new ExtRegExp_1.ExtRegExp(json.source, json.flags ?? '');
179
207
  }
180
208
  if (tg.isRGXJSONClassToken(json, false)) {
181
- const args = json.args ?? [];
182
- assertValidRGXJSONClassArgs(json.class, args);
183
- return getRGXJSONClass(json.class).construct(args);
209
+ // The guarded construct validates the args before constructing.
210
+ return getRGXJSONClass(json.class).construct(json.args ?? []);
184
211
  }
185
212
  if (Array.isArray(json))
186
213
  return json.map(item => rgxTokenFromJSON(item));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptolemy2002/rgx",
3
- "version": "13.9.0",
3
+ "version": "13.11.0",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -43,13 +43,14 @@
43
43
  "ts-jest": "^29.1.0",
44
44
  "ts-patch": "^3.3.0",
45
45
  "tsconfig-paths": "^4.2.0",
46
+ "typescript": "^5.9.3",
46
47
  "typescript-transform-paths": "^3.5.3"
47
48
  },
48
49
  "dependencies": {
49
50
  "@ptolemy2002/immutability-utils": "^2.0.0",
50
51
  "@ptolemy2002/js-utils": "^3.2.2",
51
52
  "@ptolemy2002/ts-brand-utils": "^1.0.0",
52
- "@ptolemy2002/ts-utils": "^3.4.0",
53
+ "@ptolemy2002/ts-utils": "^4.0.0",
53
54
  "is-callable": "^1.2.7",
54
55
  "lodash.clonedeep": "^4.5.0"
55
56
  }