From bbee1d839ccec806020bb9b12d0c0a9e1c5a7787 Mon Sep 17 00:00:00 2001 From: joshvera Date: Wed, 14 Feb 2018 15:48:00 -0500 Subject: [PATCH] Update JSX terms --- src/Language/TypeScript/Assignment.hs | 40 +++++++++++++++++++++------ src/Language/TypeScript/Syntax.hs | 21 ++++++++++++++ 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/src/Language/TypeScript/Assignment.hs b/src/Language/TypeScript/Assignment.hs index 89d0c7465..eab7404cd 100644 --- a/src/Language/TypeScript/Assignment.hs +++ b/src/Language/TypeScript/Assignment.hs @@ -144,6 +144,9 @@ type Syntax = '[ , TypeScript.Syntax.JsxClosingElement , TypeScript.Syntax.JsxExpression , TypeScript.Syntax.JsxAttribute + , TypeScript.Syntax.JsxMemberExpression + , TypeScript.Syntax.JsxFragment + , TypeScript.Syntax.JsxNamespaceName , TypeScript.Syntax.OptionalParameter , TypeScript.Syntax.RequiredParameter , TypeScript.Syntax.RestParameter @@ -196,8 +199,8 @@ expression = handleError everything super, object, array, - jsxElement, - jsxSelfClosingElement, + jsxElement', + jsxFragment, class', anonymousClass, function, @@ -352,26 +355,47 @@ object = makeTerm <$> (symbol Object <|> symbol ObjectPattern) <*> children (Lit array :: Assignment array = makeTerm <$> (symbol Array <|> symbol ArrayPattern) <*> children (Literal.Array <$> manyTerm (expression <|> spreadElement)) +jsxElement' :: Assignment +jsxElement' = choice [ jsxElement, jsxSelfClosingElement ] + jsxElement :: Assignment -jsxElement = makeTerm <$> symbol Grammar.JsxElement <*> children (TypeScript.Syntax.JsxElement <$> term jsxOpeningElement' <*> manyTerm (jsxElement <|> jsxSelfClosingElement <|> jsxExpression' <|> jsxText) <*> term jsxClosingElement') +jsxElement = makeTerm <$> symbol Grammar.JsxElement <*> children (TypeScript.Syntax.JsxElement <$> term jsxOpeningElement' <*> manyTerm jsxChild <*> term jsxClosingElement') + +jsxFragment :: Assignment +jsxFragment = makeTerm <$> symbol Grammar.JsxFragment <*> children (TypeScript.Syntax.JsxFragment <$> manyTerm jsxChild) + +jsxChild :: Assignment +jsxChild = choice [ jsxElement', jsxExpression', jsxText ] jsxSelfClosingElement :: Assignment -jsxSelfClosingElement = makeTerm <$> symbol Grammar.JsxSelfClosingElement <*> children (TypeScript.Syntax.JsxSelfClosingElement <$> term (identifier <|> nestedIdentifier) <*> manyTerm (jsxAttribute <|> jsxExpression')) +jsxSelfClosingElement = makeTerm <$> symbol Grammar.JsxSelfClosingElement <*> children (TypeScript.Syntax.JsxSelfClosingElement <$> term jsxElementName <*> manyTerm jsxAttribute') + +jsxAttribute' = jsxAttribute <|> jsxExpression' jsxOpeningElement' :: Assignment -jsxOpeningElement' = makeTerm <$> symbol Grammar.JsxOpeningElement <*> children (TypeScript.Syntax.JsxOpeningElement <$> term (identifier <|> nestedIdentifier) <*> manyTerm (jsxAttribute <|> jsxExpression')) +jsxOpeningElement' = makeTerm <$> symbol Grammar.JsxOpeningElement <*> children (TypeScript.Syntax.JsxOpeningElement <$> term jsxElementName <*> manyTerm jsxAttribute') + +jsxElementName :: Assignment +jsxElementName = choice [ identifier, jsxMemberExpression, jsxNamespaceName ] + +jsxNamespaceName :: Assignment +jsxNamespaceName = makeTerm <$> symbol Grammar.JsxNamespaceName <*> children (TypeScript.Syntax.JsxNamespaceName <$> identifier <*> identifier) + +jsxMemberExpression :: Assignment +jsxMemberExpression = makeTerm <$> symbol Grammar.JsxMemberExpression <*> children (TypeScript.Syntax.JsxMemberExpression <$> manyTerm identifier) jsxExpression' :: Assignment -jsxExpression' = makeTerm <$> symbol Grammar.JsxExpression <*> children (TypeScript.Syntax.JsxExpression <$> term (expressions <|> spreadElement)) +jsxExpression' = makeTerm <$> symbol Grammar.JsxExpression <*> children (TypeScript.Syntax.JsxExpression <$> term (expressions <|> spreadElement <|> emptyTerm)) jsxText :: Assignment jsxText = makeTerm <$> symbol Grammar.JsxText <*> (TypeScript.Syntax.JsxText <$> source) jsxClosingElement' :: Assignment -jsxClosingElement' = makeTerm <$> symbol Grammar.JsxClosingElement <*> children (TypeScript.Syntax.JsxClosingElement <$> term (identifier <|> nestedIdentifier)) +jsxClosingElement' = makeTerm <$> symbol Grammar.JsxClosingElement <*> children (TypeScript.Syntax.JsxClosingElement <$> term jsxElementName) jsxAttribute :: Assignment -jsxAttribute = makeTerm <$> symbol Grammar.JsxAttribute <*> children (TypeScript.Syntax.JsxAttribute <$> term propertyIdentifier <*> (term (number <|> string <|> jsxExpression') <|> emptyTerm)) +jsxAttribute = makeTerm <$> symbol Grammar.JsxAttribute <*> children (TypeScript.Syntax.JsxAttribute <$> term (propertyIdentifier <|> jsxNamespaceName) <*> (term jsxAttributeValue <|> emptyTerm)) + where jsxAttributeValue = choice [ string, jsxExpression', jsxElement', jsxFragment ] propertyIdentifier :: Assignment propertyIdentifier = makeTerm <$> symbol PropertyIdentifier <*> (Syntax.Identifier <$> source) diff --git a/src/Language/TypeScript/Syntax.hs b/src/Language/TypeScript/Syntax.hs index aa4103a8e..dc16a9fe3 100644 --- a/src/Language/TypeScript/Syntax.hs +++ b/src/Language/TypeScript/Syntax.hs @@ -493,3 +493,24 @@ data RestParameter a = RestParameter { _restParameterContext :: ![a], _restParam instance Eq1 RestParameter where liftEq = genericLiftEq instance Ord1 RestParameter where liftCompare = genericLiftCompare instance Show1 RestParameter where liftShowsPrec = genericLiftShowsPrec + +data JsxFragment a = JsxFragment [a] + deriving (Diffable, Eq, Foldable, Functor, GAlign, Generic1, Mergeable, Ord, Show, Traversable) + +instance Eq1 JsxFragment where liftEq = genericLiftEq +instance Ord1 JsxFragment where liftCompare = genericLiftCompare +instance Show1 JsxFragment where liftShowsPrec = genericLiftShowsPrec + +data JsxNamespaceName a = JsxNamespaceName a a + deriving (Diffable, Eq, Foldable, Functor, GAlign, Generic1, Mergeable, Ord, Show, Traversable) + +instance Eq1 JsxNamespaceName where liftEq = genericLiftEq +instance Ord1 JsxNamespaceName where liftCompare = genericLiftCompare +instance Show1 JsxNamespaceName where liftShowsPrec = genericLiftShowsPrec + +data JsxMemberExpression a = JsxMemberExpression [a] + deriving (Diffable, Eq, Foldable, Functor, GAlign, Generic1, Mergeable, Ord, Show, Traversable) + +instance Eq1 JsxMemberExpression where liftEq = genericLiftEq +instance Ord1 JsxMemberExpression where liftCompare = genericLiftCompare +instance Show1 JsxMemberExpression where liftShowsPrec = genericLiftShowsPrec