Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify Halstead metric #223

Merged
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
152 changes: 152 additions & 0 deletions src/getter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use tree_sitter::Node;

use crate::metrics::halstead::HalsteadType;
use crate::spaces::SpaceKind;
use crate::traits::Search;

Expand All @@ -23,6 +24,10 @@ pub trait Getter {
fn get_space_kind(_node: &Node) -> SpaceKind {
SpaceKind::Unknown
}

fn get_op_type(_node: &Node) -> HalsteadType {
HalsteadType::Unknown
}
}

impl Getter for PythonCode {
Expand All @@ -35,6 +40,34 @@ impl Getter for PythonCode {
_ => SpaceKind::Unknown,
}
}

fn get_op_type(node: &Node) -> HalsteadType {
use Python::*;

let id = node.kind_id();
match id.into() {
Import | DOT | From | LPAREN | COMMA | As | STAR | GTGT | Assert | COLONEQ | Return
| Del | Raise | Pass | Break | Continue | If | Elif | Else | Async | For | In
| While | Try | Except | Finally | With | DASHGT | EQ | Global | Exec | AT | Not
| And | Or | PLUS | DASH | SLASH | PERCENT | SLASHSLASH | STARSTAR | PIPE | AMP
| CARET | LTLT | TILDE | LT | LTEQ | EQEQ | BANGEQ | GTEQ | GT | LTGT | Is | PLUSEQ
| DASHEQ | STAREQ | SLASHEQ | ATEQ | SLASHSLASHEQ | PERCENTEQ | STARSTAREQ | GTGTEQ
| LTLTEQ | AMPEQ | CARETEQ | PIPEEQ | Yield | LBRACK | LBRACE | Await | Await2
| Print => HalsteadType::Operator,
Identifier | Integer | Float | True | False | None => HalsteadType::Operand,
String => {
let mut operator = HalsteadType::Unknown;
// check if we've a documentation string or a multiline comment
if let Some(parent) = node.parent() {
if parent.kind_id() != ExpressionStatement || parent.child_count() != 1 {
operator = HalsteadType::Operand;
};
}
operator
}
_ => HalsteadType::Unknown,
}
}
}

impl Getter for MozjsCode {
Expand Down Expand Up @@ -82,6 +115,27 @@ impl Getter for MozjsCode {
Some("<anonymous>")
}
}

fn get_op_type(node: &Node) -> HalsteadType {
use Mozjs::*;

let id = node.kind_id();
match id.into() {
Export | Import | Import2 | Extends | DOT | From | LPAREN | COMMA | As | STAR
| GTGT | GTGTGT | COLON | Return | Delete | Throw | Break | Continue | If | Else
| Switch | Case | Default | Async | For | In | Of | While | Try | Catch | Finally
| With | EQ | AT | AMPAMP | PIPEPIPE | PLUS | DASH | DASHDASH | PLUSPLUS | SLASH
| PERCENT | STARSTAR | PIPE | AMP | LTLT | TILDE | LT | LTEQ | EQEQ | BANGEQ | GTEQ
| GT | PLUSEQ | BANG | BANGEQEQ | EQEQEQ | DASHEQ | STAREQ | SLASHEQ | PERCENTEQ
| STARSTAREQ | GTGTEQ | GTGTGTEQ | LTLTEQ | AMPEQ | CARET | CARETEQ | PIPEEQ
| Yield | LBRACK | LBRACE | Await | QMARK | QMARKQMARK | New | Let | Var | Const => {
HalsteadType::Operator
}
Identifier | Identifier2 | String | Number | True | False | Null | Void | This
| Super | Undefined | Set | Get | Typeof | Instanceof => HalsteadType::Operand,
_ => HalsteadType::Unknown,
}
}
}

impl Getter for JavascriptCode {
Expand Down Expand Up @@ -129,6 +183,27 @@ impl Getter for JavascriptCode {
Some("<anonymous>")
}
}

fn get_op_type(node: &Node) -> HalsteadType {
use Javascript::*;

let id = node.kind_id();
match id.into() {
Export | Import | Import2 | Extends | DOT | From | LPAREN | COMMA | As | STAR
| GTGT | GTGTGT | COLON | Return | Delete | Throw | Break | Continue | If | Else
| Switch | Case | Default | Async | For | In | Of | While | Try | Catch | Finally
| With | EQ | AT | AMPAMP | PIPEPIPE | PLUS | DASH | DASHDASH | PLUSPLUS | SLASH
| PERCENT | STARSTAR | PIPE | AMP | LTLT | TILDE | LT | LTEQ | EQEQ | BANGEQ | GTEQ
| GT | PLUSEQ | BANG | BANGEQEQ | EQEQEQ | DASHEQ | STAREQ | SLASHEQ | PERCENTEQ
| STARSTAREQ | GTGTEQ | GTGTGTEQ | LTLTEQ | AMPEQ | CARET | CARETEQ | PIPEEQ
| Yield | LBRACK | LBRACE | Await | QMARK | QMARKQMARK | New | Let | Var | Const => {
HalsteadType::Operator
}
Identifier | Identifier2 | String | Number | True | False | Null | Void | This
| Super | Undefined | Set | Get | Typeof | Instanceof => HalsteadType::Operand,
_ => HalsteadType::Unknown,
}
}
}

impl Getter for TypescriptCode {
Expand Down Expand Up @@ -176,6 +251,27 @@ impl Getter for TypescriptCode {
Some("<anonymous>")
}
}

fn get_op_type(node: &Node) -> HalsteadType {
use Typescript::*;

let id = node.kind_id();
match id.into() {
Export | Import | Import2 | Extends | DOT | From | LPAREN | COMMA | As | STAR
| GTGT | GTGTGT | COLON | Return | Delete | Throw | Break | Continue | If | Else
| Switch | Case | Default | Async | For | In | Of | While | Try | Catch | Finally
| With | EQ | AT | AMPAMP | PIPEPIPE | PLUS | DASH | DASHDASH | PLUSPLUS | SLASH
| PERCENT | STARSTAR | PIPE | AMP | LTLT | TILDE | LT | LTEQ | EQEQ | BANGEQ | GTEQ
| GT | PLUSEQ | BANG | BANGEQEQ | EQEQEQ | DASHEQ | STAREQ | SLASHEQ | PERCENTEQ
| STARSTAREQ | GTGTEQ | GTGTGTEQ | LTLTEQ | AMPEQ | CARET | CARETEQ | PIPEEQ
| Yield | LBRACK | LBRACE | Await | QMARK | QMARKQMARK | New | Let | Var | Const => {
HalsteadType::Operator
}
Identifier | NestedIdentifier | String | Number | True | False | Null | Void | This
| Super | Undefined | Set | Get | Typeof | Instanceof => HalsteadType::Operand,
_ => HalsteadType::Unknown,
}
}
}

impl Getter for TsxCode {
Expand Down Expand Up @@ -223,6 +319,27 @@ impl Getter for TsxCode {
Some("<anonymous>")
}
}

fn get_op_type(node: &Node) -> HalsteadType {
use Tsx::*;

let id = node.kind_id();
match id.into() {
Export | Import | Import2 | Extends | DOT | From | LPAREN | COMMA | As | STAR
| GTGT | GTGTGT | COLON | Return | Delete | Throw | Break | Continue | If | Else
| Switch | Case | Default | Async | For | In | Of | While | Try | Catch | Finally
| With | EQ | AT | AMPAMP | PIPEPIPE | PLUS | DASH | DASHDASH | PLUSPLUS | SLASH
| PERCENT | STARSTAR | PIPE | AMP | LTLT | TILDE | LT | LTEQ | EQEQ | BANGEQ | GTEQ
| GT | PLUSEQ | BANG | BANGEQEQ | EQEQEQ | DASHEQ | STAREQ | SLASHEQ | PERCENTEQ
| STARSTAREQ | GTGTEQ | GTGTGTEQ | LTLTEQ | AMPEQ | CARET | CARETEQ | PIPEEQ
| Yield | LBRACK | LBRACE | Await | QMARK | QMARKQMARK | New | Let | Var | Const => {
HalsteadType::Operator
}
Identifier | NestedIdentifier | String | Number | True | False | Null | Void | This
| Super | Undefined | Set | Get | Typeof | Instanceof => HalsteadType::Operand,
_ => HalsteadType::Unknown,
}
}
}

impl Getter for RustCode {
Expand Down Expand Up @@ -252,6 +369,23 @@ impl Getter for RustCode {
_ => SpaceKind::Unknown,
}
}

fn get_op_type(node: &Node) -> HalsteadType {
use Rust::*;

let id = node.kind_id();
match id.into() {
LPAREN | LBRACE | LBRACK | EQGT | PLUS | STAR | Async | Await | Continue | For | If
| Let | Loop | Match | Return | Unsafe | While | BANG | EQ | COMMA | DASHGT | QMARK
| LT | GT | AMP | MutableSpecifier | DOTDOT | DOTDOTEQ | DASH | AMPAMP | PIPEPIPE
| PIPE | CARET | EQEQ | BANGEQ | LTEQ | GTEQ | LTLT | GTGT | SLASH | PERCENT
| PLUSEQ | DASHEQ | STAREQ | SLASHEQ | PERCENTEQ | AMPEQ | PIPEEQ | CARETEQ
| LTLTEQ | GTGTEQ | Move | DOT | PrimitiveType => HalsteadType::Operator,
Identifier | StringLiteral | RawStringLiteral | IntegerLiteral | FloatLiteral
| BooleanLiteral | Zelf | CharLiteral | UNDERSCORE => HalsteadType::Operand,
_ => HalsteadType::Unknown,
}
}
}

impl Getter for CppCode {
Expand Down Expand Up @@ -312,6 +446,24 @@ impl Getter for CppCode {
_ => SpaceKind::Unknown,
}
}

fn get_op_type(node: &Node) -> HalsteadType {
use Cpp::*;

let id = node.kind_id();
match id.into() {
DOT | LPAREN | LPAREN2 | COMMA | STAR | GTGT | COLON | SEMI | Return | Break
| Continue | If | Else | Switch | Case | Default | For | While | Goto | Do | Delete
| New | Try | Catch | Throw | EQ | AMPAMP | PIPEPIPE | DASH | DASHDASH | DASHGT
| PLUS | PLUSPLUS | SLASH | PERCENT | PIPE | AMP | LTLT | TILDE | LT | LTEQ | EQEQ
| BANGEQ | GTEQ | GT | GT2 | PLUSEQ | BANG | STAREQ | SLASHEQ | PERCENTEQ | GTGTEQ
| LTLTEQ | AMPEQ | CARET | CARETEQ | PIPEEQ | LBRACK | LBRACE | QMARK | COLONCOLON
| PrimitiveType | TypeSpecifier | Sizeof => HalsteadType::Operator,
Identifier | TypeIdentifier | FieldIdentifier | RawStringLiteral | StringLiteral
| NumberLiteral | True | False | Null | Nullptr | DOTDOTDOT => HalsteadType::Operand,
_ => HalsteadType::Unknown,
}
}
}

impl Getter for PreprocCode {}
Expand Down
Loading