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

fix(utils): dirname and basename should handle Windows paths #8737

Merged
merged 3 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion packages/utils/src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@

// Split a filename into [root, dir, basename, ext], unix version
// 'root' is just a slash, or nothing.
const splitPathRe = /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^/]+?|)(\.[^./]*|))(?:[/]*)$/;
const splitPathRe = /^(\S+:\\|\/?)([\s\S]*?)((?:\.{1,2}|[^/\\]+?|)(\.[^./\\]*|))(?:[/\\]*)$/;
timfish marked this conversation as resolved.
Show resolved Hide resolved
/** JSDoc */
function splitPath(filename: string): string[] {
const parts = splitPathRe.exec(filename);

Check failure

Code scanning / CodeQL

Polynomial regular expression used on uncontrolled data High

This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '.'.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '.'.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '.'.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '.'.
timfish marked this conversation as resolved.
Show resolved Hide resolved
return parts ? parts.slice(1) : [];
}

Expand Down
29 changes: 29 additions & 0 deletions packages/utils/test/path.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { basename, dirname } from '../src/path';

describe('path', () => {
describe('basename', () => {
test('unix', () => {
expect(basename('/foo/bar/baz/asdf/quux.html')).toEqual('quux.html');
expect(basename('../baz/asdf/quux.html')).toEqual('quux.html');
expect(basename('quux.html')).toEqual('quux.html');
});
test('windows', () => {
expect(basename('c:\\foo\\bar\\baz\\asdf\\quux.html')).toEqual('quux.html');
expect(basename('..\\bar\\baz\\asdf\\quux.html')).toEqual('quux.html');
expect(basename('quux.html')).toEqual('quux.html');
});
});

describe('dirname', () => {
test('unix', () => {
expect(dirname('/foo/bar/baz/asdf/quux.html')).toEqual('/foo/bar/baz/asdf');
expect(dirname('../baz/asdf/quux.html')).toEqual('../baz/asdf');
expect(dirname('/quux.html')).toEqual('/');
});
test('windows', () => {
expect(dirname('C:\\foo\\bar\\baz\\asdf\\quux.html')).toEqual('C:\\foo\\bar\\baz\\asdf');
expect(dirname('..\\bar\\baz\\asdf\\quux.html')).toEqual('..\\bar\\baz\\asdf');
expect(dirname('quux.html')).toEqual('.');
});
});
});
Loading