Skip to content

Commit 17f4450

Browse files
authored
Disable declaration emit for json files (#36078)
1 parent d389063 commit 17f4450

17 files changed

+99
-151
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
@@ -399,12 +400,13 @@ namespace ts {
399400
return;
400401
}
401402
const sourceFiles = isSourceFile(sourceFileOrBundle) ? [sourceFileOrBundle] : sourceFileOrBundle.sourceFiles;
403+
const filesForEmit = forceDtsEmit ? sourceFiles : filter(sourceFiles, isSourceFileNotJson);
402404
// Setup and perform the transformation to retrieve declarations from the input files
403-
const inputListOrBundle = (compilerOptions.outFile || compilerOptions.out) ? [createBundle(sourceFiles, !isSourceFile(sourceFileOrBundle) ? sourceFileOrBundle.prepends : undefined)] : sourceFiles;
405+
const inputListOrBundle = (compilerOptions.outFile || compilerOptions.out) ? [createBundle(filesForEmit, !isSourceFile(sourceFileOrBundle) ? sourceFileOrBundle.prepends : undefined)] : filesForEmit;
404406
if (emitOnlyDtsFiles && !getEmitDeclarations(compilerOptions)) {
405407
// Checker wont collect the linked aliases since thats only done when declaration is enabled.
406408
// Do that here when emitting only dts files
407-
sourceFiles.forEach(collectLinkedAliases);
409+
filesForEmit.forEach(collectLinkedAliases);
408410
}
409411
const declarationTransform = transformNodes(resolver, host, compilerOptions, inputListOrBundle, declarationTransformers, /*allowDtsFiles*/ false);
410412
if (length(declarationTransform.diagnostics)) {

src/compiler/program.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,7 @@ namespace ts {
860860
}
861861
else if (getEmitModuleKind(parsedRef.commandLine.options) === ModuleKind.None) {
862862
for (const fileName of parsedRef.commandLine.fileNames) {
863-
if (!fileExtensionIs(fileName, Extension.Dts)) {
863+
if (!fileExtensionIs(fileName, Extension.Dts) && !fileExtensionIs(fileName, Extension.Json)) {
864864
processSourceFile(getOutputDeclarationFileName(fileName, parsedRef.commandLine, !host.useCaseSensitiveFileNames()), /*isDefaultLib*/ false, /*ignoreNoDefaultLib*/ false, /*packageId*/ undefined);
865865
}
866866
}
@@ -2459,8 +2459,8 @@ namespace ts {
24592459
}
24602460

24612461
function getProjectReferenceRedirectProject(fileName: string) {
2462-
// Ignore dts
2463-
if (!resolvedProjectReferences || !resolvedProjectReferences.length || fileExtensionIs(fileName, Extension.Dts)) {
2462+
// Ignore dts or any json files
2463+
if (!resolvedProjectReferences || !resolvedProjectReferences.length || fileExtensionIs(fileName, Extension.Dts) || fileExtensionIs(fileName, Extension.Json)) {
24642464
return undefined;
24652465
}
24662466

@@ -2521,7 +2521,7 @@ namespace ts {
25212521
}
25222522
else {
25232523
forEach(resolvedRef.commandLine.fileNames, fileName => {
2524-
if (!fileExtensionIs(fileName, Extension.Dts)) {
2524+
if (!fileExtensionIs(fileName, Extension.Dts) && !fileExtensionIs(fileName, Extension.Json)) {
25252525
const outputDts = getOutputDeclarationFileName(fileName, resolvedRef.commandLine, host.useCaseSensitiveFileNames());
25262526
mapFromToProjectReferenceRedirectSource!.set(toPath(outputDts), fileName);
25272527
}

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
@@ -1802,6 +1802,10 @@ namespace ts {
18021802
return !!node && !!(node.flags & NodeFlags.JsonFile);
18031803
}
18041804

1805+
export function isSourceFileNotJson(file: SourceFile) {
1806+
return !isJsonSourceFile(file);
1807+
}
1808+
18051809
export function isInJSDoc(node: Node | undefined): boolean {
18061810
return !!node && !!(node.flags & NodeFlags.JSDoc);
18071811
}

src/harness/harness.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ namespace Harness {
888888
throw new Error("Only declaration files should be generated when emitDeclarationOnly:true");
889889
}
890890
}
891-
else if (result.dts.size !== result.getNumberOfJsFiles(/*includeJson*/ true)) {
891+
else if (result.dts.size !== result.getNumberOfJsFiles(/*includeJson*/ false)) {
892892
throw new Error("There were no errors and declFiles generated did not match number of js files generated");
893893
}
894894
}

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: 14 additions & 42 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]
@@ -27,16 +28,15 @@ exports.m = common_1["default"];
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
},
2930
"../../src/common/obj.json": {
30-
"version": "-6323167306-export declare const val: number;\r\n",
31-
"signature": "-6323167306-export declare const val: number;\r\n"
31+
"version": "2151907832-{\n \"val\": 42\n}"
3232
},
3333
"../../src/common/index.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": "-12693309262-export const m: {\r\n \"val\": number;\r\n};\r\n"
4040
}
4141
},
4242
"options": {
@@ -53,15 +53,15 @@ exports.m = common_1["default"];
5353
},
5454
"referencedMap": {
5555
"../../src/common/index.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.ts": [
64-
"../../src/common/obj.d.ts"
64+
"../../src/common/obj.json"
6565
]
6666
},
6767
"semanticDiagnosticsPerFile": [
@@ -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.json": {
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.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
"../../src/sub-project/index.js": {
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": "-12693309262-export const m: {\r\n \"val\": number;\r\n};\r\n",
108+
"signature": "-12693309262-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": "-11261617214-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.ts": [
135-
"../../src/common/obj.d.ts"
136-
],
137128
"../../src/sub-project-2/index.js": [
138129
"../sub-project/index.d.ts"
139-
],
140-
"../../src/sub-project/index.js": [
141-
"../../src/common/index.d.ts"
142-
]
143-
},
144-
"exportedModulesMap": {
145-
"../../src/common/index.ts": [
146-
"../../src/common/obj.d.ts"
147-
],
148-
"../../src/sub-project-2/index.js": [
149-
"../../src/common/obj.d.ts"
150-
],
151-
"../../src/sub-project/index.js": [
152-
"../../src/common/index.d.ts"
153130
]
154131
},
132+
"exportedModulesMap": {},
155133
"semanticDiagnosticsPerFile": [
156134
"../../lib/lib.d.ts",
157-
"../../src/common/index.ts",
158-
"../../src/common/obj.json",
159135
"../../src/sub-project-2/index.js",
160136
"../../src/sub-project/index.js"
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": {

0 commit comments

Comments
 (0)