emmylua_code_analysis/diagnostic/
lua_diagnostic_code.rs1use emmylua_diagnostic_macro::LuaDiagnosticMacro;
2use emmylua_parser::LuaLanguageLevel;
3use lsp_types::DiagnosticSeverity;
4use schemars::JsonSchema;
5use serde::{Deserialize, Serialize};
6
7#[derive(
8 Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema, LuaDiagnosticMacro,
9)]
10#[serde(rename_all = "kebab-case")]
11pub enum DiagnosticCode {
12 SyntaxError,
14 DocSyntaxError,
16 TypeNotFound,
18 MissingReturn,
20 ParamTypeMismatch,
22 MissingParameter,
24 RedundantParameter,
26 UnreachableCode,
28 Unused,
30 UndefinedGlobal,
32 Deprecated,
34 AccessInvisible,
36 DiscardReturns,
38 UndefinedField,
40 LocalConstReassign,
42 IterVariableReassign,
44 DuplicateType,
46 RedefinedLocal,
48 RedefinedLabel,
50 CodeStyleCheck,
52 NeedCheckNil,
54 AwaitInSync,
56 AnnotationUsageError,
58 ReturnTypeMismatch,
60 MissingReturnValue,
62 RedundantReturnValue,
64 UndefinedDocParam,
66 DuplicateDocField,
68 UnknownDocTag,
70 MissingFields,
72 InjectField,
74 CircleDocClass,
76 IncompleteSignatureDoc,
78 MissingGlobalDoc,
80 AssignTypeMismatch,
82 DuplicateRequire,
84 NonLiteralExpressionsInAssert,
86 UnbalancedAssignments,
88 UnnecessaryAssert,
90 UnnecessaryIf,
92 DuplicateSetField,
94 DuplicateIndex,
96 GenericConstraintMismatch,
98 CastTypeMismatch,
100 RequireModuleNotVisible,
102 EnumValueMismatch,
104 PreferredLocalAlias,
106 ReadOnly,
108 GlobalInNonModule,
110 AttributeParamTypeMismatch,
112 AttributeMissingParameter,
114 AttributeRedundantParameter,
116
117 #[serde(other)]
118 None,
119}
120
121pub fn get_default_severity(code: DiagnosticCode) -> DiagnosticSeverity {
123 match code {
124 DiagnosticCode::SyntaxError => DiagnosticSeverity::ERROR,
125 DiagnosticCode::DocSyntaxError => DiagnosticSeverity::ERROR,
126 DiagnosticCode::TypeNotFound => DiagnosticSeverity::WARNING,
127 DiagnosticCode::MissingReturn => DiagnosticSeverity::WARNING,
128 DiagnosticCode::ParamTypeMismatch => DiagnosticSeverity::WARNING,
129 DiagnosticCode::MissingParameter => DiagnosticSeverity::WARNING,
130 DiagnosticCode::UnreachableCode => DiagnosticSeverity::HINT,
131 DiagnosticCode::Unused => DiagnosticSeverity::HINT,
132 DiagnosticCode::UndefinedGlobal => DiagnosticSeverity::ERROR,
133 DiagnosticCode::Deprecated => DiagnosticSeverity::HINT,
134 DiagnosticCode::AccessInvisible => DiagnosticSeverity::WARNING,
135 DiagnosticCode::DiscardReturns => DiagnosticSeverity::WARNING,
136 DiagnosticCode::UndefinedField => DiagnosticSeverity::WARNING,
137 DiagnosticCode::LocalConstReassign => DiagnosticSeverity::ERROR,
138 DiagnosticCode::DuplicateType => DiagnosticSeverity::WARNING,
139 DiagnosticCode::AnnotationUsageError => DiagnosticSeverity::ERROR,
140 DiagnosticCode::RedefinedLocal => DiagnosticSeverity::HINT,
141 DiagnosticCode::DuplicateRequire => DiagnosticSeverity::HINT,
142 DiagnosticCode::IterVariableReassign => DiagnosticSeverity::ERROR,
143 DiagnosticCode::PreferredLocalAlias => DiagnosticSeverity::HINT,
144 _ => DiagnosticSeverity::WARNING,
145 }
146}
147
148pub fn is_code_default_enable(code: &DiagnosticCode, level: LuaLanguageLevel) -> bool {
149 match code {
150 DiagnosticCode::IterVariableReassign => level >= LuaLanguageLevel::Lua55,
151 DiagnosticCode::CodeStyleCheck => false,
152 DiagnosticCode::IncompleteSignatureDoc => false,
153 DiagnosticCode::MissingGlobalDoc => false,
154 DiagnosticCode::UnknownDocTag => false,
155 DiagnosticCode::NonLiteralExpressionsInAssert => false,
159
160 _ => true,
161 }
162}