Skip to content

Commit

Permalink
(feat) support style directive (#274)
Browse files Browse the repository at this point in the history
Requires Svelte 3.46.1 or later
  • Loading branch information
dummdidumm authored Jan 13, 2022
1 parent d97d7f6 commit ec6539e
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 2.6.0 (Unreleased)

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

## 2.5.1

Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.46.0",
"svelte": "^3.46.1",
"ts-node": "^9.1.1",
"tslib": "^2.0.3",
"typescript": "4.1.3"
Expand Down
25 changes: 24 additions & 1 deletion src/print/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
IfBlockNode,
Node,
OptionsNode,
StyleDirectiveNode,
TextNode,
} from './nodes';

Expand Down Expand Up @@ -572,6 +573,28 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
? ''
: concat(['=', ...printJsExpression()]),
]);
case 'StyleDirective':
if (isOrCanBeConvertedToShorthand(node)) {
if (options.svelteStrictMode) {
return concat([line, 'style:', node.name, '="{', node.name, '}"']);
} else if (options.svelteAllowShorthand) {
return concat([line, 'style:', node.name]);
} else {
return concat([line, 'style:', node.name, '={', node.name, '}']);
}
} else {
if (node.value === true) {
return concat([line, 'style:', node.name]);
}

const quotes = !isLoneMustacheTag(node.value) || options.svelteStrictMode;
const attrNodeValue = printAttributeNodeValue(path, print, quotes, node);
if (quotes) {
return concat([line, 'style:', node.name, '=', '"', attrNodeValue, '"']);
} else {
return concat([line, 'style:', node.name, '=', attrNodeValue]);
}
}
case 'Let':
return concat([
line,
Expand Down Expand Up @@ -732,7 +755,7 @@ function printAttributeNodeValue(
path: FastPath<any>,
print: PrintFn,
quotes: boolean,
node: AttributeNode,
node: AttributeNode | StyleDirectiveNode,
) {
const valueDocs = path.map((childPath) => childPath.call(print), 'value');

Expand Down
3 changes: 2 additions & 1 deletion src/print/node-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
ThenBlockNode,
CommentNode,
SlotTemplateNode,
StyleDirectiveNode,
} from './nodes';
import { blockElements, TagName } from '../lib/elements';
import { FastPath, ParserOptions } from 'prettier';
Expand Down Expand Up @@ -281,7 +282,7 @@ export function isAttributeShorthand(node: true | Node[]): node is [AttributeSho
/**
* True if node is of type `{a}` or `a={a}`
*/
export function isOrCanBeConvertedToShorthand(node: AttributeNode): boolean {
export function isOrCanBeConvertedToShorthand(node: AttributeNode | StyleDirectiveNode): boolean {
if (isAttributeShorthand(node.value)) {
return true;
}
Expand Down
7 changes: 7 additions & 0 deletions src/print/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ export interface ClassNode extends BaseNode {
expression: Node;
}

export interface StyleDirectiveNode extends BaseNode {
type: 'StyleDirective';
name: string;
value: Node[] | true;
}

export interface LetNode extends BaseNode {
type: 'Let';
name: string;
Expand Down Expand Up @@ -285,6 +291,7 @@ export type Node =
| EventHandlerNode
| BindingNode
| ClassNode
| StyleDirectiveNode
| LetNode
| DebugTagNode
| RefNode
Expand Down
4 changes: 4 additions & 0 deletions test/formatting/samples/style-directive/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div style:background=green />
<div style:background="{color}" />
<div style:background='prefix{color}suffix' />
<div style:background={background} />
4 changes: 4 additions & 0 deletions test/formatting/samples/style-directive/output.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div style:background="green" />
<div style:background={color} />
<div style:background="prefix{color}suffix" />
<div style:background />
7 changes: 7 additions & 0 deletions test/printer/samples/style-directive.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div style:background="green" />
<div style:background={color} />
<div style:background="prefix{color}suffix" />
<div style:background="{color}suffix" />
<div style:background="prefix{color}" />
<div style:background={`prefix${color}`} />
<div style:background />

0 comments on commit ec6539e

Please sign in to comment.