diff --git a/CHANGES.md b/CHANGES.md index 67b5ce4a78..a1a2c1e838 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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][] diff --git a/src/languages/javascript.js b/src/languages/javascript.js index e51589fd29..e2c1243aff 100644 --- a/src/languages/javascript.js +++ b/src/languages/javascript.js @@ -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 diff --git a/test/markup/javascript/class.expect.txt b/test/markup/javascript/class.expect.txt index 8f0f53554c..cf8387ee8c 100644 --- a/test/markup/javascript/class.expect.txt +++ b/test/markup/javascript/class.expect.txt @@ -19,3 +19,10 @@ } 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) {} diff --git a/test/markup/javascript/class.txt b/test/markup/javascript/class.txt index db520f2a51..0ba06eab72 100644 --- a/test/markup/javascript/class.txt +++ b/test/markup/javascript/class.txt @@ -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) {}