Skip to content

Commit d97d7f6

Browse files
authored
(feat) support const tag (#273)
Fixes #272 Also update package-lock.json for npm 8
1 parent 0473f02 commit d97d7f6

File tree

10 files changed

+3798
-25
lines changed

10 files changed

+3798
-25
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# prettier-plugin-svelte changelog
22

3+
## 2.6.0 (Unreleased)
4+
5+
* (feat) Support `@const` tag ([#272](https://github.com/sveltejs/prettier-plugin-svelte/issues/272))
6+
37
## 2.5.1
48

59
* (fix) Better handling of destructured values with defaults in `{#each}` and `{#await}` blocks ([#269](https://github.com/sveltejs/prettier-plugin-svelte/issues/269))

package-lock.json

Lines changed: 3713 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"prettier": "^2.4.1",
3838
"rollup": "2.36.0",
3939
"rollup-plugin-typescript": "1.0.1",
40-
"svelte": "^3.38.2",
40+
"svelte": "^3.46.0",
4141
"ts-node": "^9.1.1",
4242
"tslib": "^2.0.3",
4343
"typescript": "4.1.3"

src/embed.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Doc, doc, FastPath, ParserOptions } from 'prettier';
22
import { getText } from './lib/getText';
33
import { snippedTagContentAttribute } from './lib/snipTagContent';
44
import { PrintFn } from './print';
5-
import { isLine, trimRight } from './print/doc-helpers';
5+
import { isLine, removeParentheses, trimRight } from './print/doc-helpers';
66
import {
77
getAttributeTextValue,
88
getLeadingComment,
@@ -36,8 +36,14 @@ export function embed(
3636
embeddedOptions.singleQuote = true;
3737
}
3838

39-
const docs = textToDoc(forceIntoExpression(getText(node, options)), embeddedOptions);
40-
return node.forceSingleLine ? removeLines(docs) : docs;
39+
let docs = textToDoc(forceIntoExpression(getText(node, options)), embeddedOptions);
40+
if (node.forceSingleLine) {
41+
docs = removeLines(docs);
42+
}
43+
if (node.removeParentheses) {
44+
docs = removeParentheses(docs);
45+
}
46+
return docs;
4147
} catch (e) {
4248
return getText(node, options);
4349
}

src/print/doc-helpers.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,10 @@ function getParts(doc: Doc): Doc[] | undefined {
158158
}
159159
}
160160
}
161+
162+
/**
163+
* `(foo = bar)` => `foo = bar`
164+
*/
165+
export function removeParentheses(doc: Doc): Doc {
166+
return trim([doc], (_doc: Doc) => _doc === '(' || _doc === ')')[0];
167+
}

src/print/index.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
9696
const [open, close] = options.svelteStrictMode ? ['"{', '}"'] : ['{', '}'];
9797
const printJsExpression = () => [
9898
open,
99-
printJS(path, print, options.svelteStrictMode, false, 'expression'),
99+
printJS(path, print, options.svelteStrictMode, false, false, 'expression'),
100100
close,
101101
];
102102
const node = n as Node;
@@ -399,7 +399,14 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
399399
case 'MustacheTag':
400400
return concat([
401401
'{',
402-
printJS(path, print, isInsideQuotedAttribute(path, options), false, 'expression'),
402+
printJS(
403+
path,
404+
print,
405+
isInsideQuotedAttribute(path, options),
406+
false,
407+
false,
408+
'expression',
409+
),
403410
'}',
404411
]);
405412
case 'IfBlock': {
@@ -637,9 +644,24 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
637644
node.expression ? concat(['=', ...printJsExpression()]) : '',
638645
]);
639646
case 'RawMustacheTag':
640-
return concat(['{@html ', printJS(path, print, false, false, 'expression'), '}']);
647+
return concat([
648+
'{@html ',
649+
printJS(path, print, false, false, false, 'expression'),
650+
'}',
651+
]);
641652
case 'Spread':
642-
return concat([line, '{...', printJS(path, print, false, false, 'expression'), '}']);
653+
return concat([
654+
line,
655+
'{...',
656+
printJS(path, print, false, false, false, 'expression'),
657+
'}',
658+
]);
659+
case 'ConstTag':
660+
return concat([
661+
'{@const ',
662+
printJS(path, print, false, false, true, 'expression'),
663+
'}',
664+
]);
643665
}
644666

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

10531075
function printSvelteBlockJS(path: FastPath, print: PrintFn, name: string) {
1054-
return printJS(path, print, false, true, name);
1076+
return printJS(path, print, false, true, false, name);
10551077
}
10561078

10571079
function printJS(
10581080
path: FastPath,
10591081
print: PrintFn,
10601082
forceSingleQuote: boolean,
10611083
forceSingleLine: boolean,
1084+
removeParentheses: boolean,
10621085
name: string,
10631086
) {
10641087
path.getValue()[name].isJS = true;
10651088
path.getValue()[name].forceSingleQuote = forceSingleQuote;
10661089
path.getValue()[name].forceSingleLine = forceSingleLine;
1090+
path.getValue()[name].removeParentheses = removeParentheses;
10671091
return path.call(print, name);
10681092
}
10691093

src/print/nodes.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export interface BaseNode {
44
isJS?: boolean;
55
forceSingleQuote?: boolean;
66
forceSingleLine?: boolean;
7+
removeParentheses?: boolean;
78
}
89

910
export interface FragmentNode extends BaseNode {
@@ -260,6 +261,11 @@ export interface SlotTemplateNode extends BaseNode {
260261
children: Node[];
261262
}
262263

264+
export interface ConstTagNode extends BaseNode {
265+
type: 'ConstTag';
266+
expression: Node;
267+
}
268+
263269
export type Node =
264270
| FragmentNode
265271
| ElementNode
@@ -301,7 +307,8 @@ export type Node =
301307
| ModuleScriptNode
302308
| BodyNode
303309
| OptionsNode
304-
| SlotTemplateNode;
310+
| SlotTemplateNode
311+
| ConstTagNode;
305312

306313
/**
307314
* The Svelte AST root node
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{#each [1, 2] as foo}
2+
{@const bar =
3+
foo}
4+
{foo}{bar}
5+
{/each}
6+
7+
{#await aPromise then result}
8+
{@const bar = result ? 'some super long text which will force the ternary to break' : 'etc etc'}
9+
{/await}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{#each [1, 2] as foo}
2+
{@const bar = foo}
3+
{foo}{bar}
4+
{/each}
5+
6+
{#await aPromise then result}
7+
{@const bar = result
8+
? "some super long text which will force the ternary to break"
9+
: "etc etc"}
10+
{/await}

test/printer/samples/const.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{#each [1, 2] as foo}
2+
{@const bar = foo}
3+
{foo}{bar}
4+
{/each}
5+
6+
{#await aPromise then result}
7+
{@const bar = result}
8+
{/await}

0 commit comments

Comments
 (0)