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
72 changes: 30 additions & 42 deletions src/__tests__/rehype-static-to-dynamic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1068,9 +1068,7 @@ describe('rehype-static-to-dynamic', () => {
assert.strictEqual(output, expected);
});

test(
'navigator with `.with(...)` merges screenOptions and screenListeners from plain objects',
async () => {
test('navigator with `.with(...)` merges screenOptions and screenListeners from plain objects', async () => {
const input = dedent /* javascript */ `
import { useWindowDimensions } from 'react-native';
import { createDrawerNavigator } from '@react-navigation/drawer';
Expand Down Expand Up @@ -1155,14 +1153,11 @@ describe('rehype-static-to-dynamic', () => {
}
`;

assert.strictEqual(output, expected);
}
);
assert.strictEqual(output, expected);
});

test(
'navigator with `.with(...)` merges static objects with wrapper functions',
async () => {
const input = dedent /* javascript */ `
test('navigator with `.with(...)` merges static objects with wrapper functions', async () => {
const input = dedent /* javascript */ `
import { createDrawerNavigator } from '@react-navigation/drawer';
import { createStaticNavigation } from '@react-navigation/native';

Expand Down Expand Up @@ -1196,13 +1191,13 @@ describe('rehype-static-to-dynamic', () => {
}
`;

const tree = createTestTree(input);
const plugin = rehypeStaticToDynamic();
await plugin(tree);
const tree = createTestTree(input);
const plugin = rehypeStaticToDynamic();
await plugin(tree);

const output = extractTransformedCode(tree);
const output = extractTransformedCode(tree);

const expected = dedent /* javascript */ `
const expected = dedent /* javascript */ `
import { createDrawerNavigator } from '@react-navigation/drawer';
import { NavigationContainer } from '@react-navigation/native';

Expand Down Expand Up @@ -1234,14 +1229,11 @@ describe('rehype-static-to-dynamic', () => {
}
`;

assert.strictEqual(output, expected);
}
);
assert.strictEqual(output, expected);
});

test(
'navigator with `.with(...)` merges static functions with wrapper objects',
async () => {
const input = dedent /* javascript */ `
test('navigator with `.with(...)` merges static functions with wrapper objects', async () => {
const input = dedent /* javascript */ `
import { createDrawerNavigator } from '@react-navigation/drawer';
import { createStaticNavigation } from '@react-navigation/native';

Expand Down Expand Up @@ -1275,13 +1267,13 @@ describe('rehype-static-to-dynamic', () => {
}
`;

const tree = createTestTree(input);
const plugin = rehypeStaticToDynamic();
await plugin(tree);
const tree = createTestTree(input);
const plugin = rehypeStaticToDynamic();
await plugin(tree);

const output = extractTransformedCode(tree);
const output = extractTransformedCode(tree);

const expected = dedent /* javascript */ `
const expected = dedent /* javascript */ `
import { createDrawerNavigator } from '@react-navigation/drawer';
import { NavigationContainer } from '@react-navigation/native';

Expand Down Expand Up @@ -1313,14 +1305,11 @@ describe('rehype-static-to-dynamic', () => {
}
`;

assert.strictEqual(output, expected);
}
);
assert.strictEqual(output, expected);
});

test(
'navigator with `.with(...)` merges screenOptions and screenListeners when both are functions',
async () => {
const input = dedent /* javascript */ `
test('navigator with `.with(...)` merges screenOptions and screenListeners when both are functions', async () => {
const input = dedent /* javascript */ `
import { createDrawerNavigator } from '@react-navigation/drawer';
import { createStaticNavigation } from '@react-navigation/native';

Expand Down Expand Up @@ -1354,13 +1343,13 @@ describe('rehype-static-to-dynamic', () => {
}
`;

const tree = createTestTree(input);
const plugin = rehypeStaticToDynamic();
await plugin(tree);
const tree = createTestTree(input);
const plugin = rehypeStaticToDynamic();
await plugin(tree);

const output = extractTransformedCode(tree);
const output = extractTransformedCode(tree);

const expected = dedent /* javascript */ `
const expected = dedent /* javascript */ `
import { createDrawerNavigator } from '@react-navigation/drawer';
import { NavigationContainer } from '@react-navigation/native';

Expand Down Expand Up @@ -1392,9 +1381,8 @@ describe('rehype-static-to-dynamic', () => {
}
`;

assert.strictEqual(output, expected);
}
);
assert.strictEqual(output, expected);
});

test('drawer navigator', async () => {
const input = dedent /* javascript */ `
Expand Down
22 changes: 14 additions & 8 deletions src/plugins/rehype-static-to-dynamic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,11 @@ export async function convertStaticToDynamic(code: string): Promise<string> {
}

const navigatorVariable = declarator.id.name; // e.g., "MyStack"
const { type: navigatorType, config, withCallback } =
navigatorCallInfo;
const {
type: navigatorType,
config,
withCallback,
} = navigatorCallInfo;

navigatorInfos.push({
originalName: navigatorVariable,
Expand Down Expand Up @@ -1388,10 +1391,7 @@ function createJsxAttributeFromPropValue(key: string, value: t.Expression) {
return t.jsxAttribute(t.jsxIdentifier(key), t.stringLiteral(value.value));
}

return t.jsxAttribute(
t.jsxIdentifier(key),
t.jsxExpressionContainer(value)
);
return t.jsxAttribute(t.jsxIdentifier(key), t.jsxExpressionContainer(value));
}

function getJsxAttributeName(
Expand Down Expand Up @@ -1692,7 +1692,9 @@ function createFallbackMergedFunctionExpression(
[argIdentifier],
t.objectExpression([
t.spreadElement(createSpreadSourceExpression(staticValue, argIdentifier)),
t.spreadElement(createSpreadSourceExpression(dynamicValue, argIdentifier)),
t.spreadElement(
createSpreadSourceExpression(dynamicValue, argIdentifier)
),
])
);
}
Expand Down Expand Up @@ -1733,7 +1735,11 @@ function createMergedNavigatorPropExpression(
const params = getMergedFunctionParams(staticValue, dynamicValue);

if (staticObject && dynamicObject && params) {
return createMergedFunctionExpression(params, staticObject, dynamicObject);
return createMergedFunctionExpression(
params,
staticObject,
dynamicObject
);
}

return createFallbackMergedFunctionExpression(staticValue, dynamicValue);
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11056,9 +11056,9 @@ __metadata:
linkType: hard

"picomatch@npm:^2.0.4, picomatch@npm:^2.2.1, picomatch@npm:^2.2.3, picomatch@npm:^2.3.1":
version: 2.3.1
resolution: "picomatch@npm:2.3.1"
checksum: 26c02b8d06f03206fc2ab8d16f19960f2ff9e81a658f831ecb656d8f17d9edc799e8364b1f4a7873e89d9702dff96204be0fa26fe4181f6843f040f819dac4be
version: 2.3.2
resolution: "picomatch@npm:2.3.2"
checksum: a554d1709e59be97d1acb9eaedbbc700a5c03dbd4579807baed95100b00420bc729335440ef15004ae2378984e2487a7c1cebd743cfdb72b6fa9ab69223c0d61
languageName: node
linkType: hard

Expand Down
Loading