-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a554662
commit d7bdc8c
Showing
11 changed files
with
1,282 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.