Skip to content

Commit

Permalink
Add explicit function return types
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekmaj committed Jun 26, 2024
1 parent 4291619 commit dbc1e4b
Show file tree
Hide file tree
Showing 22 changed files with 62 additions and 46 deletions.
4 changes: 2 additions & 2 deletions packages/react-pdf/src/Document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ function isParameterObject(file: File): file is Source {
/**
* Loads a document passed using `file` prop.
*/
const Document = forwardRef(function Document(
const Document: React.ForwardRefExoticComponent<DocumentProps> = forwardRef(function Document(
{
children,
className,
Expand All @@ -255,7 +255,7 @@ const Document = forwardRef(function Document(
renderMode,
rotate,
...otherProps
}: DocumentProps,
},
ref,
) {
const [sourceState, sourceDispatch] = useResolver<Source | null>();
Expand Down
5 changes: 4 additions & 1 deletion packages/react-pdf/src/DocumentContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ import { createContext } from 'react';

import type { DocumentContextType } from './shared/types.js';

export default createContext<DocumentContextType>(null);
const documentContext: React.Context<DocumentContextType> =
createContext<DocumentContextType>(null);

export default documentContext;
38 changes: 19 additions & 19 deletions packages/react-pdf/src/LinkService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,31 +49,31 @@ export default class LinkService implements IPDFLinkService {
this.pdfViewer = undefined;
}

setDocument(pdfDocument: PDFDocumentProxy) {
setDocument(pdfDocument: PDFDocumentProxy): void {
this.pdfDocument = pdfDocument;
}

setViewer(pdfViewer: PDFViewer) {
setViewer(pdfViewer: PDFViewer): void {
this.pdfViewer = pdfViewer;
}

setExternalLinkRel(externalLinkRel?: ExternalLinkRel) {
setExternalLinkRel(externalLinkRel?: ExternalLinkRel): void {
this.externalLinkRel = externalLinkRel;
}

setExternalLinkTarget(externalLinkTarget?: ExternalLinkTarget) {
setExternalLinkTarget(externalLinkTarget?: ExternalLinkTarget): void {
this.externalLinkTarget = externalLinkTarget;
}

setHistory() {
setHistory(): void {
// Intentionally empty
}

get pagesCount() {
get pagesCount(): number {
return this.pdfDocument ? this.pdfDocument.numPages : 0;
}

get page() {
get page(): number {
invariant(this.pdfViewer, 'PDF viewer is not initialized.');

return this.pdfViewer.currentPageNumber || 0;
Expand All @@ -85,7 +85,7 @@ export default class LinkService implements IPDFLinkService {
this.pdfViewer.currentPageNumber = value;
}

get rotation() {
get rotation(): number {
return 0;
}

Expand Down Expand Up @@ -147,11 +147,11 @@ export default class LinkService implements IPDFLinkService {
});
}

navigateTo(dest: Dest) {
navigateTo(dest: Dest): void {
this.goToDestination(dest);
}

goToPage(pageNumber: number) {
goToPage(pageNumber: number): void {
const pageIndex = pageNumber - 1;

invariant(this.pdfViewer, 'PDF viewer is not initialized.');
Expand All @@ -167,41 +167,41 @@ export default class LinkService implements IPDFLinkService {
});
}

addLinkAttributes(link: HTMLAnchorElement, url: string, newWindow: boolean) {
addLinkAttributes(link: HTMLAnchorElement, url: string, newWindow: boolean): void {
link.href = url;
link.rel = this.externalLinkRel || DEFAULT_LINK_REL;
link.target = newWindow ? '_blank' : this.externalLinkTarget || '';
}

getDestinationHash() {
getDestinationHash(): string {
return '#';
}

getAnchorUrl() {
getAnchorUrl(): string {
return '#';
}

setHash() {
setHash(): void {
// Intentionally empty
}

executeNamedAction() {
executeNamedAction(): void {
// Intentionally empty
}

cachePageRef() {
cachePageRef(): void {
// Intentionally empty
}

isPageVisible() {
isPageVisible(): boolean {
return true;
}

isPageCached() {
isPageCached(): boolean {
return true;
}

executeSetOCGState() {
executeSetOCGState(): void {
// Intentionally empty
}
}
2 changes: 1 addition & 1 deletion packages/react-pdf/src/Message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ type MessageProps = {
type: 'error' | 'loading' | 'no-data';
};

export default function Message({ children, type }: MessageProps) {
export default function Message({ children, type }: MessageProps): React.ReactElement {
return <div className={`react-pdf__message react-pdf__message--${type}`}>{children}</div>;
}
2 changes: 1 addition & 1 deletion packages/react-pdf/src/Outline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export type OutlineProps = {
*
* Should be placed inside `<Document />`. Alternatively, it can have `pdf` prop passed, which can be obtained from `<Document />`'s `onLoadSuccess` callback function.
*/
export default function Outline(props: OutlineProps) {
export default function Outline(props: OutlineProps): React.ReactNode {
const documentContext = useDocumentContext();

const mergedProps = { ...documentContext, ...props };
Expand Down
4 changes: 3 additions & 1 deletion packages/react-pdf/src/OutlineContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ import { createContext } from 'react';

import type { OutlineContextType } from './shared/types.js';

export default createContext<OutlineContextType>(null);
const outlineContext: React.Context<OutlineContextType> = createContext<OutlineContextType>(null);

export default outlineContext;
2 changes: 1 addition & 1 deletion packages/react-pdf/src/OutlineItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type OutlineItemProps = {
pdf?: PDFDocumentProxy | false;
};

export default function OutlineItem(props: OutlineItemProps) {
export default function OutlineItem(props: OutlineItemProps): React.ReactElement {
const documentContext = useDocumentContext();

const outlineContext = useOutlineContext();
Expand Down
2 changes: 1 addition & 1 deletion packages/react-pdf/src/Page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ export type PageProps = {
*
* Should be placed inside `<Document />`. Alternatively, it can have `pdf` prop passed, which can be obtained from `<Document />`'s `onLoadSuccess` callback function, however some advanced functions like linking between pages inside a document may not be working correctly.
*/
export default function Page(props: PageProps) {
export default function Page(props: PageProps): React.ReactElement {
const documentContext = useDocumentContext();

const mergedProps = { ...documentContext, ...props };
Expand Down
2 changes: 1 addition & 1 deletion packages/react-pdf/src/Page/AnnotationLayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { cancelRunningTask } from '../shared/utils.js';

import type { Annotations } from '../shared/types.js';

export default function AnnotationLayer() {
export default function AnnotationLayer(): React.ReactElement {
const documentContext = useDocumentContext();
const pageContext = usePageContext();

Expand Down
2 changes: 1 addition & 1 deletion packages/react-pdf/src/Page/Canvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type CanvasProps = {
canvasRef?: React.Ref<HTMLCanvasElement>;
};

export default function Canvas(props: CanvasProps) {
export default function Canvas(props: CanvasProps): React.ReactElement {
const pageContext = usePageContext();

invariant(pageContext, 'Unable to find Page context.');
Expand Down
2 changes: 1 addition & 1 deletion packages/react-pdf/src/Page/TextLayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function isTextItem(item: TextItem | TextMarkedContent): item is TextItem {
return 'str' in item;
}

export default function TextLayer() {
export default function TextLayer(): React.ReactElement {
const pageContext = usePageContext();

invariant(pageContext, 'Unable to find Page context.');
Expand Down
4 changes: 3 additions & 1 deletion packages/react-pdf/src/PageContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ import { createContext } from 'react';

import type { PageContextType } from './shared/types.js';

export default createContext<PageContextType>(null);
const pageContext: React.Context<PageContextType> = createContext<PageContextType>(null);

export default pageContext;
2 changes: 1 addition & 1 deletion packages/react-pdf/src/Ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default class Ref {
this.gen = gen;
}

toString() {
toString(): string {
let str = `${this.num}R`;
if (this.gen !== 0) {
str += this.gen;
Expand Down
2 changes: 1 addition & 1 deletion packages/react-pdf/src/StructTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { cancelRunningTask } from './shared/utils.js';

import type { StructTreeNodeWithExtraAttributes } from './shared/types.js';

export default function StructTree() {
export default function StructTree(): React.ReactElement | null {
const pageContext = usePageContext();

invariant(pageContext, 'Unable to find Page context.');
Expand Down
5 changes: 4 additions & 1 deletion packages/react-pdf/src/StructTreeItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ type StructTreeItemProps = {
node: StructTreeNodeWithExtraAttributes | StructTreeContent;
};

export default function StructTreeItem({ className, node }: StructTreeItemProps) {
export default function StructTreeItem({
className,
node,
}: StructTreeItemProps): React.ReactElement {
const attributes = useMemo(() => getAttributes(node), [node]);

// biome-ignore lint/correctness/useExhaustiveDependencies: Looks like a Biome error
Expand Down
2 changes: 1 addition & 1 deletion packages/react-pdf/src/Thumbnail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export type ThumbnailProps = Omit<
*
* Should be placed inside `<Document />`. Alternatively, it can have `pdf` prop passed, which can be obtained from `<Document />`'s `onLoadSuccess` callback function.
*/
export default function Thumbnail(props: ThumbnailProps) {
export default function Thumbnail(props: ThumbnailProps): React.ReactElement {
const documentContext = useDocumentContext();

const mergedProps = { ...documentContext, ...props };
Expand Down
2 changes: 1 addition & 1 deletion packages/react-pdf/src/shared/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ export const PDF_ROLE_TO_HTML_ROLE = {
Artifact: null,
};

export const HEADING_PATTERN = /^H(\d+)$/;
export const HEADING_PATTERN: RegExp = /^H(\d+)$/;
4 changes: 3 additions & 1 deletion packages/react-pdf/src/shared/hooks/useDocumentContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { useContext } from 'react';

import DocumentContext from '../../DocumentContext.js';

export default function useDocumentContext() {
import type { DocumentContextType } from '../types.js';

export default function useDocumentContext(): DocumentContextType {
return useContext(DocumentContext);
}
4 changes: 3 additions & 1 deletion packages/react-pdf/src/shared/hooks/useOutlineContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { useContext } from 'react';

import OutlineContext from '../../OutlineContext.js';

export default function useOutlineContext() {
import type { OutlineContextType } from '../types.js';

export default function useOutlineContext(): OutlineContextType {
return useContext(OutlineContext);
}
4 changes: 3 additions & 1 deletion packages/react-pdf/src/shared/hooks/usePageContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { useContext } from 'react';

import PageContext from '../../PageContext.js';

export default function usePageContext() {
import type { PageContextType } from '../types.js';

export default function usePageContext(): PageContextType {
return useContext(PageContext);
}
2 changes: 1 addition & 1 deletion packages/react-pdf/src/shared/hooks/useResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ function reducer<T>(state: State<T>, action: Action<T>): State<T> {
}
}

export default function useResolver<T>() {
export default function useResolver<T>(): [State<T>, React.Dispatch<Action<T>>] {
return useReducer(reducer<T>, { value: undefined, error: undefined });
}
12 changes: 6 additions & 6 deletions packages/react-pdf/src/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import type { PageCallback } from './types.js';
/**
* Checks if we're running in a browser environment.
*/
export const isBrowser = typeof document !== 'undefined';
export const isBrowser: boolean = typeof document !== 'undefined';

/**
* Checks whether we're running from a local file system.
*/
export const isLocalFileSystem = isBrowser && window.location.protocol === 'file:';
export const isLocalFileSystem: boolean = isBrowser && window.location.protocol === 'file:';

/**
* Checks whether a variable is defined.
Expand Down Expand Up @@ -83,28 +83,28 @@ export function dataURItoByteString(dataURI: unknown): string {
return unescape(dataString);
}

export function getDevicePixelRatio() {
export function getDevicePixelRatio(): number {
return (isBrowser && window.devicePixelRatio) || 1;
}

const allowFileAccessFromFilesTip =
'On Chromium based browsers, you can use --allow-file-access-from-files flag for debugging purposes.';

export function displayCORSWarning() {
export function displayCORSWarning(): void {
warning(
!isLocalFileSystem,
`Loading PDF as base64 strings/URLs may not work on protocols other than HTTP/HTTPS. ${allowFileAccessFromFilesTip}`,
);
}

export function displayWorkerWarning() {
export function displayWorkerWarning(): void {
warning(
!isLocalFileSystem,
`Loading PDF.js worker may not work on protocols other than HTTP/HTTPS. ${allowFileAccessFromFilesTip}`,
);
}

export function cancelRunningTask(runningTask?: { cancel?: () => void } | null) {
export function cancelRunningTask(runningTask?: { cancel?: () => void } | null): void {
if (runningTask?.cancel) runningTask.cancel();
}

Expand Down

0 comments on commit dbc1e4b

Please sign in to comment.