-
-
Notifications
You must be signed in to change notification settings - Fork 28
Description
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
To pick up a draggable item, press the space bar. While dragging, use the arrow keys to move the item. Press space again to drop the item in its new position, or press escape to cancel.
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.To pick up a draggable item, press the space bar. While dragging, use the arrow keys to move the item. Press space again to drop the item in its new position, or press escape to cancel.
Additional comments
If you need more context, please feel free to let me know!
Metadata
Metadata
Assignees
Labels
Type
Projects
Status
Activity
nzakas commentedon 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.
lumirlumir commentedon Mar 15, 2025
@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 commentedon 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.
getLines
method in TextSourceCodeBase class #180getLocFromIndex
andgetIndexFromLoc
eslint/markdown#376nzakas commentedon May 14, 2025
Some hints around performance:
loc
property on nodes to cheat when calculating some locationsgetLocFromIndex
andgetIndexFromLoc
#212getLocFromIndex
eslint/eslint#19782