Description
Right now it doesn't seem possible to add an ImportDeclaration
programmatically via the Compiler API without updating the entire AST. My use-case is to be able to create https://github.com/zeit/styled-jsx with the TS Compiler over babel. When it detects a <style>
JSXElement, it will append import JSXStyle from 'styled-jsx
to the top of the code. I'd like to be able to do this too.
I've looked at how Typescript adds the tslib
and it seems like it uses an internal imports
key on the SourceFile
node. Would it be possible to give us access to that?
TypeScript Version: Version 2.4.1
Code
Sorry this isn't entirely self-contained, but the original is about 100 LOC. This should give you the gist, and it's mostly from the tslib
code in the compiler.
const node = (node as ts.Node) as ts.SourceFile
const externalHelpersModuleReference = ts.createLiteral('styled')
const importDecl = ts.createImportDeclaration(
/*decorators*/ undefined,
/*modifiers*/ undefined,
/*importClause*/ undefined
)
externalHelpersModuleReference.parent = importDecl
importDecl.parent = node
Expected behavior:
Get the result text back with an import statement at the top of the source file
Actual behavior:
No changes