Skip to content

Commit

Permalink
(feat) support const tag
Browse files Browse the repository at this point in the history
Fixes sveltejs#272

Also update package-lock.json for npm 8
  • Loading branch information
Simon committed Jan 12, 2022
1 parent 0473f02 commit 27bc40c
Show file tree
Hide file tree
Showing 10 changed files with 3,798 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# prettier-plugin-svelte changelog

## 2.6.0 (Unreleased)

* (feat) Support `@const` tag ([#272](https://github.com/sveltejs/prettier-plugin-svelte/issues/272))

## 2.5.1

* (fix) Better handling of destructured values with defaults in `{#each}` and `{#await}` blocks ([#269](https://github.com/sveltejs/prettier-plugin-svelte/issues/269))
Expand Down
3,728 changes: 3,713 additions & 15 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"prettier": "^2.4.1",
"rollup": "2.36.0",
"rollup-plugin-typescript": "1.0.1",
"svelte": "^3.38.2",
"svelte": "^3.46.0",
"ts-node": "^9.1.1",
"tslib": "^2.0.3",
"typescript": "4.1.3"
Expand Down
12 changes: 9 additions & 3 deletions src/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Doc, doc, FastPath, ParserOptions } from 'prettier';
import { getText } from './lib/getText';
import { snippedTagContentAttribute } from './lib/snipTagContent';
import { PrintFn } from './print';
import { isLine, trimRight } from './print/doc-helpers';
import { isLine, removeParentheses, trimRight } from './print/doc-helpers';
import {
getAttributeTextValue,
getLeadingComment,
Expand Down Expand Up @@ -36,8 +36,14 @@ export function embed(
embeddedOptions.singleQuote = true;
}

const docs = textToDoc(forceIntoExpression(getText(node, options)), embeddedOptions);
return node.forceSingleLine ? removeLines(docs) : docs;
let docs = textToDoc(forceIntoExpression(getText(node, options)), embeddedOptions);
if (node.forceSingleLine) {
docs = removeLines(docs);
}
if (node.removeParentheses) {
docs = removeParentheses(docs);
}
return docs;
} catch (e) {
return getText(node, options);
}
Expand Down
7 changes: 7 additions & 0 deletions src/print/doc-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,10 @@ function getParts(doc: Doc): Doc[] | undefined {
}
}
}

/**
* `(foo = bar)` => `foo = bar`
*/
export function removeParentheses(doc: Doc): Doc {
return trim([doc], (_doc: Doc) => _doc === '(' || _doc === ')')[0];
}
34 changes: 29 additions & 5 deletions src/print/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
const [open, close] = options.svelteStrictMode ? ['"{', '}"'] : ['{', '}'];
const printJsExpression = () => [
open,
printJS(path, print, options.svelteStrictMode, false, 'expression'),
printJS(path, print, options.svelteStrictMode, false, false, 'expression'),
close,
];
const node = n as Node;
Expand Down Expand Up @@ -399,7 +399,14 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
case 'MustacheTag':
return concat([
'{',
printJS(path, print, isInsideQuotedAttribute(path, options), false, 'expression'),
printJS(
path,
print,
isInsideQuotedAttribute(path, options),
false,
false,
'expression',
),
'}',
]);
case 'IfBlock': {
Expand Down Expand Up @@ -637,9 +644,24 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
node.expression ? concat(['=', ...printJsExpression()]) : '',
]);
case 'RawMustacheTag':
return concat(['{@html ', printJS(path, print, false, false, 'expression'), '}']);
return concat([
'{@html ',
printJS(path, print, false, false, false, 'expression'),
'}',
]);
case 'Spread':
return concat([line, '{...', printJS(path, print, false, false, 'expression'), '}']);
return concat([
line,
'{...',
printJS(path, print, false, false, false, 'expression'),
'}',
]);
case 'ConstTag':
return concat([
'{@const ',
printJS(path, print, false, false, true, 'expression'),
'}',
]);
}

console.error(JSON.stringify(node, null, 4));
Expand Down Expand Up @@ -1051,19 +1073,21 @@ function splitTextToDocs(node: TextNode): Doc[] {
}

function printSvelteBlockJS(path: FastPath, print: PrintFn, name: string) {
return printJS(path, print, false, true, name);
return printJS(path, print, false, true, false, name);
}

function printJS(
path: FastPath,
print: PrintFn,
forceSingleQuote: boolean,
forceSingleLine: boolean,
removeParentheses: boolean,
name: string,
) {
path.getValue()[name].isJS = true;
path.getValue()[name].forceSingleQuote = forceSingleQuote;
path.getValue()[name].forceSingleLine = forceSingleLine;
path.getValue()[name].removeParentheses = removeParentheses;
return path.call(print, name);
}

Expand Down
9 changes: 8 additions & 1 deletion src/print/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface BaseNode {
isJS?: boolean;
forceSingleQuote?: boolean;
forceSingleLine?: boolean;
removeParentheses?: boolean;
}

export interface FragmentNode extends BaseNode {
Expand Down Expand Up @@ -260,6 +261,11 @@ export interface SlotTemplateNode extends BaseNode {
children: Node[];
}

export interface ConstTagNode extends BaseNode {
type: 'ConstTag';
expression: Node;
}

export type Node =
| FragmentNode
| ElementNode
Expand Down Expand Up @@ -301,7 +307,8 @@ export type Node =
| ModuleScriptNode
| BodyNode
| OptionsNode
| SlotTemplateNode;
| SlotTemplateNode
| ConstTagNode;

/**
* The Svelte AST root node
Expand Down
9 changes: 9 additions & 0 deletions test/formatting/samples/const/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{#each [1, 2] as foo}
{@const bar =
foo}
{foo}{bar}
{/each}

{#await aPromise then result}
{@const bar = result ? 'some super long text which will force the ternary to break' : 'etc etc'}
{/await}
10 changes: 10 additions & 0 deletions test/formatting/samples/const/output.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{#each [1, 2] as foo}
{@const bar = foo}
{foo}{bar}
{/each}

{#await aPromise then result}
{@const bar = result
? "some super long text which will force the ternary to break"
: "etc etc"}
{/await}
8 changes: 8 additions & 0 deletions test/printer/samples/const.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{#each [1, 2] as foo}
{@const bar = foo}
{foo}{bar}
{/each}

{#await aPromise then result}
{@const bar = result}
{/await}

0 comments on commit 27bc40c

Please sign in to comment.