Skip to content

Commit

Permalink
code refactoring and documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Dec 31, 2015
1 parent 13b8144 commit 1b5bc15
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 36 deletions.
50 changes: 26 additions & 24 deletions lib/parser.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,15 @@
'use strict';

var uglify = require('uglify-js');
var estraverse = require('estraverse');
var esprima = require('esprima');

///////////////////////////////////////////////////////////
// For each AST parser here:
// The same output is expected from any AST parser here.
//
// Parses the code and returns a dictionary of relative
// coordinates, as `line index`: [{start, end},...], where
// {start, end} are the inclusive indexes of regEx content.
///////////////////////////////////////////////////////////

function parseThroughUglifyJS(code) {
var parsedCode = uglify.parse(code);
var data = {};
parsedCode.walk(new uglify.TreeWalker(function (obj) {
if (obj instanceof uglify.AST_RegExp) {
var reg = obj.end;
var name = reg.line - 1;
if (!data[name]) {
data[name] = [];
}
data[name].push({
start: reg.col + 1,
end: reg.endcol - 2
});
}
}));
return data;
}
var esprima = require('esprima');
var estraverse = require('estraverse');

function parseThroughEsprima(code) {
var parsedCode = esprima.parse(code, {
Expand All @@ -54,7 +34,29 @@ function parseThroughEsprima(code) {
return data;
}

/*
var uglify = require('uglify-js');
function parseThroughUglifyJS(code) {
var parsedCode = uglify.parse(code);
var data = {};
parsedCode.walk(new uglify.TreeWalker(function (obj) {
if (obj instanceof uglify.AST_RegExp) {
var reg = obj.end;
var name = reg.line - 1;
if (!data[name]) {
data[name] = [];
}
data[name].push({
start: reg.col + 1,
end: reg.endcol - 2
});
}
}));
return data;
}
*/

module.exports = function (code) {
//return parseThroughUglifyJS(code);
return parseThroughEsprima(code);
};
25 changes: 16 additions & 9 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ function regExAbsolute(code, relativeDict) {
return result;
}

// does linear search against array of absolute indexes;
///////////////////////////////////////////////////////////////////////
// Executes a linear search through an array of absolute regEx indexes,
// to find whether the passed index is inside one of the regEx entries.
function indexInRegEx(idx, regExData) {
var low = 0;
var high = regExData.length - 1;
var mid;
var mid, low = 0, high = regExData.length - 1;
while (low <= high) {
mid = Math.round((low + high) / 2);
var a = regExData[mid];
Expand All @@ -46,12 +46,16 @@ function indexInRegEx(idx, regExData) {
return false;
}

// If expression is found, returns {idx, lines};
// When expression is not found, returns `null`.
//////////////////////////////////////////////////////
// Searches for the next occurrence of an expression,
// while also counting the line breaks encountered.
//
// - if expression is found, it returns {idx, lines};
// - if expression is not found, it returns null.
function indexOf(source, expression, startIdx) {
var expIdx = source.indexOf(expression, startIdx);
if (expIdx < 0) {
return null;
return null; // not found;
}
var idx = expIdx - 1, count = 0, lb;
do {
Expand All @@ -64,11 +68,14 @@ function indexOf(source, expression, startIdx) {
}
} while (idx > startIdx && lb >= 0);
return {
idx: expIdx,
lines: count
idx: expIdx, // index in the source where the expression was found;
lines: count // number of line breaks found before the expression;
};
}

////////////////////////////////////////////////
// Checks the text for being HTML, by verifying
// whether `<` is the first non-empty symbol.
function isHtml(text) {
var s, idx = 0;
do {
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@
"npm": ">=1.4"
},
"dependencies": {
"esprima": "^2.7.1",
"estraverse": "^4.1.1",
"uglify-js": "2.6"
"esprima": "2.7",
"estraverse": "4.1"
},
"devDependencies": {
"coveralls": "2.x",
Expand Down

0 comments on commit 1b5bc15

Please sign in to comment.