js-dev-tool 1.0.21 → 1.1.2

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.
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Modifications Copyright 2025 jeffy-g
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,195 @@
1
+
2
+ # literate-regex
3
+
4
+ Write readable (literate) regex sources with `#` comments, and normalize them into a compact JavaScript `RegExp.source` **at the type level**.
5
+
6
+ This package is built for the real world where regexes are often:
7
+ - long,
8
+ - unreadable,
9
+ - and fragile.
10
+
11
+ With `literate-regex`, you can keep them readable (multi-line + comments) while still getting a normalized, single-line `RegExp.source` type that stays stable.
12
+
13
+ ---
14
+
15
+ ## ✨ Features
16
+
17
+ - **PCRE-ish style regex source**:
18
+ - multi-line formatting
19
+ - `# ...` line comments
20
+ - `\#` escape for literal `#`
21
+ - **Type-level normalization**:
22
+ - derive normalized JS `RegExp.source` as a string literal type
23
+ - **Optional global augmentation**:
24
+ - opt-in only (`import "literate-regex/global"`)
25
+ - Designed to reduce TypeScript instantiation pain:
26
+ - line-oriented normalization (helps avoid `ts(2589)` compared to naive full-string scanning)
27
+
28
+ ---
29
+
30
+ ## 📦 Install
31
+
32
+ ```bash
33
+ npm i literate-regex
34
+ # or
35
+ pnpm add literate-regex
36
+ # or
37
+ yarn add literate-regex
38
+ ```
39
+
40
+ ---
41
+
42
+ ## 🚀 Quick Start
43
+
44
+ ```ts
45
+ import { PCREStyleToJsRegExpSource } from "literate-regex";
46
+
47
+ // Only those who want to expand globally
48
+ import "literate-regex/global";
49
+ ```
50
+
51
+ ---
52
+
53
+ ## 🧠 Type-level normalization
54
+
55
+ ### 1) Write a readable PCRE-style source
56
+
57
+ * `#` starts a line comment (unless escaped)
58
+ * `\#` is kept as a literal `#`
59
+ * whitespace characters are stripped during normalization
60
+
61
+ ```ts
62
+ import type { PCREStyleToJsRegExpSource } from "./literate-regex";
63
+
64
+ // sample 1
65
+ const RE_SOURCE = `
66
+ ^ # start
67
+ (?:\\#\\w+) # literal "#"
68
+ \\s+ # whitespace
69
+ ` as const;
70
+
71
+ // type JsSource = "^(?:#\\w+)\\s+"
72
+ type JsSource = PCREStyleToJsRegExpSource<typeof RE_SOURCE>;
73
+ ```
74
+
75
+ > Tip: You must use `as const` to preserve the source as a string literal type.
76
+
77
+ ---
78
+
79
+ ## 🔧 Runtime normalization (optional)
80
+
81
+ `PCREStyleToJsRegExpSource<...>` is purely type-level.
82
+ If you also normalize at runtime, mirror the same rules:
83
+
84
+ ```ts
85
+ import { normalizePCREStyleSource } from "literate-regex";
86
+ // import type { PCREStyleToJsRegExpSource } from "literate-regex";
87
+
88
+ // sample 2
89
+ const src = `
90
+ ^ # start
91
+ \\#\\w+ # literal
92
+ ` as const;
93
+
94
+ // '^#\\w+'
95
+ // const normalized: "^#\\w+"
96
+ const normalized = normalizePCREStyleSource(src);
97
+ ```
98
+
99
+ ## helper function compilePCREStyleRegExpLiteral
100
+
101
+
102
+ ```ts
103
+ import {
104
+ TypedRegExp,
105
+ normalizePCREStyleSource,
106
+ compilePCREStyleRegExpLiteral,
107
+ extractJsRegexPartsFromPCREStyleRegExpLiteral
108
+ } from "literate-regex";
109
+ import type {
110
+ RegExpLiteralParts,
111
+ PCREStyleToJsRegExpSource,
112
+ RegExpExecArrayFixedPretty,
113
+ ReplacerFunctionSignature,
114
+ } from "literate-regex";
115
+
116
+ //
117
+ // sample of compilePCREStyleRegExpLiteral
118
+ //
119
+ const pcreStyledRegex = `/
120
+ (\\(\\?\#[\\s\\S]*?(?<!\\\\)\\)(?=\\s*$|.)) # multi line comment
121
+ |
122
+ (?:^(?:\\s+|))?(?<![\\\\])(\\#(?:\\s|[\\s\\S])*?$) # single line comment
123
+ |
124
+ (?<regexFragment>
125
+ (?:^\\s+)?(?:[^\\s]+)
126
+ )+ # regex flagment
127
+ |
128
+ ([\\r|\\r\\n|\\n]+|[\\x20\\t]+(?=$)?) # whitespaces
129
+ /gm`;
130
+
131
+ const jsRegex = compilePCREStyleRegExpLiteral(pcreStyledRegex);
132
+
133
+ type TPcreStyledRegex = typeof pcreStyledRegex;
134
+ type TJsRegexSource = PCREStyleToJsRegExpSource<TPcreStyledRegex>;
135
+ type TJsRegexLiteralParts = RegExpLiteralParts<TJsRegexSource>;
136
+
137
+ type TJsRegexExecArray = RegExpExecArrayFixedPretty<
138
+ TypedRegExp<TJsRegexLiteralParts["pattern"]>
139
+ >;
140
+ type TJsRegexStringReplacer = ReplacerFunctionSignature<
141
+ TypedRegExp<TJsRegexLiteralParts["pattern"]>
142
+ >;
143
+ let m = jsRegex.exec(pcreStyledRegex);
144
+ type Test0 = TJsRegexExecArray extends typeof m ? true : false;
145
+ type Test1 = typeof m extends TJsRegexExecArray ? true : false;
146
+
147
+ const replacer: TJsRegexStringReplacer = (...args) => "";
148
+ pcreStyledRegex.replace(jsRegex, replacer);
149
+ pcreStyledRegex.replace(jsRegex, "");
150
+ ```
151
+
152
+ ---
153
+
154
+ ## 🌍 Global augmentation (opt-in)
155
+
156
+ This package provides an optional global augmentation entry:
157
+
158
+ ```ts
159
+ import "literate-regex/global";
160
+ ```
161
+
162
+ This is intentionally **opt-in** to avoid unexpected type pollution across projects.
163
+
164
+ ---
165
+
166
+ ## ⚠️ Notes & limitations
167
+
168
+ * This is **not a full PCRE parser**. It focuses on:
169
+
170
+ * line comments (`# ...`)
171
+ * escaping `\#`
172
+ * whitespace stripping
173
+ * Very large type-level inputs may still hit TS limits depending on your environment.
174
+ If that happens, split your regex source into smaller pieces.
175
+
176
+ ---
177
+
178
+ ## 📚 References
179
+
180
+ This library’s whitespace set is based on the ECMAScript definition used by RegExp `\s`
181
+ (WhiteSpace ∪ LineTerminator).
182
+
183
+ - ECMA-262: White Space (Table 33)
184
+ https://tc39.es/ecma262/#sec-white-space
185
+ - ECMA-262: Line Terminators (Table 34)
186
+ https://tc39.es/ecma262/#sec-line-terminators
187
+ - MDN: RegExp character classes (`\s` equivalence)
188
+ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions/Character_classes
189
+
190
+ ---
191
+
192
+ ## 📜 License
193
+
194
+ Released under the `Apache-2.0` License.
195
+ See [LICENSE](./LICENSE) for details.
@@ -0,0 +1,16 @@
1
+ /*!
2
+ // Copyright jeffy-g 2025
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ */
16
+ export * from "./util";
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ /*!
3
+ // Copyright jeffy-g 2025
4
+ //
5
+ // Licensed under the Apache License, Version 2.0 (the "License");
6
+ // you may not use this file except in compliance with the License.
7
+ // You may obtain a copy of the License at
8
+ //
9
+ // http://www.apache.org/licenses/LICENSE-2.0
10
+ //
11
+ // Unless required by applicable law or agreed to in writing, software
12
+ // distributed under the License is distributed on an "AS IS" BASIS,
13
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ // See the License for the specific language governing permissions and
15
+ // limitations under the License.
16
+ */
17
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
18
+ if (k2 === undefined) k2 = k;
19
+ var desc = Object.getOwnPropertyDescriptor(m, k);
20
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
21
+ desc = { enumerable: true, get: function() { return m[k]; } };
22
+ }
23
+ Object.defineProperty(o, k2, desc);
24
+ }) : (function(o, m, k, k2) {
25
+ if (k2 === undefined) k2 = k;
26
+ o[k2] = m[k];
27
+ }));
28
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
29
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
30
+ };
31
+ Object.defineProperty(exports, "__esModule", { value: true });
32
+ __exportStar(require("./util"), exports);
@@ -0,0 +1,22 @@
1
+ /*!
2
+ // Copyright jeffy-g 2025
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ */
16
+ import type { TypedRegExp, PCREStyleRegExpFlags, PCREStyleRegExpParts, PCREStyleRegExpPattern, PCREStyleToJsRegExpSource } from "../";
17
+ export declare const normalizePCREStyleSource: <const S extends string>(src: S) => PCREStyleToJsRegExpSource<S>;
18
+ export declare const extractJsRegexPartsFromPCREStyleRegExpLiteral: <const S extends string, const TRegexParts extends PCREStyleRegExpParts<S>>(src: S) => {
19
+ pattern: TRegexParts["pattern"];
20
+ flags: TRegexParts["flags"];
21
+ };
22
+ export declare const compilePCREStyleRegExpLiteral: <const S extends string>(src: S) => TypedRegExp<PCREStyleRegExpPattern<S>, PCREStyleRegExpFlags<S>>;
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ /*!
3
+ // Copyright jeffy-g 2025
4
+ //
5
+ // Licensed under the Apache License, Version 2.0 (the "License");
6
+ // you may not use this file except in compliance with the License.
7
+ // You may obtain a copy of the License at
8
+ //
9
+ // http://www.apache.org/licenses/LICENSE-2.0
10
+ //
11
+ // Unless required by applicable law or agreed to in writing, software
12
+ // distributed under the License is distributed on an "AS IS" BASIS,
13
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ // See the License for the specific language governing permissions and
15
+ // limitations under the License.
16
+ */
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.compilePCREStyleRegExpLiteral = exports.extractJsRegexPartsFromPCREStyleRegExpLiteral = exports.normalizePCREStyleSource = void 0;
19
+ const NORMALIZE_PCRE_STYLE_REGEX = /(?<!\\)\#\s*.*$|\s+/gm;
20
+ const normalizePCREStyleSource = (src) => {
21
+ return src
22
+ .replace(/\\#/g, "\x00#")
23
+ .replace(NORMALIZE_PCRE_STYLE_REGEX, "")
24
+ .replace(/\x00#/g, "#");
25
+ };
26
+ exports.normalizePCREStyleSource = normalizePCREStyleSource;
27
+ const extractJsRegexPartsFromPCREStyleRegExpLiteral = (src) => {
28
+ const normalized = (0, exports.normalizePCREStyleSource)(src);
29
+ if (normalized[0] !== "/")
30
+ throw new Error("Expected regex literal to start with '/'");
31
+ const lastSlash = normalized.lastIndexOf("/");
32
+ if (lastSlash <= 0)
33
+ throw new Error("Invalid regex literal: missing trailing '/'");
34
+ const pattern = normalized.slice(1, lastSlash);
35
+ const flags = normalized.slice(lastSlash + 1);
36
+ return { pattern, flags };
37
+ };
38
+ exports.extractJsRegexPartsFromPCREStyleRegExpLiteral = extractJsRegexPartsFromPCREStyleRegExpLiteral;
39
+ const compilePCREStyleRegExpLiteral = (src) => {
40
+ const normalized = (0, exports.normalizePCREStyleSource)(src);
41
+ if (normalized[0] !== "/")
42
+ throw new Error("Expected regex literal to start with '/'");
43
+ const lastSlash = normalized.lastIndexOf("/");
44
+ if (lastSlash <= 0)
45
+ throw new Error("Invalid regex literal: missing trailing '/'");
46
+ const pattern = normalized.slice(1, lastSlash);
47
+ const flags = normalized.slice(lastSlash + 1);
48
+ return new RegExp(pattern, flags);
49
+ };
50
+ exports.compilePCREStyleRegExpLiteral = compilePCREStyleRegExpLiteral;
@@ -0,0 +1,16 @@
1
+ /*!
2
+ // Copyright jeffy-g 2025
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ */
16
+ export * from "./util";
@@ -0,0 +1,16 @@
1
+ /*!
2
+ // Copyright jeffy-g 2025
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ */
16
+ export * from "./util.mjs";
@@ -0,0 +1,22 @@
1
+ /*!
2
+ // Copyright jeffy-g 2025
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ */
16
+ import type { TypedRegExp, PCREStyleRegExpFlags, PCREStyleRegExpParts, PCREStyleRegExpPattern, PCREStyleToJsRegExpSource } from "../";
17
+ export declare const normalizePCREStyleSource: <const S extends string>(src: S) => PCREStyleToJsRegExpSource<S>;
18
+ export declare const extractJsRegexPartsFromPCREStyleRegExpLiteral: <const S extends string, const TRegexParts extends PCREStyleRegExpParts<S>>(src: S) => {
19
+ pattern: TRegexParts["pattern"];
20
+ flags: TRegexParts["flags"];
21
+ };
22
+ export declare const compilePCREStyleRegExpLiteral: <const S extends string>(src: S) => TypedRegExp<PCREStyleRegExpPattern<S>, PCREStyleRegExpFlags<S>>;
@@ -0,0 +1,44 @@
1
+ /*!
2
+ // Copyright jeffy-g 2025
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ */
16
+ const NORMALIZE_PCRE_STYLE_REGEX = /(?<!\\)\#\s*.*$|\s+/gm;
17
+ export const normalizePCREStyleSource = (src) => {
18
+ return src
19
+ .replace(/\\#/g, "\x00#")
20
+ .replace(NORMALIZE_PCRE_STYLE_REGEX, "")
21
+ .replace(/\x00#/g, "#");
22
+ };
23
+ export const extractJsRegexPartsFromPCREStyleRegExpLiteral = (src) => {
24
+ const normalized = normalizePCREStyleSource(src);
25
+ if (normalized[0] !== "/")
26
+ throw new Error("Expected regex literal to start with '/'");
27
+ const lastSlash = normalized.lastIndexOf("/");
28
+ if (lastSlash <= 0)
29
+ throw new Error("Invalid regex literal: missing trailing '/'");
30
+ const pattern = normalized.slice(1, lastSlash);
31
+ const flags = normalized.slice(lastSlash + 1);
32
+ return { pattern, flags };
33
+ };
34
+ export const compilePCREStyleRegExpLiteral = (src) => {
35
+ const normalized = normalizePCREStyleSource(src);
36
+ if (normalized[0] !== "/")
37
+ throw new Error("Expected regex literal to start with '/'");
38
+ const lastSlash = normalized.lastIndexOf("/");
39
+ if (lastSlash <= 0)
40
+ throw new Error("Invalid regex literal: missing trailing '/'");
41
+ const pattern = normalized.slice(1, lastSlash);
42
+ const flags = normalized.slice(lastSlash + 1);
43
+ return new RegExp(pattern, flags);
44
+ };
@@ -0,0 +1,67 @@
1
+ /*!
2
+ // Copyright jeffy-g 2025
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ */
16
+ /**
17
+ * @file literate-regex/global.d.ts
18
+ */
19
+ import type {
20
+ TypedRegExp,
21
+ ReplacerFunctionSignature,
22
+ RegExpExecArrayFixedPretty,
23
+ } from "./index.d.ts";
24
+ declare global {
25
+ interface RegExp {
26
+ /**
27
+ * Executes a search on a string using a regular expression pattern, and returns an array containing the results of that search.
28
+ * @template {string} P
29
+ * @template {string} F
30
+ * @param {TypedRegExp<P, F>} this The regular expression object.
31
+ * @param {string} str The String object or string literal on which to perform the search.
32
+ * @returns {RegExpExecArrayFixedPretty< this > | null} An array of results or null if no match is found.
33
+ */
34
+ exec<const P extends string, const F extends string = "">(this: TypedRegExp<P, F>, str: string): RegExpExecArrayFixedPretty< this > | null;
35
+ }
36
+ interface String {
37
+ /**
38
+ * Replaces occurrences of a pattern in the string with a specified replacement.
39
+ * @template {RegExp | string} SV
40
+ * @param searchValue A regular expression object.
41
+ * @param replaceValue A string or a function to create the new substring.
42
+ */
43
+ replace<SV extends RegExp | string>(this: string, searchValue: SV, replaceValue: ReplacerFunctionSignature<SV>): string;
44
+ }
45
+ interface RegExpConstructor {
46
+ /**
47
+ * Creates a typed RegExp object with literal types for the pattern and flags.
48
+ * @template {string} P
49
+ * @template {string} F
50
+ * @param {P} pattern The regular expression pattern.
51
+ * @param {F} [flags] The flags for the regular expression.
52
+ * @returns {TypedRegExp<P, F>} A typed RegExp object.
53
+ */
54
+ new <const P extends string, const F extends string = "">(pattern: P, flags?: F): TypedRegExp<P, F>;
55
+ /**
56
+ * Creates a typed RegExp object with literal types for the pattern and flags.
57
+ * @template {string} P
58
+ * @template {string} F
59
+ * @param {P} pattern The regular expression pattern.
60
+ * @param {F} [flags] The flags for the regular expression.
61
+ * @returns {TypedRegExp<P, F>} A typed RegExp object.
62
+ */
63
+ <const P extends string, const F extends string = "">(pattern: P, flags?: F): TypedRegExp<P, F>;
64
+ readonly prototype: RegExp;
65
+ }
66
+ }
67
+ export {};
@@ -1,58 +1,24 @@
1
1
  /*!
2
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3
- // Copyright (C) 2025 jeffy-g <hirotom1107@gmail.com>
4
- // Released under the MIT license
5
- // https://opensource.org/licenses/mit-license.php
6
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2
+ // Copyright jeffy-g 2025
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
7
15
  */
8
16
  /**
9
- * @file js-dev-scripts/regex.d.ts
17
+ * @file literate-regex/index.d.ts
10
18
  * @desc It is recommended to add `"skipLibCheck": true` to your tsconfig.
11
- * @todo publish as "annotate-regex"
19
+ * @todo publish as "literate-regex"
20
+ * @todo implement runtime utility function for `/<source>/<flags>`
12
21
  */
13
- declare global {
14
- interface RegExp {
15
- /**
16
- * Executes a search on a string using a regular expression pattern, and returns an array containing the results of that search.
17
- * @template {string} P
18
- * @template {string} F
19
- * @param {TypedRegExp<P, F>} this The regular expression object.
20
- * @param {string} str The String object or string literal on which to perform the search.
21
- * @returns {RegExpExecArrayFixed< this > | null} An array of results or null if no match is found.
22
- */
23
- exec<const P extends string, const F extends string = "">(this: TypedRegExp<P, F>, str: string): RegExpExecArrayFixed< this > | null;
24
- }
25
- interface String {
26
- /**
27
- * Replaces occurrences of a pattern in the string with a specified replacement.
28
- * @template {RegExp | string} SV
29
- * @param searchValue A regular expression object.
30
- * @param replaceValue A string or a function to create the new substring.
31
- */
32
- replace<SV extends RegExp | string>(this: string, searchValue: SV, replaceValue: ReplacerFunctionSignature<SV>): string;
33
- }
34
- interface RegExpConstructor {
35
- /**
36
- * Creates a typed RegExp object with literal types for the pattern and flags.
37
- * @template {string} P
38
- * @template {string} F
39
- * @param {P} pattern The regular expression pattern.
40
- * @param {F} [flags] The flags for the regular expression.
41
- * @returns {RegExp & { readonly source: P; readonly flags: F }} A typed RegExp object.
42
- */
43
- new <const P extends string, const F extends string = "">(pattern: P, flags?: F): TypedRegExp<P, F>;
44
- /**
45
- * Creates a typed RegExp object with literal types for the pattern and flags.
46
- * @template {string} P
47
- * @template {string} F
48
- * @param {P} pattern The regular expression pattern.
49
- * @param {F} [flags] The flags for the regular expression.
50
- * @returns {RegExp & { readonly source: P; readonly flags: F }} A typed RegExp object.
51
- */
52
- <const P extends string, const F extends string = "">(pattern: P, flags?: F): TypedRegExp<P, F>;
53
- readonly "prototype": RegExp;
54
- }
55
- }
56
22
  /*!
57
23
  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
58
24
  // Basics
@@ -161,7 +127,36 @@ export type RegExpExecArrayFixed<
161
127
  S extends RegExpSource<R> = RegExpSource<R>,
162
128
  GroupCount extends number = CountCaptureGroups<S>,
163
129
  NamedGroups = RegExpNamedGroups<R>
164
- > = [match: string, ...BuildCaptureTuple<GroupCount>] & {
130
+ > = [match: string, ...BuildCaptureTuple<GroupCount, string | undefined>] & {
131
+ groups: IsEmptyRecord<NamedGroups> extends true ? undefined : NamedGroups;
132
+ index: number;
133
+ input: string;
134
+ };
135
+ type CaptureOptTuple<N extends number, T = string | undefined> =
136
+ N extends 0 ? [] :
137
+ N extends 1 ? [$1: T] :
138
+ N extends 2 ? [$1: T, $2: T] :
139
+ N extends 3 ? [$1: T, $2: T, $3: T] :
140
+ N extends 4 ? [$1: T, $2: T, $3: T, $4: T] :
141
+ N extends 5 ? [$1: T, $2: T, $3: T, $4: T, $5: T] :
142
+ N extends 6 ? [$1: T, $2: T, $3: T, $4: T, $5: T, $6: T] :
143
+ N extends 7 ? [$1: T, $2: T, $3: T, $4: T, $5: T, $6: T, $7: T] :
144
+ N extends 8 ? [$1: T, $2: T, $3: T, $4: T, $5: T, $6: T, $7: T, $8: T] :
145
+ N extends 9 ? [$1: T, $2: T, $3: T, $4: T, $5: T, $6: T, $7: T, $8: T, $9: T] :
146
+ N extends 10 ? [$1: T, $2: T, $3: T, $4: T, $5: T, $6: T, $7: T, $8: T, $9: T, $10: T] :
147
+ N extends 11 ? [$1: T, $2: T, $3: T, $4: T, $5: T, $6: T, $7: T, $8: T, $9: T, $10: T, $11: T] :
148
+ N extends 12 ? [$1: T, $2: T, $3: T, $4: T, $5: T, $6: T, $7: T, $8: T, $9: T, $10: T, $11: T, $12: T] :
149
+ N extends 13 ? [$1: T, $2: T, $3: T, $4: T, $5: T, $6: T, $7: T, $8: T, $9: T, $10: T, $11: T, $12: T, $13: T] :
150
+ N extends 14 ? [$1: T, $2: T, $3: T, $4: T, $5: T, $6: T, $7: T, $8: T, $9: T, $10: T, $11: T, $12: T, $13: T, $14: T] :
151
+ N extends 15 ? [$1: T, $2: T, $3: T, $4: T, $5: T, $6: T, $7: T, $8: T, $9: T, $10: T, $11: T, $12: T, $13: T, $14: T, $15: T] :
152
+ N extends 16 ? [$1: T, $2: T, $3: T, $4: T, $5: T, $6: T, $7: T, $8: T, $9: T, $10: T, $11: T, $12: T, $13: T, $14: T, $15: T, $16: T] :
153
+ BuildCaptureTuple<N, T>;
154
+ export type RegExpExecArrayFixedPretty<
155
+ R extends RegExp,
156
+ S extends RegExpSource<R> = RegExpSource<R>,
157
+ GroupCount extends number = CountCaptureGroups<S>,
158
+ NamedGroups = RegExpNamedGroups<R>
159
+ > = [match: string, ...CaptureOptTuple<GroupCount>] & {
165
160
  groups: IsEmptyRecord<NamedGroups> extends true ? undefined : NamedGroups;
166
161
  index: number;
167
162
  input: string;
@@ -199,12 +194,12 @@ export type ReplaceCallbackParams<
199
194
  NamedGroups = RegExpNamedGroups<R>
200
195
  > = IsEmptyRecord<NamedGroups> extends true ? [
201
196
  match: string,
202
- ...captures: BuildCaptureTuple<GroupCount>,
197
+ ...captures: CaptureOptTuple<GroupCount>,
203
198
  offset: number,
204
199
  input: string,
205
200
  ] : [
206
201
  match: string,
207
- ...captures: BuildCaptureTuple<GroupCount>,
202
+ ...captures: CaptureOptTuple<GroupCount>,
208
203
  offset: number,
209
204
  input: string,
210
205
  groups: NamedGroups
@@ -245,33 +240,36 @@ export type ReplacerFunctionSignature<R extends unknown> =
245
240
  * This is not a full PCRE parser. It intentionally focuses on comment + whitespace
246
241
  * normalization for readability and DX.
247
242
  */
248
- export type Whitespace =
249
- | "\u{9}"
250
- | "\u{A}"
251
- | "\u{B}"
252
- | "\u{C}"
253
- | "\u{D}"
254
- | "\u{20}"
255
- | "\u{85}"
256
- | "\u{A0}"
257
- | "\u{1680}"
258
- | "\u{2000}"
259
- | "\u{2001}"
260
- | "\u{2002}"
261
- | "\u{2003}"
262
- | "\u{2004}"
263
- | "\u{2005}"
264
- | "\u{2006}"
265
- | "\u{2007}"
266
- | "\u{2008}"
267
- | "\u{2009}"
268
- | "\u{200A}"
269
- | "\u{2028}"
270
- | "\u{2029}"
271
- | "\u{202F}"
272
- | "\u{205F}"
273
- | "\u{3000}"
274
- | "\u{FEFF}";
243
+ /**
244
+ * Union of whitespace characters used by this library.
245
+ *
246
+ * + A set aligned with ECMAScript RegExp **`\s`** (WhiteSpace ∪ LineTerminator)
247
+ *
248
+ * The union is used to normalize "PCRE-style" regex sources at the type level.
249
+ */
250
+ export type TWhiteSpace =
251
+ | "\x09"
252
+ | "\x0A"
253
+ | "\x0B"
254
+ | "\x0C"
255
+ | "\x0D"
256
+ | "\x20"
257
+ | "\u00A0"
258
+ | "\u1680"
259
+ | "\u2000"
260
+ | "\u2001"
261
+ | "\u2002"
262
+ | "\u2003"
263
+ | "\u2004"
264
+ | "\u2005"
265
+ | "\u2006"
266
+ | "\u2007"
267
+ | "\u2008"
268
+ | "\u2009"
269
+ | "\u200A"
270
+ | "\u2028" | "\u2029"
271
+ | "\u202F" | "\u205F" | "\u3000"
272
+ | "\uFEFF";
275
273
  /**
276
274
  * Normalize newline sequences for line-based parsing.
277
275
  *
@@ -318,7 +316,7 @@ type StripLine<S extends string, Acc extends string = ""> =
318
316
  : S extends `#${string}`
319
317
  ? Acc
320
318
  : S extends `${infer C}${infer Rest}`
321
- ? C extends Whitespace
319
+ ? C extends TWhiteSpace
322
320
  ? StripLine<Rest, Acc>
323
321
  : StripLine<Rest, `${Acc}${C}`>
324
322
  : Acc;
@@ -351,4 +349,37 @@ export type PCREStyleToJsRegExpSource<
351
349
  S extends `${infer Line}\n${infer Rest}`
352
350
  ? PCREStyleToJsRegExpSource<Input, `${Acc}${StripLine<Line>}`, Rest>
353
351
  : `${Acc}${StripLine<S>}`;
352
+ /*!
353
+ // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
354
+ // Parse PCRE Style Regex Literal
355
+ // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
356
+ */
357
+ type AllowedRegExpFlag = "d" | "g" | "i" | "m" | "s" | "u" | "v" | "y";
358
+ type StripFlagsFromEnd<S extends string, Acc extends string = ""> =
359
+ S extends `${infer Rest}y` ? StripFlagsFromEnd<Rest, `y${Acc}`> :
360
+ S extends `${infer Rest}v` ? StripFlagsFromEnd<Rest, `v${Acc}`> :
361
+ S extends `${infer Rest}u` ? StripFlagsFromEnd<Rest, `u${Acc}`> :
362
+ S extends `${infer Rest}s` ? StripFlagsFromEnd<Rest, `s${Acc}`> :
363
+ S extends `${infer Rest}m` ? StripFlagsFromEnd<Rest, `m${Acc}`> :
364
+ S extends `${infer Rest}i` ? StripFlagsFromEnd<Rest, `i${Acc}`> :
365
+ S extends `${infer Rest}g` ? StripFlagsFromEnd<Rest, `g${Acc}`> :
366
+ S extends `${infer Rest}d` ? StripFlagsFromEnd<Rest, `d${Acc}`> :
367
+ { body: S; flags: Acc };
368
+ export type RegExpLiteralParts<L extends string> =
369
+ StripFlagsFromEnd<L> extends { body: infer B extends string; flags: infer F extends string }
370
+ ? B extends `/${infer AfterStart}`
371
+ ? AfterStart extends `${infer Pattern}/`
372
+ ? { pattern: Pattern; flags: F }
373
+ : never
374
+ : never
375
+ : never;
376
+ export type PCREStyleRegExpParts<S extends string> = RegExpLiteralParts<PCREStyleToJsRegExpSource<S>>;
377
+ export type PCREStyleRegExpPattern<S extends string> = PCREStyleRegExpParts<S>["pattern"];
378
+ export type PCREStyleRegExpFlags<S extends string> = PCREStyleRegExpParts<S>["flags"];
379
+ export declare const normalizePCREStyleSource: <S extends string>(src: S) => PCREStyleToJsRegExpSource<S>;
380
+ export declare const extractJsRegexPartsFromPCREStyleRegExpLiteral: <const S extends string>(src: S) => PCREStyleRegExpParts<S>;
381
+ export declare const compilePCREStyleRegExpLiteral: <const S extends string>(src: S) => TypedRegExp<
382
+ PCREStyleRegExpPattern<S>,
383
+ PCREStyleRegExpFlags<S>
384
+ >;
354
385
  export as namespace XRegex;
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "literate-regex",
3
+ "version": "0.1.0",
4
+ "description": "Write readable (literate) regex sources with # comments, then normalize them to JS RegExp.source at the type level.",
5
+ "license": "Apache-2.0",
6
+ "sideEffects": false,
7
+ "main": "./dist/cjs/index.js",
8
+ "module": "./dist/esm/index.mjs",
9
+ "types": "./dist/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/esm/index.mjs",
14
+ "require": "./dist/cjs/index.js"
15
+ },
16
+ "./global": {
17
+ "types": "./dist/global.d.ts"
18
+ }
19
+ },
20
+ "scripts": {
21
+ "clean": "rimraf -g ./dist/*",
22
+ "build": "tsc & tsc -p ./tsconfig-esm.json",
23
+ "postbuild": "cpx ./*.d.ts dist -v & yarn run:extras && jstool -cmd rmc -rmc4ts -basePath ./dist -test \"/\\.(m?js|d\\.m?ts)$/\"",
24
+ "run:extras": "jstool -cmd cjbm -root ./dist/esm -ext mjs && mv ./dist/esm/util.d.ts ./dist/esm/util.d.mts"
25
+ },
26
+ "files": [
27
+ "README.md",
28
+ "LICENSE",
29
+ "dist"
30
+ ],
31
+ "keywords": [
32
+ "regex",
33
+ "regexp",
34
+ "literate",
35
+ "types",
36
+ "typescript",
37
+ "pcre",
38
+ "jsdoc"
39
+ ]
40
+ }
package/package.json CHANGED
@@ -1,11 +1,46 @@
1
1
  {
2
2
  "name": "js-dev-tool",
3
- "version": "1.0.21",
3
+ "version": "1.1.2",
4
4
  "bin": {
5
5
  "jstool": "tools.js"
6
6
  },
7
7
  "types": "./index.d.ts",
8
8
  "main": "./index.js",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./index.d.ts",
12
+ "import": "./index.js",
13
+ "require": "./index.js"
14
+ },
15
+ "./utils": {
16
+ "types": "./utils.d.ts",
17
+ "import": "./utils.js",
18
+ "require": "./utils.js"
19
+ },
20
+ "./common": {
21
+ "types": "./common/index.d.ts",
22
+ "import": "./common/index.js",
23
+ "require": "./common/index.js"
24
+ },
25
+ "./extras/*": {
26
+ "types": "./extras/*.d.ts",
27
+ "import": "./extras/*.js",
28
+ "require": "./extras/*.js"
29
+ },
30
+ "./progress/*": {
31
+ "types": "./progress/*.d.ts",
32
+ "import": "./progress/*.js",
33
+ "require": "./progress/*.js"
34
+ },
35
+ "./literate-regex": {
36
+ "types": "./literate-regex/dist/index.d.ts",
37
+ "import": "./literate-regex/dist/esm/index.mjs",
38
+ "require": "./literate-regex/dist/cjs/index.js"
39
+ },
40
+ "./literate-regex/global": {
41
+ "types": "./literate-regex/dist/global.d.ts"
42
+ }
43
+ },
9
44
  "repository": {
10
45
  "type": "git",
11
46
  "url": "git+ssh://git@github.com/jeffy-g/js-dev-scripts.git"
@@ -22,24 +57,31 @@
22
57
  "version"
23
58
  ],
24
59
  "files": [
60
+ "*.d.ts",
25
61
  "common",
26
62
  "extras",
27
63
  "lib",
28
64
  "progress",
29
65
  "scripts",
66
+ "literate-regex/package.json",
67
+ "literate-regex/README.md",
68
+ "literate-regex/LICENSE",
69
+ "literate-regex/dist",
30
70
  "tool-lib",
31
- "index.js",
32
- "tools.js",
33
- "utils.js",
34
- "tsconfig.json",
71
+ "./index.js",
72
+ "./tools.js",
73
+ "./utils.js",
74
+ "./tsconfig.json",
35
75
  "!tool-lib/cjbm-*",
36
- "!scripts/unzip.ts",
76
+ "!unzip.ts",
37
77
  "!scripts/bin",
78
+ "!publish-version.json",
79
+ "!literate-regex/*.d.ts",
80
+ "!normalize-commit-msg.mjs",
38
81
  "!regex-test.ts",
39
82
  "!extras/npm",
40
83
  "!/**/*.png",
41
- "!tool.sh",
42
- "*.d.ts"
84
+ "!tool.sh"
43
85
  ],
44
86
  "peerDependencies": {
45
87
  "archiver": "^4.0.1",
package/tool-lib/cjbm.js CHANGED
@@ -12,17 +12,52 @@
12
12
  */
13
13
  const fs = require("fs");
14
14
  const path = require("path");
15
+ const {
16
+ compilePCREStyleRegExpLiteral,
17
+ } = require("../literate-regex");
18
+ const RE_TEXT = `/
19
+ # DEVNOTE: [.\/] において、js では "/" に escape は必要ないが、pcre の場合 pattern error になる都合上 escape している
20
+ # NOTE: PCRE(regex101.com) での検証時は pattern error 回避のため line comment assertion を (?<!\/\/|\/\/\s)(?:(?<!@)import|export) とすること
21
+ # line comment の import("...") を正しく除外するには: (?<!\s*\/\/+.*?)(?:(?<!@)import|export)
22
+ # NOTE: multiline comment の import statement 誤検出を回避するため、multiline comment の検出を行うようにした. (match count 45, 内 5 は multiline comment[non capture group])
23
+ # regex101.com では、line comment assertion (?<!\/\/|\/\/\s) により、48 (3 つは意図しない検出)
24
+ (?:
25
+ (?:^\\s*?\\/\\*+?[\\s\\S]*?\\*\\/)|
26
+ (?<!\\s*\\/\\/+.*?)(?:(?<!@)import|export)
27
+ \\s*
28
+ (?:
29
+ # 2025/12/22 14:57:44 - support quote character detection ["']
30
+ # global import, import "./"; import './global.mjs';
31
+ (?<qcGlobal>["'])(?!https?:)(?=(?:\\.\\.?\\/)+)(?:
32
+ (?<globalName>(?:(?:\\.\\.?\\/)+)?[^'"]+?)(?:\\.(?<globalExt>(?:c|m)?jsx?))?
33
+ )\\k<qcGlobal>|
34
+ # dynamic import, import("...") import('...')
35
+ \\(\\s*
36
+ (?<qcDynamic>["'])(?!https?:)(?=(?:\\.\\.?\\/)+)(?<dynamicName>(?:(?:\\.\\.?\\/)+)?[^'"]+?)(?:\\.(?<dynamicExt>(?:c|m)?jsx?))?\\k<qcDynamic>
37
+ \\s*\\)|
38
+ # esm import/export
39
+ (?:(?<clause>.+?|(?:\\w+\\s*,)?\\s*\\{[^}]+})\\s*from)\\s*
40
+ (?<qcEsm>["'])(?!https?:)(?=(?:\\.\\.?\\/)+)
41
+ (?:
42
+ (?<moduleName>(?:(?:\\.\\.?\\/)+)?[^'"]+?)(?:\\.(?<moduleExt>(?:c|m)?jsx?))?
43
+ )\\k<qcEsm>
44
+ )
45
+ (?=\\s*;)?
46
+ )/gm
47
+ `;
15
48
  /**
16
- * DEVNOTE: Improved `RegExpConstructor` interface makes `createRegExp` unnecessary
17
- * @date 2025/12/25 22:10:17
49
+ * @typedef {XRegex.PCREStyleToJsRegExpSource<typeof RE_TEXT>} TCjbmRegexLiteral remove line comment and whitespaces from `RE_TEXT` (type-level)
50
+ * @typedef {XRegex.RegExpLiteralParts<TCjbmRegexLiteral>} TCjbmRegexLiteralParts parts of `pattern` and `flags` (PCREStyleToJsRegExpSource)
51
+ * ```
52
+ * type TCjbmRegexLiteralParts = {
53
+ * pattern: "...";
54
+ * flags: "gm";
55
+ * }
56
+ * ```
57
+ * @typedef {XRegex.PCREStyleRegExpPattern<TCjbmRegexLiteral>} TCjbmRegexLiteralPattern
58
+ * @typedef {XRegex.PCREStyleRegExpFlags<TCjbmRegexLiteral>} TCjbmRegexLiteralFlags
18
59
  */
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"
25
- );
60
+ const reImportExportDetection = compilePCREStyleRegExpLiteral(RE_TEXT);
26
61
  /**
27
62
  * @typedef {XRegex.ReplacerFunctionSignature<typeof reImportExportDetection>} TImportExportDetectRegexReplacer
28
63
  */
@@ -113,9 +148,13 @@ module.exports = () => {
113
148
  fn() {
114
149
  const bases = getBasePaths();
115
150
  const ext = params.ext || "js";
151
+ /**
152
+ * @typedef {XRegex.RegExpExecArrayFixedPretty<typeof reImportExportDetection>} TImportExportDetectRegexExecResultPretty
153
+ */
116
154
  /* const sourceFiles = await */
117
155
  processSources(
118
156
  /** @type {string} */ (this.taskName), (data) => {
157
+ /** @type {TImportExportDetectRegexExecResultPretty | null} */
119
158
  reImportExportDetection.lastIndex = 0;
120
159
  return data.replace(reImportExportDetection, getReplacer(ext));
121
160
  }, {
@@ -19,7 +19,7 @@ function cleanup(code) {
19
19
  "\s*\/(?:\s*\/+\*\s+(?:ctt|comment-toggle-trick|https:\/\/coderwall)([\s\S]+?)(?:\s+)\/\*\/[\s\S]+|\s*\*\s+(?:ctt|comment-toggle-trick|https:\/\/coderwall)[\s\S]+\/\*\/([\s\S]+?))\s*\/{2,}\*\/", "g"
20
20
  );
21
21
  return code.replace(
22
- reCleanUp, ($0, $1, $2) => $1 || $2,
22
+ reCleanUp, ($0, $1, $2) => $1 || $2 || $0,
23
23
  );
24
24
  }
25
25
  /**
@@ -5,7 +5,7 @@
5
5
  https://opensource.org/licenses/mit-license.php
6
6
  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
7
7
  */
8
- /// <reference path="../regex.d.ts"/>
8
+ /// <reference types="../literate-regex"/>
9
9
  /**
10
10
  * @date 2023-10-25
11
11
  */
package/tsconfig.json CHANGED
@@ -27,7 +27,8 @@
27
27
  "tools.js",
28
28
  "*.d.ts",
29
29
  "./**/*.js",
30
- "./**/*.d.ts"
30
+ "./**/*.d.ts",
31
+ "./literate-regex/*.d.ts",
31
32
  ],
32
33
  "exclude": [
33
34
  "./**/npm-outdated*.js",
@@ -1,22 +0,0 @@
1
- import fs from "fs";
2
- import path from "path";
3
- import { fileURLToPath } from "url";
4
-
5
- const __filename = fileURLToPath(import.meta.url);
6
- const __dirname = path.dirname(__filename);
7
- const commitMsgPath = path.resolve(__dirname, "..", ".codex", "commit_msg.txt");
8
-
9
- if (!fs.existsSync(commitMsgPath)) {
10
- process.exit(0);
11
- }
12
-
13
- let msg = fs.readFileSync(commitMsgPath, "utf8");
14
-
15
- msg = msg.replace(/\r\n/g, "\n");
16
- msg = msg
17
- .split("\n")
18
- .map(line => line.replace(/\s+$/u, ""))
19
- .join("\n");
20
- msg = msg.replace(/\n+$/u, "\n");
21
-
22
- fs.writeFileSync(commitMsgPath, msg, "utf8");
@@ -1,3 +0,0 @@
1
- {
2
- "version": "1.0.21"
3
- }