diff --git a/src/DiffSummary.hs b/src/DiffSummary.hs index b9ce530ab..264fc16ca 100644 --- a/src/DiffSummary.hs +++ b/src/DiffSummary.hs @@ -219,7 +219,9 @@ toTermName source term = case unwrap term of S.Return expr -> maybe "empty" toTermName' expr S.Yield expr -> maybe "empty" toTermName' expr S.Error _ -> termNameFromSource term - S.If expr _ -> termNameFromSource expr + S.If expr _ -> case unwrap expr of + (S.Negate condition) -> termNameFromSource condition + _ -> termNameFromSource expr S.For clauses _ -> termNameFromChildren term clauses S.While expr _ -> toTermName' expr S.DoWhile _ expr -> toTermName' expr @@ -239,7 +241,7 @@ toTermName source term = case unwrap term of S.Export (Just identifier) expr -> "{ " <> intercalate ", " (termNameFromSource <$> expr) <> " }" <> " from " <> toTermName' identifier S.ConditionalAssignment id _ -> toTermName' id S.Until expr _ -> toTermName' expr - S.Unless expr _ -> termNameFromSource expr + S.Negate expr -> toTermName' expr S.Rescue args _ -> intercalate ", " $ toTermName' <$> args where toTermName' = toTermName source termNameFromChildren term children = termNameFromRange (unionRangesFrom (range term) (range <$> children)) diff --git a/src/Language/Ruby.hs b/src/Language/Ruby.hs index 3e804ea22..cc5633e4f 100644 --- a/src/Language/Ruby.hs +++ b/src/Language/Ruby.hs @@ -25,6 +25,16 @@ termConstructor -> IO (Term (S.Syntax Text) (Record '[Range, Category, SourceSpan])) -- ^ The resulting term, in IO. termConstructor source sourceSpan name range children | name == "ERROR" = withDefaultInfo (S.Error children) + | name == "unless_modifier" = case children of + [ lhs, rhs ] -> do + condition <- withDefaultInfo (S.Negate rhs) + withDefaultInfo $ S.If condition [lhs] + _ -> withDefaultInfo $ S.Error children + | name == "unless_statement" = case children of + ( expr : rest ) -> do + condition <- withDefaultInfo (S.Negate expr) + withDefaultInfo $ S.If condition rest + _ -> withDefaultInfo $ S.Error children | otherwise = withDefaultInfo $ case (name, children) of ("array", _ ) -> S.Array children ("assignment", [ identifier, value ]) -> S.Assignment identifier value @@ -75,7 +85,7 @@ termConstructor source sourceSpan name range children ("member_access", [ base, property ]) -> S.MemberAccess base property ("member_access", _ ) -> S.Error children ("method_declaration", _ ) -> case children of - identifier : params : body | category (extract params) == Params -> S.Method identifier (toList (unwrap params)) body + identifier : params : body | Params <- category (extract params) -> S.Method identifier (toList (unwrap params)) body identifier : body -> S.Method identifier [] body _ -> S.Error children ("module_declaration", identifier : body ) -> S.Module identifier body @@ -90,9 +100,6 @@ termConstructor source sourceSpan name range children ("rescue_modifier", [lhs, rhs] ) -> S.Rescue [lhs] [rhs] ("rescue_modifier", _ ) -> S.Error children ("return_statement", _ ) -> S.Return (listToMaybe children) - ("unless_modifier", [ lhs, condition ]) -> S.Unless condition [lhs] - ("unless_modifier", _ ) -> S.Error children - ("unless_statement", expr : rest ) -> S.Unless expr rest ("unless_statement", _ ) -> S.Error children ("until_modifier", [ lhs, condition ]) -> S.Until condition [lhs] ("until_modifier", _ ) -> S.Error children diff --git a/src/Renderer/JSON.hs b/src/Renderer/JSON.hs index 58391d3b0..665d22e3c 100644 --- a/src/Renderer/JSON.hs +++ b/src/Renderer/JSON.hs @@ -128,6 +128,6 @@ syntaxToTermField syntax = case syntax of S.ConditionalAssignment id value -> [ "conditionalIdentifier" .= id ] <> [ "value" .= value ] S.Yield expr -> [ "yieldExpression" .= expr ] S.Until expr body -> [ "untilExpr" .= expr ] <> [ "untilBody" .= body ] - S.Unless expr clauses -> [ "unless" .= expr ] <> childrenFields clauses + S.Negate expr -> [ "negate" .= expr ] S.Rescue args expressions -> [ "args" .= args ] <> childrenFields expressions where childrenFields c = [ "children" .= c ] diff --git a/src/Syntax.hs b/src/Syntax.hs index 9d345c88b..1618bbe39 100644 --- a/src/Syntax.hs +++ b/src/Syntax.hs @@ -80,8 +80,8 @@ data Syntax a f | ConditionalAssignment { conditionalAssignmentId :: f, value :: f } | Yield (Maybe f) | Until { untilExpr :: f, untilBody :: [f] } - -- | An unless statement with an expression and maybe more expression clauses. - | Unless f [f] + -- | A negation has a single expression. + | Negate f -- | A rescue block has a list of arguments to rescue and a list of expressions. | Rescue [f] [f] deriving (Eq, Foldable, Functor, Generic, Generic1, Mergeable, Ord, Show, Traversable, ToJSON) diff --git a/test/corpus/diff-summaries/ruby/unless.json b/test/corpus/diff-summaries/ruby/unless.json index 8a9320580..5ecb0f9ca 100644 --- a/test/corpus/diff-summaries/ruby/unless.json +++ b/test/corpus/diff-summaries/ruby/unless.json @@ -149,7 +149,7 @@ } ] }, - "summary": "Replaced the 'x' identifier with the 'foo' identifier" + "summary": "Replaced the 'x' identifier with the 'foo' identifier in the foo unless statement" }, { "span": { @@ -164,7 +164,7 @@ ] } }, - "summary": "Added the 'bar' identifier" + "summary": "Added the 'bar' identifier in the foo unless statement" }, { "span": { @@ -179,7 +179,7 @@ ] } }, - "summary": "Added the 'bat' identifier" + "summary": "Added the 'bat' identifier in the foo unless statement" }, { "span": { @@ -253,7 +253,7 @@ } ] }, - "summary": "Replaced the 'foo' identifier with the 'x' identifier" + "summary": "Replaced the 'foo' identifier with the 'x' identifier in the x unless statement" }, { "span": { @@ -268,7 +268,7 @@ ] } }, - "summary": "Deleted the 'bar' identifier" + "summary": "Deleted the 'bar' identifier in the x unless statement" }, { "span": { @@ -283,7 +283,7 @@ ] } }, - "summary": "Deleted the 'bat' identifier" + "summary": "Deleted the 'bat' identifier in the x unless statement" }, { "span": {