deleteman / vatican

Micro-framework designed to create REST APIs with minor effort
98 stars 6 forks source link

handleParser.js regexp #8

Closed smart--petea closed 10 years ago

smart--petea commented 10 years ago

Regexp for endpoint (from mocha-tests branch) is

regExp = /@endpoint *\((url:.+) *(method:.+)\).*\n(.*)\(/gim;

It can be transformed to

regExp = /@endpoint\s*\((url:.+)\s*(method:.+)\)[\s\n]*[^\s]*\.prototype\.([^\s]*)/gim

[\s\n]* - I do not know the distance between @endpoint statement and function declaration [^\s]* - class name ([^\s]*) - method name

In this case this snippet

var actionStr = matches[3];

if(actionStr.indexOf("=") !== -1) {
        actionStr = actionStr.split("=")[0];
}
if(actionStr.indexOf(".") !== -1) {
           actionStr = actionStr.split(".");
           actionStr = actionStr[actionStr.length - 1];
}
currentPath['action'] = actionStr.trim();

become

currentPath['action'] = matches[3];
smart--petea commented 10 years ago

I do not know if it is the case. In mocha-tests branch the regexp was defined in the module and used by parser from closure every time it is needed.

If you want to improve the preciousness of regexp then it can be tied to the file which is parsed. I see next steps for strike this goal:

  1. replace this snippet
_(files).where(function(f) { return f.match(/\.js$/i); }).forEach(function(fname) {

with

_(files).where(function(f) { return /\(.*?).js$/i.exec(f); }).forEach(function(fileInfo) {

where (if successful case) fileInfo = [ filePath, className]

  1. define regexp
regExp  = new RegExp("@endpoint\\s*\\((url:.+)\\s*(method:.+)\\)[\\s\\n]*" + className + "\\.prototype\\.([^\\s]*)", "gim")
deleteman commented 10 years ago

Closing this since I've followed your advise and improved the regex.