Skip to content

Commit f6fe7b9

Browse files
authored
Update AST type (#29)
1 parent b7e808a commit f6fe7b9

File tree

7 files changed

+240
-78
lines changed

7 files changed

+240
-78
lines changed

src/ast.ts

Lines changed: 0 additions & 76 deletions
This file was deleted.

src/ast/astro.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import type {
2+
JSXAttribute,
3+
JSXElement,
4+
JSXExpression,
5+
JSXExpressionContainer,
6+
JSXFragment,
7+
JSXText,
8+
} from "./jsx"
9+
import type { TSESTree as ES } from "@typescript-eslint/types"
10+
import type { BaseNode } from "./base"
11+
12+
export type AstroNode =
13+
| AstroProgram
14+
| AstroFragment
15+
| AstroHTMLComment
16+
| AstroDoctype
17+
| AstroShorthandAttribute
18+
| AstroTemplateLiteralAttribute
19+
| AstroRawText
20+
export type AstroChild =
21+
| JSXElement
22+
| JSXFragment
23+
| JSXExpression
24+
| JSXText
25+
| AstroHTMLComment
26+
export type AstroParentNode = JSXElement | JSXFragment | AstroFragment
27+
28+
/** Node of Astro program root */
29+
export interface AstroProgram extends Omit<ES.Program, "type" | "body"> {
30+
type: "Program"
31+
body: (ES.Program["body"][number] | AstroFragment)[]
32+
sourceType: "script" | "module"
33+
comments: ES.Comment[]
34+
tokens: ES.Token[]
35+
parent?: undefined
36+
}
37+
38+
/* --- Tags --- */
39+
/** Node of Astro fragment */
40+
export interface AstroFragment extends BaseNode {
41+
type: "AstroFragment"
42+
children: AstroChild[]
43+
parent?: AstroParentNode
44+
}
45+
/** Node of Astro html comment */
46+
export interface AstroHTMLComment extends BaseNode {
47+
type: "AstroHTMLComment"
48+
value: string
49+
parent?: AstroParentNode
50+
}
51+
/** Node of Astro doctype */
52+
export interface AstroDoctype extends BaseNode {
53+
type: "AstroDoctype"
54+
parent?: AstroFragment
55+
}
56+
57+
/* --- Attributes --- */
58+
/** Node of Astro shorthand attribute */
59+
export interface AstroShorthandAttribute extends Omit<JSXAttribute, "type"> {
60+
type: "AstroShorthandAttribute"
61+
value: JSXExpressionContainer
62+
}
63+
/** Node of Astro template-literal attribute */
64+
export interface AstroTemplateLiteralAttribute
65+
extends Omit<JSXAttribute, "type"> {
66+
type: "AstroTemplateLiteralAttribute"
67+
value: JSXExpressionContainer & {
68+
expression: ES.TemplateLiteral
69+
}
70+
}
71+
72+
/* --- Texts --- */
73+
/** Node of Astro raw text */
74+
export interface AstroRawText extends Omit<JSXText, "type"> {
75+
type: "AstroRawText"
76+
parent?: JSXElement
77+
}

src/ast/base.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { TSESTree } from "@typescript-eslint/types"
2+
3+
export interface BaseNode {
4+
loc: TSESTree.SourceLocation
5+
range: TSESTree.Range
6+
type: string
7+
}

src/ast/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./astro"
2+
export * from "./jsx"

src/ast/jsx.ts

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
import type { TSESTree as ES, AST_NODE_TYPES } from "@typescript-eslint/types"
2+
import type {
3+
AstroFragment,
4+
AstroHTMLComment,
5+
AstroShorthandAttribute,
6+
AstroTemplateLiteralAttribute,
7+
} from "./astro"
8+
import type { BaseNode } from "./base"
9+
10+
export type JSXNode =
11+
| JSXAttribute
12+
| JSXClosingElement
13+
| JSXClosingFragment
14+
| JSXElement
15+
| JSXEmptyExpression
16+
| JSXExpressionContainer
17+
| JSXFragment
18+
| JSXIdentifier
19+
| JSXMemberExpression
20+
| JSXNamespacedName
21+
| JSXOpeningElement
22+
| JSXOpeningFragment
23+
| JSXSpreadAttribute
24+
| JSXSpreadChild
25+
| JSXText
26+
export type JSXChild =
27+
| JSXElement
28+
| JSXFragment
29+
| JSXExpression
30+
| JSXText
31+
| AstroHTMLComment
32+
export type JSXParentNode = JSXElement | JSXFragment | AstroFragment
33+
/* --- Tags --- */
34+
export interface JSXElement extends BaseNode {
35+
type: AST_NODE_TYPES.JSXElement
36+
openingElement: JSXOpeningElement
37+
closingElement: JSXClosingElement | null
38+
children: JSXChild[]
39+
parent?: JSXParentNode
40+
}
41+
export interface JSXFragment extends BaseNode {
42+
type: AST_NODE_TYPES.JSXFragment
43+
openingFragment: JSXOpeningFragment
44+
closingFragment: JSXClosingFragment
45+
children: JSXChild[]
46+
parent?: JSXParentNode
47+
}
48+
export interface JSXOpeningElement extends BaseNode {
49+
type: AST_NODE_TYPES.JSXOpeningElement
50+
typeParameters?: ES.TSTypeParameterInstantiation
51+
selfClosing: boolean
52+
name: JSXTagNameExpression
53+
attributes: (
54+
| JSXAttribute
55+
| JSXSpreadAttribute
56+
| AstroShorthandAttribute
57+
| AstroTemplateLiteralAttribute
58+
)[]
59+
parent?: JSXElement
60+
}
61+
export interface JSXClosingElement extends BaseNode {
62+
type: AST_NODE_TYPES.JSXClosingElement
63+
name: JSXTagNameExpression
64+
parent?: JSXElement
65+
}
66+
export interface JSXClosingFragment extends BaseNode {
67+
type: AST_NODE_TYPES.JSXClosingFragment
68+
parent?: JSXFragment
69+
}
70+
export interface JSXOpeningFragment extends BaseNode {
71+
type: AST_NODE_TYPES.JSXOpeningFragment
72+
parent?: JSXFragment
73+
}
74+
75+
/* --- Attributes --- */
76+
export interface JSXAttribute extends BaseNode {
77+
type: AST_NODE_TYPES.JSXAttribute
78+
name: JSXIdentifier | JSXNamespacedName
79+
value: JSXExpression | ES.Literal | null
80+
parent?: JSXOpeningElement
81+
}
82+
export interface JSXSpreadAttribute extends BaseNode {
83+
type: AST_NODE_TYPES.JSXSpreadAttribute
84+
argument: ES.Expression
85+
parent?: JSXOpeningElement
86+
}
87+
88+
/* --- Names --- */
89+
export type JSXTagNameExpression =
90+
| JSXIdentifier
91+
| JSXMemberExpression
92+
| JSXNamespacedName
93+
export interface JSXIdentifier extends BaseNode {
94+
type: AST_NODE_TYPES.JSXIdentifier
95+
name: string
96+
parent?:
97+
| JSXAttribute
98+
| AstroShorthandAttribute
99+
| AstroTemplateLiteralAttribute
100+
| JSXMemberExpression
101+
| JSXNamespacedName
102+
| JSXOpeningElement
103+
| JSXClosingElement
104+
}
105+
export interface JSXMemberExpression extends BaseNode {
106+
type: AST_NODE_TYPES.JSXMemberExpression
107+
object: JSXTagNameExpression
108+
property: JSXIdentifier
109+
parent?: JSXMemberExpression | JSXOpeningElement | JSXClosingElement
110+
}
111+
export interface JSXNamespacedName extends BaseNode {
112+
type: AST_NODE_TYPES.JSXNamespacedName
113+
namespace: JSXIdentifier
114+
name: JSXIdentifier
115+
parent?:
116+
| JSXAttribute
117+
| AstroShorthandAttribute
118+
| AstroTemplateLiteralAttribute
119+
| JSXMemberExpression
120+
| JSXOpeningElement
121+
| JSXClosingElement
122+
}
123+
124+
/* --- Expressions --- */
125+
export type JSXExpression = JSXExpressionContainer | JSXSpreadChild
126+
export interface JSXExpressionContainer extends BaseNode {
127+
type: AST_NODE_TYPES.JSXExpressionContainer
128+
expression: ES.Expression | JSXEmptyExpression
129+
parent?:
130+
| JSXAttribute
131+
| AstroShorthandAttribute
132+
| AstroTemplateLiteralAttribute
133+
| JSXParentNode
134+
}
135+
export interface JSXSpreadChild extends BaseNode {
136+
type: AST_NODE_TYPES.JSXSpreadChild
137+
expression: ES.Expression
138+
parent?: JSXAttribute | JSXParentNode
139+
}
140+
export interface JSXEmptyExpression extends BaseNode {
141+
type: AST_NODE_TYPES.JSXEmptyExpression
142+
parent?: JSXExpressionContainer
143+
}
144+
145+
/* --- Texts --- */
146+
export interface JSXText extends BaseNode {
147+
type: AST_NODE_TYPES.JSXText
148+
value: string
149+
raw: string
150+
parent?: JSXParentNode
151+
}

src/parser/process-template.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import type {
2121
AstroRawText,
2222
AstroShorthandAttribute,
2323
AstroTemplateLiteralAttribute,
24+
JSXElement,
2425
} from "../ast"
2526

2627
/**
@@ -316,7 +317,7 @@ export function processTemplate(
316317
type: "AstroRawText",
317318
value: text.value,
318319
raw: text.value,
319-
parent: scriptNode,
320+
parent: scriptNode as JSXElement,
320321
...ctx.getLocations(
321322
start,
322323
start + text.value.length,

src/visitor-keys.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ type AstroKeysType<T extends AstroNode = AstroNode> = {
77
? KeyofObject<T>[]
88
: never
99
}
10-
type KeyofObject<T> = { [key in keyof T]: key }[keyof T]
10+
type KeyofObject<T> = { [key in keyof T]-?: key }[keyof T]
1111

1212
const astroKeys: AstroKeysType = {
1313
Program: ["body"],

0 commit comments

Comments
 (0)