From 175ab4aa51cba05bd24acbf55eac87c52f4a3750 Mon Sep 17 00:00:00 2001 From: Johannes Rieken Date: Thu, 24 Oct 2019 11:45:38 +0200 Subject: [PATCH] don't warn when fixing missing scheme, just do it... bring back strict mode for parse --- src/vs/base/common/uri.ts | 16 +++++++++++----- src/vs/base/test/node/uri.test.ts | 5 +++++ src/vs/monaco.d.ts | 4 +++- src/vs/vscode.d.ts | 4 ++-- .../api/common/extHostTypeConverters.ts | 2 +- 5 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/vs/base/common/uri.ts b/src/vs/base/common/uri.ts index e56858a7d7d..d00eb22908c 100644 --- a/src/vs/base/common/uri.ts +++ b/src/vs/base/common/uri.ts @@ -41,10 +41,14 @@ function _validateUri(ret: URI): void { } // graceful behaviour when scheme is missing: fallback to using 'file'-scheme -function _schemeFix(scheme: string): string { +function _schemeFix(scheme: string, strict?: boolean): string { if (!scheme) { - console.trace('BAD uri lacks scheme, falling back to file-scheme.'); - scheme = 'file'; + if (strict) { + throw new Error('[UriError]: A scheme must be provided'); + } else { + // console.trace('BAD uri lacks scheme, falling back to file-scheme.'); + scheme = 'file'; + } } return scheme; } @@ -263,15 +267,17 @@ export class URI implements UriComponents { * Creates a new URI from a string, e.g. `http://www.msft.com/some/path`, * `file:///usr/home`, or `scheme:with/path`. * + * *Note:* When the input lacks a scheme then `file` is used. + * * @param value A string which represents an URI (see `URI#toString`). */ - static parse(value: string): URI { + static parse(value: string, strict?: boolean): URI { const match = _uriRegExp.exec(value); if (!match) { throw new Error(`[UriError]: Invalid input: ${value}`); } - const scheme = _schemeFix(match[MatchIndex.scheme]) || ''; + const scheme = _schemeFix(match[MatchIndex.scheme], strict) || ''; const authority = match[MatchIndex.authority] || ''; const path = _referenceResolution(scheme, match[MatchIndex.path] || ''); const query = match[MatchIndex.query] || ''; diff --git a/src/vs/base/test/node/uri.test.ts b/src/vs/base/test/node/uri.test.ts index 736a1bbf6b1..b9fbd24de8c 100644 --- a/src/vs/base/test/node/uri.test.ts +++ b/src/vs/base/test/node/uri.test.ts @@ -235,6 +235,11 @@ suite('URI', () => { assert.throws(() => URI.parse('file:////shares/files/p.cs')); }); + test('URI#parse, missing scheme', () => { + assert.throws(() => URI.parse('/foo/bar', true)); + assertToString('/foo/bar', 'file:///foo/bar'); + }); + test('URI#file, win-speciale', () => { if (isWindows) { let value = URI.file('c:\\test\\drive'); diff --git a/src/vs/monaco.d.ts b/src/vs/monaco.d.ts index b2158819314..ed218495220 100644 --- a/src/vs/monaco.d.ts +++ b/src/vs/monaco.d.ts @@ -129,9 +129,11 @@ declare namespace monaco { * Creates a new Uri from a string, e.g. `http://www.msft.com/some/path`, * `file:///usr/home`, or `scheme:with/path`. * + * *Note:* When the input lacks a scheme then `file` is used. + * * @param value A string which represents an Uri (see `Uri#toString`). */ - static parse(value: string): Uri; + static parse(value: string, strict?: boolean): Uri; /** * Creates a new Uri from a file system path, e.g. `c:\my\files`, * `/usr/home`, or `\\server\share\some\path`. diff --git a/src/vs/vscode.d.ts b/src/vs/vscode.d.ts index 27b2d1c77f4..e8c81c72b4b 100644 --- a/src/vs/vscode.d.ts +++ b/src/vs/vscode.d.ts @@ -1236,8 +1236,8 @@ declare module 'vscode' { * `file:///usr/home`, or `scheme:with/path`. * * *Note* that for a while uris without a `scheme` were accepted. That is not correct - * as all uris should have a scheme. To avoid breakage of existing code the optional - * `strict`-argument has been added. We *strongly* advise to use it, e.g. `Uri.parse('my:uri', true)` + * as all uris should have a scheme. When missing the `file`-scheme is being used unless + * the `strict`-argument is `true` in which case an error is thrown. * * @see [Uri.toString](#Uri.toString) * @param value The string value of an Uri. diff --git a/src/vs/workbench/api/common/extHostTypeConverters.ts b/src/vs/workbench/api/common/extHostTypeConverters.ts index c3f84a27286..9d0e711bfe8 100644 --- a/src/vs/workbench/api/common/extHostTypeConverters.ts +++ b/src/vs/workbench/api/common/extHostTypeConverters.ts @@ -260,7 +260,7 @@ export namespace MarkdownString { const collectUri = (href: string): string => { try { - let uri = URI.parse(href); + let uri = URI.parse(href, true); uri = uri.with({ query: _uriMassage(uri.query, resUris) }); resUris[href] = uri; } catch (e) { -- GitLab