Skip to content

Commit

Permalink
JavaScript: Improved contextual keywords (#2713)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment committed Jan 29, 2021
1 parent 93dd83c commit 022f90a
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 27 deletions.
4 changes: 2 additions & 2 deletions components/prism-javascript.js
Expand Up @@ -8,11 +8,11 @@ Prism.languages.javascript = Prism.languages.extend('clike', {
],
'keyword': [
{
pattern: /((?:^|})\s*)(?:catch|finally)\b/,
pattern: /((?:^|})\s*)catch\b/,
lookbehind: true
},
{
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/,
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/,
lookbehind: true
},
],
Expand Down
2 changes: 1 addition & 1 deletion components/prism-javascript.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions prism.js
Expand Up @@ -1483,11 +1483,11 @@ Prism.languages.javascript = Prism.languages.extend('clike', {
],
'keyword': [
{
pattern: /((?:^|})\s*)(?:catch|finally)\b/,
pattern: /((?:^|})\s*)catch\b/,
lookbehind: true
},
{
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/,
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/,
lookbehind: true
},
],
Expand Down
198 changes: 176 additions & 22 deletions tests/languages/javascript/keyword_feature.test
@@ -1,27 +1,67 @@
catch finally;
async function;

as; await; break; case;
class; const; continue; debugger;
default; delete; do; else; enum;
export; extends; for;
from; 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;
as;
await;
break;
case;
class;
const;
continue;
debugger;
default;
delete;
do;
else;
enum;
export;
extends;
for;
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;

----------------------------------------------------
// contextual keywords

[
["keyword", "catch"],
["keyword", "finally"], ["punctuation", ";"],
try {} catch {} finally {}
try {} catch (e) {} finally {}
async function (){}
async a => {}
async (a,b,c) => {}
import {} from "foo"
import {} from 'foo'
class { get foo(){} set baz(){} get [value](){} }

["keyword", "async"],
["keyword", "function"], ["punctuation", ";"],
// variables, not keywords

const { async, from, to } = bar;
promise.catch(foo).finally(bar);

----------------------------------------------------

[
["keyword", "as"], ["punctuation", ";"],
["keyword", "await"], ["punctuation", ";"],
["keyword", "break"], ["punctuation", ";"],
Expand All @@ -38,7 +78,6 @@ with; yield;
["keyword", "export"], ["punctuation", ";"],
["keyword", "extends"], ["punctuation", ";"],
["keyword", "for"], ["punctuation", ";"],
["keyword", "from"], ["punctuation", ";"],
["keyword", "if"], ["punctuation", ";"],
["keyword", "implements"], ["punctuation", ";"],
["keyword", "import"], ["punctuation", ";"],
Expand Down Expand Up @@ -66,7 +105,122 @@ with; yield;
["keyword", "void"], ["punctuation", ";"],
["keyword", "while"], ["punctuation", ";"],
["keyword", "with"], ["punctuation", ";"],
["keyword", "yield"], ["punctuation", ";"]
["keyword", "yield"], ["punctuation", ";"],

["comment", "// contextual keywords"],

["keyword", "try"],
["punctuation", "{"],
["punctuation", "}"],
["keyword", "catch"],
["punctuation", "{"],
["punctuation", "}"],
["keyword", "finally"],
["punctuation", "{"],
["punctuation", "}"],

["keyword", "try"],
["punctuation", "{"],
["punctuation", "}"],
["keyword", "catch"],
["punctuation", "("],
"e",
["punctuation", ")"],
["punctuation", "{"],
["punctuation", "}"],
["keyword", "finally"],
["punctuation", "{"],
["punctuation", "}"],

["keyword", "async"],
["keyword", "function"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", "{"],
["punctuation", "}"],

["keyword", "async"],
["parameter", ["a"]],
["operator", "=>"],
["punctuation", "{"],
["punctuation", "}"],

["keyword", "async"],
["punctuation", "("],
["parameter", [
"a",
["punctuation", ","],
"b",
["punctuation", ","],
"c"
]],
["punctuation", ")"],
["operator", "=>"],
["punctuation", "{"],
["punctuation", "}"],

["keyword", "import"],
["punctuation", "{"],
["punctuation", "}"],
["keyword", "from"],
["string", "\"foo\""],

["keyword", "import"],
["punctuation", "{"],
["punctuation", "}"],
["keyword", "from"],
["string", "'foo'"],

["keyword", "class"],
["punctuation", "{"],
["keyword", "get"],
["function", "foo"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", "{"],
["punctuation", "}"],
["keyword", "set"],
["function", "baz"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", "{"],
["punctuation", "}"],
["keyword", "get"],
["punctuation", "["],
"value",
["punctuation", "]"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", "{"],
["punctuation", "}"],
["punctuation", "}"],

["comment", "// variables, not keywords"],

["keyword", "const"],
["punctuation", "{"],
" async",
["punctuation", ","],
" from",
["punctuation", ","],
" to ",
["punctuation", "}"],
["operator", "="],
" bar",
["punctuation", ";"],

"\r\npromise",
["punctuation", "."],
["function", "catch"],
["punctuation", "("],
"foo",
["punctuation", ")"],
["punctuation", "."],
["function", "finally"],
["punctuation", "("],
"bar",
["punctuation", ")"],
["punctuation", ";"]
]

----------------------------------------------------
Expand Down

0 comments on commit 022f90a

Please sign in to comment.