Skip to content

Commit

Permalink
Implement svelte import sort
Browse files Browse the repository at this point in the history
  • Loading branch information
secondfry committed Feb 28, 2024
1 parent 61d0697 commit cd14db2
Show file tree
Hide file tree
Showing 10 changed files with 920 additions and 7 deletions.
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
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');

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);
}
Loading

0 comments on commit cd14db2

Please sign in to comment.