-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathESLintEditor.svelte
278 lines (255 loc) · 7.04 KB
/
ESLintEditor.svelte
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<script>
import MonacoEditor from "./MonacoEditor.svelte"
import { loadMonacoEditor } from "./scripts/monaco-loader"
import { createEventDispatcher, onMount } from "svelte"
const dispatch = createEventDispatcher()
export let linter = null
export let code = ""
export let config = {}
export let options = {}
export let fix = true
export let showDiff = true
export let language = "svelte"
let fixedValue = code
let leftMarkers = []
let rightMarkers = []
let messageMap = new Map()
let editor
export function setCursorPosition(loc) {
if (editor) {
editor.setCursorPosition(loc)
}
}
$: showApplyFix = fix && fixedValue !== code
$: {
lint(linter, code, config, options)
}
onMount(() => {
lint(linter, code, config, options)
})
let lastResult = {}
async function lint(linter, code, config, options) {
messageMap.clear()
/* eslint-disable no-param-reassign -- ignore */
linter = await linter
if (!linter) {
return
}
code = await code
config = await config
options = await options
/* eslint-enable no-param-reassign -- ignore */
const start = Date.now()
// eslint-disable-next-line no-undef -- Workaround X(
if (typeof require === "undefined" && typeof window !== "undefined") {
window.require = function () {
throw new Error()
}
}
const messages = linter.verify(code, config, options)
const time = Date.now() - start
dispatch("time", time)
const fixResult = linter.verifyAndFix(code, config, options)
fixedValue = fixResult.output
dispatch("result", {
messages,
time,
output: fixResult.output,
fixedMessages: fixResult.messages,
})
lastResult = { messages, fixResult }
const markers = await Promise.all(
messages.map((m) => messageToMarker(m, messageMap)),
)
const fixedMarkers = await Promise.all(
fixResult.messages.map((m) => messageToMarker(m)),
)
if (
lastResult.messages !== messages ||
lastResult.fixResult !== fixResult
) {
// If the result has changed, don't update the markers
return
}
leftMarkers = markers
rightMarkers = fixedMarkers
}
function applyFix() {
code = fixedValue
}
/** message to marker */
async function messageToMarker(message, messageMap) {
const monaco = await loadMonacoEditor()
const rule = message.ruleId && (await linter).getRules().get(message.ruleId)
const docUrl = rule && rule.meta && rule.meta.docs && rule.meta.docs.url
const startLineNumber = ensurePositiveInt(message.line, 1)
const startColumn = ensurePositiveInt(message.column, 1)
const endLineNumber = ensurePositiveInt(message.endLine, startLineNumber)
const endColumn = ensurePositiveInt(message.endColumn, startColumn + 1)
const code = docUrl
? { value: message.ruleId, link: docUrl, target: docUrl }
: message.ruleId || "FATAL"
const marker = {
code,
severity: monaco.MarkerSeverity.Error,
source: "ESLint",
message: message.message,
startLineNumber,
startColumn,
endLineNumber,
endColumn,
}
if (messageMap) {
messageMap.set(computeKey(marker), message)
}
return marker
}
/**
* Ensure that a given value is a positive value.
* @param {number|undefined} value The value to check.
* @param {number} defaultValue The default value which is used if the `value` is undefined.
* @returns {number} The positive value as the result.
*/
function ensurePositiveInt(value, defaultValue) {
return Math.max(1, (value !== undefined ? value : defaultValue) | 0)
}
function provideCodeActions(model, _range, context) {
if (context.only !== "quickfix") {
return {
actions: [],
dispose() {
/* nop */
},
}
}
const actions = []
for (const marker of context.markers) {
const message = messageMap.get(computeKey(marker))
if (!message) {
continue
}
if (message.fix) {
actions.push(
createQuickfixCodeAction(
`Fix this ${message.ruleId} problem`,
marker,
model,
message.fix,
),
)
}
if (message.suggestions) {
for (const suggestion of message.suggestions) {
actions.push(
createQuickfixCodeAction(
`${suggestion.desc} (${message.ruleId})`,
marker,
model,
suggestion.fix,
),
)
}
}
}
return {
actions,
dispose() {
/* nop */
},
}
}
/**
* Computes the key string from the given marker.
* @param {import('monaco-editor').editor.IMarkerData} marker marker
* @returns {string} the key string
*/
function computeKey(marker) {
const code =
(typeof marker.code === "string"
? marker.code
: marker.code && marker.code.value) || ""
return `[${marker.startLineNumber},${marker.startColumn},${marker.endLineNumber},${marker.endColumn}]-${code}`
}
/**
* Create quickfix code action.
* @param {string} title title
* @param {import('monaco-editor').editor.IMarkerData} marker marker
* @param {import('monaco-editor').editor.ITextModel} model model
* @param { { range: [number, number], text: string } } fix fix data
* @returns {import('monaco-editor').languages.CodeAction} CodeAction
*/
function createQuickfixCodeAction(title, marker, model, fix) {
const start = model.getPositionAt(fix.range[0])
const end = model.getPositionAt(fix.range[1])
/**
* @type {import('monaco-editor').IRange}
*/
const editRange = {
startLineNumber: start.lineNumber,
startColumn: start.column,
endLineNumber: end.lineNumber,
endColumn: end.column,
}
return {
title,
diagnostics: [marker],
kind: "quickfix",
edit: {
edits: [
{
resource: model.uri,
textEdit: {
range: editRange,
text: fix.text,
},
versionId: model.getVersionId(),
},
],
},
}
}
</script>
<div class="eslint-editor">
<MonacoEditor
bind:this={editor}
bind:code
bind:rightCode={fixedValue}
{language}
diffEditor={fix && showDiff}
markers={leftMarkers}
{rightMarkers}
{provideCodeActions}
/>
<div class="eslint-editor__tools">
{#if showApplyFix}
<button on:click={applyFix} type="button">Apply Fix</button>
{/if}
</div>
</div>
<style>
.eslint-editor {
height: 100%;
position: relative;
}
.eslint-editor__tools {
display: flex;
height: 42px;
position: absolute;
right: 16px;
bottom: 16px;
padding: 8px;
}
.eslint-editor__tools > button {
cursor: pointer;
background-color: transparent;
color: #ddd;
border: solid #ddd 1px;
border-radius: 4px;
outline: none;
padding: 0 16px;
appearance: none;
}
.eslint-editor__tools > button:hover {
background-color: rgba(255, 255, 255, 0.2);
}
</style>