|
| 1 | +#[cfg(test)] |
| 2 | +mod test { |
| 3 | + use emmylua_parser::{LuaAstNode, LuaLocalName}; |
| 4 | + |
| 5 | + use crate::{DiagnosticCode, LuaDeclId, LuaSemanticDeclId, VirtualWorkspace}; |
| 6 | + |
| 7 | + fn assert_type_decl_deprecated(content: &str, name: &str) { |
| 8 | + let mut ws = VirtualWorkspace::new(); |
| 9 | + let file_id = ws.def(content); |
| 10 | + let db = ws.analysis.compilation.get_db(); |
| 11 | + let type_decl = db |
| 12 | + .get_type_index() |
| 13 | + .find_type_decl(file_id, name, db.resolve_workspace_id(file_id)) |
| 14 | + .expect("type declaration must exist"); |
| 15 | + let property = db |
| 16 | + .get_property_index() |
| 17 | + .get_property(&LuaSemanticDeclId::TypeDecl(type_decl.get_id())) |
| 18 | + .expect("type declaration property must exist"); |
| 19 | + |
| 20 | + assert!(property.deprecated().is_some()); |
| 21 | + } |
| 22 | + |
| 23 | + fn assert_lua_decl_deprecated(content: &str, name: &str) { |
| 24 | + let mut ws = VirtualWorkspace::new(); |
| 25 | + let file_id = ws.def(content); |
| 26 | + let db = ws.analysis.compilation.get_db(); |
| 27 | + let local_name = ws.get_node::<LuaLocalName>(file_id); |
| 28 | + assert_eq!(local_name.get_text(), name); |
| 29 | + let decl = db |
| 30 | + .get_decl_index() |
| 31 | + .get_decl(&LuaDeclId::new(file_id, local_name.get_position())) |
| 32 | + .expect("declaration must exist"); |
| 33 | + let property = db |
| 34 | + .get_property_index() |
| 35 | + .get_property(&LuaSemanticDeclId::LuaDecl(decl.get_id())) |
| 36 | + .expect("declaration property must exist"); |
| 37 | + |
| 38 | + assert!(property.deprecated().is_some()); |
| 39 | + } |
| 40 | + |
| 41 | + #[test] |
| 42 | + fn test_deprecated_alias_use() { |
| 43 | + let mut ws = VirtualWorkspace::new(); |
| 44 | + |
| 45 | + assert!(ws.has_no_diagnostic( |
| 46 | + DiagnosticCode::Deprecated, |
| 47 | + r#" |
| 48 | + ---@deprecated test |
| 49 | + ---@alias std.ConstTpl<T> unknown |
| 50 | + "# |
| 51 | + )); |
| 52 | + } |
| 53 | + |
| 54 | + #[test] |
| 55 | + fn test_deprecated_alias_no_usage_error() { |
| 56 | + let mut ws = VirtualWorkspace::new(); |
| 57 | + |
| 58 | + assert!(ws.has_no_diagnostic( |
| 59 | + DiagnosticCode::AnnotationUsageError, |
| 60 | + r#" |
| 61 | + ---@deprecated test |
| 62 | + ---@alias std.ConstTpl<T> unknown |
| 63 | + "# |
| 64 | + )); |
| 65 | + } |
| 66 | + |
| 67 | + #[test] |
| 68 | + fn test_deprecated_alias_attaches_to_type_decl() { |
| 69 | + assert_type_decl_deprecated( |
| 70 | + r#" |
| 71 | + ---@deprecated test |
| 72 | + ---@alias ConstTpl unknown |
| 73 | + "#, |
| 74 | + "ConstTpl", |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + #[test] |
| 79 | + fn test_deprecated_alias_after_alias_attaches_to_type_decl() { |
| 80 | + assert_type_decl_deprecated( |
| 81 | + r#" |
| 82 | + ---@alias ConstTpl unknown |
| 83 | + ---@deprecated test |
| 84 | + "#, |
| 85 | + "ConstTpl", |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + #[test] |
| 90 | + fn test_deprecated_class_no_usage_error() { |
| 91 | + let mut ws = VirtualWorkspace::new(); |
| 92 | + |
| 93 | + assert!(ws.has_no_diagnostic( |
| 94 | + DiagnosticCode::AnnotationUsageError, |
| 95 | + r#" |
| 96 | + ---@deprecated test |
| 97 | + ---@class Foo |
| 98 | + "# |
| 99 | + )); |
| 100 | + } |
| 101 | + |
| 102 | + #[test] |
| 103 | + fn test_deprecated_class_attaches_to_type_decl() { |
| 104 | + assert_type_decl_deprecated( |
| 105 | + r#" |
| 106 | + ---@deprecated test |
| 107 | + ---@class Foo |
| 108 | + local Foo = {} |
| 109 | + "#, |
| 110 | + "Foo", |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + #[test] |
| 115 | + fn test_deprecated_class_usage_diagnostic() { |
| 116 | + let mut ws = VirtualWorkspace::new(); |
| 117 | + |
| 118 | + assert!(!ws.has_no_diagnostic( |
| 119 | + DiagnosticCode::Deprecated, |
| 120 | + r#" |
| 121 | + ---@deprecated test |
| 122 | + ---@class Foo |
| 123 | + local Foo = {} |
| 124 | +
|
| 125 | + local x = Foo |
| 126 | + "# |
| 127 | + )); |
| 128 | + } |
| 129 | + |
| 130 | + #[test] |
| 131 | + fn test_deprecated_class_type_annotation_diagnostic() { |
| 132 | + let mut ws = VirtualWorkspace::new(); |
| 133 | + |
| 134 | + assert!(!ws.has_no_diagnostic( |
| 135 | + DiagnosticCode::Deprecated, |
| 136 | + r#" |
| 137 | + ---@deprecated |
| 138 | + ---@class A |
| 139 | +
|
| 140 | + ---@type A |
| 141 | + local a |
| 142 | + "# |
| 143 | + )); |
| 144 | + } |
| 145 | + |
| 146 | + #[test] |
| 147 | + fn test_deprecated_class_param_annotation_diagnostic() { |
| 148 | + let mut ws = VirtualWorkspace::new(); |
| 149 | + |
| 150 | + assert!(!ws.has_no_diagnostic( |
| 151 | + DiagnosticCode::Deprecated, |
| 152 | + r#" |
| 153 | + ---@deprecated |
| 154 | + ---@class A |
| 155 | +
|
| 156 | + ---@param a A |
| 157 | + local function f(a) |
| 158 | + end |
| 159 | + "# |
| 160 | + )); |
| 161 | + } |
| 162 | + |
| 163 | + #[test] |
| 164 | + fn test_deprecated_class_after_class_attaches_to_decl() { |
| 165 | + assert_lua_decl_deprecated( |
| 166 | + r#" |
| 167 | + ---@class Foo |
| 168 | + ---@deprecated test |
| 169 | + local Foo = {} |
| 170 | + "#, |
| 171 | + "Foo", |
| 172 | + ); |
| 173 | + } |
| 174 | +} |
0 commit comments