Skip to content

Commit

Permalink
fix(js/ts) Prevent for/while/if/switch from falsly matching as functi…
Browse files Browse the repository at this point in the history
…ons (#2803)
  • Loading branch information
joshgoebel committed Nov 2, 2020
1 parent 3971170 commit bfbaa08
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -10,6 +10,7 @@ New Languages:

Language Improvements:

- fix(js/ts) Prevent for/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 for"
},
{
className: 'function',
// we have to count the parens to make sure we actually have the correct
Expand Down
7 changes: 7 additions & 0 deletions test/markup/javascript/class.expect.txt
Expand Up @@ -19,3 +19,10 @@
}
<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">for</span>(expr; expr; expr) {}
<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) {}
7 changes: 7 additions & 0 deletions test/markup/javascript/class.txt
Expand Up @@ -19,3 +19,10 @@ class Car extends Vehicle {
}
onemore(a=(3+2, b=(5*9))) {}
}

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

0 comments on commit bfbaa08

Please sign in to comment.