Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
5 changes: 5 additions & 0 deletions packages/compiler-dom/__tests__/parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,15 @@
const ast = parse('<img>after', parserOptions)
const element = ast.children[0] as ElementNode

expect(element).toStrictEqual({

Check failure on line 308 in packages/compiler-dom/__tests__/parse.spec.ts

View workflow job for this annotation

GitHub Actions / test / unit-test-windows

packages/compiler-dom/__tests__/parse.spec.ts > DOM parser > Element > void element

AssertionError: expected { type: 1, tag: 'img', ns: +0, …(5) } to strictly equal { type: 1, ns: +0, tag: 'img', …(6) } - Expected + Received @@ -15,21 +15,8 @@ }, }, "ns": 0, "props": [], "tag": "img", - "tagLoc": { - "end": { - "column": 5, - "line": 1, - "offset": 4, - }, - "source": "img", - "start": { - "column": 2, - "line": 1, - "offset": 1, - }, - }, "tagType": 0, "type": 1, } ❯ packages/compiler-dom/__tests__/parse.spec.ts:308:23

Check failure on line 308 in packages/compiler-dom/__tests__/parse.spec.ts

View workflow job for this annotation

GitHub Actions / test / unit-test

packages/compiler-dom/__tests__/parse.spec.ts > DOM parser > Element > void element

AssertionError: expected { type: 1, tag: 'img', ns: +0, …(5) } to strictly equal { type: 1, ns: +0, tag: 'img', …(6) } - Expected + Received @@ -15,21 +15,8 @@ }, }, "ns": 0, "props": [], "tag": "img", - "tagLoc": { - "end": { - "column": 5, - "line": 1, - "offset": 4, - }, - "source": "img", - "start": { - "column": 2, - "line": 1, - "offset": 1, - }, - }, "tagType": 0, "type": 1, } ❯ packages/compiler-dom/__tests__/parse.spec.ts:308:23
type: NodeTypes.ELEMENT,
ns: Namespaces.HTML,
tag: 'img',
tagLoc: {
start: { offset: 1, line: 1, column: 2 },
end: { offset: 4, line: 1, column: 5 },
source: 'img',
},
tagType: ElementTypes.ELEMENT,
props: [],
children: [],
Expand Down
Loading