Skip to content

Commit

Permalink
fix: do not create link if no exact match is found
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed Nov 11, 2023
1 parent b012eee commit 04556fa
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/linker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 5 additions & 2 deletions src/template_indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

/**
Expand Down
23 changes: 23 additions & 0 deletions tests/linker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 04556fa

Please sign in to comment.