Skip to content

Commit e98c77a

Browse files
committed
feat: use real regex instead of strings
Fixes #165
1 parent 57708e4 commit e98c77a

File tree

3 files changed

+44
-42
lines changed

3 files changed

+44
-42
lines changed

.eslintrc.js

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const error = "error";
44
const warn = process.argv.includes("--report-unused-disable-directives")
55
? "error"
66
: "warn";
7+
const off = "off";
78

89
module.exports = {
910
root: true,
@@ -61,6 +62,7 @@ module.exports = {
6162
eqeqeq: [error, "always", { null: "ignore" }],
6263
strict: error,
6364
yoda: warn,
65+
"no-control-regex": off,
6466
},
6567
overrides: [
6668
{

examples/.eslintrc.js

+33-33
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,20 @@ module.exports = {
7777
// Note that if you use the `node:` prefix for Node.js builtins,
7878
// you can avoid this complexity: You can simply use "^node:".
7979
[
80-
"^(assert|buffer|child_process|cluster|console|constants|crypto|dgram|dns|domain|events|fs|http|https|module|net|os|path|punycode|querystring|readline|repl|stream|string_decoder|sys|timers|tls|tty|url|util|vm|zlib|freelist|v8|process|async_hooks|http2|perf_hooks)(/.*|$)",
80+
/^(assert|buffer|child_process|cluster|console|constants|crypto|dgram|dns|domain|events|fs|http|https|module|net|os|path|punycode|querystring|readline|repl|stream|string_decoder|sys|timers|tls|tty|url|util|vm|zlib|freelist|v8|process|async_hooks|http2|perf_hooks)(\/.*|$)/u,
8181
],
8282
// Packages. `react` related packages come first.
83-
["^react", "^@?\\w"],
83+
[/^react/u, /^@?\w/u],
8484
// Internal packages.
85-
["^(@|@company|@ui|components|utils|config|vendored-lib)(/.*|$)"],
85+
[/^(@|@company|@ui|components|utils|config|vendored-lib)(\/.*|$)/u],
8686
// Side effect imports.
87-
["^\\u0000"],
87+
[/^\u0000/u],
8888
// Parent imports. Put `..` last.
89-
["^\\.\\.(?!/?$)", "^\\.\\./?$"],
89+
[/^\.\.(?!\/?$)/u, /^\.\.\/?$/u],
9090
// Other relative imports. Put same-folder imports and `.` last.
91-
["^\\./(?=.*/)(?!/?$)", "^\\.(?!/?$)", "^\\./?$"],
91+
[/^\.\/(?=.*\/)(?!\/?$)/u, /^\.(?!\/?$)/u, /^\.\/?$/u],
9292
// Style imports.
93-
["^.+\\.s?css$"],
93+
[/^.+\.s?css$/u],
9494
],
9595
},
9696
],
@@ -103,7 +103,7 @@ module.exports = {
103103
"error",
104104
{
105105
// The default grouping, but with no blank lines.
106-
groups: [["^\\u0000", "^node:", "^@?\\w", "^", "^\\."]],
106+
groups: [[/^\u0000/u, /^node:/u, /^@?\w/u, /^/u, /^\./u]],
107107
},
108108
],
109109
},
@@ -115,7 +115,7 @@ module.exports = {
115115
"error",
116116
{
117117
// The default grouping, but in reverse.
118-
groups: [["^\\."], ["^"], ["^@?\\w"], ["^node:"], ["^\\u0000"]],
118+
groups: [[/^\./u], [/^/u], [/^@?\w/u], [/^node:/u], [/^\u0000/u]],
119119
},
120120
],
121121
},
@@ -128,7 +128,7 @@ module.exports = {
128128
"error",
129129
{
130130
// The default grouping, but with type imports first as a separate group.
131-
groups: [["^.*\\u0000$"], ["^\\u0000"], ["^node:"], ["^@?\\w"], ["^"], ["^\\."]],
131+
groups: [[/^.*\u0000$/u], [/^\u0000/u], [/^node:/u], [/^@?\w/u], [/^/u], [/^\./u]],
132132
},
133133
],
134134
},
@@ -141,7 +141,7 @@ module.exports = {
141141
"error",
142142
{
143143
// The default grouping, but with type imports last as a separate group.
144-
groups: [["^\\u0000"], ["^node:"], ["^@?\\w"], ["^"], ["^\\."], ["^.+\\u0000$"]],
144+
groups: [[/^\u0000/u], [/^node:/u], [/^@?\w/u], [/^/u], [/^\./u], [/^.+\u0000$/u]],
145145
},
146146
],
147147
},
@@ -156,12 +156,12 @@ module.exports = {
156156
// The default grouping, but with type imports first as a separate
157157
// group, sorting that group like non-type imports are grouped.
158158
groups: [
159-
["^node:.*\\u0000$", "^@?\\w.*\\u0000$", "^[^.].*\\u0000$", "^\\..*\\u0000$"],
160-
["^\\u0000"],
161-
["^node:"],
162-
["^@?\\w"],
163-
["^"],
164-
["^\\."],
159+
[/^node:.*\u0000$/u, /^@?\w.*\u0000$/u, /^[^.].*\u0000$/u, /^\..*\u0000$/u],
160+
[/^\u0000/u],
161+
[/^node:/u],
162+
[/^@?\w/u],
163+
[/^/u],
164+
[/^\./u],
165165
],
166166
},
167167
],
@@ -177,12 +177,12 @@ module.exports = {
177177
// The default grouping, but with type imports last as a separate
178178
// group, sorting that group like non-type imports are grouped.
179179
groups: [
180-
["^\\u0000"],
181-
["^node:"],
182-
["^@?\\w"],
183-
["^"],
184-
["^\\."],
185-
["^node:.*\\u0000$", "^@?\\w.*\\u0000$", "^[^.].*\\u0000$", "^\\..*\\u0000$"],
180+
[/^\u0000/u],
181+
[/^node:/u],
182+
[/^@?\w/u],
183+
[/^/u],
184+
[/^\./u],
185+
[/^node:.*\u0000$/u, /^@?\w.*\u0000$/u, /^[^.].*\u0000$/u, /^\..*\u0000$/u],
186186
],
187187
},
188188
],
@@ -197,11 +197,11 @@ module.exports = {
197197
{
198198
// The default grouping, but with type imports first in each group.
199199
groups: [
200-
["^\\u0000"],
201-
["^node:.*\\u0000$", "^node:"],
202-
["^@?\\w.*\\u0000$", "^@?\\w"],
203-
["(?<=\\u0000)$", "^"],
204-
["^\\..*\\u0000$", "^\\."],
200+
[/^\u0000/u],
201+
[/^node:.*\u0000$/u, /^node:/u],
202+
[/^@?\w.*\u0000$/u, /^@?\w/u],
203+
[/(?<=\u0000)$/u, /^/u],
204+
[/^\..*\u0000$/u, /^\./u],
205205
],
206206
},
207207
],
@@ -216,11 +216,11 @@ module.exports = {
216216
{
217217
// The default grouping, but with type imports last in each group.
218218
groups: [
219-
["^\\u0000"],
220-
["^node:", "^node:.*\\u0000$"],
221-
["^@?\\w", "^@?\\w.*\\u0000$"],
222-
["(?<!\\u0000)$", "(?<=\\u0000)$"],
223-
["^\\.", "^\\..*\\u0000$"],
219+
[/^\u0000/u],
220+
[/^node:/u, /^node:.*\u0000$/u],
221+
[/^@?\w/u, /^@?\w.*\u0000$/u],
222+
[/(?<!\u0000)$/u, /(?<=\u0000)$/u],
223+
[/^\./u, /^\..*\u0000$/u],
224224
],
225225
},
226226
],

src/imports.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
"use strict";
2+
// @ts-check
3+
// @ts-check
24

35
const shared = require("./shared");
46

57
const defaultGroups = [
68
// Side effect imports.
7-
["^\\u0000"],
9+
[/^\u0000/u],
810
// Node.js builtins prefixed with `node:`.
9-
["^node:"],
11+
[/^node:/u],
1012
// Packages.
1113
// Things that start with a letter (or digit or underscore), or `@` followed by a letter.
12-
["^@?\\w"],
14+
[/^@?\w/u],
1315
// Absolute imports and other imports such as Vue-style `@/foo`.
1416
// Anything not matched in another group.
15-
["^"],
17+
[/^/u],
1618
// Relative imports.
1719
// Anything that starts with a dot.
18-
["^\\."],
20+
[/^\./u],
1921
];
2022

2123
module.exports = {
@@ -30,9 +32,7 @@ module.exports = {
3032
type: "array",
3133
items: {
3234
type: "array",
33-
items: {
34-
type: "string",
35-
},
35+
uniqueItems: true,
3636
},
3737
},
3838
},
@@ -51,7 +51,7 @@ module.exports = {
5151
const { groups: rawGroups = defaultGroups } = context.options[0] || {};
5252

5353
const outerGroups = rawGroups.map((groups) =>
54-
groups.map((item) => RegExp(item, "u")),
54+
groups.map((item) => (item instanceof RegExp ? item : RegExp(item, "u"))),
5555
);
5656

5757
const parents = new Set();

0 commit comments

Comments
 (0)