2

I have some code I may want to refactor many times. Imagine, something like this:

Namespace.CustomObject = function(name,type){

  this.name = name;
  this.type = type;

};

Namespace.CustomObject.prototype = {

  writeName: function(){
    console.log(this.name);
  },

  changeType: function(type){
    this.type = type;
  }

};

var object1 = new Namespace.CustomObject('object1','spoon');

object1.name = 'new name';
object1.changeType('new type');

So, what I would like is to have a system to generate some sort of data structure where I can know which namespaces/objects I have, and their methods, so I can decide if I want to change "changeType" function name to "setNewType", I can know that it's not just enough with replacing the prototype "changeType" property, but also object1 call to that function, because it is a CustomObject.

So, I have been trying different options.

At first I tried with grasp:

http://graspjs.com/docs/equery/

But I can't find a way to detect the scope of elements, and I think it's not possible.

Then I thought of using esprima to create the AST of my code and maybe get the structure from there that would allow me to identify objects scope, but I can't find a way to do this either.

http://esprima.org/

I guess this must be possible, but maybe I'm trying to wrong way. I know it is also a big task, so of course I'm not asking for someone to solve this, but any hint pointing in the right direction would be very helpful.

Thanks.

  • See my essay on "Life After Parsing" (easily found in my bio [click on my name in this comment], or on the web) for why this is hard. The short answer is that you need lots of supporting machinery for tasks like this, and it mostly doesn't exist in usable form. – Ira Baxter Aug 24 '14 at 15:48
  • Thanks for your answer @IraBaxter. Anyway I still have the feeling that this must be something you can do. When you run JavaScript code in a console environment (browser debugger or NodejS), objects are allocated in memory, and it knows which functions, properties, etc. are available to each type of object. – mariogarranz Aug 25 '14 at 13:23
  • yes, of course it can be done. The question is, is this machinery available to you in a usable form? Usually the compiler/interpreter's scheme for managing data is not in a useful form for program manipulation; why should it be, since that is not what those tools do? That generally means somebody has to make that machinery available in a usable form, and that's where the effort lies. – Ira Baxter Aug 25 '14 at 14:00
  • OK now I get what you mean. Thanks a lot :) – mariogarranz Aug 25 '14 at 18:52

0 Answers0