Skip to content

Commit

Permalink
Replace ESLint and Prettier with Biome (#1819)
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekmaj authored Jun 5, 2024
1 parent 6223d08 commit f6ef21b
Show file tree
Hide file tree
Showing 39 changed files with 619 additions and 1,953 deletions.
51 changes: 6 additions & 45 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,11 @@ jobs:
- name: Checkout
uses: actions/checkout@v4

- name: Cache Yarn cache
uses: actions/cache@v4
env:
cache-name: yarn-cache
with:
path: ~/.yarn/berry/cache
key: ${{ runner.os }}-${{ env.cache-name }}-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-${{ env.cache-name }}
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: '20'

- name: Enable Corepack
run: corepack enable

- name: Install dependencies
run: yarn --immutable

- name: Build package
run: yarn build
- name: Setup Biome
uses: biomejs/setup-biome@v2

- name: Run tests
run: yarn lint
run: biome lint

typescript:
name: Type checking (React ${{ matrix.react }})
Expand Down Expand Up @@ -99,29 +78,11 @@ jobs:
- name: Checkout
uses: actions/checkout@v4

- name: Cache Yarn cache
uses: actions/cache@v4
env:
cache-name: yarn-cache
with:
path: ~/.yarn/berry/cache
key: ${{ runner.os }}-${{ env.cache-name }}-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-${{ env.cache-name }}
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: '20'

- name: Enable Corepack
run: corepack enable

- name: Install dependencies
run: yarn --immutable
- name: Setup Biome
uses: biomejs/setup-biome@v2

- name: Run formatting
run: yarn format
run: biome format

unit:
name: Unit tests (React ${{ matrix.react }})
Expand Down
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
yarn lint-staged
yarn format --staged --no-errors-on-unmatched --write
3 changes: 0 additions & 3 deletions .lintstagedrc.json

This file was deleted.

3 changes: 0 additions & 3 deletions .prettierignore

This file was deleted.

4 changes: 0 additions & 4 deletions .prettierrc.json

This file was deleted.

2 changes: 1 addition & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"recommendations": ["dbaeumer.vscode-eslint", "eamodio.gitlens", "esbenp.prettier-vscode"],
"recommendations": ["biomejs.biome", "eamodio.gitlens"],
"unwantedRecommendations": ["dbaeumer.jshint"]
}
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.defaultFormatter": "biomejs.biome",
"editor.formatOnSave": true,
"search.exclude": {
"**/.yarn": true
Expand Down
43 changes: 43 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"$schema": "https://biomejs.dev/schemas/1.8.0/schema.json",
"files": {
"ignore": [".tsimp", "coverage", "dist", ".pnp.cjs", ".pnp.loader.mjs"]
},
"formatter": {
"lineWidth": 100,
"indentStyle": "space"
},
"linter": {
"rules": {
"complexity": {
"noUselessSwitchCase": "off"
},
"suspicious": {
"noConsoleLog": "warn"
}
}
},
"javascript": {
"formatter": {
"quoteStyle": "single"
}
},
"overrides": [
{
"include": ["**/package.json"],
"formatter": {
"lineWidth": 1
}
},
{
"include": ["**/vite.config.ts"],
"linter": {
"rules": {
"suspicious": {
"noConsoleLog": "off"
}
}
}
}
]
}
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@
"unit": "yarn workspaces foreach --all run unit"
},
"devDependencies": {
"husky": "^9.0.0",
"lint-staged": "^15.0.0",
"prettier": "^3.2.0"
"husky": "^9.0.0"
},
"resolutions": {
"eslint-plugin-import": "npm:eslint-plugin-i@^2.28.0",
"pdfjs-dist": "patch:pdfjs-dist@npm:4.3.136#.yarn/patches/pdfjs-dist-npm-4.3.136-35a0e70ced.patch"
},
"packageManager": "[email protected]"
Expand Down
2 changes: 0 additions & 2 deletions packages/react-pdf/.eslintignore

This file was deleted.

3 changes: 0 additions & 3 deletions packages/react-pdf/.eslintrc.json

This file was deleted.

1 change: 0 additions & 1 deletion packages/react-pdf/.prettierignore

This file was deleted.

8 changes: 3 additions & 5 deletions packages/react-pdf/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
"build-js-cjs-package": "echo '{\n \"type\": \"commonjs\"\n}' > dist/cjs/package.json",
"clean": "rimraf dist",
"copy-styles": "cpy 'src/**/*.css' dist/esm && cpy 'src/**/*.css' dist/cjs",
"format": "prettier --check . --cache",
"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
"format": "biome format",
"lint": "biome lint",
"prepack": "yarn clean && yarn build",
"test": "yarn lint && yarn tsc && yarn format && yarn unit",
"tsc": "tsc",
Expand Down Expand Up @@ -65,6 +65,7 @@
"warning": "^4.0.0"
},
"devDependencies": {
"@biomejs/biome": "^1.8.0",
"@testing-library/dom": "^10.0.0",
"@testing-library/jest-dom": "^6.0.0",
"@testing-library/react": "^15.0.0",
Expand All @@ -74,11 +75,8 @@
"canvas": "^2.11.2",
"core-js": "^3.37.0",
"cpy-cli": "^5.0.0",
"eslint": "^8.56.0",
"eslint-config-wojtekmaj": "^1.0.0",
"jsdom": "^24.0.0",
"nodemon": "^3.0.0",
"prettier": "^3.2.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"rimraf": "^3.0.0",
Expand Down
115 changes: 55 additions & 60 deletions packages/react-pdf/src/Document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,11 @@ export type DocumentProps = {
const defaultOnPassword: OnPassword = (callback, reason) => {
switch (reason) {
case PasswordResponses.NEED_PASSWORD: {
// eslint-disable-next-line no-alert
const password = prompt('Enter the password to open this PDF file.');
callback(password);
break;
}
case PasswordResponses.INCORRECT_PASSWORD: {
// eslint-disable-next-line no-alert
const password = prompt('Invalid password. Please try again.');
callback(password);
break;
Expand Down Expand Up @@ -353,11 +351,13 @@ const Document = forwardRef(function Document(
}
}

function resetSource() {
sourceDispatch({ type: 'RESET' });
}

useEffect(resetSource, [file, sourceDispatch]);
// biome-ignore lint/correctness/useExhaustiveDependencies: useEffect intentionally triggered on file change
useEffect(
function resetSource() {
sourceDispatch({ type: 'RESET' });
},
[file, sourceDispatch],
);

const findDocumentSource = useCallback(async (): Promise<Source | null> => {
if (!file) {
Expand Down Expand Up @@ -439,23 +439,19 @@ const Document = forwardRef(function Document(
};
}, [findDocumentSource, sourceDispatch]);

useEffect(
() => {
if (typeof source === 'undefined') {
return;
}
// biome-ignore lint/correctness/useExhaustiveDependencies: Ommitted callbacks so they are not called every time they change
useEffect(() => {
if (typeof source === 'undefined') {
return;
}

if (source === false) {
onSourceError();
return;
}
if (source === false) {
onSourceError();
return;
}

onSourceSuccess();
},
// Ommitted callbacks so they are not called every time they change
// eslint-disable-next-line react-hooks/exhaustive-deps
[source],
);
onSourceSuccess();
}, [source]);

/**
* Called when a document is read successfully
Expand Down Expand Up @@ -490,11 +486,13 @@ const Document = forwardRef(function Document(
}
}

function resetDocument() {
pdfDispatch({ type: 'RESET' });
}

useEffect(resetDocument, [pdfDispatch, source]);
// biome-ignore lint/correctness/useExhaustiveDependencies: useEffect intentionally triggered on source change
useEffect(
function resetDocument() {
pdfDispatch({ type: 'RESET' });
},
[pdfDispatch, source],
);

function loadDocument() {
if (!source) {
Expand Down Expand Up @@ -532,46 +530,39 @@ const Document = forwardRef(function Document(
};
}

useEffect(
loadDocument,
// Ommitted callbacks so they are not called every time they change
// eslint-disable-next-line react-hooks/exhaustive-deps
[options, pdfDispatch, source],
);
// biome-ignore lint/correctness/useExhaustiveDependencies: Ommitted callbacks so they are not called every time they change
useEffect(loadDocument, [options, pdfDispatch, source]);

useEffect(
() => {
if (typeof pdf === 'undefined') {
return;
}
// biome-ignore lint/correctness/useExhaustiveDependencies: Ommitted callbacks so they are not called every time they change
useEffect(() => {
if (typeof pdf === 'undefined') {
return;
}

if (pdf === false) {
onLoadError();
return;
}
if (pdf === false) {
onLoadError();
return;
}

onLoadSuccess();
}, [pdf]);

onLoadSuccess();
useEffect(
function setupLinkService() {
linkService.current.setViewer(viewer.current);
linkService.current.setExternalLinkRel(externalLinkRel);
linkService.current.setExternalLinkTarget(externalLinkTarget);
},
// Ommitted callbacks so they are not called every time they change
// eslint-disable-next-line react-hooks/exhaustive-deps
[pdf],
[externalLinkRel, externalLinkTarget],
);

function setupLinkService() {
linkService.current.setViewer(viewer.current);
linkService.current.setExternalLinkRel(externalLinkRel);
linkService.current.setExternalLinkTarget(externalLinkTarget);
}

useEffect(setupLinkService, [externalLinkRel, externalLinkTarget]);

function registerPage(pageIndex: number, ref: HTMLDivElement) {
const registerPage = useCallback((pageIndex: number, ref: HTMLDivElement) => {
pages.current[pageIndex] = ref;
}
}, []);

function unregisterPage(pageIndex: number) {
const unregisterPage = useCallback((pageIndex: number) => {
delete pages.current[pageIndex];
}
}, []);

const childContext = useMemo(
() => ({
Expand All @@ -584,10 +575,14 @@ const Document = forwardRef(function Document(
rotate,
unregisterPage,
}),
[imageResourcesPath, onItemClick, pdf, renderMode, rotate],
[imageResourcesPath, onItemClick, pdf, registerPage, renderMode, rotate, unregisterPage],
);

const eventProps = useMemo(() => makeEventProps(otherProps, () => pdf), [otherProps, pdf]);
const eventProps = useMemo(
() => makeEventProps(otherProps, () => pdf),
// biome-ignore lint/correctness/useExhaustiveDependencies: FIXME
[otherProps, pdf],
);

function renderChildren() {
return <DocumentContext.Provider value={childContext}>{children}</DocumentContext.Provider>;
Expand Down
1 change: 0 additions & 1 deletion packages/react-pdf/src/LinkService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ export default class LinkService implements IPDFLinkService {
this.pdfViewer.currentPageNumber = value;
}

// eslint-disable-next-line @typescript-eslint/class-literal-property-style
get rotation() {
return 0;
}
Expand Down
Loading

0 comments on commit f6ef21b

Please sign in to comment.