refactor: opt-in

This commit is contained in:
山吹色御守 2025-06-18 21:27:41 +08:00
parent 1c89b42aaf
commit 308c7f01dc
5 changed files with 12 additions and 1679 deletions

View File

@ -446,16 +446,6 @@ describe('compiler: parse', () => {
type: NodeTypes.ELEMENT,
ns: Namespaces.HTML,
tag: 'div',
tagLoc: {
start: { offset: 1, line: 1, column: 2 },
end: { offset: 4, line: 1, column: 5 },
source: 'div',
},
closeTagLoc: {
start: { offset: 12, line: 1, column: 13 },
end: { offset: 15, line: 1, column: 16 },
source: 'div',
},
tagType: ElementTypes.ELEMENT,
codegenNode: undefined,
props: [],
@ -486,16 +476,6 @@ describe('compiler: parse', () => {
type: NodeTypes.ELEMENT,
ns: Namespaces.HTML,
tag: 'div',
tagLoc: {
start: { offset: 1, line: 1, column: 2 },
end: { offset: 4, line: 1, column: 5 },
source: 'div',
},
closeTagLoc: {
start: { offset: 7, line: 1, column: 8 },
end: { offset: 10, line: 1, column: 11 },
source: 'div',
},
tagType: ElementTypes.ELEMENT,
codegenNode: undefined,
props: [],
@ -516,11 +496,6 @@ describe('compiler: parse', () => {
type: NodeTypes.ELEMENT,
ns: Namespaces.HTML,
tag: 'div',
tagLoc: {
start: { offset: 1, line: 1, column: 2 },
end: { offset: 4, line: 1, column: 5 },
source: 'div',
},
tagType: ElementTypes.ELEMENT,
codegenNode: undefined,
props: [],
@ -544,11 +519,6 @@ describe('compiler: parse', () => {
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,
codegenNode: undefined,
props: [],
@ -571,11 +541,6 @@ describe('compiler: parse', () => {
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,
codegenNode: undefined,
props: [],
@ -763,16 +728,6 @@ describe('compiler: parse', () => {
type: NodeTypes.ELEMENT,
ns: Namespaces.HTML,
tag: 'div',
tagLoc: {
start: { offset: 1, line: 1, column: 2 },
end: { offset: 4, line: 1, column: 5 },
source: 'div',
},
closeTagLoc: {
start: { offset: 10, line: 1, column: 11 },
end: { offset: 13, line: 1, column: 14 },
source: 'div',
},
tagType: ElementTypes.ELEMENT,
codegenNode: undefined,
props: [
@ -810,16 +765,6 @@ describe('compiler: parse', () => {
type: NodeTypes.ELEMENT,
ns: Namespaces.HTML,
tag: 'div',
tagLoc: {
start: { offset: 1, line: 1, column: 2 },
end: { offset: 4, line: 1, column: 5 },
source: 'div',
},
closeTagLoc: {
start: { offset: 13, line: 1, column: 14 },
end: { offset: 16, line: 1, column: 17 },
source: 'div',
},
tagType: ElementTypes.ELEMENT,
codegenNode: undefined,
props: [
@ -865,16 +810,6 @@ describe('compiler: parse', () => {
type: NodeTypes.ELEMENT,
ns: Namespaces.HTML,
tag: 'div',
tagLoc: {
start: { offset: 1, line: 1, column: 2 },
end: { offset: 4, line: 1, column: 5 },
source: 'div',
},
closeTagLoc: {
start: { offset: 13, line: 1, column: 14 },
end: { offset: 16, line: 1, column: 17 },
source: 'div',
},
tagType: ElementTypes.ELEMENT,
codegenNode: undefined,
props: [
@ -920,16 +855,6 @@ describe('compiler: parse', () => {
type: NodeTypes.ELEMENT,
ns: Namespaces.HTML,
tag: 'div',
tagLoc: {
start: { offset: 1, line: 1, column: 2 },
end: { offset: 4, line: 1, column: 5 },
source: 'div',
},
closeTagLoc: {
start: { offset: 15, line: 1, column: 16 },
end: { offset: 18, line: 1, column: 19 },
source: 'div',
},
tagType: ElementTypes.ELEMENT,
codegenNode: undefined,
props: [
@ -975,16 +900,6 @@ describe('compiler: parse', () => {
type: NodeTypes.ELEMENT,
ns: Namespaces.HTML,
tag: 'div',
tagLoc: {
start: { offset: 1, line: 1, column: 2 },
end: { offset: 4, line: 1, column: 5 },
source: 'div',
},
closeTagLoc: {
start: { offset: 15, line: 1, column: 16 },
end: { offset: 18, line: 1, column: 19 },
source: 'div',
},
tagType: ElementTypes.ELEMENT,
codegenNode: undefined,
props: [
@ -1030,16 +945,6 @@ describe('compiler: parse', () => {
type: NodeTypes.ELEMENT,
ns: Namespaces.HTML,
tag: 'div',
tagLoc: {
start: { offset: 1, line: 1, column: 2 },
end: { offset: 4, line: 1, column: 5 },
source: 'div',
},
closeTagLoc: {
start: { offset: 13, line: 1, column: 14 },
end: { offset: 16, line: 1, column: 17 },
source: 'div',
},
tagType: ElementTypes.ELEMENT,
codegenNode: undefined,
props: [
@ -1168,16 +1073,6 @@ describe('compiler: parse', () => {
type: NodeTypes.ELEMENT,
ns: Namespaces.HTML,
tag: 'div',
tagLoc: {
start: { offset: 1, line: 1, column: 2 },
end: { offset: 4, line: 1, column: 5 },
source: 'div',
},
closeTagLoc: {
start: { offset: 37, line: 1, column: 38 },
end: { offset: 40, line: 1, column: 41 },
source: 'div',
},
tagType: ElementTypes.ELEMENT,
codegenNode: undefined,
props: [
@ -1283,11 +1178,6 @@ describe('compiler: parse', () => {
expect(element).toStrictEqual({
children: [],
closeTagLoc: {
start: { column: 6, line: 3, offset: 25 },
end: { column: 9, line: 3, offset: 28 },
source: 'div',
},
codegenNode: undefined,
loc: {
start: { column: 1, line: 1, offset: 0 },
@ -1321,11 +1211,6 @@ describe('compiler: parse', () => {
},
],
tag: 'div',
tagLoc: {
start: { column: 2, line: 1, offset: 1 },
end: { column: 5, line: 1, offset: 4 },
source: 'div',
},
tagType: ElementTypes.ELEMENT,
type: NodeTypes.ELEMENT,
})

View File

@ -130,7 +130,7 @@ export interface BaseElementNode extends Node {
type: NodeTypes.ELEMENT
ns: Namespace
tag: string
tagLoc: SourceLocation
openTagLoc?: SourceLocation
closeTagLoc?: SourceLocation
tagType: ElementTypes
props: Array<AttributeNode | DirectiveNode>

View File

@ -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.

View File

@ -78,6 +78,7 @@ export const defaultParserOptions: MergedParserOptions = {
onWarn: defaultOnWarn,
comments: __DEV__,
prefixIdentifiers: false,
tagLocations: false,
}
let currentOptions: MergedParserOptions = defaultParserOptions
@ -140,13 +141,15 @@ const tokenizer = new Tokenizer(stack, {
type: NodeTypes.ELEMENT,
tag: name,
ns: currentOptions.getNamespace(name, stack[0], currentOptions.ns),
tagLoc: getLoc(start, end),
tagType: ElementTypes.ELEMENT, // will be refined on tag close
props: [],
children: [],
loc: getLoc(start - 1, end),
codegenNode: undefined,
}
if (currentOptions.tagLocations) {
currentOpenTag.openTagLoc = getLoc(start, end)
}
},
onopentagend(end) {
@ -166,7 +169,7 @@ const tokenizer = new Tokenizer(stack, {
}
for (let j = 0; j <= i; j++) {
const el = stack.shift()!
if (j === i) {
if (j === i && currentOptions.tagLocations) {
el.closeTagLoc = getLoc(start, end)
}
onCloseTag(el, end, j < i)
@ -492,7 +495,7 @@ const tokenizer = new Tokenizer(stack, {
// This regex doesn't cover the case if key or index aliases have destructuring,
// but those do not make sense in the first place, so this works in practice.
const forIteratorRE = /,([^,\}\]]*)(?:,([^,\}\]]*))?$/
const forIteratorRE = /,([^,}\]]*)(?:,([^,}\]]*))?$/
const stripParensRE = /^\(|\)$/g
function parseForExpression(