Skip to content

Commit

Permalink
fix ignore flag/config was not applied
Browse files Browse the repository at this point in the history
  • Loading branch information
rrd108 committed Oct 9, 2024
1 parent e527d2b commit c37c688
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 42 deletions.
38 changes: 24 additions & 14 deletions src/analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import { parse } from '@vue/compiler-sfc'
import { minimatch } from 'minimatch'
import { setHasServer, setIsNuxt } from './context'
import { calculateCodeHealth } from './helpers/calculateCodeHealth'
import { FLAT_RULES } from './helpers/constants'
import { getConfig } from './helpers/getConfig'
import getProjectRoot from './helpers/getProjectRoot'
import { groupRulesByRuleset } from './helpers/groupRulesByRuleset'
import hasServerDir from './helpers/hasServerDir'
import { isNuxtProject, isVueProject } from './helpers/projectTypeChecker'
import { RULESETS } from './rules/rules'
import { checkRules } from './rulesCheck'
import { reportRules } from './rulesReport'

Expand Down Expand Up @@ -87,6 +89,7 @@ export const analyze = async ({ dir, apply = [], ignore = [], exclude = '', grou
const config = await getConfig(projectRoot)

// Use config values if not provided in cli params
// TODO add support for merging cli and config
apply = apply.length ? apply : config.apply.split(',')
ignore = ignore.length ? ignore : config.ignore ? config.ignore.split(',') : []
exclude = exclude || config.exclude
Expand All @@ -96,22 +99,25 @@ export const analyze = async ({ dir, apply = [], ignore = [], exclude = '', grou

_override = config.override

const appliedRules = apply.filter(rule => !ignore.includes(rule))
apply = apply.filter(rule => !ignore.includes(rule))
const { rulesets, individualRules } = groupRulesByRuleset(apply)

const { rulesets, individualRules } = groupRulesByRuleset(appliedRules)
const rulesetsInIgnore = ignore.filter(rule => RULESETS.includes(rule as RuleSetType))
const rulesInIgnore = ignore.filter(rule => FLAT_RULES.includes(rule))
const ignoredRulesets = [...new Set([...rulesetsInIgnore, ...RULESETS.filter(ruleset => !rulesets.includes(ruleset))])]
const ignoredRules = [...new Set([...rulesInIgnore, ...FLAT_RULES.filter(rule => !individualRules.includes(rule))])]

// Prepare output messages for applied rulesets and rules
const rulesetsOutput = rulesets.length ? `<bg_info>${rulesets.join(', ')}</bg_info>` : 'N/A'
const indRulesOutput = individualRules.length ? `<bg_info>${individualRules.join(', ')}</bg_info>` : 'N/A'
const appliedRulesets = rulesets.filter(ruleset => !ignoredRulesets.includes(ruleset as RuleSetType))
const appliedRules = individualRules.filter(rule => !ignoredRules.includes(rule))
_apply = appliedRules

let applyingMessage = ` Applying ${rulesets.length} rulesets: ${rulesetsOutput}`
if (individualRules.length > 0) {
applyingMessage += `\n Applying ${individualRules.length} individual rules: ${indRulesOutput}`
}
// Prepare output messages for applied rulesets and rules
const appliedRulesetsOutput = appliedRulesets.length ? `<bg_info>${appliedRulesets.join(', ')}</bg_info>` : 'N/A'
const appliedRulesOutput = appliedRules.length ? `<bg_info>${appliedRules.join(', ')}</bg_info>` : 'N/A'

// Prepare output message for ignored rulesets
const ignoredRulesets = ignore.filter(ruleset => !rulesets.includes(ruleset as RuleSetType))
const ignoreRulesetsOutput = ignoredRulesets.length ? `<bg_info>${ignoredRulesets.join(', ')}</bg_info>` : 'N/A'
const ignoredRulesOutput = ignoredRules.length ? `<bg_info>${ignoredRules.join(', ')}</bg_info>` : 'N/A'

const isVue = await isVueProject(projectRoot)
const isNuxt = await isNuxtProject(projectRoot)
Expand All @@ -120,6 +126,11 @@ export const analyze = async ({ dir, apply = [], ignore = [], exclude = '', grou
const hasServer = hasServerDir(projectRoot)
setHasServer(hasServer)

let applyingMessage = ` Applying ${appliedRulesets.length} rulesets: ${appliedRulesetsOutput}`
if (individualRules.length > 0) {
applyingMessage += `\n Applying ${appliedRules.length} individual rules: ${appliedRulesOutput}`
}

const output: { info: string }[] = []

output.push({ info: `<bg_info>Analyzing Vue, TS and JS files in ${dir}</bg_info>` })
Expand All @@ -130,20 +141,19 @@ export const analyze = async ({ dir, apply = [], ignore = [], exclude = '', grou
output.push({ info: ` Project type: <bg_info>${isNuxt ? 'Nuxt' : ''}${isVue ? 'Vue' : ''}${!isNuxt && !isVue ? '?' : ''}</bg_info>` })
output.push({
info: `${applyingMessage}
Ignoring ${ignoredRulesets.length} rules: ${ignoreRulesetsOutput}
Ignoring ${ignoredRulesets.length} rulesets: ${ignoreRulesetsOutput}
Ignoring ${ignoredRules.length} individual rules: ${ignoredRulesOutput}
Excluding ${exclude || '-'}
Output level <bg_info>${level}</bg_info>
Grouping by <bg_info>${groupBy}</bg_info>
Sorting <bg_info>${sortBy}</bg_info>`,
})

// Filter out ignored rules from the apply list
_apply = apply.filter(rule => !ignore.includes(rule))

if (exclude) {
excludeFiles.push(...exclude.split(',').map(pattern => pattern.trim()))
}

// walk through the directory and check the files
const overview = await walkAsync(dir)
output.push(...overview.map(info => ({ info })))

Expand Down
14 changes: 10 additions & 4 deletions src/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,20 +225,26 @@ describe('yarn analyze command with configuration file with apply flag', () => {
})
})

describe('yarn analyze command with configuration file with ignore flag', async () => {
describe('yarn analyze command with configuration file with ignore flag', () => {
const projectRoot = path.resolve(__dirname, '..')
const configPath = path.join(projectRoot, '.config', 'vue-mess-detector.json')
const config = JSON.stringify({
ignore: 'vue-strong,vue-recommended',
}, null, 2)

it('should execute without any flags and path', async () => {
beforeAll(async () => {
await createConfigFile(configPath, config)
})

afterAll(async () => {
await removeConfigFile(configPath)
})

it('should execute without any flags and path', async () => {
const { stdout } = await runCLI()
expect(stdout).toContain(`👉 Using configuration from ${BG_INFO}vue-mess-detector.json${BG_RESET}`)
expect(stdout).toContain('Analyzing Vue, TS and JS files in ')
expect(stdout).toContain(`Ignoring 12 rules`)
await removeConfigFile(configPath)
expect(stdout).toContain('Ignoring 12 individual rules')
})
})

Expand Down
2 changes: 1 addition & 1 deletion src/helpers/coerceRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const coerceRules = (optionName: 'ignore' | 'apply') => {

if (invalidValues.length > 0) {
console.error(
`\n${BG_ERR}Invalid ${optionName} values: ${invalidValues.join(', ')}${BG_RESET}. \n${TEXT_WARN}Allowed values are: ${FLAT_RULESETS_RULES.join(', ')}${TEXT_RESET}\n\n`,
`\n${BG_ERR}Invalid ${optionName} values: ${invalidValues.join(', ')}${BG_RESET}. \n${TEXT_WARN}Allowed values are: ${FLAT_RULESETS_RULES.sort().join(', ')}${TEXT_RESET}\n\n`,
)
// eslint-disable-next-line node/prefer-global/process
process.exit(1)
Expand Down
28 changes: 18 additions & 10 deletions src/helpers/groupRulesByRuleset.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
import type { RuleSetType } from '../rules/rules'
import { RULES } from '../rules/rules'

export function groupRulesByRuleset(appliedRules: string[]): { rulesets: RuleSetType[], individualRules: string[] } {
export function groupRulesByRuleset(apply: string[]): { rulesets: RuleSetType[], individualRules: string[] } {
const rulesets: RuleSetType[] = []
const individualRules: string[] = []

// Iterate through each ruleset and its associated rules
Object.entries(RULES).forEach(([ruleset, rules]) => {
// if the ruleset can be found in apply, add it to rulesets
if (apply.includes(ruleset)) {
rulesets.push(ruleset as RuleSetType)
individualRules.push(...rules)
return
}

// Check if all rules in the current ruleset are applied
if (rules.every(rule => appliedRules.includes(rule))) {
if (rules.every(rule => apply.includes(rule))) {
// If so, add the entire ruleset
rulesets.push(ruleset as RuleSetType)
individualRules.push(...rules)
return
}
else {
// Otherwise, find individual rules that are applied
const appliedRulesInSet = rules.filter(rule => appliedRules.includes(rule))
// Add these individual rules to the list
individualRules.push(...appliedRulesInSet)
}

// Otherwise, find individual rules that are applied
const appliedRulesInSet = rules.filter(rule => apply.includes(rule))
// Add these individual rules to the list
individualRules.push(...appliedRulesInSet)
})

return { rulesets, individualRules }
}
return { rulesets: rulesets.sort(), individualRules: individualRules.sort() }
}
18 changes: 5 additions & 13 deletions src/rulesCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const checkRules = (descriptor: SFCDescriptor, filePath: string, apply: s
// rrd
amountOfComments: () => checkAmountOfComments(script, filePath),
bigVif: () => checkBigVif(descriptor.template, filePath, override.maxVifLines),
bigVShow: () => checkBigVshow(descriptor.template, filePath, override.maxVshowLines),
bigVshow: () => checkBigVshow(descriptor.template, filePath, override.maxVshowLines),
complicatedConditions: () => checkComplicatedConditions(descriptor, filePath, override.warningThreshold),
cyclomaticComplexity: () => checkCyclomaticComplexity(script, filePath, override.complexityModerate),
computedSideEffects: () => checkComputedSideEffects(script, filePath),
Expand Down Expand Up @@ -82,18 +82,10 @@ export const checkRules = (descriptor: SFCDescriptor, filePath: string, apply: s
}

// Run the checks for each applied rule or ruleset
apply.forEach((item) => {
if (item in RULES) {
// If it's a ruleset, apply all rules in that ruleset
RULES[item as keyof typeof RULES].forEach((rule) => {
if (rule in ruleChecks) {
ruleChecks[rule]()
}
})
}
if (item in ruleChecks) {
// If it's an individual rule, apply it directly
ruleChecks[item]()
apply.forEach((rule) => {
if (!(rule in ruleChecks)) {
console.error(`Rule ${rule} not found in ruleChecks`)
}
ruleChecks[rule]()
})
}

0 comments on commit c37c688

Please sign in to comment.