Skip to content

Commit

Permalink
Make the default groups safely re-orderable
Browse files Browse the repository at this point in the history
Closes #47.
  • Loading branch information
lydell committed Nov 7, 2020
1 parent 2b362c4 commit 52ee218
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 8 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ type Options = {

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

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.
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.

> 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.
Expand All @@ -327,8 +327,8 @@ These are the default groups:
// Things that start with a letter (or digit or underscore), or `@` followed by a letter.
["^@?\\w"],
// Absolute imports and other imports such as Vue-style `@/foo`.
// Anything that does not start with a dot.
["^[^.]"],
// Anything not matched in another group.
["^"],
// Relative imports.
// Anything that starts with a dot.
["^\\."],
Expand Down
14 changes: 13 additions & 1 deletion examples/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,19 @@ module.exports = {
"error",
{
// The default grouping, but with no blank lines.
groups: [["^\\u0000", "^@?\\w", "^[^.]", "^\\."]],
groups: [["^\\u0000", "^@?\\w", "^", "^\\."]],
},
],
},
},
{
files: ["groups.default-reverse.js"],
rules: {
sort: [
"error",
{
// The default grouping, but in reverse.
groups: [["^\\."], ["^"], ["^@?\\w"], ["^\\u0000"]],
},
],
},
Expand Down
6 changes: 6 additions & 0 deletions examples/groups.default-reverse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import "./polyfills";
import react from "react";
import { storiesOf } from "@storybook/react";
import App from "@/App";
import styles from "./styles";
import config from "/config";
4 changes: 2 additions & 2 deletions src/sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const defaultGroups = [
// Things that start with a letter (or digit or underscore), or `@` followed by a letter.
["^@?\\w"],
// Absolute imports and other imports such as Vue-style `@/foo`.
// Anything that does not start with a dot.
["^[^.]"],
// Anything not matched in another group.
["^"],
// Relative imports.
// Anything that starts with a dot.
["^\\."],
Expand Down
13 changes: 13 additions & 0 deletions test/__snapshots__/examples.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,19 @@ import styles from "./styles.scss";
`;

exports[`examples groups.default-reverse.js 1`] = `
import styles from "./styles";
import App from "@/App";
import config from "/config";
import { storiesOf } from "@storybook/react";
import react from "react";
import "./polyfills";
`;

exports[`examples groups.no-blank-lines.js 1`] = `
import classnames from "classnames";
import PropTypes from "prop-types";
Expand Down
3 changes: 1 addition & 2 deletions test/sort.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,7 @@ const baseTests = (expect) => ({
|import {} from "lodash/fp";
|import {} from "react";
|
|import {} from "";
|import {} from "@/components/Alert"
|import {} from "@/components/error.vue"
|import {} from "/";
Expand Down Expand Up @@ -960,8 +961,6 @@ const baseTests = (expect) => ({
|import img2 from "./img2";
|import img10 from "./img10";
|import {} from ".a";
|
|import {} from "";
`);
},
errors: 1,
Expand Down

0 comments on commit 52ee218

Please sign in to comment.