Skip to content

Commit bacb4eb

Browse files
jquensetaion
authored andcommitted
fix: Fix edge cases with __DEV__ (#8)
1 parent fc63149 commit bacb4eb

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

__tests__/dev-expression-test.js

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,46 @@ describe('dev-expression', function() {
3535
process.env.NODE_ENV = oldEnv;
3636
});
3737

38-
it('should replace __DEV__ in if', () => {
39-
compare(
40-
`
38+
describe('__DEV__', () => {
39+
it('should replace __DEV__ in if', () => {
40+
compare(
41+
`
42+
if (__DEV__) {
43+
console.log('foo')
44+
}`,
45+
`if (process.env.NODE_ENV !== "production") {
46+
console.log('foo');
47+
}`
48+
);
49+
});
50+
51+
it('should not replace locally-defined __DEV__', () => {
52+
compare(
53+
`
54+
const __DEV__ = false;
55+
56+
if (__DEV__) {
57+
console.log('foo')
58+
}`,
59+
`const __DEV__ = false;
60+
4161
if (__DEV__) {
42-
console.log('foo')
43-
}`,
44-
`if (process.env.NODE_ENV !== "production") {
4562
console.log('foo');
4663
}`
47-
);
64+
);
65+
});
66+
67+
it('should not replace object key', () => {
68+
compare(
69+
`
70+
const foo = {
71+
__DEV__: 'hey',
72+
}`,
73+
`const foo = {
74+
__DEV__: 'hey'
75+
};`
76+
);
77+
});
4878
});
4979

5080
it('should replace warning calls', () => {

dev-expression.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@ module.exports = function(babel) {
3434
if (process.env.NODE_ENV === 'test') {
3535
return;
3636
}
37-
// replace __DEV__ with process.env.NODE_ENV !== 'production'
38-
if (path.isIdentifier({name: '__DEV__'})) {
37+
// replace global __DEV__ with process.env.NODE_ENV !== 'production'
38+
if (
39+
path.isIdentifier({name: '__DEV__'}) &&
40+
path.scope.hasGlobal('__DEV__')
41+
) {
3942
path.replaceWith(DEV_EXPRESSION);
4043
}
4144
},

0 commit comments

Comments
 (0)