Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep reference to this #3

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function addWith(obj, src, exclude) {

src = '(function (' + vars.join(', ') + ') {' +
src +
'}(' + inputVars.join(',') + '))'
'}.bind(this)(' + inputVars.join(',') + '))'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we fix this to use .call(this, ...args) rather than .bind(this)(...args)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stop!!! Then you should change AST checking too! otherwise it will fail.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see, you've changed AST checking too -)
Sorry, I'm thinking at lower speed than you probably


return ';' + declareLocal + ';' + unwrapReturns(src, result) + ';'
}
Expand Down Expand Up @@ -77,12 +77,17 @@ function unwrapReturns(src, result) {
var originalSource = src
var hasReturn = false
var ast = uglify.parse(src)
var ref
src = src.split('')

if (ast.body.length !== 1 || ast.body[0].TYPE !== 'SimpleStatement' ||
ast.body[0].body.TYPE !== 'Call' || ast.body[0].body.expression.TYPE !== 'Function')
if ((ref = ast.body).length !== 1
|| (ref = ref[0]).TYPE !== 'SimpleStatement'
|| (ref = ref.body).TYPE !== 'Call'
|| (ref = ref.expression).TYPE !== 'Call'
|| (ref = ref.expression).TYPE !== 'Dot' || ref.property !== 'bind'
|| (ref = ref.expression).TYPE !== 'Function')
throw new Error('AST does not seem to represent a self-calling function')
var fn = ast.body[0].body.expression
var fn = ref

var walker = new uglify.TreeWalker(visitor)
function visitor(node, descend) {
Expand Down
16 changes: 15 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,18 @@ describe('addWith("obj || {}", "return foo")', function () {
assert(Function('obj', src + ';return "ding"')({}) === 'ding')
done()
})
})
})

describe('addWith("obj || {}", "return this[foo]")', function () {
it('keeps reference to this', function (done) {
var src = addWith('obj || {}', 'return this[foo]')
// obj.bar = obj.foo
var obj = {
foo: 'bar',
bar: 'ding',
fn: Function('obj', src)
}
assert(obj.fn(obj) === 'ding')
done()
})
})