@ptolemy2002/rgx 13.9.0 → 13.10.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.
@@ -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
@@ -153,13 +153,14 @@ function rgxTokenToJSON(token, { resolveConvertible = true } = {}) {
153
153
  tg.assertRGXJSONObjectToken(token);
154
154
  return token;
155
155
  }
156
- if (class_1.RGXClassToken.check(token))
156
+ if (class_1.RGXClassToken.check(token) && token.toJSON !== class_1.RGXClassToken.prototype.toJSON)
157
157
  return token.toJSON();
158
158
  if (collection_1.RGXTokenCollection.check(token))
159
159
  return (0, class_1.toRGXClassToken)(token).toJSON();
160
+ // Class tokens without a custom toJSON fall through to here, since they are also convertible tokens.
160
161
  if (tg.isRGXConvertibleToken(token, false)) {
161
162
  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.");
163
+ 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
164
  }
164
165
  return createRGXJSONLiteralToken((0, resolve_1.resolveRGXToken)(token, { groupWrap: false }));
165
166
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptolemy2002/rgx",
3
- "version": "13.9.0",
3
+ "version": "13.10.0",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",