Skip to content

Commit 90531d5

Browse files
committed
Vue: Better parsing of v- directives
- The parsing of `v-` directives expected that this always ended in `">`, however it is possible that there are other attributes after an `v-` directive also it made the assumption that there couldn't be any spaces in the value of the directive. Therefore it could result in incorrect lexing where almost the whole file could be marked as an LiteralString. - Explicitely mark `=` as an operator token. - Tests added. - Ref: https://codeberg.org/forgejo/forgejo/issues/2945
1 parent 32c053f commit 90531d5

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

Diff for: lexers/embedded/vue.xml

+4-2
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,10 @@
8383
<token type="LiteralString"/>
8484
</bygroups>
8585
</rule>
86-
<rule pattern="(:[\S]+)(=&#34;[\S]+&#34;)">
86+
<rule pattern="(:[\S]+)(=)(&#34;[\S]+&#34;)">
8787
<bygroups>
8888
<token type="NameTag"/>
89+
<token type="Operator"/>
8990
<token type="LiteralString"/>
9091
</bygroups>
9192
</rule>
@@ -104,9 +105,10 @@
104105
<token type="Punctuation"/>
105106
</bygroups>
106107
</rule>
107-
<rule pattern="(v-[\w]+)(=&#34;[\S]+&#34;)(&gt;)">
108+
<rule pattern="(v-[\w]+)(=)(&#34;[\S| ]+&#34;)(&gt;|\s)">
108109
<bygroups>
109110
<token type="NameTag"/>
111+
<token type="Operator"/>
110112
<token type="LiteralString"/>
111113
<token type="Punctuation"/>
112114
</bygroups>

Diff for: lexers/testdata/vue.actual

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
<template>
22
<button class="button">This is MyButton</button>
3+
<ul v-if="examples.length" bordered>
4+
<li
5+
v-for="example in examples"
6+
:key="`${example.id}`"
7+
/>
8+
</ul>
39
</template>
410

511
<script>

Diff for: lexers/testdata/vue.expected

+28
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,34 @@
1818
{"type":"Punctuation","value":"\u003c/"},
1919
{"type":"NameTag","value":"button"},
2020
{"type":"Punctuation","value":"\u003e"},
21+
{"type":"Text","value":"\n "},
22+
{"type":"Punctuation","value":"\u003c"},
23+
{"type":"NameTag","value":"ul"},
24+
{"type":"Text","value":" "},
25+
{"type":"NameTag","value":"v-if"},
26+
{"type":"Operator","value":"="},
27+
{"type":"LiteralString","value":"\"examples.length\""},
28+
{"type":"Punctuation","value":" "},
29+
{"type":"NameAttribute","value":"bordered"},
30+
{"type":"Punctuation","value":"\u003e"},
31+
{"type":"Text","value":"\n "},
32+
{"type":"Punctuation","value":"\u003c"},
33+
{"type":"NameTag","value":"li"},
34+
{"type":"Text","value":"\n "},
35+
{"type":"NameTag","value":"v-for"},
36+
{"type":"Operator","value":"="},
37+
{"type":"LiteralString","value":"\"example in examples\""},
38+
{"type":"Punctuation","value":"\n"},
39+
{"type":"Text","value":" "},
40+
{"type":"NameTag","value":":key"},
41+
{"type":"Operator","value":"="},
42+
{"type":"LiteralString","value":"\"`${example.id}`\""},
43+
{"type":"Text","value":"\n "},
44+
{"type":"Punctuation","value":"/\u003e"},
45+
{"type":"Text","value":"\n "},
46+
{"type":"Punctuation","value":"\u003c/"},
47+
{"type":"NameTag","value":"ul"},
48+
{"type":"Punctuation","value":"\u003e"},
2149
{"type":"Text","value":"\n"},
2250
{"type":"Punctuation","value":"\u003c/"},
2351
{"type":"NameTag","value":"template"},

0 commit comments

Comments
 (0)