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 @@ -17,6 +17,7 @@

### 💡 Others

- Add more tests for obscure syntax used in Expo apps.
- Remove unused peer dependency on `@babel/preset-env`. ([#27705](https://github.com/expo/expo/pull/27705) by [@EvanBacon](https://github.com/EvanBacon))
- Disable color in snapshot tests in CI. ([#27301](https://github.com/expo/expo/pull/27301) by [@EvanBacon](https://github.com/EvanBacon))
- Add additional tests for undefined platform minification behavior. ([#27515](https://github.com/expo/expo/pull/27515) by [@EvanBacon](https://github.com/EvanBacon))
Expand Down
@@ -0,0 +1,36 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`compiler allows destructuring in the catch block 1`] = `
"Object.defineProperty(exports, "__esModule", { value: true });exports.App = App;var _jsxDevRuntime = require("react/jsx-dev-runtime");var _jsxFileName = "/Users/evanbacon/Documents/GitHub/expo/packages/babel-preset-expo/src/__tests__/samples/destructure-catch.tsx";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, _jsxDevRuntime.jsxDEV)("div", { children: "Hello" }, void 0, false, { fileName: _jsxFileName, lineNumber: 12, columnNumber: 10 }, this);
}"
`;

exports[`compiler 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: true }),
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;
}
}, []);
}