Skip to content

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

Open
@lumirlumir

Description

@lumirlumir

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!

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

Implementing

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions