js-dev-tool 1.0.18 → 1.0.20

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "js-dev-tool",
3
- "version": "1.0.18",
3
+ "version": "1.0.20",
4
4
  "bin": {
5
5
  "jstool": "tools.js"
6
6
  },
@@ -50,7 +50,7 @@
50
50
  "fflate": "^0.8.2",
51
51
  "mini-semaphore": "^1.4.4",
52
52
  "replace": "^1.2.2",
53
- "rm-cstyle-cmts": "^3.3.25",
53
+ "rm-cstyle-cmts": "^3.3.26",
54
54
  "terser": "^5.44.1",
55
55
  "tin-args": "^0.1.1"
56
56
  }
package/regex.d.ts CHANGED
@@ -7,9 +7,11 @@
7
7
  */
8
8
  /**
9
9
  * @file js-dev-scripts/regex.d.ts
10
+ * @desc It is recommended to add `"skipLibCheck": true` to your tsconfig.
11
+ * @todo publish as "annotate-regex"
10
12
  */
11
13
  declare global {
12
- interface RegExp/* extends TypedRegExp<string, string>*/ {
14
+ interface RegExp {
13
15
  /**
14
16
  * Executes a search on a string using a regular expression pattern, and returns an array containing the results of that search.
15
17
  * @template {string} P
@@ -38,10 +40,7 @@ declare global {
38
40
  * @param {F} [flags] The flags for the regular expression.
39
41
  * @returns {RegExp & { readonly source: P; readonly flags: F }} A typed RegExp object.
40
42
  */
41
- new <const P extends string, const F extends string = "">(pattern: P, flags?: F): RegExp & {
42
- readonly source: P;
43
- readonly flags: F;
44
- };
43
+ new <const P extends string, const F extends string = "">(pattern: P, flags?: F): TypedRegExp<P, F>;
45
44
  /**
46
45
  * Creates a typed RegExp object with literal types for the pattern and flags.
47
46
  * @template {string} P
@@ -50,10 +49,7 @@ declare global {
50
49
  * @param {F} [flags] The flags for the regular expression.
51
50
  * @returns {RegExp & { readonly source: P; readonly flags: F }} A typed RegExp object.
52
51
  */
53
- <const P extends string, const F extends string = "">(pattern: P, flags?: F): RegExp & {
54
- readonly source: P;
55
- readonly flags: F;
56
- };
52
+ <const P extends string, const F extends string = "">(pattern: P, flags?: F): TypedRegExp<P, F>;
57
53
  readonly "prototype": RegExp;
58
54
  }
59
55
  }
@@ -66,7 +62,7 @@ export type Brand<Tag extends string> = {
66
62
  [k in Tag]: true;
67
63
  };
68
64
  export type TypedRegExpBrand = Brand<"__TypedRegExp">;
69
- export type TypedRegExp<const P extends string, const F extends string = ""> = RegExp & {
65
+ export type TypedRegExp<P extends string, F extends string = ""> = RegExp & {
70
66
  readonly source: P;
71
67
  readonly flags: F;
72
68
  } & TypedRegExpBrand;
@@ -90,7 +86,7 @@ type FirstChar<S extends string> = S extends `${infer F}${infer _}` ? F : never;
90
86
  * without considering nesting, and unions them.
91
87
  * @template S - A string pattern.
92
88
  */
93
- export type ExtractGroupNames<S extends string> =
89
+ export type ExtractGroupNames<S extends unknown> =
94
90
  S extends `${infer _Before}(?<${infer Rest}`
95
91
  ? FirstChar<Rest> extends "=" | "!"
96
92
  ? ExtractGroupNames<Rest>
@@ -125,7 +121,7 @@ export type RegExpNamedGroups<R extends RegExp> = R extends { readonly source: i
125
121
  export type __PreprocessEscapes<S extends string, Result extends string = ""> =
126
122
  S extends `\\\\${infer Rest}`
127
123
  ? __PreprocessEscapes<Rest, `${Result}__`>
128
- : S extends `\\${infer Next}${infer Rest}`
124
+ : S extends `\\${infer EscapedChar}${infer Rest}`
129
125
  ? __PreprocessEscapes<Rest, `${Result}_!`>
130
126
  : S extends `${infer Char}${infer Rest}`
131
127
  ? __PreprocessEscapes<Rest, `${Result}${Char}`>
@@ -197,11 +193,146 @@ export type ReplaceCallbackParams<
197
193
  input: string,
198
194
  groups: RegExpNamedGroups<R>
199
195
  ];
200
- export type ReplacerFunctionSignature<R extends unkown> =
196
+ export type ReplacerFunctionSignature<R extends unknown> =
201
197
  R extends (
202
198
  RegExp & { readonly source: infer T }
203
199
  ) ? R extends TypedRegExpBrand
204
- ? (...args: ReplaceCallbackParams<R, T>) => string
200
+ ? (...args: ReplaceCallbackParams<R/* , T */>) => string
205
201
  : (match: string, ...rest: any[]) => string
206
202
  : string;
203
+ /*! [2025/12/31 13:01:03]
204
+ * ## Support PCRE-style regex source (type-level)
205
+ *
206
+ * @remarks
207
+ * This module provides **type-level normalization** for "PCRE-ish" regex sources:
208
+ * you can write a readable multi-line regex with indentation and `# ...` comments,
209
+ * then derive a compact JavaScript `RegExp` source at compile time.
210
+ *
211
+ * Why type-level? Because it lets us:
212
+ * - keep the regex readable in source code (with comments and formatting)
213
+ * - ensure the normalized output is stable and verifiable (via type assertions)
214
+ *
215
+ * ## Supported rules
216
+ * - `#` starts a comment and consumes the rest of the **current line**
217
+ * - `\#` escapes `#` as a literal (comment is NOT started) and normalizes to `#`
218
+ * - whitespace characters are removed during normalization
219
+ * - newlines are normalized: `\r\n` and `\r` become `\n`
220
+ *
221
+ * ## Design note (ts(2589) survival)
222
+ * Naively scanning the entire string character-by-character (multiple passes)
223
+ * can hit TypeScript's instantiation depth limit (`ts(2589)`).
224
+ * This implementation is **line-oriented**:
225
+ * it strips each line, then concatenates results.
226
+ * That tends to scale much better for long, commented regex sources.
227
+ *
228
+ * ## Non-goals (for now)
229
+ * This is not a full PCRE parser. It intentionally focuses on comment + whitespace
230
+ * normalization for readability and DX.
231
+ */
232
+ export type Whitespace =
233
+ | "\u{9}"
234
+ | "\u{A}"
235
+ | "\u{B}"
236
+ | "\u{C}"
237
+ | "\u{D}"
238
+ | "\u{20}"
239
+ | "\u{85}"
240
+ | "\u{A0}"
241
+ | "\u{1680}"
242
+ | "\u{2000}"
243
+ | "\u{2001}"
244
+ | "\u{2002}"
245
+ | "\u{2003}"
246
+ | "\u{2004}"
247
+ | "\u{2005}"
248
+ | "\u{2006}"
249
+ | "\u{2007}"
250
+ | "\u{2008}"
251
+ | "\u{2009}"
252
+ | "\u{200A}"
253
+ | "\u{2028}"
254
+ | "\u{2029}"
255
+ | "\u{202F}"
256
+ | "\u{205F}"
257
+ | "\u{3000}"
258
+ | "\u{FEFF}";
259
+ /**
260
+ * Normalize newline sequences for line-based parsing.
261
+ *
262
+ * + Align **`\r\n`** and **`\r`** to **`\n`** (makes splitting easier)
263
+ *
264
+ * @template S - Input string literal.
265
+ * @returns A new string literal with `\r\n` and `\r` normalized to `\n`.
266
+ *
267
+ * @example
268
+ * type X = NormalizeNewlines<"a\r\nb\rc\n">; // "a\nb\nc\n"
269
+ */
270
+ type NormalizeNewlines<S extends string> =
271
+ S extends `${infer A}\r\n${infer B}` ? NormalizeNewlines<`${A}\n${B}`> :
272
+ S extends `${infer A}\r${infer B}` ? NormalizeNewlines<`${A}\n${B}`> :
273
+ S;
274
+ /**
275
+ * Strip and normalize a single line of a PCRE-style regex source.
276
+ *
277
+ * @remarks
278
+ * Rules:
279
+ * - `\#` is treated as a literal `#` (the backslash is dropped)
280
+ * - an unescaped `#` starts a line comment and truncates the rest
281
+ * - all whitespace characters are removed
282
+ * - other escapes like `\s`, `\w`, `\/`, `\*` are preserved as-is
283
+ *
284
+ * @template S - One line of source (no `\n`).
285
+ * @template Acc - Accumulator for the normalized output.
286
+ *
287
+ * @example
288
+ * type A = StripLine<" \\#\\w+ # comment ">; // "#\\w+"
289
+ * type B = StripLine<" abc # comment">; // "abc"
290
+ *
291
+ * @internal
292
+ *
293
+ * @todo
294
+ * - Handle `\\#` (a literal backslash + literal #) more strictly.
295
+ * - Do not treat `#` as a comment starter inside character classes (`[...]`).
296
+ */
297
+ type StripLine<S extends string, Acc extends string = ""> =
298
+ S extends `\\#${infer Rest}`
299
+ ? StripLine<Rest, `${Acc}#`>
300
+ : S extends `\\${infer C}${infer Rest}`
301
+ ? StripLine<Rest, `${Acc}\\${C}`>
302
+ : S extends `#${string}`
303
+ ? Acc
304
+ : S extends `${infer C}${infer Rest}`
305
+ ? C extends Whitespace
306
+ ? StripLine<Rest, Acc>
307
+ : StripLine<Rest, `${Acc}${C}`>
308
+ : Acc;
309
+ /**
310
+ * Convert a multi-line PCRE-style regex source into a compact JS `RegExp.source`.
311
+ *
312
+ * @remarks
313
+ * This type:
314
+ * 1) normalizes newlines, 2) splits by line, 3) applies {@link StripLine},
315
+ * then 4) concatenates the results.
316
+ *
317
+ * @template Input - A `const` string literal of the PCRE-ish source.
318
+ * @template Acc - Accumulator for the result.
319
+ * @template S - Internal normalized newline buffer (do not pass manually).
320
+ *
321
+ * @example
322
+ * const RE_SOURCE = `
323
+ * ^ # start
324
+ * (?:\\#\\w+) # keep literal "#"
325
+ * ` as const;
326
+ *
327
+ * type Src = PCREStyleToJsRegExpSourceByLine<typeof RE_SOURCE>;
328
+ * // "^(:?##\\w+)" ... (depending on your actual input)
329
+ */
330
+ export type PCREStyleToJsRegExpSourceByLine<
331
+ Input extends string,
332
+ Acc extends string = "",
333
+ S extends string = NormalizeNewlines<Input>,
334
+ > =
335
+ S extends `${infer Line}\n${infer Rest}`
336
+ ? PCREStyleToJsRegExpSourceByLine<Input, `${Acc}${StripLine<Line>}`, Rest>
337
+ : `${Acc}${StripLine<S>}`;
207
338
  export as namespace XRegex;
@@ -1,3 +1,3 @@
1
1
  {
2
- "version": "1.0.18"
2
+ "version": "1.0.20"
3
3
  }
package/tool-lib/cjbm.js CHANGED
@@ -13,21 +13,15 @@
13
13
  const fs = require("fs");
14
14
  const path = require("path");
15
15
  /**
16
- * Creates a typed RegExp object with literal types for the pattern and flags.
17
- * @template {string} P
18
- * @template {string} F
19
- * @param {P} pattern The regular expression pattern.
20
- * @param {F} [flags] The flags for the regular expression.
21
- * @returns {XRegex.TypedRegExp<P, F>} A typed RegExp object.
16
+ * DEVNOTE: Improved `RegExpConstructor` interface makes `createRegExp` unnecessary
17
+ * @date 2025/12/25 22:10:17
22
18
  */
23
- function createRegExp(pattern, flags) {
24
- return /** @type {XRegex.TypedRegExp<P, F>} */(new RegExp(pattern, flags));
25
- }
26
- const reImportExportDetection = createRegExp(
27
- `(?<!\\s*\\/\\/+.*?)(?:(?<!@)import|export)\\s*(?:(?<qcGlobal>["'])(?!https?:)(?=[.\\/]+)\
28
- (?:(?<globalName>(?:[.\\/]+)?[^'"]+?)(?:\\.(?<globalExt>(?:c|m)?jsx?))?)\\k<qcGlobal>|\\(\\s*(?<qcDynamic>["'])(?!https?:)(?=[.\\/]+)(?<dynamicName>(?:[.\\/]+)?[^'"]+?)\
29
- (?:\\.(?<dynamicExt>(?:c|m)?jsx?))?\\k<qcDynamic>\\s*\\)|(?:(?<clause>.+?|(?:\\w+\\s*,)?\\s*\\{[^}]+\\})\\s*from)\\s*(?<qcEsm>["'])(?!https?:)(?=[.\\/]+)\
30
- (?:(?<moduleName>(?:[.\\/]+)?[^'"]+?)(?:\\.(?<moduleExt>(?:c|m)?jsx?))?)\\k<qcEsm>)(?=\\s*;)?`, "g"
19
+ const reImportExportDetection = new RegExp(
20
+ `(?:(?:^\\s*?\\/\\*+?[\\s\\S]*?\\*\\/)|(?<!\\s*\\/\\/+.*?)(?:(?<!@)import|export)\\s*\
21
+ (?:(?<qcGlobal>["'])(?!https?:)(?=(?:\\.\\.?\\/)+)(?:(?<globalName>(?:(?:\\.\\.?\\/)+)?[^'"]+?)(?:\\.(?<globalExt>(?:c|m)?jsx?))?)\\k<qcGlobal>|\
22
+ \\(\\s*(?<qcDynamic>["'])(?!https?:)(?=(?:\\.\\.?\\/)+)(?<dynamicName>(?:(?:\\.\\.?\\/)+)?[^'"]+?)(?:\\.(?<dynamicExt>(?:c|m)?jsx?))?\\k<qcDynamic>\\s*\\)|\
23
+ (?:(?<clause>.+?|(?:\\w+\\s*,)?\\s*\\{[^}]+})\\s*from)\\s*(?<qcEsm>["'])(?!https?:)(?=(?:\\.\\.?\\/)+)(?:(?<moduleName>(?:(?:\\.\\.?\\/)+)?[^'"]+?)(?:\\.(?<moduleExt>(?:c|m)?jsx?))?)\\k<qcEsm>)\
24
+ (?=\\s*;)?)`, "gm"
31
25
  );
32
26
  /**
33
27
  * @typedef {XRegex.ReplacerFunctionSignature<typeof reImportExportDetection>} TImportExportDetectRegexReplacer
@@ -49,6 +43,7 @@ function getReplacer(ext) {
49
43
  return (
50
44
  match, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _offset, _input, groups
51
45
  ) => {
46
+ if (/\s*?\/\*/.test(match)) return match;
52
47
  const {
53
48
  clause,
54
49
  dynamicName,