Skip to content

Commit

Permalink
fix: sanitizing remappings (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
gas1cent authored Feb 16, 2024
1 parent 3210e73 commit 5cd57b1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
17 changes: 15 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export async function getRemappingsFromFile(remappingsPath: string): Promise<str
.split('\n')
.map((line) => line.trim())
.filter((line) => line.length)
.map((line) => (line.slice(-1) === '/' ? line : line + '/'));
.map((line) => sanitizeRemapping(line));
}

export async function getRemappingsFromConfig(foundryConfigPath: string): Promise<string[]> {
Expand All @@ -68,12 +68,25 @@ export async function getRemappingsFromConfig(foundryConfigPath: string): Promis
.groups!.remappings.split(',')
.map((line) => line.trim())
.map((line) => line.replace(/["']/g, ''))
.filter((line) => line.length);
.filter((line) => line.length)
.map((line) => sanitizeRemapping(line));
} else {
return [];
}
}

export function sanitizeRemapping(line: string): string {
// Make sure the key and the value both either have or don't have a trailing slash
const [key, value] = line.split('=');
const slashNeeded = key.endsWith('/');

if (slashNeeded) {
return value.endsWith('/') ? line : `${line}/`;
} else {
return value.endsWith('/') ? line.slice(0, -1) : line;
}
}

export function parseNodeNatspec(node: NodeToProcess): Natspec {
if (!node.documentation) {
return { tags: [], params: [], returns: [] };
Expand Down
32 changes: 29 additions & 3 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,17 @@ describe('Utils', () => {
);

remappings.set(
['ds-test/=lib/ds-test/src'], // Expected value
[`remappings = [ 'ds-test/=lib/ds-test/src' ]`] // Remappings strings that when parsed should return the expected value
['ds-test/=lib/ds-test/src/'], // Expected value
[`remappings = [ 'ds-test/=lib/ds-test/src/' ]`] // Remappings strings that when parsed should return the expected value
);

remappings.set(
['ds-test/=node_modules/ds-test/src', 'forge-std/=node_modules/forge-std/src'], // Expected value
['ds-test=lib/ds-test/src'], // Expected value
[`remappings = [ 'ds-test=lib/ds-test/src' ]`] // Remappings strings that when parsed should return the expected value
);

remappings.set(
['ds-test/=node_modules/ds-test/src/', 'forge-std/=node_modules/forge-std/src/'], // Expected value
[
// Remappings strings that when parsed should return the expected value
`remappings = [ 'ds-test/=node_modules/ds-test/src', 'forge-std/=node_modules/forge-std/src' ]`,
Expand Down Expand Up @@ -134,6 +139,27 @@ describe('Utils', () => {
});
});

describe('sanitizeRemapping', () => {
const key = 'ds-test';
const value = 'node_modules/ds-test/src';

it('should add a missing trailing slash', async () => {
expect(utils.sanitizeRemapping(`${key}/=${value}`)).toEqual(`${key}/=${value}/`);
});

it('should remove an extra trailing slash', async () => {
expect(utils.sanitizeRemapping(`${key}=${value}/`)).toEqual(`${key}=${value}`);
});

it('should not change the line if the trailing slash is correctly placed', async () => {
expect(utils.sanitizeRemapping(`${key}/=${value}/`)).toEqual(`${key}/=${value}/`);
});

it('should not change the line if the trailing slash is not needed', async () => {
expect(utils.sanitizeRemapping(`${key}=${value}`)).toEqual(`${key}=${value}`);
});
});

describe('getLineNumberFromSrc', () => {
it('should return correct line number', async () => {
const mockFileContent = '0\n1\n2\n3\n';
Expand Down

0 comments on commit 5cd57b1

Please sign in to comment.