Skip to content

Commit

Permalink
fix: enforce & use consistent function definitions (#1464)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyperupcall committed Jan 29, 2023
1 parent aa0abfa commit e0fd7a7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/utils.bash
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ with_shim_executable() {

# This function does get invoked, but shellcheck sees it as unused code
# shellcheck disable=SC2317
function run_within_env() {
run_within_env() {
local path
path=$(remove_path_from_path "$PATH" "$(asdf_data_dir)/shims")

Expand Down
34 changes: 33 additions & 1 deletion scripts/checkstyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
import argparse
from pathlib import Path
from typing import List, Dict, Any # compat
from typing import Callable, List, Dict, Any # compat

# This file checks Bash and Shell scripts for violations not found with
# shellcheck or existing methods. You can use it in several ways:
Expand Down Expand Up @@ -33,6 +33,7 @@ class c:
RESET = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
LINK: Callable[[str, str], str] = lambda href, text: f'\033]8;;{href}\a{text}\033]8;;\a'

def utilGetStrs(line, m):
return (
Expand Down Expand Up @@ -62,6 +63,23 @@ def noTestDoubleEqualsFixer(line: str, m) -> str:

return f'{prestr}={poststr}'

# Before: function fn() { ...
# After: fn() { ...
# ---
# Before: function fn { ...
# After fn() { ...
def noFunctionKeywordFixer(line: str, m) -> str:
prestr, midstr, poststr = utilGetStrs(line, m)

midstr = midstr.strip()
midstr = midstr[len('function'):]
midstr = midstr.strip()

parenIdx = midstr.find('(')
if parenIdx != -1: midstr = midstr[:parenIdx]

return f'{prestr}{midstr}() {poststr}'

def lintfile(filepath: Path, rules: List[Rule], options: Dict[str, Any]):
content_arr = filepath.read_text().split('\n')

Expand Down Expand Up @@ -140,6 +158,20 @@ def main():
],
'found': 0
},
{
'name': 'no-function-keyword',
'regex': '^[ \\t]*(?P<match>function .*?(?:\\([ \\t]*\\))?[ \\t]*){',
'reason': 'Only allow functions declared like `fn_name() {{ :; }}` for consistency (see ' + c.LINK('https://www.shellcheck.net/wiki/SC2113', 'ShellCheck SC2113') + ')',
'fixerFn': noFunctionKeywordFixer,
'testPositiveMatches': [
'function fn() { :; }',
'function fn { :; }',
],
'testNegativeMatches': [
'fn() { :; }',
],
'found': 0
},
]

parser = argparse.ArgumentParser()
Expand Down
2 changes: 1 addition & 1 deletion test/utils.bats
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ EOF

message="callback invoked"

function callback() {
callback() {
echo "$message"
}

Expand Down
4 changes: 2 additions & 2 deletions test/where_command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

load test_helpers

function setup() {
setup() {
setup_asdf_dir
install_dummy_plugin
install_dummy_version 1.0
install_dummy_version 2.1
install_dummy_version ref-master
}

function teardown() {
teardown() {
clean_asdf_dir
}

Expand Down

0 comments on commit e0fd7a7

Please sign in to comment.