Skip to content

Commit

Permalink
Fall back to regex if babylon fails
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmfoley committed Sep 24, 2016
1 parent 86f5965 commit 9a770c1
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
36 changes: 34 additions & 2 deletions lib/analyzeFiles/findRequires/javascriptRequireFinder.js
Expand Up @@ -3,6 +3,37 @@ var requirePathFilter = require('./requirePathFilter');

module.exports = function(contents, filename) {
contents = handleShebang(contents);
try {
return babylonFinder(contents);
}
catch (err) {
//fall back to stupid/fragile regex parsing
return regexFinder(contents);
}
};

function regexFinder(contents) {
var lines = contents.split('\n');
var requires = [];
var importRegex = /(import .+? from|require\s*\()\s*['"`]/g;
lines.map(function(line, i) {
var result = importRegex.exec(line);
if (!result) return;
var location = result.index + result[0].length;
var requirePath = line.substring(location).split(/['"`]/)[0];
requires.push({
path: requirePath,
loc: {
line: i,
start: location,
length: requirePath.length
}
});
});
return requires;
}

function babylonFinder(contents) {

// kitchen-sink approach, since we just want to find requires/imports/etc.
var bbl = babylon.parse(contents, {sourceType: 'module', plugins: [
Expand All @@ -24,12 +55,13 @@ module.exports = function(contents, filename) {
var requires = [];
scan(bbl.tokens, requires);
return requires;
};
}

function scan(tokens, requires, index) {
for(var i = 0; i < tokens.length; i++) {
var token = tokens[i];
if (token.type.keyword === 'import' || (token.type.label === 'name' && token.value === 'require')) {
var type = token.type;
if (type.keyword === 'import' || (type.label === 'name' && token.value === 'require')) {
while ( tokens[i] && tokens[i].type && tokens[i].type.label !== 'string' && i < tokens.length) { i++; }

addIfMatch(requires, tokens[i]);
Expand Down
23 changes: 23 additions & 0 deletions test/findRequires_spec.js
Expand Up @@ -51,6 +51,8 @@ export const fetchLookup = query =>
expect(requires[0].path).to.equal('./foo');
});



});

describe('with javascript', () => {
Expand All @@ -65,6 +67,27 @@ export const fetchLookup = query =>
expect(requires[0].path).to.equal('./foo');
});

it('handles import in a file that babylon can\'t fully parse', () => {
const code = `import * as x from './y';
let foo;
foo = foo || () => {}; //babylon can't handle this for some reason
`;
const requires = findRequires('js', code);
expect(requires.length).to.eql(1);
expect(requires[0].path).to.equal('./y');
});

it('handles require in a file that babylon can\'t fully parse', () => {
const code = `const x = require('./y');
let foo;
foo = foo || () => {}; //babylon can't handle this for some reason
`;
const requires = findRequires('js', code);
expect(requires.length).to.eql(1);
expect(requires[0].path).to.equal('./y');
});


it('handles a shebanged javascript file', () => {
const code = "#! /usr/bin/env node\nvar foo = require( './foo' );\n";
const requires = findRequires('js', code);
Expand Down

0 comments on commit 9a770c1

Please sign in to comment.