Skip to content

Commit 52ee218

Browse files
committed
Make the default groups safely re-orderable
Closes #47.
1 parent 2b362c4 commit 52ee218

File tree

6 files changed

+38
-8
lines changed

6 files changed

+38
-8
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ type Options = {
305305

306306
Each string is a regex (with the `u` flag) and defines a group. (Remember to escape backslashes – it’s `"\\w"`, not `"\w"`, for example.)
307307

308-
Each `import` is matched against _all_ regexes on the `from` string. The import ends up in the group with **the longest match.** In case of a tie, the first matching group wins.
308+
Each `import` is matched against _all_ regexes on the `from` string. The import ends up in the group with **the longest match.** In case of a tie, the **first** matching group wins.
309309

310310
> If an import ends up in the wrong group – try making the desired group regex match more of the `from` string, or use negative lookahead (`(?!x)`) to exclude things from other groups.
311311
@@ -327,8 +327,8 @@ These are the default groups:
327327
// Things that start with a letter (or digit or underscore), or `@` followed by a letter.
328328
["^@?\\w"],
329329
// Absolute imports and other imports such as Vue-style `@/foo`.
330-
// Anything that does not start with a dot.
331-
["^[^.]"],
330+
// Anything not matched in another group.
331+
["^"],
332332
// Relative imports.
333333
// Anything that starts with a dot.
334334
["^\\."],

examples/.eslintrc.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,19 @@ module.exports = {
9898
"error",
9999
{
100100
// The default grouping, but with no blank lines.
101-
groups: [["^\\u0000", "^@?\\w", "^[^.]", "^\\."]],
101+
groups: [["^\\u0000", "^@?\\w", "^", "^\\."]],
102+
},
103+
],
104+
},
105+
},
106+
{
107+
files: ["groups.default-reverse.js"],
108+
rules: {
109+
sort: [
110+
"error",
111+
{
112+
// The default grouping, but in reverse.
113+
groups: [["^\\."], ["^"], ["^@?\\w"], ["^\\u0000"]],
102114
},
103115
],
104116
},

examples/groups.default-reverse.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import "./polyfills";
2+
import react from "react";
3+
import { storiesOf } from "@storybook/react";
4+
import App from "@/App";
5+
import styles from "./styles";
6+
import config from "/config";

src/sort.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ const defaultGroups = [
77
// Things that start with a letter (or digit or underscore), or `@` followed by a letter.
88
["^@?\\w"],
99
// Absolute imports and other imports such as Vue-style `@/foo`.
10-
// Anything that does not start with a dot.
11-
["^[^.]"],
10+
// Anything not matched in another group.
11+
["^"],
1212
// Relative imports.
1313
// Anything that starts with a dot.
1414
["^\\."],

test/__snapshots__/examples.test.js.snap

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,19 @@ import styles from "./styles.scss";
101101
102102
`;
103103

104+
exports[`examples groups.default-reverse.js 1`] = `
105+
import styles from "./styles";
106+
107+
import App from "@/App";
108+
import config from "/config";
109+
110+
import { storiesOf } from "@storybook/react";
111+
import react from "react";
112+
113+
import "./polyfills";
114+
115+
`;
116+
104117
exports[`examples groups.no-blank-lines.js 1`] = `
105118
import classnames from "classnames";
106119
import PropTypes from "prop-types";

test/sort.test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,7 @@ const baseTests = (expect) => ({
923923
|import {} from "lodash/fp";
924924
|import {} from "react";
925925
|
926+
|import {} from "";
926927
|import {} from "@/components/Alert"
927928
|import {} from "@/components/error.vue"
928929
|import {} from "/";
@@ -960,8 +961,6 @@ const baseTests = (expect) => ({
960961
|import img2 from "./img2";
961962
|import img10 from "./img10";
962963
|import {} from ".a";
963-
|
964-
|import {} from "";
965964
`);
966965
},
967966
errors: 1,

0 commit comments

Comments
 (0)