Skip to content

Commit 0f67f4b

Browse files
committed
Merge pull request #7170 from masaeedu/addDeclarationDirOption
Add declarationDir option
2 parents 88830fe + f251768 commit 0f67f4b

File tree

46 files changed

+471
-10
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+471
-10
lines changed

src/compiler/commandLineParser.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ namespace ts {
1717
type: "boolean",
1818
description: Diagnostics.Generates_corresponding_d_ts_file,
1919
},
20+
{
21+
name: "declarationDir",
22+
type: "string",
23+
isFilePath: true,
24+
paramType: Diagnostics.DIRECTORY,
25+
},
2026
{
2127
name: "diagnostics",
2228
type: "boolean",

src/compiler/program.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1670,6 +1670,15 @@ namespace ts {
16701670
}
16711671
}
16721672

1673+
if (options.declarationDir) {
1674+
if (!options.declaration) {
1675+
programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Option_0_cannot_be_specified_without_specifying_option_1, "declarationDir", "declaration"));
1676+
}
1677+
if (options.out || options.outFile) {
1678+
programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Option_0_cannot_be_specified_with_option_1, "declarationDir", options.out ? "out" : "outFile"));
1679+
}
1680+
}
1681+
16731682
const languageVersion = options.target || ScriptTarget.ES3;
16741683
const outFile = options.outFile || options.out;
16751684

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2371,6 +2371,7 @@ namespace ts {
23712371
allowNonTsExtensions?: boolean;
23722372
charset?: string;
23732373
declaration?: boolean;
2374+
declarationDir?: string;
23742375
diagnostics?: boolean;
23752376
emitBOM?: boolean;
23762377
help?: boolean;

src/compiler/utilities.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2012,6 +2012,18 @@ namespace ts {
20122012
return emitOutputFilePathWithoutExtension + extension;
20132013
}
20142014

2015+
export function getDeclarationEmitOutputFilePath(sourceFile: SourceFile, host: EmitHost) {
2016+
const options = host.getCompilerOptions();
2017+
const outputDir = options.declarationDir || options.outDir; // Prefer declaration folder if specified
2018+
2019+
if (options.declaration) {
2020+
const path = outputDir
2021+
? getSourceFilePathInNewDir(sourceFile, host, outputDir)
2022+
: sourceFile.fileName;
2023+
return removeFileExtension(path) + ".d.ts";
2024+
}
2025+
}
2026+
20152027
export function getEmitScriptTarget(compilerOptions: CompilerOptions) {
20162028
return compilerOptions.target || ScriptTarget.ES3;
20172029
}
@@ -2065,23 +2077,23 @@ namespace ts {
20652077
const emitFileNames: EmitFileNames = {
20662078
jsFilePath,
20672079
sourceMapFilePath: getSourceMapFilePath(jsFilePath, options),
2068-
declarationFilePath: !isSourceFileJavaScript(sourceFile) ? getDeclarationEmitFilePath(jsFilePath, options) : undefined
2080+
declarationFilePath: !isSourceFileJavaScript(sourceFile) ? getDeclarationEmitOutputFilePath(sourceFile, host) : undefined
20692081
};
20702082
action(emitFileNames, [sourceFile], /*isBundledEmit*/false);
20712083
}
20722084

20732085
function onBundledEmit(host: EmitHost) {
20742086
// Can emit only sources that are not declaration file and are either non module code or module with --module or --target es6 specified
2075-
const bundledSources = filter(host.getSourceFiles(),
2076-
sourceFile => !isDeclarationFile(sourceFile) && // Not a declaration file
2077-
(!isExternalModule(sourceFile) || // non module file
2078-
(getEmitModuleKind(options) && isExternalModule(sourceFile)))); // module that can emit - note falsy value from getEmitModuleKind means the module kind that shouldn't be emitted
2087+
const bundledSources = filter(host.getSourceFiles(), sourceFile =>
2088+
!isDeclarationFile(sourceFile) // Not a declaration file
2089+
&& (!isExternalModule(sourceFile) || !!getEmitModuleKind(options))); // and not a module, unless module emit enabled
2090+
20792091
if (bundledSources.length) {
20802092
const jsFilePath = options.outFile || options.out;
20812093
const emitFileNames: EmitFileNames = {
20822094
jsFilePath,
20832095
sourceMapFilePath: getSourceMapFilePath(jsFilePath, options),
2084-
declarationFilePath: getDeclarationEmitFilePath(jsFilePath, options)
2096+
declarationFilePath: options.declaration ? removeFileExtension(jsFilePath) + ".d.ts" : undefined
20852097
};
20862098
action(emitFileNames, bundledSources, /*isBundledEmit*/true);
20872099
}
@@ -2090,10 +2102,6 @@ namespace ts {
20902102
function getSourceMapFilePath(jsFilePath: string, options: CompilerOptions) {
20912103
return options.sourceMap ? jsFilePath + ".map" : undefined;
20922104
}
2093-
2094-
function getDeclarationEmitFilePath(jsFilePath: string, options: CompilerOptions) {
2095-
return options.declaration ? removeFileExtension(jsFilePath) + ".d.ts" : undefined;
2096-
}
20972105
}
20982106

20992107
export function getSourceFilePathInNewDir(sourceFile: SourceFile, host: EmitHost, newDirPath: string) {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
define(["require", "exports"], function (require, exports) {
2+
"use strict";
3+
var A = (function () {
4+
function A() {
5+
}
6+
return A;
7+
}());
8+
exports.A = A;
9+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"scenario": "declarationDir: specify declarationDir only",
3+
"projectRoot": "tests/cases/projects/declarationDir",
4+
"inputFiles": [
5+
"a.ts",
6+
"subfolder/b.ts",
7+
"subfolder/c.ts"
8+
],
9+
"declaration": true,
10+
"declarationDir": "declarations",
11+
"baselineCheck": true,
12+
"resolvedInputFiles": [
13+
"lib.d.ts",
14+
"subfolder/b.ts",
15+
"a.ts",
16+
"subfolder/c.ts"
17+
],
18+
"emittedFiles": [
19+
"subfolder/b.js",
20+
"declarations/subfolder/b.d.ts",
21+
"a.js",
22+
"declarations/a.d.ts",
23+
"subfolder/c.js",
24+
"declarations/subfolder/c.d.ts"
25+
]
26+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { B } from './subfolder/b';
2+
export declare class A {
3+
b: B;
4+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export declare class B {
2+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { A } from '../a';
2+
export declare class C {
3+
a: A;
4+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
define(["require", "exports"], function (require, exports) {
2+
"use strict";
3+
var B = (function () {
4+
function B() {
5+
}
6+
return B;
7+
}());
8+
exports.B = B;
9+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
define(["require", "exports"], function (require, exports) {
2+
"use strict";
3+
var C = (function () {
4+
function C() {
5+
}
6+
return C;
7+
}());
8+
exports.C = C;
9+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict";
2+
var A = (function () {
3+
function A() {
4+
}
5+
return A;
6+
}());
7+
exports.A = A;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"scenario": "declarationDir: specify declarationDir only",
3+
"projectRoot": "tests/cases/projects/declarationDir",
4+
"inputFiles": [
5+
"a.ts",
6+
"subfolder/b.ts",
7+
"subfolder/c.ts"
8+
],
9+
"declaration": true,
10+
"declarationDir": "declarations",
11+
"baselineCheck": true,
12+
"resolvedInputFiles": [
13+
"lib.d.ts",
14+
"subfolder/b.ts",
15+
"a.ts",
16+
"subfolder/c.ts"
17+
],
18+
"emittedFiles": [
19+
"subfolder/b.js",
20+
"declarations/subfolder/b.d.ts",
21+
"a.js",
22+
"declarations/a.d.ts",
23+
"subfolder/c.js",
24+
"declarations/subfolder/c.d.ts"
25+
]
26+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { B } from './subfolder/b';
2+
export declare class A {
3+
b: B;
4+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export declare class B {
2+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { A } from '../a';
2+
export declare class C {
3+
a: A;
4+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict";
2+
var B = (function () {
3+
function B() {
4+
}
5+
return B;
6+
}());
7+
exports.B = B;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict";
2+
var C = (function () {
3+
function C() {
4+
}
5+
return C;
6+
}());
7+
exports.C = C;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"scenario": "declarationDir: specify declarationDir with outDir",
3+
"projectRoot": "tests/cases/projects/declarationDir",
4+
"inputFiles": [
5+
"a.ts",
6+
"subfolder/b.ts",
7+
"subfolder/c.ts"
8+
],
9+
"outDir": "out",
10+
"declaration": true,
11+
"declarationDir": "declarations",
12+
"baselineCheck": true,
13+
"resolvedInputFiles": [
14+
"lib.d.ts",
15+
"subfolder/b.ts",
16+
"a.ts",
17+
"subfolder/c.ts"
18+
],
19+
"emittedFiles": [
20+
"out/subfolder/b.js",
21+
"declarations/subfolder/b.d.ts",
22+
"out/a.js",
23+
"declarations/a.d.ts",
24+
"out/subfolder/c.js",
25+
"declarations/subfolder/c.d.ts"
26+
]
27+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { B } from './subfolder/b';
2+
export declare class A {
3+
b: B;
4+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export declare class B {
2+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { A } from '../a';
2+
export declare class C {
3+
a: A;
4+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
define(["require", "exports"], function (require, exports) {
2+
"use strict";
3+
var A = (function () {
4+
function A() {
5+
}
6+
return A;
7+
}());
8+
exports.A = A;
9+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
define(["require", "exports"], function (require, exports) {
2+
"use strict";
3+
var B = (function () {
4+
function B() {
5+
}
6+
return B;
7+
}());
8+
exports.B = B;
9+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
define(["require", "exports"], function (require, exports) {
2+
"use strict";
3+
var C = (function () {
4+
function C() {
5+
}
6+
return C;
7+
}());
8+
exports.C = C;
9+
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"scenario": "declarationDir: specify declarationDir with outDir",
3+
"projectRoot": "tests/cases/projects/declarationDir",
4+
"inputFiles": [
5+
"a.ts",
6+
"subfolder/b.ts",
7+
"subfolder/c.ts"
8+
],
9+
"outDir": "out",
10+
"declaration": true,
11+
"declarationDir": "declarations",
12+
"baselineCheck": true,
13+
"resolvedInputFiles": [
14+
"lib.d.ts",
15+
"subfolder/b.ts",
16+
"a.ts",
17+
"subfolder/c.ts"
18+
],
19+
"emittedFiles": [
20+
"out/subfolder/b.js",
21+
"declarations/subfolder/b.d.ts",
22+
"out/a.js",
23+
"declarations/a.d.ts",
24+
"out/subfolder/c.js",
25+
"declarations/subfolder/c.d.ts"
26+
]
27+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { B } from './subfolder/b';
2+
export declare class A {
3+
b: B;
4+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export declare class B {
2+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { A } from '../a';
2+
export declare class C {
3+
a: A;
4+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict";
2+
var A = (function () {
3+
function A() {
4+
}
5+
return A;
6+
}());
7+
exports.A = A;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict";
2+
var B = (function () {
3+
function B() {
4+
}
5+
return B;
6+
}());
7+
exports.B = B;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict";
2+
var C = (function () {
3+
function C() {
4+
}
5+
return C;
6+
}());
7+
exports.C = C;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error TS5053: Option 'declarationDir' cannot be specified with option 'out'.
2+
3+
4+
!!! error TS5053: Option 'declarationDir' cannot be specified with option 'out'.
5+
==== b.ts (0 errors) ====
6+
export class B {
7+
8+
}
9+
==== a.ts (0 errors) ====
10+
import {B} from './subfolder/b';
11+
export class A {
12+
b: B;
13+
}
14+
==== subfolder/c.ts (0 errors) ====
15+
import {A} from '../a';
16+
17+
export class C {
18+
a: A;
19+
}

0 commit comments

Comments
 (0)