Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix minor crash for certain oddly formed *.js files #2128

Merged
merged 5 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading