|
| 1 | +use std::collections::HashSet; |
| 2 | + |
| 3 | +use emmylua_parser::{LuaAst, LuaAstNode, LuaCallExpr, LuaIndexExpr, LuaVarExpr}; |
| 4 | + |
| 5 | +use crate::{ |
| 6 | + DiagnosticCode, LuaSemanticDeclId, LuaType, ModuleInfo, SemanticDeclLevel, SemanticModel, |
| 7 | + parse_require_module_info, |
| 8 | +}; |
| 9 | + |
| 10 | +use super::{Checker, DiagnosticContext, check_field, humanize_lint_type}; |
| 11 | + |
| 12 | +pub struct CheckExportChecker; |
| 13 | + |
| 14 | +impl Checker for CheckExportChecker { |
| 15 | + const CODES: &[DiagnosticCode] = &[DiagnosticCode::InjectField, DiagnosticCode::UndefinedField]; |
| 16 | + |
| 17 | + fn check(context: &mut DiagnosticContext, semantic_model: &SemanticModel) { |
| 18 | + let root = semantic_model.get_root().clone(); |
| 19 | + let mut checked_index_expr = HashSet::new(); |
| 20 | + for node in root.descendants::<LuaAst>() { |
| 21 | + match node { |
| 22 | + LuaAst::LuaAssignStat(assign) => { |
| 23 | + let (vars, _) = assign.get_var_and_expr_list(); |
| 24 | + for var in vars.iter() { |
| 25 | + if let LuaVarExpr::IndexExpr(index_expr) = var { |
| 26 | + checked_index_expr.insert(index_expr.syntax().clone()); |
| 27 | + check_export_index_expr( |
| 28 | + context, |
| 29 | + semantic_model, |
| 30 | + index_expr, |
| 31 | + DiagnosticCode::InjectField, |
| 32 | + ); |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + LuaAst::LuaIndexExpr(index_expr) => { |
| 37 | + if checked_index_expr.contains(index_expr.syntax()) { |
| 38 | + continue; |
| 39 | + } |
| 40 | + check_export_index_expr( |
| 41 | + context, |
| 42 | + semantic_model, |
| 43 | + &index_expr, |
| 44 | + DiagnosticCode::UndefinedField, |
| 45 | + ); |
| 46 | + } |
| 47 | + _ => {} |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +fn check_export_index_expr( |
| 54 | + context: &mut DiagnosticContext, |
| 55 | + semantic_model: &SemanticModel, |
| 56 | + index_expr: &LuaIndexExpr, |
| 57 | + code: DiagnosticCode, |
| 58 | +) -> Option<()> { |
| 59 | + let db = context.db; |
| 60 | + let prefix_expr = index_expr.get_prefix_expr()?; |
| 61 | + let prefix_info = semantic_model.get_semantic_info(prefix_expr.syntax().clone().into())?; |
| 62 | + let prefix_typ = prefix_info.typ.clone(); |
| 63 | + |
| 64 | + // `check_export` 仅需要处理 `TableConst, 其它类型由 `check_field` 负责. |
| 65 | + let LuaType::TableConst(table_const) = &prefix_typ else { |
| 66 | + return Some(()); |
| 67 | + }; |
| 68 | + |
| 69 | + let index_key = index_expr.get_index_key()?; |
| 70 | + |
| 71 | + // 检查该表是否为导入的表. |
| 72 | + if let Some(module_info) = check_require_table_const_with_export(semantic_model, index_expr) { |
| 73 | + if code == DiagnosticCode::InjectField { |
| 74 | + // 检查字段定义是否来自导入的表. |
| 75 | + if let Some(info) = semantic_model.get_semantic_info(index_expr.syntax().clone().into()) |
| 76 | + && is_cross_file_member_from_imported_export_table_const( |
| 77 | + module_info, |
| 78 | + info.semantic_decl, |
| 79 | + ) |
| 80 | + { |
| 81 | + let index_name = index_key.get_path_part(); |
| 82 | + context.add_diagnostic( |
| 83 | + DiagnosticCode::InjectField, |
| 84 | + index_key.get_range()?, |
| 85 | + t!( |
| 86 | + "Fields cannot be injected into the reference of `%{class}` for `%{field}`. ", |
| 87 | + class = humanize_lint_type(db, &prefix_typ), |
| 88 | + field = index_name, |
| 89 | + ) |
| 90 | + .to_string(), |
| 91 | + None, |
| 92 | + ); |
| 93 | + return Some(()); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if check_field::is_valid_member(semantic_model, &prefix_typ, index_expr, &index_key, code) |
| 98 | + .is_some() |
| 99 | + { |
| 100 | + return Some(()); |
| 101 | + } |
| 102 | + |
| 103 | + let index_name = index_key.get_path_part(); |
| 104 | + match code { |
| 105 | + DiagnosticCode::InjectField => { |
| 106 | + context.add_diagnostic( |
| 107 | + DiagnosticCode::InjectField, |
| 108 | + index_key.get_range()?, |
| 109 | + t!( |
| 110 | + "Fields cannot be injected into the reference of `%{class}` for `%{field}`. ", |
| 111 | + class = humanize_lint_type(db, &prefix_typ), |
| 112 | + field = index_name, |
| 113 | + ) |
| 114 | + .to_string(), |
| 115 | + None, |
| 116 | + ); |
| 117 | + } |
| 118 | + DiagnosticCode::UndefinedField => { |
| 119 | + context.add_diagnostic( |
| 120 | + DiagnosticCode::UndefinedField, |
| 121 | + index_key.get_range()?, |
| 122 | + t!("Undefined field `%{field}`. ", field = index_name,).to_string(), |
| 123 | + None, |
| 124 | + ); |
| 125 | + } |
| 126 | + _ => {} |
| 127 | + } |
| 128 | + |
| 129 | + return Some(()); |
| 130 | + } |
| 131 | + |
| 132 | + // 不是导入表, 且定义位于当前文件中, 则尝试检查本地表. |
| 133 | + if code != DiagnosticCode::UndefinedField && table_const.file_id != semantic_model.get_file_id() |
| 134 | + { |
| 135 | + return Some(()); |
| 136 | + } |
| 137 | + |
| 138 | + let Some(LuaSemanticDeclId::LuaDecl(decl_id)) = prefix_info.semantic_decl else { |
| 139 | + return Some(()); |
| 140 | + }; |
| 141 | + // 必须为 local 声明 |
| 142 | + let decl = semantic_model |
| 143 | + .get_db() |
| 144 | + .get_decl_index() |
| 145 | + .get_decl(&decl_id)?; |
| 146 | + if !decl.is_local() { |
| 147 | + return Some(()); |
| 148 | + } |
| 149 | + // 且该声明标记了 `export` |
| 150 | + let property = semantic_model |
| 151 | + .get_db() |
| 152 | + .get_property_index() |
| 153 | + .get_property(&decl_id.into())?; |
| 154 | + if property.export().is_none() { |
| 155 | + return Some(()); |
| 156 | + } |
| 157 | + |
| 158 | + if check_field::is_valid_member(semantic_model, &prefix_typ, index_expr, &index_key, code) |
| 159 | + .is_some() |
| 160 | + { |
| 161 | + return Some(()); |
| 162 | + } |
| 163 | + |
| 164 | + let index_name = index_key.get_path_part(); |
| 165 | + context.add_diagnostic( |
| 166 | + DiagnosticCode::UndefinedField, |
| 167 | + index_key.get_range()?, |
| 168 | + t!("Undefined field `%{field}`. ", field = index_name,).to_string(), |
| 169 | + None, |
| 170 | + ); |
| 171 | + |
| 172 | + Some(()) |
| 173 | +} |
| 174 | + |
| 175 | +fn check_require_table_const_with_export<'a>( |
| 176 | + semantic_model: &'a SemanticModel, |
| 177 | + index_expr: &LuaIndexExpr, |
| 178 | +) -> Option<&'a ModuleInfo> { |
| 179 | + // 获取前缀表达式的语义信息 |
| 180 | + let prefix_expr = index_expr.get_prefix_expr()?; |
| 181 | + if let Some(call_expr) = LuaCallExpr::cast(prefix_expr.syntax().clone()) { |
| 182 | + let module_info = parse_require_expr_module_info(semantic_model, &call_expr)?; |
| 183 | + if module_info.is_export(semantic_model.get_db()) { |
| 184 | + return Some(module_info); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + let semantic_decl_id = semantic_model.find_decl( |
| 189 | + prefix_expr.syntax().clone().into(), |
| 190 | + SemanticDeclLevel::NoTrace, |
| 191 | + )?; |
| 192 | + // 检查是否是声明引用 |
| 193 | + let decl_id = match semantic_decl_id { |
| 194 | + LuaSemanticDeclId::LuaDecl(decl_id) => decl_id, |
| 195 | + _ => return None, |
| 196 | + }; |
| 197 | + |
| 198 | + // 获取声明 |
| 199 | + let decl = semantic_model |
| 200 | + .get_db() |
| 201 | + .get_decl_index() |
| 202 | + .get_decl(&decl_id)?; |
| 203 | + |
| 204 | + let module_info = parse_require_module_info(semantic_model, &decl)?; |
| 205 | + if module_info.is_export(semantic_model.get_db()) { |
| 206 | + return Some(module_info); |
| 207 | + } |
| 208 | + None |
| 209 | +} |
| 210 | + |
| 211 | +fn parse_require_expr_module_info<'a>( |
| 212 | + semantic_model: &'a SemanticModel, |
| 213 | + call_expr: &LuaCallExpr, |
| 214 | +) -> Option<&'a ModuleInfo> { |
| 215 | + let arg_list = call_expr.get_args_list()?; |
| 216 | + let first_arg = arg_list.get_args().next()?; |
| 217 | + let require_path_type = semantic_model.infer_expr(first_arg.clone()).ok()?; |
| 218 | + let module_path: String = match &require_path_type { |
| 219 | + LuaType::StringConst(module_path) => module_path.as_ref().to_string(), |
| 220 | + _ => return None, |
| 221 | + }; |
| 222 | + |
| 223 | + semantic_model |
| 224 | + .get_db() |
| 225 | + .get_module_index() |
| 226 | + .find_module(&module_path) |
| 227 | +} |
| 228 | + |
| 229 | +fn is_cross_file_member_from_imported_export_table_const( |
| 230 | + module_info: &ModuleInfo, |
| 231 | + semantic_decl: Option<LuaSemanticDeclId>, |
| 232 | +) -> bool { |
| 233 | + if let Some(LuaSemanticDeclId::Member(member_id)) = semantic_decl |
| 234 | + && module_info.file_id != member_id.file_id |
| 235 | + { |
| 236 | + return true; |
| 237 | + } |
| 238 | + |
| 239 | + false |
| 240 | +} |
0 commit comments