Skip to content

Commit

Permalink
Merge pull request #2128 from PierceLBrooks/plb_fix-minor-crash
Browse files Browse the repository at this point in the history
Fix minor crash for certain oddly formed *.js files
  • Loading branch information
bitwiseman authored Mar 4, 2025
2 parents dff124e + 7959281 commit 03e3cc0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion js/src/javascript/tokenizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
]));
Expand Down
1 change: 1 addition & 0 deletions python/jsbeautifier/javascript/tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,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"}
)
Expand Down
8 changes: 8 additions & 0 deletions test/data/javascript/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]
}
]
}
Expand Down

0 comments on commit 03e3cc0

Please sign in to comment.