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

Handle ensure blocks and refactor to BlockExpression syntax

This commit is contained in:
Timothy Clem 2016-11-02 12:55:48 -07:00
parent 6eebd35a8f
commit 71f53b37fd
8 changed files with 550 additions and 10 deletions

View File

@ -129,6 +129,7 @@ data Category
| Begin
| Else
| Elsif
| Ensure
deriving (Eq, Generic, Ord, Show)
-- Instances
@ -192,6 +193,7 @@ instance Arbitrary Category where
, pure Begin
, pure Else
, pure Elsif
, pure Ensure
, Other <$> arbitrary
]

View File

@ -53,8 +53,7 @@ identifiable term = isIdentifiable (unwrap term) term
S.DoWhile{} -> Identifiable
S.Import{} -> Identifiable
S.Export{} -> Identifiable
S.Begin{} -> Identifiable
S.Else{} -> Identifiable
S.BlockExpression{} -> Identifiable
_ -> Unidentifiable
data JSONSummary summary span = JSONSummary { summary :: summary, span :: span }
@ -144,6 +143,7 @@ determiner (LeafInfo "boolean" _ _) = ""
determiner (LeafInfo "begin statement" _ _) = "a"
determiner (LeafInfo "else block" _ _) = "an"
determiner (LeafInfo "elsif block" _ _) = "an"
determiner (LeafInfo "ensure block" _ _) = "an"
determiner (LeafInfo "anonymous function" _ _) = "an"
determiner (BranchInfo bs _ _) = determiner (last bs)
determiner _ = "the"
@ -160,6 +160,7 @@ toLeafInfos leaf = pure . flip JSONSummary (sourceSpan leaf) $ case leaf of
(LeafInfo cName@"begin statement" _ _) -> toDoc cName
(LeafInfo cName@"else block" _ _) -> toDoc cName
(LeafInfo cName@"elsif block" _ _) -> toDoc cName
(LeafInfo cName@"ensure 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
@ -171,8 +172,7 @@ toLeafInfos leaf = pure . flip JSONSummary (sourceSpan leaf) $ case leaf of
toTermName :: forall leaf fields. (HasCategory leaf, DefaultFields fields) => Source Char -> SyntaxTerm leaf fields -> Text
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.BlockExpression 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
@ -256,6 +256,7 @@ parentContexts contexts = hsep $ either identifiableDoc annotatableDoc <$> conte
C.Assignment -> "in an" <+> catName c <+> "to" <+> termName t
C.Else -> "in an" <+> catName c
C.Elsif -> "in an" <+> catName c
C.Ensure -> "in an" <+> catName c
C.Begin -> "in a" <+> catName c
_ -> "in the" <+> catName c
annotatableDoc (c, t) = "of the" <+> squotes (termName t) <+> catName c
@ -370,6 +371,7 @@ instance HasCategory Category where
C.Begin -> "begin statement"
C.Else -> "else block"
C.Elsif -> "elsif block"
C.Ensure -> "ensure block"
instance HasField fields Category => HasCategory (SyntaxTerm leaf fields) where
toCategoryName = toCategoryName . category . extract

View File

@ -15,6 +15,9 @@ operators = ["and", "boolean_and", "or", "boolean_or", "bitwise_or", "bitwise_an
functions :: [Text]
functions = [ "lambda_literal", "lambda_expression" ]
blocks :: [Text]
blocks = [ "begin_statement", "else_block", "elsif_block", "ensure_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).
@ -29,7 +32,6 @@ termConstructor source sourceSpan name range children
("array", _) -> S.Array children
("assignment", [ identifier, value ]) -> S.Assignment identifier value
("assignment", _ ) -> S.Error children
("begin_statement", _) -> S.Begin 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))
@ -78,7 +80,7 @@ termConstructor source sourceSpan name range children
("yield", _) -> S.Yield (listToMaybe children)
("for_statement", lhs : expr : rest ) -> S.For [lhs, expr] rest
("for_statement", _ ) -> S.Error children
_ | name `elem` ["else_block", "elsif_block"] -> S.Else children
_ | name `elem` blocks -> S.BlockExpression children
_ | name `elem` operators -> S.Operator children
_ | name `elem` functions -> case children of
[ body ] -> S.AnonymousFunction [] [body]
@ -112,7 +114,7 @@ categoryForRubyName = \case
"element_reference" -> SubscriptAccess
"else_block" -> Else
"elsif_block" -> Elsif
"ensure_block" -> ExpressionStatements
"ensure_block" -> Ensure
"ERROR" -> Error
"float" -> NumberLiteral
"for_statement" -> For

View File

@ -83,8 +83,8 @@ data Syntax a f
| Until { untilExpr :: f, untilBody :: [f] }
-- | An unless statement with an expression and maybe more expression clauses.
| Unless f [f]
| Begin [f]
| Else [f]
-- | A block expression has a list of expressions (e.g. begin, else, ensure in Ruby).
| BlockExpression [f]
deriving (Eq, Foldable, Functor, Generic, Generic1, Mergeable, Ord, Show, Traversable)

View File

@ -0,0 +1,262 @@
[{
"testCaseDescription": "ruby-begin-ensure-insert-test",
"expectedResult": {
"changes": {
"begin-ensure.rb": [
{
"span": {
"insert": {
"start": [
1,
1
],
"end": [
3,
4
]
}
},
"summary": "Added a begin statement"
}
]
},
"errors": {}
},
"filePaths": [
"begin-ensure.rb"
],
"sha1": "8414a453dd8cf3839df0096b7323cd703717c9b8",
"gitDir": "test/corpus/repos/ruby",
"sha2": "05babf64ab5aae92b53fe707952ca5c7442584d5"
}
,{
"testCaseDescription": "ruby-begin-ensure-replacement-insert-test",
"expectedResult": {
"changes": {
"begin-ensure.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-ensure.rb"
],
"sha1": "05babf64ab5aae92b53fe707952ca5c7442584d5",
"gitDir": "test/corpus/repos/ruby",
"sha2": "f646dd8d943fb3ccf7889cd1ba24ea132ebf00bc"
}
,{
"testCaseDescription": "ruby-begin-ensure-delete-insert-test",
"expectedResult": {
"changes": {
"begin-ensure.rb": [
{
"span": {
"delete": {
"start": [
3,
1
],
"end": [
5,
1
]
}
},
"summary": "Deleted an ensure block in a begin statement"
}
]
},
"errors": {}
},
"filePaths": [
"begin-ensure.rb"
],
"sha1": "f646dd8d943fb3ccf7889cd1ba24ea132ebf00bc",
"gitDir": "test/corpus/repos/ruby",
"sha2": "635bbe4b9be7b84b5a7dfcdcc8c670d02ffc0e8c"
}
,{
"testCaseDescription": "ruby-begin-ensure-replacement-test",
"expectedResult": {
"changes": {
"begin-ensure.rb": [
{
"span": {
"insert": {
"start": [
3,
1
],
"end": [
5,
1
]
}
},
"summary": "Added an ensure block in a begin statement"
}
]
},
"errors": {}
},
"filePaths": [
"begin-ensure.rb"
],
"sha1": "635bbe4b9be7b84b5a7dfcdcc8c670d02ffc0e8c",
"gitDir": "test/corpus/repos/ruby",
"sha2": "a6e689dc2a36e75affb21d63d35a151b145e46d7"
}
,{
"testCaseDescription": "ruby-begin-ensure-delete-replacement-test",
"expectedResult": {
"changes": {
"begin-ensure.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-ensure.rb"
],
"sha1": "a6e689dc2a36e75affb21d63d35a151b145e46d7",
"gitDir": "test/corpus/repos/ruby",
"sha2": "23968b67ac79bab8b065662ed871e8c6f07b0647"
}
,{
"testCaseDescription": "ruby-begin-ensure-delete-test",
"expectedResult": {
"changes": {
"begin-ensure.rb": [
{
"span": {
"delete": {
"start": [
1,
1
],
"end": [
3,
4
]
}
},
"summary": "Deleted a begin statement"
}
]
},
"errors": {}
},
"filePaths": [
"begin-ensure.rb"
],
"sha1": "23968b67ac79bab8b065662ed871e8c6f07b0647",
"gitDir": "test/corpus/repos/ruby",
"sha2": "8a286e6c5164f797379fee63173bb21db9121eea"
}
,{
"testCaseDescription": "ruby-begin-ensure-delete-rest-test",
"expectedResult": {
"changes": {
"begin-ensure.rb": [
{
"span": {
"delete": {
"start": [
1,
1
],
"end": [
5,
4
]
}
},
"summary": "Deleted a begin statement"
}
]
},
"errors": {}
},
"filePaths": [
"begin-ensure.rb"
],
"sha1": "8a286e6c5164f797379fee63173bb21db9121eea",
"gitDir": "test/corpus/repos/ruby",
"sha2": "c5cbf37fdc3cd31185850c44dc0db64f5bdd705c"
}]

View File

@ -0,0 +1,262 @@
[{
"testCaseDescription": "ruby-ensure-insert-test",
"expectedResult": {
"changes": {
"ensure.rb": [
{
"span": {
"insert": {
"start": [
1,
1
],
"end": [
4,
4
]
}
},
"summary": "Added a begin statement"
}
]
},
"errors": {}
},
"filePaths": [
"ensure.rb"
],
"sha1": "eb3e192264281bd85982434a84d3936e776a4cb2",
"gitDir": "test/corpus/repos/ruby",
"sha2": "1d0f58a4006ba171d29cbdf9fc833edcd9ee3668"
}
,{
"testCaseDescription": "ruby-ensure-replacement-insert-test",
"expectedResult": {
"changes": {
"ensure.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": [
"ensure.rb"
],
"sha1": "1d0f58a4006ba171d29cbdf9fc833edcd9ee3668",
"gitDir": "test/corpus/repos/ruby",
"sha2": "869b8019aede969ec6757b1ed89babcab2210ef9"
}
,{
"testCaseDescription": "ruby-ensure-delete-insert-test",
"expectedResult": {
"changes": {
"ensure.rb": [
{
"span": {
"delete": {
"start": [
4,
3
],
"end": [
4,
6
]
}
},
"summary": "Deleted the 'bar' identifier in an ensure block"
}
]
},
"errors": {}
},
"filePaths": [
"ensure.rb"
],
"sha1": "869b8019aede969ec6757b1ed89babcab2210ef9",
"gitDir": "test/corpus/repos/ruby",
"sha2": "a820cfd99b963215ee8fc7dbf2d3db2159929d33"
}
,{
"testCaseDescription": "ruby-ensure-replacement-test",
"expectedResult": {
"changes": {
"ensure.rb": [
{
"span": {
"insert": {
"start": [
4,
3
],
"end": [
4,
6
]
}
},
"summary": "Added the 'bar' identifier in an ensure block"
}
]
},
"errors": {}
},
"filePaths": [
"ensure.rb"
],
"sha1": "a820cfd99b963215ee8fc7dbf2d3db2159929d33",
"gitDir": "test/corpus/repos/ruby",
"sha2": "d992c120c1b21200356cc55868dadeec4eee7e8b"
}
,{
"testCaseDescription": "ruby-ensure-delete-replacement-test",
"expectedResult": {
"changes": {
"ensure.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": [
"ensure.rb"
],
"sha1": "d992c120c1b21200356cc55868dadeec4eee7e8b",
"gitDir": "test/corpus/repos/ruby",
"sha2": "d1a7e2778ce1960232822a312824df731c34e54d"
}
,{
"testCaseDescription": "ruby-ensure-delete-test",
"expectedResult": {
"changes": {
"ensure.rb": [
{
"span": {
"delete": {
"start": [
1,
1
],
"end": [
4,
4
]
}
},
"summary": "Deleted a begin statement"
}
]
},
"errors": {}
},
"filePaths": [
"ensure.rb"
],
"sha1": "d1a7e2778ce1960232822a312824df731c34e54d",
"gitDir": "test/corpus/repos/ruby",
"sha2": "e234e9fb239ccdb8f70e2d815c077713a8f9e798"
}
,{
"testCaseDescription": "ruby-ensure-delete-rest-test",
"expectedResult": {
"changes": {
"ensure.rb": [
{
"span": {
"delete": {
"start": [
1,
1
],
"end": [
5,
4
]
}
},
"summary": "Deleted a begin statement"
}
]
},
"errors": {}
},
"filePaths": [
"ensure.rb"
],
"sha1": "e234e9fb239ccdb8f70e2d815c077713a8f9e798",
"gitDir": "test/corpus/repos/ruby",
"sha2": "e8bcd0735789378b5732b7b1185c713f8cd29765"
}]

View File

@ -213,6 +213,16 @@
"syntax": "elsif",
"insert": "if bar\n foo()\nelsif baz\nend",
"replacement": "if bar\n foo()\nelsif baz\n qoz()\nend"
},
{
"syntax": "begin-ensure",
"insert": "begin\n foo\nend",
"replacement": "begin\n foo\nensure\n bar\nend"
},
{
"syntax": "ensure",
"insert": "begin\n foo\nensure\nend",
"replacement": "begin\n foo\nensure\n bar\nend"
}
]
}

@ -1 +1 @@
Subproject commit d37ca473e0bd6bb2fad505c582dcdf0442bbbdb7
Subproject commit e8bcd0735789378b5732b7b1185c713f8cd29765