Skip to content

Commit 21deea2

Browse files
committed
Fix Javascript Nom
1 parent c500123 commit 21deea2

File tree

1 file changed

+99
-18
lines changed

1 file changed

+99
-18
lines changed

src/metrics/nom.rs

Lines changed: 99 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,26 @@ impl Nom for MozjsCode {
9797
use Mozjs::*;
9898

9999
match node.object().kind_id().into() {
100-
Function | FunctionDeclaration | MethodDefinition => {
100+
FunctionDeclaration | MethodDefinition => {
101101
stats.functions += 1;
102102
}
103-
GeneratorFunction | GeneratorFunctionDeclaration | ArrowFunction => {
103+
GeneratorFunction | GeneratorFunctionDeclaration => {
104104
stats.closures += 1;
105105
}
106+
Function | ArrowFunction => {
107+
// Consider named functions as functions and unnamed ones as
108+
// closures.
109+
if count_specific_ancestors!(
110+
node,
111+
VariableDeclarator,
112+
StatementBlock | ReturnStatement
113+
) == 0
114+
{
115+
stats.closures += 1;
116+
} else {
117+
stats.functions += 1;
118+
}
119+
}
106120
_ => {}
107121
}
108122
}
@@ -113,12 +127,26 @@ impl Nom for JavascriptCode {
113127
use Javascript::*;
114128

115129
match node.object().kind_id().into() {
116-
Function | FunctionDeclaration | MethodDefinition => {
130+
FunctionDeclaration | MethodDefinition => {
117131
stats.functions += 1;
118132
}
119-
GeneratorFunction | GeneratorFunctionDeclaration | ArrowFunction => {
133+
GeneratorFunction | GeneratorFunctionDeclaration => {
120134
stats.closures += 1;
121135
}
136+
Function | ArrowFunction => {
137+
// Consider named functions as functions and unnamed ones as
138+
// closures.
139+
if count_specific_ancestors!(
140+
node,
141+
VariableDeclarator,
142+
StatementBlock | ReturnStatement
143+
) == 0
144+
{
145+
stats.closures += 1;
146+
} else {
147+
stats.functions += 1;
148+
}
149+
}
122150
_ => {}
123151
}
124152
}
@@ -129,12 +157,26 @@ impl Nom for TypescriptCode {
129157
use Typescript::*;
130158

131159
match node.object().kind_id().into() {
132-
Function | FunctionDeclaration | MethodDefinition => {
160+
FunctionDeclaration | MethodDefinition => {
133161
stats.functions += 1;
134162
}
135-
GeneratorFunction | GeneratorFunctionDeclaration | ArrowFunction => {
163+
GeneratorFunction | GeneratorFunctionDeclaration => {
136164
stats.closures += 1;
137165
}
166+
Function | ArrowFunction => {
167+
// Consider named functions as functions and unnamed ones as
168+
// closures.
169+
if count_specific_ancestors!(
170+
node,
171+
VariableDeclarator,
172+
StatementBlock | ReturnStatement
173+
) == 0
174+
{
175+
stats.closures += 1;
176+
} else {
177+
stats.functions += 1;
178+
}
179+
}
138180
_ => {}
139181
}
140182
}
@@ -145,12 +187,26 @@ impl Nom for TsxCode {
145187
use Tsx::*;
146188

147189
match node.object().kind_id().into() {
148-
Function | FunctionDeclaration | MethodDefinition => {
190+
FunctionDeclaration | MethodDefinition => {
149191
stats.functions += 1;
150192
}
151-
GeneratorFunction | GeneratorFunctionDeclaration | ArrowFunction => {
193+
GeneratorFunction | GeneratorFunctionDeclaration => {
152194
stats.closures += 1;
153195
}
196+
Function | ArrowFunction => {
197+
// Consider named functions as functions and unnamed ones as
198+
// closures.
199+
if count_specific_ancestors!(
200+
node,
201+
VariableDeclarator,
202+
StatementBlock | ReturnStatement
203+
) == 0
204+
{
205+
stats.closures += 1;
206+
} else {
207+
stats.functions += 1;
208+
}
209+
}
154210
_ => {}
155211
}
156212
}
@@ -279,20 +335,45 @@ mod tests {
279335
}
280336

281337
#[test]
282-
fn c_nom() {
338+
fn javascript_nom() {
283339
check_metrics!(
284-
"int foo();
285-
286-
int foo() {
287-
return 0;
340+
"function f(a, b) {
341+
function foo(a) {
342+
return a;
343+
}
344+
var bar = (function () {
345+
var counter = 0;
346+
return function () {
347+
counter += 1;
348+
return counter
349+
}
350+
})();
351+
return bar(foo(a), a);
288352
}",
289-
"foo.c",
290-
CppParser,
353+
"foo.js",
354+
JavascriptParser,
291355
nom,
292356
[
293-
(functions, 1, usize),
294-
(closures, 0, usize),
295-
(total, 1, usize)
357+
(functions, 3, usize), // f, foo, bar
358+
(closures, 1, usize), // return function ()
359+
(total, 4, usize)
360+
]
361+
);
362+
}
363+
364+
#[test]
365+
fn javascript_nom_arrow() {
366+
check_metrics!(
367+
"var materials = [\"Hydrogen\"];
368+
materials.map(material => material.length);
369+
let add = (a, b) => a + b;",
370+
"foo.js",
371+
JavascriptParser,
372+
nom,
373+
[
374+
(functions, 1, usize), // add
375+
(closures, 1, usize), // materials.map
376+
(total, 2, usize)
296377
]
297378
);
298379
}

0 commit comments

Comments
 (0)