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
2 changes: 2 additions & 0 deletions components/prism-flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
]
});
Prism.languages.flow['function-variable'].pattern = /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*=\s*(?:function\b|(?:\([^()]*\)(?:\s*:\s*\w+)?|[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)\s*=>))/i;
delete Prism.languages.flow['es5-function-parameter'];
delete Prism.languages.flow['es6-function-parameter'];

Prism.languages.insertBefore('flow', 'operator', {
'flow-punctuation': {
Expand Down
2 changes: 1 addition & 1 deletion components/prism-flow.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 17 additions & 1 deletion components/prism-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,25 @@ 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*)?(?:function\b|(?:\([^()]*\)|[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)\s*=>))/i,
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be async\s+ ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we use async\s+, we couldn't match the variable foo in foo = async(a, b) =>, where the async here is a reserve word instead of the function name.

I found a better solution async(?:(?:\s*(?=\())|\s+).

alias: 'function'
},
'es5-function-parameter': {
pattern: /(function\s*)(?:[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)?\s*(\([^()]*\))/,
Copy link
Member

Choose a reason for hiding this comment

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

This will also match functionAbc(). Maybe something like this is more suitable:

/(function)(?:\s+[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)?\s*(\([^()]*\))/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right, I'll fix my code. Thanks~

lookbehind: true,
inside: Prism.languages.javascript,
alias: 'parameter'
},
'es6-function-parameter': {
pattern: /(\([^()]*\)|[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)(?=\s*=>)/,
inside: Prism.languages.javascript,
alias: 'parameter'
},
'es6-class-method-parameter': {
pattern: /\b(?!await|delete|export|for|if|new|return|switch|throw|typeof|while|yield)(?:[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*\s*)(\([^()]*\))(?=\s*\{)/,
inside: Prism.languages.javascript,
alias: 'parameter'
},
'constant': /\b[A-Z][A-Z\d_]*\b/
});

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.

18 changes: 17 additions & 1 deletion prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -737,9 +737,25 @@ 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*)?(?:function\b|(?:\([^()]*\)|[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)\s*=>))/i,
alias: 'function'
},
'es5-function-parameter': {
pattern: /(function\s*)(?:[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)?\s*(\([^()]*\))/,
lookbehind: true,
inside: Prism.languages.javascript,
alias: 'parameter'
},
'es6-function-parameter': {
pattern: /(\([^()]*\)|[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)(?=\s*=>)/,
inside: Prism.languages.javascript,
alias: 'parameter'
},
'es6-class-method-parameter': {
pattern: /\b(?!await|delete|export|for|if|new|return|switch|throw|typeof|while|yield)(?:[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*\s*)(\([^()]*\))(?=\s*\{)/,
inside: Prism.languages.javascript,
alias: 'parameter'
},
'constant': /\b[A-Z][A-Z\d_]*\b/
});

Expand Down
54 changes: 54 additions & 0 deletions tests/languages/javascript/class-method_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
class Test {
foo(x, y = 0) {}
async bar(x, y = 0) {}
_ ( ) {}
}

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

[
["keyword", "class"],
["class-name", ["Test"]],
["punctuation", "{"],

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

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

["es6-class-method-parameter", [
["function", "_"],
["punctuation", "("],
["punctuation", ")"]
]],
["punctuation", "{"],
["punctuation", "}"],

["punctuation", "}"]
]

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

Checks for class methods.
86 changes: 73 additions & 13 deletions tests/languages/javascript/function-variable_feature.test
Original file line number Diff line number Diff line change
@@ -1,22 +1,82 @@
foo = function () {}
bar = function baz () {}
foo = function ( x, y) {}
bar = async function baz (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"],
["es5-function-parameter", [
["punctuation", "("],
" x",
["punctuation", ","],
" y",
["punctuation", ")"]
]],
["punctuation", "{"],
["punctuation", "}"],

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

["function-variable", "fooBar"],
["operator", "="],
["es6-function-parameter", [
"x"
]],
["operator", "=>"], " x\r\n",

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

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

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