no-debug.js
6.85 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"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 = 'no-debug';
function isRenderVariableDeclarator(node, renderFunctions) {
if (node.init) {
if (node_utils_1.isAwaitExpression(node.init)) {
return (node.init.argument &&
node_utils_1.isRenderFunction(node.init.argument, __spreadArrays([
'render'
], renderFunctions)));
}
else {
return (node_utils_1.isCallExpression(node.init) &&
node_utils_1.isRenderFunction(node.init, __spreadArrays(['render'], renderFunctions)));
}
}
return false;
}
function hasTestingLibraryImportModule(importDeclarationNode) {
var literal = importDeclarationNode.source;
return utils_1.LIBRARY_MODULES.some(function (module) { return module === literal.value; });
}
exports.default = experimental_utils_1.ESLintUtils.RuleCreator(utils_1.getDocsUrl)({
name: exports.RULE_NAME,
meta: {
type: 'problem',
docs: {
description: 'Disallow unnecessary debug usages in the tests',
category: 'Best Practices',
recommended: 'warn',
},
messages: {
noDebug: 'Unexpected debug statement',
},
fixable: null,
schema: [
{
type: 'object',
properties: {
renderFunctions: {
type: 'array',
},
},
},
],
},
defaultOptions: [
{
renderFunctions: [],
},
],
create: function (context, _a) {
var _b;
var options = _a[0];
var hasDestructuredDebugStatement = false;
var renderVariableDeclarators = [];
var renderFunctions = options.renderFunctions;
var hasImportedScreen = false;
var wildcardImportName = null;
return _b = {
VariableDeclarator: function (node) {
if (isRenderVariableDeclarator(node, renderFunctions)) {
if (node_utils_1.isObjectPattern(node.id) &&
node.id.properties.some(function (property) {
return node_utils_1.isProperty(property) &&
node_utils_1.isIdentifier(property.key) &&
property.key.name === 'debug';
})) {
hasDestructuredDebugStatement = true;
}
if (node.id.type === 'Identifier') {
renderVariableDeclarators.push(node);
}
}
}
},
_b["VariableDeclarator > CallExpression > Identifier[name=\"require\"]"] = function (node) {
var args = node.parent.arguments;
var literalNodeScreenModuleName = args.find(function (args) {
return node_utils_1.isLiteral(args) &&
typeof args.value === 'string' &&
utils_1.LIBRARY_MODULES.includes(args.value);
});
if (!literalNodeScreenModuleName) {
return;
}
var declaratorNode = node.parent
.parent;
hasImportedScreen =
node_utils_1.isObjectPattern(declaratorNode.id) &&
declaratorNode.id.properties.some(function (property) {
return node_utils_1.isProperty(property) &&
node_utils_1.isIdentifier(property.key) &&
property.key.name === 'screen';
});
},
_b.ImportDeclaration = function (node) {
if (!hasTestingLibraryImportModule(node))
return;
hasImportedScreen = node.specifiers.some(function (s) { return node_utils_1.isImportSpecifier(s) && s.imported.name === 'screen'; });
},
_b['ImportDeclaration ImportNamespaceSpecifier'] = function (node) {
var importDeclarationNode = node.parent;
if (!hasTestingLibraryImportModule(importDeclarationNode))
return;
wildcardImportName = node.local && node.local.name;
},
_b["CallExpression > Identifier[name=\"debug\"]"] = function (node) {
if (hasDestructuredDebugStatement) {
context.report({
node: node,
messageId: 'noDebug',
});
}
},
_b["CallExpression > MemberExpression > Identifier[name=\"debug\"]"] = function (node) {
var memberExpression = node.parent;
var identifier = memberExpression.object;
var memberExpressionName = identifier.name;
var isScreenDebugUsed = hasImportedScreen && memberExpressionName === 'screen';
var isNamespaceDebugUsed = wildcardImportName && memberExpressionName === wildcardImportName;
if (isScreenDebugUsed || isNamespaceDebugUsed) {
context.report({
node: node,
messageId: 'noDebug',
});
}
},
_b['Program:exit'] = function () {
renderVariableDeclarators.forEach(function (renderVar) {
var renderVarReferences = context
.getDeclaredVariables(renderVar)[0]
.references.slice(1);
renderVarReferences.forEach(function (ref) {
var parent = ref.identifier.parent;
if (node_utils_1.isMemberExpression(parent) &&
node_utils_1.isIdentifier(parent.property) &&
parent.property.name === 'debug' &&
node_utils_1.isCallExpression(parent.parent)) {
context.report({
node: parent.property,
messageId: 'noDebug',
});
}
});
});
},
_b;
},
});