Skip to content

Commit

Permalink
dx: simplify ESLint config (#477)
Browse files Browse the repository at this point in the history
* add files to biome

* replace js.recommended by used rules

* wip

* fixes

* fixes

* ts rules

* fix all lint errors

* fix import issues

* add turbo

* react config

* remove configs.biome

* update astro config

* warn

* astro

* ts

* no typed linting

* disable projectService in astro files

* disable typed rules

* test

* test

* fix

* lockfile

* remove root lint:ts

* Update package.json

* update tsconfig

* update

* move getMessageTextContent to core

* don't lint examples

* no regex rule

* no typed linting

* exclude eslint-config from bundle-size

* up

* changeset
  • Loading branch information
nickrttn authored Dec 27, 2024
1 parent 4acd94d commit 279674a
Show file tree
Hide file tree
Showing 64 changed files with 846 additions and 546 deletions.
8 changes: 8 additions & 0 deletions .changeset/ten-trainers-accept.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@markprompt/docusaurus-theme-search": minor
"@markprompt/react": minor
"@markprompt/core": minor
"@markprompt/web": minor
---

Fix linting issues
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ jobs:
with:
build-script: '"build:packages"'
pattern: '{./packages/**/dist/**/*.{cjs,js},./packages/css/markprompt.css}'
exclude: '{**/*.map,**/*.d.{ts,cts},**/node_modules/**,./packages/**/node_modules/**/dist/**/*}'
exclude: '{**/*.map,**/*.d.{ts,cts},**/node_modules/**,./packages/**/node_modules/**/dist/**/*,./packages/eslint-config/dist/**/*}'
44 changes: 44 additions & 0 deletions biome.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,16 @@
"useDateNow": "error"
},
"correctness": {
"noUndeclaredVariables": {
"level": "error"
// enable when biome@2.0.0 is released
// "options": {
// "checkTypes": true
// }
},
"noUnusedFunctionParameters": "error",
"noUnusedImports": "error",
"noUnusedPrivateClassMembers": "error",
"noUnusedVariables": "error",
"useArrayLiterals": "error",
"useHookAtTopLevel": "error"
Expand All @@ -56,6 +64,7 @@
"noDefaultExport": "warn",
"noNonNullAssertion": "warn",
"useCollapsedElseIf": "error",
"useForOf": "error",
"useThrowOnlyError": "error"
},
"suspicious": {
Expand All @@ -69,6 +78,15 @@
"useAwait": "error",
"useErrorMessage": "error",
"useNumberToFixedDigitsArgument": "error"
},
"nursery": {
"noCommonJs": "error",
"noDuplicateElseIf": "error",
"noIrregularWhitespace": "error",
"noStaticElementInteractions": "error",
"useAdjacentOverloadSignatures": "error",
"useAriaPropsSupportedByRole": "error",
"useValidAutocomplete": "error"
}
}
},
Expand Down Expand Up @@ -98,6 +116,32 @@
}
}
}
},
{
// .d.ts files import types inside their declare, which isn't parsed correctly by Biome, yet.
"include": ["**/*.d.ts"],
"linter": {
"rules": {
"correctness": {
"noUndeclaredVariables": "off",
"noUnusedImports": "off"
}
}
}
},
{
"include": [
"examples/with-docusaurus*/sidebars.js",
"examples/with-docusaurus*/babel.config.js",
"examples/with-docusaurus*/docusaurus.config.ts"
],
"linter": {
"rules": {
"nursery": {
"noCommonJs": "off"
}
}
}
}
]
}
1 change: 0 additions & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ export default [
],
},
},
...configs.biome,
];
13 changes: 9 additions & 4 deletions examples/with-function-calling/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
import Head from 'next/head';
import type { OpenAI } from 'openai';
import { useCallback, useState } from 'react';
import type { JSX } from 'react';
import type { FormEvent, JSX } from 'react';

interface ChatCompletionExecution {
run?: (args: unknown) => void;
Expand Down Expand Up @@ -44,7 +44,7 @@ export default function IndexPage(): JSX.Element {
const [streamedMessage, setStreamedMessage] = useState('');

const submitForm = useCallback(
async (event: React.FormEvent<HTMLFormElement>) => {
async (event: FormEvent<HTMLFormElement>) => {
event.preventDefault();

setStreamedMessage('Fetching response...');
Expand Down Expand Up @@ -92,7 +92,7 @@ export default function IndexPage(): JSX.Element {
tool.run?.(toolCall.function?.arguments);
setStreamedMessage(
`Calling: ${tool.function.name}\n\nArguments:\n\n${
toolCall.function?.arguments || {}
toolCall.function?.arguments || JSON.stringify({})
}`,
);
}
Expand All @@ -108,7 +108,12 @@ export default function IndexPage(): JSX.Element {
<meta charSet="utf-8" />
</Head>
<div className="Container">
<form className="InputForm" onSubmit={submitForm}>
<form
className="InputForm"
onSubmit={(event) => {
void submitForm(event);
}}
>
<label className="label" htmlFor="input">
Input
</label>
Expand Down
3 changes: 2 additions & 1 deletion examples/with-next/pages/_document.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Html, Head, Main, NextScript } from 'next/document';
import Script from 'next/script';
import type { JSX } from 'react';

export default function Document(): React.JSX.Element {
export default function Document(): JSX.Element {
return (
<Html>
<Head>
Expand Down
4 changes: 2 additions & 2 deletions examples/with-next/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import { SearchIcon } from '../components/icons';

export default function IndexPage(): JSX.Element {
useEffect(() => {
const handleKeyDown = (event: KeyboardEvent): void => {
const handleKeyDown = async (event: KeyboardEvent): Promise<void> => {
if (
(event.key === 'k' && event.ctrlKey) ||
(event.key === 'k' && event.metaKey)
) {
event.preventDefault();
openMarkprompt();
await openMarkprompt();
}
};

Expand Down
6 changes: 1 addition & 5 deletions examples/with-standalone-ticket-deflection/eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import { configs } from '@markprompt/eslint-config';

export default [
...configs.base(import.meta.dirname),
...configs.react,
...configs.biome,
];
export default [...configs.base(import.meta.dirname), ...configs.react];
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"lint:css": "stylelint \"**/*.css\"",
"lint:js": "eslint .",
"lint:md": "remark . --frail",
"lint:ts": "pnpm build:packages",
"lint": "turbo run lint:biome lint:css lint:js lint:md lint:ts",
"postinstall": "manypkg check",
"prepare": "husky",
Expand Down
1 change: 0 additions & 1 deletion packages/core/eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,4 @@ export default [
},
},
...configs.vitest,
...configs.biome,
];
6 changes: 3 additions & 3 deletions packages/core/src/chat/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('submitChat', () => {
let status = 200;

const server = setupServer(
http.post(`${DEFAULT_OPTIONS.apiUrl!}/chat`, async ({ request }) => {
http.post(`${DEFAULT_OPTIONS.apiUrl}/chat`, async ({ request }) => {
req = request;
requestBody = (await request.json()) as SubmitChatOptions;

Expand All @@ -75,7 +75,7 @@ describe('submitChat', () => {
start(controller) {
if (Array.isArray(response)) {
let i = 0;
for (const chunk of response) {
for (const chunk of response as string[]) {
controller.enqueue(
encoder.encode(
formatEvent({
Expand Down Expand Up @@ -356,7 +356,7 @@ describe('submitChat', () => {
'testKey',
{ stream: false },
)) {
await expect(json).toStrictEqual({
expect(json).toStrictEqual({
content: 'According to my calculator 1 + 2 = 3',
role: 'assistant',
messageId,
Expand Down
13 changes: 9 additions & 4 deletions packages/core/src/chat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export async function* submitChat(
checkAbortSignal(options.signal);

if (res.headers.get('Content-Type')?.includes('application/json')) {
const json = await res.json();
const json: unknown = await res.json();

if (
isChatCompletion(json) &&
Expand Down Expand Up @@ -150,8 +150,13 @@ export async function* submitChat(
const text = await res.text();

try {
const json = JSON.parse(text);
if (json.error) {
const json: unknown = JSON.parse(text);
if (
json &&
typeof json === 'object' &&
'error' in json &&
typeof json.error === 'string'
) {
throw new Error(json.error);
}
} catch {
Expand Down Expand Up @@ -182,7 +187,7 @@ export async function* submitChat(
continue;
}

const json = JSON.parse(event.data);
const json: unknown = JSON.parse(event.data);

if (!isChatCompletionChunk(json)) {
throw new Error('Malformed response from Markprompt API', {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/chat/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const COMPLETIONS_MODELS = [

export type CompletionsModel = ArrayToUnion<typeof COMPLETIONS_MODELS>;

export const EMBEDDINGS_MODEL = 'text-embedding-ada-002' as const;
export const EMBEDDINGS_MODEL = 'text-embedding-ada-002';

export type EmbeddingsModel = typeof EMBEDDINGS_MODEL;

Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/chat/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,16 @@ export function checkAbortSignal(signal?: AbortSignal): void {
throw signal.reason;
}

throw new Error(signal.reason);
throw new Error(
typeof signal.reason === 'string' ? signal.reason : 'Aborted',
);
}
}

export const parseEncodedJSONHeader = (
response: Response,
name: string,
): unknown | undefined => {
): unknown => {
try {
const headerValue = response.headers.get(name);
if (headerValue) {
Expand Down
57 changes: 48 additions & 9 deletions packages/core/src/feedback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export async function submitFeedback(
const resolvedOptions = defaults(cloneableOpts, {
...DEFAULT_OPTIONS,
...DEFAULT_SUBMIT_FEEDBACK_OPTIONS,
});
}) as SubmitFeedbackOptions & BaseOptions;

try {
const response = await fetch(
Expand All @@ -69,8 +69,21 @@ export async function submitFeedback(
);

if (!response.ok) {
const error = (await response.json())?.error;
throw new Error(`Failed to submit feedback: ${error || 'Unknown error'}`);
const json: unknown = await response.json();
if (
json &&
typeof json === 'object' &&
'error' in json &&
typeof json.error === 'string'
) {
throw new Error(`Failed to submit feedback: ${json.error}`, {
cause: json,
});
}

throw new Error(`Failed to submit feedback: 'Unknown error'`, {
cause: json,
});
}
} catch (error) {
if (error instanceof DOMException && error.name === 'AbortError') {
Expand Down Expand Up @@ -111,7 +124,7 @@ export async function submitCSAT(
const resolvedOptions = defaults(cloneableOpts, {
...DEFAULT_OPTIONS,
...DEFAULT_SUBMIT_FEEDBACK_OPTIONS,
});
}) as SubmitFeedbackOptions & BaseOptions;

try {
const response = await fetch(
Expand All @@ -129,8 +142,21 @@ export async function submitCSAT(
);

if (!response.ok) {
const error = (await response.json())?.error;
throw new Error(`Failed to submit feedback: ${error || 'Unknown error'}`);
const json: unknown = await response.json();
if (
json &&
typeof json === 'object' &&
'error' in json &&
typeof json.error === 'string'
) {
throw new Error(`Failed to submit feedback: ${json.error}`, {
cause: json,
});
}

throw new Error(`Failed to submit feedback: 'Unknown error'`, {
cause: json,
});
}
} catch (error) {
if (error instanceof DOMException && error.name === 'AbortError') {
Expand Down Expand Up @@ -169,7 +195,7 @@ export async function submitCSATReason(
const resolvedOptions = defaults(cloneableOpts, {
...DEFAULT_OPTIONS,
...DEFAULT_SUBMIT_FEEDBACK_OPTIONS,
});
}) as SubmitFeedbackOptions & BaseOptions;

try {
const response = await fetch(
Expand All @@ -187,8 +213,21 @@ export async function submitCSATReason(
);

if (!response.ok) {
const error = (await response.json())?.error;
throw new Error(`Failed to submit feedback: ${error || 'Unknown error'}`);
const json: unknown = await response.json();
if (
json &&
typeof json === 'object' &&
'error' in json &&
typeof json.error === 'string'
) {
throw new Error(`Failed to submit feedback: ${json.error}`, {
cause: json,
});
}

throw new Error(`Failed to submit feedback: 'Unknown error'`, {
cause: json,
});
}
} catch (error) {
if (error instanceof DOMException && error.name === 'AbortError') {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ let status = 200;
let wait = 0;

const server = setupServer(
http.get(`${DEFAULT_OPTIONS.apiUrl!}/search`, async ({ request }) => {
http.get(`${DEFAULT_OPTIONS.apiUrl}/search`, async ({ request }) => {
const url = new URL(request.url);
const limit = url.searchParams.get('limit');
let data = searchResults;
Expand Down
Loading

0 comments on commit 279674a

Please sign in to comment.