fix eslint errors

This commit is contained in:
Marc Rousavy 2021-02-19 16:23:54 +01:00
parent 763b4d5372
commit efc655f78f
2 changed files with 56 additions and 103 deletions

View File

@ -43,7 +43,16 @@ module.exports = {
'prettier/prettier': ['warn'], 'prettier/prettier': ['warn'],
// typescript // typescript
'@typescript-eslint/no-use-before-define': 'off', '@typescript-eslint/no-use-before-define': 'off',
'@typescript-eslint/no-unused-vars': 'warn', '@typescript-eslint/no-unused-vars': [
'error',
{
vars: 'all',
args: 'after-used',
ignoreRestSiblings: false,
varsIgnorePattern: '^_',
argsIgnorePattern: '^_',
},
],
'@typescript-eslint/explicit-function-return-type': [ '@typescript-eslint/explicit-function-return-type': [
'warn', 'warn',
{ {
@ -51,6 +60,8 @@ module.exports = {
}, },
], ],
'@typescript-eslint/no-namespace': 'off', '@typescript-eslint/no-namespace': 'off',
'@typescript-eslint/ban-ts-comment': 'off',
'@typescript-eslint/no-unsafe-assignment': 'error',
// react plugin // react plugin
'react/no-unescaped-entities': 'off', 'react/no-unescaped-entities': 'off',
// react native plugin // react native plugin

View File

@ -1,33 +1,15 @@
/* eslint-disable no-console */ import React from 'react';
/* eslint-disable @typescript-eslint/no-unsafe-call */ import { requireNativeComponent, NativeModules, ViewProps, NativeSyntheticEvent, findNodeHandle, NativeMethods, Platform } from 'react-native';
/* eslint-disable @typescript-eslint/no-unsafe-member-access */ import type { CameraPhotoCodec, CameraVideoCodec } from './CameraCodec';
/* eslint-disable @typescript-eslint/no-unsafe-return */ import type { ColorSpace, CameraDeviceFormat, CameraDevice } from './CameraDevice';
/* eslint-disable @typescript-eslint/unbound-method */ import type { ErrorWithCause } from './CameraError';
import React from "react"; import { CameraCaptureError, CameraRuntimeError, tryParseNativeCameraError, isErrorWithCause } from './CameraError';
import { import type { CameraPreset } from './CameraPreset';
requireNativeComponent, import type { CodeType, Code } from './Code';
NativeModules, import type { PhotoFile, TakePhotoOptions } from './PhotoFile';
ViewProps, import type { Point } from './Point';
NativeSyntheticEvent, import type { TakeSnapshotOptions } from './Snapshot';
findNodeHandle, import type { RecordVideoOptions, VideoFile } from './VideoFile';
NativeMethods,
Platform,
} from "react-native";
import { CameraPhotoCodec, CameraVideoCodec } from "./CameraCodec";
import { ColorSpace, CameraDeviceFormat, CameraDevice } from "./CameraDevice";
import {
CameraCaptureError,
CameraRuntimeError,
tryParseNativeCameraError,
ErrorWithCause,
isErrorWithCause,
} from "./CameraError";
import { CameraPreset } from "./CameraPreset";
import { CodeType, Code } from "./Code";
import { PhotoFile, TakePhotoOptions } from "./PhotoFile";
import { Point } from "./Point";
import { TakeSnapshotOptions } from "./Snapshot";
import { RecordVideoOptions, VideoFile } from "./VideoFile";
//#region Types //#region Types
type Modify<T, R> = Omit<T, keyof R> & R; type Modify<T, R> = Omit<T, keyof R> & R;
@ -129,7 +111,7 @@ export type CameraDynamicProps = {
* *
* @default "off" * @default "off"
*/ */
torch?: "off" | "on"; torch?: 'off' | 'on';
/** /**
* Specifies the zoom factor of the current camera, in percent. (`0.0` - `1.0`) * Specifies the zoom factor of the current camera, in percent. (`0.0` - `1.0`)
* *
@ -162,12 +144,8 @@ export type CameraProps = (CameraPresetProps | CameraFormatProps) &
CameraEventProps & CameraEventProps &
ViewProps; ViewProps;
export type CameraPermissionStatus = export type CameraPermissionStatus = 'authorized' | 'not-determined' | 'denied' | 'restricted';
| "authorized" export type CameraPermissionRequestResult = 'authorized' | 'denied';
| "not-determined"
| "denied"
| "restricted";
export type CameraPermissionRequestResult = "authorized" | "denied";
interface OnErrorEvent { interface OnErrorEvent {
code: string; code: string;
@ -183,11 +161,7 @@ interface OnCodeScannedEvent {
// NativeModules automatically resolves 'CameraView' to 'CameraViewModule' // NativeModules automatically resolves 'CameraView' to 'CameraViewModule'
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const CameraModule = NativeModules.CameraView; const CameraModule = NativeModules.CameraView;
if (CameraModule == null) { if (CameraModule == null) console.error("Camera: Native Module 'CameraView' was null! Did you run pod install?");
console.error(
`Camera: Native Module 'CameraView' was null! Did you run pod install?`
);
}
interface CameraState { interface CameraState {
/** /**
@ -202,7 +176,7 @@ type RefType = React.Component<CameraProps> & Readonly<NativeMethods>;
* ### A powerful `<Camera>` component. * ### A powerful `<Camera>` component.
*/ */
export class Camera extends React.PureComponent<CameraProps, CameraState> { export class Camera extends React.PureComponent<CameraProps, CameraState> {
static displayName = "Camera"; static displayName = 'Camera';
displayName = Camera.displayName; displayName = Camera.displayName;
private readonly ref: React.RefObject<RefType>; private readonly ref: React.RefObject<RefType>;
@ -218,11 +192,8 @@ export class Camera extends React.PureComponent<CameraProps, CameraState> {
private get handle(): number | null { private get handle(): number | null {
const nodeHandle = findNodeHandle(this.ref.current); const nodeHandle = findNodeHandle(this.ref.current);
if (nodeHandle == null) { if (nodeHandle == null) console.error('Camera: findNodeHandle(ref) returned null! Does the Camera view exist in the native view tree?');
console.error(
`Camera: findNodeHandle(ref) returned null! Does the Camera view exist in the native view tree?`
);
}
return nodeHandle; return nodeHandle;
} }
@ -248,11 +219,9 @@ export class Camera extends React.PureComponent<CameraProps, CameraState> {
* @platform Android * @platform Android
*/ */
public async takeSnapshot(options?: TakeSnapshotOptions): Promise<PhotoFile> { public async takeSnapshot(options?: TakeSnapshotOptions): Promise<PhotoFile> {
if (Platform.OS !== "android") if (Platform.OS !== 'android')
throw new CameraCaptureError( throw new CameraCaptureError('capture/capture-type-not-supported', `'takeSnapshot()' is not available on ${Platform.OS}!`);
"capture/capture-type-not-supported",
`'takeSnapshot()' is not available on ${Platform.OS}!`
);
try { try {
return await CameraModule.takeSnapshot(this.handle, options ?? {}); return await CameraModule.takeSnapshot(this.handle, options ?? {});
} catch (e) { } catch (e) {
@ -281,34 +250,17 @@ export class Camera extends React.PureComponent<CameraProps, CameraState> {
* }, 5000) * }, 5000)
*/ */
public startRecording(options: RecordVideoOptions): void { public startRecording(options: RecordVideoOptions): void {
const { const { onRecordingError, onRecordingFinished, ...passThroughOptions } = options;
onRecordingError, if (typeof onRecordingError !== 'function' || typeof onRecordingFinished !== 'function')
onRecordingFinished, throw new CameraRuntimeError('parameter/invalid-parameter', 'The onRecordingError or onRecordingFinished functions were not set!');
...passThroughOptions
} = options; const onRecordCallback = (video?: VideoFile, error?: CameraCaptureError): void => {
if (
typeof onRecordingError !== "function" ||
typeof onRecordingFinished !== "function"
) {
throw new CameraRuntimeError(
"parameter/invalid-parameter",
"The onRecordingError or onRecordingFinished functions were not set!"
);
}
const onRecordCallback = (
video?: VideoFile,
error?: CameraCaptureError
) => {
if (error != null) return onRecordingError(error); if (error != null) return onRecordingError(error);
if (video != null) return onRecordingFinished(video); if (video != null) return onRecordingFinished(video);
}; };
// TODO: Use TurboModules to either make this a sync invokation, or make it async. // TODO: Use TurboModules to either make this a sync invokation, or make it async.
try { try {
CameraModule.startRecording( CameraModule.startRecording(this.handle, passThroughOptions, onRecordCallback);
this.handle,
passThroughOptions,
onRecordCallback
);
} catch (e) { } catch (e) {
throw tryParseNativeCameraError(e); throw tryParseNativeCameraError(e);
} }
@ -441,46 +393,36 @@ export class Camera extends React.PureComponent<CameraProps, CameraState> {
//#endregion //#endregion
//#region Events (Wrapped to maintain reference equality) //#region Events (Wrapped to maintain reference equality)
private onError(event?: NativeSyntheticEvent<OnErrorEvent>) { private onError(event?: NativeSyntheticEvent<OnErrorEvent>): void {
if (event == null) { if (event == null) throw new Error('onError() was invoked but event was null!');
throw new Error("onError() was invoked but event was null!");
}
if (this.props.onError != null) { if (this.props.onError != null) {
const error = event.nativeEvent; const error = event.nativeEvent;
const cause = isErrorWithCause(error.cause) ? error.cause : undefined; const cause = isErrorWithCause(error.cause) ? error.cause : undefined;
this.props.onError( this.props.onError(
// @ts-expect-error We're casting from unknown bridge types to TS unions, I expect it to hopefully work // @ts-expect-error We're casting from unknown bridge types to TS unions, I expect it to hopefully work
new CameraRuntimeError(error.code, error.message, cause) new CameraRuntimeError(error.code, error.message, cause),
); );
} }
} }
private onInitialized() { private onInitialized(): void {
this.props.onInitialized?.(); this.props.onInitialized?.();
} }
private onCodeScanned(event?: NativeSyntheticEvent<OnCodeScannedEvent>) { private onCodeScanned(event?: NativeSyntheticEvent<OnCodeScannedEvent>): void {
if (event == null) { if (event == null) throw new Error('onCodeScanned() was invoked but event was null!');
throw new Error("onCodeScanned() was invoked but event was null!");
} if (this.props.onCodeScanned == null)
if (this.props.onCodeScanned == null) { console.warn('Camera: onCodeScanned event was invoked but no listeners attached! Did you forget to remove the `scannableCodes` property?');
console.warn( else this.props.onCodeScanned(event.nativeEvent.codes);
"Camera: onCodeScanned event was invoked but no listeners attached! Did you forget to remove the `scannableCodes` property?"
);
} else {
this.props.onCodeScanned(event.nativeEvent.codes);
}
} }
//#endregion //#endregion
static getDerivedStateFromProps( static getDerivedStateFromProps(props: CameraProps, state: CameraState): CameraState | null {
props: CameraProps,
state: CameraState
): CameraState | null {
const newCameraId = props.device.id; const newCameraId = props.device.id;
if (state.cameraId !== newCameraId) { if (state.cameraId !== newCameraId) return { ...state, cameraId: newCameraId };
return { ...state, cameraId: newCameraId };
}
return null; return null;
} }
@ -504,7 +446,7 @@ export class Camera extends React.PureComponent<CameraProps, CameraState> {
// requireNativeComponent automatically resolves 'CameraView' to 'CameraViewManager' // requireNativeComponent automatically resolves 'CameraView' to 'CameraViewManager'
const NativeCameraView = requireNativeComponent<CameraProps>( const NativeCameraView = requireNativeComponent<CameraProps>(
"CameraView", 'CameraView',
// @ts-expect-error because the type declarations are kinda wrong, no? // @ts-expect-error because the type declarations are kinda wrong, no?
Camera Camera,
); );