Skip to content

Commit

Permalink
Fix the regex which is too greedy
Browse files Browse the repository at this point in the history
  • Loading branch information
RexSkz committed Feb 11, 2019
1 parent 0331e28 commit 50bc43c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
8 changes: 4 additions & 4 deletions components/prism-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ 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-Za-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)?\s*\(\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
},
Expand All @@ -43,12 +43,12 @@ Prism.languages.insertBefore('javascript', 'keyword', {
inside: Prism.languages.javascript
},
{
pattern: /(\(\s*)[^\s].*?(?=\s*\)\s*=>)/i,
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-Za-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
21 changes: 19 additions & 2 deletions tests/languages/javascript/function-variable_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ fooBar = ( x, y ) => x
d = function Example({ props: { a: _A, b} } = Props) {}
f = function (x = fun()) {}
l = (x = fun(), y) => {}
a = function () {}, b = () => {}

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

Expand Down Expand Up @@ -43,7 +44,7 @@ l = (x = fun(), y) => {}
["function", "baz"],
["punctuation", "("],
["parameter", [
"x"
"x "
]],
["punctuation", ")"],
["punctuation", "{"],
Expand Down Expand Up @@ -72,7 +73,7 @@ l = (x = fun(), y) => {}
["parameter", [
"x",
["punctuation", ","],
" y"
" y "
]],
["punctuation", ")"],
["operator", "=>"], " x\r\n",
Expand Down Expand Up @@ -155,6 +156,22 @@ l = (x = fun(), 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

0 comments on commit 50bc43c

Please sign in to comment.