Skip to content

Commit

Permalink
feat(no-important): add rule
Browse files Browse the repository at this point in the history
  • Loading branch information
bchanez committed Oct 12, 2024
1 parent 664828d commit 7dbcf4b
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/rules/rrd/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export * from './ifWithoutCurlyBraces'
export * from './magicNumbers'
export * from './nestedTernary'
export * from './noDirectDomAccess'
export * from './noImportant'
export * from './noInlineStyles'
export * from './noPropDestructure'
export * from './noSkippedTests'
Expand Down
44 changes: 44 additions & 0 deletions src/rules/rrd/noImportant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { SFCTemplateBlock } from '@vue/compiler-sfc'
import type { FileCheckResult, Offense } from '../../types'
import getLineNumber from '../getLineNumber'

const results: FileCheckResult[] = []

const resetResults = () => (results.length = 0)

const checkNoImportant = (template: SFCTemplateBlock | null, filePath: string) => {
if (!template) {
return
}

const regex = /!important/g

const matches = [...template.content.matchAll(regex)]
let from = 0
matches.forEach((match) => {
const lineNumber = getLineNumber(template.content.trim(), match[0], from)
results.push({ filePath, message: `line #${lineNumber} <bg_warn>Found !important</bg_warn>` })
from = lineNumber
})
}

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

if (results.length > 0) {
results.forEach((result) => {
offenses.push({
file: result.filePath,
rule: `<text_info>rdd ~ no !important</text_info>`,
description: `👉 <text_warn>Avoid !important as it complicates CSS management and disrupts natural cascading.</text_warn>`,
message: `${result.message} 🚨`,
})
})
}

resetResults()

return offenses
}

export { checkNoImportant, reportNoImportant }
1 change: 1 addition & 0 deletions src/rules/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export const RULES = {
'magicNumbers',
'nestedTernary',
'noDirectDomAccess',
'noImportant',
'noInlineStyles',
'noPropDestructure',
'noSkippedTests',
Expand Down
3 changes: 2 additions & 1 deletion src/rulesCheck.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { SFCDescriptor } from '@vue/compiler-sfc'
import type { OverrideConfig } from './types/Override'
import { getHasServer, getIsNuxt } from './context'
import { checkAmountOfComments, checkBigVif, checkBigVshow, checkComplicatedConditions, checkComputedSideEffects, checkCyclomaticComplexity, checkDeepIndentation, checkElseCondition, checkFunctionSize, checkHtmlImageElements, checkHtmlLink, checkHugeFiles, checkIfWithoutCurlyBraces, checkMagicNumbers, checkNestedTernary, checkNoDirectDomAccess, checkNoInlineStyles, checkNoPropDestructure, checkNoSkippedTests, checkNoTsLang, checkNoVarDeclaration, checkParameterCount, checkPlainScript, checkPropsDrilling, checkScriptLength, checkShortVariableName, checkTooManyProps, checkVForExpression, checkVForWithIndexKey, checkZeroLengthComparison } from './rules/rrd'
import { checkAmountOfComments, checkBigVif, checkBigVshow, checkComplicatedConditions, checkComputedSideEffects, checkCyclomaticComplexity, checkDeepIndentation, checkElseCondition, checkFunctionSize, checkHtmlImageElements, checkHtmlLink, checkHugeFiles, checkIfWithoutCurlyBraces, checkMagicNumbers, checkNestedTernary, checkNoDirectDomAccess, checkNoImportant, checkNoInlineStyles, checkNoPropDestructure, checkNoSkippedTests, checkNoTsLang, checkNoVarDeclaration, checkParameterCount, checkPlainScript, checkPropsDrilling, checkScriptLength, checkShortVariableName, checkTooManyProps, checkVForExpression, checkVForWithIndexKey, checkZeroLengthComparison } from './rules/rrd'
import { checkApiWithoutMethod, checkRateLimiter } from './rules/security'
import { checkElementSelectorsWithScoped, checkImplicitParentChildCommunication } from './rules/vue-caution'
import { checkGlobalStyle, checkSimpleProp, checkSingleNameComponent, checkVforNoKey, checkVifWithVfor } from './rules/vue-essential'
Expand Down Expand Up @@ -60,6 +60,7 @@ export const checkRules = (descriptor: SFCDescriptor, filePath: string, apply: s
magicNumbers: () => checkMagicNumbers(script, filePath),
nestedTernary: () => checkNestedTernary(script, filePath),
noDirectDomAccess: () => checkNoDirectDomAccess(script, filePath),
noImportant: () => checkNoImportant(descriptor.template, filePath),
noInlineStyles: () => checkNoInlineStyles(descriptor.template, filePath),
noPropDestructure: () => checkNoPropDestructure(script, filePath),
noSkippedTests: () => checkNoSkippedTests(script, filePath),
Expand Down
3 changes: 2 additions & 1 deletion src/rulesReport.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { GroupBy, Health, Offense, OffensesGrouped, OutputLevel, ReportFunction, SortBy } from './types'
import type { OverrideConfig } from './types/Override'
import type { ReportOutput } from './types/ReportOutput'
import { reportAmountOfComments, reportBigVif, reportBigVshow, reportComplicatedConditions, reportComputedSideEffects, reportCyclomaticComplexity, reportDeepIndentation, reportElseCondition, reportFunctionSize, reportHtmlImageElements, reportHtmlLink, reportHugeFiles, reportIfWithoutCurlyBraces, reportMagicNumbers, reportNestedTernary, reportNoDirectDomAccess, reportNoInlineStyles, reportNoPropDestructure, reportNoSkippedTests, reportNoTsLang, reportNoVarDeclaration, reportParameterCount, reportPlainScript, reportPropsDrilling, reportScriptLength, reportShortVariableName, reportTooManyProps, reportVForExpression, reportVForWithIndexKey, reportZeroLengthComparison } from './rules/rrd'
import { reportAmountOfComments, reportBigVif, reportBigVshow, reportComplicatedConditions, reportComputedSideEffects, reportCyclomaticComplexity, reportDeepIndentation, reportElseCondition, reportFunctionSize, reportHtmlImageElements, reportHtmlLink, reportHugeFiles, reportIfWithoutCurlyBraces, reportMagicNumbers, reportNestedTernary, reportNoDirectDomAccess, reportNoImportant, reportNoInlineStyles, reportNoPropDestructure, reportNoSkippedTests, reportNoTsLang, reportNoVarDeclaration, reportParameterCount, reportPlainScript, reportPropsDrilling, reportScriptLength, reportShortVariableName, reportTooManyProps, reportVForExpression, reportVForWithIndexKey, reportZeroLengthComparison } from './rules/rrd'
import { reportApiWithoutMethod, reportRateLimiter } from './rules/security'
import { reportElementSelectorsWithScoped, reportImplicitParentChildCommunication } from './rules/vue-caution'
import { reportGlobalStyle, reportSimpleProp, reportSingleNameComponent, reportVforNoKey, reportVifWithVfor } from './rules/vue-essential'
Expand Down Expand Up @@ -75,6 +75,7 @@ export const reportRules = (groupBy: GroupBy, sortBy: SortBy, level: OutputLevel
processOffenses(reportMagicNumbers)
processOffenses(reportNestedTernary)
processOffenses(reportNoDirectDomAccess)
processOffenses(reportNoImportant)
processOffenses(reportNoInlineStyles)
processOffenses(reportNoPropDestructure)
processOffenses(reportNoSkippedTests)
Expand Down

0 comments on commit 7dbcf4b

Please sign in to comment.