Skip to content

Commit f8c0e18

Browse files
authored
fix: DH-18542: Remove duplicate and invalid ruff quick fixes (#2360)
Should remove the invalid quick fixes to disable syntax errors (wouldn't that be nice if you could do that) Also if there is a duplicate code detected (usually only if you have a selection and use the light bulb instead of the hover quick fix link in the modal), then the "diasble RULE for file" should only appear once. There should also be "disable RULE for line X" and "disable RULE for line Y" instead of just "for line".
1 parent e1a6a22 commit f8c0e18

File tree

1 file changed

+55
-25
lines changed

1 file changed

+55
-25
lines changed

packages/console/src/monaco/MonacoProviders.tsx

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,10 @@ class MonacoProviders extends PureComponent<
258258
d.end_location.row,
259259
d.end_location.column
260260
);
261-
return diagnosticRange.intersectRanges(range);
261+
return (
262+
d.code != null && // Syntax errors have no code and can't be fixed/disabled
263+
diagnosticRange.intersectRanges(range)
264+
);
262265
});
263266

264267
const fixActions: monaco.languages.CodeAction[] = diagnostics
@@ -296,8 +299,24 @@ class MonacoProviders extends PureComponent<
296299
};
297300
});
298301

299-
const disableActions: monaco.languages.CodeAction[] = diagnostics
302+
const seenCodes = new Set<string>();
303+
const duplicateCodes = new Set<string>();
304+
diagnostics.forEach(d => {
305+
if (d.code == null) {
306+
return;
307+
}
308+
if (seenCodes.has(d.code)) {
309+
duplicateCodes.add(d.code);
310+
}
311+
seenCodes.add(d.code);
312+
});
313+
314+
const disableLineActions: monaco.languages.CodeAction[] = diagnostics
300315
.map(d => {
316+
if (d.code == null) {
317+
// The nulls are already filtered out, but TS doesn't know that
318+
return [];
319+
}
301320
const line = model.getLineContent(d.location.row);
302321
const lastToken = monaco.editor
303322
.tokenize(line, model.getLanguageId())[0]
@@ -327,7 +346,11 @@ class MonacoProviders extends PureComponent<
327346
}
328347
return [
329348
{
330-
title: `Disable ${d.code} for this line`,
349+
title: `Disable ${d.code} for ${
350+
duplicateCodes.has(d.code)
351+
? `line ${d.location.row}`
352+
: 'this line'
353+
}`,
331354
kind: 'quickfix',
332355
edit: {
333356
edits: [
@@ -339,33 +362,40 @@ class MonacoProviders extends PureComponent<
339362
],
340363
},
341364
},
365+
];
366+
})
367+
.flat()
368+
.filter(
369+
// Remove actions with duplicate titles as you can't disable the same rule on a line twice
370+
(action, i, arr) => arr.find(a => a.title === action.title) === action
371+
);
372+
373+
const disableGlobalActions: monaco.languages.CodeAction[] = [
374+
...seenCodes,
375+
].map(code => ({
376+
title: `Disable ${code} for this file`,
377+
kind: 'quickfix',
378+
edit: {
379+
edits: [
342380
{
343-
title: `Disable ${d.code} for this file`,
344-
kind: 'quickfix',
345-
edit: {
346-
edits: [
347-
{
348-
resource: model.uri,
349-
versionId: model.getVersionId(),
350-
textEdit: {
351-
range: {
352-
startLineNumber: 1,
353-
startColumn: 1,
354-
endLineNumber: 1,
355-
endColumn: 1,
356-
},
357-
text: `# ruff: noqa: ${d.code}\n`,
358-
},
359-
},
360-
],
381+
resource: model.uri,
382+
versionId: model.getVersionId(),
383+
textEdit: {
384+
range: {
385+
startLineNumber: 1,
386+
startColumn: 1,
387+
endLineNumber: 1,
388+
endColumn: 1,
389+
},
390+
text: `# ruff: noqa: ${code}\n`,
361391
},
362392
},
363-
];
364-
})
365-
.flat();
393+
],
394+
},
395+
}));
366396

367397
return {
368-
actions: [...fixActions, ...disableActions],
398+
actions: [...fixActions, ...disableLineActions, ...disableGlobalActions],
369399
dispose: () => {
370400
/* no-op */
371401
},

0 commit comments

Comments
 (0)