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 #310

Merged
merged 8 commits into from
Nov 25, 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
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,5 @@ jobs:
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- run: npm i -g yarn
- run: yarn set version stable
- run: yarn --immutable
- run: yarn test
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ Having some trouble or an issue ? You can check [FAQ / Troubleshooting section](
| React | ✅ Everything | - |
| Angular | ✅ Everything | Supported through `importOrderParserPlugins` API |
| Vue | ✅ Everything | `@vue/compiler-sfc` is required |
| Svelte | ⚠️ Soon to be supported. | Any contribution is welcome. |
| Svelte | ✅ Everything | `prettier-plugin-svelte` is required |

### Used by

Expand Down
23 changes: 18 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"example": "prettier --config ./examples/.prettierrc --plugin lib/src/index.js",
"test": "yarn node --experimental-vm-modules $(yarn bin jest)",
"type-check": "tsc --noEmit",
"prepublishOnly": "npm run compile && npm run test"
"prepublishOnly": "npm run compile && npm run test",
"postinstall": "patch-package"
},
"keywords": [
"prettier",
Expand All @@ -41,27 +42,39 @@
"lodash": "^4.17.21"
},
"devDependencies": {
"@babel/core": "7.26.0",
"@babel/core": "^7.26.0",
"@types/chai": "5.0.1",
"@types/jest": "29.5.14",
"@types/lodash": "4.17.13",
"@types/node": "22.9.0",
"@vue/compiler-sfc": "^3.5.12",
"@types/node": "22.9.1",
"@vue/compiler-sfc": "^3.5.13",
"jest": "29.7.0",
"patch-package": "^8.0.0",
"postinstall-postinstall": "^2.1.0",
"prettier": "3.3.3",
"prettier-plugin-svelte": "3.3.1",
"svelte": "^4.2.19",
"ts-jest": "29.2.5",
"typescript": "5.6.3"
},
"peerDependencies": {
"@vue/compiler-sfc": "3.x",
"prettier": "2.x - 3.x"
"prettier": "2.x - 3.x",
"prettier-plugin-svelte": "3.x",
"svelte": "4.x"
},
"engines": {
"node": ">18.12"
},
"peerDependenciesMeta": {
"@vue/compiler-sfc": {
"optional": true
},
"prettier-plugin-svelte": {
"optional": true
},
"svelte": {
"optional": true
}
}
}
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { parsers as htmlParsers } from 'prettier/plugins/html';
import { parsers as typescriptParsers } from 'prettier/plugins/typescript';
import { defaultPreprocessor } from './preprocessors/default-processor';
import { vuePreprocessor } from './preprocessors/vue-preprocessor';
import { sveltePreprocessor } from './preprocessors/svelte-preprocessor';

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

const options = {
importOrder: {
Expand Down Expand Up @@ -67,6 +70,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');
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);
}
Loading