-
Notifications
You must be signed in to change notification settings - Fork 131
Added Parser AST format specification. #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| JSX extensions to Mozilla AST Format | ||
| ==================================== | ||
|
|
||
| JSX extends ECMAScript [Mozilla AST format](https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/Parser_API) with following node types: | ||
|
|
||
| JSX Names | ||
| --------- | ||
|
|
||
| __JSX Identifier subtype__ | ||
|
|
||
| ``` | ||
| interface XJSIdentifier <: Identifier { | ||
| type: "XJSIdentifier"; | ||
| } | ||
| ``` | ||
|
|
||
| __Namespaced names__ | ||
|
|
||
| Property-like namespace syntax (tag names only): | ||
|
|
||
| ``` | ||
| interface XJSMemberExpression <: Expression, Pattern { | ||
| type: "XJSMemberExpression"; | ||
| object: XJSMemberExpression | XJSIdentifier, | ||
| property: XJSIdentifier | ||
| } | ||
| ``` | ||
|
|
||
| XML-based namespace syntax: | ||
|
|
||
| ``` | ||
| interface XJSNamespacedName <: Expression, Pattern { | ||
| type: "XJSNamespacedName"; | ||
| namespace: XJSIdentifier, | ||
| name: XJSIdentifier | ||
| } | ||
| ``` | ||
|
|
||
| JSX Expression Container | ||
| ------------------------ | ||
|
|
||
| JSX adds empty "expression" type in order to allow comments in JSX text: | ||
|
|
||
| ``` | ||
| interface XJSEmptyExpression <: Node { | ||
| type: "XJSEmptyExpression" | ||
| } | ||
| ``` | ||
|
|
||
| Any expression used as attribute value or inside JSX text should is wrapped into expression container: | ||
|
|
||
| ``` | ||
| interface XJSExpressionContainer <: Node { | ||
| type: "XJSExpressionContainer", | ||
| expression: Expression | XJSEmptyExpression; | ||
| } | ||
| ``` | ||
|
|
||
| JSX Boundary Tags | ||
| ----------------- | ||
|
|
||
| Any JSX element is bounded by tags — either self-closing or both opening and closing elements: | ||
|
|
||
| ``` | ||
| interface XJSBoundaryElement <: Node { | ||
| name: XJSIdentifier | XJSMemberExpression | XJSNamespacedName; | ||
| } | ||
|
|
||
| interface XJSOpeningElement <: XJSBoundaryElement { | ||
| type: "XJSOpeningElement", | ||
| attributes: [ XJSAttribute | XJSSpreadAttribute ], | ||
| selfClosing: boolean; | ||
| } | ||
|
|
||
| interface XJSClosingElement <: XJSBoundaryElement { | ||
| type: "XJSClosingElement" | ||
| } | ||
| ``` | ||
|
|
||
| JSX Attributes | ||
| -------------- | ||
|
|
||
| Opening element ("tag") may contain attributes: | ||
|
|
||
| ``` | ||
| interface XJSAttribute <: Node { | ||
| type: "XJSAttribute", | ||
| name: XJSIdentifier | XJSNamespacedName, | ||
| value: Literal | XJSExpressionContainer | XJSElement | null | ||
| } | ||
|
|
||
| // This is already used by ES6 parsers, but not included | ||
| // in Mozilla's spec yet. | ||
| interface SpreadElement <: Pattern { | ||
| type: "SpreadElement"; | ||
| argument: Expression; | ||
| } | ||
|
|
||
| interface XJSSpreadAttribute <: SpreadElement { | ||
| type: "XJSSpreadAttribute"; | ||
| } | ||
| ``` | ||
|
|
||
| JSX Element | ||
| ----------- | ||
|
|
||
| Finally, JSX element itself consists of opening element, list of children and optional closing element: | ||
|
|
||
| ``` | ||
| interface XJSElement <: Expression { | ||
| type: "XJSElement", | ||
| openingElement: XJSOpeningElement, | ||
| children: [ Literal | XJSExpressionContainer | XJSElement ], | ||
| closingElement: XJSClosingElement | null | ||
| } | ||
| ``` | ||
|
|
||
| Tools that work with JSX AST | ||
| ---------------------------- | ||
|
|
||
| * Parsers: | ||
| - [acorn-jsx](https://github.com/RReverser/acorn-jsx): A fork of acorn. | ||
| - [esprima-fb](https://github.com/facebook/esprima): A fork of esprima. | ||
| * Traversal: [estraverse-fb](https://github.com/RReverser/estraverse-fb). | ||
| * Node creation and declarative traversal: [ast-types](https://github.com/benjamn/ast-types) | ||
| * Transpiling to ECMAScript AST: [jsx-transpiler](https://github.com/RReverser/jsx-transpiler) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're going to need license for this document. The simplest would be to just copy the lines from README.md.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, will do.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
|
||
| License | ||
| ------- | ||
|
|
||
| Copyright (c) 2014, Facebook, Inc. | ||
| All rights reserved. | ||
|
|
||
| This work is licensed under a [Creative Commons Attribution 4.0 | ||
| International License](http://creativecommons.org/licenses/by/4.0/). | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I remember @jeffmo having objections to referring to this as "namespacing" since it's a poor way to implement actual namespaces. I'm not sure if these should be conflated but also not sure of how else to refer to them.
This is fine for now but we might want to avoid conflating this in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Entire JS world uses objects + property syntax for namespacing, so I'm not sure why it would be problem to name it so. And, as you said, every implementor can choose to implement any subset of syntax, so I just grouped them as "namespace syntax options" but explicitly described differences between them.
However, if you have any idea how to define them more properly, you're welcome to change :)