Skip to content

Commit

Permalink
feat add a script to check if a new rule is added properly fix #126
Browse files Browse the repository at this point in the history
  • Loading branch information
rrd108 committed Aug 10, 2024
1 parent 24f24cf commit 6ec3031
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/contribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ Thank you for your interest in contributing to vue-mess-detector! Follow these s
- Add your new `ruleName` entry to function call to `src/rules/rules.ts`
- Add your new `checkRuleName` function call to `src/rulesCheck.ts`
- Add your new `reportRuleName` function call to `src/rulesReport.ts`
- Run the missing rule check to ensure you have added all the necessary entries:
```bash
yarn rules:missing
```
8. **Add the rule to the documentation**
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"lint:fix": "eslint . --fix",
"docs:dev": "vitepress dev docs",
"docs:missing": "node ./docs/checkMissing.js",
"rules:missing": "node ./src/checkMissing.js",
"docs:build": "vitepress build docs",
"docs:preview": "vitepress preview docs"
},
Expand Down
99 changes: 99 additions & 0 deletions src/checkMissing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { error } from 'node:console'
import { promises as fs } from 'node:fs'
import path from 'node:path'

const isValidSourceFile = (currentPath, file) => currentPath !== './src/rules' && file.endsWith('.ts') && !file.endsWith('.test.ts')

const fileExists = async (filePath) => {
try {
await fs.access(filePath)
return true
}
catch {
return false
}
}

const findRules = async (srcDir) => {
const allRules = []

async function traverseDirectory(currentPath) {
const files = await fs.readdir(currentPath)

for (const file of files) {
const fullPath = path.join(currentPath, file)
const stat = await fs.stat(fullPath)

if (stat.isDirectory()) {
await traverseDirectory(fullPath)
}
else if (isValidSourceFile(currentPath, file)) {
allRules.push(`${currentPath}/${file}`)
}
}
}

await traverseDirectory(srcDir)
return allRules
}

const main = async () => {
const srcDirectory = './src/rules'
const rulesFile = './src/rules/rules.ts'
const rulesCheckFile = './src/rulesCheck.ts'
const rulesReportFile = './src/rulesReport.ts'

let errors = 0

try {
const allRules = await findRules(srcDirectory)

for (const rule of allRules) {
// check if it has a test file
const testFile = rule.replace('.ts', '.test.ts')
const testFileExists = await fileExists(testFile)
if (!testFileExists) {
console.log(`❌ ${rule} is missing a test file`)
errors++
}

const ruleName = rule.split('/').pop().replace('.ts', '')

// check if the rule is added to src/rules/rules.ts
const rulesFileContent = await fs.readFile(rulesFile, 'utf8')
if (!rulesFileContent.includes(ruleName)) {
console.log(`❌ ${ruleName} is missing from ${rulesFile}`)
errors++
}

// check if the rule is added to src/rulesCheck.ts
const rulesCheckFileContent = await fs.readFile(rulesCheckFile, 'utf8')
if (!rulesCheckFileContent.includes(ruleName)) {
console.log(`❌ ${ruleName} is missing from ${rulesCheckFile}`)
errors++
}

// check if the rule is added to src/rulesReport.ts
const rulesReportFileContent = await fs.readFile(rulesReportFile, 'utf8')
if (!rulesReportFileContent.includes(ruleName)) {
console.log(`❌ ${ruleName} is missing from ${rulesReportFile}`)
errors++
}
}

if (errors) {
console.error(`\n${errors} errors were found during the check.\n`)
// eslint-disable-next-line node/prefer-global/process
process.exit(1) // Exit with error code
} else {
console.log('All checks passed successfully.')
}
}
catch (error) {
console.error('🙀 An error occurred:', error)
// eslint-disable-next-line node/prefer-global/process
process.exit(1) // Exit with error code
}
}

main()
3 changes: 2 additions & 1 deletion src/rules/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ export const RULES = {
],
'rrd': [
'cyclomaticComplexity',
'deepIndentation',
'elseCondition',
'functionSize',
'htmlLink',
'magicNumbers',
'parameterCount',
'plainScript',
'scriptLenght',
'scriptLength',
'shortVariableName',
'tooManyProps',
],
Expand Down

0 comments on commit 6ec3031

Please sign in to comment.