Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ describe('inline constants', () => {
var ReactNative = require('react-native');

function a() {
var a = ReactNative.Plaftform.select({ios: 1, android: 2});
var a = ReactNative.Platform.select({ios: 1, android: 2});
var b = a.ReactNative.Platform.select;
}
`;
Expand Down Expand Up @@ -888,4 +888,97 @@ describe('inline constants', () => {

compare([stripFlow, inlinePlugin], code, expected, {dev: false});
});

test('replaces _react.Platform.OS in the code if _react is a top level import', () => {
const code = `
var _react = require('react-native');

function a() {
var a = _react.Platform.OS;
var b = a._react.Platform.OS;
}
`;

compare(
[inlinePlugin],
code,
code.replace(/_react\.Platform\.OS/, '"ios"'),
{
inlinePlatform: 'true',
platform: 'ios',
},
);
});

test('replaces _reactNative.Platform.OS in the code if _reactNative is a top level import', () => {
const code = `
var _reactNative = require('react-native');

function a() {
var a = _reactNative.Platform.OS;
var b = a._reactNative.Platform.OS;
}
`;

compare(
[inlinePlugin],
code,
code.replace(/_reactNative\.Platform\.OS/, '"ios"'),
{inlinePlatform: true, platform: 'ios'},
);
});

test('replaces _reactNative.Platform.select in the code if _reactNative is a top level import', () => {
const code = `
var _reactNative = require('react-native');

function a() {
var a = _reactNative.Platform.select({ios: 1, android: 2});
var b = a._reactNative.Platform.select;
}
`;

compare(
[inlinePlugin],
code,
code.replace(/_reactNative\.Platform\.select[^;]+/, '2'),
{inlinePlatform: true, platform: 'android'},
);
});

test('replaces _react.Platform.select in the code if _react is a top level import', () => {
const code = `
var _react = require('react-native');

function a() {
var a = _react.Platform.select({ios: 1, android: 2});
var b = a._react.Platform.select;
}
`;

compare(
[inlinePlugin],
code,
code.replace(/_react\.Platform\.select[^;]+/, '2'),
{inlinePlatform: true, platform: 'android'},
);
});

test('replaces abracadabra in the code if abracadabra is react-native default import', () => {
const code = `
var abracadabra = require('react-native');

function a() {
var a = abracadabra.Platform.select({ios: 1, android: 2});
var b = a._react.Platform.select;
}
`;

compare(
[inlinePlugin],
code,
code.replace(/abracadabra\.Platform\.select[^;]+/, '2'),
{inlinePlatform: true, platform: 'android'},
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,26 @@ function createInlinePlatformChecks(
node: BabelNodeExpression,
scope: Scope,
patterns: Array<{name: string}>,
): boolean =>
patterns.some((pattern: {name: string}) => {
): boolean => {
if (isMemberExpression(node) && 'name' in node.object) {
const binding = scope.getBinding(node.object.name);
if (
binding &&
binding.path.isVariableDeclarator() &&
isCallExpression(binding.path.node.init) &&
isIdentifier(binding.path.node.init.callee, {name: requireName}) &&
binding.path.node.init?.arguments &&
checkRequireArgs(binding.path.node.init.arguments, 'react-native')
) {
return true;
}
}

return patterns.some((pattern: {name: string}) => {
const importName = importMap.get(pattern.name) || pattern.name;
return isRequireCall(node, importName, scope);
});
};

const isImportOrGlobal = (
node: BabelNodeExpression,
Expand Down