linter/rules/too-many-parameters-rule.spice
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2026 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | // Std imports | ||
| 4 | import "std/type/type-conversion"; | ||
| 5 | import "std/data/vector"; | ||
| 6 | |||
| 7 | // Own imports | ||
| 8 | import "@/ast/ast-nodes"; | ||
| 9 | import "@/linter/lint-finding"; | ||
| 10 | |||
| 11 | // Flags functions/procedures with more parameters than a caller can reasonably keep straight at a call site. | ||
| 12 | // A style/maintainability check (grouping related parameters into a struct is usually the fix), not a | ||
| 13 | // correctness one, so it lives in `spice lint` rather than as a compiler warning. | ||
| 14 | const string TOO_MANY_PARAMETERS_RULE_ID = "too-many-parameters"; | ||
| 15 | |||
| 16 | // A caller has to hold this many arguments in mind (and their order) at every call site. | ||
| 17 | const long MAX_PARAM_COUNT = 6l; | ||
| 18 | |||
| 19 | /** | ||
| 20 | * Emit a finding if the given function/procedure has more parameters than MAX_PARAM_COUNT. | ||
| 21 | * | ||
| 22 | * @param fctDefBase Function/procedure base node | ||
| 23 | * @param kindLabel "Function" or "Procedure", for the finding message | ||
| 24 | * @param findings Findings vector to append to | ||
| 25 | */ | ||
| 26 | 10 | p checkParamCount(ASTFctDefBaseNode& fctDefBase, const string kindLabel, Vector<LintFinding>& findings) { | |
| 27 | 10 | long paramCount = 0l; | |
| 28 | 10 | if fctDefBase.hasParams { | |
| 29 | 10 | const ASTParamLstNode* paramLst = downcast<ASTParamLstNode>(fctDefBase.paramLst); | |
| 30 | 10 | paramCount = paramLst.params.getSize(); | |
| 31 | 10 | } | |
| 32 | 10 | if paramCount > MAX_PARAM_COUNT { | |
| 33 | 1 | const String message = String(kindLabel) + " '" + fctDefBase.name.name + "' has " + toString(paramCount) + | |
| 34 | 1 | " parameters (max " + toString(MAX_PARAM_COUNT) + "); consider grouping related parameters into a struct"; | |
| 35 | 1 | findings.pushBack(LintFinding(fctDefBase.codeLoc, TOO_MANY_PARAMETERS_RULE_ID, LintSeverity::WARNING, message)); | |
| 36 | 1 | } | |
| 37 | 10 | } | |
| 38 | |||
| 39 | 7 | public p checkParamCountFctDef(ASTFctDefNode* node, Vector<LintFinding>& findings) { | |
| 40 | 7 | checkParamCount(node.node, "Function", findings); | |
| 41 | 7 | } | |
| 42 | |||
| 43 | 3 | public p checkParamCountProcDef(ASTProcDefNode* node, Vector<LintFinding>& findings) { | |
| 44 | 3 | checkParamCount(node.node, "Procedure", findings); | |
| 45 | 3 | } | |
| 46 |