feat(lint): add too_many_instance_attributes rule

This commit is contained in:
GreasySlug 2024-05-27 01:00:36 +09:00
parent 2282d12a92
commit 1a0daed8a4
2 changed files with 57 additions and 2 deletions

View file

@ -8,9 +8,11 @@ use erg_common::traits::{BlockKind, ExitStatus, Locational, New, Runnable, Strea
use erg_compiler::artifact::{Buildable, ErrorArtifact};
use erg_compiler::build_package::PackageBuilder;
use erg_compiler::error::{CompileError, CompileErrors, CompileWarnings};
use erg_compiler::hir::{Accessor, Def, Dict, Expr, List, Literal, Set, Signature, Tuple, HIR};
use erg_compiler::hir::{
Accessor, ClassDef, Def, Dict, Expr, List, Literal, Set, Signature, Tuple, HIR,
};
use erg_compiler::module::SharedCompilerResource;
use erg_compiler::ty::ValueObj;
use erg_compiler::ty::{value::TypeObj, Type, ValueObj};
use erg_parser::token::TokenKind;
use erg_parser::ParserRunner;
@ -155,6 +157,7 @@ impl Linter {
for chunk in hir.module.iter() {
self.lint_too_many_params(chunk);
self.lint_bool_comparison(chunk);
self.lint_too_many_instance_attributes(chunk);
}
log!(info "Finished linting");
self.warns.take()
@ -216,6 +219,25 @@ impl Linter {
}
}
fn lint_too_many_instance_attributes(&mut self, expr: &Expr) {
const MAX_INSTANCE_ATTRIBUTES: usize = 36;
if let Expr::ClassDef(ClassDef { obj, .. }) = expr {
if let Some(TypeObj::Builtin {
t: Type::Record(record),
..
}) = obj.base_or_sup()
{
if record.len() >= MAX_INSTANCE_ATTRIBUTES {
self.warns.push(too_many_instance_attributes(
self.input(),
self.caused_by(),
expr.loc(),
));
}
}
}
}
/* ↓ Helper methods ↓ */
fn check_recursively(&mut self, lint_fn: &impl Fn(&mut Linter, &Expr), expr: &Expr) {
match expr {

View file

@ -1,5 +1,6 @@
use erg_common::error::{ErrorCore, ErrorKind, Location, SubMessage};
use erg_common::io::Input;
use erg_common::switch_lang;
use erg_common::traits::NoTypeDisplay;
use erg_compiler::error::CompileWarning;
use erg_compiler::hir::Expr;
@ -18,6 +19,38 @@ pub(crate) fn too_many_params(input: Input, caused_by: String, loc: Location) ->
)
}
pub(crate) fn too_many_instance_attributes(
input: Input,
caused_by: String,
loc: Location,
) -> CompileWarning {
let msg = switch_lang!(
"japanese" => "インスタンス属性が多すぎます",
"simplified_chinese" => "实例属性过多",
"traditional_chinese" => "實例屬性過多",
"english" => "too many instance attributes",
)
.to_string();
let hint = switch_lang!(
"japanese" => "サブクラスやデータクラスを活用してください",
"simplified_chinese" => "利用子类和数据类",
"traditional_chinese" => "利用子類和資料類",
"english" => "take advantage of subclasses and data classes",
)
.to_string();
CompileWarning::new(
ErrorCore::new(
vec![SubMessage::ambiguous_new(loc, vec![], Some(hint))],
msg,
0,
ErrorKind::AttributeWarning,
loc,
),
input,
caused_by,
)
}
pub(crate) fn true_comparison(
expr: &Expr,
input: Input,