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

Svelte import sort #284

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,14 @@ Having some trouble or an issue ? You can check [FAQ / Troubleshooting section](

### Compatibility

| Framework | Supported | Note |
| ---------------------- | ------------------------ | ------------------------------------------------ |
| JS with ES Modules | ✅ Everything | - |
| NodeJS with ES Modules | ✅ Everything | - |
| React | ✅ Everything | - |
| Angular | ✅ Everything | Supported through `importOrderParserPlugins` API |
| Vue | ✅ Everything | `@vue/compiler-sfc` is required |
| Svelte | ⚠️ Soon to be supported. | Any contribution is welcome. |
| Framework | Supported | Note |
| ---------------------- | ------------- | ------------------------------------------------ |
| JS with ES Modules | ✅ Everything | - |
| NodeJS with ES Modules | ✅ Everything | - |
| React | ✅ Everything | - |
| Angular | ✅ Everything | Supported through `importOrderParserPlugins` API |
| Vue | ✅ Everything | `@vue/compiler-sfc` is required |
| Svelte | ✅ Everything | `prettier-plugin-svelte` is required |

### Used by

Expand Down
14 changes: 13 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,29 @@
"@types/node": "20.8.6",
"@vue/compiler-sfc": "^3.2.41",
"jest": "26.6.3",
"patch-package": "^8.0.0",
"postinstall-postinstall": "^2.1.0",
"prettier": "2.8",
"prettier-plugin-svelte": "2",
"svelte": "^4.2.10",
"ts-jest": "26.5.3",
"typescript": "4.9.4"
},
"peerDependencies": {
"@vue/compiler-sfc": "3.x",
"prettier": "2.x - 3.x"
"prettier": "2.x - 3.x",
"prettier-plugin-svelte": "2.x",
"svelte": "4.x"
},
"peerDependenciesMeta": {
"@vue/compiler-sfc": {
"optional": true
},
"prettier-plugin-svelte": {
"optional": true
},
"svelte": {
"optional": true
}
}
}
13 changes: 13 additions & 0 deletions patches/prettier-plugin-svelte+2.10.1.patch
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reason behind this is jest failing to require an ESM module.
This patch is only applied to dev install, since prettier-plugin-svelte is dev dependency.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
diff --git a/node_modules/prettier-plugin-svelte/plugin.js b/node_modules/prettier-plugin-svelte/plugin.js
index bcb248e..9d4ad94 100644
--- a/node_modules/prettier-plugin-svelte/plugin.js
+++ b/node_modules/prettier-plugin-svelte/plugin.js
@@ -2120,7 +2120,7 @@ const parsers = {
hasPragma,
parse: (text) => {
try {
- return Object.assign(Object.assign({}, require(`svelte/compiler`).parse(text)), { __isRoot: true });
+ return Object.assign(Object.assign({}, require(`svelte/compiler.cjs`).parse(text)), { __isRoot: true });
}
catch (err) {
if (err.start != null && err.end != null) {
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import { parsers as htmlParsers } from 'prettier/parser-html';
import { parsers as typescriptParsers } from 'prettier/parser-typescript';

import { defaultPreprocessor } from './preprocessors/default-processor';
import { sveltePreprocessor } from './preprocessors/svelte-preprocessor';
import { vuePreprocessor } from './preprocessors/vue-preprocessor';

const { parsers: svelteParsers } = require('prettier-plugin-svelte');
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prettier-plugin-svelte@2 works with prettier@2 and has no types. This require is used to skip typing the module.
prettier-plugin-svelte@3 has typings.


const options = {
importOrder: {
type: 'path',
Expand Down Expand Up @@ -68,6 +71,10 @@ module.exports = {
...htmlParsers.vue,
preprocess: vuePreprocessor,
},
svelte: {
...svelteParsers.svelte,
preprocess: sveltePreprocessor,
},
},
options,
};
4 changes: 3 additions & 1 deletion src/preprocessors/default-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { PrettierOptions } from '../types';
import { preprocessor } from './preprocessor';

export function defaultPreprocessor(code: string, options: PrettierOptions) {
if (options.filepath?.endsWith('.vue')) return code;
for (const extension of ['svelte', 'vue']) {
if (options.filepath?.endsWith(`.${extension}`)) return code;
}
return preprocessor(code, options);
}
24 changes: 24 additions & 0 deletions src/preprocessors/svelte-preprocessor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { PrettierOptions } from '../types';
import { preprocessor } from './preprocessor';

const booleanGuard = <T>(value: T | undefined): value is T => Boolean(value);

const sortImports = (code: string, options: PrettierOptions) => {
const { parse } = require('svelte/compiler.cjs');
const { instance, module } = parse(code);
const sources = [instance, module].filter(booleanGuard);
if (!sources.length) return code;
return sources.reduce((code, source) => {
const snippet = code.slice(source.content.start, source.content.end);
const preprocessed = preprocessor(snippet, options);
const result = code.replace(snippet, `\n${preprocessed}\n`);
return result;
}, code);
};

export function sveltePreprocessor(code: string, options: PrettierOptions) {
const sorted = sortImports(code, options);

const prettierPluginSvelte = require('prettier-plugin-svelte');
return prettierPluginSvelte.parsers.svelte.preprocess(sorted, options);
}