-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: Add <BUILTIN_MODULES>
Special Word
#86
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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, | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do this map here, vs just injecting the regex str on line 25? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could inject the regex-str up on line 25, but it wouldn’t allow for us to remove the Here’s my thought process our code sort of looks like this:
I like gaps between my groups of imports, so we’d have this awkward leading empty string on the array And we’d never even be able to express “put a gap above built-ins, but after top-of-file comments” if we didn’t have that special word. — Unfortunately, we still can’t do that (yet) even if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh right, I was neglecting to consider that we need to support both having the special word in the config and injecting it. The third-party special word is handled elsewhere. |
||
|
||
return importOrder; | ||
} |
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"; | ||
|
||
`; |
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" |
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>','','^[./]'], | ||
}); |
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>','^[./]'], | ||
}); |
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" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice little perf improvement here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted also to parse
importOrder
and normalize it only once, but that requires a further cleanup because there’s logic that looks at the first entry inimportOrder
and does something if it’s a space.I decided to submit this PR as-is and consider if it’s worth changing that at some future date.