-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add imports util to cover all possible cases
- Loading branch information
1 parent
f45952b
commit 9013404
Showing
4 changed files
with
62 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
function addNamedImport(context, fixer, importNode, importName, importPath, importAsType = false) { | ||
const fixes = []; | ||
|
||
if (importNode) { | ||
const alreadyImported = importNode.specifiers.some( | ||
specifier => specifier.imported.name === importName | ||
); | ||
|
||
if(!alreadyImported) { | ||
const lastSpecifier = importNode.specifiers[importNode.specifiers.length - 1]; | ||
|
||
// Add ValueOf to existing type-fest import | ||
fixes.push(fixer.insertTextAfter(lastSpecifier, `, ${importName}`)); | ||
} | ||
} else { | ||
// Add import if it doesn't exist | ||
fixes.push( | ||
fixer.insertTextBefore( | ||
context.getSourceCode().ast.body[0], | ||
`import ${importAsType ? "type " : ""}{${importName}} from '${importPath}';\n` | ||
) | ||
); | ||
} | ||
|
||
return fixes; | ||
} | ||
|
||
module.exports = { | ||
addNamedImport | ||
}; |