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

feat: add startIndex parser option #16849

Merged
merged 7 commits into from
Oct 23, 2024
Merged

Conversation

DylanPiercey
Copy link
Contributor

@DylanPiercey DylanPiercey commented Sep 20, 2024

Q                       A
Fixed Issues?
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature? Yes
Tests Added + Pass? Yes
Documentation PR Link babel/website#2971
Any Dependency Changes?
License MIT

Currently @babel/parser supports startColumn and startLine but no way to ensure the correct location information for indexes (eg start, end and range on node, index on loc).

This PR adds a new startIndex override which ensures all created position information on the AST is offset by the startIndex.

@babel-bot
Copy link
Collaborator

babel-bot commented Sep 20, 2024

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/58201

@nicolo-ribaudo nicolo-ribaudo added PR: New Feature 🚀 A type of pull request used for our changelog categories pkg: parser labels Sep 20, 2024
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems ok to me

options.startIndex = options.startColumn;
} else if (process.env.BABEL_8_BREAKING) {
throw new Error(
"When `startColumn` is used with a `startLine > 1` you must also provide a `startIndex`.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When startLine is 2 and startColumn is 0, we should still throw if startIndex is undefined.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JLHwung are you suggesting just changing options.startColumn > 0 to options.startColumn >= 0?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 That should work for this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, I've updated the pr 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JLHwung I've refactored the default/error checking code slightly and added another test.

I think it errors in all the cases we want now.

@nicolo-ribaudo nicolo-ribaudo added this to the v7.26.0 milestone Sep 26, 2024
@JLHwung JLHwung added PR: Ready to be Merged A pull request with already two approvals, but waiting for the next minor release PR: Needs Docs labels Sep 27, 2024
@JLHwung
Copy link
Contributor

JLHwung commented Sep 27, 2024

@DylanPiercey Thank you. This PR will be included in the next minor release. Could you prepare a docs PR? You can start by editing https://github.com/babel/website/blob/main/docs/parser.md.

@DylanPiercey
Copy link
Contributor Author

@JLHwung thanks for working with me on this! I've now added a PR for the docs website.

babel/website#2971

@nicolo-ribaudo nicolo-ribaudo changed the title feat: add startIndex parser config feat: add startIndex parser option Oct 23, 2024
@nicolo-ribaudo nicolo-ribaudo merged commit 77bc5d5 into babel:main Oct 23, 2024
52 checks passed
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jan 23, 2025
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 23, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: parser PR: New Feature 🚀 A type of pull request used for our changelog categories PR: Ready to be Merged A pull request with already two approvals, but waiting for the next minor release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants