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

Negative column with multi-line string literal #2032

Closed
lahma opened this issue Aug 30, 2020 · 5 comments
Closed

Negative column with multi-line string literal #2032

lahma opened this issue Aug 30, 2020 · 5 comments

Comments

@lahma
Copy link
Contributor

lahma commented Aug 30, 2020

When parsing a multi-line string literal that has windows line feeds (\r\n) a negative column number is produced.

Steps to reproduce

var tree = esprima.parseScript("var str = 'test \\\r\nd'+'test \\\r\ntest'+'f';", { loc: true });
tree["body"][0].declarations[0].init.left.loc
JSON.stringify(tree["body"][0].declarations[0].init.left.loc)

Expected output

{"start":{"line":1,"column":10},"end":{"line":3,"column":5}}

Actual output

{"start":{"line":1,"column":-9},"end":{"line":3,"column":5}}

Relevant references

I see there's been such problem before too: #1844

@lahma
Copy link
Contributor Author

lahma commented Aug 30, 2020

I think the problem is here:

esprima/src/parser.ts

Lines 1514 to 1521 in d277380

while ((stack.length > 2) && (prec <= precedences[precedences.length - 1])) {
right = stack.pop();
const operator = stack.pop();
precedences.pop();
left = stack.pop();
markers.pop();
const node = this.startNode(markers[markers.length - 1]);
stack.push(this.finalize(node, new Node.BinaryExpression(operator, left, right)));

This code path doesn't check for the popped marker and use its lineStart as lastLineStart for startNode call like the code below it does:

esprima/src/parser.ts

Lines 1536 to 1544 in d277380

while (i > 1) {
const marker = markers.pop();
const lastLineStart = lastMarker && lastMarker.lineStart;
const node = this.startNode(marker, lastLineStart);
const operator = stack[i - 1];
expr = this.finalize(node, new Node.BinaryExpression(operator, stack[i - 2], expr));
i -= 2;
lastMarker = marker;
}

I see that tests are explicitly checked not to contain windows line feeds so I'm unsure how to create a PR for this.

@lahma
Copy link
Contributor Author

lahma commented Aug 30, 2020

Created PR #2033

@lahma
Copy link
Contributor Author

lahma commented Sep 8, 2020

ping @ariya 🙂

@KFlash
Copy link

KFlash commented Sep 8, 2020

There already exist issues and PR"s created years back that he haven't responded too yet. Expect him to be a slow replier.

@lahma
Copy link
Contributor Author

lahma commented Sep 8, 2020

And it's all fine, I understand that when it's free work one can choose his priorities. I just have some wishful thinking to verify the approach to get that on .NET version's side where we actually throw an error when we encounter invalid index values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants