From e862415430ffba5e61107820e3a220c48e1bad4f Mon Sep 17 00:00:00 2001 From: Phil Pluckthun Date: Fri, 19 Jan 2024 13:42:04 +0000 Subject: [PATCH 1/3] Add additional tests for __DEV__ identifiers --- .../src/__tests__/inline-plugin-test.js | 125 +++++++++++++----- 1 file changed, 93 insertions(+), 32 deletions(-) diff --git a/packages/metro-transform-plugins/src/__tests__/inline-plugin-test.js b/packages/metro-transform-plugins/src/__tests__/inline-plugin-test.js index 9f59b3b2ec..5f4c318b77 100644 --- a/packages/metro-transform-plugins/src/__tests__/inline-plugin-test.js +++ b/packages/metro-transform-plugins/src/__tests__/inline-plugin-test.js @@ -16,45 +16,106 @@ const inlinePlugin = require('../inline-plugin'); const stripFlow = require('@babel/plugin-transform-flow-strip-types'); describe('inline constants', () => { - it('replaces __DEV__ in the code', () => { - const code = ` - function a() { - var a = __DEV__ ? 1 : 2; - var b = a.__DEV__; - var c = function __DEV__(__DEV__) {}; - } - `; + describe('__DEV__', () => { + it('replaces __DEV__ in the code', () => { + const code = ` + function a() { + var a = __DEV__ ? 1 : 2; + var b = a.__DEV__; + var c = function __DEV__(__DEV__) {}; + } + `; - compare([inlinePlugin], code, code.replace(/__DEV__/, 'false'), { - dev: false, + compare([inlinePlugin], code, code.replace(/__DEV__/, 'false'), { + dev: false, + }); }); - }); - it("doesn't replace a local __DEV__ variable", () => { - const code = ` - function a() { - var __DEV__ = false; - var a = __DEV__ ? 1 : 2; - } - `; + it("doesn't replace a local __DEV__ variable", () => { + const code = ` + function a() { + var __DEV__ = false; + var a = __DEV__ ? 1 : 2; + } + `; - compare([inlinePlugin], code, code, {dev: false}); - }); + compare([inlinePlugin], code, code, {dev: false}); + }); - it("doesn't replace __DEV__ in an object property key", () => { - const code = ` - const x = { - __DEV__: __DEV__ - }; - `; + it("doesn't replace __DEV__ in an object property key", () => { + const code = ` + const x = { __DEV__: __DEV__ }; + `; - const expected = ` - const x = { - __DEV__: false - }; - `; + const expected = ` + const x = { __DEV__: false }; + `; + + compare([inlinePlugin], code, expected, {dev: false}); + }); + + it("doesn't replace __DEV__ in an object shorthand method name", () => { + const code = ` + const x = { + __DEV__() { return __DEV__; }, + }; + `; + + const expected = ` + const x = { + __DEV__() { return false; }, + }; + `; + + compare([inlinePlugin], code, expected, {dev: false}); + }); + + it("doesn't replace __DEV__ as a label of a block statement", () => { + const code = ` + __DEV__: { + break __DEV__; + }; + `; + + compare([inlinePlugin], code, code, {dev: false}); + }); + + it("doesn't replace __DEV__ as the name of a function declaration or call", () => { + const code = ` + function __DEV__() { return false; } + const isDev = __DEV__(); + `; + + compare([inlinePlugin], code, code, {dev: false}); + }); + + it("doesn't replace __DEV__ as the name of a class method", () => { + const code = ` + class Test { + __DEV__() {} + } + `; + + compare([inlinePlugin], code, code, {dev: false}); + }); - compare([inlinePlugin], code, expected, {dev: false}); + it("doesn't replace __DEV__ as the name of an export alias", () => { + const code = ` + const dev = true; + export { dev as __DEV__ }; + `; + + compare([inlinePlugin], code, code, {dev: false}); + }); + + it("doesn't replace __DEV__ as the name of an optional property access", () => { + const code = ` + const dev = true; + export { dev as __DEV__ }; + `; + + compare([inlinePlugin], code, code, {dev: false}); + }); }); it('replaces Platform.OS in the code if Platform is a global', () => { From cf7faef0479afeed583c1f305498743959fbf90b Mon Sep 17 00:00:00 2001 From: Phil Pluckthun Date: Fri, 19 Jan 2024 13:42:17 +0000 Subject: [PATCH 2/3] Swap Identifier with ReferencedIdentifier visit for __DEV__ replacement --- .../src/__tests__/inline-plugin-test.js | 4 ++-- packages/metro-transform-plugins/src/inline-plugin.js | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/metro-transform-plugins/src/__tests__/inline-plugin-test.js b/packages/metro-transform-plugins/src/__tests__/inline-plugin-test.js index 5f4c318b77..0c503a0103 100644 --- a/packages/metro-transform-plugins/src/__tests__/inline-plugin-test.js +++ b/packages/metro-transform-plugins/src/__tests__/inline-plugin-test.js @@ -110,8 +110,8 @@ describe('inline constants', () => { it("doesn't replace __DEV__ as the name of an optional property access", () => { const code = ` - const dev = true; - export { dev as __DEV__ }; + x?.__DEV__; + x?.__DEV__(); `; compare([inlinePlugin], code, code, {dev: false}); diff --git a/packages/metro-transform-plugins/src/inline-plugin.js b/packages/metro-transform-plugins/src/inline-plugin.js index 58c18be9be..470bd93943 100644 --- a/packages/metro-transform-plugins/src/inline-plugin.js +++ b/packages/metro-transform-plugins/src/inline-plugin.js @@ -15,7 +15,6 @@ import type {PluginObj} from '@babel/core'; import type {Binding, NodePath, Scope} from '@babel/traverse'; import type { CallExpression, - Identifier, MemberExpression, Node, ObjectExpression, @@ -86,7 +85,7 @@ function inlinePlugin( isIdentifier(node.object.object, processId) && isGlobal(scope.getBinding(processId.name)); - const isDev = (node: Identifier, parent: Node, scope: Scope): boolean => + const isDev = (node: Node, parent: Node, scope: Scope): boolean => isIdentifier(node, dev) && isGlobalOrFlowDeclared(scope.getBinding(dev.name)) && !isMemberExpression(parent) && @@ -136,7 +135,7 @@ function inlinePlugin( return { visitor: { - Identifier(path: NodePath, state: State): void { + ReferencedIdentifier(path: NodePath, state: State): void { if (!state.opts.dev && isDev(path.node, path.parent, path.scope)) { path.replaceWith(t.booleanLiteral(state.opts.dev)); } From 7857c4dcbeaf2d08ee29f54f3ae3a690b9f424aa Mon Sep 17 00:00:00 2001 From: Phil Pluckthun Date: Fri, 19 Jan 2024 13:54:12 +0000 Subject: [PATCH 3/3] Remove redundant object property & member checks from isDev --- packages/metro-transform-plugins/src/inline-plugin.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/metro-transform-plugins/src/inline-plugin.js b/packages/metro-transform-plugins/src/inline-plugin.js index 470bd93943..e3055db25f 100644 --- a/packages/metro-transform-plugins/src/inline-plugin.js +++ b/packages/metro-transform-plugins/src/inline-plugin.js @@ -87,10 +87,7 @@ function inlinePlugin( const isDev = (node: Node, parent: Node, scope: Scope): boolean => isIdentifier(node, dev) && - isGlobalOrFlowDeclared(scope.getBinding(dev.name)) && - !isMemberExpression(parent) && - // not { __DEV__: 'value'} - (!isObjectProperty(parent) || parent.value === node); + isGlobalOrFlowDeclared(scope.getBinding(dev.name)); function findProperty( objectExpression: ObjectExpression,