Skip to content

Commit

Permalink
Improved JavaScript parameter recognization (#1722)
Browse files Browse the repository at this point in the history
- The first letter of a parameter can now be upper case.
- Fixes too greedy regex by introducing recursive expressions.
  • Loading branch information
RexSkz authored and RunDevelopment committed Feb 12, 2019
1 parent 8cf20d4 commit 57a9203
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 12 deletions.
10 changes: 5 additions & 5 deletions components/prism-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,26 @@ 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*(?:async\s*)?(?:\bfunction\b|(?:\([^()]*\)|[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)\s*=>))/i,
pattern: /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*[=:]\s*(?:async\s*)?(?:\bfunction\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*\))/,
pattern: /(function(?:\s+[_$A-Za-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*=>)/,
pattern: /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*=>)/i,
inside: Prism.languages.javascript
},
{
pattern: /(\(\s*)[^\s()][^()]*?(?=\s*\)\s*=>)/,
pattern: /(\(\s*)(?!\s)(?:[^()]|\([^()]*\))+?(?=\s*\)\s*=>)/,
lookbehind: true,
inside: Prism.languages.javascript
},
{
pattern: /((?:\b|\s|^)(?!(?: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)(?![$\w\xA0-\uFFFF]))(?:[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*\s*)\(\s*)[^\s()][^()]*?(?=\s*\)\s*\{)/,
pattern: /((?:\b|\s|^)(?!(?: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)(?![$\w\xA0-\uFFFF]))(?:[_$A-Za-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*\s*)\(\s*)(?!\s)(?:[^()]|\([^()]*\))+?(?=\s*\)\s*\{)/,
lookbehind: true,
inside: Prism.languages.javascript
}
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: 5 additions & 5 deletions prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -738,26 +738,26 @@ 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*(?:async\s*)?(?:\bfunction\b|(?:\([^()]*\)|[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)\s*=>))/i,
pattern: /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*[=:]\s*(?:async\s*)?(?:\bfunction\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*\))/,
pattern: /(function(?:\s+[_$A-Za-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*=>)/,
pattern: /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*=>)/i,
inside: Prism.languages.javascript
},
{
pattern: /(\(\s*)[^\s()][^()]*?(?=\s*\)\s*=>)/,
pattern: /(\(\s*)(?!\s)(?:[^()]|\([^()]*\))+?(?=\s*\)\s*=>)/,
lookbehind: true,
inside: Prism.languages.javascript
},
{
pattern: /((?:\b|\s|^)(?!(?: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)(?![$\w\xA0-\uFFFF]))(?:[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*\s*)\(\s*)[^\s()][^()]*?(?=\s*\)\s*\{)/,
pattern: /((?:\b|\s|^)(?!(?: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)(?![$\w\xA0-\uFFFF]))(?:[_$A-Za-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*\s*)\(\s*)(?!\s)(?:[^()]|\([^()]*\))+?(?=\s*\)\s*\{)/,
lookbehind: true,
inside: Prism.languages.javascript
}
Expand Down
40 changes: 40 additions & 0 deletions tests/languages/javascript/class-method_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class Test {
async bar(x, y = 0 ) {}
$ ( ) {}
awaitFoo(){}
Example({ props: { a: _A, b} } = Props) {}
f(x = fun(), y = 0) {}
}

----------------------------------------------------
Expand Down Expand Up @@ -51,6 +53,44 @@ class Test {
["punctuation", "{"],
["punctuation", "}"],

["function", "Example"],
["punctuation", "("],
["parameter", [
["punctuation", "{"],
" props",
["punctuation", ":"],
["punctuation", "{"],
" a",
["punctuation", ":"],
" _A",
["punctuation", ","],
" b",
["punctuation", "}"],
["punctuation", "}"],
["operator", "="],
" Props"
]],
["punctuation", ")"],
["punctuation", "{"],
["punctuation", "}"],

["function", "f"],
["punctuation", "("],
["parameter", [
"x ",
["operator", "="],
["function", "fun"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", ","],
" y ",
["operator", "="],
["number", "0"]
]],
["punctuation", ")"],
["punctuation", "{"],
["punctuation", "}"],

["punctuation", "}"]
]

Expand Down
85 changes: 85 additions & 0 deletions tests/languages/javascript/function-variable_feature.test
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
foo = function ( ) {}
foo = function ( x, y) {}
{foo: function () {}}
bar = async function baz (x ) {}
Expand All @@ -6,10 +7,22 @@ fooBar = x => x
fooBar = ( x, y ) => x
ಠ_ಠ = () => {}
Ƞȡ_҇ = async (ಠ, Ƞ = 2) => {}
d = function Example({ props: { a: _A, b} } = Props) {}
f = function (x = fun()) {}
l = (x = fun(), y) => {}
a = function () {}, b = () => {}

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

[
["function-variable", "foo"],
["operator", "="],
["keyword", "function"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", "{"],
["punctuation", "}"],

["function-variable", "foo"],
["operator", "="],
["keyword", "function"],
Expand Down Expand Up @@ -96,6 +109,78 @@ fooBar = ( x, y ) => x
["punctuation", ")"],
["operator", "=>"],
["punctuation", "{"],
["punctuation", "}"],

["function-variable", "d"],
["operator", "="],
["keyword", "function"],
["function", "Example"],
["punctuation", "("],
["parameter", [
["punctuation", "{"],
" props",
["punctuation", ":"],
["punctuation", "{"],
" a",
["punctuation", ":"],
" _A",
["punctuation", ","],
" b",
["punctuation", "}"],
["punctuation", "}"],
["operator", "="],
" Props"
]],
["punctuation", ")"],
["punctuation", "{"],
["punctuation", "}"],

["function-variable", "f"],
["operator", "="],
["keyword", "function"],
["punctuation", "("],
["parameter", [
"x ",
["operator", "="],
["function", "fun"],
["punctuation", "("],
["punctuation", ")"]
]],
["punctuation", ")"],
["punctuation", "{"],
["punctuation", "}"],

["function-variable", "l"],
["operator", "="],
["punctuation", "("],
["parameter", [
"x ",
["operator", "="],
["function", "fun"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", ","],
" y"
]],
["punctuation", ")"],
["operator", "=>"],
["punctuation", "{"],
["punctuation", "}"],

["function-variable", "a"],
["operator", "="],
["keyword", "function"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", "{"],
["punctuation", "}"],
["punctuation", ","],
["function-variable", "b"],
["operator", "="],
["punctuation", "("],
["punctuation", ")"],
["operator", "=>"],
["punctuation", "{"],
["punctuation", "}"]
]

Expand Down
6 changes: 5 additions & 1 deletion tests/languages/javascript/function_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ $()
ಠ_ಠ()
Ƞȡ_҇()
if(notAFunction)
foo({ x })
foo({ y: fun() })

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

Expand All @@ -21,7 +23,9 @@ if(notAFunction)
["function", "$"], ["punctuation", "("], ["punctuation", ")"],
["function", "ಠ_ಠ"], ["punctuation", "("], ["punctuation", ")"],
["function", "Ƞȡ_҇"], ["punctuation", "("], ["punctuation", ")"],
["keyword", "if"], ["punctuation", "("], "notAFunction", ["punctuation", ")"]
["keyword", "if"], ["punctuation", "("], "notAFunction", ["punctuation", ")"],
["function", "foo"], ["punctuation", "("], ["punctuation", "{"], " x ", ["punctuation", "}"], ["punctuation", ")"],
["function", "foo"], ["punctuation", "("], ["punctuation", "{"], " y", ["punctuation", ":"], ["function", "fun"], ["punctuation", "("], ["punctuation", ")"], ["punctuation", "}"], ["punctuation", ")"]
]

----------------------------------------------------
Expand Down

0 comments on commit 57a9203

Please sign in to comment.