Skip to content

Commit 945f453

Browse files
committed
Update: add raw names to AST
1 parent 446f7bf commit 945f453

File tree

83 files changed

+1366
-124
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+1366
-124
lines changed

src/ast/nodes.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -627,18 +627,23 @@ export interface VIdentifier extends HasLocation, HasParent {
627627
type: "VIdentifier"
628628
parent: VAttribute
629629
name: string
630+
raw: string
631+
}
632+
633+
export interface DirectiveKeyParts {
634+
name: string
635+
argument: string | null
636+
modifiers: string[]
630637
}
631638

632639
/**
633640
* Attribute name nodes.
634641
*/
635-
export interface VDirectiveKey extends HasLocation, HasParent {
642+
export interface VDirectiveKey extends HasLocation, HasParent, DirectiveKeyParts {
636643
type: "VDirectiveKey"
637644
parent: VAttribute
638-
name: string
639-
argument: string | null
640-
modifiers: string[]
641645
shorthand: boolean
646+
raw: DirectiveKeyParts
642647
}
643648

644649
/**
@@ -706,6 +711,7 @@ export interface VElement extends HasLocation, HasParent {
706711
parent: VDocumentFragment | VElement
707712
namespace: Namespace
708713
name: string
714+
rawName: string
709715
startTag: VStartTag
710716
children: (VElement | VText | VExpressionContainer)[]
711717
endTag: VEndTag | null

src/html/intermediate-tokenizer.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export type IntermediateToken = StartTag | EndTag | Text | Mustache
3131
export interface StartTag extends HasLocation {
3232
type: "StartTag"
3333
name: string
34+
rawName: string
3435
selfClosing: boolean
3536
attributes: VAttribute[]
3637
}
@@ -358,6 +359,7 @@ export class IntermediateTokenizer {
358359
loc: {start: token.loc.start, end: token.loc.end},
359360
parent: DUMMY_PARENT,
360361
name: token.value,
362+
raw: this.text.slice(token.range[0], token.range[1]),
361363
},
362364
value: null,
363365
}
@@ -473,6 +475,7 @@ export class IntermediateTokenizer {
473475
range: [token.range[0], token.range[1]],
474476
loc: {start: token.loc.start, end: token.loc.end},
475477
name: token.value,
478+
rawName: this.text.slice(token.range[0] + 1, token.range[1]),
476479
selfClosing: false,
477480
attributes: [],
478481
}

src/html/parser.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ export class Parser {
344344
loc: {start: token.loc.start, end: token.loc.end},
345345
parent,
346346
name: adjustElementName(token.name, namespace),
347+
rawName: token.rawName,
347348
namespace,
348349
startTag: {
349350
type: "VStartTag",

src/template/index.ts

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* See LICENSE file in root directory for full license.
55
*/
66
import * as lodash from "lodash"
7-
import {ParseError, Reference, Token, Variable, VAttribute, VDirective, VDirectiveKey, VDocumentFragment, VExpressionContainer, VIdentifier, VLiteral, VNode} from "../ast"
7+
import {DirectiveKeyParts, ParseError, Reference, Token, Variable, VAttribute, VDirective, VDirectiveKey, VDocumentFragment, VExpressionContainer, VIdentifier, VLiteral, VNode} from "../ast"
88
import {debug} from "../common/debug"
99
import {LocationCalculator} from "../common/location-calculator"
1010
import {ExpressionParseResult, parseExpression, parseVForExpression, parseVOnExpression} from "../script"
@@ -61,53 +61,64 @@ function createSimpleToken(type: string, start: number, end: number, value: stri
6161
* @returns The directive key node.
6262
*/
6363
function createDirectiveKey(node: VIdentifier): VDirectiveKey {
64-
let name = null
65-
let argument = null
66-
let modifiers = null
67-
let shorthand = false
68-
let remain = node.name
69-
70-
if (remain.startsWith(":")) {
71-
name = "bind"
72-
shorthand = true
73-
remain = remain.slice(1)
64+
const raw: DirectiveKeyParts = {
65+
name: "",
66+
argument: null,
67+
modifiers: [],
7468
}
75-
else if (remain.startsWith("@")) {
76-
name = "on"
77-
shorthand = true
78-
remain = remain.slice(1)
69+
const ret: VDirectiveKey = {
70+
type: "VDirectiveKey",
71+
range: node.range,
72+
loc: node.loc,
73+
parent: node.parent,
74+
name: "",
75+
argument: null,
76+
modifiers: [],
77+
shorthand: false,
78+
raw,
79+
}
80+
const id = node.name
81+
const rawId = node.raw
82+
let i = 0
83+
84+
if (node.name.startsWith(":")) {
85+
ret.name = raw.name = "bind"
86+
ret.shorthand = true
87+
i = 1
88+
}
89+
else if (id.startsWith("@")) {
90+
ret.name = raw.name = "on"
91+
ret.shorthand = true
92+
i = 1
7993
}
8094
else {
81-
const colon = remain.indexOf(":")
95+
const colon = id.indexOf(":")
8296
if (colon !== -1) {
83-
name = remain.slice(0, colon)
84-
remain = remain.slice(colon + 1)
97+
ret.name = id.slice(0, colon)
98+
raw.name = rawId.slice(0, colon)
99+
i = colon + 1
85100
}
86101
}
87102

88-
const dotSplit = remain.split(".")
89-
if (name == null) {
90-
name = dotSplit[0]
103+
const dotSplit = id.slice(i).split(".")
104+
const dotSplitRaw = rawId.slice(i).split(".")
105+
if (ret.name === "") {
106+
ret.name = dotSplit[0]
107+
raw.name = dotSplitRaw[0]
91108
}
92109
else {
93-
argument = dotSplit[0]
110+
ret.argument = dotSplit[0]
111+
raw.argument = dotSplitRaw[0]
94112
}
95-
modifiers = dotSplit.slice(1)
113+
ret.modifiers = dotSplit.slice(1)
114+
raw.modifiers = dotSplitRaw.slice(1)
96115

97-
if (name.startsWith("v-")) {
98-
name = name.slice(2)
116+
if (ret.name.startsWith("v-")) {
117+
ret.name = ret.name.slice(2)
118+
raw.name = raw.name.slice(2)
99119
}
100120

101-
return {
102-
type: "VDirectiveKey",
103-
range: node.range,
104-
loc: node.loc,
105-
parent: node.parent,
106-
name,
107-
argument,
108-
modifiers,
109-
shorthand,
110-
}
121+
return ret
111122
}
112123

113124
/**

test/fixtures/ast/attributes/ast.json

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
}
3838
},
3939
"name": "template",
40+
"rawName": "template",
4041
"namespace": "http://www.w3.org/1999/xhtml",
4142
"startTag": {
4243
"type": "VStartTag",
@@ -92,6 +93,7 @@
9293
}
9394
},
9495
"name": "div",
96+
"rawName": "div",
9597
"namespace": "http://www.w3.org/1999/xhtml",
9698
"startTag": {
9799
"type": "VStartTag",
@@ -143,7 +145,8 @@
143145
"column": 10
144146
}
145147
},
146-
"name": "a"
148+
"name": "a",
149+
"raw": "a"
147150
},
148151
"value": null
149152
}
@@ -204,6 +207,7 @@
204207
}
205208
},
206209
"name": "div",
210+
"rawName": "div",
207211
"namespace": "http://www.w3.org/1999/xhtml",
208212
"startTag": {
209213
"type": "VStartTag",
@@ -255,7 +259,8 @@
255259
"column": 10
256260
}
257261
},
258-
"name": "a"
262+
"name": "a",
263+
"raw": "a"
259264
},
260265
"value": null
261266
}
@@ -316,6 +321,7 @@
316321
}
317322
},
318323
"name": "div",
324+
"rawName": "div",
319325
"namespace": "http://www.w3.org/1999/xhtml",
320326
"startTag": {
321327
"type": "VStartTag",
@@ -367,7 +373,8 @@
367373
"column": 10
368374
}
369375
},
370-
"name": "a"
376+
"name": "a",
377+
"raw": "a"
371378
},
372379
"value": {
373380
"type": "VLiteral",
@@ -445,6 +452,7 @@
445452
}
446453
},
447454
"name": "div",
455+
"rawName": "div",
448456
"namespace": "http://www.w3.org/1999/xhtml",
449457
"startTag": {
450458
"type": "VStartTag",
@@ -496,7 +504,8 @@
496504
"column": 10
497505
}
498506
},
499-
"name": "a"
507+
"name": "a",
508+
"raw": "a"
500509
},
501510
"value": {
502511
"type": "VLiteral",
@@ -574,6 +583,7 @@
574583
}
575584
},
576585
"name": "div",
586+
"rawName": "div",
577587
"namespace": "http://www.w3.org/1999/xhtml",
578588
"startTag": {
579589
"type": "VStartTag",
@@ -625,7 +635,8 @@
625635
"column": 10
626636
}
627637
},
628-
"name": "a"
638+
"name": "a",
639+
"raw": "a"
629640
},
630641
"value": {
631642
"type": "VLiteral",
@@ -703,6 +714,7 @@
703714
}
704715
},
705716
"name": "div",
717+
"rawName": "div",
706718
"namespace": "http://www.w3.org/1999/xhtml",
707719
"startTag": {
708720
"type": "VStartTag",
@@ -754,7 +766,8 @@
754766
"column": 10
755767
}
756768
},
757-
"name": "a"
769+
"name": "a",
770+
"raw": "a"
758771
},
759772
"value": {
760773
"type": "VLiteral",

test/fixtures/ast/auto-closing-dt-dd/ast.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
}
3838
},
3939
"name": "template",
40+
"rawName": "template",
4041
"namespace": "http://www.w3.org/1999/xhtml",
4142
"startTag": {
4243
"type": "VStartTag",
@@ -92,6 +93,7 @@
9293
}
9394
},
9495
"name": "dl",
96+
"rawName": "dl",
9597
"namespace": "http://www.w3.org/1999/xhtml",
9698
"startTag": {
9799
"type": "VStartTag",
@@ -147,6 +149,7 @@
147149
}
148150
},
149151
"name": "dt",
152+
"rawName": "dt",
150153
"namespace": "http://www.w3.org/1999/xhtml",
151154
"startTag": {
152155
"type": "VStartTag",
@@ -206,6 +209,7 @@
206209
}
207210
},
208211
"name": "dd",
212+
"rawName": "dd",
209213
"namespace": "http://www.w3.org/1999/xhtml",
210214
"startTag": {
211215
"type": "VStartTag",
@@ -265,6 +269,7 @@
265269
}
266270
},
267271
"name": "dt",
272+
"rawName": "dt",
268273
"namespace": "http://www.w3.org/1999/xhtml",
269274
"startTag": {
270275
"type": "VStartTag",
@@ -324,6 +329,7 @@
324329
}
325330
},
326331
"name": "dd",
332+
"rawName": "dd",
327333
"namespace": "http://www.w3.org/1999/xhtml",
328334
"startTag": {
329335
"type": "VStartTag",

test/fixtures/ast/auto-closing-p/ast.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
}
3838
},
3939
"name": "template",
40+
"rawName": "template",
4041
"namespace": "http://www.w3.org/1999/xhtml",
4142
"startTag": {
4243
"type": "VStartTag",
@@ -92,6 +93,7 @@
9293
}
9394
},
9495
"name": "p",
96+
"rawName": "p",
9597
"namespace": "http://www.w3.org/1999/xhtml",
9698
"startTag": {
9799
"type": "VStartTag",
@@ -151,6 +153,7 @@
151153
}
152154
},
153155
"name": "p",
156+
"rawName": "p",
154157
"namespace": "http://www.w3.org/1999/xhtml",
155158
"startTag": {
156159
"type": "VStartTag",
@@ -210,6 +213,7 @@
210213
}
211214
},
212215
"name": "hr",
216+
"rawName": "hr",
213217
"namespace": "http://www.w3.org/1999/xhtml",
214218
"startTag": {
215219
"type": "VStartTag",
@@ -268,6 +272,7 @@
268272
}
269273
},
270274
"name": "p",
275+
"rawName": "p",
271276
"namespace": "http://www.w3.org/1999/xhtml",
272277
"startTag": {
273278
"type": "VStartTag",
@@ -327,6 +332,7 @@
327332
}
328333
},
329334
"name": "div",
335+
"rawName": "div",
330336
"namespace": "http://www.w3.org/1999/xhtml",
331337
"startTag": {
332338
"type": "VStartTag",

test/fixtures/ast/bogus-comments/ast.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
}
3838
},
3939
"name": "template",
40+
"rawName": "template",
4041
"namespace": "http://www.w3.org/1999/xhtml",
4142
"startTag": {
4243
"type": "VStartTag",

0 commit comments

Comments
 (0)