Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utils: paths that don't start with slash are not handled correctly #26

Merged
merged 1 commit into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/test/paths.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ suite('URI path operations', () => {
assertJoin('foo://a/foo/bar/', ['x/', '/y'], 'foo://a/foo/bar/x/y');
assertJoin('foo://a/foo/bar/', ['.', '/y'], 'foo://a/foo/bar/y');
assertJoin('foo://a/foo/bar/', ['x/y/z', '..'], 'foo://a/foo/bar/x/y');
assertJoin('untitled:untitled-1', ['..', 'untitled-2'], 'untitled:untitled-2');
});

test('resolve', async function () {
Expand All @@ -44,6 +45,7 @@ suite('URI path operations', () => {

assertResolve('foo://a/b', '/x/..//y/.', 'foo://a/y');
assertResolve('foo://a/b', 'x/..//y/.', 'foo://a/b/y');
assertResolve('untitled:untitled-1', '../foo', 'untitled:foo', false);
});

test('normalize', async function () {
Expand Down Expand Up @@ -77,6 +79,7 @@ suite('URI path operations', () => {
assertNormalize('/a/n/../../..', '/');
assertNormalize('..', '..');
assertNormalize('/..', '/');
assertNormalize('untitled-1/foo/bar/.', 'untitled-1/foo/bar');
});

test('extname', async function () {
Expand All @@ -93,6 +96,7 @@ suite('URI path operations', () => {

assertExtName('foo://a/foo/a.foo/', '.foo');
assertExtName('foo://a/foo/a.foo//', '.foo');
assertExtName('untitled:untitled-1', '');
});

test('basename', () => {
Expand All @@ -111,6 +115,7 @@ suite('URI path operations', () => {
assertBasename('foo://a/some', 'some');
assertBasename('foo://a/', '');
assertBasename('foo://a', '');
assertBasename('untitled:untitled-1', 'untitled-1');
});

test('dirname', () => {
Expand All @@ -129,6 +134,7 @@ suite('URI path operations', () => {
assertDirname('foo://a/', 'foo://a/');
assertDirname('foo://a', 'foo://a', false);
assertDirname('foo://', 'foo:', false);
assertDirname('untitled:untitled-1', 'untitled:', false);
});

});
19 changes: 16 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { URI } from './uri';
import * as nodePath from 'path';

const posixPath = nodePath.posix || nodePath;
const slash = '/';

export namespace Utils {

Expand Down Expand Up @@ -45,8 +46,17 @@ export namespace Utils {
* @returns A URI with the resolved path. All other properties of the URI (scheme, authority, query, fragments, ...) will be taken from the input URI.
*/
export function resolvePath(uri: URI, ...paths: string[]): URI {
const path = uri.path || '/'; // normalize the path which is necessary as for posixPath.resolve the first segments has to be absolute or cwd is used.
return uri.with({ path: posixPath.resolve(path, ...paths) });
let path = uri.path || slash; // normalize the path which is necessary as for posixPath.resolve the first segments has to be absolute or cwd is used.
let slashAdded = false;
if (path[0] !== slash) {
path = slash + path;
slashAdded = true;
}
let resolvedPath = posixPath.resolve(path, ...paths);
if (slashAdded && resolvedPath[0] === slash) {
resolvedPath = resolvedPath.substring(1);
}
return uri.with({ path: resolvedPath });
}

/**
Expand All @@ -58,9 +68,12 @@ export namespace Utils {
* @return The last segment of the URIs path.
*/
export function dirname(uri: URI): URI {
if (uri.path.length === 0 || uri.path === '/') {
return uri;
}
let path = posixPath.dirname(uri.path);
if (path.length === 1 && path.charCodeAt(0) === CharCode.Period) {
return uri;
path = '';
}
return uri.with({ path });
}
Expand Down