@astrojs/compiler 0.13.0 → 0.13.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # @astrojs/compiler
2
2
 
3
+ ## 0.13.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 2f8334c: Update `parse` and `serialize` functions to combine `attributes` and `directives`, fix issue with `serialize` not respecting `attributes`.
8
+ - b308955: Add self-close option to serialize util
9
+
3
10
  ## 0.13.0
4
11
 
5
12
  ### Minor Changes
package/astro.wasm CHANGED
Binary file
@@ -1,11 +1,11 @@
1
- import { Node, ParentNode, RootNode, ElementNode, CustomElementNode, ComponentNode, FragmentNode, LiteralNode, ExpressionNode, TextNode, CommentNode, DoctypeNode, FrontmatterNode } from '../shared/ast';
1
+ import { Node, ParentNode, RootNode, ElementNode, CustomElementNode, ComponentNode, FragmentNode, LiteralNode, TagLikeNode, ExpressionNode, TextNode, CommentNode, DoctypeNode, FrontmatterNode } from '../shared/ast';
2
2
  export interface Visitor {
3
3
  (node: Node, parent?: ParentNode, index?: number): void | Promise<void>;
4
4
  }
5
5
  export declare const is: {
6
6
  parent(node: Node): node is ParentNode;
7
7
  literal(node: Node): node is LiteralNode;
8
- tag(node: Node): node is ElementNode | ComponentNode | CustomElementNode;
8
+ tag(node: Node): node is TagLikeNode;
9
9
  whitespace(node: Node): node is TextNode;
10
10
  root: (node: Node) => node is RootNode;
11
11
  element: (node: Node) => node is ElementNode;
@@ -19,4 +19,7 @@ export declare const is: {
19
19
  frontmatter: (node: Node) => node is FrontmatterNode;
20
20
  };
21
21
  export declare function walk(node: ParentNode, callback: Visitor): void;
22
- export declare function serialize(root: Node): string;
22
+ export interface SerializeOtions {
23
+ selfClose: boolean;
24
+ }
25
+ export declare function serialize(root: Node, opts?: SerializeOtions): string;
package/browser/utils.js CHANGED
@@ -45,7 +45,40 @@ export function walk(node, callback) {
45
45
  const walker = new Walker(callback);
46
46
  walker.visit(node);
47
47
  }
48
- export function serialize(root) {
48
+ function serializeAttributes(node) {
49
+ let output = '';
50
+ for (const attr of node.attributes) {
51
+ output += ' ';
52
+ switch (attr.kind) {
53
+ case 'empty': {
54
+ output += `${attr.name}`;
55
+ break;
56
+ }
57
+ case 'expression': {
58
+ output += `${attr.name}={${attr.value}}`;
59
+ break;
60
+ }
61
+ case 'quoted': {
62
+ output += `${attr.name}="${attr.value}"`;
63
+ break;
64
+ }
65
+ case 'template-literal': {
66
+ output += `${attr.name}=\`${attr.value}\``;
67
+ break;
68
+ }
69
+ case 'shorthand': {
70
+ output += `{${attr.name}}`;
71
+ break;
72
+ }
73
+ case 'spread': {
74
+ output += `{...${attr.value}}`;
75
+ break;
76
+ }
77
+ }
78
+ }
79
+ return output;
80
+ }
81
+ export function serialize(root, opts = { selfClose: true }) {
49
82
  let output = '';
50
83
  function visitor(node) {
51
84
  if (is.root(node)) {
@@ -66,9 +99,16 @@ export function serialize(root) {
66
99
  output += node.value;
67
100
  }
68
101
  else if (is.tag(node)) {
69
- output += `<${node.name}>`;
70
- node.children.forEach((child) => visitor(child));
71
- output += `</${node.name}>`;
102
+ output += `<${node.name}`;
103
+ output += serializeAttributes(node);
104
+ if (node.children.length == 0 && opts.selfClose) {
105
+ output += ` />`;
106
+ }
107
+ else {
108
+ output += '>';
109
+ node.children.forEach((child) => visitor(child));
110
+ output += `</${node.name}>`;
111
+ }
72
112
  }
73
113
  }
74
114
  visitor(root);
package/node/utils.d.ts CHANGED
@@ -1,11 +1,11 @@
1
- import { Node, ParentNode, RootNode, ElementNode, CustomElementNode, ComponentNode, FragmentNode, LiteralNode, ExpressionNode, TextNode, CommentNode, DoctypeNode, FrontmatterNode } from '../shared/ast';
1
+ import { Node, ParentNode, RootNode, ElementNode, CustomElementNode, ComponentNode, FragmentNode, LiteralNode, TagLikeNode, ExpressionNode, TextNode, CommentNode, DoctypeNode, FrontmatterNode } from '../shared/ast';
2
2
  export interface Visitor {
3
3
  (node: Node, parent?: ParentNode, index?: number): void | Promise<void>;
4
4
  }
5
5
  export declare const is: {
6
6
  parent(node: Node): node is ParentNode;
7
7
  literal(node: Node): node is LiteralNode;
8
- tag(node: Node): node is ElementNode | ComponentNode | CustomElementNode;
8
+ tag(node: Node): node is TagLikeNode;
9
9
  whitespace(node: Node): node is TextNode;
10
10
  root: (node: Node) => node is RootNode;
11
11
  element: (node: Node) => node is ElementNode;
@@ -19,4 +19,7 @@ export declare const is: {
19
19
  frontmatter: (node: Node) => node is FrontmatterNode;
20
20
  };
21
21
  export declare function walk(node: ParentNode, callback: Visitor): void;
22
- export declare function serialize(root: Node): string;
22
+ export interface SerializeOtions {
23
+ selfClose: boolean;
24
+ }
25
+ export declare function serialize(root: Node, opts?: SerializeOtions): string;
package/node/utils.js CHANGED
@@ -45,7 +45,40 @@ export function walk(node, callback) {
45
45
  const walker = new Walker(callback);
46
46
  walker.visit(node);
47
47
  }
48
- export function serialize(root) {
48
+ function serializeAttributes(node) {
49
+ let output = '';
50
+ for (const attr of node.attributes) {
51
+ output += ' ';
52
+ switch (attr.kind) {
53
+ case 'empty': {
54
+ output += `${attr.name}`;
55
+ break;
56
+ }
57
+ case 'expression': {
58
+ output += `${attr.name}={${attr.value}}`;
59
+ break;
60
+ }
61
+ case 'quoted': {
62
+ output += `${attr.name}="${attr.value}"`;
63
+ break;
64
+ }
65
+ case 'template-literal': {
66
+ output += `${attr.name}=\`${attr.value}\``;
67
+ break;
68
+ }
69
+ case 'shorthand': {
70
+ output += `{${attr.name}}`;
71
+ break;
72
+ }
73
+ case 'spread': {
74
+ output += `{...${attr.value}}`;
75
+ break;
76
+ }
77
+ }
78
+ }
79
+ return output;
80
+ }
81
+ export function serialize(root, opts = { selfClose: true }) {
49
82
  let output = '';
50
83
  function visitor(node) {
51
84
  if (is.root(node)) {
@@ -66,9 +99,16 @@ export function serialize(root) {
66
99
  output += node.value;
67
100
  }
68
101
  else if (is.tag(node)) {
69
- output += `<${node.name}>`;
70
- node.children.forEach((child) => visitor(child));
71
- output += `</${node.name}>`;
102
+ output += `<${node.name}`;
103
+ output += serializeAttributes(node);
104
+ if (node.children.length == 0 && opts.selfClose) {
105
+ output += ` />`;
106
+ }
107
+ else {
108
+ output += '>';
109
+ node.children.forEach((child) => visitor(child));
110
+ output += `</${node.name}>`;
111
+ }
72
112
  }
73
113
  }
74
114
  visitor(root);
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "type": "module",
6
6
  "bugs": "https://github.com/withastro/compiler/issues",
7
7
  "homepage": "https://astro.build",
8
- "version": "0.13.0",
8
+ "version": "0.13.1",
9
9
  "scripts": {
10
10
  "build": "tsc -p ."
11
11
  },
package/shared/ast.d.ts CHANGED
@@ -33,9 +33,6 @@ export interface AttributeNode extends BaseNode {
33
33
  name: string;
34
34
  value: string;
35
35
  }
36
- export interface DirectiveNode extends Omit<AttributeNode, 'type'> {
37
- type: 'directive';
38
- }
39
36
  export interface TextNode extends LiteralNode {
40
37
  type: 'text';
41
38
  }
@@ -43,26 +40,23 @@ export interface ElementNode extends ParentLikeNode {
43
40
  type: 'element';
44
41
  name: string;
45
42
  attributes: AttributeNode[];
46
- directives: DirectiveNode[];
47
43
  }
48
44
  export interface FragmentNode extends ParentLikeNode {
49
45
  type: 'fragment';
50
46
  name: string;
51
47
  attributes: AttributeNode[];
52
- directives: DirectiveNode[];
53
48
  }
54
49
  export interface ComponentNode extends ParentLikeNode {
55
50
  type: 'component';
56
51
  name: string;
57
52
  attributes: AttributeNode[];
58
- directives: DirectiveNode[];
59
53
  }
60
54
  export interface CustomElementNode extends ParentLikeNode {
61
55
  type: 'custom-element';
62
56
  name: string;
63
57
  attributes: AttributeNode[];
64
- directives: DirectiveNode[];
65
58
  }
59
+ export declare type TagLikeNode = ElementNode | FragmentNode | ComponentNode | CustomElementNode;
66
60
  export interface DoctypeNode extends LiteralNode {
67
61
  type: 'doctype';
68
62
  }