Skip to content

Commit

Permalink
Add NodeUtils.isBefore
Browse files Browse the repository at this point in the history
  • Loading branch information
danielstjules committed Mar 28, 2017
1 parent f5b854d commit 04ff6e4
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
8 changes: 3 additions & 5 deletions lib/match.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,9 @@ class Match {
return nodeArrays.map((nodes) => {
var filename = nodes[0].loc.filename;

var start = nodes.map(node => node.loc.start).reduce((res, curr) => {
var before = (curr.line === res.line && curr.column < res.column) ||
curr.line < res.line;
return before ? curr : res;
});
var start = nodes.reduce((res, curr) => {
return NodeUtils.isBefore(curr, res) ? curr : res;
}).loc.start;

// The end line requires more careful approximation so as not to
// accidentally include a large number of irrelevant src lines
Expand Down
16 changes: 16 additions & 0 deletions lib/nodeUtils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var stable = require('stable');

/**
* Cache for getChildren, holding the keys to traverse for a given Node type.
*/
Expand Down Expand Up @@ -109,6 +111,20 @@ class NodeUtils {
return res;
}

/**
* Returns whether or not the first node appears before the second, by
* comparing both their starting lines and columns.
*
* @param {object} a
* @param {object} b
* @returns {bool}
*/
static isBefore(a, b) {
a = a.loc.start;
b = b.loc.start;
return a.line < b.line || (a.line === b.line && a.column < b.column);
}

/**
* Returns whether or not the nodes are part of an ES6 module import.
*
Expand Down
38 changes: 38 additions & 0 deletions spec/nodeUtilsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,44 @@ describe('NodeUtils', function() {
});
});

describe('isBefore', function() {
describe('given nodes with different line numbers', function() {
it('returns true if the first node has a lower line number', function() {
var res = NodeUtils.isBefore(
{loc: {start: {line: 0, column: 0}}},
{loc: {start: {line: 1, column: 0}}}
);
expect(res).to.be(true);
});

it('returns false if the first node has a higher numbered line', function() {
var res = NodeUtils.isBefore(
{loc: {start: {line: 1, column: 0}}},
{loc: {start: {line: 0, column: 0}}}
);
expect(res).to.be(false);
});
});

describe('given nodes with the same line number', function() {
it('returns true if the first node has a lower column number', function() {
var res = NodeUtils.isBefore(
{loc: {start: {line: 0, column: 0}}},
{loc: {start: {line: 0, column: 1}}}
);
expect(res).to.be(true);
});

it('returns false if the first node has a higher column number', function() {
var res = NodeUtils.isBefore(
{loc: {start: {line: 0, column: 1}}},
{loc: {start: {line: 0, column: 0}}}
);
expect(res).to.be(false);
});
});
});

describe('isES6ModuleImport', function() {
it('returns true for an import declaration', function() {
// ImportDeclaration
Expand Down

0 comments on commit 04ff6e4

Please sign in to comment.