1
1
mirror of https://github.com/github/semantic.git synced 2024-11-29 02:44:36 +03:00

Better handling of case/when

This commit is contained in:
Timothy Clem 2016-11-02 13:42:06 -07:00
parent 295d03dd43
commit fbe7bc7c13
8 changed files with 342 additions and 69 deletions

View File

@ -131,6 +131,7 @@ data Category
| Elsif
| Ensure
| Rescue
| When
deriving (Eq, Generic, Ord, Show)
-- Instances
@ -196,6 +197,7 @@ instance Arbitrary Category where
, pure Elsif
, pure Ensure
, pure Rescue
, pure When
, Other <$> arbitrary
]

View File

@ -54,7 +54,6 @@ 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 }
@ -146,6 +145,7 @@ determiner (LeafInfo "else block" _ _) = "an"
determiner (LeafInfo "elsif block" _ _) = "an"
determiner (LeafInfo "ensure block" _ _) = "an"
determiner (LeafInfo "rescue block" _ _) = "an"
determiner (LeafInfo "when block" _ _) = "an"
determiner (LeafInfo "anonymous function" _ _) = "an"
determiner (BranchInfo bs _ _) = determiner (last bs)
determiner _ = "the"
@ -164,6 +164,7 @@ toLeafInfos leaf = pure . flip JSONSummary (sourceSpan leaf) $ case leaf of
(LeafInfo cName@"elsif block" _ _) -> toDoc cName
(LeafInfo cName@"ensure block" _ _) -> toDoc cName
(LeafInfo cName@"rescue block" _ _) -> toDoc cName
(LeafInfo cName@"when 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
@ -175,8 +176,10 @@ 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.BlockExpression children -> fromMaybe "branch" $ (toCategoryName . category) . extract <$> head children
S.ConditionalBlockExpression children -> fromMaybe "branch" $ (toCategoryName . category) . extract <$> head children
-- S.BlockExpression _ children -> fromMaybe "branch" $ (toCategoryName . category) . extract <$> head children
S.BlockExpression maybeExpr children -> case maybeExpr of
Just expr -> termNameFromSource expr
Nothing -> 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
@ -263,6 +266,8 @@ parentContexts contexts = hsep $ either identifiableDoc annotatableDoc <$> conte
C.Elsif -> "in an" <+> catName c
C.Ensure -> "in an" <+> catName c
C.Rescue -> "in an" <+> catName c
C.Case -> "in a" <+> catName c
C.When -> "in a" <+> catName c
_ -> "in the" <+> catName c
annotatableDoc (c, t) = "of the" <+> squotes (termName t) <+> catName c
catName = toDoc . toCategoryName
@ -378,6 +383,7 @@ instance HasCategory Category where
C.Elsif -> "elsif block"
C.Ensure -> "ensure block"
C.Rescue -> "rescue block"
C.When -> "when comparison"
instance HasField fields Category => HasCategory (SyntaxTerm leaf fields) where
toCategoryName = toCategoryName . category . extract

View File

@ -19,7 +19,7 @@ blocks :: [Text]
blocks = [ "begin_statement", "else_block", "ensure_block" ]
conditionalBlocks :: [Text]
conditionalBlocks = [ "rescue_block", "elsif_block" ]
conditionalBlocks = [ "elsif_block", "rescue_block", "case_statement", "when_block" ]
termConstructor
:: Source Char -- ^ The source that the term occurs within.
@ -35,8 +35,6 @@ termConstructor source sourceSpan name range children
("array", _) -> S.Array children
("assignment", [ identifier, value ]) -> S.Assignment identifier value
("assignment", _ ) -> S.Error 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))
("class_declaration", [ identifier, definitions ]) -> S.Class identifier Nothing (toList (unwrap definitions))
("class_declaration", _ ) -> S.Error children
@ -83,8 +81,10 @@ 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` blocks -> S.BlockExpression children
_ | name `elem` conditionalBlocks -> S.ConditionalBlockExpression 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]
@ -109,7 +109,7 @@ categoryForRubyName = \case
"boolean_and" -> BooleanOperator -- boolean and, e.g. &&.
"boolean_or" -> BooleanOperator -- boolean or, e.g. &&.
"boolean" -> Boolean
"case_statement" -> Switch
"case_statement" -> Case
"class_declaration" -> Class
"comment" -> Comment
"comparison" -> RelationalOperator -- comparison operator, e.g. <, <=, >=, >.
@ -150,7 +150,7 @@ categoryForRubyName = \case
"unless_statement" -> Unless
"until_modifier" -> Until
"until_statement" -> Until
"when_block" -> ExpressionStatements
"when_block" -> When
"while_modifier" -> While
"while_statement" -> While
"yield" -> Yield

View File

@ -83,10 +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]
-- | 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]
-- | A block expression might have a conditional expression and always has a list of expressions (e.g. begin, else, ensure in Ruby).
| BlockExpression (Maybe f) [f]
deriving (Eq, Foldable, Functor, Generic, Generic1, Mergeable, Ord, Show, Traversable)

View File

@ -0,0 +1,262 @@
[{
"testCaseDescription": "ruby-case-when-insert-test",
"expectedResult": {
"changes": {
"case-when.rb": [
{
"span": {
"insert": {
"start": [
1,
1
],
"end": [
2,
4
]
}
},
"summary": "Added the 'foo' case statement"
}
]
},
"errors": {}
},
"filePaths": [
"case-when.rb"
],
"sha1": "9bfa7c381a92f82cdb383553b99a258f0b8e646a",
"gitDir": "test/corpus/repos/ruby",
"sha2": "cd480951b46d71bb07610fcdfa32dc58258184ea"
}
,{
"testCaseDescription": "ruby-case-when-replacement-insert-test",
"expectedResult": {
"changes": {
"case-when.rb": [
{
"span": {
"insert": {
"start": [
1,
1
],
"end": [
3,
4
]
}
},
"summary": "Added the 'foo' case statement"
},
{
"span": {
"insert": {
"start": [
4,
1
],
"end": [
5,
4
]
}
},
"summary": "Added the 'foo' case statement"
}
]
},
"errors": {}
},
"filePaths": [
"case-when.rb"
],
"sha1": "cd480951b46d71bb07610fcdfa32dc58258184ea",
"gitDir": "test/corpus/repos/ruby",
"sha2": "d1c0b9ce0908a270c7a7f58f739d65800879e4e5"
}
,{
"testCaseDescription": "ruby-case-when-delete-insert-test",
"expectedResult": {
"changes": {
"case-when.rb": [
{
"span": {
"delete": {
"start": [
2,
1
],
"end": [
3,
1
]
}
},
"summary": "Deleted the 'bar' when comparison in a case statement"
}
]
},
"errors": {}
},
"filePaths": [
"case-when.rb"
],
"sha1": "d1c0b9ce0908a270c7a7f58f739d65800879e4e5",
"gitDir": "test/corpus/repos/ruby",
"sha2": "2121bc50c94562e1fbbbc10449f6634ba6afb15f"
}
,{
"testCaseDescription": "ruby-case-when-replacement-test",
"expectedResult": {
"changes": {
"case-when.rb": [
{
"span": {
"insert": {
"start": [
2,
1
],
"end": [
3,
1
]
}
},
"summary": "Added the 'bar' when comparison in a case statement"
}
]
},
"errors": {}
},
"filePaths": [
"case-when.rb"
],
"sha1": "2121bc50c94562e1fbbbc10449f6634ba6afb15f",
"gitDir": "test/corpus/repos/ruby",
"sha2": "7b3433fa88e6bbbbe26143cb7c4e4b3639d74df2"
}
,{
"testCaseDescription": "ruby-case-when-delete-replacement-test",
"expectedResult": {
"changes": {
"case-when.rb": [
{
"span": {
"delete": {
"start": [
1,
1
],
"end": [
3,
4
]
}
},
"summary": "Deleted the 'foo' case statement"
},
{
"span": {
"delete": {
"start": [
4,
1
],
"end": [
5,
4
]
}
},
"summary": "Deleted the 'foo' case statement"
},
{
"span": {
"insert": {
"start": [
3,
1
],
"end": [
5,
4
]
}
},
"summary": "Added the 'foo' case statement"
}
]
},
"errors": {}
},
"filePaths": [
"case-when.rb"
],
"sha1": "7b3433fa88e6bbbbe26143cb7c4e4b3639d74df2",
"gitDir": "test/corpus/repos/ruby",
"sha2": "840953000093da540af7204788baa198dcf9cbc7"
}
,{
"testCaseDescription": "ruby-case-when-delete-test",
"expectedResult": {
"changes": {
"case-when.rb": [
{
"span": {
"delete": {
"start": [
1,
1
],
"end": [
2,
4
]
}
},
"summary": "Deleted the 'foo' case statement"
}
]
},
"errors": {}
},
"filePaths": [
"case-when.rb"
],
"sha1": "840953000093da540af7204788baa198dcf9cbc7",
"gitDir": "test/corpus/repos/ruby",
"sha2": "41f337d1125500767bcb4b83c895f0a2b9f953fb"
}
,{
"testCaseDescription": "ruby-case-when-delete-rest-test",
"expectedResult": {
"changes": {
"case-when.rb": [
{
"span": {
"delete": {
"start": [
1,
1
],
"end": [
3,
4
]
}
},
"summary": "Deleted the 'foo' case statement"
}
]
},
"errors": {}
},
"filePaths": [
"case-when.rb"
],
"sha1": "41f337d1125500767bcb4b83c895f0a2b9f953fb",
"gitDir": "test/corpus/repos/ruby",
"sha2": "7699a00ae4f2db5a3570048f56b83158c513a7e1"
}]

View File

@ -1,8 +1,8 @@
[{
"testCaseDescription": "ruby-case-statement-insert-test",
"testCaseDescription": "ruby-when-insert-test",
"expectedResult": {
"changes": {
"case-statement.rb": [
"when.rb": [
{
"span": {
"insert": {
@ -16,24 +16,24 @@
]
}
},
"summary": "Added the 'foo' switch statement"
"summary": "Added the 'foo' case statement"
}
]
},
"errors": {}
},
"filePaths": [
"case-statement.rb"
"when.rb"
],
"sha1": "c6074d2900bc75792281ccca5e4cf1385ffd61f4",
"sha1": "7699a00ae4f2db5a3570048f56b83158c513a7e1",
"gitDir": "test/corpus/repos/ruby",
"sha2": "3288ef6a638a3a2757505d9b23d4c44abe33c29b"
"sha2": "c27a43cc441ebb84e2fecd86ad9569bda89b6f66"
}
,{
"testCaseDescription": "ruby-case-statement-replacement-insert-test",
"testCaseDescription": "ruby-when-replacement-insert-test",
"expectedResult": {
"changes": {
"case-statement.rb": [
"when.rb": [
{
"span": {
"insert": {
@ -47,7 +47,7 @@
]
}
},
"summary": "Added the 'foo' switch statement"
"summary": "Added the 'foo' case statement"
},
{
"span": {
@ -62,86 +62,86 @@
]
}
},
"summary": "Added the 'foo' switch statement"
"summary": "Added the 'foo' case statement"
}
]
},
"errors": {}
},
"filePaths": [
"case-statement.rb"
"when.rb"
],
"sha1": "3288ef6a638a3a2757505d9b23d4c44abe33c29b",
"sha1": "c27a43cc441ebb84e2fecd86ad9569bda89b6f66",
"gitDir": "test/corpus/repos/ruby",
"sha2": "a676c60cf58f985d18682c713b79b5463851f675"
"sha2": "7d9e6213787892e24789ffd2ff01cde77d7ae296"
}
,{
"testCaseDescription": "ruby-case-statement-delete-insert-test",
"testCaseDescription": "ruby-when-delete-insert-test",
"expectedResult": {
"changes": {
"case-statement.rb": [
"when.rb": [
{
"span": {
"delete": {
"start": [
3,
1
3
],
"end": [
3,
5
6
]
}
},
"summary": "Deleted an else block"
"summary": "Deleted the 'baz' identifier in a when comparison"
}
]
},
"errors": {}
},
"filePaths": [
"case-statement.rb"
"when.rb"
],
"sha1": "a676c60cf58f985d18682c713b79b5463851f675",
"sha1": "7d9e6213787892e24789ffd2ff01cde77d7ae296",
"gitDir": "test/corpus/repos/ruby",
"sha2": "abaca6882e5f28164dfc23d785202e29a2a1579c"
"sha2": "c41aa9aae0ac7962a4424529f5fc7979c5981ba0"
}
,{
"testCaseDescription": "ruby-case-statement-replacement-test",
"testCaseDescription": "ruby-when-replacement-test",
"expectedResult": {
"changes": {
"case-statement.rb": [
"when.rb": [
{
"span": {
"insert": {
"start": [
3,
1
3
],
"end": [
3,
5
6
]
}
},
"summary": "Added an else block"
"summary": "Added the 'baz' identifier in a when comparison"
}
]
},
"errors": {}
},
"filePaths": [
"case-statement.rb"
"when.rb"
],
"sha1": "abaca6882e5f28164dfc23d785202e29a2a1579c",
"sha1": "c41aa9aae0ac7962a4424529f5fc7979c5981ba0",
"gitDir": "test/corpus/repos/ruby",
"sha2": "dd16b53bc99dbfbdfd31b99fc6c35674df291fc3"
"sha2": "9e3e5119a2fd787bd1d50679f739811a95258a95"
}
,{
"testCaseDescription": "ruby-case-statement-delete-replacement-test",
"testCaseDescription": "ruby-when-delete-replacement-test",
"expectedResult": {
"changes": {
"case-statement.rb": [
"when.rb": [
{
"span": {
"delete": {
@ -155,7 +155,7 @@
]
}
},
"summary": "Deleted the 'foo' switch statement"
"summary": "Deleted the 'foo' case statement"
},
{
"span": {
@ -170,7 +170,7 @@
]
}
},
"summary": "Deleted the 'foo' switch statement"
"summary": "Deleted the 'foo' case statement"
},
{
"span": {
@ -185,24 +185,24 @@
]
}
},
"summary": "Added the 'foo' switch statement"
"summary": "Added the 'foo' case statement"
}
]
},
"errors": {}
},
"filePaths": [
"case-statement.rb"
"when.rb"
],
"sha1": "dd16b53bc99dbfbdfd31b99fc6c35674df291fc3",
"sha1": "9e3e5119a2fd787bd1d50679f739811a95258a95",
"gitDir": "test/corpus/repos/ruby",
"sha2": "f9c9a56769aecf7ff7917fdf5b2c6dc102001af4"
"sha2": "6b668407a0448f0bc0d1390c7a9d5a27d894c0fb"
}
,{
"testCaseDescription": "ruby-case-statement-delete-test",
"testCaseDescription": "ruby-when-delete-test",
"expectedResult": {
"changes": {
"case-statement.rb": [
"when.rb": [
{
"span": {
"delete": {
@ -216,24 +216,24 @@
]
}
},
"summary": "Deleted the 'foo' switch statement"
"summary": "Deleted the 'foo' case statement"
}
]
},
"errors": {}
},
"filePaths": [
"case-statement.rb"
"when.rb"
],
"sha1": "f9c9a56769aecf7ff7917fdf5b2c6dc102001af4",
"sha1": "6b668407a0448f0bc0d1390c7a9d5a27d894c0fb",
"gitDir": "test/corpus/repos/ruby",
"sha2": "bbfee7639ee0c9382aaa6d4fd99aa16004b0d382"
"sha2": "46deadb9ce696bf99314411f7cf56519dc9d9a03"
}
,{
"testCaseDescription": "ruby-case-statement-delete-rest-test",
"testCaseDescription": "ruby-when-delete-rest-test",
"expectedResult": {
"changes": {
"case-statement.rb": [
"when.rb": [
{
"span": {
"delete": {
@ -247,16 +247,16 @@
]
}
},
"summary": "Deleted the 'foo' switch statement"
"summary": "Deleted the 'foo' case statement"
}
]
},
"errors": {}
},
"filePaths": [
"case-statement.rb"
"when.rb"
],
"sha1": "bbfee7639ee0c9382aaa6d4fd99aa16004b0d382",
"sha1": "46deadb9ce696bf99314411f7cf56519dc9d9a03",
"gitDir": "test/corpus/repos/ruby",
"sha2": "f39262cb00c1a446c3fb7bcb435732e9ab0384a3"
"sha2": "ff122969324986ab2efa865790c05098cf5bf7eb"
}]

View File

@ -99,11 +99,6 @@
"insert": "print\nfoo.bar\nbar",
"replacement": "print(\"hello world\")\nfoo.bar()\nbar 2, 3\nbar(2, 3)"
},
{
"syntax": "case-statement",
"insert": "case foo\nwhen bar\nend",
"replacement": "case foo\nwhen bar\nelse\nend"
},
{
"syntax": "class",
"insert": "class Foo < Super\n def test; end\nend",
@ -233,6 +228,16 @@
"syntax": "rescue",
"insert": "begin\n foo\nrescue x\nend",
"replacement": "begin\n foo\nrescue x\n bar\nend"
},
{
"syntax": "case-when",
"insert": "case foo\nend",
"replacement": "case foo\nwhen bar\nend"
},
{
"syntax": "when",
"insert": "case foo\nwhen bar\nend",
"replacement": "case foo\nwhen bar\n baz\nend"
}
]
}

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