|
| 1 | +import { Linter, type Rule } from 'eslint'; |
| 2 | +import { builtinRules } from 'eslint/use-at-your-own-risk'; |
| 3 | +import assert from 'node:assert'; |
| 4 | +import type * as preferArrowCallbackModule from './prefer-arrow-callback.js'; |
| 5 | + |
| 6 | +const builtinRuleName = 'prefer-arrow-callback'; |
| 7 | +const originalGet = builtinRules.get.bind(builtinRules); |
| 8 | + |
| 9 | +type PreferArrowCallbackModule = typeof preferArrowCallbackModule; |
| 10 | + |
| 11 | +function isPreferArrowCallbackModule(value: unknown): value is PreferArrowCallbackModule { |
| 12 | + return typeof value === 'object' && |
| 13 | + value !== null && |
| 14 | + 'preferArrowCallbackRule' in value; |
| 15 | +} |
| 16 | + |
| 17 | +async function importPreferArrowCallbackRule(uniqueKey: string): Promise<PreferArrowCallbackModule> { |
| 18 | + const importedModule: unknown = await import(`./prefer-arrow-callback.js?${uniqueKey}=${Date.now()}`); |
| 19 | + |
| 20 | + assert.ok(isPreferArrowCallbackModule(importedModule)); |
| 21 | + |
| 22 | + return importedModule; |
| 23 | +} |
| 24 | + |
| 25 | +describe('prefer-arrow-callback rule wrapper', function () { |
| 26 | + it('throws when the ESLint core prefer-arrow-callback rule cannot be loaded', async function () { |
| 27 | + try { |
| 28 | + builtinRules.get = function (name) { |
| 29 | + return name === builtinRuleName ? undefined : originalGet(name); |
| 30 | + }; |
| 31 | + |
| 32 | + await assert.rejects( |
| 33 | + async function () { |
| 34 | + await importPreferArrowCallbackRule('missing-core-rule'); |
| 35 | + }, |
| 36 | + function (error: unknown) { |
| 37 | + return error instanceof Error && |
| 38 | + error.message === 'Unable to load the ESLint core "prefer-arrow-callback" rule.'; |
| 39 | + } |
| 40 | + ); |
| 41 | + } finally { |
| 42 | + builtinRules.get = originalGet; |
| 43 | + } |
| 44 | + }); |
| 45 | + |
| 46 | + it('falls back to default metadata when the core rule metadata is incomplete', async function () { |
| 47 | + try { |
| 48 | + builtinRules.get = function (name) { |
| 49 | + if (name === builtinRuleName) { |
| 50 | + return { |
| 51 | + meta: {}, |
| 52 | + create() { |
| 53 | + return {}; |
| 54 | + } |
| 55 | + }; |
| 56 | + } |
| 57 | + |
| 58 | + return originalGet(name); |
| 59 | + }; |
| 60 | + |
| 61 | + const { preferArrowCallbackRule } = await importPreferArrowCallbackRule('fallback-metadata'); |
| 62 | + |
| 63 | + assert.deepStrictEqual(preferArrowCallbackRule.meta, { |
| 64 | + type: 'suggestion', |
| 65 | + docs: { |
| 66 | + description: 'Require using arrow functions for callbacks', |
| 67 | + recommended: false, |
| 68 | + url: 'https://github.com/lo1tuma/eslint-plugin-mocha/blob/main/documentation/rules/prefer-arrow-callback.md' |
| 69 | + }, |
| 70 | + defaultOptions: [], |
| 71 | + schema: [], |
| 72 | + fixable: undefined, |
| 73 | + hasSuggestions: undefined, |
| 74 | + messages: { |
| 75 | + preferArrowCallback: 'Unexpected function expression.' |
| 76 | + } |
| 77 | + }); |
| 78 | + } finally { |
| 79 | + builtinRules.get = originalGet; |
| 80 | + } |
| 81 | + }); |
| 82 | + |
| 83 | + it('forwards rule context helper methods to the wrapped core rule', async function () { |
| 84 | + const observedCalls: string[] = []; |
| 85 | + const reportedMessages: string[] = []; |
| 86 | + |
| 87 | + try { |
| 88 | + builtinRules.get = function (name) { |
| 89 | + if (name === builtinRuleName) { |
| 90 | + return { |
| 91 | + meta: { |
| 92 | + type: 'problem', |
| 93 | + docs: { |
| 94 | + description: 'stub rule' |
| 95 | + }, |
| 96 | + defaultOptions: [], |
| 97 | + schema: [], |
| 98 | + fixable: 'code', |
| 99 | + hasSuggestions: true, |
| 100 | + messages: { |
| 101 | + preferArrowCallback: 'stub message' |
| 102 | + } |
| 103 | + }, |
| 104 | + create(ruleContext: Rule.RuleContext) { |
| 105 | + const [node] = ruleContext.sourceCode.ast.body as [Rule.Node?]; |
| 106 | + const reportNode = node ?? ruleContext.sourceCode.ast; |
| 107 | + |
| 108 | + observedCalls.push('getAncestors'); |
| 109 | + ruleContext.getAncestors(); |
| 110 | + observedCalls.push('getDeclaredVariables'); |
| 111 | + ruleContext.getDeclaredVariables(reportNode); |
| 112 | + observedCalls.push('getFilename'); |
| 113 | + ruleContext.getFilename(); |
| 114 | + observedCalls.push('getPhysicalFilename'); |
| 115 | + ruleContext.getPhysicalFilename(); |
| 116 | + observedCalls.push('getCwd'); |
| 117 | + ruleContext.getCwd(); |
| 118 | + observedCalls.push('getScope'); |
| 119 | + ruleContext.getScope(); |
| 120 | + observedCalls.push('getSourceCode'); |
| 121 | + ruleContext.getSourceCode(); |
| 122 | + observedCalls.push('markVariableAsUsed'); |
| 123 | + ruleContext.markVariableAsUsed('foo'); |
| 124 | + observedCalls.push('report'); |
| 125 | + ruleContext.report({ |
| 126 | + node: reportNode, |
| 127 | + message: 'wrapped report' |
| 128 | + }); |
| 129 | + |
| 130 | + return {}; |
| 131 | + } |
| 132 | + }; |
| 133 | + } |
| 134 | + |
| 135 | + return originalGet(name); |
| 136 | + }; |
| 137 | + |
| 138 | + const { preferArrowCallbackRule } = await importPreferArrowCallbackRule('forwarded-context'); |
| 139 | + const linter = new Linter({ configType: 'flat' }); |
| 140 | + const text = 'foo();'; |
| 141 | + linter.verify(text, [{ |
| 142 | + languageOptions: { ecmaVersion: 2022, sourceType: 'script' }, |
| 143 | + rules: {} |
| 144 | + }]); |
| 145 | + const sourceCode = linter.getSourceCode(); |
| 146 | + const ruleContext: Rule.RuleContext = { |
| 147 | + id: 'prefer-arrow-callback', |
| 148 | + options: [], |
| 149 | + settings: {}, |
| 150 | + parserPath: '<text>', |
| 151 | + languageOptions: { ecmaVersion: 2022, sourceType: 'script' }, |
| 152 | + parserOptions: {}, |
| 153 | + cwd: process.cwd(), |
| 154 | + filename: '<text>', |
| 155 | + physicalFilename: '<text>', |
| 156 | + sourceCode, |
| 157 | + getAncestors() { |
| 158 | + return []; |
| 159 | + }, |
| 160 | + getDeclaredVariables() { |
| 161 | + return []; |
| 162 | + }, |
| 163 | + getFilename() { |
| 164 | + return '<text>'; |
| 165 | + }, |
| 166 | + getPhysicalFilename() { |
| 167 | + return '<text>'; |
| 168 | + }, |
| 169 | + getCwd() { |
| 170 | + return process.cwd(); |
| 171 | + }, |
| 172 | + getScope() { |
| 173 | + const [scope] = sourceCode.scopeManager.scopes; |
| 174 | + |
| 175 | + assert.ok(scope !== undefined); |
| 176 | + |
| 177 | + return scope; |
| 178 | + }, |
| 179 | + getSourceCode() { |
| 180 | + return sourceCode; |
| 181 | + }, |
| 182 | + markVariableAsUsed() { |
| 183 | + return false; |
| 184 | + }, |
| 185 | + report(descriptor) { |
| 186 | + if ('message' in descriptor) { |
| 187 | + reportedMessages.push(descriptor.message); |
| 188 | + } |
| 189 | + } |
| 190 | + }; |
| 191 | + |
| 192 | + preferArrowCallbackRule.create(ruleContext); |
| 193 | + } finally { |
| 194 | + builtinRules.get = originalGet; |
| 195 | + } |
| 196 | + |
| 197 | + assert.deepStrictEqual(observedCalls, [ |
| 198 | + 'getAncestors', |
| 199 | + 'getDeclaredVariables', |
| 200 | + 'getFilename', |
| 201 | + 'getPhysicalFilename', |
| 202 | + 'getCwd', |
| 203 | + 'getScope', |
| 204 | + 'getSourceCode', |
| 205 | + 'markVariableAsUsed', |
| 206 | + 'report' |
| 207 | + ]); |
| 208 | + assert.deepStrictEqual(reportedMessages, ['wrapped report']); |
| 209 | + }); |
| 210 | +}); |
0 commit comments