Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Use TSX parser instead of Typescript parser for JavaScript and JSX #229

Merged
merged 5 commits into from
Aug 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Language/TSX/Assignment.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
Expand Down
2 changes: 1 addition & 1 deletion src/Language/TSX/Syntax/JSX.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 6 additions & 2 deletions src/Parsing/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/javascript/corpus/jsx.A.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function Something() {
return <div>
<Foo>hello</Foo>
</div>;
}
5 changes: 5 additions & 0 deletions test/fixtures/javascript/corpus/jsx.B.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function Something() {
return <div>
<Foo<T>>goodbye</Foo>
</div>;
}
25 changes: 25 additions & 0 deletions test/fixtures/javascript/corpus/jsx.diffA-B.txt
Original file line number Diff line number Diff line change
@@ -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)))))))
25 changes: 25 additions & 0 deletions test/fixtures/javascript/corpus/jsx.diffB-A.txt
Original file line number Diff line number Diff line change
@@ -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)))))))
22 changes: 22 additions & 0 deletions test/fixtures/javascript/corpus/jsx.parseA.txt
Original file line number Diff line number Diff line change
@@ -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)))))))
23 changes: 23 additions & 0 deletions test/fixtures/javascript/corpus/jsx.parseB.txt
Original file line number Diff line number Diff line change
@@ -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)))))))
4 changes: 3 additions & 1 deletion test/fixtures/tsx/corpus/jsx-elements.diffA-B.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
(JsxElement
(JsxOpeningElement
(Identifier)
(Empty)
Copy link
Contributor Author

@maxbrunsfeld maxbrunsfeld Aug 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JsxOpeningElement terms now have an optional TypeArguments child. We based this off the way that type arguments are handled in the New node.

{+(JsxAttribute
{+(Identifier)+}
{+(JsxExpression
Expand All @@ -31,7 +32,8 @@
{-(JsxOpeningElement
{-(NestedIdentifier
{-(Identifier)-}
{-(Identifier)-})-})-}
{-(Identifier)-})-}
{-(Empty)-})-}
{-(JsxClosingElement
{-(NestedIdentifier
{-(Identifier)-}
Expand Down
4 changes: 3 additions & 1 deletion test/fixtures/tsx/corpus/jsx-elements.diffB-A.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
(JsxElement
(JsxOpeningElement
(Identifier)
(Empty)
{+(JsxExpression
{+(Call
{+(Identifier)+}
Expand All @@ -31,7 +32,8 @@
{+(JsxOpeningElement
{+(NestedIdentifier
{+(Identifier)+}
{+(Identifier)+})+})+}
{+(Identifier)+})+}
{+(Empty)+})+}
{+(JsxClosingElement
{+(NestedIdentifier
{+(Identifier)+}
Expand Down
4 changes: 3 additions & 1 deletion test/fixtures/tsx/corpus/jsx-elements.parseA.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
(JsxElement
(JsxOpeningElement
(Identifier)
(Empty)
(JsxExpression
(Call
(Identifier)
Expand All @@ -25,7 +26,8 @@
(JsxOpeningElement
(NestedIdentifier
(Identifier)
(Identifier)))
(Identifier))
(Empty))
(JsxClosingElement
(NestedIdentifier
(Identifier)
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/tsx/corpus/jsx-elements.parseB.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
(JsxElement
(JsxOpeningElement
(Identifier)
(Empty)
(JsxAttribute
(Identifier)
(JsxExpression
Expand Down