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

Scope hoisting renaming after babel transforms #2292

Merged
merged 3 commits into from
Nov 18, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 1 addition & 25 deletions packages/core/parcel-bundler/src/scope-hoisting/renamer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,7 @@ function rename(scope, oldName, newName) {
return;
}

let binding = scope.getBinding(oldName);

// Rename all constant violations
for (let violation of binding.constantViolations) {
let bindingIds = violation.getBindingIdentifierPaths(true, false);
for (let name in bindingIds) {
if (name === oldName) {
for (let idPath of bindingIds[name]) {
idPath.node.name = newName;
}
}
}
}

// Rename all references
for (let path of binding.referencePaths) {
if (path.node.name === oldName) {
path.node.name = newName;
}
}

// Rename binding identifier, and update scope.
binding.identifier.name = newName;
scope.bindings[newName] = binding;
delete scope.bindings[oldName];
scope.rename(oldName, newName);
mischnic marked this conversation as resolved.
Show resolved Hide resolved
}

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

output = <span>Test</span>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// mock for React.createElement

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

class Test extends Superclass {}

output = new Test().parentMethod();
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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