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

Conversation

PierceLBrooks
Copy link
Contributor

@PierceLBrooks PierceLBrooks commented Jan 17, 2023

Description

  • Source branch in your fork has meaningful name (not main)

Hello there! I recently discovered an interesting piece of apparent javascript code that I wanted to inspect further at https://www.scribd.com/document/512791356/Player - which has been attached here - and it was found that neither the https://beautifier.io/ web interface nor this python module could beautify the content without either hanging indefinitely or throwing an exception like this one below:

pbrooks@MacBook-Pro Downloads % python3 ./jsbeautify.py file://$PWD/Player.js ./Vimeo.js
Traceback (most recent call last):
  File "/Users/pbrooks/Downloads/./jsbeautify.py", line 40, in <module>
    main()
  File "/Users/pbrooks/Downloads/./jsbeautify.py", line 33, in main
    js = beauty(r["content"])
  File "/Users/pbrooks/Downloads/./jsbeautify.py", line 12, in beauty
    return jsbeautifier.beautify(content.decode())
  File "/opt/homebrew/lib/python3.10/site-packages/jsbeautifier/__init__.py", line 82, in beautify
    return b.beautify(string, opts)
  File "/opt/homebrew/lib/python3.10/site-packages/jsbeautifier/javascript/beautifier.py", line 187, in beautify
    self._tokens = Tokenizer(source_text, self._options).tokenize()
  File "/opt/homebrew/lib/python3.10/site-packages/jsbeautifier/core/tokenizer.py", line 71, in tokenize
    current = self.__get_next_token_with_comments(previous, open_token)
  File "/opt/homebrew/lib/python3.10/site-packages/jsbeautifier/core/tokenizer.py", line 93, in __get_next_token_with_comments
    current = self._get_next_token(previous, open_token)
  File "/opt/homebrew/lib/python3.10/site-packages/jsbeautifier/javascript/tokenizer.py", line 226, in _get_next_token
    token = token or self._read_regexp(c, previous_token)
  File "/opt/homebrew/lib/python3.10/site-packages/jsbeautifier/javascript/tokenizer.py", line 331, in _read_regexp
    if c == "/" and self.allowRegExOrXML(previous_token):
  File "/opt/homebrew/lib/python3.10/site-packages/jsbeautifier/javascript/tokenizer.py", line 514, in allowRegExOrXML
    and previous_token.opened.previous.type == TOKEN.RESERVED
AttributeError: 'NoneType' object has no attribute 'previous'

The beautification script I used can be located on this page... https://gist.github.com/PierceLBrooks/7a29d53f4915f847b82e41e2954ca000

Before Merge Checklist

These items can be completed after PR is created.

(Check any items that are not applicable (NA) for this PR)

  • JavaScript implementation
  • Python implementation (NA if HTML beautifier)
  • Added Tests to data file(s)
  • Added command-line option(s) (NA if
  • README.md documents new feature/option(s)

Player.txt

@bitwiseman
Copy link
Member

Could you add a test with the input that crashes without this fix?

@bitwiseman bitwiseman marked this pull request as draft July 5, 2023 19:56
@UlyssesWu
Copy link

AttributeError: 'NoneType' object has no attribute 'previous'

Same problem here. This fix still works.

@bitwiseman
Copy link
Member

@UlyssesWu
What is an input that causes this?

@PierceLBrooks
Copy link
Contributor Author

@bitwiseman
Copy link
Member

@PierceLBrooks
Please add the following the regression tests in test/data/javascript/tests.js

        {
          comment: 'Issue #2128 - NPE in python implementation',
          fragment: true,
          unchanged: [
            ') / a / g'
          ]
        },

This input fails before this PR and succeeds with this change in place.

@PierceLBrooks
Copy link
Contributor Author

PierceLBrooks commented Mar 3, 2025

@bitwiseman I forgot all about your original unit test request! My bad.

PierceLBrooks@de2b0c6

^ Does this suffice? Your unit test framework is unfamiliar to me unfortunately.

@PierceLBrooks PierceLBrooks marked this pull request as ready for review March 3, 2025 23:43
@PierceLBrooks
Copy link
Contributor Author

The failure encountered by the https://github.com/beautifier/js-beautify/actions/runs/13642772279 CI run appears to have been on account of the issue manifesting not within the fixed Python code, but by the at-that-time-yet-to-be-fixed Javascript code's version of the same bug. This should now be resolved by https://github.com/beautifier/js-beautify/actions/runs/13643064944

@bitwiseman bitwiseman merged commit 03e3cc0 into beautifier:main Mar 4, 2025
12 checks passed
@bitwiseman bitwiseman added this to the v2.0.0 (next release) milestone Mar 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants