Skip to content

Commit

Permalink
refactor for #120
Browse files Browse the repository at this point in the history
  • Loading branch information
rrd108 committed Aug 9, 2024
1 parent 58d8b31 commit ad19bdb
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/rules/vue-strong/directiveShorthands.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { SFCDescriptor } from '@vue/compiler-sfc'
import { BG_RESET, BG_WARN, TEXT_INFO, TEXT_RESET, TEXT_WARN } from '../asceeCodes'
import getLineNumber from '../getLineNumber'
import type { Offense } from '../../types'
import type { FileCheckResult, Offense } from '../../types'

const results: FileCheckResult[] = []

const directiveShorthandsTargets: { filename: string, message: string }[] = []
const directiveShorthandsFiles: { filePath: string }[] = []

const directivesToCheck = ['v-slot', 'v-bind', 'v-on']
Expand All @@ -17,7 +18,7 @@ const checkDirectiveShorthands = (descriptor: SFCDescriptor | null, filePath: st
directivesToCheck.forEach((directive) => {
if (template?.content.includes(`${directive}:`)) {
const lineNumber = getLineNumber(descriptor.source, directive)
directiveShorthandsTargets.push({ filename: filePath, message: `line #${lineNumber} ${BG_WARN}${directive}${BG_RESET}` })
results.push({ filePath, message: `line #${lineNumber} ${BG_WARN}${directive}${BG_RESET}` })

if (!directiveShorthandsFiles.some(file => file.filePath === filePath)) {
directiveShorthandsFiles.push({ filePath })
Expand All @@ -29,13 +30,13 @@ const checkDirectiveShorthands = (descriptor: SFCDescriptor | null, filePath: st
const reportDirectiveShorthands = () => {
const offenses: Offense[] = []

if (directiveShorthandsTargets.length > 0) {
directiveShorthandsTargets.forEach((file) => {
if (results.length > 0) {
results.forEach((result) => {
offenses.push({
file: file.filename,
file: result.filePath,
rule: `${TEXT_INFO}vue-strong ~ directive shorthands not used${TEXT_RESET}`,
description: `👉 ${TEXT_WARN}Use ":" for v-bind:, "@" for v-on: and "#" for v-slot.${TEXT_RESET} See: https://vuejs.org/style-guide/rules-strongly-recommended.html#directive-shorthands`,
message: `${file.message} 🚨`,
message: `${result.message} 🚨`,
})
})
}
Expand All @@ -44,7 +45,7 @@ const reportDirectiveShorthands = () => {

const resetDirectiveShorthands = () => {
directiveShorthandsFiles.length = 0
directiveShorthandsTargets.length = 0
results.length = 0
}

export { checkDirectiveShorthands, reportDirectiveShorthands, resetDirectiveShorthands }

0 comments on commit ad19bdb

Please sign in to comment.