Skip to content

Commit

Permalink
Disable declaration emit for json files
Browse files Browse the repository at this point in the history
  • Loading branch information
sheetalkamat committed Jan 8, 2020
1 parent 9fbcdb1 commit 0c4d161
Show file tree
Hide file tree
Showing 17 changed files with 103 additions and 155 deletions.
12 changes: 7 additions & 5 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -400,12 +401,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)) {
Expand Down
8 changes: 4 additions & 4 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,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);
}
}
Expand Down Expand Up @@ -2500,8 +2500,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;
}

Expand Down Expand Up @@ -2562,7 +2562,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);
}
Expand Down
5 changes: 4 additions & 1 deletion src/compiler/transformers/declarations.ts
Original file line number Diff line number Diff line change
@@ -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;
}

Expand Down
4 changes: 4 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1808,6 +1808,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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/harness/harnessIO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,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");
}
}
Expand Down
18 changes: 0 additions & 18 deletions tests/baselines/reference/jsDeclarationsJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
28 changes: 0 additions & 28 deletions tests/baselines/reference/jsDeclarationsPackageJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
20 changes: 0 additions & 20 deletions tests/baselines/reference/requireOfJsonFileTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};
3 changes: 0 additions & 3 deletions tests/baselines/reference/requireOfJsonFileWithDeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -26,17 +27,16 @@ exports.m = common_1["default"];
"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.d.ts": {
"version": "-6323167306-export declare const val: number;\r\n",
"signature": "-6323167306-export declare const val: number;\r\n"
"../../src/common/obj.json": {
"version": "2151907832-{\n \"val\": 42\n}"
},
"../../src/common/index.d.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": "-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": "-15768184370-export const m: {\r\n val: number;\r\n};\r\n"
}
},
"options": {
Expand All @@ -53,21 +53,21 @@ exports.m = common_1["default"];
},
"referencedMap": {
"../../src/common/index.d.ts": [
"../../src/common/obj.d.ts"
"../../src/common/obj.json"
],
"../../src/sub-project/index.js": [
"../../src/common/index.d.ts"
]
},
"exportedModulesMap": {
"../../src/common/index.d.ts": [
"../../src/common/obj.d.ts"
"../../src/common/obj.json"
]
},
"semanticDiagnosticsPerFile": [
"../../lib/lib.d.ts",
"../../src/common/index.d.ts",
"../../src/common/obj.d.ts",
"../../src/common/obj.json",
"../../src/sub-project/index.js"
]
},
Expand All @@ -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;
};
};


Expand All @@ -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.d.ts": {
"version": "-6323167306-export declare const val: number;\r\n",
"signature": "-6323167306-export declare const val: number;\r\n"
},
"../../src/common/index.d.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"
},
"../sub-project/index.d.ts": {
"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": "-15768184370-export const m: {\r\n val: number;\r\n};\r\n",
"signature": "-15768184370-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": "-2686589794-export function getVar(): {\r\n key: {\r\n val: number;\r\n };\r\n};\r\n"
}
},
"options": {
Expand All @@ -131,31 +125,13 @@ exports.getVar = getVar;
"configFilePath": "../../src/sub-project-2/tsconfig.json"
},
"referencedMap": {
"../../src/common/index.d.ts": [
"../../src/common/obj.d.ts"
],
"../../src/sub-project-2/index.js": [
"../sub-project/index.d.ts"
],
"../sub-project/index.d.ts": [
"../../src/common/index.d.ts"
]
},
"exportedModulesMap": {
"../../src/common/index.d.ts": [
"../../src/common/obj.d.ts"
],
"../../src/sub-project-2/index.js": [
"../../src/common/obj.d.ts"
],
"../sub-project/index.d.ts": [
"../../src/common/index.d.ts"
]
},
"exportedModulesMap": {},
"semanticDiagnosticsPerFile": [
"../../lib/lib.d.ts",
"../../src/common/index.d.ts",
"../../src/common/obj.d.ts",
"../../src/sub-project-2/index.js",
"../sub-project/index.d.ts"
]
Expand All @@ -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": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ console.log(foo_json_1.foo);
"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; };"
},
"../strings/foo.d.ts": {
"version": "-1457151099-export declare const foo: string;\r\n",
"signature": "-1457151099-export declare const foo: string;\r\n"
"../strings/foo.json": {
"version": "4395333385-{\n \"foo\": \"bar baz\"\n}"
},
"./index.ts": {
"version": "-4651661680-import { foo } from '../strings/foo.json';\n\nconsole.log(foo);",
Expand All @@ -56,23 +55,19 @@ console.log(foo_json_1.foo);
},
"referencedMap": {
"./index.ts": [
"../strings/foo.d.ts"
"../strings/foo.json"
]
},
"exportedModulesMap": {},
"semanticDiagnosticsPerFile": [
"../../lib/lib.d.ts",
"../strings/foo.d.ts",
"../strings/foo.json",
"./index.ts"
]
},
"version": "FakeTSVersion"
}

//// [/src/strings/foo.d.ts]
export declare const foo: string;


//// [/src/strings/tsconfig.tsbuildinfo]
{
"program": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading

0 comments on commit 0c4d161

Please sign in to comment.