Skip to content

Commit

Permalink
chore disable check for arrow funstions for #116
Browse files Browse the repository at this point in the history
  • Loading branch information
rrd108 committed Aug 9, 2024
1 parent c1d7758 commit 5aa283d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
10 changes: 5 additions & 5 deletions src/analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ import { checkRules } from './rulesCheck'
import type { GroupBy } from './types'

let filesCount = 0
let _apply:Array<RuleSetType> = []
let _apply: Array<RuleSetType> = []

const dirs2Check = [
'src',
'components',
'pages',
'layouts',
'server',
'pages',
/* 'server',
'composables',
'store',
'utils',
'plugins',
'middleware',
'middleware', */
]

const walkAsync = async (dir: string) => {
Expand All @@ -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)
}
}
Expand Down
41 changes: 25 additions & 16 deletions src/rules/rrd/functionSize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down

0 comments on commit 5aa283d

Please sign in to comment.