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

test: eval a strict function #5250

Closed
wants to merge 1 commit into from
Closed
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
28 changes: 28 additions & 0 deletions test/parallel/test-regress-GH-2245.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* eslint-disable strict */
Copy link
Contributor

Choose a reason for hiding this comment

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

Why this is necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

in strict mode running this will fail as bar is undefined until the code has been evaled. If this line is not present the build will fail linting

Copy link
Member

Choose a reason for hiding this comment

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

it would be better to disable the no-undef rule inline

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I could have been clearer above, the issue here is that this code is not valid in strict mode, since eval in strict mode does not introduce new variables into the surrounding scope. Therefore this file cannot have strict mode enabled, and hence this linter rule.

Copy link
Member

Choose a reason for hiding this comment

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

Oh I see, my bad.

require('../common');
var assert = require('assert');

/*
in node 0.10 a bug existed that caused strict functions to not capture
their environment when evaluated. When run in 0.10 `test()` fails with a
`ReferenceError`. See https://github.com/nodejs/node/issues/2245 for details.
*/

function test() {

var code = [
'var foo = {m: 1};',
'',
'function bar() {',
'\'use strict\';',
'return foo; // foo isn\'t captured in 0.10',
'};'
].join('\n');

eval(code);

return bar();

}

assert.deepEqual(test(), {m: 1});