Skip to content
This repository has been archived by the owner on Jan 13, 2022. It is now read-only.

Commit

Permalink
Merge pull request #4 from oyvindkinsey/patch2
Browse files Browse the repository at this point in the history
This adds support for applying overlapping patches to the same AST witho...
  • Loading branch information
Ryan Patterson committed Apr 23, 2012
2 parents 74b64eb + a42b831 commit a1f0d72
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
29 changes: 24 additions & 5 deletions bin/jspatch-cli.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -120,17 +120,36 @@ function processPatch(patchSource, patchFilename) {
return chunk; return chunk;
}); });


return function(ast) { return function(matcher) {
_.each(chunks, function(chunk) { _.each(chunks, function(chunk) {
var matchOptions = { var matchOptions = {
strictMatches: true, strictMatches: true,
filename: chunk.filename, filename: chunk.filename,
lineNumber: chunk.lineNumber lineNumber: chunk.lineNumber
}; };
ast.findStrict(chunk.find, function(v) { var reparse;
v.node.applyPatch(chunk.patch, patchFilename, chunk.lineNumber); do {
}, matchOptions); if (reparse) {
matcher.ast = Narcissus.parser.parse(
matcher.ast.tokenizer.source, chunk.filename, 1);
reparse = false;
}
try {
matcher.findStrict(chunk.find, function(v) {
if (reparse) {
throw 'reparse';
}
v.node.applyPatch(chunk.patch, patchFilename, chunk.lineNumber);
reparse = true;
}, matchOptions);
} catch (ex) {
if (ex !== 'reparse') {
throw ex;
}
}
} while(reparse);
}); });
return matcher.ast;
}; };
} }


Expand Down Expand Up @@ -183,7 +202,7 @@ function doFile(filename, callback) {
var fileMatched = false; var fileMatched = false;


try { try {
jsgrep.jsgrep({ ast = jsgrep.jsgrep({
source: ast, source: ast,
matchScript: matchFn, matchScript: matchFn,
callback: function(node, variables) { callback: function(node, variables) {
Expand Down
3 changes: 2 additions & 1 deletion lib/jsgrep.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ var jsgrep = exports.jsgrep = function(options) {
matchFn = options.matchScript; matchFn = options.matchScript;
} }
try { try {
matchFn(matcher); ast = matchFn(matcher);
} catch(e) { } catch(e) {
throw new JsgrepError('Matcher threw exception') throw new JsgrepError('Matcher threw exception')
.setSource(ast.tokenizer.filename) .setSource(ast.tokenizer.filename)
Expand All @@ -531,6 +531,7 @@ var jsgrep = exports.jsgrep = function(options) {
} }
}); });
} }
return ast;
} }


/** /**
Expand Down

0 comments on commit a1f0d72

Please sign in to comment.