|
| 1 | +use emmylua_parser::{LuaAst, LuaAstNode, LuaAstToken, LuaIndexExpr, LuaNameExpr, VisibilityKind}; |
| 2 | +use rowan::TextRange; |
| 3 | + |
| 4 | +use crate::{DiagnosticCode, Emmyrc, LuaPropertyOwnerId, SemanticModel}; |
| 5 | + |
| 6 | +use super::DiagnosticContext; |
| 7 | + |
| 8 | +pub const CODES: &[DiagnosticCode] = &[DiagnosticCode::AccessInvisible]; |
| 9 | + |
| 10 | +pub fn check(context: &mut DiagnosticContext, semantic_model: &mut SemanticModel) -> Option<()> { |
| 11 | + let file_id = semantic_model.get_file_id(); |
| 12 | + let root = semantic_model.get_root().clone(); |
| 13 | + for node in root.descendants::<LuaAst>() { |
| 14 | + match node { |
| 15 | + LuaAst::LuaNameExpr(name_expr) => { |
| 16 | + if semantic_model |
| 17 | + .get_db() |
| 18 | + .get_reference_index() |
| 19 | + .is_write_range(file_id, name_expr.get_range()) |
| 20 | + { |
| 21 | + continue; |
| 22 | + } |
| 23 | + check_name_expr(context, semantic_model, name_expr); |
| 24 | + } |
| 25 | + LuaAst::LuaIndexExpr(index_expr) => { |
| 26 | + if semantic_model |
| 27 | + .get_db() |
| 28 | + .get_reference_index() |
| 29 | + .is_write_range(file_id, index_expr.get_range()) |
| 30 | + { |
| 31 | + continue; |
| 32 | + } |
| 33 | + check_index_expr(context, semantic_model, index_expr); |
| 34 | + } |
| 35 | + _ => {} |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + Some(()) |
| 40 | +} |
| 41 | + |
| 42 | +fn check_name_expr( |
| 43 | + context: &mut DiagnosticContext, |
| 44 | + semantic_model: &mut SemanticModel, |
| 45 | + name_expr: LuaNameExpr, |
| 46 | +) -> Option<()> { |
| 47 | + let property_owner = semantic_model |
| 48 | + .get_property_owner_id(rowan::NodeOrToken::Node(name_expr.syntax().clone()))?; |
| 49 | + |
| 50 | + let name_token = name_expr.get_name_token()?; |
| 51 | + if !semantic_model.is_property_visiable(name_token.syntax().clone(), property_owner.clone()) { |
| 52 | + let emmyrc = semantic_model.get_emmyrc(); |
| 53 | + report_reson(context, &emmyrc, name_token.get_range(), property_owner); |
| 54 | + } |
| 55 | + Some(()) |
| 56 | +} |
| 57 | + |
| 58 | +fn check_index_expr( |
| 59 | + context: &mut DiagnosticContext, |
| 60 | + semantic_model: &mut SemanticModel, |
| 61 | + index_expr: LuaIndexExpr, |
| 62 | +) -> Option<()> { |
| 63 | + let property_owner = semantic_model |
| 64 | + .get_property_owner_id(rowan::NodeOrToken::Node(index_expr.syntax().clone()))?; |
| 65 | + |
| 66 | + let index_token = index_expr.get_index_name_token()?; |
| 67 | + if !semantic_model.is_property_visiable(index_token.clone(), property_owner.clone()) { |
| 68 | + let emmyrc = semantic_model.get_emmyrc(); |
| 69 | + report_reson(context, &emmyrc, index_token.text_range(), property_owner); |
| 70 | + } |
| 71 | + |
| 72 | + Some(()) |
| 73 | +} |
| 74 | + |
| 75 | +fn report_reson( |
| 76 | + context: &mut DiagnosticContext, |
| 77 | + emmyrc: &Emmyrc, |
| 78 | + range: TextRange, |
| 79 | + property_owner_id: LuaPropertyOwnerId, |
| 80 | +) -> Option<()> { |
| 81 | + let property = context |
| 82 | + .db |
| 83 | + .get_property_index() |
| 84 | + .get_property(property_owner_id)?; |
| 85 | + |
| 86 | + if let Some(version_conds) = &property.version_conds { |
| 87 | + let version_number = emmyrc.runtime.version.to_lua_version_number(); |
| 88 | + let visiable = version_conds.iter().any(|cond| cond.check(&version_number)); |
| 89 | + if !visiable { |
| 90 | + let message = t!( |
| 91 | + "The current Lua version %{version} is not accessible; expected %{conds}.", |
| 92 | + version = version_number, |
| 93 | + conds = version_conds |
| 94 | + .iter() |
| 95 | + .map(|it| format!("{}", it)) |
| 96 | + .collect::<Vec<_>>() |
| 97 | + .join(", ") |
| 98 | + ); |
| 99 | + |
| 100 | + context.add_diagnostic( |
| 101 | + DiagnosticCode::AccessInvisible, |
| 102 | + range, |
| 103 | + message.to_string(), |
| 104 | + None, |
| 105 | + ); |
| 106 | + return Some(()); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + if let Some(visiblity) = property.visibility { |
| 111 | + let message = match visiblity { |
| 112 | + VisibilityKind::Protected => { |
| 113 | + t!("The property is protected and cannot be accessed outside its subclasses.") |
| 114 | + } |
| 115 | + VisibilityKind::Private => { |
| 116 | + t!("The property is private and cannot be accessed outside the class.") |
| 117 | + } |
| 118 | + VisibilityKind::Package => { |
| 119 | + t!("The property is package-private and cannot be accessed outside the package.") |
| 120 | + } |
| 121 | + _ => { |
| 122 | + return None; |
| 123 | + } |
| 124 | + }; |
| 125 | + |
| 126 | + context.add_diagnostic( |
| 127 | + DiagnosticCode::AccessInvisible, |
| 128 | + range, |
| 129 | + message.to_string(), |
| 130 | + None, |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + Some(()) |
| 135 | +} |
0 commit comments