Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/pages/home/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default function Footer() {
<div className="container container-fluid">
<div className="footer__bottom text--center">
<div className="footer__copyright">
Copyright © 2024 React Navigation. Built with{' '}
Copyright © {new Date().getFullYear()} React Navigation. Built with{' '}
{links.map((link, index) => (
<React.Fragment key={link.href}>
{index > 0 && ', '}
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
Loading