-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Add
<BUILTIN_MODULES>
Special Word
- This allows users to specify a gap between built-ins and third-party in a slightly less wonky way. - This also allows users to move the built-ins around relative to other imports.
- Loading branch information
Showing
11 changed files
with
85 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { | ||
BUILTIN_MODULES_REGEX_STR, | ||
BUILTIN_MODULES_SPECIAL_WORD, | ||
THIRD_PARTY_MODULES_SPECIAL_WORD, | ||
} from '../constants'; | ||
import { PrettierOptions } from '../types'; | ||
|
||
export function normalizeImportOrderOption( | ||
importOrder: PrettierOptions['importOrder'], | ||
) { | ||
// THIRD_PARTY_MODULES_SPECIAL_WORD is magic because "everything not matched by other groups goes here" | ||
// So it must always be present. | ||
if (!importOrder.includes(THIRD_PARTY_MODULES_SPECIAL_WORD)) { | ||
importOrder = [THIRD_PARTY_MODULES_SPECIAL_WORD, ...importOrder]; | ||
} | ||
|
||
// Opinionated Decision: NodeJS Builtin modules should always be separate from third party modules | ||
// Users may want to add their own separators around them or insert other modules above them though | ||
if ( | ||
!( | ||
importOrder.includes(BUILTIN_MODULES_SPECIAL_WORD) || | ||
importOrder.includes(BUILTIN_MODULES_REGEX_STR) | ||
) | ||
) { | ||
importOrder = [BUILTIN_MODULES_SPECIAL_WORD, ...importOrder]; | ||
} | ||
|
||
importOrder = importOrder.map((g) => | ||
g === BUILTIN_MODULES_SPECIAL_WORD ? BUILTIN_MODULES_REGEX_STR : g, | ||
); | ||
|
||
return importOrder; | ||
} |
20 changes: 20 additions & 0 deletions
20
tests/ImportOrderBuiltinModulesToCustom/__snapshots__/ppsi.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`exercise-custom-builtin-modules-spacing.ts - typescript-verify > exercise-custom-builtin-modules-spacing.ts 1`] = ` | ||
// Top-of-file-comment | ||
import path from "path" | ||
import b from 'b'; | ||
import foo from './foo'; | ||
import thirdParty from 'third-party'; | ||
import fs from "node:fs" | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
// Top-of-file-comment | ||
import b from "b"; | ||
import thirdParty from "third-party"; | ||
import fs from "node:fs"; | ||
import path from "path"; | ||
import foo from "./foo"; | ||
`; |
6 changes: 6 additions & 0 deletions
6
tests/ImportOrderBuiltinModulesToCustom/exercise-custom-builtin-modules-spacing.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Top-of-file-comment | ||
import path from "path" | ||
import b from 'b'; | ||
import foo from './foo'; | ||
import thirdParty from 'third-party'; | ||
import fs from "node:fs" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import {run_spec} from '../../test-setup/run_spec'; | ||
|
||
run_spec(__dirname, ['typescript'], { | ||
importOrder: ['<THIRD_PARTY_MODULES>','','<BUILTIN_MODULES>','','^[./]'], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import {run_spec} from '../../test-setup/run_spec'; | ||
|
||
run_spec(__dirname, ['typescript'], { | ||
importOrder: ['^[./]'], | ||
importOrder: ['','<THIRD_PARTY_MODULES>','^[./]'], | ||
}); |
1 change: 1 addition & 0 deletions
1
tests/ImportOrderBuiltinModulesToTop/relative-import-with-builtin-substring.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
import foo from './constants/foo'; | ||
import thirdParty from 'third-party'; | ||
import fs from "fs" |