UNPKG

eslint-plugin-ava

Version:
104 lines (88 loc) • 2.15 kB
import fs from 'node:fs'; import path from 'node:path'; import util from '../util.js'; const MESSAGE_ID = 'no-import-test-files'; // Assume absolute paths can be classified by AVA. const isFileImport = name => path.isAbsolute(name) || name.startsWith('./') || name.startsWith('../'); const create = context => { const {filename} = context; const [overrides] = context.options; if (filename === '<input>' || filename === '<text>') { return {}; } const resolveFrom = path.dirname(filename); let isAvaHelperLoaded = false; let avaHelper; const validateImportPath = (node, importPath) => { if (!importPath || typeof importPath !== 'string') { return; } if (!isFileImport(importPath)) { return; } if (!isAvaHelperLoaded) { avaHelper = util.loadAvaHelper(filename, overrides); isAvaHelperLoaded = true; } if (!avaHelper) { return {}; } let resolvedPath = path.resolve(resolveFrom, importPath); try { if (fs.statSync(resolvedPath).isDirectory()) { resolvedPath = path.join(resolvedPath, 'index'); } } catch {} const {isTest} = avaHelper.classifyImport(resolvedPath); if (isTest) { context.report({ node, messageId: MESSAGE_ID, }); } }; return { ImportDeclaration(node) { validateImportPath(node, node.source.value); }, ExportAllDeclaration(node) { validateImportPath(node, node.source?.value); }, ExportNamedDeclaration(node) { validateImportPath(node, node.source?.value); }, }; }; const schema = [{ type: 'object', properties: { extensions: { description: 'File extensions recognized as test files.', type: 'array', items: {type: 'string'}, }, files: { description: 'Glob patterns to match test files.', type: 'array', items: {type: 'string'}, }, }, additionalProperties: false, }]; export default { create, meta: { type: 'suggestion', docs: { description: 'Disallow importing test files.', recommended: true, url: util.getDocsUrl(import.meta.filename), }, schema, defaultOptions: [{}], messages: { [MESSAGE_ID]: 'Test files should not be imported.', }, languages: ['js/js'], }, };