Skip to content

Commit

Permalink
Highlighting of supposed classes and functions (#1482)
Browse files Browse the repository at this point in the history
Fixes #1471.

It also extends function-variable to include object properties.

e.g.

```js
{foo: function() {}}
```
  • Loading branch information
RunDevelopment authored and mAAdhaTTah committed Aug 19, 2018
1 parent 0f75d9d commit c40f604
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 8 deletions.
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))/
],
'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.

0 comments on commit c40f604

Please sign in to comment.