1
1
mirror of https://github.com/github/semantic.git synced 2024-12-19 12:51:52 +03:00

Merge pull request #229 from github/use-tsx-for-js-and-flow

Use TSX parser instead of Typescript parser for JavaScript and JSX
This commit is contained in:
Max Brunsfeld 2019-08-21 11:44:24 -07:00 committed by GitHub
commit b7a52b4524
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 123 additions and 7 deletions

View File

@ -429,7 +429,7 @@ jsxAttribute' :: Assignment Term
jsxAttribute' = jsxAttribute <|> jsxExpression'
jsxOpeningElement' :: Assignment Term
jsxOpeningElement' = makeTerm <$> symbol Grammar.JsxOpeningElement <*> children (TSX.Syntax.JsxOpeningElement <$> term jsxElementName <*> manyTerm jsxAttribute')
jsxOpeningElement' = makeTerm <$> symbol Grammar.JsxOpeningElement <*> children (TSX.Syntax.JsxOpeningElement <$> term jsxElementName <*> term (typeArguments' <|> emptyTerm) <*> manyTerm jsxAttribute')
jsxElementName :: Assignment Term
jsxElementName = choice [ identifier, nestedIdentifier, jsxNamespaceName ]

View File

@ -28,7 +28,7 @@ newtype JsxExpression a = JsxExpression { jsxExpression :: a }
instance Evaluatable JsxExpression
data JsxOpeningElement a = JsxOpeningElement { jsxOpeningElementIdentifier :: !a, jsxAttributes :: ![a] }
data JsxOpeningElement a = JsxOpeningElement { jsxOpeningElementIdentifier :: !a, jsxOpeningElementTypeArguments :: a, jsxAttributes :: ![a] }
deriving (Declarations1, Diffable, Eq, Foldable, FreeVariables1, Functor, Generic1, Hashable1, NFData1, Ord, Show, ToJSONFields1, Traversable)
deriving (Eq1, Show1, Ord1) via Generically JsxOpeningElement

View File

@ -191,9 +191,13 @@ someASTParser :: Language -> Maybe SomeASTParser
someASTParser Go = Just (SomeASTParser (ASTParser tree_sitter_go :: Parser (AST [] Go.Grammar)))
someASTParser Haskell = Just (SomeASTParser (ASTParser tree_sitter_haskell :: Parser (AST [] Haskell.Grammar)))
someASTParser Java = Just (SomeASTParser (ASTParser tree_sitter_java :: Parser (AST [] Java.Grammar)))
someASTParser JavaScript = Just (SomeASTParser (ASTParser tree_sitter_typescript :: Parser (AST [] TypeScript.Grammar)))
someASTParser JSON = Just (SomeASTParser (ASTParser tree_sitter_json :: Parser (AST [] JSON.Grammar)))
someASTParser JSX = Just (SomeASTParser (ASTParser tree_sitter_typescript :: Parser (AST [] TypeScript.Grammar)))
-- Use the TSX parser for `.js` and `.jsx` files in case they use Flow type-annotation syntax.
-- The TSX and Flow syntaxes are the same, whereas the normal TypeScript syntax is different.
someASTParser JavaScript = Just (SomeASTParser (ASTParser tree_sitter_tsx :: Parser (AST [] TSX.Grammar)))
someASTParser JSX = Just (SomeASTParser (ASTParser tree_sitter_tsx :: Parser (AST [] TSX.Grammar)))
someASTParser Python = Just (SomeASTParser (ASTParser tree_sitter_python :: Parser (AST [] Python.Grammar)))
someASTParser Ruby = Just (SomeASTParser (ASTParser tree_sitter_ruby :: Parser (AST [] Ruby.Grammar)))
someASTParser TypeScript = Just (SomeASTParser (ASTParser tree_sitter_typescript :: Parser (AST [] TypeScript.Grammar)))

View File

@ -0,0 +1,5 @@
function Something() {
return <div>
<Foo>hello</Foo>
</div>;
}

View File

@ -0,0 +1,5 @@
function Something() {
return <div>
<Foo<T>>goodbye</Foo>
</div>;
}

View File

@ -0,0 +1,25 @@
(Statements
(Function
(Empty)
(Empty)
(Identifier)
(StatementBlock
(Return
(JsxElement
(JsxOpeningElement
(Identifier)
(Empty))
(JsxText)
(JsxElement
(JsxOpeningElement
(Identifier)
{ (Empty)
->(TypeArguments
{+(TypeIdentifier)+}) })
{ (JsxText)
->(JsxText) }
(JsxClosingElement
(Identifier)))
(JsxText)
(JsxClosingElement
(Identifier)))))))

View File

@ -0,0 +1,25 @@
(Statements
(Function
(Empty)
(Empty)
(Identifier)
(StatementBlock
(Return
(JsxElement
(JsxOpeningElement
(Identifier)
(Empty))
(JsxText)
(JsxElement
(JsxOpeningElement
(Identifier)
{ (TypeArguments
{-(TypeIdentifier)-})
->(Empty) })
{ (JsxText)
->(JsxText) }
(JsxClosingElement
(Identifier)))
(JsxText)
(JsxClosingElement
(Identifier)))))))

View File

@ -0,0 +1,22 @@
(Statements
(Function
(Empty)
(Empty)
(Identifier)
(StatementBlock
(Return
(JsxElement
(JsxOpeningElement
(Identifier)
(Empty))
(JsxText)
(JsxElement
(JsxOpeningElement
(Identifier)
(Empty))
(JsxText)
(JsxClosingElement
(Identifier)))
(JsxText)
(JsxClosingElement
(Identifier)))))))

View File

@ -0,0 +1,23 @@
(Statements
(Function
(Empty)
(Empty)
(Identifier)
(StatementBlock
(Return
(JsxElement
(JsxOpeningElement
(Identifier)
(Empty))
(JsxText)
(JsxElement
(JsxOpeningElement
(Identifier)
(TypeArguments
(TypeIdentifier)))
(JsxText)
(JsxClosingElement
(Identifier)))
(JsxText)
(JsxClosingElement
(Identifier)))))))

View File

@ -6,6 +6,7 @@
(JsxElement
(JsxOpeningElement
(Identifier)
(Empty)
{+(JsxAttribute
{+(Identifier)+}
{+(JsxExpression
@ -31,7 +32,8 @@
{-(JsxOpeningElement
{-(NestedIdentifier
{-(Identifier)-}
{-(Identifier)-})-})-}
{-(Identifier)-})-}
{-(Empty)-})-}
{-(JsxClosingElement
{-(NestedIdentifier
{-(Identifier)-}

View File

@ -6,6 +6,7 @@
(JsxElement
(JsxOpeningElement
(Identifier)
(Empty)
{+(JsxExpression
{+(Call
{+(Identifier)+}
@ -31,7 +32,8 @@
{+(JsxOpeningElement
{+(NestedIdentifier
{+(Identifier)+}
{+(Identifier)+})+})+}
{+(Identifier)+})+}
{+(Empty)+})+}
{+(JsxClosingElement
{+(NestedIdentifier
{+(Identifier)+}

View File

@ -6,6 +6,7 @@
(JsxElement
(JsxOpeningElement
(Identifier)
(Empty)
(JsxExpression
(Call
(Identifier)
@ -25,7 +26,8 @@
(JsxOpeningElement
(NestedIdentifier
(Identifier)
(Identifier)))
(Identifier))
(Empty))
(JsxClosingElement
(NestedIdentifier
(Identifier)

View File

@ -6,6 +6,7 @@
(JsxElement
(JsxOpeningElement
(Identifier)
(Empty)
(JsxAttribute
(Identifier)
(JsxExpression