Skip to content

ZSH widget: ShellCheck/bashrs lint warnings (word splitting, quoting) #96

@noahgift

Description

@noahgift

Lint Analysis

Running bashrs lint on the ZSH widget reveals several issues:

Original Widget (v1)

Summary: 0 error(s), 2 warning(s), 6 info(s)
Score: B (7.5/10)

Fixed Widget (v2)

Summary: 0 error(s), 13 warning(s), 10 info(s)  
Score: C+ (6.8/10)

The v2 widget actually has MORE warnings because it's more complex.

Key Issues

1. SC2046: Unquoted command substitution (word splitting risk)

# Current (vulnerable):
suggestion=$(timeout 0.1 aprender-shell suggest "$BUFFER" 2>/dev/null | head -1 | cut -f1)

# Fixed:
suggestion="$(timeout 0.1 aprender-shell suggest "$BUFFER" 2>/dev/null | head -1 | cut -f1)"

If aprender-shell suggest returns a string with spaces or special characters, word splitting could cause issues.

2. SC2086: Unquoted variable in comparison

# Current:
if [[ -n "$POSTDISPLAY" && $CURSOR -eq ${#BUFFER} ]]; then

# Fixed:
if [[ -n "$POSTDISPLAY" && "$CURSOR" -eq "${#BUFFER}" ]]; then

3. SC2148: Missing shebang

The widget should have a shebang for clarity:

#!/usr/bin/env zsh
# aprender-shell ZSH widget v2

4. SC2125/SC2201: Array assignment issues

# Current (confusing):
region_highlight=()

# Better (explicit):
local -a region_highlight
region_highlight=()

Note on False Positives

Some warnings are false positives for ZSH:

  • SC2031 (subshell variable scope) - ZSH handles this differently
  • SC2154 (APRENDER_DISABLED unassigned) - It's an environment variable

Recommended Improvements

#!/usr/bin/env zsh
# aprender-shell ZSH widget v2
# Toggle: export APRENDER_DISABLED=1 to disable

_aprender_suggest() {
    [[ "$APRENDER_DISABLED" == "1" ]] && return
    [[ "${#BUFFER}" -lt 2 ]] && { POSTDISPLAY=''; region_highlight=(); return; }

    local suggestion
    suggestion="$(timeout 0.1 aprender-shell suggest "$BUFFER" 2>/dev/null | head -1 | cut -f1)"

    if [[ -n "$suggestion" && "$suggestion" != "$BUFFER" ]]; then
        POSTDISPLAY=" ${suggestion#$BUFFER}"
        region_highlight=("${#BUFFER} $((${#BUFFER} + ${#POSTDISPLAY})) fg=8")
    else
        POSTDISPLAY=''
        region_highlight=()
    fi
}

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions