From 2c1497ab99f76a97929709cd184efece8d77cf37 Mon Sep 17 00:00:00 2001 From: Timothy Clem Date: Wed, 2 Nov 2016 11:10:01 -0700 Subject: [PATCH] Handle else blocks in begin statements --- src/Category.hs | 2 + src/DiffSummary.hs | 4 + src/Language/Ruby.hs | 3 +- src/Syntax.hs | 1 + .../diff-summaries/ruby/begin-else.json | 262 ++++++++++++++++++ test/corpus/generated/ruby.json | 5 + test/corpus/repos/ruby | 2 +- 7 files changed, 277 insertions(+), 2 deletions(-) create mode 100644 test/corpus/diff-summaries/ruby/begin-else.json diff --git a/src/Category.hs b/src/Category.hs index 6ab684ab3..693e01817 100644 --- a/src/Category.hs +++ b/src/Category.hs @@ -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 ] diff --git a/src/DiffSummary.hs b/src/DiffSummary.hs index 36813b61e..7f2cc40f2 100644 --- a/src/DiffSummary.hs +++ b/src/DiffSummary.hs @@ -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 diff --git a/src/Language/Ruby.hs b/src/Language/Ruby.hs index 8dafe7efa..bcaafd900 100644 --- a/src/Language/Ruby.hs +++ b/src/Language/Ruby.hs @@ -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 diff --git a/src/Syntax.hs b/src/Syntax.hs index 4e4c74e01..bd54c25f7 100644 --- a/src/Syntax.hs +++ b/src/Syntax.hs @@ -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) diff --git a/test/corpus/diff-summaries/ruby/begin-else.json b/test/corpus/diff-summaries/ruby/begin-else.json new file mode 100644 index 000000000..f12410443 --- /dev/null +++ b/test/corpus/diff-summaries/ruby/begin-else.json @@ -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" +}] diff --git a/test/corpus/generated/ruby.json b/test/corpus/generated/ruby.json index 542053e93..3bafa2bbc 100644 --- a/test/corpus/generated/ruby.json +++ b/test/corpus/generated/ruby.json @@ -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" } ] } diff --git a/test/corpus/repos/ruby b/test/corpus/repos/ruby index 7b6af2586..6592554e5 160000 --- a/test/corpus/repos/ruby +++ b/test/corpus/repos/ruby @@ -1 +1 @@ -Subproject commit 7b6af2586beb21bf33227b781a0b07d9d074c81a +Subproject commit 6592554e576fcb7805c20f575575bcc05f96d450