Skip to content

Commit

Permalink
Add es3 support for namespace object import
Browse files Browse the repository at this point in the history
  • Loading branch information
Artem Butusov committed May 3, 2019
1 parent 61a7947 commit f79ace8
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 1 deletion.
5 changes: 4 additions & 1 deletion 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';
Expand Down Expand Up @@ -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();
Expand Down
8 changes: 8 additions & 0 deletions src/utils/reservedNames.ts
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions 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']
}
};
11 changes: 11 additions & 0 deletions 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);

});
9 changes: 9 additions & 0 deletions 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);
7 changes: 7 additions & 0 deletions 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);
12 changes: 12 additions & 0 deletions 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);

}());
16 changes: 16 additions & 0 deletions 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);

}
};
});
14 changes: 14 additions & 0 deletions 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);

}));
1 change: 1 addition & 0 deletions test/form/samples/namespace-object-import/dep.js
@@ -0,0 +1 @@
export default "default"
3 changes: 3 additions & 0 deletions test/form/samples/namespace-object-import/main.js
@@ -0,0 +1,3 @@
import * as dep from './dep.js';

console.log(dep);

0 comments on commit f79ace8

Please sign in to comment.