@junoflow/eslint-plugin 0.1.5 → 0.3.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/index.js +46 -31
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -123,7 +123,7 @@ function subtreeHasCall(root, pred) {
|
|
|
123
123
|
* null.
|
|
124
124
|
*
|
|
125
125
|
* Deliberately the callback and not the call: an argument evaluated eagerly at
|
|
126
|
-
* the call site (`step.run('x', fn, await state.get(
|
|
126
|
+
* the call site (`step.run('x', fn, await state.y.get())`) is inside the
|
|
127
127
|
* CallExpression but runs outside the step, and the two have opposite replay
|
|
128
128
|
* behaviour.
|
|
129
129
|
*/
|
|
@@ -172,22 +172,35 @@ function isStepCall(p) {
|
|
|
172
172
|
return null;
|
|
173
173
|
}
|
|
174
174
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
/** The state method name, or null. */
|
|
175
|
+
// The six verbs a binding handle carries. `scoped` is not among them: it is a
|
|
176
|
+
// method on `state` itself, not on a binding, and stays two segments long.
|
|
177
|
+
const STATE_METHODS = ['get', 'set', 'claim', 'release', 'signal', 'retract'];
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* The state verb, or null. A binding is a handle — `state.<binding>.<verb>()`,
|
|
181
|
+
* the shape `isTargetCall` already matches — so the state segment is third
|
|
182
|
+
* from the end. `state.scoped()` is the one exception and keeps its own arm.
|
|
183
|
+
*/
|
|
185
184
|
function isStateCall(p) {
|
|
186
|
-
if (!p
|
|
187
|
-
|
|
185
|
+
if (!p) return null;
|
|
186
|
+
if (tailIs(p, 'state', 'scoped')) return 'scoped';
|
|
187
|
+
if (p.length < 3) return null;
|
|
188
|
+
const [obj, method] = [p[p.length - 3], p[p.length - 1]];
|
|
188
189
|
return obj === 'state' && STATE_METHODS.includes(method) ? method : null;
|
|
189
190
|
}
|
|
190
191
|
|
|
192
|
+
/**
|
|
193
|
+
* The binding name in `state.<binding>.<verb>()`, or null for `state.scoped()`
|
|
194
|
+
* and anything that is not a state call. A property name is always static, so
|
|
195
|
+
* unlike the string-literal argument it replaces there is no computed form for
|
|
196
|
+
* a rule to fall blind on.
|
|
197
|
+
*/
|
|
198
|
+
function stateBindingName(p) {
|
|
199
|
+
const verb = isStateCall(p);
|
|
200
|
+
if (!verb || verb === 'scoped') return null;
|
|
201
|
+
return p[p.length - 2];
|
|
202
|
+
}
|
|
203
|
+
|
|
191
204
|
const FINDING_METHODS = ['open', 'clear', 'set'];
|
|
192
205
|
function isFindingsCall(p) {
|
|
193
206
|
if (!p || p.length < 2) return null;
|
|
@@ -475,9 +488,11 @@ const validDuration = {
|
|
|
475
488
|
check(objectProp(node.arguments[1], 'timeout'));
|
|
476
489
|
return;
|
|
477
490
|
}
|
|
491
|
+
// The binding names the state, the slot and the signal, so a claim's
|
|
492
|
+
// options are its second argument and a signal's its first.
|
|
478
493
|
const state = isStateCall(p);
|
|
479
|
-
if (state === 'claim') check(objectProp(node.arguments[
|
|
480
|
-
if (state === 'signal') check(objectProp(node.arguments[
|
|
494
|
+
if (state === 'claim') check(objectProp(node.arguments[1], 'ttl'));
|
|
495
|
+
if (state === 'signal') check(objectProp(node.arguments[0], 'ttl'));
|
|
481
496
|
if (isFlowInvoke(p)) check(objectProp(node.arguments[1], 'timeout'));
|
|
482
497
|
},
|
|
483
498
|
};
|
|
@@ -1070,17 +1085,19 @@ const requireSignalInListener = {
|
|
|
1070
1085
|
|
|
1071
1086
|
// ─── prefer-claim-over-set ───────────────────────────────────────────────────
|
|
1072
1087
|
//
|
|
1073
|
-
// state
|
|
1074
|
-
// stack is active. On a state with no layers that is the whole story and this
|
|
1088
|
+
// state.<binding>.set writes a state's base — the value it holds when no layer
|
|
1089
|
+
// of its stack is active. On a state with no layers that is the whole story and this
|
|
1075
1090
|
// rule says nothing. On a state that has declared a claim slot, a claim can
|
|
1076
1091
|
// outrank the base, and a flow that meant "make it this now" has written
|
|
1077
1092
|
// somewhere nobody is currently reading.
|
|
1078
1093
|
//
|
|
1079
|
-
// The
|
|
1080
|
-
//
|
|
1081
|
-
//
|
|
1082
|
-
//
|
|
1083
|
-
//
|
|
1094
|
+
// The option names *bindings*, not states, because the binding is all the
|
|
1095
|
+
// source says: `state.<binding>.set(...)` never spells the state id. Which
|
|
1096
|
+
// bindings resolve to a slotted state is a fact about the manifest and the
|
|
1097
|
+
// registry, not about the source, so the rule cannot work it out and is told.
|
|
1098
|
+
// Juno's verify step renders the option from the same catalog typegen reads;
|
|
1099
|
+
// an author linting locally without it gets a rule that reports nothing, which
|
|
1100
|
+
// is the right way for a missing catalog to fail.
|
|
1084
1101
|
//
|
|
1085
1102
|
// A warning, not an error, on the severity rule in rulesets.json: this reasons
|
|
1086
1103
|
// about intent. Writing the base of a state that has slots is a legitimate
|
|
@@ -1091,7 +1108,7 @@ const preferClaimOverSet = {
|
|
|
1091
1108
|
type: 'suggestion',
|
|
1092
1109
|
docs: {
|
|
1093
1110
|
description:
|
|
1094
|
-
'prefer
|
|
1111
|
+
'prefer a claim binding over set on a state that declares claim slots',
|
|
1095
1112
|
},
|
|
1096
1113
|
schema: [
|
|
1097
1114
|
{
|
|
@@ -1108,15 +1125,13 @@ const preferClaimOverSet = {
|
|
|
1108
1125
|
if (ids.size === 0) return {};
|
|
1109
1126
|
return {
|
|
1110
1127
|
CallExpression(node) {
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
}
|
|
1116
|
-
if (!ids.has(id.value)) return;
|
|
1128
|
+
const p = calleePath(node);
|
|
1129
|
+
if (isStateCall(p) !== 'set') return;
|
|
1130
|
+
const binding = stateBindingName(p);
|
|
1131
|
+
if (!binding || !ids.has(binding)) return;
|
|
1117
1132
|
context.report({
|
|
1118
|
-
node:
|
|
1119
|
-
message: `${
|
|
1133
|
+
node: node.callee,
|
|
1134
|
+
message: `${binding} names a state with claim slots, so state.${binding}.set writes its base and a claim can outrank it — the value may not move. Declare a claim binding on the slot and call its claim(value, { ttl }) if you meant this flow's write to take effect now, or keep set if you meant to change what the state falls back to`,
|
|
1120
1135
|
});
|
|
1121
1136
|
},
|
|
1122
1137
|
};
|