Skip to content
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
5 changes: 3 additions & 2 deletions examples/.prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
"tabWidth": 4,
"trailingComma": "all",
"singleQuote": true,
"jsxBracketSameLine": true,
"bracketSameLine": true,
"semi": true,
"importOrder": ["^@server/(.*)$", "^@core/(.*)$", "^@ui/(.*)$", "^[./]"],
"importOrderSeparation": true,
"importOrderSortSpecifiers": true
"importOrderSortSpecifiers": true,
"plugins": ["../lib/src/index.js"]
}
48 changes: 48 additions & 0 deletions examples/example.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<script>
import { onMount } from 'svelte';
// I am top level comment in this file.
// I am second line of top level comment in this file.
import React from 'react';
import thirdParty from 'third-party';

import something from '@server/something';


import component from '@ui/hello';

import fourLevelRelativePath from '../../../../fourLevelRelativePath';
import threeLevelRelativePath from '../../../threeLevelRelativePath';
import twoLevelRelativePath from '../../twoLevelRelativePath';
import oneLevelRelativePath from '../oneLevelRelativePath';
import sameLevelRelativePath from './sameLevelRelativePath';

import otherthing from '@core/otherthing';
let count = 0;

function increment() {
count += 1;
}
</script>

<main>
<h1>Hello Svelte!</h1>
<p>The count is {count}</p>
<button on:click={increment}>Increment</button>
</main>

<style>
main {
text-align: center;
padding: 1em;
max-width: 240px;
margin: 0 auto;
}

h1 {
color: #ff3e00;
}

button {
font-size: 1.2em;
}
</style>
10 changes: 6 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import { parsers as typescriptParsers } from 'prettier/plugins/typescript';
import { defaultPreprocessor } from './preprocessors/default-processor';
import { sveltePreprocessor } from './preprocessors/svelte-preprocessor';
import { vuePreprocessor } from './preprocessors/vue-preprocessor';
import type { Options } from 'prettier';
import { createSvelteParsers } from './utils/create-svelte-parsers';

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

const options = {
const options: Options = {
importOrder: {
type: 'path',
category: 'Global',
Expand Down Expand Up @@ -62,7 +64,7 @@ const options = {
category: 'Global',
default: 'with',
description: 'Provide a keyword for import attributes',
}
},
};

module.exports = {
Expand All @@ -84,7 +86,7 @@ module.exports = {
preprocess: vuePreprocessor,
},
svelte: {
...svelteParsers.svelte,
...svelteParsers.parsers.svelte,
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The only branch of createSvelteParsers where parsers is defined is the successful try case.

This will cause a type error for anyone without prettier-plugin-svelte.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The fix is coming soon and I should never release anything on a Friday!

preprocess: sveltePreprocessor,
},
},
Expand Down
8 changes: 8 additions & 0 deletions src/utils/create-svelte-parsers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function createSvelteParsers() {
try {
var { parsers } = require('prettier-plugin-svelte');
} catch {
return {};
}
return { parsers };
}