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

Commit

Permalink
jspatch refactor for programmability
Browse files Browse the repository at this point in the history
Factor out the getPatternFromPatch and getPatchedCode, useful for integration
with other tools.
  • Loading branch information
Ryan Patterson committed Jan 16, 2012
1 parent 277e691 commit ab21d53
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
19 changes: 2 additions & 17 deletions bin/jspatch-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,30 +94,15 @@ var matchFn;
function processPatch(patchSource, patchFilename) {
var lineNumber = 1;
var chunks = _.map(patchSource.split('\n---\n'), function(chunkSource) {
var lines = chunkSource.split('\n');
var chunk = {
find: [],
find: jsgrep.Matcher.getPatternFromPatch(chunkSource),
patch: chunkSource,
filename: patchFilename || "",
lineNumber: lineNumber
};

for(var i = 0; i < lines.length; i++) {
if (lines[i][0] == '-') {
chunk.find.push(lines[i].substr(1));
lineNumber += chunkSource.split('\n').length + 1;

} else if (lines[i][0] == '+') {
// So line numbers match up
chunk.find.push('\n');

} else {
chunk.find.push(lines[i]);
}
}

chunk.find = chunk.find.join('\n').trim();

lineNumber += lines.length + 1;
return chunk;
});

Expand Down
34 changes: 31 additions & 3 deletions lib/jsgrep.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,25 @@ Matcher.getSourceForNode = function(target) {
return target.tokenizer.source.substring(start, end);
};

Matcher.getPatternFromPatch = function(patch) {
var pattern = [];
var lines = patch.split('\n');
for(var i = 0; i < lines.length; i++) {
if (lines[i][0] == '-') {
pattern.push(lines[i].substr(1));

} else if (lines[i][0] == '+') {
// So line numbers match up
pattern.push('\n');

} else {
pattern.push(lines[i]);
}
}

return pattern.join('\n');
};

Matcher.prototype.find = function(pattern, callback, options) {
var ret = [ ];
var self = this;
Expand All @@ -88,10 +107,10 @@ Matcher.prototype.matchStrict = function(pattern, callback) {
};

/**
* Apply a single patch chunk to this matcher's node. The matcher must strictly
* match the patch!
* Return the results of applying a single patch chunk to this matcher's node.
* The matcher must strictly match the patch!
*/
Matcher.prototype.applyPatch = function(patch, filename, line) {
Matcher.prototype.getPatchedCode = function(patch, filename, line) {
const tokens = Narcissus.definitions.tokenIds;
const REMOVE = '!REMOVE';
const ADD = '!ADD';
Expand Down Expand Up @@ -249,6 +268,15 @@ Matcher.prototype.applyPatch = function(patch, filename, line) {
// Remove blank lines from new code
nodeSource = nodeSource.replace(/^\s*\n|\n*$/g, '');

return nodeSource;
}

/**
* Apply a single patch chunk to this matcher's node. The matcher must strictly
* match the patch!
*/
Matcher.prototype.applyPatch = function(patch, filename, line) {
var nodeSource = this.getPatchedCode(patch, filename, line);
this._replaceWithString(this.ast, nodeSource);
};

Expand Down

0 comments on commit ab21d53

Please sign in to comment.