Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/generators/extend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import { declMapping } from '../config';
import { GeneratorResult, TsGenConfig, TsHelperConfig } from '..';

const debug = debuglog('egg-ts-helper#generators_extend');
const hasExportWithoutDefault = (filePath: string) => {
const result = utils.findExportNode(fs.readFileSync(filePath, 'utf-8'));
return !result.exportDefaultNode;
};

export default function ExtendGenerator(config: TsGenConfig, baseConfig: TsHelperConfig) {
const fileList = config.file ? [ config.file ] : config.fileList;
Expand Down Expand Up @@ -41,7 +45,9 @@ export default function ExtendGenerator(config: TsGenConfig, baseConfig: TsHelpe

// get import info
const moduleName = `Extend${interfaceEnvironment}${interfaceName}`;
const importContext = utils.getImportStr(config.dtsDir, f, moduleName);

const useImportStar = hasExportWithoutDefault(f);
const importContext = utils.getImportStr(config.dtsDir, f, moduleName, useImportStar);
tsList.push({
dist,
content:
Expand Down
18 changes: 6 additions & 12 deletions test/fixtures/app/app/extend/helper.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
let helper;
export function isCool() {
console.info('is Cool');
}

helper = {
isCool() {
console.info('is Cool');
},

isNotCool() {
console.info('is not Cool');
},
};

export default helper;
export function isNotCool() {
console.info('is not Cool');
}
4 changes: 2 additions & 2 deletions test/generators/extend.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('generators/extend.test.ts', () => {
it('should works without error with helper', () => {
const result = triggerGenerator('extend', appDir, 'helper.ts');
const item = result[0];
assert(item.content!.includes('../../../app/extend/helper'));
assert(item.content!.includes('import * as ExtendIHelper from \'../../../app/extend/helper\''));
assert(item.content!.includes('type ExtendIHelperType = typeof ExtendIHelper'));
assert(item.content!.includes('interface IHelper extends ExtendIHelperType { }'));
});
Expand All @@ -58,7 +58,7 @@ describe('generators/extend.test.ts', () => {
const result = triggerGenerator('extend', appDir, 'application.unittest.ts');
const item = result[0];
assert(item.dist === path.resolve(appDir, './typings/app/extend/application.unittest.d.ts'));
assert(item.content!.includes('../../../app/extend/application.unittest'));
assert(item.content!.includes('import ExtendUnittestApplication from \'../../../app/extend/application.unittest\''));
assert(item.content!.includes('type ExtendUnittestApplicationType = typeof ExtendUnittestApplication;'));
assert(item.content!.includes('interface Application extends ExtendUnittestApplicationType { }'));
});
Expand Down
Loading