Skip to content

Commit 03e3cc0

Browse files
authored
Merge pull request #2128 from PierceLBrooks/plb_fix-minor-crash
Fix minor crash for certain oddly formed *.js files
2 parents dff124e + 7959281 commit 03e3cc0

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

js/src/javascript/tokenizer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ Tokenizer.prototype._allow_regexp_or_xml = function(previous_token) {
371371
// regex and xml can only appear in specific locations during parsing
372372
return (previous_token.type === TOKEN.RESERVED && in_array(previous_token.text, ['return', 'case', 'throw', 'else', 'do', 'typeof', 'yield'])) ||
373373
(previous_token.type === TOKEN.END_EXPR && previous_token.text === ')' &&
374-
previous_token.opened.previous.type === TOKEN.RESERVED && in_array(previous_token.opened.previous.text, ['if', 'while', 'for'])) ||
374+
previous_token.opened && previous_token.opened.previous.type === TOKEN.RESERVED && in_array(previous_token.opened.previous.text, ['if', 'while', 'for'])) ||
375375
(in_array(previous_token.type, [TOKEN.COMMENT, TOKEN.START_EXPR, TOKEN.START_BLOCK, TOKEN.START,
376376
TOKEN.END_BLOCK, TOKEN.OPERATOR, TOKEN.EQUALS, TOKEN.EOF, TOKEN.SEMICOLON, TOKEN.COMMA
377377
]));

python/jsbeautifier/javascript/tokenizer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ def allowRegExOrXML(self, previous_token):
523523
or (
524524
previous_token.type == TOKEN.END_EXPR
525525
and previous_token.text == ")"
526+
and previous_token.opened != None
526527
and previous_token.opened.previous.type == TOKEN.RESERVED
527528
and previous_token.opened.previous.text in {"if", "while", "for"}
528529
)

test/data/javascript/tests.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5890,6 +5890,14 @@ exports.test_data = {
58905890
comment: 'Issue #1896: Handle newlines with bitwise ~ operator',
58915891
input: 'if (foo) {\nvar bar = 1;\n~bar ? 0 : 1\n }',
58925892
output: 'if (foo) {\n var bar = 1;\n ~bar ? 0 : 1\n}'
5893+
},
5894+
5895+
{
5896+
comment: 'Issue #2128 - NPE in python implementation',
5897+
fragment: true,
5898+
unchanged: [
5899+
') / a / g'
5900+
]
58935901
}
58945902
]
58955903
}

0 commit comments

Comments
 (0)