Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions packages/babel-plugin-minify-simplify/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -490,16 +490,6 @@ module.exports = ({ types: t }) => {
return;
}
node.body = statements;

// this additional traversal is horrible but it's done to fix
// https://github.com/babel/minify/issues/323
// in which type annotation somehow gets messed up
// during sequence expression transformation
path.traverse({
Identifier: function(path) {
path.getTypeAnnotation();
}
});
},

BlockStatement: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,67 @@
"use strict";

module.exports = function() {
module.exports = function({ types: t }) {
// custom implementation of getTypeAnnotation that fixes
// the type information that is lost during sequence expression transformation
// https://github.com/babel/minify/issues/323
function customTypeAnnotation(path) {
if (path.typeAnnotation) {
return path.typeAnnotation;
}

// We are not handling the case of literals and other base types
// since they are already handled via getTypeAnnotation
const { node } = path;
const binding = path.parentPath.scope.getBinding(node.name);

const types = [];
if (binding && binding.constantViolations) {
if (binding.identifier.typeAnnotation) {
return binding.identifier.typeAnnotation;
}
if (binding.constantViolations) {
const violations = binding.constantViolations;
for (let violation of violations) {
types.push(violation.getTypeAnnotation());
}
}
}

if (types.length > 0) {
return t.createUnionTypeAnnotation(types);
}
return types;
}

// Based on the type inference in Babel
function baseTypeStrictlyMatches(left, right) {
let leftTypes, rightTypes;

if (t.isIdentifier(left)) {
leftTypes = customTypeAnnotation(left);
} else if (t.isIdentifier(right)) {
rightTypes = customTypeAnnotation(right);
}

// Early exit
if (t.isAnyTypeAnnotation(leftTypes) || t.isAnyTypeAnnotation(rightTypes)) {
return false;
}

leftTypes = [].concat(leftTypes, left.getTypeAnnotation());
rightTypes = [].concat(rightTypes, right.getTypeAnnotation());

leftTypes = t.createUnionTypeAnnotation(leftTypes);
rightTypes = t.createUnionTypeAnnotation(rightTypes);

if (
!t.isAnyTypeAnnotation(leftTypes) &&
t.isFlowBaseAnnotation(leftTypes)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this affects the babel-7 upgrade. I think we should test this against Babel7. What do you think?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

) {
return leftTypes.type === rightTypes.type;
}
}

return {
name: "transform-simplify-comparison-operators",
visitor: {
Expand All @@ -15,7 +76,8 @@ module.exports = function() {

const left = path.get("left");
const right = path.get("right");
const strictMatch = left.baseTypeStrictlyMatches(right);

const strictMatch = baseTypeStrictlyMatches(left, right);
if (strictMatch) {
node.operator = node.operator.slice(0, -1);
}
Expand Down