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

Confusing error thrown when no matching importOrder is found #190

Open
1 task
gyenabubakar opened this issue Oct 31, 2024 · 3 comments · May be fixed by #192
Open
1 task

Confusing error thrown when no matching importOrder is found #190

gyenabubakar opened this issue Oct 31, 2024 · 3 comments · May be fixed by #192
Assignees

Comments

@gyenabubakar
Copy link

gyenabubakar commented Oct 31, 2024

Your Environment

  • Prettier version: 3.3.2
  • node version: 20.11.1
  • package manager: [email protected]
  • IDE: Both VScode and Webstorm

Describe the bug

After setting up a new SvelteKit project, adding the plugin (@ianvs/prettier-plugin-sort-imports), there's an error formatting some TS files:

[error] src/routes/sverdle/+page.server.ts: TypeError: Cannot read properties of undefined (reading 'push')
[error]     at getSortedNodesByImportOrder (/Users/gyen/CODING/GitHub-Issue-Reproductions/import-sort-issue/node_modules/@ianvs/prettier-plugin-sort-imports/lib/src/utils/get-sorted-nodes-by-import-order.js:37:41)
[error]     at getSortedNodes (/Users/gyen/CODING/GitHub-Issue-Reproductions/import-sort-issue/node_modules/@ianvs/prettier-plugin-sort-imports/lib/src/utils/get-sorted-nodes.js:66:95)
[error]     at preprocessor (/Users/gyen/CODING/GitHub-Issue-Reproductions/import-sort-issue/node_modules/@ianvs/prettier-plugin-sort-imports/lib/src/preprocessors/preprocessor.js:44:65)
[error]     at Object.defaultPreprocessor [as preprocess] (/Users/gyen/CODING/GitHub-Issue-Reproductions/import-sort-issue/node_modules/@ianvs/prettier-plugin-sort-imports/lib/src/preprocessors/default.js:8:44)
[error]     at parse4 (file:///Users/gyen/CODING/GitHub-Issue-Reproductions/import-sort-issue/node_modules/prettier/index.mjs:20681:43)
[error]     at async coreFormat (file:///Users/gyen/CODING/GitHub-Issue-Reproductions/import-sort-issue/node_modules/prettier/index.mjs:21146:7)
[error]     at async formatWithCursor (file:///Users/gyen/CODING/GitHub-Issue-Reproductions/import-sort-issue/node_modules/prettier/index.mjs:21348:14)
[error]     at async formatFiles (file:///Users/gyen/CODING/GitHub-Issue-Reproductions/import-sort-issue/node_modules/prettier/internal/cli.mjs:3468:18)
[error]     at async main (file:///Users/gyen/CODING/GitHub-Issue-Reproductions/import-sort-issue/node_modules/prettier/internal/cli.mjs:3876:5)
[error]     at async Module.run (file:///Users/gyen/CODING/GitHub-Issue-Reproductions/import-sort-issue/node_modules/prettier/internal/cli.mjs:3822:5)
src/routes/sverdle/+page.svelteTypeError: Cannot read properties of undefined (reading 'push')
    at getSortedNodesByImportOrder (/Users/gyen/CODING/GitHub-Issue-Reproductions/import-sort-issue/node_modules/@ianvs/prettier-plugin-sort-imports/lib/src/utils/get-sorted-nodes-by-import-order.js:37:41)
    at getSortedNodes (/Users/gyen/CODING/GitHub-Issue-Reproductions/import-sort-issue/node_modules/@ianvs/prettier-plugin-sort-imports/lib/src/utils/get-sorted-nodes.js:66:95)
    at preprocessor (/Users/gyen/CODING/GitHub-Issue-Reproductions/import-sort-issue/node_modules/@ianvs/prettier-plugin-sort-imports/lib/src/preprocessors/preprocessor.js:44:65)
    at Object.defaultPreprocessor [as preprocess] (/Users/gyen/CODING/GitHub-Issue-Reproductions/import-sort-issue/node_modules/@ianvs/prettier-plugin-sort-imports/lib/src/preprocessors/default.js:8:44)
    at parse4 (file:///Users/gyen/CODING/GitHub-Issue-Reproductions/import-sort-issue/node_modules/prettier/index.mjs:20681:43)
    at async textToDoc (file:///Users/gyen/CODING/GitHub-Issue-Reproductions/import-sort-issue/node_modules/prettier/index.mjs:20792:19)

To Reproduce

  1. Clone this repo — gyenabubakar/prettier-import-sort-issue-repro
  2. Install packages — npm install.
  3. Format codebase — npm run format

Expected behavior

Should format the non-ignored files in the codebase without errors.

Configuration File (cat .prettierrc, prettier.config.js, .prettier.js)

{
	"useTabs": true,
	"singleQuote": true,
	"trailingComma": "none",
	"printWidth": 100,
	"plugins": ["prettier-plugin-svelte", "@ianvs/prettier-plugin-sort-imports"],
	"overrides": [
		{
			"files": "*.svelte",
			"options": {
				"parser": "svelte"
			}
		}
	],
	"importOrder": [
		"^svelte(/.+)?",
		"^@sveltejs/.+",
		"^@melt-ui/.+",
		"<THIRD_PARTY_MODULES>",
		"<TYPES>^svelte/?",
		"<TYPES>^@sveltejs/.+",
		"<TYPES>^@melt-ui/.+",
		"<TYPES><THIRD_PARTY_MODULES>",
		"",
		"^utils$",
		"^ui/.+",
		"^[.]",
		"<TYPES>^utils$",
		"<TYPES>^ui/.+",
		""
	]
}

Contribute to @ianvs/prettier-plugin-sort-imports

  • I'm willing to fix this bug 🥇
@IanVS
Copy link
Owner

IanVS commented Oct 31, 2024

Thanks for the reproduction, that was very helpful. The problem here is that you did not define a sort order for a plain old <TYPES>, and the file causing issues as a type import from './$types' which doesn't match any of your other type import order regexes. Adding "<TYPES>" somewhere in your importOrder will fix the issue.

That said, I'm going to keep this issue open because we should either do some default sorting in this case or (my preference) throw a better error message.

Let me know if you hit any other issues!

@IanVS IanVS changed the title Error formatting a Svelte codebase Confusing error thrown when no matching importOrder is found Oct 31, 2024
@gyenabubakar
Copy link
Author

@IanVS that indeed was the problem. Thanks for the fix!

@IanVS
Copy link
Owner

IanVS commented Oct 31, 2024

Also note that <TYPES><THIRD_PARTY_MODULES> will not do what you want, but see https://github.com/IanVS/prettier-plugin-sort-imports#4-group-type-imports-separately-from-values for an example of a way to achieve something similar (basically grouping local imports, ie anything starting with a ., separately from the rest).

@IanVS IanVS linked a pull request Oct 31, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants