linter/lint-config.spice
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2026 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | // Own imports | ||
| 4 | import "@/linter/lint-finding"; | ||
| 5 | |||
| 6 | // Ids of the lint rules. They are the keys of the rules in the [lint.rules] table of the manifest | ||
| 7 | public const string NAMING_CONVENTION_RULE_ID = "naming-convention"; | ||
| 8 | public const string TOO_MANY_PARAMETERS_RULE_ID = "too-many-parameters"; | ||
| 9 | public const string CYCLOMATIC_COMPLEXITY_RULE_ID = "cyclomatic-complexity"; | ||
| 10 | |||
| 11 | // A caller has to hold this many arguments in mind (and their order) at every call site | ||
| 12 | const long DEFAULT_MAX_PARAM_COUNT = 6l; | ||
| 13 | // McCabe's original recommendation for the complexity ceiling of a single routine | ||
| 14 | const long DEFAULT_MAX_CYCLOMATIC_COMPLEXITY = 10l; | ||
| 15 | |||
| 16 | /** | ||
| 17 | * Configuration of a single lint rule. It can be adjusted in the [lint.rules] table of the manifest (spice.toml) | ||
| 18 | */ | ||
| 19 | 3729 | public type LintRuleConfig struct { | |
| 20 | 3729 | public bool enabled = true | |
| 21 | 3729 | public LintSeverity severity = LintSeverity::WARNING | |
| 22 | 3729 | public long max = 0l // Threshold of the rules, that have one, e.g. the maximum number of parameters. Unused otherwise | |
| 23 | } | ||
| 24 | |||
| 25 | /** | ||
| 26 | * Configuration of all lint rules. Defaults to the built-in configuration, if the manifest does not adjust it | ||
| 27 | */ | ||
| 28 | public type LintConfig struct { | ||
| 29 | public LintRuleConfig namingConvention | ||
| 30 | public LintRuleConfig tooManyParameters | ||
| 31 | public LintRuleConfig cyclomaticComplexity | ||
| 32 | } | ||
| 33 | |||
| 34 | 1243 | public p LintConfig.ctor() { | |
| 35 | 1243 | this.tooManyParameters.max = DEFAULT_MAX_PARAM_COUNT; | |
| 36 | 1243 | this.cyclomaticComplexity.max = DEFAULT_MAX_CYCLOMATIC_COMPLEXITY; | |
| 37 | 1243 | } | |
| 38 | |||
| 39 | /** | ||
| 40 | * Get the configuration of the rule with the given id | ||
| 41 | * | ||
| 42 | * @param ruleId Id of the rule | ||
| 43 | * @return Configuration of the rule or nil, if there is no rule with the given id | ||
| 44 | */ | ||
| 45 | 7 | public f<LintRuleConfig*> LintConfig.getRuleConfig(const String& ruleId) { | |
| 46 | 7 | if ruleId == NAMING_CONVENTION_RULE_ID { | |
| 47 | 3 | return &this.namingConvention; | |
| 48 | } | ||
| 49 | 4 | if ruleId == TOO_MANY_PARAMETERS_RULE_ID { | |
| 50 | 2 | return &this.tooManyParameters; | |
| 51 | } | ||
| 52 | 2 | if ruleId == CYCLOMATIC_COMPLEXITY_RULE_ID { | |
| 53 | 1 | return &this.cyclomaticComplexity; | |
| 54 | } | ||
| 55 | 1 | return nil<LintRuleConfig*>; | |
| 56 | } | ||
| 57 | |||
| 58 | /** | ||
| 59 | * Check if the rule with the given id has a threshold, that can be adjusted with the 'max' key | ||
| 60 | * | ||
| 61 | * @param ruleId Id of the rule | ||
| 62 | * @return Has a threshold or not | ||
| 63 | */ | ||
| 64 | 2 | public f<bool> hasRuleThreshold(const String& ruleId) { | |
| 65 | 2 | return ruleId == TOO_MANY_PARAMETERS_RULE_ID || ruleId == CYCLOMATIC_COMPLEXITY_RULE_ID; | |
| 66 | } | ||
| 67 |