don't warn when fixing missing scheme, just do it... bring back strict mode for parse

This commit is contained in:
Johannes Rieken 2019-10-24 11:45:38 +02:00
parent 666ad41b9c
commit 175ab4aa51
5 changed files with 22 additions and 9 deletions

View file

@ -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] || '';

View file

@ -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');

4
src/vs/monaco.d.ts vendored
View file

@ -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`.

4
src/vs/vscode.d.ts vendored
View file

@ -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.

View file

@ -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) {