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 all 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: 7 additions & 3 deletions components/prism-javascript.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
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': [
{
pattern: /((?:^|})\s*)(?:catch|finally)\b/,
Expand All @@ -8,11 +12,11 @@ Prism.languages.javascript = Prism.languages.extend('clike', {
],
'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}/
});

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

Prism.languages.insertBefore('javascript', 'keyword', {
'regex': {
Expand All @@ -22,7 +26,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.

10 changes: 7 additions & 3 deletions prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,10 @@ 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': [
{
pattern: /((?:^|})\s*)(?:catch|finally)\b/,
Expand All @@ -734,11 +738,11 @@ Prism.languages.javascript = Prism.languages.extend('clike', {
],
'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}/
});

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

Prism.languages.insertBefore('javascript', 'keyword', {
'regex': {
Expand All @@ -748,7 +752,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.