From b98089dc53e0ce6c4e61815a95fdcd4b5dfb0fcb Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Thu, 3 Nov 2016 10:35:23 -0700 Subject: [PATCH] Update LKG --- lib/tsc.js | 38 ++++++++++++++++++---- lib/tsserver.js | 41 +++++++++++++++++++---- lib/tsserverlibrary.d.ts | 8 +++++ lib/tsserverlibrary.js | 41 +++++++++++++++++++---- lib/typescript.js | 68 ++++++++++++++++++++++++++++++++------- lib/typescriptServices.js | 68 ++++++++++++++++++++++++++++++++------- lib/typingsInstaller.js | 12 +++++++ 7 files changed, 234 insertions(+), 42 deletions(-) diff --git a/lib/tsc.js b/lib/tsc.js index e190108c5bc..bcaf43bad9e 100644 --- a/lib/tsc.js +++ b/lib/tsc.js @@ -966,6 +966,17 @@ var ts; }; } ts.createCompilerDiagnostic = createCompilerDiagnostic; + function createCompilerDiagnosticFromMessageChain(chain) { + return { + file: undefined, + start: undefined, + length: undefined, + code: chain.code, + category: chain.category, + messageText: chain.next ? chain : chain.messageText + }; + } + ts.createCompilerDiagnosticFromMessageChain = createCompilerDiagnosticFromMessageChain; function chainDiagnosticMessages(details, message) { var text = getLocaleSpecificMessage(message); if (arguments.length > 2) { @@ -3041,6 +3052,7 @@ var ts; Implement_interface_on_reference: { code: 90005, category: ts.DiagnosticCategory.Message, key: "Implement_interface_on_reference_90005", message: "Implement interface on reference" }, Implement_interface_on_class: { code: 90006, category: ts.DiagnosticCategory.Message, key: "Implement_interface_on_class_90006", message: "Implement interface on class" }, Implement_inherited_abstract_class: { code: 90007, category: ts.DiagnosticCategory.Message, key: "Implement_inherited_abstract_class_90007", message: "Implement inherited abstract class" }, + Adding_a_tsconfig_json_file_will_help_organize_projects_that_contain_both_TypeScript_and_JavaScript_files_Learn_more_at_https_Colon_Slash_Slashaka_ms_Slashtsconfig: { code: 90009, category: ts.DiagnosticCategory.Error, key: "Adding_a_tsconfig_json_file_will_help_organize_projects_that_contain_both_TypeScript_and_JavaScript__90009", message: "Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig" }, }; })(ts || (ts = {})); var ts; @@ -20148,7 +20160,9 @@ var ts; else if (getObjectFlags(type) & 3 || type.flags & (16 | 16384)) { buildSymbolDisplay(type.symbol, writer, enclosingDeclaration, 793064, 0, nextFlags); } - else if (!(flags & 512) && ((getObjectFlags(type) & 16 && !type.target) || type.flags & 196608) && type.aliasSymbol && + else if (!(flags & 512) && + (getObjectFlags(type) & 16 && !type.target || type.flags & 196608) && + type.aliasSymbol && isSymbolAccessible(type.aliasSymbol, enclosingDeclaration, 793064, false).accessibility === 0) { var typeArguments = type.aliasTypeArguments; writeSymbolTypeReference(type.aliasSymbol, typeArguments, 0, typeArguments ? typeArguments.length : 0, nextFlags); @@ -20257,7 +20271,7 @@ var ts; } else if (ts.contains(symbolStack, symbol)) { var typeAlias = getTypeAliasForTypeLiteral(type); - if (typeAlias) { + if (typeAlias && !type.target) { buildSymbolDisplay(typeAlias, writer, enclosingDeclaration, 793064, 0, flags); } else { @@ -48026,10 +48040,20 @@ var ts; if (emitFileName) { var emitFilePath = ts.toPath(emitFileName, currentDirectory, getCanonicalFileName); if (filesByName.contains(emitFilePath)) { - createEmitBlockingDiagnostics(emitFileName, ts.Diagnostics.Cannot_write_file_0_because_it_would_overwrite_input_file); + if (options.noEmitOverwritenFiles && !options.out && !options.outDir && !options.outFile) { + blockEmittingOfFile(emitFileName); + } + else { + var chain_1; + if (!options.configFilePath) { + chain_1 = ts.chainDiagnosticMessages(undefined, ts.Diagnostics.Adding_a_tsconfig_json_file_will_help_organize_projects_that_contain_both_TypeScript_and_JavaScript_files_Learn_more_at_https_Colon_Slash_Slashaka_ms_Slashtsconfig); + } + chain_1 = ts.chainDiagnosticMessages(chain_1, ts.Diagnostics.Cannot_write_file_0_because_it_would_overwrite_input_file, emitFileName); + blockEmittingOfFile(emitFileName, ts.createCompilerDiagnosticFromMessageChain(chain_1)); + } } if (emitFilesSeen.contains(emitFilePath)) { - createEmitBlockingDiagnostics(emitFileName, ts.Diagnostics.Cannot_write_file_0_because_it_would_be_overwritten_by_multiple_input_files); + blockEmittingOfFile(emitFileName, ts.createCompilerDiagnostic(ts.Diagnostics.Cannot_write_file_0_because_it_would_be_overwritten_by_multiple_input_files, emitFileName)); } else { emitFilesSeen.set(emitFilePath, true); @@ -48037,9 +48061,11 @@ var ts; } } } - function createEmitBlockingDiagnostics(emitFileName, message) { + function blockEmittingOfFile(emitFileName, diag) { hasEmitBlockingDiagnostics.set(ts.toPath(emitFileName, currentDirectory, getCanonicalFileName), true); - programDiagnostics.add(ts.createCompilerDiagnostic(message, emitFileName)); + if (diag) { + programDiagnostics.add(diag); + } } } ts.createProgram = createProgram; diff --git a/lib/tsserver.js b/lib/tsserver.js index 75110bce98d..86952bdb027 100644 --- a/lib/tsserver.js +++ b/lib/tsserver.js @@ -971,6 +971,17 @@ var ts; }; } ts.createCompilerDiagnostic = createCompilerDiagnostic; + function createCompilerDiagnosticFromMessageChain(chain) { + return { + file: undefined, + start: undefined, + length: undefined, + code: chain.code, + category: chain.category, + messageText: chain.next ? chain : chain.messageText + }; + } + ts.createCompilerDiagnosticFromMessageChain = createCompilerDiagnosticFromMessageChain; function chainDiagnosticMessages(details, message) { var text = getLocaleSpecificMessage(message); if (arguments.length > 2) { @@ -3046,6 +3057,7 @@ var ts; Implement_interface_on_reference: { code: 90005, category: ts.DiagnosticCategory.Message, key: "Implement_interface_on_reference_90005", message: "Implement interface on reference" }, Implement_interface_on_class: { code: 90006, category: ts.DiagnosticCategory.Message, key: "Implement_interface_on_class_90006", message: "Implement interface on class" }, Implement_inherited_abstract_class: { code: 90007, category: ts.DiagnosticCategory.Message, key: "Implement_inherited_abstract_class_90007", message: "Implement inherited abstract class" }, + Adding_a_tsconfig_json_file_will_help_organize_projects_that_contain_both_TypeScript_and_JavaScript_files_Learn_more_at_https_Colon_Slash_Slashaka_ms_Slashtsconfig: { code: 90009, category: ts.DiagnosticCategory.Error, key: "Adding_a_tsconfig_json_file_will_help_organize_projects_that_contain_both_TypeScript_and_JavaScript__90009", message: "Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig" }, }; })(ts || (ts = {})); var ts; @@ -21653,7 +21665,9 @@ var ts; else if (getObjectFlags(type) & 3 || type.flags & (16 | 16384)) { buildSymbolDisplay(type.symbol, writer, enclosingDeclaration, 793064, 0, nextFlags); } - else if (!(flags & 512) && ((getObjectFlags(type) & 16 && !type.target) || type.flags & 196608) && type.aliasSymbol && + else if (!(flags & 512) && + (getObjectFlags(type) & 16 && !type.target || type.flags & 196608) && + type.aliasSymbol && isSymbolAccessible(type.aliasSymbol, enclosingDeclaration, 793064, false).accessibility === 0) { var typeArguments = type.aliasTypeArguments; writeSymbolTypeReference(type.aliasSymbol, typeArguments, 0, typeArguments ? typeArguments.length : 0, nextFlags); @@ -21762,7 +21776,7 @@ var ts; } else if (ts.contains(symbolStack, symbol)) { var typeAlias = getTypeAliasForTypeLiteral(type); - if (typeAlias) { + if (typeAlias && !type.target) { buildSymbolDisplay(typeAlias, writer, enclosingDeclaration, 793064, 0, flags); } else { @@ -49531,10 +49545,20 @@ var ts; if (emitFileName) { var emitFilePath = ts.toPath(emitFileName, currentDirectory, getCanonicalFileName); if (filesByName.contains(emitFilePath)) { - createEmitBlockingDiagnostics(emitFileName, ts.Diagnostics.Cannot_write_file_0_because_it_would_overwrite_input_file); + if (options.noEmitOverwritenFiles && !options.out && !options.outDir && !options.outFile) { + blockEmittingOfFile(emitFileName); + } + else { + var chain_1; + if (!options.configFilePath) { + chain_1 = ts.chainDiagnosticMessages(undefined, ts.Diagnostics.Adding_a_tsconfig_json_file_will_help_organize_projects_that_contain_both_TypeScript_and_JavaScript_files_Learn_more_at_https_Colon_Slash_Slashaka_ms_Slashtsconfig); + } + chain_1 = ts.chainDiagnosticMessages(chain_1, ts.Diagnostics.Cannot_write_file_0_because_it_would_overwrite_input_file, emitFileName); + blockEmittingOfFile(emitFileName, ts.createCompilerDiagnosticFromMessageChain(chain_1)); + } } if (emitFilesSeen.contains(emitFilePath)) { - createEmitBlockingDiagnostics(emitFileName, ts.Diagnostics.Cannot_write_file_0_because_it_would_be_overwritten_by_multiple_input_files); + blockEmittingOfFile(emitFileName, ts.createCompilerDiagnostic(ts.Diagnostics.Cannot_write_file_0_because_it_would_be_overwritten_by_multiple_input_files, emitFileName)); } else { emitFilesSeen.set(emitFilePath, true); @@ -49542,9 +49566,11 @@ var ts; } } } - function createEmitBlockingDiagnostics(emitFileName, message) { + function blockEmittingOfFile(emitFileName, diag) { hasEmitBlockingDiagnostics.set(ts.toPath(emitFileName, currentDirectory, getCanonicalFileName), true); - programDiagnostics.add(ts.createCompilerDiagnostic(message, emitFileName)); + if (diag) { + programDiagnostics.add(diag); + } } } ts.createProgram = createProgram; @@ -62510,6 +62536,9 @@ var ts; else if (hasExplicitListOfFiles) { this.compilerOptions.allowNonTsExtensions = true; } + if (this.projectKind === ProjectKind.Inferred) { + this.compilerOptions.noEmitOverwritenFiles = true; + } if (languageServiceEnabled) { this.enableLanguageService(); } diff --git a/lib/tsserverlibrary.d.ts b/lib/tsserverlibrary.d.ts index 5005e5831b7..5ff54e33d7b 100644 --- a/lib/tsserverlibrary.d.ts +++ b/lib/tsserverlibrary.d.ts @@ -2924,6 +2924,7 @@ declare namespace ts { moduleResolution?: ModuleResolutionKind; newLine?: NewLineKind; noEmit?: boolean; + noEmitOverwritenFiles?: boolean; noEmitHelpers?: boolean; noEmitOnError?: boolean; noErrorTruncation?: boolean; @@ -3445,6 +3446,7 @@ declare namespace ts { function createFileDiagnostic(file: SourceFile, start: number, length: number, message: DiagnosticMessage, ...args: (string | number)[]): Diagnostic; function formatMessage(_dummy: any, message: DiagnosticMessage): string; function createCompilerDiagnostic(message: DiagnosticMessage, ...args: (string | number)[]): Diagnostic; + function createCompilerDiagnosticFromMessageChain(chain: DiagnosticMessageChain): Diagnostic; function chainDiagnosticMessages(details: DiagnosticMessageChain, message: DiagnosticMessage, ...args: any[]): DiagnosticMessageChain; function concatenateDiagnosticMessageChains(headChain: DiagnosticMessageChain, tailChain: DiagnosticMessageChain): DiagnosticMessageChain; function compareValues(a: T, b: T): Comparison; @@ -8272,6 +8274,12 @@ declare namespace ts { key: string; message: string; }; + Adding_a_tsconfig_json_file_will_help_organize_projects_that_contain_both_TypeScript_and_JavaScript_files_Learn_more_at_https_Colon_Slash_Slashaka_ms_Slashtsconfig: { + code: number; + category: DiagnosticCategory; + key: string; + message: string; + }; }; } declare namespace ts { diff --git a/lib/tsserverlibrary.js b/lib/tsserverlibrary.js index e8ee1ffb7b7..28bd566bf5c 100644 --- a/lib/tsserverlibrary.js +++ b/lib/tsserverlibrary.js @@ -971,6 +971,17 @@ var ts; }; } ts.createCompilerDiagnostic = createCompilerDiagnostic; + function createCompilerDiagnosticFromMessageChain(chain) { + return { + file: undefined, + start: undefined, + length: undefined, + code: chain.code, + category: chain.category, + messageText: chain.next ? chain : chain.messageText + }; + } + ts.createCompilerDiagnosticFromMessageChain = createCompilerDiagnosticFromMessageChain; function chainDiagnosticMessages(details, message) { var text = getLocaleSpecificMessage(message); if (arguments.length > 2) { @@ -3046,6 +3057,7 @@ var ts; Implement_interface_on_reference: { code: 90005, category: ts.DiagnosticCategory.Message, key: "Implement_interface_on_reference_90005", message: "Implement interface on reference" }, Implement_interface_on_class: { code: 90006, category: ts.DiagnosticCategory.Message, key: "Implement_interface_on_class_90006", message: "Implement interface on class" }, Implement_inherited_abstract_class: { code: 90007, category: ts.DiagnosticCategory.Message, key: "Implement_inherited_abstract_class_90007", message: "Implement inherited abstract class" }, + Adding_a_tsconfig_json_file_will_help_organize_projects_that_contain_both_TypeScript_and_JavaScript_files_Learn_more_at_https_Colon_Slash_Slashaka_ms_Slashtsconfig: { code: 90009, category: ts.DiagnosticCategory.Error, key: "Adding_a_tsconfig_json_file_will_help_organize_projects_that_contain_both_TypeScript_and_JavaScript__90009", message: "Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig" }, }; })(ts || (ts = {})); var ts; @@ -21653,7 +21665,9 @@ var ts; else if (getObjectFlags(type) & 3 || type.flags & (16 | 16384)) { buildSymbolDisplay(type.symbol, writer, enclosingDeclaration, 793064, 0, nextFlags); } - else if (!(flags & 512) && ((getObjectFlags(type) & 16 && !type.target) || type.flags & 196608) && type.aliasSymbol && + else if (!(flags & 512) && + (getObjectFlags(type) & 16 && !type.target || type.flags & 196608) && + type.aliasSymbol && isSymbolAccessible(type.aliasSymbol, enclosingDeclaration, 793064, false).accessibility === 0) { var typeArguments = type.aliasTypeArguments; writeSymbolTypeReference(type.aliasSymbol, typeArguments, 0, typeArguments ? typeArguments.length : 0, nextFlags); @@ -21762,7 +21776,7 @@ var ts; } else if (ts.contains(symbolStack, symbol)) { var typeAlias = getTypeAliasForTypeLiteral(type); - if (typeAlias) { + if (typeAlias && !type.target) { buildSymbolDisplay(typeAlias, writer, enclosingDeclaration, 793064, 0, flags); } else { @@ -49531,10 +49545,20 @@ var ts; if (emitFileName) { var emitFilePath = ts.toPath(emitFileName, currentDirectory, getCanonicalFileName); if (filesByName.contains(emitFilePath)) { - createEmitBlockingDiagnostics(emitFileName, ts.Diagnostics.Cannot_write_file_0_because_it_would_overwrite_input_file); + if (options.noEmitOverwritenFiles && !options.out && !options.outDir && !options.outFile) { + blockEmittingOfFile(emitFileName); + } + else { + var chain_1; + if (!options.configFilePath) { + chain_1 = ts.chainDiagnosticMessages(undefined, ts.Diagnostics.Adding_a_tsconfig_json_file_will_help_organize_projects_that_contain_both_TypeScript_and_JavaScript_files_Learn_more_at_https_Colon_Slash_Slashaka_ms_Slashtsconfig); + } + chain_1 = ts.chainDiagnosticMessages(chain_1, ts.Diagnostics.Cannot_write_file_0_because_it_would_overwrite_input_file, emitFileName); + blockEmittingOfFile(emitFileName, ts.createCompilerDiagnosticFromMessageChain(chain_1)); + } } if (emitFilesSeen.contains(emitFilePath)) { - createEmitBlockingDiagnostics(emitFileName, ts.Diagnostics.Cannot_write_file_0_because_it_would_be_overwritten_by_multiple_input_files); + blockEmittingOfFile(emitFileName, ts.createCompilerDiagnostic(ts.Diagnostics.Cannot_write_file_0_because_it_would_be_overwritten_by_multiple_input_files, emitFileName)); } else { emitFilesSeen.set(emitFilePath, true); @@ -49542,9 +49566,11 @@ var ts; } } } - function createEmitBlockingDiagnostics(emitFileName, message) { + function blockEmittingOfFile(emitFileName, diag) { hasEmitBlockingDiagnostics.set(ts.toPath(emitFileName, currentDirectory, getCanonicalFileName), true); - programDiagnostics.add(ts.createCompilerDiagnostic(message, emitFileName)); + if (diag) { + programDiagnostics.add(diag); + } } } ts.createProgram = createProgram; @@ -62510,6 +62536,9 @@ var ts; else if (hasExplicitListOfFiles) { this.compilerOptions.allowNonTsExtensions = true; } + if (this.projectKind === ProjectKind.Inferred) { + this.compilerOptions.noEmitOverwritenFiles = true; + } if (languageServiceEnabled) { this.enableLanguageService(); } diff --git a/lib/typescript.js b/lib/typescript.js index 0aef63d6c67..83494513a5f 100644 --- a/lib/typescript.js +++ b/lib/typescript.js @@ -2148,6 +2148,17 @@ var ts; }; } ts.createCompilerDiagnostic = createCompilerDiagnostic; + function createCompilerDiagnosticFromMessageChain(chain) { + return { + file: undefined, + start: undefined, + length: undefined, + code: chain.code, + category: chain.category, + messageText: chain.next ? chain : chain.messageText + }; + } + ts.createCompilerDiagnosticFromMessageChain = createCompilerDiagnosticFromMessageChain; function chainDiagnosticMessages(details, message) { var text = getLocaleSpecificMessage(message); if (arguments.length > 2) { @@ -4411,6 +4422,7 @@ var ts; Implement_interface_on_reference: { code: 90005, category: ts.DiagnosticCategory.Message, key: "Implement_interface_on_reference_90005", message: "Implement interface on reference" }, Implement_interface_on_class: { code: 90006, category: ts.DiagnosticCategory.Message, key: "Implement_interface_on_class_90006", message: "Implement interface on class" }, Implement_inherited_abstract_class: { code: 90007, category: ts.DiagnosticCategory.Message, key: "Implement_inherited_abstract_class_90007", message: "Implement inherited abstract class" }, + Adding_a_tsconfig_json_file_will_help_organize_projects_that_contain_both_TypeScript_and_JavaScript_files_Learn_more_at_https_Colon_Slash_Slashaka_ms_Slashtsconfig: { code: 90009, category: ts.DiagnosticCategory.Error, key: "Adding_a_tsconfig_json_file_will_help_organize_projects_that_contain_both_TypeScript_and_JavaScript__90009", message: "Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig" }, }; })(ts || (ts = {})); /// @@ -24845,14 +24857,31 @@ var ts; // The specified symbol flags need to be reinterpreted as type flags buildSymbolDisplay(type.symbol, writer, enclosingDeclaration, 793064 /* Type */, 0 /* None */, nextFlags); } - else if (!(flags & 512 /* InTypeAlias */) && ((getObjectFlags(type) & 16 /* Anonymous */ && !type.target) || type.flags & 196608 /* UnionOrIntersection */) && type.aliasSymbol && + else if (!(flags & 512 /* InTypeAlias */) && + (getObjectFlags(type) & 16 /* Anonymous */ && !type.target || type.flags & 196608 /* UnionOrIntersection */) && + type.aliasSymbol && isSymbolAccessible(type.aliasSymbol, enclosingDeclaration, 793064 /* Type */, /*shouldComputeAliasesToMakeVisible*/ false).accessibility === 0 /* Accessible */) { - // We emit inferred type as type-alias at the current localtion if all the following is true - // the input type is has alias symbol that is accessible - // the input type is a union, intersection or anonymous type that is fully instantiated (if not we want to keep dive into) - // e.g.: export type Bar = () => [X, Y]; - // export type Foo = Bar; - // export const y = (x: Foo) => 1 // we want to emit as ...x: () => [any, string]) + // We emit inferred type as type-alias if type is not in type-alias declaration, existed accessible alias-symbol, type is anonymous or union or intersection. + // However, if the type is an anonymous type with type arguments, we need to perform additional check. + // 1) No type arguments, just emit type-alias as is + // 2) Existed type arguments, check if the type arguments full fill all type parameters of the alias-symbol by + // checking whether the target's aliasTypeArguments has the same size as type's aliasTypeArguments: + // i.e + // type Foo = { + // foo(): Foo + // }; + // function foo() { + // return {} as Foo; + // } + // Should be emitted as + // declare type Foo = { + // foo(): Foo; + // }; + // declare function foo(): Foo; + // Otherwise type-alias is point to another generic type-alias then don't write it using alias symbol + // export type Bar = () => [X, Y]; + // export type Foo = Bar; + // export const y = (x: Foo) => 1 // this should be emit as "export declare const y: (x: () => [any, string]) => number;" var typeArguments = type.aliasTypeArguments; writeSymbolTypeReference(type.aliasSymbol, typeArguments, 0, typeArguments ? typeArguments.length : 0, nextFlags); } @@ -24971,7 +25000,9 @@ var ts; else if (ts.contains(symbolStack, symbol)) { // If type is an anonymous type literal in a type alias declaration, use type alias name var typeAlias = getTypeAliasForTypeLiteral(type); - if (typeAlias) { + // We only want to use type-alias here if the typeAlias is not a generic one. (i.e it doesn't have a target type) + // If it is a generic type-alias just write out "any" + if (typeAlias && !type.target) { // The specified symbol flags need to be reinterpreted as type flags buildSymbolDisplay(typeAlias, writer, enclosingDeclaration, 793064 /* Type */, 0 /* None */, flags); } @@ -60182,12 +60213,23 @@ var ts; var emitFilePath = ts.toPath(emitFileName, currentDirectory, getCanonicalFileName); // Report error if the output overwrites input file if (filesByName.contains(emitFilePath)) { - createEmitBlockingDiagnostics(emitFileName, ts.Diagnostics.Cannot_write_file_0_because_it_would_overwrite_input_file); + if (options.noEmitOverwritenFiles && !options.out && !options.outDir && !options.outFile) { + blockEmittingOfFile(emitFileName); + } + else { + var chain_1; + if (!options.configFilePath) { + // The program is from either an inferred project or an external project + chain_1 = ts.chainDiagnosticMessages(/*details*/ undefined, ts.Diagnostics.Adding_a_tsconfig_json_file_will_help_organize_projects_that_contain_both_TypeScript_and_JavaScript_files_Learn_more_at_https_Colon_Slash_Slashaka_ms_Slashtsconfig); + } + chain_1 = ts.chainDiagnosticMessages(chain_1, ts.Diagnostics.Cannot_write_file_0_because_it_would_overwrite_input_file, emitFileName); + blockEmittingOfFile(emitFileName, ts.createCompilerDiagnosticFromMessageChain(chain_1)); + } } // Report error if multiple files write into same file if (emitFilesSeen.contains(emitFilePath)) { // Already seen the same emit file - report error - createEmitBlockingDiagnostics(emitFileName, ts.Diagnostics.Cannot_write_file_0_because_it_would_be_overwritten_by_multiple_input_files); + blockEmittingOfFile(emitFileName, ts.createCompilerDiagnostic(ts.Diagnostics.Cannot_write_file_0_because_it_would_be_overwritten_by_multiple_input_files, emitFileName)); } else { emitFilesSeen.set(emitFilePath, true); @@ -60195,9 +60237,11 @@ var ts; } } } - function createEmitBlockingDiagnostics(emitFileName, message) { + function blockEmittingOfFile(emitFileName, diag) { hasEmitBlockingDiagnostics.set(ts.toPath(emitFileName, currentDirectory, getCanonicalFileName), true); - programDiagnostics.add(ts.createCompilerDiagnostic(message, emitFileName)); + if (diag) { + programDiagnostics.add(diag); + } } } ts.createProgram = createProgram; diff --git a/lib/typescriptServices.js b/lib/typescriptServices.js index 0aef63d6c67..83494513a5f 100644 --- a/lib/typescriptServices.js +++ b/lib/typescriptServices.js @@ -2148,6 +2148,17 @@ var ts; }; } ts.createCompilerDiagnostic = createCompilerDiagnostic; + function createCompilerDiagnosticFromMessageChain(chain) { + return { + file: undefined, + start: undefined, + length: undefined, + code: chain.code, + category: chain.category, + messageText: chain.next ? chain : chain.messageText + }; + } + ts.createCompilerDiagnosticFromMessageChain = createCompilerDiagnosticFromMessageChain; function chainDiagnosticMessages(details, message) { var text = getLocaleSpecificMessage(message); if (arguments.length > 2) { @@ -4411,6 +4422,7 @@ var ts; Implement_interface_on_reference: { code: 90005, category: ts.DiagnosticCategory.Message, key: "Implement_interface_on_reference_90005", message: "Implement interface on reference" }, Implement_interface_on_class: { code: 90006, category: ts.DiagnosticCategory.Message, key: "Implement_interface_on_class_90006", message: "Implement interface on class" }, Implement_inherited_abstract_class: { code: 90007, category: ts.DiagnosticCategory.Message, key: "Implement_inherited_abstract_class_90007", message: "Implement inherited abstract class" }, + Adding_a_tsconfig_json_file_will_help_organize_projects_that_contain_both_TypeScript_and_JavaScript_files_Learn_more_at_https_Colon_Slash_Slashaka_ms_Slashtsconfig: { code: 90009, category: ts.DiagnosticCategory.Error, key: "Adding_a_tsconfig_json_file_will_help_organize_projects_that_contain_both_TypeScript_and_JavaScript__90009", message: "Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig" }, }; })(ts || (ts = {})); /// @@ -24845,14 +24857,31 @@ var ts; // The specified symbol flags need to be reinterpreted as type flags buildSymbolDisplay(type.symbol, writer, enclosingDeclaration, 793064 /* Type */, 0 /* None */, nextFlags); } - else if (!(flags & 512 /* InTypeAlias */) && ((getObjectFlags(type) & 16 /* Anonymous */ && !type.target) || type.flags & 196608 /* UnionOrIntersection */) && type.aliasSymbol && + else if (!(flags & 512 /* InTypeAlias */) && + (getObjectFlags(type) & 16 /* Anonymous */ && !type.target || type.flags & 196608 /* UnionOrIntersection */) && + type.aliasSymbol && isSymbolAccessible(type.aliasSymbol, enclosingDeclaration, 793064 /* Type */, /*shouldComputeAliasesToMakeVisible*/ false).accessibility === 0 /* Accessible */) { - // We emit inferred type as type-alias at the current localtion if all the following is true - // the input type is has alias symbol that is accessible - // the input type is a union, intersection or anonymous type that is fully instantiated (if not we want to keep dive into) - // e.g.: export type Bar = () => [X, Y]; - // export type Foo = Bar; - // export const y = (x: Foo) => 1 // we want to emit as ...x: () => [any, string]) + // We emit inferred type as type-alias if type is not in type-alias declaration, existed accessible alias-symbol, type is anonymous or union or intersection. + // However, if the type is an anonymous type with type arguments, we need to perform additional check. + // 1) No type arguments, just emit type-alias as is + // 2) Existed type arguments, check if the type arguments full fill all type parameters of the alias-symbol by + // checking whether the target's aliasTypeArguments has the same size as type's aliasTypeArguments: + // i.e + // type Foo = { + // foo(): Foo + // }; + // function foo() { + // return {} as Foo; + // } + // Should be emitted as + // declare type Foo = { + // foo(): Foo; + // }; + // declare function foo(): Foo; + // Otherwise type-alias is point to another generic type-alias then don't write it using alias symbol + // export type Bar = () => [X, Y]; + // export type Foo = Bar; + // export const y = (x: Foo) => 1 // this should be emit as "export declare const y: (x: () => [any, string]) => number;" var typeArguments = type.aliasTypeArguments; writeSymbolTypeReference(type.aliasSymbol, typeArguments, 0, typeArguments ? typeArguments.length : 0, nextFlags); } @@ -24971,7 +25000,9 @@ var ts; else if (ts.contains(symbolStack, symbol)) { // If type is an anonymous type literal in a type alias declaration, use type alias name var typeAlias = getTypeAliasForTypeLiteral(type); - if (typeAlias) { + // We only want to use type-alias here if the typeAlias is not a generic one. (i.e it doesn't have a target type) + // If it is a generic type-alias just write out "any" + if (typeAlias && !type.target) { // The specified symbol flags need to be reinterpreted as type flags buildSymbolDisplay(typeAlias, writer, enclosingDeclaration, 793064 /* Type */, 0 /* None */, flags); } @@ -60182,12 +60213,23 @@ var ts; var emitFilePath = ts.toPath(emitFileName, currentDirectory, getCanonicalFileName); // Report error if the output overwrites input file if (filesByName.contains(emitFilePath)) { - createEmitBlockingDiagnostics(emitFileName, ts.Diagnostics.Cannot_write_file_0_because_it_would_overwrite_input_file); + if (options.noEmitOverwritenFiles && !options.out && !options.outDir && !options.outFile) { + blockEmittingOfFile(emitFileName); + } + else { + var chain_1; + if (!options.configFilePath) { + // The program is from either an inferred project or an external project + chain_1 = ts.chainDiagnosticMessages(/*details*/ undefined, ts.Diagnostics.Adding_a_tsconfig_json_file_will_help_organize_projects_that_contain_both_TypeScript_and_JavaScript_files_Learn_more_at_https_Colon_Slash_Slashaka_ms_Slashtsconfig); + } + chain_1 = ts.chainDiagnosticMessages(chain_1, ts.Diagnostics.Cannot_write_file_0_because_it_would_overwrite_input_file, emitFileName); + blockEmittingOfFile(emitFileName, ts.createCompilerDiagnosticFromMessageChain(chain_1)); + } } // Report error if multiple files write into same file if (emitFilesSeen.contains(emitFilePath)) { // Already seen the same emit file - report error - createEmitBlockingDiagnostics(emitFileName, ts.Diagnostics.Cannot_write_file_0_because_it_would_be_overwritten_by_multiple_input_files); + blockEmittingOfFile(emitFileName, ts.createCompilerDiagnostic(ts.Diagnostics.Cannot_write_file_0_because_it_would_be_overwritten_by_multiple_input_files, emitFileName)); } else { emitFilesSeen.set(emitFilePath, true); @@ -60195,9 +60237,11 @@ var ts; } } } - function createEmitBlockingDiagnostics(emitFileName, message) { + function blockEmittingOfFile(emitFileName, diag) { hasEmitBlockingDiagnostics.set(ts.toPath(emitFileName, currentDirectory, getCanonicalFileName), true); - programDiagnostics.add(ts.createCompilerDiagnostic(message, emitFileName)); + if (diag) { + programDiagnostics.add(diag); + } } } ts.createProgram = createProgram; diff --git a/lib/typingsInstaller.js b/lib/typingsInstaller.js index 06fff6592e1..f4b9d66aef0 100644 --- a/lib/typingsInstaller.js +++ b/lib/typingsInstaller.js @@ -971,6 +971,17 @@ var ts; }; } ts.createCompilerDiagnostic = createCompilerDiagnostic; + function createCompilerDiagnosticFromMessageChain(chain) { + return { + file: undefined, + start: undefined, + length: undefined, + code: chain.code, + category: chain.category, + messageText: chain.next ? chain : chain.messageText + }; + } + ts.createCompilerDiagnosticFromMessageChain = createCompilerDiagnosticFromMessageChain; function chainDiagnosticMessages(details, message) { var text = getLocaleSpecificMessage(message); if (arguments.length > 2) { @@ -3046,6 +3057,7 @@ var ts; Implement_interface_on_reference: { code: 90005, category: ts.DiagnosticCategory.Message, key: "Implement_interface_on_reference_90005", message: "Implement interface on reference" }, Implement_interface_on_class: { code: 90006, category: ts.DiagnosticCategory.Message, key: "Implement_interface_on_class_90006", message: "Implement interface on class" }, Implement_inherited_abstract_class: { code: 90007, category: ts.DiagnosticCategory.Message, key: "Implement_inherited_abstract_class_90007", message: "Implement inherited abstract class" }, + Adding_a_tsconfig_json_file_will_help_organize_projects_that_contain_both_TypeScript_and_JavaScript_files_Learn_more_at_https_Colon_Slash_Slashaka_ms_Slashtsconfig: { code: 90009, category: ts.DiagnosticCategory.Error, key: "Adding_a_tsconfig_json_file_will_help_organize_projects_that_contain_both_TypeScript_and_JavaScript__90009", message: "Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig" }, }; })(ts || (ts = {})); var ts;