Skip to content

Commit

Permalink
support "rename -i.bak" for inplace update
Browse files Browse the repository at this point in the history
  • Loading branch information
Claus Reinke committed Jun 16, 2012
1 parent 491b6da commit 3d76dbb
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 9 deletions.
24 changes: 20 additions & 4 deletions estr.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ switch (process.argv.shift()) {
console.log('estr tags ..paths');
console.log(' traverse paths, extract tags from .js-files, write to file "tags"');
console.log();
console.log('estr rename file.js oldName <line> <column> newName');
console.log('estr rename [-i.suffix] file.js oldName <line> <column> newName');
console.log(' rename oldName (at <line> <column>) to newName');
console.log(' -i.suffix : update file.js in-place; save original to file.js.suffix');
console.log();
console.log('estr findVar file.js name <line> <column>');
console.log(' find binding and other occurrences for name');
Expand Down Expand Up @@ -125,9 +126,12 @@ switch (process.argv.shift()) {
}());
break;

case "rename": // file oldName line column newName
// experimental, work in progress
case "rename": // [-i.suffix] file oldName line column newName
(function(){
var inplace;
if (inplace = process.argv[0].match(/^-i(\S*)/))
process.argv.shift();

var file = process.argv.shift();
var oldName = process.argv.shift();
var line = +process.argv.shift();
Expand All @@ -148,7 +152,19 @@ switch (process.argv.shift()) {

if (results[0].source) {

process.stdout.write(results[0].source);
if (inplace) { // update file inplace, with optional backup

if (inplace[1]!=='') {
console.info(inplace[1]);
fs.writeFileSync(file+inplace[1],fs.readFileSync(file,'utf8'));
}
fs.writeFileSync(file,results[0].source);

} else { // no update, write renamed source to stdout

process.stdout.write(results[0].source);

}

} else if (results[0].parseError) {

Expand Down
10 changes: 6 additions & 4 deletions tests/.gitattributes
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
sample.js -text
tags -text
*.stdout -text
*.stderr -text
sample.js -text
sample-work.js -text
sample-work.js.bak -text
tags -text
*.stdout -text
*.stderr -text
3 changes: 2 additions & 1 deletion tests/out/help.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ estr (Ecmascript traversals)
estr tags ..paths
traverse paths, extract tags from .js-files, write to file "tags"

estr rename file.js oldName <line> <column> newName
estr rename [-i.suffix] file.js oldName <line> <column> newName
rename oldName (at <line> <column>) to newName
-i.suffix : update file.js in-place; save original to file.js.suffix

estr findVar file.js name <line> <column>
find binding and other occurrences for name
1 change: 1 addition & 0 deletions tests/out/rename-success-inplace.error
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
null
6 changes: 6 additions & 0 deletions tests/out/rename-success-inplace.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
WARNING! hoisting var declaration over catch of same name: e
{ start: { line: 20, column: 8 },
end: { line: 20, column: 16 } }
WARNING! hoisting function declaration over catch of same name: f
{ start: { line: 28, column: 4 },
end: { line: 28, column: 24 } }
1 change: 1 addition & 0 deletions tests/out/rename-success-inplace.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.bak
48 changes: 48 additions & 0 deletions tests/out/sample-work.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
function log_it(x) { }
var z;
function A() {
var x;
var y = [x,function(x) {
log_it(z.a);
function A() {
log_it(z.b);
var x;
var z;
return x;
}
return A(x);
}];
var z = [function(x) { return (function(x) { return x; }(function(x) { return x; }))(x); },x];

try {
throw "hi";
} catch (e) {
var e = "ho"; // double scope binding, initialization!=declaration:-(
log_it(e);
}
log_it(e);

try {
throw "hi";
} catch (f) {
function f(){log_it(f)} // yet another scope border case, non-standard
log_it(f);
}
log_it(f);
return x;
}
A();
function outer(x,y) { return function(a,b) {
return [x,y,a,b];
}}
exports.none = null;
try {
throw "hi";
} catch (f) {
var ff = "hu";
log_it(f);
}
log_it(ff);
function require(dependency) {
return require.cache[dependency].dependency // computed vs non-computed properties
}
48 changes: 48 additions & 0 deletions tests/out/sample-work.js.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
function log(x) { }
var z;
function A() {
var x;
var y = [x,function(x) {
log(z.a);
function A() {
log(z.b);
var x;
var z;
return x;
}
return A(x);
}];
var z = [function(x) { return (function(x) { return x; }(function(x) { return x; }))(x); },x];

try {
throw "hi";
} catch (e) {
var e = "ho"; // double scope binding, initialization!=declaration:-(
log(e);
}
log(e);

try {
throw "hi";
} catch (f) {
function f(){log(f)} // yet another scope border case, non-standard
log(f);
}
log(f);
return x;
}
A();
function outer(x,y) { return function(a,b) {
return [x,y,a,b];
}}
exports.none = null;
try {
throw "hi";
} catch (f) {
var ff = "hu";
log(f);
}
log(ff);
function require(dependency) {
return require.cache[dependency].dependency // computed vs non-computed properties
}
4 changes: 4 additions & 0 deletions tests/run_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ test('rename-introduce-catch-hoist-conflict','node ../estr.js rename sample.js f
// should succeed
test('rename-success','node ../estr.js rename sample.js z 6 20 x_____x');
test('rename-success-properties','node ../estr.js rename sample.js dependency 47 23 x_____x');
test('rename-success-inplace'
,['cp sample.js sample-work.js'
,'node ../estr.js rename -i.bak sample-work.js log 1 9 log_it']
,['sample-work.js','sample-work.js.bak']);

// experimental
test('findVar','node ../estr.js findVar sample.js z 15 6');
Expand Down

0 comments on commit 3d76dbb

Please sign in to comment.