-
Notifications
You must be signed in to change notification settings - Fork 98
Chore/upgrade prettier v3 #365
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
Merged
dummdidumm
merged 31 commits into
sveltejs:next
from
carmanchris31:chore/upgrade-prettier-v3
Jul 14, 2023
Merged
Changes from 13 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
5c088d8
docs: clarify CLI run
dummdidumm 830f5a8
Bump ansi-regex from 4.1.0 to 4.1.1 (#286)
dependabot[bot] caa47ca
Bump trim-off-newlines from 1.0.1 to 1.0.3 (#287)
dependabot[bot] 34999ca
Bump minimatch from 3.0.4 to 3.1.2 (#327)
dependabot[bot] c4490bd
Bump http-cache-semantics from 4.1.0 to 4.1.1 (#342)
dependabot[bot] 2e9599b
Bump minimist from 1.2.5 to 1.2.8 (#351)
dependabot[bot] 2fbd812
feat: support insert/require pragma options (#354)
dummdidumm 7abac5f
feat: support `svelte:document` (#355)
dummdidumm a94563c
(feat) trim whitespace in `class` attributes (#356)
dummdidumm 4ce3dd3
fix: refine attributeRegex (#345)
Rolaka b7d0562
feat: allow multiple comments atop of script/style tags (#357)
dummdidumm bd91bbf
chore: release 2.10.0 (#358)
dummdidumm fdb31a7
Update Prettier to v3.0.0-alpha.11
carmanchris31 43d1e06
Remove incompatible @prettier/plugin-pug
carmanchris31 afc48fc
Remove unsupported since property
carmanchris31 51a7030
Update splitTextToDocs
carmanchris31 1ab5ec5
Remove unused concat import
carmanchris31 c361d6a
Bypass call type issue
carmanchris31 b4a707a
Fix ParserOptions
carmanchris31 c3af343
Await async APIs
carmanchris31 0f278aa
Remove unnecessary any
carmanchris31 169e0b4
Fix importing Svelte plugin
carmanchris31 a6f32ad
Enable better errors when tests fail
carmanchris31 ddf961b
Fix TypeError
carmanchris31 8530863
Use newer actions
carmanchris31 97f1d17
chore: mark as compatible with Svelte 4 (#367)
dummdidumm 9dcc594
docs: update README.md with information for using prettier-plugin-tai…
opensas bad0d7f
Merge branch 'master' into chore/upgrade-prettier-v3
dummdidumm 7b7a668
get tests passing
dummdidumm 4975e49
cleanup
dummdidumm fc26baf
package bumps
dummdidumm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { Doc, doc, FastPath, ParserOptions } from 'prettier'; | ||
import { Doc, doc, FastPath, Options } from 'prettier'; | ||
import { getText } from './lib/getText'; | ||
import { snippedTagContentAttribute } from './lib/snipTagContent'; | ||
import { isBracketSameLine } from './options'; | ||
import { isBracketSameLine, ParserOptions } from './options'; | ||
import { PrintFn } from './print'; | ||
import { isLine, removeParentheses, trimRight } from './print/doc-helpers'; | ||
import { printWithPrependedAttributeLine } from './print/helpers'; | ||
|
@@ -23,86 +23,91 @@ const { | |
|
||
export function embed( | ||
path: FastPath, | ||
print: PrintFn, | ||
textToDoc: (text: string, options: object) => Doc, | ||
options: ParserOptions, | ||
): Doc | null { | ||
const node: Node = path.getNode(); | ||
|
||
if (node.isJS) { | ||
try { | ||
const embeddedOptions: any = { | ||
parser: expressionParser, | ||
}; | ||
if (node.forceSingleQuote) { | ||
embeddedOptions.singleQuote = true; | ||
} | ||
_options: Options, | ||
) { | ||
return async ( | ||
textToDoc: (text: string, options: Options) => Promise<Doc>, | ||
print: PrintFn, | ||
): Promise<Doc | undefined> => { | ||
const node: Node = path.getNode(); | ||
const options = _options as ParserOptions | ||
if (!options.locStart || !options.locEnd || !options.originalText) { | ||
throw new Error(`Missing required options`) | ||
} | ||
|
||
let docs = textToDoc( | ||
forceIntoExpression( | ||
// If we have snipped content, it was done wrongly and we need to unsnip it. | ||
// This happens for example for {@html `<script>{foo}</script>`} | ||
getText(node, options, true), | ||
), | ||
embeddedOptions, | ||
); | ||
if (node.forceSingleLine) { | ||
docs = removeLines(docs); | ||
} | ||
if (node.removeParentheses) { | ||
docs = removeParentheses(docs); | ||
if (node.isJS) { | ||
try { | ||
const embeddedOptions = { | ||
parser: expressionParser, | ||
singleQuote: node.forceSingleQuote ? true : undefined | ||
}; | ||
|
||
let docs = await textToDoc( | ||
forceIntoExpression( | ||
// If we have snipped content, it was done wrongly and we need to unsnip it. | ||
// This happens for example for {@html `<script>{foo}</script>`} | ||
getText(node, options, true), | ||
), | ||
embeddedOptions, | ||
); | ||
if (node.forceSingleLine) { | ||
docs = removeLines(docs); | ||
} | ||
if (node.removeParentheses) { | ||
docs = removeParentheses(docs); | ||
} | ||
return docs; | ||
} catch (e) { | ||
return getText(node, options, true); | ||
} | ||
return docs; | ||
} catch (e) { | ||
return getText(node, options, true); | ||
} | ||
} | ||
|
||
const embedType = ( | ||
tag: 'script' | 'style' | 'template', | ||
parser: 'typescript' | 'babel-ts' | 'css' | 'pug', | ||
isTopLevel: boolean, | ||
) => | ||
embedTag( | ||
tag, | ||
options.originalText, | ||
path, | ||
(content) => formatBodyContent(content, parser, textToDoc, options), | ||
print, | ||
isTopLevel, | ||
options, | ||
); | ||
|
||
const embedScript = (isTopLevel: boolean) => | ||
embedType( | ||
'script', | ||
// Use babel-ts as fallback because the absence does not mean the content is not TS, | ||
// the user could have set the default language. babel-ts will format things a little | ||
// bit different though, especially preserving parentheses around dot notation which | ||
// fixes https://github.com/sveltejs/prettier-plugin-svelte/issues/218 | ||
isTypeScript(node) ? 'typescript' : 'babel-ts', | ||
isTopLevel, | ||
); | ||
const embedStyle = (isTopLevel: boolean) => embedType('style', 'css', isTopLevel); | ||
const embedPug = () => embedType('template', 'pug', false); | ||
|
||
switch (node.type) { | ||
case 'Script': | ||
return embedScript(true); | ||
case 'Style': | ||
return embedStyle(true); | ||
case 'Element': { | ||
if (node.name === 'script') { | ||
return embedScript(false); | ||
} else if (node.name === 'style') { | ||
return embedStyle(false); | ||
} else if (isPugTemplate(node)) { | ||
return embedPug(); | ||
const embedType = ( | ||
tag: 'script' | 'style' | 'template', | ||
parser: 'typescript' | 'babel-ts' | 'css' | 'pug', | ||
isTopLevel: boolean, | ||
) => | ||
embedTag( | ||
tag, | ||
options.originalText, | ||
path, | ||
(content) => formatBodyContent(content, parser, textToDoc, options), | ||
print, | ||
isTopLevel, | ||
options, | ||
); | ||
|
||
const embedScript = (isTopLevel: boolean) => | ||
embedType( | ||
'script', | ||
// Use babel-ts as fallback because the absence does not mean the content is not TS, | ||
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. we no longer support default language. you must now specify it explicitly in the |
||
// the user could have set the default language. babel-ts will format things a little | ||
// bit different though, especially preserving parentheses around dot notation which | ||
// fixes https://github.com/sveltejs/prettier-plugin-svelte/issues/218 | ||
isTypeScript(node) ? 'typescript' : 'babel-ts', | ||
isTopLevel, | ||
); | ||
const embedStyle = (isTopLevel: boolean) => embedType('style', 'css', isTopLevel); | ||
const embedPug = () => embedType('template', 'pug', false); | ||
|
||
switch (node.type) { | ||
case 'Script': | ||
return embedScript(true); | ||
case 'Style': | ||
return embedStyle(true); | ||
case 'Element': { | ||
if (node.name === 'script') { | ||
return embedScript(false); | ||
} else if (node.name === 'style') { | ||
return embedStyle(false); | ||
} else if (isPugTemplate(node)) { | ||
return embedPug(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return null; | ||
return; | ||
} | ||
} | ||
|
||
function forceIntoExpression(statement: string) { | ||
|
@@ -136,14 +141,14 @@ function getSnippedContent(node: Node) { | |
} | ||
} | ||
|
||
function formatBodyContent( | ||
async function formatBodyContent( | ||
content: string, | ||
parser: 'typescript' | 'babel-ts' | 'css' | 'pug', | ||
textToDoc: (text: string, options: object) => Doc, | ||
textToDoc: (text: string, options: object) => Promise<Doc>, | ||
options: ParserOptions & { pugTabWidth?: number }, | ||
) { | ||
try { | ||
const body = textToDoc(content, { parser }); | ||
const body = await textToDoc(content, { parser }); | ||
|
||
if (parser === 'pug' && typeof body === 'string') { | ||
// Pug returns no docs but a final string. | ||
|
@@ -181,11 +186,11 @@ function formatBodyContent( | |
} | ||
} | ||
|
||
function embedTag( | ||
async function embedTag( | ||
tag: 'script' | 'style' | 'template', | ||
text: string, | ||
path: FastPath, | ||
formatBodyContent: (content: string) => Doc, | ||
formatBodyContent: (content: string) => Promise<Doc>, | ||
print: PrintFn, | ||
isTopLevel: boolean, | ||
options: ParserOptions, | ||
|
@@ -204,7 +209,7 @@ function embedTag( | |
)); | ||
const body: Doc = canFormat | ||
? content.trim() !== '' | ||
? formatBodyContent(content) | ||
? await formatBodyContent(content) | ||
: content === '' | ||
? '' | ||
: hardline | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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
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.
this can be updated to
^3.0.0
now