emmylua_code_analysis/diagnostic/
lua_diagnostic_code.rs

1use 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    /// Syntax error
13    SyntaxError,
14    /// Doc syntax error
15    DocSyntaxError,
16    /// Type not found
17    TypeNotFound,
18    /// Missing return statement
19    MissingReturn,
20    /// Param Type not match
21    ParamTypeMismatch,
22    /// Missing parameter
23    MissingParameter,
24    /// Redundant parameter
25    RedundantParameter,
26    /// Unreachable code
27    UnreachableCode,
28    /// Unused
29    Unused,
30    /// Undefined global
31    UndefinedGlobal,
32    /// Deprecated
33    Deprecated,
34    /// Access invisible
35    AccessInvisible,
36    /// Discard return value
37    DiscardReturns,
38    /// Undefined field
39    UndefinedField,
40    /// Local const reassign
41    LocalConstReassign,
42    /// Iter variable reassign
43    IterVariableReassign,
44    /// Duplicate type
45    DuplicateType,
46    /// Redefined local
47    RedefinedLocal,
48    /// Redefined label
49    RedefinedLabel,
50    /// Code style check
51    CodeStyleCheck,
52    /// Need check nil
53    NeedCheckNil,
54    /// Await in sync
55    AwaitInSync,
56    /// Doc tag usage error
57    AnnotationUsageError,
58    /// Return type mismatch
59    ReturnTypeMismatch,
60    /// Missing return value
61    MissingReturnValue,
62    /// Redundant return value
63    RedundantReturnValue,
64    /// Undefined Doc Param
65    UndefinedDocParam,
66    /// Duplicate doc field
67    DuplicateDocField,
68    /// Unknown doc annotation
69    UnknownDocTag,
70    /// Missing fields
71    MissingFields,
72    /// Inject Field
73    InjectField,
74    /// Circle Doc Class
75    CircleDocClass,
76    /// Incomplete signature doc
77    IncompleteSignatureDoc,
78    /// Missing global doc
79    MissingGlobalDoc,
80    /// Assign type mismatch
81    AssignTypeMismatch,
82    /// Duplicate require
83    DuplicateRequire,
84    /// non-literal-expressions-in-assert
85    NonLiteralExpressionsInAssert,
86    /// Unbalanced assignments
87    UnbalancedAssignments,
88    /// unnecessary-assert
89    UnnecessaryAssert,
90    /// unnecessary-if
91    UnnecessaryIf,
92    /// duplicate-set-field
93    DuplicateSetField,
94    /// duplicate-index
95    DuplicateIndex,
96    /// generic-constraint-mismatch
97    GenericConstraintMismatch,
98    /// cast-type-mismatch
99    CastTypeMismatch,
100    /// require-module-not-visible
101    RequireModuleNotVisible,
102    /// enum-value-mismatch
103    EnumValueMismatch,
104    /// preferred-local-alias
105    PreferredLocalAlias,
106    /// readonly
107    ReadOnly,
108    /// Global variable defined in non-module scope
109    GlobalInNonModule,
110    /// attribute-param-type-mismatch
111    AttributeParamTypeMismatch,
112    /// attribute-missing-parameter
113    AttributeMissingParameter,
114    /// attribute-redundant-parameter
115    AttributeRedundantParameter,
116
117    #[serde(other)]
118    None,
119}
120
121// Update functions to match enum variants
122pub 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        // ... handle other variants
156
157        // neovim-code-style
158        DiagnosticCode::NonLiteralExpressionsInAssert => false,
159
160        _ => true,
161    }
162}