From 81e85d10eaee3593c9d7631f9f4218564b79c93b Mon Sep 17 00:00:00 2001 From: Rich Chiodo Date: Thu, 18 Jan 2024 10:54:40 -0800 Subject: [PATCH] Fix combining paths with empty uri (#7037) * Fix combining paths with empty uri * Force empty to be case sensitive * Remove unneeded code --- .../src/common/uri/emptyUri.ts | 18 +++++------------- .../pyright-internal/src/common/uri/fileUri.ts | 2 +- .../pyright-internal/src/tests/uri.test.ts | 6 +++++- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/packages/pyright-internal/src/common/uri/emptyUri.ts b/packages/pyright-internal/src/common/uri/emptyUri.ts index 7ac632e98d0e..f0fcb77cccee 100644 --- a/packages/pyright-internal/src/common/uri/emptyUri.ts +++ b/packages/pyright-internal/src/common/uri/emptyUri.ts @@ -6,15 +6,15 @@ * URI class that represents an empty URI. */ -import { BaseUri } from './baseUri'; +import { FileUri } from './fileUri'; import { Uri } from './uri'; const EmptyKey = ''; -export class EmptyUri extends BaseUri { +export class EmptyUri extends FileUri { private static _instance = new EmptyUri(); private constructor() { - super(EmptyKey); + super(EmptyKey, '', '', '', undefined, /* isCaseSensitive */ true); } static get instance() { @@ -37,11 +37,7 @@ export class EmptyUri extends BaseUri { return this; } - get isCaseSensitive(): boolean { - return true; - } - - get fragment(): string { + override get fragment(): string { return ''; } @@ -104,10 +100,6 @@ export class EmptyUri extends BaseUri { return 0; } - override combinePaths(...paths: string[]): Uri { - return this; - } - override getShortenedFileName(maxDirLength: number): string { return ''; } @@ -116,7 +108,7 @@ export class EmptyUri extends BaseUri { return this; } - withFragment(fragment: string): Uri { + override withFragment(fragment: string): Uri { return this; } diff --git a/packages/pyright-internal/src/common/uri/fileUri.ts b/packages/pyright-internal/src/common/uri/fileUri.ts index dded60e5c292..7f7cc238b504 100644 --- a/packages/pyright-internal/src/common/uri/fileUri.ts +++ b/packages/pyright-internal/src/common/uri/fileUri.ts @@ -25,7 +25,7 @@ import { Uri } from './uri'; export class FileUri extends BaseUri { private _formattedString: string | undefined; - private constructor( + protected constructor( key: string, private readonly _filePath: string, private readonly _query: string, diff --git a/packages/pyright-internal/src/tests/uri.test.ts b/packages/pyright-internal/src/tests/uri.test.ts index 1e7d2c1b9d00..0e246541f48f 100644 --- a/packages/pyright-internal/src/tests/uri.test.ts +++ b/packages/pyright-internal/src/tests/uri.test.ts @@ -177,6 +177,8 @@ test('empty', () => { assert.equal(empty4.isEmpty(), true); assert.ok(empty4.equals(empty3)); assert.ok(empty3.equals(empty)); + const combined = empty.combinePaths(normalizeSlashes('/d/e/f')); + assert.equal(combined.getFilePath(), normalizeSlashes('/d/e/f')); }); test('file', () => { @@ -184,6 +186,8 @@ test('file', () => { assert.equal(file1, normalizeSlashes('/a/b/c')); const file2 = Uri.file('file:///a/b/c').getFilePath(); assert.equal(file2, normalizeSlashes('/a/b/c')); + const resolved = Uri.file(normalizeSlashes('/a/b/c')).combinePaths(normalizeSlashes('/d/e/f')); + assert.equal(resolved.getFilePath(), normalizeSlashes('/d/e/f')); }); test('isUri', () => { @@ -390,7 +394,7 @@ test('combinePaths', () => { const uri10 = uri9.combinePaths('d', 'e'); assert.equal(uri10.toString(), 'foo:///d/e'); const uri11 = Uri.empty().combinePaths('d', 'e'); - assert.equal(uri11.toString(), Uri.empty().toString()); + assert.equal(uri11.toString(), Uri.file(normalizeSlashes('/d/e')).toString()); }); test('combinePaths non file', () => {