From 3df23c53307f78a508f9aeb0fca6822fa0abfd5c Mon Sep 17 00:00:00 2001 From: David-Pena Date: Mon, 15 Jul 2024 22:03:20 -0700 Subject: [PATCH 1/3] feat: add rrd parameter-count rule --- src/rules/rrd/parameterCount.test.ts | 0 src/rules/rrd/parameterCount.ts | 44 ++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 src/rules/rrd/parameterCount.test.ts create mode 100644 src/rules/rrd/parameterCount.ts diff --git a/src/rules/rrd/parameterCount.test.ts b/src/rules/rrd/parameterCount.test.ts new file mode 100644 index 00000000..e69de29b diff --git a/src/rules/rrd/parameterCount.ts b/src/rules/rrd/parameterCount.ts new file mode 100644 index 00000000..c39bde08 --- /dev/null +++ b/src/rules/rrd/parameterCount.ts @@ -0,0 +1,44 @@ +import { SFCScriptBlock } from '@vue/compiler-sfc'; +import { BG_RESET, BG_WARN, TEXT_WARN, TEXT_RESET, TEXT_INFO } from '../asceeCodes' + +const parameterCountFiles: { filename: string, funcName: string, paramsCount: number }[] = []; + +const MAX_PARAMETER_COUNT = 3 // completely rrd made-up number + +// Function used in both scenarios (regular and arrow function) to count parameters +const checkParameters = (funcName: string, params: string, filePath: string) => { + const paramsArray = params.split(',').map(param => param.trim()).filter(param => param.length > 0); + if (paramsArray.length > MAX_PARAMETER_COUNT) { + parameterCountFiles.push({ filename: filePath, funcName, paramsCount: paramsArray.length }); + } +} + +const checkParameterCount = (script: SFCScriptBlock, filePath: string) => { + // regular expression to match both regular and arrow functions and capture their params + const regex = /function\s+([a-zA-Z0-9_$]+)\s*\(([^)]*)\)\s*{|const\s+([a-zA-Z0-9_$]+)\s*=\s*\(([^)]*)\)\s*=>\s*{/g; + let match; + + while ((match = regex.exec(script.content)) !== null) { + if (match[1]) { // Regular function + checkParameters(match[1], match[2], filePath); // match[2] are the params for current regular function + } else if (match[3]) { // Arrow function + checkParameters(match[3], match[4], filePath) // match[4] are the params for current arrow function + } + } +} + +const reportParameterCount = () => { + if (parameterCountFiles.length > 0) { + console.log( + `\n${TEXT_INFO}rrd${TEXT_RESET} ${BG_WARN}parameter count${BG_RESET} exceeds recommended limit in ${parameterCountFiles.length} files.` + ) + console.log(`👉 ${TEXT_WARN}Max number of function parameters should be ${MAX_PARAMETER_COUNT}${TEXT_RESET}`); + parameterCountFiles.forEach(file => { + console.log(`- ${file.funcName} in file ${file.filename} 🚨 ${BG_WARN}(${file.paramsCount})${BG_RESET}`); + }) + } + + return parameterCountFiles.length; +} + +export { checkParameterCount, reportParameterCount }; \ No newline at end of file From 7b942aebfcd3be06afa05cf8395932a1c09c1bc6 Mon Sep 17 00:00:00 2001 From: David-Pena Date: Mon, 15 Jul 2024 22:27:20 -0700 Subject: [PATCH 2/3] feat: add rrd parameter-count tests --- src/rules/rrd/parameterCount.test.ts | 73 ++++++++++++++++++++++++++++ src/rules/rrd/parameterCount.ts | 2 +- 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/src/rules/rrd/parameterCount.test.ts b/src/rules/rrd/parameterCount.test.ts index e69de29b..328036e8 100644 --- a/src/rules/rrd/parameterCount.test.ts +++ b/src/rules/rrd/parameterCount.test.ts @@ -0,0 +1,73 @@ +import { describe, expect, it, vi } from 'vitest' +import { SFCScriptBlock } from '@vue/compiler-sfc' +import { checkParameterCount, reportParameterCount } from './parameterCount' +import { BG_RESET, BG_WARN } from '../asceeCodes' + +const mockConsoleLog = vi.spyOn(console, 'log').mockImplementation(() => {}) + +describe('checkParameterCount', () => { + it('should not report files where functions do not exceed the recommended limit', () => { + const script = { + content: ` + + ` + } as SFCScriptBlock; + const filename = 'no-parameters.vue'; + checkParameterCount(script, filename); + expect(reportParameterCount()).toBe(0); + expect(mockConsoleLog).not.toHaveBeenCalled(); + }) + + it('should report files where one function exceeds the recommended limit', () => { + const script = { + content: ` + + ` + } as SFCScriptBlock; + const filename = 'one-parameter.vue'; + const funcName = 'dummyFuncTwo'; + const paramsCount = 4; + checkParameterCount(script, filename); + expect(reportParameterCount()).toBe(1); + expect(mockConsoleLog).toHaveBeenCalled(); + expect(mockConsoleLog).toHaveBeenLastCalledWith(`- ${BG_WARN}${funcName}${BG_RESET} in file ${filename} 🚨 ${BG_WARN}(${paramsCount})${BG_RESET}`) + + }) + + it('should report files where multiple functions exceed the recommended limit', () => { + const script = { + content: ` + + ` + } as SFCScriptBlock; + const filename = 'multiple-parameters.vue'; + const funcName = 'dummyFuncTwo'; + const paramsCount = 4; + checkParameterCount(script, filename); + expect(reportParameterCount()).toBe(3); + expect(mockConsoleLog).toHaveBeenCalled(); + expect(mockConsoleLog).toHaveBeenLastCalledWith(`- ${BG_WARN}${funcName}${BG_RESET} in file ${filename} 🚨 ${BG_WARN}(${paramsCount})${BG_RESET}`) + }) +}) \ No newline at end of file diff --git a/src/rules/rrd/parameterCount.ts b/src/rules/rrd/parameterCount.ts index c39bde08..44ee16a5 100644 --- a/src/rules/rrd/parameterCount.ts +++ b/src/rules/rrd/parameterCount.ts @@ -34,7 +34,7 @@ const reportParameterCount = () => { ) console.log(`👉 ${TEXT_WARN}Max number of function parameters should be ${MAX_PARAMETER_COUNT}${TEXT_RESET}`); parameterCountFiles.forEach(file => { - console.log(`- ${file.funcName} in file ${file.filename} 🚨 ${BG_WARN}(${file.paramsCount})${BG_RESET}`); + console.log(`- ${BG_WARN}${file.funcName}${BG_RESET} in file ${file.filename} 🚨 ${BG_WARN}(${file.paramsCount})${BG_RESET}`); }) } From 585f047223bf45cb75139f6ee4115ec6a36bcbb6 Mon Sep 17 00:00:00 2001 From: David-Pena Date: Mon, 15 Jul 2024 22:27:48 -0700 Subject: [PATCH 3/3] chore: add rule to main file & update readme --- README.md | 4 ++++ src/analyzer.ts | 3 +++ 2 files changed, 7 insertions(+) diff --git a/README.md b/README.md index 37e9350b..0e8c7b86 100644 --- a/README.md +++ b/README.md @@ -145,3 +145,7 @@ Checks if there are any `else` condition in the `