8000 Improved JavaScript parameter recognization (#1722) · PrismJS/prism@57a9203 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 57a9203

Browse files
RexSkzRunDevelopment
authored andcommitted
Improved JavaScript parameter recognization (#1722)
- The first letter of a parameter can now be upper case. - Fixes too greedy regex by introducing recursive expressions.
1 parent 8cf20d4 commit 57a9203

File tree

6 files changed

+141
-12
lines changed

6 files changed

+141
-12
lines changed

components/prism-javascript.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,26 @@ 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*(?:async\s*)?(?:\bfunction\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
},
3535
'parameter': [
3636
{
37-
pattern: /(function(?:\s+[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)?\s*\(\s*)[^\s()][^()]*?(?=\s*\))/,
37+
pattern: /(function(?:\s+[_$A-Za-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)?\s*\(\s*)(?!\s)(?:[^()]|\([^()]*\))+?(?=\s*\))/,
3838
lookbehind: true,
3939
inside: Prism.languages.javascript
4040
},
4141
{
42-
pattern: /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*=>)/,
42+
pattern: /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*=>)/i,
4343
inside: Prism.languages.javascript
4444
},
4545
{
46-
pattern: /(\(\s*)[^\s()][^()]*?(?=\s*\)\s*=>)/,
46+
pattern: /(\(\s*)(?!\s)(?:[^()]|\([^()]*\))+?(?=\s*\)\s*=>)/,
4747
lookbehind: true,
4848
inside: Prism.languages.javascript
4949
},
5050
{
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*\{)/,
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-Za-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*\s*)\(\s*)(?!\s)(?:[^()]|\([^()]*\))+?(?=\s*\)\s*\{)/,
5252
lookbehind: true,
5353
inside: Prism.languages.javascript
5454
}

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -738,26 +738,26 @@ Prism.languages.insertBefore('javascript', 'keyword', {
738738
},
739739
// This must be declared before keyword because we use "function" inside the look-forward
740740
'function-variable': {
741-
pattern: /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*[=:]\s*(?:async\s*)?(?:\bfunction\b|(?:\([^()]*\)|[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)\s*=>))/i,
741+
pattern: /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*[=:]\s*(?:async\s*)?(?:\bfunction\b|(?:\((?:[^()]|\([^()]*\))*\)|[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)\s*=>))/i,
742742
alias: 'function'
743743
},
744744
'parameter': [
745745
{
746-
pattern: /(function(?:\s+[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)?\s*\(\s*)[^\s()][^()]*?(?=\s*\))/,
746+
pattern: /(function(?:\s+[_$A-Za-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)?\s*\(\s*)(?!\s)(?:[^()]|\([^()]*\))+?(?=\s*\))/,
747747
lookbehind: true,
748748
inside: Prism.languages.javascript
749749
},
750750
{
751-
pattern: /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*=>)/,
751+
pattern: /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*=>)/i,
752752
inside: Prism.languages.javascript
753753
},
754754
{
755-
pattern: /(\(\s*)[^\s()][^()]*?(?=\s*\)\s*=>)/,
755+
pattern: /(\(\s*)(?!\s)(?:[^()]|\([^()]*\))+?(?=\s*\)\s*=>)/,
756756
lookbehind: true,
757757
inside: Prism.languages.javascript
758758
},
759759
{
760-
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*\{)/,
760+
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*\{)/,
761761
lookbehind: true,
762762
inside: Prism.languages.javascript
763763
}

tests/languages/javascript/class-method_feature.test

+40
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ class Test {
33
async bar(x, y = 0 ) {}
44
$ ( ) {}
55
awaitFoo(){}
6+
Example({ props: { a: _A, b} } = Props) {}
7+
f(x = fun(), y = 0) {}
68
}
79

810
----------------------------------------------------
@@ -51,6 +53,44 @@ class Test {
5153
["punctuation", "{"],
5254
["punctuation", "}"],
5355

56+
["function", "Example"],
57+
["punctuation", "("],
58+
["parameter", [
59+
["punctuation", "{"],
60+
" props",
61+
["punctuation", ":"],
62+
["punctuation", "{"],
63+
" a",
64+
["punctuation", ":"],
65+
" _A",
66+
["punctuation", ","],
67+
" b",
68+
["punctuation", "}"],
69+
["punctuation", "}"],
70+
["operator", "="],
71+
" Props"
72+
]],
73+
["punctuation", ")"],
74+
["punctuation", "{"],
75+
["punctuation", "}"],
76+
77+
["function", "f"],
78+
["punctuation", "("],
79+
["parameter", [
80+
"x ",
81+
["operator", "="],
82+
["function", "fun"],
83+
["punctuation", "("],
84+
["punctuation", ")"],
85+
["punctuation", ","],
86+
" y ",
87+
["operator", "="],
88+
["number", "0"]
89+
]],
90+
["punctuation", ")"],
91+
["punctuation", "{"],
92+
["punctuation", "}"],
93+
5494
["punctuation", "}"]
5595
]
5696

tests/languages/javascript/function-variable_feature.test

+85
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
foo = function ( ) {}
12
foo = function ( x, y) {}
23
{foo: function () {}}
34
bar = async function baz (x ) {}
@@ -6,10 +7,22 @@ fooBar = x => x
67
fooBar = ( x, y ) => x
78
ಠ_ಠ = () => {}
89
Ƞȡ_҇ = async (ಠ, Ƞ = 2) => {}
10+
d = function Example({ props: { a: _A, b} } = Props) {}
11+
f = function (x = fun()) {}
12+
l = (x = fun(), y) => {}
13+
a = function () {}, b = () => {}
914

1015
----------------------------------------------------
1116

1217
[
18+
["function-variable", "foo"],
19+
["operator", "="],
20+
["keyword", "function"],
21+
["punctuation", "("],
22+
["punctuation", ")"],
23+
["punctuation", "{"],
24+
["punctuation", "}"],
25+
1326
["function-variable", "foo"],
1427
["operator", "="],
1528
["keyword", "function"],
@@ -96,6 +109,78 @@ fooBar = ( x, y ) => x
96109
["punctuation", ")"],
97110
["operator", "=>"],
98111
["punctuation", "{"],
112+
["punctuation", "}"],
113+
114+
["function-variable", "d"],
115+
["operator", "="],
116+
["keyword", "function"],
117+
["function", "Example"],
118+
["punctuation", "("],
119+
["parameter", [
120+
["punctuation", "{"],
121+
" props",
122+
["punctuation", ":"],
123+
["punctuation", "{"],
124+
" a",
125+
["punctuation", ":"],
126+
" _A",
127+
["punctuation", ","],
128+
" b",
129+
["punctuation", "}"],
130+
["punctuation", "}"],
131+
["operator", "="],
132+
" Props"
133+
]],
134+
["punctuation", ")"],
135+
["punctuation", "{"],
136+
["punctuation", "}"],
137+
138+
["function-variable", "f"],
139+
["operator", "="],
140+
["keyword", "function"],
141+
["punctuation", "("],
142+
["parameter", [
143+
"x ",
144+
["operator", "="],
145+
["function", "fun"],
146+
["punctuation", "("],
147+
["punctuation", ")"]
148+
]],
149+
["punctuation", ")"],
150+
["punctuation", "{"],
151+
["punctuation", "}"],
152+
153+
["function-variable", "l"],
154+
["operator", "="],
155+
["punctuation", "("],
156+
["parameter", [
157+
"x ",
158+
["operator", "="],
159+
["function", "fun"],
160+
["punctuation", "("],
161+
["punctuation", ")"],
162+
["punctuation", ","],
163+
" y"
164+
]],
165+
["punctuation", ")"],
166+
["operator", "=>"],
167+
["punctuation", "{"],
168+
["punctuation", "}"],
169+
170+
["function-variable", "a"],
171+
["operator", "="],
172+
["keyword", "function"],
173+
["punctuation", "("],
174+
["punctuation", ")"],
175+
["punctuation", "{"],
176+
["punctuation", "}"],
177+
["punctuation", ","],
178+
["function-variable", "b"],
179+
["operator", "="],
180+
["punctuation", "("],
181+
["punctuation", ")"],
182+
["operator", "=>"],
183+
["punctuation", "{"],
99184
["punctuation", "}"]
100185
]
101186

tests/languages/javascript/function_feature.test

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ $()
88
ಠ_ಠ()
99
Ƞȡ_҇()
1010
if(notAFunction)
11+
foo({ x })
12+
foo({ y: fun() })
1113

1214
----------------------------------------------------
1315

@@ -21,7 +23,9 @@ if(notAFunction)
2123
["function", "$"], ["punctuation", "("], ["punctuation", ")"],
2224
["function", "ಠ_ಠ"], ["punctuation", "("], ["punctuation", ")"],
2325
["function", "Ƞȡ_҇"], ["punctuation", "("], ["punctuation", ")"],
24-
["keyword", "if"], ["punctuation", "("], "notAFunction", ["punctuation", ")"]
26+
["keyword", "if"], ["punctuation", "("], "notAFunction", ["punctuation", ")"],
27+
["function", "foo"], ["punctuation", "("], ["punctuation", "{"], " x ", ["punctuation", "}"], ["punctuation", ")"],
28+
["function", "foo"], ["punctuation", "("], ["punctuation", "{"], " y", ["punctuation", ":"], ["function", "fun"], ["punctuation", "("], ["punctuation", ")"], ["punctuation", "}"], ["punctuation", ")"]
2529
]
2630

2731
----------------------------------------------------

0 commit comments

Comments
 (0)
0