I have a use case where I want to replace variable function calls, specifically foo.value.toString()
, with a helper function getStringValue(foo)
. If I find it, I can use the fix to replace the text on the CallExpression
node, so my rule fix currently looks like this:
fixer => fixer.replaceText(node, `getStringValue(${identifierNode.getText()})`);
The problem with automatically fixing this error this way is that getStringValue
may or may not have been imported into the file. I want this fix to have the following behavior:
As far as I understand from the documentation, there is no easy way to access the root ESTree node using a fixer
or context
object. The closest is SourceCode.getText()
, which means I have to parse the source text to resolve the import - I'd rather interact with the entire AST directly. What is the best way to perform this automated import process?
If you want to be slightly unsafe here, you can assume that the user has not redefined the
getStringValue
function locally in their files (usually a safe assumption if you have a codebase to which this rule applies ).In this case, the best way is to use a selector to check the import, for example:
It turns out there is an easy way to extract the AST root node from the
context
object. It is located atcontext.getSourceCode().ast
. I rewrote my fix with the following logic: