1
1
mirror of https://github.com/github/semantic.git synced 2024-12-23 14:54:16 +03:00

Merge pull request #1060 from github/typescript

Add typescript support
This commit is contained in:
Josh Vera 2017-03-31 11:36:24 -04:00 committed by GitHub
commit 048003d18a
1455 changed files with 11483 additions and 1564 deletions

3
.gitmodules vendored
View File

@ -34,3 +34,6 @@
[submodule "vendor/haskell-tree-sitter"]
path = vendor/haskell-tree-sitter
url = https://github.com/tree-sitter/haskell-tree-sitter.git
[submodule "languages/typescript/vendor/tree-sitter-typescript"]
path = languages/typescript/vendor/tree-sitter-typescript
url = https://github.com/tree-sitter/tree-sitter-typescript/

@ -1 +1 @@
Subproject commit 1e46713a228508ae83e2513b194647f6c508a17c
Subproject commit ed4d8d2a42576776742f80725834b8a72796d090

@ -1 +1 @@
Subproject commit ca3d2de4bdeebba0a408fc5936883045981880cf
Subproject commit d212ef9441a55870d2683b2ddbbf2de05e3c89e2

View File

@ -18,6 +18,9 @@ library
, haskell-tree-sitter
default-language: Haskell2010
c-sources: vendor/tree-sitter-javascript/src/parser.c
, vendor/tree-sitter-javascript/src/scanner.c
cc-options: -std=c99 -Os
source-repository head
type: git

@ -1 +1 @@
Subproject commit 1fdf9f1f73b71cd82c0f977e2e42a6c5c353785c
Subproject commit 078db5b06ec5d0f158b5799e1cfb2d00c336f692

@ -1 +1 @@
Subproject commit fbe95be1a216432f8660d617e19290f43cf1a682
Subproject commit dd1c7953f2b83a86d10a6d34ae20ff11267bc5a2

View File

@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain

View File

@ -0,0 +1,6 @@
module Text.Parser.TreeSitter.TypeScript where
import Foreign.Ptr
import Text.Parser.TreeSitter
foreign import ccall "vendor/tree-sitter-typescript/src/parser.c tree_sitter_typescript" tree_sitter_typescript :: Ptr Language

View File

@ -0,0 +1,27 @@
name: typescript
version: 0.1.0
synopsis: tree-sitter typescript language bindings
description: Please see README.md
homepage: https://github.com/github/semantic-diff#readme
author: semantic-code
maintainer: vera@github.com
copyright: 2017 GitHub
category: Web
build-type: Simple
-- extra-source-files:
cabal-version: >=1.10
library
hs-source-dirs: src
exposed-modules: Text.Parser.TreeSitter.TypeScript
build-depends: base >= 4.7 && < 5
, haskell-tree-sitter
default-language: Haskell2010
c-sources: vendor/tree-sitter-typescript/src/parser.c
, vendor/tree-sitter-typescript/src/scanner.c
cc-options: -std=c99 -Os
source-repository head
type: git
location: https://github.com/github/semantic-diff

@ -0,0 +1 @@
Subproject commit c3071ea4b7278e271621825a7d3d417c7c06e960

View File

@ -33,6 +33,7 @@ library
, Language
, Language.C
, Language.JavaScript
, Language.TypeScript
, Language.Markdown
, Language.Go
, Language.Ruby
@ -104,6 +105,7 @@ library
, go
, ruby
, javascript
, typescript
, network
, clock
, yaml

View File

@ -113,6 +113,10 @@ data Category
| MathOperator
-- | A module
| Module
-- | A namespace in TypeScript.
| Namespace
-- | An interface
| Interface
-- | An import
| Import
-- | An export
@ -228,6 +232,8 @@ data Category
| Modifier Category
-- | A singleton method declaration, e.g. `def self.foo;end` in Ruby
| SingletonMethod
-- | An arbitrary type annotation.
| Ty
deriving (Eq, Generic, Ord, Show, NFData)
{-# DEPRECATED RescueModifier "Deprecated; use Modifier Rescue instead." #-}

View File

@ -69,8 +69,9 @@ algorithmWithTerms t1 t2 = maybe (linearly t1 t2) (fmap annotate) $ case (unwrap
Just $ Indexed <$> byRWS a b
(S.Module idA a, S.Module idB b) ->
Just $ S.Module <$> linearly idA idB <*> byRWS a b
(S.FunctionCall identifierA argsA, S.FunctionCall identifierB argsB) -> Just $
(S.FunctionCall identifierA typeParamsA argsA, S.FunctionCall identifierB typeParamsB argsB) -> Just $
S.FunctionCall <$> linearly identifierA identifierB
<*> byRWS typeParamsA typeParamsB
<*> byRWS argsA argsB
(S.Switch exprA casesA, S.Switch exprB casesB) -> Just $
S.Switch <$> byRWS exprA exprB
@ -84,20 +85,19 @@ algorithmWithTerms t1 t2 = maybe (linearly t1 t2) (fmap annotate) $ case (unwrap
(Array tyA a, Array tyB b) -> Just $
Array <$> maybeLinearly tyA tyB
<*> byRWS a b
(S.Class identifierA paramsA expressionsA, S.Class identifierB paramsB expressionsB) -> Just $
(S.Class identifierA clausesA expressionsA, S.Class identifierB clausesB expressionsB) -> Just $
S.Class <$> linearly identifierA identifierB
<*> maybeLinearly paramsA paramsB
<*> byRWS clausesA clausesB
<*> byRWS expressionsA expressionsB
(S.Method identifierA receiverA tyA paramsA expressionsA, S.Method identifierB receiverB tyB paramsB expressionsB) -> Just $
S.Method <$> linearly identifierA identifierB
(S.Method clausesA identifierA receiverA paramsA expressionsA, S.Method clausesB identifierB receiverB paramsB expressionsB) -> Just $
S.Method <$> byRWS clausesA clausesB
<*> linearly identifierA identifierB
<*> maybeLinearly receiverA receiverB
<*> maybeLinearly tyA tyB
<*> byRWS paramsA paramsB
<*> byRWS expressionsA expressionsB
(S.Function idA paramsA tyA bodyA, S.Function idB paramsB tyB bodyB) -> Just $
(S.Function idA paramsA bodyA, S.Function idB paramsB bodyB) -> Just $
S.Function <$> linearly idA idB
<*> byRWS paramsA paramsB
<*> maybeLinearly tyA tyB
<*> byRWS bodyA bodyB
_ -> Nothing
where

View File

@ -26,6 +26,7 @@ data Language =
| R
| Ruby
| Swift
| TypeScript
| Go
deriving (Show)
@ -34,22 +35,39 @@ languageForType :: Text -> Maybe Language
languageForType mediaType = case mediaType of
".h" -> Just C
".c" -> Just C
".js" -> Just JavaScript
".md" -> Just Markdown
".rb" -> Just Ruby
".go" -> Just Language.Go
".js" -> Just TypeScript
".ts" -> Just TypeScript
".tsx" -> Just TypeScript
_ -> Nothing
toVarDeclOrAssignment :: (HasField fields Category) => Term (S.Syntax Text) (Record fields) -> Term (S.Syntax Text) (Record fields)
toVarDeclOrAssignment child = case unwrap child of
S.Indexed [child', assignment] -> cofree $ setCategory (extract child) VarAssignment :< S.VarAssignment child' assignment
_ -> cofree $ setCategory (extract child) VarDecl :< S.VarDecl child Nothing
S.Indexed [child', assignment] -> cofree $ setCategory (extract child) VarAssignment :< S.VarAssignment [child'] assignment
S.Indexed [child'] -> cofree $ setCategory (extract child) VarDecl :< S.VarDecl [child']
S.VarDecl _ -> cofree $ setCategory (extract child) VarDecl :< unwrap child
S.VarAssignment _ _ -> child
_ -> toVarDecl child
toVarDecl :: (HasField fields Category) => Term (S.Syntax Text) (Record fields) -> Term (S.Syntax Text) (Record fields)
toVarDecl child = cofree $ setCategory (extract child) VarDecl :< S.VarDecl child Nothing
toVarDecl child = cofree $ setCategory (extract child) VarDecl :< S.VarDecl [child]
toTuple :: Term (S.Syntax Text) (Record fields) -> [Term (S.Syntax Text) (Record fields)]
toTuple child | S.Indexed [key,value] <- unwrap child = [cofree (extract child :< S.Pair key value)]
toTuple child | S.Fixed [key,value] <- unwrap child = [cofree (extract child :< S.Pair key value)]
toTuple child | S.Leaf c <- unwrap child = [cofree (extract child :< S.Comment c)]
toTuple child = pure child
toPublicFieldDefinition :: (HasField fields Category) => [SyntaxTerm Text fields] -> Maybe (S.Syntax Text (SyntaxTerm Text fields))
toPublicFieldDefinition children = case break (\x -> category (extract x) == Identifier) children of
(prev, [identifier, assignment]) -> Just $ S.VarAssignment (prev ++ [identifier]) assignment
(_, [_]) -> Just $ S.VarDecl children
_ -> Nothing
toInterface :: (HasField fields Category) => [SyntaxTerm Text fields] -> Maybe (S.Syntax Text (SyntaxTerm Text fields))
toInterface (id : rest) = case break (\x -> category (extract x) == Other "object_type") rest of
(clauses, [body]) -> Just $ S.Interface id clauses (toList (unwrap body))
_ -> Nothing
toInterface _ = Nothing

View File

@ -15,26 +15,16 @@ termAssignment
termAssignment source category children = case (category, children) of
(Module, [moduleName]) -> Just $ S.Module moduleName []
(Import, [importName]) -> Just $ S.Import importName []
(Function, [id, params, block]) -> Just $ S.Function id (toList (unwrap params)) Nothing (toList (unwrap block))
(Function, [id, params, ty, block]) -> Just $ S.Function id (toList (unwrap params)) (Just ty) (toList (unwrap block))
(Function, [id, params, block]) -> Just $ S.Function id [params] (toList (unwrap block))
(Function, [id, params, ty, block]) -> Just $ S.Function id [params, ty] (toList (unwrap block))
(For, [body]) | Other "block" <- Info.category (extract body) -> Just $ S.For [] (toList (unwrap body))
(For, [forClause, body]) | Other "for_clause" <- Info.category (extract forClause) -> Just $ S.For (toList (unwrap forClause)) (toList (unwrap body))
(For, [rangeClause, body]) | Other "range_clause" <- Info.category (extract rangeClause) -> Just $ S.For (toList (unwrap rangeClause)) (toList (unwrap body))
(TypeDecl, [identifier, ty]) -> Just $ S.TypeDecl identifier ty
(StructTy, _) -> Just (S.Ty children)
(FieldDecl, [idList])
| [ident] <- toList (unwrap idList)
-> Just (S.FieldDecl ident Nothing Nothing)
(FieldDecl, [idList, ty])
| [ident] <- toList (unwrap idList)
-> Just $ case Info.category (extract ty) of
StringLiteral -> S.FieldDecl ident Nothing (Just ty)
_ -> S.FieldDecl ident (Just ty) Nothing
(FieldDecl, [idList, ty, tag])
| [ident] <- toList (unwrap idList)
-> Just (S.FieldDecl ident (Just ty) (Just tag))
(FieldDecl, _) -> Just (S.FieldDecl children)
(ParameterDecl, param : ty) -> Just $ S.ParameterDecl (listToMaybe ty) param
(Assignment, [identifier, expression]) -> Just $ S.VarAssignment identifier expression
(Assignment, [identifier, expression]) -> Just $ S.VarAssignment [identifier] expression
(Select, _) -> Just $ S.Select (children >>= toList . unwrap)
(Go, [expr]) -> Just $ S.Go expr
(Defer, [expr]) -> Just $ S.Defer expr
@ -54,9 +44,9 @@ termAssignment source category children = case (category, children) of
(TypeAssertion, [a, b]) -> Just $ S.TypeAssertion a b
(TypeConversion, [a, b]) -> Just $ S.TypeConversion a b
-- TODO: Handle multiple var specs
(VarAssignment, [identifier, expression]) -> Just $ S.VarAssignment identifier expression
(VarDecl, [idList, ty]) | Identifier <- Info.category (extract ty) -> Just $ S.VarDecl idList (Just ty)
(FunctionCall, id : rest) -> Just $ S.FunctionCall id rest
(VarAssignment, [identifier, expression]) -> Just $ S.VarAssignment [identifier] expression
(VarDecl, children) -> Just $ S.VarDecl children
(FunctionCall, id : rest) -> Just $ S.FunctionCall id [] rest
(AnonymousFunction, [params, _, body])
| [params'] <- toList (unwrap params)
-> Just $ S.AnonymousFunction (toList (unwrap params')) (toList (unwrap body))
@ -68,11 +58,11 @@ termAssignment source category children = case (category, children) of
(IncrementStatement, _) -> Just $ S.Leaf (toText source)
(DecrementStatement, _) -> Just $ S.Leaf (toText source)
(QualifiedIdentifier, _) -> Just $ S.Leaf (toText source)
(Method, [receiverParams, name, body]) -> Just (S.Method name (Just receiverParams) Nothing [] (toList (unwrap body)))
(Method, [receiverParams, name, body]) -> Just (S.Method [] name (Just receiverParams) [] (toList (unwrap body)))
(Method, [receiverParams, name, params, body])
-> Just (S.Method name (Just receiverParams) Nothing (toList (unwrap params)) (toList (unwrap body)))
-> Just (S.Method [] name (Just receiverParams) [params] (toList (unwrap body)))
(Method, [receiverParams, name, params, ty, body])
-> Just (S.Method name (Just receiverParams) (Just ty) (toList (unwrap params)) (toList (unwrap body)))
-> Just (S.Method [] name (Just receiverParams) [params, ty] (toList (unwrap body)))
_ -> Nothing
categoryForGoName :: Text -> Category

View File

@ -24,8 +24,8 @@ termAssignment _ category children
-> Just $ S.Indexed $ a : rest
(FunctionCall, member : args)
| S.MemberAccess target method <- unwrap member
-> Just $ S.MethodCall target method (toList . unwrap =<< args)
(FunctionCall, function : args) -> Just $ S.FunctionCall function (toList . unwrap =<< args)
-> Just $ S.MethodCall target method [] (toList . unwrap =<< args)
(FunctionCall, function : args) -> Just $ S.FunctionCall function [] (toList . unwrap =<< args)
(Ternary, condition : cases) -> Just $ S.Ternary condition cases
(VarDecl, _) -> Just . S.Indexed $ toVarDeclOrAssignment <$> children
(Object, _) -> Just . S.Object Nothing $ foldMap toTuple children
@ -43,10 +43,10 @@ termAssignment _ category children
, Finally <- Info.category (extract finally)
-> Just $ S.Try [body] [catch] Nothing (Just finally)
(ArrayLiteral, _) -> Just $ S.Array Nothing children
(Method, [ identifier, params, exprs ]) -> Just $ S.Method identifier Nothing Nothing (toList (unwrap params)) (toList (unwrap exprs))
(Method, [ identifier, exprs ]) -> Just $ S.Method identifier Nothing Nothing [] (toList (unwrap exprs))
(Class, [ identifier, superclass, definitions ]) -> Just $ S.Class identifier (Just superclass) (toList (unwrap definitions))
(Class, [ identifier, definitions ]) -> Just $ S.Class identifier Nothing (toList (unwrap definitions))
(Method, [ identifier, params, exprs ]) -> Just $ S.Method [] identifier Nothing [params] (toList (unwrap exprs))
(Method, [ identifier, exprs ]) -> Just $ S.Method [] identifier Nothing [] (toList (unwrap exprs))
(Class, [ identifier, superclass, definitions ]) -> Just $ S.Class identifier [superclass] (toList (unwrap definitions))
(Class, [ identifier, definitions ]) -> Just $ S.Class identifier [] (toList (unwrap definitions))
(Import, [ statements, identifier ] ) -> Just $ S.Import identifier (toList (unwrap statements))
(Import, [ identifier ] ) -> Just $ S.Import identifier []
(Export, [ statements, identifier] ) -> Just $ S.Export (Just identifier) (toList (unwrap statements))
@ -58,8 +58,8 @@ termAssignment _ category children
| Just (exprs, body) <- unsnoc children
-> Just $ S.For exprs [body]
(Function, [ body ]) -> Just $ S.AnonymousFunction [] [body]
(Function, [ params, body ]) -> Just $ S.AnonymousFunction (toList (unwrap params)) [body]
(Function, [ id, params, body ]) -> Just $ S.Function id (toList (unwrap params)) Nothing [body]
(Function, [ params, body ]) -> Just $ S.AnonymousFunction [params] (toList (unwrap body))
(Function, [ id, params, body ]) -> Just $ S.Function id [params] (toList (unwrap body))
_ -> Nothing
categoryForJavaScriptProductionName :: Text -> Category
@ -113,6 +113,7 @@ categoryForJavaScriptProductionName name = case name of
"subscript_access" -> SubscriptAccess
"regex" -> Regex
"template_string" -> TemplateString
"template_chars" -> TemplateString
"lexical_declaration" -> VarDecl
"variable_declaration" -> VarDecl
"trailing_variable_declaration" -> VarDecl
@ -140,4 +141,5 @@ categoryForJavaScriptProductionName name = case name of
"break_statement" -> Break
"continue_statement" -> Continue
"yield_expression" -> Yield
"variable_declarator" -> VarAssignment
_ -> Other name

View File

@ -41,18 +41,18 @@ termAssignment _ category children
_ -> S.Try body rescues Nothing Nothing
(Class, constant : superclass : body)
| Superclass <- Info.category (extract superclass)
-> Just $ S.Class constant (Just superclass) body
(Class, constant : rest) -> Just $ S.Class constant Nothing rest
(SingletonClass, identifier : rest) -> Just $ S.Class identifier Nothing rest
-> Just $ S.Class constant [superclass] body
(Class, constant : rest) -> Just $ S.Class constant [] rest
(SingletonClass, identifier : rest) -> Just $ S.Class identifier [] rest
(Case, _) -> Just $ uncurry S.Switch (Prologue.break ((== When) . Info.category . extract) children)
(When, expr : body) -> Just $ S.Case expr body
(Ternary, condition : cases) -> Just $ S.Ternary condition cases
(MethodCall, fn : args)
| MemberAccess <- Info.category (extract fn)
, [target, method] <- toList (unwrap fn)
-> Just $ S.MethodCall target method (toList . unwrap =<< args)
-> Just $ S.MethodCall target method [] (toList . unwrap =<< args)
| otherwise
-> Just $ S.FunctionCall fn (toList . unwrap =<< args)
-> Just $ S.FunctionCall fn [] (toList . unwrap =<< args)
(Object, _ ) -> Just . S.Object Nothing $ foldMap toTuple children
(Modifier If, [ lhs, condition ]) -> Just $ S.If condition [lhs]
(Modifier Unless, [lhs, rhs]) -> Just $ S.If (withRecord (setCategory (extract rhs) Negate) (S.Negate rhs)) [lhs]
@ -67,15 +67,15 @@ termAssignment _ category children
(SingletonMethod, expr : methodName : rest)
| params : body <- rest
, Params <- Info.category (extract params)
-> Just $ S.Method methodName (Just expr) Nothing (toList (unwrap params)) body
-> Just $ S.Method [] methodName (Just expr) [params] body
| Identifier <- Info.category (extract methodName)
-> Just $ S.Method methodName (Just expr) Nothing [] rest
-> Just $ S.Method [] methodName (Just expr) [] rest
(Method, identifier : rest)
| params : body <- rest
, Params <- Info.category (extract params)
-> Just $ S.Method identifier Nothing Nothing (toList (unwrap params)) body
-> Just $ S.Method [] identifier Nothing [params] body
| otherwise
-> Just $ S.Method identifier Nothing Nothing [] rest
-> Just $ S.Method [] identifier Nothing [] rest
(Module, constant : body ) -> Just $ S.Module constant body
(Modifier Rescue, [lhs, rhs] ) -> Just $ S.Rescue [lhs] [rhs]
(Rescue, exceptions : exceptionVar : rest)

162
src/Language/TypeScript.hs Normal file
View File

@ -0,0 +1,162 @@
{-# LANGUAGE DataKinds #-}
module Language.TypeScript where
import Info
import Prologue
import Source
import Language
import qualified Syntax as S
import Term
termAssignment
:: Source -- ^ The source of the term.
-> Category -- ^ The category for the term.
-> [ SyntaxTerm Text '[Range, Category, SourceSpan] ] -- ^ The child nodes of the term.
-> Maybe (S.Syntax Text (SyntaxTerm Text '[Range, Category, SourceSpan])) -- ^ The resulting term, in Maybe.
termAssignment _ category children =
case (category, children) of
(Assignment, [ identifier, value ]) -> Just $ S.Assignment identifier value
(MathAssignment, [ identifier, value ]) -> Just $ S.OperatorAssignment identifier value
(MemberAccess, [ base, property ]) -> Just $ S.MemberAccess base property
(SubscriptAccess, [ base, element ]) -> Just $ S.SubscriptAccess base element
(CommaOperator, [ a, b ])
| S.Indexed rest <- unwrap b
-> Just $ S.Indexed $ a : rest
(FunctionCall, id : rest) -> case Prologue.break ((== Args) . Info.category . extract) rest of
(typeArgs, [ args ]) -> let flatArgs = toList (unwrap args) in
Just $ case unwrap id of
S.MemberAccess target method -> S.MethodCall target method typeArgs flatArgs
_ -> S.FunctionCall id typeArgs flatArgs
_ -> Nothing
(Ternary, condition : cases) -> Just $ S.Ternary condition cases
(Other "variable_declaration", _) -> Just . S.Indexed $ toVarDeclOrAssignment <$> children
(Other "trailing_variable_declaration", _) -> Just . S.Indexed $ toVarDeclOrAssignment <$> children
(Other "lexical_declaration", _) -> Just . S.Indexed $ toVarDeclOrAssignment <$> children
(VarAssignment, [id, assignment]) -> Just $ S.VarAssignment [id] assignment
(FieldDecl, _) -> Just $ S.FieldDecl children
(Object, _) -> Just . S.Object Nothing $ foldMap toTuple children
(DoWhile, [ expr, body ]) -> Just $ S.DoWhile expr body
(Constructor, [ expr ]) -> Just $ S.Constructor expr
(Try, [ body ]) -> Just $ S.Try [body] [] Nothing Nothing
(Try, [ body, catch ])
| Catch <- Info.category (extract catch)
-> Just $ S.Try [body] [catch] Nothing Nothing
(Try, [ body, finally ])
| Finally <- Info.category (extract finally)
-> Just $ S.Try [body] [] Nothing (Just finally)
(Try, [ body, catch, finally ])
| Catch <- Info.category (extract catch)
, Finally <- Info.category (extract finally)
-> Just $ S.Try [body] [catch] Nothing (Just finally)
(ArrayLiteral, _) -> Just $ S.Array Nothing children
(Method, children) -> case Prologue.break ((== ExpressionStatements) . Info.category . extract) children of
(prev, [body]) -> case Prologue.break ((== Identifier) . Info.category . extract) prev of
(prev, [id, callSignature]) -> Just $ S.Method prev id Nothing (toList (unwrap callSignature)) (toList (unwrap body))
_ -> Nothing -- No identifier found or callSignature found.
_ -> Nothing -- No body found.``
(Class, identifier : rest) -> case Prologue.break ((== Other "class_body") . Info.category . extract) rest of
(clauses, [ definitions ]) -> Just $ S.Class identifier clauses (toList (unwrap definitions))
_ -> Nothing
(Module, [ identifier, definitions ]) -> Just $ S.Module identifier (toList (unwrap definitions))
(Namespace, [ identifier, definitions ]) -> Just $ S.Namespace identifier (toList (unwrap definitions))
(Import, [ statements, identifier ] ) -> Just $ S.Import identifier (toList (unwrap statements))
(Import, [ identifier ] ) -> Just $ S.Import identifier []
(Export, [ statements, identifier] ) -> Just $ S.Export (Just identifier) (toList (unwrap statements))
(Export, [ statements ] )
| S.Indexed _ <- unwrap statements
-> Just $ S.Export Nothing (toList (unwrap statements))
| otherwise -> Just $ S.Export (Just statements) []
(For, _)
| Just (exprs, body) <- unsnoc children
-> Just $ S.For exprs [body]
(Function, children) -> case Prologue.break ((== ExpressionStatements) . Info.category . extract) children of
(inits, [body]) -> case inits of
[id, callSignature] -> Just $ S.Function id (toList (unwrap callSignature)) (toList (unwrap body))
[callSignature] -> Just $ S.AnonymousFunction (toList (unwrap callSignature)) (toList (unwrap body))
_ -> Nothing -- More than 1 identifier found or no call signature found
_ -> Nothing -- No body found.
(Ty, children) -> Just $ S.Ty children
(Interface, children) -> toInterface children
_ -> Nothing
categoryForTypeScriptName :: Text -> Category
categoryForTypeScriptName category = case category of
"object" -> Object
"expression_statement" -> ExpressionStatements
"trailing_expression_statement" -> ExpressionStatements
"this_expression" -> Identifier
"null" -> Identifier
"undefined" -> Identifier
"arrow_function" -> Function
"generator_function" -> Function
"math_op" -> MathOperator -- math operator, e.g. +, -, *, /.
"bool_op" -> BooleanOperator -- boolean operator, e.g. ||, &&.
"comma_op" -> CommaOperator -- comma operator, e.g. expr1, expr2.
"delete_op" -> Operator -- delete operator, e.g. delete x[2].
"type_op" -> Operator -- type operator, e.g. typeof Object.
"void_op" -> Operator -- void operator, e.g. void 2.
"for_statement" -> For
"trailing_for_statement" -> For
"for_in_statement" -> For
"trailing_for_in_statement" -> For
"for_of_statement" -> For
"trailing_for_of_statement" -> For
"new_expression" -> Constructor
"class" -> Class
"catch" -> Catch
"finally" -> Finally
"if_statement" -> If
"trailing_if_statement" -> If
"empty_statement" -> Empty
"program" -> Program
"function_call" -> FunctionCall
"pair" -> Pair
"string" -> StringLiteral
"integer" -> IntegerLiteral
"number" -> NumberLiteral
"float" -> FloatLiteral
"symbol" -> SymbolLiteral
"array" -> ArrayLiteral
"function" -> Function
"identifier" -> Identifier
"formal_parameters" -> Params
"arguments" -> Args
"statement_block" -> ExpressionStatements
"assignment" -> Assignment
"member_access" -> MemberAccess
"op" -> Operator
"subscript_access" -> SubscriptAccess
"regex" -> Regex
"template_string" -> TemplateString
"switch_statement" -> Switch
"math_assignment" -> MathAssignment
"case" -> Case
"true" -> Boolean
"false" -> Boolean
"ternary" -> Ternary
"while_statement" -> While
"trailing_while_statement" -> While
"do_statement" -> DoWhile
"trailing_do_statement" -> DoWhile
"return_statement" -> Return
"trailing_return_statement" -> Return
"throw_statement" -> Throw
"trailing_throw_statement" -> Throw
"try_statement" -> Try
"method_definition" -> Method
"comment" -> Comment
"bitwise_op" -> BitwiseOperator
"rel_op" -> RelationalOperator
"import_statement" -> Import
"export_statement" -> Export
"break_statement" -> Break
"continue_statement" -> Continue
"yield_expression" -> Yield
"public_field_definition" -> FieldDecl
"variable_declarator" -> VarAssignment
"type_annotation" -> Ty
"template_chars" -> TemplateString
"module" -> Module
"ambient_namespace" -> Namespace
"interface_declaration" -> Interface
name -> Other name

View File

@ -27,6 +27,7 @@ import Text.Parser.TreeSitter.C
import Text.Parser.TreeSitter.Go
import Text.Parser.TreeSitter.JavaScript
import Text.Parser.TreeSitter.Ruby
import Text.Parser.TreeSitter.TypeScript
data ParseTreeFile = ParseTreeFile { parseTreeFilePath :: FilePath, node :: ParseNode } deriving (Show)
@ -157,6 +158,7 @@ parserForType :: Text -> Parser (Syntax Text) (Record '[Range, Category, SourceS
parserForType mediaType = case languageForType mediaType of
Just C -> treeSitterParser C tree_sitter_c
Just JavaScript -> treeSitterParser JavaScript tree_sitter_javascript
Just TypeScript -> treeSitterParser TypeScript tree_sitter_typescript
Just Markdown -> cmarkParser
Just Ruby -> treeSitterParser Ruby tree_sitter_ruby
Just Language.Go -> treeSitterParser Language.Go tree_sitter_go

View File

@ -101,16 +101,16 @@ syntaxToTermField syntax = case syntax of
Leaf _ -> []
Indexed c -> childrenFields c
Fixed c -> childrenFields c
S.FunctionCall identifier parameters -> [ "identifier" .= identifier ] <> [ "parameters" .= parameters ]
S.FunctionCall identifier typeParameters parameters -> [ "identifier" .= identifier ] <> [ "typeArguments" .= typeParameters] <> [ "parameters" .= parameters ]
S.Ternary expression cases -> [ "expression" .= expression ] <> [ "cases" .= cases ]
S.AnonymousFunction parameters c -> [ "parameters" .= parameters ] <> childrenFields c
S.Function identifier parameters ty c -> [ "identifier" .= identifier ] <> [ "parameters" .= parameters ] <> [ "type" .= ty ] <> childrenFields c
S.AnonymousFunction callSignature c -> [ "callSignature" .= callSignature ] <> childrenFields c
S.Function identifier callSignature c -> [ "identifier" .= identifier ] <> [ "callSignature" .= callSignature ] <> childrenFields c
S.Assignment assignmentId value -> [ "identifier" .= assignmentId ] <> [ "value" .= value ]
S.OperatorAssignment identifier value -> [ "identifier" .= identifier ] <> [ "value" .= value ]
S.MemberAccess identifier value -> [ "identifier" .= identifier ] <> [ "value" .= value ]
S.MethodCall identifier methodIdentifier parameters -> [ "identifier" .= identifier ] <> [ "methodIdentifier" .= methodIdentifier ] <> [ "parameters" .= parameters ]
S.MethodCall identifier methodIdentifier typeParameters parameters -> [ "identifier" .= identifier ] <> [ "methodIdentifier" .= methodIdentifier ] <> [ "typeParameters" .= typeParameters ] <> [ "parameters" .= parameters ]
S.Operator syntaxes -> [ "operatorSyntaxes" .= syntaxes ]
S.VarDecl declaration ty -> [ "declaration" .= declaration ] <> [ "type" .= ty]
S.VarDecl children -> childrenFields children
S.VarAssignment identifier value -> [ "identifier" .= identifier ] <> [ "value" .= value ]
S.SubscriptAccess identifier property -> [ "identifier" .= identifier ] <> [ "property" .= property ]
S.Switch expression cases -> [ "expression" .= expression ] <> [ "cases" .= cases ]
@ -129,9 +129,11 @@ syntaxToTermField syntax = case syntax of
S.Try body catchExpression elseExpression finallyExpression -> [ "body" .= body ] <> [ "catchExpression" .= catchExpression ] <> [ "elseExpression" .= elseExpression ] <> [ "finallyExpression" .= finallyExpression ]
S.Array ty c -> [ "type" .= ty ] <> childrenFields c
S.Class identifier superclass definitions -> [ "identifier" .= identifier ] <> [ "superclass" .= superclass ] <> [ "definitions" .= definitions ]
S.Method identifier receiver ty parameters definitions -> [ "identifier" .= identifier ] <> [ "receiver" .= receiver ] <> [ "type" .= ty ] <> [ "parameters" .= parameters ] <> [ "definitions" .= definitions ]
S.Method clauses identifier receiver callSignature definitions -> [ "clauses" .= clauses ] <> [ "identifier" .= identifier ] <> [ "receiver" .= receiver ] <> [ "callSignature" .= callSignature ] <> [ "definitions" .= definitions ]
S.If expression clauses -> [ "expression" .= expression ] <> childrenFields clauses
S.Module identifier definitions-> [ "identifier" .= identifier ] <> [ "definitions" .= definitions ]
S.Module identifier definitions -> [ "identifier" .= identifier ] <> [ "definitions" .= definitions ]
S.Namespace identifier definitions -> [ "identifier" .= identifier ] <> [ "definitions" .= definitions ]
S.Interface identifier clauses definitions -> [ "identifier" .= identifier ] <> [ "clauses" .= clauses ] <> [ "definitions" .= definitions ]
S.Import identifier statements -> [ "identifier" .= identifier ] <> [ "statements" .= statements ]
S.Export identifier statements -> [ "identifier" .= identifier ] <> [ "statements" .= statements ]
S.Yield expr -> [ "yieldExpression" .= expr ]
@ -149,7 +151,7 @@ syntaxToTermField syntax = case syntax of
S.ParameterDecl ty field -> [ "type" .= ty ] <> [ "identifier" .= field ]
S.DefaultCase c -> childrenFields c
S.TypeDecl id ty -> [ "type" .= ty ] <> [ "identifier" .= id ]
S.FieldDecl id ty tag -> [ "type" .= ty ] <> [ "identifier" .= id ] <> [ "tag" .= tag]
S.FieldDecl children -> childrenFields children
S.Ty ty -> [ "type" .= ty ]
S.Send channel expr -> [ "channel" .= channel ] <> [ "expression" .= expr ]
where childrenFields c = [ "children" .= c ]

View File

@ -84,6 +84,8 @@ styleName category = "category-" <> case category of
C.CommaOperator -> "comma_operator"
Other string -> string
C.Module -> "module_statement"
C.Namespace -> "namespace"
C.Interface -> "interface"
C.Import -> "import_statement"
C.Export -> "export_statement"
C.AnonymousFunction -> "anonymous_function"
@ -149,6 +151,7 @@ styleName category = "category-" <> case category of
C.FieldDeclarations -> "field_declarations"
C.RuneLiteral -> "rune_literal"
C.Modifier c -> styleName c <> "_modifier"
C.Ty -> "type"
-- | Pick the class name for a split patch.
splitPatchToClassName :: SplitPatch a -> AttributeValue

View File

@ -38,6 +38,8 @@ annotatable term = isAnnotatable (unwrap term) term
S.Method{} -> Annotatable
S.Function{} -> Annotatable
S.Module{} -> Annotatable
S.Namespace{} -> Annotatable
S.Interface{} -> Annotatable
_ -> Unannotatable
data Identifiable a = Identifiable a | Unidentifiable a
@ -53,6 +55,8 @@ identifiable term = isIdentifiable (unwrap term) term
S.VarAssignment{} -> Identifiable
S.SubscriptAccess{} -> Identifiable
S.Module{} -> Identifiable
S.Namespace{} -> Identifiable
S.Interface{} -> Identifiable
S.Class{} -> Identifiable
S.Method{} -> Identifiable
S.Leaf{} -> Identifiable
@ -214,9 +218,9 @@ toTermName source term = case unwrap term of
S.Indexed children -> maybe "branch" sconcat (nonEmpty (intersperse ", " (toTermName' <$> children)))
Leaf leaf -> toS leaf
S.Assignment identifier _ -> toTermName' identifier
S.Function identifier _ _ _ -> toTermName' identifier
S.Function identifier _ _ -> toTermName' identifier
S.ParameterDecl _ _ -> termNameFromSource term
S.FunctionCall i args -> case unwrap i of
S.FunctionCall i _ args -> case unwrap i of
S.AnonymousFunction params _ ->
-- Omit a function call's arguments if it's arguments match the underlying
-- anonymous function's arguments.
@ -229,7 +233,7 @@ toTermName source term = case unwrap term of
(S.FunctionCall{}, _) -> toTermName' base <> "()." <> toTermName' property
(_, S.FunctionCall{}) -> toTermName' base <> "." <> toTermName' property <> "()"
(_, _) -> toTermName' base <> "." <> toTermName' property
S.MethodCall targetId methodId methodParams -> toTermName' targetId <> sep <> toTermName' methodId <> paramsToArgNames methodParams
S.MethodCall targetId methodId _ methodParams -> toTermName' targetId <> sep <> toTermName' methodId <> paramsToArgNames methodParams
where sep = case unwrap targetId of
S.FunctionCall{} -> "()."
_ -> "."
@ -241,8 +245,8 @@ toTermName source term = case unwrap term of
SliceTy -> termNameFromSource base <> toTermName' element
_ -> toTermName' base <> "[" <> toTermName' element <> "]"
(_, _) -> toTermName' base <> "[" <> toTermName' element <> "]"
S.VarAssignment varId _ -> toTermName' varId
S.VarDecl decl _ -> toTermName' decl
S.VarAssignment varId _ -> termNameFromChildren term varId
S.VarDecl _ -> termNameFromSource term
-- TODO: We should remove Case from Syntax since I don't think we should ever
-- evaluate Case as a single toTermName Text - joshvera
S.Case expr _ -> termNameFromSource expr
@ -265,11 +269,13 @@ toTermName source term = case unwrap term of
S.Select clauses -> termNameFromChildren term clauses
S.Array ty _ -> maybe (termNameFromSource term) termNameFromSource ty
S.Class identifier _ _ -> toTermName' identifier
S.Method identifier (Just receiver) _ args _ -> termNameFromSource receiver <> "." <> toTermName' identifier <> paramsToArgNames args
S.Method identifier Nothing _ args _ -> toTermName' identifier <> paramsToArgNames args
S.Method _ identifier (Just receiver) args _ -> termNameFromSource receiver <> "." <> toTermName' identifier <> paramsToArgNames args
S.Method _ identifier Nothing args _ -> toTermName' identifier <> paramsToArgNames args
S.Comment a -> toS a
S.Commented _ _ -> termNameFromChildren term (toList $ unwrap term)
S.Module identifier _ -> toTermName' identifier
S.Namespace identifier _ -> toTermName' identifier
S.Interface identifier _ _ -> toTermName' identifier
S.Import identifier [] -> termNameFromSource identifier
S.Import identifier exprs -> termNameFromChildren term exprs <> " from " <> toTermName' identifier
S.Export Nothing expr -> "{ " <> Text.intercalate ", " (termNameFromSource <$> expr) <> " }"
@ -282,7 +288,7 @@ toTermName source term = case unwrap term of
S.Continue expr -> maybe "" toTermName' expr
S.BlockStatement children -> termNameFromChildren term children
S.DefaultCase children -> termNameFromChildren term children
S.FieldDecl id expr tag -> termNameFromSource id <> maybe "" (\expr' -> " " <> termNameFromSource expr') expr <> maybe "" ((" " <>) . termNameFromSource) tag
S.FieldDecl children -> termNameFromChildren term children
where toTermName' = toTermName source
termNameFromChildren term children = termNameFromRange (unionRangesFrom (range term) (range <$> children))
termNameFromSource term = termNameFromRange (range term)
@ -381,6 +387,7 @@ instance HasCategory Text where
instance HasCategory Category where
toCategoryName category = case category of
C.Ty -> "type"
ArrayLiteral -> "array"
BooleanOperator -> "boolean operator"
MathOperator -> "math operator"
@ -433,6 +440,8 @@ instance HasCategory Category where
C.CommaOperator -> "comma operator"
C.Empty -> "empty statement"
C.Module -> "module"
C.Namespace -> "namespace"
C.Interface -> "interface"
C.Import -> "import statement"
C.Export -> "export statement"
C.AnonymousFunction -> "anonymous function"

View File

@ -165,9 +165,9 @@ termToDiffInfo source term = case unwrap term of
toTermName :: forall leaf fields. DefaultFields fields => Int -> Source -> SyntaxTerm leaf fields -> Text
toTermName parentOffset parentSource term = case unwrap term of
S.Function identifier _ _ _ -> toTermName' identifier
S.Method identifier Nothing _ _ _ -> toTermName' identifier
S.Method identifier (Just receiver) _ _ _ -> case unwrap receiver of
S.Function identifier _ _ -> toTermName' identifier
S.Method _ identifier Nothing _ _ -> toTermName' identifier
S.Method _ identifier (Just receiver) _ _ -> case unwrap receiver of
S.Indexed [receiverParams] -> case unwrap receiverParams of
S.ParameterDecl (Just ty) _ -> "(" <> toTermName' ty <> ") " <> toTermName' identifier
_ -> toMethodNameWithReceiver receiver identifier

View File

@ -1,6 +1,8 @@
{-# LANGUAGE DeriveAnyClass, ScopedTypeVariables #-}
{-# LANGUAGE DeriveAnyClass, ScopedTypeVariables, DataKinds, KindSignatures #-}
module Syntax where
import Data.Record
import qualified Info
import Data.Aeson
import Data.Functor.Classes
import Data.Functor.Classes.Eq.Generic
@ -21,13 +23,13 @@ data Syntax a f
-- | An ordered branch of child nodes, expected to be of fixed length in the grammar, e.g. a binary operator & its operands.
| Fixed [f]
-- | A function call has an identifier where f is a (Leaf a) and a list of arguments.
| FunctionCall f [f]
| FunctionCall f [f] [f]
-- | A ternary has a condition, a true case and a false case
| Ternary { ternaryCondition :: f, ternaryCases :: [f] }
-- | An anonymous function has a list of expressions and params.
| AnonymousFunction { params :: [f], expressions :: [f] }
-- | A function has a list of expressions.
| Function { id :: f, params :: [f], ty :: (Maybe f), expressions :: [f] }
-- | A function has an identifier, possible type arguments, params, a possible type, and list of expressions.
| Function { id :: f, params :: [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.)
| Assignment { assignmentId :: f, value :: f }
-- | An operator assignment represents expressions with operators like math (e.g x += 1) or conditional (e.g. x ||= 1) assignment.
@ -37,13 +39,13 @@ data Syntax a f
| MemberAccess { memberId :: f, property :: f }
-- | A method call consisting of its target, the method name, and the parameters passed to the method.
-- | e.g. in Javascript console.log('hello') represents a method call.
| MethodCall { targetId :: f, methodId :: f, methodParams :: [f] }
| MethodCall { targetId :: f, methodId :: f, typeArgs :: [f], methodParams :: [f] }
-- | An operator can be applied to a list of syntaxes.
| Operator [f]
-- | A variable declaration. e.g. var foo;
| VarDecl f (Maybe f)
| VarDecl [f]
-- | A variable assignment in a variable declaration. var foo = bar;
| VarAssignment { varId :: f, varValue :: f }
| VarAssignment { varId :: [f], varValue :: f }
-- | A subscript access contains a syntax, and another syntax that indefies a property or value in the first syntax.
-- | e.g. in Javascript x["y"] represents a subscript access syntax.
| SubscriptAccess { subscriptId :: f, subscriptElement :: f }
@ -72,13 +74,16 @@ data Syntax a f
-- | An array literal with list of children.
| Array (Maybe f) [f]
-- | A class with an identifier, superclass, and a list of definitions.
| Class f (Maybe f) [f]
-- | A method definition with an identifier, optional receiver, optional return type, params, and a list of expressions.
| Method f (Maybe f) (Maybe f) [f] [f]
| Class f [f] [f]
-- | A method definition with an identifier, optional receiver, optional type arguments, params, optional return type, and a list of expressions.
| Method [f] f (Maybe f) [f] [f]
-- | An if statement with an expression and maybe more expression clauses.
| If f [f]
-- | A module with an identifier, and a list of syntaxes.
| Module { moduleId:: f, moduleBody :: [f] }
-- | An interface with an identifier, a list of clauses, and a list of declarations..
| Interface f [f] [f]
| Namespace { namespaceId:: f, namespaceBody :: [f] }
| Import f [f]
| Export (Maybe f) [f]
| Yield [f]
@ -101,7 +106,7 @@ data Syntax a f
-- | A type declaration has an identifier and a type.
| TypeDecl f f
-- | A field declaration with an optional type, and an optional tag.
| FieldDecl f (Maybe f) (Maybe f)
| FieldDecl [f]
-- | A type.
| Ty [f]
-- | A send statement has a channel and an expression in Go.
@ -114,21 +119,21 @@ extractLeafValue syntax = case syntax of
Leaf a -> Just a
_ -> Nothing
maybeIdentifier :: forall leaf identifier. Syntax leaf identifier -> Maybe identifier
maybeIdentifier :: forall leaf (fields :: [*]). (HasField fields Info.Category) => Syntax leaf (Cofree (Syntax leaf) (Record fields)) -> Maybe (Cofree (Syntax leaf) (Record fields))
maybeIdentifier syntax = case syntax of
Assignment f _ -> Just f
Class f _ _ -> Just f
Export f _ -> f
Function f _ _ _ -> Just f
FunctionCall f _ -> Just f
Function f _ _ -> Just f
FunctionCall f _ _ -> Just f
Import f _ -> Just f
Method f _ _ _ _ -> Just f
MethodCall _ f _ -> Just f
Method _ f _ _ _ -> Just f
MethodCall _ f _ _ -> Just f
Module f _ -> Just f
OperatorAssignment f _ -> Just f
SubscriptAccess f _ -> Just f
TypeDecl f _ -> Just f
VarAssignment f _ -> Just f
VarAssignment f _ -> find ((== Info.Identifier) . Info.category . extract) f
_ -> Nothing
-- Instances
@ -138,17 +143,17 @@ instance Listable2 Syntax where
= liftCons1 leaf Leaf
\/ liftCons1 (liftTiers recur) Indexed
\/ liftCons1 (liftTiers recur) Fixed
\/ liftCons2 recur (liftTiers recur) FunctionCall
\/ liftCons3 recur (liftTiers recur) (liftTiers recur) FunctionCall
\/ liftCons2 recur (liftTiers recur) Ternary
\/ liftCons2 (liftTiers recur) (liftTiers recur) AnonymousFunction
\/ liftCons4 recur (liftTiers recur) (liftTiers recur) (liftTiers recur) Function
\/ liftCons3 recur (liftTiers recur) (liftTiers recur) Function
\/ liftCons2 recur recur Assignment
\/ liftCons2 recur recur OperatorAssignment
\/ liftCons2 recur recur MemberAccess
\/ liftCons3 recur recur (liftTiers recur) MethodCall
\/ liftCons4 recur recur (liftTiers recur) (liftTiers recur) MethodCall
\/ liftCons1 (liftTiers recur) Operator
\/ liftCons2 recur (liftTiers recur) VarDecl
\/ liftCons2 recur recur VarAssignment
\/ liftCons1 (liftTiers recur) VarDecl
\/ liftCons2 (liftTiers recur) recur VarAssignment
\/ liftCons2 recur recur SubscriptAccess
\/ liftCons2 (liftTiers recur) (liftTiers recur) Switch
\/ liftCons2 recur (liftTiers recur) Case
@ -167,9 +172,10 @@ instance Listable2 Syntax where
\/ liftCons4 (liftTiers recur) (liftTiers recur) (liftTiers recur) (liftTiers recur) Try
\/ liftCons2 (liftTiers recur) (liftTiers recur) Syntax.Array
\/ liftCons3 recur (liftTiers recur) (liftTiers recur) Class
\/ liftCons5 recur (liftTiers recur) (liftTiers recur) (liftTiers recur) (liftTiers recur) Method
\/ liftCons5 (liftTiers recur) recur (liftTiers recur) (liftTiers recur) (liftTiers recur) Method
\/ liftCons2 recur (liftTiers recur) If
\/ liftCons2 recur (liftTiers recur) Module
\/ liftCons2 recur (liftTiers recur) Namespace
\/ liftCons2 recur (liftTiers recur) Import
\/ liftCons2 (liftTiers recur) (liftTiers recur) Export
\/ liftCons1 (liftTiers recur) Yield
@ -184,7 +190,7 @@ instance Listable2 Syntax where
\/ liftCons1 (liftTiers recur) BlockStatement
\/ liftCons2 (liftTiers recur) recur ParameterDecl
\/ liftCons2 recur recur TypeDecl
\/ liftCons3 recur (liftTiers recur) (liftTiers recur) FieldDecl
\/ liftCons1 (liftTiers recur) FieldDecl
\/ liftCons1 (liftTiers recur) Ty
\/ liftCons2 recur recur Send
\/ liftCons1 (liftTiers recur) DefaultCase

View File

@ -11,6 +11,7 @@ import Language
import qualified Language.C as C
import qualified Language.Go as Go
import qualified Language.JavaScript as JS
import qualified Language.TypeScript as TS
import qualified Language.Ruby as Ruby
import Parser
import Range
@ -66,7 +67,6 @@ documentToTerm language document SourceBlob{..} = do
copyNamed = ts_node_copy_named_child_nodes document
copyAll = ts_node_copy_child_nodes document
isNonEmpty :: HasField fields Category => SyntaxTerm Text fields -> Bool
isNonEmpty = (/= Empty) . category . extract
@ -88,6 +88,7 @@ assignTerm language source annotation children allChildren =
C -> C.termAssignment
Language.Go -> Go.termAssignment
Ruby -> Ruby.termAssignment
TypeScript -> TS.termAssignment
_ -> \ _ _ _ -> Nothing
defaultTermAssignment :: Source -> Category -> [ SyntaxTerm Text '[Range, Category, SourceSpan] ] -> IO [ SyntaxTerm Text '[Range, Category, SourceSpan] ] -> IO (S.Syntax Text (SyntaxTerm Text '[Range, Category, SourceSpan]))
@ -132,12 +133,15 @@ defaultTermAssignment source category children allChildren
categoryForLanguageProductionName :: Language -> Text -> Category
categoryForLanguageProductionName = withDefaults . byLanguage
where withDefaults productionMap name = case name of
"ERROR" -> ParseError
s -> productionMap s
byLanguage language = case language of
JavaScript -> JS.categoryForJavaScriptProductionName
C -> C.categoryForCProductionName
Ruby -> Ruby.categoryForRubyName
Language.Go -> Go.categoryForGoName
_ -> Other
where
withDefaults productionMap name = case name of
"ERROR" -> ParseError
s -> productionMap s
byLanguage language = case language of
JavaScript -> JS.categoryForJavaScriptProductionName
C -> C.categoryForCProductionName
Ruby -> Ruby.categoryForRubyName
Language.Go -> Go.categoryForGoName
TypeScript -> TS.categoryForTypeScriptName
_ -> Other

View File

@ -27,10 +27,12 @@ spec = parallel $ do
examples "test/fixtures/go/" `shouldNotReturn` []
examples "test/fixtures/javascript/" `shouldNotReturn` []
examples "test/fixtures/ruby/" `shouldNotReturn` []
examples "test/fixtures/typescript/" `shouldNotReturn` []
describe "go" $ runTestsIn "test/fixtures/go/"
describe "javascript" $ runTestsIn "test/fixtures/javascript/"
describe "ruby" $ runTestsIn "test/fixtures/ruby/"
describe "typescript" $ runTestsIn "test/fixtures/typescript/"
where
runTestsIn :: FilePath -> SpecWith ()

View File

@ -134,14 +134,14 @@ numTocSummaries diff = Prologue.length $ filter (not . isErrorSummary) (diffTOC
programWithChange :: Term' -> Diff'
programWithChange body = free $ Free (pure programInfo :< Indexed [ function' ])
where
function' = free $ Free (pure functionInfo :< S.Function name' [] Nothing [ free $ Pure (Insert body) ] )
function' = free $ Free (pure functionInfo :< S.Function name' [] [ free $ Pure (Insert body) ] )
name' = free $ Free (pure (Range 0 0 :. C.Identifier :. sourceSpanBetween (0,0) (0,0) :. Nil) :< Leaf "foo")
-- Return a diff where term is inserted in the program, below a function found on both sides of the diff.
programWithChangeOutsideFunction :: Term' -> Diff'
programWithChangeOutsideFunction term = free $ Free (pure programInfo :< Indexed [ function', term' ])
where
function' = free $ Free (pure functionInfo :< S.Function name' [] Nothing [] )
function' = free $ Free (pure functionInfo :< S.Function name' [] [] )
name' = free $ Free (pure (Range 0 0 :. C.Identifier :. sourceSpanBetween (0,0) (0,0) :. Nil) :< Leaf "foo")
term' = free $ Pure (Insert term)
@ -158,7 +158,7 @@ programOf :: Patch Term' -> Diff'
programOf patch = free $ Free (pure programInfo :< Indexed [ free $ Pure patch ])
functionOf :: String -> Term' -> Term'
functionOf name body = cofree $ functionInfo :< S.Function name' [] Nothing [body]
functionOf name body = cofree $ functionInfo :< S.Function name' [] [body]
where
name' = cofree $ (Range 0 0 :. C.Identifier :. sourceSpanBetween (0,0) (0,0) :. Nil) :< Leaf name

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "type_declaration"
(TypeDecl
(Identifier)

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "type_declaration"
(TypeDecl
(Identifier)

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "type_declaration"
(TypeDecl
(Identifier)

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "type_declaration"
(TypeDecl
(Identifier)

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "type_declaration"
(TypeDecl
(Identifier)

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "type_declaration"
(TypeDecl
(Identifier)

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "type_declaration"
(TypeDecl
(Identifier)

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "type_declaration"
(TypeDecl
(Identifier)

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "const_declaration"
(VarAssignment
(Other "identifier_list"

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "const_declaration"
(VarAssignment
(Other "identifier_list"

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "const_declaration"
(VarAssignment
(Other "identifier_list"

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "const_declaration"
(VarAssignment
(Other "identifier_list"

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "const_declaration"
(VarAssignment
(Other "identifier_list"
@ -11,14 +12,12 @@
(Other "composite_literal"
(ArrayTy
(Identifier))
{+(Element
(NumberLiteral))+}
{+(Element
(NumberLiteral))+}
(Element
{ (NumberLiteral)
->(NumberLiteral) })
{-(Element
(NumberLiteral))-}
{-(Element
(NumberLiteral))-}))))))
(Element
{ (NumberLiteral)
->(NumberLiteral) })
(Element
{ (NumberLiteral)
->(NumberLiteral) })))))))

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "const_declaration"
(VarAssignment
(Other "identifier_list"
@ -11,14 +12,12 @@
(Other "composite_literal"
(ArrayTy
(Identifier))
{+(Element
(NumberLiteral))+}
(Element
{ (NumberLiteral)
->(NumberLiteral) })
{+(Element
(NumberLiteral))+}
{-(Element
(NumberLiteral))-}
{-(Element
(NumberLiteral))-}))))))
(Element
{ (NumberLiteral)
->(NumberLiteral) })
(Element
{ (NumberLiteral)
->(NumberLiteral) })))))))

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "const_declaration"
(VarAssignment
(Other "identifier_list"

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "const_declaration"
(VarAssignment
(Other "identifier_list"

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Assignment
(Other "expression_list"
(Identifier))

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Assignment
(Other "expression_list"
(Identifier))

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Assignment
(Other "expression_list"
(Identifier))

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Assignment
(Other "expression_list"
(Identifier))

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Assignment
(Other "expression_list"
{ (Identifier)

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Assignment
(Other "expression_list"
{ (Identifier)

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Assignment
(Other "expression_list"
(Identifier))

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Assignment
(Other "expression_list"
(Identifier))

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(FunctionCall
(Identifier)
(Identifier)

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(FunctionCall
(Identifier)
(Identifier)

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(FunctionCall
(Identifier)
(Identifier)

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(FunctionCall
(Identifier)
(Identifier)

View File

@ -3,18 +3,25 @@
(Identifier))
(Function
(Identifier)
(Args)
{+(FunctionCall
(Identifier)
(Identifier)
(Identifier))+}
(FunctionCall
{ (Identifier)
->(Identifier) }
(Identifier)
(Identifier))
(FunctionCall
{ (Identifier)
->(Identifier) }
{+(FunctionCall
(Identifier)
(Identifier))
(FunctionCall
{ (Identifier)
->(Identifier) }
(Identifier)
(Identifier))))
(Identifier))+}
{-(FunctionCall
(Identifier)
(Identifier)
(Identifier))-}
{-(FunctionCall
(Identifier)
(Identifier)
(Identifier))-}))

View File

@ -3,18 +3,25 @@
(Identifier))
(Function
(Identifier)
(Args)
{+(FunctionCall
(Identifier)
(Identifier)
(Identifier))+}
(FunctionCall
{ (Identifier)
->(Identifier) }
(Identifier)
(Identifier))
(FunctionCall
{ (Identifier)
->(Identifier) }
{+(FunctionCall
(Identifier)
(Identifier))
(FunctionCall
{ (Identifier)
->(Identifier) }
(Identifier)
(Identifier))))
(Identifier))+}
{-(FunctionCall
(Identifier)
(Identifier)
(Identifier))-}
{-(FunctionCall
(Identifier)
(Identifier)
(Identifier))-}))

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(FunctionCall
(Identifier)
(Identifier)

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(FunctionCall
(Identifier)
(Identifier)

View File

@ -3,4 +3,5 @@
(Identifier))
(Function
(Identifier)
(Args)
(Switch)))+}

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Switch
(Case
(Case

View File

@ -3,4 +3,5 @@
(Identifier))
(Function
(Identifier)
(Args)
(Switch)))-}

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Switch
(Case
(Case

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Switch
{+(Case
(Case

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Switch
{-(Case
(Case

View File

@ -3,4 +3,5 @@
(Identifier))
(Function
(Identifier)
(Args)
(Switch)))

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Switch
(Case
(Case

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "type_declaration"
(TypeDecl
(Identifier)

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "type_declaration"
(TypeDecl
(Identifier)

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "type_declaration"
(TypeDecl
(Identifier)

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "type_declaration"
(TypeDecl
(Identifier)

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "type_declaration"
(TypeDecl
{ (Identifier)

View File

@ -3,16 +3,27 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "type_declaration"
{ +(TypeDecl (Identifier) (ChannelTy (ChannelTy (Identifier))))+ }
{+(TypeDecl
(Identifier)
(ChannelTy
(ChannelTy
(Identifier))))+}
(TypeDecl
(Identifier)
(ChannelTy
(ChannelTy
{ (Identifier) -> (StructTy) })))
{ (Identifier)
->(StructTy) })))
(TypeDecl
(Identifier)
(ChannelTy
(ChannelTy
{ (StructTy) -> (Identifier) })))
{ -(TypeDecl (Identifier) (ChannelTy (ChannelTy (Identifier))))- })))
{ (StructTy)
->(Identifier) })))
{-(TypeDecl
(Identifier)
(ChannelTy
(ChannelTy
(Identifier))))-})))

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "type_declaration"
(TypeDecl
(Identifier)

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "type_declaration"
(TypeDecl
(Identifier)

View File

@ -3,4 +3,5 @@
(Identifier))
(Function
(Identifier)
(Args)
(Comment)))+}

View File

@ -3,4 +3,5 @@
(Identifier))
(Function
(Identifier)
(Args)
(Comment)))+}

View File

@ -3,4 +3,5 @@
(Identifier))
(Function
(Identifier)
(Args)
(Comment)))-}

View File

@ -3,4 +3,5 @@
(Identifier))
(Function
(Identifier)
(Args)
(Comment)))-}

View File

@ -3,5 +3,6 @@
(Identifier))
(Function
(Identifier)
(Args)
{ (Comment)
->(Comment) }))

View File

@ -3,5 +3,6 @@
(Identifier))
(Function
(Identifier)
(Args)
{ (Comment)
->(Comment) }))

View File

@ -3,4 +3,5 @@
(Identifier))
(Function
(Identifier)
(Args)
(Comment)))

View File

@ -3,4 +3,5 @@
(Identifier))
(Function
(Identifier)
(Args)
(Comment)))

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "const_declaration"
(VarAssignment
(Other "identifier_list"

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "const_declaration"
(VarAssignment
(Other "identifier_list"

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "const_declaration"
(VarAssignment
(Other "identifier_list"

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "const_declaration"
(VarAssignment
(Other "identifier_list"

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "const_declaration"
(VarAssignment
(Other "identifier_list"

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "const_declaration"
(VarAssignment
(Other "identifier_list"

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "const_declaration"
(VarAssignment
(Other "identifier_list"

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "const_declaration"
(VarAssignment
(Other "identifier_list"

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "const_declaration"
(VarAssignment
(Other "identifier_list"

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "const_declaration"
(VarAssignment
(Other "identifier_list"

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "const_declaration"
(VarAssignment
(Other "identifier_list"

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "const_declaration"
(VarAssignment
(Other "identifier_list"

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "const_declaration"
(VarAssignment
(Other "identifier_list"

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "const_declaration"
(VarAssignment
(Other "identifier_list"

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "const_declaration"
(VarAssignment
(Other "identifier_list"

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "const_declaration"
(VarAssignment
(Other "identifier_list"

View File

@ -3,6 +3,7 @@
(Identifier))
(Function
(Identifier)
(Args)
(Other "const_declaration"
(VarAssignment
(Other "identifier_list"

Some files were not shown because too many files have changed in this diff Show More