use emmylua_diagnostic_macro::LuaDiagnosticMacro;
use emmylua_parser::LuaLanguageLevel;
use lsp_types::DiagnosticSeverity;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[derive(
Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema, LuaDiagnosticMacro,
)]
#[serde(rename_all = "kebab-case")]
pub enum DiagnosticCode {
SyntaxError,
DocSyntaxError,
TypeNotFound,
MissingReturn,
ParamTypeMismatch,
MissingParameter,
RedundantParameter,
UnreachableCode,
Unused,
UndefinedGlobal,
Deprecated,
AccessInvisible,
DiscardReturns,
UndefinedField,
LocalConstReassign,
IterVariableReassign,
DuplicateType,
RedefinedLocal,
RedefinedLabel,
CodeStyleCheck,
NeedCheckNil,
AwaitInSync,
AnnotationUsageError,
ReturnTypeMismatch,
MissingReturnValue,
RedundantReturnValue,
UndefinedDocParam,
DuplicateDocField,
UnknownDocTag,
MissingFields,
InjectField,
CircleDocClass,
IncompleteSignatureDoc,
MissingGlobalDoc,
AssignTypeMismatch,
DuplicateRequire,
NonLiteralExpressionsInAssert,
UnbalancedAssignments,
UnnecessaryAssert,
UnnecessaryIf,
DuplicateSetField,
DuplicateIndex,
GenericConstraintMismatch,
CastTypeMismatch,
UnresolvedRequire,
RequireModuleNotVisible,
EnumValueMismatch,
PreferredLocalAlias,
ReadOnly,
GlobalInNonModule,
AttributeParamTypeMismatch,
AttributeMissingParameter,
AttributeRedundantParameter,
InvertIf,
CallNonCallable,
#[serde(other)]
None,
}
pub fn get_default_severity(code: DiagnosticCode) -> DiagnosticSeverity {
match code {
DiagnosticCode::SyntaxError => DiagnosticSeverity::ERROR,
DiagnosticCode::DocSyntaxError => DiagnosticSeverity::ERROR,
DiagnosticCode::TypeNotFound => DiagnosticSeverity::WARNING,
DiagnosticCode::MissingReturn => DiagnosticSeverity::WARNING,
DiagnosticCode::ParamTypeMismatch => DiagnosticSeverity::WARNING,
DiagnosticCode::MissingParameter => DiagnosticSeverity::WARNING,
DiagnosticCode::UnreachableCode => DiagnosticSeverity::HINT,
DiagnosticCode::Unused => DiagnosticSeverity::HINT,
DiagnosticCode::UndefinedGlobal => DiagnosticSeverity::ERROR,
DiagnosticCode::Deprecated => DiagnosticSeverity::HINT,
DiagnosticCode::AccessInvisible => DiagnosticSeverity::WARNING,
DiagnosticCode::DiscardReturns => DiagnosticSeverity::WARNING,
DiagnosticCode::UndefinedField => DiagnosticSeverity::WARNING,
DiagnosticCode::LocalConstReassign => DiagnosticSeverity::ERROR,
DiagnosticCode::DuplicateType => DiagnosticSeverity::WARNING,
DiagnosticCode::AnnotationUsageError => DiagnosticSeverity::ERROR,
DiagnosticCode::RedefinedLocal => DiagnosticSeverity::HINT,
DiagnosticCode::DuplicateRequire => DiagnosticSeverity::HINT,
DiagnosticCode::UnresolvedRequire => DiagnosticSeverity::WARNING,
DiagnosticCode::IterVariableReassign => DiagnosticSeverity::ERROR,
DiagnosticCode::PreferredLocalAlias => DiagnosticSeverity::HINT,
DiagnosticCode::CallNonCallable => DiagnosticSeverity::WARNING,
_ => DiagnosticSeverity::WARNING,
}
}
pub fn is_code_default_enable(code: &DiagnosticCode, level: LuaLanguageLevel) -> bool {
match code {
DiagnosticCode::IterVariableReassign => level >= LuaLanguageLevel::Lua55,
DiagnosticCode::CodeStyleCheck => false,
DiagnosticCode::IncompleteSignatureDoc => false,
DiagnosticCode::MissingGlobalDoc => false,
DiagnosticCode::UnknownDocTag => false,
DiagnosticCode::NonLiteralExpressionsInAssert => false,
_ => true,
}
}