Skip to content

Commit

Permalink
feat: add Linter
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Mar 24, 2024
1 parent d7d422d commit 11f2b48
Show file tree
Hide file tree
Showing 13 changed files with 422 additions and 18 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 11 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ homepage.workspace = true
members = [
"crates/erg_common",
"crates/erg_compiler",
"crates/erg_linter/",
"crates/erg_parser",
"crates/els",
"crates/erg_proc_macros/",
Expand All @@ -30,45 +31,50 @@ homepage = "https://erg-lang.org/"

[features]
# when "debug" feature is turned on, that of the following crates will also be turned on.
debug = ["erg_common/debug", "erg_parser/debug", "erg_compiler/debug"] # "els/debug"
debug = ["erg_common/debug", "erg_parser/debug", "erg_compiler/debug", "erg_linter/debug"] # "els/debug"
backtrace = ["erg_common/backtrace", "els/backtrace"]
japanese = [
"erg_common/japanese",
"erg_parser/japanese",
"erg_compiler/japanese",
"els/japanese",
"erg_linter/japanese",
]
simplified_chinese = [
"erg_common/simplified_chinese",
"erg_parser/simplified_chinese",
"erg_compiler/simplified_chinese",
"els/simplified_chinese",
"erg_linter/simplified_chinese",
]
traditional_chinese = [
"erg_common/traditional_chinese",
"erg_parser/traditional_chinese",
"erg_compiler/traditional_chinese",
"els/traditional_chinese",
"erg_linter/traditional_chinese",
]
unicode = ["erg_common/unicode", "erg_parser/unicode", "erg_compiler/unicode", "els/unicode"]
pretty = ["erg_common/pretty", "erg_parser/pretty", "erg_compiler/pretty", "els/pretty"]
unicode = ["erg_common/unicode", "erg_parser/unicode", "erg_compiler/unicode", "els/unicode", "erg_linter/unicode"]
pretty = ["erg_common/pretty", "erg_parser/pretty", "erg_compiler/pretty", "els/pretty", "erg_linter/pretty"]
large_thread = [
"erg_common/large_thread",
"erg_parser/large_thread",
"erg_compiler/large_thread",
"els/large_thread",
"erg_linter/large_thread",
]
py_compat = ["erg_compiler/py_compat", "els/py_compat"]
gal = ["erg_common/gal", "erg_compiler/gal"]
els = ["erg_common/els", "erg_compiler/els", "dep:els"]
full-repl = ["erg_common/full-repl"]
full = ["els", "full-repl", "unicode", "pretty"]
experimental = ["erg_common/experimental", "erg_parser/experimental", "erg_compiler/experimental"]
experimental = ["erg_common/experimental", "erg_parser/experimental", "erg_compiler/experimental", "erg_linter/experimental"]

[workspace.dependencies]
erg_common = { version = "0.6.33", path = "./crates/erg_common" }
erg_parser = { version = "0.6.33", path = "./crates/erg_parser" }
erg_compiler = { version = "0.6.33", path = "./crates/erg_compiler" }
erg_linter = { version = "0.6.33", path = "./crates/erg_linter" }
els = { version = "0.1.45", path = "./crates/els" }
erg_proc_macros = { version = "0.6.33", path = "./crates/erg_proc_macros" }
pyo3 = { version = "0.20", features = ["extension-module"] }
Expand All @@ -77,6 +83,7 @@ pyo3 = { version = "0.20", features = ["extension-module"] }
erg_common = { workspace = true }
erg_parser = { workspace = true }
erg_compiler = { workspace = true }
erg_linter = { workspace = true }
els = { workspace = true, optional = true }

[build-dependencies]
Expand Down
1 change: 0 additions & 1 deletion crates/els/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use erg_common::dict::Dict;
use erg_common::pathutil::{project_entry_file_of, project_root_dir_of};
use erg_common::spawn::{safe_yield, spawn_new_thread};
use erg_common::style::*;
use erg_common::traits::Stream;
use erg_common::{fn_name, lsp_log};
use erg_compiler::artifact::BuildRunnable;
use erg_compiler::build_package::CheckStatus;
Expand Down
3 changes: 3 additions & 0 deletions crates/erg_common/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub enum ErgMode {
Transpile,
Execute,
LanguageServer,
Lint,
Read,
Pack,
}
Expand All @@ -48,6 +49,7 @@ impl TryFrom<&str> for ErgMode {
"trans" | "transpile" | "transpiler" => Ok(Self::Transpile),
"run" | "execute" => Ok(Self::Execute),
"server" | "language-server" => Ok(Self::LanguageServer),
"lint" | "linter" => Ok(Self::Lint),
"byteread" | "read" | "reader" | "dis" => Ok(Self::Read),
"pack" | "package" => Ok(Self::Pack),
_ => Err(()),
Expand All @@ -67,6 +69,7 @@ impl From<ErgMode> for &str {
ErgMode::Transpile => "transpile",
ErgMode::Execute => "execute",
ErgMode::LanguageServer => "language-server",
ErgMode::Lint => "lint",
ErgMode::Read => "read",
ErgMode::Pack => "pack",
}
Expand Down
8 changes: 8 additions & 0 deletions crates/erg_compiler/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,14 @@ impl CompileErrors {
pub fn take(&mut self) -> Self {
self.flush()
}

pub fn len(&self) -> usize {
self.0.len()
}

pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}

pub type SingleCompileResult<T> = Result<T, CompileError>;
Expand Down
49 changes: 49 additions & 0 deletions crates/erg_linter/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
[package]
name = "erg_linter"
description = "the Erg linter"
documentation = "http://docs.rs/erg_linter"
version.workspace = true
authors.workspace = true
license.workspace = true
edition.workspace = true
repository.workspace = true
homepage.workspace = true

[features]
debug = ["erg_common/debug", "erg_parser/debug", "erg_compiler/debug"]
backtrace = ["erg_common/backtrace"]
japanese = [
"erg_common/japanese",
"erg_parser/japanese",
"erg_compiler/japanese",
]
simplified_chinese = [
"erg_common/simplified_chinese",
"erg_parser/simplified_chinese",
"erg_compiler/simplified_chinese",
]
traditional_chinese = [
"erg_common/traditional_chinese",
"erg_parser/traditional_chinese",
"erg_compiler/traditional_chinese",
]
unicode = ["erg_common/unicode", "erg_parser/unicode", "erg_compiler/unicode"]
pretty = ["erg_common/pretty", "erg_parser/pretty", "erg_compiler/pretty"]
large_thread = [
"erg_common/large_thread",
"erg_parser/large_thread",
"erg_compiler/large_thread",
]
py_compat = ["erg_compiler/py_compat"]
gal = ["erg_common/gal", "erg_compiler/gal"]
full-repl = ["erg_common/full-repl"]
full = ["full-repl", "unicode", "pretty"]
experimental = ["erg_common/experimental", "erg_parser/experimental", "erg_compiler/experimental"]

[dependencies]
erg_common = { workspace = true }
erg_parser = { workspace = true }
erg_compiler ={ workspace = true }

[lib]
path = "lib.rs"
8 changes: 6 additions & 2 deletions crates/erg_linter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ The following codes are warned.

* Unreachable codes
* Wildcard import
* Unused variables
* Shadowing of built-in variables
* Unused objects that are not `NoneLike`
* Procedures without side-effects
* Variables that can be defined as constants
* Unnecessary `.clone`
* Mutable objects that do not change
* Hardcoded well-known constants (e.g. `3.14`)
* Defining a subroutine with too many parameters
* Defining a class with too many fields

### These are warned by the compiler

* Unused variables
* Unused objects that are not `NoneLike`
1 change: 1 addition & 0 deletions crates/erg_linter/mod.rs → crates/erg_linter/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod lint;
mod warn;

pub use lint::Linter;
Loading

0 comments on commit 11f2b48

Please sign in to comment.