Skip to content

Allow multiple ambient declarations for same module #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
/test/build
!/test/src/typings/*.d.ts
/test/src/**/*.d.ts
!/test/src/ambient/*.d.ts
/test/src/**/*.js
/test/src/**/*.js.map

Expand Down
5 changes: 5 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ module.exports = function (grunt) {
testConflicts: {
src: ['test/src/conflicts/dirname/index.ts'],
outDir: 'test/build/conflicts/dirname'
},
testAmbient: {
src: ['test/src/ambient/index.ts'],
outDir: 'test/build/ambient'
}
},
mochaTest: {
Expand All @@ -92,6 +96,7 @@ module.exports = function (grunt) {
'ts:testEs6',
'ts:testCommonJs',
'ts:testConflicts',
'ts:testAmbient',
'run'
]);

Expand Down
50 changes: 33 additions & 17 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export function bundle(options: Options): BundleResult {
mainFileContent += generatedLine + "\n";
});
mainFile = path.resolve(baseDir, "dts-bundle.tmp." + exportName + ".d.ts");
fs.writeFileSync(mainFile, mainFileContent, 'utf8');
fs.writeFileSync(mainFile, mainFileContent, { encoding: 'utf8' });
}

trace('\n### find typings ###');
Expand Down Expand Up @@ -231,12 +231,11 @@ export function bundle(options: Options): BundleResult {
// map all exports to their file
trace('\n### map exports ###');

let exportMap = Object.create(null);
let exportMap = Object.create(null); // values are arrays of parsed files
Object.keys(fileMap).forEach(file => {
let parse = fileMap[file];
parse.exports.forEach(name => {
assert(!(name in exportMap), 'already got export for: ' + name);
exportMap[name] = parse;
(exportMap[name] || (exportMap[name] = [])).push(parse);
trace('- %s -> %s', name, parse.file);
});
});
Expand Down Expand Up @@ -266,20 +265,32 @@ export function bundle(options: Options): BundleResult {
usedTypings.push(parse);

parse.externalImports.forEach(name => {
let p = exportMap[name];
let parses = exportMap[name];

if (!externals) {
trace(' - exclude external %s', name);
pushUnique(externalDependencies, !p ? name : p.file);
return;
}
if (isExclude(path.relative(baseDir, p.file), true)) {
trace(' - exclude external filter %s', name);
pushUnique(excludedTypings, p.file);
return;

if (parses) {
for (let p of parses) {
pushUnique(externalDependencies, p.file);
}
} else {
pushUnique(externalDependencies, name);
}
} else {
if (parses) {
for (let p of parses) {
if (isExclude(path.relative(baseDir, p.file), true)) {
trace(' - exclude external filter %s', name);
pushUnique(excludedTypings, p.file);
} else {
trace(' - include external %s', name);
assert(p, name);
queue.push(p);
}
}
}
}
trace(' - include external %s', name);
assert(p, name);
queue.push(p);
});
parse.relativeImports.forEach(file => {
let p = fileMap[file];
Expand Down Expand Up @@ -424,7 +435,7 @@ export function bundle(options: Options): BundleResult {
}
}

fs.writeFileSync(outFile, content, 'utf8');
fs.writeFileSync(outFile, content, { encoding: 'utf8' });
bundleResult.emitted = true;
} else {
warning(" XXX Not emit due to exist files not found.")
Expand Down Expand Up @@ -587,7 +598,7 @@ export function bundle(options: Options): BundleResult {
if (fs.lstatSync(file).isDirectory()) { // if file is a directory then lets assume commonjs convention of an index file in the given folder
file = path.join(file, 'index.d.ts');
}
const code = fs.readFileSync(file, 'utf8').replace(bomOptExp, '').replace(/\s*$/, '');
const code = fs.readFileSync(file, { encoding: 'utf8' }).replace(bomOptExp, '').replace(/\s*$/, '');
res.indent = detectIndent(code) || indent;

// buffer multi-line comments, handle JSDoc
Expand Down Expand Up @@ -749,6 +760,11 @@ export function bundle(options: Options): BundleResult {

trace(' - declare %s', moduleName);
pushUnique(res.exports, moduleName);

// for internal typings, can't have nested declares w/ modules
if (inSourceTypings(file)) {
line = line.replace('declare module', 'module');
}
let modLine: ModLine = {
original: line
};
Expand Down
31 changes: 31 additions & 0 deletions test/expected/ambient/foo-mx.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Dependencies for this module:
// ambient1.d.ts
// ambient2.d.ts
// ../../src/ambient/mymodule.d.ts

declare module 'foo-mx' {
export { MyInterface } from 'mymodule';
export { Sup1 } from 'foo-mx/ambient1';
export { Sup2 } from 'foo-mx/ambient2';
}

declare module 'foo-mx/ambient1' {
module 'mymodule' {
interface MyInterface {
prop1: string;
}
}
export interface Sup1 {
}
}

declare module 'foo-mx/ambient2' {
module 'mymodule' {
interface MyInterface {
prop2: string;
}
}
export interface Sup2 {
}
}

10 changes: 10 additions & 0 deletions test/src/ambient/ambient1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

/// <reference path="./mymodule.d.ts" />

declare module 'mymodule' {
interface MyInterface {
prop1: string
}
}

export interface Sup1 {}
10 changes: 10 additions & 0 deletions test/src/ambient/ambient2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

/// <reference path="./mymodule.d.ts" />

declare module 'mymodule' {
interface MyInterface {
prop2: string
}
}

export interface Sup2 {}
3 changes: 3 additions & 0 deletions test/src/ambient/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { MyInterface } from 'mymodule';
export { Sup1 } from './ambient1';
export { Sup2 } from './ambient2';
6 changes: 6 additions & 0 deletions test/src/ambient/mymodule.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

declare module 'mymodule' {
export interface MyInterface {
prop0: string
}
}
50 changes: 50 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,56 @@ describe('dts bundle', function () {
assert.strictEqual(getFile(actualFile), getFile(expectedFile));
});

(function testit(name, assertion, run) {
var buildDir = path.resolve(__dirname, 'build', 'ambient');
var call = function (done) {
var testDir = path.join(tmpDir, name);
var expDir = path.join(expectDir, name);

mkdirp.sync(testDir);

ncp.ncp(buildDir, testDir, function (err) {
if (err) {
done(err);
return;
}
assertion(testDir, expDir);
done();
});
};

var label = 'bundle ' + name;

if (run === 'skip') {
it.skip(label, call);
}
else if (run === 'only') {
it.only(label, call);
}
else {
it(label, call);
}
})('ambient', function (actDir, expDir) {
var result = dts.bundle({
name: 'foo-mx',
main: path.join(actDir, 'index.d.ts'),
newline: '\n',
verbose: true,
headerPath: "none"
});
var name = 'foo-mx.d.ts';
var actualFile = path.join(actDir, name);
assert.isTrue(result.emitted, "not emit " + actualFile);
var expectedFile = path.join(expDir, name);
assertFiles(actDir, [
name,
'index.d.ts',
'ambient1.d.ts',
'ambient2.d.ts',
]);
assert.strictEqual(getFile(actualFile), getFile(expectedFile));
});

(function testit(name, assertion, run) {
var buildDir = path.resolve(__dirname, 'build', 'es6');
var call = function (done) {
Expand Down