prefer-wait-for.js
5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"use strict";
var __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RULE_NAME = void 0;
var experimental_utils_1 = require("@typescript-eslint/experimental-utils");
var utils_1 = require("../utils");
var node_utils_1 = require("../node-utils");
exports.RULE_NAME = 'prefer-wait-for';
var DEPRECATED_METHODS = ['wait', 'waitForElement', 'waitForDomChange'];
exports.default = experimental_utils_1.ESLintUtils.RuleCreator(utils_1.getDocsUrl)({
name: exports.RULE_NAME,
meta: {
type: 'suggestion',
docs: {
description: 'Use `waitFor` instead of deprecated wait methods',
category: 'Best Practices',
recommended: false,
},
messages: {
preferWaitForMethod: '`{{ methodName }}` is deprecated in favour of `waitFor`',
preferWaitForImport: 'import `waitFor` instead of deprecated async utils',
},
fixable: 'code',
schema: [],
},
defaultOptions: [],
create: function (context) {
var reportImport = function (node) {
context.report({
node: node,
messageId: 'preferWaitForImport',
fix: function (fixer) {
var excludedImports = __spreadArrays(DEPRECATED_METHODS, ['waitFor']);
var newImports = node.specifiers
.filter(function (specifier) {
return node_utils_1.isImportSpecifier(specifier) &&
!excludedImports.includes(specifier.imported.name);
})
.map(function (specifier) { return specifier.imported.name; });
newImports.push('waitFor');
var newNode = "import { " + newImports.join(',') + " } from '" + node.source.value + "';";
return fixer.replaceText(node, newNode);
},
});
};
var reportWait = function (node) {
context.report({
node: node,
messageId: 'preferWaitForMethod',
data: {
methodName: node.name,
},
fix: function (fixer) {
var callExpressionNode = node_utils_1.findClosestCallExpressionNode(node);
var arg = callExpressionNode.arguments[0];
var fixers = [];
if (arg) {
fixers.push(fixer.replaceText(node, 'waitFor'));
if (node.name === 'waitForDomChange') {
fixers.push(fixer.insertTextBefore(arg, '() => {}, '));
}
}
else {
var methodReplacement = 'waitFor(() => {})';
if (node_utils_1.isMemberExpression(node.parent) &&
node_utils_1.isIdentifier(node.parent.object)) {
methodReplacement = node.parent.object.name + "." + methodReplacement;
}
var newText = methodReplacement;
fixers.push(fixer.replaceText(callExpressionNode, newText));
}
return fixers;
},
});
};
return {
'ImportDeclaration[source.value=/testing-library/]': function (node) {
var deprecatedImportSpecifiers = node.specifiers.filter(function (specifier) {
return node_utils_1.isImportSpecifier(specifier) &&
specifier.imported &&
DEPRECATED_METHODS.includes(specifier.imported.name);
});
deprecatedImportSpecifiers.forEach(function (importSpecifier, i) {
if (i === 0) {
reportImport(node);
}
context
.getDeclaredVariables(importSpecifier)
.forEach(function (variable) {
return variable.references.forEach(function (reference) {
return reportWait(reference.identifier);
});
});
});
},
'ImportDeclaration[source.value=/testing-library/] > ImportNamespaceSpecifier': function (node) {
context.getDeclaredVariables(node).forEach(function (variable) {
return variable.references.forEach(function (reference) {
if (node_utils_1.isMemberExpression(reference.identifier.parent) &&
node_utils_1.isIdentifier(reference.identifier.parent.property) &&
DEPRECATED_METHODS.includes(reference.identifier.parent.property.name)) {
reportWait(reference.identifier.parent.property);
}
});
});
},
};
},
});