Skip to content

Commit

Permalink
refactor: run lint on all files (#349)
Browse files Browse the repository at this point in the history
* Fix lint command so it runs on files directly in src

* Run lint --fix

* Use vitest env in test files

* Remove useless test file

* Remove useless import

* Use ts-expect-error and remove unused ignores

* Format and lint

* Correctly type averageObjectProperties and calcAverage

* Correctly type diffScores

* Remove anys

* Add typeroots

* Disable import plugin

* Allow single any use

* import vi

* Fix typeroots

* Use vi.mocked for types in tests

* Improve ts-expect-error cases

* Commit dist

* Update dist
  • Loading branch information
Weetbix authored Dec 3, 2024
1 parent 537a1a8 commit f38abcb
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 121 deletions.
14 changes: 10 additions & 4 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,18 @@
"sourceType": "module",
"project": "./tsconfig.json"
},
"overrides": [
{
"files": ["*.test.ts"],
"env": {
"jest": true
}
}
],
"rules": {
"i18n-text/no-en": "off",
"eslint-comments/no-use": "off",
"import/named": "off",
"import/no-namespace": "off",
"no-console": "off",
"@typescript-eslint/no-unused-vars": "error",
Expand All @@ -23,10 +32,7 @@
"@typescript-eslint/ban-ts-comment": "error",
"camelcase": "off",
"@typescript-eslint/consistent-type-assertions": "error",
"@typescript-eslint/explicit-function-return-type": [
"error",
{"allowExpressions": true}
],
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/func-call-spacing": ["error", "never"],
"@typescript-eslint/no-array-constructor": "error",
"@typescript-eslint/no-empty-interface": "error",
Expand Down
65 changes: 27 additions & 38 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"test": "vitest run",
"format": "prettier --write .",
"format-check": "prettier --check '**/*.ts'",
"lint": "eslint src/**/*.ts",
"lint": "eslint 'src/**/*.ts'",
"all": "npm run build && npm run format && npm run lint && npm test",
"run:local:report": "tsx ./src/cli/report.ts",
"run:local:debugfile": "tsx ./src/cli/debug-file.ts",
Expand Down
2 changes: 1 addition & 1 deletion playground/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {createApp} from 'vue';
import {install as VueMonacoEditorPlugin} from '@guolao/vue-monaco-editor';
// @ts-ignore
// @ts-expect-error
import revel from '@rebilly/revel';
import './style.css';
import App from './App.vue';
Expand Down
8 changes: 4 additions & 4 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const listComments = async (
return existingComments.filter(({body}) => body?.startsWith(hiddenHeader));
};

const insertComment = (
const insertComment = async (
client: Octokit,
context: Context,
prNumber: number,
Expand All @@ -46,7 +46,7 @@ const insertComment = (
body: appendHiddenHeaderToComment(body, hiddenHeader),
});

const updateComment = (
const updateComment = async (
client: Octokit,
context: Context,
body: string,
Expand All @@ -59,13 +59,13 @@ const updateComment = (
body: appendHiddenHeaderToComment(body, hiddenHeader),
});

const deleteComments = (
const deleteComments = async (
client: Octokit,
context: Context,
comments: {id: number}[],
) =>
Promise.all(
comments.map(({id}) =>
comments.map(async ({id}) =>
client.rest.issues.deleteComment({
...context.repo,
comment_id: id,
Expand Down
3 changes: 0 additions & 3 deletions src/index.test.ts

This file was deleted.

6 changes: 1 addition & 5 deletions src/markdown.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import path from 'path';
import {
ReadabilityReport,
ReadabilityReportFileResult,
SingleReadabilityResultWithDiff,
} from './report';
import {ReadabilityReport, SingleReadabilityResultWithDiff} from './report';

const arrayToCells = (rows: string[]) => `${rows.join(' | ')}\n`;

Expand Down
26 changes: 13 additions & 13 deletions src/readability-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,27 @@ export type ReadabilityResults = {
};

// Calculates the average of a particular property value, given an array of objects
function calcAverage(
arrayOfObjects: Record<string, number>[],
accessorFn: (value: Record<string, number>) => number,
) {
function calcAverage<T extends Record<string, number>>(
arrayOfObjects: T[],
accessorFn: (value: T) => number,
): number {
return (
arrayOfObjects.reduce(
(acc: number, value) => acc + accessorFn(value),
0,
) / arrayOfObjects.length
arrayOfObjects.reduce((acc, value) => acc + accessorFn(value), 0) /
arrayOfObjects.length
);
}

// Returns a score object containing the averages, given an array of scores
export function averageObjectProperties(objects: Record<string, number>[]) {
return Object.keys(objects[0]).reduce(
(acc: Record<string, number>, key) => {
export function averageObjectProperties<T extends Record<string, number>>(
objects: T[],
): Record<keyof T, number> {
const keys = Object.keys(objects[0]) as (keyof T)[];
return keys.reduce(
(acc, key) => {
acc[key] = calcAverage(objects, (object) => object[key]);
return acc;
},
{},
{} as Record<keyof T, number>,
);
}

Expand Down Expand Up @@ -61,7 +62,6 @@ export function calculateReadability(globPath: string): ReadabilityResults {

return {
fileResults,
// @ts-ignore
averageResult,
};
}
22 changes: 11 additions & 11 deletions src/readability.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {describe} from 'node:test';
import {describe, it, expect, vi} from 'vitest';
import {preprocessMarkdown, calculateReadabilityOfText} from './readability';
import readability from 'text-readability';

Expand Down Expand Up @@ -394,11 +394,11 @@ describe('scoring', () => {
});

it('should cap min and max scores', () => {
readability.automatedReadabilityIndex.mockReturnValue(-1);
readability.colemanLiauIndex.mockReturnValue(-1);
readability.daleChallReadabilityScore.mockReturnValue(-1);
readability.fleschReadingEase.mockReturnValue(-1);
readability.gunningFog.mockReturnValue(-1);
vi.mocked(readability.automatedReadabilityIndex).mockReturnValue(-1);
vi.mocked(readability.colemanLiauIndex).mockReturnValue(-1);
vi.mocked(readability.daleChallReadabilityScore).mockReturnValue(-1);
vi.mocked(readability.fleschReadingEase).mockReturnValue(-1);
vi.mocked(readability.gunningFog).mockReturnValue(-1);

expect(calculateReadabilityOfText('dummy text')).toMatchObject({
automatedReadabilityIndex: 6,
Expand All @@ -408,11 +408,11 @@ describe('scoring', () => {
gunningFog: 6,
});

readability.automatedReadabilityIndex.mockReturnValue(1000);
readability.colemanLiauIndex.mockReturnValue(1000);
readability.daleChallReadabilityScore.mockReturnValue(1000);
readability.fleschReadingEase.mockReturnValue(1000);
readability.gunningFog.mockReturnValue(1000);
vi.mocked(readability.automatedReadabilityIndex).mockReturnValue(1000);
vi.mocked(readability.colemanLiauIndex).mockReturnValue(1000);
vi.mocked(readability.daleChallReadabilityScore).mockReturnValue(1000);
vi.mocked(readability.fleschReadingEase).mockReturnValue(1000);
vi.mocked(readability.gunningFog).mockReturnValue(1000);

expect(calculateReadabilityOfText('dummy text')).toMatchObject({
automatedReadabilityIndex: 22,
Expand Down
Loading

0 comments on commit f38abcb

Please sign in to comment.