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
Expand Up @@ -120,17 +120,36 @@ function processPatch(patchSource, patchFilename) {
return chunk;
});

return function(ast) {
return function(matcher) {
_.each(chunks, function(chunk) {
var matchOptions = {
strictMatches: true,
filename: chunk.filename,
lineNumber: chunk.lineNumber
};
ast.findStrict(chunk.find, function(v) {
v.node.applyPatch(chunk.patch, patchFilename, chunk.lineNumber);
}, matchOptions);
var reparse;
do {
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;

try {
jsgrep.jsgrep({
ast = jsgrep.jsgrep({
source: ast,
matchScript: matchFn,
callback: function(node, variables) {
Expand Down
3 changes: 2 additions & 1 deletion lib/jsgrep.js
Expand Up @@ -508,7 +508,7 @@ var jsgrep = exports.jsgrep = function(options) {
matchFn = options.matchScript;
}
try {
matchFn(matcher);
ast = matchFn(matcher);
} catch(e) {
throw new JsgrepError('Matcher threw exception')
.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.