| [9af201e] | 1 | "use strict";
|
|---|
| 2 | Object.defineProperty(exports, "__esModule", { value: true });
|
|---|
| 3 | exports.RULE_NAME = void 0;
|
|---|
| 4 | const create_testing_library_rule_1 = require("../create-testing-library-rule");
|
|---|
| 5 | const node_utils_1 = require("../node-utils");
|
|---|
| 6 | exports.RULE_NAME = 'no-dom-import';
|
|---|
| 7 | const DOM_TESTING_LIBRARY_MODULES = [
|
|---|
| 8 | 'dom-testing-library',
|
|---|
| 9 | '@testing-library/dom',
|
|---|
| 10 | ];
|
|---|
| 11 | const CORRECT_MODULE_NAME_BY_FRAMEWORK = {
|
|---|
| 12 | angular: '@testing-library/angular',
|
|---|
| 13 | marko: '@marko/testing-library',
|
|---|
| 14 | };
|
|---|
| 15 | const getCorrectModuleName = (moduleName, framework) => {
|
|---|
| 16 | var _a;
|
|---|
| 17 | return ((_a = CORRECT_MODULE_NAME_BY_FRAMEWORK[framework]) !== null && _a !== void 0 ? _a : moduleName.replace('dom', framework));
|
|---|
| 18 | };
|
|---|
| 19 | exports.default = (0, create_testing_library_rule_1.createTestingLibraryRule)({
|
|---|
| 20 | name: exports.RULE_NAME,
|
|---|
| 21 | meta: {
|
|---|
| 22 | type: 'problem',
|
|---|
| 23 | docs: {
|
|---|
| 24 | description: 'Disallow importing from DOM Testing Library',
|
|---|
| 25 | recommendedConfig: {
|
|---|
| 26 | dom: false,
|
|---|
| 27 | angular: ['error', 'angular'],
|
|---|
| 28 | react: ['error', 'react'],
|
|---|
| 29 | vue: ['error', 'vue'],
|
|---|
| 30 | marko: ['error', 'marko'],
|
|---|
| 31 | },
|
|---|
| 32 | },
|
|---|
| 33 | messages: {
|
|---|
| 34 | noDomImport: 'import from DOM Testing Library is restricted, import from corresponding Testing Library framework instead',
|
|---|
| 35 | noDomImportFramework: 'import from DOM Testing Library is restricted, import from {{module}} instead',
|
|---|
| 36 | },
|
|---|
| 37 | fixable: 'code',
|
|---|
| 38 | schema: [{ type: 'string' }],
|
|---|
| 39 | },
|
|---|
| 40 | defaultOptions: [''],
|
|---|
| 41 | create(context, [framework], helpers) {
|
|---|
| 42 | function report(node, moduleName) {
|
|---|
| 43 | if (!framework) {
|
|---|
| 44 | return context.report({
|
|---|
| 45 | node,
|
|---|
| 46 | messageId: 'noDomImport',
|
|---|
| 47 | });
|
|---|
| 48 | }
|
|---|
| 49 | const correctModuleName = getCorrectModuleName(moduleName, framework);
|
|---|
| 50 | context.report({
|
|---|
| 51 | data: { module: correctModuleName },
|
|---|
| 52 | fix(fixer) {
|
|---|
| 53 | if ((0, node_utils_1.isCallExpression)(node)) {
|
|---|
| 54 | const name = node.arguments[0];
|
|---|
| 55 | return fixer.replaceText(name, name.raw.replace(moduleName, correctModuleName));
|
|---|
| 56 | }
|
|---|
| 57 | else {
|
|---|
| 58 | const name = node.source;
|
|---|
| 59 | return fixer.replaceText(name, name.raw.replace(moduleName, correctModuleName));
|
|---|
| 60 | }
|
|---|
| 61 | },
|
|---|
| 62 | messageId: 'noDomImportFramework',
|
|---|
| 63 | node,
|
|---|
| 64 | });
|
|---|
| 65 | }
|
|---|
| 66 | return {
|
|---|
| 67 | 'Program:exit'() {
|
|---|
| 68 | let importName;
|
|---|
| 69 | const allImportNodes = helpers.getAllTestingLibraryImportNodes();
|
|---|
| 70 | allImportNodes.forEach((importNode) => {
|
|---|
| 71 | importName = (0, node_utils_1.getImportModuleName)(importNode);
|
|---|
| 72 | const domModuleName = DOM_TESTING_LIBRARY_MODULES.find((module) => module === importName);
|
|---|
| 73 | if (!domModuleName) {
|
|---|
| 74 | return;
|
|---|
| 75 | }
|
|---|
| 76 | report(importNode, domModuleName);
|
|---|
| 77 | });
|
|---|
| 78 | },
|
|---|
| 79 | };
|
|---|
| 80 | },
|
|---|
| 81 | });
|
|---|