rtd icon indicating copy to clipboard operation
rtd copied to clipboard

Wrap files in Immediately-invoked function expression

Open deepwell opened this issue 12 years ago • 6 comments

Meteor wraps all files in Immediately-invoked function expression (function() { /* your code */ }()); This makes sure nothing is accidentally made available on a global scope and gives each file private functions.

RTD should also do the same thing, because otherwise functions with the same name that exist in more than one file can be run for a different context than you would expect. For unit tests and code coverage.

Eg: Create a function called function test() { console.log('hello') } and on that say 'goodbye' in 2 different view files. Now when you call function test() in view 1, and it might call the same function name in view 2.

I recommend RTD wraps all files in an immediately invoked function expression before executing them like Meteor.

deepwell avatar Aug 28 '13 01:08 deepwell

In which files did you find a missing function wrap ?

lc3t35 avatar Sep 06 '13 06:09 lc3t35

Every single file inside my application ie. the app/* directory.

deepwell avatar Sep 06 '13 17:09 deepwell

@deepwell , then it's yours, not rtd related ;) http://en.wikipedia.org/wiki/Immediately-invoked_function_expression; well known Javascript pattern.

lc3t35 avatar Sep 07 '13 15:09 lc3t35

That's exactly what I thought at first too, so I modified every single one of my app files to be wrapped in immediately-invoked function expressions. But the more I thought about it, the more I realized that now on production with Meteor, all of my code is being wrapped twice. Once by me, and once by Meteor. Seems kind of silly. And only required to make my code work with RTD.

Because RTD mocks and stubs Meteor, I now feel that RTD should also wrap all files in an immediately invoked function exp. just like Meteor itself.

Thoughts?

deepwell avatar Sep 07 '13 18:09 deepwell

Oh ! Well done ! you're right, i didn't noticed that some of my mirror_app files have dual function wrapping, excellent ! Waiting for Sam comment ...

lc3t35 avatar Sep 08 '13 19:09 lc3t35

Hello. We actually do wrap all functions manually, as you can see here: https://github.com/xolvio/meteor-rtd-example-project/blob/master. This keeps the jshint happy in our IDE :)

I think it would be a nice option to have RTD do it so I'll look into it and report back here.

xolvio avatar Sep 11 '13 01:09 xolvio