Skip to content

Commit ec6539e

Browse files
authored
(feat) support style directive (#274)
Requires Svelte 3.46.1 or later
1 parent d97d7f6 commit ec6539e

File tree

9 files changed

+57
-10
lines changed

9 files changed

+57
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 2.6.0 (Unreleased)
44

55
* (feat) Support `@const` tag ([#272](https://github.com/sveltejs/prettier-plugin-svelte/issues/272))
6+
* (feat) Support `style` directive (requires Svelte 3.46.1 or later) ([#274](https://github.com/sveltejs/prettier-plugin-svelte/issues/274))
67

78
## 2.5.1
89

package-lock.json

Lines changed: 7 additions & 7 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.46.0",
40+
"svelte": "^3.46.1",
4141
"ts-node": "^9.1.1",
4242
"tslib": "^2.0.3",
4343
"typescript": "4.1.3"

src/print/index.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import {
4444
IfBlockNode,
4545
Node,
4646
OptionsNode,
47+
StyleDirectiveNode,
4748
TextNode,
4849
} from './nodes';
4950

@@ -572,6 +573,28 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
572573
? ''
573574
: concat(['=', ...printJsExpression()]),
574575
]);
576+
case 'StyleDirective':
577+
if (isOrCanBeConvertedToShorthand(node)) {
578+
if (options.svelteStrictMode) {
579+
return concat([line, 'style:', node.name, '="{', node.name, '}"']);
580+
} else if (options.svelteAllowShorthand) {
581+
return concat([line, 'style:', node.name]);
582+
} else {
583+
return concat([line, 'style:', node.name, '={', node.name, '}']);
584+
}
585+
} else {
586+
if (node.value === true) {
587+
return concat([line, 'style:', node.name]);
588+
}
589+
590+
const quotes = !isLoneMustacheTag(node.value) || options.svelteStrictMode;
591+
const attrNodeValue = printAttributeNodeValue(path, print, quotes, node);
592+
if (quotes) {
593+
return concat([line, 'style:', node.name, '=', '"', attrNodeValue, '"']);
594+
} else {
595+
return concat([line, 'style:', node.name, '=', attrNodeValue]);
596+
}
597+
}
575598
case 'Let':
576599
return concat([
577600
line,
@@ -732,7 +755,7 @@ function printAttributeNodeValue(
732755
path: FastPath<any>,
733756
print: PrintFn,
734757
quotes: boolean,
735-
node: AttributeNode,
758+
node: AttributeNode | StyleDirectiveNode,
736759
) {
737760
const valueDocs = path.map((childPath) => childPath.call(print), 'value');
738761

src/print/node-helpers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
ThenBlockNode,
2121
CommentNode,
2222
SlotTemplateNode,
23+
StyleDirectiveNode,
2324
} from './nodes';
2425
import { blockElements, TagName } from '../lib/elements';
2526
import { FastPath, ParserOptions } from 'prettier';
@@ -281,7 +282,7 @@ export function isAttributeShorthand(node: true | Node[]): node is [AttributeSho
281282
/**
282283
* True if node is of type `{a}` or `a={a}`
283284
*/
284-
export function isOrCanBeConvertedToShorthand(node: AttributeNode): boolean {
285+
export function isOrCanBeConvertedToShorthand(node: AttributeNode | StyleDirectiveNode): boolean {
285286
if (isAttributeShorthand(node.value)) {
286287
return true;
287288
}

src/print/nodes.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ export interface ClassNode extends BaseNode {
119119
expression: Node;
120120
}
121121

122+
export interface StyleDirectiveNode extends BaseNode {
123+
type: 'StyleDirective';
124+
name: string;
125+
value: Node[] | true;
126+
}
127+
122128
export interface LetNode extends BaseNode {
123129
type: 'Let';
124130
name: string;
@@ -285,6 +291,7 @@ export type Node =
285291
| EventHandlerNode
286292
| BindingNode
287293
| ClassNode
294+
| StyleDirectiveNode
288295
| LetNode
289296
| DebugTagNode
290297
| RefNode
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<div style:background=green />
2+
<div style:background="{color}" />
3+
<div style:background='prefix{color}suffix' />
4+
<div style:background={background} />
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<div style:background="green" />
2+
<div style:background={color} />
3+
<div style:background="prefix{color}suffix" />
4+
<div style:background />
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div style:background="green" />
2+
<div style:background={color} />
3+
<div style:background="prefix{color}suffix" />
4+
<div style:background="{color}suffix" />
5+
<div style:background="prefix{color}" />
6+
<div style:background={`prefix${color}`} />
7+
<div style:background />

0 commit comments

Comments
 (0)