From 66b671a5f0752bf12878f4cceb44421d573b44af Mon Sep 17 00:00:00 2001 From: Ned Palacios Date: Mon, 1 Apr 2024 15:47:07 +0800 Subject: [PATCH] fix: illegal expression start not showing --- .../java/illegal_expression_start_error.go | 30 +++++++++++++++---- .../Main.java | 14 +++++++++ .../test.txt | 30 +++++++++++++++++++ 3 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 error_templates/java/test_files/illegal_expression_start_error_if/Main.java create mode 100644 error_templates/java/test_files/illegal_expression_start_error_if/test.txt diff --git a/error_templates/java/illegal_expression_start_error.go b/error_templates/java/illegal_expression_start_error.go index fff6671..ac51662 100644 --- a/error_templates/java/illegal_expression_start_error.go +++ b/error_templates/java/illegal_expression_start_error.go @@ -1,6 +1,8 @@ package java import ( + "strings" + lib "github.com/nedpals/errgoengine" ) @@ -9,11 +11,20 @@ var IllegalExpressionStartError = lib.ErrorTemplate{ Pattern: comptimeErrorPattern(`illegal start of expression`), StackTracePattern: comptimeStackTracePattern, OnAnalyzeErrorFn: func(cd *lib.ContextData, m *lib.MainError) { - for q := m.Nearest.Query("(ERROR) @error"); q.Next(); { - node := q.CurrentNode() - m.Nearest = node - // aCtx.NearestClass = node - break + for nearest := m.Nearest; !nearest.IsNull(); nearest = nearest.Parent() { + found := false + + for q := nearest.Query("(ERROR) @error"); q.Next(); { + node := q.CurrentNode() + m.Nearest = node + found = true + // aCtx.NearestClass = node + break + } + + if found { + break + } } }, OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) { @@ -44,6 +55,15 @@ var IllegalExpressionStartError = lib.ErrorTemplate{ EndPosition: parent.EndPosition(), }) }) + } else if errNodeText := cd.MainError.Nearest.Text(); !strings.HasPrefix(errNodeText, "}") && strings.HasSuffix(errNodeText, "else") { + gen.Add("Use the right closing bracket", func(s *lib.BugFixSuggestion) { + s.AddStep("Ensure that the right closing bracket for the else branch of your if statement is used"). + AddFix(lib.FixSuggestion{ + NewText: "} else", + StartPosition: cd.MainError.Nearest.StartPosition(), + EndPosition: cd.MainError.Nearest.EndPosition(), + }) + }) } }, } diff --git a/error_templates/java/test_files/illegal_expression_start_error_if/Main.java b/error_templates/java/test_files/illegal_expression_start_error_if/Main.java new file mode 100644 index 0000000..29ab1d6 --- /dev/null +++ b/error_templates/java/test_files/illegal_expression_start_error_if/Main.java @@ -0,0 +1,14 @@ +public class Main { + public static void main(String[] args) { + int inventoryCount = 10; + String item = "a"; + System.out.println(item); + + if (true) { + System.out.println("b"); + ) else { + System.out.println("c"); + } + } + } + diff --git a/error_templates/java/test_files/illegal_expression_start_error_if/test.txt b/error_templates/java/test_files/illegal_expression_start_error_if/test.txt new file mode 100644 index 0000000..dd93d05 --- /dev/null +++ b/error_templates/java/test_files/illegal_expression_start_error_if/test.txt @@ -0,0 +1,30 @@ +template: "Java.IllegalExpressionStartError" +name: "If" +--- +Main.java:9: error: illegal start of expression + ) else { + ^ +1 errors +=== +template: "Java.IllegalExpressionStartError" +--- +# IllegalExpressionStartError +This error occurs when the compiler encounters an expression that is not valid. +``` + System.out.println("b"); + ) else { + ^^^^^^ + System.out.println("c"); + } +``` +## Steps to fix +### Use the right closing bracket +Ensure that the right closing bracket for the else branch of your if statement is used. +```diff + if (true) { + System.out.println("b"); +- ) else { ++ } else { + System.out.println("c"); + } +``` \ No newline at end of file