Skip to content
This repository
Browse code

jspatch refactor for programmability

Factor out the getPatternFromPatch and getPatchedCode, useful for integration
with other tools.
  • Loading branch information...
commit ab21d53e2ef49b0446bc7f93ce5e96df78e20638 1 parent 277e691
authored January 15, 2012
19  bin/jspatch-cli.js
@@ -94,30 +94,15 @@ var matchFn;
94 94
 function processPatch(patchSource, patchFilename) {
95 95
   var lineNumber = 1;
96 96
   var chunks = _.map(patchSource.split('\n---\n'), function(chunkSource) {
97  
-    var lines = chunkSource.split('\n');
98 97
     var chunk = {
99  
-      find: [],
  98
+      find: jsgrep.Matcher.getPatternFromPatch(chunkSource),
100 99
       patch: chunkSource,
101 100
       filename: patchFilename || "",
102 101
       lineNumber: lineNumber
103 102
     };
104 103
 
105  
-    for(var i = 0; i < lines.length; i++) {
106  
-      if (lines[i][0] == '-') {
107  
-        chunk.find.push(lines[i].substr(1));
  104
+    lineNumber += chunkSource.split('\n').length + 1;
108 105
 
109  
-      } else if (lines[i][0] == '+') {
110  
-        // So line numbers match up
111  
-        chunk.find.push('\n');
112  
-
113  
-      } else {
114  
-        chunk.find.push(lines[i]);
115  
-      }
116  
-    }
117  
-
118  
-    chunk.find = chunk.find.join('\n').trim();
119  
-
120  
-    lineNumber += lines.length + 1;
121 106
     return chunk;
122 107
   });
123 108
 
34  lib/jsgrep.js
@@ -62,6 +62,25 @@ Matcher.getSourceForNode = function(target) {
62 62
   return target.tokenizer.source.substring(start, end);
63 63
 };
64 64
 
  65
+Matcher.getPatternFromPatch = function(patch) {
  66
+  var pattern = [];
  67
+  var lines = patch.split('\n');
  68
+  for(var i = 0; i < lines.length; i++) {
  69
+    if (lines[i][0] == '-') {
  70
+      pattern.push(lines[i].substr(1));
  71
+
  72
+    } else if (lines[i][0] == '+') {
  73
+      // So line numbers match up
  74
+      pattern.push('\n');
  75
+
  76
+    } else {
  77
+      pattern.push(lines[i]);
  78
+    }
  79
+  }
  80
+
  81
+  return pattern.join('\n');
  82
+};
  83
+
65 84
 Matcher.prototype.find = function(pattern, callback, options) {
66 85
   var ret = [ ];
67 86
   var self = this;
@@ -88,10 +107,10 @@ Matcher.prototype.matchStrict = function(pattern, callback) {
88 107
 };
89 108
 
90 109
 /**
91  
- * Apply a single patch chunk to this matcher's node. The matcher must strictly
92  
- * match the patch!
  110
+ * Return the results of applying a single patch chunk to this matcher's node.
  111
+ * The matcher must strictly match the patch!
93 112
  */
94  
-Matcher.prototype.applyPatch = function(patch, filename, line) {
  113
+Matcher.prototype.getPatchedCode = function(patch, filename, line) {
95 114
   const tokens = Narcissus.definitions.tokenIds;
96 115
   const REMOVE = '!REMOVE';
97 116
   const ADD = '!ADD';
@@ -249,6 +268,15 @@ Matcher.prototype.applyPatch = function(patch, filename, line) {
249 268
   // Remove blank lines from new code
250 269
   nodeSource = nodeSource.replace(/^\s*\n|\n*$/g, '');
251 270
 
  271
+  return nodeSource;
  272
+}
  273
+
  274
+/**
  275
+ * Apply a single patch chunk to this matcher's node. The matcher must strictly
  276
+ * match the patch!
  277
+ */
  278
+Matcher.prototype.applyPatch = function(patch, filename, line) {
  279
+  var nodeSource = this.getPatchedCode(patch, filename, line);
252 280
   this._replaceWithString(this.ast, nodeSource);
253 281
 };
254 282
 

0 notes on commit ab21d53

Please sign in to comment.
Something went wrong with that request. Please try again.