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

fix: handle non array statements in evaluate helper #846

Merged
merged 4 commits into from May 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
};