diff --git a/src/ast/variables/NamespaceVariable.ts b/src/ast/variables/NamespaceVariable.ts index ffb033ca55b..b25570560ea 100644 --- a/src/ast/variables/NamespaceVariable.ts +++ b/src/ast/variables/NamespaceVariable.ts @@ -1,5 +1,6 @@ import Module, { AstContext } from '../../Module'; import { RenderOptions } from '../../utils/renderHelpers'; +import { RESERVED_NAMES } from '../../utils/reservedNames'; import Identifier from '../nodes/Identifier'; import { UNKNOWN_PATH } from '../values'; import Variable from './Variable'; @@ -82,7 +83,9 @@ export default class NamespaceVariable extends Variable { }${_}}`; } - return `${t}${name}: ${original.getName()}`; + const safeName = RESERVED_NAMES[name] ? `'${name}'` : name; + + return `${t}${safeName}: ${original.getName()}`; }); const name = this.getName(); diff --git a/src/utils/reservedNames.ts b/src/utils/reservedNames.ts index 710cb9df452..a72f80a7bfb 100644 --- a/src/utils/reservedNames.ts +++ b/src/utils/reservedNames.ts @@ -4,6 +4,11 @@ export interface NameCollection { [name: string]: true; } +// Verified on IE 6/7 that these keywords can't be used for object properties without escaping: +// break case catch class const continue debugger default delete do +// else enum export extends false finally for function if import +// in instanceof new null return super switch this throw true +// try typeof var void while with export const RESERVED_NAMES: NameCollection = Object.assign(Object.create(null), { await: true, break: true, @@ -21,6 +26,7 @@ export const RESERVED_NAMES: NameCollection = Object.assign(Object.create(null), eval: true, export: true, extends: true, + false: true, finally: true, for: true, function: true, @@ -41,7 +47,9 @@ export const RESERVED_NAMES: NameCollection = Object.assign(Object.create(null), static: true, super: true, switch: true, + this: true, throw: true, + true: true, try: true, typeof: true, undefined: true, diff --git a/test/form/samples/namespace-object-import/_config.js b/test/form/samples/namespace-object-import/_config.js new file mode 100644 index 00000000000..010ac0d5f9d --- /dev/null +++ b/test/form/samples/namespace-object-import/_config.js @@ -0,0 +1,6 @@ +module.exports = { + description: 'properly encodes reserved names if namespace import is used', + options: { + input: ['main.js'] + } +}; diff --git a/test/form/samples/namespace-object-import/_expected/amd.js b/test/form/samples/namespace-object-import/_expected/amd.js new file mode 100644 index 00000000000..eea779d6e20 --- /dev/null +++ b/test/form/samples/namespace-object-import/_expected/amd.js @@ -0,0 +1,11 @@ +define(function () { 'use strict'; + + var dep = "default"; + + var dep$1 = /*#__PURE__*/Object.freeze({ + 'default': dep + }); + + console.log(dep$1); + +}); diff --git a/test/form/samples/namespace-object-import/_expected/cjs.js b/test/form/samples/namespace-object-import/_expected/cjs.js new file mode 100644 index 00000000000..2271b4c45d8 --- /dev/null +++ b/test/form/samples/namespace-object-import/_expected/cjs.js @@ -0,0 +1,9 @@ +'use strict'; + +var dep = "default"; + +var dep$1 = /*#__PURE__*/Object.freeze({ + 'default': dep +}); + +console.log(dep$1); diff --git a/test/form/samples/namespace-object-import/_expected/es.js b/test/form/samples/namespace-object-import/_expected/es.js new file mode 100644 index 00000000000..321ce0ebcbc --- /dev/null +++ b/test/form/samples/namespace-object-import/_expected/es.js @@ -0,0 +1,7 @@ +var dep = "default"; + +var dep$1 = /*#__PURE__*/Object.freeze({ + 'default': dep +}); + +console.log(dep$1); diff --git a/test/form/samples/namespace-object-import/_expected/iife.js b/test/form/samples/namespace-object-import/_expected/iife.js new file mode 100644 index 00000000000..8560919ed54 --- /dev/null +++ b/test/form/samples/namespace-object-import/_expected/iife.js @@ -0,0 +1,12 @@ +(function () { + 'use strict'; + + var dep = "default"; + + var dep$1 = /*#__PURE__*/Object.freeze({ + 'default': dep + }); + + console.log(dep$1); + +}()); diff --git a/test/form/samples/namespace-object-import/_expected/system.js b/test/form/samples/namespace-object-import/_expected/system.js new file mode 100644 index 00000000000..736bef6a486 --- /dev/null +++ b/test/form/samples/namespace-object-import/_expected/system.js @@ -0,0 +1,16 @@ +System.register([], function (exports, module) { + 'use strict'; + return { + execute: function () { + + var dep = "default"; + + var dep$1 = /*#__PURE__*/Object.freeze({ + 'default': dep + }); + + console.log(dep$1); + + } + }; +}); diff --git a/test/form/samples/namespace-object-import/_expected/umd.js b/test/form/samples/namespace-object-import/_expected/umd.js new file mode 100644 index 00000000000..781060a2c00 --- /dev/null +++ b/test/form/samples/namespace-object-import/_expected/umd.js @@ -0,0 +1,14 @@ +(function (factory) { + typeof define === 'function' && define.amd ? define(factory) : + factory(); +}(function () { 'use strict'; + + var dep = "default"; + + var dep$1 = /*#__PURE__*/Object.freeze({ + 'default': dep + }); + + console.log(dep$1); + +})); diff --git a/test/form/samples/namespace-object-import/dep.js b/test/form/samples/namespace-object-import/dep.js new file mode 100644 index 00000000000..eb3cab741ed --- /dev/null +++ b/test/form/samples/namespace-object-import/dep.js @@ -0,0 +1 @@ +export default "default" diff --git a/test/form/samples/namespace-object-import/main.js b/test/form/samples/namespace-object-import/main.js new file mode 100644 index 00000000000..2a8d642de3d --- /dev/null +++ b/test/form/samples/namespace-object-import/main.js @@ -0,0 +1,3 @@ +import * as dep from './dep.js'; + +console.log(dep); \ No newline at end of file