Skip to content

Commit

Permalink
bug: yarn 2 lock file parsing issues snyk#56
Browse files Browse the repository at this point in the history
  • Loading branch information
regevbr committed Apr 14, 2020
1 parent 5ef2940 commit 1a45373
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
10 changes: 8 additions & 2 deletions lib/parsers/yarn-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ export type ParseResolution = typeof parseResolution;

const keyNormalizer = (resolutionParser: ParseResolution) => (key: string): string => {
// See https://yarnpkg.com/features/protocols
key = key.replace(/::.*$/, '')
.trim();
let normalizedKey = key
.replace(/(#|::).*$/, '')
.replace(/#.*$/, '')
.trim();
const patchIndex = normalizedKey.indexOf(PATCH_PLACEHOLDER);
if (patchIndex > -1) {
normalizedKey = normalizedKey.substr(patchIndex + PATCH_PLACEHOLDER.length);
if (key.indexOf('#builtin') > -1) {
normalizedKey = normalizedKey.substr(patchIndex + PATCH_PLACEHOLDER.length);
} else {
return key;
}
}
const fileProtocol = normalizedKey.match(/^(.+)@((file|link|portal):.+)$/);
if (fileProtocol) {
Expand Down
12 changes: 11 additions & 1 deletion test/lib/yarn-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,21 @@ if (getRuntimeVersion() >= 10) {
});

test('Should work for patch protocol', async (t) => {
const key = normalizer('left-pad@patch:[email protected]#./my-patch.patch::locator=external-tarball%40workspace%3A.');
t.deepEqual(key, [ 'left-pad@patch:[email protected]#./my-patch.patch'], 'Resolution is normalized');
});

test('Should work for patch protocol with scope', async (t) => {
const key = normalizer('@types/istanbul-reports@patch:@types/[email protected]#./my-patch.patch::locator=external-tarball%40workspace%3A.');
t.deepEqual(key, [ '@types/istanbul-reports@patch:@types/[email protected]#./my-patch.patch'], 'Resolution is normalized');
});

test('Should work for builtin patch protocol', async (t) => {
const key = normalizer('fsevents@patch:fsevents@^1.2.7#builtin<compat/fsevents>');
t.deepEqual(key, [ 'fsevents@^1.2.7'], 'Resolution is normalized');
});

test('Should work for patch protocol - multiple resolutions', async (t) => {
test('Should work for builtin patch protocol - multiple resolutions', async (t) => {
const key = normalizer('fsevents@patch:fsevents@^1.2.7#builtin<compat/fsevents>, fsevents@patch:fsevents@^1.2.6#builtin<compat/fsevents>');
t.deepEqual(key, [ 'fsevents@^1.2.7', 'fsevents@^1.2.6'], 'Resolution is normalized');
});
Expand Down

0 comments on commit 1a45373

Please sign in to comment.