1
1
mirror of https://github.com/github/semantic.git synced 2024-11-28 10:15:55 +03:00

Parse rescue modifiers

This commit is contained in:
Timothy Clem 2016-11-03 09:23:35 -07:00
parent 18c7914216
commit 4daf39476e
12 changed files with 612 additions and 2 deletions

View File

@ -131,6 +131,7 @@ data Category
| Elsif
| Ensure
| Rescue
| RescueModifier
| When
| LastException
deriving (Eq, Generic, Ord, Show)
@ -198,6 +199,7 @@ instance Arbitrary Category where
, pure Elsif
, pure Ensure
, pure Rescue
, pure RescueModifier
, pure When
, pure LastException
, Other <$> arbitrary

View File

@ -55,6 +55,7 @@ identifiable term = isIdentifiable (unwrap term) term
S.Export{} -> Identifiable
S.BlockExpression{} -> Identifiable
S.Rescue{} -> Identifiable
S.RescueModifier{} -> Identifiable
_ -> Unidentifiable
data JSONSummary summary span = JSONSummary { summary :: summary, span :: span }
@ -162,6 +163,7 @@ toLeafInfos leaf = pure . flip JSONSummary (sourceSpan leaf) $ case leaf of
(LeafInfo cName@"else block" _ _) -> toDoc cName
(LeafInfo cName@"ensure block" _ _) -> toDoc cName
(LeafInfo cName@"when block" _ _) -> toDoc cName
(LeafInfo "rescue modifier" termName _) -> squotes ("rescue" <+> toDoc termName) <+> "modifier"
(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
@ -245,6 +247,7 @@ toTermName source term = case unwrap term of
(Just args, Just ex) -> toTermName' args <> " => " <> toTermName' ex
(Just args, Nothing) -> toTermName' args
_ -> ""
S.RescueModifier _ rhs -> termNameFromSource rhs
S.LastException expr -> termNameFromSource expr
where toTermName' = toTermName source
termNameFromChildren term children = termNameFromRange (unionRangesFrom (range term) (range <$> children))
@ -269,6 +272,7 @@ parentContexts contexts = hsep $ either identifiableDoc annotatableDoc <$> conte
C.Rescue -> case t of
"" -> "in a" <+> catName c
_ -> "in the" <+> squotes (termName t) <+> catName c
C.RescueModifier -> "in the" <+> squotes ("rescue" <+> termName t) <+> "modifier"
C.Case -> "in the" <+> squotes (termName t) <+> catName c
C.When -> "in a" <+> catName c
_ -> "in the" <+> termName t <+> catName c
@ -386,6 +390,7 @@ instance HasCategory Category where
C.Elsif -> "elsif block"
C.Ensure -> "ensure block"
C.Rescue -> "rescue block"
C.RescueModifier -> "rescue modifier"
C.When -> "when comparison"
C.LastException -> "last exception"

View File

@ -73,6 +73,8 @@ termConstructor source sourceSpan name range children
(args@(_ :< S.Args _) : e@(_ :< S.LastException _) : rest) -> S.Rescue (Just (cofree args)) (Just (cofree e)) (cofree <$> rest)
(args@(_ :< S.Args _) : rest) -> S.Rescue (Just (cofree args)) Nothing (cofree <$> rest)
_ -> S.Rescue Nothing Nothing children
("rescue_modifier", [lhs, rhs] ) -> S.RescueModifier lhs rhs
("rescue_modifier", _ ) -> S.Error children
("return_statement", _ ) -> S.Return (listToMaybe children)
("unless_modifier", [ lhs, condition ]) -> S.Unless condition [lhs]
("unless_modifier", _ ) -> S.Error children
@ -148,6 +150,7 @@ categoryForRubyName = \case
"regex" -> Regex
"relational" -> RelationalOperator -- relational operator, e.g. ==, !=, ===, <=>, =~, !~.
"rescue_block" -> Rescue
"rescue_modifier" -> RescueModifier
"return_statement" -> Return
"shift" -> BitwiseOperator -- bitwise shift, e.g <<, >>.
"string" -> StringLiteral

View File

@ -128,5 +128,6 @@ syntaxToTermField syntax = case syntax of
S.Unless expr clauses -> [ "unless" .= expr ] <> childrenFields clauses
S.BlockExpression condition expressions -> [ "condition" .= condition ] <> childrenFields expressions
S.Rescue args ex expressions -> [ "args" .= args ] <> [ "ex" .= ex ] <> childrenFields expressions
S.RescueModifier lhs rhs -> [ "lhs" .= lhs ] <> [ "rhs" .= rhs ]
S.LastException e -> [ "ex" .= e ]
where childrenFields c = [ "children" .= c ]

View File

@ -94,6 +94,7 @@ styleName category = "category-" <> case category of
C.Elsif -> "elsif_block"
C.Ensure -> "ensure_block"
C.Rescue -> "rescue_block"
C.RescueModifier -> "rescue_modifier"
C.When -> "when_block"
C.LastException -> "last_exception"

View File

@ -87,6 +87,8 @@ data Syntax a f
| BlockExpression (Maybe f) [f]
-- | A rescue block: maybe Args to rescue, maybe a local var for the last exception, and a list of expressions.
| Rescue (Maybe f) (Maybe f) [f]
-- | A rescue modifier has a left and right expression (e.g. foo rescue nil).
| RescueModifier f f
| LastException f
deriving (Eq, Foldable, Functor, Generic, Generic1, Mergeable, Ord, Show, Traversable)

View File

@ -0,0 +1,286 @@
[{
"testCaseDescription": "ruby-rescue-modifier-insert-test",
"expectedResult": {
"changes": {
"rescue-modifier.rb": [
{
"span": {
"insert": {
"start": [
1,
1
],
"end": [
1,
15
]
}
},
"summary": "Added the 'rescue nil' modifier"
}
]
},
"errors": {}
},
"filePaths": [
"rescue-modifier.rb"
],
"sha1": "59a9759ed2f863210fba0ebe543cfdb7a02c7342",
"gitDir": "test/corpus/repos/ruby",
"sha2": "badca3f03550951b38703f5995f62ee8d0a1da48"
}
,{
"testCaseDescription": "ruby-rescue-modifier-replacement-insert-test",
"expectedResult": {
"changes": {
"rescue-modifier.rb": [
{
"span": {
"insert": {
"start": [
1,
1
],
"end": [
1,
17
]
}
},
"summary": "Added the 'rescue false' modifier"
},
{
"span": {
"insert": {
"start": [
2,
1
],
"end": [
2,
15
]
}
},
"summary": "Added the 'rescue nil' modifier"
}
]
},
"errors": {}
},
"filePaths": [
"rescue-modifier.rb"
],
"sha1": "badca3f03550951b38703f5995f62ee8d0a1da48",
"gitDir": "test/corpus/repos/ruby",
"sha2": "7fb9b2faedcaece1faacc6ac6d26fd2d721dd98f"
}
,{
"testCaseDescription": "ruby-rescue-modifier-delete-insert-test",
"expectedResult": {
"changes": {
"rescue-modifier.rb": [
{
"span": {
"replace": [
{
"start": [
1,
1
],
"end": [
1,
17
]
},
{
"start": [
1,
1
],
"end": [
1,
15
]
}
]
},
"summary": "Replaced the 'rescue false' modifier with the 'rescue nil' modifier"
}
]
},
"errors": {}
},
"filePaths": [
"rescue-modifier.rb"
],
"sha1": "7fb9b2faedcaece1faacc6ac6d26fd2d721dd98f",
"gitDir": "test/corpus/repos/ruby",
"sha2": "9cd8a1a8c08d02c996e065ff343a7a14842e4c1d"
}
,{
"testCaseDescription": "ruby-rescue-modifier-replacement-test",
"expectedResult": {
"changes": {
"rescue-modifier.rb": [
{
"span": {
"replace": [
{
"start": [
1,
1
],
"end": [
1,
15
]
},
{
"start": [
1,
1
],
"end": [
1,
17
]
}
]
},
"summary": "Replaced the 'rescue nil' modifier with the 'rescue false' modifier"
}
]
},
"errors": {}
},
"filePaths": [
"rescue-modifier.rb"
],
"sha1": "9cd8a1a8c08d02c996e065ff343a7a14842e4c1d",
"gitDir": "test/corpus/repos/ruby",
"sha2": "53210ef19442457d281b7345e2adf35b65c6b0b0"
}
,{
"testCaseDescription": "ruby-rescue-modifier-delete-replacement-test",
"expectedResult": {
"changes": {
"rescue-modifier.rb": [
{
"span": {
"delete": {
"start": [
1,
1
],
"end": [
1,
17
]
}
},
"summary": "Deleted the 'rescue false' modifier"
},
{
"span": {
"delete": {
"start": [
2,
1
],
"end": [
2,
15
]
}
},
"summary": "Deleted the 'rescue nil' modifier"
},
{
"span": {
"insert": {
"start": [
2,
1
],
"end": [
2,
17
]
}
},
"summary": "Added the 'rescue false' modifier"
}
]
},
"errors": {}
},
"filePaths": [
"rescue-modifier.rb"
],
"sha1": "53210ef19442457d281b7345e2adf35b65c6b0b0",
"gitDir": "test/corpus/repos/ruby",
"sha2": "91395bedfccd9c9d793f1c32f423c40f689bef8b"
}
,{
"testCaseDescription": "ruby-rescue-modifier-delete-test",
"expectedResult": {
"changes": {
"rescue-modifier.rb": [
{
"span": {
"delete": {
"start": [
1,
1
],
"end": [
1,
15
]
}
},
"summary": "Deleted the 'rescue nil' modifier"
}
]
},
"errors": {}
},
"filePaths": [
"rescue-modifier.rb"
],
"sha1": "91395bedfccd9c9d793f1c32f423c40f689bef8b",
"gitDir": "test/corpus/repos/ruby",
"sha2": "6fbf1592a69bf9faa4a2efe3a40e5c5653448b2e"
}
,{
"testCaseDescription": "ruby-rescue-modifier-delete-rest-test",
"expectedResult": {
"changes": {
"rescue-modifier.rb": [
{
"span": {
"delete": {
"start": [
1,
1
],
"end": [
1,
17
]
}
},
"summary": "Deleted the 'rescue false' modifier"
}
]
},
"errors": {}
},
"filePaths": [
"rescue-modifier.rb"
],
"sha1": "6fbf1592a69bf9faa4a2efe3a40e5c5653448b2e",
"gitDir": "test/corpus/repos/ruby",
"sha2": "546403a4d56edb42ee8fecb780f372200d5e906e"
}]

View File

@ -0,0 +1,286 @@
[{
"testCaseDescription": "ruby-rescue-modifier2-insert-test",
"expectedResult": {
"changes": {
"rescue-modifier2.rb": [
{
"span": {
"insert": {
"start": [
1,
1
],
"end": [
1,
15
]
}
},
"summary": "Added the 'rescue nil' modifier"
}
]
},
"errors": {}
},
"filePaths": [
"rescue-modifier2.rb"
],
"sha1": "7e0ebe98de8bf5ee5e7ce55fcb2094c482cb7b7f",
"gitDir": "test/corpus/repos/ruby",
"sha2": "cd16d47d649a4293498963fa39ab45b48ec70c23"
}
,{
"testCaseDescription": "ruby-rescue-modifier2-replacement-insert-test",
"expectedResult": {
"changes": {
"rescue-modifier2.rb": [
{
"span": {
"insert": {
"start": [
1,
1
],
"end": [
1,
15
]
}
},
"summary": "Added the 'rescue nil' modifier"
},
{
"span": {
"insert": {
"start": [
2,
1
],
"end": [
2,
15
]
}
},
"summary": "Added the 'rescue nil' modifier"
}
]
},
"errors": {}
},
"filePaths": [
"rescue-modifier2.rb"
],
"sha1": "cd16d47d649a4293498963fa39ab45b48ec70c23",
"gitDir": "test/corpus/repos/ruby",
"sha2": "00696e572d69a88e4fcb2e69399458f9bdd3f53a"
}
,{
"testCaseDescription": "ruby-rescue-modifier2-delete-insert-test",
"expectedResult": {
"changes": {
"rescue-modifier2.rb": [
{
"span": {
"replace": [
{
"start": [
1,
1
],
"end": [
1,
4
]
},
{
"start": [
1,
1
],
"end": [
1,
4
]
}
]
},
"summary": "Replaced the 'bar' identifier with the 'foo' identifier in the 'rescue nil' modifier"
}
]
},
"errors": {}
},
"filePaths": [
"rescue-modifier2.rb"
],
"sha1": "00696e572d69a88e4fcb2e69399458f9bdd3f53a",
"gitDir": "test/corpus/repos/ruby",
"sha2": "03c0aa5215d4723f4ac7354eff3d1995a7d8548c"
}
,{
"testCaseDescription": "ruby-rescue-modifier2-replacement-test",
"expectedResult": {
"changes": {
"rescue-modifier2.rb": [
{
"span": {
"replace": [
{
"start": [
1,
1
],
"end": [
1,
4
]
},
{
"start": [
1,
1
],
"end": [
1,
4
]
}
]
},
"summary": "Replaced the 'foo' identifier with the 'bar' identifier in the 'rescue nil' modifier"
}
]
},
"errors": {}
},
"filePaths": [
"rescue-modifier2.rb"
],
"sha1": "03c0aa5215d4723f4ac7354eff3d1995a7d8548c",
"gitDir": "test/corpus/repos/ruby",
"sha2": "fe6fceb0e275a75cc0e5db4d1972e3446094b960"
}
,{
"testCaseDescription": "ruby-rescue-modifier2-delete-replacement-test",
"expectedResult": {
"changes": {
"rescue-modifier2.rb": [
{
"span": {
"delete": {
"start": [
1,
1
],
"end": [
1,
15
]
}
},
"summary": "Deleted the 'rescue nil' modifier"
},
{
"span": {
"delete": {
"start": [
2,
1
],
"end": [
2,
15
]
}
},
"summary": "Deleted the 'rescue nil' modifier"
},
{
"span": {
"insert": {
"start": [
2,
1
],
"end": [
2,
15
]
}
},
"summary": "Added the 'rescue nil' modifier"
}
]
},
"errors": {}
},
"filePaths": [
"rescue-modifier2.rb"
],
"sha1": "fe6fceb0e275a75cc0e5db4d1972e3446094b960",
"gitDir": "test/corpus/repos/ruby",
"sha2": "e8a2fe4b6228a8b52635301f8055244f16536565"
}
,{
"testCaseDescription": "ruby-rescue-modifier2-delete-test",
"expectedResult": {
"changes": {
"rescue-modifier2.rb": [
{
"span": {
"delete": {
"start": [
1,
1
],
"end": [
1,
15
]
}
},
"summary": "Deleted the 'rescue nil' modifier"
}
]
},
"errors": {}
},
"filePaths": [
"rescue-modifier2.rb"
],
"sha1": "e8a2fe4b6228a8b52635301f8055244f16536565",
"gitDir": "test/corpus/repos/ruby",
"sha2": "c6fd6f46b12e65ad9f69b10d4989d9bc7033347a"
}
,{
"testCaseDescription": "ruby-rescue-modifier2-delete-rest-test",
"expectedResult": {
"changes": {
"rescue-modifier2.rb": [
{
"span": {
"delete": {
"start": [
1,
1
],
"end": [
1,
15
]
}
},
"summary": "Deleted the 'rescue nil' modifier"
}
]
},
"errors": {}
},
"filePaths": [
"rescue-modifier2.rb"
],
"sha1": "c6fd6f46b12e65ad9f69b10d4989d9bc7033347a",
"gitDir": "test/corpus/repos/ruby",
"sha2": "11ad4e0860b7b48bdcb45da7f1f47a0ed2170060"
}]

View File

@ -0,0 +1,14 @@
[
{
"language": "ruby",
"fileExt": ".rb",
"repoUrl": "https://github.com/diff-fixtures/ruby.git",
"syntaxes": [
{
"syntax": "rescue-modifier2",
"insert": "foo rescue nil",
"replacement": "bar rescue nil"
}
]
}
]

View File

@ -239,6 +239,16 @@
"insert": "begin\n foo\nrescue Error => x\nend",
"replacement": "begin\n foo\nrescue Error => x\n bar\nend"
},
{
"syntax": "rescue-modifier",
"insert": "foo rescue nil",
"replacement": "foo rescue false"
},
{
"syntax": "rescue-modifier2",
"insert": "foo rescue nil",
"replacement": "bar rescue nil"
},
{
"syntax": "case-when",
"insert": "case foo\nend",

@ -1 +1 @@
Subproject commit d00fdf73e29c3ee54c0d74ee0083b049c296c0de
Subproject commit 11ad4e0860b7b48bdcb45da7f1f47a0ed2170060

@ -1 +1 @@
Subproject commit 2e0c69c2b61519b0dc0b5c597d02f3bfe8f5b767
Subproject commit 7dfb3f7ed5a00f516859cf60f2861dd3b41fe28f