|
4 | 4 | * See LICENSE file in root directory for full license. |
5 | 5 | */ |
6 | 6 | 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" |
8 | 8 | import {debug} from "../common/debug" |
9 | 9 | import {LocationCalculator} from "../common/location-calculator" |
10 | 10 | import {ExpressionParseResult, parseExpression, parseVForExpression, parseVOnExpression} from "../script" |
@@ -61,53 +61,64 @@ function createSimpleToken(type: string, start: number, end: number, value: stri |
61 | 61 | * @returns The directive key node. |
62 | 62 | */ |
63 | 63 | 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: [], |
74 | 68 | } |
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 |
79 | 93 | } |
80 | 94 | else { |
81 | | - const colon = remain.indexOf(":") |
| 95 | + const colon = id.indexOf(":") |
82 | 96 | 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 |
85 | 100 | } |
86 | 101 | } |
87 | 102 |
|
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] |
91 | 108 | } |
92 | 109 | else { |
93 | | - argument = dotSplit[0] |
| 110 | + ret.argument = dotSplit[0] |
| 111 | + raw.argument = dotSplitRaw[0] |
94 | 112 | } |
95 | | - modifiers = dotSplit.slice(1) |
| 113 | + ret.modifiers = dotSplit.slice(1) |
| 114 | + raw.modifiers = dotSplitRaw.slice(1) |
96 | 115 |
|
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) |
99 | 119 | } |
100 | 120 |
|
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 |
111 | 122 | } |
112 | 123 |
|
113 | 124 | /** |
|
0 commit comments