eslint-plugin-no-mistakes 0.61.7 → 0.61.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const { unwrapExpression } = require("./async-ast");
|
|
4
4
|
const { compileTargets, matchingTargets } = require("./async-patterns");
|
|
5
|
+
const { isDefinitelyUndefinedValue } = require("./imported-call-undefined");
|
|
5
6
|
const { propertyName, literalString } = require("./module-mock-helpers");
|
|
6
7
|
|
|
7
8
|
const importedCallOptionsSchema = [
|
|
@@ -65,20 +66,32 @@ function staticPropertyName(property) {
|
|
|
65
66
|
return propertyName(property.key);
|
|
66
67
|
}
|
|
67
68
|
|
|
68
|
-
function
|
|
69
|
-
const
|
|
70
|
-
if (node?.type !== "ObjectExpression") return
|
|
69
|
+
function visiblePropertyValues(node) {
|
|
70
|
+
const values = new Map();
|
|
71
|
+
if (node?.type !== "ObjectExpression") return values;
|
|
71
72
|
for (const property of node.properties) {
|
|
72
73
|
const name = staticPropertyName(property);
|
|
73
|
-
if (name)
|
|
74
|
+
if (name) values.set(name, property.value);
|
|
75
|
+
}
|
|
76
|
+
return values;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function visiblePropertyNames(node) {
|
|
80
|
+
return new Set(visiblePropertyValues(node).keys());
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function presentPropertyNames(node, sourceCode) {
|
|
84
|
+
const names = new Set();
|
|
85
|
+
for (const [name, value] of visiblePropertyValues(node)) {
|
|
86
|
+
if (!isDefinitelyUndefinedValue(value, sourceCode)) names.add(name);
|
|
74
87
|
}
|
|
75
88
|
return names;
|
|
76
89
|
}
|
|
77
90
|
|
|
78
|
-
function hasRequiredOptions(argument, target) {
|
|
91
|
+
function hasRequiredOptions(argument, target, sourceCode) {
|
|
79
92
|
const object = unwrapExpression(argument);
|
|
80
93
|
if (object?.type !== "ObjectExpression") return false;
|
|
81
|
-
const names =
|
|
94
|
+
const names = presentPropertyNames(object, sourceCode);
|
|
82
95
|
if (target.propertyMatch === "all") {
|
|
83
96
|
return target.requiredProperties.every((name) => names.has(name));
|
|
84
97
|
}
|
|
@@ -89,6 +102,7 @@ module.exports = {
|
|
|
89
102
|
compileImportedCallTargets,
|
|
90
103
|
hasRequiredOptions,
|
|
91
104
|
importedCallOptionsSchema,
|
|
105
|
+
isDefinitelyUndefinedValue,
|
|
92
106
|
matchingTargets,
|
|
93
107
|
staticPropertyName,
|
|
94
108
|
visiblePropertyNames,
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { unwrapExpression } = require("./async-ast");
|
|
4
|
+
|
|
5
|
+
function findVariable(sourceCode, identifier) {
|
|
6
|
+
if (!sourceCode?.getScope) return null;
|
|
7
|
+
let scope = sourceCode.getScope(identifier);
|
|
8
|
+
while (scope) {
|
|
9
|
+
const variable = scope.variables.find((candidate) => candidate.name === identifier.name);
|
|
10
|
+
if (variable) return variable;
|
|
11
|
+
scope = scope.upper;
|
|
12
|
+
}
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function isShadowedUndefined(identifier, sourceCode) {
|
|
17
|
+
const variable = findVariable(sourceCode, identifier);
|
|
18
|
+
return Boolean(variable?.defs.some((def) => def.type && def.type !== "ImplicitGlobalVariable"));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function isDefinitelyUndefinedLiteral(node, sourceCode) {
|
|
22
|
+
const current = unwrapExpression(node);
|
|
23
|
+
if (!current) return false;
|
|
24
|
+
if (current.type === "UnaryExpression" && current.operator === "void") return true;
|
|
25
|
+
if (current.type === "Identifier" && current.name === "undefined") {
|
|
26
|
+
return !isShadowedUndefined(current, sourceCode);
|
|
27
|
+
}
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function hasLaterWrite(variable) {
|
|
32
|
+
return variable.references.some((reference) => reference.isWrite() && !reference.init);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function isDefinitelyUndefinedBinding(variable, sourceCode, seen) {
|
|
36
|
+
if (variable.defs.length !== 1) return false;
|
|
37
|
+
const def = variable.defs[0];
|
|
38
|
+
if (def.type !== "Variable" || !def.node?.init || hasLaterWrite(variable)) return false;
|
|
39
|
+
return isDefinitelyUndefinedValue(def.node.init, sourceCode, seen);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function isDefinitelyUndefinedValue(node, sourceCode, seen = new Set()) {
|
|
43
|
+
if (isDefinitelyUndefinedLiteral(node, sourceCode)) return true;
|
|
44
|
+
const current = unwrapExpression(node);
|
|
45
|
+
if (current?.type !== "Identifier") return false;
|
|
46
|
+
const variable = findVariable(sourceCode, current);
|
|
47
|
+
if (!variable || seen.has(variable)) return false;
|
|
48
|
+
seen.add(variable);
|
|
49
|
+
return isDefinitelyUndefinedBinding(variable, sourceCode, seen);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
module.exports = {
|
|
53
|
+
isDefinitelyUndefinedValue,
|
|
54
|
+
};
|
|
@@ -6,6 +6,7 @@ const {
|
|
|
6
6
|
compileImportedCallTargets,
|
|
7
7
|
hasRequiredOptions,
|
|
8
8
|
importedCallOptionsSchema,
|
|
9
|
+
isDefinitelyUndefinedValue,
|
|
9
10
|
matchingTargets,
|
|
10
11
|
staticPropertyName,
|
|
11
12
|
visiblePropertyNames,
|
|
@@ -41,7 +42,15 @@ module.exports = Object.assign(
|
|
|
41
42
|
resolved.source,
|
|
42
43
|
resolved.calleeName,
|
|
43
44
|
)) {
|
|
44
|
-
if (
|
|
45
|
+
if (
|
|
46
|
+
hasRequiredOptions(
|
|
47
|
+
node.arguments[target.optionsPosition - 1],
|
|
48
|
+
target,
|
|
49
|
+
context.sourceCode,
|
|
50
|
+
)
|
|
51
|
+
) {
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
45
54
|
context.report({
|
|
46
55
|
node,
|
|
47
56
|
messageId: "missingOptions",
|
|
@@ -62,6 +71,7 @@ module.exports = Object.assign(
|
|
|
62
71
|
__test: {
|
|
63
72
|
compileImportedCallTargets,
|
|
64
73
|
hasRequiredOptions,
|
|
74
|
+
isDefinitelyUndefinedValue,
|
|
65
75
|
staticPropertyName,
|
|
66
76
|
visiblePropertyNames,
|
|
67
77
|
},
|