From d0282b75a112a211b7f5d92c62ebf0720bf9c16a Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Thu, 13 Jun 2019 14:22:37 -0700 Subject: [PATCH 01/97] Add test to verify when source changes --- .../unittests/tsserver/projectReferences.ts | 75 ++++++++++++++++++- 1 file changed, 73 insertions(+), 2 deletions(-) diff --git a/src/testRunner/unittests/tsserver/projectReferences.ts b/src/testRunner/unittests/tsserver/projectReferences.ts index 1b2e90bcd4f..eb3d83e2b98 100644 --- a/src/testRunner/unittests/tsserver/projectReferences.ts +++ b/src/testRunner/unittests/tsserver/projectReferences.ts @@ -169,6 +169,8 @@ fn5(); expectedResponse: Response; expectedResponseNoMap?: Response; expectedResponseNoDts?: Response; + requestDependencyChange?: Partial; + expectedResponseDependencyChange: Response; } function gotoDefintinionFromMainTs(fn: number): SessionAction { const textSpan = usageSpan(fn); @@ -200,6 +202,11 @@ fn5(); // To import declaration definitions: [{ file: mainTs.path, ...importSpan(fn) }], textSpan + }, + expectedResponseDependencyChange: { + // Definition on fn + 1 line + definitions: [{ file: dependencyTs.path, ...declarationSpan(fn + 1) }], + textSpan } }; } @@ -227,6 +234,8 @@ fn5(); function renameFromDependencyTs(fn: number): SessionAction { const defSpan = declarationSpan(fn); const { contextStart: _, contextEnd: _1, ...triggerSpan } = defSpan; + const defSpanPlusOne = declarationSpan(fn + 1); + const { contextStart: _2, contextEnd: _3, ...triggerSpanPlusOne } = defSpanPlusOne; return { reqName: "rename", request: { @@ -246,12 +255,30 @@ fn5(); locs: [ { file: dependencyTs.path, locs: [defSpan] } ] + }, + requestDependencyChange: { + command: protocol.CommandTypes.Rename, + arguments: { file: dependencyTs.path, ...triggerSpanPlusOne.start } + }, + expectedResponseDependencyChange: { + info: { + canRename: true, + fileToRename: undefined, + displayName: `fn${fn}`, + fullDisplayName: `"${dependecyLocation}/FnS".fn${fn}`, + kind: ScriptElementKind.functionElement, + kindModifiers: "export", + triggerSpan: triggerSpanPlusOne + }, + locs: [ + { file: dependencyTs.path, locs: [defSpanPlusOne] } + ] } }; } function renameFromDependencyTsWithBothProjectsOpen(fn: number): SessionAction { - const { reqName, request, expectedResponse } = renameFromDependencyTs(fn); + const { reqName, request, expectedResponse, expectedResponseDependencyChange, requestDependencyChange } = renameFromDependencyTs(fn); const { info, locs } = expectedResponse; return { reqName, @@ -271,7 +298,21 @@ fn5(); }, // Only dependency result expectedResponseNoMap: expectedResponse, - expectedResponseNoDts: expectedResponse + expectedResponseNoDts: expectedResponse, + requestDependencyChange, + expectedResponseDependencyChange: { + info: expectedResponseDependencyChange.info, + locs: [ + expectedResponseDependencyChange.locs[0], + { + file: mainTs.path, + locs: [ + importSpan(fn), + usageSpan(fn) + ] + } + ] + } }; } @@ -633,6 +674,36 @@ fn5(); verifyMainScenarioAndScriptInfoCollectionWithNoDts, /*noDts*/ true ); + + it("when defining project source changes", () => { + const { host, session } = openTsFile(); + + // First action + firstAction(session); + + // Make change, without rebuild of solution + if (contains(openInfos, dependencyTs.path)) { + session.executeCommandSeq({ + command: protocol.CommandTypes.Change, + arguments: { + file: dependencyTs.path, line: 1, offset: 1, endLine: 1, endOffset: 1, insertString: `function fooBar() { } +`} + }); + } + else { + host.writeFile(dependencyTs.path, `function fooBar() { } +${dependencyTs.content}`); + } + host.runQueuedTimeoutCallbacks(); + + for (const actionGetter of actionGetters) { + for (let fn = 1; fn <= 5; fn++) { + const { reqName, request, requestDependencyChange, expectedResponseDependencyChange } = actionGetter(fn); + const { response } = session.executeCommandSeq(requestDependencyChange || request); + assert.deepEqual(response, expectedResponseDependencyChange, `Failed on ${reqName}`); + } + } + }); } const usageVerifier: DocumentPositionMapperVerifier = { From 0adab8934aad26fb3a5883b64f1198943700c0f2 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Fri, 21 Jun 2019 13:11:39 -0700 Subject: [PATCH 02/97] Use source files instead of .d.ts files from project references --- src/compiler/program.ts | 86 +++++++++++++++---- src/compiler/types.ts | 9 ++ src/server/editorServices.ts | 4 +- src/server/project.ts | 26 ++++++ src/services/services.ts | 6 ++ src/services/types.ts | 4 + .../reference/api/tsserverlibrary.d.ts | 2 + 7 files changed, 119 insertions(+), 18 deletions(-) diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 2b62da72d24..27bd1fd3d3b 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -813,6 +813,8 @@ namespace ts { let resolvedProjectReferences: ReadonlyArray | undefined; let projectReferenceRedirects: Map | undefined; let mapFromFileToProjectReferenceRedirects: Map | undefined; + let mapFromToProjectReferenceRedirectSource: Map | undefined; + const useSourceOfReference = host.useSourceInsteadOfReferenceRedirect && host.useSourceInsteadOfReferenceRedirect(); const shouldCreateNewSourceFile = shouldProgramCreateNewSourceFiles(oldProgram, options); const structuralIsReused = tryReuseStructureFromOldProgram(); @@ -824,17 +826,29 @@ namespace ts { if (!resolvedProjectReferences) { resolvedProjectReferences = projectReferences.map(parseProjectReferenceConfigFile); } + if (host.setGetSourceOfProjectReferenceRedirect) { + host.setGetSourceOfProjectReferenceRedirect(getSourceOfProjectReferenceRedirect); + } if (rootNames.length) { for (const parsedRef of resolvedProjectReferences) { if (!parsedRef) continue; const out = parsedRef.commandLine.options.outFile || parsedRef.commandLine.options.out; - if (out) { - processSourceFile(changeExtension(out, ".d.ts"), /*isDefaultLib*/ false, /*ignoreNoDefaultLib*/ false, /*packageId*/ undefined); + if (useSourceOfReference) { + if (out || getEmitModuleKind(parsedRef.commandLine.options) === ModuleKind.None) { + for (const fileName of parsedRef.commandLine.fileNames) { + processSourceFile(fileName, /*isDefaultLib*/ false, /*ignoreNoDefaultLib*/ false, /*packageId*/ undefined); + } + } } - else if (getEmitModuleKind(parsedRef.commandLine.options) === ModuleKind.None) { - for (const fileName of parsedRef.commandLine.fileNames) { - if (!fileExtensionIs(fileName, Extension.Dts) && hasTSFileExtension(fileName)) { - processSourceFile(getOutputDeclarationFileName(fileName, parsedRef.commandLine, !host.useCaseSensitiveFileNames()), /*isDefaultLib*/ false, /*ignoreNoDefaultLib*/ false, /*packageId*/ undefined); + else { + if (out) { + processSourceFile(changeExtension(out, ".d.ts"), /*isDefaultLib*/ false, /*ignoreNoDefaultLib*/ false, /*packageId*/ undefined); + } + else if (getEmitModuleKind(parsedRef.commandLine.options) === ModuleKind.None) { + for (const fileName of parsedRef.commandLine.fileNames) { + if (!fileExtensionIs(fileName, Extension.Dts) && hasTSFileExtension(fileName)) { + processSourceFile(getOutputDeclarationFileName(fileName, parsedRef.commandLine, !host.useCaseSensitiveFileNames()), /*isDefaultLib*/ false, /*ignoreNoDefaultLib*/ false, /*packageId*/ undefined); + } } } } @@ -1212,6 +1226,9 @@ namespace ts { } if (projectReferences) { resolvedProjectReferences = projectReferences.map(parseProjectReferenceConfigFile); + if (host.setGetSourceOfProjectReferenceRedirect) { + host.setGetSourceOfProjectReferenceRedirect(getSourceOfProjectReferenceRedirect); + } } // check if program source files has changed in the way that can affect structure of the program @@ -2220,6 +2237,14 @@ namespace ts { // Get source file from normalized fileName function findSourceFile(fileName: string, path: Path, isDefaultLib: boolean, ignoreNoDefaultLib: boolean, refFile: SourceFile, refPos: number, refEnd: number, packageId: PackageId | undefined): SourceFile | undefined { + if (useSourceOfReference) { + const source = getSourceOfProjectReferenceRedirect(fileName); + if (source) { + return isString(source) ? + findSourceFile(source, toPath(source), isDefaultLib, ignoreNoDefaultLib, refFile, refPos, refEnd, packageId) : + undefined; + } + } const originalFileName = fileName; if (filesByName.has(path)) { const file = filesByName.get(path); @@ -2267,7 +2292,7 @@ namespace ts { } let redirectedPath: Path | undefined; - if (refFile) { + if (refFile && !useSourceOfReference) { const redirectProject = getProjectReferenceRedirectProject(fileName); if (redirectProject) { if (redirectProject.commandLine.options.outFile || redirectProject.commandLine.options.out) { @@ -2286,15 +2311,20 @@ namespace ts { } // We haven't looked for this file, do so now and cache result - const file = host.getSourceFile(fileName, options.target!, hostErrorMessage => { // TODO: GH#18217 - if (refFile !== undefined && refPos !== undefined && refEnd !== undefined) { - fileProcessingDiagnostics.add(createFileDiagnostic(refFile, refPos, refEnd - refPos, - Diagnostics.Cannot_read_file_0_Colon_1, fileName, hostErrorMessage)); - } - else { - fileProcessingDiagnostics.add(createCompilerDiagnostic(Diagnostics.Cannot_read_file_0_Colon_1, fileName, hostErrorMessage)); - } - }, shouldCreateNewSourceFile); + const file = host.getSourceFile( + fileName, + options.target!, + hostErrorMessage => { // TODO: GH#18217 + if (refFile !== undefined && refPos !== undefined && refEnd !== undefined) { + fileProcessingDiagnostics.add(createFileDiagnostic(refFile, refPos, refEnd - refPos, + Diagnostics.Cannot_read_file_0_Colon_1, fileName, hostErrorMessage)); + } + else { + fileProcessingDiagnostics.add(createCompilerDiagnostic(Diagnostics.Cannot_read_file_0_Colon_1, fileName, hostErrorMessage)); + } + }, + shouldCreateNewSourceFile + ); if (packageId) { const packageIdKey = packageIdToString(packageId); @@ -2424,6 +2454,30 @@ namespace ts { }); } + function getSourceOfProjectReferenceRedirect(file: string) { + if (!isDeclarationFileName(file)) return undefined; + if (mapFromToProjectReferenceRedirectSource === undefined) { + mapFromToProjectReferenceRedirectSource = createMap(); + forEachResolvedProjectReference(resolvedRef => { + if (resolvedRef) { + const out = resolvedRef.commandLine.options.outFile || resolvedRef.commandLine.options.out; + if (out) { + // Dont know which source file it means so return true? + const outputDts = changeExtension(out, Extension.Dts); + mapFromToProjectReferenceRedirectSource!.set(toPath(outputDts), true); + } + else { + forEach(resolvedRef.commandLine.fileNames, fileName => { + const outputDts = getOutputDeclarationFileName(fileName, resolvedRef.commandLine, host.useCaseSensitiveFileNames()); + mapFromToProjectReferenceRedirectSource!.set(toPath(outputDts), fileName); + }); + } + } + }); + } + return mapFromToProjectReferenceRedirectSource.get(toPath(file)); + } + function forEachProjectReference( projectReferences: ReadonlyArray | undefined, resolvedProjectReferences: ReadonlyArray | undefined, diff --git a/src/compiler/types.ts b/src/compiler/types.ts index ec46da039d9..fc24088e0fa 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -5166,11 +5166,20 @@ namespace ts { /* @internal */ hasChangedAutomaticTypeDirectiveNames?: boolean; createHash?(data: string): string; getParsedCommandLine?(fileName: string): ParsedCommandLine | undefined; + /* @internal */ setGetSourceOfProjectReferenceRedirect?(getSource: GetSourceOfProjectReferenceRedirect): void; + /* @internal */ useSourceInsteadOfReferenceRedirect?(): boolean; // TODO: later handle this in better way in builder host instead once the api for tsbuild finalizes and doesn't use compilerHost as base /*@internal*/createDirectory?(directory: string): void; } + /** true if --out otherwise source file name */ + /*@internal*/ + export type SourceOfProjectReferenceRedirect = string | true ; + + /*@internal*/ + export type GetSourceOfProjectReferenceRedirect = (fileName: string) => SourceOfProjectReferenceRedirect | undefined; + /* @internal */ export const enum TransformFlags { None = 0, diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index 4e5435feaba..f7fb537f543 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -1798,7 +1798,7 @@ namespace ts.server { let scriptInfo: ScriptInfo | NormalizedPath; let path: Path; // Use the project's fileExists so that it can use caching instead of reaching to disk for the query - if (!isDynamic && !project.fileExists(newRootFile)) { + if (!isDynamic && !project.fileExistsWithCache(newRootFile)) { path = normalizedPathToPath(normalizedPath, this.currentDirectory, this.toCanonicalFileName); const existingValue = projectRootFilesMap.get(path)!; if (isScriptInfo(existingValue)) { @@ -1831,7 +1831,7 @@ namespace ts.server { projectRootFilesMap.forEach((value, path) => { if (!newRootScriptInfoMap.has(path)) { if (isScriptInfo(value)) { - project.removeFile(value, project.fileExists(path), /*detachFromProject*/ true); + project.removeFile(value, project.fileExistsWithCache(path), /*detachFromProject*/ true); } else { projectRootFilesMap.delete(path); diff --git a/src/server/project.ts b/src/server/project.ts index d1605e22d1d..3e69a785516 100644 --- a/src/server/project.ts +++ b/src/server/project.ts @@ -381,6 +381,11 @@ namespace ts.server { } fileExists(file: string): boolean { + return this.fileExistsWithCache(file); + } + + /* @internal */ + fileExistsWithCache(file: string): boolean { // As an optimization, don't hit the disks for files we already know don't exist // (because we're watching for their creation). const path = this.toPath(file); @@ -1369,6 +1374,7 @@ namespace ts.server { configFileWatcher: FileWatcher | undefined; private directoriesWatchedForWildcards: Map | undefined; readonly canonicalConfigFilePath: NormalizedPath; + private getSourceOfProjectReferenceRedirect: GetSourceOfProjectReferenceRedirect | undefined; /* @internal */ pendingReload: ConfigFileProgramReloadLevel | undefined; @@ -1414,6 +1420,25 @@ namespace ts.server { this.canonicalConfigFilePath = asNormalizedPath(projectService.toCanonicalFileName(configFileName)); } + /* @internal */ + setGetSourceOfProjectReferenceRedirect(getSource: GetSourceOfProjectReferenceRedirect) { + this.getSourceOfProjectReferenceRedirect = getSource; + } + + /* @internal */ + useSourceInsteadOfReferenceRedirect() { + return true; + } + + fileExists(file: string): boolean { + // Project references go to source file instead of .d.ts file + if (this.getSourceOfProjectReferenceRedirect) { + const source = this.getSourceOfProjectReferenceRedirect(file); + if (source) return isString(source) ? super.fileExists(source) : true; + } + return super.fileExists(file); + } + /** * If the project has reload from disk pending, it reloads (and then updates graph as part of that) instead of just updating the graph * @returns: true if set of files in the project stays the same and false - otherwise. @@ -1436,6 +1461,7 @@ namespace ts.server { default: result = super.updateGraph(); } + this.getSourceOfProjectReferenceRedirect = undefined; this.projectService.sendProjectLoadingFinishEvent(this); this.projectService.sendProjectTelemetry(this); return result; diff --git a/src/services/services.ts b/src/services/services.ts index fab6f88b779..a8b05402e76 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1245,6 +1245,12 @@ namespace ts { return host.resolveTypeReferenceDirectives!(typeReferenceDirectiveNames, containingFile, redirectedReference); }; } + if (host.setGetSourceOfProjectReferenceRedirect) { + compilerHost.setGetSourceOfProjectReferenceRedirect = getSource => host.setGetSourceOfProjectReferenceRedirect!(getSource); + } + if (host.useSourceInsteadOfReferenceRedirect) { + compilerHost.useSourceInsteadOfReferenceRedirect = () => host.useSourceInsteadOfReferenceRedirect!(); + } const documentRegistryBucketKey = documentRegistry.getKeyForCompilationSettings(newSettings); const options: CreateProgramOptions = { diff --git a/src/services/types.ts b/src/services/types.ts index b97125734f7..3c9509ca5e9 100644 --- a/src/services/types.ts +++ b/src/services/types.ts @@ -236,6 +236,10 @@ namespace ts { getDocumentPositionMapper?(generatedFileName: string, sourceFileName?: string): DocumentPositionMapper | undefined; /* @internal */ getSourceFileLike?(fileName: string): SourceFileLike | undefined; + /* @internal */ + setGetSourceOfProjectReferenceRedirect?(getSource: GetSourceOfProjectReferenceRedirect): void; + /* @internal */ + useSourceInsteadOfReferenceRedirect?(): boolean; } /* @internal */ diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 82ab70b6e6f..53459d993cf 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -8546,11 +8546,13 @@ declare namespace ts.server { private typeAcquisition; private directoriesWatchedForWildcards; readonly canonicalConfigFilePath: NormalizedPath; + private getSourceOfProjectReferenceRedirect; /** Ref count to the project when opened from external project */ private externalProjectRefCount; private projectErrors; private projectReferences; protected isInitialLoadPending: () => boolean; + fileExists(file: string): boolean; /** * If the project has reload from disk pending, it reloads (and then updates graph as part of that) instead of just updating the graph * @returns: true if set of files in the project stays the same and false - otherwise. From c97be16fa192e4c6ce04e5867b0a9e042cfdb392 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Fri, 21 Jun 2019 13:54:06 -0700 Subject: [PATCH 03/97] Log the config of the project --- src/server/editorServices.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index f7fb537f543..1b2666d4cba 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -1757,6 +1757,12 @@ namespace ts.server { configFileErrors.push(...parsedCommandLine.errors); } + this.logger.info(`Config: ${configFilename} : ${JSON.stringify({ + rootNames: parsedCommandLine.fileNames, + options: parsedCommandLine.options, + projectReferences: parsedCommandLine.projectReferences + }, /*replacer*/ undefined, " ")}`); + Debug.assert(!!parsedCommandLine.fileNames); const compilerOptions = parsedCommandLine.options; From 746b01e5772d5eda7f71a72265c5fee3a3a6ba8e Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Fri, 21 Jun 2019 14:16:40 -0700 Subject: [PATCH 04/97] Check only for .d.ts files --- src/compiler/program.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 27bd1fd3d3b..6fee35d2448 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -2468,8 +2468,10 @@ namespace ts { } else { forEach(resolvedRef.commandLine.fileNames, fileName => { - const outputDts = getOutputDeclarationFileName(fileName, resolvedRef.commandLine, host.useCaseSensitiveFileNames()); - mapFromToProjectReferenceRedirectSource!.set(toPath(outputDts), fileName); + if (!fileExtensionIs(fileName, Extension.Dts) && hasTSFileExtension(fileName)) { + const outputDts = getOutputDeclarationFileName(fileName, resolvedRef.commandLine, host.useCaseSensitiveFileNames()); + mapFromToProjectReferenceRedirectSource!.set(toPath(outputDts), fileName); + } }); } } From ecf875112b7a7faea715a60ac1ee4da8c201bb38 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Fri, 21 Jun 2019 14:22:11 -0700 Subject: [PATCH 05/97] Check for language serivice enabled when including source files --- src/server/project.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/server/project.ts b/src/server/project.ts index 3e69a785516..3b721c14b58 100644 --- a/src/server/project.ts +++ b/src/server/project.ts @@ -1427,12 +1427,12 @@ namespace ts.server { /* @internal */ useSourceInsteadOfReferenceRedirect() { - return true; + return !!this.languageServiceEnabled; } fileExists(file: string): boolean { // Project references go to source file instead of .d.ts file - if (this.getSourceOfProjectReferenceRedirect) { + if (this.languageServiceEnabled && this.getSourceOfProjectReferenceRedirect) { const source = this.getSourceOfProjectReferenceRedirect(file); if (source) return isString(source) ? super.fileExists(source) : true; } From 181028821ba5600324b13b645f93b098747f6d5a Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Fri, 21 Jun 2019 14:54:38 -0700 Subject: [PATCH 06/97] Fix tests --- src/testRunner/unittests/tsserver/projectReferences.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/testRunner/unittests/tsserver/projectReferences.ts b/src/testRunner/unittests/tsserver/projectReferences.ts index eb3d83e2b98..d5a67252824 100644 --- a/src/testRunner/unittests/tsserver/projectReferences.ts +++ b/src/testRunner/unittests/tsserver/projectReferences.ts @@ -85,8 +85,8 @@ namespace ts.projectSystem { }); const { file: _, ...renameTextOfMyConstInLib } = locationOfMyConstInLib; assert.deepEqual(response.locs, [ - { file: myConstFile, locs: [{ start: myConstStart, end: myConstEnd }] }, - { file: locationOfMyConstInLib.file, locs: [renameTextOfMyConstInLib] } + { file: locationOfMyConstInLib.file, locs: [renameTextOfMyConstInLib] }, + { file: myConstFile, locs: [{ start: myConstStart, end: myConstEnd }] } ]); }); }); From f4728682b7f6a8275b034fb9be5a2f64b84e0aea Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Tue, 25 Jun 2019 12:15:04 -0700 Subject: [PATCH 07/97] Watch generated file if it doesnt exist when trying to translate it to to source generated position --- src/server/editorServices.ts | 8 +- src/server/project.ts | 100 +++++++++ src/server/utilities.ts | 1 + .../unittests/tsserver/projectReferences.ts | 200 ++++++++++-------- .../reference/api/tsserverlibrary.d.ts | 4 + 5 files changed, 229 insertions(+), 84 deletions(-) diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index 1b2666d4cba..8c7462d857b 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -2240,7 +2240,13 @@ namespace ts.server { getDocumentPositionMapper(project: Project, generatedFileName: string, sourceFileName?: string): DocumentPositionMapper | undefined { // Since declaration info and map file watches arent updating project's directory structure host (which can cache file structure) use host const declarationInfo = this.getOrCreateScriptInfoNotOpenedByClient(generatedFileName, project.currentDirectory, this.host); - if (!declarationInfo) return undefined; + if (!declarationInfo) { + if (sourceFileName) { + // Project contains source file and it generates the generated file name + project.addGeneratedFileWatch(generatedFileName, sourceFileName); + } + return undefined; + } // Try to get from cache declarationInfo.getSnapshot(); // Ensure synchronized diff --git a/src/server/project.ts b/src/server/project.ts index 3b721c14b58..8f8e6e5da8e 100644 --- a/src/server/project.ts +++ b/src/server/project.ts @@ -109,12 +109,22 @@ namespace ts.server { return value instanceof ScriptInfo; } + interface GeneratedFileWatcher { + generatedFilePath: Path; + watcher: FileWatcher; + } + type GeneratedFileWatcherMap = GeneratedFileWatcher | Map; + function isGeneratedFileWatcher(watch: GeneratedFileWatcherMap): watch is GeneratedFileWatcher { + return (watch as GeneratedFileWatcher).generatedFilePath !== undefined; + } + export abstract class Project implements LanguageServiceHost, ModuleResolutionHost { private rootFiles: ScriptInfo[] = []; private rootFilesMap: Map = createMap(); private program: Program | undefined; private externalFiles: SortedReadonlyArray | undefined; private missingFilesMap: Map | undefined; + private generatedFilesMap: GeneratedFileWatcherMap | undefined; private plugins: PluginModuleWithName[] = []; /*@internal*/ @@ -573,6 +583,7 @@ namespace ts.server { this.lastFileExceededProgramSize = lastFileExceededProgramSize; this.builderState = undefined; this.resolutionCache.closeTypeRootsWatch(); + this.clearGeneratedFileWatch(); this.projectService.onUpdateLanguageServiceStateForProject(this, /*languageServiceEnabled*/ false); } @@ -654,6 +665,7 @@ namespace ts.server { clearMap(this.missingFilesMap, closeFileWatcher); this.missingFilesMap = undefined!; } + this.clearGeneratedFileWatch(); // signal language service to release source files acquired from document registry this.languageService.dispose(); @@ -947,6 +959,39 @@ namespace ts.server { missingFilePath => this.addMissingFileWatcher(missingFilePath) ); + if (this.generatedFilesMap) { + const outPath = this.compilerOptions.outFile && this.compilerOptions.out; + if (isGeneratedFileWatcher(this.generatedFilesMap)) { + // --out + if (!outPath || !this.isValidGeneratedFileWatcher( + removeFileExtension(outPath) + Extension.Dts, + this.generatedFilesMap, + )) { + this.clearGeneratedFileWatch(); + } + } + else { + // MultiFile + if (outPath) { + this.clearGeneratedFileWatch(); + } + else { + this.generatedFilesMap.forEach((watcher, source) => { + const sourceFile = this.program!.getSourceFileByPath(source as Path); + if (!sourceFile || + sourceFile.resolvedPath !== source || + !this.isValidGeneratedFileWatcher( + getDeclarationEmitOutputFilePathWorker(sourceFile.fileName, this.compilerOptions, this.currentDirectory, this.program!.getCommonSourceDirectory(), this.getCanonicalFileName), + watcher + )) { + closeFileWatcherOf(watcher); + (this.generatedFilesMap as Map).delete(source); + } + }); + } + } + } + // Watch the type locations that would be added to program as part of automatic type resolutions if (this.languageServiceEnabled) { this.resolutionCache.updateTypeRootsWatch(); @@ -1011,6 +1056,61 @@ namespace ts.server { return !!this.missingFilesMap && this.missingFilesMap.has(path); } + /* @internal */ + addGeneratedFileWatch(generatedFile: string, sourceFile: string) { + if (this.compilerOptions.outFile || this.compilerOptions.out) { + // Single watcher + if (!this.generatedFilesMap) { + this.generatedFilesMap = this.createGeneratedFileWatcher(generatedFile); + } + } + else { + // Map + const path = this.toPath(sourceFile); + if (this.generatedFilesMap) { + if (isGeneratedFileWatcher(this.generatedFilesMap)) { + Debug.fail(`${this.projectName} Expected not to have --out watcher for generated file with options: ${JSON.stringify(this.compilerOptions)}`); + return; + } + if (this.generatedFilesMap.has(path)) return; + } + else { + this.generatedFilesMap = createMap(); + } + this.generatedFilesMap.set(path, this.createGeneratedFileWatcher(generatedFile)); + } + } + + private createGeneratedFileWatcher(generatedFile: string): GeneratedFileWatcher { + return { + generatedFilePath: this.toPath(generatedFile), + watcher: this.projectService.watchFactory.watchFile( + this.projectService.host, + generatedFile, + () => this.projectService.delayUpdateProjectGraphAndEnsureProjectStructureForOpenFiles(this), + PollingInterval.High, + WatchType.MissingGeneratedFile, + this + ) + }; + } + + private isValidGeneratedFileWatcher(generateFile: string, watcher: GeneratedFileWatcher) { + return this.toPath(generateFile) === watcher.generatedFilePath; + } + + private clearGeneratedFileWatch() { + if (this.generatedFilesMap) { + if (isGeneratedFileWatcher(this.generatedFilesMap)) { + closeFileWatcherOf(this.generatedFilesMap); + } + else { + clearMap(this.generatedFilesMap, closeFileWatcherOf); + } + this.generatedFilesMap = undefined; + } + } + getScriptInfoForNormalizedPath(fileName: NormalizedPath): ScriptInfo | undefined { const scriptInfo = this.projectService.getScriptInfoForPath(this.toPath(fileName)); if (scriptInfo && !scriptInfo.isAttached(this)) { diff --git a/src/server/utilities.ts b/src/server/utilities.ts index 91880ccfdbe..fd38c6a4326 100644 --- a/src/server/utilities.ts +++ b/src/server/utilities.ts @@ -227,5 +227,6 @@ namespace ts { NodeModulesForClosedScriptInfo = "node_modules for closed script infos in them", MissingSourceMapFile = "Missing source map file", NoopConfigFileForInferredRoot = "Noop Config file for the inferred project root", + MissingGeneratedFile = "Missing generated file" } } diff --git a/src/testRunner/unittests/tsserver/projectReferences.ts b/src/testRunner/unittests/tsserver/projectReferences.ts index d5a67252824..9c02b670bb7 100644 --- a/src/testRunner/unittests/tsserver/projectReferences.ts +++ b/src/testRunner/unittests/tsserver/projectReferences.ts @@ -94,6 +94,7 @@ namespace ts.projectSystem { describe("with main and depedency project", () => { const projectLocation = "/user/username/projects/myproject"; const dependecyLocation = `${projectLocation}/dependency`; + const dependecyDeclsLocation = `${projectLocation}/decls`; const mainLocation = `${projectLocation}/main`; const dependencyTs: File = { path: `${dependecyLocation}/FnS.ts`, @@ -106,7 +107,7 @@ export function fn5() { } }; const dependencyConfig: File = { path: `${dependecyLocation}/tsconfig.json`, - content: JSON.stringify({ compilerOptions: { composite: true, declarationMap: true } }) + content: JSON.stringify({ compilerOptions: { composite: true, declarationMap: true, declarationDir: "../decls" } }) }; const mainTs: File = { @@ -117,7 +118,7 @@ export function fn5() { } fn3, fn4, fn5 -} from '../dependency/fns' +} from '../decls/fns' fn1(); fn2(); @@ -142,9 +143,9 @@ fn5(); path: `${projectLocation}/random/tsconfig.json`, content: "{}" }; - const dtsLocation = `${dependecyLocation}/FnS.d.ts`; + const dtsLocation = `${dependecyDeclsLocation}/FnS.d.ts`; const dtsPath = dtsLocation.toLowerCase() as Path; - const dtsMapLocation = `${dtsLocation}.map`; + const dtsMapLocation = `${dependecyDeclsLocation}/FnS.d.ts.map`; const dtsMapPath = dtsMapLocation.toLowerCase() as Path; const files = [dependencyTs, dependencyConfig, mainTs, mainConfig, libFile, randomFile, randomConfig]; @@ -224,7 +225,7 @@ fn5(); start: { line: fn + 1, offset: 5 }, end: { line: fn + 1, offset: 8 }, contextStart: { line: 1, offset: 1 }, - contextEnd: { line: 7, offset: 27 } + contextEnd: { line: 7, offset: 22 } }; } function usageSpan(fn: number): protocol.TextSpan { @@ -328,19 +329,25 @@ fn5(); function verifyDocumentPositionMapperUpdates( mainScenario: string, verifier: ReadonlyArray, - closedInfos: ReadonlyArray) { + closedInfos: ReadonlyArray, + withRefs: boolean) { const openFiles = verifier.map(v => v.openFile); const expectedProjectActualFiles = verifier.map(v => v.expectedProjectActualFiles); - const actionGetters = verifier.map(v => v.actionGetter); const openFileLastLines = verifier.map(v => v.openFileLastLine); const configFiles = openFiles.map(openFile => `${getDirectoryPath(openFile.path)}/tsconfig.json`); const openInfos = openFiles.map(f => f.path); // When usage and dependency are used, dependency config is part of closedInfo so ignore - const otherWatchedFiles = verifier.length > 1 ? [configFiles[0]] : configFiles; + const otherWatchedFiles = withRefs && verifier.length > 1 ? [configFiles[0]] : configFiles; function openTsFile(onHostCreate?: (host: TestServerHost) => void) { const host = createHost(files, [mainConfig.path]); + if (!withRefs) { + // Erase project reference + host.writeFile(mainConfig.path, JSON.stringify({ + compilerOptions: { composite: true, declarationMap: true } + })); + } if (onHostCreate) { onHostCreate(host); } @@ -377,7 +384,7 @@ fn5(); ); } - function verifyInfosWhenNoDtsFile(session: TestSession, host: TestServerHost, dependencyTsAndMapOk?: true) { + function verifyInfosWhenNoDtsFile(session: TestSession, host: TestServerHost, watchDts: boolean, dependencyTsAndMapOk?: true) { const dtsMapClosedInfo = firstDefined(closedInfos, f => f.toLowerCase() === dtsMapPath ? f : undefined); const dtsClosedInfo = firstDefined(closedInfos, f => f.toLowerCase() === dtsPath ? f : undefined); verifyInfosWithRandom( @@ -385,8 +392,7 @@ fn5(); host, openInfos, closedInfos.filter(f => (dependencyTsAndMapOk || f !== dtsMapClosedInfo) && f !== dtsClosedInfo && (dependencyTsAndMapOk || f !== dependencyTs.path)), - // When project actual file contains dts, it needs to be watched - dtsClosedInfo && expectedProjectActualFiles.some(expectedProjectActualFiles => expectedProjectActualFiles.some(f => f.toLowerCase() === dtsPath)) ? + dtsClosedInfo && watchDts ? otherWatchedFiles.concat(dtsClosedInfo) : otherWatchedFiles ); @@ -402,22 +408,22 @@ fn5(); } } - function action(actionGetter: SessionActionGetter, fn: number, session: TestSession) { - const { reqName, request, expectedResponse, expectedResponseNoMap, expectedResponseNoDts } = actionGetter(fn); + function action(verifier: DocumentPositionMapperVerifier, fn: number, session: TestSession) { + const { reqName, request, expectedResponse, expectedResponseNoMap, expectedResponseNoDts } = verifier.actionGetter(fn); const { response } = session.executeCommandSeq(request); - return { reqName, response, expectedResponse, expectedResponseNoMap, expectedResponseNoDts }; + return { reqName, response, expectedResponse, expectedResponseNoMap, expectedResponseNoDts, verifier }; } function firstAction(session: TestSession) { - actionGetters.forEach(actionGetter => action(actionGetter, 1, session)); + verifier.forEach(v => action(v, 1, session)); } function verifyAllFnActionWorker(session: TestSession, verifyAction: (result: ReturnType, dtsInfo: server.ScriptInfo | undefined, isFirst: boolean) => void, dtsAbsent?: true) { // action let isFirst = true; - for (const actionGetter of actionGetters) { + for (const v of verifier) { for (let fn = 1; fn <= 5; fn++) { - const result = action(actionGetter, fn, session); + const result = action(v, fn, session); const dtsInfo = session.getProjectService().filenameToScriptInfo.get(dtsPath); if (dtsAbsent) { assert.isUndefined(dtsInfo); @@ -490,9 +496,17 @@ fn5(); dependencyTsAndMapOk?: true ) { // action - verifyAllFnActionWorker(session, ({ reqName, response, expectedResponse, expectedResponseNoDts }) => { + verifyAllFnActionWorker(session, ({ reqName, response, expectedResponse, expectedResponseNoDts, verifier }) => { assert.deepEqual(response, expectedResponseNoDts || expectedResponse, `Failed on ${reqName}`); - verifyInfosWhenNoDtsFile(session, host, dependencyTsAndMapOk); + verifyInfosWhenNoDtsFile( + session, + host, + // Even when project actual file contains dts, its not watched because the dts is in another folder and module resolution just fails + // instead of succeeding to source file and then mapping using project reference (When using usage location) + // But watched if sourcemapper is in source project since we need to keep track of dts to update the source mapper for any potential usages + verifier.expectedProjectActualFiles.every(f => f.toLowerCase() !== dtsPath), + dependencyTsAndMapOk, + ); }, /*dtsAbsent*/ true); } @@ -576,7 +590,11 @@ fn5(); // Collecting at this point retains dependency.d.ts and map watcher closeFilesForSession([randomFile], session); openFilesForSession([randomFile], session); - verifyInfosWhenNoDtsFile(session, host); + verifyInfosWhenNoDtsFile( + session, + host, + !!forEach(verifier, v => v.expectedProjectActualFiles.every(f => f.toLowerCase() !== dtsPath)) + ); // Closing open file, removes dependencies too closeFilesForSession([...openFiles, randomFile], session); @@ -657,7 +675,7 @@ fn5(); "when dependency file's map changes", host => host.writeFile( dtsMapLocation, - `{"version":3,"file":"FnS.d.ts","sourceRoot":"","sources":["FnS.ts"],"names":[],"mappings":"AAAA,wBAAgB,GAAG,SAAM;AACzB,wBAAgB,GAAG,SAAM;AACzB,wBAAgB,GAAG,SAAM;AACzB,wBAAgB,GAAG,SAAM;AACzB,wBAAgB,GAAG,SAAM;AACzB,eAAO,MAAM,CAAC,KAAK,CAAC"}` + `{"version":3,"file":"FnS.d.ts","sourceRoot":"","sources":["../dependency/FnS.ts"],"names":[],"mappings":"AAAA,wBAAgB,GAAG,SAAM;AACzB,wBAAgB,GAAG,SAAM;AACzB,wBAAgB,GAAG,SAAM;AACzB,wBAAgB,GAAG,SAAM;AACzB,wBAAgB,GAAG,SAAM;AACzB,eAAO,MAAM,CAAC,KAAK,CAAC"}` ), /*afterActionDocumentPositionMapperNotEquals*/ true ); @@ -675,75 +693,91 @@ fn5(); /*noDts*/ true ); - it("when defining project source changes", () => { - const { host, session } = openTsFile(); + if (withRefs) { + it("when defining project source changes", () => { + const { host, session } = openTsFile(); - // First action - firstAction(session); + // First action + firstAction(session); - // Make change, without rebuild of solution - if (contains(openInfos, dependencyTs.path)) { - session.executeCommandSeq({ - command: protocol.CommandTypes.Change, - arguments: { - file: dependencyTs.path, line: 1, offset: 1, endLine: 1, endOffset: 1, insertString: `function fooBar() { } + // Make change, without rebuild of solution + if (contains(openInfos, dependencyTs.path)) { + session.executeCommandSeq({ + command: protocol.CommandTypes.Change, + arguments: { + file: dependencyTs.path, line: 1, offset: 1, endLine: 1, endOffset: 1, insertString: `function fooBar() { } `} - }); - } - else { - host.writeFile(dependencyTs.path, `function fooBar() { } -${dependencyTs.content}`); - } - host.runQueuedTimeoutCallbacks(); - - for (const actionGetter of actionGetters) { - for (let fn = 1; fn <= 5; fn++) { - const { reqName, request, requestDependencyChange, expectedResponseDependencyChange } = actionGetter(fn); - const { response } = session.executeCommandSeq(requestDependencyChange || request); - assert.deepEqual(response, expectedResponseDependencyChange, `Failed on ${reqName}`); + }); } - } + else { + host.writeFile(dependencyTs.path, `function fooBar() { } +${dependencyTs.content}`); + } + host.runQueuedTimeoutCallbacks(); + + for (const v of verifier) { + for (let fn = 1; fn <= 5; fn++) { + const { reqName, request, requestDependencyChange, expectedResponseDependencyChange } = v.actionGetter(fn); + const { response } = session.executeCommandSeq(requestDependencyChange || request); + assert.deepEqual(response, expectedResponseDependencyChange, `Failed on ${reqName}`); + } + } + }); + } + } + + function verifyScenarios(withRefs: boolean) { + describe(withRefs ? "when main tsconfig has project reference" : "when main tsconfig doesnt have project reference", () => { + const usageVerifier: DocumentPositionMapperVerifier = { + openFile: mainTs, + expectedProjectActualFiles: [mainTs.path, libFile.path, mainConfig.path, dtsPath], + actionGetter: gotoDefintinionFromMainTs, + openFileLastLine: 14 + }; + describe("from project that uses dependency", () => { + const closedInfos = withRefs ? + [dependencyTs.path, dependencyConfig.path, libFile.path, dtsPath, dtsMapLocation] : + [dependencyTs.path, libFile.path, dtsPath, dtsMapLocation]; + verifyDocumentPositionMapperUpdates( + "can go to definition correctly", + [usageVerifier], + closedInfos, + withRefs + ); + }); + + const definingVerifier: DocumentPositionMapperVerifier = { + openFile: dependencyTs, + expectedProjectActualFiles: [dependencyTs.path, libFile.path, dependencyConfig.path], + actionGetter: renameFromDependencyTs, + openFileLastLine: 6, + }; + describe("from defining project", () => { + const closedInfos = [libFile.path, dtsLocation, dtsMapLocation]; + verifyDocumentPositionMapperUpdates( + "rename locations from dependency", + [definingVerifier], + closedInfos, + withRefs + ); + }); + + describe("when opening depedency and usage project", () => { + const closedInfos = withRefs ? + [libFile.path, dtsPath, dtsMapLocation, dependencyConfig.path] : + [libFile.path, dtsPath, dtsMapLocation]; + verifyDocumentPositionMapperUpdates( + "goto Definition in usage and rename locations from defining project", + [usageVerifier, { ...definingVerifier, actionGetter: renameFromDependencyTsWithBothProjectsOpen }], + closedInfos, + withRefs + ); + }); }); } - const usageVerifier: DocumentPositionMapperVerifier = { - openFile: mainTs, - expectedProjectActualFiles: [mainTs.path, libFile.path, mainConfig.path, dtsPath], - actionGetter: gotoDefintinionFromMainTs, - openFileLastLine: 14 - }; - describe("from project that uses dependency", () => { - const closedInfos = [dependencyTs.path, dependencyConfig.path, libFile.path, dtsPath, dtsMapLocation]; - verifyDocumentPositionMapperUpdates( - "can go to definition correctly", - [usageVerifier], - closedInfos - ); - }); - - const definingVerifier: DocumentPositionMapperVerifier = { - openFile: dependencyTs, - expectedProjectActualFiles: [dependencyTs.path, libFile.path, dependencyConfig.path], - actionGetter: renameFromDependencyTs, - openFileLastLine: 6 - }; - describe("from defining project", () => { - const closedInfos = [libFile.path, dtsLocation, dtsMapLocation]; - verifyDocumentPositionMapperUpdates( - "rename locations from dependency", - [definingVerifier], - closedInfos - ); - }); - - describe("when opening depedency and usage project", () => { - const closedInfos = [libFile.path, dtsPath, dtsMapLocation, dependencyConfig.path]; - verifyDocumentPositionMapperUpdates( - "goto Definition in usage and rename locations from defining project", - [usageVerifier, { ...definingVerifier, actionGetter: renameFromDependencyTsWithBothProjectsOpen }], - closedInfos - ); - }); + verifyScenarios(/*withRefs*/ false); + verifyScenarios(/*withRefs*/ true); }); }); } diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 53459d993cf..286516f37af 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -8397,6 +8397,7 @@ declare namespace ts.server { private program; private externalFiles; private missingFilesMap; + private generatedFilesMap; private plugins; private lastFileExceededProgramSize; protected languageService: LanguageService; @@ -8509,6 +8510,9 @@ declare namespace ts.server { private detachScriptInfoFromProject; private addMissingFileWatcher; private isWatchedMissingFile; + private createGeneratedFileWatcher; + private isValidGeneratedFileWatcher; + private clearGeneratedFileWatch; getScriptInfoForNormalizedPath(fileName: NormalizedPath): ScriptInfo | undefined; getScriptInfo(uncheckedFileName: string): ScriptInfo | undefined; filesToString(writeProjectFileNames: boolean): string; From 012ecdacde36330ecd1eab532dbe1d83624d87be Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Wed, 26 Jun 2019 13:41:46 -0700 Subject: [PATCH 08/97] Add sourceOf project reference redirect to filesByName list for redirect path so that module symbol is correctly resolved --- src/compiler/program.ts | 11 +- src/services/services.ts | 9 +- src/services/sourcemaps.ts | 6 + .../unittests/tsserver/projectReferences.ts | 158 +++++++++--------- 4 files changed, 103 insertions(+), 81 deletions(-) diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 6fee35d2448..9a4e26a2903 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -1411,6 +1411,13 @@ namespace ts { for (const newSourceFile of newSourceFiles) { const filePath = newSourceFile.path; addFileToFilesByName(newSourceFile, filePath, newSourceFile.resolvedPath); + if (useSourceOfReference) { + 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); + } + } // Set the file as found during node modules search if it was found that way in old progra, if (oldProgram.isSourceFileFromExternalLibrary(oldProgram.getSourceFileByPath(filePath)!)) { sourceFilesFoundSearchingNodeModules.set(filePath, true); @@ -2240,9 +2247,11 @@ namespace ts { if (useSourceOfReference) { const source = getSourceOfProjectReferenceRedirect(fileName); if (source) { - return isString(source) ? + const file = isString(source) ? findSourceFile(source, toPath(source), isDefaultLib, ignoreNoDefaultLib, refFile, refPos, refEnd, packageId) : undefined; + if (file) addFileToFilesByName(file, path, /*redirectedPath*/ undefined); + return file; } } const originalFileName = fileName; diff --git a/src/services/services.ts b/src/services/services.ts index a8b05402e76..aa6ea119225 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1148,10 +1148,11 @@ namespace ts { useCaseSensitiveFileNames: () => useCaseSensitiveFileNames, getCurrentDirectory: () => currentDirectory, getProgram, - fileExists: host.fileExists && (f => host.fileExists!(f)), - readFile: host.readFile && ((f, encoding) => host.readFile!(f, encoding)), - getDocumentPositionMapper: host.getDocumentPositionMapper && ((generatedFileName, sourceFileName) => host.getDocumentPositionMapper!(generatedFileName, sourceFileName)), - getSourceFileLike: host.getSourceFileLike && (f => host.getSourceFileLike!(f)), + fileExists: maybeBind(host, host.fileExists), + readFile: maybeBind(host, host.readFile), + getDocumentPositionMapper: maybeBind(host, host.getDocumentPositionMapper), + useSourceInsteadOfReferenceRedirect: maybeBind(host, host.useSourceInsteadOfReferenceRedirect), + getSourceFileLike: maybeBind(host, host.getSourceFileLike), log }); diff --git a/src/services/sourcemaps.ts b/src/services/sourcemaps.ts index d07c21a9f43..de590d4e5b4 100644 --- a/src/services/sourcemaps.ts +++ b/src/services/sourcemaps.ts @@ -17,6 +17,7 @@ namespace ts { readFile?(path: string, encoding?: string): string | undefined; getSourceFileLike?(fileName: string): SourceFileLike | undefined; getDocumentPositionMapper?(generatedFileName: string, sourceFileName?: string): DocumentPositionMapper | undefined; + /* @internal */ useSourceInsteadOfReferenceRedirect?(): boolean; log(s: string): void; } @@ -70,6 +71,11 @@ namespace ts { if (!sourceFile) return undefined; const program = host.getProgram()!; + // If this is source file of project reference source (instead of redirect) there is no generated position + if (host.useSourceInsteadOfReferenceRedirect && + host.useSourceInsteadOfReferenceRedirect() && + program.getResolvedProjectReferenceToRedirect(sourceFile.fileName)) return undefined; + const options = program.getCompilerOptions(); const outPath = options.outFile || options.out; diff --git a/src/testRunner/unittests/tsserver/projectReferences.ts b/src/testRunner/unittests/tsserver/projectReferences.ts index 9c02b670bb7..5320cfe13bc 100644 --- a/src/testRunner/unittests/tsserver/projectReferences.ts +++ b/src/testRunner/unittests/tsserver/projectReferences.ts @@ -373,7 +373,7 @@ fn5(); verifyInfosWithRandom(session, host, openInfos, closedInfos, otherWatchedFiles); } - function verifyInfosWhenNoMapFile(session: TestSession, host: TestServerHost, dependencyTsOK?: true) { + function verifyInfosWhenNoMapFile(session: TestSession, host: TestServerHost, dependencyTsOK?: boolean) { const dtsMapClosedInfo = firstDefined(closedInfos, f => f.toLowerCase() === dtsMapPath ? f : undefined); verifyInfosWithRandom( session, @@ -384,46 +384,48 @@ fn5(); ); } - function verifyInfosWhenNoDtsFile(session: TestSession, host: TestServerHost, watchDts: boolean, dependencyTsAndMapOk?: true) { + function verifyInfosWhenNoDtsFile(session: TestSession, host: TestServerHost, watchDts: boolean, dependencyTsOk?: boolean, depedencyMapOk?: boolean) { const dtsMapClosedInfo = firstDefined(closedInfos, f => f.toLowerCase() === dtsMapPath ? f : undefined); const dtsClosedInfo = firstDefined(closedInfos, f => f.toLowerCase() === dtsPath ? f : undefined); verifyInfosWithRandom( session, host, openInfos, - closedInfos.filter(f => (dependencyTsAndMapOk || f !== dtsMapClosedInfo) && f !== dtsClosedInfo && (dependencyTsAndMapOk || f !== dependencyTs.path)), + closedInfos.filter(f => (depedencyMapOk || f !== dtsMapClosedInfo) && f !== dtsClosedInfo && (dependencyTsOk || f !== dependencyTs.path)), dtsClosedInfo && watchDts ? otherWatchedFiles.concat(dtsClosedInfo) : otherWatchedFiles ); } - function verifyDocumentPositionMapper(session: TestSession, dependencyMap: server.ScriptInfo, documentPositionMapper: server.ScriptInfo["documentPositionMapper"], notEqual?: true) { + function verifyDocumentPositionMapper(session: TestSession, dependencyMap: server.ScriptInfo | undefined, documentPositionMapper: server.ScriptInfo["documentPositionMapper"], notEqual?: true) { assert.strictEqual(session.getProjectService().filenameToScriptInfo.get(dtsMapPath), dependencyMap); - if (notEqual) { - assert.notStrictEqual(dependencyMap.documentPositionMapper, documentPositionMapper); - } - else { - assert.strictEqual(dependencyMap.documentPositionMapper, documentPositionMapper); + if (dependencyMap) { + if (notEqual) { + assert.notStrictEqual(dependencyMap.documentPositionMapper, documentPositionMapper); + } + else { + assert.strictEqual(dependencyMap.documentPositionMapper, documentPositionMapper); + } } } - function action(verifier: DocumentPositionMapperVerifier, fn: number, session: TestSession) { - const { reqName, request, expectedResponse, expectedResponseNoMap, expectedResponseNoDts } = verifier.actionGetter(fn); - const { response } = session.executeCommandSeq(request); - return { reqName, response, expectedResponse, expectedResponseNoMap, expectedResponseNoDts, verifier }; + function action(verifier: DocumentPositionMapperVerifier, fn: number, session: TestSession, useDependencyChange?: boolean) { + const { reqName, request, expectedResponse, expectedResponseNoMap, expectedResponseNoDts, requestDependencyChange, expectedResponseDependencyChange } = verifier.actionGetter(fn); + const { response } = session.executeCommandSeq(useDependencyChange ? requestDependencyChange || request : request); + return { reqName, response, expectedResponse, expectedResponseNoMap, expectedResponseNoDts, expectedResponseDependencyChange, verifier }; } function firstAction(session: TestSession) { verifier.forEach(v => action(v, 1, session)); } - function verifyAllFnActionWorker(session: TestSession, verifyAction: (result: ReturnType, dtsInfo: server.ScriptInfo | undefined, isFirst: boolean) => void, dtsAbsent?: true) { + function verifyAllFnActionWorker(session: TestSession, verifyAction: (result: ReturnType, dtsInfo: server.ScriptInfo | undefined, isFirst: boolean) => void, dtsAbsent?: boolean, useDependencyChange?: boolean) { // action let isFirst = true; for (const v of verifier) { for (let fn = 1; fn <= 5; fn++) { - const result = action(v, fn, session); + const result = action(v, fn, session, useDependencyChange); const dtsInfo = session.getProjectService().filenameToScriptInfo.get(dtsPath); if (dtsAbsent) { assert.isUndefined(dtsInfo); @@ -437,33 +439,38 @@ fn5(); } } + function dtsAbsent() { + return withRefs && !contains(closedInfos, dtsPath, (a, b) => a.toLowerCase() === b.toLowerCase()); + } + function verifyAllFnAction( session: TestSession, host: TestServerHost, firstDocumentPositionMapperNotEquals?: true, dependencyMap?: server.ScriptInfo, - documentPositionMapper?: server.ScriptInfo["documentPositionMapper"] + documentPositionMapper?: server.ScriptInfo["documentPositionMapper"], + useDependencyChange?: boolean ) { // action - verifyAllFnActionWorker(session, ({ reqName, response, expectedResponse }, dtsInfo, isFirst) => { - assert.deepEqual(response, expectedResponse, `Failed on ${reqName}`); + verifyAllFnActionWorker(session, ({ reqName, response, expectedResponse, expectedResponseDependencyChange }, dtsInfo, isFirst) => { + assert.deepEqual(response, useDependencyChange ? expectedResponseDependencyChange || expectedResponse : expectedResponse, `Failed on ${reqName}`); verifyInfos(session, host); - assert.equal(dtsInfo!.sourceMapFilePath, dtsMapPath); + if (dtsInfo) assert.equal(dtsInfo.sourceMapFilePath, dtsMapPath); if (isFirst) { if (dependencyMap) { verifyDocumentPositionMapper(session, dependencyMap, documentPositionMapper, firstDocumentPositionMapperNotEquals); documentPositionMapper = dependencyMap.documentPositionMapper; } else { - dependencyMap = session.getProjectService().filenameToScriptInfo.get(dtsMapPath)!; - documentPositionMapper = dependencyMap.documentPositionMapper; + dependencyMap = session.getProjectService().filenameToScriptInfo.get(dtsMapPath); + documentPositionMapper = dependencyMap && dependencyMap.documentPositionMapper; } } else { - verifyDocumentPositionMapper(session, dependencyMap!, documentPositionMapper); + verifyDocumentPositionMapper(session, dependencyMap, documentPositionMapper); } - }); - return { dependencyMap: dependencyMap!, documentPositionMapper }; + }, dtsAbsent(), useDependencyChange); + return { dependencyMap, documentPositionMapper }; } function verifyAllFnActionWithNoMap( @@ -474,19 +481,21 @@ fn5(); let sourceMapFilePath: server.ScriptInfo["sourceMapFilePath"]; // action verifyAllFnActionWorker(session, ({ reqName, response, expectedResponse, expectedResponseNoMap }, dtsInfo, isFirst) => { - assert.deepEqual(response, expectedResponseNoMap || expectedResponse, `Failed on ${reqName}`); + assert.deepEqual(response, withRefs ? expectedResponse : expectedResponseNoMap || expectedResponse, `Failed on ${reqName}`); verifyInfosWhenNoMapFile(session, host, dependencyTsOK); assert.isUndefined(session.getProjectService().filenameToScriptInfo.get(dtsMapPath)); - if (isFirst) { - assert.isNotString(dtsInfo!.sourceMapFilePath); - assert.isNotFalse(dtsInfo!.sourceMapFilePath); - assert.isDefined(dtsInfo!.sourceMapFilePath); - sourceMapFilePath = dtsInfo!.sourceMapFilePath; + if (!withRefs) { + if (isFirst) { + assert.isNotString(dtsInfo!.sourceMapFilePath); + assert.isNotFalse(dtsInfo!.sourceMapFilePath); + assert.isDefined(dtsInfo!.sourceMapFilePath); + sourceMapFilePath = dtsInfo!.sourceMapFilePath; + } + else { + assert.equal(dtsInfo!.sourceMapFilePath, sourceMapFilePath); + } } - else { - assert.equal(dtsInfo!.sourceMapFilePath, sourceMapFilePath); - } - }); + }, dtsAbsent()); return sourceMapFilePath; } @@ -497,7 +506,7 @@ fn5(); ) { // action verifyAllFnActionWorker(session, ({ reqName, response, expectedResponse, expectedResponseNoDts, verifier }) => { - assert.deepEqual(response, expectedResponseNoDts || expectedResponse, `Failed on ${reqName}`); + assert.deepEqual(response, withRefs ? expectedResponse : expectedResponseNoDts || expectedResponse, `Failed on ${reqName}`); verifyInfosWhenNoDtsFile( session, host, @@ -505,7 +514,8 @@ fn5(); // instead of succeeding to source file and then mapping using project reference (When using usage location) // But watched if sourcemapper is in source project since we need to keep track of dts to update the source mapper for any potential usages verifier.expectedProjectActualFiles.every(f => f.toLowerCase() !== dtsPath), - dependencyTsAndMapOk, + /*dependencyTsOk*/ withRefs || dependencyTsAndMapOk, + /*dependencyMapOk*/ dependencyTsAndMapOk ); }, /*dtsAbsent*/ true); } @@ -513,14 +523,15 @@ fn5(); function verifyScenarioWithChangesWorker( change: (host: TestServerHost, session: TestSession) => void, afterActionDocumentPositionMapperNotEquals: true | undefined, - timeoutBeforeAction: boolean + timeoutBeforeAction: boolean, + useDependencyChange?: boolean ) { const { host, session } = openTsFile(); // Create DocumentPositionMapper firstAction(session); - const dependencyMap = session.getProjectService().filenameToScriptInfo.get(dtsMapPath)!; - const documentPositionMapper = dependencyMap.documentPositionMapper; + const dependencyMap = session.getProjectService().filenameToScriptInfo.get(dtsMapPath); + const documentPositionMapper = dependencyMap && dependencyMap.documentPositionMapper; // change change(host, session); @@ -531,21 +542,22 @@ fn5(); } // action - verifyAllFnAction(session, host, afterActionDocumentPositionMapperNotEquals, dependencyMap, documentPositionMapper); + verifyAllFnAction(session, host, afterActionDocumentPositionMapperNotEquals, dependencyMap, documentPositionMapper, useDependencyChange); } function verifyScenarioWithChanges( scenarioName: string, change: (host: TestServerHost, session: TestSession) => void, - afterActionDocumentPositionMapperNotEquals?: true + afterActionDocumentPositionMapperNotEquals?: true, + useDependencyChange?: boolean ) { describe(scenarioName, () => { it("when timeout occurs before request", () => { - verifyScenarioWithChangesWorker(change, afterActionDocumentPositionMapperNotEquals, /*timeoutBeforeAction*/ true); + verifyScenarioWithChangesWorker(change, afterActionDocumentPositionMapperNotEquals, /*timeoutBeforeAction*/ true, useDependencyChange); }); it("when timeout does not occur before request", () => { - verifyScenarioWithChangesWorker(change, afterActionDocumentPositionMapperNotEquals, /*timeoutBeforeAction*/ false); + verifyScenarioWithChangesWorker(change, afterActionDocumentPositionMapperNotEquals, /*timeoutBeforeAction*/ false, useDependencyChange); }); }); } @@ -570,12 +582,12 @@ fn5(); function verifyMainScenarioAndScriptInfoCollectionWithNoMap(session: TestSession, host: TestServerHost, dependencyTsOKInScenario?: true) { // Main scenario action - verifyAllFnActionWithNoMap(session, host, dependencyTsOKInScenario); + verifyAllFnActionWithNoMap(session, host, withRefs || dependencyTsOKInScenario); // Collecting at this point retains dependency.d.ts and map watcher closeFilesForSession([randomFile], session); openFilesForSession([randomFile], session); - verifyInfosWhenNoMapFile(session, host); + verifyInfosWhenNoMapFile(session, host, withRefs); // Closing open file, removes dependencies too closeFilesForSession([...openFiles, randomFile], session); @@ -593,7 +605,8 @@ fn5(); verifyInfosWhenNoDtsFile( session, host, - !!forEach(verifier, v => v.expectedProjectActualFiles.every(f => f.toLowerCase() !== dtsPath)) + !!forEach(verifier, v => v.expectedProjectActualFiles.every(f => f.toLowerCase() !== dtsPath)), + /*dependencyTsOk*/ withRefs ); // Closing open file, removes dependencies too @@ -694,35 +707,26 @@ fn5(); ); if (withRefs) { - it("when defining project source changes", () => { - const { host, session } = openTsFile(); - - // First action - firstAction(session); - - // Make change, without rebuild of solution - if (contains(openInfos, dependencyTs.path)) { - session.executeCommandSeq({ - command: protocol.CommandTypes.Change, - arguments: { - file: dependencyTs.path, line: 1, offset: 1, endLine: 1, endOffset: 1, insertString: `function fooBar() { } + verifyScenarioWithChanges( + "when defining project source changes", + (host, session) => { + // Make change, without rebuild of solution + if (contains(openInfos, dependencyTs.path)) { + session.executeCommandSeq({ + command: protocol.CommandTypes.Change, + arguments: { + file: dependencyTs.path, line: 1, offset: 1, endLine: 1, endOffset: 1, insertString: `function fooBar() { } `} - }); - } - else { - host.writeFile(dependencyTs.path, `function fooBar() { } -${dependencyTs.content}`); - } - host.runQueuedTimeoutCallbacks(); - - for (const v of verifier) { - for (let fn = 1; fn <= 5; fn++) { - const { reqName, request, requestDependencyChange, expectedResponseDependencyChange } = v.actionGetter(fn); - const { response } = session.executeCommandSeq(requestDependencyChange || request); - assert.deepEqual(response, expectedResponseDependencyChange, `Failed on ${reqName}`); + }); } - } - }); + else { + host.writeFile(dependencyTs.path, `function fooBar() { } +${dependencyTs.content}`); + } + }, + /*afterActionDocumentPositionMapperNotEquals*/ undefined, + /*useDepedencyChange*/ true + ); } } @@ -730,13 +734,15 @@ ${dependencyTs.content}`); describe(withRefs ? "when main tsconfig has project reference" : "when main tsconfig doesnt have project reference", () => { const usageVerifier: DocumentPositionMapperVerifier = { openFile: mainTs, - expectedProjectActualFiles: [mainTs.path, libFile.path, mainConfig.path, dtsPath], + expectedProjectActualFiles: withRefs ? + [mainTs.path, libFile.path, mainConfig.path, dependencyTs.path] : + [mainTs.path, libFile.path, mainConfig.path, dtsPath], actionGetter: gotoDefintinionFromMainTs, openFileLastLine: 14 }; describe("from project that uses dependency", () => { const closedInfos = withRefs ? - [dependencyTs.path, dependencyConfig.path, libFile.path, dtsPath, dtsMapLocation] : + [dependencyTs.path, dependencyConfig.path, libFile.path] : [dependencyTs.path, libFile.path, dtsPath, dtsMapLocation]; verifyDocumentPositionMapperUpdates( "can go to definition correctly", @@ -764,7 +770,7 @@ ${dependencyTs.content}`); describe("when opening depedency and usage project", () => { const closedInfos = withRefs ? - [libFile.path, dtsPath, dtsMapLocation, dependencyConfig.path] : + [libFile.path, dependencyConfig.path] : [libFile.path, dtsPath, dtsMapLocation]; verifyDocumentPositionMapperUpdates( "goto Definition in usage and rename locations from defining project", From 2f30add809bbc5988d817896f85ecb93dfec61c4 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Wed, 26 Jun 2019 15:29:35 -0700 Subject: [PATCH 09/97] More tests --- .../unittests/tsserver/declarationFileMaps.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/testRunner/unittests/tsserver/declarationFileMaps.ts b/src/testRunner/unittests/tsserver/declarationFileMaps.ts index 0f8af3c414e..b060790939f 100644 --- a/src/testRunner/unittests/tsserver/declarationFileMaps.ts +++ b/src/testRunner/unittests/tsserver/declarationFileMaps.ts @@ -199,7 +199,7 @@ namespace ts.projectSystem { } function verifyUserTsConfigProject(session: TestSession) { - checkProjectActualFiles(session.getProjectService().configuredProjects.get(userTsconfig.path)!, [userTs.path, aDts.path, userTsconfig.path]); + checkProjectActualFiles(session.getProjectService().configuredProjects.get(userTsconfig.path)!, [userTs.path, aTs.path, userTsconfig.path]); } it("goToDefinition", () => { @@ -470,6 +470,13 @@ namespace ts.projectSystem { name: "function f(): void", }, references: [ + makeReferenceEntry({ + file: aTs, + text: "f", + options: { index: 1 }, + contextText: "function f() {}", + isDefinition: true + }), { fileName: bTs.path, isDefinition: false, @@ -477,13 +484,6 @@ namespace ts.projectSystem { isWriteAccess: false, textSpan: { start: 0, length: 1 }, }, - makeReferenceEntry({ - file: aTs, - text: "f", - options: { index: 1 }, - contextText: "function f() {}", - isDefinition: true - }) ], } ]); From da9260c01305bc4f91193f787c868f0def6c0f48 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Wed, 26 Jun 2019 15:57:22 -0700 Subject: [PATCH 10/97] Create original project when location is in source of project reference redirect --- src/compiler/program.ts | 2 +- src/compiler/utilities.ts | 4 ++++ src/server/editorServices.ts | 7 +++++-- src/server/project.ts | 18 +++++++++++++++--- src/server/session.ts | 6 ++++-- src/services/sourcemaps.ts | 7 ++++--- .../tsserver/events/projectLoading.ts | 2 +- 7 files changed, 34 insertions(+), 12 deletions(-) diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 9a4e26a2903..2ddf191a327 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -814,7 +814,7 @@ namespace ts { let projectReferenceRedirects: Map | undefined; let mapFromFileToProjectReferenceRedirects: Map | undefined; let mapFromToProjectReferenceRedirectSource: Map | undefined; - const useSourceOfReference = host.useSourceInsteadOfReferenceRedirect && host.useSourceInsteadOfReferenceRedirect(); + const useSourceOfReference = useSourceInsteadOfReferenceRedirect(host); const shouldCreateNewSourceFile = shouldProgramCreateNewSourceFiles(oldProgram, options); const structuralIsReused = tryReuseStructureFromOldProgram(); diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 0e2488a0233..ecf7d431a2e 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -4614,6 +4614,10 @@ namespace ts { return false; } } + + export function useSourceInsteadOfReferenceRedirect(host: { useSourceInsteadOfReferenceRedirect?(): boolean; }) { + return host.useSourceInsteadOfReferenceRedirect && host.useSourceInsteadOfReferenceRedirect(); + } } namespace ts { diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index 8c7462d857b..e379d1fda2c 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -2570,7 +2570,9 @@ namespace ts.server { /*@internal*/ getOriginalLocationEnsuringConfiguredProject(project: Project, location: DocumentPosition): DocumentPosition | undefined { - const originalLocation = project.getSourceMapper().tryGetSourcePosition(location); + const originalLocation = useSourceInsteadOfReferenceRedirect(project) && project.getResolvedProjectReferenceToRedirect(location.fileName) ? + location : + project.getSourceMapper().tryGetSourcePosition(location); if (!originalLocation) return undefined; const { fileName } = originalLocation; @@ -2581,7 +2583,8 @@ namespace ts.server { if (!configFileName) return undefined; const configuredProject = this.findConfiguredProjectByProjectName(configFileName) || - this.createAndLoadConfiguredProject(configFileName, `Creating project for original file: ${originalFileInfo.fileName} for location: ${location.fileName}`); + this.createAndLoadConfiguredProject(configFileName, `Creating project for original file: ${originalFileInfo.fileName}${location !== originalLocation ? " for location " + location.fileName : ""}`); + if (configuredProject === project) return originalLocation; updateProjectIfDirty(configuredProject); // Keep this configured project as referenced from project addOriginalConfiguredProject(configuredProject); diff --git a/src/server/project.ts b/src/server/project.ts index 8f8e6e5da8e..c6c8ab84b20 100644 --- a/src/server/project.ts +++ b/src/server/project.ts @@ -196,6 +196,14 @@ namespace ts.server { /*@internal*/ originalConfiguredProjects: Map | undefined; + /*@internal*/ + useSourceInsteadOfReferenceRedirect?: () => boolean; + + /*@internal*/ + getResolvedProjectReferenceToRedirect(_fileName: string): ResolvedProjectReference | undefined { + return undefined; + } + private readonly cancellationToken: ThrottledCancellationToken; public isNonTsProject() { @@ -1526,9 +1534,7 @@ namespace ts.server { } /* @internal */ - useSourceInsteadOfReferenceRedirect() { - return !!this.languageServiceEnabled; - } + useSourceInsteadOfReferenceRedirect = () => !!this.languageServiceEnabled; fileExists(file: string): boolean { // Project references go to source file instead of .d.ts file @@ -1590,6 +1596,12 @@ namespace ts.server { return program && program.forEachResolvedProjectReference(cb); } + /*@internal*/ + getResolvedProjectReferenceToRedirect(fileName: string): ResolvedProjectReference | undefined { + const program = this.getCurrentProgram(); + return program && program.getResolvedProjectReferenceToRedirect(fileName); + } + /*@internal*/ enablePluginsWithOptions(options: CompilerOptions, pluginConfigOverrides: Map | undefined) { const host = this.projectService.host; diff --git a/src/server/session.ts b/src/server/session.ts index 5064b529560..792ec2cbce9 100644 --- a/src/server/session.ts +++ b/src/server/session.ts @@ -443,7 +443,9 @@ namespace ts.server { function getDefinitionInProject(definition: DocumentPosition | undefined, definingProject: Project, project: Project): DocumentPosition | undefined { if (!definition || project.containsFile(toNormalizedPath(definition.fileName))) return definition; - const mappedDefinition = definingProject.getLanguageService().getSourceMapper().tryGetGeneratedPosition(definition); + const mappedDefinition = useSourceInsteadOfReferenceRedirect(definingProject) && definingProject.getResolvedProjectReferenceToRedirect(definition.fileName) ? + definition : + definingProject.getLanguageService().getSourceMapper().tryGetGeneratedPosition(definition); return mappedDefinition && project.containsFile(toNormalizedPath(mappedDefinition.fileName)) ? mappedDefinition : undefined; } @@ -472,7 +474,7 @@ namespace ts.server { for (const symlinkedProject of symlinkedProjects) addToTodo({ project: symlinkedProject, location: originalLocation as TLocation }, toDo!, seenProjects); }); } - return originalLocation; + return originalLocation === location ? undefined : originalLocation; }); return toDo; } diff --git a/src/services/sourcemaps.ts b/src/services/sourcemaps.ts index de590d4e5b4..c4c14e58686 100644 --- a/src/services/sourcemaps.ts +++ b/src/services/sourcemaps.ts @@ -72,9 +72,10 @@ namespace ts { const program = host.getProgram()!; // If this is source file of project reference source (instead of redirect) there is no generated position - if (host.useSourceInsteadOfReferenceRedirect && - host.useSourceInsteadOfReferenceRedirect() && - program.getResolvedProjectReferenceToRedirect(sourceFile.fileName)) return undefined; + if (useSourceInsteadOfReferenceRedirect(host) && + program.getResolvedProjectReferenceToRedirect(sourceFile.fileName)) { + return undefined; + } const options = program.getCompilerOptions(); const outPath = options.outFile || options.out; diff --git a/src/testRunner/unittests/tsserver/events/projectLoading.ts b/src/testRunner/unittests/tsserver/events/projectLoading.ts index 7a881ff1380..53fe28240f8 100644 --- a/src/testRunner/unittests/tsserver/events/projectLoading.ts +++ b/src/testRunner/unittests/tsserver/events/projectLoading.ts @@ -110,7 +110,7 @@ namespace ts.projectSystem { checkNumberOfProjects(service, { configuredProjects: 2 }); const project = service.configuredProjects.get(configA.path)!; assert.isDefined(project); - verifyEvent(project, `Creating project for original file: ${aTs.path} for location: ${aDTs.path}`); + verifyEvent(project, `Creating project for original file: ${aTs.path}`); }); describe("with external projects and config files ", () => { From 75bd3cd9be28686c492d7028ed828fce7f9bcef9 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Thu, 27 Jun 2019 12:12:02 -0700 Subject: [PATCH 11/97] Fix more tests --- src/testRunner/unittests/tsbuildWatchMode.ts | 191 +++++++++++-------- 1 file changed, 108 insertions(+), 83 deletions(-) diff --git a/src/testRunner/unittests/tsbuildWatchMode.ts b/src/testRunner/unittests/tsbuildWatchMode.ts index a630e28f1d8..6a577888c7e 100644 --- a/src/testRunner/unittests/tsbuildWatchMode.ts +++ b/src/testRunner/unittests/tsbuildWatchMode.ts @@ -681,9 +681,9 @@ let x: string = 10;`); const coreIndexDts = projectFileName(SubProject.core, "index.d.ts"); const coreAnotherModuleDts = projectFileName(SubProject.core, "anotherModule.d.ts"); const logicIndexDts = projectFileName(SubProject.logic, "index.d.ts"); - const expectedWatchedFiles = [core[0], logic[0], ...tests, libFile].map(f => f.path).concat([coreIndexDts, coreAnotherModuleDts, logicIndexDts].map(f => f.toLowerCase())); const expectedWatchedDirectoriesRecursive = projectSystem.getTypeRootsFromLocation(projectPath(SubProject.tests)); const expectedProgramFiles = [tests[1].path, libFile.path, coreIndexDts, coreAnotherModuleDts, logicIndexDts]; + const expectedProjectFiles = [libFile, ...tests, ...logic.slice(1), ...core.slice(1, core.length - 1)].map(f => f.path); function createSolutionAndWatchMode() { return createSolutionAndWatchModeOfProject(allFiles, projectsLocation, `${project}/${SubProject.tests}`, tests[0].path, getOutputFileStamps); @@ -694,12 +694,19 @@ let x: string = 10;`); } function verifyWatches(host: TsBuildWatchSystem, withTsserver?: boolean) { - verifyWatchesOfProject(host, withTsserver ? expectedWatchedFiles.filter(f => f !== tests[1].path.toLowerCase()) : expectedWatchedFiles, expectedWatchedDirectoriesRecursive); + verifyWatchesOfProject( + host, + withTsserver ? + [...core.slice(0, core.length - 1), ...logic, tests[0], libFile].map(f => f.path.toLowerCase()) : + [core[0], logic[0], ...tests, libFile].map(f => f.path).concat([coreIndexDts, coreAnotherModuleDts, logicIndexDts].map(f => f.toLowerCase())), + expectedWatchedDirectoriesRecursive + ); } function verifyScenario( edit: (host: TsBuildWatchSystem, solutionBuilder: SolutionBuilder) => void, - expectedFilesAfterEdit: ReadonlyArray + expectedProgramFilesAfterEdit: ReadonlyArray, + expectedProjectFilesAfterEdit: ReadonlyArray ) { it("with tsc-watch", () => { const { host, solutionBuilder, watch } = createSolutionAndWatchMode(); @@ -708,7 +715,7 @@ let x: string = 10;`); host.checkTimeoutQueueLengthAndRun(1); checkOutputErrorsIncremental(host, emptyArray); - checkProgramActualFiles(watch(), expectedFilesAfterEdit); + checkProgramActualFiles(watch(), expectedProgramFilesAfterEdit); }); @@ -718,7 +725,7 @@ let x: string = 10;`); edit(host, solutionBuilder); host.checkTimeoutQueueLengthAndRun(2); - checkProjectActualFiles(service, tests[0].path, [tests[0].path, ...expectedFilesAfterEdit]); + checkProjectActualFiles(service, tests[0].path, [...expectedProjectFilesAfterEdit]); }); } @@ -748,7 +755,7 @@ function foo() { // not ideal, but currently because of d.ts but no new file is written // There will be timeout queued even though file contents are same - }, expectedProgramFiles); + }, expectedProgramFiles, expectedProjectFiles); }); describe("non local edit in ts file, rebuilds in watch compilation", () => { @@ -758,7 +765,7 @@ export function gfoo() { }`); solutionBuilder.invalidateProject(logic[0].path.toLowerCase() as ResolvedConfigFilePath); solutionBuilder.buildNextInvalidatedProject(); - }, expectedProgramFiles); + }, expectedProgramFiles, expectedProjectFiles); }); describe("change in project reference config file builds correctly", () => { @@ -769,7 +776,7 @@ export function gfoo() { })); solutionBuilder.invalidateProject(logic[0].path.toLowerCase() as ResolvedConfigFilePath, ConfigFileProgramReloadLevel.Full); solutionBuilder.buildNextInvalidatedProject(); - }, [tests[1].path, libFile.path, coreIndexDts, coreAnotherModuleDts, projectFilePath(SubProject.logic, "decls/index.d.ts")]); + }, [tests[1].path, libFile.path, coreIndexDts, coreAnotherModuleDts, projectFilePath(SubProject.logic, "decls/index.d.ts")], expectedProjectFiles); }); }); @@ -859,7 +866,9 @@ export function gfoo() { const aDts = dtsFile(multiFolder ? "a/index" : "a"), bDts = dtsFile(multiFolder ? "b/index" : "b"); const expectedFiles = [jsFile(multiFolder ? "a/index" : "a"), aDts, jsFile(multiFolder ? "b/index" : "b"), bDts, jsFile(multiFolder ? "c/index" : "c")]; const expectedProgramFiles = [cTs.path, libFile.path, aDts, refs.path, bDts]; + const expectedProjectFiles = [cTs.path, libFile.path, aTs.path, refs.path, bTs.path]; const expectedWatchedFiles = expectedProgramFiles.concat(cTsconfig.path, bTsconfig.path, aTsconfig.path).map(s => s.toLowerCase()); + const expectedProjectWatchedFiles = expectedProjectFiles.concat(cTsconfig.path, bTsconfig.path, aTsconfig.path).map(s => s.toLowerCase()); const expectedWatchedDirectories = multiFolder ? [ getProjectPath(project).toLowerCase() // watches for directories created for resolution of b ] : emptyArray; @@ -897,22 +906,29 @@ export function gfoo() { } function verifyProject(host: TsBuildWatchSystem, service: projectSystem.TestProjectService, orphanInfos?: ReadonlyArray) { - verifyServerState(host, service, expectedProgramFiles, expectedWatchedFiles, expectedWatchedDirectoriesRecursive, orphanInfos); + verifyServerState({ host, service, expectedProjectFiles, expectedProjectWatchedFiles, expectedWatchedDirectoriesRecursive, orphanInfos }); } - function verifyServerState( - host: TsBuildWatchSystem, - service: projectSystem.TestProjectService, - expectedProgramFiles: ReadonlyArray, - expectedWatchedFiles: ReadonlyArray, - expectedWatchedDirectoriesRecursive: ReadonlyArray, - orphanInfos?: ReadonlyArray) { - checkProjectActualFiles(service, cTsconfig.path, expectedProgramFiles.concat(cTsconfig.path)); - const watchedFiles = expectedWatchedFiles.filter(f => f !== cTs.path.toLowerCase()); - if (orphanInfos) { + interface VerifyServerState { + host: TsBuildWatchSystem; + service: projectSystem.TestProjectService; + expectedProjectFiles: ReadonlyArray; + expectedProjectWatchedFiles: ReadonlyArray; + expectedWatchedDirectoriesRecursive: ReadonlyArray; + orphanInfos?: ReadonlyArray; + } + function verifyServerState({ host, service, expectedProjectFiles, expectedProjectWatchedFiles, expectedWatchedDirectoriesRecursive, orphanInfos }: VerifyServerState) { + checkProjectActualFiles(service, cTsconfig.path, expectedProjectFiles.concat(cTsconfig.path)); + const watchedFiles = expectedProjectWatchedFiles.filter(f => f !== cTs.path.toLowerCase()); + const actualOrphan = arrayFrom(mapDefinedIterator( + service.filenameToScriptInfo.values(), + v => v.containingProjects.length === 0 ? v.fileName : undefined + )); + assert.equal(actualOrphan.length, orphanInfos ? orphanInfos.length : 0, `Orphans found: ${JSON.stringify(actualOrphan, /*replacer*/ undefined, " ")}`); + if (orphanInfos && orphanInfos.length) { for (const orphan of orphanInfos) { const info = service.getScriptInfoForPath(orphan as Path); - assert.isDefined(info); + assert.isDefined(info, `${orphan} expected to be present. Actual: ${JSON.stringify(actualOrphan, /*replacer*/ undefined, " ")}`); assert.equal(info!.containingProjects.length, 0); watchedFiles.push(orphan); } @@ -920,16 +936,20 @@ export function gfoo() { verifyWatchesOfProject(host, watchedFiles, expectedWatchedDirectoriesRecursive, expectedWatchedDirectories); } - function verifyScenario( - edit: (host: TsBuildWatchSystem, solutionBuilder: SolutionBuilder) => void, - expectedEditErrors: ReadonlyArray, - expectedProgramFiles: ReadonlyArray, - expectedWatchedFiles: ReadonlyArray, - expectedWatchedDirectoriesRecursive: ReadonlyArray, - dependencies: ReadonlyArray<[string, ReadonlyArray]>, - revert?: (host: TsBuildWatchSystem) => void, - orphanInfosAfterEdit?: ReadonlyArray, - orphanInfosAfterRevert?: ReadonlyArray) { + interface VerifyScenario { + edit: (host: TsBuildWatchSystem, solutionBuilder: SolutionBuilder) => void; + expectedEditErrors: ReadonlyArray; + expectedProgramFiles: ReadonlyArray; + expectedProjectFiles: ReadonlyArray; + expectedWatchedFiles: ReadonlyArray; + expectedProjectWatchedFiles: ReadonlyArray; + expectedWatchedDirectoriesRecursive: ReadonlyArray; + dependencies: ReadonlyArray<[string, ReadonlyArray]>; + revert?: (host: TsBuildWatchSystem) => void; + orphanInfosAfterEdit?: ReadonlyArray; + orphanInfosAfterRevert?: ReadonlyArray; + } + function verifyScenario({ edit, expectedEditErrors, expectedProgramFiles, expectedProjectFiles, expectedWatchedFiles, expectedProjectWatchedFiles, expectedWatchedDirectoriesRecursive, dependencies, revert, orphanInfosAfterEdit, orphanInfosAfterRevert }: VerifyScenario) { it("with tsc-watch", () => { const { host, solutionBuilder, watch } = createSolutionAndWatchMode(); @@ -956,7 +976,7 @@ export function gfoo() { edit(host, solutionBuilder); host.checkTimeoutQueueLengthAndRun(2); - verifyServerState(host, service, expectedProgramFiles, expectedWatchedFiles, expectedWatchedDirectoriesRecursive, orphanInfosAfterEdit); + verifyServerState({ host, service, expectedProjectFiles, expectedProjectWatchedFiles, expectedWatchedDirectoriesRecursive, orphanInfos: orphanInfosAfterEdit }); if (revert) { revert(host); @@ -981,20 +1001,21 @@ export function gfoo() { }); describe("non local edit updates the program and watch correctly", () => { - verifyScenario( - (host, solutionBuilder) => { + verifyScenario({ + edit: (host, solutionBuilder) => { // edit - host.writeFile(bTs.path, `${bTs.content} -export function gfoo() { -}`); - solutionBuilder.invalidateProject(bTsconfig.path.toLowerCase() as ResolvedConfigFilePath); + host.writeFile(bTs.path, `${bTs.content}\nexport function gfoo() {\n}`); + solutionBuilder.invalidateProject((bTsconfig.path.toLowerCase() as ResolvedConfigFilePath)); solutionBuilder.buildNextInvalidatedProject(); }, - emptyArray, + expectedEditErrors: emptyArray, expectedProgramFiles, + expectedProjectFiles, expectedWatchedFiles, + expectedProjectWatchedFiles, expectedWatchedDirectoriesRecursive, - defaultDependencies); + dependencies: defaultDependencies + }); }); describe("edit on config file", () => { @@ -1003,30 +1024,32 @@ export function gfoo() { path: getFilePathInProject(project, "nrefs/a.d.ts"), content: refs.content }; - verifyScenario( - host => { + verifyScenario({ + edit: host => { const cTsConfigJson = JSON.parse(cTsconfig.content); host.ensureFileOrFolder(nrefs); cTsConfigJson.compilerOptions.paths = { "@ref/*": nrefsPath }; host.writeFile(cTsconfig.path, JSON.stringify(cTsConfigJson)); }, - emptyArray, - expectedProgramFiles.map(nrefReplacer), - expectedWatchedFiles.map(nrefReplacer), - expectedWatchedDirectoriesRecursive.map(nrefReplacer), - [ + expectedEditErrors: emptyArray, + expectedProgramFiles: expectedProgramFiles.map(nrefReplacer), + expectedProjectFiles: expectedProjectFiles.map(nrefReplacer), + expectedWatchedFiles: expectedWatchedFiles.map(nrefReplacer), + expectedProjectWatchedFiles: expectedProjectWatchedFiles.map(nrefReplacer), + expectedWatchedDirectoriesRecursive: expectedWatchedDirectoriesRecursive.map(nrefReplacer), + dependencies: [ [aDts, [aDts]], [bDts, [bDts, aDts]], [nrefs.path, [nrefs.path]], [cTs.path, [cTs.path, nrefs.path, bDts]] ], // revert the update - host => host.writeFile(cTsconfig.path, cTsconfig.content), + revert: host => host.writeFile(cTsconfig.path, cTsconfig.content), // AfterEdit:: Extra watched files on server since the script infos arent deleted till next file open - [refs.path.toLowerCase()], + orphanInfosAfterEdit: [refs.path.toLowerCase()], // AfterRevert:: Extra watched files on server since the script infos arent deleted till next file open - [nrefs.path.toLowerCase()] - ); + orphanInfosAfterRevert: [nrefs.path.toLowerCase()] + }); }); describe("edit in referenced config file", () => { @@ -1035,82 +1058,84 @@ export function gfoo() { content: "export declare class A {}" }; const expectedProgramFiles = [cTs.path, bDts, nrefs.path, refs.path, libFile.path]; + const expectedProjectFiles = [cTs.path, bTs.path, nrefs.path, refs.path, libFile.path]; const [, ...expectedWatchedDirectoriesRecursiveWithoutA] = expectedWatchedDirectoriesRecursive; // Not looking in a folder for resolution in multi folder scenario - verifyScenario( - host => { + verifyScenario({ + edit: host => { const bTsConfigJson = JSON.parse(bTsconfig.content); host.ensureFileOrFolder(nrefs); bTsConfigJson.compilerOptions.paths = { "@ref/*": nrefsPath }; host.writeFile(bTsconfig.path, JSON.stringify(bTsConfigJson)); }, - emptyArray, + expectedEditErrors: emptyArray, expectedProgramFiles, - expectedProgramFiles.concat(cTsconfig.path, bTsconfig.path, aTsconfig.path).map(s => s.toLowerCase()), - (multiFolder ? expectedWatchedDirectoriesRecursiveWithoutA : expectedWatchedDirectoriesRecursive).concat(getFilePathInProject(project, "nrefs").toLowerCase()), - [ + expectedProjectFiles, + expectedWatchedFiles: expectedProgramFiles.concat(cTsconfig.path, bTsconfig.path, aTsconfig.path).map(s => s.toLowerCase()), + expectedProjectWatchedFiles: expectedProjectFiles.concat(cTsconfig.path, bTsconfig.path, aTsconfig.path).map(s => s.toLowerCase()), + expectedWatchedDirectoriesRecursive: (multiFolder ? expectedWatchedDirectoriesRecursiveWithoutA : expectedWatchedDirectoriesRecursive).concat(getFilePathInProject(project, "nrefs").toLowerCase()), + dependencies: [ [nrefs.path, [nrefs.path]], [bDts, [bDts, nrefs.path]], [refs.path, [refs.path]], [cTs.path, [cTs.path, refs.path, bDts]], ], // revert the update - host => host.writeFile(bTsconfig.path, bTsconfig.content), + revert: host => host.writeFile(bTsconfig.path, bTsconfig.content), // AfterEdit:: Extra watched files on server since the script infos arent deleted till next file open - [aDts.toLowerCase()], + orphanInfosAfterEdit: [aTs.path.toLowerCase()], // AfterRevert:: Extra watched files on server since the script infos arent deleted till next file open - [nrefs.path.toLowerCase()] - ); + orphanInfosAfterRevert: [nrefs.path.toLowerCase()] + }); }); describe("deleting referenced config file", () => { const expectedProgramFiles = [cTs.path, bTs.path, refs.path, libFile.path]; + const expectedWatchedFiles = expectedProgramFiles.concat(cTsconfig.path, bTsconfig.path).map(s => s.toLowerCase()); const [, ...expectedWatchedDirectoriesRecursiveWithoutA] = expectedWatchedDirectoriesRecursive; // Not looking in a folder for resolution in multi folder scenario // Resolutions should change now // Should map to b.ts instead with options from our own config - verifyScenario( - host => host.deleteFile(bTsconfig.path), - [ + verifyScenario({ + edit: host => host.deleteFile(bTsconfig.path), + expectedEditErrors: [ `${multiFolder ? "c/tsconfig.json" : "tsconfig.c.json"}(9,21): error TS6053: File '/user/username/projects/transitiveReferences/${multiFolder ? "b" : "tsconfig.b.json"}' not found.\n` ], expectedProgramFiles, - expectedProgramFiles.concat(cTsconfig.path, bTsconfig.path).map(s => s.toLowerCase()), - multiFolder ? expectedWatchedDirectoriesRecursiveWithoutA : expectedWatchedDirectoriesRecursive, - [ + expectedProjectFiles: expectedProgramFiles, + expectedWatchedFiles, + expectedProjectWatchedFiles: expectedWatchedFiles, + expectedWatchedDirectoriesRecursive: multiFolder ? expectedWatchedDirectoriesRecursiveWithoutA : expectedWatchedDirectoriesRecursive, + dependencies: [ [bTs.path, [bTs.path, refs.path]], [refs.path, [refs.path]], [cTs.path, [cTs.path, refs.path, bTs.path]], ], // revert the update - host => host.writeFile(bTsconfig.path, bTsconfig.content), + revert: host => host.writeFile(bTsconfig.path, bTsconfig.content), // AfterEdit:: Extra watched files on server since the script infos arent deleted till next file open - [bDts.toLowerCase(), aDts.toLowerCase(), aTsconfig.path.toLowerCase()], - // AfterRevert:: Extra watched files on server since the script infos arent deleted till next file open - [bTs.path.toLowerCase()] - ); + orphanInfosAfterEdit: [aTs.path.toLowerCase(), aTsconfig.path.toLowerCase()], + }); }); describe("deleting transitively referenced config file", () => { - verifyScenario( - host => host.deleteFile(aTsconfig.path), - [ + verifyScenario({ + edit: host => host.deleteFile(aTsconfig.path), + expectedEditErrors: [ `${multiFolder ? "b/tsconfig.json" : "tsconfig.b.json"}(10,21): error TS6053: File '/user/username/projects/transitiveReferences/${multiFolder ? "a" : "tsconfig.a.json"}' not found.\n` ], - expectedProgramFiles.map(s => s.replace(aDts, aTs.path)), - expectedWatchedFiles.map(s => s.replace(aDts.toLowerCase(), aTs.path.toLocaleLowerCase())), + expectedProgramFiles: expectedProgramFiles.map(s => s.replace(aDts, aTs.path)), + expectedProjectFiles, + expectedWatchedFiles: expectedWatchedFiles.map(s => s.replace(aDts.toLowerCase(), aTs.path.toLocaleLowerCase())), + expectedProjectWatchedFiles, expectedWatchedDirectoriesRecursive, - [ + dependencies: [ [aTs.path, [aTs.path]], [bDts, [bDts, aTs.path]], [refs.path, [refs.path]], [cTs.path, [cTs.path, refs.path, bDts]], ], // revert the update - host => host.writeFile(aTsconfig.path, aTsconfig.content), - // AfterEdit:: Extra watched files on server since the script infos arent deleted till next file open - [aDts.toLowerCase()], - // AfterRevert:: Extra watched files on server since the script infos arent deleted till next file open - [aTs.path.toLowerCase()] - ); + revert: host => host.writeFile(aTsconfig.path, aTsconfig.content), + }); }); } From f72af3be60688fa30a2e918e8dc86c1fad882b8a Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Fri, 28 Jun 2019 15:49:48 -0700 Subject: [PATCH 12/97] Verify the scenarios when d.ts directory of dependency doesnt exist --- src/compiler/program.ts | 14 ++++-- src/compiler/types.ts | 9 ++-- src/server/project.ts | 45 ++++++++++++++++--- src/services/services.ts | 4 +- src/services/types.ts | 2 +- .../unittests/tsserver/projectReferences.ts | 30 +++++++++++++ 6 files changed, 88 insertions(+), 16 deletions(-) diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 27f5d25c847..37c2d6a9c83 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -826,8 +826,11 @@ namespace ts { if (!resolvedProjectReferences) { resolvedProjectReferences = projectReferences.map(parseProjectReferenceConfigFile); } - if (host.setGetSourceOfProjectReferenceRedirect) { - host.setGetSourceOfProjectReferenceRedirect(getSourceOfProjectReferenceRedirect); + if (host.setResolvedProjectReferenceCallbacks) { + host.setResolvedProjectReferenceCallbacks({ + getSourceOfProjectReferenceRedirect, + forEachResolvedProjectReference + }); } if (rootNames.length) { for (const parsedRef of resolvedProjectReferences) { @@ -1226,8 +1229,11 @@ namespace ts { } if (projectReferences) { resolvedProjectReferences = projectReferences.map(parseProjectReferenceConfigFile); - if (host.setGetSourceOfProjectReferenceRedirect) { - host.setGetSourceOfProjectReferenceRedirect(getSourceOfProjectReferenceRedirect); + if (host.setResolvedProjectReferenceCallbacks) { + host.setResolvedProjectReferenceCallbacks({ + getSourceOfProjectReferenceRedirect, + forEachResolvedProjectReference + }); } } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 79a92cfa31d..3cf506b0d58 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -5166,7 +5166,7 @@ namespace ts { /* @internal */ hasChangedAutomaticTypeDirectiveNames?: boolean; createHash?(data: string): string; getParsedCommandLine?(fileName: string): ParsedCommandLine | undefined; - /* @internal */ setGetSourceOfProjectReferenceRedirect?(getSource: GetSourceOfProjectReferenceRedirect): void; + /* @internal */ setResolvedProjectReferenceCallbacks?(callbacks: ResolvedProjectReferenceCallbacks): void; /* @internal */ useSourceInsteadOfReferenceRedirect?(): boolean; // TODO: later handle this in better way in builder host instead once the api for tsbuild finalizes and doesn't use compilerHost as base @@ -5175,10 +5175,13 @@ namespace ts { /** true if --out otherwise source file name */ /*@internal*/ - export type SourceOfProjectReferenceRedirect = string | true ; + export type SourceOfProjectReferenceRedirect = string | true; /*@internal*/ - export type GetSourceOfProjectReferenceRedirect = (fileName: string) => SourceOfProjectReferenceRedirect | undefined; + interface ResolvedProjectReferenceCallbacks { + getSourceOfProjectReferenceRedirect(fileName: string): SourceOfProjectReferenceRedirect | undefined; + forEachResolvedProjectReference(cb: (resolvedProjectReference: ResolvedProjectReference | undefined, resolvedProjectReferencePath: Path) => T | undefined): T | undefined; + } /* @internal */ export const enum TransformFlags { diff --git a/src/server/project.ts b/src/server/project.ts index c6c8ab84b20..04a5b91531a 100644 --- a/src/server/project.ts +++ b/src/server/project.ts @@ -1482,7 +1482,8 @@ namespace ts.server { configFileWatcher: FileWatcher | undefined; private directoriesWatchedForWildcards: Map | undefined; readonly canonicalConfigFilePath: NormalizedPath; - private getSourceOfProjectReferenceRedirect: GetSourceOfProjectReferenceRedirect | undefined; + private projectReferenceCallbacks: ResolvedProjectReferenceCallbacks | undefined; + private mapOfDeclarationDirectories: Map | undefined; /* @internal */ pendingReload: ConfigFileProgramReloadLevel | undefined; @@ -1529,8 +1530,8 @@ namespace ts.server { } /* @internal */ - setGetSourceOfProjectReferenceRedirect(getSource: GetSourceOfProjectReferenceRedirect) { - this.getSourceOfProjectReferenceRedirect = getSource; + setResolvedProjectReferenceCallbacks(projectReferenceCallbacks: ResolvedProjectReferenceCallbacks) { + this.projectReferenceCallbacks = projectReferenceCallbacks; } /* @internal */ @@ -1538,13 +1539,42 @@ namespace ts.server { fileExists(file: string): boolean { // Project references go to source file instead of .d.ts file - if (this.languageServiceEnabled && this.getSourceOfProjectReferenceRedirect) { - const source = this.getSourceOfProjectReferenceRedirect(file); + if (this.useSourceInsteadOfReferenceRedirect() && this.projectReferenceCallbacks) { + const source = this.projectReferenceCallbacks.getSourceOfProjectReferenceRedirect(file); if (source) return isString(source) ? super.fileExists(source) : true; } return super.fileExists(file); } + directoryExists(path: string): boolean { + if (super.directoryExists(path)) return true; + if (!this.useSourceInsteadOfReferenceRedirect() || !this.projectReferenceCallbacks) return false; + + if (!this.mapOfDeclarationDirectories) { + this.mapOfDeclarationDirectories = createMap(); + this.projectReferenceCallbacks.forEachResolvedProjectReference(ref => { + if (!ref) return; + const out = ref.commandLine.options.outFile || ref.commandLine.options.outDir; + if (out) { + this.mapOfDeclarationDirectories!.set(getDirectoryPath(this.toPath(out)), true); + } + else { + // Set declaration's in different locations only, if they are next to source the directory present doesnt change + const declarationDir = ref.commandLine.options.declarationDir || ref.commandLine.options.outDir; + if (declarationDir) { + this.mapOfDeclarationDirectories!.set(this.toPath(declarationDir), true); + } + } + }); + } + const dirPath = this.toPath(path); + const dirPathWithTrailingDirectorySeparator = `${dirPath}${directorySeparator}`; + return !!forEachKey( + this.mapOfDeclarationDirectories, + declDirPath => dirPath === declDirPath || startsWith(declDirPath, dirPathWithTrailingDirectorySeparator) + ); + } + /** * If the project has reload from disk pending, it reloads (and then updates graph as part of that) instead of just updating the graph * @returns: true if set of files in the project stays the same and false - otherwise. @@ -1553,6 +1583,8 @@ namespace ts.server { this.isInitialLoadPending = returnFalse; const reloadLevel = this.pendingReload; this.pendingReload = ConfigFileProgramReloadLevel.None; + this.projectReferenceCallbacks = undefined; + this.mapOfDeclarationDirectories = undefined; let result: boolean; switch (reloadLevel) { case ConfigFileProgramReloadLevel.Partial: @@ -1567,7 +1599,6 @@ namespace ts.server { default: result = super.updateGraph(); } - this.getSourceOfProjectReferenceRedirect = undefined; this.projectService.sendProjectLoadingFinishEvent(this); this.projectService.sendProjectTelemetry(this); return result; @@ -1684,6 +1715,8 @@ namespace ts.server { this.stopWatchingWildCards(); this.projectErrors = undefined; this.configFileSpecs = undefined; + this.projectReferenceCallbacks = undefined; + this.mapOfDeclarationDirectories = undefined; super.close(); } diff --git a/src/services/services.ts b/src/services/services.ts index aa6ea119225..7de7934653e 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1246,8 +1246,8 @@ namespace ts { return host.resolveTypeReferenceDirectives!(typeReferenceDirectiveNames, containingFile, redirectedReference); }; } - if (host.setGetSourceOfProjectReferenceRedirect) { - compilerHost.setGetSourceOfProjectReferenceRedirect = getSource => host.setGetSourceOfProjectReferenceRedirect!(getSource); + if (host.setResolvedProjectReferenceCallbacks) { + compilerHost.setResolvedProjectReferenceCallbacks = callbacks => host.setResolvedProjectReferenceCallbacks!(callbacks); } if (host.useSourceInsteadOfReferenceRedirect) { compilerHost.useSourceInsteadOfReferenceRedirect = () => host.useSourceInsteadOfReferenceRedirect!(); diff --git a/src/services/types.ts b/src/services/types.ts index 3c9509ca5e9..b4fc25e587f 100644 --- a/src/services/types.ts +++ b/src/services/types.ts @@ -237,7 +237,7 @@ namespace ts { /* @internal */ getSourceFileLike?(fileName: string): SourceFileLike | undefined; /* @internal */ - setGetSourceOfProjectReferenceRedirect?(getSource: GetSourceOfProjectReferenceRedirect): void; + setResolvedProjectReferenceCallbacks?(callbacks: ResolvedProjectReferenceCallbacks): void; /* @internal */ useSourceInsteadOfReferenceRedirect?(): boolean; } diff --git a/src/testRunner/unittests/tsserver/projectReferences.ts b/src/testRunner/unittests/tsserver/projectReferences.ts index 5320cfe13bc..0b8b074f8e2 100644 --- a/src/testRunner/unittests/tsserver/projectReferences.ts +++ b/src/testRunner/unittests/tsserver/projectReferences.ts @@ -727,6 +727,36 @@ ${dependencyTs.content}`); /*afterActionDocumentPositionMapperNotEquals*/ undefined, /*useDepedencyChange*/ true ); + + it("when d.ts file is not generated", () => { + const host = createServerHost(files); + const session = createSession(host); + openFilesForSession([...openFiles, randomFile], session); + + const expectedClosedInfos = closedInfos.filter(f => f.toLowerCase() !== dtsPath && f.toLowerCase() !== dtsMapPath); + // If closed infos includes dts and dtsMap, watch dts since its not present + const expectedWatchedFiles = closedInfos.length === expectedClosedInfos.length ? + otherWatchedFiles : + otherWatchedFiles.concat(dtsPath); + // Main scenario action + verifyAllFnActionWorker(session, ({ reqName, response, expectedResponse }) => { + assert.deepEqual(response, expectedResponse, `Failed on ${reqName}`); + verifyInfosWithRandom(session, host, openInfos, expectedClosedInfos, expectedWatchedFiles); + verifyDocumentPositionMapper(session, /*dependencyMap*/ undefined, /*documentPositionMapper*/ undefined); + }, /*dtsAbsent*/ true); + checkProject(session); + + // Collecting at this point retains dependency.d.ts and map + closeFilesForSession([randomFile], session); + openFilesForSession([randomFile], session); + verifyInfosWithRandom(session, host, openInfos, expectedClosedInfos, expectedWatchedFiles); + verifyDocumentPositionMapper(session, /*dependencyMap*/ undefined, /*documentPositionMapper*/ undefined); + + // Closing open file, removes dependencies too + closeFilesForSession([...openFiles, randomFile], session); + openFilesForSession([randomFile], session); + verifyOnlyRandomInfos(session, host); + }); } } From f9e4b91203a0bcbddcca2acee442bb4f5ca0a869 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Mon, 1 Jul 2019 13:37:35 -0700 Subject: [PATCH 13/97] Fix incorrectly exported type --- src/compiler/types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 3cf506b0d58..ff0aba6acad 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -5178,7 +5178,7 @@ namespace ts { export type SourceOfProjectReferenceRedirect = string | true; /*@internal*/ - interface ResolvedProjectReferenceCallbacks { + export interface ResolvedProjectReferenceCallbacks { getSourceOfProjectReferenceRedirect(fileName: string): SourceOfProjectReferenceRedirect | undefined; forEachResolvedProjectReference(cb: (resolvedProjectReference: ResolvedProjectReference | undefined, resolvedProjectReferencePath: Path) => T | undefined): T | undefined; } From f7ea0bab60d4198c04f81de624ea61b6d75bcc9c Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Mon, 1 Jul 2019 14:29:32 -0700 Subject: [PATCH 14/97] Refactoring --- src/compiler/program.ts | 7 ++++++- src/compiler/types.ts | 2 ++ src/compiler/utilities.ts | 4 ---- src/server/editorServices.ts | 2 +- src/server/project.ts | 8 +++++--- src/server/session.ts | 2 +- src/services/services.ts | 1 - src/services/sourcemaps.ts | 4 +--- 8 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 9da4b2efe06..78e5f314879 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -814,7 +814,7 @@ namespace ts { let projectReferenceRedirects: Map | undefined; let mapFromFileToProjectReferenceRedirects: Map | undefined; let mapFromToProjectReferenceRedirectSource: Map | undefined; - const useSourceOfReference = useSourceInsteadOfReferenceRedirect(host); + const useSourceOfReference = !!host.useSourceInsteadOfReferenceRedirect && host.useSourceInsteadOfReferenceRedirect(); const shouldCreateNewSourceFile = shouldProgramCreateNewSourceFiles(oldProgram, options); const structuralIsReused = tryReuseStructureFromOldProgram(); @@ -964,6 +964,7 @@ namespace ts { getResolvedProjectReferenceToRedirect, getResolvedProjectReferenceByPath, forEachResolvedProjectReference, + isSourceOfProjectReferenceRedirect, emitBuildInfo }; @@ -2496,6 +2497,10 @@ namespace ts { return mapFromToProjectReferenceRedirectSource.get(toPath(file)); } + function isSourceOfProjectReferenceRedirect(fileName: string) { + return useSourceOfReference && !!getResolvedProjectReferenceToRedirect(fileName); + } + function forEachProjectReference( projectReferences: ReadonlyArray | undefined, resolvedProjectReferences: ReadonlyArray | undefined, diff --git a/src/compiler/types.ts b/src/compiler/types.ts index ff0aba6acad..16ea47beecc 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2993,6 +2993,7 @@ namespace ts { /*@internal*/ getResolvedProjectReferenceToRedirect(fileName: string): ResolvedProjectReference | undefined; /*@internal*/ forEachResolvedProjectReference(cb: (resolvedProjectReference: ResolvedProjectReference | undefined, resolvedProjectReferencePath: Path) => T | undefined): T | undefined; /*@internal*/ getResolvedProjectReferenceByPath(projectReferencePath: Path): ResolvedProjectReference | undefined; + /*@internal*/ isSourceOfProjectReferenceRedirect(fileName: string): boolean; /*@internal*/ getProgramBuildInfo?(): ProgramBuildInfo | undefined; /*@internal*/ emitBuildInfo(writeFile?: WriteFileCallback, cancellationToken?: CancellationToken): EmitResult; } @@ -3090,6 +3091,7 @@ namespace ts { getSourceFile(fileName: string): SourceFile | undefined; getResolvedTypeReferenceDirectives(): ReadonlyMap; getProjectReferenceRedirect(fileName: string): string | undefined; + isSourceOfProjectReferenceRedirect(fileName: string): boolean; readonly redirectTargetsMap: RedirectTargetsMap; } diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 47877e1b27f..562d066b64e 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -4616,10 +4616,6 @@ namespace ts { return false; } } - - export function useSourceInsteadOfReferenceRedirect(host: { useSourceInsteadOfReferenceRedirect?(): boolean; }) { - return host.useSourceInsteadOfReferenceRedirect && host.useSourceInsteadOfReferenceRedirect(); - } } namespace ts { diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index e379d1fda2c..6fea0bc4171 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -2570,7 +2570,7 @@ namespace ts.server { /*@internal*/ getOriginalLocationEnsuringConfiguredProject(project: Project, location: DocumentPosition): DocumentPosition | undefined { - const originalLocation = useSourceInsteadOfReferenceRedirect(project) && project.getResolvedProjectReferenceToRedirect(location.fileName) ? + const originalLocation = project.isSourceOfProjectReferenceRedirect(location.fileName) ? location : project.getSourceMapper().tryGetSourcePosition(location); if (!originalLocation) return undefined; diff --git a/src/server/project.ts b/src/server/project.ts index 4e038b94773..cd5a3fec855 100644 --- a/src/server/project.ts +++ b/src/server/project.ts @@ -196,9 +196,6 @@ namespace ts.server { /*@internal*/ originalConfiguredProjects: Map | undefined; - /*@internal*/ - useSourceInsteadOfReferenceRedirect?: () => boolean; - /*@internal*/ getResolvedProjectReferenceToRedirect(_fileName: string): ResolvedProjectReference | undefined { return undefined; @@ -1231,6 +1228,11 @@ namespace ts.server { this.rootFilesMap.delete(info.path); } + /*@internal*/ + isSourceOfProjectReferenceRedirect(fileName: string) { + return !!this.program && this.program.isSourceOfProjectReferenceRedirect(fileName); + } + protected enableGlobalPlugins(options: CompilerOptions, pluginConfigOverrides: Map | undefined) { const host = this.projectService.host; diff --git a/src/server/session.ts b/src/server/session.ts index 088a3a1a249..ff339b06a81 100644 --- a/src/server/session.ts +++ b/src/server/session.ts @@ -443,7 +443,7 @@ namespace ts.server { function getDefinitionInProject(definition: DocumentPosition | undefined, definingProject: Project, project: Project): DocumentPosition | undefined { if (!definition || project.containsFile(toNormalizedPath(definition.fileName))) return definition; - const mappedDefinition = useSourceInsteadOfReferenceRedirect(definingProject) && definingProject.getResolvedProjectReferenceToRedirect(definition.fileName) ? + const mappedDefinition = definingProject.isSourceOfProjectReferenceRedirect(definition.fileName) ? definition : definingProject.getLanguageService().getSourceMapper().tryGetGeneratedPosition(definition); return mappedDefinition && project.containsFile(toNormalizedPath(mappedDefinition.fileName)) ? mappedDefinition : undefined; diff --git a/src/services/services.ts b/src/services/services.ts index 7de7934653e..2f9ba6eec94 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1151,7 +1151,6 @@ namespace ts { fileExists: maybeBind(host, host.fileExists), readFile: maybeBind(host, host.readFile), getDocumentPositionMapper: maybeBind(host, host.getDocumentPositionMapper), - useSourceInsteadOfReferenceRedirect: maybeBind(host, host.useSourceInsteadOfReferenceRedirect), getSourceFileLike: maybeBind(host, host.getSourceFileLike), log }); diff --git a/src/services/sourcemaps.ts b/src/services/sourcemaps.ts index c4c14e58686..6ab656b4236 100644 --- a/src/services/sourcemaps.ts +++ b/src/services/sourcemaps.ts @@ -17,7 +17,6 @@ namespace ts { readFile?(path: string, encoding?: string): string | undefined; getSourceFileLike?(fileName: string): SourceFileLike | undefined; getDocumentPositionMapper?(generatedFileName: string, sourceFileName?: string): DocumentPositionMapper | undefined; - /* @internal */ useSourceInsteadOfReferenceRedirect?(): boolean; log(s: string): void; } @@ -72,8 +71,7 @@ namespace ts { const program = host.getProgram()!; // If this is source file of project reference source (instead of redirect) there is no generated position - if (useSourceInsteadOfReferenceRedirect(host) && - program.getResolvedProjectReferenceToRedirect(sourceFile.fileName)) { + if (program.isSourceOfProjectReferenceRedirect(sourceFile.fileName)) { return undefined; } From b5737fc535701caff9b197b3cba6dd4c832e745f Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Tue, 2 Jul 2019 16:29:09 -0700 Subject: [PATCH 15/97] Refactor tests so its easy to edit and reason about them --- src/harness/virtualFileSystemWithWatch.ts | 4 +- src/testRunner/unittests/tsserver/helpers.ts | 4 +- .../unittests/tsserver/projectReferences.ts | 1321 +++++++++++------ 3 files changed, 835 insertions(+), 494 deletions(-) diff --git a/src/harness/virtualFileSystemWithWatch.ts b/src/harness/virtualFileSystemWithWatch.ts index f49f15cadc2..9e615a02638 100644 --- a/src/harness/virtualFileSystemWithWatch.ts +++ b/src/harness/virtualFileSystemWithWatch.ts @@ -196,8 +196,8 @@ interface Array {}` } } - export function checkWatchedFiles(host: TestServerHost, expectedFiles: string[]) { - checkMapKeys("watchedFiles", host.watchedFiles, expectedFiles); + export function checkWatchedFiles(host: TestServerHost, expectedFiles: string[], additionalInfo?: string) { + checkMapKeys(`watchedFiles:: ${additionalInfo || ""}::`, host.watchedFiles, expectedFiles); } export function checkWatchedFilesDetailed(host: TestServerHost, expectedFiles: ReadonlyMap): void; diff --git a/src/testRunner/unittests/tsserver/helpers.ts b/src/testRunner/unittests/tsserver/helpers.ts index 1c2383ab004..1c6bd4e9f59 100644 --- a/src/testRunner/unittests/tsserver/helpers.ts +++ b/src/testRunner/unittests/tsserver/helpers.ts @@ -488,8 +488,8 @@ namespace ts.projectSystem { checkArray("Open files", arrayFrom(projectService.openFiles.keys(), path => projectService.getScriptInfoForPath(path as Path)!.fileName), expectedFiles.map(file => file.path)); } - export function checkScriptInfos(projectService: server.ProjectService, expectedFiles: ReadonlyArray) { - checkArray("ScriptInfos files", arrayFrom(projectService.filenameToScriptInfo.values(), info => info.fileName), expectedFiles); + export function checkScriptInfos(projectService: server.ProjectService, expectedFiles: ReadonlyArray, additionInfo?: string) { + checkArray(`ScriptInfos files: ${additionInfo || ""}`, arrayFrom(projectService.filenameToScriptInfo.values(), info => info.fileName), expectedFiles); } export function protocolLocationFromSubstring(str: string, substring: string): protocol.Location { diff --git a/src/testRunner/unittests/tsserver/projectReferences.ts b/src/testRunner/unittests/tsserver/projectReferences.ts index 1eb7e8ecd18..c5712df2e1a 100644 --- a/src/testRunner/unittests/tsserver/projectReferences.ts +++ b/src/testRunner/unittests/tsserver/projectReferences.ts @@ -150,66 +150,17 @@ fn5(); const files = [dependencyTs, dependencyConfig, mainTs, mainConfig, libFile, randomFile, randomConfig]; - function verifyScriptInfos(session: TestSession, host: TestServerHost, openInfos: ReadonlyArray, closedInfos: ReadonlyArray, otherWatchedFiles: ReadonlyArray) { - checkScriptInfos(session.getProjectService(), openInfos.concat(closedInfos)); - checkWatchedFiles(host, closedInfos.concat(otherWatchedFiles).map(f => f.toLowerCase())); + function verifyScriptInfos(session: TestSession, host: TestServerHost, openInfos: ReadonlyArray, closedInfos: ReadonlyArray, otherWatchedFiles: ReadonlyArray, additionalInfo: string) { + checkScriptInfos(session.getProjectService(), openInfos.concat(closedInfos), additionalInfo); + checkWatchedFiles(host, closedInfos.concat(otherWatchedFiles).map(f => f.toLowerCase()), additionalInfo); } - function verifyInfosWithRandom(session: TestSession, host: TestServerHost, openInfos: ReadonlyArray, closedInfos: ReadonlyArray, otherWatchedFiles: ReadonlyArray) { - verifyScriptInfos(session, host, openInfos.concat(randomFile.path), closedInfos, otherWatchedFiles.concat(randomConfig.path)); + function verifyInfosWithRandom(session: TestSession, host: TestServerHost, openInfos: ReadonlyArray, closedInfos: ReadonlyArray, otherWatchedFiles: ReadonlyArray, reqName: string) { + verifyScriptInfos(session, host, openInfos.concat(randomFile.path), closedInfos, otherWatchedFiles.concat(randomConfig.path), reqName); } function verifyOnlyRandomInfos(session: TestSession, host: TestServerHost) { - verifyScriptInfos(session, host, [randomFile.path], [libFile.path], [randomConfig.path]); - } - - // Returns request and expected Response, expected response when no map file - interface SessionAction { - reqName: string; - request: Partial; - expectedResponse: Response; - expectedResponseNoMap?: Response; - expectedResponseNoDts?: Response; - requestDependencyChange?: Partial; - expectedResponseDependencyChange: Response; - } - function gotoDefintinionFromMainTs(fn: number): SessionAction { - const textSpan = usageSpan(fn); - const definition: protocol.FileSpan = { file: dependencyTs.path, ...declarationSpan(fn) }; - const declareSpaceLength = "declare ".length; - return { - reqName: "goToDef", - request: { - command: protocol.CommandTypes.DefinitionAndBoundSpan, - arguments: { file: mainTs.path, ...textSpan.start } - }, - expectedResponse: { - // To dependency - definitions: [definition], - textSpan - }, - expectedResponseNoMap: { - // To the dts - definitions: [{ - file: dtsPath, - start: { line: fn, offset: definition.start.offset + declareSpaceLength }, - end: { line: fn, offset: definition.end.offset + declareSpaceLength }, - contextStart: { line: fn, offset: 1 }, - contextEnd: { line: fn, offset: 37 } - }], - textSpan - }, - expectedResponseNoDts: { - // To import declaration - definitions: [{ file: mainTs.path, ...importSpan(fn) }], - textSpan - }, - expectedResponseDependencyChange: { - // Definition on fn + 1 line - definitions: [{ file: dependencyTs.path, ...declarationSpan(fn + 1) }], - textSpan - } - }; + verifyScriptInfos(session, host, [randomFile.path], [libFile.path], [randomConfig.path], "Random"); } function declarationSpan(fn: number): protocol.TextSpanWithContext { @@ -232,11 +183,93 @@ fn5(); return { start: { line: fn + 8, offset: 1 }, end: { line: fn + 8, offset: 4 } }; } - function renameFromDependencyTs(fn: number): SessionAction { + function goToDefFromMainTs(fn: number): Action { + const textSpan = usageSpan(fn); + const definition: protocol.FileSpan = { file: dependencyTs.path, ...declarationSpan(fn) }; + return { + reqName: "goToDef", + request: { + command: protocol.CommandTypes.DefinitionAndBoundSpan, + arguments: { file: mainTs.path, ...textSpan.start } + }, + expectedResponse: { + // To dependency + definitions: [definition], + textSpan + } + }; + } + + function goToDefFromMainTsWithNoMap(fn: number): Action { + const textSpan = usageSpan(fn); + const definition = declarationSpan(fn); + const declareSpaceLength = "declare ".length; + return { + reqName: "goToDef", + request: { + command: protocol.CommandTypes.DefinitionAndBoundSpan, + arguments: { file: mainTs.path, ...textSpan.start } + }, + expectedResponse: { + // To the dts + definitions: [{ + file: dtsPath, + start: { line: fn, offset: definition.start.offset + declareSpaceLength }, + end: { line: fn, offset: definition.end.offset + declareSpaceLength }, + contextStart: { line: fn, offset: 1 }, + contextEnd: { line: fn, offset: 37 } + }], + textSpan + } + }; + } + + function goToDefFromMainTsWithNoDts(fn: number): Action { + const textSpan = usageSpan(fn); + return { + reqName: "goToDef", + request: { + command: protocol.CommandTypes.DefinitionAndBoundSpan, + arguments: { file: mainTs.path, ...textSpan.start } + }, + expectedResponse: { + // To import declaration + definitions: [{ file: mainTs.path, ...importSpan(fn) }], + textSpan + } + }; + } + + function goToDefFromMainTsWithDependencyChange(fn: number): Action { + const textSpan = usageSpan(fn); + return { + reqName: "goToDef", + request: { + command: protocol.CommandTypes.DefinitionAndBoundSpan, + arguments: { file: mainTs.path, ...textSpan.start } + }, + expectedResponse: { + // Definition on fn + 1 line + definitions: [{ file: dependencyTs.path, ...declarationSpan(fn + 1) }], + textSpan + } + }; + } + + function goToDefFromMainTsProjectInfoVerifier(withRefs: boolean): ProjectInfoVerifier { + return { + openFile: mainTs, + openFileLastLine: 14, + configFile: mainConfig, + expectedProjectActualFiles: withRefs ? + [mainTs.path, libFile.path, mainConfig.path, dependencyTs.path] : + [mainTs.path, libFile.path, mainConfig.path, dtsPath] + }; + } + + function renameFromDependencyTs(fn: number): Action { const defSpan = declarationSpan(fn); const { contextStart: _, contextEnd: _1, ...triggerSpan } = defSpan; - const defSpanPlusOne = declarationSpan(fn + 1); - const { contextStart: _2, contextEnd: _3, ...triggerSpanPlusOne } = defSpanPlusOne; return { reqName: "rename", request: { @@ -256,30 +289,37 @@ fn5(); locs: [ { file: dependencyTs.path, locs: [defSpan] } ] - }, - requestDependencyChange: { - command: protocol.CommandTypes.Rename, - arguments: { file: dependencyTs.path, ...triggerSpanPlusOne.start } - }, - expectedResponseDependencyChange: { - info: { - canRename: true, - fileToRename: undefined, - displayName: `fn${fn}`, - fullDisplayName: `"${dependecyLocation}/FnS".fn${fn}`, - kind: ScriptElementKind.functionElement, - kindModifiers: "export", - triggerSpan: triggerSpanPlusOne - }, - locs: [ - { file: dependencyTs.path, locs: [defSpanPlusOne] } - ] } }; } - function renameFromDependencyTsWithBothProjectsOpen(fn: number): SessionAction { - const { reqName, request, expectedResponse, expectedResponseDependencyChange, requestDependencyChange } = renameFromDependencyTs(fn); + function renameFromDependencyTsWithDependencyChange(fn: number): Action { + const { expectedResponse: { info, locs }, ...rest } = renameFromDependencyTs(fn + 1); + + return { + ...rest, + expectedResponse: { + info: { + ...info as protocol.RenameInfoSuccess, + displayName: `fn${fn}`, + fullDisplayName: `"${dependecyLocation}/FnS".fn${fn}`, + }, + locs + } + }; + } + + function renameFromDependencyTsProjectInfoVerifier(): ProjectInfoVerifier { + return { + openFile: dependencyTs, + openFileLastLine: 6, + configFile: dependencyConfig, + expectedProjectActualFiles: [dependencyTs.path, libFile.path, dependencyConfig.path] + }; + } + + function renameFromDependencyTsWithBothProjectsOpen(fn: number): Action { + const { reqName, request, expectedResponse } = renameFromDependencyTs(fn); const { info, locs } = expectedResponse; return { reqName, @@ -296,15 +336,20 @@ fn5(); ] } ] - }, - // Only dependency result - expectedResponseNoMap: expectedResponse, - expectedResponseNoDts: expectedResponse, - requestDependencyChange, - expectedResponseDependencyChange: { - info: expectedResponseDependencyChange.info, + } + }; + } + + function renameFromDependencyTsWithBothProjectsOpenWithDependencyChange(fn: number): Action { + const { reqName, request, expectedResponse, } = renameFromDependencyTsWithDependencyChange(fn); + const { info, locs } = expectedResponse; + return { + reqName, + request, + expectedResponse: { + info, locs: [ - expectedResponseDependencyChange.locs[0], + locs[0], { file: mainTs.path, locs: [ @@ -317,401 +362,453 @@ fn5(); }; } - // Returns request and expected Response - type SessionActionGetter = (fn: number) => SessionAction; - // Open File, expectedProjectActualFiles, actionGetter, openFileLastLine - interface DocumentPositionMapperVerifier { + interface Action { + reqName: string; + request: Partial; + expectedResponse: Response; + } + interface ActionInfo { + action: (fn: number) => Action; + closedInfos: () => readonly string[]; + otherWatchedFiles: () => readonly string[]; + expectsDts: boolean; + expectsMap: boolean; + } + type ActionKey = keyof ActionInfoVerifier; + type ActionInfoGetterFn = () => ActionInfo; + type ActionInfoGetter = ActionInfoGetterFn | ActionKey; + interface ProjectInfoVerifier { openFile: File; - expectedProjectActualFiles: ReadonlyArray; - actionGetter: SessionActionGetter; openFileLastLine: number; + configFile: File; + expectedProjectActualFiles: readonly string[]; + } + interface ActionInfoVerifier { + main: ActionInfoGetter; + noMap: ActionInfoGetter; + mapFileCreated: ActionInfoGetter; + mapFileDeleted: ActionInfoGetter; + noDts: ActionInfoGetter; + dtsFileCreated: ActionInfoGetter; + dtsFileDeleted: ActionInfoGetter; + dependencyChange: ActionInfoGetter; + noBuild: ActionInfoGetter; + } + interface DocumentPositionMapperVerifier extends ProjectInfoVerifier, ActionInfoVerifier { } - function verifyDocumentPositionMapperUpdates( - mainScenario: string, - verifier: ReadonlyArray, - closedInfos: ReadonlyArray, - withRefs: boolean) { - const openFiles = verifier.map(v => v.openFile); - const expectedProjectActualFiles = verifier.map(v => v.expectedProjectActualFiles); - const openFileLastLines = verifier.map(v => v.openFileLastLine); + interface VerifierAndWithRefs { + withRefs: boolean; + verifier: (withRefs: boolean) => readonly DocumentPositionMapperVerifier[]; + } - const configFiles = openFiles.map(openFile => `${getDirectoryPath(openFile.path)}/tsconfig.json`); - const openInfos = openFiles.map(f => f.path); - // When usage and dependency are used, dependency config is part of closedInfo so ignore - const otherWatchedFiles = withRefs && verifier.length > 1 ? [configFiles[0]] : configFiles; - function openTsFile(onHostCreate?: (host: TestServerHost) => void) { - const host = createHost(files, [mainConfig.path]); - if (!withRefs) { - // Erase project reference - host.writeFile(mainConfig.path, JSON.stringify({ - compilerOptions: { composite: true, declarationMap: true } - })); - } - if (onHostCreate) { - onHostCreate(host); - } - const session = createSession(host); - openFilesForSession([...openFiles, randomFile], session); - return { host, session }; + function openFiles(verifiers: readonly DocumentPositionMapperVerifier[]) { + return verifiers.map(v => v.openFile); + } + interface OpenTsFile extends VerifierAndWithRefs { + onHostCreate?: (host: TestServerHost) => void; + } + function openTsFile({ withRefs, verifier, onHostCreate }: OpenTsFile) { + const host = createHost(files, [mainConfig.path]); + if (!withRefs) { + // Erase project reference + host.writeFile(mainConfig.path, JSON.stringify({ + compilerOptions: { composite: true, declarationMap: true } + })); } - - function checkProject(session: TestSession, noDts?: true) { - const service = session.getProjectService(); - checkNumberOfProjects(service, { configuredProjects: 1 + verifier.length }); - configFiles.forEach((configFile, index) => { - checkProjectActualFiles( - service.configuredProjects.get(configFile)!, - noDts ? - expectedProjectActualFiles[index].filter(f => f.toLowerCase() !== dtsPath) : - expectedProjectActualFiles[index] - ); - }); + if (onHostCreate) { + onHostCreate(host); } + const session = createSession(host); + const verifiers = verifier(withRefs); + openFilesForSession([...openFiles(verifiers), randomFile], session); + return { host, session, verifiers }; + } - function verifyInfos(session: TestSession, host: TestServerHost) { - verifyInfosWithRandom(session, host, openInfos, closedInfos, otherWatchedFiles); - } - - function verifyInfosWhenNoMapFile(session: TestSession, host: TestServerHost, dependencyTsOK?: boolean) { - const dtsMapClosedInfo = firstDefined(closedInfos, f => f.toLowerCase() === dtsMapPath ? f : undefined); - verifyInfosWithRandom( - session, - host, - openInfos, - closedInfos.filter(f => f !== dtsMapClosedInfo && (dependencyTsOK || f !== dependencyTs.path)), - dtsMapClosedInfo ? otherWatchedFiles.concat(dtsMapClosedInfo) : otherWatchedFiles + function checkProject(session: TestSession, verifiers: readonly DocumentPositionMapperVerifier[], noDts?: true) { + const service = session.getProjectService(); + checkNumberOfProjects(service, { configuredProjects: 1 + verifiers.length }); + verifiers.forEach(({ configFile, expectedProjectActualFiles }) => { + checkProjectActualFiles( + service.configuredProjects.get(configFile.path.toLowerCase())!, + noDts ? + expectedProjectActualFiles.filter(f => f.toLowerCase() !== dtsPath) : + expectedProjectActualFiles ); - } + }); + } - function verifyInfosWhenNoDtsFile(session: TestSession, host: TestServerHost, watchDts: boolean, dependencyTsOk?: boolean, depedencyMapOk?: boolean) { - const dtsMapClosedInfo = firstDefined(closedInfos, f => f.toLowerCase() === dtsMapPath ? f : undefined); - const dtsClosedInfo = firstDefined(closedInfos, f => f.toLowerCase() === dtsPath ? f : undefined); - verifyInfosWithRandom( - session, - host, - openInfos, - closedInfos.filter(f => (depedencyMapOk || f !== dtsMapClosedInfo) && f !== dtsClosedInfo && (dependencyTsOk || f !== dependencyTs.path)), - dtsClosedInfo && watchDts ? - otherWatchedFiles.concat(dtsClosedInfo) : - otherWatchedFiles - ); + function firstAction(session: TestSession, verifiers: readonly DocumentPositionMapperVerifier[]) { + for (const { action } of getActionInfo(verifiers, "main")) { + const { request } = action(1); + session.executeCommandSeq(request); } + } - function verifyDocumentPositionMapper(session: TestSession, dependencyMap: server.ScriptInfo | undefined, documentPositionMapper: server.ScriptInfo["documentPositionMapper"], notEqual?: true) { - assert.strictEqual(session.getProjectService().filenameToScriptInfo.get(dtsMapPath), dependencyMap); - if (dependencyMap) { - if (notEqual) { - assert.notStrictEqual(dependencyMap.documentPositionMapper, documentPositionMapper); - } - else { - assert.strictEqual(dependencyMap.documentPositionMapper, documentPositionMapper); - } + function verifyAction(session: TestSession, { reqName, request, expectedResponse }: Action) { + const { response } = session.executeCommandSeq(request); + assert.deepEqual(response, expectedResponse, `Failed Request: ${reqName}`); + } + + function verifyScriptInfoPresence(session: TestSession, path: string, expectedToBePresent: boolean, reqName: string) { + const info = session.getProjectService().filenameToScriptInfo.get(path); + if (expectedToBePresent) { + assert.isDefined(info, `${reqName}:: ${path} expected to be present`); + } + else { + assert.isUndefined(info, `${reqName}:: ${path} expected to be not present`); + } + return info; + } + + function verifyDocumentPositionMapper(session: TestSession, dependencyMap: server.ScriptInfo | undefined, documentPositionMapper: server.ScriptInfo["documentPositionMapper"], equal: boolean) { + assert.strictEqual(session.getProjectService().filenameToScriptInfo.get(dtsMapPath), dependencyMap); + if (dependencyMap) { + if (equal) { + assert.strictEqual(dependencyMap.documentPositionMapper, documentPositionMapper); + } + else { + assert.notStrictEqual(dependencyMap.documentPositionMapper, documentPositionMapper); } } + } - function action(verifier: DocumentPositionMapperVerifier, fn: number, session: TestSession, useDependencyChange?: boolean) { - const { reqName, request, expectedResponse, expectedResponseNoMap, expectedResponseNoDts, requestDependencyChange, expectedResponseDependencyChange } = verifier.actionGetter(fn); - const { response } = session.executeCommandSeq(useDependencyChange ? requestDependencyChange || request : request); - return { reqName, response, expectedResponse, expectedResponseNoMap, expectedResponseNoDts, expectedResponseDependencyChange, verifier }; - } - - function firstAction(session: TestSession) { - verifier.forEach(v => action(v, 1, session)); - } - - function verifyAllFnActionWorker(session: TestSession, verifyAction: (result: ReturnType, dtsInfo: server.ScriptInfo | undefined, isFirst: boolean) => void, dtsAbsent?: boolean, useDependencyChange?: boolean) { - // action - let isFirst = true; - for (const v of verifier) { - for (let fn = 1; fn <= 5; fn++) { - const result = action(v, fn, session, useDependencyChange); - const dtsInfo = session.getProjectService().filenameToScriptInfo.get(dtsPath); - if (dtsAbsent) { - assert.isUndefined(dtsInfo); - } - else { - assert.isDefined(dtsInfo); - } - verifyAction(result, dtsInfo, isFirst); - isFirst = false; - } + function getActionInfo(verifiers: readonly DocumentPositionMapperVerifier[], actionKey: ActionKey): ActionInfo[] { + return verifiers.map(v => { + let actionInfoGetter = v[actionKey]; + while (isString(actionInfoGetter)) { + actionInfoGetter = v[actionInfoGetter]; } - } + return actionInfoGetter(); + }); + } - function dtsAbsent() { - return withRefs && !contains(closedInfos, dtsPath, (a, b) => a.toLowerCase() === b.toLowerCase()); - } - - function verifyAllFnAction( - session: TestSession, - host: TestServerHost, - firstDocumentPositionMapperNotEquals?: true, - dependencyMap?: server.ScriptInfo, - documentPositionMapper?: server.ScriptInfo["documentPositionMapper"], - useDependencyChange?: boolean - ) { - // action - verifyAllFnActionWorker(session, ({ reqName, response, expectedResponse, expectedResponseDependencyChange }, dtsInfo, isFirst) => { - assert.deepEqual(response, useDependencyChange ? expectedResponseDependencyChange || expectedResponse : expectedResponse, `Failed on ${reqName}`); - verifyInfos(session, host); - if (dtsInfo) assert.equal(dtsInfo.sourceMapFilePath, dtsMapPath); - if (isFirst) { - if (dependencyMap) { - verifyDocumentPositionMapper(session, dependencyMap, documentPositionMapper, firstDocumentPositionMapperNotEquals); - documentPositionMapper = dependencyMap.documentPositionMapper; - } - else { - dependencyMap = session.getProjectService().filenameToScriptInfo.get(dtsMapPath); - documentPositionMapper = dependencyMap && dependencyMap.documentPositionMapper; - } - } - else { - verifyDocumentPositionMapper(session, dependencyMap, documentPositionMapper); - } - }, dtsAbsent(), useDependencyChange); - return { dependencyMap, documentPositionMapper }; - } - - function verifyAllFnActionWithNoMap( - session: TestSession, - host: TestServerHost, - dependencyTsOK?: true - ) { - let sourceMapFilePath: server.ScriptInfo["sourceMapFilePath"]; - // action - verifyAllFnActionWorker(session, ({ reqName, response, expectedResponse, expectedResponseNoMap }, dtsInfo, isFirst) => { - assert.deepEqual(response, withRefs ? expectedResponse : expectedResponseNoMap || expectedResponse, `Failed on ${reqName}`); - verifyInfosWhenNoMapFile(session, host, dependencyTsOK); - assert.isUndefined(session.getProjectService().filenameToScriptInfo.get(dtsMapPath)); - if (!withRefs) { - if (isFirst) { - assert.isNotString(dtsInfo!.sourceMapFilePath); - assert.isNotFalse(dtsInfo!.sourceMapFilePath); - assert.isDefined(dtsInfo!.sourceMapFilePath); - sourceMapFilePath = dtsInfo!.sourceMapFilePath; - } - else { - assert.equal(dtsInfo!.sourceMapFilePath, sourceMapFilePath); - } - } - }, dtsAbsent()); - return sourceMapFilePath; - } - - function verifyAllFnActionWithNoDts( - session: TestSession, - host: TestServerHost, - dependencyTsAndMapOk?: true - ) { - // action - verifyAllFnActionWorker(session, ({ reqName, response, expectedResponse, expectedResponseNoDts, verifier }) => { - assert.deepEqual(response, withRefs ? expectedResponse : expectedResponseNoDts || expectedResponse, `Failed on ${reqName}`); - verifyInfosWhenNoDtsFile( + interface VerifyAllFnAction { + session: TestSession; + host: TestServerHost; + verifiers: readonly DocumentPositionMapperVerifier[]; + actionKey: ActionKey; + sourceMapPath?: server.ScriptInfo["sourceMapFilePath"]; + dependencyMap?: server.ScriptInfo | undefined; + documentPositionMapper?: server.ScriptInfo["documentPositionMapper"]; + firstEquals?: boolean; + } + interface VerifyAllFnActionResult { + actionInfos: readonly ActionInfo[]; + actionKey: ActionKey; + dependencyMap: server.ScriptInfo | undefined; + documentPositionMapper: server.ScriptInfo["documentPositionMapper"] | undefined; + } + function verifyAllFnAction({ + session, + host, + verifiers, + actionKey, + dependencyMap, + documentPositionMapper, + firstEquals + }: VerifyAllFnAction): VerifyAllFnActionResult { + const actionInfos = getActionInfo(verifiers, actionKey); + let sourceMapPath: server.ScriptInfo["sourceMapFilePath"] | undefined; + // action + let first = true; + for (const { action, closedInfos, otherWatchedFiles, expectsDts, expectsMap } of actionInfos) { + for (let fn = 1; fn <= 5; fn++) { + const fnAction = action(fn); + verifyAction(session, fnAction); + const dtsInfo = verifyScriptInfoPresence(session, dtsPath, expectsDts, fnAction.reqName); + const dtsMapInfo = verifyScriptInfoPresence(session, dtsMapPath, expectsMap, fnAction.reqName); + verifyInfosWithRandom( session, host, - // Even when project actual file contains dts, its not watched because the dts is in another folder and module resolution just fails - // instead of succeeding to source file and then mapping using project reference (When using usage location) - // But watched if sourcemapper is in source project since we need to keep track of dts to update the source mapper for any potential usages - verifier.expectedProjectActualFiles.every(f => f.toLowerCase() !== dtsPath), - /*dependencyTsOk*/ withRefs || dependencyTsAndMapOk, - /*dependencyMapOk*/ dependencyTsAndMapOk + openFiles(verifiers).map(f => f.path), + closedInfos(), + otherWatchedFiles(), + `${actionKey}:: ${fnAction.reqName}` ); - }, /*dtsAbsent*/ true); + + if (dtsInfo) { + if (first) { + if (dtsMapInfo) { + assert.equal(dtsInfo.sourceMapFilePath, dtsMapPath, `${actionKey}:: ${fnAction.reqName}`); + } + else { + assert.isNotString(dtsInfo.sourceMapFilePath); + assert.isNotFalse(dtsInfo.sourceMapFilePath); + assert.isDefined(dtsInfo.sourceMapFilePath); + } + } + else { + assert.equal(dtsInfo.sourceMapFilePath, sourceMapPath, `${actionKey}:: ${fnAction.reqName}`); + } + } + if (!first || firstEquals !== undefined) { + verifyDocumentPositionMapper(session, dependencyMap, documentPositionMapper, !first || !!firstEquals); + } + sourceMapPath = dtsInfo && dtsInfo.sourceMapFilePath; + dependencyMap = dtsMapInfo; + documentPositionMapper = dependencyMap && dependencyMap.documentPositionMapper; + first = false; + } } - function verifyScenarioWithChangesWorker( - change: (host: TestServerHost, session: TestSession) => void, - afterActionDocumentPositionMapperNotEquals: true | undefined, - timeoutBeforeAction: boolean, - useDependencyChange?: boolean - ) { - const { host, session } = openTsFile(); + return { actionInfos, actionKey, dependencyMap, documentPositionMapper }; + } + + function verifyScriptInfoCollection( + session: TestSession, + host: TestServerHost, + verifiers: readonly DocumentPositionMapperVerifier[], + { dependencyMap, documentPositionMapper, actionInfos, actionKey }: VerifyAllFnActionResult + ) { + // Collecting at this point retains dependency.d.ts and map + closeFilesForSession([randomFile], session); + openFilesForSession([randomFile], session); + + const { closedInfos, otherWatchedFiles } = last(actionInfos); + verifyInfosWithRandom( + session, + host, + openFiles(verifiers).map(f => f.path), + closedInfos(), + otherWatchedFiles(), + `${actionKey} Collection` + ); + verifyDocumentPositionMapper(session, dependencyMap, documentPositionMapper, /*equal*/ true); + + // Closing open file, removes dependencies too + closeFilesForSession([...openFiles(verifiers), randomFile], session); + openFilesForSession([randomFile], session); + verifyOnlyRandomInfos(session, host); + } + + function verifyScenarioAndScriptInfoCollection( + session: TestSession, + host: TestServerHost, + verifiers: readonly DocumentPositionMapperVerifier[], + actionKey: ActionKey, + noDts?: true + ) { + // Main scenario action + const result = verifyAllFnAction({ session, host, verifiers, actionKey }); + checkProject(session, verifiers, noDts); + verifyScriptInfoCollection(session, host, verifiers, result); + } + + function verifyScenarioWithChangesWorker( + { + scenarioName, + verifier, + withRefs, + change, + afterActionDocumentPositionMapperNotEquals, + afterChangeActionKey + }: VerifyScenarioWithChanges, + timeoutBeforeAction: boolean, + ) { + it(scenarioName, () => { + const { host, session, verifiers } = openTsFile({ verifier, withRefs }); // Create DocumentPositionMapper - firstAction(session); + firstAction(session, verifiers); const dependencyMap = session.getProjectService().filenameToScriptInfo.get(dtsMapPath); const documentPositionMapper = dependencyMap && dependencyMap.documentPositionMapper; // change - change(host, session); + change(host, session, verifiers); if (timeoutBeforeAction) { host.runQueuedTimeoutCallbacks(); - checkProject(session); - verifyDocumentPositionMapper(session, dependencyMap, documentPositionMapper); + checkProject(session, verifiers); + verifyDocumentPositionMapper(session, dependencyMap, documentPositionMapper, /*equal*/ true); } // action - verifyAllFnAction(session, host, afterActionDocumentPositionMapperNotEquals, dependencyMap, documentPositionMapper, useDependencyChange); - } - - function verifyScenarioWithChanges( - scenarioName: string, - change: (host: TestServerHost, session: TestSession) => void, - afterActionDocumentPositionMapperNotEquals?: true, - useDependencyChange?: boolean - ) { - describe(scenarioName, () => { - it("when timeout occurs before request", () => { - verifyScenarioWithChangesWorker(change, afterActionDocumentPositionMapperNotEquals, /*timeoutBeforeAction*/ true, useDependencyChange); - }); - - it("when timeout does not occur before request", () => { - verifyScenarioWithChangesWorker(change, afterActionDocumentPositionMapperNotEquals, /*timeoutBeforeAction*/ false, useDependencyChange); - }); - }); - } - - function verifyMainScenarioAndScriptInfoCollection(session: TestSession, host: TestServerHost) { - // Main scenario action - const { dependencyMap, documentPositionMapper } = verifyAllFnAction(session, host); - checkProject(session); - verifyInfos(session, host); - - // Collecting at this point retains dependency.d.ts and map - closeFilesForSession([randomFile], session); - openFilesForSession([randomFile], session); - verifyInfos(session, host); - verifyDocumentPositionMapper(session, dependencyMap, documentPositionMapper); - - // Closing open file, removes dependencies too - closeFilesForSession([...openFiles, randomFile], session); - openFilesForSession([randomFile], session); - verifyOnlyRandomInfos(session, host); - } - - function verifyMainScenarioAndScriptInfoCollectionWithNoMap(session: TestSession, host: TestServerHost, dependencyTsOKInScenario?: true) { - // Main scenario action - verifyAllFnActionWithNoMap(session, host, withRefs || dependencyTsOKInScenario); - - // Collecting at this point retains dependency.d.ts and map watcher - closeFilesForSession([randomFile], session); - openFilesForSession([randomFile], session); - verifyInfosWhenNoMapFile(session, host, withRefs); - - // Closing open file, removes dependencies too - closeFilesForSession([...openFiles, randomFile], session); - openFilesForSession([randomFile], session); - verifyOnlyRandomInfos(session, host); - } - - function verifyMainScenarioAndScriptInfoCollectionWithNoDts(session: TestSession, host: TestServerHost, dependencyTsAndMapOk?: true) { - // Main scenario action - verifyAllFnActionWithNoDts(session, host, dependencyTsAndMapOk); - - // Collecting at this point retains dependency.d.ts and map watcher - closeFilesForSession([randomFile], session); - openFilesForSession([randomFile], session); - verifyInfosWhenNoDtsFile( + verifyAllFnAction({ session, host, - !!forEach(verifier, v => v.expectedProjectActualFiles.every(f => f.toLowerCase() !== dtsPath)), - /*dependencyTsOk*/ withRefs - ); + verifiers, + actionKey: afterChangeActionKey, + dependencyMap, + documentPositionMapper, + firstEquals: !afterActionDocumentPositionMapperNotEquals + }); + }); + } - // Closing open file, removes dependencies too - closeFilesForSession([...openFiles, randomFile], session); - openFilesForSession([randomFile], session); - verifyOnlyRandomInfos(session, host); - } + interface VerifyScenarioWithChanges extends VerifierAndWithRefs { + scenarioName: string; + change: (host: TestServerHost, session: TestSession, verifiers: readonly DocumentPositionMapperVerifier[]) => void; + afterActionDocumentPositionMapperNotEquals?: true; + afterChangeActionKey: ActionKey; + } + function verifyScenarioWithChanges(verify: VerifyScenarioWithChanges) { + describe("when timeout occurs before request", () => { + verifyScenarioWithChangesWorker(verify, /*timeoutBeforeAction*/ true); + }); - function verifyScenarioWhenFileNotPresent( - scenarioName: string, - fileLocation: string, - verifyScenarioAndScriptInfoCollection: (session: TestSession, host: TestServerHost, dependencyTsOk?: true) => void, - noDts?: true - ) { - describe(scenarioName, () => { - it(mainScenario, () => { - const { host, session } = openTsFile(host => host.deleteFile(fileLocation)); - checkProject(session, noDts); + describe("when timeout does not occur before request", () => { + verifyScenarioWithChangesWorker(verify, /*timeoutBeforeAction*/ false); + }); + } - verifyScenarioAndScriptInfoCollection(session, host); + interface VerifyScenarioWhenFileNotPresent extends VerifierAndWithRefs { + scenarioName: string; + fileLocation: string; + fileNotPresentKey: ActionKey; + fileCreatedKey: ActionKey; + fileDeletedKey: ActionKey; + noDts?: true; + } + function verifyScenarioWhenFileNotPresent({ + scenarioName, + verifier, + withRefs, + fileLocation, + fileNotPresentKey, + fileCreatedKey, + fileDeletedKey, + noDts + }: VerifyScenarioWhenFileNotPresent) { + describe(scenarioName, () => { + it("when file is not present", () => { + const { host, session, verifiers } = openTsFile({ + verifier, + withRefs, + onHostCreate: host => host.deleteFile(fileLocation) }); + checkProject(session, verifiers, noDts); - it("when file is created", () => { - let fileContents: string | undefined; - const { host, session } = openTsFile(host => { + verifyScenarioAndScriptInfoCollection(session, host, verifiers, fileNotPresentKey, noDts); + }); + + it("when file is created after actions on projects", () => { + let fileContents: string | undefined; + const { host, session, verifiers } = openTsFile({ + verifier, + withRefs, + onHostCreate: host => { fileContents = host.readFile(fileLocation); host.deleteFile(fileLocation); - }); - firstAction(session); - - host.writeFile(fileLocation, fileContents!); - verifyMainScenarioAndScriptInfoCollection(session, host); + } }); + firstAction(session, verifiers); - it("when file is deleted", () => { - const { host, session } = openTsFile(); - firstAction(session); - - // The dependency file is deleted when orphan files are collected - host.deleteFile(fileLocation); - verifyScenarioAndScriptInfoCollection(session, host, /*dependencyTsOk*/ true); - }); + host.writeFile(fileLocation, fileContents!); + verifyScenarioAndScriptInfoCollection(session, host, verifiers, fileCreatedKey); }); - } + it("when file is deleted after actions on the projects", () => { + const { host, session, verifiers } = openTsFile({ verifier, withRefs }); + firstAction(session, verifiers); + + // The dependency file is deleted when orphan files are collected + host.deleteFile(fileLocation); + // Verify with deleted action key + const result = verifyAllFnAction({ session, host, verifiers, actionKey: fileDeletedKey }); + checkProject(session, verifiers, noDts); + + // Script info collection should behave as fileNotPresentKey + verifyScriptInfoCollection( + session, + host, + verifiers, + { + actionInfos: getActionInfo(verifiers, fileNotPresentKey), + actionKey: result.actionKey, + dependencyMap: undefined, + documentPositionMapper: undefined + } + ); + }); + }); + } + + function verifyScenarioWorker({ mainScenario, verifier }: VerifyScenario, withRefs: boolean) { it(mainScenario, () => { - const { host, session } = openTsFile(); - checkProject(session); - - verifyMainScenarioAndScriptInfoCollection(session, host); + const { host, session, verifiers } = openTsFile({ withRefs, verifier }); + checkProject(session, verifiers); + verifyScenarioAndScriptInfoCollection(session, host, verifiers, "main"); }); // Edit - verifyScenarioWithChanges( - "when usage file changes, document position mapper doesnt change", - (_host, session) => openFiles.forEach( - (openFile, index) => session.executeCommandSeq({ + verifyScenarioWithChanges({ + scenarioName: "when usage file changes, document position mapper doesnt change", + verifier, + withRefs, + change: (_host, session, verifiers) => verifiers.forEach( + verifier => session.executeCommandSeq({ command: protocol.CommandTypes.Change, - arguments: { file: openFile.path, line: openFileLastLines[index], offset: 1, endLine: openFileLastLines[index], endOffset: 1, insertString: "const x = 10;" } + arguments: { + file: verifier.openFile.path, + line: verifier.openFileLastLine, + offset: 1, + endLine: verifier.openFileLastLine, + endOffset: 1, + insertString: "const x = 10;" + } }) - ) - ); + ), + afterChangeActionKey: "main" + }); // Edit dts to add new fn - verifyScenarioWithChanges( - "when dependency .d.ts changes, document position mapper doesnt change", - host => host.writeFile( + verifyScenarioWithChanges({ + scenarioName: "when dependency .d.ts changes, document position mapper doesnt change", + verifier, + withRefs, + change: host => host.writeFile( dtsLocation, host.readFile(dtsLocation)!.replace( "//# sourceMappingURL=FnS.d.ts.map", `export declare function fn6(): void; //# sourceMappingURL=FnS.d.ts.map` ) - ) - ); + ), + afterChangeActionKey: "main" + }); // Edit map file to represent added new line - verifyScenarioWithChanges( - "when dependency file's map changes", - host => host.writeFile( + verifyScenarioWithChanges({ + scenarioName: "when dependency file's map changes", + verifier, + withRefs, + change: host => host.writeFile( dtsMapLocation, `{"version":3,"file":"FnS.d.ts","sourceRoot":"","sources":["../dependency/FnS.ts"],"names":[],"mappings":"AAAA,wBAAgB,GAAG,SAAM;AACzB,wBAAgB,GAAG,SAAM;AACzB,wBAAgB,GAAG,SAAM;AACzB,wBAAgB,GAAG,SAAM;AACzB,wBAAgB,GAAG,SAAM;AACzB,eAAO,MAAM,CAAC,KAAK,CAAC"}` ), - /*afterActionDocumentPositionMapperNotEquals*/ true - ); + afterChangeActionKey: "main", + afterActionDocumentPositionMapperNotEquals: true + }); - verifyScenarioWhenFileNotPresent( - "when map file is not present", - dtsMapLocation, - verifyMainScenarioAndScriptInfoCollectionWithNoMap - ); + verifyScenarioWhenFileNotPresent({ + scenarioName: "with depedency files map file", + verifier, + withRefs, + fileLocation: dtsMapLocation, + fileNotPresentKey: "noMap", + fileCreatedKey: "mapFileCreated", + fileDeletedKey: "mapFileDeleted" + }); - verifyScenarioWhenFileNotPresent( - "when .d.ts file is not present", - dtsLocation, - verifyMainScenarioAndScriptInfoCollectionWithNoDts, - /*noDts*/ true - ); + verifyScenarioWhenFileNotPresent({ + scenarioName: "with depedency .d.ts file", + verifier, + withRefs, + fileLocation: dtsLocation, + fileNotPresentKey: "noDts", + fileCreatedKey: "dtsFileCreated", + fileDeletedKey: "dtsFileDeleted", + noDts: true + }); if (withRefs) { - verifyScenarioWithChanges( - "when defining project source changes", - (host, session) => { + verifyScenarioWithChanges({ + scenarioName: "when defining project source changes", + verifier, + withRefs, + change: (host, session, verifiers) => { // Make change, without rebuild of solution - if (contains(openInfos, dependencyTs.path)) { + if (contains(openFiles(verifiers), dependencyTs)) { session.executeCommandSeq({ command: protocol.CommandTypes.Change, arguments: { @@ -724,96 +821,340 @@ fn5(); ${dependencyTs.content}`); } }, - /*afterActionDocumentPositionMapperNotEquals*/ undefined, - /*useDepedencyChange*/ true - ); + afterChangeActionKey: "dependencyChange" + }); it("when d.ts file is not generated", () => { const host = createServerHost(files); const session = createSession(host); - openFilesForSession([...openFiles, randomFile], session); - - const expectedClosedInfos = closedInfos.filter(f => f.toLowerCase() !== dtsPath && f.toLowerCase() !== dtsMapPath); - // If closed infos includes dts and dtsMap, watch dts since its not present - const expectedWatchedFiles = closedInfos.length === expectedClosedInfos.length ? - otherWatchedFiles : - otherWatchedFiles.concat(dtsPath); - // Main scenario action - verifyAllFnActionWorker(session, ({ reqName, response, expectedResponse }) => { - assert.deepEqual(response, expectedResponse, `Failed on ${reqName}`); - verifyInfosWithRandom(session, host, openInfos, expectedClosedInfos, expectedWatchedFiles); - verifyDocumentPositionMapper(session, /*dependencyMap*/ undefined, /*documentPositionMapper*/ undefined); - }, /*dtsAbsent*/ true); - checkProject(session); - - // Collecting at this point retains dependency.d.ts and map - closeFilesForSession([randomFile], session); - openFilesForSession([randomFile], session); - verifyInfosWithRandom(session, host, openInfos, expectedClosedInfos, expectedWatchedFiles); - verifyDocumentPositionMapper(session, /*dependencyMap*/ undefined, /*documentPositionMapper*/ undefined); - - // Closing open file, removes dependencies too - closeFilesForSession([...openFiles, randomFile], session); - openFilesForSession([randomFile], session); - verifyOnlyRandomInfos(session, host); + const verifiers = verifier(withRefs); + openFilesForSession([...openFiles(verifiers), randomFile], session); + verifyScenarioAndScriptInfoCollection(session, host, verifiers, "noBuild"); }); } } - function verifyScenarios(withRefs: boolean) { - describe(withRefs ? "when main tsconfig has project reference" : "when main tsconfig doesnt have project reference", () => { - const usageVerifier: DocumentPositionMapperVerifier = { - openFile: mainTs, - expectedProjectActualFiles: withRefs ? - [mainTs.path, libFile.path, mainConfig.path, dependencyTs.path] : - [mainTs.path, libFile.path, mainConfig.path, dtsPath], - actionGetter: gotoDefintinionFromMainTs, - openFileLastLine: 14 - }; - describe("from project that uses dependency", () => { - const closedInfos = withRefs ? - [dependencyTs.path, dependencyConfig.path, libFile.path] : - [dependencyTs.path, libFile.path, dtsPath, dtsMapLocation]; - verifyDocumentPositionMapperUpdates( - "can go to definition correctly", - [usageVerifier], - closedInfos, - withRefs - ); - }); - - const definingVerifier: DocumentPositionMapperVerifier = { - openFile: dependencyTs, - expectedProjectActualFiles: [dependencyTs.path, libFile.path, dependencyConfig.path], - actionGetter: renameFromDependencyTs, - openFileLastLine: 6, - }; - describe("from defining project", () => { - const closedInfos = [libFile.path, dtsLocation, dtsMapLocation]; - verifyDocumentPositionMapperUpdates( - "rename locations from dependency", - [definingVerifier], - closedInfos, - withRefs - ); - }); - - describe("when opening depedency and usage project", () => { - const closedInfos = withRefs ? - [libFile.path, dependencyConfig.path] : - [libFile.path, dtsPath, dtsMapLocation]; - verifyDocumentPositionMapperUpdates( - "goto Definition in usage and rename locations from defining project", - [usageVerifier, { ...definingVerifier, actionGetter: renameFromDependencyTsWithBothProjectsOpen }], - closedInfos, - withRefs - ); - }); + interface VerifyScenario { + mainScenario: string; + verifier: (withRefs: boolean) => readonly DocumentPositionMapperVerifier[]; + } + function verifyScenario(scenario: VerifyScenario) { + describe("when main tsconfig doesnt have project reference", () => { + verifyScenarioWorker(scenario, /*withRefs*/ false); + }); + describe("when main tsconfig has project reference", () => { + verifyScenarioWorker(scenario, /*withRefs*/ true); }); } - verifyScenarios(/*withRefs*/ false); - verifyScenarios(/*withRefs*/ true); + describe("from project that uses dependency", () => { + function goToDefActionInfo(withRefs: boolean): ActionInfo { + return { + action: goToDefFromMainTs, + closedInfos: () => withRefs ? + [dependencyTs.path, dependencyConfig.path, libFile.path] : + [dependencyTs.path, libFile.path, dtsPath, dtsMapLocation], + otherWatchedFiles: () => [mainConfig.path], + expectsDts: !withRefs, // Dts script info present only if no project reference + expectsMap: !withRefs // Map script info present only if no project reference + }; + } + + function goToDefNoMapActionInfo(withRefs: boolean): ActionInfo { + return { + ...goToDefActionInfo(withRefs), + action: withRefs ? + goToDefFromMainTs : + goToDefFromMainTsWithNoMap, + closedInfos: () => withRefs ? + [dependencyTs.path, dependencyConfig.path, libFile.path] : + [libFile.path, dtsPath], // Because map is deleted, dts and dependency are released + otherWatchedFiles: () => withRefs ? + [mainConfig.path] : + [mainConfig.path, dtsMapPath], // Watches deleted file + expectsMap: false + }; + } + + function goToDefNoDtsActionInfo(withRefs: boolean): ActionInfo { + return { + ...goToDefActionInfo(withRefs), + action: withRefs ? + goToDefFromMainTs : + goToDefFromMainTsWithNoDts, + closedInfos: () => withRefs ? + [dependencyTs.path, dependencyConfig.path, libFile.path] : + [libFile.path], // No dts means no map, no dependency + expectsDts: false, + expectsMap: false + }; + } + + verifyScenario({ + mainScenario: "can go to definition correctly", + verifier: withRefs => [ + { + ...goToDefFromMainTsProjectInfoVerifier(withRefs), + main: () => goToDefActionInfo(withRefs), + noMap: () => goToDefNoMapActionInfo(withRefs), + mapFileCreated: "main", + mapFileDeleted: () => ({ + ...goToDefNoMapActionInfo(withRefs), + closedInfos: () => withRefs ? + [dependencyTs.path, dependencyConfig.path, libFile.path] : + // The script info for depedency is collected only after file open + [dependencyTs.path, libFile.path, dtsPath] + }), + noDts: () => goToDefNoDtsActionInfo(withRefs), + dtsFileCreated: "main", + dtsFileDeleted: () => ({ + ...goToDefNoDtsActionInfo(withRefs), + // The script info for map is collected only after file open + closedInfos: () => withRefs ? + [dependencyTs.path, dependencyConfig.path, libFile.path] : + [dependencyTs.path, libFile.path, dtsMapLocation], + expectsMap: !withRefs + }), + dependencyChange: () => ({ + ...goToDefActionInfo(withRefs), + action: goToDefFromMainTsWithDependencyChange, + expectsDts: false, + expectsMap: false + }), + noBuild: "main" + } + ] + }); + }); + + describe("from defining project", () => { + function renameActionInfo(): ActionInfo { + return { + action: renameFromDependencyTs, + closedInfos: () => [libFile.path, dtsLocation, dtsMapLocation], + otherWatchedFiles: () => [dependencyConfig.path], + expectsDts: true, + expectsMap: true + }; + } + + function renameNoMapActionInfo(): ActionInfo { + return { + ...renameActionInfo(), + closedInfos: () => [libFile.path, dtsLocation], // No map + otherWatchedFiles: () => [dependencyConfig.path, dtsMapLocation], // watch map + expectsMap: false + }; + } + + function renameNoDtsActionInfo(): ActionInfo { + return { + action: renameFromDependencyTs, + closedInfos: () => [libFile.path], // no dts or map since dts itself doesnt exist + otherWatchedFiles: () => [dependencyConfig.path, dtsPath], // watch deleted file + expectsDts: false, + expectsMap: false + }; + } + + verifyScenario({ + mainScenario: "rename locations from dependency", + verifier: () => [ + { + ...renameFromDependencyTsProjectInfoVerifier(), + main: renameActionInfo, + noMap: renameNoMapActionInfo, + mapFileCreated: "main", + mapFileDeleted: "noMap", + noDts: renameNoDtsActionInfo, + dtsFileCreated: "main", + dtsFileDeleted: () => ({ + ...renameNoDtsActionInfo(), + // Map is collected after file open + closedInfos: () => [libFile.path, dtsMapLocation], + expectsMap: true + }), + dependencyChange: () => ({ + ...renameActionInfo(), + action: renameFromDependencyTsWithDependencyChange + }), + noBuild: () => ({ + action: renameFromDependencyTs, + closedInfos: () => [libFile.path], // No dts or map since its not built/present + // Watching for creation of dts so that it can give correct results across projects + otherWatchedFiles: () => [dependencyConfig.path, dtsPath], + expectsDts: false, + expectsMap: false + }) + } + ] + }); + }); + + describe("when opening depedency and usage project", () => { + function closedInfos(withRefs: boolean) { + // DependencyTs is open, so omit it from closed infos + return () => withRefs ? + [dependencyConfig.path, libFile.path] : + [libFile.path, dtsPath, dtsMapLocation]; + } + + function otherWatchedFiles(withRefs: boolean) { + return () => withRefs ? + [mainConfig.path] : // Its in closed info + [mainConfig.path, dependencyConfig.path]; + } + + function goToDefActionInfo(withRefs: boolean): ActionInfo { + return { + action: goToDefFromMainTs, + closedInfos: closedInfos(withRefs), + otherWatchedFiles: otherWatchedFiles(withRefs), + expectsDts: !withRefs, // Dts script info present only if no project reference + expectsMap: !withRefs // Map script info present only if no project reference + }; + } + + function renameActionInfo(withRefs: boolean): ActionInfo { + return { + action: renameFromDependencyTsWithBothProjectsOpen, + closedInfos: closedInfos(withRefs), + otherWatchedFiles: otherWatchedFiles(withRefs), + expectsDts: !withRefs, // Dts script info present only if no project reference + expectsMap: !withRefs // Map script info present only if no project reference + }; + } + + function closedInfosNoMap(withRefs: boolean) { + return withRefs ? + closedInfos(withRefs) : + () => [libFile.path, dtsPath]; // No map + } + + function otherWatchedFilesNoMap(withRefs: boolean) { + return withRefs ? + otherWatchedFiles(withRefs) : + () => [mainConfig.path, dependencyConfig.path, dtsMapLocation]; // Watch map file + } + + function goToDefNoMapActionInfo(withRefs: boolean): ActionInfo { + return { + ...goToDefActionInfo(withRefs), + action: withRefs ? + goToDefFromMainTs : + goToDefFromMainTsWithNoMap, + closedInfos: closedInfosNoMap(withRefs), + otherWatchedFiles: otherWatchedFilesNoMap(withRefs), + expectsMap: false + }; + } + + function renameNoMapActionInfo(withRefs: boolean): ActionInfo { + return { + ...renameActionInfo(withRefs), + action: withRefs ? + renameFromDependencyTsWithBothProjectsOpen : + renameFromDependencyTs, + closedInfos: closedInfosNoMap(withRefs), + otherWatchedFiles: otherWatchedFilesNoMap(withRefs), + expectsMap: false + }; + } + + function closedInfosNoDts(withRefs: boolean) { + return withRefs ? + closedInfos(withRefs) : + () => [libFile.path]; // No dts or map + } + + function otherWatchedFilesNoDts(withRefs: boolean) { + return withRefs ? + otherWatchedFiles(withRefs) : + () => [mainConfig.path, dependencyConfig.path, dtsPath]; // Watch dts + } + + function goToDefNoDtsActionInfo(withRefs: boolean): ActionInfo { + return { + ...goToDefActionInfo(withRefs), + action: withRefs ? + goToDefFromMainTs : + goToDefFromMainTsWithNoDts, + closedInfos: closedInfosNoDts(withRefs), + expectsDts: false, + expectsMap: false + }; + } + + function renameNoDtsActionInfo(withRefs: boolean): ActionInfo { + return { + action: withRefs ? + renameFromDependencyTsWithBothProjectsOpen : + renameFromDependencyTs, + closedInfos: closedInfosNoDts(withRefs), + otherWatchedFiles: otherWatchedFilesNoDts(withRefs), + expectsDts: false, + expectsMap: false + }; + } + + function closedInfosDtsFileDeleted(withRefs: boolean) { + // Map collection after file open + return withRefs ? + closedInfos(withRefs) : + () => [libFile.path, dtsMapLocation]; + } + + verifyScenario({ + mainScenario: "goto Definition in usage and rename locations from defining project", + verifier: withRefs => [ + { + ...goToDefFromMainTsProjectInfoVerifier(withRefs), + main: () => goToDefActionInfo(withRefs), + noMap: () => goToDefNoMapActionInfo(withRefs), + mapFileCreated: "main", + mapFileDeleted: "noMap", + noDts: () => goToDefNoDtsActionInfo(withRefs), + dtsFileCreated: "main", + dtsFileDeleted: () => ({ + ...goToDefNoDtsActionInfo(withRefs), + // Map collection after file open + closedInfos: closedInfosDtsFileDeleted(withRefs), + expectsMap: !withRefs + }), + dependencyChange: () => ({ + ...goToDefActionInfo(withRefs), + action: goToDefFromMainTsWithDependencyChange, + expectsDts: false, + expectsMap: false + }), + noBuild: "main" + }, + { + ...renameFromDependencyTsProjectInfoVerifier(), + main: () => renameActionInfo(withRefs), + noMap: () => renameNoMapActionInfo(withRefs), + mapFileCreated: "main", + mapFileDeleted: "noMap", + noDts: () => renameNoDtsActionInfo(withRefs), + dtsFileCreated: "main", + dtsFileDeleted: () => ({ + ...renameNoDtsActionInfo(withRefs), + // Map collection after file open + closedInfos: closedInfosDtsFileDeleted(withRefs), + expectsMap: !withRefs + }), + dependencyChange: () => ({ + ...renameActionInfo(withRefs), + action: renameFromDependencyTsWithBothProjectsOpenWithDependencyChange + }), + noBuild: () => ({ + ...renameActionInfo(withRefs), + expectDts: false + }) + } + ] + }); + }); }); it("reusing d.ts files from composite and non composite projects", () => { From 15b68a93966530f184d471234e3a0566e8c37307 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Mon, 1 Jul 2019 14:43:11 -0700 Subject: [PATCH 16/97] Skip typechecking of source of project reference redirect --- src/compiler/builder.ts | 2 +- src/compiler/checker.ts | 4 +- src/compiler/program.ts | 2 +- src/compiler/utilities.ts | 9 +- src/server/scriptInfo.ts | 6 +- src/server/session.ts | 4 +- src/testRunner/tsconfig.json | 1 + .../tsserver/projectReferenceErrors.ts | 236 +++++++++++++ .../unittests/tsserver/projectReferences.ts | 322 +++++++++++------- 9 files changed, 460 insertions(+), 126 deletions(-) create mode 100644 src/testRunner/unittests/tsserver/projectReferenceErrors.ts diff --git a/src/compiler/builder.ts b/src/compiler/builder.ts index 704cdaf23f9..bd35bac6c5f 100644 --- a/src/compiler/builder.ts +++ b/src/compiler/builder.ts @@ -407,7 +407,7 @@ namespace ts { const options = program.getCompilerOptions(); forEach(program.getSourceFiles(), f => program.isSourceFileDefaultLibrary(f) && - !skipTypeChecking(f, options) && + !skipTypeChecking(f, options, program) && removeSemanticDiagnosticsOf(state, f.path) ); } diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index c2e6b0d518a..40040302123 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -338,7 +338,7 @@ namespace ts { return node && getTypeArgumentConstraint(node); }, getSuggestionDiagnostics: (file, ct) => { - if (skipTypeChecking(file, compilerOptions)) { + if (skipTypeChecking(file, compilerOptions, host)) { return emptyArray; } @@ -29657,7 +29657,7 @@ namespace ts { function checkSourceFileWorker(node: SourceFile) { const links = getNodeLinks(node); if (!(links.flags & NodeCheckFlags.TypeChecked)) { - if (skipTypeChecking(node, compilerOptions)) { + if (skipTypeChecking(node, compilerOptions, host)) { return; } diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 78e5f314879..2149e175ca2 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -1701,7 +1701,7 @@ namespace ts { function getSemanticDiagnosticsForFileNoCache(sourceFile: SourceFile, cancellationToken: CancellationToken): Diagnostic[] | undefined { return runWithCancellationToken(() => { - if (skipTypeChecking(sourceFile, options)) { + if (skipTypeChecking(sourceFile, options, program)) { return emptyArray; } diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 562d066b64e..ab6c0bfd83d 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -8573,11 +8573,16 @@ namespace ts { return { pos: typeParameters.pos - 1, end: typeParameters.end + 1 }; } - export function skipTypeChecking(sourceFile: SourceFile, options: CompilerOptions) { + export interface HostWithIsSourceOfProjectReferenceRedirect { + isSourceOfProjectReferenceRedirect(fileName: string): boolean; + } + export function skipTypeChecking(sourceFile: SourceFile, options: CompilerOptions, host: HostWithIsSourceOfProjectReferenceRedirect) { // If skipLibCheck is enabled, skip reporting errors if file is a declaration file. // If skipDefaultLibCheck is enabled, skip reporting errors if file contains a // '/// ' directive. - return options.skipLibCheck && sourceFile.isDeclarationFile || options.skipDefaultLibCheck && sourceFile.hasNoDefaultLib; + return (options.skipLibCheck && sourceFile.isDeclarationFile || + options.skipDefaultLibCheck && sourceFile.hasNoDefaultLib) || + host.isSourceOfProjectReferenceRedirect(sourceFile.fileName); } export function isJsonEqual(a: unknown, b: unknown): boolean { diff --git a/src/server/scriptInfo.ts b/src/server/scriptInfo.ts index 68c448a7e76..a2e75e3dac9 100644 --- a/src/server/scriptInfo.ts +++ b/src/server/scriptInfo.ts @@ -495,15 +495,17 @@ namespace ts.server { // the default project; if no configured projects, the first external project should // be the default project; otherwise the first inferred project should be the default. let firstExternalProject; + let firstConfiguredProject; for (const project of this.containingProjects) { if (project.projectKind === ProjectKind.Configured) { - return project; + if (!project.isSourceOfProjectReferenceRedirect(this.fileName)) return project; + if (!firstConfiguredProject) firstConfiguredProject = project; } else if (project.projectKind === ProjectKind.External && !firstExternalProject) { firstExternalProject = project; } } - return firstExternalProject || this.containingProjects[0]; + return firstConfiguredProject || firstExternalProject || this.containingProjects[0]; } } diff --git a/src/server/session.ts b/src/server/session.ts index ff339b06a81..a5c13574805 100644 --- a/src/server/session.ts +++ b/src/server/session.ts @@ -1658,10 +1658,10 @@ namespace ts.server { } } - private createCheckList(fileNames: string[], defaultProject?: Project): PendingErrorCheck[] { + private createCheckList(fileNames: string[]): PendingErrorCheck[] { return mapDefined(fileNames, uncheckedFileName => { const fileName = toNormalizedPath(uncheckedFileName); - const project = defaultProject || this.projectService.tryGetDefaultProjectForFile(fileName); + const project = this.projectService.tryGetDefaultProjectForFile(fileName); return project && { fileName, project }; }); } diff --git a/src/testRunner/tsconfig.json b/src/testRunner/tsconfig.json index b2e7e3e78b9..9d67215ffd9 100644 --- a/src/testRunner/tsconfig.json +++ b/src/testRunner/tsconfig.json @@ -137,6 +137,7 @@ "unittests/tsserver/occurences.ts", "unittests/tsserver/openFile.ts", "unittests/tsserver/projectErrors.ts", + "unittests/tsserver/projectReferenceErrors.ts", "unittests/tsserver/projectReferences.ts", "unittests/tsserver/projects.ts", "unittests/tsserver/refactors.ts", diff --git a/src/testRunner/unittests/tsserver/projectReferenceErrors.ts b/src/testRunner/unittests/tsserver/projectReferenceErrors.ts new file mode 100644 index 00000000000..8006b916037 --- /dev/null +++ b/src/testRunner/unittests/tsserver/projectReferenceErrors.ts @@ -0,0 +1,236 @@ +namespace ts.projectSystem { + describe("unittests:: tsserver:: with project references and error reporting", () => { + const projectLocation = "/user/username/projects/myproject"; + const dependecyLocation = `${projectLocation}/dependency`; + const usageLocation = `${projectLocation}/usage`; + const dependencyTs: File = { + path: `${dependecyLocation}/fns.ts`, + content: `export function fn1() { } +export function fn2() { } +// Introduce error for fnErr import in main +// export function fnErr() { } +// Error in dependency ts file +export let x: string = 10;` + }; + const dependencyConfig: File = { + path: `${dependecyLocation}/tsconfig.json`, + content: JSON.stringify({ compilerOptions: { composite: true, declarationDir: "../decls" } }) + }; + const usageTs: File = { + path: `${usageLocation}/usage.ts`, + content: `import { + fn1, + fn2, + fnErr +} from '../decls/fns' +fn1(); +fn2(); +fnErr(); +` + }; + const usageConfig: File = { + path: `${usageLocation}/tsconfig.json`, + content: JSON.stringify({ + references: [{ path: "../dependency" }] + }) + }; + + interface CheckErrorsInFile { + session: TestSession; + host: TestServerHost; + expected: GetErrDiagnostics; + expectedSequenceId?: number; + } + function checkErrorsInFile({ session, host, expected: { file, syntax, semantic, suggestion }, expectedSequenceId }: CheckErrorsInFile) { + host.checkTimeoutQueueLengthAndRun(1); + checkErrorMessage(session, "syntaxDiag", { file: file.path, diagnostics: syntax }); + session.clearMessages(); + + host.runQueuedImmediateCallbacks(1); + checkErrorMessage(session, "semanticDiag", { file: file.path, diagnostics: semantic }); + session.clearMessages(); + + host.runQueuedImmediateCallbacks(1); + checkErrorMessage(session, "suggestionDiag", { file: file.path, diagnostics: suggestion }); + if (expectedSequenceId !== undefined) { + checkCompleteEvent(session, 2, expectedSequenceId); + } + session.clearMessages(); + } + + interface CheckAllErrors { + session: TestSession; + host: TestServerHost; + expected: ReadonlyArray; + expectedSequenceId: number; + } + function checkAllErrors({ session, host, expected, expectedSequenceId }: CheckAllErrors) { + for (let i = 0; i < expected.length; i++) { + checkErrorsInFile({ + session, + host, + expected: expected[i], + expectedSequenceId: i === expected.length - 1 ? expectedSequenceId : undefined + }); + } + } + + function verifyErrorsUsingGeterr({ openFiles, expectedGetErr }: VerifyScenario) { + it("verifies the errors in open file", () => { + const host = createServerHost([dependencyTs, dependencyConfig, usageTs, usageConfig, libFile]); + const session = createSession(host, { canUseEvents: true, }); + openFilesForSession(openFiles(), session); + + session.clearMessages(); + const expectedSequenceId = session.getNextSeq(); + const expected = expectedGetErr(); + session.executeCommandSeq({ + command: protocol.CommandTypes.Geterr, + arguments: { + delay: 0, + files: expected.map(f => f.file.path) + } + }); + + checkAllErrors({ session, host, expected, expectedSequenceId }); + }); + } + + function verifyErrorsUsingGeterrForProject({ openFiles, expectedGetErrForProject }: VerifyScenario) { + it("verifies the errors in projects", () => { + const host = createServerHost([dependencyTs, dependencyConfig, usageTs, usageConfig, libFile]); + const session = createSession(host, { canUseEvents: true, }); + openFilesForSession(openFiles(), session); + + session.clearMessages(); + for (const expected of expectedGetErrForProject()) { + const expectedSequenceId = session.getNextSeq(); + session.executeCommandSeq({ + command: protocol.CommandTypes.GeterrForProject, + arguments: { + delay: 0, + file: expected.project + } + }); + + checkAllErrors({ session, host, expected: expected.errors, expectedSequenceId }); + } + }); + } + + interface GetErrDiagnostics { + file: File; + syntax: protocol.Diagnostic[]; + semantic: protocol.Diagnostic[]; + suggestion: protocol.Diagnostic[]; + } + interface GetErrForProjectDiagnostics { + project: string; + errors: ReadonlyArray; + } + interface VerifyScenario { + openFiles: () => ReadonlyArray; + expectedGetErr: () => ReadonlyArray; + expectedGetErrForProject: () => ReadonlyArray; + } + function verifyScenario(scenario: VerifyScenario) { + verifyErrorsUsingGeterr(scenario); + verifyErrorsUsingGeterrForProject(scenario); + } + + function emptyDiagnostics(file: File): GetErrDiagnostics { + return { + file, + syntax: emptyArray, + semantic: emptyArray, + suggestion: emptyArray + }; + } + + function usageDiagnostics(): GetErrDiagnostics { + return { + file: usageTs, + syntax: emptyArray, + semantic: [ + createDiagnostic( + { line: 4, offset: 5 }, + { line: 4, offset: 10 }, + Diagnostics.Module_0_has_no_exported_member_1, + [`"../dependency/fns"`, "fnErr"], + "error", + ) + ], + suggestion: emptyArray + }; + } + + function dependencyDiagnostics(): GetErrDiagnostics { + return { + file: dependencyTs, + syntax: emptyArray, + semantic: [ + createDiagnostic( + { line: 6, offset: 12 }, + { line: 6, offset: 13 }, + Diagnostics.Type_0_is_not_assignable_to_type_1, + ["10", "string"], + "error", + ) + ], + suggestion: emptyArray + }; + } + + function usageProjectDiagnostics(): GetErrForProjectDiagnostics { + return { + project: usageTs.path, + errors: [ + usageDiagnostics(), + emptyDiagnostics(dependencyTs) + ] + }; + } + + function dependencyProjectDiagnostics(): GetErrForProjectDiagnostics { + return { + project: dependencyTs.path, + errors: [ + dependencyDiagnostics() + ] + }; + } + + describe("when dependency project is not open", () => { + verifyScenario({ + openFiles: () => [usageTs], + expectedGetErr: () => [ + usageDiagnostics() + ], + expectedGetErrForProject: () => [ + usageProjectDiagnostics(), + { + project: dependencyTs.path, + errors: [ + emptyDiagnostics(dependencyTs), + usageDiagnostics() + ] + } + ] + }); + }); + + describe("when the depedency file is open", () => { + verifyScenario({ + openFiles: () => [usageTs, dependencyTs], + expectedGetErr: () => [ + usageDiagnostics(), + dependencyDiagnostics(), + ], + expectedGetErrForProject: () => [ + usageProjectDiagnostics(), + dependencyProjectDiagnostics() + ] + }); + }); + }); +} diff --git a/src/testRunner/unittests/tsserver/projectReferences.ts b/src/testRunner/unittests/tsserver/projectReferences.ts index c5712df2e1a..f39ec72e03e 100644 --- a/src/testRunner/unittests/tsserver/projectReferences.ts +++ b/src/testRunner/unittests/tsserver/projectReferences.ts @@ -373,6 +373,9 @@ fn5(); otherWatchedFiles: () => readonly string[]; expectsDts: boolean; expectsMap: boolean; + freshMapInfo?: boolean; + freshDocumentMapper?: boolean; + skipDtsMapCheck?: boolean; } type ActionKey = keyof ActionInfoVerifier; type ActionInfoGetterFn = () => ActionInfo; @@ -385,6 +388,9 @@ fn5(); } interface ActionInfoVerifier { main: ActionInfoGetter; + change: ActionInfoGetter; + dtsChange: ActionInfoGetter; + mapChange: ActionInfoGetter; noMap: ActionInfoGetter; mapFileCreated: ActionInfoGetter; mapFileDeleted: ActionInfoGetter; @@ -461,14 +467,21 @@ fn5(); return info; } - function verifyDocumentPositionMapper(session: TestSession, dependencyMap: server.ScriptInfo | undefined, documentPositionMapper: server.ScriptInfo["documentPositionMapper"], equal: boolean) { - assert.strictEqual(session.getProjectService().filenameToScriptInfo.get(dtsMapPath), dependencyMap); + interface VerifyDocumentPositionMapper { + session: TestSession; + dependencyMap: server.ScriptInfo | undefined; + documentPositionMapper: server.ScriptInfo["documentPositionMapper"]; + equal: boolean; + debugInfo: string; + } + function verifyDocumentPositionMapper({ session, dependencyMap, documentPositionMapper, equal, debugInfo }: VerifyDocumentPositionMapper) { + assert.strictEqual(session.getProjectService().filenameToScriptInfo.get(dtsMapPath), dependencyMap, debugInfo); if (dependencyMap) { if (equal) { - assert.strictEqual(dependencyMap.documentPositionMapper, documentPositionMapper); + assert.strictEqual(dependencyMap.documentPositionMapper, documentPositionMapper, debugInfo); } else { - assert.notStrictEqual(dependencyMap.documentPositionMapper, documentPositionMapper); + assert.notStrictEqual(dependencyMap.documentPositionMapper, documentPositionMapper, debugInfo); } } } @@ -491,7 +504,6 @@ fn5(); sourceMapPath?: server.ScriptInfo["sourceMapFilePath"]; dependencyMap?: server.ScriptInfo | undefined; documentPositionMapper?: server.ScriptInfo["documentPositionMapper"]; - firstEquals?: boolean; } interface VerifyAllFnActionResult { actionInfos: readonly ActionInfo[]; @@ -506,44 +518,62 @@ fn5(); actionKey, dependencyMap, documentPositionMapper, - firstEquals }: VerifyAllFnAction): VerifyAllFnActionResult { const actionInfos = getActionInfo(verifiers, actionKey); let sourceMapPath: server.ScriptInfo["sourceMapFilePath"] | undefined; // action let first = true; - for (const { action, closedInfos, otherWatchedFiles, expectsDts, expectsMap } of actionInfos) { + for (const { + action, + closedInfos, + otherWatchedFiles, + expectsDts, + expectsMap, + freshMapInfo, + freshDocumentMapper, + skipDtsMapCheck + } of actionInfos) { for (let fn = 1; fn <= 5; fn++) { const fnAction = action(fn); verifyAction(session, fnAction); - const dtsInfo = verifyScriptInfoPresence(session, dtsPath, expectsDts, fnAction.reqName); - const dtsMapInfo = verifyScriptInfoPresence(session, dtsMapPath, expectsMap, fnAction.reqName); + const debugInfo = `${actionKey}:: ${fnAction.reqName}:: ${fn}`; + const dtsInfo = verifyScriptInfoPresence(session, dtsPath, expectsDts, debugInfo); + const dtsMapInfo = verifyScriptInfoPresence(session, dtsMapPath, expectsMap, debugInfo); verifyInfosWithRandom( session, host, openFiles(verifiers).map(f => f.path), closedInfos(), otherWatchedFiles(), - `${actionKey}:: ${fnAction.reqName}` + debugInfo ); if (dtsInfo) { - if (first) { - if (dtsMapInfo) { - assert.equal(dtsInfo.sourceMapFilePath, dtsMapPath, `${actionKey}:: ${fnAction.reqName}`); - } - else { - assert.isNotString(dtsInfo.sourceMapFilePath); - assert.isNotFalse(dtsInfo.sourceMapFilePath); - assert.isDefined(dtsInfo.sourceMapFilePath); + if (first || (fn === 1 && freshMapInfo)) { + if (!skipDtsMapCheck) { + if (dtsMapInfo) { + assert.equal(dtsInfo.sourceMapFilePath, dtsMapPath, debugInfo); + } + else { + assert.isNotString(dtsInfo.sourceMapFilePath, debugInfo); + assert.isNotFalse(dtsInfo.sourceMapFilePath, debugInfo); + assert.isDefined(dtsInfo.sourceMapFilePath, debugInfo); + } } } else { - assert.equal(dtsInfo.sourceMapFilePath, sourceMapPath, `${actionKey}:: ${fnAction.reqName}`); + assert.equal(dtsInfo.sourceMapFilePath, sourceMapPath, debugInfo); } } - if (!first || firstEquals !== undefined) { - verifyDocumentPositionMapper(session, dependencyMap, documentPositionMapper, !first || !!firstEquals); + + if (!first && (fn !== 1 || !freshMapInfo)) { + verifyDocumentPositionMapper({ + session, + dependencyMap, + documentPositionMapper, + equal: fn !== 1 || !freshDocumentMapper, + debugInfo + }); } sourceMapPath = dtsInfo && dtsInfo.sourceMapFilePath; dependencyMap = dtsMapInfo; @@ -566,15 +596,22 @@ fn5(); openFilesForSession([randomFile], session); const { closedInfos, otherWatchedFiles } = last(actionInfos); + const debugInfo = `${actionKey} Collection`; verifyInfosWithRandom( session, host, openFiles(verifiers).map(f => f.path), closedInfos(), otherWatchedFiles(), - `${actionKey} Collection` + debugInfo ); - verifyDocumentPositionMapper(session, dependencyMap, documentPositionMapper, /*equal*/ true); + verifyDocumentPositionMapper({ + session, + dependencyMap, + documentPositionMapper, + equal: true, + debugInfo + }); // Closing open file, removes dependencies too closeFilesForSession([...openFiles(verifiers), randomFile], session); @@ -601,7 +638,6 @@ fn5(); verifier, withRefs, change, - afterActionDocumentPositionMapperNotEquals, afterChangeActionKey }: VerifyScenarioWithChanges, timeoutBeforeAction: boolean, @@ -619,7 +655,13 @@ fn5(); if (timeoutBeforeAction) { host.runQueuedTimeoutCallbacks(); checkProject(session, verifiers); - verifyDocumentPositionMapper(session, dependencyMap, documentPositionMapper, /*equal*/ true); + verifyDocumentPositionMapper({ + session, + dependencyMap, + documentPositionMapper, + equal: true, + debugInfo: "After change timeout" + }); } // action @@ -629,8 +671,7 @@ fn5(); verifiers, actionKey: afterChangeActionKey, dependencyMap, - documentPositionMapper, - firstEquals: !afterActionDocumentPositionMapperNotEquals + documentPositionMapper }); }); } @@ -638,7 +679,6 @@ fn5(); interface VerifyScenarioWithChanges extends VerifierAndWithRefs { scenarioName: string; change: (host: TestServerHost, session: TestSession, verifiers: readonly DocumentPositionMapperVerifier[]) => void; - afterActionDocumentPositionMapperNotEquals?: true; afterChangeActionKey: ActionKey; } function verifyScenarioWithChanges(verify: VerifyScenarioWithChanges) { @@ -704,7 +744,7 @@ fn5(); // The dependency file is deleted when orphan files are collected host.deleteFile(fileLocation); // Verify with deleted action key - const result = verifyAllFnAction({ session, host, verifiers, actionKey: fileDeletedKey }); + verifyAllFnAction({ session, host, verifiers, actionKey: fileDeletedKey }); checkProject(session, verifiers, noDts); // Script info collection should behave as fileNotPresentKey @@ -714,7 +754,7 @@ fn5(); verifiers, { actionInfos: getActionInfo(verifiers, fileNotPresentKey), - actionKey: result.actionKey, + actionKey: fileNotPresentKey, dependencyMap: undefined, documentPositionMapper: undefined } @@ -748,7 +788,7 @@ fn5(); } }) ), - afterChangeActionKey: "main" + afterChangeActionKey: "change" }); // Edit dts to add new fn @@ -764,7 +804,7 @@ fn5(); //# sourceMappingURL=FnS.d.ts.map` ) ), - afterChangeActionKey: "main" + afterChangeActionKey: "dtsChange" }); // Edit map file to represent added new line @@ -776,8 +816,7 @@ fn5(); dtsMapLocation, `{"version":3,"file":"FnS.d.ts","sourceRoot":"","sources":["../dependency/FnS.ts"],"names":[],"mappings":"AAAA,wBAAgB,GAAG,SAAM;AACzB,wBAAgB,GAAG,SAAM;AACzB,wBAAgB,GAAG,SAAM;AACzB,wBAAgB,GAAG,SAAM;AACzB,wBAAgB,GAAG,SAAM;AACzB,eAAO,MAAM,CAAC,KAAK,CAAC"}` ), - afterChangeActionKey: "main", - afterActionDocumentPositionMapperNotEquals: true + afterChangeActionKey: "mapChange" }); verifyScenarioWhenFileNotPresent({ @@ -824,7 +863,7 @@ ${dependencyTs.content}`); afterChangeActionKey: "dependencyChange" }); - it("when d.ts file is not generated", () => { + it("when projects are not built", () => { const host = createServerHost(files); const session = createSession(host); const verifiers = verifier(withRefs); @@ -896,6 +935,12 @@ ${dependencyTs.content}`); { ...goToDefFromMainTsProjectInfoVerifier(withRefs), main: () => goToDefActionInfo(withRefs), + change: "main", + dtsChange: "main", + mapChange: () => ({ + ...goToDefActionInfo(withRefs), + freshDocumentMapper: true + }), noMap: () => goToDefNoMapActionInfo(withRefs), mapFileCreated: "main", mapFileDeleted: () => ({ @@ -938,15 +983,6 @@ ${dependencyTs.content}`); }; } - function renameNoMapActionInfo(): ActionInfo { - return { - ...renameActionInfo(), - closedInfos: () => [libFile.path, dtsLocation], // No map - otherWatchedFiles: () => [dependencyConfig.path, dtsMapLocation], // watch map - expectsMap: false - }; - } - function renameNoDtsActionInfo(): ActionInfo { return { action: renameFromDependencyTs, @@ -963,7 +999,18 @@ ${dependencyTs.content}`); { ...renameFromDependencyTsProjectInfoVerifier(), main: renameActionInfo, - noMap: renameNoMapActionInfo, + change: "main", + dtsChange: "main", + mapChange: () => ({ + ...renameActionInfo(), + freshDocumentMapper: true + }), + noMap: () => ({ + ...renameActionInfo(), + closedInfos: () => [libFile.path, dtsLocation], // No map + otherWatchedFiles: () => [dependencyConfig.path, dtsMapLocation], // watch map + expectsMap: false + }), mapFileCreated: "main", mapFileDeleted: "noMap", noDts: renameNoDtsActionInfo, @@ -992,24 +1039,16 @@ ${dependencyTs.content}`); }); describe("when opening depedency and usage project", () => { - function closedInfos(withRefs: boolean) { - // DependencyTs is open, so omit it from closed infos - return () => withRefs ? - [dependencyConfig.path, libFile.path] : - [libFile.path, dtsPath, dtsMapLocation]; - } - - function otherWatchedFiles(withRefs: boolean) { - return () => withRefs ? - [mainConfig.path] : // Its in closed info - [mainConfig.path, dependencyConfig.path]; - } - function goToDefActionInfo(withRefs: boolean): ActionInfo { return { action: goToDefFromMainTs, - closedInfos: closedInfos(withRefs), - otherWatchedFiles: otherWatchedFiles(withRefs), + // DependencyTs is open, so omit it from closed infos + closedInfos: () => withRefs ? + [dependencyConfig.path, libFile.path] : + [libFile.path, dtsPath, dtsMapLocation], + otherWatchedFiles: () => withRefs ? + [mainConfig.path] : // Its in closed info + [mainConfig.path, dependencyConfig.path], expectsDts: !withRefs, // Dts script info present only if no project reference expectsMap: !withRefs // Map script info present only if no project reference }; @@ -1018,33 +1057,30 @@ ${dependencyTs.content}`); function renameActionInfo(withRefs: boolean): ActionInfo { return { action: renameFromDependencyTsWithBothProjectsOpen, - closedInfos: closedInfos(withRefs), - otherWatchedFiles: otherWatchedFiles(withRefs), - expectsDts: !withRefs, // Dts script info present only if no project reference - expectsMap: !withRefs // Map script info present only if no project reference + // DependencyTs is open, so omit it from closed infos + closedInfos: () => withRefs ? + [dependencyConfig.path, libFile.path, dtsLocation, dtsMapLocation] : + [libFile.path, dtsPath, dtsMapLocation], + otherWatchedFiles: () => withRefs ? + [mainConfig.path] : // Its in closed info + [mainConfig.path, dependencyConfig.path], + expectsDts: true, + expectsMap: true }; } - function closedInfosNoMap(withRefs: boolean) { - return withRefs ? - closedInfos(withRefs) : - () => [libFile.path, dtsPath]; // No map - } - - function otherWatchedFilesNoMap(withRefs: boolean) { - return withRefs ? - otherWatchedFiles(withRefs) : - () => [mainConfig.path, dependencyConfig.path, dtsMapLocation]; // Watch map file - } - function goToDefNoMapActionInfo(withRefs: boolean): ActionInfo { return { - ...goToDefActionInfo(withRefs), action: withRefs ? goToDefFromMainTs : goToDefFromMainTsWithNoMap, - closedInfos: closedInfosNoMap(withRefs), - otherWatchedFiles: otherWatchedFilesNoMap(withRefs), + closedInfos: () => withRefs ? + [dependencyConfig.path, libFile.path] : + [libFile.path, dtsPath], // No map + otherWatchedFiles: () => withRefs ? + [mainConfig.path] : // Its in closed info + [mainConfig.path, dependencyConfig.path, dtsMapLocation], // Watch map file + expectsDts: !withRefs, // Dts script info present only if no project reference expectsMap: false }; } @@ -1055,31 +1091,25 @@ ${dependencyTs.content}`); action: withRefs ? renameFromDependencyTsWithBothProjectsOpen : renameFromDependencyTs, - closedInfos: closedInfosNoMap(withRefs), - otherWatchedFiles: otherWatchedFilesNoMap(withRefs), + closedInfos: () => withRefs ? + [dependencyConfig.path, libFile.path, dtsLocation] : + [libFile.path, dtsPath], // No map + otherWatchedFiles: () => withRefs ? + [mainConfig.path, dtsMapLocation] : // Its in closed info + [mainConfig.path, dependencyConfig.path, dtsMapLocation], // Watch map file expectsMap: false }; } - function closedInfosNoDts(withRefs: boolean) { - return withRefs ? - closedInfos(withRefs) : - () => [libFile.path]; // No dts or map - } - - function otherWatchedFilesNoDts(withRefs: boolean) { - return withRefs ? - otherWatchedFiles(withRefs) : - () => [mainConfig.path, dependencyConfig.path, dtsPath]; // Watch dts - } - function goToDefNoDtsActionInfo(withRefs: boolean): ActionInfo { return { ...goToDefActionInfo(withRefs), action: withRefs ? goToDefFromMainTs : goToDefFromMainTsWithNoDts, - closedInfos: closedInfosNoDts(withRefs), + closedInfos: () => withRefs ? + [dependencyConfig.path, libFile.path] : + [libFile.path], // No dts or map, expectsDts: false, expectsMap: false }; @@ -1090,49 +1120,100 @@ ${dependencyTs.content}`); action: withRefs ? renameFromDependencyTsWithBothProjectsOpen : renameFromDependencyTs, - closedInfos: closedInfosNoDts(withRefs), - otherWatchedFiles: otherWatchedFilesNoDts(withRefs), + closedInfos: () => withRefs ? + [dependencyConfig.path, libFile.path] : + [libFile.path], // No dts or map, + otherWatchedFiles: () => withRefs ? + [mainConfig.path, dtsLocation] : + [mainConfig.path, dependencyConfig.path, dtsPath], // Watch dts, expectsDts: false, expectsMap: false }; } - function closedInfosDtsFileDeleted(withRefs: boolean) { - // Map collection after file open - return withRefs ? - closedInfos(withRefs) : - () => [libFile.path, dtsMapLocation]; - } - verifyScenario({ mainScenario: "goto Definition in usage and rename locations from defining project", verifier: withRefs => [ { ...goToDefFromMainTsProjectInfoVerifier(withRefs), main: () => goToDefActionInfo(withRefs), + change: () => ({ + // Because before this rename is done the closed info remains same as rename's main operation + ...goToDefActionInfo(withRefs), + closedInfos: () => withRefs ? + [dependencyConfig.path, libFile.path, dtsLocation, dtsMapLocation] : + [libFile.path, dtsPath, dtsMapLocation], + expectsDts: true, + expectsMap: true + }), + dtsChange: "change", + mapChange: "change", noMap: () => goToDefNoMapActionInfo(withRefs), - mapFileCreated: "main", - mapFileDeleted: "noMap", + mapFileCreated: () => ({ + // Because before this rename is done the closed info remains same as rename's main + ...goToDefActionInfo(withRefs), + closedInfos: () => withRefs ? + [dependencyConfig.path, libFile.path, dtsLocation] : + [libFile.path, dtsPath, dtsMapLocation], + expectsDts: true, + // This operation doesnt need map so the map info path in dts is not refreshed + skipDtsMapCheck: withRefs + }), + mapFileDeleted: () => ({ + // Because before this rename is done the closed info remains same as rename's noMap operation + ...goToDefNoMapActionInfo(withRefs), + closedInfos: () => withRefs ? + [dependencyConfig.path, libFile.path, dtsLocation] : + [libFile.path, dtsPath], // No map, + expectsDts: true, + // This operation doesnt need map so the map info path in dts is not refreshed + skipDtsMapCheck: withRefs + }), noDts: () => goToDefNoDtsActionInfo(withRefs), - dtsFileCreated: "main", + dtsFileCreated: () => ({ + ...goToDefActionInfo(withRefs), + // Since the project for dependency is not updated, the watcher from rename for dts still there + otherWatchedFiles: () => withRefs ? + [mainConfig.path, dtsLocation] : + [mainConfig.path, dependencyConfig.path], + }), dtsFileDeleted: () => ({ ...goToDefNoDtsActionInfo(withRefs), // Map collection after file open - closedInfos: closedInfosDtsFileDeleted(withRefs), - expectsMap: !withRefs + closedInfos: () => withRefs ? + [dependencyConfig.path, libFile.path, dtsMapLocation] : + [libFile.path, dtsMapLocation], + expectsMap: true }), dependencyChange: () => ({ ...goToDefActionInfo(withRefs), action: goToDefFromMainTsWithDependencyChange, - expectsDts: false, - expectsMap: false + // From rename main action + closedInfos: () => withRefs ? + [dependencyConfig.path, libFile.path, dtsLocation, dtsMapLocation] : + [libFile.path, dtsPath, dtsMapLocation], + expectsDts: withRefs, + expectsMap: withRefs }), noBuild: "main" }, { ...renameFromDependencyTsProjectInfoVerifier(), - main: () => renameActionInfo(withRefs), - noMap: () => renameNoMapActionInfo(withRefs), + main: () => ({ + ...renameActionInfo(withRefs), + freshMapInfo: withRefs + }), + change: () => renameActionInfo(withRefs), + dtsChange: "change", + mapChange: () => ({ + ...renameActionInfo(withRefs), + freshDocumentMapper: withRefs + }), + noMap: () => ({ + ...renameNoMapActionInfo(withRefs), + freshMapInfo: withRefs, + freshDocumentMapper: withRefs + }), mapFileCreated: "main", mapFileDeleted: "noMap", noDts: () => renameNoDtsActionInfo(withRefs), @@ -1140,16 +1221,25 @@ ${dependencyTs.content}`); dtsFileDeleted: () => ({ ...renameNoDtsActionInfo(withRefs), // Map collection after file open - closedInfos: closedInfosDtsFileDeleted(withRefs), - expectsMap: !withRefs + closedInfos: () => withRefs ? + [dependencyConfig.path, libFile.path, dtsMapLocation] : + [libFile.path, dtsMapLocation], + expectsMap: true }), dependencyChange: () => ({ ...renameActionInfo(withRefs), action: renameFromDependencyTsWithBothProjectsOpenWithDependencyChange }), noBuild: () => ({ - ...renameActionInfo(withRefs), - expectDts: false + action: renameFromDependencyTsWithBothProjectsOpen, + closedInfos: () => withRefs ? + [dependencyConfig.path, libFile.path] : + [libFile.path], + otherWatchedFiles: () => withRefs ? + [mainConfig.path, dtsLocation] : // Its in closed info + [mainConfig.path, dependencyConfig.path], + expectsDts: false, + expectsMap: false }) } ] From 9be475bdaa9e40787e610ffe0a27ecfe9ae323a8 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Wed, 3 Jul 2019 15:47:32 -0700 Subject: [PATCH 17/97] Refactoring --- .../unittests/tsserver/projectReferences.ts | 532 ++++++++---------- 1 file changed, 222 insertions(+), 310 deletions(-) diff --git a/src/testRunner/unittests/tsserver/projectReferences.ts b/src/testRunner/unittests/tsserver/projectReferences.ts index f39ec72e03e..f9445dc6d19 100644 --- a/src/testRunner/unittests/tsserver/projectReferences.ts +++ b/src/testRunner/unittests/tsserver/projectReferences.ts @@ -105,6 +105,7 @@ export function fn4() { } export function fn5() { } ` }; + const dependencyTsPath = dependencyTs.path.toLowerCase(); const dependencyConfig: File = { path: `${dependecyLocation}/tsconfig.json`, content: JSON.stringify({ compilerOptions: { composite: true, declarationMap: true, declarationDir: "../decls" } }) @@ -362,6 +363,13 @@ fn5(); }; } + function removePath(array: readonly string[], ...delPaths: string[]) { + return array.filter(a => { + const aLower = a.toLowerCase(); + return delPaths.every(dPath => dPath !== aLower); + }); + } + interface Action { reqName: string; request: Partial; @@ -369,8 +377,8 @@ fn5(); } interface ActionInfo { action: (fn: number) => Action; - closedInfos: () => readonly string[]; - otherWatchedFiles: () => readonly string[]; + closedInfos: readonly string[]; + otherWatchedFiles: readonly string[]; expectsDts: boolean; expectsMap: boolean; freshMapInfo?: boolean; @@ -379,7 +387,11 @@ fn5(); } type ActionKey = keyof ActionInfoVerifier; type ActionInfoGetterFn = () => ActionInfo; - type ActionInfoGetter = ActionInfoGetterFn | ActionKey; + type ActionInfoSpreader = [ + ActionKey, // Key to get initial value and pass this value to spread function + (actionInfo: ActionInfo) => Partial> + ]; + type ActionInfoGetter = ActionInfoGetterFn | ActionKey | ActionInfoSpreader; interface ProjectInfoVerifier { openFile: File; openFileLastLine: number; @@ -486,14 +498,25 @@ fn5(); } } + function getActionInfoOfVerfier(verifier: DocumentPositionMapperVerifier, actionKey: ActionKey): ActionInfo { + const actionInfoGetter = verifier[actionKey]; + if (isString(actionInfoGetter)) { + return getActionInfoOfVerfier(verifier, actionInfoGetter); + } + + if (isArray(actionInfoGetter)) { + const initialValue = getActionInfoOfVerfier(verifier, actionInfoGetter[0]); + return { + ...initialValue, + ...actionInfoGetter[1](initialValue) + }; + } + + return actionInfoGetter(); + } + function getActionInfo(verifiers: readonly DocumentPositionMapperVerifier[], actionKey: ActionKey): ActionInfo[] { - return verifiers.map(v => { - let actionInfoGetter = v[actionKey]; - while (isString(actionInfoGetter)) { - actionInfoGetter = v[actionInfoGetter]; - } - return actionInfoGetter(); - }); + return verifiers.map(v => getActionInfoOfVerfier(v, actionKey)); } interface VerifyAllFnAction { @@ -543,8 +566,8 @@ fn5(); session, host, openFiles(verifiers).map(f => f.path), - closedInfos(), - otherWatchedFiles(), + closedInfos, + otherWatchedFiles, debugInfo ); @@ -601,8 +624,8 @@ fn5(); session, host, openFiles(verifiers).map(f => f.path), - closedInfos(), - otherWatchedFiles(), + closedInfos, + otherWatchedFiles, debugInfo ); verifyDocumentPositionMapper({ @@ -887,360 +910,249 @@ ${dependencyTs.content}`); } describe("from project that uses dependency", () => { - function goToDefActionInfo(withRefs: boolean): ActionInfo { - return { - action: goToDefFromMainTs, - closedInfos: () => withRefs ? - [dependencyTs.path, dependencyConfig.path, libFile.path] : - [dependencyTs.path, libFile.path, dtsPath, dtsMapLocation], - otherWatchedFiles: () => [mainConfig.path], - expectsDts: !withRefs, // Dts script info present only if no project reference - expectsMap: !withRefs // Map script info present only if no project reference - }; - } - - function goToDefNoMapActionInfo(withRefs: boolean): ActionInfo { - return { - ...goToDefActionInfo(withRefs), - action: withRefs ? - goToDefFromMainTs : - goToDefFromMainTsWithNoMap, - closedInfos: () => withRefs ? - [dependencyTs.path, dependencyConfig.path, libFile.path] : - [libFile.path, dtsPath], // Because map is deleted, dts and dependency are released - otherWatchedFiles: () => withRefs ? - [mainConfig.path] : - [mainConfig.path, dtsMapPath], // Watches deleted file - expectsMap: false - }; - } - - function goToDefNoDtsActionInfo(withRefs: boolean): ActionInfo { - return { - ...goToDefActionInfo(withRefs), - action: withRefs ? - goToDefFromMainTs : - goToDefFromMainTsWithNoDts, - closedInfos: () => withRefs ? - [dependencyTs.path, dependencyConfig.path, libFile.path] : - [libFile.path], // No dts means no map, no dependency - expectsDts: false, - expectsMap: false - }; - } - verifyScenario({ mainScenario: "can go to definition correctly", verifier: withRefs => [ { ...goToDefFromMainTsProjectInfoVerifier(withRefs), - main: () => goToDefActionInfo(withRefs), + main: () => ({ + action: goToDefFromMainTs, + closedInfos: withRefs ? + [dependencyTs.path, dependencyConfig.path, libFile.path] : + [dependencyTs.path, libFile.path, dtsPath, dtsMapLocation], + otherWatchedFiles: [mainConfig.path], + expectsDts: !withRefs, // Dts script info present only if no project reference + expectsMap: !withRefs // Map script info present only if no project reference + }), change: "main", dtsChange: "main", - mapChange: () => ({ - ...goToDefActionInfo(withRefs), + mapChange: ["main", () => ({ freshDocumentMapper: true - }), - noMap: () => goToDefNoMapActionInfo(withRefs), + })], + noMap: withRefs ? + "main" : + ["main", main => ({ + action: goToDefFromMainTsWithNoMap, + // Because map is deleted, dts and dependency are released + closedInfos: removePath(main.closedInfos, dtsMapPath, dependencyTsPath), + // Watches deleted file + otherWatchedFiles: main.otherWatchedFiles.concat(dtsMapLocation), + expectsMap: false + })], mapFileCreated: "main", - mapFileDeleted: () => ({ - ...goToDefNoMapActionInfo(withRefs), - closedInfos: () => withRefs ? - [dependencyTs.path, dependencyConfig.path, libFile.path] : + mapFileDeleted: withRefs ? + "main" : + ["noMap", noMap => ({ // The script info for depedency is collected only after file open - [dependencyTs.path, libFile.path, dtsPath] - }), - noDts: () => goToDefNoDtsActionInfo(withRefs), + closedInfos: noMap.closedInfos.concat(dependencyTs.path) + })], + noDts: withRefs ? + "main" : + ["main", main => ({ + action: goToDefFromMainTsWithNoDts, + // No dts, no map, no dependency + closedInfos: removePath(main.closedInfos, dtsPath, dtsMapPath, dependencyTsPath), + expectsDts: false, + expectsMap: false + })], dtsFileCreated: "main", - dtsFileDeleted: () => ({ - ...goToDefNoDtsActionInfo(withRefs), - // The script info for map is collected only after file open - closedInfos: () => withRefs ? - [dependencyTs.path, dependencyConfig.path, libFile.path] : - [dependencyTs.path, libFile.path, dtsMapLocation], - expectsMap: !withRefs - }), - dependencyChange: () => ({ - ...goToDefActionInfo(withRefs), + dtsFileDeleted: withRefs ? + "main" : + ["noDts", noDts => ({ + // The script info for map is collected only after file open + closedInfos: noDts.closedInfos.concat(dependencyTs.path, dtsMapLocation), + expectsMap: true + })], + dependencyChange: ["main", () => ({ action: goToDefFromMainTsWithDependencyChange, - expectsDts: false, - expectsMap: false - }), - noBuild: "main" + })], + noBuild: "noDts" } ] }); }); describe("from defining project", () => { - function renameActionInfo(): ActionInfo { - return { - action: renameFromDependencyTs, - closedInfos: () => [libFile.path, dtsLocation, dtsMapLocation], - otherWatchedFiles: () => [dependencyConfig.path], - expectsDts: true, - expectsMap: true - }; - } - - function renameNoDtsActionInfo(): ActionInfo { - return { - action: renameFromDependencyTs, - closedInfos: () => [libFile.path], // no dts or map since dts itself doesnt exist - otherWatchedFiles: () => [dependencyConfig.path, dtsPath], // watch deleted file - expectsDts: false, - expectsMap: false - }; - } - verifyScenario({ mainScenario: "rename locations from dependency", verifier: () => [ { ...renameFromDependencyTsProjectInfoVerifier(), - main: renameActionInfo, - change: "main", - dtsChange: "main", - mapChange: () => ({ - ...renameActionInfo(), - freshDocumentMapper: true - }), - noMap: () => ({ - ...renameActionInfo(), - closedInfos: () => [libFile.path, dtsLocation], // No map - otherWatchedFiles: () => [dependencyConfig.path, dtsMapLocation], // watch map - expectsMap: false - }), - mapFileCreated: "main", - mapFileDeleted: "noMap", - noDts: renameNoDtsActionInfo, - dtsFileCreated: "main", - dtsFileDeleted: () => ({ - ...renameNoDtsActionInfo(), - // Map is collected after file open - closedInfos: () => [libFile.path, dtsMapLocation], + main: () => ({ + action: renameFromDependencyTs, + closedInfos: [libFile.path, dtsLocation, dtsMapLocation], + otherWatchedFiles: [dependencyConfig.path], + expectsDts: true, expectsMap: true }), - dependencyChange: () => ({ - ...renameActionInfo(), - action: renameFromDependencyTsWithDependencyChange - }), - noBuild: () => ({ - action: renameFromDependencyTs, - closedInfos: () => [libFile.path], // No dts or map since its not built/present - // Watching for creation of dts so that it can give correct results across projects - otherWatchedFiles: () => [dependencyConfig.path, dtsPath], + change: "main", + dtsChange: "main", + mapChange: ["main", () => ({ + freshDocumentMapper: true + })], + noMap: ["main", main => ({ + // No map + closedInfos: removePath(main.closedInfos, dtsMapPath), + // watch map + otherWatchedFiles: [...main.otherWatchedFiles, dtsMapLocation], + expectsMap: false + })], + mapFileCreated: "main", + mapFileDeleted: "noMap", + noDts: ["main", main => ({ + // no dts or map since dts itself doesnt exist + closedInfos: removePath(main.closedInfos, dtsMapPath, dtsPath), + // watch deleted file + otherWatchedFiles: [...main.otherWatchedFiles, dtsLocation], expectsDts: false, expectsMap: false - }) + })], + dtsFileCreated: "main", + dtsFileDeleted: ["noDts", noDts => ({ + // Map is collected after file open + closedInfos: noDts.closedInfos.concat(dtsMapLocation), + expectsMap: true + })], + dependencyChange: ["main", () => ({ + action: renameFromDependencyTsWithDependencyChange + })], + noBuild: "noDts" } ] }); }); describe("when opening depedency and usage project", () => { - function goToDefActionInfo(withRefs: boolean): ActionInfo { - return { - action: goToDefFromMainTs, - // DependencyTs is open, so omit it from closed infos - closedInfos: () => withRefs ? - [dependencyConfig.path, libFile.path] : - [libFile.path, dtsPath, dtsMapLocation], - otherWatchedFiles: () => withRefs ? - [mainConfig.path] : // Its in closed info - [mainConfig.path, dependencyConfig.path], - expectsDts: !withRefs, // Dts script info present only if no project reference - expectsMap: !withRefs // Map script info present only if no project reference - }; - } - - function renameActionInfo(withRefs: boolean): ActionInfo { - return { - action: renameFromDependencyTsWithBothProjectsOpen, - // DependencyTs is open, so omit it from closed infos - closedInfos: () => withRefs ? - [dependencyConfig.path, libFile.path, dtsLocation, dtsMapLocation] : - [libFile.path, dtsPath, dtsMapLocation], - otherWatchedFiles: () => withRefs ? - [mainConfig.path] : // Its in closed info - [mainConfig.path, dependencyConfig.path], - expectsDts: true, - expectsMap: true - }; - } - - function goToDefNoMapActionInfo(withRefs: boolean): ActionInfo { - return { - action: withRefs ? - goToDefFromMainTs : - goToDefFromMainTsWithNoMap, - closedInfos: () => withRefs ? - [dependencyConfig.path, libFile.path] : - [libFile.path, dtsPath], // No map - otherWatchedFiles: () => withRefs ? - [mainConfig.path] : // Its in closed info - [mainConfig.path, dependencyConfig.path, dtsMapLocation], // Watch map file - expectsDts: !withRefs, // Dts script info present only if no project reference - expectsMap: false - }; - } - - function renameNoMapActionInfo(withRefs: boolean): ActionInfo { - return { - ...renameActionInfo(withRefs), - action: withRefs ? - renameFromDependencyTsWithBothProjectsOpen : - renameFromDependencyTs, - closedInfos: () => withRefs ? - [dependencyConfig.path, libFile.path, dtsLocation] : - [libFile.path, dtsPath], // No map - otherWatchedFiles: () => withRefs ? - [mainConfig.path, dtsMapLocation] : // Its in closed info - [mainConfig.path, dependencyConfig.path, dtsMapLocation], // Watch map file - expectsMap: false - }; - } - - function goToDefNoDtsActionInfo(withRefs: boolean): ActionInfo { - return { - ...goToDefActionInfo(withRefs), - action: withRefs ? - goToDefFromMainTs : - goToDefFromMainTsWithNoDts, - closedInfos: () => withRefs ? - [dependencyConfig.path, libFile.path] : - [libFile.path], // No dts or map, - expectsDts: false, - expectsMap: false - }; - } - - function renameNoDtsActionInfo(withRefs: boolean): ActionInfo { - return { - action: withRefs ? - renameFromDependencyTsWithBothProjectsOpen : - renameFromDependencyTs, - closedInfos: () => withRefs ? - [dependencyConfig.path, libFile.path] : - [libFile.path], // No dts or map, - otherWatchedFiles: () => withRefs ? - [mainConfig.path, dtsLocation] : - [mainConfig.path, dependencyConfig.path, dtsPath], // Watch dts, - expectsDts: false, - expectsMap: false - }; - } - verifyScenario({ mainScenario: "goto Definition in usage and rename locations from defining project", verifier: withRefs => [ { ...goToDefFromMainTsProjectInfoVerifier(withRefs), - main: () => goToDefActionInfo(withRefs), - change: () => ({ - // Because before this rename is done the closed info remains same as rename's main operation - ...goToDefActionInfo(withRefs), - closedInfos: () => withRefs ? - [dependencyConfig.path, libFile.path, dtsLocation, dtsMapLocation] : + main: () => ({ + action: goToDefFromMainTs, + // DependencyTs is open, so omit it from closed infos + closedInfos: withRefs ? + [dependencyConfig.path, libFile.path] : [libFile.path, dtsPath, dtsMapLocation], - expectsDts: true, - expectsMap: true + otherWatchedFiles: withRefs ? + [mainConfig.path] : // Its in closed info + [mainConfig.path, dependencyConfig.path], + expectsDts: !withRefs, // Dts script info present only if no project reference + expectsMap: !withRefs // Map script info present only if no project reference }), + change: withRefs ? + ["main", main => ({ + // Because before this rename is done the closed info remains same as rename's main operation + closedInfos: main.closedInfos.concat(dtsLocation, dtsMapLocation), + expectsDts: true, + expectsMap: true + })] : + "main", dtsChange: "change", mapChange: "change", - noMap: () => goToDefNoMapActionInfo(withRefs), - mapFileCreated: () => ({ - // Because before this rename is done the closed info remains same as rename's main - ...goToDefActionInfo(withRefs), - closedInfos: () => withRefs ? - [dependencyConfig.path, libFile.path, dtsLocation] : - [libFile.path, dtsPath, dtsMapLocation], - expectsDts: true, - // This operation doesnt need map so the map info path in dts is not refreshed - skipDtsMapCheck: withRefs - }), - mapFileDeleted: () => ({ - // Because before this rename is done the closed info remains same as rename's noMap operation - ...goToDefNoMapActionInfo(withRefs), - closedInfos: () => withRefs ? - [dependencyConfig.path, libFile.path, dtsLocation] : - [libFile.path, dtsPath], // No map, - expectsDts: true, - // This operation doesnt need map so the map info path in dts is not refreshed - skipDtsMapCheck: withRefs - }), - noDts: () => goToDefNoDtsActionInfo(withRefs), - dtsFileCreated: () => ({ - ...goToDefActionInfo(withRefs), - // Since the project for dependency is not updated, the watcher from rename for dts still there - otherWatchedFiles: () => withRefs ? - [mainConfig.path, dtsLocation] : - [mainConfig.path, dependencyConfig.path], - }), - dtsFileDeleted: () => ({ - ...goToDefNoDtsActionInfo(withRefs), + noMap: withRefs ? + "main" : + ["main", main => ({ + action: goToDefFromMainTsWithNoMap, + closedInfos: removePath(main.closedInfos, dtsMapPath), + otherWatchedFiles: main.otherWatchedFiles.concat(dtsMapLocation), + expectsMap: false + })], + mapFileCreated: withRefs ? + ["main", main => ({ + // Because before this rename is done the closed info remains same as rename's main + closedInfos: main.closedInfos.concat(dtsLocation), + expectsDts: true, + // This operation doesnt need map so the map info path in dts is not refreshed + skipDtsMapCheck: withRefs + })] : + "main", + mapFileDeleted: withRefs ? + ["noMap", noMap => ({ + // Because before this rename is done the closed info remains same as rename's noMap operation + closedInfos: noMap.closedInfos.concat(dtsLocation), + expectsDts: true, + // This operation doesnt need map so the map info path in dts is not refreshed + skipDtsMapCheck: true + })] : + "noMap", + noDts: withRefs ? + "main" : + ["main", main => ({ + action: goToDefFromMainTsWithNoDts, + closedInfos: removePath(main.closedInfos, dtsMapPath, dtsPath), + expectsDts: false, + expectsMap: false + })], + dtsFileCreated: withRefs ? + ["main", main => ({ + // Since the project for dependency is not updated, the watcher from rename for dts still there + otherWatchedFiles: main.otherWatchedFiles.concat(dtsLocation) + })] : + "main", + dtsFileDeleted: ["noDts", noDts => ({ // Map collection after file open - closedInfos: () => withRefs ? - [dependencyConfig.path, libFile.path, dtsMapLocation] : - [libFile.path, dtsMapLocation], + closedInfos: noDts.closedInfos.concat(dtsMapLocation), expectsMap: true - }), - dependencyChange: () => ({ - ...goToDefActionInfo(withRefs), + })], + dependencyChange: ["change", () => ({ action: goToDefFromMainTsWithDependencyChange, - // From rename main action - closedInfos: () => withRefs ? - [dependencyConfig.path, libFile.path, dtsLocation, dtsMapLocation] : - [libFile.path, dtsPath, dtsMapLocation], - expectsDts: withRefs, - expectsMap: withRefs - }), - noBuild: "main" + })], + noBuild: "noDts" }, { ...renameFromDependencyTsProjectInfoVerifier(), main: () => ({ - ...renameActionInfo(withRefs), + action: renameFromDependencyTsWithBothProjectsOpen, + // DependencyTs is open, so omit it from closed infos + closedInfos: withRefs ? + [dependencyConfig.path, libFile.path, dtsLocation, dtsMapLocation] : + [libFile.path, dtsPath, dtsMapLocation], + otherWatchedFiles: withRefs ? + [mainConfig.path] : // Its in closed info + [mainConfig.path, dependencyConfig.path], + expectsDts: true, + expectsMap: true, freshMapInfo: withRefs }), - change: () => renameActionInfo(withRefs), + change: ["main", () => ({ + freshMapInfo: false + })], dtsChange: "change", - mapChange: () => ({ - ...renameActionInfo(withRefs), + mapChange: ["main", () => ({ + freshMapInfo: false, freshDocumentMapper: withRefs - }), - noMap: () => ({ - ...renameNoMapActionInfo(withRefs), - freshMapInfo: withRefs, + })], + noMap: ["main", main => ({ + action: withRefs ? + renameFromDependencyTsWithBothProjectsOpen : + renameFromDependencyTs, + closedInfos: removePath(main.closedInfos, dtsMapPath), + otherWatchedFiles: main.otherWatchedFiles.concat(dtsMapLocation), + expectsMap: false, freshDocumentMapper: withRefs - }), + })], mapFileCreated: "main", mapFileDeleted: "noMap", - noDts: () => renameNoDtsActionInfo(withRefs), - dtsFileCreated: "main", - dtsFileDeleted: () => ({ - ...renameNoDtsActionInfo(withRefs), - // Map collection after file open - closedInfos: () => withRefs ? - [dependencyConfig.path, libFile.path, dtsMapLocation] : - [libFile.path, dtsMapLocation], - expectsMap: true - }), - dependencyChange: () => ({ - ...renameActionInfo(withRefs), - action: renameFromDependencyTsWithBothProjectsOpenWithDependencyChange - }), - noBuild: () => ({ - action: renameFromDependencyTsWithBothProjectsOpen, - closedInfos: () => withRefs ? - [dependencyConfig.path, libFile.path] : - [libFile.path], - otherWatchedFiles: () => withRefs ? - [mainConfig.path, dtsLocation] : // Its in closed info - [mainConfig.path, dependencyConfig.path], + noDts: ["change", change => ({ + action: withRefs ? + renameFromDependencyTsWithBothProjectsOpen : + renameFromDependencyTs, + closedInfos: removePath(change.closedInfos, dtsPath, dtsMapPath), + otherWatchedFiles: change.otherWatchedFiles.concat(dtsLocation), expectsDts: false, expectsMap: false - }) + })], + dtsFileCreated: "main", + dtsFileDeleted: ["noDts", noDts => ({ + // Map collection after file open + closedInfos: noDts.closedInfos.concat(dtsMapLocation) , + expectsMap: true + })], + dependencyChange: ["change", () => ({ + action: renameFromDependencyTsWithBothProjectsOpenWithDependencyChange + })], + noBuild: "noDts" } ] }); From b1fa2ebff5bfcd8bc69932cbec01d0c1479f091f Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Fri, 5 Jul 2019 12:17:01 -0700 Subject: [PATCH 18/97] Errors using DiagnosticsSync commands --- .../tsserver/projectReferenceErrors.ts | 88 ++++++++++++++++--- 1 file changed, 74 insertions(+), 14 deletions(-) diff --git a/src/testRunner/unittests/tsserver/projectReferenceErrors.ts b/src/testRunner/unittests/tsserver/projectReferenceErrors.ts index 8006b916037..303808cb4e4 100644 --- a/src/testRunner/unittests/tsserver/projectReferenceErrors.ts +++ b/src/testRunner/unittests/tsserver/projectReferenceErrors.ts @@ -61,7 +61,7 @@ fnErr(); interface CheckAllErrors { session: TestSession; host: TestServerHost; - expected: ReadonlyArray; + expected: readonly GetErrDiagnostics[]; expectedSequenceId: number; } function checkAllErrors({ session, host, expected, expectedSequenceId }: CheckAllErrors) { @@ -118,6 +118,40 @@ fnErr(); }); } + function verifyErrorsUsingSyncMethods({ openFiles, expectedSyncDiagnostics }: VerifyScenario) { + it("verifies the errors using sync commands", () => { + const host = createServerHost([dependencyTs, dependencyConfig, usageTs, usageConfig, libFile]); + const session = createSession(host); + openFilesForSession(openFiles(), session); + for (const { file, project, syntax, semantic, suggestion } of expectedSyncDiagnostics()) { + const actualSyntax = session.executeCommandSeq({ + command: protocol.CommandTypes.SyntacticDiagnosticsSync, + arguments: { + file: file.path, + projectFileName: project + } + }).response as protocol.Diagnostic[]; + assert.deepEqual(actualSyntax, syntax, `Syntax diagnostics for file: ${file.path}, project: ${project}`); + const actualSemantic = session.executeCommandSeq({ + command: protocol.CommandTypes.SemanticDiagnosticsSync, + arguments: { + file: file.path, + projectFileName: project + } + }).response as protocol.Diagnostic[]; + assert.deepEqual(actualSemantic, semantic, `Semantic diagnostics for file: ${file.path}, project: ${project}`); + const actualSuggestion = session.executeCommandSeq({ + command: protocol.CommandTypes.SuggestionDiagnosticsSync, + arguments: { + file: file.path, + projectFileName: project + } + }).response as protocol.Diagnostic[]; + assert.deepEqual(actualSuggestion, suggestion, `Suggestion diagnostics for file: ${file.path}, project: ${project}`); + } + }); + } + interface GetErrDiagnostics { file: File; syntax: protocol.Diagnostic[]; @@ -126,16 +160,21 @@ fnErr(); } interface GetErrForProjectDiagnostics { project: string; - errors: ReadonlyArray; + errors: readonly GetErrDiagnostics[]; + } + interface SyncDiagnostics extends GetErrDiagnostics { + project?: string; } interface VerifyScenario { - openFiles: () => ReadonlyArray; - expectedGetErr: () => ReadonlyArray; - expectedGetErrForProject: () => ReadonlyArray; + openFiles: () => readonly File[]; + expectedGetErr: () => readonly GetErrDiagnostics[]; + expectedGetErrForProject: () => readonly GetErrForProjectDiagnostics[]; + expectedSyncDiagnostics: () => readonly SyncDiagnostics[]; } function verifyScenario(scenario: VerifyScenario) { verifyErrorsUsingGeterr(scenario); verifyErrorsUsingGeterrForProject(scenario); + verifyErrorsUsingSyncMethods(scenario); } function emptyDiagnostics(file: File): GetErrDiagnostics { @@ -169,13 +208,13 @@ fnErr(); file: dependencyTs, syntax: emptyArray, semantic: [ - createDiagnostic( - { line: 6, offset: 12 }, - { line: 6, offset: 13 }, - Diagnostics.Type_0_is_not_assignable_to_type_1, - ["10", "string"], - "error", - ) + createDiagnostic( + { line: 6, offset: 12 }, + { line: 6, offset: 13 }, + Diagnostics.Type_0_is_not_assignable_to_type_1, + ["10", "string"], + "error", + ) ], suggestion: emptyArray }; @@ -200,6 +239,10 @@ fnErr(); }; } + function syncDiagnostics(diagnostics: GetErrDiagnostics, project: string): SyncDiagnostics { + return { project, ...diagnostics }; + } + describe("when dependency project is not open", () => { verifyScenario({ openFiles: () => [usageTs], @@ -215,7 +258,15 @@ fnErr(); usageDiagnostics() ] } - ] + ], + expectedSyncDiagnostics: () => [ + // Without project + usageDiagnostics(), + emptyDiagnostics(dependencyTs), + // With project + syncDiagnostics(usageDiagnostics(), usageConfig.path), + syncDiagnostics(emptyDiagnostics(dependencyTs), usageConfig.path), + ], }); }); @@ -229,7 +280,16 @@ fnErr(); expectedGetErrForProject: () => [ usageProjectDiagnostics(), dependencyProjectDiagnostics() - ] + ], + expectedSyncDiagnostics: () => [ + // Without project + usageDiagnostics(), + dependencyDiagnostics(), + // With project + syncDiagnostics(usageDiagnostics(), usageConfig.path), + syncDiagnostics(emptyDiagnostics(dependencyTs), usageConfig.path), + syncDiagnostics(dependencyDiagnostics(), dependencyConfig.path), + ], }); }); }); From 824c22c460b9a57b9c67398890449cd6799fb4e0 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Mon, 8 Jul 2019 15:55:35 -0700 Subject: [PATCH 19/97] Source of project reference behave as if those files cannot be emitted. --- src/compiler/sys.ts | 47 +- src/harness/virtualFileSystemWithWatch.ts | 15 + src/server/project.ts | 9 +- src/server/session.ts | 4 +- src/testRunner/tsconfig.json | 1 + src/testRunner/unittests/tsbuildWatchMode.ts | 14 +- src/testRunner/unittests/tsserver/helpers.ts | 2 +- .../tsserver/projectReferenceCompileOnSave.ts | 410 ++++++++++++++++++ .../reference/api/tsserverlibrary.d.ts | 1 - 9 files changed, 468 insertions(+), 35 deletions(-) create mode 100644 src/testRunner/unittests/tsserver/projectReferenceCompileOnSave.ts diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index 5abc19cdbc4..b7eca0ee51c 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -472,6 +472,33 @@ namespace ts { } } + function recursiveCreateDirectory(directoryPath: string, sys: System) { + const basePath = getDirectoryPath(directoryPath); + const shouldCreateParent = basePath !== "" && directoryPath !== basePath && !sys.directoryExists(basePath); + if (shouldCreateParent) { + recursiveCreateDirectory(basePath, sys); + } + if (shouldCreateParent || !sys.directoryExists(directoryPath)) { + sys.createDirectory(directoryPath); + } + } + + /** + * patch writefile to create folder before writing the file + */ + /*@internal*/ + export function patchWriteFileEnsuringDirectory(sys: System) { + // patch writefile to create folder before writing the file + const originalWriteFile = sys.writeFile; + sys.writeFile = (path, data, writeBom) => { + const directoryPath = getDirectoryPath(normalizeSlashes(path)); + if (directoryPath && !sys.directoryExists(directoryPath)) { + recursiveCreateDirectory(directoryPath, sys); + } + originalWriteFile.call(sys, path, data, writeBom); + }; + } + /*@internal*/ interface NodeBuffer extends Uint8Array { write(str: string, offset?: number, length?: number, encoding?: string): number; @@ -1259,17 +1286,6 @@ namespace ts { }; } - function recursiveCreateDirectory(directoryPath: string, sys: System) { - const basePath = getDirectoryPath(directoryPath); - const shouldCreateParent = basePath !== "" && directoryPath !== basePath && !sys.directoryExists(basePath); - if (shouldCreateParent) { - recursiveCreateDirectory(basePath, sys); - } - if (shouldCreateParent || !sys.directoryExists(directoryPath)) { - sys.createDirectory(directoryPath); - } - } - let sys: System | undefined; if (typeof ChakraHost !== "undefined") { sys = getChakraSystem(); @@ -1281,14 +1297,7 @@ namespace ts { } if (sys) { // patch writefile to create folder before writing the file - const originalWriteFile = sys.writeFile; - sys.writeFile = (path, data, writeBom) => { - const directoryPath = getDirectoryPath(normalizeSlashes(path)); - if (directoryPath && !sys!.directoryExists(directoryPath)) { - recursiveCreateDirectory(directoryPath, sys!); - } - originalWriteFile.call(sys, path, data, writeBom); - }; + patchWriteFileEnsuringDirectory(sys); } return sys!; })(); diff --git a/src/harness/virtualFileSystemWithWatch.ts b/src/harness/virtualFileSystemWithWatch.ts index 9e615a02638..23127f49aad 100644 --- a/src/harness/virtualFileSystemWithWatch.ts +++ b/src/harness/virtualFileSystemWithWatch.ts @@ -66,6 +66,8 @@ interface Array {}` params.newLine, params.useWindowsStylePaths, params.environmentVariables); + // Just like sys, patch the host to use writeFile + patchWriteFileEnsuringDirectory(host); return host; } @@ -990,6 +992,19 @@ interface Array {}` } } + export type TestServerHostTrackingWrittenFiles = TestServerHost & { writtenFiles: Map; }; + + export function changeToHostTrackingWrittenFiles(inputHost: TestServerHost) { + const host = inputHost as TestServerHostTrackingWrittenFiles; + const originalWriteFile = host.writeFile; + host.writtenFiles = createMap(); + host.writeFile = (fileName, content) => { + originalWriteFile.call(host, fileName, content); + const path = host.toFullPath(fileName); + host.writtenFiles.set(path, true); + }; + return host; + } export const tsbuildProjectsLocation = "/user/username/projects"; export function getTsBuildProjectFilePath(project: string, file: string) { return `${tsbuildProjectsLocation}/${project}/${file}`; diff --git a/src/server/project.ts b/src/server/project.ts index cd5a3fec855..2811b04c413 100644 --- a/src/server/project.ts +++ b/src/server/project.ts @@ -537,8 +537,11 @@ namespace ts.server { return this.projectService.getSourceFileLike(fileName, this); } - private shouldEmitFile(scriptInfo: ScriptInfo) { - return scriptInfo && !scriptInfo.isDynamicOrHasMixedContent(); + /*@internal*/ + shouldEmitFile(scriptInfo: ScriptInfo | undefined) { + return scriptInfo && + !scriptInfo.isDynamicOrHasMixedContent() && + !this.program!.isSourceOfProjectReferenceRedirect(scriptInfo.path); } getCompileOnSaveAffectedFileList(scriptInfo: ScriptInfo): string[] { @@ -548,7 +551,7 @@ namespace ts.server { updateProjectIfDirty(this); this.builderState = BuilderState.create(this.program!, this.projectService.toCanonicalFileName, this.builderState); return mapDefined(BuilderState.getFilesAffectedBy(this.builderState, this.program!, scriptInfo.path, this.cancellationToken, data => this.projectService.host.createHash!(data)), // TODO: GH#18217 - sourceFile => this.shouldEmitFile(this.projectService.getScriptInfoForPath(sourceFile.path)!) ? sourceFile.fileName : undefined); + sourceFile => this.shouldEmitFile(this.projectService.getScriptInfoForPath(sourceFile.path)) ? sourceFile.fileName : undefined); } /** diff --git a/src/server/session.ts b/src/server/session.ts index a5c13574805..ff080998552 100644 --- a/src/server/session.ts +++ b/src/server/session.ts @@ -1030,7 +1030,9 @@ namespace ts.server { private getEmitOutput(args: protocol.FileRequestArgs): EmitOutput { const { file, project } = this.getFileAndProject(args); - return project.getLanguageService().getEmitOutput(file); + return project.shouldEmitFile(project.getScriptInfo(file)) ? + project.getLanguageService().getEmitOutput(file) : + { emitSkipped: true, outputFiles: [] }; } private mapDefinitionInfo(definitions: ReadonlyArray, project: Project): ReadonlyArray { diff --git a/src/testRunner/tsconfig.json b/src/testRunner/tsconfig.json index 9d67215ffd9..ee2aba6275f 100644 --- a/src/testRunner/tsconfig.json +++ b/src/testRunner/tsconfig.json @@ -137,6 +137,7 @@ "unittests/tsserver/occurences.ts", "unittests/tsserver/openFile.ts", "unittests/tsserver/projectErrors.ts", + "unittests/tsserver/projectReferenceCompileOnSave.ts", "unittests/tsserver/projectReferenceErrors.ts", "unittests/tsserver/projectReferences.ts", "unittests/tsserver/projects.ts", diff --git a/src/testRunner/unittests/tsbuildWatchMode.ts b/src/testRunner/unittests/tsbuildWatchMode.ts index 6a577888c7e..dcc2fdbb1ac 100644 --- a/src/testRunner/unittests/tsbuildWatchMode.ts +++ b/src/testRunner/unittests/tsbuildWatchMode.ts @@ -2,18 +2,12 @@ namespace ts.tscWatch { import projectsLocation = TestFSWithWatch.tsbuildProjectsLocation; import getFilePathInProject = TestFSWithWatch.getTsBuildProjectFilePath; import getFileFromProject = TestFSWithWatch.getTsBuildProjectFile; - type TsBuildWatchSystem = WatchedSystem & { writtenFiles: Map; }; + type TsBuildWatchSystem = TestFSWithWatch.TestServerHostTrackingWrittenFiles; function createTsBuildWatchSystem(fileOrFolderList: ReadonlyArray, params?: TestFSWithWatch.TestServerHostCreationParameters) { - const host = createWatchedSystem(fileOrFolderList, params) as TsBuildWatchSystem; - const originalWriteFile = host.writeFile; - host.writtenFiles = createMap(); - host.writeFile = (fileName, content) => { - originalWriteFile.call(host, fileName, content); - const path = host.toFullPath(fileName); - host.writtenFiles.set(path, true); - }; - return host; + return TestFSWithWatch.changeToHostTrackingWrittenFiles( + createWatchedSystem(fileOrFolderList, params) + ); } export function createSolutionBuilder(system: WatchedSystem, rootNames: ReadonlyArray, defaultOptions?: BuildOptions) { diff --git a/src/testRunner/unittests/tsserver/helpers.ts b/src/testRunner/unittests/tsserver/helpers.ts index 1c6bd4e9f59..12611f79266 100644 --- a/src/testRunner/unittests/tsserver/helpers.ts +++ b/src/testRunner/unittests/tsserver/helpers.ts @@ -498,7 +498,7 @@ namespace ts.projectSystem { return protocolToLocation(str)(start); } - function protocolToLocation(text: string): (pos: number) => protocol.Location { + export function protocolToLocation(text: string): (pos: number) => protocol.Location { const lineStarts = computeLineStarts(text); return pos => { const x = computeLineAndCharacterOfPosition(lineStarts, pos); diff --git a/src/testRunner/unittests/tsserver/projectReferenceCompileOnSave.ts b/src/testRunner/unittests/tsserver/projectReferenceCompileOnSave.ts new file mode 100644 index 00000000000..9602ef6360e --- /dev/null +++ b/src/testRunner/unittests/tsserver/projectReferenceCompileOnSave.ts @@ -0,0 +1,410 @@ +namespace ts.projectSystem { + describe("unittests:: tsserver:: with project references and compile on save", () => { + const projectLocation = "/user/username/projects/myproject"; + const dependecyLocation = `${projectLocation}/dependency`; + const usageLocation = `${projectLocation}/usage`; + const dependencyTs: File = { + path: `${dependecyLocation}/fns.ts`, + content: `export function fn1() { } +export function fn2() { } +` + }; + const dependencyConfig: File = { + path: `${dependecyLocation}/tsconfig.json`, + content: JSON.stringify({ + compilerOptions: { composite: true, declarationDir: "../decls" }, + compileOnSave: true + }) + }; + const usageTs: File = { + path: `${usageLocation}/usage.ts`, + content: `import { + fn1, + fn2, +} from '../decls/fns' +fn1(); +fn2(); +` + }; + const usageConfig: File = { + path: `${usageLocation}/tsconfig.json`, + content: JSON.stringify({ + compileOnSave: true, + references: [{ path: "../dependency" }] + }) + }; + + interface VerifySingleScenarioWorker extends VerifySingleScenario { + withProject: boolean; + } + function verifySingleScenarioWorker({ + withProject, scenario, openFiles, requestArgs, change, expectedResult + }: VerifySingleScenarioWorker) { + it(scenario, () => { + const host = TestFSWithWatch.changeToHostTrackingWrittenFiles( + createServerHost([dependencyTs, dependencyConfig, usageTs, usageConfig, libFile]) + ); + const session = createSession(host); + openFilesForSession(openFiles(), session); + const reqArgs = requestArgs(); + const { + expectedAffected, + expectedEmit: { expectedEmitSuccess, expectedFiles }, + expectedEmitOutput + } = expectedResult(withProject); + + if (change) { + session.executeCommandSeq({ + command: protocol.CommandTypes.CompileOnSaveAffectedFileList, + arguments: { file: dependencyTs.path } + }); + const { file, insertString } = change(); + if (session.getProjectService().openFiles.has(file.path)) { + const toLocation = protocolToLocation(file.content); + const location = toLocation(file.content.length); + session.executeCommandSeq({ + command: protocol.CommandTypes.Change, + arguments: { + file: file.path, + ...location, + endLine: location.line, + endOffset: location.offset, + insertString + } + }); + } + else { + host.writeFile(file.path, `${file.content}${insertString}`); + } + host.writtenFiles.clear(); + } + + const args = withProject ? reqArgs : { file: reqArgs.file }; + // Verify CompileOnSaveAffectedFileList + const actualAffectedFiles = session.executeCommandSeq({ + command: protocol.CommandTypes.CompileOnSaveAffectedFileList, + arguments: args + }).response as protocol.CompileOnSaveAffectedFileListSingleProject[]; + assert.deepEqual(actualAffectedFiles, expectedAffected, "Affected files"); + + // Verify CompileOnSaveEmit + const actualEmit = session.executeCommandSeq({ + command: protocol.CommandTypes.CompileOnSaveEmitFile, + arguments: args + }).response; + assert.deepEqual(actualEmit, expectedEmitSuccess, "Emit files"); + assert.equal(host.writtenFiles.size, expectedFiles.length); + for (const file of expectedFiles) { + assert.equal(host.readFile(file.path), file.content, `Expected to write ${file.path}`); + assert.isTrue(host.writtenFiles.has(file.path), `${file.path} is newly written`); + } + + // Verify EmitOutput + const { exportedModulesFromDeclarationEmit: _1, ...actualEmitOutput } = session.executeCommandSeq({ + command: protocol.CommandTypes.EmitOutput, + arguments: args + }).response as EmitOutput; + assert.deepEqual(actualEmitOutput, expectedEmitOutput, "Emit output"); + }); + } + + interface VerifySingleScenario { + scenario: string; + openFiles: () => readonly File[]; + requestArgs: () => protocol.FileRequestArgs; + skipWithoutProject?: boolean; + change?: () => SingleScenarioChange; + expectedResult: GetSingleScenarioResult; + } + function verifySingleScenario(scenario: VerifySingleScenario) { + if (!scenario.skipWithoutProject) { + describe("without specifying project file", () => { + verifySingleScenarioWorker({ + withProject: false, + ...scenario + }); + }); + } + describe("with specifying project file", () => { + verifySingleScenarioWorker({ + withProject: true, + ...scenario + }); + }); + } + + interface SingleScenarioExpectedEmit { + expectedEmitSuccess: boolean; + expectedFiles: readonly File[]; + } + interface SingleScenarioResult { + expectedAffected: protocol.CompileOnSaveAffectedFileListSingleProject[]; + expectedEmit: SingleScenarioExpectedEmit; + expectedEmitOutput: EmitOutput; + } + type GetSingleScenarioResult = (withProject: boolean) => SingleScenarioResult; + interface SingleScenarioChange { + file: File; + insertString: string; + } + interface ScenarioDetails { + scenarioName: string; + requestArgs: () => protocol.FileRequestArgs; + skipWithoutProject?: boolean; + initial: GetSingleScenarioResult; + localChangeToDependency: GetSingleScenarioResult; + localChangeToUsage: GetSingleScenarioResult; + changeToDependency: GetSingleScenarioResult; + changeToUsage: GetSingleScenarioResult; + } + interface VerifyScenario { + openFiles: () => readonly File[]; + scenarios: readonly ScenarioDetails[]; + } + + const localChange = "function fn3() { }"; + const change = `export ${localChange}`; + const changeJs = `function fn3() { } +exports.fn3 = fn3;`; + const changeDts = "export declare function fn3(): void;"; + function verifyScenario({ openFiles, scenarios }: VerifyScenario) { + for (const { + scenarioName, requestArgs, skipWithoutProject, initial, + localChangeToDependency, localChangeToUsage, + changeToDependency, changeToUsage + } of scenarios) { + describe(scenarioName, () => { + verifySingleScenario({ + scenario: "with initial file open", + openFiles, + requestArgs, + skipWithoutProject, + expectedResult: initial + }); + + verifySingleScenario({ + scenario: "with local change to dependency", + openFiles, + requestArgs, + skipWithoutProject, + change: () => ({ file: dependencyTs, insertString: localChange }), + expectedResult: localChangeToDependency + }); + + verifySingleScenario({ + scenario: "with local change to usage", + openFiles, + requestArgs, + skipWithoutProject, + change: () => ({ file: usageTs, insertString: localChange }), + expectedResult: localChangeToUsage + }); + + verifySingleScenario({ + scenario: "with change to dependency", + openFiles, + requestArgs, + skipWithoutProject, + change: () => ({ file: dependencyTs, insertString: change }), + expectedResult: changeToDependency + }); + + verifySingleScenario({ + scenario: "with change to usage", + openFiles, + requestArgs, + skipWithoutProject, + change: () => ({ file: usageTs, insertString: change }), + expectedResult: changeToUsage + }); + }); + } + } + + function expectedAffectedFiles(config: File, fileNames: File[]): protocol.CompileOnSaveAffectedFileListSingleProject { + return { + projectFileName: config.path, + fileNames: fileNames.map(f => f.path), + projectUsesOutFile: false + }; + } + + function expectedUsageEmit(appendJsText?: string): SingleScenarioExpectedEmit { + const appendJs = appendJsText ? `${appendJsText} +` : ""; + return { + expectedEmitSuccess: true, + expectedFiles: [{ + path: `${usageLocation}/usage.js`, + content: `"use strict"; +exports.__esModule = true; +var fns_1 = require("../decls/fns"); +fns_1.fn1(); +fns_1.fn2(); +${appendJs}` + }] + }; + } + + function expectedEmitOutput({ expectedFiles }: SingleScenarioExpectedEmit): EmitOutput { + return { + outputFiles: expectedFiles.map(({ path, content }) => ({ + name: path, + text: content, + writeByteOrderMark: false + })), + emitSkipped: false + }; + } + + function expectedUsageEmitOutput(appendJsText?: string): EmitOutput { + return expectedEmitOutput(expectedUsageEmit(appendJsText)); + } + + function noEmit(): SingleScenarioExpectedEmit { + return { + expectedEmitSuccess: false, + expectedFiles: emptyArray + }; + } + + function noEmitOutput(): EmitOutput { + return { + emitSkipped: true, + outputFiles: [] + }; + } + + function expectedDependencyEmit(appendJsText?: string, appendDtsText?: string): SingleScenarioExpectedEmit { + const appendJs = appendJsText ? `${appendJsText} +` : ""; + const appendDts = appendDtsText ? `${appendDtsText} +` : ""; + return { + expectedEmitSuccess: true, + expectedFiles: [ + { + path: `${dependecyLocation}/fns.js`, + content: `"use strict"; +exports.__esModule = true; +function fn1() { } +exports.fn1 = fn1; +function fn2() { } +exports.fn2 = fn2; +${appendJs}` + }, + { + path: `${projectLocation}/decls/fns.d.ts`, + content: `export declare function fn1(): void; +export declare function fn2(): void; +${appendDts}` + } + ] + }; + } + + function expectedDependencyEmitOutput(appendJsText?: string, appendDtsText?: string): EmitOutput { + return expectedEmitOutput(expectedDependencyEmit(appendJsText, appendDtsText)); + } + + function scenarioDetailsOfUsage(isDependencyOpen?: boolean): ScenarioDetails[] { + return [ + { + scenarioName: "Of usageTs", + requestArgs: () => ({ file: usageTs.path, projectFileName: usageConfig.path }), + initial: () => initialUsageTs(), + // no change to usage so same as initial only usage file + localChangeToDependency: () => initialUsageTs(), + localChangeToUsage: () => initialUsageTs(localChange), + changeToDependency: () => initialUsageTs(), + changeToUsage: () => initialUsageTs(changeJs) + }, + { + scenarioName: "Of dependencyTs in usage project", + requestArgs: () => ({ file: dependencyTs.path, projectFileName: usageConfig.path }), + skipWithoutProject: !!isDependencyOpen, + initial: () => initialDependencyTs(), + localChangeToDependency: () => initialDependencyTs(/*noUsageFiles*/ true), + localChangeToUsage: () => initialDependencyTs(/*noUsageFiles*/ true), + changeToDependency: () => initialDependencyTs(), + changeToUsage: () => initialDependencyTs(/*noUsageFiles*/ true) + } + ]; + + function initialUsageTs(jsText?: string) { + return { + expectedAffected: [ + expectedAffectedFiles(usageConfig, [usageTs]) + ], + expectedEmit: expectedUsageEmit(jsText), + expectedEmitOutput: expectedUsageEmitOutput(jsText) + }; + } + + function initialDependencyTs(noUsageFiles?: true) { + return { + expectedAffected: [ + expectedAffectedFiles(usageConfig, noUsageFiles ? [] : [usageTs]) + ], + expectedEmit: noEmit(), + expectedEmitOutput: noEmitOutput() + }; + } + } + + function scenarioDetailsOfDependencyWhenOpen(): ScenarioDetails { + return { + scenarioName: "Of dependencyTs", + requestArgs: () => ({ file: dependencyTs.path, projectFileName: dependencyConfig.path }), + initial, + localChangeToDependency: withProject => ({ + expectedAffected: withProject ? + [ + expectedAffectedFiles(dependencyConfig, [dependencyTs]) + ] : + [ + expectedAffectedFiles(usageConfig, []), + expectedAffectedFiles(dependencyConfig, [dependencyTs]) + ], + expectedEmit: expectedDependencyEmit(localChange), + expectedEmitOutput: expectedDependencyEmitOutput(localChange) + }), + localChangeToUsage: withProject => initial(withProject, /*noUsageFiles*/ true), + changeToDependency: withProject => initial(withProject, /*noUsageFiles*/ undefined, changeJs, changeDts), + changeToUsage: withProject => initial(withProject, /*noUsageFiles*/ true) + }; + + function initial(withProject: boolean, noUsageFiles?: true, appendJs?: string, appendDts?: string): SingleScenarioResult { + return { + expectedAffected: withProject ? + [ + expectedAffectedFiles(dependencyConfig, [dependencyTs]) + ] : + [ + expectedAffectedFiles(usageConfig, noUsageFiles ? [] : [usageTs]), + expectedAffectedFiles(dependencyConfig, [dependencyTs]) + ], + expectedEmit: expectedDependencyEmit(appendJs, appendDts), + expectedEmitOutput: expectedDependencyEmitOutput(appendJs, appendDts) + }; + } + } + + describe("when dependency project is not open", () => { + verifyScenario({ + openFiles: () => [usageTs], + scenarios: scenarioDetailsOfUsage() + }); + }); + + describe("when the depedency file is open", () => { + verifyScenario({ + openFiles: () => [usageTs, dependencyTs], + scenarios: [ + ...scenarioDetailsOfUsage(/*isDependencyOpen*/ true), + scenarioDetailsOfDependencyWhenOpen(), + ] + }); + }); + }); +} diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index ea7003e471f..0b728ac95d8 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -8463,7 +8463,6 @@ declare namespace ts.server { getGlobalProjectErrors(): ReadonlyArray; getAllProjectErrors(): ReadonlyArray; getLanguageService(ensureSynchronized?: boolean): LanguageService; - private shouldEmitFile; getCompileOnSaveAffectedFileList(scriptInfo: ScriptInfo): string[]; /** * Returns true if emit was conducted From b63185097889376b4c02094b863af9cf752f34cc Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Wed, 10 Jul 2019 15:21:24 -0700 Subject: [PATCH 20/97] Add option disableSourceOfProjectReferenceRedirect to disable using sources of project reference redirect from editor --- src/compiler/commandLineParser.ts | 6 ++ src/compiler/diagnosticMessages.json | 4 + src/compiler/program.ts | 12 +-- src/compiler/types.ts | 3 +- src/server/editorServices.ts | 2 +- src/server/project.ts | 7 +- src/services/services.ts | 4 +- src/services/types.ts | 2 +- .../tsserver/events/projectLoading.ts | 84 ++++++++++++------- .../unittests/tsserver/projectReferences.ts | 30 +++++-- .../reference/api/tsserverlibrary.d.ts | 1 + tests/baselines/reference/api/typescript.d.ts | 1 + .../tsconfig.json | 5 ++ 13 files changed, 110 insertions(+), 51 deletions(-) create mode 100644 tests/baselines/reference/showConfig/Shows tsconfig for single option/disableSourceOfProjectReferenceRedirect/tsconfig.json diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 4747b89f08c..7f67b9c5097 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -751,6 +751,12 @@ namespace ts { category: Diagnostics.Advanced_Options, description: Diagnostics.Disable_size_limitations_on_JavaScript_projects }, + { + name: "disableSourceOfProjectReferenceRedirect", + type: "boolean", + category: Diagnostics.Advanced_Options, + description: Diagnostics.Disable_using_source_of_project_reference_redirect_files + }, { name: "noImplicitUseStrict", type: "boolean", diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 7d178f1d478..edc3903b38e 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -3967,6 +3967,10 @@ "category": "Message", "code": 6220 }, + "Disable using source of project reference redirect files.": { + "category": "Message", + "code": 6221 + }, "Projects to reference": { "category": "Message", diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 2149e175ca2..63f58066174 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -814,7 +814,7 @@ namespace ts { let projectReferenceRedirects: Map | undefined; let mapFromFileToProjectReferenceRedirects: Map | undefined; let mapFromToProjectReferenceRedirectSource: Map | undefined; - const useSourceOfReference = !!host.useSourceInsteadOfReferenceRedirect && host.useSourceInsteadOfReferenceRedirect(); + const useSourceOfProjectReferenceRedirect = !!host.useSourceOfProjectReferenceRedirect && host.useSourceOfProjectReferenceRedirect(); const shouldCreateNewSourceFile = shouldProgramCreateNewSourceFiles(oldProgram, options); const structuralIsReused = tryReuseStructureFromOldProgram(); @@ -836,7 +836,7 @@ namespace ts { for (const parsedRef of resolvedProjectReferences) { if (!parsedRef) continue; const out = parsedRef.commandLine.options.outFile || parsedRef.commandLine.options.out; - if (useSourceOfReference) { + if (useSourceOfProjectReferenceRedirect) { if (out || getEmitModuleKind(parsedRef.commandLine.options) === ModuleKind.None) { for (const fileName of parsedRef.commandLine.fileNames) { processSourceFile(fileName, /*isDefaultLib*/ false, /*ignoreNoDefaultLib*/ false, /*packageId*/ undefined); @@ -1418,7 +1418,7 @@ namespace ts { for (const newSourceFile of newSourceFiles) { const filePath = newSourceFile.path; addFileToFilesByName(newSourceFile, filePath, newSourceFile.resolvedPath); - if (useSourceOfReference) { + if (useSourceOfProjectReferenceRedirect) { const redirectProject = getProjectReferenceRedirectProject(newSourceFile.fileName); if (redirectProject && !(redirectProject.commandLine.options.outFile || redirectProject.commandLine.options.out)) { const redirect = getProjectReferenceOutputName(redirectProject, newSourceFile.fileName); @@ -2252,7 +2252,7 @@ namespace ts { // Get source file from normalized fileName function findSourceFile(fileName: string, path: Path, isDefaultLib: boolean, ignoreNoDefaultLib: boolean, refFile: SourceFile, refPos: number, refEnd: number, packageId: PackageId | undefined): SourceFile | undefined { - if (useSourceOfReference) { + if (useSourceOfProjectReferenceRedirect) { const source = getSourceOfProjectReferenceRedirect(fileName); if (source) { const file = isString(source) ? @@ -2309,7 +2309,7 @@ namespace ts { } let redirectedPath: Path | undefined; - if (refFile && !useSourceOfReference) { + if (refFile && !useSourceOfProjectReferenceRedirect) { const redirectProject = getProjectReferenceRedirectProject(fileName); if (redirectProject) { if (redirectProject.commandLine.options.outFile || redirectProject.commandLine.options.out) { @@ -2498,7 +2498,7 @@ namespace ts { } function isSourceOfProjectReferenceRedirect(fileName: string) { - return useSourceOfReference && !!getResolvedProjectReferenceToRedirect(fileName); + return useSourceOfProjectReferenceRedirect && !!getResolvedProjectReferenceToRedirect(fileName); } function forEachProjectReference( diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 16ea47beecc..69ac55e6e1b 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -4645,6 +4645,7 @@ namespace ts { /* @internal */ diagnostics?: boolean; /* @internal */ extendedDiagnostics?: boolean; disableSizeLimit?: boolean; + disableSourceOfProjectReferenceRedirect?: boolean; downlevelIteration?: boolean; emitBOM?: boolean; emitDecoratorMetadata?: boolean; @@ -5169,7 +5170,7 @@ namespace ts { createHash?(data: string): string; getParsedCommandLine?(fileName: string): ParsedCommandLine | undefined; /* @internal */ setResolvedProjectReferenceCallbacks?(callbacks: ResolvedProjectReferenceCallbacks): void; - /* @internal */ useSourceInsteadOfReferenceRedirect?(): boolean; + /* @internal */ useSourceOfProjectReferenceRedirect?(): boolean; // TODO: later handle this in better way in builder host instead once the api for tsbuild finalizes and doesn't use compilerHost as base /*@internal*/createDirectory?(directory: string): void; diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index 6fea0bc4171..ab325d5ed33 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -2583,7 +2583,7 @@ namespace ts.server { if (!configFileName) return undefined; const configuredProject = this.findConfiguredProjectByProjectName(configFileName) || - this.createAndLoadConfiguredProject(configFileName, `Creating project for original file: ${originalFileInfo.fileName}${location !== originalLocation ? " for location " + location.fileName : ""}`); + this.createAndLoadConfiguredProject(configFileName, `Creating project for original file: ${originalFileInfo.fileName}${location !== originalLocation ? " for location: " + location.fileName : ""}`); if (configuredProject === project) return originalLocation; updateProjectIfDirty(configuredProject); // Keep this configured project as referenced from project diff --git a/src/server/project.ts b/src/server/project.ts index 2811b04c413..87ced54de8e 100644 --- a/src/server/project.ts +++ b/src/server/project.ts @@ -1540,11 +1540,12 @@ namespace ts.server { } /* @internal */ - useSourceInsteadOfReferenceRedirect = () => !!this.languageServiceEnabled; + useSourceOfProjectReferenceRedirect = () => !!this.languageServiceEnabled && + !this.getCompilerOptions().disableSourceOfProjectReferenceRedirect; fileExists(file: string): boolean { // Project references go to source file instead of .d.ts file - if (this.useSourceInsteadOfReferenceRedirect() && this.projectReferenceCallbacks) { + if (this.useSourceOfProjectReferenceRedirect() && this.projectReferenceCallbacks) { const source = this.projectReferenceCallbacks.getSourceOfProjectReferenceRedirect(file); if (source) return isString(source) ? super.fileExists(source) : true; } @@ -1553,7 +1554,7 @@ namespace ts.server { directoryExists(path: string): boolean { if (super.directoryExists(path)) return true; - if (!this.useSourceInsteadOfReferenceRedirect() || !this.projectReferenceCallbacks) return false; + if (!this.useSourceOfProjectReferenceRedirect() || !this.projectReferenceCallbacks) return false; if (!this.mapOfDeclarationDirectories) { this.mapOfDeclarationDirectories = createMap(); diff --git a/src/services/services.ts b/src/services/services.ts index 2f9ba6eec94..c4af3c94702 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1248,8 +1248,8 @@ namespace ts { if (host.setResolvedProjectReferenceCallbacks) { compilerHost.setResolvedProjectReferenceCallbacks = callbacks => host.setResolvedProjectReferenceCallbacks!(callbacks); } - if (host.useSourceInsteadOfReferenceRedirect) { - compilerHost.useSourceInsteadOfReferenceRedirect = () => host.useSourceInsteadOfReferenceRedirect!(); + if (host.useSourceOfProjectReferenceRedirect) { + compilerHost.useSourceOfProjectReferenceRedirect = () => host.useSourceOfProjectReferenceRedirect!(); } const documentRegistryBucketKey = documentRegistry.getKeyForCompilationSettings(newSettings); diff --git a/src/services/types.ts b/src/services/types.ts index b4fc25e587f..ec71813e1dc 100644 --- a/src/services/types.ts +++ b/src/services/types.ts @@ -239,7 +239,7 @@ namespace ts { /* @internal */ setResolvedProjectReferenceCallbacks?(callbacks: ResolvedProjectReferenceCallbacks): void; /* @internal */ - useSourceInsteadOfReferenceRedirect?(): boolean; + useSourceOfProjectReferenceRedirect?(): boolean; } /* @internal */ diff --git a/src/testRunner/unittests/tsserver/events/projectLoading.ts b/src/testRunner/unittests/tsserver/events/projectLoading.ts index 53fe28240f8..3cc25232bc2 100644 --- a/src/testRunner/unittests/tsserver/events/projectLoading.ts +++ b/src/testRunner/unittests/tsserver/events/projectLoading.ts @@ -73,44 +73,64 @@ namespace ts.projectSystem { verifyEvent(project, `Change in config file detected`); }); - it("when opening original location project", () => { - const aDTs: File = { - path: `${projectRoot}/a/a.d.ts`, - content: `export declare class A { + describe("when opening original location project", () => { + it("with project references", () => { + verify(); + }); + + it("when disableSourceOfProjectReferenceRedirect is true", () => { + verify(/*disableSourceOfProjectReferenceRedirect*/ true); + }); + + function verify(disableSourceOfProjectReferenceRedirect?: true) { + const aDTs: File = { + path: `${projectRoot}/a/a.d.ts`, + content: `export declare class A { } //# sourceMappingURL=a.d.ts.map ` - }; - const aDTsMap: File = { - path: `${projectRoot}/a/a.d.ts.map`, - content: `{"version":3,"file":"a.d.ts","sourceRoot":"","sources":["./a.ts"],"names":[],"mappings":"AAAA,qBAAa,CAAC;CAAI"}` - }; - const bTs: File = { - path: bTsPath, - content: `import {A} from "../a/a"; new A();` - }; - const configB: File = { - path: configBPath, - content: JSON.stringify({ - references: [{ path: "../a" }] - }) - }; + }; + const aDTsMap: File = { + path: `${projectRoot}/a/a.d.ts.map`, + content: `{"version":3,"file":"a.d.ts","sourceRoot":"","sources":["./a.ts"],"names":[],"mappings":"AAAA,qBAAa,CAAC;CAAI"}` + }; + const bTs: File = { + path: bTsPath, + content: `import {A} from "../a/a"; new A();` + }; + const configB: File = { + path: configBPath, + content: JSON.stringify({ + ...(disableSourceOfProjectReferenceRedirect && { + compilerOptions: { + disableSourceOfProjectReferenceRedirect + } + }), + references: [{ path: "../a" }] + }) + }; - const { service, session, verifyEventWithOpenTs, verifyEvent } = createSessionToVerifyEvent(files.concat(aDTs, aDTsMap, bTs, configB)); - verifyEventWithOpenTs(bTs, configB.path, 1); + const { service, session, verifyEventWithOpenTs, verifyEvent } = createSessionToVerifyEvent(files.concat(aDTs, aDTsMap, bTs, configB)); + verifyEventWithOpenTs(bTs, configB.path, 1); - session.executeCommandSeq({ - command: protocol.CommandTypes.References, - arguments: { - file: bTs.path, - ...protocolLocationFromSubstring(bTs.content, "A()") - } - }); + session.executeCommandSeq({ + command: protocol.CommandTypes.References, + arguments: { + file: bTs.path, + ...protocolLocationFromSubstring(bTs.content, "A()") + } + }); - checkNumberOfProjects(service, { configuredProjects: 2 }); - const project = service.configuredProjects.get(configA.path)!; - assert.isDefined(project); - verifyEvent(project, `Creating project for original file: ${aTs.path}`); + checkNumberOfProjects(service, { configuredProjects: 2 }); + const project = service.configuredProjects.get(configA.path)!; + assert.isDefined(project); + verifyEvent( + project, + disableSourceOfProjectReferenceRedirect ? + `Creating project for original file: ${aTs.path} for location: ${aDTs.path}` : + `Creating project for original file: ${aTs.path}` + ); + } }); describe("with external projects and config files ", () => { diff --git a/src/testRunner/unittests/tsserver/projectReferences.ts b/src/testRunner/unittests/tsserver/projectReferences.ts index f9445dc6d19..fe1ff10aac1 100644 --- a/src/testRunner/unittests/tsserver/projectReferences.ts +++ b/src/testRunner/unittests/tsserver/projectReferences.ts @@ -417,6 +417,7 @@ fn5(); interface VerifierAndWithRefs { withRefs: boolean; + disableSourceOfProjectReferenceRedirect?: true; verifier: (withRefs: boolean) => readonly DocumentPositionMapperVerifier[]; } @@ -426,7 +427,7 @@ fn5(); interface OpenTsFile extends VerifierAndWithRefs { onHostCreate?: (host: TestServerHost) => void; } - function openTsFile({ withRefs, verifier, onHostCreate }: OpenTsFile) { + function openTsFile({ withRefs, disableSourceOfProjectReferenceRedirect, verifier, onHostCreate }: OpenTsFile) { const host = createHost(files, [mainConfig.path]); if (!withRefs) { // Erase project reference @@ -434,11 +435,22 @@ fn5(); compilerOptions: { composite: true, declarationMap: true } })); } + else if (disableSourceOfProjectReferenceRedirect) { + // Erase project reference + host.writeFile(mainConfig.path, JSON.stringify({ + compilerOptions: { + composite: true, + declarationMap: true, + disableSourceOfProjectReferenceRedirect: !!disableSourceOfProjectReferenceRedirect + }, + references: [{ path: "../dependency" }] + })); + } if (onHostCreate) { onHostCreate(host); } const session = createSession(host); - const verifiers = verifier(withRefs); + const verifiers = verifier(withRefs && !disableSourceOfProjectReferenceRedirect); openFilesForSession([...openFiles(verifiers), randomFile], session); return { host, session, verifiers }; } @@ -786,9 +798,9 @@ fn5(); }); } - function verifyScenarioWorker({ mainScenario, verifier }: VerifyScenario, withRefs: boolean) { + function verifyScenarioWorker({ mainScenario, verifier }: VerifyScenario, withRefs: boolean, disableSourceOfProjectReferenceRedirect?: true) { it(mainScenario, () => { - const { host, session, verifiers } = openTsFile({ withRefs, verifier }); + const { host, session, verifiers } = openTsFile({ withRefs, disableSourceOfProjectReferenceRedirect, verifier }); checkProject(session, verifiers); verifyScenarioAndScriptInfoCollection(session, host, verifiers, "main"); }); @@ -798,6 +810,7 @@ fn5(); scenarioName: "when usage file changes, document position mapper doesnt change", verifier, withRefs, + disableSourceOfProjectReferenceRedirect, change: (_host, session, verifiers) => verifiers.forEach( verifier => session.executeCommandSeq({ command: protocol.CommandTypes.Change, @@ -819,6 +832,7 @@ fn5(); scenarioName: "when dependency .d.ts changes, document position mapper doesnt change", verifier, withRefs, + disableSourceOfProjectReferenceRedirect, change: host => host.writeFile( dtsLocation, host.readFile(dtsLocation)!.replace( @@ -835,6 +849,7 @@ fn5(); scenarioName: "when dependency file's map changes", verifier, withRefs, + disableSourceOfProjectReferenceRedirect, change: host => host.writeFile( dtsMapLocation, `{"version":3,"file":"FnS.d.ts","sourceRoot":"","sources":["../dependency/FnS.ts"],"names":[],"mappings":"AAAA,wBAAgB,GAAG,SAAM;AACzB,wBAAgB,GAAG,SAAM;AACzB,wBAAgB,GAAG,SAAM;AACzB,wBAAgB,GAAG,SAAM;AACzB,wBAAgB,GAAG,SAAM;AACzB,eAAO,MAAM,CAAC,KAAK,CAAC"}` @@ -846,6 +861,7 @@ fn5(); scenarioName: "with depedency files map file", verifier, withRefs, + disableSourceOfProjectReferenceRedirect, fileLocation: dtsMapLocation, fileNotPresentKey: "noMap", fileCreatedKey: "mapFileCreated", @@ -856,6 +872,7 @@ fn5(); scenarioName: "with depedency .d.ts file", verifier, withRefs, + disableSourceOfProjectReferenceRedirect, fileLocation: dtsLocation, fileNotPresentKey: "noDts", fileCreatedKey: "dtsFileCreated", @@ -863,7 +880,7 @@ fn5(); noDts: true }); - if (withRefs) { + if (withRefs && !disableSourceOfProjectReferenceRedirect) { verifyScenarioWithChanges({ scenarioName: "when defining project source changes", verifier, @@ -907,6 +924,9 @@ ${dependencyTs.content}`); describe("when main tsconfig has project reference", () => { verifyScenarioWorker(scenario, /*withRefs*/ true); }); + describe("when main tsconfig has but has disableSourceOfProjectReferenceRedirect", () => { + verifyScenarioWorker(scenario, /*withRefs*/ true); + }); } describe("from project that uses dependency", () => { diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 0b728ac95d8..09931908eda 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -2513,6 +2513,7 @@ declare namespace ts { emitDeclarationOnly?: boolean; declarationDir?: string; disableSizeLimit?: boolean; + disableSourceOfProjectReferenceRedirect?: boolean; downlevelIteration?: boolean; emitBOM?: boolean; emitDecoratorMetadata?: boolean; diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index be8eea8062b..34f6fd36dad 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -2513,6 +2513,7 @@ declare namespace ts { emitDeclarationOnly?: boolean; declarationDir?: string; disableSizeLimit?: boolean; + disableSourceOfProjectReferenceRedirect?: boolean; downlevelIteration?: boolean; emitBOM?: boolean; emitDecoratorMetadata?: boolean; diff --git a/tests/baselines/reference/showConfig/Shows tsconfig for single option/disableSourceOfProjectReferenceRedirect/tsconfig.json b/tests/baselines/reference/showConfig/Shows tsconfig for single option/disableSourceOfProjectReferenceRedirect/tsconfig.json new file mode 100644 index 00000000000..c8b95e0909d --- /dev/null +++ b/tests/baselines/reference/showConfig/Shows tsconfig for single option/disableSourceOfProjectReferenceRedirect/tsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "disableSourceOfProjectReferenceRedirect": true + } +} From e89acb6358845b310753e132f5ae692c72d5bb6a Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Wed, 31 Jul 2019 16:30:54 -0700 Subject: [PATCH 21/97] Reflect effects of assertion calls in control flow analysis --- src/compiler/binder.ts | 22 +++++++++++++ src/compiler/checker.ts | 71 +++++++++++++++++++++++++++++++++++++++++ src/compiler/types.ts | 19 +++++++---- 3 files changed, 106 insertions(+), 6 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index d18ab45a347..16dffe46aad 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -706,6 +706,9 @@ namespace ts { case SyntaxKind.CaseClause: bindCaseClause(node); break; + case SyntaxKind.ExpressionStatement: + bindExpressionStatement(node); + break; case SyntaxKind.LabeledStatement: bindLabeledStatement(node); break; @@ -896,6 +899,11 @@ namespace ts { return flowNodeCreated({ flags: FlowFlags.Assignment, antecedent, node }); } + function createFlowCall(antecedent: FlowNode, node: CallExpression): FlowNode { + setFlowNodeReferenced(antecedent); + return flowNodeCreated({ flags: FlowFlags.Call, antecedent, node }); + } + function createFlowArrayMutation(antecedent: FlowNode, node: CallExpression | BinaryExpression): FlowNode { setFlowNodeReferenced(antecedent); const res: FlowArrayMutation = flowNodeCreated({ flags: FlowFlags.ArrayMutation, antecedent, node }); @@ -1276,6 +1284,20 @@ namespace ts { activeLabels!.pop(); } + function isDottedName(node: Expression) { + return node.kind === SyntaxKind.Identifier || node.kind === SyntaxKind.PropertyAccessExpression && isQualifiedName((node).expression); + } + + function bindExpressionStatement(node: ExpressionStatement): void { + bind(node.expression); + if (node.expression.kind === SyntaxKind.CallExpression) { + const call = node.expression; + if (isDottedName(call.expression) && call.arguments.length >= 1) { + currentFlow = createFlowCall(currentFlow, call); + } + } + } + function bindLabeledStatement(node: LabeledStatement): void { const preStatementLabel = createLoopLabel(); const postStatementLabel = createBranchLabel(); diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 3fa9c2da66e..2ed815e8978 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -16864,6 +16864,44 @@ namespace ts { return false; } + function getTypeOfDottedName(node: Expression) { + if (node.kind === SyntaxKind.Identifier) { + const symbol = getResolvedSymbol(node); + const nonAliasSymbol = symbol.flags & SymbolFlags.Alias ? resolveAlias(symbol) : symbol; + return nonAliasSymbol.flags & (SymbolFlags.Function | SymbolFlags.Class | SymbolFlags.ValueModule) ? getTypeOfSymbol(nonAliasSymbol) : undefined; + } + if (node.kind === SyntaxKind.PropertyAccessExpression) { + const type = getTypeOfDottedName((node).expression); + if (type) { + const prop = getPropertyOfType(type, (node).name.escapedText); + return prop && prop.flags & (SymbolFlags.Function | SymbolFlags.Method | SymbolFlags.Class | SymbolFlags.ValueModule) ? getTypeOfSymbol(prop) : undefined; + } + } + } + + function getIsAssertCall(node: CallExpression) { + const type = getTypeOfDottedName(node.expression); + if (type) { + const signature = getSingleCallSignature(type); + if (signature && signature.declaration) { + const typeNode = getEffectiveReturnTypeNode(signature.declaration); + if (typeNode && typeNode.kind === SyntaxKind.UnionType) { + const types = (typeNode).types; + return types.length === 2 && types[0].kind === SyntaxKind.VoidKeyword && types[1].kind === SyntaxKind.NeverKeyword; + } + } + } + return false; + } + + function isAssertCall(node: CallExpression) { + const links = getNodeLinks(node); + if (links.isAssertCall === undefined) { + links.isAssertCall = getIsAssertCall(node); + } + return links.isAssertCall; + } + function reportFlowControlError(node: Node) { const block = findAncestor(node, isFunctionOrModuleBlock); const sourceFile = getSourceFileOfNode(node); @@ -16962,6 +17000,13 @@ namespace ts { } } } + else if (flags & FlowFlags.Call) { + type = getTypeAtFlowCall(flow); + if (!type) { + flow = (flow).antecedent; + continue; + } + } else if (flags & FlowFlags.Condition) { type = getTypeAtFlowCondition(flow); } @@ -17057,6 +17102,32 @@ namespace ts { return undefined; } + function narrowTypeByAssertion(type: Type, expr: Expression): Type { + const node = skipParentheses(expr); + if (node.kind === SyntaxKind.BinaryExpression) { + if ((node).operatorToken.kind === SyntaxKind.AmpersandAmpersandToken) { + return narrowTypeByAssertion(narrowTypeByAssertion(type, (node).left), (node).right); + } + if ((node).operatorToken.kind === SyntaxKind.BarBarToken) { + return getUnionType([narrowTypeByAssertion(type, (node).left), narrowTypeByAssertion(type, (node).right)]); + } + } + return narrowType(type, node, /*assumeTrue*/ true); + } + + function getTypeAtFlowCall(flow: FlowCall): FlowType | undefined { + if (isAssertCall(flow.node)) { + const flowType = getTypeAtFlowNode(flow.antecedent); + const type = getTypeFromFlowType(flowType); + const narrowedType = narrowTypeByAssertion(type, flow.node.arguments[0]); + if (narrowedType === type) { + return flowType; + } + return createFlowType(narrowedType, isIncomplete(flowType)); + } + return undefined; + } + function getTypeAtFlowArrayMutation(flow: FlowArrayMutation): FlowType | undefined { if (declaredType === autoType || declaredType === autoArrayType) { const node = flow.node; diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 099f7705f84..aec65b03367 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2550,12 +2550,13 @@ namespace ts { FalseCondition = 1 << 6, // Condition known to be false SwitchClause = 1 << 7, // Switch statement clause ArrayMutation = 1 << 8, // Potential array mutation - Referenced = 1 << 9, // Referenced as antecedent once - Shared = 1 << 10, // Referenced as antecedent more than once - PreFinally = 1 << 11, // Injected edge that links pre-finally label and pre-try flow - AfterFinally = 1 << 12, // Injected edge that links post-finally flow with the rest of the graph + Call = 1 << 9, // Potential assertion call + Referenced = 1 << 10, // Referenced as antecedent once + Shared = 1 << 11, // Referenced as antecedent more than once + PreFinally = 1 << 12, // Injected edge that links pre-finally label and pre-try flow + AfterFinally = 1 << 13, // Injected edge that links post-finally flow with the rest of the graph /** @internal */ - Cached = 1 << 13, // Indicates that at least one cross-call cache entry exists for this node, even if not a loop participant + Cached = 1 << 14, // Indicates that at least one cross-call cache entry exists for this node, even if not a loop participant Label = BranchLabel | LoopLabel, Condition = TrueCondition | FalseCondition } @@ -2574,7 +2575,7 @@ namespace ts { } export type FlowNode = - | AfterFinallyFlow | PreFinallyFlow | FlowStart | FlowLabel | FlowAssignment | FlowCondition | FlowSwitchClause | FlowArrayMutation; + | AfterFinallyFlow | PreFinallyFlow | FlowStart | FlowLabel | FlowAssignment | FlowCall | FlowCondition | FlowSwitchClause | FlowArrayMutation; export interface FlowNodeBase { flags: FlowFlags; id?: number; // Node id used by flow type cache in checker @@ -2599,6 +2600,11 @@ namespace ts { antecedent: FlowNode; } + export interface FlowCall extends FlowNodeBase { + node: CallExpression; + antecedent: FlowNode; + } + // FlowCondition represents a condition that is known to be true or false at the // node's location in the control flow. export interface FlowCondition extends FlowNodeBase { @@ -3902,6 +3908,7 @@ namespace ts { resolvedSymbol?: Symbol; // Cached name resolution result resolvedIndexInfo?: IndexInfo; // Cached indexing info resolution result maybeTypePredicate?: boolean; // Cached check whether call expression might reference a type predicate + isAssertCall?: boolean; enumMemberValue?: string | number; // Constant value of enum member isVisible?: boolean; // Is this node visible containsArgumentsReference?: boolean; // Whether a function-like declaration contains an 'arguments' reference From 77f2a412e15a3f3ac2a01d648a64e75670d47204 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Fri, 2 Aug 2019 17:57:26 -0700 Subject: [PATCH 22/97] Support 'asserts' type predicates in control flow analysis --- src/compiler/binder.ts | 6 +- src/compiler/checker.ts | 230 +++++++++++++++++++--------------------- src/compiler/emitter.ts | 14 ++- src/compiler/factory.ts | 7 +- src/compiler/parser.ts | 13 +++ src/compiler/scanner.ts | 1 + src/compiler/types.ts | 36 ++++--- src/compiler/visitor.ts | 1 + 8 files changed, 166 insertions(+), 142 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 16dffe46aad..2b917bc577c 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -1284,12 +1284,14 @@ namespace ts { activeLabels!.pop(); } - function isDottedName(node: Expression) { - return node.kind === SyntaxKind.Identifier || node.kind === SyntaxKind.PropertyAccessExpression && isQualifiedName((node).expression); + function isDottedName(node: Expression): boolean { + return node.kind === SyntaxKind.Identifier || node.kind === SyntaxKind.PropertyAccessExpression && isDottedName((node).expression); } function bindExpressionStatement(node: ExpressionStatement): void { bind(node.expression); + // A top level call expression with a dotted function name and at least one argument + // is potentially an assertion and is therefore included in the control flow. if (node.expression.kind === SyntaxKind.CallExpression) { const call = node.expression; if (isDottedName(call.expression) && call.arguments.length >= 1) { diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 2ed815e8978..c8ad1f66064 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -522,7 +522,7 @@ namespace ts { markerSubType.constraint = markerSuperType; const markerOtherType = createTypeParameter(); - const noTypePredicate = createIdentifierTypePredicate("<>", 0, anyType); + const noTypePredicate = createTypePredicate(TypePredicateKind.Identifier, "<>", 0, anyType); const anySignature = createSignature(undefined, undefined, undefined, emptyArray, anyType, /*resolvedTypePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false); const unknownSignature = createSignature(undefined, undefined, undefined, emptyArray, errorType, /*resolvedTypePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false); @@ -4212,11 +4212,12 @@ namespace ts { let returnTypeNode: TypeNode | undefined; const typePredicate = getTypePredicateOfSignature(signature); if (typePredicate) { - const parameterName = typePredicate.kind === TypePredicateKind.Identifier ? + const assertsModifier = typePredicate.kind === TypePredicateKind.Assertion ? createToken(SyntaxKind.AssertsKeyword) : undefined; + const parameterName = typePredicate.kind !== TypePredicateKind.This ? setEmitFlags(createIdentifier(typePredicate.parameterName), EmitFlags.NoAsciiEscaping) : createThisTypeNode(); - const typeNode = typeToTypeNodeHelper(typePredicate.type, context); - returnTypeNode = createTypePredicateNode(parameterName, typeNode); + const typeNode = typePredicate.type && typeToTypeNodeHelper(typePredicate.type, context); + returnTypeNode = createTypePredicateNode(assertsModifier, parameterName, typeNode); } else { const returnType = getReturnTypeOfSignature(signature); @@ -4685,8 +4686,9 @@ namespace ts { function typePredicateToStringWorker(writer: EmitTextWriter) { const predicate = createTypePredicateNode( - typePredicate.kind === TypePredicateKind.Identifier ? createIdentifier(typePredicate.parameterName) : createThisTypeNode(), - nodeBuilder.typeToTypeNode(typePredicate.type, enclosingDeclaration, toNodeBuilderFlags(flags) | NodeBuilderFlags.IgnoreErrors | NodeBuilderFlags.WriteTypeParametersInQualifiedName)!, // TODO: GH#18217 + typePredicate.kind === TypePredicateKind.Assertion ? createToken(SyntaxKind.AssertsKeyword) : undefined, + typePredicate.kind !== TypePredicateKind.This ? createIdentifier(typePredicate.parameterName) : createThisTypeNode(), + typePredicate.type && nodeBuilder.typeToTypeNode(typePredicate.type, enclosingDeclaration, toNodeBuilderFlags(flags) | NodeBuilderFlags.IgnoreErrors | NodeBuilderFlags.WriteTypeParametersInQualifiedName)! // TODO: GH#18217 ); const printer = createPrinter({ removeComments: true }); const sourceFile = enclosingDeclaration && getSourceFileOfNode(enclosingDeclaration); @@ -8432,12 +8434,8 @@ namespace ts { return isBracketed || !!typeExpression && typeExpression.type.kind === SyntaxKind.JSDocOptionalType; } - function createIdentifierTypePredicate(parameterName: string, parameterIndex: number, type: Type): IdentifierTypePredicate { - return { kind: TypePredicateKind.Identifier, parameterName, parameterIndex, type }; - } - - function createThisTypePredicate(type: Type): ThisTypePredicate { - return { kind: TypePredicateKind.This, type }; + function createTypePredicate(kind: TypePredicateKind, parameterName: string | undefined, parameterIndex: number | undefined, type: Type | undefined): TypePredicate { + return { kind, parameterName, parameterIndex, type } as TypePredicate; } /** @@ -8672,9 +8670,15 @@ namespace ts { } } - function signatureHasTypePredicate(signature: Signature): boolean { - return getTypePredicateOfSignature(signature) !== undefined; - } + // function hasAssertionTypePredicate(signature: Signature): boolean { + // const predicate = getTypePredicateOfSignature(signature); + // return !!predicate && predicate.kind === TypePredicateKind.Assertion; + // } + + // function hasBooleanTypePredicate(signature: Signature): boolean { + // const predicate = getTypePredicateOfSignature(signature); + // return !!predicate && (predicate.kind === TypePredicateKind.This || predicate.kind === TypePredicateKind.Identifier); + // } function getTypePredicateOfSignature(signature: Signature): TypePredicate | undefined { if (!signature.resolvedTypePredicate) { @@ -8703,18 +8707,13 @@ namespace ts { return signature.resolvedTypePredicate === noTypePredicate ? undefined : signature.resolvedTypePredicate; } - function createTypePredicateFromTypePredicateNode(node: TypePredicateNode, signature: Signature): IdentifierTypePredicate | ThisTypePredicate { - const { parameterName } = node; - const type = getTypeFromTypeNode(node.type); - if (parameterName.kind === SyntaxKind.Identifier) { - return createIdentifierTypePredicate( - parameterName.escapedText as string, - findIndex(signature.parameters, p => p.escapedName === parameterName.escapedText), - type); - } - else { - return createThisTypePredicate(type); - } + function createTypePredicateFromTypePredicateNode(node: TypePredicateNode, signature: Signature): TypePredicate { + const parameterName = node.parameterName; + const type = node.type && getTypeFromTypeNode(node.type); + return parameterName.kind === SyntaxKind.ThisType ? + createTypePredicate(TypePredicateKind.This, /*parameterName*/ undefined, /*parameterIndex*/ undefined, type) : + createTypePredicate(node.assertsModifier ? TypePredicateKind.Assertion : TypePredicateKind.Identifier, parameterName.escapedText as string, + findIndex(signature.parameters, p => p.escapedName === parameterName.escapedText), type); } function getReturnTypeOfSignature(signature: Signature): Type { @@ -9820,7 +9819,7 @@ namespace ts { const types: Type[] = []; for (const sig of signatures) { const pred = getTypePredicateOfSignature(sig); - if (!pred) { + if (!pred || pred.kind === TypePredicateKind.Assertion) { continue; } @@ -9840,15 +9839,11 @@ namespace ts { return undefined; } const unionType = getUnionType(types); - return isIdentifierTypePredicate(first) - ? createIdentifierTypePredicate(first.parameterName, first.parameterIndex, unionType) - : createThisTypePredicate(unionType); + return createTypePredicate(first.kind, first.parameterName, first.parameterIndex, unionType); } function typePredicateKindsMatch(a: TypePredicate, b: TypePredicate): boolean { - return isIdentifierTypePredicate(a) - ? isIdentifierTypePredicate(b) && a.parameterIndex === b.parameterIndex - : !isIdentifierTypePredicate(b); + return a.kind === b.kind && a.parameterIndex === b.parameterIndex; } // This function assumes the constituent type list is sorted and deduplicated. @@ -11114,7 +11109,7 @@ namespace ts { case SyntaxKind.TypeReference: return getTypeFromTypeReference(node); case SyntaxKind.TypePredicate: - return booleanType; + return (node).assertsModifier ? voidType : booleanType; case SyntaxKind.ExpressionWithTypeArguments: return getTypeFromTypeReference(node); case SyntaxKind.TypeQuery: @@ -11272,21 +11267,8 @@ namespace ts { return result; } - function instantiateTypePredicate(predicate: TypePredicate, mapper: TypeMapper): ThisTypePredicate | IdentifierTypePredicate { - if (isIdentifierTypePredicate(predicate)) { - return { - kind: TypePredicateKind.Identifier, - parameterName: predicate.parameterName, - parameterIndex: predicate.parameterIndex, - type: instantiateType(predicate.type, mapper) - }; - } - else { - return { - kind: TypePredicateKind.This, - type: instantiateType(predicate.type, mapper) - }; - } + function instantiateTypePredicate(predicate: TypePredicate, mapper: TypeMapper): TypePredicate { + return createTypePredicate(predicate.kind, predicate.parameterName, predicate.parameterIndex, instantiateType(predicate.type, mapper)); } function instantiateSignature(signature: Signature, mapper: TypeMapper, eraseTypeParameters?: boolean): Signature { @@ -12338,7 +12320,7 @@ namespace ts { // with respect to T. const sourceSig = callbackCheck ? undefined : getSingleCallSignature(getNonNullableType(sourceType)); const targetSig = callbackCheck ? undefined : getSingleCallSignature(getNonNullableType(targetType)); - const callbacks = sourceSig && targetSig && !signatureHasTypePredicate(sourceSig) && !signatureHasTypePredicate(targetSig) && + const callbacks = sourceSig && targetSig && !getTypePredicateOfSignature(sourceSig) && !getTypePredicateOfSignature(targetSig) && (getFalsyFlags(sourceType) & TypeFlags.Nullable) === (getFalsyFlags(targetType) & TypeFlags.Nullable); const related = callbacks ? // TODO: GH#18217 It will work if they're both `undefined`, but not if only one is @@ -12407,7 +12389,7 @@ namespace ts { return Ternary.False; } - if (source.kind === TypePredicateKind.Identifier) { + if (source.kind !== TypePredicateKind.This) { if (source.parameterIndex !== (target as IdentifierTypePredicate).parameterIndex) { if (reportErrors) { errorReporter!(Diagnostics.Parameter_0_is_not_in_the_same_position_as_parameter_1, source.parameterName, (target as IdentifierTypePredicate).parameterName); @@ -12417,7 +12399,9 @@ namespace ts { } } - const related = compareTypes(source.type, target.type, reportErrors); + const related = source.type === target.type ? Ternary.True : + source.type && target.type ? compareTypes(source.type, target.type, reportErrors) : + Ternary.False; if (related === Ternary.False && reportErrors) { errorReporter!(Diagnostics.Type_predicate_0_is_not_assignable_to_1, typePredicateToString(source), typePredicateToString(target)); } @@ -14602,16 +14586,18 @@ namespace ts { if (!ignoreReturnTypes) { const sourceTypePredicate = getTypePredicateOfSignature(source); const targetTypePredicate = getTypePredicateOfSignature(target); - result &= sourceTypePredicate !== undefined || targetTypePredicate !== undefined - ? compareTypePredicatesIdentical(sourceTypePredicate, targetTypePredicate, compareTypes) - // If they're both type predicates their return types will both be `boolean`, so no need to compare those. - : compareTypes(getReturnTypeOfSignature(source), getReturnTypeOfSignature(target)); + result &= sourceTypePredicate || targetTypePredicate ? + compareTypePredicatesIdentical(sourceTypePredicate, targetTypePredicate, compareTypes) : + compareTypes(getReturnTypeOfSignature(source), getReturnTypeOfSignature(target)); } return result; } function compareTypePredicatesIdentical(source: TypePredicate | undefined, target: TypePredicate | undefined, compareTypes: (s: Type, t: Type) => Ternary): Ternary { - return source === undefined || target === undefined || !typePredicateKindsMatch(source, target) ? Ternary.False : compareTypes(source.type, target.type); + return !(source && target && typePredicateKindsMatch(source, target)) ? Ternary.False : + source.type === target.type ? Ternary.True : + source.type && target.type ? compareTypes(source.type, target.type) : + Ternary.False; } function literalTypesWithSameBaseType(types: Type[]): boolean { @@ -15219,8 +15205,7 @@ namespace ts { function applyToReturnTypes(source: Signature, target: Signature, callback: (s: Type, t: Type) => void) { const sourceTypePredicate = getTypePredicateOfSignature(source); const targetTypePredicate = getTypePredicateOfSignature(target); - if (sourceTypePredicate && targetTypePredicate && sourceTypePredicate.kind === targetTypePredicate.kind && - (sourceTypePredicate.kind === TypePredicateKind.This || sourceTypePredicate.parameterIndex === (targetTypePredicate).parameterIndex)) { + if (sourceTypePredicate && targetTypePredicate && typePredicateKindsMatch(sourceTypePredicate, targetTypePredicate) && sourceTypePredicate.type && targetTypePredicate.type) { callback(sourceTypePredicate.type, targetTypePredicate.type); } else { @@ -16845,61 +16830,62 @@ namespace ts { return isLengthPushOrUnshift || isElementAssignment; } - function maybeTypePredicateCall(node: CallExpression) { - const links = getNodeLinks(node); - if (links.maybeTypePredicate === undefined) { - links.maybeTypePredicate = getMaybeTypePredicate(node); - } - return links.maybeTypePredicate; + function isDeclarationWithExplicitTypeAnnotation(declaration: Declaration | undefined) { + return !!(declaration && ( + declaration.kind === SyntaxKind.VariableDeclaration || declaration.kind === SyntaxKind.Parameter || + declaration.kind === SyntaxKind.PropertyDeclaration || declaration.kind === SyntaxKind.PropertySignature) && + (declaration as VariableDeclaration | ParameterDeclaration | PropertyDeclaration | PropertySignature).type); } - function getMaybeTypePredicate(node: CallExpression) { - if (node.expression.kind !== SyntaxKind.SuperKeyword) { - const funcType = checkNonNullExpression(node.expression); - if (funcType !== silentNeverType) { - const apparentType = getApparentType(funcType); - return apparentType !== errorType && some(getSignaturesOfType(apparentType, SignatureKind.Call), signatureHasTypePredicate); - } - } - return false; + function getExplicitTypeOfSymbol(symbol: Symbol) { + return symbol.flags & (SymbolFlags.Function | SymbolFlags.Method | SymbolFlags.Class | SymbolFlags.ValueModule) || + symbol.flags & (SymbolFlags.Variable | SymbolFlags.Property) && isDeclarationWithExplicitTypeAnnotation(symbol.valueDeclaration) ? + getTypeOfSymbol(symbol) : undefined; } function getTypeOfDottedName(node: Expression) { + // We require the dotted function name in an assertion expression to be comprised of identifiers + // that reference function, method, class or value module symbols; or variable, property or + // parameter symbols with declarations that have explicit type annotations. Such references are + // resolvable with no possibility of triggering circularities in control flow analysis. if (node.kind === SyntaxKind.Identifier) { const symbol = getResolvedSymbol(node); - const nonAliasSymbol = symbol.flags & SymbolFlags.Alias ? resolveAlias(symbol) : symbol; - return nonAliasSymbol.flags & (SymbolFlags.Function | SymbolFlags.Class | SymbolFlags.ValueModule) ? getTypeOfSymbol(nonAliasSymbol) : undefined; + return getExplicitTypeOfSymbol(symbol.flags & SymbolFlags.Alias ? resolveAlias(symbol) : symbol); } if (node.kind === SyntaxKind.PropertyAccessExpression) { const type = getTypeOfDottedName((node).expression); if (type) { const prop = getPropertyOfType(type, (node).name.escapedText); - return prop && prop.flags & (SymbolFlags.Function | SymbolFlags.Method | SymbolFlags.Class | SymbolFlags.ValueModule) ? getTypeOfSymbol(prop) : undefined; + return prop && getExplicitTypeOfSymbol(prop); } } } - function getIsAssertCall(node: CallExpression) { - const type = getTypeOfDottedName(node.expression); - if (type) { - const signature = getSingleCallSignature(type); - if (signature && signature.declaration) { - const typeNode = getEffectiveReturnTypeNode(signature.declaration); - if (typeNode && typeNode.kind === SyntaxKind.UnionType) { - const types = (typeNode).types; - return types.length === 2 && types[0].kind === SyntaxKind.VoidKeyword && types[1].kind === SyntaxKind.NeverKeyword; - } - } - } - return false; - } - - function isAssertCall(node: CallExpression) { + function getTypePredicateForCall(node: CallExpression) { const links = getNodeLinks(node); - if (links.isAssertCall === undefined) { - links.isAssertCall = getIsAssertCall(node); + if (links.resolvedTypePredicate === undefined) { + links.resolvedTypePredicate = computeTypePredicateForCall(node) || noTypePredicate; } - return links.isAssertCall; + return links.resolvedTypePredicate === noTypePredicate ? undefined : links.resolvedTypePredicate; + } + + function computeTypePredicateForCall(node: CallExpression) { + // A call expression parented by an expression statement is a potential assertion. Other call + // expressions are potential type predicate function calls. + const funcType = node.parent.kind === SyntaxKind.ExpressionStatement ? getTypeOfDottedName(node.expression) : + node.expression.kind !== SyntaxKind.SuperKeyword ? checkNonNullExpression(node.expression) : + undefined; + if (funcType && funcType !== silentNeverType) { + const apparentType = getApparentType(funcType); + if (some(getSignaturesOfType(apparentType, SignatureKind.Call), hasTypePredicate)) { + return getTypePredicateOfSignature(getResolvedSignature(node)); + } + } + return undefined; + } + + function hasTypePredicate(signature: Signature) { + return !!getTypePredicateOfSignature(signature); } function reportFlowControlError(node: Node) { @@ -17116,14 +17102,14 @@ namespace ts { } function getTypeAtFlowCall(flow: FlowCall): FlowType | undefined { - if (isAssertCall(flow.node)) { + const predicate = getTypePredicateForCall(flow.node); + if (predicate && predicate.kind === TypePredicateKind.Assertion) { const flowType = getTypeAtFlowNode(flow.antecedent); const type = getTypeFromFlowType(flowType); - const narrowedType = narrowTypeByAssertion(type, flow.node.arguments[0]); - if (narrowedType === type) { - return flowType; - } - return createFlowType(narrowedType, isIncomplete(flowType)); + const narrowedType = predicate.type ? + narrowTypeByTypePredicate(type, predicate, flow.node, /*assumeTrue*/ true) : + narrowTypeByAssertion(type, flow.node.arguments[predicate.parameterIndex]); + return narrowedType === type ? flowType : createFlowType(narrowedType, isIncomplete(flowType)); } return undefined; } @@ -17711,24 +17697,24 @@ namespace ts { getIntersectionType([type, candidate]); } - function narrowTypeByTypePredicate(type: Type, callExpression: CallExpression, assumeTrue: boolean): Type { - if (!hasMatchingArgument(callExpression, reference) || !maybeTypePredicateCall(callExpression)) { - return type; - } - const signature = getResolvedSignature(callExpression); - const predicate = getTypePredicateOfSignature(signature); - if (!predicate) { - return type; + function narrowTypeByCallExpression(type: Type, callExpression: CallExpression, assumeTrue: boolean): Type { + if (hasMatchingArgument(callExpression, reference)) { + const predicate = getTypePredicateForCall(callExpression); + if (predicate && predicate.kind !== TypePredicateKind.Assertion) { + return narrowTypeByTypePredicate(type, predicate, callExpression, assumeTrue); + } } + return type; + } + function narrowTypeByTypePredicate(type: Type, predicate: TypePredicate, callExpression: CallExpression, assumeTrue: boolean): Type { // Don't narrow from 'any' if the predicate type is exactly 'Object' or 'Function' if (isTypeAny(type) && (predicate.type === globalObjectType || predicate.type === globalFunctionType)) { return type; } - - if (isIdentifierTypePredicate(predicate)) { + if (predicate.kind !== TypePredicateKind.This) { const predicateArgument = callExpression.arguments[predicate.parameterIndex]; - if (predicateArgument) { + if (predicateArgument && predicate.type) { if (isMatchingReference(reference, predicateArgument)) { return getNarrowedType(type, predicate.type, assumeTrue, isTypeSubtypeOf); } @@ -17763,7 +17749,7 @@ namespace ts { case SyntaxKind.ElementAccessExpression: return narrowTypeByTruthiness(type, expr, assumeTrue); case SyntaxKind.CallExpression: - return narrowTypeByTypePredicate(type, expr, assumeTrue); + return narrowTypeByCallExpression(type, expr, assumeTrue); case SyntaxKind.ParenthesizedExpression: return narrowType(type, (expr).expression, assumeTrue); case SyntaxKind.BinaryExpression: @@ -25485,12 +25471,14 @@ namespace ts { error(parameterName, Diagnostics.A_type_predicate_cannot_reference_a_rest_parameter); } else { - const leadingError = () => chainDiagnosticMessages(/*details*/ undefined, Diagnostics.A_type_predicate_s_type_must_be_assignable_to_its_parameter_s_type); - checkTypeAssignableTo(typePredicate.type, - getTypeOfSymbol(signature.parameters[typePredicate.parameterIndex]), - node.type, - /*headMessage*/ undefined, - leadingError); + if (typePredicate.type) { + const leadingError = () => chainDiagnosticMessages(/*details*/ undefined, Diagnostics.A_type_predicate_s_type_must_be_assignable_to_its_parameter_s_type); + checkTypeAssignableTo(typePredicate.type, + getTypeOfSymbol(signature.parameters[typePredicate.parameterIndex]), + node.type, + /*headMessage*/ undefined, + leadingError); + } } } else if (parameterName) { diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 8681a452e2a..415f8f9680c 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -1906,11 +1906,17 @@ namespace ts { // function emitTypePredicate(node: TypePredicateNode) { + if (node.assertsModifier) { + emit(node.assertsModifier); + writeSpace(); + } emit(node.parameterName); - writeSpace(); - writeKeyword("is"); - writeSpace(); - emit(node.type); + if (node.type) { + writeSpace(); + writeKeyword("is"); + writeSpace(); + emit(node.type); + } } function emitTypeReference(node: TypeReferenceNode) { diff --git a/src/compiler/factory.ts b/src/compiler/factory.ts index 1f0294423a5..166999a7059 100644 --- a/src/compiler/factory.ts +++ b/src/compiler/factory.ts @@ -667,17 +667,18 @@ namespace ts { return createSynthesizedNode(kind); } - export function createTypePredicateNode(parameterName: Identifier | ThisTypeNode | string, type: TypeNode) { + export function createTypePredicateNode(assertsModifier: AssertsToken | undefined, parameterName: Identifier | ThisTypeNode | string, type: TypeNode | undefined) { const node = createSynthesizedNode(SyntaxKind.TypePredicate) as TypePredicateNode; + node.assertsModifier = assertsModifier; node.parameterName = asName(parameterName); node.type = type; return node; } - export function updateTypePredicateNode(node: TypePredicateNode, parameterName: Identifier | ThisTypeNode, type: TypeNode) { + export function updateTypePredicateNode(node: TypePredicateNode, assertsModifier: AssertsToken | undefined, parameterName: Identifier | ThisTypeNode, type: TypeNode | undefined) { return node.parameterName !== parameterName || node.type !== type - ? updateNode(createTypePredicateNode(parameterName, type), node) + ? updateNode(createTypePredicateNode(assertsModifier, parameterName, type), node) : node; } diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 254be93000b..3e0ea65476a 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -2992,6 +2992,8 @@ namespace ts { return parseParenthesizedType(); case SyntaxKind.ImportKeyword: return parseImportType(); + case SyntaxKind.AssertsKeyword: + return lookAhead(nextTokenIsIdentifierOrKeywordOnSameLine) ? parseAssertsTypePredicate() : parseTypeReference(); default: return parseTypeReference(); } @@ -3032,6 +3034,7 @@ namespace ts { case SyntaxKind.DotDotDotToken: case SyntaxKind.InferKeyword: case SyntaxKind.ImportKeyword: + case SyntaxKind.AssertsKeyword: return true; case SyntaxKind.FunctionKeyword: return !inStartOfParameter; @@ -3225,6 +3228,16 @@ namespace ts { } } + function parseAssertsTypePredicate(): TypeNode { + const node = createNode(SyntaxKind.TypePredicate); + node.assertsModifier = parseExpectedToken(SyntaxKind.AssertsKeyword); + node.parameterName = parseIdentifier(); + if (parseOptional(SyntaxKind.IsKeyword)) { + node.type = parseType(); + } + return finishNode(node); + } + function parseType(): TypeNode { // The rules about 'yield' only apply to actual code/expression contexts. They don't // apply to 'type' contexts. So we disable these parameters here before moving on. diff --git a/src/compiler/scanner.ts b/src/compiler/scanner.ts index f949893171a..31e54e283fe 100644 --- a/src/compiler/scanner.ts +++ b/src/compiler/scanner.ts @@ -65,6 +65,7 @@ namespace ts { abstract: SyntaxKind.AbstractKeyword, any: SyntaxKind.AnyKeyword, as: SyntaxKind.AsKeyword, + asserts: SyntaxKind.AssertsKeyword, bigint: SyntaxKind.BigIntKeyword, boolean: SyntaxKind.BooleanKeyword, break: SyntaxKind.BreakKeyword, diff --git a/src/compiler/types.ts b/src/compiler/types.ts index aec65b03367..ba1ad79a8c2 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -32,6 +32,7 @@ namespace ts { | SyntaxKind.AbstractKeyword | SyntaxKind.AnyKeyword | SyntaxKind.AsKeyword + | SyntaxKind.AssertsKeyword | SyntaxKind.BigIntKeyword | SyntaxKind.BooleanKeyword | SyntaxKind.BreakKeyword @@ -250,6 +251,7 @@ namespace ts { // Contextual keywords AbstractKeyword, AsKeyword, + AssertsKeyword, AnyKeyword, AsyncKeyword, AwaitKeyword, @@ -734,6 +736,7 @@ namespace ts { export type AwaitKeywordToken = Token; export type PlusToken = Token; export type MinusToken = Token; + export type AssertsToken = Token; export type Modifier = Token @@ -1177,8 +1180,9 @@ namespace ts { export interface TypePredicateNode extends TypeNode { kind: SyntaxKind.TypePredicate; parent: SignatureDeclaration | JSDocTypeExpression; + assertsModifier?: AssertsToken; parameterName: Identifier | ThisTypeNode; - type: TypeNode; + type?: TypeNode; } export interface TypeQueryNode extends TypeNode { @@ -3493,25 +3497,32 @@ namespace ts { export const enum TypePredicateKind { This, - Identifier + Identifier, + Assertion } - export interface TypePredicateBase { - kind: TypePredicateKind; + export interface ThisTypePredicate { + kind: TypePredicateKind.This; + parameterName: undefined; + parameterIndex: undefined; type: Type; } - export interface ThisTypePredicate extends TypePredicateBase { - kind: TypePredicateKind.This; - } - - export interface IdentifierTypePredicate extends TypePredicateBase { + export interface IdentifierTypePredicate { kind: TypePredicateKind.Identifier; parameterName: string; parameterIndex: number; + type: Type; } - export type TypePredicate = IdentifierTypePredicate | ThisTypePredicate; + export interface AssertionTypePredicate { + kind: TypePredicateKind.Assertion; + parameterName: string; + parameterIndex: number; + type: Type | undefined; + } + + export type TypePredicate = ThisTypePredicate | IdentifierTypePredicate | AssertionTypePredicate; /* @internal */ export type AnyImportSyntax = ImportDeclaration | ImportEqualsDeclaration; @@ -3907,8 +3918,9 @@ namespace ts { resolvedSignature?: Signature; // Cached signature of signature node or call expression resolvedSymbol?: Symbol; // Cached name resolution result resolvedIndexInfo?: IndexInfo; // Cached indexing info resolution result - maybeTypePredicate?: boolean; // Cached check whether call expression might reference a type predicate - isAssertCall?: boolean; + //maybeTypePredicate?: boolean; // Cached check whether call expression might reference a type predicate + //isAssertCall?: boolean; + resolvedTypePredicate?: TypePredicate; // Cached type predicate for call expression enumMemberValue?: string | number; // Constant value of enum member isVisible?: boolean; // Is this node visible containsArgumentsReference?: boolean; // Whether a function-like declaration contains an 'arguments' reference diff --git a/src/compiler/visitor.ts b/src/compiler/visitor.ts index 53ccd81f7a6..aa497511f78 100644 --- a/src/compiler/visitor.ts +++ b/src/compiler/visitor.ts @@ -340,6 +340,7 @@ namespace ts { case SyntaxKind.TypePredicate: return updateTypePredicateNode(node, + visitNode((node).assertsModifier, visitor), visitNode((node).parameterName, visitor), visitNode((node).type, visitor, isTypeNode)); From 1f5bb970d987a7387a3531e1cea00f51b08fe20a Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sat, 3 Aug 2019 08:28:53 -0700 Subject: [PATCH 23/97] Remove unused code --- src/compiler/checker.ts | 10 ---------- src/compiler/types.ts | 2 -- 2 files changed, 12 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index c8ad1f66064..7dfb0cd2757 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -8670,16 +8670,6 @@ namespace ts { } } - // function hasAssertionTypePredicate(signature: Signature): boolean { - // const predicate = getTypePredicateOfSignature(signature); - // return !!predicate && predicate.kind === TypePredicateKind.Assertion; - // } - - // function hasBooleanTypePredicate(signature: Signature): boolean { - // const predicate = getTypePredicateOfSignature(signature); - // return !!predicate && (predicate.kind === TypePredicateKind.This || predicate.kind === TypePredicateKind.Identifier); - // } - function getTypePredicateOfSignature(signature: Signature): TypePredicate | undefined { if (!signature.resolvedTypePredicate) { if (signature.target) { diff --git a/src/compiler/types.ts b/src/compiler/types.ts index ba1ad79a8c2..3cdfba2aafb 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -3918,8 +3918,6 @@ namespace ts { resolvedSignature?: Signature; // Cached signature of signature node or call expression resolvedSymbol?: Symbol; // Cached name resolution result resolvedIndexInfo?: IndexInfo; // Cached indexing info resolution result - //maybeTypePredicate?: boolean; // Cached check whether call expression might reference a type predicate - //isAssertCall?: boolean; resolvedTypePredicate?: TypePredicate; // Cached type predicate for call expression enumMemberValue?: string | number; // Constant value of enum member isVisible?: boolean; // Is this node visible From fe70a62ef1ef87d20a860c6bdea999cdfab76592 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sat, 3 Aug 2019 08:55:55 -0700 Subject: [PATCH 24/97] Accept new API baselines --- .../reference/api/tsserverlibrary.d.ts | 465 +++++++++--------- tests/baselines/reference/api/typescript.d.ts | 465 +++++++++--------- 2 files changed, 480 insertions(+), 450 deletions(-) diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index d73cbab6b5f..00c88fc4963 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -73,7 +73,7 @@ declare namespace ts { end: number; } export type JSDocSyntaxKind = SyntaxKind.EndOfFileToken | SyntaxKind.WhitespaceTrivia | SyntaxKind.AtToken | SyntaxKind.NewLineTrivia | SyntaxKind.AsteriskToken | SyntaxKind.OpenBraceToken | SyntaxKind.CloseBraceToken | SyntaxKind.LessThanToken | SyntaxKind.GreaterThanToken | SyntaxKind.OpenBracketToken | SyntaxKind.CloseBracketToken | SyntaxKind.EqualsToken | SyntaxKind.CommaToken | SyntaxKind.DotToken | SyntaxKind.Identifier | SyntaxKind.BacktickToken | SyntaxKind.Unknown | KeywordSyntaxKind; - export type KeywordSyntaxKind = SyntaxKind.AbstractKeyword | SyntaxKind.AnyKeyword | SyntaxKind.AsKeyword | SyntaxKind.BigIntKeyword | SyntaxKind.BooleanKeyword | SyntaxKind.BreakKeyword | SyntaxKind.CaseKeyword | SyntaxKind.CatchKeyword | SyntaxKind.ClassKeyword | SyntaxKind.ContinueKeyword | SyntaxKind.ConstKeyword | SyntaxKind.ConstructorKeyword | SyntaxKind.DebuggerKeyword | SyntaxKind.DeclareKeyword | SyntaxKind.DefaultKeyword | SyntaxKind.DeleteKeyword | SyntaxKind.DoKeyword | SyntaxKind.ElseKeyword | SyntaxKind.EnumKeyword | SyntaxKind.ExportKeyword | SyntaxKind.ExtendsKeyword | SyntaxKind.FalseKeyword | SyntaxKind.FinallyKeyword | SyntaxKind.ForKeyword | SyntaxKind.FromKeyword | SyntaxKind.FunctionKeyword | SyntaxKind.GetKeyword | SyntaxKind.IfKeyword | SyntaxKind.ImplementsKeyword | SyntaxKind.ImportKeyword | SyntaxKind.InKeyword | SyntaxKind.InferKeyword | SyntaxKind.InstanceOfKeyword | SyntaxKind.InterfaceKeyword | SyntaxKind.IsKeyword | SyntaxKind.KeyOfKeyword | SyntaxKind.LetKeyword | SyntaxKind.ModuleKeyword | SyntaxKind.NamespaceKeyword | SyntaxKind.NeverKeyword | SyntaxKind.NewKeyword | SyntaxKind.NullKeyword | SyntaxKind.NumberKeyword | SyntaxKind.ObjectKeyword | SyntaxKind.PackageKeyword | SyntaxKind.PrivateKeyword | SyntaxKind.ProtectedKeyword | SyntaxKind.PublicKeyword | SyntaxKind.ReadonlyKeyword | SyntaxKind.RequireKeyword | SyntaxKind.GlobalKeyword | SyntaxKind.ReturnKeyword | SyntaxKind.SetKeyword | SyntaxKind.StaticKeyword | SyntaxKind.StringKeyword | SyntaxKind.SuperKeyword | SyntaxKind.SwitchKeyword | SyntaxKind.SymbolKeyword | SyntaxKind.ThisKeyword | SyntaxKind.ThrowKeyword | SyntaxKind.TrueKeyword | SyntaxKind.TryKeyword | SyntaxKind.TypeKeyword | SyntaxKind.TypeOfKeyword | SyntaxKind.UndefinedKeyword | SyntaxKind.UniqueKeyword | SyntaxKind.UnknownKeyword | SyntaxKind.VarKeyword | SyntaxKind.VoidKeyword | SyntaxKind.WhileKeyword | SyntaxKind.WithKeyword | SyntaxKind.YieldKeyword | SyntaxKind.AsyncKeyword | SyntaxKind.AwaitKeyword | SyntaxKind.OfKeyword; + export type KeywordSyntaxKind = SyntaxKind.AbstractKeyword | SyntaxKind.AnyKeyword | SyntaxKind.AsKeyword | SyntaxKind.AssertsKeyword | SyntaxKind.BigIntKeyword | SyntaxKind.BooleanKeyword | SyntaxKind.BreakKeyword | SyntaxKind.CaseKeyword | SyntaxKind.CatchKeyword | SyntaxKind.ClassKeyword | SyntaxKind.ContinueKeyword | SyntaxKind.ConstKeyword | SyntaxKind.ConstructorKeyword | SyntaxKind.DebuggerKeyword | SyntaxKind.DeclareKeyword | SyntaxKind.DefaultKeyword | SyntaxKind.DeleteKeyword | SyntaxKind.DoKeyword | SyntaxKind.ElseKeyword | SyntaxKind.EnumKeyword | SyntaxKind.ExportKeyword | SyntaxKind.ExtendsKeyword | SyntaxKind.FalseKeyword | SyntaxKind.FinallyKeyword | SyntaxKind.ForKeyword | SyntaxKind.FromKeyword | SyntaxKind.FunctionKeyword | SyntaxKind.GetKeyword | SyntaxKind.IfKeyword | SyntaxKind.ImplementsKeyword | SyntaxKind.ImportKeyword | SyntaxKind.InKeyword | SyntaxKind.InferKeyword | SyntaxKind.InstanceOfKeyword | SyntaxKind.InterfaceKeyword | SyntaxKind.IsKeyword | SyntaxKind.KeyOfKeyword | SyntaxKind.LetKeyword | SyntaxKind.ModuleKeyword | SyntaxKind.NamespaceKeyword | SyntaxKind.NeverKeyword | SyntaxKind.NewKeyword | SyntaxKind.NullKeyword | SyntaxKind.NumberKeyword | SyntaxKind.ObjectKeyword | SyntaxKind.PackageKeyword | SyntaxKind.PrivateKeyword | SyntaxKind.ProtectedKeyword | SyntaxKind.PublicKeyword | SyntaxKind.ReadonlyKeyword | SyntaxKind.RequireKeyword | SyntaxKind.GlobalKeyword | SyntaxKind.ReturnKeyword | SyntaxKind.SetKeyword | SyntaxKind.StaticKeyword | SyntaxKind.StringKeyword | SyntaxKind.SuperKeyword | SyntaxKind.SwitchKeyword | SyntaxKind.SymbolKeyword | SyntaxKind.ThisKeyword | SyntaxKind.ThrowKeyword | SyntaxKind.TrueKeyword | SyntaxKind.TryKeyword | SyntaxKind.TypeKeyword | SyntaxKind.TypeOfKeyword | SyntaxKind.UndefinedKeyword | SyntaxKind.UniqueKeyword | SyntaxKind.UnknownKeyword | SyntaxKind.VarKeyword | SyntaxKind.VoidKeyword | SyntaxKind.WhileKeyword | SyntaxKind.WithKeyword | SyntaxKind.YieldKeyword | SyntaxKind.AsyncKeyword | SyntaxKind.AwaitKeyword | SyntaxKind.OfKeyword; export type JsxTokenSyntaxKind = SyntaxKind.LessThanSlashToken | SyntaxKind.EndOfFileToken | SyntaxKind.ConflictMarkerTrivia | SyntaxKind.JsxText | SyntaxKind.JsxTextAllWhiteSpaces | SyntaxKind.OpenBraceToken | SyntaxKind.LessThanToken; export enum SyntaxKind { Unknown = 0, @@ -198,205 +198,206 @@ declare namespace ts { YieldKeyword = 118, AbstractKeyword = 119, AsKeyword = 120, - AnyKeyword = 121, - AsyncKeyword = 122, - AwaitKeyword = 123, - BooleanKeyword = 124, - ConstructorKeyword = 125, - DeclareKeyword = 126, - GetKeyword = 127, - InferKeyword = 128, - IsKeyword = 129, - KeyOfKeyword = 130, - ModuleKeyword = 131, - NamespaceKeyword = 132, - NeverKeyword = 133, - ReadonlyKeyword = 134, - RequireKeyword = 135, - NumberKeyword = 136, - ObjectKeyword = 137, - SetKeyword = 138, - StringKeyword = 139, - SymbolKeyword = 140, - TypeKeyword = 141, - UndefinedKeyword = 142, - UniqueKeyword = 143, - UnknownKeyword = 144, - FromKeyword = 145, - GlobalKeyword = 146, - BigIntKeyword = 147, - OfKeyword = 148, - QualifiedName = 149, - ComputedPropertyName = 150, - TypeParameter = 151, - Parameter = 152, - Decorator = 153, - PropertySignature = 154, - PropertyDeclaration = 155, - MethodSignature = 156, - MethodDeclaration = 157, - Constructor = 158, - GetAccessor = 159, - SetAccessor = 160, - CallSignature = 161, - ConstructSignature = 162, - IndexSignature = 163, - TypePredicate = 164, - TypeReference = 165, - FunctionType = 166, - ConstructorType = 167, - TypeQuery = 168, - TypeLiteral = 169, - ArrayType = 170, - TupleType = 171, - OptionalType = 172, - RestType = 173, - UnionType = 174, - IntersectionType = 175, - ConditionalType = 176, - InferType = 177, - ParenthesizedType = 178, - ThisType = 179, - TypeOperator = 180, - IndexedAccessType = 181, - MappedType = 182, - LiteralType = 183, - ImportType = 184, - ObjectBindingPattern = 185, - ArrayBindingPattern = 186, - BindingElement = 187, - ArrayLiteralExpression = 188, - ObjectLiteralExpression = 189, - PropertyAccessExpression = 190, - ElementAccessExpression = 191, - CallExpression = 192, - NewExpression = 193, - TaggedTemplateExpression = 194, - TypeAssertionExpression = 195, - ParenthesizedExpression = 196, - FunctionExpression = 197, - ArrowFunction = 198, - DeleteExpression = 199, - TypeOfExpression = 200, - VoidExpression = 201, - AwaitExpression = 202, - PrefixUnaryExpression = 203, - PostfixUnaryExpression = 204, - BinaryExpression = 205, - ConditionalExpression = 206, - TemplateExpression = 207, - YieldExpression = 208, - SpreadElement = 209, - ClassExpression = 210, - OmittedExpression = 211, - ExpressionWithTypeArguments = 212, - AsExpression = 213, - NonNullExpression = 214, - MetaProperty = 215, - SyntheticExpression = 216, - TemplateSpan = 217, - SemicolonClassElement = 218, - Block = 219, - VariableStatement = 220, - EmptyStatement = 221, - ExpressionStatement = 222, - IfStatement = 223, - DoStatement = 224, - WhileStatement = 225, - ForStatement = 226, - ForInStatement = 227, - ForOfStatement = 228, - ContinueStatement = 229, - BreakStatement = 230, - ReturnStatement = 231, - WithStatement = 232, - SwitchStatement = 233, - LabeledStatement = 234, - ThrowStatement = 235, - TryStatement = 236, - DebuggerStatement = 237, - VariableDeclaration = 238, - VariableDeclarationList = 239, - FunctionDeclaration = 240, - ClassDeclaration = 241, - InterfaceDeclaration = 242, - TypeAliasDeclaration = 243, - EnumDeclaration = 244, - ModuleDeclaration = 245, - ModuleBlock = 246, - CaseBlock = 247, - NamespaceExportDeclaration = 248, - ImportEqualsDeclaration = 249, - ImportDeclaration = 250, - ImportClause = 251, - NamespaceImport = 252, - NamedImports = 253, - ImportSpecifier = 254, - ExportAssignment = 255, - ExportDeclaration = 256, - NamedExports = 257, - ExportSpecifier = 258, - MissingDeclaration = 259, - ExternalModuleReference = 260, - JsxElement = 261, - JsxSelfClosingElement = 262, - JsxOpeningElement = 263, - JsxClosingElement = 264, - JsxFragment = 265, - JsxOpeningFragment = 266, - JsxClosingFragment = 267, - JsxAttribute = 268, - JsxAttributes = 269, - JsxSpreadAttribute = 270, - JsxExpression = 271, - CaseClause = 272, - DefaultClause = 273, - HeritageClause = 274, - CatchClause = 275, - PropertyAssignment = 276, - ShorthandPropertyAssignment = 277, - SpreadAssignment = 278, - EnumMember = 279, - UnparsedPrologue = 280, - UnparsedPrepend = 281, - UnparsedText = 282, - UnparsedInternalText = 283, - UnparsedSyntheticReference = 284, - SourceFile = 285, - Bundle = 286, - UnparsedSource = 287, - InputFiles = 288, - JSDocTypeExpression = 289, - JSDocAllType = 290, - JSDocUnknownType = 291, - JSDocNullableType = 292, - JSDocNonNullableType = 293, - JSDocOptionalType = 294, - JSDocFunctionType = 295, - JSDocVariadicType = 296, - JSDocComment = 297, - JSDocTypeLiteral = 298, - JSDocSignature = 299, - JSDocTag = 300, - JSDocAugmentsTag = 301, - JSDocAuthorTag = 302, - JSDocClassTag = 303, - JSDocCallbackTag = 304, - JSDocEnumTag = 305, - JSDocParameterTag = 306, - JSDocReturnTag = 307, - JSDocThisTag = 308, - JSDocTypeTag = 309, - JSDocTemplateTag = 310, - JSDocTypedefTag = 311, - JSDocPropertyTag = 312, - SyntaxList = 313, - NotEmittedStatement = 314, - PartiallyEmittedExpression = 315, - CommaListExpression = 316, - MergeDeclarationMarker = 317, - EndOfDeclarationMarker = 318, - Count = 319, + AssertsKeyword = 121, + AnyKeyword = 122, + AsyncKeyword = 123, + AwaitKeyword = 124, + BooleanKeyword = 125, + ConstructorKeyword = 126, + DeclareKeyword = 127, + GetKeyword = 128, + InferKeyword = 129, + IsKeyword = 130, + KeyOfKeyword = 131, + ModuleKeyword = 132, + NamespaceKeyword = 133, + NeverKeyword = 134, + ReadonlyKeyword = 135, + RequireKeyword = 136, + NumberKeyword = 137, + ObjectKeyword = 138, + SetKeyword = 139, + StringKeyword = 140, + SymbolKeyword = 141, + TypeKeyword = 142, + UndefinedKeyword = 143, + UniqueKeyword = 144, + UnknownKeyword = 145, + FromKeyword = 146, + GlobalKeyword = 147, + BigIntKeyword = 148, + OfKeyword = 149, + QualifiedName = 150, + ComputedPropertyName = 151, + TypeParameter = 152, + Parameter = 153, + Decorator = 154, + PropertySignature = 155, + PropertyDeclaration = 156, + MethodSignature = 157, + MethodDeclaration = 158, + Constructor = 159, + GetAccessor = 160, + SetAccessor = 161, + CallSignature = 162, + ConstructSignature = 163, + IndexSignature = 164, + TypePredicate = 165, + TypeReference = 166, + FunctionType = 167, + ConstructorType = 168, + TypeQuery = 169, + TypeLiteral = 170, + ArrayType = 171, + TupleType = 172, + OptionalType = 173, + RestType = 174, + UnionType = 175, + IntersectionType = 176, + ConditionalType = 177, + InferType = 178, + ParenthesizedType = 179, + ThisType = 180, + TypeOperator = 181, + IndexedAccessType = 182, + MappedType = 183, + LiteralType = 184, + ImportType = 185, + ObjectBindingPattern = 186, + ArrayBindingPattern = 187, + BindingElement = 188, + ArrayLiteralExpression = 189, + ObjectLiteralExpression = 190, + PropertyAccessExpression = 191, + ElementAccessExpression = 192, + CallExpression = 193, + NewExpression = 194, + TaggedTemplateExpression = 195, + TypeAssertionExpression = 196, + ParenthesizedExpression = 197, + FunctionExpression = 198, + ArrowFunction = 199, + DeleteExpression = 200, + TypeOfExpression = 201, + VoidExpression = 202, + AwaitExpression = 203, + PrefixUnaryExpression = 204, + PostfixUnaryExpression = 205, + BinaryExpression = 206, + ConditionalExpression = 207, + TemplateExpression = 208, + YieldExpression = 209, + SpreadElement = 210, + ClassExpression = 211, + OmittedExpression = 212, + ExpressionWithTypeArguments = 213, + AsExpression = 214, + NonNullExpression = 215, + MetaProperty = 216, + SyntheticExpression = 217, + TemplateSpan = 218, + SemicolonClassElement = 219, + Block = 220, + VariableStatement = 221, + EmptyStatement = 222, + ExpressionStatement = 223, + IfStatement = 224, + DoStatement = 225, + WhileStatement = 226, + ForStatement = 227, + ForInStatement = 228, + ForOfStatement = 229, + ContinueStatement = 230, + BreakStatement = 231, + ReturnStatement = 232, + WithStatement = 233, + SwitchStatement = 234, + LabeledStatement = 235, + ThrowStatement = 236, + TryStatement = 237, + DebuggerStatement = 238, + VariableDeclaration = 239, + VariableDeclarationList = 240, + FunctionDeclaration = 241, + ClassDeclaration = 242, + InterfaceDeclaration = 243, + TypeAliasDeclaration = 244, + EnumDeclaration = 245, + ModuleDeclaration = 246, + ModuleBlock = 247, + CaseBlock = 248, + NamespaceExportDeclaration = 249, + ImportEqualsDeclaration = 250, + ImportDeclaration = 251, + ImportClause = 252, + NamespaceImport = 253, + NamedImports = 254, + ImportSpecifier = 255, + ExportAssignment = 256, + ExportDeclaration = 257, + NamedExports = 258, + ExportSpecifier = 259, + MissingDeclaration = 260, + ExternalModuleReference = 261, + JsxElement = 262, + JsxSelfClosingElement = 263, + JsxOpeningElement = 264, + JsxClosingElement = 265, + JsxFragment = 266, + JsxOpeningFragment = 267, + JsxClosingFragment = 268, + JsxAttribute = 269, + JsxAttributes = 270, + JsxSpreadAttribute = 271, + JsxExpression = 272, + CaseClause = 273, + DefaultClause = 274, + HeritageClause = 275, + CatchClause = 276, + PropertyAssignment = 277, + ShorthandPropertyAssignment = 278, + SpreadAssignment = 279, + EnumMember = 280, + UnparsedPrologue = 281, + UnparsedPrepend = 282, + UnparsedText = 283, + UnparsedInternalText = 284, + UnparsedSyntheticReference = 285, + SourceFile = 286, + Bundle = 287, + UnparsedSource = 288, + InputFiles = 289, + JSDocTypeExpression = 290, + JSDocAllType = 291, + JSDocUnknownType = 292, + JSDocNullableType = 293, + JSDocNonNullableType = 294, + JSDocOptionalType = 295, + JSDocFunctionType = 296, + JSDocVariadicType = 297, + JSDocComment = 298, + JSDocTypeLiteral = 299, + JSDocSignature = 300, + JSDocTag = 301, + JSDocAugmentsTag = 302, + JSDocAuthorTag = 303, + JSDocClassTag = 304, + JSDocCallbackTag = 305, + JSDocEnumTag = 306, + JSDocParameterTag = 307, + JSDocReturnTag = 308, + JSDocThisTag = 309, + JSDocTypeTag = 310, + JSDocTemplateTag = 311, + JSDocTypedefTag = 312, + JSDocPropertyTag = 313, + SyntaxList = 314, + NotEmittedStatement = 315, + PartiallyEmittedExpression = 316, + CommaListExpression = 317, + MergeDeclarationMarker = 318, + EndOfDeclarationMarker = 319, + Count = 320, FirstAssignment = 60, LastAssignment = 72, FirstCompoundAssignment = 61, @@ -404,15 +405,15 @@ declare namespace ts { FirstReservedWord = 74, LastReservedWord = 109, FirstKeyword = 74, - LastKeyword = 148, + LastKeyword = 149, FirstFutureReservedWord = 110, LastFutureReservedWord = 118, - FirstTypeNode = 164, - LastTypeNode = 184, + FirstTypeNode = 165, + LastTypeNode = 185, FirstPunctuation = 18, LastPunctuation = 72, FirstToken = 0, - LastToken = 148, + LastToken = 149, FirstTriviaToken = 2, LastTriviaToken = 7, FirstLiteralToken = 8, @@ -421,11 +422,11 @@ declare namespace ts { LastTemplateToken = 17, FirstBinaryOperator = 28, LastBinaryOperator = 72, - FirstNode = 149, - FirstJSDocNode = 289, - LastJSDocNode = 312, - FirstJSDocTagNode = 300, - LastJSDocTagNode = 312, + FirstNode = 150, + FirstJSDocNode = 290, + LastJSDocNode = 313, + FirstJSDocTagNode = 301, + LastJSDocTagNode = 313, } export enum NodeFlags { None = 0, @@ -516,6 +517,7 @@ declare namespace ts { export type AwaitKeywordToken = Token; export type PlusToken = Token; export type MinusToken = Token; + export type AssertsToken = Token; export type Modifier = Token | Token | Token | Token | Token | Token | Token | Token | Token | Token | Token; export type ModifiersArray = NodeArray; export interface Identifier extends PrimaryExpression, Declaration { @@ -769,8 +771,9 @@ declare namespace ts { export interface TypePredicateNode extends TypeNode { kind: SyntaxKind.TypePredicate; parent: SignatureDeclaration | JSDocTypeExpression; + assertsModifier?: AssertsToken; parameterName: Identifier | ThisTypeNode; - type: TypeNode; + type?: TypeNode; } export interface TypeQueryNode extends TypeNode { kind: SyntaxKind.TypeQuery; @@ -1661,10 +1664,11 @@ declare namespace ts { FalseCondition = 64, SwitchClause = 128, ArrayMutation = 256, - Referenced = 512, - Shared = 1024, - PreFinally = 2048, - AfterFinally = 4096, + Call = 512, + Referenced = 1024, + Shared = 2048, + PreFinally = 4096, + AfterFinally = 8192, Label = 12, Condition = 96 } @@ -1678,7 +1682,7 @@ declare namespace ts { antecedent: FlowNode; lock: FlowLock; } - export type FlowNode = AfterFinallyFlow | PreFinallyFlow | FlowStart | FlowLabel | FlowAssignment | FlowCondition | FlowSwitchClause | FlowArrayMutation; + export type FlowNode = AfterFinallyFlow | PreFinallyFlow | FlowStart | FlowLabel | FlowAssignment | FlowCall | FlowCondition | FlowSwitchClause | FlowArrayMutation; export interface FlowNodeBase { flags: FlowFlags; id?: number; @@ -1693,6 +1697,10 @@ declare namespace ts { node: Expression | VariableDeclaration | BindingElement; antecedent: FlowNode; } + export interface FlowCall extends FlowNodeBase { + node: CallExpression; + antecedent: FlowNode; + } export interface FlowCondition extends FlowNodeBase { expression: Expression; antecedent: FlowNode; @@ -2080,21 +2088,28 @@ declare namespace ts { } export enum TypePredicateKind { This = 0, - Identifier = 1 + Identifier = 1, + Assertion = 2 } - export interface TypePredicateBase { - kind: TypePredicateKind; + export interface ThisTypePredicate { + kind: TypePredicateKind.This; + parameterName: undefined; + parameterIndex: undefined; type: Type; } - export interface ThisTypePredicate extends TypePredicateBase { - kind: TypePredicateKind.This; - } - export interface IdentifierTypePredicate extends TypePredicateBase { + export interface IdentifierTypePredicate { kind: TypePredicateKind.Identifier; parameterName: string; parameterIndex: number; + type: Type; } - export type TypePredicate = IdentifierTypePredicate | ThisTypePredicate; + export interface AssertionTypePredicate { + kind: TypePredicateKind.Assertion; + parameterName: string; + parameterIndex: number; + type: Type | undefined; + } + export type TypePredicate = ThisTypePredicate | IdentifierTypePredicate | AssertionTypePredicate; export enum SymbolFlags { None = 0, FunctionScopedVariable = 1, @@ -3823,8 +3838,8 @@ declare namespace ts { function createIndexSignature(decorators: ReadonlyArray | undefined, modifiers: ReadonlyArray | undefined, parameters: ReadonlyArray, type: TypeNode): IndexSignatureDeclaration; function updateIndexSignature(node: IndexSignatureDeclaration, decorators: ReadonlyArray | undefined, modifiers: ReadonlyArray | undefined, parameters: ReadonlyArray, type: TypeNode): IndexSignatureDeclaration; function createKeywordTypeNode(kind: KeywordTypeNode["kind"]): KeywordTypeNode; - function createTypePredicateNode(parameterName: Identifier | ThisTypeNode | string, type: TypeNode): TypePredicateNode; - function updateTypePredicateNode(node: TypePredicateNode, parameterName: Identifier | ThisTypeNode, type: TypeNode): TypePredicateNode; + function createTypePredicateNode(assertsModifier: AssertsToken | undefined, parameterName: Identifier | ThisTypeNode | string, type: TypeNode | undefined): TypePredicateNode; + function updateTypePredicateNode(node: TypePredicateNode, assertsModifier: AssertsToken | undefined, parameterName: Identifier | ThisTypeNode, type: TypeNode | undefined): TypePredicateNode; function createTypeReferenceNode(typeName: string | EntityName, typeArguments: ReadonlyArray | undefined): TypeReferenceNode; function updateTypeReferenceNode(node: TypeReferenceNode, typeName: EntityName, typeArguments: NodeArray | undefined): TypeReferenceNode; function createFunctionTypeNode(typeParameters: ReadonlyArray | undefined, parameters: ReadonlyArray, type: TypeNode | undefined): FunctionTypeNode; diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index 1bb693c0845..df8d0e1ee79 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -73,7 +73,7 @@ declare namespace ts { end: number; } export type JSDocSyntaxKind = SyntaxKind.EndOfFileToken | SyntaxKind.WhitespaceTrivia | SyntaxKind.AtToken | SyntaxKind.NewLineTrivia | SyntaxKind.AsteriskToken | SyntaxKind.OpenBraceToken | SyntaxKind.CloseBraceToken | SyntaxKind.LessThanToken | SyntaxKind.GreaterThanToken | SyntaxKind.OpenBracketToken | SyntaxKind.CloseBracketToken | SyntaxKind.EqualsToken | SyntaxKind.CommaToken | SyntaxKind.DotToken | SyntaxKind.Identifier | SyntaxKind.BacktickToken | SyntaxKind.Unknown | KeywordSyntaxKind; - export type KeywordSyntaxKind = SyntaxKind.AbstractKeyword | SyntaxKind.AnyKeyword | SyntaxKind.AsKeyword | SyntaxKind.BigIntKeyword | SyntaxKind.BooleanKeyword | SyntaxKind.BreakKeyword | SyntaxKind.CaseKeyword | SyntaxKind.CatchKeyword | SyntaxKind.ClassKeyword | SyntaxKind.ContinueKeyword | SyntaxKind.ConstKeyword | SyntaxKind.ConstructorKeyword | SyntaxKind.DebuggerKeyword | SyntaxKind.DeclareKeyword | SyntaxKind.DefaultKeyword | SyntaxKind.DeleteKeyword | SyntaxKind.DoKeyword | SyntaxKind.ElseKeyword | SyntaxKind.EnumKeyword | SyntaxKind.ExportKeyword | SyntaxKind.ExtendsKeyword | SyntaxKind.FalseKeyword | SyntaxKind.FinallyKeyword | SyntaxKind.ForKeyword | SyntaxKind.FromKeyword | SyntaxKind.FunctionKeyword | SyntaxKind.GetKeyword | SyntaxKind.IfKeyword | SyntaxKind.ImplementsKeyword | SyntaxKind.ImportKeyword | SyntaxKind.InKeyword | SyntaxKind.InferKeyword | SyntaxKind.InstanceOfKeyword | SyntaxKind.InterfaceKeyword | SyntaxKind.IsKeyword | SyntaxKind.KeyOfKeyword | SyntaxKind.LetKeyword | SyntaxKind.ModuleKeyword | SyntaxKind.NamespaceKeyword | SyntaxKind.NeverKeyword | SyntaxKind.NewKeyword | SyntaxKind.NullKeyword | SyntaxKind.NumberKeyword | SyntaxKind.ObjectKeyword | SyntaxKind.PackageKeyword | SyntaxKind.PrivateKeyword | SyntaxKind.ProtectedKeyword | SyntaxKind.PublicKeyword | SyntaxKind.ReadonlyKeyword | SyntaxKind.RequireKeyword | SyntaxKind.GlobalKeyword | SyntaxKind.ReturnKeyword | SyntaxKind.SetKeyword | SyntaxKind.StaticKeyword | SyntaxKind.StringKeyword | SyntaxKind.SuperKeyword | SyntaxKind.SwitchKeyword | SyntaxKind.SymbolKeyword | SyntaxKind.ThisKeyword | SyntaxKind.ThrowKeyword | SyntaxKind.TrueKeyword | SyntaxKind.TryKeyword | SyntaxKind.TypeKeyword | SyntaxKind.TypeOfKeyword | SyntaxKind.UndefinedKeyword | SyntaxKind.UniqueKeyword | SyntaxKind.UnknownKeyword | SyntaxKind.VarKeyword | SyntaxKind.VoidKeyword | SyntaxKind.WhileKeyword | SyntaxKind.WithKeyword | SyntaxKind.YieldKeyword | SyntaxKind.AsyncKeyword | SyntaxKind.AwaitKeyword | SyntaxKind.OfKeyword; + export type KeywordSyntaxKind = SyntaxKind.AbstractKeyword | SyntaxKind.AnyKeyword | SyntaxKind.AsKeyword | SyntaxKind.AssertsKeyword | SyntaxKind.BigIntKeyword | SyntaxKind.BooleanKeyword | SyntaxKind.BreakKeyword | SyntaxKind.CaseKeyword | SyntaxKind.CatchKeyword | SyntaxKind.ClassKeyword | SyntaxKind.ContinueKeyword | SyntaxKind.ConstKeyword | SyntaxKind.ConstructorKeyword | SyntaxKind.DebuggerKeyword | SyntaxKind.DeclareKeyword | SyntaxKind.DefaultKeyword | SyntaxKind.DeleteKeyword | SyntaxKind.DoKeyword | SyntaxKind.ElseKeyword | SyntaxKind.EnumKeyword | SyntaxKind.ExportKeyword | SyntaxKind.ExtendsKeyword | SyntaxKind.FalseKeyword | SyntaxKind.FinallyKeyword | SyntaxKind.ForKeyword | SyntaxKind.FromKeyword | SyntaxKind.FunctionKeyword | SyntaxKind.GetKeyword | SyntaxKind.IfKeyword | SyntaxKind.ImplementsKeyword | SyntaxKind.ImportKeyword | SyntaxKind.InKeyword | SyntaxKind.InferKeyword | SyntaxKind.InstanceOfKeyword | SyntaxKind.InterfaceKeyword | SyntaxKind.IsKeyword | SyntaxKind.KeyOfKeyword | SyntaxKind.LetKeyword | SyntaxKind.ModuleKeyword | SyntaxKind.NamespaceKeyword | SyntaxKind.NeverKeyword | SyntaxKind.NewKeyword | SyntaxKind.NullKeyword | SyntaxKind.NumberKeyword | SyntaxKind.ObjectKeyword | SyntaxKind.PackageKeyword | SyntaxKind.PrivateKeyword | SyntaxKind.ProtectedKeyword | SyntaxKind.PublicKeyword | SyntaxKind.ReadonlyKeyword | SyntaxKind.RequireKeyword | SyntaxKind.GlobalKeyword | SyntaxKind.ReturnKeyword | SyntaxKind.SetKeyword | SyntaxKind.StaticKeyword | SyntaxKind.StringKeyword | SyntaxKind.SuperKeyword | SyntaxKind.SwitchKeyword | SyntaxKind.SymbolKeyword | SyntaxKind.ThisKeyword | SyntaxKind.ThrowKeyword | SyntaxKind.TrueKeyword | SyntaxKind.TryKeyword | SyntaxKind.TypeKeyword | SyntaxKind.TypeOfKeyword | SyntaxKind.UndefinedKeyword | SyntaxKind.UniqueKeyword | SyntaxKind.UnknownKeyword | SyntaxKind.VarKeyword | SyntaxKind.VoidKeyword | SyntaxKind.WhileKeyword | SyntaxKind.WithKeyword | SyntaxKind.YieldKeyword | SyntaxKind.AsyncKeyword | SyntaxKind.AwaitKeyword | SyntaxKind.OfKeyword; export type JsxTokenSyntaxKind = SyntaxKind.LessThanSlashToken | SyntaxKind.EndOfFileToken | SyntaxKind.ConflictMarkerTrivia | SyntaxKind.JsxText | SyntaxKind.JsxTextAllWhiteSpaces | SyntaxKind.OpenBraceToken | SyntaxKind.LessThanToken; export enum SyntaxKind { Unknown = 0, @@ -198,205 +198,206 @@ declare namespace ts { YieldKeyword = 118, AbstractKeyword = 119, AsKeyword = 120, - AnyKeyword = 121, - AsyncKeyword = 122, - AwaitKeyword = 123, - BooleanKeyword = 124, - ConstructorKeyword = 125, - DeclareKeyword = 126, - GetKeyword = 127, - InferKeyword = 128, - IsKeyword = 129, - KeyOfKeyword = 130, - ModuleKeyword = 131, - NamespaceKeyword = 132, - NeverKeyword = 133, - ReadonlyKeyword = 134, - RequireKeyword = 135, - NumberKeyword = 136, - ObjectKeyword = 137, - SetKeyword = 138, - StringKeyword = 139, - SymbolKeyword = 140, - TypeKeyword = 141, - UndefinedKeyword = 142, - UniqueKeyword = 143, - UnknownKeyword = 144, - FromKeyword = 145, - GlobalKeyword = 146, - BigIntKeyword = 147, - OfKeyword = 148, - QualifiedName = 149, - ComputedPropertyName = 150, - TypeParameter = 151, - Parameter = 152, - Decorator = 153, - PropertySignature = 154, - PropertyDeclaration = 155, - MethodSignature = 156, - MethodDeclaration = 157, - Constructor = 158, - GetAccessor = 159, - SetAccessor = 160, - CallSignature = 161, - ConstructSignature = 162, - IndexSignature = 163, - TypePredicate = 164, - TypeReference = 165, - FunctionType = 166, - ConstructorType = 167, - TypeQuery = 168, - TypeLiteral = 169, - ArrayType = 170, - TupleType = 171, - OptionalType = 172, - RestType = 173, - UnionType = 174, - IntersectionType = 175, - ConditionalType = 176, - InferType = 177, - ParenthesizedType = 178, - ThisType = 179, - TypeOperator = 180, - IndexedAccessType = 181, - MappedType = 182, - LiteralType = 183, - ImportType = 184, - ObjectBindingPattern = 185, - ArrayBindingPattern = 186, - BindingElement = 187, - ArrayLiteralExpression = 188, - ObjectLiteralExpression = 189, - PropertyAccessExpression = 190, - ElementAccessExpression = 191, - CallExpression = 192, - NewExpression = 193, - TaggedTemplateExpression = 194, - TypeAssertionExpression = 195, - ParenthesizedExpression = 196, - FunctionExpression = 197, - ArrowFunction = 198, - DeleteExpression = 199, - TypeOfExpression = 200, - VoidExpression = 201, - AwaitExpression = 202, - PrefixUnaryExpression = 203, - PostfixUnaryExpression = 204, - BinaryExpression = 205, - ConditionalExpression = 206, - TemplateExpression = 207, - YieldExpression = 208, - SpreadElement = 209, - ClassExpression = 210, - OmittedExpression = 211, - ExpressionWithTypeArguments = 212, - AsExpression = 213, - NonNullExpression = 214, - MetaProperty = 215, - SyntheticExpression = 216, - TemplateSpan = 217, - SemicolonClassElement = 218, - Block = 219, - VariableStatement = 220, - EmptyStatement = 221, - ExpressionStatement = 222, - IfStatement = 223, - DoStatement = 224, - WhileStatement = 225, - ForStatement = 226, - ForInStatement = 227, - ForOfStatement = 228, - ContinueStatement = 229, - BreakStatement = 230, - ReturnStatement = 231, - WithStatement = 232, - SwitchStatement = 233, - LabeledStatement = 234, - ThrowStatement = 235, - TryStatement = 236, - DebuggerStatement = 237, - VariableDeclaration = 238, - VariableDeclarationList = 239, - FunctionDeclaration = 240, - ClassDeclaration = 241, - InterfaceDeclaration = 242, - TypeAliasDeclaration = 243, - EnumDeclaration = 244, - ModuleDeclaration = 245, - ModuleBlock = 246, - CaseBlock = 247, - NamespaceExportDeclaration = 248, - ImportEqualsDeclaration = 249, - ImportDeclaration = 250, - ImportClause = 251, - NamespaceImport = 252, - NamedImports = 253, - ImportSpecifier = 254, - ExportAssignment = 255, - ExportDeclaration = 256, - NamedExports = 257, - ExportSpecifier = 258, - MissingDeclaration = 259, - ExternalModuleReference = 260, - JsxElement = 261, - JsxSelfClosingElement = 262, - JsxOpeningElement = 263, - JsxClosingElement = 264, - JsxFragment = 265, - JsxOpeningFragment = 266, - JsxClosingFragment = 267, - JsxAttribute = 268, - JsxAttributes = 269, - JsxSpreadAttribute = 270, - JsxExpression = 271, - CaseClause = 272, - DefaultClause = 273, - HeritageClause = 274, - CatchClause = 275, - PropertyAssignment = 276, - ShorthandPropertyAssignment = 277, - SpreadAssignment = 278, - EnumMember = 279, - UnparsedPrologue = 280, - UnparsedPrepend = 281, - UnparsedText = 282, - UnparsedInternalText = 283, - UnparsedSyntheticReference = 284, - SourceFile = 285, - Bundle = 286, - UnparsedSource = 287, - InputFiles = 288, - JSDocTypeExpression = 289, - JSDocAllType = 290, - JSDocUnknownType = 291, - JSDocNullableType = 292, - JSDocNonNullableType = 293, - JSDocOptionalType = 294, - JSDocFunctionType = 295, - JSDocVariadicType = 296, - JSDocComment = 297, - JSDocTypeLiteral = 298, - JSDocSignature = 299, - JSDocTag = 300, - JSDocAugmentsTag = 301, - JSDocAuthorTag = 302, - JSDocClassTag = 303, - JSDocCallbackTag = 304, - JSDocEnumTag = 305, - JSDocParameterTag = 306, - JSDocReturnTag = 307, - JSDocThisTag = 308, - JSDocTypeTag = 309, - JSDocTemplateTag = 310, - JSDocTypedefTag = 311, - JSDocPropertyTag = 312, - SyntaxList = 313, - NotEmittedStatement = 314, - PartiallyEmittedExpression = 315, - CommaListExpression = 316, - MergeDeclarationMarker = 317, - EndOfDeclarationMarker = 318, - Count = 319, + AssertsKeyword = 121, + AnyKeyword = 122, + AsyncKeyword = 123, + AwaitKeyword = 124, + BooleanKeyword = 125, + ConstructorKeyword = 126, + DeclareKeyword = 127, + GetKeyword = 128, + InferKeyword = 129, + IsKeyword = 130, + KeyOfKeyword = 131, + ModuleKeyword = 132, + NamespaceKeyword = 133, + NeverKeyword = 134, + ReadonlyKeyword = 135, + RequireKeyword = 136, + NumberKeyword = 137, + ObjectKeyword = 138, + SetKeyword = 139, + StringKeyword = 140, + SymbolKeyword = 141, + TypeKeyword = 142, + UndefinedKeyword = 143, + UniqueKeyword = 144, + UnknownKeyword = 145, + FromKeyword = 146, + GlobalKeyword = 147, + BigIntKeyword = 148, + OfKeyword = 149, + QualifiedName = 150, + ComputedPropertyName = 151, + TypeParameter = 152, + Parameter = 153, + Decorator = 154, + PropertySignature = 155, + PropertyDeclaration = 156, + MethodSignature = 157, + MethodDeclaration = 158, + Constructor = 159, + GetAccessor = 160, + SetAccessor = 161, + CallSignature = 162, + ConstructSignature = 163, + IndexSignature = 164, + TypePredicate = 165, + TypeReference = 166, + FunctionType = 167, + ConstructorType = 168, + TypeQuery = 169, + TypeLiteral = 170, + ArrayType = 171, + TupleType = 172, + OptionalType = 173, + RestType = 174, + UnionType = 175, + IntersectionType = 176, + ConditionalType = 177, + InferType = 178, + ParenthesizedType = 179, + ThisType = 180, + TypeOperator = 181, + IndexedAccessType = 182, + MappedType = 183, + LiteralType = 184, + ImportType = 185, + ObjectBindingPattern = 186, + ArrayBindingPattern = 187, + BindingElement = 188, + ArrayLiteralExpression = 189, + ObjectLiteralExpression = 190, + PropertyAccessExpression = 191, + ElementAccessExpression = 192, + CallExpression = 193, + NewExpression = 194, + TaggedTemplateExpression = 195, + TypeAssertionExpression = 196, + ParenthesizedExpression = 197, + FunctionExpression = 198, + ArrowFunction = 199, + DeleteExpression = 200, + TypeOfExpression = 201, + VoidExpression = 202, + AwaitExpression = 203, + PrefixUnaryExpression = 204, + PostfixUnaryExpression = 205, + BinaryExpression = 206, + ConditionalExpression = 207, + TemplateExpression = 208, + YieldExpression = 209, + SpreadElement = 210, + ClassExpression = 211, + OmittedExpression = 212, + ExpressionWithTypeArguments = 213, + AsExpression = 214, + NonNullExpression = 215, + MetaProperty = 216, + SyntheticExpression = 217, + TemplateSpan = 218, + SemicolonClassElement = 219, + Block = 220, + VariableStatement = 221, + EmptyStatement = 222, + ExpressionStatement = 223, + IfStatement = 224, + DoStatement = 225, + WhileStatement = 226, + ForStatement = 227, + ForInStatement = 228, + ForOfStatement = 229, + ContinueStatement = 230, + BreakStatement = 231, + ReturnStatement = 232, + WithStatement = 233, + SwitchStatement = 234, + LabeledStatement = 235, + ThrowStatement = 236, + TryStatement = 237, + DebuggerStatement = 238, + VariableDeclaration = 239, + VariableDeclarationList = 240, + FunctionDeclaration = 241, + ClassDeclaration = 242, + InterfaceDeclaration = 243, + TypeAliasDeclaration = 244, + EnumDeclaration = 245, + ModuleDeclaration = 246, + ModuleBlock = 247, + CaseBlock = 248, + NamespaceExportDeclaration = 249, + ImportEqualsDeclaration = 250, + ImportDeclaration = 251, + ImportClause = 252, + NamespaceImport = 253, + NamedImports = 254, + ImportSpecifier = 255, + ExportAssignment = 256, + ExportDeclaration = 257, + NamedExports = 258, + ExportSpecifier = 259, + MissingDeclaration = 260, + ExternalModuleReference = 261, + JsxElement = 262, + JsxSelfClosingElement = 263, + JsxOpeningElement = 264, + JsxClosingElement = 265, + JsxFragment = 266, + JsxOpeningFragment = 267, + JsxClosingFragment = 268, + JsxAttribute = 269, + JsxAttributes = 270, + JsxSpreadAttribute = 271, + JsxExpression = 272, + CaseClause = 273, + DefaultClause = 274, + HeritageClause = 275, + CatchClause = 276, + PropertyAssignment = 277, + ShorthandPropertyAssignment = 278, + SpreadAssignment = 279, + EnumMember = 280, + UnparsedPrologue = 281, + UnparsedPrepend = 282, + UnparsedText = 283, + UnparsedInternalText = 284, + UnparsedSyntheticReference = 285, + SourceFile = 286, + Bundle = 287, + UnparsedSource = 288, + InputFiles = 289, + JSDocTypeExpression = 290, + JSDocAllType = 291, + JSDocUnknownType = 292, + JSDocNullableType = 293, + JSDocNonNullableType = 294, + JSDocOptionalType = 295, + JSDocFunctionType = 296, + JSDocVariadicType = 297, + JSDocComment = 298, + JSDocTypeLiteral = 299, + JSDocSignature = 300, + JSDocTag = 301, + JSDocAugmentsTag = 302, + JSDocAuthorTag = 303, + JSDocClassTag = 304, + JSDocCallbackTag = 305, + JSDocEnumTag = 306, + JSDocParameterTag = 307, + JSDocReturnTag = 308, + JSDocThisTag = 309, + JSDocTypeTag = 310, + JSDocTemplateTag = 311, + JSDocTypedefTag = 312, + JSDocPropertyTag = 313, + SyntaxList = 314, + NotEmittedStatement = 315, + PartiallyEmittedExpression = 316, + CommaListExpression = 317, + MergeDeclarationMarker = 318, + EndOfDeclarationMarker = 319, + Count = 320, FirstAssignment = 60, LastAssignment = 72, FirstCompoundAssignment = 61, @@ -404,15 +405,15 @@ declare namespace ts { FirstReservedWord = 74, LastReservedWord = 109, FirstKeyword = 74, - LastKeyword = 148, + LastKeyword = 149, FirstFutureReservedWord = 110, LastFutureReservedWord = 118, - FirstTypeNode = 164, - LastTypeNode = 184, + FirstTypeNode = 165, + LastTypeNode = 185, FirstPunctuation = 18, LastPunctuation = 72, FirstToken = 0, - LastToken = 148, + LastToken = 149, FirstTriviaToken = 2, LastTriviaToken = 7, FirstLiteralToken = 8, @@ -421,11 +422,11 @@ declare namespace ts { LastTemplateToken = 17, FirstBinaryOperator = 28, LastBinaryOperator = 72, - FirstNode = 149, - FirstJSDocNode = 289, - LastJSDocNode = 312, - FirstJSDocTagNode = 300, - LastJSDocTagNode = 312, + FirstNode = 150, + FirstJSDocNode = 290, + LastJSDocNode = 313, + FirstJSDocTagNode = 301, + LastJSDocTagNode = 313, } export enum NodeFlags { None = 0, @@ -516,6 +517,7 @@ declare namespace ts { export type AwaitKeywordToken = Token; export type PlusToken = Token; export type MinusToken = Token; + export type AssertsToken = Token; export type Modifier = Token | Token | Token | Token | Token | Token | Token | Token | Token | Token | Token; export type ModifiersArray = NodeArray; export interface Identifier extends PrimaryExpression, Declaration { @@ -769,8 +771,9 @@ declare namespace ts { export interface TypePredicateNode extends TypeNode { kind: SyntaxKind.TypePredicate; parent: SignatureDeclaration | JSDocTypeExpression; + assertsModifier?: AssertsToken; parameterName: Identifier | ThisTypeNode; - type: TypeNode; + type?: TypeNode; } export interface TypeQueryNode extends TypeNode { kind: SyntaxKind.TypeQuery; @@ -1661,10 +1664,11 @@ declare namespace ts { FalseCondition = 64, SwitchClause = 128, ArrayMutation = 256, - Referenced = 512, - Shared = 1024, - PreFinally = 2048, - AfterFinally = 4096, + Call = 512, + Referenced = 1024, + Shared = 2048, + PreFinally = 4096, + AfterFinally = 8192, Label = 12, Condition = 96 } @@ -1678,7 +1682,7 @@ declare namespace ts { antecedent: FlowNode; lock: FlowLock; } - export type FlowNode = AfterFinallyFlow | PreFinallyFlow | FlowStart | FlowLabel | FlowAssignment | FlowCondition | FlowSwitchClause | FlowArrayMutation; + export type FlowNode = AfterFinallyFlow | PreFinallyFlow | FlowStart | FlowLabel | FlowAssignment | FlowCall | FlowCondition | FlowSwitchClause | FlowArrayMutation; export interface FlowNodeBase { flags: FlowFlags; id?: number; @@ -1693,6 +1697,10 @@ declare namespace ts { node: Expression | VariableDeclaration | BindingElement; antecedent: FlowNode; } + export interface FlowCall extends FlowNodeBase { + node: CallExpression; + antecedent: FlowNode; + } export interface FlowCondition extends FlowNodeBase { expression: Expression; antecedent: FlowNode; @@ -2080,21 +2088,28 @@ declare namespace ts { } export enum TypePredicateKind { This = 0, - Identifier = 1 + Identifier = 1, + Assertion = 2 } - export interface TypePredicateBase { - kind: TypePredicateKind; + export interface ThisTypePredicate { + kind: TypePredicateKind.This; + parameterName: undefined; + parameterIndex: undefined; type: Type; } - export interface ThisTypePredicate extends TypePredicateBase { - kind: TypePredicateKind.This; - } - export interface IdentifierTypePredicate extends TypePredicateBase { + export interface IdentifierTypePredicate { kind: TypePredicateKind.Identifier; parameterName: string; parameterIndex: number; + type: Type; } - export type TypePredicate = IdentifierTypePredicate | ThisTypePredicate; + export interface AssertionTypePredicate { + kind: TypePredicateKind.Assertion; + parameterName: string; + parameterIndex: number; + type: Type | undefined; + } + export type TypePredicate = ThisTypePredicate | IdentifierTypePredicate | AssertionTypePredicate; export enum SymbolFlags { None = 0, FunctionScopedVariable = 1, @@ -3823,8 +3838,8 @@ declare namespace ts { function createIndexSignature(decorators: ReadonlyArray | undefined, modifiers: ReadonlyArray | undefined, parameters: ReadonlyArray, type: TypeNode): IndexSignatureDeclaration; function updateIndexSignature(node: IndexSignatureDeclaration, decorators: ReadonlyArray | undefined, modifiers: ReadonlyArray | undefined, parameters: ReadonlyArray, type: TypeNode): IndexSignatureDeclaration; function createKeywordTypeNode(kind: KeywordTypeNode["kind"]): KeywordTypeNode; - function createTypePredicateNode(parameterName: Identifier | ThisTypeNode | string, type: TypeNode): TypePredicateNode; - function updateTypePredicateNode(node: TypePredicateNode, parameterName: Identifier | ThisTypeNode, type: TypeNode): TypePredicateNode; + function createTypePredicateNode(assertsModifier: AssertsToken | undefined, parameterName: Identifier | ThisTypeNode | string, type: TypeNode | undefined): TypePredicateNode; + function updateTypePredicateNode(node: TypePredicateNode, assertsModifier: AssertsToken | undefined, parameterName: Identifier | ThisTypeNode, type: TypeNode | undefined): TypePredicateNode; function createTypeReferenceNode(typeName: string | EntityName, typeArguments: ReadonlyArray | undefined): TypeReferenceNode; function updateTypeReferenceNode(node: TypeReferenceNode, typeName: EntityName, typeArguments: NodeArray | undefined): TypeReferenceNode; function createFunctionTypeNode(typeParameters: ReadonlyArray | undefined, parameters: ReadonlyArray, type: TypeNode | undefined): FunctionTypeNode; From 1c55e5de69c581da19184c1b9bfa08c800d1bb7c Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Mon, 5 Aug 2019 08:27:54 +0200 Subject: [PATCH 25/97] Address code review feedback --- src/compiler/binder.ts | 3 ++- src/compiler/checker.ts | 22 ++++++++++++---------- src/compiler/parser.ts | 5 ++--- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 2b917bc577c..e48943d3c83 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -1285,7 +1285,8 @@ namespace ts { } function isDottedName(node: Expression): boolean { - return node.kind === SyntaxKind.Identifier || node.kind === SyntaxKind.PropertyAccessExpression && isDottedName((node).expression); + return node.kind === SyntaxKind.Identifier || node.kind === SyntaxKind.ThisKeyword || + node.kind === SyntaxKind.PropertyAccessExpression && isDottedName((node).expression); } function bindExpressionStatement(node: ExpressionStatement): void { diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 7dfb0cd2757..66aa107c561 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -16838,16 +16838,18 @@ namespace ts { // that reference function, method, class or value module symbols; or variable, property or // parameter symbols with declarations that have explicit type annotations. Such references are // resolvable with no possibility of triggering circularities in control flow analysis. - if (node.kind === SyntaxKind.Identifier) { - const symbol = getResolvedSymbol(node); - return getExplicitTypeOfSymbol(symbol.flags & SymbolFlags.Alias ? resolveAlias(symbol) : symbol); - } - if (node.kind === SyntaxKind.PropertyAccessExpression) { - const type = getTypeOfDottedName((node).expression); - if (type) { - const prop = getPropertyOfType(type, (node).name.escapedText); - return prop && getExplicitTypeOfSymbol(prop); - } + switch (node.kind) { + case SyntaxKind.Identifier: + const symbol = getResolvedSymbol(node); + return getExplicitTypeOfSymbol(symbol.flags & SymbolFlags.Alias ? resolveAlias(symbol) : symbol); + case SyntaxKind.ThisKeyword: + return checkThisExpression(node); + case SyntaxKind.PropertyAccessExpression: + const type = getTypeOfDottedName((node).expression); + if (type) { + const prop = getPropertyOfType(type, (node).name.escapedText); + return prop && getExplicitTypeOfSymbol(prop); + } } } diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 3e0ea65476a..6dc0235a7d0 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -3211,6 +3211,7 @@ namespace ts { const type = parseType(); if (typePredicateVariable) { const node = createNode(SyntaxKind.TypePredicate, typePredicateVariable.pos); + node.assertsModifier = undefined; node.parameterName = typePredicateVariable; node.type = type; return finishNode(node); @@ -3232,9 +3233,7 @@ namespace ts { const node = createNode(SyntaxKind.TypePredicate); node.assertsModifier = parseExpectedToken(SyntaxKind.AssertsKeyword); node.parameterName = parseIdentifier(); - if (parseOptional(SyntaxKind.IsKeyword)) { - node.type = parseType(); - } + node.type = parseOptional(SyntaxKind.IsKeyword) ? parseType() : undefined; return finishNode(node); } From df02ad6e59b0f3388f6644688303b4419f6e771a Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 8 Aug 2019 10:48:45 +0200 Subject: [PATCH 26/97] Reflect control flow effects of calls to never-returning functions --- src/compiler/binder.ts | 2 +- src/compiler/checker.ts | 76 ++++++++++++++++++++--------------------- src/compiler/types.ts | 2 +- 3 files changed, 39 insertions(+), 41 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index e48943d3c83..8e004b125ab 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -1295,7 +1295,7 @@ namespace ts { // is potentially an assertion and is therefore included in the control flow. if (node.expression.kind === SyntaxKind.CallExpression) { const call = node.expression; - if (isDottedName(call.expression) && call.arguments.length >= 1) { + if (isDottedName(call.expression)) { currentFlow = createFlowCall(currentFlow, call); } } diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 66aa107c561..01de800c7c8 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -16853,31 +16853,23 @@ namespace ts { } } - function getTypePredicateForCall(node: CallExpression) { + function isCallWithEffects(node: CallExpression) { const links = getNodeLinks(node); - if (links.resolvedTypePredicate === undefined) { - links.resolvedTypePredicate = computeTypePredicateForCall(node) || noTypePredicate; + if (links.isCallWithEffects === undefined) { + // A call expression parented by an expression statement is a potential assertion. Other call + // expressions are potential type predicate function calls. + const funcType = node.parent.kind === SyntaxKind.ExpressionStatement ? getTypeOfDottedName(node.expression) : + node.expression.kind !== SyntaxKind.SuperKeyword ? checkNonNullExpression(node.expression) : + undefined; + const apparentType = funcType && getApparentType(funcType) || unknownType; + links.isCallWithEffects = some(getSignaturesOfType(apparentType, SignatureKind.Call), hasTypePredicateOrNeverReturnType); } - return links.resolvedTypePredicate === noTypePredicate ? undefined : links.resolvedTypePredicate; + return links.isCallWithEffects; } - function computeTypePredicateForCall(node: CallExpression) { - // A call expression parented by an expression statement is a potential assertion. Other call - // expressions are potential type predicate function calls. - const funcType = node.parent.kind === SyntaxKind.ExpressionStatement ? getTypeOfDottedName(node.expression) : - node.expression.kind !== SyntaxKind.SuperKeyword ? checkNonNullExpression(node.expression) : - undefined; - if (funcType && funcType !== silentNeverType) { - const apparentType = getApparentType(funcType); - if (some(getSignaturesOfType(apparentType, SignatureKind.Call), hasTypePredicate)) { - return getTypePredicateOfSignature(getResolvedSignature(node)); - } - } - return undefined; - } - - function hasTypePredicate(signature: Signature) { - return !!getTypePredicateOfSignature(signature); + function hasTypePredicateOrNeverReturnType(signature: Signature) { + return !!(getTypePredicateOfSignature(signature) || + signature.declaration && (getReturnTypeFromAnnotation(signature.declaration) || unknownType).flags & TypeFlags.Never); } function reportFlowControlError(node: Node) { @@ -17094,14 +17086,20 @@ namespace ts { } function getTypeAtFlowCall(flow: FlowCall): FlowType | undefined { - const predicate = getTypePredicateForCall(flow.node); - if (predicate && predicate.kind === TypePredicateKind.Assertion) { - const flowType = getTypeAtFlowNode(flow.antecedent); - const type = getTypeFromFlowType(flowType); - const narrowedType = predicate.type ? - narrowTypeByTypePredicate(type, predicate, flow.node, /*assumeTrue*/ true) : - narrowTypeByAssertion(type, flow.node.arguments[predicate.parameterIndex]); - return narrowedType === type ? flowType : createFlowType(narrowedType, isIncomplete(flowType)); + if (isCallWithEffects(flow.node)) { + const signature = getResolvedSignature(flow.node); + const predicate = getTypePredicateOfSignature(signature); + if (predicate && predicate.kind === TypePredicateKind.Assertion) { + const flowType = getTypeAtFlowNode(flow.antecedent); + const type = getTypeFromFlowType(flowType); + const narrowedType = predicate.type ? + narrowTypeByTypePredicate(type, predicate, flow.node, /*assumeTrue*/ true) : + narrowTypeByAssertion(type, flow.node.arguments[predicate.parameterIndex]); + return narrowedType === type ? flowType : createFlowType(narrowedType, isIncomplete(flowType)); + } + if (getReturnTypeOfSignature(signature).flags & TypeFlags.Never) { + return neverType; + } } return undefined; } @@ -17690,8 +17688,9 @@ namespace ts { } function narrowTypeByCallExpression(type: Type, callExpression: CallExpression, assumeTrue: boolean): Type { - if (hasMatchingArgument(callExpression, reference)) { - const predicate = getTypePredicateForCall(callExpression); + if (hasMatchingArgument(callExpression, reference) && isCallWithEffects(callExpression)) { + const signature = getResolvedSignature(callExpression); + const predicate = getTypePredicateOfSignature(signature); if (predicate && predicate.kind !== TypePredicateKind.Assertion) { return narrowTypeByTypePredicate(type, predicate, callExpression, assumeTrue); } @@ -23652,15 +23651,14 @@ namespace ts { return eachTypeContainedIn(mapType(type, getRegularTypeOfLiteralType), switchTypes); } - function functionHasImplicitReturn(func: FunctionLikeDeclaration) { - if (!(func.flags & NodeFlags.HasImplicitReturn)) { - return false; - } + function isNeverFunctionCall(expr: Expression) { + return expr.kind === SyntaxKind.CallExpression && isCallWithEffects(expr) && !!(getTypeOfExpression(expr).flags & TypeFlags.Never); + } - if (some((func.body).statements, statement => statement.kind === SyntaxKind.SwitchStatement && isExhaustiveSwitchStatement(statement))) { - return false; - } - return true; + function functionHasImplicitReturn(func: FunctionLikeDeclaration) { + return !!(func.flags & NodeFlags.HasImplicitReturn) && !some((func.body).statements, statement => + statement.kind === SyntaxKind.SwitchStatement && isExhaustiveSwitchStatement(statement) || + statement.kind === SyntaxKind.ExpressionStatement && isNeverFunctionCall((statement).expression)); } /** NOTE: Return value of `[]` means a different thing than `undefined`. `[]` means func returns `void`, `undefined` means it returns `never`. */ diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 3cdfba2aafb..6b757736cc5 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -3918,7 +3918,7 @@ namespace ts { resolvedSignature?: Signature; // Cached signature of signature node or call expression resolvedSymbol?: Symbol; // Cached name resolution result resolvedIndexInfo?: IndexInfo; // Cached indexing info resolution result - resolvedTypePredicate?: TypePredicate; // Cached type predicate for call expression + isCallWithEffects?: boolean; // Is call expression with possible control flow effects? enumMemberValue?: string | number; // Constant value of enum member isVisible?: boolean; // Is this node visible containsArgumentsReference?: boolean; // Whether a function-like declaration contains an 'arguments' reference From 99ab53edcd01fd217f575e805fd16ecf6c0ba7f9 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Fri, 9 Aug 2019 10:15:47 +0200 Subject: [PATCH 27/97] Make flow nodes more monomorphic --- src/compiler/binder.ts | 50 ++++++++++++++++++++++++----------------- src/compiler/checker.ts | 4 ++-- src/compiler/types.ts | 29 +++++++++++++++--------- 3 files changed, 50 insertions(+), 33 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 8e004b125ab..b4ccdf488b1 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -148,8 +148,8 @@ namespace ts { let Symbol: new (flags: SymbolFlags, name: __String) => Symbol; // tslint:disable-line variable-name let classifiableNames: UnderscoreEscapedMap; - const unreachableFlow: FlowNode = { flags: FlowFlags.Unreachable }; - const reportedUnreachableFlow: FlowNode = { flags: FlowFlags.Unreachable }; + const unreachableFlow = createFlowNode(FlowFlags.Unreachable, /*antecedent*/ undefined, /*node*/ undefined); + const reportedUnreachableFlow: FlowNode = createFlowNode(FlowFlags.Unreachable, /*antecedent*/ undefined, /*node*/ undefined); // state used to aggregate transform flags during bind. let subtreeTransformFlags: TransformFlags = TransformFlags.None; @@ -560,9 +560,9 @@ namespace ts { // A non-async, non-generator IIFE is considered part of the containing control flow. Return statements behave // similarly to break statements that exit to a label just past the statement body. if (!isIIFE) { - currentFlow = { flags: FlowFlags.Start }; + currentFlow = createFlowNode(FlowFlags.Start, /*antecedent*/ undefined, /*node*/ undefined) as FlowStart; if (containerFlags & (ContainerFlags.IsFunctionExpression | ContainerFlags.IsObjectLiteralOrClassExpressionMethod)) { - currentFlow.container = node; + currentFlow.node = node; } } // We create a return control flow graph for IIFEs and constructors. For constructors @@ -842,18 +842,22 @@ namespace ts { return isNarrowableReference(expr); } - function createBranchLabel(): FlowLabel { + function createFlowNode(flags: FlowFlags, antecedent: FlowNode | undefined, node: Node | undefined) { return { - flags: FlowFlags.BranchLabel, - antecedents: undefined - }; + flags, + antecedent, + node, + id: undefined, + antecedents: undefined, + } as FlowNode; + } + + function createBranchLabel(): FlowLabel { + return createFlowNode(FlowFlags.BranchLabel, /*antecedent*/ undefined, /*node*/ undefined) as FlowLabel; } function createLoopLabel(): FlowLabel { - return { - flags: FlowFlags.LoopLabel, - antecedents: undefined - }; + return createFlowNode(FlowFlags.LoopLabel, /*antecedent*/ undefined, /*node*/ undefined) as FlowLabel; } function setFlowNodeReferenced(flow: FlowNode) { @@ -883,7 +887,7 @@ namespace ts { return antecedent; } setFlowNodeReferenced(antecedent); - return flowNodeCreated({ flags, expression, antecedent }); + return flowNodeCreated(createFlowNode(flags, antecedent, expression)); } function createFlowSwitchClause(antecedent: FlowNode, switchStatement: SwitchStatement, clauseStart: number, clauseEnd: number): FlowNode { @@ -891,23 +895,26 @@ namespace ts { return antecedent; } setFlowNodeReferenced(antecedent); - return flowNodeCreated({ flags: FlowFlags.SwitchClause, switchStatement, clauseStart, clauseEnd, antecedent }); + const result = createFlowNode(FlowFlags.SwitchClause, antecedent, /*node*/ undefined) as FlowSwitchClause; + result.switchStatement = switchStatement; + result.clauseStart = clauseStart; + result.clauseEnd = clauseEnd; + return flowNodeCreated(result); } function createFlowAssignment(antecedent: FlowNode, node: Expression | VariableDeclaration | BindingElement): FlowNode { setFlowNodeReferenced(antecedent); - return flowNodeCreated({ flags: FlowFlags.Assignment, antecedent, node }); + return flowNodeCreated(createFlowNode(FlowFlags.Assignment, antecedent, node)); } function createFlowCall(antecedent: FlowNode, node: CallExpression): FlowNode { setFlowNodeReferenced(antecedent); - return flowNodeCreated({ flags: FlowFlags.Call, antecedent, node }); + return flowNodeCreated(createFlowNode(FlowFlags.Call, antecedent, node)); } function createFlowArrayMutation(antecedent: FlowNode, node: CallExpression | BinaryExpression): FlowNode { setFlowNodeReferenced(antecedent); - const res: FlowArrayMutation = flowNodeCreated({ flags: FlowFlags.ArrayMutation, antecedent, node }); - return res; + return flowNodeCreated(createFlowNode(FlowFlags.ArrayMutation, antecedent, node)); } function finishFlowLabel(flow: FlowLabel): FlowNode { @@ -1185,7 +1192,8 @@ namespace ts { // // extra edges that we inject allows to control this behavior // if when walking the flow we step on post-finally edge - we can mark matching pre-finally edge as locked so it will be skipped. - const preFinallyFlow: PreFinallyFlow = { flags: FlowFlags.PreFinally, antecedent: preFinallyPrior, lock: {} }; + const preFinallyFlow = createFlowNode(FlowFlags.PreFinally, preFinallyPrior, /*node*/ undefined) as PreFinallyFlow; + preFinallyFlow.lock = {}; addAntecedent(preFinallyLabel, preFinallyFlow); currentFlow = finishFlowLabel(preFinallyLabel); @@ -1204,7 +1212,7 @@ namespace ts { } } if (!(currentFlow.flags & FlowFlags.Unreachable)) { - const afterFinallyFlow: AfterFinallyFlow = flowNodeCreated({ flags: FlowFlags.AfterFinally, antecedent: currentFlow }); + const afterFinallyFlow = flowNodeCreated(createFlowNode(FlowFlags.AfterFinally, currentFlow, /*node*/ undefined) as AfterFinallyFlow); preFinallyFlow.lock = afterFinallyFlow; currentFlow = afterFinallyFlow; } @@ -1828,7 +1836,7 @@ namespace ts { const host = getJSDocHost(typeAlias); container = findAncestor(host.parent, n => !!(getContainerFlags(n) & ContainerFlags.IsContainer)) || file; blockScopeContainer = getEnclosingBlockScopeContainer(host) || file; - currentFlow = { flags: FlowFlags.Start }; + currentFlow = createFlowNode(FlowFlags.Start, /*antecedent*/ undefined, /*node*/ undefined); parent = typeAlias; bind(typeAlias.typeExpression); if (isJSDocEnumTag(typeAlias) || !typeAlias.fullName || typeAlias.fullName.kind === SyntaxKind.Identifier) { diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 01de800c7c8..491dc35a2b2 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -17001,7 +17001,7 @@ namespace ts { } else if (flags & FlowFlags.Start) { // Check if we should continue with the control flow of the containing function. - const container = (flow).container; + const container = (flow).node; if (container && container !== flowContainer && reference.kind !== SyntaxKind.PropertyAccessExpression && reference.kind !== SyntaxKind.ElementAccessExpression && @@ -17150,7 +17150,7 @@ namespace ts { // *only* place a silent never type is ever generated. const assumeTrue = (flow.flags & FlowFlags.TrueCondition) !== 0; const nonEvolvingType = finalizeEvolvingArrayType(type); - const narrowedType = narrowType(nonEvolvingType, flow.expression, assumeTrue); + const narrowedType = narrowType(nonEvolvingType, flow.node, assumeTrue); if (narrowedType === nonEvolvingType) { return flowType; } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 6b757736cc5..96a054e841b 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2565,6 +2565,22 @@ namespace ts { Condition = TrueCondition | FalseCondition } + export type FlowNode = + | AfterFinallyFlow + | PreFinallyFlow + | FlowStart + | FlowLabel + | FlowAssignment + | FlowCall + | FlowCondition + | FlowSwitchClause + | FlowArrayMutation; + + export interface FlowNodeBase { + flags: FlowFlags; + id: number | undefined; // Node id used by flow type cache in checker + } + export interface FlowLock { locked?: boolean; } @@ -2578,18 +2594,11 @@ namespace ts { lock: FlowLock; } - export type FlowNode = - | AfterFinallyFlow | PreFinallyFlow | FlowStart | FlowLabel | FlowAssignment | FlowCall | FlowCondition | FlowSwitchClause | FlowArrayMutation; - export interface FlowNodeBase { - flags: FlowFlags; - id?: number; // Node id used by flow type cache in checker - } - // FlowStart represents the start of a control flow. For a function expression or arrow - // function, the container property references the function (which in turn has a flowNode + // function, the node property references the function (which in turn has a flowNode // property for the containing control flow). export interface FlowStart extends FlowNodeBase { - container?: FunctionExpression | ArrowFunction | MethodDeclaration; + node?: FunctionExpression | ArrowFunction | MethodDeclaration; } // FlowLabel represents a junction with multiple possible preceding control flows. @@ -2612,7 +2621,7 @@ namespace ts { // FlowCondition represents a condition that is known to be true or false at the // node's location in the control flow. export interface FlowCondition extends FlowNodeBase { - expression: Expression; + node: Expression; antecedent: FlowNode; } From d5e08d485d783537c4bbdc5d72425763656cba5d Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Fri, 9 Aug 2019 10:16:16 +0200 Subject: [PATCH 28/97] Accept baseline API changes --- tests/baselines/reference/api/tsserverlibrary.d.ts | 14 +++++++------- tests/baselines/reference/api/typescript.d.ts | 14 +++++++------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 00c88fc4963..c1a271bb0af 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -1672,6 +1672,11 @@ declare namespace ts { Label = 12, Condition = 96 } + export type FlowNode = AfterFinallyFlow | PreFinallyFlow | FlowStart | FlowLabel | FlowAssignment | FlowCall | FlowCondition | FlowSwitchClause | FlowArrayMutation; + export interface FlowNodeBase { + flags: FlowFlags; + id: number | undefined; + } export interface FlowLock { locked?: boolean; } @@ -1682,13 +1687,8 @@ declare namespace ts { antecedent: FlowNode; lock: FlowLock; } - export type FlowNode = AfterFinallyFlow | PreFinallyFlow | FlowStart | FlowLabel | FlowAssignment | FlowCall | FlowCondition | FlowSwitchClause | FlowArrayMutation; - export interface FlowNodeBase { - flags: FlowFlags; - id?: number; - } export interface FlowStart extends FlowNodeBase { - container?: FunctionExpression | ArrowFunction | MethodDeclaration; + node?: FunctionExpression | ArrowFunction | MethodDeclaration; } export interface FlowLabel extends FlowNodeBase { antecedents: FlowNode[] | undefined; @@ -1702,7 +1702,7 @@ declare namespace ts { antecedent: FlowNode; } export interface FlowCondition extends FlowNodeBase { - expression: Expression; + node: Expression; antecedent: FlowNode; } export interface FlowSwitchClause extends FlowNodeBase { diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index df8d0e1ee79..0bb7d724ee0 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -1672,6 +1672,11 @@ declare namespace ts { Label = 12, Condition = 96 } + export type FlowNode = AfterFinallyFlow | PreFinallyFlow | FlowStart | FlowLabel | FlowAssignment | FlowCall | FlowCondition | FlowSwitchClause | FlowArrayMutation; + export interface FlowNodeBase { + flags: FlowFlags; + id: number | undefined; + } export interface FlowLock { locked?: boolean; } @@ -1682,13 +1687,8 @@ declare namespace ts { antecedent: FlowNode; lock: FlowLock; } - export type FlowNode = AfterFinallyFlow | PreFinallyFlow | FlowStart | FlowLabel | FlowAssignment | FlowCall | FlowCondition | FlowSwitchClause | FlowArrayMutation; - export interface FlowNodeBase { - flags: FlowFlags; - id?: number; - } export interface FlowStart extends FlowNodeBase { - container?: FunctionExpression | ArrowFunction | MethodDeclaration; + node?: FunctionExpression | ArrowFunction | MethodDeclaration; } export interface FlowLabel extends FlowNodeBase { antecedents: FlowNode[] | undefined; @@ -1702,7 +1702,7 @@ declare namespace ts { antecedent: FlowNode; } export interface FlowCondition extends FlowNodeBase { - expression: Expression; + node: Expression; antecedent: FlowNode; } export interface FlowSwitchClause extends FlowNodeBase { From 19f1d3ba0a8c0f85a99a770d3a344161b911d934 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Fri, 9 Aug 2019 12:35:28 +0200 Subject: [PATCH 29/97] Less aggressive monomorphism for flow nodes --- src/compiler/binder.ts | 41 +++++++++++++---------------------------- src/compiler/types.ts | 2 +- 2 files changed, 14 insertions(+), 29 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index b4ccdf488b1..19dd6bee232 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -148,8 +148,8 @@ namespace ts { let Symbol: new (flags: SymbolFlags, name: __String) => Symbol; // tslint:disable-line variable-name let classifiableNames: UnderscoreEscapedMap; - const unreachableFlow = createFlowNode(FlowFlags.Unreachable, /*antecedent*/ undefined, /*node*/ undefined); - const reportedUnreachableFlow: FlowNode = createFlowNode(FlowFlags.Unreachable, /*antecedent*/ undefined, /*node*/ undefined); + const unreachableFlow: FlowNode = { flags: FlowFlags.Unreachable }; + const reportedUnreachableFlow: FlowNode = { flags: FlowFlags.Unreachable }; // state used to aggregate transform flags during bind. let subtreeTransformFlags: TransformFlags = TransformFlags.None; @@ -560,7 +560,7 @@ namespace ts { // A non-async, non-generator IIFE is considered part of the containing control flow. Return statements behave // similarly to break statements that exit to a label just past the statement body. if (!isIIFE) { - currentFlow = createFlowNode(FlowFlags.Start, /*antecedent*/ undefined, /*node*/ undefined) as FlowStart; + currentFlow = { flags: FlowFlags.Start }; if (containerFlags & (ContainerFlags.IsFunctionExpression | ContainerFlags.IsObjectLiteralOrClassExpressionMethod)) { currentFlow.node = node; } @@ -842,22 +842,12 @@ namespace ts { return isNarrowableReference(expr); } - function createFlowNode(flags: FlowFlags, antecedent: FlowNode | undefined, node: Node | undefined) { - return { - flags, - antecedent, - node, - id: undefined, - antecedents: undefined, - } as FlowNode; - } - function createBranchLabel(): FlowLabel { - return createFlowNode(FlowFlags.BranchLabel, /*antecedent*/ undefined, /*node*/ undefined) as FlowLabel; + return { flags: FlowFlags.BranchLabel, antecedents: undefined }; } function createLoopLabel(): FlowLabel { - return createFlowNode(FlowFlags.LoopLabel, /*antecedent*/ undefined, /*node*/ undefined) as FlowLabel; + return { flags: FlowFlags.LoopLabel, antecedents: undefined }; } function setFlowNodeReferenced(flow: FlowNode) { @@ -887,7 +877,7 @@ namespace ts { return antecedent; } setFlowNodeReferenced(antecedent); - return flowNodeCreated(createFlowNode(flags, antecedent, expression)); + return flowNodeCreated({ flags, antecedent, node: expression }); } function createFlowSwitchClause(antecedent: FlowNode, switchStatement: SwitchStatement, clauseStart: number, clauseEnd: number): FlowNode { @@ -895,26 +885,22 @@ namespace ts { return antecedent; } setFlowNodeReferenced(antecedent); - const result = createFlowNode(FlowFlags.SwitchClause, antecedent, /*node*/ undefined) as FlowSwitchClause; - result.switchStatement = switchStatement; - result.clauseStart = clauseStart; - result.clauseEnd = clauseEnd; - return flowNodeCreated(result); + return flowNodeCreated({ flags: FlowFlags.SwitchClause, antecedent, switchStatement, clauseStart, clauseEnd }); } function createFlowAssignment(antecedent: FlowNode, node: Expression | VariableDeclaration | BindingElement): FlowNode { setFlowNodeReferenced(antecedent); - return flowNodeCreated(createFlowNode(FlowFlags.Assignment, antecedent, node)); + return flowNodeCreated({ flags: FlowFlags.Assignment, antecedent, node }); } function createFlowCall(antecedent: FlowNode, node: CallExpression): FlowNode { setFlowNodeReferenced(antecedent); - return flowNodeCreated(createFlowNode(FlowFlags.Call, antecedent, node)); + return flowNodeCreated({ flags: FlowFlags.Call, antecedent, node }); } function createFlowArrayMutation(antecedent: FlowNode, node: CallExpression | BinaryExpression): FlowNode { setFlowNodeReferenced(antecedent); - return flowNodeCreated(createFlowNode(FlowFlags.ArrayMutation, antecedent, node)); + return flowNodeCreated({ flags: FlowFlags.ArrayMutation, antecedent, node }); } function finishFlowLabel(flow: FlowLabel): FlowNode { @@ -1192,8 +1178,7 @@ namespace ts { // // extra edges that we inject allows to control this behavior // if when walking the flow we step on post-finally edge - we can mark matching pre-finally edge as locked so it will be skipped. - const preFinallyFlow = createFlowNode(FlowFlags.PreFinally, preFinallyPrior, /*node*/ undefined) as PreFinallyFlow; - preFinallyFlow.lock = {}; + const preFinallyFlow: PreFinallyFlow = { flags: FlowFlags.PreFinally, antecedent: preFinallyPrior, lock: {} }; addAntecedent(preFinallyLabel, preFinallyFlow); currentFlow = finishFlowLabel(preFinallyLabel); @@ -1212,7 +1197,7 @@ namespace ts { } } if (!(currentFlow.flags & FlowFlags.Unreachable)) { - const afterFinallyFlow = flowNodeCreated(createFlowNode(FlowFlags.AfterFinally, currentFlow, /*node*/ undefined) as AfterFinallyFlow); + const afterFinallyFlow: AfterFinallyFlow = flowNodeCreated({ flags: FlowFlags.AfterFinally, antecedent: currentFlow }); preFinallyFlow.lock = afterFinallyFlow; currentFlow = afterFinallyFlow; } @@ -1836,7 +1821,7 @@ namespace ts { const host = getJSDocHost(typeAlias); container = findAncestor(host.parent, n => !!(getContainerFlags(n) & ContainerFlags.IsContainer)) || file; blockScopeContainer = getEnclosingBlockScopeContainer(host) || file; - currentFlow = createFlowNode(FlowFlags.Start, /*antecedent*/ undefined, /*node*/ undefined); + currentFlow = { flags: FlowFlags.Start }; parent = typeAlias; bind(typeAlias.typeExpression); if (isJSDocEnumTag(typeAlias) || !typeAlias.fullName || typeAlias.fullName.kind === SyntaxKind.Identifier) { diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 96a054e841b..2702f2c5249 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2578,7 +2578,7 @@ namespace ts { export interface FlowNodeBase { flags: FlowFlags; - id: number | undefined; // Node id used by flow type cache in checker + id?: number; // Node id used by flow type cache in checker } export interface FlowLock { From 83212e72407967c194762e2a2e097e2dc2076007 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Fri, 9 Aug 2019 12:35:48 +0200 Subject: [PATCH 30/97] Accept API baseline changes --- tests/baselines/reference/api/tsserverlibrary.d.ts | 2 +- tests/baselines/reference/api/typescript.d.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index c1a271bb0af..2579518fe17 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -1675,7 +1675,7 @@ declare namespace ts { export type FlowNode = AfterFinallyFlow | PreFinallyFlow | FlowStart | FlowLabel | FlowAssignment | FlowCall | FlowCondition | FlowSwitchClause | FlowArrayMutation; export interface FlowNodeBase { flags: FlowFlags; - id: number | undefined; + id?: number; } export interface FlowLock { locked?: boolean; diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index 0bb7d724ee0..4417b52af36 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -1675,7 +1675,7 @@ declare namespace ts { export type FlowNode = AfterFinallyFlow | PreFinallyFlow | FlowStart | FlowLabel | FlowAssignment | FlowCall | FlowCondition | FlowSwitchClause | FlowArrayMutation; export interface FlowNodeBase { flags: FlowFlags; - id: number | undefined; + id?: number; } export interface FlowLock { locked?: boolean; From cdeddf14e9a112e64403eecb09257ac51023467f Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sat, 10 Aug 2019 08:38:18 +0200 Subject: [PATCH 31/97] Call getResolvedSignature only when needed for generics or overloads --- src/compiler/checker.ts | 27 ++++++++++++++++----------- src/compiler/types.ts | 2 +- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 71493cbe634..b2bc96fa9cd 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -16877,18 +16877,22 @@ namespace ts { } } - function isCallWithEffects(node: CallExpression) { + function getEffectsSignature(node: CallExpression) { const links = getNodeLinks(node); - if (links.isCallWithEffects === undefined) { + let signature = links.effectsSignature; + if (signature === undefined) { // A call expression parented by an expression statement is a potential assertion. Other call // expressions are potential type predicate function calls. const funcType = node.parent.kind === SyntaxKind.ExpressionStatement ? getTypeOfDottedName(node.expression) : node.expression.kind !== SyntaxKind.SuperKeyword ? checkNonNullExpression(node.expression) : undefined; - const apparentType = funcType && getApparentType(funcType) || unknownType; - links.isCallWithEffects = some(getSignaturesOfType(apparentType, SignatureKind.Call), hasTypePredicateOrNeverReturnType); + const signatures = getSignaturesOfType(funcType && getApparentType(funcType) || unknownType, SignatureKind.Call); + const candidate = signatures.length === 1 && !signatures[0].typeParameters ? signatures[0] : + some(signatures, hasTypePredicateOrNeverReturnType) ? getResolvedSignature(node) : + undefined; + signature = links.effectsSignature = candidate && hasTypePredicateOrNeverReturnType(candidate) ? candidate : unknownSignature; } - return links.isCallWithEffects; + return signature === unknownSignature ? undefined : signature; } function hasTypePredicateOrNeverReturnType(signature: Signature) { @@ -17110,8 +17114,8 @@ namespace ts { } function getTypeAtFlowCall(flow: FlowCall): FlowType | undefined { - if (isCallWithEffects(flow.node)) { - const signature = getResolvedSignature(flow.node); + const signature = getEffectsSignature(flow.node); + if (signature) { const predicate = getTypePredicateOfSignature(signature); if (predicate && predicate.kind === TypePredicateKind.Assertion) { const flowType = getTypeAtFlowNode(flow.antecedent); @@ -17712,9 +17716,9 @@ namespace ts { } function narrowTypeByCallExpression(type: Type, callExpression: CallExpression, assumeTrue: boolean): Type { - if (hasMatchingArgument(callExpression, reference) && isCallWithEffects(callExpression)) { - const signature = getResolvedSignature(callExpression); - const predicate = getTypePredicateOfSignature(signature); + if (hasMatchingArgument(callExpression, reference)) { + const signature = getEffectsSignature(callExpression); + const predicate = signature && getTypePredicateOfSignature(signature); if (predicate && predicate.kind !== TypePredicateKind.Assertion) { return narrowTypeByTypePredicate(type, predicate, callExpression, assumeTrue); } @@ -23695,7 +23699,8 @@ namespace ts { } function isNeverFunctionCall(expr: Expression) { - return expr.kind === SyntaxKind.CallExpression && isCallWithEffects(expr) && !!(getTypeOfExpression(expr).flags & TypeFlags.Never); + const signature = expr.kind === SyntaxKind.CallExpression && getEffectsSignature(expr); + return !!(signature && getReturnTypeOfSignature(signature).flags & TypeFlags.Never); } function functionHasImplicitReturn(func: FunctionLikeDeclaration) { diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 7038b45e24a..9be0bf42db9 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -3927,7 +3927,7 @@ namespace ts { resolvedSignature?: Signature; // Cached signature of signature node or call expression resolvedSymbol?: Symbol; // Cached name resolution result resolvedIndexInfo?: IndexInfo; // Cached indexing info resolution result - isCallWithEffects?: boolean; // Is call expression with possible control flow effects? + effectsSignature?: Signature; // Signature with possible control flow effects enumMemberValue?: string | number; // Constant value of enum member isVisible?: boolean; // Is this node visible containsArgumentsReference?: boolean; // Whether a function-like declaration contains an 'arguments' reference From 0599f848575dcfbda5730c446003f4e59411446d Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sat, 17 Aug 2019 06:23:07 -0700 Subject: [PATCH 32/97] Support 'asserts this' and 'asserts this is T' type predicates --- src/compiler/checker.ts | 34 ++++++++++++++++++---------------- src/compiler/parser.ts | 2 +- src/compiler/types.ts | 16 ++++++++++++---- 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 58af0a7134a..c919bb986e4 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4228,8 +4228,10 @@ namespace ts { let returnTypeNode: TypeNode | undefined; const typePredicate = getTypePredicateOfSignature(signature); if (typePredicate) { - const assertsModifier = typePredicate.kind === TypePredicateKind.Assertion ? createToken(SyntaxKind.AssertsKeyword) : undefined; - const parameterName = typePredicate.kind !== TypePredicateKind.This ? + const assertsModifier = typePredicate.kind === TypePredicateKind.AssertsThis || typePredicate.kind === TypePredicateKind.AssertsIdentifier ? + createToken(SyntaxKind.AssertsKeyword) : + undefined; + const parameterName = typePredicate.kind === TypePredicateKind.Identifier || typePredicate.kind === TypePredicateKind.AssertsIdentifier ? setEmitFlags(createIdentifier(typePredicate.parameterName), EmitFlags.NoAsciiEscaping) : createThisTypeNode(); const typeNode = typePredicate.type && typeToTypeNodeHelper(typePredicate.type, context); @@ -4702,8 +4704,8 @@ namespace ts { function typePredicateToStringWorker(writer: EmitTextWriter) { const predicate = createTypePredicateNode( - typePredicate.kind === TypePredicateKind.Assertion ? createToken(SyntaxKind.AssertsKeyword) : undefined, - typePredicate.kind !== TypePredicateKind.This ? createIdentifier(typePredicate.parameterName) : createThisTypeNode(), + typePredicate.kind === TypePredicateKind.AssertsThis || typePredicate.kind === TypePredicateKind.AssertsIdentifier ? createToken(SyntaxKind.AssertsKeyword) : undefined, + typePredicate.kind === TypePredicateKind.Identifier || typePredicate.kind === TypePredicateKind.AssertsIdentifier ? createIdentifier(typePredicate.parameterName) : createThisTypeNode(), typePredicate.type && nodeBuilder.typeToTypeNode(typePredicate.type, enclosingDeclaration, toNodeBuilderFlags(flags) | NodeBuilderFlags.IgnoreErrors | NodeBuilderFlags.WriteTypeParametersInQualifiedName)! // TODO: GH#18217 ); const printer = createPrinter({ removeComments: true }); @@ -8721,8 +8723,8 @@ namespace ts { const parameterName = node.parameterName; const type = node.type && getTypeFromTypeNode(node.type); return parameterName.kind === SyntaxKind.ThisType ? - createTypePredicate(TypePredicateKind.This, /*parameterName*/ undefined, /*parameterIndex*/ undefined, type) : - createTypePredicate(node.assertsModifier ? TypePredicateKind.Assertion : TypePredicateKind.Identifier, parameterName.escapedText as string, + createTypePredicate(node.assertsModifier ? TypePredicateKind.AssertsThis : TypePredicateKind.This, /*parameterName*/ undefined, /*parameterIndex*/ undefined, type) : + createTypePredicate(node.assertsModifier ? TypePredicateKind.AssertsIdentifier : TypePredicateKind.Identifier, parameterName.escapedText as string, findIndex(signature.parameters, p => p.escapedName === parameterName.escapedText), type); } @@ -9829,7 +9831,7 @@ namespace ts { const types: Type[] = []; for (const sig of signatures) { const pred = getTypePredicateOfSignature(sig); - if (!pred || pred.kind === TypePredicateKind.Assertion) { + if (!pred || pred.kind === TypePredicateKind.AssertsThis || pred.kind === TypePredicateKind.AssertsIdentifier) { continue; } @@ -12400,7 +12402,7 @@ namespace ts { return Ternary.False; } - if (source.kind !== TypePredicateKind.This) { + if (source.kind === TypePredicateKind.Identifier || source.kind === TypePredicateKind.AssertsIdentifier) { if (source.parameterIndex !== (target as IdentifierTypePredicate).parameterIndex) { if (reportErrors) { errorReporter!(Diagnostics.Parameter_0_is_not_in_the_same_position_as_parameter_1, source.parameterName, (target as IdentifierTypePredicate).parameterName); @@ -17119,12 +17121,12 @@ namespace ts { const signature = getEffectsSignature(flow.node); if (signature) { const predicate = getTypePredicateOfSignature(signature); - if (predicate && predicate.kind === TypePredicateKind.Assertion) { + if (predicate && (predicate.kind === TypePredicateKind.AssertsThis || predicate.kind === TypePredicateKind.AssertsIdentifier)) { const flowType = getTypeAtFlowNode(flow.antecedent); const type = getTypeFromFlowType(flowType); - const narrowedType = predicate.type ? - narrowTypeByTypePredicate(type, predicate, flow.node, /*assumeTrue*/ true) : - narrowTypeByAssertion(type, flow.node.arguments[predicate.parameterIndex]); + const narrowedType = predicate.type ? narrowTypeByTypePredicate(type, predicate, flow.node, /*assumeTrue*/ true) : + predicate.kind === TypePredicateKind.AssertsIdentifier ? narrowTypeByAssertion(type, flow.node.arguments[predicate.parameterIndex]) : + type; return narrowedType === type ? flowType : createFlowType(narrowedType, isIncomplete(flowType)); } if (getReturnTypeOfSignature(signature).flags & TypeFlags.Never) { @@ -17721,7 +17723,7 @@ namespace ts { if (hasMatchingArgument(callExpression, reference)) { const signature = getEffectsSignature(callExpression); const predicate = signature && getTypePredicateOfSignature(signature); - if (predicate && predicate.kind !== TypePredicateKind.Assertion) { + if (predicate && (predicate.kind === TypePredicateKind.This || predicate.kind === TypePredicateKind.Identifier)) { return narrowTypeByTypePredicate(type, predicate, callExpression, assumeTrue); } } @@ -17733,7 +17735,7 @@ namespace ts { if (isTypeAny(type) && (predicate.type === globalObjectType || predicate.type === globalFunctionType)) { return type; } - if (predicate.kind !== TypePredicateKind.This) { + if (predicate.kind === TypePredicateKind.Identifier || predicate.kind === TypePredicateKind.AssertsIdentifier) { const predicateArgument = callExpression.arguments[predicate.parameterIndex]; if (predicateArgument && predicate.type) { if (isMatchingReference(reference, predicateArgument)) { @@ -17746,7 +17748,7 @@ namespace ts { } else { const invokedExpression = skipParentheses(callExpression.expression); - if (isAccessExpression(invokedExpression)) { + if (isAccessExpression(invokedExpression) && predicate.type) { const possibleReference = skipParentheses(invokedExpression.expression); if (isMatchingReference(reference, possibleReference)) { return getNarrowedType(type, predicate.type, assumeTrue, isTypeSubtypeOf); @@ -25501,7 +25503,7 @@ namespace ts { checkSourceElement(node.type); const { parameterName } = node; - if (isThisTypePredicate(typePredicate)) { + if (typePredicate.kind === TypePredicateKind.This || typePredicate.kind === TypePredicateKind.AssertsThis) { getTypeFromThisTypeNode(parameterName as ThisTypeNode); } else { diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 3c2b7bd42cc..c1331165327 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -3258,7 +3258,7 @@ namespace ts { function parseAssertsTypePredicate(): TypeNode { const node = createNode(SyntaxKind.TypePredicate); node.assertsModifier = parseExpectedToken(SyntaxKind.AssertsKeyword); - node.parameterName = parseIdentifier(); + node.parameterName = token() === SyntaxKind.ThisKeyword ? parseThisTypeNode() : parseIdentifier(); node.type = parseOptional(SyntaxKind.IsKeyword) ? parseType() : undefined; return finishNode(node); } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 3b121c76e20..b27de58134a 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -3514,7 +3514,8 @@ namespace ts { export const enum TypePredicateKind { This, Identifier, - Assertion + AssertsThis, + AssertsIdentifier } export interface ThisTypePredicate { @@ -3531,14 +3532,21 @@ namespace ts { type: Type; } - export interface AssertionTypePredicate { - kind: TypePredicateKind.Assertion; + export interface AssertsThisTypePredicate { + kind: TypePredicateKind.AssertsThis; + parameterName: undefined; + parameterIndex: undefined; + type: Type | undefined; + } + + export interface AssertsIdentifierTypePredicate { + kind: TypePredicateKind.AssertsIdentifier; parameterName: string; parameterIndex: number; type: Type | undefined; } - export type TypePredicate = ThisTypePredicate | IdentifierTypePredicate | AssertionTypePredicate; + export type TypePredicate = ThisTypePredicate | IdentifierTypePredicate | AssertsThisTypePredicate | AssertsIdentifierTypePredicate; /* @internal */ export type AnyImportSyntax = ImportDeclaration | ImportEqualsDeclaration; From e7cbfc41e5afbaa68bdc30e2865acd3a7b0b44e8 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sat, 17 Aug 2019 07:05:49 -0700 Subject: [PATCH 33/97] Update API to be backwards compatible --- src/compiler/checker.ts | 4 ++-- src/compiler/factory.ts | 17 +++++++++++++---- src/compiler/visitor.ts | 2 +- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index c919bb986e4..0af56d7f172 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4235,7 +4235,7 @@ namespace ts { setEmitFlags(createIdentifier(typePredicate.parameterName), EmitFlags.NoAsciiEscaping) : createThisTypeNode(); const typeNode = typePredicate.type && typeToTypeNodeHelper(typePredicate.type, context); - returnTypeNode = createTypePredicateNode(assertsModifier, parameterName, typeNode); + returnTypeNode = createTypePredicateNodeWithModifier(assertsModifier, parameterName, typeNode); } else { const returnType = getReturnTypeOfSignature(signature); @@ -4703,7 +4703,7 @@ namespace ts { return writer ? typePredicateToStringWorker(writer).getText() : usingSingleLineStringWriter(typePredicateToStringWorker); function typePredicateToStringWorker(writer: EmitTextWriter) { - const predicate = createTypePredicateNode( + const predicate = createTypePredicateNodeWithModifier( typePredicate.kind === TypePredicateKind.AssertsThis || typePredicate.kind === TypePredicateKind.AssertsIdentifier ? createToken(SyntaxKind.AssertsKeyword) : undefined, typePredicate.kind === TypePredicateKind.Identifier || typePredicate.kind === TypePredicateKind.AssertsIdentifier ? createIdentifier(typePredicate.parameterName) : createThisTypeNode(), typePredicate.type && nodeBuilder.typeToTypeNode(typePredicate.type, enclosingDeclaration, toNodeBuilderFlags(flags) | NodeBuilderFlags.IgnoreErrors | NodeBuilderFlags.WriteTypeParametersInQualifiedName)! // TODO: GH#18217 diff --git a/src/compiler/factory.ts b/src/compiler/factory.ts index 9a484681acf..3118b9a2efe 100644 --- a/src/compiler/factory.ts +++ b/src/compiler/factory.ts @@ -667,7 +667,11 @@ namespace ts { return createSynthesizedNode(kind); } - export function createTypePredicateNode(assertsModifier: AssertsToken | undefined, parameterName: Identifier | ThisTypeNode | string, type: TypeNode | undefined) { + export function createTypePredicateNode(parameterName: Identifier | ThisTypeNode | string, type: TypeNode | undefined) { + return createTypePredicateNodeWithModifier(/*assertsModifier*/ undefined, parameterName, type); + } + + export function createTypePredicateNodeWithModifier(assertsModifier: AssertsToken | undefined, parameterName: Identifier | ThisTypeNode | string, type: TypeNode | undefined) { const node = createSynthesizedNode(SyntaxKind.TypePredicate) as TypePredicateNode; node.assertsModifier = assertsModifier; node.parameterName = asName(parameterName); @@ -675,10 +679,15 @@ namespace ts { return node; } - export function updateTypePredicateNode(node: TypePredicateNode, assertsModifier: AssertsToken | undefined, parameterName: Identifier | ThisTypeNode, type: TypeNode | undefined) { - return node.parameterName !== parameterName + export function updateTypePredicateNode(node: TypePredicateNode, parameterName: Identifier | ThisTypeNode, type: TypeNode | undefined) { + return updateTypePredicateNodeWithModifier(node, node.assertsModifier, parameterName, type); + } + + export function updateTypePredicateNodeWithModifier(node: TypePredicateNode, assertsModifier: AssertsToken | undefined, parameterName: Identifier | ThisTypeNode, type: TypeNode | undefined) { + return node.assertsModifier !== assertsModifier + || node.parameterName !== parameterName || node.type !== type - ? updateNode(createTypePredicateNode(assertsModifier, parameterName, type), node) + ? updateNode(createTypePredicateNodeWithModifier(assertsModifier, parameterName, type), node) : node; } diff --git a/src/compiler/visitor.ts b/src/compiler/visitor.ts index aa497511f78..266b794438b 100644 --- a/src/compiler/visitor.ts +++ b/src/compiler/visitor.ts @@ -339,7 +339,7 @@ namespace ts { // Types case SyntaxKind.TypePredicate: - return updateTypePredicateNode(node, + return updateTypePredicateNodeWithModifier(node, visitNode((node).assertsModifier, visitor), visitNode((node).parameterName, visitor), visitNode((node).type, visitor, isTypeNode)); From 2c36249ed6bd63f4fd66813985ae2dff681349a5 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sat, 17 Aug 2019 07:07:27 -0700 Subject: [PATCH 34/97] Accept new API baselines --- .../reference/api/tsserverlibrary.d.ts | 21 +++++++++++++------ tests/baselines/reference/api/typescript.d.ts | 21 +++++++++++++------ 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 60222fc8bd3..c458e88300b 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -2094,7 +2094,8 @@ declare namespace ts { export enum TypePredicateKind { This = 0, Identifier = 1, - Assertion = 2 + AssertsThis = 2, + AssertsIdentifier = 3 } export interface ThisTypePredicate { kind: TypePredicateKind.This; @@ -2108,13 +2109,19 @@ declare namespace ts { parameterIndex: number; type: Type; } - export interface AssertionTypePredicate { - kind: TypePredicateKind.Assertion; + export interface AssertsThisTypePredicate { + kind: TypePredicateKind.AssertsThis; + parameterName: undefined; + parameterIndex: undefined; + type: Type | undefined; + } + export interface AssertsIdentifierTypePredicate { + kind: TypePredicateKind.AssertsIdentifier; parameterName: string; parameterIndex: number; type: Type | undefined; } - export type TypePredicate = ThisTypePredicate | IdentifierTypePredicate | AssertionTypePredicate; + export type TypePredicate = ThisTypePredicate | IdentifierTypePredicate | AssertsThisTypePredicate | AssertsIdentifierTypePredicate; export enum SymbolFlags { None = 0, FunctionScopedVariable = 1, @@ -3843,8 +3850,10 @@ declare namespace ts { function createIndexSignature(decorators: ReadonlyArray | undefined, modifiers: ReadonlyArray | undefined, parameters: ReadonlyArray, type: TypeNode): IndexSignatureDeclaration; function updateIndexSignature(node: IndexSignatureDeclaration, decorators: ReadonlyArray | undefined, modifiers: ReadonlyArray | undefined, parameters: ReadonlyArray, type: TypeNode): IndexSignatureDeclaration; function createKeywordTypeNode(kind: KeywordTypeNode["kind"]): KeywordTypeNode; - function createTypePredicateNode(assertsModifier: AssertsToken | undefined, parameterName: Identifier | ThisTypeNode | string, type: TypeNode | undefined): TypePredicateNode; - function updateTypePredicateNode(node: TypePredicateNode, assertsModifier: AssertsToken | undefined, parameterName: Identifier | ThisTypeNode, type: TypeNode | undefined): TypePredicateNode; + function createTypePredicateNode(parameterName: Identifier | ThisTypeNode | string, type: TypeNode | undefined): TypePredicateNode; + function createTypePredicateNodeWithModifier(assertsModifier: AssertsToken | undefined, parameterName: Identifier | ThisTypeNode | string, type: TypeNode | undefined): TypePredicateNode; + function updateTypePredicateNode(node: TypePredicateNode, parameterName: Identifier | ThisTypeNode, type: TypeNode | undefined): TypePredicateNode; + function updateTypePredicateNodeWithModifier(node: TypePredicateNode, assertsModifier: AssertsToken | undefined, parameterName: Identifier | ThisTypeNode, type: TypeNode | undefined): TypePredicateNode; function createTypeReferenceNode(typeName: string | EntityName, typeArguments: ReadonlyArray | undefined): TypeReferenceNode; function updateTypeReferenceNode(node: TypeReferenceNode, typeName: EntityName, typeArguments: NodeArray | undefined): TypeReferenceNode; function createFunctionTypeNode(typeParameters: ReadonlyArray | undefined, parameters: ReadonlyArray, type: TypeNode | undefined): FunctionTypeNode; diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index dbb9ac16f83..d8df06e936f 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -2094,7 +2094,8 @@ declare namespace ts { export enum TypePredicateKind { This = 0, Identifier = 1, - Assertion = 2 + AssertsThis = 2, + AssertsIdentifier = 3 } export interface ThisTypePredicate { kind: TypePredicateKind.This; @@ -2108,13 +2109,19 @@ declare namespace ts { parameterIndex: number; type: Type; } - export interface AssertionTypePredicate { - kind: TypePredicateKind.Assertion; + export interface AssertsThisTypePredicate { + kind: TypePredicateKind.AssertsThis; + parameterName: undefined; + parameterIndex: undefined; + type: Type | undefined; + } + export interface AssertsIdentifierTypePredicate { + kind: TypePredicateKind.AssertsIdentifier; parameterName: string; parameterIndex: number; type: Type | undefined; } - export type TypePredicate = ThisTypePredicate | IdentifierTypePredicate | AssertionTypePredicate; + export type TypePredicate = ThisTypePredicate | IdentifierTypePredicate | AssertsThisTypePredicate | AssertsIdentifierTypePredicate; export enum SymbolFlags { None = 0, FunctionScopedVariable = 1, @@ -3843,8 +3850,10 @@ declare namespace ts { function createIndexSignature(decorators: ReadonlyArray | undefined, modifiers: ReadonlyArray | undefined, parameters: ReadonlyArray, type: TypeNode): IndexSignatureDeclaration; function updateIndexSignature(node: IndexSignatureDeclaration, decorators: ReadonlyArray | undefined, modifiers: ReadonlyArray | undefined, parameters: ReadonlyArray, type: TypeNode): IndexSignatureDeclaration; function createKeywordTypeNode(kind: KeywordTypeNode["kind"]): KeywordTypeNode; - function createTypePredicateNode(assertsModifier: AssertsToken | undefined, parameterName: Identifier | ThisTypeNode | string, type: TypeNode | undefined): TypePredicateNode; - function updateTypePredicateNode(node: TypePredicateNode, assertsModifier: AssertsToken | undefined, parameterName: Identifier | ThisTypeNode, type: TypeNode | undefined): TypePredicateNode; + function createTypePredicateNode(parameterName: Identifier | ThisTypeNode | string, type: TypeNode | undefined): TypePredicateNode; + function createTypePredicateNodeWithModifier(assertsModifier: AssertsToken | undefined, parameterName: Identifier | ThisTypeNode | string, type: TypeNode | undefined): TypePredicateNode; + function updateTypePredicateNode(node: TypePredicateNode, parameterName: Identifier | ThisTypeNode, type: TypeNode | undefined): TypePredicateNode; + function updateTypePredicateNodeWithModifier(node: TypePredicateNode, assertsModifier: AssertsToken | undefined, parameterName: Identifier | ThisTypeNode, type: TypeNode | undefined): TypePredicateNode; function createTypeReferenceNode(typeName: string | EntityName, typeArguments: ReadonlyArray | undefined): TypeReferenceNode; function updateTypeReferenceNode(node: TypeReferenceNode, typeName: EntityName, typeArguments: NodeArray | undefined): TypeReferenceNode; function createFunctionTypeNode(typeParameters: ReadonlyArray | undefined, parameters: ReadonlyArray, type: TypeNode | undefined): FunctionTypeNode; From c6e502be7d4682250b7ad8d88eb77f8187a719dc Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Thu, 22 Aug 2019 11:26:26 -0700 Subject: [PATCH 35/97] Verify config file errors --- .../tsserver/projectReferenceErrors.ts | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/testRunner/unittests/tsserver/projectReferenceErrors.ts b/src/testRunner/unittests/tsserver/projectReferenceErrors.ts index 303808cb4e4..62468fcc6f7 100644 --- a/src/testRunner/unittests/tsserver/projectReferenceErrors.ts +++ b/src/testRunner/unittests/tsserver/projectReferenceErrors.ts @@ -152,6 +152,25 @@ fnErr(); }); } + function verifyConfigFileErrors({ openFiles, expectedConfigFileDiagEvents }: VerifyScenario) { + it("verify config file errors", () => { + const host = createServerHost([dependencyTs, dependencyConfig, usageTs, usageConfig, libFile]); + const { session, events } = createSessionWithEventTracking(host, server.ConfigFileDiagEvent); + + for (const file of openFiles()) { + session.executeCommandSeq({ + command: protocol.CommandTypes.Open, + arguments: { file: file.path } + }); + } + + assert.deepEqual(events, expectedConfigFileDiagEvents().map(data => ({ + eventName: server.ConfigFileDiagEvent, + data + }))); + }); + } + interface GetErrDiagnostics { file: File; syntax: protocol.Diagnostic[]; @@ -170,11 +189,13 @@ fnErr(); expectedGetErr: () => readonly GetErrDiagnostics[]; expectedGetErrForProject: () => readonly GetErrForProjectDiagnostics[]; expectedSyncDiagnostics: () => readonly SyncDiagnostics[]; + expectedConfigFileDiagEvents: () => readonly server.ConfigFileDiagEvent["data"][]; } function verifyScenario(scenario: VerifyScenario) { verifyErrorsUsingGeterr(scenario); verifyErrorsUsingGeterrForProject(scenario); verifyErrorsUsingSyncMethods(scenario); + verifyConfigFileErrors(scenario); } function emptyDiagnostics(file: File): GetErrDiagnostics { @@ -243,6 +264,22 @@ fnErr(); return { project, ...diagnostics }; } + function usageConfigDiag(): server.ConfigFileDiagEvent["data"] { + return { + triggerFile: usageTs.path, + configFileName: usageConfig.path, + diagnostics: emptyArray + }; + } + + function dependencyConfigDiag(): server.ConfigFileDiagEvent["data"] { + return { + triggerFile: dependencyTs.path, + configFileName: dependencyConfig.path, + diagnostics: emptyArray + }; + } + describe("when dependency project is not open", () => { verifyScenario({ openFiles: () => [usageTs], @@ -267,6 +304,9 @@ fnErr(); syncDiagnostics(usageDiagnostics(), usageConfig.path), syncDiagnostics(emptyDiagnostics(dependencyTs), usageConfig.path), ], + expectedConfigFileDiagEvents: () => [ + usageConfigDiag() + ], }); }); @@ -290,6 +330,10 @@ fnErr(); syncDiagnostics(emptyDiagnostics(dependencyTs), usageConfig.path), syncDiagnostics(dependencyDiagnostics(), dependencyConfig.path), ], + expectedConfigFileDiagEvents: () => [ + usageConfigDiag(), + dependencyConfigDiag() + ], }); }); }); From 076dde482045de6ff7189d75459a5aab756b4336 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Thu, 22 Aug 2019 12:59:08 -0700 Subject: [PATCH 36/97] Test with --out as well --- .../tsserver/projectReferenceErrors.ts | 399 +++++++++++------- 1 file changed, 244 insertions(+), 155 deletions(-) diff --git a/src/testRunner/unittests/tsserver/projectReferenceErrors.ts b/src/testRunner/unittests/tsserver/projectReferenceErrors.ts index 62468fcc6f7..ac023743da3 100644 --- a/src/testRunner/unittests/tsserver/projectReferenceErrors.ts +++ b/src/testRunner/unittests/tsserver/projectReferenceErrors.ts @@ -3,37 +3,6 @@ namespace ts.projectSystem { const projectLocation = "/user/username/projects/myproject"; const dependecyLocation = `${projectLocation}/dependency`; const usageLocation = `${projectLocation}/usage`; - const dependencyTs: File = { - path: `${dependecyLocation}/fns.ts`, - content: `export function fn1() { } -export function fn2() { } -// Introduce error for fnErr import in main -// export function fnErr() { } -// Error in dependency ts file -export let x: string = 10;` - }; - const dependencyConfig: File = { - path: `${dependecyLocation}/tsconfig.json`, - content: JSON.stringify({ compilerOptions: { composite: true, declarationDir: "../decls" } }) - }; - const usageTs: File = { - path: `${usageLocation}/usage.ts`, - content: `import { - fn1, - fn2, - fnErr -} from '../decls/fns' -fn1(); -fn2(); -fnErr(); -` - }; - const usageConfig: File = { - path: `${usageLocation}/tsconfig.json`, - content: JSON.stringify({ - references: [{ path: "../dependency" }] - }) - }; interface CheckErrorsInFile { session: TestSession; @@ -75,9 +44,9 @@ fnErr(); } } - function verifyErrorsUsingGeterr({ openFiles, expectedGetErr }: VerifyScenario) { + function verifyErrorsUsingGeterr({ allFiles, openFiles, expectedGetErr }: VerifyScenario) { it("verifies the errors in open file", () => { - const host = createServerHost([dependencyTs, dependencyConfig, usageTs, usageConfig, libFile]); + const host = createServerHost([...allFiles(), libFile]); const session = createSession(host, { canUseEvents: true, }); openFilesForSession(openFiles(), session); @@ -96,9 +65,9 @@ fnErr(); }); } - function verifyErrorsUsingGeterrForProject({ openFiles, expectedGetErrForProject }: VerifyScenario) { + function verifyErrorsUsingGeterrForProject({ allFiles, openFiles, expectedGetErrForProject }: VerifyScenario) { it("verifies the errors in projects", () => { - const host = createServerHost([dependencyTs, dependencyConfig, usageTs, usageConfig, libFile]); + const host = createServerHost([...allFiles(), libFile]); const session = createSession(host, { canUseEvents: true, }); openFilesForSession(openFiles(), session); @@ -118,9 +87,9 @@ fnErr(); }); } - function verifyErrorsUsingSyncMethods({ openFiles, expectedSyncDiagnostics }: VerifyScenario) { + function verifyErrorsUsingSyncMethods({ allFiles, openFiles, expectedSyncDiagnostics }: VerifyScenario) { it("verifies the errors using sync commands", () => { - const host = createServerHost([dependencyTs, dependencyConfig, usageTs, usageConfig, libFile]); + const host = createServerHost([...allFiles(), libFile]); const session = createSession(host); openFilesForSession(openFiles(), session); for (const { file, project, syntax, semantic, suggestion } of expectedSyncDiagnostics()) { @@ -152,9 +121,9 @@ fnErr(); }); } - function verifyConfigFileErrors({ openFiles, expectedConfigFileDiagEvents }: VerifyScenario) { + function verifyConfigFileErrors({ allFiles, openFiles, expectedConfigFileDiagEvents }: VerifyScenario) { it("verify config file errors", () => { - const host = createServerHost([dependencyTs, dependencyConfig, usageTs, usageConfig, libFile]); + const host = createServerHost([...allFiles(), libFile]); const { session, events } = createSessionWithEventTracking(host, server.ConfigFileDiagEvent); for (const file of openFiles()) { @@ -185,6 +154,7 @@ fnErr(); project?: string; } interface VerifyScenario { + allFiles: () => readonly File[]; openFiles: () => readonly File[]; expectedGetErr: () => readonly GetErrDiagnostics[]; expectedGetErrForProject: () => readonly GetErrForProjectDiagnostics[]; @@ -207,133 +177,252 @@ fnErr(); }; } - function usageDiagnostics(): GetErrDiagnostics { - return { - file: usageTs, - syntax: emptyArray, - semantic: [ - createDiagnostic( - { line: 4, offset: 5 }, - { line: 4, offset: 10 }, - Diagnostics.Module_0_has_no_exported_member_1, - [`"../dependency/fns"`, "fnErr"], - "error", - ) - ], - suggestion: emptyArray - }; - } - - function dependencyDiagnostics(): GetErrDiagnostics { - return { - file: dependencyTs, - syntax: emptyArray, - semantic: [ - createDiagnostic( - { line: 6, offset: 12 }, - { line: 6, offset: 13 }, - Diagnostics.Type_0_is_not_assignable_to_type_1, - ["10", "string"], - "error", - ) - ], - suggestion: emptyArray - }; - } - - function usageProjectDiagnostics(): GetErrForProjectDiagnostics { - return { - project: usageTs.path, - errors: [ - usageDiagnostics(), - emptyDiagnostics(dependencyTs) - ] - }; - } - - function dependencyProjectDiagnostics(): GetErrForProjectDiagnostics { - return { - project: dependencyTs.path, - errors: [ - dependencyDiagnostics() - ] - }; - } - function syncDiagnostics(diagnostics: GetErrDiagnostics, project: string): SyncDiagnostics { return { project, ...diagnostics }; } - function usageConfigDiag(): server.ConfigFileDiagEvent["data"] { - return { - triggerFile: usageTs.path, - configFileName: usageConfig.path, - diagnostics: emptyArray - }; + interface VerifyUsageAndDependency { + allFiles: readonly [File, File, File, File]; // dependencyTs, dependencyConfig, usageTs, usageConfig + usageDiagnostics(): GetErrDiagnostics; + dependencyDiagnostics(): GetErrDiagnostics; + + } + function verifyUsageAndDependency({ allFiles, usageDiagnostics, dependencyDiagnostics }: VerifyUsageAndDependency) { + const [dependencyTs, dependencyConfig, usageTs, usageConfig] = allFiles; + function usageProjectDiagnostics(): GetErrForProjectDiagnostics { + return { + project: usageTs.path, + errors: [ + usageDiagnostics(), + emptyDiagnostics(dependencyTs) + ] + }; + } + + function dependencyProjectDiagnostics(): GetErrForProjectDiagnostics { + return { + project: dependencyTs.path, + errors: [ + dependencyDiagnostics() + ] + }; + } + + function usageConfigDiag(): server.ConfigFileDiagEvent["data"] { + return { + triggerFile: usageTs.path, + configFileName: usageConfig.path, + diagnostics: emptyArray + }; + } + + function dependencyConfigDiag(): server.ConfigFileDiagEvent["data"] { + return { + triggerFile: dependencyTs.path, + configFileName: dependencyConfig.path, + diagnostics: emptyArray + }; + } + + describe("when dependency project is not open", () => { + verifyScenario({ + allFiles: () => allFiles, + openFiles: () => [usageTs], + expectedGetErr: () => [ + usageDiagnostics() + ], + expectedGetErrForProject: () => [ + usageProjectDiagnostics(), + { + project: dependencyTs.path, + errors: [ + emptyDiagnostics(dependencyTs), + usageDiagnostics() + ] + } + ], + expectedSyncDiagnostics: () => [ + // Without project + usageDiagnostics(), + emptyDiagnostics(dependencyTs), + // With project + syncDiagnostics(usageDiagnostics(), usageConfig.path), + syncDiagnostics(emptyDiagnostics(dependencyTs), usageConfig.path), + ], + expectedConfigFileDiagEvents: () => [ + usageConfigDiag() + ], + }); + }); + + describe("when the depedency file is open", () => { + verifyScenario({ + allFiles: () => allFiles, + openFiles: () => [usageTs, dependencyTs], + expectedGetErr: () => [ + usageDiagnostics(), + dependencyDiagnostics(), + ], + expectedGetErrForProject: () => [ + usageProjectDiagnostics(), + dependencyProjectDiagnostics() + ], + expectedSyncDiagnostics: () => [ + // Without project + usageDiagnostics(), + dependencyDiagnostics(), + // With project + syncDiagnostics(usageDiagnostics(), usageConfig.path), + syncDiagnostics(emptyDiagnostics(dependencyTs), usageConfig.path), + syncDiagnostics(dependencyDiagnostics(), dependencyConfig.path), + ], + expectedConfigFileDiagEvents: () => [ + usageConfigDiag(), + dependencyConfigDiag() + ], + }); + }); } - function dependencyConfigDiag(): server.ConfigFileDiagEvent["data"] { - return { - triggerFile: dependencyTs.path, - configFileName: dependencyConfig.path, - diagnostics: emptyArray + describe("with module scenario", () => { + const dependencyTs: File = { + path: `${dependecyLocation}/fns.ts`, + content: `export function fn1() { } +export function fn2() { } +// Introduce error for fnErr import in main +// export function fnErr() { } +// Error in dependency ts file +export let x: string = 10;` }; - } + const dependencyConfig: File = { + path: `${dependecyLocation}/tsconfig.json`, + content: JSON.stringify({ compilerOptions: { composite: true, declarationDir: "../decls" } }) + }; + const usageTs: File = { + path: `${usageLocation}/usage.ts`, + content: `import { + fn1, + fn2, + fnErr +} from '../decls/fns' +fn1(); +fn2(); +fnErr(); +` + }; + const usageConfig: File = { + path: `${usageLocation}/tsconfig.json`, + content: JSON.stringify({ + references: [{ path: "../dependency" }] + }) + }; + function usageDiagnostics(): GetErrDiagnostics { + return { + file: usageTs, + syntax: emptyArray, + semantic: [ + createDiagnostic( + { line: 4, offset: 5 }, + { line: 4, offset: 10 }, + Diagnostics.Module_0_has_no_exported_member_1, + [`"../dependency/fns"`, "fnErr"], + "error", + ) + ], + suggestion: emptyArray + }; + } - describe("when dependency project is not open", () => { - verifyScenario({ - openFiles: () => [usageTs], - expectedGetErr: () => [ - usageDiagnostics() - ], - expectedGetErrForProject: () => [ - usageProjectDiagnostics(), - { - project: dependencyTs.path, - errors: [ - emptyDiagnostics(dependencyTs), - usageDiagnostics() - ] - } - ], - expectedSyncDiagnostics: () => [ - // Without project - usageDiagnostics(), - emptyDiagnostics(dependencyTs), - // With project - syncDiagnostics(usageDiagnostics(), usageConfig.path), - syncDiagnostics(emptyDiagnostics(dependencyTs), usageConfig.path), - ], - expectedConfigFileDiagEvents: () => [ - usageConfigDiag() - ], + function dependencyDiagnostics(): GetErrDiagnostics { + return { + file: dependencyTs, + syntax: emptyArray, + semantic: [ + createDiagnostic( + { line: 6, offset: 12 }, + { line: 6, offset: 13 }, + Diagnostics.Type_0_is_not_assignable_to_type_1, + ["10", "string"], + "error", + ) + ], + suggestion: emptyArray + }; + } + + verifyUsageAndDependency({ + allFiles: [dependencyTs, dependencyConfig, usageTs, usageConfig], + usageDiagnostics, + dependencyDiagnostics }); }); - describe("when the depedency file is open", () => { - verifyScenario({ - openFiles: () => [usageTs, dependencyTs], - expectedGetErr: () => [ - usageDiagnostics(), - dependencyDiagnostics(), - ], - expectedGetErrForProject: () => [ - usageProjectDiagnostics(), - dependencyProjectDiagnostics() - ], - expectedSyncDiagnostics: () => [ - // Without project - usageDiagnostics(), - dependencyDiagnostics(), - // With project - syncDiagnostics(usageDiagnostics(), usageConfig.path), - syncDiagnostics(emptyDiagnostics(dependencyTs), usageConfig.path), - syncDiagnostics(dependencyDiagnostics(), dependencyConfig.path), - ], - expectedConfigFileDiagEvents: () => [ - usageConfigDiag(), - dependencyConfigDiag() - ], + describe("with non module --out", () => { + const dependencyTs: File = { + path: `${dependecyLocation}/fns.ts`, + content: `function fn1() { } +function fn2() { } +// Introduce error for fnErr import in main +// function fnErr() { } +// Error in dependency ts file +let x: string = 10;` + }; + const dependencyConfig: File = { + path: `${dependecyLocation}/tsconfig.json`, + content: JSON.stringify({ compilerOptions: { composite: true, outFile: "../dependency.js" } }) + }; + const usageTs: File = { + path: `${usageLocation}/usage.ts`, + content: `fn1(); +fn2(); +fnErr(); +` + }; + const usageConfig: File = { + path: `${usageLocation}/tsconfig.json`, + content: JSON.stringify({ + compilerOptions: { outFile: "../usage.js" }, + references: [{ path: "../dependency" }] + }) + }; + function usageDiagnostics(): GetErrDiagnostics { + return { + file: usageTs, + syntax: emptyArray, + semantic: [ + createDiagnostic( + { line: 3, offset: 1 }, + { line: 3, offset: 6 }, + Diagnostics.Cannot_find_name_0, + ["fnErr"], + "error", + ) + ], + suggestion: emptyArray + }; + } + + function dependencyDiagnostics(): GetErrDiagnostics { + return { + file: dependencyTs, + syntax: emptyArray, + semantic: [ + createDiagnostic( + { line: 6, offset: 5 }, + { line: 6, offset: 6 }, + Diagnostics.Type_0_is_not_assignable_to_type_1, + ["10", "string"], + "error", + ) + ], + suggestion: emptyArray + }; + } + + verifyUsageAndDependency({ + allFiles: [dependencyTs, dependencyConfig, usageTs, usageConfig], + usageDiagnostics, + dependencyDiagnostics }); }); }); From a469fd82b9f33071841da9c2a38237915c105c11 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Thu, 22 Aug 2019 13:14:50 -0700 Subject: [PATCH 37/97] Should not report that files are not part of config for files that are not going to be emitted --- src/compiler/program.ts | 11 ++++++++--- .../unittests/tsserver/projectReferenceErrors.ts | 3 ++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 62f003910ad..45918e26e36 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -1008,9 +1008,15 @@ namespace ts { return ts.toPath(fileName, currentDirectory, getCanonicalFileName); } + function isValidSourceFileForEmit(file: SourceFile) { + // source file is allowed to be emitted and its not source of project reference redirect + return sourceFileMayBeEmitted(file, options, isSourceFileFromExternalLibrary, getResolvedProjectReferenceToRedirect) && + !isSourceOfProjectReferenceRedirect(file.fileName); + } + function getCommonSourceDirectory() { if (commonSourceDirectory === undefined) { - const emittedFiles = filter(files, file => sourceFileMayBeEmitted(file, options, isSourceFileFromExternalLibrary, getResolvedProjectReferenceToRedirect)); + const emittedFiles = filter(files, file => isValidSourceFileForEmit(file)); if (options.rootDir && checkSourceFilesBelongToPath(emittedFiles, options.rootDir)) { // If a rootDir is specified use it as the commonSourceDirectory commonSourceDirectory = getNormalizedAbsolutePath(options.rootDir, currentDirectory); @@ -2933,8 +2939,7 @@ namespace ts { const rootPaths = arrayToSet(rootNames, toPath); for (const file of files) { // Ignore file that is not emitted - if (!sourceFileMayBeEmitted(file, options, isSourceFileFromExternalLibrary, getResolvedProjectReferenceToRedirect)) continue; - if (!rootPaths.has(file.path)) { + if (isValidSourceFileForEmit(file) && !rootPaths.has(file.path)) { addProgramDiagnosticAtRefPath( file, rootPaths, diff --git a/src/testRunner/unittests/tsserver/projectReferenceErrors.ts b/src/testRunner/unittests/tsserver/projectReferenceErrors.ts index ac023743da3..a3ec4848615 100644 --- a/src/testRunner/unittests/tsserver/projectReferenceErrors.ts +++ b/src/testRunner/unittests/tsserver/projectReferenceErrors.ts @@ -313,6 +313,7 @@ fnErr(); const usageConfig: File = { path: `${usageLocation}/tsconfig.json`, content: JSON.stringify({ + compilerOptions: { composite: true }, references: [{ path: "../dependency" }] }) }; @@ -381,7 +382,7 @@ fnErr(); const usageConfig: File = { path: `${usageLocation}/tsconfig.json`, content: JSON.stringify({ - compilerOptions: { outFile: "../usage.js" }, + compilerOptions: { composite: true, outFile: "../usage.js" }, references: [{ path: "../dependency" }] }) }; From 3be6e75d6aee0c2a0a3be1b42ab84aa8c5c2e8ac Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Mon, 26 Aug 2019 11:13:17 -0700 Subject: [PATCH 38/97] Improve names in infer-from-usage Basically, drop "Context" from all names, because it just indicates that it's an implementation of the State monad. --- src/services/codefixes/inferFromUsage.ts | 238 +++++++++++------------ 1 file changed, 119 insertions(+), 119 deletions(-) diff --git a/src/services/codefixes/inferFromUsage.ts b/src/services/codefixes/inferFromUsage.ts index 396d7f44fa8..417ed5c625c 100644 --- a/src/services/codefixes/inferFromUsage.ts +++ b/src/services/codefixes/inferFromUsage.ts @@ -359,7 +359,7 @@ namespace ts.codefix { const references = getReferences(token, program, cancellationToken); const checker = program.getTypeChecker(); const types = InferFromReference.inferTypesFromReferences(references, checker, cancellationToken); - return InferFromReference.unifyFromContext(types, checker); + return InferFromReference.unifyFromUsage(types, checker); } function inferFunctionReferencesFromUsage(containingFunction: FunctionLike, sourceFile: SourceFile, program: Program, cancellationToken: CancellationToken): ReadonlyArray | undefined { @@ -395,33 +395,33 @@ namespace ts.codefix { } namespace InferFromReference { - interface CallContext { + interface CallUsage { argumentTypes: Type[]; - returnType: UsageContext; + returnType: Usage; } - interface UsageContext { + interface Usage { isNumber?: boolean; isString?: boolean; /** Used ambiguously, eg x + ___ or object[___]; results in string | number if no other evidence exists */ isNumberOrString?: boolean; candidateTypes?: Type[]; - properties?: UnderscoreEscapedMap; - callContexts?: CallContext[]; - constructContexts?: CallContext[]; - numberIndexContext?: UsageContext; - stringIndexContext?: UsageContext; + properties?: UnderscoreEscapedMap; + calls?: CallUsage[]; + constructs?: CallUsage[]; + numberIndex?: Usage; + stringIndex?: Usage; candidateThisTypes?: Type[]; } export function inferTypesFromReferences(references: ReadonlyArray, checker: TypeChecker, cancellationToken: CancellationToken): Type[] { - const usageContext: UsageContext = {}; + const usage: Usage = {}; for (const reference of references) { cancellationToken.throwIfCancellationRequested(); - inferTypeFromContext(reference, checker, usageContext); + calculateUsageOfNode(reference, checker, usage); } - return inferFromContext(usageContext, checker); + return inferFromUsage(usage, checker); } export function inferTypeForParametersFromReferences(references: ReadonlyArray | undefined, declaration: FunctionLike, program: Program, cancellationToken: CancellationToken): ParameterInference[] | undefined { @@ -430,35 +430,35 @@ namespace ts.codefix { } const checker = program.getTypeChecker(); - const usageContext: UsageContext = {}; + const usage: Usage = {}; for (const reference of references) { cancellationToken.throwIfCancellationRequested(); - inferTypeFromContext(reference, checker, usageContext); + calculateUsageOfNode(reference, checker, usage); } - const callContexts = [...usageContext.constructContexts || [], ...usageContext.callContexts || []]; + const calls = [...usage.constructs || [], ...usage.calls || []]; return declaration.parameters.map((parameter, parameterIndex): ParameterInference => { const types = []; const isRest = isRestParameter(parameter); let isOptional = false; - for (const callContext of callContexts) { - if (callContext.argumentTypes.length <= parameterIndex) { + for (const call of calls) { + if (call.argumentTypes.length <= parameterIndex) { isOptional = isInJSFile(declaration); types.push(checker.getUndefinedType()); } else if (isRest) { - for (let i = parameterIndex; i < callContext.argumentTypes.length; i++) { - types.push(checker.getBaseTypeOfLiteralType(callContext.argumentTypes[i])); + for (let i = parameterIndex; i < call.argumentTypes.length; i++) { + types.push(checker.getBaseTypeOfLiteralType(call.argumentTypes[i])); } } else { - types.push(checker.getBaseTypeOfLiteralType(callContext.argumentTypes[parameterIndex])); + types.push(checker.getBaseTypeOfLiteralType(call.argumentTypes[parameterIndex])); } } if (isIdentifier(parameter.name)) { const inferred = inferTypesFromReferences(getReferences(parameter.name, program, cancellationToken), checker, cancellationToken); types.push(...(isRest ? mapDefined(inferred, checker.getElementTypeOfArrayType) : inferred)); } - const type = unifyFromContext(types, checker); + const type = unifyFromUsage(types, checker); return { type: isRest ? checker.createArrayType(type) : type, isOptional: isOptional && !isRest, @@ -473,89 +473,89 @@ namespace ts.codefix { } const checker = program.getTypeChecker(); - const usageContext: UsageContext = {}; + const usage: Usage = {}; for (const reference of references) { cancellationToken.throwIfCancellationRequested(); - inferTypeFromContext(reference, checker, usageContext); + calculateUsageOfNode(reference, checker, usage); } - return unifyFromContext(usageContext.candidateThisTypes || emptyArray, checker); + return unifyFromUsage(usage.candidateThisTypes || emptyArray, checker); } - function inferTypeFromContext(node: Expression, checker: TypeChecker, usageContext: UsageContext): void { + function calculateUsageOfNode(node: Expression, checker: TypeChecker, usage: Usage): void { while (isRightSideOfQualifiedNameOrPropertyAccess(node)) { node = node.parent; } switch (node.parent.kind) { case SyntaxKind.PostfixUnaryExpression: - usageContext.isNumber = true; + usage.isNumber = true; break; case SyntaxKind.PrefixUnaryExpression: - inferTypeFromPrefixUnaryExpressionContext(node.parent, usageContext); + inferTypeFromPrefixUnaryExpression(node.parent, usage); break; case SyntaxKind.BinaryExpression: - inferTypeFromBinaryExpressionContext(node, node.parent, checker, usageContext); + inferTypeFromBinaryExpression(node, node.parent, checker, usage); break; case SyntaxKind.CaseClause: case SyntaxKind.DefaultClause: - inferTypeFromSwitchStatementLabelContext(node.parent, checker, usageContext); + inferTypeFromSwitchStatementLabel(node.parent, checker, usage); break; case SyntaxKind.CallExpression: case SyntaxKind.NewExpression: if ((node.parent).expression === node) { - inferTypeFromCallExpressionContext(node.parent, checker, usageContext); + inferTypeFromCallExpression(node.parent, checker, usage); } else { - inferTypeFromContextualType(node, checker, usageContext); + inferTypeFromContextualType(node, checker, usage); } break; case SyntaxKind.PropertyAccessExpression: - inferTypeFromPropertyAccessExpressionContext(node.parent, checker, usageContext); + inferTypeFromPropertyAccessExpression(node.parent, checker, usage); break; case SyntaxKind.ElementAccessExpression: - inferTypeFromPropertyElementExpressionContext(node.parent, node, checker, usageContext); + inferTypeFromPropertyElementExpression(node.parent, node, checker, usage); break; case SyntaxKind.PropertyAssignment: case SyntaxKind.ShorthandPropertyAssignment: - inferTypeFromPropertyAssignment(node.parent, checker, usageContext); + inferTypeFromPropertyAssignment(node.parent, checker, usage); break; case SyntaxKind.PropertyDeclaration: - inferTypeFromPropertyDeclaration(node.parent, checker, usageContext); + inferTypeFromPropertyDeclaration(node.parent, checker, usage); break; case SyntaxKind.VariableDeclaration: { const { name, initializer } = node.parent as VariableDeclaration; if (node === name) { if (initializer) { // This can happen for `let x = null;` which still has an implicit-any error. - addCandidateType(usageContext, checker.getTypeAtLocation(initializer)); + addCandidateType(usage, checker.getTypeAtLocation(initializer)); } break; } } // falls through default: - return inferTypeFromContextualType(node, checker, usageContext); + return inferTypeFromContextualType(node, checker, usage); } } - function inferTypeFromContextualType(node: Expression, checker: TypeChecker, usageContext: UsageContext): void { + function inferTypeFromContextualType(node: Expression, checker: TypeChecker, usage: Usage): void { if (isExpressionNode(node)) { - addCandidateType(usageContext, checker.getContextualType(node)); + addCandidateType(usage, checker.getContextualType(node)); } } - function inferTypeFromPrefixUnaryExpressionContext(node: PrefixUnaryExpression, usageContext: UsageContext): void { + function inferTypeFromPrefixUnaryExpression(node: PrefixUnaryExpression, usage: Usage): void { switch (node.operator) { case SyntaxKind.PlusPlusToken: case SyntaxKind.MinusMinusToken: case SyntaxKind.MinusToken: case SyntaxKind.TildeToken: - usageContext.isNumber = true; + usage.isNumber = true; break; case SyntaxKind.PlusToken: - usageContext.isNumberOrString = true; + usage.isNumberOrString = true; break; // case SyntaxKind.ExclamationToken: @@ -563,7 +563,7 @@ namespace ts.codefix { } } - function inferTypeFromBinaryExpressionContext(node: Expression, parent: BinaryExpression, checker: TypeChecker, usageContext: UsageContext): void { + function inferTypeFromBinaryExpression(node: Expression, parent: BinaryExpression, checker: TypeChecker, usage: Usage): void { switch (parent.operatorToken.kind) { // ExponentiationOperator case SyntaxKind.AsteriskAsteriskToken: @@ -606,10 +606,10 @@ namespace ts.codefix { case SyntaxKind.GreaterThanEqualsToken: const operandType = checker.getTypeAtLocation(parent.left === node ? parent.right : parent.left); if (operandType.flags & TypeFlags.EnumLike) { - addCandidateType(usageContext, operandType); + addCandidateType(usage, operandType); } else { - usageContext.isNumber = true; + usage.isNumber = true; } break; @@ -617,16 +617,16 @@ namespace ts.codefix { case SyntaxKind.PlusToken: const otherOperandType = checker.getTypeAtLocation(parent.left === node ? parent.right : parent.left); if (otherOperandType.flags & TypeFlags.EnumLike) { - addCandidateType(usageContext, otherOperandType); + addCandidateType(usage, otherOperandType); } else if (otherOperandType.flags & TypeFlags.NumberLike) { - usageContext.isNumber = true; + usage.isNumber = true; } else if (otherOperandType.flags & TypeFlags.StringLike) { - usageContext.isString = true; + usage.isString = true; } else { - usageContext.isNumberOrString = true; + usage.isNumberOrString = true; } break; @@ -636,12 +636,12 @@ namespace ts.codefix { case SyntaxKind.EqualsEqualsEqualsToken: case SyntaxKind.ExclamationEqualsEqualsToken: case SyntaxKind.ExclamationEqualsToken: - addCandidateType(usageContext, checker.getTypeAtLocation(parent.left === node ? parent.right : parent.left)); + addCandidateType(usage, checker.getTypeAtLocation(parent.left === node ? parent.right : parent.left)); break; case SyntaxKind.InKeyword: if (node === parent.left) { - usageContext.isString = true; + usage.isString = true; } break; @@ -651,7 +651,7 @@ namespace ts.codefix { (node.parent.parent.kind === SyntaxKind.VariableDeclaration || isAssignmentExpression(node.parent.parent, /*excludeCompoundAssignment*/ true))) { // var x = x || {}; // TODO: use getFalsyflagsOfType - addCandidateType(usageContext, checker.getTypeAtLocation(parent.right)); + addCandidateType(usage, checker.getTypeAtLocation(parent.right)); } break; @@ -663,68 +663,68 @@ namespace ts.codefix { } } - function inferTypeFromSwitchStatementLabelContext(parent: CaseOrDefaultClause, checker: TypeChecker, usageContext: UsageContext): void { - addCandidateType(usageContext, checker.getTypeAtLocation(parent.parent.parent.expression)); + function inferTypeFromSwitchStatementLabel(parent: CaseOrDefaultClause, checker: TypeChecker, usage: Usage): void { + addCandidateType(usage, checker.getTypeAtLocation(parent.parent.parent.expression)); } - function inferTypeFromCallExpressionContext(parent: CallExpression | NewExpression, checker: TypeChecker, usageContext: UsageContext): void { - const callContext: CallContext = { + function inferTypeFromCallExpression(parent: CallExpression | NewExpression, checker: TypeChecker, usage: Usage): void { + const call: CallUsage = { argumentTypes: [], returnType: {} }; if (parent.arguments) { for (const argument of parent.arguments) { - callContext.argumentTypes.push(checker.getTypeAtLocation(argument)); + call.argumentTypes.push(checker.getTypeAtLocation(argument)); } } - inferTypeFromContext(parent, checker, callContext.returnType); + calculateUsageOfNode(parent, checker, call.returnType); if (parent.kind === SyntaxKind.CallExpression) { - (usageContext.callContexts || (usageContext.callContexts = [])).push(callContext); + (usage.calls || (usage.calls = [])).push(call); } else { - (usageContext.constructContexts || (usageContext.constructContexts = [])).push(callContext); + (usage.constructs || (usage.constructs = [])).push(call); } } - function inferTypeFromPropertyAccessExpressionContext(parent: PropertyAccessExpression, checker: TypeChecker, usageContext: UsageContext): void { + function inferTypeFromPropertyAccessExpression(parent: PropertyAccessExpression, checker: TypeChecker, usage: Usage): void { const name = escapeLeadingUnderscores(parent.name.text); - if (!usageContext.properties) { - usageContext.properties = createUnderscoreEscapedMap(); + if (!usage.properties) { + usage.properties = createUnderscoreEscapedMap(); } - const propertyUsageContext = usageContext.properties.get(name) || { }; - inferTypeFromContext(parent, checker, propertyUsageContext); - usageContext.properties.set(name, propertyUsageContext); + const propertyUsage = usage.properties.get(name) || { }; + calculateUsageOfNode(parent, checker, propertyUsage); + usage.properties.set(name, propertyUsage); } - function inferTypeFromPropertyElementExpressionContext(parent: ElementAccessExpression, node: Expression, checker: TypeChecker, usageContext: UsageContext): void { + function inferTypeFromPropertyElementExpression(parent: ElementAccessExpression, node: Expression, checker: TypeChecker, usage: Usage): void { if (node === parent.argumentExpression) { - usageContext.isNumberOrString = true; + usage.isNumberOrString = true; return; } else { const indexType = checker.getTypeAtLocation(parent.argumentExpression); - const indexUsageContext = {}; - inferTypeFromContext(parent, checker, indexUsageContext); + const indexUsage = {}; + calculateUsageOfNode(parent, checker, indexUsage); if (indexType.flags & TypeFlags.NumberLike) { - usageContext.numberIndexContext = indexUsageContext; + usage.numberIndex = indexUsage; } else { - usageContext.stringIndexContext = indexUsageContext; + usage.stringIndex = indexUsage; } } } - function inferTypeFromPropertyAssignment(assignment: PropertyAssignment | ShorthandPropertyAssignment, checker: TypeChecker, usageContext: UsageContext) { + function inferTypeFromPropertyAssignment(assignment: PropertyAssignment | ShorthandPropertyAssignment, checker: TypeChecker, usage: Usage) { const nodeWithRealType = isVariableDeclaration(assignment.parent.parent) ? assignment.parent.parent : assignment.parent; - addCandidateThisType(usageContext, checker.getTypeAtLocation(nodeWithRealType)); + addCandidateThisType(usage, checker.getTypeAtLocation(nodeWithRealType)); } - function inferTypeFromPropertyDeclaration(declaration: PropertyDeclaration, checker: TypeChecker, usageContext: UsageContext) { - addCandidateThisType(usageContext, checker.getTypeAtLocation(declaration.parent)); + function inferTypeFromPropertyDeclaration(declaration: PropertyDeclaration, checker: TypeChecker, usage: Usage) { + addCandidateThisType(usage, checker.getTypeAtLocation(declaration.parent)); } interface Priority { @@ -745,7 +745,7 @@ namespace ts.codefix { return inferences.filter(i => toRemove.every(f => !f(i))); } - export function unifyFromContext(inferences: ReadonlyArray, checker: TypeChecker, fallback = checker.getAnyType()): Type { + export function unifyFromUsage(inferences: ReadonlyArray, checker: TypeChecker, fallback = checker.getAnyType()): Type { if (!inferences.length) return fallback; // 1. string or number individually override string | number @@ -815,82 +815,82 @@ namespace ts.codefix { numberIndices.length ? checker.createIndexInfo(checker.getUnionType(numberIndices), numberIndexReadonly) : undefined); } - function inferFromContext(usageContext: UsageContext, checker: TypeChecker) { + function inferFromUsage(usage: Usage, checker: TypeChecker) { const types = []; - if (usageContext.isNumber) { + if (usage.isNumber) { types.push(checker.getNumberType()); } - if (usageContext.isString) { + if (usage.isString) { types.push(checker.getStringType()); } - if (usageContext.isNumberOrString) { + if (usage.isNumberOrString) { types.push(checker.getUnionType([checker.getStringType(), checker.getNumberType()])); } - types.push(...(usageContext.candidateTypes || []).map(t => checker.getBaseTypeOfLiteralType(t))); + types.push(...(usage.candidateTypes || []).map(t => checker.getBaseTypeOfLiteralType(t))); - if (usageContext.properties && hasCallContext(usageContext.properties.get("then" as __String))) { - const paramType = getParameterTypeFromCallContexts(0, usageContext.properties.get("then" as __String)!.callContexts!, /*isRestParameter*/ false, checker)!; // TODO: GH#18217 + if (usage.properties && hasCalls(usage.properties.get("then" as __String))) { + const paramType = getParameterTypeFromCalls(0, usage.properties.get("then" as __String)!.calls!, /*isRestParameter*/ false, checker)!; // TODO: GH#18217 const types = paramType.getCallSignatures().map(c => c.getReturnType()); types.push(checker.createPromiseType(types.length ? checker.getUnionType(types, UnionReduction.Subtype) : checker.getAnyType())); } - else if (usageContext.properties && hasCallContext(usageContext.properties.get("push" as __String))) { - types.push(checker.createArrayType(getParameterTypeFromCallContexts(0, usageContext.properties.get("push" as __String)!.callContexts!, /*isRestParameter*/ false, checker)!)); + else if (usage.properties && hasCalls(usage.properties.get("push" as __String))) { + types.push(checker.createArrayType(getParameterTypeFromCalls(0, usage.properties.get("push" as __String)!.calls!, /*isRestParameter*/ false, checker)!)); } - if (usageContext.numberIndexContext) { - types.push(checker.createArrayType(recur(usageContext.numberIndexContext))); + if (usage.numberIndex) { + types.push(checker.createArrayType(recur(usage.numberIndex))); } - else if (usageContext.properties || usageContext.callContexts || usageContext.constructContexts || usageContext.stringIndexContext) { + else if (usage.properties || usage.calls || usage.constructs || usage.stringIndex) { const members = createUnderscoreEscapedMap(); const callSignatures: Signature[] = []; const constructSignatures: Signature[] = []; let stringIndexInfo: IndexInfo | undefined; - if (usageContext.properties) { - usageContext.properties.forEach((context, name) => { + if (usage.properties) { + usage.properties.forEach((u, name) => { const symbol = checker.createSymbol(SymbolFlags.Property, name); - symbol.type = recur(context); + symbol.type = recur(u); members.set(name, symbol); }); } - if (usageContext.callContexts) { - for (const callContext of usageContext.callContexts) { - callSignatures.push(getSignatureFromCallContext(callContext, checker)); + if (usage.calls) { + for (const call of usage.calls) { + callSignatures.push(getSignatureFromCall(call, checker)); } } - if (usageContext.constructContexts) { - for (const constructContext of usageContext.constructContexts) { - constructSignatures.push(getSignatureFromCallContext(constructContext, checker)); + if (usage.constructs) { + for (const construct of usage.constructs) { + constructSignatures.push(getSignatureFromCall(construct, checker)); } } - if (usageContext.stringIndexContext) { - stringIndexInfo = checker.createIndexInfo(recur(usageContext.stringIndexContext), /*isReadonly*/ false); + if (usage.stringIndex) { + stringIndexInfo = checker.createIndexInfo(recur(usage.stringIndex), /*isReadonly*/ false); } types.push(checker.createAnonymousType(/*symbol*/ undefined!, members, callSignatures, constructSignatures, stringIndexInfo, /*numberIndexInfo*/ undefined)); // TODO: GH#18217 } return types; - function recur(innerContext: UsageContext): Type { - return unifyFromContext(inferFromContext(innerContext, checker), checker); + function recur(innerUsage: Usage): Type { + return unifyFromUsage(inferFromUsage(innerUsage, checker), checker); } } - function getParameterTypeFromCallContexts(parameterIndex: number, callContexts: CallContext[], isRestParameter: boolean, checker: TypeChecker) { + function getParameterTypeFromCalls(parameterIndex: number, calls: CallUsage[], isRestParameter: boolean, checker: TypeChecker) { let types: Type[] = []; - if (callContexts) { - for (const callContext of callContexts) { - if (callContext.argumentTypes.length > parameterIndex) { + if (calls) { + for (const call of calls) { + if (call.argumentTypes.length > parameterIndex) { if (isRestParameter) { - types = concatenate(types, map(callContext.argumentTypes.slice(parameterIndex), a => checker.getBaseTypeOfLiteralType(a))); + types = concatenate(types, map(call.argumentTypes.slice(parameterIndex), a => checker.getBaseTypeOfLiteralType(a))); } else { - types.push(checker.getBaseTypeOfLiteralType(callContext.argumentTypes[parameterIndex])); + types.push(checker.getBaseTypeOfLiteralType(call.argumentTypes[parameterIndex])); } } } @@ -903,32 +903,32 @@ namespace ts.codefix { return undefined; } - function getSignatureFromCallContext(callContext: CallContext, checker: TypeChecker): Signature { + function getSignatureFromCall(call: CallUsage, checker: TypeChecker): Signature { const parameters: Symbol[] = []; - for (let i = 0; i < callContext.argumentTypes.length; i++) { + for (let i = 0; i < call.argumentTypes.length; i++) { const symbol = checker.createSymbol(SymbolFlags.FunctionScopedVariable, escapeLeadingUnderscores(`arg${i}`)); - symbol.type = checker.getWidenedType(checker.getBaseTypeOfLiteralType(callContext.argumentTypes[i])); + symbol.type = checker.getWidenedType(checker.getBaseTypeOfLiteralType(call.argumentTypes[i])); parameters.push(symbol); } - const returnType = unifyFromContext(inferFromContext(callContext.returnType, checker), checker, checker.getVoidType()); + const returnType = unifyFromUsage(inferFromUsage(call.returnType, checker), checker, checker.getVoidType()); // TODO: GH#18217 - return checker.createSignature(/*declaration*/ undefined!, /*typeParameters*/ undefined, /*thisParameter*/ undefined, parameters, returnType, /*typePredicate*/ undefined, callContext.argumentTypes.length, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false); + return checker.createSignature(/*declaration*/ undefined!, /*typeParameters*/ undefined, /*thisParameter*/ undefined, parameters, returnType, /*typePredicate*/ undefined, call.argumentTypes.length, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false); } - function addCandidateType(context: UsageContext, type: Type | undefined) { + function addCandidateType(usage: Usage, type: Type | undefined) { if (type && !(type.flags & TypeFlags.Any) && !(type.flags & TypeFlags.Never)) { - (context.candidateTypes || (context.candidateTypes = [])).push(type); + (usage.candidateTypes || (usage.candidateTypes = [])).push(type); } } - function addCandidateThisType(context: UsageContext, type: Type | undefined) { + function addCandidateThisType(usage: Usage, type: Type | undefined) { if (type && !(type.flags & TypeFlags.Any) && !(type.flags & TypeFlags.Never)) { - (context.candidateThisTypes || (context.candidateThisTypes = [])).push(type); + (usage.candidateThisTypes || (usage.candidateThisTypes = [])).push(type); } } - function hasCallContext(usageContext: UsageContext | undefined): boolean { - return !!usageContext && !!usageContext.callContexts; + function hasCalls(usage: Usage | undefined): boolean { + return !!usage && !!usage.calls; } } } From d347b08a42bb705415fe8b859a69697c1d155a59 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Tue, 27 Aug 2019 13:13:58 -0700 Subject: [PATCH 39/97] Copied from old branch 1. Everything explodes! Out of stack space! 2. Results aren't used yet. 3. But call and construct use the new getSignatureFromCalls, so I expect some baseline changes after I get the infinite recursion fixed. --- src/compiler/checker.ts | 3 + src/compiler/types.ts | 1 + src/services/codefixes/inferFromUsage.ts | 85 ++++++++++++++++++++---- 3 files changed, 75 insertions(+), 14 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index cfd83efeb31..1b6fee6f0de 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -524,6 +524,9 @@ namespace ts { }, getApparentType, getUnionType, + isTypeAssignableTo: (source, target) => { + return isTypeAssignableTo(source, target); + }, createAnonymousType, createSignature, createSymbol, diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 2c8a882c164..6e92eba8ded 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -3286,6 +3286,7 @@ namespace ts { /* @internal */ getElementTypeOfArrayType(arrayType: Type): Type | undefined; /* @internal */ createPromiseType(type: Type): Type; + /* @internal */ isTypeAssignableTo(source: Type, target: Type): boolean; /* @internal */ createAnonymousType(symbol: Symbol, members: SymbolTable, callSignatures: Signature[], constructSignatures: Signature[], stringIndexInfo: IndexInfo | undefined, numberIndexInfo: IndexInfo | undefined): Type; /* @internal */ createSignature( declaration: SignatureDeclaration, diff --git a/src/services/codefixes/inferFromUsage.ts b/src/services/codefixes/inferFromUsage.ts index 2eb9eaca7b7..4b111dae25b 100644 --- a/src/services/codefixes/inferFromUsage.ts +++ b/src/services/codefixes/inferFromUsage.ts @@ -401,7 +401,7 @@ namespace ts.codefix { interface CallUsage { argumentTypes: Type[]; - returnType: Usage; + return_: Usage; } interface Usage { @@ -671,16 +671,17 @@ namespace ts.codefix { function inferTypeFromCallExpression(parent: CallExpression | NewExpression, usage: Usage): void { const call: CallUsage = { argumentTypes: [], - returnType: {} + return_: {} }; if (parent.arguments) { for (const argument of parent.arguments) { + // TODO: should recursively infer a usage here, right? call.argumentTypes.push(checker.getTypeAtLocation(argument)); } } - calculateUsageOfNode(parent, call.returnType); + calculateUsageOfNode(parent, call.return_); if (parent.kind === SyntaxKind.CallExpression) { (usage.calls || (usage.calls = [])).push(call); } @@ -830,6 +831,7 @@ namespace ts.codefix { } types.push(...(usage.candidateTypes || []).map(t => checker.getBaseTypeOfLiteralType(t))); + types.push(...findBuiltinType(usage)); if (usage.properties && hasCalls(usage.properties.get("then" as __String))) { const paramType = getParameterTypeFromCalls(0, usage.properties.get("then" as __String)!.calls!, /*isRestParameter*/ false)!; // TODO: GH#18217 @@ -858,15 +860,11 @@ namespace ts.codefix { } if (usage.calls) { - for (const call of usage.calls) { - callSignatures.push(getSignatureFromCall(call)); - } + callSignatures.push(getSignatureFromCalls(usage.calls)); } if (usage.constructs) { - for (const construct of usage.constructs) { - constructSignatures.push(getSignatureFromCall(construct)); - } + constructSignatures.push(getSignatureFromCalls(usage.constructs)); } if (usage.stringIndex) { @@ -882,6 +880,61 @@ namespace ts.codefix { } } + function combineUsages(usages: Usage[]): Usage { + return { + isNumber: usages.some(u => u.isNumber), + isString: usages.some(u => u.isString), + isNumberOrString: usages.some(u => u.isNumberOrString), + candidateTypes: flatMap(usages, u => u.candidateTypes) as Type[], + properties: undefined, // TODO + calls: flatMap(usages, u => u.calls) as CallUsage[], + constructs: flatMap(usages, u => u.constructs) as CallUsage[], + numberIndex: forEach(usages, u => u.numberIndex), + stringIndex: forEach(usages, u => u.stringIndex), + candidateThisTypes: flatMap(usages, u => u.candidateThisTypes) as Type[], + } + } + + function findBuiltinType(usage: Usage): Type[] { + const builtins = [ + checker.getStringType(), + checker.getNumberType(), + checker.createArrayType(checker.getAnyType()), + checker.createPromiseType(checker.getAnyType()), + // checker.getFunctionType() // not sure what this was supposed to be good for. + ]; + const matches = builtins.filter(t => matchesAllPropertiesOf(t, usage)); + if (false && 0 < matches.length && matches.length < 3) { + return matches; + } + return []; + } + + function matchesAllPropertiesOf(type: Type, usage: Usage) { + if (!usage.properties) return false; + let result = true; + usage.properties.forEach((prop, name) => { + const source = checker.getUnionType(inferFromUsage(prop)); + const target = checker.getTypeOfPropertyOfType(type, name as string); + if (target && prop.calls) { + const sigs = checker.getSignaturesOfType(target, ts.SignatureKind.Call); + result = result && !!sigs.length && sigs.some( + sig => checker.isTypeAssignableTo( + getFunctionFromCalls(prop.calls!), + checker.createAnonymousType(undefined!, createSymbolTable(), [sig], emptyArray, undefined, undefined))); + } + else { + result = result && !!source && !!target && checker.isTypeAssignableTo(source, target); + } + }); + return result; + } + + + function getFunctionFromCalls(calls: CallUsage[]) { + return checker.createAnonymousType(undefined!, createSymbolTable(), [getSignatureFromCalls(calls)], emptyArray, undefined, undefined); + } + function getParameterTypeFromCalls(parameterIndex: number, calls: CallUsage[], isRestParameter: boolean) { let types: Type[] = []; if (calls) { @@ -904,16 +957,20 @@ namespace ts.codefix { return undefined; } - function getSignatureFromCall(call: CallUsage): Signature { + function getSignatureFromCalls(calls: CallUsage[]): Signature { const parameters: Symbol[] = []; - for (let i = 0; i < call.argumentTypes.length; i++) { + const length = Math.max(...calls.map(c => c.argumentTypes.length)); + for (let i = 0; i < length; i++) { const symbol = checker.createSymbol(SymbolFlags.FunctionScopedVariable, escapeLeadingUnderscores(`arg${i}`)); - symbol.type = checker.getWidenedType(checker.getBaseTypeOfLiteralType(call.argumentTypes[i])); + symbol.type = unifyFromUsage(calls.map(call => call.argumentTypes[i] || checker.getUndefinedType())); + if (calls.some(call => call.argumentTypes[i] === undefined)) { + symbol.flags |= SymbolFlags.Optional; + } parameters.push(symbol); } - const returnType = unifyFromUsage(inferFromUsage(call.returnType), checker.getVoidType()); + const returnType = unifyFromUsage(inferFromUsage(combineUsages(calls.map(call => call.return_))), checker.getVoidType()); // TODO: GH#18217 - return checker.createSignature(/*declaration*/ undefined!, /*typeParameters*/ undefined, /*thisParameter*/ undefined, parameters, returnType, /*typePredicate*/ undefined, call.argumentTypes.length, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false); + return checker.createSignature(/*declaration*/ undefined!, /*typeParameters*/ undefined, /*thisParameter*/ undefined, parameters, returnType, /*typePredicate*/ undefined, length, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false); } function addCandidateType(usage: Usage, type: Type | undefined) { From 945d423ef5b511ae157b15849b2b822de4b48953 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Wed, 28 Aug 2019 14:12:21 -0700 Subject: [PATCH 40/97] Fix bugs in combineUsages/getSignatureFromCalls --- src/services/codefixes/inferFromUsage.ts | 76 +++++++++++++++++------- 1 file changed, 56 insertions(+), 20 deletions(-) diff --git a/src/services/codefixes/inferFromUsage.ts b/src/services/codefixes/inferFromUsage.ts index 4b111dae25b..92382c462fa 100644 --- a/src/services/codefixes/inferFromUsage.ts +++ b/src/services/codefixes/inferFromUsage.ts @@ -405,18 +405,19 @@ namespace ts.codefix { } interface Usage { - isNumber?: boolean; - isString?: boolean; + isNumber: boolean | undefined; + isString: boolean | undefined; /** Used ambiguously, eg x + ___ or object[___]; results in string | number if no other evidence exists */ - isNumberOrString?: boolean; + isNumberOrString: boolean | undefined; - candidateTypes?: Type[]; - properties?: UnderscoreEscapedMap; - calls?: CallUsage[]; - constructs?: CallUsage[]; - numberIndex?: Usage; - stringIndex?: Usage; - candidateThisTypes?: Type[]; + candidateTypes: Type[] | undefined; + properties: UnderscoreEscapedMap | undefined; + calls: CallUsage[] | undefined; + constructs: CallUsage[] | undefined; + numberIndex: Usage | undefined; + stringIndex: Usage | undefined; + candidateThisTypes: Type[] | undefined; + inferredTypes: Type[] | undefined; } function single(): Type { @@ -428,7 +429,7 @@ namespace ts.codefix { return undefined; } - const usage: Usage = {}; + const usage = createEmptyUsage(); for (const reference of references) { cancellationToken.throwIfCancellationRequested(); calculateUsageOfNode(reference, usage); @@ -466,7 +467,7 @@ namespace ts.codefix { } function thisParameter() { - const usage: Usage = {}; + const usage = createEmptyUsage(); for (const reference of references) { cancellationToken.throwIfCancellationRequested(); calculateUsageOfNode(reference, usage); @@ -476,7 +477,7 @@ namespace ts.codefix { } function inferTypesFromReferencesSingle(references: readonly Identifier[]): Type[] { - const usage: Usage = {}; + const usage: Usage = createEmptyUsage(); for (const reference of references) { cancellationToken.throwIfCancellationRequested(); calculateUsageOfNode(reference, usage); @@ -671,7 +672,7 @@ namespace ts.codefix { function inferTypeFromCallExpression(parent: CallExpression | NewExpression, usage: Usage): void { const call: CallUsage = { argumentTypes: [], - return_: {} + return_: createEmptyUsage() }; if (parent.arguments) { @@ -695,7 +696,7 @@ namespace ts.codefix { if (!usage.properties) { usage.properties = createUnderscoreEscapedMap(); } - const propertyUsage = usage.properties.get(name) || { }; + const propertyUsage = usage.properties.get(name) || createEmptyUsage(); calculateUsageOfNode(parent, propertyUsage); usage.properties.set(name, propertyUsage); } @@ -707,7 +708,7 @@ namespace ts.codefix { } else { const indexType = checker.getTypeAtLocation(parent.argumentExpression); - const indexUsage = {}; + const indexUsage = createEmptyUsage(); calculateUsageOfNode(parent, indexUsage); if (indexType.flags & TypeFlags.NumberLike) { usage.numberIndex = indexUsage; @@ -845,7 +846,10 @@ namespace ts.codefix { if (usage.numberIndex) { types.push(checker.createArrayType(recur(usage.numberIndex))); } - else if (usage.properties || usage.calls || usage.constructs || usage.stringIndex) { + else if (usage.properties && usage.properties.size + || usage.calls && usage.calls.length + || usage.constructs && usage.constructs.length + || usage.stringIndex) { const members = createUnderscoreEscapedMap(); const callSignatures: Signature[] = []; const constructSignatures: Signature[] = []; @@ -873,25 +877,57 @@ namespace ts.codefix { types.push(checker.createAnonymousType(/*symbol*/ undefined!, members, callSignatures, constructSignatures, stringIndexInfo, /*numberIndexInfo*/ undefined)); // TODO: GH#18217 } - return types; + return types; // TODO: Should cache this since I HOPE it doesn't change function recur(innerUsage: Usage): Type { return unifyFromUsage(inferFromUsage(innerUsage)); } } + function createEmptyUsage(): Usage { + return { + isNumber: undefined, + isString: undefined, + isNumberOrString: undefined, + candidateTypes: undefined, + properties: undefined, + calls: undefined, + constructs: undefined, + numberIndex: undefined, + stringIndex: undefined, + candidateThisTypes: undefined, + inferredTypes: undefined, + } + } + function combineUsages(usages: Usage[]): Usage { + const combinedProperties = createUnderscoreEscapedMap() + for (const u of usages) { + if (u.properties) { + u.properties.forEach((p,name) => { + if (!combinedProperties.has(name)) { + combinedProperties.set(name, []); + } + combinedProperties.get(name)!.push(p); + }); + } + } + const properties = createUnderscoreEscapedMap() + combinedProperties.forEach((ps,name) => { + properties.set(name, combineUsages(ps)); + }); return { isNumber: usages.some(u => u.isNumber), isString: usages.some(u => u.isString), isNumberOrString: usages.some(u => u.isNumberOrString), candidateTypes: flatMap(usages, u => u.candidateTypes) as Type[], - properties: undefined, // TODO + properties, calls: flatMap(usages, u => u.calls) as CallUsage[], constructs: flatMap(usages, u => u.constructs) as CallUsage[], numberIndex: forEach(usages, u => u.numberIndex), stringIndex: forEach(usages, u => u.stringIndex), candidateThisTypes: flatMap(usages, u => u.candidateThisTypes) as Type[], + inferredTypes: undefined, // clear type cache } } @@ -901,7 +937,7 @@ namespace ts.codefix { checker.getNumberType(), checker.createArrayType(checker.getAnyType()), checker.createPromiseType(checker.getAnyType()), - // checker.getFunctionType() // not sure what this was supposed to be good for. + // checker.getFunctionType() // TODO: not sure what this was supposed to be good for. ]; const matches = builtins.filter(t => matchesAllPropertiesOf(t, usage)); if (false && 0 < matches.length && matches.length < 3) { From 37150d9cb548e6e7802b731e8c03a298434e18d7 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Thu, 29 Aug 2019 12:40:33 -0700 Subject: [PATCH 41/97] Turn on findBuiltinTypes Type parameter inference is special-cased, just moved from its previous place with no improvement. --- src/services/codefixes/inferFromUsage.ts | 103 +++++++++--------- .../codeFixInferFromFunctionUsage.ts | 4 +- .../codeFixInferFromPrimitiveUsage.ts | 9 ++ .../codeFixInferFromUsageCallBodyBoth.ts | 2 +- 4 files changed, 65 insertions(+), 53 deletions(-) create mode 100644 tests/cases/fourslash/codeFixInferFromPrimitiveUsage.ts diff --git a/src/services/codefixes/inferFromUsage.ts b/src/services/codefixes/inferFromUsage.ts index 92382c462fa..ab1c2a4a52d 100644 --- a/src/services/codefixes/inferFromUsage.ts +++ b/src/services/codefixes/inferFromUsage.ts @@ -421,7 +421,7 @@ namespace ts.codefix { } function single(): Type { - return unifyFromUsage(inferTypesFromReferencesSingle(references)); + return unifyTypes(inferTypesFromReferencesSingle(references)); } function parameters(declaration: FunctionLike): ParameterInference[] | undefined { @@ -457,7 +457,7 @@ namespace ts.codefix { const inferred = inferTypesFromReferencesSingle(getReferences(parameter.name, program, cancellationToken)); types.push(...(isRest ? mapDefined(inferred, checker.getElementTypeOfArrayType) : inferred)); } - const type = unifyFromUsage(types); + const type = unifyTypes(types); return { type: isRest ? checker.createArrayType(type) : type, isOptional: isOptional && !isRest, @@ -473,7 +473,7 @@ namespace ts.codefix { calculateUsageOfNode(reference, usage); } - return unifyFromUsage(usage.candidateThisTypes || emptyArray); + return unifyTypes(usage.candidateThisTypes || emptyArray); } function inferTypesFromReferencesSingle(references: readonly Identifier[]): Type[] { @@ -627,6 +627,9 @@ namespace ts.codefix { else if (otherOperandType.flags & TypeFlags.StringLike) { usage.isString = true; } + else if (otherOperandType.flags & TypeFlags.Any) { + // do nothing, maybe we'll learn something elsewhere + } else { usage.isNumberOrString = true; } @@ -748,7 +751,7 @@ namespace ts.codefix { return inferences.filter(i => toRemove.every(f => !f(i))); } - function unifyFromUsage(inferences: ReadonlyArray, fallback = checker.getAnyType()): Type { + function unifyTypes(inferences: ReadonlyArray, fallback = checker.getAnyType()): Type { if (!inferences.length) return fallback; // 1. string or number individually override string | number @@ -774,7 +777,7 @@ namespace ts.codefix { good = good.filter(i => !(checker.getObjectFlags(i) & ObjectFlags.Anonymous)); good.push(unifyAnonymousTypes(anons)); } - return checker.getWidenedType(checker.getUnionType(good)); + return checker.getWidenedType(checker.getUnionType(good, UnionReduction.Subtype)); } function unifyAnonymousTypes(anons: AnonymousType[]) { @@ -832,16 +835,7 @@ namespace ts.codefix { } types.push(...(usage.candidateTypes || []).map(t => checker.getBaseTypeOfLiteralType(t))); - types.push(...findBuiltinType(usage)); - - if (usage.properties && hasCalls(usage.properties.get("then" as __String))) { - const paramType = getParameterTypeFromCalls(0, usage.properties.get("then" as __String)!.calls!, /*isRestParameter*/ false)!; // TODO: GH#18217 - const types = paramType.getCallSignatures().map(sig => sig.getReturnType()); - types.push(checker.createPromiseType(types.length ? checker.getUnionType(types, UnionReduction.Subtype) : checker.getAnyType())); - } - else if (usage.properties && hasCalls(usage.properties.get("push" as __String))) { - types.push(checker.createArrayType(getParameterTypeFromCalls(0, usage.properties.get("push" as __String)!.calls!, /*isRestParameter*/ false)!)); - } + types.push(...findBuiltinTypes(usage)); if (usage.numberIndex) { types.push(checker.createArrayType(recur(usage.numberIndex))); @@ -864,11 +858,12 @@ namespace ts.codefix { } if (usage.calls) { - callSignatures.push(getSignatureFromCalls(usage.calls)); + callSignatures.push(getSignatureFromCalls(usage.calls, checker.getVoidType())); } if (usage.constructs) { - constructSignatures.push(getSignatureFromCalls(usage.constructs)); + // TODO: fallback return should maybe be {}? + constructSignatures.push(getSignatureFromCalls(usage.constructs, checker.getVoidType())); } if (usage.stringIndex) { @@ -880,7 +875,7 @@ namespace ts.codefix { return types; // TODO: Should cache this since I HOPE it doesn't change function recur(innerUsage: Usage): Type { - return unifyFromUsage(inferFromUsage(innerUsage)); + return unifyTypes(inferFromUsage(innerUsage)); } } @@ -931,7 +926,8 @@ namespace ts.codefix { } } - function findBuiltinType(usage: Usage): Type[] { + function findBuiltinTypes(usage: Usage): Type[] { + if (!usage.properties || !usage.properties.size) return []; const builtins = [ checker.getStringType(), checker.getNumberType(), @@ -939,9 +935,21 @@ namespace ts.codefix { checker.createPromiseType(checker.getAnyType()), // checker.getFunctionType() // TODO: not sure what this was supposed to be good for. ]; + // TODO: Still need to infer type parameters const matches = builtins.filter(t => matchesAllPropertiesOf(t, usage)); - if (false && 0 < matches.length && matches.length < 3) { - return matches; + if (0 < matches.length && matches.length < 3) { + return matches.map(m => { + // special-case array and promise for now + if (m === builtins[3] && hasCalls(usage.properties!.get("then" as __String))) { + const paramType = getParameterTypeFromCalls(0, usage.properties!.get("then" as __String)!.calls!, /*isRestParameter*/ false)!; // TODO: GH#18217 + const returns = paramType.getCallSignatures().map(sig => sig.getReturnType()); + return checker.createPromiseType(returns.length ? checker.getUnionType(returns, UnionReduction.Subtype) : checker.getAnyType()); + } + else if (m === builtins[2] && hasCalls(usage.properties!.get("push" as __String))) { + return checker.createArrayType(getParameterTypeFromCalls(0, usage.properties!.get("push" as __String)!.calls!, /*isRestParameter*/ false)!); + } + return m; + }); } return []; } @@ -949,18 +957,18 @@ namespace ts.codefix { function matchesAllPropertiesOf(type: Type, usage: Usage) { if (!usage.properties) return false; let result = true; - usage.properties.forEach((prop, name) => { - const source = checker.getUnionType(inferFromUsage(prop)); - const target = checker.getTypeOfPropertyOfType(type, name as string); - if (target && prop.calls) { - const sigs = checker.getSignaturesOfType(target, ts.SignatureKind.Call); - result = result && !!sigs.length && sigs.some( - sig => checker.isTypeAssignableTo( - getFunctionFromCalls(prop.calls!), - checker.createAnonymousType(undefined!, createSymbolTable(), [sig], emptyArray, undefined, undefined))); + usage.properties.forEach((propUsage, name) => { + const source = checker.getTypeOfPropertyOfType(type, name as string); + if (!source) { + result = false; + return; + } + if (propUsage.calls) { + const sigs = checker.getSignaturesOfType(source, ts.SignatureKind.Call); + result = result && !!sigs.length && checker.isTypeAssignableTo(source, getFunctionFromCalls(propUsage.calls)); } else { - result = result && !!source && !!target && checker.isTypeAssignableTo(source, target); + result = result && checker.isTypeAssignableTo(source, unifyTypes(inferFromUsage(propUsage))); } }); return result; @@ -968,43 +976,38 @@ namespace ts.codefix { function getFunctionFromCalls(calls: CallUsage[]) { - return checker.createAnonymousType(undefined!, createSymbolTable(), [getSignatureFromCalls(calls)], emptyArray, undefined, undefined); + return checker.createAnonymousType(undefined!, createSymbolTable(), [getSignatureFromCalls(calls, checker.getAnyType())], emptyArray, undefined, undefined); } function getParameterTypeFromCalls(parameterIndex: number, calls: CallUsage[], isRestParameter: boolean) { + // TODO: This is largely redundant with getSignatureFromCalls, I think. (though it handles rest parameters correctly, so that needs to be integrated there) let types: Type[] = []; - if (calls) { - for (const call of calls) { - if (call.argumentTypes.length > parameterIndex) { - if (isRestParameter) { - types = concatenate(types, map(call.argumentTypes.slice(parameterIndex), a => checker.getBaseTypeOfLiteralType(a))); - } - else { - types.push(checker.getBaseTypeOfLiteralType(call.argumentTypes[parameterIndex])); - } + for (const call of calls) { + if (call.argumentTypes.length > parameterIndex) { + if (isRestParameter) { + types = concatenate(types, map(call.argumentTypes.slice(parameterIndex), a => checker.getBaseTypeOfLiteralType(a))); + } + else { + types.push(checker.getBaseTypeOfLiteralType(call.argumentTypes[parameterIndex])); } } } - - if (types.length) { - const type = checker.getWidenedType(checker.getUnionType(types, UnionReduction.Subtype)); - return isRestParameter ? checker.createArrayType(type) : type; - } - return undefined; + const type = unifyTypes(types); + return isRestParameter ? checker.createArrayType(type) : type; } - function getSignatureFromCalls(calls: CallUsage[]): Signature { + function getSignatureFromCalls(calls: CallUsage[], fallbackReturn: Type): Signature { const parameters: Symbol[] = []; const length = Math.max(...calls.map(c => c.argumentTypes.length)); for (let i = 0; i < length; i++) { const symbol = checker.createSymbol(SymbolFlags.FunctionScopedVariable, escapeLeadingUnderscores(`arg${i}`)); - symbol.type = unifyFromUsage(calls.map(call => call.argumentTypes[i] || checker.getUndefinedType())); + symbol.type = unifyTypes(calls.map(call => call.argumentTypes[i] || checker.getUndefinedType())); if (calls.some(call => call.argumentTypes[i] === undefined)) { symbol.flags |= SymbolFlags.Optional; } parameters.push(symbol); } - const returnType = unifyFromUsage(inferFromUsage(combineUsages(calls.map(call => call.return_))), checker.getVoidType()); + const returnType = unifyTypes(inferFromUsage(combineUsages(calls.map(call => call.return_))), fallbackReturn); // TODO: GH#18217 return checker.createSignature(/*declaration*/ undefined!, /*typeParameters*/ undefined, /*thisParameter*/ undefined, parameters, returnType, /*typePredicate*/ undefined, length, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false); } diff --git a/tests/cases/fourslash/codeFixInferFromFunctionUsage.ts b/tests/cases/fourslash/codeFixInferFromFunctionUsage.ts index 27a039878ba..f2dbad67b4b 100644 --- a/tests/cases/fourslash/codeFixInferFromFunctionUsage.ts +++ b/tests/cases/fourslash/codeFixInferFromFunctionUsage.ts @@ -2,8 +2,8 @@ // @noImplicitAny: true ////function wrap( [| arr |] ) { -//// arr.sort(function (a: number, b: number) { return a < b ? -1 : 1 }) +//// arr.other(function (a: number, b: number) { return a < b ? -1 : 1 }) //// } // https://github.com/Microsoft/TypeScript/issues/29330 -verify.rangeAfterCodeFix("arr: { sort: (arg0: (a: number, b: number) => 1 | -1) => void; }"); +verify.rangeAfterCodeFix("arr: { other: (arg0: (a: number, b: number) => 1 | -1) => void; }"); diff --git a/tests/cases/fourslash/codeFixInferFromPrimitiveUsage.ts b/tests/cases/fourslash/codeFixInferFromPrimitiveUsage.ts new file mode 100644 index 00000000000..81c25770085 --- /dev/null +++ b/tests/cases/fourslash/codeFixInferFromPrimitiveUsage.ts @@ -0,0 +1,9 @@ +/// + +// @noImplicitAny: true +////function wrap( [| s |] ) { +//// return s.length + s.toUpperCase() +//// } + +// https://github.com/Microsoft/TypeScript/issues/29330 +verify.rangeAfterCodeFix("s: string"); diff --git a/tests/cases/fourslash/codeFixInferFromUsageCallBodyBoth.ts b/tests/cases/fourslash/codeFixInferFromUsageCallBodyBoth.ts index 859d5ea2ce7..f59d1bc1907 100644 --- a/tests/cases/fourslash/codeFixInferFromUsageCallBodyBoth.ts +++ b/tests/cases/fourslash/codeFixInferFromUsageCallBodyBoth.ts @@ -1,7 +1,7 @@ /// ////class C { -//// +//// p = 2 ////} ////var c = new C() ////function f([|x, y |]) { From 383286ff533aad1e9fe3bfa907ef84689eff920e Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Fri, 30 Aug 2019 13:44:06 -0700 Subject: [PATCH 42/97] Add type parameter inference It's a smeary copy of the checker's type parameter, so I feel bad about duplicating that code. Not sure what the solution is, architecturally. --- src/services/codefixes/inferFromUsage.ts | 132 ++++++++++++------ .../codeFixInferFromPrimitiveUsage.ts | 7 +- 2 files changed, 94 insertions(+), 45 deletions(-) diff --git a/src/services/codefixes/inferFromUsage.ts b/src/services/codefixes/inferFromUsage.ts index ab1c2a4a52d..0a190e7b379 100644 --- a/src/services/codefixes/inferFromUsage.ts +++ b/src/services/codefixes/inferFromUsage.ts @@ -393,6 +393,19 @@ namespace ts.codefix { function inferTypeFromReferences(program: Program, references: ReadonlyArray, cancellationToken: CancellationToken) { const checker = program.getTypeChecker(); + const builtinConstructors: { [s: string]: (t: Type) => Type } = { + string: () => checker.getStringType(), + number: () => checker.getNumberType(), + Array: t => checker.createArrayType(t), + Promise: t => checker.createPromiseType(t), + }; + const builtins = [ + checker.getStringType(), + checker.getNumberType(), + checker.createArrayType(checker.getAnyType()), + checker.createPromiseType(checker.getAnyType()), + ]; + return { single, parameters, @@ -777,7 +790,7 @@ namespace ts.codefix { good = good.filter(i => !(checker.getObjectFlags(i) & ObjectFlags.Anonymous)); good.push(unifyAnonymousTypes(anons)); } - return checker.getWidenedType(checker.getUnionType(good, UnionReduction.Subtype)); + return checker.getWidenedType(checker.getUnionType(good.map(checker.getBaseTypeOfLiteralType), UnionReduction.Subtype)); } function unifyAnonymousTypes(anons: AnonymousType[]) { @@ -928,28 +941,9 @@ namespace ts.codefix { function findBuiltinTypes(usage: Usage): Type[] { if (!usage.properties || !usage.properties.size) return []; - const builtins = [ - checker.getStringType(), - checker.getNumberType(), - checker.createArrayType(checker.getAnyType()), - checker.createPromiseType(checker.getAnyType()), - // checker.getFunctionType() // TODO: not sure what this was supposed to be good for. - ]; - // TODO: Still need to infer type parameters const matches = builtins.filter(t => matchesAllPropertiesOf(t, usage)); if (0 < matches.length && matches.length < 3) { - return matches.map(m => { - // special-case array and promise for now - if (m === builtins[3] && hasCalls(usage.properties!.get("then" as __String))) { - const paramType = getParameterTypeFromCalls(0, usage.properties!.get("then" as __String)!.calls!, /*isRestParameter*/ false)!; // TODO: GH#18217 - const returns = paramType.getCallSignatures().map(sig => sig.getReturnType()); - return checker.createPromiseType(returns.length ? checker.getUnionType(returns, UnionReduction.Subtype) : checker.getAnyType()); - } - else if (m === builtins[2] && hasCalls(usage.properties!.get("push" as __String))) { - return checker.createArrayType(getParameterTypeFromCalls(0, usage.properties!.get("push" as __String)!.calls!, /*isRestParameter*/ false)!); - } - return m; - }); + return matches.map(m => inferTypeParameterFromUsage(m, usage)); } return []; } @@ -974,28 +968,86 @@ namespace ts.codefix { return result; } + // inference is limited to + // 1. generic types with a single parameter + // 2. inference to/from calls with a single signature + function inferTypeParameterFromUsage(type: Type, usage: Usage) { + if (!usage.properties || !(getObjectFlags(type) & ObjectFlags.Reference)) return type; + const generic = (type as TypeReference).target; + const singleTypeParameter = singleOrUndefined(generic.typeParameters); + if (!singleTypeParameter) return type; + + const types: Type[] = []; + usage.properties.forEach((propUsage, name) => { + const source = checker.getTypeOfPropertyOfType(generic, name as string); + if (!source) { + return Debug.fail("generic should have all the properties of its reference."); + } + if (!propUsage.calls) return; + + types.push(...infer(source, getFunctionFromCalls(propUsage.calls), singleTypeParameter)); + }); + return builtinConstructors[type.symbol.escapedName as string](unifyTypes(types)); + } + + // TODO: Source and target are bad names. Should be builtinType and usageType...or something + // and search is a bad name + function infer(source: Type, target: Type, search: Type): readonly Type[] { + if (source === search) { + return [target]; + } + else if (source.flags & TypeFlags.UnionOrIntersection) { + return flatMap((source as UnionOrIntersectionType).types, t => infer(t, target, search)); + } + else if (getObjectFlags(source) & ObjectFlags.Reference && getObjectFlags(target) & ObjectFlags.Reference) { + // this is wrong because we need a reference to the targetType to, so we can check that it's also a reference + const sourceArgs = (source as TypeReference).typeArguments; + const targetArgs = (target as TypeReference).typeArguments; + const types = []; + if (sourceArgs && targetArgs) { + for (let i = 0; i < sourceArgs.length; i++) { + if (targetArgs[i]) { + types.push(...infer(sourceArgs[i], targetArgs[i], search)); + } + } + } + return types; + } + const sourceSigs = checker.getSignaturesOfType(source, SignatureKind.Call); + const targetSigs = checker.getSignaturesOfType(target, SignatureKind.Call); + if (sourceSigs.length === 1 && targetSigs.length === 1) { + return inferFromSignatures(sourceSigs[0], targetSigs[0], search); + } + return []; + } + + function inferFromSignatures(sourceSig: Signature, targetSig: Signature, search: Type) { + const types = []; + for (let i = 0; i < sourceSig.parameters.length; i++) { + const sourceParam = sourceSig.parameters[i]; + const targetParam = targetSig.parameters[i]; + const isRest = sourceSig.declaration && isRestParameter(sourceSig.declaration.parameters[i]); + if (!targetParam) { + break; + } + let sourceType = checker.getTypeOfSymbolAtLocation(sourceParam, sourceParam.valueDeclaration); + let elementType = isRest && checker.getElementTypeOfArrayType(sourceType); + if (elementType) { + sourceType = elementType; + } + const targetType = (targetParam as SymbolLinks).type || checker.getTypeOfSymbolAtLocation(targetParam, targetParam.valueDeclaration); + types.push(...infer(sourceType, targetType, search)); + } + const sourceReturn = checker.getReturnTypeOfSignature(sourceSig); + const targetReturn = checker.getReturnTypeOfSignature(targetSig); + types.push(...infer(sourceReturn, targetReturn, search)); + return types; + } function getFunctionFromCalls(calls: CallUsage[]) { return checker.createAnonymousType(undefined!, createSymbolTable(), [getSignatureFromCalls(calls, checker.getAnyType())], emptyArray, undefined, undefined); } - function getParameterTypeFromCalls(parameterIndex: number, calls: CallUsage[], isRestParameter: boolean) { - // TODO: This is largely redundant with getSignatureFromCalls, I think. (though it handles rest parameters correctly, so that needs to be integrated there) - let types: Type[] = []; - for (const call of calls) { - if (call.argumentTypes.length > parameterIndex) { - if (isRestParameter) { - types = concatenate(types, map(call.argumentTypes.slice(parameterIndex), a => checker.getBaseTypeOfLiteralType(a))); - } - else { - types.push(checker.getBaseTypeOfLiteralType(call.argumentTypes[parameterIndex])); - } - } - } - const type = unifyTypes(types); - return isRestParameter ? checker.createArrayType(type) : type; - } - function getSignatureFromCalls(calls: CallUsage[], fallbackReturn: Type): Signature { const parameters: Symbol[] = []; const length = Math.max(...calls.map(c => c.argumentTypes.length)); @@ -1023,9 +1075,5 @@ namespace ts.codefix { (usage.candidateThisTypes || (usage.candidateThisTypes = [])).push(type); } } - - function hasCalls(usage: Usage | undefined): boolean { - return !!usage && !!usage.calls; - } } } diff --git a/tests/cases/fourslash/codeFixInferFromPrimitiveUsage.ts b/tests/cases/fourslash/codeFixInferFromPrimitiveUsage.ts index 81c25770085..b74e49b4d39 100644 --- a/tests/cases/fourslash/codeFixInferFromPrimitiveUsage.ts +++ b/tests/cases/fourslash/codeFixInferFromPrimitiveUsage.ts @@ -1,9 +1,10 @@ /// // @noImplicitAny: true -////function wrap( [| s |] ) { -//// return s.length + s.toUpperCase() +//// function wrap( [| s |] ) { +//// return s.length + s.indexOf('hi') //// } // https://github.com/Microsoft/TypeScript/issues/29330 -verify.rangeAfterCodeFix("s: string"); +verify.rangeAfterCodeFix("s: string | string[]"); + From 8ffc42f5a6739f257ecf52a24fee0c45c1c282a3 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Tue, 3 Sep 2019 15:24:49 -0700 Subject: [PATCH 43/97] Don't instantiate-in-context-of when inferring to type variable --- src/compiler/checker.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index cfad47b6533..f51aee47a1a 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -177,6 +177,7 @@ namespace ts { const enum ContextFlags { None = 0, Signature = 1 << 0, // Obtaining contextual signature + NoConstraints = 1 << 1, // Don't obtain type variable constraints } const enum AccessFlags { @@ -19193,7 +19194,7 @@ namespace ts { getContextualTypeForObjectLiteralMethod(node, contextFlags) : getContextualType(node, contextFlags); const instantiatedType = instantiateContextualType(contextualType, node, contextFlags); - if (instantiatedType) { + if (instantiatedType && !(contextFlags && contextFlags & ContextFlags.NoConstraints && instantiatedType.flags & TypeFlags.TypeVariable)) { const apparentType = mapType(instantiatedType, getApparentType, /*noReductions*/ true); if (apparentType.flags & TypeFlags.Union) { if (isObjectLiteralExpression(node)) { @@ -25098,8 +25099,8 @@ namespace ts { const constructSignature = getSingleSignature(type, SignatureKind.Construct, /*allowMembers*/ true); const signature = callSignature || constructSignature; if (signature && signature.typeParameters) { - const contextualType = getApparentTypeOfContextualType(node); - if (contextualType && !isMixinConstructorType(contextualType)) { + const contextualType = getApparentTypeOfContextualType(node, ContextFlags.NoConstraints); + if (contextualType) { const contextualSignature = getSingleSignature(getNonNullableType(contextualType), callSignature ? SignatureKind.Call : SignatureKind.Construct, /*allowMembers*/ false); if (contextualSignature && !contextualSignature.typeParameters) { if (checkMode & CheckMode.SkipGenericFunctions) { From c02fdaa590ecea073894d0d05b3cadd60270d5ff Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Tue, 3 Sep 2019 16:36:41 -0700 Subject: [PATCH 44/97] Accept new baselines --- .../reference/functionConstraintSatisfaction2.types | 4 ++-- .../reference/functionConstraintSatisfaction3.types | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/baselines/reference/functionConstraintSatisfaction2.types b/tests/baselines/reference/functionConstraintSatisfaction2.types index f5a7eb0fd65..f0a0f3b61f2 100644 --- a/tests/baselines/reference/functionConstraintSatisfaction2.types +++ b/tests/baselines/reference/functionConstraintSatisfaction2.types @@ -79,8 +79,8 @@ var r7 = foo2(b); >b : new (x: string) => string var r8 = foo2((x: U) => x); // no error expected ->r8 : (x: string) => string ->foo2((x: U) => x) : (x: string) => string +>r8 : (x: U) => U +>foo2((x: U) => x) : (x: U) => U >foo2 : string>(x: T) => T >(x: U) => x : (x: U) => U >x : U diff --git a/tests/baselines/reference/functionConstraintSatisfaction3.types b/tests/baselines/reference/functionConstraintSatisfaction3.types index 499f73b1664..dc760608c73 100644 --- a/tests/baselines/reference/functionConstraintSatisfaction3.types +++ b/tests/baselines/reference/functionConstraintSatisfaction3.types @@ -103,16 +103,16 @@ var c2: { (x: T): T; (x: T, y: T): T }; >y : T var r9 = foo(function (x: U) { return x; }); ->r9 : (x: string) => string ->foo(function (x: U) { return x; }) : (x: string) => string +>r9 : (x: U) => U +>foo(function (x: U) { return x; }) : (x: U) => U >foo : string>(x: T) => T >function (x: U) { return x; } : (x: U) => U >x : U >x : U var r10 = foo((x: U) => x); ->r10 : (x: string) => string ->foo((x: U) => x) : (x: string) => string +>r10 : (x: U) => U +>foo((x: U) => x) : (x: U) => U >foo : string>(x: T) => T >(x: U) => x : (x: U) => U >x : U From 4bd9b62fa6e02cc72eb511a23cc42ea1dc153f86 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Tue, 3 Sep 2019 17:23:43 -0700 Subject: [PATCH 45/97] Add regression tests --- .../contextualSignatureInstantiation4.ts | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 tests/cases/compiler/contextualSignatureInstantiation4.ts diff --git a/tests/cases/compiler/contextualSignatureInstantiation4.ts b/tests/cases/compiler/contextualSignatureInstantiation4.ts new file mode 100644 index 00000000000..6c29ca3d40a --- /dev/null +++ b/tests/cases/compiler/contextualSignatureInstantiation4.ts @@ -0,0 +1,20 @@ +// @strict: true + +// Repros from #32976 + +declare class Banana { constructor(a: string, property: T) } + +declare function fruitFactory1(Fruit: new (...args: any[]) => TFruit): TFruit +const banana1 = fruitFactory1(Banana) // Banana + +declare function fruitFactory2(Fruit: new (a: string, ...args: any[]) => TFruit): TFruit +const banana2 = fruitFactory2(Banana) // Banana + +declare function fruitFactory3(Fruit: new (a: string, s: "foo", ...args: any[]) => TFruit): TFruit +const banana3 = fruitFactory3(Banana) // Banana<"foo"> + +declare function fruitFactory4(Fruit: new (a: string, ...args: "foo"[]) => TFruit): TFruit +const banana4 = fruitFactory4(Banana) // Banana<"foo"> + +declare function fruitFactory5(Fruit: new (...args: "foo"[]) => TFruit): TFruit +const banana5 = fruitFactory5(Banana) // Banana<"foo"> From bbec8b36ef346e497a90bc99c00556dc97939976 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Tue, 3 Sep 2019 17:23:52 -0700 Subject: [PATCH 46/97] Accept new baselines --- .../contextualSignatureInstantiation4.js | 29 +++++++ .../contextualSignatureInstantiation4.symbols | 79 +++++++++++++++++++ .../contextualSignatureInstantiation4.types | 67 ++++++++++++++++ 3 files changed, 175 insertions(+) create mode 100644 tests/baselines/reference/contextualSignatureInstantiation4.js create mode 100644 tests/baselines/reference/contextualSignatureInstantiation4.symbols create mode 100644 tests/baselines/reference/contextualSignatureInstantiation4.types diff --git a/tests/baselines/reference/contextualSignatureInstantiation4.js b/tests/baselines/reference/contextualSignatureInstantiation4.js new file mode 100644 index 00000000000..8db041b29b5 --- /dev/null +++ b/tests/baselines/reference/contextualSignatureInstantiation4.js @@ -0,0 +1,29 @@ +//// [contextualSignatureInstantiation4.ts] +// Repros from #32976 + +declare class Banana { constructor(a: string, property: T) } + +declare function fruitFactory1(Fruit: new (...args: any[]) => TFruit): TFruit +const banana1 = fruitFactory1(Banana) // Banana + +declare function fruitFactory2(Fruit: new (a: string, ...args: any[]) => TFruit): TFruit +const banana2 = fruitFactory2(Banana) // Banana + +declare function fruitFactory3(Fruit: new (a: string, s: "foo", ...args: any[]) => TFruit): TFruit +const banana3 = fruitFactory3(Banana) // Banana<"foo"> + +declare function fruitFactory4(Fruit: new (a: string, ...args: "foo"[]) => TFruit): TFruit +const banana4 = fruitFactory4(Banana) // Banana<"foo"> + +declare function fruitFactory5(Fruit: new (...args: "foo"[]) => TFruit): TFruit +const banana5 = fruitFactory5(Banana) // Banana<"foo"> + + +//// [contextualSignatureInstantiation4.js] +"use strict"; +// Repros from #32976 +var banana1 = fruitFactory1(Banana); // Banana +var banana2 = fruitFactory2(Banana); // Banana +var banana3 = fruitFactory3(Banana); // Banana<"foo"> +var banana4 = fruitFactory4(Banana); // Banana<"foo"> +var banana5 = fruitFactory5(Banana); // Banana<"foo"> diff --git a/tests/baselines/reference/contextualSignatureInstantiation4.symbols b/tests/baselines/reference/contextualSignatureInstantiation4.symbols new file mode 100644 index 00000000000..a08486b43d8 --- /dev/null +++ b/tests/baselines/reference/contextualSignatureInstantiation4.symbols @@ -0,0 +1,79 @@ +=== tests/cases/compiler/contextualSignatureInstantiation4.ts === +// Repros from #32976 + +declare class Banana { constructor(a: string, property: T) } +>Banana : Symbol(Banana, Decl(contextualSignatureInstantiation4.ts, 0, 0)) +>T : Symbol(T, Decl(contextualSignatureInstantiation4.ts, 2, 21)) +>a : Symbol(a, Decl(contextualSignatureInstantiation4.ts, 2, 53)) +>property : Symbol(property, Decl(contextualSignatureInstantiation4.ts, 2, 63)) +>T : Symbol(T, Decl(contextualSignatureInstantiation4.ts, 2, 21)) + +declare function fruitFactory1(Fruit: new (...args: any[]) => TFruit): TFruit +>fruitFactory1 : Symbol(fruitFactory1, Decl(contextualSignatureInstantiation4.ts, 2, 78)) +>TFruit : Symbol(TFruit, Decl(contextualSignatureInstantiation4.ts, 4, 31)) +>Fruit : Symbol(Fruit, Decl(contextualSignatureInstantiation4.ts, 4, 39)) +>args : Symbol(args, Decl(contextualSignatureInstantiation4.ts, 4, 51)) +>TFruit : Symbol(TFruit, Decl(contextualSignatureInstantiation4.ts, 4, 31)) +>TFruit : Symbol(TFruit, Decl(contextualSignatureInstantiation4.ts, 4, 31)) + +const banana1 = fruitFactory1(Banana) // Banana +>banana1 : Symbol(banana1, Decl(contextualSignatureInstantiation4.ts, 5, 5)) +>fruitFactory1 : Symbol(fruitFactory1, Decl(contextualSignatureInstantiation4.ts, 2, 78)) +>Banana : Symbol(Banana, Decl(contextualSignatureInstantiation4.ts, 0, 0)) + +declare function fruitFactory2(Fruit: new (a: string, ...args: any[]) => TFruit): TFruit +>fruitFactory2 : Symbol(fruitFactory2, Decl(contextualSignatureInstantiation4.ts, 5, 37)) +>TFruit : Symbol(TFruit, Decl(contextualSignatureInstantiation4.ts, 7, 31)) +>Fruit : Symbol(Fruit, Decl(contextualSignatureInstantiation4.ts, 7, 39)) +>a : Symbol(a, Decl(contextualSignatureInstantiation4.ts, 7, 51)) +>args : Symbol(args, Decl(contextualSignatureInstantiation4.ts, 7, 61)) +>TFruit : Symbol(TFruit, Decl(contextualSignatureInstantiation4.ts, 7, 31)) +>TFruit : Symbol(TFruit, Decl(contextualSignatureInstantiation4.ts, 7, 31)) + +const banana2 = fruitFactory2(Banana) // Banana +>banana2 : Symbol(banana2, Decl(contextualSignatureInstantiation4.ts, 8, 5)) +>fruitFactory2 : Symbol(fruitFactory2, Decl(contextualSignatureInstantiation4.ts, 5, 37)) +>Banana : Symbol(Banana, Decl(contextualSignatureInstantiation4.ts, 0, 0)) + +declare function fruitFactory3(Fruit: new (a: string, s: "foo", ...args: any[]) => TFruit): TFruit +>fruitFactory3 : Symbol(fruitFactory3, Decl(contextualSignatureInstantiation4.ts, 8, 37)) +>TFruit : Symbol(TFruit, Decl(contextualSignatureInstantiation4.ts, 10, 31)) +>Fruit : Symbol(Fruit, Decl(contextualSignatureInstantiation4.ts, 10, 39)) +>a : Symbol(a, Decl(contextualSignatureInstantiation4.ts, 10, 51)) +>s : Symbol(s, Decl(contextualSignatureInstantiation4.ts, 10, 61)) +>args : Symbol(args, Decl(contextualSignatureInstantiation4.ts, 10, 71)) +>TFruit : Symbol(TFruit, Decl(contextualSignatureInstantiation4.ts, 10, 31)) +>TFruit : Symbol(TFruit, Decl(contextualSignatureInstantiation4.ts, 10, 31)) + +const banana3 = fruitFactory3(Banana) // Banana<"foo"> +>banana3 : Symbol(banana3, Decl(contextualSignatureInstantiation4.ts, 11, 5)) +>fruitFactory3 : Symbol(fruitFactory3, Decl(contextualSignatureInstantiation4.ts, 8, 37)) +>Banana : Symbol(Banana, Decl(contextualSignatureInstantiation4.ts, 0, 0)) + +declare function fruitFactory4(Fruit: new (a: string, ...args: "foo"[]) => TFruit): TFruit +>fruitFactory4 : Symbol(fruitFactory4, Decl(contextualSignatureInstantiation4.ts, 11, 37)) +>TFruit : Symbol(TFruit, Decl(contextualSignatureInstantiation4.ts, 13, 31)) +>Fruit : Symbol(Fruit, Decl(contextualSignatureInstantiation4.ts, 13, 39)) +>a : Symbol(a, Decl(contextualSignatureInstantiation4.ts, 13, 51)) +>args : Symbol(args, Decl(contextualSignatureInstantiation4.ts, 13, 61)) +>TFruit : Symbol(TFruit, Decl(contextualSignatureInstantiation4.ts, 13, 31)) +>TFruit : Symbol(TFruit, Decl(contextualSignatureInstantiation4.ts, 13, 31)) + +const banana4 = fruitFactory4(Banana) // Banana<"foo"> +>banana4 : Symbol(banana4, Decl(contextualSignatureInstantiation4.ts, 14, 5)) +>fruitFactory4 : Symbol(fruitFactory4, Decl(contextualSignatureInstantiation4.ts, 11, 37)) +>Banana : Symbol(Banana, Decl(contextualSignatureInstantiation4.ts, 0, 0)) + +declare function fruitFactory5(Fruit: new (...args: "foo"[]) => TFruit): TFruit +>fruitFactory5 : Symbol(fruitFactory5, Decl(contextualSignatureInstantiation4.ts, 14, 37)) +>TFruit : Symbol(TFruit, Decl(contextualSignatureInstantiation4.ts, 16, 31)) +>Fruit : Symbol(Fruit, Decl(contextualSignatureInstantiation4.ts, 16, 39)) +>args : Symbol(args, Decl(contextualSignatureInstantiation4.ts, 16, 51)) +>TFruit : Symbol(TFruit, Decl(contextualSignatureInstantiation4.ts, 16, 31)) +>TFruit : Symbol(TFruit, Decl(contextualSignatureInstantiation4.ts, 16, 31)) + +const banana5 = fruitFactory5(Banana) // Banana<"foo"> +>banana5 : Symbol(banana5, Decl(contextualSignatureInstantiation4.ts, 17, 5)) +>fruitFactory5 : Symbol(fruitFactory5, Decl(contextualSignatureInstantiation4.ts, 14, 37)) +>Banana : Symbol(Banana, Decl(contextualSignatureInstantiation4.ts, 0, 0)) + diff --git a/tests/baselines/reference/contextualSignatureInstantiation4.types b/tests/baselines/reference/contextualSignatureInstantiation4.types new file mode 100644 index 00000000000..1c27df7ff69 --- /dev/null +++ b/tests/baselines/reference/contextualSignatureInstantiation4.types @@ -0,0 +1,67 @@ +=== tests/cases/compiler/contextualSignatureInstantiation4.ts === +// Repros from #32976 + +declare class Banana { constructor(a: string, property: T) } +>Banana : Banana +>a : string +>property : T + +declare function fruitFactory1(Fruit: new (...args: any[]) => TFruit): TFruit +>fruitFactory1 : (Fruit: new (...args: any[]) => TFruit) => TFruit +>Fruit : new (...args: any[]) => TFruit +>args : any[] + +const banana1 = fruitFactory1(Banana) // Banana +>banana1 : Banana +>fruitFactory1(Banana) : Banana +>fruitFactory1 : (Fruit: new (...args: any[]) => TFruit) => TFruit +>Banana : typeof Banana + +declare function fruitFactory2(Fruit: new (a: string, ...args: any[]) => TFruit): TFruit +>fruitFactory2 : (Fruit: new (a: string, ...args: any[]) => TFruit) => TFruit +>Fruit : new (a: string, ...args: any[]) => TFruit +>a : string +>args : any[] + +const banana2 = fruitFactory2(Banana) // Banana +>banana2 : Banana +>fruitFactory2(Banana) : Banana +>fruitFactory2 : (Fruit: new (a: string, ...args: any[]) => TFruit) => TFruit +>Banana : typeof Banana + +declare function fruitFactory3(Fruit: new (a: string, s: "foo", ...args: any[]) => TFruit): TFruit +>fruitFactory3 : (Fruit: new (a: string, s: "foo", ...args: any[]) => TFruit) => TFruit +>Fruit : new (a: string, s: "foo", ...args: any[]) => TFruit +>a : string +>s : "foo" +>args : any[] + +const banana3 = fruitFactory3(Banana) // Banana<"foo"> +>banana3 : Banana<"foo"> +>fruitFactory3(Banana) : Banana<"foo"> +>fruitFactory3 : (Fruit: new (a: string, s: "foo", ...args: any[]) => TFruit) => TFruit +>Banana : typeof Banana + +declare function fruitFactory4(Fruit: new (a: string, ...args: "foo"[]) => TFruit): TFruit +>fruitFactory4 : (Fruit: new (a: string, ...args: "foo"[]) => TFruit) => TFruit +>Fruit : new (a: string, ...args: "foo"[]) => TFruit +>a : string +>args : "foo"[] + +const banana4 = fruitFactory4(Banana) // Banana<"foo"> +>banana4 : Banana<"foo"> +>fruitFactory4(Banana) : Banana<"foo"> +>fruitFactory4 : (Fruit: new (a: string, ...args: "foo"[]) => TFruit) => TFruit +>Banana : typeof Banana + +declare function fruitFactory5(Fruit: new (...args: "foo"[]) => TFruit): TFruit +>fruitFactory5 : (Fruit: new (...args: "foo"[]) => TFruit) => TFruit +>Fruit : new (...args: "foo"[]) => TFruit +>args : "foo"[] + +const banana5 = fruitFactory5(Banana) // Banana<"foo"> +>banana5 : Banana<"foo"> +>fruitFactory5(Banana) : Banana<"foo"> +>fruitFactory5 : (Fruit: new (...args: "foo"[]) => TFruit) => TFruit +>Banana : typeof Banana + From 052a3d9d73a009e11f26fa8ba99f6e72c63b5e6d Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Thu, 5 Sep 2019 16:16:35 -0700 Subject: [PATCH 47/97] Infer void from expr statement usage, not calls This makes inferences a lot better. --- src/services/codefixes/inferFromUsage.ts | 8 +++++--- .../cases/fourslash/codeFixInferFromCallInAssignment.ts | 9 +++++++++ .../fourslash/codeFixInferFromExpressionStatement.ts | 8 ++++++++ .../codeFixInferFromUsageCommentAfterParameter.ts | 2 +- tests/cases/fourslash/codeFixInferFromUsageJSXElement.ts | 2 +- .../fourslash/codeFixInferFromUsagePropertyAccess.ts | 2 +- .../fourslash/codeFixInferFromUsagePropertyAccessJS.ts | 2 +- 7 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 tests/cases/fourslash/codeFixInferFromCallInAssignment.ts create mode 100644 tests/cases/fourslash/codeFixInferFromExpressionStatement.ts diff --git a/src/services/codefixes/inferFromUsage.ts b/src/services/codefixes/inferFromUsage.ts index 0a190e7b379..daa2166103a 100644 --- a/src/services/codefixes/inferFromUsage.ts +++ b/src/services/codefixes/inferFromUsage.ts @@ -504,6 +504,9 @@ namespace ts.codefix { } switch (node.parent.kind) { + case SyntaxKind.ExpressionStatement: + addCandidateType(usage, checker.getVoidType()); + break; case SyntaxKind.PostfixUnaryExpression: usage.isNumber = true; break; @@ -871,12 +874,11 @@ namespace ts.codefix { } if (usage.calls) { - callSignatures.push(getSignatureFromCalls(usage.calls, checker.getVoidType())); + callSignatures.push(getSignatureFromCalls(usage.calls, checker.getAnyType())); } if (usage.constructs) { - // TODO: fallback return should maybe be {}? - constructSignatures.push(getSignatureFromCalls(usage.constructs, checker.getVoidType())); + constructSignatures.push(getSignatureFromCalls(usage.constructs, checker.getAnyType())); } if (usage.stringIndex) { diff --git a/tests/cases/fourslash/codeFixInferFromCallInAssignment.ts b/tests/cases/fourslash/codeFixInferFromCallInAssignment.ts new file mode 100644 index 00000000000..85e1d3bc509 --- /dev/null +++ b/tests/cases/fourslash/codeFixInferFromCallInAssignment.ts @@ -0,0 +1,9 @@ +/// + +// @noImplicitAny: true +//// function inferAny( [| app |] ) { +//// const result = app.use('hi') +//// return result +//// } + +verify.rangeAfterCodeFix("app: { use: (arg0: string) => any; }"); diff --git a/tests/cases/fourslash/codeFixInferFromExpressionStatement.ts b/tests/cases/fourslash/codeFixInferFromExpressionStatement.ts new file mode 100644 index 00000000000..b5969c79378 --- /dev/null +++ b/tests/cases/fourslash/codeFixInferFromExpressionStatement.ts @@ -0,0 +1,8 @@ +/// + +// @noImplicitAny: true +//// function inferVoid( [| app |] ) { +//// app.use('hi') +//// } + +verify.rangeAfterCodeFix("app: { use: (arg0: string) => void; }"); diff --git a/tests/cases/fourslash/codeFixInferFromUsageCommentAfterParameter.ts b/tests/cases/fourslash/codeFixInferFromUsageCommentAfterParameter.ts index bdefd2b5dea..866bfe04c13 100644 --- a/tests/cases/fourslash/codeFixInferFromUsageCommentAfterParameter.ts +++ b/tests/cases/fourslash/codeFixInferFromUsageCommentAfterParameter.ts @@ -16,7 +16,7 @@ verify.codeFix({ index: 0, newFileContent: `/** - * @param {(arg0: any) => void} callback + * @param {(arg0: any) => any} callback */ function coll(callback /*, name1, name2, ... */) { return callback(this); diff --git a/tests/cases/fourslash/codeFixInferFromUsageJSXElement.ts b/tests/cases/fourslash/codeFixInferFromUsageJSXElement.ts index 3200fffafb8..abe35fe46c8 100644 --- a/tests/cases/fourslash/codeFixInferFromUsageJSXElement.ts +++ b/tests/cases/fourslash/codeFixInferFromUsageJSXElement.ts @@ -30,4 +30,4 @@ //// } -verify.rangeAfterCodeFix("props: { isLoading: any; update: (arg0: any) => void; }",/*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, 0); +verify.rangeAfterCodeFix("props: { isLoading: any; update: (arg0: any) => any; }",/*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, 0); diff --git a/tests/cases/fourslash/codeFixInferFromUsagePropertyAccess.ts b/tests/cases/fourslash/codeFixInferFromUsagePropertyAccess.ts index c8b85ee11e8..44d2e2d7b1c 100644 --- a/tests/cases/fourslash/codeFixInferFromUsagePropertyAccess.ts +++ b/tests/cases/fourslash/codeFixInferFromUsagePropertyAccess.ts @@ -12,4 +12,4 @@ //// return x.y.z ////} -verify.rangeAfterCodeFix("a: { b: { c: any; }; }, m: { n: () => number; }, x: { y: { z: number[]; }; }", /*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, /*index*/0); \ No newline at end of file +verify.rangeAfterCodeFix("a: { b: { c: void; }; }, m: { n: () => number; }, x: { y: { z: number[]; }; }", /*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, /*index*/0); diff --git a/tests/cases/fourslash/codeFixInferFromUsagePropertyAccessJS.ts b/tests/cases/fourslash/codeFixInferFromUsagePropertyAccessJS.ts index ed9d4b33b37..357b1a991ba 100644 --- a/tests/cases/fourslash/codeFixInferFromUsagePropertyAccessJS.ts +++ b/tests/cases/fourslash/codeFixInferFromUsagePropertyAccessJS.ts @@ -21,7 +21,7 @@ verify.codeFix({ index: 0, newFileContent: `/** - * @param {{ b: { c: any; }; }} a + * @param {{ b: { c: void; }; }} a * @param {{ n: () => number; }} m * @param {{ y: { z: number[]; }; }} x */ From d32c6b2df1e1988c9565b4a7d08e422337540c2a Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Thu, 5 Sep 2019 16:21:08 -0700 Subject: [PATCH 48/97] Fallback type is always any now void is explicitly inferred now, never used as a fallback. --- src/services/codefixes/inferFromUsage.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/services/codefixes/inferFromUsage.ts b/src/services/codefixes/inferFromUsage.ts index daa2166103a..b453535b462 100644 --- a/src/services/codefixes/inferFromUsage.ts +++ b/src/services/codefixes/inferFromUsage.ts @@ -767,8 +767,8 @@ namespace ts.codefix { return inferences.filter(i => toRemove.every(f => !f(i))); } - function unifyTypes(inferences: ReadonlyArray, fallback = checker.getAnyType()): Type { - if (!inferences.length) return fallback; + function unifyTypes(inferences: ReadonlyArray): Type { + if (!inferences.length) return checker.getAnyType(); // 1. string or number individually override string | number // 2. non-any, non-void overrides any or void @@ -874,11 +874,11 @@ namespace ts.codefix { } if (usage.calls) { - callSignatures.push(getSignatureFromCalls(usage.calls, checker.getAnyType())); + callSignatures.push(getSignatureFromCalls(usage.calls)); } if (usage.constructs) { - constructSignatures.push(getSignatureFromCalls(usage.constructs, checker.getAnyType())); + constructSignatures.push(getSignatureFromCalls(usage.constructs)); } if (usage.stringIndex) { @@ -1047,10 +1047,10 @@ namespace ts.codefix { } function getFunctionFromCalls(calls: CallUsage[]) { - return checker.createAnonymousType(undefined!, createSymbolTable(), [getSignatureFromCalls(calls, checker.getAnyType())], emptyArray, undefined, undefined); + return checker.createAnonymousType(undefined!, createSymbolTable(), [getSignatureFromCalls(calls)], emptyArray, undefined, undefined); } - function getSignatureFromCalls(calls: CallUsage[], fallbackReturn: Type): Signature { + function getSignatureFromCalls(calls: CallUsage[]): Signature { const parameters: Symbol[] = []; const length = Math.max(...calls.map(c => c.argumentTypes.length)); for (let i = 0; i < length; i++) { @@ -1061,7 +1061,7 @@ namespace ts.codefix { } parameters.push(symbol); } - const returnType = unifyTypes(inferFromUsage(combineUsages(calls.map(call => call.return_))), fallbackReturn); + const returnType = unifyTypes(inferFromUsage(combineUsages(calls.map(call => call.return_)))); // TODO: GH#18217 return checker.createSignature(/*declaration*/ undefined!, /*typeParameters*/ undefined, /*thisParameter*/ undefined, parameters, returnType, /*typePredicate*/ undefined, length, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false); } From f394190572fb76b194df5f093f1af51abc1ffb98 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Fri, 6 Sep 2019 11:18:50 -0700 Subject: [PATCH 49/97] Tonnes of cleanup --- src/services/codefixes/inferFromUsage.ts | 167 ++++++++++++----------- 1 file changed, 87 insertions(+), 80 deletions(-) diff --git a/src/services/codefixes/inferFromUsage.ts b/src/services/codefixes/inferFromUsage.ts index b453535b462..2e9fb0dfc72 100644 --- a/src/services/codefixes/inferFromUsage.ts +++ b/src/services/codefixes/inferFromUsage.ts @@ -433,6 +433,53 @@ namespace ts.codefix { inferredTypes: Type[] | undefined; } + function createEmptyUsage(): Usage { + return { + isNumber: undefined, + isString: undefined, + isNumberOrString: undefined, + candidateTypes: undefined, + properties: undefined, + calls: undefined, + constructs: undefined, + numberIndex: undefined, + stringIndex: undefined, + candidateThisTypes: undefined, + inferredTypes: undefined, + }; + } + + function combineUsages(usages: Usage[]): Usage { + const combinedProperties = createUnderscoreEscapedMap(); + for (const u of usages) { + if (u.properties) { + u.properties.forEach((p, name) => { + if (!combinedProperties.has(name)) { + combinedProperties.set(name, []); + } + combinedProperties.get(name)!.push(p); + }); + } + } + const properties = createUnderscoreEscapedMap(); + combinedProperties.forEach((ps, name) => { + properties.set(name, combineUsages(ps)); + }); + return { + isNumber: usages.some(u => u.isNumber), + isString: usages.some(u => u.isString), + isNumberOrString: usages.some(u => u.isNumberOrString), + candidateTypes: flatMap(usages, u => u.candidateTypes) as Type[], + properties, + calls: flatMap(usages, u => u.calls) as CallUsage[], + constructs: flatMap(usages, u => u.constructs) as CallUsage[], + numberIndex: forEach(usages, u => u.numberIndex), + stringIndex: forEach(usages, u => u.stringIndex), + candidateThisTypes: flatMap(usages, u => u.candidateThisTypes) as Type[], + inferredTypes: undefined, // clear type cache + }; + } + function single(): Type { return unifyTypes(inferTypesFromReferencesSingle(references)); } @@ -767,6 +814,10 @@ namespace ts.codefix { return inferences.filter(i => toRemove.every(f => !f(i))); } + function unifyFromUsage(usage: Usage) { + return unifyTypes(inferFromUsage(usage)); + } + function unifyTypes(inferences: ReadonlyArray): Type { if (!inferences.length) return checker.getAnyType(); @@ -837,7 +888,7 @@ namespace ts.codefix { numberIndices.length ? checker.createIndexInfo(checker.getUnionType(numberIndices), numberIndexReadonly) : undefined); } - function inferFromUsage(usage: Usage) { + function inferFromUsage(usage: Usage): Type[] { const types = []; if (usage.isNumber) { @@ -851,12 +902,20 @@ namespace ts.codefix { } types.push(...(usage.candidateTypes || []).map(t => checker.getBaseTypeOfLiteralType(t))); - types.push(...findBuiltinTypes(usage)); + types.push(...inferNamedTypesFromProperties(usage)); if (usage.numberIndex) { - types.push(checker.createArrayType(recur(usage.numberIndex))); + types.push(checker.createArrayType(unifyFromUsage(usage.numberIndex))); } - else if (usage.properties && usage.properties.size + const structural = inferStructuralType(usage); + if (structural) { + types.push(structural); + } + return types; + } + + function inferStructuralType(usage: Usage) { + if (usage.properties && usage.properties.size || usage.calls && usage.calls.length || usage.constructs && usage.constructs.length || usage.stringIndex) { @@ -868,7 +927,7 @@ namespace ts.codefix { if (usage.properties) { usage.properties.forEach((u, name) => { const symbol = checker.createSymbol(SymbolFlags.Property, name); - symbol.type = recur(u); + symbol.type = unifyFromUsage(u); members.set(name, symbol); }); } @@ -882,75 +941,23 @@ namespace ts.codefix { } if (usage.stringIndex) { - stringIndexInfo = checker.createIndexInfo(recur(usage.stringIndex), /*isReadonly*/ false); + stringIndexInfo = checker.createIndexInfo(unifyFromUsage(usage.stringIndex), /*isReadonly*/ false); } - types.push(checker.createAnonymousType(/*symbol*/ undefined!, members, callSignatures, constructSignatures, stringIndexInfo, /*numberIndexInfo*/ undefined)); // TODO: GH#18217 - } - return types; // TODO: Should cache this since I HOPE it doesn't change - - function recur(innerUsage: Usage): Type { - return unifyTypes(inferFromUsage(innerUsage)); + return checker.createAnonymousType(/*symbol*/ undefined!, members, callSignatures, constructSignatures, stringIndexInfo, /*numberIndexInfo*/ undefined); // TODO: GH#18217 } } - function createEmptyUsage(): Usage { - return { - isNumber: undefined, - isString: undefined, - isNumberOrString: undefined, - candidateTypes: undefined, - properties: undefined, - calls: undefined, - constructs: undefined, - numberIndex: undefined, - stringIndex: undefined, - candidateThisTypes: undefined, - inferredTypes: undefined, - } - } - - function combineUsages(usages: Usage[]): Usage { - const combinedProperties = createUnderscoreEscapedMap() - for (const u of usages) { - if (u.properties) { - u.properties.forEach((p,name) => { - if (!combinedProperties.has(name)) { - combinedProperties.set(name, []); - } - combinedProperties.get(name)!.push(p); - }); - } - } - const properties = createUnderscoreEscapedMap() - combinedProperties.forEach((ps,name) => { - properties.set(name, combineUsages(ps)); - }); - return { - isNumber: usages.some(u => u.isNumber), - isString: usages.some(u => u.isString), - isNumberOrString: usages.some(u => u.isNumberOrString), - candidateTypes: flatMap(usages, u => u.candidateTypes) as Type[], - properties, - calls: flatMap(usages, u => u.calls) as CallUsage[], - constructs: flatMap(usages, u => u.constructs) as CallUsage[], - numberIndex: forEach(usages, u => u.numberIndex), - stringIndex: forEach(usages, u => u.stringIndex), - candidateThisTypes: flatMap(usages, u => u.candidateThisTypes) as Type[], - inferredTypes: undefined, // clear type cache - } - } - - function findBuiltinTypes(usage: Usage): Type[] { + function inferNamedTypesFromProperties(usage: Usage): Type[] { if (!usage.properties || !usage.properties.size) return []; - const matches = builtins.filter(t => matchesAllPropertiesOf(t, usage)); + const matches = builtins.filter(t => allPropertiesAreAssignableToUsage(t, usage)); if (0 < matches.length && matches.length < 3) { - return matches.map(m => inferTypeParameterFromUsage(m, usage)); + return matches.map(m => inferInstantiationFromUsage(m, usage)); } return []; } - function matchesAllPropertiesOf(type: Type, usage: Usage) { + function allPropertiesAreAssignableToUsage(type: Type, usage: Usage) { if (!usage.properties) return false; let result = true; usage.properties.forEach((propUsage, name) => { @@ -960,34 +967,34 @@ namespace ts.codefix { return; } if (propUsage.calls) { - const sigs = checker.getSignaturesOfType(source, ts.SignatureKind.Call); + const sigs = checker.getSignaturesOfType(source, SignatureKind.Call); result = result && !!sigs.length && checker.isTypeAssignableTo(source, getFunctionFromCalls(propUsage.calls)); } else { - result = result && checker.isTypeAssignableTo(source, unifyTypes(inferFromUsage(propUsage))); + result = result && checker.isTypeAssignableTo(source, unifyFromUsage(propUsage)); } }); return result; } - // inference is limited to - // 1. generic types with a single parameter - // 2. inference to/from calls with a single signature - function inferTypeParameterFromUsage(type: Type, usage: Usage) { - if (!usage.properties || !(getObjectFlags(type) & ObjectFlags.Reference)) return type; + /** + * inference is limited to + * 1. generic types with a single parameter + * 2. inference to/from calls with a single signature + */ + function inferInstantiationFromUsage(type: Type, usage: Usage) { + if (!(getObjectFlags(type) & ObjectFlags.Reference) || !usage.properties) { + return type; + } const generic = (type as TypeReference).target; const singleTypeParameter = singleOrUndefined(generic.typeParameters); if (!singleTypeParameter) return type; const types: Type[] = []; usage.properties.forEach((propUsage, name) => { - const source = checker.getTypeOfPropertyOfType(generic, name as string); - if (!source) { - return Debug.fail("generic should have all the properties of its reference."); - } - if (!propUsage.calls) return; - - types.push(...infer(source, getFunctionFromCalls(propUsage.calls), singleTypeParameter)); + const genericPropertyType = checker.getTypeOfPropertyOfType(generic, name as string); + Debug.assert(!!genericPropertyType, "generic should have all the properties of its reference."); + types.push(...infer(genericPropertyType!, unifyFromUsage(propUsage), singleTypeParameter)); }); return builtinConstructors[type.symbol.escapedName as string](unifyTypes(types)); } @@ -1033,7 +1040,7 @@ namespace ts.codefix { break; } let sourceType = checker.getTypeOfSymbolAtLocation(sourceParam, sourceParam.valueDeclaration); - let elementType = isRest && checker.getElementTypeOfArrayType(sourceType); + const elementType = isRest && checker.getElementTypeOfArrayType(sourceType); if (elementType) { sourceType = elementType; } @@ -1047,7 +1054,7 @@ namespace ts.codefix { } function getFunctionFromCalls(calls: CallUsage[]) { - return checker.createAnonymousType(undefined!, createSymbolTable(), [getSignatureFromCalls(calls)], emptyArray, undefined, undefined); + return checker.createAnonymousType(undefined!, createSymbolTable(), [getSignatureFromCalls(calls)], emptyArray, /*stringIndexInfo*/ undefined, /*numberIndexInfo*/ undefined); } function getSignatureFromCalls(calls: CallUsage[]): Signature { @@ -1061,7 +1068,7 @@ namespace ts.codefix { } parameters.push(symbol); } - const returnType = unifyTypes(inferFromUsage(combineUsages(calls.map(call => call.return_)))); + const returnType = unifyFromUsage(combineUsages(calls.map(call => call.return_))); // TODO: GH#18217 return checker.createSignature(/*declaration*/ undefined!, /*typeParameters*/ undefined, /*thisParameter*/ undefined, parameters, returnType, /*typePredicate*/ undefined, length, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false); } From 1703ae0f46407beb4701e7234c9de558b20e17c0 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Fri, 6 Sep 2019 11:27:34 -0700 Subject: [PATCH 50/97] Renames and more cleanup --- src/services/codefixes/inferFromUsage.ts | 63 +++++++++++------------- 1 file changed, 30 insertions(+), 33 deletions(-) diff --git a/src/services/codefixes/inferFromUsage.ts b/src/services/codefixes/inferFromUsage.ts index 2e9fb0dfc72..0c711cfa4c4 100644 --- a/src/services/codefixes/inferFromUsage.ts +++ b/src/services/codefixes/inferFromUsage.ts @@ -743,7 +743,6 @@ namespace ts.codefix { if (parent.arguments) { for (const argument of parent.arguments) { - // TODO: should recursively infer a usage here, right? call.argumentTypes.push(checker.getTypeAtLocation(argument)); } } @@ -999,57 +998,55 @@ namespace ts.codefix { return builtinConstructors[type.symbol.escapedName as string](unifyTypes(types)); } - // TODO: Source and target are bad names. Should be builtinType and usageType...or something - // and search is a bad name - function infer(source: Type, target: Type, search: Type): readonly Type[] { - if (source === search) { - return [target]; + function infer(genericType: Type, usageType: Type, typeParameter: Type): readonly Type[] { + if (genericType === typeParameter) { + return [usageType]; } - else if (source.flags & TypeFlags.UnionOrIntersection) { - return flatMap((source as UnionOrIntersectionType).types, t => infer(t, target, search)); + else if (genericType.flags & TypeFlags.UnionOrIntersection) { + return flatMap((genericType as UnionOrIntersectionType).types, t => infer(t, usageType, typeParameter)); } - else if (getObjectFlags(source) & ObjectFlags.Reference && getObjectFlags(target) & ObjectFlags.Reference) { + else if (getObjectFlags(genericType) & ObjectFlags.Reference && getObjectFlags(usageType) & ObjectFlags.Reference) { // this is wrong because we need a reference to the targetType to, so we can check that it's also a reference - const sourceArgs = (source as TypeReference).typeArguments; - const targetArgs = (target as TypeReference).typeArguments; + const genericArgs = (genericType as TypeReference).typeArguments; + const usageArgs = (usageType as TypeReference).typeArguments; const types = []; - if (sourceArgs && targetArgs) { - for (let i = 0; i < sourceArgs.length; i++) { - if (targetArgs[i]) { - types.push(...infer(sourceArgs[i], targetArgs[i], search)); + if (genericArgs && usageArgs) { + for (let i = 0; i < genericArgs.length; i++) { + if (usageArgs[i]) { + types.push(...infer(genericArgs[i], usageArgs[i], typeParameter)); } } } return types; } - const sourceSigs = checker.getSignaturesOfType(source, SignatureKind.Call); - const targetSigs = checker.getSignaturesOfType(target, SignatureKind.Call); - if (sourceSigs.length === 1 && targetSigs.length === 1) { - return inferFromSignatures(sourceSigs[0], targetSigs[0], search); + const genericSigs = checker.getSignaturesOfType(genericType, SignatureKind.Call); + const usageSigs = checker.getSignaturesOfType(usageType, SignatureKind.Call); + if (genericSigs.length === 1 && usageSigs.length === 1) { + return inferFromSignatures(genericSigs[0], usageSigs[0], typeParameter); } return []; } - function inferFromSignatures(sourceSig: Signature, targetSig: Signature, search: Type) { + function inferFromSignatures(genericSig: Signature, usageSig: Signature, typeParameter: Type) { const types = []; - for (let i = 0; i < sourceSig.parameters.length; i++) { - const sourceParam = sourceSig.parameters[i]; - const targetParam = targetSig.parameters[i]; - const isRest = sourceSig.declaration && isRestParameter(sourceSig.declaration.parameters[i]); - if (!targetParam) { + for (let i = 0; i < genericSig.parameters.length; i++) { + const genericParam = genericSig.parameters[i]; + const usageParam = usageSig.parameters[i]; + const isRest = genericSig.declaration && isRestParameter(genericSig.declaration.parameters[i]); + if (!usageParam) { break; } - let sourceType = checker.getTypeOfSymbolAtLocation(sourceParam, sourceParam.valueDeclaration); - const elementType = isRest && checker.getElementTypeOfArrayType(sourceType); + let genericParamType = checker.getTypeOfSymbolAtLocation(genericParam, genericParam.valueDeclaration); + const elementType = isRest && checker.getElementTypeOfArrayType(genericParamType); if (elementType) { - sourceType = elementType; + genericParamType = elementType; } - const targetType = (targetParam as SymbolLinks).type || checker.getTypeOfSymbolAtLocation(targetParam, targetParam.valueDeclaration); - types.push(...infer(sourceType, targetType, search)); + const targetType = (usageParam as SymbolLinks).type || checker.getTypeOfSymbolAtLocation(usageParam, usageParam.valueDeclaration); + types.push(...infer(genericParamType, targetType, typeParameter)); } - const sourceReturn = checker.getReturnTypeOfSignature(sourceSig); - const targetReturn = checker.getReturnTypeOfSignature(targetSig); - types.push(...infer(sourceReturn, targetReturn, search)); + const genericReturn = checker.getReturnTypeOfSignature(genericSig); + const usageReturn = checker.getReturnTypeOfSignature(usageSig); + types.push(...infer(genericReturn, usageReturn, typeParameter)); return types; } From 330e51f09811a41bddd71bfe2eea3baa4e6651bc Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Fri, 6 Sep 2019 15:15:19 -0700 Subject: [PATCH 51/97] Add test + reshuffle/rename new code --- src/services/codefixes/inferFromUsage.ts | 63 +++++++------------ .../codeFixInferFromUsageAddition.ts | 9 +++ .../fourslash/codeFixInferFromUsageArray.ts | 14 +++++ .../codeFixInferFromUsageLiteralTypes.ts | 10 +++ .../fourslash/codeFixInferFromUsagePromise.ts | 9 +++ .../fourslash/codeFixInferFromUsageString.ts | 12 ++++ 6 files changed, 77 insertions(+), 40 deletions(-) create mode 100644 tests/cases/fourslash/codeFixInferFromUsageAddition.ts create mode 100644 tests/cases/fourslash/codeFixInferFromUsageArray.ts create mode 100644 tests/cases/fourslash/codeFixInferFromUsageLiteralTypes.ts create mode 100644 tests/cases/fourslash/codeFixInferFromUsagePromise.ts create mode 100644 tests/cases/fourslash/codeFixInferFromUsageString.ts diff --git a/src/services/codefixes/inferFromUsage.ts b/src/services/codefixes/inferFromUsage.ts index 0c711cfa4c4..520f8520d21 100644 --- a/src/services/codefixes/inferFromUsage.ts +++ b/src/services/codefixes/inferFromUsage.ts @@ -899,59 +899,42 @@ namespace ts.codefix { if (usage.isNumberOrString) { types.push(checker.getUnionType([checker.getStringType(), checker.getNumberType()])); } + if (usage.numberIndex) { + types.push(checker.createArrayType(unifyFromUsage(usage.numberIndex))); + } + if (usage.properties && usage.properties.size + || usage.calls && usage.calls.length + || usage.constructs && usage.constructs.length + || usage.stringIndex) { + types.push(inferStructuralType(usage)); + } types.push(...(usage.candidateTypes || []).map(t => checker.getBaseTypeOfLiteralType(t))); types.push(...inferNamedTypesFromProperties(usage)); - if (usage.numberIndex) { - types.push(checker.createArrayType(unifyFromUsage(usage.numberIndex))); - } - const structural = inferStructuralType(usage); - if (structural) { - types.push(structural); - } return types; } function inferStructuralType(usage: Usage) { - if (usage.properties && usage.properties.size - || usage.calls && usage.calls.length - || usage.constructs && usage.constructs.length - || usage.stringIndex) { - const members = createUnderscoreEscapedMap(); - const callSignatures: Signature[] = []; - const constructSignatures: Signature[] = []; - let stringIndexInfo: IndexInfo | undefined; - - if (usage.properties) { - usage.properties.forEach((u, name) => { - const symbol = checker.createSymbol(SymbolFlags.Property, name); - symbol.type = unifyFromUsage(u); - members.set(name, symbol); - }); - } - - if (usage.calls) { - callSignatures.push(getSignatureFromCalls(usage.calls)); - } - - if (usage.constructs) { - constructSignatures.push(getSignatureFromCalls(usage.constructs)); - } - - if (usage.stringIndex) { - stringIndexInfo = checker.createIndexInfo(unifyFromUsage(usage.stringIndex), /*isReadonly*/ false); - } - - return checker.createAnonymousType(/*symbol*/ undefined!, members, callSignatures, constructSignatures, stringIndexInfo, /*numberIndexInfo*/ undefined); // TODO: GH#18217 + const members = createUnderscoreEscapedMap(); + if (usage.properties) { + usage.properties.forEach((u, name) => { + const symbol = checker.createSymbol(SymbolFlags.Property, name); + symbol.type = unifyFromUsage(u); + members.set(name, symbol); + }); } + const callSignatures: Signature[] = usage.calls ? [getSignatureFromCalls(usage.calls)] : []; + const constructSignatures: Signature[] = usage.constructs ? [getSignatureFromCalls(usage.constructs)] : []; + const stringIndexInfo = usage.stringIndex && checker.createIndexInfo(unifyFromUsage(usage.stringIndex), /*isReadonly*/ false); + return checker.createAnonymousType(/*symbol*/ undefined!, members, callSignatures, constructSignatures, stringIndexInfo, /*numberIndexInfo*/ undefined); // TODO: GH#18217 } function inferNamedTypesFromProperties(usage: Usage): Type[] { if (!usage.properties || !usage.properties.size) return []; - const matches = builtins.filter(t => allPropertiesAreAssignableToUsage(t, usage)); - if (0 < matches.length && matches.length < 3) { - return matches.map(m => inferInstantiationFromUsage(m, usage)); + const types = builtins.filter(t => allPropertiesAreAssignableToUsage(t, usage)); + if (0 < types.length && types.length < 3) { + return types.map(t => inferInstantiationFromUsage(t, usage)); } return []; } diff --git a/tests/cases/fourslash/codeFixInferFromUsageAddition.ts b/tests/cases/fourslash/codeFixInferFromUsageAddition.ts new file mode 100644 index 00000000000..4b57d0603f1 --- /dev/null +++ b/tests/cases/fourslash/codeFixInferFromUsageAddition.ts @@ -0,0 +1,9 @@ +/// + +// @noImplicitAny: true +//// function foo([|a, m |]) { +//// return a + m +//// } + +verify.rangeAfterCodeFix("a: any, m: any", /*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, /*index*/0); + diff --git a/tests/cases/fourslash/codeFixInferFromUsageArray.ts b/tests/cases/fourslash/codeFixInferFromUsageArray.ts new file mode 100644 index 00000000000..03d6b09e99b --- /dev/null +++ b/tests/cases/fourslash/codeFixInferFromUsageArray.ts @@ -0,0 +1,14 @@ +/// + +// @noImplicitAny: true +//// function foo([|p, a, b, c, d, e |]) { +//// var x: string = a.pop() +//// b.reverse() +//// var rr: boolean[] = c.reverse() +//// d.some(t => t > 1); // can't infer from callbacks right now +//// var y = e.concat(12); // can't infer from overloaded functions right now +//// return p.push(12) +//// } + +verify.rangeAfterCodeFix("p: number[], a: string[], b: any[], c: boolean[], d: any[], e: number[]", /*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, /*index*/0); + diff --git a/tests/cases/fourslash/codeFixInferFromUsageLiteralTypes.ts b/tests/cases/fourslash/codeFixInferFromUsageLiteralTypes.ts new file mode 100644 index 00000000000..3b8c9441159 --- /dev/null +++ b/tests/cases/fourslash/codeFixInferFromUsageLiteralTypes.ts @@ -0,0 +1,10 @@ +/// + +// @noImplicitAny: true +//// function foo([|a, m |]) { +//// a = 'hi' +//// m = 1 +//// } + +verify.rangeAfterCodeFix("a: string, m: number", /*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, /*index*/0); + diff --git a/tests/cases/fourslash/codeFixInferFromUsagePromise.ts b/tests/cases/fourslash/codeFixInferFromUsagePromise.ts new file mode 100644 index 00000000000..d06bb3b6a0f --- /dev/null +++ b/tests/cases/fourslash/codeFixInferFromUsagePromise.ts @@ -0,0 +1,9 @@ +/// + +// @noImplicitAny: true +//// function foo([|p |]) { +//// return p.then((x: string[]) => x[0]) +//// } + +verify.rangeAfterCodeFix("p: Promise", /*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, /*index*/0); + diff --git a/tests/cases/fourslash/codeFixInferFromUsageString.ts b/tests/cases/fourslash/codeFixInferFromUsageString.ts new file mode 100644 index 00000000000..dc5787dc75b --- /dev/null +++ b/tests/cases/fourslash/codeFixInferFromUsageString.ts @@ -0,0 +1,12 @@ +/// + +// @noImplicitAny: true +//// function foo([|p, a, b, c, d |]) { +//// var x +//// p.charAt(x) +//// a.charAt(0) +//// b.concat('hi') +//// } + +verify.rangeAfterCodeFix("p: string, a: string, b: string | string[]", /*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, /*index*/0); + From 21528748c6dd7d46a9a2c428a302495debba119e Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Tue, 10 Sep 2019 16:24:00 -0700 Subject: [PATCH 52/97] Address CR feedback --- src/compiler/binder.ts | 3 ++- src/compiler/checker.ts | 24 +++++++++++++----------- src/compiler/types.ts | 13 +++++++++---- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index b73431f6f56..df312b991c6 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -1281,7 +1281,8 @@ namespace ts { function isDottedName(node: Expression): boolean { return node.kind === SyntaxKind.Identifier || node.kind === SyntaxKind.ThisKeyword || - node.kind === SyntaxKind.PropertyAccessExpression && isDottedName((node).expression); + node.kind === SyntaxKind.PropertyAccessExpression && isDottedName((node).expression) || + node.kind === SyntaxKind.ParenthesizedExpression && isDottedName((node).expression); } function bindExpressionStatement(node: ExpressionStatement): void { diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 0af56d7f172..bb59a6143fd 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -16852,7 +16852,7 @@ namespace ts { return !!(declaration && ( declaration.kind === SyntaxKind.VariableDeclaration || declaration.kind === SyntaxKind.Parameter || declaration.kind === SyntaxKind.PropertyDeclaration || declaration.kind === SyntaxKind.PropertySignature) && - (declaration as VariableDeclaration | ParameterDeclaration | PropertyDeclaration | PropertySignature).type); + getEffectiveTypeAnnotationNode(declaration as VariableDeclaration | ParameterDeclaration | PropertyDeclaration | PropertySignature)); } function getExplicitTypeOfSymbol(symbol: Symbol) { @@ -16861,11 +16861,11 @@ namespace ts { getTypeOfSymbol(symbol) : undefined; } - function getTypeOfDottedName(node: Expression) { - // We require the dotted function name in an assertion expression to be comprised of identifiers - // that reference function, method, class or value module symbols; or variable, property or - // parameter symbols with declarations that have explicit type annotations. Such references are - // resolvable with no possibility of triggering circularities in control flow analysis. + // We require the dotted function name in an assertion expression to be comprised of identifiers + // that reference function, method, class or value module symbols; or variable, property or + // parameter symbols with declarations that have explicit type annotations. Such references are + // resolvable with no possibility of triggering circularities in control flow analysis. + function getTypeOfDottedName(node: Expression): Type | undefined { switch (node.kind) { case SyntaxKind.Identifier: const symbol = getResolvedSymbol(node); @@ -16874,10 +16874,10 @@ namespace ts { return checkThisExpression(node); case SyntaxKind.PropertyAccessExpression: const type = getTypeOfDottedName((node).expression); - if (type) { - const prop = getPropertyOfType(type, (node).name.escapedText); - return prop && getExplicitTypeOfSymbol(prop); - } + const prop = type && getPropertyOfType(type, (node).name.escapedText); + return prop && getExplicitTypeOfSymbol(prop); + case SyntaxKind.ParenthesizedExpression: + return getTypeOfDottedName((node).expression); } } @@ -16886,7 +16886,9 @@ namespace ts { let signature = links.effectsSignature; if (signature === undefined) { // A call expression parented by an expression statement is a potential assertion. Other call - // expressions are potential type predicate function calls. + // expressions are potential type predicate function calls. In order to avoid triggering + // circularities in control flow analysis, we use getTypeOfDottedName when resolving the call + // target expression of an assertion. const funcType = node.parent.kind === SyntaxKind.ExpressionStatement ? getTypeOfDottedName(node.expression) : node.expression.kind !== SyntaxKind.SuperKeyword ? checkNonNullExpression(node.expression) : undefined; diff --git a/src/compiler/types.ts b/src/compiler/types.ts index b27de58134a..b4e4769ab47 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -3518,28 +3518,33 @@ namespace ts { AssertsIdentifier } - export interface ThisTypePredicate { + export interface TypePredicateBase { + kind: TypePredicateKind; + type: Type | undefined; + } + + export interface ThisTypePredicate extends TypePredicateBase { kind: TypePredicateKind.This; parameterName: undefined; parameterIndex: undefined; type: Type; } - export interface IdentifierTypePredicate { + export interface IdentifierTypePredicate extends TypePredicateBase { kind: TypePredicateKind.Identifier; parameterName: string; parameterIndex: number; type: Type; } - export interface AssertsThisTypePredicate { + export interface AssertsThisTypePredicate extends TypePredicateBase { kind: TypePredicateKind.AssertsThis; parameterName: undefined; parameterIndex: undefined; type: Type | undefined; } - export interface AssertsIdentifierTypePredicate { + export interface AssertsIdentifierTypePredicate extends TypePredicateBase { kind: TypePredicateKind.AssertsIdentifier; parameterName: string; parameterIndex: number; From 8791b62c9614b83f23928aa0208fbfe07f508904 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Tue, 10 Sep 2019 22:26:36 -0700 Subject: [PATCH 53/97] Accept new baselines --- tests/baselines/reference/api/tsserverlibrary.d.ts | 12 ++++++++---- tests/baselines/reference/api/typescript.d.ts | 12 ++++++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index c458e88300b..35f3d7a7a54 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -2097,25 +2097,29 @@ declare namespace ts { AssertsThis = 2, AssertsIdentifier = 3 } - export interface ThisTypePredicate { + export interface TypePredicateBase { + kind: TypePredicateKind; + type: Type | undefined; + } + export interface ThisTypePredicate extends TypePredicateBase { kind: TypePredicateKind.This; parameterName: undefined; parameterIndex: undefined; type: Type; } - export interface IdentifierTypePredicate { + export interface IdentifierTypePredicate extends TypePredicateBase { kind: TypePredicateKind.Identifier; parameterName: string; parameterIndex: number; type: Type; } - export interface AssertsThisTypePredicate { + export interface AssertsThisTypePredicate extends TypePredicateBase { kind: TypePredicateKind.AssertsThis; parameterName: undefined; parameterIndex: undefined; type: Type | undefined; } - export interface AssertsIdentifierTypePredicate { + export interface AssertsIdentifierTypePredicate extends TypePredicateBase { kind: TypePredicateKind.AssertsIdentifier; parameterName: string; parameterIndex: number; diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index d8df06e936f..ba196621c14 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -2097,25 +2097,29 @@ declare namespace ts { AssertsThis = 2, AssertsIdentifier = 3 } - export interface ThisTypePredicate { + export interface TypePredicateBase { + kind: TypePredicateKind; + type: Type | undefined; + } + export interface ThisTypePredicate extends TypePredicateBase { kind: TypePredicateKind.This; parameterName: undefined; parameterIndex: undefined; type: Type; } - export interface IdentifierTypePredicate { + export interface IdentifierTypePredicate extends TypePredicateBase { kind: TypePredicateKind.Identifier; parameterName: string; parameterIndex: number; type: Type; } - export interface AssertsThisTypePredicate { + export interface AssertsThisTypePredicate extends TypePredicateBase { kind: TypePredicateKind.AssertsThis; parameterName: undefined; parameterIndex: undefined; type: Type | undefined; } - export interface AssertsIdentifierTypePredicate { + export interface AssertsIdentifierTypePredicate extends TypePredicateBase { kind: TypePredicateKind.AssertsIdentifier; parameterName: string; parameterIndex: number; From 436339ddef241c3ae9da802ecddb9782e768cdc7 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 12 Sep 2019 07:41:47 -0700 Subject: [PATCH 54/97] Use declared type for references in unreachable code --- src/compiler/checker.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index d2a106b903d..c7e11e5be6a 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -671,6 +671,7 @@ namespace ts { const silentNeverType = createIntrinsicType(TypeFlags.Never, "never"); const nonInferrableType = createIntrinsicType(TypeFlags.Never, "never", ObjectFlags.NonInferrableType); const implicitNeverType = createIntrinsicType(TypeFlags.Never, "never"); + const unreachableNeverType = createIntrinsicType(TypeFlags.Never, "never"); const nonPrimitiveType = createIntrinsicType(TypeFlags.NonPrimitive, "object"); const stringNumberSymbolType = getUnionType([stringType, numberType, esSymbolType]); const keyofConstraintType = keyofStringsOnly ? stringType : stringNumberSymbolType; @@ -17008,7 +17009,7 @@ namespace ts { // on empty arrays are possible without implicit any errors and new element types can be inferred without // type mismatch errors. const resultType = getObjectFlags(evolvedType) & ObjectFlags.EvolvingArray && isEvolvingArrayOperationTarget(reference) ? autoArrayType : finalizeEvolvingArrayType(evolvedType); - if (reference.parent && reference.parent.kind === SyntaxKind.NonNullExpression && getTypeWithFacts(resultType, TypeFacts.NEUndefinedOrNull).flags & TypeFlags.Never) { + if (resultType === unreachableNeverType || reference.parent && reference.parent.kind === SyntaxKind.NonNullExpression && getTypeWithFacts(resultType, TypeFacts.NEUndefinedOrNull).flags & TypeFlags.Never) { return declaredType; } return resultType; @@ -17144,8 +17145,11 @@ namespace ts { // Assignments only narrow the computed type if the declared type is a union type. Thus, we // only need to evaluate the assigned type if the declared type is a union type. if (isMatchingReference(reference, node)) { + const flowType = getTypeAtFlowNode(flow.antecedent); + if (flowType === unreachableNeverType) { + return flowType; + } if (getAssignmentTargetKind(node) === AssignmentKind.Compound) { - const flowType = getTypeAtFlowNode(flow.antecedent); return createFlowType(getBaseTypeOfLiteralType(getTypeFromFlowType(flowType)), isIncomplete(flowType)); } if (declaredType === autoType || declaredType === autoArrayType) { @@ -17165,12 +17169,16 @@ namespace ts { // reference 'x.y.z', we may be at an assignment to 'x.y' or 'x'. In that case, // return the declared type. if (containsMatchingReference(reference, node)) { + const flowType = getTypeAtFlowNode(flow.antecedent); + if (flowType === unreachableNeverType) { + return flowType; + } // A matching dotted name might also be an expando property on a function *expression*, // in which case we continue control flow analysis back to the function's declaration if (isVariableDeclaration(node) && (isInJSFile(node) || isVarConst(node))) { const init = getDeclaredExpandoInitializer(node); if (init && (init.kind === SyntaxKind.FunctionExpression || init.kind === SyntaxKind.ArrowFunction)) { - return getTypeAtFlowNode(flow.antecedent); + return flowType; } } return declaredType; @@ -17209,7 +17217,7 @@ namespace ts { return narrowedType === type ? flowType : createFlowType(narrowedType, isIncomplete(flowType)); } if (getReturnTypeOfSignature(signature).flags & TypeFlags.Never) { - return neverType; + return unreachableNeverType; } } return undefined; From a9336ba8a5df106fd40ca8816735b6a98cd7381e Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 12 Sep 2019 09:15:43 -0700 Subject: [PATCH 55/97] Revert "Use declared type for references in unreachable code" This reverts commit 436339ddef241c3ae9da802ecddb9782e768cdc7. --- src/compiler/checker.ts | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index c7e11e5be6a..d2a106b903d 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -671,7 +671,6 @@ namespace ts { const silentNeverType = createIntrinsicType(TypeFlags.Never, "never"); const nonInferrableType = createIntrinsicType(TypeFlags.Never, "never", ObjectFlags.NonInferrableType); const implicitNeverType = createIntrinsicType(TypeFlags.Never, "never"); - const unreachableNeverType = createIntrinsicType(TypeFlags.Never, "never"); const nonPrimitiveType = createIntrinsicType(TypeFlags.NonPrimitive, "object"); const stringNumberSymbolType = getUnionType([stringType, numberType, esSymbolType]); const keyofConstraintType = keyofStringsOnly ? stringType : stringNumberSymbolType; @@ -17009,7 +17008,7 @@ namespace ts { // on empty arrays are possible without implicit any errors and new element types can be inferred without // type mismatch errors. const resultType = getObjectFlags(evolvedType) & ObjectFlags.EvolvingArray && isEvolvingArrayOperationTarget(reference) ? autoArrayType : finalizeEvolvingArrayType(evolvedType); - if (resultType === unreachableNeverType || reference.parent && reference.parent.kind === SyntaxKind.NonNullExpression && getTypeWithFacts(resultType, TypeFacts.NEUndefinedOrNull).flags & TypeFlags.Never) { + if (reference.parent && reference.parent.kind === SyntaxKind.NonNullExpression && getTypeWithFacts(resultType, TypeFacts.NEUndefinedOrNull).flags & TypeFlags.Never) { return declaredType; } return resultType; @@ -17145,11 +17144,8 @@ namespace ts { // Assignments only narrow the computed type if the declared type is a union type. Thus, we // only need to evaluate the assigned type if the declared type is a union type. if (isMatchingReference(reference, node)) { - const flowType = getTypeAtFlowNode(flow.antecedent); - if (flowType === unreachableNeverType) { - return flowType; - } if (getAssignmentTargetKind(node) === AssignmentKind.Compound) { + const flowType = getTypeAtFlowNode(flow.antecedent); return createFlowType(getBaseTypeOfLiteralType(getTypeFromFlowType(flowType)), isIncomplete(flowType)); } if (declaredType === autoType || declaredType === autoArrayType) { @@ -17169,16 +17165,12 @@ namespace ts { // reference 'x.y.z', we may be at an assignment to 'x.y' or 'x'. In that case, // return the declared type. if (containsMatchingReference(reference, node)) { - const flowType = getTypeAtFlowNode(flow.antecedent); - if (flowType === unreachableNeverType) { - return flowType; - } // A matching dotted name might also be an expando property on a function *expression*, // in which case we continue control flow analysis back to the function's declaration if (isVariableDeclaration(node) && (isInJSFile(node) || isVarConst(node))) { const init = getDeclaredExpandoInitializer(node); if (init && (init.kind === SyntaxKind.FunctionExpression || init.kind === SyntaxKind.ArrowFunction)) { - return flowType; + return getTypeAtFlowNode(flow.antecedent); } } return declaredType; @@ -17217,7 +17209,7 @@ namespace ts { return narrowedType === type ? flowType : createFlowType(narrowedType, isIncomplete(flowType)); } if (getReturnTypeOfSignature(signature).flags & TypeFlags.Never) { - return unreachableNeverType; + return neverType; } } return undefined; From 3c79225f48773cccdf247578cf2297111f0ccd2a Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Thu, 12 Sep 2019 11:18:47 -0700 Subject: [PATCH 56/97] Update baselines with any[] inferences --- tests/cases/fourslash/codeFixInferFromUsageArray.ts | 2 +- tests/cases/fourslash/codeFixInferFromUsageString.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/cases/fourslash/codeFixInferFromUsageArray.ts b/tests/cases/fourslash/codeFixInferFromUsageArray.ts index 03d6b09e99b..75f923d5aee 100644 --- a/tests/cases/fourslash/codeFixInferFromUsageArray.ts +++ b/tests/cases/fourslash/codeFixInferFromUsageArray.ts @@ -10,5 +10,5 @@ //// return p.push(12) //// } -verify.rangeAfterCodeFix("p: number[], a: string[], b: any[], c: boolean[], d: any[], e: number[]", /*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, /*index*/0); +verify.rangeAfterCodeFix("p: number[], a: string[], b: any[], c: boolean[], d: any[], e: any[]", /*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, /*index*/0); diff --git a/tests/cases/fourslash/codeFixInferFromUsageString.ts b/tests/cases/fourslash/codeFixInferFromUsageString.ts index dc5787dc75b..d58998cf4a3 100644 --- a/tests/cases/fourslash/codeFixInferFromUsageString.ts +++ b/tests/cases/fourslash/codeFixInferFromUsageString.ts @@ -1,12 +1,12 @@ /// // @noImplicitAny: true -//// function foo([|p, a, b, c, d |]) { +//// function foo([|p, a, b |]) { //// var x //// p.charAt(x) //// a.charAt(0) //// b.concat('hi') //// } -verify.rangeAfterCodeFix("p: string, a: string, b: string | string[]", /*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, /*index*/0); +verify.rangeAfterCodeFix("p: string, a: string, b: string | any[]", /*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, /*index*/0); From de7d68a6d8704545fa4d04e3e0cf3072bda887c2 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Thu, 12 Sep 2019 11:30:51 -0700 Subject: [PATCH 57/97] Even more renaming --- src/services/codefixes/inferFromUsage.ts | 46 ++++++++++++------------ 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/src/services/codefixes/inferFromUsage.ts b/src/services/codefixes/inferFromUsage.ts index 00b95c57d1a..090c241b05d 100644 --- a/src/services/codefixes/inferFromUsage.ts +++ b/src/services/codefixes/inferFromUsage.ts @@ -481,7 +481,7 @@ namespace ts.codefix { } function single(): Type { - return unifyTypes(inferTypesFromReferencesSingle(references)); + return combineTypes(inferTypesFromReferencesSingle(references)); } function parameters(declaration: FunctionLike): ParameterInference[] | undefined { @@ -517,7 +517,7 @@ namespace ts.codefix { const inferred = inferTypesFromReferencesSingle(getReferences(parameter.name, program, cancellationToken)); types.push(...(isRest ? mapDefined(inferred, checker.getElementTypeOfArrayType) : inferred)); } - const type = unifyTypes(types); + const type = combineTypes(types); return { type: isRest ? checker.createArrayType(type) : type, isOptional: isOptional && !isRest, @@ -533,7 +533,7 @@ namespace ts.codefix { calculateUsageOfNode(reference, usage); } - return unifyTypes(usage.candidateThisTypes || emptyArray); + return combineTypes(usage.candidateThisTypes || emptyArray); } function inferTypesFromReferencesSingle(references: readonly Identifier[]): Type[] { @@ -542,7 +542,7 @@ namespace ts.codefix { cancellationToken.throwIfCancellationRequested(); calculateUsageOfNode(reference, usage); } - return inferFromUsage(usage); + return inferTypes(usage); } function calculateUsageOfNode(node: Expression, usage: Usage): void { @@ -819,11 +819,11 @@ namespace ts.codefix { return inferences.filter(i => toRemove.every(f => !f(i))); } - function unifyFromUsage(usage: Usage) { - return unifyTypes(inferFromUsage(usage)); + function combineFromUsage(usage: Usage) { + return combineTypes(inferTypes(usage)); } - function unifyTypes(inferences: readonly Type[]): Type { + function combineTypes(inferences: readonly Type[]): Type { if (!inferences.length) return checker.getAnyType(); // 1. string or number individually override string | number @@ -847,12 +847,12 @@ namespace ts.codefix { const anons = good.filter(i => checker.getObjectFlags(i) & ObjectFlags.Anonymous) as AnonymousType[]; if (anons.length) { good = good.filter(i => !(checker.getObjectFlags(i) & ObjectFlags.Anonymous)); - good.push(unifyAnonymousTypes(anons)); + good.push(combineAnonymousTypes(anons)); } return checker.getWidenedType(checker.getUnionType(good.map(checker.getBaseTypeOfLiteralType), UnionReduction.Subtype)); } - function unifyAnonymousTypes(anons: AnonymousType[]) { + function combineAnonymousTypes(anons: AnonymousType[]) { if (anons.length === 1) { return anons[0]; } @@ -893,7 +893,7 @@ namespace ts.codefix { numberIndices.length ? checker.createIndexInfo(checker.getUnionType(numberIndices), numberIndexReadonly) : undefined); } - function inferFromUsage(usage: Usage): Type[] { + function inferTypes(usage: Usage): Type[] { const types = []; if (usage.isNumber) { @@ -906,7 +906,7 @@ namespace ts.codefix { types.push(checker.getUnionType([checker.getStringType(), checker.getNumberType()])); } if (usage.numberIndex) { - types.push(checker.createArrayType(unifyFromUsage(usage.numberIndex))); + types.push(checker.createArrayType(combineFromUsage(usage.numberIndex))); } if (usage.properties && usage.properties.size || usage.calls && usage.calls.length @@ -926,13 +926,13 @@ namespace ts.codefix { if (usage.properties) { usage.properties.forEach((u, name) => { const symbol = checker.createSymbol(SymbolFlags.Property, name); - symbol.type = unifyFromUsage(u); + symbol.type = combineFromUsage(u); members.set(name, symbol); }); } const callSignatures: Signature[] = usage.calls ? [getSignatureFromCalls(usage.calls)] : []; const constructSignatures: Signature[] = usage.constructs ? [getSignatureFromCalls(usage.constructs)] : []; - const stringIndexInfo = usage.stringIndex && checker.createIndexInfo(unifyFromUsage(usage.stringIndex), /*isReadonly*/ false); + const stringIndexInfo = usage.stringIndex && checker.createIndexInfo(combineFromUsage(usage.stringIndex), /*isReadonly*/ false); return checker.createAnonymousType(/*symbol*/ undefined!, members, callSignatures, constructSignatures, stringIndexInfo, /*numberIndexInfo*/ undefined); // TODO: GH#18217 } @@ -959,7 +959,7 @@ namespace ts.codefix { result = result && !!sigs.length && checker.isTypeAssignableTo(source, getFunctionFromCalls(propUsage.calls)); } else { - result = result && checker.isTypeAssignableTo(source, unifyFromUsage(propUsage)); + result = result && checker.isTypeAssignableTo(source, combineFromUsage(propUsage)); } }); return result; @@ -982,17 +982,17 @@ namespace ts.codefix { usage.properties.forEach((propUsage, name) => { const genericPropertyType = checker.getTypeOfPropertyOfType(generic, name as string); Debug.assert(!!genericPropertyType, "generic should have all the properties of its reference."); - types.push(...infer(genericPropertyType!, unifyFromUsage(propUsage), singleTypeParameter)); + types.push(...inferTypeParameters(genericPropertyType!, combineFromUsage(propUsage), singleTypeParameter)); }); - return builtinConstructors[type.symbol.escapedName as string](unifyTypes(types)); + return builtinConstructors[type.symbol.escapedName as string](combineTypes(types)); } - function infer(genericType: Type, usageType: Type, typeParameter: Type): readonly Type[] { + function inferTypeParameters(genericType: Type, usageType: Type, typeParameter: Type): readonly Type[] { if (genericType === typeParameter) { return [usageType]; } else if (genericType.flags & TypeFlags.UnionOrIntersection) { - return flatMap((genericType as UnionOrIntersectionType).types, t => infer(t, usageType, typeParameter)); + return flatMap((genericType as UnionOrIntersectionType).types, t => inferTypeParameters(t, usageType, typeParameter)); } else if (getObjectFlags(genericType) & ObjectFlags.Reference && getObjectFlags(usageType) & ObjectFlags.Reference) { // this is wrong because we need a reference to the targetType to, so we can check that it's also a reference @@ -1002,7 +1002,7 @@ namespace ts.codefix { if (genericArgs && usageArgs) { for (let i = 0; i < genericArgs.length; i++) { if (usageArgs[i]) { - types.push(...infer(genericArgs[i], usageArgs[i], typeParameter)); + types.push(...inferTypeParameters(genericArgs[i], usageArgs[i], typeParameter)); } } } @@ -1031,11 +1031,11 @@ namespace ts.codefix { genericParamType = elementType; } const targetType = (usageParam as SymbolLinks).type || checker.getTypeOfSymbolAtLocation(usageParam, usageParam.valueDeclaration); - types.push(...infer(genericParamType, targetType, typeParameter)); + types.push(...inferTypeParameters(genericParamType, targetType, typeParameter)); } const genericReturn = checker.getReturnTypeOfSignature(genericSig); const usageReturn = checker.getReturnTypeOfSignature(usageSig); - types.push(...infer(genericReturn, usageReturn, typeParameter)); + types.push(...inferTypeParameters(genericReturn, usageReturn, typeParameter)); return types; } @@ -1048,13 +1048,13 @@ namespace ts.codefix { const length = Math.max(...calls.map(c => c.argumentTypes.length)); for (let i = 0; i < length; i++) { const symbol = checker.createSymbol(SymbolFlags.FunctionScopedVariable, escapeLeadingUnderscores(`arg${i}`)); - symbol.type = unifyTypes(calls.map(call => call.argumentTypes[i] || checker.getUndefinedType())); + symbol.type = combineTypes(calls.map(call => call.argumentTypes[i] || checker.getUndefinedType())); if (calls.some(call => call.argumentTypes[i] === undefined)) { symbol.flags |= SymbolFlags.Optional; } parameters.push(symbol); } - const returnType = unifyFromUsage(combineUsages(calls.map(call => call.return_))); + const returnType = combineFromUsage(combineUsages(calls.map(call => call.return_))); // TODO: GH#18217 return checker.createSignature(/*declaration*/ undefined!, /*typeParameters*/ undefined, /*thisParameter*/ undefined, parameters, returnType, /*typePredicate*/ undefined, length, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false); } From 971b0df80a3bacf9d6108c5053cabb18f0ae74f6 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Fri, 13 Sep 2019 07:12:19 -0700 Subject: [PATCH 58/97] Use declared type for references in unreachable code (again) --- src/compiler/checker.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index d2a106b903d..2e792040bb4 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -671,6 +671,7 @@ namespace ts { const silentNeverType = createIntrinsicType(TypeFlags.Never, "never"); const nonInferrableType = createIntrinsicType(TypeFlags.Never, "never", ObjectFlags.NonInferrableType); const implicitNeverType = createIntrinsicType(TypeFlags.Never, "never"); + const unreachableNeverType = createIntrinsicType(TypeFlags.Never, "never"); const nonPrimitiveType = createIntrinsicType(TypeFlags.NonPrimitive, "object"); const stringNumberSymbolType = getUnionType([stringType, numberType, esSymbolType]); const keyofConstraintType = keyofStringsOnly ? stringType : stringNumberSymbolType; @@ -17008,7 +17009,7 @@ namespace ts { // on empty arrays are possible without implicit any errors and new element types can be inferred without // type mismatch errors. const resultType = getObjectFlags(evolvedType) & ObjectFlags.EvolvingArray && isEvolvingArrayOperationTarget(reference) ? autoArrayType : finalizeEvolvingArrayType(evolvedType); - if (reference.parent && reference.parent.kind === SyntaxKind.NonNullExpression && getTypeWithFacts(resultType, TypeFacts.NEUndefinedOrNull).flags & TypeFlags.Never) { + if (resultType === unreachableNeverType || reference.parent && reference.parent.kind === SyntaxKind.NonNullExpression && getTypeWithFacts(resultType, TypeFacts.NEUndefinedOrNull).flags & TypeFlags.Never) { return declaredType; } return resultType; @@ -17037,6 +17038,7 @@ namespace ts { if (key) { const id = getFlowNodeId(flow); if (flowAssignmentKeys[id] === key) { + flowDepth--; return flowAssignmentTypes[id]; } } @@ -17144,8 +17146,11 @@ namespace ts { // Assignments only narrow the computed type if the declared type is a union type. Thus, we // only need to evaluate the assigned type if the declared type is a union type. if (isMatchingReference(reference, node)) { + const flowType = getTypeAtFlowNode(flow.antecedent); + if (flowType === unreachableNeverType) { + return flowType; + } if (getAssignmentTargetKind(node) === AssignmentKind.Compound) { - const flowType = getTypeAtFlowNode(flow.antecedent); return createFlowType(getBaseTypeOfLiteralType(getTypeFromFlowType(flowType)), isIncomplete(flowType)); } if (declaredType === autoType || declaredType === autoArrayType) { @@ -17165,12 +17170,16 @@ namespace ts { // reference 'x.y.z', we may be at an assignment to 'x.y' or 'x'. In that case, // return the declared type. if (containsMatchingReference(reference, node)) { + const flowType = getTypeAtFlowNode(flow.antecedent); + if (flowType === unreachableNeverType) { + return flowType; + } // A matching dotted name might also be an expando property on a function *expression*, // in which case we continue control flow analysis back to the function's declaration if (isVariableDeclaration(node) && (isInJSFile(node) || isVarConst(node))) { const init = getDeclaredExpandoInitializer(node); if (init && (init.kind === SyntaxKind.FunctionExpression || init.kind === SyntaxKind.ArrowFunction)) { - return getTypeAtFlowNode(flow.antecedent); + return flowType; } } return declaredType; @@ -17209,7 +17218,7 @@ namespace ts { return narrowedType === type ? flowType : createFlowType(narrowedType, isIncomplete(flowType)); } if (getReturnTypeOfSignature(signature).flags & TypeFlags.Never) { - return neverType; + return unreachableNeverType; } } return undefined; From 3749de60195f0bce34ac5ed4f265102cbc425193 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Fri, 13 Sep 2019 11:33:16 -0700 Subject: [PATCH 59/97] Dedicated isReachableFlowNode function to determine reachability --- src/compiler/checker.ts | 48 +++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 2e792040bb4..b5630a6ef10 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -842,6 +842,7 @@ namespace ts { const flowLoopTypes: Type[][] = []; const sharedFlowNodes: FlowNode[] = []; const sharedFlowTypes: FlowType[] = []; + const flowNodeReachable: (boolean | undefined)[] = []; const potentialThisCollisions: Node[] = []; const potentialNewTargetCollisions: Node[] = []; const awaitedTypeStack: number[] = []; @@ -16991,6 +16992,40 @@ namespace ts { diagnostics.add(createFileDiagnostic(sourceFile, span.start, span.length, Diagnostics.The_containing_function_or_module_body_is_too_large_for_control_flow_analysis)); } + function isReachableFlowNode(flow: FlowNode) { + return isReachableFlowNodeWorker(flow, /*skipCacheCheck*/ false); + } + + function isReachableFlowNodeWorker(flow: FlowNode, skipCacheCheck: boolean): boolean { + while (true) { + const flags = flow.flags; + if (flags & FlowFlags.Shared && !skipCacheCheck) { + const id = getFlowNodeId(flow); + const reachable = flowNodeReachable[id]; + return reachable !== undefined ? reachable : (flowNodeReachable[id] = isReachableFlowNodeWorker(flow, /*skipCacheCheck*/ true)); + } + if (flags & (FlowFlags.Assignment | FlowFlags.Condition | FlowFlags.SwitchClause | FlowFlags.ArrayMutation | FlowFlags.PreFinally | FlowFlags.AfterFinally)) { + flow = (flow).antecedent; + } + else if (flags & FlowFlags.Call) { + const signature = getEffectsSignature((flow).node); + if (signature && getReturnTypeOfSignature(signature).flags & TypeFlags.Never) { + return false; + } + flow = (flow).antecedent; + } + else if (flags & FlowFlags.LoopLabel) { + flow = (flow).antecedents![0]; + } + else if (flags & FlowFlags.BranchLabel) { + return every((flow).antecedents!, isReachableFlowNode); + } + else { + return true; + } + } + } + function getFlowTypeOfReference(reference: Node, declaredType: Type, initialType = declaredType, flowContainer?: Node, couldBeUninitialized?: boolean) { let key: string | undefined; let keySet = false; @@ -17146,11 +17181,11 @@ namespace ts { // Assignments only narrow the computed type if the declared type is a union type. Thus, we // only need to evaluate the assigned type if the declared type is a union type. if (isMatchingReference(reference, node)) { - const flowType = getTypeAtFlowNode(flow.antecedent); - if (flowType === unreachableNeverType) { - return flowType; + if (!isReachableFlowNode(flow)) { + return unreachableNeverType; } if (getAssignmentTargetKind(node) === AssignmentKind.Compound) { + const flowType = getTypeAtFlowNode(flow.antecedent); return createFlowType(getBaseTypeOfLiteralType(getTypeFromFlowType(flowType)), isIncomplete(flowType)); } if (declaredType === autoType || declaredType === autoArrayType) { @@ -17170,16 +17205,15 @@ namespace ts { // reference 'x.y.z', we may be at an assignment to 'x.y' or 'x'. In that case, // return the declared type. if (containsMatchingReference(reference, node)) { - const flowType = getTypeAtFlowNode(flow.antecedent); - if (flowType === unreachableNeverType) { - return flowType; + if (!isReachableFlowNode(flow)) { + return unreachableNeverType; } // A matching dotted name might also be an expando property on a function *expression*, // in which case we continue control flow analysis back to the function's declaration if (isVariableDeclaration(node) && (isInJSFile(node) || isVarConst(node))) { const init = getDeclaredExpandoInitializer(node); if (init && (init.kind === SyntaxKind.FunctionExpression || init.kind === SyntaxKind.ArrowFunction)) { - return flowType; + return getTypeAtFlowNode(flow.antecedent); } } return declaredType; From 3a89c8cc5c06669c7d53e4e382db4390df1dfb59 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Fri, 13 Sep 2019 14:38:12 -0700 Subject: [PATCH 60/97] Use isReachableFlowNode to check for implicit return --- src/compiler/binder.ts | 6 ++---- src/compiler/checker.ts | 11 +++-------- src/compiler/types.ts | 2 +- 3 files changed, 6 insertions(+), 13 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 77ad55a20c9..bb3b7b10508 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -569,7 +569,7 @@ namespace ts { } // We create a return control flow graph for IIFEs and constructors. For constructors // we use the return control flow graph in strict property initialization checks. - currentReturnTarget = isIIFE || node.kind === SyntaxKind.Constructor ? createBranchLabel() : undefined; + currentReturnTarget = containerFlags & ContainerFlags.IsFunctionLike && nodeIsPresent((node).body) ? createBranchLabel() : undefined; currentBreakTarget = undefined; currentContinueTarget = undefined; activeLabels = undefined; @@ -589,9 +589,7 @@ namespace ts { if (currentReturnTarget) { addAntecedent(currentReturnTarget, currentFlow); currentFlow = finishFlowLabel(currentReturnTarget); - if (node.kind === SyntaxKind.Constructor) { - (node).returnFlowNode = currentFlow; - } + (node).returnFlowNode = currentFlow; } if (!isIIFE) { currentFlow = saveCurrentFlow; diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index b5630a6ef10..5e491b2e730 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -23803,15 +23803,10 @@ namespace ts { return eachTypeContainedIn(mapType(type, getRegularTypeOfLiteralType), switchTypes); } - function isNeverFunctionCall(expr: Expression) { - const signature = expr.kind === SyntaxKind.CallExpression && getEffectsSignature(expr); - return !!(signature && getReturnTypeOfSignature(signature).flags & TypeFlags.Never); - } - function functionHasImplicitReturn(func: FunctionLikeDeclaration) { - return !!(func.flags & NodeFlags.HasImplicitReturn) && !some((func.body).statements, statement => - statement.kind === SyntaxKind.SwitchStatement && isExhaustiveSwitchStatement(statement) || - statement.kind === SyntaxKind.ExpressionStatement && isNeverFunctionCall((statement).expression)); + return !!(func.flags & NodeFlags.HasImplicitReturn && + !some((func.body).statements, s => s.kind === SyntaxKind.SwitchStatement && isExhaustiveSwitchStatement(s)) && + !(func.returnFlowNode && !isReachableFlowNode(func.returnFlowNode))); } /** NOTE: Return value of `[]` means a different thing than `undefined`. `[]` means func returns `void`, `undefined` means it returns `never`. */ diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 7486af7148d..214c2d58788 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1040,6 +1040,7 @@ namespace ts { questionToken?: QuestionToken; exclamationToken?: ExclamationToken; body?: Block | Expression; + /* @internal */ returnFlowNode?: FlowNode; } export type FunctionLikeDeclaration = @@ -1085,7 +1086,6 @@ namespace ts { kind: SyntaxKind.Constructor; parent: ClassLikeDeclaration; body?: FunctionBody; - /* @internal */ returnFlowNode?: FlowNode; } /** For when we encounter a semicolon in a class declaration. ES6 allows these as class elements. */ From cc6e4938aedaa017a999c4c53bd5e560999f6fde Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sat, 14 Sep 2019 15:30:09 -0700 Subject: [PATCH 61/97] Treat exhaustive switch statements like non-returning functions in CFA --- src/compiler/binder.ts | 7 +++++-- src/compiler/checker.ts | 39 +++++++++++++++++++++++++-------------- src/compiler/types.ts | 3 ++- 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index bb3b7b10508..e93fef838e8 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -569,7 +569,7 @@ namespace ts { } // We create a return control flow graph for IIFEs and constructors. For constructors // we use the return control flow graph in strict property initialization checks. - currentReturnTarget = containerFlags & ContainerFlags.IsFunctionLike && nodeIsPresent((node).body) ? createBranchLabel() : undefined; + currentReturnTarget = isIIFE || node.kind === SyntaxKind.Constructor ? createBranchLabel() : undefined; currentBreakTarget = undefined; currentContinueTarget = undefined; activeLabels = undefined; @@ -581,6 +581,7 @@ namespace ts { if (!(currentFlow.flags & FlowFlags.Unreachable) && containerFlags & ContainerFlags.IsFunctionLike && nodeIsPresent((node).body)) { node.flags |= NodeFlags.HasImplicitReturn; if (hasExplicitReturn) node.flags |= NodeFlags.HasExplicitReturn; + (node).endFlowNode = currentFlow; } if (node.kind === SyntaxKind.SourceFile) { node.flags |= emitFlags; @@ -589,7 +590,9 @@ namespace ts { if (currentReturnTarget) { addAntecedent(currentReturnTarget, currentFlow); currentFlow = finishFlowLabel(currentReturnTarget); - (node).returnFlowNode = currentFlow; + if (node.kind === SyntaxKind.Constructor) { + (node).returnFlowNode = currentFlow; + } } if (!isIIFE) { currentFlow = saveCurrentFlow; diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 5e491b2e730..0d9397eb6b3 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -16996,16 +16996,19 @@ namespace ts { return isReachableFlowNodeWorker(flow, /*skipCacheCheck*/ false); } - function isReachableFlowNodeWorker(flow: FlowNode, skipCacheCheck: boolean): boolean { + function isReachableFlowNodeWorker(flow: FlowNode, noCacheCheck: boolean): boolean { while (true) { const flags = flow.flags; - if (flags & FlowFlags.Shared && !skipCacheCheck) { - const id = getFlowNodeId(flow); - const reachable = flowNodeReachable[id]; - return reachable !== undefined ? reachable : (flowNodeReachable[id] = isReachableFlowNodeWorker(flow, /*skipCacheCheck*/ true)); + if (flags & FlowFlags.Shared | flags & FlowFlags.SwitchClause) { + if (!noCacheCheck) { + const id = getFlowNodeId(flow); + const reachable = flowNodeReachable[id]; + return reachable !== undefined ? reachable : (flowNodeReachable[id] = isReachableFlowNodeWorker(flow, /*skipCacheCheck*/ true)); + } + noCacheCheck = false; } - if (flags & (FlowFlags.Assignment | FlowFlags.Condition | FlowFlags.SwitchClause | FlowFlags.ArrayMutation | FlowFlags.PreFinally | FlowFlags.AfterFinally)) { - flow = (flow).antecedent; + if (flags & (FlowFlags.Assignment | FlowFlags.Condition | FlowFlags.ArrayMutation | FlowFlags.PreFinally | FlowFlags.AfterFinally)) { + flow = (flow).antecedent; } else if (flags & FlowFlags.Call) { const signature = getEffectsSignature((flow).node); @@ -17014,14 +17017,20 @@ namespace ts { } flow = (flow).antecedent; } + else if (flags & FlowFlags.BranchLabel) { + return some((flow).antecedents!, isReachableFlowNode); + } else if (flags & FlowFlags.LoopLabel) { flow = (flow).antecedents![0]; } - else if (flags & FlowFlags.BranchLabel) { - return every((flow).antecedents!, isReachableFlowNode); + else if (flags & FlowFlags.SwitchClause) { + if ((flow).clauseStart === (flow).clauseEnd && isExhaustiveSwitchStatement((flow).switchStatement)) { + return false; + } + flow = (flow).antecedent; } else { - return true; + return !(flags & FlowFlags.Unreachable); } } } @@ -17044,7 +17053,11 @@ namespace ts { // on empty arrays are possible without implicit any errors and new element types can be inferred without // type mismatch errors. const resultType = getObjectFlags(evolvedType) & ObjectFlags.EvolvingArray && isEvolvingArrayOperationTarget(reference) ? autoArrayType : finalizeEvolvingArrayType(evolvedType); - if (resultType === unreachableNeverType || reference.parent && reference.parent.kind === SyntaxKind.NonNullExpression && getTypeWithFacts(resultType, TypeFacts.NEUndefinedOrNull).flags & TypeFlags.Never) { + if (resultType === unreachableNeverType) { + error(reference, Diagnostics.Unreachable_code_detected); + return declaredType; + } + if (reference.parent && reference.parent.kind === SyntaxKind.NonNullExpression && getTypeWithFacts(resultType, TypeFacts.NEUndefinedOrNull).flags & TypeFlags.Never) { return declaredType; } return resultType; @@ -23804,9 +23817,7 @@ namespace ts { } function functionHasImplicitReturn(func: FunctionLikeDeclaration) { - return !!(func.flags & NodeFlags.HasImplicitReturn && - !some((func.body).statements, s => s.kind === SyntaxKind.SwitchStatement && isExhaustiveSwitchStatement(s)) && - !(func.returnFlowNode && !isReachableFlowNode(func.returnFlowNode))); + return func.endFlowNode && isReachableFlowNode(func.endFlowNode); } /** NOTE: Return value of `[]` means a different thing than `undefined`. `[]` means func returns `void`, `undefined` means it returns `never`. */ diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 214c2d58788..3ba88e3e45d 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1040,7 +1040,7 @@ namespace ts { questionToken?: QuestionToken; exclamationToken?: ExclamationToken; body?: Block | Expression; - /* @internal */ returnFlowNode?: FlowNode; + /* @internal */ endFlowNode?: FlowNode; } export type FunctionLikeDeclaration = @@ -1086,6 +1086,7 @@ namespace ts { kind: SyntaxKind.Constructor; parent: ClassLikeDeclaration; body?: FunctionBody; + /* @internal */ returnFlowNode?: FlowNode; } /** For when we encounter a semicolon in a class declaration. ES6 allows these as class elements. */ From 0060964fba457f83eb77c48b60909e23184b59d7 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sun, 15 Sep 2019 08:25:07 -0700 Subject: [PATCH 62/97] Further CFA handling of exhaustive switch statements --- src/compiler/binder.ts | 3 ++- src/compiler/checker.ts | 17 +++++++++++++---- src/compiler/types.ts | 1 + 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index e93fef838e8..6efc4ae05b0 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -1224,7 +1224,8 @@ namespace ts { addAntecedent(postSwitchLabel, currentFlow); const hasDefault = forEach(node.caseBlock.clauses, c => c.kind === SyntaxKind.DefaultClause); // We mark a switch statement as possibly exhaustive if it has no default clause and if all - // case clauses have unreachable end points (e.g. they all return). + // case clauses have unreachable end points (e.g. they all return). Note, we no longer need + // this property in control flow analysis, it's there only for backwards compatibility. node.possiblyExhaustive = !hasDefault && !postSwitchLabel.antecedents; if (!hasDefault) { addAntecedent(postSwitchLabel, createFlowSwitchClause(preSwitchCaseFlow, node, 0, 0)); diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 0d9397eb6b3..e5204350742 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -16999,7 +16999,7 @@ namespace ts { function isReachableFlowNodeWorker(flow: FlowNode, noCacheCheck: boolean): boolean { while (true) { const flags = flow.flags; - if (flags & FlowFlags.Shared | flags & FlowFlags.SwitchClause) { + if (flags & FlowFlags.Shared) { if (!noCacheCheck) { const id = getFlowNodeId(flow); const reachable = flowNodeReachable[id]; @@ -17018,12 +17018,16 @@ namespace ts { flow = (flow).antecedent; } else if (flags & FlowFlags.BranchLabel) { + // A branching point is reachable if any branch is reachable. return some((flow).antecedents!, isReachableFlowNode); } else if (flags & FlowFlags.LoopLabel) { + // A loop is reachable if the control flow path that leads to the top is reachable. flow = (flow).antecedents![0]; } else if (flags & FlowFlags.SwitchClause) { + // The control flow path representing an unmatched value in a switch statement with + // no default clause is unreachable if the switch statement is exhaustive. if ((flow).clauseStart === (flow).clauseEnd && isExhaustiveSwitchStatement((flow).switchStatement)) { return false; } @@ -17327,6 +17331,9 @@ namespace ts { } function getTypeAtSwitchClause(flow: FlowSwitchClause): FlowType { + if (flow.clauseStart === flow.clauseEnd && isExhaustiveSwitchStatement((flow).switchStatement)) { + return neverType; + } const expr = flow.switchStatement.expression; const flowType = getTypeAtFlowNode(flow.antecedent); let type = getTypeFromFlowType(flowType); @@ -23793,9 +23800,11 @@ namespace ts { } function isExhaustiveSwitchStatement(node: SwitchStatement): boolean { - if (!node.possiblyExhaustive) { - return false; - } + const links = getNodeLinks(node); + return links.isExhaustive !== undefined ? links.isExhaustive : (links.isExhaustive = computeExhaustiveSwitchStatement(node)); + } + + function computeExhaustiveSwitchStatement(node: SwitchStatement): boolean { if (node.expression.kind === SyntaxKind.TypeOfExpression) { const operandType = getTypeOfExpression((node.expression as TypeOfExpression).expression); // This cast is safe because the switch is possibly exhaustive and does not contain a default case, so there can be no undefined. diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 3ba88e3e45d..f80126b64c5 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -4016,6 +4016,7 @@ namespace ts { contextFreeType?: Type; // Cached context-free type used by the first pass of inference; used when a function's return is partially contextually sensitive deferredNodes?: Map; // Set of nodes whose checking has been deferred capturedBlockScopeBindings?: Symbol[]; // Block-scoped bindings captured beneath this part of an IterationStatement + isExhaustive?: boolean; // Is node an exhaustive switch statement } export const enum TypeFlags { From 51dcce212498a8cb8f6a8ca3d846501252f649fa Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sun, 15 Sep 2019 08:28:17 -0700 Subject: [PATCH 63/97] Accept new baselines --- tests/baselines/reference/narrowingByTypeofInSwitch.types | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/baselines/reference/narrowingByTypeofInSwitch.types b/tests/baselines/reference/narrowingByTypeofInSwitch.types index f823c57770c..fbfe7fe92ee 100644 --- a/tests/baselines/reference/narrowingByTypeofInSwitch.types +++ b/tests/baselines/reference/narrowingByTypeofInSwitch.types @@ -208,7 +208,7 @@ function testExtendsUnion(x: T) { assertAll(x); >assertAll(x) : Basic >assertAll : (x: Basic) => Basic ->x : T +>x : never } function testAny(x: any) { From 59b76cee89254dba2dd36abea850ecd9e578f3c7 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sun, 15 Sep 2019 08:38:17 -0700 Subject: [PATCH 64/97] Fix call to Debug.fail in compiler --- src/compiler/checker.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index e5204350742..58b6d2e4ceb 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -31531,8 +31531,7 @@ namespace ts { const nameType = checkComputedPropertyName(name); return isTypeAssignableToKind(nameType, TypeFlags.ESSymbolLike) ? nameType : stringType; default: - Debug.fail("Unsupported property name."); - return errorType; + return Debug.fail("Unsupported property name."); } } From 945babbaacb0cbb09c39f554c4141bed9647a9c0 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sun, 15 Sep 2019 09:49:57 -0700 Subject: [PATCH 65/97] Fix inference circularity error triggered by exhaustive switch analysis --- src/compiler/transformers/generators.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/transformers/generators.ts b/src/compiler/transformers/generators.ts index 3b1a8379e07..cc16aef6009 100644 --- a/src/compiler/transformers/generators.ts +++ b/src/compiler/transformers/generators.ts @@ -2871,7 +2871,7 @@ namespace ts { function tryEnterOrLeaveBlock(operationIndex: number): void { if (blocks) { for (; blockIndex < blockActions!.length && blockOffsets![blockIndex] <= operationIndex; blockIndex++) { - const block = blocks[blockIndex]; + const block: CodeBlock = blocks[blockIndex]; const blockAction = blockActions![blockIndex]; switch (block.kind) { case CodeBlockKind.Exception: From d26afd7273c8aabfb92157df55eadbc907c6fb65 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sun, 15 Sep 2019 11:07:51 -0700 Subject: [PATCH 66/97] for-in or for-of expression is evaluated before loop back edge --- src/compiler/binder.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 6efc4ae05b0..5006e841dc7 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -1032,12 +1032,12 @@ namespace ts { function bindForInOrForOfStatement(node: ForInOrOfStatement): void { const preLoopLabel = createLoopLabel(); const postLoopLabel = createBranchLabel(); + bind(node.expression); addAntecedent(preLoopLabel, currentFlow); currentFlow = preLoopLabel; if (node.kind === SyntaxKind.ForOfStatement) { bind(node.awaitModifier); } - bind(node.expression); addAntecedent(postLoopLabel, currentFlow); bind(node.initializer); if (node.initializer.kind !== SyntaxKind.VariableDeclarationList) { From 05d1e68e6296d1b951010e8bad6168b88cef05f1 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sun, 15 Sep 2019 18:13:49 -0700 Subject: [PATCH 67/97] Fix linting issues --- src/compiler/checker.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 58b6d2e4ceb..ba06e08a301 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -17019,7 +17019,7 @@ namespace ts { } else if (flags & FlowFlags.BranchLabel) { // A branching point is reachable if any branch is reachable. - return some((flow).antecedents!, isReachableFlowNode); + return some((flow).antecedents, isReachableFlowNode); } else if (flags & FlowFlags.LoopLabel) { // A loop is reachable if the control flow path that leads to the top is reachable. @@ -17331,7 +17331,7 @@ namespace ts { } function getTypeAtSwitchClause(flow: FlowSwitchClause): FlowType { - if (flow.clauseStart === flow.clauseEnd && isExhaustiveSwitchStatement((flow).switchStatement)) { + if (flow.clauseStart === flow.clauseEnd && isExhaustiveSwitchStatement(flow.switchStatement)) { return neverType; } const expr = flow.switchStatement.expression; From e97ebb7f1c6ee93ce3ed6a4f996e54f346a0ff44 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Mon, 16 Sep 2019 13:04:22 -0700 Subject: [PATCH 68/97] More efficient scheme for caching flow node reachability --- src/compiler/checker.ts | 20 ++++++++++++++------ src/compiler/types.ts | 4 ++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index ba06e08a301..c49ef71c19d 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -6,6 +6,7 @@ namespace ts { let nextNodeId = 1; let nextMergeId = 1; let nextFlowId = 1; + let nextCheckerId = 1; const enum IterationUse { AllowsSyncIterablesFlag = 1 << 0, @@ -298,6 +299,7 @@ namespace ts { let instantiationDepth = 0; let constraintDepth = 0; let currentNode: Node | undefined; + let checkerId: number; const emptySymbols = createSymbolTable(); const identityMapper: (type: Type) => Type = identity; @@ -842,7 +844,6 @@ namespace ts { const flowLoopTypes: Type[][] = []; const sharedFlowNodes: FlowNode[] = []; const sharedFlowTypes: FlowType[] = []; - const flowNodeReachable: (boolean | undefined)[] = []; const potentialThisCollisions: Node[] = []; const potentialNewTargetCollisions: Node[] = []; const awaitedTypeStack: number[] = []; @@ -16993,17 +16994,21 @@ namespace ts { } function isReachableFlowNode(flow: FlowNode) { - return isReachableFlowNodeWorker(flow, /*skipCacheCheck*/ false); + return isReachableFlowNodeWorker(flow, /*noCacheCheck*/ false); } function isReachableFlowNodeWorker(flow: FlowNode, noCacheCheck: boolean): boolean { while (true) { const flags = flow.flags; - if (flags & FlowFlags.Shared) { + if (flags & (FlowFlags.Shared | FlowFlags.Assignment | FlowFlags.Label)) { if (!noCacheCheck) { - const id = getFlowNodeId(flow); - const reachable = flowNodeReachable[id]; - return reachable !== undefined ? reachable : (flowNodeReachable[id] = isReachableFlowNodeWorker(flow, /*skipCacheCheck*/ true)); + if (flow.checkerId === checkerId) { + return !!(flow.flags & FlowFlags.Reachable); + } + const reachable = isReachableFlowNodeWorker(flow, /*noCacheCheck*/ true); + flow.checkerId = checkerId; + flow.flags = (flow.flags & ~FlowFlags.Reachable) | (reachable ? FlowFlags.Reachable : 0); + return reachable; } noCacheCheck = false; } @@ -32287,6 +32292,9 @@ namespace ts { } function initializeTypeChecker() { + checkerId = nextCheckerId; + nextCheckerId++; + // Bind all source files and propagate errors for (const file of host.getSourceFiles()) { bindSourceFile(file, compilerOptions); diff --git a/src/compiler/types.ts b/src/compiler/types.ts index f80126b64c5..b9931adb604 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2582,6 +2582,8 @@ namespace ts { AfterFinally = 1 << 13, // Injected edge that links post-finally flow with the rest of the graph /** @internal */ Cached = 1 << 14, // Indicates that at least one cross-call cache entry exists for this node, even if not a loop participant + /** @internal */ + Reachable = 1 << 15, // Reachability as computed by isReachableFlowNode Label = BranchLabel | LoopLabel, Condition = TrueCondition | FalseCondition } @@ -2600,6 +2602,8 @@ namespace ts { export interface FlowNodeBase { flags: FlowFlags; id?: number; // Node id used by flow type cache in checker + /** @internal */ + checkerId?: number; // Checker id for FlowFlags.Reachable } export interface FlowLock { From def5e37e6a03d42dd5c0093b70ab22eda36b5c10 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Mon, 16 Sep 2019 14:20:53 -0700 Subject: [PATCH 69/97] Revert "More efficient scheme for caching flow node reachability" This reverts commit e97ebb7f1c6ee93ce3ed6a4f996e54f346a0ff44. --- src/compiler/checker.ts | 20 ++++++-------------- src/compiler/types.ts | 4 ---- 2 files changed, 6 insertions(+), 18 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index c49ef71c19d..ba06e08a301 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -6,7 +6,6 @@ namespace ts { let nextNodeId = 1; let nextMergeId = 1; let nextFlowId = 1; - let nextCheckerId = 1; const enum IterationUse { AllowsSyncIterablesFlag = 1 << 0, @@ -299,7 +298,6 @@ namespace ts { let instantiationDepth = 0; let constraintDepth = 0; let currentNode: Node | undefined; - let checkerId: number; const emptySymbols = createSymbolTable(); const identityMapper: (type: Type) => Type = identity; @@ -844,6 +842,7 @@ namespace ts { const flowLoopTypes: Type[][] = []; const sharedFlowNodes: FlowNode[] = []; const sharedFlowTypes: FlowType[] = []; + const flowNodeReachable: (boolean | undefined)[] = []; const potentialThisCollisions: Node[] = []; const potentialNewTargetCollisions: Node[] = []; const awaitedTypeStack: number[] = []; @@ -16994,21 +16993,17 @@ namespace ts { } function isReachableFlowNode(flow: FlowNode) { - return isReachableFlowNodeWorker(flow, /*noCacheCheck*/ false); + return isReachableFlowNodeWorker(flow, /*skipCacheCheck*/ false); } function isReachableFlowNodeWorker(flow: FlowNode, noCacheCheck: boolean): boolean { while (true) { const flags = flow.flags; - if (flags & (FlowFlags.Shared | FlowFlags.Assignment | FlowFlags.Label)) { + if (flags & FlowFlags.Shared) { if (!noCacheCheck) { - if (flow.checkerId === checkerId) { - return !!(flow.flags & FlowFlags.Reachable); - } - const reachable = isReachableFlowNodeWorker(flow, /*noCacheCheck*/ true); - flow.checkerId = checkerId; - flow.flags = (flow.flags & ~FlowFlags.Reachable) | (reachable ? FlowFlags.Reachable : 0); - return reachable; + const id = getFlowNodeId(flow); + const reachable = flowNodeReachable[id]; + return reachable !== undefined ? reachable : (flowNodeReachable[id] = isReachableFlowNodeWorker(flow, /*skipCacheCheck*/ true)); } noCacheCheck = false; } @@ -32292,9 +32287,6 @@ namespace ts { } function initializeTypeChecker() { - checkerId = nextCheckerId; - nextCheckerId++; - // Bind all source files and propagate errors for (const file of host.getSourceFiles()) { bindSourceFile(file, compilerOptions); diff --git a/src/compiler/types.ts b/src/compiler/types.ts index b9931adb604..f80126b64c5 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2582,8 +2582,6 @@ namespace ts { AfterFinally = 1 << 13, // Injected edge that links post-finally flow with the rest of the graph /** @internal */ Cached = 1 << 14, // Indicates that at least one cross-call cache entry exists for this node, even if not a loop participant - /** @internal */ - Reachable = 1 << 15, // Reachability as computed by isReachableFlowNode Label = BranchLabel | LoopLabel, Condition = TrueCondition | FalseCondition } @@ -2602,8 +2600,6 @@ namespace ts { export interface FlowNodeBase { flags: FlowFlags; id?: number; // Node id used by flow type cache in checker - /** @internal */ - checkerId?: number; // Checker id for FlowFlags.Reachable } export interface FlowLock { From 6d6c620cc2e047047f8aba4eb7fe6acea81d77b4 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Mon, 16 Sep 2019 15:30:45 -0700 Subject: [PATCH 70/97] Report grammatic and type-based unreachable code errors in the same way --- src/compiler/binder.ts | 3 +++ src/compiler/checker.ts | 10 +++++----- src/compiler/types.ts | 4 +++- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 5006e841dc7..cf93ee17ebd 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -672,6 +672,9 @@ namespace ts { bindJSDoc(node); return; } + if (node.kind >= SyntaxKind.FirstStatement && node.kind <= SyntaxKind.LastStatement) { + node.flowNode = currentFlow; + } switch (node.kind) { case SyntaxKind.WhileStatement: bindWhileStatement(node); diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index ba06e08a301..501e6c4d92e 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -17057,11 +17057,7 @@ namespace ts { // on empty arrays are possible without implicit any errors and new element types can be inferred without // type mismatch errors. const resultType = getObjectFlags(evolvedType) & ObjectFlags.EvolvingArray && isEvolvingArrayOperationTarget(reference) ? autoArrayType : finalizeEvolvingArrayType(evolvedType); - if (resultType === unreachableNeverType) { - error(reference, Diagnostics.Unreachable_code_detected); - return declaredType; - } - if (reference.parent && reference.parent.kind === SyntaxKind.NonNullExpression && getTypeWithFacts(resultType, TypeFacts.NEUndefinedOrNull).flags & TypeFlags.Never) { + if (resultType === unreachableNeverType|| reference.parent && reference.parent.kind === SyntaxKind.NonNullExpression && getTypeWithFacts(resultType, TypeFacts.NEUndefinedOrNull).flags & TypeFlags.Never) { return declaredType; } return resultType; @@ -30533,6 +30529,10 @@ namespace ts { cancellationToken.throwIfCancellationRequested(); } } + if (kind >= SyntaxKind.FirstStatement && kind <= SyntaxKind.LastStatement && + !compilerOptions.allowUnreachableCode && node.flowNode && !isReachableFlowNode(node.flowNode)) { + errorOrSuggestion(compilerOptions.allowUnreachableCode === false, node, Diagnostics.Unreachable_code_detected); + } switch (kind) { case SyntaxKind.TypeParameter: diff --git a/src/compiler/types.ts b/src/compiler/types.ts index f80126b64c5..afb53aea7a3 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -363,8 +363,8 @@ namespace ts { SemicolonClassElement, // Element Block, - VariableStatement, EmptyStatement, + VariableStatement, ExpressionStatement, IfStatement, DoStatement, @@ -514,6 +514,8 @@ namespace ts { LastTemplateToken = TemplateTail, FirstBinaryOperator = LessThanToken, LastBinaryOperator = CaretEqualsToken, + FirstStatement = VariableStatement, + LastStatement = DebuggerStatement, FirstNode = QualifiedName, FirstJSDocNode = JSDocTypeExpression, LastJSDocNode = JSDocPropertyTag, From d9c9129720536f2d989772ab2498b900471d83e2 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Mon, 16 Sep 2019 15:56:55 -0700 Subject: [PATCH 71/97] Ignore references in with statements in getTypeOfDottedName --- src/compiler/checker.ts | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 501e6c4d92e..ca55f755b7b 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -16945,18 +16945,20 @@ namespace ts { // parameter symbols with declarations that have explicit type annotations. Such references are // resolvable with no possibility of triggering circularities in control flow analysis. function getTypeOfDottedName(node: Expression): Type | undefined { - switch (node.kind) { - case SyntaxKind.Identifier: - const symbol = getResolvedSymbol(node); - return getExplicitTypeOfSymbol(symbol.flags & SymbolFlags.Alias ? resolveAlias(symbol) : symbol); - case SyntaxKind.ThisKeyword: - return checkThisExpression(node); - case SyntaxKind.PropertyAccessExpression: - const type = getTypeOfDottedName((node).expression); - const prop = type && getPropertyOfType(type, (node).name.escapedText); - return prop && getExplicitTypeOfSymbol(prop); - case SyntaxKind.ParenthesizedExpression: - return getTypeOfDottedName((node).expression); + if (!(node.flags & NodeFlags.InWithStatement)) { + switch (node.kind) { + case SyntaxKind.Identifier: + const symbol = getResolvedSymbol(node); + return getExplicitTypeOfSymbol(symbol.flags & SymbolFlags.Alias ? resolveAlias(symbol) : symbol); + case SyntaxKind.ThisKeyword: + return checkThisExpression(node); + case SyntaxKind.PropertyAccessExpression: + const type = getTypeOfDottedName((node).expression); + const prop = type && getPropertyOfType(type, (node).name.escapedText); + return prop && getExplicitTypeOfSymbol(prop); + case SyntaxKind.ParenthesizedExpression: + return getTypeOfDottedName((node).expression); + } } } From 282a7aff6e984ddee05561a557b08034f35f113f Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Mon, 16 Sep 2019 15:58:42 -0700 Subject: [PATCH 72/97] Accept new API baselines --- tests/baselines/reference/api/tsserverlibrary.d.ts | 6 ++++-- tests/baselines/reference/api/typescript.d.ts | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 121c14b4056..3ebd9418f8b 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -298,8 +298,8 @@ declare namespace ts { TemplateSpan = 218, SemicolonClassElement = 219, Block = 220, - VariableStatement = 221, - EmptyStatement = 222, + EmptyStatement = 221, + VariableStatement = 222, ExpressionStatement = 223, IfStatement = 224, DoStatement = 225, @@ -423,6 +423,8 @@ declare namespace ts { LastTemplateToken = 17, FirstBinaryOperator = 28, LastBinaryOperator = 72, + FirstStatement = 222, + LastStatement = 238, FirstNode = 150, FirstJSDocNode = 290, LastJSDocNode = 314, diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index 22ed36d7ff9..e4d4648b172 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -298,8 +298,8 @@ declare namespace ts { TemplateSpan = 218, SemicolonClassElement = 219, Block = 220, - VariableStatement = 221, - EmptyStatement = 222, + EmptyStatement = 221, + VariableStatement = 222, ExpressionStatement = 223, IfStatement = 224, DoStatement = 225, @@ -423,6 +423,8 @@ declare namespace ts { LastTemplateToken = 17, FirstBinaryOperator = 28, LastBinaryOperator = 72, + FirstStatement = 222, + LastStatement = 238, FirstNode = 150, FirstJSDocNode = 290, LastJSDocNode = 314, From 8cbf69489cd1974354ae07c81a4f03dc251d84b3 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Tue, 17 Sep 2019 07:01:07 -0700 Subject: [PATCH 73/97] Cache last isReachableFlowNode result + switch statement CFA fix --- src/compiler/checker.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index ca55f755b7b..9e331dd0854 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -820,6 +820,8 @@ namespace ts { let flowLoopCount = 0; let sharedFlowCount = 0; let flowAnalysisDisabled = false; + let lastFlowNode: FlowNode | undefined; + let lastFlowNodeReachable: boolean; const emptyStringType = getLiteralType(""); const zeroType = getLiteralType(0); @@ -16995,11 +16997,17 @@ namespace ts { } function isReachableFlowNode(flow: FlowNode) { - return isReachableFlowNodeWorker(flow, /*skipCacheCheck*/ false); + const result = isReachableFlowNodeWorker(flow, /*skipCacheCheck*/ false); + lastFlowNode = flow; + lastFlowNodeReachable = result; + return result; } function isReachableFlowNodeWorker(flow: FlowNode, noCacheCheck: boolean): boolean { while (true) { + if (flow === lastFlowNode) { + return lastFlowNodeReachable; + } const flags = flow.flags; if (flags & FlowFlags.Shared) { if (!noCacheCheck) { @@ -17329,9 +17337,6 @@ namespace ts { } function getTypeAtSwitchClause(flow: FlowSwitchClause): FlowType { - if (flow.clauseStart === flow.clauseEnd && isExhaustiveSwitchStatement(flow.switchStatement)) { - return neverType; - } const expr = flow.switchStatement.expression; const flowType = getTypeAtFlowNode(flow.antecedent); let type = getTypeFromFlowType(flowType); @@ -17350,6 +17355,9 @@ namespace ts { else if (containsMatchingReferenceDiscriminant(reference, expr)) { type = declaredType; } + else if (flow.clauseStart === flow.clauseEnd && isExhaustiveSwitchStatement(flow.switchStatement)) { + return unreachableNeverType; + } return createFlowType(type, isIncomplete(flowType)); } From 946602599611aa542d558f0b68436f7cac0584df Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Tue, 17 Sep 2019 07:41:36 -0700 Subject: [PATCH 74/97] Accept new baselines --- tests/baselines/reference/narrowingByTypeofInSwitch.types | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/baselines/reference/narrowingByTypeofInSwitch.types b/tests/baselines/reference/narrowingByTypeofInSwitch.types index fbfe7fe92ee..f823c57770c 100644 --- a/tests/baselines/reference/narrowingByTypeofInSwitch.types +++ b/tests/baselines/reference/narrowingByTypeofInSwitch.types @@ -208,7 +208,7 @@ function testExtendsUnion(x: T) { assertAll(x); >assertAll(x) : Basic >assertAll : (x: Basic) => Basic ->x : never +>x : T } function testAny(x: any) { From ba30fdc4ae961298d1ac6a3e0b6fc3d1ac1c0e74 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Wed, 18 Sep 2019 06:36:51 -0700 Subject: [PATCH 75/97] Attach flow nodes only when allowUnreachableCode !== true --- src/compiler/binder.ts | 2 +- src/compiler/checker.ts | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index cf93ee17ebd..c9d3d15b9af 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -672,7 +672,7 @@ namespace ts { bindJSDoc(node); return; } - if (node.kind >= SyntaxKind.FirstStatement && node.kind <= SyntaxKind.LastStatement) { + if (node.kind >= SyntaxKind.FirstStatement && node.kind <= SyntaxKind.LastStatement && !options.allowUnreachableCode) { node.flowNode = currentFlow; } switch (node.kind) { diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 9e331dd0854..7e9e8bd4b0e 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -30539,8 +30539,7 @@ namespace ts { cancellationToken.throwIfCancellationRequested(); } } - if (kind >= SyntaxKind.FirstStatement && kind <= SyntaxKind.LastStatement && - !compilerOptions.allowUnreachableCode && node.flowNode && !isReachableFlowNode(node.flowNode)) { + if (kind >= SyntaxKind.FirstStatement && kind <= SyntaxKind.LastStatement && node.flowNode && !isReachableFlowNode(node.flowNode)) { errorOrSuggestion(compilerOptions.allowUnreachableCode === false, node, Diagnostics.Unreachable_code_detected); } From cafec556f338b1ef9bc06f88eb324a28df5b1d9b Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Wed, 18 Sep 2019 15:20:20 -0700 Subject: [PATCH 76/97] Properly handle try-finally statements in isReachableFlowNode --- src/compiler/checker.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 7e9e8bd4b0e..77814ccc544 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -17003,6 +17003,10 @@ namespace ts { return result; } + function isUnlockedReachableFlowNode(flow: FlowNode) { + return !(flow.flags & FlowFlags.PreFinally && (flow).lock.locked) && isReachableFlowNodeWorker(flow, /*skipCacheCheck*/ false); + } + function isReachableFlowNodeWorker(flow: FlowNode, noCacheCheck: boolean): boolean { while (true) { if (flow === lastFlowNode) { @@ -17017,8 +17021,8 @@ namespace ts { } noCacheCheck = false; } - if (flags & (FlowFlags.Assignment | FlowFlags.Condition | FlowFlags.ArrayMutation | FlowFlags.PreFinally | FlowFlags.AfterFinally)) { - flow = (flow).antecedent; + if (flags & (FlowFlags.Assignment | FlowFlags.Condition | FlowFlags.ArrayMutation | FlowFlags.PreFinally)) { + flow = (flow).antecedent; } else if (flags & FlowFlags.Call) { const signature = getEffectsSignature((flow).node); @@ -17029,7 +17033,7 @@ namespace ts { } else if (flags & FlowFlags.BranchLabel) { // A branching point is reachable if any branch is reachable. - return some((flow).antecedents, isReachableFlowNode); + return some((flow).antecedents, isUnlockedReachableFlowNode); } else if (flags & FlowFlags.LoopLabel) { // A loop is reachable if the control flow path that leads to the top is reachable. @@ -17043,6 +17047,14 @@ namespace ts { } flow = (flow).antecedent; } + else if (flags & FlowFlags.AfterFinally) { + // Cache is unreliable once we start locking nodes + lastFlowNode = undefined; + (flow).locked = true; + const result = isReachableFlowNodeWorker((flow).antecedent, /*skipCacheCheck*/ false); + (flow).locked = false; + return result; + } else { return !(flags & FlowFlags.Unreachable); } From 940231785e8b99df9af1ba39433ba10b120e51ca Mon Sep 17 00:00:00 2001 From: Nathan Fenner Date: Tue, 17 Sep 2019 15:00:37 -0700 Subject: [PATCH 77/97] report error on extra jsx prop instead of component name --- src/compiler/checker.ts | 10 ++++++++ .../checkJsxChildrenProperty15.errors.txt | 4 ++-- ...StringLiteralsInJsxAttributes02.errors.txt | 24 +++++++++---------- .../tsxAttributeResolution1.errors.txt | 12 +++++----- .../tsxAttributeResolution11.errors.txt | 4 ++-- .../tsxAttributeResolution15.errors.txt | 4 ++-- .../tsxElementResolution11.errors.txt | 4 ++-- .../tsxElementResolution3.errors.txt | 4 ++-- .../tsxElementResolution4.errors.txt | 4 ++-- .../tsxLibraryManagedAttributes.errors.txt | 16 ++++++------- ...tsxSpreadAttributesResolution14.errors.txt | 4 ++-- .../tsxSpreadAttributesResolution2.errors.txt | 4 ++-- ...elessFunctionComponentOverload4.errors.txt | 16 ++++++------- ...elessFunctionComponentOverload5.errors.txt | 4 ++-- ...tsxStatelessFunctionComponents1.errors.txt | 16 ++++++------- ...tsxStatelessFunctionComponents2.errors.txt | 4 ++-- .../reference/tsxUnionElementType4.errors.txt | 8 +++---- .../reference/tsxUnionElementType6.errors.txt | 4 ++-- 18 files changed, 78 insertions(+), 68 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 463d00c13e2..f361657e2f8 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -13040,6 +13040,16 @@ namespace ts { // JsxAttributes has an object-literal flag and undergo same type-assignablity check as normal object-literal. // However, using an object-literal error message will be very confusing to the users so we give different a message. // TODO: Spelling suggestions for excess jsx attributes (needs new diagnostic messages) + + if (errorNode && isJsxOpeningLikeElement(errorNode.parent)) { + const attributes = errorNode.parent.attributes; + for (const jsxProperty of attributes.properties) { + if (jsxProperty.kind === SyntaxKind.JsxAttribute && jsxProperty.name.escapedText === prop.escapedName) { + // Move the error node to the actual JSX property, instead of pointing to the identifier in the JSX element. + errorNode = jsxProperty; + } + } + } reportError(Diagnostics.Property_0_does_not_exist_on_type_1, symbolToString(prop), typeToString(errorTarget)); } else { diff --git a/tests/baselines/reference/checkJsxChildrenProperty15.errors.txt b/tests/baselines/reference/checkJsxChildrenProperty15.errors.txt index 605a293ffaa..fa034be0f18 100644 --- a/tests/baselines/reference/checkJsxChildrenProperty15.errors.txt +++ b/tests/baselines/reference/checkJsxChildrenProperty15.errors.txt @@ -1,4 +1,4 @@ -tests/cases/conformance/jsx/file.tsx(10,13): error TS2322: Type '{ children: Element; }' is not assignable to type 'IntrinsicAttributes'. +tests/cases/conformance/jsx/file.tsx(10,17): error TS2322: Type '{ children: Element; }' is not assignable to type 'IntrinsicAttributes'. Property 'children' does not exist on type 'IntrinsicAttributes'. tests/cases/conformance/jsx/file.tsx(11,13): error TS2322: Type '{ children: Element; key: string; }' is not assignable to type 'IntrinsicAttributes'. Property 'children' does not exist on type 'IntrinsicAttributes'. @@ -17,7 +17,7 @@ tests/cases/conformance/jsx/file.tsx(12,13): error TS2322: Type '{ children: Ele // Not OK (excess children) const k3 = } />; - ~~~ + ~~~~~~~~~~~~~~~~~~~~~~ !!! error TS2322: Type '{ children: Element; }' is not assignable to type 'IntrinsicAttributes'. !!! error TS2322: Property 'children' does not exist on type 'IntrinsicAttributes'. const k4 =
; diff --git a/tests/baselines/reference/contextuallyTypedStringLiteralsInJsxAttributes02.errors.txt b/tests/baselines/reference/contextuallyTypedStringLiteralsInJsxAttributes02.errors.txt index ff2ec7e5106..b66975fdb73 100644 --- a/tests/baselines/reference/contextuallyTypedStringLiteralsInJsxAttributes02.errors.txt +++ b/tests/baselines/reference/contextuallyTypedStringLiteralsInJsxAttributes02.errors.txt @@ -1,34 +1,34 @@ -tests/cases/conformance/types/contextualTypes/jsxAttributes/file.tsx(27,13): error TS2769: No overload matches this call. +tests/cases/conformance/types/contextualTypes/jsxAttributes/file.tsx(27,64): error TS2769: No overload matches this call. Overload 1 of 2, '(buttonProps: ButtonProps): Element', gave the following error. Type '{ extra: true; onClick: (k: "left" | "right") => void; }' is not assignable to type 'IntrinsicAttributes & ButtonProps'. Property 'extra' does not exist on type 'IntrinsicAttributes & ButtonProps'. Overload 2 of 2, '(linkProps: LinkProps): Element', gave the following error. Type '{ extra: true; onClick: (k: "left" | "right") => void; }' is not assignable to type 'IntrinsicAttributes & LinkProps'. Property 'extra' does not exist on type 'IntrinsicAttributes & LinkProps'. -tests/cases/conformance/types/contextualTypes/jsxAttributes/file.tsx(28,13): error TS2769: No overload matches this call. +tests/cases/conformance/types/contextualTypes/jsxAttributes/file.tsx(28,12): error TS2769: No overload matches this call. Overload 1 of 2, '(buttonProps: ButtonProps): Element', gave the following error. Type '{ onClick: (k: "left" | "right") => void; extra: true; }' is not assignable to type 'IntrinsicAttributes & ButtonProps'. Property 'extra' does not exist on type 'IntrinsicAttributes & ButtonProps'. Overload 2 of 2, '(linkProps: LinkProps): Element', gave the following error. Type '{ onClick: (k: "left" | "right") => void; extra: true; }' is not assignable to type 'IntrinsicAttributes & LinkProps'. Property 'onClick' does not exist on type 'IntrinsicAttributes & LinkProps'. -tests/cases/conformance/types/contextualTypes/jsxAttributes/file.tsx(29,13): error TS2769: No overload matches this call. +tests/cases/conformance/types/contextualTypes/jsxAttributes/file.tsx(29,43): error TS2769: No overload matches this call. Overload 1 of 2, '(buttonProps: ButtonProps): Element', gave the following error. Type '{ extra: true; goTo: string; }' is not assignable to type 'IntrinsicAttributes & ButtonProps'. Property 'extra' does not exist on type 'IntrinsicAttributes & ButtonProps'. Overload 2 of 2, '(linkProps: LinkProps): Element', gave the following error. Type '{ extra: true; goTo: "home"; }' is not assignable to type 'IntrinsicAttributes & LinkProps'. Property 'extra' does not exist on type 'IntrinsicAttributes & LinkProps'. -tests/cases/conformance/types/contextualTypes/jsxAttributes/file.tsx(30,13): error TS2769: No overload matches this call. +tests/cases/conformance/types/contextualTypes/jsxAttributes/file.tsx(30,12): error TS2769: No overload matches this call. Overload 1 of 2, '(buttonProps: ButtonProps): Element', gave the following error. Type '{ goTo: string; extra: true; }' is not assignable to type 'IntrinsicAttributes & ButtonProps'. Property 'goTo' does not exist on type 'IntrinsicAttributes & ButtonProps'. Overload 2 of 2, '(linkProps: LinkProps): Element', gave the following error. Type '{ goTo: "home"; extra: true; }' is not assignable to type 'IntrinsicAttributes & LinkProps'. Property 'extra' does not exist on type 'IntrinsicAttributes & LinkProps'. -tests/cases/conformance/types/contextualTypes/jsxAttributes/file.tsx(33,13): error TS2322: Type '{ extra: true; onClick: (k: "left" | "right") => void; }' is not assignable to type 'IntrinsicAttributes & ButtonProps'. +tests/cases/conformance/types/contextualTypes/jsxAttributes/file.tsx(33,65): error TS2322: Type '{ extra: true; onClick: (k: "left" | "right") => void; }' is not assignable to type 'IntrinsicAttributes & ButtonProps'. Property 'extra' does not exist on type 'IntrinsicAttributes & ButtonProps'. -tests/cases/conformance/types/contextualTypes/jsxAttributes/file.tsx(36,13): error TS2322: Type '{ extra: true; goTo: "home"; }' is not assignable to type 'IntrinsicAttributes & LinkProps'. +tests/cases/conformance/types/contextualTypes/jsxAttributes/file.tsx(36,44): error TS2322: Type '{ extra: true; goTo: "home"; }' is not assignable to type 'IntrinsicAttributes & LinkProps'. Property 'extra' does not exist on type 'IntrinsicAttributes & LinkProps'. @@ -60,7 +60,7 @@ tests/cases/conformance/types/contextualTypes/jsxAttributes/file.tsx(36,13): err } const b0 = {console.log(k)}}} extra />; // k has type "left" | "right" - ~~~~~~~~~~ + ~~~~~ !!! error TS2769: No overload matches this call. !!! error TS2769: Overload 1 of 2, '(buttonProps: ButtonProps): Element', gave the following error. !!! error TS2769: Type '{ extra: true; onClick: (k: "left" | "right") => void; }' is not assignable to type 'IntrinsicAttributes & ButtonProps'. @@ -69,7 +69,7 @@ tests/cases/conformance/types/contextualTypes/jsxAttributes/file.tsx(36,13): err !!! error TS2769: Type '{ extra: true; onClick: (k: "left" | "right") => void; }' is not assignable to type 'IntrinsicAttributes & LinkProps'. !!! error TS2769: Property 'extra' does not exist on type 'IntrinsicAttributes & LinkProps'. const b2 = {console.log(k)}} extra />; // k has type "left" | "right" - ~~~~~~~~~~ + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ !!! error TS2769: No overload matches this call. !!! error TS2769: Overload 1 of 2, '(buttonProps: ButtonProps): Element', gave the following error. !!! error TS2769: Type '{ onClick: (k: "left" | "right") => void; extra: true; }' is not assignable to type 'IntrinsicAttributes & ButtonProps'. @@ -78,7 +78,7 @@ tests/cases/conformance/types/contextualTypes/jsxAttributes/file.tsx(36,13): err !!! error TS2769: Type '{ onClick: (k: "left" | "right") => void; extra: true; }' is not assignable to type 'IntrinsicAttributes & LinkProps'. !!! error TS2769: Property 'onClick' does not exist on type 'IntrinsicAttributes & LinkProps'. const b3 = ; // goTo has type"home" | "contact" - ~~~~~~~~~~ + ~~~~~ !!! error TS2769: No overload matches this call. !!! error TS2769: Overload 1 of 2, '(buttonProps: ButtonProps): Element', gave the following error. !!! error TS2769: Type '{ extra: true; goTo: string; }' is not assignable to type 'IntrinsicAttributes & ButtonProps'. @@ -87,7 +87,7 @@ tests/cases/conformance/types/contextualTypes/jsxAttributes/file.tsx(36,13): err !!! error TS2769: Type '{ extra: true; goTo: "home"; }' is not assignable to type 'IntrinsicAttributes & LinkProps'. !!! error TS2769: Property 'extra' does not exist on type 'IntrinsicAttributes & LinkProps'. const b4 = ; // goTo has type "home" | "contact" - ~~~~~~~~~~ + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ !!! error TS2769: No overload matches this call. !!! error TS2769: Overload 1 of 2, '(buttonProps: ButtonProps): Element', gave the following error. !!! error TS2769: Type '{ goTo: string; extra: true; }' is not assignable to type 'IntrinsicAttributes & ButtonProps'. @@ -98,13 +98,13 @@ tests/cases/conformance/types/contextualTypes/jsxAttributes/file.tsx(36,13): err export function NoOverload(buttonProps: ButtonProps): JSX.Element { return undefined } const c1 = {console.log(k)}}} extra />; // k has type any - ~~~~~~~~~~ + ~~~~~ !!! error TS2322: Type '{ extra: true; onClick: (k: "left" | "right") => void; }' is not assignable to type 'IntrinsicAttributes & ButtonProps'. !!! error TS2322: Property 'extra' does not exist on type 'IntrinsicAttributes & ButtonProps'. export function NoOverload1(linkProps: LinkProps): JSX.Element { return undefined } const d1 = ; // goTo has type "home" | "contact" - ~~~~~~~~~~~ + ~~~~~ !!! error TS2322: Type '{ extra: true; goTo: "home"; }' is not assignable to type 'IntrinsicAttributes & LinkProps'. !!! error TS2322: Property 'extra' does not exist on type 'IntrinsicAttributes & LinkProps'. \ No newline at end of file diff --git a/tests/baselines/reference/tsxAttributeResolution1.errors.txt b/tests/baselines/reference/tsxAttributeResolution1.errors.txt index 9fc3e8f94da..00e35caf3a2 100644 --- a/tests/baselines/reference/tsxAttributeResolution1.errors.txt +++ b/tests/baselines/reference/tsxAttributeResolution1.errors.txt @@ -1,10 +1,10 @@ tests/cases/conformance/jsx/file.tsx(23,8): error TS2322: Type 'string' is not assignable to type 'number'. -tests/cases/conformance/jsx/file.tsx(24,2): error TS2322: Type '{ y: number; }' is not assignable to type 'Attribs1'. +tests/cases/conformance/jsx/file.tsx(24,8): error TS2322: Type '{ y: number; }' is not assignable to type 'Attribs1'. Property 'y' does not exist on type 'Attribs1'. -tests/cases/conformance/jsx/file.tsx(25,2): error TS2322: Type '{ y: string; }' is not assignable to type 'Attribs1'. +tests/cases/conformance/jsx/file.tsx(25,8): error TS2322: Type '{ y: string; }' is not assignable to type 'Attribs1'. Property 'y' does not exist on type 'Attribs1'. tests/cases/conformance/jsx/file.tsx(26,8): error TS2322: Type 'string' is not assignable to type 'number'. -tests/cases/conformance/jsx/file.tsx(27,2): error TS2322: Type '{ var: string; }' is not assignable to type 'Attribs1'. +tests/cases/conformance/jsx/file.tsx(27,8): error TS2322: Type '{ var: string; }' is not assignable to type 'Attribs1'. Property 'var' does not exist on type 'Attribs1'. tests/cases/conformance/jsx/file.tsx(29,2): error TS2741: Property 'reqd' is missing in type '{}' but required in type '{ reqd: string; }'. tests/cases/conformance/jsx/file.tsx(30,8): error TS2322: Type 'number' is not assignable to type 'string'. @@ -38,11 +38,11 @@ tests/cases/conformance/jsx/file.tsx(30,8): error TS2322: Type 'number' is not a !!! error TS2322: Type 'string' is not assignable to type 'number'. !!! related TS6500 tests/cases/conformance/jsx/file.tsx:10:2: The expected type comes from property 'x' which is declared here on type 'Attribs1' ; // Error, no property "y" - ~~~~~ + ~~~~~ !!! error TS2322: Type '{ y: number; }' is not assignable to type 'Attribs1'. !!! error TS2322: Property 'y' does not exist on type 'Attribs1'. ; // Error, no property "y" - ~~~~~ + ~~~~~~~ !!! error TS2322: Type '{ y: string; }' is not assignable to type 'Attribs1'. !!! error TS2322: Property 'y' does not exist on type 'Attribs1'. ; // Error, "32" is not number @@ -50,7 +50,7 @@ tests/cases/conformance/jsx/file.tsx(30,8): error TS2322: Type 'number' is not a !!! error TS2322: Type 'string' is not assignable to type 'number'. !!! related TS6500 tests/cases/conformance/jsx/file.tsx:10:2: The expected type comes from property 'x' which is declared here on type 'Attribs1' ; // Error, no 'var' property - ~~~~~ + ~~~~~~~~ !!! error TS2322: Type '{ var: string; }' is not assignable to type 'Attribs1'. !!! error TS2322: Property 'var' does not exist on type 'Attribs1'. diff --git a/tests/baselines/reference/tsxAttributeResolution11.errors.txt b/tests/baselines/reference/tsxAttributeResolution11.errors.txt index 6c6a23a9b70..163932ce2e0 100644 --- a/tests/baselines/reference/tsxAttributeResolution11.errors.txt +++ b/tests/baselines/reference/tsxAttributeResolution11.errors.txt @@ -1,4 +1,4 @@ -tests/cases/conformance/jsx/file.tsx(11,10): error TS2322: Type '{ bar: string; }' is not assignable to type 'IntrinsicAttributes & { ref?: string; }'. +tests/cases/conformance/jsx/file.tsx(11,22): error TS2322: Type '{ bar: string; }' is not assignable to type 'IntrinsicAttributes & { ref?: string; }'. Property 'bar' does not exist on type 'IntrinsicAttributes & { ref?: string; }'. @@ -27,7 +27,7 @@ tests/cases/conformance/jsx/file.tsx(11,10): error TS2322: Type '{ bar: string; // Should be an OK var x = ; - ~~~~~~~~~~~ + ~~~~~~~~~~~ !!! error TS2322: Type '{ bar: string; }' is not assignable to type 'IntrinsicAttributes & { ref?: string; }'. !!! error TS2322: Property 'bar' does not exist on type 'IntrinsicAttributes & { ref?: string; }'. diff --git a/tests/baselines/reference/tsxAttributeResolution15.errors.txt b/tests/baselines/reference/tsxAttributeResolution15.errors.txt index ca86178b578..b147464b840 100644 --- a/tests/baselines/reference/tsxAttributeResolution15.errors.txt +++ b/tests/baselines/reference/tsxAttributeResolution15.errors.txt @@ -1,4 +1,4 @@ -tests/cases/conformance/jsx/file.tsx(11,10): error TS2322: Type '{ prop1: string; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes & { children?: ReactNode; }'. +tests/cases/conformance/jsx/file.tsx(11,21): error TS2322: Type '{ prop1: string; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes & { children?: ReactNode; }'. Property 'prop1' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes & { children?: ReactNode; }'. tests/cases/conformance/jsx/file.tsx(14,44): error TS7017: Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature. @@ -15,7 +15,7 @@ tests/cases/conformance/jsx/file.tsx(14,44): error TS7017: Element implicitly ha // Error let a = - ~~~~~~~~~~ + ~~~~~~~~~~~~~ !!! error TS2322: Type '{ prop1: string; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes & { children?: ReactNode; }'. !!! error TS2322: Property 'prop1' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes & { children?: ReactNode; }'. diff --git a/tests/baselines/reference/tsxElementResolution11.errors.txt b/tests/baselines/reference/tsxElementResolution11.errors.txt index d08a6ed2721..59ce93a4ccf 100644 --- a/tests/baselines/reference/tsxElementResolution11.errors.txt +++ b/tests/baselines/reference/tsxElementResolution11.errors.txt @@ -1,4 +1,4 @@ -tests/cases/conformance/jsx/file.tsx(17,2): error TS2322: Type '{ x: number; }' is not assignable to type '{ q?: number; }'. +tests/cases/conformance/jsx/file.tsx(17,7): error TS2322: Type '{ x: number; }' is not assignable to type '{ q?: number; }'. Property 'x' does not exist on type '{ q?: number; }'. @@ -20,7 +20,7 @@ tests/cases/conformance/jsx/file.tsx(17,2): error TS2322: Type '{ x: number; }' } var Obj2: Obj2type; ; // Error - ~~~~ + ~~~~~~ !!! error TS2322: Type '{ x: number; }' is not assignable to type '{ q?: number; }'. !!! error TS2322: Property 'x' does not exist on type '{ q?: number; }'. diff --git a/tests/baselines/reference/tsxElementResolution3.errors.txt b/tests/baselines/reference/tsxElementResolution3.errors.txt index f220dbd8a5b..c5a11b9cbc1 100644 --- a/tests/baselines/reference/tsxElementResolution3.errors.txt +++ b/tests/baselines/reference/tsxElementResolution3.errors.txt @@ -1,4 +1,4 @@ -tests/cases/conformance/jsx/file.tsx(12,2): error TS2322: Type '{ w: string; }' is not assignable to type '{ n: string; }'. +tests/cases/conformance/jsx/file.tsx(12,7): error TS2322: Type '{ w: string; }' is not assignable to type '{ n: string; }'. Property 'w' does not exist on type '{ n: string; }'. @@ -15,6 +15,6 @@ tests/cases/conformance/jsx/file.tsx(12,2): error TS2322: Type '{ w: string; }' // Error ; - ~~~~ + ~~~~~~~ !!! error TS2322: Type '{ w: string; }' is not assignable to type '{ n: string; }'. !!! error TS2322: Property 'w' does not exist on type '{ n: string; }'. \ No newline at end of file diff --git a/tests/baselines/reference/tsxElementResolution4.errors.txt b/tests/baselines/reference/tsxElementResolution4.errors.txt index f76925030f2..ea2ae7b0073 100644 --- a/tests/baselines/reference/tsxElementResolution4.errors.txt +++ b/tests/baselines/reference/tsxElementResolution4.errors.txt @@ -1,4 +1,4 @@ -tests/cases/conformance/jsx/file.tsx(16,2): error TS2322: Type '{ q: string; }' is not assignable to type '{ m: string; }'. +tests/cases/conformance/jsx/file.tsx(16,7): error TS2322: Type '{ q: string; }' is not assignable to type '{ m: string; }'. Property 'q' does not exist on type '{ m: string; }'. @@ -19,7 +19,7 @@ tests/cases/conformance/jsx/file.tsx(16,2): error TS2322: Type '{ q: string; }' // Error ; - ~~~~ + ~~~~ !!! error TS2322: Type '{ q: string; }' is not assignable to type '{ m: string; }'. !!! error TS2322: Property 'q' does not exist on type '{ m: string; }'. \ No newline at end of file diff --git a/tests/baselines/reference/tsxLibraryManagedAttributes.errors.txt b/tests/baselines/reference/tsxLibraryManagedAttributes.errors.txt index b34ee77d1fb..6bf817f0c7d 100644 --- a/tests/baselines/reference/tsxLibraryManagedAttributes.errors.txt +++ b/tests/baselines/reference/tsxLibraryManagedAttributes.errors.txt @@ -1,22 +1,22 @@ tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(55,12): error TS2322: Type '{ foo: number; }' is not assignable to type 'Defaultize; bar: PropTypeChecker; baz: PropTypeChecker; }>, { foo: number; }>'. Type '{ foo: number; }' is missing the following properties from type '{ bar: string | number | ReactComponent<{}, {}> | null | undefined; baz: string; }': bar, baz -tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(57,12): error TS2322: Type '{ bar: string; baz: string; bat: string; }' is not assignable to type 'Defaultize; bar: PropTypeChecker; baz: PropTypeChecker; }>, { foo: number; }>'. +tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(57,41): error TS2322: Type '{ bar: string; baz: string; bat: string; }' is not assignable to type 'Defaultize; bar: PropTypeChecker; baz: PropTypeChecker; }>, { foo: number; }>'. Property 'bat' does not exist on type 'Defaultize; bar: PropTypeChecker; baz: PropTypeChecker; }>, { foo: number; }>'. tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(59,42): error TS2322: Type 'null' is not assignable to type 'string'. tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(69,26): error TS2322: Type 'string' is not assignable to type 'number | null | undefined'. tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(71,35): error TS2322: Type 'null' is not assignable to type 'ReactNode'. -tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(80,12): error TS2322: Type '{ foo: number; bar: string; }' is not assignable to type 'Defaultize<{}, { foo: number; }>'. +tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(80,38): error TS2322: Type '{ foo: number; bar: string; }' is not assignable to type 'Defaultize<{}, { foo: number; }>'. Property 'bar' does not exist on type 'Defaultize<{}, { foo: number; }>'. tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(81,29): error TS2322: Type 'string' is not assignable to type 'number | undefined'. tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(98,12): error TS2322: Type '{ foo: string; }' is not assignable to type 'Defaultize; bar: PropTypeChecker; baz: PropTypeChecker; }>, { foo: string; }>'. Type '{ foo: string; }' is missing the following properties from type '{ bar: string | number | ReactComponent<{}, {}> | null | undefined; baz: number; }': bar, baz -tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(100,12): error TS2322: Type '{ bar: string; baz: number; bat: string; }' is not assignable to type 'Defaultize; bar: PropTypeChecker; baz: PropTypeChecker; }>, { foo: string; }>'. +tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(100,56): error TS2322: Type '{ bar: string; baz: number; bat: string; }' is not assignable to type 'Defaultize; bar: PropTypeChecker; baz: PropTypeChecker; }>, { foo: string; }>'. Property 'bat' does not exist on type 'Defaultize; bar: PropTypeChecker; baz: PropTypeChecker; }>, { foo: string; }>'. tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(102,57): error TS2322: Type 'null' is not assignable to type 'number'. tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(111,46): error TS2322: Type 'number' is not assignable to type 'string'. tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(112,46): error TS2322: Type 'null' is not assignable to type 'string'. tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(113,57): error TS2322: Type 'null' is not assignable to type 'ReactNode'. -tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(122,12): error TS2322: Type '{ foo: string; bar: string; }' is not assignable to type 'Defaultize'. +tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(122,58): error TS2322: Type '{ foo: string; bar: string; }' is not assignable to type 'Defaultize'. Property 'bar' does not exist on type 'Defaultize'. tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(123,49): error TS2322: Type 'number' is not assignable to type 'string | undefined'. @@ -82,7 +82,7 @@ tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(123,49): error TS232 !!! error TS2322: Type '{ foo: number; }' is missing the following properties from type '{ bar: string | number | ReactComponent<{}, {}> | null | undefined; baz: string; }': bar, baz const c = ; const d = ; // Error, baz not a valid prop - ~~~~~~~~~ + ~~~~~~~~~~ !!! error TS2322: Type '{ bar: string; baz: string; bat: string; }' is not assignable to type 'Defaultize; bar: PropTypeChecker; baz: PropTypeChecker; }>, { foo: number; }>'. !!! error TS2322: Property 'bat' does not exist on type 'Defaultize; bar: PropTypeChecker; baz: PropTypeChecker; }>, { foo: number; }>'. const e = ; // bar is nullable/undefinable since it's not marked `isRequired` @@ -116,7 +116,7 @@ tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(123,49): error TS232 const k = ; const l = ; // error, no prop named bar - ~~~~~~~~~~~~~~~~ + ~~~~~~~~ !!! error TS2322: Type '{ foo: number; bar: string; }' is not assignable to type 'Defaultize<{}, { foo: number; }>'. !!! error TS2322: Property 'bar' does not exist on type 'Defaultize<{}, { foo: number; }>'. const m = ; // error, wrong type @@ -145,7 +145,7 @@ tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(123,49): error TS232 !!! error TS2322: Type '{ foo: string; }' is missing the following properties from type '{ bar: string | number | ReactComponent<{}, {}> | null | undefined; baz: number; }': bar, baz const p = ; const q = ; // Error, baz not a valid prop - ~~~~~~~~~~~~~~~~~~~~~~~~ + ~~~~~~~~~~ !!! error TS2322: Type '{ bar: string; baz: number; bat: string; }' is not assignable to type 'Defaultize; bar: PropTypeChecker; baz: PropTypeChecker; }>, { foo: string; }>'. !!! error TS2322: Property 'bat' does not exist on type 'Defaultize; bar: PropTypeChecker; baz: PropTypeChecker; }>, { foo: string; }>'. const r = ; // bar is nullable/undefinable since it's not marked `isRequired` @@ -181,7 +181,7 @@ tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(123,49): error TS232 const x = ; const y = ; // error, no prop named bar - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + ~~~~~~~~ !!! error TS2322: Type '{ foo: string; bar: string; }' is not assignable to type 'Defaultize'. !!! error TS2322: Property 'bar' does not exist on type 'Defaultize'. const z = ; // error, wrong type diff --git a/tests/baselines/reference/tsxSpreadAttributesResolution14.errors.txt b/tests/baselines/reference/tsxSpreadAttributesResolution14.errors.txt index 67beb30ccb0..f000257b9d5 100644 --- a/tests/baselines/reference/tsxSpreadAttributesResolution14.errors.txt +++ b/tests/baselines/reference/tsxSpreadAttributesResolution14.errors.txt @@ -1,4 +1,4 @@ -tests/cases/conformance/jsx/file.tsx(11,10): error TS2322: Type '{ Property1: true; property1: string; property2: number; }' is not assignable to type 'IntrinsicAttributes & AnotherComponentProps'. +tests/cases/conformance/jsx/file.tsx(11,38): error TS2322: Type '{ Property1: true; property1: string; property2: number; }' is not assignable to type 'IntrinsicAttributes & AnotherComponentProps'. Property 'Property1' does not exist on type 'IntrinsicAttributes & AnotherComponentProps'. @@ -14,7 +14,7 @@ tests/cases/conformance/jsx/file.tsx(11,10): error TS2322: Type '{ Property1: tr return ( // Error extra property - ~~~~~~~~~~~~~~~~ + ~~~~~~~~~ !!! error TS2322: Type '{ Property1: true; property1: string; property2: number; }' is not assignable to type 'IntrinsicAttributes & AnotherComponentProps'. !!! error TS2322: Property 'Property1' does not exist on type 'IntrinsicAttributes & AnotherComponentProps'. ); diff --git a/tests/baselines/reference/tsxSpreadAttributesResolution2.errors.txt b/tests/baselines/reference/tsxSpreadAttributesResolution2.errors.txt index d9748382f25..e46732e0cde 100644 --- a/tests/baselines/reference/tsxSpreadAttributesResolution2.errors.txt +++ b/tests/baselines/reference/tsxSpreadAttributesResolution2.errors.txt @@ -5,7 +5,7 @@ tests/cases/conformance/jsx/file.tsx(22,21): error TS2322: Type 'true' is not as tests/cases/conformance/jsx/file.tsx(23,10): error TS2322: Type '{ x: number; y: "2"; }' is not assignable to type 'PoisonedProp'. Types of property 'x' are incompatible. Type 'number' is not assignable to type 'string'. -tests/cases/conformance/jsx/file.tsx(24,11): error TS2322: Type '{ X: string; x: number; y: "2"; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes & PoisonedProp & { children?: ReactNode; }'. +tests/cases/conformance/jsx/file.tsx(24,40): error TS2322: Type '{ X: string; x: number; y: "2"; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes & PoisonedProp & { children?: ReactNode; }'. Property 'X' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes & PoisonedProp & { children?: ReactNode; }'. @@ -48,6 +48,6 @@ tests/cases/conformance/jsx/file.tsx(24,11): error TS2322: Type '{ X: string; x: !!! error TS2322: Types of property 'x' are incompatible. !!! error TS2322: Type 'number' is not assignable to type 'string'. let w1 = ; - ~~~~~~~~ + ~~~~~~ !!! error TS2322: Type '{ X: string; x: number; y: "2"; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes & PoisonedProp & { children?: ReactNode; }'. !!! error TS2322: Property 'X' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes & PoisonedProp & { children?: ReactNode; }'. \ No newline at end of file diff --git a/tests/baselines/reference/tsxStatelessFunctionComponentOverload4.errors.txt b/tests/baselines/reference/tsxStatelessFunctionComponentOverload4.errors.txt index 5318c77efae..43935105d84 100644 --- a/tests/baselines/reference/tsxStatelessFunctionComponentOverload4.errors.txt +++ b/tests/baselines/reference/tsxStatelessFunctionComponentOverload4.errors.txt @@ -1,23 +1,23 @@ -tests/cases/conformance/jsx/file.tsx(12,13): error TS2769: No overload matches this call. +tests/cases/conformance/jsx/file.tsx(12,22): error TS2769: No overload matches this call. Overload 1 of 2, '(): Element', gave the following error. Type '{ extraProp: true; }' is not assignable to type 'IntrinsicAttributes'. Property 'extraProp' does not exist on type 'IntrinsicAttributes'. Overload 2 of 2, '(l: { yy: number; yy1: string; }): Element', gave the following error. Type '{ extraProp: true; }' is not assignable to type 'IntrinsicAttributes & { yy: number; yy1: string; }'. Property 'extraProp' does not exist on type 'IntrinsicAttributes & { yy: number; yy1: string; }'. -tests/cases/conformance/jsx/file.tsx(13,13): error TS2769: No overload matches this call. +tests/cases/conformance/jsx/file.tsx(13,12): error TS2769: No overload matches this call. Overload 1 of 2, '(): Element', gave the following error. Type '{ yy: number; }' is not assignable to type 'IntrinsicAttributes'. Property 'yy' does not exist on type 'IntrinsicAttributes'. Overload 2 of 2, '(l: { yy: number; yy1: string; }): Element', gave the following error. Property 'yy1' is missing in type '{ yy: number; }' but required in type '{ yy: number; yy1: string; }'. -tests/cases/conformance/jsx/file.tsx(14,12): error TS2769: No overload matches this call. +tests/cases/conformance/jsx/file.tsx(14,31): error TS2769: No overload matches this call. Overload 1 of 2, '(): Element', gave the following error. Type '{ yy1: true; yy: number; }' is not assignable to type 'IntrinsicAttributes'. Property 'yy1' does not exist on type 'IntrinsicAttributes'. Overload 2 of 2, '(l: { yy: number; yy1: string; }): Element', gave the following error. Type 'true' is not assignable to type 'string'. -tests/cases/conformance/jsx/file.tsx(16,13): error TS2769: No overload matches this call. +tests/cases/conformance/jsx/file.tsx(16,31): error TS2769: No overload matches this call. Overload 1 of 2, '(): Element', gave the following error. Type '{ y1: number; yy: number; yy1: string; }' is not assignable to type 'IntrinsicAttributes'. Property 'y1' does not exist on type 'IntrinsicAttributes'. @@ -89,7 +89,7 @@ tests/cases/conformance/jsx/file.tsx(36,12): error TS2769: No overload matches t // Error const c0 = ; // extra property; - ~~~~~~~~ + ~~~~~~~~~ !!! error TS2769: No overload matches this call. !!! error TS2769: Overload 1 of 2, '(): Element', gave the following error. !!! error TS2769: Type '{ extraProp: true; }' is not assignable to type 'IntrinsicAttributes'. @@ -98,7 +98,7 @@ tests/cases/conformance/jsx/file.tsx(36,12): error TS2769: No overload matches t !!! error TS2769: Type '{ extraProp: true; }' is not assignable to type 'IntrinsicAttributes & { yy: number; yy1: string; }'. !!! error TS2769: Property 'extraProp' does not exist on type 'IntrinsicAttributes & { yy: number; yy1: string; }'. const c1 = ; // missing property; - ~~~~~~~~ + ~~~~~~~~~~~~~~~~~~~ !!! error TS2769: No overload matches this call. !!! error TS2769: Overload 1 of 2, '(): Element', gave the following error. !!! error TS2769: Type '{ yy: number; }' is not assignable to type 'IntrinsicAttributes'. @@ -107,7 +107,7 @@ tests/cases/conformance/jsx/file.tsx(36,12): error TS2769: No overload matches t !!! error TS2769: Property 'yy1' is missing in type '{ yy: number; }' but required in type '{ yy: number; yy1: string; }'. !!! related TS2728 tests/cases/conformance/jsx/file.tsx:3:43: 'yy1' is declared here. const c2 = ; // type incompatible; - ~~~~~~~~~~~~~~~~~~~~~~~~~ + ~~~ !!! error TS2769: No overload matches this call. !!! error TS2769: Overload 1 of 2, '(): Element', gave the following error. !!! error TS2769: Type '{ yy1: true; yy: number; }' is not assignable to type 'IntrinsicAttributes'. @@ -117,7 +117,7 @@ tests/cases/conformance/jsx/file.tsx(36,12): error TS2769: No overload matches t !!! related TS6500 tests/cases/conformance/jsx/file.tsx:3:43: The expected type comes from property 'yy1' which is declared here on type 'IntrinsicAttributes & { yy: number; yy1: string; }' const c3 = ; // This is OK becuase all attribute are spread const c4 = ; // extra property; - ~~~~~~~~ + ~~~~~~~~~~ !!! error TS2769: No overload matches this call. !!! error TS2769: Overload 1 of 2, '(): Element', gave the following error. !!! error TS2769: Type '{ y1: number; yy: number; yy1: string; }' is not assignable to type 'IntrinsicAttributes'. diff --git a/tests/baselines/reference/tsxStatelessFunctionComponentOverload5.errors.txt b/tests/baselines/reference/tsxStatelessFunctionComponentOverload5.errors.txt index faf129dda4b..aa2e4fe19c6 100644 --- a/tests/baselines/reference/tsxStatelessFunctionComponentOverload5.errors.txt +++ b/tests/baselines/reference/tsxStatelessFunctionComponentOverload5.errors.txt @@ -1,4 +1,4 @@ -tests/cases/conformance/jsx/file.tsx(48,13): error TS2769: No overload matches this call. +tests/cases/conformance/jsx/file.tsx(48,12): error TS2769: No overload matches this call. Overload 1 of 3, '(buttonProps: ButtonProps): Element', gave the following error. Type '{ children: string; to: string; onClick: (e: MouseEvent) => void; }' is not assignable to type 'IntrinsicAttributes & ButtonProps'. Property 'to' does not exist on type 'IntrinsicAttributes & ButtonProps'. @@ -80,7 +80,7 @@ tests/cases/conformance/jsx/file.tsx(56,12): error TS2769: No overload matches t // Error const b0 = {}}>GO; // extra property; - ~~~~~~~~~~ + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ !!! error TS2769: No overload matches this call. !!! error TS2769: Overload 1 of 3, '(buttonProps: ButtonProps): Element', gave the following error. !!! error TS2769: Type '{ children: string; to: string; onClick: (e: MouseEvent) => void; }' is not assignable to type 'IntrinsicAttributes & ButtonProps'. diff --git a/tests/baselines/reference/tsxStatelessFunctionComponents1.errors.txt b/tests/baselines/reference/tsxStatelessFunctionComponents1.errors.txt index 2ca7adfbc3e..a8ba635f7a7 100644 --- a/tests/baselines/reference/tsxStatelessFunctionComponents1.errors.txt +++ b/tests/baselines/reference/tsxStatelessFunctionComponents1.errors.txt @@ -1,12 +1,12 @@ -tests/cases/conformance/jsx/file.tsx(19,10): error TS2322: Type '{ naaame: string; }' is not assignable to type 'IntrinsicAttributes & { name: string; }'. +tests/cases/conformance/jsx/file.tsx(19,16): error TS2322: Type '{ naaame: string; }' is not assignable to type 'IntrinsicAttributes & { name: string; }'. Property 'naaame' does not exist on type 'IntrinsicAttributes & { name: string; }'. tests/cases/conformance/jsx/file.tsx(27,15): error TS2322: Type 'number' is not assignable to type 'string'. -tests/cases/conformance/jsx/file.tsx(29,10): error TS2322: Type '{ naaaaaaame: string; }' is not assignable to type 'IntrinsicAttributes & { name?: string; }'. +tests/cases/conformance/jsx/file.tsx(29,15): error TS2322: Type '{ naaaaaaame: string; }' is not assignable to type 'IntrinsicAttributes & { name?: string; }'. Property 'naaaaaaame' does not exist on type 'IntrinsicAttributes & { name?: string; }'. tests/cases/conformance/jsx/file.tsx(34,10): error TS2741: Property '"prop-name"' is missing in type '{ extra-prop-name: string; }' but required in type '{ "prop-name": string; }'. -tests/cases/conformance/jsx/file.tsx(37,10): error TS2322: Type '{ prop1: true; }' is not assignable to type 'IntrinsicAttributes'. +tests/cases/conformance/jsx/file.tsx(37,23): error TS2322: Type '{ prop1: true; }' is not assignable to type 'IntrinsicAttributes'. Property 'prop1' does not exist on type 'IntrinsicAttributes'. -tests/cases/conformance/jsx/file.tsx(38,11): error TS2322: Type '{ ref: (x: any) => any; }' is not assignable to type 'IntrinsicAttributes'. +tests/cases/conformance/jsx/file.tsx(38,24): error TS2322: Type '{ ref: (x: any) => any; }' is not assignable to type 'IntrinsicAttributes'. Property 'ref' does not exist on type 'IntrinsicAttributes'. tests/cases/conformance/jsx/file.tsx(41,16): error TS1005: ',' expected. tests/cases/conformance/jsx/file.tsx(45,11): error TS2559: Type '{ prop1: boolean; }' has no properties in common with type 'IntrinsicAttributes'. @@ -32,7 +32,7 @@ tests/cases/conformance/jsx/file.tsx(45,11): error TS2559: Type '{ prop1: boolea let a1 = ; // Error let b = ; - ~~~~~ + ~~~~~~~~~~~~~~ !!! error TS2322: Type '{ naaame: string; }' is not assignable to type 'IntrinsicAttributes & { name: string; }'. !!! error TS2322: Property 'naaame' does not exist on type 'IntrinsicAttributes & { name: string; }'. @@ -47,7 +47,7 @@ tests/cases/conformance/jsx/file.tsx(45,11): error TS2559: Type '{ prop1: boolea !!! error TS2322: Type 'number' is not assignable to type 'string'. // Error let f = ; - ~~~~ + ~~~~~~~~~~~~~~~ !!! error TS2322: Type '{ naaaaaaame: string; }' is not assignable to type 'IntrinsicAttributes & { name?: string; }'. !!! error TS2322: Property 'naaaaaaame' does not exist on type 'IntrinsicAttributes & { name?: string; }'. @@ -61,11 +61,11 @@ tests/cases/conformance/jsx/file.tsx(45,11): error TS2559: Type '{ prop1: boolea // Error let i = - ~~~~~~~~~~~~ + ~~~~~ !!! error TS2322: Type '{ prop1: true; }' is not assignable to type 'IntrinsicAttributes'. !!! error TS2322: Property 'prop1' does not exist on type 'IntrinsicAttributes'. let i1 = x.greeting.substr(10)} /> - ~~~~~~~~~~~~ + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ !!! error TS2322: Type '{ ref: (x: any) => any; }' is not assignable to type 'IntrinsicAttributes'. !!! error TS2322: Property 'ref' does not exist on type 'IntrinsicAttributes'. diff --git a/tests/baselines/reference/tsxStatelessFunctionComponents2.errors.txt b/tests/baselines/reference/tsxStatelessFunctionComponents2.errors.txt index 551d0e72523..57ef8c2efbf 100644 --- a/tests/baselines/reference/tsxStatelessFunctionComponents2.errors.txt +++ b/tests/baselines/reference/tsxStatelessFunctionComponents2.errors.txt @@ -1,4 +1,4 @@ -tests/cases/conformance/jsx/file.tsx(19,10): error TS2322: Type '{ ref: string; }' is not assignable to type 'IntrinsicAttributes & { name?: string; }'. +tests/cases/conformance/jsx/file.tsx(19,16): error TS2322: Type '{ ref: string; }' is not assignable to type 'IntrinsicAttributes & { name?: string; }'. Property 'ref' does not exist on type 'IntrinsicAttributes & { name?: string; }'. tests/cases/conformance/jsx/file.tsx(25,42): error TS2551: Property 'subtr' does not exist on type 'string'. Did you mean 'substr'? tests/cases/conformance/jsx/file.tsx(27,33): error TS2339: Property 'notARealProperty' does not exist on type 'BigGreeter'. @@ -25,7 +25,7 @@ tests/cases/conformance/jsx/file.tsx(35,26): error TS2339: Property 'propertyNot let b = ; // Error - not allowed to specify 'ref' on SFCs let c = ; - ~~~~~ + ~~~~~~~~~~~ !!! error TS2322: Type '{ ref: string; }' is not assignable to type 'IntrinsicAttributes & { name?: string; }'. !!! error TS2322: Property 'ref' does not exist on type 'IntrinsicAttributes & { name?: string; }'. diff --git a/tests/baselines/reference/tsxUnionElementType4.errors.txt b/tests/baselines/reference/tsxUnionElementType4.errors.txt index bca0229de54..2048b100c11 100644 --- a/tests/baselines/reference/tsxUnionElementType4.errors.txt +++ b/tests/baselines/reference/tsxUnionElementType4.errors.txt @@ -1,7 +1,7 @@ tests/cases/conformance/jsx/file.tsx(32,17): error TS2322: Type 'true' is not assignable to type 'never'. -tests/cases/conformance/jsx/file.tsx(33,10): error TS2322: Type '{ x: number; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes & { children?: ReactNode; }'. +tests/cases/conformance/jsx/file.tsx(33,21): error TS2322: Type '{ x: number; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes & { children?: ReactNode; }'. Property 'x' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes & { children?: ReactNode; }'. -tests/cases/conformance/jsx/file.tsx(34,10): error TS2322: Type '{ prop: true; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes & { children?: ReactNode; }'. +tests/cases/conformance/jsx/file.tsx(34,22): error TS2322: Type '{ prop: true; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes & { children?: ReactNode; }'. Property 'prop' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes & { children?: ReactNode; }'. @@ -42,11 +42,11 @@ tests/cases/conformance/jsx/file.tsx(34,10): error TS2322: Type '{ prop: true; } !!! error TS2322: Type 'true' is not assignable to type 'never'. !!! related TS6500 tests/cases/conformance/jsx/file.tsx:3:36: The expected type comes from property 'x' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes & { x: number; } & { children?: ReactNode; } & { x: string; } & { children?: ReactNode; }' let b = - ~~~~~~~~~~ + ~~~~~~ !!! error TS2322: Type '{ x: number; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes & { children?: ReactNode; }'. !!! error TS2322: Property 'x' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes & { children?: ReactNode; }'. let c = ; - ~~~~~~~~~~~ + ~~~~ !!! error TS2322: Type '{ prop: true; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes & { children?: ReactNode; }'. !!! error TS2322: Property 'prop' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes & { children?: ReactNode; }'. \ No newline at end of file diff --git a/tests/baselines/reference/tsxUnionElementType6.errors.txt b/tests/baselines/reference/tsxUnionElementType6.errors.txt index 790285f2978..cad93a38ae9 100644 --- a/tests/baselines/reference/tsxUnionElementType6.errors.txt +++ b/tests/baselines/reference/tsxUnionElementType6.errors.txt @@ -1,4 +1,4 @@ -tests/cases/conformance/jsx/file.tsx(18,10): error TS2322: Type '{ x: true; }' is not assignable to type 'IntrinsicAttributes'. +tests/cases/conformance/jsx/file.tsx(18,23): error TS2322: Type '{ x: true; }' is not assignable to type 'IntrinsicAttributes'. Property 'x' does not exist on type 'IntrinsicAttributes'. tests/cases/conformance/jsx/file.tsx(19,27): error TS2322: Type 'string' is not assignable to type 'boolean'. tests/cases/conformance/jsx/file.tsx(20,10): error TS2741: Property 'x' is missing in type '{}' but required in type '{ x: boolean; }'. @@ -24,7 +24,7 @@ tests/cases/conformance/jsx/file.tsx(21,10): error TS2741: Property 'x' is missi var SFC2AndEmptyComp = SFC2 || EmptySFC1; // Error let a = ; - ~~~~~~~~~~~~ + ~ !!! error TS2322: Type '{ x: true; }' is not assignable to type 'IntrinsicAttributes'. !!! error TS2322: Property 'x' does not exist on type 'IntrinsicAttributes'. let b = ; From dbc17229f79d19a07ad087e5f36f221f4b351e8d Mon Sep 17 00:00:00 2001 From: Nathan Fenner Date: Wed, 18 Sep 2019 14:42:38 -0700 Subject: [PATCH 78/97] report extraneous jsx attribute error on attribute name instead of entire attribute assignment --- src/compiler/checker.ts | 13 ++++--------- .../reference/tsxAttributeResolution1.errors.txt | 6 +++--- .../reference/tsxAttributeResolution11.errors.txt | 2 +- .../reference/tsxAttributeResolution15.errors.txt | 2 +- .../reference/tsxElementResolution11.errors.txt | 2 +- .../reference/tsxElementResolution3.errors.txt | 2 +- .../reference/tsxElementResolution4.errors.txt | 2 +- .../tsxLibraryManagedAttributes.errors.txt | 8 ++++---- .../tsxSpreadAttributesResolution2.errors.txt | 2 +- ...sxStatelessFunctionComponentOverload4.errors.txt | 2 +- .../tsxStatelessFunctionComponents1.errors.txt | 6 +++--- .../tsxStatelessFunctionComponents2.errors.txt | 2 +- 12 files changed, 22 insertions(+), 27 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index f361657e2f8..5833d2e9c8c 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -13040,15 +13040,10 @@ namespace ts { // JsxAttributes has an object-literal flag and undergo same type-assignablity check as normal object-literal. // However, using an object-literal error message will be very confusing to the users so we give different a message. // TODO: Spelling suggestions for excess jsx attributes (needs new diagnostic messages) - - if (errorNode && isJsxOpeningLikeElement(errorNode.parent)) { - const attributes = errorNode.parent.attributes; - for (const jsxProperty of attributes.properties) { - if (jsxProperty.kind === SyntaxKind.JsxAttribute && jsxProperty.name.escapedText === prop.escapedName) { - // Move the error node to the actual JSX property, instead of pointing to the identifier in the JSX element. - errorNode = jsxProperty; - } - } + if (prop.valueDeclaration && isNamedDeclaration(prop.valueDeclaration) && prop.valueDeclaration.name && prop.valueDeclaration.name.pos !== -1) { + // If the "children" attribute is extraneous `extra` then the declaration's name has no location. + // In that case, do not update the error location, since there's no name to point to. + errorNode = prop.valueDeclaration.name; } reportError(Diagnostics.Property_0_does_not_exist_on_type_1, symbolToString(prop), typeToString(errorTarget)); } diff --git a/tests/baselines/reference/tsxAttributeResolution1.errors.txt b/tests/baselines/reference/tsxAttributeResolution1.errors.txt index 00e35caf3a2..46ccb39e704 100644 --- a/tests/baselines/reference/tsxAttributeResolution1.errors.txt +++ b/tests/baselines/reference/tsxAttributeResolution1.errors.txt @@ -38,11 +38,11 @@ tests/cases/conformance/jsx/file.tsx(30,8): error TS2322: Type 'number' is not a !!! error TS2322: Type 'string' is not assignable to type 'number'. !!! related TS6500 tests/cases/conformance/jsx/file.tsx:10:2: The expected type comes from property 'x' which is declared here on type 'Attribs1' ; // Error, no property "y" - ~~~~~ + ~ !!! error TS2322: Type '{ y: number; }' is not assignable to type 'Attribs1'. !!! error TS2322: Property 'y' does not exist on type 'Attribs1'. ; // Error, no property "y" - ~~~~~~~ + ~ !!! error TS2322: Type '{ y: string; }' is not assignable to type 'Attribs1'. !!! error TS2322: Property 'y' does not exist on type 'Attribs1'. ; // Error, "32" is not number @@ -50,7 +50,7 @@ tests/cases/conformance/jsx/file.tsx(30,8): error TS2322: Type 'number' is not a !!! error TS2322: Type 'string' is not assignable to type 'number'. !!! related TS6500 tests/cases/conformance/jsx/file.tsx:10:2: The expected type comes from property 'x' which is declared here on type 'Attribs1' ; // Error, no 'var' property - ~~~~~~~~ + ~~~ !!! error TS2322: Type '{ var: string; }' is not assignable to type 'Attribs1'. !!! error TS2322: Property 'var' does not exist on type 'Attribs1'. diff --git a/tests/baselines/reference/tsxAttributeResolution11.errors.txt b/tests/baselines/reference/tsxAttributeResolution11.errors.txt index 163932ce2e0..d490faad314 100644 --- a/tests/baselines/reference/tsxAttributeResolution11.errors.txt +++ b/tests/baselines/reference/tsxAttributeResolution11.errors.txt @@ -27,7 +27,7 @@ tests/cases/conformance/jsx/file.tsx(11,22): error TS2322: Type '{ bar: string; // Should be an OK var x = ; - ~~~~~~~~~~~ + ~~~ !!! error TS2322: Type '{ bar: string; }' is not assignable to type 'IntrinsicAttributes & { ref?: string; }'. !!! error TS2322: Property 'bar' does not exist on type 'IntrinsicAttributes & { ref?: string; }'. diff --git a/tests/baselines/reference/tsxAttributeResolution15.errors.txt b/tests/baselines/reference/tsxAttributeResolution15.errors.txt index b147464b840..7ec97ba4675 100644 --- a/tests/baselines/reference/tsxAttributeResolution15.errors.txt +++ b/tests/baselines/reference/tsxAttributeResolution15.errors.txt @@ -15,7 +15,7 @@ tests/cases/conformance/jsx/file.tsx(14,44): error TS7017: Element implicitly ha // Error let a = - ~~~~~~~~~~~~~ + ~~~~~ !!! error TS2322: Type '{ prop1: string; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes & { children?: ReactNode; }'. !!! error TS2322: Property 'prop1' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes & { children?: ReactNode; }'. diff --git a/tests/baselines/reference/tsxElementResolution11.errors.txt b/tests/baselines/reference/tsxElementResolution11.errors.txt index 59ce93a4ccf..cc032610e2b 100644 --- a/tests/baselines/reference/tsxElementResolution11.errors.txt +++ b/tests/baselines/reference/tsxElementResolution11.errors.txt @@ -20,7 +20,7 @@ tests/cases/conformance/jsx/file.tsx(17,7): error TS2322: Type '{ x: number; }' } var Obj2: Obj2type; ; // Error - ~~~~~~ + ~ !!! error TS2322: Type '{ x: number; }' is not assignable to type '{ q?: number; }'. !!! error TS2322: Property 'x' does not exist on type '{ q?: number; }'. diff --git a/tests/baselines/reference/tsxElementResolution3.errors.txt b/tests/baselines/reference/tsxElementResolution3.errors.txt index c5a11b9cbc1..8f8679b3857 100644 --- a/tests/baselines/reference/tsxElementResolution3.errors.txt +++ b/tests/baselines/reference/tsxElementResolution3.errors.txt @@ -15,6 +15,6 @@ tests/cases/conformance/jsx/file.tsx(12,7): error TS2322: Type '{ w: string; }' // Error ; - ~~~~~~~ + ~ !!! error TS2322: Type '{ w: string; }' is not assignable to type '{ n: string; }'. !!! error TS2322: Property 'w' does not exist on type '{ n: string; }'. \ No newline at end of file diff --git a/tests/baselines/reference/tsxElementResolution4.errors.txt b/tests/baselines/reference/tsxElementResolution4.errors.txt index ea2ae7b0073..235adbb199a 100644 --- a/tests/baselines/reference/tsxElementResolution4.errors.txt +++ b/tests/baselines/reference/tsxElementResolution4.errors.txt @@ -19,7 +19,7 @@ tests/cases/conformance/jsx/file.tsx(16,7): error TS2322: Type '{ q: string; }' // Error ; - ~~~~ + ~ !!! error TS2322: Type '{ q: string; }' is not assignable to type '{ m: string; }'. !!! error TS2322: Property 'q' does not exist on type '{ m: string; }'. \ No newline at end of file diff --git a/tests/baselines/reference/tsxLibraryManagedAttributes.errors.txt b/tests/baselines/reference/tsxLibraryManagedAttributes.errors.txt index 6bf817f0c7d..4b4448e42f6 100644 --- a/tests/baselines/reference/tsxLibraryManagedAttributes.errors.txt +++ b/tests/baselines/reference/tsxLibraryManagedAttributes.errors.txt @@ -82,7 +82,7 @@ tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(123,49): error TS232 !!! error TS2322: Type '{ foo: number; }' is missing the following properties from type '{ bar: string | number | ReactComponent<{}, {}> | null | undefined; baz: string; }': bar, baz const c = ; const d = ; // Error, baz not a valid prop - ~~~~~~~~~~ + ~~~ !!! error TS2322: Type '{ bar: string; baz: string; bat: string; }' is not assignable to type 'Defaultize; bar: PropTypeChecker; baz: PropTypeChecker; }>, { foo: number; }>'. !!! error TS2322: Property 'bat' does not exist on type 'Defaultize; bar: PropTypeChecker; baz: PropTypeChecker; }>, { foo: number; }>'. const e = ; // bar is nullable/undefinable since it's not marked `isRequired` @@ -116,7 +116,7 @@ tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(123,49): error TS232 const k = ; const l = ; // error, no prop named bar - ~~~~~~~~ + ~~~ !!! error TS2322: Type '{ foo: number; bar: string; }' is not assignable to type 'Defaultize<{}, { foo: number; }>'. !!! error TS2322: Property 'bar' does not exist on type 'Defaultize<{}, { foo: number; }>'. const m = ; // error, wrong type @@ -145,7 +145,7 @@ tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(123,49): error TS232 !!! error TS2322: Type '{ foo: string; }' is missing the following properties from type '{ bar: string | number | ReactComponent<{}, {}> | null | undefined; baz: number; }': bar, baz const p = ; const q = ; // Error, baz not a valid prop - ~~~~~~~~~~ + ~~~ !!! error TS2322: Type '{ bar: string; baz: number; bat: string; }' is not assignable to type 'Defaultize; bar: PropTypeChecker; baz: PropTypeChecker; }>, { foo: string; }>'. !!! error TS2322: Property 'bat' does not exist on type 'Defaultize; bar: PropTypeChecker; baz: PropTypeChecker; }>, { foo: string; }>'. const r = ; // bar is nullable/undefinable since it's not marked `isRequired` @@ -181,7 +181,7 @@ tests/cases/conformance/jsx/tsxLibraryManagedAttributes.tsx(123,49): error TS232 const x = ; const y = ; // error, no prop named bar - ~~~~~~~~ + ~~~ !!! error TS2322: Type '{ foo: string; bar: string; }' is not assignable to type 'Defaultize'. !!! error TS2322: Property 'bar' does not exist on type 'Defaultize'. const z = ; // error, wrong type diff --git a/tests/baselines/reference/tsxSpreadAttributesResolution2.errors.txt b/tests/baselines/reference/tsxSpreadAttributesResolution2.errors.txt index e46732e0cde..a5f36e5ed08 100644 --- a/tests/baselines/reference/tsxSpreadAttributesResolution2.errors.txt +++ b/tests/baselines/reference/tsxSpreadAttributesResolution2.errors.txt @@ -48,6 +48,6 @@ tests/cases/conformance/jsx/file.tsx(24,40): error TS2322: Type '{ X: string; x: !!! error TS2322: Types of property 'x' are incompatible. !!! error TS2322: Type 'number' is not assignable to type 'string'. let w1 = ; - ~~~~~~ + ~ !!! error TS2322: Type '{ X: string; x: number; y: "2"; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes & PoisonedProp & { children?: ReactNode; }'. !!! error TS2322: Property 'X' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes & PoisonedProp & { children?: ReactNode; }'. \ No newline at end of file diff --git a/tests/baselines/reference/tsxStatelessFunctionComponentOverload4.errors.txt b/tests/baselines/reference/tsxStatelessFunctionComponentOverload4.errors.txt index 43935105d84..3a47e329162 100644 --- a/tests/baselines/reference/tsxStatelessFunctionComponentOverload4.errors.txt +++ b/tests/baselines/reference/tsxStatelessFunctionComponentOverload4.errors.txt @@ -117,7 +117,7 @@ tests/cases/conformance/jsx/file.tsx(36,12): error TS2769: No overload matches t !!! related TS6500 tests/cases/conformance/jsx/file.tsx:3:43: The expected type comes from property 'yy1' which is declared here on type 'IntrinsicAttributes & { yy: number; yy1: string; }' const c3 = ; // This is OK becuase all attribute are spread const c4 = ; // extra property; - ~~~~~~~~~~ + ~~ !!! error TS2769: No overload matches this call. !!! error TS2769: Overload 1 of 2, '(): Element', gave the following error. !!! error TS2769: Type '{ y1: number; yy: number; yy1: string; }' is not assignable to type 'IntrinsicAttributes'. diff --git a/tests/baselines/reference/tsxStatelessFunctionComponents1.errors.txt b/tests/baselines/reference/tsxStatelessFunctionComponents1.errors.txt index a8ba635f7a7..7827c0625a4 100644 --- a/tests/baselines/reference/tsxStatelessFunctionComponents1.errors.txt +++ b/tests/baselines/reference/tsxStatelessFunctionComponents1.errors.txt @@ -32,7 +32,7 @@ tests/cases/conformance/jsx/file.tsx(45,11): error TS2559: Type '{ prop1: boolea let a1 = ; // Error let b = ; - ~~~~~~~~~~~~~~ + ~~~~~~ !!! error TS2322: Type '{ naaame: string; }' is not assignable to type 'IntrinsicAttributes & { name: string; }'. !!! error TS2322: Property 'naaame' does not exist on type 'IntrinsicAttributes & { name: string; }'. @@ -47,7 +47,7 @@ tests/cases/conformance/jsx/file.tsx(45,11): error TS2559: Type '{ prop1: boolea !!! error TS2322: Type 'number' is not assignable to type 'string'. // Error let f = ; - ~~~~~~~~~~~~~~~ + ~~~~~~~~~~ !!! error TS2322: Type '{ naaaaaaame: string; }' is not assignable to type 'IntrinsicAttributes & { name?: string; }'. !!! error TS2322: Property 'naaaaaaame' does not exist on type 'IntrinsicAttributes & { name?: string; }'. @@ -65,7 +65,7 @@ tests/cases/conformance/jsx/file.tsx(45,11): error TS2559: Type '{ prop1: boolea !!! error TS2322: Type '{ prop1: true; }' is not assignable to type 'IntrinsicAttributes'. !!! error TS2322: Property 'prop1' does not exist on type 'IntrinsicAttributes'. let i1 = x.greeting.substr(10)} /> - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + ~~~ !!! error TS2322: Type '{ ref: (x: any) => any; }' is not assignable to type 'IntrinsicAttributes'. !!! error TS2322: Property 'ref' does not exist on type 'IntrinsicAttributes'. diff --git a/tests/baselines/reference/tsxStatelessFunctionComponents2.errors.txt b/tests/baselines/reference/tsxStatelessFunctionComponents2.errors.txt index 57ef8c2efbf..b6727eb5ca8 100644 --- a/tests/baselines/reference/tsxStatelessFunctionComponents2.errors.txt +++ b/tests/baselines/reference/tsxStatelessFunctionComponents2.errors.txt @@ -25,7 +25,7 @@ tests/cases/conformance/jsx/file.tsx(35,26): error TS2339: Property 'propertyNot let b = ; // Error - not allowed to specify 'ref' on SFCs let c = ; - ~~~~~~~~~~~ + ~~~ !!! error TS2322: Type '{ ref: string; }' is not assignable to type 'IntrinsicAttributes & { name?: string; }'. !!! error TS2322: Property 'ref' does not exist on type 'IntrinsicAttributes & { name?: string; }'. From 78057a64ac02a2d6b364fade502ff63edec2cc3e Mon Sep 17 00:00:00 2001 From: Jack Bates Date: Fri, 23 Aug 2019 12:57:44 -0700 Subject: [PATCH 79/97] Allow readonly arguments to Promise.all(), etc. --- src/lib/es2015.promise.d.ts | 22 +++++++++---------- .../reference/correctOrderOfPromiseMethod.js | 5 +++-- .../correctOrderOfPromiseMethod.symbols | 3 ++- .../correctOrderOfPromiseMethod.types | 14 +++++++----- ...inferFromGenericFunctionReturnTypes3.types | 4 ++-- .../baselines/reference/inferenceLimit.types | 4 ++-- .../compiler/correctOrderOfPromiseMethod.ts | 4 ++-- 7 files changed, 30 insertions(+), 26 deletions(-) diff --git a/src/lib/es2015.promise.d.ts b/src/lib/es2015.promise.d.ts index 83776137c33..45b0fa5dc59 100644 --- a/src/lib/es2015.promise.d.ts +++ b/src/lib/es2015.promise.d.ts @@ -18,7 +18,7 @@ interface PromiseConstructor { * @param values An array of Promises. * @returns A new Promise. */ - all(values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike , T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike, T9 | PromiseLike, T10 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>; + all(values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike , T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike, T9 | PromiseLike, T10 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>; /** * Creates a Promise that is resolved with an array of results when all of the provided Promises @@ -26,7 +26,7 @@ interface PromiseConstructor { * @param values An array of Promises. * @returns A new Promise. */ - all(values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike , T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike, T9 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>; + all(values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike , T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike, T9 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>; /** * Creates a Promise that is resolved with an array of results when all of the provided Promises @@ -34,7 +34,7 @@ interface PromiseConstructor { * @param values An array of Promises. * @returns A new Promise. */ - all(values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike , T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8]>; + all(values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike , T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8]>; /** * Creates a Promise that is resolved with an array of results when all of the provided Promises @@ -42,7 +42,7 @@ interface PromiseConstructor { * @param values An array of Promises. * @returns A new Promise. */ - all(values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike , T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7]>; + all(values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike , T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7]>; /** * Creates a Promise that is resolved with an array of results when all of the provided Promises @@ -50,7 +50,7 @@ interface PromiseConstructor { * @param values An array of Promises. * @returns A new Promise. */ - all(values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike , T5 | PromiseLike, T6 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6]>; + all(values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike , T5 | PromiseLike, T6 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6]>; /** * Creates a Promise that is resolved with an array of results when all of the provided Promises @@ -58,7 +58,7 @@ interface PromiseConstructor { * @param values An array of Promises. * @returns A new Promise. */ - all(values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike , T5 | PromiseLike]): Promise<[T1, T2, T3, T4, T5]>; + all(values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike , T5 | PromiseLike]): Promise<[T1, T2, T3, T4, T5]>; /** * Creates a Promise that is resolved with an array of results when all of the provided Promises @@ -66,7 +66,7 @@ interface PromiseConstructor { * @param values An array of Promises. * @returns A new Promise. */ - all(values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike ]): Promise<[T1, T2, T3, T4]>; + all(values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike ]): Promise<[T1, T2, T3, T4]>; /** * Creates a Promise that is resolved with an array of results when all of the provided Promises @@ -74,7 +74,7 @@ interface PromiseConstructor { * @param values An array of Promises. * @returns A new Promise. */ - all(values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike]): Promise<[T1, T2, T3]>; + all(values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike]): Promise<[T1, T2, T3]>; /** * Creates a Promise that is resolved with an array of results when all of the provided Promises @@ -82,7 +82,7 @@ interface PromiseConstructor { * @param values An array of Promises. * @returns A new Promise. */ - all(values: [T1 | PromiseLike, T2 | PromiseLike]): Promise<[T1, T2]>; + all(values: readonly [T1 | PromiseLike, T2 | PromiseLike]): Promise<[T1, T2]>; /** * Creates a Promise that is resolved with an array of results when all of the provided Promises @@ -90,7 +90,7 @@ interface PromiseConstructor { * @param values An array of Promises. * @returns A new Promise. */ - all(values: (T | PromiseLike)[]): Promise; + all(values: readonly (T | PromiseLike)[]): Promise; /** * Creates a Promise that is resolved or rejected when any of the provided Promises are resolved @@ -98,7 +98,7 @@ interface PromiseConstructor { * @param values An array of Promises. * @returns A new Promise. */ - race(values: T[]): Promise ? U : T>; + race(values: readonly T[]): Promise ? U : T>; /** * Creates a Promise that is resolved or rejected when any of the provided Promises are resolved diff --git a/tests/baselines/reference/correctOrderOfPromiseMethod.js b/tests/baselines/reference/correctOrderOfPromiseMethod.js index 58c98189a43..fadda95374f 100644 --- a/tests/baselines/reference/correctOrderOfPromiseMethod.js +++ b/tests/baselines/reference/correctOrderOfPromiseMethod.js @@ -15,7 +15,7 @@ async function countEverything(): Promise { const [resultA, resultB] = await Promise.all([ providerA(), providerB(), - ]); + ] as const); const dataA: A[] = resultA; const dataB: B[] = resultB; @@ -23,7 +23,8 @@ async function countEverything(): Promise { return dataA.length + dataB.length; } return 0; -} +} + //// [correctOrderOfPromiseMethod.js] var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { diff --git a/tests/baselines/reference/correctOrderOfPromiseMethod.symbols b/tests/baselines/reference/correctOrderOfPromiseMethod.symbols index 290b482e5c7..1e67e9444c6 100644 --- a/tests/baselines/reference/correctOrderOfPromiseMethod.symbols +++ b/tests/baselines/reference/correctOrderOfPromiseMethod.symbols @@ -43,7 +43,7 @@ async function countEverything(): Promise { providerB(), >providerB : Symbol(providerB, Decl(correctOrderOfPromiseMethod.ts, 11, 9)) - ]); + ] as const); const dataA: A[] = resultA; >dataA : Symbol(dataA, Decl(correctOrderOfPromiseMethod.ts, 18, 9)) @@ -69,3 +69,4 @@ async function countEverything(): Promise { } return 0; } + diff --git a/tests/baselines/reference/correctOrderOfPromiseMethod.types b/tests/baselines/reference/correctOrderOfPromiseMethod.types index 5e8eaa72f42..a6f2c46782e 100644 --- a/tests/baselines/reference/correctOrderOfPromiseMethod.types +++ b/tests/baselines/reference/correctOrderOfPromiseMethod.types @@ -28,12 +28,13 @@ async function countEverything(): Promise { const [resultA, resultB] = await Promise.all([ >resultA : A[] >resultB : B[] ->await Promise.all([ providerA(), providerB(), ]) : [A[], B[]] ->Promise.all([ providerA(), providerB(), ]) : Promise<[A[], B[]]> ->Promise.all : { (values: Iterable>): Promise; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike, T9 | PromiseLike, T10 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike, T9 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike]): Promise<[T1, T2, T3, T4, T5]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike]): Promise<[T1, T2, T3, T4]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike]): Promise<[T1, T2, T3]>; (values: [T1 | PromiseLike, T2 | PromiseLike]): Promise<[T1, T2]>; (values: (T | PromiseLike)[]): Promise; } +>await Promise.all([ providerA(), providerB(), ] as const) : [A[], B[]] +>Promise.all([ providerA(), providerB(), ] as const) : Promise<[A[], B[]]> +>Promise.all : { (values: Iterable>): Promise; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike, T9 | PromiseLike, T10 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike, T9 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike]): Promise<[T1, T2, T3, T4, T5]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike]): Promise<[T1, T2, T3, T4]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike]): Promise<[T1, T2, T3]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike]): Promise<[T1, T2]>; (values: readonly (T | PromiseLike)[]): Promise; } >Promise : PromiseConstructor ->all : { (values: Iterable>): Promise; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike, T9 | PromiseLike, T10 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike, T9 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike]): Promise<[T1, T2, T3, T4, T5]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike]): Promise<[T1, T2, T3, T4]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike]): Promise<[T1, T2, T3]>; (values: [T1 | PromiseLike, T2 | PromiseLike]): Promise<[T1, T2]>; (values: (T | PromiseLike)[]): Promise; } ->[ providerA(), providerB(), ] : [Promise, Promise] +>all : { (values: Iterable>): Promise; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike, T9 | PromiseLike, T10 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike, T9 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike]): Promise<[T1, T2, T3, T4, T5]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike]): Promise<[T1, T2, T3, T4]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike]): Promise<[T1, T2, T3]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike]): Promise<[T1, T2]>; (values: readonly (T | PromiseLike)[]): Promise; } +>[ providerA(), providerB(), ] as const : readonly [Promise, Promise] +>[ providerA(), providerB(), ] : readonly [Promise, Promise] providerA(), >providerA() : Promise @@ -43,7 +44,7 @@ async function countEverything(): Promise { >providerB() : Promise >providerB : () => Promise - ]); + ] as const); const dataA: A[] = resultA; >dataA : A[] @@ -70,3 +71,4 @@ async function countEverything(): Promise { return 0; >0 : 0 } + diff --git a/tests/baselines/reference/inferFromGenericFunctionReturnTypes3.types b/tests/baselines/reference/inferFromGenericFunctionReturnTypes3.types index 4c56070fd3c..f9192dd11ba 100644 --- a/tests/baselines/reference/inferFromGenericFunctionReturnTypes3.types +++ b/tests/baselines/reference/inferFromGenericFunctionReturnTypes3.types @@ -413,9 +413,9 @@ const f1: F = () => { return Promise.all([ >Promise.all([ { name: "David Gomes", age: 23, position: "GOALKEEPER", }, { name: "Cristiano Ronaldo", age: 33, position: "STRIKER", } ]) : Promise<[{ name: string; age: number; position: "GOALKEEPER"; }, { name: string; age: number; position: "STRIKER"; }]> ->Promise.all : { (values: Iterable>): Promise; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike, T9 | PromiseLike, T10 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike, T9 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike]): Promise<[T1, T2, T3, T4, T5]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike]): Promise<[T1, T2, T3, T4]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike]): Promise<[T1, T2, T3]>; (values: [T1 | PromiseLike, T2 | PromiseLike]): Promise<[T1, T2]>; (values: (T | PromiseLike)[]): Promise; } +>Promise.all : { (values: Iterable>): Promise; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike, T9 | PromiseLike, T10 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike, T9 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike]): Promise<[T1, T2, T3, T4, T5]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike]): Promise<[T1, T2, T3, T4]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike]): Promise<[T1, T2, T3]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike]): Promise<[T1, T2]>; (values: readonly (T | PromiseLike)[]): Promise; } >Promise : PromiseConstructor ->all : { (values: Iterable>): Promise; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike, T9 | PromiseLike, T10 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike, T9 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike]): Promise<[T1, T2, T3, T4, T5]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike]): Promise<[T1, T2, T3, T4]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike]): Promise<[T1, T2, T3]>; (values: [T1 | PromiseLike, T2 | PromiseLike]): Promise<[T1, T2]>; (values: (T | PromiseLike)[]): Promise; } +>all : { (values: Iterable>): Promise; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike, T9 | PromiseLike, T10 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike, T9 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike]): Promise<[T1, T2, T3, T4, T5]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike]): Promise<[T1, T2, T3, T4]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike]): Promise<[T1, T2, T3]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike]): Promise<[T1, T2]>; (values: readonly (T | PromiseLike)[]): Promise; } >[ { name: "David Gomes", age: 23, position: "GOALKEEPER", }, { name: "Cristiano Ronaldo", age: 33, position: "STRIKER", } ] : [{ name: string; age: number; position: "GOALKEEPER"; }, { name: string; age: number; position: "STRIKER"; }] { >{ name: "David Gomes", age: 23, position: "GOALKEEPER", } : { name: string; age: number; position: "GOALKEEPER"; } diff --git a/tests/baselines/reference/inferenceLimit.types b/tests/baselines/reference/inferenceLimit.types index 6aa7ab501ea..fc2836e6dea 100644 --- a/tests/baselines/reference/inferenceLimit.types +++ b/tests/baselines/reference/inferenceLimit.types @@ -76,9 +76,9 @@ export class BrokenClass { >Promise.all(result.map(populateItems)) .then((orders: Array) => { resolve(orders); }) : Promise >Promise.all(result.map(populateItems)) .then : (onfulfilled?: (value: unknown[]) => TResult1 | PromiseLike, onrejected?: (reason: any) => TResult2 | PromiseLike) => Promise >Promise.all(result.map(populateItems)) : Promise ->Promise.all : { (values: Iterable>): Promise; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike, T9 | PromiseLike, T10 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike, T9 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike]): Promise<[T1, T2, T3, T4, T5]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike]): Promise<[T1, T2, T3, T4]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike]): Promise<[T1, T2, T3]>; (values: [T1 | PromiseLike, T2 | PromiseLike]): Promise<[T1, T2]>; (values: (T | PromiseLike)[]): Promise; } +>Promise.all : { (values: Iterable>): Promise; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike, T9 | PromiseLike, T10 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike, T9 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike]): Promise<[T1, T2, T3, T4, T5]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike]): Promise<[T1, T2, T3, T4]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike]): Promise<[T1, T2, T3]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike]): Promise<[T1, T2]>; (values: readonly (T | PromiseLike)[]): Promise; } >Promise : PromiseConstructor ->all : { (values: Iterable>): Promise; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike, T9 | PromiseLike, T10 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike, T9 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike]): Promise<[T1, T2, T3, T4, T5]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike]): Promise<[T1, T2, T3, T4]>; (values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike]): Promise<[T1, T2, T3]>; (values: [T1 | PromiseLike, T2 | PromiseLike]): Promise<[T1, T2]>; (values: (T | PromiseLike)[]): Promise; } +>all : { (values: Iterable>): Promise; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike, T9 | PromiseLike, T10 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike, T9 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6, T7]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike]): Promise<[T1, T2, T3, T4, T5, T6]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike]): Promise<[T1, T2, T3, T4, T5]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike]): Promise<[T1, T2, T3, T4]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike]): Promise<[T1, T2, T3]>; (values: readonly [T1 | PromiseLike, T2 | PromiseLike]): Promise<[T1, T2]>; (values: readonly (T | PromiseLike)[]): Promise; } >result.map(populateItems) : Promise[] >result.map : (callbackfn: (value: MyModule.MyModel, index: number, array: MyModule.MyModel[]) => U, thisArg?: any) => U[] >result : MyModule.MyModel[] diff --git a/tests/cases/compiler/correctOrderOfPromiseMethod.ts b/tests/cases/compiler/correctOrderOfPromiseMethod.ts index 85f4be9843c..70c730c6b20 100644 --- a/tests/cases/compiler/correctOrderOfPromiseMethod.ts +++ b/tests/cases/compiler/correctOrderOfPromiseMethod.ts @@ -17,7 +17,7 @@ async function countEverything(): Promise { const [resultA, resultB] = await Promise.all([ providerA(), providerB(), - ]); + ] as const); const dataA: A[] = resultA; const dataB: B[] = resultB; @@ -25,4 +25,4 @@ async function countEverything(): Promise { return dataA.length + dataB.length; } return 0; -} \ No newline at end of file +} From 1cad8edfa74ce97d1e868b071997a5354f16f93e Mon Sep 17 00:00:00 2001 From: Nathan Fenner Date: Fri, 20 Sep 2019 14:00:11 -0700 Subject: [PATCH 80/97] check for SyntaxKind.JSXAttribute instead of located-ness of name prop --- src/compiler/checker.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 5833d2e9c8c..e7954aa5d63 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -13040,9 +13040,9 @@ namespace ts { // JsxAttributes has an object-literal flag and undergo same type-assignablity check as normal object-literal. // However, using an object-literal error message will be very confusing to the users so we give different a message. // TODO: Spelling suggestions for excess jsx attributes (needs new diagnostic messages) - if (prop.valueDeclaration && isNamedDeclaration(prop.valueDeclaration) && prop.valueDeclaration.name && prop.valueDeclaration.name.pos !== -1) { - // If the "children" attribute is extraneous `extra` then the declaration's name has no location. - // In that case, do not update the error location, since there's no name to point to. + if (prop.valueDeclaration && isJsxAttribute(prop.valueDeclaration)) { + // Note that extraneous children (as in `extra`) don't pass this check, + // since `children` is a SyntaxKind.PropertySignature instead of a SyntaxKind.JsxAttribute. errorNode = prop.valueDeclaration.name; } reportError(Diagnostics.Property_0_does_not_exist_on_type_1, symbolToString(prop), typeToString(errorTarget)); From 21b5418cef70ac23a95570d38758b75b90684b72 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Fri, 20 Sep 2019 17:17:39 -0700 Subject: [PATCH 81/97] Add tests --- .../exhaustiveSwitchImplicitReturn.ts | 14 ++ .../controlFlow/assertionTypePredicates1.ts | 128 +++++++++++ .../exhaustiveSwitchStatements1.ts | 199 ++++++++++++++++++ .../controlFlow/neverReturningFunctions1.ts | 158 ++++++++++++++ .../assertionsAndNonReturningFunctions.ts | 65 ++++++ 5 files changed, 564 insertions(+) create mode 100644 tests/cases/conformance/controlFlow/assertionTypePredicates1.ts create mode 100644 tests/cases/conformance/controlFlow/exhaustiveSwitchStatements1.ts create mode 100644 tests/cases/conformance/controlFlow/neverReturningFunctions1.ts create mode 100644 tests/cases/conformance/jsdoc/assertionsAndNonReturningFunctions.ts diff --git a/tests/cases/compiler/exhaustiveSwitchImplicitReturn.ts b/tests/cases/compiler/exhaustiveSwitchImplicitReturn.ts index b87baffdaf9..7355a5dba30 100644 --- a/tests/cases/compiler/exhaustiveSwitchImplicitReturn.ts +++ b/tests/cases/compiler/exhaustiveSwitchImplicitReturn.ts @@ -40,3 +40,17 @@ function foo5(bar: "a" | "b"): number { return 1; } } + +function foo6(bar: "a", a: boolean, b: boolean): number { + if (a) { + switch (bar) { + case "a": return 1; + } + } + else { + switch (b) { + case true: return -1; + case false: return 0; + } + } +} diff --git a/tests/cases/conformance/controlFlow/assertionTypePredicates1.ts b/tests/cases/conformance/controlFlow/assertionTypePredicates1.ts new file mode 100644 index 00000000000..b20cd42414b --- /dev/null +++ b/tests/cases/conformance/controlFlow/assertionTypePredicates1.ts @@ -0,0 +1,128 @@ +// @strict: true +// @declaration: true + +declare function isString(value: unknown): value is string; +declare function isArrayOfStrings(value: unknown): value is string[]; + +const assert: (value: unknown) => asserts value = value => {} + +declare function assertIsString(value: unknown): asserts value is string; +declare function assertIsArrayOfStrings(value: unknown): asserts value is string[]; +declare function assertDefined(value: T): asserts value is NonNullable; + +function f01(x: unknown) { + if (!!true) { + assert(typeof x === "string"); + x.length; + } + if (!!true) { + assert(x instanceof Error); + x.message; + } + if (!!true) { + assert(typeof x === "boolean" || typeof x === "number"); + x.toLocaleString; + } + if (!!true) { + assert(isArrayOfStrings(x)); + x[0].length; + } + if (!!true) { + assertIsArrayOfStrings(x); + x[0].length; + } + if (!!true) { + assert(x === undefined || typeof x === "string"); + x; // string | undefined + assertDefined(x); + x; // string + } +} + +function f02(x: string | undefined) { + if (!!true) { + assert(x); + x.length; + } + if (!!true) { + assert(x !== undefined); + x.length; + } + if (!!true) { + assertDefined(x); + x.length; + } +} + +function f03(x: string | undefined, assert: (value: unknown) => asserts value) { + assert(x); + x.length; +} + +namespace Debug { + export declare function assert(value: unknown, message?: string): asserts value; + export declare function assertDefined(value: T): asserts value is NonNullable; +} + +function f10(x: string | undefined) { + if (!!true) { + Debug.assert(x); + x.length; + } + if (!!true) { + Debug.assert(x !== undefined); + x.length; + } + if (!!true) { + Debug.assertDefined(x); + x.length; + } +} + +class Test { + assert(value: unknown): asserts value { + if (value) return; + throw new Error(); + } + isTest2(): this is Test2 { + return this instanceof Test2; + } + assertIsTest2(): asserts this is Test2 { + if (this instanceof Test2) return; + throw new Error(); + } + assertThis(): asserts this { + if (!this) return; + throw new Error(); + } + bar() { + this.assertThis(); + this; + } + foo(x: unknown) { + this.assert(typeof x === "string"); + x.length; + if (this.isTest2()) { + this.z; + } + this.assertIsTest2(); + this.z; + } +} + +class Test2 extends Test { + z = 0; +} + +// Invalid constructs + +declare let Q1: new (x: unknown) => x is string; +declare let Q2: new (x: boolean) => asserts x; +declare let Q3: new (x: unknown) => asserts x is string; + +declare class Wat { + get p1(): this is string; + set p1(x: this is string); + get p2(): asserts this is string; + set p2(x: asserts this is string); +} diff --git a/tests/cases/conformance/controlFlow/exhaustiveSwitchStatements1.ts b/tests/cases/conformance/controlFlow/exhaustiveSwitchStatements1.ts new file mode 100644 index 00000000000..9fd0f608ddb --- /dev/null +++ b/tests/cases/conformance/controlFlow/exhaustiveSwitchStatements1.ts @@ -0,0 +1,199 @@ +// @strict: true +// @allowUnreachableCode: false +// @declaration: true + +function f1(x: 1 | 2): string { + if (!!true) { + switch (x) { + case 1: return 'a'; + case 2: return 'b'; + } + x; // Unreachable + } + else { + throw 0; + } +} + +function f2(x: 1 | 2) { + let z: number; + switch (x) { + case 1: z = 10; break; + case 2: z = 20; break; + } + z; // Definitely assigned +} + +function f3(x: 1 | 2) { + switch (x) { + case 1: return 10; + case 2: return 20; + // Default considered reachable to allow defensive coding + default: throw new Error("Bad input"); + } +} + +// Repro from #11572 + +enum E { A, B } + +function f(e: E): number { + switch (e) { + case E.A: return 0 + case E.B: return 1 + } +} + +function g(e: E): number { + if (!true) + return -1 + else + switch (e) { + case E.A: return 0 + case E.B: return 1 + } +} + +// Repro from #12668 + +interface Square { kind: "square"; size: number; } + +interface Rectangle { kind: "rectangle"; width: number; height: number; } + +interface Circle { kind: "circle"; radius: number; } + +interface Triangle { kind: "triangle"; side: number; } + +type Shape = Square | Rectangle | Circle | Triangle; + +function area(s: Shape): number { + let area; + switch (s.kind) { + case "square": area = s.size * s.size; break; + case "rectangle": area = s.width * s.height; break; + case "circle": area = Math.PI * s.radius * s.radius; break; + case "triangle": area = Math.sqrt(3) / 4 * s.side * s.side; break; + } + return area; +} + +function areaWrapped(s: Shape): number { + let area; + area = (() => { + switch (s.kind) { + case "square": return s.size * s.size; + case "rectangle": return s.width * s.height; + case "circle": return Math.PI * s.radius * s.radius; + case "triangle": return Math.sqrt(3) / 4 * s.side * s.side; + } + })(); + return area; +} + +// Repro from #13241 + +enum MyEnum { + A, + B +} + +function thisGivesError(e: MyEnum): string { + let s: string; + switch (e) { + case MyEnum.A: s = "it was A"; break; + case MyEnum.B: s = "it was B"; break; + } + return s; +} + +function good1(e: MyEnum): string { + let s: string; + switch (e) { + case MyEnum.A: s = "it was A"; break; + case MyEnum.B: s = "it was B"; break; + default: s = "it was something else"; break; + } + return s; +} + +function good2(e: MyEnum): string { + switch (e) { + case MyEnum.A: return "it was A"; + case MyEnum.B: return "it was B"; + } +} + +// Repro from #18362 + +enum Level { + One, + Two, +} + +const doSomethingWithLevel = (level: Level) => { + let next: Level; + switch (level) { + case Level.One: + next = Level.Two; + break; + case Level.Two: + next = Level.One; + break; + } + return next; +}; + +// Repro from #20409 + +interface Square2 { + kind: "square"; + size: number; +} + +interface Circle2 { + kind: "circle"; + radius: number; +} + +type Shape2 = Square2 | Circle2; + +function withDefault(s1: Shape2, s2: Shape2): string { + switch (s1.kind) { + case "square": + return "1"; + case "circle": + switch (s2.kind) { + case "square": + return "2"; + case "circle": + return "3"; + default: + return "never"; + } + } +} + +function withoutDefault(s1: Shape2, s2: Shape2): string { + switch (s1.kind) { + case "square": + return "1"; + case "circle": + switch (s2.kind) { + case "square": + return "2"; + case "circle": + return "3"; + } + } +} + +// Repro from #20823 + +function test4(value: 1 | 2) { + let x: string; + switch (value) { + case 1: x = "one"; break; + case 2: x = "two"; break; + } + return x; +} diff --git a/tests/cases/conformance/controlFlow/neverReturningFunctions1.ts b/tests/cases/conformance/controlFlow/neverReturningFunctions1.ts new file mode 100644 index 00000000000..63e7ecf786f --- /dev/null +++ b/tests/cases/conformance/controlFlow/neverReturningFunctions1.ts @@ -0,0 +1,158 @@ +// @strict: true +// @allowUnreachableCode: false +// @declaration: true + +function fail(message?: string): never { + throw new Error(message); +} + +function f01(x: string | undefined) { + if (x === undefined) fail("undefined argument"); + x.length; // string +} + +function f02(x: number): number { + if (x >= 0) return x; + fail("negative number"); + x; // Unreachable +} + +function f03(x: string) { + x; // string + fail(); + x; // Unreachable +} + +function f11(x: string | undefined, fail: (message?: string) => never) { + if (x === undefined) fail("undefined argument"); + x.length; // string +} + +function f12(x: number, fail: (message?: string) => never): number { + if (x >= 0) return x; + fail("negative number"); + x; // Unreachable +} + +function f13(x: string, fail: (message?: string) => never) { + x; // string + fail(); + x; // Unreachable +} + +namespace Debug { + export declare function fail(message?: string): never; +} + +function f21(x: string | undefined) { + if (x === undefined) Debug.fail("undefined argument"); + x.length; // string +} + +function f22(x: number): number { + if (x >= 0) return x; + Debug.fail("negative number"); + x; // Unreachable +} + +function f23(x: string) { + x; // string + Debug.fail(); + x; // Unreachable +} + +function f24(x: string) { + x; // string + ((Debug).fail)(); + x; // Unreachable +} + +class Test { + fail(message?: string): never { + throw new Error(message); + } + f1(x: string | undefined) { + if (x === undefined) this.fail("undefined argument"); + x.length; // string + } + f2(x: number): number { + if (x >= 0) return x; + this.fail("negative number"); + x; // Unreachable + } + f3(x: string) { + x; // string + this.fail(); + x; // Unreachable + } +} + +function f30(x: string | number | undefined) { + if (typeof x === "string") { + fail(); + x; // Unreachable + } + else { + x; // number | undefined + if (x !== undefined) { + x; // number + fail(); + x; // Unreachable + } + else { + x; // undefined + fail(); + x; // Unreachable + } + x; // Unreachable + } + x; // Unreachable +} + +function f31(x: { a: string | number }) { + if (typeof x.a === "string") { + fail(); + x; // Unreachable + x.a; // Unreachable + } + x; // { a: string | number } + x.a; // number +} + +function f40(x: number) { + try { + x; + fail(); + x; // Unreachable + } + finally { + x; + fail(); + x; // Unreachable + } + x; // Unreachable +} + +function f41(x: number) { + try { + x; + } + finally { + x; + fail(); + x; // Unreachable + } + x; // Unreachable +} + +function f42(x: number) { + try { + x; + fail(); + x; // Unreachable + } + finally { + x; + } + x; // Unreachable +} diff --git a/tests/cases/conformance/jsdoc/assertionsAndNonReturningFunctions.ts b/tests/cases/conformance/jsdoc/assertionsAndNonReturningFunctions.ts new file mode 100644 index 00000000000..9a15a5b5245 --- /dev/null +++ b/tests/cases/conformance/jsdoc/assertionsAndNonReturningFunctions.ts @@ -0,0 +1,65 @@ +// @allowJs: true +// @checkJs: true +// @noEmit: true +// @allowUnreachableCode: false +// @filename: assertionsAndNonReturningFunctions.js + +/** @typedef {(check: boolean) => asserts check} AssertFunc */ + +/** @type {AssertFunc} */ +const assert = check => { + if (!check) throw new Error(); +} + +/** @type {(x: unknown) => asserts x is string } */ +function assertIsString(x) { + if (!(typeof x === "string")) throw new Error(); +} + +/** + * @param {boolean} check + * @returns {asserts check} +*/ +function assert2(check) { + if (!check) throw new Error(); +} + +/** + * @returns {never} + */ +function fail() { + throw new Error(); +} + +/** + * @param {*} x + */ +function f1(x) { + if (!!true) { + assert(typeof x === "string"); + x.length; + } + if (!!true) { + assert2(typeof x === "string"); + x.length; + } + if (!!true) { + assertIsString(x); + x.length; + } + if (!!true) { + fail(); + x; // Unreachable + } +} + +/** + * @param {boolean} b + */ +function f2(b) { + switch (b) { + case true: return 1; + case false: return 0; + } + b; // Unreachable +} From 97d69d442d78b30caef06101b27758d7d97c8e3f Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Fri, 20 Sep 2019 17:18:08 -0700 Subject: [PATCH 82/97] Accept new baselines --- .../assertionTypePredicates1.errors.txt | 150 +++++ .../reference/assertionTypePredicates1.js | 289 +++++++++ .../assertionTypePredicates1.symbols | 359 +++++++++++ .../reference/assertionTypePredicates1.types | 438 +++++++++++++ ...ertionsAndNonReturningFunctions.errors.txt | 69 ++ ...assertionsAndNonReturningFunctions.symbols | 109 ++++ .../assertionsAndNonReturningFunctions.types | 152 +++++ .../exhaustiveSwitchImplicitReturn.errors.txt | 14 + .../exhaustiveSwitchImplicitReturn.js | 27 + .../exhaustiveSwitchImplicitReturn.symbols | 25 + .../exhaustiveSwitchImplicitReturn.types | 33 + .../exhaustiveSwitchStatements1.errors.txt | 202 ++++++ .../reference/exhaustiveSwitchStatements1.js | 437 +++++++++++++ .../exhaustiveSwitchStatements1.symbols | 503 +++++++++++++++ .../exhaustiveSwitchStatements1.types | 595 ++++++++++++++++++ .../neverReturningFunctions1.errors.txt | 227 +++++++ .../reference/neverReturningFunctions1.js | 337 ++++++++++ .../neverReturningFunctions1.symbols | 387 ++++++++++++ .../reference/neverReturningFunctions1.types | 439 +++++++++++++ 19 files changed, 4792 insertions(+) create mode 100644 tests/baselines/reference/assertionTypePredicates1.errors.txt create mode 100644 tests/baselines/reference/assertionTypePredicates1.js create mode 100644 tests/baselines/reference/assertionTypePredicates1.symbols create mode 100644 tests/baselines/reference/assertionTypePredicates1.types create mode 100644 tests/baselines/reference/assertionsAndNonReturningFunctions.errors.txt create mode 100644 tests/baselines/reference/assertionsAndNonReturningFunctions.symbols create mode 100644 tests/baselines/reference/assertionsAndNonReturningFunctions.types create mode 100644 tests/baselines/reference/exhaustiveSwitchStatements1.errors.txt create mode 100644 tests/baselines/reference/exhaustiveSwitchStatements1.js create mode 100644 tests/baselines/reference/exhaustiveSwitchStatements1.symbols create mode 100644 tests/baselines/reference/exhaustiveSwitchStatements1.types create mode 100644 tests/baselines/reference/neverReturningFunctions1.errors.txt create mode 100644 tests/baselines/reference/neverReturningFunctions1.js create mode 100644 tests/baselines/reference/neverReturningFunctions1.symbols create mode 100644 tests/baselines/reference/neverReturningFunctions1.types diff --git a/tests/baselines/reference/assertionTypePredicates1.errors.txt b/tests/baselines/reference/assertionTypePredicates1.errors.txt new file mode 100644 index 00000000000..91d2e869bd2 --- /dev/null +++ b/tests/baselines/reference/assertionTypePredicates1.errors.txt @@ -0,0 +1,150 @@ +tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(116,37): error TS1228: A type predicate is only allowed in return type position for functions and methods. +tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(117,37): error TS1228: A type predicate is only allowed in return type position for functions and methods. +tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(118,37): error TS1228: A type predicate is only allowed in return type position for functions and methods. +tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(121,15): error TS1228: A type predicate is only allowed in return type position for functions and methods. +tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(122,15): error TS1228: A type predicate is only allowed in return type position for functions and methods. +tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(123,15): error TS1228: A type predicate is only allowed in return type position for functions and methods. +tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(124,15): error TS1228: A type predicate is only allowed in return type position for functions and methods. + + +==== tests/cases/conformance/controlFlow/assertionTypePredicates1.ts (7 errors) ==== + declare function isString(value: unknown): value is string; + declare function isArrayOfStrings(value: unknown): value is string[]; + + const assert: (value: unknown) => asserts value = value => {} + + declare function assertIsString(value: unknown): asserts value is string; + declare function assertIsArrayOfStrings(value: unknown): asserts value is string[]; + declare function assertDefined(value: T): asserts value is NonNullable; + + function f01(x: unknown) { + if (!!true) { + assert(typeof x === "string"); + x.length; + } + if (!!true) { + assert(x instanceof Error); + x.message; + } + if (!!true) { + assert(typeof x === "boolean" || typeof x === "number"); + x.toLocaleString; + } + if (!!true) { + assert(isArrayOfStrings(x)); + x[0].length; + } + if (!!true) { + assertIsArrayOfStrings(x); + x[0].length; + } + if (!!true) { + assert(x === undefined || typeof x === "string"); + x; // string | undefined + assertDefined(x); + x; // string + } + } + + function f02(x: string | undefined) { + if (!!true) { + assert(x); + x.length; + } + if (!!true) { + assert(x !== undefined); + x.length; + } + if (!!true) { + assertDefined(x); + x.length; + } + } + + function f03(x: string | undefined, assert: (value: unknown) => asserts value) { + assert(x); + x.length; + } + + namespace Debug { + export declare function assert(value: unknown, message?: string): asserts value; + export declare function assertDefined(value: T): asserts value is NonNullable; + } + + function f10(x: string | undefined) { + if (!!true) { + Debug.assert(x); + x.length; + } + if (!!true) { + Debug.assert(x !== undefined); + x.length; + } + if (!!true) { + Debug.assertDefined(x); + x.length; + } + } + + class Test { + assert(value: unknown): asserts value { + if (value) return; + throw new Error(); + } + isTest2(): this is Test2 { + return this instanceof Test2; + } + assertIsTest2(): asserts this is Test2 { + if (this instanceof Test2) return; + throw new Error(); + } + assertThis(): asserts this { + if (!this) return; + throw new Error(); + } + bar() { + this.assertThis(); + this; + } + foo(x: unknown) { + this.assert(typeof x === "string"); + x.length; + if (this.isTest2()) { + this.z; + } + this.assertIsTest2(); + this.z; + } + } + + class Test2 extends Test { + z = 0; + } + + // Invalid constructs + + declare let Q1: new (x: unknown) => x is string; + ~~~~~~~~~~~ +!!! error TS1228: A type predicate is only allowed in return type position for functions and methods. + declare let Q2: new (x: boolean) => asserts x; + ~~~~~~~~~ +!!! error TS1228: A type predicate is only allowed in return type position for functions and methods. + declare let Q3: new (x: unknown) => asserts x is string; + ~~~~~~~~~~~~~~~~~~~ +!!! error TS1228: A type predicate is only allowed in return type position for functions and methods. + + declare class Wat { + get p1(): this is string; + ~~~~~~~~~~~~~~ +!!! error TS1228: A type predicate is only allowed in return type position for functions and methods. + set p1(x: this is string); + ~~~~~~~~~~~~~~ +!!! error TS1228: A type predicate is only allowed in return type position for functions and methods. + get p2(): asserts this is string; + ~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1228: A type predicate is only allowed in return type position for functions and methods. + set p2(x: asserts this is string); + ~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1228: A type predicate is only allowed in return type position for functions and methods. + } + \ No newline at end of file diff --git a/tests/baselines/reference/assertionTypePredicates1.js b/tests/baselines/reference/assertionTypePredicates1.js new file mode 100644 index 00000000000..ad21d116b73 --- /dev/null +++ b/tests/baselines/reference/assertionTypePredicates1.js @@ -0,0 +1,289 @@ +//// [assertionTypePredicates1.ts] +declare function isString(value: unknown): value is string; +declare function isArrayOfStrings(value: unknown): value is string[]; + +const assert: (value: unknown) => asserts value = value => {} + +declare function assertIsString(value: unknown): asserts value is string; +declare function assertIsArrayOfStrings(value: unknown): asserts value is string[]; +declare function assertDefined(value: T): asserts value is NonNullable; + +function f01(x: unknown) { + if (!!true) { + assert(typeof x === "string"); + x.length; + } + if (!!true) { + assert(x instanceof Error); + x.message; + } + if (!!true) { + assert(typeof x === "boolean" || typeof x === "number"); + x.toLocaleString; + } + if (!!true) { + assert(isArrayOfStrings(x)); + x[0].length; + } + if (!!true) { + assertIsArrayOfStrings(x); + x[0].length; + } + if (!!true) { + assert(x === undefined || typeof x === "string"); + x; // string | undefined + assertDefined(x); + x; // string + } +} + +function f02(x: string | undefined) { + if (!!true) { + assert(x); + x.length; + } + if (!!true) { + assert(x !== undefined); + x.length; + } + if (!!true) { + assertDefined(x); + x.length; + } +} + +function f03(x: string | undefined, assert: (value: unknown) => asserts value) { + assert(x); + x.length; +} + +namespace Debug { + export declare function assert(value: unknown, message?: string): asserts value; + export declare function assertDefined(value: T): asserts value is NonNullable; +} + +function f10(x: string | undefined) { + if (!!true) { + Debug.assert(x); + x.length; + } + if (!!true) { + Debug.assert(x !== undefined); + x.length; + } + if (!!true) { + Debug.assertDefined(x); + x.length; + } +} + +class Test { + assert(value: unknown): asserts value { + if (value) return; + throw new Error(); + } + isTest2(): this is Test2 { + return this instanceof Test2; + } + assertIsTest2(): asserts this is Test2 { + if (this instanceof Test2) return; + throw new Error(); + } + assertThis(): asserts this { + if (!this) return; + throw new Error(); + } + bar() { + this.assertThis(); + this; + } + foo(x: unknown) { + this.assert(typeof x === "string"); + x.length; + if (this.isTest2()) { + this.z; + } + this.assertIsTest2(); + this.z; + } +} + +class Test2 extends Test { + z = 0; +} + +// Invalid constructs + +declare let Q1: new (x: unknown) => x is string; +declare let Q2: new (x: boolean) => asserts x; +declare let Q3: new (x: unknown) => asserts x is string; + +declare class Wat { + get p1(): this is string; + set p1(x: this is string); + get p2(): asserts this is string; + set p2(x: asserts this is string); +} + + +//// [assertionTypePredicates1.js] +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +var assert = function (value) { }; +function f01(x) { + if (!!true) { + assert(typeof x === "string"); + x.length; + } + if (!!true) { + assert(x instanceof Error); + x.message; + } + if (!!true) { + assert(typeof x === "boolean" || typeof x === "number"); + x.toLocaleString; + } + if (!!true) { + assert(isArrayOfStrings(x)); + x[0].length; + } + if (!!true) { + assertIsArrayOfStrings(x); + x[0].length; + } + if (!!true) { + assert(x === undefined || typeof x === "string"); + x; // string | undefined + assertDefined(x); + x; // string + } +} +function f02(x) { + if (!!true) { + assert(x); + x.length; + } + if (!!true) { + assert(x !== undefined); + x.length; + } + if (!!true) { + assertDefined(x); + x.length; + } +} +function f03(x, assert) { + assert(x); + x.length; +} +var Debug; +(function (Debug) { +})(Debug || (Debug = {})); +function f10(x) { + if (!!true) { + Debug.assert(x); + x.length; + } + if (!!true) { + Debug.assert(x !== undefined); + x.length; + } + if (!!true) { + Debug.assertDefined(x); + x.length; + } +} +var Test = /** @class */ (function () { + function Test() { + } + Test.prototype.assert = function (value) { + if (value) + return; + throw new Error(); + }; + Test.prototype.isTest2 = function () { + return this instanceof Test2; + }; + Test.prototype.assertIsTest2 = function () { + if (this instanceof Test2) + return; + throw new Error(); + }; + Test.prototype.assertThis = function () { + if (!this) + return; + throw new Error(); + }; + Test.prototype.bar = function () { + this.assertThis(); + this; + }; + Test.prototype.foo = function (x) { + this.assert(typeof x === "string"); + x.length; + if (this.isTest2()) { + this.z; + } + this.assertIsTest2(); + this.z; + }; + return Test; +}()); +var Test2 = /** @class */ (function (_super) { + __extends(Test2, _super); + function Test2() { + var _this = _super !== null && _super.apply(this, arguments) || this; + _this.z = 0; + return _this; + } + return Test2; +}(Test)); + + +//// [assertionTypePredicates1.d.ts] +declare function isString(value: unknown): value is string; +declare function isArrayOfStrings(value: unknown): value is string[]; +declare const assert: (value: unknown) => asserts value; +declare function assertIsString(value: unknown): asserts value is string; +declare function assertIsArrayOfStrings(value: unknown): asserts value is string[]; +declare function assertDefined(value: T): asserts value is NonNullable; +declare function f01(x: unknown): void; +declare function f02(x: string | undefined): void; +declare function f03(x: string | undefined, assert: (value: unknown) => asserts value): void; +declare namespace Debug { + function assert(value: unknown, message?: string): asserts value; + function assertDefined(value: T): asserts value is NonNullable; +} +declare function f10(x: string | undefined): void; +declare class Test { + assert(value: unknown): asserts value; + isTest2(): this is Test2; + assertIsTest2(): asserts this is Test2; + assertThis(): asserts this; + bar(): void; + foo(x: unknown): void; +} +declare class Test2 extends Test { + z: number; +} +declare let Q1: new (x: unknown) => x is string; +declare let Q2: new (x: boolean) => asserts x; +declare let Q3: new (x: unknown) => asserts x is string; +declare class Wat { + get p1(): this is string; + set p1(x: this is string); + get p2(): asserts this is string; + set p2(x: asserts this is string); +} diff --git a/tests/baselines/reference/assertionTypePredicates1.symbols b/tests/baselines/reference/assertionTypePredicates1.symbols new file mode 100644 index 00000000000..c68a1926aff --- /dev/null +++ b/tests/baselines/reference/assertionTypePredicates1.symbols @@ -0,0 +1,359 @@ +=== tests/cases/conformance/controlFlow/assertionTypePredicates1.ts === +declare function isString(value: unknown): value is string; +>isString : Symbol(isString, Decl(assertionTypePredicates1.ts, 0, 0)) +>value : Symbol(value, Decl(assertionTypePredicates1.ts, 0, 26)) +>value : Symbol(value, Decl(assertionTypePredicates1.ts, 0, 26)) + +declare function isArrayOfStrings(value: unknown): value is string[]; +>isArrayOfStrings : Symbol(isArrayOfStrings, Decl(assertionTypePredicates1.ts, 0, 59)) +>value : Symbol(value, Decl(assertionTypePredicates1.ts, 1, 34)) +>value : Symbol(value, Decl(assertionTypePredicates1.ts, 1, 34)) + +const assert: (value: unknown) => asserts value = value => {} +>assert : Symbol(assert, Decl(assertionTypePredicates1.ts, 3, 5)) +>value : Symbol(value, Decl(assertionTypePredicates1.ts, 3, 15)) +>value : Symbol(value, Decl(assertionTypePredicates1.ts, 3, 15)) +>value : Symbol(value, Decl(assertionTypePredicates1.ts, 3, 49)) + +declare function assertIsString(value: unknown): asserts value is string; +>assertIsString : Symbol(assertIsString, Decl(assertionTypePredicates1.ts, 3, 61)) +>value : Symbol(value, Decl(assertionTypePredicates1.ts, 5, 32)) +>value : Symbol(value, Decl(assertionTypePredicates1.ts, 5, 32)) + +declare function assertIsArrayOfStrings(value: unknown): asserts value is string[]; +>assertIsArrayOfStrings : Symbol(assertIsArrayOfStrings, Decl(assertionTypePredicates1.ts, 5, 73)) +>value : Symbol(value, Decl(assertionTypePredicates1.ts, 6, 40)) +>value : Symbol(value, Decl(assertionTypePredicates1.ts, 6, 40)) + +declare function assertDefined(value: T): asserts value is NonNullable; +>assertDefined : Symbol(assertDefined, Decl(assertionTypePredicates1.ts, 6, 83)) +>T : Symbol(T, Decl(assertionTypePredicates1.ts, 7, 31)) +>value : Symbol(value, Decl(assertionTypePredicates1.ts, 7, 34)) +>T : Symbol(T, Decl(assertionTypePredicates1.ts, 7, 31)) +>value : Symbol(value, Decl(assertionTypePredicates1.ts, 7, 34)) +>NonNullable : Symbol(NonNullable, Decl(lib.es5.d.ts, --, --)) +>T : Symbol(T, Decl(assertionTypePredicates1.ts, 7, 31)) + +function f01(x: unknown) { +>f01 : Symbol(f01, Decl(assertionTypePredicates1.ts, 7, 77)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 9, 13)) + + if (!!true) { + assert(typeof x === "string"); +>assert : Symbol(assert, Decl(assertionTypePredicates1.ts, 3, 5)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 9, 13)) + + x.length; +>x.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 9, 13)) +>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) + } + if (!!true) { + assert(x instanceof Error); +>assert : Symbol(assert, Decl(assertionTypePredicates1.ts, 3, 5)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 9, 13)) +>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) + + x.message; +>x.message : Symbol(Error.message, Decl(lib.es5.d.ts, --, --)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 9, 13)) +>message : Symbol(Error.message, Decl(lib.es5.d.ts, --, --)) + } + if (!!true) { + assert(typeof x === "boolean" || typeof x === "number"); +>assert : Symbol(assert, Decl(assertionTypePredicates1.ts, 3, 5)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 9, 13)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 9, 13)) + + x.toLocaleString; +>x.toLocaleString : Symbol(toLocaleString, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 9, 13)) +>toLocaleString : Symbol(toLocaleString, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) + } + if (!!true) { + assert(isArrayOfStrings(x)); +>assert : Symbol(assert, Decl(assertionTypePredicates1.ts, 3, 5)) +>isArrayOfStrings : Symbol(isArrayOfStrings, Decl(assertionTypePredicates1.ts, 0, 59)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 9, 13)) + + x[0].length; +>x[0].length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 9, 13)) +>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) + } + if (!!true) { + assertIsArrayOfStrings(x); +>assertIsArrayOfStrings : Symbol(assertIsArrayOfStrings, Decl(assertionTypePredicates1.ts, 5, 73)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 9, 13)) + + x[0].length; +>x[0].length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 9, 13)) +>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) + } + if (!!true) { + assert(x === undefined || typeof x === "string"); +>assert : Symbol(assert, Decl(assertionTypePredicates1.ts, 3, 5)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 9, 13)) +>undefined : Symbol(undefined) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 9, 13)) + + x; // string | undefined +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 9, 13)) + + assertDefined(x); +>assertDefined : Symbol(assertDefined, Decl(assertionTypePredicates1.ts, 6, 83)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 9, 13)) + + x; // string +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 9, 13)) + } +} + +function f02(x: string | undefined) { +>f02 : Symbol(f02, Decl(assertionTypePredicates1.ts, 36, 1)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 38, 13)) + + if (!!true) { + assert(x); +>assert : Symbol(assert, Decl(assertionTypePredicates1.ts, 3, 5)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 38, 13)) + + x.length; +>x.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 38, 13)) +>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) + } + if (!!true) { + assert(x !== undefined); +>assert : Symbol(assert, Decl(assertionTypePredicates1.ts, 3, 5)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 38, 13)) +>undefined : Symbol(undefined) + + x.length; +>x.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 38, 13)) +>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) + } + if (!!true) { + assertDefined(x); +>assertDefined : Symbol(assertDefined, Decl(assertionTypePredicates1.ts, 6, 83)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 38, 13)) + + x.length; +>x.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 38, 13)) +>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) + } +} + +function f03(x: string | undefined, assert: (value: unknown) => asserts value) { +>f03 : Symbol(f03, Decl(assertionTypePredicates1.ts, 51, 1)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 53, 13)) +>assert : Symbol(assert, Decl(assertionTypePredicates1.ts, 53, 35)) +>value : Symbol(value, Decl(assertionTypePredicates1.ts, 53, 45)) +>value : Symbol(value, Decl(assertionTypePredicates1.ts, 53, 45)) + + assert(x); +>assert : Symbol(assert, Decl(assertionTypePredicates1.ts, 53, 35)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 53, 13)) + + x.length; +>x.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 53, 13)) +>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) +} + +namespace Debug { +>Debug : Symbol(Debug, Decl(assertionTypePredicates1.ts, 56, 1)) + + export declare function assert(value: unknown, message?: string): asserts value; +>assert : Symbol(assert, Decl(assertionTypePredicates1.ts, 58, 17)) +>value : Symbol(value, Decl(assertionTypePredicates1.ts, 59, 35)) +>message : Symbol(message, Decl(assertionTypePredicates1.ts, 59, 50)) +>value : Symbol(value, Decl(assertionTypePredicates1.ts, 59, 35)) + + export declare function assertDefined(value: T): asserts value is NonNullable; +>assertDefined : Symbol(assertDefined, Decl(assertionTypePredicates1.ts, 59, 84)) +>T : Symbol(T, Decl(assertionTypePredicates1.ts, 60, 42)) +>value : Symbol(value, Decl(assertionTypePredicates1.ts, 60, 45)) +>T : Symbol(T, Decl(assertionTypePredicates1.ts, 60, 42)) +>value : Symbol(value, Decl(assertionTypePredicates1.ts, 60, 45)) +>NonNullable : Symbol(NonNullable, Decl(lib.es5.d.ts, --, --)) +>T : Symbol(T, Decl(assertionTypePredicates1.ts, 60, 42)) +} + +function f10(x: string | undefined) { +>f10 : Symbol(f10, Decl(assertionTypePredicates1.ts, 61, 1)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 63, 13)) + + if (!!true) { + Debug.assert(x); +>Debug.assert : Symbol(Debug.assert, Decl(assertionTypePredicates1.ts, 58, 17)) +>Debug : Symbol(Debug, Decl(assertionTypePredicates1.ts, 56, 1)) +>assert : Symbol(Debug.assert, Decl(assertionTypePredicates1.ts, 58, 17)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 63, 13)) + + x.length; +>x.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 63, 13)) +>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) + } + if (!!true) { + Debug.assert(x !== undefined); +>Debug.assert : Symbol(Debug.assert, Decl(assertionTypePredicates1.ts, 58, 17)) +>Debug : Symbol(Debug, Decl(assertionTypePredicates1.ts, 56, 1)) +>assert : Symbol(Debug.assert, Decl(assertionTypePredicates1.ts, 58, 17)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 63, 13)) +>undefined : Symbol(undefined) + + x.length; +>x.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 63, 13)) +>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) + } + if (!!true) { + Debug.assertDefined(x); +>Debug.assertDefined : Symbol(Debug.assertDefined, Decl(assertionTypePredicates1.ts, 59, 84)) +>Debug : Symbol(Debug, Decl(assertionTypePredicates1.ts, 56, 1)) +>assertDefined : Symbol(Debug.assertDefined, Decl(assertionTypePredicates1.ts, 59, 84)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 63, 13)) + + x.length; +>x.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 63, 13)) +>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) + } +} + +class Test { +>Test : Symbol(Test, Decl(assertionTypePredicates1.ts, 76, 1)) + + assert(value: unknown): asserts value { +>assert : Symbol(Test.assert, Decl(assertionTypePredicates1.ts, 78, 12)) +>value : Symbol(value, Decl(assertionTypePredicates1.ts, 79, 11)) +>value : Symbol(value, Decl(assertionTypePredicates1.ts, 79, 11)) + + if (value) return; +>value : Symbol(value, Decl(assertionTypePredicates1.ts, 79, 11)) + + throw new Error(); +>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) + } + isTest2(): this is Test2 { +>isTest2 : Symbol(Test.isTest2, Decl(assertionTypePredicates1.ts, 82, 5)) +>Test2 : Symbol(Test2, Decl(assertionTypePredicates1.ts, 107, 1)) + + return this instanceof Test2; +>this : Symbol(Test, Decl(assertionTypePredicates1.ts, 76, 1)) +>Test2 : Symbol(Test2, Decl(assertionTypePredicates1.ts, 107, 1)) + } + assertIsTest2(): asserts this is Test2 { +>assertIsTest2 : Symbol(Test.assertIsTest2, Decl(assertionTypePredicates1.ts, 85, 5)) +>Test2 : Symbol(Test2, Decl(assertionTypePredicates1.ts, 107, 1)) + + if (this instanceof Test2) return; +>this : Symbol(Test, Decl(assertionTypePredicates1.ts, 76, 1)) +>Test2 : Symbol(Test2, Decl(assertionTypePredicates1.ts, 107, 1)) + + throw new Error(); +>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) + } + assertThis(): asserts this { +>assertThis : Symbol(Test.assertThis, Decl(assertionTypePredicates1.ts, 89, 5)) + + if (!this) return; +>this : Symbol(Test, Decl(assertionTypePredicates1.ts, 76, 1)) + + throw new Error(); +>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) + } + bar() { +>bar : Symbol(Test.bar, Decl(assertionTypePredicates1.ts, 93, 5)) + + this.assertThis(); +>this.assertThis : Symbol(Test.assertThis, Decl(assertionTypePredicates1.ts, 89, 5)) +>this : Symbol(Test, Decl(assertionTypePredicates1.ts, 76, 1)) +>assertThis : Symbol(Test.assertThis, Decl(assertionTypePredicates1.ts, 89, 5)) + + this; +>this : Symbol(Test, Decl(assertionTypePredicates1.ts, 76, 1)) + } + foo(x: unknown) { +>foo : Symbol(Test.foo, Decl(assertionTypePredicates1.ts, 97, 5)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 98, 8)) + + this.assert(typeof x === "string"); +>this.assert : Symbol(Test.assert, Decl(assertionTypePredicates1.ts, 78, 12)) +>this : Symbol(Test, Decl(assertionTypePredicates1.ts, 76, 1)) +>assert : Symbol(Test.assert, Decl(assertionTypePredicates1.ts, 78, 12)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 98, 8)) + + x.length; +>x.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 98, 8)) +>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) + + if (this.isTest2()) { +>this.isTest2 : Symbol(Test.isTest2, Decl(assertionTypePredicates1.ts, 82, 5)) +>this : Symbol(Test, Decl(assertionTypePredicates1.ts, 76, 1)) +>isTest2 : Symbol(Test.isTest2, Decl(assertionTypePredicates1.ts, 82, 5)) + + this.z; +>this.z : Symbol(Test2.z, Decl(assertionTypePredicates1.ts, 109, 26)) +>z : Symbol(Test2.z, Decl(assertionTypePredicates1.ts, 109, 26)) + } + this.assertIsTest2(); +>this.assertIsTest2 : Symbol(Test.assertIsTest2, Decl(assertionTypePredicates1.ts, 85, 5)) +>this : Symbol(Test, Decl(assertionTypePredicates1.ts, 76, 1)) +>assertIsTest2 : Symbol(Test.assertIsTest2, Decl(assertionTypePredicates1.ts, 85, 5)) + + this.z; +>this.z : Symbol(Test2.z, Decl(assertionTypePredicates1.ts, 109, 26)) +>z : Symbol(Test2.z, Decl(assertionTypePredicates1.ts, 109, 26)) + } +} + +class Test2 extends Test { +>Test2 : Symbol(Test2, Decl(assertionTypePredicates1.ts, 107, 1)) +>Test : Symbol(Test, Decl(assertionTypePredicates1.ts, 76, 1)) + + z = 0; +>z : Symbol(Test2.z, Decl(assertionTypePredicates1.ts, 109, 26)) +} + +// Invalid constructs + +declare let Q1: new (x: unknown) => x is string; +>Q1 : Symbol(Q1, Decl(assertionTypePredicates1.ts, 115, 11)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 115, 21)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 115, 21)) + +declare let Q2: new (x: boolean) => asserts x; +>Q2 : Symbol(Q2, Decl(assertionTypePredicates1.ts, 116, 11)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 116, 21)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 116, 21)) + +declare let Q3: new (x: unknown) => asserts x is string; +>Q3 : Symbol(Q3, Decl(assertionTypePredicates1.ts, 117, 11)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 117, 21)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 117, 21)) + +declare class Wat { +>Wat : Symbol(Wat, Decl(assertionTypePredicates1.ts, 117, 56)) + + get p1(): this is string; +>p1 : Symbol(Wat.p1, Decl(assertionTypePredicates1.ts, 119, 19), Decl(assertionTypePredicates1.ts, 120, 29)) + + set p1(x: this is string); +>p1 : Symbol(Wat.p1, Decl(assertionTypePredicates1.ts, 119, 19), Decl(assertionTypePredicates1.ts, 120, 29)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 121, 11)) + + get p2(): asserts this is string; +>p2 : Symbol(Wat.p2, Decl(assertionTypePredicates1.ts, 121, 30), Decl(assertionTypePredicates1.ts, 122, 37)) + + set p2(x: asserts this is string); +>p2 : Symbol(Wat.p2, Decl(assertionTypePredicates1.ts, 121, 30), Decl(assertionTypePredicates1.ts, 122, 37)) +>x : Symbol(x, Decl(assertionTypePredicates1.ts, 123, 11)) +} + diff --git a/tests/baselines/reference/assertionTypePredicates1.types b/tests/baselines/reference/assertionTypePredicates1.types new file mode 100644 index 00000000000..f72ec641013 --- /dev/null +++ b/tests/baselines/reference/assertionTypePredicates1.types @@ -0,0 +1,438 @@ +=== tests/cases/conformance/controlFlow/assertionTypePredicates1.ts === +declare function isString(value: unknown): value is string; +>isString : (value: unknown) => value is string +>value : unknown + +declare function isArrayOfStrings(value: unknown): value is string[]; +>isArrayOfStrings : (value: unknown) => value is string[] +>value : unknown + +const assert: (value: unknown) => asserts value = value => {} +>assert : (value: unknown) => asserts value +>value : unknown +>value => {} : (value: unknown) => void +>value : unknown + +declare function assertIsString(value: unknown): asserts value is string; +>assertIsString : (value: unknown) => asserts value is string +>value : unknown + +declare function assertIsArrayOfStrings(value: unknown): asserts value is string[]; +>assertIsArrayOfStrings : (value: unknown) => asserts value is string[] +>value : unknown + +declare function assertDefined(value: T): asserts value is NonNullable; +>assertDefined : (value: T) => asserts value is NonNullable +>value : T + +function f01(x: unknown) { +>f01 : (x: unknown) => void +>x : unknown + + if (!!true) { +>!!true : true +>!true : false +>true : true + + assert(typeof x === "string"); +>assert(typeof x === "string") : void +>assert : (value: unknown) => asserts value +>typeof x === "string" : boolean +>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" +>x : unknown +>"string" : "string" + + x.length; +>x.length : number +>x : string +>length : number + } + if (!!true) { +>!!true : true +>!true : false +>true : true + + assert(x instanceof Error); +>assert(x instanceof Error) : void +>assert : (value: unknown) => asserts value +>x instanceof Error : boolean +>x : unknown +>Error : ErrorConstructor + + x.message; +>x.message : string +>x : Error +>message : string + } + if (!!true) { +>!!true : true +>!true : false +>true : true + + assert(typeof x === "boolean" || typeof x === "number"); +>assert(typeof x === "boolean" || typeof x === "number") : void +>assert : (value: unknown) => asserts value +>typeof x === "boolean" || typeof x === "number" : boolean +>typeof x === "boolean" : boolean +>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" +>x : unknown +>"boolean" : "boolean" +>typeof x === "number" : boolean +>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" +>x : unknown +>"number" : "number" + + x.toLocaleString; +>x.toLocaleString : ((locales?: string | string[] | undefined, options?: Intl.NumberFormatOptions | undefined) => string) | (() => string) +>x : number | boolean +>toLocaleString : ((locales?: string | string[] | undefined, options?: Intl.NumberFormatOptions | undefined) => string) | (() => string) + } + if (!!true) { +>!!true : true +>!true : false +>true : true + + assert(isArrayOfStrings(x)); +>assert(isArrayOfStrings(x)) : void +>assert : (value: unknown) => asserts value +>isArrayOfStrings(x) : boolean +>isArrayOfStrings : (value: unknown) => value is string[] +>x : unknown + + x[0].length; +>x[0].length : number +>x[0] : string +>x : string[] +>0 : 0 +>length : number + } + if (!!true) { +>!!true : true +>!true : false +>true : true + + assertIsArrayOfStrings(x); +>assertIsArrayOfStrings(x) : void +>assertIsArrayOfStrings : (value: unknown) => asserts value is string[] +>x : unknown + + x[0].length; +>x[0].length : number +>x[0] : string +>x : string[] +>0 : 0 +>length : number + } + if (!!true) { +>!!true : true +>!true : false +>true : true + + assert(x === undefined || typeof x === "string"); +>assert(x === undefined || typeof x === "string") : void +>assert : (value: unknown) => asserts value +>x === undefined || typeof x === "string" : boolean +>x === undefined : boolean +>x : unknown +>undefined : undefined +>typeof x === "string" : boolean +>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" +>x : unknown +>"string" : "string" + + x; // string | undefined +>x : string | undefined + + assertDefined(x); +>assertDefined(x) : void +>assertDefined : (value: T) => asserts value is NonNullable +>x : string | undefined + + x; // string +>x : string + } +} + +function f02(x: string | undefined) { +>f02 : (x: string | undefined) => void +>x : string | undefined + + if (!!true) { +>!!true : true +>!true : false +>true : true + + assert(x); +>assert(x) : void +>assert : (value: unknown) => asserts value +>x : string | undefined + + x.length; +>x.length : number +>x : string +>length : number + } + if (!!true) { +>!!true : true +>!true : false +>true : true + + assert(x !== undefined); +>assert(x !== undefined) : void +>assert : (value: unknown) => asserts value +>x !== undefined : boolean +>x : string | undefined +>undefined : undefined + + x.length; +>x.length : number +>x : string +>length : number + } + if (!!true) { +>!!true : true +>!true : false +>true : true + + assertDefined(x); +>assertDefined(x) : void +>assertDefined : (value: T) => asserts value is NonNullable +>x : string | undefined + + x.length; +>x.length : number +>x : string +>length : number + } +} + +function f03(x: string | undefined, assert: (value: unknown) => asserts value) { +>f03 : (x: string | undefined, assert: (value: unknown) => asserts value) => void +>x : string | undefined +>assert : (value: unknown) => asserts value +>value : unknown + + assert(x); +>assert(x) : void +>assert : (value: unknown) => asserts value +>x : string | undefined + + x.length; +>x.length : number +>x : string +>length : number +} + +namespace Debug { +>Debug : typeof Debug + + export declare function assert(value: unknown, message?: string): asserts value; +>assert : (value: unknown, message?: string | undefined) => asserts value +>value : unknown +>message : string | undefined + + export declare function assertDefined(value: T): asserts value is NonNullable; +>assertDefined : (value: T) => asserts value is NonNullable +>value : T +} + +function f10(x: string | undefined) { +>f10 : (x: string | undefined) => void +>x : string | undefined + + if (!!true) { +>!!true : true +>!true : false +>true : true + + Debug.assert(x); +>Debug.assert(x) : void +>Debug.assert : (value: unknown, message?: string | undefined) => asserts value +>Debug : typeof Debug +>assert : (value: unknown, message?: string | undefined) => asserts value +>x : string | undefined + + x.length; +>x.length : number +>x : string +>length : number + } + if (!!true) { +>!!true : true +>!true : false +>true : true + + Debug.assert(x !== undefined); +>Debug.assert(x !== undefined) : void +>Debug.assert : (value: unknown, message?: string | undefined) => asserts value +>Debug : typeof Debug +>assert : (value: unknown, message?: string | undefined) => asserts value +>x !== undefined : boolean +>x : string | undefined +>undefined : undefined + + x.length; +>x.length : number +>x : string +>length : number + } + if (!!true) { +>!!true : true +>!true : false +>true : true + + Debug.assertDefined(x); +>Debug.assertDefined(x) : void +>Debug.assertDefined : (value: T) => asserts value is NonNullable +>Debug : typeof Debug +>assertDefined : (value: T) => asserts value is NonNullable +>x : string | undefined + + x.length; +>x.length : number +>x : string +>length : number + } +} + +class Test { +>Test : Test + + assert(value: unknown): asserts value { +>assert : (value: unknown) => asserts value +>value : unknown + + if (value) return; +>value : unknown + + throw new Error(); +>new Error() : Error +>Error : ErrorConstructor + } + isTest2(): this is Test2 { +>isTest2 : () => this is Test2 + + return this instanceof Test2; +>this instanceof Test2 : boolean +>this : this +>Test2 : typeof Test2 + } + assertIsTest2(): asserts this is Test2 { +>assertIsTest2 : () => asserts this is Test2 + + if (this instanceof Test2) return; +>this instanceof Test2 : boolean +>this : this +>Test2 : typeof Test2 + + throw new Error(); +>new Error() : Error +>Error : ErrorConstructor + } + assertThis(): asserts this { +>assertThis : () => asserts this + + if (!this) return; +>!this : false +>this : this + + throw new Error(); +>new Error() : Error +>Error : ErrorConstructor + } + bar() { +>bar : () => void + + this.assertThis(); +>this.assertThis() : void +>this.assertThis : () => asserts this +>this : this +>assertThis : () => asserts this + + this; +>this : this + } + foo(x: unknown) { +>foo : (x: unknown) => void +>x : unknown + + this.assert(typeof x === "string"); +>this.assert(typeof x === "string") : void +>this.assert : (value: unknown) => asserts value +>this : this +>assert : (value: unknown) => asserts value +>typeof x === "string" : boolean +>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" +>x : unknown +>"string" : "string" + + x.length; +>x.length : number +>x : string +>length : number + + if (this.isTest2()) { +>this.isTest2() : boolean +>this.isTest2 : () => this is Test2 +>this : this +>isTest2 : () => this is Test2 + + this.z; +>this.z : number +>this : this & Test2 +>z : number + } + this.assertIsTest2(); +>this.assertIsTest2() : void +>this.assertIsTest2 : () => asserts this is Test2 +>this : this +>assertIsTest2 : () => asserts this is Test2 + + this.z; +>this.z : number +>this : this & Test2 +>z : number + } +} + +class Test2 extends Test { +>Test2 : Test2 +>Test : Test + + z = 0; +>z : number +>0 : 0 +} + +// Invalid constructs + +declare let Q1: new (x: unknown) => x is string; +>Q1 : new (x: unknown) => x is string +>x : unknown + +declare let Q2: new (x: boolean) => asserts x; +>Q2 : new (x: boolean) => asserts x +>x : boolean + +declare let Q3: new (x: unknown) => asserts x is string; +>Q3 : new (x: unknown) => asserts x is string +>x : unknown + +declare class Wat { +>Wat : Wat + + get p1(): this is string; +>p1 : boolean + + set p1(x: this is string); +>p1 : boolean +>x : boolean + + get p2(): asserts this is string; +>p2 : void + + set p2(x: asserts this is string); +>p2 : void +>x : void +} + diff --git a/tests/baselines/reference/assertionsAndNonReturningFunctions.errors.txt b/tests/baselines/reference/assertionsAndNonReturningFunctions.errors.txt new file mode 100644 index 00000000000..e1648d7643d --- /dev/null +++ b/tests/baselines/reference/assertionsAndNonReturningFunctions.errors.txt @@ -0,0 +1,69 @@ +tests/cases/conformance/jsdoc/assertionsAndNonReturningFunctions.js(46,9): error TS7027: Unreachable code detected. +tests/cases/conformance/jsdoc/assertionsAndNonReturningFunctions.js(58,5): error TS7027: Unreachable code detected. + + +==== tests/cases/conformance/jsdoc/assertionsAndNonReturningFunctions.js (2 errors) ==== + /** @typedef {(check: boolean) => asserts check} AssertFunc */ + + /** @type {AssertFunc} */ + const assert = check => { + if (!check) throw new Error(); + } + + /** @type {(x: unknown) => asserts x is string } */ + function assertIsString(x) { + if (!(typeof x === "string")) throw new Error(); + } + + /** + * @param {boolean} check + * @returns {asserts check} + */ + function assert2(check) { + if (!check) throw new Error(); + } + + /** + * @returns {never} + */ + function fail() { + throw new Error(); + } + + /** + * @param {*} x + */ + function f1(x) { + if (!!true) { + assert(typeof x === "string"); + x.length; + } + if (!!true) { + assert2(typeof x === "string"); + x.length; + } + if (!!true) { + assertIsString(x); + x.length; + } + if (!!true) { + fail(); + x; // Unreachable + ~~ +!!! error TS7027: Unreachable code detected. + } + } + + /** + * @param {boolean} b + */ + function f2(b) { + switch (b) { + case true: return 1; + case false: return 0; + } + b; // Unreachable + ~~ +!!! error TS7027: Unreachable code detected. + } + \ No newline at end of file diff --git a/tests/baselines/reference/assertionsAndNonReturningFunctions.symbols b/tests/baselines/reference/assertionsAndNonReturningFunctions.symbols new file mode 100644 index 00000000000..88931cffbd8 --- /dev/null +++ b/tests/baselines/reference/assertionsAndNonReturningFunctions.symbols @@ -0,0 +1,109 @@ +=== tests/cases/conformance/jsdoc/assertionsAndNonReturningFunctions.js === +/** @typedef {(check: boolean) => asserts check} AssertFunc */ + +/** @type {AssertFunc} */ +const assert = check => { +>assert : Symbol(assert, Decl(assertionsAndNonReturningFunctions.js, 3, 5)) +>check : Symbol(check, Decl(assertionsAndNonReturningFunctions.js, 3, 14)) + + if (!check) throw new Error(); +>check : Symbol(check, Decl(assertionsAndNonReturningFunctions.js, 3, 14)) +>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +} + +/** @type {(x: unknown) => asserts x is string } */ +function assertIsString(x) { +>assertIsString : Symbol(assertIsString, Decl(assertionsAndNonReturningFunctions.js, 5, 1)) +>x : Symbol(x, Decl(assertionsAndNonReturningFunctions.js, 8, 24)) + + if (!(typeof x === "string")) throw new Error(); +>x : Symbol(x, Decl(assertionsAndNonReturningFunctions.js, 8, 24)) +>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +} + +/** + * @param {boolean} check + * @returns {asserts check} +*/ +function assert2(check) { +>assert2 : Symbol(assert2, Decl(assertionsAndNonReturningFunctions.js, 10, 1)) +>check : Symbol(check, Decl(assertionsAndNonReturningFunctions.js, 16, 17)) + + if (!check) throw new Error(); +>check : Symbol(check, Decl(assertionsAndNonReturningFunctions.js, 16, 17)) +>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +} + +/** + * @returns {never} + */ +function fail() { +>fail : Symbol(fail, Decl(assertionsAndNonReturningFunctions.js, 18, 1)) + + throw new Error(); +>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +} + +/** + * @param {*} x + */ +function f1(x) { +>f1 : Symbol(f1, Decl(assertionsAndNonReturningFunctions.js, 25, 1)) +>x : Symbol(x, Decl(assertionsAndNonReturningFunctions.js, 30, 12)) + + if (!!true) { + assert(typeof x === "string"); +>assert : Symbol(assert, Decl(assertionsAndNonReturningFunctions.js, 3, 5)) +>x : Symbol(x, Decl(assertionsAndNonReturningFunctions.js, 30, 12)) + + x.length; +>x.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) +>x : Symbol(x, Decl(assertionsAndNonReturningFunctions.js, 30, 12)) +>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) + } + if (!!true) { + assert2(typeof x === "string"); +>assert2 : Symbol(assert2, Decl(assertionsAndNonReturningFunctions.js, 10, 1)) +>x : Symbol(x, Decl(assertionsAndNonReturningFunctions.js, 30, 12)) + + x.length; +>x.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) +>x : Symbol(x, Decl(assertionsAndNonReturningFunctions.js, 30, 12)) +>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) + } + if (!!true) { + assertIsString(x); +>assertIsString : Symbol(assertIsString, Decl(assertionsAndNonReturningFunctions.js, 5, 1)) +>x : Symbol(x, Decl(assertionsAndNonReturningFunctions.js, 30, 12)) + + x.length; +>x.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) +>x : Symbol(x, Decl(assertionsAndNonReturningFunctions.js, 30, 12)) +>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) + } + if (!!true) { + fail(); +>fail : Symbol(fail, Decl(assertionsAndNonReturningFunctions.js, 18, 1)) + + x; // Unreachable +>x : Symbol(x, Decl(assertionsAndNonReturningFunctions.js, 30, 12)) + } +} + +/** + * @param {boolean} b + */ +function f2(b) { +>f2 : Symbol(f2, Decl(assertionsAndNonReturningFunctions.js, 47, 1)) +>b : Symbol(b, Decl(assertionsAndNonReturningFunctions.js, 52, 12)) + + switch (b) { +>b : Symbol(b, Decl(assertionsAndNonReturningFunctions.js, 52, 12)) + + case true: return 1; + case false: return 0; + } + b; // Unreachable +>b : Symbol(b, Decl(assertionsAndNonReturningFunctions.js, 52, 12)) +} + diff --git a/tests/baselines/reference/assertionsAndNonReturningFunctions.types b/tests/baselines/reference/assertionsAndNonReturningFunctions.types new file mode 100644 index 00000000000..32100b6c3d2 --- /dev/null +++ b/tests/baselines/reference/assertionsAndNonReturningFunctions.types @@ -0,0 +1,152 @@ +=== tests/cases/conformance/jsdoc/assertionsAndNonReturningFunctions.js === +/** @typedef {(check: boolean) => asserts check} AssertFunc */ + +/** @type {AssertFunc} */ +const assert = check => { +>assert : (check: boolean) => asserts check +>check => { if (!check) throw new Error();} : (check: boolean) => asserts check +>check : boolean + + if (!check) throw new Error(); +>!check : boolean +>check : boolean +>new Error() : Error +>Error : ErrorConstructor +} + +/** @type {(x: unknown) => asserts x is string } */ +function assertIsString(x) { +>assertIsString : (x: unknown) => asserts x is string +>x : unknown + + if (!(typeof x === "string")) throw new Error(); +>!(typeof x === "string") : boolean +>(typeof x === "string") : boolean +>typeof x === "string" : boolean +>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" +>x : unknown +>"string" : "string" +>new Error() : Error +>Error : ErrorConstructor +} + +/** + * @param {boolean} check + * @returns {asserts check} +*/ +function assert2(check) { +>assert2 : (check: boolean) => asserts check +>check : boolean + + if (!check) throw new Error(); +>!check : boolean +>check : boolean +>new Error() : Error +>Error : ErrorConstructor +} + +/** + * @returns {never} + */ +function fail() { +>fail : () => never + + throw new Error(); +>new Error() : Error +>Error : ErrorConstructor +} + +/** + * @param {*} x + */ +function f1(x) { +>f1 : (x: any) => void +>x : any + + if (!!true) { +>!!true : boolean +>!true : boolean +>true : true + + assert(typeof x === "string"); +>assert(typeof x === "string") : void +>assert : (check: boolean) => asserts check +>typeof x === "string" : boolean +>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" +>x : any +>"string" : "string" + + x.length; +>x.length : number +>x : string +>length : number + } + if (!!true) { +>!!true : boolean +>!true : boolean +>true : true + + assert2(typeof x === "string"); +>assert2(typeof x === "string") : void +>assert2 : (check: boolean) => asserts check +>typeof x === "string" : boolean +>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" +>x : any +>"string" : "string" + + x.length; +>x.length : number +>x : string +>length : number + } + if (!!true) { +>!!true : boolean +>!true : boolean +>true : true + + assertIsString(x); +>assertIsString(x) : void +>assertIsString : (x: unknown) => asserts x is string +>x : any + + x.length; +>x.length : number +>x : string +>length : number + } + if (!!true) { +>!!true : boolean +>!true : boolean +>true : true + + fail(); +>fail() : never +>fail : () => never + + x; // Unreachable +>x : any + } +} + +/** + * @param {boolean} b + */ +function f2(b) { +>f2 : (b: boolean) => 1 | 0 +>b : boolean + + switch (b) { +>b : boolean + + case true: return 1; +>true : true +>1 : 1 + + case false: return 0; +>false : false +>0 : 0 + } + b; // Unreachable +>b : never +} + diff --git a/tests/baselines/reference/exhaustiveSwitchImplicitReturn.errors.txt b/tests/baselines/reference/exhaustiveSwitchImplicitReturn.errors.txt index 43b5467f0d2..89daa42c268 100644 --- a/tests/baselines/reference/exhaustiveSwitchImplicitReturn.errors.txt +++ b/tests/baselines/reference/exhaustiveSwitchImplicitReturn.errors.txt @@ -44,4 +44,18 @@ tests/cases/compiler/exhaustiveSwitchImplicitReturn.ts(35,32): error TS7030: Not return 1; } } + + function foo6(bar: "a", a: boolean, b: boolean): number { + if (a) { + switch (bar) { + case "a": return 1; + } + } + else { + switch (b) { + case true: return -1; + case false: return 0; + } + } + } \ No newline at end of file diff --git a/tests/baselines/reference/exhaustiveSwitchImplicitReturn.js b/tests/baselines/reference/exhaustiveSwitchImplicitReturn.js index 5f8bf348c5f..9877f251993 100644 --- a/tests/baselines/reference/exhaustiveSwitchImplicitReturn.js +++ b/tests/baselines/reference/exhaustiveSwitchImplicitReturn.js @@ -39,6 +39,20 @@ function foo5(bar: "a" | "b"): number { return 1; } } + +function foo6(bar: "a", a: boolean, b: boolean): number { + if (a) { + switch (bar) { + case "a": return 1; + } + } + else { + switch (b) { + case true: return -1; + case false: return 0; + } + } +} //// [exhaustiveSwitchImplicitReturn.js] @@ -75,3 +89,16 @@ function foo5(bar) { return 1; } } +function foo6(bar, a, b) { + if (a) { + switch (bar) { + case "a": return 1; + } + } + else { + switch (b) { + case true: return -1; + case false: return 0; + } + } +} diff --git a/tests/baselines/reference/exhaustiveSwitchImplicitReturn.symbols b/tests/baselines/reference/exhaustiveSwitchImplicitReturn.symbols index d93a5228362..dcad190a9b3 100644 --- a/tests/baselines/reference/exhaustiveSwitchImplicitReturn.symbols +++ b/tests/baselines/reference/exhaustiveSwitchImplicitReturn.symbols @@ -69,3 +69,28 @@ function foo5(bar: "a" | "b"): number { } } +function foo6(bar: "a", a: boolean, b: boolean): number { +>foo6 : Symbol(foo6, Decl(exhaustiveSwitchImplicitReturn.ts, 39, 1)) +>bar : Symbol(bar, Decl(exhaustiveSwitchImplicitReturn.ts, 41, 14)) +>a : Symbol(a, Decl(exhaustiveSwitchImplicitReturn.ts, 41, 23)) +>b : Symbol(b, Decl(exhaustiveSwitchImplicitReturn.ts, 41, 35)) + + if (a) { +>a : Symbol(a, Decl(exhaustiveSwitchImplicitReturn.ts, 41, 23)) + + switch (bar) { +>bar : Symbol(bar, Decl(exhaustiveSwitchImplicitReturn.ts, 41, 14)) + + case "a": return 1; + } + } + else { + switch (b) { +>b : Symbol(b, Decl(exhaustiveSwitchImplicitReturn.ts, 41, 35)) + + case true: return -1; + case false: return 0; + } + } +} + diff --git a/tests/baselines/reference/exhaustiveSwitchImplicitReturn.types b/tests/baselines/reference/exhaustiveSwitchImplicitReturn.types index c868aa99b8f..c72b3b24477 100644 --- a/tests/baselines/reference/exhaustiveSwitchImplicitReturn.types +++ b/tests/baselines/reference/exhaustiveSwitchImplicitReturn.types @@ -85,3 +85,36 @@ function foo5(bar: "a" | "b"): number { } } +function foo6(bar: "a", a: boolean, b: boolean): number { +>foo6 : (bar: "a", a: boolean, b: boolean) => number +>bar : "a" +>a : boolean +>b : boolean + + if (a) { +>a : boolean + + switch (bar) { +>bar : "a" + + case "a": return 1; +>"a" : "a" +>1 : 1 + } + } + else { + switch (b) { +>b : boolean + + case true: return -1; +>true : true +>-1 : -1 +>1 : 1 + + case false: return 0; +>false : false +>0 : 0 + } + } +} + diff --git a/tests/baselines/reference/exhaustiveSwitchStatements1.errors.txt b/tests/baselines/reference/exhaustiveSwitchStatements1.errors.txt new file mode 100644 index 00000000000..4a54518413d --- /dev/null +++ b/tests/baselines/reference/exhaustiveSwitchStatements1.errors.txt @@ -0,0 +1,202 @@ +tests/cases/conformance/controlFlow/exhaustiveSwitchStatements1.ts(7,9): error TS7027: Unreachable code detected. + + +==== tests/cases/conformance/controlFlow/exhaustiveSwitchStatements1.ts (1 errors) ==== + function f1(x: 1 | 2): string { + if (!!true) { + switch (x) { + case 1: return 'a'; + case 2: return 'b'; + } + x; // Unreachable + ~~ +!!! error TS7027: Unreachable code detected. + } + else { + throw 0; + } + } + + function f2(x: 1 | 2) { + let z: number; + switch (x) { + case 1: z = 10; break; + case 2: z = 20; break; + } + z; // Definitely assigned + } + + function f3(x: 1 | 2) { + switch (x) { + case 1: return 10; + case 2: return 20; + // Default considered reachable to allow defensive coding + default: throw new Error("Bad input"); + } + } + + // Repro from #11572 + + enum E { A, B } + + function f(e: E): number { + switch (e) { + case E.A: return 0 + case E.B: return 1 + } + } + + function g(e: E): number { + if (!true) + return -1 + else + switch (e) { + case E.A: return 0 + case E.B: return 1 + } + } + + // Repro from #12668 + + interface Square { kind: "square"; size: number; } + + interface Rectangle { kind: "rectangle"; width: number; height: number; } + + interface Circle { kind: "circle"; radius: number; } + + interface Triangle { kind: "triangle"; side: number; } + + type Shape = Square | Rectangle | Circle | Triangle; + + function area(s: Shape): number { + let area; + switch (s.kind) { + case "square": area = s.size * s.size; break; + case "rectangle": area = s.width * s.height; break; + case "circle": area = Math.PI * s.radius * s.radius; break; + case "triangle": area = Math.sqrt(3) / 4 * s.side * s.side; break; + } + return area; + } + + function areaWrapped(s: Shape): number { + let area; + area = (() => { + switch (s.kind) { + case "square": return s.size * s.size; + case "rectangle": return s.width * s.height; + case "circle": return Math.PI * s.radius * s.radius; + case "triangle": return Math.sqrt(3) / 4 * s.side * s.side; + } + })(); + return area; + } + + // Repro from #13241 + + enum MyEnum { + A, + B + } + + function thisGivesError(e: MyEnum): string { + let s: string; + switch (e) { + case MyEnum.A: s = "it was A"; break; + case MyEnum.B: s = "it was B"; break; + } + return s; + } + + function good1(e: MyEnum): string { + let s: string; + switch (e) { + case MyEnum.A: s = "it was A"; break; + case MyEnum.B: s = "it was B"; break; + default: s = "it was something else"; break; + } + return s; + } + + function good2(e: MyEnum): string { + switch (e) { + case MyEnum.A: return "it was A"; + case MyEnum.B: return "it was B"; + } + } + + // Repro from #18362 + + enum Level { + One, + Two, + } + + const doSomethingWithLevel = (level: Level) => { + let next: Level; + switch (level) { + case Level.One: + next = Level.Two; + break; + case Level.Two: + next = Level.One; + break; + } + return next; + }; + + // Repro from #20409 + + interface Square2 { + kind: "square"; + size: number; + } + + interface Circle2 { + kind: "circle"; + radius: number; + } + + type Shape2 = Square2 | Circle2; + + function withDefault(s1: Shape2, s2: Shape2): string { + switch (s1.kind) { + case "square": + return "1"; + case "circle": + switch (s2.kind) { + case "square": + return "2"; + case "circle": + return "3"; + default: + return "never"; + } + } + } + + function withoutDefault(s1: Shape2, s2: Shape2): string { + switch (s1.kind) { + case "square": + return "1"; + case "circle": + switch (s2.kind) { + case "square": + return "2"; + case "circle": + return "3"; + } + } + } + + // Repro from #20823 + + function test4(value: 1 | 2) { + let x: string; + switch (value) { + case 1: x = "one"; break; + case 2: x = "two"; break; + } + return x; + } + \ No newline at end of file diff --git a/tests/baselines/reference/exhaustiveSwitchStatements1.js b/tests/baselines/reference/exhaustiveSwitchStatements1.js new file mode 100644 index 00000000000..a39db3b8101 --- /dev/null +++ b/tests/baselines/reference/exhaustiveSwitchStatements1.js @@ -0,0 +1,437 @@ +//// [exhaustiveSwitchStatements1.ts] +function f1(x: 1 | 2): string { + if (!!true) { + switch (x) { + case 1: return 'a'; + case 2: return 'b'; + } + x; // Unreachable + } + else { + throw 0; + } +} + +function f2(x: 1 | 2) { + let z: number; + switch (x) { + case 1: z = 10; break; + case 2: z = 20; break; + } + z; // Definitely assigned +} + +function f3(x: 1 | 2) { + switch (x) { + case 1: return 10; + case 2: return 20; + // Default considered reachable to allow defensive coding + default: throw new Error("Bad input"); + } +} + +// Repro from #11572 + +enum E { A, B } + +function f(e: E): number { + switch (e) { + case E.A: return 0 + case E.B: return 1 + } +} + +function g(e: E): number { + if (!true) + return -1 + else + switch (e) { + case E.A: return 0 + case E.B: return 1 + } +} + +// Repro from #12668 + +interface Square { kind: "square"; size: number; } + +interface Rectangle { kind: "rectangle"; width: number; height: number; } + +interface Circle { kind: "circle"; radius: number; } + +interface Triangle { kind: "triangle"; side: number; } + +type Shape = Square | Rectangle | Circle | Triangle; + +function area(s: Shape): number { + let area; + switch (s.kind) { + case "square": area = s.size * s.size; break; + case "rectangle": area = s.width * s.height; break; + case "circle": area = Math.PI * s.radius * s.radius; break; + case "triangle": area = Math.sqrt(3) / 4 * s.side * s.side; break; + } + return area; +} + +function areaWrapped(s: Shape): number { + let area; + area = (() => { + switch (s.kind) { + case "square": return s.size * s.size; + case "rectangle": return s.width * s.height; + case "circle": return Math.PI * s.radius * s.radius; + case "triangle": return Math.sqrt(3) / 4 * s.side * s.side; + } + })(); + return area; +} + +// Repro from #13241 + +enum MyEnum { + A, + B +} + +function thisGivesError(e: MyEnum): string { + let s: string; + switch (e) { + case MyEnum.A: s = "it was A"; break; + case MyEnum.B: s = "it was B"; break; + } + return s; +} + +function good1(e: MyEnum): string { + let s: string; + switch (e) { + case MyEnum.A: s = "it was A"; break; + case MyEnum.B: s = "it was B"; break; + default: s = "it was something else"; break; + } + return s; +} + +function good2(e: MyEnum): string { + switch (e) { + case MyEnum.A: return "it was A"; + case MyEnum.B: return "it was B"; + } +} + +// Repro from #18362 + +enum Level { + One, + Two, +} + +const doSomethingWithLevel = (level: Level) => { + let next: Level; + switch (level) { + case Level.One: + next = Level.Two; + break; + case Level.Two: + next = Level.One; + break; + } + return next; +}; + +// Repro from #20409 + +interface Square2 { + kind: "square"; + size: number; +} + +interface Circle2 { + kind: "circle"; + radius: number; +} + +type Shape2 = Square2 | Circle2; + +function withDefault(s1: Shape2, s2: Shape2): string { + switch (s1.kind) { + case "square": + return "1"; + case "circle": + switch (s2.kind) { + case "square": + return "2"; + case "circle": + return "3"; + default: + return "never"; + } + } +} + +function withoutDefault(s1: Shape2, s2: Shape2): string { + switch (s1.kind) { + case "square": + return "1"; + case "circle": + switch (s2.kind) { + case "square": + return "2"; + case "circle": + return "3"; + } + } +} + +// Repro from #20823 + +function test4(value: 1 | 2) { + let x: string; + switch (value) { + case 1: x = "one"; break; + case 2: x = "two"; break; + } + return x; +} + + +//// [exhaustiveSwitchStatements1.js] +"use strict"; +function f1(x) { + if (!!true) { + switch (x) { + case 1: return 'a'; + case 2: return 'b'; + } + x; // Unreachable + } + else { + throw 0; + } +} +function f2(x) { + var z; + switch (x) { + case 1: + z = 10; + break; + case 2: + z = 20; + break; + } + z; // Definitely assigned +} +function f3(x) { + switch (x) { + case 1: return 10; + case 2: return 20; + // Default considered reachable to allow defensive coding + default: throw new Error("Bad input"); + } +} +// Repro from #11572 +var E; +(function (E) { + E[E["A"] = 0] = "A"; + E[E["B"] = 1] = "B"; +})(E || (E = {})); +function f(e) { + switch (e) { + case E.A: return 0; + case E.B: return 1; + } +} +function g(e) { + if (!true) + return -1; + else + switch (e) { + case E.A: return 0; + case E.B: return 1; + } +} +function area(s) { + var area; + switch (s.kind) { + case "square": + area = s.size * s.size; + break; + case "rectangle": + area = s.width * s.height; + break; + case "circle": + area = Math.PI * s.radius * s.radius; + break; + case "triangle": + area = Math.sqrt(3) / 4 * s.side * s.side; + break; + } + return area; +} +function areaWrapped(s) { + var area; + area = (function () { + switch (s.kind) { + case "square": return s.size * s.size; + case "rectangle": return s.width * s.height; + case "circle": return Math.PI * s.radius * s.radius; + case "triangle": return Math.sqrt(3) / 4 * s.side * s.side; + } + })(); + return area; +} +// Repro from #13241 +var MyEnum; +(function (MyEnum) { + MyEnum[MyEnum["A"] = 0] = "A"; + MyEnum[MyEnum["B"] = 1] = "B"; +})(MyEnum || (MyEnum = {})); +function thisGivesError(e) { + var s; + switch (e) { + case MyEnum.A: + s = "it was A"; + break; + case MyEnum.B: + s = "it was B"; + break; + } + return s; +} +function good1(e) { + var s; + switch (e) { + case MyEnum.A: + s = "it was A"; + break; + case MyEnum.B: + s = "it was B"; + break; + default: + s = "it was something else"; + break; + } + return s; +} +function good2(e) { + switch (e) { + case MyEnum.A: return "it was A"; + case MyEnum.B: return "it was B"; + } +} +// Repro from #18362 +var Level; +(function (Level) { + Level[Level["One"] = 0] = "One"; + Level[Level["Two"] = 1] = "Two"; +})(Level || (Level = {})); +var doSomethingWithLevel = function (level) { + var next; + switch (level) { + case Level.One: + next = Level.Two; + break; + case Level.Two: + next = Level.One; + break; + } + return next; +}; +function withDefault(s1, s2) { + switch (s1.kind) { + case "square": + return "1"; + case "circle": + switch (s2.kind) { + case "square": + return "2"; + case "circle": + return "3"; + default: + return "never"; + } + } +} +function withoutDefault(s1, s2) { + switch (s1.kind) { + case "square": + return "1"; + case "circle": + switch (s2.kind) { + case "square": + return "2"; + case "circle": + return "3"; + } + } +} +// Repro from #20823 +function test4(value) { + var x; + switch (value) { + case 1: + x = "one"; + break; + case 2: + x = "two"; + break; + } + return x; +} + + +//// [exhaustiveSwitchStatements1.d.ts] +declare function f1(x: 1 | 2): string; +declare function f2(x: 1 | 2): void; +declare function f3(x: 1 | 2): 10 | 20; +declare enum E { + A = 0, + B = 1 +} +declare function f(e: E): number; +declare function g(e: E): number; +interface Square { + kind: "square"; + size: number; +} +interface Rectangle { + kind: "rectangle"; + width: number; + height: number; +} +interface Circle { + kind: "circle"; + radius: number; +} +interface Triangle { + kind: "triangle"; + side: number; +} +declare type Shape = Square | Rectangle | Circle | Triangle; +declare function area(s: Shape): number; +declare function areaWrapped(s: Shape): number; +declare enum MyEnum { + A = 0, + B = 1 +} +declare function thisGivesError(e: MyEnum): string; +declare function good1(e: MyEnum): string; +declare function good2(e: MyEnum): string; +declare enum Level { + One = 0, + Two = 1 +} +declare const doSomethingWithLevel: (level: Level) => Level; +interface Square2 { + kind: "square"; + size: number; +} +interface Circle2 { + kind: "circle"; + radius: number; +} +declare type Shape2 = Square2 | Circle2; +declare function withDefault(s1: Shape2, s2: Shape2): string; +declare function withoutDefault(s1: Shape2, s2: Shape2): string; +declare function test4(value: 1 | 2): string; diff --git a/tests/baselines/reference/exhaustiveSwitchStatements1.symbols b/tests/baselines/reference/exhaustiveSwitchStatements1.symbols new file mode 100644 index 00000000000..8beb3911883 --- /dev/null +++ b/tests/baselines/reference/exhaustiveSwitchStatements1.symbols @@ -0,0 +1,503 @@ +=== tests/cases/conformance/controlFlow/exhaustiveSwitchStatements1.ts === +function f1(x: 1 | 2): string { +>f1 : Symbol(f1, Decl(exhaustiveSwitchStatements1.ts, 0, 0)) +>x : Symbol(x, Decl(exhaustiveSwitchStatements1.ts, 0, 12)) + + if (!!true) { + switch (x) { +>x : Symbol(x, Decl(exhaustiveSwitchStatements1.ts, 0, 12)) + + case 1: return 'a'; + case 2: return 'b'; + } + x; // Unreachable +>x : Symbol(x, Decl(exhaustiveSwitchStatements1.ts, 0, 12)) + } + else { + throw 0; + } +} + +function f2(x: 1 | 2) { +>f2 : Symbol(f2, Decl(exhaustiveSwitchStatements1.ts, 11, 1)) +>x : Symbol(x, Decl(exhaustiveSwitchStatements1.ts, 13, 12)) + + let z: number; +>z : Symbol(z, Decl(exhaustiveSwitchStatements1.ts, 14, 7)) + + switch (x) { +>x : Symbol(x, Decl(exhaustiveSwitchStatements1.ts, 13, 12)) + + case 1: z = 10; break; +>z : Symbol(z, Decl(exhaustiveSwitchStatements1.ts, 14, 7)) + + case 2: z = 20; break; +>z : Symbol(z, Decl(exhaustiveSwitchStatements1.ts, 14, 7)) + } + z; // Definitely assigned +>z : Symbol(z, Decl(exhaustiveSwitchStatements1.ts, 14, 7)) +} + +function f3(x: 1 | 2) { +>f3 : Symbol(f3, Decl(exhaustiveSwitchStatements1.ts, 20, 1)) +>x : Symbol(x, Decl(exhaustiveSwitchStatements1.ts, 22, 12)) + + switch (x) { +>x : Symbol(x, Decl(exhaustiveSwitchStatements1.ts, 22, 12)) + + case 1: return 10; + case 2: return 20; + // Default considered reachable to allow defensive coding + default: throw new Error("Bad input"); +>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) + } +} + +// Repro from #11572 + +enum E { A, B } +>E : Symbol(E, Decl(exhaustiveSwitchStatements1.ts, 29, 1)) +>A : Symbol(E.A, Decl(exhaustiveSwitchStatements1.ts, 33, 8)) +>B : Symbol(E.B, Decl(exhaustiveSwitchStatements1.ts, 33, 11)) + +function f(e: E): number { +>f : Symbol(f, Decl(exhaustiveSwitchStatements1.ts, 33, 15)) +>e : Symbol(e, Decl(exhaustiveSwitchStatements1.ts, 35, 11)) +>E : Symbol(E, Decl(exhaustiveSwitchStatements1.ts, 29, 1)) + + switch (e) { +>e : Symbol(e, Decl(exhaustiveSwitchStatements1.ts, 35, 11)) + + case E.A: return 0 +>E.A : Symbol(E.A, Decl(exhaustiveSwitchStatements1.ts, 33, 8)) +>E : Symbol(E, Decl(exhaustiveSwitchStatements1.ts, 29, 1)) +>A : Symbol(E.A, Decl(exhaustiveSwitchStatements1.ts, 33, 8)) + + case E.B: return 1 +>E.B : Symbol(E.B, Decl(exhaustiveSwitchStatements1.ts, 33, 11)) +>E : Symbol(E, Decl(exhaustiveSwitchStatements1.ts, 29, 1)) +>B : Symbol(E.B, Decl(exhaustiveSwitchStatements1.ts, 33, 11)) + } +} + +function g(e: E): number { +>g : Symbol(g, Decl(exhaustiveSwitchStatements1.ts, 40, 1)) +>e : Symbol(e, Decl(exhaustiveSwitchStatements1.ts, 42, 11)) +>E : Symbol(E, Decl(exhaustiveSwitchStatements1.ts, 29, 1)) + + if (!true) + return -1 + else + switch (e) { +>e : Symbol(e, Decl(exhaustiveSwitchStatements1.ts, 42, 11)) + + case E.A: return 0 +>E.A : Symbol(E.A, Decl(exhaustiveSwitchStatements1.ts, 33, 8)) +>E : Symbol(E, Decl(exhaustiveSwitchStatements1.ts, 29, 1)) +>A : Symbol(E.A, Decl(exhaustiveSwitchStatements1.ts, 33, 8)) + + case E.B: return 1 +>E.B : Symbol(E.B, Decl(exhaustiveSwitchStatements1.ts, 33, 11)) +>E : Symbol(E, Decl(exhaustiveSwitchStatements1.ts, 29, 1)) +>B : Symbol(E.B, Decl(exhaustiveSwitchStatements1.ts, 33, 11)) + } +} + +// Repro from #12668 + +interface Square { kind: "square"; size: number; } +>Square : Symbol(Square, Decl(exhaustiveSwitchStatements1.ts, 50, 1)) +>kind : Symbol(Square.kind, Decl(exhaustiveSwitchStatements1.ts, 54, 18)) +>size : Symbol(Square.size, Decl(exhaustiveSwitchStatements1.ts, 54, 34)) + +interface Rectangle { kind: "rectangle"; width: number; height: number; } +>Rectangle : Symbol(Rectangle, Decl(exhaustiveSwitchStatements1.ts, 54, 50)) +>kind : Symbol(Rectangle.kind, Decl(exhaustiveSwitchStatements1.ts, 56, 21)) +>width : Symbol(Rectangle.width, Decl(exhaustiveSwitchStatements1.ts, 56, 40)) +>height : Symbol(Rectangle.height, Decl(exhaustiveSwitchStatements1.ts, 56, 55)) + +interface Circle { kind: "circle"; radius: number; } +>Circle : Symbol(Circle, Decl(exhaustiveSwitchStatements1.ts, 56, 73)) +>kind : Symbol(Circle.kind, Decl(exhaustiveSwitchStatements1.ts, 58, 18)) +>radius : Symbol(Circle.radius, Decl(exhaustiveSwitchStatements1.ts, 58, 34)) + +interface Triangle { kind: "triangle"; side: number; } +>Triangle : Symbol(Triangle, Decl(exhaustiveSwitchStatements1.ts, 58, 52)) +>kind : Symbol(Triangle.kind, Decl(exhaustiveSwitchStatements1.ts, 60, 20)) +>side : Symbol(Triangle.side, Decl(exhaustiveSwitchStatements1.ts, 60, 38)) + +type Shape = Square | Rectangle | Circle | Triangle; +>Shape : Symbol(Shape, Decl(exhaustiveSwitchStatements1.ts, 60, 54)) +>Square : Symbol(Square, Decl(exhaustiveSwitchStatements1.ts, 50, 1)) +>Rectangle : Symbol(Rectangle, Decl(exhaustiveSwitchStatements1.ts, 54, 50)) +>Circle : Symbol(Circle, Decl(exhaustiveSwitchStatements1.ts, 56, 73)) +>Triangle : Symbol(Triangle, Decl(exhaustiveSwitchStatements1.ts, 58, 52)) + +function area(s: Shape): number { +>area : Symbol(area, Decl(exhaustiveSwitchStatements1.ts, 62, 52)) +>s : Symbol(s, Decl(exhaustiveSwitchStatements1.ts, 64, 14)) +>Shape : Symbol(Shape, Decl(exhaustiveSwitchStatements1.ts, 60, 54)) + + let area; +>area : Symbol(area, Decl(exhaustiveSwitchStatements1.ts, 65, 7)) + + switch (s.kind) { +>s.kind : Symbol(kind, Decl(exhaustiveSwitchStatements1.ts, 54, 18), Decl(exhaustiveSwitchStatements1.ts, 56, 21), Decl(exhaustiveSwitchStatements1.ts, 58, 18), Decl(exhaustiveSwitchStatements1.ts, 60, 20)) +>s : Symbol(s, Decl(exhaustiveSwitchStatements1.ts, 64, 14)) +>kind : Symbol(kind, Decl(exhaustiveSwitchStatements1.ts, 54, 18), Decl(exhaustiveSwitchStatements1.ts, 56, 21), Decl(exhaustiveSwitchStatements1.ts, 58, 18), Decl(exhaustiveSwitchStatements1.ts, 60, 20)) + + case "square": area = s.size * s.size; break; +>area : Symbol(area, Decl(exhaustiveSwitchStatements1.ts, 65, 7)) +>s.size : Symbol(Square.size, Decl(exhaustiveSwitchStatements1.ts, 54, 34)) +>s : Symbol(s, Decl(exhaustiveSwitchStatements1.ts, 64, 14)) +>size : Symbol(Square.size, Decl(exhaustiveSwitchStatements1.ts, 54, 34)) +>s.size : Symbol(Square.size, Decl(exhaustiveSwitchStatements1.ts, 54, 34)) +>s : Symbol(s, Decl(exhaustiveSwitchStatements1.ts, 64, 14)) +>size : Symbol(Square.size, Decl(exhaustiveSwitchStatements1.ts, 54, 34)) + + case "rectangle": area = s.width * s.height; break; +>area : Symbol(area, Decl(exhaustiveSwitchStatements1.ts, 65, 7)) +>s.width : Symbol(Rectangle.width, Decl(exhaustiveSwitchStatements1.ts, 56, 40)) +>s : Symbol(s, Decl(exhaustiveSwitchStatements1.ts, 64, 14)) +>width : Symbol(Rectangle.width, Decl(exhaustiveSwitchStatements1.ts, 56, 40)) +>s.height : Symbol(Rectangle.height, Decl(exhaustiveSwitchStatements1.ts, 56, 55)) +>s : Symbol(s, Decl(exhaustiveSwitchStatements1.ts, 64, 14)) +>height : Symbol(Rectangle.height, Decl(exhaustiveSwitchStatements1.ts, 56, 55)) + + case "circle": area = Math.PI * s.radius * s.radius; break; +>area : Symbol(area, Decl(exhaustiveSwitchStatements1.ts, 65, 7)) +>Math.PI : Symbol(Math.PI, Decl(lib.es5.d.ts, --, --)) +>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +>PI : Symbol(Math.PI, Decl(lib.es5.d.ts, --, --)) +>s.radius : Symbol(Circle.radius, Decl(exhaustiveSwitchStatements1.ts, 58, 34)) +>s : Symbol(s, Decl(exhaustiveSwitchStatements1.ts, 64, 14)) +>radius : Symbol(Circle.radius, Decl(exhaustiveSwitchStatements1.ts, 58, 34)) +>s.radius : Symbol(Circle.radius, Decl(exhaustiveSwitchStatements1.ts, 58, 34)) +>s : Symbol(s, Decl(exhaustiveSwitchStatements1.ts, 64, 14)) +>radius : Symbol(Circle.radius, Decl(exhaustiveSwitchStatements1.ts, 58, 34)) + + case "triangle": area = Math.sqrt(3) / 4 * s.side * s.side; break; +>area : Symbol(area, Decl(exhaustiveSwitchStatements1.ts, 65, 7)) +>Math.sqrt : Symbol(Math.sqrt, Decl(lib.es5.d.ts, --, --)) +>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +>sqrt : Symbol(Math.sqrt, Decl(lib.es5.d.ts, --, --)) +>s.side : Symbol(Triangle.side, Decl(exhaustiveSwitchStatements1.ts, 60, 38)) +>s : Symbol(s, Decl(exhaustiveSwitchStatements1.ts, 64, 14)) +>side : Symbol(Triangle.side, Decl(exhaustiveSwitchStatements1.ts, 60, 38)) +>s.side : Symbol(Triangle.side, Decl(exhaustiveSwitchStatements1.ts, 60, 38)) +>s : Symbol(s, Decl(exhaustiveSwitchStatements1.ts, 64, 14)) +>side : Symbol(Triangle.side, Decl(exhaustiveSwitchStatements1.ts, 60, 38)) + } + return area; +>area : Symbol(area, Decl(exhaustiveSwitchStatements1.ts, 65, 7)) +} + +function areaWrapped(s: Shape): number { +>areaWrapped : Symbol(areaWrapped, Decl(exhaustiveSwitchStatements1.ts, 73, 1)) +>s : Symbol(s, Decl(exhaustiveSwitchStatements1.ts, 75, 21)) +>Shape : Symbol(Shape, Decl(exhaustiveSwitchStatements1.ts, 60, 54)) + + let area; +>area : Symbol(area, Decl(exhaustiveSwitchStatements1.ts, 76, 7)) + + area = (() => { +>area : Symbol(area, Decl(exhaustiveSwitchStatements1.ts, 76, 7)) + + switch (s.kind) { +>s.kind : Symbol(kind, Decl(exhaustiveSwitchStatements1.ts, 54, 18), Decl(exhaustiveSwitchStatements1.ts, 56, 21), Decl(exhaustiveSwitchStatements1.ts, 58, 18), Decl(exhaustiveSwitchStatements1.ts, 60, 20)) +>s : Symbol(s, Decl(exhaustiveSwitchStatements1.ts, 75, 21)) +>kind : Symbol(kind, Decl(exhaustiveSwitchStatements1.ts, 54, 18), Decl(exhaustiveSwitchStatements1.ts, 56, 21), Decl(exhaustiveSwitchStatements1.ts, 58, 18), Decl(exhaustiveSwitchStatements1.ts, 60, 20)) + + case "square": return s.size * s.size; +>s.size : Symbol(Square.size, Decl(exhaustiveSwitchStatements1.ts, 54, 34)) +>s : Symbol(s, Decl(exhaustiveSwitchStatements1.ts, 75, 21)) +>size : Symbol(Square.size, Decl(exhaustiveSwitchStatements1.ts, 54, 34)) +>s.size : Symbol(Square.size, Decl(exhaustiveSwitchStatements1.ts, 54, 34)) +>s : Symbol(s, Decl(exhaustiveSwitchStatements1.ts, 75, 21)) +>size : Symbol(Square.size, Decl(exhaustiveSwitchStatements1.ts, 54, 34)) + + case "rectangle": return s.width * s.height; +>s.width : Symbol(Rectangle.width, Decl(exhaustiveSwitchStatements1.ts, 56, 40)) +>s : Symbol(s, Decl(exhaustiveSwitchStatements1.ts, 75, 21)) +>width : Symbol(Rectangle.width, Decl(exhaustiveSwitchStatements1.ts, 56, 40)) +>s.height : Symbol(Rectangle.height, Decl(exhaustiveSwitchStatements1.ts, 56, 55)) +>s : Symbol(s, Decl(exhaustiveSwitchStatements1.ts, 75, 21)) +>height : Symbol(Rectangle.height, Decl(exhaustiveSwitchStatements1.ts, 56, 55)) + + case "circle": return Math.PI * s.radius * s.radius; +>Math.PI : Symbol(Math.PI, Decl(lib.es5.d.ts, --, --)) +>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +>PI : Symbol(Math.PI, Decl(lib.es5.d.ts, --, --)) +>s.radius : Symbol(Circle.radius, Decl(exhaustiveSwitchStatements1.ts, 58, 34)) +>s : Symbol(s, Decl(exhaustiveSwitchStatements1.ts, 75, 21)) +>radius : Symbol(Circle.radius, Decl(exhaustiveSwitchStatements1.ts, 58, 34)) +>s.radius : Symbol(Circle.radius, Decl(exhaustiveSwitchStatements1.ts, 58, 34)) +>s : Symbol(s, Decl(exhaustiveSwitchStatements1.ts, 75, 21)) +>radius : Symbol(Circle.radius, Decl(exhaustiveSwitchStatements1.ts, 58, 34)) + + case "triangle": return Math.sqrt(3) / 4 * s.side * s.side; +>Math.sqrt : Symbol(Math.sqrt, Decl(lib.es5.d.ts, --, --)) +>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +>sqrt : Symbol(Math.sqrt, Decl(lib.es5.d.ts, --, --)) +>s.side : Symbol(Triangle.side, Decl(exhaustiveSwitchStatements1.ts, 60, 38)) +>s : Symbol(s, Decl(exhaustiveSwitchStatements1.ts, 75, 21)) +>side : Symbol(Triangle.side, Decl(exhaustiveSwitchStatements1.ts, 60, 38)) +>s.side : Symbol(Triangle.side, Decl(exhaustiveSwitchStatements1.ts, 60, 38)) +>s : Symbol(s, Decl(exhaustiveSwitchStatements1.ts, 75, 21)) +>side : Symbol(Triangle.side, Decl(exhaustiveSwitchStatements1.ts, 60, 38)) + } + })(); + return area; +>area : Symbol(area, Decl(exhaustiveSwitchStatements1.ts, 76, 7)) +} + +// Repro from #13241 + +enum MyEnum { +>MyEnum : Symbol(MyEnum, Decl(exhaustiveSwitchStatements1.ts, 86, 1)) + + A, +>A : Symbol(MyEnum.A, Decl(exhaustiveSwitchStatements1.ts, 90, 13)) + + B +>B : Symbol(MyEnum.B, Decl(exhaustiveSwitchStatements1.ts, 91, 3)) +} + +function thisGivesError(e: MyEnum): string { +>thisGivesError : Symbol(thisGivesError, Decl(exhaustiveSwitchStatements1.ts, 93, 1)) +>e : Symbol(e, Decl(exhaustiveSwitchStatements1.ts, 95, 24)) +>MyEnum : Symbol(MyEnum, Decl(exhaustiveSwitchStatements1.ts, 86, 1)) + + let s: string; +>s : Symbol(s, Decl(exhaustiveSwitchStatements1.ts, 96, 4)) + + switch (e) { +>e : Symbol(e, Decl(exhaustiveSwitchStatements1.ts, 95, 24)) + + case MyEnum.A: s = "it was A"; break; +>MyEnum.A : Symbol(MyEnum.A, Decl(exhaustiveSwitchStatements1.ts, 90, 13)) +>MyEnum : Symbol(MyEnum, Decl(exhaustiveSwitchStatements1.ts, 86, 1)) +>A : Symbol(MyEnum.A, Decl(exhaustiveSwitchStatements1.ts, 90, 13)) +>s : Symbol(s, Decl(exhaustiveSwitchStatements1.ts, 96, 4)) + + case MyEnum.B: s = "it was B"; break; +>MyEnum.B : Symbol(MyEnum.B, Decl(exhaustiveSwitchStatements1.ts, 91, 3)) +>MyEnum : Symbol(MyEnum, Decl(exhaustiveSwitchStatements1.ts, 86, 1)) +>B : Symbol(MyEnum.B, Decl(exhaustiveSwitchStatements1.ts, 91, 3)) +>s : Symbol(s, Decl(exhaustiveSwitchStatements1.ts, 96, 4)) + } + return s; +>s : Symbol(s, Decl(exhaustiveSwitchStatements1.ts, 96, 4)) +} + +function good1(e: MyEnum): string { +>good1 : Symbol(good1, Decl(exhaustiveSwitchStatements1.ts, 102, 1)) +>e : Symbol(e, Decl(exhaustiveSwitchStatements1.ts, 104, 15)) +>MyEnum : Symbol(MyEnum, Decl(exhaustiveSwitchStatements1.ts, 86, 1)) + + let s: string; +>s : Symbol(s, Decl(exhaustiveSwitchStatements1.ts, 105, 4)) + + switch (e) { +>e : Symbol(e, Decl(exhaustiveSwitchStatements1.ts, 104, 15)) + + case MyEnum.A: s = "it was A"; break; +>MyEnum.A : Symbol(MyEnum.A, Decl(exhaustiveSwitchStatements1.ts, 90, 13)) +>MyEnum : Symbol(MyEnum, Decl(exhaustiveSwitchStatements1.ts, 86, 1)) +>A : Symbol(MyEnum.A, Decl(exhaustiveSwitchStatements1.ts, 90, 13)) +>s : Symbol(s, Decl(exhaustiveSwitchStatements1.ts, 105, 4)) + + case MyEnum.B: s = "it was B"; break; +>MyEnum.B : Symbol(MyEnum.B, Decl(exhaustiveSwitchStatements1.ts, 91, 3)) +>MyEnum : Symbol(MyEnum, Decl(exhaustiveSwitchStatements1.ts, 86, 1)) +>B : Symbol(MyEnum.B, Decl(exhaustiveSwitchStatements1.ts, 91, 3)) +>s : Symbol(s, Decl(exhaustiveSwitchStatements1.ts, 105, 4)) + + default: s = "it was something else"; break; +>s : Symbol(s, Decl(exhaustiveSwitchStatements1.ts, 105, 4)) + } + return s; +>s : Symbol(s, Decl(exhaustiveSwitchStatements1.ts, 105, 4)) +} + +function good2(e: MyEnum): string { +>good2 : Symbol(good2, Decl(exhaustiveSwitchStatements1.ts, 112, 1)) +>e : Symbol(e, Decl(exhaustiveSwitchStatements1.ts, 114, 15)) +>MyEnum : Symbol(MyEnum, Decl(exhaustiveSwitchStatements1.ts, 86, 1)) + + switch (e) { +>e : Symbol(e, Decl(exhaustiveSwitchStatements1.ts, 114, 15)) + + case MyEnum.A: return "it was A"; +>MyEnum.A : Symbol(MyEnum.A, Decl(exhaustiveSwitchStatements1.ts, 90, 13)) +>MyEnum : Symbol(MyEnum, Decl(exhaustiveSwitchStatements1.ts, 86, 1)) +>A : Symbol(MyEnum.A, Decl(exhaustiveSwitchStatements1.ts, 90, 13)) + + case MyEnum.B: return "it was B"; +>MyEnum.B : Symbol(MyEnum.B, Decl(exhaustiveSwitchStatements1.ts, 91, 3)) +>MyEnum : Symbol(MyEnum, Decl(exhaustiveSwitchStatements1.ts, 86, 1)) +>B : Symbol(MyEnum.B, Decl(exhaustiveSwitchStatements1.ts, 91, 3)) + } +} + +// Repro from #18362 + +enum Level { +>Level : Symbol(Level, Decl(exhaustiveSwitchStatements1.ts, 119, 1)) + + One, +>One : Symbol(Level.One, Decl(exhaustiveSwitchStatements1.ts, 123, 12)) + + Two, +>Two : Symbol(Level.Two, Decl(exhaustiveSwitchStatements1.ts, 124, 6)) +} + +const doSomethingWithLevel = (level: Level) => { +>doSomethingWithLevel : Symbol(doSomethingWithLevel, Decl(exhaustiveSwitchStatements1.ts, 128, 5)) +>level : Symbol(level, Decl(exhaustiveSwitchStatements1.ts, 128, 30)) +>Level : Symbol(Level, Decl(exhaustiveSwitchStatements1.ts, 119, 1)) + + let next: Level; +>next : Symbol(next, Decl(exhaustiveSwitchStatements1.ts, 129, 5)) +>Level : Symbol(Level, Decl(exhaustiveSwitchStatements1.ts, 119, 1)) + + switch (level) { +>level : Symbol(level, Decl(exhaustiveSwitchStatements1.ts, 128, 30)) + + case Level.One: +>Level.One : Symbol(Level.One, Decl(exhaustiveSwitchStatements1.ts, 123, 12)) +>Level : Symbol(Level, Decl(exhaustiveSwitchStatements1.ts, 119, 1)) +>One : Symbol(Level.One, Decl(exhaustiveSwitchStatements1.ts, 123, 12)) + + next = Level.Two; +>next : Symbol(next, Decl(exhaustiveSwitchStatements1.ts, 129, 5)) +>Level.Two : Symbol(Level.Two, Decl(exhaustiveSwitchStatements1.ts, 124, 6)) +>Level : Symbol(Level, Decl(exhaustiveSwitchStatements1.ts, 119, 1)) +>Two : Symbol(Level.Two, Decl(exhaustiveSwitchStatements1.ts, 124, 6)) + + break; + case Level.Two: +>Level.Two : Symbol(Level.Two, Decl(exhaustiveSwitchStatements1.ts, 124, 6)) +>Level : Symbol(Level, Decl(exhaustiveSwitchStatements1.ts, 119, 1)) +>Two : Symbol(Level.Two, Decl(exhaustiveSwitchStatements1.ts, 124, 6)) + + next = Level.One; +>next : Symbol(next, Decl(exhaustiveSwitchStatements1.ts, 129, 5)) +>Level.One : Symbol(Level.One, Decl(exhaustiveSwitchStatements1.ts, 123, 12)) +>Level : Symbol(Level, Decl(exhaustiveSwitchStatements1.ts, 119, 1)) +>One : Symbol(Level.One, Decl(exhaustiveSwitchStatements1.ts, 123, 12)) + + break; + } + return next; +>next : Symbol(next, Decl(exhaustiveSwitchStatements1.ts, 129, 5)) + +}; + +// Repro from #20409 + +interface Square2 { +>Square2 : Symbol(Square2, Decl(exhaustiveSwitchStatements1.ts, 139, 2)) + + kind: "square"; +>kind : Symbol(Square2.kind, Decl(exhaustiveSwitchStatements1.ts, 143, 19)) + + size: number; +>size : Symbol(Square2.size, Decl(exhaustiveSwitchStatements1.ts, 144, 19)) +} + +interface Circle2 { +>Circle2 : Symbol(Circle2, Decl(exhaustiveSwitchStatements1.ts, 146, 1)) + + kind: "circle"; +>kind : Symbol(Circle2.kind, Decl(exhaustiveSwitchStatements1.ts, 148, 19)) + + radius: number; +>radius : Symbol(Circle2.radius, Decl(exhaustiveSwitchStatements1.ts, 149, 19)) +} + +type Shape2 = Square2 | Circle2; +>Shape2 : Symbol(Shape2, Decl(exhaustiveSwitchStatements1.ts, 151, 1)) +>Square2 : Symbol(Square2, Decl(exhaustiveSwitchStatements1.ts, 139, 2)) +>Circle2 : Symbol(Circle2, Decl(exhaustiveSwitchStatements1.ts, 146, 1)) + +function withDefault(s1: Shape2, s2: Shape2): string { +>withDefault : Symbol(withDefault, Decl(exhaustiveSwitchStatements1.ts, 153, 32)) +>s1 : Symbol(s1, Decl(exhaustiveSwitchStatements1.ts, 155, 21)) +>Shape2 : Symbol(Shape2, Decl(exhaustiveSwitchStatements1.ts, 151, 1)) +>s2 : Symbol(s2, Decl(exhaustiveSwitchStatements1.ts, 155, 32)) +>Shape2 : Symbol(Shape2, Decl(exhaustiveSwitchStatements1.ts, 151, 1)) + + switch (s1.kind) { +>s1.kind : Symbol(kind, Decl(exhaustiveSwitchStatements1.ts, 143, 19), Decl(exhaustiveSwitchStatements1.ts, 148, 19)) +>s1 : Symbol(s1, Decl(exhaustiveSwitchStatements1.ts, 155, 21)) +>kind : Symbol(kind, Decl(exhaustiveSwitchStatements1.ts, 143, 19), Decl(exhaustiveSwitchStatements1.ts, 148, 19)) + + case "square": + return "1"; + case "circle": + switch (s2.kind) { +>s2.kind : Symbol(kind, Decl(exhaustiveSwitchStatements1.ts, 143, 19), Decl(exhaustiveSwitchStatements1.ts, 148, 19)) +>s2 : Symbol(s2, Decl(exhaustiveSwitchStatements1.ts, 155, 32)) +>kind : Symbol(kind, Decl(exhaustiveSwitchStatements1.ts, 143, 19), Decl(exhaustiveSwitchStatements1.ts, 148, 19)) + + case "square": + return "2"; + case "circle": + return "3"; + default: + return "never"; + } + } +} + +function withoutDefault(s1: Shape2, s2: Shape2): string { +>withoutDefault : Symbol(withoutDefault, Decl(exhaustiveSwitchStatements1.ts, 169, 1)) +>s1 : Symbol(s1, Decl(exhaustiveSwitchStatements1.ts, 171, 24)) +>Shape2 : Symbol(Shape2, Decl(exhaustiveSwitchStatements1.ts, 151, 1)) +>s2 : Symbol(s2, Decl(exhaustiveSwitchStatements1.ts, 171, 35)) +>Shape2 : Symbol(Shape2, Decl(exhaustiveSwitchStatements1.ts, 151, 1)) + + switch (s1.kind) { +>s1.kind : Symbol(kind, Decl(exhaustiveSwitchStatements1.ts, 143, 19), Decl(exhaustiveSwitchStatements1.ts, 148, 19)) +>s1 : Symbol(s1, Decl(exhaustiveSwitchStatements1.ts, 171, 24)) +>kind : Symbol(kind, Decl(exhaustiveSwitchStatements1.ts, 143, 19), Decl(exhaustiveSwitchStatements1.ts, 148, 19)) + + case "square": + return "1"; + case "circle": + switch (s2.kind) { +>s2.kind : Symbol(kind, Decl(exhaustiveSwitchStatements1.ts, 143, 19), Decl(exhaustiveSwitchStatements1.ts, 148, 19)) +>s2 : Symbol(s2, Decl(exhaustiveSwitchStatements1.ts, 171, 35)) +>kind : Symbol(kind, Decl(exhaustiveSwitchStatements1.ts, 143, 19), Decl(exhaustiveSwitchStatements1.ts, 148, 19)) + + case "square": + return "2"; + case "circle": + return "3"; + } + } +} + +// Repro from #20823 + +function test4(value: 1 | 2) { +>test4 : Symbol(test4, Decl(exhaustiveSwitchStatements1.ts, 183, 1)) +>value : Symbol(value, Decl(exhaustiveSwitchStatements1.ts, 187, 15)) + + let x: string; +>x : Symbol(x, Decl(exhaustiveSwitchStatements1.ts, 188, 7)) + + switch (value) { +>value : Symbol(value, Decl(exhaustiveSwitchStatements1.ts, 187, 15)) + + case 1: x = "one"; break; +>x : Symbol(x, Decl(exhaustiveSwitchStatements1.ts, 188, 7)) + + case 2: x = "two"; break; +>x : Symbol(x, Decl(exhaustiveSwitchStatements1.ts, 188, 7)) + } + return x; +>x : Symbol(x, Decl(exhaustiveSwitchStatements1.ts, 188, 7)) +} + diff --git a/tests/baselines/reference/exhaustiveSwitchStatements1.types b/tests/baselines/reference/exhaustiveSwitchStatements1.types new file mode 100644 index 00000000000..04a9bf531ce --- /dev/null +++ b/tests/baselines/reference/exhaustiveSwitchStatements1.types @@ -0,0 +1,595 @@ +=== tests/cases/conformance/controlFlow/exhaustiveSwitchStatements1.ts === +function f1(x: 1 | 2): string { +>f1 : (x: 1 | 2) => string +>x : 1 | 2 + + if (!!true) { +>!!true : true +>!true : false +>true : true + + switch (x) { +>x : 1 | 2 + + case 1: return 'a'; +>1 : 1 +>'a' : "a" + + case 2: return 'b'; +>2 : 2 +>'b' : "b" + } + x; // Unreachable +>x : never + } + else { + throw 0; +>0 : 0 + } +} + +function f2(x: 1 | 2) { +>f2 : (x: 1 | 2) => void +>x : 1 | 2 + + let z: number; +>z : number + + switch (x) { +>x : 1 | 2 + + case 1: z = 10; break; +>1 : 1 +>z = 10 : 10 +>z : number +>10 : 10 + + case 2: z = 20; break; +>2 : 2 +>z = 20 : 20 +>z : number +>20 : 20 + } + z; // Definitely assigned +>z : number +} + +function f3(x: 1 | 2) { +>f3 : (x: 1 | 2) => 10 | 20 +>x : 1 | 2 + + switch (x) { +>x : 1 | 2 + + case 1: return 10; +>1 : 1 +>10 : 10 + + case 2: return 20; +>2 : 2 +>20 : 20 + + // Default considered reachable to allow defensive coding + default: throw new Error("Bad input"); +>new Error("Bad input") : Error +>Error : ErrorConstructor +>"Bad input" : "Bad input" + } +} + +// Repro from #11572 + +enum E { A, B } +>E : E +>A : E.A +>B : E.B + +function f(e: E): number { +>f : (e: E) => number +>e : E + + switch (e) { +>e : E + + case E.A: return 0 +>E.A : E.A +>E : typeof E +>A : E.A +>0 : 0 + + case E.B: return 1 +>E.B : E.B +>E : typeof E +>B : E.B +>1 : 1 + } +} + +function g(e: E): number { +>g : (e: E) => number +>e : E + + if (!true) +>!true : false +>true : true + + return -1 +>-1 : -1 +>1 : 1 + + else + switch (e) { +>e : E + + case E.A: return 0 +>E.A : E.A +>E : typeof E +>A : E.A +>0 : 0 + + case E.B: return 1 +>E.B : E.B +>E : typeof E +>B : E.B +>1 : 1 + } +} + +// Repro from #12668 + +interface Square { kind: "square"; size: number; } +>kind : "square" +>size : number + +interface Rectangle { kind: "rectangle"; width: number; height: number; } +>kind : "rectangle" +>width : number +>height : number + +interface Circle { kind: "circle"; radius: number; } +>kind : "circle" +>radius : number + +interface Triangle { kind: "triangle"; side: number; } +>kind : "triangle" +>side : number + +type Shape = Square | Rectangle | Circle | Triangle; +>Shape : Shape + +function area(s: Shape): number { +>area : (s: Shape) => number +>s : Shape + + let area; +>area : any + + switch (s.kind) { +>s.kind : "square" | "rectangle" | "circle" | "triangle" +>s : Shape +>kind : "square" | "rectangle" | "circle" | "triangle" + + case "square": area = s.size * s.size; break; +>"square" : "square" +>area = s.size * s.size : number +>area : any +>s.size * s.size : number +>s.size : number +>s : Square +>size : number +>s.size : number +>s : Square +>size : number + + case "rectangle": area = s.width * s.height; break; +>"rectangle" : "rectangle" +>area = s.width * s.height : number +>area : any +>s.width * s.height : number +>s.width : number +>s : Rectangle +>width : number +>s.height : number +>s : Rectangle +>height : number + + case "circle": area = Math.PI * s.radius * s.radius; break; +>"circle" : "circle" +>area = Math.PI * s.radius * s.radius : number +>area : any +>Math.PI * s.radius * s.radius : number +>Math.PI * s.radius : number +>Math.PI : number +>Math : Math +>PI : number +>s.radius : number +>s : Circle +>radius : number +>s.radius : number +>s : Circle +>radius : number + + case "triangle": area = Math.sqrt(3) / 4 * s.side * s.side; break; +>"triangle" : "triangle" +>area = Math.sqrt(3) / 4 * s.side * s.side : number +>area : any +>Math.sqrt(3) / 4 * s.side * s.side : number +>Math.sqrt(3) / 4 * s.side : number +>Math.sqrt(3) / 4 : number +>Math.sqrt(3) : number +>Math.sqrt : (x: number) => number +>Math : Math +>sqrt : (x: number) => number +>3 : 3 +>4 : 4 +>s.side : number +>s : Triangle +>side : number +>s.side : number +>s : Triangle +>side : number + } + return area; +>area : number +} + +function areaWrapped(s: Shape): number { +>areaWrapped : (s: Shape) => number +>s : Shape + + let area; +>area : any + + area = (() => { +>area = (() => { switch (s.kind) { case "square": return s.size * s.size; case "rectangle": return s.width * s.height; case "circle": return Math.PI * s.radius * s.radius; case "triangle": return Math.sqrt(3) / 4 * s.side * s.side; } })() : number +>area : any +>(() => { switch (s.kind) { case "square": return s.size * s.size; case "rectangle": return s.width * s.height; case "circle": return Math.PI * s.radius * s.radius; case "triangle": return Math.sqrt(3) / 4 * s.side * s.side; } })() : number +>(() => { switch (s.kind) { case "square": return s.size * s.size; case "rectangle": return s.width * s.height; case "circle": return Math.PI * s.radius * s.radius; case "triangle": return Math.sqrt(3) / 4 * s.side * s.side; } }) : () => number +>() => { switch (s.kind) { case "square": return s.size * s.size; case "rectangle": return s.width * s.height; case "circle": return Math.PI * s.radius * s.radius; case "triangle": return Math.sqrt(3) / 4 * s.side * s.side; } } : () => number + + switch (s.kind) { +>s.kind : "square" | "rectangle" | "circle" | "triangle" +>s : Shape +>kind : "square" | "rectangle" | "circle" | "triangle" + + case "square": return s.size * s.size; +>"square" : "square" +>s.size * s.size : number +>s.size : number +>s : Square +>size : number +>s.size : number +>s : Square +>size : number + + case "rectangle": return s.width * s.height; +>"rectangle" : "rectangle" +>s.width * s.height : number +>s.width : number +>s : Rectangle +>width : number +>s.height : number +>s : Rectangle +>height : number + + case "circle": return Math.PI * s.radius * s.radius; +>"circle" : "circle" +>Math.PI * s.radius * s.radius : number +>Math.PI * s.radius : number +>Math.PI : number +>Math : Math +>PI : number +>s.radius : number +>s : Circle +>radius : number +>s.radius : number +>s : Circle +>radius : number + + case "triangle": return Math.sqrt(3) / 4 * s.side * s.side; +>"triangle" : "triangle" +>Math.sqrt(3) / 4 * s.side * s.side : number +>Math.sqrt(3) / 4 * s.side : number +>Math.sqrt(3) / 4 : number +>Math.sqrt(3) : number +>Math.sqrt : (x: number) => number +>Math : Math +>sqrt : (x: number) => number +>3 : 3 +>4 : 4 +>s.side : number +>s : Triangle +>side : number +>s.side : number +>s : Triangle +>side : number + } + })(); + return area; +>area : number +} + +// Repro from #13241 + +enum MyEnum { +>MyEnum : MyEnum + + A, +>A : MyEnum.A + + B +>B : MyEnum.B +} + +function thisGivesError(e: MyEnum): string { +>thisGivesError : (e: MyEnum) => string +>e : MyEnum + + let s: string; +>s : string + + switch (e) { +>e : MyEnum + + case MyEnum.A: s = "it was A"; break; +>MyEnum.A : MyEnum.A +>MyEnum : typeof MyEnum +>A : MyEnum.A +>s = "it was A" : "it was A" +>s : string +>"it was A" : "it was A" + + case MyEnum.B: s = "it was B"; break; +>MyEnum.B : MyEnum.B +>MyEnum : typeof MyEnum +>B : MyEnum.B +>s = "it was B" : "it was B" +>s : string +>"it was B" : "it was B" + } + return s; +>s : string +} + +function good1(e: MyEnum): string { +>good1 : (e: MyEnum) => string +>e : MyEnum + + let s: string; +>s : string + + switch (e) { +>e : MyEnum + + case MyEnum.A: s = "it was A"; break; +>MyEnum.A : MyEnum.A +>MyEnum : typeof MyEnum +>A : MyEnum.A +>s = "it was A" : "it was A" +>s : string +>"it was A" : "it was A" + + case MyEnum.B: s = "it was B"; break; +>MyEnum.B : MyEnum.B +>MyEnum : typeof MyEnum +>B : MyEnum.B +>s = "it was B" : "it was B" +>s : string +>"it was B" : "it was B" + + default: s = "it was something else"; break; +>s = "it was something else" : "it was something else" +>s : string +>"it was something else" : "it was something else" + } + return s; +>s : string +} + +function good2(e: MyEnum): string { +>good2 : (e: MyEnum) => string +>e : MyEnum + + switch (e) { +>e : MyEnum + + case MyEnum.A: return "it was A"; +>MyEnum.A : MyEnum.A +>MyEnum : typeof MyEnum +>A : MyEnum.A +>"it was A" : "it was A" + + case MyEnum.B: return "it was B"; +>MyEnum.B : MyEnum.B +>MyEnum : typeof MyEnum +>B : MyEnum.B +>"it was B" : "it was B" + } +} + +// Repro from #18362 + +enum Level { +>Level : Level + + One, +>One : Level.One + + Two, +>Two : Level.Two +} + +const doSomethingWithLevel = (level: Level) => { +>doSomethingWithLevel : (level: Level) => Level +>(level: Level) => { let next: Level; switch (level) { case Level.One: next = Level.Two; break; case Level.Two: next = Level.One; break; } return next;} : (level: Level) => Level +>level : Level + + let next: Level; +>next : Level + + switch (level) { +>level : Level + + case Level.One: +>Level.One : Level.One +>Level : typeof Level +>One : Level.One + + next = Level.Two; +>next = Level.Two : Level.Two +>next : Level +>Level.Two : Level.Two +>Level : typeof Level +>Two : Level.Two + + break; + case Level.Two: +>Level.Two : Level.Two +>Level : typeof Level +>Two : Level.Two + + next = Level.One; +>next = Level.One : Level.One +>next : Level +>Level.One : Level.One +>Level : typeof Level +>One : Level.One + + break; + } + return next; +>next : Level + +}; + +// Repro from #20409 + +interface Square2 { + kind: "square"; +>kind : "square" + + size: number; +>size : number +} + +interface Circle2 { + kind: "circle"; +>kind : "circle" + + radius: number; +>radius : number +} + +type Shape2 = Square2 | Circle2; +>Shape2 : Shape2 + +function withDefault(s1: Shape2, s2: Shape2): string { +>withDefault : (s1: Shape2, s2: Shape2) => string +>s1 : Shape2 +>s2 : Shape2 + + switch (s1.kind) { +>s1.kind : "square" | "circle" +>s1 : Shape2 +>kind : "square" | "circle" + + case "square": +>"square" : "square" + + return "1"; +>"1" : "1" + + case "circle": +>"circle" : "circle" + + switch (s2.kind) { +>s2.kind : "square" | "circle" +>s2 : Shape2 +>kind : "square" | "circle" + + case "square": +>"square" : "square" + + return "2"; +>"2" : "2" + + case "circle": +>"circle" : "circle" + + return "3"; +>"3" : "3" + + default: + return "never"; +>"never" : "never" + } + } +} + +function withoutDefault(s1: Shape2, s2: Shape2): string { +>withoutDefault : (s1: Shape2, s2: Shape2) => string +>s1 : Shape2 +>s2 : Shape2 + + switch (s1.kind) { +>s1.kind : "square" | "circle" +>s1 : Shape2 +>kind : "square" | "circle" + + case "square": +>"square" : "square" + + return "1"; +>"1" : "1" + + case "circle": +>"circle" : "circle" + + switch (s2.kind) { +>s2.kind : "square" | "circle" +>s2 : Shape2 +>kind : "square" | "circle" + + case "square": +>"square" : "square" + + return "2"; +>"2" : "2" + + case "circle": +>"circle" : "circle" + + return "3"; +>"3" : "3" + } + } +} + +// Repro from #20823 + +function test4(value: 1 | 2) { +>test4 : (value: 1 | 2) => string +>value : 1 | 2 + + let x: string; +>x : string + + switch (value) { +>value : 1 | 2 + + case 1: x = "one"; break; +>1 : 1 +>x = "one" : "one" +>x : string +>"one" : "one" + + case 2: x = "two"; break; +>2 : 2 +>x = "two" : "two" +>x : string +>"two" : "two" + } + return x; +>x : string +} + diff --git a/tests/baselines/reference/neverReturningFunctions1.errors.txt b/tests/baselines/reference/neverReturningFunctions1.errors.txt new file mode 100644 index 00000000000..f8656bbed08 --- /dev/null +++ b/tests/baselines/reference/neverReturningFunctions1.errors.txt @@ -0,0 +1,227 @@ +tests/cases/conformance/controlFlow/neverReturningFunctions1.ts(13,5): error TS7027: Unreachable code detected. +tests/cases/conformance/controlFlow/neverReturningFunctions1.ts(19,5): error TS7027: Unreachable code detected. +tests/cases/conformance/controlFlow/neverReturningFunctions1.ts(30,5): error TS7027: Unreachable code detected. +tests/cases/conformance/controlFlow/neverReturningFunctions1.ts(36,5): error TS7027: Unreachable code detected. +tests/cases/conformance/controlFlow/neverReturningFunctions1.ts(51,5): error TS7027: Unreachable code detected. +tests/cases/conformance/controlFlow/neverReturningFunctions1.ts(57,5): error TS7027: Unreachable code detected. +tests/cases/conformance/controlFlow/neverReturningFunctions1.ts(63,5): error TS7027: Unreachable code detected. +tests/cases/conformance/controlFlow/neverReturningFunctions1.ts(77,9): error TS7027: Unreachable code detected. +tests/cases/conformance/controlFlow/neverReturningFunctions1.ts(82,9): error TS7027: Unreachable code detected. +tests/cases/conformance/controlFlow/neverReturningFunctions1.ts(89,9): error TS7027: Unreachable code detected. +tests/cases/conformance/controlFlow/neverReturningFunctions1.ts(96,13): error TS7027: Unreachable code detected. +tests/cases/conformance/controlFlow/neverReturningFunctions1.ts(101,13): error TS7027: Unreachable code detected. +tests/cases/conformance/controlFlow/neverReturningFunctions1.ts(103,9): error TS7027: Unreachable code detected. +tests/cases/conformance/controlFlow/neverReturningFunctions1.ts(105,5): error TS7027: Unreachable code detected. +tests/cases/conformance/controlFlow/neverReturningFunctions1.ts(111,9): error TS7027: Unreachable code detected. +tests/cases/conformance/controlFlow/neverReturningFunctions1.ts(112,9): error TS7027: Unreachable code detected. +tests/cases/conformance/controlFlow/neverReturningFunctions1.ts(122,9): error TS7027: Unreachable code detected. +tests/cases/conformance/controlFlow/neverReturningFunctions1.ts(127,9): error TS7027: Unreachable code detected. +tests/cases/conformance/controlFlow/neverReturningFunctions1.ts(129,5): error TS7027: Unreachable code detected. +tests/cases/conformance/controlFlow/neverReturningFunctions1.ts(139,9): error TS7027: Unreachable code detected. +tests/cases/conformance/controlFlow/neverReturningFunctions1.ts(141,5): error TS7027: Unreachable code detected. +tests/cases/conformance/controlFlow/neverReturningFunctions1.ts(148,9): error TS7027: Unreachable code detected. +tests/cases/conformance/controlFlow/neverReturningFunctions1.ts(153,5): error TS7027: Unreachable code detected. + + +==== tests/cases/conformance/controlFlow/neverReturningFunctions1.ts (23 errors) ==== + function fail(message?: string): never { + throw new Error(message); + } + + function f01(x: string | undefined) { + if (x === undefined) fail("undefined argument"); + x.length; // string + } + + function f02(x: number): number { + if (x >= 0) return x; + fail("negative number"); + x; // Unreachable + ~~ +!!! error TS7027: Unreachable code detected. + } + + function f03(x: string) { + x; // string + fail(); + x; // Unreachable + ~~ +!!! error TS7027: Unreachable code detected. + } + + function f11(x: string | undefined, fail: (message?: string) => never) { + if (x === undefined) fail("undefined argument"); + x.length; // string + } + + function f12(x: number, fail: (message?: string) => never): number { + if (x >= 0) return x; + fail("negative number"); + x; // Unreachable + ~~ +!!! error TS7027: Unreachable code detected. + } + + function f13(x: string, fail: (message?: string) => never) { + x; // string + fail(); + x; // Unreachable + ~~ +!!! error TS7027: Unreachable code detected. + } + + namespace Debug { + export declare function fail(message?: string): never; + } + + function f21(x: string | undefined) { + if (x === undefined) Debug.fail("undefined argument"); + x.length; // string + } + + function f22(x: number): number { + if (x >= 0) return x; + Debug.fail("negative number"); + x; // Unreachable + ~~ +!!! error TS7027: Unreachable code detected. + } + + function f23(x: string) { + x; // string + Debug.fail(); + x; // Unreachable + ~~ +!!! error TS7027: Unreachable code detected. + } + + function f24(x: string) { + x; // string + ((Debug).fail)(); + x; // Unreachable + ~~ +!!! error TS7027: Unreachable code detected. + } + + class Test { + fail(message?: string): never { + throw new Error(message); + } + f1(x: string | undefined) { + if (x === undefined) this.fail("undefined argument"); + x.length; // string + } + f2(x: number): number { + if (x >= 0) return x; + this.fail("negative number"); + x; // Unreachable + ~~ +!!! error TS7027: Unreachable code detected. + } + f3(x: string) { + x; // string + this.fail(); + x; // Unreachable + ~~ +!!! error TS7027: Unreachable code detected. + } + } + + function f30(x: string | number | undefined) { + if (typeof x === "string") { + fail(); + x; // Unreachable + ~~ +!!! error TS7027: Unreachable code detected. + } + else { + x; // number | undefined + if (x !== undefined) { + x; // number + fail(); + x; // Unreachable + ~~ +!!! error TS7027: Unreachable code detected. + } + else { + x; // undefined + fail(); + x; // Unreachable + ~~ +!!! error TS7027: Unreachable code detected. + } + x; // Unreachable + ~~ +!!! error TS7027: Unreachable code detected. + } + x; // Unreachable + ~~ +!!! error TS7027: Unreachable code detected. + } + + function f31(x: { a: string | number }) { + if (typeof x.a === "string") { + fail(); + x; // Unreachable + ~~ +!!! error TS7027: Unreachable code detected. + x.a; // Unreachable + ~~~~ +!!! error TS7027: Unreachable code detected. + } + x; // { a: string | number } + x.a; // number + } + + function f40(x: number) { + try { + x; + fail(); + x; // Unreachable + ~~ +!!! error TS7027: Unreachable code detected. + } + finally { + x; + fail(); + x; // Unreachable + ~~ +!!! error TS7027: Unreachable code detected. + } + x; // Unreachable + ~~ +!!! error TS7027: Unreachable code detected. + } + + function f41(x: number) { + try { + x; + } + finally { + x; + fail(); + x; // Unreachable + ~~ +!!! error TS7027: Unreachable code detected. + } + x; // Unreachable + ~~ +!!! error TS7027: Unreachable code detected. + } + + function f42(x: number) { + try { + x; + fail(); + x; // Unreachable + ~~ +!!! error TS7027: Unreachable code detected. + } + finally { + x; + } + x; // Unreachable + ~~ +!!! error TS7027: Unreachable code detected. + } + \ No newline at end of file diff --git a/tests/baselines/reference/neverReturningFunctions1.js b/tests/baselines/reference/neverReturningFunctions1.js new file mode 100644 index 00000000000..cce75c6668a --- /dev/null +++ b/tests/baselines/reference/neverReturningFunctions1.js @@ -0,0 +1,337 @@ +//// [neverReturningFunctions1.ts] +function fail(message?: string): never { + throw new Error(message); +} + +function f01(x: string | undefined) { + if (x === undefined) fail("undefined argument"); + x.length; // string +} + +function f02(x: number): number { + if (x >= 0) return x; + fail("negative number"); + x; // Unreachable +} + +function f03(x: string) { + x; // string + fail(); + x; // Unreachable +} + +function f11(x: string | undefined, fail: (message?: string) => never) { + if (x === undefined) fail("undefined argument"); + x.length; // string +} + +function f12(x: number, fail: (message?: string) => never): number { + if (x >= 0) return x; + fail("negative number"); + x; // Unreachable +} + +function f13(x: string, fail: (message?: string) => never) { + x; // string + fail(); + x; // Unreachable +} + +namespace Debug { + export declare function fail(message?: string): never; +} + +function f21(x: string | undefined) { + if (x === undefined) Debug.fail("undefined argument"); + x.length; // string +} + +function f22(x: number): number { + if (x >= 0) return x; + Debug.fail("negative number"); + x; // Unreachable +} + +function f23(x: string) { + x; // string + Debug.fail(); + x; // Unreachable +} + +function f24(x: string) { + x; // string + ((Debug).fail)(); + x; // Unreachable +} + +class Test { + fail(message?: string): never { + throw new Error(message); + } + f1(x: string | undefined) { + if (x === undefined) this.fail("undefined argument"); + x.length; // string + } + f2(x: number): number { + if (x >= 0) return x; + this.fail("negative number"); + x; // Unreachable + } + f3(x: string) { + x; // string + this.fail(); + x; // Unreachable + } +} + +function f30(x: string | number | undefined) { + if (typeof x === "string") { + fail(); + x; // Unreachable + } + else { + x; // number | undefined + if (x !== undefined) { + x; // number + fail(); + x; // Unreachable + } + else { + x; // undefined + fail(); + x; // Unreachable + } + x; // Unreachable + } + x; // Unreachable +} + +function f31(x: { a: string | number }) { + if (typeof x.a === "string") { + fail(); + x; // Unreachable + x.a; // Unreachable + } + x; // { a: string | number } + x.a; // number +} + +function f40(x: number) { + try { + x; + fail(); + x; // Unreachable + } + finally { + x; + fail(); + x; // Unreachable + } + x; // Unreachable +} + +function f41(x: number) { + try { + x; + } + finally { + x; + fail(); + x; // Unreachable + } + x; // Unreachable +} + +function f42(x: number) { + try { + x; + fail(); + x; // Unreachable + } + finally { + x; + } + x; // Unreachable +} + + +//// [neverReturningFunctions1.js] +"use strict"; +function fail(message) { + throw new Error(message); +} +function f01(x) { + if (x === undefined) + fail("undefined argument"); + x.length; // string +} +function f02(x) { + if (x >= 0) + return x; + fail("negative number"); + x; // Unreachable +} +function f03(x) { + x; // string + fail(); + x; // Unreachable +} +function f11(x, fail) { + if (x === undefined) + fail("undefined argument"); + x.length; // string +} +function f12(x, fail) { + if (x >= 0) + return x; + fail("negative number"); + x; // Unreachable +} +function f13(x, fail) { + x; // string + fail(); + x; // Unreachable +} +var Debug; +(function (Debug) { +})(Debug || (Debug = {})); +function f21(x) { + if (x === undefined) + Debug.fail("undefined argument"); + x.length; // string +} +function f22(x) { + if (x >= 0) + return x; + Debug.fail("negative number"); + x; // Unreachable +} +function f23(x) { + x; // string + Debug.fail(); + x; // Unreachable +} +function f24(x) { + x; // string + ((Debug).fail)(); + x; // Unreachable +} +var Test = /** @class */ (function () { + function Test() { + } + Test.prototype.fail = function (message) { + throw new Error(message); + }; + Test.prototype.f1 = function (x) { + if (x === undefined) + this.fail("undefined argument"); + x.length; // string + }; + Test.prototype.f2 = function (x) { + if (x >= 0) + return x; + this.fail("negative number"); + x; // Unreachable + }; + Test.prototype.f3 = function (x) { + x; // string + this.fail(); + x; // Unreachable + }; + return Test; +}()); +function f30(x) { + if (typeof x === "string") { + fail(); + x; // Unreachable + } + else { + x; // number | undefined + if (x !== undefined) { + x; // number + fail(); + x; // Unreachable + } + else { + x; // undefined + fail(); + x; // Unreachable + } + x; // Unreachable + } + x; // Unreachable +} +function f31(x) { + if (typeof x.a === "string") { + fail(); + x; // Unreachable + x.a; // Unreachable + } + x; // { a: string | number } + x.a; // number +} +function f40(x) { + try { + x; + fail(); + x; // Unreachable + } + finally { + x; + fail(); + x; // Unreachable + } + x; // Unreachable +} +function f41(x) { + try { + x; + } + finally { + x; + fail(); + x; // Unreachable + } + x; // Unreachable +} +function f42(x) { + try { + x; + fail(); + x; // Unreachable + } + finally { + x; + } + x; // Unreachable +} + + +//// [neverReturningFunctions1.d.ts] +declare function fail(message?: string): never; +declare function f01(x: string | undefined): void; +declare function f02(x: number): number; +declare function f03(x: string): void; +declare function f11(x: string | undefined, fail: (message?: string) => never): void; +declare function f12(x: number, fail: (message?: string) => never): number; +declare function f13(x: string, fail: (message?: string) => never): void; +declare namespace Debug { + function fail(message?: string): never; +} +declare function f21(x: string | undefined): void; +declare function f22(x: number): number; +declare function f23(x: string): void; +declare function f24(x: string): void; +declare class Test { + fail(message?: string): never; + f1(x: string | undefined): void; + f2(x: number): number; + f3(x: string): void; +} +declare function f30(x: string | number | undefined): void; +declare function f31(x: { + a: string | number; +}): void; +declare function f40(x: number): void; +declare function f41(x: number): void; +declare function f42(x: number): void; diff --git a/tests/baselines/reference/neverReturningFunctions1.symbols b/tests/baselines/reference/neverReturningFunctions1.symbols new file mode 100644 index 00000000000..7f1b5c489ac --- /dev/null +++ b/tests/baselines/reference/neverReturningFunctions1.symbols @@ -0,0 +1,387 @@ +=== tests/cases/conformance/controlFlow/neverReturningFunctions1.ts === +function fail(message?: string): never { +>fail : Symbol(fail, Decl(neverReturningFunctions1.ts, 0, 0)) +>message : Symbol(message, Decl(neverReturningFunctions1.ts, 0, 14)) + + throw new Error(message); +>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +>message : Symbol(message, Decl(neverReturningFunctions1.ts, 0, 14)) +} + +function f01(x: string | undefined) { +>f01 : Symbol(f01, Decl(neverReturningFunctions1.ts, 2, 1)) +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 4, 13)) + + if (x === undefined) fail("undefined argument"); +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 4, 13)) +>undefined : Symbol(undefined) +>fail : Symbol(fail, Decl(neverReturningFunctions1.ts, 0, 0)) + + x.length; // string +>x.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 4, 13)) +>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) +} + +function f02(x: number): number { +>f02 : Symbol(f02, Decl(neverReturningFunctions1.ts, 7, 1)) +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 9, 13)) + + if (x >= 0) return x; +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 9, 13)) +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 9, 13)) + + fail("negative number"); +>fail : Symbol(fail, Decl(neverReturningFunctions1.ts, 0, 0)) + + x; // Unreachable +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 9, 13)) +} + +function f03(x: string) { +>f03 : Symbol(f03, Decl(neverReturningFunctions1.ts, 13, 1)) +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 15, 13)) + + x; // string +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 15, 13)) + + fail(); +>fail : Symbol(fail, Decl(neverReturningFunctions1.ts, 0, 0)) + + x; // Unreachable +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 15, 13)) +} + +function f11(x: string | undefined, fail: (message?: string) => never) { +>f11 : Symbol(f11, Decl(neverReturningFunctions1.ts, 19, 1)) +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 21, 13)) +>fail : Symbol(fail, Decl(neverReturningFunctions1.ts, 21, 35)) +>message : Symbol(message, Decl(neverReturningFunctions1.ts, 21, 43)) + + if (x === undefined) fail("undefined argument"); +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 21, 13)) +>undefined : Symbol(undefined) +>fail : Symbol(fail, Decl(neverReturningFunctions1.ts, 21, 35)) + + x.length; // string +>x.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 21, 13)) +>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) +} + +function f12(x: number, fail: (message?: string) => never): number { +>f12 : Symbol(f12, Decl(neverReturningFunctions1.ts, 24, 1)) +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 26, 13)) +>fail : Symbol(fail, Decl(neverReturningFunctions1.ts, 26, 23)) +>message : Symbol(message, Decl(neverReturningFunctions1.ts, 26, 31)) + + if (x >= 0) return x; +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 26, 13)) +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 26, 13)) + + fail("negative number"); +>fail : Symbol(fail, Decl(neverReturningFunctions1.ts, 26, 23)) + + x; // Unreachable +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 26, 13)) +} + +function f13(x: string, fail: (message?: string) => never) { +>f13 : Symbol(f13, Decl(neverReturningFunctions1.ts, 30, 1)) +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 32, 13)) +>fail : Symbol(fail, Decl(neverReturningFunctions1.ts, 32, 23)) +>message : Symbol(message, Decl(neverReturningFunctions1.ts, 32, 31)) + + x; // string +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 32, 13)) + + fail(); +>fail : Symbol(fail, Decl(neverReturningFunctions1.ts, 32, 23)) + + x; // Unreachable +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 32, 13)) +} + +namespace Debug { +>Debug : Symbol(Debug, Decl(neverReturningFunctions1.ts, 36, 1)) + + export declare function fail(message?: string): never; +>fail : Symbol(fail, Decl(neverReturningFunctions1.ts, 38, 17)) +>message : Symbol(message, Decl(neverReturningFunctions1.ts, 39, 33)) +} + +function f21(x: string | undefined) { +>f21 : Symbol(f21, Decl(neverReturningFunctions1.ts, 40, 1)) +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 42, 13)) + + if (x === undefined) Debug.fail("undefined argument"); +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 42, 13)) +>undefined : Symbol(undefined) +>Debug.fail : Symbol(Debug.fail, Decl(neverReturningFunctions1.ts, 38, 17)) +>Debug : Symbol(Debug, Decl(neverReturningFunctions1.ts, 36, 1)) +>fail : Symbol(Debug.fail, Decl(neverReturningFunctions1.ts, 38, 17)) + + x.length; // string +>x.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 42, 13)) +>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) +} + +function f22(x: number): number { +>f22 : Symbol(f22, Decl(neverReturningFunctions1.ts, 45, 1)) +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 47, 13)) + + if (x >= 0) return x; +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 47, 13)) +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 47, 13)) + + Debug.fail("negative number"); +>Debug.fail : Symbol(Debug.fail, Decl(neverReturningFunctions1.ts, 38, 17)) +>Debug : Symbol(Debug, Decl(neverReturningFunctions1.ts, 36, 1)) +>fail : Symbol(Debug.fail, Decl(neverReturningFunctions1.ts, 38, 17)) + + x; // Unreachable +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 47, 13)) +} + +function f23(x: string) { +>f23 : Symbol(f23, Decl(neverReturningFunctions1.ts, 51, 1)) +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 53, 13)) + + x; // string +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 53, 13)) + + Debug.fail(); +>Debug.fail : Symbol(Debug.fail, Decl(neverReturningFunctions1.ts, 38, 17)) +>Debug : Symbol(Debug, Decl(neverReturningFunctions1.ts, 36, 1)) +>fail : Symbol(Debug.fail, Decl(neverReturningFunctions1.ts, 38, 17)) + + x; // Unreachable +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 53, 13)) +} + +function f24(x: string) { +>f24 : Symbol(f24, Decl(neverReturningFunctions1.ts, 57, 1)) +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 59, 13)) + + x; // string +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 59, 13)) + + ((Debug).fail)(); +>(Debug).fail : Symbol(Debug.fail, Decl(neverReturningFunctions1.ts, 38, 17)) +>Debug : Symbol(Debug, Decl(neverReturningFunctions1.ts, 36, 1)) +>fail : Symbol(Debug.fail, Decl(neverReturningFunctions1.ts, 38, 17)) + + x; // Unreachable +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 59, 13)) +} + +class Test { +>Test : Symbol(Test, Decl(neverReturningFunctions1.ts, 63, 1)) + + fail(message?: string): never { +>fail : Symbol(Test.fail, Decl(neverReturningFunctions1.ts, 65, 12)) +>message : Symbol(message, Decl(neverReturningFunctions1.ts, 66, 9)) + + throw new Error(message); +>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +>message : Symbol(message, Decl(neverReturningFunctions1.ts, 66, 9)) + } + f1(x: string | undefined) { +>f1 : Symbol(Test.f1, Decl(neverReturningFunctions1.ts, 68, 5)) +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 69, 7)) + + if (x === undefined) this.fail("undefined argument"); +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 69, 7)) +>undefined : Symbol(undefined) +>this.fail : Symbol(Test.fail, Decl(neverReturningFunctions1.ts, 65, 12)) +>this : Symbol(Test, Decl(neverReturningFunctions1.ts, 63, 1)) +>fail : Symbol(Test.fail, Decl(neverReturningFunctions1.ts, 65, 12)) + + x.length; // string +>x.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 69, 7)) +>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) + } + f2(x: number): number { +>f2 : Symbol(Test.f2, Decl(neverReturningFunctions1.ts, 72, 5)) +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 73, 7)) + + if (x >= 0) return x; +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 73, 7)) +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 73, 7)) + + this.fail("negative number"); +>this.fail : Symbol(Test.fail, Decl(neverReturningFunctions1.ts, 65, 12)) +>this : Symbol(Test, Decl(neverReturningFunctions1.ts, 63, 1)) +>fail : Symbol(Test.fail, Decl(neverReturningFunctions1.ts, 65, 12)) + + x; // Unreachable +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 73, 7)) + } + f3(x: string) { +>f3 : Symbol(Test.f3, Decl(neverReturningFunctions1.ts, 77, 5)) +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 78, 7)) + + x; // string +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 78, 7)) + + this.fail(); +>this.fail : Symbol(Test.fail, Decl(neverReturningFunctions1.ts, 65, 12)) +>this : Symbol(Test, Decl(neverReturningFunctions1.ts, 63, 1)) +>fail : Symbol(Test.fail, Decl(neverReturningFunctions1.ts, 65, 12)) + + x; // Unreachable +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 78, 7)) + } +} + +function f30(x: string | number | undefined) { +>f30 : Symbol(f30, Decl(neverReturningFunctions1.ts, 83, 1)) +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 85, 13)) + + if (typeof x === "string") { +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 85, 13)) + + fail(); +>fail : Symbol(fail, Decl(neverReturningFunctions1.ts, 0, 0)) + + x; // Unreachable +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 85, 13)) + } + else { + x; // number | undefined +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 85, 13)) + + if (x !== undefined) { +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 85, 13)) +>undefined : Symbol(undefined) + + x; // number +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 85, 13)) + + fail(); +>fail : Symbol(fail, Decl(neverReturningFunctions1.ts, 0, 0)) + + x; // Unreachable +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 85, 13)) + } + else { + x; // undefined +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 85, 13)) + + fail(); +>fail : Symbol(fail, Decl(neverReturningFunctions1.ts, 0, 0)) + + x; // Unreachable +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 85, 13)) + } + x; // Unreachable +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 85, 13)) + } + x; // Unreachable +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 85, 13)) +} + +function f31(x: { a: string | number }) { +>f31 : Symbol(f31, Decl(neverReturningFunctions1.ts, 105, 1)) +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 107, 13)) +>a : Symbol(a, Decl(neverReturningFunctions1.ts, 107, 17)) + + if (typeof x.a === "string") { +>x.a : Symbol(a, Decl(neverReturningFunctions1.ts, 107, 17)) +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 107, 13)) +>a : Symbol(a, Decl(neverReturningFunctions1.ts, 107, 17)) + + fail(); +>fail : Symbol(fail, Decl(neverReturningFunctions1.ts, 0, 0)) + + x; // Unreachable +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 107, 13)) + + x.a; // Unreachable +>x.a : Symbol(a, Decl(neverReturningFunctions1.ts, 107, 17)) +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 107, 13)) +>a : Symbol(a, Decl(neverReturningFunctions1.ts, 107, 17)) + } + x; // { a: string | number } +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 107, 13)) + + x.a; // number +>x.a : Symbol(a, Decl(neverReturningFunctions1.ts, 107, 17)) +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 107, 13)) +>a : Symbol(a, Decl(neverReturningFunctions1.ts, 107, 17)) +} + +function f40(x: number) { +>f40 : Symbol(f40, Decl(neverReturningFunctions1.ts, 115, 1)) +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 117, 13)) + + try { + x; +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 117, 13)) + + fail(); +>fail : Symbol(fail, Decl(neverReturningFunctions1.ts, 0, 0)) + + x; // Unreachable +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 117, 13)) + } + finally { + x; +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 117, 13)) + + fail(); +>fail : Symbol(fail, Decl(neverReturningFunctions1.ts, 0, 0)) + + x; // Unreachable +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 117, 13)) + } + x; // Unreachable +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 117, 13)) +} + +function f41(x: number) { +>f41 : Symbol(f41, Decl(neverReturningFunctions1.ts, 129, 1)) +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 131, 13)) + + try { + x; +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 131, 13)) + } + finally { + x; +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 131, 13)) + + fail(); +>fail : Symbol(fail, Decl(neverReturningFunctions1.ts, 0, 0)) + + x; // Unreachable +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 131, 13)) + } + x; // Unreachable +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 131, 13)) +} + +function f42(x: number) { +>f42 : Symbol(f42, Decl(neverReturningFunctions1.ts, 141, 1)) +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 143, 13)) + + try { + x; +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 143, 13)) + + fail(); +>fail : Symbol(fail, Decl(neverReturningFunctions1.ts, 0, 0)) + + x; // Unreachable +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 143, 13)) + } + finally { + x; +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 143, 13)) + } + x; // Unreachable +>x : Symbol(x, Decl(neverReturningFunctions1.ts, 143, 13)) +} + diff --git a/tests/baselines/reference/neverReturningFunctions1.types b/tests/baselines/reference/neverReturningFunctions1.types new file mode 100644 index 00000000000..e7d6afa36a5 --- /dev/null +++ b/tests/baselines/reference/neverReturningFunctions1.types @@ -0,0 +1,439 @@ +=== tests/cases/conformance/controlFlow/neverReturningFunctions1.ts === +function fail(message?: string): never { +>fail : (message?: string | undefined) => never +>message : string | undefined + + throw new Error(message); +>new Error(message) : Error +>Error : ErrorConstructor +>message : string | undefined +} + +function f01(x: string | undefined) { +>f01 : (x: string | undefined) => void +>x : string | undefined + + if (x === undefined) fail("undefined argument"); +>x === undefined : boolean +>x : string | undefined +>undefined : undefined +>fail("undefined argument") : never +>fail : (message?: string | undefined) => never +>"undefined argument" : "undefined argument" + + x.length; // string +>x.length : number +>x : string +>length : number +} + +function f02(x: number): number { +>f02 : (x: number) => number +>x : number + + if (x >= 0) return x; +>x >= 0 : boolean +>x : number +>0 : 0 +>x : number + + fail("negative number"); +>fail("negative number") : never +>fail : (message?: string | undefined) => never +>"negative number" : "negative number" + + x; // Unreachable +>x : number +} + +function f03(x: string) { +>f03 : (x: string) => void +>x : string + + x; // string +>x : string + + fail(); +>fail() : never +>fail : (message?: string | undefined) => never + + x; // Unreachable +>x : string +} + +function f11(x: string | undefined, fail: (message?: string) => never) { +>f11 : (x: string | undefined, fail: (message?: string | undefined) => never) => void +>x : string | undefined +>fail : (message?: string | undefined) => never +>message : string | undefined + + if (x === undefined) fail("undefined argument"); +>x === undefined : boolean +>x : string | undefined +>undefined : undefined +>fail("undefined argument") : never +>fail : (message?: string | undefined) => never +>"undefined argument" : "undefined argument" + + x.length; // string +>x.length : number +>x : string +>length : number +} + +function f12(x: number, fail: (message?: string) => never): number { +>f12 : (x: number, fail: (message?: string | undefined) => never) => number +>x : number +>fail : (message?: string | undefined) => never +>message : string | undefined + + if (x >= 0) return x; +>x >= 0 : boolean +>x : number +>0 : 0 +>x : number + + fail("negative number"); +>fail("negative number") : never +>fail : (message?: string | undefined) => never +>"negative number" : "negative number" + + x; // Unreachable +>x : number +} + +function f13(x: string, fail: (message?: string) => never) { +>f13 : (x: string, fail: (message?: string | undefined) => never) => void +>x : string +>fail : (message?: string | undefined) => never +>message : string | undefined + + x; // string +>x : string + + fail(); +>fail() : never +>fail : (message?: string | undefined) => never + + x; // Unreachable +>x : string +} + +namespace Debug { +>Debug : typeof Debug + + export declare function fail(message?: string): never; +>fail : (message?: string | undefined) => never +>message : string | undefined +} + +function f21(x: string | undefined) { +>f21 : (x: string | undefined) => void +>x : string | undefined + + if (x === undefined) Debug.fail("undefined argument"); +>x === undefined : boolean +>x : string | undefined +>undefined : undefined +>Debug.fail("undefined argument") : never +>Debug.fail : (message?: string | undefined) => never +>Debug : typeof Debug +>fail : (message?: string | undefined) => never +>"undefined argument" : "undefined argument" + + x.length; // string +>x.length : number +>x : string +>length : number +} + +function f22(x: number): number { +>f22 : (x: number) => number +>x : number + + if (x >= 0) return x; +>x >= 0 : boolean +>x : number +>0 : 0 +>x : number + + Debug.fail("negative number"); +>Debug.fail("negative number") : never +>Debug.fail : (message?: string | undefined) => never +>Debug : typeof Debug +>fail : (message?: string | undefined) => never +>"negative number" : "negative number" + + x; // Unreachable +>x : number +} + +function f23(x: string) { +>f23 : (x: string) => void +>x : string + + x; // string +>x : string + + Debug.fail(); +>Debug.fail() : never +>Debug.fail : (message?: string | undefined) => never +>Debug : typeof Debug +>fail : (message?: string | undefined) => never + + x; // Unreachable +>x : string +} + +function f24(x: string) { +>f24 : (x: string) => void +>x : string + + x; // string +>x : string + + ((Debug).fail)(); +>((Debug).fail)() : never +>((Debug).fail) : (message?: string | undefined) => never +>(Debug).fail : (message?: string | undefined) => never +>(Debug) : typeof Debug +>Debug : typeof Debug +>fail : (message?: string | undefined) => never + + x; // Unreachable +>x : string +} + +class Test { +>Test : Test + + fail(message?: string): never { +>fail : (message?: string | undefined) => never +>message : string | undefined + + throw new Error(message); +>new Error(message) : Error +>Error : ErrorConstructor +>message : string | undefined + } + f1(x: string | undefined) { +>f1 : (x: string | undefined) => void +>x : string | undefined + + if (x === undefined) this.fail("undefined argument"); +>x === undefined : boolean +>x : string | undefined +>undefined : undefined +>this.fail("undefined argument") : never +>this.fail : (message?: string | undefined) => never +>this : this +>fail : (message?: string | undefined) => never +>"undefined argument" : "undefined argument" + + x.length; // string +>x.length : number +>x : string +>length : number + } + f2(x: number): number { +>f2 : (x: number) => number +>x : number + + if (x >= 0) return x; +>x >= 0 : boolean +>x : number +>0 : 0 +>x : number + + this.fail("negative number"); +>this.fail("negative number") : never +>this.fail : (message?: string | undefined) => never +>this : this +>fail : (message?: string | undefined) => never +>"negative number" : "negative number" + + x; // Unreachable +>x : number + } + f3(x: string) { +>f3 : (x: string) => void +>x : string + + x; // string +>x : string + + this.fail(); +>this.fail() : never +>this.fail : (message?: string | undefined) => never +>this : this +>fail : (message?: string | undefined) => never + + x; // Unreachable +>x : string + } +} + +function f30(x: string | number | undefined) { +>f30 : (x: string | number | undefined) => void +>x : string | number | undefined + + if (typeof x === "string") { +>typeof x === "string" : boolean +>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" +>x : string | number | undefined +>"string" : "string" + + fail(); +>fail() : never +>fail : (message?: string | undefined) => never + + x; // Unreachable +>x : string | number | undefined + } + else { + x; // number | undefined +>x : number | undefined + + if (x !== undefined) { +>x !== undefined : boolean +>x : number | undefined +>undefined : undefined + + x; // number +>x : number + + fail(); +>fail() : never +>fail : (message?: string | undefined) => never + + x; // Unreachable +>x : string | number | undefined + } + else { + x; // undefined +>x : undefined + + fail(); +>fail() : never +>fail : (message?: string | undefined) => never + + x; // Unreachable +>x : string | number | undefined + } + x; // Unreachable +>x : string | number | undefined + } + x; // Unreachable +>x : string | number | undefined +} + +function f31(x: { a: string | number }) { +>f31 : (x: { a: string | number; }) => void +>x : { a: string | number; } +>a : string | number + + if (typeof x.a === "string") { +>typeof x.a === "string" : boolean +>typeof x.a : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" +>x.a : string | number +>x : { a: string | number; } +>a : string | number +>"string" : "string" + + fail(); +>fail() : never +>fail : (message?: string | undefined) => never + + x; // Unreachable +>x : { a: string | number; } + + x.a; // Unreachable +>x.a : string | number +>x : { a: string | number; } +>a : string | number + } + x; // { a: string | number } +>x : { a: string | number; } + + x.a; // number +>x.a : number +>x : { a: string | number; } +>a : number +} + +function f40(x: number) { +>f40 : (x: number) => void +>x : number + + try { + x; +>x : number + + fail(); +>fail() : never +>fail : (message?: string | undefined) => never + + x; // Unreachable +>x : number + } + finally { + x; +>x : number + + fail(); +>fail() : never +>fail : (message?: string | undefined) => never + + x; // Unreachable +>x : number + } + x; // Unreachable +>x : number +} + +function f41(x: number) { +>f41 : (x: number) => void +>x : number + + try { + x; +>x : number + } + finally { + x; +>x : number + + fail(); +>fail() : never +>fail : (message?: string | undefined) => never + + x; // Unreachable +>x : number + } + x; // Unreachable +>x : number +} + +function f42(x: number) { +>f42 : (x: number) => void +>x : number + + try { + x; +>x : number + + fail(); +>fail() : never +>fail : (message?: string | undefined) => never + + x; // Unreachable +>x : number + } + finally { + x; +>x : number + } + x; // Unreachable +>x : number +} + From bcdf33d8de066b827e8251875675b7a0084446c5 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Fri, 20 Sep 2019 17:44:19 -0700 Subject: [PATCH 83/97] Fix forEachChild --- src/compiler/parser.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 9852ee8feb7..edaa7ebeaeb 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -165,7 +165,8 @@ namespace ts { return visitNode(cbNode, (node).typeName) || visitNodes(cbNode, cbNodes, (node).typeArguments); case SyntaxKind.TypePredicate: - return visitNode(cbNode, (node).parameterName) || + return visitNode(cbNode, (node).assertsModifier) || + visitNode(cbNode, (node).parameterName) || visitNode(cbNode, (node).type); case SyntaxKind.TypeQuery: return visitNode(cbNode, (node).exprName); From 0da541528e89f5483a44fc0b200d9b537d93a0b6 Mon Sep 17 00:00:00 2001 From: Orta Therox Date: Mon, 23 Sep 2019 14:08:43 -0400 Subject: [PATCH 84/97] Remove errors from the gulpfile --- Gulpfile.js | 2 ++ scripts/types/ambient.d.ts | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Gulpfile.js b/Gulpfile.js index 51127f9822d..1f930e3b019 100644 --- a/Gulpfile.js +++ b/Gulpfile.js @@ -333,6 +333,8 @@ task("run-eslint-rules-tests").description = "Runs the eslint rule tests"; const lintFoldStart = async () => { if (fold.isTravis()) console.log(fold.start("lint")); }; const lintFoldEnd = async () => { if (fold.isTravis()) console.log(fold.end("lint")); }; + +/** @type { (folder: string) => { (): Promise; displayName?: string } } */ const eslint = (folder) => async () => { const ESLINTRC_CI = ".eslintrc.ci.json"; const isCIEnv = cmdLineOptions.ci || process.env.CI === "true"; diff --git a/scripts/types/ambient.d.ts b/scripts/types/ambient.d.ts index 9ea70adee50..d48de7c05d7 100644 --- a/scripts/types/ambient.d.ts +++ b/scripts/types/ambient.d.ts @@ -76,10 +76,15 @@ declare module "undertaker" { interface TaskFunctionParams { flags?: Record; } + interface TaskFunctionWrapped { + description: string + flags: { [name: string]: string } + } } declare module "gulp-sourcemaps" { interface WriteOptions { destPath?: string; } -} \ No newline at end of file + +} From 86c7d84457290d98e71a4d5b6f50623bc3221bde Mon Sep 17 00:00:00 2001 From: Nathan Fenner Date: Mon, 23 Sep 2019 12:02:13 -0700 Subject: [PATCH 85/97] update missing baselines --- tests/baselines/reference/checkJsxChildrenProperty15.errors.txt | 2 +- tests/baselines/reference/tsxUnionElementType4.errors.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/baselines/reference/checkJsxChildrenProperty15.errors.txt b/tests/baselines/reference/checkJsxChildrenProperty15.errors.txt index fa034be0f18..d0be275765f 100644 --- a/tests/baselines/reference/checkJsxChildrenProperty15.errors.txt +++ b/tests/baselines/reference/checkJsxChildrenProperty15.errors.txt @@ -17,7 +17,7 @@ tests/cases/conformance/jsx/file.tsx(12,13): error TS2322: Type '{ children: Ele // Not OK (excess children) const k3 = } />; - ~~~~~~~~~~~~~~~~~~~~~~ + ~~~~~~~~ !!! error TS2322: Type '{ children: Element; }' is not assignable to type 'IntrinsicAttributes'. !!! error TS2322: Property 'children' does not exist on type 'IntrinsicAttributes'. const k4 =
; diff --git a/tests/baselines/reference/tsxUnionElementType4.errors.txt b/tests/baselines/reference/tsxUnionElementType4.errors.txt index 2048b100c11..40463bcbb79 100644 --- a/tests/baselines/reference/tsxUnionElementType4.errors.txt +++ b/tests/baselines/reference/tsxUnionElementType4.errors.txt @@ -42,7 +42,7 @@ tests/cases/conformance/jsx/file.tsx(34,22): error TS2322: Type '{ prop: true; } !!! error TS2322: Type 'true' is not assignable to type 'never'. !!! related TS6500 tests/cases/conformance/jsx/file.tsx:3:36: The expected type comes from property 'x' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes & { x: number; } & { children?: ReactNode; } & { x: string; } & { children?: ReactNode; }' let b = - ~~~~~~ + ~ !!! error TS2322: Type '{ x: number; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes & { children?: ReactNode; }'. !!! error TS2322: Property 'x' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes & { children?: ReactNode; }'. let c = ; From 432da939c1d7d9f82e21b89224960fea34b44f63 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Mon, 23 Sep 2019 13:54:12 -0700 Subject: [PATCH 86/97] Add doc comments for fileExists and directoryExists implementation --- src/server/project.ts | 10 ++++++++++ tests/baselines/reference/api/tsserverlibrary.d.ts | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/server/project.ts b/src/server/project.ts index 080d3ee54cc..418532bdc45 100644 --- a/src/server/project.ts +++ b/src/server/project.ts @@ -1549,6 +1549,11 @@ namespace ts.server { useSourceOfProjectReferenceRedirect = () => !!this.languageServiceEnabled && !this.getCompilerOptions().disableSourceOfProjectReferenceRedirect; + /** + * This implementation of fileExists checks if the file being requested is + * .d.ts file for the referenced Project. + * If it is it returns true irrespective of whether that file exists on host + */ fileExists(file: string): boolean { // Project references go to source file instead of .d.ts file if (this.useSourceOfProjectReferenceRedirect() && this.projectReferenceCallbacks) { @@ -1558,6 +1563,11 @@ namespace ts.server { return super.fileExists(file); } + /** + * This implementation of directoryExists checks if the directory being requested is + * directory of .d.ts file for the referenced Project. + * If it is it returns true irrespective of whether that directory exists on host + */ directoryExists(path: string): boolean { if (super.directoryExists(path)) return true; if (!this.useSourceOfProjectReferenceRedirect() || !this.projectReferenceCallbacks) return false; diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 1a3e9baf942..d97f62d0ba8 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -8576,7 +8576,17 @@ declare namespace ts.server { private projectErrors; private projectReferences; protected isInitialLoadPending: () => boolean; + /** + * This implementation of fileExists checks if the file being requested is + * .d.ts file for the referenced Project. + * If it is it returns true irrespective of whether that file exists on host + */ fileExists(file: string): boolean; + /** + * This implementation of directoryExists checks if the directory being requested is + * directory of .d.ts file for the referenced Project. + * If it is it returns true irrespective of whether that directory exists on host + */ directoryExists(path: string): boolean; /** * If the project has reload from disk pending, it reloads (and then updates graph as part of that) instead of just updating the graph From f9ca8ba6f863e2de07b6bcf224ecb8460fc17016 Mon Sep 17 00:00:00 2001 From: TypeScript Bot Date: Mon, 23 Sep 2019 14:09:51 -0700 Subject: [PATCH 87/97] Update user baselines (#33557) --- tests/baselines/reference/docker/office-ui-fabric.log | 2 +- tests/baselines/reference/user/prettier.log | 4 +--- tests/baselines/reference/user/webpack.log | 10 ---------- 3 files changed, 2 insertions(+), 14 deletions(-) delete mode 100644 tests/baselines/reference/user/webpack.log diff --git a/tests/baselines/reference/docker/office-ui-fabric.log b/tests/baselines/reference/docker/office-ui-fabric.log index 7bd402b8327..9cda725e39e 100644 --- a/tests/baselines/reference/docker/office-ui-fabric.log +++ b/tests/baselines/reference/docker/office-ui-fabric.log @@ -134,9 +134,9 @@ Standard output: @uifabric/utilities: PASS src/warn/warnControlledUsage.test.ts @uifabric/utilities: PASS src/focus.test.tsx @uifabric/utilities: PASS src/styled.test.tsx +@uifabric/utilities: PASS src/customizations/Customizer.test.tsx @uifabric/utilities: PASS src/EventGroup.test.ts @uifabric/utilities: PASS src/array.test.ts -@uifabric/utilities: PASS src/customizations/Customizer.test.tsx @uifabric/utilities: PASS src/math.test.ts @uifabric/utilities: PASS src/warn/warn.test.ts @uifabric/utilities: PASS src/dom/dom.test.ts diff --git a/tests/baselines/reference/user/prettier.log b/tests/baselines/reference/user/prettier.log index 93ccd99a0d6..635c0bc0b36 100644 --- a/tests/baselines/reference/user/prettier.log +++ b/tests/baselines/reference/user/prettier.log @@ -169,7 +169,7 @@ src/language-html/syntax-vue.js(14,27): error TS2339: Property 'right' does not src/language-html/utils.js(10,30): error TS2307: Cannot find module 'html-tag-names'. src/language-html/utils.js(11,39): error TS2307: Cannot find module 'html-element-attributes'. src/language-html/utils.js(444,17): error TS2554: Expected 0 arguments, but got 1. -src/language-js/comments.js(864,64): error TS2554: Expected 0 arguments, but got 1. +src/language-js/comments.js(865,64): error TS2554: Expected 0 arguments, but got 1. src/language-js/index.js(9,26): error TS2307: Cannot find module 'linguist-languages/data/JavaScript'. src/language-js/index.js(9,65): error TS2345: Argument of type '{ override: { since: string; parsers: string[]; vscodeLanguageIds: string[]; }; extend: { interpreters: string[]; }; }' is not assignable to parameter of type '{ extend: any; override: any; exclude: any; }'. Property 'exclude' is missing in type '{ override: { since: string; parsers: string[]; vscodeLanguageIds: string[]; }; extend: { interpreters: string[]; }; }' but required in type '{ extend: any; override: any; exclude: any; }'. @@ -335,8 +335,6 @@ src/main/options-normalizer.js(36,35): error TS2339: Property 'blue' does not ex src/main/options-normalizer.js(54,5): error TS2525: Initializer provides no value for this binding element and the binding element has no default value. src/main/options-normalizer.js(74,16): error TS2341: Property '_hasDeprecationWarned' is private and only accessible within class 'Normalizer'. src/main/options-normalizer.js(80,39): error TS2341: Property '_hasDeprecationWarned' is private and only accessible within class 'Normalizer'. -src/main/options-normalizer.js(90,44): error TS2345: Argument of type '{ name: string; }' is not assignable to parameter of type 'SchemaHandlers'. - Object literal may only specify known properties, and 'name' does not exist in type 'SchemaHandlers'. src/main/options-normalizer.js(99,11): error TS2345: Argument of type '{ name: any; sourceName: any; }' is not assignable to parameter of type 'SchemaHandlers'. Object literal may only specify known properties, and 'name' does not exist in type 'SchemaHandlers'. src/main/options-normalizer.js(143,13): error TS2769: No overload matches this call. diff --git a/tests/baselines/reference/user/webpack.log b/tests/baselines/reference/user/webpack.log deleted file mode 100644 index d1fbc9df903..00000000000 --- a/tests/baselines/reference/user/webpack.log +++ /dev/null @@ -1,10 +0,0 @@ -Exit Code: 1 -Standard output: -lib/Compilation.js(575,5): error TS2322: Type 'number | "toString" | "valueOf" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | ... 29 more ... | "trimRight"' is not assignable to type 'string'. - Type 'number' is not assignable to type 'string'. -lib/Compiler.js(228,48): error TS2345: Argument of type 'number | "toString" | "valueOf" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | ... 29 more ... | "trimRight"' is not assignable to parameter of type 'string'. - Type 'number' is not assignable to type 'string'. - - - -Standard error: From 84e857b6f3624fcb01db807affa49ca8d9580f3b Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Mon, 23 Sep 2019 15:57:32 -0700 Subject: [PATCH 88/97] use forEachEntry --- src/services/codefixes/inferFromUsage.ts | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/services/codefixes/inferFromUsage.ts b/src/services/codefixes/inferFromUsage.ts index 090c241b05d..5dd34782885 100644 --- a/src/services/codefixes/inferFromUsage.ts +++ b/src/services/codefixes/inferFromUsage.ts @@ -947,22 +947,19 @@ namespace ts.codefix { function allPropertiesAreAssignableToUsage(type: Type, usage: Usage) { if (!usage.properties) return false; - let result = true; - usage.properties.forEach((propUsage, name) => { + return !forEachEntry(usage.properties, (propUsage, name) => { const source = checker.getTypeOfPropertyOfType(type, name as string); if (!source) { - result = false; - return; + return true; } if (propUsage.calls) { const sigs = checker.getSignaturesOfType(source, SignatureKind.Call); - result = result && !!sigs.length && checker.isTypeAssignableTo(source, getFunctionFromCalls(propUsage.calls)); + return !sigs.length || !checker.isTypeAssignableTo(source, getFunctionFromCalls(propUsage.calls)); } else { - result = result && checker.isTypeAssignableTo(source, combineFromUsage(propUsage)); + return !checker.isTypeAssignableTo(source, combineFromUsage(propUsage)); } }); - return result; } /** From 6c2ae12559508b594f7038f8d6165b92ec89c58c Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Mon, 23 Sep 2019 15:58:03 -0700 Subject: [PATCH 89/97] Relax the constraints of isValidBaseType to allow base types to be constructor types (#33146) * Relax the constraints of isValidBaseType to allow base types to be constructor types * Fix nit * Reduce confusion between isConstructorType and isValidBaseType * Update comment --- src/compiler/checker.ts | 19 +++-- src/compiler/types.ts | 2 +- .../reference/api/tsserverlibrary.d.ts | 2 +- tests/baselines/reference/api/typescript.d.ts | 2 +- .../baseConstraintOfDecorator.errors.txt | 17 +--- .../baseConstraintOfDecorator.symbols | 1 + .../reference/baseConstraintOfDecorator.types | 8 +- .../mixinIntersectionIsValidbaseType.js | 77 +++++++++++++++++++ .../mixinIntersectionIsValidbaseType.symbols | 74 ++++++++++++++++++ .../mixinIntersectionIsValidbaseType.types | 67 ++++++++++++++++ .../mixinIntersectionIsValidbaseType.ts | 27 +++++++ 11 files changed, 271 insertions(+), 25 deletions(-) create mode 100644 tests/baselines/reference/mixinIntersectionIsValidbaseType.js create mode 100644 tests/baselines/reference/mixinIntersectionIsValidbaseType.symbols create mode 100644 tests/baselines/reference/mixinIntersectionIsValidbaseType.types create mode 100644 tests/cases/compiler/mixinIntersectionIsValidbaseType.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index a3be2cfce43..e29f46f2dec 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -5985,7 +5985,9 @@ namespace ts { function getBaseTypeVariableOfClass(symbol: Symbol) { const baseConstructorType = getBaseConstructorTypeOfClass(getDeclaredTypeOfClassOrInterface(symbol)); - return baseConstructorType.flags & TypeFlags.TypeVariable ? baseConstructorType : undefined; + return baseConstructorType.flags & TypeFlags.TypeVariable ? baseConstructorType : + baseConstructorType.flags & TypeFlags.Intersection ? find((baseConstructorType as IntersectionType).types, t => !!(t.flags & TypeFlags.TypeVariable)) : + undefined; } function getTypeOfFuncClassEnumModule(symbol: Symbol): Type { @@ -6263,12 +6265,12 @@ namespace ts { } function isConstructorType(type: Type): boolean { - if (isValidBaseType(type) && getSignaturesOfType(type, SignatureKind.Construct).length > 0) { + if (getSignaturesOfType(type, SignatureKind.Construct).length > 0) { return true; } if (type.flags & TypeFlags.TypeVariable) { const constraint = getBaseConstraintOfType(type); - return !!constraint && isValidBaseType(constraint) && isMixinConstructorType(constraint); + return !!constraint && isMixinConstructorType(constraint); } return false; } @@ -6429,9 +6431,16 @@ namespace ts { return true; } - // A valid base type is `any`, any non-generic object type or intersection of non-generic - // object types. + // A valid base type is `any`, an object type or intersection of object types. function isValidBaseType(type: Type): type is BaseType { + if (type.flags & TypeFlags.TypeParameter) { + const constraint = getBaseConstraintOfType(type); + if (constraint) { + return isValidBaseType(constraint); + } + } + // TODO: Given that we allow type parmeters here now, is this `!isGenericMappedType(type)` check really needed? + // There's no reason a `T` should be allowed while a `Readonly` should not. return !!(type.flags & (TypeFlags.Object | TypeFlags.NonPrimitive | TypeFlags.Any)) && !isGenericMappedType(type) || !!(type.flags & TypeFlags.Intersection) && every((type).types, isValidBaseType); } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 1828d056405..e20518ed119 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -4202,7 +4202,7 @@ namespace ts { } // Object type or intersection of object types - export type BaseType = ObjectType | IntersectionType; + export type BaseType = ObjectType | IntersectionType | TypeVariable; // Also `any` and `object` export interface InterfaceTypeWithDeclaredMembers extends InterfaceType { declaredProperties: Symbol[]; // Declared members diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 79450ca83c9..c87d62b1139 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -2340,7 +2340,7 @@ declare namespace ts { localTypeParameters: TypeParameter[] | undefined; thisType: TypeParameter | undefined; } - export type BaseType = ObjectType | IntersectionType; + export type BaseType = ObjectType | IntersectionType | TypeVariable; export interface InterfaceTypeWithDeclaredMembers extends InterfaceType { declaredProperties: Symbol[]; declaredCallSignatures: Signature[]; diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index f0ae718d570..9c509d34a95 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -2340,7 +2340,7 @@ declare namespace ts { localTypeParameters: TypeParameter[] | undefined; thisType: TypeParameter | undefined; } - export type BaseType = ObjectType | IntersectionType; + export type BaseType = ObjectType | IntersectionType | TypeVariable; export interface InterfaceTypeWithDeclaredMembers extends InterfaceType { declaredProperties: Symbol[]; declaredCallSignatures: Signature[]; diff --git a/tests/baselines/reference/baseConstraintOfDecorator.errors.txt b/tests/baselines/reference/baseConstraintOfDecorator.errors.txt index 8317e5c55c6..8aad9f6fead 100644 --- a/tests/baselines/reference/baseConstraintOfDecorator.errors.txt +++ b/tests/baselines/reference/baseConstraintOfDecorator.errors.txt @@ -1,11 +1,10 @@ tests/cases/compiler/baseConstraintOfDecorator.ts(2,5): error TS2322: Type 'typeof decoratorFunc' is not assignable to type 'TFunction'. 'typeof decoratorFunc' is assignable to the constraint of type 'TFunction', but 'TFunction' could be instantiated with a different subtype of constraint '{}'. tests/cases/compiler/baseConstraintOfDecorator.ts(2,40): error TS2507: Type 'TFunction' is not a constructor function type. -tests/cases/compiler/baseConstraintOfDecorator.ts(12,5): error TS2322: Type 'typeof decoratorFunc' is not assignable to type 'TFunction'. -tests/cases/compiler/baseConstraintOfDecorator.ts(12,40): error TS2507: Type 'TFunction' is not a constructor function type. +tests/cases/compiler/baseConstraintOfDecorator.ts(12,18): error TS2545: A mixin class must have a constructor with a single rest parameter of type 'any[]'. -==== tests/cases/compiler/baseConstraintOfDecorator.ts (4 errors) ==== +==== tests/cases/compiler/baseConstraintOfDecorator.ts (3 errors) ==== export function classExtender(superClass: TFunction, _instanceModifier: (instance: any, args: any[]) => void): TFunction { return class decoratorFunc extends superClass { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -29,20 +28,12 @@ tests/cases/compiler/baseConstraintOfDecorator.ts(12,40): error TS2507: Type 'TF class MyClass { private x; } export function classExtender2 MyClass>(superClass: TFunction, _instanceModifier: (instance: any, args: any[]) => void): TFunction { return class decoratorFunc extends superClass { - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - ~~~~~~~~~~ -!!! error TS2507: Type 'TFunction' is not a constructor function type. -!!! related TS2735 tests/cases/compiler/baseConstraintOfDecorator.ts:11:32: Did you mean for 'TFunction' to be constrained to type 'new (...args: any[]) => MyClass'? + ~~~~~~~~~~~~~ +!!! error TS2545: A mixin class must have a constructor with a single rest parameter of type 'any[]'. constructor(...args: any[]) { - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ super(...args); - ~~~~~~~~~~~~~~~~~~~~~~~~~~~ _instanceModifier(this, args); - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ } - ~~~~~~~~~ }; - ~~~~~~ -!!! error TS2322: Type 'typeof decoratorFunc' is not assignable to type 'TFunction'. } \ No newline at end of file diff --git a/tests/baselines/reference/baseConstraintOfDecorator.symbols b/tests/baselines/reference/baseConstraintOfDecorator.symbols index 9048d7ed568..0a1dad239af 100644 --- a/tests/baselines/reference/baseConstraintOfDecorator.symbols +++ b/tests/baselines/reference/baseConstraintOfDecorator.symbols @@ -51,6 +51,7 @@ export function classExtender2 MyCl >args : Symbol(args, Decl(baseConstraintOfDecorator.ts, 12, 20)) super(...args); +>super : Symbol(TFunction, Decl(baseConstraintOfDecorator.ts, 10, 31)) >args : Symbol(args, Decl(baseConstraintOfDecorator.ts, 12, 20)) _instanceModifier(this, args); diff --git a/tests/baselines/reference/baseConstraintOfDecorator.types b/tests/baselines/reference/baseConstraintOfDecorator.types index a5a277c5d2a..78c77ee78a2 100644 --- a/tests/baselines/reference/baseConstraintOfDecorator.types +++ b/tests/baselines/reference/baseConstraintOfDecorator.types @@ -42,16 +42,16 @@ export function classExtender2 MyCl >args : any[] return class decoratorFunc extends superClass { ->class decoratorFunc extends superClass { constructor(...args: any[]) { super(...args); _instanceModifier(this, args); } } : typeof decoratorFunc ->decoratorFunc : typeof decoratorFunc ->superClass : TFunction +>class decoratorFunc extends superClass { constructor(...args: any[]) { super(...args); _instanceModifier(this, args); } } : { new (...args: any[]): decoratorFunc; prototype: classExtender2.decoratorFunc; } & TFunction +>decoratorFunc : { new (...args: any[]): decoratorFunc; prototype: classExtender2.decoratorFunc; } & TFunction +>superClass : MyClass constructor(...args: any[]) { >args : any[] super(...args); >super(...args) : void ->super : any +>super : TFunction >...args : any >args : any[] diff --git a/tests/baselines/reference/mixinIntersectionIsValidbaseType.js b/tests/baselines/reference/mixinIntersectionIsValidbaseType.js new file mode 100644 index 00000000000..ed02aecd53f --- /dev/null +++ b/tests/baselines/reference/mixinIntersectionIsValidbaseType.js @@ -0,0 +1,77 @@ +//// [mixinIntersectionIsValidbaseType.ts] +export type Constructor = new (...args: any[]) => T; + +export interface Initable { + init(...args: any[]): void; +} + +/** + * Plain mixin where the superclass must be Initable + */ +export const Serializable = & Initable>( + SuperClass: K +) => { + const LocalMixin = (InnerSuperClass: K) => { + return class SerializableLocal extends InnerSuperClass { + } + }; + let ResultClass = LocalMixin(SuperClass); + return ResultClass; +}; + +const AMixin = & Initable>(SuperClass: K) => { + let SomeHowOkay = class A extends SuperClass { + }; + + let SomeHowNotOkay = class A extends Serializable(SuperClass) { + }; +}; + +//// [mixinIntersectionIsValidbaseType.js] +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +exports.__esModule = true; +/** + * Plain mixin where the superclass must be Initable + */ +exports.Serializable = function (SuperClass) { + var LocalMixin = function (InnerSuperClass) { + return /** @class */ (function (_super) { + __extends(SerializableLocal, _super); + function SerializableLocal() { + return _super !== null && _super.apply(this, arguments) || this; + } + return SerializableLocal; + }(InnerSuperClass)); + }; + var ResultClass = LocalMixin(SuperClass); + return ResultClass; +}; +var AMixin = function (SuperClass) { + var SomeHowOkay = /** @class */ (function (_super) { + __extends(A, _super); + function A() { + return _super !== null && _super.apply(this, arguments) || this; + } + return A; + }(SuperClass)); + var SomeHowNotOkay = /** @class */ (function (_super) { + __extends(A, _super); + function A() { + return _super !== null && _super.apply(this, arguments) || this; + } + return A; + }(exports.Serializable(SuperClass))); +}; diff --git a/tests/baselines/reference/mixinIntersectionIsValidbaseType.symbols b/tests/baselines/reference/mixinIntersectionIsValidbaseType.symbols new file mode 100644 index 00000000000..69fe63bab23 --- /dev/null +++ b/tests/baselines/reference/mixinIntersectionIsValidbaseType.symbols @@ -0,0 +1,74 @@ +=== tests/cases/compiler/mixinIntersectionIsValidbaseType.ts === +export type Constructor = new (...args: any[]) => T; +>Constructor : Symbol(Constructor, Decl(mixinIntersectionIsValidbaseType.ts, 0, 0)) +>T : Symbol(T, Decl(mixinIntersectionIsValidbaseType.ts, 0, 24)) +>args : Symbol(args, Decl(mixinIntersectionIsValidbaseType.ts, 0, 58)) +>T : Symbol(T, Decl(mixinIntersectionIsValidbaseType.ts, 0, 24)) + +export interface Initable { +>Initable : Symbol(Initable, Decl(mixinIntersectionIsValidbaseType.ts, 0, 79)) + + init(...args: any[]): void; +>init : Symbol(Initable.init, Decl(mixinIntersectionIsValidbaseType.ts, 2, 27)) +>args : Symbol(args, Decl(mixinIntersectionIsValidbaseType.ts, 3, 9)) +} + +/** + * Plain mixin where the superclass must be Initable + */ +export const Serializable = & Initable>( +>Serializable : Symbol(Serializable, Decl(mixinIntersectionIsValidbaseType.ts, 9, 12)) +>K : Symbol(K, Decl(mixinIntersectionIsValidbaseType.ts, 9, 29)) +>Constructor : Symbol(Constructor, Decl(mixinIntersectionIsValidbaseType.ts, 0, 0)) +>Initable : Symbol(Initable, Decl(mixinIntersectionIsValidbaseType.ts, 0, 79)) +>Initable : Symbol(Initable, Decl(mixinIntersectionIsValidbaseType.ts, 0, 79)) + + SuperClass: K +>SuperClass : Symbol(SuperClass, Decl(mixinIntersectionIsValidbaseType.ts, 9, 73)) +>K : Symbol(K, Decl(mixinIntersectionIsValidbaseType.ts, 9, 29)) + +) => { + const LocalMixin = (InnerSuperClass: K) => { +>LocalMixin : Symbol(LocalMixin, Decl(mixinIntersectionIsValidbaseType.ts, 12, 9)) +>InnerSuperClass : Symbol(InnerSuperClass, Decl(mixinIntersectionIsValidbaseType.ts, 12, 24)) +>K : Symbol(K, Decl(mixinIntersectionIsValidbaseType.ts, 9, 29)) + + return class SerializableLocal extends InnerSuperClass { +>SerializableLocal : Symbol(SerializableLocal, Decl(mixinIntersectionIsValidbaseType.ts, 13, 14)) +>InnerSuperClass : Symbol(InnerSuperClass, Decl(mixinIntersectionIsValidbaseType.ts, 12, 24)) + } + }; + let ResultClass = LocalMixin(SuperClass); +>ResultClass : Symbol(ResultClass, Decl(mixinIntersectionIsValidbaseType.ts, 16, 7)) +>LocalMixin : Symbol(LocalMixin, Decl(mixinIntersectionIsValidbaseType.ts, 12, 9)) +>SuperClass : Symbol(SuperClass, Decl(mixinIntersectionIsValidbaseType.ts, 9, 73)) + + return ResultClass; +>ResultClass : Symbol(ResultClass, Decl(mixinIntersectionIsValidbaseType.ts, 16, 7)) + +}; + +const AMixin = & Initable>(SuperClass: K) => { +>AMixin : Symbol(AMixin, Decl(mixinIntersectionIsValidbaseType.ts, 20, 5)) +>K : Symbol(K, Decl(mixinIntersectionIsValidbaseType.ts, 20, 16)) +>Constructor : Symbol(Constructor, Decl(mixinIntersectionIsValidbaseType.ts, 0, 0)) +>Initable : Symbol(Initable, Decl(mixinIntersectionIsValidbaseType.ts, 0, 79)) +>Initable : Symbol(Initable, Decl(mixinIntersectionIsValidbaseType.ts, 0, 79)) +>SuperClass : Symbol(SuperClass, Decl(mixinIntersectionIsValidbaseType.ts, 20, 60)) +>K : Symbol(K, Decl(mixinIntersectionIsValidbaseType.ts, 20, 16)) + + let SomeHowOkay = class A extends SuperClass { +>SomeHowOkay : Symbol(SomeHowOkay, Decl(mixinIntersectionIsValidbaseType.ts, 21, 7)) +>A : Symbol(A, Decl(mixinIntersectionIsValidbaseType.ts, 21, 21)) +>SuperClass : Symbol(SuperClass, Decl(mixinIntersectionIsValidbaseType.ts, 20, 60)) + + }; + + let SomeHowNotOkay = class A extends Serializable(SuperClass) { +>SomeHowNotOkay : Symbol(SomeHowNotOkay, Decl(mixinIntersectionIsValidbaseType.ts, 24, 7)) +>A : Symbol(A, Decl(mixinIntersectionIsValidbaseType.ts, 24, 24)) +>Serializable : Symbol(Serializable, Decl(mixinIntersectionIsValidbaseType.ts, 9, 12)) +>SuperClass : Symbol(SuperClass, Decl(mixinIntersectionIsValidbaseType.ts, 20, 60)) + + }; +}; diff --git a/tests/baselines/reference/mixinIntersectionIsValidbaseType.types b/tests/baselines/reference/mixinIntersectionIsValidbaseType.types new file mode 100644 index 00000000000..c828188dcd6 --- /dev/null +++ b/tests/baselines/reference/mixinIntersectionIsValidbaseType.types @@ -0,0 +1,67 @@ +=== tests/cases/compiler/mixinIntersectionIsValidbaseType.ts === +export type Constructor = new (...args: any[]) => T; +>Constructor : Constructor +>args : any[] + +export interface Initable { + init(...args: any[]): void; +>init : (...args: any[]) => void +>args : any[] +} + +/** + * Plain mixin where the superclass must be Initable + */ +export const Serializable = & Initable>( +>Serializable : & Initable>(SuperClass: K) => { new (...args: any[]): SerializableLocal; prototype: Serializable.SerializableLocal; init(...args: any[]): void; } & K +> & Initable>( SuperClass: K) => { const LocalMixin = (InnerSuperClass: K) => { return class SerializableLocal extends InnerSuperClass { } }; let ResultClass = LocalMixin(SuperClass); return ResultClass;} : & Initable>(SuperClass: K) => { new (...args: any[]): SerializableLocal; prototype: Serializable.SerializableLocal; init(...args: any[]): void; } & K + + SuperClass: K +>SuperClass : K + +) => { + const LocalMixin = (InnerSuperClass: K) => { +>LocalMixin : (InnerSuperClass: K) => { new (...args: any[]): SerializableLocal; prototype: Serializable.SerializableLocal; init(...args: any[]): void; } & K +>(InnerSuperClass: K) => { return class SerializableLocal extends InnerSuperClass { } } : (InnerSuperClass: K) => { new (...args: any[]): SerializableLocal; prototype: Serializable.SerializableLocal; init(...args: any[]): void; } & K +>InnerSuperClass : K + + return class SerializableLocal extends InnerSuperClass { +>class SerializableLocal extends InnerSuperClass { } : { new (...args: any[]): SerializableLocal; prototype: Serializable.SerializableLocal; init(...args: any[]): void; } & K +>SerializableLocal : { new (...args: any[]): SerializableLocal; prototype: Serializable.SerializableLocal; init(...args: any[]): void; } & K +>InnerSuperClass : Initable + } + }; + let ResultClass = LocalMixin(SuperClass); +>ResultClass : { new (...args: any[]): SerializableLocal; prototype: Serializable.SerializableLocal; init(...args: any[]): void; } & K +>LocalMixin(SuperClass) : { new (...args: any[]): SerializableLocal; prototype: Serializable.SerializableLocal; init(...args: any[]): void; } & K +>LocalMixin : (InnerSuperClass: K) => { new (...args: any[]): SerializableLocal; prototype: Serializable.SerializableLocal; init(...args: any[]): void; } & K +>SuperClass : K + + return ResultClass; +>ResultClass : { new (...args: any[]): SerializableLocal; prototype: Serializable.SerializableLocal; init(...args: any[]): void; } & K + +}; + +const AMixin = & Initable>(SuperClass: K) => { +>AMixin : & Initable>(SuperClass: K) => void +> & Initable>(SuperClass: K) => { let SomeHowOkay = class A extends SuperClass { }; let SomeHowNotOkay = class A extends Serializable(SuperClass) { };} : & Initable>(SuperClass: K) => void +>SuperClass : K + + let SomeHowOkay = class A extends SuperClass { +>SomeHowOkay : { new (...args: any[]): A; prototype: AMixin.A; init(...args: any[]): void; } & K +>class A extends SuperClass { } : { new (...args: any[]): A; prototype: AMixin.A; init(...args: any[]): void; } & K +>A : { new (...args: any[]): A; prototype: AMixin.A; init(...args: any[]): void; } & K +>SuperClass : Initable + + }; + + let SomeHowNotOkay = class A extends Serializable(SuperClass) { +>SomeHowNotOkay : { new (...args: any[]): A; prototype: AMixin.A; init: (...args: any[]) => void; } & K +>class A extends Serializable(SuperClass) { } : { new (...args: any[]): A; prototype: AMixin.A; init: (...args: any[]) => void; } & K +>A : { new (...args: any[]): A; prototype: AMixin.A; init: (...args: any[]) => void; } & K +>Serializable(SuperClass) : Serializable.SerializableLocal & Initable +>Serializable : & Initable>(SuperClass: K) => { new (...args: any[]): SerializableLocal; prototype: Serializable.SerializableLocal; init(...args: any[]): void; } & K +>SuperClass : K + + }; +}; diff --git a/tests/cases/compiler/mixinIntersectionIsValidbaseType.ts b/tests/cases/compiler/mixinIntersectionIsValidbaseType.ts new file mode 100644 index 00000000000..4f446a9db8d --- /dev/null +++ b/tests/cases/compiler/mixinIntersectionIsValidbaseType.ts @@ -0,0 +1,27 @@ +export type Constructor = new (...args: any[]) => T; + +export interface Initable { + init(...args: any[]): void; +} + +/** + * Plain mixin where the superclass must be Initable + */ +export const Serializable = & Initable>( + SuperClass: K +) => { + const LocalMixin = (InnerSuperClass: K) => { + return class SerializableLocal extends InnerSuperClass { + } + }; + let ResultClass = LocalMixin(SuperClass); + return ResultClass; +}; + +const AMixin = & Initable>(SuperClass: K) => { + let SomeHowOkay = class A extends SuperClass { + }; + + let SomeHowNotOkay = class A extends Serializable(SuperClass) { + }; +}; \ No newline at end of file From 26caa3793e310e271ddee8adc1804486e5b0749f Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Mon, 23 Sep 2019 16:08:44 -0700 Subject: [PATCH 90/97] Introduce flattened error reporting for properties, call signatures, and construct signatures (#33473) * Introduce flattened error reporting for properties, call signatures, and construct signatures * Update message, specialize output for argument-less signatures * Skip leading signature incompatability flattening * Add return type specialized message --- scripts/processDiagnosticMessages.ts | 10 +- src/compiler/checker.ts | 226 +++++++++++++++--- src/compiler/diagnosticMessages.json | 29 +++ src/compiler/types.ts | 2 + .../reference/arrayLiterals3.errors.txt | 14 +- ...typeIsAssignableToReadonlyArray.errors.txt | 14 +- .../assignFromBooleanInterface2.errors.txt | 10 +- .../asyncFunctionDeclaration15_es5.errors.txt | 14 +- .../reference/bigintWithLib.errors.txt | 28 +-- .../reference/booleanAssignment.errors.txt | 10 +- ...atureAssignabilityInInheritance.errors.txt | 24 +- ...tureAssignabilityInInheritance3.errors.txt | 32 ++- .../checkJsxChildrenCanBeTupleType.errors.txt | 12 +- .../complexRecursiveCollections.errors.txt | 42 ++-- ...atureAssignabilityInInheritance.errors.txt | 24 +- ...tureAssignabilityInInheritance3.errors.txt | 32 ++- .../reference/covariantCallbacks.errors.txt | 10 +- .../reference/decoratorCallGeneric.errors.txt | 10 +- ...heckingWhenTargetIsIntersection.errors.txt | 16 +- ...stedAssignabilityErrorsCombined.errors.txt | 32 +++ ...deeplyNestedAssignabilityErrorsCombined.js | 36 +++ ...yNestedAssignabilityErrorsCombined.symbols | 63 +++++ ...plyNestedAssignabilityErrorsCombined.types | 95 ++++++++ ...oratedErrorsOnNullableTargets01.errors.txt | 16 +- ...AnnotationAndInvalidInitializer.errors.txt | 10 +- ...endAndImplementTheSameBaseType2.errors.txt | 10 +- tests/baselines/reference/for-of39.errors.txt | 40 ++-- .../reference/generatorTypeCheck25.errors.txt | 36 ++- .../reference/generatorTypeCheck63.errors.txt | 26 +- .../reference/generatorTypeCheck8.errors.txt | 22 +- .../baselines/reference/generics4.errors.txt | 10 +- .../reference/incompatibleTypes.errors.txt | 20 +- ...nheritedModuleMembersForClodule.errors.txt | 10 +- ...interfaceThatHidesBaseProperty2.errors.txt | 12 +- .../interfaceWithMultipleBaseTypes.errors.txt | 36 +-- ...interfaceWithMultipleBaseTypes2.errors.txt | 12 +- ...nvariantGenericErrorElaboration.errors.txt | 25 +- .../iterableArrayPattern28.errors.txt | 40 ++-- .../iteratorSpreadInArray6.errors.txt | 14 +- .../reference/mergedDeclarations7.errors.txt | 10 +- .../reference/multiLineErrors.errors.txt | 12 +- .../mutuallyRecursiveCallbacks.errors.txt | 10 +- ...nestedCallbackErrorNotFlattened.errors.txt | 20 ++ .../nestedCallbackErrorNotFlattened.js | 11 + .../nestedCallbackErrorNotFlattened.symbols | 27 +++ .../nestedCallbackErrorNotFlattened.types | 18 ++ ...RecursiveArraysOrObjectsError01.errors.txt | 42 ++-- ...MembersOfObjectAssignmentCompat.errors.txt | 30 +-- ...embersOfObjectAssignmentCompat2.errors.txt | 62 ++--- .../reference/promisePermutations.errors.txt | 8 +- .../reference/promisePermutations2.errors.txt | 8 +- .../reference/promisePermutations3.errors.txt | 8 +- .../reference/promiseTypeInference.errors.txt | 14 +- .../strictFunctionTypesErrors.errors.txt | 10 +- ...aturesWithSpecializedSignatures.errors.txt | 24 +- ...aturesWithSpecializedSignatures.errors.txt | 24 +- ...ignaturesWithOptionalParameters.errors.txt | 84 +++---- ...ignaturesWithOptionalParameters.errors.txt | 84 +++---- ...peParameterArgumentEquivalence5.errors.txt | 8 +- .../types.asyncGenerators.es2018.2.errors.txt | 98 +++----- ...deeplyNestedAssignabilityErrorsCombined.ts | 15 ++ .../nestedCallbackErrorNotFlattened.ts | 7 + 62 files changed, 1023 insertions(+), 735 deletions(-) create mode 100644 tests/baselines/reference/deeplyNestedAssignabilityErrorsCombined.errors.txt create mode 100644 tests/baselines/reference/deeplyNestedAssignabilityErrorsCombined.js create mode 100644 tests/baselines/reference/deeplyNestedAssignabilityErrorsCombined.symbols create mode 100644 tests/baselines/reference/deeplyNestedAssignabilityErrorsCombined.types create mode 100644 tests/baselines/reference/nestedCallbackErrorNotFlattened.errors.txt create mode 100644 tests/baselines/reference/nestedCallbackErrorNotFlattened.js create mode 100644 tests/baselines/reference/nestedCallbackErrorNotFlattened.symbols create mode 100644 tests/baselines/reference/nestedCallbackErrorNotFlattened.types create mode 100644 tests/cases/compiler/deeplyNestedAssignabilityErrorsCombined.ts create mode 100644 tests/cases/compiler/nestedCallbackErrorNotFlattened.ts diff --git a/scripts/processDiagnosticMessages.ts b/scripts/processDiagnosticMessages.ts index 4bdf3ca0178..7577707340a 100644 --- a/scripts/processDiagnosticMessages.ts +++ b/scripts/processDiagnosticMessages.ts @@ -6,6 +6,7 @@ interface DiagnosticDetails { code: number; reportsUnnecessary?: {}; isEarly?: boolean; + elidedInCompatabilityPyramid?: boolean; } type InputDiagnosticMessageTable = Map; @@ -63,14 +64,15 @@ function buildInfoFileOutput(messageTable: InputDiagnosticMessageTable, inputFil "// generated from '" + inputFilePathRel + "' by '" + thisFilePathRel.replace(/\\/g, "/") + "'\r\n" + "/* @internal */\r\n" + "namespace ts {\r\n" + - " function diag(code: number, category: DiagnosticCategory, key: string, message: string, reportsUnnecessary?: {}): DiagnosticMessage {\r\n" + - " return { code, category, key, message, reportsUnnecessary };\r\n" + + " function diag(code: number, category: DiagnosticCategory, key: string, message: string, reportsUnnecessary?: {}, elidedInCompatabilityPyramid?: boolean): DiagnosticMessage {\r\n" + + " return { code, category, key, message, reportsUnnecessary, elidedInCompatabilityPyramid };\r\n" + " }\r\n" + " export const Diagnostics = {\r\n"; - messageTable.forEach(({ code, category, reportsUnnecessary }, name) => { + messageTable.forEach(({ code, category, reportsUnnecessary, elidedInCompatabilityPyramid }, name) => { const propName = convertPropertyName(name); const argReportsUnnecessary = reportsUnnecessary ? `, /*reportsUnnecessary*/ ${reportsUnnecessary}` : ""; - result += ` ${propName}: diag(${code}, DiagnosticCategory.${category}, "${createKey(propName, code)}", ${JSON.stringify(name)}${argReportsUnnecessary}),\r\n`; + const argElidedInCompatabilityPyramid = elidedInCompatabilityPyramid ? `${!reportsUnnecessary ? ", /*reportsUnnecessary*/ undefined" : ""}, /*elidedInCompatabilityPyramid*/ ${elidedInCompatabilityPyramid}` : ""; + result += ` ${propName}: diag(${code}, DiagnosticCategory.${category}, "${createKey(propName, code)}", ${JSON.stringify(name)}${argReportsUnnecessary}${argElidedInCompatabilityPyramid}),\r\n`; }); result += " };\r\n}"; diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index e29f46f2dec..de5c3b5b358 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -12329,7 +12329,7 @@ namespace ts { target: Signature, ignoreReturnTypes: boolean): boolean { return compareSignaturesRelated(source, target, CallbackCheck.None, ignoreReturnTypes, /*reportErrors*/ false, - /*errorReporter*/ undefined, compareTypesAssignable) !== Ternary.False; + /*errorReporter*/ undefined, /*errorReporter*/ undefined, compareTypesAssignable) !== Ternary.False; } type ErrorReporter = (message: DiagnosticMessage, arg0?: string, arg1?: string) => void; @@ -12352,6 +12352,7 @@ namespace ts { ignoreReturnTypes: boolean, reportErrors: boolean, errorReporter: ErrorReporter | undefined, + incompatibleErrorReporter: ((source: Type, target: Type) => void) | undefined, compareTypes: TypeComparer): Ternary { // TODO (drosen): De-duplicate code between related functions. if (source === target) { @@ -12422,7 +12423,7 @@ namespace ts { (getFalsyFlags(sourceType) & TypeFlags.Nullable) === (getFalsyFlags(targetType) & TypeFlags.Nullable); const related = callbacks ? // TODO: GH#18217 It will work if they're both `undefined`, but not if only one is - compareSignaturesRelated(targetSig!, sourceSig!, strictVariance ? CallbackCheck.Strict : CallbackCheck.Bivariant, /*ignoreReturnTypes*/ false, reportErrors, errorReporter, compareTypes) : + compareSignaturesRelated(targetSig!, sourceSig!, strictVariance ? CallbackCheck.Strict : CallbackCheck.Bivariant, /*ignoreReturnTypes*/ false, reportErrors, errorReporter, incompatibleErrorReporter, compareTypes) : !callbackCheck && !strictVariance && compareTypes(sourceType, targetType, /*reportErrors*/ false) || compareTypes(targetType, sourceType, reportErrors); if (!related) { if (reportErrors) { @@ -12468,6 +12469,9 @@ namespace ts { // wouldn't be co-variant for T without this rule. result &= callbackCheck === CallbackCheck.Bivariant && compareTypes(targetReturnType, sourceReturnType, /*reportErrors*/ false) || compareTypes(sourceReturnType, targetReturnType, reportErrors); + if (!result && reportErrors && incompatibleErrorReporter) { + incompatibleErrorReporter(sourceReturnType, targetReturnType); + } } } @@ -12677,11 +12681,16 @@ namespace ts { let depth = 0; let expandingFlags = ExpandingFlags.None; let overflow = false; - let overrideNextErrorInfo: DiagnosticMessageChain | undefined; + let overrideNextErrorInfo = 0; // How many `reportRelationError` calls should be skipped in the elaboration pyramid + let lastSkippedInfo: [Type, Type] | undefined; + let incompatibleStack: [DiagnosticMessage, (string | number)?, (string | number)?, (string | number)?, (string | number)?][] = []; Debug.assert(relation !== identityRelation || !errorNode, "no error reporting in identity checking"); const result = isRelatedTo(source, target, /*reportErrors*/ !!errorNode, headMessage); + if (incompatibleStack.length) { + reportIncompatibleStack(); + } if (overflow) { const diag = error(errorNode, Diagnostics.Excessive_stack_depth_comparing_types_0_and_1, typeToString(source), typeToString(target)); if (errorOutputContainer) { @@ -12726,8 +12735,134 @@ namespace ts { } return result !== Ternary.False; + function resetErrorInfo(saved: ReturnType) { + errorInfo = saved.errorInfo; + lastSkippedInfo = saved.lastSkippedInfo; + incompatibleStack = saved.incompatibleStack; + overrideNextErrorInfo = saved.overrideNextErrorInfo; + relatedInfo = saved.relatedInfo; + } + + function captureErrorCalculationState() { + return { + errorInfo, + lastSkippedInfo, + incompatibleStack: incompatibleStack.slice(), + overrideNextErrorInfo, + relatedInfo: !relatedInfo ? undefined : relatedInfo.slice() as ([DiagnosticRelatedInformation, ...DiagnosticRelatedInformation[]] | undefined) + }; + } + + function reportIncompatibleError(message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number, arg3?: string | number) { + overrideNextErrorInfo++; // Suppress the next relation error + lastSkippedInfo = undefined; // Reset skipped info cache + incompatibleStack.push([message, arg0, arg1, arg2, arg3]); + } + + function reportIncompatibleStack() { + const stack = incompatibleStack; + incompatibleStack = []; + const info = lastSkippedInfo; + lastSkippedInfo = undefined; + if (stack.length === 1) { + reportError(...stack[0]); + if (info) { + // Actually do the last relation error + reportRelationError(/*headMessage*/ undefined, ...info); + } + return; + } + // The first error will be the innermost, while the last will be the outermost - so by popping off the end, + // we can build from left to right + let path = ""; + const secondaryRootErrors: typeof incompatibleStack = []; + while (stack.length) { + const [msg, ...args] = stack.pop()!; + switch (msg.code) { + case Diagnostics.Types_of_property_0_are_incompatible.code: { + // Parenthesize a `new` if there is one + if (path.indexOf("new ") === 0) { + path = `(${path})`; + } + const str = "" + args[0]; + // If leading, just print back the arg (irrespective of if it's a valid identifier) + if (path.length === 0) { + path = `${str}`; + } + // Otherwise write a dotted name if possible + else if (isIdentifierText(str, compilerOptions.target)) { + path = `${path}.${str}`; + } + // Failing that, check if the name is already a computed name + else if (str[0] === "[" && str[str.length - 1] === "]") { + path = `${path}${str}`; + } + // And finally write out a computed name as a last resort + else { + path = `${path}[${str}]`; + } + break; + } + case Diagnostics.Call_signature_return_types_0_and_1_are_incompatible.code: + case Diagnostics.Construct_signature_return_types_0_and_1_are_incompatible.code: + case Diagnostics.Call_signatures_with_no_arguments_have_incompatible_return_types_0_and_1.code: + case Diagnostics.Construct_signatures_with_no_arguments_have_incompatible_return_types_0_and_1.code: { + if (path.length === 0) { + // Don't flatten signature compatability errors at the start of a chain - instead prefer + // to unify (the with no arguments bit is excessive for printback) and print them back + let mappedMsg = msg; + if (msg.code === Diagnostics.Call_signatures_with_no_arguments_have_incompatible_return_types_0_and_1.code) { + mappedMsg = Diagnostics.Call_signature_return_types_0_and_1_are_incompatible; + } + else if (msg.code === Diagnostics.Construct_signatures_with_no_arguments_have_incompatible_return_types_0_and_1.code) { + mappedMsg = Diagnostics.Construct_signature_return_types_0_and_1_are_incompatible; + } + secondaryRootErrors.unshift([mappedMsg, args[0], args[1]]); + } + else { + const prefix = (msg.code === Diagnostics.Construct_signature_return_types_0_and_1_are_incompatible.code || + msg.code === Diagnostics.Construct_signatures_with_no_arguments_have_incompatible_return_types_0_and_1.code) + ? "new " + : ""; + const params = (msg.code === Diagnostics.Call_signatures_with_no_arguments_have_incompatible_return_types_0_and_1.code || + msg.code === Diagnostics.Construct_signatures_with_no_arguments_have_incompatible_return_types_0_and_1.code) + ? "" + : "..."; + path = `${prefix}${path}(${params})`; + } + break; + } + default: + return Debug.fail(`Unhandled Diagnostic: ${msg.code}`); + } + } + if (path) { + reportError(path[path.length - 1] === ")" + ? Diagnostics.The_types_returned_by_0_are_incompatible_between_these_types + : Diagnostics.The_types_of_0_are_incompatible_between_these_types, + path + ); + } + else { + // Remove the innermost secondary error as it will duplicate the error already reported by `reportRelationError` on entry + secondaryRootErrors.shift(); + } + for (const [msg, ...args] of secondaryRootErrors) { + const originalValue = msg.elidedInCompatabilityPyramid; + msg.elidedInCompatabilityPyramid = false; // Teporarily override elision to ensure error is reported + reportError(msg, ...args); + msg.elidedInCompatabilityPyramid = originalValue; + } + if (info) { + // Actually do the last relation error + reportRelationError(/*headMessage*/ undefined, ...info); + } + } + function reportError(message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number, arg3?: string | number): void { Debug.assert(!!errorNode); + if (incompatibleStack.length) reportIncompatibleStack(); + if (message.elidedInCompatabilityPyramid) return; errorInfo = chainDiagnosticMessages(errorInfo, message, arg0, arg1, arg2, arg3); } @@ -12742,6 +12877,7 @@ namespace ts { } function reportRelationError(message: DiagnosticMessage | undefined, source: Type, target: Type) { + if (incompatibleStack.length) reportIncompatibleStack(); const [sourceType, targetType] = getTypeNamesForErrorDisplay(source, target); if (target.flags & TypeFlags.TypeParameter && target.immediateBaseConstraint !== undefined && isTypeAssignableTo(source, target.immediateBaseConstraint)) { @@ -12899,7 +13035,7 @@ namespace ts { } let result = Ternary.False; - const saveErrorInfo = errorInfo; + const saveErrorInfo = captureErrorCalculationState(); let isIntersectionConstituent = !!isApparentIntersectionConstituent; // Note that these checks are specifically ordered to produce correct results. In particular, @@ -12949,7 +13085,7 @@ namespace ts { } if (!result && (source.flags & TypeFlags.StructuredOrInstantiable || target.flags & TypeFlags.StructuredOrInstantiable)) { if (result = recursiveTypeRelatedTo(source, target, reportErrors, isIntersectionConstituent)) { - errorInfo = saveErrorInfo; + resetErrorInfo(saveErrorInfo); } } } @@ -12966,19 +13102,21 @@ namespace ts { const constraint = getUnionConstraintOfIntersection(source, !!(target.flags & TypeFlags.Union)); if (constraint) { if (result = isRelatedTo(constraint, target, reportErrors, /*headMessage*/ undefined, isIntersectionConstituent)) { - errorInfo = saveErrorInfo; + resetErrorInfo(saveErrorInfo); } } } if (!result && reportErrors) { - let maybeSuppress = overrideNextErrorInfo; - overrideNextErrorInfo = undefined; + let maybeSuppress = overrideNextErrorInfo > 0; + if (maybeSuppress) { + overrideNextErrorInfo--; + } if (source.flags & TypeFlags.Object && target.flags & TypeFlags.Object) { const currentError = errorInfo; tryElaborateArrayLikeErrors(source, target, reportErrors); if (errorInfo !== currentError) { - maybeSuppress = errorInfo; + maybeSuppress = !!errorInfo; } } if (source.flags & TypeFlags.Object && target.flags & TypeFlags.Primitive) { @@ -12998,6 +13136,7 @@ namespace ts { } } if (!headMessage && maybeSuppress) { + lastSkippedInfo = [source, target]; // Used by, eg, missing property checking to replace the top-level message with a more informative one return result; } @@ -13439,7 +13578,7 @@ namespace ts { let result: Ternary; let originalErrorInfo: DiagnosticMessageChain | undefined; let varianceCheckFailed = false; - const saveErrorInfo = errorInfo; + const saveErrorInfo = captureErrorCalculationState(); // We limit alias variance probing to only object and conditional types since their alias behavior // is more predictable than other, interned types, which may or may not have an alias depending on @@ -13531,7 +13670,7 @@ namespace ts { } } originalErrorInfo = errorInfo; - errorInfo = saveErrorInfo; + resetErrorInfo(saveErrorInfo); } } } @@ -13543,7 +13682,7 @@ namespace ts { result &= isRelatedTo((source).indexType, (target).indexType, reportErrors); } if (result) { - errorInfo = saveErrorInfo; + resetErrorInfo(saveErrorInfo); return result; } } @@ -13552,25 +13691,25 @@ namespace ts { if (!constraint || (source.flags & TypeFlags.TypeParameter && constraint.flags & TypeFlags.Any)) { // A type variable with no constraint is not related to the non-primitive object type. if (result = isRelatedTo(emptyObjectType, extractTypesOfKind(target, ~TypeFlags.NonPrimitive))) { - errorInfo = saveErrorInfo; + resetErrorInfo(saveErrorInfo); return result; } } // hi-speed no-this-instantiation check (less accurate, but avoids costly `this`-instantiation when the constraint will suffice), see #28231 for report on why this is needed else if (result = isRelatedTo(constraint, target, /*reportErrors*/ false, /*headMessage*/ undefined, isIntersectionConstituent)) { - errorInfo = saveErrorInfo; + resetErrorInfo(saveErrorInfo); return result; } // slower, fuller, this-instantiated check (necessary when comparing raw `this` types from base classes), see `subclassWithPolymorphicThisIsAssignable.ts` test for example else if (result = isRelatedTo(getTypeWithThisArgument(constraint, source), target, reportErrors, /*headMessage*/ undefined, isIntersectionConstituent)) { - errorInfo = saveErrorInfo; + resetErrorInfo(saveErrorInfo); return result; } } } else if (source.flags & TypeFlags.Index) { if (result = isRelatedTo(keyofConstraintType, target, reportErrors)) { - errorInfo = saveErrorInfo; + resetErrorInfo(saveErrorInfo); return result; } } @@ -13595,7 +13734,7 @@ namespace ts { result &= isRelatedTo(getFalseTypeFromConditionalType(source), getFalseTypeFromConditionalType(target), reportErrors); } if (result) { - errorInfo = saveErrorInfo; + resetErrorInfo(saveErrorInfo); return result; } } @@ -13604,14 +13743,14 @@ namespace ts { const distributiveConstraint = getConstraintOfDistributiveConditionalType(source); if (distributiveConstraint) { if (result = isRelatedTo(distributiveConstraint, target, reportErrors)) { - errorInfo = saveErrorInfo; + resetErrorInfo(saveErrorInfo); return result; } } const defaultConstraint = getDefaultConstraintOfConditionalType(source); if (defaultConstraint) { if (result = isRelatedTo(defaultConstraint, target, reportErrors)) { - errorInfo = saveErrorInfo; + resetErrorInfo(saveErrorInfo); return result; } } @@ -13625,7 +13764,7 @@ namespace ts { if (isGenericMappedType(target)) { if (isGenericMappedType(source)) { if (result = mappedTypeRelatedTo(source, target, reportErrors)) { - errorInfo = saveErrorInfo; + resetErrorInfo(saveErrorInfo); return result; } } @@ -13671,7 +13810,7 @@ namespace ts { // relates to X. Thus, we include intersection types on the source side here. if (source.flags & (TypeFlags.Object | TypeFlags.Intersection) && target.flags & TypeFlags.Object) { // Report structural errors only if we haven't reported any errors yet - const reportStructuralErrors = reportErrors && errorInfo === saveErrorInfo && !sourceIsPrimitive; + const reportStructuralErrors = reportErrors && errorInfo === saveErrorInfo.errorInfo && !sourceIsPrimitive; result = propertiesRelatedTo(source, target, reportStructuralErrors, /*excludedProperties*/ undefined, isIntersectionConstituent); if (result) { result &= signaturesRelatedTo(source, target, SignatureKind.Call, reportStructuralErrors); @@ -13686,7 +13825,7 @@ namespace ts { } } if (varianceCheckFailed && result) { - errorInfo = originalErrorInfo || errorInfo || saveErrorInfo; // Use variance error (there is no structural one) and return false + errorInfo = originalErrorInfo || errorInfo || saveErrorInfo.errorInfo; // Use variance error (there is no structural one) and return false } else if (result) { return result; @@ -13718,7 +13857,7 @@ namespace ts { // We elide the variance-based error elaborations, since those might not be too helpful, since we'll potentially // be assuming identity of the type parameter. originalErrorInfo = undefined; - errorInfo = saveErrorInfo; + resetErrorInfo(saveErrorInfo); return undefined; } const allowStructuralFallback = targetTypeArguments && hasCovariantVoidArgument(targetTypeArguments, variances); @@ -13746,7 +13885,7 @@ namespace ts { // comparison unexpectedly succeeds. This can happen when the structural comparison result // is a Ternary.Maybe for example caused by the recursion depth limiter. originalErrorInfo = errorInfo; - errorInfo = saveErrorInfo; + resetErrorInfo(saveErrorInfo); } } } @@ -13980,7 +14119,7 @@ namespace ts { const related = isPropertySymbolTypeRelated(sourceProp, targetProp, getTypeOfSourceProperty, reportErrors, isIntersectionConstituent); if (!related) { if (reportErrors) { - reportError(Diagnostics.Types_of_property_0_are_incompatible, symbolToString(targetProp)); + reportIncompatibleError(Diagnostics.Types_of_property_0_are_incompatible, symbolToString(targetProp)); } return Ternary.False; } @@ -14022,8 +14161,8 @@ namespace ts { if (length(unmatchedProperty.declarations)) { associateRelatedInfo(createDiagnosticForNode(unmatchedProperty.declarations[0], Diagnostics._0_is_declared_here, propName)); } - if (shouldSkipElaboration) { - overrideNextErrorInfo = errorInfo; + if (shouldSkipElaboration && errorInfo) { + overrideNextErrorInfo++; } } else if (tryElaborateArrayLikeErrors(source, target, /*reportErrors*/ false)) { @@ -14033,8 +14172,8 @@ namespace ts { else { reportError(Diagnostics.Type_0_is_missing_the_following_properties_from_type_1_Colon_2, typeToString(source), typeToString(target), map(props, p => symbolToString(p)).join(", ")); } - if (shouldSkipElaboration) { - overrideNextErrorInfo = errorInfo; + if (shouldSkipElaboration && errorInfo) { + overrideNextErrorInfo++; } } // ELSE: No array like or unmatched property error - just issue top level error (errorInfo = undefined) @@ -14157,7 +14296,8 @@ namespace ts { } let result = Ternary.True; - const saveErrorInfo = errorInfo; + const saveErrorInfo = captureErrorCalculationState(); + const incompatibleReporter = kind === SignatureKind.Construct ? reportIncompatibleConstructSignatureReturn : reportIncompatibleCallSignatureReturn; if (getObjectFlags(source) & ObjectFlags.Instantiated && getObjectFlags(target) & ObjectFlags.Instantiated && source.symbol === target.symbol) { // We have instantiations of the same anonymous type (which typically will be the type of a @@ -14165,7 +14305,7 @@ namespace ts { // of the much more expensive N * M comparison matrix we explore below. We erase type parameters // as they are known to always be the same. for (let i = 0; i < targetSignatures.length; i++) { - const related = signatureRelatedTo(sourceSignatures[i], targetSignatures[i], /*erase*/ true, reportErrors); + const related = signatureRelatedTo(sourceSignatures[i], targetSignatures[i], /*erase*/ true, reportErrors, incompatibleReporter(sourceSignatures[i], targetSignatures[i])); if (!related) { return Ternary.False; } @@ -14179,17 +14319,17 @@ namespace ts { // this regardless of the number of signatures, but the potential costs are prohibitive due // to the quadratic nature of the logic below. const eraseGenerics = relation === comparableRelation || !!compilerOptions.noStrictGenericChecks; - result = signatureRelatedTo(sourceSignatures[0], targetSignatures[0], eraseGenerics, reportErrors); + result = signatureRelatedTo(sourceSignatures[0], targetSignatures[0], eraseGenerics, reportErrors, incompatibleReporter(sourceSignatures[0], targetSignatures[0])); } else { outer: for (const t of targetSignatures) { // Only elaborate errors from the first failure let shouldElaborateErrors = reportErrors; for (const s of sourceSignatures) { - const related = signatureRelatedTo(s, t, /*erase*/ true, shouldElaborateErrors); + const related = signatureRelatedTo(s, t, /*erase*/ true, shouldElaborateErrors, incompatibleReporter(s, t)); if (related) { result &= related; - errorInfo = saveErrorInfo; + resetErrorInfo(saveErrorInfo); continue outer; } shouldElaborateErrors = false; @@ -14206,12 +14346,26 @@ namespace ts { return result; } + function reportIncompatibleCallSignatureReturn(siga: Signature, sigb: Signature) { + if (siga.parameters.length === 0 && sigb.parameters.length === 0) { + return (source: Type, target: Type) => reportIncompatibleError(Diagnostics.Call_signatures_with_no_arguments_have_incompatible_return_types_0_and_1, typeToString(source), typeToString(target)); + } + return (source: Type, target: Type) => reportIncompatibleError(Diagnostics.Call_signature_return_types_0_and_1_are_incompatible, typeToString(source), typeToString(target)); + } + + function reportIncompatibleConstructSignatureReturn(siga: Signature, sigb: Signature) { + if (siga.parameters.length === 0 && sigb.parameters.length === 0) { + return (source: Type, target: Type) => reportIncompatibleError(Diagnostics.Construct_signatures_with_no_arguments_have_incompatible_return_types_0_and_1, typeToString(source), typeToString(target)); + } + return (source: Type, target: Type) => reportIncompatibleError(Diagnostics.Construct_signature_return_types_0_and_1_are_incompatible, typeToString(source), typeToString(target)); + } + /** * See signatureAssignableTo, compareSignaturesIdentical */ - function signatureRelatedTo(source: Signature, target: Signature, erase: boolean, reportErrors: boolean): Ternary { + function signatureRelatedTo(source: Signature, target: Signature, erase: boolean, reportErrors: boolean, incompatibleReporter: (source: Type, target: Type) => void): Ternary { return compareSignaturesRelated(erase ? getErasedSignature(source) : source, erase ? getErasedSignature(target) : target, - CallbackCheck.None, /*ignoreReturnTypes*/ false, reportErrors, reportError, isRelatedTo); + CallbackCheck.None, /*ignoreReturnTypes*/ false, reportErrors, reportError, incompatibleReporter, isRelatedTo); } function signaturesIdenticalTo(source: Type, target: Type, kind: SignatureKind): Ternary { diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 44bba361bbc..3b219b37de4 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1040,6 +1040,35 @@ "code": 1357 }, + "The types of '{0}' are incompatible between these types.": { + "category": "Error", + "code": 2200 + }, + "The types returned by '{0}' are incompatible between these types.": { + "category": "Error", + "code": 2201 + }, + "Call signature return types '{0}' and '{1}' are incompatible.": { + "category": "Error", + "code": 2202, + "elidedInCompatabilityPyramid": true + }, + "Construct signature return types '{0}' and '{1}' are incompatible.": { + "category": "Error", + "code": 2203, + "elidedInCompatabilityPyramid": true + }, + "Call signatures with no arguments have incompatible return types '{0}' and '{1}'.": { + "category": "Error", + "code": 2204, + "elidedInCompatabilityPyramid": true + }, + "Construct signatures with no arguments have incompatible return types '{0}' and '{1}'.": { + "category": "Error", + "code": 2205, + "elidedInCompatabilityPyramid": true + }, + "Duplicate identifier '{0}'.": { "category": "Error", "code": 2300 diff --git a/src/compiler/types.ts b/src/compiler/types.ts index e20518ed119..eb568ed5cff 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -4631,6 +4631,8 @@ namespace ts { code: number; message: string; reportsUnnecessary?: {}; + /* @internal */ + elidedInCompatabilityPyramid?: boolean; } /** diff --git a/tests/baselines/reference/arrayLiterals3.errors.txt b/tests/baselines/reference/arrayLiterals3.errors.txt index 4d398978c96..5f592b74f3e 100644 --- a/tests/baselines/reference/arrayLiterals3.errors.txt +++ b/tests/baselines/reference/arrayLiterals3.errors.txt @@ -8,10 +8,9 @@ tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(17,5): error tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(32,5): error TS2739: Type '(number[] | string[])[]' is missing the following properties from type 'tup': 0, 1 tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(33,5): error TS2739: Type 'number[]' is missing the following properties from type '[number, number, number]': 0, 1, 2 tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(34,5): error TS2322: Type '(string | number)[]' is not assignable to type 'myArray'. - Types of property 'pop' are incompatible. - Type '() => string | number' is not assignable to type '() => Number'. - Type 'string | number' is not assignable to type 'Number'. - Type 'string' is not assignable to type 'Number'. + The types returned by 'pop()' are incompatible between these types. + Type 'string | number' is not assignable to type 'Number'. + Type 'string' is not assignable to type 'Number'. ==== tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts (8 errors) ==== @@ -67,8 +66,7 @@ tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(34,5): error var c2: myArray = [...temp1, ...temp]; // Error cannot assign (number|string)[] to number[] ~~ !!! error TS2322: Type '(string | number)[]' is not assignable to type 'myArray'. -!!! error TS2322: Types of property 'pop' are incompatible. -!!! error TS2322: Type '() => string | number' is not assignable to type '() => Number'. -!!! error TS2322: Type 'string | number' is not assignable to type 'Number'. -!!! error TS2322: Type 'string' is not assignable to type 'Number'. +!!! error TS2322: The types returned by 'pop()' are incompatible between these types. +!!! error TS2322: Type 'string | number' is not assignable to type 'Number'. +!!! error TS2322: Type 'string' is not assignable to type 'Number'. \ No newline at end of file diff --git a/tests/baselines/reference/arrayOfSubtypeIsAssignableToReadonlyArray.errors.txt b/tests/baselines/reference/arrayOfSubtypeIsAssignableToReadonlyArray.errors.txt index 63d3a6f7b64..66b3c2c8691 100644 --- a/tests/baselines/reference/arrayOfSubtypeIsAssignableToReadonlyArray.errors.txt +++ b/tests/baselines/reference/arrayOfSubtypeIsAssignableToReadonlyArray.errors.txt @@ -1,10 +1,9 @@ tests/cases/compiler/arrayOfSubtypeIsAssignableToReadonlyArray.ts(13,1): error TS2322: Type 'A[]' is not assignable to type 'readonly B[]'. Property 'b' is missing in type 'A' but required in type 'B'. tests/cases/compiler/arrayOfSubtypeIsAssignableToReadonlyArray.ts(18,1): error TS2322: Type 'C' is not assignable to type 'readonly B[]'. - Types of property 'concat' are incompatible. - Type '{ (...items: ConcatArray[]): A[]; (...items: (A | ConcatArray)[]): A[]; }' is not assignable to type '{ (...items: ConcatArray[]): B[]; (...items: (B | ConcatArray)[]): B[]; }'. - Type 'A[]' is not assignable to type 'B[]'. - Type 'A' is not assignable to type 'B'. + The types returned by 'concat(...)' are incompatible between these types. + Type 'A[]' is not assignable to type 'B[]'. + Type 'A' is not assignable to type 'B'. ==== tests/cases/compiler/arrayOfSubtypeIsAssignableToReadonlyArray.ts (2 errors) ==== @@ -32,8 +31,7 @@ tests/cases/compiler/arrayOfSubtypeIsAssignableToReadonlyArray.ts(18,1): error T rrb = cra; // error: 'A' is not assignable to 'B' ~~~ !!! error TS2322: Type 'C' is not assignable to type 'readonly B[]'. -!!! error TS2322: Types of property 'concat' are incompatible. -!!! error TS2322: Type '{ (...items: ConcatArray[]): A[]; (...items: (A | ConcatArray)[]): A[]; }' is not assignable to type '{ (...items: ConcatArray[]): B[]; (...items: (B | ConcatArray)[]): B[]; }'. -!!! error TS2322: Type 'A[]' is not assignable to type 'B[]'. -!!! error TS2322: Type 'A' is not assignable to type 'B'. +!!! error TS2322: The types returned by 'concat(...)' are incompatible between these types. +!!! error TS2322: Type 'A[]' is not assignable to type 'B[]'. +!!! error TS2322: Type 'A' is not assignable to type 'B'. \ No newline at end of file diff --git a/tests/baselines/reference/assignFromBooleanInterface2.errors.txt b/tests/baselines/reference/assignFromBooleanInterface2.errors.txt index 6c7ebbe5ee8..ba8b2f5a9c9 100644 --- a/tests/baselines/reference/assignFromBooleanInterface2.errors.txt +++ b/tests/baselines/reference/assignFromBooleanInterface2.errors.txt @@ -1,7 +1,6 @@ tests/cases/conformance/types/primitives/boolean/assignFromBooleanInterface2.ts(14,1): error TS2322: Type 'NotBoolean' is not assignable to type 'Boolean'. - Types of property 'valueOf' are incompatible. - Type '() => Object' is not assignable to type '() => boolean'. - Type 'Object' is not assignable to type 'boolean'. + The types returned by 'valueOf()' are incompatible between these types. + Type 'Object' is not assignable to type 'boolean'. tests/cases/conformance/types/primitives/boolean/assignFromBooleanInterface2.ts(19,1): error TS2322: Type 'Boolean' is not assignable to type 'boolean'. 'boolean' is a primitive, but 'Boolean' is a wrapper object. Prefer using 'boolean' when possible. tests/cases/conformance/types/primitives/boolean/assignFromBooleanInterface2.ts(20,1): error TS2322: Type 'NotBoolean' is not assignable to type 'boolean'. @@ -24,9 +23,8 @@ tests/cases/conformance/types/primitives/boolean/assignFromBooleanInterface2.ts( a = b; ~ !!! error TS2322: Type 'NotBoolean' is not assignable to type 'Boolean'. -!!! error TS2322: Types of property 'valueOf' are incompatible. -!!! error TS2322: Type '() => Object' is not assignable to type '() => boolean'. -!!! error TS2322: Type 'Object' is not assignable to type 'boolean'. +!!! error TS2322: The types returned by 'valueOf()' are incompatible between these types. +!!! error TS2322: Type 'Object' is not assignable to type 'boolean'. b = a; b = x; diff --git a/tests/baselines/reference/asyncFunctionDeclaration15_es5.errors.txt b/tests/baselines/reference/asyncFunctionDeclaration15_es5.errors.txt index 6b4b0a71c6f..7e3595ec6f4 100644 --- a/tests/baselines/reference/asyncFunctionDeclaration15_es5.errors.txt +++ b/tests/baselines/reference/asyncFunctionDeclaration15_es5.errors.txt @@ -5,10 +5,9 @@ tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration1 tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(8,23): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(9,23): error TS1055: Type 'PromiseLike' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value. tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(10,23): error TS1055: Type 'typeof Thenable' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value. - Type 'Thenable' is not assignable to type 'PromiseLike'. - Types of property 'then' are incompatible. - Type '() => void' is not assignable to type '(onfulfilled?: (value: T) => TResult1 | PromiseLike, onrejected?: (reason: any) => TResult2 | PromiseLike) => PromiseLike'. - Type 'void' is not assignable to type 'PromiseLike'. + Construct signature return types 'Thenable' and 'PromiseLike' are incompatible. + The types returned by 'then(...)' are incompatible between these types. + Type 'void' is not assignable to type 'PromiseLike'. tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(17,16): error TS1058: The return type of an async function must either be a valid promise or must not contain a callable 'then' member. tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(23,25): error TS1320: Type of 'await' operand must either be a valid promise or must not contain a callable 'then' member. @@ -38,10 +37,9 @@ tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration1 async function fn6(): Thenable { } // error ~~~~~~~~ !!! error TS1055: Type 'typeof Thenable' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value. -!!! error TS1055: Type 'Thenable' is not assignable to type 'PromiseLike'. -!!! error TS1055: Types of property 'then' are incompatible. -!!! error TS1055: Type '() => void' is not assignable to type '(onfulfilled?: (value: T) => TResult1 | PromiseLike, onrejected?: (reason: any) => TResult2 | PromiseLike) => PromiseLike'. -!!! error TS1055: Type 'void' is not assignable to type 'PromiseLike'. +!!! error TS1055: Construct signature return types 'Thenable' and 'PromiseLike' are incompatible. +!!! error TS1055: The types returned by 'then(...)' are incompatible between these types. +!!! error TS1055: Type 'void' is not assignable to type 'PromiseLike'. async function fn7() { return; } // valid: Promise async function fn8() { return 1; } // valid: Promise async function fn9() { return null; } // valid: Promise diff --git a/tests/baselines/reference/bigintWithLib.errors.txt b/tests/baselines/reference/bigintWithLib.errors.txt index b4c9f65cf9c..fb54c2a530b 100644 --- a/tests/baselines/reference/bigintWithLib.errors.txt +++ b/tests/baselines/reference/bigintWithLib.errors.txt @@ -4,15 +4,11 @@ tests/cases/compiler/bigintWithLib.ts(16,33): error TS2769: No overload matches Argument of type 'number[]' is not assignable to parameter of type 'number'. Overload 2 of 3, '(array: Iterable): BigInt64Array', gave the following error. Argument of type 'number[]' is not assignable to parameter of type 'Iterable'. - Types of property '[Symbol.iterator]' are incompatible. - Type '() => IterableIterator' is not assignable to type '() => Iterator'. - Type 'IterableIterator' is not assignable to type 'Iterator'. - Types of property 'next' are incompatible. - Type '(...args: [] | [undefined]) => IteratorResult' is not assignable to type '(...args: [] | [undefined]) => IteratorResult'. - Type 'IteratorResult' is not assignable to type 'IteratorResult'. - Type 'IteratorYieldResult' is not assignable to type 'IteratorResult'. - Type 'IteratorYieldResult' is not assignable to type 'IteratorYieldResult'. - Type 'number' is not assignable to type 'bigint'. + The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types. + Type 'IteratorResult' is not assignable to type 'IteratorResult'. + Type 'IteratorYieldResult' is not assignable to type 'IteratorResult'. + Type 'IteratorYieldResult' is not assignable to type 'IteratorYieldResult'. + Type 'number' is not assignable to type 'bigint'. Overload 3 of 3, '(buffer: ArrayBuffer | SharedArrayBuffer, byteOffset?: number, length?: number): BigInt64Array', gave the following error. Argument of type 'number[]' is not assignable to parameter of type 'ArrayBuffer | SharedArrayBuffer'. Type 'number[]' is missing the following properties from type 'SharedArrayBuffer': byteLength, [Symbol.species], [Symbol.toStringTag] @@ -55,15 +51,11 @@ tests/cases/compiler/bigintWithLib.ts(43,26): error TS2345: Argument of type '12 !!! error TS2769: Argument of type 'number[]' is not assignable to parameter of type 'number'. !!! error TS2769: Overload 2 of 3, '(array: Iterable): BigInt64Array', gave the following error. !!! error TS2769: Argument of type 'number[]' is not assignable to parameter of type 'Iterable'. -!!! error TS2769: Types of property '[Symbol.iterator]' are incompatible. -!!! error TS2769: Type '() => IterableIterator' is not assignable to type '() => Iterator'. -!!! error TS2769: Type 'IterableIterator' is not assignable to type 'Iterator'. -!!! error TS2769: Types of property 'next' are incompatible. -!!! error TS2769: Type '(...args: [] | [undefined]) => IteratorResult' is not assignable to type '(...args: [] | [undefined]) => IteratorResult'. -!!! error TS2769: Type 'IteratorResult' is not assignable to type 'IteratorResult'. -!!! error TS2769: Type 'IteratorYieldResult' is not assignable to type 'IteratorResult'. -!!! error TS2769: Type 'IteratorYieldResult' is not assignable to type 'IteratorYieldResult'. -!!! error TS2769: Type 'number' is not assignable to type 'bigint'. +!!! error TS2769: The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types. +!!! error TS2769: Type 'IteratorResult' is not assignable to type 'IteratorResult'. +!!! error TS2769: Type 'IteratorYieldResult' is not assignable to type 'IteratorResult'. +!!! error TS2769: Type 'IteratorYieldResult' is not assignable to type 'IteratorYieldResult'. +!!! error TS2769: Type 'number' is not assignable to type 'bigint'. !!! error TS2769: Overload 3 of 3, '(buffer: ArrayBuffer | SharedArrayBuffer, byteOffset?: number, length?: number): BigInt64Array', gave the following error. !!! error TS2769: Argument of type 'number[]' is not assignable to parameter of type 'ArrayBuffer | SharedArrayBuffer'. !!! error TS2769: Type 'number[]' is missing the following properties from type 'SharedArrayBuffer': byteLength, [Symbol.species], [Symbol.toStringTag] diff --git a/tests/baselines/reference/booleanAssignment.errors.txt b/tests/baselines/reference/booleanAssignment.errors.txt index fc25fd4007f..8a928c57404 100644 --- a/tests/baselines/reference/booleanAssignment.errors.txt +++ b/tests/baselines/reference/booleanAssignment.errors.txt @@ -1,9 +1,8 @@ tests/cases/compiler/booleanAssignment.ts(2,1): error TS2322: Type '1' is not assignable to type 'Boolean'. tests/cases/compiler/booleanAssignment.ts(3,1): error TS2322: Type '"a"' is not assignable to type 'Boolean'. tests/cases/compiler/booleanAssignment.ts(4,1): error TS2322: Type '{}' is not assignable to type 'Boolean'. - Types of property 'valueOf' are incompatible. - Type '() => Object' is not assignable to type '() => boolean'. - Type 'Object' is not assignable to type 'boolean'. + The types returned by 'valueOf()' are incompatible between these types. + Type 'Object' is not assignable to type 'boolean'. ==== tests/cases/compiler/booleanAssignment.ts (3 errors) ==== @@ -17,9 +16,8 @@ tests/cases/compiler/booleanAssignment.ts(4,1): error TS2322: Type '{}' is not a b = {}; // Error ~ !!! error TS2322: Type '{}' is not assignable to type 'Boolean'. -!!! error TS2322: Types of property 'valueOf' are incompatible. -!!! error TS2322: Type '() => Object' is not assignable to type '() => boolean'. -!!! error TS2322: Type 'Object' is not assignable to type 'boolean'. +!!! error TS2322: The types returned by 'valueOf()' are incompatible between these types. +!!! error TS2322: Type 'Object' is not assignable to type 'boolean'. var o = {}; o = b; // OK diff --git a/tests/baselines/reference/callSignatureAssignabilityInInheritance.errors.txt b/tests/baselines/reference/callSignatureAssignabilityInInheritance.errors.txt index 2ca3bb1aaf0..43589e94370 100644 --- a/tests/baselines/reference/callSignatureAssignabilityInInheritance.errors.txt +++ b/tests/baselines/reference/callSignatureAssignabilityInInheritance.errors.txt @@ -1,12 +1,10 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/callSignatureAssignabilityInInheritance.ts(57,15): error TS2430: Interface 'I2' incorrectly extends interface 'Base2'. - Types of property 'a' are incompatible. - Type '(x: number) => string' is not assignable to type '(x: number) => number'. - Type 'string' is not assignable to type 'number'. + The types returned by 'a(...)' are incompatible between these types. + Type 'string' is not assignable to type 'number'. tests/cases/conformance/types/typeRelationships/assignmentCompatibility/callSignatureAssignabilityInInheritance.ts(63,15): error TS2430: Interface 'I3' incorrectly extends interface 'Base2'. - Types of property 'a2' are incompatible. - Type '(x: T) => string' is not assignable to type '(x: T) => T'. - Type 'string' is not assignable to type 'T'. - 'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. + The types returned by 'a2(...)' are incompatible between these types. + Type 'string' is not assignable to type 'T'. + 'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. ==== tests/cases/conformance/types/typeRelationships/assignmentCompatibility/callSignatureAssignabilityInInheritance.ts (2 errors) ==== @@ -69,9 +67,8 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/callSign interface I2 extends Base2 { ~~ !!! error TS2430: Interface 'I2' incorrectly extends interface 'Base2'. -!!! error TS2430: Types of property 'a' are incompatible. -!!! error TS2430: Type '(x: number) => string' is not assignable to type '(x: number) => number'. -!!! error TS2430: Type 'string' is not assignable to type 'number'. +!!! error TS2430: The types returned by 'a(...)' are incompatible between these types. +!!! error TS2430: Type 'string' is not assignable to type 'number'. // N's a: (x: number) => string; // error because base returns non-void; } @@ -80,10 +77,9 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/callSign interface I3 extends Base2 { ~~ !!! error TS2430: Interface 'I3' incorrectly extends interface 'Base2'. -!!! error TS2430: Types of property 'a2' are incompatible. -!!! error TS2430: Type '(x: T) => string' is not assignable to type '(x: T) => T'. -!!! error TS2430: Type 'string' is not assignable to type 'T'. -!!! error TS2430: 'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. +!!! error TS2430: The types returned by 'a2(...)' are incompatible between these types. +!!! error TS2430: Type 'string' is not assignable to type 'T'. +!!! error TS2430: 'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. // N's a2: (x: T) => string; // error because base returns non-void; } diff --git a/tests/baselines/reference/callSignatureAssignabilityInInheritance3.errors.txt b/tests/baselines/reference/callSignatureAssignabilityInInheritance3.errors.txt index 671c09b3d79..287875d44da 100644 --- a/tests/baselines/reference/callSignatureAssignabilityInInheritance3.errors.txt +++ b/tests/baselines/reference/callSignatureAssignabilityInInheritance3.errors.txt @@ -27,16 +27,14 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/callSign Types of property 'a' are incompatible. Type 'string' is not assignable to type 'Base'. tests/cases/conformance/types/typeRelationships/assignmentCompatibility/callSignatureAssignabilityInInheritance3.ts(100,19): error TS2430: Interface 'I6' incorrectly extends interface 'B'. - Types of property 'a2' are incompatible. - Type '(x: T) => string[]' is not assignable to type '(x: T) => T[]'. - Type 'string[]' is not assignable to type 'T[]'. - Type 'string' is not assignable to type 'T'. - 'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. + The types returned by 'a2(...)' are incompatible between these types. + Type 'string[]' is not assignable to type 'T[]'. + Type 'string' is not assignable to type 'T'. + 'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. tests/cases/conformance/types/typeRelationships/assignmentCompatibility/callSignatureAssignabilityInInheritance3.ts(109,19): error TS2430: Interface 'I7' incorrectly extends interface 'C'. - Types of property 'a2' are incompatible. - Type '(x: T) => T[]' is not assignable to type '(x: T) => string[]'. - Type 'T[]' is not assignable to type 'string[]'. - Type 'T' is not assignable to type 'string'. + The types returned by 'a2(...)' are incompatible between these types. + Type 'T[]' is not assignable to type 'string[]'. + Type 'T' is not assignable to type 'string'. ==== tests/cases/conformance/types/typeRelationships/assignmentCompatibility/callSignatureAssignabilityInInheritance3.ts (6 errors) ==== @@ -174,11 +172,10 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/callSign interface I6 extends B { ~~ !!! error TS2430: Interface 'I6' incorrectly extends interface 'B'. -!!! error TS2430: Types of property 'a2' are incompatible. -!!! error TS2430: Type '(x: T) => string[]' is not assignable to type '(x: T) => T[]'. -!!! error TS2430: Type 'string[]' is not assignable to type 'T[]'. -!!! error TS2430: Type 'string' is not assignable to type 'T'. -!!! error TS2430: 'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. +!!! error TS2430: The types returned by 'a2(...)' are incompatible between these types. +!!! error TS2430: Type 'string[]' is not assignable to type 'T[]'. +!!! error TS2430: Type 'string' is not assignable to type 'T'. +!!! error TS2430: 'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. a2: (x: T) => string[]; // error } @@ -190,10 +187,9 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/callSign interface I7 extends C { ~~ !!! error TS2430: Interface 'I7' incorrectly extends interface 'C'. -!!! error TS2430: Types of property 'a2' are incompatible. -!!! error TS2430: Type '(x: T) => T[]' is not assignable to type '(x: T) => string[]'. -!!! error TS2430: Type 'T[]' is not assignable to type 'string[]'. -!!! error TS2430: Type 'T' is not assignable to type 'string'. +!!! error TS2430: The types returned by 'a2(...)' are incompatible between these types. +!!! error TS2430: Type 'T[]' is not assignable to type 'string[]'. +!!! error TS2430: Type 'T' is not assignable to type 'string'. a2: (x: T) => T[]; // error } } diff --git a/tests/baselines/reference/checkJsxChildrenCanBeTupleType.errors.txt b/tests/baselines/reference/checkJsxChildrenCanBeTupleType.errors.txt index aeaeb56ea75..0780f4e92c1 100644 --- a/tests/baselines/reference/checkJsxChildrenCanBeTupleType.errors.txt +++ b/tests/baselines/reference/checkJsxChildrenCanBeTupleType.errors.txt @@ -1,10 +1,8 @@ tests/cases/conformance/jsx/checkJsxChildrenCanBeTupleType.tsx(17,18): error TS2769: No overload matches this call. Overload 1 of 2, '(props: Readonly): ResizablePanel', gave the following error. Type '{ children: [Element, Element, Element]; }' is not assignable to type 'Readonly'. - Types of property 'children' are incompatible. - Type '[Element, Element, Element]' is not assignable to type '[ReactNode, ReactNode]'. - Types of property 'length' are incompatible. - Type '3' is not assignable to type '2'. + The types of 'children.length' are incompatible between these types. + Type '3' is not assignable to type '2'. Overload 2 of 2, '(props: ResizablePanelProps, context?: any): ResizablePanel', gave the following error. Type '{ children: [Element, Element, Element]; }' is not assignable to type 'Readonly'. Types of property 'children' are incompatible. @@ -33,10 +31,8 @@ tests/cases/conformance/jsx/checkJsxChildrenCanBeTupleType.tsx(17,18): error TS2 !!! error TS2769: No overload matches this call. !!! error TS2769: Overload 1 of 2, '(props: Readonly): ResizablePanel', gave the following error. !!! error TS2769: Type '{ children: [Element, Element, Element]; }' is not assignable to type 'Readonly'. -!!! error TS2769: Types of property 'children' are incompatible. -!!! error TS2769: Type '[Element, Element, Element]' is not assignable to type '[ReactNode, ReactNode]'. -!!! error TS2769: Types of property 'length' are incompatible. -!!! error TS2769: Type '3' is not assignable to type '2'. +!!! error TS2769: The types of 'children.length' are incompatible between these types. +!!! error TS2769: Type '3' is not assignable to type '2'. !!! error TS2769: Overload 2 of 2, '(props: ResizablePanelProps, context?: any): ResizablePanel', gave the following error. !!! error TS2769: Type '{ children: [Element, Element, Element]; }' is not assignable to type 'Readonly'. !!! error TS2769: Types of property 'children' are incompatible. diff --git a/tests/baselines/reference/complexRecursiveCollections.errors.txt b/tests/baselines/reference/complexRecursiveCollections.errors.txt index 27bb24bc5cc..cd88e596fd1 100644 --- a/tests/baselines/reference/complexRecursiveCollections.errors.txt +++ b/tests/baselines/reference/complexRecursiveCollections.errors.txt @@ -1,18 +1,15 @@ tests/cases/compiler/immutable.ts(341,22): error TS2430: Interface 'Keyed' incorrectly extends interface 'Collection'. - Types of property 'toSeq' are incompatible. - Type '() => Keyed' is not assignable to type '() => this'. - Type 'Keyed' is not assignable to type 'this'. - 'Keyed' is assignable to the constraint of type 'this', but 'this' could be instantiated with a different subtype of constraint 'Keyed'. + The types returned by 'toSeq()' are incompatible between these types. + Type 'Keyed' is not assignable to type 'this'. + 'Keyed' is assignable to the constraint of type 'this', but 'this' could be instantiated with a different subtype of constraint 'Keyed'. tests/cases/compiler/immutable.ts(359,22): error TS2430: Interface 'Indexed' incorrectly extends interface 'Collection'. - Types of property 'toSeq' are incompatible. - Type '() => Indexed' is not assignable to type '() => this'. - Type 'Indexed' is not assignable to type 'this'. - 'Indexed' is assignable to the constraint of type 'this', but 'this' could be instantiated with a different subtype of constraint 'Indexed'. + The types returned by 'toSeq()' are incompatible between these types. + Type 'Indexed' is not assignable to type 'this'. + 'Indexed' is assignable to the constraint of type 'this', but 'this' could be instantiated with a different subtype of constraint 'Indexed'. tests/cases/compiler/immutable.ts(391,22): error TS2430: Interface 'Set' incorrectly extends interface 'Collection'. - Types of property 'toSeq' are incompatible. - Type '() => Set' is not assignable to type '() => this'. - Type 'Set' is not assignable to type 'this'. - 'Set' is assignable to the constraint of type 'this', but 'this' could be instantiated with a different subtype of constraint 'Set'. + The types returned by 'toSeq()' are incompatible between these types. + Type 'Set' is not assignable to type 'this'. + 'Set' is assignable to the constraint of type 'this', but 'this' could be instantiated with a different subtype of constraint 'Set'. ==== tests/cases/compiler/complex.ts (0 errors) ==== @@ -380,10 +377,9 @@ tests/cases/compiler/immutable.ts(391,22): error TS2430: Interface 'Set' inco export interface Keyed extends Collection { ~~~~~ !!! error TS2430: Interface 'Keyed' incorrectly extends interface 'Collection'. -!!! error TS2430: Types of property 'toSeq' are incompatible. -!!! error TS2430: Type '() => Keyed' is not assignable to type '() => this'. -!!! error TS2430: Type 'Keyed' is not assignable to type 'this'. -!!! error TS2430: 'Keyed' is assignable to the constraint of type 'this', but 'this' could be instantiated with a different subtype of constraint 'Keyed'. +!!! error TS2430: The types returned by 'toSeq()' are incompatible between these types. +!!! error TS2430: Type 'Keyed' is not assignable to type 'this'. +!!! error TS2430: 'Keyed' is assignable to the constraint of type 'this', but 'this' could be instantiated with a different subtype of constraint 'Keyed'. toJS(): Object; toJSON(): { [key: string]: V }; toSeq(): Seq.Keyed; @@ -404,10 +400,9 @@ tests/cases/compiler/immutable.ts(391,22): error TS2430: Interface 'Set' inco export interface Indexed extends Collection { ~~~~~~~ !!! error TS2430: Interface 'Indexed' incorrectly extends interface 'Collection'. -!!! error TS2430: Types of property 'toSeq' are incompatible. -!!! error TS2430: Type '() => Indexed' is not assignable to type '() => this'. -!!! error TS2430: Type 'Indexed' is not assignable to type 'this'. -!!! error TS2430: 'Indexed' is assignable to the constraint of type 'this', but 'this' could be instantiated with a different subtype of constraint 'Indexed'. +!!! error TS2430: The types returned by 'toSeq()' are incompatible between these types. +!!! error TS2430: Type 'Indexed' is not assignable to type 'this'. +!!! error TS2430: 'Indexed' is assignable to the constraint of type 'this', but 'this' could be instantiated with a different subtype of constraint 'Indexed'. toJS(): Array; toJSON(): Array; // Reading values @@ -442,10 +437,9 @@ tests/cases/compiler/immutable.ts(391,22): error TS2430: Interface 'Set' inco export interface Set extends Collection { ~~~ !!! error TS2430: Interface 'Set' incorrectly extends interface 'Collection'. -!!! error TS2430: Types of property 'toSeq' are incompatible. -!!! error TS2430: Type '() => Set' is not assignable to type '() => this'. -!!! error TS2430: Type 'Set' is not assignable to type 'this'. -!!! error TS2430: 'Set' is assignable to the constraint of type 'this', but 'this' could be instantiated with a different subtype of constraint 'Set'. +!!! error TS2430: The types returned by 'toSeq()' are incompatible between these types. +!!! error TS2430: Type 'Set' is not assignable to type 'this'. +!!! error TS2430: 'Set' is assignable to the constraint of type 'this', but 'this' could be instantiated with a different subtype of constraint 'Set'. toJS(): Array; toJSON(): Array; toSeq(): Seq.Set; diff --git a/tests/baselines/reference/constructSignatureAssignabilityInInheritance.errors.txt b/tests/baselines/reference/constructSignatureAssignabilityInInheritance.errors.txt index e326d163c8c..1cc019746ba 100644 --- a/tests/baselines/reference/constructSignatureAssignabilityInInheritance.errors.txt +++ b/tests/baselines/reference/constructSignatureAssignabilityInInheritance.errors.txt @@ -1,12 +1,10 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/constructSignatureAssignabilityInInheritance.ts(61,15): error TS2430: Interface 'I2' incorrectly extends interface 'Base2'. - Types of property 'a' are incompatible. - Type 'new (x: number) => string' is not assignable to type 'new (x: number) => number'. - Type 'string' is not assignable to type 'number'. + The types returned by 'new a(...)' are incompatible between these types. + Type 'string' is not assignable to type 'number'. tests/cases/conformance/types/typeRelationships/assignmentCompatibility/constructSignatureAssignabilityInInheritance.ts(67,15): error TS2430: Interface 'I3' incorrectly extends interface 'Base2'. - Types of property 'a2' are incompatible. - Type 'new (x: T) => string' is not assignable to type 'new (x: T) => T'. - Type 'string' is not assignable to type 'T'. - 'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. + The types returned by 'new a2(...)' are incompatible between these types. + Type 'string' is not assignable to type 'T'. + 'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. ==== tests/cases/conformance/types/typeRelationships/assignmentCompatibility/constructSignatureAssignabilityInInheritance.ts (2 errors) ==== @@ -73,9 +71,8 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/construc interface I2 extends Base2 { ~~ !!! error TS2430: Interface 'I2' incorrectly extends interface 'Base2'. -!!! error TS2430: Types of property 'a' are incompatible. -!!! error TS2430: Type 'new (x: number) => string' is not assignable to type 'new (x: number) => number'. -!!! error TS2430: Type 'string' is not assignable to type 'number'. +!!! error TS2430: The types returned by 'new a(...)' are incompatible between these types. +!!! error TS2430: Type 'string' is not assignable to type 'number'. // N's a: new (x: number) => string; // error because base returns non-void; } @@ -84,10 +81,9 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/construc interface I3 extends Base2 { ~~ !!! error TS2430: Interface 'I3' incorrectly extends interface 'Base2'. -!!! error TS2430: Types of property 'a2' are incompatible. -!!! error TS2430: Type 'new (x: T) => string' is not assignable to type 'new (x: T) => T'. -!!! error TS2430: Type 'string' is not assignable to type 'T'. -!!! error TS2430: 'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. +!!! error TS2430: The types returned by 'new a2(...)' are incompatible between these types. +!!! error TS2430: Type 'string' is not assignable to type 'T'. +!!! error TS2430: 'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. // N's a2: new (x: T) => string; // error because base returns non-void; } diff --git a/tests/baselines/reference/constructSignatureAssignabilityInInheritance3.errors.txt b/tests/baselines/reference/constructSignatureAssignabilityInInheritance3.errors.txt index 59d8105c59c..d4c9e808200 100644 --- a/tests/baselines/reference/constructSignatureAssignabilityInInheritance3.errors.txt +++ b/tests/baselines/reference/constructSignatureAssignabilityInInheritance3.errors.txt @@ -27,16 +27,14 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/construc Types of property 'a' are incompatible. Type 'string' is not assignable to type 'Base'. tests/cases/conformance/types/typeRelationships/assignmentCompatibility/constructSignatureAssignabilityInInheritance3.ts(86,19): error TS2430: Interface 'I6' incorrectly extends interface 'B'. - Types of property 'a2' are incompatible. - Type 'new (x: T) => string[]' is not assignable to type 'new (x: T) => T[]'. - Type 'string[]' is not assignable to type 'T[]'. - Type 'string' is not assignable to type 'T'. - 'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. + The types returned by 'new a2(...)' are incompatible between these types. + Type 'string[]' is not assignable to type 'T[]'. + Type 'string' is not assignable to type 'T'. + 'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. tests/cases/conformance/types/typeRelationships/assignmentCompatibility/constructSignatureAssignabilityInInheritance3.ts(95,19): error TS2430: Interface 'I7' incorrectly extends interface 'C'. - Types of property 'a2' are incompatible. - Type 'new (x: T) => T[]' is not assignable to type 'new (x: T) => string[]'. - Type 'T[]' is not assignable to type 'string[]'. - Type 'T' is not assignable to type 'string'. + The types returned by 'new a2(...)' are incompatible between these types. + Type 'T[]' is not assignable to type 'string[]'. + Type 'T' is not assignable to type 'string'. ==== tests/cases/conformance/types/typeRelationships/assignmentCompatibility/constructSignatureAssignabilityInInheritance3.ts (6 errors) ==== @@ -160,11 +158,10 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/construc interface I6 extends B { ~~ !!! error TS2430: Interface 'I6' incorrectly extends interface 'B'. -!!! error TS2430: Types of property 'a2' are incompatible. -!!! error TS2430: Type 'new (x: T) => string[]' is not assignable to type 'new (x: T) => T[]'. -!!! error TS2430: Type 'string[]' is not assignable to type 'T[]'. -!!! error TS2430: Type 'string' is not assignable to type 'T'. -!!! error TS2430: 'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. +!!! error TS2430: The types returned by 'new a2(...)' are incompatible between these types. +!!! error TS2430: Type 'string[]' is not assignable to type 'T[]'. +!!! error TS2430: Type 'string' is not assignable to type 'T'. +!!! error TS2430: 'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. a2: new (x: T) => string[]; // error } @@ -176,10 +173,9 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/construc interface I7 extends C { ~~ !!! error TS2430: Interface 'I7' incorrectly extends interface 'C'. -!!! error TS2430: Types of property 'a2' are incompatible. -!!! error TS2430: Type 'new (x: T) => T[]' is not assignable to type 'new (x: T) => string[]'. -!!! error TS2430: Type 'T[]' is not assignable to type 'string[]'. -!!! error TS2430: Type 'T' is not assignable to type 'string'. +!!! error TS2430: The types returned by 'new a2(...)' are incompatible between these types. +!!! error TS2430: Type 'T[]' is not assignable to type 'string[]'. +!!! error TS2430: Type 'T' is not assignable to type 'string'. a2: new (x: T) => T[]; // error } diff --git a/tests/baselines/reference/covariantCallbacks.errors.txt b/tests/baselines/reference/covariantCallbacks.errors.txt index 9ee5a9e2fed..401bf454baa 100644 --- a/tests/baselines/reference/covariantCallbacks.errors.txt +++ b/tests/baselines/reference/covariantCallbacks.errors.txt @@ -10,9 +10,8 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/covarian Type 'A' is not assignable to type 'B'. tests/cases/conformance/types/typeRelationships/assignmentCompatibility/covariantCallbacks.ts(43,5): error TS2322: Type 'AList2' is not assignable to type 'BList2'. Types of property 'forEach' are incompatible. - Type '(cb: (item: A) => boolean) => void' is not assignable to type '(cb: (item: A) => void) => void'. - Types of parameters 'cb' and 'cb' are incompatible. - Type 'void' is not assignable to type 'boolean'. + Types of parameters 'cb' and 'cb' are incompatible. + Type 'void' is not assignable to type 'boolean'. tests/cases/conformance/types/typeRelationships/assignmentCompatibility/covariantCallbacks.ts(56,5): error TS2322: Type 'AList3' is not assignable to type 'BList3'. Types of property 'forEach' are incompatible. Type '(cb: (item: A) => void) => void' is not assignable to type '(cb: (item: A, context: any) => void) => void'. @@ -86,9 +85,8 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/covarian ~ !!! error TS2322: Type 'AList2' is not assignable to type 'BList2'. !!! error TS2322: Types of property 'forEach' are incompatible. -!!! error TS2322: Type '(cb: (item: A) => boolean) => void' is not assignable to type '(cb: (item: A) => void) => void'. -!!! error TS2322: Types of parameters 'cb' and 'cb' are incompatible. -!!! error TS2322: Type 'void' is not assignable to type 'boolean'. +!!! error TS2322: Types of parameters 'cb' and 'cb' are incompatible. +!!! error TS2322: Type 'void' is not assignable to type 'boolean'. } interface AList3 { diff --git a/tests/baselines/reference/decoratorCallGeneric.errors.txt b/tests/baselines/reference/decoratorCallGeneric.errors.txt index c2e4042743e..495160a191b 100644 --- a/tests/baselines/reference/decoratorCallGeneric.errors.txt +++ b/tests/baselines/reference/decoratorCallGeneric.errors.txt @@ -1,7 +1,6 @@ tests/cases/conformance/decorators/decoratorCallGeneric.ts(7,2): error TS2345: Argument of type 'typeof C' is not assignable to parameter of type 'I'. - Types of property 'm' are incompatible. - Type '() => void' is not assignable to type '() => C'. - Type 'void' is not assignable to type 'C'. + The types returned by 'm()' are incompatible between these types. + Type 'void' is not assignable to type 'C'. ==== tests/cases/conformance/decorators/decoratorCallGeneric.ts (1 errors) ==== @@ -14,9 +13,8 @@ tests/cases/conformance/decorators/decoratorCallGeneric.ts(7,2): error TS2345: A @dec ~~~ !!! error TS2345: Argument of type 'typeof C' is not assignable to parameter of type 'I'. -!!! error TS2345: Types of property 'm' are incompatible. -!!! error TS2345: Type '() => void' is not assignable to type '() => C'. -!!! error TS2345: Type 'void' is not assignable to type 'C'. +!!! error TS2345: The types returned by 'm()' are incompatible between these types. +!!! error TS2345: Type 'void' is not assignable to type 'C'. class C { _brand: any; static m() {} diff --git a/tests/baselines/reference/deepExcessPropertyCheckingWhenTargetIsIntersection.errors.txt b/tests/baselines/reference/deepExcessPropertyCheckingWhenTargetIsIntersection.errors.txt index 6a83638ba90..1f2552d47f2 100644 --- a/tests/baselines/reference/deepExcessPropertyCheckingWhenTargetIsIntersection.errors.txt +++ b/tests/baselines/reference/deepExcessPropertyCheckingWhenTargetIsIntersection.errors.txt @@ -1,10 +1,8 @@ tests/cases/compiler/deepExcessPropertyCheckingWhenTargetIsIntersection.ts(21,33): error TS2322: Type '{ INVALID_PROP_NAME: string; ariaLabel: string; }' is not assignable to type 'ITestProps'. Object literal may only specify known properties, and 'INVALID_PROP_NAME' does not exist in type 'ITestProps'. -tests/cases/compiler/deepExcessPropertyCheckingWhenTargetIsIntersection.ts(27,34): error TS2326: Types of property 'icon' are incompatible. - Type '{ props: { INVALID_PROP_NAME: string; ariaLabel: string; }; }' is not assignable to type 'NestedProp'. - Types of property 'props' are incompatible. - Type '{ INVALID_PROP_NAME: string; ariaLabel: string; }' is not assignable to type 'ITestProps'. - Object literal may only specify known properties, and 'INVALID_PROP_NAME' does not exist in type 'ITestProps'. +tests/cases/compiler/deepExcessPropertyCheckingWhenTargetIsIntersection.ts(27,34): error TS2200: The types of 'icon.props' are incompatible between these types. + Type '{ INVALID_PROP_NAME: string; ariaLabel: string; }' is not assignable to type 'ITestProps'. + Object literal may only specify known properties, and 'INVALID_PROP_NAME' does not exist in type 'ITestProps'. ==== tests/cases/compiler/deepExcessPropertyCheckingWhenTargetIsIntersection.ts (2 errors) ==== @@ -40,9 +38,7 @@ tests/cases/compiler/deepExcessPropertyCheckingWhenTargetIsIntersection.ts(27,34 TestComponent2({icon: { props: { INVALID_PROP_NAME: 'share', ariaLabel: 'test label' } }}); ~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2326: Types of property 'icon' are incompatible. -!!! error TS2326: Type '{ props: { INVALID_PROP_NAME: string; ariaLabel: string; }; }' is not assignable to type 'NestedProp'. -!!! error TS2326: Types of property 'props' are incompatible. -!!! error TS2326: Type '{ INVALID_PROP_NAME: string; ariaLabel: string; }' is not assignable to type 'ITestProps'. -!!! error TS2326: Object literal may only specify known properties, and 'INVALID_PROP_NAME' does not exist in type 'ITestProps'. +!!! error TS2200: The types of 'icon.props' are incompatible between these types. +!!! error TS2200: Type '{ INVALID_PROP_NAME: string; ariaLabel: string; }' is not assignable to type 'ITestProps'. +!!! error TS2200: Object literal may only specify known properties, and 'INVALID_PROP_NAME' does not exist in type 'ITestProps'. \ No newline at end of file diff --git a/tests/baselines/reference/deeplyNestedAssignabilityErrorsCombined.errors.txt b/tests/baselines/reference/deeplyNestedAssignabilityErrorsCombined.errors.txt new file mode 100644 index 00000000000..6245d06d2b7 --- /dev/null +++ b/tests/baselines/reference/deeplyNestedAssignabilityErrorsCombined.errors.txt @@ -0,0 +1,32 @@ +tests/cases/compiler/deeplyNestedAssignabilityErrorsCombined.ts(3,1): error TS2322: Type '{ a: { b: { c: { d: { e: { f(): { g: number; }; }; }; }; }; }; }' is not assignable to type '{ a: { b: { c: { d: { e: { f(): { g: string; }; }; }; }; }; }; }'. + The types of 'a.b.c.d.e.f().g' are incompatible between these types. + Type 'number' is not assignable to type 'string'. +tests/cases/compiler/deeplyNestedAssignabilityErrorsCombined.ts(15,1): error TS2322: Type '{ a: { b: { c: { d: { e: { f: typeof Ctor2; }; }; }; }; }; }' is not assignable to type '{ a: { b: { c: { d: { e: { f: typeof Ctor1; }; }; }; }; }; }'. + The types of '(new a.b.c.d.e.f()).g' are incompatible between these types. + Type 'number' is not assignable to type 'string'. + + +==== tests/cases/compiler/deeplyNestedAssignabilityErrorsCombined.ts (2 errors) ==== + let x = { a: { b: { c: { d: { e: { f() { return { g: "hello" }; } } } } } } }; + let y = { a: { b: { c: { d: { e: { f() { return { g: 12345 }; } } } } } } }; + x = y; + ~ +!!! error TS2322: Type '{ a: { b: { c: { d: { e: { f(): { g: number; }; }; }; }; }; }; }' is not assignable to type '{ a: { b: { c: { d: { e: { f(): { g: string; }; }; }; }; }; }; }'. +!!! error TS2322: The types of 'a.b.c.d.e.f().g' are incompatible between these types. +!!! error TS2322: Type 'number' is not assignable to type 'string'. + + class Ctor1 { + g = "ok" + } + + class Ctor2 { + g = 12; + } + + let x2 = { a: { b: { c: { d: { e: { f: Ctor1 } } } } } }; + let y2 = { a: { b: { c: { d: { e: { f: Ctor2 } } } } } }; + x2 = y2; + ~~ +!!! error TS2322: Type '{ a: { b: { c: { d: { e: { f: typeof Ctor2; }; }; }; }; }; }' is not assignable to type '{ a: { b: { c: { d: { e: { f: typeof Ctor1; }; }; }; }; }; }'. +!!! error TS2322: The types of '(new a.b.c.d.e.f()).g' are incompatible between these types. +!!! error TS2322: Type 'number' is not assignable to type 'string'. \ No newline at end of file diff --git a/tests/baselines/reference/deeplyNestedAssignabilityErrorsCombined.js b/tests/baselines/reference/deeplyNestedAssignabilityErrorsCombined.js new file mode 100644 index 00000000000..01bdcc7f253 --- /dev/null +++ b/tests/baselines/reference/deeplyNestedAssignabilityErrorsCombined.js @@ -0,0 +1,36 @@ +//// [deeplyNestedAssignabilityErrorsCombined.ts] +let x = { a: { b: { c: { d: { e: { f() { return { g: "hello" }; } } } } } } }; +let y = { a: { b: { c: { d: { e: { f() { return { g: 12345 }; } } } } } } }; +x = y; + +class Ctor1 { + g = "ok" +} + +class Ctor2 { + g = 12; +} + +let x2 = { a: { b: { c: { d: { e: { f: Ctor1 } } } } } }; +let y2 = { a: { b: { c: { d: { e: { f: Ctor2 } } } } } }; +x2 = y2; + +//// [deeplyNestedAssignabilityErrorsCombined.js] +var x = { a: { b: { c: { d: { e: { f: function () { return { g: "hello" }; } } } } } } }; +var y = { a: { b: { c: { d: { e: { f: function () { return { g: 12345 }; } } } } } } }; +x = y; +var Ctor1 = /** @class */ (function () { + function Ctor1() { + this.g = "ok"; + } + return Ctor1; +}()); +var Ctor2 = /** @class */ (function () { + function Ctor2() { + this.g = 12; + } + return Ctor2; +}()); +var x2 = { a: { b: { c: { d: { e: { f: Ctor1 } } } } } }; +var y2 = { a: { b: { c: { d: { e: { f: Ctor2 } } } } } }; +x2 = y2; diff --git a/tests/baselines/reference/deeplyNestedAssignabilityErrorsCombined.symbols b/tests/baselines/reference/deeplyNestedAssignabilityErrorsCombined.symbols new file mode 100644 index 00000000000..12333869923 --- /dev/null +++ b/tests/baselines/reference/deeplyNestedAssignabilityErrorsCombined.symbols @@ -0,0 +1,63 @@ +=== tests/cases/compiler/deeplyNestedAssignabilityErrorsCombined.ts === +let x = { a: { b: { c: { d: { e: { f() { return { g: "hello" }; } } } } } } }; +>x : Symbol(x, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 0, 3)) +>a : Symbol(a, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 0, 9)) +>b : Symbol(b, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 0, 14)) +>c : Symbol(c, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 0, 19)) +>d : Symbol(d, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 0, 24)) +>e : Symbol(e, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 0, 29)) +>f : Symbol(f, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 0, 34)) +>g : Symbol(g, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 0, 49)) + +let y = { a: { b: { c: { d: { e: { f() { return { g: 12345 }; } } } } } } }; +>y : Symbol(y, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 1, 3)) +>a : Symbol(a, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 1, 9)) +>b : Symbol(b, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 1, 14)) +>c : Symbol(c, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 1, 19)) +>d : Symbol(d, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 1, 24)) +>e : Symbol(e, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 1, 29)) +>f : Symbol(f, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 1, 34)) +>g : Symbol(g, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 1, 49)) + +x = y; +>x : Symbol(x, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 0, 3)) +>y : Symbol(y, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 1, 3)) + +class Ctor1 { +>Ctor1 : Symbol(Ctor1, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 2, 6)) + + g = "ok" +>g : Symbol(Ctor1.g, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 4, 13)) +} + +class Ctor2 { +>Ctor2 : Symbol(Ctor2, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 6, 1)) + + g = 12; +>g : Symbol(Ctor2.g, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 8, 13)) +} + +let x2 = { a: { b: { c: { d: { e: { f: Ctor1 } } } } } }; +>x2 : Symbol(x2, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 12, 3)) +>a : Symbol(a, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 12, 10)) +>b : Symbol(b, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 12, 15)) +>c : Symbol(c, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 12, 20)) +>d : Symbol(d, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 12, 25)) +>e : Symbol(e, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 12, 30)) +>f : Symbol(f, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 12, 35)) +>Ctor1 : Symbol(Ctor1, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 2, 6)) + +let y2 = { a: { b: { c: { d: { e: { f: Ctor2 } } } } } }; +>y2 : Symbol(y2, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 13, 3)) +>a : Symbol(a, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 13, 10)) +>b : Symbol(b, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 13, 15)) +>c : Symbol(c, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 13, 20)) +>d : Symbol(d, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 13, 25)) +>e : Symbol(e, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 13, 30)) +>f : Symbol(f, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 13, 35)) +>Ctor2 : Symbol(Ctor2, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 6, 1)) + +x2 = y2; +>x2 : Symbol(x2, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 12, 3)) +>y2 : Symbol(y2, Decl(deeplyNestedAssignabilityErrorsCombined.ts, 13, 3)) + diff --git a/tests/baselines/reference/deeplyNestedAssignabilityErrorsCombined.types b/tests/baselines/reference/deeplyNestedAssignabilityErrorsCombined.types new file mode 100644 index 00000000000..565de61ef08 --- /dev/null +++ b/tests/baselines/reference/deeplyNestedAssignabilityErrorsCombined.types @@ -0,0 +1,95 @@ +=== tests/cases/compiler/deeplyNestedAssignabilityErrorsCombined.ts === +let x = { a: { b: { c: { d: { e: { f() { return { g: "hello" }; } } } } } } }; +>x : { a: { b: { c: { d: { e: { f(): { g: string; }; }; }; }; }; }; } +>{ a: { b: { c: { d: { e: { f() { return { g: "hello" }; } } } } } } } : { a: { b: { c: { d: { e: { f(): { g: string; }; }; }; }; }; }; } +>a : { b: { c: { d: { e: { f(): { g: string; }; }; }; }; }; } +>{ b: { c: { d: { e: { f() { return { g: "hello" }; } } } } } } : { b: { c: { d: { e: { f(): { g: string; }; }; }; }; }; } +>b : { c: { d: { e: { f(): { g: string; }; }; }; }; } +>{ c: { d: { e: { f() { return { g: "hello" }; } } } } } : { c: { d: { e: { f(): { g: string; }; }; }; }; } +>c : { d: { e: { f(): { g: string; }; }; }; } +>{ d: { e: { f() { return { g: "hello" }; } } } } : { d: { e: { f(): { g: string; }; }; }; } +>d : { e: { f(): { g: string; }; }; } +>{ e: { f() { return { g: "hello" }; } } } : { e: { f(): { g: string; }; }; } +>e : { f(): { g: string; }; } +>{ f() { return { g: "hello" }; } } : { f(): { g: string; }; } +>f : () => { g: string; } +>{ g: "hello" } : { g: string; } +>g : string +>"hello" : "hello" + +let y = { a: { b: { c: { d: { e: { f() { return { g: 12345 }; } } } } } } }; +>y : { a: { b: { c: { d: { e: { f(): { g: number; }; }; }; }; }; }; } +>{ a: { b: { c: { d: { e: { f() { return { g: 12345 }; } } } } } } } : { a: { b: { c: { d: { e: { f(): { g: number; }; }; }; }; }; }; } +>a : { b: { c: { d: { e: { f(): { g: number; }; }; }; }; }; } +>{ b: { c: { d: { e: { f() { return { g: 12345 }; } } } } } } : { b: { c: { d: { e: { f(): { g: number; }; }; }; }; }; } +>b : { c: { d: { e: { f(): { g: number; }; }; }; }; } +>{ c: { d: { e: { f() { return { g: 12345 }; } } } } } : { c: { d: { e: { f(): { g: number; }; }; }; }; } +>c : { d: { e: { f(): { g: number; }; }; }; } +>{ d: { e: { f() { return { g: 12345 }; } } } } : { d: { e: { f(): { g: number; }; }; }; } +>d : { e: { f(): { g: number; }; }; } +>{ e: { f() { return { g: 12345 }; } } } : { e: { f(): { g: number; }; }; } +>e : { f(): { g: number; }; } +>{ f() { return { g: 12345 }; } } : { f(): { g: number; }; } +>f : () => { g: number; } +>{ g: 12345 } : { g: number; } +>g : number +>12345 : 12345 + +x = y; +>x = y : { a: { b: { c: { d: { e: { f(): { g: number; }; }; }; }; }; }; } +>x : { a: { b: { c: { d: { e: { f(): { g: string; }; }; }; }; }; }; } +>y : { a: { b: { c: { d: { e: { f(): { g: number; }; }; }; }; }; }; } + +class Ctor1 { +>Ctor1 : Ctor1 + + g = "ok" +>g : string +>"ok" : "ok" +} + +class Ctor2 { +>Ctor2 : Ctor2 + + g = 12; +>g : number +>12 : 12 +} + +let x2 = { a: { b: { c: { d: { e: { f: Ctor1 } } } } } }; +>x2 : { a: { b: { c: { d: { e: { f: typeof Ctor1; }; }; }; }; }; } +>{ a: { b: { c: { d: { e: { f: Ctor1 } } } } } } : { a: { b: { c: { d: { e: { f: typeof Ctor1; }; }; }; }; }; } +>a : { b: { c: { d: { e: { f: typeof Ctor1; }; }; }; }; } +>{ b: { c: { d: { e: { f: Ctor1 } } } } } : { b: { c: { d: { e: { f: typeof Ctor1; }; }; }; }; } +>b : { c: { d: { e: { f: typeof Ctor1; }; }; }; } +>{ c: { d: { e: { f: Ctor1 } } } } : { c: { d: { e: { f: typeof Ctor1; }; }; }; } +>c : { d: { e: { f: typeof Ctor1; }; }; } +>{ d: { e: { f: Ctor1 } } } : { d: { e: { f: typeof Ctor1; }; }; } +>d : { e: { f: typeof Ctor1; }; } +>{ e: { f: Ctor1 } } : { e: { f: typeof Ctor1; }; } +>e : { f: typeof Ctor1; } +>{ f: Ctor1 } : { f: typeof Ctor1; } +>f : typeof Ctor1 +>Ctor1 : typeof Ctor1 + +let y2 = { a: { b: { c: { d: { e: { f: Ctor2 } } } } } }; +>y2 : { a: { b: { c: { d: { e: { f: typeof Ctor2; }; }; }; }; }; } +>{ a: { b: { c: { d: { e: { f: Ctor2 } } } } } } : { a: { b: { c: { d: { e: { f: typeof Ctor2; }; }; }; }; }; } +>a : { b: { c: { d: { e: { f: typeof Ctor2; }; }; }; }; } +>{ b: { c: { d: { e: { f: Ctor2 } } } } } : { b: { c: { d: { e: { f: typeof Ctor2; }; }; }; }; } +>b : { c: { d: { e: { f: typeof Ctor2; }; }; }; } +>{ c: { d: { e: { f: Ctor2 } } } } : { c: { d: { e: { f: typeof Ctor2; }; }; }; } +>c : { d: { e: { f: typeof Ctor2; }; }; } +>{ d: { e: { f: Ctor2 } } } : { d: { e: { f: typeof Ctor2; }; }; } +>d : { e: { f: typeof Ctor2; }; } +>{ e: { f: Ctor2 } } : { e: { f: typeof Ctor2; }; } +>e : { f: typeof Ctor2; } +>{ f: Ctor2 } : { f: typeof Ctor2; } +>f : typeof Ctor2 +>Ctor2 : typeof Ctor2 + +x2 = y2; +>x2 = y2 : { a: { b: { c: { d: { e: { f: typeof Ctor2; }; }; }; }; }; } +>x2 : { a: { b: { c: { d: { e: { f: typeof Ctor1; }; }; }; }; }; } +>y2 : { a: { b: { c: { d: { e: { f: typeof Ctor2; }; }; }; }; }; } + diff --git a/tests/baselines/reference/elaboratedErrorsOnNullableTargets01.errors.txt b/tests/baselines/reference/elaboratedErrorsOnNullableTargets01.errors.txt index dc7e3f10bd1..b84fb863ad8 100644 --- a/tests/baselines/reference/elaboratedErrorsOnNullableTargets01.errors.txt +++ b/tests/baselines/reference/elaboratedErrorsOnNullableTargets01.errors.txt @@ -1,9 +1,7 @@ tests/cases/compiler/elaboratedErrorsOnNullableTargets01.ts(4,1): error TS2322: Type '{ foo: { bar: number | undefined; }; }' is not assignable to type '{ foo: { bar: string | null; } | undefined; }'. - Types of property 'foo' are incompatible. - Type '{ bar: number | undefined; }' is not assignable to type '{ bar: string | null; }'. - Types of property 'bar' are incompatible. - Type 'number | undefined' is not assignable to type 'string | null'. - Type 'undefined' is not assignable to type 'string | null'. + The types of 'foo.bar' are incompatible between these types. + Type 'number | undefined' is not assignable to type 'string | null'. + Type 'undefined' is not assignable to type 'string | null'. tests/cases/compiler/elaboratedErrorsOnNullableTargets01.ts(6,1): error TS2322: Type '{ foo: { bar: string | null; } | undefined; } | null | undefined' is not assignable to type '{ foo: { bar: number | undefined; }; }'. Type 'undefined' is not assignable to type '{ foo: { bar: number | undefined; }; }'. @@ -15,11 +13,9 @@ tests/cases/compiler/elaboratedErrorsOnNullableTargets01.ts(6,1): error TS2322: x = y; ~ !!! error TS2322: Type '{ foo: { bar: number | undefined; }; }' is not assignable to type '{ foo: { bar: string | null; } | undefined; }'. -!!! error TS2322: Types of property 'foo' are incompatible. -!!! error TS2322: Type '{ bar: number | undefined; }' is not assignable to type '{ bar: string | null; }'. -!!! error TS2322: Types of property 'bar' are incompatible. -!!! error TS2322: Type 'number | undefined' is not assignable to type 'string | null'. -!!! error TS2322: Type 'undefined' is not assignable to type 'string | null'. +!!! error TS2322: The types of 'foo.bar' are incompatible between these types. +!!! error TS2322: Type 'number | undefined' is not assignable to type 'string | null'. +!!! error TS2322: Type 'undefined' is not assignable to type 'string | null'. y = x; ~ diff --git a/tests/baselines/reference/everyTypeWithAnnotationAndInvalidInitializer.errors.txt b/tests/baselines/reference/everyTypeWithAnnotationAndInvalidInitializer.errors.txt index 31848458a87..f552bb35ca4 100644 --- a/tests/baselines/reference/everyTypeWithAnnotationAndInvalidInitializer.errors.txt +++ b/tests/baselines/reference/everyTypeWithAnnotationAndInvalidInitializer.errors.txt @@ -17,9 +17,8 @@ tests/cases/conformance/statements/VariableStatements/everyTypeWithAnnotationAnd Type 'string' is not assignable to type 'number'. tests/cases/conformance/statements/VariableStatements/everyTypeWithAnnotationAndInvalidInitializer.ts(48,32): error TS2322: Type 'string' is not assignable to type 'number'. tests/cases/conformance/statements/VariableStatements/everyTypeWithAnnotationAndInvalidInitializer.ts(50,5): error TS2322: Type 'typeof N' is not assignable to type 'typeof M'. - Types of property 'A' are incompatible. - Type 'typeof N.A' is not assignable to type 'typeof M.A'. - Property 'name' is missing in type 'N.A' but required in type 'M.A'. + The types returned by 'new A()' are incompatible between these types. + Property 'name' is missing in type 'N.A' but required in type 'M.A'. tests/cases/conformance/statements/VariableStatements/everyTypeWithAnnotationAndInvalidInitializer.ts(51,5): error TS2322: Type 'N.A' is not assignable to type 'M.A'. tests/cases/conformance/statements/VariableStatements/everyTypeWithAnnotationAndInvalidInitializer.ts(52,5): error TS2322: Type '(x: number) => boolean' is not assignable to type '(x: number) => string'. Type 'boolean' is not assignable to type 'string'. @@ -112,9 +111,8 @@ tests/cases/conformance/statements/VariableStatements/everyTypeWithAnnotationAnd var aModule: typeof M = N; ~~~~~~~ !!! error TS2322: Type 'typeof N' is not assignable to type 'typeof M'. -!!! error TS2322: Types of property 'A' are incompatible. -!!! error TS2322: Type 'typeof N.A' is not assignable to type 'typeof M.A'. -!!! error TS2322: Property 'name' is missing in type 'N.A' but required in type 'M.A'. +!!! error TS2322: The types returned by 'new A()' are incompatible between these types. +!!! error TS2322: Property 'name' is missing in type 'N.A' but required in type 'M.A'. !!! related TS2728 tests/cases/conformance/statements/VariableStatements/everyTypeWithAnnotationAndInvalidInitializer.ts:20:9: 'name' is declared here. var aClassInModule: M.A = new N.A(); ~~~~~~~~~~~~~~ diff --git a/tests/baselines/reference/extendAndImplementTheSameBaseType2.errors.txt b/tests/baselines/reference/extendAndImplementTheSameBaseType2.errors.txt index 799fc7bb8b0..e23d9ebd282 100644 --- a/tests/baselines/reference/extendAndImplementTheSameBaseType2.errors.txt +++ b/tests/baselines/reference/extendAndImplementTheSameBaseType2.errors.txt @@ -1,7 +1,6 @@ tests/cases/compiler/extendAndImplementTheSameBaseType2.ts(7,7): error TS2720: Class 'D' incorrectly implements class 'C'. Did you mean to extend 'C' and inherit its members as a subclass? - Types of property 'bar' are incompatible. - Type '() => string' is not assignable to type '() => number'. - Type 'string' is not assignable to type 'number'. + The types returned by 'bar()' are incompatible between these types. + Type 'string' is not assignable to type 'number'. tests/cases/compiler/extendAndImplementTheSameBaseType2.ts(12,5): error TS2322: Type 'number' is not assignable to type 'string'. tests/cases/compiler/extendAndImplementTheSameBaseType2.ts(16,5): error TS2322: Type 'string' is not assignable to type 'number'. @@ -16,9 +15,8 @@ tests/cases/compiler/extendAndImplementTheSameBaseType2.ts(16,5): error TS2322: class D extends C implements C { ~ !!! error TS2720: Class 'D' incorrectly implements class 'C'. Did you mean to extend 'C' and inherit its members as a subclass? -!!! error TS2720: Types of property 'bar' are incompatible. -!!! error TS2720: Type '() => string' is not assignable to type '() => number'. -!!! error TS2720: Type 'string' is not assignable to type 'number'. +!!! error TS2720: The types returned by 'bar()' are incompatible between these types. +!!! error TS2720: Type 'string' is not assignable to type 'number'. baz() { } } diff --git a/tests/baselines/reference/for-of39.errors.txt b/tests/baselines/reference/for-of39.errors.txt index a36c1c28a8c..3ecdc46bcfc 100644 --- a/tests/baselines/reference/for-of39.errors.txt +++ b/tests/baselines/reference/for-of39.errors.txt @@ -1,18 +1,14 @@ tests/cases/conformance/es6/for-ofStatements/for-of39.ts(1,11): error TS2769: No overload matches this call. Overload 1 of 3, '(iterable: Iterable): Map', gave the following error. Argument of type '([string, number] | [string, true])[]' is not assignable to parameter of type 'Iterable'. - Types of property '[Symbol.iterator]' are incompatible. - Type '() => IterableIterator<[string, number] | [string, true]>' is not assignable to type '() => Iterator'. - Type 'IterableIterator<[string, number] | [string, true]>' is not assignable to type 'Iterator'. - Types of property 'next' are incompatible. - Type '(...args: [] | [undefined]) => IteratorResult<[string, number] | [string, true], any>' is not assignable to type '(...args: [] | [undefined]) => IteratorResult'. - Type 'IteratorResult<[string, number] | [string, true], any>' is not assignable to type 'IteratorResult'. - Type 'IteratorYieldResult<[string, number] | [string, true]>' is not assignable to type 'IteratorResult'. - Type 'IteratorYieldResult<[string, number] | [string, true]>' is not assignable to type 'IteratorYieldResult'. - Type '[string, number] | [string, true]' is not assignable to type 'readonly [string, boolean]'. - Type '[string, number]' is not assignable to type 'readonly [string, boolean]'. - Types of property '1' are incompatible. - Type 'number' is not assignable to type 'boolean'. + The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types. + Type 'IteratorResult<[string, number] | [string, true], any>' is not assignable to type 'IteratorResult'. + Type 'IteratorYieldResult<[string, number] | [string, true]>' is not assignable to type 'IteratorResult'. + Type 'IteratorYieldResult<[string, number] | [string, true]>' is not assignable to type 'IteratorYieldResult'. + Type '[string, number] | [string, true]' is not assignable to type 'readonly [string, boolean]'. + Type '[string, number]' is not assignable to type 'readonly [string, boolean]'. + Types of property '1' are incompatible. + Type 'number' is not assignable to type 'boolean'. Overload 2 of 3, '(entries?: readonly (readonly [string, boolean])[]): Map', gave the following error. Type 'number' is not assignable to type 'boolean'. @@ -23,18 +19,14 @@ tests/cases/conformance/es6/for-ofStatements/for-of39.ts(1,11): error TS2769: No !!! error TS2769: No overload matches this call. !!! error TS2769: Overload 1 of 3, '(iterable: Iterable): Map', gave the following error. !!! error TS2769: Argument of type '([string, number] | [string, true])[]' is not assignable to parameter of type 'Iterable'. -!!! error TS2769: Types of property '[Symbol.iterator]' are incompatible. -!!! error TS2769: Type '() => IterableIterator<[string, number] | [string, true]>' is not assignable to type '() => Iterator'. -!!! error TS2769: Type 'IterableIterator<[string, number] | [string, true]>' is not assignable to type 'Iterator'. -!!! error TS2769: Types of property 'next' are incompatible. -!!! error TS2769: Type '(...args: [] | [undefined]) => IteratorResult<[string, number] | [string, true], any>' is not assignable to type '(...args: [] | [undefined]) => IteratorResult'. -!!! error TS2769: Type 'IteratorResult<[string, number] | [string, true], any>' is not assignable to type 'IteratorResult'. -!!! error TS2769: Type 'IteratorYieldResult<[string, number] | [string, true]>' is not assignable to type 'IteratorResult'. -!!! error TS2769: Type 'IteratorYieldResult<[string, number] | [string, true]>' is not assignable to type 'IteratorYieldResult'. -!!! error TS2769: Type '[string, number] | [string, true]' is not assignable to type 'readonly [string, boolean]'. -!!! error TS2769: Type '[string, number]' is not assignable to type 'readonly [string, boolean]'. -!!! error TS2769: Types of property '1' are incompatible. -!!! error TS2769: Type 'number' is not assignable to type 'boolean'. +!!! error TS2769: The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types. +!!! error TS2769: Type 'IteratorResult<[string, number] | [string, true], any>' is not assignable to type 'IteratorResult'. +!!! error TS2769: Type 'IteratorYieldResult<[string, number] | [string, true]>' is not assignable to type 'IteratorResult'. +!!! error TS2769: Type 'IteratorYieldResult<[string, number] | [string, true]>' is not assignable to type 'IteratorYieldResult'. +!!! error TS2769: Type '[string, number] | [string, true]' is not assignable to type 'readonly [string, boolean]'. +!!! error TS2769: Type '[string, number]' is not assignable to type 'readonly [string, boolean]'. +!!! error TS2769: Types of property '1' are incompatible. +!!! error TS2769: Type 'number' is not assignable to type 'boolean'. !!! error TS2769: Overload 2 of 3, '(entries?: readonly (readonly [string, boolean])[]): Map', gave the following error. !!! error TS2769: Type 'number' is not assignable to type 'boolean'. for (var [k, v] of map) { diff --git a/tests/baselines/reference/generatorTypeCheck25.errors.txt b/tests/baselines/reference/generatorTypeCheck25.errors.txt index 365a19dfdd7..e37f1304963 100644 --- a/tests/baselines/reference/generatorTypeCheck25.errors.txt +++ b/tests/baselines/reference/generatorTypeCheck25.errors.txt @@ -1,15 +1,11 @@ tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck25.ts(4,5): error TS2322: Type '() => Generator' is not assignable to type '() => Iterable'. - Type 'Generator' is not assignable to type 'Iterable'. - Types of property '[Symbol.iterator]' are incompatible. - Type '() => Generator' is not assignable to type '() => Iterator'. - Type 'Generator' is not assignable to type 'Iterator'. - Types of property 'next' are incompatible. - Type '(...args: [] | [undefined]) => IteratorResult' is not assignable to type '(...args: [] | [undefined]) => IteratorResult'. - Type 'IteratorResult' is not assignable to type 'IteratorResult'. - Type 'IteratorYieldResult' is not assignable to type 'IteratorResult'. - Type 'IteratorYieldResult' is not assignable to type 'IteratorYieldResult'. - Type 'Bar | Baz' is not assignable to type 'Foo'. - Property 'x' is missing in type 'Baz' but required in type 'Foo'. + Call signature return types 'Generator' and 'Iterable' are incompatible. + The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types. + Type 'IteratorResult' is not assignable to type 'IteratorResult'. + Type 'IteratorYieldResult' is not assignable to type 'IteratorResult'. + Type 'IteratorYieldResult' is not assignable to type 'IteratorYieldResult'. + Type 'Bar | Baz' is not assignable to type 'Foo'. + Property 'x' is missing in type 'Baz' but required in type 'Foo'. ==== tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck25.ts (1 errors) ==== @@ -19,17 +15,13 @@ tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck25.ts(4,5): error var g3: () => Iterable = function* () { ~~ !!! error TS2322: Type '() => Generator' is not assignable to type '() => Iterable'. -!!! error TS2322: Type 'Generator' is not assignable to type 'Iterable'. -!!! error TS2322: Types of property '[Symbol.iterator]' are incompatible. -!!! error TS2322: Type '() => Generator' is not assignable to type '() => Iterator'. -!!! error TS2322: Type 'Generator' is not assignable to type 'Iterator'. -!!! error TS2322: Types of property 'next' are incompatible. -!!! error TS2322: Type '(...args: [] | [undefined]) => IteratorResult' is not assignable to type '(...args: [] | [undefined]) => IteratorResult'. -!!! error TS2322: Type 'IteratorResult' is not assignable to type 'IteratorResult'. -!!! error TS2322: Type 'IteratorYieldResult' is not assignable to type 'IteratorResult'. -!!! error TS2322: Type 'IteratorYieldResult' is not assignable to type 'IteratorYieldResult'. -!!! error TS2322: Type 'Bar | Baz' is not assignable to type 'Foo'. -!!! error TS2322: Property 'x' is missing in type 'Baz' but required in type 'Foo'. +!!! error TS2322: Call signature return types 'Generator' and 'Iterable' are incompatible. +!!! error TS2322: The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types. +!!! error TS2322: Type 'IteratorResult' is not assignable to type 'IteratorResult'. +!!! error TS2322: Type 'IteratorYieldResult' is not assignable to type 'IteratorResult'. +!!! error TS2322: Type 'IteratorYieldResult' is not assignable to type 'IteratorYieldResult'. +!!! error TS2322: Type 'Bar | Baz' is not assignable to type 'Foo'. +!!! error TS2322: Property 'x' is missing in type 'Baz' but required in type 'Foo'. !!! related TS2728 tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck25.ts:1:13: 'x' is declared here. yield; yield new Bar; diff --git a/tests/baselines/reference/generatorTypeCheck63.errors.txt b/tests/baselines/reference/generatorTypeCheck63.errors.txt index 60dfb089822..10cafbe0ced 100644 --- a/tests/baselines/reference/generatorTypeCheck63.errors.txt +++ b/tests/baselines/reference/generatorTypeCheck63.errors.txt @@ -1,11 +1,10 @@ tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck63.ts(24,61): error TS2345: Argument of type '(state: State) => Generator' is not assignable to parameter of type '(a: State) => IterableIterator'. - Type 'Generator' is not assignable to type 'IterableIterator'. - Types of property 'next' are incompatible. - Type '(...args: [] | [undefined]) => IteratorResult' is not assignable to type '(...args: [] | [undefined]) => IteratorResult'. - Type 'IteratorResult' is not assignable to type 'IteratorResult'. - Type 'IteratorYieldResult' is not assignable to type 'IteratorResult'. - Type 'IteratorYieldResult' is not assignable to type 'IteratorYieldResult'. - Type 'number' is not assignable to type 'State'. + Call signature return types 'Generator' and 'IterableIterator' are incompatible. + The types returned by 'next(...)' are incompatible between these types. + Type 'IteratorResult' is not assignable to type 'IteratorResult'. + Type 'IteratorYieldResult' is not assignable to type 'IteratorResult'. + Type 'IteratorYieldResult' is not assignable to type 'IteratorYieldResult'. + Type 'number' is not assignable to type 'State'. ==== tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck63.ts (1 errors) ==== @@ -35,13 +34,12 @@ tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck63.ts(24,61): err export const Nothing: Strategy = strategy("Nothing", function* (state: State) { ~~~~~~~~ !!! error TS2345: Argument of type '(state: State) => Generator' is not assignable to parameter of type '(a: State) => IterableIterator'. -!!! error TS2345: Type 'Generator' is not assignable to type 'IterableIterator'. -!!! error TS2345: Types of property 'next' are incompatible. -!!! error TS2345: Type '(...args: [] | [undefined]) => IteratorResult' is not assignable to type '(...args: [] | [undefined]) => IteratorResult'. -!!! error TS2345: Type 'IteratorResult' is not assignable to type 'IteratorResult'. -!!! error TS2345: Type 'IteratorYieldResult' is not assignable to type 'IteratorResult'. -!!! error TS2345: Type 'IteratorYieldResult' is not assignable to type 'IteratorYieldResult'. -!!! error TS2345: Type 'number' is not assignable to type 'State'. +!!! error TS2345: Call signature return types 'Generator' and 'IterableIterator' are incompatible. +!!! error TS2345: The types returned by 'next(...)' are incompatible between these types. +!!! error TS2345: Type 'IteratorResult' is not assignable to type 'IteratorResult'. +!!! error TS2345: Type 'IteratorYieldResult' is not assignable to type 'IteratorResult'. +!!! error TS2345: Type 'IteratorYieldResult' is not assignable to type 'IteratorYieldResult'. +!!! error TS2345: Type 'number' is not assignable to type 'State'. yield 1; return state; }); diff --git a/tests/baselines/reference/generatorTypeCheck8.errors.txt b/tests/baselines/reference/generatorTypeCheck8.errors.txt index 6261d419ddd..ca0b1e30366 100644 --- a/tests/baselines/reference/generatorTypeCheck8.errors.txt +++ b/tests/baselines/reference/generatorTypeCheck8.errors.txt @@ -1,10 +1,9 @@ tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck8.ts(2,17): error TS2322: Type 'Generator' is not assignable to type 'BadGenerator'. - Types of property 'next' are incompatible. - Type '(...args: [] | [undefined]) => IteratorResult' is not assignable to type '(...args: [] | [undefined]) => IteratorResult'. - Type 'IteratorResult' is not assignable to type 'IteratorResult'. - Type 'IteratorYieldResult' is not assignable to type 'IteratorResult'. - Type 'IteratorYieldResult' is not assignable to type 'IteratorYieldResult'. - Type 'string' is not assignable to type 'number'. + The types returned by 'next(...)' are incompatible between these types. + Type 'IteratorResult' is not assignable to type 'IteratorResult'. + Type 'IteratorYieldResult' is not assignable to type 'IteratorResult'. + Type 'IteratorYieldResult' is not assignable to type 'IteratorYieldResult'. + Type 'string' is not assignable to type 'number'. ==== tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck8.ts (1 errors) ==== @@ -12,9 +11,8 @@ tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck8.ts(2,17): error function* g3(): BadGenerator { } ~~~~~~~~~~~~ !!! error TS2322: Type 'Generator' is not assignable to type 'BadGenerator'. -!!! error TS2322: Types of property 'next' are incompatible. -!!! error TS2322: Type '(...args: [] | [undefined]) => IteratorResult' is not assignable to type '(...args: [] | [undefined]) => IteratorResult'. -!!! error TS2322: Type 'IteratorResult' is not assignable to type 'IteratorResult'. -!!! error TS2322: Type 'IteratorYieldResult' is not assignable to type 'IteratorResult'. -!!! error TS2322: Type 'IteratorYieldResult' is not assignable to type 'IteratorYieldResult'. -!!! error TS2322: Type 'string' is not assignable to type 'number'. \ No newline at end of file +!!! error TS2322: The types returned by 'next(...)' are incompatible between these types. +!!! error TS2322: Type 'IteratorResult' is not assignable to type 'IteratorResult'. +!!! error TS2322: Type 'IteratorYieldResult' is not assignable to type 'IteratorResult'. +!!! error TS2322: Type 'IteratorYieldResult' is not assignable to type 'IteratorYieldResult'. +!!! error TS2322: Type 'string' is not assignable to type 'number'. \ No newline at end of file diff --git a/tests/baselines/reference/generics4.errors.txt b/tests/baselines/reference/generics4.errors.txt index 06bfa122901..6181685983e 100644 --- a/tests/baselines/reference/generics4.errors.txt +++ b/tests/baselines/reference/generics4.errors.txt @@ -1,8 +1,7 @@ tests/cases/compiler/generics4.ts(7,1): error TS2322: Type 'C' is not assignable to type 'C'. Type 'Y' is not assignable to type 'X'. - Types of property 'f' are incompatible. - Type '() => boolean' is not assignable to type '() => string'. - Type 'boolean' is not assignable to type 'string'. + The types returned by 'f()' are incompatible between these types. + Type 'boolean' is not assignable to type 'string'. ==== tests/cases/compiler/generics4.ts (1 errors) ==== @@ -16,6 +15,5 @@ tests/cases/compiler/generics4.ts(7,1): error TS2322: Type 'C' is not assigna ~ !!! error TS2322: Type 'C' is not assignable to type 'C'. !!! error TS2322: Type 'Y' is not assignable to type 'X'. -!!! error TS2322: Types of property 'f' are incompatible. -!!! error TS2322: Type '() => boolean' is not assignable to type '() => string'. -!!! error TS2322: Type 'boolean' is not assignable to type 'string'. \ No newline at end of file +!!! error TS2322: The types returned by 'f()' are incompatible between these types. +!!! error TS2322: Type 'boolean' is not assignable to type 'string'. \ No newline at end of file diff --git a/tests/baselines/reference/incompatibleTypes.errors.txt b/tests/baselines/reference/incompatibleTypes.errors.txt index 0b5e587ad53..66d94bea79c 100644 --- a/tests/baselines/reference/incompatibleTypes.errors.txt +++ b/tests/baselines/reference/incompatibleTypes.errors.txt @@ -12,14 +12,12 @@ tests/cases/compiler/incompatibleTypes.ts(34,12): error TS2416: Property 'p1' in tests/cases/compiler/incompatibleTypes.ts(42,5): error TS2769: No overload matches this call. Overload 1 of 2, '(i: IFoo1): void', gave the following error. Argument of type 'C1' is not assignable to parameter of type 'IFoo1'. - Types of property 'p1' are incompatible. - Type '() => string' is not assignable to type '() => number'. - Type 'string' is not assignable to type 'number'. + The types returned by 'p1()' are incompatible between these types. + Type 'string' is not assignable to type 'number'. Overload 2 of 2, '(i: IFoo2): void', gave the following error. Argument of type 'C1' is not assignable to parameter of type 'IFoo2'. - Types of property 'p1' are incompatible. - Type '() => string' is not assignable to type '(s: string) => number'. - Type 'string' is not assignable to type 'number'. + The types returned by 'p1(...)' are incompatible between these types. + Type 'string' is not assignable to type 'number'. tests/cases/compiler/incompatibleTypes.ts(49,7): error TS2769: No overload matches this call. Overload 1 of 2, '(n: { a: { a: string; }; b: string; }): number', gave the following error. Argument of type '{ e: number; f: number; }' is not assignable to parameter of type '{ a: { a: string; }; b: string; }'. @@ -95,14 +93,12 @@ tests/cases/compiler/incompatibleTypes.ts(74,5): error TS2322: Type '(a: any) => !!! error TS2769: No overload matches this call. !!! error TS2769: Overload 1 of 2, '(i: IFoo1): void', gave the following error. !!! error TS2769: Argument of type 'C1' is not assignable to parameter of type 'IFoo1'. -!!! error TS2769: Types of property 'p1' are incompatible. -!!! error TS2769: Type '() => string' is not assignable to type '() => number'. -!!! error TS2769: Type 'string' is not assignable to type 'number'. +!!! error TS2769: The types returned by 'p1()' are incompatible between these types. +!!! error TS2769: Type 'string' is not assignable to type 'number'. !!! error TS2769: Overload 2 of 2, '(i: IFoo2): void', gave the following error. !!! error TS2769: Argument of type 'C1' is not assignable to parameter of type 'IFoo2'. -!!! error TS2769: Types of property 'p1' are incompatible. -!!! error TS2769: Type '() => string' is not assignable to type '(s: string) => number'. -!!! error TS2769: Type 'string' is not assignable to type 'number'. +!!! error TS2769: The types returned by 'p1(...)' are incompatible between these types. +!!! error TS2769: Type 'string' is not assignable to type 'number'. function of1(n: { a: { a: string; }; b: string; }): number; diff --git a/tests/baselines/reference/inheritedModuleMembersForClodule.errors.txt b/tests/baselines/reference/inheritedModuleMembersForClodule.errors.txt index 0b7e00d95ce..e2b744d5a25 100644 --- a/tests/baselines/reference/inheritedModuleMembersForClodule.errors.txt +++ b/tests/baselines/reference/inheritedModuleMembersForClodule.errors.txt @@ -1,7 +1,6 @@ tests/cases/compiler/inheritedModuleMembersForClodule.ts(7,7): error TS2417: Class static side 'typeof D' incorrectly extends base class static side 'typeof C'. - Types of property 'foo' are incompatible. - Type '() => number' is not assignable to type '() => string'. - Type 'number' is not assignable to type 'string'. + The types returned by 'foo()' are incompatible between these types. + Type 'number' is not assignable to type 'string'. ==== tests/cases/compiler/inheritedModuleMembersForClodule.ts (1 errors) ==== @@ -14,9 +13,8 @@ tests/cases/compiler/inheritedModuleMembersForClodule.ts(7,7): error TS2417: Cla class D extends C { ~ !!! error TS2417: Class static side 'typeof D' incorrectly extends base class static side 'typeof C'. -!!! error TS2417: Types of property 'foo' are incompatible. -!!! error TS2417: Type '() => number' is not assignable to type '() => string'. -!!! error TS2417: Type 'number' is not assignable to type 'string'. +!!! error TS2417: The types returned by 'foo()' are incompatible between these types. +!!! error TS2417: Type 'number' is not assignable to type 'string'. } module D { diff --git a/tests/baselines/reference/interfaceThatHidesBaseProperty2.errors.txt b/tests/baselines/reference/interfaceThatHidesBaseProperty2.errors.txt index f66e13c0d9d..779a160c5a6 100644 --- a/tests/baselines/reference/interfaceThatHidesBaseProperty2.errors.txt +++ b/tests/baselines/reference/interfaceThatHidesBaseProperty2.errors.txt @@ -1,8 +1,6 @@ tests/cases/conformance/interfaces/interfaceDeclarations/interfaceThatHidesBaseProperty2.ts(5,11): error TS2430: Interface 'Derived' incorrectly extends interface 'Base'. - Types of property 'x' are incompatible. - Type '{ a: string; }' is not assignable to type '{ a: number; }'. - Types of property 'a' are incompatible. - Type 'string' is not assignable to type 'number'. + The types of 'x.a' are incompatible between these types. + Type 'string' is not assignable to type 'number'. ==== tests/cases/conformance/interfaces/interfaceDeclarations/interfaceThatHidesBaseProperty2.ts (1 errors) ==== @@ -13,10 +11,8 @@ tests/cases/conformance/interfaces/interfaceDeclarations/interfaceThatHidesBaseP interface Derived extends Base { // error ~~~~~~~ !!! error TS2430: Interface 'Derived' incorrectly extends interface 'Base'. -!!! error TS2430: Types of property 'x' are incompatible. -!!! error TS2430: Type '{ a: string; }' is not assignable to type '{ a: number; }'. -!!! error TS2430: Types of property 'a' are incompatible. -!!! error TS2430: Type 'string' is not assignable to type 'number'. +!!! error TS2430: The types of 'x.a' are incompatible between these types. +!!! error TS2430: Type 'string' is not assignable to type 'number'. x: { a: string; }; diff --git a/tests/baselines/reference/interfaceWithMultipleBaseTypes.errors.txt b/tests/baselines/reference/interfaceWithMultipleBaseTypes.errors.txt index 138f3d92096..c885a7859d4 100644 --- a/tests/baselines/reference/interfaceWithMultipleBaseTypes.errors.txt +++ b/tests/baselines/reference/interfaceWithMultipleBaseTypes.errors.txt @@ -1,20 +1,14 @@ tests/cases/conformance/interfaces/interfaceDeclarations/interfaceWithMultipleBaseTypes.ts(21,11): error TS2430: Interface 'Derived2' incorrectly extends interface 'Base2'. - Types of property 'x' are incompatible. - Type '{ a: string; b: number; }' is not assignable to type '{ b: string; }'. - Types of property 'b' are incompatible. - Type 'number' is not assignable to type 'string'. + The types of 'x.b' are incompatible between these types. + Type 'number' is not assignable to type 'string'. tests/cases/conformance/interfaces/interfaceDeclarations/interfaceWithMultipleBaseTypes.ts(52,15): error TS2320: Interface 'Derived3' cannot simultaneously extend types 'Base1' and 'Base2'. Named property 'x' of types 'Base1' and 'Base2' are not identical. tests/cases/conformance/interfaces/interfaceDeclarations/interfaceWithMultipleBaseTypes.ts(54,15): error TS2430: Interface 'Derived4' incorrectly extends interface 'Base1'. - Types of property 'x' are incompatible. - Type '{ a: T; b: T; }' is not assignable to type '{ a: number; }'. - Types of property 'a' are incompatible. - Type 'T' is not assignable to type 'number'. + The types of 'x.a' are incompatible between these types. + Type 'T' is not assignable to type 'number'. tests/cases/conformance/interfaces/interfaceDeclarations/interfaceWithMultipleBaseTypes.ts(54,15): error TS2430: Interface 'Derived4' incorrectly extends interface 'Base2'. - Types of property 'x' are incompatible. - Type '{ a: T; b: T; }' is not assignable to type '{ b: number; }'. - Types of property 'b' are incompatible. - Type 'T' is not assignable to type 'number'. + The types of 'x.b' are incompatible between these types. + Type 'T' is not assignable to type 'number'. tests/cases/conformance/interfaces/interfaceDeclarations/interfaceWithMultipleBaseTypes.ts(60,15): error TS2430: Interface 'Derived5' incorrectly extends interface 'Base1'. Types of property 'x' are incompatible. Type 'T' is not assignable to type '{ a: T; }'. @@ -47,10 +41,8 @@ tests/cases/conformance/interfaces/interfaceDeclarations/interfaceWithMultipleBa interface Derived2 extends Base1, Base2 { // error ~~~~~~~~ !!! error TS2430: Interface 'Derived2' incorrectly extends interface 'Base2'. -!!! error TS2430: Types of property 'x' are incompatible. -!!! error TS2430: Type '{ a: string; b: number; }' is not assignable to type '{ b: string; }'. -!!! error TS2430: Types of property 'b' are incompatible. -!!! error TS2430: Type 'number' is not assignable to type 'string'. +!!! error TS2430: The types of 'x.b' are incompatible between these types. +!!! error TS2430: Type 'number' is not assignable to type 'string'. x: { a: string; b: number; } @@ -89,16 +81,12 @@ tests/cases/conformance/interfaces/interfaceDeclarations/interfaceWithMultipleBa interface Derived4 extends Base1, Base2 { // error ~~~~~~~~ !!! error TS2430: Interface 'Derived4' incorrectly extends interface 'Base1'. -!!! error TS2430: Types of property 'x' are incompatible. -!!! error TS2430: Type '{ a: T; b: T; }' is not assignable to type '{ a: number; }'. -!!! error TS2430: Types of property 'a' are incompatible. -!!! error TS2430: Type 'T' is not assignable to type 'number'. +!!! error TS2430: The types of 'x.a' are incompatible between these types. +!!! error TS2430: Type 'T' is not assignable to type 'number'. ~~~~~~~~ !!! error TS2430: Interface 'Derived4' incorrectly extends interface 'Base2'. -!!! error TS2430: Types of property 'x' are incompatible. -!!! error TS2430: Type '{ a: T; b: T; }' is not assignable to type '{ b: number; }'. -!!! error TS2430: Types of property 'b' are incompatible. -!!! error TS2430: Type 'T' is not assignable to type 'number'. +!!! error TS2430: The types of 'x.b' are incompatible between these types. +!!! error TS2430: Type 'T' is not assignable to type 'number'. x: { a: T; b: T; } diff --git a/tests/baselines/reference/interfaceWithMultipleBaseTypes2.errors.txt b/tests/baselines/reference/interfaceWithMultipleBaseTypes2.errors.txt index b3459233923..9b7b2eff8ec 100644 --- a/tests/baselines/reference/interfaceWithMultipleBaseTypes2.errors.txt +++ b/tests/baselines/reference/interfaceWithMultipleBaseTypes2.errors.txt @@ -1,8 +1,6 @@ tests/cases/conformance/interfaces/interfaceDeclarations/interfaceWithMultipleBaseTypes2.ts(17,11): error TS2430: Interface 'Derived2' incorrectly extends interface 'Base'. - Types of property 'x' are incompatible. - Type '{ a: number; b: string; }' is not assignable to type '{ a?: string; b: string; }'. - Types of property 'a' are incompatible. - Type 'number' is not assignable to type 'string'. + The types of 'x.a' are incompatible between these types. + Type 'number' is not assignable to type 'string'. ==== tests/cases/conformance/interfaces/interfaceDeclarations/interfaceWithMultipleBaseTypes2.ts (1 errors) ==== @@ -25,10 +23,8 @@ tests/cases/conformance/interfaces/interfaceDeclarations/interfaceWithMultipleBa interface Derived2 extends Base, Base2 { // error ~~~~~~~~ !!! error TS2430: Interface 'Derived2' incorrectly extends interface 'Base'. -!!! error TS2430: Types of property 'x' are incompatible. -!!! error TS2430: Type '{ a: number; b: string; }' is not assignable to type '{ a?: string; b: string; }'. -!!! error TS2430: Types of property 'a' are incompatible. -!!! error TS2430: Type 'number' is not assignable to type 'string'. +!!! error TS2430: The types of 'x.a' are incompatible between these types. +!!! error TS2430: Type 'number' is not assignable to type 'string'. x: { a: number; b: string } } diff --git a/tests/baselines/reference/invariantGenericErrorElaboration.errors.txt b/tests/baselines/reference/invariantGenericErrorElaboration.errors.txt index 0bd79bcf11d..7ab83c61bcd 100644 --- a/tests/baselines/reference/invariantGenericErrorElaboration.errors.txt +++ b/tests/baselines/reference/invariantGenericErrorElaboration.errors.txt @@ -1,13 +1,7 @@ tests/cases/compiler/invariantGenericErrorElaboration.ts(3,7): error TS2322: Type 'Num' is not assignable to type 'Runtype'. - Types of property 'constraint' are incompatible. - Type 'Constraint' is not assignable to type 'Constraint>'. - Types of property 'constraint' are incompatible. - Type 'Constraint>' is not assignable to type 'Constraint>>'. - Types of property 'constraint' are incompatible. - Type 'Constraint>>' is not assignable to type 'Constraint>>>'. - Type 'Constraint>>' is not assignable to type 'Constraint>'. - Types of property 'underlying' are incompatible. - Type 'Constraint>' is not assignable to type 'Constraint'. + The types of 'constraint.constraint.constraint' are incompatible between these types. + Type 'Constraint>>' is not assignable to type 'Constraint>>>'. + Type 'Constraint>' is not assignable to type 'Constraint'. tests/cases/compiler/invariantGenericErrorElaboration.ts(4,19): error TS2322: Type 'Num' is not assignable to type 'Runtype'. @@ -17,16 +11,9 @@ tests/cases/compiler/invariantGenericErrorElaboration.ts(4,19): error TS2322: Ty const wat: Runtype = Num; ~~~ !!! error TS2322: Type 'Num' is not assignable to type 'Runtype'. -!!! error TS2322: Types of property 'constraint' are incompatible. -!!! error TS2322: Type 'Constraint' is not assignable to type 'Constraint>'. -!!! error TS2322: Types of property 'constraint' are incompatible. -!!! error TS2322: Type 'Constraint>' is not assignable to type 'Constraint>>'. -!!! error TS2322: Types of property 'constraint' are incompatible. -!!! error TS2322: Type 'Constraint>>' is not assignable to type 'Constraint>>>'. -!!! error TS2322: Type 'Constraint>>' is not assignable to type 'Constraint>'. -!!! error TS2322: Types of property 'underlying' are incompatible. -!!! error TS2322: Type 'Constraint>' is not assignable to type 'Constraint'. -!!! related TS2728 tests/cases/compiler/invariantGenericErrorElaboration.ts:12:3: 'tag' is declared here. +!!! error TS2322: The types of 'constraint.constraint.constraint' are incompatible between these types. +!!! error TS2322: Type 'Constraint>>' is not assignable to type 'Constraint>>>'. +!!! error TS2322: Type 'Constraint>' is not assignable to type 'Constraint'. const Foo = Obj({ foo: Num }) ~~~ !!! error TS2322: Type 'Num' is not assignable to type 'Runtype'. diff --git a/tests/baselines/reference/iterableArrayPattern28.errors.txt b/tests/baselines/reference/iterableArrayPattern28.errors.txt index 7a1ff6cf42c..605c6feb405 100644 --- a/tests/baselines/reference/iterableArrayPattern28.errors.txt +++ b/tests/baselines/reference/iterableArrayPattern28.errors.txt @@ -1,18 +1,14 @@ tests/cases/conformance/es6/destructuring/iterableArrayPattern28.ts(2,24): error TS2769: No overload matches this call. Overload 1 of 3, '(iterable: Iterable): Map', gave the following error. Argument of type '([string, number] | [string, boolean])[]' is not assignable to parameter of type 'Iterable'. - Types of property '[Symbol.iterator]' are incompatible. - Type '() => IterableIterator<[string, number] | [string, boolean]>' is not assignable to type '() => Iterator'. - Type 'IterableIterator<[string, number] | [string, boolean]>' is not assignable to type 'Iterator'. - Types of property 'next' are incompatible. - Type '(...args: [] | [undefined]) => IteratorResult<[string, number] | [string, boolean], any>' is not assignable to type '(...args: [] | [undefined]) => IteratorResult'. - Type 'IteratorResult<[string, number] | [string, boolean], any>' is not assignable to type 'IteratorResult'. - Type 'IteratorYieldResult<[string, number] | [string, boolean]>' is not assignable to type 'IteratorResult'. - Type 'IteratorYieldResult<[string, number] | [string, boolean]>' is not assignable to type 'IteratorYieldResult'. - Type '[string, number] | [string, boolean]' is not assignable to type 'readonly [string, number]'. - Type '[string, boolean]' is not assignable to type 'readonly [string, number]'. - Types of property '1' are incompatible. - Type 'boolean' is not assignable to type 'number'. + The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types. + Type 'IteratorResult<[string, number] | [string, boolean], any>' is not assignable to type 'IteratorResult'. + Type 'IteratorYieldResult<[string, number] | [string, boolean]>' is not assignable to type 'IteratorResult'. + Type 'IteratorYieldResult<[string, number] | [string, boolean]>' is not assignable to type 'IteratorYieldResult'. + Type '[string, number] | [string, boolean]' is not assignable to type 'readonly [string, number]'. + Type '[string, boolean]' is not assignable to type 'readonly [string, number]'. + Types of property '1' are incompatible. + Type 'boolean' is not assignable to type 'number'. Overload 2 of 3, '(entries?: readonly (readonly [string, number])[]): Map', gave the following error. Type 'true' is not assignable to type 'number'. @@ -24,17 +20,13 @@ tests/cases/conformance/es6/destructuring/iterableArrayPattern28.ts(2,24): error !!! error TS2769: No overload matches this call. !!! error TS2769: Overload 1 of 3, '(iterable: Iterable): Map', gave the following error. !!! error TS2769: Argument of type '([string, number] | [string, boolean])[]' is not assignable to parameter of type 'Iterable'. -!!! error TS2769: Types of property '[Symbol.iterator]' are incompatible. -!!! error TS2769: Type '() => IterableIterator<[string, number] | [string, boolean]>' is not assignable to type '() => Iterator'. -!!! error TS2769: Type 'IterableIterator<[string, number] | [string, boolean]>' is not assignable to type 'Iterator'. -!!! error TS2769: Types of property 'next' are incompatible. -!!! error TS2769: Type '(...args: [] | [undefined]) => IteratorResult<[string, number] | [string, boolean], any>' is not assignable to type '(...args: [] | [undefined]) => IteratorResult'. -!!! error TS2769: Type 'IteratorResult<[string, number] | [string, boolean], any>' is not assignable to type 'IteratorResult'. -!!! error TS2769: Type 'IteratorYieldResult<[string, number] | [string, boolean]>' is not assignable to type 'IteratorResult'. -!!! error TS2769: Type 'IteratorYieldResult<[string, number] | [string, boolean]>' is not assignable to type 'IteratorYieldResult'. -!!! error TS2769: Type '[string, number] | [string, boolean]' is not assignable to type 'readonly [string, number]'. -!!! error TS2769: Type '[string, boolean]' is not assignable to type 'readonly [string, number]'. -!!! error TS2769: Types of property '1' are incompatible. -!!! error TS2769: Type 'boolean' is not assignable to type 'number'. +!!! error TS2769: The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types. +!!! error TS2769: Type 'IteratorResult<[string, number] | [string, boolean], any>' is not assignable to type 'IteratorResult'. +!!! error TS2769: Type 'IteratorYieldResult<[string, number] | [string, boolean]>' is not assignable to type 'IteratorResult'. +!!! error TS2769: Type 'IteratorYieldResult<[string, number] | [string, boolean]>' is not assignable to type 'IteratorYieldResult'. +!!! error TS2769: Type '[string, number] | [string, boolean]' is not assignable to type 'readonly [string, number]'. +!!! error TS2769: Type '[string, boolean]' is not assignable to type 'readonly [string, number]'. +!!! error TS2769: Types of property '1' are incompatible. +!!! error TS2769: Type 'boolean' is not assignable to type 'number'. !!! error TS2769: Overload 2 of 3, '(entries?: readonly (readonly [string, number])[]): Map', gave the following error. !!! error TS2769: Type 'true' is not assignable to type 'number'. \ No newline at end of file diff --git a/tests/baselines/reference/iteratorSpreadInArray6.errors.txt b/tests/baselines/reference/iteratorSpreadInArray6.errors.txt index 0b114bc6477..202c0ab3bf2 100644 --- a/tests/baselines/reference/iteratorSpreadInArray6.errors.txt +++ b/tests/baselines/reference/iteratorSpreadInArray6.errors.txt @@ -1,10 +1,9 @@ tests/cases/conformance/es6/spread/iteratorSpreadInArray6.ts(15,14): error TS2769: No overload matches this call. Overload 1 of 2, '(...items: ConcatArray[]): number[]', gave the following error. Argument of type 'symbol[]' is not assignable to parameter of type 'ConcatArray'. - Types of property 'slice' are incompatible. - Type '(start?: number, end?: number) => symbol[]' is not assignable to type '(start?: number, end?: number) => number[]'. - Type 'symbol[]' is not assignable to type 'number[]'. - Type 'symbol' is not assignable to type 'number'. + The types returned by 'slice(...)' are incompatible between these types. + Type 'symbol[]' is not assignable to type 'number[]'. + Type 'symbol' is not assignable to type 'number'. Overload 2 of 2, '(...items: (number | ConcatArray)[]): number[]', gave the following error. Argument of type 'symbol[]' is not assignable to parameter of type 'number | ConcatArray'. Type 'symbol[]' is not assignable to type 'ConcatArray'. @@ -30,10 +29,9 @@ tests/cases/conformance/es6/spread/iteratorSpreadInArray6.ts(15,14): error TS276 !!! error TS2769: No overload matches this call. !!! error TS2769: Overload 1 of 2, '(...items: ConcatArray[]): number[]', gave the following error. !!! error TS2769: Argument of type 'symbol[]' is not assignable to parameter of type 'ConcatArray'. -!!! error TS2769: Types of property 'slice' are incompatible. -!!! error TS2769: Type '(start?: number, end?: number) => symbol[]' is not assignable to type '(start?: number, end?: number) => number[]'. -!!! error TS2769: Type 'symbol[]' is not assignable to type 'number[]'. -!!! error TS2769: Type 'symbol' is not assignable to type 'number'. +!!! error TS2769: The types returned by 'slice(...)' are incompatible between these types. +!!! error TS2769: Type 'symbol[]' is not assignable to type 'number[]'. +!!! error TS2769: Type 'symbol' is not assignable to type 'number'. !!! error TS2769: Overload 2 of 2, '(...items: (number | ConcatArray)[]): number[]', gave the following error. !!! error TS2769: Argument of type 'symbol[]' is not assignable to parameter of type 'number | ConcatArray'. !!! error TS2769: Type 'symbol[]' is not assignable to type 'ConcatArray'. \ No newline at end of file diff --git a/tests/baselines/reference/mergedDeclarations7.errors.txt b/tests/baselines/reference/mergedDeclarations7.errors.txt index cd9b3a5e939..bc09ea081a3 100644 --- a/tests/baselines/reference/mergedDeclarations7.errors.txt +++ b/tests/baselines/reference/mergedDeclarations7.errors.txt @@ -1,7 +1,6 @@ tests/cases/compiler/test.ts(4,5): error TS2322: Type 'PassportStatic' is not assignable to type 'Passport'. - Types of property 'use' are incompatible. - Type '() => PassportStatic' is not assignable to type '() => this'. - Type 'PassportStatic' is not assignable to type 'this'. + The types returned by 'use()' are incompatible between these types. + Type 'PassportStatic' is not assignable to type 'this'. ==== tests/cases/compiler/passport.d.ts (0 errors) ==== @@ -27,6 +26,5 @@ tests/cases/compiler/test.ts(4,5): error TS2322: Type 'PassportStatic' is not as let p: Passport = passport.use(); ~ !!! error TS2322: Type 'PassportStatic' is not assignable to type 'Passport'. -!!! error TS2322: Types of property 'use' are incompatible. -!!! error TS2322: Type '() => PassportStatic' is not assignable to type '() => this'. -!!! error TS2322: Type 'PassportStatic' is not assignable to type 'this'. \ No newline at end of file +!!! error TS2322: The types returned by 'use()' are incompatible between these types. +!!! error TS2322: Type 'PassportStatic' is not assignable to type 'this'. \ No newline at end of file diff --git a/tests/baselines/reference/multiLineErrors.errors.txt b/tests/baselines/reference/multiLineErrors.errors.txt index 70366aeabed..851425baef4 100644 --- a/tests/baselines/reference/multiLineErrors.errors.txt +++ b/tests/baselines/reference/multiLineErrors.errors.txt @@ -1,9 +1,7 @@ tests/cases/compiler/multiLineErrors.ts(3,22): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. tests/cases/compiler/multiLineErrors.ts(21,1): error TS2322: Type 'A2' is not assignable to type 'A1'. - Types of property 'x' are incompatible. - Type '{ y: string; }' is not assignable to type '{ y: number; }'. - Types of property 'y' are incompatible. - Type 'string' is not assignable to type 'number'. + The types of 'x.y' are incompatible between these types. + Type 'string' is not assignable to type 'number'. ==== tests/cases/compiler/multiLineErrors.ts (2 errors) ==== @@ -35,8 +33,6 @@ tests/cases/compiler/multiLineErrors.ts(21,1): error TS2322: Type 'A2' is not as t1 = t2; ~~ !!! error TS2322: Type 'A2' is not assignable to type 'A1'. -!!! error TS2322: Types of property 'x' are incompatible. -!!! error TS2322: Type '{ y: string; }' is not assignable to type '{ y: number; }'. -!!! error TS2322: Types of property 'y' are incompatible. -!!! error TS2322: Type 'string' is not assignable to type 'number'. +!!! error TS2322: The types of 'x.y' are incompatible between these types. +!!! error TS2322: Type 'string' is not assignable to type 'number'. \ No newline at end of file diff --git a/tests/baselines/reference/mutuallyRecursiveCallbacks.errors.txt b/tests/baselines/reference/mutuallyRecursiveCallbacks.errors.txt index bb90bcee999..63a568bf10a 100644 --- a/tests/baselines/reference/mutuallyRecursiveCallbacks.errors.txt +++ b/tests/baselines/reference/mutuallyRecursiveCallbacks.errors.txt @@ -1,9 +1,8 @@ tests/cases/compiler/mutuallyRecursiveCallbacks.ts(7,1): error TS2322: Type '(bar: Bar) => void' is not assignable to type 'Bar<{}>'. Types of parameters 'bar' and 'foo' are incompatible. Types of parameters 'bar' and 'foo' are incompatible. - Type 'Foo' is not assignable to type 'Bar<{}>'. - Types of parameters 'bar' and 'foo' are incompatible. - Type 'void' is not assignable to type 'Foo'. + Types of parameters 'bar' and 'foo' are incompatible. + Type 'void' is not assignable to type 'Foo'. ==== tests/cases/compiler/mutuallyRecursiveCallbacks.ts (1 errors) ==== @@ -18,7 +17,6 @@ tests/cases/compiler/mutuallyRecursiveCallbacks.ts(7,1): error TS2322: Type ' !!! error TS2322: Type '(bar: Bar) => void' is not assignable to type 'Bar<{}>'. !!! error TS2322: Types of parameters 'bar' and 'foo' are incompatible. !!! error TS2322: Types of parameters 'bar' and 'foo' are incompatible. -!!! error TS2322: Type 'Foo' is not assignable to type 'Bar<{}>'. -!!! error TS2322: Types of parameters 'bar' and 'foo' are incompatible. -!!! error TS2322: Type 'void' is not assignable to type 'Foo'. +!!! error TS2322: Types of parameters 'bar' and 'foo' are incompatible. +!!! error TS2322: Type 'void' is not assignable to type 'Foo'. \ No newline at end of file diff --git a/tests/baselines/reference/nestedCallbackErrorNotFlattened.errors.txt b/tests/baselines/reference/nestedCallbackErrorNotFlattened.errors.txt new file mode 100644 index 00000000000..3749d3765b0 --- /dev/null +++ b/tests/baselines/reference/nestedCallbackErrorNotFlattened.errors.txt @@ -0,0 +1,20 @@ +tests/cases/compiler/nestedCallbackErrorNotFlattened.ts(6,1): error TS2322: Type '() => () => () => () => number' is not assignable to type '() => () => () => () => string'. + Call signature return types '() => () => () => number' and '() => () => () => string' are incompatible. + Call signature return types '() => () => number' and '() => () => string' are incompatible. + Call signature return types '() => number' and '() => string' are incompatible. + Type 'number' is not assignable to type 'string'. + + +==== tests/cases/compiler/nestedCallbackErrorNotFlattened.ts (1 errors) ==== + type Cb = {noAlias: () => T}["noAlias"]; // `"noAlias"` here prevents an alias symbol from being made + // which means the comparison will definitely be structural, rather than by variance + + declare const x: Cb>>>; // one more layer of `Cb` adn we'd get a `true` from the deeply-nested symbol check + declare let y: Cb>>>; + y = x; + ~ +!!! error TS2322: Type '() => () => () => () => number' is not assignable to type '() => () => () => () => string'. +!!! error TS2322: Call signature return types '() => () => () => number' and '() => () => () => string' are incompatible. +!!! error TS2322: Call signature return types '() => () => number' and '() => () => string' are incompatible. +!!! error TS2322: Call signature return types '() => number' and '() => string' are incompatible. +!!! error TS2322: Type 'number' is not assignable to type 'string'. \ No newline at end of file diff --git a/tests/baselines/reference/nestedCallbackErrorNotFlattened.js b/tests/baselines/reference/nestedCallbackErrorNotFlattened.js new file mode 100644 index 00000000000..0c5edf9760b --- /dev/null +++ b/tests/baselines/reference/nestedCallbackErrorNotFlattened.js @@ -0,0 +1,11 @@ +//// [nestedCallbackErrorNotFlattened.ts] +type Cb = {noAlias: () => T}["noAlias"]; // `"noAlias"` here prevents an alias symbol from being made +// which means the comparison will definitely be structural, rather than by variance + +declare const x: Cb>>>; // one more layer of `Cb` adn we'd get a `true` from the deeply-nested symbol check +declare let y: Cb>>>; +y = x; + +//// [nestedCallbackErrorNotFlattened.js] +"use strict"; +y = x; diff --git a/tests/baselines/reference/nestedCallbackErrorNotFlattened.symbols b/tests/baselines/reference/nestedCallbackErrorNotFlattened.symbols new file mode 100644 index 00000000000..2019b787822 --- /dev/null +++ b/tests/baselines/reference/nestedCallbackErrorNotFlattened.symbols @@ -0,0 +1,27 @@ +=== tests/cases/compiler/nestedCallbackErrorNotFlattened.ts === +type Cb = {noAlias: () => T}["noAlias"]; // `"noAlias"` here prevents an alias symbol from being made +>Cb : Symbol(Cb, Decl(nestedCallbackErrorNotFlattened.ts, 0, 0)) +>T : Symbol(T, Decl(nestedCallbackErrorNotFlattened.ts, 0, 8)) +>noAlias : Symbol(noAlias, Decl(nestedCallbackErrorNotFlattened.ts, 0, 14)) +>T : Symbol(T, Decl(nestedCallbackErrorNotFlattened.ts, 0, 8)) + +// which means the comparison will definitely be structural, rather than by variance + +declare const x: Cb>>>; // one more layer of `Cb` adn we'd get a `true` from the deeply-nested symbol check +>x : Symbol(x, Decl(nestedCallbackErrorNotFlattened.ts, 3, 13)) +>Cb : Symbol(Cb, Decl(nestedCallbackErrorNotFlattened.ts, 0, 0)) +>Cb : Symbol(Cb, Decl(nestedCallbackErrorNotFlattened.ts, 0, 0)) +>Cb : Symbol(Cb, Decl(nestedCallbackErrorNotFlattened.ts, 0, 0)) +>Cb : Symbol(Cb, Decl(nestedCallbackErrorNotFlattened.ts, 0, 0)) + +declare let y: Cb>>>; +>y : Symbol(y, Decl(nestedCallbackErrorNotFlattened.ts, 4, 11)) +>Cb : Symbol(Cb, Decl(nestedCallbackErrorNotFlattened.ts, 0, 0)) +>Cb : Symbol(Cb, Decl(nestedCallbackErrorNotFlattened.ts, 0, 0)) +>Cb : Symbol(Cb, Decl(nestedCallbackErrorNotFlattened.ts, 0, 0)) +>Cb : Symbol(Cb, Decl(nestedCallbackErrorNotFlattened.ts, 0, 0)) + +y = x; +>y : Symbol(y, Decl(nestedCallbackErrorNotFlattened.ts, 4, 11)) +>x : Symbol(x, Decl(nestedCallbackErrorNotFlattened.ts, 3, 13)) + diff --git a/tests/baselines/reference/nestedCallbackErrorNotFlattened.types b/tests/baselines/reference/nestedCallbackErrorNotFlattened.types new file mode 100644 index 00000000000..5e753714b34 --- /dev/null +++ b/tests/baselines/reference/nestedCallbackErrorNotFlattened.types @@ -0,0 +1,18 @@ +=== tests/cases/compiler/nestedCallbackErrorNotFlattened.ts === +type Cb = {noAlias: () => T}["noAlias"]; // `"noAlias"` here prevents an alias symbol from being made +>Cb : () => T +>noAlias : () => T + +// which means the comparison will definitely be structural, rather than by variance + +declare const x: Cb>>>; // one more layer of `Cb` adn we'd get a `true` from the deeply-nested symbol check +>x : () => () => () => () => number + +declare let y: Cb>>>; +>y : () => () => () => () => string + +y = x; +>y = x : () => () => () => () => number +>y : () => () => () => () => string +>x : () => () => () => () => number + diff --git a/tests/baselines/reference/nestedRecursiveArraysOrObjectsError01.errors.txt b/tests/baselines/reference/nestedRecursiveArraysOrObjectsError01.errors.txt index 138a4a84158..650a808e598 100644 --- a/tests/baselines/reference/nestedRecursiveArraysOrObjectsError01.errors.txt +++ b/tests/baselines/reference/nestedRecursiveArraysOrObjectsError01.errors.txt @@ -1,17 +1,14 @@ tests/cases/compiler/nestedRecursiveArraysOrObjectsError01.ts(10,9): error TS2322: Type '{ foo: string; jj: number; }[][][]' is not assignable to type 'Style'. Type '{ foo: string; jj: number; }[][][]' is not assignable to type 'StyleArray'. - Types of property 'pop' are incompatible. - Type '() => { foo: string; jj: number; }[][]' is not assignable to type '() => Style'. - Type '{ foo: string; jj: number; }[][]' is not assignable to type 'Style'. - Type '{ foo: string; jj: number; }[][]' is not assignable to type 'StyleArray'. - Types of property 'pop' are incompatible. - Type '() => { foo: string; jj: number; }[]' is not assignable to type '() => Style'. - Type '{ foo: string; jj: number; }[]' is not assignable to type 'Style'. - Type '{ foo: string; jj: number; }[]' is not assignable to type 'StyleArray'. - Types of property 'pop' are incompatible. - Type '() => { foo: string; jj: number; }' is not assignable to type '() => Style'. - Type '{ foo: string; jj: number; }' is not assignable to type 'Style'. - Object literal may only specify known properties, and 'jj' does not exist in type 'Style'. + The types returned by 'pop()' are incompatible between these types. + Type '{ foo: string; jj: number; }[][]' is not assignable to type 'Style'. + Type '{ foo: string; jj: number; }[][]' is not assignable to type 'StyleArray'. + The types returned by 'pop()' are incompatible between these types. + Type '{ foo: string; jj: number; }[]' is not assignable to type 'Style'. + Type '{ foo: string; jj: number; }[]' is not assignable to type 'StyleArray'. + The types returned by 'pop()' are incompatible between these types. + Type '{ foo: string; jj: number; }' is not assignable to type 'Style'. + Object literal may only specify known properties, and 'jj' does not exist in type 'Style'. ==== tests/cases/compiler/nestedRecursiveArraysOrObjectsError01.ts (1 errors) ==== @@ -28,18 +25,15 @@ tests/cases/compiler/nestedRecursiveArraysOrObjectsError01.ts(10,9): error TS232 ~~~~~ !!! error TS2322: Type '{ foo: string; jj: number; }[][][]' is not assignable to type 'Style'. !!! error TS2322: Type '{ foo: string; jj: number; }[][][]' is not assignable to type 'StyleArray'. -!!! error TS2322: Types of property 'pop' are incompatible. -!!! error TS2322: Type '() => { foo: string; jj: number; }[][]' is not assignable to type '() => Style'. -!!! error TS2322: Type '{ foo: string; jj: number; }[][]' is not assignable to type 'Style'. -!!! error TS2322: Type '{ foo: string; jj: number; }[][]' is not assignable to type 'StyleArray'. -!!! error TS2322: Types of property 'pop' are incompatible. -!!! error TS2322: Type '() => { foo: string; jj: number; }[]' is not assignable to type '() => Style'. -!!! error TS2322: Type '{ foo: string; jj: number; }[]' is not assignable to type 'Style'. -!!! error TS2322: Type '{ foo: string; jj: number; }[]' is not assignable to type 'StyleArray'. -!!! error TS2322: Types of property 'pop' are incompatible. -!!! error TS2322: Type '() => { foo: string; jj: number; }' is not assignable to type '() => Style'. -!!! error TS2322: Type '{ foo: string; jj: number; }' is not assignable to type 'Style'. -!!! error TS2322: Object literal may only specify known properties, and 'jj' does not exist in type 'Style'. +!!! error TS2322: The types returned by 'pop()' are incompatible between these types. +!!! error TS2322: Type '{ foo: string; jj: number; }[][]' is not assignable to type 'Style'. +!!! error TS2322: Type '{ foo: string; jj: number; }[][]' is not assignable to type 'StyleArray'. +!!! error TS2322: The types returned by 'pop()' are incompatible between these types. +!!! error TS2322: Type '{ foo: string; jj: number; }[]' is not assignable to type 'Style'. +!!! error TS2322: Type '{ foo: string; jj: number; }[]' is not assignable to type 'StyleArray'. +!!! error TS2322: The types returned by 'pop()' are incompatible between these types. +!!! error TS2322: Type '{ foo: string; jj: number; }' is not assignable to type 'Style'. +!!! error TS2322: Object literal may only specify known properties, and 'jj' does not exist in type 'Style'. }]] ]; diff --git a/tests/baselines/reference/objectTypeHidingMembersOfObjectAssignmentCompat.errors.txt b/tests/baselines/reference/objectTypeHidingMembersOfObjectAssignmentCompat.errors.txt index c4ee3ea17b2..d4afd97723e 100644 --- a/tests/baselines/reference/objectTypeHidingMembersOfObjectAssignmentCompat.errors.txt +++ b/tests/baselines/reference/objectTypeHidingMembersOfObjectAssignmentCompat.errors.txt @@ -1,15 +1,12 @@ tests/cases/conformance/types/members/objectTypeHidingMembersOfObjectAssignmentCompat.ts(7,1): error TS2322: Type 'I' is not assignable to type 'Object'. - Types of property 'toString' are incompatible. - Type '() => void' is not assignable to type '() => string'. - Type 'void' is not assignable to type 'string'. + The types returned by 'toString()' are incompatible between these types. + Type 'void' is not assignable to type 'string'. tests/cases/conformance/types/members/objectTypeHidingMembersOfObjectAssignmentCompat.ts(14,1): error TS2322: Type 'C' is not assignable to type 'Object'. - Types of property 'toString' are incompatible. - Type '() => void' is not assignable to type '() => string'. - Type 'void' is not assignable to type 'string'. + The types returned by 'toString()' are incompatible between these types. + Type 'void' is not assignable to type 'string'. tests/cases/conformance/types/members/objectTypeHidingMembersOfObjectAssignmentCompat.ts(20,1): error TS2322: Type '{ toString: () => void; }' is not assignable to type 'Object'. - Types of property 'toString' are incompatible. - Type '() => void' is not assignable to type '() => string'. - Type 'void' is not assignable to type 'string'. + The types returned by 'toString()' are incompatible between these types. + Type 'void' is not assignable to type 'string'. ==== tests/cases/conformance/types/members/objectTypeHidingMembersOfObjectAssignmentCompat.ts (3 errors) ==== @@ -22,9 +19,8 @@ tests/cases/conformance/types/members/objectTypeHidingMembersOfObjectAssignmentC o = i; // error ~ !!! error TS2322: Type 'I' is not assignable to type 'Object'. -!!! error TS2322: Types of property 'toString' are incompatible. -!!! error TS2322: Type '() => void' is not assignable to type '() => string'. -!!! error TS2322: Type 'void' is not assignable to type 'string'. +!!! error TS2322: The types returned by 'toString()' are incompatible between these types. +!!! error TS2322: Type 'void' is not assignable to type 'string'. i = o; // ok class C { @@ -34,9 +30,8 @@ tests/cases/conformance/types/members/objectTypeHidingMembersOfObjectAssignmentC o = c; // error ~ !!! error TS2322: Type 'C' is not assignable to type 'Object'. -!!! error TS2322: Types of property 'toString' are incompatible. -!!! error TS2322: Type '() => void' is not assignable to type '() => string'. -!!! error TS2322: Type 'void' is not assignable to type 'string'. +!!! error TS2322: The types returned by 'toString()' are incompatible between these types. +!!! error TS2322: Type 'void' is not assignable to type 'string'. c = o; // ok var a = { @@ -45,7 +40,6 @@ tests/cases/conformance/types/members/objectTypeHidingMembersOfObjectAssignmentC o = a; // error ~ !!! error TS2322: Type '{ toString: () => void; }' is not assignable to type 'Object'. -!!! error TS2322: Types of property 'toString' are incompatible. -!!! error TS2322: Type '() => void' is not assignable to type '() => string'. -!!! error TS2322: Type 'void' is not assignable to type 'string'. +!!! error TS2322: The types returned by 'toString()' are incompatible between these types. +!!! error TS2322: Type 'void' is not assignable to type 'string'. a = o; // ok \ No newline at end of file diff --git a/tests/baselines/reference/objectTypeHidingMembersOfObjectAssignmentCompat2.errors.txt b/tests/baselines/reference/objectTypeHidingMembersOfObjectAssignmentCompat2.errors.txt index 3e27b22240e..28d466457d7 100644 --- a/tests/baselines/reference/objectTypeHidingMembersOfObjectAssignmentCompat2.errors.txt +++ b/tests/baselines/reference/objectTypeHidingMembersOfObjectAssignmentCompat2.errors.txt @@ -1,25 +1,18 @@ tests/cases/conformance/types/members/objectTypeHidingMembersOfObjectAssignmentCompat2.ts(7,1): error TS2322: Type 'I' is not assignable to type 'Object'. - Types of property 'toString' are incompatible. - Type '() => number' is not assignable to type '() => string'. - Type 'number' is not assignable to type 'string'. -tests/cases/conformance/types/members/objectTypeHidingMembersOfObjectAssignmentCompat2.ts(8,1): error TS2322: Type 'Object' is not assignable to type 'I'. - The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead? - Types of property 'toString' are incompatible. - Type '() => string' is not assignable to type '() => number'. - Type 'string' is not assignable to type 'number'. + The types returned by 'toString()' are incompatible between these types. + Type 'number' is not assignable to type 'string'. +tests/cases/conformance/types/members/objectTypeHidingMembersOfObjectAssignmentCompat2.ts(8,1): error TS2696: The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead? + The types returned by 'toString()' are incompatible between these types. + Type 'string' is not assignable to type 'number'. tests/cases/conformance/types/members/objectTypeHidingMembersOfObjectAssignmentCompat2.ts(14,1): error TS2322: Type 'C' is not assignable to type 'Object'. - Types of property 'toString' are incompatible. - Type '() => number' is not assignable to type '() => string'. - Type 'number' is not assignable to type 'string'. -tests/cases/conformance/types/members/objectTypeHidingMembersOfObjectAssignmentCompat2.ts(15,1): error TS2322: Type 'Object' is not assignable to type 'C'. - The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead? - Types of property 'toString' are incompatible. - Type '() => string' is not assignable to type '() => number'. - Type 'string' is not assignable to type 'number'. + The types returned by 'toString()' are incompatible between these types. + Type 'number' is not assignable to type 'string'. +tests/cases/conformance/types/members/objectTypeHidingMembersOfObjectAssignmentCompat2.ts(15,1): error TS2696: The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead? + The types returned by 'toString()' are incompatible between these types. + Type 'string' is not assignable to type 'number'. tests/cases/conformance/types/members/objectTypeHidingMembersOfObjectAssignmentCompat2.ts(20,1): error TS2322: Type '{ toString: () => void; }' is not assignable to type 'Object'. - Types of property 'toString' are incompatible. - Type '() => void' is not assignable to type '() => string'. - Type 'void' is not assignable to type 'string'. + The types returned by 'toString()' are incompatible between these types. + Type 'void' is not assignable to type 'string'. ==== tests/cases/conformance/types/members/objectTypeHidingMembersOfObjectAssignmentCompat2.ts (5 errors) ==== @@ -32,16 +25,13 @@ tests/cases/conformance/types/members/objectTypeHidingMembersOfObjectAssignmentC o = i; // error ~ !!! error TS2322: Type 'I' is not assignable to type 'Object'. -!!! error TS2322: Types of property 'toString' are incompatible. -!!! error TS2322: Type '() => number' is not assignable to type '() => string'. -!!! error TS2322: Type 'number' is not assignable to type 'string'. +!!! error TS2322: The types returned by 'toString()' are incompatible between these types. +!!! error TS2322: Type 'number' is not assignable to type 'string'. i = o; // error ~ -!!! error TS2322: Type 'Object' is not assignable to type 'I'. -!!! error TS2322: The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead? -!!! error TS2322: Types of property 'toString' are incompatible. -!!! error TS2322: Type '() => string' is not assignable to type '() => number'. -!!! error TS2322: Type 'string' is not assignable to type 'number'. +!!! error TS2696: The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead? +!!! error TS2696: The types returned by 'toString()' are incompatible between these types. +!!! error TS2696: Type 'string' is not assignable to type 'number'. class C { toString(): number { return 1; } @@ -50,16 +40,13 @@ tests/cases/conformance/types/members/objectTypeHidingMembersOfObjectAssignmentC o = c; // error ~ !!! error TS2322: Type 'C' is not assignable to type 'Object'. -!!! error TS2322: Types of property 'toString' are incompatible. -!!! error TS2322: Type '() => number' is not assignable to type '() => string'. -!!! error TS2322: Type 'number' is not assignable to type 'string'. +!!! error TS2322: The types returned by 'toString()' are incompatible between these types. +!!! error TS2322: Type 'number' is not assignable to type 'string'. c = o; // error ~ -!!! error TS2322: Type 'Object' is not assignable to type 'C'. -!!! error TS2322: The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead? -!!! error TS2322: Types of property 'toString' are incompatible. -!!! error TS2322: Type '() => string' is not assignable to type '() => number'. -!!! error TS2322: Type 'string' is not assignable to type 'number'. +!!! error TS2696: The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead? +!!! error TS2696: The types returned by 'toString()' are incompatible between these types. +!!! error TS2696: Type 'string' is not assignable to type 'number'. var a = { toString: () => { } @@ -67,7 +54,6 @@ tests/cases/conformance/types/members/objectTypeHidingMembersOfObjectAssignmentC o = a; // error ~ !!! error TS2322: Type '{ toString: () => void; }' is not assignable to type 'Object'. -!!! error TS2322: Types of property 'toString' are incompatible. -!!! error TS2322: Type '() => void' is not assignable to type '() => string'. -!!! error TS2322: Type 'void' is not assignable to type 'string'. +!!! error TS2322: The types returned by 'toString()' are incompatible between these types. +!!! error TS2322: Type 'void' is not assignable to type 'string'. a = o; // ok \ No newline at end of file diff --git a/tests/baselines/reference/promisePermutations.errors.txt b/tests/baselines/reference/promisePermutations.errors.txt index 1cb61562fe3..87743ab77a3 100644 --- a/tests/baselines/reference/promisePermutations.errors.txt +++ b/tests/baselines/reference/promisePermutations.errors.txt @@ -122,8 +122,8 @@ tests/cases/compiler/promisePermutations.ts(159,21): error TS2769: No overload m tests/cases/compiler/promisePermutations.ts(160,21): error TS2769: No overload matches this call. The last overload gave the following error. Argument of type '{ (x: number): Promise; (x: string): Promise; }' is not assignable to parameter of type '(value: number) => IPromise'. - Type 'Promise' is not assignable to type 'IPromise'. - Types of property 'then' are incompatible. + Call signature return types 'Promise' and 'IPromise' are incompatible. + The types of 'then' are incompatible between these types. Type '{ (onfulfilled?: (value: number) => TResult1 | PromiseLike, onrejected?: (reason: any) => TResult2 | PromiseLike): Promise; (success?: (value: number) => Promise, error?: (error: any) => Promise, progress?: (progress: any) => void): Promise; (success?: (value: number) => Promise, error?: (error: any) => U, progress?: (progress: any) => void): Promise; (success?: (value: number) => U, error?: (error: any) => Promise, progress?: (progress: any) => void): Promise; (success?: (value: number) => U, error?: (error: any) => U, progress?: (progress: any) => void): Promise; }' is not assignable to type '{ (success?: (value: string) => IPromise, error?: (error: any) => IPromise, progress?: (progress: any) => void): IPromise; (success?: (value: string) => IPromise, error?: (error: any) => U, progress?: (progress: any) => void): IPromise; (success?: (value: string) => U, error?: (error: any) => IPromise, progress?: (progress: any) => void): IPromise; (success?: (value: string) => U, error?: (error: any) => U, progress?: (progress: any) => void): IPromise; }'. Types of parameters 'onfulfilled' and 'success' are incompatible. Types of parameters 'value' and 'value' are incompatible. @@ -481,8 +481,8 @@ tests/cases/compiler/promisePermutations.ts(160,21): error TS2769: No overload m !!! error TS2769: No overload matches this call. !!! error TS2769: The last overload gave the following error. !!! error TS2769: Argument of type '{ (x: number): Promise; (x: string): Promise; }' is not assignable to parameter of type '(value: number) => IPromise'. -!!! error TS2769: Type 'Promise' is not assignable to type 'IPromise'. -!!! error TS2769: Types of property 'then' are incompatible. +!!! error TS2769: Call signature return types 'Promise' and 'IPromise' are incompatible. +!!! error TS2769: The types of 'then' are incompatible between these types. !!! error TS2769: Type '{ (onfulfilled?: (value: number) => TResult1 | PromiseLike, onrejected?: (reason: any) => TResult2 | PromiseLike): Promise; (success?: (value: number) => Promise, error?: (error: any) => Promise, progress?: (progress: any) => void): Promise; (success?: (value: number) => Promise, error?: (error: any) => U, progress?: (progress: any) => void): Promise; (success?: (value: number) => U, error?: (error: any) => Promise, progress?: (progress: any) => void): Promise; (success?: (value: number) => U, error?: (error: any) => U, progress?: (progress: any) => void): Promise; }' is not assignable to type '{ (success?: (value: string) => IPromise, error?: (error: any) => IPromise, progress?: (progress: any) => void): IPromise; (success?: (value: string) => IPromise, error?: (error: any) => U, progress?: (progress: any) => void): IPromise; (success?: (value: string) => U, error?: (error: any) => IPromise, progress?: (progress: any) => void): IPromise; (success?: (value: string) => U, error?: (error: any) => U, progress?: (progress: any) => void): IPromise; }'. !!! error TS2769: Types of parameters 'onfulfilled' and 'success' are incompatible. !!! error TS2769: Types of parameters 'value' and 'value' are incompatible. diff --git a/tests/baselines/reference/promisePermutations2.errors.txt b/tests/baselines/reference/promisePermutations2.errors.txt index d601a5049bf..b69eaab3157 100644 --- a/tests/baselines/reference/promisePermutations2.errors.txt +++ b/tests/baselines/reference/promisePermutations2.errors.txt @@ -80,8 +80,8 @@ tests/cases/compiler/promisePermutations2.ts(158,21): error TS2345: Argument of Type 'Promise' is not assignable to type 'Promise'. Type 'number' is not assignable to type 'string'. tests/cases/compiler/promisePermutations2.ts(159,21): error TS2345: Argument of type '{ (x: number): Promise; (x: string): Promise; }' is not assignable to parameter of type '(value: number) => IPromise'. - Type 'Promise' is not assignable to type 'IPromise'. - Types of property 'then' are incompatible. + Call signature return types 'Promise' and 'IPromise' are incompatible. + The types of 'then' are incompatible between these types. Type '{ (onfulfilled?: (value: number) => TResult1 | PromiseLike, onrejected?: (reason: any) => TResult2 | PromiseLike): Promise; (success?: (value: number) => U, error?: (error: any) => U, progress?: (progress: any) => void): Promise; }' is not assignable to type '{ (success?: (value: string) => IPromise, error?: (error: any) => IPromise, progress?: (progress: any) => void): IPromise; (success?: (value: string) => IPromise, error?: (error: any) => U, progress?: (progress: any) => void): IPromise; (success?: (value: string) => U, error?: (error: any) => IPromise, progress?: (progress: any) => void): IPromise; (success?: (value: string) => U, error?: (error: any) => U, progress?: (progress: any) => void): IPromise; }'. Types of parameters 'onfulfilled' and 'success' are incompatible. Types of parameters 'value' and 'value' are incompatible. @@ -376,8 +376,8 @@ tests/cases/compiler/promisePermutations2.ts(159,21): error TS2345: Argument of var s11c = s11.then(testFunction11P, testFunction11, testFunction11); // ok ~~~~~~~~~~~~~~~ !!! error TS2345: Argument of type '{ (x: number): Promise; (x: string): Promise; }' is not assignable to parameter of type '(value: number) => IPromise'. -!!! error TS2345: Type 'Promise' is not assignable to type 'IPromise'. -!!! error TS2345: Types of property 'then' are incompatible. +!!! error TS2345: Call signature return types 'Promise' and 'IPromise' are incompatible. +!!! error TS2345: The types of 'then' are incompatible between these types. !!! error TS2345: Type '{ (onfulfilled?: (value: number) => TResult1 | PromiseLike, onrejected?: (reason: any) => TResult2 | PromiseLike): Promise; (success?: (value: number) => U, error?: (error: any) => U, progress?: (progress: any) => void): Promise; }' is not assignable to type '{ (success?: (value: string) => IPromise, error?: (error: any) => IPromise, progress?: (progress: any) => void): IPromise; (success?: (value: string) => IPromise, error?: (error: any) => U, progress?: (progress: any) => void): IPromise; (success?: (value: string) => U, error?: (error: any) => IPromise, progress?: (progress: any) => void): IPromise; (success?: (value: string) => U, error?: (error: any) => U, progress?: (progress: any) => void): IPromise; }'. !!! error TS2345: Types of parameters 'onfulfilled' and 'success' are incompatible. !!! error TS2345: Types of parameters 'value' and 'value' are incompatible. diff --git a/tests/baselines/reference/promisePermutations3.errors.txt b/tests/baselines/reference/promisePermutations3.errors.txt index d3574d2a4c3..929b0d5459c 100644 --- a/tests/baselines/reference/promisePermutations3.errors.txt +++ b/tests/baselines/reference/promisePermutations3.errors.txt @@ -101,8 +101,8 @@ tests/cases/compiler/promisePermutations3.ts(158,21): error TS2769: No overload tests/cases/compiler/promisePermutations3.ts(159,21): error TS2769: No overload matches this call. The last overload gave the following error. Argument of type '{ (x: number): Promise; (x: string): Promise; }' is not assignable to parameter of type '(value: number) => IPromise'. - Type 'Promise' is not assignable to type 'IPromise'. - Types of property 'then' are incompatible. + Call signature return types 'Promise' and 'IPromise' are incompatible. + The types of 'then' are incompatible between these types. Type '{ (onfulfilled?: (value: number) => TResult1 | PromiseLike, onrejected?: (reason: any) => TResult2 | PromiseLike): Promise; (success?: (value: number) => Promise, error?: (error: any) => Promise, progress?: (progress: any) => void): Promise; (success?: (value: number) => Promise, error?: (error: any) => U, progress?: (progress: any) => void): Promise; (success?: (value: number) => U, error?: (error: any) => Promise, progress?: (progress: any) => void): Promise; (success?: (value: number) => U, error?: (error: any) => U, progress?: (progress: any) => void): Promise; }' is not assignable to type '(success?: (value: string) => U, error?: (error: any) => U, progress?: (progress: any) => void) => IPromise'. Types of parameters 'onfulfilled' and 'success' are incompatible. Types of parameters 'value' and 'value' are incompatible. @@ -429,8 +429,8 @@ tests/cases/compiler/promisePermutations3.ts(165,21): error TS2345: Argument of !!! error TS2769: No overload matches this call. !!! error TS2769: The last overload gave the following error. !!! error TS2769: Argument of type '{ (x: number): Promise; (x: string): Promise; }' is not assignable to parameter of type '(value: number) => IPromise'. -!!! error TS2769: Type 'Promise' is not assignable to type 'IPromise'. -!!! error TS2769: Types of property 'then' are incompatible. +!!! error TS2769: Call signature return types 'Promise' and 'IPromise' are incompatible. +!!! error TS2769: The types of 'then' are incompatible between these types. !!! error TS2769: Type '{ (onfulfilled?: (value: number) => TResult1 | PromiseLike, onrejected?: (reason: any) => TResult2 | PromiseLike): Promise; (success?: (value: number) => Promise, error?: (error: any) => Promise, progress?: (progress: any) => void): Promise; (success?: (value: number) => Promise, error?: (error: any) => U, progress?: (progress: any) => void): Promise; (success?: (value: number) => U, error?: (error: any) => Promise, progress?: (progress: any) => void): Promise; (success?: (value: number) => U, error?: (error: any) => U, progress?: (progress: any) => void): Promise; }' is not assignable to type '(success?: (value: string) => U, error?: (error: any) => U, progress?: (progress: any) => void) => IPromise'. !!! error TS2769: Types of parameters 'onfulfilled' and 'success' are incompatible. !!! error TS2769: Types of parameters 'value' and 'value' are incompatible. diff --git a/tests/baselines/reference/promiseTypeInference.errors.txt b/tests/baselines/reference/promiseTypeInference.errors.txt index 04ba3a0d878..7056e35432f 100644 --- a/tests/baselines/reference/promiseTypeInference.errors.txt +++ b/tests/baselines/reference/promiseTypeInference.errors.txt @@ -5,10 +5,9 @@ tests/cases/compiler/promiseTypeInference.ts(10,39): error TS2769: No overload m Type 'IPromise' is not assignable to type 'number | PromiseLike'. Type 'IPromise' is not assignable to type 'PromiseLike'. Types of property 'then' are incompatible. - Type '(success?: (value: number) => IPromise) => IPromise' is not assignable to type '(onfulfilled?: (value: number) => TResult1 | PromiseLike, onrejected?: (reason: any) => TResult2 | PromiseLike) => PromiseLike'. - Types of parameters 'success' and 'onfulfilled' are incompatible. - Type 'TResult1 | PromiseLike' is not assignable to type 'IPromise'. - Type 'TResult1' is not assignable to type 'IPromise'. + Types of parameters 'success' and 'onfulfilled' are incompatible. + Type 'TResult1 | PromiseLike' is not assignable to type 'IPromise'. + Type 'TResult1' is not assignable to type 'IPromise'. ==== tests/cases/compiler/promiseTypeInference.ts (1 errors) ==== @@ -30,10 +29,9 @@ tests/cases/compiler/promiseTypeInference.ts(10,39): error TS2769: No overload m !!! error TS2769: Type 'IPromise' is not assignable to type 'number | PromiseLike'. !!! error TS2769: Type 'IPromise' is not assignable to type 'PromiseLike'. !!! error TS2769: Types of property 'then' are incompatible. -!!! error TS2769: Type '(success?: (value: number) => IPromise) => IPromise' is not assignable to type '(onfulfilled?: (value: number) => TResult1 | PromiseLike, onrejected?: (reason: any) => TResult2 | PromiseLike) => PromiseLike'. -!!! error TS2769: Types of parameters 'success' and 'onfulfilled' are incompatible. -!!! error TS2769: Type 'TResult1 | PromiseLike' is not assignable to type 'IPromise'. -!!! error TS2769: Type 'TResult1' is not assignable to type 'IPromise'. +!!! error TS2769: Types of parameters 'success' and 'onfulfilled' are incompatible. +!!! error TS2769: Type 'TResult1 | PromiseLike' is not assignable to type 'IPromise'. +!!! error TS2769: Type 'TResult1' is not assignable to type 'IPromise'. !!! related TS2728 /.ts/lib.es5.d.ts:1413:5: 'catch' is declared here. !!! related TS6502 tests/cases/compiler/promiseTypeInference.ts:2:23: The expected type comes from the return type of this signature. !!! related TS6502 /.ts/lib.es5.d.ts:1406:57: The expected type comes from the return type of this signature. diff --git a/tests/baselines/reference/strictFunctionTypesErrors.errors.txt b/tests/baselines/reference/strictFunctionTypesErrors.errors.txt index 3ff04c44fb7..6f474663dcb 100644 --- a/tests/baselines/reference/strictFunctionTypesErrors.errors.txt +++ b/tests/baselines/reference/strictFunctionTypesErrors.errors.txt @@ -69,9 +69,8 @@ tests/cases/compiler/strictFunctionTypesErrors.ts(126,1): error TS2322: Type 'Cr tests/cases/compiler/strictFunctionTypesErrors.ts(127,1): error TS2322: Type 'Crate' is not assignable to type 'Crate'. Types of property 'item' are incompatible. Type 'Animal' is not assignable to type 'Dog'. -tests/cases/compiler/strictFunctionTypesErrors.ts(133,1): error TS2322: Type '(f: (x: Dog) => Dog) => void' is not assignable to type '(f: (x: Animal) => Animal) => void'. - Types of parameters 'f' and 'f' are incompatible. - Type 'Animal' is not assignable to type 'Dog'. +tests/cases/compiler/strictFunctionTypesErrors.ts(133,1): error TS2328: Types of parameters 'f' and 'f' are incompatible. + Type 'Animal' is not assignable to type 'Dog'. tests/cases/compiler/strictFunctionTypesErrors.ts(134,1): error TS2322: Type '(f: (x: Animal) => Animal) => void' is not assignable to type '(f: (x: Dog) => Dog) => void'. Types of parameters 'f' and 'f' are incompatible. Types of parameters 'x' and 'x' are incompatible. @@ -324,9 +323,8 @@ tests/cases/compiler/strictFunctionTypesErrors.ts(155,5): error TS2322: Type '(c declare let fc2: (f: (x: Dog) => Dog) => void; fc1 = fc2; // Error ~~~ -!!! error TS2322: Type '(f: (x: Dog) => Dog) => void' is not assignable to type '(f: (x: Animal) => Animal) => void'. -!!! error TS2322: Types of parameters 'f' and 'f' are incompatible. -!!! error TS2322: Type 'Animal' is not assignable to type 'Dog'. +!!! error TS2328: Types of parameters 'f' and 'f' are incompatible. +!!! error TS2328: Type 'Animal' is not assignable to type 'Dog'. fc2 = fc1; // Error ~~~ !!! error TS2322: Type '(f: (x: Animal) => Animal) => void' is not assignable to type '(f: (x: Dog) => Dog) => void'. diff --git a/tests/baselines/reference/subtypingWithCallSignaturesWithSpecializedSignatures.errors.txt b/tests/baselines/reference/subtypingWithCallSignaturesWithSpecializedSignatures.errors.txt index 7dfb078170a..b8bc5100402 100644 --- a/tests/baselines/reference/subtypingWithCallSignaturesWithSpecializedSignatures.errors.txt +++ b/tests/baselines/reference/subtypingWithCallSignaturesWithSpecializedSignatures.errors.txt @@ -1,12 +1,10 @@ tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithCallSignaturesWithSpecializedSignatures.ts(70,15): error TS2430: Interface 'I2' incorrectly extends interface 'Base2'. - Types of property 'a' are incompatible. - Type '(x: string) => string' is not assignable to type '{ (x: "a"): number; (x: string): number; }'. - Type 'string' is not assignable to type 'number'. + The types returned by 'a(...)' are incompatible between these types. + Type 'string' is not assignable to type 'number'. tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithCallSignaturesWithSpecializedSignatures.ts(76,15): error TS2430: Interface 'I3' incorrectly extends interface 'Base2'. - Types of property 'a2' are incompatible. - Type '(x: T) => string' is not assignable to type '(x: T) => T'. - Type 'string' is not assignable to type 'T'. - 'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. + The types returned by 'a2(...)' are incompatible between these types. + Type 'string' is not assignable to type 'T'. + 'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. ==== tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithCallSignaturesWithSpecializedSignatures.ts (2 errors) ==== @@ -82,9 +80,8 @@ tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingW interface I2 extends Base2 { ~~ !!! error TS2430: Interface 'I2' incorrectly extends interface 'Base2'. -!!! error TS2430: Types of property 'a' are incompatible. -!!! error TS2430: Type '(x: string) => string' is not assignable to type '{ (x: "a"): number; (x: string): number; }'. -!!! error TS2430: Type 'string' is not assignable to type 'number'. +!!! error TS2430: The types returned by 'a(...)' are incompatible between these types. +!!! error TS2430: Type 'string' is not assignable to type 'number'. // N's a: (x: string) => string; // error because base returns non-void; } @@ -93,10 +90,9 @@ tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingW interface I3 extends Base2 { ~~ !!! error TS2430: Interface 'I3' incorrectly extends interface 'Base2'. -!!! error TS2430: Types of property 'a2' are incompatible. -!!! error TS2430: Type '(x: T) => string' is not assignable to type '(x: T) => T'. -!!! error TS2430: Type 'string' is not assignable to type 'T'. -!!! error TS2430: 'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. +!!! error TS2430: The types returned by 'a2(...)' are incompatible between these types. +!!! error TS2430: Type 'string' is not assignable to type 'T'. +!!! error TS2430: 'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. // N's a2: (x: T) => string; // error because base returns non-void; } diff --git a/tests/baselines/reference/subtypingWithConstructSignaturesWithSpecializedSignatures.errors.txt b/tests/baselines/reference/subtypingWithConstructSignaturesWithSpecializedSignatures.errors.txt index 8f4a219407a..db493045f09 100644 --- a/tests/baselines/reference/subtypingWithConstructSignaturesWithSpecializedSignatures.errors.txt +++ b/tests/baselines/reference/subtypingWithConstructSignaturesWithSpecializedSignatures.errors.txt @@ -1,12 +1,10 @@ tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithConstructSignaturesWithSpecializedSignatures.ts(70,15): error TS2430: Interface 'I2' incorrectly extends interface 'Base2'. - Types of property 'a' are incompatible. - Type 'new (x: string) => string' is not assignable to type '{ new (x: "a"): number; new (x: string): number; }'. - Type 'string' is not assignable to type 'number'. + The types returned by 'new a(...)' are incompatible between these types. + Type 'string' is not assignable to type 'number'. tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithConstructSignaturesWithSpecializedSignatures.ts(76,15): error TS2430: Interface 'I3' incorrectly extends interface 'Base2'. - Types of property 'a2' are incompatible. - Type 'new (x: T) => string' is not assignable to type 'new (x: T) => T'. - Type 'string' is not assignable to type 'T'. - 'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. + The types returned by 'new a2(...)' are incompatible between these types. + Type 'string' is not assignable to type 'T'. + 'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. ==== tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithConstructSignaturesWithSpecializedSignatures.ts (2 errors) ==== @@ -82,9 +80,8 @@ tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingW interface I2 extends Base2 { ~~ !!! error TS2430: Interface 'I2' incorrectly extends interface 'Base2'. -!!! error TS2430: Types of property 'a' are incompatible. -!!! error TS2430: Type 'new (x: string) => string' is not assignable to type '{ new (x: "a"): number; new (x: string): number; }'. -!!! error TS2430: Type 'string' is not assignable to type 'number'. +!!! error TS2430: The types returned by 'new a(...)' are incompatible between these types. +!!! error TS2430: Type 'string' is not assignable to type 'number'. // N's a: new (x: string) => string; // error because base returns non-void; } @@ -93,10 +90,9 @@ tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingW interface I3 extends Base2 { ~~ !!! error TS2430: Interface 'I3' incorrectly extends interface 'Base2'. -!!! error TS2430: Types of property 'a2' are incompatible. -!!! error TS2430: Type 'new (x: T) => string' is not assignable to type 'new (x: T) => T'. -!!! error TS2430: Type 'string' is not assignable to type 'T'. -!!! error TS2430: 'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. +!!! error TS2430: The types returned by 'new a2(...)' are incompatible between these types. +!!! error TS2430: Type 'string' is not assignable to type 'T'. +!!! error TS2430: 'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. // N's a2: new (x: T) => string; // error because base returns non-void; } diff --git a/tests/baselines/reference/subtypingWithGenericCallSignaturesWithOptionalParameters.errors.txt b/tests/baselines/reference/subtypingWithGenericCallSignaturesWithOptionalParameters.errors.txt index 36380fc97f4..4e0ef2113ec 100644 --- a/tests/baselines/reference/subtypingWithGenericCallSignaturesWithOptionalParameters.errors.txt +++ b/tests/baselines/reference/subtypingWithGenericCallSignaturesWithOptionalParameters.errors.txt @@ -5,23 +5,20 @@ tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingW Types of property 'a3' are incompatible. Type '(x: T, y: T) => T' is not assignable to type '(x: T) => T'. tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithGenericCallSignaturesWithOptionalParameters.ts(100,15): error TS2430: Interface 'I1' incorrectly extends interface 'Base2'. - Types of property 'a' are incompatible. - Type '() => T' is not assignable to type '() => T'. - Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. - 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. + The types returned by 'a()' are incompatible between these types. + Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. + 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithGenericCallSignaturesWithOptionalParameters.ts(104,15): error TS2430: Interface 'I2' incorrectly extends interface 'Base2'. - Types of property 'a' are incompatible. - Type '(x?: T) => T' is not assignable to type '() => T'. - Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. - 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. + The types returned by 'a(...)' are incompatible between these types. + Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. + 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithGenericCallSignaturesWithOptionalParameters.ts(108,15): error TS2430: Interface 'I3' incorrectly extends interface 'Base2'. Types of property 'a' are incompatible. Type '(x: T) => T' is not assignable to type '() => T'. tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithGenericCallSignaturesWithOptionalParameters.ts(113,15): error TS2430: Interface 'I4' incorrectly extends interface 'Base2'. - Types of property 'a2' are incompatible. - Type '() => T' is not assignable to type '(x?: T) => T'. - Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. - 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. + The types returned by 'a2(...)' are incompatible between these types. + Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. + 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithGenericCallSignaturesWithOptionalParameters.ts(117,15): error TS2430: Interface 'I5' incorrectly extends interface 'Base2'. Types of property 'a2' are incompatible. Type '(x?: T) => T' is not assignable to type '(x?: T) => T'. @@ -35,10 +32,9 @@ tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingW Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithGenericCallSignaturesWithOptionalParameters.ts(126,15): error TS2430: Interface 'I7' incorrectly extends interface 'Base2'. - Types of property 'a3' are incompatible. - Type '() => T' is not assignable to type '(x: T) => T'. - Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. - 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. + The types returned by 'a3(...)' are incompatible between these types. + Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. + 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithGenericCallSignaturesWithOptionalParameters.ts(130,15): error TS2430: Interface 'I8' incorrectly extends interface 'Base2'. Types of property 'a3' are incompatible. Type '(x?: T) => T' is not assignable to type '(x: T) => T'. @@ -55,10 +51,9 @@ tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingW Types of property 'a3' are incompatible. Type '(x: T, y: T) => T' is not assignable to type '(x: T) => T'. tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithGenericCallSignaturesWithOptionalParameters.ts(143,15): error TS2430: Interface 'I11' incorrectly extends interface 'Base2'. - Types of property 'a4' are incompatible. - Type '() => T' is not assignable to type '(x: T, y?: T) => T'. - Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. - 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. + The types returned by 'a4(...)' are incompatible between these types. + Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. + 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithGenericCallSignaturesWithOptionalParameters.ts(147,15): error TS2430: Interface 'I12' incorrectly extends interface 'Base2'. Types of property 'a4' are incompatible. Type '(x?: T, y?: T) => T' is not assignable to type '(x: T, y?: T) => T'. @@ -78,10 +73,9 @@ tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingW Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithGenericCallSignaturesWithOptionalParameters.ts(160,15): error TS2430: Interface 'I15' incorrectly extends interface 'Base2'. - Types of property 'a5' are incompatible. - Type '() => T' is not assignable to type '(x?: T, y?: T) => T'. - Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. - 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. + The types returned by 'a5(...)' are incompatible between these types. + Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. + 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithGenericCallSignaturesWithOptionalParameters.ts(164,15): error TS2430: Interface 'I16' incorrectly extends interface 'Base2'. Types of property 'a5' are incompatible. Type '(x?: T, y?: T) => T' is not assignable to type '(x?: T, y?: T) => T'. @@ -219,20 +213,18 @@ tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingW interface I1 extends Base2 { ~~ !!! error TS2430: Interface 'I1' incorrectly extends interface 'Base2'. -!!! error TS2430: Types of property 'a' are incompatible. -!!! error TS2430: Type '() => T' is not assignable to type '() => T'. -!!! error TS2430: Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. -!!! error TS2430: 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. +!!! error TS2430: The types returned by 'a()' are incompatible between these types. +!!! error TS2430: Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. +!!! error TS2430: 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. a: () => T; } interface I2 extends Base2 { ~~ !!! error TS2430: Interface 'I2' incorrectly extends interface 'Base2'. -!!! error TS2430: Types of property 'a' are incompatible. -!!! error TS2430: Type '(x?: T) => T' is not assignable to type '() => T'. -!!! error TS2430: Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. -!!! error TS2430: 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. +!!! error TS2430: The types returned by 'a(...)' are incompatible between these types. +!!! error TS2430: Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. +!!! error TS2430: 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. a: (x?: T) => T; } @@ -248,10 +240,9 @@ tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingW interface I4 extends Base2 { ~~ !!! error TS2430: Interface 'I4' incorrectly extends interface 'Base2'. -!!! error TS2430: Types of property 'a2' are incompatible. -!!! error TS2430: Type '() => T' is not assignable to type '(x?: T) => T'. -!!! error TS2430: Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. -!!! error TS2430: 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. +!!! error TS2430: The types returned by 'a2(...)' are incompatible between these types. +!!! error TS2430: Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. +!!! error TS2430: 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. a2: () => T; } @@ -281,10 +272,9 @@ tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingW interface I7 extends Base2 { ~~ !!! error TS2430: Interface 'I7' incorrectly extends interface 'Base2'. -!!! error TS2430: Types of property 'a3' are incompatible. -!!! error TS2430: Type '() => T' is not assignable to type '(x: T) => T'. -!!! error TS2430: Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. -!!! error TS2430: 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. +!!! error TS2430: The types returned by 'a3(...)' are incompatible between these types. +!!! error TS2430: Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. +!!! error TS2430: 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. a3: () => T; } @@ -322,10 +312,9 @@ tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingW interface I11 extends Base2 { ~~~ !!! error TS2430: Interface 'I11' incorrectly extends interface 'Base2'. -!!! error TS2430: Types of property 'a4' are incompatible. -!!! error TS2430: Type '() => T' is not assignable to type '(x: T, y?: T) => T'. -!!! error TS2430: Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. -!!! error TS2430: 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. +!!! error TS2430: The types returned by 'a4(...)' are incompatible between these types. +!!! error TS2430: Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. +!!! error TS2430: 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. a4: () => T; } @@ -366,10 +355,9 @@ tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingW interface I15 extends Base2 { ~~~ !!! error TS2430: Interface 'I15' incorrectly extends interface 'Base2'. -!!! error TS2430: Types of property 'a5' are incompatible. -!!! error TS2430: Type '() => T' is not assignable to type '(x?: T, y?: T) => T'. -!!! error TS2430: Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. -!!! error TS2430: 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. +!!! error TS2430: The types returned by 'a5(...)' are incompatible between these types. +!!! error TS2430: Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. +!!! error TS2430: 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. a5: () => T; } diff --git a/tests/baselines/reference/subtypingWithGenericConstructSignaturesWithOptionalParameters.errors.txt b/tests/baselines/reference/subtypingWithGenericConstructSignaturesWithOptionalParameters.errors.txt index 427c7c1f19e..7481ba54f1e 100644 --- a/tests/baselines/reference/subtypingWithGenericConstructSignaturesWithOptionalParameters.errors.txt +++ b/tests/baselines/reference/subtypingWithGenericConstructSignaturesWithOptionalParameters.errors.txt @@ -5,23 +5,20 @@ tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingW Types of property 'a3' are incompatible. Type 'new (x: T, y: T) => T' is not assignable to type 'new (x: T) => T'. tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithGenericConstructSignaturesWithOptionalParameters.ts(100,15): error TS2430: Interface 'I1' incorrectly extends interface 'Base2'. - Types of property 'a' are incompatible. - Type 'new () => T' is not assignable to type 'new () => T'. - Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. - 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. + The types returned by 'new a()' are incompatible between these types. + Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. + 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithGenericConstructSignaturesWithOptionalParameters.ts(104,15): error TS2430: Interface 'I2' incorrectly extends interface 'Base2'. - Types of property 'a' are incompatible. - Type 'new (x?: T) => T' is not assignable to type 'new () => T'. - Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. - 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. + The types returned by 'new a(...)' are incompatible between these types. + Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. + 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithGenericConstructSignaturesWithOptionalParameters.ts(108,15): error TS2430: Interface 'I3' incorrectly extends interface 'Base2'. Types of property 'a' are incompatible. Type 'new (x: T) => T' is not assignable to type 'new () => T'. tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithGenericConstructSignaturesWithOptionalParameters.ts(113,15): error TS2430: Interface 'I4' incorrectly extends interface 'Base2'. - Types of property 'a2' are incompatible. - Type 'new () => T' is not assignable to type 'new (x?: T) => T'. - Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. - 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. + The types returned by 'new a2(...)' are incompatible between these types. + Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. + 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithGenericConstructSignaturesWithOptionalParameters.ts(117,15): error TS2430: Interface 'I5' incorrectly extends interface 'Base2'. Types of property 'a2' are incompatible. Type 'new (x?: T) => T' is not assignable to type 'new (x?: T) => T'. @@ -35,10 +32,9 @@ tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingW Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithGenericConstructSignaturesWithOptionalParameters.ts(126,15): error TS2430: Interface 'I7' incorrectly extends interface 'Base2'. - Types of property 'a3' are incompatible. - Type 'new () => T' is not assignable to type 'new (x: T) => T'. - Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. - 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. + The types returned by 'new a3(...)' are incompatible between these types. + Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. + 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithGenericConstructSignaturesWithOptionalParameters.ts(130,15): error TS2430: Interface 'I8' incorrectly extends interface 'Base2'. Types of property 'a3' are incompatible. Type 'new (x?: T) => T' is not assignable to type 'new (x: T) => T'. @@ -55,10 +51,9 @@ tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingW Types of property 'a3' are incompatible. Type 'new (x: T, y: T) => T' is not assignable to type 'new (x: T) => T'. tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithGenericConstructSignaturesWithOptionalParameters.ts(143,15): error TS2430: Interface 'I11' incorrectly extends interface 'Base2'. - Types of property 'a4' are incompatible. - Type 'new () => T' is not assignable to type 'new (x: T, y?: T) => T'. - Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. - 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. + The types returned by 'new a4(...)' are incompatible between these types. + Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. + 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithGenericConstructSignaturesWithOptionalParameters.ts(147,15): error TS2430: Interface 'I12' incorrectly extends interface 'Base2'. Types of property 'a4' are incompatible. Type 'new (x?: T, y?: T) => T' is not assignable to type 'new (x: T, y?: T) => T'. @@ -78,10 +73,9 @@ tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingW Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithGenericConstructSignaturesWithOptionalParameters.ts(160,15): error TS2430: Interface 'I15' incorrectly extends interface 'Base2'. - Types of property 'a5' are incompatible. - Type 'new () => T' is not assignable to type 'new (x?: T, y?: T) => T'. - Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. - 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. + The types returned by 'new a5(...)' are incompatible between these types. + Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. + 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithGenericConstructSignaturesWithOptionalParameters.ts(164,15): error TS2430: Interface 'I16' incorrectly extends interface 'Base2'. Types of property 'a5' are incompatible. Type 'new (x?: T, y?: T) => T' is not assignable to type 'new (x?: T, y?: T) => T'. @@ -219,20 +213,18 @@ tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingW interface I1 extends Base2 { ~~ !!! error TS2430: Interface 'I1' incorrectly extends interface 'Base2'. -!!! error TS2430: Types of property 'a' are incompatible. -!!! error TS2430: Type 'new () => T' is not assignable to type 'new () => T'. -!!! error TS2430: Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. -!!! error TS2430: 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. +!!! error TS2430: The types returned by 'new a()' are incompatible between these types. +!!! error TS2430: Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. +!!! error TS2430: 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. a: new () => T; } interface I2 extends Base2 { ~~ !!! error TS2430: Interface 'I2' incorrectly extends interface 'Base2'. -!!! error TS2430: Types of property 'a' are incompatible. -!!! error TS2430: Type 'new (x?: T) => T' is not assignable to type 'new () => T'. -!!! error TS2430: Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. -!!! error TS2430: 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. +!!! error TS2430: The types returned by 'new a(...)' are incompatible between these types. +!!! error TS2430: Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. +!!! error TS2430: 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. a: new (x?: T) => T; } @@ -248,10 +240,9 @@ tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingW interface I4 extends Base2 { ~~ !!! error TS2430: Interface 'I4' incorrectly extends interface 'Base2'. -!!! error TS2430: Types of property 'a2' are incompatible. -!!! error TS2430: Type 'new () => T' is not assignable to type 'new (x?: T) => T'. -!!! error TS2430: Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. -!!! error TS2430: 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. +!!! error TS2430: The types returned by 'new a2(...)' are incompatible between these types. +!!! error TS2430: Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. +!!! error TS2430: 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. a2: new () => T; } @@ -281,10 +272,9 @@ tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingW interface I7 extends Base2 { ~~ !!! error TS2430: Interface 'I7' incorrectly extends interface 'Base2'. -!!! error TS2430: Types of property 'a3' are incompatible. -!!! error TS2430: Type 'new () => T' is not assignable to type 'new (x: T) => T'. -!!! error TS2430: Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. -!!! error TS2430: 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. +!!! error TS2430: The types returned by 'new a3(...)' are incompatible between these types. +!!! error TS2430: Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. +!!! error TS2430: 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. a3: new () => T; } @@ -322,10 +312,9 @@ tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingW interface I11 extends Base2 { ~~~ !!! error TS2430: Interface 'I11' incorrectly extends interface 'Base2'. -!!! error TS2430: Types of property 'a4' are incompatible. -!!! error TS2430: Type 'new () => T' is not assignable to type 'new (x: T, y?: T) => T'. -!!! error TS2430: Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. -!!! error TS2430: 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. +!!! error TS2430: The types returned by 'new a4(...)' are incompatible between these types. +!!! error TS2430: Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. +!!! error TS2430: 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. a4: new () => T; } @@ -366,10 +355,9 @@ tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingW interface I15 extends Base2 { ~~~ !!! error TS2430: Interface 'I15' incorrectly extends interface 'Base2'. -!!! error TS2430: Types of property 'a5' are incompatible. -!!! error TS2430: Type 'new () => T' is not assignable to type 'new (x?: T, y?: T) => T'. -!!! error TS2430: Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. -!!! error TS2430: 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. +!!! error TS2430: The types returned by 'new a5(...)' are incompatible between these types. +!!! error TS2430: Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated. +!!! error TS2430: 'T' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. a5: new () => T; } diff --git a/tests/baselines/reference/typeParameterArgumentEquivalence5.errors.txt b/tests/baselines/reference/typeParameterArgumentEquivalence5.errors.txt index e5196d3d4a5..b832ce6cb7c 100644 --- a/tests/baselines/reference/typeParameterArgumentEquivalence5.errors.txt +++ b/tests/baselines/reference/typeParameterArgumentEquivalence5.errors.txt @@ -1,9 +1,9 @@ tests/cases/compiler/typeParameterArgumentEquivalence5.ts(4,5): error TS2322: Type '() => (item: any) => T' is not assignable to type '() => (item: any) => U'. - Type '(item: any) => T' is not assignable to type '(item: any) => U'. + Call signature return types '(item: any) => T' and '(item: any) => U' are incompatible. Type 'T' is not assignable to type 'U'. 'T' is assignable to the constraint of type 'U', but 'U' could be instantiated with a different subtype of constraint '{}'. tests/cases/compiler/typeParameterArgumentEquivalence5.ts(5,5): error TS2322: Type '() => (item: any) => U' is not assignable to type '() => (item: any) => T'. - Type '(item: any) => U' is not assignable to type '(item: any) => T'. + Call signature return types '(item: any) => U' and '(item: any) => T' are incompatible. Type 'U' is not assignable to type 'T'. 'U' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. @@ -15,13 +15,13 @@ tests/cases/compiler/typeParameterArgumentEquivalence5.ts(5,5): error TS2322: Ty x = y; // Should be an error ~ !!! error TS2322: Type '() => (item: any) => T' is not assignable to type '() => (item: any) => U'. -!!! error TS2322: Type '(item: any) => T' is not assignable to type '(item: any) => U'. +!!! error TS2322: Call signature return types '(item: any) => T' and '(item: any) => U' are incompatible. !!! error TS2322: Type 'T' is not assignable to type 'U'. !!! error TS2322: 'T' is assignable to the constraint of type 'U', but 'U' could be instantiated with a different subtype of constraint '{}'. y = x; // Shound be an error ~ !!! error TS2322: Type '() => (item: any) => U' is not assignable to type '() => (item: any) => T'. -!!! error TS2322: Type '(item: any) => U' is not assignable to type '(item: any) => T'. +!!! error TS2322: Call signature return types '(item: any) => U' and '(item: any) => T' are incompatible. !!! error TS2322: Type 'U' is not assignable to type 'T'. !!! error TS2322: 'U' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. } diff --git a/tests/baselines/reference/types.asyncGenerators.es2018.2.errors.txt b/tests/baselines/reference/types.asyncGenerators.es2018.2.errors.txt index 9f22060b9e9..3d588abf1ee 100644 --- a/tests/baselines/reference/types.asyncGenerators.es2018.2.errors.txt +++ b/tests/baselines/reference/types.asyncGenerators.es2018.2.errors.txt @@ -1,39 +1,29 @@ tests/cases/conformance/types/asyncGenerators/types.asyncGenerators.es2018.2.ts(2,12): error TS2504: Type '{}' must have a '[Symbol.asyncIterator]()' method that returns an async iterator. tests/cases/conformance/types/asyncGenerators/types.asyncGenerators.es2018.2.ts(8,12): error TS2504: Type 'Promise' must have a '[Symbol.asyncIterator]()' method that returns an async iterator. tests/cases/conformance/types/asyncGenerators/types.asyncGenerators.es2018.2.ts(10,7): error TS2322: Type '() => AsyncGenerator' is not assignable to type '() => AsyncIterableIterator'. - Type 'AsyncGenerator' is not assignable to type 'AsyncIterableIterator'. - Types of property 'next' are incompatible. - Type '(...args: [] | [undefined]) => Promise>' is not assignable to type '(...args: [] | [undefined]) => Promise>'. - Type 'Promise>' is not assignable to type 'Promise>'. - Type 'IteratorResult' is not assignable to type 'IteratorResult'. - Type 'IteratorYieldResult' is not assignable to type 'IteratorResult'. - Type 'IteratorYieldResult' is not assignable to type 'IteratorYieldResult'. - Type 'string' is not assignable to type 'number'. + Call signature return types 'AsyncGenerator' and 'AsyncIterableIterator' are incompatible. + The types returned by 'next(...)' are incompatible between these types. + Type 'Promise>' is not assignable to type 'Promise>'. + Type 'IteratorResult' is not assignable to type 'IteratorResult'. + Type 'IteratorYieldResult' is not assignable to type 'IteratorResult'. + Type 'IteratorYieldResult' is not assignable to type 'IteratorYieldResult'. + Type 'string' is not assignable to type 'number'. tests/cases/conformance/types/asyncGenerators/types.asyncGenerators.es2018.2.ts(13,7): error TS2322: Type '() => AsyncGenerator' is not assignable to type '() => AsyncIterableIterator'. Type 'AsyncGenerator' is not assignable to type 'AsyncIterableIterator'. tests/cases/conformance/types/asyncGenerators/types.asyncGenerators.es2018.2.ts(16,7): error TS2322: Type '() => AsyncGenerator' is not assignable to type '() => AsyncIterableIterator'. - Type 'AsyncGenerator' is not assignable to type 'AsyncIterableIterator'. - Types of property 'next' are incompatible. - Type '(...args: [] | [unknown]) => Promise>' is not assignable to type '(...args: [] | [undefined]) => Promise>'. - Type 'Promise>' is not assignable to type 'Promise>'. + Call signature return types 'AsyncGenerator' and 'AsyncIterableIterator' are incompatible. + The types returned by 'next(...)' are incompatible between these types. + Type 'Promise>' is not assignable to type 'Promise>'. tests/cases/conformance/types/asyncGenerators/types.asyncGenerators.es2018.2.ts(19,7): error TS2322: Type '() => AsyncGenerator' is not assignable to type '() => AsyncIterable'. - Type 'AsyncGenerator' is not assignable to type 'AsyncIterable'. - Types of property '[Symbol.asyncIterator]' are incompatible. - Type '() => AsyncGenerator' is not assignable to type '() => AsyncIterator'. - Type 'AsyncGenerator' is not assignable to type 'AsyncIterator'. - Types of property 'next' are incompatible. - Type '(...args: [] | [undefined]) => Promise>' is not assignable to type '(...args: [] | [undefined]) => Promise>'. - Type 'Promise>' is not assignable to type 'Promise>'. + Call signature return types 'AsyncGenerator' and 'AsyncIterable' are incompatible. + The types returned by '[Symbol.asyncIterator]().next(...)' are incompatible between these types. + Type 'Promise>' is not assignable to type 'Promise>'. tests/cases/conformance/types/asyncGenerators/types.asyncGenerators.es2018.2.ts(22,7): error TS2322: Type '() => AsyncGenerator' is not assignable to type '() => AsyncIterable'. Type 'AsyncGenerator' is not assignable to type 'AsyncIterable'. tests/cases/conformance/types/asyncGenerators/types.asyncGenerators.es2018.2.ts(25,7): error TS2322: Type '() => AsyncGenerator' is not assignable to type '() => AsyncIterable'. - Type 'AsyncGenerator' is not assignable to type 'AsyncIterable'. - Types of property '[Symbol.asyncIterator]' are incompatible. - Type '() => AsyncGenerator' is not assignable to type '() => AsyncIterator'. - Type 'AsyncGenerator' is not assignable to type 'AsyncIterator'. - Types of property 'next' are incompatible. - Type '(...args: [] | [unknown]) => Promise>' is not assignable to type '(...args: [] | [undefined]) => Promise>'. - Type 'Promise>' is not assignable to type 'Promise>'. + Call signature return types 'AsyncGenerator' and 'AsyncIterable' are incompatible. + The types returned by '[Symbol.asyncIterator]().next(...)' are incompatible between these types. + Type 'Promise>' is not assignable to type 'Promise>'. tests/cases/conformance/types/asyncGenerators/types.asyncGenerators.es2018.2.ts(28,7): error TS2322: Type '() => AsyncGenerator' is not assignable to type '() => AsyncIterator'. Type 'AsyncGenerator' is not assignable to type 'AsyncIterator'. tests/cases/conformance/types/asyncGenerators/types.asyncGenerators.es2018.2.ts(31,7): error TS2322: Type '() => AsyncGenerator' is not assignable to type '() => AsyncIterator'. @@ -52,10 +42,9 @@ tests/cases/conformance/types/asyncGenerators/types.asyncGenerators.es2018.2.ts( tests/cases/conformance/types/asyncGenerators/types.asyncGenerators.es2018.2.ts(64,42): error TS2741: Property '[Symbol.iterator]' is missing in type 'AsyncGenerator' but required in type 'IterableIterator'. tests/cases/conformance/types/asyncGenerators/types.asyncGenerators.es2018.2.ts(67,42): error TS2741: Property '[Symbol.iterator]' is missing in type 'AsyncGenerator' but required in type 'Iterable'. tests/cases/conformance/types/asyncGenerators/types.asyncGenerators.es2018.2.ts(70,42): error TS2322: Type 'AsyncGenerator' is not assignable to type 'Iterator'. - Types of property 'next' are incompatible. - Type '(...args: [] | [undefined]) => Promise>' is not assignable to type '(...args: [] | [undefined]) => IteratorResult'. - Type 'Promise>' is not assignable to type 'IteratorResult'. - Property 'value' is missing in type 'Promise>' but required in type 'IteratorYieldResult'. + The types returned by 'next(...)' are incompatible between these types. + Type 'Promise>' is not assignable to type 'IteratorResult'. + Property 'value' is missing in type 'Promise>' but required in type 'IteratorYieldResult'. tests/cases/conformance/types/asyncGenerators/types.asyncGenerators.es2018.2.ts(74,12): error TS2504: Type '{}' must have a '[Symbol.asyncIterator]()' method that returns an async iterator. @@ -77,14 +66,13 @@ tests/cases/conformance/types/asyncGenerators/types.asyncGenerators.es2018.2.ts( const assignability1: () => AsyncIterableIterator = async function * () { ~~~~~~~~~~~~~~ !!! error TS2322: Type '() => AsyncGenerator' is not assignable to type '() => AsyncIterableIterator'. -!!! error TS2322: Type 'AsyncGenerator' is not assignable to type 'AsyncIterableIterator'. -!!! error TS2322: Types of property 'next' are incompatible. -!!! error TS2322: Type '(...args: [] | [undefined]) => Promise>' is not assignable to type '(...args: [] | [undefined]) => Promise>'. -!!! error TS2322: Type 'Promise>' is not assignable to type 'Promise>'. -!!! error TS2322: Type 'IteratorResult' is not assignable to type 'IteratorResult'. -!!! error TS2322: Type 'IteratorYieldResult' is not assignable to type 'IteratorResult'. -!!! error TS2322: Type 'IteratorYieldResult' is not assignable to type 'IteratorYieldResult'. -!!! error TS2322: Type 'string' is not assignable to type 'number'. +!!! error TS2322: Call signature return types 'AsyncGenerator' and 'AsyncIterableIterator' are incompatible. +!!! error TS2322: The types returned by 'next(...)' are incompatible between these types. +!!! error TS2322: Type 'Promise>' is not assignable to type 'Promise>'. +!!! error TS2322: Type 'IteratorResult' is not assignable to type 'IteratorResult'. +!!! error TS2322: Type 'IteratorYieldResult' is not assignable to type 'IteratorResult'. +!!! error TS2322: Type 'IteratorYieldResult' is not assignable to type 'IteratorYieldResult'. +!!! error TS2322: Type 'string' is not assignable to type 'number'. yield "a"; }; const assignability2: () => AsyncIterableIterator = async function * () { @@ -96,22 +84,17 @@ tests/cases/conformance/types/asyncGenerators/types.asyncGenerators.es2018.2.ts( const assignability3: () => AsyncIterableIterator = async function * () { ~~~~~~~~~~~~~~ !!! error TS2322: Type '() => AsyncGenerator' is not assignable to type '() => AsyncIterableIterator'. -!!! error TS2322: Type 'AsyncGenerator' is not assignable to type 'AsyncIterableIterator'. -!!! error TS2322: Types of property 'next' are incompatible. -!!! error TS2322: Type '(...args: [] | [unknown]) => Promise>' is not assignable to type '(...args: [] | [undefined]) => Promise>'. -!!! error TS2322: Type 'Promise>' is not assignable to type 'Promise>'. +!!! error TS2322: Call signature return types 'AsyncGenerator' and 'AsyncIterableIterator' are incompatible. +!!! error TS2322: The types returned by 'next(...)' are incompatible between these types. +!!! error TS2322: Type 'Promise>' is not assignable to type 'Promise>'. yield* (async function * () { yield "a"; })(); }; const assignability4: () => AsyncIterable = async function * () { ~~~~~~~~~~~~~~ !!! error TS2322: Type '() => AsyncGenerator' is not assignable to type '() => AsyncIterable'. -!!! error TS2322: Type 'AsyncGenerator' is not assignable to type 'AsyncIterable'. -!!! error TS2322: Types of property '[Symbol.asyncIterator]' are incompatible. -!!! error TS2322: Type '() => AsyncGenerator' is not assignable to type '() => AsyncIterator'. -!!! error TS2322: Type 'AsyncGenerator' is not assignable to type 'AsyncIterator'. -!!! error TS2322: Types of property 'next' are incompatible. -!!! error TS2322: Type '(...args: [] | [undefined]) => Promise>' is not assignable to type '(...args: [] | [undefined]) => Promise>'. -!!! error TS2322: Type 'Promise>' is not assignable to type 'Promise>'. +!!! error TS2322: Call signature return types 'AsyncGenerator' and 'AsyncIterable' are incompatible. +!!! error TS2322: The types returned by '[Symbol.asyncIterator]().next(...)' are incompatible between these types. +!!! error TS2322: Type 'Promise>' is not assignable to type 'Promise>'. yield "a"; }; const assignability5: () => AsyncIterable = async function * () { @@ -123,13 +106,9 @@ tests/cases/conformance/types/asyncGenerators/types.asyncGenerators.es2018.2.ts( const assignability6: () => AsyncIterable = async function * () { ~~~~~~~~~~~~~~ !!! error TS2322: Type '() => AsyncGenerator' is not assignable to type '() => AsyncIterable'. -!!! error TS2322: Type 'AsyncGenerator' is not assignable to type 'AsyncIterable'. -!!! error TS2322: Types of property '[Symbol.asyncIterator]' are incompatible. -!!! error TS2322: Type '() => AsyncGenerator' is not assignable to type '() => AsyncIterator'. -!!! error TS2322: Type 'AsyncGenerator' is not assignable to type 'AsyncIterator'. -!!! error TS2322: Types of property 'next' are incompatible. -!!! error TS2322: Type '(...args: [] | [unknown]) => Promise>' is not assignable to type '(...args: [] | [undefined]) => Promise>'. -!!! error TS2322: Type 'Promise>' is not assignable to type 'Promise>'. +!!! error TS2322: Call signature return types 'AsyncGenerator' and 'AsyncIterable' are incompatible. +!!! error TS2322: The types returned by '[Symbol.asyncIterator]().next(...)' are incompatible between these types. +!!! error TS2322: Type 'Promise>' is not assignable to type 'Promise>'. yield* (async function * () { yield "a"; })(); }; const assignability7: () => AsyncIterator = async function * () { @@ -210,10 +189,9 @@ tests/cases/conformance/types/asyncGenerators/types.asyncGenerators.es2018.2.ts( async function * explicitReturnType12(): Iterator { ~~~~~~~~~~~~~~~~ !!! error TS2322: Type 'AsyncGenerator' is not assignable to type 'Iterator'. -!!! error TS2322: Types of property 'next' are incompatible. -!!! error TS2322: Type '(...args: [] | [undefined]) => Promise>' is not assignable to type '(...args: [] | [undefined]) => IteratorResult'. -!!! error TS2322: Type 'Promise>' is not assignable to type 'IteratorResult'. -!!! error TS2322: Property 'value' is missing in type 'Promise>' but required in type 'IteratorYieldResult'. +!!! error TS2322: The types returned by 'next(...)' are incompatible between these types. +!!! error TS2322: Type 'Promise>' is not assignable to type 'IteratorResult'. +!!! error TS2322: Property 'value' is missing in type 'Promise>' but required in type 'IteratorYieldResult'. !!! related TS2728 /.ts/lib.es2015.iterable.d.ts:33:5: 'value' is declared here. yield 1; } diff --git a/tests/cases/compiler/deeplyNestedAssignabilityErrorsCombined.ts b/tests/cases/compiler/deeplyNestedAssignabilityErrorsCombined.ts new file mode 100644 index 00000000000..d802c707830 --- /dev/null +++ b/tests/cases/compiler/deeplyNestedAssignabilityErrorsCombined.ts @@ -0,0 +1,15 @@ +let x = { a: { b: { c: { d: { e: { f() { return { g: "hello" }; } } } } } } }; +let y = { a: { b: { c: { d: { e: { f() { return { g: 12345 }; } } } } } } }; +x = y; + +class Ctor1 { + g = "ok" +} + +class Ctor2 { + g = 12; +} + +let x2 = { a: { b: { c: { d: { e: { f: Ctor1 } } } } } }; +let y2 = { a: { b: { c: { d: { e: { f: Ctor2 } } } } } }; +x2 = y2; \ No newline at end of file diff --git a/tests/cases/compiler/nestedCallbackErrorNotFlattened.ts b/tests/cases/compiler/nestedCallbackErrorNotFlattened.ts new file mode 100644 index 00000000000..76a1fabdf6e --- /dev/null +++ b/tests/cases/compiler/nestedCallbackErrorNotFlattened.ts @@ -0,0 +1,7 @@ +// @strict: true +type Cb = {noAlias: () => T}["noAlias"]; // `"noAlias"` here prevents an alias symbol from being made +// which means the comparison will definitely be structural, rather than by variance + +declare const x: Cb>>>; // one more layer of `Cb` adn we'd get a `true` from the deeply-nested symbol check +declare let y: Cb>>>; +y = x; \ No newline at end of file From 367b82055cf231948901e472cb3531450a8e0d70 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Mon, 23 Sep 2019 16:52:03 -0700 Subject: [PATCH 91/97] =?UTF-8?q?Hoist=20and=20distribute=20type=20paramet?= =?UTF-8?q?er=20constraints=20over=20type=20parameters=20=E2=80=A6=20(#334?= =?UTF-8?q?53)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Hoist and distribute type parameter constraints over type parameters when comparing against union targets when fetching union constraints * Fix PR nits --- src/compiler/checker.ts | 26 ++++++++---- ...intersectionWithUnionConstraint.errors.txt | 40 +++++-------------- .../keyofAndIndexedAccessErrors.errors.txt | 8 ---- ...ameterExtendsUnionConstraintDistributed.js | 11 +++++ ...rExtendsUnionConstraintDistributed.symbols | 32 +++++++++++++++ ...terExtendsUnionConstraintDistributed.types | 17 ++++++++ ...ameterExtendsUnionConstraintDistributed.ts | 5 +++ 7 files changed, 93 insertions(+), 46 deletions(-) create mode 100644 tests/baselines/reference/typeParameterExtendsUnionConstraintDistributed.js create mode 100644 tests/baselines/reference/typeParameterExtendsUnionConstraintDistributed.symbols create mode 100644 tests/baselines/reference/typeParameterExtendsUnionConstraintDistributed.types create mode 100644 tests/cases/conformance/jsdoc/typeParameterExtendsUnionConstraintDistributed.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index de5c3b5b358..b3f88661a8c 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7965,10 +7965,10 @@ namespace ts { return hasNonCircularBaseConstraint(type) ? getConstraintFromConditionalType(type) : undefined; } - function getUnionConstraintOfIntersection(type: IntersectionType, targetIsUnion: boolean) { + function getEffectiveConstraintOfIntersection(types: readonly Type[], targetIsUnion: boolean) { let constraints: Type[] | undefined; let hasDisjointDomainType = false; - for (const t of type.types) { + for (const t of types) { if (t.flags & TypeFlags.Instantiable) { // We keep following constraints as long as we have an instantiable type that is known // not to be circular or infinite (hence we stop on index access types). @@ -7978,6 +7978,9 @@ namespace ts { } if (constraint) { constraints = append(constraints, constraint); + if (targetIsUnion) { + constraints = append(constraints, t); + } } } else if (t.flags & TypeFlags.DisjointDomains) { @@ -7990,7 +7993,7 @@ namespace ts { if (hasDisjointDomainType) { // We add any types belong to one of the disjoint domains because they might cause the final // intersection operation to reduce the union constraints. - for (const t of type.types) { + for (const t of types) { if (t.flags & TypeFlags.DisjointDomains) { constraints = append(constraints, t); } @@ -13089,7 +13092,7 @@ namespace ts { } } } - if (!result && source.flags & TypeFlags.Intersection) { + if (!result && source.flags & (TypeFlags.Intersection | TypeFlags.TypeParameter)) { // The combined constraint of an intersection type is the intersection of the constraints of // the constituents. When an intersection type contains instantiable types with union type // constraints, there are situations where we need to examine the combined constraint. One is @@ -13099,10 +13102,17 @@ namespace ts { // we need to check this constraint against a union on the target side. Also, given a type // variable V constrained to 'string | number', 'V & number' has a combined constraint of // 'string & number | number & number' which reduces to just 'number'. - const constraint = getUnionConstraintOfIntersection(source, !!(target.flags & TypeFlags.Union)); - if (constraint) { - if (result = isRelatedTo(constraint, target, reportErrors, /*headMessage*/ undefined, isIntersectionConstituent)) { - resetErrorInfo(saveErrorInfo); + // This also handles type parameters, as a type parameter with a union constraint compared against a union + // needs to have its constraint hoisted into an intersection with said type parameter, this way + // the type param can be compared with itself in the target (with the influence of its constraint to match other parts) + // For example, if `T extends 1 | 2` and `U extends 2 | 3` and we compare `T & U` to `T & U & (1 | 2 | 3)` + const constraint = getEffectiveConstraintOfIntersection(source.flags & TypeFlags.Intersection ? (source).types: [source], !!(target.flags & TypeFlags.Union)); + if (constraint && (source.flags & TypeFlags.Intersection || target.flags & TypeFlags.Union)) { + if (everyType(constraint, c => c !== source)) { // Skip comparison if expansion contains the source itself + // TODO: Stack errors so we get a pyramid for the "normal" comparison above, _and_ a second for this + if (result = isRelatedTo(constraint, target, /*reportErrors*/ false, /*headMessage*/ undefined, isIntersectionConstituent)) { + resetErrorInfo(saveErrorInfo); + } } } } diff --git a/tests/baselines/reference/intersectionWithUnionConstraint.errors.txt b/tests/baselines/reference/intersectionWithUnionConstraint.errors.txt index 97ed1cf4895..7bc436915a1 100644 --- a/tests/baselines/reference/intersectionWithUnionConstraint.errors.txt +++ b/tests/baselines/reference/intersectionWithUnionConstraint.errors.txt @@ -1,23 +1,13 @@ tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts(7,9): error TS2322: Type 'T & U' is not assignable to type 'string | number'. - Type 'string | undefined' is not assignable to type 'string | number'. - Type 'undefined' is not assignable to type 'string | number'. - Type 'T & U' is not assignable to type 'number'. + Type 'T & U' is not assignable to type 'number'. tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts(8,9): error TS2322: Type 'T & U' is not assignable to type 'string | null'. - Type 'string | undefined' is not assignable to type 'string | null'. - Type 'undefined' is not assignable to type 'string | null'. - Type 'T & U' is not assignable to type 'string'. + Type 'T & U' is not assignable to type 'string'. tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts(10,9): error TS2322: Type 'T & U' is not assignable to type 'number | null'. - Type 'string | undefined' is not assignable to type 'number | null'. - Type 'undefined' is not assignable to type 'number | null'. - Type 'T & U' is not assignable to type 'number'. + Type 'T & U' is not assignable to type 'number'. tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts(11,9): error TS2322: Type 'T & U' is not assignable to type 'number | undefined'. - Type 'string | undefined' is not assignable to type 'number | undefined'. - Type 'string' is not assignable to type 'number | undefined'. - Type 'T & U' is not assignable to type 'number'. + Type 'T & U' is not assignable to type 'number'. tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts(12,9): error TS2322: Type 'T & U' is not assignable to type 'null | undefined'. - Type 'string | undefined' is not assignable to type 'null | undefined'. - Type 'string' is not assignable to type 'null | undefined'. - Type 'T & U' is not assignable to type 'null'. + Type 'T & U' is not assignable to type 'null'. ==== tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts (5 errors) ==== @@ -30,34 +20,24 @@ tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts(12 let y1: string | number = x; // Error ~~ !!! error TS2322: Type 'T & U' is not assignable to type 'string | number'. -!!! error TS2322: Type 'string | undefined' is not assignable to type 'string | number'. -!!! error TS2322: Type 'undefined' is not assignable to type 'string | number'. -!!! error TS2322: Type 'T & U' is not assignable to type 'number'. +!!! error TS2322: Type 'T & U' is not assignable to type 'number'. let y2: string | null = x; // Error ~~ !!! error TS2322: Type 'T & U' is not assignable to type 'string | null'. -!!! error TS2322: Type 'string | undefined' is not assignable to type 'string | null'. -!!! error TS2322: Type 'undefined' is not assignable to type 'string | null'. -!!! error TS2322: Type 'T & U' is not assignable to type 'string'. +!!! error TS2322: Type 'T & U' is not assignable to type 'string'. let y3: string | undefined = x; let y4: number | null = x; // Error ~~ !!! error TS2322: Type 'T & U' is not assignable to type 'number | null'. -!!! error TS2322: Type 'string | undefined' is not assignable to type 'number | null'. -!!! error TS2322: Type 'undefined' is not assignable to type 'number | null'. -!!! error TS2322: Type 'T & U' is not assignable to type 'number'. +!!! error TS2322: Type 'T & U' is not assignable to type 'number'. let y5: number | undefined = x; // Error ~~ !!! error TS2322: Type 'T & U' is not assignable to type 'number | undefined'. -!!! error TS2322: Type 'string | undefined' is not assignable to type 'number | undefined'. -!!! error TS2322: Type 'string' is not assignable to type 'number | undefined'. -!!! error TS2322: Type 'T & U' is not assignable to type 'number'. +!!! error TS2322: Type 'T & U' is not assignable to type 'number'. let y6: null | undefined = x; // Error ~~ !!! error TS2322: Type 'T & U' is not assignable to type 'null | undefined'. -!!! error TS2322: Type 'string | undefined' is not assignable to type 'null | undefined'. -!!! error TS2322: Type 'string' is not assignable to type 'null | undefined'. -!!! error TS2322: Type 'T & U' is not assignable to type 'null'. +!!! error TS2322: Type 'T & U' is not assignable to type 'null'. } type T1 = (string | number | undefined) & (string | null | undefined); // string | undefined diff --git a/tests/baselines/reference/keyofAndIndexedAccessErrors.errors.txt b/tests/baselines/reference/keyofAndIndexedAccessErrors.errors.txt index 69f992447e8..bb628d4f4fb 100644 --- a/tests/baselines/reference/keyofAndIndexedAccessErrors.errors.txt +++ b/tests/baselines/reference/keyofAndIndexedAccessErrors.errors.txt @@ -48,8 +48,6 @@ tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(103,9): error 'string & keyof T' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint 'string'. Type 'string' is not assignable to type 'K'. 'string' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint 'string'. - Type 'string' is not assignable to type 'K'. - 'string' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint 'string'. tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(105,9): error TS2322: Type 'T[Extract]' is not assignable to type 'T[K]'. Type 'Extract' is not assignable to type 'K'. 'Extract' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint 'string'. @@ -68,8 +66,6 @@ tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(114,5): error 'string & keyof T' is assignable to the constraint of type 'J', but 'J' could be instantiated with a different subtype of constraint 'string'. Type 'string' is not assignable to type 'J'. 'string' is assignable to the constraint of type 'J', but 'J' could be instantiated with a different subtype of constraint 'string'. - Type 'string' is not assignable to type 'J'. - 'string' is assignable to the constraint of type 'J', but 'J' could be instantiated with a different subtype of constraint 'string'. tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(117,5): error TS2322: Type 'T[K]' is not assignable to type 'U[J]'. Type 'T' is not assignable to type 'U'. 'T' is assignable to the constraint of type 'U', but 'U' could be instantiated with a different subtype of constraint '{}'. @@ -264,8 +260,6 @@ tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(142,5): error !!! error TS2322: 'string & keyof T' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint 'string'. !!! error TS2322: Type 'string' is not assignable to type 'K'. !!! error TS2322: 'string' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint 'string'. -!!! error TS2322: Type 'string' is not assignable to type 'K'. -!!! error TS2322: 'string' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint 'string'. t[key] = tk; // ok, T[K] ==> T[keyof T] tk = t[key]; // error, T[keyof T] =/=> T[K] ~~ @@ -299,8 +293,6 @@ tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(142,5): error !!! error TS2322: 'string & keyof T' is assignable to the constraint of type 'J', but 'J' could be instantiated with a different subtype of constraint 'string'. !!! error TS2322: Type 'string' is not assignable to type 'J'. !!! error TS2322: 'string' is assignable to the constraint of type 'J', but 'J' could be instantiated with a different subtype of constraint 'string'. -!!! error TS2322: Type 'string' is not assignable to type 'J'. -!!! error TS2322: 'string' is assignable to the constraint of type 'J', but 'J' could be instantiated with a different subtype of constraint 'string'. tk = uj; uj = tk; // error diff --git a/tests/baselines/reference/typeParameterExtendsUnionConstraintDistributed.js b/tests/baselines/reference/typeParameterExtendsUnionConstraintDistributed.js new file mode 100644 index 00000000000..ea8ac927061 --- /dev/null +++ b/tests/baselines/reference/typeParameterExtendsUnionConstraintDistributed.js @@ -0,0 +1,11 @@ +//// [typeParameterExtendsUnionConstraintDistributed.ts] +type A = 1 | 2; +function f(a: T): A & T { return a; } // Shouldn't error + +type B = 2 | 3; +function f2(ab: T & U): (A | B) & T & U { return ab; } // Also shouldn't error + + +//// [typeParameterExtendsUnionConstraintDistributed.js] +function f(a) { return a; } // Shouldn't error +function f2(ab) { return ab; } // Also shouldn't error diff --git a/tests/baselines/reference/typeParameterExtendsUnionConstraintDistributed.symbols b/tests/baselines/reference/typeParameterExtendsUnionConstraintDistributed.symbols new file mode 100644 index 00000000000..2d47ed96e14 --- /dev/null +++ b/tests/baselines/reference/typeParameterExtendsUnionConstraintDistributed.symbols @@ -0,0 +1,32 @@ +=== tests/cases/conformance/jsdoc/typeParameterExtendsUnionConstraintDistributed.ts === +type A = 1 | 2; +>A : Symbol(A, Decl(typeParameterExtendsUnionConstraintDistributed.ts, 0, 0)) + +function f(a: T): A & T { return a; } // Shouldn't error +>f : Symbol(f, Decl(typeParameterExtendsUnionConstraintDistributed.ts, 0, 15)) +>T : Symbol(T, Decl(typeParameterExtendsUnionConstraintDistributed.ts, 1, 11)) +>A : Symbol(A, Decl(typeParameterExtendsUnionConstraintDistributed.ts, 0, 0)) +>a : Symbol(a, Decl(typeParameterExtendsUnionConstraintDistributed.ts, 1, 24)) +>T : Symbol(T, Decl(typeParameterExtendsUnionConstraintDistributed.ts, 1, 11)) +>A : Symbol(A, Decl(typeParameterExtendsUnionConstraintDistributed.ts, 0, 0)) +>T : Symbol(T, Decl(typeParameterExtendsUnionConstraintDistributed.ts, 1, 11)) +>a : Symbol(a, Decl(typeParameterExtendsUnionConstraintDistributed.ts, 1, 24)) + +type B = 2 | 3; +>B : Symbol(B, Decl(typeParameterExtendsUnionConstraintDistributed.ts, 1, 50)) + +function f2(ab: T & U): (A | B) & T & U { return ab; } // Also shouldn't error +>f2 : Symbol(f2, Decl(typeParameterExtendsUnionConstraintDistributed.ts, 3, 15)) +>T : Symbol(T, Decl(typeParameterExtendsUnionConstraintDistributed.ts, 4, 12)) +>A : Symbol(A, Decl(typeParameterExtendsUnionConstraintDistributed.ts, 0, 0)) +>U : Symbol(U, Decl(typeParameterExtendsUnionConstraintDistributed.ts, 4, 24)) +>B : Symbol(B, Decl(typeParameterExtendsUnionConstraintDistributed.ts, 1, 50)) +>ab : Symbol(ab, Decl(typeParameterExtendsUnionConstraintDistributed.ts, 4, 38)) +>T : Symbol(T, Decl(typeParameterExtendsUnionConstraintDistributed.ts, 4, 12)) +>U : Symbol(U, Decl(typeParameterExtendsUnionConstraintDistributed.ts, 4, 24)) +>A : Symbol(A, Decl(typeParameterExtendsUnionConstraintDistributed.ts, 0, 0)) +>B : Symbol(B, Decl(typeParameterExtendsUnionConstraintDistributed.ts, 1, 50)) +>T : Symbol(T, Decl(typeParameterExtendsUnionConstraintDistributed.ts, 4, 12)) +>U : Symbol(U, Decl(typeParameterExtendsUnionConstraintDistributed.ts, 4, 24)) +>ab : Symbol(ab, Decl(typeParameterExtendsUnionConstraintDistributed.ts, 4, 38)) + diff --git a/tests/baselines/reference/typeParameterExtendsUnionConstraintDistributed.types b/tests/baselines/reference/typeParameterExtendsUnionConstraintDistributed.types new file mode 100644 index 00000000000..4e81c9728c4 --- /dev/null +++ b/tests/baselines/reference/typeParameterExtendsUnionConstraintDistributed.types @@ -0,0 +1,17 @@ +=== tests/cases/conformance/jsdoc/typeParameterExtendsUnionConstraintDistributed.ts === +type A = 1 | 2; +>A : A + +function f(a: T): A & T { return a; } // Shouldn't error +>f : (a: T) => (1 & T) | (2 & T) +>a : T +>a : T + +type B = 2 | 3; +>B : B + +function f2(ab: T & U): (A | B) & T & U { return ab; } // Also shouldn't error +>f2 : (ab: T & U) => (1 & T & U) | (2 & T & U) | (3 & T & U) +>ab : T & U +>ab : T & U + diff --git a/tests/cases/conformance/jsdoc/typeParameterExtendsUnionConstraintDistributed.ts b/tests/cases/conformance/jsdoc/typeParameterExtendsUnionConstraintDistributed.ts new file mode 100644 index 00000000000..2a87bb3ccaa --- /dev/null +++ b/tests/cases/conformance/jsdoc/typeParameterExtendsUnionConstraintDistributed.ts @@ -0,0 +1,5 @@ +type A = 1 | 2; +function f(a: T): A & T { return a; } // Shouldn't error + +type B = 2 | 3; +function f2(ab: T & U): (A | B) & T & U { return ab; } // Also shouldn't error From 00a43d7b49cc3b067a33c44f6e953a9d73b9e904 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Mon, 23 Sep 2019 16:55:19 -0700 Subject: [PATCH 92/97] Cache propagating variance flags in the relationship cache (#32225) * Cache propagating variance flags in the relationship cache * Convert base fields in relation comparison result to flags --- src/compiler/checker.ts | 58 +++++++------- src/compiler/types.ts | 10 ++- ...eRepeatedlyPropegatesWithUnreliableFlag.js | 25 ++++++ ...atedlyPropegatesWithUnreliableFlag.symbols | 77 +++++++++++++++++++ ...peatedlyPropegatesWithUnreliableFlag.types | 51 ++++++++++++ ...eRepeatedlyPropegatesWithUnreliableFlag.ts | 17 ++++ 6 files changed, 206 insertions(+), 32 deletions(-) create mode 100644 tests/baselines/reference/varianceRepeatedlyPropegatesWithUnreliableFlag.js create mode 100644 tests/baselines/reference/varianceRepeatedlyPropegatesWithUnreliableFlag.symbols create mode 100644 tests/baselines/reference/varianceRepeatedlyPropegatesWithUnreliableFlag.types create mode 100644 tests/cases/compiler/varianceRepeatedlyPropegatesWithUnreliableFlag.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index b3f88661a8c..fdd3cac69b4 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -12561,12 +12561,12 @@ namespace ts { return true; } const id = getSymbolId(sourceSymbol) + "," + getSymbolId(targetSymbol); - const relation = enumRelation.get(id); - if (relation !== undefined && !(relation === RelationComparisonResult.Failed && errorReporter)) { - return relation === RelationComparisonResult.Succeeded; + const entry = enumRelation.get(id); + if (entry !== undefined && !(!(entry & RelationComparisonResult.Reported) && entry & RelationComparisonResult.Failed && errorReporter)) { + return !!(entry & RelationComparisonResult.Succeeded); } if (sourceSymbol.escapedName !== targetSymbol.escapedName || !(sourceSymbol.flags & SymbolFlags.RegularEnum) || !(targetSymbol.flags & SymbolFlags.RegularEnum)) { - enumRelation.set(id, RelationComparisonResult.FailedAndReported); + enumRelation.set(id, RelationComparisonResult.Failed | RelationComparisonResult.Reported); return false; } const targetEnumType = getTypeOfSymbol(targetSymbol); @@ -12577,7 +12577,7 @@ namespace ts { if (errorReporter) { errorReporter(Diagnostics.Property_0_is_missing_in_type_1, symbolName(property), typeToString(getDeclaredTypeOfSymbol(targetSymbol), /*enclosingDeclaration*/ undefined, TypeFormatFlags.UseFullyQualifiedType)); - enumRelation.set(id, RelationComparisonResult.FailedAndReported); + enumRelation.set(id, RelationComparisonResult.Failed | RelationComparisonResult.Reported); } else { enumRelation.set(id, RelationComparisonResult.Failed); @@ -12642,7 +12642,7 @@ namespace ts { if (source.flags & TypeFlags.Object && target.flags & TypeFlags.Object) { const related = relation.get(getRelationKey(source, target, relation)); if (related !== undefined) { - return related === RelationComparisonResult.Succeeded; + return !!(related & RelationComparisonResult.Succeeded); } } if (source.flags & TypeFlags.StructuredOrInstantiable || target.flags & TypeFlags.StructuredOrInstantiable) { @@ -13463,18 +13463,6 @@ namespace ts { return result; } - function propagateSidebandVarianceFlags(typeArguments: readonly Type[], variances: VarianceFlags[]) { - for (let i = 0; i < variances.length; i++) { - const v = variances[i]; - if (v & VarianceFlags.Unmeasurable) { - instantiateType(typeArguments[i], reportUnmeasurableMarkers); - } - if (v & VarianceFlags.Unreliable) { - instantiateType(typeArguments[i], reportUnreliableMarkers); - } - } - } - // Determine if possibly recursive types are related. First, check if the result is already available in the global cache. // Second, check if we have already started a comparison of the given two types in which case we assume the result to be true. // Third, check if both types are part of deeply nested chains of generic type instantiations and if so assume the types are @@ -13485,24 +13473,24 @@ namespace ts { return Ternary.False; } const id = getRelationKey(source, target, relation); - const related = relation.get(id); - if (related !== undefined) { - if (reportErrors && related === RelationComparisonResult.Failed) { + const entry = relation.get(id); + if (entry !== undefined) { + if (reportErrors && entry & RelationComparisonResult.Failed && !(entry & RelationComparisonResult.Reported)) { // We are elaborating errors and the cached result is an unreported failure. The result will be reported // as a failure, and should be updated as a reported failure by the bottom of this function. } else { if (outofbandVarianceMarkerHandler) { // We're in the middle of variance checking - integrate any unmeasurable/unreliable flags from this cached component - if (source.flags & (TypeFlags.Object | TypeFlags.Conditional) && source.aliasSymbol && - source.aliasTypeArguments && source.aliasSymbol === target.aliasSymbol) { - propagateSidebandVarianceFlags(source.aliasTypeArguments, getAliasVariances(source.aliasSymbol)); + const saved = entry & RelationComparisonResult.ReportsMask; + if (saved & RelationComparisonResult.ReportsUnmeasurable) { + instantiateType(source, reportUnmeasurableMarkers); } - if (getObjectFlags(source) & ObjectFlags.Reference && getObjectFlags(target) & ObjectFlags.Reference && (source).target === (target).target && length((source).typeArguments)) { - propagateSidebandVarianceFlags((source).typeArguments!, getVariances((source).target)); + if (saved & RelationComparisonResult.ReportsUnreliable) { + instantiateType(source, reportUnreliableMarkers); } } - return related === RelationComparisonResult.Succeeded ? Ternary.True : Ternary.False; + return entry & RelationComparisonResult.Succeeded ? Ternary.True : Ternary.False; } } if (!maybeKeys) { @@ -13531,14 +13519,26 @@ namespace ts { const saveExpandingFlags = expandingFlags; if (!(expandingFlags & ExpandingFlags.Source) && isDeeplyNestedType(source, sourceStack, depth)) expandingFlags |= ExpandingFlags.Source; if (!(expandingFlags & ExpandingFlags.Target) && isDeeplyNestedType(target, targetStack, depth)) expandingFlags |= ExpandingFlags.Target; + let originalHandler: typeof outofbandVarianceMarkerHandler; + let propagatingVarianceFlags: RelationComparisonResult = 0; + if (outofbandVarianceMarkerHandler) { + originalHandler = outofbandVarianceMarkerHandler; + outofbandVarianceMarkerHandler = onlyUnreliable => { + propagatingVarianceFlags |= onlyUnreliable ? RelationComparisonResult.ReportsUnreliable : RelationComparisonResult.ReportsUnmeasurable; + return originalHandler!(onlyUnreliable); + }; + } const result = expandingFlags !== ExpandingFlags.Both ? structuredTypeRelatedTo(source, target, reportErrors, isIntersectionConstituent) : Ternary.Maybe; + if (outofbandVarianceMarkerHandler) { + outofbandVarianceMarkerHandler = originalHandler; + } expandingFlags = saveExpandingFlags; depth--; if (result) { if (result === Ternary.True || depth === 0) { // If result is definitely true, record all maybe keys as having succeeded for (let i = maybeStart; i < maybeCount; i++) { - relation.set(maybeKeys[i], RelationComparisonResult.Succeeded); + relation.set(maybeKeys[i], RelationComparisonResult.Succeeded | propagatingVarianceFlags); } maybeCount = maybeStart; } @@ -13546,7 +13546,7 @@ namespace ts { else { // A false result goes straight into global cache (when something is false under // assumptions it will also be false without assumptions) - relation.set(id, reportErrors ? RelationComparisonResult.FailedAndReported : RelationComparisonResult.Failed); + relation.set(id, (reportErrors ? RelationComparisonResult.Reported : 0) | RelationComparisonResult.Failed | propagatingVarianceFlags); maybeCount = maybeStart; } return result; diff --git a/src/compiler/types.ts b/src/compiler/types.ts index eb568ed5cff..76bc461d878 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -614,9 +614,13 @@ namespace ts { /* @internal */ export const enum RelationComparisonResult { - Succeeded = 1, // Should be truthy - Failed = 2, - FailedAndReported = 3 + Succeeded = 1 << 0, // Should be truthy + Failed = 1 << 1, + Reported = 1 << 2, + + ReportsUnmeasurable = 1 << 3, + ReportsUnreliable = 1 << 4, + ReportsMask = ReportsUnmeasurable | ReportsUnreliable } export interface Node extends TextRange { diff --git a/tests/baselines/reference/varianceRepeatedlyPropegatesWithUnreliableFlag.js b/tests/baselines/reference/varianceRepeatedlyPropegatesWithUnreliableFlag.js new file mode 100644 index 00000000000..73062e82014 --- /dev/null +++ b/tests/baselines/reference/varianceRepeatedlyPropegatesWithUnreliableFlag.js @@ -0,0 +1,25 @@ +//// [varianceRepeatedlyPropegatesWithUnreliableFlag.ts] +type A = { a: number }; +type B = { b: number }; +type X = ({ [K in keyof T]: T[K] } & Record)[keyof T]; +type P1 = { data: X }; +type P2 = { data: X }; + +interface I { + fn(p1: P1>, p2: P2>): void; +} + +const i: I = null as any; +const p2: P2 = null as any; + +// Commenting out the below line will remove the error on the `const _i: I = i;` +i.fn(null as any, p2); + +const _i: I = i; + +//// [varianceRepeatedlyPropegatesWithUnreliableFlag.js] +var i = null; +var p2 = null; +// Commenting out the below line will remove the error on the `const _i: I = i;` +i.fn(null, p2); +var _i = i; diff --git a/tests/baselines/reference/varianceRepeatedlyPropegatesWithUnreliableFlag.symbols b/tests/baselines/reference/varianceRepeatedlyPropegatesWithUnreliableFlag.symbols new file mode 100644 index 00000000000..b2e1f46f1e8 --- /dev/null +++ b/tests/baselines/reference/varianceRepeatedlyPropegatesWithUnreliableFlag.symbols @@ -0,0 +1,77 @@ +=== tests/cases/compiler/varianceRepeatedlyPropegatesWithUnreliableFlag.ts === +type A = { a: number }; +>A : Symbol(A, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 0, 0)) +>a : Symbol(a, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 0, 10)) + +type B = { b: number }; +>B : Symbol(B, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 0, 23)) +>b : Symbol(b, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 1, 10)) + +type X = ({ [K in keyof T]: T[K] } & Record)[keyof T]; +>X : Symbol(X, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 1, 23)) +>T : Symbol(T, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 2, 7)) +>K : Symbol(K, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 2, 16)) +>T : Symbol(T, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 2, 7)) +>T : Symbol(T, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 2, 7)) +>K : Symbol(K, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 2, 16)) +>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) +>T : Symbol(T, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 2, 7)) + +type P1 = { data: X }; +>P1 : Symbol(P1, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 2, 71)) +>T : Symbol(T, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 3, 8)) +>data : Symbol(data, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 3, 14)) +>X : Symbol(X, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 1, 23)) +>T : Symbol(T, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 3, 8)) + +type P2 = { data: X }; +>P2 : Symbol(P2, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 3, 28)) +>T : Symbol(T, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 4, 8)) +>data : Symbol(data, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 4, 14)) +>X : Symbol(X, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 1, 23)) +>T : Symbol(T, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 4, 8)) + +interface I { +>I : Symbol(I, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 4, 28)) +>T : Symbol(T, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 6, 12)) + + fn(p1: P1>, p2: P2>): void; +>fn : Symbol(I.fn, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 6, 16)) +>K : Symbol(K, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 7, 7)) +>T : Symbol(T, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 6, 12)) +>p1 : Symbol(p1, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 7, 26)) +>P1 : Symbol(P1, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 2, 71)) +>Pick : Symbol(Pick, Decl(lib.es5.d.ts, --, --)) +>T : Symbol(T, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 6, 12)) +>K : Symbol(K, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 7, 7)) +>p2 : Symbol(p2, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 7, 45)) +>P2 : Symbol(P2, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 3, 28)) +>Pick : Symbol(Pick, Decl(lib.es5.d.ts, --, --)) +>T : Symbol(T, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 6, 12)) +>K : Symbol(K, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 7, 7)) +} + +const i: I = null as any; +>i : Symbol(i, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 10, 5)) +>I : Symbol(I, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 4, 28)) +>A : Symbol(A, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 0, 0)) +>B : Symbol(B, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 0, 23)) + +const p2: P2 = null as any; +>p2 : Symbol(p2, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 11, 5)) +>P2 : Symbol(P2, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 3, 28)) +>A : Symbol(A, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 0, 0)) + +// Commenting out the below line will remove the error on the `const _i: I = i;` +i.fn(null as any, p2); +>i.fn : Symbol(I.fn, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 6, 16)) +>i : Symbol(i, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 10, 5)) +>fn : Symbol(I.fn, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 6, 16)) +>p2 : Symbol(p2, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 11, 5)) + +const _i: I = i; +>_i : Symbol(_i, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 16, 5)) +>I : Symbol(I, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 4, 28)) +>A : Symbol(A, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 0, 0)) +>i : Symbol(i, Decl(varianceRepeatedlyPropegatesWithUnreliableFlag.ts, 10, 5)) + diff --git a/tests/baselines/reference/varianceRepeatedlyPropegatesWithUnreliableFlag.types b/tests/baselines/reference/varianceRepeatedlyPropegatesWithUnreliableFlag.types new file mode 100644 index 00000000000..82bf01d05d9 --- /dev/null +++ b/tests/baselines/reference/varianceRepeatedlyPropegatesWithUnreliableFlag.types @@ -0,0 +1,51 @@ +=== tests/cases/compiler/varianceRepeatedlyPropegatesWithUnreliableFlag.ts === +type A = { a: number }; +>A : A +>a : number + +type B = { b: number }; +>B : B +>b : number + +type X = ({ [K in keyof T]: T[K] } & Record)[keyof T]; +>X : ({ [K in keyof T]: T[K]; } & Record)[keyof T] + +type P1 = { data: X }; +>P1 : P1 +>data : ({ [K in keyof T]: T[K]; } & Record)[keyof T] + +type P2 = { data: X }; +>P2 : P2 +>data : ({ [K in keyof T]: T[K]; } & Record)[keyof T] + +interface I { + fn(p1: P1>, p2: P2>): void; +>fn : (p1: P1>, p2: P2>) => void +>p1 : P1> +>p2 : P2> +} + +const i: I = null as any; +>i : I +>null as any : any +>null : null + +const p2: P2 = null as any; +>p2 : P2 +>null as any : any +>null : null + +// Commenting out the below line will remove the error on the `const _i: I = i;` +i.fn(null as any, p2); +>i.fn(null as any, p2) : void +>i.fn : (p1: P1>, p2: P2>) => void +>i : I +>fn : (p1: P1>, p2: P2>) => void +>null as any : any +>null : null +>p2 : P2 + +const _i: I = i; +>_i : I +>i : I + diff --git a/tests/cases/compiler/varianceRepeatedlyPropegatesWithUnreliableFlag.ts b/tests/cases/compiler/varianceRepeatedlyPropegatesWithUnreliableFlag.ts new file mode 100644 index 00000000000..03cdaee0e9d --- /dev/null +++ b/tests/cases/compiler/varianceRepeatedlyPropegatesWithUnreliableFlag.ts @@ -0,0 +1,17 @@ +type A = { a: number }; +type B = { b: number }; +type X = ({ [K in keyof T]: T[K] } & Record)[keyof T]; +type P1 = { data: X }; +type P2 = { data: X }; + +interface I { + fn(p1: P1>, p2: P2>): void; +} + +const i: I = null as any; +const p2: P2 = null as any; + +// Commenting out the below line will remove the error on the `const _i: I = i;` +i.fn(null as any, p2); + +const _i: I = i; \ No newline at end of file From 0a4f39af715c164cf7c905999fd8051f839e5d6c Mon Sep 17 00:00:00 2001 From: typescript-bot Date: Tue, 24 Sep 2019 14:10:40 +0000 Subject: [PATCH 93/97] Update user baselines --- .../reference/docker/office-ui-fabric.log | 21 +---- tests/baselines/reference/user/assert.log | 40 ++++---- .../user/chrome-devtools-frontend.log | 91 ++++++++----------- tests/baselines/reference/user/prettier.log | 75 ++++++++------- 4 files changed, 100 insertions(+), 127 deletions(-) diff --git a/tests/baselines/reference/docker/office-ui-fabric.log b/tests/baselines/reference/docker/office-ui-fabric.log index 9cda725e39e..e75c479de90 100644 --- a/tests/baselines/reference/docker/office-ui-fabric.log +++ b/tests/baselines/reference/docker/office-ui-fabric.log @@ -1,14 +1,5 @@ Exit Code: 1 Standard output: -@uifabric/codepen-loader: yarn run vX.X.X -@uifabric/codepen-loader: $ just-scripts build --production --lint -@uifabric/codepen-loader: [XX:XX:XX XM] ■ Removing [lib, temp, dist, coverage, lib-commonjs] -@uifabric/codepen-loader: [XX:XX:XX XM] ■ Running /office-ui-fabric-react/node_modules/typescript/lib/tsc.js with /office-ui-fabric-react/packages/codepen-loader/tsconfig.json -@uifabric/codepen-loader: [XX:XX:XX XM] ■ Executing: /usr/local/bin/node "/office-ui-fabric-react/node_modules/typescript/lib/tsc.js" --module commonjs --outDir "./lib" --project "/office-ui-fabric-react/packages/codepen-loader/tsconfig.json" -@uifabric/codepen-loader: [XX:XX:XX XM] ■ Running Jest -@uifabric/codepen-loader: [XX:XX:XX XM] ■ /usr/local/bin/node "/office-ui-fabric-react/node_modules/jest/bin/jest.js" --config "/office-ui-fabric-react/packages/codepen-loader/jest.config.js" --passWithNoTests --colors --forceExit -@uifabric/codepen-loader: PASS src/__tests__/codepenTransform.test.ts -@uifabric/codepen-loader: Done in ?s. @uifabric/build: yarn run vX.X.X @uifabric/build: $ node ./just-scripts.js no-op --production --lint @uifabric/build: Done in ?s. @@ -36,9 +27,11 @@ Standard output: @uifabric/migration: Done in ?s. @uifabric/monaco-editor: yarn run vX.X.X @uifabric/monaco-editor: $ just-scripts build --production --lint -@uifabric/monaco-editor: [XX:XX:XX XM] ■ Removing [esm, lib] +@uifabric/monaco-editor: [XX:XX:XX XM] ■ Removing [esm, lib, lib-commonjs] @uifabric/monaco-editor: [XX:XX:XX XM] ■ Running /office-ui-fabric-react/node_modules/typescript/lib/tsc.js with /office-ui-fabric-react/packages/monaco-editor/tsconfig.json @uifabric/monaco-editor: [XX:XX:XX XM] ■ Executing: /usr/local/bin/node "/office-ui-fabric-react/node_modules/typescript/lib/tsc.js" --pretty --target es5 --inlineSources --sourceRoot "../src" --outDir lib --module esnext --project "/office-ui-fabric-react/packages/monaco-editor/tsconfig.json" +@uifabric/monaco-editor: [XX:XX:XX XM] ■ Running /office-ui-fabric-react/node_modules/typescript/lib/tsc.js with /office-ui-fabric-react/packages/monaco-editor/tsconfig.json +@uifabric/monaco-editor: [XX:XX:XX XM] ■ Executing: /usr/local/bin/node "/office-ui-fabric-react/node_modules/typescript/lib/tsc.js" --pretty --target es5 --inlineSources --sourceRoot "../src" --outDir lib-commonjs --module commonjs --project "/office-ui-fabric-react/packages/monaco-editor/tsconfig.json" @uifabric/monaco-editor: Done in ?s. @uifabric/set-version: yarn run vX.X.X @uifabric/set-version: $ just-scripts build --production --lint @@ -91,6 +84,7 @@ Standard output: @uifabric/merge-styles: PASS src/extractStyleParts.test.ts @uifabric/merge-styles: PASS src/server.test.ts @uifabric/merge-styles: PASS src/concatStyleSetsWithProps.test.ts +@uifabric/merge-styles: PASS src/fontFace.test.ts @uifabric/merge-styles: [XX:XX:XX XM] ■ Extracting Public API surface from '/office-ui-fabric-react/packages/merge-styles/lib/index.d.ts' @uifabric/merge-styles: Done in ?s. @uifabric/jest-serializer-merge-styles: yarn run vX.X.X @@ -206,7 +200,6 @@ Standard output: @uifabric/styling: PASS src/styles/theme.test.ts @uifabric/styling: PASS src/styles/scheme.test.ts @uifabric/styling: PASS src/styles/getGlobalClassNames.test.ts -@uifabric/styling: PASS src/utilities/icons.test.ts @uifabric/styling: [XX:XX:XX XM] ■ Extracting Public API surface from '/office-ui-fabric-react/packages/styling/lib/index.d.ts' @uifabric/styling: Done in ?s. @uifabric/file-type-icons: yarn run vX.X.X @@ -249,11 +242,7 @@ Standard output: Standard error: info cli using local version of lerna lerna notice cli vX.X.X -lerna info Executing command in 43 packages: "yarn run build --production --lint" -@uifabric/codepen-loader: ts-jest[versions] (WARN) Version X.X.X-insiders.xxxxxxxx of typescript installed has not been tested with ts-jest. If you're experiencing issues, consider using a supported version (>=2.7.0 <4.0.0). Please do not report issues in ts-jest if you are using unsupported versions. -@uifabric/codepen-loader: Force exiting Jest -@uifabric/codepen-loader: -@uifabric/codepen-loader: Have you considered using `--detectOpenHandles` to detect async operations that kept running after all tests finished? +lerna info Executing command in 42 packages: "yarn run build --production --lint" @uifabric/example-data: [XX:XX:XX XM] ▲ One of these [node-sass, postcss, autoprefixer] is not installed, so this task has no effect @uifabric/set-version: [XX:XX:XX XM] ▲ One of these [node-sass, postcss, autoprefixer] is not installed, so this task has no effect @uifabric/merge-styles: [XX:XX:XX XM] ▲ One of these [node-sass, postcss, autoprefixer] is not installed, so this task has no effect diff --git a/tests/baselines/reference/user/assert.log b/tests/baselines/reference/user/assert.log index 4b00cccc6ab..ce8e6cc19d0 100644 --- a/tests/baselines/reference/user/assert.log +++ b/tests/baselines/reference/user/assert.log @@ -1,34 +1,34 @@ Exit Code: 1 Standard output: node_modules/assert/test.js(25,5): error TS2367: This condition will always return 'false' since the types 'string | undefined' and 'boolean' have no overlap. -node_modules/assert/test.js(39,5): error TS2552: Cannot find name 'test'. Did you mean 'tests'? -node_modules/assert/test.js(55,5): error TS2552: Cannot find name 'test'. Did you mean 'tests'? -node_modules/assert/test.js(74,5): error TS2552: Cannot find name 'test'. Did you mean 'tests'? -node_modules/assert/test.js(84,5): error TS2552: Cannot find name 'test'. Did you mean 'tests'? -node_modules/assert/test.js(94,5): error TS2552: Cannot find name 'test'. Did you mean 'tests'? -node_modules/assert/test.js(103,5): error TS2552: Cannot find name 'test'. Did you mean 'tests'? -node_modules/assert/test.js(120,5): error TS2552: Cannot find name 'test'. Did you mean 'tests'? -node_modules/assert/test.js(128,5): error TS2552: Cannot find name 'test'. Did you mean 'tests'? +node_modules/assert/test.js(39,5): error TS2593: Cannot find name 'test'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types field in your tsconfig. +node_modules/assert/test.js(55,5): error TS2593: Cannot find name 'test'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types field in your tsconfig. +node_modules/assert/test.js(74,5): error TS2593: Cannot find name 'test'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types field in your tsconfig. +node_modules/assert/test.js(84,5): error TS2593: Cannot find name 'test'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types field in your tsconfig. +node_modules/assert/test.js(94,5): error TS2593: Cannot find name 'test'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types field in your tsconfig. +node_modules/assert/test.js(103,5): error TS2593: Cannot find name 'test'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types field in your tsconfig. +node_modules/assert/test.js(120,5): error TS2593: Cannot find name 'test'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types field in your tsconfig. +node_modules/assert/test.js(128,5): error TS2593: Cannot find name 'test'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types field in your tsconfig. node_modules/assert/test.js(140,10): error TS2339: Property 'a' does not exist on type 'number[]'. node_modules/assert/test.js(141,10): error TS2339: Property 'b' does not exist on type 'number[]'. node_modules/assert/test.js(142,10): error TS2339: Property 'b' does not exist on type 'number[]'. node_modules/assert/test.js(143,10): error TS2339: Property 'a' does not exist on type 'number[]'. -node_modules/assert/test.js(149,5): error TS2552: Cannot find name 'test'. Did you mean 'tests'? +node_modules/assert/test.js(149,5): error TS2593: Cannot find name 'test'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types field in your tsconfig. node_modules/assert/test.js(157,51): error TS2349: This expression is not callable. Type 'never' has no call signatures. -node_modules/assert/test.js(161,5): error TS2552: Cannot find name 'test'. Did you mean 'tests'? +node_modules/assert/test.js(161,5): error TS2593: Cannot find name 'test'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types field in your tsconfig. node_modules/assert/test.js(168,5): error TS2593: Cannot find name 'test'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types field in your tsconfig. -node_modules/assert/test.js(182,5): error TS2593: Cannot find name 'test'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types field in your tsconfig. -node_modules/assert/test.js(229,5): error TS2593: Cannot find name 'test'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types field in your tsconfig. -node_modules/assert/test.js(235,5): error TS2593: Cannot find name 'test'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types field in your tsconfig. -node_modules/assert/test.js(250,5): error TS2593: Cannot find name 'test'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types field in your tsconfig. -node_modules/assert/test.js(254,5): error TS2593: Cannot find name 'test'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types field in your tsconfig. +node_modules/assert/test.js(182,5): error TS2552: Cannot find name 'test'. Did you mean 'tests'? +node_modules/assert/test.js(229,5): error TS2552: Cannot find name 'test'. Did you mean 'tests'? +node_modules/assert/test.js(235,5): error TS2552: Cannot find name 'test'. Did you mean 'tests'? +node_modules/assert/test.js(250,5): error TS2552: Cannot find name 'test'. Did you mean 'tests'? +node_modules/assert/test.js(254,5): error TS2552: Cannot find name 'test'. Did you mean 'tests'? node_modules/assert/test.js(256,55): error TS2345: Argument of type 'TypeError' is not assignable to parameter of type 'string'. -node_modules/assert/test.js(262,5): error TS2593: Cannot find name 'test'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types field in your tsconfig. -node_modules/assert/test.js(279,5): error TS2593: Cannot find name 'test'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types field in your tsconfig. -node_modules/assert/test.js(285,5): error TS2593: Cannot find name 'test'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types field in your tsconfig. -node_modules/assert/test.js(320,5): error TS2593: Cannot find name 'test'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types field in your tsconfig. -node_modules/assert/test.js(346,5): error TS2593: Cannot find name 'test'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types field in your tsconfig. +node_modules/assert/test.js(262,5): error TS2552: Cannot find name 'test'. Did you mean 'tests'? +node_modules/assert/test.js(279,5): error TS2552: Cannot find name 'test'. Did you mean 'tests'? +node_modules/assert/test.js(285,5): error TS2552: Cannot find name 'test'. Did you mean 'tests'? +node_modules/assert/test.js(320,5): error TS2552: Cannot find name 'test'. Did you mean 'tests'? +node_modules/assert/test.js(346,5): error TS2552: Cannot find name 'test'. Did you mean 'tests'? diff --git a/tests/baselines/reference/user/chrome-devtools-frontend.log b/tests/baselines/reference/user/chrome-devtools-frontend.log index bad901d3d57..c9ce3bc730b 100644 --- a/tests/baselines/reference/user/chrome-devtools-frontend.log +++ b/tests/baselines/reference/user/chrome-devtools-frontend.log @@ -3990,10 +3990,9 @@ node_modules/chrome-devtools-frontend/front_end/console/ConsoleView.js(1277,60): node_modules/chrome-devtools-frontend/front_end/console/ConsoleView.js(1278,42): error TS2769: No overload matches this call. Overload 1 of 2, '(...items: ConcatArray<{ key: string; text: string; regex: RegExp; negative: boolean; }>[]): { key: string; text: string; regex: RegExp; negative: boolean; }[]', gave the following error. Argument of type '{ key: any; text: string; negative: boolean; }[]' is not assignable to parameter of type 'ConcatArray<{ key: string; text: string; regex: RegExp; negative: boolean; }>'. - Types of property 'slice' are incompatible. - Type '(start?: number, end?: number) => { key: any; text: string; negative: boolean; }[]' is not assignable to type '(start?: number, end?: number) => { key: string; text: string; regex: RegExp; negative: boolean; }[]'. - Type '{ key: any; text: string; negative: boolean; }[]' is not assignable to type '{ key: string; text: string; regex: RegExp; negative: boolean; }[]'. - Property 'regex' is missing in type '{ key: any; text: string; negative: boolean; }' but required in type '{ key: string; text: string; regex: RegExp; negative: boolean; }'. + The types returned by 'slice(...)' are incompatible between these types. + Type '{ key: any; text: string; negative: boolean; }[]' is not assignable to type '{ key: string; text: string; regex: RegExp; negative: boolean; }[]'. + Property 'regex' is missing in type '{ key: any; text: string; negative: boolean; }' but required in type '{ key: string; text: string; regex: RegExp; negative: boolean; }'. Overload 2 of 2, '(...items: ({ key: string; text: string; regex: RegExp; negative: boolean; } | ConcatArray<{ key: string; text: string; regex: RegExp; negative: boolean; }>)[]): { key: string; text: string; regex: RegExp; negative: boolean; }[]', gave the following error. Argument of type '{ key: any; text: string; negative: boolean; }[]' is not assignable to parameter of type '{ key: string; text: string; regex: RegExp; negative: boolean; } | ConcatArray<{ key: string; text: string; regex: RegExp; negative: boolean; }>'. Type '{ key: any; text: string; negative: boolean; }[]' is not assignable to type 'ConcatArray<{ key: string; text: string; regex: RegExp; negative: boolean; }>'. @@ -5038,22 +5037,18 @@ node_modules/chrome-devtools-frontend/front_end/elements/ComputedStyleWidget.js( node_modules/chrome-devtools-frontend/front_end/elements/ComputedStyleWidget.js(91,24): error TS2769: No overload matches this call. The last overload gave the following error. Argument of type '(Promise | Promise)[]' is not assignable to parameter of type 'Iterable>'. - Types of property '[Symbol.iterator]' are incompatible. - Type '() => IterableIterator | Promise>' is not assignable to type '() => Iterator, any, undefined>'. - Type 'IterableIterator | Promise>' is not assignable to type 'Iterator, any, undefined>'. - Types of property 'next' are incompatible. - Type '(...args: [] | [undefined]) => IteratorResult | Promise, any>' is not assignable to type '(...args: [] | [undefined]) => IteratorResult, any>'. - Type 'IteratorResult | Promise, any>' is not assignable to type 'IteratorResult, any>'. - Type 'IteratorYieldResult | Promise>' is not assignable to type 'IteratorResult, any>'. - Type 'IteratorYieldResult | Promise>' is not assignable to type 'IteratorYieldResult>'. - Type 'Promise | Promise' is not assignable to type 'CSSMatchedStyles | PromiseLike'. - Type 'Promise' is not assignable to type 'CSSMatchedStyles | PromiseLike'. - Type 'Promise' is not assignable to type 'PromiseLike'. - Types of property 'then' are incompatible. - Type '(onfulfilled?: (value: ComputedStyle) => TResult1 | PromiseLike, onrejected?: (reason: any) => TResult2 | PromiseLike) => Promise' is not assignable to type '(onfulfilled?: (value: CSSMatchedStyles) => TResult1 | PromiseLike, onrejected?: (reason: any) => TResult2 | PromiseLike) => PromiseLike<...>'. - Types of parameters 'onfulfilled' and 'onfulfilled' are incompatible. - Types of parameters 'value' and 'value' are incompatible. - Type 'ComputedStyle' is missing the following properties from type 'CSSMatchedStyles': _cssModel, _node, _nodeStyles, _nodeForStyle, and 22 more. + The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types. + Type 'IteratorResult | Promise, any>' is not assignable to type 'IteratorResult, any>'. + Type 'IteratorYieldResult | Promise>' is not assignable to type 'IteratorResult, any>'. + Type 'IteratorYieldResult | Promise>' is not assignable to type 'IteratorYieldResult>'. + Type 'Promise | Promise' is not assignable to type 'CSSMatchedStyles | PromiseLike'. + Type 'Promise' is not assignable to type 'CSSMatchedStyles | PromiseLike'. + Type 'Promise' is not assignable to type 'PromiseLike'. + Types of property 'then' are incompatible. + Type '(onfulfilled?: (value: ComputedStyle) => TResult1 | PromiseLike, onrejected?: (reason: any) => TResult2 | PromiseLike) => Promise' is not assignable to type '(onfulfilled?: (value: CSSMatchedStyles) => TResult1 | PromiseLike, onrejected?: (reason: any) => TResult2 | PromiseLike) => PromiseLike<...>'. + Types of parameters 'onfulfilled' and 'onfulfilled' are incompatible. + Types of parameters 'value' and 'value' are incompatible. + Type 'ComputedStyle' is missing the following properties from type 'CSSMatchedStyles': _cssModel, _node, _nodeStyles, _nodeForStyle, and 22 more. node_modules/chrome-devtools-frontend/front_end/elements/ComputedStyleWidget.js(147,52): error TS2339: Property 'keysArray' does not exist on type 'Map'. node_modules/chrome-devtools-frontend/front_end/elements/ComputedStyleWidget.js(179,50): error TS2339: Property 'createChild' does not exist on type 'Element'. node_modules/chrome-devtools-frontend/front_end/elements/ComputedStyleWidget.js(200,74): error TS2339: Property 'consume' does not exist on type 'Event'. @@ -5339,22 +5334,18 @@ node_modules/chrome-devtools-frontend/front_end/elements/MetricsSidebarPane.js(5 node_modules/chrome-devtools-frontend/front_end/elements/MetricsSidebarPane.js(82,24): error TS2769: No overload matches this call. The last overload gave the following error. Argument of type '(Promise> | Promise)[]' is not assignable to parameter of type 'Iterable | PromiseLike>>'. - Types of property '[Symbol.iterator]' are incompatible. - Type '() => IterableIterator> | Promise>' is not assignable to type '() => Iterator | PromiseLike>, any, undefined>'. - Type 'IterableIterator> | Promise>' is not assignable to type 'Iterator | PromiseLike>, any, undefined>'. - Types of property 'next' are incompatible. - Type '(...args: [] | [undefined]) => IteratorResult> | Promise, any>' is not assignable to type '(...args: [] | [undefined]) => IteratorResult | PromiseLike>, any>'. - Type 'IteratorResult> | Promise, any>' is not assignable to type 'IteratorResult | PromiseLike>, any>'. - Type 'IteratorYieldResult> | Promise>' is not assignable to type 'IteratorResult | PromiseLike>, any>'. - Type 'IteratorYieldResult> | Promise>' is not assignable to type 'IteratorYieldResult | PromiseLike>>'. - Type 'Promise> | Promise' is not assignable to type 'Map | PromiseLike>'. - Type 'Promise' is not assignable to type 'Map | PromiseLike>'. - Type 'Promise' is not assignable to type 'PromiseLike>'. - Types of property 'then' are incompatible. - Type '(onfulfilled?: (value: InlineStyleResult) => TResult1 | PromiseLike, onrejected?: (reason: any) => TResult2 | PromiseLike) => Promise<...>' is not assignable to type ', TResult2 = never>(onfulfilled?: (value: Map) => TResult1 | PromiseLike, onrejected?: (reason: any) => TResult2 | PromiseLike) => PromiseLike<...>'. - Types of parameters 'onfulfilled' and 'onfulfilled' are incompatible. - Types of parameters 'value' and 'value' are incompatible. - Type 'InlineStyleResult' is missing the following properties from type 'Map': clear, delete, forEach, get, and 8 more. + The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types. + Type 'IteratorResult> | Promise, any>' is not assignable to type 'IteratorResult | PromiseLike>, any>'. + Type 'IteratorYieldResult> | Promise>' is not assignable to type 'IteratorResult | PromiseLike>, any>'. + Type 'IteratorYieldResult> | Promise>' is not assignable to type 'IteratorYieldResult | PromiseLike>>'. + Type 'Promise> | Promise' is not assignable to type 'Map | PromiseLike>'. + Type 'Promise' is not assignable to type 'Map | PromiseLike>'. + Type 'Promise' is not assignable to type 'PromiseLike>'. + Types of property 'then' are incompatible. + Type '(onfulfilled?: (value: InlineStyleResult) => TResult1 | PromiseLike, onrejected?: (reason: any) => TResult2 | PromiseLike) => Promise<...>' is not assignable to type ', TResult2 = never>(onfulfilled?: (value: Map) => TResult1 | PromiseLike, onrejected?: (reason: any) => TResult2 | PromiseLike) => PromiseLike<...>'. + Types of parameters 'onfulfilled' and 'onfulfilled' are incompatible. + Types of parameters 'value' and 'value' are incompatible. + Type 'InlineStyleResult' is missing the following properties from type 'Map': clear, delete, forEach, get, and 8 more. node_modules/chrome-devtools-frontend/front_end/elements/MetricsSidebarPane.js(120,11): error TS2339: Property 'consume' does not exist on type 'Event'. node_modules/chrome-devtools-frontend/front_end/elements/MetricsSidebarPane.js(164,22): error TS2339: Property 'toFixedIfFloating' does not exist on type 'NumberConstructor'. node_modules/chrome-devtools-frontend/front_end/elements/MetricsSidebarPane.js(179,18): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'. @@ -12648,22 +12639,18 @@ node_modules/chrome-devtools-frontend/front_end/ui/View.js(454,38): error TS2339 node_modules/chrome-devtools-frontend/front_end/ui/View.js(461,44): error TS2769: No overload matches this call. The last overload gave the following error. Argument of type '(Promise | Promise)[]' is not assignable to parameter of type 'Iterable>'. - Types of property '[Symbol.iterator]' are incompatible. - Type '() => IterableIterator | Promise>' is not assignable to type '() => Iterator, any, undefined>'. - Type 'IterableIterator | Promise>' is not assignable to type 'Iterator, any, undefined>'. - Types of property 'next' are incompatible. - Type '(...args: [] | [undefined]) => IteratorResult | Promise, any>' is not assignable to type '(...args: [] | [undefined]) => IteratorResult, any>'. - Type 'IteratorResult | Promise, any>' is not assignable to type 'IteratorResult, any>'. - Type 'IteratorYieldResult | Promise>' is not assignable to type 'IteratorResult, any>'. - Type 'IteratorYieldResult | Promise>' is not assignable to type 'IteratorYieldResult>'. - Type 'Promise | Promise' is not assignable to type 'void | PromiseLike'. - Type 'Promise' is not assignable to type 'void | PromiseLike'. - Type 'Promise' is not assignable to type 'PromiseLike'. - Types of property 'then' are incompatible. - Type '(onfulfilled?: (value: ToolbarItem[]) => TResult1 | PromiseLike, onrejected?: (reason: any) => TResult2 | PromiseLike) => Promise' is not assignable to type '(onfulfilled?: (value: void) => TResult1 | PromiseLike, onrejected?: (reason: any) => TResult2 | PromiseLike) => PromiseLike'. - Types of parameters 'onfulfilled' and 'onfulfilled' are incompatible. - Types of parameters 'value' and 'value' are incompatible. - Type 'ToolbarItem[]' is not assignable to type 'void'. + The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types. + Type 'IteratorResult | Promise, any>' is not assignable to type 'IteratorResult, any>'. + Type 'IteratorYieldResult | Promise>' is not assignable to type 'IteratorResult, any>'. + Type 'IteratorYieldResult | Promise>' is not assignable to type 'IteratorYieldResult>'. + Type 'Promise | Promise' is not assignable to type 'void | PromiseLike'. + Type 'Promise' is not assignable to type 'void | PromiseLike'. + Type 'Promise' is not assignable to type 'PromiseLike'. + Types of property 'then' are incompatible. + Type '(onfulfilled?: (value: ToolbarItem[]) => TResult1 | PromiseLike, onrejected?: (reason: any) => TResult2 | PromiseLike) => Promise' is not assignable to type '(onfulfilled?: (value: void) => TResult1 | PromiseLike, onrejected?: (reason: any) => TResult2 | PromiseLike) => PromiseLike'. + Types of parameters 'onfulfilled' and 'onfulfilled' are incompatible. + Types of parameters 'value' and 'value' are incompatible. + Type 'ToolbarItem[]' is not assignable to type 'void'. node_modules/chrome-devtools-frontend/front_end/ui/View.js(495,24): error TS2339: Property 'createTextChild' does not exist on type 'Element'. node_modules/chrome-devtools-frontend/front_end/ui/View.js(496,24): error TS2339: Property 'tabIndex' does not exist on type 'Element'. node_modules/chrome-devtools-frontend/front_end/ui/View.js(501,25): error TS2339: Property 'createChild' does not exist on type 'Element'. diff --git a/tests/baselines/reference/user/prettier.log b/tests/baselines/reference/user/prettier.log index 635c0bc0b36..71fb532b794 100644 --- a/tests/baselines/reference/user/prettier.log +++ b/tests/baselines/reference/user/prettier.log @@ -142,10 +142,9 @@ src/language-html/printer-html.js(314,13): error TS2769: No overload matches thi src/language-html/printer-html.js(471,11): error TS2769: No overload matches this call. Overload 1 of 2, '(...items: ConcatArray[]): never[]', gave the following error. Argument of type '{ type: string; parts: any; }[]' is not assignable to parameter of type 'ConcatArray'. - Types of property 'slice' are incompatible. - Type '(start?: number | undefined, end?: number | undefined) => { type: string; parts: any; }[]' is not assignable to type '(start?: number | undefined, end?: number | undefined) => never[]'. - Type '{ type: string; parts: any; }[]' is not assignable to type 'never[]'. - Type '{ type: string; parts: any; }' is not assignable to type 'never'. + The types returned by 'slice(...)' are incompatible between these types. + Type '{ type: string; parts: any; }[]' is not assignable to type 'never[]'. + Type '{ type: string; parts: any; }' is not assignable to type 'never'. Overload 2 of 2, '(...items: ConcatArray[]): never[]', gave the following error. Argument of type '{ type: string; parts: any; }[]' is not assignable to parameter of type 'ConcatArray'. src/language-html/printer-html.js(492,11): error TS2769: No overload matches this call. @@ -200,11 +199,10 @@ src/language-js/index.js(81,60): error TS2345: Argument of type '{ override: { s src/language-js/needs-parens.js(871,14): error TS2769: No overload matches this call. Overload 1 of 2, '(...items: ConcatArray<(childPath: any) => any>[]): ((childPath: any) => any)[]', gave the following error. Argument of type '(string | number)[]' is not assignable to parameter of type 'ConcatArray<(childPath: any) => any>'. - Types of property 'slice' are incompatible. - Type '(start?: number | undefined, end?: number | undefined) => (string | number)[]' is not assignable to type '(start?: number | undefined, end?: number | undefined) => ((childPath: any) => any)[]'. - Type '(string | number)[]' is not assignable to type '((childPath: any) => any)[]'. - Type 'string | number' is not assignable to type '(childPath: any) => any'. - Type 'string' is not assignable to type '(childPath: any) => any'. + The types returned by 'slice(...)' are incompatible between these types. + Type '(string | number)[]' is not assignable to type '((childPath: any) => any)[]'. + Type 'string | number' is not assignable to type '(childPath: any) => any'. + Type 'string' is not assignable to type '(childPath: any) => any'. Overload 2 of 2, '(...items: (((childPath: any) => any) | ConcatArray<(childPath: any) => any>)[]): ((childPath: any) => any)[]', gave the following error. Argument of type '(string | number)[]' is not assignable to parameter of type '((childPath: any) => any) | ConcatArray<(childPath: any) => any>'. Type '(string | number)[]' is not assignable to type 'ConcatArray<(childPath: any) => any>'. @@ -219,56 +217,55 @@ src/language-js/printer-estree.js(400,9): error TS2769: No overload matches this Overload 2 of 2, '(...items: ConcatArray[]): never[]', gave the following error. Argument of type '{ type: string; parts: any; } | { type: string; contents: any; n: any; }' is not assignable to parameter of type 'ConcatArray'. Type '{ type: string; parts: any; }' is not assignable to type 'ConcatArray'. -src/language-js/printer-estree.js(1480,28): error TS2769: No overload matches this call. +src/language-js/printer-estree.js(1481,28): error TS2769: No overload matches this call. Overload 1 of 2, '(...items: ConcatArray[]): (string | { type: string; id: any; contents: any; break: boolean; expandedStates: any; })[]', gave the following error. Argument of type '{ type: string; parts: any; }' is not assignable to parameter of type 'ConcatArray'. Type '{ type: string; parts: any; }' is missing the following properties from type 'ConcatArray': length, join, slice Overload 2 of 2, '(...items: (string | { type: string; id: any; contents: any; break: boolean; expandedStates: any; } | ConcatArray)[]): (string | { ...; })[]', gave the following error. Argument of type '{ type: string; parts: any; }' is not assignable to parameter of type 'string | { type: string; id: any; contents: any; break: boolean; expandedStates: any; } | ConcatArray'. Type '{ type: string; parts: any; }' is missing the following properties from type '{ type: string; id: any; contents: any; break: boolean; expandedStates: any; }': id, contents, break, expandedStates -src/language-js/printer-estree.js(1913,20): error TS2345: Argument of type '" "' is not assignable to parameter of type '{ type: string; id: any; contents: any; break: boolean; expandedStates: any; }'. -src/language-js/printer-estree.js(1915,20): error TS2345: Argument of type '{ type: string; parts: any; }' is not assignable to parameter of type '{ type: string; id: any; contents: any; break: boolean; expandedStates: any; }'. -src/language-js/printer-estree.js(1917,18): error TS2345: Argument of type '"while ("' is not assignable to parameter of type '{ type: string; id: any; contents: any; break: boolean; expandedStates: any; }'. -src/language-js/printer-estree.js(1926,9): error TS2345: Argument of type '")"' is not assignable to parameter of type '{ type: string; id: any; contents: any; break: boolean; expandedStates: any; }'. -src/language-js/printer-estree.js(3472,11): error TS2769: No overload matches this call. +src/language-js/printer-estree.js(1914,20): error TS2345: Argument of type '" "' is not assignable to parameter of type '{ type: string; id: any; contents: any; break: boolean; expandedStates: any; }'. +src/language-js/printer-estree.js(1916,20): error TS2345: Argument of type '{ type: string; parts: any; }' is not assignable to parameter of type '{ type: string; id: any; contents: any; break: boolean; expandedStates: any; }'. +src/language-js/printer-estree.js(1918,18): error TS2345: Argument of type '"while ("' is not assignable to parameter of type '{ type: string; id: any; contents: any; break: boolean; expandedStates: any; }'. +src/language-js/printer-estree.js(1927,9): error TS2345: Argument of type '")"' is not assignable to parameter of type '{ type: string; id: any; contents: any; break: boolean; expandedStates: any; }'. +src/language-js/printer-estree.js(3473,11): error TS2769: No overload matches this call. Overload 1 of 2, '(...items: ConcatArray[]): never[]', gave the following error. Argument of type 'never[] | { type: string; parts: any; }' is not assignable to parameter of type 'ConcatArray'. Type '{ type: string; parts: any; }' is not assignable to type 'ConcatArray'. Overload 2 of 2, '(...items: ConcatArray[]): never[]', gave the following error. Argument of type 'never[] | { type: string; parts: any; }' is not assignable to parameter of type 'ConcatArray'. Type '{ type: string; parts: any; }' is not assignable to type 'ConcatArray'. -src/language-js/printer-estree.js(3901,22): error TS2345: Argument of type 'any' is not assignable to parameter of type 'never'. -src/language-js/printer-estree.js(3968,14): error TS2339: Property 'comments' does not exist on type 'Expression'. +src/language-js/printer-estree.js(3902,22): error TS2345: Argument of type 'any' is not assignable to parameter of type 'never'. +src/language-js/printer-estree.js(3969,14): error TS2339: Property 'comments' does not exist on type 'Expression'. Property 'comments' does not exist on type 'Identifier'. -src/language-js/printer-estree.js(3980,9): error TS2367: This condition will always return 'false' since the types '"FunctionExpression" | "ClassExpression" | "ObjectExpression" | "TaggedTemplateExpression" | "CallExpression" | "ConditionalExpression" | "UpdateExpression" | "SequenceExpression" | ... 11 more ... | "MetaProperty"' and '"OptionalMemberExpression"' have no overlap. -src/language-js/printer-estree.js(3981,13): error TS2339: Property 'property' does not exist on type 'SimpleLiteral | RegExpLiteral | FunctionExpression | ArrowFunctionExpression | ArrayExpression | ObjectExpression | YieldExpression | UnaryExpression | UpdateExpression | ... 12 more ... | AwaitExpression'. +src/language-js/printer-estree.js(3981,9): error TS2367: This condition will always return 'false' since the types '"FunctionExpression" | "ClassExpression" | "ObjectExpression" | "TaggedTemplateExpression" | "CallExpression" | "ConditionalExpression" | "UpdateExpression" | "SequenceExpression" | ... 11 more ... | "MetaProperty"' and '"OptionalMemberExpression"' have no overlap. +src/language-js/printer-estree.js(3982,13): error TS2339: Property 'property' does not exist on type 'SimpleLiteral | RegExpLiteral | FunctionExpression | ArrowFunctionExpression | ArrayExpression | ObjectExpression | YieldExpression | UnaryExpression | UpdateExpression | ... 12 more ... | AwaitExpression'. Property 'property' does not exist on type 'SimpleLiteral'. -src/language-js/printer-estree.js(3981,52): error TS2339: Property 'property' does not exist on type 'SimpleLiteral | RegExpLiteral | FunctionExpression | ArrowFunctionExpression | ArrayExpression | ObjectExpression | YieldExpression | UnaryExpression | UpdateExpression | ... 12 more ... | AwaitExpression'. +src/language-js/printer-estree.js(3982,52): error TS2339: Property 'property' does not exist on type 'SimpleLiteral | RegExpLiteral | FunctionExpression | ArrowFunctionExpression | ArrayExpression | ObjectExpression | YieldExpression | UnaryExpression | UpdateExpression | ... 12 more ... | AwaitExpression'. Property 'property' does not exist on type 'SimpleLiteral'. -src/language-js/printer-estree.js(3986,9): error TS2367: This condition will always return 'false' since the types '"FunctionExpression" | "ClassExpression" | "ObjectExpression" | "TaggedTemplateExpression" | "CallExpression" | "ConditionalExpression" | "UpdateExpression" | "SequenceExpression" | ... 11 more ... | "MetaProperty"' and '"OptionalMemberExpression"' have no overlap. -src/language-js/printer-estree.js(3988,29): error TS2339: Property 'object' does not exist on type 'SimpleLiteral | RegExpLiteral | FunctionExpression | ArrowFunctionExpression | ArrayExpression | ObjectExpression | YieldExpression | UnaryExpression | UpdateExpression | ... 12 more ... | AwaitExpression'. +src/language-js/printer-estree.js(3987,9): error TS2367: This condition will always return 'false' since the types '"FunctionExpression" | "ClassExpression" | "ObjectExpression" | "TaggedTemplateExpression" | "CallExpression" | "ConditionalExpression" | "UpdateExpression" | "SequenceExpression" | ... 11 more ... | "MetaProperty"' and '"OptionalMemberExpression"' have no overlap. +src/language-js/printer-estree.js(3989,29): error TS2339: Property 'object' does not exist on type 'SimpleLiteral | RegExpLiteral | FunctionExpression | ArrowFunctionExpression | ArrayExpression | ObjectExpression | YieldExpression | UnaryExpression | UpdateExpression | ... 12 more ... | AwaitExpression'. Property 'object' does not exist on type 'SimpleLiteral'. -src/language-js/printer-estree.js(3989,22): error TS2339: Property 'comments' does not exist on type 'SimpleLiteral | RegExpLiteral | FunctionExpression | ArrowFunctionExpression | ArrayExpression | ObjectExpression | YieldExpression | UnaryExpression | UpdateExpression | ... 12 more ... | AwaitExpression'. +src/language-js/printer-estree.js(3990,22): error TS2339: Property 'comments' does not exist on type 'SimpleLiteral | RegExpLiteral | FunctionExpression | ArrowFunctionExpression | ArrayExpression | ObjectExpression | YieldExpression | UnaryExpression | UpdateExpression | ... 12 more ... | AwaitExpression'. Property 'comments' does not exist on type 'SimpleLiteral'. -src/language-js/printer-estree.js(3995,9): error TS2367: This condition will always return 'false' since the types '"FunctionExpression" | "ClassExpression" | "ObjectExpression" | "TaggedTemplateExpression" | "CallExpression" | "ConditionalExpression" | "UpdateExpression" | "SequenceExpression" | ... 11 more ... | "MetaProperty"' and '"Identifier"' have no overlap. -src/language-js/printer-estree.js(3996,9): error TS2367: This condition will always return 'false' since the types '"FunctionExpression" | "ClassExpression" | "ObjectExpression" | "TaggedTemplateExpression" | "CallExpression" | "ConditionalExpression" | "UpdateExpression" | "SequenceExpression" | ... 11 more ... | "MetaProperty"' and '"ThisExpression"' have no overlap. -src/language-js/printer-estree.js(4200,23): error TS2532: Object is possibly 'undefined'. -src/language-js/printer-estree.js(4201,24): error TS2532: Object is possibly 'undefined'. -src/language-js/printer-estree.js(4557,5): error TS2345: Argument of type '"" | { type: string; parts: any; } | { type: string; contents: any; }' is not assignable to parameter of type 'string'. +src/language-js/printer-estree.js(3996,9): error TS2367: This condition will always return 'false' since the types '"FunctionExpression" | "ClassExpression" | "ObjectExpression" | "TaggedTemplateExpression" | "CallExpression" | "ConditionalExpression" | "UpdateExpression" | "SequenceExpression" | ... 11 more ... | "MetaProperty"' and '"Identifier"' have no overlap. +src/language-js/printer-estree.js(3997,9): error TS2367: This condition will always return 'false' since the types '"FunctionExpression" | "ClassExpression" | "ObjectExpression" | "TaggedTemplateExpression" | "CallExpression" | "ConditionalExpression" | "UpdateExpression" | "SequenceExpression" | ... 11 more ... | "MetaProperty"' and '"ThisExpression"' have no overlap. +src/language-js/printer-estree.js(4201,23): error TS2532: Object is possibly 'undefined'. +src/language-js/printer-estree.js(4202,24): error TS2532: Object is possibly 'undefined'. +src/language-js/printer-estree.js(4558,5): error TS2345: Argument of type '"" | { type: string; parts: any; } | { type: string; contents: any; }' is not assignable to parameter of type 'string'. Type '{ type: string; parts: any; }' is not assignable to type 'string'. -src/language-js/printer-estree.js(4561,16): error TS2345: Argument of type '{ type: string; parts: any; }' is not assignable to parameter of type 'string'. -src/language-js/printer-estree.js(4609,11): error TS2322: Type '{ type: string; id: any; contents: any; break: boolean; expandedStates: any; }' is not assignable to type 'string'. -src/language-js/printer-estree.js(4624,11): error TS2322: Type '{ type: string; parts: any; }' is not assignable to type 'string'. -src/language-js/printer-estree.js(4636,9): error TS2345: Argument of type '{ type: string; parts: any; }' is not assignable to parameter of type 'string'. -src/language-js/printer-estree.js(4923,9): error TS2554: Expected 0-2 arguments, but got 3. -src/language-js/printer-estree.js(6130,7): error TS2769: No overload matches this call. +src/language-js/printer-estree.js(4562,16): error TS2345: Argument of type '{ type: string; parts: any; }' is not assignable to parameter of type 'string'. +src/language-js/printer-estree.js(4610,11): error TS2322: Type '{ type: string; id: any; contents: any; break: boolean; expandedStates: any; }' is not assignable to type 'string'. +src/language-js/printer-estree.js(4625,11): error TS2322: Type '{ type: string; parts: any; }' is not assignable to type 'string'. +src/language-js/printer-estree.js(4637,9): error TS2345: Argument of type '{ type: string; parts: any; }' is not assignable to parameter of type 'string'. +src/language-js/printer-estree.js(4924,9): error TS2554: Expected 0-2 arguments, but got 3. +src/language-js/printer-estree.js(6131,7): error TS2769: No overload matches this call. Overload 1 of 2, '(...items: ConcatArray<(childPath: any) => any>[]): ((childPath: any) => any)[]', gave the following error. Argument of type '(string | number)[]' is not assignable to parameter of type 'ConcatArray<(childPath: any) => any>'. - Types of property 'slice' are incompatible. - Type '(start?: number | undefined, end?: number | undefined) => (string | number)[]' is not assignable to type '(start?: number | undefined, end?: number | undefined) => ((childPath: any) => any)[]'. - Type '(string | number)[]' is not assignable to type '((childPath: any) => any)[]'. - Type 'string | number' is not assignable to type '(childPath: any) => any'. - Type 'string' is not assignable to type '(childPath: any) => any'. + The types returned by 'slice(...)' are incompatible between these types. + Type '(string | number)[]' is not assignable to type '((childPath: any) => any)[]'. + Type 'string | number' is not assignable to type '(childPath: any) => any'. + Type 'string' is not assignable to type '(childPath: any) => any'. Overload 2 of 2, '(...items: (((childPath: any) => any) | ConcatArray<(childPath: any) => any>)[]): ((childPath: any) => any)[]', gave the following error. Argument of type '(string | number)[]' is not assignable to parameter of type '((childPath: any) => any) | ConcatArray<(childPath: any) => any>'. Type '(string | number)[]' is not assignable to type 'ConcatArray<(childPath: any) => any>'. From aca0bb943f24c57854062b1a0bc13b1d64d73136 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Tue, 24 Sep 2019 08:30:18 -0700 Subject: [PATCH 94/97] Fix lint on master --- src/compiler/checker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 7a44ac66229..71c72a4cf82 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -33314,7 +33314,7 @@ namespace ts { // Modifiers are never allowed on properties except for 'async' on a method declaration if (prop.modifiers) { - for (const mod of prop.modifiers!) { // TODO: GH#19955 + for (const mod of prop.modifiers) { if (mod.kind !== SyntaxKind.AsyncKeyword || prop.kind !== SyntaxKind.MethodDeclaration) { grammarErrorOnNode(mod, Diagnostics._0_modifier_cannot_be_used_here, getTextOfNode(mod)); } From 4ddf1919bbdf3df09fe32cfdbe3fef7423fc5e51 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Tue, 24 Sep 2019 08:47:45 -0700 Subject: [PATCH 95/97] try eslint-disable-next-line instead --- src/compiler/checker.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 71c72a4cf82..e3494a1f341 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -33314,7 +33314,8 @@ namespace ts { // Modifiers are never allowed on properties except for 'async' on a method declaration if (prop.modifiers) { - for (const mod of prop.modifiers) { + // eslint-disable-next-line no-unnecessary-type-assertion + for (const mod of prop.modifiers!) { // TODO: GH#19955 if (mod.kind !== SyntaxKind.AsyncKeyword || prop.kind !== SyntaxKind.MethodDeclaration) { grammarErrorOnNode(mod, Diagnostics._0_modifier_cannot_be_used_here, getTextOfNode(mod)); } From 41c3e545457005c6c8fe3c78f1da56b4379bcf03 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Tue, 24 Sep 2019 09:38:44 -0700 Subject: [PATCH 96/97] change eslint-disable rule name --- src/compiler/checker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index e3494a1f341..2a3f2390cf3 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -33314,7 +33314,7 @@ namespace ts { // Modifiers are never allowed on properties except for 'async' on a method declaration if (prop.modifiers) { - // eslint-disable-next-line no-unnecessary-type-assertion + // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion for (const mod of prop.modifiers!) { // TODO: GH#19955 if (mod.kind !== SyntaxKind.AsyncKeyword || prop.kind !== SyntaxKind.MethodDeclaration) { grammarErrorOnNode(mod, Diagnostics._0_modifier_cannot_be_used_here, getTextOfNode(mod)); From fd3ba679ec174180acac5c89e572a8ccba97870b Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Tue, 24 Sep 2019 12:22:42 -0700 Subject: [PATCH 97/97] Reword the option description per feedback --- src/compiler/commandLineParser.ts | 2 +- src/compiler/diagnosticMessages.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index ca113fb2b33..fd0d428d54b 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -776,7 +776,7 @@ namespace ts { name: "disableSourceOfProjectReferenceRedirect", type: "boolean", category: Diagnostics.Advanced_Options, - description: Diagnostics.Disable_using_source_of_project_reference_redirect_files + description: Diagnostics.Disable_use_of_source_files_instead_of_declaration_files_from_referenced_projects }, { name: "noImplicitUseStrict", diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 3b4b25986c2..fa62a0bb514 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -4003,7 +4003,7 @@ "category": "Message", "code": 6220 }, - "Disable using source of project reference redirect files.": { + "Disable use of source files instead of declaration files from referenced projects.": { "category": "Message", "code": 6221 },