Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move json declaration file emit behind a experimentalJsonDeclarationEmit flag #34962

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,13 @@ namespace ts {
category: Diagnostics.Experimental_Options,
description: Diagnostics.Enables_experimental_support_for_emitting_type_metadata_for_decorators
},
{
name: "experimentalJsonDeclarationEmit",
type: "boolean",
category: Diagnostics.Advanced_Options, // Under "advanced options" so it's hidden from --init
description: Diagnostics.Enables_experimental_declaration_emit_for_json_files,
affectsEmit: true,
},

// Advanced
{
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -4140,6 +4140,10 @@
"category": "Message",
"code": 6223
},
"Enables experimental declaration emit for json files.": {
"category": "Message",
"code": 6224
},

"Projects to reference": {
"category": "Message",
Expand Down
16 changes: 15 additions & 1 deletion src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,20 @@ namespace ts {
inputFileName;
}

/* @internal */
export function hasDeclarationFileOutput(filename: string, options: CompilerOptions) {
if (fileExtensionIs(filename, Extension.Json) && !options.experimentalJsonDeclarationEmit) {
return false;
}
return true;
}

/* @internal */
export function getOutputDeclarationFileName(inputFileName: string, configFile: ParsedCommandLine, ignoreCase: boolean) {
Debug.assert(!fileExtensionIs(inputFileName, Extension.Dts));
if (!hasDeclarationFileOutput(inputFileName, configFile.options)) {
return undefined;
}
return changeExtension(
getOutputPathWithoutChangingExt(inputFileName, configFile, ignoreCase, configFile.options.declarationDir || configFile.options.outDir),
Extension.Dts
Expand Down Expand Up @@ -247,7 +258,10 @@ namespace ts {
if (jsFilePath) return jsFilePath;
if (fileExtensionIs(inputFileName, Extension.Json)) continue;
if (getEmitDeclarations(configFile.options)) {
return getOutputDeclarationFileName(inputFileName, configFile, ignoreCase);
const result = getOutputDeclarationFileName(inputFileName, configFile, ignoreCase);
if (result) {
return result;
}
}
}
const buildInfoPath = getTsBuildInfoEmitOutputFilePath(configFile.options);
Expand Down
29 changes: 19 additions & 10 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,10 @@ namespace ts {
else if (getEmitModuleKind(parsedRef.commandLine.options) === ModuleKind.None) {
for (const fileName of parsedRef.commandLine.fileNames) {
if (!fileExtensionIs(fileName, Extension.Dts)) {
processSourceFile(getOutputDeclarationFileName(fileName, parsedRef.commandLine, !host.useCaseSensitiveFileNames()), /*isDefaultLib*/ false, /*ignoreNoDefaultLib*/ false, /*packageId*/ undefined);
const outputPath = getOutputDeclarationFileName(fileName, parsedRef.commandLine, !host.useCaseSensitiveFileNames());
if (outputPath) {
processSourceFile(outputPath, /*isDefaultLib*/ false, /*ignoreNoDefaultLib*/ false, /*packageId*/ undefined);
}
}
}
}
Expand Down Expand Up @@ -1434,7 +1437,9 @@ namespace ts {
const redirectProject = getProjectReferenceRedirectProject(newSourceFile.fileName);
if (redirectProject && !(redirectProject.commandLine.options.outFile || redirectProject.commandLine.options.out)) {
const redirect = getProjectReferenceOutputName(redirectProject, newSourceFile.fileName);
addFileToFilesByName(newSourceFile, toPath(redirect), /*redirectedPath*/ undefined);
if (redirect) {
addFileToFilesByName(newSourceFile, toPath(redirect), /*redirectedPath*/ undefined);
}
}
}
// Set the file as found during node modules search if it was found that way in old progra,
Expand Down Expand Up @@ -2356,13 +2361,15 @@ namespace ts {
return undefined;
}
const redirect = getProjectReferenceOutputName(redirectProject, fileName);
fileName = redirect;
// Once we start redirecting to a file, we can potentially come back to it
// via a back-reference from another file in the .d.ts folder. If that happens we'll
// end up trying to add it to the program *again* because we were tracking it via its
// original (un-redirected) name. So we have to map both the original path and the redirected path
// to the source file we're about to find/create
redirectedPath = toPath(redirect);
if (redirect) {
fileName = redirect;
// Once we start redirecting to a file, we can potentially come back to it
// via a back-reference from another file in the .d.ts folder. If that happens we'll
// end up trying to add it to the program *again* because we were tracking it via its
// original (un-redirected) name. So we have to map both the original path and the redirected path
// to the source file we're about to find/create
redirectedPath = toPath(redirect);
}
}
}

Expand Down Expand Up @@ -2533,7 +2540,9 @@ namespace ts {
forEach(resolvedRef.commandLine.fileNames, fileName => {
if (!fileExtensionIs(fileName, Extension.Dts)) {
const outputDts = getOutputDeclarationFileName(fileName, resolvedRef.commandLine, host.useCaseSensitiveFileNames());
mapFromToProjectReferenceRedirectSource!.set(toPath(outputDts), fileName);
if (outputDts) {
mapFromToProjectReferenceRedirectSource!.set(toPath(outputDts), fileName);
}
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/transformers/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
namespace ts {
export function getDeclarationDiagnostics(host: EmitHost, resolver: EmitResolver, file: SourceFile | undefined): DiagnosticWithLocation[] | undefined {
const compilerOptions = host.getCompilerOptions();
const result = transformNodes(resolver, host, compilerOptions, file ? [file] : host.getSourceFiles(), [transformDeclarations], /*allowDtsFiles*/ false);
const result = transformNodes(resolver, host, compilerOptions, filter(file ? [file] : host.getSourceFiles(), f => !!getOutputPathsFor(f, host, /*forcedtsPaths*/ false).declarationFilePath), [transformDeclarations], /*allowDtsFiles*/ false);
return result.diagnostics;
}

Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5029,6 +5029,7 @@ namespace ts {
esModuleInterop?: boolean;
/* @internal */ showConfig?: boolean;
useDefineForClassFields?: boolean;
/* @internal */ experimentalJsonDeclarationEmit?: boolean;

[option: string]: CompilerOptionsValue | TsConfigSourceFile | undefined;
}
Expand Down
5 changes: 4 additions & 1 deletion src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3633,7 +3633,10 @@ namespace ts {
return getDeclarationEmitOutputFilePathWorker(fileName, host.getCompilerOptions(), host.getCurrentDirectory(), host.getCommonSourceDirectory(), f => host.getCanonicalFileName(f));
}

export function getDeclarationEmitOutputFilePathWorker(fileName: string, options: CompilerOptions, currentDirectory: string, commonSourceDirectory: string, getCanonicalFileName: GetCanonicalFileName): string {
export function getDeclarationEmitOutputFilePathWorker(fileName: string, options: CompilerOptions, currentDirectory: string, commonSourceDirectory: string, getCanonicalFileName: GetCanonicalFileName): string | undefined {
if (!hasDeclarationFileOutput(fileName, options)) {
return undefined;
}
const outputDir = options.declarationDir || options.outDir; // Prefer declaration folder if specified

const path = outputDir
Expand Down
2 changes: 1 addition & 1 deletion src/harness/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(ts.hasDeclarationFileOutput("/foo.json", options))) {
throw new Error("There were no errors and declFiles generated did not match number of js files generated");
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/server/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1219,8 +1219,8 @@ namespace ts.server {
};
}

private isValidGeneratedFileWatcher(generateFile: string, watcher: GeneratedFileWatcher) {
return this.toPath(generateFile) === watcher.generatedFilePath;
private isValidGeneratedFileWatcher(generateFile: string | undefined, watcher: GeneratedFileWatcher) {
return generateFile !== undefined && this.toPath(generateFile) === watcher.generatedFilePath;
}

private clearGeneratedFileWatch() {
Expand Down
3 changes: 2 additions & 1 deletion src/testRunner/unittests/tsbuild/javascriptProjectEmit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@ namespace ts {
"checkJs": true,
"resolveJsonModule": true,
"esModuleInterop": true,
"declaration": true
"declaration": true,
"experimentalJsonDeclarationEmit": true
}
}`,
}, symbolLibContent),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"experimentalJsonDeclarationEmit": true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ exports.m = common_1["default"];
"resolveJsonModule": true,
"esModuleInterop": true,
"declaration": true,
"experimentalJsonDeclarationEmit": true,
"composite": true,
"configFilePath": "../../src/sub-project/tsconfig.json"
},
Expand Down Expand Up @@ -127,6 +128,7 @@ exports.getVar = getVar;
"resolveJsonModule": true,
"esModuleInterop": true,
"declaration": true,
"experimentalJsonDeclarationEmit": true,
"composite": true,
"configFilePath": "../../src/sub-project-2/tsconfig.json"
},
Expand Down Expand Up @@ -204,6 +206,7 @@ export declare const val: number;
"resolveJsonModule": true,
"esModuleInterop": true,
"declaration": true,
"experimentalJsonDeclarationEmit": true,
"composite": true,
"configFilePath": "./tsconfig.json"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ exports["default"] = hello_json_1["default"].hello;
"allowSyntheticDefaultImports": true,
"outDir": "./",
"skipDefaultLibCheck": true,
"experimentalJsonDeclarationEmit": true,
"configFilePath": "../tsconfig_withFiles.json"
},
"referencedMap": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ console.log(foo_json_1.foo);
"resolveJsonModule": true,
"strict": true,
"esModuleInterop": true,
"experimentalJsonDeclarationEmit": true,
"configFilePath": "./tsconfig.json"
},
"referencedMap": {
Expand Down Expand Up @@ -94,6 +95,7 @@ export declare const foo: string;
"resolveJsonModule": true,
"strict": true,
"esModuleInterop": true,
"experimentalJsonDeclarationEmit": true,
"configFilePath": "./tsconfig.json"
},
"referencedMap": {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ exports["default"] = hello_json_1["default"].hello;
"allowSyntheticDefaultImports": true,
"outDir": "./",
"skipDefaultLibCheck": true,
"experimentalJsonDeclarationEmit": true,
"configFilePath": "../tsconfig_withIncludeAndFiles.json"
},
"referencedMap": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ exports["default"] = hello_json_1["default"].hello;
"allowSyntheticDefaultImports": true,
"outDir": "./",
"skipDefaultLibCheck": true,
"experimentalJsonDeclarationEmit": true,
"configFilePath": "../tsconfig_withIncludeOfJson.json"
},
"referencedMap": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ exports["default"] = hello_json_1["default"].hello;
"allowSyntheticDefaultImports": true,
"outDir": "./",
"skipDefaultLibCheck": true,
"experimentalJsonDeclarationEmit": true,
"configFilePath": "../tsconfig_withFiles.json"
},
"referencedMap": {
Expand Down Expand Up @@ -92,7 +93,8 @@ exports["default"] = hello_json_1["default"].hello;
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"outDir": "dist",
"skipDefaultLibCheck": true
"skipDefaultLibCheck": true,
"experimentalJsonDeclarationEmit": true
},
"files": [
"src/index.ts", "src/hello.json"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ exports["default"] = hello_json_1["default"].hello;
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,

"skipDefaultLibCheck": true
"skipDefaultLibCheck": true,
"experimentalJsonDeclarationEmit": true
},
"files": [
"src/index.ts", "src/hello.json"
Expand Down Expand Up @@ -71,6 +72,7 @@ exports["default"] = hello_json_1["default"].hello;
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"skipDefaultLibCheck": true,
"experimentalJsonDeclarationEmit": true,
"configFilePath": "./tsconfig_withFiles.json"
},
"referencedMap": {
Expand Down
1 change: 1 addition & 0 deletions tests/cases/compiler/requireOfJsonFileTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// @fullEmitPaths: true
// @resolveJsonModule: true
// @declaration: true
// @experimentalJsonDeclarationEmit: true

// @Filename: file1.ts
import b = require('./b.json');
Expand Down
1 change: 1 addition & 0 deletions tests/cases/compiler/requireOfJsonFileWithDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// @fullEmitPaths: true
// @resolveJsonModule: true
// @declaration: true
// @experimentalJsonDeclarationEmit: true

// @Filename: file1.ts
import b1 = require('./b.json');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// @resolveJsonModule: true
// @outDir: ./out
// @declaration: true
// @experimentalJsonDeclarationEmit: true
// @filename: index.js
const j = require("./obj.json");
module.exports = j;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// @resolveJsonModule: true
// @outDir: ./out
// @declaration: true
// @experimentalJsonDeclarationEmit: true
// @filename: index.js
const j = require("./package.json");
module.exports = j;
Expand Down
3 changes: 2 additions & 1 deletion tests/projects/importJsonFromProjectReference/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"composite": true,
"resolveJsonModule": true,
"strict": true,
"esModuleInterop": true
"esModuleInterop": true,
"experimentalJsonDeclarationEmit": true
},
"references": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"outDir": "dist",
"skipDefaultLibCheck": true
"skipDefaultLibCheck": true,
"experimentalJsonDeclarationEmit": true
},
"files": [
"src/index.ts", "src/hello.json"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"outDir": "dist",
"skipDefaultLibCheck": true
"skipDefaultLibCheck": true,
"experimentalJsonDeclarationEmit": true
},
"include": [
"src/**/*"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"outDir": "dist",
"skipDefaultLibCheck": true
"skipDefaultLibCheck": true,
"experimentalJsonDeclarationEmit": true
},
"files": [
"src/hello.json"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"outDir": "dist",
"skipDefaultLibCheck": true
"skipDefaultLibCheck": true,
"experimentalJsonDeclarationEmit": true
},
"include": [
"src/**/*", "src/**/*.json"
Expand Down