Comparing version 0.0.13 to 0.0.15
@@ -17,2 +17,4 @@ #!/usr/bin/env node | ||
var LONG_LINE_LENGTH = 120; | ||
optimist.usage("jsgrep [options] pattern file.js [file2.js] [dir]"); | ||
@@ -42,2 +44,14 @@ | ||
optimist.boolean("l").options("l", { | ||
alias: "long-lines", | ||
describe: "Print long (over " + LONG_LINE_LENGTH + " characters long) lines.", | ||
default: false, | ||
}); | ||
optimist.boolean("s").options("s", { | ||
alias: "shebang", | ||
describe: "Strip shebang from input files", | ||
default: true, | ||
}); | ||
function beautifyPath(p) { | ||
@@ -61,2 +75,38 @@ var parts = p.split(path.sep).reverse(); | ||
function colorizeLine(line, steps) { | ||
steps = _.sortBy(steps, "pos"); | ||
var prevPos = 0; | ||
var currVal = 0; | ||
var buf = ""; | ||
_.each(steps, function (step) { | ||
var currPos = step.pos; | ||
var part = line.substr(prevPos, currPos - prevPos); | ||
switch (currVal) { | ||
case 0: | ||
break; | ||
case 1: | ||
part = part.red; | ||
break; | ||
case 2: | ||
part = part.yellow; | ||
break; | ||
case 3: | ||
part = part.green; | ||
break; | ||
default: | ||
part = part.blue; | ||
} | ||
buf += part; | ||
currVal = currVal + step.val; | ||
prevPos = currPos; | ||
}); | ||
return buf; | ||
} | ||
function cli(argv) { | ||
@@ -91,3 +141,3 @@ var options = optimist.parse(argv); | ||
} catch (e) { | ||
console.error("Error: invalid pattern -- " + e.message); | ||
console.error("Error: ".red + "invalid pattern -- " + e.message); | ||
return 1; | ||
@@ -99,3 +149,8 @@ } | ||
walk.sync(file, { "no_return": true }, function (p /*, stat */) { | ||
if (!fs.existsSync(absfile)) { | ||
console.log("Error: ".red + " file not exists -- " + file); | ||
return; | ||
} | ||
walk.sync(absfile, { "no_return": true }, function (p /*, stat */) { | ||
p = path.resolve(p); | ||
@@ -108,4 +163,18 @@ | ||
var contents = fs.readFileSync(p); | ||
var syntax = esprima.parse(contents, { tolerant: true, range: true, loc: true }); | ||
// strip shebang | ||
if (options.shebang) { | ||
contents = contents.toString(); | ||
var m = contents.match(/^#![^\n]*\n/); | ||
if (m) { | ||
contents = contents.substr(m[0].length - 1); | ||
} | ||
} | ||
var syntax; | ||
try { | ||
syntax = esprima.parse(contents, { tolerant: true, range: true, loc: true }); | ||
} catch (e) { | ||
console.log("Error: ".red + "cannot parse " + relpath.bold + " -- " + e.message); | ||
} | ||
var lines; | ||
@@ -115,3 +184,4 @@ | ||
enter: function(node /* , parent */) { | ||
if (pattern(node)) { | ||
var match = pattern(node); | ||
if (match) { | ||
if (!lines) { | ||
@@ -126,21 +196,38 @@ lines = contents.toString().split(/\n/); | ||
if (options.H && options.n) { | ||
prefix = relpath + ":" + lineNumber + ": "; | ||
prefix = relpath + ":" + lineNumber + ":"; | ||
} else if (options.H) { | ||
prefix = relpath + ": "; | ||
prefix = relpath + ":"; | ||
} else if (options.n) { | ||
prefix = lineNumber + ": "; | ||
prefix = lineNumber + ":"; | ||
} | ||
var before = line.substr(0, node.loc.start.column - 1); | ||
var match; | ||
var after; | ||
// Gather steps for colorize | ||
var steps = [{ val: 1, pos: node.loc.start.column }]; | ||
if (node.loc.start.line === node.loc.end.line) { | ||
match = line.substr(node.loc.start.column - 1, node.loc.end.column - node.loc.start.column + 1); | ||
after = line.substr(node.loc.end.column); | ||
} else { | ||
match = line.substr(node.loc.start.column - 1); | ||
after = ""; | ||
steps.push({ val: -1, pos: node.loc.end.column }); | ||
} | ||
console.log(prefix + before + match.red + after); | ||
_.each(match, function (matchNode) { | ||
if (matchNode && matchNode.loc) { | ||
steps.push({ val: 1, pos: matchNode.loc.start.column }); | ||
steps.push({ val: -1, pos: matchNode.loc.end.column }); | ||
} | ||
}); | ||
steps.push({ val: -1, pos: line.length }); | ||
// truncate line if it's too long | ||
if (line.length > LONG_LINE_LENGTH && !options.l) { | ||
var start = Math.max(0, steps[0].pos - 10); | ||
var linePrefix = (start === 0 ? "" : "..." ); | ||
var lineSuffix = (start + LONG_LINE_LENGTH < line.length ? "..." : ""); | ||
_.each(steps, function (s) { | ||
s.pos = s.pos - start + (start === 0 ? 0 : 3); | ||
}); | ||
line = linePrefix + line.substr(start, LONG_LINE_LENGTH) + lineSuffix; | ||
} | ||
// print match | ||
console.log(prefix.bold + " " + colorizeLine(line, steps)); | ||
} | ||
@@ -155,2 +242,2 @@ }, | ||
var ret = cli(process.argv.slice(2)); | ||
process.exit(ret); | ||
process.exit(ret); |
## Release History | ||
- 0.0.15 Updates | ||
- Dependencies updated | ||
- Introduce eslint | ||
- Fix logical expressions: `&&` and `||` | ||
- 0.0.14 Better cli experience | ||
- Strip shebang by default | ||
- Truncate long output lines | ||
- Fancier colorize of jsgrep output | ||
- Catch parse errors and unexisting files | ||
- 0.0.13 nand, nor and ? | ||
@@ -4,0 +13,0 @@ - node capturing |
@@ -52,3 +52,3 @@ /** | ||
var assert = require("assert"); | ||
var sexpr = require("./sexpr.js"); | ||
var sexprParse = require("./sexpr.js").parse; | ||
var levenshtein = require("levenshtein"); | ||
@@ -172,5 +172,5 @@ | ||
rator = rator.substr(1); | ||
var matcher = matcherArray.call(this, rands); | ||
var nonCapturingMatcher = matcherArray.call(this, rands); | ||
return function (node) { | ||
var m = matcher(node); | ||
var m = nonCapturingMatcher(node); | ||
if (m !== undefined) { | ||
@@ -195,2 +195,4 @@ var res = {}; | ||
/* jshint validthis:true */ | ||
/* eslint no-use-before-define: 0 */ | ||
// JsstancaContext is used here, even defined later | ||
@@ -272,3 +274,3 @@ assert(_.isString(sexpr) || _.isNumber(sexpr) || _.isArray(sexpr), | ||
if (!_.has(matchPatterns, pattern)) { | ||
matchPatterns[pattern] = that.matcher(sexpr.parse(pattern)); | ||
matchPatterns[pattern] = that.matcher(sexprParse(pattern)); | ||
} | ||
@@ -300,3 +302,3 @@ | ||
var args = _.toArray(arguments); | ||
args[0] = sexpr.parse(args[0]); | ||
args[0] = sexprParse(args[0]); | ||
return matcher.apply(this, args); | ||
@@ -303,0 +305,0 @@ } |
@@ -84,2 +84,2 @@ "use strict"; | ||
"new": _.partial(callMatcher, false), | ||
}; | ||
}; |
@@ -80,2 +80,2 @@ "use strict"; | ||
"ident": identMatcher, | ||
}; | ||
}; |
@@ -148,2 +148,2 @@ "use strict"; | ||
"undefined": _.partial(literalBuiltinMatcher, "undefined"), | ||
}; | ||
}; |
@@ -8,3 +8,3 @@ "use strict"; | ||
return matcher(node) ? undefined : {}; | ||
} | ||
}; | ||
} | ||
@@ -94,2 +94,2 @@ | ||
"nand": nandMatcher, | ||
}; | ||
}; |
@@ -20,2 +20,2 @@ "use strict"; | ||
"null-node": nullNodeMatcher, | ||
}; | ||
}; |
@@ -30,3 +30,2 @@ "use strict"; | ||
"==", "!=", "===", "!==", | ||
"&&", "||", | ||
"&", "|", "^", | ||
@@ -36,2 +35,6 @@ "instanceof", "in", | ||
var validLogicOperators = [ | ||
"&&", "||", | ||
]; | ||
var validUnaryOperators = [ | ||
@@ -63,2 +66,6 @@ "!", "~", "+", "-", | ||
Also shorthand syntax is supported, `(+ a b)` is the same as `(binary + a b)`. | ||
#### (logical op lhs rhs) | ||
Matches `LogicalExpression`. ie. `&&` and `||` operators. | ||
*/ | ||
@@ -92,2 +99,3 @@ function binaryAssignMatcher(ratorName, validOperators, exprType, operator, lhs, rhs) { | ||
var binaryMatcher = _.partial(binaryAssignMatcher, "binary", validBinaryOperators, "BinaryExpression"); | ||
var logicalMatcher = _.partial(binaryAssignMatcher, "logical", validLogicOperators, "LogicalExpression"); | ||
@@ -163,2 +171,3 @@ /** | ||
exports.binary = binaryMatcher; | ||
exports.logical = logicalMatcher; | ||
exports.unary = unaryMatcher; | ||
@@ -180,2 +189,2 @@ exports.update = _.partial(updateMatcher, undefined); | ||
exports[assignOp] = _.partial(assignMatcher, assignOp); | ||
}); | ||
}); |
@@ -44,2 +44,2 @@ "use strict"; | ||
"throw": throwMatcher, | ||
}; | ||
}; |
@@ -34,2 +34,2 @@ "use strict"; | ||
"ternary": ternaryMatcher, | ||
}; | ||
}; |
@@ -1,2 +0,2 @@ | ||
"use string"; | ||
"use strict"; | ||
@@ -3,0 +3,0 @@ var p = require("packrattle"); |
{ | ||
"name": "jsstana", | ||
"description": "s-expression match patterns for Mozilla Parser AST", | ||
"version": "0.0.13", | ||
"version": "0.0.15", | ||
"homepage": "https://github.com/phadej/jsstana", | ||
@@ -39,3 +39,3 @@ "author": { | ||
"devDependencies": { | ||
"grunt-contrib-jshint": "~0.7.1", | ||
"grunt-contrib-jshint": "~0.8.0", | ||
"grunt-contrib-nodeunit": "~0.2.0", | ||
@@ -46,3 +46,3 @@ "grunt-contrib-watch": "~0.5.3", | ||
"grunt": "~0.4.1", | ||
"escodegen": "0.0.28" | ||
"escodegen": "~1.0.0" | ||
}, | ||
@@ -64,3 +64,3 @@ "keywords": [ | ||
"esprima": "~1.0.4", | ||
"estraverse": "~1.4.0", | ||
"estraverse": "~1.5.0", | ||
"colors": "~0.6.2", | ||
@@ -67,0 +67,0 @@ "levenshtein": "~1.0.2" |
@@ -141,2 +141,6 @@ # jsstana | ||
#### (logical op lhs rhs) | ||
Matches `LogicalExpression`. ie. `&&` and `||` operators. | ||
#### (unary op value) | ||
@@ -215,2 +219,11 @@ | ||
- 0.0.15 Updates | ||
- Dependencies updated | ||
- Introduce eslint | ||
- Fix logical expressions: `&&` and `||` | ||
- 0.0.14 Better cli experience | ||
- Strip shebang by default | ||
- Truncate long output lines | ||
- Fancier colorize of jsgrep output | ||
- Catch parse errors and unexisting files | ||
- 0.0.13 nand, nor and ? | ||
@@ -217,0 +230,0 @@ - node capturing |
@@ -28,2 +28,2 @@ /* global describe:true, it:true */ | ||
}); | ||
}); | ||
}); |
@@ -79,2 +79,2 @@ /* global describe:true, it:true */ | ||
}); | ||
}); | ||
}); |
@@ -97,2 +97,2 @@ /* global describe:true, it:true */ | ||
}); | ||
}); | ||
}); |
@@ -102,2 +102,2 @@ /* global describe:true, it:true */ | ||
}); | ||
}); | ||
}); |
@@ -27,2 +27,2 @@ /* global describe:true, it:true */ | ||
}); | ||
}); | ||
}); |
@@ -26,2 +26,2 @@ /* global describe:true, it:true */ | ||
}); | ||
}); | ||
}); |
@@ -29,2 +29,2 @@ /* global describe:true, it:true */ | ||
}); | ||
}); | ||
}); |
@@ -29,2 +29,2 @@ /* global describe:true, it:true */ | ||
}); | ||
}); | ||
}); |
@@ -436,2 +436,2 @@ /* global describe:true, it:true */ | ||
}); | ||
}); | ||
}); |
@@ -8,3 +8,3 @@ /* global describe:true, it:true */ | ||
var esprima = require("esprima"); | ||
describe("not", function () { | ||
@@ -11,0 +11,0 @@ it("negates the match", function () { |
@@ -71,2 +71,2 @@ /* global describe:true, it:true */ | ||
}); | ||
}); | ||
}); |
@@ -126,2 +126,2 @@ /* global describe:true, it:true */ | ||
}); | ||
}); | ||
}); |
@@ -36,2 +36,2 @@ /* global describe:true, it:true */ | ||
}); | ||
}); | ||
}); |
@@ -92,2 +92,2 @@ /* global describe:true, it:true */ | ||
}); | ||
}); | ||
}); |
@@ -45,2 +45,2 @@ /* global describe:true, it:true */ | ||
}); | ||
}); | ||
}); |
@@ -52,2 +52,2 @@ /* global describe:true, it:true */ | ||
}); | ||
}); | ||
}); |
@@ -54,2 +54,2 @@ /* global describe:true, it:true */ | ||
}); | ||
}); | ||
}); |
@@ -36,2 +36,2 @@ /* global describe:true, it:true */ | ||
}); | ||
}); | ||
}); |
@@ -91,2 +91,2 @@ /* global describe:true, it:true */ | ||
}); | ||
}); | ||
}); |
@@ -66,2 +66,2 @@ /* global describe:true, it:true */ | ||
}); | ||
}); | ||
}); |
@@ -54,4 +54,4 @@ /* global describe:true, it:true */ | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.createMatcher("(expr (= a $0))", function (node) { | ||
return node.type === "ObjectExpression" && node.properties.length === 0 ? {} : undefined; | ||
var matcher = jsstana.createMatcher("(expr (= a $0))", function (subnode) { | ||
return subnode.type === "ObjectExpression" && subnode.properties.length === 0 ? {} : undefined; | ||
}); | ||
@@ -62,2 +62,2 @@ | ||
}); | ||
}); | ||
}); |
@@ -8,3 +8,3 @@ /* global describe:true, it:true */ | ||
var esprima = require("esprima"); | ||
describe("var", function () { | ||
@@ -93,2 +93,2 @@ it("zero arguments", function () { | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
612317
53
11606
287
1
+ Addedestraverse@1.5.1(transitive)
- Removedestraverse@1.4.0(transitive)
Updatedestraverse@~1.5.0