Skip to content

Commit

Permalink
Check object extensibility before adding default property.
Browse files Browse the repository at this point in the history
The checks added previously were checking for `Object.isFrozen`, but
that only covers one scenario (`Object.freeze`) and it is still possible
to make an object non-extensible without freezing it.

This adds tests for frozen objects (previously passing) and a new test
for `Object.seal`'ed objects (which was failing prior to this change).
  • Loading branch information
rwjblue committed Mar 5, 2017
1 parent b0876f8 commit a1950aa
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/loader/loader.js
Expand Up @@ -123,7 +123,7 @@ var loader, define, requireModule, require, requirejs;
var exports = this.module.exports;
if (exports !== null &&
(typeof exports === 'object' || typeof exports === 'function') &&
exports['default'] === undefined && !Object.isFrozen(exports)) {
exports['default'] === undefined && Object.isExtensible(exports)) {
exports['default'] = exports;
}
};
Expand Down
74 changes: 74 additions & 0 deletions tests/all.js
Expand Up @@ -736,6 +736,80 @@ test('if a module has no default property assume its export is default (function
});
});

test('if a module has no default property assume its export is default (object)', function() {
var theObject = {};
define('foo', ['require', 'exports', 'module'], function() {
return theObject;
});

equal(require('foo')['default'], theObject);
equal(require('foo'), theObject);

var stats = statsForMonitor('loaderjs', tree);

deepEqual(stats, {
findDeps: 1,
define: 1,
exports: 1,
findModule: 2,
modules: 1,
reify: 1,
require: 2,
resolve: 0,
resolveRelative: 0,
pendingQueueLength: 1
});
});

test('does not add default if export is frozen', function() {
var theObject = Object.freeze({});
define('foo', ['require', 'exports', 'module'], function() {
return theObject;
});

ok(!('default' in require('foo')));
equal(require('foo'), theObject);

var stats = statsForMonitor('loaderjs', tree);

deepEqual(stats, {
findDeps: 1,
define: 1,
exports: 1,
findModule: 2,
modules: 1,
reify: 1,
require: 2,
resolve: 0,
resolveRelative: 0,
pendingQueueLength: 1
});
});

test('does not add default if export is sealed', function() {
var theObject = Object.seal({ derp: {} });
define('foo', ['require', 'exports', 'module'], function() {
return theObject;
});

ok(!('default' in require('foo')));
equal(require('foo'), theObject);

var stats = statsForMonitor('loaderjs', tree);

deepEqual(stats, {
findDeps: 1,
define: 1,
exports: 1,
findModule: 2,
modules: 1,
reify: 1,
require: 2,
resolve: 0,
resolveRelative: 0,
pendingQueueLength: 1
});
});

test('has good error message for missing module', function() {
var theFunction = function theFunction() {};
Expand Down

0 comments on commit a1950aa

Please sign in to comment.