|
| 1 | +import type { Rule } from 'eslint'; |
| 2 | + |
| 3 | +import { evaluateObjectProperties, getKeyName, getTestInfo } from '../utils.ts'; |
| 4 | +import type { TestInfo } from '../types.ts'; |
| 5 | + |
| 6 | +type TestCaseData = { |
| 7 | + node: NonNullable<TestInfo['valid'][number]>; |
| 8 | + isObject: boolean; |
| 9 | + hasName: boolean; |
| 10 | + hasConfig: boolean; |
| 11 | +}; |
| 12 | + |
| 13 | +const violationFilters = { |
| 14 | + always: (testCase: TestCaseData) => !testCase.hasName, |
| 15 | + objects: (testCase: TestCaseData) => testCase.isObject && !testCase.hasName, |
| 16 | + 'objects-with-config': (testCase: TestCaseData) => |
| 17 | + testCase.isObject && testCase.hasConfig && !testCase.hasName, |
| 18 | +} satisfies Record<Options['require'], (testCase: TestCaseData) => boolean>; |
| 19 | + |
| 20 | +const violationMessages = { |
| 21 | + always: 'nameRequiredAlways', |
| 22 | + objects: 'nameRequiredObjects', |
| 23 | + 'objects-with-config': 'nameRequiredObjectsWithConfig', |
| 24 | +} satisfies Record<Options['require'], string>; |
| 25 | + |
| 26 | +type Options = { |
| 27 | + require: 'always' | 'objects' | 'objects-with-config'; |
| 28 | +}; |
| 29 | + |
| 30 | +const rule: Rule.RuleModule = { |
| 31 | + meta: { |
| 32 | + type: 'suggestion', |
| 33 | + docs: { |
| 34 | + description: |
| 35 | + 'require test cases to have a `name` property under certain conditions', |
| 36 | + category: 'Tests', |
| 37 | + recommended: false, |
| 38 | + url: 'https://github.com/eslint-community/eslint-plugin-eslint-plugin/tree/HEAD/docs/rules/require-test-case-name.md', |
| 39 | + }, |
| 40 | + schema: [ |
| 41 | + { |
| 42 | + additionalProperties: false, |
| 43 | + properties: { |
| 44 | + require: { |
| 45 | + description: |
| 46 | + 'When should the name property be required on a test case object.', |
| 47 | + enum: ['always', 'objects', 'objects-with-config'], |
| 48 | + }, |
| 49 | + }, |
| 50 | + type: 'object', |
| 51 | + }, |
| 52 | + ], |
| 53 | + defaultOptions: [{ require: 'objects-with-config' }], |
| 54 | + messages: { |
| 55 | + nameRequiredAlways: |
| 56 | + 'This test case is missing the `name` property. All test cases should have a name property.', |
| 57 | + nameRequiredObjects: |
| 58 | + 'This test case is missing the `name` property. Test cases defined as objects should have a name property.', |
| 59 | + nameRequiredObjectsWithConfig: |
| 60 | + 'This test case is missing the `name` property. Test cases defined as objects with additional configuration should have a name property.', |
| 61 | + }, |
| 62 | + }, |
| 63 | + |
| 64 | + create(context) { |
| 65 | + const { require: requireOption = 'objects-with-config' }: Options = |
| 66 | + context.options[0] || {}; |
| 67 | + const sourceCode = context.sourceCode; |
| 68 | + |
| 69 | + /** |
| 70 | + * Validates test cases and reports them if found in violation |
| 71 | + * @param cases A list of test case nodes |
| 72 | + */ |
| 73 | + function validateTestCases(cases: TestInfo['valid']): void { |
| 74 | + // Gather all of the information from each test case |
| 75 | + const testCaseData: TestCaseData[] = cases |
| 76 | + .filter((testCase) => !!testCase) |
| 77 | + .map((testCase) => { |
| 78 | + if ( |
| 79 | + testCase.type === 'Literal' || |
| 80 | + testCase.type === 'TemplateLiteral' |
| 81 | + ) { |
| 82 | + return { |
| 83 | + node: testCase, |
| 84 | + isObject: false, |
| 85 | + hasName: false, |
| 86 | + hasConfig: false, |
| 87 | + }; |
| 88 | + } |
| 89 | + if (testCase.type === 'ObjectExpression') { |
| 90 | + let hasName = false; |
| 91 | + let hasConfig = false; |
| 92 | + |
| 93 | + // evaluateObjectProperties is used here to expand spread elements |
| 94 | + for (const property of evaluateObjectProperties( |
| 95 | + testCase, |
| 96 | + sourceCode.scopeManager, |
| 97 | + )) { |
| 98 | + if (property.type === 'Property') { |
| 99 | + const keyName = getKeyName( |
| 100 | + property, |
| 101 | + sourceCode.getScope(testCase), |
| 102 | + ); |
| 103 | + if (keyName === 'name') { |
| 104 | + hasName = true; |
| 105 | + } else if (keyName === 'options' || keyName === 'settings') { |
| 106 | + hasConfig = true; |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return { |
| 112 | + node: testCase, |
| 113 | + isObject: true, |
| 114 | + hasName, |
| 115 | + hasConfig, |
| 116 | + }; |
| 117 | + } |
| 118 | + return null; |
| 119 | + }) |
| 120 | + .filter((testCase) => !!testCase); |
| 121 | + |
| 122 | + const violations = testCaseData.filter(violationFilters[requireOption]); |
| 123 | + for (const violation of violations) { |
| 124 | + context.report({ |
| 125 | + node: violation.node, |
| 126 | + messageId: violationMessages[requireOption], |
| 127 | + }); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + return { |
| 132 | + Program(ast) { |
| 133 | + getTestInfo(context, ast) |
| 134 | + .map((testRun) => [...testRun.valid, ...testRun.invalid]) |
| 135 | + .forEach(validateTestCases); |
| 136 | + }, |
| 137 | + }; |
| 138 | + }, |
| 139 | +}; |
| 140 | + |
| 141 | +export default rule; |
0 commit comments