Skip to content

Commit

Permalink
Merge pull request #403 from rrd108/david/feature/391-self-closing-fa…
Browse files Browse the repository at this point in the history
…lse-positive

fix: add new regex to handle nested self-closing (#391)
  • Loading branch information
rrd108 authored Oct 12, 2024
2 parents 480b12c + 59a0025 commit ff2ccf0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/rules/vue-strong/selfClosingComponents.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@ describe('checkSelfClosingComponents', () => {
expect(result).toStrictEqual([])
})

it('should not report files with self-closing components', () => {
const template = `<template>
<a
href="/path/to/link"
><InsideComponent /></a>
</template>`
const descriptor = {
source: template,
template: {
content: template,
},
} as SFCDescriptor
const fileName = 'self-close-component.vue'
checkSelfClosingComponents(descriptor, fileName)
const result = reportSelfClosingComponents()
expect(result.length).toBe(0)
expect(result).toStrictEqual([])
})

it('should not report files where the components self close', () => {
const template = `<template>
<MyComponent />
Expand Down
18 changes: 17 additions & 1 deletion src/rules/vue-strong/selfClosingComponents.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { SFCDescriptor } from '@vue/compiler-sfc'
import type { FileCheckResult, Offense } from '../../types'

import { charNotIn, createRegExp, letter, linefeed, maybe, oneOrMore, tab, wordChar } from 'magic-regexp'
import getLineNumber from '../getLineNumber'

Expand All @@ -25,13 +24,30 @@ const checkSelfClosingComponents = (descriptor: SFCDescriptor | null, filePath:
'>',
['g'],
)

// New regex to match self-closing components
const regexValidSelfClosingComponent = createRegExp(
'<',
oneOrMore(letter.uppercase, wordChar),
maybe(linefeed, tab),
maybe(oneOrMore(charNotIn('>'))),
'/>',
['g'],
)

const matches = template?.content?.match(regexSelfClosingComponent)

if (matches === null) {
return
}

matches?.forEach((componentTag) => {
// Check if the component tag contains a valid self-closing component
const validSelfClosingMatches = componentTag.match(regexValidSelfClosingComponent)
if (validSelfClosingMatches) {
return // Skip this match as it contains a valid self-closing component
}

const lineNumber = getLineNumber(descriptor.source, componentTag)
const lastPart = componentTag.split('\n').at(-1)?.trim() || ''
results.push({ filePath, message: `line #${lineNumber} <bg_warn>${lastPart}</bg_warn>` })
Expand Down

0 comments on commit ff2ccf0

Please sign in to comment.