UNPKG

eslint-plugin-ava

Version:
72 lines (61 loc) 1.69 kB
import path from 'node:path'; import util from '../util.js'; const MESSAGE_ID = 'use-test'; function report(context, node) { context.report({ node, messageId: MESSAGE_ID, }); } const create = context => { const extension = path.extname(context.filename); const isTypeScript = ['.ts', '.tsx', '.mts', '.cts'].includes(extension); return { 'ImportDeclaration[importKind!="type"]'(node) { if (node.source.value !== 'ava') { return; } // Side-effect import: `import 'ava'` if (node.specifiers.length === 0) { report(context, node); return; } // Skip inline type imports: `import {type Foo} from 'ava'` if (node.specifiers.every(specifier => specifier.importKind === 'type')) { return; } // Only default imports are accepted const defaultSpecifier = node.specifiers.find(specifier => specifier.type === 'ImportDefaultSpecifier'); if (!defaultSpecifier) { report(context, node); return; } const {name} = defaultSpecifier.local; if (name !== 'test' && (!isTypeScript || name !== 'anyTest')) { report(context, node); return; } const hasRenamedNamedImport = node.specifiers.some(specifier => specifier.type === 'ImportSpecifier' && specifier.importKind !== 'type' && specifier.imported.name !== specifier.local.name); if (hasRenamedNamedImport) { report(context, node); } }, }; }; export default { create, meta: { type: 'suggestion', docs: { description: 'Require AVA to be imported as `test`.', recommended: true, url: util.getDocsUrl(import.meta.filename), }, schema: [], messages: { [MESSAGE_ID]: 'AVA should be imported as `test`.', }, }, };