From 0680d5ba92914205055a12bdc6b989c2c7ed210c Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Tue, 24 Jul 2018 15:07:22 -0700 Subject: [PATCH 1/2] Check if the file returned by the program actually refers to the same file as we intend --- src/compiler/sourcemapDecoder.ts | 6 +- src/harness/fourslash.ts | 2 +- ...oToDefinitionSameNameDifferentDirectory.ts | 58 +++++++++++++++++++ 3 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 tests/cases/fourslash/server/declarationMapsGoToDefinitionSameNameDifferentDirectory.ts diff --git a/src/compiler/sourcemapDecoder.ts b/src/compiler/sourcemapDecoder.ts index 57f46bdcfe817..cd9ee764ceb9a 100644 --- a/src/compiler/sourcemapDecoder.ts +++ b/src/compiler/sourcemapDecoder.ts @@ -99,10 +99,10 @@ namespace ts.sourcemaps { function getSourceFileLike(fileName: string, location: string): SourceFileLike | undefined { // Lookup file in program, if provided const file = program && program.getSourceFile(fileName); - if (!file) { + const path = toPath(fileName, location, host.getCanonicalFileName); + if (!file || file.path !== path) { // Otherwise check the cache (which may hit disk) - const path = toPath(fileName, location, host.getCanonicalFileName); - return fallbackCache.get(path); + return program && program.getSourceFile(path) || fallbackCache.get(path); } return file; } diff --git a/src/harness/fourslash.ts b/src/harness/fourslash.ts index 4d9e653f27d61..6d50a66ad49df 100644 --- a/src/harness/fourslash.ts +++ b/src/harness/fourslash.ts @@ -709,7 +709,7 @@ namespace FourSlash { ts.zipWith(endMarkers, definitions, (endMarker, definition, i) => { const marker = this.getMarkerByName(endMarker); - if (marker.fileName !== definition.fileName || marker.position !== definition.textSpan.start) { + if (ts.comparePaths(marker.fileName, definition.fileName, /*ignoreCase*/ true) !== ts.Comparison.EqualTo || marker.position !== definition.textSpan.start) { this.raiseError(`${testName} failed for definition ${endMarker} (${i}): expected ${marker.fileName} at ${marker.position}, got ${definition.fileName} at ${definition.textSpan.start}`); } }); diff --git a/tests/cases/fourslash/server/declarationMapsGoToDefinitionSameNameDifferentDirectory.ts b/tests/cases/fourslash/server/declarationMapsGoToDefinitionSameNameDifferentDirectory.ts new file mode 100644 index 0000000000000..68495d7006c94 --- /dev/null +++ b/tests/cases/fourslash/server/declarationMapsGoToDefinitionSameNameDifferentDirectory.ts @@ -0,0 +1,58 @@ +/// +// @Filename: BaseClass/Source.d.ts +////declare class Control { +//// constructor(); +//// /** this is a super var */ +//// myVar: boolean | 'yeah'; +////} +//////# sourceMappingURL=Source.d.ts.map +// @Filename: BaseClass/Source.d.ts.map +////{"version":3,"file":"Source.d.ts","sourceRoot":"","sources":["Source.ts"],"names":[],"mappings":"AAAA,cAAM,OAAO;;IAIT,0BAA0B;IACnB,KAAK,EAAE,OAAO,GAAG,MAAM,CAAQ;CACzC"} +// @Filename: BaseClass/Source.ts +////class /*2*/Control{ +//// constructor(){ +//// return; +//// } +//// /** this is a super var */ +//// public /*4*/myVar: boolean | 'yeah' = true; +////} +// @Filename: tsbase.json +////{ +//// "$schema": "http://json.schemastore.org/tsconfig", +//// "compileOnSave": true, +//// "compilerOptions": { +//// "sourceMap": true, +//// "declaration": true, +//// "declarationMap": true +//// } +//// } +// @Filename: buttonClass/tsconfig.json +////{ +//// "extends": "../tsbase.json", +//// "compilerOptions": { +//// "outFile": "Source.js" +//// }, +//// "files": [ +//// "Source.ts" +//// ], +//// "include": [ +//// "../BaseClass/Source.d.ts" +//// ] +//// } +// @Filename: buttonClass/Source.ts +////// I cannot F12 navigate to Control +////// vvvvvvv +////class Button extends [|/*1*/Control|] { +//// public myFunction() { +//// // I cannot F12 navigate to myVar +//// // vvvvv +//// if (typeof this.[|/*3*/myVar|] === 'boolean') { +//// this.myVar; +//// } else { +//// this.myVar.toLocaleUpperCase(); +//// } +//// } +////} + +verify.goToDefinition("1", "2"); +verify.goToDefinition("3", "4"); From 03d9322bf73ec93447f925b87398bc70f22e8422 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Tue, 24 Jul 2018 15:16:04 -0700 Subject: [PATCH 2/2] Simplify --- src/compiler/sourcemapDecoder.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/compiler/sourcemapDecoder.ts b/src/compiler/sourcemapDecoder.ts index cd9ee764ceb9a..76f5943be66d4 100644 --- a/src/compiler/sourcemapDecoder.ts +++ b/src/compiler/sourcemapDecoder.ts @@ -98,11 +98,11 @@ namespace ts.sourcemaps { function getSourceFileLike(fileName: string, location: string): SourceFileLike | undefined { // Lookup file in program, if provided - const file = program && program.getSourceFile(fileName); const path = toPath(fileName, location, host.getCanonicalFileName); - if (!file || file.path !== path) { + const file = program && program.getSourceFile(path); + if (!file) { // Otherwise check the cache (which may hit disk) - return program && program.getSourceFile(path) || fallbackCache.get(path); + return fallbackCache.get(path); } return file; }