Skip to content

Commit

Permalink
Put node:* modules in a separate group by default (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
lydell committed Sep 3, 2022
1 parent 5c8ef2a commit 0e3719e
Show file tree
Hide file tree
Showing 15 changed files with 101 additions and 14 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018, 2019, 2020 Simon Lydell
Copyright (c) 2018, 2019, 2020, 2022 Simon Lydell

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,10 @@ First, the plugin finds all _chunks_ of imports. A “chunk” is a sequence of
Then, each chunk is _grouped_ into sections with a blank line between each.

1. `import "./setup"`: Side effect imports. (These are not sorted internally.)
2. `import react from "react"`: Packages (npm packages and Node.js builtins).
3. `import a from "/a"`: Absolute imports and other imports such as Vue-style `@/foo`.
4. `import a from "./a"`: Relative imports.
2. `import * as fs from "node:fs"`: Node.js builtin modules prefixed with `node:`.
3. `import react from "react"`: Packages (npm packages and Node.js builtins _without_ `node:`).
4. `import a from "/a"`: Absolute imports and other imports such as Vue-style `@/foo`.
5. `import a from "./a"`: Relative imports.

Note: The above groups are very loosely defined. See [Custom grouping] for more information.

Expand Down Expand Up @@ -227,10 +228,13 @@ import "./setup";
import "some-polyfill";
import "./global.css";

// Node.js builtins prefixed with `node:`.
import * as fs from "node:fs";

// Packages.
import type A from "an-npm-package";
import a from "an-npm-package";
import fs from "fs";
import fs2 from "fs";
import b from "https://example.com/script.js";

// Absolute imports and other imports.
Expand Down Expand Up @@ -323,7 +327,6 @@ There is **one** option (see [Not for everyone]) called `groups` that allows you
- Move `src/Button`, `@company/Button` and similar out of the (third party) “packages” group, into their own group.
- Move `react` first.
- Remove blank lines between groups.
- Make a separate group for Node.js builtins.
- Make a separate group for style imports.
- Separate `./` and `../` imports.
- Not use groups at all and only sort alphabetically.
Expand Down Expand Up @@ -364,6 +367,8 @@ These are the default groups:
[
// Side effect imports.
["^\\u0000"],
// Node.js builtins prefixed with `node:`.
["^node:"],
// Packages.
// Things that start with a letter (or digit or underscore), or `@` followed by a letter.
["^@?\\w"],
Expand Down
18 changes: 12 additions & 6 deletions examples/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ module.exports = {
groups: [
// Node.js builtins. You could also generate this regex if you use a `.js` config.
// For example: `^(${require("module").builtinModules.join("|")})(/|$)`
// 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)(/.*|$)",
],
Expand Down Expand Up @@ -101,7 +103,7 @@ module.exports = {
"error",
{
// The default grouping, but with no blank lines.
groups: [["^\\u0000", "^@?\\w", "^", "^\\."]],
groups: [["^\\u0000", "^node:", "^@?\\w", "^", "^\\."]],
},
],
},
Expand All @@ -113,7 +115,7 @@ module.exports = {
"error",
{
// The default grouping, but in reverse.
groups: [["^\\."], ["^"], ["^@?\\w"], ["^\\u0000"]],
groups: [["^\\."], ["^"], ["^@?\\w"], ["^node:"], ["^\\u0000"]],
},
],
},
Expand All @@ -126,7 +128,7 @@ module.exports = {
"error",
{
// The default grouping, but with type imports first as a separate group.
groups: [["^.*\\u0000$"], ["^\\u0000"], ["^@?\\w"], ["^"], ["^\\."]],
groups: [["^.*\\u0000$"], ["^\\u0000"], ["^node:"], ["^@?\\w"], ["^"], ["^\\."]],
},
],
},
Expand All @@ -139,7 +141,7 @@ module.exports = {
"error",
{
// The default grouping, but with type imports last as a separate group.
groups: [["^\\u0000"], ["^@?\\w"], ["^"], ["^\\."], ["^.+\\u0000$"]],
groups: [["^\\u0000"], ["^node:"], ["^@?\\w"], ["^"], ["^\\."], ["^.+\\u0000$"]],
},
],
},
Expand All @@ -154,8 +156,9 @@ module.exports = {
// The default grouping, but with type imports first as a separate
// group, sorting that group like non-type imports are grouped.
groups: [
["^@?\\w.*\\u0000$", "^[^.].*\\u0000$", "^\\..*\\u0000$"],
["^node:.*\\u0000$", "^@?\\w.*\\u0000$", "^[^.].*\\u0000$", "^\\..*\\u0000$"],
["^\\u0000"],
["^node:"],
["^@?\\w"],
["^"],
["^\\."],
Expand All @@ -175,10 +178,11 @@ module.exports = {
// group, sorting that group like non-type imports are grouped.
groups: [
["^\\u0000"],
["^node:"],
["^@?\\w"],
["^"],
["^\\."],
["^@?\\w.*\\u0000$", "^[^.].*\\u0000$", "^\\..*\\u0000$"],
["^node:.*\\u0000$", "^@?\\w.*\\u0000$", "^[^.].*\\u0000$", "^\\..*\\u0000$"],
],
},
],
Expand All @@ -194,6 +198,7 @@ module.exports = {
// The default grouping, but with type imports first in each group.
groups: [
["^\\u0000"],
["^node:.*\\u0000$", "^node:"],
["^@?\\w.*\\u0000$", "^@?\\w"],
["(?<=\\u0000)$", "^"],
["^\\..*\\u0000$", "^\\."],
Expand All @@ -212,6 +217,7 @@ module.exports = {
// The default grouping, but with type imports last in each group.
groups: [
["^\\u0000"],
["^node:", "^node:.*\\u0000$"],
["^@?\\w", "^@?\\w.*\\u0000$"],
["(?<!\\u0000)$", "(?<=\\u0000)$"],
["^\\.", "^\\..*\\u0000$"],
Expand Down
3 changes: 3 additions & 0 deletions examples/groups.default-reverse.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import "./polyfills";
import * as fs from "node:fs";
import fs2 from "fs";
import forge from "node-forge";
import react from "react";
import { storiesOf } from "@storybook/react";
import App from "@/App";
Expand Down
1 change: 1 addition & 0 deletions examples/groups.none.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as fs from "node:fs";
import react from "react";
import { storiesOf } from "@storybook/react";
import App from "@/App";
Expand Down
4 changes: 4 additions & 0 deletions examples/groups.type-imports-first-in-each-group.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import "./polyfills";
import fs from "node:fs";
import type { Dirent } from "node:fs";
import type { ParsedPath } from "node:path";
import * as path from "node:path";
import react from "react";
import type { Component } from "react";
import type { Store } from "redux";
Expand Down
4 changes: 4 additions & 0 deletions examples/groups.type-imports-first-sorted.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import "./polyfills";
import fs from "node:fs";
import type { Dirent } from "node:fs";
import type { ParsedPath } from "node:path";
import * as path from "node:path";
import react from "react";
import type { Component } from "react";
import type { Store } from "redux";
Expand Down
4 changes: 4 additions & 0 deletions examples/groups.type-imports-first.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import "./polyfills";
import fs from "node:fs";
import type { Dirent } from "node:fs";
import type { ParsedPath } from "node:path";
import * as path from "node:path";
import react from "react";
import type { Component } from "react";
import type { Store } from "redux";
Expand Down
4 changes: 4 additions & 0 deletions examples/groups.type-imports-last-in-each-group.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import "./polyfills";
import fs from "node:fs";
import type { Dirent } from "node:fs";
import type { ParsedPath } from "node:path";
import * as path from "node:path";
import react from "react";
import type { Component } from "react";
import type { Store } from "redux";
Expand Down
4 changes: 4 additions & 0 deletions examples/groups.type-imports-last-sorted.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import "./polyfills";
import fs from "node:fs";
import type { Dirent } from "node:fs";
import type { ParsedPath } from "node:path";
import * as path from "node:path";
import react from "react";
import type { Component } from "react";
import type { Store } from "redux";
Expand Down
4 changes: 4 additions & 0 deletions examples/groups.type-imports-last.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import "./polyfills";
import fs from "node:fs";
import type { Dirent } from "node:fs";
import type { ParsedPath } from "node:path";
import * as path from "node:path";
import react from "react";
import type { Component } from "react";
import type { Store } from "redux";
Expand Down
5 changes: 4 additions & 1 deletion examples/readme-order.prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import "./setup";
import "some-polyfill";
import "./global.css";

// Node.js builtins prefixed with `node:`.
import * as fs from "node:fs";

// Packages.
import type A from "an-npm-package";
import a from "an-npm-package";
import fs from "fs";
import fs2 from "fs";
import b from "https://example.com/script.js";

// Absolute imports and other imports.
Expand Down
2 changes: 2 additions & 0 deletions src/imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const shared = require("./shared");
const defaultGroups = [
// Side effect imports.
["^\\u0000"],
// Node.js builtins prefixed with `node:`.
["^node:"],
// Packages.
// Things that start with a letter (or digit or underscore), or `@` followed by a letter.
["^@?\\w"],
Expand Down
40 changes: 39 additions & 1 deletion test/__snapshots__/examples.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,12 @@ import config from "/config";
import App from "@/App";
import { storiesOf } from "@storybook/react";
import fs2 from "fs";
import forge from "node-forge";
import react from "react";
import * as fs from "node:fs";
import "./polyfills";
`;
Expand All @@ -131,6 +135,7 @@ import styles from "./styles";
import config from "/config";
import App from "@/App";
import { storiesOf } from "@storybook/react";
import * as fs from "node:fs";
import react from "react";
`;
Expand All @@ -141,11 +146,16 @@ import type { Css } from "./styles";
import type { Config } from "/config";
import type { AppRouter } from "@/App";
import type { Story } from "@storybook/react";
import type { Dirent } from "node:fs";
import type { ParsedPath } from "node:path";
import type { Component } from "react";
import type { Store } from "redux";
import "./polyfills";
import fs from "node:fs";
import * as path from "node:path";
import { storiesOf } from "@storybook/react";
import react from "react";
Expand All @@ -160,6 +170,11 @@ import styles from "./styles";
exports[`examples groups.type-imports-first-in-each-group.ts 1`] = `
import "./polyfills";
import type { Dirent } from "node:fs";
import type { ParsedPath } from "node:path";
import fs from "node:fs";
import * as path from "node:path";
import type { Story } from "@storybook/react";
import type { Component } from "react";
import type { Store } from "redux";
Expand All @@ -179,6 +194,8 @@ import styles from "./styles";
`;

exports[`examples groups.type-imports-first-sorted.ts 1`] = `
import type { Dirent } from "node:fs";
import type { ParsedPath } from "node:path";
import type { Story } from "@storybook/react";
import type { Component } from "react";
import type { Store } from "redux";
Expand All @@ -189,6 +206,9 @@ import type { Css } from "./styles";
import "./polyfills";
import fs from "node:fs";
import * as path from "node:path";
import { storiesOf } from "@storybook/react";
import react from "react";
Expand All @@ -203,6 +223,9 @@ import styles from "./styles";
exports[`examples groups.type-imports-last.ts 1`] = `
import "./polyfills";
import fs from "node:fs";
import * as path from "node:path";
import { storiesOf } from "@storybook/react";
import react from "react";
Expand All @@ -217,6 +240,8 @@ import type { Css } from "./styles";
import type { Config } from "/config";
import type { AppRouter } from "@/App";
import type { Story } from "@storybook/react";
import type { Dirent } from "node:fs";
import type { ParsedPath } from "node:path";
import type { Component } from "react";
import type { Store } from "redux";
Expand All @@ -225,6 +250,11 @@ import type { Store } from "redux";
exports[`examples groups.type-imports-last-in-each-group.ts 1`] = `
import "./polyfills";
import fs from "node:fs";
import * as path from "node:path";
import type { Dirent } from "node:fs";
import type { ParsedPath } from "node:path";
import { storiesOf } from "@storybook/react";
import react from "react";
import type { Story } from "@storybook/react";
Expand All @@ -246,6 +276,9 @@ import type { Css } from "./styles";
exports[`examples groups.type-imports-last-sorted.ts 1`] = `
import "./polyfills";
import fs from "node:fs";
import * as path from "node:path";
import { storiesOf } from "@storybook/react";
import react from "react";
Expand All @@ -255,6 +288,8 @@ import App from "@/App";
import page from "./page";
import styles from "./styles";
import type { Dirent } from "node:fs";
import type { ParsedPath } from "node:path";
import type { Story } from "@storybook/react";
import type { Component } from "react";
import type { Store } from "redux";
Expand Down Expand Up @@ -439,10 +474,13 @@ import "./setup";
import "some-polyfill";
import "./global.css";
// Node.js builtins prefixed with \`node:\`.
import * as fs from "node:fs";
// Packages.
import type A from "an-npm-package";
import a from "an-npm-package";
import fs from "fs";
import fs2 from "fs";
import b from "https://example.com/script.js";
// Absolute imports and other imports.
Expand Down
5 changes: 5 additions & 0 deletions test/imports.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,8 @@ const baseTests = (expect) => ({
|import {} from "#/test"
|import {} from "fs";
|import {} from "fs/something";
|import {} from "node:fs/something";
|import {} from "node:fs";
|import {} from "Fs";
|import {} from "lodash/fp";
|import {} from "@storybook/react";
Expand All @@ -854,6 +856,9 @@ const baseTests = (expect) => ({
`,
output: (actual) => {
expect(actual).toMatchInlineSnapshot(`
|import {} from "node:fs";
|import {} from "node:fs/something";
|
|import {} from "@storybook/react";
|import {} from "@storybook/react/something";
|import {} from "1";
Expand Down

0 comments on commit 0e3719e

Please sign in to comment.