Skip to content

Commit

Permalink
feat: auto fix tools. Add '--fix' param for kclvm_cli lint and auto f…
Browse files Browse the repository at this point in the history
…ix unused import and reimport warning
  • Loading branch information
He1pa committed Sep 18, 2023
1 parent 139c3a3 commit 1d041df
Show file tree
Hide file tree
Showing 19 changed files with 478 additions and 4 deletions.
3 changes: 2 additions & 1 deletion kclvm/cmd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ pub fn app() -> Command {
.arg(arg!(path_selector: -S --path_selector <path_selector> ... "Specify the path selector").num_args(1..))
.arg(arg!(overrides: -O --overrides <overrides> ... "Specify the configuration override path and value").num_args(1..))
.arg(arg!(target: --target <target> "Specify the target type"))
.arg(arg!(package_map: -E --external <package_map> ... "Mapping of package name and path where the package is located").num_args(1..)),
.arg(arg!(package_map: -E --external <package_map> ... "Mapping of package name and path where the package is located").num_args(1..))
.arg(arg!(fix: -f --fix "Auto fix")),
)
.subcommand(
Command::new("fmt")
Expand Down
9 changes: 8 additions & 1 deletion kclvm/cmd/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use anyhow::Result;
use clap::ArgMatches;
use kclvm_error::Handler;
use kclvm_runner::ExecProgramArgs;
use kclvm_tools::lint::lint_files;
use kclvm_tools::{fix, lint::lint_files};

use crate::settings::must_build_settings;

Expand All @@ -28,6 +28,13 @@ pub fn lint_command(matches: &ArgMatches) -> Result<()> {
if bool_from_matches(matches, "emit_warning").unwrap_or_default() {
warning_handler.emit()?;
}

if bool_from_matches(matches, "fix").unwrap_or_default() {
let mut diags = vec![];
diags.extend(err_handler.diagnostics.clone());
diags.extend(warning_handler.diagnostics);
fix::fix(diags).unwrap();
}
err_handler.abort_if_any_errors();
Ok(())
}
5 changes: 4 additions & 1 deletion kclvm/error/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl From<Loc> for Position {

impl Diagnostic {
pub fn new(level: Level, message: &str, range: Range) -> Self {
Diagnostic::new_with_code(level, message, None, range, None)
Diagnostic::new_with_code(level, message, None, range, None, None)
}

/// New a diagnostic with error code.
Expand All @@ -106,6 +106,7 @@ impl Diagnostic {
note: Option<&str>,
range: Range,
code: Option<DiagnosticId>,
suggested_replacement: Option<String>,
) -> Self {
Diagnostic {
level,
Expand All @@ -114,6 +115,7 @@ impl Diagnostic {
style: Style::LineAndColumn,
message: message.to_string(),
note: note.map(|s| s.to_string()),
suggested_replacement,
}],
code,
}
Expand All @@ -133,6 +135,7 @@ pub struct Message {
pub style: Style,
pub message: String,
pub note: Option<String>,
pub suggested_replacement: Option<String>,
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
Expand Down
17 changes: 16 additions & 1 deletion kclvm/error/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ impl Handler {
None,
range,
Some(DiagnosticId::Error(E1001.kind)),
None,
);
self.add_diagnostic(diag);

Expand All @@ -114,6 +115,7 @@ impl Handler {
None,
range,
Some(DiagnosticId::Error(E2G22.kind)),
None,
);
self.add_diagnostic(diag);

Expand All @@ -128,6 +130,7 @@ impl Handler {
None,
range,
Some(DiagnosticId::Error(E2L23.kind)),
None,
);
self.add_diagnostic(diag);

Expand All @@ -151,6 +154,7 @@ impl Handler {
/// style: Style::LineAndColumn,
/// message: "Invalid syntax: expected '+', got '-'".to_string(),
/// note: None,
/// suggested_replacement: Some("".to_string()),
/// }
/// ]);
/// ```
Expand All @@ -175,6 +179,7 @@ impl Handler {
/// style: Style::LineAndColumn,
/// message: "Module 'a' imported but unused.".to_string(),
/// note: None,
/// suggested_replacement: Some("".to_string()),
/// }],
/// );
/// ```
Expand Down Expand Up @@ -235,7 +240,14 @@ impl From<PanicInfo> for Diagnostic {
line: panic_info.kcl_line as u64,
column: None,
};
Diagnostic::new_with_code(Level::Error, &panic_msg, None, (pos.clone(), pos), None)
Diagnostic::new_with_code(
Level::Error,
&panic_msg,
None,
(pos.clone(), pos),
None,
None,
)
} else {
let mut backtrace_msg = "backtrace:\n".to_string();
let mut backtrace = panic_info.backtrace.clone();
Expand All @@ -261,6 +273,7 @@ impl From<PanicInfo> for Diagnostic {
Some(&backtrace_msg),
(pos.clone(), pos),
None,
None,
)
};

Expand All @@ -278,6 +291,7 @@ impl From<PanicInfo> for Diagnostic {
None,
(pos.clone(), pos),
None,
None,
);
config_meta_diag.messages.append(&mut diag.messages);
config_meta_diag
Expand Down Expand Up @@ -334,6 +348,7 @@ impl ParseError {
None,
(pos.clone(), pos),
Some(DiagnosticId::Error(ErrorKind::InvalidSyntax)),
None,
))
}
}
Expand Down
3 changes: 3 additions & 0 deletions kclvm/parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ impl Loader {
pkg_path
),
note: None,
suggested_replacement: None,
}],
);
return Ok(None);
Expand All @@ -402,6 +403,7 @@ impl Loader {
style: Style::Line,
message: format!("pkgpath {} not found in the program", pkg_path),
note: None,
suggested_replacement: None,
}],
);
return Ok(None);
Expand Down Expand Up @@ -498,6 +500,7 @@ impl Loader {
style: Style::Line,
message: format!("the plugin package `{}` is not found, please confirm if plugin mode is enabled", pkgpath),
note: None,
suggested_replacement: None,
}],
);
}
Expand Down
3 changes: 3 additions & 0 deletions kclvm/sema/src/lint/lints_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ impl LintPass for ImportPosition {
note: Some(
"Consider moving tihs statement to the top of the file".to_string(),
),
suggested_replacement: None,
}],
);
}
Expand Down Expand Up @@ -108,6 +109,7 @@ impl LintPass for UnusedImport {
style: Style::Line,
message: format!("Module '{}' imported but unused", scope_obj.name),
note: Some("Consider removing this statement".to_string()),
suggested_replacement: Some("".to_string()),
}],
);
}
Expand Down Expand Up @@ -161,6 +163,7 @@ impl LintPass for ReImport {
&import_stmt.name
),
note: Some("Consider removing this statement".to_string()),
suggested_replacement: Some("".to_string()),
}],
);
} else {
Expand Down
1 change: 1 addition & 0 deletions kclvm/sema/src/resolver/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ impl<'ctx> Resolver<'ctx> {
attr_ty.ty_str()
),
note: None,
suggested_replacement: None,
}],
);
}
Expand Down
1 change: 1 addition & 0 deletions kclvm/sema/src/resolver/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@ impl<'ctx> Resolver<'ctx> {
val_ty.ty_str()
),
note: None,
suggested_replacement: None,
}],
);
}
Expand Down
20 changes: 20 additions & 0 deletions kclvm/sema/src/resolver/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ impl<'ctx> Resolver<'ctx> {
style: Style::LineAndColumn,
message: format!("unique key error name '{}'", name),
note: None,
suggested_replacement: None,
}],
);
continue;
Expand Down Expand Up @@ -184,6 +185,7 @@ impl<'ctx> Resolver<'ctx> {
style: Style::Line,
message: format!("pkgpath {} not found in the program", self.ctx.pkgpath),
note: None,
suggested_replacement: None,
}],
);
}
Expand Down Expand Up @@ -239,6 +241,7 @@ impl<'ctx> Resolver<'ctx> {
name
),
note: None,
suggested_replacement: None,
},
Message {
range: self
Expand All @@ -255,6 +258,7 @@ impl<'ctx> Resolver<'ctx> {
"change the variable name to '_{}' to make it mutable",
name
)),
suggested_replacement: None,
},
],
);
Expand All @@ -278,12 +282,14 @@ impl<'ctx> Resolver<'ctx> {
obj.ty.ty_str()
),
note: None,
suggested_replacement: None,
},
Message {
range: obj.get_span_pos(),
style: Style::LineAndColumn,
message: format!("expected {}", obj.ty.ty_str()),
note: None,
suggested_replacement: None,
},
],
);
Expand Down Expand Up @@ -333,6 +339,7 @@ impl<'ctx> Resolver<'ctx> {
name
),
note: None,
suggested_replacement: None,
},
Message {
range: self
Expand All @@ -349,6 +356,7 @@ impl<'ctx> Resolver<'ctx> {
"change the variable name to '_{}' to make it mutable",
name
)),
suggested_replacement: None,
},
],
);
Expand Down Expand Up @@ -390,6 +398,7 @@ impl<'ctx> Resolver<'ctx> {
ty.ty_str()
),
note: None,
suggested_replacement: None,
}],
);
None
Expand All @@ -413,6 +422,7 @@ impl<'ctx> Resolver<'ctx> {
style: Style::LineAndColumn,
message: "only schema mixin can inherit from protocol".to_string(),
note: None,
suggested_replacement: None,
}],
);
return None;
Expand All @@ -434,6 +444,7 @@ impl<'ctx> Resolver<'ctx> {
ty.ty_str()
),
note: None,
suggested_replacement: None,
}],
);
None
Expand Down Expand Up @@ -467,6 +478,7 @@ impl<'ctx> Resolver<'ctx> {
ty.ty_str()
),
note: None,
suggested_replacement: None,
}],
);
None
Expand Down Expand Up @@ -503,6 +515,7 @@ impl<'ctx> Resolver<'ctx> {
style: Style::LineAndColumn,
message: format!("schema protocol name must end with '{}'", PROTOCOL_SUFFIX),
note: None,
suggested_replacement: None,
}],
);
}
Expand All @@ -523,6 +536,7 @@ impl<'ctx> Resolver<'ctx> {
style: Style::LineAndColumn,
message: format!("mixin inheritance {} is prohibited", parent_name),
note: None,
suggested_replacement: None,
}],
);
}
Expand All @@ -541,6 +555,7 @@ impl<'ctx> Resolver<'ctx> {
style: Style::LineAndColumn,
message: format!("index signature attribute name '{}' cannot have the same name as schema attributes", index_sign_name),
note: None,
suggested_replacement: None,
}],
);
}
Expand All @@ -565,6 +580,7 @@ impl<'ctx> Resolver<'ctx> {
style: Style::LineAndColumn,
message: format!("invalid index signature key type: '{}'", key_ty.ty_str()),
note: None,
suggested_replacement: None,
}],
);
}
Expand Down Expand Up @@ -707,6 +723,7 @@ impl<'ctx> Resolver<'ctx> {
style: Style::LineAndColumn,
message: format!("the type '{}' of schema attribute '{}' does not meet the index signature definition {}", ty.ty_str(), name, index_signature_obj.ty_str()),
note: None,
suggested_replacement: None,
}],
);
}
Expand All @@ -727,6 +744,7 @@ impl<'ctx> Resolver<'ctx> {
mixin_names[mixin_names.len() - 1]
),
note: None,
suggested_replacement: None,
}],
);
}
Expand All @@ -748,6 +766,7 @@ impl<'ctx> Resolver<'ctx> {
ty.ty_str()
),
note: None,
suggested_replacement: None,
}],
);
None
Expand Down Expand Up @@ -873,6 +892,7 @@ impl<'ctx> Resolver<'ctx> {
style: Style::LineAndColumn,
message: format!("illegal rule type '{}'", ty.ty_str()),
note: None,
suggested_replacement: None,
}],
);
None
Expand Down
2 changes: 2 additions & 0 deletions kclvm/sema/src/resolver/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ impl<'ctx> Resolver<'ctx> {
real_path.to_str().unwrap()
),
note: None,
suggested_replacement: None,
}],
);
} else {
Expand All @@ -60,6 +61,7 @@ impl<'ctx> Resolver<'ctx> {
file
),
note: None,
suggested_replacement: None,
}],
);
}
Expand Down
Loading

0 comments on commit 1d041df

Please sign in to comment.