From ad19bdb9969b3bc236de9415a7d825e93996d3e2 Mon Sep 17 00:00:00 2001 From: rrd108 Date: Fri, 9 Aug 2024 18:42:42 +0200 Subject: [PATCH] refactor for #120 --- src/rules/vue-strong/directiveShorthands.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/rules/vue-strong/directiveShorthands.ts b/src/rules/vue-strong/directiveShorthands.ts index 46f25018..a4a45265 100644 --- a/src/rules/vue-strong/directiveShorthands.ts +++ b/src/rules/vue-strong/directiveShorthands.ts @@ -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'] @@ -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 }) @@ -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} 🚨`, }) }) } @@ -44,7 +45,7 @@ const reportDirectiveShorthands = () => { const resetDirectiveShorthands = () => { directiveShorthandsFiles.length = 0 - directiveShorthandsTargets.length = 0 + results.length = 0 } export { checkDirectiveShorthands, reportDirectiveShorthands, resetDirectiveShorthands }