@antelopejs/tooling-configs 0.6.0 → 0.7.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 +15 -15
- package/dist/{api-CCOiLN3w.mjs → api-BQLrI_S1.mjs} +19 -2
- package/dist/bin/body-check.mjs +1 -1
- package/dist/bin/checks.mjs +1 -1
- package/dist/bin/garden.mjs +9 -2
- package/dist/bin/ratchet.mjs +2 -2
- package/dist/bin/suppressions.mjs +7 -3
- package/dist/bin/sync.mjs +3 -3
- package/dist/bin/triage.mjs +1 -1
- package/dist/{checks-C_I0jCN4.mjs → checks-BefrkjiT.mjs} +350 -10
- package/dist/eslint.mjs +1 -1
- package/dist/{guardrails-C72rFG8J.mjs → guardrails-BfGwehVj.mjs} +480 -118
- package/dist/oxc/anti-slop/index.mjs +5 -4
- package/dist/oxc/guardrails/index.d.mts +1 -1
- package/dist/oxc/guardrails/index.mjs +1 -1
- package/dist/{oxlint-CCOJqj3d.mjs → oxlint-CkHhK5K_.mjs} +33 -11
- package/dist/{ratchet-qu2aLzQU.mjs → ratchet-2FeMs1kh.mjs} +1 -1
- package/package.json +1 -1
|
@@ -467,8 +467,8 @@ function differsFromRootWhen(node, read) {
|
|
|
467
467
|
}
|
|
468
468
|
/** `a && b` that is true, or `a || b` that is false, needs one operand to hold; `a || b` that is true, or `a && b` that is false, needs both. */
|
|
469
469
|
function combinedHolds(node, outcome, read, depth) {
|
|
470
|
-
const left = holds(node.left, outcome, read, depth);
|
|
471
|
-
const right = holds(node.right, outcome, read, depth);
|
|
470
|
+
const left = holds$1(node.left, outcome, read, depth);
|
|
471
|
+
const right = holds$1(node.right, outcome, read, depth);
|
|
472
472
|
if (node.operator === (outcome ? "&&" : "||")) return left || right;
|
|
473
473
|
return node.operator === (outcome ? "||" : "&&") && left && right;
|
|
474
474
|
}
|
|
@@ -477,23 +477,23 @@ function combinedHolds(node, outcome, read, depth) {
|
|
|
477
477
|
* the key is no `Object.prototype` name, or the table answers it with something other than
|
|
478
478
|
* `Object.prototype`'s own value.
|
|
479
479
|
*/
|
|
480
|
-
function holds(node, outcome, read, depth = 0) {
|
|
480
|
+
function holds$1(node, outcome, read, depth = 0) {
|
|
481
481
|
if (depth > MAX_DEPTH$2) return false;
|
|
482
482
|
switch (node.type) {
|
|
483
|
-
case "ParenthesizedExpression": return holds(node.expression, outcome, read, depth);
|
|
483
|
+
case "ParenthesizedExpression": return holds$1(node.expression, outcome, read, depth);
|
|
484
484
|
case "CallExpression": return outcome ? checksOwnKey(node, read) : checksOwnKey(node, {
|
|
485
485
|
...read,
|
|
486
486
|
table: "Object.prototype"
|
|
487
487
|
});
|
|
488
488
|
case "Identifier": {
|
|
489
489
|
const value = constValue(node, read);
|
|
490
|
-
return value !== null && holds(value, outcome, read, depth + 1);
|
|
490
|
+
return value !== null && holds$1(value, outcome, read, depth + 1);
|
|
491
491
|
}
|
|
492
|
-
case "UnaryExpression": return node.operator === "!" && holds(node.argument, !outcome, read, depth);
|
|
492
|
+
case "UnaryExpression": return node.operator === "!" && holds$1(node.argument, !outcome, read, depth);
|
|
493
493
|
case "BinaryExpression": {
|
|
494
494
|
if (differsFromRootWhen(node, read) === outcome) return true;
|
|
495
495
|
const compared = comparedOutcome(node, outcome);
|
|
496
|
-
return compared !== null && isBooleanValued(compared.operand, read, depth) && holds(compared.operand, compared.outcome, read, depth);
|
|
496
|
+
return compared !== null && isBooleanValued(compared.operand, read, depth) && holds$1(compared.operand, compared.outcome, read, depth);
|
|
497
497
|
}
|
|
498
498
|
case "LogicalExpression": return combinedHolds(node, outcome, read, depth);
|
|
499
499
|
default: return false;
|
|
@@ -527,11 +527,11 @@ function settles(statement, read) {
|
|
|
527
527
|
function establishes(statement, read) {
|
|
528
528
|
if (statement.type === "ExpressionStatement" && statement.expression.type === "CallExpression") {
|
|
529
529
|
const [condition] = statement.expression.arguments;
|
|
530
|
-
return ASSERTS.has(callName(statement.expression.callee) ?? "") && condition !== void 0 && holds(condition, true, read);
|
|
530
|
+
return ASSERTS.has(callName(statement.expression.callee) ?? "") && condition !== void 0 && holds$1(condition, true, read);
|
|
531
531
|
}
|
|
532
532
|
if (statement.type !== "IfStatement") return false;
|
|
533
|
-
if (settles(statement.consequent, read) && holds(statement.test, false, read)) return true;
|
|
534
|
-
return statement.alternate !== null && settles(statement.alternate, read) && holds(statement.test, true, read);
|
|
533
|
+
if (settles(statement.consequent, read) && holds$1(statement.test, false, read)) return true;
|
|
534
|
+
return statement.alternate !== null && settles(statement.alternate, read) && holds$1(statement.test, true, read);
|
|
535
535
|
}
|
|
536
536
|
function statementsOf(node) {
|
|
537
537
|
if (node.type === "BlockStatement" || node.type === "Program" || node.type === "StaticBlock") return node.body;
|
|
@@ -539,12 +539,12 @@ function statementsOf(node) {
|
|
|
539
539
|
}
|
|
540
540
|
function guardsChild(parent, child, read) {
|
|
541
541
|
if (parent.type === "ConditionalExpression" || parent.type === "IfStatement") {
|
|
542
|
-
if (child === parent.consequent) return holds(parent.test, true, read);
|
|
543
|
-
return child === parent.alternate && holds(parent.test, false, read);
|
|
542
|
+
if (child === parent.consequent) return holds$1(parent.test, true, read);
|
|
543
|
+
return child === parent.alternate && holds$1(parent.test, false, read);
|
|
544
544
|
}
|
|
545
|
-
if (parent.type === "LogicalExpression" && child === parent.right) return (parent.operator === "&&" || parent.operator === "||") && holds(parent.left, parent.operator === "&&", read);
|
|
546
|
-
if (parent.type === "WhileStatement") return child === parent.body && holds(parent.test, true, read);
|
|
547
|
-
if (parent.type === "ForStatement") return (child === parent.body || child === parent.update) && parent.test !== null && holds(parent.test, true, read);
|
|
545
|
+
if (parent.type === "LogicalExpression" && child === parent.right) return (parent.operator === "&&" || parent.operator === "||") && holds$1(parent.left, parent.operator === "&&", read);
|
|
546
|
+
if (parent.type === "WhileStatement") return child === parent.body && holds$1(parent.test, true, read);
|
|
547
|
+
if (parent.type === "ForStatement") return (child === parent.body || child === parent.update) && parent.test !== null && holds$1(parent.test, true, read);
|
|
548
548
|
const statements = statementsOf(parent);
|
|
549
549
|
if (statements === null) return false;
|
|
550
550
|
const index = statements.indexOf(child);
|
|
@@ -1031,20 +1031,206 @@ const noPresenceOnlyTypeGuardRule = defineRule({
|
|
|
1031
1031
|
}
|
|
1032
1032
|
});
|
|
1033
1033
|
//#endregion
|
|
1034
|
+
//#region src/oxc/guardrails/read-decision.ts
|
|
1035
|
+
const MIRRORED = {
|
|
1036
|
+
"==": "==",
|
|
1037
|
+
"===": "===",
|
|
1038
|
+
"!=": "!=",
|
|
1039
|
+
"!==": "!==",
|
|
1040
|
+
"<": ">",
|
|
1041
|
+
"<=": ">=",
|
|
1042
|
+
">": "<",
|
|
1043
|
+
">=": "<="
|
|
1044
|
+
};
|
|
1045
|
+
const UNKNOWN = {
|
|
1046
|
+
whenTrue: false,
|
|
1047
|
+
whenFalse: false
|
|
1048
|
+
};
|
|
1049
|
+
const FOUND_WHEN_TRUE = {
|
|
1050
|
+
whenTrue: false,
|
|
1051
|
+
whenFalse: true
|
|
1052
|
+
};
|
|
1053
|
+
const EMPTY_WHEN_TRUE = {
|
|
1054
|
+
whenTrue: true,
|
|
1055
|
+
whenFalse: false
|
|
1056
|
+
};
|
|
1057
|
+
/** Whether `node` is `call`, or a call or member chained from it such as `table.get(id).run()`. */
|
|
1058
|
+
function chainsFrom(node, call) {
|
|
1059
|
+
let current = node;
|
|
1060
|
+
while (current !== call) if (current.type === "CallExpression") current = current.callee;
|
|
1061
|
+
else if (current.type === "MemberExpression") current = current.object;
|
|
1062
|
+
else return false;
|
|
1063
|
+
return true;
|
|
1064
|
+
}
|
|
1065
|
+
function unwrap(node) {
|
|
1066
|
+
if (node.type === "AwaitExpression") return unwrap(node.argument);
|
|
1067
|
+
if (node.type === "ChainExpression" || node.type === "ParenthesizedExpression" || node.type === "TSAsExpression" || node.type === "TSNonNullExpression") return unwrap(node.expression);
|
|
1068
|
+
return node;
|
|
1069
|
+
}
|
|
1070
|
+
function isMethodCall(member) {
|
|
1071
|
+
return member.parent?.type === "CallExpression" && member.parent.callee === member;
|
|
1072
|
+
}
|
|
1073
|
+
/** Whether `.length` or an index such as `[0]` reads the settled result: on rows, they still tell whether the read found any. */
|
|
1074
|
+
function isCountOrIndex(member) {
|
|
1075
|
+
return memberName(member) === "length" || member.computed && member.property.type === "Literal" && Number.isInteger(member.property.value);
|
|
1076
|
+
}
|
|
1077
|
+
/** Whether `parent` passes on the read's value. Before the read settles a method call builds the query (`.limit(1).run()`, `.then(...)`); once awaited, only `.length` or an index keeps it, since a field or method of the row is other data. */
|
|
1078
|
+
function carriesValue(node, parent, settled) {
|
|
1079
|
+
if (parent.type === "MemberExpression") return parent.object === node && (settled ? isCountOrIndex(parent) : isMethodCall(parent));
|
|
1080
|
+
return parent.type === "AwaitExpression" || parent.type === "ChainExpression" || parent.type === "ParenthesizedExpression" || parent.type === "TSAsExpression" || parent.type === "TSNonNullExpression" || parent.type === "CallExpression" && parent.callee === node;
|
|
1081
|
+
}
|
|
1082
|
+
/** The outermost expression that still holds a read's value, the node around it, and whether the read settled on the way through `await` or `.then(...)`. */
|
|
1083
|
+
function readValue(call) {
|
|
1084
|
+
let node = call;
|
|
1085
|
+
let parent = node.parent;
|
|
1086
|
+
let settled = false;
|
|
1087
|
+
while (parent !== null && carriesValue(node, parent, settled)) {
|
|
1088
|
+
if (parent.type === "AwaitExpression" || parent.type === "MemberExpression" && memberName(parent) === "then") settled = true;
|
|
1089
|
+
node = parent;
|
|
1090
|
+
parent = node.parent;
|
|
1091
|
+
}
|
|
1092
|
+
return {
|
|
1093
|
+
node,
|
|
1094
|
+
parent,
|
|
1095
|
+
settled
|
|
1096
|
+
};
|
|
1097
|
+
}
|
|
1098
|
+
/** Whether a read's value is awaited, directly or through a chain such as `table.get(id).run()` or `.then(...)`: a table read is asynchronous, unlike `Map.get` or `headers.get`. */
|
|
1099
|
+
function isAwaitedRead(call) {
|
|
1100
|
+
return readValue(call).settled;
|
|
1101
|
+
}
|
|
1102
|
+
/** The variable a read's result is stored in, through `await`, `.run()` and the like: `rows` in `const rows = await table.getAll().run()`, or the first row in `const [row] = await table.getAll()` or `(await table.getAll())[0]`. */
|
|
1103
|
+
function readBinding(source, call) {
|
|
1104
|
+
const { node, parent } = readValue(call);
|
|
1105
|
+
if (parent?.type !== "VariableDeclarator" || parent.init !== node) return null;
|
|
1106
|
+
const target = parent.id.type === "ArrayPattern" ? parent.id.elements[0] : parent.id;
|
|
1107
|
+
if (target?.type !== "Identifier") return null;
|
|
1108
|
+
return source.getDeclaredVariables(parent).find((variable) => variable.name === target.name) ?? null;
|
|
1109
|
+
}
|
|
1110
|
+
function isSubject(source, node, subject) {
|
|
1111
|
+
const value = unwrap(node);
|
|
1112
|
+
if (value.type === "Identifier") return subject.binding !== null && resolveVariable(source, value) === subject.binding;
|
|
1113
|
+
return chainsFrom(value, subject.call);
|
|
1114
|
+
}
|
|
1115
|
+
function isCounted(source, node, subject) {
|
|
1116
|
+
const value = unwrap(node);
|
|
1117
|
+
return isSubject(source, value, subject) || value.type === "MemberExpression" && memberName(value) === "length" && isSubject(source, value.object, subject);
|
|
1118
|
+
}
|
|
1119
|
+
function holds(operator, left, right) {
|
|
1120
|
+
switch (operator) {
|
|
1121
|
+
case "==":
|
|
1122
|
+
case "===": return left === right;
|
|
1123
|
+
case "!=":
|
|
1124
|
+
case "!==": return left !== right;
|
|
1125
|
+
case "<": return left < right;
|
|
1126
|
+
case "<=": return left <= right;
|
|
1127
|
+
case ">": return left > right;
|
|
1128
|
+
case ">=": return left >= right;
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
function isComparison(operator) {
|
|
1132
|
+
return Object.hasOwn(MIRRORED, operator);
|
|
1133
|
+
}
|
|
1134
|
+
/** What `subject <operator> constant` proves: `record === undefined`, `count === 0` and `rows.length < 1` pass only when the read found nothing. */
|
|
1135
|
+
function compared(operator, constant) {
|
|
1136
|
+
if (constant.type === "Literal" && constant.value === null && !("regex" in constant) || constant.type === "Identifier" && constant.name === "undefined") return operator === "==" || operator === "===" ? EMPTY_WHEN_TRUE : FOUND_WHEN_TRUE;
|
|
1137
|
+
if (constant.type !== "Literal" || !Number.isFinite(constant.value)) return UNKNOWN;
|
|
1138
|
+
const bound = Number(constant.value);
|
|
1139
|
+
const empty = holds(operator, 0, bound);
|
|
1140
|
+
if (empty === holds(operator, Number.MAX_SAFE_INTEGER, bound)) return UNKNOWN;
|
|
1141
|
+
return empty ? EMPTY_WHEN_TRUE : FOUND_WHEN_TRUE;
|
|
1142
|
+
}
|
|
1143
|
+
function implication(source, test, subject) {
|
|
1144
|
+
const value = unwrap(test);
|
|
1145
|
+
if (isCounted(source, value, subject)) return FOUND_WHEN_TRUE;
|
|
1146
|
+
if (value.type === "UnaryExpression" && value.operator === "!") {
|
|
1147
|
+
const inner = implication(source, value.argument, subject);
|
|
1148
|
+
return {
|
|
1149
|
+
whenTrue: inner.whenFalse,
|
|
1150
|
+
whenFalse: inner.whenTrue
|
|
1151
|
+
};
|
|
1152
|
+
}
|
|
1153
|
+
if (value.type === "LogicalExpression" && value.operator !== "??") {
|
|
1154
|
+
const left = implication(source, value.left, subject);
|
|
1155
|
+
const right = implication(source, value.right, subject);
|
|
1156
|
+
return value.operator === "&&" ? {
|
|
1157
|
+
whenTrue: left.whenTrue || right.whenTrue,
|
|
1158
|
+
whenFalse: false
|
|
1159
|
+
} : {
|
|
1160
|
+
whenTrue: false,
|
|
1161
|
+
whenFalse: left.whenFalse || right.whenFalse
|
|
1162
|
+
};
|
|
1163
|
+
}
|
|
1164
|
+
if (value.type !== "BinaryExpression" || !isComparison(value.operator)) return UNKNOWN;
|
|
1165
|
+
if (isCounted(source, value.left, subject)) return compared(value.operator, unwrap(value.right));
|
|
1166
|
+
if (isCounted(source, value.right, subject)) return compared(MIRRORED[value.operator], unwrap(value.left));
|
|
1167
|
+
return UNKNOWN;
|
|
1168
|
+
}
|
|
1169
|
+
function within(outer, inner) {
|
|
1170
|
+
return outer !== null && outer.range[0] <= inner.range[0] && inner.range[1] <= outer.range[1];
|
|
1171
|
+
}
|
|
1172
|
+
function lastStatement(statement) {
|
|
1173
|
+
if (statement?.type === "BlockStatement") return lastStatement(statement.body.at(-1) ?? null);
|
|
1174
|
+
return statement;
|
|
1175
|
+
}
|
|
1176
|
+
function isFunction$1(node) {
|
|
1177
|
+
return node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression";
|
|
1178
|
+
}
|
|
1179
|
+
function isLoop(node) {
|
|
1180
|
+
return node.type === "ForStatement" || node.type === "ForInStatement" || node.type === "ForOfStatement" || node.type === "WhileStatement" || node.type === "DoWhileStatement";
|
|
1181
|
+
}
|
|
1182
|
+
/** The node an exit leaves: its function for `return` and `throw`, its loop for `continue`, its loop or switch for `break`. */
|
|
1183
|
+
function exited(exit) {
|
|
1184
|
+
const leavesFunction = exit.type === "ReturnStatement" || exit.type === "ThrowStatement";
|
|
1185
|
+
if (!leavesFunction && exit.type !== "ContinueStatement" && exit.type !== "BreakStatement") return null;
|
|
1186
|
+
let current = exit.parent;
|
|
1187
|
+
while (current !== null) {
|
|
1188
|
+
if (isFunction$1(current)) return current;
|
|
1189
|
+
if (!leavesFunction && isLoop(current)) return current;
|
|
1190
|
+
if (exit.type === "BreakStatement" && current.type === "SwitchStatement") return current;
|
|
1191
|
+
current = current.parent;
|
|
1192
|
+
}
|
|
1193
|
+
return null;
|
|
1194
|
+
}
|
|
1195
|
+
function leaves(branch, site) {
|
|
1196
|
+
const exit = lastStatement(branch);
|
|
1197
|
+
return exit !== null && within(exited(exit), site);
|
|
1198
|
+
}
|
|
1199
|
+
/** Whether a branch ends in an exit that skips `site`: the exit leaves a function or loop that holds the site, and the site follows the `if` in the block that holds it. An `else if` reaches that block only when every earlier arm exits past the site too. */
|
|
1200
|
+
function skips(conditional, branch, site) {
|
|
1201
|
+
let arm = conditional;
|
|
1202
|
+
let block = conditional.parent;
|
|
1203
|
+
while (block?.type === "IfStatement") {
|
|
1204
|
+
if (block.alternate !== arm || !leaves(block.consequent, site)) return false;
|
|
1205
|
+
arm = block;
|
|
1206
|
+
block = block.parent;
|
|
1207
|
+
}
|
|
1208
|
+
return conditional.range[1] <= site.range[0] && within(block, site) && leaves(branch, site);
|
|
1209
|
+
}
|
|
1210
|
+
/** Whether a conditional runs `site` only when the read found nothing: the site sits in the branch taken only then, or it follows an `if` whose branch for a found row skips it, as in `if (record) return;`. */
|
|
1211
|
+
function decides(source, subject, conditional, site) {
|
|
1212
|
+
if (conditional.type === "LogicalExpression") {
|
|
1213
|
+
const left = implication(source, conditional.left, subject);
|
|
1214
|
+
return (conditional.operator === "&&" ? left.whenTrue : left.whenFalse) && within(conditional.right, site);
|
|
1215
|
+
}
|
|
1216
|
+
const { whenTrue, whenFalse } = implication(source, conditional.test, subject);
|
|
1217
|
+
if (whenTrue && within(conditional.consequent, site) || whenFalse && within(conditional.alternate, site)) return true;
|
|
1218
|
+
return conditional.type === "IfStatement" && (whenFalse && skips(conditional, conditional.consequent, site) || whenTrue && skips(conditional, conditional.alternate, site));
|
|
1219
|
+
}
|
|
1220
|
+
/** The function a plain call runs, found through the scope its name resolves in: a function declaration (the implementation of an overloaded one, whose signatures declare no function), or a variable that holds a function. A name the code reassigns is not followed. */
|
|
1221
|
+
function calledFunction(source, callee) {
|
|
1222
|
+
const variable = resolveVariable(source, callee);
|
|
1223
|
+
if (variable === null || variable.references.some((reference) => reference.isWrite() && !reference.init)) return null;
|
|
1224
|
+
for (const { node } of variable.defs) {
|
|
1225
|
+
if (node.type === "FunctionDeclaration") return node;
|
|
1226
|
+
if (node.type === "VariableDeclarator" && (node.init?.type === "ArrowFunctionExpression" || node.init?.type === "FunctionExpression")) return node.init;
|
|
1227
|
+
}
|
|
1228
|
+
return null;
|
|
1229
|
+
}
|
|
1230
|
+
//#endregion
|
|
1034
1231
|
//#region src/oxc/guardrails/rules/no-read-then-insert.ts
|
|
1035
|
-
const
|
|
1036
|
-
|
|
1037
|
-
"getBy",
|
|
1038
|
-
"getAll",
|
|
1039
|
-
"count",
|
|
1040
|
-
"find",
|
|
1041
|
-
"findOne"
|
|
1042
|
-
]);
|
|
1043
|
-
const INSERT_METHODS = /* @__PURE__ */ new Set([
|
|
1044
|
-
"insert",
|
|
1045
|
-
"insertOne",
|
|
1046
|
-
"insertMany"
|
|
1047
|
-
]);
|
|
1232
|
+
const READ_METHOD = /^(?:get|getAll|count|find|findOne|(?:get|find)By(?:[A-Z]\w*)?)$/u;
|
|
1233
|
+
const INSERT_METHOD = /^(?:insert|insertOne|insertMany)$/u;
|
|
1048
1234
|
const INLINE_CALLBACK_METHODS = /* @__PURE__ */ new Set([
|
|
1049
1235
|
"map",
|
|
1050
1236
|
"flatMap",
|
|
@@ -1098,13 +1284,6 @@ function runningScope(node) {
|
|
|
1098
1284
|
while (scope !== null && isInlineCallback(scope)) scope = enclosingScope(scope);
|
|
1099
1285
|
return scope;
|
|
1100
1286
|
}
|
|
1101
|
-
function chainsFrom(node, call) {
|
|
1102
|
-
let current = node;
|
|
1103
|
-
while (current !== call) if (current.type === "CallExpression") current = current.callee;
|
|
1104
|
-
else if (current.type === "MemberExpression") current = current.object;
|
|
1105
|
-
else return false;
|
|
1106
|
-
return true;
|
|
1107
|
-
}
|
|
1108
1287
|
function rootName(node) {
|
|
1109
1288
|
let current = node;
|
|
1110
1289
|
while (current.type === "MemberExpression" || current.type === "CallExpression") current = current.type === "MemberExpression" ? current.object : current.callee;
|
|
@@ -1116,7 +1295,7 @@ function declares(scope, name) {
|
|
|
1116
1295
|
function calledMember(node, methods) {
|
|
1117
1296
|
if (node.callee.type !== "MemberExpression") return null;
|
|
1118
1297
|
const name = memberName(node.callee);
|
|
1119
|
-
return name !== null && methods.
|
|
1298
|
+
return name !== null && methods.test(name) ? node.callee : null;
|
|
1120
1299
|
}
|
|
1121
1300
|
function isUpsert(node) {
|
|
1122
1301
|
return node.arguments.slice(1).some((argument) => argument.type === "ObjectExpression" && argument.properties.some((property) => property.type === "Property" && !property.computed && (property.key.type === "Identifier" && property.key.name === "conflict" || property.key.type === "Literal" && property.key.value === "conflict") && !(property.value.type === "Literal" && property.value.value === "error")));
|
|
@@ -1142,19 +1321,20 @@ function recovers(facts, handler, insert) {
|
|
|
1142
1321
|
function isHandedToHelper(facts, insert, child, parent) {
|
|
1143
1322
|
return (child.type === "ArrowFunctionExpression" || child.type === "FunctionExpression") && parent.type === "CallExpression" && parent.arguments.some((argument) => argument !== child && facts.text(argument) === insert.receiver);
|
|
1144
1323
|
}
|
|
1145
|
-
function recoveryOf(
|
|
1324
|
+
function recoveryOf(site, child, parent) {
|
|
1146
1325
|
if (parent.type === "TryStatement" && parent.block === child) return parent.handler?.body ?? null;
|
|
1147
|
-
if (parent.type === "MemberExpression" && parent.object === child && chainsFrom(child,
|
|
1326
|
+
if (parent.type === "MemberExpression" && parent.object === child && chainsFrom(child, site) && memberName(parent) === "catch" && parent.parent?.type === "CallExpression" && parent.parent.callee === parent) return parent.parent.arguments[0] ?? null;
|
|
1148
1327
|
return null;
|
|
1149
1328
|
}
|
|
1150
|
-
|
|
1151
|
-
|
|
1329
|
+
/** Whether the insert is guarded on the way from `site` up to `stop`: `site` is the insert itself, or a call that reaches it. Only the insert's own function can shadow the read's receiver or hand the insert to a helper; any call on the way can recover from it. */
|
|
1330
|
+
function isGuarded(facts, insert, site, stop, read) {
|
|
1331
|
+
let child = site;
|
|
1152
1332
|
let parent = child.parent;
|
|
1153
|
-
while (parent !== null && child !==
|
|
1154
|
-
const between = !contains(child.range, read.range);
|
|
1155
|
-
if (between && insert.root !== null && isScope(child) && declares(child, insert.root)) return true;
|
|
1333
|
+
while (parent !== null && child !== stop) {
|
|
1334
|
+
const between = site === insert.call && (read === null || !contains(child.range, read.range));
|
|
1335
|
+
if (between && read !== null && insert.root !== null && isScope(child) && declares(child, insert.root)) return true;
|
|
1156
1336
|
if (between && isHandedToHelper(facts, insert, child, parent)) return true;
|
|
1157
|
-
const recovery = recoveryOf(
|
|
1337
|
+
const recovery = recoveryOf(site, child, parent);
|
|
1158
1338
|
if (recovery !== null && recovers(facts, recovery, insert)) return true;
|
|
1159
1339
|
child = parent;
|
|
1160
1340
|
parent = child.parent;
|
|
@@ -1163,97 +1343,175 @@ function isGuarded(facts, insert, read) {
|
|
|
1163
1343
|
}
|
|
1164
1344
|
function isRaced(facts, insert) {
|
|
1165
1345
|
const start = insert.call.range[0];
|
|
1166
|
-
return facts.reads.some((read) => sameTable(read, insert) && read.range[1] <= start && read.scope === insert.scope && !isGuarded(facts, insert, read));
|
|
1346
|
+
return facts.reads.some((read) => sameTable(read, insert) && read.range[1] <= start && read.scope === insert.scope && !isGuarded(facts, insert, insert.call, insert.scope, read));
|
|
1347
|
+
}
|
|
1348
|
+
/** The unguarded inserts each function makes, in its own body or through the functions of this file it calls, grown until no call adds one so the order of a cycle does not matter. */
|
|
1349
|
+
function reachedInserts(facts) {
|
|
1350
|
+
const reached = /* @__PURE__ */ new Map();
|
|
1351
|
+
const of = (scope) => {
|
|
1352
|
+
const known = reached.get(scope);
|
|
1353
|
+
if (known !== void 0) return known;
|
|
1354
|
+
const created = /* @__PURE__ */ new Set();
|
|
1355
|
+
reached.set(scope, created);
|
|
1356
|
+
return created;
|
|
1357
|
+
};
|
|
1358
|
+
for (const insert of facts.inserts) if (!isGuarded(facts, insert, insert.call, insert.scope, null)) of(insert.scope).add(insert);
|
|
1359
|
+
let changed = true;
|
|
1360
|
+
while (changed) {
|
|
1361
|
+
changed = false;
|
|
1362
|
+
for (const site of facts.sites) {
|
|
1363
|
+
const into = of(site.scope);
|
|
1364
|
+
for (const insert of of(site.target)) {
|
|
1365
|
+
if (into.has(insert) || isGuarded(facts, insert, site.call, site.scope, null)) continue;
|
|
1366
|
+
into.add(insert);
|
|
1367
|
+
changed = true;
|
|
1368
|
+
}
|
|
1369
|
+
}
|
|
1370
|
+
}
|
|
1371
|
+
return reached;
|
|
1372
|
+
}
|
|
1373
|
+
function racedThroughCalls(facts) {
|
|
1374
|
+
const reached = reachedInserts(facts);
|
|
1375
|
+
return facts.reads.filter(({ call }) => isAwaitedRead(call)).flatMap((read) => {
|
|
1376
|
+
const conditionals = facts.conditionals.filter((conditional) => conditional.range[1] > read.range[0] && runningScope(conditional) === read.scope);
|
|
1377
|
+
return facts.sites.filter((site) => site.scope === read.scope && read.range[1] <= site.call.range[0] && conditionals.some((conditional) => decides(facts.source, read, conditional, site.call))).flatMap((site) => [...reached.get(site.target) ?? []].filter((insert) => !isGuarded(facts, insert, site.call, read.scope, null)).map((insert) => ({
|
|
1378
|
+
insert,
|
|
1379
|
+
read,
|
|
1380
|
+
site
|
|
1381
|
+
})));
|
|
1382
|
+
});
|
|
1167
1383
|
}
|
|
1168
|
-
|
|
1384
|
+
function receiverChain(facts, start) {
|
|
1385
|
+
const texts = /* @__PURE__ */ new Set();
|
|
1386
|
+
let current = start;
|
|
1387
|
+
while (current !== null) {
|
|
1388
|
+
texts.add(facts.text(current));
|
|
1389
|
+
if (current.type === "CallExpression") current = current.callee;
|
|
1390
|
+
else if (current.type === "MemberExpression") current = current.object;
|
|
1391
|
+
else current = null;
|
|
1392
|
+
}
|
|
1393
|
+
return texts;
|
|
1394
|
+
}
|
|
1395
|
+
function collectCall(facts, node) {
|
|
1396
|
+
if (facts.text(node.callee) === "Promise.reject") {
|
|
1397
|
+
facts.throwRanges.push(node.range);
|
|
1398
|
+
const statement = node.parent;
|
|
1399
|
+
if (statement?.type === "ReturnStatement" && statement.parent !== null) facts.throwBlocks.push(statement.parent);
|
|
1400
|
+
}
|
|
1401
|
+
const scope = runningScope(node);
|
|
1402
|
+
if (scope === null) return;
|
|
1403
|
+
const target = node.callee.type === "Identifier" ? calledFunction(facts.source, node.callee) : null;
|
|
1404
|
+
if (target !== null && node.callee.type === "Identifier") facts.sites.push({
|
|
1405
|
+
call: node,
|
|
1406
|
+
scope,
|
|
1407
|
+
name: node.callee.name,
|
|
1408
|
+
target
|
|
1409
|
+
});
|
|
1410
|
+
const read = calledMember(node, READ_METHOD);
|
|
1411
|
+
if (read !== null) facts.reads.push({
|
|
1412
|
+
call: node,
|
|
1413
|
+
binding: readBinding(facts.source, node),
|
|
1414
|
+
range: node.range,
|
|
1415
|
+
scope,
|
|
1416
|
+
receiver: facts.text(read.object),
|
|
1417
|
+
receivers: receiverChain(facts, read.object)
|
|
1418
|
+
});
|
|
1419
|
+
const insert = calledMember(node, INSERT_METHOD);
|
|
1420
|
+
if (insert !== null && !isUpsert(node)) facts.inserts.push({
|
|
1421
|
+
call: node,
|
|
1422
|
+
scope,
|
|
1423
|
+
callee: insert,
|
|
1424
|
+
receiver: facts.text(insert.object),
|
|
1425
|
+
root: rootName(insert.object)
|
|
1426
|
+
});
|
|
1427
|
+
}
|
|
1428
|
+
const RACE = "Two replicas that start on an empty table both read nothing and both insert, so one writes a duplicate row or fails on the key. Key the row by a stable id, wrap the insert in try/catch, read the row back in the catch, and rethrow only when it is still missing, or upsert with `insert(row, { conflict: \"update\" })`.";
|
|
1429
|
+
/** Writing a row that a startup read found missing races every other replica that read the same, whether the insert sits in the reading function or in a function of the file it calls only when the read found nothing. A catch that rethrows nothing, recognises a duplicate in a condition, or reads the row back and branches, tolerates the lost race, and so does an upsert or a helper handed the table. */
|
|
1169
1430
|
const noReadThenInsertRule = defineRule({
|
|
1170
1431
|
meta: {
|
|
1171
1432
|
type: "problem",
|
|
1172
|
-
docs: { description: "Reject an insert that follows a read of the same table unless a catch tolerates losing it." },
|
|
1173
|
-
messages: {
|
|
1433
|
+
docs: { description: "Reject an insert that follows a read of the same table, or that a called function makes when a read found nothing, unless a catch tolerates losing it." },
|
|
1434
|
+
messages: {
|
|
1435
|
+
readThenInsert: `\`{{receiver}}\` is read and then inserted into. ${RACE}`,
|
|
1436
|
+
readThenCalledInsert: `\`{{read}}\` found nothing, so \`{{call}}\` runs and inserts into \`{{receiver}}\`. ${RACE}`
|
|
1437
|
+
}
|
|
1174
1438
|
},
|
|
1175
1439
|
createOnce(context) {
|
|
1176
|
-
const inserts = [];
|
|
1177
1440
|
const facts = {
|
|
1178
1441
|
reads: [],
|
|
1442
|
+
inserts: [],
|
|
1179
1443
|
duplicateChecks: [],
|
|
1180
1444
|
throwRanges: [],
|
|
1181
1445
|
throwBlocks: [],
|
|
1182
1446
|
exits: [],
|
|
1447
|
+
conditionals: [],
|
|
1448
|
+
sites: [],
|
|
1449
|
+
get source() {
|
|
1450
|
+
return context.sourceCode;
|
|
1451
|
+
},
|
|
1183
1452
|
text: (node) => context.sourceCode.getText(node)
|
|
1184
1453
|
};
|
|
1185
|
-
const
|
|
1186
|
-
|
|
1187
|
-
const texts = /* @__PURE__ */ new Set();
|
|
1188
|
-
let current = start;
|
|
1189
|
-
while (current !== null) {
|
|
1190
|
-
texts.add(facts.text(current));
|
|
1191
|
-
if (current.type === "CallExpression") current = current.callee;
|
|
1192
|
-
else if (current.type === "MemberExpression") current = current.object;
|
|
1193
|
-
else current = null;
|
|
1194
|
-
}
|
|
1195
|
-
return texts;
|
|
1196
|
-
}
|
|
1454
|
+
const exit = (node) => facts.exits.push(node);
|
|
1455
|
+
const conditional = (node) => facts.conditionals.push(node);
|
|
1197
1456
|
return {
|
|
1198
1457
|
before() {
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1458
|
+
for (const list of [
|
|
1459
|
+
facts.reads,
|
|
1460
|
+
facts.inserts,
|
|
1461
|
+
facts.duplicateChecks,
|
|
1462
|
+
facts.throwRanges,
|
|
1463
|
+
facts.throwBlocks,
|
|
1464
|
+
facts.exits,
|
|
1465
|
+
facts.conditionals,
|
|
1466
|
+
facts.sites
|
|
1467
|
+
]) list.length = 0;
|
|
1205
1468
|
},
|
|
1206
1469
|
ThrowStatement(node) {
|
|
1207
|
-
throwRanges.push(node.range);
|
|
1470
|
+
facts.throwRanges.push(node.range);
|
|
1208
1471
|
if (node.parent !== null) facts.throwBlocks.push(node.parent);
|
|
1209
1472
|
},
|
|
1210
|
-
ReturnStatement
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
BreakStatement(node) {
|
|
1217
|
-
exits.push(node);
|
|
1218
|
-
},
|
|
1473
|
+
ReturnStatement: exit,
|
|
1474
|
+
ContinueStatement: exit,
|
|
1475
|
+
BreakStatement: exit,
|
|
1476
|
+
IfStatement: conditional,
|
|
1477
|
+
ConditionalExpression: conditional,
|
|
1478
|
+
LogicalExpression: conditional,
|
|
1219
1479
|
Identifier(node) {
|
|
1220
|
-
if (DUPLICATE_CHECK.test(node.name)) duplicateChecks.push(node.range);
|
|
1480
|
+
if (DUPLICATE_CHECK.test(node.name)) facts.duplicateChecks.push(node.range);
|
|
1221
1481
|
},
|
|
1222
1482
|
TemplateLiteral(node) {
|
|
1223
|
-
if (node.quasis.some((quasi) => DUPLICATE_CHECK.test(quasi.value.raw))) duplicateChecks.push(node.range);
|
|
1483
|
+
if (node.quasis.some((quasi) => DUPLICATE_CHECK.test(quasi.value.raw))) facts.duplicateChecks.push(node.range);
|
|
1224
1484
|
},
|
|
1225
1485
|
Literal(node) {
|
|
1226
|
-
if (DUPLICATE_CHECK.test(String(node.value))) duplicateChecks.push(node.range);
|
|
1486
|
+
if (DUPLICATE_CHECK.test(String(node.value))) facts.duplicateChecks.push(node.range);
|
|
1227
1487
|
},
|
|
1228
1488
|
CallExpression(node) {
|
|
1229
|
-
|
|
1230
|
-
throwRanges.push(node.range);
|
|
1231
|
-
const statement = node.parent;
|
|
1232
|
-
if (statement?.type === "ReturnStatement" && statement.parent !== null) facts.throwBlocks.push(statement.parent);
|
|
1233
|
-
}
|
|
1234
|
-
const read = calledMember(node, READ_METHODS);
|
|
1235
|
-
const scope = runningScope(node);
|
|
1236
|
-
if (read !== null && scope !== null) reads.push({
|
|
1237
|
-
range: node.range,
|
|
1238
|
-
scope,
|
|
1239
|
-
receiver: facts.text(read.object),
|
|
1240
|
-
receivers: receiverChain(read.object)
|
|
1241
|
-
});
|
|
1242
|
-
const insert = calledMember(node, INSERT_METHODS);
|
|
1243
|
-
if (insert !== null && scope !== null && !isUpsert(node)) inserts.push({
|
|
1244
|
-
call: node,
|
|
1245
|
-
scope,
|
|
1246
|
-
callee: insert,
|
|
1247
|
-
receiver: facts.text(insert.object),
|
|
1248
|
-
root: rootName(insert.object)
|
|
1249
|
-
});
|
|
1489
|
+
collectCall(facts, node);
|
|
1250
1490
|
},
|
|
1251
1491
|
"Program:exit"() {
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1492
|
+
const reported = /* @__PURE__ */ new Set();
|
|
1493
|
+
for (const insert of facts.inserts) {
|
|
1494
|
+
if (!isRaced(facts, insert)) continue;
|
|
1495
|
+
reported.add(insert);
|
|
1496
|
+
context.report({
|
|
1497
|
+
node: insert.callee,
|
|
1498
|
+
messageId: "readThenInsert",
|
|
1499
|
+
data: { receiver: insert.receiver }
|
|
1500
|
+
});
|
|
1501
|
+
}
|
|
1502
|
+
for (const { insert, read, site } of racedThroughCalls(facts)) {
|
|
1503
|
+
if (reported.has(insert)) continue;
|
|
1504
|
+
reported.add(insert);
|
|
1505
|
+
context.report({
|
|
1506
|
+
node: insert.callee,
|
|
1507
|
+
messageId: "readThenCalledInsert",
|
|
1508
|
+
data: {
|
|
1509
|
+
read: facts.text(read.call.callee),
|
|
1510
|
+
call: site.name,
|
|
1511
|
+
receiver: insert.receiver
|
|
1512
|
+
}
|
|
1513
|
+
});
|
|
1514
|
+
}
|
|
1257
1515
|
}
|
|
1258
1516
|
};
|
|
1259
1517
|
}
|
|
@@ -1688,46 +1946,150 @@ function installsInterfaceDatabase(directory) {
|
|
|
1688
1946
|
installs.set(directory, found);
|
|
1689
1947
|
return found;
|
|
1690
1948
|
}
|
|
1949
|
+
function isFunction(node) {
|
|
1950
|
+
return node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression";
|
|
1951
|
+
}
|
|
1691
1952
|
function enclosingFunction(node) {
|
|
1692
|
-
for (let current = node.parent; current !== null; current = current.parent) if (current
|
|
1953
|
+
for (let current = node.parent; current !== null; current = current.parent) if (isFunction(current)) return current;
|
|
1693
1954
|
return null;
|
|
1694
1955
|
}
|
|
1956
|
+
/** Whether `call` hands its arguments to a promise that awaits what they return: `then`, `catch` or `finally`. */
|
|
1957
|
+
function isPromiseCallback(call) {
|
|
1958
|
+
return call.callee.type === "MemberExpression" && PROMISE_CALLBACK_METHODS.has(memberName(call.callee) ?? "");
|
|
1959
|
+
}
|
|
1695
1960
|
/** Whether the caller awaits what the function returns: an async function, or a callback handed to `then`, `catch` or `finally`. */
|
|
1696
1961
|
function resultIsAwaited(fn) {
|
|
1697
1962
|
if (fn.async) return true;
|
|
1698
1963
|
const call = fn.parent;
|
|
1699
|
-
return call?.type === "CallExpression" && call.arguments.some((argument) => argument === fn) &&
|
|
1964
|
+
return call?.type === "CallExpression" && call.arguments.some((argument) => argument === fn) && isPromiseCallback(call);
|
|
1965
|
+
}
|
|
1966
|
+
/** Whether `call` calls the first parameter of a function it sits in, as a `new Promise` executor calls its `resolve`. */
|
|
1967
|
+
function callsFirstParameter(call) {
|
|
1968
|
+
const { callee } = call;
|
|
1969
|
+
if (callee.type !== "Identifier") return false;
|
|
1970
|
+
for (let fn = enclosingFunction(call); fn !== null; fn = enclosingFunction(fn)) {
|
|
1971
|
+
const [first] = fn.params;
|
|
1972
|
+
const name = first?.type === "AssignmentPattern" ? first.left : first;
|
|
1973
|
+
if (name?.type === "Identifier" && name.name === callee.name) return true;
|
|
1974
|
+
}
|
|
1975
|
+
return false;
|
|
1976
|
+
}
|
|
1977
|
+
const HOLDERS = ts.SymbolFlags.ObjectLiteral | ts.SymbolFlags.Interface | ts.SymbolFlags.TypeLiteral;
|
|
1978
|
+
function declaredIn(symbol, path) {
|
|
1979
|
+
return symbol?.declarations?.some((declaration) => declaration.getSourceFile().fileName.includes(path)) === true;
|
|
1700
1980
|
}
|
|
1701
1981
|
/** Whether `type` is an interface-database query: its `run()` comes from that package. */
|
|
1702
1982
|
function isQuery(checker, type) {
|
|
1703
|
-
return (
|
|
1983
|
+
return declaredIn(checker.getPropertyOfType(type, "run"), INTERFACE_DATABASE);
|
|
1984
|
+
}
|
|
1985
|
+
/** Whether `type` is an interface-database `Table`, alone, extended or intersected, which a value holds as a handle to query later rather than as a query to run: only a table has that package's `insert()`. */
|
|
1986
|
+
function isTable(checker, type) {
|
|
1987
|
+
return declaredIn(checker.getPropertyOfType(type, "insert"), INTERFACE_DATABASE);
|
|
1988
|
+
}
|
|
1989
|
+
/** Whether `type` is an object literal, or an interface or object type the project declares, as opposed to a class instance or a library type. */
|
|
1990
|
+
function isHolder(type) {
|
|
1991
|
+
const symbol = type.getSymbol();
|
|
1992
|
+
return symbol !== void 0 && (symbol.flags & HOLDERS) !== 0 && (symbol.flags & ts.SymbolFlags.Class) === 0 && !declaredIn(symbol, "/node_modules/");
|
|
1993
|
+
}
|
|
1994
|
+
function members$1(type) {
|
|
1995
|
+
return type.isUnion() ? type.types : [type];
|
|
1704
1996
|
}
|
|
1705
|
-
/**
|
|
1997
|
+
/** The values an array, a tuple or a holder keeps, which awaiting it leaves as they are. */
|
|
1998
|
+
function heldValues(checker, type) {
|
|
1999
|
+
if (checker.isArrayType(type) || checker.isTupleType(type)) {
|
|
2000
|
+
const element = checker.getIndexTypeOfType(type, ts.IndexKind.Number);
|
|
2001
|
+
return element === void 0 ? [] : [element];
|
|
2002
|
+
}
|
|
2003
|
+
return isHolder(type) ? checker.getPropertiesOfType(type).map((property) => checker.getTypeOfSymbol(property)) : [];
|
|
2004
|
+
}
|
|
2005
|
+
/** Whether an array, a tuple or a holder of `type` keeps a query other than a table at any depth. */
|
|
2006
|
+
function holdsHeldQuery(checker, type, visited) {
|
|
2007
|
+
return heldValues(checker, type).some((value) => members$1(value).some((member) => {
|
|
2008
|
+
if (isQuery(checker, member)) return !isTable(checker, member);
|
|
2009
|
+
if (visited.has(member)) return false;
|
|
2010
|
+
visited.add(member);
|
|
2011
|
+
return holdsHeldQuery(checker, member, visited);
|
|
2012
|
+
}));
|
|
2013
|
+
}
|
|
2014
|
+
/** Whether an awaiting caller receives an unrun query from a value of `type`: the query itself, possibly behind a promise, or a query other than a table that arrays, tuples, object literals and project-declared object types keep. */
|
|
2015
|
+
function holdsQuery(checker, type) {
|
|
2016
|
+
const visited = /* @__PURE__ */ new Set();
|
|
2017
|
+
return members$1(checker.getAwaitedType(type) ?? type).some((member) => isQuery(checker, member) || holdsHeldQuery(checker, member, visited));
|
|
2018
|
+
}
|
|
2019
|
+
/** What a function value handed to `then`, `catch` or `finally` resolves the promise to: the return types of its non-async call signatures, since an async one reports its own returns. */
|
|
2020
|
+
function callbackResult(checker, callback) {
|
|
2021
|
+
return checker.getTypeAtLocation(callback).getCallSignatures().filter(({ declaration }) => declaration === void 0 || (ts.getCombinedModifierFlags(declaration) & ts.ModifierFlags.Async) === 0).map((signature) => checker.getReturnTypeOfSignature(signature));
|
|
2022
|
+
}
|
|
2023
|
+
const executorsByFile = /* @__PURE__ */ new WeakMap();
|
|
2024
|
+
/** The function a `new Promise` argument names: the function written there, or the function declaration or initialised variable its identifier resolves to. */
|
|
2025
|
+
function namedFunction(checker, argument) {
|
|
2026
|
+
if (ts.isFunctionExpression(argument) || ts.isArrowFunction(argument)) return argument;
|
|
2027
|
+
if (!ts.isIdentifier(argument)) return void 0;
|
|
2028
|
+
const declaration = checker.getSymbolAtLocation(argument)?.valueDeclaration;
|
|
2029
|
+
return declaration !== void 0 && ts.isVariableDeclaration(declaration) && declaration.initializer !== void 0 ? declaration.initializer : declaration;
|
|
2030
|
+
}
|
|
2031
|
+
/** The functions `sourceFile` hands to `new Promise` as its executor, inline or by name. */
|
|
2032
|
+
function promiseExecutors(checker, sourceFile) {
|
|
2033
|
+
const known = executorsByFile.get(sourceFile);
|
|
2034
|
+
if (known !== void 0) return known;
|
|
2035
|
+
const executors = /* @__PURE__ */ new Set();
|
|
2036
|
+
const visit = (node) => {
|
|
2037
|
+
const argument = ts.isNewExpression(node) && ts.isIdentifier(node.expression) && node.expression.text === "Promise" ? node.arguments?.[0] : void 0;
|
|
2038
|
+
const executor = argument && namedFunction(checker, argument);
|
|
2039
|
+
if (executor !== void 0) executors.add(executor);
|
|
2040
|
+
ts.forEachChild(node, visit);
|
|
2041
|
+
};
|
|
2042
|
+
visit(sourceFile);
|
|
2043
|
+
executorsByFile.set(sourceFile, executors);
|
|
2044
|
+
return executors;
|
|
2045
|
+
}
|
|
2046
|
+
/** The value `call` resolves its promise with, when its callee is the first parameter of a function the file hands to `new Promise` as its executor. */
|
|
2047
|
+
function resolvedValue(checker, call) {
|
|
2048
|
+
const parameter = checker.getSymbolAtLocation(call.expression)?.valueDeclaration;
|
|
2049
|
+
const executor = parameter?.parent;
|
|
2050
|
+
return parameter !== void 0 && ts.isParameter(parameter) && executor !== void 0 && ts.isFunctionLike(executor) && executor.parameters[0] === parameter && promiseExecutors(checker, call.getSourceFile()).has(executor) ? call.arguments[0] : void 0;
|
|
2051
|
+
}
|
|
2052
|
+
/** Since interface-database 0.4.0 a query is no longer thenable, so an awaited function that returns one, alone or wrapped, resolves to the unrun query and sends nothing. */
|
|
1706
2053
|
const noUnrunQueryRule = defineRule({
|
|
1707
2054
|
meta: {
|
|
1708
2055
|
type: "problem",
|
|
1709
|
-
docs: { description: "An async function or promise
|
|
1710
|
-
messages: { unrun: "This
|
|
2056
|
+
docs: { description: "An async function, promise callback or promise executor resolves to no unrun interface-database query." },
|
|
2057
|
+
messages: { unrun: "This resolves to an interface-database query without running it, so the caller awaits the query object and nothing reaches the database. Call `.run()` on it." }
|
|
1711
2058
|
},
|
|
1712
2059
|
createOnce(context) {
|
|
1713
|
-
function check(value,
|
|
2060
|
+
function check(value, resolved) {
|
|
1714
2061
|
if (!installsInterfaceDatabase(dirname(resolve(context.filename)))) return;
|
|
1715
2062
|
const typed = typedFile(context.filename, context.sourceCode.text);
|
|
1716
|
-
|
|
1717
|
-
if (typed !== void 0 && expression !== void 0 && isQuery(typed.checker, typed.checker.getTypeAtLocation(expression))) context.report({
|
|
2063
|
+
if (typed !== void 0 && resolved(typed).some((type) => holdsQuery(typed.checker, type))) context.report({
|
|
1718
2064
|
node: value,
|
|
1719
2065
|
messageId: "unrun"
|
|
1720
2066
|
});
|
|
1721
2067
|
}
|
|
2068
|
+
function typeOf(node, { checker }) {
|
|
2069
|
+
return node === void 0 ? [] : [checker.getTypeAtLocation(node)];
|
|
2070
|
+
}
|
|
1722
2071
|
return {
|
|
1723
2072
|
ReturnStatement(node) {
|
|
1724
2073
|
const fn = enclosingFunction(node);
|
|
1725
2074
|
if (fn === null || node.argument === null || !resultIsAwaited(fn)) return;
|
|
1726
|
-
check(node.argument, (
|
|
2075
|
+
check(node.argument, (typed) => typeOf(nodeAt(typed.sourceFile, node, ts.isReturnStatement)?.expression, typed));
|
|
1727
2076
|
},
|
|
1728
2077
|
ArrowFunctionExpression(node) {
|
|
1729
2078
|
if (node.body.type === "BlockStatement" || !resultIsAwaited(node)) return;
|
|
1730
|
-
check(node.body, (
|
|
2079
|
+
check(node.body, (typed) => typeOf(nodeAt(typed.sourceFile, node, ts.isArrowFunction)?.body, typed));
|
|
2080
|
+
},
|
|
2081
|
+
CallExpression(node) {
|
|
2082
|
+
if (isPromiseCallback(node)) {
|
|
2083
|
+
for (const argument of node.arguments) if (!isFunction(argument)) check(argument, (typed) => {
|
|
2084
|
+
const callback = nodeAt(typed.sourceFile, argument, ts.isExpression);
|
|
2085
|
+
return callback === void 0 ? [] : callbackResult(typed.checker, callback);
|
|
2086
|
+
});
|
|
2087
|
+
}
|
|
2088
|
+
const [value] = node.arguments;
|
|
2089
|
+
if (value !== void 0 && callsFirstParameter(node)) check(value, (typed) => {
|
|
2090
|
+
const call = nodeAt(typed.sourceFile, node, ts.isCallExpression);
|
|
2091
|
+
return typeOf(call && resolvedValue(typed.checker, call), typed);
|
|
2092
|
+
});
|
|
1731
2093
|
}
|
|
1732
2094
|
};
|
|
1733
2095
|
}
|
|
@@ -2029,7 +2391,7 @@ const requireTsconfigTypesRule = defineRule({
|
|
|
2029
2391
|
});
|
|
2030
2392
|
//#endregion
|
|
2031
2393
|
//#region src/oxc/guardrails/index.ts
|
|
2032
|
-
/** AntelopeJS rules that reject comments, inherited-key lookups, presence-only type guards, inserts that follow a read of the same table, unrun interface-database queries an awaited function returns, unchecked Object.assign writes, low-evidence tests, files no tsconfig lists and tsconfigs that list no `types`. */
|
|
2394
|
+
/** AntelopeJS rules that reject comments, inherited-key lookups, presence-only type guards, inserts that follow a read of the same table or that a called function makes once an awaited read found nothing, unrun interface-database queries an awaited function returns, unchecked Object.assign writes, low-evidence tests, files no tsconfig lists and tsconfigs that list no `types`. */
|
|
2033
2395
|
const guardrailsPlugin = eslintCompatPlugin({
|
|
2034
2396
|
meta: { name: "antelopejs" },
|
|
2035
2397
|
rules: {
|