Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(babel): add more language tests #27709

Merged
merged 10 commits into from Apr 12, 2024
1 change: 1 addition & 0 deletions packages/babel-preset-expo/CHANGELOG.md
Expand Up @@ -19,6 +19,7 @@

### 💡 Others

- Add more tests for obscure syntax used in Expo apps. ([#27709](https://github.com/expo/expo/pull/27709) by [@EvanBacon](https://github.com/EvanBacon))
- Relax forbidden React server API errors to better support shared components. ([#27878](https://github.com/expo/expo/pull/27878) by [@EvanBacon](https://github.com/EvanBacon))
- Reset env in tests. ([#27950](https://github.com/expo/expo/pull/27950) by [@EvanBacon](https://github.com/EvanBacon))
- Add Hermes language support tests. ([#27900](https://github.com/expo/expo/pull/27900) by [@EvanBacon](https://github.com/EvanBacon))
Expand Down
@@ -0,0 +1,36 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`allows destructuring in the catch block 1`] = `
"Object.defineProperty(exports, "__esModule", { value: true });exports.App = App;var _jsxRuntime = require("react/jsx-runtime");function App() {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
var foo = function () {
try {
console.log('hey');
} catch (_ref) {var message = _ref.message;
// NOTE(EvanBacon): React compiler throws:
// (BuildHIR::lowerAssignment) Could not find binding for declaration.
console.error(message);
}
};
return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", { children: "Hello" });
}"
`;

exports[`supports functions with discarded return values in try/catch blocks that run in memos 1`] = `
"Object.defineProperty(exports, "__esModule", { value: true });exports.useSideEffectMayThrow = useSideEffectMayThrow;var _react = require("react");

function SIDE_EFFECT_MAY_THROW() {}

// NOTE(EvanBacon): React compiler throws:
// Cannot read properties of undefined (reading 'preds')
function useSideEffectMayThrow() {
return (0, _react.useMemo)(function () {
try {
SIDE_EFFECT_MAY_THROW();
return true;
} catch {
return false;
}
}, []);
}"
`;
38 changes: 38 additions & 0 deletions packages/babel-preset-expo/src/__tests__/compiler.test.ts
@@ -0,0 +1,38 @@
import * as babel from '@babel/core';
import * as path from 'node:path';

import preset from '..';

function getCaller(props: Record<string, string | boolean>): babel.TransformCaller {
return props as unknown as babel.TransformCaller;
}

const options = {
caller: getCaller({ name: 'metro', engine: 'hermes', platform: 'ios', isDev: false }),
babelrc: false,
presets: [preset],
sourceMaps: false,

compact: false,
comments: true,
retainLines: true,
};

it(`allows destructuring in the catch block`, () => {
// Ensuring the transform doesn't throw.
const { code } = babel.transformFileSync(
path.resolve(__dirname, 'samples/destructure-catch.tsx'),
options
)!;

expect(code).toMatchSnapshot();
});
it(`supports functions with discarded return values in try/catch blocks that run in memos`, () => {
// Ensuring the transform doesn't throw.
const { code } = babel.transformFileSync(
path.resolve(__dirname, 'samples/try-catch-hook.tsx'),
options
)!;

expect(code).toMatchSnapshot();
});
@@ -0,0 +1,13 @@
export function App() {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const foo = () => {
try {
console.log('hey');
} catch ({ message }) {
// NOTE(EvanBacon): React compiler throws:
// (BuildHIR::lowerAssignment) Could not find binding for declaration.
console.error(message);
}
};
return <div>Hello</div>;
}
@@ -0,0 +1,16 @@
import { useMemo } from 'react';

function SIDE_EFFECT_MAY_THROW() {}

// NOTE(EvanBacon): React compiler throws:
// Cannot read properties of undefined (reading 'preds')
export function useSideEffectMayThrow() {
return useMemo(() => {
try {
SIDE_EFFECT_MAY_THROW();
return true;
} catch {
return false;
}
}, []);
}