Skip to content

Commit 8418438

Browse files
feat: support handling .svelte files (#950)
Co-authored-by: JounQin <[email protected]>
1 parent 689f67a commit 8418438

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

.changeset/fair-foxes-brake.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"prettier-eslint": minor
3+
---
4+
5+
feat: support handling `.svelte` files

package.json

+16-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@
2020
"JounQin (https://www.1stG.me) <[email protected]>"
2121
],
2222
"license": "MIT",
23+
"peerDependencies": {
24+
"prettier-plugin-svelte": "^3.0.0",
25+
"svelte-eslint-parser": "*"
26+
},
27+
"peerDependenciesMeta": {
28+
"prettier-plugin-svelte": {
29+
"optional": true
30+
},
31+
"svelte-eslint-parser": {
32+
"optional": true
33+
}
34+
},
2335
"dependencies": {
2436
"@typescript-eslint/parser": "^6.7.5",
2537
"common-tags": "^1.4.0",
@@ -49,8 +61,11 @@
4961
"nps": "^5.7.1",
5062
"nps-utils": "^1.3.0",
5163
"prettier-eslint-cli": "^8.0.0",
64+
"prettier-plugin-svelte": "^3.1.2",
5265
"rimraf": "^5.0.5",
53-
"strip-indent": "^3.0.0"
66+
"strip-indent": "^3.0.0",
67+
"svelte": "^4.2.9",
68+
"svelte-eslint-parser": "^0.33.1"
5469
},
5570
"engines": {
5671
"node": ">=16.10.0"

src/__tests__/index.js

+13
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,19 @@ const tests = [
193193
output:
194194
'<template>\n <div></div>\n</template>\n<script>\nfunction foo () {\n return "foo";\n}\n</script>\n<style></style>'
195195
},
196+
{
197+
title: 'Svelte example',
198+
input: {
199+
prettierOptions: {
200+
plugins: ['prettier-plugin-svelte'],
201+
overrides: [{ files: '*.svelte', options: { parser: 'svelte' } }]
202+
},
203+
text: '<script>\nfunction foo() { return "foo" }\n</script>\n <div>test</div>\n<style>\n</style>',
204+
filePath: path.resolve('./test.svelte')
205+
},
206+
output:
207+
'<script>\n function foo() {\n return "foo";\n }\n</script>\n\n<div>test</div>\n\n<style>\n</style>'
208+
},
196209
{
197210
title: 'GraphQL example',
198211
input: {

src/index.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ async function format(options) {
5656
* `r.output` is the formatted string and `r.messages` is an array of
5757
* message specifications from eslint.
5858
*/
59+
// eslint-disable-next-line complexity
5960
async function analyze(options) {
6061
const { logLevel = getDefaultLogLevel() } = options;
6162
logger.setLevel(logLevel);
@@ -112,7 +113,8 @@ async function analyze(options) {
112113
'.ts',
113114
'.tsx',
114115
'.mjs',
115-
'.vue'
116+
'.vue',
117+
'.svelte'
116118
];
117119
const fileExtension = path.extname(filePath || '');
118120

@@ -138,6 +140,10 @@ async function analyze(options) {
138140
formattingOptions.eslint.parser ||= require.resolve('vue-eslint-parser');
139141
}
140142

143+
if (['.svelte'].includes(fileExtension)) {
144+
formattingOptions.eslint.parser ||= require.resolve('svelte-eslint-parser');
145+
}
146+
141147
const eslintFix = await createEslintFix(formattingOptions.eslint, eslintPath);
142148

143149
if (prettierLast) {

0 commit comments

Comments
 (0)