Skip to content

Commit

Permalink
fix(transform): fix addEsmImports
Browse files Browse the repository at this point in the history
fixes #1761
  • Loading branch information
manucorporat committed Jul 25, 2019
1 parent 41c4f75 commit 5819328
Showing 1 changed file with 10 additions and 38 deletions.
48 changes: 10 additions & 38 deletions src/compiler/transformers/add-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const addEsmImports = (tsSourceFile: ts.SourceFile, importFnNames: string[], imp
// ESM Imports
// import { importNames } from 'importPath';

let importSpecifiers = importFnNames.map(importKey => {
const importSpecifiers = importFnNames.map(importKey => {
const splt = importKey.split(' as ');
let importAs = importKey;
let importFnName = importKey;
Expand All @@ -38,44 +38,16 @@ const addEsmImports = (tsSourceFile: ts.SourceFile, importFnNames: string[], imp
});

const statements = tsSourceFile.statements.slice();

const existingImportIndex = statements.findIndex(s => {
return ts.isImportDeclaration(s) &&
s.moduleSpecifier &&
(s.moduleSpecifier as ts.StringLiteral).text === importPath;
});

if (existingImportIndex > -1) {
const existingImport = statements[existingImportIndex] as ts.ImportDeclaration;

importSpecifiers = [
...(existingImport.importClause.namedBindings as ts.NamedImports).elements.map(elm => elm),
...importSpecifiers
];

statements[existingImportIndex] = ts.updateImportDeclaration(
existingImport,
undefined,
undefined,
ts.createImportClause(
undefined,
ts.createNamedImports(importSpecifiers)
),
ts.createLiteral(importPath)
);

} else {
const newImport = ts.createImportDeclaration(
undefined,
const newImport = ts.createImportDeclaration(
undefined,
undefined,
ts.createImportClause(
undefined,
ts.createImportClause(
undefined,
ts.createNamedImports(importSpecifiers)
),
ts.createLiteral(importPath)
);
statements.unshift(newImport);
}
ts.createNamedImports(importSpecifiers)
),
ts.createLiteral(importPath)
);
statements.unshift(newImport);

return ts.updateSourceFileNode(tsSourceFile, statements);
};
Expand Down

0 comments on commit 5819328

Please sign in to comment.