Skip to content

Commit

Permalink
more jsdoc for the URI class, #56108
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken committed Aug 10, 2018
1 parent ffba35d commit 15b5eb4
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 12 deletions.
63 changes: 57 additions & 6 deletions src/vs/base/common/uri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const _regexp = /^(([^:/?#]+?):)?(\/\/([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/;

/**
* Uniform Resource Identifier (URI) http://tools.ietf.org/html/rfc3986.
* This class is a simple parser which creates the basic component paths
* This class is a simple parser which creates the basic component parts
* (http://tools.ietf.org/html/rfc3986#section-3) with minimal validation
* and encoding.
*
Expand All @@ -80,8 +80,6 @@ const _regexp = /^(([^:/?#]+?):)?(\/\/([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/;
* | _____________________|__
* / \ / \
* urn:example:animal:ferret:nose
*
*
*/
export default class URI implements UriComponents {

Expand Down Expand Up @@ -165,9 +163,27 @@ export default class URI implements UriComponents {

/**
* Returns a string representing the corresponding file system path of this URI.
* Will handle UNC paths and normalize windows drive letters to lower-case. Also
* uses the platform specific path separator. Will *not* validate the path for
* invalid characters and semantics. Will *not* look at the scheme of this URI.
* Will handle UNC paths, normalizes windows drive letters to lower-case, and uses the
* platform specific path separator.
*
* * Will *not* validate the path for invalid characters and semantics.
* * Will *not* look at the scheme of this URI.
* * The result shall *not* be used for display purposes but for accessing a file on disk.
*
*
* The *difference* to `URI#path` is the use of the platform specific separator and the handling
* of UNC paths. See the below sample of a file-uri with an authority (UNC path).
*
* ```ts
const u = URI.parse('file://server/c$/folder/file.txt')
u.authority === 'server'
u.path === '/shares/c$/file.txt'
u.fsPath === '\\server\c$\folder\file.txt'
```
*
* Using `URI#path` to read a file (using fs-apis) would not be enough because parts of the path,
* namely the server name, would be missing. Therefore `URI#fsPath` exists - it's sugar to ease working
* with URIs that represent files on disk (`file` scheme).
*/
get fsPath(): string {
return _makeFsPath(this);
Expand Down Expand Up @@ -222,6 +238,12 @@ export default class URI implements UriComponents {

// ---- parse & validate ------------------------

/**
* Creates a new URI from a string, e.g. `http://www.msft.com/some/path`,
* `file:///usr/home`, or `scheme:with/path`.
*
* @param value A string which represents an URI (see `URI#toString`).
*/
public static parse(value: string): URI {
const match = _regexp.exec(value);
if (!match) {
Expand All @@ -236,6 +258,28 @@ export default class URI implements UriComponents {
);
}

/**
* Creates a new URI from a file system path, e.g. `c:\my\files`,
* `/usr/home`, or `\\server\share\some\path`.
*
* The *difference* between `URI#parse` and `URI#file` is that the latter treats the argument
* as path, not as stringified-uri. E.g. `URI.file(path)` is **not the same as**
* `URI.parse('file://' + path)` because the path might contain characters that are
* interpreted (# and ?). See the following sample:
* ```ts
const good = URI.file('/coding/c#/project1');
good.scheme === 'file';
good.path === '/coding/c#/project1';
good.fragment === '';
const bad = URI.parse('file://' + '/coding/c#/project1');
bad.scheme === 'file';
bad.path === '/coding/c'; // path is now broken
bad.fragment === '/project1';
```
*
* @param path A file system path (see `URI#fsPath`)
*/
public static file(path: string): URI {

let authority = _empty;
Expand Down Expand Up @@ -276,6 +320,13 @@ export default class URI implements UriComponents {
// ---- printing/externalize ---------------------------

/**
* Creates a string presentation for this URI. It's guardeed that calling
* `URI.parse` with the result of this function creates an URI which is equal
* to this URI.
*
* * The result shall *not* be used for display purposes but for externalization or transport.
* * The result will be encoded using the percentage encoding and encoding happens mostly
* ignore the scheme-specific encoding rules.
*
* @param skipEncoding Do not encode the result, default is `false`
*/
Expand Down
63 changes: 57 additions & 6 deletions src/vs/monaco.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ declare namespace monaco {
}
/**
* Uniform Resource Identifier (Uri) http://tools.ietf.org/html/rfc3986.
* This class is a simple parser which creates the basic component paths
* This class is a simple parser which creates the basic component parts
* (http://tools.ietf.org/html/rfc3986#section-3) with minimal validation
* and encoding.
*
Expand All @@ -111,8 +111,6 @@ declare namespace monaco {
* | _____________________|__
* / \ / \
* urn:example:animal:ferret:nose
*
*
*/
export class Uri implements UriComponents {
static isUri(thing: any): thing is Uri;
Expand Down Expand Up @@ -140,9 +138,27 @@ declare namespace monaco {
readonly fragment: string;
/**
* Returns a string representing the corresponding file system path of this Uri.
* Will handle UNC paths and normalize windows drive letters to lower-case. Also
* uses the platform specific path separator. Will *not* validate the path for
* invalid characters and semantics. Will *not* look at the scheme of this Uri.
* Will handle UNC paths, normalizes windows drive letters to lower-case, and uses the
* platform specific path separator.
*
* * Will *not* validate the path for invalid characters and semantics.
* * Will *not* look at the scheme of this Uri.
* * The result shall *not* be used for display purposes but for accessing a file on disk.
*
*
* The *difference* to `Uri#path` is the use of the platform specific separator and the handling
* of UNC paths. See the below sample of a file-uri with an authority (UNC path).
*
* ```ts
const u = Uri.parse('file://server/c$/folder/file.txt')
u.authority === 'server'
u.path === '/shares/c$/file.txt'
u.fsPath === '\\server\c$\folder\file.txt'
```
*
* Using `Uri#path` to read a file (using fs-apis) would not be enough because parts of the path,
* namely the server name, would be missing. Therefore `Uri#fsPath` exists - it's sugar to ease working
* with URIs that represent files on disk (`file` scheme).
*/
readonly fsPath: string;
with(change: {
Expand All @@ -152,7 +168,35 @@ declare namespace monaco {
query?: string;
fragment?: string;
}): Uri;
/**
* Creates a new Uri from a string, e.g. `http://www.msft.com/some/path`,
* `file:///usr/home`, or `scheme:with/path`.
*
* @param value A string which represents an Uri (see `Uri#toString`).
*/
static parse(value: string): Uri;
/**
* Creates a new Uri from a file system path, e.g. `c:\my\files`,
* `/usr/home`, or `\\server\share\some\path`.
*
* The *difference* between `Uri#parse` and `Uri#file` is that the latter treats the argument
* as path, not as stringified-uri. E.g. `Uri.file(path)` is **not the same as**
* `Uri.parse('file://' + path)` because the path might contain characters that are
* interpreted (# and ?). See the following sample:
* ```ts
const good = Uri.file('/coding/c#/project1');
good.scheme === 'file';
good.path === '/coding/c#/project1';
good.fragment === '';
const bad = Uri.parse('file://' + '/coding/c#/project1');
bad.scheme === 'file';
bad.path === '/coding/c'; // path is now broken
bad.fragment === '/project1';
```
*
* @param path A file system path (see `Uri#fsPath`)
*/
static file(path: string): Uri;
static from(components: {
scheme?: string;
Expand All @@ -162,6 +206,13 @@ declare namespace monaco {
fragment?: string;
}): Uri;
/**
* Creates a string presentation for this Uri. It's guardeed that calling
* `Uri.parse` with the result of this function creates an Uri which is equal
* to this Uri.
*
* * The result shall *not* be used for display purposes but for externalization or transport.
* * The result will be encoded using the percentage encoding and encoding happens mostly
* ignore the scheme-specific encoding rules.
*
* @param skipEncoding Do not encode the result, default is `false`
*/
Expand Down

0 comments on commit 15b5eb4

Please sign in to comment.