From f86b69476bbf52b1b84796ad831f4e8f011f36c6 Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Fri, 30 Oct 2020 02:05:10 -0400 Subject: [PATCH 1/4] fix(js/ts) Prevent while/if/switch from falsly matching as functions --- CHANGES.md | 1 + src/languages/javascript.js | 5 +++++ test/markup/javascript/class.expect.txt | 6 ++++++ test/markup/javascript/class.txt | 6 ++++++ 4 files changed, 18 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 9a6b14c6fa..ffee26e7a6 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,7 @@ Language Improvements: +- fix(js/ts) Prevent while/if/switch from falsly matching as functions () [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..bbcf682417 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" + }, { 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..f460b1be25 100644 --- a/test/markup/javascript/class.expect.txt +++ b/test/markup/javascript/class.expect.txt @@ -19,3 +19,9 @@ } onemore(a=(3+2, b=(5*9))) {} } + +// these should not be matched as class functions +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..5cd42b2ea2 100644 --- a/test/markup/javascript/class.txt +++ b/test/markup/javascript/class.txt @@ -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) {} From e947d1652851052347e91a4ecfa60f4b65bb888e Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Fri, 30 Oct 2020 02:05:36 -0400 Subject: [PATCH 2/4] add pr --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index ffee26e7a6..dd09f1fa59 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,7 +2,7 @@ Language Improvements: -- fix(js/ts) Prevent while/if/switch from falsly matching as functions () [Josh Goebel][] +- 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][] From 6377981df1d5774a8c9155adc90e61d69b7dea9f Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Mon, 2 Nov 2020 07:56:21 -0500 Subject: [PATCH 3/4] add for --- src/languages/javascript.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/languages/javascript.js b/src/languages/javascript.js index bbcf682417..e2c1243aff 100644 --- a/src/languages/javascript.js +++ b/src/languages/javascript.js @@ -362,7 +362,7 @@ export default function(hljs) { { // prevent this from getting swallowed up by function // since they appear "function like" - beginKeywords: "while if switch catch" + beginKeywords: "while if switch catch for" }, { className: 'function', From bb33d262fdf14272f41eeb2da70f71690497e801 Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Mon, 2 Nov 2020 15:16:56 -0500 Subject: [PATCH 4/4] update tests/changlog --- CHANGES.md | 2 +- test/markup/javascript/class.expect.txt | 1 + test/markup/javascript/class.txt | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 828fce9354..fca1dcead6 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,7 +2,7 @@ Language Improvements: -- fix(js/ts) Prevent while/if/switch from falsly matching as functions (#2803) [Josh Goebel][] +- 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/test/markup/javascript/class.expect.txt b/test/markup/javascript/class.expect.txt index f460b1be25..cf8387ee8c 100644 --- a/test/markup/javascript/class.expect.txt +++ b/test/markup/javascript/class.expect.txt @@ -21,6 +21,7 @@ } // these should not be matched as class functions +for(expr; expr; expr) {} while(value) {} if(value) {} switch(value) {} diff --git a/test/markup/javascript/class.txt b/test/markup/javascript/class.txt index 5cd42b2ea2..0ba06eab72 100644 --- a/test/markup/javascript/class.txt +++ b/test/markup/javascript/class.txt @@ -21,6 +21,7 @@ class Car extends Vehicle { } // these should not be matched as class functions +for(expr; expr; expr) {} while(value) {} if(value) {} switch(value) {}