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 getLoc/getRange to SourceCode interface #89

Merged
merged 3 commits into from
Jul 22, 2024
Merged

Conversation

nzakas
Copy link
Member

@nzakas nzakas commented Jul 17, 2024

Prerequisites checklist

What is the purpose of this pull request?

Updates the SourceCode interface with getLoc() and getRange() methods.

What changes did you make? (Give an overview)

  • Added getLoc() method to SourceCode interface
  • Added getRange() method to SourceCode interface
  • Added SourceRange interface
  • Updated SyntaxElement type to be a union of several popular AST-location formats
  • Added an optional offset property to Position, as many ASTs support that

Related Issues

Refs eslint/eslint#18695

Is there anything you'd like reviewers to focus on?

I marked this as breaking as existing implementations of SourceCode would now be invalid.

Also, I thought that including different formats of SyntaxElement would provide the best developer experience. Otherwise, I'd need to set SourceCode.ast to Object, so there would effectively be no type checking or intellisense available. I'm not sure if that makes the most sense.

* Represents a location coordinate inside the source.
* Represents the start and end coordinates of a node inside the source with an offset.
*/
export interface SourceLocationWithOffset {
Copy link
Member Author

Choose a reason for hiding this comment

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

Keeping this as it seems like it would still be helpful for developers.

/**
* Represents a location coordinate inside the source with an offset.
*/
export interface PositionWithOffset extends Position {
Copy link
Member Author

Choose a reason for hiding this comment

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

Same here

Comment on lines +180 to +181
node: object,
ancestry: Array<object>,
Copy link
Member Author

Choose a reason for hiding this comment

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

Using object instead of Object to enforce that this cannot be a primitive value.

@@ -222,7 +242,7 @@ export interface File {
/**
* Represents the successful result of parsing a file.
*/
export interface OkParseResult {
export interface OkParseResult<T extends object = object> {
Copy link
Member Author

Choose a reason for hiding this comment

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

Making this generic allows devs to specify the type of the ast property to get intellisense completions.

Copy link
Member

@mdjermanovic mdjermanovic left a comment

Choose a reason for hiding this comment

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

LGTM, thanks! Node 22 CI is currently broken so holding off merging until that's fixed.

@nzakas
Copy link
Member Author

nzakas commented Jul 19, 2024

@mdjermanovic just a reminder: when it looks like a PR is ready to merge, please move to "Merge Candidates"

@nzakas
Copy link
Member Author

nzakas commented Jul 22, 2024

This is blocking me currently, so going to merge to get moving again.

@nzakas nzakas merged commit d51f979 into main Jul 22, 2024
14 checks passed
@nzakas nzakas deleted the loc-and-range branch July 22, 2024 15:07
@github-actions github-actions bot mentioned this pull request Jul 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants