From 233a51cf62ec46b42e59a049fb648d22e668d004 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 14 Aug 2019 10:08:25 -0700 Subject: [PATCH 1/4] Use TSX parser instead of Typescript parser for JavaScript and JSX --- src/Parsing/Parser.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Parsing/Parser.hs b/src/Parsing/Parser.hs index c4bca8f5a..364744600 100644 --- a/src/Parsing/Parser.hs +++ b/src/Parsing/Parser.hs @@ -191,9 +191,9 @@ 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 JavaScript = Just (SomeASTParser (ASTParser tree_sitter_tsx :: Parser (AST [] TSX.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))) +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))) From 5d62300ba53a931d0abe0c868bc21e3d519c787d Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 14 Aug 2019 10:17:57 -0700 Subject: [PATCH 2/4] Add comment about why JS and JSX are handled with the TSX parser --- src/Parsing/Parser.hs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Parsing/Parser.hs b/src/Parsing/Parser.hs index 364744600..09c37e63e 100644 --- a/src/Parsing/Parser.hs +++ b/src/Parsing/Parser.hs @@ -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_tsx :: Parser (AST [] TSX.Grammar))) someASTParser JSON = Just (SomeASTParser (ASTParser tree_sitter_json :: Parser (AST [] JSON.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))) From 07ccbb8966972566954596dfaae94daa5ff96c68 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 21 Aug 2019 10:31:32 -0700 Subject: [PATCH 3/4] Handle JSX elements with type arguments Also, add a unit test for JSX in JavaScript. Co-Authored-By: Rick Winfrey --- src/Language/TSX/Assignment.hs | 2 +- src/Language/TSX/Syntax/JSX.hs | 2 +- test/fixtures/javascript/corpus/jsx.A.js | 5 ++++ test/fixtures/javascript/corpus/jsx.B.js | 5 ++++ .../javascript/corpus/jsx.diffA-B.txt | 25 +++++++++++++++++++ .../javascript/corpus/jsx.diffB-A.txt | 25 +++++++++++++++++++ .../fixtures/javascript/corpus/jsx.parseA.txt | 22 ++++++++++++++++ .../fixtures/javascript/corpus/jsx.parseB.txt | 23 +++++++++++++++++ 8 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/javascript/corpus/jsx.A.js create mode 100644 test/fixtures/javascript/corpus/jsx.B.js create mode 100644 test/fixtures/javascript/corpus/jsx.diffA-B.txt create mode 100644 test/fixtures/javascript/corpus/jsx.diffB-A.txt create mode 100644 test/fixtures/javascript/corpus/jsx.parseA.txt create mode 100644 test/fixtures/javascript/corpus/jsx.parseB.txt diff --git a/src/Language/TSX/Assignment.hs b/src/Language/TSX/Assignment.hs index 839f7aeca..2f79a0bae 100644 --- a/src/Language/TSX/Assignment.hs +++ b/src/Language/TSX/Assignment.hs @@ -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 ] diff --git a/src/Language/TSX/Syntax/JSX.hs b/src/Language/TSX/Syntax/JSX.hs index 9430e6743..113b0375a 100644 --- a/src/Language/TSX/Syntax/JSX.hs +++ b/src/Language/TSX/Syntax/JSX.hs @@ -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 diff --git a/test/fixtures/javascript/corpus/jsx.A.js b/test/fixtures/javascript/corpus/jsx.A.js new file mode 100644 index 000000000..5b4c90566 --- /dev/null +++ b/test/fixtures/javascript/corpus/jsx.A.js @@ -0,0 +1,5 @@ +function Something() { + return
+ hello +
; +} diff --git a/test/fixtures/javascript/corpus/jsx.B.js b/test/fixtures/javascript/corpus/jsx.B.js new file mode 100644 index 000000000..237faaca5 --- /dev/null +++ b/test/fixtures/javascript/corpus/jsx.B.js @@ -0,0 +1,5 @@ +function Something() { + return
+ >goodbye +
; +} diff --git a/test/fixtures/javascript/corpus/jsx.diffA-B.txt b/test/fixtures/javascript/corpus/jsx.diffA-B.txt new file mode 100644 index 000000000..9286a8dc2 --- /dev/null +++ b/test/fixtures/javascript/corpus/jsx.diffA-B.txt @@ -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))))))) diff --git a/test/fixtures/javascript/corpus/jsx.diffB-A.txt b/test/fixtures/javascript/corpus/jsx.diffB-A.txt new file mode 100644 index 000000000..708286acd --- /dev/null +++ b/test/fixtures/javascript/corpus/jsx.diffB-A.txt @@ -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))))))) diff --git a/test/fixtures/javascript/corpus/jsx.parseA.txt b/test/fixtures/javascript/corpus/jsx.parseA.txt new file mode 100644 index 000000000..c42723ce3 --- /dev/null +++ b/test/fixtures/javascript/corpus/jsx.parseA.txt @@ -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))))))) diff --git a/test/fixtures/javascript/corpus/jsx.parseB.txt b/test/fixtures/javascript/corpus/jsx.parseB.txt new file mode 100644 index 000000000..4916b0be6 --- /dev/null +++ b/test/fixtures/javascript/corpus/jsx.parseB.txt @@ -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))))))) From 5491001cf77590d17e82f85d52a383e33ff435ac Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 21 Aug 2019 10:49:27 -0700 Subject: [PATCH 4/4] Regenerate jsx corpus files --- test/fixtures/tsx/corpus/jsx-elements.diffA-B.txt | 4 +++- test/fixtures/tsx/corpus/jsx-elements.diffB-A.txt | 4 +++- test/fixtures/tsx/corpus/jsx-elements.parseA.txt | 4 +++- test/fixtures/tsx/corpus/jsx-elements.parseB.txt | 1 + 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/test/fixtures/tsx/corpus/jsx-elements.diffA-B.txt b/test/fixtures/tsx/corpus/jsx-elements.diffA-B.txt index 9e330c8d5..04d7fa687 100644 --- a/test/fixtures/tsx/corpus/jsx-elements.diffA-B.txt +++ b/test/fixtures/tsx/corpus/jsx-elements.diffA-B.txt @@ -6,6 +6,7 @@ (JsxElement (JsxOpeningElement (Identifier) + (Empty) {+(JsxAttribute {+(Identifier)+} {+(JsxExpression @@ -31,7 +32,8 @@ {-(JsxOpeningElement {-(NestedIdentifier {-(Identifier)-} - {-(Identifier)-})-})-} + {-(Identifier)-})-} + {-(Empty)-})-} {-(JsxClosingElement {-(NestedIdentifier {-(Identifier)-} diff --git a/test/fixtures/tsx/corpus/jsx-elements.diffB-A.txt b/test/fixtures/tsx/corpus/jsx-elements.diffB-A.txt index 6f4ba9f23..bb6402bef 100644 --- a/test/fixtures/tsx/corpus/jsx-elements.diffB-A.txt +++ b/test/fixtures/tsx/corpus/jsx-elements.diffB-A.txt @@ -6,6 +6,7 @@ (JsxElement (JsxOpeningElement (Identifier) + (Empty) {+(JsxExpression {+(Call {+(Identifier)+} @@ -31,7 +32,8 @@ {+(JsxOpeningElement {+(NestedIdentifier {+(Identifier)+} - {+(Identifier)+})+})+} + {+(Identifier)+})+} + {+(Empty)+})+} {+(JsxClosingElement {+(NestedIdentifier {+(Identifier)+} diff --git a/test/fixtures/tsx/corpus/jsx-elements.parseA.txt b/test/fixtures/tsx/corpus/jsx-elements.parseA.txt index 85d98a035..579b272fe 100644 --- a/test/fixtures/tsx/corpus/jsx-elements.parseA.txt +++ b/test/fixtures/tsx/corpus/jsx-elements.parseA.txt @@ -6,6 +6,7 @@ (JsxElement (JsxOpeningElement (Identifier) + (Empty) (JsxExpression (Call (Identifier) @@ -25,7 +26,8 @@ (JsxOpeningElement (NestedIdentifier (Identifier) - (Identifier))) + (Identifier)) + (Empty)) (JsxClosingElement (NestedIdentifier (Identifier) diff --git a/test/fixtures/tsx/corpus/jsx-elements.parseB.txt b/test/fixtures/tsx/corpus/jsx-elements.parseB.txt index 32aee1e26..6e1a3a3ac 100644 --- a/test/fixtures/tsx/corpus/jsx-elements.parseB.txt +++ b/test/fixtures/tsx/corpus/jsx-elements.parseB.txt @@ -6,6 +6,7 @@ (JsxElement (JsxOpeningElement (Identifier) + (Empty) (JsxAttribute (Identifier) (JsxExpression