Skip to content

Commit d218cab

Browse files
authored
Throw an error when no matching sortOrder group (#192)
Closes #190 This adds a more understandable error message when we encounter an import that does not fit into any group within `sortOrder`. I think the only time that should happen is when the user adds groups with `<TYPES>^.....` but no fallback `<TYPES>` bucket. We could try to shove one in somewhere automatically like we do with `<THIRD_PARTY_MODULES>`, but that could be pretty confusing and I don't know where it should go in that case. This also includes a hint about how to fix the error.
1 parent 41df923 commit d218cab

4 files changed

Lines changed: 65 additions & 2 deletions

File tree

src/utils/get-sorted-nodes-by-import-order.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,18 @@ export const getSortedNodesByImportOrder: GetSortedNodesByImportOrder = (
5656

5757
// Assign import nodes into import order groups
5858
for (const node of originalNodes) {
59-
const matchedGroup = getImportNodesMatchedGroup(
59+
const matchedGroupName = getImportNodesMatchedGroup(
6060
node,
6161
sanitizedImportOrder,
6262
);
63-
importOrderGroups[matchedGroup].push(node);
63+
const matchedGroup = importOrderGroups[matchedGroupName];
64+
if (matchedGroup) {
65+
matchedGroup.push(node);
66+
} else {
67+
throw new Error(
68+
`Could not find a matching group in importOrder for: "${node.source.value}" on line ${node.source.loc?.start.line}.${node.importKind === 'type' ? ' Did you forget to include "<TYPES>"?' : ''}`,
69+
);
70+
}
6471
}
6572

6673
for (const group of importOrder) {

test-setup/run_spec.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,47 @@ export async function run_spec(dirname, parsers, options) {
6464
}
6565
}
6666

67+
export async function expectError(
68+
dirname: string,
69+
parser: string,
70+
expectedError: string | Error | RegExp,
71+
options,
72+
) {
73+
options = Object.assign(
74+
{
75+
plugins: options.plugins ?? [plugin],
76+
tabWidth: 4,
77+
},
78+
options,
79+
);
80+
81+
/* instabul ignore if */
82+
if (!parser) {
83+
throw new Error(`No parser was specified for ${dirname}`);
84+
}
85+
86+
for (const filename of fs.readdirSync(dirname)) {
87+
const path = dirname + '/' + filename;
88+
if (
89+
extname(filename) !== '.snap' &&
90+
fs.lstatSync(path).isFile() &&
91+
filename[0] !== '.' &&
92+
filename !== 'ppsi.spec.ts'
93+
) {
94+
const source = read(path).replace(/\r\n/g, '\n');
95+
96+
const mergedOptions = Object.assign({}, options, {
97+
parser,
98+
});
99+
test(`${filename} - verify-error`, async () => {
100+
expect(() =>
101+
prettyprint(source, path, mergedOptions),
102+
).rejects.toThrowError(expectedError);
103+
});
104+
}
105+
}
106+
}
107+
67108
async function prettyprint(src, filename, options) {
68109
return await format(
69110
src,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import Foo from "./bar";
2+
import type {Justify} from "#utils"
3+
import type {React} from "React";
4+
import type {Internal} from "./types.ts";
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {expectError} from '../../test-setup/run_spec';
2+
3+
expectError(
4+
__dirname,
5+
"typescript",
6+
/Could not find a matching group in importOrder for: \"React\" on line 3. Did you forget to include \"<TYPES>\"\?$/,
7+
{
8+
importOrder: ['^[./]', '<TYPES>^#utils$', '<TYPES>[.]'],
9+
importOrderParserPlugins: ['typescript'],
10+
}
11+
);

0 commit comments

Comments
 (0)