Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS: Updated catch and finally #1527

Merged
merged 4 commits into from
Aug 19, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion components/prism-javascript.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
Prism.languages.javascript = Prism.languages.extend('clike', {
'keyword': /\b(?:as|async|await|break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|export|extends|finally|for|from|function|get|if|implements|import|in|instanceof|interface|let|new|null|of|package|private|protected|public|return|set|static|super|switch|this|throw|try|typeof|var|void|while|with|yield)\b/,
'keyword': [
{
pattern: /(^|}\s*)(?:catch|finally)\b/,
lookbehind: true
},
/\b(?:as|async|await|break|case|class|const|continue|debugger|default|delete|do|else|enum|export|extends|for|from|function|get|if|implements|import|in|instanceof|interface|let|new|null|of|package|private|protected|public|return|set|static|super|switch|this|throw|try|typeof|var|void|while|with|yield)\b/
],
'number': /\b(?:0[xX][\dA-Fa-f]+|0[bB][01]+|0[oO][0-7]+|NaN|Infinity)\b|(?:\b\d+\.?\d*|\B\.\d+)(?:[Ee][+-]?\d+)?/,
// Allow for all non-ASCII characters (See http://stackoverflow.com/a/2008444)
'function': /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*\()/i,
'operator': /-[-=]?|\+[+=]?|!=?=?|<<?=?|>>?>?=?|=(?:==?|>)?|&[&=]?|\|[|=]?|\*\*?=?|\/=?|~|\^=?|%=?|\?|\.{3}/
});

Prism.languages.javascript['class-name'].pattern = /(\b(?:class|interface|extends|implements|instanceof|new)\s+)[\w.\\]+/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the story with this line in this PR?

Copy link
Member Author

@RunDevelopment RunDevelopment Aug 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line changes the class-name pattern to remove catch to no longer highlight the exception variable as a class name. (I added a new test for that)
trait was also removed, because this isn't a keyword in JS. (I forgot to mention it)

I didn't overwrite class-name in extend because it would have required more code to do so. (lookbehind and inside)
Or should I do it anyway for better readability?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I think this is fine. Maybe a comment would be helpful?

Could we also get a test for promise().catch().finally() to make sure they're caught as functions?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, we have that already, my bad.


Prism.languages.insertBefore('javascript', 'keyword', {
'regex': {
pattern: /((?:^|[^$\w\xA0-\uFFFF."'\])\s])\s*)\/(\[[^\]\r\n]+]|\\.|[^/\\\[\r\n])+\/[gimyu]{0,5}(?=\s*($|[\r\n,.;})\]]))/,
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.

10 changes: 9 additions & 1 deletion prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -725,13 +725,21 @@ Prism.languages.clike = {
********************************************** */

Prism.languages.javascript = Prism.languages.extend('clike', {
'keyword': /\b(?:as|async|await|break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|export|extends|finally|for|from|function|get|if|implements|import|in|instanceof|interface|let|new|null|of|package|private|protected|public|return|set|static|super|switch|this|throw|try|typeof|var|void|while|with|yield)\b/,
'keyword': [
{
pattern: /(^|}\s*)(?:catch|finally)\b/,
lookbehind: true
},
/\b(?:as|async|await|break|case|class|const|continue|debugger|default|delete|do|else|enum|export|extends|for|from|function|get|if|implements|import|in|instanceof|interface|let|new|null|of|package|private|protected|public|return|set|static|super|switch|this|throw|try|typeof|var|void|while|with|yield)\b/
],
'number': /\b(?:0[xX][\dA-Fa-f]+|0[bB][01]+|0[oO][0-7]+|NaN|Infinity)\b|(?:\b\d+\.?\d*|\B\.\d+)(?:[Ee][+-]?\d+)?/,
// Allow for all non-ASCII characters (See http://stackoverflow.com/a/2008444)
'function': /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*\()/i,
'operator': /-[-=]?|\+[+=]?|!=?=?|<<?=?|>>?>?=?|=(?:==?|>)?|&[&=]?|\|[|=]?|\*\*?=?|\/=?|~|\^=?|%=?|\?|\.{3}/
});

Prism.languages.javascript['class-name'].pattern = /(\b(?:class|interface|extends|implements|instanceof|new)\s+)[\w.\\]+/

Prism.languages.insertBefore('javascript', 'keyword', {
'regex': {
pattern: /((?:^|[^$\w\xA0-\uFFFF."'\])\s])\s*)\/(\[[^\]\r\n]+]|\\.|[^/\\\[\r\n])+\/[gimyu]{0,5}(?=\s*($|[\r\n,.;})\]]))/,
Expand Down
35 changes: 35 additions & 0 deletions tests/languages/javascript/issue1526.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
fetch('some-resource.json')
.then(response => response.json())
.catch(console.error);

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

[
["function", "fetch"],
["punctuation", "("],
["string", "'some-resource.json'"],
["punctuation", ")"],
["punctuation", "."],
["function", "then"],
["punctuation", "("],
"response ",
["operator", "=>"],
" response",
["punctuation", "."],
["function", "json"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", ")"],
["punctuation", "."],
["function", "catch"],
["punctuation", "("],
"console",
["punctuation", "."],
"error",
["punctuation", ")"],
["punctuation", ";"]
]

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

Checks for catch function which is not a keyword. See #1526
19 changes: 11 additions & 8 deletions tests/languages/javascript/keyword_feature.test
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
as; async; await; break; case;
catch; class; const; continue; debugger;
class; const; continue; debugger;
default; delete; do; else; enum;
export; extends; finally; for;
export; extends; for;
from; function; get; if; implements;
import; in; instanceof; interface; let;
new; null; of; package; private;
protected; public; return; set; static;
super; switch; this; throw;
try; typeof; var; void; while;
typeof; var; void; while;
with; yield;

try {} catch (e) {} finally {}

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

[
Expand All @@ -18,7 +20,6 @@ with; yield;
["keyword", "await"], ["punctuation", ";"],
["keyword", "break"], ["punctuation", ";"],
["keyword", "case"], ["punctuation", ";"],
["keyword", "catch"], ["punctuation", ";"],
["keyword", "class"], ["punctuation", ";"],
["keyword", "const"], ["punctuation", ";"],
["keyword", "continue"], ["punctuation", ";"],
Expand All @@ -30,7 +31,6 @@ with; yield;
["keyword", "enum"], ["punctuation", ";"],
["keyword", "export"], ["punctuation", ";"],
["keyword", "extends"], ["punctuation", ";"],
["keyword", "finally"], ["punctuation", ";"],
["keyword", "for"], ["punctuation", ";"],
["keyword", "from"], ["punctuation", ";"],
["keyword", "function"], ["punctuation", ";"],
Expand All @@ -56,15 +56,18 @@ with; yield;
["keyword", "switch"], ["punctuation", ";"],
["keyword", "this"], ["punctuation", ";"],
["keyword", "throw"], ["punctuation", ";"],
["keyword", "try"], ["punctuation", ";"],
["keyword", "typeof"], ["punctuation", ";"],
["keyword", "var"], ["punctuation", ";"],
["keyword", "void"], ["punctuation", ";"],
["keyword", "while"], ["punctuation", ";"],
["keyword", "with"], ["punctuation", ";"],
["keyword", "yield"], ["punctuation", ";"]
["keyword", "yield"], ["punctuation", ";"],

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

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

Checks for all keywords.
Checks for all keywords.