@r_masseater/oxlint-plugin-decision 0.6.0 → 0.8.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 -3
- package/dist/{src-CknkhDKf.mjs → app-UakaWKAT.mjs} +69 -209
- package/dist/app-UakaWKAT.mjs.map +1 -0
- package/dist/cloudflare-worker.mjs +5 -182
- package/dist/cloudflare-worker.mjs.map +1 -1
- package/dist/decision-B7hahWHz.mjs +561 -0
- package/dist/decision-B7hahWHz.mjs.map +1 -0
- package/dist/{presets-CxYinkkJ.mjs → define-presets-yflVt8vT.mjs} +197 -17
- package/dist/define-presets-yflVt8vT.mjs.map +1 -0
- package/dist/{index-DiH-hZUS.d.mts → index-C0Twu0w7.d.mts} +26 -33
- package/dist/index-HOwrGbIA.d.mts +89 -0
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +1 -1
- package/dist/preset-plugin-ClpEFUN5.mjs +10 -0
- package/dist/preset-plugin-ClpEFUN5.mjs.map +1 -0
- package/dist/presets/ai-slop.d.mts +3 -13
- package/dist/presets/ai-slop.mjs +3 -183
- package/dist/presets/ai-slop.mjs.map +1 -1
- package/dist/presets/index.d.mts +2 -78
- package/dist/presets/index.mjs +1 -1
- package/dist/presets/recommended.d.mts +1 -1
- package/dist/presets/recommended.mjs +3 -2
- package/dist/presets/recommended.mjs.map +1 -1
- package/dist/presets/review-only.d.mts +1 -1
- package/dist/presets/review-only.mjs +3 -2
- package/dist/presets/review-only.mjs.map +1 -1
- package/dist/presets/strict.d.mts +1 -1
- package/dist/presets/strict.mjs +3 -2
- package/dist/presets/strict.mjs.map +1 -1
- package/dist/serve-decisions-DYGwLMft.mjs +287 -0
- package/dist/serve-decisions-DYGwLMft.mjs.map +1 -0
- package/dist/typesafe-worker.mjs +11 -0
- package/dist/typesafe-worker.mjs.map +1 -0
- package/package.json +7 -12
- package/skills/write-decision-rules/SKILL.md +18 -17
- package/skills/write-decision-rules/references/tuning.md +1 -3
- package/dist/openrouter-app.d.mts +0 -15
- package/dist/openrouter-app.mjs +0 -89
- package/dist/openrouter-app.mjs.map +0 -1
- package/dist/openrouter-worker.mjs +0 -46
- package/dist/openrouter-worker.mjs.map +0 -1
- package/dist/presets-CxYinkkJ.mjs.map +0 -1
- package/dist/runtime-CICpEidL.mjs +0 -271
- package/dist/runtime-CICpEidL.mjs.map +0 -1
- package/dist/select-bvQylrUt.mjs +0 -12
- package/dist/select-bvQylrUt.mjs.map +0 -1
- package/dist/serve-decisions-BM0bUWWe.mjs +0 -119
- package/dist/serve-decisions-BM0bUWWe.mjs.map +0 -1
- package/dist/src-CknkhDKf.mjs.map +0 -1
- package/dist/wire-CRD1mSAo.mjs +0 -251
- package/dist/wire-CRD1mSAo.mjs.map +0 -1
- package/skills/write-decision-rules/references/providers.md +0 -55
- /package/dist/{openrouter-worker.d.mts → typesafe-worker.d.mts} +0 -0
|
@@ -0,0 +1,561 @@
|
|
|
1
|
+
import { Array, Boolean, Cause, Config, Console, DateTime, Effect, Exit, Function, Match, Number as Number$1, Option, Predicate, Record, Result, Schema, String as String$1, pipe } from "effect";
|
|
2
|
+
// src/entities/decision/model/schema.ts
|
|
3
|
+
const NONE = 0;
|
|
4
|
+
const MINIMUM_LABELS = 2;
|
|
5
|
+
const NonNegativeInt = Schema.Int.check(Schema.isGreaterThanOrEqualTo(NONE));
|
|
6
|
+
const Probability = Schema.Finite.check(Schema.isBetween({
|
|
7
|
+
minimum: 0,
|
|
8
|
+
maximum: 1
|
|
9
|
+
}));
|
|
10
|
+
const CONFIDENT = .8;
|
|
11
|
+
const Cutoff = Schema.Finite.check(Schema.isBetween({
|
|
12
|
+
minimum: CONFIDENT,
|
|
13
|
+
maximum: 1
|
|
14
|
+
}, { message: `cutoff must be between ${CONFIDENT} and 1` }));
|
|
15
|
+
const TargetSchema = Schema.Literals([
|
|
16
|
+
"function",
|
|
17
|
+
"call",
|
|
18
|
+
"jsx",
|
|
19
|
+
"file"
|
|
20
|
+
]);
|
|
21
|
+
const CiBehaviorSchema = Schema.Literals(["skip", "fail"]);
|
|
22
|
+
const ExplainSettingSchema = Schema.Union([Schema.Boolean, Schema.Literals(["text", "json"])]);
|
|
23
|
+
const EnclosingKindSchema = Schema.Literals([
|
|
24
|
+
"function",
|
|
25
|
+
"class",
|
|
26
|
+
"block"
|
|
27
|
+
]);
|
|
28
|
+
const RuleContextSchema = Schema.Union([
|
|
29
|
+
Schema.Struct({ scope: Schema.Literals([
|
|
30
|
+
"node",
|
|
31
|
+
"file",
|
|
32
|
+
"comment"
|
|
33
|
+
]) }),
|
|
34
|
+
Schema.Struct({
|
|
35
|
+
scope: Schema.Literal("lines"),
|
|
36
|
+
before: Schema.optionalKey(NonNegativeInt),
|
|
37
|
+
after: Schema.optionalKey(NonNegativeInt)
|
|
38
|
+
}),
|
|
39
|
+
Schema.Struct({
|
|
40
|
+
scope: Schema.Literal("enclosing"),
|
|
41
|
+
kind: EnclosingKindSchema
|
|
42
|
+
})
|
|
43
|
+
]);
|
|
44
|
+
const SeveritySchema = Schema.Literals([
|
|
45
|
+
"info",
|
|
46
|
+
"warn",
|
|
47
|
+
"error"
|
|
48
|
+
]);
|
|
49
|
+
const Label = Schema.NonEmptyString;
|
|
50
|
+
const BooleanAnswer = Schema.Struct({ kind: Schema.Literal("boolean") });
|
|
51
|
+
const ClassifyAnswer = Schema.Struct({
|
|
52
|
+
kind: Schema.Literal("classify"),
|
|
53
|
+
labels: Schema.Record(Label, Schema.String).check(Schema.isMinProperties(MINIMUM_LABELS)),
|
|
54
|
+
report: Schema.NonEmptyArray(Label)
|
|
55
|
+
}).check(Schema.makeFilter(({ labels, report }) => pipe(report, Array.every((label) => Record.has(labels, label))), { message: "every label in report must be one of labels" }));
|
|
56
|
+
const SeverityAnswer = Schema.Struct({
|
|
57
|
+
kind: Schema.Literal("severity"),
|
|
58
|
+
minimum: Schema.optionalKey(SeveritySchema)
|
|
59
|
+
});
|
|
60
|
+
const RuleAnswerSchema = Schema.Union([
|
|
61
|
+
BooleanAnswer,
|
|
62
|
+
ClassifyAnswer,
|
|
63
|
+
SeverityAnswer
|
|
64
|
+
]);
|
|
65
|
+
// src/entities/decision/model/answer.ts
|
|
66
|
+
const DATA_FIRST_ARITY$4 = 2;
|
|
67
|
+
const YES = "yes";
|
|
68
|
+
const DEFAULT_MINIMUM = "warn";
|
|
69
|
+
const SEVERITY_CRITERIA = {
|
|
70
|
+
none: "No problem",
|
|
71
|
+
info: "Worth noting, but not a problem",
|
|
72
|
+
warn: "Likely a problem",
|
|
73
|
+
error: "Clearly a problem"
|
|
74
|
+
};
|
|
75
|
+
const SEVERITY_ORDER = Record.keys(SEVERITY_CRITERIA);
|
|
76
|
+
const BOOLEAN_ANSWER = { kind: "boolean" };
|
|
77
|
+
const decodeAnswer = Schema.decodeUnknownResult(RuleAnswerSchema);
|
|
78
|
+
const decodeRuleAnswer = (answer) => {
|
|
79
|
+
const decoded = decodeAnswer(answer);
|
|
80
|
+
if (Result.isSuccess(decoded)) return Result.succeed(decoded.success);
|
|
81
|
+
return Result.fail(decoded.failure.message);
|
|
82
|
+
};
|
|
83
|
+
const NoulAnswerSchema = Schema.Struct({
|
|
84
|
+
type: Schema.Literal("noul"),
|
|
85
|
+
noul: Probability
|
|
86
|
+
});
|
|
87
|
+
const ChoiceAnswerSchema = Schema.Struct({
|
|
88
|
+
type: Schema.Literal("choice"),
|
|
89
|
+
choice: Schema.String,
|
|
90
|
+
probabilities: Schema.optional(Schema.Record(Schema.String, Probability))
|
|
91
|
+
});
|
|
92
|
+
const AnswerSchema = Schema.Union([NoulAnswerSchema, ChoiceAnswerSchema]);
|
|
93
|
+
const decodeNoul = Schema.decodeOption(NoulAnswerSchema);
|
|
94
|
+
const decodeChoice = Schema.decodeOption(ChoiceAnswerSchema);
|
|
95
|
+
const isNoul = (answer) => answer.type === "noul";
|
|
96
|
+
const isChoice = (answer) => answer.type === "choice";
|
|
97
|
+
const noulOf = (answer) => Option.fromUndefinedOr(answer).pipe(Option.filter(isNoul), Option.flatMap(decodeNoul));
|
|
98
|
+
const choiceOf = (answer) => Option.fromUndefinedOr(answer).pipe(Option.filter(isChoice), Option.flatMap(decodeChoice));
|
|
99
|
+
const probabilityOfChoice = ({ choice, probabilities }) => Option.fromUndefinedOr(probabilities).pipe(Option.flatMap((byLabel) => Option.fromUndefinedOr(byLabel[choice])));
|
|
100
|
+
const questionFor = Function.dual(DATA_FIRST_ARITY$4, (answer, instructions) => {
|
|
101
|
+
switch (answer.kind) {
|
|
102
|
+
case "classify": return {
|
|
103
|
+
type: "choice",
|
|
104
|
+
instructions: `${instructions} Select the label that fits best.`,
|
|
105
|
+
criteria: answer.labels
|
|
106
|
+
};
|
|
107
|
+
case "severity": return {
|
|
108
|
+
type: "choice",
|
|
109
|
+
instructions: `${instructions} Rate how serious the problem is, and select none if there is no problem.`,
|
|
110
|
+
criteria: SEVERITY_CRITERIA
|
|
111
|
+
};
|
|
112
|
+
case "boolean": return {
|
|
113
|
+
type: "noul",
|
|
114
|
+
instructions
|
|
115
|
+
};
|
|
116
|
+
default: return {
|
|
117
|
+
type: "noul",
|
|
118
|
+
instructions
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
const choiceVerdictOf = (criteria, answer) => choiceOf(answer).pipe(Option.filter(({ choice }) => Record.has(criteria, choice)), Option.flatMap((chosen) => Option.map(probabilityOfChoice(chosen), (probability) => ({
|
|
123
|
+
answer: chosen.choice,
|
|
124
|
+
probability
|
|
125
|
+
}))));
|
|
126
|
+
const verdictOf = Function.dual(DATA_FIRST_ARITY$4, (question, answer) => {
|
|
127
|
+
if (question.type === "choice") return choiceVerdictOf(question.criteria, answer);
|
|
128
|
+
return noulOf(answer).pipe(Option.map(({ noul }) => ({
|
|
129
|
+
answer: YES,
|
|
130
|
+
probability: noul
|
|
131
|
+
})));
|
|
132
|
+
});
|
|
133
|
+
const severityMatches = ({ minimum = DEFAULT_MINIMUM }, label) => SEVERITY_ORDER.indexOf(label) >= SEVERITY_ORDER.indexOf(minimum);
|
|
134
|
+
const answerMatches = (answer, label) => {
|
|
135
|
+
switch (answer.kind) {
|
|
136
|
+
case "classify": return answer.report.includes(label);
|
|
137
|
+
case "severity": return severityMatches(answer, label);
|
|
138
|
+
case "boolean": return label === YES;
|
|
139
|
+
default: return false;
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
const isReported = Function.dual(DATA_FIRST_ARITY$4, (rule, verdict) => verdict.probability >= rule.cutoff && answerMatches(rule.answer, verdict.answer));
|
|
143
|
+
// src/entities/decision/model/location.ts
|
|
144
|
+
const DATA_FIRST_ARITY$3 = 2;
|
|
145
|
+
const DATA_FIRST_ARITY_3$3 = 3;
|
|
146
|
+
const CHOICE_OPTION_LIMIT = 254;
|
|
147
|
+
const FIRST_LINE$3 = 1;
|
|
148
|
+
const MINIMUM_WIDTH = 1;
|
|
149
|
+
const UNKNOWN_CHOICE = "unknown";
|
|
150
|
+
const sourceLines = (text) => String$1.split(text, /\r\n|[\n\r\u2028\u2029]/u);
|
|
151
|
+
const locates = (match) => Predicate.isNotUndefined(match.rule.location) && !match.truncated;
|
|
152
|
+
const EVERY_LINE = {
|
|
153
|
+
start: FIRST_LINE$3,
|
|
154
|
+
end: Number.POSITIVE_INFINITY
|
|
155
|
+
};
|
|
156
|
+
const isWithin = (line, within) => Number$1.between(line, {
|
|
157
|
+
minimum: within.start,
|
|
158
|
+
maximum: within.end
|
|
159
|
+
});
|
|
160
|
+
const rangeKey = ({ start, end }) => Boolean.match(start === end, {
|
|
161
|
+
onTrue: () => `L${String(start)}`,
|
|
162
|
+
onFalse: () => `L${String(start)}-L${String(end)}`
|
|
163
|
+
});
|
|
164
|
+
const rangeLabel = ({ start, end }) => Boolean.match(start === end, {
|
|
165
|
+
onTrue: () => `Line ${String(start)}`,
|
|
166
|
+
onFalse: () => `Lines ${String(start)} to ${String(end)}`
|
|
167
|
+
});
|
|
168
|
+
const rangeOf = (group) => ({
|
|
169
|
+
start: Array.headNonEmpty(group).line,
|
|
170
|
+
end: Array.lastNonEmpty(group).line
|
|
171
|
+
});
|
|
172
|
+
const numberedLines = (snippet) => pipe(sourceLines(snippet), Array.map((text, index) => ({
|
|
173
|
+
text,
|
|
174
|
+
line: Number$1.sum(index, FIRST_LINE$3)
|
|
175
|
+
})));
|
|
176
|
+
const locationCandidates = Function.dual(DATA_FIRST_ARITY$3, (snippet, within) => {
|
|
177
|
+
const lines = pipe(numberedLines(snippet), Array.filter(({ text, line }) => String$1.trim(text).length >= FIRST_LINE$3 && isWithin(line, within)));
|
|
178
|
+
const width = Math.max(MINIMUM_WIDTH, Math.ceil(lines.length / CHOICE_OPTION_LIMIT));
|
|
179
|
+
return Record.fromEntries(pipe(Array.chunksOf(lines, width), Array.map((group) => {
|
|
180
|
+
const range = rangeOf(group);
|
|
181
|
+
return [rangeKey(range), range];
|
|
182
|
+
})));
|
|
183
|
+
});
|
|
184
|
+
const locationQuestion = Function.dual(DATA_FIRST_ARITY_3$3, (match, ref, candidates) => ({
|
|
185
|
+
type: "choice",
|
|
186
|
+
instructions: `Consider only the numbered source in state.snippets.${ref}. If it violates this rule, select the line (or range containing that line) to annotate. Rule: ${match.rule.question} Location: ${match.rule.location.question} If multiple violations exist, select the earliest one. Select ${UNKNOWN_CHOICE} if no violation exists or its location is unclear.`,
|
|
187
|
+
criteria: {
|
|
188
|
+
...Record.fromEntries(pipe(Record.toEntries(candidates), Array.map(([key, range]) => [key, rangeLabel(range)]))),
|
|
189
|
+
[UNKNOWN_CHOICE]: "No clear source location for this violation"
|
|
190
|
+
}
|
|
191
|
+
}));
|
|
192
|
+
const validChoice = Function.dual(DATA_FIRST_ARITY$3, (answer, criteria) => Option.exists(choiceOf(answer), (chosen) => Record.has(criteria, chosen.choice) && Option.isSome(probabilityOfChoice(chosen))));
|
|
193
|
+
const selectedLocation = Function.dual(DATA_FIRST_ARITY_3$3, (answer, candidates, cutoff) => choiceOf(answer).pipe(Option.filter((chosen) => Option.exists(probabilityOfChoice(chosen), (probability) => Number$1.isGreaterThanOrEqualTo(probability, cutoff))), Option.flatMap(({ choice }) => Option.fromUndefinedOr(candidates[choice]))));
|
|
194
|
+
const locationOfLine = Function.dual(DATA_FIRST_ARITY$3, (snippet, line) => {
|
|
195
|
+
const text = Option.getOrElse(Array.get(sourceLines(snippet), Number$1.decrement(line)), () => "");
|
|
196
|
+
return {
|
|
197
|
+
start: {
|
|
198
|
+
line,
|
|
199
|
+
column: Option.getOrElse(String$1.search(/\S/u)(text), () => Number$1.decrement(FIRST_LINE$3))
|
|
200
|
+
},
|
|
201
|
+
end: {
|
|
202
|
+
line,
|
|
203
|
+
column: String$1.trimEnd(text).length
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
});
|
|
207
|
+
// src/entities/decision/model/nodes.ts
|
|
208
|
+
const FIRST_LINE$2 = 1;
|
|
209
|
+
const FIRST_COLUMN = 0;
|
|
210
|
+
const TARGET_NODE_TYPES = {
|
|
211
|
+
function: [
|
|
212
|
+
"FunctionDeclaration",
|
|
213
|
+
"FunctionExpression",
|
|
214
|
+
"ArrowFunctionExpression"
|
|
215
|
+
],
|
|
216
|
+
call: ["CallExpression"],
|
|
217
|
+
jsx: ["JSXElement"],
|
|
218
|
+
file: ["Program"]
|
|
219
|
+
};
|
|
220
|
+
const NAMED_PARENTS = {
|
|
221
|
+
VariableDeclarator: "init",
|
|
222
|
+
Property: "value",
|
|
223
|
+
MethodDefinition: "value",
|
|
224
|
+
PropertyDefinition: "value"
|
|
225
|
+
};
|
|
226
|
+
const NAMED_PARENT_TYPES = new Set(Record.keys(NAMED_PARENTS));
|
|
227
|
+
const isNamedParentType = (type) => NAMED_PARENT_TYPES.has(type);
|
|
228
|
+
const ANONYMOUS_FUNCTIONS = /* @__PURE__ */ new Set(["FunctionExpression", "ArrowFunctionExpression"]);
|
|
229
|
+
const holdsAsName = (parent, node) => {
|
|
230
|
+
if (!isNamedParentType(parent.type)) return false;
|
|
231
|
+
const field = NAMED_PARENTS[parent.type];
|
|
232
|
+
return ANONYMOUS_FUNCTIONS.has(node.type) && parent[field] === node;
|
|
233
|
+
};
|
|
234
|
+
const snippetNodeFor = (node) => Option.fromNullishOr(node.parent).pipe(Option.filter((parent) => holdsAsName(parent, node)), Option.getOrElse(() => node));
|
|
235
|
+
const headOf = (text, line, column) => {
|
|
236
|
+
const [first] = String$1.split(text, "\n");
|
|
237
|
+
return {
|
|
238
|
+
start: {
|
|
239
|
+
line,
|
|
240
|
+
column
|
|
241
|
+
},
|
|
242
|
+
end: {
|
|
243
|
+
line,
|
|
244
|
+
column: column + first.length
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
};
|
|
248
|
+
const REPORT_LOC = {
|
|
249
|
+
function: (node, text) => headOf(text, node.loc.start.line, node.loc.start.column),
|
|
250
|
+
call: (node) => node.loc,
|
|
251
|
+
jsx: (node) => Option.getOrElse(Option.fromNullishOr(node.openingElement), () => node).loc,
|
|
252
|
+
file: (_node, text) => headOf(text, FIRST_LINE$2, FIRST_COLUMN)
|
|
253
|
+
};
|
|
254
|
+
// src/entities/decision/model/context.ts
|
|
255
|
+
const DATA_FIRST_ARITY_3$2 = 3;
|
|
256
|
+
const DEFAULT_LINES = 5;
|
|
257
|
+
const FIRST_LINE$1 = 1;
|
|
258
|
+
const LAST = 1;
|
|
259
|
+
const END = 1;
|
|
260
|
+
const ENCLOSING_TYPES = {
|
|
261
|
+
function: [
|
|
262
|
+
"FunctionDeclaration",
|
|
263
|
+
"FunctionExpression",
|
|
264
|
+
"ArrowFunctionExpression"
|
|
265
|
+
],
|
|
266
|
+
class: ["ClassDeclaration", "ClassExpression"],
|
|
267
|
+
block: [
|
|
268
|
+
"BlockStatement",
|
|
269
|
+
"StaticBlock",
|
|
270
|
+
"Program"
|
|
271
|
+
]
|
|
272
|
+
};
|
|
273
|
+
const ancestorsOf = (node) => Array.unfold(Option.fromNullishOr(node.parent), (current) => current.pipe(Option.map((ancestor) => [ancestor, Option.fromNullishOr(ancestor.parent)])));
|
|
274
|
+
const enclosingText = (context, node, kind) => Array.findFirst(ancestorsOf(node), (ancestor) => ENCLOSING_TYPES[kind].includes(ancestor.type)).pipe(Option.match({
|
|
275
|
+
onNone: () => context.sourceCode.text,
|
|
276
|
+
onSome: (ancestor) => context.sourceCode.getText(ancestor)
|
|
277
|
+
}));
|
|
278
|
+
const linesAround = (context, node, { before = DEFAULT_LINES, after = DEFAULT_LINES }) => {
|
|
279
|
+
const lines = sourceLines(context.sourceCode.text);
|
|
280
|
+
const first = Math.max(FIRST_LINE$1, node.loc.start.line - before);
|
|
281
|
+
const last = Math.min(lines.length, node.loc.end.line + after);
|
|
282
|
+
return pipe(lines, Array.drop(first - FIRST_LINE$1), Array.take(last - first + FIRST_LINE$1), Array.map((text, index) => `L${String(first + index)}| ${text}`), Array.join("\n"));
|
|
283
|
+
};
|
|
284
|
+
const DECLARING_PARENTS = /* @__PURE__ */ new Set([
|
|
285
|
+
"VariableDeclaration",
|
|
286
|
+
"ExportNamedDeclaration",
|
|
287
|
+
"ExportDefaultDeclaration"
|
|
288
|
+
]);
|
|
289
|
+
const declarationOf = (node) => Option.fromNullishOr(node.parent).pipe(Option.filter((parent) => DECLARING_PARENTS.has(parent.type)), Option.match({
|
|
290
|
+
onNone: () => node,
|
|
291
|
+
onSome: declarationOf
|
|
292
|
+
}));
|
|
293
|
+
const leadingOf = (text, comments, until) => Option.match(Array.last(comments), {
|
|
294
|
+
onNone: () => [],
|
|
295
|
+
onSome: (last) => {
|
|
296
|
+
if (String$1.isNonEmpty(String$1.trim(String$1.slice(last.end, until)(text)))) return [];
|
|
297
|
+
return [...leadingOf(text, Array.dropRight(comments, LAST), last.start), last];
|
|
298
|
+
}
|
|
299
|
+
});
|
|
300
|
+
const commentedTextOf = (context, node) => {
|
|
301
|
+
const { text } = context.sourceCode;
|
|
302
|
+
const named = snippetNodeFor(node);
|
|
303
|
+
const [start] = declarationOf(named).range;
|
|
304
|
+
const before = pipe(context.sourceCode.getAllComments(), Array.filter((comment) => comment.type !== "Shebang" && comment.end <= start));
|
|
305
|
+
return Array.match(leadingOf(text, before, start), {
|
|
306
|
+
onEmpty: () => Option.none(),
|
|
307
|
+
onNonEmpty: ([first]) => Option.some(String$1.slice(first.start, named.range[END])(text))
|
|
308
|
+
});
|
|
309
|
+
};
|
|
310
|
+
const isCommented = ({ context: wanted }) => Predicate.isNotUndefined(wanted) && wanted.scope === "comment";
|
|
311
|
+
const commentedSnippetOf = Function.dual(DATA_FIRST_ARITY_3$2, (context, node, rule) => Option.flatMap(Option.liftPredicate(rule, isCommented), () => commentedTextOf(context, node)));
|
|
312
|
+
const lacksComment = Function.dual(DATA_FIRST_ARITY_3$2, (context, node, rule) => isCommented(rule) && Option.isNone(commentedTextOf(context, node)));
|
|
313
|
+
const contextOf = Function.dual(DATA_FIRST_ARITY_3$2, (context, node, rule) => {
|
|
314
|
+
if (rule.scope === "lines") return Option.some(linesAround(context, node, rule));
|
|
315
|
+
if (rule.scope === "enclosing") return Option.some(enclosingText(context, node, rule.kind));
|
|
316
|
+
return Option.liftPredicate(context.sourceCode.text, () => rule.scope === "file");
|
|
317
|
+
});
|
|
318
|
+
// src/entities/decision/model/name.ts
|
|
319
|
+
const DATA_FIRST_ARITY_3$1 = 3;
|
|
320
|
+
const heldName = (node) => Option.flatMap(Option.fromNullishOr(node.parent), (parent) => Match.value(parent).pipe(Match.when({ type: "VariableDeclarator" }, (holder) => Option.liftPredicate(holder.id, () => holder.init === node)), Match.when({ type: Match.is("Property", "MethodDefinition", "PropertyDefinition") }, (holder) => Option.liftPredicate(holder.key, () => holder.value === node)), Match.orElse(() => Option.none())));
|
|
321
|
+
const nameNodeOf = (node) => Match.value(node).pipe(Match.when({ type: Match.is("FunctionDeclaration", "FunctionExpression") }, (fn) => Option.orElse(Option.fromNullishOr(fn.id), () => heldName(fn))), Match.when({ type: "ArrowFunctionExpression" }, heldName), Match.when({ type: Match.is("CallExpression", "NewExpression") }, (call) => Option.some(call.callee)), Match.when({ type: "JSXElement" }, (element) => Option.some(element.openingElement.name)), Match.orElse(() => Option.none()));
|
|
322
|
+
const nameOf = (node, context) => Option.getOrUndefined(Option.map(nameNodeOf(node), (name) => String$1.slice(name.start, name.end)(context.sourceCode.text)));
|
|
323
|
+
const isSelected = Function.dual(DATA_FIRST_ARITY_3$1, (context, node, { select }) => Option.match(Option.fromUndefinedOr(select), {
|
|
324
|
+
onNone: () => true,
|
|
325
|
+
onSome: (selects) => selects(node, {
|
|
326
|
+
name: nameOf(node, context),
|
|
327
|
+
context
|
|
328
|
+
})
|
|
329
|
+
}));
|
|
330
|
+
// src/entities/decision/model/models.ts
|
|
331
|
+
const JEV_MODEL = /^jev-(?:latest|\d+\.\d+)$/u;
|
|
332
|
+
const CLEF_MODEL = /^@cf\/cloudflare\/clef(?:-flash|-omni)?$/u;
|
|
333
|
+
const DECISION_MODEL = /^(?:typesafe\/jev-(?:latest|\d+\.\d+)|cloudflare\/@cf\/cloudflare\/clef(?:-flash|-omni)?)$/u;
|
|
334
|
+
const isDecisionModel = (model) => DECISION_MODEL.test(model);
|
|
335
|
+
const DecisionModelSchema = Schema.String.pipe(Schema.refine(isDecisionModel, { message: "model must be a decision model: \"typesafe/jev-latest\", \"typesafe/jev-<major>.<minor>\", or \"cloudflare/@cf/cloudflare/clef\", \"clef-flash\" or \"clef-omni\"" }));
|
|
336
|
+
// src/entities/decision/model/options.ts
|
|
337
|
+
const DATA_FIRST_ARITY_3 = 3;
|
|
338
|
+
const DATA_FIRST_ARITY$2 = 2;
|
|
339
|
+
const PREFIX = "oxlint-plugin-decision";
|
|
340
|
+
const PROVIDER_SEPARATOR = "/";
|
|
341
|
+
const ONE_RULE = 1;
|
|
342
|
+
const DEFAULTS = {
|
|
343
|
+
ci: "fail",
|
|
344
|
+
timeoutMs: 1e4,
|
|
345
|
+
maxSnippetChars: 4e3,
|
|
346
|
+
model: "cloudflare/@cf/cloudflare/clef-flash",
|
|
347
|
+
env: {},
|
|
348
|
+
explain: false
|
|
349
|
+
};
|
|
350
|
+
const isSelector = (input) => Predicate.isFunction(input);
|
|
351
|
+
const RULE_ID = /^[a-z0-9]+(?:-[a-z0-9]+)*$/u;
|
|
352
|
+
const ModelSpecSchema = Schema.Union([DecisionModelSchema, Schema.NonEmptyArray(DecisionModelSchema)]);
|
|
353
|
+
const PositiveInt = Schema.Int.check(Schema.isGreaterThanOrEqualTo(1));
|
|
354
|
+
const RuleId = Schema.NonEmptyString.check(Schema.isPattern(RULE_ID, { message: "id must be lowercase words joined by hyphens" }));
|
|
355
|
+
const OptionsSchema = Schema.Struct({
|
|
356
|
+
ci: Schema.optionalKey(CiBehaviorSchema),
|
|
357
|
+
timeoutMs: Schema.optionalKey(PositiveInt),
|
|
358
|
+
maxSnippetChars: Schema.optionalKey(PositiveInt),
|
|
359
|
+
model: Schema.optionalKey(ModelSpecSchema),
|
|
360
|
+
env: Schema.optionalKey(Schema.Record(Schema.NonEmptyString, Schema.NonEmptyString)),
|
|
361
|
+
explain: Schema.optionalKey(ExplainSettingSchema)
|
|
362
|
+
});
|
|
363
|
+
const RuleSchema = Schema.Struct({
|
|
364
|
+
id: RuleId,
|
|
365
|
+
target: TargetSchema,
|
|
366
|
+
question: Schema.NonEmptyString,
|
|
367
|
+
cutoff: Cutoff,
|
|
368
|
+
message: Schema.optionalKey(Schema.NonEmptyString),
|
|
369
|
+
select: Schema.optionalKey(Schema.declare(isSelector)),
|
|
370
|
+
location: Schema.optionalKey(Schema.Struct({
|
|
371
|
+
question: Schema.NonEmptyString,
|
|
372
|
+
cutoff: Cutoff
|
|
373
|
+
})),
|
|
374
|
+
model: Schema.optionalKey(ModelSpecSchema),
|
|
375
|
+
context: Schema.optionalKey(RuleContextSchema),
|
|
376
|
+
answer: Schema.optionalKey(Schema.Unknown)
|
|
377
|
+
});
|
|
378
|
+
const STRICT = { onExcessProperty: "error" };
|
|
379
|
+
const decodeOptions = Schema.decodeUnknownResult(OptionsSchema, STRICT);
|
|
380
|
+
const decodeRule = Schema.decodeUnknownResult(RuleSchema, STRICT);
|
|
381
|
+
const taggedError = Schema.TaggedError;
|
|
382
|
+
var OptionsError = class extends taggedError()("OptionsError", { message: Schema.String }) {};
|
|
383
|
+
const optionsError = (message) => OptionsError.make({ message: `${PREFIX}: ${message}` });
|
|
384
|
+
const modelIdsOf = (spec) => Array.dedupe(Array.ensure(spec));
|
|
385
|
+
const splitModelId = (id) => {
|
|
386
|
+
const [provider, ...rest] = String$1.split(id, PROVIDER_SEPARATOR);
|
|
387
|
+
const model = pipe(rest, Array.join(PROVIDER_SEPARATOR));
|
|
388
|
+
return Option.liftPredicate([provider, model], ([left, right]) => String$1.isNonEmpty(left) && String$1.isNonEmpty(right));
|
|
389
|
+
};
|
|
390
|
+
const acceptedModel = (ref) => {
|
|
391
|
+
const { models, name } = ref.provider;
|
|
392
|
+
if (!models.test(ref.model)) throw optionsError(`model "${ref.id}" is not a decision model that the "${name}" provider accepts`);
|
|
393
|
+
return ref;
|
|
394
|
+
};
|
|
395
|
+
const resolveModel = Function.dual(DATA_FIRST_ARITY$2, (id, providers) => acceptedModel(splitModelId(id).pipe(Option.flatMap(([name, model]) => Option.map(Option.fromUndefinedOr(providers[name]), (provider) => ({
|
|
396
|
+
id,
|
|
397
|
+
provider,
|
|
398
|
+
model
|
|
399
|
+
}))), Option.getOrThrowWith(() => optionsError(`model "${id}" must be "<provider>/<model>" with a provider from: ${pipe(Record.keys(providers), Array.join(", "))}`)))));
|
|
400
|
+
const duplicateIdOf = (rules) => Array.findFirst(Record.toEntries(Array.groupBy(rules, ({ id }) => id)), ([, same]) => same.length > ONE_RULE).pipe(Option.map(([id]) => id));
|
|
401
|
+
const answerOf = (rule) => {
|
|
402
|
+
if (Predicate.isUndefined(rule.answer)) return BOOLEAN_ANSWER;
|
|
403
|
+
return Result.getOrThrowWith(decodeRuleAnswer(rule.answer), (issue) => optionsError(`rule "${rule.id}" has an invalid answer: ${issue}`));
|
|
404
|
+
};
|
|
405
|
+
const checked = Function.dual(DATA_FIRST_ARITY_3, (decode, input, what) => Result.getOrThrowWith(decode(input), (issue) => optionsError(`invalid ${what}: ${issue.message}`)));
|
|
406
|
+
const checkRule = (rule) => {
|
|
407
|
+
checked(decodeRule, rule, `rule "${rule.id}"`);
|
|
408
|
+
if (Predicate.isNotUndefined(rule.location) && rule.target !== "file") throw optionsError("location is supported only for file rules");
|
|
409
|
+
answerOf(rule);
|
|
410
|
+
return rule;
|
|
411
|
+
};
|
|
412
|
+
const checkRules = (rules) => {
|
|
413
|
+
if (!Array.isReadonlyArrayNonEmpty(rules)) throw optionsError("rules must list at least one rule");
|
|
414
|
+
Option.map(duplicateIdOf(rules), (duplicate) => {
|
|
415
|
+
throw optionsError(`rule id "${duplicate}" is used more than once`);
|
|
416
|
+
});
|
|
417
|
+
return pipe(rules, Array.map((rule) => checkRule(rule)));
|
|
418
|
+
};
|
|
419
|
+
const resolveRule = (fallback, providers) => (rule) => ({
|
|
420
|
+
...rule,
|
|
421
|
+
answer: answerOf(rule),
|
|
422
|
+
models: pipe(modelIdsOf(rule.model ?? fallback), Array.map((id) => resolveModel(id, providers)))
|
|
423
|
+
});
|
|
424
|
+
const settingsFrom = (settings) => {
|
|
425
|
+
if (Predicate.isUndefined(settings)) return {};
|
|
426
|
+
return checked(decodeOptions, settings, "settings");
|
|
427
|
+
};
|
|
428
|
+
const resolveOptions = Function.dual(DATA_FIRST_ARITY_3, ({ options, settings }, rules, providers) => {
|
|
429
|
+
const fromCode = checked(decodeOptions, options, "createDecisionPlugin options");
|
|
430
|
+
const { model, ...merged } = {
|
|
431
|
+
...DEFAULTS,
|
|
432
|
+
...fromCode,
|
|
433
|
+
...settingsFrom(settings)
|
|
434
|
+
};
|
|
435
|
+
return {
|
|
436
|
+
ci: merged.ci,
|
|
437
|
+
timeoutMs: merged.timeoutMs,
|
|
438
|
+
maxSnippetChars: merged.maxSnippetChars,
|
|
439
|
+
env: merged.env,
|
|
440
|
+
explain: merged.explain,
|
|
441
|
+
rules: pipe(checkRules(rules), Array.map((rule) => resolveRule(model, providers)(rule)))
|
|
442
|
+
};
|
|
443
|
+
});
|
|
444
|
+
// src/entities/decision/model/request.ts
|
|
445
|
+
const DATA_FIRST_ARITY$1 = 2;
|
|
446
|
+
const FIRST_LINE = 1;
|
|
447
|
+
const LOCATION_SUFFIX = "_location";
|
|
448
|
+
const TRUNCATION_MARKER = "/* ...truncated */";
|
|
449
|
+
const refAt = (index) => `s${String(index)}`;
|
|
450
|
+
const locationRefOf = (ref) => `${ref}${LOCATION_SUFFIX}`;
|
|
451
|
+
const snippetRefOf = (questionId) => Boolean.match(String$1.endsWith(LOCATION_SUFFIX)(questionId), {
|
|
452
|
+
onTrue: () => String$1.slice(Number$1.decrement(FIRST_LINE), -9)(questionId),
|
|
453
|
+
onFalse: () => questionId
|
|
454
|
+
});
|
|
455
|
+
const truncateSnippet = Function.dual(DATA_FIRST_ARITY$1, (text, maxChars) => Boolean.match(text.length <= maxChars, {
|
|
456
|
+
onTrue: () => text,
|
|
457
|
+
onFalse: () => `${String$1.slice(Number$1.decrement(FIRST_LINE), maxChars)(text)}${TRUNCATION_MARKER}`
|
|
458
|
+
}));
|
|
459
|
+
const numbered = (snippet) => pipe(numberedLines(snippet), Array.map(({ text, line }) => `L${String(line)}| ${text}`), Array.join("\n"));
|
|
460
|
+
const contextNote = (match, ref) => {
|
|
461
|
+
if (Predicate.isUndefined(match.context)) return "";
|
|
462
|
+
return ` state.contexts.${ref} shows the source around it; use it only to understand the snippet.`;
|
|
463
|
+
};
|
|
464
|
+
const mainQuestionFor = (match, ref) => [ref, questionFor(match.rule.answer, `Consider only snippet "${ref}" in state.snippets.${contextNote(match, ref)} ${match.rule.question}`)];
|
|
465
|
+
const questionsFor = (match, ref) => {
|
|
466
|
+
if (!locates(match)) return [mainQuestionFor(match, ref)];
|
|
467
|
+
return [mainQuestionFor(match, ref), [locationRefOf(ref), locationQuestion(match, ref, locationCandidates(match.snippet, EVERY_LINE))]];
|
|
468
|
+
};
|
|
469
|
+
const snippetFor = (match) => {
|
|
470
|
+
if (locates(match)) return numbered(match.snippet);
|
|
471
|
+
return match.snippet;
|
|
472
|
+
};
|
|
473
|
+
const contextsOf = (matches) => {
|
|
474
|
+
const contexts = pipe(matches, Array.flatMap((match, index) => Option.toArray(Option.map(Option.fromUndefinedOr(match.context), (text) => [refAt(index), text]))));
|
|
475
|
+
if (Array.isReadonlyArrayEmpty(contexts)) return {};
|
|
476
|
+
return { contexts: Record.fromEntries(contexts) };
|
|
477
|
+
};
|
|
478
|
+
const buildRequest = Function.dual(DATA_FIRST_ARITY$1, (model, matches) => ({
|
|
479
|
+
model,
|
|
480
|
+
state: {
|
|
481
|
+
snippets: Record.fromEntries(pipe(matches, Array.map((match, index) => [refAt(index), snippetFor(match)]))),
|
|
482
|
+
...contextsOf(matches)
|
|
483
|
+
},
|
|
484
|
+
questions: Record.fromEntries(pipe(matches, Array.flatMap((match, index) => questionsFor(match, refAt(index)))))
|
|
485
|
+
}));
|
|
486
|
+
const choiceQuestionsOf = (request) => pipe(Record.toEntries(request.questions), Array.flatMap(([id, question]) => {
|
|
487
|
+
if (question.type === "choice") return [[id, question.criteria]];
|
|
488
|
+
return [];
|
|
489
|
+
}));
|
|
490
|
+
const mainQuestionsOf = (request) => pipe(Record.toEntries(request.questions), Array.filter(([id]) => !String$1.endsWith(LOCATION_SUFFIX)(id)));
|
|
491
|
+
const resultOf = (answers, [ref, question]) => [ref, Option.getOrThrowWith(verdictOf(question, answers[ref]), () => /* @__PURE__ */ new Error(`response has no valid answer for "${ref}"`))];
|
|
492
|
+
const parseVerdicts = Function.dual(DATA_FIRST_ARITY$1, (response, request) => {
|
|
493
|
+
const { model, answers } = Option.getOrThrowWith(Option.liftPredicate(response, ({ model: served }) => String$1.isNonEmpty(served)), () => /* @__PURE__ */ new Error("response has no model id"));
|
|
494
|
+
return {
|
|
495
|
+
model,
|
|
496
|
+
results: Record.fromEntries(pipe(mainQuestionsOf(request), Array.map((entry) => resultOf(answers, entry)))),
|
|
497
|
+
answers,
|
|
498
|
+
complete: pipe(choiceQuestionsOf(request), Array.every(([id, criteria]) => validChoice(answers[id], criteria)))
|
|
499
|
+
};
|
|
500
|
+
});
|
|
501
|
+
// src/entities/decision/model/runtime.ts
|
|
502
|
+
const DATA_FIRST_ARITY = 2;
|
|
503
|
+
const encodeJson = Schema.encodeUnknownOption(Schema.fromJsonString(Schema.Unknown));
|
|
504
|
+
const toJson = (value) => Option.getOrElse(encodeJson(value), () => String(value));
|
|
505
|
+
const nowMs = () => DateTime.toEpochMillis(DateTime.nowUnsafe());
|
|
506
|
+
const runOrElse = Function.dual(DATA_FIRST_ARITY, (effect, fallback) => Exit.match(Effect.runSyncExit(effect), {
|
|
507
|
+
onSuccess: (value) => value,
|
|
508
|
+
onFailure: (cause) => fallback(Cause.squash(cause))
|
|
509
|
+
}));
|
|
510
|
+
const envOf = (name) => runOrElse(Config.option(Config.NonEmptyString(name)), () => Option.none()).pipe(Option.map((value) => String$1.trim(value)), Option.filter(String$1.isNonEmpty));
|
|
511
|
+
const envNameOf = Function.dual(DATA_FIRST_ARITY, (env, name) => env[name] ?? name);
|
|
512
|
+
const failsInCi = (options) => options.ci === "fail" && Option.isSome(envOf("CI"));
|
|
513
|
+
const warned = /* @__PURE__ */ new Set();
|
|
514
|
+
const warnOnce = Function.dual(DATA_FIRST_ARITY, (reason, message) => {
|
|
515
|
+
if (warned.has(reason)) return;
|
|
516
|
+
warned.add(reason);
|
|
517
|
+
Effect.runFork(Console.warn(`${PREFIX}: ${message}`));
|
|
518
|
+
});
|
|
519
|
+
const messageOf = (error) => {
|
|
520
|
+
if (error instanceof Error) return error.message;
|
|
521
|
+
return String(error);
|
|
522
|
+
};
|
|
523
|
+
const captured = Function.dual(DATA_FIRST_ARITY, (match, name) => match.pipe(Option.flatMap((found) => Option.fromNullishOr(found.groups)), Option.flatMap((groups) => Option.fromNullishOr(groups[name]))));
|
|
524
|
+
// src/entities/decision/model/wire.ts
|
|
525
|
+
const MAX_QUESTIONS = 64;
|
|
526
|
+
const only = (entries, ref) => Option.match(Option.fromUndefinedOr(entries[ref]), {
|
|
527
|
+
onNone: () => ({}),
|
|
528
|
+
onSome: (text) => ({ [ref]: text })
|
|
529
|
+
});
|
|
530
|
+
const stateOf = ({ snippets, contexts }, ref) => {
|
|
531
|
+
if (Predicate.isUndefined(contexts)) return { snippets: only(snippets, ref) };
|
|
532
|
+
return {
|
|
533
|
+
snippets: only(snippets, ref),
|
|
534
|
+
contexts: only(contexts, ref)
|
|
535
|
+
};
|
|
536
|
+
};
|
|
537
|
+
const fileRequestsFor = (request) => {
|
|
538
|
+
const wires = pipe(Array.chunksOf(Record.toEntries(request.questions), MAX_QUESTIONS), Array.map((chunk) => ({
|
|
539
|
+
...request,
|
|
540
|
+
questions: Record.fromEntries(chunk)
|
|
541
|
+
})));
|
|
542
|
+
if (Array.isReadonlyArrayNonEmpty(wires)) return wires;
|
|
543
|
+
return [request];
|
|
544
|
+
};
|
|
545
|
+
const snippetRequestsFor = (request) => {
|
|
546
|
+
const bySnippet = Array.groupBy(Record.toEntries(request.questions), ([id]) => snippetRefOf(id));
|
|
547
|
+
const wires = pipe(Record.toEntries(bySnippet), Array.flatMap(([ref, questions]) => pipe(Array.chunksOf(questions, MAX_QUESTIONS), Array.map((chunk) => ({
|
|
548
|
+
model: request.model,
|
|
549
|
+
state: stateOf(request.state, ref),
|
|
550
|
+
questions: Record.fromEntries(chunk)
|
|
551
|
+
})))));
|
|
552
|
+
if (Array.isReadonlyArrayNonEmpty(wires)) return wires;
|
|
553
|
+
return [request];
|
|
554
|
+
};
|
|
555
|
+
const mergeResponses = (responses) => ({
|
|
556
|
+
model: Array.headNonEmpty(responses).model,
|
|
557
|
+
answers: pipe(responses, Array.flatMap(({ answers }) => Record.toEntries(answers)), Record.fromEntries)
|
|
558
|
+
});
|
|
559
|
+
export { commentedSnippetOf as A, locationQuestion as B, modelIdsOf as C, CLEF_MODEL as D, resolveOptions as E, snippetNodeFor as F, AnswerSchema as H, EVERY_LINE as I, locates as L, lacksComment as M, REPORT_LOC as N, JEV_MODEL as O, TARGET_NODE_TYPES as P, locationCandidates as R, checked as S, resolveModel as T, isReported as U, selectedLocation as V, truncateSnippet as _, envNameOf as a, STRICT as b, messageOf as c, toJson as d, warnOnce as f, refAt as g, parseVerdicts as h, captured as i, contextOf as j, isSelected as k, nowMs as l, locationRefOf as m, mergeResponses as n, envOf as o, buildRequest as p, snippetRequestsFor as r, failsInCi as s, fileRequestsFor as t, runOrElse as u, ModelSpecSchema as v, optionsError as w, checkRules as x, PREFIX as y, locationOfLine as z };
|
|
560
|
+
|
|
561
|
+
//# sourceMappingURL=decision-B7hahWHz.mjs.map
|