From 0d13819f30d4bdf5ef67bf1589466069e29fa1cd Mon Sep 17 00:00:00 2001 From: joshvera Date: Tue, 10 Jan 2017 16:41:55 -0500 Subject: [PATCH] Make a switch statements clause a Maybe --- src/DiffSummary.hs | 7 +++++-- src/Interpreter.hs | 2 +- src/Language/Go.hs | 8 +++++--- src/Language/JavaScript.hs | 2 +- src/Language/Ruby.hs | 2 +- src/Syntax.hs | 2 +- 6 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/DiffSummary.hs b/src/DiffSummary.hs index 9defc2afd..7b992264c 100644 --- a/src/DiffSummary.hs +++ b/src/DiffSummary.hs @@ -171,6 +171,7 @@ toLeafInfos LeafInfo{..} = pure $ JSONSummary (summary leafCategory termName) so C.EndBlock -> categoryName' C.Yield | Text.null termName -> categoryName' C.Return | Text.null termName -> categoryName' + C.Switch | Text.null termName -> categoryName' _ -> "the" <+> squotes (toDoc termName) <+> toDoc categoryName where termAndCategoryName = "the" <+> toDoc termName <+> toDoc categoryName @@ -222,7 +223,7 @@ toTermName source term = case unwrap term of -- TODO: We should remove Case from Syntax since I don't think we should ever -- evaluate Case as a single toTermName Text - joshvera S.Case expr _ -> termNameFromSource expr - S.Switch expr _ -> toTermName' expr + S.Switch expr _ -> maybe "" toTermName' expr S.Ternary expr _ -> toTermName' expr S.OperatorAssignment id _ -> toTermName' id S.Operator _ -> termNameFromSource term @@ -289,7 +290,9 @@ parentContexts contexts = hsep $ either identifiableDoc annotatableDoc <$> conte C.RescueModifier -> "in the" <+> squotes ("rescue" <+> termName t) <+> "modifier" C.If -> "in the" <+> squotes (termName t) <+> catName c C.Case -> "in the" <+> squotes (termName t) <+> catName c - C.Switch -> "in the" <+> squotes (termName t) <+> catName c + C.Switch -> case t of + "" -> "in a" <+> catName c + _ -> "in the" <+> squotes (termName t) <+> catName c C.When -> "in a" <+> catName c C.BeginBlock -> "in a" <+> catName c C.EndBlock -> "in an" <+> catName c diff --git a/src/Interpreter.hs b/src/Interpreter.hs index c48951afb..cd76a5460 100644 --- a/src/Interpreter.hs +++ b/src/Interpreter.hs @@ -62,7 +62,7 @@ algorithmWithTerms construct t1 t2 = maybe (recursively t1 t2) (fmap annotate) $ S.FunctionCall <$> recursively identifierA identifierB <*> bySimilarity argsA argsB (S.Switch exprA casesA, S.Switch exprB casesB) -> Just $ - S.Switch <$> recursively exprA exprB + S.Switch <$> sequenceA (recursively <$> exprA <*> exprB) <*> bySimilarity casesA casesB (S.Object tyA a, S.Object tyB b) -> Just $ S.Object <$> sequenceA (recursively <$> tyA <*> tyB) diff --git a/src/Language/Go.hs b/src/Language/Go.hs index 4d8fe4087..85e2a3d36 100644 --- a/src/Language/Go.hs +++ b/src/Language/Go.hs @@ -43,9 +43,11 @@ termConstructor source sourceSpan name range children _ = case name of "expression_switch_statement" -> case Prologue.break isCaseClause children of (clauses, cases) -> do - clauses <- withCategory ExpressionStatements (S.Indexed clauses) + clauses' <- case clauses of + [] -> pure Nothing + clauses'' -> Just <$> (withCategory ExpressionStatements (S.Indexed clauses'')) cases' <- sequenceA $ toCase <$> cases - withDefaultInfo $ S.Switch clauses cases' + withDefaultInfo $ S.Switch clauses' cases' where isCaseClause = (== Other "expression_case_clause") . category . extract toCase clause = case toList (unwrap clause) of @@ -63,7 +65,7 @@ termConstructor source sourceSpan name range children _ = case name of case Prologue.break isCaseClause children of (clauses, cases) -> do withDefaultInfo $ case clauses of - [id] -> S.Switch id cases + [id] -> S.Switch (Just id) cases _ -> S.Error children where isCaseClause = (== Case) . category . extract "select_statement" -> withDefaultInfo $ S.Select (toCommunicationCase =<< children) diff --git a/src/Language/JavaScript.hs b/src/Language/JavaScript.hs index 1eeb72aa2..9b7388cae 100644 --- a/src/Language/JavaScript.hs +++ b/src/Language/JavaScript.hs @@ -58,7 +58,7 @@ termConstructor source sourceSpan name range children allChildren ("var_assignment", _ ) -> S.Error children ("var_declaration", _) -> S.Indexed $ toVarDecl <$> children ("trailing_var_declaration", _) -> S.Indexed $ toVarDecl <$> children - ("switch_statement", expr : rest) -> S.Switch expr rest + ("switch_statement", expr : rest) -> S.Switch (Just expr) rest ("switch_statement", _ ) -> S.Error children ("case", [ expr, body ]) -> S.Case expr [body] ("case", _ ) -> S.Error children diff --git a/src/Language/Ruby.hs b/src/Language/Ruby.hs index 6f2e1c665..641a90b09 100644 --- a/src/Language/Ruby.hs +++ b/src/Language/Ruby.hs @@ -70,7 +70,7 @@ termConstructor source sourceSpan name range children allChildren [ elseBlock ] | Else <- category (extract elseBlock) -> S.Try body rescues (Just elseBlock) Nothing [ ensure ] | Ensure <- category (extract ensure) -> S.Try body rescues Nothing (Just ensure) _ -> S.Try body rescues Nothing Nothing - ("case", expr : body ) -> S.Switch expr body + ("case", expr : body ) -> S.Switch (Just expr) body ("case", _ ) -> S.Error children ("when", condition : body ) -> S.Case condition body ("when", _ ) -> S.Error children diff --git a/src/Syntax.hs b/src/Syntax.hs index d872fe08f..4cc28f471 100644 --- a/src/Syntax.hs +++ b/src/Syntax.hs @@ -45,7 +45,7 @@ data Syntax a f -- | A subscript access contains a syntax, and another syntax that indefies a property or value in the first syntax. -- | e.g. in Javascript x["y"] represents a subscript access syntax. | SubscriptAccess { subscriptId :: f, subscriptElement :: f } - | Switch { switchExpr :: f, cases :: [f] } + | Switch { switchExpr :: (Maybe f), cases :: [f] } | Case { caseExpr :: f, caseStatements :: [f] } -- | A default case in a switch statement. | Default [f]