| 52 | } |
| 53 | |
| 54 | bool LuaCheck::CheckSingleFile(const FormatContext &context, std::string_view path, std::string &&sourceText, LuaStyle style) { |
| 55 | auto file = std::make_shared<LuaSource>(std::move(sourceText)); |
| 56 | LuaLexer luaLexer(file); |
| 57 | if (context.IsNonStandardLua()) { |
| 58 | luaLexer.SupportNonStandardSymbol(); |
| 59 | } |
| 60 | if (context.IsCLikeCommentsSupport()) { |
| 61 | luaLexer.SupportCLikeComments(); |
| 62 | } |
| 63 | |
| 64 | luaLexer.Parse(); |
| 65 | |
| 66 | LuaParser p(file, std::move(luaLexer.GetTokens())); |
| 67 | p.Parse(); |
| 68 | |
| 69 | if (p.HasError()) { |
| 70 | auto &errors = p.GetErrors(); |
| 71 | std::cout << util::format("Check {} ...\t{} error", path, errors.size()) << std::endl; |
| 72 | for (auto &error: errors) { |
| 73 | DiagnosticInspection(error.ErrorMessage, error.ErrorRange, *file, path); |
| 74 | } |
| 75 | |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | LuaSyntaxTree t; |
| 80 | t.BuildTree(p); |
| 81 | auto diagnosticStyle = context.GetDiagnosticStyle(); |
| 82 | DiagnosticBuilder diagnosticBuilder(style, diagnosticStyle); |
| 83 | diagnosticBuilder.CodeStyleCheck(t); |
| 84 | diagnosticBuilder.NameStyleCheck(t); |
| 85 | |
| 86 | auto diagnostics = diagnosticBuilder.GetDiagnosticResults(t); |
| 87 | if (diagnostics.empty()) { |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | if (context.IsDumpJson()) { |
| 92 | std::cerr << util::format("Check {}\t{} warning", path, diagnostics.size()) << std::endl; |
| 93 | |
| 94 | auto arr = nlohmann::json::array(); |
| 95 | for (auto &d: diagnostics) { |
| 96 | auto obj = DiagnosticToJson(d.Message, d.Range, *file); |
| 97 | arr.push_back(obj); |
| 98 | } |
| 99 | std::cout << arr.dump() << std::endl; |
| 100 | |
| 101 | return false; |
| 102 | } else { |
| 103 | std::cerr << util::format("Check {}\t{} warning", path, diagnostics.size()) << std::endl; |
| 104 | |
| 105 | for (auto &d: diagnostics) { |
| 106 | DiagnosticInspection(d.Message, d.Range, *file, path); |
| 107 | } |
| 108 | |
| 109 | return false; |
| 110 | } |
| 111 | } |
nothing calls this directly
no test coverage detected