AST for
May 14, 2023 ยท View on GitHub
Some types are featured from ESTree.
You can use the type definition of this AST:
import { AST } from "vue-eslint-parser"
export function create(context) {
return context.parserServices.defineTemplateBodyVisitor(
// Event handlers for <template>.
{
VElement(node: AST.VElement): void {
//...
}
},
// Event handlers for <script> or scripts. (optional)
{
Program(node: AST.ESLintProgram): void {
//...
}
}
)
}
AST has the types of ESLint's AST with the prefix ESLint.
See details: ../src/ast/nodes.ts
Node
extend interface Node {
range: [ number ]
}
- This AST spec enhances the Node nodes like ESLint.
- The
rangeproperty is an array which has 2 integers. The 1st integer is the offset of the start location of the node. The 2nd integer is the offset of the end location of the node.
VIdentifier
interface VIdentifier <: Node {
type: "VIdentifier"
name: string
rawName: string
}
- This is similar to Identifier nodes but this
nameproperty can include any characters except U+0000-U+001F, U+007F-U+009F, U+0020, U+0022, U+0027, U+003E, U+002F, U+003D, U+FDD0-U+FDEF, U+FFFE, U+FFFF, U+1FFFE, U+1FFFF, U+2FFFE, U+2FFFF, U+3FFFE, U+3FFFF, U+4FFFE, U+4FFFF, U+5FFFE, U+5FFFF, U+6FFFE, U+6FFFF, U+7FFFE, U+7FFFF, U+8FFFE, U+8FFFF, U+9FFFE, U+9FFFF, U+AFFFE, U+AFFFF, U+BFFFE, U+BFFFF, U+CFFFE, U+CFFFF, U+DFFFE, U+DFFFF, U+EFFFE, U+EFFFF, U+FFFFE, U+FFFFF, U+10FFFE and U+10FFFF. - This is attribute names.
VText
interface VText <: Node {
type: "VText"
value: string
}
- Plain text of HTML.
- HTML entities in the
valueproperty are decoded.
VExpressionContainer
interface VExpressionContainer <: Node {
type: "VExpressionContainer"
expression: Expression | null
references: [ Reference ]
}
interface Reference {
id: Identifier
mode: "rw" | "r" | "w"
variable: Variable | null
}
interface VForExpression <: Expression {
type: "VForExpression"
left: [ Pattern ]
right: Expression
}
interface VOnExpression <: Expression {
type: "VOnExpression"
body: [ Statement ]
}
interface VSlotScopeExpression <: Expression {
type: "VSlotScopeExpression"
params: [ Pattern | RestElement ]
}
interface VGenericExpression <: Expression {
type: "VGenericExpression"
params: [ TSTypeParameter ]
}
interface VFilterSequenceExpression <: Expression {
type: "VFilterSequenceExpression"
expression: Expression
filters: [ VFilter ]
}
interface VFilter <: Node {
type: "VFilter"
callee: Identifier
arguments: [ Expression ]
}
- This is mustaches, directive values, or
v-bind()in<style>. - If syntax errors exist,
VExpressionContainer#expressionisnull. - If it's an empty mustache,
VExpressionContainer#expressionisnull. (e.g.,{{ /* a comment */ }}) Referenceis objects but notNode. Those are external references which are in the expression.Reference#variableis the variable which is defined by aVElement. If a reference uses a global variable or a member of VM, this isnull.VForExpressionis an expression node like ForInStatement but it has an array asleftproperty and does not havebodyproperty. This is the value ofv-fordirectives.VOnExpressionis an expression node like BlockStatement but it does not have braces. This is the value ofv-ondirectives only if thev-ondirective doesn't have that argument.VSlotScopeExpressionis an expression node like VariableDeclarator. This is the value ofv-slotdirectives,slot-scopeattributes, andscopeattributes.VGenericExpressionis an expression node like typescript-eslint's TSTypeParameterDeclaration. This is the value of thegenericattributes on the<script>tag.VFilterSequenceExpressionis an expression node for Vue.js Filters syntax.
Note:
vue-eslint-parsertransformsv-for="(x, i) in list"tofor(let [x, i] in list);then gives the configured parser (espreeby default) it. This implies that it needs the capability to parse ES2015 destructuring in order to parsev-fordirectives.
VDirectiveKey
interface VDirectiveKey <: Node {
type: "VDirectiveKey"
name: VIdentifier
argument: VExpressionContainer | VIdentifier | null
modifiers: [ VIdentifier ]
}
- The
nameproperty doesn't havev-prefix. It's dropped. - The
argumentproperty is aVExpressionContainernode if it's a dynamic argument. - In the shorthand of
v-bindcase, thename.nameproperty is"bind"and thename.rawNameproperty is":". - In the shorthand of
v-bindwith.propmodifier case, thename.nameproperty is"bind"and thename.rawNameproperty is"."and themodifiersproperty includes aVIdentifiernode of"prop". - In the shorthand of
v-oncase, thename.nameproperty is"on"and thename.rawNameproperty is@. - In the shorthand of
v-slotcase, thename.nameproperty is"slot"and thename.rawNameproperty is#. - Otherwise,
shorthandproperty is alwaysfalse.
VLiteral
interface VLiteral <: Node {
type: "VAttributeValue"
value: string
}
- This is similar to Literal nodes but this is not always quoted.
- HTML entities in the
valueproperty are decoded.
VAttribute
interface VAttribute <: Node {
type: "VAttribute"
directive: false
key: VIdentifier
value: VLiteral | null
}
interface VDirective <: Node {
type: "VAttribute"
directive: true
key: VDirectiveKey
value: VExpressionContainer | null
}
- If their attribute value does not exist, the
valueproperty isnull. - The
slot-scopeattribute becomesdirective:truespecially.
VStartTag
interface VStartTag <: Node {
type: "VStartTag"
attributes: [ VAttribute ]
}
VEndTag
interface VEndTag <: Node {
type: "VEndTag"
}
VElement
interface VElement <: Node {
type: "VElement"
namespace: string
name: string
startTag: VStartTag
children: [ VText | VExpressionContainer | VElement ]
endTag: VEndTag | null
variables: [ Variable ]
}
interface Variable {
id: Identifier
kind: "v-for" | "scope"
references: [ Reference ]
}
Variableis objects but notNode. Those are variable declarations that child elements can use. The elements which havev-fordirectives or a special attribute scope can declare variables.Variable#referencesis an array of references which use this variable.
VRootElement
interface VRootElement <: VElement {
tokens: [ Token ]
comments: [ Token ]
errors: [ ParseError ]
}
interface Token <: Node {
type: string
value: string
}
interface ParseError <: Error {
code?: string
message: string
index: number
lineNumber: number
column: number
}
Program
extend interface Program {
templateBody: VRootElement | null
}
This spec enhances Program nodes as it has the root node of <template>.
This supports only HTML for now. However, I'm going to add other languages Vue.js supports. The AST of other languages may be different form to VElement.