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: sanitizing remappings #39

Merged
merged 3 commits into from
Feb 16, 2024
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
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
Loading