Skip to content

Commit 736c0ea

Browse files
authored
Typescript: Several fixes (alecthomas#952)
1 parent e5c25d0 commit 736c0ea

File tree

5 files changed

+116
-2
lines changed

5 files changed

+116
-2
lines changed

lexers/embedded/typescript.xml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,13 @@
5151
</rule>
5252
</state>
5353
<state name="tag">
54+
<rule>
55+
<include state="commentsandwhitespace"/>
56+
</rule>
5457
<rule pattern="\s+">
5558
<token type="Text"/>
5659
</rule>
57-
<rule pattern="([\w]+\s*)(=)(\s*)">
60+
<rule pattern="([\w-]+\s*)(=)(\s*)">
5861
<bygroups>
5962
<token type="NameAttribute"/>
6063
<token type="Operator"/>
@@ -77,12 +80,25 @@
7780
<pop depth="1"/>
7881
</rule>
7982
</state>
83+
<state name="comment">
84+
<rule pattern="[^-]+">
85+
<token type="Comment"/>
86+
</rule>
87+
<rule pattern="--&gt;">
88+
<token type="Comment"/>
89+
<pop depth="1"/>
90+
</rule>
91+
<rule pattern="-">
92+
<token type="Comment"/>
93+
</rule>
94+
</state>
8095
<state name="commentsandwhitespace">
8196
<rule pattern="\s+">
8297
<token type="Text"/>
8398
</rule>
8499
<rule pattern="&lt;!--">
85100
<token type="Comment"/>
101+
<push state="comment"/>
86102
</rule>
87103
<rule pattern="//.*?\n">
88104
<token type="CommentSingle"/>
@@ -200,7 +216,7 @@
200216
<rule pattern="(Array|Boolean|Date|Error|Function|Math|Number|Object|Packages|RegExp|String|decodeURI|decodeURIComponent|encodeURI|encodeURIComponent|eval|isFinite|isNaN|parseFloat|parseInt|document|this|window)\b">
201217
<token type="NameBuiltin"/>
202218
</rule>
203-
<rule pattern="\b(module)(\s*)(\s*[\w?.$][\w?.$]*)(\s*)">
219+
<rule pattern="\b(module)(\s+)(&quot;[\w\./@]+&quot;)(\s+)">
204220
<bygroups>
205221
<token type="KeywordReserved"/>
206222
<token type="Text"/>

lexers/testdata/tsx.actual

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,19 @@ ReactDOM.render(
1010
</React.StrictMode>
1111
</>,
1212
document.getElementById('root'),
13+
)
14+
15+
ReactDOM.render(
16+
<p
17+
// This is a inline comment.
18+
/*
19+
This is a
20+
Multi line comment
21+
*/
22+
<!-- Another multiline
23+
comment !-->
24+
data-test-id="outro" disabled>
25+
Some text here.
26+
</p>,
27+
document.getElementById('root'),
1328
)

lexers/testdata/tsx.expected

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,46 @@
6060
{"type":"LiteralStringSingle","value":"'root'"},
6161
{"type":"Punctuation","value":"),"},
6262
{"type":"Text","value":"\n"},
63+
{"type":"Punctuation","value":")"},
64+
{"type":"Text","value":"\n\n"},
65+
{"type":"NameOther","value":"ReactDOM"},
66+
{"type":"Punctuation","value":"."},
67+
{"type":"NameOther","value":"render"},
68+
{"type":"Punctuation","value":"("},
69+
{"type":"Text","value":"\n "},
70+
{"type":"Punctuation","value":"\u003c"},
71+
{"type":"NameTag","value":"p"},
72+
{"type":"Text","value":"\n "},
73+
{"type":"CommentSingle","value":"// This is a inline comment.\n"},
74+
{"type":"Text","value":" "},
75+
{"type":"CommentMultiline","value":"/*\n This is a\n Multi line comment\n */"},
76+
{"type":"Text","value":"\n "},
77+
{"type":"Comment","value":"\u003c!-- Another multiline\n comment !--\u003e"},
78+
{"type":"Text","value":"\n "},
79+
{"type":"NameAttribute","value":"data-test-id"},
80+
{"type":"Operator","value":"="},
81+
{"type":"LiteralString","value":"\"outro\""},
82+
{"type":"Text","value":" "},
83+
{"type":"NameAttribute","value":"disabled"},
84+
{"type":"Punctuation","value":"\u003e"},
85+
{"type":"Text","value":"\n\t"},
86+
{"type":"NameOther","value":"Some"},
87+
{"type":"Text","value":" "},
88+
{"type":"NameOther","value":"text"},
89+
{"type":"Text","value":" "},
90+
{"type":"NameOther","value":"here"},
91+
{"type":"Punctuation","value":"."},
92+
{"type":"Text","value":"\n "},
93+
{"type":"Punctuation","value":"\u003c/"},
94+
{"type":"NameTag","value":"p"},
95+
{"type":"Punctuation","value":"\u003e,"},
96+
{"type":"Text","value":"\n "},
97+
{"type":"NameBuiltin","value":"document"},
98+
{"type":"Punctuation","value":"."},
99+
{"type":"NameOther","value":"getElementById"},
100+
{"type":"Punctuation","value":"("},
101+
{"type":"LiteralStringSingle","value":"'root'"},
102+
{"type":"Punctuation","value":"),"},
103+
{"type":"Text","value":"\n"},
63104
{"type":"Punctuation","value":")"}
64105
]

lexers/testdata/typescript.actual

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const moduleName = modules.map();
2+
3+
declare module "fs" {}
4+
declare module "@custom/plugin" {}
5+
declare module "../../compiler/types" {}

lexers/testdata/typescript.expected

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
[
2+
{"type":"KeywordReserved","value":"const"},
3+
{"type":"Text","value":" "},
4+
{"type":"NameOther","value":"moduleName"},
5+
{"type":"Text","value":" "},
6+
{"type":"Operator","value":"="},
7+
{"type":"Text","value":" "},
8+
{"type":"NameOther","value":"modules"},
9+
{"type":"Punctuation","value":"."},
10+
{"type":"NameOther","value":"map"},
11+
{"type":"Punctuation","value":"();"},
12+
{"type":"Text","value":"\n\n"},
13+
{"type":"KeywordReserved","value":"declare"},
14+
{"type":"Text","value":" "},
15+
{"type":"KeywordReserved","value":"module"},
16+
{"type":"Text","value":" "},
17+
{"type":"NameOther","value":"\"fs\""},
18+
{"type":"Text","value":" "},
19+
{"type":"Punctuation","value":"{}"},
20+
{"type":"Text","value":"\n"},
21+
{"type":"KeywordReserved","value":"declare"},
22+
{"type":"Text","value":" "},
23+
{"type":"KeywordReserved","value":"module"},
24+
{"type":"Text","value":" "},
25+
{"type":"NameOther","value":"\"@custom/plugin\""},
26+
{"type":"Text","value":" "},
27+
{"type":"Punctuation","value":"{}"},
28+
{"type":"Text","value":"\n"},
29+
{"type":"KeywordReserved","value":"declare"},
30+
{"type":"Text","value":" "},
31+
{"type":"KeywordReserved","value":"module"},
32+
{"type":"Text","value":" "},
33+
{"type":"NameOther","value":"\"../../compiler/types\""},
34+
{"type":"Text","value":" "},
35+
{"type":"Punctuation","value":"{}"},
36+
{"type":"Text","value":"\n"}
37+
]

0 commit comments

Comments
 (0)