Skip to content

Commit

Permalink
Merge pull request #80 from worldbank/v1.02
Browse files Browse the repository at this point in the history
v1.02
  • Loading branch information
luisesanmartin authored Apr 11, 2023
2 parents aa65243 + 75b33ba commit a58ebd4
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 30 deletions.
2 changes: 1 addition & 1 deletion admin/checklist-submitting-SSC.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
- [ ] 3.2 **Update version and date** - In the _version_ branch, update the version number and date in all ado-files and all dates in all help files. See section below for details.
- [ ] 3.3 **Update version globals** - Update the _version_ado_ local in the file _lint.ado_ and the _VERSION_ global in _stata_linter_detect.py_ and _stata_linter_correct.py_.
- [ ] 3.4 **Update version in .pkg and .toc** - This has nothing to do with SSC but should be kept up to date to. This is for when people install directly through GitHub using `net install`. If any new command has been added, remember to add the files for that command to the `.pkg` file.
- [ ] 3.5 **Create a .zip file** - Create a .zip file with all ado-files and help files only. These files are not allowed to be in a sub-folder in this .zip file. No other files should be in this folder. Make a copy of this file in the archive folder of this package.
- [ ] 3.5 **Create a .zip file** - Create a .zip file with the files listed below (ado-files, Python scripts, and help files). If a version update ever includes a new ado-file or Python script necessary to run the linter, include that new file in the .zip too. These files are not allowed to be in a sub-folder in this .zip file. No other files should be in this folder. Make a copy of this file in the archive folder of this package.
- [ ] 4. **Email Prof. Baum** - Email the .zip file created in step 3.5 to **[email protected]**.
- [ ] 4.1 - If any commands are added or deleted, make note of that in the email.
- [ ] 4.2 - If any of the meta info (title, description, keywords, version or author/contact) has changed then include those updates in your email.
Expand Down
2 changes: 1 addition & 1 deletion admin/ssc-meta-info.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ The package contains a command that detects bad Stata coding practices in a do-f
The command can also correct some of the issues flagged in a new do-file.
The purpose of the command is to help users improve code clarity, readability, and organization in Stata do-files.
This linter is based on the best practices outlined in The DIME Analytics Coding Guide published as an appendix to the book Development Research in Practice.
See here https://worldbank.github.io/dime-data-handbook/coding.html. For more info about this linter, see https://github.com/worldbank/stata_linter.
See here https://worldbank.github.io/dime-data-handbook/coding.html. For more info about this linter, see https://github.com/worldbank/stata-linter.

### AUTHOR:
"DIME Analytics, DIME, The World Bank Group", [email protected]
Expand Down
4 changes: 2 additions & 2 deletions src/lint.ado
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*! version 1.1 07dec2022 DIME Analytics [email protected]
*! version 1.02 06apr2023 DIME Analytics [email protected]

capture program drop lint
program lint
Expand Down Expand Up @@ -536,7 +536,7 @@ capture program drop _checkversions

* IMPORTANT: Every time we have a package update, update the version number here
* Otherwise we'd be introducing a major bug!
local version_ado 1.1
local version_ado 1.02

* Check versions of .py files
python: from sfi import Macro
Expand Down
2 changes: 1 addition & 1 deletion src/lint.sthlp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{smcl}
{* 07 Dec 2022}{...}
{* 06 Apr 2023}{...}
{hline}
help for {hi:lint}
{hline}
Expand Down
4 changes: 2 additions & 2 deletions src/stata_linter_correct.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# version 1.1 07dec2022 DIME Analytics [email protected]
# version 1.02 06apr2023 DIME Analytics [email protected]
# Import packages ============
import os
import re
Expand All @@ -8,7 +8,7 @@
# Version Global
## VERY IMPORTANT: Update the version number here every time there's an update
## in the package. Otherwise this will cause a major bug
VERSION = "1.1"
VERSION = "1.02"

# Function to update comment delimiter =============
# (detection works only when comment delimiter == 0)
Expand Down
54 changes: 34 additions & 20 deletions src/stata_linter_detect.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# version 1.1 07dec2022 DIME Analytics [email protected]
# version 1.02 06apr2023 DIME Analytics [email protected]
# Import packages ====================
import os
import re
Expand All @@ -9,7 +9,7 @@
# Version Global
## VERY IMPORTANT: Update the version number here every time there's an update
## in the package. Otherwise this will cause a major bug
VERSION = "1.1"
VERSION = "1.02"

# simple run entry point
def run():
Expand Down Expand Up @@ -247,11 +247,12 @@ def no_space_before_symbol(line):

line = line.split('///')[0]
groups = line.split('"')
pattern = r"(?:[a-z]|[A-Z]|[0-9]|_|\)|')(?:<|>|=|\+|-|\*|\^)"

for i, group in enumerate(groups):

if i % 2 == 0:
if re.search(r"(?:[a-z]|[A-Z]|[0-9]|_|\)|')(?:<|>|=|\+|-|\*|\^)", group):
if re.search(pattern, group):
return True

return False
Expand All @@ -260,11 +261,12 @@ def no_space_after_symbol(line):

line = line.split('///')[0]
groups = line.split('"')
pattern = r"(?:(?:<|>|=|\+|-|\*|\^)(?:[a-z]|[A-Z]|_|\(|`|\.|$))|(?:(?:<|>|=|\+|\*|\^)(?:[0-9]))"

for i, group in enumerate(groups):

if i % 2 == 0:
if re.search(r"(?:<|>|=|\+|-|\*|\^)(?:[a-z]|[A-Z]|[0-9]|_|\(|`|\.|$)", group):
if re.search(pattern, group):
return True

return False
Expand Down Expand Up @@ -413,30 +415,42 @@ def too_long_line(
return([style_dictionary, excel_output_list])

# "if" condition should be explicit
def detect_implicit_if(line):

search_if = re.search(r"(?:^|\s)(?:if|else if)\s", line.lstrip())

if search_if != None:

line = line[search_if.span()[0]:]
if (
(re.search(r"missing\(", line) == None) &
(re.search(r"inrange\(", line) == None) &
(re.search(r"inlist\(", line) == None) &
(re.search(r"=|<|>", line) == None)
):
return True

return False

def explicit_if(
line_index, line, input_lines, indent,
suppress, style_dictionary, excel_output_list,
tab_space
):

# warn if "if" statement is used but the condition is not explicit
search_if = re.search(r"^(if|else if) ", line.lstrip())
if (search_if != None):
if (
(re.search(r"missing\(", line[search_if.span()[0]:]) == None) &
(re.search(r"((=|<|>))", line[search_if.span()[0]:]) == None)
):
print_output = (
'''Always explicitly specify the condition in the if statement. ''' +
'''(For example, declare "if var == 1" instead of "if var".) '''
if detect_implicit_if(line):
print_output = (
'''Always explicitly specify the condition in the if statement. ''' +
'''(For example, declare "if var == 1" instead of "if var".) '''
)
if suppress != "1":
print(
'''(line {:d}): '''.format(line_index + 1) +
print_output
)
if suppress != "1":
print(
'''(line {:d}): '''.format(line_index + 1) +
print_output
)
style_dictionary["explicit_if"] += 1
excel_output_list.append([line_index + 1, "style", print_output])
style_dictionary["explicit_if"] += 1
excel_output_list.append([line_index + 1, "style", print_output])

return([style_dictionary, excel_output_list])

Expand Down
2 changes: 1 addition & 1 deletion src/stata_linter_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# version 1.1 07dec2022 DIME Analytics [email protected]
# version 1.02 06apr2023 DIME Analytics [email protected]
# Import packages ====================
import re
import pandas as pd
Expand Down
2 changes: 1 addition & 1 deletion stata.toc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
v 1.1
v 1.02
d DIME Analytics, World Bank Group, Development Economics Research
p stata_linter
2 changes: 1 addition & 1 deletion stata_linter.pkg
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
v 1.1
v 1.02
d DIME Analytics, World Bank Group, Development Economics Research
p stata_linter
f /src/stata_linter_detect.py
Expand Down

0 comments on commit a58ebd4

Please sign in to comment.