Skip to content

Commit

Permalink
Ignore empty JSXText nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
danielstjules committed Mar 28, 2017
1 parent 119e32d commit 77da91f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 60 deletions.
10 changes: 7 additions & 3 deletions lib/nodeUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var stable = require('stable');
/**
* Cache for getChildren, holding the keys to traverse for a given Node type.
* Some JSX node types are hardcoded to ensure correct property traversal order.
* For example, this ensures that a JSXElement's children is traversed prior
* For example, this ensures that a JSXElement's children are traversed prior
* to the closing element.
*/
var childKeys = {
Expand Down Expand Up @@ -104,14 +104,18 @@ class NodeUtils {
});
}

var ignoreNulls = (nodes) => nodes.filter(x => x);
// Ignore null values, as well as JSText nodes incorrectly generated
// by babylon that contain only newlines and spaces
var filterIgnored = (nodes) => nodes.filter(node => {
return node && (node.type !== 'JSXText' || node.value.trim());
});

childKeys[node.type].forEach((key) => {
var val = node[key];
if (val && val.type) {
res.push(val);
} else if (val instanceof Array) {
res = res.concat(ignoreNulls(val));
res = res.concat(filterIgnored(val));
}
});

Expand Down
48 changes: 0 additions & 48 deletions spec/fixtures/jsxLong.js

This file was deleted.

19 changes: 19 additions & 0 deletions spec/fixtures/jsxNesting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<View>
<Animatable.View ref='content' animation='fadeInUp' duration={2000} ease='ease-in'>
<LinearGradient
start={{x: 0.0, y: 0.0}} end={{x: 0.0, y: 1.0}}
locations={[0, 1]}
colors={['#074595', '#6589A4']}
style={Styles.signInContainer}>
</LinearGradient>
</Animatable.View>

<Animatable.View ref='container' style={Styles.container} easing='ease-in'>
<LinearGradient
start={{x: 0.0, y: 0.0}} end={{x: 0.0, y: 1.0}}
locations={[0, 1]}
colors={['#074595', '#6589A4']}
style={Styles.blurOverlay}>
</LinearGradient>
</Animatable.View>
</View>
24 changes: 15 additions & 9 deletions spec/nodeUtilsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,6 @@ describe('NodeUtils', function() {
});

describe('getChildren', function() {
it('ignores null children', function() {
var parent = helpers.parse(fixtures.nullChildren)[1].expression.left;
// parent.elements is an array with a leading null that should be ignored,
// followed by an identifier
var res = NodeUtils.getChildren(parent);
expect(res).to.have.length(1);
expect(res[0].type).to.be('Identifier');
});

it('returns the children of a Node', function() {
var parent = helpers.parse(fixtures.intersection)[0];
var res = NodeUtils.getChildren(parent);
Expand All @@ -76,6 +67,21 @@ describe('NodeUtils', function() {
'BlockStatement'
])
});

it('ignores null children', function() {
var parent = helpers.parse(fixtures.nullChildren)[1].expression.left;
// parent.elements is an array with a leading null that should be ignored,
// followed by an identifier
var res = NodeUtils.getChildren(parent);
expect(res).to.have.length(1);
expect(res[0].type).to.be('Identifier');
});

it('ignores empty JSXText nodes', function() {
var parent = helpers.parse(fixtures.jsxNesting)[0].expression;
var res = NodeUtils.getChildren(parent);
res.forEach(node => expect(node.type).not.to.be('JSXText'));
});
});

describe('isBefore', function() {
Expand Down

0 comments on commit 77da91f

Please sign in to comment.