Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(js/ts) Prevent while/if/switch from falsly matching as functions #2803

Merged
merged 5 commits into from Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -2,6 +2,7 @@

Language Improvements:

- fix(js/ts) Prevent while/if/switch from falsly matching as functions (#2803) [Josh Goebel][]
- enh(julia) Update keyword lists for Julia 1.x (#2781) [Fredrik Ekre][]
- enh(python) Match numeric literals per the language reference [Richard Gibson][]
- enh(ruby) Match numeric literals per language documentation [Richard Gibson][]
Expand Down
5 changes: 5 additions & 0 deletions src/languages/javascript.js
Expand Up @@ -359,6 +359,11 @@ export default function(hljs) {
],
illegal: /%/
},
{
// prevent this from getting swallowed up by function
// since they appear "function like"
beginKeywords: "while if switch catch"
},
{
className: 'function',
// we have to count the parens to make sure we actually have the correct
Expand Down
6 changes: 6 additions & 0 deletions test/markup/javascript/class.expect.txt
Expand Up @@ -19,3 +19,9 @@
}
<span class="hljs-function"><span class="hljs-title">onemore</span>(<span class="hljs-params">a=(<span class="hljs-number">3</span>+<span class="hljs-number">2</span>, b=(<span class="hljs-number">5</span>*<span class="hljs-number">9</span>))</span>)</span> {}
}

<span class="hljs-comment">// these should not be matched as class functions</span>
<span class="hljs-keyword">while</span>(value) {}
<span class="hljs-keyword">if</span>(value) {}
<span class="hljs-keyword">switch</span>(value) {}
<span class="hljs-keyword">try</span> {} <span class="hljs-keyword">catch</span>(err) {}
6 changes: 6 additions & 0 deletions test/markup/javascript/class.txt
Expand Up @@ -19,3 +19,9 @@ class Car extends Vehicle {
}
onemore(a=(3+2, b=(5*9))) {}
}

// these should not be matched as class functions
while(value) {}
if(value) {}
switch(value) {}
try {} catch(err) {}