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

JavaScript: Better highlighting of functions #1190

Merged
merged 1 commit into from
Oct 10, 2017
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
7 changes: 6 additions & 1 deletion components/prism-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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/,
'number': /\b-?(0[xX][\dA-Fa-f]+|0[bB][01]+|0[oO][0-7]+|\d*\.?\d+([Ee][+-]?\d+)?|NaN|Infinity)\b/,
// Allow for all non-ASCII characters (See http://stackoverflow.com/a/2008444)
'function': /[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*(?=\()/i,
'function': /[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*(?=\s*\()/i,
'operator': /-[-=]?|\+[+=]?|!=?=?|<<?=?|>>?>?=?|=(?:==?|>)?|&[&=]?|\|[|=]?|\*\*?=?|\/=?|~|\^=?|%=?|\?|\.{3}/
});

Expand All @@ -11,6 +11,11 @@ Prism.languages.insertBefore('javascript', 'keyword', {
pattern: /(^|[^/])\/(?!\/)(\[[^\]\r\n]+]|\\.|[^/\\\[\r\n])+\/[gimyu]{0,5}(?=\s*($|[\r\n,.;})]))/,
lookbehind: true,
greedy: true
},
// This must be declared before keyword because we use "function" inside the look-forward
'function-variable': {
pattern: /[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*(?=\s*=\s*(?:function\b|(?:\([^()]*\)|[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*)\s*=>))/i,
alias: 'function'
}
});

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.

7 changes: 6 additions & 1 deletion prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ 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/,
'number': /\b-?(0[xX][\dA-Fa-f]+|0[bB][01]+|0[oO][0-7]+|\d*\.?\d+([Ee][+-]?\d+)?|NaN|Infinity)\b/,
// Allow for all non-ASCII characters (See http://stackoverflow.com/a/2008444)
'function': /[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*(?=\()/i,
'function': /[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*(?=\s*\()/i,
'operator': /-[-=]?|\+[+=]?|!=?=?|<<?=?|>>?>?=?|=(?:==?|>)?|&[&=]?|\|[|=]?|\*\*?=?|\/=?|~|\^=?|%=?|\?|\.{3}/
});

Expand All @@ -701,6 +701,11 @@ Prism.languages.insertBefore('javascript', 'keyword', {
pattern: /(^|[^/])\/(?!\/)(\[[^\]\r\n]+]|\\.|[^/\\\[\r\n])+\/[gimyu]{0,5}(?=\s*($|[\r\n,.;})]))/,
lookbehind: true,
greedy: true
},
// This must be declared before keyword because we use "function" inside the look-forward
'function-variable': {
pattern: /[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*(?=\s*=\s*(?:function\b|(?:\([^()]*\)|[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*)\s*=>))/i,
alias: 'function'
}
});

Expand Down
24 changes: 24 additions & 0 deletions tests/languages/javascript/function-variable_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
foo = function () {}
bar = function baz () {}
fooBar = x => x
ಠ_ಠ = () => {}
Ƞȡ_҇ = (ಠ, Ƞ = 2) => {}

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

[
["function-variable", "foo"], ["operator", "="], ["keyword", "function"],
["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",
["function-variable", "ಠ_ಠ"], ["operator", "="], ["punctuation", "("], ["punctuation", ")"],
["operator", "=>"], ["punctuation", "{"], ["punctuation", "}"],
["function-variable", "Ƞȡ_҇"], ["operator", "="],
["punctuation", "("], "ಠ", ["punctuation", ","], " Ƞ ", ["operator", "="], ["number", "2"], ["punctuation", ")"],
["operator", "=>"], ["punctuation", "{"], ["punctuation", "}"]
]

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

Checks for variables obviously containing functions.
4 changes: 4 additions & 0 deletions tests/languages/javascript/function_feature.test
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
foo()
foo ()
foo_bar()
foo_bar ( )
f42()
_()
$()
Expand All @@ -10,6 +12,8 @@ $()

[
["function", "foo"], ["punctuation", "("], ["punctuation", ")"],
["function", "foo"], ["punctuation", "("], ["punctuation", ")"],
["function", "foo_bar"], ["punctuation", "("], ["punctuation", ")"],
["function", "foo_bar"], ["punctuation", "("], ["punctuation", ")"],
["function", "f42"], ["punctuation", "("], ["punctuation", ")"],
["function", "_"], ["punctuation", "("], ["punctuation", ")"],
Expand Down