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

feat: support handling .svelte files #950

Merged
merged 9 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
21 changes: 20 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,27 @@
"nps": "^5.7.1",
"nps-utils": "^1.3.0",
"prettier-eslint-cli": "^8.0.0",
"prettier-plugin-svelte": "^3.1.2",
"rimraf": "^5.0.5",
"strip-indent": "^3.0.0"
"strip-indent": "^3.0.0",
"svelte": "^4.2.9",
"svelte-eslint-parser": "^0.33.1"
},
"peerDependencies": {
"prettier-plugin-svelte": "^3.1.2",
robertheessels marked this conversation as resolved.
Show resolved Hide resolved
"svelte": "^4.2.9",
JounQin marked this conversation as resolved.
Show resolved Hide resolved
"svelte-eslint-parser": "^0.33.1"
robertheessels marked this conversation as resolved.
Show resolved Hide resolved
},
"peerDependenciesMeta": {
"prettier-plugin-svelte": {
"optional": true
},
"svelte": {
"optional": true
},
"svelte-eslint-parser": {
"optional": true
}
},
"engines": {
"node": ">=16.10.0"
Expand Down
13 changes: 13 additions & 0 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,19 @@ const tests = [
output:
'<template>\n <div></div>\n</template>\n<script>\nfunction foo () {\n return "foo";\n}\n</script>\n<style></style>'
},
{
title: 'Svelte example',
input: {
prettierOptions: {
plugins: ['prettier-plugin-svelte'],
overrides: [{ files: '*.svelte', options: { parser: 'svelte' } }]
},
text: '<script>\nfunction foo() { return "foo" }\n</script>\n <div>test</div>\n<style>\n</style>',
filePath: path.resolve('./test.svelte')
},
output:
'<script>\n function foo() {\n return "foo";\n }\n</script>\n\n<div>test</div>\n\n<style>\n</style>'
},
{
title: 'GraphQL example',
input: {
Expand Down
8 changes: 7 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ async function format(options) {
* `r.output` is the formatted string and `r.messages` is an array of
* message specifications from eslint.
*/
// eslint-disable-next-line complexity
async function analyze(options) {
const { logLevel = getDefaultLogLevel() } = options;
logger.setLevel(logLevel);
Expand Down Expand Up @@ -112,7 +113,8 @@ async function analyze(options) {
'.ts',
'.tsx',
'.mjs',
'.vue'
'.vue',
'.svelte'
];
const fileExtension = path.extname(filePath || '');

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

if (['.svelte'].includes(fileExtension)) {
formattingOptions.eslint.parser ||= require.resolve('svelte-eslint-parser');
}

const eslintFix = await createEslintFix(formattingOptions.eslint, eslintPath);

if (prettierLast) {
Expand Down