Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion crates/oxc_language_server/src/code_actions.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use log::debug;
use tower_lsp_server::lsp_types::{CodeAction, CodeActionKind, TextEdit, Uri, WorkspaceEdit};

use crate::linter::error_with_position::{DiagnosticReport, FixedContent, PossibleFixContent};
Expand Down Expand Up @@ -87,7 +88,30 @@ pub fn apply_all_fix_code_action<'a>(
PossibleFixContent::Single(fixed_content) => Some(fixed_content),
// For multiple fixes, we take the first one as a representative fix.
// Applying all possible fixes at once is not possible in this context.
PossibleFixContent::Multiple(multi) => multi.first(),
PossibleFixContent::Multiple(multi) => {
// for a real linter fix, we expect at least 3 fixes
if multi.len() > 2 {
multi.first()
} else {
debug!("Multiple fixes found, but only ignore fixes available");
#[cfg(debug_assertions)]
{
if !multi.is_empty() {
debug_assert!(multi[0].message.as_ref().is_some());
debug_assert!(
multi[0].message.as_ref().unwrap().starts_with("Disable")
);
debug_assert!(
multi[0].message.as_ref().unwrap().ends_with("for this line")
);
}
}

// this fix is only for "ignore this line/file" fixes
// do not apply them for "fix all" code action
None
}
}
};

if let Some(fixed_content) = &fix {
Expand Down
24 changes: 23 additions & 1 deletion crates/oxc_language_server/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,29 @@ impl WorkspaceWorker {
PossibleFixContent::Single(fixed_content) => Some(fixed_content),
// For multiple fixes, we take the first one as a representative fix.
// Applying all possible fixes at once is not possible in this context.
PossibleFixContent::Multiple(multi) => multi.first(),
PossibleFixContent::Multiple(multi) => {
// for a real linter fix, we expect at least 3 fixes
if multi.len() > 2 {
multi.first()
} else {
debug!("Multiple fixes found, but only ignore fixes available");
#[cfg(debug_assertions)]
{
if !multi.is_empty() {
debug_assert!(multi[0].message.as_ref().is_some());
debug_assert!(
multi[0].message.as_ref().unwrap().starts_with("Disable")
);
debug_assert!(
multi[0].message.as_ref().unwrap().ends_with("for this line")
);
}
}
// this fix is only for "ignore this line/file" fixes
// do not apply them for "fix all" code action
None
}
}
};

if let Some(fixed_content) = &fix {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"no-const-assign": "error"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const a = 0;
a = 1;

console.log(a);
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const a = 0;
a = 1;

console.log(a);
29 changes: 29 additions & 0 deletions editors/vscode/tests/code_actions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,35 @@ suite('code actions', () => {
strictEqual(content.toString(), expected.toString());
});

// https://discord.com/channels/1079625926024900739/1080723403595591700/1422191300395929620
test('code action `source.fixAll.oxc` ignores "ignore this rule for this line/file"', async () => {
let file = Uri.joinPath(fixturesWorkspaceUri(), 'fixtures', 'file2.js');
let expectedFile = Uri.joinPath(fixturesWorkspaceUri(), 'fixtures', 'expected.txt');

await workspace.getConfiguration('editor').update('codeActionsOnSave', {
'source.fixAll.oxc': 'always',
});
await workspace.saveAll();

const range = new Range(new Position(0, 0), new Position(0, 0));
const edit = new WorkspaceEdit();
edit.replace(file, range, ' ');

await sleep(1000);

await loadFixture('fixall_code_action_ignore_only_disable_fix');
await workspace.openTextDocument(file);
await workspace.applyEdit(edit);
await sleep(1000);
await workspace.saveAll();
await sleep(500);

const content = await workspace.fs.readFile(file);
const expected = await workspace.fs.readFile(expectedFile);

strictEqual(content.toString(), expected.toString());
});

test('changing configuration flag "fix_kind" will reveal more code actions', async () => {
await loadFixture('changing_fix_kind');
const fileUri = Uri.joinPath(fixturesWorkspaceUri(), 'fixtures', 'for_direction.ts');
Expand Down
Loading