Skip to content

Commit

Permalink
fix: remove babel types and handle tdz binding
Browse files Browse the repository at this point in the history
  • Loading branch information
vigneshshanmugam committed May 14, 2018
1 parent 8ea0709 commit 6c7196a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
27 changes: 15 additions & 12 deletions packages/babel-helper-evaluate-path/src/index.js
@@ -1,7 +1,5 @@
"use strict";

const t = require("@babel/types");

module.exports = function evaluate(path, { tdz = false } = {}) {
if (!tdz && !path.isReferencedIdentifier()) {
return baseEvaluate(path);
Expand Down Expand Up @@ -122,14 +120,15 @@ function evaluateBasedOnControlFlow(binding, refPath) {
// early-exit
const declaration = binding.path.parentPath;

if (declaration.parentPath.removed) {
return { confident: true, value: void 0 };
}

if (
t.isIfStatement(declaration.parentPath) ||
t.isLoop(declaration.parentPath) ||
t.isSwitchCase(declaration.parentPath)
declaration.parentPath.isIfStatement() ||
declaration.parentPath.isLoop() ||
declaration.parentPath.isSwitchCase()
) {
if (declaration.parentPath.removed) {
return { confident: true, value: void 0 };
}
return { shouldDeopt: true };
}

Expand Down Expand Up @@ -181,9 +180,10 @@ function evaluateBasedOnControlFlow(binding, refPath) {
const declaration = declarator.parentPath;

if (
declaration.parentPath.isIfStatement() ||
declaration.parentPath.isLoop() ||
declaration.parentPath.isSwitchCase()
declaration.parentPath &&
(declaration.parentPath.isIfStatement() ||
declaration.parentPath.isLoop() ||
declaration.parentPath.isSwitchCase())
) {
return { shouldDeopt: true };
}
Expand All @@ -194,7 +194,10 @@ function evaluateBasedOnControlFlow(binding, refPath) {
}

// Detect Usage before Init
const stmts = scopePath.get("body");
let stmts = scopePath.get("body");
if (!Array.isArray(stmts)) {
stmts = [stmts];
}

const compareResult = compareBindingAndReference({
binding,
Expand Down
15 changes: 15 additions & 0 deletions packages/babel-preset-minify/__tests__/minify-env-tests.js
Expand Up @@ -251,4 +251,19 @@ describe("preset along with env", () => {
}
`
);
thePlugin(
"should fix issue#844",
`
class MyComponent {
}
MyComponent.propTypes = {
userName: 123
}
`,
`
"use strict";
class a {}
a.propTypes = { userName: 123 };
`
);
});

0 comments on commit 6c7196a

Please sign in to comment.