Skip to content

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
merged 31 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
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 Mar 14, 2023
830f5a8
Bump ansi-regex from 4.1.0 to 4.1.1 (#286)
dependabot[bot] Mar 15, 2023
caa47ca
Bump trim-off-newlines from 1.0.1 to 1.0.3 (#287)
dependabot[bot] Mar 15, 2023
34999ca
Bump minimatch from 3.0.4 to 3.1.2 (#327)
dependabot[bot] Mar 15, 2023
c4490bd
Bump http-cache-semantics from 4.1.0 to 4.1.1 (#342)
dependabot[bot] Mar 15, 2023
2e9599b
Bump minimist from 1.2.5 to 1.2.8 (#351)
dependabot[bot] Mar 15, 2023
2fbd812
feat: support insert/require pragma options (#354)
dummdidumm Mar 21, 2023
7abac5f
feat: support `svelte:document` (#355)
dummdidumm Mar 21, 2023
a94563c
(feat) trim whitespace in `class` attributes (#356)
dummdidumm Mar 22, 2023
4ce3dd3
fix: refine attributeRegex (#345)
Rolaka Mar 22, 2023
b7d0562
feat: allow multiple comments atop of script/style tags (#357)
dummdidumm Mar 22, 2023
bd91bbf
chore: release 2.10.0 (#358)
dummdidumm Mar 22, 2023
fdb31a7
Update Prettier to v3.0.0-alpha.11
carmanchris31 Apr 30, 2023
43d1e06
Remove incompatible @prettier/plugin-pug
carmanchris31 Apr 30, 2023
afc48fc
Remove unsupported since property
carmanchris31 Apr 30, 2023
51a7030
Update splitTextToDocs
carmanchris31 Apr 30, 2023
1ab5ec5
Remove unused concat import
carmanchris31 Apr 30, 2023
c361d6a
Bypass call type issue
carmanchris31 Apr 30, 2023
b4a707a
Fix ParserOptions
carmanchris31 Apr 30, 2023
c3af343
Await async APIs
carmanchris31 Apr 30, 2023
0f278aa
Remove unnecessary any
carmanchris31 Apr 30, 2023
169e0b4
Fix importing Svelte plugin
carmanchris31 Apr 30, 2023
a6f32ad
Enable better errors when tests fail
carmanchris31 Apr 30, 2023
ddf961b
Fix TypeError
carmanchris31 Apr 30, 2023
8530863
Use newer actions
carmanchris31 May 1, 2023
97f1d17
chore: mark as compatible with Svelte 4 (#367)
dummdidumm May 27, 2023
9dcc594
docs: update README.md with information for using prettier-plugin-tai…
opensas Jul 5, 2023
bad0d7f
Merge branch 'master' into chore/upgrade-prettier-v3
dummdidumm Jul 14, 2023
7b7a668
get tests passing
dummdidumm Jul 14, 2023
4975e49
cleanup
dummdidumm Jul 14, 2023
fc26baf
package bumps
dummdidumm Jul 14, 2023
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
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
cache: npm

Expand Down
276 changes: 10 additions & 266 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
],
"scripts": {
"build": "rollup -c",
"test": "ava",
"test": "PRETTIER_DEBUG=1 ava",
"prepare": "npm run build",
"prepublishOnly": "npm test"
},
Expand All @@ -28,13 +28,11 @@
},
"homepage": "https://github.com/sveltejs/prettier-plugin-svelte#readme",
"devDependencies": {
"@prettier/plugin-pug": "^1.16.0",
"@rollup/plugin-commonjs": "14.0.0",
"@rollup/plugin-node-resolve": "11.0.1",
"@types/node": "^10.12.18",
"@types/prettier": "^2.4.1",
"ava": "3.15.0",
"prettier": "^2.7.1",
"prettier": "^3.0.0-alpha.11",
Copy link
Member

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

"rollup": "2.36.0",
"rollup-plugin-typescript": "1.0.1",
"svelte": "^3.54.0",
Expand All @@ -43,7 +41,7 @@
"typescript": "4.1.3"
},
"peerDependencies": {
"prettier": "^1.16.4 || ^2.0.0",
"prettier": "^3.0.0-alpha.11",
"svelte": "^3.2.0"
}
}
167 changes: 86 additions & 81 deletions src/embed.ts
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';
Expand All @@ -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,
Copy link
Member

Choose a reason for hiding this comment

The 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 script block. the comment on isTypeScript can be updated as well

// 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) {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -204,7 +209,7 @@ function embedTag(
));
const body: Doc = canFormat
? content.trim() !== ''
? formatBodyContent(content)
? await formatBodyContent(content)
: content === ''
? ''
: hardline
Expand Down
2 changes: 1 addition & 1 deletion src/lib/getText.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ParserOptions } from 'prettier';
import { ParserOptions } from '../options';
import { Node } from '../print/nodes';
import { hasSnippedContent, unsnipContent } from './snipTagContent';

Expand Down
12 changes: 3 additions & 9 deletions src/options.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ParserOptions, SupportOption } from 'prettier';
import { ParserOptions as PrettierParserOptions, SupportOption } from 'prettier';

declare module 'prettier' {
interface RequiredOptions extends PluginOptions {}
export interface ParserOptions<T = any> extends PrettierParserOptions<T>, Partial<PluginOptions> {
}

export interface PluginOptions {
Expand All @@ -18,7 +17,6 @@ function makeChoice(choice: string) {

export const options: Record<keyof PluginOptions, SupportOption> = {
svelteSortOrder: {
since: '0.6.0',
category: 'Svelte',
type: 'choice',
default: 'options-scripts-markup-styles',
Expand Down Expand Up @@ -59,29 +57,25 @@ export const options: Record<keyof PluginOptions, SupportOption> = {
],
},
svelteStrictMode: {
since: '0.7.0',
category: 'Svelte',
type: 'boolean',
default: false,
description: 'More strict HTML syntax: self-closed tags, quotes in attributes',
},
svelteBracketNewLine: {
since: '0.6.0',
category: 'Svelte',
type: 'boolean',
description: 'Put the `>` of a multiline element on a new line',
deprecated: '2.5.0',
},
svelteAllowShorthand: {
since: '1.0.0',
category: 'Svelte',
type: 'boolean',
default: true,
description:
'Option to enable/disable component attribute shorthand if attribute name and expressions are same',
},
svelteIndentScriptAndStyle: {
since: '1.2.0',
category: 'Svelte',
type: 'boolean',
default: true,
Expand Down Expand Up @@ -130,7 +124,7 @@ export type SortOrderPart = 'scripts' | 'markup' | 'styles' | 'options';

const sortOrderSeparator = '-';

export function parseSortOrder(sortOrder: SortOrder): SortOrderPart[] {
export function parseSortOrder(sortOrder: SortOrder = 'options-scripts-markup-styles'): SortOrderPart[] {
if (sortOrder === 'none') {
return [];
}
Expand Down
3 changes: 2 additions & 1 deletion src/print/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Doc, doc, FastPath, ParserOptions } from 'prettier';
import { Doc, doc, FastPath } from 'prettier';
import { PrintFn } from '.';
import { formattableAttributes } from '../lib/elements';
import { snippedTagContentAttribute } from '../lib/snipTagContent';
Expand All @@ -18,6 +18,7 @@ import {
TitleNode,
WindowNode
} from './nodes';
import { ParserOptions } from '../options';

/**
* Determines whether or not given node
Expand Down
Loading