Skip to content

Commit fa633ca

Browse files
committed
Add support for tracking function scope
1 parent 19b89ae commit fa633ca

29 files changed

Lines changed: 2352 additions & 26 deletions

lib/node_modules/@stdlib/_tools/js/program-summary/lib/analyze.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ var type2key = require( './type2key.js' );
2626
* @param {Node} ast - AST node
2727
* @param {string} src - source code
2828
* @param {StringArray} lines - source code lines
29-
* @param {NonNegativeInteger} level - recursion level
29+
* @param {NonNegativeInteger} scope - scope level
3030
* @param {Options} opts - parse options
3131
* @returns {Object} results
3232
*/
33-
function analyze( results, ast, src, lines, level, opts ) {
33+
function analyze( results, ast, src, lines, scope, opts ) {
3434
var nodes;
3535
var node;
3636
var type;
@@ -47,7 +47,7 @@ function analyze( results, ast, src, lines, level, opts ) {
4747

4848
// Flatten the AST:
4949
node = ( ast.type !== 'Program' && ast.body ) ? ast.body : ast;
50-
nodes = flatten( node, level );
50+
nodes = flatten( node, scope );
5151

5252
// Calculate the number of source lines of codes (SLOC):
5353
results.sloc += sloc( nodes );
@@ -58,16 +58,17 @@ function analyze( results, ast, src, lines, level, opts ) {
5858
// Calculate the number of empty lines:
5959
results.empty += emptyLines( lines );
6060

61+
// Set the scope level:
62+
results.scope = scope;
63+
6164
// Tabulate AST node occurrences:
6265
results = tabulate( results, nodes );
6366

64-
// Increment the recursion level:
65-
level += 1;
66-
6767
// Recursively analyze AST nodes:
6868
for ( i = 0; i < nodes.length; i++ ) {
6969
node = nodes[ i ];
7070
type = node.type;
71+
scope = node.__scope__; // eslint-disable-line no-underscore-dangle
7172
if ( contains( recurseNodes, type ) ) {
7273
// Initialize a new results object:
7374
res = resultsObject();
@@ -85,18 +86,22 @@ function analyze( results, ast, src, lines, level, opts ) {
8586
// Isolate the AST body (which should consist of only 1 top-level AST node):
8687
ast = ast.body[ 0 ];
8788

89+
// Increment the scope if we are analyzing a function AST:
90+
if ( type === 'FunctionDeclaration' ) {
91+
scope += 1;
92+
}
8893
// Analyze the AST:
89-
key = type2key( type );
90-
res = analyze( res, ast, str, lines, level, opts );
94+
res = analyze( res, ast, str, lines, scope, opts );
9195

9296
// Include results specific to particular AST nodes:
9397
if ( type === 'SwitchStatement' ) {
9498
res.case = node.cases.length;
9599
} else if ( type === 'FunctionDeclaration' ) {
96100
res.params = node.params.length;
97-
res.level = node.__scope_level__; // eslint-disable-line no-underscore-dangle
101+
res.scope -= 1; // while contents are in a nested scope, declaration is in parent scope
98102
}
99103
// Cache the sub-tree results:
104+
key = type2key( type );
100105
results[ key ].data.push( res );
101106
}
102107
}

lib/node_modules/@stdlib/_tools/js/program-summary/lib/flatten.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ var types = require( './node_types.js' );
1414
*
1515
* @private
1616
* @param {Node} ast - AST node
17-
* @param {NonNegativeInteger} level - recursion level
17+
* @param {NonNegativeInteger} scope - scope level
1818
* @returns {Array} flattened AST
1919
*/
20-
function flatten( ast, level ) {
20+
function flatten( ast, scope ) {
2121
var out = [];
2222
walk( ast, visit );
2323
return out;
@@ -31,17 +31,16 @@ function flatten( ast, level ) {
3131
* @param {Array<Node>} ancestors - ancestor nodes
3232
*/
3333
function visit( node, state, ancestors ) {
34+
var level;
3435
var i;
3536
if ( node !== ast && contains( types, node.type ) ) {
36-
// For functions, track the function scope (i.e., level of function nesting):
37-
if ( node.type === 'FunctionDeclaration' ) {
38-
node.__scope_level__ = level; // eslint-disable-line no-underscore-dangle
39-
for ( i = 0; i < ancestors.length-1; i++ ) {
40-
if ( ancestors[ i ].type === 'FunctionDeclaration' ) {
41-
node.__scope_level__ += 1; // eslint-disable-line no-underscore-dangle
42-
}
37+
level = scope;
38+
for ( i = 0; i < ancestors.length-1; i++ ) {
39+
if ( ancestors[ i ].type === 'FunctionDeclaration' ) {
40+
level += 1;
4341
}
4442
}
43+
node.__scope__ = level; // eslint-disable-line no-underscore-dangle
4544
out.push( node );
4645
}
4746
} // end FUNCTION visit()

lib/node_modules/@stdlib/_tools/js/program-summary/lib/results.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ function results() {
1919
'empty': 0,
2020
'sloc': 0,
2121
'lloc': 0,
22+
'scope': 0,
2223
'comments': {
2324
'count': 0,
2425
'length': 0,

lib/node_modules/@stdlib/_tools/js/program-summary/test/fixtures/1.json

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"empty": 14,
55
"sloc": 58,
66
"lloc": 43,
7+
"scope": 0,
78
"comments": {
89
"count": 65,
910
"length": 821,
@@ -39,6 +40,7 @@
3940
"empty": 0,
4041
"sloc": 1,
4142
"lloc": 1,
43+
"scope": 0,
4244
"comments": {
4345
"count": 2,
4446
"length": 20,
@@ -106,6 +108,7 @@
106108
"empty": 0,
107109
"sloc": 1,
108110
"lloc": 1,
111+
"scope": 0,
109112
"comments": {
110113
"count": 2,
111114
"length": 20,
@@ -173,6 +176,7 @@
173176
"empty": 0,
174177
"sloc": 2,
175178
"lloc": 2,
179+
"scope": 0,
176180
"comments": {
177181
"count": 4,
178182
"length": 35,
@@ -240,6 +244,7 @@
240244
"empty": 0,
241245
"sloc": 6,
242246
"lloc": 4,
247+
"scope": 0,
243248
"comments": {
244249
"count": 6,
245250
"length": 55,
@@ -275,6 +280,7 @@
275280
"empty": 0,
276281
"sloc": 2,
277282
"lloc": 2,
283+
"scope": 0,
278284
"comments": {
279285
"count": 4,
280286
"length": 35,
@@ -375,6 +381,7 @@
375381
"empty": 0,
376382
"sloc": 8,
377383
"lloc": 6,
384+
"scope": 0,
378385
"comments": {
379386
"count": 8,
380387
"length": 75,
@@ -410,6 +417,7 @@
410417
"empty": 0,
411418
"sloc": 2,
412419
"lloc": 2,
420+
"scope": 0,
413421
"comments": {
414422
"count": 4,
415423
"length": 35,
@@ -477,6 +485,7 @@
477485
"empty": 0,
478486
"sloc": 6,
479487
"lloc": 4,
488+
"scope": 0,
480489
"comments": {
481490
"count": 6,
482491
"length": 55,
@@ -512,6 +521,7 @@
512521
"empty": 0,
513522
"sloc": 2,
514523
"lloc": 2,
524+
"scope": 0,
515525
"comments": {
516526
"count": 4,
517527
"length": 35,
@@ -650,6 +660,7 @@
650660
"empty": 0,
651661
"sloc": 6,
652662
"lloc": 6,
663+
"scope": 0,
653664
"comments": {
654665
"count": 10,
655666
"length": 85,
@@ -724,6 +735,7 @@
724735
"empty": 0,
725736
"sloc": 2,
726737
"lloc": 2,
738+
"scope": 0,
727739
"comments": {
728740
"count": 4,
729741
"length": 35,
@@ -796,6 +808,7 @@
796808
"empty": 0,
797809
"sloc": 2,
798810
"lloc": 2,
811+
"scope": 0,
799812
"comments": {
800813
"count": 3,
801814
"length": 30,
@@ -868,6 +881,7 @@
868881
"empty": 0,
869882
"sloc": 2,
870883
"lloc": 2,
884+
"scope": 0,
871885
"comments": {
872886
"count": 3,
873887
"length": 30,
@@ -940,6 +954,7 @@
940954
"empty": 0,
941955
"sloc": 2,
942956
"lloc": 2,
957+
"scope": 0,
943958
"comments": {
944959
"count": 3,
945960
"length": 27,
@@ -1012,6 +1027,7 @@
10121027
"empty": 0,
10131028
"sloc": 3,
10141029
"lloc": 2,
1030+
"scope": 0,
10151031
"comments": {
10161032
"count": 5,
10171033
"length": 88,
@@ -1047,6 +1063,7 @@
10471063
"empty": 0,
10481064
"sloc": 1,
10491065
"lloc": 1,
1066+
"scope": 0,
10501067
"comments": {
10511068
"count": 2,
10521069
"length": 20,
@@ -1153,6 +1170,7 @@
11531170
"empty": 0,
11541171
"sloc": 1,
11551172
"lloc": 1,
1173+
"scope": 0,
11561174
"comments": {
11571175
"count": 2,
11581176
"length": 16,
@@ -1213,8 +1231,7 @@
12131231
"count": 0,
12141232
"data": []
12151233
},
1216-
"params": 2,
1217-
"level": 0
1234+
"params": 2
12181235
}
12191236
]
12201237
}

lib/node_modules/@stdlib/_tools/js/program-summary/test/fixtures/2.json

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"empty": 9,
55
"sloc": 31,
66
"lloc": 24,
7+
"scope": 0,
78
"comments": {
89
"count": 36,
910
"length": 684,
@@ -39,6 +40,7 @@
3940
"empty": 0,
4041
"sloc": 1,
4142
"lloc": 1,
43+
"scope": 1,
4244
"comments": {
4345
"count": 2,
4446
"length": 16,
@@ -106,6 +108,7 @@
106108
"empty": 0,
107109
"sloc": 1,
108110
"lloc": 1,
111+
"scope": 1,
109112
"comments": {
110113
"count": 2,
111114
"length": 20,
@@ -173,6 +176,7 @@
173176
"empty": 0,
174177
"sloc": 1,
175178
"lloc": 1,
179+
"scope": 1,
176180
"comments": {
177181
"count": 2,
178182
"length": 20,
@@ -240,6 +244,7 @@
240244
"empty": 0,
241245
"sloc": 1,
242246
"lloc": 1,
247+
"scope": 1,
243248
"comments": {
244249
"count": 2,
245250
"length": 20,
@@ -307,6 +312,7 @@
307312
"empty": 0,
308313
"sloc": 3,
309314
"lloc": 3,
315+
"scope": 1,
310316
"comments": {
311317
"count": 4,
312318
"length": 40,
@@ -374,6 +380,7 @@
374380
"empty": 0,
375381
"sloc": 1,
376382
"lloc": 1,
383+
"scope": 1,
377384
"comments": {
378385
"count": 2,
379386
"length": 20,
@@ -472,6 +479,7 @@
472479
"empty": 0,
473480
"sloc": 23,
474481
"lloc": 17,
482+
"scope": 0,
475483
"comments": {
476484
"count": 24,
477485
"length": 202,
@@ -507,6 +515,7 @@
507515
"empty": 0,
508516
"sloc": 1,
509517
"lloc": 1,
518+
"scope": 1,
510519
"comments": {
511520
"count": 2,
512521
"length": 16,
@@ -574,6 +583,7 @@
574583
"empty": 0,
575584
"sloc": 1,
576585
"lloc": 1,
586+
"scope": 1,
577587
"comments": {
578588
"count": 2,
579589
"length": 20,
@@ -641,6 +651,7 @@
641651
"empty": 0,
642652
"sloc": 1,
643653
"lloc": 1,
654+
"scope": 1,
644655
"comments": {
645656
"count": 2,
646657
"length": 20,
@@ -708,6 +719,7 @@
708719
"empty": 0,
709720
"sloc": 1,
710721
"lloc": 1,
722+
"scope": 1,
711723
"comments": {
712724
"count": 2,
713725
"length": 20,
@@ -775,6 +787,7 @@
775787
"empty": 0,
776788
"sloc": 3,
777789
"lloc": 3,
790+
"scope": 1,
778791
"comments": {
779792
"count": 4,
780793
"length": 40,
@@ -842,6 +855,7 @@
842855
"empty": 0,
843856
"sloc": 1,
844857
"lloc": 1,
858+
"scope": 1,
845859
"comments": {
846860
"count": 2,
847861
"length": 20,
@@ -935,8 +949,7 @@
935949
"count": 0,
936950
"data": []
937951
},
938-
"params": 2,
939-
"level": 0
952+
"params": 2
940953
}
941954
]
942955
}

lib/node_modules/@stdlib/_tools/js/program-summary/test/fixtures/block_comment.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"empty": 1,
55
"sloc": 0,
66
"lloc": 0,
7+
"scope": 0,
78
"comments": {
89
"count": 1,
910
"length": 32,

0 commit comments

Comments
 (0)