-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Identify JavaScript function parameters #1446
Changes from 3 commits
5cf3b3d
9ef0c3e
7205979
4e7261e
685d0db
f90f138
d2f3c9d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,9 +14,30 @@ 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*(?:async(?:(?:\s*(?=\())|\s+))?(?:function\b|(?:\([^()]*\)|[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)\s*=>))/i, | ||
alias: 'function' | ||
}, | ||
'parameter': [ | ||
{ | ||
pattern: /(function(?:\s+[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)?\s*\(\s*)\S[^()]*(?=\s*\))/, | ||
lookbehind: true, | ||
inside: Prism.languages.javascript | ||
}, | ||
{ | ||
pattern: /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*=>)/, | ||
inside: Prism.languages.javascript | ||
}, | ||
{ | ||
pattern: /(\()[^()]+(?=\)\s*=>)/, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leading and trailing spaces might not be wanted. |
||
lookbehind: true, | ||
inside: Prism.languages.javascript | ||
}, | ||
{ | ||
pattern: /(\b(?!await|delete|export|for|if|new|return|switch|throw|typeof|while|yield)(?:[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*\s*)\(\s*)\S[^()]*(?=\s*\)\s*\{)/, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
lookbehind: true, | ||
inside: Prism.languages.javascript | ||
} | ||
], | ||
'constant': /\b[A-Z][A-Z\d_]*\b/ | ||
}); | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
class Test { | ||
foo(x, y = 0) {} | ||
async bar(x, y = 0) {} | ||
_ ( ) {} | ||
} | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["keyword", "class"], | ||
["class-name", ["Test"]], | ||
["punctuation", "{"], | ||
|
||
["function", "foo"], | ||
["punctuation", "("], | ||
["parameter", [ | ||
"x", | ||
["punctuation", ","], | ||
" y ", | ||
["operator", "="], | ||
["number", "0"] | ||
]], | ||
["punctuation", ")"], | ||
["punctuation", "{"], | ||
["punctuation", "}"], | ||
|
||
["keyword", "async"], | ||
["function", "bar"], | ||
["punctuation", "("], | ||
["parameter", [ | ||
"x", | ||
["punctuation", ","], | ||
" y ", | ||
["operator", "="], | ||
["number", "0"] | ||
]], | ||
["punctuation", ")"], | ||
["punctuation", "{"], | ||
["punctuation", "}"], | ||
|
||
["function", "_"], | ||
["punctuation", "("], | ||
["punctuation", ")"], | ||
["punctuation", "{"], | ||
["punctuation", "}"], | ||
|
||
["punctuation", "}"] | ||
] | ||
|
||
---------------------------------------------------- | ||
|
||
Checks for class methods. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,93 @@ | ||
foo = function () {} | ||
bar = function baz () {} | ||
foo = function ( x, y) {} | ||
bar = async function baz (x ) {} | ||
baz = async(x) => x | ||
fooBar = x => x | ||
fooBar = (x, y) => x | ||
ಠ_ಠ = () => {} | ||
Ƞȡ_҇ = (ಠ, Ƞ = 2) => {} | ||
Ƞȡ_҇ = async (ಠ, Ƞ = 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", "}"] | ||
["function-variable", "foo"], | ||
["operator", "="], | ||
["keyword", "function"], | ||
["punctuation", "("], | ||
["parameter", [ | ||
"x", | ||
["punctuation", ","], | ||
" y" | ||
]], | ||
["punctuation", ")"], | ||
["punctuation", "{"], | ||
["punctuation", "}"], | ||
|
||
["function-variable", "bar"], | ||
["operator", "="], | ||
["keyword", "async"], | ||
["keyword", "function"], | ||
["function", "baz"], | ||
["punctuation", "("], | ||
["parameter", [ | ||
"x " | ||
]], | ||
["punctuation", ")"], | ||
["punctuation", "{"], | ||
["punctuation", "}"], | ||
|
||
["function-variable", "baz"], | ||
["operator", "="], | ||
["keyword", "async"], | ||
["punctuation", "("], | ||
["parameter", [ | ||
"x" | ||
]], | ||
["punctuation", ")"], | ||
["operator", "=>"], " x\r\n", | ||
|
||
["function-variable", "fooBar"], | ||
["operator", "="], | ||
["parameter", [ | ||
"x" | ||
]], | ||
["operator", "=>"], " x\r\n", | ||
|
||
["function-variable", "fooBar"], | ||
["operator", "="], | ||
["punctuation", "("], | ||
["parameter", [ | ||
"x", | ||
["punctuation", ","], | ||
" y" | ||
]], | ||
["punctuation", ")"], | ||
["operator", "=>"], " x\r\n", | ||
|
||
["function-variable", "ಠ_ಠ"], | ||
["operator", "="], | ||
["punctuation", "("], | ||
["punctuation", ")"], | ||
["operator", "=>"], | ||
["punctuation", "{"], | ||
["punctuation", "}"], | ||
|
||
["function-variable", "Ƞȡ_҇"], | ||
["operator", "="], | ||
["keyword", "async"], | ||
["punctuation", "("], | ||
["parameter", [ | ||
"ಠ", | ||
["punctuation", ","], | ||
" Ƞ ", | ||
["operator", "="], | ||
["number", "2"] | ||
]], | ||
["punctuation", ")"], | ||
["operator", "=>"], | ||
["punctuation", "{"], | ||
["punctuation", "}"] | ||
] | ||
|
||
---------------------------------------------------- | ||
|
||
Checks for variables obviously containing functions. | ||
Checks for variables obviously containing functions. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2 Problems here:
[^()]*?
. Otherwise(?=\s*
won't match anything.function a())
. Not valid JS, I know, but still. Replace\S
with[^\s()]
and your good to go.