From 18b634632d2b8090d07f29a453ee396fe5f1755b Mon Sep 17 00:00:00 2001 From: Ian VanSchooten Date: Thu, 31 Oct 2024 12:32:45 -0400 Subject: [PATCH 1/3] Throw an error when no matching sortOrder group --- src/utils/get-sorted-nodes-by-import-order.ts | 9 +++-- test-setup/run_spec.ts | 36 +++++++++++++++++++ .../ImportOrderMissing/importOrderMissing.ts | 4 +++ tests/ImportOrderMissing/ppsi.spec.ts | 11 ++++++ 4 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 tests/ImportOrderMissing/importOrderMissing.ts create mode 100644 tests/ImportOrderMissing/ppsi.spec.ts diff --git a/src/utils/get-sorted-nodes-by-import-order.ts b/src/utils/get-sorted-nodes-by-import-order.ts index dc0f8e3..c7aa99f 100644 --- a/src/utils/get-sorted-nodes-by-import-order.ts +++ b/src/utils/get-sorted-nodes-by-import-order.ts @@ -56,11 +56,16 @@ 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}`)); + } } for (const group of importOrder) { diff --git a/test-setup/run_spec.ts b/test-setup/run_spec.ts index 48a0e85..419de58 100644 --- a/test-setup/run_spec.ts +++ b/test-setup/run_spec.ts @@ -64,6 +64,42 @@ 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, diff --git a/tests/ImportOrderMissing/importOrderMissing.ts b/tests/ImportOrderMissing/importOrderMissing.ts new file mode 100644 index 0000000..8afc9b4 --- /dev/null +++ b/tests/ImportOrderMissing/importOrderMissing.ts @@ -0,0 +1,4 @@ +import Foo from "./bar"; +import type {Justify} from "#utils" +import type {React} from "React"; +import type {Internal} from "./types.ts"; diff --git a/tests/ImportOrderMissing/ppsi.spec.ts b/tests/ImportOrderMissing/ppsi.spec.ts new file mode 100644 index 0000000..65fff2d --- /dev/null +++ b/tests/ImportOrderMissing/ppsi.spec.ts @@ -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", + { + importOrder: ['^[./]', '^#utils$', '[.]'], + importOrderParserPlugins: ['typescript'], + } +); From 06d886e3de2919a515155f868b9e8d7ce4958730 Mon Sep 17 00:00:00 2001 From: Ian VanSchooten Date: Thu, 31 Oct 2024 12:40:33 -0400 Subject: [PATCH 2/3] Add a hint for how to fix it --- src/utils/get-sorted-nodes-by-import-order.ts | 2 +- tests/ImportOrderMissing/ppsi.spec.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/get-sorted-nodes-by-import-order.ts b/src/utils/get-sorted-nodes-by-import-order.ts index c7aa99f..8d79c23 100644 --- a/src/utils/get-sorted-nodes-by-import-order.ts +++ b/src/utils/get-sorted-nodes-by-import-order.ts @@ -64,7 +64,7 @@ export const getSortedNodesByImportOrder: GetSortedNodesByImportOrder = ( 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}`)); + 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 ""?' : ''}`)); } } diff --git a/tests/ImportOrderMissing/ppsi.spec.ts b/tests/ImportOrderMissing/ppsi.spec.ts index 65fff2d..6e7058d 100644 --- a/tests/ImportOrderMissing/ppsi.spec.ts +++ b/tests/ImportOrderMissing/ppsi.spec.ts @@ -3,7 +3,7 @@ import {expectError} from '../../test-setup/run_spec'; expectError( __dirname, "typescript", - "Could not find a matching group in importOrder for: \"React\" on line 3", + /Could not find a matching group in importOrder for: \"React\" on line 3. Did you forget to include \"\"\?$/, { importOrder: ['^[./]', '^#utils$', '[.]'], importOrderParserPlugins: ['typescript'], From ca5645feb96402b39ca3bafbacd1e6fc530dff25 Mon Sep 17 00:00:00 2001 From: Ian VanSchooten Date: Thu, 31 Oct 2024 13:17:05 -0400 Subject: [PATCH 3/3] Format --- src/utils/get-sorted-nodes-by-import-order.ts | 4 +++- test-setup/run_spec.ts | 13 +++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/utils/get-sorted-nodes-by-import-order.ts b/src/utils/get-sorted-nodes-by-import-order.ts index 8d79c23..9c04215 100644 --- a/src/utils/get-sorted-nodes-by-import-order.ts +++ b/src/utils/get-sorted-nodes-by-import-order.ts @@ -64,7 +64,9 @@ export const getSortedNodesByImportOrder: GetSortedNodesByImportOrder = ( 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 ""?' : ''}`)); + 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 ""?' : ''}`, + ); } } diff --git a/test-setup/run_spec.ts b/test-setup/run_spec.ts index 419de58..7a277d8 100644 --- a/test-setup/run_spec.ts +++ b/test-setup/run_spec.ts @@ -64,7 +64,12 @@ export async function run_spec(dirname, parsers, options) { } } -export async function expectError(dirname: string, parser: string, expectedError: string | Error | RegExp, options) { +export async function expectError( + dirname: string, + parser: string, + expectedError: string | Error | RegExp, + options, +) { options = Object.assign( { plugins: options.plugins ?? [plugin], @@ -89,11 +94,11 @@ export async function expectError(dirname: string, parser: string, expectedError const source = read(path).replace(/\r\n/g, '\n'); const mergedOptions = Object.assign({}, options, { - parser + parser, }); test(`${filename} - verify-error`, async () => { - expect( - () => prettyprint(source, path, mergedOptions) + expect(() => + prettyprint(source, path, mergedOptions), ).rejects.toThrowError(expectedError); }); }