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: Highlighting of supposed classes and functions #1482

Merged
merged 7 commits into from
Aug 19, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
8 changes: 6 additions & 2 deletions components/prism-javascript.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
Prism.languages.javascript = Prism.languages.extend('clike', {
'class-name': [
Prism.languages.clike['class-name'],
/[_$A-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\.(?:prototype|constructor))/
Copy link
Member

Choose a reason for hiding this comment

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

Tests for prototype / constructor?

],
'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/,
'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,
'function': /[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*\(|\.(?:apply|bind|call)\()/,
Copy link
Member

Choose a reason for hiding this comment

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

Can we get a test for the apply / bind / call cases?

'operator': /-[-=]?|\+[+=]?|!=?=?|<<?=?|>>?>?=?|=(?:==?|>)?|&[&=]?|\|[|=]?|\*\*?=?|\/=?|~|\^=?|%=?|\?|\.{3}/
});

Expand All @@ -14,7 +18,7 @@ Prism.languages.insertBefore('javascript', 'keyword', {
},
// This must be declared before keyword because we use "function" inside the look-forward
'function-variable': {
pattern: /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*=\s*(?:function\b|(?:\([^()]*\)|[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)\s*=>))/i,
pattern: /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*[=:]\s*(?:function\b|(?:\([^()]*\)|[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)\s*=>))/i,
alias: 'function'
},
'constant': /\b[A-Z][A-Z\d_]*\b/
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.

8 changes: 6 additions & 2 deletions prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -722,10 +722,14 @@ Prism.languages.clike = {
********************************************** */

Prism.languages.javascript = Prism.languages.extend('clike', {
'class-name': [
Prism.languages.clike['class-name'],
/[_$A-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\.(?:prototype|constructor))/
],
'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/,
'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,
'function': /[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*\(|\.(?:apply|bind|call)\()/,
'operator': /-[-=]?|\+[+=]?|!=?=?|<<?=?|>>?>?=?|=(?:==?|>)?|&[&=]?|\|[|=]?|\*\*?=?|\/=?|~|\^=?|%=?|\?|\.{3}/
});

Expand All @@ -737,7 +741,7 @@ Prism.languages.insertBefore('javascript', 'keyword', {
},
// This must be declared before keyword because we use "function" inside the look-forward
'function-variable': {
pattern: /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*=\s*(?:function\b|(?:\([^()]*\)|[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)\s*=>))/i,
pattern: /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*[=:]\s*(?:function\b|(?:\([^()]*\)|[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)\s*=>))/i,
alias: 'function'
},
'constant': /\b[A-Z][A-Z\d_]*\b/
Expand Down
5 changes: 4 additions & 1 deletion tests/languages/javascript/function-variable_feature.test
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
foo = function () {}
{foo: function () {}}
bar = function baz () {}
fooBar = x => x
ಠ_ಠ = () => {}
Expand All @@ -9,6 +10,8 @@ fooBar = x => x
[
["function-variable", "foo"], ["operator", "="], ["keyword", "function"],
["punctuation", "("], ["punctuation", ")"], ["punctuation", "{"], ["punctuation", "}"],
["punctuation", "{"], ["function-variable", "foo"], ["punctuation", ":"], ["keyword", "function"],
["punctuation", "("], ["punctuation", ")"], ["punctuation", "{"],["punctuation","}"],["punctuation","}"],
["function-variable", "bar"], ["operator", "="], ["keyword", "function"], ["function", "baz"],
["punctuation", "("], ["punctuation", ")"], ["punctuation", "{"], ["punctuation", "}"],
["function-variable", "fooBar"], ["operator", "="], " x ", ["operator", "=>"], " x\r\n",
Expand All @@ -21,4 +24,4 @@ fooBar = x => x

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

Checks for variables obviously containing functions.
Checks for variables obviously containing functions.
22 changes: 22 additions & 0 deletions tests/languages/javascript/supposed-classes_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Foo.prototype.bar;
Bar.constructor;

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

[
["class-name", "Foo"],
["punctuation", "."],
"prototype",
["punctuation", "."],
"bar",
["punctuation", ";"],

["class-name", "Bar"],
["punctuation", "."],
"constructor",
["punctuation", ";"]
]

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

Checks for supposed function variables based on standard function invocations or modifications.
38 changes: 38 additions & 0 deletions tests/languages/javascript/supposed-function_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
foo.apply(thisArg, args);
bar.call(...args);
fooBar.bind(thisArg);

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

[
["function", "foo"],
["punctuation", "."],
["function", "apply"],
["punctuation", "("],
"thisArg",
["punctuation", ","],
" args",
["punctuation", ")"],
["punctuation", ";"],

["function", "bar"],
["punctuation", "."],
["function", "call"],
["punctuation", "("],
["operator", "..."],
"args",
["punctuation", ")"],
["punctuation", ";"],

["function", "fooBar"],
["punctuation", "."],
["function", "bind"],
["punctuation", "("],
"thisArg",
["punctuation", ")"],
["punctuation", ";"]
]

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

Checks for supposed function variables based on standard function invocations or modifications.