Skip to content

Commit

Permalink
Add metrics for javascript
Browse files Browse the repository at this point in the history
  • Loading branch information
calixteman committed Nov 19, 2019
1 parent f65dc8e commit 406cad2
Show file tree
Hide file tree
Showing 17 changed files with 1,588 additions and 1,324 deletions.
42 changes: 26 additions & 16 deletions enums/src/common.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::hash_map::{Entry, HashMap};
use std::collections::BTreeMap;
use tree_sitter::Language;

pub fn capitalize(s: &str) -> String {
Expand Down Expand Up @@ -119,25 +120,34 @@ pub fn camel_case(name: String) -> String {
}

pub fn get_token_names(language: &Language, escape: bool) -> Vec<(String, bool, String)> {
let mut names = Vec::with_capacity(language.node_kind_count());
let count = language.node_kind_count();
let mut names = BTreeMap::default();
let mut name_count = HashMap::new();
for i in 0..language.node_kind_count() {
let kind = language.node_kind_for_id(i as u16);
let name = sanitize_identifier(kind);
let ts_name = sanitize_string(kind, escape);
let name = camel_case(name);
let e = match name_count.entry(name.clone()) {
Entry::Occupied(mut e) => {
*e.get_mut() += 1;
(format!("{}{}", name, e.get()), true, ts_name)
for anon in vec![false, true] {
for i in 0..count {
let anonymous = !language.node_kind_is_named(i as u16);
if anonymous != anon {
continue;
}
Entry::Vacant(e) => {
e.insert(1);
(name, false, ts_name)
}
};
names.push(e);
let kind = language.node_kind_for_id(i as u16);
let name = sanitize_identifier(kind);
let ts_name = sanitize_string(kind, escape);
let name = camel_case(name);
let e = match name_count.entry(name.clone()) {
Entry::Occupied(mut e) => {
*e.get_mut() += 1;
(format!("{}{}", name, e.get()), true, ts_name)
}
Entry::Vacant(e) => {
e.insert(1);
(name, false, ts_name)
}
};
names.insert(i, e);
}
}
let mut names: Vec<_> = names.values().map(move |x| x.clone()).collect();
names.push(("Error".to_string(), false, "ERROR".to_string()));

names
}
24 changes: 13 additions & 11 deletions src/checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,18 @@ impl Checker for MozjsCode {
mk_checker!(
is_func,
Function,
FunctionDeclaration,
GeneratorFunction,
FunctionDeclaration,
GeneratorFunctionDeclaration
);
mk_checker!(
is_func_space,
Program,
Function,
FunctionDeclaration,
Class,
GeneratorFunction,
FunctionDeclaration,
GeneratorFunctionDeclaration,
Class,
ClassDeclaration
);
}
Expand All @@ -167,18 +168,18 @@ impl Checker for JavascriptCode {
mk_checker!(
is_func,
Function,
FunctionDeclaration,
GeneratorFunction,
FunctionDeclaration,
GeneratorFunctionDeclaration
);
mk_checker!(
is_func_space,
Program,
Function,
FunctionDeclaration,
GeneratorFunction,
GeneratorFunctionDeclaration,
Class,
FunctionDeclaration,
GeneratorFunctionDeclaration,
ClassDeclaration
);
}
Expand All @@ -190,18 +191,18 @@ impl Checker for TypescriptCode {
mk_checker!(
is_func,
Function,
FunctionDeclaration,
GeneratorFunction,
FunctionDeclaration,
GeneratorFunctionDeclaration
);
mk_checker!(
is_func_space,
Program,
Function,
FunctionDeclaration,
Class,
GeneratorFunction,
FunctionDeclaration,
GeneratorFunctionDeclaration,
Class,
ClassDeclaration
);
}
Expand All @@ -213,18 +214,19 @@ impl Checker for TsxCode {
mk_checker!(
is_func,
Function,
FunctionDeclaration,
GeneratorFunction,
FunctionDeclaration,
GeneratorFunctionDeclaration
);
mk_checker!(
is_func_space,
Program,
Function,
GeneratorFunction,
Class,
FunctionDeclaration,
GeneratorFunction,
GeneratorFunctionDeclaration,
Class,
ClassDeclaration
);
}
Expand Down
56 changes: 52 additions & 4 deletions src/cyclomatic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,64 @@ impl Cyclomatic for PythonCode {
}
}

impl Cyclomatic for MozjsCode {
fn compute(node: &Node, stats: &mut Stats) {
use Mozjs::*;

match node.kind_id().into() {
If | For | While | Case | Catch | TernaryExpression | AMPAMP | PIPEPIPE => {
stats.cyclomatic += 1.;
}
_ => {}
}
}
}

impl Cyclomatic for JavascriptCode {
fn compute(node: &Node, stats: &mut Stats) {
use Javascript::*;

match node.kind_id().into() {
If | For | While | Case | Catch | TernaryExpression | AMPAMP | PIPEPIPE => {
stats.cyclomatic += 1.;
}
_ => {}
}
}
}

impl Cyclomatic for TypescriptCode {
fn compute(node: &Node, stats: &mut Stats) {
use Typescript::*;

match node.kind_id().into() {
If | For | While | Case | Catch | TernaryExpression | AMPAMP | PIPEPIPE => {
stats.cyclomatic += 1.;
}
_ => {}
}
}
}

impl Cyclomatic for TsxCode {
fn compute(node: &Node, stats: &mut Stats) {
use Tsx::*;

match node.kind_id().into() {
If | For | While | Case | Catch | TernaryExpression | AMPAMP | PIPEPIPE => {
stats.cyclomatic += 1.;
}
_ => {}
}
}
}

impl Cyclomatic for PreprocCode {}
impl Cyclomatic for CcommentCode {}
impl Cyclomatic for CCode {}
impl Cyclomatic for CppCode {}
impl Cyclomatic for CSharpCode {}
impl Cyclomatic for JavaCode {}
impl Cyclomatic for MozjsCode {}
impl Cyclomatic for JavascriptCode {}
impl Cyclomatic for TypescriptCode {}
impl Cyclomatic for TsxCode {}
impl Cyclomatic for GoCode {}
impl Cyclomatic for CssCode {}
impl Cyclomatic for HtmlCode {}
Expand Down
90 changes: 71 additions & 19 deletions src/getter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,6 @@ use crate::enums::NodeKind;
use crate::*;

pub trait Getter {
fn get_func_name<'a>(_node: &Node, _code: &'a [u8]) -> Option<&'a str> {
None
}

fn get_func_space_name<'a>(_node: &Node, _code: &'a [u8]) -> Option<&'a str> {
None
}

fn get_kind(_node: &Node) -> NodeKind {
NodeKind::Unknown
}
}

impl Getter for PythonCode {
fn get_func_name<'a>(node: &Node, code: &'a [u8]) -> Option<&'a str> {
Self::get_func_space_name(node, code)
}
Expand All @@ -28,10 +14,16 @@ impl Getter for PythonCode {
let code = &code[name.start_byte()..name.end_byte()];
std::str::from_utf8(code).ok()
} else {
None
Some("<anonymous>")
}
}

fn get_kind(_node: &Node) -> NodeKind {
NodeKind::Unknown
}
}

impl Getter for PythonCode {
fn get_kind(node: &Node) -> NodeKind {
let typ = node.kind_id();
match typ.into() {
Expand All @@ -43,16 +35,76 @@ impl Getter for PythonCode {
}
}

impl Getter for MozjsCode {
fn get_kind(node: &Node) -> NodeKind {
use Mozjs::*;

let typ = node.kind_id();
match typ.into() {
Function | GeneratorFunction | FunctionDeclaration | GeneratorFunctionDeclaration => {
NodeKind::Function
}
Class | ClassDeclaration => NodeKind::Class,
Program => NodeKind::Unit,
_ => NodeKind::Unknown,
}
}
}

impl Getter for JavascriptCode {
fn get_kind(node: &Node) -> NodeKind {
use Javascript::*;

let typ = node.kind_id();
match typ.into() {
Function | GeneratorFunction | FunctionDeclaration | GeneratorFunctionDeclaration => {
NodeKind::Function
}
Class | ClassDeclaration => NodeKind::Class,
Program => NodeKind::Unit,
_ => NodeKind::Unknown,
}
}
}

impl Getter for TypescriptCode {
fn get_kind(node: &Node) -> NodeKind {
use Typescript::*;

let typ = node.kind_id();
match typ.into() {
Function | GeneratorFunction | FunctionDeclaration | GeneratorFunctionDeclaration => {
NodeKind::Function
}
Class | ClassDeclaration => NodeKind::Class,
Program => NodeKind::Unit,
_ => NodeKind::Unknown,
}
}
}

impl Getter for TsxCode {
fn get_kind(node: &Node) -> NodeKind {
use Tsx::*;

let typ = node.kind_id();
match typ.into() {
Function | GeneratorFunction | FunctionDeclaration | GeneratorFunctionDeclaration => {
NodeKind::Function
}
Class | ClassDeclaration => NodeKind::Class,
Program => NodeKind::Unit,
_ => NodeKind::Unknown,
}
}
}

impl Getter for PreprocCode {}
impl Getter for CcommentCode {}
impl Getter for CCode {}
impl Getter for CppCode {}
impl Getter for CSharpCode {}
impl Getter for JavaCode {}
impl Getter for MozjsCode {}
impl Getter for JavascriptCode {}
impl Getter for TypescriptCode {}
impl Getter for TsxCode {}
impl Getter for GoCode {}
impl Getter for CssCode {}
impl Getter for HtmlCode {}
Expand Down
Loading

0 comments on commit 406cad2

Please sign in to comment.