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

Enhance showPosition() to Accurately Display Error Position by Line Number #406

Open
satyajitnayk opened this issue Dec 12, 2023 · 0 comments

Comments

@satyajitnayk
Copy link

satyajitnayk commented Dec 12, 2023

@zaach @JamieSlome can we enhance showPosition() ??
The showPosition() function, found here, can be enhanced to provide a detailed error context according to the specific line number.

This modification ensures that the ----^ indicator accurately reflects the error position on the relevant line, rather than defaulting to the last line:

            showPosition: function showPosition() {
                var pre = this.pastInput();
                var upcoming = this.upcomingInput();
                var allText = pre + upcoming;
                var lines = allText.split("\n");

                // Calculate line number and column of the caret
                var errorLine = pre.split("\n").length;
                var errorColumn = pre.length - pre.lastIndexOf("\n") - 1;

                // Build the output with caret under the error position
                var output = "";
                for (var i = 0; i < lines.length; i++) {
                    output += lines[i] + "\n";
                    if (i === errorLine - 1) {
                        output += new Array(errorColumn + 1).join("-") + "^" + "\n";
                    }
                }
                return output;
            }
  1. Current error message:
...LLO{{/if}}
    {{#if}
    HELLO
    {{/i
-----------------------^
  1. New error message:
Parse error on line 2:
...LLO{{/if}}
    {{#if}
---------^
    HELLO
    {{/i
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

No branches or pull requests

1 participant