Skip to content

Commit

Permalink
Merge pull request #66 from David-Pena/fix/duplicated-objects-count
Browse files Browse the repository at this point in the history
fix: fileCount in output was counting duplicates
  • Loading branch information
rrd108 authored Jul 20, 2024
2 parents dbdbdcb + b4a815a commit 7305169
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
5 changes: 5 additions & 0 deletions src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Helper function to count non duplicated objects (using `field` to filter)
export function getUniqueFilenameCount<T>(arr: T[], field: Partial<keyof T>): number {
const uniqueFilenames = new Set(arr.map((item) => item[field]));
return uniqueFilenames.size;
}
13 changes: 11 additions & 2 deletions src/rules/rrd/functionSize.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { SFCScriptBlock } from '@vue/compiler-sfc';
import { BG_RESET, BG_WARN, TEXT_WARN, TEXT_RESET, TEXT_INFO } from '../asceeCodes'
import { getUniqueFilenameCount } from '../../helpers';

const functionSizeFiles: { filename: string, funcName: string }[] = [];
type FunctionSizeFile = {
filename: string;
funcName: string
};

const functionSizeFiles: FunctionSizeFile[] = [];

const MAX_FUNCTION_LENGTH = 20 // completely rrd made-up number

Expand All @@ -28,8 +34,11 @@ const checkFunctionSize = (script: SFCScriptBlock, filePath: string) => {

const reportFunctionSize = () => {
if (functionSizeFiles.length > 0) {
// Count only non duplicated objects (by its `filename` property)
const fileCount = getUniqueFilenameCount<FunctionSizeFile>(functionSizeFiles, 'filename');

console.log(
`\n${TEXT_INFO}rrd${TEXT_RESET} ${BG_WARN}function size${BG_RESET} exceeds recommended limit in ${functionSizeFiles.length} files.`
`\n${TEXT_INFO}rrd${TEXT_RESET} ${BG_WARN}function size${BG_RESET} exceeds recommended limit in ${fileCount} files.`
)
console.log(`👉 ${TEXT_WARN}Functions must be shorter than ${MAX_FUNCTION_LENGTH} lines${TEXT_RESET}`)
functionSizeFiles.forEach(file => {
Expand Down
11 changes: 2 additions & 9 deletions src/rules/rrd/shortVariableName.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SFCScriptBlock } from "@vue/compiler-sfc";
import { BG_RESET, BG_WARN, TEXT_WARN, TEXT_RESET, TEXT_INFO } from "../asceeCodes";
import { getUniqueFilenameCount } from "../../helpers";

type ShortVariableNameFile = {
filename: string;
Expand All @@ -10,12 +11,6 @@ const MIN_VARIABLE_NAME = 4; // completely rrd made-up number

const shortVariableNameFile: ShortVariableNameFile[] = [];

// Helper function to count non duplicated objects
const getUniqueFilenameCount = (arr: ShortVariableNameFile[]) => {
const uniqueFilenames = new Set(arr.map((item) => item.filename));
return uniqueFilenames.size;
};

const checkShortVariableName = (script: SFCScriptBlock, filePath: string) => {
// Regular expression to match variable names
const regex = /\b(?:const|var|let)\s+([a-zA-Z_$][a-zA-Z0-9_$]*)/g;
Expand All @@ -33,9 +28,7 @@ const checkShortVariableName = (script: SFCScriptBlock, filePath: string) => {
const reportShortVariableName = () => {
if (shortVariableNameFile.length > 0) {
// Count only non duplicated objects (by its `filename` property)
// i.e if three variables have short name from the same file, it will only count as 1 file
// and not as 3 files
const fileCount = getUniqueFilenameCount(shortVariableNameFile);
const fileCount = getUniqueFilenameCount<ShortVariableNameFile>(shortVariableNameFile, "filename");

console.log(
`\n${TEXT_INFO}rrd${TEXT_RESET} ${BG_WARN}variable names${BG_RESET} are too short in ${fileCount} files.`
Expand Down
12 changes: 9 additions & 3 deletions src/rules/vue-strong/templateSimpleExpression.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { SFCTemplateBlock } from '@vue/compiler-sfc'
import { BG_RESET, TEXT_WARN, TEXT_RESET, BG_ERR, TEXT_INFO, BG_WARN } from '../asceeCodes'
import getLineNumber from '../getLineNumber'
import { getUniqueFilenameCount } from '../../helpers';

const templateSimpleExpressionFiles: { message: string }[] = []
type TemplateSimpleExpressionFile = { filename: string, message: string };

const templateSimpleExpressionFiles: TemplateSimpleExpressionFile[] = []

const MAX_EXPRESSION_LENGTH = 40 // completely rrd made-up number

Expand All @@ -14,15 +17,18 @@ const checkTemplateSimpleExpression = (template: SFCTemplateBlock, filePath: str
if (expression.length > MAX_EXPRESSION_LENGTH) {
const lineNumber = getLineNumber(template.content, expression)
const firstPart = expression.split('\n').at(0)?.trim() || ''
templateSimpleExpressionFiles.push({ message: `${filePath}#${lineNumber} ${BG_WARN}${firstPart}${BG_RESET}` })
templateSimpleExpressionFiles.push({ filename: filePath, message: `${filePath}#${lineNumber} ${BG_WARN}${firstPart}${BG_RESET}` })
}
})
}

const reportTemplateSimpleExpression = () => {
if (templateSimpleExpressionFiles.length > 0) {
// Count only non duplicated objects (by its `filename` property)
const fileCount = getUniqueFilenameCount<TemplateSimpleExpressionFile>(templateSimpleExpressionFiles, 'filename')

console.log(
`\n${TEXT_INFO}vue-strong${TEXT_RESET} ${BG_ERR}Lengthy template expression${BG_RESET} found in ${templateSimpleExpressionFiles.length} files.`
`\n${TEXT_INFO}vue-strong${TEXT_RESET} ${BG_ERR}Lengthy template expression${BG_RESET} found in ${fileCount} files.`
)
console.log(
`👉 ${TEXT_WARN}Refactor the expression into a computed property.${TEXT_RESET} See: https://vuejs.org/style-guide/rules-strongly-recommended.html#simple-expressions-in-templates`
Expand Down

0 comments on commit 7305169

Please sign in to comment.