Skip to content

Commit 022f90a

Browse files
JavaScript: Improved contextual keywords (#2713)
1 parent 93dd83c commit 022f90a

File tree

4 files changed

+181
-27
lines changed

4 files changed

+181
-27
lines changed

components/prism-javascript.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ Prism.languages.javascript = Prism.languages.extend('clike', {
88
],
99
'keyword': [
1010
{
11-
pattern: /((?:^|})\s*)(?:catch|finally)\b/,
11+
pattern: /((?:^|})\s*)catch\b/,
1212
lookbehind: true
1313
},
1414
{
15-
pattern: /(^|[^.]|\.\.\.\s*)\b(?:as|async(?=\s*(?:function\b|\(|[$\w\xA0-\uFFFF]|$))|await|break|case|class|const|continue|debugger|default|delete|do|else|enum|export|extends|for|from|function|(?:get|set)(?=\s*[\[$\w\xA0-\uFFFF])|if|implements|import|in|instanceof|interface|let|new|null|of|package|private|protected|public|return|static|super|switch|this|throw|try|typeof|undefined|var|void|while|with|yield)\b/,
15+
pattern: /(^|[^.]|\.\.\.\s*)\b(?:as|async(?=\s*(?:function\b|\(|[$\w\xA0-\uFFFF]|$))|await|break|case|class|const|continue|debugger|default|delete|do|else|enum|export|extends|finally(?=\s*(?:\{|$))|for|from(?=\s*(?:['"]|$))|function|(?:get|set)(?=\s*(?:[\[$\w\xA0-\uFFFF]|$))|if|implements|import|in|instanceof|interface|let|new|null|of|package|private|protected|public|return|static|super|switch|this|throw|try|typeof|undefined|var|void|while|with|yield)\b/,
1616
lookbehind: true
1717
},
1818
],

components/prism-javascript.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

prism.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1483,11 +1483,11 @@ Prism.languages.javascript = Prism.languages.extend('clike', {
14831483
],
14841484
'keyword': [
14851485
{
1486-
pattern: /((?:^|})\s*)(?:catch|finally)\b/,
1486+
pattern: /((?:^|})\s*)catch\b/,
14871487
lookbehind: true
14881488
},
14891489
{
1490-
pattern: /(^|[^.]|\.\.\.\s*)\b(?:as|async(?=\s*(?:function\b|\(|[$\w\xA0-\uFFFF]|$))|await|break|case|class|const|continue|debugger|default|delete|do|else|enum|export|extends|for|from|function|(?:get|set)(?=\s*[\[$\w\xA0-\uFFFF])|if|implements|import|in|instanceof|interface|let|new|null|of|package|private|protected|public|return|static|super|switch|this|throw|try|typeof|undefined|var|void|while|with|yield)\b/,
1490+
pattern: /(^|[^.]|\.\.\.\s*)\b(?:as|async(?=\s*(?:function\b|\(|[$\w\xA0-\uFFFF]|$))|await|break|case|class|const|continue|debugger|default|delete|do|else|enum|export|extends|finally(?=\s*(?:\{|$))|for|from(?=\s*(?:['"]|$))|function|(?:get|set)(?=\s*(?:[\[$\w\xA0-\uFFFF]|$))|if|implements|import|in|instanceof|interface|let|new|null|of|package|private|protected|public|return|static|super|switch|this|throw|try|typeof|undefined|var|void|while|with|yield)\b/,
14911491
lookbehind: true
14921492
},
14931493
],

tests/languages/javascript/keyword_feature.test

+176-22
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,67 @@
1-
catch finally;
2-
async function;
3-
4-
as; await; break; case;
5-
class; const; continue; debugger;
6-
default; delete; do; else; enum;
7-
export; extends; for;
8-
from; if; implements;
9-
import; in; instanceof; interface; let;
10-
new; null; of; package; private;
11-
protected; public; return; static;
12-
super; switch; this; throw; try;
13-
typeof; undefined; var; void; while;
14-
with; yield;
1+
as;
2+
await;
3+
break;
4+
case;
5+
class;
6+
const;
7+
continue;
8+
debugger;
9+
default;
10+
delete;
11+
do;
12+
else;
13+
enum;
14+
export;
15+
extends;
16+
for;
17+
if;
18+
implements;
19+
import;
20+
in;
21+
instanceof;
22+
interface;
23+
let;
24+
new;
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;
1545

16-
----------------------------------------------------
46+
// contextual keywords
1747

18-
[
19-
["keyword", "catch"],
20-
["keyword", "finally"], ["punctuation", ";"],
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](){} }
2156

22-
["keyword", "async"],
23-
["keyword", "function"], ["punctuation", ";"],
57+
// variables, not keywords
58+
59+
const { async, from, to } = bar;
60+
promise.catch(foo).finally(bar);
61+
62+
----------------------------------------------------
2463

64+
[
2565
["keyword", "as"], ["punctuation", ";"],
2666
["keyword", "await"], ["punctuation", ";"],
2767
["keyword", "break"], ["punctuation", ";"],
@@ -38,7 +78,6 @@ with; yield;
3878
["keyword", "export"], ["punctuation", ";"],
3979
["keyword", "extends"], ["punctuation", ";"],
4080
["keyword", "for"], ["punctuation", ";"],
41-
["keyword", "from"], ["punctuation", ";"],
4281
["keyword", "if"], ["punctuation", ";"],
4382
["keyword", "implements"], ["punctuation", ";"],
4483
["keyword", "import"], ["punctuation", ";"],
@@ -66,7 +105,122 @@ with; yield;
66105
["keyword", "void"], ["punctuation", ";"],
67106
["keyword", "while"], ["punctuation", ";"],
68107
["keyword", "with"], ["punctuation", ";"],
69-
["keyword", "yield"], ["punctuation", ";"]
108+
["keyword", "yield"], ["punctuation", ";"],
109+
110+
["comment", "// contextual keywords"],
111+
112+
["keyword", "try"],
113+
["punctuation", "{"],
114+
["punctuation", "}"],
115+
["keyword", "catch"],
116+
["punctuation", "{"],
117+
["punctuation", "}"],
118+
["keyword", "finally"],
119+
["punctuation", "{"],
120+
["punctuation", "}"],
121+
122+
["keyword", "try"],
123+
["punctuation", "{"],
124+
["punctuation", "}"],
125+
["keyword", "catch"],
126+
["punctuation", "("],
127+
"e",
128+
["punctuation", ")"],
129+
["punctuation", "{"],
130+
["punctuation", "}"],
131+
["keyword", "finally"],
132+
["punctuation", "{"],
133+
["punctuation", "}"],
134+
135+
["keyword", "async"],
136+
["keyword", "function"],
137+
["punctuation", "("],
138+
["punctuation", ")"],
139+
["punctuation", "{"],
140+
["punctuation", "}"],
141+
142+
["keyword", "async"],
143+
["parameter", ["a"]],
144+
["operator", "=>"],
145+
["punctuation", "{"],
146+
["punctuation", "}"],
147+
148+
["keyword", "async"],
149+
["punctuation", "("],
150+
["parameter", [
151+
"a",
152+
["punctuation", ","],
153+
"b",
154+
["punctuation", ","],
155+
"c"
156+
]],
157+
["punctuation", ")"],
158+
["operator", "=>"],
159+
["punctuation", "{"],
160+
["punctuation", "}"],
161+
162+
["keyword", "import"],
163+
["punctuation", "{"],
164+
["punctuation", "}"],
165+
["keyword", "from"],
166+
["string", "\"foo\""],
167+
168+
["keyword", "import"],
169+
["punctuation", "{"],
170+
["punctuation", "}"],
171+
["keyword", "from"],
172+
["string", "'foo'"],
173+
174+
["keyword", "class"],
175+
["punctuation", "{"],
176+
["keyword", "get"],
177+
["function", "foo"],
178+
["punctuation", "("],
179+
["punctuation", ")"],
180+
["punctuation", "{"],
181+
["punctuation", "}"],
182+
["keyword", "set"],
183+
["function", "baz"],
184+
["punctuation", "("],
185+
["punctuation", ")"],
186+
["punctuation", "{"],
187+
["punctuation", "}"],
188+
["keyword", "get"],
189+
["punctuation", "["],
190+
"value",
191+
["punctuation", "]"],
192+
["punctuation", "("],
193+
["punctuation", ")"],
194+
["punctuation", "{"],
195+
["punctuation", "}"],
196+
["punctuation", "}"],
197+
198+
["comment", "// variables, not keywords"],
199+
200+
["keyword", "const"],
201+
["punctuation", "{"],
202+
" async",
203+
["punctuation", ","],
204+
" from",
205+
["punctuation", ","],
206+
" to ",
207+
["punctuation", "}"],
208+
["operator", "="],
209+
" bar",
210+
["punctuation", ";"],
211+
212+
"\r\npromise",
213+
["punctuation", "."],
214+
["function", "catch"],
215+
["punctuation", "("],
216+
"foo",
217+
["punctuation", ")"],
218+
["punctuation", "."],
219+
["function", "finally"],
220+
["punctuation", "("],
221+
"bar",
222+
["punctuation", ")"],
223+
["punctuation", ";"]
70224
]
71225

72226
----------------------------------------------------

0 commit comments

Comments
 (0)