Skip to content

Commit

Permalink
feat: use real regex instead of strings
Browse files Browse the repository at this point in the history
Fixes #165
  • Loading branch information
lishaduck committed Jul 10, 2024
1 parent 57708e4 commit 915f30d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 42 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -61,6 +62,7 @@ module.exports = {
eqeqeq: [error, "always", { null: "ignore" }],
strict: error,
yoda: warn,
"no-control-regex": off,
},
overrides: [
{
Expand Down
66 changes: 33 additions & 33 deletions examples/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
],
},
],
Expand All @@ -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]],
},
],
},
Expand All @@ -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]],
},
],
},
Expand All @@ -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]],
},
],
},
Expand All @@ -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]],
},
],
},
Expand All @@ -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],
],
},
],
Expand All @@ -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],
],
},
],
Expand All @@ -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],
],
},
],
Expand All @@ -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$"],
["(?<!\\u0000)$", "(?<=\\u0000)$"],
["^\\.", "^\\..*\\u0000$"],
[/^\u0000/u],
[/^node:/u, /^node:.*\u0000$/u],
[/^@?\w/u, /^@?\w.*\u0000$/u],
[/(?<!\u0000)$/u, /(?<=\u0000)$/u],
[/^\./u, /^\..*\u0000$/u],
],
},
],
Expand Down
17 changes: 8 additions & 9 deletions src/imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ const shared = require("./shared");

const defaultGroups = [
// Side effect imports.
["^\\u0000"],
[/^\u0000/u],
// Node.js builtins prefixed with `node:`.
["^node:"],
[/^node:/u],
// Packages.
// Things that start with a letter (or digit or underscore), or `@` followed by a letter.
["^@?\\w"],
[/^@?\w/u],
// Absolute imports and other imports such as Vue-style `@/foo`.
// Anything not matched in another group.
["^"],
[/^/u],
// Relative imports.
// Anything that starts with a dot.
["^\\."],
[/^\./u],
];

module.exports = {
Expand All @@ -30,10 +30,9 @@ module.exports = {
type: "array",
items: {
type: "array",
items: {
type: "string",
},
uniqueItems: true,
},
uniqueItems: true,
},
},
additionalProperties: false,
Expand All @@ -51,7 +50,7 @@ module.exports = {
const { groups: rawGroups = defaultGroups } = context.options[0] || {};

const outerGroups = rawGroups.map((groups) =>
groups.map((item) => RegExp(item, "u")),
groups.map((item) => (item instanceof RegExp ? item : RegExp(item, "u"))),
);

const parents = new Set();
Expand Down

0 comments on commit 915f30d

Please sign in to comment.