terryyin / lizard

A simple code complexity analyser without caring about the C/C++ header files or Java imports, supports most of the popular languages.
Other
1.85k stars 250 forks source link

Count nested control structures gives weird output for python 3 #236

Closed sak96 closed 2 years ago

sak96 commented 6 years ago

For python the nested control structure stack up until a "}" structure is found. had trouble figuring out the issue. This may be because the structure for end of function is marked by "}" in case of C, C++, JAVA, whereas indentation in case of python

for the following code (filename give as comment)

# test.py
def foo():
    if bar:
        return

def foo1():
    if bar:
        return

def foo2():
    if bar:
        return
# test2.py
def foo():
    if bar:
        return

def foo1():
    a = {}
    if bar:
        return

def foo2():
    if bar:
        return

the output was:

$lizard -ENS test.py
======================================================
  NLOC    CCN   token  PARAM  length   NS   location
------------------------------------------------------
       3      2      8      0       3     1 foo@1-3@test.py
       3      2      8      0       3     2 foo1@6-8@test.py
       3      2      8      0       3     3 foo2@10-12@test.py
1 file analyzed.
==============================================================
NLOC    Avg.NLOC  AvgCCN  Avg.token  function_cnt    file
--------------------------------------------------------------
      9       3.0     2.0        8.0         3     test.py

==========================================================================================================================
No thresholds exceeded (cyclomatic_complexity > 15 or max_nested_structures > 3 or length > 1000 or parameter_count > 100)
==========================================================================================
Total nloc   Avg.NLOC  AvgCCN  Avg.token   Fun Cnt  Warning cnt   Fun Rt   nloc Rt
------------------------------------------------------------------------------------------
         9       3.0     2.0        8.0        3            0      0.00    0.00
$
$
$
$lizard -ENS test2.py
======================================================
  NLOC    CCN   token  PARAM  length   NS   location
------------------------------------------------------
       3      2      8      0       3     1 foo@1-3@test2.py
       4      2     12      0       4     1 foo1@6-9@test2.py
       3      2      8      0       3     2 foo2@11-13@test2.py
1 file analyzed.
==============================================================
NLOC    Avg.NLOC  AvgCCN  Avg.token  function_cnt    file
--------------------------------------------------------------
     10       3.3     2.0        9.3         3     test2.py

==========================================================================================================================
No thresholds exceeded (parameter_count > 100 or max_nested_structures > 3 or length > 1000 or cyclomatic_complexity > 15)
==========================================================================================
Total nloc   Avg.NLOC  AvgCCN  Avg.token   Fun Cnt  Warning cnt   Fun Rt   nloc Rt
------------------------------------------------------------------------------------------
        10       3.3     2.0        9.3        3            0      0.00    0.00
$
terryyin commented 6 years ago

This is a bug. Will find some time to fix it.

sak96 commented 2 years ago

finally got some time.

did some digging:

This code in nesting run when token is '}' https://github.com/terryyin/lizard/blob/a2b8cdb5e83a6514c183bc30e59570fadcc1cadd/lizard_ext/lizardns.py#L90

which flows to https://github.com/terryyin/lizard/blob/a2b8cdb5e83a6514c183bc30e59570fadcc1cadd/lizard_ext/lizardns.py#L104

which sets the nesting level to 0 as the token is not if else.

sak96 commented 2 years ago

i don't see that this is easily fixable issue. closing as this project is not active. making this issue stale.