Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/compiler-core/src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ export interface BaseElementNode extends Node {
type: NodeTypes.ELEMENT
ns: Namespace
tag: string
openTagLoc?: SourceLocation
closeTagLoc?: SourceLocation
tagType: ElementTypes
props: Array<AttributeNode | DirectiveNode>
children: TemplateChildNode[]
Expand Down
5 changes: 5 additions & 0 deletions packages/compiler-core/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ export interface ParserOptions
* @default false
*/
prefixIdentifiers?: boolean
/**
* Whether to calculate the location of open tag and close tag of each node.
* @default false
*/
tagLocations?: boolean
/**
* A list of parser plugins to enable for `@babel/parser`, which is used to
* parse expressions in bindings and interpolations.
Expand Down
7 changes: 7 additions & 0 deletions packages/compiler-core/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export const defaultParserOptions: MergedParserOptions = {
onWarn: defaultOnWarn,
comments: __DEV__,
prefixIdentifiers: false,
tagLocations: false,
}

let currentOptions: MergedParserOptions = defaultParserOptions
Expand Down Expand Up @@ -146,6 +147,9 @@ const tokenizer = new Tokenizer(stack, {
loc: getLoc(start - 1, end),
codegenNode: undefined,
}
if (currentOptions.tagLocations) {
currentOpenTag.openTagLoc = getLoc(start, end)
}
},

onopentagend(end) {
Expand All @@ -165,6 +169,9 @@ const tokenizer = new Tokenizer(stack, {
}
for (let j = 0; j <= i; j++) {
const el = stack.shift()!
if (j === i && currentOptions.tagLocations) {
el.closeTagLoc = getLoc(start, end)
}
onCloseTag(el, end, j < i)
}
break
Expand Down