|
| 1 | +const MESSAGE_ID_ERROR = 'consistent-assert/error'; |
| 2 | +const messages = { |
| 3 | + [MESSAGE_ID_ERROR]: 'Prefer `{{name}}.ok(…)` over `{{name}}(…)`.', |
| 4 | +}; |
| 5 | + |
| 6 | +/** |
| 7 | +@param {import('estree').ImportSpecifier | import('estree').ImportDefaultSpecifier | import('estree').ImportSpecifier | import('estree').ImportDeclaration} node |
| 8 | +*/ |
| 9 | +const isValueImport = node => !node.importKind || node.importKind === 'value'; |
| 10 | + |
| 11 | +/** |
| 12 | +Check if a specifier is `assert` function. |
| 13 | +
|
| 14 | +@param {import('estree').ImportSpecifier | import('estree').ImportDefaultSpecifier} specifier |
| 15 | +@param {string} moduleName |
| 16 | +*/ |
| 17 | +const isAssertFunction = (specifier, moduleName) => |
| 18 | + // `import assert from 'node:assert';` |
| 19 | + // `import assert from 'node:assert/strict';` |
| 20 | + specifier.type === 'ImportDefaultSpecifier' |
| 21 | + // `import {default as assert} from 'node:assert';` |
| 22 | + // `import {default as assert} from 'node:assert/strict';` |
| 23 | + || ( |
| 24 | + specifier.type === 'ImportSpecifier' |
| 25 | + && specifier.imported.name === 'default' |
| 26 | + ) |
| 27 | + // `import {strict as assert} from 'node:assert';` |
| 28 | + || ( |
| 29 | + moduleName === 'assert' |
| 30 | + && specifier.type === 'ImportSpecifier' |
| 31 | + && specifier.imported.name === 'strict' |
| 32 | + ); |
| 33 | + |
| 34 | +const NODE_PROTOCOL = 'node:'; |
| 35 | + |
| 36 | +/** @type {import('eslint').Rule.RuleModule['create']} */ |
| 37 | +const create = context => ({ |
| 38 | + * ImportDeclaration(importDeclaration) { |
| 39 | + if (!isValueImport(importDeclaration)) { |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + let moduleName = importDeclaration.source.value; |
| 44 | + |
| 45 | + if (moduleName.startsWith(NODE_PROTOCOL)) { |
| 46 | + moduleName = moduleName.slice(NODE_PROTOCOL.length); |
| 47 | + } |
| 48 | + |
| 49 | + if (moduleName !== 'assert' && moduleName !== 'assert/strict') { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + for (const specifier of importDeclaration.specifiers) { |
| 54 | + if (!isValueImport(specifier) || !isAssertFunction(specifier, moduleName)) { |
| 55 | + continue; |
| 56 | + } |
| 57 | + |
| 58 | + const variables = context.sourceCode.getDeclaredVariables(specifier); |
| 59 | + |
| 60 | + /* c8 ignore next 3 */ |
| 61 | + if (!Array.isArray(variables) && variables.length === 1) { |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + const [variable] = variables; |
| 66 | + |
| 67 | + for (const {identifier} of variable.references) { |
| 68 | + if (!(identifier.parent.type === 'CallExpression' && identifier.parent.callee === identifier)) { |
| 69 | + continue; |
| 70 | + } |
| 71 | + |
| 72 | + yield { |
| 73 | + node: identifier, |
| 74 | + messageId: MESSAGE_ID_ERROR, |
| 75 | + data: {name: identifier.name}, |
| 76 | + /** @param {import('eslint').Rule.RuleFixer} fixer */ |
| 77 | + fix: fixer => fixer.insertTextAfter(identifier, '.ok'), |
| 78 | + }; |
| 79 | + } |
| 80 | + } |
| 81 | + }, |
| 82 | +}); |
| 83 | + |
| 84 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 85 | +const config = { |
| 86 | + create, |
| 87 | + meta: { |
| 88 | + type: 'problem', |
| 89 | + docs: { |
| 90 | + description: 'Enforce consistent assertion style with `node:assert`.', |
| 91 | + recommended: true, |
| 92 | + }, |
| 93 | + fixable: 'code', |
| 94 | + messages, |
| 95 | + }, |
| 96 | +}; |
| 97 | + |
| 98 | +export default config; |
0 commit comments