Skip to content
This repository has been archived by the owner on Jan 19, 2019. It is now read-only.

Commit

Permalink
Fix: Add name to JSXIdentifier when converting ThisKeyword (fixes #307)…
Browse files Browse the repository at this point in the history
… (#310)
  • Loading branch information
soda0289 authored and JamesHenry committed Jun 4, 2017
1 parent 519907e commit 2ad791b
Show file tree
Hide file tree
Showing 5 changed files with 729 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,7 @@ module.exports = function convert(config) {
// Assign the appropriate types
tagNameToken.object.type = (isNestedMemberExpression) ? AST_NODE_TYPES.JSXMemberExpression : AST_NODE_TYPES.JSXIdentifier;
tagNameToken.property.type = AST_NODE_TYPES.JSXIdentifier;

} else {

tagNameToken.name = tagNameToken.value;
}

Expand Down Expand Up @@ -1447,6 +1445,9 @@ module.exports = function convert(config) {
property: convertChild(node.name)
};
const isNestedMemberExpression = (node.expression.kind === SyntaxKind.PropertyAccessExpression);
if (node.expression.kind === SyntaxKind.ThisKeyword) {
jsxMemberExpression.object.name = "this";
}

jsxMemberExpression.object.type = (isNestedMemberExpression) ? AST_NODE_TYPES.MemberExpression : AST_NODE_TYPES.JSXIdentifier;
jsxMemberExpression.property.type = AST_NODE_TYPES.JSXIdentifier;
Expand Down
327 changes: 327 additions & 0 deletions tests/fixtures/jsx/member-expression-this.result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,327 @@
module.exports = {
"type": "Program",
"range": [
0,
25
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 25
}
},
"body": [
{
"type": "ExpressionStatement",
"range": [
0,
25
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 25
}
},
"expression": {
"type": "JSXElement",
"range": [
0,
24
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 24
}
},
"openingElement": {
"type": "JSXOpeningElement",
"range": [
0,
24
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 24
}
},
"selfClosing": true,
"name": {
"type": "JSXMemberExpression",
"range": [
1,
21
],
"loc": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 21
}
},
"object": {
"type": "JSXMemberExpression",
"range": [
1,
11
],
"loc": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 11
}
},
"object": {
"type": "JSXIdentifier",
"range": [
1,
5
],
"loc": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 5
}
},
"name": "this"
},
"property": {
"type": "JSXIdentifier",
"range": [
6,
11
],
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 11
}
},
"name": "state"
}
},
"property": {
"type": "JSXIdentifier",
"range": [
12,
21
],
"loc": {
"start": {
"line": 1,
"column": 12
},
"end": {
"line": 1,
"column": 21
}
},
"name": "Component"
}
},
"attributes": []
},
"closingElement": null,
"children": []
}
}
],
"sourceType": "script",
"tokens": [
{
"type": "Punctuator",
"value": "<",
"range": [
0,
1
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 1
}
}
},
{
"type": "Keyword",
"value": "this",
"range": [
1,
5
],
"loc": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 5
}
}
},
{
"type": "Punctuator",
"value": ".",
"range": [
5,
6
],
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 6
}
}
},
{
"type": "JSXIdentifier",
"value": "state",
"range": [
6,
11
],
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 11
}
}
},
{
"type": "Punctuator",
"value": ".",
"range": [
11,
12
],
"loc": {
"start": {
"line": 1,
"column": 11
},
"end": {
"line": 1,
"column": 12
}
}
},
{
"type": "JSXIdentifier",
"value": "Component",
"range": [
12,
21
],
"loc": {
"start": {
"line": 1,
"column": 12
},
"end": {
"line": 1,
"column": 21
}
}
},
{
"type": "Punctuator",
"value": "/",
"range": [
22,
23
],
"loc": {
"start": {
"line": 1,
"column": 22
},
"end": {
"line": 1,
"column": 23
}
}
},
{
"type": "Punctuator",
"value": ">",
"range": [
23,
24
],
"loc": {
"start": {
"line": 1,
"column": 23
},
"end": {
"line": 1,
"column": 24
}
}
},
{
"type": "Punctuator",
"value": ";",
"range": [
24,
25
],
"loc": {
"start": {
"line": 1,
"column": 24
},
"end": {
"line": 1,
"column": 25
}
}
}
]
};
1 change: 1 addition & 0 deletions tests/fixtures/jsx/member-expression-this.src.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<this.state.Component />;
Loading

0 comments on commit 2ad791b

Please sign in to comment.