-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathrunESLint.tsx
More file actions
86 lines (72 loc) · 1.93 KB
/
runESLint.tsx
File metadata and controls
86 lines (72 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
// @ts-nocheck
import {Linter} from 'eslint/lib/linter/linter';
import type {Diagnostic} from '@codemirror/lint';
import type {Text} from '@codemirror/text';
const getCodeMirrorPosition = (
doc: Text,
{line, column}: {line: number; column?: number}
): number => {
return doc.line(line).from + (column ?? 0) - 1;
};
const linter = new Linter();
const reactRules = require('eslint-plugin-react-hooks').rules;
linter.defineRules({
'react-hooks/rules-of-hooks': reactRules['rules-of-hooks'],
'react-hooks/exhaustive-deps': reactRules['exhaustive-deps'],
});
const options = {
parserOptions: {
ecmaVersion: 12,
sourceType: 'module',
ecmaFeatures: {jsx: true},
},
rules: {
'react-hooks/rules-of-hooks': 'error',
'react-hooks/exhaustive-deps': 'error',
},
};
export const runESLint = (
doc: Text
): {errors: any[]; codeMirrorErrors: Diagnostic[]} => {
const codeString = doc.toString();
const errors = linter.verify(codeString, options) as any[];
const severity = {
1: 'warning',
2: 'error',
};
const codeMirrorErrors = errors
.map((error) => {
if (!error) return undefined;
const from = getCodeMirrorPosition(doc, {
line: error.line,
column: error.column,
});
const to = getCodeMirrorPosition(doc, {
line: error.endLine ?? error.line,
column: error.endColumn ?? error.column,
});
return {
ruleId: error.ruleId,
from,
to,
severity: severity[error.severity],
message: error.message,
};
})
.filter(Boolean) as Diagnostic[];
return {
codeMirrorErrors,
errors: errors.map((item) => {
return {
...item,
severity: severity[item.severity],
};
}),
};
};