Skip to content

Commit

Permalink
JSX fragments
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtemGovorov authored and RReverser committed Nov 11, 2017
1 parent 924f9ea commit 1a71881
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 1 deletion.
7 changes: 6 additions & 1 deletion inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ module.exports = function(acorn) {
// Transforms JSX element name to string.

function getQualifiedJSXName(object) {
if (!object)
return object;

if (object.type === 'JSXIdentifier')
return object.name;

Expand Down Expand Up @@ -209,6 +212,8 @@ module.exports = function(acorn) {
// or single identifier.

pp.jsx_parseElementName = function() {
if (this.type === tt.jsxTagEnd)
return '';
var startPos = this.start, startLoc = this.startLoc;
var node = this.jsx_parseNamespacedName();
if (this.type === tt.dot && node.type === 'JSXNamespacedName' && !this.options.plugins.jsx.allowNamespacedObjects) {
Expand Down Expand Up @@ -348,7 +353,7 @@ module.exports = function(acorn) {
if (this.type === tt.relational && this.value === "<") {
this.raise(this.start, "Adjacent JSX elements must be wrapped in an enclosing tag");
}
return this.finishNode(node, 'JSXElement');
return this.finishNode(node, openingElement.name ? 'JSXElement' : 'JSXFragment');
};

// Parse JSX text
Expand Down
154 changes: 154 additions & 0 deletions test/tests-jsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -3183,6 +3183,160 @@ var fbTestFixture = {
}
]
}
},

'<><div></div></>': {
type: 'ExpressionStatement',
start: 0,
end: 16,
loc: {
start: {
line: 1,
column: 0
},
end: {
line: 1,
column: 16
}
},
range: [0, 16],
expression: {
type: 'JSXFragment',
start: 0,
end: 16,
loc: {
start: {
line: 1,
column: 0
},
end: {
line: 1,
column: 16
}
},
range: [0, 16],
openingElement: {
type: 'JSXOpeningElement',
start: 0,
end: 2,
loc: {
start: {
line: 1,
column: 0
},
end: {
line: 1,
column: 2
}
},
range: [0, 2],
attributes: [],
name: '',
selfClosing: false
},
closingElement: {
type: 'JSXClosingElement',
start: 13,
end: 16,
loc: {
start: {
line: 1,
column: 13
},
end: {
line: 1,
column: 16
}
},
range: [13, 16],
name: ''
},
children: [{
type: 'JSXElement',
start: 2,
end: 13,
loc: {
start: {
line: 1,
column: 2
},
end: {
line: 1,
column: 13
}
},
range: [2, 13],
openingElement: {
type: 'JSXOpeningElement',
start: 2,
end: 7,
loc: {
start: {
line: 1,
column: 2
},
end: {
line: 1,
column: 7
}
},
range: [2, 7],
attributes: [],
name: {
type: 'JSXIdentifier',
start: 3,
end: 6,
loc: {
start: {
line: 1,
column: 3
},
end: {
line: 1,
column: 6
}
},
range: [3, 6],
name: 'div'
},
selfClosing: false
},
closingElement: {
type: 'JSXClosingElement',
start: 7,
end: 13,
loc: {
start: {
line: 1,
column: 7
},
end: {
line: 1,
column: 13
}
},
range: [7, 13],
name: {
type: 'JSXIdentifier',
start: 9,
end: 12,
loc: {
start: {
line: 1,
column: 9
},
end: {
line: 1,
column: 12
}
},
range: [9, 12],
name: 'div'
}
},
children: []
}]
}
}
},
'Regression': {
Expand Down

0 comments on commit 1a71881

Please sign in to comment.