From 199e9ca2b363c1cd6c4c7479aabcba0e2a9bbdb1 Mon Sep 17 00:00:00 2001 From: dan Date: Wed, 18 May 2022 23:02:15 +0100 Subject: [PATCH] [Beta] Fix error precedence (#4666) --- beta/src/components/MDX/Sandpack/Error.tsx | 22 --------------- beta/src/components/MDX/Sandpack/Preview.tsx | 28 +++++++++++--------- beta/src/components/MDX/Sandpack/utils.ts | 9 +++---- 3 files changed, 19 insertions(+), 40 deletions(-) diff --git a/beta/src/components/MDX/Sandpack/Error.tsx b/beta/src/components/MDX/Sandpack/Error.tsx index 9529a99f1..257ce0369 100644 --- a/beta/src/components/MDX/Sandpack/Error.tsx +++ b/beta/src/components/MDX/Sandpack/Error.tsx @@ -24,25 +24,3 @@ export function Error({error}: {error: ErrorType}) { ); } - -export function LintError({ - error: {line, column, message}, -}: { - error: { - line: number; - column: number; - message: string; - }; -}) { - return ( -
-

Lint Error

-
-        {line}:{column} - {message}
-      
-
- ); -} diff --git a/beta/src/components/MDX/Sandpack/Preview.tsx b/beta/src/components/MDX/Sandpack/Preview.tsx index e544b4fd1..9712b2896 100644 --- a/beta/src/components/MDX/Sandpack/Preview.tsx +++ b/beta/src/components/MDX/Sandpack/Preview.tsx @@ -7,7 +7,7 @@ import * as React from 'react'; import {useSandpack, LoadingOverlay} from '@codesandbox/sandpack-react'; import cn from 'classnames'; -import {Error, LintError} from './Error'; +import {Error} from './Error'; import {computeViewportSize, generateRandomId} from './utils'; import type {LintDiagnostic} from './utils'; @@ -59,6 +59,19 @@ export function Preview({ // Work around a noisy internal error. rawError = null; } + + if (lintErrors.length > 0) { + if (rawError == null || rawError.title === 'Runtime Exception') { + // When there's a lint error, show it -- even over a runtime error. + // (However, when there's a build error, we keep showing the build one.) + const {line, column, message} = lintErrors[0]; + rawError = { + title: 'Lint Error', + message: `${line}:${column} - ${message}`, + }; + } + } + // It changes too fast, causing flicker. const error = useDebounced(rawError); @@ -110,7 +123,7 @@ export function Preview({ maxHeight: undefined, } : null; - const hideContent = !isReady || error || lintErrors.length; + const hideContent = !isReady || error; // WARNING: // The layout and styling here is convoluted and really easy to break. @@ -189,17 +202,6 @@ export function Preview({ clientId={clientId.current} loading={!isReady && iframeComputedHeight === null} /> - - {/* - * TODO: properly style the errors - */} - {lintErrors.length > 0 && !error && ( -
-
- -
-
- )} ); diff --git a/beta/src/components/MDX/Sandpack/utils.ts b/beta/src/components/MDX/Sandpack/utils.ts index 4aaa73020..bc8c88a4d 100644 --- a/beta/src/components/MDX/Sandpack/utils.ts +++ b/beta/src/components/MDX/Sandpack/utils.ts @@ -108,15 +108,14 @@ export type LintDiagnostic = { }[]; export const useSandpackLint = () => { - const [lintErrors, setDiagnostic] = useState([]); + const [lintErrors, setLintErrors] = useState([]); const onLint = linter((props: EditorView) => { const editorState = props.state.doc; return import('./eslint-integration').then((module) => { - const {errors} = module.lintDiagnostic(editorState); - - setDiagnostic(errors); - + let {errors} = module.lintDiagnostic(editorState); + // Only show errors from rules, not parsing errors etc + setLintErrors(errors.filter((e) => !e.fatal)); return module.lintDiagnostic(editorState).codeMirrorPayload; }); });