Disable declaration emit for json files (#36078)

This commit is contained in:
Sheetal Nandi
2020-01-08 09:51:37 -08:00
committed by GitHub
parent d38906353f
commit 17f445035a
17 changed files with 99 additions and 151 deletions
+7 -5
View File
@@ -91,12 +91,13 @@ namespace ts {
}
else {
const ownOutputFilePath = getOwnEmitOutputFilePath(sourceFile.fileName, host, getOutputExtension(sourceFile, options));
const isJsonFile = isJsonSourceFile(sourceFile);
// If json file emits to the same location skip writing it, if emitDeclarationOnly skip writing it
const isJsonEmittedToSameLocation = isJsonSourceFile(sourceFile) &&
const isJsonEmittedToSameLocation = isJsonFile &&
comparePaths(sourceFile.fileName, ownOutputFilePath, host.getCurrentDirectory(), !host.useCaseSensitiveFileNames()) === Comparison.EqualTo;
const jsFilePath = options.emitDeclarationOnly || isJsonEmittedToSameLocation ? undefined : ownOutputFilePath;
const sourceMapFilePath = !jsFilePath || isJsonSourceFile(sourceFile) ? undefined : getSourceMapFilePath(jsFilePath, options);
const declarationFilePath = (forceDtsPaths || getEmitDeclarations(options)) ? getDeclarationEmitOutputFilePath(sourceFile.fileName, host) : undefined;
const declarationFilePath = (forceDtsPaths || (getEmitDeclarations(options) && !isJsonFile)) ? getDeclarationEmitOutputFilePath(sourceFile.fileName, host) : undefined;
const declarationMapPath = declarationFilePath && getAreDeclarationMapsEnabled(options) ? declarationFilePath + ".map" : undefined;
return { jsFilePath, sourceMapFilePath, declarationFilePath, declarationMapPath, buildInfoPath: undefined };
}
@@ -144,7 +145,7 @@ namespace ts {
/* @internal */
export function getOutputDeclarationFileName(inputFileName: string, configFile: ParsedCommandLine, ignoreCase: boolean) {
Debug.assert(!fileExtensionIs(inputFileName, Extension.Dts));
Debug.assert(!fileExtensionIs(inputFileName, Extension.Dts) && !fileExtensionIs(inputFileName, Extension.Json));
return changeExtension(
getOutputPathWithoutChangingExt(inputFileName, configFile, ignoreCase, configFile.options.declarationDir || configFile.options.outDir),
Extension.Dts
@@ -399,12 +400,13 @@ namespace ts {
return;
}
const sourceFiles = isSourceFile(sourceFileOrBundle) ? [sourceFileOrBundle] : sourceFileOrBundle.sourceFiles;
const filesForEmit = forceDtsEmit ? sourceFiles : filter(sourceFiles, isSourceFileNotJson);
// Setup and perform the transformation to retrieve declarations from the input files
const inputListOrBundle = (compilerOptions.outFile || compilerOptions.out) ? [createBundle(sourceFiles, !isSourceFile(sourceFileOrBundle) ? sourceFileOrBundle.prepends : undefined)] : sourceFiles;
const inputListOrBundle = (compilerOptions.outFile || compilerOptions.out) ? [createBundle(filesForEmit, !isSourceFile(sourceFileOrBundle) ? sourceFileOrBundle.prepends : undefined)] : filesForEmit;
if (emitOnlyDtsFiles && !getEmitDeclarations(compilerOptions)) {
// Checker wont collect the linked aliases since thats only done when declaration is enabled.
// Do that here when emitting only dts files
sourceFiles.forEach(collectLinkedAliases);
filesForEmit.forEach(collectLinkedAliases);
}
const declarationTransform = transformNodes(resolver, host, compilerOptions, inputListOrBundle, declarationTransformers, /*allowDtsFiles*/ false);
if (length(declarationTransform.diagnostics)) {
+4 -4
View File
@@ -860,7 +860,7 @@ namespace ts {
}
else if (getEmitModuleKind(parsedRef.commandLine.options) === ModuleKind.None) {
for (const fileName of parsedRef.commandLine.fileNames) {
if (!fileExtensionIs(fileName, Extension.Dts)) {
if (!fileExtensionIs(fileName, Extension.Dts) && !fileExtensionIs(fileName, Extension.Json)) {
processSourceFile(getOutputDeclarationFileName(fileName, parsedRef.commandLine, !host.useCaseSensitiveFileNames()), /*isDefaultLib*/ false, /*ignoreNoDefaultLib*/ false, /*packageId*/ undefined);
}
}
@@ -2459,8 +2459,8 @@ namespace ts {
}
function getProjectReferenceRedirectProject(fileName: string) {
// Ignore dts
if (!resolvedProjectReferences || !resolvedProjectReferences.length || fileExtensionIs(fileName, Extension.Dts)) {
// Ignore dts or any json files
if (!resolvedProjectReferences || !resolvedProjectReferences.length || fileExtensionIs(fileName, Extension.Dts) || fileExtensionIs(fileName, Extension.Json)) {
return undefined;
}
@@ -2521,7 +2521,7 @@ namespace ts {
}
else {
forEach(resolvedRef.commandLine.fileNames, fileName => {
if (!fileExtensionIs(fileName, Extension.Dts)) {
if (!fileExtensionIs(fileName, Extension.Dts) && !fileExtensionIs(fileName, Extension.Json)) {
const outputDts = getOutputDeclarationFileName(fileName, resolvedRef.commandLine, host.useCaseSensitiveFileNames());
mapFromToProjectReferenceRedirectSource!.set(toPath(outputDts), fileName);
}
+4 -1
View File
@@ -1,8 +1,11 @@
/*@internal*/
namespace ts {
export function getDeclarationDiagnostics(host: EmitHost, resolver: EmitResolver, file: SourceFile | undefined): DiagnosticWithLocation[] | undefined {
if (file && isJsonSourceFile(file)) {
return []; // No declaration diagnostics for json for now
}
const compilerOptions = host.getCompilerOptions();
const result = transformNodes(resolver, host, compilerOptions, file ? [file] : host.getSourceFiles(), [transformDeclarations], /*allowDtsFiles*/ false);
const result = transformNodes(resolver, host, compilerOptions, file ? [file] : filter(host.getSourceFiles(), isSourceFileNotJson), [transformDeclarations], /*allowDtsFiles*/ false);
return result.diagnostics;
}
+4
View File
@@ -1802,6 +1802,10 @@ namespace ts {
return !!node && !!(node.flags & NodeFlags.JsonFile);
}
export function isSourceFileNotJson(file: SourceFile) {
return !isJsonSourceFile(file);
}
export function isInJSDoc(node: Node | undefined): boolean {
return !!node && !!(node.flags & NodeFlags.JSDoc);
}
+1 -1
View File
@@ -888,7 +888,7 @@ namespace Harness {
throw new Error("Only declaration files should be generated when emitDeclarationOnly:true");
}
}
else if (result.dts.size !== result.getNumberOfJsFiles(/*includeJson*/ true)) {
else if (result.dts.size !== result.getNumberOfJsFiles(/*includeJson*/ false)) {
throw new Error("There were no errors and declFiles generated did not match number of js files generated");
}
}
@@ -25,24 +25,6 @@ var j = require("./obj.json");
module.exports = j;
//// [obj.d.ts]
export declare const x: number;
export declare const y: number;
export declare namespace obj {
export const items: ({
"x": number;
"y"?: undefined;
"err"?: undefined;
} | {
"x": number;
"y": number;
"err"?: undefined;
} | {
"x": number;
"err": boolean;
"y"?: undefined;
})[];
}
//// [index.d.ts]
export = j;
declare const j: {
@@ -74,34 +74,6 @@ var j = require("./package.json");
module.exports = j;
//// [package.d.ts]
export declare const name: string;
export declare const version: string;
export declare const description: string;
export declare const main: string;
export declare namespace bin {
export const cli: string;
}
export declare namespace engines {
export const node: string;
}
export declare namespace scripts {
export const scriptname: string;
}
export declare const devDependencies: {
"@ns/dep": string;
};
export declare namespace dependencies {
export const dep: string;
}
export declare const repository: string;
export declare const keywords: string[];
export declare const author: string;
export declare const license: string;
export declare const homepage: string;
export declare namespace config {
export const o: string[];
}
//// [index.d.ts]
export = j;
declare const j: {
@@ -85,25 +85,5 @@ numberLiteral = f[0];
booleanLiteral = g[0];
//// [out/b.d.ts]
export declare const a: boolean;
export declare const b: string;
export declare const c: null;
export declare const d: boolean;
//// [out/c.d.ts]
declare const _exports: (string | null)[];
export = _exports;
//// [out/d.d.ts]
declare const _exports: string;
export = _exports;
//// [out/e.d.ts]
declare const _exports: number;
export = _exports;
//// [out/f.d.ts]
declare const _exports: number[];
export = _exports;
//// [out/g.d.ts]
declare const _exports: boolean[];
export = _exports;
//// [out/file1.d.ts]
export {};
@@ -32,8 +32,5 @@ if (x) {
}
//// [out/b.d.ts]
export declare const a: boolean;
export declare const b: string;
//// [out/file1.d.ts]
export {};
@@ -4,8 +4,9 @@ exitCode:: ExitStatus.Success
//// [/out/sub-project/index.d.ts]
export const m: typeof mod;
import mod from "../common";
export const m: {
"val": number;
};
//// [/out/sub-project/index.js]
@@ -27,8 +28,7 @@ exports.m = common_1["default"];
"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"
},
"../../src/common/obj.json": {
"version": "-6323167306-export declare const val: number;\r\n",
"signature": "-6323167306-export declare const val: number;\r\n"
"version": "2151907832-{\n \"val\": 42\n}"
},
"../../src/common/index.ts": {
"version": "-4085459678-import x = require(\"./obj.json\");\r\nexport = x;\r\n",
@@ -36,7 +36,7 @@ exports.m = common_1["default"];
},
"../../src/sub-project/index.js": {
"version": "-14684157955-import mod from '../common';\n\nexport const m = mod;\n",
"signature": "-229957289-export const m: typeof mod;\r\nimport mod from \"../common\";\r\n"
"signature": "-12693309262-export const m: {\r\n \"val\": number;\r\n};\r\n"
}
},
"options": {
@@ -53,7 +53,7 @@ exports.m = common_1["default"];
},
"referencedMap": {
"../../src/common/index.ts": [
"../../src/common/obj.d.ts"
"../../src/common/obj.json"
],
"../../src/sub-project/index.js": [
"../../src/common/index.d.ts"
@@ -61,7 +61,7 @@ exports.m = common_1["default"];
},
"exportedModulesMap": {
"../../src/common/index.ts": [
"../../src/common/obj.d.ts"
"../../src/common/obj.json"
]
},
"semanticDiagnosticsPerFile": [
@@ -76,7 +76,9 @@ exports.m = common_1["default"];
//// [/out/sub-project-2/index.d.ts]
export function getVar(): {
key: typeof import("../common/obj.json");
key: {
"val": number;
};
};
@@ -101,21 +103,13 @@ exports.getVar = getVar;
"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",
"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"
},
"../../src/common/obj.json": {
"version": "-6323167306-export declare const val: number;\r\n",
"signature": "-6323167306-export declare const val: number;\r\n"
},
"../../src/common/index.ts": {
"version": "-4085459678-import x = require(\"./obj.json\");\r\nexport = x;\r\n",
"signature": "-4085459678-import x = require(\"./obj.json\");\r\nexport = x;\r\n"
},
"../../src/sub-project/index.js": {
"version": "-229957289-export const m: typeof mod;\r\nimport mod from \"../common\";\r\n",
"signature": "-229957289-export const m: typeof mod;\r\nimport mod from \"../common\";\r\n"
"version": "-12693309262-export const m: {\r\n \"val\": number;\r\n};\r\n",
"signature": "-12693309262-export const m: {\r\n \"val\": number;\r\n};\r\n"
},
"../../src/sub-project-2/index.js": {
"version": "13545386800-import { m } from '../sub-project/index';\n\nconst variable = {\n key: m,\n};\n\nexport function getVar() {\n return variable;\n}\n",
"signature": "-9206156860-export function getVar(): {\r\n key: typeof import(\"../common/obj.json\");\r\n};\r\n"
"signature": "-11261617214-export function getVar(): {\r\n key: {\r\n \"val\": number;\r\n };\r\n};\r\n"
}
},
"options": {
@@ -131,31 +125,13 @@ exports.getVar = getVar;
"configFilePath": "../../src/sub-project-2/tsconfig.json"
},
"referencedMap": {
"../../src/common/index.ts": [
"../../src/common/obj.d.ts"
],
"../../src/sub-project-2/index.js": [
"../sub-project/index.d.ts"
],
"../../src/sub-project/index.js": [
"../../src/common/index.d.ts"
]
},
"exportedModulesMap": {
"../../src/common/index.ts": [
"../../src/common/obj.d.ts"
],
"../../src/sub-project-2/index.js": [
"../../src/common/obj.d.ts"
],
"../../src/sub-project/index.js": [
"../../src/common/index.d.ts"
]
},
"exportedModulesMap": {},
"semanticDiagnosticsPerFile": [
"../../lib/lib.d.ts",
"../../src/common/index.ts",
"../../src/common/obj.json",
"../../src/sub-project-2/index.js",
"../../src/sub-project/index.js"
]
@@ -174,10 +150,6 @@ var x = require("./obj.json");
module.exports = x;
//// [/src/common/obj.d.ts]
export declare const val: number;
//// [/src/common/tsconfig.tsbuildinfo]
{
"program": {
@@ -3,10 +3,6 @@
exitCode:: ExitStatus.Success
//// [/src/dist/src/hello.d.ts]
export declare const hello: string;
//// [/src/dist/src/hello.json]
{
"hello": "world"
@@ -36,8 +36,7 @@ console.log(foo_json_1.foo);
"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; };"
},
"../strings/foo.json": {
"version": "-1457151099-export declare const foo: string;\r\n",
"signature": "-1457151099-export declare const foo: string;\r\n"
"version": "4395333385-{\n \"foo\": \"bar baz\"\n}"
},
"./index.ts": {
"version": "-4651661680-import { foo } from '../strings/foo.json';\n\nconsole.log(foo);",
@@ -56,7 +55,7 @@ console.log(foo_json_1.foo);
},
"referencedMap": {
"./index.ts": [
"../strings/foo.d.ts"
"../strings/foo.json"
]
},
"exportedModulesMap": {},
@@ -69,10 +68,6 @@ console.log(foo_json_1.foo);
"version": "FakeTSVersion"
}
//// [/src/strings/foo.d.ts]
export declare const foo: string;
//// [/src/strings/tsconfig.tsbuildinfo]
{
"program": {
@@ -3,10 +3,6 @@
exitCode:: ExitStatus.Success
//// [/src/dist/src/hello.d.ts]
export declare const hello: string;
//// [/src/dist/src/hello.json]
{
"hello": "world"
@@ -1,9 +1,70 @@
//// [/lib/initial-buildOutput.txt]
/lib/tsc --b /src/tsconfig_withIncludeOfJson.json
error TS5056: Cannot write file '/src/dist/src/index.d.ts' because it would be overwritten by multiple input files.
exitCode:: ExitStatus.DiagnosticsPresent_OutputsSkipped
exitCode:: ExitStatus.Success
//// [/src/dist/src/index.d.ts]
declare const _default: string;
export default _default;
//// [/src/dist/src/index.js]
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
exports.__esModule = true;
var index_json_1 = __importDefault(require("./index.json"));
exports["default"] = index_json_1["default"].hello;
//// [/src/dist/src/index.json]
{ "hello": "world" }
//// [/src/dist/tsconfig_withIncludeOfJson.tsbuildinfo]
{
"program": {
"fileInfos": {
"../../lib/lib.d.ts": {
"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; };",
"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; };"
},
"../src/index.json": {
"version": "-2379406821-{\"hello\":\"world\"}",
"signature": "-4341462827-export declare const hello: string;\r\n"
},
"../src/index.ts": {
"version": "-6335882310-import hello from \"./index.json\"\n\nexport default hello.hello",
"signature": "-1680156224-declare const _default: string;\r\nexport default _default;\r\n"
}
},
"options": {
"composite": true,
"moduleResolution": 2,
"module": 1,
"resolveJsonModule": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"outDir": "./",
"skipDefaultLibCheck": true,
"configFilePath": "../tsconfig_withIncludeOfJson.json"
},
"referencedMap": {
"../src/index.ts": [
"../src/index.json"
]
},
"exportedModulesMap": {},
"semanticDiagnosticsPerFile": [
"../../lib/lib.d.ts",
"../src/index.json",
"../src/index.ts"
]
},
"version": "FakeTSVersion"
}
//// [/src/src/hello.json] unlink
//// [/src/src/index.json]
{"hello":"world"}
@@ -3,10 +3,6 @@
exitCode:: ExitStatus.Success
//// [/src/dist/src/hello.d.ts]
export declare const hello: string;
//// [/src/dist/src/hello.json]
{
"hello": "world"
@@ -10,10 +10,6 @@
exitCode:: ExitStatus.Success
//// [/src/dist/src/hello.d.ts]
export declare const hello: string;
//// [/src/dist/src/hello.json]
{
"hello": "world"
@@ -10,10 +10,6 @@
exitCode:: ExitStatus.Success
//// [/src/src/hello.d.ts]
export declare const hello: string;
//// [/src/src/index.d.ts]
declare const _default: string;
export default _default;