Skip to content

Commit

Permalink
fix: handle non array statements in evaluate helper (#846)
Browse files Browse the repository at this point in the history
* fix: handle non array statements in evaluate helper

* add babel-types deps

* use path.is check

* remove babel-types dep
  • Loading branch information
vigneshshanmugam authored and boopathi committed May 14, 2018
1 parent 8ea0709 commit 91ed362
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
32 changes: 20 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,20 @@ function evaluateBasedOnControlFlow(binding, refPath) {
// early-exit
const declaration = binding.path.parentPath;

/**
* Handle when binding is created inside a parent block and
* the corresponding parent is removed by other plugins
* if (false) { var a } -> var a
*/
if (declaration.parentPath && 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 +185,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 +199,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
@@ -0,0 +1,2 @@
class MyComponent {}
MyComponent.propTypes = { userName: 123 };
@@ -0,0 +1,5 @@
class MyComponent {}

MyComponent.propTypes = {
userName: 123
};

0 comments on commit 91ed362

Please sign in to comment.