Skip to content

Commit

Permalink
Add attribute expression syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
amiller-gh committed Jun 10, 2017
1 parent dc87d99 commit de84728
Show file tree
Hide file tree
Showing 5 changed files with 763 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/plugins/jsx/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,17 +208,30 @@ export default (superClass: Class<Parser>): Class<Parser> => class extends super
return this.finishNode(node, "JSXIdentifier");
}

jsxParseMemberExpression(): N.JSXIdentifier | N.JSXMemberExpression {
const startPos = this.state.start;
const startLoc = this.state.startLoc;
let node = this.jsxParseIdentifier();
while (this.eat(tt.dot)) {
const newNode = this.startNodeAt(startPos, startLoc);
newNode.object = node;
newNode.property = this.jsxParseIdentifier();
node = this.finishNode(newNode, "JSXMemberExpression");
}
return node;
}

// Parse namespaced identifier.

jsxParseNamespacedName(): N.JSXNamespacedName {
const startPos = this.state.start;
const startLoc = this.state.startLoc;
const name = this.jsxParseIdentifier();
const name = this.jsxParseMemberExpression();
if (!this.eat(tt.colon)) return name;

const node = this.startNodeAt(startPos, startLoc);
node.namespace = name;
node.name = this.jsxParseIdentifier();
node.name = this.jsxParseMemberExpression();
return this.finishNode(node, "JSXNamespacedName");
}

Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/jsx/basic/attribute-expression/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<div foo.bar/>;
<div foo.bar="baz"></div>
341 changes: 341 additions & 0 deletions test/fixtures/jsx/basic/attribute-expression/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,341 @@
{
"type": "File",
"start": 0,
"end": 41,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 25
}
},
"program": {
"type": "Program",
"start": 0,
"end": 41,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 25
}
},
"sourceType": "script",
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 15,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 15
}
},
"expression": {
"type": "JSXElement",
"start": 0,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 14
}
},
"openingElement": {
"type": "JSXOpeningElement",
"start": 0,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 14
}
},
"attributes": [
{
"type": "JSXAttribute",
"start": 5,
"end": 12,
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 12
}
},
"name": {
"type": "JSXMemberExpression",
"start": 5,
"end": 12,
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 12
}
},
"object": {
"type": "JSXIdentifier",
"start": 5,
"end": 8,
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 8
}
},
"name": "foo"
},
"property": {
"type": "JSXIdentifier",
"start": 9,
"end": 12,
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 12
}
},
"name": "bar"
}
},
"value": null
}
],
"name": {
"type": "JSXIdentifier",
"start": 1,
"end": 4,
"loc": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 4
}
},
"name": "div"
},
"selfClosing": true
},
"closingElement": null,
"children": []
}
},
{
"type": "ExpressionStatement",
"start": 16,
"end": 41,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 25
}
},
"expression": {
"type": "JSXElement",
"start": 16,
"end": 41,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 25
}
},
"openingElement": {
"type": "JSXOpeningElement",
"start": 16,
"end": 35,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 19
}
},
"attributes": [
{
"type": "JSXAttribute",
"start": 21,
"end": 34,
"loc": {
"start": {
"line": 2,
"column": 5
},
"end": {
"line": 2,
"column": 18
}
},
"name": {
"type": "JSXMemberExpression",
"start": 21,
"end": 28,
"loc": {
"start": {
"line": 2,
"column": 5
},
"end": {
"line": 2,
"column": 12
}
},
"object": {
"type": "JSXIdentifier",
"start": 21,
"end": 24,
"loc": {
"start": {
"line": 2,
"column": 5
},
"end": {
"line": 2,
"column": 8
}
},
"name": "foo"
},
"property": {
"type": "JSXIdentifier",
"start": 25,
"end": 28,
"loc": {
"start": {
"line": 2,
"column": 9
},
"end": {
"line": 2,
"column": 12
}
},
"name": "bar"
}
},
"value": {
"type": "StringLiteral",
"start": 29,
"end": 34,
"loc": {
"start": {
"line": 2,
"column": 13
},
"end": {
"line": 2,
"column": 18
}
},
"extra": {
"rawValue": "baz",
"raw": "\"baz\""
},
"value": "baz"
}
}
],
"name": {
"type": "JSXIdentifier",
"start": 17,
"end": 20,
"loc": {
"start": {
"line": 2,
"column": 1
},
"end": {
"line": 2,
"column": 4
}
},
"name": "div"
},
"selfClosing": false
},
"closingElement": {
"type": "JSXClosingElement",
"start": 35,
"end": 41,
"loc": {
"start": {
"line": 2,
"column": 19
},
"end": {
"line": 2,
"column": 25
}
},
"name": {
"type": "JSXIdentifier",
"start": 37,
"end": 40,
"loc": {
"start": {
"line": 2,
"column": 21
},
"end": {
"line": 2,
"column": 24
}
},
"name": "div"
}
},
"children": []
}
}
],
"directives": []
}
}
2 changes: 2 additions & 0 deletions test/fixtures/jsx/basic/namespace-attribute/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<div foo:bar/>;
<div foo.bar:biz.baz="value"></div>
Loading

0 comments on commit de84728

Please sign in to comment.