1
1
mirror of https://github.com/github/semantic.git synced 2025-01-03 04:51:57 +03:00

Handle else blocks in begin statements

This commit is contained in:
Timothy Clem 2016-11-02 11:10:01 -07:00
parent fc73be16d7
commit 2c1497ab99
7 changed files with 277 additions and 2 deletions

View File

@ -127,6 +127,7 @@ data Category
-- | A unless/else expression.
| Unless
| Begin
| Else
deriving (Eq, Generic, Ord, Show)
-- Instances
@ -188,6 +189,7 @@ instance Arbitrary Category where
, pure Until
, pure Unless
, pure Begin
, pure Else
, Other <$> arbitrary
]

View File

@ -141,6 +141,7 @@ determiner (LeafInfo "number" _ _) = ""
determiner (LeafInfo "integer" _ _) = ""
determiner (LeafInfo "boolean" _ _) = ""
determiner (LeafInfo "begin statement" _ _) = "a"
determiner (LeafInfo "else block" _ _) = "an"
determiner (LeafInfo "anonymous function" _ _) = "an"
determiner (BranchInfo bs _ _) = determiner (last bs)
determiner _ = "the"
@ -155,6 +156,7 @@ toLeafInfos leaf = pure . flip JSONSummary (sourceSpan leaf) $ case leaf of
(LeafInfo "boolean" termName _) -> squotes $ toDoc termName
(LeafInfo "anonymous function" termName _) -> toDoc termName <+> "function"
(LeafInfo cName@"begin statement" _ _) -> toDoc cName
(LeafInfo cName@"else block" _ _) -> toDoc cName
(LeafInfo cName@"string" termName _) -> toDoc termName <+> toDoc cName
(LeafInfo cName@"export statement" termName _) -> toDoc termName <+> toDoc cName
(LeafInfo cName@"import statement" termName _) -> toDoc termName <+> toDoc cName
@ -167,6 +169,7 @@ toTermName :: forall leaf fields. (HasCategory leaf, DefaultFields fields) => So
toTermName source term = case unwrap term of
S.AnonymousFunction params _ -> "anonymous" <> paramsToArgNames params
S.Begin children -> fromMaybe "branch" $ (toCategoryName . category) . extract <$> head children
S.Else children -> fromMaybe "branch" $ (toCategoryName . category) . extract <$> head children
S.Fixed children -> fromMaybe "branch" $ (toCategoryName . category) . extract <$> head children
S.Indexed children -> fromMaybe "branch" $ (toCategoryName . category) . extract <$> head children
Leaf leaf -> toCategoryName leaf
@ -360,6 +363,7 @@ instance HasCategory Category where
C.Until -> "until statement"
C.Unless -> "unless statement"
C.Begin -> "begin statement"
C.Else -> "else block"
instance HasField fields Category => HasCategory (SyntaxTerm leaf fields) where
toCategoryName = toCategoryName . category . extract

View File

@ -30,6 +30,7 @@ termConstructor source sourceSpan name range children
("assignment", [ identifier, value ]) -> S.Assignment identifier value
("assignment", _ ) -> S.Error children
("begin_statement", _) -> S.Begin children
("else_block", _) -> S.Else children
("case_statement", expr : rest) -> S.Switch expr rest
("case_statement", _ ) -> S.Error children
("class_declaration", [ identifier, superclass, definitions ]) -> S.Class identifier (Just superclass) (toList (unwrap definitions))
@ -109,7 +110,7 @@ categoryForRubyName = \case
"conditional_assignment" -> ConditionalAssignment
"conditional" -> Ternary
"element_reference" -> SubscriptAccess
"else_block" -> ExpressionStatements
"else_block" -> Else
"elsif_block" -> ExpressionStatements
"ensure_block" -> ExpressionStatements
"ERROR" -> Error

View File

@ -84,6 +84,7 @@ data Syntax a f
-- | An unless statement with an expression and maybe more expression clauses.
| Unless f [f]
| Begin [f]
| Else [f]
deriving (Eq, Foldable, Functor, Generic, Generic1, Mergeable, Ord, Show, Traversable)

View File

@ -0,0 +1,262 @@
[{
"testCaseDescription": "ruby-begin-else-insert-test",
"expectedResult": {
"changes": {
"begin-else.rb": [
{
"span": {
"insert": {
"start": [
1,
1
],
"end": [
3,
4
]
}
},
"summary": "Added a begin statement"
}
]
},
"errors": {}
},
"filePaths": [
"begin-else.rb"
],
"sha1": "92906a8fecac8451a93d006f532f99903d75f756",
"gitDir": "test/corpus/repos/ruby",
"sha2": "373027785f09b73e0f8701167c810ba3dabb9ac8"
}
,{
"testCaseDescription": "ruby-begin-else-replacement-insert-test",
"expectedResult": {
"changes": {
"begin-else.rb": [
{
"span": {
"insert": {
"start": [
1,
1
],
"end": [
5,
4
]
}
},
"summary": "Added a begin statement"
},
{
"span": {
"insert": {
"start": [
6,
1
],
"end": [
8,
4
]
}
},
"summary": "Added a begin statement"
}
]
},
"errors": {}
},
"filePaths": [
"begin-else.rb"
],
"sha1": "373027785f09b73e0f8701167c810ba3dabb9ac8",
"gitDir": "test/corpus/repos/ruby",
"sha2": "b95c073afcbd94fc7b26ba3b6120e845504a0906"
}
,{
"testCaseDescription": "ruby-begin-else-delete-insert-test",
"expectedResult": {
"changes": {
"begin-else.rb": [
{
"span": {
"delete": {
"start": [
3,
1
],
"end": [
5,
1
]
}
},
"summary": "Deleted an else block in a begin statement"
}
]
},
"errors": {}
},
"filePaths": [
"begin-else.rb"
],
"sha1": "b95c073afcbd94fc7b26ba3b6120e845504a0906",
"gitDir": "test/corpus/repos/ruby",
"sha2": "50bbf77e12e055a10a18777d5cd0d64cba07dd41"
}
,{
"testCaseDescription": "ruby-begin-else-replacement-test",
"expectedResult": {
"changes": {
"begin-else.rb": [
{
"span": {
"insert": {
"start": [
3,
1
],
"end": [
5,
1
]
}
},
"summary": "Added an else block in a begin statement"
}
]
},
"errors": {}
},
"filePaths": [
"begin-else.rb"
],
"sha1": "50bbf77e12e055a10a18777d5cd0d64cba07dd41",
"gitDir": "test/corpus/repos/ruby",
"sha2": "e2663b44ae83e64287df918ff156539aebb906de"
}
,{
"testCaseDescription": "ruby-begin-else-delete-replacement-test",
"expectedResult": {
"changes": {
"begin-else.rb": [
{
"span": {
"delete": {
"start": [
1,
1
],
"end": [
5,
4
]
}
},
"summary": "Deleted a begin statement"
},
{
"span": {
"delete": {
"start": [
6,
1
],
"end": [
8,
4
]
}
},
"summary": "Deleted a begin statement"
},
{
"span": {
"insert": {
"start": [
4,
1
],
"end": [
8,
4
]
}
},
"summary": "Added a begin statement"
}
]
},
"errors": {}
},
"filePaths": [
"begin-else.rb"
],
"sha1": "e2663b44ae83e64287df918ff156539aebb906de",
"gitDir": "test/corpus/repos/ruby",
"sha2": "531e205bd89e49f2b24effc365320f25fbde600e"
}
,{
"testCaseDescription": "ruby-begin-else-delete-test",
"expectedResult": {
"changes": {
"begin-else.rb": [
{
"span": {
"delete": {
"start": [
1,
1
],
"end": [
3,
4
]
}
},
"summary": "Deleted a begin statement"
}
]
},
"errors": {}
},
"filePaths": [
"begin-else.rb"
],
"sha1": "531e205bd89e49f2b24effc365320f25fbde600e",
"gitDir": "test/corpus/repos/ruby",
"sha2": "2f2adf17406f548e989464cf6cfe811d75b2dfcb"
}
,{
"testCaseDescription": "ruby-begin-else-delete-rest-test",
"expectedResult": {
"changes": {
"begin-else.rb": [
{
"span": {
"delete": {
"start": [
1,
1
],
"end": [
5,
4
]
}
},
"summary": "Deleted a begin statement"
}
]
},
"errors": {}
},
"filePaths": [
"begin-else.rb"
],
"sha1": "2f2adf17406f548e989464cf6cfe811d75b2dfcb",
"gitDir": "test/corpus/repos/ruby",
"sha2": "6592554e576fcb7805c20f575575bcc05f96d450"
}]

View File

@ -193,6 +193,11 @@
"syntax": "begin",
"insert": "begin\nend",
"replacement": "begin\n foo()\nend"
},
{
"syntax": "begin-else",
"insert": "begin\n foo()\nend",
"replacement": "begin\n foo()\nelse\n bar()\nend"
}
]
}

@ -1 +1 @@
Subproject commit 7b6af2586beb21bf33227b781a0b07d9d074c81a
Subproject commit 6592554e576fcb7805c20f575575bcc05f96d450