From 50f320f4ec287700da033f28021c876665dfcf84 Mon Sep 17 00:00:00 2001 From: Borewit Date: Mon, 3 Sep 2018 20:37:28 +0200 Subject: [PATCH 1/7] #65: wrap encoding string parameter in option object --- lib/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/index.ts b/lib/index.ts index 6e744c4..8446f34 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -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 ###'); @@ -424,7 +424,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.") From 49218c68e0d74e38ccd4341c93c732a8ed23ab6d Mon Sep 17 00:00:00 2001 From: Borewit Date: Tue, 4 Sep 2018 19:53:40 +0200 Subject: [PATCH 2/7] #60: avoid duplicate files are included. Normalize directory to actual declaration path. --- lib/index.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/index.ts b/lib/index.ts index 6e744c4..9d957b0 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -717,6 +717,8 @@ export function bundle(options: Options): BundleResult { if(!fs.existsSync(full) || fs.existsSync(full + '.d.ts')) { full += '.d.ts'; } + let stat = fs.statSync(full); + full = stat.isDirectory() ? path.join(full, 'index.d.ts') : full; trace(' - import relative %s (%s)', moduleName, full); pushUnique(res.relativeImports, full); From e0c50b2e1a34575a5b7f8ad3bc7c7445303c2ab7 Mon Sep 17 00:00:00 2001 From: Borewit Date: Tue, 4 Sep 2018 21:11:51 +0200 Subject: [PATCH 3/7] #60: Fix unit tests --- lib/index.ts | 10 ++++------ test/expected/externals/foo-mx.d.ts | 8 ++++---- test/expected/includeExclude/foo-mx.d.ts | 6 +++--- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/lib/index.ts b/lib/index.ts index 0ac722c..01076e9 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -517,7 +517,7 @@ export function bundle(options: Options): BundleResult { } function getModName(file: string) { - return path.relative(baseDir, path.dirname(file) + path.sep + path.basename(file).replace(/\.d\.ts$/, '')); + return path.relative(baseDir, path.dirname(file) + path.sep + path.basename(file).replace(/index\.d\.ts$|\.d\.ts$/, '')); } function getExpName(file: string) { @@ -584,9 +584,6 @@ export function bundle(options: Options): BundleResult { res.fileExists = false; return res; } - 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*$/, ''); res.indent = detectIndent(code) || indent; @@ -717,8 +714,9 @@ export function bundle(options: Options): BundleResult { if(!fs.existsSync(full) || fs.existsSync(full + '.d.ts')) { full += '.d.ts'; } - let stat = fs.statSync(full); - full = stat.isDirectory() ? path.join(full, 'index.d.ts') : full; + if (fs.lstatSync(full).isDirectory()) { + full = path.join(full, 'index.d.ts'); // normalize to actual file path + } trace(' - import relative %s (%s)', moduleName, full); pushUnique(res.relativeImports, full); diff --git a/test/expected/externals/foo-mx.d.ts b/test/expected/externals/foo-mx.d.ts index d92b6b8..a80823d 100644 --- a/test/expected/externals/foo-mx.d.ts +++ b/test/expected/externals/foo-mx.d.ts @@ -1,20 +1,20 @@ declare module 'foo-mx' { import exp = require('foo-mx/lib/exported-sub'); - import mod1 = require('foo-mx/index//external1'); + import mod1 = require('foo-mx///external1'); export import Foo = require('foo-mx/Foo'); export function run(foo?: Foo): Foo; export function flep(): exp.ExternalContainer; export function bar(): mod1.SomeType; } -declare module 'foo-mx/index//external1' { +declare module 'foo-mx///external1' { export class SomeType { foo(): void; } } -declare module 'foo-mx/index//external2' { +declare module 'foo-mx///external2' { export class AnotherType { foo(): void; } @@ -22,7 +22,7 @@ declare module 'foo-mx/index//external2' { declare module 'foo-mx/lib/exported-sub' { import Foo = require('foo-mx/Foo'); - import mod2 = require('foo-mx/index//external2'); + import mod2 = require('foo-mx///external2'); export class ExternalContainer { something: mod2.AnotherType; } diff --git a/test/expected/includeExclude/foo-mx.d.ts b/test/expected/includeExclude/foo-mx.d.ts index ba3848c..162c609 100644 --- a/test/expected/includeExclude/foo-mx.d.ts +++ b/test/expected/includeExclude/foo-mx.d.ts @@ -1,20 +1,20 @@ declare module 'foo-mx' { import exp = require('foo-mx/lib/exported-sub'); - import mod1 = require('foo-mx/index//external1'); + import mod1 = require('foo-mx///external1'); export import Foo = require('foo-mx/Foo'); export function run(foo?: Foo): Foo; export function flep(): exp.ExternalContainer; export function bar(): mod1.SomeType; } -declare module 'foo-mx/index//external1' { +declare module 'foo-mx///external1' { export class SomeType { foo(): void; } } -declare module 'foo-mx/index//external2' { +declare module 'foo-mx///external2' { export class AnotherType { foo(): void; } From 97719ef5f3b3b7b0728f56ab59f24bf1a5344281 Mon Sep 17 00:00:00 2001 From: Borewit Date: Wed, 5 Sep 2018 08:52:17 +0200 Subject: [PATCH 4/7] #60: add unit test which caused the duplication of declaration --- test/expected/dot-slash/bundle.d.ts | 19 +++++++++++ test/src/dot-slash/SomeOtherClass.ts | 11 ++++++ test/src/dot-slash/index.ts | 13 ++++++++ test/test.js | 50 ++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+) create mode 100644 test/expected/dot-slash/bundle.d.ts create mode 100644 test/src/dot-slash/SomeOtherClass.ts create mode 100644 test/src/dot-slash/index.ts diff --git a/test/expected/dot-slash/bundle.d.ts b/test/expected/dot-slash/bundle.d.ts new file mode 100644 index 0000000..7d271eb --- /dev/null +++ b/test/expected/dot-slash/bundle.d.ts @@ -0,0 +1,19 @@ + +declare module 'bundle' { + import { SomeOtherClass } from "bundle/SomeOtherClass"; + export interface IShouldBeThereOnlyOnce { + name: string; + } + export function getOther(): SomeOtherClass; +} + +declare module 'bundle/SomeOtherClass' { + import { IShouldBeThereOnlyOnce } from "bundle/"; + export class SomeOtherClass { + /** + * Extract metadata from the given audio file + */ + static saveTheWorld(once: IShouldBeThereOnlyOnce): Promise; + } +} + diff --git a/test/src/dot-slash/SomeOtherClass.ts b/test/src/dot-slash/SomeOtherClass.ts new file mode 100644 index 0000000..ca2f4b5 --- /dev/null +++ b/test/src/dot-slash/SomeOtherClass.ts @@ -0,0 +1,11 @@ +import {IShouldBeThereOnlyOnce} from "./"; + +export class SomeOtherClass { + + /** + * Extract metadata from the given audio file + */ + public static saveTheWorld(once: IShouldBeThereOnlyOnce): Promise { + return null; + } +} diff --git a/test/src/dot-slash/index.ts b/test/src/dot-slash/index.ts new file mode 100644 index 0000000..65fcf51 --- /dev/null +++ b/test/src/dot-slash/index.ts @@ -0,0 +1,13 @@ +'use strict'; + +import {SomeOtherClass} from "./SomeOtherClass"; + +const other: SomeOtherClass = new SomeOtherClass(); + +export interface IShouldBeThereOnlyOnce { + name: string, +} + +export function getOther(): SomeOtherClass { + return null; +} diff --git a/test/test.js b/test/test.js index 25f95dc..b4081c8 100644 --- a/test/test.js +++ b/test/test.js @@ -532,4 +532,54 @@ describe('dts bundle', function () { ]); assert.strictEqual(getFile(actualFile), getFile(expectedFile)); }); + + (function testit(name, assertion, run) { + var buildDir = path.resolve(__dirname, 'build', 'dot-slash'); + 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); + } + })('dot-slash', function (actDir, expDir) { + var result = dts.bundle({ + name: 'bundle', + main: path.join(actDir, '../dot-slash', 'index.d.ts'), + newline: '\n', + verbose: true, + headerPath: "none" + }); + var name = 'bundle.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', + 'SomeOtherClass.d.ts' + ]); + assert.strictEqual(getFile(actualFile), getFile(expectedFile)); + }) + }); From 345a8c93ab6c3411a7ff78c80f37b27bc3fbe022 Mon Sep 17 00:00:00 2001 From: Borewit Date: Wed, 5 Sep 2018 19:41:03 +0200 Subject: [PATCH 5/7] Add build directory in grunt. --- Gruntfile.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Gruntfile.js b/Gruntfile.js index 5212b2e..60b8b79 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -67,7 +67,11 @@ module.exports = function (grunt) { testConflicts: { src: ['test/src/conflicts/dirname/index.ts'], outDir: 'test/build/conflicts/dirname' - } + }, + testDotSlash: { + src: ['test/src/dot-slash/index.ts'], + outDir: 'test/build/dot-slash' + } }, mochaTest: { options: { From 42f6be6dc7cbae97d8010a04f622431e0fe137c3 Mon Sep 17 00:00:00 2001 From: Borewit Date: Wed, 5 Sep 2018 20:14:37 +0200 Subject: [PATCH 6/7] Fix creation of unit test build directory in grunt. --- Gruntfile.js | 7 ++++--- test/test.js | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 60b8b79..73e3cc0 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -68,9 +68,9 @@ module.exports = function (grunt) { src: ['test/src/conflicts/dirname/index.ts'], outDir: 'test/build/conflicts/dirname' }, - testDotSlash: { - src: ['test/src/dot-slash/index.ts'], - outDir: 'test/build/dot-slash' + testDotSlash: { + src: ['test/src/dot-slash/index.ts'], + outDir: 'test/build/dot-slash' } }, mochaTest: { @@ -96,6 +96,7 @@ module.exports = function (grunt) { 'ts:testEs6', 'ts:testCommonJs', 'ts:testConflicts', + 'ts:testDotSlash', 'run' ]); diff --git a/test/test.js b/test/test.js index b4081c8..504dac0 100644 --- a/test/test.js +++ b/test/test.js @@ -534,7 +534,7 @@ describe('dts bundle', function () { }); (function testit(name, assertion, run) { - var buildDir = path.resolve(__dirname, 'build', 'dot-slash'); + var buildDir = path.resolve(__dirname, 'build', name); var call = function (done) { var testDir = path.join(tmpDir, name); var expDir = path.join(expectDir, name); From e064ad71b1651ffae6620684be43b55020f924f8 Mon Sep 17 00:00:00 2001 From: Borewit Date: Wed, 5 Sep 2018 20:24:15 +0200 Subject: [PATCH 7/7] Update comment of test sample --- test/expected/dot-slash/bundle.d.ts | 3 --- test/src/dot-slash/SomeOtherClass.ts | 5 +---- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/test/expected/dot-slash/bundle.d.ts b/test/expected/dot-slash/bundle.d.ts index 7d271eb..2ce2e6f 100644 --- a/test/expected/dot-slash/bundle.d.ts +++ b/test/expected/dot-slash/bundle.d.ts @@ -10,9 +10,6 @@ declare module 'bundle' { declare module 'bundle/SomeOtherClass' { import { IShouldBeThereOnlyOnce } from "bundle/"; export class SomeOtherClass { - /** - * Extract metadata from the given audio file - */ static saveTheWorld(once: IShouldBeThereOnlyOnce): Promise; } } diff --git a/test/src/dot-slash/SomeOtherClass.ts b/test/src/dot-slash/SomeOtherClass.ts index ca2f4b5..18369c0 100644 --- a/test/src/dot-slash/SomeOtherClass.ts +++ b/test/src/dot-slash/SomeOtherClass.ts @@ -1,10 +1,7 @@ -import {IShouldBeThereOnlyOnce} from "./"; +import {IShouldBeThereOnlyOnce} from "./"; // The tricky part export class SomeOtherClass { - /** - * Extract metadata from the given audio file - */ public static saveTheWorld(once: IShouldBeThereOnlyOnce): Promise { return null; }