From 915f30d6d6c1118710b2924a669680c319d9e2f2 Mon Sep 17 00:00:00 2001 From: Eli <88557639+lishaduck@users.noreply.github.com> Date: Tue, 9 Jul 2024 22:42:54 -0500 Subject: [PATCH] feat: use real regex instead of strings Fixes #165 --- .eslintrc.js | 2 ++ examples/.eslintrc.js | 66 +++++++++++++++++++++---------------------- src/imports.js | 17 ++++++----- 3 files changed, 43 insertions(+), 42 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 54f89af..e599e07 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -4,6 +4,7 @@ const error = "error"; const warn = process.argv.includes("--report-unused-disable-directives") ? "error" : "warn"; +const off = "off"; module.exports = { root: true, @@ -61,6 +62,7 @@ module.exports = { eqeqeq: [error, "always", { null: "ignore" }], strict: error, yoda: warn, + "no-control-regex": off, }, overrides: [ { diff --git a/examples/.eslintrc.js b/examples/.eslintrc.js index 44df29b..5119fe0 100644 --- a/examples/.eslintrc.js +++ b/examples/.eslintrc.js @@ -77,20 +77,20 @@ module.exports = { // Note that if you use the `node:` prefix for Node.js builtins, // you can avoid this complexity: You can simply use "^node:". [ - "^(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)(/.*|$)", + /^(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, ], // Packages. `react` related packages come first. - ["^react", "^@?\\w"], + [/^react/u, /^@?\w/u], // Internal packages. - ["^(@|@company|@ui|components|utils|config|vendored-lib)(/.*|$)"], + [/^(@|@company|@ui|components|utils|config|vendored-lib)(\/.*|$)/u], // Side effect imports. - ["^\\u0000"], + [/^\u0000/u], // Parent imports. Put `..` last. - ["^\\.\\.(?!/?$)", "^\\.\\./?$"], + [/^\.\.(?!\/?$)/u, /^\.\.\/?$/u], // Other relative imports. Put same-folder imports and `.` last. - ["^\\./(?=.*/)(?!/?$)", "^\\.(?!/?$)", "^\\./?$"], + [/^\.\/(?=.*\/)(?!\/?$)/u, /^\.(?!\/?$)/u, /^\.\/?$/u], // Style imports. - ["^.+\\.s?css$"], + [/^.+\.s?css$/u], ], }, ], @@ -103,7 +103,7 @@ module.exports = { "error", { // The default grouping, but with no blank lines. - groups: [["^\\u0000", "^node:", "^@?\\w", "^", "^\\."]], + groups: [[/^\u0000/u, /^node:/u, /^@?\w/u, /^/u, /^\./u]], }, ], }, @@ -115,7 +115,7 @@ module.exports = { "error", { // The default grouping, but in reverse. - groups: [["^\\."], ["^"], ["^@?\\w"], ["^node:"], ["^\\u0000"]], + groups: [[/^\./u], [/^/u], [/^@?\w/u], [/^node:/u], [/^\u0000/u]], }, ], }, @@ -128,7 +128,7 @@ module.exports = { "error", { // The default grouping, but with type imports first as a separate group. - groups: [["^.*\\u0000$"], ["^\\u0000"], ["^node:"], ["^@?\\w"], ["^"], ["^\\."]], + groups: [[/^.*\u0000$/u], [/^\u0000/u], [/^node:/u], [/^@?\w/u], [/^/u], [/^\./u]], }, ], }, @@ -141,7 +141,7 @@ module.exports = { "error", { // The default grouping, but with type imports last as a separate group. - groups: [["^\\u0000"], ["^node:"], ["^@?\\w"], ["^"], ["^\\."], ["^.+\\u0000$"]], + groups: [[/^\u0000/u], [/^node:/u], [/^@?\w/u], [/^/u], [/^\./u], [/^.+\u0000$/u]], }, ], }, @@ -156,12 +156,12 @@ module.exports = { // The default grouping, but with type imports first as a separate // group, sorting that group like non-type imports are grouped. groups: [ - ["^node:.*\\u0000$", "^@?\\w.*\\u0000$", "^[^.].*\\u0000$", "^\\..*\\u0000$"], - ["^\\u0000"], - ["^node:"], - ["^@?\\w"], - ["^"], - ["^\\."], + [/^node:.*\u0000$/u, /^@?\w.*\u0000$/u, /^[^.].*\u0000$/u, /^\..*\u0000$/u], + [/^\u0000/u], + [/^node:/u], + [/^@?\w/u], + [/^/u], + [/^\./u], ], }, ], @@ -177,12 +177,12 @@ module.exports = { // The default grouping, but with type imports last as a separate // group, sorting that group like non-type imports are grouped. groups: [ - ["^\\u0000"], - ["^node:"], - ["^@?\\w"], - ["^"], - ["^\\."], - ["^node:.*\\u0000$", "^@?\\w.*\\u0000$", "^[^.].*\\u0000$", "^\\..*\\u0000$"], + [/^\u0000/u], + [/^node:/u], + [/^@?\w/u], + [/^/u], + [/^\./u], + [/^node:.*\u0000$/u, /^@?\w.*\u0000$/u, /^[^.].*\u0000$/u, /^\..*\u0000$/u], ], }, ], @@ -197,11 +197,11 @@ module.exports = { { // The default grouping, but with type imports first in each group. groups: [ - ["^\\u0000"], - ["^node:.*\\u0000$", "^node:"], - ["^@?\\w.*\\u0000$", "^@?\\w"], - ["(?<=\\u0000)$", "^"], - ["^\\..*\\u0000$", "^\\."], + [/^\u0000/u], + [/^node:.*\u0000$/u, /^node:/u], + [/^@?\w.*\u0000$/u, /^@?\w/u], + [/(?<=\u0000)$/u, /^/u], + [/^\..*\u0000$/u, /^\./u], ], }, ], @@ -216,11 +216,11 @@ module.exports = { { // The default grouping, but with type imports last in each group. groups: [ - ["^\\u0000"], - ["^node:", "^node:.*\\u0000$"], - ["^@?\\w", "^@?\\w.*\\u0000$"], - ["(? - groups.map((item) => RegExp(item, "u")), + groups.map((item) => (item instanceof RegExp ? item : RegExp(item, "u"))), ); const parents = new Set();