Skip to content

Commit

Permalink
Preserve import binding locations during module rewriting (babel#7378)
Browse files Browse the repository at this point in the history
* Only wrap import references that need it.

* Preserve the import binding location for sourcemaps.

* Add tests.
  • Loading branch information
loganfsmyth authored and aminmarashi committed Mar 17, 2018
1 parent 14f3710 commit 625e5b4
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 6 deletions.
Expand Up @@ -181,7 +181,13 @@ const rewriteReferencesVisitor = {
if (importData) {
const ref = buildImportReference(importData, path.node);

if (path.parentPath.isCallExpression({ callee: path.node })) {
// Preserve the binding location so that sourcemaps are nicer.
ref.loc = path.node.loc;

if (
path.parentPath.isCallExpression({ callee: path.node }) &&
t.isMemberExpression(ref)
) {
path.replaceWith(t.sequenceExpression([t.numericLiteral(0), ref]));
} else if (path.isJSXIdentifier() && t.isMemberExpression(ref)) {
const { object, property } = ref;
Expand Down
Expand Up @@ -61,25 +61,49 @@ exports.test2 = test2;
/*after*/

/*before*/
(0, _foo4.bar)
(0,
/*after*/

/*before*/
_foo4
/*after*/
.
/*before*/
bar)
/*after*/
(
/*before*/
_foo2.default
_foo2
/*after*/
.
/*before*/
default
/*after*/
,
/*before*/
_foo5.foo
_foo5
/*after*/
.
/*before*/
foo
/*after*/
);
/* my comment */

/*before*/
_foo5.foo
_foo5
/*after*/
.
/*before*/
foo
/*after*/
;

/*before*/
_foo2.default
_foo2
/*after*/
.
/*before*/
default
/*after*/
;
@@ -0,0 +1,14 @@
import aDefault from "one";
import { aNamed } from "two";
import { orig as anAliased } from "three";
import * as aNamespace from "four";

console.log(aDefault);
console.log(aNamed);
console.log(anAliased);
console.log(aNamespace);

console.log(aDefault());
console.log(aNamed());
console.log(anAliased());
console.log(aNamespace());
@@ -0,0 +1,3 @@
{
"plugins": ["external-helpers", "transform-modules-commonjs"]
}
@@ -0,0 +1,17 @@
"use strict";

var _one = babelHelpers.interopRequireDefault(require("one"));

var _two = require("two");

var _three = require("three");

var aNamespace = babelHelpers.interopRequireWildcard(require("four"));
console.log(_one.default);
console.log(_two.aNamed);
console.log(_three.orig);
console.log(aNamespace);
console.log((0, _one.default)());
console.log((0, _two.aNamed)());
console.log((0, _three.orig)());
console.log(aNamespace());
@@ -0,0 +1,162 @@
[
{
"generated": {
"line": 10,
"column": 12
},
"original": {
"line": 6,
"column": 12
}
},
{
"generated": {
"line": 10,
"column": 23
},
"original": {
"line": 6,
"column": 12
}
},
{
"generated": {
"line": 11,
"column": 12
},
"original": {
"line": 7,
"column": 12
}
},
{
"generated": {
"line": 11,
"column": 22
},
"original": {
"line": 7,
"column": 12
}
},
{
"generated": {
"line": 12,
"column": 12
},
"original": {
"line": 8,
"column": 12
}
},
{
"generated": {
"line": 12,
"column": 22
},
"original": {
"line": 8,
"column": 12
}
},
{
"generated": {
"line": 13,
"column": 12
},
"original": {
"line": 9,
"column": 12
}
},
{
"generated": {
"line": 13,
"column": 21
},
"original": {
"line": 9,
"column": 12
}
},
{
"generated": {
"line": 14,
"column": 16
},
"original": {
"line": 11,
"column": 12
}
},
{
"generated": {
"line": 14,
"column": 27
},
"original": {
"line": 11,
"column": 12
}
},
{
"generated": {
"line": 15,
"column": 16
},
"original": {
"line": 12,
"column": 12
}
},
{
"generated": {
"line": 15,
"column": 26
},
"original": {
"line": 12,
"column": 12
}
},
{
"generated": {
"line": 16,
"column": 16
},
"original": {
"line": 13,
"column": 12
}
},
{
"generated": {
"line": 16,
"column": 26
},
"original": {
"line": 13,
"column": 12
}
},
{
"generated": {
"line": 17,
"column": 12
},
"original": {
"line": 14,
"column": 12
}
},
{
"generated": {
"line": 17,
"column": 21
},
"original": {
"line": 14,
"column": 12
}
}
]

0 comments on commit 625e5b4

Please sign in to comment.