@ptolemy2002/rgx 13.3.3 → 13.3.4

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.
Files changed (2) hide show
  1. package/dist/resolve.js +33 -17
  2. package/package.json +1 -1
package/dist/resolve.js CHANGED
@@ -58,9 +58,39 @@ function localizableVanillaRegexFlagDiff(prev, next) {
58
58
  return `${added}`;
59
59
  return `${added}-${removed}`;
60
60
  }
61
+ function hasParenErrors(pattern) {
62
+ let depth = 0;
63
+ let charClassDepth = 0;
64
+ for (let i = 0; i < pattern.length; i++) {
65
+ const ch = pattern[i];
66
+ if (ch === '\\') {
67
+ i++;
68
+ continue;
69
+ }
70
+ if (charClassDepth > 0) {
71
+ if (ch === '[')
72
+ charClassDepth++;
73
+ else if (ch === ']')
74
+ charClassDepth--;
75
+ continue;
76
+ }
77
+ if (ch === '[') {
78
+ charClassDepth++;
79
+ }
80
+ else if (ch === '(') {
81
+ depth++;
82
+ }
83
+ else if (ch === ')') {
84
+ depth--;
85
+ if (depth < 0)
86
+ return true;
87
+ }
88
+ }
89
+ return depth !== 0;
90
+ }
61
91
  function resolveRGXToken(token, { groupWrap = true, topLevel = true, currentFlags = '' } = {}) {
62
92
  (0, ExtRegExp_1.assertValidRegexFlags)(currentFlags);
63
- let acceptUnterminatedGroup = false;
93
+ let acceptParenErrors = false;
64
94
  const innerResolve = () => {
65
95
  if (tg.isRGXNoOpToken(token))
66
96
  return '';
@@ -86,7 +116,7 @@ function resolveRGXToken(token, { groupWrap = true, topLevel = true, currentFlag
86
116
  // will be caught by one of the checks that the entire resolved string
87
117
  // is a valid regex string.
88
118
  if (token.rgxInterpolate) {
89
- acceptUnterminatedGroup = true;
119
+ acceptParenErrors = true;
90
120
  return String(token.toRgx());
91
121
  }
92
122
  // The top-level group-wrapping preference propogates to a direct convertible token, but after that
@@ -113,21 +143,7 @@ function resolveRGXToken(token, { groupWrap = true, topLevel = true, currentFlag
113
143
  throw new e.RGXInvalidTokenError(`Invalid RGX token: ${token}`, null, token);
114
144
  };
115
145
  const result = innerResolve();
116
- try {
146
+ if (!(acceptParenErrors && hasParenErrors(result)))
117
147
  tg.assertValidRegexString(result);
118
- }
119
- catch (err) {
120
- if (err instanceof e.RGXInvalidRegexStringError) {
121
- if (acceptUnterminatedGroup &&
122
- (err.cause.message.endsWith('Unterminated group') ||
123
- err.cause.message.endsWith("Unmatched ')'"))) {
124
- return result;
125
- }
126
- }
127
- // This is ignored because I don't know what kind of
128
- // unexpected errors might happen.
129
- /* istanbul ignore next */
130
- throw err;
131
- }
132
148
  return result;
133
149
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptolemy2002/rgx",
3
- "version": "13.3.3",
3
+ "version": "13.3.4",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",