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

Handle rescue blocks

This commit is contained in:
Timothy Clem 2016-11-02 13:05:30 -07:00
parent 71f53b37fd
commit 295d03dd43
8 changed files with 553 additions and 5 deletions

View File

@ -130,6 +130,7 @@ data Category
| Else
| Elsif
| Ensure
| Rescue
deriving (Eq, Generic, Ord, Show)
-- Instances
@ -194,6 +195,7 @@ instance Arbitrary Category where
, pure Else
, pure Elsif
, pure Ensure
, pure Rescue
, Other <$> arbitrary
]

View File

@ -54,6 +54,7 @@ identifiable term = isIdentifiable (unwrap term) term
S.Import{} -> Identifiable
S.Export{} -> Identifiable
S.BlockExpression{} -> Identifiable
S.ConditionalBlockExpression{} -> Identifiable
_ -> Unidentifiable
data JSONSummary summary span = JSONSummary { summary :: summary, span :: span }
@ -144,6 +145,7 @@ determiner (LeafInfo "begin statement" _ _) = "a"
determiner (LeafInfo "else block" _ _) = "an"
determiner (LeafInfo "elsif block" _ _) = "an"
determiner (LeafInfo "ensure block" _ _) = "an"
determiner (LeafInfo "rescue block" _ _) = "an"
determiner (LeafInfo "anonymous function" _ _) = "an"
determiner (BranchInfo bs _ _) = determiner (last bs)
determiner _ = "the"
@ -161,6 +163,7 @@ toLeafInfos leaf = pure . flip JSONSummary (sourceSpan leaf) $ case leaf of
(LeafInfo cName@"else block" _ _) -> toDoc cName
(LeafInfo cName@"elsif block" _ _) -> toDoc cName
(LeafInfo cName@"ensure block" _ _) -> toDoc cName
(LeafInfo cName@"rescue 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
@ -173,6 +176,7 @@ toTermName :: forall leaf fields. (HasCategory leaf, DefaultFields fields) => So
toTermName source term = case unwrap term of
S.AnonymousFunction params _ -> "anonymous" <> paramsToArgNames params
S.BlockExpression children -> fromMaybe "branch" $ (toCategoryName . category) . extract <$> head children
S.ConditionalBlockExpression 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
@ -254,10 +258,11 @@ parentContexts contexts = hsep $ either identifiableDoc annotatableDoc <$> conte
where
identifiableDoc (c, t) = case c of
C.Assignment -> "in an" <+> catName c <+> "to" <+> termName t
C.Begin -> "in a" <+> catName c
C.Else -> "in an" <+> catName c
C.Elsif -> "in an" <+> catName c
C.Ensure -> "in an" <+> catName c
C.Begin -> "in a" <+> catName c
C.Rescue -> "in an" <+> catName c
_ -> "in the" <+> catName c
annotatableDoc (c, t) = "of the" <+> squotes (termName t) <+> catName c
catName = toDoc . toCategoryName
@ -372,6 +377,7 @@ instance HasCategory Category where
C.Else -> "else block"
C.Elsif -> "elsif block"
C.Ensure -> "ensure block"
C.Rescue -> "rescue block"
instance HasField fields Category => HasCategory (SyntaxTerm leaf fields) where
toCategoryName = toCategoryName . category . extract

View File

@ -10,13 +10,16 @@ import qualified Syntax as S
import Term
operators :: [Text]
operators = ["and", "boolean_and", "or", "boolean_or", "bitwise_or", "bitwise_and", "shift", "relational", "comparison"]
operators = [ "and", "boolean_and", "or", "boolean_or", "bitwise_or", "bitwise_and", "shift", "relational", "comparison" ]
functions :: [Text]
functions = [ "lambda_literal", "lambda_expression" ]
blocks :: [Text]
blocks = [ "begin_statement", "else_block", "elsif_block", "ensure_block" ]
blocks = [ "begin_statement", "else_block", "ensure_block" ]
conditionalBlocks :: [Text]
conditionalBlocks = [ "rescue_block", "elsif_block" ]
termConstructor
:: Source Char -- ^ The source that the term occurs within.
@ -81,6 +84,7 @@ termConstructor source sourceSpan name range children
("for_statement", lhs : expr : rest ) -> S.For [lhs, expr] rest
("for_statement", _ ) -> S.Error children
_ | name `elem` blocks -> S.BlockExpression children
_ | name `elem` conditionalBlocks -> S.ConditionalBlockExpression children
_ | name `elem` operators -> S.Operator children
_ | name `elem` functions -> case children of
[ body ] -> S.AnonymousFunction [] [body]
@ -136,7 +140,7 @@ categoryForRubyName = \case
"program" -> Program
"regex" -> Regex
"relational" -> RelationalOperator -- relational operator, e.g. ==, !=, ===, <=>, =~, !~.
"rescue_block" -> ExpressionStatements
"rescue_block" -> Rescue
"return_statement" -> Return
"shift" -> BitwiseOperator -- bitwise shift, e.g <<, >>.
"string" -> StringLiteral

View File

@ -85,6 +85,8 @@ data Syntax a f
| Unless f [f]
-- | A block expression has a list of expressions (e.g. begin, else, ensure in Ruby).
| BlockExpression [f]
-- | A conditional block expression has a conditional expression and list of expressions (e.g. rescue in Ruby).
| ConditionalBlockExpression [f]
deriving (Eq, Foldable, Functor, Generic, Generic1, Mergeable, Ord, Show, Traversable)

View File

@ -0,0 +1,262 @@
[{
"testCaseDescription": "ruby-begin-rescue-insert-test",
"expectedResult": {
"changes": {
"begin-rescue.rb": [
{
"span": {
"insert": {
"start": [
1,
1
],
"end": [
3,
4
]
}
},
"summary": "Added a begin statement"
}
]
},
"errors": {}
},
"filePaths": [
"begin-rescue.rb"
],
"sha1": "21b5c52a862dd540d98fdecc22d3c4313f5ba9bd",
"gitDir": "test/corpus/repos/ruby",
"sha2": "977983e66129257cd757452663890156bad99ad9"
}
,{
"testCaseDescription": "ruby-begin-rescue-replacement-insert-test",
"expectedResult": {
"changes": {
"begin-rescue.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-rescue.rb"
],
"sha1": "977983e66129257cd757452663890156bad99ad9",
"gitDir": "test/corpus/repos/ruby",
"sha2": "88d32139664840e5d311402fa030c6fb29a873b3"
}
,{
"testCaseDescription": "ruby-begin-rescue-delete-insert-test",
"expectedResult": {
"changes": {
"begin-rescue.rb": [
{
"span": {
"delete": {
"start": [
3,
1
],
"end": [
5,
1
]
}
},
"summary": "Deleted an rescue block in a begin statement"
}
]
},
"errors": {}
},
"filePaths": [
"begin-rescue.rb"
],
"sha1": "88d32139664840e5d311402fa030c6fb29a873b3",
"gitDir": "test/corpus/repos/ruby",
"sha2": "f683c3c2e72b4d28e401871407a2de5c2e0a89a4"
}
,{
"testCaseDescription": "ruby-begin-rescue-replacement-test",
"expectedResult": {
"changes": {
"begin-rescue.rb": [
{
"span": {
"insert": {
"start": [
3,
1
],
"end": [
5,
1
]
}
},
"summary": "Added an rescue block in a begin statement"
}
]
},
"errors": {}
},
"filePaths": [
"begin-rescue.rb"
],
"sha1": "f683c3c2e72b4d28e401871407a2de5c2e0a89a4",
"gitDir": "test/corpus/repos/ruby",
"sha2": "104bae02b1a739b38c6e020ee19d5892d95c58fe"
}
,{
"testCaseDescription": "ruby-begin-rescue-delete-replacement-test",
"expectedResult": {
"changes": {
"begin-rescue.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-rescue.rb"
],
"sha1": "104bae02b1a739b38c6e020ee19d5892d95c58fe",
"gitDir": "test/corpus/repos/ruby",
"sha2": "1bae10f12aee7a7c9c0a6eed7395998df2cf339e"
}
,{
"testCaseDescription": "ruby-begin-rescue-delete-test",
"expectedResult": {
"changes": {
"begin-rescue.rb": [
{
"span": {
"delete": {
"start": [
1,
1
],
"end": [
3,
4
]
}
},
"summary": "Deleted a begin statement"
}
]
},
"errors": {}
},
"filePaths": [
"begin-rescue.rb"
],
"sha1": "1bae10f12aee7a7c9c0a6eed7395998df2cf339e",
"gitDir": "test/corpus/repos/ruby",
"sha2": "6ea02eed44a5d2b4c229b009e3415f36930faacd"
}
,{
"testCaseDescription": "ruby-begin-rescue-delete-rest-test",
"expectedResult": {
"changes": {
"begin-rescue.rb": [
{
"span": {
"delete": {
"start": [
1,
1
],
"end": [
5,
4
]
}
},
"summary": "Deleted a begin statement"
}
]
},
"errors": {}
},
"filePaths": [
"begin-rescue.rb"
],
"sha1": "6ea02eed44a5d2b4c229b009e3415f36930faacd",
"gitDir": "test/corpus/repos/ruby",
"sha2": "f238cdb2ac430dc152785789b687fee81a58a41c"
}]

View File

@ -0,0 +1,262 @@
[{
"testCaseDescription": "ruby-rescue-insert-test",
"expectedResult": {
"changes": {
"rescue.rb": [
{
"span": {
"insert": {
"start": [
1,
1
],
"end": [
4,
4
]
}
},
"summary": "Added a begin statement"
}
]
},
"errors": {}
},
"filePaths": [
"rescue.rb"
],
"sha1": "50c82fa654f0034327299dd8199684f8cf85f66e",
"gitDir": "test/corpus/repos/ruby",
"sha2": "1779e3cc69cbd93ad7347d1155836f9ba2ff60e3"
}
,{
"testCaseDescription": "ruby-rescue-replacement-insert-test",
"expectedResult": {
"changes": {
"rescue.rb": [
{
"span": {
"insert": {
"start": [
1,
1
],
"end": [
5,
4
]
}
},
"summary": "Added a begin statement"
},
{
"span": {
"insert": {
"start": [
6,
1
],
"end": [
9,
4
]
}
},
"summary": "Added a begin statement"
}
]
},
"errors": {}
},
"filePaths": [
"rescue.rb"
],
"sha1": "1779e3cc69cbd93ad7347d1155836f9ba2ff60e3",
"gitDir": "test/corpus/repos/ruby",
"sha2": "243a4cce922381ecab2555e069a8aff9d40f1ebd"
}
,{
"testCaseDescription": "ruby-rescue-delete-insert-test",
"expectedResult": {
"changes": {
"rescue.rb": [
{
"span": {
"delete": {
"start": [
4,
3
],
"end": [
4,
6
]
}
},
"summary": "Deleted the 'bar' identifier in an rescue block"
}
]
},
"errors": {}
},
"filePaths": [
"rescue.rb"
],
"sha1": "243a4cce922381ecab2555e069a8aff9d40f1ebd",
"gitDir": "test/corpus/repos/ruby",
"sha2": "4ef80a5375d333d2c92af09cb47ec5a301739232"
}
,{
"testCaseDescription": "ruby-rescue-replacement-test",
"expectedResult": {
"changes": {
"rescue.rb": [
{
"span": {
"insert": {
"start": [
4,
3
],
"end": [
4,
6
]
}
},
"summary": "Added the 'bar' identifier in an rescue block"
}
]
},
"errors": {}
},
"filePaths": [
"rescue.rb"
],
"sha1": "4ef80a5375d333d2c92af09cb47ec5a301739232",
"gitDir": "test/corpus/repos/ruby",
"sha2": "a4b1cedb8d344102e20fcbca66020c9a890496a5"
}
,{
"testCaseDescription": "ruby-rescue-delete-replacement-test",
"expectedResult": {
"changes": {
"rescue.rb": [
{
"span": {
"delete": {
"start": [
1,
1
],
"end": [
5,
4
]
}
},
"summary": "Deleted a begin statement"
},
{
"span": {
"delete": {
"start": [
6,
1
],
"end": [
9,
4
]
}
},
"summary": "Deleted a begin statement"
},
{
"span": {
"insert": {
"start": [
5,
1
],
"end": [
9,
4
]
}
},
"summary": "Added a begin statement"
}
]
},
"errors": {}
},
"filePaths": [
"rescue.rb"
],
"sha1": "a4b1cedb8d344102e20fcbca66020c9a890496a5",
"gitDir": "test/corpus/repos/ruby",
"sha2": "603a677e1e53aba705d234be023fcb6eb16b8fa6"
}
,{
"testCaseDescription": "ruby-rescue-delete-test",
"expectedResult": {
"changes": {
"rescue.rb": [
{
"span": {
"delete": {
"start": [
1,
1
],
"end": [
4,
4
]
}
},
"summary": "Deleted a begin statement"
}
]
},
"errors": {}
},
"filePaths": [
"rescue.rb"
],
"sha1": "603a677e1e53aba705d234be023fcb6eb16b8fa6",
"gitDir": "test/corpus/repos/ruby",
"sha2": "e908e1528b1a63126473b0af87da9a9e6e797593"
}
,{
"testCaseDescription": "ruby-rescue-delete-rest-test",
"expectedResult": {
"changes": {
"rescue.rb": [
{
"span": {
"delete": {
"start": [
1,
1
],
"end": [
5,
4
]
}
},
"summary": "Deleted a begin statement"
}
]
},
"errors": {}
},
"filePaths": [
"rescue.rb"
],
"sha1": "e908e1528b1a63126473b0af87da9a9e6e797593",
"gitDir": "test/corpus/repos/ruby",
"sha2": "7475561115b4241d0ed7ea33b7c9c2c5da3435f5"
}]

View File

@ -223,6 +223,16 @@
"syntax": "ensure",
"insert": "begin\n foo\nensure\nend",
"replacement": "begin\n foo\nensure\n bar\nend"
},
{
"syntax": "begin-rescue",
"insert": "begin\n foo\nend",
"replacement": "begin\n foo\nrescue x\n bar\nend"
},
{
"syntax": "rescue",
"insert": "begin\n foo\nrescue x\nend",
"replacement": "begin\n foo\nrescue x\n bar\nend"
}
]
}

@ -1 +1 @@
Subproject commit e8bcd0735789378b5732b7b1185c713f8cd29765
Subproject commit 7475561115b4241d0ed7ea33b7c9c2c5da3435f5