1
1
mirror of https://github.com/github/semantic.git synced 2024-12-20 05:11:44 +03:00

Merge pull request #758 from github/assignment-statements

Prevent "anonymous" function identifier in assignment diff summary statements
This commit is contained in:
Josh Vera 2016-08-22 20:28:17 -04:00 committed by GitHub
commit fc0ecda5a0
5 changed files with 19 additions and 13 deletions

View File

@ -68,11 +68,14 @@ toLeafInfos err@ErrorInfo{} = pure (err, pretty err)
toTermName :: (HasCategory leaf, HasField fields Category, HasField fields Range) => Source Char -> Term leaf (Record fields) -> Text toTermName :: (HasCategory leaf, HasField fields Category, HasField fields Range) => Source Char -> Term leaf (Record fields) -> Text
toTermName source term = case unwrap term of toTermName source term = case unwrap term of
S.AnonymousFunction _ _ -> "anonymous"
S.Fixed 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 S.Indexed children -> fromMaybe "branch" $ (toCategoryName . category) . extract <$> head children
Leaf leaf -> toCategoryName leaf Leaf leaf -> toCategoryName leaf
S.Assignment identifier value -> toTermName' identifier <> toTermName' value S.Assignment identifier value -> case (unwrap identifier, unwrap value) of
S.Function identifier _ _ -> (maybe "anonymous" toTermName' identifier) (S.MemberAccess{}, S.AnonymousFunction{..}) -> toTermName' identifier
(_, _) -> toTermName' identifier <> toTermName' value
S.Function identifier _ _ -> toTermName' identifier
S.FunctionCall i _ -> toTermName' i S.FunctionCall i _ -> toTermName' i
S.MemberAccess base property -> case (unwrap base, unwrap property) of S.MemberAccess base property -> case (unwrap base, unwrap property) of
(S.FunctionCall{}, S.FunctionCall{}) -> toTermName' base <> "()." <> toTermName' property <> "()" (S.FunctionCall{}, S.FunctionCall{}) -> toTermName' base <> "()." <> toTermName' property <> "()"
@ -132,11 +135,12 @@ toDoc = string . toS
termToDiffInfo :: (HasCategory leaf, HasField fields Category, HasField fields Range) => Source Char -> Term leaf (Record fields) -> DiffInfo termToDiffInfo :: (HasCategory leaf, HasField fields Category, HasField fields Range) => Source Char -> Term leaf (Record fields) -> DiffInfo
termToDiffInfo blob term = case unwrap term of termToDiffInfo blob term = case unwrap term of
Leaf _ -> LeafInfo (toCategoryName term) (toTermName' term) Leaf _ -> LeafInfo (toCategoryName term) (toTermName' term)
S.AnonymousFunction _ _ -> LeafInfo (toCategoryName term) ("anonymous")
S.Indexed children -> BranchInfo (termToDiffInfo' <$> children) (toCategoryName term) BIndexed S.Indexed children -> BranchInfo (termToDiffInfo' <$> children) (toCategoryName term) BIndexed
S.Fixed children -> BranchInfo (termToDiffInfo' <$> children) (toCategoryName term) BFixed S.Fixed children -> BranchInfo (termToDiffInfo' <$> children) (toCategoryName term) BFixed
S.FunctionCall identifier _ -> LeafInfo (toCategoryName term) (toTermName' identifier) S.FunctionCall identifier _ -> LeafInfo (toCategoryName term) (toTermName' identifier)
S.Ternary ternaryCondition _ -> LeafInfo (toCategoryName term) (toTermName' ternaryCondition) S.Ternary ternaryCondition _ -> LeafInfo (toCategoryName term) (toTermName' ternaryCondition)
S.Function identifier _ _ -> LeafInfo (toCategoryName term) (maybe "anonymous" toTermName' identifier) S.Function identifier _ _ -> LeafInfo (toCategoryName term) (toTermName' identifier)
S.Assignment identifier _ -> LeafInfo (toCategoryName term) (toTermName' identifier) S.Assignment identifier _ -> LeafInfo (toCategoryName term) (toTermName' identifier)
S.MathAssignment identifier _ -> LeafInfo (toCategoryName term) (toTermName' identifier) S.MathAssignment identifier _ -> LeafInfo (toCategoryName term) (toTermName' identifier)
-- Currently we cannot express the operator for an operator production from TreeSitter. Eventually we should be able to -- Currently we cannot express the operator for an operator production from TreeSitter. Eventually we should be able to
@ -154,7 +158,7 @@ prependSummary source term summary = if (isNothing $ parentAnnotation summary) &
else summary else summary
where hasIdentifier term = case unwrap term of where hasIdentifier term = case unwrap term of
S.FunctionCall{} -> True S.FunctionCall{} -> True
S.Function id _ _ -> isJust id S.Function _ _ _ -> True
S.Assignment{} -> True S.Assignment{} -> True
S.MathAssignment{} -> True S.MathAssignment{} -> True
S.MemberAccess{} -> True S.MemberAccess{} -> True

View File

@ -59,13 +59,13 @@ termConstructor source sourceSpan info = fmap cofree . construct
[child, rest] | S.Indexed cs <- unwrap rest -> S.Indexed $ child : toList cs [child, rest] | S.Indexed cs <- unwrap rest -> S.Indexed $ child : toList cs
_ -> S.Indexed children _ -> S.Indexed children
construct children | Function == category info = case children of construct children | Function == category info = case children of
(body:[]) -> withDefaultInfo $ S.Function Nothing Nothing body (body:[]) -> withDefaultInfo $ S.AnonymousFunction Nothing body
(params:body:[]) | (info :< _) <- runCofree params, Params == category info -> (params:body:[]) | (info :< _) <- runCofree params, Params == category info ->
withDefaultInfo $ S.Function Nothing (Just params) body withDefaultInfo $ S.AnonymousFunction (Just params) body
(id:body:[]) | (info :< _) <- runCofree id, Identifier == category info -> (id:body:[]) | (info :< _) <- runCofree id, Identifier == category info ->
withDefaultInfo $ S.Function (Just id) Nothing body withDefaultInfo $ S.Function id Nothing body
(id:params:body:[]) | (info :< _) <- runCofree id, Identifier == category info -> (id:params:body:[]) | (info :< _) <- runCofree id, Identifier == category info ->
withDefaultInfo $ S.Function (Just id) (Just params) body withDefaultInfo $ S.Function id (Just params) body
_ -> errorWith children _ -> errorWith children
construct children | FunctionCall == category info = case runCofree <$> children of construct children | FunctionCall == category info = case runCofree <$> children of

View File

@ -65,6 +65,7 @@ termFields info syntax = "range" .= characterRange info : "category" .= category
Leaf _ -> [] Leaf _ -> []
Indexed c -> childrenFields c Indexed c -> childrenFields c
Fixed c -> childrenFields c Fixed c -> childrenFields c
S.AnonymousFunction params c -> [ "params" .= params ] <> childrenFields c
S.FunctionCall identifier params -> [ "identifier" .= identifier ] <> [ "params" .= params ] S.FunctionCall identifier params -> [ "identifier" .= identifier ] <> [ "params" .= params ]
S.Function identifier params c -> [ "identifier" .= identifier ] <> [ "params" .= params ] <> childrenFields c S.Function identifier params c -> [ "identifier" .= identifier ] <> [ "params" .= params ] <> childrenFields c
S.MethodCall targetId methodId args -> [ "targetIdentifier" .= targetId ] <> [ "methodId" .= methodId ] <> [ "args" .= args ] S.MethodCall targetId methodId args -> [ "targetIdentifier" .= targetId ] <> [ "methodId" .= methodId ] <> [ "args" .= args ]

View File

@ -33,10 +33,9 @@ styleName :: Category -> Text
styleName category = "category-" <> case category of styleName category = "category-" <> case category of
Program -> "program" Program -> "program"
C.Error -> "error" C.Error -> "error"
BinaryOperator -> "binary-operator" BinaryOperator -> "binary_operator"
BitwiseOperator -> "bitwise-operator" BitwiseOperator -> "bitwise_operator"
RelationalOperator -> "relational-operator" RelationalOperator -> "relational_operator"
C.CommaOperator -> "comma-operator"
Boolean -> "boolean" Boolean -> "boolean"
DictionaryLiteral -> "dictionary" DictionaryLiteral -> "dictionary"
C.Pair -> "pair" C.Pair -> "pair"

View File

@ -22,8 +22,10 @@ data Syntax a f
| FunctionCall f [f] | FunctionCall f [f]
-- | A ternary has a condition, a true case and a false case -- | A ternary has a condition, a true case and a false case
| Ternary { ternaryCondition :: f, ternaryCases :: [f] } | Ternary { ternaryCondition :: f, ternaryCases :: [f] }
-- | An anonymous function has a list of expressions and params.
| AnonymousFunction { params :: (Maybe f), expressions :: f }
-- | A function has a list of expressions. -- | A function has a list of expressions.
| Function { id :: (Maybe f), params :: (Maybe f), expressions :: f } | Function { id :: f, params :: (Maybe f), expressions :: f }
-- | An assignment has an identifier where f can be a member access, and the value is another syntax element (function call, leaf, etc.) -- | An assignment has an identifier where f can be a member access, and the value is another syntax element (function call, leaf, etc.)
| Assignment { assignmentId :: f, value :: f } | Assignment { assignmentId :: f, value :: f }
-- | A math assignment represents expressions whose operator classifies as mathy (e.g. += or *=). -- | A math assignment represents expressions whose operator classifies as mathy (e.g. += or *=).