Skip to content

Commit

Permalink
Rename fixed_code to suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
chriscerie committed Oct 19, 2023
1 parent 6749c77 commit 67b740a
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 49 deletions.
20 changes: 10 additions & 10 deletions selene-lib/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub struct Diagnostic {
pub notes: Vec<String>,
pub primary_label: Label,
pub secondary_labels: Vec<Label>,
pub fixed_code: Option<String>,
pub suggestion: Option<String>,
pub applicability: Applicability,
}

Expand All @@ -126,11 +126,11 @@ impl Diagnostic {
code: &'static str,
message: String,
primary_label: Label,
fixed_code: Option<String>,
suggestion: Option<String>,
applicability: Applicability,
) -> Self {
let notes = if let Some(ref fixed_code_str) = fixed_code {
vec![format!("try: `{}`", fixed_code_str)]
let notes = if let Some(ref suggestion_str) = suggestion {
vec![format!("try: `{}`", suggestion_str)]
} else {
Vec::new()
};
Expand All @@ -139,7 +139,7 @@ impl Diagnostic {
code,
message,
primary_label,
fixed_code,
suggestion,
applicability,
notes,

Expand All @@ -153,7 +153,7 @@ impl Diagnostic {
primary_label: Label,
notes: Vec<String>,
secondary_labels: Vec<Label>,
fixed_code: Option<String>,
suggestion: Option<String>,
applicability: Applicability,
) -> Self {
Self {
Expand All @@ -162,7 +162,7 @@ impl Diagnostic {
notes,
primary_label,
secondary_labels,
fixed_code,
suggestion,
applicability,
}
}
Expand Down Expand Up @@ -193,11 +193,11 @@ impl Diagnostic {
}

pub fn has_machine_applicable_fix(&self) -> bool {
self.fixed_code.is_some() && self.applicability == Applicability::MachineApplicable
self.suggestion.is_some() && self.applicability == Applicability::MachineApplicable
}

pub fn has_maybe_incorrect_fix(&self) -> bool {
self.fixed_code.is_some() && self.applicability == Applicability::MaybeIncorrect
self.suggestion.is_some() && self.applicability == Applicability::MaybeIncorrect
}

/// After applying suggestions, calls `get_new_diagnostics` and reruns to ensure fixes didn't produce new errors
Expand All @@ -219,7 +219,7 @@ impl Diagnostic {
let mut bytes_offset = 0;

for diagnostic in chosen_diagnostics {
if let Some(suggestion) = &diagnostic.fixed_code {
if let Some(suggestion) = &diagnostic.suggestion {
let (start, end) = diagnostic.primary_label.range;

// This conversion can theoretically overflow, but it's tied to string length so the user
Expand Down
4 changes: 2 additions & 2 deletions selene-lib/src/lints/compare_nan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Lint for CompareNanLint {
.comparisons
.iter()
.map(|comparisons| {
let fixed_code = format!(
let suggestion = format!(
"{variable} {operator} {variable}",
variable = comparisons.variable,
operator = comparisons.operator,
Expand All @@ -40,7 +40,7 @@ impl Lint for CompareNanLint {
"compare_nan",
"comparing things to nan directly is not allowed".to_owned(),
Label::new(comparisons.range),
Some(fixed_code),
Some(suggestion),
Applicability::MaybeIncorrect,
)
})
Expand Down
6 changes: 3 additions & 3 deletions selene-lib/src/lints/deprecated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ impl<'a> DeprecatedVisitor<'a> {

let mut notes = vec![deprecated.message.to_owned()];

let mut fixed_code = None;
let mut suggestion = None;

if let Some(replace_with) = deprecated.try_instead(parameters) {
notes.push(format!("try: {replace_with}"));
fixed_code = Some(replace_with);
suggestion = Some(replace_with);
}

self.diagnostics.push(Diagnostic::new_complete(
Expand All @@ -131,7 +131,7 @@ impl<'a> DeprecatedVisitor<'a> {
Label::from_node(node, None),
notes,
Vec::new(),
fixed_code,
suggestion,
Applicability::MaybeIncorrect,
));
}
Expand Down
16 changes: 8 additions & 8 deletions selene-lib/src/lints/test_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,18 @@ pub fn test_lint_config_with_output<
let mut diagnostics = lint.pass(&ast, &context, &AstContext::from_ast(&ast, &lua_source));
diagnostics.sort_by_key(|diagnostic| diagnostic.primary_label.range);

let mut fixed_code = lua_source.to_string();
let mut suggestion = lua_source.to_string();
let mut fixed_diagnostics = diagnostics
.iter()
.filter(|diagnostic| {
diagnostic.fixed_code.is_some()
diagnostic.suggestion.is_some()
&& (diagnostic.applicability == Applicability::MachineApplicable
|| diagnostic.applicability == Applicability::MaybeIncorrect)
})
.collect::<Vec<_>>();

fixed_code = Diagnostic::get_applied_suggestions_code(
fixed_code.as_str(),
suggestion = Diagnostic::get_applied_suggestions_code(
suggestion.as_str(),
fixed_diagnostics,
|new_code| {
let fixed_ast = full_moon::parse(new_code).unwrap_or_else(|_| {
Expand All @@ -161,26 +161,26 @@ pub fn test_lint_config_with_output<
},
);

let fixed_ast = full_moon::parse(&fixed_code).unwrap_or_else(|_| {
let fixed_ast = full_moon::parse(&suggestion).unwrap_or_else(|_| {
panic!(
"Fixer generated invalid code:\n\
----------------\n\
{}\n\
----------------\n",
fixed_code
suggestion
)
});
let lint_results = lint.pass(
&fixed_ast,
&context,
&AstContext::from_ast(&fixed_ast, &fixed_code),
&AstContext::from_ast(&fixed_ast, &suggestion),
);
fixed_diagnostics = lint_results.iter().collect::<Vec<_>>();
fixed_diagnostics.sort_by_key(|diagnostic| diagnostic.start_position());

let fixed_diff = generate_diff(
&lua_source,
&fixed_code,
&suggestion,
&diagnostics.iter().collect::<Vec<_>>(),
);
let diff_output_path = path_base.with_extension("fixed.diff");
Expand Down
8 changes: 4 additions & 4 deletions selene-lib/src/lints/unused_variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl Lint for UnusedVariableLint {
let mut notes = Vec::new();
let mut report_range = variable.identifiers[0];

let mut fixed_code = Some(format!("_{}", variable.name));
let mut suggestion = Some(format!("_{}", variable.name));
let mut applicability = Applicability::MachineApplicable;

if variable.is_self {
Expand All @@ -177,7 +177,7 @@ impl Lint for UnusedVariableLint {

// Applying fix by changing `:` to `.` would break any existing methods calls
applicability = Applicability::MaybeIncorrect;
fixed_code = Some(".".to_string());
suggestion = Some(".".to_string());
}
}

Expand All @@ -189,7 +189,7 @@ impl Lint for UnusedVariableLint {
.0
!= variable.identifiers[0].0
}) {
fixed_code = None;
suggestion = None;
}

let write_only = !analyzed_references.is_empty();
Expand All @@ -213,7 +213,7 @@ impl Lint for UnusedVariableLint {
}
})
.collect(),
fixed_code,
suggestion,
applicability,
));
};
Expand Down
6 changes: 3 additions & 3 deletions selene-vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class SeleneCodeActionProvider implements vscode.CodeActionProvider {
for (const diagnostic of context.diagnostics) {
if (
diagnostic instanceof SeleneDiagnostic &&
diagnostic.fixed_code
diagnostic.suggestion
) {
const action = new vscode.CodeAction(
`fix: ${diagnostic.message}`,
Expand All @@ -46,7 +46,7 @@ class SeleneCodeActionProvider implements vscode.CodeActionProvider {
action.edit.replace(
document.uri,
diagnostic.range,
diagnostic.fixed_code,
diagnostic.suggestion,
)

codeActions.push(action)
Expand Down Expand Up @@ -256,7 +256,7 @@ export async function activate(
data.severity === Severity.Error
? vscode.DiagnosticSeverity.Error
: vscode.DiagnosticSeverity.Warning,
data.fixed_code,
data.suggestion,
)

diagnostic.source = `selene::${data.code}`
Expand Down
8 changes: 4 additions & 4 deletions selene-vscode/src/structures/diagnostic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ export interface Diagnostic {
notes: string[]
primary_label: Label
secondary_labels: Label[]
fixed_code: string
suggestion: string
}

export class SeleneDiagnostic extends vscode.Diagnostic {
fixed_code?: string
suggestion?: string

constructor(
range: vscode.Range,
message: string,
severity: vscode.DiagnosticSeverity,
fixed_code?: string,
suggestion?: string,
) {
super(range, message, severity)
this.fixed_code = fixed_code
this.suggestion = suggestion
}
}
6 changes: 3 additions & 3 deletions selene/src/json_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct JsonDiagnostic {
primary_label: Label,
notes: Vec<String>,
secondary_labels: Vec<Label>,
fixed_code: Option<String>,
suggestion: Option<String>,
}

#[derive(Serialize)]
Expand Down Expand Up @@ -78,7 +78,7 @@ fn label_to_serializable(
pub fn diagnostic_to_json(
diagnostic: &CodespanDiagnostic<codespan::FileId>,
files: &codespan::Files<&str>,
fixed_code: Option<String>,
suggestion: Option<String>,
) -> JsonDiagnostic {
let label = diagnostic.labels.first().expect("no labels passed");
let filename = files.name(label.file_id).to_string_lossy().into_owned();
Expand All @@ -95,7 +95,7 @@ pub fn diagnostic_to_json(
.filter(|label| label.style == LabelStyle::Secondary)
.map(|label| label_to_serializable(&filename, label, files))
.collect(),
fixed_code,
suggestion,
}
}

Expand Down
24 changes: 12 additions & 12 deletions selene/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ fn emit_codespan(
writer: &mut impl termcolor::WriteColor,
files: &codespan::Files<&str>,
diagnostic: &CodespanDiagnostic<codespan::FileId>,
fixed_code: Option<String>,
suggestion: Option<String>,
) {
let lock = OPTIONS.read().unwrap();
let opts = lock.as_ref().unwrap();
Expand All @@ -150,7 +150,7 @@ fn emit_codespan(
writer,
"{}",
serde_json::to_string(&json_output::diagnostic_to_json(
diagnostic, files, fixed_code
diagnostic, files, suggestion
))
.unwrap()
)
Expand All @@ -162,7 +162,7 @@ fn emit_codespan(
writer,
"{}",
serde_json::to_string(&json_output::JsonOutput::Diagnostic(
json_output::diagnostic_to_json(diagnostic, files, fixed_code)
json_output::diagnostic_to_json(diagnostic, files, suggestion)
))
.unwrap()
)
Expand Down Expand Up @@ -286,7 +286,7 @@ fn read<R: Read>(
let mut diagnostics = checker.test_on(&ast, &contents.as_ref().to_string());
diagnostics.sort_by_key(|diagnostic| diagnostic.diagnostic.start_position());

let mut fixed_code = contents.as_ref().to_string();
let mut suggestion = contents.as_ref().to_string();
if is_fix {
// This only counts the number of inital automatic fixes. Additional fixes caused by a previous fix won't
// be counted
Expand All @@ -295,8 +295,8 @@ fn read<R: Read>(
.filter(|diagnostic| diagnostic.diagnostic.has_machine_applicable_fix())
.count();

fixed_code = Diagnostic::get_applied_suggestions_code(
fixed_code.as_str(),
suggestion = Diagnostic::get_applied_suggestions_code(
suggestion.as_str(),
diagnostics
.iter()
.filter(|diagnostic| diagnostic.diagnostic.has_machine_applicable_fix())
Expand All @@ -317,16 +317,16 @@ fn read<R: Read>(
},
);

let fixed_ast = full_moon::parse(&fixed_code).expect(
let fixed_ast = full_moon::parse(&suggestion).expect(
"selene tried applying lint suggestions, but it generated invalid code that could not be parsed; \
this is likely a selene bug",
);
diagnostics = checker.test_on(&fixed_ast, &fixed_code);
diagnostics = checker.test_on(&fixed_ast, &suggestion);
diagnostics.sort_by_key(|diagnostic| diagnostic.diagnostic.start_position());

if num_fixes > 0 {
if fs::write(filename, fixed_code.clone()).is_ok() {
files.update(source_id, &fixed_code);
if fs::write(filename, suggestion.clone()).is_ok() {
files.update(source_id, &suggestion);

let stdout = StandardStream::stdout(get_color());
let mut stdout = stdout.lock();
Expand Down Expand Up @@ -444,7 +444,7 @@ fn read<R: Read>(
write(&mut stack, new_start).unwrap();
}
} else {
let fixed_code = diagnostic.diagnostic.fixed_code.clone();
let suggestion = diagnostic.diagnostic.suggestion.clone();
let diagnostic = diagnostic.diagnostic.into_codespan_diagnostic(
source_id,
match diagnostic.severity {
Expand All @@ -454,7 +454,7 @@ fn read<R: Read>(
},
);

emit_codespan(&mut stdout, &files, &diagnostic, fixed_code);
emit_codespan(&mut stdout, &files, &diagnostic, suggestion);
}
}
}
Expand Down

0 comments on commit 67b740a

Please sign in to comment.