|
| 1 | +/* |
| 2 | + * Copyright (c) 2026, Salesforce, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * For full license text, see the LICENSE file in the repo root or https://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + */ |
| 7 | + |
| 8 | +/** |
| 9 | + * setVariables I/O validation — validates that `with` clause parameters in |
| 10 | + * @utils.setVariables reasoning actions reference defined mutable variables. |
| 11 | + * |
| 12 | + * Diagnostics: set-variables-unknown-variable, set-variables-immutable-target |
| 13 | + */ |
| 14 | + |
| 15 | +import type { LintPass } from '@agentscript/language'; |
| 16 | +import { |
| 17 | + defineRule, |
| 18 | + each, |
| 19 | + attachDiagnostic, |
| 20 | + findSuggestion, |
| 21 | + lintDiagnostic, |
| 22 | +} from '@agentscript/language'; |
| 23 | +import type { CstMeta, SyntaxNode } from '@agentscript/types'; |
| 24 | +import { toRange, DiagnosticSeverity } from '@agentscript/types'; |
| 25 | +import { setVariablesEntriesKey } from './reasoning-actions.js'; |
| 26 | +import { typeMapKey } from './type-map.js'; |
| 27 | + |
| 28 | +export function setVariablesIoRule(): LintPass { |
| 29 | + return defineRule({ |
| 30 | + id: 'set-variables-io', |
| 31 | + description: |
| 32 | + 'Validates with clause params in @utils.setVariables reference defined mutable variables', |
| 33 | + deps: { entry: each(setVariablesEntriesKey), typeMap: typeMapKey }, |
| 34 | + |
| 35 | + run({ entry, typeMap }) { |
| 36 | + const { statements } = entry; |
| 37 | + if (!statements) return; |
| 38 | + |
| 39 | + for (const stmt of statements) { |
| 40 | + if (stmt.__kind !== 'WithClause') continue; |
| 41 | + const param = stmt.param as string; |
| 42 | + if (!param) continue; |
| 43 | + |
| 44 | + const varInfo = typeMap.variables.get(param); |
| 45 | + if (!varInfo) { |
| 46 | + const cst = stmt.__cst as CstMeta | undefined; |
| 47 | + if (!cst) continue; |
| 48 | + const paramCstNode = (stmt as { __paramCstNode?: SyntaxNode }) |
| 49 | + .__paramCstNode; |
| 50 | + const range = paramCstNode ? toRange(paramCstNode) : cst.range; |
| 51 | + |
| 52 | + const suggestion = findSuggestion(param, [ |
| 53 | + ...typeMap.variables.keys(), |
| 54 | + ]); |
| 55 | + const msg = `'${param}' is not a defined variable. @utils.setVariables can only assign to declared variables.`; |
| 56 | + attachDiagnostic( |
| 57 | + stmt, |
| 58 | + lintDiagnostic( |
| 59 | + range, |
| 60 | + msg, |
| 61 | + DiagnosticSeverity.Error, |
| 62 | + 'set-variables-unknown-variable', |
| 63 | + { suggestion } |
| 64 | + ) |
| 65 | + ); |
| 66 | + continue; |
| 67 | + } |
| 68 | + |
| 69 | + if (varInfo.modifier !== 'mutable') { |
| 70 | + const cst = stmt.__cst as CstMeta | undefined; |
| 71 | + if (!cst) continue; |
| 72 | + const paramCstNode = (stmt as { __paramCstNode?: SyntaxNode }) |
| 73 | + .__paramCstNode; |
| 74 | + const range = paramCstNode ? toRange(paramCstNode) : cst.range; |
| 75 | + |
| 76 | + const qualifier = varInfo.modifier ?? 'non-mutable'; |
| 77 | + const msg = `'${param}' is a ${qualifier} variable. @utils.setVariables can only assign to mutable variables.`; |
| 78 | + attachDiagnostic( |
| 79 | + stmt, |
| 80 | + lintDiagnostic( |
| 81 | + range, |
| 82 | + msg, |
| 83 | + DiagnosticSeverity.Error, |
| 84 | + 'set-variables-immutable-target' |
| 85 | + ) |
| 86 | + ); |
| 87 | + } |
| 88 | + } |
| 89 | + }, |
| 90 | + }); |
| 91 | +} |
0 commit comments