diff --git a/docs/contribute.md b/docs/contribute.md index 2caf7a0d..180e007d 100644 --- a/docs/contribute.md +++ b/docs/contribute.md @@ -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** diff --git a/package.json b/package.json index a85720b4..832e7ff1 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/src/checkMissing.js b/src/checkMissing.js new file mode 100644 index 00000000..998b168b --- /dev/null +++ b/src/checkMissing.js @@ -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() \ No newline at end of file diff --git a/src/rules/rules.ts b/src/rules/rules.ts index 9ab7f0c9..3e07dd9f 100644 --- a/src/rules/rules.ts +++ b/src/rules/rules.ts @@ -15,13 +15,14 @@ export const RULES = { ], 'rrd': [ 'cyclomaticComplexity', + 'deepIndentation', 'elseCondition', 'functionSize', 'htmlLink', 'magicNumbers', 'parameterCount', 'plainScript', - 'scriptLenght', + 'scriptLength', 'shortVariableName', 'tooManyProps', ],