eslint-plugin-no-mistakes 0.91.2 → 0.92.0
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/README.md +16 -0
- package/package.json +1 -1
- package/src/index.js +4 -0
- package/src/rule-suppression.js +11 -1
- package/src/rules/argv-main-values.js +114 -0
- package/src/rules/forbidden-member-chains-destructure.js +74 -0
- package/src/rules/forbidden-member-chains-match.js +120 -0
- package/src/rules/forbidden-member-chains-schema.js +104 -0
- package/src/rules/forbidden-member-chains.js +49 -0
- package/src/rules/gh-api-argument-safety.js +125 -0
- package/src/rules/gh-api-argv.js +101 -0
- package/src/rules/imported-call-options.js +70 -20
- package/src/rules/no-argv-main-guard.js +59 -0
- package/src/rules/no-comment-phrases.js +113 -0
- package/src/rules/require-options-on-imported-call.js +63 -30
- package/src/rules/required-call-selector-compile.js +99 -0
- package/src/rules/required-call-selectors.js +158 -0
- package/src/rules/test-no-skips-bindings.js +43 -4
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const { unwrapExpression } = require("./async-ast");
|
|
3
|
+
|
|
4
|
+
// Unknown template substitutions are separators, never invented flag bytes.
|
|
5
|
+
function token(node) {
|
|
6
|
+
node = unwrapExpression(node);
|
|
7
|
+
if (node?.type === "Literal" && typeof node.value === "string") return node.value;
|
|
8
|
+
if (node?.type === "TemplateLiteral") {
|
|
9
|
+
return node.quasis.map((part) => part.value.cooked ?? part.value.raw).join("\0");
|
|
10
|
+
}
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
const fields = /^(?:-[fF].+|--(?:field|raw-field)=.*)$/;
|
|
14
|
+
const fieldFlags = new Set(["-f", "-F", "--field", "--raw-field"]);
|
|
15
|
+
const valueFlags = new Set([
|
|
16
|
+
"-X",
|
|
17
|
+
"--method",
|
|
18
|
+
"-f",
|
|
19
|
+
"-F",
|
|
20
|
+
"--field",
|
|
21
|
+
"--raw-field",
|
|
22
|
+
"-q",
|
|
23
|
+
"--jq",
|
|
24
|
+
"-t",
|
|
25
|
+
"--template",
|
|
26
|
+
"-H",
|
|
27
|
+
"--header",
|
|
28
|
+
"--hostname",
|
|
29
|
+
"--input",
|
|
30
|
+
"--preview",
|
|
31
|
+
"--cache",
|
|
32
|
+
]);
|
|
33
|
+
const switches = new Set([
|
|
34
|
+
"--paginate",
|
|
35
|
+
"--slurp",
|
|
36
|
+
"--silent",
|
|
37
|
+
"--verbose",
|
|
38
|
+
"--include",
|
|
39
|
+
"-i",
|
|
40
|
+
"--help",
|
|
41
|
+
"--exit-status",
|
|
42
|
+
]);
|
|
43
|
+
function method(value) {
|
|
44
|
+
return typeof value === "string" && /^[A-Za-z]+$/.test(value);
|
|
45
|
+
}
|
|
46
|
+
function inspectArgv(array, includesApi) {
|
|
47
|
+
if (array?.type !== "ArrayExpression") return null;
|
|
48
|
+
const args = array.elements;
|
|
49
|
+
let start = 0;
|
|
50
|
+
if (includesApi) {
|
|
51
|
+
if (token(args[0]) !== "api") return null;
|
|
52
|
+
start = 1;
|
|
53
|
+
}
|
|
54
|
+
let endpoint = null;
|
|
55
|
+
let hasFields = false;
|
|
56
|
+
let hasMethod = false;
|
|
57
|
+
let uncertain = false;
|
|
58
|
+
for (let i = start; i < args.length; i += 1) {
|
|
59
|
+
const text = token(args[i]);
|
|
60
|
+
if (text === null) {
|
|
61
|
+
uncertain = true;
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
if (text.startsWith("-") && text.includes("\0")) uncertain = true;
|
|
65
|
+
if (fieldFlags.has(text)) hasFields = true;
|
|
66
|
+
if (fields.test(text)) hasFields = true;
|
|
67
|
+
if (text === "-X" || text === "--method") hasMethod = method(token(args[i + 1]));
|
|
68
|
+
if (text.startsWith("-X") && text !== "-X") hasMethod = method(text.slice(2));
|
|
69
|
+
if (text.startsWith("--method=")) hasMethod = method(text.slice(9));
|
|
70
|
+
if (valueFlags.has(text)) {
|
|
71
|
+
if (token(args[i + 1]) === null) uncertain = true;
|
|
72
|
+
const next = token(args[i + 1]);
|
|
73
|
+
if (!(text === "-X" || text === "--method") || !next?.startsWith("-")) i += 1;
|
|
74
|
+
} else if (
|
|
75
|
+
switches.has(text) ||
|
|
76
|
+
/^(?:-[fFXqHt].+)/.test(text) ||
|
|
77
|
+
(text.includes("=") && valueFlags.has(text.split("=", 1)[0]))
|
|
78
|
+
) {
|
|
79
|
+
continue;
|
|
80
|
+
} else if (text.startsWith("-")) {
|
|
81
|
+
uncertain = true;
|
|
82
|
+
} else if (endpoint === null) {
|
|
83
|
+
if (uncertain) return null;
|
|
84
|
+
endpoint = { node: args[i], text };
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (!endpoint) return null;
|
|
88
|
+
// A fixed leading REST prefix plus a fixed slash cannot become a flag or
|
|
89
|
+
// the exact GraphQL endpoint, even when another segment is interpolated.
|
|
90
|
+
const knownRest = !endpoint.text.startsWith("\0") && endpoint.text.includes("/");
|
|
91
|
+
return {
|
|
92
|
+
endpoint: /\/[^?]*\?/.test(endpoint.text) ? endpoint.node : null,
|
|
93
|
+
missingMethod:
|
|
94
|
+
hasFields &&
|
|
95
|
+
!hasMethod &&
|
|
96
|
+
!uncertain &&
|
|
97
|
+
(!endpoint.text.includes("\0") || knownRest) &&
|
|
98
|
+
endpoint.text !== "graphql",
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
module.exports = { inspectArgv, token };
|
|
@@ -5,43 +5,88 @@ const { compileTargets, matchingTargets } = require("./async-patterns");
|
|
|
5
5
|
const { isDefinitelyUndefinedValue } = require("./imported-call-undefined");
|
|
6
6
|
const { propertyName, literalString } = require("./module-mock-helpers");
|
|
7
7
|
|
|
8
|
+
const pathPatternList = { type: "array", items: { type: "string" } };
|
|
9
|
+
// Slash-wrapped values are regexes. An unclosed class such as `/[/` must fail
|
|
10
|
+
// here, before the rule can drop the target and match nothing.
|
|
11
|
+
const selectorPattern = {
|
|
12
|
+
type: "string",
|
|
13
|
+
minLength: 1,
|
|
14
|
+
pattern: "^(?:[^/].*|/(?:[^/\\\\\\[\\]]|\\\\.|\\[[^\\]\\\\]*(?:\\\\.[^\\]\\\\]*)*\\])*/)$",
|
|
15
|
+
};
|
|
16
|
+
const selectorPatterns = { type: "array", minItems: 1, items: selectorPattern };
|
|
17
|
+
const targetOptions = {
|
|
18
|
+
optionsPosition: { type: "integer", minimum: 1 },
|
|
19
|
+
requiredProperties: { type: "array", items: { type: "string" }, minItems: 1 },
|
|
20
|
+
propertyMatch: { type: "string", enum: ["any", "all"] },
|
|
21
|
+
message: { type: "string", minLength: 1 },
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
function targetKind(required, properties) {
|
|
25
|
+
return {
|
|
26
|
+
type: "object",
|
|
27
|
+
additionalProperties: false,
|
|
28
|
+
required: [...required, "optionsPosition", "requiredProperties"],
|
|
29
|
+
properties: { ...properties, ...targetOptions },
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
8
33
|
const importedCallOptionsSchema = [
|
|
9
34
|
{
|
|
10
35
|
type: "object",
|
|
36
|
+
additionalProperties: false,
|
|
11
37
|
properties: {
|
|
12
38
|
targets: {
|
|
13
39
|
type: "array",
|
|
14
40
|
items: {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
"calleeNamePatterns",
|
|
26
|
-
"optionsPosition",
|
|
27
|
-
"requiredProperties",
|
|
41
|
+
oneOf: [
|
|
42
|
+
targetKind(["sourceSpecifierPatterns", "calleeNamePatterns"], {
|
|
43
|
+
sourceSpecifierPatterns: selectorPatterns,
|
|
44
|
+
calleeNamePatterns: selectorPatterns,
|
|
45
|
+
}),
|
|
46
|
+
targetKind(["globalNamePatterns"], { globalNamePatterns: selectorPatterns }),
|
|
47
|
+
targetKind(["receiverPatterns", "calleeNamePatterns"], {
|
|
48
|
+
receiverPatterns: selectorPatterns,
|
|
49
|
+
calleeNamePatterns: selectorPatterns,
|
|
50
|
+
}),
|
|
28
51
|
],
|
|
29
|
-
additionalProperties: false,
|
|
30
52
|
},
|
|
31
53
|
},
|
|
54
|
+
message: { type: "string", minLength: 1 },
|
|
55
|
+
checkedPathPatterns: pathPatternList,
|
|
56
|
+
allowedPathPatterns: pathPatternList,
|
|
32
57
|
},
|
|
33
|
-
additionalProperties: false,
|
|
34
58
|
},
|
|
35
59
|
];
|
|
36
60
|
|
|
61
|
+
function normalizeRequiredProperties(value) {
|
|
62
|
+
return Array.isArray(value)
|
|
63
|
+
? [...new Set(value.filter((name) => typeof name === "string" && name))]
|
|
64
|
+
: [];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Slash-wrapped patterns are regexes. `/(/` and `/*/` pass the schema, and
|
|
68
|
+
// `compileTargets` would drop them so the rule loads and checks nothing.
|
|
69
|
+
function assertCompilableSelectorPatterns(patterns) {
|
|
70
|
+
if (!Array.isArray(patterns)) return;
|
|
71
|
+
for (const pattern of patterns) {
|
|
72
|
+
if (typeof pattern !== "string" || pattern.length < 3) continue;
|
|
73
|
+
if (!pattern.startsWith("/") || !pattern.endsWith("/")) continue;
|
|
74
|
+
try {
|
|
75
|
+
RegExp(pattern.slice(1, -1));
|
|
76
|
+
} catch (error) {
|
|
77
|
+
throw new Error(`Invalid selector pattern ${pattern}: ${error.message}`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
37
82
|
function compileImportedCallTargets(options) {
|
|
38
83
|
const compiled = [];
|
|
39
84
|
for (const target of options.targets || []) {
|
|
85
|
+
assertCompilableSelectorPatterns(target.sourceSpecifierPatterns);
|
|
86
|
+
assertCompilableSelectorPatterns(target.calleeNamePatterns);
|
|
40
87
|
const [matched] = compileTargets({ targets: [target] }, "targets");
|
|
41
88
|
const optionsPosition = target.optionsPosition;
|
|
42
|
-
const requiredProperties =
|
|
43
|
-
? [...new Set(target.requiredProperties.filter((name) => typeof name === "string" && name))]
|
|
44
|
-
: [];
|
|
89
|
+
const requiredProperties = normalizeRequiredProperties(target.requiredProperties);
|
|
45
90
|
if (
|
|
46
91
|
!matched ||
|
|
47
92
|
!Number.isInteger(optionsPosition) ||
|
|
@@ -50,12 +95,15 @@ function compileImportedCallTargets(options) {
|
|
|
50
95
|
) {
|
|
51
96
|
continue;
|
|
52
97
|
}
|
|
53
|
-
|
|
98
|
+
const next = {
|
|
54
99
|
...matched,
|
|
55
100
|
optionsPosition,
|
|
56
101
|
requiredProperties,
|
|
57
102
|
propertyMatch: target.propertyMatch === "all" ? "all" : "any",
|
|
58
|
-
}
|
|
103
|
+
};
|
|
104
|
+
// Per-target text stays off the object unless set, so older compiled shapes stay stable.
|
|
105
|
+
if (typeof target.message === "string" && target.message) next.message = target.message;
|
|
106
|
+
compiled.push(next);
|
|
59
107
|
}
|
|
60
108
|
return compiled;
|
|
61
109
|
}
|
|
@@ -66,6 +114,7 @@ function staticPropertyName(property) {
|
|
|
66
114
|
return propertyName(property.key);
|
|
67
115
|
}
|
|
68
116
|
|
|
117
|
+
// Later static keys replace earlier ones. A spread neither adds nor erases a key.
|
|
69
118
|
function visiblePropertyValues(node) {
|
|
70
119
|
const values = new Map();
|
|
71
120
|
if (node?.type !== "ObjectExpression") return values;
|
|
@@ -104,6 +153,7 @@ module.exports = {
|
|
|
104
153
|
importedCallOptionsSchema,
|
|
105
154
|
isDefinitelyUndefinedValue,
|
|
106
155
|
matchingTargets,
|
|
156
|
+
normalizeRequiredProperties,
|
|
107
157
|
staticPropertyName,
|
|
108
158
|
visiblePropertyNames,
|
|
109
159
|
};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const { createTargetMatcher } = require("./async-targets");
|
|
3
|
+
const { ruleSuppression } = require("../rule-suppression");
|
|
4
|
+
const { createClassifier, ARGV, MAIN } = require("./argv-main-values");
|
|
5
|
+
module.exports = {
|
|
6
|
+
meta: {
|
|
7
|
+
type: "problem",
|
|
8
|
+
docs: {
|
|
9
|
+
description: "avoid argv-based main-module comparisons that fail through symlinks",
|
|
10
|
+
recommended: false,
|
|
11
|
+
},
|
|
12
|
+
schema: [
|
|
13
|
+
{
|
|
14
|
+
type: "object",
|
|
15
|
+
properties: { message: { type: "string", minLength: 1 } },
|
|
16
|
+
additionalProperties: false,
|
|
17
|
+
},
|
|
18
|
+
],
|
|
19
|
+
messages: { argvMain: "{{message}}" },
|
|
20
|
+
},
|
|
21
|
+
create(context) {
|
|
22
|
+
const matcherContext = Object.create(context);
|
|
23
|
+
Object.defineProperty(matcherContext, "options", {
|
|
24
|
+
value: [
|
|
25
|
+
{
|
|
26
|
+
targets: [
|
|
27
|
+
{
|
|
28
|
+
sourceSpecifierPatterns: ["node:url", "url"],
|
|
29
|
+
calleeNamePatterns: ["fileURLToPath", "pathToFileURL"],
|
|
30
|
+
},
|
|
31
|
+
{ sourceSpecifierPatterns: ["node:path", "path"], calleeNamePatterns: ["resolve"] },
|
|
32
|
+
{ sourceSpecifierPatterns: ["node:fs", "fs"], calleeNamePatterns: ["realpathSync"] },
|
|
33
|
+
],
|
|
34
|
+
},
|
|
35
|
+
],
|
|
36
|
+
});
|
|
37
|
+
const wrappers = createTargetMatcher(matcherContext);
|
|
38
|
+
const classify = createClassifier(context, wrappers);
|
|
39
|
+
const suppressed = ruleSuppression(context, "no-argv-main-guard");
|
|
40
|
+
return {
|
|
41
|
+
...wrappers.visitors,
|
|
42
|
+
BinaryExpression(node) {
|
|
43
|
+
if (!["===", "!==", "==", "!="].includes(node.operator) || suppressed(node)) return;
|
|
44
|
+
const left = classify(node.left);
|
|
45
|
+
const right = classify(node.right);
|
|
46
|
+
if (!((left === ARGV && right === MAIN) || (left === MAIN && right === ARGV))) return;
|
|
47
|
+
context.report({
|
|
48
|
+
node,
|
|
49
|
+
messageId: "argvMain",
|
|
50
|
+
data: {
|
|
51
|
+
message:
|
|
52
|
+
context.options[0]?.message ||
|
|
53
|
+
"Use import.meta.main or inject isMain instead of comparing argv[1] with the module path; symlinked entry paths can differ.",
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
},
|
|
59
|
+
};
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { ruleSuppression } = require("../rule-suppression");
|
|
4
|
+
|
|
5
|
+
const DEFAULT_MESSAGE =
|
|
6
|
+
"This comment matches a configured phrase. Remove the preserved path, or suppress a genuine external constraint with a nonempty reason.";
|
|
7
|
+
|
|
8
|
+
// Unique members of the JS RegExp flag set, and never both `u` and `v`.
|
|
9
|
+
// Repeats, other characters, and that pair are config errors, not one
|
|
10
|
+
// invalidPattern diagnostic per linted file.
|
|
11
|
+
const FLAG_PATTERN = "^(?!.*(.).*\\1)(?!.*u.*v)(?!.*v.*u)[dgimsuvy]*$";
|
|
12
|
+
|
|
13
|
+
// Compiled once per file application. `g` and `y` keep state on the RegExp, so
|
|
14
|
+
// each comment is tested once from index 0. A zero-width success reports that
|
|
15
|
+
// comment once and cannot loop; sticky patterns match only at the start.
|
|
16
|
+
function compilePatterns(patterns) {
|
|
17
|
+
return patterns.map((entry) => {
|
|
18
|
+
try {
|
|
19
|
+
return {
|
|
20
|
+
regex: new RegExp(entry.pattern, entry.flags ?? ""),
|
|
21
|
+
message: entry.message,
|
|
22
|
+
pattern: entry.pattern,
|
|
23
|
+
};
|
|
24
|
+
} catch (error) {
|
|
25
|
+
return { error, pattern: entry.pattern };
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function matches(regex, value) {
|
|
31
|
+
regex.lastIndex = 0;
|
|
32
|
+
const matched = regex.test(value);
|
|
33
|
+
regex.lastIndex = 0;
|
|
34
|
+
return matched;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
module.exports = {
|
|
38
|
+
meta: {
|
|
39
|
+
type: "problem",
|
|
40
|
+
docs: {
|
|
41
|
+
description: "flag configured phrases in line and block comments",
|
|
42
|
+
recommended: false,
|
|
43
|
+
},
|
|
44
|
+
schema: [
|
|
45
|
+
{
|
|
46
|
+
type: "object",
|
|
47
|
+
properties: {
|
|
48
|
+
patterns: {
|
|
49
|
+
type: "array",
|
|
50
|
+
maxItems: 64,
|
|
51
|
+
items: {
|
|
52
|
+
type: "object",
|
|
53
|
+
properties: {
|
|
54
|
+
pattern: { type: "string", minLength: 1 },
|
|
55
|
+
flags: { type: "string", pattern: FLAG_PATTERN },
|
|
56
|
+
message: { type: "string", minLength: 1 },
|
|
57
|
+
},
|
|
58
|
+
required: ["pattern"],
|
|
59
|
+
additionalProperties: false,
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
message: { type: "string", minLength: 1 },
|
|
63
|
+
requireSuppressionReason: { type: "boolean" },
|
|
64
|
+
},
|
|
65
|
+
additionalProperties: false,
|
|
66
|
+
},
|
|
67
|
+
],
|
|
68
|
+
messages: {
|
|
69
|
+
phrase: "{{message}}",
|
|
70
|
+
invalidPattern: "Invalid comment phrase pattern {{pattern}}: {{detail}}",
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
create(context) {
|
|
74
|
+
const options = context.options[0] || {};
|
|
75
|
+
const compiled = compilePatterns(options.patterns || []);
|
|
76
|
+
const suppressed = ruleSuppression(context, "no-comment-phrases", {
|
|
77
|
+
requireReason: options.requireSuppressionReason === true,
|
|
78
|
+
});
|
|
79
|
+
const fallback = options.message || DEFAULT_MESSAGE;
|
|
80
|
+
return {
|
|
81
|
+
Program(node) {
|
|
82
|
+
const ready = [];
|
|
83
|
+
for (const entry of compiled) {
|
|
84
|
+
if (entry.error) {
|
|
85
|
+
// Program starts after a leading comment in oxlint and at byte 0 in
|
|
86
|
+
// ESLint. Pin the config diagnostic to the first line so they agree.
|
|
87
|
+
context.report({
|
|
88
|
+
node,
|
|
89
|
+
loc: { start: { line: 1, column: 0 }, end: { line: 1, column: 0 } },
|
|
90
|
+
messageId: "invalidPattern",
|
|
91
|
+
data: { pattern: entry.pattern, detail: entry.error.message },
|
|
92
|
+
});
|
|
93
|
+
} else ready.push(entry);
|
|
94
|
+
}
|
|
95
|
+
if (ready.length === 0) return;
|
|
96
|
+
for (const comment of context.sourceCode.getAllComments()) {
|
|
97
|
+
// Shebang comments are neither line nor block comment tokens.
|
|
98
|
+
if (comment.type !== "Line" && comment.type !== "Block") continue;
|
|
99
|
+
if (suppressed(comment)) continue;
|
|
100
|
+
for (const entry of ready) {
|
|
101
|
+
if (!matches(entry.regex, comment.value)) continue;
|
|
102
|
+
context.report({
|
|
103
|
+
node,
|
|
104
|
+
loc: comment.loc,
|
|
105
|
+
messageId: "phrase",
|
|
106
|
+
data: { message: entry.message || fallback },
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
},
|
|
113
|
+
};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
+
const { ruleSuppression } = require("../rule-suppression");
|
|
3
4
|
const { rule } = require("../helpers");
|
|
4
5
|
const { createTargetMatcher } = require("./async-targets");
|
|
5
6
|
const {
|
|
@@ -11,6 +12,32 @@ const {
|
|
|
11
12
|
staticPropertyName,
|
|
12
13
|
visiblePropertyNames,
|
|
13
14
|
} = require("./imported-call-options");
|
|
15
|
+
const { shouldCheckOptionsFile } = require("./required-call-selector-compile");
|
|
16
|
+
const { createCallSelectors } = require("./required-call-selectors");
|
|
17
|
+
|
|
18
|
+
function reportMissing(context, node, target, resolved, suppressed, fallback, reported) {
|
|
19
|
+
if (suppressed(node) || reported?.has(node)) return;
|
|
20
|
+
if (hasRequiredOptions(node.arguments[target.optionsPosition - 1], target, context.sourceCode)) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
reported?.add(node);
|
|
24
|
+
const custom = target.message || fallback;
|
|
25
|
+
if (custom) {
|
|
26
|
+
context.report({ node, messageId: "customOptions", data: { message: custom } });
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
context.report({
|
|
30
|
+
node,
|
|
31
|
+
messageId: "missingOptions",
|
|
32
|
+
data: {
|
|
33
|
+
source: resolved.source,
|
|
34
|
+
calleeName: resolved.calleeName,
|
|
35
|
+
optionsPosition: String(target.optionsPosition),
|
|
36
|
+
propertyMatch: target.propertyMatch,
|
|
37
|
+
requiredProperties: target.requiredProperties.join(", "),
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
}
|
|
14
41
|
|
|
15
42
|
module.exports = Object.assign(
|
|
16
43
|
rule(
|
|
@@ -18,50 +45,56 @@ module.exports = Object.assign(
|
|
|
18
45
|
type: "problem",
|
|
19
46
|
docs: {
|
|
20
47
|
description:
|
|
21
|
-
"require a statically visible options object on
|
|
48
|
+
"require a statically visible options object on configured imported, global, or receiver-shaped calls",
|
|
22
49
|
recommended: false,
|
|
23
50
|
},
|
|
24
51
|
schema: importedCallOptionsSchema,
|
|
25
52
|
messages: {
|
|
26
53
|
missingOptions:
|
|
27
54
|
"Call '{{calleeName}}' from '{{source}}' must pass a statically visible options object at argument {{optionsPosition}} containing {{propertyMatch}} of: {{requiredProperties}}.",
|
|
55
|
+
customOptions: "{{message}}",
|
|
28
56
|
},
|
|
29
57
|
},
|
|
30
58
|
(context) => {
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
59
|
+
const options = context.options?.[0] || {};
|
|
60
|
+
if (!shouldCheckOptionsFile(context.filename, options)) return {};
|
|
61
|
+
const optionTargets = compileImportedCallTargets(options);
|
|
62
|
+
const selectors = createCallSelectors(context, options);
|
|
63
|
+
if (optionTargets.length === 0 && !selectors.hasTargets) return {};
|
|
64
|
+
const matcher = optionTargets.length > 0 ? createTargetMatcher(context) : null;
|
|
65
|
+
const suppressed = ruleSuppression(context, "require-options-on-imported-call");
|
|
66
|
+
const fallback = typeof options.message === "string" ? options.message : "";
|
|
67
|
+
const reported = new WeakSet();
|
|
34
68
|
|
|
35
69
|
return {
|
|
36
|
-
...matcher.visitors,
|
|
70
|
+
...(matcher ? matcher.visitors : {}),
|
|
71
|
+
Program(node) {
|
|
72
|
+
matcher?.visitors.Program(node);
|
|
73
|
+
selectors.record(node);
|
|
74
|
+
},
|
|
37
75
|
CallExpression(node) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
target,
|
|
49
|
-
context.sourceCode,
|
|
50
|
-
)
|
|
51
|
-
) {
|
|
52
|
-
continue;
|
|
76
|
+
if (matcher) {
|
|
77
|
+
const resolved = matcher.resolveCallTarget(node);
|
|
78
|
+
if (resolved) {
|
|
79
|
+
for (const target of matchingTargets(
|
|
80
|
+
optionTargets,
|
|
81
|
+
resolved.source,
|
|
82
|
+
resolved.calleeName,
|
|
83
|
+
)) {
|
|
84
|
+
reportMissing(context, node, target, resolved, suppressed, fallback, null);
|
|
85
|
+
}
|
|
53
86
|
}
|
|
54
|
-
|
|
87
|
+
}
|
|
88
|
+
for (const match of selectors.matches(node)) {
|
|
89
|
+
reportMissing(
|
|
90
|
+
context,
|
|
55
91
|
node,
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
requiredProperties: target.requiredProperties.join(", "),
|
|
63
|
-
},
|
|
64
|
-
});
|
|
92
|
+
match.target,
|
|
93
|
+
match.resolved,
|
|
94
|
+
suppressed,
|
|
95
|
+
fallback,
|
|
96
|
+
reported,
|
|
97
|
+
);
|
|
65
98
|
}
|
|
66
99
|
},
|
|
67
100
|
};
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { patternToRegExp } = require("./async-patterns");
|
|
4
|
+
const { normalizeRequiredProperties } = require("./imported-call-options");
|
|
5
|
+
const { repoRelativeFilename, stringMatches } = require("./module-mock-helpers");
|
|
6
|
+
|
|
7
|
+
function compileSelectorPattern(pattern, globSource) {
|
|
8
|
+
const regexForm = pattern.startsWith("/") && pattern.endsWith("/") && pattern.length > 2;
|
|
9
|
+
if (regexForm) {
|
|
10
|
+
try {
|
|
11
|
+
return { regex: new RegExp(pattern.slice(1, -1)), glob: false };
|
|
12
|
+
} catch (error) {
|
|
13
|
+
throw new Error(`Invalid selector pattern ${pattern}: ${error.message}`);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
const regex = patternToRegExp(globSource);
|
|
17
|
+
return regex ? { regex, glob: true } : null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function compileNamePatterns(patterns) {
|
|
21
|
+
if (!Array.isArray(patterns)) return [];
|
|
22
|
+
return patterns
|
|
23
|
+
.filter((pattern) => typeof pattern === "string" && pattern)
|
|
24
|
+
.map((pattern) => compileSelectorPattern(pattern, pattern)?.regex)
|
|
25
|
+
.filter(Boolean);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Globs use `.` as a segment separator. `/regex/` is tested against the dotted path.
|
|
29
|
+
function compileDottedPattern(pattern) {
|
|
30
|
+
if (typeof pattern !== "string" || !pattern) return null;
|
|
31
|
+
return compileSelectorPattern(pattern, pattern.replaceAll(".", "/"));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function compileDottedPatterns(patterns) {
|
|
35
|
+
return patterns.map((pattern) => compileDottedPattern(pattern)).filter(Boolean);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function matchesDotted(path, patterns) {
|
|
39
|
+
const slashed = path.replaceAll(".", "/");
|
|
40
|
+
return patterns.some((pattern) => pattern.regex.test(pattern.glob ? slashed : path));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function selectorTarget(target, requiredProperties) {
|
|
44
|
+
const compiled = {
|
|
45
|
+
optionsPosition: target.optionsPosition,
|
|
46
|
+
requiredProperties,
|
|
47
|
+
propertyMatch: target.propertyMatch === "all" ? "all" : "any",
|
|
48
|
+
};
|
|
49
|
+
if (typeof target.message === "string" && target.message) compiled.message = target.message;
|
|
50
|
+
return compiled;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function compileCallSelectors(options) {
|
|
54
|
+
const globals = [];
|
|
55
|
+
const members = [];
|
|
56
|
+
for (const target of options.targets || []) {
|
|
57
|
+
// Import targets keep today's matcher. Global and receiver kinds cannot share a target.
|
|
58
|
+
if (!target || Array.isArray(target.sourceSpecifierPatterns)) continue;
|
|
59
|
+
const hasGlobal = Array.isArray(target.globalNamePatterns);
|
|
60
|
+
const hasReceiver = Array.isArray(target.receiverPatterns);
|
|
61
|
+
const optionsPosition = target.optionsPosition;
|
|
62
|
+
const names = normalizeRequiredProperties(target.requiredProperties);
|
|
63
|
+
if (
|
|
64
|
+
hasGlobal === hasReceiver ||
|
|
65
|
+
!Number.isInteger(optionsPosition) ||
|
|
66
|
+
optionsPosition < 1 ||
|
|
67
|
+
names.length === 0
|
|
68
|
+
) {
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
const base = selectorTarget(target, names);
|
|
72
|
+
if (hasGlobal) {
|
|
73
|
+
const globalNamePatterns = compileNamePatterns(target.globalNamePatterns);
|
|
74
|
+
if (globalNamePatterns.length === 0) continue;
|
|
75
|
+
globals.push({ ...base, globalNamePatterns });
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
const receiverPatterns = compileDottedPatterns(target.receiverPatterns);
|
|
79
|
+
const calleeNamePatterns = compileNamePatterns(target.calleeNamePatterns);
|
|
80
|
+
if (receiverPatterns.length === 0 || calleeNamePatterns.length === 0) continue;
|
|
81
|
+
members.push({ ...base, receiverPatterns, calleeNamePatterns });
|
|
82
|
+
}
|
|
83
|
+
return { globals, members };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function shouldCheckOptionsFile(filename, options) {
|
|
87
|
+
const checked = options.checkedPathPatterns || [];
|
|
88
|
+
const allowed = options.allowedPathPatterns || [];
|
|
89
|
+
if (checked.length === 0 && allowed.length === 0) return true;
|
|
90
|
+
const file = repoRelativeFilename(filename || "");
|
|
91
|
+
if (checked.length > 0 && !stringMatches(file, checked)) return false;
|
|
92
|
+
return !stringMatches(file, allowed);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
module.exports = {
|
|
96
|
+
compileCallSelectors,
|
|
97
|
+
matchesDotted,
|
|
98
|
+
shouldCheckOptionsFile,
|
|
99
|
+
};
|