diff --git a/.config/eslint.config.mjs b/.config/eslint.config.mjs index 7836a30b..c5fc528e 100644 --- a/.config/eslint.config.mjs +++ b/.config/eslint.config.mjs @@ -65,4 +65,20 @@ export default tseslint.config( "@typescript-eslint/no-unsafe-member-access": "off", }, }, + // Enforce new members automatically via linting rather than tests + // An optional prop this package adds to a React Native interface takes `| undefined`, as React Native's own do, so it accepts an explicit undefined under exactOptionalPropertyTypes + { + files: ["types.d.ts"], + rules: { + "no-restricted-syntax": [ + "error", + { + selector: + "TSModuleDeclaration TSInterfaceBody > TSPropertySignature[optional=true] > TSTypeAnnotation > :not(TSUnionType:has(> TSUndefinedKeyword))", + message: + "Write `?: T | undefined`: under exactOptionalPropertyTypes a bare `?: T` rejects the explicit undefined React Native's own props accept.", + }, + ], + }, + }, ); diff --git a/src/__tests__/native/styled.test.ios.tsx b/src/__tests__/native/styled.test.ios.tsx index 3f4ef620..21e55876 100644 --- a/src/__tests__/native/styled.test.ios.tsx +++ b/src/__tests__/native/styled.test.ios.tsx @@ -163,6 +163,25 @@ test.each(["nativeStyleMapping", "nativeStyleToProp"] as const)( }, ); +test("a mapping entry set to undefined maps nothing", () => { + registerCSS(`.subject { color: blue; background-color: red; }`); + const StyledView = styled(View, { + className: { + target: "style", + nativeStyleMapping: { + color: "accessibilityLabel", + backgroundColor: undefined, + }, + }, + }); + + render(); + + const props = screen.getByTestId(testID).props; + expect(props.accessibilityLabel).toBe("#00f"); + expect(props.style).toEqual({ backgroundColor: "#f00" }); +}); + test.each([false, true])( "current mapping wins over deprecated alias (empty: %s)", (empty) => { diff --git a/src/__tests__/types/exact-optional.test.ts b/src/__tests__/types/exact-optional.test.ts new file mode 100644 index 00000000..46fd9391 --- /dev/null +++ b/src/__tests__/types/exact-optional.test.ts @@ -0,0 +1,133 @@ +import { join } from "node:path"; + +import ts from "typescript"; + +const root = join(__dirname, "../../..").replaceAll("\\", "/"); +const fixtureName = `${root}/src/__tests__/types/exact-optional.fixture.tsx`; + +// Writes an explicit undefined to each optional member a consumer writes +const fixture = ` +import createDebug from "debug"; +import type { MetroConfig } from "metro-config"; +import { + Button, + FlatList, + Image, + ImageBackground, + KeyboardAvoidingView, + ScrollView, + Switch, + Text, + TouchableWithoutFeedback, + View, +} from "react-native"; +import { styled } from "react-native-css"; +import { compile } from "react-native-css/compiler"; +import { compileWithAutoDebug, registerCSS } from "react-native-css/jest"; +import { withReactNativeCSS } from "react-native-css/metro"; +import type { Config } from "react-native-css/native/react/useNativeCss"; +import type { Effect } from "react-native-css/native/reactivity"; + +declare const maybe: string | undefined; +declare const metroConfig: MetroConfig; + +const StyledView = styled(View); +const MappedView = styled( + View, + { + className: { + target: "style", + nativeStyleMapping: { color: "accessibilityLabel", backgroundColor: undefined }, + }, + contentClassName: { target: "style", nativeStyleMapping: undefined }, + }, + { passThrough: undefined }, +); + +export const elements = [ + , + , + , + , + , +