diff --git a/README.md b/README.md index 7e279c6..ef05341 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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. ["^\\."], diff --git a/examples/.eslintrc.js b/examples/.eslintrc.js index 09ff288..ca07d36 100644 --- a/examples/.eslintrc.js +++ b/examples/.eslintrc.js @@ -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"]], }, ], }, diff --git a/examples/groups.default-reverse.js b/examples/groups.default-reverse.js new file mode 100644 index 0000000..3055313 --- /dev/null +++ b/examples/groups.default-reverse.js @@ -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"; diff --git a/src/sort.js b/src/sort.js index f07943b..93bf3e8 100644 --- a/src/sort.js +++ b/src/sort.js @@ -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. ["^\\."], diff --git a/test/__snapshots__/examples.test.js.snap b/test/__snapshots__/examples.test.js.snap index 0273177..1dc68d3 100644 --- a/test/__snapshots__/examples.test.js.snap +++ b/test/__snapshots__/examples.test.js.snap @@ -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"; diff --git a/test/sort.test.js b/test/sort.test.js index 4ad8024..90cf253 100644 --- a/test/sort.test.js +++ b/test/sort.test.js @@ -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 "/"; @@ -960,8 +961,6 @@ const baseTests = (expect) => ({ |import img2 from "./img2"; |import img10 from "./img10"; |import {} from ".a"; - | - |import {} from ""; `); }, errors: 1,