diff --git a/src/Module.js b/src/Module.js index 9725bea..606f525 100644 --- a/src/Module.js +++ b/src/Module.js @@ -111,7 +111,7 @@ class SyntheticNamespaceDeclaration { 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 () { diff --git a/test/function/namespaces-are-frozen/_config.js b/test/function/namespaces-are-frozen/_config.js new file mode 100644 index 0000000..f0fd432 --- /dev/null +++ b/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 ); + }); + } +}; diff --git a/test/function/namespaces-are-frozen/main.js b/test/function/namespaces-are-frozen/main.js new file mode 100644 index 0000000..e0af622 --- /dev/null +++ b/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 }); diff --git a/test/function/namespaces-are-frozen/mod.js b/test/function/namespaces-are-frozen/mod.js new file mode 100644 index 0000000..0b0c330 --- /dev/null +++ b/test/function/namespaces-are-frozen/mod.js @@ -0,0 +1,2 @@ +export var a = 1; +export var b = 2;