Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 9 additions & 3 deletions components/prism-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,22 @@ Prism.languages.insertBefore('javascript', 'keyword', {
},
'parameter': [
{
pattern: /(function)(?:\s+[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)?\s*(\([^()]*\))/,
pattern: /(function(?:\s+[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)?\s*\(\s*)\S[^()]*(?=\s*\))/,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 Problems here:

  1. You probably don't want trailing spaces, so please make it [^()]*?. Otherwise (?=\s* won't match anything.
  2. This also matches function a()). Not valid JS, I know, but still. Replace \S with [^\s()] and your good to go.

lookbehind: true,
inside: Prism.languages.javascript
},
{
pattern: /(\([^()]*\)|[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)(?=\s*=>)/,
pattern: /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*=>)/,
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*\{)/,
pattern: /(\()[^()]+(?=\)\s*=>)/,
Copy link
Member

Choose a reason for hiding this comment

The 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*\{)/,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. There might be problems with \S again.
  2. Spaces?
  3. awaitSomething(a){} is not matched. Neither is $await(a){} (\b might not have been the best choice).

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.

12 changes: 9 additions & 3 deletions prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -742,16 +742,22 @@ Prism.languages.insertBefore('javascript', 'keyword', {
},
'parameter': [
{
pattern: /(function)(?:\s+[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)?\s*(\([^()]*\))/,
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*=>)/,
pattern: /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*=>)/,
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*\{)/,
pattern: /(\()[^()]+(?=\)\s*=>)/,
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*\{)/,
lookbehind: true,
inside: Prism.languages.javascript
}
],
Expand Down
24 changes: 11 additions & 13 deletions tests/languages/javascript/class-method_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,36 @@ class Test {
["class-name", ["Test"]],
["punctuation", "{"],

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

["keyword", "async"],
["function", "bar"],
["punctuation", "("],
["parameter", [
["function", "bar"],
["punctuation", "("],
"x",
["punctuation", ","],
" y ",
["operator", "="],
["number", "0"],
["punctuation", ")"]
["number", "0"]
]],
["punctuation", ")"],
["punctuation", "{"],
["punctuation", "}"],

["parameter", [
["function", "_"],
["punctuation", "("],
["punctuation", ")"]
]],
["function", "_"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", "{"],
["punctuation", "}"],

Expand Down
40 changes: 19 additions & 21 deletions tests/languages/javascript/function-variable_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,37 @@ fooBar = (x, y) => x
["function-variable", "foo"],
["operator", "="],
["keyword", "function"],
["punctuation", "("],
["parameter", [
["punctuation", "("],
" x",
"x",
["punctuation", ","],
" y",
["punctuation", ")"]
" y"
]],
["punctuation", ")"],
["punctuation", "{"],
["punctuation", "}"],

["function-variable", "bar"],
["operator", "="],
["keyword", "async"],
["keyword", "function"],
["function", "baz"],
["punctuation", "("],
["parameter", [
["function", "baz"],
["punctuation", "("],
"x ",
["punctuation", ")"]
"x "
]],
["punctuation", ")"],
["punctuation", "{"],
["punctuation", "}"],

["function-variable", "baz"],
["operator", "="],
["keyword", "async"],
["punctuation", "("],
["parameter", [
["punctuation", "("],
"x",
["punctuation", ")"]
"x"
]],
["punctuation", ")"],
["operator", "=>"], " x\r\n",

["function-variable", "fooBar"],
Expand All @@ -54,37 +54,35 @@ fooBar = (x, y) => x

["function-variable", "fooBar"],
["operator", "="],
["punctuation", "("],
["parameter", [
["punctuation", "("],
"x",
["punctuation", ","],
" y",
["punctuation", ")"]
" y"
]],
["punctuation", ")"],
["operator", "=>"], " x\r\n",

["function-variable", "ಠ_ಠ"],
["operator", "="],
["parameter", [
["punctuation", "("],
["punctuation", ")"]
]],
["punctuation", "("],
["punctuation", ")"],
["operator", "=>"],
["punctuation", "{"],
["punctuation", "}"],

["function-variable", "Ƞȡ_҇"],
["operator", "="],
["keyword", "async"],
["punctuation", "("],
["parameter", [
["punctuation", "("],
"ಠ",
["punctuation", ","],
" Ƞ ",
["operator", "="],
["number", "2"],
["punctuation", ")"]
["number", "2"]
]],
["punctuation", ")"],
["operator", "=>"],
["punctuation", "{"],
["punctuation", "}"]
Expand Down
3 changes: 2 additions & 1 deletion tests/languages/jsx/issue1294.test
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export default () => (
[
["keyword", "export"],
["keyword", "default"],
["parameter", [["punctuation", "("], ["punctuation", ")"]]],
["punctuation", "("],
["punctuation", ")"],
["operator", "=>"], ["punctuation", "("],
["tag", [
["tag", [
Expand Down
6 changes: 3 additions & 3 deletions tests/languages/jsx/issue1335.test
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
["script", [
["script-punctuation", "="],
["punctuation", "{"],
["punctuation", "("],
["parameter", [
["punctuation", "("],
"e",
["punctuation", ")"]
"e"
]],
["punctuation", ")"],
["operator", "=>"],
["keyword", "this"],
["punctuation", "."],
Expand Down
6 changes: 2 additions & 4 deletions tests/languages/jsx/issue1421.test
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ class Columns extends React.Component {
["keyword", "extends"],
["class-name", ["React", ["punctuation", "."], "Component"]],
["punctuation", "{"],
["parameter", [
["function", "render"],
["punctuation", "("], ["punctuation", ")"]
]],
["function", "render"],
["punctuation", "("], ["punctuation", ")"],
["punctuation", "{"],
["keyword", "return"], ["punctuation", "("],
["tag", [
Expand Down
8 changes: 3 additions & 5 deletions tests/languages/tsx/tag_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,9 @@ class Test extends Component {
["keyword", "extends"],
["class-name", ["Component"]],
["punctuation", "{"],
["parameter", [
["function", "render"],
["punctuation", "("],
["punctuation", ")"]
]],
["function", "render"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", "{"],
["keyword","return"],
["tag", [
Expand Down