-
Notifications
You must be signed in to change notification settings - Fork 121
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
Svelte import sort #284
Closed
Closed
Svelte import sort #284
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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) { |
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 |
---|---|---|
|
@@ -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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
const options = { | ||
importOrder: { | ||
type: 'path', | ||
|
@@ -68,6 +71,10 @@ module.exports = { | |
...htmlParsers.vue, | ||
preprocess: vuePreprocessor, | ||
}, | ||
svelte: { | ||
...svelteParsers.svelte, | ||
preprocess: sveltePreprocessor, | ||
}, | ||
}, | ||
options, | ||
}; |
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reason behind this is jest failing to require an ESM module.
This patch is only applied to dev install, since
prettier-plugin-svelte
is dev dependency.