Skip to content

Commit

Permalink
fix: Fix env expressions when used as an object property value.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Dec 29, 2020
1 parent 949a143 commit c09880a
Showing 1 changed file with 29 additions and 32 deletions.
61 changes: 29 additions & 32 deletions src/babel/plugins/envExpressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,36 @@ export default function envExpressions() {
Identifier: {
enter(path: NodePath<t.Identifier>) {
Object.entries(exprs).forEach(([expr, [op, env]]) => {
if (
path.isIdentifier({ name: expr }) &&
// const __DEV__ = var;
!path.parentPath.isVariableDeclarator() &&
// { __DEV__: var }
!path.parentPath.isObjectProperty() &&
// obj.__DEV__ = var;
!path.parentPath.isMemberExpression()
// // if (__DEV__)
// (path.parentPath.isIfStatement() ||
// // switch(__DEV__)
// path.parentPath.isSwitchStatement() ||
// // __DEV__ && var
// path.parentPath.isLogicalExpression() ||
// // __DEV__ ? a : b
// path.parentPath.isConditionalExpression() ||
// // !__DEV__
// path.parentPath.isUnaryExpression() ||
// // prop={__DEV__}
// path.parentPath.isJSXExpressionContainer())
) {
path.replaceWith(
t.binaryExpression(
op as '===',
t.memberExpression(
t.memberExpression(t.identifier('process'), t.identifier('env'), false),
t.identifier('NODE_ENV'),
false,
),
t.stringLiteral(env),
),
);
if (!path.isIdentifier({ name: expr })) {
return;
}

// const __DEV__ = var;
if (path.parentPath.isVariableDeclarator()) {
return;
}

// { __DEV__: var }, { [__DEV__]: var }
if (path.parentPath.isObjectProperty() && path.parentPath.node.value !== path.node) {
return;
}

// obj.__DEV__ = var;
if (path.parentPath.isMemberExpression()) {
return;
}

path.replaceWith(
t.binaryExpression(
op as '===',
t.memberExpression(
t.memberExpression(t.identifier('process'), t.identifier('env'), false),
t.identifier('NODE_ENV'),
false,
),
t.stringLiteral(env),
),
);
});
},
},
Expand Down

0 comments on commit c09880a

Please sign in to comment.