|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { readdirSync, readFileSync } = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | +const assert = require('chai').assert; |
| 6 | +const plugin = require('../..'); |
| 7 | + |
| 8 | +const RULE_NAMES = Object.keys(plugin.rules); |
| 9 | +const RULE_NAMES_RECOMMENDED = new Set(Object.keys(plugin.configs.recommended.rules)); |
| 10 | + |
| 11 | +const MESSAGES = { |
| 12 | + fixable: |
| 13 | + '⚒️ The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#-fix) can automatically fix some of the problems reported by this rule.', |
| 14 | + configRecommended: |
| 15 | + '✔️ The `"extends": "plugin:eslint-plugin/recommended"` property in a configuration file enables this rule.', |
| 16 | +}; |
| 17 | + |
| 18 | +describe('rule setup is correct', () => { |
| 19 | + it('should have a list of exported rules and rules directory that match', () => { |
| 20 | + const filePath = path.join(__dirname, '..', 'lib', 'rules'); |
| 21 | + const files = readdirSync(filePath); |
| 22 | + |
| 23 | + assert.deepStrictEqual( |
| 24 | + RULE_NAMES, |
| 25 | + files |
| 26 | + .filter(file => !file.startsWith('.')) |
| 27 | + .map(file => file.replace('.js', '')) |
| 28 | + ); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should have tests for all rules', () => { |
| 32 | + const filePath = path.join(__dirname, 'rules'); |
| 33 | + const files = readdirSync(filePath); |
| 34 | + |
| 35 | + assert.deepStrictEqual( |
| 36 | + RULE_NAMES, |
| 37 | + files |
| 38 | + .filter(file => !file.startsWith('.')) |
| 39 | + .map(file => file.replace('.js', '')) |
| 40 | + ); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should have documentation for all rules', () => { |
| 44 | + const filePath = path.join(__dirname, '..', '..', 'docs', 'rules'); |
| 45 | + const files = readdirSync(filePath); |
| 46 | + |
| 47 | + assert.deepStrictEqual( |
| 48 | + RULE_NAMES, |
| 49 | + files |
| 50 | + .filter(file => !file.startsWith('.')) |
| 51 | + .map(file => file.replace('.md', '')) |
| 52 | + ); |
| 53 | + }); |
| 54 | + |
| 55 | + describe('rule documentation files', () => { |
| 56 | + for (const ruleName of RULE_NAMES) { |
| 57 | + const rule = plugin.rules[ruleName]; |
| 58 | + const filePath = path.join( |
| 59 | + __dirname, |
| 60 | + '..', |
| 61 | + '..', |
| 62 | + 'docs', |
| 63 | + 'rules', |
| 64 | + `${ruleName}.md` |
| 65 | + ); |
| 66 | + const fileContents = readFileSync(filePath, 'utf8'); |
| 67 | + const lines = fileContents.split('\n'); |
| 68 | + |
| 69 | + describe(ruleName, () => { |
| 70 | + it('should have the right contents (title, notices, etc)', () => { |
| 71 | + // Title |
| 72 | + assert.ok(lines[0].endsWith(`(${ruleName})`), 'first line ends with rule name'); |
| 73 | + assert.strictEqual(lines[1], '', 'second line is blank'); |
| 74 | + |
| 75 | + // Rule Details |
| 76 | + assert.ok(fileContents.includes('## Rule Details'), 'includes "## Rule Details" header'); |
| 77 | + |
| 78 | + // Examples |
| 79 | + assert.ok(fileContents.includes('Examples of **incorrect** code for this rule'), 'includes incorrect examples'); |
| 80 | + assert.ok(fileContents.includes('Examples of **correct** code for this rule'), 'includes correct examples'); |
| 81 | + |
| 82 | + // Decide which notices should be shown at the top of the doc. |
| 83 | + const expectedNotices = []; |
| 84 | + const unexpectedNotices = []; |
| 85 | + if (RULE_NAMES_RECOMMENDED.has('eslint-plugin/' + ruleName)) { |
| 86 | + expectedNotices.push('configRecommended'); |
| 87 | + } else { |
| 88 | + unexpectedNotices.push('configRecommended'); |
| 89 | + } |
| 90 | + if (rule.meta.fixable) { |
| 91 | + expectedNotices.push('fixable'); |
| 92 | + } else { |
| 93 | + unexpectedNotices.push('fixable'); |
| 94 | + } |
| 95 | + |
| 96 | + // Ensure that expected notices are present in the correct order. |
| 97 | + let currentLineNumber = 1; |
| 98 | + for (const expectedNotice of expectedNotices) { |
| 99 | + assert.strictEqual(lines[currentLineNumber], ''); |
| 100 | + assert.strictEqual(lines[currentLineNumber + 1], MESSAGES[expectedNotice]); |
| 101 | + currentLineNumber += 2; |
| 102 | + } |
| 103 | + |
| 104 | + // Ensure that unexpected notices are not present. |
| 105 | + for (const unexpectedNotice of unexpectedNotices) { |
| 106 | + assert.ok(!fileContents.includes(MESSAGES[unexpectedNotice]), 'does not include notice: ' + MESSAGES[unexpectedNotice]); |
| 107 | + } |
| 108 | + }); |
| 109 | + }); |
| 110 | + } |
| 111 | + }); |
| 112 | +}); |
0 commit comments