Skip to content

Commit

Permalink
Throw an error when no matching sortOrder group (#192)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
IanVS authored Nov 6, 2024
1 parent 41df923 commit d218cab
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/utils/get-sorted-nodes-by-import-order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,18 @@ export const getSortedNodesByImportOrder: GetSortedNodesByImportOrder = (

// Assign import nodes into import order groups
for (const node of originalNodes) {
const matchedGroup = getImportNodesMatchedGroup(
const matchedGroupName = getImportNodesMatchedGroup(
node,
sanitizedImportOrder,
);
importOrderGroups[matchedGroup].push(node);
const matchedGroup = importOrderGroups[matchedGroupName];
if (matchedGroup) {
matchedGroup.push(node);
} else {
throw new Error(
`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>"?' : ''}`,
);
}
}

for (const group of importOrder) {
Expand Down
41 changes: 41 additions & 0 deletions test-setup/run_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,47 @@ export async function run_spec(dirname, parsers, options) {
}
}

export async function expectError(
dirname: string,
parser: string,
expectedError: string | Error | RegExp,
options,
) {
options = Object.assign(
{
plugins: options.plugins ?? [plugin],
tabWidth: 4,
},
options,
);

/* instabul ignore if */
if (!parser) {
throw new Error(`No parser was specified for ${dirname}`);
}

for (const filename of fs.readdirSync(dirname)) {
const path = dirname + '/' + filename;
if (
extname(filename) !== '.snap' &&
fs.lstatSync(path).isFile() &&
filename[0] !== '.' &&
filename !== 'ppsi.spec.ts'
) {
const source = read(path).replace(/\r\n/g, '\n');

const mergedOptions = Object.assign({}, options, {
parser,
});
test(`${filename} - verify-error`, async () => {
expect(() =>
prettyprint(source, path, mergedOptions),
).rejects.toThrowError(expectedError);
});
}
}
}

async function prettyprint(src, filename, options) {
return await format(
src,
Expand Down
4 changes: 4 additions & 0 deletions tests/ImportOrderMissing/importOrderMissing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Foo from "./bar";
import type {Justify} from "#utils"
import type {React} from "React";
import type {Internal} from "./types.ts";
11 changes: 11 additions & 0 deletions tests/ImportOrderMissing/ppsi.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {expectError} from '../../test-setup/run_spec';

expectError(
__dirname,
"typescript",
/Could not find a matching group in importOrder for: \"React\" on line 3. Did you forget to include \"<TYPES>\"\?$/,
{
importOrder: ['^[./]', '<TYPES>^#utils$', '<TYPES>[.]'],
importOrderParserPlugins: ['typescript'],
}
);

0 comments on commit d218cab

Please sign in to comment.