Skip to content

Change Request: Support getLocFromIndex() and getIndexFromLoc() methods for TextSourceCodeBase class #166

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

Open
2 of 8 tasks
lumirlumir opened this issue Mar 14, 2025 · 4 comments
Assignees
Labels
enhancement New feature or request

Comments

@lumirlumir
Copy link
Member

Which packages would you like to change?

  • @eslint/compat
  • @eslint/config-array
  • @eslint/config-helpers
  • @eslint/core
  • @eslint/migrate-config
  • @eslint/object-schema
  • @eslint/plugin-kit

What problem do you want to solve?

Hello,

Currently, when I try to use getLocFromIndex() or getIndexFromLoc() in languages like @eslint/markdown, I encounter an error stating that context.sourceCode.getLocFromIndex() is not a function.

I believe this error occurs because these methods are not implemented.

These methods are highly useful when creating custom rules, so I’d like to suggest implementing them in the TextSourceCodeBase class for more general usage.


/** @type {RuleModule} */
export default {
  create(context) {
    return {
      /** @param {Text} node */
      text(node) {
        context.sourceCode.getLocFromIndex(index)
      },
    };
  },
};

When I attempt to access getLocFromIndex() or getIndexFromLoc() in the context of markdown, css, or json, I encounter an error stating "getLocFromIndex() is not a function."

What do you think is the correct solution?

Please add support for both methods.

The original implementations of getLocFromIndex() and getIndexFromLoc() can be found in the following link:

https://github.com/eslint/eslint/blob/main/lib/languages/js/source-code/source-code.js#L654-L736

We can add support for these methods by implementing them in the TextSourceCodeBase class, which is linked below:

https://github.com/eslint/rewrite/blob/main/packages/plugin-kit/src/source-code.js#L217-L361

Participation

  • I am willing to submit a pull request for this change.

Additional comments

If you need more context, please feel free to let me know!

@lumirlumir lumirlumir added the enhancement New feature or request label Mar 14, 2025
@github-project-automation github-project-automation bot moved this to Needs Triage in Triage Mar 14, 2025
@nzakas
Copy link
Member

nzakas commented Mar 14, 2025

Thanks for pointing this out. Yes, it's true that not all languages will support the same method on their SourceCode objects. The implementation of these methods is quite heavy and take up a lot more code that what you have highlighted there, that's why they're not included by default.

We can see if there's a more performant and language-agnostic way to do this when we have time.

@nzakas nzakas moved this from Needs Triage to Evaluating in Triage Mar 14, 2025
@lumirlumir
Copy link
Member Author

@nzakas Thanks for reviewing!

Then, should this feature request be put on hold until the ESLint team finds an effective solution to this issue?

@nzakas
Copy link
Member

nzakas commented Mar 17, 2025

Yes. We'll leave this open to investigate a bit. I think it's an interesting problem so I'd like to look into it when I have some time.

@nzakas
Copy link
Member

nzakas commented May 14, 2025

Some hints around performance:

  1. We should be caching every index's location after it's calculated.
  2. We should keep track of every index that begins a new line when we find it
  3. We may be able to use the loc property on nodes to cheat when calculating some locations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Evaluating
Development

No branches or pull requests

2 participants