diff --git a/src/linker.ts b/src/linker.ts index bfef3f8..e2f0eb2 100644 --- a/src/linker.ts +++ b/src/linker.ts @@ -64,7 +64,7 @@ export class Linker { const lines = options.fileContent.split('\n') return matches.map((match) => { - const result = options.indexer?.search(match[2]!) + const result = options.indexer?.search(match[2]!, true) return { templatePath: result?.[0]?.path, diff --git a/src/template_indexer.ts b/src/template_indexer.ts index 2e578e1..8b56a18 100644 --- a/src/template_indexer.ts +++ b/src/template_indexer.ts @@ -51,11 +51,14 @@ export class TemplateIndexer { /** * Search for a template by the given text */ - search(text: string) { + search(text: string, exact = false) { text = text.replaceAll(/"|'/g, '').replaceAll('.', '/').replaceAll(/\s/g, '') if (!text) return this.#templates - return this.#templates.filter((template) => template.name.startsWith(text)) + return this.#templates.filter((template) => { + if (exact) return template.name === text + return template.name.startsWith(text) + }) } /** diff --git a/tests/linker.spec.ts b/tests/linker.spec.ts index 1483ff2..a7032b3 100644 --- a/tests/linker.spec.ts +++ b/tests/linker.spec.ts @@ -316,6 +316,29 @@ test.group('Views Linker | .edge', () => { const positions = result.map((r) => r.position) assert.deepEqual(positions, [{ colEnd: 8, colStart: 2, line: 0 }]) }) + + test('should not create link if exact match is not found', async ({ assert, fs }) => { + await fs.create('resources/views/components/button.edge', '') + + const template = dedent` + @component('components') + ` + + const indexer = new TemplateIndexer({ + rootPath: fs.basePath, + disks: { default: 'resources/views' }, + }) + + await indexer.scan() + + const result = await Linker.getLinks({ + fileContent: template, + indexer, + sourceType: 'edge', + }) + + assert.deepEqual(result, []) + }) }) test.group('Views Linker | .ts', () => {