From 95498f0341ec414b0324855107b27b38e7688395 Mon Sep 17 00:00:00 2001 From: Pierce Brooks Date: Tue, 17 Jan 2023 11:27:45 -0500 Subject: [PATCH 1/3] fix minor crash seemingly arising from unterminated encapsulation token pairings --- python/jsbeautifier/javascript/tokenizer.py | 1 + 1 file changed, 1 insertion(+) diff --git a/python/jsbeautifier/javascript/tokenizer.py b/python/jsbeautifier/javascript/tokenizer.py index a121a31ac..0787dbca8 100644 --- a/python/jsbeautifier/javascript/tokenizer.py +++ b/python/jsbeautifier/javascript/tokenizer.py @@ -526,6 +526,7 @@ def allowRegExOrXML(self, previous_token): or ( previous_token.type == TOKEN.END_EXPR and previous_token.text == ")" + and previous_token.opened != None and previous_token.opened.previous.type == TOKEN.RESERVED and previous_token.opened.previous.text in {"if", "while", "for"} ) From de2b0c6754248572a69b02154c36ce208ef01fbd Mon Sep 17 00:00:00 2001 From: Pierce Brooks Date: Mon, 3 Mar 2025 18:41:39 -0500 Subject: [PATCH 2/3] finally acquiesce to unit test request ( https://github.com/beautifier/js-beautify/pull/2128#issuecomment-2695794024 ) --- test/data/javascript/tests.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/data/javascript/tests.js b/test/data/javascript/tests.js index dd0f31690..c37efdcb4 100644 --- a/test/data/javascript/tests.js +++ b/test/data/javascript/tests.js @@ -5890,6 +5890,14 @@ exports.test_data = { comment: 'Issue #1896: Handle newlines with bitwise ~ operator', input: 'if (foo) {\nvar bar = 1;\n~bar ? 0 : 1\n }', output: 'if (foo) {\n var bar = 1;\n ~bar ? 0 : 1\n}' + }, + + { + comment: 'Issue #2128 - NPE in python implementation', + fragment: true, + unchanged: [ + ') / a / g' + ] } ] } From 7959281a16852779a16fae9942c929c16c0d20dd Mon Sep 17 00:00:00 2001 From: Pierce Brooks Date: Mon, 3 Mar 2025 19:04:59 -0500 Subject: [PATCH 3/3] fix the javascript version of the python bug too --- js/src/javascript/tokenizer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/src/javascript/tokenizer.js b/js/src/javascript/tokenizer.js index ea3f2e26c..2b2b73fb0 100644 --- a/js/src/javascript/tokenizer.js +++ b/js/src/javascript/tokenizer.js @@ -371,7 +371,7 @@ Tokenizer.prototype._allow_regexp_or_xml = function(previous_token) { // regex and xml can only appear in specific locations during parsing return (previous_token.type === TOKEN.RESERVED && in_array(previous_token.text, ['return', 'case', 'throw', 'else', 'do', 'typeof', 'yield'])) || (previous_token.type === TOKEN.END_EXPR && previous_token.text === ')' && - previous_token.opened.previous.type === TOKEN.RESERVED && in_array(previous_token.opened.previous.text, ['if', 'while', 'for'])) || + previous_token.opened && previous_token.opened.previous.type === TOKEN.RESERVED && in_array(previous_token.opened.previous.text, ['if', 'while', 'for'])) || (in_array(previous_token.type, [TOKEN.COMMENT, TOKEN.START_EXPR, TOKEN.START_BLOCK, TOKEN.START, TOKEN.END_BLOCK, TOKEN.OPERATOR, TOKEN.EQUALS, TOKEN.EOF, TOKEN.SEMICOLON, TOKEN.COMMA ]));