Wednesday, 11 September 2013

JavaScript that must be placed outside a function

JavaScript that must be placed outside a function

I am writing a JS merger application, which would consider two similar js
files, (maintained by different developers) namely oldfile and newfile and
merges the changes from newfile to oldfile while keeping additional
content that is solely present in oldfile.
JavaScript found to be so dynamic that it is almost impossible to identify
the matching content between oldfile and newfile. The only area that works
for me is the named functions. So I am going to wrap all the JS file
contents into named functions before delivering to the developers to
maintain. When it comes back for merge, I will match the function names to
find what to replace and what to keep intact.
The areas I workout are:
Move the anonymous functions found in jQuery into named functions and call
them in jQuery
Name any anonymous function like var v = function()...
Wrap all jQueries into named self executable functions
Wrap any other pieces of code that is not inside the functions into self
executable functions while keeping the order intact.
The global variables are moved to top of the JS file. They are verified by
their names.
I am wondering if there is any other crucial content than variables that
must be placed outside the function.
The merging process works as follows:
Capture all strings (single quote, double quote), comments (inline, block)
and replace with some unique identifiers
Capture the function body (based on the count of '{' and '}') and replace
with unique identifier.
Compare and replace the captured function bodies with latest version content.
Restore all captures to generate the final output.
This is working for the files in hand. Is there a plausible cases that
would fail in this approach?

No comments:

Post a Comment