From 5aa283d41a6aeb06dd90806a7c22061868f003c7 Mon Sep 17 00:00:00 2001 From: rrd108 Date: Fri, 9 Aug 2024 14:58:15 +0200 Subject: [PATCH] chore disable check for arrow funstions for #116 --- src/analyzer.ts | 10 ++++----- src/rules/rrd/functionSize.ts | 41 +++++++++++++++++++++-------------- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/src/analyzer.ts b/src/analyzer.ts index ddaba4d7..e31856a7 100644 --- a/src/analyzer.ts +++ b/src/analyzer.ts @@ -8,19 +8,19 @@ import { checkRules } from './rulesCheck' import type { GroupBy } from './types' let filesCount = 0 -let _apply:Array = [] +let _apply: Array = [] const dirs2Check = [ 'src', 'components', - 'pages', 'layouts', - 'server', + 'pages', + /* 'server', 'composables', 'store', 'utils', 'plugins', - 'middleware', + 'middleware', */ ] const walkAsync = async (dir: string) => { @@ -29,7 +29,7 @@ const walkAsync = async (dir: string) => { const filePath = path.join(dir, file) const stats = await fs.stat(filePath) if (stats.isDirectory()) { - if (dirs2Check.some(dir => filePath.includes(dir))) { + if (dirs2Check.some(dir => filePath.endsWith(`/${dir}`))) { await walkAsync(filePath) } } diff --git a/src/rules/rrd/functionSize.ts b/src/rules/rrd/functionSize.ts index 3f5888da..b8485d60 100644 --- a/src/rules/rrd/functionSize.ts +++ b/src/rules/rrd/functionSize.ts @@ -11,30 +11,39 @@ const functionSizeFiles: FunctionSizeFile[] = [] export const MAX_FUNCTION_LENGTH = 20 // completely rrd made-up number +const addFunctionToFiles = (match: RegExpExecArray, filePath: string) => { + const funcName = match[1] + const funcBody = match[2] + + // Check if the function block has more than `MAX_FUNCTION_LENGTH` lines + const lineCount = funcBody.split('\n').length + if (lineCount > MAX_FUNCTION_LENGTH) { + functionSizeFiles.push({ filename: filePath, funcName }) + } +} + const checkFunctionSize = (script: SFCScriptBlock | null, filePath: string) => { if (!script) { return } - // Regular expression to match function definitions (both regular and arrow functions) - const regex - = /function\s+([\w$]+)\s*\([^)]*\)\s*\{([^{}]*(([^{}]*\{[^{}]*\}[^{}]*)*[^{}]*))\}|const\s+([\w$]+)\s*=\s*\([^)]*\)\s*=>\s*\{([^{}]*(([^{}]*\{[^{}]*\}[^{}]*)*[^{}]*))\}/g + const regexNormalFunction + = /function\s+([\w$]+)\s*\([^)]*\)\s*\{([^{}]*(([^{}]*\{[^{}]*\}[^{}]*)*[^{}]*))\}/g + const regexArrowFunction + = /const\s+([\w$]+)\s*=\s*\([^)]*\)\s*=>\s*\{([^{}]*(([^{}]*\{[^{}]*\}[^{}]*)*[^{}]*))\}/g + // TODO it does not match arrow functions with no curly braces + let match // eslint-disable-next-line no-cond-assign - while ((match = regex.exec(script.content)) !== null) { - /* - We use match[1] and match[2] for regular functions - and match[5] and match[6] for arrow functions - */ - const funcName = match[1] || match[5] - const funcBody = match[2] || match[6] - - // Check if the function block has more than `MAX_FUNCTION_LENGTH` lines - const lineCount = funcBody.split('\n').length - if (lineCount > MAX_FUNCTION_LENGTH) { - functionSizeFiles.push({ filename: filePath, funcName }) - } + while ((match = regexNormalFunction.exec(script.content)) !== null) { + addFunctionToFiles(match, filePath) } + + // TODO temporary switch off see #116 + + // while ((match = regexArrowFunction.exec(script.content)) !== null) { + // addFunctionToFiles(match, filePath) + // } } const reportFunctionSize = () => {