1
1
mirror of https://github.com/github/semantic.git synced 2024-12-01 09:15:01 +03:00

Refactor ruby case/when into switch/case Syntax and start on elsif

This commit is contained in:
Timothy Clem 2016-11-04 15:35:09 -07:00
parent 961ea9a1de
commit 463d075879
8 changed files with 51 additions and 46 deletions

View File

@ -55,7 +55,10 @@ identifiable term = isIdentifiable (unwrap term) term
S.Import{} -> Identifiable
S.Export{} -> Identifiable
S.Ternary{} -> Identifiable
S.If{} -> Identifiable
S.BlockExpression{} -> Identifiable
S.Switch{} -> Identifiable
S.Case{} -> Identifiable
S.Rescue{} -> Identifiable
_ -> Unidentifiable
@ -206,7 +209,7 @@ toTermName source term = case unwrap term of
S.VarDecl decl -> toTermName' decl
-- 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 _ -> toTermName' expr
S.Case expr _ -> termNameFromSource expr
S.Switch expr _ -> toTermName' expr
S.Ternary expr _ -> toTermName' expr
S.MathAssignment id _ -> toTermName' id
@ -267,7 +270,9 @@ parentContexts contexts = hsep $ either identifiableDoc annotatableDoc <$> conte
"" -> "in a" <+> catName c
_ -> "in the" <+> squotes (termName t) <+> catName c
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.When -> "in a" <+> catName c
_ -> "in the" <+> termName t <+> catName c
annotatableDoc (c, t) = "of the" <+> squotes (termName t) <+> catName c

View File

@ -54,7 +54,7 @@ termConstructor source sourceSpan name range children
("var_declaration", _) -> S.Indexed $ toVarDecl <$> children
("switch_statement", expr : rest) -> S.Switch expr rest
("switch_statement", _ ) -> S.Error children
("case", [ expr, body ]) -> S.Case expr body
("case", [ expr, body ]) -> S.Case expr [body]
("case", _ ) -> S.Error children
("object", _) -> S.Object $ foldMap toTuple children
("pair", _) -> S.Fixed children

View File

@ -18,9 +18,6 @@ functions = [ "lambda_literal", "lambda_expression" ]
blocks :: [Text]
blocks = [ "begin_statement", "else_block", "ensure_block" ]
conditionalBlocks :: [Text]
conditionalBlocks = [ "elsif_block", "case_statement", "when_block" ]
termConstructor
:: Source Char -- ^ The source that the term occurs within.
-> IO SourceSpan -- ^ The span that the term occupies. This is passed in 'IO' to guarantee some access constraints & encourage its use only when needed (improving performance).
@ -34,6 +31,10 @@ termConstructor source sourceSpan name range children
("array", _) -> S.Array children
("assignment", [ identifier, value ]) -> S.Assignment identifier value
("assignment", _ ) -> S.Error children
("case_statement", expr : body ) -> S.Switch expr body
("case_statement", _ ) -> S.Error children
("when_block", condition : body ) -> S.Case condition body
("when_block", _ ) -> S.Error children
("class_declaration", [ identifier, superclass, definitions ]) -> S.Class identifier (Just superclass) (toList (unwrap definitions))
("class_declaration", [ identifier, definitions ]) -> S.Class identifier Nothing (toList (unwrap definitions))
("class_declaration", _ ) -> S.Error children
@ -51,8 +52,10 @@ termConstructor source sourceSpan name range children
("hash", _) -> S.Object $ foldMap toTuple children
("if_modifier", [ lhs, condition ]) -> S.If condition [lhs]
("if_modifier", _ ) -> S.Error children
("if_statement", expr : rest ) -> S.If expr rest
("if_statement", condition : body ) -> S.If condition body
("if_statement", _ ) -> S.Error children
("elsif_block", condition : body ) -> S.If condition body
("elsif_block", _ ) -> S.Error children
("element_reference", [ base, element ]) -> S.SubscriptAccess base element
("element_reference", _ ) -> S.Error children
("for_statement", lhs : expr : rest ) -> S.For [lhs, expr] rest
@ -92,9 +95,6 @@ termConstructor source sourceSpan name range children
("while_statement", _ ) -> S.Error children
("yield", _) -> S.Yield (listToMaybe children)
_ | name `elem` blocks -> S.BlockExpression Nothing children
_ | name `elem` conditionalBlocks -> case children of
( condition : rest ) -> S.BlockExpression (Just condition) rest
_ -> S.Error children
_ | name `elem` operators -> S.Operator children
_ | name `elem` functions -> case children of
[ body ] -> S.AnonymousFunction [] [body]

View File

@ -46,7 +46,7 @@ data Syntax a f
-- | e.g. in Javascript x["y"] represents a subscript access syntax.
| SubscriptAccess { subscriptId :: f, subscriptElement :: f }
| Switch { switchExpr :: f, cases :: [f] }
| Case { caseExpr :: f, caseStatements :: f }
| Case { caseExpr :: f, caseStatements :: [f] }
| Object { keyValues :: [f] }
-- | A pair in an Object. e.g. foo: bar or foo => bar
| Pair f f

View File

@ -121,7 +121,7 @@
}
]
},
"summary": "Replaced the 'g' identifier with the 'x' identifier"
"summary": "Replaced the 'g' identifier with the 'x' identifier in the 'x' if statement"
},
{
"span": {
@ -148,7 +148,7 @@
}
]
},
"summary": "Replaced the 'h' identifier with the 'y' identifier"
"summary": "Replaced the 'h' identifier with the 'y' identifier in the 'x' if statement"
},
{
"span": {
@ -175,7 +175,7 @@
}
]
},
"summary": "Replaced the 'i' identifier with the 'a' identifier"
"summary": "Replaced the 'i' identifier with the 'a' identifier in the 'a' if statement"
},
{
"span": {
@ -190,7 +190,7 @@
]
}
},
"summary": "Added the 'b' identifier"
"summary": "Added the 'b' identifier in the 'a' if statement"
},
{
"span": {
@ -205,7 +205,7 @@
]
}
},
"summary": "Deleted the 'j' identifier"
"summary": "Deleted the 'j' identifier in the 'a' if statement"
},
{
"span": {
@ -232,7 +232,7 @@
}
]
},
"summary": "Replaced the 'k' identifier with the 'c' identifier"
"summary": "Replaced the 'k' identifier with the 'c' identifier in the 'c' if statement"
},
{
"span": {
@ -259,7 +259,7 @@
}
]
},
"summary": "Replaced the 'l' identifier with the 'd' identifier"
"summary": "Replaced the 'l' identifier with the 'd' identifier in the 'c' if statement"
},
{
"span": {
@ -286,7 +286,7 @@
}
]
},
"summary": "Replaced the 'm' identifier with the 'e' identifier"
"summary": "Replaced the 'm' identifier with the 'e' identifier in the 'e' if statement"
},
{
"span": {
@ -301,7 +301,7 @@
]
}
},
"summary": "Added the 'f' identifier"
"summary": "Added the 'f' identifier in the 'e' if statement"
},
{
"span": {
@ -316,7 +316,7 @@
]
}
},
"summary": "Deleted the 'n' identifier"
"summary": "Deleted the 'n' identifier in the 'e' if statement"
},
{
"span": {
@ -343,7 +343,7 @@
}
]
},
"summary": "Replaced the 'o' identifier with the 'g' identifier"
"summary": "Replaced the 'o' identifier with the 'g' identifier in the 'e' if statement"
}
]
},
@ -396,7 +396,7 @@
}
]
},
"summary": "Replaced the 'x' identifier with the 'g' identifier"
"summary": "Replaced the 'x' identifier with the 'g' identifier in the 'g' if statement"
},
{
"span": {
@ -423,7 +423,7 @@
}
]
},
"summary": "Replaced the 'y' identifier with the 'h' identifier"
"summary": "Replaced the 'y' identifier with the 'h' identifier in the 'g' if statement"
},
{
"span": {
@ -450,7 +450,7 @@
}
]
},
"summary": "Replaced the 'a' identifier with the 'i' identifier"
"summary": "Replaced the 'a' identifier with the 'i' identifier in the 'i' if statement"
},
{
"span": {
@ -465,7 +465,7 @@
]
}
},
"summary": "Added the 'j' identifier"
"summary": "Added the 'j' identifier in the 'i' if statement"
},
{
"span": {
@ -480,7 +480,7 @@
]
}
},
"summary": "Deleted the 'b' identifier"
"summary": "Deleted the 'b' identifier in the 'i' if statement"
},
{
"span": {
@ -507,7 +507,7 @@
}
]
},
"summary": "Replaced the 'c' identifier with the 'k' identifier"
"summary": "Replaced the 'c' identifier with the 'k' identifier in the 'k' if statement"
},
{
"span": {
@ -534,7 +534,7 @@
}
]
},
"summary": "Replaced the 'd' identifier with the 'l' identifier"
"summary": "Replaced the 'd' identifier with the 'l' identifier in the 'k' if statement"
},
{
"span": {
@ -561,7 +561,7 @@
}
]
},
"summary": "Replaced the 'e' identifier with the 'm' identifier"
"summary": "Replaced the 'e' identifier with the 'm' identifier in the 'm' if statement"
},
{
"span": {
@ -576,7 +576,7 @@
]
}
},
"summary": "Added the 'n' identifier"
"summary": "Added the 'n' identifier in the 'm' if statement"
},
{
"span": {
@ -591,7 +591,7 @@
]
}
},
"summary": "Deleted the 'f' identifier"
"summary": "Deleted the 'f' identifier in the 'm' if statement"
},
{
"span": {
@ -618,7 +618,7 @@
}
]
},
"summary": "Replaced the 'g' identifier with the 'o' identifier"
"summary": "Replaced the 'g' identifier with the 'o' identifier in the 'm' if statement"
}
]
},

View File

@ -121,7 +121,7 @@
}
]
},
"summary": "Replaced '2' with '1'"
"summary": "Replaced '2' with '1' in the '1' switch statement"
},
{
"span": {
@ -148,7 +148,7 @@
}
]
},
"summary": "Replaced '2' with '1'"
"summary": "Replaced '2' with '1' in the '2' case statement"
}
]
},
@ -201,7 +201,7 @@
}
]
},
"summary": "Replaced '1' with '2'"
"summary": "Replaced '1' with '2' in the '2' switch statement"
},
{
"span": {
@ -228,7 +228,7 @@
}
]
},
"summary": "Replaced '1' with '2'"
"summary": "Replaced '1' with '2' in the '2' case statement"
}
]
},

View File

@ -56,7 +56,7 @@
]
}
},
"summary": "Added the 'baz' elsif block"
"summary": "Added the 'baz' elsif block in the 'bar' if statement"
}
]
},
@ -181,7 +181,7 @@
]
}
},
"summary": "Deleted the 'baz' elsif block"
"summary": "Deleted the 'baz' elsif block in the 'bar' if statement"
}
]
},

View File

@ -153,7 +153,7 @@
}
]
},
"summary": "Replaced the 'x' identifier with the 'foo' identifier"
"summary": "Replaced the 'x' identifier with the 'foo' identifier in the 'foo' if statement"
},
{
"span": {
@ -168,7 +168,7 @@
]
}
},
"summary": "Added the 'bar' identifier"
"summary": "Added the 'bar' identifier in the 'foo' if statement"
},
{
"span": {
@ -183,7 +183,7 @@
]
}
},
"summary": "Added the 'quux' elsif block"
"summary": "Added the 'quux' elsif block in the 'foo' if statement"
},
{
"span": {
@ -198,7 +198,7 @@
]
}
},
"summary": "Added an else block"
"summary": "Added an else block in the 'foo' if statement"
},
{
"span": {
@ -274,7 +274,7 @@
}
]
},
"summary": "Replaced the 'foo' identifier with the 'x' identifier"
"summary": "Replaced the 'foo' identifier with the 'x' identifier in the 'x' if statement"
},
{
"span": {
@ -289,7 +289,7 @@
]
}
},
"summary": "Deleted the 'bar' identifier"
"summary": "Deleted the 'bar' identifier in the 'x' if statement"
},
{
"span": {
@ -304,7 +304,7 @@
]
}
},
"summary": "Deleted the 'quux' elsif block"
"summary": "Deleted the 'quux' elsif block in the 'x' if statement"
},
{
"span": {
@ -319,7 +319,7 @@
]
}
},
"summary": "Deleted an else block"
"summary": "Deleted an else block in the 'x' if statement"
},
{
"span": {