Skip to content

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

@lumirlumir

Description

@lumirlumir
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!

Activity

nzakas

nzakas commented on Mar 14, 2025

@nzakas
Member

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.

moved this from Needs Triage to Evaluating in Triageon Mar 14, 2025
lumirlumir

lumirlumir commented on Mar 15, 2025

@lumirlumir
MemberAuthor

@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

nzakas commented on Mar 17, 2025

@nzakas
Member

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.

self-assigned this
on May 14, 2025
nzakas

nzakas commented on May 14, 2025

@nzakas
Member

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
moved this from Evaluating to Implementing in Triageon Jun 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

Implementing

Milestone

No milestone

Relationships

None yet

    Development

    Participants

    @nzakas@lumirlumir

    Issue actions

      Change Request: Support `getLocFromIndex()` and `getIndexFromLoc()` methods for `TextSourceCodeBase` class · Issue #166 · eslint/rewrite