1
1
mirror of https://github.com/github/semantic.git synced 2024-12-30 18:36:27 +03:00

Make a switch statements clause a Maybe

This commit is contained in:
joshvera 2017-01-10 16:41:55 -05:00
parent 4d06cbd63c
commit 0d13819f30
6 changed files with 14 additions and 9 deletions

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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]