Skip to content

Commit 6b750ac

Browse files
committed
TypescriptAPIUtils: add AppendImportsBuffer class
1 parent ac0e81f commit 6b750ac

File tree

6 files changed

+55
-2
lines changed

6 files changed

+55
-2
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {findNodesByType} from './findNodesByType';
33
import * as ts from 'typescript';
44

55

6-
export function appendImport(moduleSpecifier: string, propertyName: string|undefined, name: string, sourceFile: ts.SourceFile): string
6+
export function appendImport(moduleSpecifier: string, propertyName: string|undefined, name: string, sourceFile: ts.SourceFile, noImportReuseOnDifferentNames: boolean = false): string
77
{
88
const imports = findNodesByType<ts.ImportDeclaration>(ts.SyntaxKind.ImportDeclaration, sourceFile);
99
const pos = imports.length ? (sourceFile.statements.indexOf(imports[imports.length - 1]) + 1 || 0) : 0;
@@ -22,7 +22,7 @@ export function appendImport(moduleSpecifier: string, propertyName: string|undef
2222
const namedBindings = <ts.NamedImports>existingImport.importClause.namedBindings;
2323
const elements = clone(namedBindings.elements);
2424

25-
const existingElement: ts.ImportSpecifier = find(elements, (existingElement: ts.ImportSpecifier) => {
25+
let existingElement: ts.ImportSpecifier = find(elements, (existingElement: ts.ImportSpecifier) => {
2626
return (
2727
(<ts.Identifier>existingElement.name).text === name ||
2828
(
@@ -42,6 +42,10 @@ export function appendImport(moduleSpecifier: string, propertyName: string|undef
4242
);
4343
});
4444

45+
if (noImportReuseOnDifferentNames && (<ts.Identifier>existingElement.name).text !== name) {
46+
existingElement = undefined;
47+
}
48+
4549
if (existingElement) {
4650
if (exists(propertyName) && exists(existingElement.propertyName) && propertyName !== (<ts.Identifier>existingElement.propertyName).text) {
4751
throw new Error(`appendImport: can not append new import {${propertyName} as ${name}}. File already contains import with the same name {${(<ts.Identifier>existingElement.propertyName).text} as ${(<ts.Identifier>existingElement.name).text}}.`);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import {forEach} from '@slicky/utils';
2+
import {appendImport} from './appendImport';
3+
import * as ts from 'typescript';
4+
5+
6+
export declare interface ImportItem
7+
{
8+
moduleSpecifier: string,
9+
propertyName: string|undefined,
10+
name: string,
11+
}
12+
13+
14+
export class AppendImportsBuffer
15+
{
16+
17+
18+
private imports: Array<ImportItem> = [];
19+
20+
21+
public add(moduleSpecifier: string, propertyName: string|undefined, name: string): void
22+
{
23+
this.imports.push({
24+
moduleSpecifier: moduleSpecifier,
25+
propertyName: propertyName,
26+
name: name,
27+
});
28+
}
29+
30+
31+
public applyImports(sourceFile: ts.SourceFile): void
32+
{
33+
forEach(this.imports, (newImport: ImportItem) => {
34+
appendImport(newImport.moduleSpecifier, newImport.propertyName, newImport.name, sourceFile, true);
35+
});
36+
}
37+
38+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './appendImport';
2+
export * from './appendImportsBuffer';
23
export * from './findNode';
34
export * from './findLastNodeOfType';
45
export * from './findNodesByType';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import {A as B} from '/import';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import { A as B, A } from "/import";

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@ describe('#sourceFiles/appendImport', () => {
114114
compareSourceFile('valid_9.updated', sourceFile);
115115
});
116116

117+
it('should not reuse import with different names', () => {
118+
const sourceFile = getFileSource('valid_10.original');
119+
const imported = appendImport('/import', undefined, 'A', sourceFile, true);
120+
121+
expect(imported).to.be.equal('A');
122+
compareSourceFile('valid_10.updated', sourceFile);
123+
});
124+
117125
});
118126

119127
});

0 commit comments

Comments
 (0)