Skip to content

Commit 0cc8c56

Browse files
RexSkzmAAdhaTTah
authored andcommitted
Identify JavaScript function parameters (#1446)
Support parameters for these types of functions: ```javascript // es6 class method foo(x, y) {} // es6 arrow function (x, y) => x x => x // es5 function function foo(x, y) {} // es5 anonymous function function (x, y) {} ```
1 parent 0c8f650 commit 0cc8c56

12 files changed

+245
-60
lines changed

components/prism-flow.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
]
1111
});
1212
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;
13+
delete Prism.languages.flow['parameter'];
1314

1415
Prism.languages.insertBefore('flow', 'operator', {
1516
'flow-punctuation': {
@@ -31,4 +32,4 @@
3132
lookbehind: true
3233
}
3334
);
34-
}(Prism));
35+
}(Prism));

components/prism-flow.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/prism-javascript.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,30 @@ Prism.languages.insertBefore('javascript', 'keyword', {
2929
},
3030
// This must be declared before keyword because we use "function" inside the look-forward
3131
'function-variable': {
32-
pattern: /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*[=:]\s*(?:function\b|(?:\([^()]*\)|[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)\s*=>))/i,
32+
pattern: /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*[=:]\s*(?:async\s*)?(?:\bfunction\b|(?:\([^()]*\)|[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)\s*=>))/i,
3333
alias: 'function'
3434
},
35+
'parameter': [
36+
{
37+
pattern: /(function(?:\s+[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)?\s*\(\s*)[^\s()][^()]*?(?=\s*\))/,
38+
lookbehind: true,
39+
inside: Prism.languages.javascript
40+
},
41+
{
42+
pattern: /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*=>)/,
43+
inside: Prism.languages.javascript
44+
},
45+
{
46+
pattern: /(\(\s*)[^\s()][^()]*?(?=\s*\)\s*=>)/,
47+
lookbehind: true,
48+
inside: Prism.languages.javascript
49+
},
50+
{
51+
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*\{)/,
52+
lookbehind: true,
53+
inside: Prism.languages.javascript
54+
}
55+
],
3556
'constant': /\b[A-Z][A-Z\d_]*\b/
3657
});
3758

components/prism-javascript.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

prism.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -744,9 +744,30 @@ Prism.languages.insertBefore('javascript', 'keyword', {
744744
},
745745
// This must be declared before keyword because we use "function" inside the look-forward
746746
'function-variable': {
747-
pattern: /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*[=:]\s*(?:function\b|(?:\([^()]*\)|[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)\s*=>))/i,
747+
pattern: /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*[=:]\s*(?:async\s*)?(?:\bfunction\b|(?:\([^()]*\)|[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)\s*=>))/i,
748748
alias: 'function'
749749
},
750+
'parameter': [
751+
{
752+
pattern: /(function(?:\s+[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)?\s*\(\s*)[^\s()][^()]*?(?=\s*\))/,
753+
lookbehind: true,
754+
inside: Prism.languages.javascript
755+
},
756+
{
757+
pattern: /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*=>)/,
758+
inside: Prism.languages.javascript
759+
},
760+
{
761+
pattern: /(\(\s*)[^\s()][^()]*?(?=\s*\)\s*=>)/,
762+
lookbehind: true,
763+
inside: Prism.languages.javascript
764+
},
765+
{
766+
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*\{)/,
767+
lookbehind: true,
768+
inside: Prism.languages.javascript
769+
}
770+
],
750771
'constant': /\b[A-Z][A-Z\d_]*\b/
751772
});
752773

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
class Test {
2+
foo( x, y = 0) {}
3+
async bar(x, y = 0 ) {}
4+
$ ( ) {}
5+
awaitFoo(){}
6+
}
7+
8+
----------------------------------------------------
9+
10+
[
11+
["keyword", "class"],
12+
["class-name", ["Test"]],
13+
["punctuation", "{"],
14+
15+
["function", "foo"],
16+
["punctuation", "("],
17+
["parameter", [
18+
"x",
19+
["punctuation", ","],
20+
" y ",
21+
["operator", "="],
22+
["number", "0"]
23+
]],
24+
["punctuation", ")"],
25+
["punctuation", "{"],
26+
["punctuation", "}"],
27+
28+
["keyword", "async"],
29+
["function", "bar"],
30+
["punctuation", "("],
31+
["parameter", [
32+
"x",
33+
["punctuation", ","],
34+
" y ",
35+
["operator", "="],
36+
["number", "0"]
37+
]],
38+
["punctuation", ")"],
39+
["punctuation", "{"],
40+
["punctuation", "}"],
41+
42+
["function", "$"],
43+
["punctuation", "("],
44+
["punctuation", ")"],
45+
["punctuation", "{"],
46+
["punctuation", "}"],
47+
48+
["function", "awaitFoo"],
49+
["punctuation", "("],
50+
["punctuation", ")"],
51+
["punctuation", "{"],
52+
["punctuation", "}"],
53+
54+
["punctuation", "}"]
55+
]
56+
57+
----------------------------------------------------
58+
59+
Checks for class methods.

tests/languages/javascript/function-variable_feature.test

+92-15
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,102 @@
1-
foo = function () {}
1+
foo = function ( x, y) {}
22
{foo: function () {}}
3-
bar = function baz () {}
3+
bar = async function baz (x ) {}
4+
baz = async(x) => x
45
fooBar = x => x
6+
fooBar = ( x, y ) => x
57
ಠ_ಠ = () => {}
6-
Ƞȡ_҇ = (ಠ, Ƞ = 2) => {}
8+
Ƞȡ_҇ = async (ಠ, Ƞ = 2) => {}
79

810
----------------------------------------------------
911

1012
[
11-
["function-variable", "foo"], ["operator", "="], ["keyword", "function"],
12-
["punctuation", "("], ["punctuation", ")"], ["punctuation", "{"], ["punctuation", "}"],
13-
["punctuation", "{"], ["function-variable", "foo"], ["punctuation", ":"], ["keyword", "function"],
14-
["punctuation", "("], ["punctuation", ")"], ["punctuation", "{"],["punctuation","}"],["punctuation","}"],
15-
["function-variable", "bar"], ["operator", "="], ["keyword", "function"], ["function", "baz"],
16-
["punctuation", "("], ["punctuation", ")"], ["punctuation", "{"], ["punctuation", "}"],
17-
["function-variable", "fooBar"], ["operator", "="], " x ", ["operator", "=>"], " x\r\n",
18-
["function-variable", "ಠ_ಠ"], ["operator", "="], ["punctuation", "("], ["punctuation", ")"],
19-
["operator", "=>"], ["punctuation", "{"], ["punctuation", "}"],
20-
["function-variable", "Ƞȡ_҇"], ["operator", "="],
21-
["punctuation", "("], "ಠ", ["punctuation", ","], " Ƞ ", ["operator", "="], ["number", "2"], ["punctuation", ")"],
22-
["operator", "=>"], ["punctuation", "{"], ["punctuation", "}"]
13+
["function-variable", "foo"],
14+
["operator", "="],
15+
["keyword", "function"],
16+
["punctuation", "("],
17+
["parameter", [
18+
"x",
19+
["punctuation", ","],
20+
" y"
21+
]],
22+
["punctuation", ")"],
23+
["punctuation", "{"],
24+
["punctuation", "}"],
25+
26+
["punctuation", "{"],
27+
["function-variable", "foo"],
28+
["punctuation", ":"],
29+
["keyword", "function"],
30+
["punctuation", "("],
31+
["punctuation", ")"],
32+
["punctuation", "{"],
33+
["punctuation","}"],
34+
["punctuation","}"],
35+
36+
["function-variable", "bar"],
37+
["operator", "="],
38+
["keyword", "async"],
39+
["keyword", "function"],
40+
["function", "baz"],
41+
["punctuation", "("],
42+
["parameter", [
43+
"x"
44+
]],
45+
["punctuation", ")"],
46+
["punctuation", "{"],
47+
["punctuation", "}"],
48+
49+
["function-variable", "baz"],
50+
["operator", "="],
51+
["keyword", "async"],
52+
["punctuation", "("],
53+
["parameter", [
54+
"x"
55+
]],
56+
["punctuation", ")"],
57+
["operator", "=>"], " x\r\n",
58+
59+
["function-variable", "fooBar"],
60+
["operator", "="],
61+
["parameter", [
62+
"x"
63+
]],
64+
["operator", "=>"], " x\r\n",
65+
66+
["function-variable", "fooBar"],
67+
["operator", "="],
68+
["punctuation", "("],
69+
["parameter", [
70+
"x",
71+
["punctuation", ","],
72+
" y"
73+
]],
74+
["punctuation", ")"],
75+
["operator", "=>"], " x\r\n",
76+
77+
["function-variable", "ಠ_ಠ"],
78+
["operator", "="],
79+
["punctuation", "("],
80+
["punctuation", ")"],
81+
["operator", "=>"],
82+
["punctuation", "{"],
83+
["punctuation", "}"],
84+
85+
["function-variable", "Ƞȡ_҇"],
86+
["operator", "="],
87+
["keyword", "async"],
88+
["punctuation", "("],
89+
["parameter", [
90+
"ಠ",
91+
["punctuation", ","],
92+
" Ƞ ",
93+
["operator", "="],
94+
["number", "2"]
95+
]],
96+
["punctuation", ")"],
97+
["operator", "=>"],
98+
["punctuation", "{"],
99+
["punctuation", "}"]
23100
]
24101

25102
----------------------------------------------------

tests/languages/javascript/function_feature.test

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ _()
77
$()
88
ಠ_ಠ()
99
Ƞȡ_҇()
10+
if(notAFunction)
1011

1112
----------------------------------------------------
1213

@@ -19,7 +20,8 @@ $()
1920
["function", "_"], ["punctuation", "("], ["punctuation", ")"],
2021
["function", "$"], ["punctuation", "("], ["punctuation", ")"],
2122
["function", "ಠ_ಠ"], ["punctuation", "("], ["punctuation", ")"],
22-
["function", "Ƞȡ_҇"], ["punctuation", "("], ["punctuation", ")"]
23+
["function", "Ƞȡ_҇"], ["punctuation", "("], ["punctuation", ")"],
24+
["keyword", "if"], ["punctuation", "("], "notAFunction", ["punctuation", ")"]
2325
]
2426

2527
----------------------------------------------------
+35-35
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
fetch('some-resource.json')
2-
.then(response => response.json())
3-
.catch(console.error);
4-
5-
----------------------------------------------------
6-
7-
[
8-
["function", "fetch"],
9-
["punctuation", "("],
10-
["string", "'some-resource.json'"],
11-
["punctuation", ")"],
12-
["punctuation", "."],
13-
["function", "then"],
14-
["punctuation", "("],
15-
"response ",
16-
["operator", "=>"],
17-
" response",
18-
["punctuation", "."],
19-
["function", "json"],
20-
["punctuation", "("],
21-
["punctuation", ")"],
22-
["punctuation", ")"],
23-
["punctuation", "."],
24-
["function", "catch"],
25-
["punctuation", "("],
26-
"console",
27-
["punctuation", "."],
28-
"error",
29-
["punctuation", ")"],
30-
["punctuation", ";"]
31-
]
32-
33-
----------------------------------------------------
34-
35-
Checks for catch function which is not a keyword. See #1526
1+
fetch('some-resource.json')
2+
.then(response => response.json())
3+
.catch(console.error);
4+
5+
----------------------------------------------------
6+
7+
[
8+
["function", "fetch"],
9+
["punctuation", "("],
10+
["string", "'some-resource.json'"],
11+
["punctuation", ")"],
12+
["punctuation", "."],
13+
["function", "then"],
14+
["punctuation", "("],
15+
["parameter", ["response"]],
16+
["operator", "=>"],
17+
" response",
18+
["punctuation", "."],
19+
["function", "json"],
20+
["punctuation", "("],
21+
["punctuation", ")"],
22+
["punctuation", ")"],
23+
["punctuation", "."],
24+
["function", "catch"],
25+
["punctuation", "("],
26+
"console",
27+
["punctuation", "."],
28+
"error",
29+
["punctuation", ")"],
30+
["punctuation", ";"]
31+
]
32+
33+
----------------------------------------------------
34+
35+
Checks for catch function which is not a keyword. See #1526

tests/languages/jsx/issue1294.test

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ export default () => (
1313
[
1414
["keyword", "export"],
1515
["keyword", "default"],
16-
["punctuation", "("], ["punctuation", ")"], ["operator", "=>"], ["punctuation", "("],
16+
["punctuation", "("],
17+
["punctuation", ")"],
18+
["operator", "=>"], ["punctuation", "("],
1719
["tag", [
1820
["tag", [
1921
["punctuation", "<"],
@@ -66,4 +68,4 @@ export default () => (
6668

6769
----------------------------------------------------
6870

69-
See #1294.
71+
See #1294.

tests/languages/jsx/issue1335.test

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
["script-punctuation", "="],
2222
["punctuation", "{"],
2323
["punctuation", "("],
24-
"e",
24+
["parameter", [
25+
"e"
26+
]],
2527
["punctuation", ")"],
2628
["operator", "=>"],
2729
["keyword", "this"],

tests/languages/jsx/issue1421.test

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,4 @@ class Columns extends React.Component {
5555
]
5656

5757
----------------------------------------------------
58-
Checks for fragments short syntax. See #1421
58+
Checks for fragments short syntax. See #1421

0 commit comments

Comments
 (0)