Skip to content

Commit

Permalink
fix(ts-transform-paths): interface import non-removal
Browse files Browse the repository at this point in the history
  • Loading branch information
zerkalica committed May 29, 2018
1 parent 19edfc8 commit 31d294e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
39 changes: 30 additions & 9 deletions packages/ts-transform-paths/__tests__/transforms.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('transforms', () => {
content: `import {some} from 'someRoot/lib'
export default some
`,
esnext: `import { some } from "./some/lib";
esnext: `import { some } from './some/lib';
export default some;
`,
commonjs: `"use strict";
Expand All @@ -19,6 +19,23 @@ exports.default = lib_1.some;
`,
declaration: `import { some } from './some/lib';
export default some;
`,
},

{
title: 'interface import',
path: 'index.ts',
content: `import {Some} from 'someRoot/lib'
export const some: Some = { self: 'test' }
`,
esnext: `export const some = { self: 'test' };
`,
commonjs: `"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.some = { self: 'test' };
`,
declaration: `import { Some } from './some/lib';
export declare const some: Some;
`,
},
]
Expand All @@ -28,13 +45,17 @@ export default some;
const data = transpile([item], { module: ts.ModuleKind.ESNext })
expect(data.outputFiles[0].text).toEqual(item.esnext)
})
it(`${item.title} transform to commonjs`, () => {
const data = transpile([item], { module: ts.ModuleKind.CommonJS })
expect(data.outputFiles[0].text).toEqual(item.commonjs)
})
it(`${item.title} declaration`, () => {
const data = transpile([item], { module: ts.ModuleKind.CommonJS })
expect(data.outputFiles[1].text).toEqual(item.declaration)
})

if (item.commonjs)
it(`${item.title} transform to commonjs`, () => {
const data = transpile([item], { module: ts.ModuleKind.CommonJS })
expect(data.outputFiles[0].text).toEqual(item.commonjs)
})

if (item.declaration)
it(`${item.title} declaration`, () => {
const data = transpile([item], { module: ts.ModuleKind.CommonJS })
expect(data.outputFiles[1].text).toEqual(item.declaration)
})
})
})
13 changes: 1 addition & 12 deletions packages/ts-transform-paths/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,7 @@ function importPathVisitor(
fixNode.end = cachedPos + newStr.length

const newSpec = ts.createLiteral(newImport)

if (ts.isImportDeclaration(node)) return ts.updateImportDeclaration(
node, node.decorators, node.modifiers, node.importClause, newSpec
)

if (ts.isExportDeclaration(node)) return ts.updateExportDeclaration(
node, node.decorators, node.modifiers, node.exportClause, newSpec
)

if (ts.isCallExpression(node)) return ts.updateCall(
node, node.expression, node.typeArguments, [newSpec]
)
;(fixNode as any).text = newImport
}

function patchEmitFiles(host: any): ts.TransformerFactory<ts.SourceFile>[] {
Expand Down

0 comments on commit 31d294e

Please sign in to comment.