Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Symbol polyfill in module namespaces #3135

Merged
merged 5 commits into from
Sep 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 6 additions & 16 deletions src/ast/variables/NamespaceVariable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,25 +91,15 @@ export default class NamespaceVariable extends Variable {
return `${t}${safeName}: ${original.getName()}`;
});

if (options.namespaceToStringTag) {
members.unshift(`${t}[Symbol.toStringTag]:${_}'Module'`);
}

const name = this.getName();

const callee = options.freeze ? `/*#__PURE__*/Object.freeze` : '';

let output = `${options.varOrConst} ${name} = ${
options.namespaceToStringTag
? `{${n}${members.join(`,${n}`)}${n}};`
: `${callee}({${n}${members.join(`,${n}`)}${n}});`
}`;

if (options.namespaceToStringTag) {
output += `${n}if${_}(typeof Symbol${_}!==${_}'undefined'${_}&&${_}Symbol.toStringTag)${n}`;
output += `${t}Object.defineProperty(${name},${_}Symbol.toStringTag,${_}{${_}value:${_}'Module'${_}});${n}`;
output += `else${n || ' '}`;
output += `${t}Object.defineProperty(${name},${_}'toString',${_}{${_}value:${_}function${_}()${_}{${_}return${_}'[object Module]'${
options.compact ? ';' : ''
}${_}}${_}});${n}`;
output += `${callee}(${name});`;
}
const membersStr = members.join(`,${n}`);
let output = `${options.varOrConst} ${name}${_}=${_}${callee}({${n}${membersStr}${n}});`;

if (options.format === 'system' && this.exportName) {
output += `${n}exports('${this.exportName}',${_}${name});`;
Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/compact/_expected/amd.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
define(['external'],function(x){'use strict';x=x&&x.hasOwnProperty('default')?x['default']:x;var self = {get default(){return foo}};if(typeof Symbol!=='undefined'&&Symbol.toStringTag)Object.defineProperty(self,Symbol.toStringTag,{value:'Module'});else Object.defineProperty(self,'toString',{value:function(){return'[object Module]';}});/*#__PURE__*/Object.freeze(self);console.log(self);
define(['external'],function(x){'use strict';x=x&&x.hasOwnProperty('default')?x['default']:x;var self=/*#__PURE__*/Object.freeze({[Symbol.toStringTag]:'Module',get default(){return foo}});console.log(self);
function foo () {
console.log( x );
}
Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/compact/_expected/cjs.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';function _interopDefault(e){return(e&&(typeof e==='object')&&'default'in e)?e['default']:e}var x=_interopDefault(require('external'));var self = {get default(){return foo}};if(typeof Symbol!=='undefined'&&Symbol.toStringTag)Object.defineProperty(self,Symbol.toStringTag,{value:'Module'});else Object.defineProperty(self,'toString',{value:function(){return'[object Module]';}});/*#__PURE__*/Object.freeze(self);console.log(self);
'use strict';function _interopDefault(e){return(e&&(typeof e==='object')&&'default'in e)?e['default']:e}var x=_interopDefault(require('external'));var self=/*#__PURE__*/Object.freeze({[Symbol.toStringTag]:'Module',get default(){return foo}});console.log(self);
function foo () {
console.log( x );
}
Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/compact/_expected/es.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import x from'external';var self = {get default(){return foo}};if(typeof Symbol!=='undefined'&&Symbol.toStringTag)Object.defineProperty(self,Symbol.toStringTag,{value:'Module'});else Object.defineProperty(self,'toString',{value:function(){return'[object Module]';}});/*#__PURE__*/Object.freeze(self);console.log(self);
import x from'external';var self=/*#__PURE__*/Object.freeze({[Symbol.toStringTag]:'Module',get default(){return foo}});console.log(self);
function foo () {
console.log( x );
}
Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/compact/_expected/iife.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var foo=(function(x){'use strict';x=x&&x.hasOwnProperty('default')?x['default']:x;var self = {get default(){return foo}};if(typeof Symbol!=='undefined'&&Symbol.toStringTag)Object.defineProperty(self,Symbol.toStringTag,{value:'Module'});else Object.defineProperty(self,'toString',{value:function(){return'[object Module]';}});/*#__PURE__*/Object.freeze(self);console.log(self);
var foo=(function(x){'use strict';x=x&&x.hasOwnProperty('default')?x['default']:x;var self=/*#__PURE__*/Object.freeze({[Symbol.toStringTag]:'Module',get default(){return foo}});console.log(self);
function foo () {
console.log( x );
}
Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/compact/_expected/system.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
System.register('foo',['external'],function(exports){'use strict';var x;return{setters:[function(module){x=module.default;}],execute:function(){exports('default',foo);var self = {get default(){return foo}};if(typeof Symbol!=='undefined'&&Symbol.toStringTag)Object.defineProperty(self,Symbol.toStringTag,{value:'Module'});else Object.defineProperty(self,'toString',{value:function(){return'[object Module]';}});/*#__PURE__*/Object.freeze(self);console.log(self);
System.register('foo',['external'],function(exports){'use strict';var x;return{setters:[function(module){x=module.default;}],execute:function(){exports('default',foo);var self=/*#__PURE__*/Object.freeze({[Symbol.toStringTag]:'Module',get default(){return foo}});console.log(self);
function foo () {
console.log( x );
}
Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/compact/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(function(g,f){typeof exports==='object'&&typeof module!=='undefined'?module.exports=f(require('external')):typeof define==='function'&&define.amd?define(['external'],f):(g=g||self,g.foo=f(g.x));}(this,function(x){'use strict';x=x&&x.hasOwnProperty('default')?x['default']:x;var self = {get default(){return foo}};if(typeof Symbol!=='undefined'&&Symbol.toStringTag)Object.defineProperty(self,Symbol.toStringTag,{value:'Module'});else Object.defineProperty(self,'toString',{value:function(){return'[object Module]';}});/*#__PURE__*/Object.freeze(self);console.log(self);
(function(g,f){typeof exports==='object'&&typeof module!=='undefined'?module.exports=f(require('external')):typeof define==='function'&&define.amd?define(['external'],f):(g=g||self,g.foo=f(g.x));}(this,function(x){'use strict';x=x&&x.hasOwnProperty('default')?x['default']:x;var self=/*#__PURE__*/Object.freeze({[Symbol.toStringTag]:'Module',get default(){return foo}});console.log(self);
function foo () {
console.log( x );
}
Expand Down
10 changes: 3 additions & 7 deletions test/form/samples/namespace-tostringtag/_expected/amd.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
define(['exports'], function (exports) { 'use strict';

var self = {
var self = /*#__PURE__*/Object.freeze({
[Symbol.toStringTag]: 'Module',
get p () { return p; }
};
if (typeof Symbol !== 'undefined' && Symbol.toStringTag)
Object.defineProperty(self, Symbol.toStringTag, { value: 'Module' });
else
Object.defineProperty(self, 'toString', { value: function () { return '[object Module]' } });
/*#__PURE__*/Object.freeze(self);
});

console.log(Object.keys(self));

Expand Down
10 changes: 3 additions & 7 deletions test/form/samples/namespace-tostringtag/_expected/cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@

Object.defineProperty(exports, '__esModule', { value: true });

var self = {
var self = /*#__PURE__*/Object.freeze({
[Symbol.toStringTag]: 'Module',
get p () { return p; }
};
if (typeof Symbol !== 'undefined' && Symbol.toStringTag)
Object.defineProperty(self, Symbol.toStringTag, { value: 'Module' });
else
Object.defineProperty(self, 'toString', { value: function () { return '[object Module]' } });
/*#__PURE__*/Object.freeze(self);
});

console.log(Object.keys(self));

Expand Down
10 changes: 3 additions & 7 deletions test/form/samples/namespace-tostringtag/_expected/es.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
var self = {
var self = /*#__PURE__*/Object.freeze({
[Symbol.toStringTag]: 'Module',
get p () { return p; }
};
if (typeof Symbol !== 'undefined' && Symbol.toStringTag)
Object.defineProperty(self, Symbol.toStringTag, { value: 'Module' });
else
Object.defineProperty(self, 'toString', { value: function () { return '[object Module]' } });
/*#__PURE__*/Object.freeze(self);
});

console.log(Object.keys(self));

Expand Down
10 changes: 3 additions & 7 deletions test/form/samples/namespace-tostringtag/_expected/iife.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
var iife = (function (exports) {
'use strict';

var self = {
var self = /*#__PURE__*/Object.freeze({
[Symbol.toStringTag]: 'Module',
get p () { return p; }
};
if (typeof Symbol !== 'undefined' && Symbol.toStringTag)
Object.defineProperty(self, Symbol.toStringTag, { value: 'Module' });
else
Object.defineProperty(self, 'toString', { value: function () { return '[object Module]' } });
/*#__PURE__*/Object.freeze(self);
});

console.log(Object.keys(self));

Expand Down
10 changes: 3 additions & 7 deletions test/form/samples/namespace-tostringtag/_expected/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@ System.register('iife', [], function (exports) {
return {
execute: function () {

var self = {
var self = /*#__PURE__*/Object.freeze({
[Symbol.toStringTag]: 'Module',
get p () { return p; }
};
if (typeof Symbol !== 'undefined' && Symbol.toStringTag)
Object.defineProperty(self, Symbol.toStringTag, { value: 'Module' });
else
Object.defineProperty(self, 'toString', { value: function () { return '[object Module]' } });
/*#__PURE__*/Object.freeze(self);
});

console.log(Object.keys(self));

Expand Down
10 changes: 3 additions & 7 deletions test/form/samples/namespace-tostringtag/_expected/umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@
(global = global || self, factory(global.iife = {}));
}(this, function (exports) { 'use strict';

var self = {
var self = /*#__PURE__*/Object.freeze({
[Symbol.toStringTag]: 'Module',
get p () { return p; }
};
if (typeof Symbol !== 'undefined' && Symbol.toStringTag)
Object.defineProperty(self, Symbol.toStringTag, { value: 'Module' });
else
Object.defineProperty(self, 'toString', { value: function () { return '[object Module]' } });
/*#__PURE__*/Object.freeze(self);
});

console.log(Object.keys(self));

Expand Down
8 changes: 8 additions & 0 deletions test/function/samples/namespace-tostring/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
description: 'creates Symbol.toStringTag property',
options: {
output: {
namespaceToStringTag: true
}
}
};
1 change: 1 addition & 0 deletions test/function/samples/namespace-tostring/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const bar = 42;
4 changes: 4 additions & 0 deletions test/function/samples/namespace-tostring/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import * as foo from './foo';

assert.equal("" + foo, "[object Module]");
assert.equal(foo.bar, 42);