@antelopejs/tooling-configs 0.6.0 → 0.7.1
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 +10 -18
- 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-DJ_CFEZH.mjs} +446 -23
- package/dist/eslint.mjs +1 -1
- package/dist/{guardrails-C72rFG8J.mjs → guardrails-6i82vsUi.mjs} +839 -124
- package/dist/npm-CIIqL5SG.mjs +18 -0
- 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/oxc/lint.d.mts +5 -2
- package/dist/oxc/lint.mjs +1 -0
- package/dist/{oxlint-CCOJqj3d.mjs → oxlint-CIeonWMB.mjs} +37 -11
- package/dist/{ratchet-qu2aLzQU.mjs → ratchet-DLCfeaPY.mjs} +2 -1
- package/package.json +1 -1
|
@@ -414,7 +414,7 @@ function checksOwnKey(node, read) {
|
|
|
414
414
|
}
|
|
415
415
|
const EQUALITY = /* @__PURE__ */ new Set(["==", "==="]);
|
|
416
416
|
const INEQUALITY = /* @__PURE__ */ new Set(["!=", "!=="]);
|
|
417
|
-
const MAX_DEPTH$
|
|
417
|
+
const MAX_DEPTH$3 = 8;
|
|
418
418
|
const ASSERTS = /* @__PURE__ */ new Set([
|
|
419
419
|
"assert",
|
|
420
420
|
"assert.ok",
|
|
@@ -441,7 +441,7 @@ function comparedOutcome(node, outcome) {
|
|
|
441
441
|
}
|
|
442
442
|
/** A value that is `true` or `false`, so comparing it with a boolean literal tests its truth: a call, a `!`, or a `const` of one. */
|
|
443
443
|
function isBooleanValued(node, read, depth) {
|
|
444
|
-
if (depth > MAX_DEPTH$
|
|
444
|
+
if (depth > MAX_DEPTH$3) return false;
|
|
445
445
|
if (node.type === "ParenthesizedExpression") return isBooleanValued(node.expression, read, depth);
|
|
446
446
|
if (node.type === "Identifier") {
|
|
447
447
|
const value = constValue(node, read);
|
|
@@ -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) {
|
|
481
|
-
if (depth > MAX_DEPTH$
|
|
480
|
+
function holds$1(node, outcome, read, depth = 0) {
|
|
481
|
+
if (depth > MAX_DEPTH$3) 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
|
+
});
|
|
1383
|
+
}
|
|
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;
|
|
1167
1394
|
}
|
|
1168
|
-
|
|
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
|
}
|
|
@@ -1581,6 +1839,29 @@ function scriptView(sfc) {
|
|
|
1581
1839
|
}
|
|
1582
1840
|
return end === 0 ? void 0 : `${view}${blank(sfc.slice(end))}${MODULE}`;
|
|
1583
1841
|
}
|
|
1842
|
+
/**
|
|
1843
|
+
* The text to type for a linted file. A component whose linted text holds no
|
|
1844
|
+
* script block, as oxlint passes a component's script alone, is read whole
|
|
1845
|
+
* from disk, and `offset` is where that script starts inside the block whose
|
|
1846
|
+
* contents it is, which oxlint passes trimmed. Undefined for a component with no such block, or that
|
|
1847
|
+
* declares `generic` type parameters.
|
|
1848
|
+
*/
|
|
1849
|
+
function lintedSource(filename, text) {
|
|
1850
|
+
if (!filename.endsWith(".vue")) return {
|
|
1851
|
+
text,
|
|
1852
|
+
offset: 0
|
|
1853
|
+
};
|
|
1854
|
+
if (scriptView(text) !== void 0) return declaresTypeParameters(text) ? void 0 : {
|
|
1855
|
+
text,
|
|
1856
|
+
offset: 0
|
|
1857
|
+
};
|
|
1858
|
+
const component = readFileSync(filename, "utf8");
|
|
1859
|
+
const block = Array.from(component.matchAll(BLOCK)).find(({ 1: open, 2: content }) => open !== void 0 && content?.trim() === text.trim());
|
|
1860
|
+
return block?.[1] === void 0 || block[2] === void 0 || declaresTypeParameters(component) ? void 0 : {
|
|
1861
|
+
text: component,
|
|
1862
|
+
offset: block.index + block[1].length + block[2].indexOf(text)
|
|
1863
|
+
};
|
|
1864
|
+
}
|
|
1584
1865
|
/** Whether a script block of the component declares type parameters with `generic`, which only Vue's compiler brings into scope, so the view types them as error types. */
|
|
1585
1866
|
function declaresTypeParameters(sfc) {
|
|
1586
1867
|
return Array.from(sfc.matchAll(BLOCK)).some(({ 1: open }) => open !== void 0 && GENERIC.test(open));
|
|
@@ -1688,51 +1969,482 @@ function installsInterfaceDatabase(directory) {
|
|
|
1688
1969
|
installs.set(directory, found);
|
|
1689
1970
|
return found;
|
|
1690
1971
|
}
|
|
1972
|
+
function isFunction(node) {
|
|
1973
|
+
return node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression";
|
|
1974
|
+
}
|
|
1691
1975
|
function enclosingFunction(node) {
|
|
1692
|
-
for (let current = node.parent; current !== null; current = current.parent) if (current
|
|
1976
|
+
for (let current = node.parent; current !== null; current = current.parent) if (isFunction(current)) return current;
|
|
1693
1977
|
return null;
|
|
1694
1978
|
}
|
|
1979
|
+
/** Whether `call` hands its arguments to a promise that awaits what they return: `then`, `catch` or `finally`. */
|
|
1980
|
+
function isPromiseCallback(call) {
|
|
1981
|
+
return call.callee.type === "MemberExpression" && PROMISE_CALLBACK_METHODS.has(memberName(call.callee) ?? "");
|
|
1982
|
+
}
|
|
1695
1983
|
/** Whether the caller awaits what the function returns: an async function, or a callback handed to `then`, `catch` or `finally`. */
|
|
1696
1984
|
function resultIsAwaited(fn) {
|
|
1697
1985
|
if (fn.async) return true;
|
|
1698
1986
|
const call = fn.parent;
|
|
1699
|
-
return call?.type === "CallExpression" && call.arguments.some((argument) => argument === fn) &&
|
|
1987
|
+
return call?.type === "CallExpression" && call.arguments.some((argument) => argument === fn) && isPromiseCallback(call);
|
|
1988
|
+
}
|
|
1989
|
+
/** Whether `call` calls the first parameter after any `this` parameter of a function it sits in, as a `new Promise` executor calls its `resolve`. */
|
|
1990
|
+
function callsFirstParameter(call) {
|
|
1991
|
+
const { callee } = call;
|
|
1992
|
+
if (callee.type !== "Identifier") return false;
|
|
1993
|
+
for (let fn = enclosingFunction(call); fn !== null; fn = enclosingFunction(fn)) {
|
|
1994
|
+
const first = fn.params.find((param) => param.type !== "Identifier" || param.name !== "this");
|
|
1995
|
+
const name = first?.type === "AssignmentPattern" ? first.left : first;
|
|
1996
|
+
if (name?.type === "Identifier" && name.name === callee.name) return true;
|
|
1997
|
+
}
|
|
1998
|
+
return false;
|
|
1999
|
+
}
|
|
2000
|
+
const HOLDERS = ts.SymbolFlags.ObjectLiteral | ts.SymbolFlags.Interface | ts.SymbolFlags.TypeLiteral;
|
|
2001
|
+
function declaredIn(symbol, path) {
|
|
2002
|
+
return symbol?.declarations?.some((declaration) => declaration.getSourceFile().fileName.includes(path)) === true;
|
|
1700
2003
|
}
|
|
1701
2004
|
/** Whether `type` is an interface-database query: its `run()` comes from that package. */
|
|
1702
2005
|
function isQuery(checker, type) {
|
|
1703
|
-
return (
|
|
2006
|
+
return declaredIn(checker.getPropertyOfType(type, "run"), INTERFACE_DATABASE);
|
|
2007
|
+
}
|
|
2008
|
+
/** 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()`. */
|
|
2009
|
+
function isTable(checker, type) {
|
|
2010
|
+
return declaredIn(checker.getPropertyOfType(type, "insert"), INTERFACE_DATABASE);
|
|
2011
|
+
}
|
|
2012
|
+
/** Whether `type` is an object literal, an interface or object type the project declares, a mapped type such as `Readonly<T>` or `Record<K, V>`, or an intersection of those, as opposed to a class instance or a library type. */
|
|
2013
|
+
function isHolder(type) {
|
|
2014
|
+
if (type.isIntersection()) return type.types.every(isHolder);
|
|
2015
|
+
const symbol = type.getSymbol();
|
|
2016
|
+
if (symbol?.declarations?.some(ts.isMappedTypeNode) === true) return true;
|
|
2017
|
+
return symbol !== void 0 && (symbol.flags & HOLDERS) !== 0 && (symbol.flags & ts.SymbolFlags.Class) === 0 && !declaredIn(symbol, "/node_modules/");
|
|
1704
2018
|
}
|
|
1705
|
-
|
|
2019
|
+
function members$1(type) {
|
|
2020
|
+
return type.isUnion() ? type.types : [type];
|
|
2021
|
+
}
|
|
2022
|
+
/** The values an array, a tuple or a holder keeps in its properties and index signatures, which awaiting it leaves as they are. */
|
|
2023
|
+
function heldValues(checker, type) {
|
|
2024
|
+
if (checker.isArrayType(type) || checker.isTupleType(type)) {
|
|
2025
|
+
const element = checker.getIndexTypeOfType(type, ts.IndexKind.Number);
|
|
2026
|
+
return element === void 0 ? [] : [element];
|
|
2027
|
+
}
|
|
2028
|
+
if (!isHolder(type)) return [];
|
|
2029
|
+
return [...checker.getPropertiesOfType(type).map((property) => checker.getTypeOfSymbol(property)), ...checker.getIndexInfosOfType(type).map((info) => info.type)];
|
|
2030
|
+
}
|
|
2031
|
+
/** Whether an array, a tuple or a holder of `type` keeps a query other than a table at any depth. */
|
|
2032
|
+
function holdsHeldQuery(checker, type, visited) {
|
|
2033
|
+
return heldValues(checker, type).some((value) => members$1(value).some((member) => {
|
|
2034
|
+
if (isQuery(checker, member)) return !isTable(checker, member);
|
|
2035
|
+
if (visited.has(member)) return false;
|
|
2036
|
+
visited.add(member);
|
|
2037
|
+
return holdsHeldQuery(checker, member, visited);
|
|
2038
|
+
}));
|
|
2039
|
+
}
|
|
2040
|
+
/** 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. */
|
|
2041
|
+
function holdsQuery(checker, type) {
|
|
2042
|
+
const visited = /* @__PURE__ */ new Set();
|
|
2043
|
+
return members$1(checker.getAwaitedType(type) ?? type).some((member) => isQuery(checker, member) || holdsHeldQuery(checker, member, visited));
|
|
2044
|
+
}
|
|
2045
|
+
/** 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. */
|
|
2046
|
+
function callbackResult(checker, callback) {
|
|
2047
|
+
return checker.getTypeAtLocation(callback).getCallSignatures().filter(({ declaration }) => declaration === void 0 || (ts.getCombinedModifierFlags(declaration) & ts.ModifierFlags.Async) === 0).map((signature) => checker.getReturnTypeOfSignature(signature));
|
|
2048
|
+
}
|
|
2049
|
+
const executorsByFile = /* @__PURE__ */ new WeakMap();
|
|
2050
|
+
/** `expression` without the parentheses, `as`, `<T>`, `satisfies` and `!` around it. */
|
|
2051
|
+
function unwrapped(expression) {
|
|
2052
|
+
return ts.isParenthesizedExpression(expression) || ts.isAsExpression(expression) || ts.isTypeAssertionExpression(expression) || ts.isSatisfiesExpression(expression) || ts.isNonNullExpression(expression) ? unwrapped(expression.expression) : expression;
|
|
2053
|
+
}
|
|
2054
|
+
/** The function a declaration holds: the initialiser of a variable or property, the value a shorthand property names, or the declaration itself. */
|
|
2055
|
+
function declaredFunctions(checker, declaration) {
|
|
2056
|
+
if (ts.isShorthandPropertyAssignment(declaration)) return (checker.getShorthandAssignmentValueSymbol(declaration)?.declarations ?? []).flatMap((value) => declaredFunctions(checker, value));
|
|
2057
|
+
return (ts.isVariableDeclaration(declaration) || ts.isPropertyDeclaration(declaration) || ts.isPropertyAssignment(declaration)) && declaration.initializer !== void 0 ? [unwrapped(declaration.initializer)] : [declaration];
|
|
2058
|
+
}
|
|
2059
|
+
/** `value` as a member access the source names, or `undefined` for any other expression. */
|
|
2060
|
+
function namedMember(value) {
|
|
2061
|
+
if (ts.isPropertyAccessExpression(value)) return {
|
|
2062
|
+
receiver: value.expression,
|
|
2063
|
+
key: value,
|
|
2064
|
+
name: value.name.text
|
|
2065
|
+
};
|
|
2066
|
+
return ts.isElementAccessExpression(value) && ts.isStringLiteralLike(value.argumentExpression) ? {
|
|
2067
|
+
receiver: value.expression,
|
|
2068
|
+
key: value.argumentExpression,
|
|
2069
|
+
name: value.argumentExpression.text
|
|
2070
|
+
} : void 0;
|
|
2071
|
+
}
|
|
2072
|
+
/** The declarations of `member` on the value its receiver variable is initialised with, past any type annotation that names only a signature. */
|
|
2073
|
+
function initialisedMember(checker, member) {
|
|
2074
|
+
const receiver = ts.isIdentifier(member.receiver) ? checker.getSymbolAtLocation(member.receiver)?.valueDeclaration : void 0;
|
|
2075
|
+
return receiver !== void 0 && ts.isVariableDeclaration(receiver) && receiver.initializer !== void 0 ? checker.getTypeAtLocation(unwrapped(receiver.initializer)).getProperty(member.name)?.declarations ?? [] : [];
|
|
2076
|
+
}
|
|
2077
|
+
/** The functions a `new Promise` argument names: the function written there, or each declaration, overloads included, or initialiser of the variable, property or method its identifier, member access or literal-keyed element access resolves to. */
|
|
2078
|
+
function namedFunctions(checker, argument) {
|
|
2079
|
+
const value = unwrapped(argument);
|
|
2080
|
+
if (ts.isFunctionExpression(value) || ts.isArrowFunction(value)) return [value];
|
|
2081
|
+
const member = namedMember(value);
|
|
2082
|
+
if (!ts.isIdentifier(value) && member === void 0) return [];
|
|
2083
|
+
return [...checker.getSymbolAtLocation(member?.key ?? value)?.declarations ?? [], ...member === void 0 ? [] : initialisedMember(checker, member)].flatMap((declaration) => declaredFunctions(checker, declaration));
|
|
2084
|
+
}
|
|
2085
|
+
/** The functions `sourceFile` hands to `new Promise` as its executor, inline or by name. */
|
|
2086
|
+
function promiseExecutors(checker, sourceFile) {
|
|
2087
|
+
const known = executorsByFile.get(sourceFile);
|
|
2088
|
+
if (known !== void 0) return known;
|
|
2089
|
+
const executors = /* @__PURE__ */ new Set();
|
|
2090
|
+
const visit = (node) => {
|
|
2091
|
+
const argument = ts.isNewExpression(node) && ts.isIdentifier(node.expression) && node.expression.text === "Promise" ? node.arguments?.[0] : void 0;
|
|
2092
|
+
for (const executor of argument ? namedFunctions(checker, argument) : []) executors.add(executor);
|
|
2093
|
+
ts.forEachChild(node, visit);
|
|
2094
|
+
};
|
|
2095
|
+
visit(sourceFile);
|
|
2096
|
+
executorsByFile.set(sourceFile, executors);
|
|
2097
|
+
return executors;
|
|
2098
|
+
}
|
|
2099
|
+
/** The value `call` resolves its promise with, when its callee is the first parameter after any `this` parameter of a function the file hands to `new Promise` as its executor. */
|
|
2100
|
+
function resolvedValue(checker, call) {
|
|
2101
|
+
const parameter = checker.getSymbolAtLocation(call.expression)?.valueDeclaration;
|
|
2102
|
+
const executor = parameter?.parent;
|
|
2103
|
+
return parameter !== void 0 && ts.isParameter(parameter) && executor !== void 0 && ts.isFunctionLike(executor) && executor.parameters.find(({ name }) => !ts.isIdentifier(name) || name.text !== "this") === parameter && promiseExecutors(checker, call.getSourceFile()).has(executor) ? call.arguments[0] : void 0;
|
|
2104
|
+
}
|
|
2105
|
+
/** 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
2106
|
const noUnrunQueryRule = defineRule({
|
|
1707
2107
|
meta: {
|
|
1708
2108
|
type: "problem",
|
|
1709
|
-
docs: { description: "An async function or promise
|
|
1710
|
-
messages: { unrun: "This
|
|
2109
|
+
docs: { description: "An async function, promise callback or promise executor resolves to no unrun interface-database query." },
|
|
2110
|
+
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
2111
|
},
|
|
1712
2112
|
createOnce(context) {
|
|
1713
|
-
function check(value,
|
|
2113
|
+
function check(value, resolved) {
|
|
1714
2114
|
if (!installsInterfaceDatabase(dirname(resolve(context.filename)))) return;
|
|
1715
2115
|
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({
|
|
2116
|
+
if (typed !== void 0 && resolved(typed).some((type) => holdsQuery(typed.checker, type))) context.report({
|
|
1718
2117
|
node: value,
|
|
1719
2118
|
messageId: "unrun"
|
|
1720
2119
|
});
|
|
1721
2120
|
}
|
|
2121
|
+
function typeOf(node, { checker }) {
|
|
2122
|
+
return node === void 0 ? [] : [checker.getTypeAtLocation(node)];
|
|
2123
|
+
}
|
|
1722
2124
|
return {
|
|
1723
2125
|
ReturnStatement(node) {
|
|
1724
2126
|
const fn = enclosingFunction(node);
|
|
1725
2127
|
if (fn === null || node.argument === null || !resultIsAwaited(fn)) return;
|
|
1726
|
-
check(node.argument, (
|
|
2128
|
+
check(node.argument, (typed) => typeOf(nodeAt(typed.sourceFile, node, ts.isReturnStatement)?.expression, typed));
|
|
1727
2129
|
},
|
|
1728
2130
|
ArrowFunctionExpression(node) {
|
|
1729
2131
|
if (node.body.type === "BlockStatement" || !resultIsAwaited(node)) return;
|
|
1730
|
-
check(node.body, (
|
|
2132
|
+
check(node.body, (typed) => typeOf(nodeAt(typed.sourceFile, node, ts.isArrowFunction)?.body, typed));
|
|
2133
|
+
},
|
|
2134
|
+
CallExpression(node) {
|
|
2135
|
+
if (isPromiseCallback(node)) {
|
|
2136
|
+
for (const argument of node.arguments) if (!isFunction(argument)) check(argument, (typed) => {
|
|
2137
|
+
const callback = nodeAt(typed.sourceFile, argument, ts.isExpression);
|
|
2138
|
+
return callback === void 0 ? [] : callbackResult(typed.checker, callback);
|
|
2139
|
+
});
|
|
2140
|
+
}
|
|
2141
|
+
const [value] = node.arguments;
|
|
2142
|
+
if (value !== void 0 && callsFirstParameter(node)) check(value, (typed) => {
|
|
2143
|
+
const call = nodeAt(typed.sourceFile, node, ts.isCallExpression);
|
|
2144
|
+
return typeOf(call && resolvedValue(typed.checker, call), typed);
|
|
2145
|
+
});
|
|
1731
2146
|
}
|
|
1732
2147
|
};
|
|
1733
2148
|
}
|
|
1734
2149
|
});
|
|
1735
2150
|
//#endregion
|
|
2151
|
+
//#region src/oxc/guardrails/rules/no-unsafe-any-in-vue.ts
|
|
2152
|
+
const UNTYPED$1 = ts.TypeFlags.Any | ts.TypeFlags.Unknown;
|
|
2153
|
+
const MAX_DEPTH$2 = 4;
|
|
2154
|
+
/** Whether `type` is `any` itself, not an error type from an import the program cannot resolve, such as another component. */
|
|
2155
|
+
function isAnyType(checker, type) {
|
|
2156
|
+
return type === checker.getAnyType();
|
|
2157
|
+
}
|
|
2158
|
+
function isAny(checker, node) {
|
|
2159
|
+
return isAnyType(checker, checker.getTypeAtLocation(node));
|
|
2160
|
+
}
|
|
2161
|
+
function takesAnything(type) {
|
|
2162
|
+
return type === void 0 || (type.flags & UNTYPED$1) !== 0;
|
|
2163
|
+
}
|
|
2164
|
+
function isObjectType(type) {
|
|
2165
|
+
return (type.flags & ts.TypeFlags.Object) !== 0;
|
|
2166
|
+
}
|
|
2167
|
+
function isReference(type) {
|
|
2168
|
+
return (type.objectFlags & ts.ObjectFlags.Reference) !== 0;
|
|
2169
|
+
}
|
|
2170
|
+
/** The array or tuple reference `type` is, for comparing by its generic target as tsgolint does. */
|
|
2171
|
+
function arrayReference(checker, type) {
|
|
2172
|
+
return (checker.isArrayType(type) || checker.isTupleType(type)) && isObjectType(type) && isReference(type) ? type : void 0;
|
|
2173
|
+
}
|
|
2174
|
+
/**
|
|
2175
|
+
* Whether a `sender` value lets `any` into a `receiver` slot as an argument,
|
|
2176
|
+
* a return or a type argument: `any` into a slot that is undeclared or typed
|
|
2177
|
+
* neither `any` nor `unknown`, or an array or tuple holding `any`, as in
|
|
2178
|
+
* `any[]`, into one of the same generic target, such as `string[]`, with
|
|
2179
|
+
* another type in that position. `readonly` and labelled tuples have targets
|
|
2180
|
+
* of their own.
|
|
2181
|
+
*/
|
|
2182
|
+
function leaks(checker, sender, receiver, depth = 0) {
|
|
2183
|
+
if (isAnyType(checker, sender)) return receiver === void 0 || !takesAnything(receiver);
|
|
2184
|
+
if (receiver === void 0 || depth === MAX_DEPTH$2) return false;
|
|
2185
|
+
const from = arrayReference(checker, sender);
|
|
2186
|
+
const to = arrayReference(checker, receiver);
|
|
2187
|
+
if (from === void 0 || to === void 0 || from.target !== to.target) return false;
|
|
2188
|
+
const slots = checker.getTypeArguments(to);
|
|
2189
|
+
return checker.getTypeArguments(from).some((argument, index) => leaks(checker, argument, slots[index], depth + 1));
|
|
2190
|
+
}
|
|
2191
|
+
/** Whether `sender` lets `any` into an assigned `receiver`: `any` into anything but `unknown`, a slot declared `any` included, or what {@link leaks} finds. */
|
|
2192
|
+
function assignmentLeaks(checker, sender, receiver) {
|
|
2193
|
+
return isAnyType(checker, sender) ? receiver === void 0 || (receiver.flags & ts.TypeFlags.Unknown) === 0 : leaks(checker, sender, receiver);
|
|
2194
|
+
}
|
|
2195
|
+
/** Whether `value`, flowing into the slot its context declares, lets `any` in. */
|
|
2196
|
+
function leaksIntoContext(checker, value) {
|
|
2197
|
+
return assignmentLeaks(checker, checker.getTypeAtLocation(value), checker.getContextualType(value));
|
|
2198
|
+
}
|
|
2199
|
+
/** Whether `literal` is the target of a destructuring assignment, as in `({ a } = value)`, whose elements name targets, not values. */
|
|
2200
|
+
function isDestructuringTarget(literal) {
|
|
2201
|
+
let child = literal;
|
|
2202
|
+
let parent = literal.parent;
|
|
2203
|
+
while (ts.isParenthesizedExpression(parent) || ts.isArrayLiteralExpression(parent) || ts.isObjectLiteralExpression(parent) || ts.isPropertyAssignment(parent) || ts.isShorthandPropertyAssignment(parent) || ts.isSpreadElement(parent) || ts.isSpreadAssignment(parent)) {
|
|
2204
|
+
child = parent;
|
|
2205
|
+
parent = parent.parent;
|
|
2206
|
+
}
|
|
2207
|
+
return ts.isBinaryExpression(parent) && parent.operatorToken.kind === ts.SyntaxKind.EqualsToken && parent.left === child || (ts.isForOfStatement(parent) || ts.isForInStatement(parent)) && parent.initializer === child;
|
|
2208
|
+
}
|
|
2209
|
+
function declaredReturn(checker, node) {
|
|
2210
|
+
let owner = node.parent;
|
|
2211
|
+
while (!ts.isFunctionLike(owner)) {
|
|
2212
|
+
if (ts.isSourceFile(owner)) return void 0;
|
|
2213
|
+
owner = owner.parent;
|
|
2214
|
+
}
|
|
2215
|
+
const contextual = ts.isExpression(owner) ? checker.getContextualType(owner) : void 0;
|
|
2216
|
+
const declared = owner.type === void 0 ? contextual && checker.getNonNullableType(contextual).getCallSignatures()[0]?.getReturnType() : checker.getTypeFromTypeNode(owner.type);
|
|
2217
|
+
const isAsync = ts.canHaveModifiers(owner) && ts.getModifiers(owner)?.some((modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword);
|
|
2218
|
+
return declared && isAsync ? checker.getAwaitedType(declared) : declared;
|
|
2219
|
+
}
|
|
2220
|
+
function argumentFindings(checker, call) {
|
|
2221
|
+
return (call.arguments ?? []).flatMap((argument) => {
|
|
2222
|
+
const parameter = checker.getContextualType(argument);
|
|
2223
|
+
return ts.isSpreadElement(argument) || parameter === void 0 || !leaks(checker, checker.getTypeAtLocation(argument), parameter) ? [] : [{
|
|
2224
|
+
kind: "argument",
|
|
2225
|
+
node: argument
|
|
2226
|
+
}];
|
|
2227
|
+
});
|
|
2228
|
+
}
|
|
2229
|
+
function returned(checker, value) {
|
|
2230
|
+
return leaks(checker, checker.getTypeAtLocation(value), declaredReturn(checker, value)) ? [{
|
|
2231
|
+
kind: "return",
|
|
2232
|
+
node: value
|
|
2233
|
+
}] : [];
|
|
2234
|
+
}
|
|
2235
|
+
/** Whether `type` is `any` or an array of `any`, as a spread or rest holds it. */
|
|
2236
|
+
function isAnyOrAnyArray(checker, type) {
|
|
2237
|
+
const element = checker.isArrayType(type) ? checker.getIndexTypeOfType(type, ts.IndexKind.Number) : void 0;
|
|
2238
|
+
return isAnyType(checker, type) || element !== void 0 && isAnyType(checker, element);
|
|
2239
|
+
}
|
|
2240
|
+
function spreadFindings(checker, spread) {
|
|
2241
|
+
const { parent } = spread;
|
|
2242
|
+
return !((ts.isCallExpression(parent) || ts.isNewExpression(parent)) && isAny(checker, parent.expression)) && !isDestructuringTarget(parent) && isAnyOrAnyArray(checker, checker.getTypeAtLocation(spread.expression)) ? [{
|
|
2243
|
+
kind: ts.isArrayLiteralExpression(spread.parent) ? "assignment" : "argument",
|
|
2244
|
+
node: spread
|
|
2245
|
+
}] : [];
|
|
2246
|
+
}
|
|
2247
|
+
function propertyFindings(checker, property) {
|
|
2248
|
+
if (isDestructuringTarget(property.parent)) {
|
|
2249
|
+
const fallback = ts.isShorthandPropertyAssignment(property) ? property.objectAssignmentInitializer : void 0;
|
|
2250
|
+
return fallback !== void 0 && assignmentLeaks(checker, checker.getTypeAtLocation(fallback), checker.getTypeAtLocation(property.name)) ? [{
|
|
2251
|
+
kind: "assignment",
|
|
2252
|
+
node: property
|
|
2253
|
+
}] : [];
|
|
2254
|
+
}
|
|
2255
|
+
return leaksIntoContext(checker, ts.isPropertyAssignment(property) ? property.initializer : property.name) ? [{
|
|
2256
|
+
kind: "assignment",
|
|
2257
|
+
node: property
|
|
2258
|
+
}] : [];
|
|
2259
|
+
}
|
|
2260
|
+
function isPattern(node) {
|
|
2261
|
+
return ts.isObjectBindingPattern(node) || ts.isArrayBindingPattern(node) || ts.isObjectLiteralExpression(node) || ts.isArrayLiteralExpression(node);
|
|
2262
|
+
}
|
|
2263
|
+
/** The key a property name spells, a computed literal such as `['a']` included. */
|
|
2264
|
+
function literalKey(name) {
|
|
2265
|
+
if (ts.isIdentifier(name)) return name.text;
|
|
2266
|
+
const key = ts.isComputedPropertyName(name) ? name.expression : name;
|
|
2267
|
+
return ts.isStringLiteral(key) || ts.isNumericLiteral(key) || ts.isNoSubstitutionTemplateLiteral(key) ? key.text : void 0;
|
|
2268
|
+
}
|
|
2269
|
+
/** The target and default of an assignment pattern element, as in `a = fallback`. */
|
|
2270
|
+
function withDefault(element) {
|
|
2271
|
+
return ts.isBinaryExpression(element) && element.operatorToken.kind === ts.SyntaxKind.EqualsToken ? {
|
|
2272
|
+
target: element.left,
|
|
2273
|
+
initializer: element.right
|
|
2274
|
+
} : {
|
|
2275
|
+
target: element,
|
|
2276
|
+
initializer: void 0
|
|
2277
|
+
};
|
|
2278
|
+
}
|
|
2279
|
+
function objectElements(pattern) {
|
|
2280
|
+
if (ts.isObjectBindingPattern(pattern)) return pattern.elements.map((element) => ({
|
|
2281
|
+
key: element.dotDotDotToken === void 0 ? literalKey(element.propertyName ?? element.name) : void 0,
|
|
2282
|
+
target: element.name,
|
|
2283
|
+
initializer: element.initializer,
|
|
2284
|
+
node: element
|
|
2285
|
+
}));
|
|
2286
|
+
return pattern.properties.map((property) => {
|
|
2287
|
+
if (ts.isPropertyAssignment(property)) return {
|
|
2288
|
+
key: literalKey(property.name),
|
|
2289
|
+
...withDefault(property.initializer),
|
|
2290
|
+
node: property
|
|
2291
|
+
};
|
|
2292
|
+
if (ts.isShorthandPropertyAssignment(property)) return {
|
|
2293
|
+
key: property.name.text,
|
|
2294
|
+
target: property.name,
|
|
2295
|
+
initializer: property.objectAssignmentInitializer,
|
|
2296
|
+
node: property
|
|
2297
|
+
};
|
|
2298
|
+
return {
|
|
2299
|
+
key: void 0,
|
|
2300
|
+
target: property,
|
|
2301
|
+
initializer: void 0,
|
|
2302
|
+
node: property
|
|
2303
|
+
};
|
|
2304
|
+
});
|
|
2305
|
+
}
|
|
2306
|
+
function arrayElements(pattern) {
|
|
2307
|
+
return pattern.elements.flatMap((element, index) => {
|
|
2308
|
+
if (ts.isOmittedExpression(element)) return [];
|
|
2309
|
+
if (ts.isBindingElement(element)) return [{
|
|
2310
|
+
key: element.dotDotDotToken === void 0 ? String(index) : void 0,
|
|
2311
|
+
target: element.name,
|
|
2312
|
+
initializer: element.initializer,
|
|
2313
|
+
node: element
|
|
2314
|
+
}];
|
|
2315
|
+
if (ts.isSpreadElement(element)) return [{
|
|
2316
|
+
key: void 0,
|
|
2317
|
+
target: element.expression,
|
|
2318
|
+
initializer: void 0,
|
|
2319
|
+
node: element
|
|
2320
|
+
}];
|
|
2321
|
+
return [{
|
|
2322
|
+
key: String(index),
|
|
2323
|
+
...withDefault(element),
|
|
2324
|
+
node: element
|
|
2325
|
+
}];
|
|
2326
|
+
});
|
|
2327
|
+
}
|
|
2328
|
+
/** An element that takes an `any` its source declares, but for one whose default is itself reported, and the elements nested under it. */
|
|
2329
|
+
function elementFindings(checker, element, source) {
|
|
2330
|
+
if (isAnyType(checker, source)) return element.initializer !== void 0 && isAny(checker, element.initializer) ? [] : [{
|
|
2331
|
+
kind: "assignment",
|
|
2332
|
+
node: element.node
|
|
2333
|
+
}];
|
|
2334
|
+
return isPattern(element.target) ? destructuredFindings(checker, element.target, source) : [];
|
|
2335
|
+
}
|
|
2336
|
+
/** The property or tuple slot named `key` that `source` declares, not one an index signature stands for. */
|
|
2337
|
+
function declaredSlot(checker, source, key, at) {
|
|
2338
|
+
const slot = checker.getPropertyOfType(source, key);
|
|
2339
|
+
return slot === void 0 ? void 0 : checker.getTypeOfSymbolAtLocation(slot, at);
|
|
2340
|
+
}
|
|
2341
|
+
function slotFindings(checker, element, source) {
|
|
2342
|
+
const slot = element.key === void 0 ? void 0 : declaredSlot(checker, source, element.key, element.node);
|
|
2343
|
+
return slot === void 0 ? [] : elementFindings(checker, element, slot);
|
|
2344
|
+
}
|
|
2345
|
+
/**
|
|
2346
|
+
* The elements of an untyped destructuring declaration or of a destructuring
|
|
2347
|
+
* assignment that take `any` from what `source` declares: a property or
|
|
2348
|
+
* tuple slot typed `any`, whatever the target's own type, or the whole
|
|
2349
|
+
* pattern once when it reads an `any[]`. An index signature, another iterable
|
|
2350
|
+
* and a rest from an object or a tuple are left alone, as
|
|
2351
|
+
* `typescript/no-unsafe-assignment` leaves them.
|
|
2352
|
+
*/
|
|
2353
|
+
function destructuredFindings(checker, pattern, source) {
|
|
2354
|
+
if (ts.isObjectBindingPattern(pattern) || ts.isObjectLiteralExpression(pattern)) return objectElements(pattern).flatMap((element) => slotFindings(checker, element, source));
|
|
2355
|
+
if (isAnyOrAnyArray(checker, source)) return [{
|
|
2356
|
+
kind: "assignment",
|
|
2357
|
+
node: pattern
|
|
2358
|
+
}];
|
|
2359
|
+
return checker.isTupleType(source) ? arrayElements(pattern).flatMap((element) => slotFindings(checker, element, source)) : [];
|
|
2360
|
+
}
|
|
2361
|
+
function declarationFindings(checker, node, initializer) {
|
|
2362
|
+
if (assignmentLeaks(checker, checker.getTypeAtLocation(initializer), node.type === void 0 ? void 0 : checker.getTypeAtLocation(node.name))) return [{
|
|
2363
|
+
kind: "assignment",
|
|
2364
|
+
node
|
|
2365
|
+
}];
|
|
2366
|
+
return node.type === void 0 && !ts.isIdentifier(node.name) ? destructuredFindings(checker, node.name, checker.getTypeAtLocation(initializer)) : [];
|
|
2367
|
+
}
|
|
2368
|
+
function assignmentFindings(checker, node) {
|
|
2369
|
+
if (ts.isVariableDeclaration(node) && node.initializer !== void 0) return declarationFindings(checker, node, node.initializer);
|
|
2370
|
+
if (ts.isBinaryExpression(node) && node.operatorToken.kind === ts.SyntaxKind.EqualsToken) {
|
|
2371
|
+
const source = checker.getTypeAtLocation(node.right);
|
|
2372
|
+
if (assignmentLeaks(checker, source, checker.getTypeAtLocation(node.left))) return [{
|
|
2373
|
+
kind: "assignment",
|
|
2374
|
+
node
|
|
2375
|
+
}];
|
|
2376
|
+
return ts.isObjectLiteralExpression(node.left) || ts.isArrayLiteralExpression(node.left) ? destructuredFindings(checker, node.left, source) : [];
|
|
2377
|
+
}
|
|
2378
|
+
if ((ts.isParameter(node) || ts.isBindingElement(node)) && node.initializer !== void 0) return leaksIntoContext(checker, node.initializer) ? [{
|
|
2379
|
+
kind: "assignment",
|
|
2380
|
+
node
|
|
2381
|
+
}] : [];
|
|
2382
|
+
if (ts.isPropertyAssignment(node) || ts.isShorthandPropertyAssignment(node)) return propertyFindings(checker, node);
|
|
2383
|
+
return [];
|
|
2384
|
+
}
|
|
2385
|
+
function nodeFindings(checker, node) {
|
|
2386
|
+
if (ts.isSpreadElement(node)) return spreadFindings(checker, node);
|
|
2387
|
+
if (ts.isCallExpression(node) || ts.isNewExpression(node)) return argumentFindings(checker, node);
|
|
2388
|
+
if (ts.isReturnStatement(node) && node.expression !== void 0) return returned(checker, node.expression);
|
|
2389
|
+
if (ts.isArrowFunction(node) && !ts.isBlock(node.body)) return returned(checker, node.body);
|
|
2390
|
+
return assignmentFindings(checker, node);
|
|
2391
|
+
}
|
|
2392
|
+
function findings({ checker, sourceFile }) {
|
|
2393
|
+
const found = [];
|
|
2394
|
+
function visit(node) {
|
|
2395
|
+
found.push(...nodeFindings(checker, node));
|
|
2396
|
+
ts.forEachChild(node, visit);
|
|
2397
|
+
}
|
|
2398
|
+
visit(sourceFile);
|
|
2399
|
+
return found;
|
|
2400
|
+
}
|
|
2401
|
+
/**
|
|
2402
|
+
* `typescript/no-unsafe-assignment`, `no-unsafe-argument` and
|
|
2403
|
+
* `no-unsafe-return` for the script blocks of a `.vue` file, which
|
|
2404
|
+
* oxlint-tsgolint cannot read: an `any` or `any[]` reaching a declared type
|
|
2405
|
+
* through a declaration, an assignment, an object literal property, a
|
|
2406
|
+
* default value, an argument, a spread, a destructured binding, a
|
|
2407
|
+
* destructuring assignment such as `({ a } = value)` or a return. An `any`
|
|
2408
|
+
* assigned to a slot declared `any` is reported too, as tsgolint reports it.
|
|
2409
|
+
* TypeScript files stay with tsgolint, and a
|
|
2410
|
+
* component with `generic` type parameters is skipped, as only Vue's compiler
|
|
2411
|
+
* brings them into scope.
|
|
2412
|
+
*/
|
|
2413
|
+
const noUnsafeAnyInVueRule = defineRule({
|
|
2414
|
+
meta: {
|
|
2415
|
+
type: "problem",
|
|
2416
|
+
docs: { description: "A Vue component's script assigns, passes and returns no `any` where a type is declared." },
|
|
2417
|
+
messages: {
|
|
2418
|
+
assignment: "Unsafe assignment of an `any` value, which `typescript/no-unsafe-assignment` reports in a TypeScript file. Use a typed value or `unknown`, and narrow it before use.",
|
|
2419
|
+
argument: "Unsafe argument of type `any` to a typed parameter, which `typescript/no-unsafe-argument` reports in a TypeScript file. Pass a typed value.",
|
|
2420
|
+
return: "Unsafe return of an `any` value from a function with a declared return type, which `typescript/no-unsafe-return` reports in a TypeScript file. Return a typed value."
|
|
2421
|
+
}
|
|
2422
|
+
},
|
|
2423
|
+
createOnce(context) {
|
|
2424
|
+
return { Program() {
|
|
2425
|
+
const { filename, sourceCode } = context;
|
|
2426
|
+
if (!filename.endsWith(".vue")) return;
|
|
2427
|
+
const { text } = sourceCode;
|
|
2428
|
+
const linted = lintedSource(filename, text);
|
|
2429
|
+
if (linted === void 0) return;
|
|
2430
|
+
const { offset } = linted;
|
|
2431
|
+
const typed = typedFile(filename, linted.text);
|
|
2432
|
+
if (typed === void 0) return;
|
|
2433
|
+
for (const { kind, node } of findings(typed)) {
|
|
2434
|
+
const start = node.getStart(typed.sourceFile) - offset;
|
|
2435
|
+
const end = node.end - offset;
|
|
2436
|
+
if (start >= 0 && end <= text.length) context.report({
|
|
2437
|
+
messageId: kind,
|
|
2438
|
+
loc: {
|
|
2439
|
+
start: sourceCode.getLocFromIndex(start),
|
|
2440
|
+
end: sourceCode.getLocFromIndex(end)
|
|
2441
|
+
}
|
|
2442
|
+
});
|
|
2443
|
+
}
|
|
2444
|
+
} };
|
|
2445
|
+
}
|
|
2446
|
+
});
|
|
2447
|
+
//#endregion
|
|
1736
2448
|
//#region src/oxc/guardrails/literal-fit.ts
|
|
1737
2449
|
const MAX_DEPTH$1 = 8;
|
|
1738
2450
|
const INDEX = /^\d+$/u;
|
|
@@ -1871,9 +2583,11 @@ const noUnsafeObjectAssignRule = defineRule({
|
|
|
1871
2583
|
},
|
|
1872
2584
|
createOnce(context) {
|
|
1873
2585
|
return { CallExpression(node) {
|
|
1874
|
-
if (callName(node.callee) !== "Object.assign" || node.arguments.length < 2
|
|
1875
|
-
const
|
|
1876
|
-
const
|
|
2586
|
+
if (callName(node.callee) !== "Object.assign" || node.arguments.length < 2) return;
|
|
2587
|
+
const linted = lintedSource(context.filename, context.sourceCode.text);
|
|
2588
|
+
const typed = linted && typedFile(context.filename, linted.text);
|
|
2589
|
+
const [start, end] = node.range;
|
|
2590
|
+
const call = linted && typed && nodeAt(typed.sourceFile, { range: [start + linted.offset, end + linted.offset] }, ts.isCallExpression);
|
|
1877
2591
|
if (typed === void 0 || call === void 0) return;
|
|
1878
2592
|
const declared = typed.checker.getTypeAtLocation(call.arguments[0]);
|
|
1879
2593
|
const target = declared.isTypeParameter() ? typed.checker.getBaseConstraintOfType(declared) ?? declared : declared;
|
|
@@ -2029,7 +2743,7 @@ const requireTsconfigTypesRule = defineRule({
|
|
|
2029
2743
|
});
|
|
2030
2744
|
//#endregion
|
|
2031
2745
|
//#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`. */
|
|
2746
|
+
/** 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, `any` assigned, passed or returned in a Vue script, low-evidence tests, files no tsconfig lists and tsconfigs that list no `types`. */
|
|
2033
2747
|
const guardrailsPlugin = eslintCompatPlugin({
|
|
2034
2748
|
meta: { name: "antelopejs" },
|
|
2035
2749
|
rules: {
|
|
@@ -2042,6 +2756,7 @@ const guardrailsPlugin = eslintCompatPlugin({
|
|
|
2042
2756
|
"no-self-import-in-tests": noSelfImportInTestsRule,
|
|
2043
2757
|
"no-typeof-function-only-test": noTypeofFunctionOnlyTestRule,
|
|
2044
2758
|
"no-unrun-query": noUnrunQueryRule,
|
|
2759
|
+
"no-unsafe-any-in-vue": noUnsafeAnyInVueRule,
|
|
2045
2760
|
"no-unsafe-object-assign": noUnsafeObjectAssignRule,
|
|
2046
2761
|
"require-assertion": requireAssertionRule,
|
|
2047
2762
|
"require-tsconfig-include": requireTsconfigIncludeRule,
|