Skip to content

Commit 406cad2

Browse files
committed
Add metrics for javascript
1 parent f65dc8e commit 406cad2

17 files changed

+1588
-1324
lines changed

enums/src/common.rs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::collections::hash_map::{Entry, HashMap};
2+
use std::collections::BTreeMap;
23
use tree_sitter::Language;
34

45
pub fn capitalize(s: &str) -> String {
@@ -119,25 +120,34 @@ pub fn camel_case(name: String) -> String {
119120
}
120121

121122
pub fn get_token_names(language: &Language, escape: bool) -> Vec<(String, bool, String)> {
122-
let mut names = Vec::with_capacity(language.node_kind_count());
123+
let count = language.node_kind_count();
124+
let mut names = BTreeMap::default();
123125
let mut name_count = HashMap::new();
124-
for i in 0..language.node_kind_count() {
125-
let kind = language.node_kind_for_id(i as u16);
126-
let name = sanitize_identifier(kind);
127-
let ts_name = sanitize_string(kind, escape);
128-
let name = camel_case(name);
129-
let e = match name_count.entry(name.clone()) {
130-
Entry::Occupied(mut e) => {
131-
*e.get_mut() += 1;
132-
(format!("{}{}", name, e.get()), true, ts_name)
126+
for anon in vec![false, true] {
127+
for i in 0..count {
128+
let anonymous = !language.node_kind_is_named(i as u16);
129+
if anonymous != anon {
130+
continue;
133131
}
134-
Entry::Vacant(e) => {
135-
e.insert(1);
136-
(name, false, ts_name)
137-
}
138-
};
139-
names.push(e);
132+
let kind = language.node_kind_for_id(i as u16);
133+
let name = sanitize_identifier(kind);
134+
let ts_name = sanitize_string(kind, escape);
135+
let name = camel_case(name);
136+
let e = match name_count.entry(name.clone()) {
137+
Entry::Occupied(mut e) => {
138+
*e.get_mut() += 1;
139+
(format!("{}{}", name, e.get()), true, ts_name)
140+
}
141+
Entry::Vacant(e) => {
142+
e.insert(1);
143+
(name, false, ts_name)
144+
}
145+
};
146+
names.insert(i, e);
147+
}
140148
}
149+
let mut names: Vec<_> = names.values().map(move |x| x.clone()).collect();
141150
names.push(("Error".to_string(), false, "ERROR".to_string()));
151+
142152
names
143153
}

src/checker.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -145,17 +145,18 @@ impl Checker for MozjsCode {
145145
mk_checker!(
146146
is_func,
147147
Function,
148-
FunctionDeclaration,
149148
GeneratorFunction,
149+
FunctionDeclaration,
150150
GeneratorFunctionDeclaration
151151
);
152152
mk_checker!(
153153
is_func_space,
154+
Program,
154155
Function,
155-
FunctionDeclaration,
156+
Class,
156157
GeneratorFunction,
158+
FunctionDeclaration,
157159
GeneratorFunctionDeclaration,
158-
Class,
159160
ClassDeclaration
160161
);
161162
}
@@ -167,18 +168,18 @@ impl Checker for JavascriptCode {
167168
mk_checker!(
168169
is_func,
169170
Function,
170-
FunctionDeclaration,
171171
GeneratorFunction,
172+
FunctionDeclaration,
172173
GeneratorFunctionDeclaration
173174
);
174175
mk_checker!(
175176
is_func_space,
176177
Program,
177178
Function,
178-
FunctionDeclaration,
179179
GeneratorFunction,
180-
GeneratorFunctionDeclaration,
181180
Class,
181+
FunctionDeclaration,
182+
GeneratorFunctionDeclaration,
182183
ClassDeclaration
183184
);
184185
}
@@ -190,18 +191,18 @@ impl Checker for TypescriptCode {
190191
mk_checker!(
191192
is_func,
192193
Function,
193-
FunctionDeclaration,
194194
GeneratorFunction,
195+
FunctionDeclaration,
195196
GeneratorFunctionDeclaration
196197
);
197198
mk_checker!(
198199
is_func_space,
199200
Program,
200201
Function,
201-
FunctionDeclaration,
202+
Class,
202203
GeneratorFunction,
204+
FunctionDeclaration,
203205
GeneratorFunctionDeclaration,
204-
Class,
205206
ClassDeclaration
206207
);
207208
}
@@ -213,18 +214,19 @@ impl Checker for TsxCode {
213214
mk_checker!(
214215
is_func,
215216
Function,
216-
FunctionDeclaration,
217217
GeneratorFunction,
218+
FunctionDeclaration,
218219
GeneratorFunctionDeclaration
219220
);
220221
mk_checker!(
221222
is_func_space,
222223
Program,
223224
Function,
225+
GeneratorFunction,
226+
Class,
224227
FunctionDeclaration,
225228
GeneratorFunction,
226229
GeneratorFunctionDeclaration,
227-
Class,
228230
ClassDeclaration
229231
);
230232
}

src/cyclomatic.rs

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,64 @@ impl Cyclomatic for PythonCode {
6565
}
6666
}
6767

68+
impl Cyclomatic for MozjsCode {
69+
fn compute(node: &Node, stats: &mut Stats) {
70+
use Mozjs::*;
71+
72+
match node.kind_id().into() {
73+
If | For | While | Case | Catch | TernaryExpression | AMPAMP | PIPEPIPE => {
74+
stats.cyclomatic += 1.;
75+
}
76+
_ => {}
77+
}
78+
}
79+
}
80+
81+
impl Cyclomatic for JavascriptCode {
82+
fn compute(node: &Node, stats: &mut Stats) {
83+
use Javascript::*;
84+
85+
match node.kind_id().into() {
86+
If | For | While | Case | Catch | TernaryExpression | AMPAMP | PIPEPIPE => {
87+
stats.cyclomatic += 1.;
88+
}
89+
_ => {}
90+
}
91+
}
92+
}
93+
94+
impl Cyclomatic for TypescriptCode {
95+
fn compute(node: &Node, stats: &mut Stats) {
96+
use Typescript::*;
97+
98+
match node.kind_id().into() {
99+
If | For | While | Case | Catch | TernaryExpression | AMPAMP | PIPEPIPE => {
100+
stats.cyclomatic += 1.;
101+
}
102+
_ => {}
103+
}
104+
}
105+
}
106+
107+
impl Cyclomatic for TsxCode {
108+
fn compute(node: &Node, stats: &mut Stats) {
109+
use Tsx::*;
110+
111+
match node.kind_id().into() {
112+
If | For | While | Case | Catch | TernaryExpression | AMPAMP | PIPEPIPE => {
113+
stats.cyclomatic += 1.;
114+
}
115+
_ => {}
116+
}
117+
}
118+
}
119+
68120
impl Cyclomatic for PreprocCode {}
69121
impl Cyclomatic for CcommentCode {}
70122
impl Cyclomatic for CCode {}
71123
impl Cyclomatic for CppCode {}
72124
impl Cyclomatic for CSharpCode {}
73125
impl Cyclomatic for JavaCode {}
74-
impl Cyclomatic for MozjsCode {}
75-
impl Cyclomatic for JavascriptCode {}
76-
impl Cyclomatic for TypescriptCode {}
77-
impl Cyclomatic for TsxCode {}
78126
impl Cyclomatic for GoCode {}
79127
impl Cyclomatic for CssCode {}
80128
impl Cyclomatic for HtmlCode {}

src/getter.rs

Lines changed: 71 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,6 @@ use crate::enums::NodeKind;
44
use crate::*;
55

66
pub trait Getter {
7-
fn get_func_name<'a>(_node: &Node, _code: &'a [u8]) -> Option<&'a str> {
8-
None
9-
}
10-
11-
fn get_func_space_name<'a>(_node: &Node, _code: &'a [u8]) -> Option<&'a str> {
12-
None
13-
}
14-
15-
fn get_kind(_node: &Node) -> NodeKind {
16-
NodeKind::Unknown
17-
}
18-
}
19-
20-
impl Getter for PythonCode {
217
fn get_func_name<'a>(node: &Node, code: &'a [u8]) -> Option<&'a str> {
228
Self::get_func_space_name(node, code)
239
}
@@ -28,10 +14,16 @@ impl Getter for PythonCode {
2814
let code = &code[name.start_byte()..name.end_byte()];
2915
std::str::from_utf8(code).ok()
3016
} else {
31-
None
17+
Some("<anonymous>")
3218
}
3319
}
3420

21+
fn get_kind(_node: &Node) -> NodeKind {
22+
NodeKind::Unknown
23+
}
24+
}
25+
26+
impl Getter for PythonCode {
3527
fn get_kind(node: &Node) -> NodeKind {
3628
let typ = node.kind_id();
3729
match typ.into() {
@@ -43,16 +35,76 @@ impl Getter for PythonCode {
4335
}
4436
}
4537

38+
impl Getter for MozjsCode {
39+
fn get_kind(node: &Node) -> NodeKind {
40+
use Mozjs::*;
41+
42+
let typ = node.kind_id();
43+
match typ.into() {
44+
Function | GeneratorFunction | FunctionDeclaration | GeneratorFunctionDeclaration => {
45+
NodeKind::Function
46+
}
47+
Class | ClassDeclaration => NodeKind::Class,
48+
Program => NodeKind::Unit,
49+
_ => NodeKind::Unknown,
50+
}
51+
}
52+
}
53+
54+
impl Getter for JavascriptCode {
55+
fn get_kind(node: &Node) -> NodeKind {
56+
use Javascript::*;
57+
58+
let typ = node.kind_id();
59+
match typ.into() {
60+
Function | GeneratorFunction | FunctionDeclaration | GeneratorFunctionDeclaration => {
61+
NodeKind::Function
62+
}
63+
Class | ClassDeclaration => NodeKind::Class,
64+
Program => NodeKind::Unit,
65+
_ => NodeKind::Unknown,
66+
}
67+
}
68+
}
69+
70+
impl Getter for TypescriptCode {
71+
fn get_kind(node: &Node) -> NodeKind {
72+
use Typescript::*;
73+
74+
let typ = node.kind_id();
75+
match typ.into() {
76+
Function | GeneratorFunction | FunctionDeclaration | GeneratorFunctionDeclaration => {
77+
NodeKind::Function
78+
}
79+
Class | ClassDeclaration => NodeKind::Class,
80+
Program => NodeKind::Unit,
81+
_ => NodeKind::Unknown,
82+
}
83+
}
84+
}
85+
86+
impl Getter for TsxCode {
87+
fn get_kind(node: &Node) -> NodeKind {
88+
use Tsx::*;
89+
90+
let typ = node.kind_id();
91+
match typ.into() {
92+
Function | GeneratorFunction | FunctionDeclaration | GeneratorFunctionDeclaration => {
93+
NodeKind::Function
94+
}
95+
Class | ClassDeclaration => NodeKind::Class,
96+
Program => NodeKind::Unit,
97+
_ => NodeKind::Unknown,
98+
}
99+
}
100+
}
101+
46102
impl Getter for PreprocCode {}
47103
impl Getter for CcommentCode {}
48104
impl Getter for CCode {}
49105
impl Getter for CppCode {}
50106
impl Getter for CSharpCode {}
51107
impl Getter for JavaCode {}
52-
impl Getter for MozjsCode {}
53-
impl Getter for JavascriptCode {}
54-
impl Getter for TypescriptCode {}
55-
impl Getter for TsxCode {}
56108
impl Getter for GoCode {}
57109
impl Getter for CssCode {}
58110
impl Getter for HtmlCode {}

0 commit comments

Comments
 (0)