diff --git a/src/utils.ts b/src/utils.ts index aca49d5..25e4a40 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -56,7 +56,7 @@ export async function getRemappingsFromFile(remappingsPath: string): Promise line.trim()) .filter((line) => line.length) - .map((line) => (line.slice(-1) === '/' ? line : line + '/')); + .map((line) => sanitizeRemapping(line)); } export async function getRemappingsFromConfig(foundryConfigPath: string): Promise { @@ -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: [] }; diff --git a/test/utils.test.ts b/test/utils.test.ts index 49462cb..270263f 100644 --- a/test/utils.test.ts +++ b/test/utils.test.ts @@ -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' ]`, @@ -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';