Add line numbers to errors#322
Open
Andrew-Bonilla wants to merge 3 commits into
Open
Conversation
The acorn pre-parse was rejecting valid sketches that use top-level await (e.g. await loadScript for extensions), because acorn parses with script semantics by default. The actual eval wraps the code in an async IIFE, so these sketches run fine — only the pre-check was failing. Pass allowAwaitOutsideFunction: true so the syntax check matches what's actually executed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem: When a user runs code and an error occurs (syntax, nonexistent function), they are not given the line number of the issue. This can be a problem for live coding scenarios where the user is adding several lines all at once or is picking up off of incomplete code from a previous performer.
Solution: For runtime errors, parse the line number from the stack trace via err.stack. For syntax errors, use acorn (since it's already a project dependency) to parse the code before evaluation, giving the line number. The acorn error messages can sometimes be imprecise about which token/item is reported (reporting a space character ' ' instead of the symbol). Improving the quality of syntax error messages could be a future PR. I think it might require replacing acorn with something else. I didn't do this here because it seemed like too large of a change for this PR. Line numbers for syntax errors are still accurate even if the message text is not at times. It's also worth noting that runtime errors display as "message (line N)" while syntax errors follow acorn's format which includes the column position, for instance, "Unexpected token (3:5)". The column info is useful for finding the exact error location, but can be formatted out.
A utils directory and file src/utils/error-utils.js are now implemented to keep the logic in one place, used by both repl-v2.js and store.js.
Note: views/editor/repl.js appears to be unused legacy code, but I updated the error formatting calls to fit the new functionality.