Skip to content

Commit

Permalink
Format directives (#156)
Browse files Browse the repository at this point in the history
* format directives

* chore: add changeset

* chore: fix test title
  • Loading branch information
antonyfaris authored Mar 24, 2022
1 parent 0ed661a commit 67341e7
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/bright-pens-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'prettier-plugin-astro': patch
---

Format directives
2 changes: 0 additions & 2 deletions src/nodes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
Node,
AttributeNode,
DirectiveNode,
RootNode,
ElementNode,
ComponentNode,
Expand Down Expand Up @@ -286,7 +285,6 @@ export const selfClosingTags = [
export type anyNode =
| RootNode
| AttributeNode
| DirectiveNode
| ElementNode
| ComponentNode
| CustomElementNode
Expand Down
5 changes: 5 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { SupportOption } from 'prettier';

declare module 'prettier' {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface RequiredOptions extends PluginOptions {}
}

export interface PluginOptions {
astroSortOrder: SortOrder;
astroAllowShorthand: boolean;
Expand Down
16 changes: 5 additions & 11 deletions src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ function print(path: AstPath, opts: ParserOptions, print: printFn): Doc {
isEmpty &&
(node.type !== 'element' || selfClosingTags.indexOf(node.name) !== -1);

const attributes = node.attributes ? path.map(print, 'attributes') : [];
const attributes = path.map(print, 'attributes');
if (isSelfClosingTag) {
return group(['<', node.name, indent(group(attributes)), line, `/>`]);
// return group(['<', node.name, indent(group([...attributes, opts.jsxBracketNewLine ? dedent(line) : ''])), ...[opts.jsxBracketNewLine ? '' : ' ', `/>`]]);
Expand Down Expand Up @@ -419,10 +419,6 @@ function print(path: AstPath, opts: ParserOptions, print: printFn): Doc {
return '';
}

case 'directive': {
return [line, node.name];
}

case 'doctype': {
// https://www.w3.org/wiki/Doctypes_and_markup_styles
return ['<!DOCTYPE html>', hardline];
Expand Down Expand Up @@ -619,7 +615,7 @@ function embed(
formatttedScript = stripTrailingHardline(formatttedScript);

// print
const attributes = node.attributes ? path.map(print, 'attributes') : [];
const attributes = path.map(print, 'attributes');
const openingTag = group([
'<script',
indent(group(attributes)),
Expand Down Expand Up @@ -671,12 +667,10 @@ function embed(
formattedStyles = stripTrailingHardline(formattedStyles);

// print
const attributes = node.attributes ? path.map(print, 'attributes') : [];
const directives = node.directives ? path.map(print, 'directives') : [];
const attrAndDirectives = directives.concat(attributes);
const attributes = path.map(print, 'attributes');
const openingTag = group([
'<style',
indent(group(attrAndDirectives)),
indent(group(attributes)),
softline,
'>',
]);
Expand Down Expand Up @@ -707,7 +701,7 @@ function embed(

// print
const formattedSass = join(hardline, formattedSassIndented.split('\n'));
const attributes = node.attributes ? path.map(print, 'attributes') : [];
const attributes = path.map(print, 'attributes');
const openingTag = group([
'<style',
indent(group(attributes)),
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/directive/input.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
const content = "lorem"
---

<h1 set:html = {content }></h1>
5 changes: 5 additions & 0 deletions test/fixtures/directive/output.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
const content = "lorem";
---

<h1 set:html={content}></h1>
2 changes: 2 additions & 0 deletions test/tests/other.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,5 @@ test('Format spread operator', 'spread-operator');
test('Can format nested comment', 'nested-comment');

test('format binary expressions', 'binary-expression');

test('format directives', 'directive');

0 comments on commit 67341e7

Please sign in to comment.