Browse Source

Prevent namespace abuse by freezing them

Fixes #202
better-aggressive
Oskar Segersvärd 9 years ago
parent
commit
17df91a529
  1. 2
      src/Module.js
  2. 35
      test/function/namespaces-are-frozen/_config.js
  3. 5
      test/function/namespaces-are-frozen/main.js
  4. 2
      test/function/namespaces-are-frozen/mod.js

2
src/Module.js

@ -111,7 +111,7 @@ class SyntheticNamespaceDeclaration {
return `${indentString}${name}: ${original.render()}`; return `${indentString}${name}: ${original.render()}`;
}); });
return `var ${this.render()} = {\n${members.join( ',\n' )}\n};\n\n`; return `var ${this.render()} = Object.freeze({\n${members.join( ',\n' )}\n});\n\n`;
} }
render () { render () {

35
test/function/namespaces-are-frozen/_config.js

@ -0,0 +1,35 @@
var assert = require( 'assert' );
module.exports = {
description: 'namespaces should be non-extensible and its properties immutatable and non-configurable',
exports: function ( exports ) {
const ns = exports.ns;
function extend ( obj ) {
'use strict';
obj.newProperty = true;
}
function reconfigure ( obj ) {
Object.defineProperty( obj, 'a', { value: null } );
}
function mutate ( obj ) {
'use strict';
obj.a = 2;
}
assert.throws(function () {
extend( ns );
});
assert.throws(function () {
reconfigure( ns );
});
assert.throws(function () {
mutate( ns );
});
}
};

5
test/function/namespaces-are-frozen/main.js

@ -0,0 +1,5 @@
// import * as ns from './mod';
// export { ns };
export var ns = Object.freeze({ a: 1, b: 2 });

2
test/function/namespaces-are-frozen/mod.js

@ -0,0 +1,2 @@
export var a = 1;
export var b = 2;
Loading…
Cancel
Save