Danilo Woznica
3 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with
10 additions and
8 deletions
-
beta/src/components/MDX/Sandpack/Preview.tsx
-
beta/src/components/MDX/Sandpack/runESLint.tsx
-
beta/src/components/MDX/Sandpack/useSandpackLint.tsx
|
|
@ -7,7 +7,7 @@ import * as React from 'react'; |
|
|
|
import {useSandpack, LoadingOverlay} from '@codesandbox/sandpack-react'; |
|
|
|
import cn from 'classnames'; |
|
|
|
import {Error} from './Error'; |
|
|
|
import type {LintDiagnostic} from './utils'; |
|
|
|
import type {LintDiagnostic} from './useSandpackLint'; |
|
|
|
|
|
|
|
const generateRandomId = (): string => |
|
|
|
Math.floor(Math.random() * 10000).toString(); |
|
|
|
|
|
@ -41,7 +41,7 @@ const options = { |
|
|
|
|
|
|
|
export const runESLint = ( |
|
|
|
doc: Text |
|
|
|
): {errors: any[]; codeMirrorPayload: Diagnostic[]} => { |
|
|
|
): {errors: any[]; codeMirrorErrors: Diagnostic[]} => { |
|
|
|
const codeString = doc.toString(); |
|
|
|
const errors = linter.verify(codeString, options) as any[]; |
|
|
|
|
|
|
@ -50,7 +50,7 @@ export const runESLint = ( |
|
|
|
2: 'error', |
|
|
|
}; |
|
|
|
|
|
|
|
const codeMirrorPayload = errors |
|
|
|
const codeMirrorErrors = errors |
|
|
|
.map((error) => { |
|
|
|
if (!error) return undefined; |
|
|
|
|
|
|
@ -65,6 +65,7 @@ export const runESLint = ( |
|
|
|
}); |
|
|
|
|
|
|
|
return { |
|
|
|
ruleId: error.ruleId, |
|
|
|
from, |
|
|
|
to, |
|
|
|
severity: severity[error.severity], |
|
|
@ -74,7 +75,7 @@ export const runESLint = ( |
|
|
|
.filter(Boolean) as Diagnostic[]; |
|
|
|
|
|
|
|
return { |
|
|
|
codeMirrorPayload, |
|
|
|
codeMirrorErrors, |
|
|
|
errors: errors.map((item) => { |
|
|
|
return { |
|
|
|
...item, |
|
|
|
|
|
@ -25,10 +25,11 @@ export const useSandpackLint = () => { |
|
|
|
const onLint = linter(async (props: EditorView) => { |
|
|
|
const {runESLint} = await import('./runESLint'); |
|
|
|
const editorState = props.state.doc; |
|
|
|
let {errors, codeMirrorPayload} = runESLint(editorState); |
|
|
|
// Only show errors from rules, not parsing errors etc
|
|
|
|
setLintErrors(errors.filter((e) => !e.fatal)); |
|
|
|
return codeMirrorPayload; |
|
|
|
let {errors, codeMirrorErrors} = runESLint(editorState); |
|
|
|
// Ignore parsing or internal linter errors.
|
|
|
|
const isReactRuleError = (error: any) => error.ruleId != null; |
|
|
|
setLintErrors(errors.filter(isReactRuleError)); |
|
|
|
return codeMirrorErrors.filter(isReactRuleError); |
|
|
|
}); |
|
|
|
|
|
|
|
return {lintErrors, lintExtensions: [onLint]}; |
|
|
|