diff --git a/src/ShellCheck/CFG.hs b/src/ShellCheck/CFG.hs index 0cd6326ba..5476ad533 100644 --- a/src/ShellCheck/CFG.hs +++ b/src/ShellCheck/CFG.hs @@ -51,6 +51,7 @@ import Control.Monad.Identity import Data.Array.Unboxed import Data.Array.ST import Data.List hiding (map) +import qualified Data.List.NonEmpty as NE import Data.Maybe import qualified Data.Map as M import qualified Data.Set as S @@ -857,8 +858,8 @@ build t = do status <- newNodeRange (CFSetExitCode id) linkRange assignments status - T_SimpleCommand id vars list@(cmd:_) -> - handleCommand t vars list $ getUnquotedLiteral cmd + T_SimpleCommand id vars (cmd:args) -> + handleCommand t vars (cmd NE.:| args) $ getUnquotedLiteral cmd T_SingleQuoted _ _ -> none @@ -925,8 +926,8 @@ handleCommand cmd vars args literalCmd = do -- TODO: Handle assignments in declaring commands case literalCmd of - Just "exit" -> regularExpansion vars args $ handleExit - Just "return" -> regularExpansion vars args $ handleReturn + Just "exit" -> regularExpansion vars (NE.toList args) $ handleExit + Just "return" -> regularExpansion vars (NE.toList args) $ handleReturn Just "unset" -> regularExpansionWithStatus vars args $ handleUnset args Just "declare" -> handleDeclare args @@ -949,14 +950,14 @@ handleCommand cmd vars args literalCmd = do -- This will mostly behave like 'command' but ok Just "builtin" -> case args of - [_] -> regular - (_:newargs@(newcmd:_)) -> - handleCommand newcmd vars newargs $ getLiteralString newcmd + _ NE.:| [] -> regular + (_ NE.:| newcmd:newargs) -> + handleCommand newcmd vars (newcmd NE.:| newargs) $ getLiteralString newcmd Just "command" -> case args of - [_] -> regular - (_:newargs@(newcmd:_)) -> - handleOthers (getId newcmd) vars newargs $ getLiteralString newcmd + _ NE.:| [] -> regular + (_ NE.:| newcmd:newargs) -> + handleOthers (getId newcmd) vars (newcmd NE.:| newargs) $ getLiteralString newcmd _ -> regular where @@ -984,7 +985,7 @@ handleCommand cmd vars args literalCmd = do unreachable <- newNode CFUnreachable return $ Range ret unreachable - handleUnset (cmd:args) = do + handleUnset (cmd NE.:| args) = do case () of _ | "n" `elem` flagNames -> unsetWith CFUndefineNameref _ | "v" `elem` flagNames -> unsetWith CFUndefineVariable @@ -1003,7 +1004,7 @@ handleCommand cmd vars args literalCmd = do variableAssignRegex = mkRegex "^([_a-zA-Z][_a-zA-Z0-9]*)=" - handleDeclare (cmd:args) = do + handleDeclare (cmd NE.:| args) = do isFunc <- asks cfIsFunction -- This is a bit of a kludge: we don't have great support for things like -- 'declare -i x=$x' so do one round with declare x=$x, followed by declare -i x @@ -1092,7 +1093,7 @@ handleCommand cmd vars args literalCmd = do in concatMap (drop 1) plusses - handlePrintf (cmd:args) = + handlePrintf (cmd NE.:| args) = newNodeRange $ CFApplyEffects $ maybeToList findVar where findVar = do @@ -1101,7 +1102,7 @@ handleCommand cmd vars args literalCmd = do name <- getLiteralString arg return $ IdTagged (getId arg) $ CFWriteVariable name CFValueString - handleWait (cmd:args) = + handleWait (cmd NE.:| args) = newNodeRange $ CFApplyEffects $ maybeToList findVar where findVar = do @@ -1110,7 +1111,7 @@ handleCommand cmd vars args literalCmd = do name <- getLiteralString arg return $ IdTagged (getId arg) $ CFWriteVariable name CFValueInteger - handleMapfile (cmd:args) = + handleMapfile (cmd NE.:| args) = newNodeRange $ CFApplyEffects [findVar] where findVar = @@ -1130,7 +1131,7 @@ handleCommand cmd vars args literalCmd = do guard $ isVariableName name return (getId c, name) - handleRead (cmd:args) = newNodeRange $ CFApplyEffects main + handleRead (cmd NE.:| args) = newNodeRange $ CFApplyEffects main where main = fromMaybe fallback $ do flags <- getGnuOpts flagsForRead args @@ -1160,7 +1161,7 @@ handleCommand cmd vars args literalCmd = do in map (\(id, name) -> IdTagged id $ CFWriteVariable name value) namesOrDefault - handleDEFINE (cmd:args) = + handleDEFINE (cmd NE.:| args) = newNodeRange $ CFApplyEffects $ maybeToList findVar where findVar = do @@ -1170,7 +1171,7 @@ handleCommand cmd vars args literalCmd = do return $ IdTagged (getId name) $ CFWriteVariable str CFValueString handleOthers id vars args cmd = - regularExpansion vars args $ do + regularExpansion vars (NE.toList args) $ do exe <- newNodeRange $ CFExecuteCommand cmd status <- newNodeRange $ CFSetExitCode id linkRange exe status @@ -1189,8 +1190,8 @@ handleCommand cmd vars args literalCmd = do linkRanges $ [args] ++ assignments ++ [exe] ++ dropAssignments - regularExpansionWithStatus vars args@(cmd:_) p = do - initial <- regularExpansion vars args p + regularExpansionWithStatus vars args@(cmd NE.:| _) p = do + initial <- regularExpansion vars (NE.toList args) p status <- newNodeRange $ CFSetExitCode (getId cmd) linkRange initial status