|
| 1 | +/** |
| 2 | + * @fileoverview disallow identical tests |
| 3 | + * @author 薛定谔的猫<[email protected]> |
| 4 | + */ |
| 5 | + |
| 6 | +'use strict'; |
| 7 | + |
| 8 | +const utils = require('../utils'); |
| 9 | + |
| 10 | +// ------------------------------------------------------------------------------ |
| 11 | +// Rule Definition |
| 12 | +// ------------------------------------------------------------------------------ |
| 13 | + |
| 14 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 15 | +module.exports = { |
| 16 | + meta: { |
| 17 | + type: 'problem', |
| 18 | + docs: { |
| 19 | + description: 'Disallow identical tests', |
| 20 | + category: 'Tests', |
| 21 | + recommended: true, |
| 22 | + url: 'https://github.com/eslint-community/eslint-plugin-eslint-plugin/tree/HEAD/docs/rules/no-identical-tests.md', |
| 23 | + }, |
| 24 | + fixable: 'code', |
| 25 | + schema: [], |
| 26 | + messages: { |
| 27 | + identical: 'This test case is identical to another case.', |
| 28 | + }, |
| 29 | + }, |
| 30 | + |
| 31 | + create(context) { |
| 32 | + // ---------------------------------------------------------------------- |
| 33 | + // Public |
| 34 | + // ---------------------------------------------------------------------- |
| 35 | + const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9 |
| 36 | + |
| 37 | + // ---------------------------------------------------------------------- |
| 38 | + // Helpers |
| 39 | + // ---------------------------------------------------------------------- |
| 40 | + /** |
| 41 | + * Create a unique cache key |
| 42 | + * @param {object} test |
| 43 | + * @returns {string} |
| 44 | + */ |
| 45 | + function toKey(test) { |
| 46 | + if (test.type !== 'ObjectExpression') { |
| 47 | + return JSON.stringify([test.type, sourceCode.getText(test)]); |
| 48 | + } |
| 49 | + return JSON.stringify([ |
| 50 | + test.type, |
| 51 | + ...test.properties.map((p) => sourceCode.getText(p)).sort(), |
| 52 | + ]); |
| 53 | + } |
| 54 | + |
| 55 | + return { |
| 56 | + Program(ast) { |
| 57 | + utils.getTestInfo(context, ast).forEach((testRun) => { |
| 58 | + [testRun.valid, testRun.invalid].forEach((tests) => { |
| 59 | + const cache = new Set(); |
| 60 | + tests.forEach((test) => { |
| 61 | + const key = toKey(test); |
| 62 | + if (cache.has(key)) { |
| 63 | + context.report({ |
| 64 | + node: test, |
| 65 | + messageId: 'identical', |
| 66 | + fix(fixer) { |
| 67 | + const start = sourceCode.getTokenBefore(test); |
| 68 | + const end = sourceCode.getTokenAfter(test); |
| 69 | + return fixer.removeRange( |
| 70 | + // should remove test's trailing comma |
| 71 | + [ |
| 72 | + start.range[1], |
| 73 | + end.value === ',' ? end.range[1] : test.range[1], |
| 74 | + ], |
| 75 | + ); |
| 76 | + }, |
| 77 | + }); |
| 78 | + } else { |
| 79 | + cache.add(key); |
| 80 | + } |
| 81 | + }); |
| 82 | + }); |
| 83 | + }); |
| 84 | + }, |
| 85 | + }; |
| 86 | + }, |
| 87 | +}; |
0 commit comments