Skip to content

Commit 9889c74

Browse files
authored
Disable declaration emit for json files (#36066)
1 parent 78748c0 commit 9889c74

17 files changed

+103
-155
lines changed

src/compiler/emitter.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,13 @@ namespace ts {
9191
}
9292
else {
9393
const ownOutputFilePath = getOwnEmitOutputFilePath(sourceFile.fileName, host, getOutputExtension(sourceFile, options));
94+
const isJsonFile = isJsonSourceFile(sourceFile);
9495
// If json file emits to the same location skip writing it, if emitDeclarationOnly skip writing it
95-
const isJsonEmittedToSameLocation = isJsonSourceFile(sourceFile) &&
96+
const isJsonEmittedToSameLocation = isJsonFile &&
9697
comparePaths(sourceFile.fileName, ownOutputFilePath, host.getCurrentDirectory(), !host.useCaseSensitiveFileNames()) === Comparison.EqualTo;
9798
const jsFilePath = options.emitDeclarationOnly || isJsonEmittedToSameLocation ? undefined : ownOutputFilePath;
9899
const sourceMapFilePath = !jsFilePath || isJsonSourceFile(sourceFile) ? undefined : getSourceMapFilePath(jsFilePath, options);
99-
const declarationFilePath = (forceDtsPaths || getEmitDeclarations(options)) ? getDeclarationEmitOutputFilePath(sourceFile.fileName, host) : undefined;
100+
const declarationFilePath = (forceDtsPaths || (getEmitDeclarations(options) && !isJsonFile)) ? getDeclarationEmitOutputFilePath(sourceFile.fileName, host) : undefined;
100101
const declarationMapPath = declarationFilePath && getAreDeclarationMapsEnabled(options) ? declarationFilePath + ".map" : undefined;
101102
return { jsFilePath, sourceMapFilePath, declarationFilePath, declarationMapPath, buildInfoPath: undefined };
102103
}
@@ -144,7 +145,7 @@ namespace ts {
144145

145146
/* @internal */
146147
export function getOutputDeclarationFileName(inputFileName: string, configFile: ParsedCommandLine, ignoreCase: boolean) {
147-
Debug.assert(!fileExtensionIs(inputFileName, Extension.Dts));
148+
Debug.assert(!fileExtensionIs(inputFileName, Extension.Dts) && !fileExtensionIs(inputFileName, Extension.Json));
148149
return changeExtension(
149150
getOutputPathWithoutChangingExt(inputFileName, configFile, ignoreCase, configFile.options.declarationDir || configFile.options.outDir),
150151
Extension.Dts
@@ -400,12 +401,13 @@ namespace ts {
400401
return;
401402
}
402403
const sourceFiles = isSourceFile(sourceFileOrBundle) ? [sourceFileOrBundle] : sourceFileOrBundle.sourceFiles;
404+
const filesForEmit = forceDtsEmit ? sourceFiles : filter(sourceFiles, isSourceFileNotJson);
403405
// Setup and perform the transformation to retrieve declarations from the input files
404-
const inputListOrBundle = (compilerOptions.outFile || compilerOptions.out) ? [createBundle(sourceFiles, !isSourceFile(sourceFileOrBundle) ? sourceFileOrBundle.prepends : undefined)] : sourceFiles;
406+
const inputListOrBundle = (compilerOptions.outFile || compilerOptions.out) ? [createBundle(filesForEmit, !isSourceFile(sourceFileOrBundle) ? sourceFileOrBundle.prepends : undefined)] : filesForEmit;
405407
if (emitOnlyDtsFiles && !getEmitDeclarations(compilerOptions)) {
406408
// Checker wont collect the linked aliases since thats only done when declaration is enabled.
407409
// Do that here when emitting only dts files
408-
sourceFiles.forEach(collectLinkedAliases);
410+
filesForEmit.forEach(collectLinkedAliases);
409411
}
410412
const declarationTransform = transformNodes(resolver, host, compilerOptions, inputListOrBundle, declarationTransformers, /*allowDtsFiles*/ false);
411413
if (length(declarationTransform.diagnostics)) {

src/compiler/program.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ namespace ts {
853853
}
854854
else if (getEmitModuleKind(parsedRef.commandLine.options) === ModuleKind.None) {
855855
for (const fileName of parsedRef.commandLine.fileNames) {
856-
if (!fileExtensionIs(fileName, Extension.Dts)) {
856+
if (!fileExtensionIs(fileName, Extension.Dts) && !fileExtensionIs(fileName, Extension.Json)) {
857857
processSourceFile(getOutputDeclarationFileName(fileName, parsedRef.commandLine, !host.useCaseSensitiveFileNames()), /*isDefaultLib*/ false, /*ignoreNoDefaultLib*/ false, /*packageId*/ undefined);
858858
}
859859
}
@@ -2500,8 +2500,8 @@ namespace ts {
25002500
}
25012501

25022502
function getProjectReferenceRedirectProject(fileName: string) {
2503-
// Ignore dts
2504-
if (!resolvedProjectReferences || !resolvedProjectReferences.length || fileExtensionIs(fileName, Extension.Dts)) {
2503+
// Ignore dts or any json files
2504+
if (!resolvedProjectReferences || !resolvedProjectReferences.length || fileExtensionIs(fileName, Extension.Dts) || fileExtensionIs(fileName, Extension.Json)) {
25052505
return undefined;
25062506
}
25072507

@@ -2562,7 +2562,7 @@ namespace ts {
25622562
}
25632563
else {
25642564
forEach(resolvedRef.commandLine.fileNames, fileName => {
2565-
if (!fileExtensionIs(fileName, Extension.Dts)) {
2565+
if (!fileExtensionIs(fileName, Extension.Dts) && !fileExtensionIs(fileName, Extension.Json)) {
25662566
const outputDts = getOutputDeclarationFileName(fileName, resolvedRef.commandLine, host.useCaseSensitiveFileNames());
25672567
mapFromToProjectReferenceRedirectSource!.set(toPath(outputDts), fileName);
25682568
}

src/compiler/transformers/declarations.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
/*@internal*/
22
namespace ts {
33
export function getDeclarationDiagnostics(host: EmitHost, resolver: EmitResolver, file: SourceFile | undefined): DiagnosticWithLocation[] | undefined {
4+
if (file && isJsonSourceFile(file)) {
5+
return []; // No declaration diagnostics for json for now
6+
}
47
const compilerOptions = host.getCompilerOptions();
5-
const result = transformNodes(resolver, host, compilerOptions, file ? [file] : host.getSourceFiles(), [transformDeclarations], /*allowDtsFiles*/ false);
8+
const result = transformNodes(resolver, host, compilerOptions, file ? [file] : filter(host.getSourceFiles(), isSourceFileNotJson), [transformDeclarations], /*allowDtsFiles*/ false);
69
return result.diagnostics;
710
}
811

src/compiler/utilities.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1808,6 +1808,10 @@ namespace ts {
18081808
return !!node && !!(node.flags & NodeFlags.JsonFile);
18091809
}
18101810

1811+
export function isSourceFileNotJson(file: SourceFile) {
1812+
return !isJsonSourceFile(file);
1813+
}
1814+
18111815
export function isInJSDoc(node: Node | undefined): boolean {
18121816
return !!node && !!(node.flags & NodeFlags.JSDoc);
18131817
}

src/harness/harnessIO.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ namespace Harness {
451451
throw new Error("Only declaration files should be generated when emitDeclarationOnly:true");
452452
}
453453
}
454-
else if (result.dts.size !== result.getNumberOfJsFiles(/*includeJson*/ true)) {
454+
else if (result.dts.size !== result.getNumberOfJsFiles(/*includeJson*/ false)) {
455455
throw new Error("There were no errors and declFiles generated did not match number of js files generated");
456456
}
457457
}

tests/baselines/reference/jsDeclarationsJson.js

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,6 @@ var j = require("./obj.json");
2525
module.exports = j;
2626

2727

28-
//// [obj.d.ts]
29-
export declare const x: number;
30-
export declare const y: number;
31-
export declare namespace obj {
32-
export const items: ({
33-
x: number;
34-
y?: undefined;
35-
err?: undefined;
36-
} | {
37-
x: number;
38-
y: number;
39-
err?: undefined;
40-
} | {
41-
x: number;
42-
err: boolean;
43-
y?: undefined;
44-
})[];
45-
}
4628
//// [index.d.ts]
4729
export = j;
4830
declare const j: {

tests/baselines/reference/jsDeclarationsPackageJson.js

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -74,34 +74,6 @@ var j = require("./package.json");
7474
module.exports = j;
7575

7676

77-
//// [package.d.ts]
78-
export declare const name: string;
79-
export declare const version: string;
80-
export declare const description: string;
81-
export declare const main: string;
82-
export declare namespace bin {
83-
export const cli: string;
84-
}
85-
export declare namespace engines {
86-
export const node: string;
87-
}
88-
export declare namespace scripts {
89-
export const scriptname: string;
90-
}
91-
export declare const devDependencies: {
92-
"@ns/dep": string;
93-
};
94-
export declare namespace dependencies {
95-
export const dep: string;
96-
}
97-
export declare const repository: string;
98-
export declare const keywords: string[];
99-
export declare const author: string;
100-
export declare const license: string;
101-
export declare const homepage: string;
102-
export declare namespace config {
103-
export const o: string[];
104-
}
10577
//// [index.d.ts]
10678
export = j;
10779
declare const j: {

tests/baselines/reference/requireOfJsonFileTypes.js

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -85,25 +85,5 @@ numberLiteral = f[0];
8585
booleanLiteral = g[0];
8686

8787

88-
//// [out/b.d.ts]
89-
export declare const a: boolean;
90-
export declare const b: string;
91-
export declare const c: null;
92-
export declare const d: boolean;
93-
//// [out/c.d.ts]
94-
declare const _exports: (string | null)[];
95-
export = _exports;
96-
//// [out/d.d.ts]
97-
declare const _exports: string;
98-
export = _exports;
99-
//// [out/e.d.ts]
100-
declare const _exports: number;
101-
export = _exports;
102-
//// [out/f.d.ts]
103-
declare const _exports: number[];
104-
export = _exports;
105-
//// [out/g.d.ts]
106-
declare const _exports: boolean[];
107-
export = _exports;
10888
//// [out/file1.d.ts]
10989
export {};

tests/baselines/reference/requireOfJsonFileWithDeclaration.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,5 @@ if (x) {
3232
}
3333

3434

35-
//// [out/b.d.ts]
36-
export declare const a: boolean;
37-
export declare const b: string;
3835
//// [out/file1.d.ts]
3936
export {};

tests/baselines/reference/tsbuild/javascriptProjectEmit/initial-build/loads-js-based-projects-with-non-moved-json-files-and-emits-them-correctly.js

Lines changed: 16 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ exitCode:: ExitStatus.Success
44

55

66
//// [/out/sub-project/index.d.ts]
7-
export const m: typeof mod;
8-
import mod from "../common";
7+
export const m: {
8+
val: number;
9+
};
910

1011

1112
//// [/out/sub-project/index.js]
@@ -26,17 +27,16 @@ exports.m = common_1["default"];
2627
"version": "-32082413277-/// <reference no-default-lib=\"true\"/>\ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array<T> { length: number; [n: number]: T; }\ninterface ReadonlyArray<T> {}\ndeclare const console: { log(msg: any): void; };\ninterface SymbolConstructor {\n readonly species: symbol;\n readonly toStringTag: symbol;\n}\ndeclare var Symbol: SymbolConstructor;\ninterface Symbol {\n readonly [Symbol.toStringTag]: string;\n}\n",
2728
"signature": "-32082413277-/// <reference no-default-lib=\"true\"/>\ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array<T> { length: number; [n: number]: T; }\ninterface ReadonlyArray<T> {}\ndeclare const console: { log(msg: any): void; };\ninterface SymbolConstructor {\n readonly species: symbol;\n readonly toStringTag: symbol;\n}\ndeclare var Symbol: SymbolConstructor;\ninterface Symbol {\n readonly [Symbol.toStringTag]: string;\n}\n"
2829
},
29-
"../../src/common/obj.d.ts": {
30-
"version": "-6323167306-export declare const val: number;\r\n",
31-
"signature": "-6323167306-export declare const val: number;\r\n"
30+
"../../src/common/obj.json": {
31+
"version": "2151907832-{\n \"val\": 42\n}"
3232
},
3333
"../../src/common/index.d.ts": {
3434
"version": "-4085459678-import x = require(\"./obj.json\");\r\nexport = x;\r\n",
3535
"signature": "-4085459678-import x = require(\"./obj.json\");\r\nexport = x;\r\n"
3636
},
3737
"../../src/sub-project/index.js": {
3838
"version": "-14684157955-import mod from '../common';\n\nexport const m = mod;\n",
39-
"signature": "-229957289-export const m: typeof mod;\r\nimport mod from \"../common\";\r\n"
39+
"signature": "-15768184370-export const m: {\r\n val: number;\r\n};\r\n"
4040
}
4141
},
4242
"options": {
@@ -53,21 +53,21 @@ exports.m = common_1["default"];
5353
},
5454
"referencedMap": {
5555
"../../src/common/index.d.ts": [
56-
"../../src/common/obj.d.ts"
56+
"../../src/common/obj.json"
5757
],
5858
"../../src/sub-project/index.js": [
5959
"../../src/common/index.d.ts"
6060
]
6161
},
6262
"exportedModulesMap": {
6363
"../../src/common/index.d.ts": [
64-
"../../src/common/obj.d.ts"
64+
"../../src/common/obj.json"
6565
]
6666
},
6767
"semanticDiagnosticsPerFile": [
6868
"../../lib/lib.d.ts",
6969
"../../src/common/index.d.ts",
70-
"../../src/common/obj.d.ts",
70+
"../../src/common/obj.json",
7171
"../../src/sub-project/index.js"
7272
]
7373
},
@@ -76,7 +76,9 @@ exports.m = common_1["default"];
7676

7777
//// [/out/sub-project-2/index.d.ts]
7878
export function getVar(): {
79-
key: typeof import("../common/obj.json");
79+
key: {
80+
val: number;
81+
};
8082
};
8183

8284

@@ -101,21 +103,13 @@ exports.getVar = getVar;
101103
"version": "-32082413277-/// <reference no-default-lib=\"true\"/>\ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array<T> { length: number; [n: number]: T; }\ninterface ReadonlyArray<T> {}\ndeclare const console: { log(msg: any): void; };\ninterface SymbolConstructor {\n readonly species: symbol;\n readonly toStringTag: symbol;\n}\ndeclare var Symbol: SymbolConstructor;\ninterface Symbol {\n readonly [Symbol.toStringTag]: string;\n}\n",
102104
"signature": "-32082413277-/// <reference no-default-lib=\"true\"/>\ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array<T> { length: number; [n: number]: T; }\ninterface ReadonlyArray<T> {}\ndeclare const console: { log(msg: any): void; };\ninterface SymbolConstructor {\n readonly species: symbol;\n readonly toStringTag: symbol;\n}\ndeclare var Symbol: SymbolConstructor;\ninterface Symbol {\n readonly [Symbol.toStringTag]: string;\n}\n"
103105
},
104-
"../../src/common/obj.d.ts": {
105-
"version": "-6323167306-export declare const val: number;\r\n",
106-
"signature": "-6323167306-export declare const val: number;\r\n"
107-
},
108-
"../../src/common/index.d.ts": {
109-
"version": "-4085459678-import x = require(\"./obj.json\");\r\nexport = x;\r\n",
110-
"signature": "-4085459678-import x = require(\"./obj.json\");\r\nexport = x;\r\n"
111-
},
112106
"../sub-project/index.d.ts": {
113-
"version": "-229957289-export const m: typeof mod;\r\nimport mod from \"../common\";\r\n",
114-
"signature": "-229957289-export const m: typeof mod;\r\nimport mod from \"../common\";\r\n"
107+
"version": "-15768184370-export const m: {\r\n val: number;\r\n};\r\n",
108+
"signature": "-15768184370-export const m: {\r\n val: number;\r\n};\r\n"
115109
},
116110
"../../src/sub-project-2/index.js": {
117111
"version": "13545386800-import { m } from '../sub-project/index';\n\nconst variable = {\n key: m,\n};\n\nexport function getVar() {\n return variable;\n}\n",
118-
"signature": "-9206156860-export function getVar(): {\r\n key: typeof import(\"../common/obj.json\");\r\n};\r\n"
112+
"signature": "-2686589794-export function getVar(): {\r\n key: {\r\n val: number;\r\n };\r\n};\r\n"
119113
}
120114
},
121115
"options": {
@@ -131,31 +125,13 @@ exports.getVar = getVar;
131125
"configFilePath": "../../src/sub-project-2/tsconfig.json"
132126
},
133127
"referencedMap": {
134-
"../../src/common/index.d.ts": [
135-
"../../src/common/obj.d.ts"
136-
],
137128
"../../src/sub-project-2/index.js": [
138129
"../sub-project/index.d.ts"
139-
],
140-
"../sub-project/index.d.ts": [
141-
"../../src/common/index.d.ts"
142-
]
143-
},
144-
"exportedModulesMap": {
145-
"../../src/common/index.d.ts": [
146-
"../../src/common/obj.d.ts"
147-
],
148-
"../../src/sub-project-2/index.js": [
149-
"../../src/common/obj.d.ts"
150-
],
151-
"../sub-project/index.d.ts": [
152-
"../../src/common/index.d.ts"
153130
]
154131
},
132+
"exportedModulesMap": {},
155133
"semanticDiagnosticsPerFile": [
156134
"../../lib/lib.d.ts",
157-
"../../src/common/index.d.ts",
158-
"../../src/common/obj.d.ts",
159135
"../../src/sub-project-2/index.js",
160136
"../sub-project/index.d.ts"
161137
]
@@ -174,10 +150,6 @@ var x = require("./obj.json");
174150
module.exports = x;
175151

176152

177-
//// [/src/common/obj.d.ts]
178-
export declare const val: number;
179-
180-
181153
//// [/src/common/tsconfig.tsbuildinfo]
182154
{
183155
"program": {

tests/baselines/reference/tsbuild/resolveJsonModule/initial-build/files-containing-json-file.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
exitCode:: ExitStatus.Success
44

55

6-
//// [/src/dist/src/hello.d.ts]
7-
export declare const hello: string;
8-
9-
106
//// [/src/dist/src/hello.json]
117
{
128
"hello": "world"

tests/baselines/reference/tsbuild/resolveJsonModule/initial-build/importing-json-module-from-project-reference.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ console.log(foo_json_1.foo);
3535
"version": "3858781397-/// <reference no-default-lib=\"true\"/>\ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array<T> { length: number; [n: number]: T; }\ninterface ReadonlyArray<T> {}\ndeclare const console: { log(msg: any): void; };",
3636
"signature": "3858781397-/// <reference no-default-lib=\"true\"/>\ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array<T> { length: number; [n: number]: T; }\ninterface ReadonlyArray<T> {}\ndeclare const console: { log(msg: any): void; };"
3737
},
38-
"../strings/foo.d.ts": {
39-
"version": "-1457151099-export declare const foo: string;\r\n",
40-
"signature": "-1457151099-export declare const foo: string;\r\n"
38+
"../strings/foo.json": {
39+
"version": "4395333385-{\n \"foo\": \"bar baz\"\n}"
4140
},
4241
"./index.ts": {
4342
"version": "-4651661680-import { foo } from '../strings/foo.json';\n\nconsole.log(foo);",
@@ -56,23 +55,19 @@ console.log(foo_json_1.foo);
5655
},
5756
"referencedMap": {
5857
"./index.ts": [
59-
"../strings/foo.d.ts"
58+
"../strings/foo.json"
6059
]
6160
},
6261
"exportedModulesMap": {},
6362
"semanticDiagnosticsPerFile": [
6463
"../../lib/lib.d.ts",
65-
"../strings/foo.d.ts",
64+
"../strings/foo.json",
6665
"./index.ts"
6766
]
6867
},
6968
"version": "FakeTSVersion"
7069
}
7170

72-
//// [/src/strings/foo.d.ts]
73-
export declare const foo: string;
74-
75-
7671
//// [/src/strings/tsconfig.tsbuildinfo]
7772
{
7873
"program": {

tests/baselines/reference/tsbuild/resolveJsonModule/initial-build/include-and-files.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
exitCode:: ExitStatus.Success
44

55

6-
//// [/src/dist/src/hello.d.ts]
7-
export declare const hello: string;
8-
9-
106
//// [/src/dist/src/hello.json]
117
{
128
"hello": "world"

0 commit comments

Comments
 (0)