Skip to content

Commit

Permalink
Fix wrong variable name in import expression (#3073)
Browse files Browse the repository at this point in the history
* Create test

* Make sure import expressions are properly rendered
  • Loading branch information
lukastaegert committed Aug 22, 2019
1 parent 0314917 commit 372101f
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/ast/nodes/ImportExpression.ts
Expand Up @@ -54,6 +54,7 @@ export default class Import extends NodeBase {
);
code.overwrite(this.end - 1, this.end, importMechanism.right);
}
this.source.render(code, options);
}

renderFinalResolution(code: MagicString, resolution: string, format: string) {
Expand Down
6 changes: 6 additions & 0 deletions test/form/samples/import-expression/_config.js
@@ -0,0 +1,6 @@
module.exports = {
description: 'correctly transforms variables in imported expressions',
options: {
external: 'external'
}
};
25 changes: 25 additions & 0 deletions test/form/samples/import-expression/_expected/amd.js
@@ -0,0 +1,25 @@
define(['require', 'external'], function (require, external) { 'use strict';

function _interopNamespace(e) {
if (e && e.__esModule) { return e; } else {
var n = {};
if (e) {
Object.keys(e).forEach(function (k) {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () {
return e[k];
}
});
});
}
n['default'] = e;
return n;
}
}

new Promise(function (resolve, reject) { require([external.join('a', 'b')], function (m) { resolve(_interopNamespace(m)); }, reject) });
console.log(external.join);

});
25 changes: 25 additions & 0 deletions test/form/samples/import-expression/_expected/cjs.js
@@ -0,0 +1,25 @@
'use strict';

function _interopNamespace(e) {
if (e && e.__esModule) { return e; } else {
var n = {};
if (e) {
Object.keys(e).forEach(function (k) {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () {
return e[k];
}
});
});
}
n['default'] = e;
return n;
}
}

var external = require('external');

new Promise(function (resolve) { resolve(_interopNamespace(require(external.join('a', 'b')))); });
console.log(external.join);
4 changes: 4 additions & 0 deletions test/form/samples/import-expression/_expected/es.js
@@ -0,0 +1,4 @@
import { join } from 'external';

import(join('a', 'b'));
console.log(join);
7 changes: 7 additions & 0 deletions test/form/samples/import-expression/_expected/iife.js
@@ -0,0 +1,7 @@
(function (external) {
'use strict';

import(external.join('a', 'b'));
console.log(external.join);

}(external));
15 changes: 15 additions & 0 deletions test/form/samples/import-expression/_expected/system.js
@@ -0,0 +1,15 @@
System.register(['external'], function (exports, module) {
'use strict';
var join;
return {
setters: [function (module) {
join = module.join;
}],
execute: function () {

module.import(join('a', 'b'));
console.log(join);

}
};
});
10 changes: 10 additions & 0 deletions test/form/samples/import-expression/_expected/umd.js
@@ -0,0 +1,10 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('external')) :
typeof define === 'function' && define.amd ? define(['external'], factory) :
(global = global || self, factory(global.external));
}(this, function (external) { 'use strict';

import(external.join('a', 'b'));
console.log(external.join);

}));
4 changes: 4 additions & 0 deletions test/form/samples/import-expression/main.js
@@ -0,0 +1,4 @@
import { join as pathJoin } from 'external';

import(pathJoin('a', 'b'));
console.log(pathJoin);

0 comments on commit 372101f

Please sign in to comment.