Skip to content

Commit ac0e81f

Browse files
committed
TypescriptAPIUtils.appendImport: add imports after the last one
1 parent da2eb05 commit ac0e81f

File tree

5 files changed

+12
-2
lines changed

5 files changed

+12
-2
lines changed

packages/typescript-api-utils/sourceFiles/appendImport.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as ts from 'typescript';
66
export function appendImport(moduleSpecifier: string, propertyName: string|undefined, name: string, sourceFile: ts.SourceFile): string
77
{
88
const imports = findNodesByType<ts.ImportDeclaration>(ts.SyntaxKind.ImportDeclaration, sourceFile);
9-
const pos = imports.length ? (sourceFile.statements.indexOf(imports[imports.length - 1]) || 0) : 0;
9+
const pos = imports.length ? (sourceFile.statements.indexOf(imports[imports.length - 1]) + 1 || 0) : 0;
1010

1111
const existingImport: ts.ImportDeclaration = find(imports, (existingImport: ts.ImportDeclaration) => {
1212
return (
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import {A} from '/import_1';
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { A } from "/import_1";
2+
import { B } from "/import_2";

packages/typescript-api-utils/tests/tests/sourceFiles/appendImport.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,14 @@ describe('#sourceFiles/appendImport', () => {
106106
compareSourceFile('valid_8.updated', sourceFile);
107107
});
108108

109+
it('should add import after the last one', () => {
110+
const sourceFile = getFileSource('valid_9.original');
111+
const imported = appendImport('/import_2', undefined, 'B', sourceFile);
112+
113+
expect(imported).to.be.equal('B');
114+
compareSourceFile('valid_9.updated', sourceFile);
115+
});
116+
109117
});
110118

111119
});

packages/typescript-api-utils/tests/tests/sourceFiles/findNodesByType.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ describe('#sourceFiles/findNodesByType', () => {
1616

1717
const nodes = findNodesByType<ts.Identifier>(ts.SyntaxKind.Identifier, sourceFile);
1818

19-
console.log(nodes);
2019
expect(nodes.length).to.be.equal(2);
2120
expect(nodes[0].text).to.be.equal('a');
2221
expect(nodes[1].text).to.be.equal('b');

0 commit comments

Comments
 (0)