Skip to content

Commit fe98d53

Browse files
authoredMay 1, 2021
TypeScript: Updated keywords (#2861)
1 parent 80d475e commit fe98d53

File tree

4 files changed

+300
-114
lines changed

4 files changed

+300
-114
lines changed
 

‎components/prism-typescript.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@
77
greedy: true,
88
inside: null // see below
99
},
10-
// From JavaScript Prism keyword list and TypeScript language spec: https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#221-reserved-words
11-
'keyword': /\b(?:abstract|as|asserts|async|await|break|case|catch|class|const|constructor|continue|debugger|declare|default|delete|do|else|enum|export|extends|finally|for|from|function|get|if|implements|import|in|instanceof|interface|is|keyof|let|module|namespace|new|null|of|package|private|protected|public|readonly|return|require|set|static|super|switch|this|throw|try|type|typeof|undefined|var|void|while|with|yield)\b/,
1210
'builtin': /\b(?:string|Function|any|number|boolean|Array|symbol|console|Promise|unknown|never)\b/,
1311
});
1412

13+
// The keywords TypeScript adds to JavaScript
14+
Prism.languages.typescript.keyword.push(
15+
/\b(?:abstract|as|declare|implements|is|keyof|readonly|require)\b/,
16+
// keywords that have to be followed by an identifier
17+
/\b(?:asserts|infer|interface|module|namespace|type)(?!\s*[^\s_${}*a-zA-Z\xA0-\uFFFF])/
18+
)
19+
1520
// doesn't work with TS because TS is too complex
1621
delete Prism.languages.typescript['parameter'];
1722

‎components/prism-typescript.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
export interface R<T> {
2+
data: T;
3+
total: number;
4+
type?: string;
5+
}
6+
7+
----------------------------------------------------
8+
9+
[
10+
["keyword", "export"],
11+
["keyword", "interface"],
12+
["class-name", [
13+
["constant", "R"],
14+
["operator", "<"],
15+
["constant", "T"],
16+
["operator", ">"]
17+
]],
18+
["punctuation", "{"],
19+
20+
"\r\n data",
21+
["operator", ":"],
22+
["constant", "T"],
23+
["punctuation", ";"],
24+
25+
"\r\n total",
26+
["operator", ":"],
27+
["builtin", "number"],
28+
["punctuation", ";"],
29+
30+
"\r\n type",
31+
["operator", "?"],
32+
["operator", ":"],
33+
["builtin", "string"],
34+
["punctuation", ";"],
35+
36+
["punctuation", "}"]
37+
]

‎tests/languages/typescript/keyword_feature.test

+255-111
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,275 @@
1-
abstract
2-
as
3-
asserts
4-
async
5-
await
6-
break
7-
case
8-
catch
1+
as;
2+
await;
3+
break;
4+
case;
95
class;
10-
const
11-
constructor
12-
continue
13-
debugger
14-
declare
15-
default
16-
delete
17-
do
18-
else
19-
enum
20-
export
6+
const;
7+
continue;
8+
debugger;
9+
default;
10+
delete;
11+
do;
12+
else;
13+
enum;
14+
export;
2115
extends;
22-
finally
23-
for
24-
from
25-
function
26-
get
27-
if
16+
for;
17+
if;
2818
implements;
29-
import
30-
in
19+
import;
20+
in;
3121
instanceof;
3222
interface;
33-
is
34-
keyof
35-
let
36-
module
37-
namespace
23+
let;
3824
new;
39-
null
40-
of
41-
package
42-
private
43-
protected
44-
public
45-
readonly
46-
return
47-
require
48-
set
49-
static
50-
super
51-
switch
52-
this
53-
throw
54-
try
55-
type;
56-
typeof
57-
undefined
58-
var
59-
void
60-
while
61-
with
62-
yield
25+
null;
26+
of;
27+
package;
28+
private;
29+
protected;
30+
public;
31+
return;
32+
static;
33+
super;
34+
switch;
35+
this;
36+
throw;
37+
try;
38+
typeof;
39+
undefined;
40+
var;
41+
void;
42+
while;
43+
with;
44+
yield;
45+
46+
// contextual keywords
47+
48+
try {} catch {} finally {}
49+
try {} catch (e) {} finally {}
50+
async function (){}
51+
async a => {}
52+
async (a,b,c) => {}
53+
import {} from "foo"
54+
import {} from 'foo'
55+
class { get foo(){} set baz(){} get [value](){} }
56+
57+
// variables, not keywords
58+
59+
const { async, from, to } = bar;
60+
promise.catch(foo).finally(bar);
61+
62+
// TypeScript keywords
63+
64+
abstract;
65+
as;
66+
declare;
67+
implements;
68+
is;
69+
keyof;
70+
readonly;
71+
require;
72+
73+
// contextual keywords
74+
75+
asserts foo;
76+
infer foo;
77+
interface foo;
78+
module foo;
79+
namespace foo;
80+
type foo;
81+
82+
import type { Component } from "react";
6383

6484
----------------------------------------------------
6585

6686
[
67-
["keyword", "abstract"],
68-
["keyword", "as"],
69-
["keyword", "asserts"],
70-
["keyword", "async"],
71-
["keyword", "await"],
72-
["keyword", "break"],
73-
["keyword", "case"],
74-
["keyword", "catch"],
87+
["keyword", "as"], ["punctuation", ";"],
88+
["keyword", "await"], ["punctuation", ";"],
89+
["keyword", "break"], ["punctuation", ";"],
90+
["keyword", "case"], ["punctuation", ";"],
7591
["keyword", "class"], ["punctuation", ";"],
76-
["keyword", "const"],
77-
["keyword", "constructor"],
78-
["keyword", "continue"],
79-
["keyword", "debugger"],
80-
["keyword", "declare"],
81-
["keyword", "default"],
82-
["keyword", "delete"],
83-
["keyword", "do"],
84-
["keyword", "else"],
85-
["keyword", "enum"],
86-
["keyword", "export"],
92+
["keyword", "const"], ["punctuation", ";"],
93+
["keyword", "continue"], ["punctuation", ";"],
94+
["keyword", "debugger"], ["punctuation", ";"],
95+
["keyword", "default"], ["punctuation", ";"],
96+
["keyword", "delete"], ["punctuation", ";"],
97+
["keyword", "do"], ["punctuation", ";"],
98+
["keyword", "else"], ["punctuation", ";"],
99+
["keyword", "enum"], ["punctuation", ";"],
100+
["keyword", "export"], ["punctuation", ";"],
87101
["keyword", "extends"], ["punctuation", ";"],
88-
["keyword", "finally"],
89-
["keyword", "for"],
90-
["keyword", "from"],
91-
["keyword", "function"],
92-
["keyword", "get"],
93-
["keyword", "if"],
102+
["keyword", "for"], ["punctuation", ";"],
103+
["keyword", "if"], ["punctuation", ";"],
94104
["keyword", "implements"], ["punctuation", ";"],
95-
["keyword", "import"],
96-
["keyword", "in"],
105+
["keyword", "import"], ["punctuation", ";"],
106+
["keyword", "in"], ["punctuation", ";"],
97107
["keyword", "instanceof"], ["punctuation", ";"],
98108
["keyword", "interface"], ["punctuation", ";"],
99-
["keyword", "is"],
100-
["keyword", "keyof"],
101-
["keyword", "let"],
102-
["keyword", "module"],
103-
["keyword", "namespace"],
109+
["keyword", "let"], ["punctuation", ";"],
104110
["keyword", "new"], ["punctuation", ";"],
105-
["keyword", "null"],
106-
["keyword", "of"],
107-
["keyword", "package"],
108-
["keyword", "private"],
109-
["keyword", "protected"],
110-
["keyword", "public"],
111-
["keyword", "readonly"],
112-
["keyword", "return"],
113-
["keyword", "require"],
114-
["keyword", "set"],
115-
["keyword", "static"],
116-
["keyword", "super"],
117-
["keyword", "switch"],
118-
["keyword", "this"],
119-
["keyword", "throw"],
111+
["keyword", "null"], ["punctuation", ";"],
112+
["keyword", "of"], ["punctuation", ";"],
113+
["keyword", "package"], ["punctuation", ";"],
114+
["keyword", "private"], ["punctuation", ";"],
115+
["keyword", "protected"], ["punctuation", ";"],
116+
["keyword", "public"], ["punctuation", ";"],
117+
["keyword", "return"], ["punctuation", ";"],
118+
["keyword", "static"], ["punctuation", ";"],
119+
["keyword", "super"], ["punctuation", ";"],
120+
["keyword", "switch"], ["punctuation", ";"],
121+
["keyword", "this"], ["punctuation", ";"],
122+
["keyword", "throw"], ["punctuation", ";"],
123+
["keyword", "try"], ["punctuation", ";"],
124+
["keyword", "typeof"], ["punctuation", ";"],
125+
["keyword", "undefined"], ["punctuation", ";"],
126+
["keyword", "var"], ["punctuation", ";"],
127+
["keyword", "void"], ["punctuation", ";"],
128+
["keyword", "while"], ["punctuation", ";"],
129+
["keyword", "with"], ["punctuation", ";"],
130+
["keyword", "yield"], ["punctuation", ";"],
131+
132+
["comment", "// contextual keywords"],
133+
120134
["keyword", "try"],
121-
["keyword", "type"], ["punctuation", ";"],
122-
["keyword", "typeof"],
123-
["keyword", "undefined"],
124-
["keyword", "var"],
125-
["keyword", "void"],
126-
["keyword", "while"],
127-
["keyword", "with"],
128-
["keyword", "yield"]
135+
["punctuation", "{"],
136+
["punctuation", "}"],
137+
["keyword", "catch"],
138+
["punctuation", "{"],
139+
["punctuation", "}"],
140+
["keyword", "finally"],
141+
["punctuation", "{"],
142+
["punctuation", "}"],
143+
144+
["keyword", "try"],
145+
["punctuation", "{"],
146+
["punctuation", "}"],
147+
["keyword", "catch"],
148+
["punctuation", "("],
149+
"e",
150+
["punctuation", ")"],
151+
["punctuation", "{"],
152+
["punctuation", "}"],
153+
["keyword", "finally"],
154+
["punctuation", "{"],
155+
["punctuation", "}"],
156+
157+
["keyword", "async"],
158+
["keyword", "function"],
159+
["punctuation", "("],
160+
["punctuation", ")"],
161+
["punctuation", "{"],
162+
["punctuation", "}"],
163+
164+
["keyword", "async"],
165+
" a ",
166+
["operator", "=>"],
167+
["punctuation", "{"],
168+
["punctuation", "}"],
169+
170+
["keyword", "async"],
171+
["punctuation", "("],
172+
"a",
173+
["punctuation", ","],
174+
"b",
175+
["punctuation", ","],
176+
"c",
177+
["punctuation", ")"],
178+
["operator", "=>"],
179+
["punctuation", "{"],
180+
["punctuation", "}"],
181+
182+
["keyword", "import"],
183+
["punctuation", "{"],
184+
["punctuation", "}"],
185+
["keyword", "from"],
186+
["string", "\"foo\""],
187+
188+
["keyword", "import"],
189+
["punctuation", "{"],
190+
["punctuation", "}"],
191+
["keyword", "from"],
192+
["string", "'foo'"],
193+
194+
["keyword", "class"],
195+
["punctuation", "{"],
196+
["keyword", "get"],
197+
["function", "foo"],
198+
["punctuation", "("],
199+
["punctuation", ")"],
200+
["punctuation", "{"],
201+
["punctuation", "}"],
202+
["keyword", "set"],
203+
["function", "baz"],
204+
["punctuation", "("],
205+
["punctuation", ")"],
206+
["punctuation", "{"],
207+
["punctuation", "}"],
208+
["keyword", "get"],
209+
["punctuation", "["],
210+
"value",
211+
["punctuation", "]"],
212+
["punctuation", "("],
213+
["punctuation", ")"],
214+
["punctuation", "{"],
215+
["punctuation", "}"],
216+
["punctuation", "}"],
217+
218+
["comment", "// variables, not keywords"],
219+
220+
["keyword", "const"],
221+
["punctuation", "{"],
222+
" async",
223+
["punctuation", ","],
224+
" from",
225+
["punctuation", ","],
226+
" to ",
227+
["punctuation", "}"],
228+
["operator", "="],
229+
" bar",
230+
["punctuation", ";"],
231+
232+
"\r\npromise",
233+
["punctuation", "."],
234+
["function", "catch"],
235+
["punctuation", "("],
236+
"foo",
237+
["punctuation", ")"],
238+
["punctuation", "."],
239+
["function", "finally"],
240+
["punctuation", "("],
241+
"bar",
242+
["punctuation", ")"],
243+
["punctuation", ";"],
244+
245+
["comment", "// TypeScript keywords"],
246+
247+
["keyword", "abstract"], ["punctuation", ";"],
248+
["keyword", "as"], ["punctuation", ";"],
249+
["keyword", "declare"], ["punctuation", ";"],
250+
["keyword", "implements"], ["punctuation", ";"],
251+
["keyword", "is"], ["punctuation", ";"],
252+
["keyword", "keyof"], ["punctuation", ";"],
253+
["keyword", "readonly"], ["punctuation", ";"],
254+
["keyword", "require"], ["punctuation", ";"],
255+
256+
["comment", "// contextual keywords"],
257+
258+
["keyword", "asserts"], " foo", ["punctuation", ";"],
259+
["keyword", "infer"], " foo", ["punctuation", ";"],
260+
["keyword", "interface"], ["class-name", ["foo"]], ["punctuation", ";"],
261+
["keyword", "module"], " foo", ["punctuation", ";"],
262+
["keyword", "namespace"], " foo", ["punctuation", ";"],
263+
["keyword", "type"], ["class-name", ["foo"]], ["punctuation", ";"],
264+
265+
["keyword", "import"],
266+
["keyword", "type"],
267+
["punctuation", "{"],
268+
" Component ",
269+
["punctuation", "}"],
270+
["keyword", "from"],
271+
["string", "\"react\""],
272+
["punctuation", ";"]
129273
]
130274

131275
----------------------------------------------------

0 commit comments

Comments
 (0)
Please sign in to comment.