Skip to content

Commit

Permalink
Merge pull request #1 from akinomyoga/support-bash-scripts
Browse files Browse the repository at this point in the history
Add quick fixes for Bash scripts
  • Loading branch information
ko1nksm committed Apr 4, 2020
2 parents edfbc4f + 189f7c5 commit 1c9fa28
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
17 changes: 13 additions & 4 deletions shellmetrics
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ HERE

[ "${ZSH_VERSION:-}" ] && setopt shwordsplit

MARK="@SHELLMATRICS_LINENO@"
MARK="@SHELLMETRICS_LINENO@"
SP=$(printf ' \t') && TAB=${SP#?}

SHELL_VERSION='' INDENT="$TAB"
Expand Down Expand Up @@ -151,15 +151,23 @@ pretty() {
while [ "$i" -lt "$1" ] && i=$((i + 1)); do
echo 'shellmetrics_wrapper() {'
done
sed "s/\([^\$'\"()]\)\( *()\)/\1_L$MARK\2/g" \
| awk "{i++; gsub(/$MARK/,i); print}"
mark "$MARK" | awk "{i++; gsub(/$MARK/,i); print}"
while [ "$i" -gt 0 ] && i=$((i - 1)); do
echo '}'
done
echo "typeset -fp shellmetrics_wrapper 2>/dev/null" \
"|| typeset -f shellmetrics_wrapper"
}

mark() {
sed " \
s/\([^[:space:]\$'\"();&|<>=@!*?+]\)\([[:space:]]*([[:space:]]*)\)/\1_L${1}\2/g; \
s/^[[:space:]]*function[[:space:]]\{1,\}[^[:space:]\$'\"();&|<>]\{1,\}/&_L${1}/g; \
s/[;&|(][[:space:]]*function[[:space:]]\{1,\}[^[:space:]\$'\"();&|<>]\{1,\}/&_L${1}/g; \
s/_L${1}_L${1}/_L${1}/g \
"
}

process() {
set -- "$(($1 + 1))"
eval "$(pretty "$1")" | peel "$1" | parse "$1"
Expand Down Expand Up @@ -262,7 +270,8 @@ analyze() {

case $mark in (*"f"*)
func=${line%"()"*} && func=${func%" "} && func=${func##*[$SP]}
lineno=${func##*_L} && func=${func%_L*}:$lineno
lineno=${func##*_L}
[ "$lineno" != "$func" ] && func=${func%_L*}:$lineno
push_array indent func ccn lloc
ccn=1 lloc=0
esac
Expand Down
32 changes: 32 additions & 0 deletions spec/shellmetrics_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,38 @@ Describe "proxy()"
End
End

Describe "mark()"
Parameters
"foo()" "foo_L$MARK()"
"foo ()" "foo_L$MARK ()"
"function foo" "function foo_L$MARK"
"function foo()" "function foo_L$MARK()"
"nofunction foo" "nofunction foo"
"echo function foo" "echo function foo"
"echo;function foo" "echo;function foo_L$MARK"
"a=()" "a=()"
"<()" "<()"
">()" ">()"
"@()" "@()"
"!()" "!()"
"*()" "*()"
"?()" "?()"
"+()" "+()"
" ()" " ()"
"echo; ()" "echo; ()"
"echo | ()" "echo | ()"
"echo & ()" "echo & ()"
"f()();function g" "f_L$MARK()();function g_L$MARK"
End

_mark() { echo "$1" | mark "$MARK"; }

It "marks tag for line number"
When call _mark "$1"
The output should eq "$2"
End
End

Describe "putsn()"
It "puts string"
When call putsn "test"
Expand Down

0 comments on commit 1c9fa28

Please sign in to comment.