@cosyte/vitest-config 0.1.2 → 0.1.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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.1.3
4
+
5
+ ### Patch Changes
6
+
7
+ - 095c103: `@cosyte/vitest-config/snippets` reads a markdown fence opener and a `// =>` doctest line in time linear in the line's length. Two regular expressions there let several quantifiers take the same spaces, so a long line that failed to match took time quadratic in its length: 100,000 spaces took 8 to 32 seconds on Node 26 (CodeQL js/polynomial-redos). Every snippet with an expression and a value is read as before. A doctest line with no expression, `; // => 1`, now becomes `expect()`, which fails the snippet, where an unindented one used to pass as a line that asserted nothing.
8
+
3
9
  ## 0.1.2
4
10
 
5
11
  ### Patch Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cosyte/vitest-config",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "Shared Vitest config (v8 coverage, per-directory >=90 gates) for @cosyte/* packages.",
5
5
  "keywords": [
6
6
  "vitest",
package/snippets.js CHANGED
@@ -30,6 +30,30 @@ const RUNNABLE_LANGS = new Set(["ts", "typescript", "tsx"]);
30
30
  /** Monotonic counter so every emitted temp module gets a unique, cache-busting filename. */
31
31
  let moduleCounter = 0;
32
32
 
33
+ /**
34
+ * A fence opener's indentation and its run of three or more backticks or tildes.
35
+ *
36
+ * Nothing follows the run in the pattern: the info string is the rest of the line, cut off by hand.
37
+ * The earlier form, `\s*(.*)$` after the run, had two quantifiers that could both take the same
38
+ * spaces, so a long line that failed to match cost time quadratic in its length (CodeQL
39
+ * js/polynomial-redos). These docs come from the package being tested, and a markdown file is
40
+ * still input this harness should read in linear time.
41
+ */
42
+ const FENCE_OPEN = /^(\s*)(`{3,}|~{3,})/;
43
+
44
+ /**
45
+ * The characters `.` does not match in a JavaScript regular expression. A fence's info string, a
46
+ * doctest expression and its expected value were each `.+` or `.*` in the earlier patterns, so none
47
+ * of them may hold one; the hand-written parsing below keeps that rule.
48
+ */
49
+ const LINE_TERMINATOR = /[\n\r\u2028\u2029]/;
50
+
51
+ /**
52
+ * The `; // =>` marker of a doctest assertion. Each quantifier is followed by a character it cannot
53
+ * match, so a scan of a line is linear in its length.
54
+ */
55
+ const ASSERTION_MARKER = /;\s*\/\/\s*=>/g;
56
+
33
57
  /**
34
58
  * Extract the opt-in runnable snippets from a markdown string.
35
59
  *
@@ -50,12 +74,13 @@ export function extractRunnableSnippets(markdown, opts = {}) {
50
74
  const out = [];
51
75
  let i = 0;
52
76
  while (i < lines.length) {
53
- const open = lines[i].match(/^(\s*)(`{3,}|~{3,})\s*(.*)$/);
54
- if (!open) {
77
+ const open = FENCE_OPEN.exec(lines[i]);
78
+ const info = open ? lines[i].slice(open[0].length).trimStart() : "";
79
+ if (!open || LINE_TERMINATOR.test(info)) {
55
80
  i++;
56
81
  continue;
57
82
  }
58
- const [, indent, fence, info] = open;
83
+ const [, indent, fence] = open;
59
84
  const firstCodeLine = i + 2; // 1-based line of the first body line
60
85
  const body = [];
61
86
  let j = i + 1;
@@ -107,16 +132,46 @@ export function rewriteAssertions(code) {
107
132
  const rewritten = code
108
133
  .split(/\r?\n/)
109
134
  .map((line) => {
110
- const m = line.match(/^(\s*)(.+?);\s*\/\/\s*=>\s*(.+?)\s*$/);
135
+ const m = splitAssertion(line);
111
136
  if (!m) return line;
112
137
  assertions++;
113
- const [, ws, expr, expected] = m;
114
- return `${ws}expect(${expr}).toStrictEqual(${expected});`;
138
+ return `${m.ws}expect(${m.expr}).toStrictEqual(${m.expected});`;
115
139
  })
116
140
  .join("\n");
117
141
  return { code: rewritten, assertions };
118
142
  }
119
143
 
144
+ /**
145
+ * Split a doctest line into its indentation, its expression and its expected value.
146
+ *
147
+ * The first `; // =>` with anything after it splits the line: the expression is what lies between
148
+ * the indentation and that marker, the expected value is the rest, trimmed. A line with no such
149
+ * marker, or whose expression or value holds a line terminator, is not an assertion.
150
+ *
151
+ * Parsed by hand rather than by one pattern: `^(\s*)(.+?);\s*\/\/\s*=>\s*(.+?)\s*$` let several
152
+ * quantifiers take the same spaces, so a long line that failed to match cost time quadratic in its
153
+ * length (CodeQL js/polynomial-redos). This is linear. It agrees with that pattern on every line
154
+ * with an expression and a value. Where the expression is empty (`; // => 1`), it emits `expect()`,
155
+ * which fails the snippet; the pattern emitted `expect( )` for an indented line and left an
156
+ * unindented one as it was, a line that asserts nothing.
157
+ *
158
+ * @param {string} line - One source line.
159
+ * @returns {{ ws: string, expr: string, expected: string } | null} The parts, or `null`.
160
+ */
161
+ function splitAssertion(line) {
162
+ for (const marker of line.matchAll(ASSERTION_MARKER)) {
163
+ const end = marker.index + marker[0].length;
164
+ // Only the last marker can end the line, so this skips at most once.
165
+ if (end === line.length) continue;
166
+ const ws = line.slice(0, line.length - line.trimStart().length);
167
+ const expr = line.slice(ws.length, marker.index);
168
+ const expected = line.slice(end).trim();
169
+ if (LINE_TERMINATOR.test(expr) || LINE_TERMINATOR.test(expected)) return null;
170
+ return { ws, expr, expected };
171
+ }
172
+ return null;
173
+ }
174
+
120
175
  /**
121
176
  * Rewrite the module specifiers in a snippet's `import ... from "<spec>"` clauses.
122
177
  *