Skip to content
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

Throw an error when no matching sortOrder group #192

Merged
merged 3 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'],
}
);
Loading