Skip to content

Commit

Permalink
Merge branch '120-refactor-standardize-file-arrays-use'
Browse files Browse the repository at this point in the history
  • Loading branch information
rrd108 committed Aug 9, 2024
2 parents aba32a3 + 44ce66f commit 58d8b31
Show file tree
Hide file tree
Showing 36 changed files with 249 additions and 279 deletions.
30 changes: 12 additions & 18 deletions src/rules/rrd/cyclomaticComplexity.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
import type { SFCScriptBlock } from '@vue/compiler-sfc'
import { caseInsensitive, createRegExp, global, wordBoundary } from 'magic-regexp'
import { BG_ERR, BG_RESET, BG_WARN, TEXT_INFO, TEXT_RESET, TEXT_WARN } from '../asceeCodes'
import type { Offense } from '../../types'
import type { FileCheckResult, Offense } from '../../types'

const results: FileCheckResult[] = []

/**
* Defines complexity thresholds.
*/
const COMPLEXITY_MODERATE = 5
const COMPLEXITY_HIGH = 10

/**
* Array to store files with medium or high cyclomatic complexity.
*/
const cyclomaticComplexityFiles: { fileName: string, cyclomaticComplexity: number }[] = []

/**
* Function to check cyclomatic complexity of a SFC script block.
*
* @param {SFCScriptBlock} script - The SFC script block to analyze.
* @param {string} file - The filename of the SFC.
*/
const checkCyclomaticComplexity = (script: SFCScriptBlock | null, file: string) => {
const checkCyclomaticComplexity = (script: SFCScriptBlock | null, filePath: string) => {
if (!script) {
return
}
Expand All @@ -44,7 +38,9 @@ const checkCyclomaticComplexity = (script: SFCScriptBlock | null, file: string)
+ (_caseCount?.length || 0)

if (cyclomaticComplexity > COMPLEXITY_MODERATE) {
cyclomaticComplexityFiles.push({ fileName: file, cyclomaticComplexity })
results.push({ filePath, message: `${cyclomaticComplexity > COMPLEXITY_HIGH ? BG_ERR : BG_WARN}(${
cyclomaticComplexity
})${BG_RESET}`, })
}
}

Expand All @@ -56,21 +52,19 @@ const checkCyclomaticComplexity = (script: SFCScriptBlock | null, file: string)
const reportCyclomaticComplexity = () => {
const offenses: Offense[] = []

if (cyclomaticComplexityFiles.length > 0) {
cyclomaticComplexityFiles.forEach((file) => {
if (results.length > 0) {
results.forEach((result) => {
offenses.push({
file: file.fileName,
file: result.filePath,
rule: `${TEXT_INFO}rrd ~ cyclomatic complexity${TEXT_RESET}`,
description: `👉 ${TEXT_WARN}Try to reduce complexity.${TEXT_RESET}`,
message: `${file.cyclomaticComplexity > COMPLEXITY_HIGH ? BG_ERR : BG_WARN}(${
file.cyclomaticComplexity
})${BG_RESET} 🚨`,
message: `${result.message} 🚨`,
})
})
}
return offenses
}

const resetCyclomaticComplexity = () => (cyclomaticComplexityFiles.length = 0)
const resetCyclomaticComplexity = () => (results.length = 0)

export { checkCyclomaticComplexity, reportCyclomaticComplexity, resetCyclomaticComplexity }
21 changes: 9 additions & 12 deletions src/rules/rrd/deepIndentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ import type { SFCScriptBlock } from '@vue/compiler-sfc'
import { caseInsensitive, createRegExp, global, tab, whitespace } from 'magic-regexp'
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[] = []

interface DeepIndentationFile {
filePath: string
message: string
}
const deepIndentationFiles: DeepIndentationFile[] = []
const MAX_TABS = 5
const WHITESPACE_TO_TABS = 3

Expand All @@ -24,7 +21,7 @@ const checkDeepIndentation = (script: SFCScriptBlock | null, filePath: string) =

matches?.forEach((match) => {
const lineNumber = getLineNumber(script.content, match)
deepIndentationFiles.push({
results.push({
filePath,
message: `line #${lineNumber} ${BG_WARN}indentation: ${match.length}${BG_RESET}`,
})
Expand All @@ -34,19 +31,19 @@ const checkDeepIndentation = (script: SFCScriptBlock | null, filePath: string) =
const reportDeepIndentation = () => {
const offenses: Offense[] = []

if (deepIndentationFiles.length > 0) {
deepIndentationFiles.forEach((file) => {
if (results.length > 0) {
results.forEach((result) => {
offenses.push({
file: file.filePath,
file: result.filePath,
rule: `${TEXT_INFO}rrd ~ deep indentation${TEXT_RESET}`,
description: `👉 ${TEXT_WARN}Try to refactor your component to child components, to avoid deep indentations..${TEXT_RESET}`,
message: `${file.message} 🚨`,
message: `${result.message} 🚨`,
})
})
}
return offenses
}

const resetDeepIndentation = () => (deepIndentationFiles.length = 0)
const resetDeepIndentation = () => (results.length = 0)

export { checkDeepIndentation, reportDeepIndentation, resetDeepIndentation }
18 changes: 9 additions & 9 deletions src/rules/rrd/elseCondition.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
import type { SFCScriptBlock } from '@vue/compiler-sfc'
import { caseInsensitive, createRegExp, global, wordBoundary } from 'magic-regexp'
import { BG_ERR, BG_RESET, TEXT_INFO, TEXT_RESET, TEXT_WARN } from '../asceeCodes'
import type { Offense } from '../../types'
import type { FileCheckResult, Offense } from '../../types'

const elseConditionFiles: { fileName: string, elseCount: number }[] = []
const results: FileCheckResult[] = []

const checkElseCondition = (script: SFCScriptBlock | null, file: string) => {
const checkElseCondition = (script: SFCScriptBlock | null, filePath: string) => {
if (!script) {
return
}
const regex = createRegExp(wordBoundary, 'else', wordBoundary, [global, caseInsensitive])
const matches = script.content.match(regex)

if (matches?.length) {
elseConditionFiles.push({ fileName: file, elseCount: matches.length })
results.push({ filePath, message: `else clauses found ${BG_ERR}(${matches.length})${BG_RESET}`, })
}
}

const reportElseCondition = () => {
const offenses: Offense[] = []

if (elseConditionFiles.length > 0) {
elseConditionFiles.forEach((file) => {
if (results.length > 0) {
results.forEach((result) => {
offenses.push({
file: file.fileName,
file: result.filePath,
rule: `${TEXT_INFO}rrd ~ else conditions${TEXT_RESET}`,
description: `👉 ${TEXT_WARN}Try to rewrite the conditions in a way that the else clause is not necessary.${TEXT_RESET}`,
message: `else clauses found ${BG_ERR}(${file.elseCount})${BG_RESET} 🚨`,
message: `${result.message} 🚨`,
})
})
}
return offenses
}

const resetElseCondition = () => (elseConditionFiles.length = 0)
const resetElseCondition = () => (results.length = 0)

export { checkElseCondition, reportElseCondition, resetElseCondition }
4 changes: 2 additions & 2 deletions src/rules/rrd/functionSize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe('checkFunctionSize', () => {
file: fileName,
rule: `${TEXT_INFO}rrd ~ function size${TEXT_RESET}`,
description: `👉 ${TEXT_WARN}Functions must be shorter than ${MAX_FUNCTION_LENGTH} lines${TEXT_RESET}`,
message: `function ${BG_ERR}(${funcName})${BG_RESET} 🚨`,
message: `function ${BG_ERR}(${funcName})${BG_RESET} is too long 🚨`,
}])
})

Expand Down Expand Up @@ -151,7 +151,7 @@ describe('checkFunctionSize', () => {
file: fileName,
rule: `${TEXT_INFO}rrd ~ function size${TEXT_RESET}`,
description: `👉 ${TEXT_WARN}Functions must be shorter than ${MAX_FUNCTION_LENGTH} lines${TEXT_RESET}`,
message: `function ${BG_ERR}(dummyRegularFunction)${BG_RESET} 🚨`,
message: `function ${BG_ERR}(dummyRegularFunction)${BG_RESET} is too long 🚨`,
}/* TODO temporary disabled for #116
, {
file: fileName,
Expand Down
21 changes: 8 additions & 13 deletions src/rules/rrd/functionSize.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import type { SFCScriptBlock } from '@vue/compiler-sfc'
import { BG_ERR, BG_RESET, TEXT_INFO, TEXT_RESET, TEXT_WARN } from '../asceeCodes'
import type { Offense } from '../../types'
import type { FileCheckResult, Offense } from '../../types'

interface FunctionSizeFile {
filename: string
funcName: string
}

const functionSizeFiles: FunctionSizeFile[] = []
const results: FileCheckResult[] = []

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

Expand All @@ -18,7 +13,7 @@ const addFunctionToFiles = (match: RegExpExecArray, filePath: string) => {
// 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 })
results.push({ filePath, message: `function ${BG_ERR}(${funcName})${BG_RESET} is too long`, })
}
}

Expand Down Expand Up @@ -49,19 +44,19 @@ const checkFunctionSize = (script: SFCScriptBlock | null, filePath: string) => {
const reportFunctionSize = () => {
const offenses: Offense[] = []

if (functionSizeFiles.length > 0) {
functionSizeFiles.forEach((file) => {
if (results.length > 0) {
results.forEach((result) => {
offenses.push({
file: file.filename,
file: result.filePath,
rule: `${TEXT_INFO}rrd ~ function size${TEXT_RESET}`,
description: `👉 ${TEXT_WARN}Functions must be shorter than ${MAX_FUNCTION_LENGTH} lines${TEXT_RESET}`,
message: `function ${BG_ERR}(${file.funcName})${BG_RESET} 🚨`,
message: `${result.message} 🚨`,
})
})
}
return offenses
}

const resetFunctionSize = () => (functionSizeFiles.length = 0)
const resetFunctionSize = () => (results.length = 0)

export { checkFunctionSize, reportFunctionSize, resetFunctionSize }
16 changes: 8 additions & 8 deletions src/rules/rrd/magicNumbers.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { SFCScriptBlock } from '@vue/compiler-sfc'
import { anyOf, createRegExp, digit, global, linefeed, } from 'magic-regexp'
import { BG_RESET, BG_WARN, TEXT_INFO, TEXT_RESET, TEXT_WARN } from '../asceeCodes'
import type { Offense } from '../../types'
import type { FileCheckResult, Offense } from '../../types'
import getLineNumber from '../getLineNumber'

const magicNumberFiles: { filePath: string, message: string }[] = []
const results: FileCheckResult[] = []

const checkMagicNumbers = (script: SFCScriptBlock | null, filePath: string) => {
if (!script) {
Expand All @@ -15,7 +15,7 @@ const checkMagicNumbers = (script: SFCScriptBlock | null, filePath: string) => {

matches?.forEach((match) => {
const lineNumber = getLineNumber(script.content, match)
magicNumberFiles.push({
results.push({
filePath,
message: `line #${lineNumber} ${BG_WARN}magic number: ${match.length}${BG_RESET}`,
})
Expand All @@ -26,19 +26,19 @@ const checkMagicNumbers = (script: SFCScriptBlock | null, filePath: string) => {
const reportMagicNumbers = () => {
const offenses: Offense[] = []

if (magicNumberFiles.length > 0) {
magicNumberFiles.forEach((file) => {
if (results.length > 0) {
results.forEach((result) => {
offenses.push({
file: file.filePath,
file: result.filePath,
rule: `${TEXT_INFO}rrd ~ magic numbers${TEXT_RESET}`,
description: `👉 ${TEXT_WARN}Extract magic numbers to a constant.${TEXT_RESET}`,
message: `magic numbers found (${file.message}) 🚨`,
message: `magic numbers found (${result.message}) 🚨`,
})
})
}
return offenses
}

const resetMagicNumbers = () => (magicNumberFiles.length = 0)
const resetMagicNumbers = () => (results.length = 0)

export { checkMagicNumbers, reportMagicNumbers, resetMagicNumbers }
16 changes: 8 additions & 8 deletions src/rules/rrd/parameterCount.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { SFCScriptBlock } from '@vue/compiler-sfc'
import { BG_RESET, BG_WARN, TEXT_INFO, TEXT_RESET, TEXT_WARN } from '../asceeCodes'
import type { Offense } from '../../types'
import type { FileCheckResult, Offense } from '../../types'

const parameterCountFiles: { filename: string, funcName: string, paramsCount: number }[] = []
const results: FileCheckResult[] = []

export const MAX_PARAMETER_COUNT = 3 // completely rrd made-up number

Expand All @@ -13,7 +13,7 @@ const checkParameters = (funcName: string, params: string, filePath: string) =>
.map(param => param.trim())
.filter(param => param.length > 0)
if (paramsArray.length > MAX_PARAMETER_COUNT) {
parameterCountFiles.push({ filename: filePath, funcName, paramsCount: paramsArray.length })
results.push({ filePath, message: `function ${BG_WARN}${funcName}${BG_RESET} has ${BG_WARN}${paramsArray.length}${BG_RESET} parameters`, })
}
}

Expand Down Expand Up @@ -41,20 +41,20 @@ const checkParameterCount = (script: SFCScriptBlock | null, filePath: string) =>
const reportParameterCount = () => {
const offenses: Offense[] = []

if (parameterCountFiles.length > 0) {
parameterCountFiles.forEach((file) => {
if (results.length > 0) {
results.forEach((result) => {
offenses.push({
file: file.filename,
file: result.filePath,
rule: `${TEXT_INFO}rrd ~ parameter count${TEXT_RESET}`,
description: `👉 ${TEXT_WARN}Max number of function parameters should be ${MAX_PARAMETER_COUNT}${TEXT_RESET}`,
message: `function ${BG_WARN}${file.funcName}${BG_RESET} has ${BG_WARN}${file.paramsCount}${BG_RESET} parameters 🚨`,
message: `${result.message} 🚨`,
})
})
}

return offenses
}

const resetParameterCount = () => (parameterCountFiles.length = 0)
const resetParameterCount = () => (results.length = 0)

export { checkParameterCount, reportParameterCount, resetParameterCount }
16 changes: 8 additions & 8 deletions src/rules/rrd/plainScript.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import type { SFCScriptBlock } from '@vue/compiler-sfc'
import { TEXT_INFO, TEXT_RESET, TEXT_WARN } from '../asceeCodes'
import type { Offense } from '../../types'
import { BG_RESET, BG_WARN, TEXT_INFO, TEXT_RESET, TEXT_WARN } from '../asceeCodes'
import type { FileCheckResult, Offense } from '../../types'

const plainScriptFiles: string[] = []
const results: FileCheckResult[] = []

const checkPlainScript = (script: SFCScriptBlock | null, filePath: string) => {
if (!script || !script.setup) {
return
}
plainScriptFiles.push(filePath)
results.push({filePath, message: `${BG_WARN}Plain <script> block${BG_RESET} found`})
}

const reportPlainScript = () => {
const offenses: Offense[] = []

if (plainScriptFiles.length > 0) {
plainScriptFiles.forEach((file) => {
if (results.length > 0) {
results.forEach((result) => {
offenses.push({
file,
file: result.filePath,
rule: `${TEXT_INFO}rrd ~ Plain <script> blocks${TEXT_RESET}`,
description: `👉 ${TEXT_WARN} Consider using <script setup> to leverage the new SFC <script> syntax.${TEXT_RESET}`,
message: `🚨`,
message: `${result.message} 🚨`,
})
})
}
Expand Down
Loading

0 comments on commit 58d8b31

Please sign in to comment.