Skip to content

Commit

Permalink
Scope hoisting renaming after babel transforms (#2292)
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic authored and devongovett committed Nov 18, 2018
1 parent 4773d65 commit 0937705
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/core/parcel-bundler/src/scope-hoisting/hoist.js
Expand Up @@ -51,6 +51,8 @@ function hasSideEffects(asset, {sideEffects} = asset._package) {
module.exports = {
Program: {
enter(path, asset) {
path.scope.crawl();

asset.cacheData.imports = asset.cacheData.imports || Object.create(null);
asset.cacheData.exports = asset.cacheData.exports || Object.create(null);
asset.cacheData.wildcards = asset.cacheData.wildcards || [];
Expand Down
4 changes: 2 additions & 2 deletions packages/core/parcel-bundler/src/scope-hoisting/renamer.js
Expand Up @@ -25,9 +25,9 @@ function rename(scope, oldName, newName) {
}

// Rename binding identifier, and update scope.
binding.identifier.name = newName;
scope.removeOwnBinding(oldName);
scope.bindings[newName] = binding;
delete scope.bindings[oldName];
binding.identifier.name = newName;
}

module.exports = rename;
@@ -0,0 +1,3 @@
{
"plugins": ["@babel/plugin-transform-react-jsx"]
}
@@ -0,0 +1,3 @@
import React from "./react.js";

output = <span>Test</span>;
@@ -0,0 +1,9 @@
// mock for React.createElement

export default {
createElement(type, props, children){
return {
type, props, children
};
}
}
@@ -0,0 +1,5 @@
import Superclass from './b';

class Test extends Superclass {}

output = new Test().parentMethod();
@@ -0,0 +1,5 @@
export default class Superclass {
parentMethod(){
return 2;
}
}
24 changes: 24 additions & 0 deletions packages/core/parcel-bundler/test/scope-hoisting.js
Expand Up @@ -89,6 +89,17 @@ describe('scope hoisting', function() {
assert.equal(output, 2);
});

it('supports renaming superclass identifiers', async function() {
let b = await bundle(
path.join(
__dirname,
'/integration/scope-hoisting/es6/rename-superclass/a.js'
)
);
let output = await run(b);
assert.equal(output, 2);
});

it('supports renaming imports', async function() {
let b = await bundle(
path.join(
Expand Down Expand Up @@ -446,6 +457,19 @@ describe('scope hoisting', function() {
assert.deepEqual(output, 'bar');
});

it('should support the jsx pragma', async function() {
let b = await bundle(
path.join(__dirname, '/integration/scope-hoisting/es6/jsx-pragma/a.js')
);

let output = await run(b);
assert.deepEqual(output, {
children: 'Test',
props: null,
type: 'span'
});
});

it('should not nameclash with internal variables', async function() {
let b = await bundle(
path.join(__dirname, '/integration/scope-hoisting/es6/name-clash/a.js')
Expand Down

0 comments on commit 0937705

Please sign in to comment.