diff --git a/LICENSE b/LICENSE
--- a/LICENSE
+++ b/LICENSE
@@ -1,7 +1,7 @@
                     GNU GENERAL PUBLIC LICENSE
                        Version 3, 29 June 2007
 
- Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
+ Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
  Everyone is permitted to copy and distribute verbatim copies
  of this license document, but changing it is not allowed.
 
@@ -645,7 +645,7 @@
     GNU General Public License for more details.
 
     You should have received a copy of the GNU General Public License
-    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 
 Also add information on how to contact you by electronic and paper mail.
 
@@ -664,11 +664,11 @@
   You should also get your employer (if you work as a programmer) or school,
 if any, to sign a "copyright disclaimer" for the program, if necessary.
 For more information on this, and how to apply and follow the GNU GPL, see
-<http://www.gnu.org/licenses/>.
+<https://www.gnu.org/licenses/>.
 
   The GNU General Public License does not permit incorporating your program
 into proprietary programs.  If your program is a subroutine library, you
 may consider it more useful to permit linking proprietary applications with
 the library.  If this is what you want to do, use the GNU Lesser General
 Public License instead of this License.  But first, please read
-<http://www.gnu.org/philosophy/why-not-lgpl.html>.
+<https://www.gnu.org/philosophy/why-not-lgpl.html>.
diff --git a/README.md b/README.md
--- a/README.md
+++ b/README.md
@@ -1,3 +1,5 @@
+[![Build Status](https://travis-ci.org/koalaman/shellcheck.svg?branch=master)](https://travis-ci.org/koalaman/shellcheck)
+
 # ShellCheck - A shell script static analysis tool
 
 ShellCheck is a GPLv3 tool that gives warnings and suggestions for bash/sh shell scripts:
@@ -25,7 +27,7 @@
   - [In your editor](#in-your-editor)
   - [In your build or test suites](#in-your-build-or-test-suites)
 - [Installing](#installing)
-- [Travis CI Setup](#travis-ci-setup)
+- [Travis CI](#travis-ci)
 - [Compiling from source](#compiling-from-source)
   - [Installing Cabal](#installing-cabal)
   - [Compiling ShellCheck](#compiling-shellcheck)
@@ -52,9 +54,9 @@
 
 ### On the web
 
-Paste a shell script on http://www.shellcheck.net for instant feedback.
+Paste a shell script on https://www.shellcheck.net for instant feedback.
 
-[ShellCheck.net](http://www.shellcheck.net) is always synchronized to the latest git commit, and is the easiest way to give ShellCheck a go. Tell your friends!
+[ShellCheck.net](https://www.shellcheck.net) is always synchronized to the latest git commit, and is the easiest way to give ShellCheck a go. Tell your friends!
 
 ### From your terminal
 
@@ -99,7 +101,7 @@
 
     stack update
     stack install ShellCheck
-    
+
 On Debian based distros:
 
     apt-get install shellcheck
@@ -108,6 +110,8 @@
 
     pacman -S shellcheck
 
+or get the dependency free [shellcheck-static](https://aur.archlinux.org/packages/shellcheck-static/) from the AUR.
+
 On Gentoo based distros:
 
     emerge --ask shellcheck
@@ -121,62 +125,66 @@
 
     dnf install ShellCheck
 
-On OS X with homebrew:
+On FreeBSD:
 
-    brew install shellcheck
+    pkg install hs-ShellCheck
 
-On OS X with MacPorts:
+On OS X with homebrew:
 
-    port install shellcheck
+    brew install shellcheck
 
-On openSUSE:Tumbleweed:
+On openSUSE
 
     zypper in ShellCheck
 
-On other openSUSE distributions:
-
-Add OBS devel:languages:haskell repository from https://build.opensuse.org/project/repositories/devel:languages:haskell
-
-    zypper ar http://download.opensuse.org/repositories/devel:/languages:/haskell/openSUSE_$(version)/devel:languages:haskell.repo
-    zypper in ShellCheck
-
 Or use OneClickInstall - https://software.opensuse.org/package/ShellCheck
 
 On Solus:
 
     eopkg install shellcheck
+    
+On Windows (via [scoop](http://scoop.sh)):
 
+    scoop install shellcheck
+
+From Snap Store:
+
+    snap install --channel=edge shellcheck
+
 From Docker Hub:
 
 ```sh
-docker pull koalaman/shellcheck:latest  # Or  :v0.4.6  for a release version
+docker pull koalaman/shellcheck:stable  # Or :v0.4.7 for that version, or :latest for daily builds
 docker run -v "$PWD:/mnt" koalaman/shellcheck myscript
 ```
 
-or use `koalaman/shellcheck-alpine` if you want a larger Alpine Linux based image to extend.
+or use `koalaman/shellcheck-alpine` if you want a larger Alpine Linux based image to extend. It works exactly like a regular Alpine image, but has shellcheck preinstalled.
 
-Alternatively, get freshly built binaries for the latest commit here:
+Alternatively, you can download pre-compiled binaries for the latest release here:
 
-* [Linux, x86_64](https://storage.googleapis.com/shellcheck/shellcheck-latest.linux.x86_64.tar.xz) (statically linked)
-* [Windows, x86](https://storage.googleapis.com/shellcheck/shellcheck-latest.zip)
+* [Linux, x86_64](https://storage.googleapis.com/shellcheck/shellcheck-stable.linux.x86_64.tar.xz) (statically linked)
+* [Windows, x86](https://storage.googleapis.com/shellcheck/shellcheck-stable.zip)
 
-or see the [storage bucket listing](https://shellcheck.storage.googleapis.com/index.html) for checksums and release builds.
+or see the [storage bucket listing](https://shellcheck.storage.googleapis.com/index.html) for checksums, older versions and the latest daily builds.
 
-## Travis CI Setup
+## Travis CI
 
-If you want to use ShellCheck in Travis CI, you can most easily install it via `apt`:
+Travis CI has now integrated ShellCheck by default, so you don't need to manually install it.
 
-```yml
-language: bash
-addons:
-  apt:
-    sources:
-    - debian-sid    # Grab ShellCheck from the Debian repo
-    packages:
-    - shellcheck
-```
+If you still want to do so in order to upgrade at your leisure or ensure the latest release:
 
+    install:
 
+      # Install a custom version of shellcheck instead of Travis CI's default
+      - scversion="stable" # or "v0.4.7", or "latest"
+      - wget "https://storage.googleapis.com/shellcheck/shellcheck-$scversion.linux.x86_64.tar.xz"
+      - tar --xz -xvf "shellcheck-$scversion.linux.x86_64.tar.xz"
+      - shellcheck() { "shellcheck-$scversion/shellcheck" "$@"; }
+      - shellcheck --version
+
+    script:
+      - shellcheck *.sh
+
 ## Compiling from source
 
 This section describes how to build ShellCheck from a source directory. ShellCheck is written in Haskell and requires 2GB of RAM to compile.
@@ -293,6 +301,7 @@
 tr -cd '[a-zA-Z0-9]'              # [] around ranges in tr
 exec foo; echo "Done!"            # Misused 'exec'
 find -name \*.bak -o -name \*~ -delete  # Implicit precedence in find
+# find . -exec foo > bar \;       # Redirections in find
 f() { whoami; }; sudo f           # External use of internal functions
 ```
 
@@ -308,9 +317,13 @@
 echo ${var$n}                     # Wrong indirect reference
 var=(1, 2, 3)                     # Comma separated arrays
 array=( [index] = value )         # Incorrect index initialization
+echo $var[14]                     # Missing {} in array references
 echo "Argument 10 is $10"         # Positional parameter misreference
 if $(myfunction); then ..; fi     # Wrapping commands in $()
 else if othercondition; then ..   # Using 'else if'
+f; f() { echo "hello world; }     # Using function before definition
+[ false ]                         # 'false' being true
+if ( -f file )                    # Using (..) instead of test
 ```
 
 ### Style
@@ -341,6 +354,8 @@
 var=World; echo "Hello " var      # Unused lowercase variables
 echo "Hello $name"                # Unassigned lowercase variables
 cmd | read bar; echo $bar         # Assignments in subshells
+cat foo | cp bar                  # Piping to commands that don't read
+printf '%s: %s\n' foo             # Mismatches in printf argument count
 ```
 
 ### Robustness
@@ -354,6 +369,7 @@
 printf "Hello $name"             # Variables in printf format
 for f in $(ls *.txt); do         # Iterating over ls output
 export MYVAR=$(cmd)              # Masked exit codes
+case $version in 2.*) :;; 2.6.*) # Shadowed case branches
 ```
 
 ### Portability
@@ -388,6 +404,7 @@
 echo $((n/180*100))               # Unnecessary loss of precision
 ls *[:digit:].txt                 # Bad character class globs
 sed 's/foo/bar/' file > file      # Redirecting to input
+while getopts "a" f; do case $f in "b") # Unhandled getopts flags
 ```
 
 ## Testimonials
@@ -422,6 +439,6 @@
 
 ShellCheck is licensed under the GNU General Public License, v3. A copy of this license is included in the file [LICENSE](LICENSE).
 
-Copyright 2012-2015, Vidar 'koala_man' Holen and contributors.
+Copyright 2012-2018, Vidar 'koala_man' Holen and contributors.
 
 Happy ShellChecking!
diff --git a/ShellCheck.cabal b/ShellCheck.cabal
--- a/ShellCheck.cabal
+++ b/ShellCheck.cabal
@@ -1,12 +1,12 @@
 Name:             ShellCheck
-Version:          0.4.7
+Version:          0.5.0
 Synopsis:         Shell script analysis tool
 License:          GPL-3
 License-file:     LICENSE
 Category:         Static Analysis
 Author:           Vidar Holen
 Maintainer:       vidar@vidarholen.net
-Homepage:         http://www.shellcheck.net/
+Homepage:         https://www.shellcheck.net/
 Build-Type:       Custom
 Cabal-Version:    >= 1.8
 Bug-reports:      https://github.com/koalaman/shellcheck/issues
@@ -31,16 +31,29 @@
     -- tests
     test/shellcheck.hs
 
+custom-setup
+  setup-depends:
+    base    >= 4    && <5,
+    process >= 1.0  && <1.7,
+    Cabal   >= 1.10 && <2.3
+
 source-repository head
     type: git
     location: git://github.com/koalaman/shellcheck.git
 
 library
+    hs-source-dirs: src
+    if impl(ghc < 8.0)
+      build-depends:
+        semigroups
     build-depends:
-      base >= 4 && < 5,
-      containers,
+      -- GHC 7.6.3 (base 4.6.0.1) is buggy (#1131, #1119) in optimized mode.
+      -- Just disable that version entirely to fail fast.
+      aeson,
+      base > 4.6.0.1 && < 5,
+      bytestring,
+      containers >= 0.5,
       directory,
-      json,
       mtl >= 2.2.1,
       parsec,
       regex-tdfa,
@@ -69,27 +82,34 @@
       Paths_ShellCheck
 
 executable shellcheck
+    if impl(ghc < 8.0)
+      build-depends:
+        semigroups
     build-depends:
+      aeson,
       base >= 4 && < 5,
+      bytestring,
+      ShellCheck,
       containers,
       directory,
-      json,
       mtl >= 2.2.1,
-      parsec,
-      regex-tdfa,
-      QuickCheck >= 2.7.4
+      parsec >= 3.0,
+      QuickCheck >= 2.7.4,
+      regex-tdfa
     main-is: shellcheck.hs
 
 test-suite test-shellcheck
     type: exitcode-stdio-1.0
     build-depends:
+      aeson,
       base >= 4 && < 5,
+      bytestring,
+      ShellCheck,
       containers,
       directory,
-      json,
       mtl >= 2.2.1,
       parsec,
-      regex-tdfa,
-      QuickCheck >= 2.7.4
+      QuickCheck >= 2.7.4,
+      regex-tdfa
     main-is: test/shellcheck.hs
 
diff --git a/ShellCheck/AST.hs b/ShellCheck/AST.hs
deleted file mode 100644
--- a/ShellCheck/AST.hs
+++ /dev/null
@@ -1,386 +0,0 @@
-{-
-    Copyright 2012-2015 Vidar Holen
-
-    This file is part of ShellCheck.
-    http://www.vidarholen.net/contents/shellcheck
-
-    ShellCheck is free software: you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation, either version 3 of the License, or
-    (at your option) any later version.
-
-    ShellCheck is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program.  If not, see <http://www.gnu.org/licenses/>.
--}
-module ShellCheck.AST where
-
-import Control.Monad.Identity
-import Text.Parsec
-import qualified ShellCheck.Regex as Re
-import Prelude hiding (id)
-
-newtype Id = Id Int deriving (Show, Eq, Ord)
-
-data Quoted = Quoted | Unquoted deriving (Show, Eq)
-data Dashed = Dashed | Undashed deriving (Show, Eq)
-data AssignmentMode = Assign | Append deriving (Show, Eq)
-newtype FunctionKeyword = FunctionKeyword Bool deriving (Show, Eq)
-newtype FunctionParentheses = FunctionParentheses Bool deriving (Show, Eq)
-data CaseType = CaseBreak | CaseFallThrough | CaseContinue deriving (Show, Eq)
-
-newtype Root = Root Token
-data Token =
-    TA_Binary Id String Token Token
-    | TA_Assignment Id String Token Token
-    | TA_Expansion Id [Token]
-    | TA_Index Id Token
-    | TA_Sequence Id [Token]
-    | TA_Trinary Id Token Token Token
-    | TA_Unary Id String Token
-    | TC_And Id ConditionType String Token Token
-    | TC_Binary Id ConditionType String Token Token
-    | TC_Group Id ConditionType Token
-    | TC_Nullary Id ConditionType Token
-    | TC_Or Id ConditionType String Token Token
-    | TC_Unary Id ConditionType String Token
-    | TC_Empty Id ConditionType
-    | T_AND_IF Id
-    | T_AndIf Id Token Token
-    | T_Arithmetic Id Token
-    | T_Array Id [Token]
-    | T_IndexedElement Id [Token] Token
-    -- Store the index as string, and parse as arithmetic or string later
-    | T_UnparsedIndex Id SourcePos String
-    | T_Assignment Id AssignmentMode String [Token] Token
-    | T_Backgrounded Id Token
-    | T_Backticked Id [Token]
-    | T_Bang Id
-    | T_Banged Id Token
-    | T_BraceExpansion Id [Token]
-    | T_BraceGroup Id [Token]
-    | T_CLOBBER Id
-    | T_Case Id
-    | T_CaseExpression Id Token [(CaseType, [Token], [Token])]
-    | T_Condition Id ConditionType Token
-    | T_DGREAT Id
-    | T_DLESS Id
-    | T_DLESSDASH Id
-    | T_DSEMI Id
-    | T_Do Id
-    | T_DollarArithmetic Id Token
-    | T_DollarBraced Id Token
-    | T_DollarBracket Id Token
-    | T_DollarDoubleQuoted Id [Token]
-    | T_DollarExpansion Id [Token]
-    | T_DollarSingleQuoted Id String
-    | T_DollarBraceCommandExpansion Id [Token]
-    | T_Done Id
-    | T_DoubleQuoted Id [Token]
-    | T_EOF Id
-    | T_Elif Id
-    | T_Else Id
-    | T_Esac Id
-    | T_Extglob Id String [Token]
-    | T_FdRedirect Id String Token
-    | T_Fi Id
-    | T_For Id
-    | T_ForArithmetic Id Token Token Token [Token]
-    | T_ForIn Id String [Token] [Token]
-    | T_Function Id FunctionKeyword FunctionParentheses String Token
-    | T_GREATAND Id
-    | T_Glob Id String
-    | T_Greater Id
-    | T_HereDoc Id Dashed Quoted String [Token]
-    | T_HereString Id Token
-    | T_If Id
-    | T_IfExpression Id [([Token],[Token])] [Token]
-    | T_In  Id
-    | T_IoFile Id Token Token
-    | T_IoDuplicate Id Token String
-    | T_LESSAND Id
-    | T_LESSGREAT Id
-    | T_Lbrace Id
-    | T_Less Id
-    | T_Literal Id String
-    | T_Lparen Id
-    | T_NEWLINE Id
-    | T_NormalWord Id [Token]
-    | T_OR_IF Id
-    | T_OrIf Id Token Token
-    | T_ParamSubSpecialChar Id String -- e.g. '%' in ${foo%bar}  or '/' in ${foo/bar/baz}
-    | T_Pipeline Id [Token] [Token] -- [Pipe separators] [Commands]
-    | T_ProcSub Id String [Token]
-    | T_Rbrace Id
-    | T_Redirecting Id [Token] Token
-    | T_Rparen Id
-    | T_Script Id String [Token]
-    | T_Select Id
-    | T_SelectIn Id String [Token] [Token]
-    | T_Semi Id
-    | T_SimpleCommand Id [Token] [Token]
-    | T_SingleQuoted Id String
-    | T_Subshell Id [Token]
-    | T_Then Id
-    | T_Until Id
-    | T_UntilExpression Id [Token] [Token]
-    | T_While Id
-    | T_WhileExpression Id [Token] [Token]
-    | T_Annotation Id [Annotation] Token
-    | T_Pipe Id String
-    | T_CoProc Id (Maybe String) Token
-    | T_CoProcBody Id Token
-    | T_Include Id Token Token -- . & source: SimpleCommand T_Script
-    deriving (Show)
-
-data Annotation =
-    DisableComment Integer
-    | SourceOverride String
-    | ShellOverride String
-    deriving (Show, Eq)
-data ConditionType = DoubleBracket | SingleBracket deriving (Show, Eq)
-
--- This is an abomination.
-tokenEquals :: Token -> Token -> Bool
-tokenEquals a b = kludge a == kludge b
-    where kludge s = Re.subRegex (Re.mkRegex "\\(Id [0-9]+\\)") (show s) "(Id 0)"
-
-instance Eq Token where
-    (==) = tokenEquals
-
-analyze :: Monad m => (Token -> m ()) -> (Token -> m ()) -> (Token -> m Token) -> Token -> m Token
-analyze f g i =
-    round
-  where
-    round t = do
-        f t
-        newT <- delve t
-        g t
-        i newT
-    roundAll = mapM round
-
-    dl l v = do
-        x <- roundAll l
-        return $ v x
-    dll l m v = do
-        x <- roundAll l
-        y <- roundAll m
-        return $ v x y
-    d1 t v = do
-        x <- round t
-        return $ v x
-    d2 t1 t2 v = do
-        x <- round t1
-        y <- round t2
-        return $ v x y
-
-    delve (T_NormalWord id list) = dl list $ T_NormalWord id
-    delve (T_DoubleQuoted id list) = dl list $ T_DoubleQuoted id
-    delve (T_DollarDoubleQuoted id list) = dl list $ T_DollarDoubleQuoted id
-    delve (T_DollarExpansion id list) = dl list $ T_DollarExpansion id
-    delve (T_DollarBraceCommandExpansion id list) = dl list $ T_DollarBraceCommandExpansion id
-    delve (T_BraceExpansion id list) = dl list $ T_BraceExpansion id
-    delve (T_Backticked id list) = dl list $ T_Backticked id
-    delve (T_DollarArithmetic id c) = d1 c $ T_DollarArithmetic id
-    delve (T_DollarBracket id c) = d1 c $ T_DollarBracket id
-    delve (T_IoFile id op file) = d2 op file $ T_IoFile id
-    delve (T_IoDuplicate id op num) = d1 op $ \x -> T_IoDuplicate id x num
-    delve (T_HereString id word) = d1 word $ T_HereString id
-    delve (T_FdRedirect id v t) = d1 t $ T_FdRedirect id v
-    delve (T_Assignment id mode var indices value) = do
-        a <- roundAll indices
-        b <- round value
-        return $ T_Assignment id mode var a b
-    delve (T_Array id t) = dl t $ T_Array id
-    delve (T_IndexedElement id indices t) = do
-        a <- roundAll indices
-        b <- round t
-        return $ T_IndexedElement id a b
-    delve (T_Redirecting id redirs cmd) = do
-        a <- roundAll redirs
-        b <- round cmd
-        return $ T_Redirecting id a b
-    delve (T_SimpleCommand id vars cmds) = dll vars cmds $ T_SimpleCommand id
-    delve (T_Pipeline id l1 l2) = dll l1 l2 $ T_Pipeline id
-    delve (T_Banged id l) = d1 l $ T_Banged id
-    delve (T_AndIf id t u) = d2 t u $ T_AndIf id
-    delve (T_OrIf id t u) = d2 t u $ T_OrIf id
-    delve (T_Backgrounded id l) = d1 l $ T_Backgrounded id
-    delve (T_Subshell id l) = dl l $ T_Subshell id
-    delve (T_ProcSub id typ l) = dl l $ T_ProcSub id typ
-    delve (T_Arithmetic id c) = d1 c $ T_Arithmetic id
-    delve (T_IfExpression id conditions elses) = do
-        newConds <- mapM (\(c, t) -> do
-                            x <- mapM round c
-                            y <- mapM round t
-                            return (x,y)
-                    ) conditions
-        newElses <- roundAll elses
-        return $ T_IfExpression id newConds newElses
-    delve (T_BraceGroup id l) = dl l $ T_BraceGroup id
-    delve (T_WhileExpression id c l) = dll c l $ T_WhileExpression id
-    delve (T_UntilExpression id c l) = dll c l $ T_UntilExpression id
-    delve (T_ForIn id v w l) = dll w l $ T_ForIn id v
-    delve (T_SelectIn id v w l) = dll w l $ T_SelectIn id v
-    delve (T_CaseExpression id word cases) = do
-        newWord <- round word
-        newCases <- mapM (\(o, c, t) -> do
-                            x <- mapM round c
-                            y <- mapM round t
-                            return (o, x,y)
-                        ) cases
-        return $ T_CaseExpression id newWord newCases
-
-    delve (T_ForArithmetic id a b c group) = do
-        x <- round a
-        y <- round b
-        z <- round c
-        list <- mapM round group
-        return $ T_ForArithmetic id x y z list
-
-    delve (T_Script id s l) = dl l $ T_Script id s
-    delve (T_Function id a b name body) = d1 body $ T_Function id a b name
-    delve (T_Condition id typ token) = d1 token $ T_Condition id typ
-    delve (T_Extglob id str l) = dl l $ T_Extglob id str
-    delve (T_DollarBraced id op) = d1 op $ T_DollarBraced id
-    delve (T_HereDoc id d q str l) = dl l $ T_HereDoc id d q str
-
-    delve (TC_And id typ str t1 t2) = d2 t1 t2 $ TC_And id typ str
-    delve (TC_Or id typ str t1 t2) = d2 t1 t2 $ TC_Or id typ str
-    delve (TC_Group id typ token) = d1 token $ TC_Group id typ
-    delve (TC_Binary id typ op lhs rhs) = d2 lhs rhs $ TC_Binary id typ op
-    delve (TC_Unary id typ op token) = d1 token $ TC_Unary id typ op
-    delve (TC_Nullary id typ token) = d1 token $ TC_Nullary id typ
-
-    delve (TA_Binary id op t1 t2) = d2 t1 t2 $ TA_Binary id op
-    delve (TA_Assignment id op t1 t2) = d2 t1 t2 $ TA_Assignment id op
-    delve (TA_Unary id op t1) = d1 t1 $ TA_Unary id op
-    delve (TA_Sequence id l) = dl l $ TA_Sequence id
-    delve (TA_Trinary id t1 t2 t3) = do
-        a <- round t1
-        b <- round t2
-        c <- round t3
-        return $ TA_Trinary id a b c
-    delve (TA_Expansion id t) = dl t $ TA_Expansion id
-    delve (TA_Index id t) = d1 t $ TA_Index id
-    delve (T_Annotation id anns t) = d1 t $ T_Annotation id anns
-    delve (T_CoProc id var body) = d1 body $ T_CoProc id var
-    delve (T_CoProcBody id t) = d1 t $ T_CoProcBody id
-    delve (T_Include id includer script) = d2 includer script $ T_Include id
-    delve t = return t
-
-getId :: Token -> Id
-getId t = case t of
-        T_AND_IF id  -> id
-        T_OR_IF id  -> id
-        T_DSEMI id  -> id
-        T_Semi id  -> id
-        T_DLESS id  -> id
-        T_DGREAT id  -> id
-        T_LESSAND id  -> id
-        T_GREATAND id  -> id
-        T_LESSGREAT id  -> id
-        T_DLESSDASH id  -> id
-        T_CLOBBER id  -> id
-        T_If id  -> id
-        T_Then id  -> id
-        T_Else id  -> id
-        T_Elif id  -> id
-        T_Fi id  -> id
-        T_Do id  -> id
-        T_Done id  -> id
-        T_Case id  -> id
-        T_Esac id  -> id
-        T_While id  -> id
-        T_Until id  -> id
-        T_For id  -> id
-        T_Select id  -> id
-        T_Lbrace id  -> id
-        T_Rbrace id  -> id
-        T_Lparen id  -> id
-        T_Rparen id  -> id
-        T_Bang id  -> id
-        T_In  id  -> id
-        T_NEWLINE id  -> id
-        T_EOF id  -> id
-        T_Less id  -> id
-        T_Greater id  -> id
-        T_SingleQuoted id _  -> id
-        T_Literal id _  -> id
-        T_NormalWord id _  -> id
-        T_DoubleQuoted id _  -> id
-        T_DollarExpansion id _  -> id
-        T_DollarBraced id _  -> id
-        T_DollarArithmetic id _  -> id
-        T_BraceExpansion id _  -> id
-        T_ParamSubSpecialChar id _ -> id
-        T_DollarBraceCommandExpansion id _  -> id
-        T_IoFile id _ _  -> id
-        T_IoDuplicate id _ _  -> id
-        T_HereDoc id _ _ _ _ -> id
-        T_HereString id _  -> id
-        T_FdRedirect id _ _  -> id
-        T_Assignment id _ _ _ _  -> id
-        T_Array id _  -> id
-        T_IndexedElement id _ _  -> id
-        T_Redirecting id _ _  -> id
-        T_SimpleCommand id _ _  -> id
-        T_Pipeline id _ _  -> id
-        T_Banged id _  -> id
-        T_AndIf id _ _ -> id
-        T_OrIf id _ _ -> id
-        T_Backgrounded id _  -> id
-        T_IfExpression id _ _  -> id
-        T_Subshell id _  -> id
-        T_BraceGroup id _  -> id
-        T_WhileExpression id _ _  -> id
-        T_UntilExpression id _ _  -> id
-        T_ForIn id _ _ _  -> id
-        T_SelectIn id _ _ _  -> id
-        T_CaseExpression id _ _ -> id
-        T_Function id _ _ _ _  -> id
-        T_Arithmetic id _  -> id
-        T_Script id _ _  -> id
-        T_Condition id _ _  -> id
-        T_Extglob id _ _ -> id
-        T_Backticked id _ -> id
-        TC_And id _ _ _ _  -> id
-        TC_Or id _ _ _ _  -> id
-        TC_Group id _ _  -> id
-        TC_Binary id _ _ _ _  -> id
-        TC_Unary id _ _ _  -> id
-        TC_Nullary id _ _  -> id
-        TA_Binary id _ _ _  -> id
-        TA_Assignment id _ _ _  -> id
-        TA_Unary id _ _  -> id
-        TA_Sequence id _  -> id
-        TA_Trinary id _ _ _  -> id
-        TA_Expansion id _  -> id
-        TA_Index id _  -> id
-        T_ProcSub id _ _ -> id
-        T_Glob id _ -> id
-        T_ForArithmetic id _ _ _ _ -> id
-        T_DollarSingleQuoted id _ -> id
-        T_DollarDoubleQuoted id _ -> id
-        T_DollarBracket id _ -> id
-        T_Annotation id _ _ -> id
-        T_Pipe id _ -> id
-        T_CoProc id _ _ -> id
-        T_CoProcBody id _ -> id
-        T_Include id _ _ -> id
-        T_UnparsedIndex id _ _ -> id
-        TC_Empty id _ -> id
-
-blank :: Monad m => Token -> m ()
-blank = const $ return ()
-doAnalysis :: Monad m => (Token -> m ()) -> Token -> m Token
-doAnalysis f = analyze f blank return
-doStackAnalysis :: Monad m => (Token -> m ()) -> (Token -> m ()) -> Token -> m Token
-doStackAnalysis startToken endToken = analyze startToken endToken return
-doTransform :: (Token -> Token) -> Token -> Token
-doTransform i = runIdentity . analyze blank blank (return . i)
-
diff --git a/ShellCheck/ASTLib.hs b/ShellCheck/ASTLib.hs
deleted file mode 100644
--- a/ShellCheck/ASTLib.hs
+++ /dev/null
@@ -1,436 +0,0 @@
-{-
-    Copyright 2012-2015 Vidar Holen
-
-    This file is part of ShellCheck.
-    http://www.vidarholen.net/contents/shellcheck
-
-    ShellCheck is free software: you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation, either version 3 of the License, or
-    (at your option) any later version.
-
-    ShellCheck is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program.  If not, see <http://www.gnu.org/licenses/>.
--}
-module ShellCheck.ASTLib where
-
-import ShellCheck.AST
-
-import Control.Monad.Writer
-import Control.Monad
-import Data.Functor
-import Data.List
-import Data.Maybe
-
--- Is this a type of loop?
-isLoop t = case t of
-        T_WhileExpression {} -> True
-        T_UntilExpression {} -> True
-        T_ForIn {} -> True
-        T_ForArithmetic {} -> True
-        T_SelectIn {}  -> True
-        _ -> False
-
--- Will this split into multiple words when used as an argument?
-willSplit x =
-  case x of
-    T_DollarBraced {} -> True
-    T_DollarExpansion {} -> True
-    T_Backticked {} -> True
-    T_BraceExpansion {} -> True
-    T_Glob {} -> True
-    T_Extglob {} -> True
-    T_NormalWord _ l -> any willSplit l
-    _ -> False
-
-isGlob T_Extglob {} = True
-isGlob T_Glob {} = True
-isGlob (T_NormalWord _ l) = any isGlob l
-isGlob _ = False
-
--- Is this shell word a constant?
-isConstant token =
-    case token of
-        -- This ignores some cases like ~"foo":
-        T_NormalWord _ (T_Literal _ ('~':_) : _)  -> False
-        T_NormalWord _ l   -> all isConstant l
-        T_DoubleQuoted _ l -> all isConstant l
-        T_SingleQuoted _ _ -> True
-        T_Literal _ _ -> True
-        _ -> False
-
--- Is this an empty literal?
-isEmpty token =
-    case token of
-        T_NormalWord _ l   -> all isEmpty l
-        T_DoubleQuoted _ l -> all isEmpty l
-        T_SingleQuoted _ "" -> True
-        T_Literal _ "" -> True
-        _ -> False
-
--- Quick&lazy oversimplification of commands, throwing away details
--- and returning a list like  ["find", ".", "-name", "${VAR}*" ].
-oversimplify token =
-    case token of
-        (T_NormalWord _ l) -> [concat (concatMap oversimplify l)]
-        (T_DoubleQuoted _ l) -> [concat (concatMap oversimplify l)]
-        (T_SingleQuoted _ s) -> [s]
-        (T_DollarBraced _ _) -> ["${VAR}"]
-        (T_DollarArithmetic _ _) -> ["${VAR}"]
-        (T_DollarExpansion _ _) -> ["${VAR}"]
-        (T_Backticked _ _) -> ["${VAR}"]
-        (T_Glob _ s) -> [s]
-        (T_Pipeline _ _ [x]) -> oversimplify x
-        (T_Literal _ x) -> [x]
-        (T_ParamSubSpecialChar _ x) -> [x]
-        (T_SimpleCommand _ vars words) -> concatMap oversimplify words
-        (T_Redirecting _ _ foo) -> oversimplify foo
-        (T_DollarSingleQuoted _ s) -> [s]
-        (T_Annotation _ _ s) -> oversimplify s
-        -- Workaround for let "foo = bar" parsing
-        (TA_Sequence _ [TA_Expansion _ v]) -> concatMap oversimplify v
-        _ -> []
-
-
--- Turn a SimpleCommand foo -avz --bar=baz into args "a", "v", "z", "bar",
--- each in a tuple of (token, stringFlag). Non-flag arguments are added with
--- stringFlag == "".
-getFlagsUntil stopCondition (T_SimpleCommand _ _ (_:args)) =
-    let tokenAndText = map (\x -> (x, concat $ oversimplify x)) args
-        (flagArgs, rest) = break (stopCondition . snd) tokenAndText
-    in
-        concatMap flag flagArgs ++ map (\(t, _) -> (t, "")) rest
-  where
-    flag (x, '-':'-':arg) = [ (x, takeWhile (/= '=') arg) ]
-    flag (x, '-':args) = map (\v -> (x, [v])) args
-    flag (x, _) = [ (x, "") ]
-getFlagsUntil _ _ = error "Internal shellcheck error, please report! (getFlags on non-command)"
-
--- Get all flags in a GNU way, up until --
-getAllFlags = getFlagsUntil (== "--")
--- Get all flags in a BSD way, up until first non-flag argument or --
-getLeadingFlags = getFlagsUntil (\x -> x == "--" || (not $ "-" `isPrefixOf` x))
-
--- Check if a command has a flag.
-hasFlag cmd str = str `elem` (map snd $ getAllFlags cmd)
-
--- Is this token a word that starts with a dash?
-isFlag token =
-    case getWordParts token of
-        T_Literal _ ('-':_) : _ -> True
-        _ -> False
-
--- Is this token a flag where the - is unquoted?
-isUnquotedFlag token = fromMaybe False $ do
-    str <- getLeadingUnquotedString token
-    return $ "-" `isPrefixOf` str
-
--- Given a T_DollarBraced, return a simplified version of the string contents.
-bracedString (T_DollarBraced _ l) = concat $ oversimplify l
-bracedString _ = error "Internal shellcheck error, please report! (bracedString on non-variable)"
-
--- Is this an expansion of multiple items of an array?
-isArrayExpansion t@(T_DollarBraced _ _) =
-    let string = bracedString t in
-        "@" `isPrefixOf` string ||
-            not ("#" `isPrefixOf` string) && "[@]" `isInfixOf` string
-isArrayExpansion _ = False
-
--- Is it possible that this arg becomes multiple args?
-mayBecomeMultipleArgs t = willBecomeMultipleArgs t || f t
-  where
-    f t@(T_DollarBraced _ _) =
-        let string = bracedString t in
-            "!" `isPrefixOf` string
-    f (T_DoubleQuoted _ parts) = any f parts
-    f (T_NormalWord _ parts) = any f parts
-    f _ = False
-
--- Is it certain that this word will becomes multiple words?
-willBecomeMultipleArgs t = willConcatInAssignment t || f t
-  where
-    f T_Extglob {} = True
-    f T_Glob {} = True
-    f T_BraceExpansion {} = True
-    f (T_DoubleQuoted _ parts) = any f parts
-    f (T_NormalWord _ parts) = any f parts
-    f _ = False
-
--- This does token cause implicit concatenation in assignments?
-willConcatInAssignment token =
-    case token of
-        t@T_DollarBraced {} -> isArrayExpansion t
-        (T_DoubleQuoted _ parts) -> any willConcatInAssignment parts
-        (T_NormalWord _ parts) -> any willConcatInAssignment parts
-        _ -> False
-
--- Maybe get the literal string corresponding to this token
-getLiteralString :: Token -> Maybe String
-getLiteralString = getLiteralStringExt (const Nothing)
-
--- Definitely get a literal string, skipping over all non-literals
-onlyLiteralString :: Token -> String
-onlyLiteralString = fromJust . getLiteralStringExt (const $ return "")
-
--- Maybe get a literal string, but only if it's an unquoted argument.
-getUnquotedLiteral (T_NormalWord _ list) =
-    concat <$> mapM str list
-  where
-    str (T_Literal _ s) = return s
-    str _ = Nothing
-getUnquotedLiteral _ = Nothing
-
--- Get the last unquoted T_Literal in a word like "${var}foo"THIS
--- or nothing if the word does not end in an unquoted literal.
-getTrailingUnquotedLiteral :: Token -> Maybe Token
-getTrailingUnquotedLiteral t =
-    case t of
-        (T_NormalWord _ list@(_:_)) ->
-            from (last list)
-        _ -> Nothing
-  where
-    from t =
-        case t of
-            T_Literal {} -> return t
-            _ -> Nothing
-
--- Get the leading, unquoted, literal string of a token (if any).
-getLeadingUnquotedString :: Token -> Maybe String
-getLeadingUnquotedString t =
-    case t of
-        T_NormalWord _ ((T_Literal _ s) : _) -> return s
-        _ -> Nothing
-
--- Maybe get the literal string of this token and any globs in it.
-getGlobOrLiteralString = getLiteralStringExt f
-  where
-    f (T_Glob _ str) = return str
-    f _ = Nothing
-
--- Maybe get the literal value of a token, using a custom function
--- to map unrecognized Tokens into strings.
-getLiteralStringExt :: (Token -> Maybe String) -> Token -> Maybe String
-getLiteralStringExt more = g
-  where
-    allInList = fmap concat . mapM g
-    g (T_DoubleQuoted _ l) = allInList l
-    g (T_DollarDoubleQuoted _ l) = allInList l
-    g (T_NormalWord _ l) = allInList l
-    g (TA_Expansion _ l) = allInList l
-    g (T_SingleQuoted _ s) = return s
-    g (T_Literal _ s) = return s
-    g (T_ParamSubSpecialChar _ s) = return s
-    g x = more x
-
--- Is this token a string literal?
-isLiteral t = isJust $ getLiteralString t
-
-
--- Turn a NormalWord like foo="bar $baz" into a series of constituent elements like [foo=,bar ,$baz]
-getWordParts (T_NormalWord _ l)   = concatMap getWordParts l
-getWordParts (T_DoubleQuoted _ l) = l
--- TA_Expansion is basically T_NormalWord for arithmetic expressions
-getWordParts (TA_Expansion _ l)   = concatMap getWordParts l
-getWordParts other                = [other]
-
--- Return a list of NormalWords that would result from brace expansion
-braceExpand (T_NormalWord id list) = take 1000 $ do
-    items <- mapM part list
-    return $ T_NormalWord id items
-  where
-    part (T_BraceExpansion id items) = do
-        item <- items
-        braceExpand item
-    part x = return x
-
--- Maybe get a SimpleCommand from immediate wrappers like T_Redirections
-getCommand t =
-    case t of
-        T_Redirecting _ _ w -> getCommand w
-        T_SimpleCommand _ _ (w:_) -> return t
-        T_Annotation _ _ t -> getCommand t
-        _ -> Nothing
-
--- Maybe get the command name of a token representing a command
-getCommandName t = do
-    (T_SimpleCommand _ _ (w:rest)) <- getCommand t
-    s <- getLiteralString w
-    if "busybox" `isSuffixOf` s
-        then
-            case rest of
-                (applet:_) -> getLiteralString applet
-                _ -> return s
-        else
-            return s
-
--- If a command substitution is a single command, get its name.
---  $(date +%s) = Just "date"
-getCommandNameFromExpansion :: Token -> Maybe String
-getCommandNameFromExpansion t =
-    case t of
-        T_DollarExpansion _ [c] -> extract c
-        T_Backticked _ [c] -> extract c
-        T_DollarBraceCommandExpansion _ [c] -> extract c
-        _ -> Nothing
-  where
-    extract (T_Pipeline _ _ [cmd]) = getCommandName cmd
-    extract _ = Nothing
-
--- Get the basename of a token representing a command
-getCommandBasename = fmap basename . getCommandName
-  where
-    basename = reverse . takeWhile (/= '/') . reverse
-
-isAssignment t =
-    case t of
-        T_Redirecting _ _ w -> isAssignment w
-        T_SimpleCommand _ (w:_) [] -> True
-        T_Assignment {} -> True
-        T_Annotation _ _ w -> isAssignment w
-        _ -> False
-
-isOnlyRedirection t =
-    case t of
-        T_Pipeline _ _ [x] -> isOnlyRedirection x
-        T_Annotation _ _ w -> isOnlyRedirection w
-        T_Redirecting _ (_:_) c -> isOnlyRedirection c
-        T_SimpleCommand _ [] [] -> True
-        _ -> False
-
-isFunction t = case t of T_Function {} -> True; _ -> False
-
-isBraceExpansion t = case t of T_BraceExpansion {} -> True; _ -> False
-
--- Get the lists of commands from tokens that contain them, such as
--- the body of while loops or branches of if statements.
-getCommandSequences :: Token -> [[Token]]
-getCommandSequences t =
-    case t of
-        T_Script _ _ cmds -> [cmds]
-        T_BraceGroup _ cmds -> [cmds]
-        T_Subshell _ cmds -> [cmds]
-        T_WhileExpression _ _ cmds -> [cmds]
-        T_UntilExpression _ _ cmds -> [cmds]
-        T_ForIn _ _ _ cmds -> [cmds]
-        T_ForArithmetic _ _ _ _ cmds -> [cmds]
-        T_IfExpression _ thens elses -> map snd thens ++ [elses]
-        T_Annotation _ _ t -> getCommandSequences t
-        _ -> []
-
--- Get a list of names of associative arrays
-getAssociativeArrays t =
-    nub . execWriter $ doAnalysis f t
-  where
-    f :: Token -> Writer [String] ()
-    f t@T_SimpleCommand {} = fromMaybe (return ()) $ do
-        name <- getCommandName t
-        let assocNames = ["declare","local","typeset"]
-        guard $ elem name assocNames
-        let flags = getAllFlags t
-        guard $ elem "A" $ map snd flags
-        let args = map fst . filter ((==) "" . snd) $ flags
-        let names = mapMaybe (getLiteralStringExt nameAssignments) args
-        return $ tell names
-    f _ = return ()
-
-    nameAssignments t =
-        case t of
-            T_Assignment _ _ name _ _ -> return name
-            _ -> Nothing
-
--- A Pseudoglob is a wildcard pattern used for checking if a match can succeed.
--- For example, [[ $(cmd).jpg == [a-z] ]] will give the patterns *.jpg and ?, which
--- can be proven never to match.
-data PseudoGlob = PGAny | PGMany | PGChar Char
-    deriving (Eq, Show)
-
--- Turn a word into a PG pattern, replacing all unknown/runtime values with
--- PGMany.
-wordToPseudoGlob :: Token -> Maybe [PseudoGlob]
-wordToPseudoGlob word =
-    simplifyPseudoGlob . concat <$> mapM f (getWordParts word)
-  where
-    f x = case x of
-        T_Literal _ s -> return $ map PGChar s
-        T_SingleQuoted _ s -> return $ map PGChar s
-
-        T_DollarBraced {} -> return [PGMany]
-        T_DollarExpansion {} -> return [PGMany]
-        T_Backticked {} -> return [PGMany]
-
-        T_Glob _ "?" -> return [PGAny]
-        T_Glob _ ('[':_)  -> return [PGAny]
-        T_Glob {} -> return [PGMany]
-
-        T_Extglob {} -> return [PGMany]
-
-        _ -> return [PGMany]
-
--- Turn a word into a PG pattern, but only if we can preserve
--- exact semantics.
-wordToExactPseudoGlob :: Token -> Maybe [PseudoGlob]
-wordToExactPseudoGlob word =
-    simplifyPseudoGlob . concat <$> mapM f (getWordParts word)
-  where
-    f x = case x of
-        T_Literal _ s -> return $ map PGChar s
-        T_SingleQuoted _ s -> return $ map PGChar s
-        T_Glob _ "?" -> return [PGAny]
-        T_Glob _ "*" -> return [PGMany]
-        _ -> fail "Unknown token type"
-
--- Reorder a PseudoGlob for more efficient matching, e.g.
--- f?*?**g -> f??*g
-simplifyPseudoGlob :: [PseudoGlob] -> [PseudoGlob]
-simplifyPseudoGlob = f
-  where
-    f [] = []
-    f (x@(PGChar _) : rest ) = x : f rest
-    f list =
-        let (anys, rest) = span (\x -> x == PGMany || x == PGAny) list in
-            order anys ++ f rest
-
-    order s = let (any, many) = partition (== PGAny) s in
-        any ++ take 1 many
-
--- Check whether the two patterns can ever overlap.
-pseudoGlobsCanOverlap :: [PseudoGlob] -> [PseudoGlob] -> Bool
-pseudoGlobsCanOverlap = matchable
-  where
-    matchable x@(xf:xs) y@(yf:ys) =
-        case (xf, yf) of
-            (PGMany, _) -> matchable x ys || matchable xs y
-            (_, PGMany) -> matchable x ys || matchable xs y
-            (PGAny, _) -> matchable xs ys
-            (_, PGAny) -> matchable xs ys
-            (_, _) -> xf == yf && matchable xs ys
-
-    matchable [] [] = True
-    matchable (PGMany : rest) [] = matchable rest []
-    matchable (_:_) [] = False
-    matchable [] r = matchable r []
-
--- Check whether the first pattern always overlaps the second.
-pseudoGlobIsSuperSetof :: [PseudoGlob] -> [PseudoGlob] -> Bool
-pseudoGlobIsSuperSetof = matchable
-  where
-    matchable x@(xf:xs) y@(yf:ys) =
-        case (xf, yf) of
-            (PGMany, PGMany) -> matchable x ys
-            (PGMany, _) -> matchable x ys || matchable xs y
-            (_, PGMany) -> False
-            (PGAny, _) -> matchable xs ys
-            (_, PGAny) -> False
-            (_, _) -> xf == yf && matchable xs ys
-
-    matchable [] [] = True
-    matchable (PGMany : rest) [] = matchable rest []
-    matchable _ _ = False
-
-wordsCanBeEqual x y = fromMaybe True $
-    liftM2 pseudoGlobsCanOverlap (wordToPseudoGlob x) (wordToPseudoGlob y)
diff --git a/ShellCheck/Analytics.hs b/ShellCheck/Analytics.hs
deleted file mode 100644
--- a/ShellCheck/Analytics.hs
+++ /dev/null
@@ -1,2895 +0,0 @@
-{-
-    Copyright 2012-2015 Vidar Holen
-
-    This file is part of ShellCheck.
-    http://www.vidarholen.net/contents/shellcheck
-
-    ShellCheck is free software: you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation, either version 3 of the License, or
-    (at your option) any later version.
-
-    ShellCheck is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program.  If not, see <http://www.gnu.org/licenses/>.
--}
-{-# LANGUAGE TemplateHaskell, FlexibleContexts #-}
-module ShellCheck.Analytics (runAnalytics, ShellCheck.Analytics.runTests) where
-
-import ShellCheck.AST
-import ShellCheck.ASTLib
-import ShellCheck.AnalyzerLib hiding (producesComments)
-import ShellCheck.Data
-import ShellCheck.Parser
-import ShellCheck.Interface
-import ShellCheck.Regex
-
-import Control.Arrow (first)
-import Control.Monad
-import Control.Monad.Identity
-import Control.Monad.State
-import Control.Monad.Writer
-import Control.Monad.Reader
-import Data.Char
-import Data.Functor
-import Data.Function (on)
-import Data.List
-import Data.Maybe
-import Data.Ord
-import Debug.Trace
-import qualified Data.Map.Strict as Map
-import Test.QuickCheck.All (forAllProperties)
-import Test.QuickCheck.Test (quickCheckWithResult, stdArgs, maxSuccess)
-
--- Checks that are run on the AST root
-treeChecks :: [Parameters -> Token -> [TokenComment]]
-treeChecks = [
-    runNodeAnalysis
-        (\p t -> (mapM_ ((\ f -> f t) . (\ f -> f p))
-            nodeChecks))
-    ,subshellAssignmentCheck
-    ,checkSpacefulness
-    ,checkQuotesInLiterals
-    ,checkShebangParameters
-    ,checkFunctionsUsedExternally
-    ,checkUnusedAssignments
-    ,checkUnpassedInFunctions
-    ,checkArrayWithoutIndex
-    ,checkShebang
-    ,checkUnassignedReferences
-    ,checkUncheckedCdPushdPopd
-    ,checkArrayAssignmentIndices
-    ,checkUseBeforeDefinition
-    ]
-
-runAnalytics :: AnalysisSpec -> [TokenComment]
-runAnalytics options =
-        runList options treeChecks
-
-runList :: AnalysisSpec -> [Parameters -> Token -> [TokenComment]]
-    -> [TokenComment]
-runList spec list = notes
-    where
-        root = asScript spec
-        params = makeParameters spec
-        notes = concatMap (\f -> f params root) list
-
-
-checkList l t = concatMap (\f -> f t) l
-
-
--- Checks that are run on each node in the AST
-runNodeAnalysis f p t = execWriter (doAnalysis (f p) t)
-
-nodeChecks :: [Parameters -> Token -> Writer [TokenComment] ()]
-nodeChecks = [
-    checkUuoc
-    ,checkPipePitfalls
-    ,checkForInQuoted
-    ,checkForInLs
-    ,checkShorthandIf
-    ,checkDollarStar
-    ,checkUnquotedDollarAt
-    ,checkStderrRedirect
-    ,checkUnquotedN
-    ,checkNumberComparisons
-    ,checkSingleBracketOperators
-    ,checkDoubleBracketOperators
-    ,checkLiteralBreakingTest
-    ,checkConstantNullary
-    ,checkDivBeforeMult
-    ,checkArithmeticDeref
-    ,checkArithmeticBadOctal
-    ,checkComparisonAgainstGlob
-    ,checkCommarrays
-    ,checkOrNeq
-    ,checkEchoWc
-    ,checkConstantIfs
-    ,checkPipedAssignment
-    ,checkAssignAteCommand
-    ,checkUuoeVar
-    ,checkQuotedCondRegex
-    ,checkForInCat
-    ,checkFindExec
-    ,checkValidCondOps
-    ,checkGlobbedRegex
-    ,checkTestRedirects
-    ,checkIndirectExpansion
-    ,checkSudoRedirect
-    ,checkPS1Assignments
-    ,checkBackticks
-    ,checkInexplicablyUnquoted
-    ,checkTildeInQuotes
-    ,checkLonelyDotDash
-    ,checkSpuriousExec
-    ,checkSpuriousExpansion
-    ,checkDollarBrackets
-    ,checkSshHereDoc
-    ,checkGlobsAsOptions
-    ,checkWhileReadPitfalls
-    ,checkArithmeticOpCommand
-    ,checkCharRangeGlob
-    ,checkUnquotedExpansions
-    ,checkSingleQuotedVariables
-    ,checkRedirectToSame
-    ,checkPrefixAssignmentReference
-    ,checkLoopKeywordScope
-    ,checkCdAndBack
-    ,checkWrongArithmeticAssignment
-    ,checkConditionalAndOrs
-    ,checkFunctionDeclarations
-    ,checkStderrPipe
-    ,checkOverridingPath
-    ,checkArrayAsString
-    ,checkUnsupported
-    ,checkMultipleAppends
-    ,checkSuspiciousIFS
-    ,checkShouldUseGrepQ
-    ,checkTestArgumentSplitting
-    ,checkConcatenatedDollarAt
-    ,checkTildeInPath
-    ,checkMaskedReturns
-    ,checkReadWithoutR
-    ,checkLoopVariableReassignment
-    ,checkTrailingBracket
-    ,checkReturnAgainstZero
-    ,checkRedirectedNowhere
-    ,checkUnmatchableCases
-    ,checkSubshellAsTest
-    ,checkSplittingInArrays
-    ,checkRedirectionToNumber
-    ,checkGlobAsCommand
-    ,checkFlagAsCommand
-    ,checkEmptyCondition
-    ,checkPipeToNowhere
-    ]
-
-
-wouldHaveBeenGlob s = '*' `elem` s
-
-verify :: (Parameters -> Token -> Writer [TokenComment] ()) -> String -> Bool
-verify f s = checkNode f s == Just True
-
-verifyNot :: (Parameters -> Token -> Writer [TokenComment] ()) -> String -> Bool
-verifyNot f s = checkNode f s == Just False
-
-verifyTree :: (Parameters -> Token -> [TokenComment]) -> String -> Bool
-verifyTree f s = producesComments f s == Just True
-
-verifyNotTree :: (Parameters -> Token -> [TokenComment]) -> String -> Bool
-verifyNotTree f s = producesComments f s == Just False
-
-checkCommand str f t@(T_SimpleCommand id _ (cmd:rest)) =
-    when (t `isCommand` str) $ f cmd rest
-checkCommand _ _ _ = return ()
-
-checkUnqualifiedCommand str f t@(T_SimpleCommand id _ (cmd:rest)) =
-    when (t `isUnqualifiedCommand` str) $ f cmd rest
-checkUnqualifiedCommand _ _ _ = return ()
-
-
-checkNode f = producesComments (runNodeAnalysis f)
-producesComments :: (Parameters -> Token -> [TokenComment]) -> String -> Maybe Bool
-producesComments f s = do
-        root <- pScript s
-        return . not . null $ runList (defaultSpec root) [f]
-
--- Copied from https://wiki.haskell.org/Edit_distance
-dist :: Eq a => [a] -> [a] -> Int
-dist a b
-    = last (if lab == 0 then mainDiag
-            else if lab > 0 then lowers !! (lab - 1)
-                 else{- < 0 -}   uppers !! (-1 - lab))
-    where mainDiag = oneDiag a b (head uppers) (-1 : head lowers)
-          uppers = eachDiag a b (mainDiag : uppers) -- upper diagonals
-          lowers = eachDiag b a (mainDiag : lowers) -- lower diagonals
-          eachDiag a [] diags = []
-          eachDiag a (bch:bs) (lastDiag:diags) = oneDiag a bs nextDiag lastDiag : eachDiag a bs diags
-              where nextDiag = head (tail diags)
-          oneDiag a b diagAbove diagBelow = thisdiag
-              where doDiag [] b nw n w = []
-                    doDiag a [] nw n w = []
-                    doDiag (ach:as) (bch:bs) nw n w = me : doDiag as bs me (tail n) (tail w)
-                        where me = if ach == bch then nw else 1 + min3 (head w) nw (head n)
-                    firstelt = 1 + head diagBelow
-                    thisdiag = firstelt : doDiag a b firstelt diagAbove (tail diagBelow)
-          lab = length a - length b
-          min3 x y z = if x < y then x else min y z
-
-hasFloatingPoint params = shellType params == Ksh
-
--- Checks whether the current parent path is part of a condition
-isCondition [] = False
-isCondition [_] = False
-isCondition (child:parent:rest) =
-    getId child `elem` map getId (getConditionChildren parent) || isCondition (parent:rest)
-  where
-    getConditionChildren t =
-        case t of
-            T_AndIf _ left right -> [left]
-            T_OrIf id left right -> [left]
-            T_IfExpression id conditions elses -> concatMap (take 1 . reverse . fst) conditions
-            T_WhileExpression id c l -> take 1 . reverse $ c
-            T_UntilExpression id c l -> take 1 . reverse $ c
-            _ -> []
-
-prop_checkEchoWc3 = verify checkEchoWc "n=$(echo $foo | wc -c)"
-checkEchoWc _ (T_Pipeline id _ [a, b]) =
-    when (acmd == ["echo", "${VAR}"]) $
-        case bcmd of
-            ["wc", "-c"] -> countMsg
-            ["wc", "-m"] -> countMsg
-            _ -> return ()
-  where
-    acmd = oversimplify a
-    bcmd = oversimplify b
-    countMsg = style id 2000 "See if you can use ${#variable} instead."
-checkEchoWc _ _ = return ()
-
-prop_checkPipedAssignment1 = verify checkPipedAssignment "A=ls | grep foo"
-prop_checkPipedAssignment2 = verifyNot checkPipedAssignment "A=foo cmd | grep foo"
-prop_checkPipedAssignment3 = verifyNot checkPipedAssignment "A=foo"
-checkPipedAssignment _ (T_Pipeline _ _ (T_Redirecting _ _ (T_SimpleCommand id (_:_) []):_:_)) =
-    warn id 2036 "If you wanted to assign the output of the pipeline, use a=$(b | c) ."
-checkPipedAssignment _ _ = return ()
-
-prop_checkAssignAteCommand1 = verify checkAssignAteCommand "A=ls -l"
-prop_checkAssignAteCommand2 = verify checkAssignAteCommand "A=ls --sort=$foo"
-prop_checkAssignAteCommand3 = verify checkAssignAteCommand "A=cat foo | grep bar"
-prop_checkAssignAteCommand4 = verifyNot checkAssignAteCommand "A=foo ls -l"
-prop_checkAssignAteCommand5 = verify checkAssignAteCommand "PAGER=cat grep bar"
-prop_checkAssignAteCommand6 = verifyNot checkAssignAteCommand "PAGER=\"cat\" grep bar"
-prop_checkAssignAteCommand7 = verify checkAssignAteCommand "here=pwd"
-checkAssignAteCommand _ (T_SimpleCommand id (T_Assignment _ _ _ _ assignmentTerm:[]) list) =
-    -- Check if first word is intended as an argument (flag or glob).
-    if firstWordIsArg list
-    then
-        err id 2037 "To assign the output of a command, use var=$(cmd) ."
-    else
-        -- Check if it's a known, unquoted command name.
-        when (isCommonCommand $ getUnquotedLiteral assignmentTerm) $
-            warn id 2209 "Use var=$(command) to assign output (or quote to assign string)."
-  where
-    isCommonCommand (Just s) = s `elem` commonCommands
-    isCommonCommand _ = False
-    firstWordIsArg list = fromMaybe False $ do
-        head <- list !!! 0
-        return $ isGlob head || isUnquotedFlag head
-
-checkAssignAteCommand _ _ = return ()
-
-prop_checkArithmeticOpCommand1 = verify checkArithmeticOpCommand "i=i + 1"
-prop_checkArithmeticOpCommand2 = verify checkArithmeticOpCommand "foo=bar * 2"
-prop_checkArithmeticOpCommand3 = verifyNot checkArithmeticOpCommand "foo + opts"
-checkArithmeticOpCommand _ (T_SimpleCommand id [T_Assignment {}] (firstWord:_)) =
-    fromMaybe (return ()) $ check <$> getGlobOrLiteralString firstWord
-  where
-    check op =
-        when (op `elem` ["+", "-", "*", "/"]) $
-            warn (getId firstWord) 2099 $
-                "Use $((..)) for arithmetics, e.g. i=$((i " ++ op ++ " 2))"
-checkArithmeticOpCommand _ _ = return ()
-
-prop_checkWrongArit = verify checkWrongArithmeticAssignment "i=i+1"
-prop_checkWrongArit2 = verify checkWrongArithmeticAssignment "n=2; i=n*2"
-checkWrongArithmeticAssignment params (T_SimpleCommand id (T_Assignment _ _ _ _ val:[]) []) =
-  fromMaybe (return ()) $ do
-    str <- getNormalString val
-    match <- matchRegex regex str
-    var <- match !!! 0
-    op <- match !!! 1
-    Map.lookup var references
-    return . warn (getId val) 2100 $
-        "Use $((..)) for arithmetics, e.g. i=$((i " ++ op ++ " 2))"
-  where
-    regex = mkRegex "^([_a-zA-Z][_a-zA-Z0-9]*)([+*-]).+$"
-    references = foldl (flip ($)) Map.empty (map insertRef $ variableFlow params)
-    insertRef (Assignment (_, _, name, _)) =
-        Map.insert name ()
-    insertRef _ = Prelude.id
-
-    getNormalString (T_NormalWord _ words) = do
-        parts <- foldl (liftM2 (\x y -> x ++ [y])) (Just []) $ map getLiterals words
-        return $ concat parts
-    getNormalString _ = Nothing
-
-    getLiterals (T_Literal _ s) = return s
-    getLiterals (T_Glob _ s) = return s
-    getLiterals _ = Nothing
-checkWrongArithmeticAssignment _ _ = return ()
-
-
-prop_checkUuoc1 = verify checkUuoc "cat foo | grep bar"
-prop_checkUuoc2 = verifyNot checkUuoc "cat * | grep bar"
-prop_checkUuoc3 = verify checkUuoc "cat $var | grep bar"
-prop_checkUuoc4 = verifyNot checkUuoc "cat $var"
-prop_checkUuoc5 = verifyNot checkUuoc "cat \"$@\""
-prop_checkUuoc6 = verifyNot checkUuoc "cat -n | grep bar"
-checkUuoc _ (T_Pipeline _ _ (T_Redirecting _ _ cmd:_:_)) =
-    checkCommand "cat" (const f) cmd
-  where
-    f [word] = unless (mayBecomeMultipleArgs word || isOption word) $
-        style (getId word) 2002 "Useless cat. Consider 'cmd < file | ..' or 'cmd file | ..' instead."
-    f _ = return ()
-    isOption word = "-" `isPrefixOf` onlyLiteralString word
-checkUuoc _ _ = return ()
-
-prop_checkPipePitfalls3 = verify checkPipePitfalls "ls | grep -v mp3"
-prop_checkPipePitfalls4 = verifyNot checkPipePitfalls "find . -print0 | xargs -0 foo"
-prop_checkPipePitfalls5 = verifyNot checkPipePitfalls "ls -N | foo"
-prop_checkPipePitfalls6 = verify checkPipePitfalls "find . | xargs foo"
-prop_checkPipePitfalls7 = verifyNot checkPipePitfalls "find . -printf '%s\\n' | xargs foo"
-prop_checkPipePitfalls8 = verify checkPipePitfalls "foo | grep bar | wc -l"
-prop_checkPipePitfalls9 = verifyNot checkPipePitfalls "foo | grep -o bar | wc -l"
-prop_checkPipePitfalls10 = verifyNot checkPipePitfalls "foo | grep -o bar | wc"
-prop_checkPipePitfalls11 = verifyNot checkPipePitfalls "foo | grep bar | wc"
-prop_checkPipePitfalls12 = verifyNot checkPipePitfalls "foo | grep -o bar | wc -c"
-prop_checkPipePitfalls13 = verifyNot checkPipePitfalls "foo | grep bar | wc -c"
-prop_checkPipePitfalls14 = verifyNot checkPipePitfalls "foo | grep -o bar | wc -cmwL"
-prop_checkPipePitfalls15 = verifyNot checkPipePitfalls "foo | grep bar | wc -cmwL"
-prop_checkPipePitfalls16 = verifyNot checkPipePitfalls "foo | grep -r bar | wc -l"
-checkPipePitfalls _ (T_Pipeline id _ commands) = do
-    for ["find", "xargs"] $
-        \(find:xargs:_) ->
-          let args = oversimplify xargs ++ oversimplify find
-          in
-            unless (any ($ args) [
-                hasShortParameter '0',
-                hasParameter "null",
-                hasParameter "print0",
-                hasParameter "printf"
-              ]) $ warn (getId find) 2038
-                      "Use -print0/-0 or -exec + to allow for non-alphanumeric filenames."
-
-    for' ["ps", "grep"] $
-        \x -> info x 2009 "Consider using pgrep instead of grepping ps output."
-
-    for ["grep", "wc"] $
-        \(grep:wc:_) ->
-            let flagsGrep = fromMaybe [] $ map snd . getAllFlags <$> getCommand grep
-                flagsWc = fromMaybe [] $ map snd . getAllFlags <$> getCommand wc
-            in
-                unless (any (`elem` ["o", "only-matching", "r", "R", "recursive"]) flagsGrep || any (`elem` ["m", "chars", "w", "words", "c", "bytes", "L", "max-line-length"]) flagsWc || null flagsWc) $
-                    style (getId grep) 2126 "Consider using grep -c instead of grep|wc -l."
-
-    didLs <- fmap or . sequence $ [
-        for' ["ls", "grep"] $
-            \x -> warn x 2010 "Don't use ls | grep. Use a glob or a for loop with a condition to allow non-alphanumeric filenames.",
-        for' ["ls", "xargs"] $
-            \x -> warn x 2011 "Use 'find .. -print0 | xargs -0 ..' or 'find .. -exec .. +' to allow non-alphanumeric filenames."
-        ]
-    unless didLs $ do
-        for ["ls", "?"] $
-            \(ls:_) -> unless (hasShortParameter 'N' (oversimplify ls)) $
-                info (getId ls) 2012 "Use find instead of ls to better handle non-alphanumeric filenames."
-        return ()
-  where
-    for l f =
-        let indices = indexOfSublists l (map (headOrDefault "" . oversimplify) commands)
-        in do
-            mapM_ (f . (\ n -> take (length l) $ drop n commands)) indices
-            return . not . null $ indices
-    for' l f = for l (first f)
-    first func (x:_) = func (getId x)
-    first _ _ = return ()
-    hasShortParameter char = any (\x -> "-" `isPrefixOf` x && char `elem` x)
-    hasParameter string =
-        any (isPrefixOf string . dropWhile (== '-'))
-checkPipePitfalls _ _ = return ()
-
-indexOfSublists sub = f 0
-  where
-    f _ [] = []
-    f n a@(r:rest) =
-        let others = f (n+1) rest in
-            if match sub a
-              then n:others
-              else others
-    match ("?":r1) (_:r2) = match r1 r2
-    match (x1:r1) (x2:r2) | x1 == x2 = match r1 r2
-    match [] _ = True
-    match _ _ = False
-
-
-prop_checkShebangParameters1 = verifyTree checkShebangParameters "#!/usr/bin/env bash -x\necho cow"
-prop_checkShebangParameters2 = verifyNotTree checkShebangParameters "#! /bin/sh  -l "
-checkShebangParameters p (T_Annotation _ _ t) = checkShebangParameters p t
-checkShebangParameters _ (T_Script id sb _) =
-    [makeComment ErrorC id 2096 "On most OS, shebangs can only specify a single parameter." | length (words sb) > 2]
-
-prop_checkShebang1 = verifyNotTree checkShebang "#!/usr/bin/env bash -x\necho cow"
-prop_checkShebang2 = verifyNotTree checkShebang "#! /bin/sh  -l "
-prop_checkShebang3 = verifyTree checkShebang "ls -l"
-prop_checkShebang4 = verifyNotTree checkShebang "#shellcheck shell=sh\nfoo"
-prop_checkShebang5 = verifyTree checkShebang "#!/usr/bin/env ash"
-prop_checkShebang6 = verifyNotTree checkShebang "#!/usr/bin/env ash\n# shellcheck shell=dash\n"
-prop_checkShebang7 = verifyNotTree checkShebang "#!/usr/bin/env ash\n# shellcheck shell=sh\n"
-checkShebang params (T_Annotation _ list t) =
-    if any isOverride list then [] else checkShebang params t
-  where
-    isOverride (ShellOverride _) = True
-    isOverride _ = False
-checkShebang params (T_Script id sb _) = execWriter $
-    unless (shellTypeSpecified params) $ do
-        when (sb == "") $
-            err id 2148 "Tips depend on target shell and yours is unknown. Add a shebang."
-        when (executableFromShebang sb == "ash") $
-            warn id 2187 "Ash scripts will be checked as Dash. Add '# shellcheck shell=dash' to silence."
-
-
-prop_checkForInQuoted = verify checkForInQuoted "for f in \"$(ls)\"; do echo foo; done"
-prop_checkForInQuoted2 = verifyNot checkForInQuoted "for f in \"$@\"; do echo foo; done"
-prop_checkForInQuoted2a = verifyNot checkForInQuoted "for f in *.mp3; do echo foo; done"
-prop_checkForInQuoted2b = verify checkForInQuoted "for f in \"*.mp3\"; do echo foo; done"
-prop_checkForInQuoted3 = verify checkForInQuoted "for f in 'find /'; do true; done"
-prop_checkForInQuoted4 = verify checkForInQuoted "for f in 1,2,3; do true; done"
-prop_checkForInQuoted4a = verifyNot checkForInQuoted "for f in foo{1,2,3}; do true; done"
-prop_checkForInQuoted5 = verify checkForInQuoted "for f in ls; do true; done"
-prop_checkForInQuoted6 = verifyNot checkForInQuoted "for f in \"${!arr}\"; do true; done"
-checkForInQuoted _ (T_ForIn _ f [T_NormalWord _ [word@(T_DoubleQuoted id list)]] _) =
-    when (any (\x -> willSplit x && not (mayBecomeMultipleArgs x)) list
-            || (fmap wouldHaveBeenGlob (getLiteralString word) == Just True)) $
-        err id 2066 "Since you double quoted this, it will not word split, and the loop will only run once."
-checkForInQuoted _ (T_ForIn _ f [T_NormalWord _ [T_SingleQuoted id _]] _) =
-    warn id 2041 "This is a literal string. To run as a command, use $(..) instead of '..' . "
-checkForInQuoted _ (T_ForIn _ f [T_NormalWord _ [T_Literal id s]] _) =
-    if ',' `elem` s
-      then unless ('{' `elem` s) $
-            warn id 2042 "Use spaces, not commas, to separate loop elements."
-      else warn id 2043 "This loop will only ever run once for a constant value. Did you perhaps mean to loop over dir/*, $var or $(cmd)?"
-checkForInQuoted _ _ = return ()
-
-prop_checkForInCat1 = verify checkForInCat "for f in $(cat foo); do stuff; done"
-prop_checkForInCat1a= verify checkForInCat "for f in `cat foo`; do stuff; done"
-prop_checkForInCat2 = verify checkForInCat "for f in $(cat foo | grep lol); do stuff; done"
-prop_checkForInCat2a= verify checkForInCat "for f in `cat foo | grep lol`; do stuff; done"
-prop_checkForInCat3 = verifyNot checkForInCat "for f in $(cat foo | grep bar | wc -l); do stuff; done"
-checkForInCat _ (T_ForIn _ f [T_NormalWord _ w] _) = mapM_ checkF w
-  where
-    checkF (T_DollarExpansion id [T_Pipeline _ _ r])
-        | all isLineBased r =
-            info id 2013 "To read lines rather than words, pipe/redirect to a 'while read' loop."
-    checkF (T_Backticked id cmds) = checkF (T_DollarExpansion id cmds)
-    checkF _ = return ()
-    isLineBased cmd = any (cmd `isCommand`)
-                        ["grep", "fgrep", "egrep", "sed", "cat", "awk", "cut", "sort"]
-checkForInCat _ _ = return ()
-
-prop_checkForInLs = verify checkForInLs "for f in $(ls *.mp3); do mplayer \"$f\"; done"
-prop_checkForInLs2 = verify checkForInLs "for f in `ls *.mp3`; do mplayer \"$f\"; done"
-prop_checkForInLs3 = verify checkForInLs "for f in `find / -name '*.mp3'`; do mplayer \"$f\"; done"
-checkForInLs _ = try
-  where
-   try (T_ForIn _ f [T_NormalWord _ [T_DollarExpansion id [x]]] _) =
-        check id f x
-   try (T_ForIn _ f [T_NormalWord _ [T_Backticked id [x]]] _) =
-        check id f x
-   try _ = return ()
-   check id f x =
-    case oversimplify x of
-      ("ls":n) ->
-        let warntype = if any ("-" `isPrefixOf`) n then warn else err in
-          warntype id 2045 "Iterating over ls output is fragile. Use globs."
-      ("find":_) -> warn id 2044 "For loops over find output are fragile. Use find -exec or a while read loop."
-      _ -> return ()
-
-
-prop_checkFindExec1 = verify checkFindExec "find / -name '*.php' -exec rm {};"
-prop_checkFindExec2 = verify checkFindExec "find / -exec touch {} && ls {} \\;"
-prop_checkFindExec3 = verify checkFindExec "find / -execdir cat {} | grep lol +"
-prop_checkFindExec4 = verifyNot checkFindExec "find / -name '*.php' -exec foo {} +"
-prop_checkFindExec5 = verifyNot checkFindExec "find / -execdir bash -c 'a && b' \\;"
-prop_checkFindExec6 = verify checkFindExec "find / -type d -execdir rm *.jpg \\;"
-checkFindExec _ cmd@(T_SimpleCommand _ _ t@(h:r)) | cmd `isCommand` "find" = do
-    c <- broken r False
-    when c $
-        let wordId = getId $ last t in
-            err wordId 2067 "Missing ';' or + terminating -exec. You can't use |/||/&&, and ';' has to be a separate, quoted argument."
-
-  where
-    broken [] v = return v
-    broken (w:r) v = do
-        when v (mapM_ warnFor $ fromWord w)
-        case getLiteralString w of
-            Just "-exec" -> broken r True
-            Just "-execdir" -> broken r True
-            Just "+" -> broken r False
-            Just ";" -> broken r False
-            _ -> broken r v
-
-    shouldWarn x =
-      case x of
-        T_DollarExpansion _ _ -> True
-        T_Backticked _ _ -> True
-        T_Glob _ _ -> True
-        T_Extglob {} -> True
-        _ -> False
-
-    warnFor x =
-        when(shouldWarn x) $
-            info (getId x) 2014 "This will expand once before find runs, not per file found."
-
-    fromWord (T_NormalWord _ l) = l
-    fromWord _ = []
-checkFindExec _ _ = return ()
-
-
-prop_checkUnquotedExpansions1 = verify checkUnquotedExpansions "rm $(ls)"
-prop_checkUnquotedExpansions1a= verify checkUnquotedExpansions "rm `ls`"
-prop_checkUnquotedExpansions2 = verify checkUnquotedExpansions "rm foo$(date)"
-prop_checkUnquotedExpansions3 = verify checkUnquotedExpansions "[ $(foo) == cow ]"
-prop_checkUnquotedExpansions3a= verify checkUnquotedExpansions "[ ! $(foo) ]"
-prop_checkUnquotedExpansions4 = verifyNot checkUnquotedExpansions "[[ $(foo) == cow ]]"
-prop_checkUnquotedExpansions5 = verifyNot checkUnquotedExpansions "for f in $(cmd); do echo $f; done"
-prop_checkUnquotedExpansions6 = verifyNot checkUnquotedExpansions "$(cmd)"
-prop_checkUnquotedExpansions7 = verifyNot checkUnquotedExpansions "cat << foo\n$(ls)\nfoo"
-prop_checkUnquotedExpansions8 = verifyNot checkUnquotedExpansions "set -- $(seq 1 4)"
-prop_checkUnquotedExpansions9 = verifyNot checkUnquotedExpansions "echo foo `# inline comment`"
-checkUnquotedExpansions params =
-    check
-  where
-    check t@(T_DollarExpansion _ c) = examine t c
-    check t@(T_Backticked _ c) = examine t c
-    check t@(T_DollarBraceCommandExpansion _ c) = examine t c
-    check _ = return ()
-    tree = parentMap params
-    examine t contents =
-        unless (null contents || shouldBeSplit t || isQuoteFree tree t || usedAsCommandName tree t) $
-            warn (getId t) 2046 "Quote this to prevent word splitting."
-
-    shouldBeSplit t =
-        getCommandNameFromExpansion t == Just "seq"
-
-
-prop_checkRedirectToSame = verify checkRedirectToSame "cat foo > foo"
-prop_checkRedirectToSame2 = verify checkRedirectToSame "cat lol | sed -e 's/a/b/g' > lol"
-prop_checkRedirectToSame3 = verifyNot checkRedirectToSame "cat lol | sed -e 's/a/b/g' > foo.bar && mv foo.bar lol"
-prop_checkRedirectToSame4 = verifyNot checkRedirectToSame "foo /dev/null > /dev/null"
-prop_checkRedirectToSame5 = verifyNot checkRedirectToSame "foo > bar 2> bar"
-prop_checkRedirectToSame6 = verifyNot checkRedirectToSame "echo foo > foo"
-prop_checkRedirectToSame7 = verifyNot checkRedirectToSame "sed 's/foo/bar/g' file | sponge file"
-checkRedirectToSame params s@(T_Pipeline _ _ list) =
-    mapM_ (\l -> (mapM_ (\x -> doAnalysis (checkOccurrences x) l) (getAllRedirs list))) list
-  where
-    note x = makeComment InfoC x 2094
-                "Make sure not to read and write the same file in the same pipeline."
-    checkOccurrences t@(T_NormalWord exceptId x) u@(T_NormalWord newId y) =
-        when (exceptId /= newId
-                && x == y
-                && not (isOutput t && isOutput u)
-                && not (special t)
-                && not (any isHarmlessCommand [t,u])) $ do
-            addComment $ note newId
-            addComment $ note exceptId
-    checkOccurrences _ _ = return ()
-    getAllRedirs = concatMap (\t ->
-        case t of
-            T_Redirecting _ ls _ -> concatMap getRedirs ls
-            _ -> [])
-    getRedirs (T_FdRedirect _ _ (T_IoFile _ op file)) =
-            case op of T_Greater _ -> [file]
-                       T_Less _    -> [file]
-                       T_DGREAT _  -> [file]
-                       _ -> []
-    getRedirs _ = []
-    special x = "/dev/" `isPrefixOf` concat (oversimplify x)
-    isOutput t =
-        case drop 1 $ getPath (parentMap params) t of
-            T_IoFile _ op _:_ ->
-                case op of
-                    T_Greater _  -> True
-                    T_DGREAT _ -> True
-                    _ -> False
-            _ -> False
-    isHarmlessCommand arg = fromMaybe False $ do
-        cmd <- getClosestCommand (parentMap params) arg
-        name <- getCommandBasename cmd
-        return $ name `elem` ["echo", "printf", "sponge"]
-
-checkRedirectToSame _ _ = return ()
-
-
-prop_checkShorthandIf  = verify checkShorthandIf "[[ ! -z file ]] && scp file host || rm file"
-prop_checkShorthandIf2 = verifyNot checkShorthandIf "[[ ! -z file ]] && { scp file host || echo 'Eek'; }"
-prop_checkShorthandIf3 = verifyNot checkShorthandIf "foo && bar || echo baz"
-prop_checkShorthandIf4 = verifyNot checkShorthandIf "foo && a=b || a=c"
-prop_checkShorthandIf5 = verifyNot checkShorthandIf "foo && rm || printf b"
-prop_checkShorthandIf6 = verifyNot checkShorthandIf "if foo && bar || baz; then true; fi"
-prop_checkShorthandIf7 = verifyNot checkShorthandIf "while foo && bar || baz; do true; done"
-prop_checkShorthandIf8 = verify checkShorthandIf "if true; then foo && bar || baz; fi"
-checkShorthandIf params x@(T_AndIf id _ (T_OrIf _ _ (T_Pipeline _ _ t)))
-        | not (isOk t || inCondition) =
-    info id 2015 "Note that A && B || C is not if-then-else. C may run when A is true."
-  where
-    isOk [t] = isAssignment t || fromMaybe False (do
-        name <- getCommandBasename t
-        return $ name `elem` ["echo", "exit", "return", "printf"])
-    isOk _ = False
-    inCondition = isCondition $ getPath (parentMap params) x
-checkShorthandIf _ _ = return ()
-
-
-prop_checkDollarStar = verify checkDollarStar "for f in $*; do ..; done"
-prop_checkDollarStar2 = verifyNot checkDollarStar "a=$*"
-prop_checkDollarStar3 = verifyNot checkDollarStar "[[ $* = 'a b' ]]"
-checkDollarStar p t@(T_NormalWord _ [b@(T_DollarBraced id _)])
-      | bracedString b == "*"  =
-    unless (isStrictlyQuoteFree (parentMap p) t) $
-        warn id 2048 "Use \"$@\" (with quotes) to prevent whitespace problems."
-checkDollarStar _ _ = return ()
-
-
-prop_checkUnquotedDollarAt = verify checkUnquotedDollarAt "ls $@"
-prop_checkUnquotedDollarAt1= verifyNot checkUnquotedDollarAt "ls ${#@}"
-prop_checkUnquotedDollarAt2 = verify checkUnquotedDollarAt "ls ${foo[@]}"
-prop_checkUnquotedDollarAt3 = verifyNot checkUnquotedDollarAt "ls ${#foo[@]}"
-prop_checkUnquotedDollarAt4 = verifyNot checkUnquotedDollarAt "ls \"$@\""
-prop_checkUnquotedDollarAt5 = verifyNot checkUnquotedDollarAt "ls ${foo/@/ at }"
-prop_checkUnquotedDollarAt6 = verifyNot checkUnquotedDollarAt "a=$@"
-prop_checkUnquotedDollarAt7 = verify checkUnquotedDollarAt "for f in ${var[@]}; do true; done"
-prop_checkUnquotedDollarAt8 = verifyNot checkUnquotedDollarAt "echo \"${args[@]:+${args[@]}}\""
-prop_checkUnquotedDollarAt9 = verifyNot checkUnquotedDollarAt "echo ${args[@]:+\"${args[@]}\"}"
-prop_checkUnquotedDollarAt10 = verifyNot checkUnquotedDollarAt "echo ${@+\"$@\"}"
-checkUnquotedDollarAt p word@(T_NormalWord _ parts) | not $ isStrictlyQuoteFree (parentMap p) word =
-    forM_ (take 1 $ filter isArrayExpansion parts) $ \x ->
-        unless (isQuotedAlternativeReference x) $
-            err (getId x) 2068
-                "Double quote array expansions to avoid re-splitting elements."
-checkUnquotedDollarAt _ _ = return ()
-
-prop_checkConcatenatedDollarAt1 = verify checkConcatenatedDollarAt "echo \"foo$@\""
-prop_checkConcatenatedDollarAt2 = verify checkConcatenatedDollarAt "echo ${arr[@]}lol"
-prop_checkConcatenatedDollarAt3 = verify checkConcatenatedDollarAt "echo $a$@"
-prop_checkConcatenatedDollarAt4 = verifyNot checkConcatenatedDollarAt "echo $@"
-prop_checkConcatenatedDollarAt5 = verifyNot checkConcatenatedDollarAt "echo \"${arr[@]}\""
-checkConcatenatedDollarAt p word@T_NormalWord {}
-    | not $ isQuoteFree (parentMap p) word =
-        unless (null $ drop 1 parts) $
-            mapM_ for array
-  where
-    parts = getWordParts word
-    array = take 1 $ filter isArrayExpansion parts
-    for t = err (getId t) 2145 "Argument mixes string and array. Use * or separate argument."
-checkConcatenatedDollarAt _ _ = return ()
-
-prop_checkArrayAsString1 = verify checkArrayAsString "a=$@"
-prop_checkArrayAsString2 = verify checkArrayAsString "a=\"${arr[@]}\""
-prop_checkArrayAsString3 = verify checkArrayAsString "a=*.png"
-prop_checkArrayAsString4 = verify checkArrayAsString "a={1..10}"
-prop_checkArrayAsString5 = verifyNot checkArrayAsString "a='*.gif'"
-prop_checkArrayAsString6 = verifyNot checkArrayAsString "a=$*"
-prop_checkArrayAsString7 = verifyNot checkArrayAsString "a=( $@ )"
-checkArrayAsString _ (T_Assignment id _ _ _ word) =
-    if willConcatInAssignment word
-    then
-      warn (getId word) 2124
-        "Assigning an array to a string! Assign as array, or use * instead of @ to concatenate."
-    else
-      when (willBecomeMultipleArgs word) $
-        warn (getId word) 2125
-          "Brace expansions and globs are literal in assignments. Quote it or use an array."
-checkArrayAsString _ _ = return ()
-
-prop_checkArrayWithoutIndex1 = verifyTree checkArrayWithoutIndex "foo=(a b); echo $foo"
-prop_checkArrayWithoutIndex2 = verifyNotTree checkArrayWithoutIndex "foo='bar baz'; foo=($foo); echo ${foo[0]}"
-prop_checkArrayWithoutIndex3 = verifyTree checkArrayWithoutIndex "coproc foo while true; do echo cow; done; echo $foo"
-prop_checkArrayWithoutIndex4 = verifyTree checkArrayWithoutIndex "coproc tail -f log; echo $COPROC"
-prop_checkArrayWithoutIndex5 = verifyTree checkArrayWithoutIndex "a[0]=foo; echo $a"
-prop_checkArrayWithoutIndex6 = verifyTree checkArrayWithoutIndex "echo $PIPESTATUS"
-prop_checkArrayWithoutIndex7 = verifyTree checkArrayWithoutIndex "a=(a b); a+=c"
-prop_checkArrayWithoutIndex8 = verifyTree checkArrayWithoutIndex "declare -a foo; foo=bar;"
-checkArrayWithoutIndex params _ =
-    doVariableFlowAnalysis readF writeF defaultMap (variableFlow params)
-  where
-    defaultMap = Map.fromList $ map (\x -> (x,())) arrayVariables
-    readF _ (T_DollarBraced id token) _ = do
-        map <- get
-        return . maybeToList $ do
-            name <- getLiteralString token
-            assigned <- Map.lookup name map
-            return $ makeComment WarningC id 2128
-                    "Expanding an array without an index only gives the first element."
-    readF _ _ _ = return []
-
-    writeF _ (T_Assignment id mode name [] _) _ (DataString _) = do
-        isArray <- gets (isJust . Map.lookup name)
-        return $ if not isArray then [] else
-            case mode of
-                Assign -> [makeComment WarningC id 2178 "Variable was used as an array but is now assigned a string."]
-                Append -> [makeComment WarningC id 2179 "Use array+=(\"item\") to append items to an array."]
-
-    writeF _ t name (DataArray _) = do
-        modify (Map.insert name ())
-        return []
-    writeF _ expr name _ = do
-        if isIndexed expr
-          then modify (Map.insert name ())
-          else modify (Map.delete name)
-        return []
-
-    isIndexed expr =
-        case expr of
-            T_Assignment _ _ _ (_:_) _ -> True
-            _ -> False
-
-prop_checkStderrRedirect = verify checkStderrRedirect "test 2>&1 > cow"
-prop_checkStderrRedirect2 = verifyNot checkStderrRedirect "test > cow 2>&1"
-prop_checkStderrRedirect3 = verifyNot checkStderrRedirect "test 2>&1 > file | grep stderr"
-prop_checkStderrRedirect4 = verifyNot checkStderrRedirect "errors=$(test 2>&1 > file)"
-prop_checkStderrRedirect5 = verifyNot checkStderrRedirect "read < <(test 2>&1 > file)"
-prop_checkStderrRedirect6 = verify checkStderrRedirect "foo | bar 2>&1 > /dev/null"
-checkStderrRedirect params redir@(T_Redirecting _ [
-    T_FdRedirect id "2" (T_IoDuplicate _ (T_GREATAND _) "1"),
-    T_FdRedirect _ _ (T_IoFile _ op _)
-    ] _) = case op of
-            T_Greater _ -> error
-            T_DGREAT _ -> error
-            _ -> return ()
-  where
-    usesOutput t =
-        case t of
-            (T_Pipeline _ _ list) -> length list > 1 && not (isParentOf (parentMap params) (last list) redir)
-            T_ProcSub {} -> True
-            T_DollarExpansion {} -> True
-            T_Backticked {} -> True
-            _ -> False
-    isCaptured = any usesOutput $ getPath (parentMap params) redir
-
-    error = unless isCaptured $
-        err id 2069 "The order of the 2>&1 and the redirect matters. The 2>&1 has to be last."
-
-checkStderrRedirect _ _ = return ()
-
-lt x = trace ("Tracing " ++ show x) x
-ltt t = trace ("Tracing " ++ show t)
-
-
-prop_checkSingleQuotedVariables  = verify checkSingleQuotedVariables "echo '$foo'"
-prop_checkSingleQuotedVariables2 = verify checkSingleQuotedVariables "echo 'lol$1.jpg'"
-prop_checkSingleQuotedVariables3 = verifyNot checkSingleQuotedVariables "sed 's/foo$/bar/'"
-prop_checkSingleQuotedVariables3a= verify checkSingleQuotedVariables "sed 's/${foo}/bar/'"
-prop_checkSingleQuotedVariables3b= verify checkSingleQuotedVariables "sed 's/$(echo cow)/bar/'"
-prop_checkSingleQuotedVariables3c= verify checkSingleQuotedVariables "sed 's/$((1+foo))/bar/'"
-prop_checkSingleQuotedVariables4 = verifyNot checkSingleQuotedVariables "awk '{print $1}'"
-prop_checkSingleQuotedVariables5 = verifyNot checkSingleQuotedVariables "trap 'echo $SECONDS' EXIT"
-prop_checkSingleQuotedVariables6 = verifyNot checkSingleQuotedVariables "sed -n '$p'"
-prop_checkSingleQuotedVariables6a= verify checkSingleQuotedVariables "sed -n '$pattern'"
-prop_checkSingleQuotedVariables7 = verifyNot checkSingleQuotedVariables "PS1='$PWD \\$ '"
-prop_checkSingleQuotedVariables8 = verify checkSingleQuotedVariables "find . -exec echo '$1' {} +"
-prop_checkSingleQuotedVariables9 = verifyNot checkSingleQuotedVariables "find . -exec awk '{print $1}' {} \\;"
-prop_checkSingleQuotedVariables10= verify checkSingleQuotedVariables "echo '`pwd`'"
-prop_checkSingleQuotedVariables11= verifyNot checkSingleQuotedVariables "sed '${/lol/d}'"
-prop_checkSingleQuotedVariables12= verifyNot checkSingleQuotedVariables "eval 'echo $1'"
-prop_checkSingleQuotedVariables13= verifyNot checkSingleQuotedVariables "busybox awk '{print $1}'"
-prop_checkSingleQuotedVariables14= verifyNot checkSingleQuotedVariables "[ -v 'bar[$foo]' ]"
-checkSingleQuotedVariables params t@(T_SingleQuoted id s) =
-    when (s `matches` re) $
-        if "sed" == commandName
-        then unless (s `matches` sedContra) showMessage
-        else unless isProbablyOk showMessage
-  where
-    parents = parentMap params
-    showMessage = info id 2016
-        "Expressions don't expand in single quotes, use double quotes for that."
-    commandName = fromMaybe "" $ do
-        cmd <- getClosestCommand parents t
-        name <- getCommandBasename cmd
-        return $ if name == "find" then getFindCommand cmd else name
-
-    isProbablyOk =
-            any isOkAssignment (take 3 $ getPath parents t)
-            || commandName `elem` [
-                "trap"
-                ,"sh"
-                ,"bash"
-                ,"ksh"
-                ,"zsh"
-                ,"ssh"
-                ,"eval"
-                ,"xprop"
-                ,"alias"
-                ,"sudo" -- covering "sudo sh" and such
-                ,"dpkg-query"
-                ,"jq"  -- could also check that user provides --arg
-                ]
-            || "awk" `isSuffixOf` commandName
-            || "perl" `isPrefixOf` commandName
-
-    commonlyQuoted = ["PS1", "PS2", "PS3", "PS4", "PROMPT_COMMAND"]
-    isOkAssignment t =
-        case t of
-            T_Assignment _ _ name _ _ -> name `elem` commonlyQuoted
-            TC_Unary _ _ "-v" _ -> True
-            _ -> False
-
-    re = mkRegex "\\$[{(0-9a-zA-Z_]|`.*`"
-    sedContra = mkRegex "\\$[{dpsaic]($|[^a-zA-Z])"
-
-    getFindCommand (T_SimpleCommand _ _ words) =
-        let list = map getLiteralString words
-            cmd  = dropWhile (\x -> x /= Just "-exec" && x /= Just "-execdir") list
-        in
-          case cmd of
-            (flag:cmd:rest) -> fromMaybe "find" cmd
-            _ -> "find"
-    getFindCommand (T_Redirecting _ _ cmd) = getFindCommand cmd
-    getFindCommand _ = "find"
-checkSingleQuotedVariables _ _ = return ()
-
-
-prop_checkUnquotedN = verify checkUnquotedN "if [ -n $foo ]; then echo cow; fi"
-prop_checkUnquotedN2 = verify checkUnquotedN "[ -n $cow ]"
-prop_checkUnquotedN3 = verifyNot checkUnquotedN "[[ -n $foo ]] && echo cow"
-prop_checkUnquotedN4 = verify checkUnquotedN "[ -n $cow -o -t 1 ]"
-checkUnquotedN _ (TC_Unary _ SingleBracket "-n" (T_NormalWord id [t])) | willSplit t =
-       err id 2070 "-n doesn't work with unquoted arguments. Quote or use [[ ]]."
-checkUnquotedN _ _ = return ()
-
-prop_checkNumberComparisons1 = verify checkNumberComparisons "[[ $foo < 3 ]]"
-prop_checkNumberComparisons2 = verify checkNumberComparisons "[[ 0 >= $(cmd) ]]"
-prop_checkNumberComparisons3 = verifyNot checkNumberComparisons "[[ $foo ]] > 3"
-prop_checkNumberComparisons4 = verify checkNumberComparisons "[[ $foo > 2.72 ]]"
-prop_checkNumberComparisons5 = verify checkNumberComparisons "[[ $foo -le 2.72 ]]"
-prop_checkNumberComparisons6 = verify checkNumberComparisons "[[ 3.14 -eq $foo ]]"
-prop_checkNumberComparisons7 = verifyNot checkNumberComparisons "[[ 3.14 == $foo ]]"
-prop_checkNumberComparisons8 = verify checkNumberComparisons "[ foo <= bar ]"
-prop_checkNumberComparisons9 = verify checkNumberComparisons "[ foo \\>= bar ]"
-prop_checkNumberComparisons11 = verify checkNumberComparisons "[ $foo -eq 'N' ]"
-prop_checkNumberComparisons12 = verify checkNumberComparisons "[ x$foo -gt x${N} ]"
-prop_checkNumberComparisons13 = verify checkNumberComparisons "[ $foo > $bar ]"
-prop_checkNumberComparisons14 = verifyNot checkNumberComparisons "[[ foo < bar ]]"
-prop_checkNumberComparisons15 = verifyNot checkNumberComparisons "[ $foo '>' $bar ]"
-checkNumberComparisons params (TC_Binary id typ op lhs rhs) = do
-    if isNum lhs || isNum rhs
-      then do
-        when (isLtGt op) $
-          err id 2071 $
-            op ++ " is for string comparisons. Use " ++ eqv op ++ " instead."
-        when (isLeGe op && hasStringComparison) $
-            err id 2071 $ op ++ " is not a valid operator. " ++
-              "Use " ++ eqv op ++ " ."
-      else do
-        when (isLeGe op || isLtGt op) $
-            mapM_ checkDecimals [lhs, rhs]
-
-        when (isLeGe op && hasStringComparison) $
-            err id 2122 $ op ++ " is not a valid operator. " ++
-                "Use '! a " ++ esc ++ invert op ++ " b' instead."
-
-        when (typ == SingleBracket && op `elem` ["<", ">"]) $
-            case shellType params of
-                Sh -> return ()  -- These are unsupported and will be caught by bashism checks.
-                Dash -> err id 2073 $ "Escape \\" ++ op ++ " to prevent it redirecting."
-                _ -> err id 2073 $ "Escape \\" ++ op ++ " to prevent it redirecting (or switch to [[ .. ]])."
-
-    when (op `elem` ["-lt", "-gt", "-le", "-ge", "-eq"]) $ do
-        mapM_ checkDecimals [lhs, rhs]
-        when (typ == SingleBracket) $
-            checkStrings [lhs, rhs]
-
-  where
-      hasStringComparison = shellType params /= Sh
-      isLtGt = flip elem ["<", "\\<", ">", "\\>"]
-      isLeGe = flip elem ["<=", "\\<=", ">=", "\\>="]
-
-      checkDecimals hs =
-        when (isFraction hs && not (hasFloatingPoint params)) $
-            err (getId hs) 2072 decimalError
-      decimalError = "Decimals are not supported. " ++
-        "Either use integers only, or use bc or awk to compare."
-
-      checkStrings =
-        mapM_ stringError . take 1 . filter isNonNum
-
-      isNonNum t = fromMaybe False $ do
-        s <- getLiteralStringExt (const $ return "") t
-        return . not . all numChar $ s
-      numChar x = isDigit x || x `elem` "+-. "
-
-      stringError t = err (getId t) 2170 $
-          "Numerical " ++ op ++ " does not dereference in [..]. Expand or use string operator."
-
-      isNum t =
-        case oversimplify t of
-            [v] -> all isDigit v
-            _ -> False
-      isFraction t =
-        case oversimplify t of
-            [v] -> isJust $ matchRegex floatRegex v
-            _ -> False
-
-      eqv ('\\':s) = eqv s
-      eqv "<" = "-lt"
-      eqv ">" = "-gt"
-      eqv "<=" = "-le"
-      eqv ">=" = "-ge"
-      eqv _ = "the numerical equivalent"
-
-      esc = if typ == SingleBracket then "\\" else ""
-      seqv "-ge" = "! a " ++ esc ++ "< b"
-      seqv "-gt" = esc ++ ">"
-      seqv "-le" = "! a " ++ esc ++ "> b"
-      seqv "-lt" = esc ++ "<"
-      seqv "-eq" = "="
-      seqv "-ne" = "!="
-      seqv _ = "the string equivalent"
-
-      invert ('\\':s) = invert s
-      invert "<=" = ">"
-      invert ">=" = "<"
-
-      floatRegex = mkRegex "^[-+]?[0-9]+\\.[0-9]+$"
-checkNumberComparisons _ _ = return ()
-
-prop_checkSingleBracketOperators1 = verify checkSingleBracketOperators "[ test =~ foo ]"
-checkSingleBracketOperators params (TC_Binary id SingleBracket "=~" lhs rhs) =
-    when (shellType params `elem` [Bash, Ksh]) $
-        err id 2074 $ "Can't use =~ in [ ]. Use [[..]] instead."
-checkSingleBracketOperators _ _ = return ()
-
-prop_checkDoubleBracketOperators1 = verify checkDoubleBracketOperators "[[ 3 \\< 4 ]]"
-prop_checkDoubleBracketOperators3 = verifyNot checkDoubleBracketOperators "[[ foo < bar ]]"
-checkDoubleBracketOperators _ x@(TC_Binary id typ op lhs rhs)
-    | typ == DoubleBracket && op `elem` ["\\<", "\\>"] =
-        err id 2075 $ "Escaping " ++ op ++" is required in [..], but invalid in [[..]]"
-checkDoubleBracketOperators _ _ = return ()
-
-prop_checkConditionalAndOrs1 = verify checkConditionalAndOrs "[ foo && bar ]"
-prop_checkConditionalAndOrs2 = verify checkConditionalAndOrs "[[ foo -o bar ]]"
-prop_checkConditionalAndOrs3 = verifyNot checkConditionalAndOrs "[[ foo || bar ]]"
-prop_checkConditionalAndOrs4 = verify checkConditionalAndOrs "[ foo -a bar ]"
-prop_checkConditionalAndOrs5 = verify checkConditionalAndOrs "[ -z 3 -o a = b ]"
-checkConditionalAndOrs _ t =
-    case t of
-        (TC_And id SingleBracket "&&" _ _) ->
-            err id 2107 "Instead of [ a && b ], use [ a ] && [ b ]."
-        (TC_And id DoubleBracket "-a" _ _) ->
-            err id 2108 "In [[..]], use && instead of -a."
-        (TC_Or id SingleBracket "||" _ _) ->
-            err id 2109 "Instead of [ a || b ], use [ a ] || [ b ]."
-        (TC_Or id DoubleBracket "-o" _ _) ->
-            err id 2110 "In [[..]], use || instead of -o."
-
-        (TC_And id SingleBracket "-a" _ _) ->
-            warn id 2166 "Prefer [ p ] && [ q ] as [ p -a q ] is not well defined."
-        (TC_Or id SingleBracket "-o" _ _) ->
-            warn id 2166 "Prefer [ p ] || [ q ] as [ p -o q ] is not well defined."
-
-        _ -> return ()
-
-prop_checkQuotedCondRegex1 = verify checkQuotedCondRegex "[[ $foo =~ \"bar.*\" ]]"
-prop_checkQuotedCondRegex2 = verify checkQuotedCondRegex "[[ $foo =~ '(cow|bar)' ]]"
-prop_checkQuotedCondRegex3 = verifyNot checkQuotedCondRegex "[[ $foo =~ $foo ]]"
-prop_checkQuotedCondRegex4 = verifyNot checkQuotedCondRegex "[[ $foo =~ \"bar\" ]]"
-prop_checkQuotedCondRegex5 = verifyNot checkQuotedCondRegex "[[ $foo =~ 'cow bar' ]]"
-prop_checkQuotedCondRegex6 = verify checkQuotedCondRegex "[[ $foo =~ 'cow|bar' ]]"
-checkQuotedCondRegex _ (TC_Binary _ _ "=~" _ rhs) =
-    case rhs of
-        T_NormalWord id [T_DoubleQuoted _ _] -> error rhs
-        T_NormalWord id [T_SingleQuoted _ _] -> error rhs
-        _ -> return ()
-  where
-    error t =
-        unless (isConstantNonRe t) $
-            err (getId t) 2076
-                "Don't quote rhs of =~, it'll match literally rather than as a regex."
-    re = mkRegex "[][*.+()|]"
-    hasMetachars s = s `matches` re
-    isConstantNonRe t = fromMaybe False $ do
-        s <- getLiteralString t
-        return . not $ hasMetachars s
-checkQuotedCondRegex _ _ = return ()
-
-prop_checkGlobbedRegex1 = verify checkGlobbedRegex "[[ $foo =~ *foo* ]]"
-prop_checkGlobbedRegex2 = verify checkGlobbedRegex "[[ $foo =~ f* ]]"
-prop_checkGlobbedRegex2a = verify checkGlobbedRegex "[[ $foo =~ \\#* ]]"
-prop_checkGlobbedRegex3 = verifyNot checkGlobbedRegex "[[ $foo =~ $foo ]]"
-prop_checkGlobbedRegex4 = verifyNot checkGlobbedRegex "[[ $foo =~ ^c.* ]]"
-checkGlobbedRegex _ (TC_Binary _ DoubleBracket "=~" _ rhs) =
-    let s = concat $ oversimplify rhs in
-        when (isConfusedGlobRegex s) $
-            warn (getId rhs) 2049 "=~ is for regex. Use == for globs."
-checkGlobbedRegex _ _ = return ()
-
-
-prop_checkConstantIfs1 = verify checkConstantIfs "[[ foo != bar ]]"
-prop_checkConstantIfs2a= verify checkConstantIfs "[ n -le 4 ]"
-prop_checkConstantIfs2b= verifyNot checkConstantIfs "[[ n -le 4 ]]"
-prop_checkConstantIfs3 = verify checkConstantIfs "[[ $n -le 4 && n != 2 ]]"
-prop_checkConstantIfs4 = verifyNot checkConstantIfs "[[ $n -le 3 ]]"
-prop_checkConstantIfs5 = verifyNot checkConstantIfs "[[ $n -le $n ]]"
-prop_checkConstantIfs6 = verifyNot checkConstantIfs "[[ a -ot b ]]"
-prop_checkConstantIfs7 = verifyNot checkConstantIfs "[ a -nt b ]"
-prop_checkConstantIfs8 = verifyNot checkConstantIfs "[[ ~foo == '~foo' ]]"
-prop_checkConstantIfs9 = verify checkConstantIfs "[[ *.png == [a-z] ]]"
-checkConstantIfs _ (TC_Binary id typ op lhs rhs) | not isDynamic =
-    if isConstant lhs && isConstant rhs
-        then  warn id 2050 "This expression is constant. Did you forget the $ on a variable?"
-        else checkUnmatchable id op lhs rhs
-  where
-    isDynamic =
-        op `elem` [ "-lt", "-gt", "-le", "-ge", "-eq", "-ne" ]
-            && typ == DoubleBracket
-        || op `elem` [ "-nt", "-ot", "-ef"]
-
-    checkUnmatchable id op lhs rhs =
-        when (op `elem` ["=", "==", "!="] && not (wordsCanBeEqual lhs rhs)) $
-            warn id 2193 "The arguments to this comparison can never be equal. Make sure your syntax is correct."
-checkConstantIfs _ _ = return ()
-
-prop_checkLiteralBreakingTest = verify checkLiteralBreakingTest "[[ a==$foo ]]"
-prop_checkLiteralBreakingTest2 = verify checkLiteralBreakingTest "[ $foo=3 ]"
-prop_checkLiteralBreakingTest3 = verify checkLiteralBreakingTest "[ $foo!=3 ]"
-prop_checkLiteralBreakingTest4 = verify checkLiteralBreakingTest "[ \"$(ls) \" ]"
-prop_checkLiteralBreakingTest5 = verify checkLiteralBreakingTest "[ -n \"$(true) \" ]"
-prop_checkLiteralBreakingTest6 = verify checkLiteralBreakingTest "[ -z $(true)z ]"
-prop_checkLiteralBreakingTest7 = verifyNot checkLiteralBreakingTest "[ -z $(true) ]"
-prop_checkLiteralBreakingTest8 = verifyNot checkLiteralBreakingTest "[ $(true)$(true) ]"
-prop_checkLiteralBreakingTest10 = verify checkLiteralBreakingTest "[ -z foo ]"
-checkLiteralBreakingTest _ t = potentially $
-        case t of
-            (TC_Nullary _ _ w@(T_NormalWord _ l)) -> do
-                guard . not $ isConstant w -- Covered by SC2078
-                comparisonWarning l `mplus` tautologyWarning w "Argument to implicit -n is always true due to literal strings."
-            (TC_Unary _ _ op w@(T_NormalWord _ l)) ->
-                case op of
-                    "-n" -> tautologyWarning w "Argument to -n is always true due to literal strings."
-                    "-z" -> tautologyWarning w "Argument to -z is always false due to literal strings."
-                    _ -> fail "not relevant"
-            _ -> fail "not my problem"
-  where
-    hasEquals = matchToken ('=' `elem`)
-    isNonEmpty = matchToken (not . null)
-    matchToken m t = isJust $ do
-        str <- getLiteralString t
-        guard $ m str
-        return ()
-
-    comparisonWarning list = do
-        token <- listToMaybe $ filter hasEquals list
-        return $ err (getId token) 2077 "You need spaces around the comparison operator."
-    tautologyWarning t s = do
-        token <- listToMaybe $ filter isNonEmpty $ getWordParts t
-        return $ err (getId token) 2157 s
-
-prop_checkConstantNullary = verify checkConstantNullary "[[ '$(foo)' ]]"
-prop_checkConstantNullary2 = verify checkConstantNullary "[ \"-f lol\" ]"
-prop_checkConstantNullary3 = verify checkConstantNullary "[[ cmd ]]"
-prop_checkConstantNullary4 = verify checkConstantNullary "[[ ! cmd ]]"
-prop_checkConstantNullary5 = verify checkConstantNullary "[[ true ]]"
-prop_checkConstantNullary6 = verify checkConstantNullary "[ 1 ]"
-prop_checkConstantNullary7 = verify checkConstantNullary "[ false ]"
-checkConstantNullary _ (TC_Nullary _ _ t) | isConstant t =
-    case fromMaybe "" $ getLiteralString t of
-        "false" -> err (getId t) 2158 "[ false ] is true. Remove the brackets."
-        "0" -> err (getId t) 2159 "[ 0 ] is true. Use 'false' instead."
-        "true" -> style (getId t) 2160 "Instead of '[ true ]', just use 'true'."
-        "1" -> style (getId t) 2161 "Instead of '[ 1 ]', use 'true'."
-        _ -> err (getId t) 2078 "This expression is constant. Did you forget a $ somewhere?"
-  where
-    string = fromMaybe "" $ getLiteralString t
-
-checkConstantNullary _ _ = return ()
-
-prop_checkForDecimals1 = verify checkForDecimals "((3.14*c))"
-prop_checkForDecimals2 = verify checkForDecimals "foo[1.2]=bar"
-prop_checkForDecimals3 = verifyNot checkForDecimals "declare -A foo; foo[1.2]=bar"
-checkForDecimals params t@(TA_Expansion id _) = potentially $ do
-    guard $ not (hasFloatingPoint params)
-    str <- getLiteralString t
-    first <- str !!! 0
-    guard $ isDigit first && '.' `elem` str
-    return $ err id 2079 "(( )) doesn't support decimals. Use bc or awk."
-checkForDecimals _ _ = return ()
-
-prop_checkDivBeforeMult = verify checkDivBeforeMult "echo $((c/n*100))"
-prop_checkDivBeforeMult2 = verifyNot checkDivBeforeMult "echo $((c*100/n))"
-prop_checkDivBeforeMult3 = verifyNot checkDivBeforeMult "echo $((c/10*10))"
-checkDivBeforeMult params (TA_Binary _ "*" (TA_Binary id "/" _ x) y)
-    | not (hasFloatingPoint params) && x /= y =
-        info id 2017 "Increase precision by replacing a/b*c with a*c/b."
-checkDivBeforeMult _ _ = return ()
-
-prop_checkArithmeticDeref = verify checkArithmeticDeref "echo $((3+$foo))"
-prop_checkArithmeticDeref2 = verify checkArithmeticDeref "cow=14; (( s+= $cow ))"
-prop_checkArithmeticDeref3 = verifyNot checkArithmeticDeref "cow=1/40; (( s+= ${cow%%/*} ))"
-prop_checkArithmeticDeref4 = verifyNot checkArithmeticDeref "(( ! $? ))"
-prop_checkArithmeticDeref5 = verifyNot checkArithmeticDeref "(($1))"
-prop_checkArithmeticDeref6 = verify checkArithmeticDeref "(( a[$i] ))"
-prop_checkArithmeticDeref7 = verifyNot checkArithmeticDeref "(( 10#$n ))"
-prop_checkArithmeticDeref8 = verifyNot checkArithmeticDeref "let i=$i+1"
-prop_checkArithmeticDeref9 = verifyNot checkArithmeticDeref "(( a[foo] ))"
-prop_checkArithmeticDeref10= verifyNot checkArithmeticDeref "(( a[\\$foo] ))"
-prop_checkArithmeticDeref11= verifyNot checkArithmeticDeref "a[$foo]=wee"
-prop_checkArithmeticDeref12= verify checkArithmeticDeref "for ((i=0; $i < 3; i)); do true; done"
-prop_checkArithmeticDeref13= verifyNot checkArithmeticDeref "(( $$ ))"
-prop_checkArithmeticDeref14= verifyNot checkArithmeticDeref "(( $! ))"
-prop_checkArithmeticDeref15= verifyNot checkArithmeticDeref "(( ${!var} ))"
-checkArithmeticDeref params t@(TA_Expansion _ [b@(T_DollarBraced id _)]) =
-    unless (isException $ bracedString b) getWarning
-  where
-    isException [] = True
-    isException s = any (`elem` "/.:#%?*@$-!") s || isDigit (head s)
-    getWarning = fromMaybe noWarning . msum . map warningFor $ parents params t
-    warningFor t =
-        case t of
-            T_Arithmetic {} -> return normalWarning
-            T_DollarArithmetic {} -> return normalWarning
-            T_ForArithmetic {} -> return normalWarning
-            TA_Index {} -> return indexWarning
-            T_SimpleCommand {} -> return noWarning
-            _ -> Nothing
-
-    normalWarning = style id 2004 "$/${} is unnecessary on arithmetic variables."
-    indexWarning = style id 2149 "Remove $/${} for numeric index, or escape it for string."
-    noWarning = return ()
-checkArithmeticDeref _ _ = return ()
-
-prop_checkArithmeticBadOctal1 = verify checkArithmeticBadOctal "(( 0192 ))"
-prop_checkArithmeticBadOctal2 = verifyNot checkArithmeticBadOctal "(( 0x192 ))"
-prop_checkArithmeticBadOctal3 = verifyNot checkArithmeticBadOctal "(( 1 ^ 0777 ))"
-checkArithmeticBadOctal _ t@(TA_Expansion id _) = potentially $ do
-    str <- getLiteralString t
-    guard $ str `matches` octalRE
-    return $ err id 2080 "Numbers with leading 0 are considered octal."
-  where
-    octalRE = mkRegex "^0[0-7]*[8-9]"
-checkArithmeticBadOctal _ _ = return ()
-
-prop_checkComparisonAgainstGlob = verify checkComparisonAgainstGlob "[[ $cow == $bar ]]"
-prop_checkComparisonAgainstGlob2 = verifyNot checkComparisonAgainstGlob "[[ $cow == \"$bar\" ]]"
-prop_checkComparisonAgainstGlob3 = verify checkComparisonAgainstGlob "[ $cow = *foo* ]"
-prop_checkComparisonAgainstGlob4 = verifyNot checkComparisonAgainstGlob "[ $cow = foo ]"
-prop_checkComparisonAgainstGlob5 = verify checkComparisonAgainstGlob "[[ $cow != $bar ]]"
-checkComparisonAgainstGlob _ (TC_Binary _ DoubleBracket op _ (T_NormalWord id [T_DollarBraced _ _]))
-    | op `elem` ["=", "==", "!="] =
-        warn id 2053 $ "Quote the rhs of " ++ op ++ " in [[ ]] to prevent glob matching."
-checkComparisonAgainstGlob _ (TC_Binary _ SingleBracket op _ word)
-        | (op == "=" || op == "==") && isGlob word =
-    err (getId word) 2081 "[ .. ] can't match globs. Use [[ .. ]] or case statement."
-checkComparisonAgainstGlob _ _ = return ()
-
-prop_checkCommarrays1 = verify checkCommarrays "a=(1, 2)"
-prop_checkCommarrays2 = verify checkCommarrays "a+=(1,2,3)"
-prop_checkCommarrays3 = verifyNot checkCommarrays "cow=(1 \"foo,bar\" 3)"
-prop_checkCommarrays4 = verifyNot checkCommarrays "cow=('one,' 'two')"
-prop_checkCommarrays5 = verify checkCommarrays "a=([a]=b, [c]=d)"
-prop_checkCommarrays6 = verify checkCommarrays "a=([a]=b,[c]=d,[e]=f)"
-checkCommarrays _ (T_Array id l) =
-    when (any (isCommaSeparated . literal) l) $
-        warn id 2054 "Use spaces, not commas, to separate array elements."
-  where
-    literal (T_IndexedElement _ _ l) = literal l
-    literal (T_NormalWord _ l) = concatMap literal l
-    literal (T_Literal _ str) = str
-    literal _ = "str"
-
-    isCommaSeparated str = "," `isSuffixOf` str || length (filter (== ',') str) > 1
-checkCommarrays _ _ = return ()
-
-prop_checkOrNeq1 = verify checkOrNeq "if [[ $lol -ne cow || $lol -ne foo ]]; then echo foo; fi"
-prop_checkOrNeq2 = verify checkOrNeq "(( a!=lol || a!=foo ))"
-prop_checkOrNeq3 = verify checkOrNeq "[ \"$a\" != lol || \"$a\" != foo ]"
-prop_checkOrNeq4 = verifyNot checkOrNeq "[ a != $cow || b != $foo ]"
-prop_checkOrNeq5 = verifyNot checkOrNeq "[[ $a != /home || $a != */public_html/* ]]"
--- This only catches the most idiomatic cases. Fixme?
-checkOrNeq _ (TC_Or id typ op (TC_Binary _ _ op1 lhs1 rhs1 ) (TC_Binary _ _ op2 lhs2 rhs2))
-    | lhs1 == lhs2 && (op1 == op2 && (op1 == "-ne" || op1 == "!=")) && not (any isGlob [rhs1,rhs2]) =
-        warn id 2055 $ "You probably wanted " ++ (if typ == SingleBracket then "-a" else "&&") ++ " here."
-
-checkOrNeq _ (TA_Binary id "||" (TA_Binary _ "!=" word1 _) (TA_Binary _ "!=" word2 _))
-    | word1 == word2 =
-        warn id 2056 "You probably wanted && here."
-checkOrNeq _ _ = return ()
-
-
-prop_checkValidCondOps1 = verify checkValidCondOps "[[ a -xz b ]]"
-prop_checkValidCondOps2 = verify checkValidCondOps "[ -M a ]"
-prop_checkValidCondOps2a= verifyNot checkValidCondOps "[ 3 \\> 2 ]"
-prop_checkValidCondOps3 = verifyNot checkValidCondOps "[ 1 = 2 -a 3 -ge 4 ]"
-prop_checkValidCondOps4 = verifyNot checkValidCondOps "[[ ! -v foo ]]"
-checkValidCondOps _ (TC_Binary id _ s _ _)
-    | s `notElem` binaryTestOps =
-        warn id 2057 "Unknown binary operator."
-checkValidCondOps _ (TC_Unary id _ s _)
-    | s `notElem`  unaryTestOps =
-        warn id 2058 "Unknown unary operator."
-checkValidCondOps _ _ = return ()
-
-prop_checkUuoeVar1 = verify checkUuoeVar "for f in $(echo $tmp); do echo lol; done"
-prop_checkUuoeVar2 = verify checkUuoeVar "date +`echo \"$format\"`"
-prop_checkUuoeVar3 = verifyNot checkUuoeVar "foo \"$(echo -e '\r')\""
-prop_checkUuoeVar4 = verifyNot checkUuoeVar "echo $tmp"
-prop_checkUuoeVar5 = verify checkUuoeVar "foo \"$(echo \"$(date) value:\" $value)\""
-prop_checkUuoeVar6 = verifyNot checkUuoeVar "foo \"$(echo files: *.png)\""
-prop_checkUuoeVar7 = verifyNot checkUuoeVar "foo $(echo $(bar))" -- covered by 2005
-prop_checkUuoeVar8 = verifyNot checkUuoeVar "#!/bin/sh\nz=$(echo)"
-prop_checkUuoeVar9 = verify checkUuoeVar "foo $(echo $(<file))"
-checkUuoeVar _ p =
-    case p of
-        T_Backticked id [cmd] -> check id cmd
-        T_DollarExpansion id [cmd] -> check id cmd
-        _ -> return ()
-  where
-    couldBeOptimized f = case f of
-        T_Glob {} -> False
-        T_Extglob {} -> False
-        T_BraceExpansion {} -> False
-        T_NormalWord _ l -> all couldBeOptimized l
-        T_DoubleQuoted _ l -> all couldBeOptimized l
-        _ -> True
-
-    check id (T_Pipeline _ _ [T_Redirecting _ _ c]) = warnForEcho id c
-    check _ _ = return ()
-    isCovered first rest = null rest && tokenIsJustCommandOutput first
-    warnForEcho id = checkUnqualifiedCommand "echo" $ \_ vars ->
-        case vars of
-          (first:rest) ->
-            unless (isCovered first rest || "-" `isPrefixOf` onlyLiteralString first) $
-                when (all couldBeOptimized vars) $ style id 2116
-                    "Useless echo? Instead of 'cmd $(echo foo)', just use 'cmd foo'."
-          _ -> return ()
-
-
-prop_checkTestRedirects1 = verify checkTestRedirects "test 3 > 1"
-prop_checkTestRedirects2 = verifyNot checkTestRedirects "test 3 \\> 1"
-prop_checkTestRedirects3 = verify checkTestRedirects "/usr/bin/test $var > $foo"
-prop_checkTestRedirects4 = verifyNot checkTestRedirects "test 1 -eq 2 2> file"
-checkTestRedirects _ (T_Redirecting id redirs cmd) | cmd `isCommand` "test" =
-    mapM_ check redirs
-  where
-    check t =
-        when (suspicious t) $
-            warn (getId t) 2065 "This is interpreted as a shell file redirection, not a comparison."
-    suspicious t = -- Ignore redirections of stderr because these are valid for squashing e.g. int errors,
-        case t of  -- and >> and similar redirections because these are probably not comparisons.
-            T_FdRedirect _ fd (T_IoFile _ op _) -> fd /= "2" && isComparison op
-            _ -> False
-    isComparison t =
-        case t of
-            T_Greater _ -> True
-            T_Less _ -> True
-            _ -> False
-checkTestRedirects _ _ = return ()
-
-prop_checkSudoRedirect1 = verify checkSudoRedirect "sudo echo 3 > /proc/file"
-prop_checkSudoRedirect2 = verify checkSudoRedirect "sudo cmd < input"
-prop_checkSudoRedirect3 = verify checkSudoRedirect "sudo cmd >> file"
-prop_checkSudoRedirect4 = verify checkSudoRedirect "sudo cmd &> file"
-prop_checkSudoRedirect5 = verifyNot checkSudoRedirect "sudo cmd 2>&1"
-prop_checkSudoRedirect6 = verifyNot checkSudoRedirect "sudo cmd 2> log"
-prop_checkSudoRedirect7 = verifyNot checkSudoRedirect "sudo cmd > /dev/null 2>&1"
-checkSudoRedirect _ (T_Redirecting _ redirs cmd) | cmd `isCommand` "sudo" =
-    mapM_ warnAbout redirs
-  where
-    warnAbout (T_FdRedirect _ s (T_IoFile id op file))
-        | (s == "" || s == "&") && not (special file) =
-        case op of
-            T_Less _ ->
-              info (getId op) 2024
-                "sudo doesn't affect redirects. Use sudo cat file | .."
-            T_Greater _ ->
-              warn (getId op) 2024
-                "sudo doesn't affect redirects. Use ..| sudo tee file"
-            T_DGREAT _ ->
-              warn (getId op) 2024
-                "sudo doesn't affect redirects. Use .. | sudo tee -a file"
-            _ -> return ()
-    warnAbout _ = return ()
-    special file = concat (oversimplify file) == "/dev/null"
-checkSudoRedirect _ _ = return ()
-
-prop_checkPS11 = verify checkPS1Assignments "PS1='\\033[1;35m\\$ '"
-prop_checkPS11a= verify checkPS1Assignments "export PS1='\\033[1;35m\\$ '"
-prop_checkPSf2 = verify checkPS1Assignments "PS1='\\h \\e[0m\\$ '"
-prop_checkPS13 = verify checkPS1Assignments "PS1=$'\\x1b[c '"
-prop_checkPS14 = verify checkPS1Assignments "PS1=$'\\e[3m; '"
-prop_checkPS14a= verify checkPS1Assignments "export PS1=$'\\e[3m; '"
-prop_checkPS15 = verifyNot checkPS1Assignments "PS1='\\[\\033[1;35m\\]\\$ '"
-prop_checkPS16 = verifyNot checkPS1Assignments "PS1='\\[\\e1m\\e[1m\\]\\$ '"
-prop_checkPS17 = verifyNot checkPS1Assignments "PS1='e033x1B'"
-prop_checkPS18 = verifyNot checkPS1Assignments "PS1='\\[\\e\\]'"
-checkPS1Assignments _ (T_Assignment _ _ "PS1" _ word) = warnFor word
-  where
-    warnFor word =
-        let contents = concat $ oversimplify word in
-            when (containsUnescaped contents) $
-                info (getId word) 2025 "Make sure all escape sequences are enclosed in \\[..\\] to prevent line wrapping issues"
-    containsUnescaped s =
-        let unenclosed = subRegex enclosedRegex s "" in
-           isJust $ matchRegex escapeRegex unenclosed
-    enclosedRegex = mkRegex "\\\\\\[.*\\\\\\]" -- FIXME: shouldn't be eager
-    escapeRegex = mkRegex "\\\\x1[Bb]|\\\\e|\x1B|\\\\033"
-checkPS1Assignments _ _ = return ()
-
-prop_checkBackticks1 = verify checkBackticks "echo `foo`"
-prop_checkBackticks2 = verifyNot checkBackticks "echo $(foo)"
-prop_checkBackticks3 = verifyNot checkBackticks "echo `#inlined comment` foo"
-checkBackticks _ (T_Backticked id list) | not (null list) =
-    style id 2006 "Use $(..) instead of legacy `..`."
-checkBackticks _ _ = return ()
-
-prop_checkIndirectExpansion1 = verify checkIndirectExpansion "${foo$n}"
-prop_checkIndirectExpansion2 = verifyNot checkIndirectExpansion "${foo//$n/lol}"
-prop_checkIndirectExpansion3 = verify checkIndirectExpansion "${$#}"
-prop_checkIndirectExpansion4 = verify checkIndirectExpansion "${var${n}_$((i%2))}"
-prop_checkIndirectExpansion5 = verifyNot checkIndirectExpansion "${bar}"
-checkIndirectExpansion _ (T_DollarBraced i (T_NormalWord _ contents)) =
-    when (isIndirection contents) $
-        err i 2082 "To expand via indirection, use arrays, ${!name} or (for sh only) eval."
-  where
-    isIndirection vars =
-        let list = mapMaybe isIndirectionPart vars in
-            not (null list) && and list
-    isIndirectionPart t =
-        case t of T_DollarExpansion _ _ ->  Just True
-                  T_Backticked _ _ ->       Just True
-                  T_DollarBraced _ _ ->     Just True
-                  T_DollarArithmetic _ _ -> Just True
-                  T_Literal _ s -> if all isVariableChar s
-                                    then Nothing
-                                    else Just False
-                  _ -> Just False
-
-checkIndirectExpansion _ _ = return ()
-
-prop_checkInexplicablyUnquoted1 = verify checkInexplicablyUnquoted "echo 'var='value';'"
-prop_checkInexplicablyUnquoted2 = verifyNot checkInexplicablyUnquoted "'foo'*"
-prop_checkInexplicablyUnquoted3 = verifyNot checkInexplicablyUnquoted "wget --user-agent='something'"
-prop_checkInexplicablyUnquoted4 = verify checkInexplicablyUnquoted "echo \"VALUES (\"id\")\""
-prop_checkInexplicablyUnquoted5 = verifyNot checkInexplicablyUnquoted "\"$dir\"/\"$file\""
-prop_checkInexplicablyUnquoted6 = verifyNot checkInexplicablyUnquoted "\"$dir\"some_stuff\"$file\""
-prop_checkInexplicablyUnquoted7 = verifyNot checkInexplicablyUnquoted "${dir/\"foo\"/\"bar\"}"
-prop_checkInexplicablyUnquoted8 = verifyNot checkInexplicablyUnquoted "  'foo'\\\n  'bar'"
-checkInexplicablyUnquoted _ (T_NormalWord id tokens) = mapM_ check (tails tokens)
-  where
-    check (T_SingleQuoted _ _:T_Literal id str:_)
-        | not (null str) && all isAlphaNum str =
-        info id 2026 "This word is outside of quotes. Did you intend to 'nest '\"'single quotes'\"' instead'? "
-
-    check (T_DoubleQuoted _ a:trapped:T_DoubleQuoted _ b:_) =
-        case trapped of
-            T_DollarExpansion id _ -> warnAboutExpansion id
-            T_DollarBraced id _ -> warnAboutExpansion id
-            T_Literal id s ->
-                unless (quotesSingleThing a && quotesSingleThing b) $
-                    warnAboutLiteral id
-            _ -> return ()
-
-    check _ = return ()
-
-    -- If the surrounding quotes quote single things, like "$foo"_and_then_some_"$stuff",
-    -- the quotes were probably intentional and harmless.
-    quotesSingleThing x = case x of
-        [T_DollarExpansion _ _] -> True
-        [T_DollarBraced _ _] -> True
-        [T_Backticked _ _] -> True
-        _ -> False
-
-    warnAboutExpansion id =
-        warn id 2027 "The surrounding quotes actually unquote this. Remove or escape them."
-    warnAboutLiteral id =
-        warn id 2140 "Word is of the form \"A\"B\"C\" (B indicated). Did you mean \"ABC\" or \"A\\\"B\\\"C\"?"
-checkInexplicablyUnquoted _ _ = return ()
-
-prop_checkTildeInQuotes1 = verify checkTildeInQuotes "var=\"~/out.txt\""
-prop_checkTildeInQuotes2 = verify checkTildeInQuotes "foo > '~/dir'"
-prop_checkTildeInQuotes4 = verifyNot checkTildeInQuotes "~/file"
-prop_checkTildeInQuotes5 = verifyNot checkTildeInQuotes "echo '/~foo/cow'"
-prop_checkTildeInQuotes6 = verifyNot checkTildeInQuotes "awk '$0 ~ /foo/'"
-checkTildeInQuotes _ = check
-  where
-    verify id ('~':'/':_) = warn id 2088 "Tilde does not expand in quotes. Use $HOME."
-    verify _ _ = return ()
-    check (T_NormalWord _ (T_SingleQuoted id str:_)) =
-        verify id str
-    check (T_NormalWord _ (T_DoubleQuoted _ (T_Literal id str:_):_)) =
-        verify id str
-    check _ = return ()
-
-prop_checkLonelyDotDash1 = verify checkLonelyDotDash "./ file"
-prop_checkLonelyDotDash2 = verifyNot checkLonelyDotDash "./file"
-checkLonelyDotDash _ t@(T_Redirecting id _ _)
-    | isUnqualifiedCommand t "./" =
-        err id 2083 "Don't add spaces after the slash in './file'."
-checkLonelyDotDash _ _ = return ()
-
-
-prop_checkSpuriousExec1 = verify checkSpuriousExec "exec foo; true"
-prop_checkSpuriousExec2 = verify checkSpuriousExec "if a; then exec b; exec c; fi"
-prop_checkSpuriousExec3 = verifyNot checkSpuriousExec "echo cow; exec foo"
-prop_checkSpuriousExec4 = verifyNot checkSpuriousExec "if a; then exec b; fi"
-prop_checkSpuriousExec5 = verifyNot checkSpuriousExec "exec > file; cmd"
-prop_checkSpuriousExec6 = verify checkSpuriousExec "exec foo > file; cmd"
-prop_checkSpuriousExec7 = verifyNot checkSpuriousExec "exec file; echo failed; exit 3"
-checkSpuriousExec _ = doLists
-  where
-    doLists (T_Script _ _ cmds) = doList cmds
-    doLists (T_BraceGroup _ cmds) = doList cmds
-    doLists (T_WhileExpression _ _ cmds) = doList cmds
-    doLists (T_UntilExpression _ _ cmds) = doList cmds
-    doLists (T_ForIn _ _ _ cmds) = doList cmds
-    doLists (T_ForArithmetic _ _ _ _ cmds) = doList cmds
-    doLists (T_IfExpression _ thens elses) = do
-        mapM_ (\(_, l) -> doList l) thens
-        doList elses
-    doLists _ = return ()
-
-    stripCleanup = reverse . dropWhile cleanup . reverse
-    cleanup (T_Pipeline _ _ [cmd]) =
-        isCommandMatch cmd (`elem` ["echo", "exit"])
-    cleanup _ = False
-
-    doList = doList' . stripCleanup
-    doList' t@(current:following:_) = do
-        commentIfExec current
-        doList (tail t)
-    doList' _ = return ()
-
-    commentIfExec (T_Pipeline id _ list) =
-      mapM_ commentIfExec $ take 1 list
-    commentIfExec (T_Redirecting _ _ f@(
-      T_SimpleCommand id _ (cmd:arg:_))) =
-        when (f `isUnqualifiedCommand` "exec") $
-          warn id 2093
-            "Remove \"exec \" if script should continue after this command."
-    commentIfExec _ = return ()
-
-
-prop_checkSpuriousExpansion1 = verify checkSpuriousExpansion "if $(true); then true; fi"
-prop_checkSpuriousExpansion2 = verify checkSpuriousExpansion "while \"$(cmd)\"; do :; done"
-prop_checkSpuriousExpansion3 = verifyNot checkSpuriousExpansion "$(cmd) --flag1 --flag2"
-prop_checkSpuriousExpansion4 = verify checkSpuriousExpansion "$((i++))"
-checkSpuriousExpansion _ (T_SimpleCommand _ _ [T_NormalWord _ [word]]) = check word
-  where
-    check word = case word of
-        T_DollarExpansion id _ ->
-            warn id 2091 "Remove surrounding $() to avoid executing output."
-        T_Backticked id _ ->
-            warn id 2092 "Remove backticks to avoid executing output."
-        T_DollarArithmetic id _ ->
-            err id 2084 "Remove '$' or use '_=$((expr))' to avoid executing output."
-        T_DoubleQuoted id [subword] -> check subword
-        _ -> return ()
-checkSpuriousExpansion _ _ = return ()
-
-
-prop_checkDollarBrackets1 = verify checkDollarBrackets "echo $[1+2]"
-prop_checkDollarBrackets2 = verifyNot checkDollarBrackets "echo $((1+2))"
-checkDollarBrackets _ (T_DollarBracket id _) =
-    style id 2007 "Use $((..)) instead of deprecated $[..]"
-checkDollarBrackets _ _ = return ()
-
-prop_checkSshHereDoc1 = verify checkSshHereDoc "ssh host << foo\necho $PATH\nfoo"
-prop_checkSshHereDoc2 = verifyNot checkSshHereDoc "ssh host << 'foo'\necho $PATH\nfoo"
-checkSshHereDoc _ (T_Redirecting _ redirs cmd)
-        | cmd `isCommand` "ssh" =
-    mapM_ checkHereDoc redirs
-  where
-    hasVariables = mkRegex "[`$]"
-    checkHereDoc (T_FdRedirect _ _ (T_HereDoc id _ Unquoted token tokens))
-        | not (all isConstant tokens) =
-        warn id 2087 $ "Quote '" ++ token ++ "' to make here document expansions happen on the server side rather than on the client."
-    checkHereDoc _ = return ()
-checkSshHereDoc _ _ = return ()
-
---- Subshell detection
-prop_subshellAssignmentCheck = verifyTree     subshellAssignmentCheck "cat foo | while read bar; do a=$bar; done; echo \"$a\""
-prop_subshellAssignmentCheck2 = verifyNotTree subshellAssignmentCheck "while read bar; do a=$bar; done < file; echo \"$a\""
-prop_subshellAssignmentCheck3 = verifyTree    subshellAssignmentCheck "( A=foo; ); rm $A"
-prop_subshellAssignmentCheck4 = verifyNotTree subshellAssignmentCheck "( A=foo; rm $A; )"
-prop_subshellAssignmentCheck5 = verifyTree    subshellAssignmentCheck "cat foo | while read cow; do true; done; echo $cow;"
-prop_subshellAssignmentCheck6 = verifyTree    subshellAssignmentCheck "( export lol=$(ls); ); echo $lol;"
-prop_subshellAssignmentCheck6a= verifyTree    subshellAssignmentCheck "( typeset -a lol=a; ); echo $lol;"
-prop_subshellAssignmentCheck7 = verifyTree    subshellAssignmentCheck "cmd | while read foo; do (( n++ )); done; echo \"$n lines\""
-prop_subshellAssignmentCheck8 = verifyTree    subshellAssignmentCheck "n=3 & echo $((n++))"
-prop_subshellAssignmentCheck9 = verifyTree    subshellAssignmentCheck "read n & n=foo$n"
-prop_subshellAssignmentCheck10 = verifyTree    subshellAssignmentCheck "(( n <<= 3 )) & (( n |= 4 )) &"
-prop_subshellAssignmentCheck11 = verifyTree subshellAssignmentCheck "cat /etc/passwd | while read line; do let n=n+1; done\necho $n"
-prop_subshellAssignmentCheck12 = verifyTree subshellAssignmentCheck "cat /etc/passwd | while read line; do let ++n; done\necho $n"
-prop_subshellAssignmentCheck13 = verifyTree subshellAssignmentCheck "#!/bin/bash\necho foo | read bar; echo $bar"
-prop_subshellAssignmentCheck14 = verifyNotTree subshellAssignmentCheck "#!/bin/ksh93\necho foo | read bar; echo $bar"
-prop_subshellAssignmentCheck15 = verifyNotTree subshellAssignmentCheck "#!/bin/ksh\ncat foo | while read bar; do a=$bar; done\necho \"$a\""
-prop_subshellAssignmentCheck16 = verifyNotTree subshellAssignmentCheck "(set -e); echo $@"
-prop_subshellAssignmentCheck17 = verifyNotTree subshellAssignmentCheck "foo=${ { bar=$(baz); } 2>&1; }; echo $foo $bar"
-prop_subshellAssignmentCheck18 = verifyTree subshellAssignmentCheck "( exec {n}>&2; ); echo $n"
-prop_subshellAssignmentCheck19 = verifyNotTree subshellAssignmentCheck "#!/bin/bash\nshopt -s lastpipe; echo a | read -r b; echo \"$b\""
-subshellAssignmentCheck params t =
-    let flow = variableFlow params
-        check = findSubshelled flow [("oops",[])] Map.empty
-    in execWriter check
-
-
-findSubshelled [] _ _ = return ()
-findSubshelled (Assignment x@(_, _, str, _):rest) ((reason,scope):lol) deadVars =
-    findSubshelled rest ((reason, x:scope):lol) $ Map.insert str Alive deadVars
-findSubshelled (Reference (_, readToken, str):rest) scopes deadVars = do
-    unless (shouldIgnore str) $ case Map.findWithDefault Alive str deadVars of
-        Alive -> return ()
-        Dead writeToken reason -> do
-                    info (getId writeToken) 2030 $ "Modification of " ++ str ++ " is local (to subshell caused by "++ reason ++")."
-                    info (getId readToken) 2031 $ str ++ " was modified in a subshell. That change might be lost."
-    findSubshelled rest scopes deadVars
-  where
-    shouldIgnore str =
-        str `elem` ["@", "*", "IFS"]
-
-findSubshelled (StackScope (SubshellScope reason):rest) scopes deadVars =
-    findSubshelled rest ((reason,[]):scopes) deadVars
-
-findSubshelled (StackScopeEnd:rest) ((reason, scope):oldScopes) deadVars =
-    findSubshelled rest oldScopes $
-        foldl (\m (_, token, var, _) ->
-            Map.insert var (Dead token reason) m) deadVars scope
-
-
--- FIXME: This is a very strange way of doing it.
--- For each variable read/write, run a stateful function that emits
--- comments. The comments are collected and returned.
-doVariableFlowAnalysis ::
-    (Token -> Token -> String -> State t [v])
-    -> (Token -> Token -> String -> DataType -> State t [v])
-    -> t
-    -> [StackData]
-    -> [v]
-
-doVariableFlowAnalysis readFunc writeFunc empty flow = evalState (
-    foldM (\list x -> do { l <- doFlow x;  return $ l ++ list; }) [] flow
-    ) empty
-  where
-    doFlow (Reference (base, token, name)) =
-        readFunc base token name
-    doFlow (Assignment (base, token, name, values)) =
-        writeFunc base token name values
-    doFlow _ = return []
-
----- Check whether variables could have spaces/globs
-prop_checkSpacefulness1 = verifyTree checkSpacefulness "a='cow moo'; echo $a"
-prop_checkSpacefulness2 = verifyNotTree checkSpacefulness "a='cow moo'; [[ $a ]]"
-prop_checkSpacefulness3 = verifyNotTree checkSpacefulness "a='cow*.mp3'; echo \"$a\""
-prop_checkSpacefulness4 = verifyTree checkSpacefulness "for f in *.mp3; do echo $f; done"
-prop_checkSpacefulness4a= verifyNotTree checkSpacefulness "foo=3; foo=$(echo $foo)"
-prop_checkSpacefulness5 = verifyTree checkSpacefulness "a='*'; b=$a; c=lol${b//foo/bar}; echo $c"
-prop_checkSpacefulness6 = verifyTree checkSpacefulness "a=foo$(lol); echo $a"
-prop_checkSpacefulness7 = verifyTree checkSpacefulness "a=foo\\ bar; rm $a"
-prop_checkSpacefulness8 = verifyNotTree checkSpacefulness "a=foo\\ bar; a=foo; rm $a"
-prop_checkSpacefulness10= verifyTree checkSpacefulness "rm $1"
-prop_checkSpacefulness11= verifyTree checkSpacefulness "rm ${10//foo/bar}"
-prop_checkSpacefulness12= verifyNotTree checkSpacefulness "(( $1 + 3 ))"
-prop_checkSpacefulness13= verifyNotTree checkSpacefulness "if [[ $2 -gt 14 ]]; then true; fi"
-prop_checkSpacefulness14= verifyNotTree checkSpacefulness "foo=$3 env"
-prop_checkSpacefulness15= verifyNotTree checkSpacefulness "local foo=$1"
-prop_checkSpacefulness16= verifyNotTree checkSpacefulness "declare foo=$1"
-prop_checkSpacefulness17= verifyTree checkSpacefulness "echo foo=$1"
-prop_checkSpacefulness18= verifyNotTree checkSpacefulness "$1 --flags"
-prop_checkSpacefulness19= verifyTree checkSpacefulness "echo $PWD"
-prop_checkSpacefulness20= verifyNotTree checkSpacefulness "n+='foo bar'"
-prop_checkSpacefulness21= verifyNotTree checkSpacefulness "select foo in $bar; do true; done"
-prop_checkSpacefulness22= verifyNotTree checkSpacefulness "echo $\"$1\""
-prop_checkSpacefulness23= verifyNotTree checkSpacefulness "a=(1); echo ${a[@]}"
-prop_checkSpacefulness24= verifyTree checkSpacefulness "a='a    b'; cat <<< $a"
-prop_checkSpacefulness25= verifyTree checkSpacefulness "a='s/[0-9]//g'; sed $a"
-prop_checkSpacefulness26= verifyTree checkSpacefulness "a='foo bar'; echo {1,2,$a}"
-prop_checkSpacefulness27= verifyNotTree checkSpacefulness "echo ${a:+'foo'}"
-prop_checkSpacefulness28= verifyNotTree checkSpacefulness "exec {n}>&1; echo $n"
-prop_checkSpacefulness29= verifyNotTree checkSpacefulness "n=$(stuff); exec {n}>&-;"
-prop_checkSpacefulness30= verifyTree checkSpacefulness "file='foo bar'; echo foo > $file;"
-prop_checkSpacefulness31= verifyNotTree checkSpacefulness "echo \"`echo \\\"$1\\\"`\""
-prop_checkSpacefulness32= verifyNotTree checkSpacefulness "var=$1; [ -v var ]"
-prop_checkSpacefulness33= verifyTree checkSpacefulness "for file; do echo $file; done"
-prop_checkSpacefulness34= verifyTree checkSpacefulness "declare foo$n=$1"
-prop_checkSpacefulness35= verifyNotTree checkSpacefulness "echo ${1+\"$1\"}"
-
-checkSpacefulness params t =
-    doVariableFlowAnalysis readF writeF (Map.fromList defaults) (variableFlow params)
-  where
-    defaults = zip variablesWithoutSpaces (repeat False)
-
-    hasSpaces name = do
-        map <- get
-        return $ Map.findWithDefault True name map
-
-    setSpaces name bool =
-        modify $ Map.insert name bool
-
-    readF _ token name = do
-        spaced <- hasSpaces name
-        return [makeComment InfoC (getId token) 2086 warning |
-                  isExpansion token && spaced
-                  && not (isArrayExpansion token) -- There's another warning for this
-                  && not (isCountingReference token)
-                  && not (isQuoteFree parents token)
-                  && not (isQuotedAlternativeReference token)
-                  && not (usedAsCommandName parents token)]
-      where
-        warning = "Double quote to prevent globbing and word splitting."
-
-    writeF _ _ name (DataString SourceExternal) = setSpaces name True >> return []
-    writeF _ _ name (DataString SourceInteger) = setSpaces name False >> return []
-
-    writeF _ _ name (DataString (SourceFrom vals)) = do
-        map <- get
-        setSpaces name
-            (isSpacefulWord (\x -> Map.findWithDefault True x map) vals)
-        return []
-
-    writeF _ _ _ _ = return []
-
-    parents = parentMap params
-
-    isExpansion t =
-        case t of
-            (T_DollarBraced _ _ ) -> True
-            _ -> False
-
-    isSpacefulWord :: (String -> Bool) -> [Token] -> Bool
-    isSpacefulWord f = any (isSpaceful f)
-    isSpaceful :: (String -> Bool) -> Token -> Bool
-    isSpaceful spacefulF x =
-        case x of
-          T_DollarExpansion _ _ -> True
-          T_Backticked _ _ -> True
-          T_Glob _ _         -> True
-          T_Extglob {}       -> True
-          T_Literal _ s      -> s `containsAny` globspace
-          T_SingleQuoted _ s -> s `containsAny` globspace
-          T_DollarBraced _ _ -> spacefulF $ getBracedReference $ bracedString x
-          T_NormalWord _ w   -> isSpacefulWord spacefulF w
-          T_DoubleQuoted _ w -> isSpacefulWord spacefulF w
-          _ -> False
-      where
-        globspace = "*?[] \t\n"
-        containsAny s = any (`elem` s)
-
-prop_checkQuotesInLiterals1 = verifyTree checkQuotesInLiterals "param='--foo=\"bar\"'; app $param"
-prop_checkQuotesInLiterals1a= verifyTree checkQuotesInLiterals "param=\"--foo='lolbar'\"; app $param"
-prop_checkQuotesInLiterals2 = verifyNotTree checkQuotesInLiterals "param='--foo=\"bar\"'; app \"$param\""
-prop_checkQuotesInLiterals3 =verifyNotTree checkQuotesInLiterals "param=('--foo='); app \"${param[@]}\""
-prop_checkQuotesInLiterals4 = verifyNotTree checkQuotesInLiterals "param=\"don't bother with this one\"; app $param"
-prop_checkQuotesInLiterals5 = verifyNotTree checkQuotesInLiterals "param=\"--foo='lolbar'\"; eval app $param"
-prop_checkQuotesInLiterals6 = verifyTree checkQuotesInLiterals "param='my\\ file'; cmd=\"rm $param\"; $cmd"
-prop_checkQuotesInLiterals6a= verifyNotTree checkQuotesInLiterals "param='my\\ file'; cmd=\"rm ${#param}\"; $cmd"
-prop_checkQuotesInLiterals7 = verifyTree checkQuotesInLiterals "param='my\\ file'; rm $param"
-prop_checkQuotesInLiterals8 = verifyTree checkQuotesInLiterals "param=\"/foo/'bar baz'/etc\"; rm $param"
-prop_checkQuotesInLiterals9 = verifyNotTree checkQuotesInLiterals "param=\"/foo/'bar baz'/etc\"; rm ${#param}"
-checkQuotesInLiterals params t =
-    doVariableFlowAnalysis readF writeF Map.empty (variableFlow params)
-  where
-    getQuotes name = fmap (Map.lookup name) get
-    setQuotes name ref = modify $ Map.insert name ref
-    deleteQuotes = modify . Map.delete
-    parents = parentMap params
-    quoteRegex = mkRegex "\"|([/= ]|^)'|'( |$)|\\\\ "
-    containsQuotes s = s `matches` quoteRegex
-
-    writeF _ _ name (DataString (SourceFrom values)) = do
-        quoteMap <- get
-        let quotedVars = msum $ map (forToken quoteMap) values
-        case quotedVars of
-            Nothing -> deleteQuotes name
-            Just x -> setQuotes name x
-        return []
-    writeF _ _ _ _ = return []
-
-    forToken map (T_DollarBraced id t) =
-        -- skip getBracedReference here to avoid false positives on PE
-        Map.lookup (concat . oversimplify $ t) map
-    forToken quoteMap (T_DoubleQuoted id tokens) =
-        msum $ map (forToken quoteMap) tokens
-    forToken quoteMap (T_NormalWord id tokens) =
-        msum $ map (forToken quoteMap) tokens
-    forToken _ t =
-        if containsQuotes (concat $ oversimplify t)
-        then return $ getId t
-        else Nothing
-
-    squashesQuotes t =
-        case t of
-            T_DollarBraced id _ -> "#" `isPrefixOf` bracedString t
-            _ -> False
-
-    readF _ expr name = do
-        assignment <- getQuotes name
-        return
-          (if isJust assignment
-              && not (isParamTo parents "eval" expr)
-              && not (isQuoteFree parents expr)
-              && not (squashesQuotes expr)
-              then [
-                  makeComment WarningC (fromJust assignment) 2089
-                      "Quotes/backslashes will be treated literally. Use an array.",
-                  makeComment WarningC (getId expr) 2090
-                      "Quotes/backslashes in this variable will not be respected."
-                ]
-              else [])
-
-
-prop_checkFunctionsUsedExternally1 =
-  verifyTree checkFunctionsUsedExternally "foo() { :; }; sudo foo"
-prop_checkFunctionsUsedExternally2 =
-  verifyTree checkFunctionsUsedExternally "alias f='a'; xargs -n 1 f"
-prop_checkFunctionsUsedExternally3 =
-  verifyNotTree checkFunctionsUsedExternally "f() { :; }; echo f"
-prop_checkFunctionsUsedExternally4 =
-  verifyNotTree checkFunctionsUsedExternally "foo() { :; }; sudo \"foo\""
-checkFunctionsUsedExternally params t =
-    runNodeAnalysis checkCommand params t
-  where
-    invokingCmds = [
-        "chroot",
-        "find",
-        "screen",
-        "ssh",
-        "su",
-        "sudo",
-        "xargs"
-        ]
-    checkCommand _ t@(T_SimpleCommand _ _ (cmd:args)) =
-        let name = fromMaybe "" $ getCommandBasename t in
-          when (name `elem` invokingCmds) $
-            mapM_ (checkArg name) args
-    checkCommand _ _ = return ()
-
-    analyse f t = execState (doAnalysis f t) []
-    functions = Map.fromList $ analyse findFunctions t
-    findFunctions (T_Function id _ _ name _) = modify ((name, id):)
-    findFunctions t@(T_SimpleCommand id _ (_:args))
-        | t `isUnqualifiedCommand` "alias" = mapM_ getAlias args
-    findFunctions _ = return ()
-    getAlias arg =
-        let string = concat $ oversimplify arg
-        in when ('=' `elem` string) $
-            modify ((takeWhile (/= '=') string, getId arg):)
-    checkArg cmd arg = potentially $ do
-        literalArg <- getUnquotedLiteral arg  -- only consider unquoted literals
-        definitionId <- Map.lookup literalArg functions
-        return $ do
-            warn (getId arg) 2033
-              "Shell functions can't be passed to external commands."
-            info definitionId 2032 $
-              "Use own script or sh -c '..' to run this from " ++ cmd ++ "."
-
-prop_checkUnused0 = verifyNotTree checkUnusedAssignments "var=foo; echo $var"
-prop_checkUnused1 = verifyTree checkUnusedAssignments "var=foo; echo $bar"
-prop_checkUnused2 = verifyNotTree checkUnusedAssignments "var=foo; export var;"
-prop_checkUnused3 = verifyTree checkUnusedAssignments "for f in *; do echo '$f'; done"
-prop_checkUnused4 = verifyTree checkUnusedAssignments "local i=0"
-prop_checkUnused5 = verifyNotTree checkUnusedAssignments "read lol; echo $lol"
-prop_checkUnused6 = verifyNotTree checkUnusedAssignments "var=4; (( var++ ))"
-prop_checkUnused7 = verifyNotTree checkUnusedAssignments "var=2; $((var))"
-prop_checkUnused8 = verifyTree checkUnusedAssignments "var=2; var=3;"
-prop_checkUnused9 = verifyNotTree checkUnusedAssignments "read ''"
-prop_checkUnused10= verifyNotTree checkUnusedAssignments "read -p 'test: '"
-prop_checkUnused11= verifyNotTree checkUnusedAssignments "bar=5; export foo[$bar]=3"
-prop_checkUnused12= verifyNotTree checkUnusedAssignments "read foo; echo ${!foo}"
-prop_checkUnused13= verifyNotTree checkUnusedAssignments "x=(1); (( x[0] ))"
-prop_checkUnused14= verifyNotTree checkUnusedAssignments "x=(1); n=0; echo ${x[n]}"
-prop_checkUnused15= verifyNotTree checkUnusedAssignments "x=(1); n=0; (( x[n] ))"
-prop_checkUnused16= verifyNotTree checkUnusedAssignments "foo=5; declare -x foo"
-prop_checkUnused17= verifyNotTree checkUnusedAssignments "read -i 'foo' -e -p 'Input: ' bar; $bar;"
-prop_checkUnused18= verifyNotTree checkUnusedAssignments "a=1; arr=( [$a]=42 ); echo \"${arr[@]}\""
-prop_checkUnused19= verifyNotTree checkUnusedAssignments "a=1; let b=a+1; echo $b"
-prop_checkUnused20= verifyNotTree checkUnusedAssignments "a=1; PS1='$a'"
-prop_checkUnused21= verifyNotTree checkUnusedAssignments "a=1; trap 'echo $a' INT"
-prop_checkUnused22= verifyNotTree checkUnusedAssignments "a=1; [ -v a ]"
-prop_checkUnused23= verifyNotTree checkUnusedAssignments "a=1; [ -R a ]"
-prop_checkUnused24= verifyNotTree checkUnusedAssignments "mapfile -C a b; echo ${b[@]}"
-prop_checkUnused25= verifyNotTree checkUnusedAssignments "readarray foo; echo ${foo[@]}"
-prop_checkUnused26= verifyNotTree checkUnusedAssignments "declare -F foo"
-prop_checkUnused27= verifyTree checkUnusedAssignments "var=3; [ var -eq 3 ]"
-prop_checkUnused28= verifyNotTree checkUnusedAssignments "var=3; [[ var -eq 3 ]]"
-prop_checkUnused29= verifyNotTree checkUnusedAssignments "var=(a b); declare -p var"
-prop_checkUnused30= verifyTree checkUnusedAssignments "let a=1"
-prop_checkUnused31= verifyTree checkUnusedAssignments "let 'a=1'"
-prop_checkUnused32= verifyTree checkUnusedAssignments "let a=b=c; echo $a"
-prop_checkUnused33= verifyNotTree checkUnusedAssignments "a=foo; [[ foo =~ ^{$a}$ ]]"
-prop_checkUnused34= verifyNotTree checkUnusedAssignments "foo=1; (( t = foo )); echo $t"
-prop_checkUnused35= verifyNotTree checkUnusedAssignments "a=foo; b=2; echo ${a:b}"
-prop_checkUnused36= verifyNotTree checkUnusedAssignments "if [[ -v foo ]]; then true; fi"
-prop_checkUnused37= verifyNotTree checkUnusedAssignments "fd=2; exec {fd}>&-"
-checkUnusedAssignments params t = execWriter (mapM_ warnFor unused)
-  where
-    flow = variableFlow params
-    references = foldl (flip ($)) defaultMap (map insertRef flow)
-    insertRef (Reference (base, token, name)) =
-        Map.insert (stripSuffix name) ()
-    insertRef _ = id
-
-    assignments = foldl (flip ($)) Map.empty (map insertAssignment flow)
-    insertAssignment (Assignment (_, token, name, _)) | isVariableName name =
-        Map.insert name token
-    insertAssignment _ = id
-
-    unused = Map.assocs $ Map.difference assignments references
-
-    warnFor (name, token) =
-        warn (getId token) 2034 $
-            name ++ " appears unused. Verify it or export it."
-
-    stripSuffix = takeWhile isVariableChar
-    defaultMap = Map.fromList $ zip internalVariables $ repeat ()
-
-prop_checkUnassignedReferences1 = verifyTree checkUnassignedReferences "echo $foo"
-prop_checkUnassignedReferences2 = verifyNotTree checkUnassignedReferences "foo=hello; echo $foo"
-prop_checkUnassignedReferences3 = verifyTree checkUnassignedReferences "MY_VALUE=3; echo $MYVALUE"
-prop_checkUnassignedReferences4 = verifyNotTree checkUnassignedReferences "RANDOM2=foo; echo $RANDOM"
-prop_checkUnassignedReferences5 = verifyNotTree checkUnassignedReferences "declare -A foo=([bar]=baz); echo ${foo[bar]}"
-prop_checkUnassignedReferences6 = verifyNotTree checkUnassignedReferences "foo=..; echo ${foo-bar}"
-prop_checkUnassignedReferences7 = verifyNotTree checkUnassignedReferences "getopts ':h' foo; echo $foo"
-prop_checkUnassignedReferences8 = verifyNotTree checkUnassignedReferences "let 'foo = 1'; echo $foo"
-prop_checkUnassignedReferences9 = verifyNotTree checkUnassignedReferences "echo ${foo-bar}"
-prop_checkUnassignedReferences10= verifyNotTree checkUnassignedReferences "echo ${foo:?}"
-prop_checkUnassignedReferences11= verifyNotTree checkUnassignedReferences "declare -A foo; echo \"${foo[@]}\""
-prop_checkUnassignedReferences12= verifyNotTree checkUnassignedReferences "typeset -a foo; echo \"${foo[@]}\""
-prop_checkUnassignedReferences13= verifyNotTree checkUnassignedReferences "f() { local foo; echo $foo; }"
-prop_checkUnassignedReferences14= verifyNotTree checkUnassignedReferences "foo=; echo $foo"
-prop_checkUnassignedReferences15= verifyNotTree checkUnassignedReferences "f() { true; }; export -f f"
-prop_checkUnassignedReferences16= verifyNotTree checkUnassignedReferences "declare -A foo=( [a b]=bar ); echo ${foo[a b]}"
-prop_checkUnassignedReferences17= verifyNotTree checkUnassignedReferences "USERS=foo; echo $USER"
-prop_checkUnassignedReferences18= verifyNotTree checkUnassignedReferences "FOOBAR=42; export FOOBAR="
-prop_checkUnassignedReferences19= verifyNotTree checkUnassignedReferences "readonly foo=bar; echo $foo"
-prop_checkUnassignedReferences20= verifyNotTree checkUnassignedReferences "printf -v foo bar; echo $foo"
-prop_checkUnassignedReferences21= verifyTree checkUnassignedReferences "echo ${#foo}"
-prop_checkUnassignedReferences22= verifyNotTree checkUnassignedReferences "echo ${!os*}"
-prop_checkUnassignedReferences23= verifyTree checkUnassignedReferences "declare -a foo; foo[bar]=42;"
-prop_checkUnassignedReferences24= verifyNotTree checkUnassignedReferences "declare -A foo; foo[bar]=42;"
-prop_checkUnassignedReferences25= verifyNotTree checkUnassignedReferences "declare -A foo=(); foo[bar]=42;"
-prop_checkUnassignedReferences26= verifyNotTree checkUnassignedReferences "a::b() { foo; }; readonly -f a::b"
-prop_checkUnassignedReferences27= verifyNotTree checkUnassignedReferences ": ${foo:=bar}"
-prop_checkUnassignedReferences28= verifyNotTree checkUnassignedReferences "#!/bin/ksh\necho \"${.sh.version}\"\n"
-prop_checkUnassignedReferences29= verifyNotTree checkUnassignedReferences "if [[ -v foo ]]; then echo $foo; fi"
-prop_checkUnassignedReferences30= verifyNotTree checkUnassignedReferences "if [[ -v foo[3] ]]; then echo ${foo[3]}; fi"
-prop_checkUnassignedReferences31= verifyNotTree checkUnassignedReferences "X=1; if [[ -v foo[$X+42] ]]; then echo ${foo[$X+42]}; fi"
-prop_checkUnassignedReferences32= verifyNotTree checkUnassignedReferences "if [[ -v \"foo[1]\" ]]; then echo ${foo[@]}; fi"
-checkUnassignedReferences params t = warnings
-  where
-    (readMap, writeMap) = execState (mapM tally $ variableFlow params) (Map.empty, Map.empty)
-    defaultAssigned = Map.fromList $ map (\a -> (a, ())) $ filter (not . null) internalVariables
-
-    tally (Assignment (_, _, name, _))  =
-        modify (\(read, written) -> (read, Map.insert name () written))
-    tally (Reference (_, place, name)) =
-        modify (\(read, written) -> (Map.insertWith (const id) name place read, written))
-    tally _ = return ()
-
-    unassigned = Map.toList $ Map.difference (Map.difference readMap writeMap) defaultAssigned
-    writtenVars = filter isVariableName $ Map.keys writeMap
-
-    getBestMatch var = do
-        (match, score) <- listToMaybe best
-        guard $ goodMatch var match score
-        return match
-      where
-        matches = map (\x -> (x, match var x)) writtenVars
-        best = sortBy (comparing snd) matches
-        goodMatch var match score =
-            let l = length match in
-                l > 3 && score <= 1
-                || l > 7 && score <= 2
-
-    isLocal = any isLower
-
-    warningForGlobals var place = do
-        match <- getBestMatch var
-        return $ warn (getId place) 2153 $
-            "Possible misspelling: " ++ var ++ " may not be assigned, but " ++ match ++ " is."
-
-    warningForLocals var place =
-        return $ warn (getId place) 2154 $
-            var ++ " is referenced but not assigned" ++ optionalTip ++ "."
-      where
-        optionalTip =
-            if var `elem` commonCommands
-            then " (for output from commands, use \"$(" ++ var ++ " ..." ++ ")\" )"
-            else fromMaybe "" $ do
-                    match <- getBestMatch var
-                    return $ " (did you mean '" ++ match ++ "'?)"
-
-    warningFor var place = do
-        guard . not $ isInArray var place || isGuarded place
-        (if isLocal var then warningForLocals else warningForGlobals) var place
-
-    warnings = execWriter . sequence $ mapMaybe (uncurry warningFor) unassigned
-
-    -- Due to parsing, foo=( [bar]=baz ) parses 'bar' as a reference even for assoc arrays.
-    -- Similarly, ${foo[bar baz]} may not be referencing bar/baz. Just skip these.
-    isInArray var t = any isArray $ getPath (parentMap params) t
-      where
-        isArray T_Array {} = True
-        isArray b@(T_DollarBraced _ _) | var /= getBracedReference (bracedString b) = True
-        isArray _ = False
-
-    isGuarded (T_DollarBraced _ v) =
-        any (`isPrefixOf` rest) ["-", ":-", "?", ":?"]
-      where
-        name = concat $ oversimplify v
-        rest = dropWhile isVariableChar $ dropWhile (`elem` "#!") name
-    isGuarded _ = False
-
-    match var candidate =
-        if var /= candidate && map toLower var == map toLower candidate
-        then 1
-        else dist var candidate
-
-
-prop_checkGlobsAsOptions1 = verify checkGlobsAsOptions "rm *.txt"
-prop_checkGlobsAsOptions2 = verify checkGlobsAsOptions "ls ??.*"
-prop_checkGlobsAsOptions3 = verifyNot checkGlobsAsOptions "rm -- *.txt"
-prop_checkGlobsAsOptions4 = verifyNot checkGlobsAsOptions "*.txt"
-checkGlobsAsOptions _ (T_SimpleCommand _ _ args) =
-    mapM_ check $ takeWhile (not . isEndOfArgs) (drop 1 args)
-  where
-    check v@(T_NormalWord _ (T_Glob id s:_)) | s == "*" || s == "?" =
-        info id 2035 "Use ./*glob* or -- *glob* so names with dashes won't become options."
-    check _ = return ()
-
-    isEndOfArgs t =
-        case concat $ oversimplify t of
-            "--" -> True
-            ":::" -> True
-            "::::" -> True
-            _ -> False
-
-checkGlobsAsOptions _ _ = return ()
-
-
-prop_checkWhileReadPitfalls1 = verify checkWhileReadPitfalls "while read foo; do ssh $foo uptime; done < file"
-prop_checkWhileReadPitfalls2 = verifyNot checkWhileReadPitfalls "while read -u 3 foo; do ssh $foo uptime; done 3< file"
-prop_checkWhileReadPitfalls3 = verifyNot checkWhileReadPitfalls "while true; do ssh host uptime; done"
-prop_checkWhileReadPitfalls4 = verifyNot checkWhileReadPitfalls "while read foo; do ssh $foo hostname < /dev/null; done"
-prop_checkWhileReadPitfalls5 = verifyNot checkWhileReadPitfalls "while read foo; do echo ls | ssh $foo; done"
-prop_checkWhileReadPitfalls6 = verifyNot checkWhileReadPitfalls "while read foo <&3; do ssh $foo; done 3< foo"
-prop_checkWhileReadPitfalls7 = verify checkWhileReadPitfalls "while read foo; do if true; then ssh $foo uptime; fi; done < file"
-prop_checkWhileReadPitfalls8 = verifyNot checkWhileReadPitfalls "while read foo; do ssh -n $foo uptime; done < file"
-
-checkWhileReadPitfalls _ (T_WhileExpression id [command] contents)
-        | isStdinReadCommand command =
-    mapM_ checkMuncher contents
-  where
-    munchers = [ "ssh", "ffmpeg", "mplayer", "HandBrakeCLI" ]
-    preventionFlags = ["n", "noconsolecontrols" ]
-
-    isStdinReadCommand (T_Pipeline _ _ [T_Redirecting id redirs cmd]) =
-        let plaintext = oversimplify cmd
-        in head (plaintext ++ [""]) == "read"
-            && ("-u" `notElem` plaintext)
-            && all (not . stdinRedirect) redirs
-    isStdinReadCommand _ = False
-
-    checkMuncher (T_Pipeline _ _ (T_Redirecting _ redirs cmd:_)) | not $ any stdinRedirect redirs =
-        case cmd of
-            (T_IfExpression _ thens elses) ->
-              mapM_ checkMuncher . concat $ map fst thens ++ map snd thens ++ [elses]
-
-            _ -> potentially $ do
-                name <- getCommandBasename cmd
-                guard $ name `elem` munchers
-
-                -- Sloppily check if the command has a flag to prevent eating stdin.
-                let flags = getAllFlags cmd
-                guard . not $ any (`elem` preventionFlags) $ map snd flags
-                return $ do
-                    info id 2095 $
-                        name ++ " may swallow stdin, preventing this loop from working properly."
-                    warn (getId cmd) 2095 $
-                        "Add < /dev/null to prevent " ++ name ++ " from swallowing stdin."
-    checkMuncher _ = return ()
-
-    stdinRedirect (T_FdRedirect _ fd _)
-        | fd == "" || fd == "0" = True
-    stdinRedirect _ = False
-checkWhileReadPitfalls _ _ = return ()
-
-
-prop_checkPrefixAssign1 = verify checkPrefixAssignmentReference "var=foo echo $var"
-prop_checkPrefixAssign2 = verifyNot checkPrefixAssignmentReference "var=$(echo $var) cmd"
-checkPrefixAssignmentReference params t@(T_DollarBraced id value) =
-    check path
-  where
-    name = getBracedReference $ bracedString t
-    path = getPath (parentMap params) t
-    idPath = map getId path
-
-    check [] = return ()
-    check (t:rest) =
-        case t of
-            T_SimpleCommand _ vars (_:_) -> mapM_ checkVar vars
-            _ -> check rest
-    checkVar (T_Assignment aId mode aName [] value) |
-            aName == name && (aId `notElem` idPath) = do
-        warn aId 2097 "This assignment is only seen by the forked process."
-        warn id 2098 "This expansion will not see the mentioned assignment."
-    checkVar _ = return ()
-
-checkPrefixAssignmentReference _ _ = return ()
-
-prop_checkCharRangeGlob1 = verify checkCharRangeGlob "ls *[:digit:].jpg"
-prop_checkCharRangeGlob2 = verifyNot checkCharRangeGlob "ls *[[:digit:]].jpg"
-prop_checkCharRangeGlob3 = verify checkCharRangeGlob "ls [10-15]"
-prop_checkCharRangeGlob4 = verifyNot checkCharRangeGlob "ls [a-zA-Z]"
-prop_checkCharRangeGlob5 = verifyNot checkCharRangeGlob "tr -d [a-zA-Z]" -- tr has 2060
-checkCharRangeGlob p t@(T_Glob id str) |
-  isCharClass str && not (isParamTo (parentMap p) "tr" t) =
-    if ":" `isPrefixOf` contents
-        && ":" `isSuffixOf` contents
-        && contents /= ":"
-    then warn id 2101 "Named class needs outer [], e.g. [[:digit:]]."
-    else
-        when ('[' `notElem` contents && hasDupes) $
-            info id 2102 "Ranges can only match single chars (mentioned due to duplicates)."
-  where
-    isCharClass str = "[" `isPrefixOf` str && "]" `isSuffixOf` str
-    contents = drop 1 . take (length str - 1) $ str
-    hasDupes = any (>1) . map length . group . sort . filter (/= '-') $ contents
-checkCharRangeGlob _ _ = return ()
-
-
-
-prop_checkCdAndBack1 = verify checkCdAndBack "for f in *; do cd $f; git pull; cd ..; done"
-prop_checkCdAndBack2 = verifyNot checkCdAndBack "for f in *; do cd $f || continue; git pull; cd ..; done"
-prop_checkCdAndBack3 = verifyNot checkCdAndBack "while [[ $PWD != / ]]; do cd ..; done"
-prop_checkCdAndBack4 = verify checkCdAndBack "cd $tmp; foo; cd -"
-checkCdAndBack params = doLists
-  where
-    shell = shellType params
-    doLists (T_ForIn _ _ _ cmds) = doList cmds
-    doLists (T_ForArithmetic _ _ _ _ cmds) = doList cmds
-    doLists (T_WhileExpression _ _ cmds) = doList cmds
-    doLists (T_UntilExpression _ _ cmds) = doList cmds
-    doLists (T_Script _ _ cmds) = doList cmds
-    doLists (T_IfExpression _ thens elses) = do
-        mapM_ (\(_, l) -> doList l) thens
-        doList elses
-    doLists _ = return ()
-
-    isCdRevert t =
-        case oversimplify t of
-            ["cd", p] -> p `elem` ["..", "-"]
-            _ -> False
-
-    getCmd (T_Annotation id _ x) = getCmd x
-    getCmd (T_Pipeline id _ [x]) = getCommandName x
-    getCmd _ = Nothing
-
-    doList list =
-        let cds = filter ((== Just "cd") . getCmd) list in
-            when (length cds >= 2 && isCdRevert (last cds)) $
-               info (getId $ last cds) 2103 message
-
-    message = "Use a ( subshell ) to avoid having to cd back."
-
-prop_checkLoopKeywordScope1 = verify checkLoopKeywordScope "continue 2"
-prop_checkLoopKeywordScope2 = verify checkLoopKeywordScope "for f; do ( break; ); done"
-prop_checkLoopKeywordScope3 = verify checkLoopKeywordScope "if true; then continue; fi"
-prop_checkLoopKeywordScope4 = verifyNot checkLoopKeywordScope "while true; do break; done"
-prop_checkLoopKeywordScope5 = verify checkLoopKeywordScope "if true; then break; fi"
-prop_checkLoopKeywordScope6 = verify checkLoopKeywordScope "while true; do true | { break; }; done"
-prop_checkLoopKeywordScope7 = verifyNot checkLoopKeywordScope "#!/bin/ksh\nwhile true; do true | { break; }; done"
-checkLoopKeywordScope params t |
-        name `elem` map Just ["continue", "break"] =
-    if not $ any isLoop path
-    then if any isFunction $ take 1 path
-        -- breaking at a source/function invocation is an abomination. Let's ignore it.
-        then err (getId t) 2104 $ "In functions, use return instead of " ++ fromJust name ++ "."
-        else err (getId t) 2105 $ fromJust name ++ " is only valid in loops."
-    else case map subshellType $ filter (not . isFunction) path of
-        Just str:_ -> warn (getId t) 2106 $
-            "This only exits the subshell caused by the " ++ str ++ "."
-        _ -> return ()
-  where
-    name = getCommandName t
-    path = let p = getPath (parentMap params) t in filter relevant p
-    subshellType t = case leadType params t of
-        NoneScope -> Nothing
-        SubshellScope str -> return str
-    relevant t = isLoop t || isFunction t || isJust (subshellType t)
-checkLoopKeywordScope _ _ = return ()
-
-
-prop_checkFunctionDeclarations1 = verify checkFunctionDeclarations "#!/bin/ksh\nfunction foo() { command foo --lol \"$@\"; }"
-prop_checkFunctionDeclarations2 = verify checkFunctionDeclarations "#!/bin/dash\nfunction foo { lol; }"
-prop_checkFunctionDeclarations3 = verifyNot checkFunctionDeclarations "foo() { echo bar; }"
-checkFunctionDeclarations params
-        (T_Function id (FunctionKeyword hasKeyword) (FunctionParentheses hasParens) _ _) =
-    case shellType params of
-        Bash -> return ()
-        Ksh ->
-            when (hasKeyword && hasParens) $
-                err id 2111 "ksh does not allow 'function' keyword and '()' at the same time."
-        Dash -> forSh
-        Sh   -> forSh
-
-    where
-        forSh = do
-            when (hasKeyword && hasParens) $
-                warn id 2112 "'function' keyword is non-standard. Delete it."
-            when (hasKeyword && not hasParens) $
-                warn id 2113 "'function' keyword is non-standard. Use 'foo()' instead of 'function foo'."
-checkFunctionDeclarations _ _ = return ()
-
-
-
-prop_checkStderrPipe1 = verify checkStderrPipe "#!/bin/ksh\nfoo |& bar"
-prop_checkStderrPipe2 = verifyNot checkStderrPipe "#!/bin/bash\nfoo |& bar"
-checkStderrPipe params =
-    case shellType params of
-        Ksh -> match
-        _ -> const $ return ()
-  where
-    match (T_Pipe id "|&") =
-        err id 2118 "Ksh does not support |&. Use 2>&1 |."
-    match _ = return ()
-
-prop_checkUnpassedInFunctions1 = verifyTree checkUnpassedInFunctions "foo() { echo $1; }; foo"
-prop_checkUnpassedInFunctions2 = verifyNotTree checkUnpassedInFunctions "foo() { echo $1; };"
-prop_checkUnpassedInFunctions3 = verifyNotTree checkUnpassedInFunctions "foo() { echo $lol; }; foo"
-prop_checkUnpassedInFunctions4 = verifyNotTree checkUnpassedInFunctions "foo() { echo $0; }; foo"
-prop_checkUnpassedInFunctions5 = verifyNotTree checkUnpassedInFunctions "foo() { echo $1; }; foo 'lol'; foo"
-prop_checkUnpassedInFunctions6 = verifyNotTree checkUnpassedInFunctions "foo() { set -- *; echo $1; }; foo"
-prop_checkUnpassedInFunctions7 = verifyTree checkUnpassedInFunctions "foo() { echo $1; }; foo; foo;"
-prop_checkUnpassedInFunctions8 = verifyNotTree checkUnpassedInFunctions "foo() { echo $((1)); }; foo;"
-prop_checkUnpassedInFunctions9 = verifyNotTree checkUnpassedInFunctions "foo() { echo $(($b)); }; foo;"
-prop_checkUnpassedInFunctions10= verifyNotTree checkUnpassedInFunctions "foo() { echo $!; }; foo;"
-prop_checkUnpassedInFunctions11= verifyNotTree checkUnpassedInFunctions "foo() { bar() { echo $1; }; bar baz; }; foo;"
-prop_checkUnpassedInFunctions12= verifyNotTree checkUnpassedInFunctions "foo() { echo ${!var*}; }; foo;"
-checkUnpassedInFunctions params root =
-    execWriter $ mapM_ warnForGroup referenceGroups
-  where
-    functionMap :: Map.Map String Token
-    functionMap = Map.fromList $
-        map (\t@(T_Function _ _ _ name _) -> (name,t)) functions
-    functions = execWriter $ doAnalysis (tell . maybeToList . findFunction) root
-
-    findFunction t@(T_Function id _ _ name body) =
-        let flow = getVariableFlow params body
-        in
-          if any (isPositionalReference t) flow && not (any isPositionalAssignment flow)
-            then return t
-            else Nothing
-    findFunction _ = Nothing
-
-    isPositionalAssignment x =
-        case x of
-            Assignment (_, _, str, _) -> isPositional str
-            _ -> False
-    isPositionalReference function x =
-        case x of
-            Reference (_, t, str) -> isPositional str && t `isDirectChildOf` function
-            _ -> False
-
-    isDirectChildOf child parent = fromMaybe False $ do
-        function <- find (\x -> case x of
-            T_Function {} -> True
-            T_Script {} -> True  -- for sourced files
-            _ -> False) $
-                getPath (parentMap params) child
-        return $ getId parent == getId function
-
-    referenceList :: [(String, Bool, Token)]
-    referenceList = execWriter $
-        doAnalysis (fromMaybe (return ()) . checkCommand) root
-    checkCommand :: Token -> Maybe (Writer [(String, Bool, Token)] ())
-    checkCommand t@(T_SimpleCommand _ _ (cmd:args)) = do
-        str <- getLiteralString cmd
-        guard $ Map.member str functionMap
-        return $ tell [(str, null args, t)]
-    checkCommand _ = Nothing
-
-    isPositional str = str == "*" || str == "@"
-        || (all isDigit str && str /= "0" && str /= "")
-
-    isArgumentless (_, b, _) = b
-    referenceGroups = Map.elems $ foldr updateWith Map.empty referenceList
-    updateWith x@(name, _, _) = Map.insertWith (++) name [x]
-
-    warnForGroup group =
-        when (all isArgumentless group) $ do
-            mapM_ suggestParams group
-            warnForDeclaration group
-
-    suggestParams (name, _, thing) =
-        info (getId thing) 2119 $
-            "Use " ++ name ++ " \"$@\" if function's $1 should mean script's $1."
-    warnForDeclaration ((name, _, _):_) =
-        warn (getId . fromJust $ Map.lookup name functionMap) 2120 $
-            name ++ " references arguments, but none are ever passed."
-
-
-prop_checkOverridingPath1 = verify checkOverridingPath "PATH=\"$var/$foo\""
-prop_checkOverridingPath2 = verify checkOverridingPath "PATH=\"mydir\""
-prop_checkOverridingPath3 = verify checkOverridingPath "PATH=/cow/foo"
-prop_checkOverridingPath4 = verifyNot checkOverridingPath "PATH=/cow/foo/bin"
-prop_checkOverridingPath5 = verifyNot checkOverridingPath "PATH='/bin:/sbin'"
-prop_checkOverridingPath6 = verifyNot checkOverridingPath "PATH=\"$var/$foo\" cmd"
-prop_checkOverridingPath7 = verifyNot checkOverridingPath "PATH=$OLDPATH"
-prop_checkOverridingPath8 = verifyNot checkOverridingPath "PATH=$PATH:/stuff"
-checkOverridingPath _ (T_SimpleCommand _ vars []) =
-    mapM_ checkVar vars
-  where
-    checkVar (T_Assignment id Assign "PATH" [] word) =
-        let string = concat $ oversimplify word
-        in unless (any (`isInfixOf` string) ["/bin", "/sbin" ]) $ do
-            when ('/' `elem` string && ':' `notElem` string) $ notify id
-            when (isLiteral word && ':' `notElem` string && '/' `notElem` string) $ notify id
-    checkVar _ = return ()
-    notify id = warn id 2123 "PATH is the shell search path. Use another name."
-checkOverridingPath _ _ = return ()
-
-prop_checkTildeInPath1 = verify checkTildeInPath "PATH=\"$PATH:~/bin\""
-prop_checkTildeInPath2 = verify checkTildeInPath "PATH='~foo/bin'"
-prop_checkTildeInPath3 = verifyNot checkTildeInPath "PATH=~/bin"
-checkTildeInPath _ (T_SimpleCommand _ vars _) =
-    mapM_ checkVar vars
-  where
-    checkVar (T_Assignment id Assign "PATH" [] (T_NormalWord _ parts)) =
-        when (any (\x -> isQuoted x && hasTilde x) parts) $
-            warn id 2147 "Literal tilde in PATH works poorly across programs."
-    checkVar _ = return ()
-
-    hasTilde t = fromMaybe False (liftM2 elem (return '~') (getLiteralStringExt (const $ return "") t))
-    isQuoted T_DoubleQuoted {} = True
-    isQuoted T_SingleQuoted {} = True
-    isQuoted _ = False
-checkTildeInPath _ _ = return ()
-
-prop_checkUnsupported3 = verify checkUnsupported "#!/bin/sh\ncase foo in bar) baz ;& esac"
-prop_checkUnsupported4 = verify checkUnsupported "#!/bin/ksh\ncase foo in bar) baz ;;& esac"
-prop_checkUnsupported5 = verify checkUnsupported "#!/bin/bash\necho \"${ ls; }\""
-checkUnsupported params t =
-    when (not (null support) && (shellType params `notElem` support)) $
-        report name
- where
-    (name, support) = shellSupport t
-    report s = err (getId t) 2127 $
-        "To use " ++ s ++ ", specify #!/usr/bin/env " ++
-            (map toLower . intercalate " or " . map show $ support)
-
--- TODO: Move more of these checks here
-shellSupport t =
-  case t of
-    T_CaseExpression _ _ list -> forCase (map (\(a,_,_) -> a) list)
-    T_DollarBraceCommandExpansion {} -> ("${ ..; } command expansion", [Ksh])
-    _ -> ("", [])
-  where
-    forCase seps | CaseContinue `elem` seps = ("cases with ;;&", [Bash])
-    forCase seps | CaseFallThrough `elem` seps = ("cases with ;&", [Bash, Ksh])
-    forCase _ = ("", [])
-
-
-groupWith f = groupBy ((==) `on` f)
-
-prop_checkMultipleAppends1 = verify checkMultipleAppends "foo >> file; bar >> file; baz >> file;"
-prop_checkMultipleAppends2 = verify checkMultipleAppends "foo >> file; bar | grep f >> file; baz >> file;"
-prop_checkMultipleAppends3 = verifyNot checkMultipleAppends "foo < file; bar < file; baz < file;"
-checkMultipleAppends params t =
-    mapM_ checkList $ getCommandSequences t
-  where
-    checkList list =
-        mapM_ checkGroup (groupWith (fmap fst) $ map getTarget list)
-    checkGroup (f:_:_:_) | isJust f =
-        style (snd $ fromJust f) 2129
-            "Consider using { cmd1; cmd2; } >> file instead of individual redirects."
-    checkGroup _ = return ()
-    getTarget (T_Annotation _ _ t) = getTarget t
-    getTarget (T_Pipeline _ _ args@(_:_)) = getTarget (last args)
-    getTarget (T_Redirecting id list _) = do
-        file <- mapMaybe getAppend list !!! 0
-        return (file, id)
-    getTarget _ = Nothing
-    getAppend (T_FdRedirect _ _ (T_IoFile _ T_DGREAT {} f)) = return f
-    getAppend _ = Nothing
-
-
-prop_checkSuspiciousIFS1 = verify checkSuspiciousIFS "IFS=\"\\n\""
-prop_checkSuspiciousIFS2 = verifyNot checkSuspiciousIFS "IFS=$'\\t'"
-checkSuspiciousIFS params (T_Assignment id Assign "IFS" [] value) =
-    potentially $ do
-        str <- getLiteralString value
-        return $ check str
-  where
-    n = if shellType params == Sh then "'<literal linefeed here>'" else "$'\\n'"
-    t = if shellType params == Sh then "\"$(printf '\\t')\"" else "$'\\t'"
-    check value =
-        case value of
-            "\\n" -> suggest n
-            "/n" -> suggest n
-            "\\t" -> suggest t
-            "/t" -> suggest t
-            _ -> return ()
-    suggest r = warn id 2141 $ "Did you mean IFS=" ++ r ++ " ?"
-checkSuspiciousIFS _ _ = return ()
-
-
-prop_checkGrepQ1= verify checkShouldUseGrepQ "[[ $(foo | grep bar) ]]"
-prop_checkGrepQ2= verify checkShouldUseGrepQ "[ -z $(fgrep lol) ]"
-prop_checkGrepQ3= verify checkShouldUseGrepQ "[ -n \"$(foo | zgrep lol)\" ]"
-prop_checkGrepQ4= verifyNot checkShouldUseGrepQ "[ -z $(grep bar | cmd) ]"
-prop_checkGrepQ5= verifyNot checkShouldUseGrepQ "rm $(ls | grep file)"
-prop_checkGrepQ6= verifyNot checkShouldUseGrepQ "[[ -n $(pgrep foo) ]]"
-checkShouldUseGrepQ params t =
-    potentially $ case t of
-        TC_Nullary id _ token -> check id True token
-        TC_Unary id _ "-n" token -> check id True token
-        TC_Unary id _ "-z" token -> check id False token
-        _ -> fail "not check"
-  where
-    check id bool token = do
-        name <- getFinalGrep token
-        let op = if bool then "-n" else "-z"
-        let flip = if bool then "" else "! "
-        return . style id 2143 $
-            "Use " ++ flip ++ name ++ " -q instead of " ++
-                "comparing output with [ " ++ op ++ " .. ]."
-
-    getFinalGrep t = do
-        cmds <- getPipeline t
-        guard . not . null $ cmds
-        name <- getCommandBasename $ last cmds
-        guard . isGrep $ name
-        return name
-    getPipeline t =
-        case t of
-            T_NormalWord _ [x] -> getPipeline x
-            T_DoubleQuoted _ [x] -> getPipeline x
-            T_DollarExpansion _ [x] -> getPipeline x
-            T_Pipeline _ _ cmds -> return cmds
-            _ -> fail "unknown"
-    isGrep = (`elem` ["grep", "egrep", "fgrep", "zgrep"])
-
-prop_checkTestArgumentSplitting1 = verify checkTestArgumentSplitting "[ -e *.mp3 ]"
-prop_checkTestArgumentSplitting2 = verifyNot checkTestArgumentSplitting "[[ $a == *b* ]]"
-prop_checkTestArgumentSplitting3 = verify checkTestArgumentSplitting "[[ *.png == '' ]]"
-prop_checkTestArgumentSplitting4 = verify checkTestArgumentSplitting "[[ foo == f{o,oo,ooo} ]]"
-prop_checkTestArgumentSplitting5 = verify checkTestArgumentSplitting "[[ $@ ]]"
-prop_checkTestArgumentSplitting6 = verify checkTestArgumentSplitting "[ -e $@ ]"
-prop_checkTestArgumentSplitting7 = verify checkTestArgumentSplitting "[ $@ == $@ ]"
-prop_checkTestArgumentSplitting8 = verify checkTestArgumentSplitting "[[ $@ = $@ ]]"
-prop_checkTestArgumentSplitting9 = verifyNot checkTestArgumentSplitting "[[ foo =~ bar{1,2} ]]"
-prop_checkTestArgumentSplitting10 = verifyNot checkTestArgumentSplitting "[ \"$@\" ]"
-prop_checkTestArgumentSplitting11 = verify checkTestArgumentSplitting "[[ \"$@\" ]]"
-prop_checkTestArgumentSplitting12 = verify checkTestArgumentSplitting "[ *.png ]"
-prop_checkTestArgumentSplitting13 = verify checkTestArgumentSplitting "[ \"$@\" == \"\" ]"
-prop_checkTestArgumentSplitting14 = verify checkTestArgumentSplitting "[[ \"$@\" == \"\" ]]"
-prop_checkTestArgumentSplitting15 = verifyNot checkTestArgumentSplitting "[[ \"$*\" == \"\" ]]"
-prop_checkTestArgumentSplitting16 = verifyNot checkTestArgumentSplitting "[[ -v foo[123] ]]"
-checkTestArgumentSplitting :: Parameters -> Token -> Writer [TokenComment] ()
-checkTestArgumentSplitting _ t =
-    case t of
-        (TC_Unary _ typ op token) | isGlob token ->
-            if op == "-v"
-            then
-                when (typ == SingleBracket) $
-                    err (getId token) 2208 $
-                      "Use [[ ]] or quote arguments to -v to avoid glob expansion."
-            else
-                err (getId token) 2144 $
-                   op ++ " doesn't work with globs. Use a for loop."
-
-        (TC_Nullary _ typ token) -> do
-            checkBraces typ token
-            checkGlobs typ token
-            when (typ == DoubleBracket) $
-                checkArrays typ token
-
-        (TC_Unary _ typ op token) -> checkAll typ token
-
-        (TC_Binary _ typ op lhs rhs) ->
-            if op `elem` ["=", "==", "!=", "=~"]
-            then do
-                checkAll typ lhs
-                checkArrays typ rhs
-                checkBraces typ rhs
-            else mapM_ (checkAll typ) [lhs, rhs]
-        _ -> return ()
-  where
-    checkAll typ token = do
-        checkArrays typ token
-        checkBraces typ token
-        checkGlobs typ token
-
-    checkArrays typ token =
-        when (any isArrayExpansion $ getWordParts token) $
-            if typ == SingleBracket
-            then warn (getId token) 2198 "Arrays don't work as operands in [ ]. Use a loop (or concatenate with * instead of @)."
-            else err (getId token) 2199 "Arrays implicitly concatenate in [[ ]]. Use a loop (or explicit * instead of @)."
-
-    checkBraces typ token =
-        when (any isBraceExpansion $ getWordParts token) $
-            if typ == SingleBracket
-            then warn (getId token) 2200 "Brace expansions don't work as operands in [ ]. Use a loop."
-            else err (getId token) 2201 "Brace expansion doesn't happen in [[ ]]. Use a loop."
-
-    checkGlobs typ token =
-        when (isGlob token) $
-            if typ == SingleBracket
-            then warn (getId token) 2202 "Globs don't work as operands in [ ]. Use a loop."
-            else err (getId token) 2203 "Globs are ignored in [[ ]] except right of =/!=. Use a loop."
-
-
-prop_checkMaskedReturns1 = verify checkMaskedReturns "f() { local a=$(false); }"
-prop_checkMaskedReturns2 = verify checkMaskedReturns "declare a=$(false)"
-prop_checkMaskedReturns3 = verify checkMaskedReturns "declare a=\"`false`\""
-prop_checkMaskedReturns4 = verifyNot checkMaskedReturns "declare a; a=$(false)"
-prop_checkMaskedReturns5 = verifyNot checkMaskedReturns "f() { local -r a=$(false); }"
-checkMaskedReturns _ t@(T_SimpleCommand id _ (cmd:rest)) = potentially $ do
-    name <- getCommandName t
-    guard $ name `elem` ["declare", "export"]
-        || name == "local" && "r" `notElem` map snd (getAllFlags t)
-    return $ mapM_ checkArgs rest
-  where
-    checkArgs (T_Assignment id _ _ _ word) | any hasReturn $ getWordParts word =
-        warn id 2155 "Declare and assign separately to avoid masking return values."
-    checkArgs _ = return ()
-
-    hasReturn t = case t of
-        T_Backticked {} -> True
-        T_DollarExpansion {} -> True
-        _ -> False
-checkMaskedReturns _ _ = return ()
-
-
-prop_checkReadWithoutR1 = verify checkReadWithoutR "read -a foo"
-prop_checkReadWithoutR2 = verifyNot checkReadWithoutR "read -ar foo"
-checkReadWithoutR _ t@T_SimpleCommand {} | t `isUnqualifiedCommand` "read" =
-    unless ("r" `elem` map snd (getAllFlags t)) $
-        info (getId t) 2162 "read without -r will mangle backslashes."
-checkReadWithoutR _ _ = return ()
-
-prop_checkUncheckedCd1 = verifyTree checkUncheckedCdPushdPopd "cd ~/src; rm -r foo"
-prop_checkUncheckedCd2 = verifyNotTree checkUncheckedCdPushdPopd "cd ~/src || exit; rm -r foo"
-prop_checkUncheckedCd3 = verifyNotTree checkUncheckedCdPushdPopd "set -e; cd ~/src; rm -r foo"
-prop_checkUncheckedCd4 = verifyNotTree checkUncheckedCdPushdPopd "if cd foo; then rm foo; fi"
-prop_checkUncheckedCd5 = verifyTree checkUncheckedCdPushdPopd "if true; then cd foo; fi"
-prop_checkUncheckedCd6 = verifyNotTree checkUncheckedCdPushdPopd "cd .."
-prop_checkUncheckedCd7 = verifyNotTree checkUncheckedCdPushdPopd "#!/bin/bash -e\ncd foo\nrm bar"
-prop_checkUncheckedCd8 = verifyNotTree checkUncheckedCdPushdPopd "set -o errexit; cd foo; rm bar"
-prop_checkUncheckedPushd1 = verifyTree checkUncheckedCdPushdPopd "pushd ~/src; rm -r foo"
-prop_checkUncheckedPushd2 = verifyNotTree checkUncheckedCdPushdPopd "pushd ~/src || exit; rm -r foo"
-prop_checkUncheckedPushd3 = verifyNotTree checkUncheckedCdPushdPopd "set -e; pushd ~/src; rm -r foo"
-prop_checkUncheckedPushd4 = verifyNotTree checkUncheckedCdPushdPopd "if pushd foo; then rm foo; fi"
-prop_checkUncheckedPushd5 = verifyTree checkUncheckedCdPushdPopd "if true; then pushd foo; fi"
-prop_checkUncheckedPushd6 = verifyNotTree checkUncheckedCdPushdPopd "pushd .."
-prop_checkUncheckedPushd7 = verifyNotTree checkUncheckedCdPushdPopd "#!/bin/bash -e\npushd foo\nrm bar"
-prop_checkUncheckedPushd8 = verifyNotTree checkUncheckedCdPushdPopd "set -o errexit; pushd foo; rm bar"
-prop_checkUncheckedPushd9 = verifyNotTree checkUncheckedCdPushdPopd "pushd -n foo"
-prop_checkUncheckedPopd1 = verifyTree checkUncheckedCdPushdPopd "popd; rm -r foo"
-prop_checkUncheckedPopd2 = verifyNotTree checkUncheckedCdPushdPopd "popd || exit; rm -r foo"
-prop_checkUncheckedPopd3 = verifyNotTree checkUncheckedCdPushdPopd "set -e; popd; rm -r foo"
-prop_checkUncheckedPopd4 = verifyNotTree checkUncheckedCdPushdPopd "if popd; then rm foo; fi"
-prop_checkUncheckedPopd5 = verifyTree checkUncheckedCdPushdPopd "if true; then popd; fi"
-prop_checkUncheckedPopd6 = verifyTree checkUncheckedCdPushdPopd "popd"
-prop_checkUncheckedPopd7 = verifyNotTree checkUncheckedCdPushdPopd "#!/bin/bash -e\npopd\nrm bar"
-prop_checkUncheckedPopd8 = verifyNotTree checkUncheckedCdPushdPopd "set -o errexit; popd; rm bar"
-
-checkUncheckedCdPushdPopd params root =
-    if hasSetE params then
-        []
-    else execWriter $ doAnalysis checkElement root
-  where
-    checkElement t@T_SimpleCommand {} =
-        when(name t `elem` ["cd", "pushd", "popd"]
-            && not (isSafeDir t)
-            && not (name t == "pushd" && ("n" `elem` map snd (getAllFlags t)))
-            && not (isCondition $ getPath (parentMap params) t)) $
-                warn (getId t) 2164 "Use 'cd ... || exit' or 'cd ... || return' in case cd fails."
-    checkElement _ = return ()
-    name t = fromMaybe "" $ getCommandName t
-    isSafeDir t = case oversimplify t of
-          [_, ".."] -> True;
-          _ -> False
-
-prop_checkLoopVariableReassignment1 = verify checkLoopVariableReassignment "for i in *; do for i in *.bar; do true; done; done"
-prop_checkLoopVariableReassignment2 = verify checkLoopVariableReassignment "for i in *; do for((i=0; i<3; i++)); do true; done; done"
-prop_checkLoopVariableReassignment3 = verifyNot checkLoopVariableReassignment "for i in *; do for j in *.bar; do true; done; done"
-checkLoopVariableReassignment params token =
-    potentially $ case token of
-        T_ForIn {} -> check
-        T_ForArithmetic {} -> check
-        _ -> Nothing
-  where
-    check = do
-        str <- loopVariable token
-        next <- listToMaybe $ filter (\x -> loopVariable x == Just str) path
-        return $ do
-            warn (getId token) 2165 "This nested loop overrides the index variable of its parent."
-            warn (getId next)  2167 "This parent loop has its index variable overridden."
-    path = drop 1 $ getPath (parentMap params) token
-    loopVariable :: Token -> Maybe String
-    loopVariable t =
-        case t of
-            T_ForIn _ s _ _ -> return s
-            T_ForArithmetic _
-                (TA_Sequence _
-                    [TA_Assignment _ "="
-                        (TA_Expansion _ [T_Literal _ var]) _])
-                            _ _ _ -> return var
-            _ -> fail "not loop"
-
-prop_checkTrailingBracket1 = verify checkTrailingBracket "if -z n ]]; then true; fi "
-prop_checkTrailingBracket2 = verifyNot checkTrailingBracket "if [[ -z n ]]; then true; fi "
-prop_checkTrailingBracket3 = verify checkTrailingBracket "a || b ] && thing"
-prop_checkTrailingBracket4 = verifyNot checkTrailingBracket "run [ foo ]"
-prop_checkTrailingBracket5 = verifyNot checkTrailingBracket "run bar ']'"
-checkTrailingBracket _ token =
-    case token of
-        T_SimpleCommand _ _ tokens@(_:_) -> check (last tokens) token
-        _ -> return ()
-  where
-    check t command =
-        case t of
-            T_NormalWord id [T_Literal _ str] -> potentially $ do
-                guard $ str `elem` [ "]]", "]" ]
-                let opposite = invert str
-                    parameters = oversimplify command
-                guard $ opposite `notElem` parameters
-                return $ warn id 2171 $
-                    "Found trailing " ++ str ++ " outside test. Missing " ++ opposite ++ "?"
-            _ -> return ()
-    invert s =
-        case s of
-            "]]" -> "[["
-            "]" -> "["
-            x -> x
-
-prop_checkReturnAgainstZero1 = verify checkReturnAgainstZero "[ $? -eq 0 ]"
-prop_checkReturnAgainstZero2 = verify checkReturnAgainstZero "[[ \"$?\" -gt 0 ]]"
-prop_checkReturnAgainstZero3 = verify checkReturnAgainstZero "[[ 0 -ne $? ]]"
-prop_checkReturnAgainstZero4 = verifyNot checkReturnAgainstZero "[[ $? -eq 4 ]]"
-prop_checkReturnAgainstZero5 = verify checkReturnAgainstZero "[[ 0 -eq $? ]]"
-prop_checkReturnAgainstZero6 = verifyNot checkReturnAgainstZero "[[ $R -eq 0 ]]"
-prop_checkReturnAgainstZero7 = verify checkReturnAgainstZero "(( $? == 0 ))"
-prop_checkReturnAgainstZero8 = verify checkReturnAgainstZero "(( $? ))"
-prop_checkReturnAgainstZero9 = verify checkReturnAgainstZero "(( ! $? ))"
-checkReturnAgainstZero _ token =
-    case token of
-        TC_Binary id _ _ lhs rhs -> check lhs rhs
-        TA_Binary id _ lhs rhs -> check lhs rhs
-        TA_Unary id _ exp ->
-            when (isExitCode exp) $ message (getId exp)
-        TA_Sequence _ [exp] ->
-            when (isExitCode exp) $ message (getId exp)
-        _ -> return ()
-  where
-    check lhs rhs =
-        if isZero rhs && isExitCode lhs
-        then message (getId lhs)
-        else when (isZero lhs && isExitCode rhs) $ message (getId rhs)
-    isZero t = getLiteralString t == Just "0"
-    isExitCode t =
-        case getWordParts t of
-            [exp@T_DollarBraced {}] -> bracedString exp == "?"
-            _ -> False
-    message id = style id 2181 "Check exit code directly with e.g. 'if mycmd;', not indirectly with $?."
-
-prop_checkRedirectedNowhere1 = verify checkRedirectedNowhere "> file"
-prop_checkRedirectedNowhere2 = verify checkRedirectedNowhere "> file | grep foo"
-prop_checkRedirectedNowhere3 = verify checkRedirectedNowhere "grep foo | > bar"
-prop_checkRedirectedNowhere4 = verifyNot checkRedirectedNowhere "grep foo > bar"
-prop_checkRedirectedNowhere5 = verifyNot checkRedirectedNowhere "foo | grep bar > baz"
-prop_checkRedirectedNowhere6 = verifyNot checkRedirectedNowhere "var=$(value) 2> /dev/null"
-prop_checkRedirectedNowhere7 = verifyNot checkRedirectedNowhere "var=$(< file)"
-prop_checkRedirectedNowhere8 = verifyNot checkRedirectedNowhere "var=`< file`"
-checkRedirectedNowhere params token =
-    case token of
-        T_Pipeline _ _ [single] -> potentially $ do
-            redir <- getDanglingRedirect single
-            guard . not $ isInExpansion token
-            return $ warn (getId redir) 2188 "This redirection doesn't have a command. Move to its command (or use 'true' as no-op)."
-
-        T_Pipeline _ _ list -> forM_ list $ \x -> potentially $ do
-            redir <- getDanglingRedirect x
-            return $ err (getId redir) 2189 "You can't have | between this redirection and the command it should apply to."
-
-        _ -> return ()
-  where
-    isInExpansion t =
-        case drop 1 $ getPath (parentMap params) t of
-            T_DollarExpansion _ [_] : _ -> True
-            T_Backticked _ [_] : _ -> True
-            T_Annotation _ _ u : _ -> isInExpansion u
-            _ -> False
-    getDanglingRedirect token =
-        case token of
-            T_Redirecting _ (first:_) (T_SimpleCommand _ [] []) -> return first
-            _ -> Nothing
-
-
-prop_checkArrayAssignmentIndices1 = verifyTree checkArrayAssignmentIndices "declare -A foo; foo=(bar)"
-prop_checkArrayAssignmentIndices2 = verifyNotTree checkArrayAssignmentIndices "declare -a foo; foo=(bar)"
-prop_checkArrayAssignmentIndices3 = verifyNotTree checkArrayAssignmentIndices "declare -A foo; foo=([i]=bar)"
-prop_checkArrayAssignmentIndices4 = verifyTree checkArrayAssignmentIndices "typeset -A foo; foo+=(bar)"
-prop_checkArrayAssignmentIndices5 = verifyTree checkArrayAssignmentIndices "arr=( [foo]= bar )"
-prop_checkArrayAssignmentIndices6 = verifyTree checkArrayAssignmentIndices "arr=( [foo] = bar )"
-prop_checkArrayAssignmentIndices7 = verifyTree checkArrayAssignmentIndices "arr=( var=value )"
-prop_checkArrayAssignmentIndices8 = verifyNotTree checkArrayAssignmentIndices "arr=( [foo]=bar )"
-prop_checkArrayAssignmentIndices9 = verifyNotTree checkArrayAssignmentIndices "arr=( [foo]=\"\" )"
-checkArrayAssignmentIndices params root =
-    runNodeAnalysis check params root
-  where
-    assocs = getAssociativeArrays root
-    check _ t =
-        case t of
-            T_Assignment _ _ name [] (T_Array _ list) ->
-                let isAssoc = name `elem` assocs in
-                    mapM_ (checkElement isAssoc) list
-            _ -> return ()
-
-    checkElement isAssociative t =
-        case t of
-            T_IndexedElement _ _ (T_Literal id "") ->
-                warn id 2192 "This array element has no value. Remove spaces after = or use \"\" for empty string."
-            T_IndexedElement {} ->
-                return ()
-
-            T_NormalWord _ parts ->
-                let literalEquals = do
-                    part <- parts
-                    (id, str) <- case part of
-                        T_Literal id str -> [(id,str)]
-                        _ -> []
-                    guard $ '=' `elem` str
-                    return $ warn id 2191 "The = here is literal. To assign by index, use ( [index]=value ) with no spaces. To keep as literal, quote it."
-                in
-                    if null literalEquals && isAssociative
-                    then warn (getId t) 2190 "Elements in associative arrays need index, e.g. array=( [index]=value ) ."
-                    else sequence_ literalEquals
-
-            _ -> return ()
-
-prop_checkUnmatchableCases1 = verify checkUnmatchableCases "case foo in bar) true; esac"
-prop_checkUnmatchableCases2 = verify checkUnmatchableCases "case foo-$bar in ??|*) true; esac"
-prop_checkUnmatchableCases3 = verify checkUnmatchableCases "case foo in foo) true; esac"
-prop_checkUnmatchableCases4 = verifyNot checkUnmatchableCases "case foo-$bar in foo*|*bar|*baz*) true; esac"
-prop_checkUnmatchableCases5 = verify checkUnmatchableCases "case $f in *.txt) true;; f??.txt) false;; esac"
-prop_checkUnmatchableCases6 = verifyNot checkUnmatchableCases "case $f in ?*) true;; *) false;; esac"
-prop_checkUnmatchableCases7 = verifyNot checkUnmatchableCases "case $f in $(x)) true;; asdf) false;; esac"
-prop_checkUnmatchableCases8 = verify checkUnmatchableCases "case $f in cow) true;; bar|cow) false;; esac"
-checkUnmatchableCases _ t =
-    case t of
-        T_CaseExpression _ word list -> do
-            let patterns = concatMap snd3 list
-
-            if isConstant word
-                then warn (getId word) 2194
-                        "This word is constant. Did you forget the $ on a variable?"
-                else  potentially $ do
-                    pg <- wordToPseudoGlob word
-                    return $ mapM_ (check pg) patterns
-
-            let exactGlobs = tupMap wordToExactPseudoGlob patterns
-            let fuzzyGlobs = tupMap wordToPseudoGlob patterns
-            let dominators = zip exactGlobs (tails $ drop 1 fuzzyGlobs)
-
-            mapM_ checkDoms dominators
-
-        _ -> return ()
-  where
-    snd3 (_,x,_) = x
-    check target candidate = potentially $ do
-        candidateGlob <- wordToPseudoGlob candidate
-        guard . not $ pseudoGlobsCanOverlap target candidateGlob
-        return $ warn (getId candidate) 2195
-                    "This pattern will never match the case statement's word. Double check them."
-
-    tupMap f l = zip l (map f l)
-    checkDoms ((glob, Just x), rest) =
-        case filter (\(_, p) -> x `pseudoGlobIsSuperSetof` p) valids of
-            ((first,_):_) -> do
-                warn (getId glob) 2221 "This pattern always overrides a later one."
-                warn (getId first) 2222 "This pattern never matches because of a previous pattern."
-            _ -> return ()
-      where
-        valids = concatMap f rest
-        f (x, Just y) = [(x,y)]
-        f _ = []
-    checkDoms _ = return ()
-
-
-prop_checkSubshellAsTest1 = verify checkSubshellAsTest "( -e file )"
-prop_checkSubshellAsTest2 = verify checkSubshellAsTest "( 1 -gt 2 )"
-prop_checkSubshellAsTest3 = verifyNot checkSubshellAsTest "( grep -c foo bar )"
-prop_checkSubshellAsTest4 = verifyNot checkSubshellAsTest "[ 1 -gt 2 ]"
-prop_checkSubshellAsTest5 = verify checkSubshellAsTest "( -e file && -x file )"
-prop_checkSubshellAsTest6 = verify checkSubshellAsTest "( -e file || -x file && -t 1 )"
-prop_checkSubshellAsTest7 = verify checkSubshellAsTest "( ! -d file )"
-checkSubshellAsTest _ t =
-    case t of
-        T_Subshell id [w] -> check id w
-        _ -> return ()
-  where
-    check id t = case t of
-        (T_Banged _ w) -> check id w
-        (T_AndIf _ w _) -> check id w
-        (T_OrIf _ w _) -> check id w
-        (T_Pipeline _ _ [T_Redirecting _ _ (T_SimpleCommand _ [] (first:second:_))]) ->
-            checkParams id first second
-        _ -> return ()
-
-
-    checkParams id first second = do
-        when (fromMaybe False $ (`elem` unaryTestOps) <$> getLiteralString first) $
-            err id 2204 "(..) is a subshell. Did you mean [ .. ], a test expression?"
-        when (fromMaybe False $ (`elem` binaryTestOps) <$> getLiteralString second) $
-            warn id 2205 "(..) is a subshell. Did you mean [ .. ], a test expression?"
-
-
-prop_checkSplittingInArrays1 = verify checkSplittingInArrays "a=( $var )"
-prop_checkSplittingInArrays2 = verify checkSplittingInArrays "a=( $(cmd) )"
-prop_checkSplittingInArrays3 = verifyNot checkSplittingInArrays "a=( \"$var\" )"
-prop_checkSplittingInArrays4 = verifyNot checkSplittingInArrays "a=( \"$(cmd)\" )"
-prop_checkSplittingInArrays5 = verifyNot checkSplittingInArrays "a=( $! $$ $# )"
-prop_checkSplittingInArrays6 = verifyNot checkSplittingInArrays "a=( ${#arr[@]} )"
-prop_checkSplittingInArrays7 = verifyNot checkSplittingInArrays "a=( foo{1,2} )"
-prop_checkSplittingInArrays8 = verifyNot checkSplittingInArrays "a=( * )"
-checkSplittingInArrays params t =
-    case t of
-        T_Array _ elements -> mapM_ check elements
-        _ -> return ()
-  where
-    check word = case word of
-        T_NormalWord _ parts -> mapM_ checkPart parts
-        _ -> return ()
-    checkPart part = case part of
-        T_DollarExpansion id _ -> forCommand id
-        T_DollarBraceCommandExpansion id _ -> forCommand id
-        T_Backticked id _ -> forCommand id
-        T_DollarBraced id str |
-            not (isCountingReference part)
-            && not (isQuotedAlternativeReference part)
-            && not (getBracedReference (bracedString part) `elem` variablesWithoutSpaces)
-            -> warn id 2206 $
-                if shellType params == Ksh
-                then "Quote to prevent word splitting, or split robustly with read -A or while read."
-                else "Quote to prevent word splitting, or split robustly with mapfile or read -a."
-        _ -> return ()
-
-    forCommand id =
-        warn id 2207 $
-            if shellType params == Ksh
-            then "Prefer read -A or while read to split command output (or quote to avoid splitting)."
-            else "Prefer mapfile or read -a to split command output (or quote to avoid splitting)."
-
-
-prop_checkRedirectionToNumber1 = verify checkRedirectionToNumber "( 1 > 2 )"
-prop_checkRedirectionToNumber2 = verify checkRedirectionToNumber "foo 1>2"
-prop_checkRedirectionToNumber3 = verifyNot checkRedirectionToNumber "echo foo > '2'"
-prop_checkRedirectionToNumber4 = verifyNot checkRedirectionToNumber "foo 1>&2"
-checkRedirectionToNumber _ t = case t of
-    T_IoFile id _ word -> potentially $ do
-        file <- getUnquotedLiteral word
-        guard $ all isDigit file
-        return $ warn id 2210 "This is a file redirection. Was it supposed to be a comparison or fd operation?"
-    _ -> return ()
-
-prop_checkGlobAsCommand1 = verify checkGlobAsCommand "foo*"
-prop_checkGlobAsCommand2 = verify checkGlobAsCommand "$(var[i])"
-prop_checkGlobAsCommand3 = verifyNot checkGlobAsCommand "echo foo*"
-checkGlobAsCommand _ t = case t of
-    T_SimpleCommand _ _ (first:_) ->
-        when (isGlob first) $
-            warn (getId first) 2211 "This is a glob used as a command name. Was it supposed to be in ${..}, array, or is it missing quoting?"
-    _ -> return ()
-
-
-prop_checkFlagAsCommand1 = verify checkFlagAsCommand "-e file"
-prop_checkFlagAsCommand2 = verify checkFlagAsCommand "foo\n  --bar=baz"
-prop_checkFlagAsCommand3 = verifyNot checkFlagAsCommand "'--myexec--' args"
-prop_checkFlagAsCommand4 = verifyNot checkFlagAsCommand "var=cmd --arg"  -- Handled by SC2037
-checkFlagAsCommand _ t = case t of
-    T_SimpleCommand _ [] (first:_) ->
-        when (isUnquotedFlag first) $
-            warn (getId first) 2215 "This flag is used as a command name. Bad line break or missing [ .. ]?"
-    _ -> return ()
-
-
-prop_checkEmptyCondition1 = verify checkEmptyCondition "if [ ]; then ..; fi"
-prop_checkEmptyCondition2 = verifyNot checkEmptyCondition "[ foo -o bar ]"
-checkEmptyCondition _ t = case t of
-    TC_Empty id _ -> style id 2212 "Use 'false' instead of empty [/[[ conditionals."
-    _ -> return ()
-
-prop_checkPipeToNowhere1 = verify checkPipeToNowhere "foo | echo bar"
-prop_checkPipeToNowhere2 = verify checkPipeToNowhere "basename < file.txt"
-prop_checkPipeToNowhere3 = verify checkPipeToNowhere "printf 'Lol' <<< str"
-prop_checkPipeToNowhere4 = verify checkPipeToNowhere "printf 'Lol' << eof\nlol\neof\n"
-prop_checkPipeToNowhere5 = verifyNot checkPipeToNowhere "echo foo | xargs du"
-prop_checkPipeToNowhere6 = verifyNot checkPipeToNowhere "ls | echo $(cat)"
-prop_checkPipeToNowhere7 = verifyNot checkPipeToNowhere "echo foo | var=$(cat) ls"
-checkPipeToNowhere :: Parameters -> Token -> WriterT [TokenComment] Identity ()
-checkPipeToNowhere _ t =
-    case t of
-        T_Pipeline _ _ (first:rest) -> mapM_ checkPipe rest
-        T_Redirecting _ redirects cmd -> when (any redirectsStdin redirects) $ checkRedir cmd
-        _ -> return ()
-  where
-    checkPipe redir = potentially $ do
-        cmd <- getCommand redir
-        name <- getCommandBasename cmd
-        guard $ name `elem` nonReadingCommands
-        guard . not $ hasAdditionalConsumers cmd
-        return $ warn (getId cmd) 2216 $
-            "Piping to '" ++ name ++ "', a command that doesn't read stdin. Wrong command or missing xargs?"
-
-    checkRedir cmd = potentially $ do
-        name <- getCommandBasename cmd
-        guard $ name `elem` nonReadingCommands
-        guard . not $ hasAdditionalConsumers cmd
-        return $ warn (getId cmd) 2217 $
-            "Redirecting to '" ++ name ++ "', a command that doesn't read stdin. Bad quoting or missing xargs?"
-
-    -- Could any words in a SimpleCommand consume stdin (e.g. echo "$(cat)")?
-    hasAdditionalConsumers t = fromMaybe True $ do
-        doAnalysis (guard . not . mayConsume) t
-        return False
-
-    mayConsume t =
-        case t of
-            T_ProcSub {} -> True
-            T_Backticked {} -> True
-            T_DollarExpansion {} -> True
-            _ -> False
-
-    redirectsStdin t =
-        case t of
-            T_FdRedirect _ _ (T_IoFile _ T_Less {} _) -> True
-            T_FdRedirect _ _ T_HereDoc {} -> True
-            T_FdRedirect _ _ T_HereString {} -> True
-            _ -> False
-
-prop_checkUseBeforeDefinition1 = verifyTree checkUseBeforeDefinition "f; f() { true; }"
-prop_checkUseBeforeDefinition2 = verifyNotTree checkUseBeforeDefinition "f() { true; }; f"
-prop_checkUseBeforeDefinition3 = verifyNotTree checkUseBeforeDefinition "if ! mycmd --version; then mycmd() { true; }; fi"
-prop_checkUseBeforeDefinition4 = verifyNotTree checkUseBeforeDefinition "mycmd || mycmd() { f; }"
-checkUseBeforeDefinition _ t =
-    execWriter $ evalStateT (mapM_ examine $ revCommands) Map.empty
-  where
-    examine t = case t of
-        T_Pipeline _ _ [T_Redirecting _ _ (T_Function _ _ _ name _)] ->
-            modify $ Map.insert name t
-        T_Annotation _ _ w -> examine w
-        T_Pipeline _ _ cmds -> do
-            m <- get
-            unless (Map.null m) $
-                mapM_ (checkUsage m) $ concatMap recursiveSequences cmds
-        _ -> return ()
-
-    checkUsage map cmd = potentially $ do
-        name <- getCommandName cmd
-        def <- Map.lookup name map
-        return $
-            err (getId cmd) 2218
-                "This function is only defined later. Move the definition up."
-
-    revCommands = reverse $ concat $ getCommandSequences t
-    recursiveSequences x =
-        let list = concat $ getCommandSequences x in
-            if null list
-            then [x]
-            else concatMap recursiveSequences list
-
-return []
-runTests =  $( [| $(forAllProperties) (quickCheckWithResult (stdArgs { maxSuccess = 1 }) ) |])
diff --git a/ShellCheck/Analyzer.hs b/ShellCheck/Analyzer.hs
deleted file mode 100644
--- a/ShellCheck/Analyzer.hs
+++ /dev/null
@@ -1,45 +0,0 @@
-{-
-    Copyright 2012-2015 Vidar Holen
-
-    This file is part of ShellCheck.
-    http://www.vidarholen.net/contents/shellcheck
-
-    ShellCheck is free software: you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation, either version 3 of the License, or
-    (at your option) any later version.
-
-    ShellCheck is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program.  If not, see <http://www.gnu.org/licenses/>.
--}
-module ShellCheck.Analyzer (analyzeScript) where
-
-import ShellCheck.Analytics
-import ShellCheck.AnalyzerLib
-import ShellCheck.Interface
-import Data.List
-import Data.Monoid
-import qualified ShellCheck.Checks.Commands
-import qualified ShellCheck.Checks.ShellSupport
-
-
--- TODO: Clean up the cruft this is layered on
-analyzeScript :: AnalysisSpec -> AnalysisResult
-analyzeScript spec = AnalysisResult {
-    arComments =
-        filterByAnnotation spec params . nub $
-            runAnalytics spec
-            ++ runChecker params (checkers params)
-}
-  where
-    params = makeParameters spec
-
-checkers params = mconcat $ map ($ params) [
-    ShellCheck.Checks.Commands.checker,
-    ShellCheck.Checks.ShellSupport.checker
-    ]
diff --git a/ShellCheck/AnalyzerLib.hs b/ShellCheck/AnalyzerLib.hs
deleted file mode 100644
--- a/ShellCheck/AnalyzerLib.hs
+++ /dev/null
@@ -1,843 +0,0 @@
-{-
-    Copyright 2012-2015 Vidar Holen
-
-    This file is part of ShellCheck.
-    http://www.vidarholen.net/contents/shellcheck
-
-    ShellCheck is free software: you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation, either version 3 of the License, or
-    (at your option) any later version.
-
-    ShellCheck is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program.  If not, see <http://www.gnu.org/licenses/>.
--}
-{-# LANGUAGE TemplateHaskell #-}
-{-# LANGUAGE FlexibleContexts #-}
-module ShellCheck.AnalyzerLib where
-import ShellCheck.AST
-import ShellCheck.ASTLib
-import ShellCheck.Data
-import ShellCheck.Interface
-import ShellCheck.Parser
-import ShellCheck.Regex
-
-import Control.Arrow (first)
-import Control.Monad.Identity
-import Control.Monad.RWS
-import Control.Monad.State
-import Control.Monad.Writer
-import Data.Char
-import Data.List
-import Data.Maybe
-import qualified Data.Map as Map
-
-import Test.QuickCheck.All (forAllProperties)
-import Test.QuickCheck.Test (quickCheckWithResult, stdArgs, maxSuccess)
-
-type Analysis = AnalyzerM ()
-type AnalyzerM a = RWS Parameters [TokenComment] Cache a
-nullCheck = const $ return ()
-
-
-data Checker = Checker {
-    perScript :: Root -> Analysis,
-    perToken :: Token -> Analysis
-}
-
-runChecker :: Parameters -> Checker -> [TokenComment]
-runChecker params checker = notes
-    where
-        root = rootNode params
-        check = perScript checker `composeAnalyzers` (\(Root x) -> void $ doAnalysis (perToken checker) x)
-        notes = snd $ evalRWS (check $ Root root) params Cache
-
-instance Monoid Checker where
-    mempty = Checker {
-        perScript = nullCheck,
-        perToken = nullCheck
-        }
-    mappend x y = Checker {
-        perScript = perScript x `composeAnalyzers` perScript y,
-        perToken = perToken x `composeAnalyzers` perToken y
-        }
-
-
-composeAnalyzers :: (a -> Analysis) -> (a -> Analysis) -> a -> Analysis
-composeAnalyzers f g x = f x >> g x
-
-data Parameters = Parameters {
-    hasLastpipe :: Bool,           -- Whether this script has the 'lastpipe' option set/default.
-    hasSetE     :: Bool,           -- Whether this script has 'set -e' anywhere.
-    variableFlow :: [StackData],   -- A linear (bad) analysis of data flow
-    parentMap :: Map.Map Id Token, -- A map from Id to parent Token
-    shellType :: Shell,            -- The shell type, such as Bash or Ksh
-    shellTypeSpecified :: Bool,    -- True if shell type was forced via flags
-    rootNode :: Token              -- The root node of the AST
-    }
-
--- TODO: Cache results of common AST ops here
-data Cache = Cache {}
-
-data Scope = SubshellScope String | NoneScope deriving (Show, Eq)
-data StackData =
-    StackScope Scope
-    | StackScopeEnd
-    -- (Base expression, specific position, var name, assigned values)
-    | Assignment (Token, Token, String, DataType)
-    | Reference (Token, Token, String)
-  deriving (Show)
-
-data DataType = DataString DataSource | DataArray DataSource
-  deriving (Show)
-
-data DataSource =
-    SourceFrom [Token]
-    | SourceExternal
-    | SourceDeclaration
-    | SourceInteger
-    | SourceChecked
-  deriving (Show)
-
-data VariableState = Dead Token String | Alive deriving (Show)
-
-defaultSpec root = AnalysisSpec {
-    asScript = root,
-    asShellType = Nothing,
-    asCheckSourced = False,
-    asExecutionMode = Executed
-}
-
-pScript s =
-  let
-    pSpec = ParseSpec {
-        psFilename = "script",
-        psScript = s,
-        psCheckSourced = False
-    }
-  in prRoot . runIdentity $ parseScript (mockedSystemInterface []) pSpec
-
--- For testing. If parsed, returns whether there are any comments
-producesComments :: Checker -> String -> Maybe Bool
-producesComments c s = do
-        root <- pScript s
-        let spec = defaultSpec root
-        let params = makeParameters spec
-        return . not . null $ runChecker params c
-
-makeComment :: Severity -> Id -> Code -> String -> TokenComment
-makeComment severity id code note =
-    TokenComment id $ Comment severity code note
-
-addComment note = tell [note]
-
-warn :: MonadWriter [TokenComment] m => Id -> Code -> String -> m ()
-warn  id code str = addComment $ makeComment WarningC id code str
-err   id code str = addComment $ makeComment ErrorC id code str
-info  id code str = addComment $ makeComment InfoC id code str
-style id code str = addComment $ makeComment StyleC id code str
-
-makeParameters spec =
-    let params = Parameters {
-        rootNode = root,
-        shellType = fromMaybe (determineShell root) $ asShellType spec,
-        hasSetE = containsSetE root,
-        hasLastpipe =
-            case shellType params of
-                Bash -> containsLastpipe root
-                Dash -> False
-                Sh -> False
-                Ksh -> True,
-
-        shellTypeSpecified = isJust $ asShellType spec,
-        parentMap = getParentTree root,
-        variableFlow = getVariableFlow params root
-    } in params
-  where root = asScript spec
-
-
--- Does this script mention 'set -e' anywhere?
--- Used as a hack to disable certain warnings.
-containsSetE root = isNothing $ doAnalysis (guard . not . isSetE) root
-  where
-    isSetE t =
-        case t of
-            T_Script _ str _ -> str `matches` re
-            T_SimpleCommand {}  ->
-                t `isUnqualifiedCommand` "set" &&
-                    ("errexit" `elem` oversimplify t ||
-                        "e" `elem` map snd (getAllFlags t))
-            _ -> False
-    re = mkRegex "[[:space:]]-[^-]*e"
-
--- Does this script mention 'shopt -s lastpipe' anywhere?
--- Also used as a hack.
-containsLastpipe root =
-        isNothing $ doAnalysis (guard . not . isShoptLastPipe) root
-    where
-        isShoptLastPipe t =
-            case t of
-                T_SimpleCommand {}  ->
-                    t `isUnqualifiedCommand` "shopt" &&
-                        ("lastpipe" `elem` oversimplify t)
-                _ -> False
-
-
-prop_determineShell0 = determineShell (fromJust $ pScript "#!/bin/sh") == Sh
-prop_determineShell1 = determineShell (fromJust $ pScript "#!/usr/bin/env ksh") == Ksh
-prop_determineShell2 = determineShell (fromJust $ pScript "") == Bash
-prop_determineShell3 = determineShell (fromJust $ pScript "#!/bin/sh -e") == Sh
-prop_determineShell4 = determineShell (fromJust $ pScript
-    "#!/bin/ksh\n#shellcheck shell=sh\nfoo") == Sh
-prop_determineShell5 = determineShell (fromJust $ pScript
-    "#shellcheck shell=sh\nfoo") == Sh
-prop_determineShell6 = determineShell (fromJust $ pScript "#! /bin/sh") == Sh
-prop_determineShell7 = determineShell (fromJust $ pScript "#! /bin/ash") == Dash
-determineShell t = fromMaybe Bash $ do
-    shellString <- foldl mplus Nothing $ getCandidates t
-    shellForExecutable shellString
-  where
-    forAnnotation t =
-        case t of
-            (ShellOverride s) -> return s
-            _ -> fail ""
-    getCandidates :: Token -> [Maybe String]
-    getCandidates t@T_Script {} = [Just $ fromShebang t]
-    getCandidates (T_Annotation _ annotations s) =
-        map forAnnotation annotations ++
-           [Just $ fromShebang s]
-    fromShebang (T_Script _ s t) = executableFromShebang s
-
--- Given a string like "/bin/bash" or "/usr/bin/env dash",
--- return the shell basename like "bash" or "dash"
-executableFromShebang :: String -> String
-executableFromShebang = shellFor
-  where
-    shellFor s | "/env " `isInfixOf` s = head (drop 1 (words s)++[""])
-    shellFor s | ' ' `elem` s = shellFor $ takeWhile (/= ' ') s
-    shellFor s = reverse . takeWhile (/= '/') . reverse $ s
-
-
-
--- Given a root node, make a map from Id to parent Token.
--- This is used to populate parentMap in Parameters
-getParentTree :: Token -> Map.Map Id Token
-getParentTree t =
-    snd . snd $ runState (doStackAnalysis pre post t) ([], Map.empty)
-  where
-    pre t = modify (first ((:) t))
-    post t = do
-        (_:rest, map) <- get
-        case rest of [] -> put (rest, map)
-                     (x:_) -> put (rest, Map.insert (getId t) x map)
-
--- Given a root node, make a map from Id to Token
-getTokenMap :: Token -> Map.Map Id Token
-getTokenMap t =
-    execState (doAnalysis f t) Map.empty
-  where
-    f t = modify (Map.insert (getId t) t)
-
-
--- Is this token in a quoting free context? (i.e. would variable expansion split)
--- True:  Assignments, [[ .. ]], here docs, already in double quotes
--- False: Regular words
-isStrictlyQuoteFree = isQuoteFreeNode True
-
--- Like above, but also allow some cases where splitting may be desired.
--- True:  Like above + for loops
--- False: Like above
-isQuoteFree = isQuoteFreeNode False
-
-
-isQuoteFreeNode strict tree t =
-    (isQuoteFreeElement t == Just True) ||
-        head (mapMaybe isQuoteFreeContext (drop 1 $ getPath tree t) ++ [False])
-  where
-    -- Is this node self-quoting in itself?
-    isQuoteFreeElement t =
-        case t of
-            T_Assignment {} -> return True
-            T_FdRedirect {} -> return True
-            _ -> Nothing
-
-    -- Are any subnodes inherently self-quoting?
-    isQuoteFreeContext t =
-        case t of
-            TC_Nullary _ DoubleBracket _ -> return True
-            TC_Unary _ DoubleBracket _ _ -> return True
-            TC_Binary _ DoubleBracket _ _ _ -> return True
-            TA_Sequence {} -> return True
-            T_Arithmetic {} -> return True
-            T_Assignment {} -> return True
-            T_Redirecting {} -> return False
-            T_DoubleQuoted _ _ -> return True
-            T_DollarDoubleQuoted _ _ -> return True
-            T_CaseExpression {} -> return True
-            T_HereDoc {} -> return True
-            T_DollarBraced {} -> return True
-            -- When non-strict, pragmatically assume it's desirable to split here
-            T_ForIn {} -> return (not strict)
-            T_SelectIn {} -> return (not strict)
-            _ -> Nothing
-
--- Check if a token is a parameter to a certain command by name:
--- Example: isParamTo (parentMap params) "sed" t
-isParamTo :: Map.Map Id Token -> String -> Token -> Bool
-isParamTo tree cmd =
-    go
-  where
-    go x = case Map.lookup (getId x) tree of
-                Nothing -> False
-                Just parent -> check parent
-    check t =
-        case t of
-            T_SingleQuoted _ _ -> go t
-            T_DoubleQuoted _ _ -> go t
-            T_NormalWord _ _ -> go t
-            T_SimpleCommand {} -> isCommand t cmd
-            T_Redirecting {} -> isCommand t cmd
-            _ -> False
-
--- Get the parent command (T_Redirecting) of a Token, if any.
-getClosestCommand :: Map.Map Id Token -> Token -> Maybe Token
-getClosestCommand tree t =
-    findFirst findCommand $ getPath tree t
-  where
-    findCommand t =
-        case t of
-            T_Redirecting {} -> return True
-            T_Script {} -> return False
-            _ -> Nothing
-
--- Like above, if koala_man knew Haskell when starting this project.
-getClosestCommandM t = do
-    tree <- asks parentMap
-    return $ getClosestCommand tree t
-
--- Is the token used as a command name (the first word in a T_SimpleCommand)?
-usedAsCommandName tree token = go (getId token) (tail $ getPath tree token)
-  where
-    go currentId (T_NormalWord id [word]:rest)
-        | currentId == getId word = go id rest
-    go currentId (T_DoubleQuoted id [word]:rest)
-        | currentId == getId word = go id rest
-    go currentId (T_SimpleCommand _ _ (word:_):_)
-        | currentId == getId word = True
-    go _ _ = False
-
--- A list of the element and all its parents up to the root node.
-getPath tree t = t :
-    case Map.lookup (getId t) tree of
-        Nothing -> []
-        Just parent -> getPath tree parent
-
--- Version of the above taking the map from the current context
--- Todo: give this the name "getPath"
-getPathM t = do
-    map <- asks parentMap
-    return $ getPath map t
-
-isParentOf tree parent child =
-    elem (getId parent) . map getId $ getPath tree child
-
-parents params = getPath (parentMap params)
-
-pathTo t = do
-    parents <- reader parentMap
-    return $ getPath parents t
-
--- Find the first match in a list where the predicate is Just True.
--- Stops if it's Just False and ignores Nothing.
-findFirst :: (a -> Maybe Bool) -> [a] -> Maybe a
-findFirst p l =
-    case l of
-        [] -> Nothing
-        (x:xs) ->
-            case p x of
-                Just True -> return x
-                Just False -> Nothing
-                Nothing -> findFirst p xs
-
--- Check whether a word is entirely output from a single command
-tokenIsJustCommandOutput t = case t of
-    T_NormalWord id [T_DollarExpansion _ cmds] -> check cmds
-    T_NormalWord id [T_DoubleQuoted _ [T_DollarExpansion _ cmds]] -> check cmds
-    T_NormalWord id [T_Backticked _ cmds] -> check cmds
-    T_NormalWord id [T_DoubleQuoted _ [T_Backticked _ cmds]] -> check cmds
-    _ -> False
-  where
-    check [x] = not $ isOnlyRedirection x
-    check _ = False
-
--- TODO: Replace this with a proper Control Flow Graph
-getVariableFlow params t =
-    let (_, stack) = runState (doStackAnalysis startScope endScope t) []
-    in reverse stack
-  where
-    startScope t =
-        let scopeType = leadType params t
-        in do
-            when (scopeType /= NoneScope) $ modify (StackScope scopeType:)
-            when (assignFirst t) $ setWritten t
-
-    endScope t =
-        let scopeType = leadType params t
-        in do
-            setRead t
-            unless (assignFirst t) $ setWritten t
-            when (scopeType /= NoneScope) $ modify (StackScopeEnd:)
-
-    assignFirst T_ForIn {} = True
-    assignFirst T_SelectIn {} = True
-    assignFirst _ = False
-
-    setRead t =
-        let read    = getReferencedVariables (parentMap params) t
-        in mapM_ (\v -> modify (Reference v:)) read
-
-    setWritten t =
-        let written = getModifiedVariables t
-        in mapM_ (\v -> modify (Assignment v:)) written
-
-
-leadType params t =
-    case t of
-        T_DollarExpansion _ _  -> SubshellScope "$(..) expansion"
-        T_Backticked _ _  -> SubshellScope "`..` expansion"
-        T_Backgrounded _ _  -> SubshellScope "backgrounding &"
-        T_Subshell _ _  -> SubshellScope "(..) group"
-        T_CoProcBody _ _  -> SubshellScope "coproc"
-        T_Redirecting {}  ->
-            if fromMaybe False causesSubshell
-            then SubshellScope "pipeline"
-            else NoneScope
-        _ -> NoneScope
-  where
-    parentPipeline = do
-        parent <- Map.lookup (getId t) (parentMap params)
-        case parent of
-            T_Pipeline {} -> return parent
-            _ -> Nothing
-
-    causesSubshell = do
-        (T_Pipeline _ _ list) <- parentPipeline
-        if length list <= 1
-            then return False
-            else if not $ hasLastpipe params
-                then return True
-                else return . not $ (getId . head $ reverse list) == getId t
-
-getModifiedVariables t =
-    case t of
-        T_SimpleCommand _ vars [] ->
-            concatMap (\x -> case x of
-                                T_Assignment id _ name _ w  ->
-                                    [(x, x, name, dataTypeFrom DataString w)]
-                                _ -> []
-                      ) vars
-        c@T_SimpleCommand {} ->
-            getModifiedVariableCommand c
-
-        TA_Unary _ "++|" var -> maybeToList $ do
-            name <- getLiteralString var
-            return (t, t, name, DataString $ SourceFrom [t])
-        TA_Unary _ "|++" var -> maybeToList $ do
-            name <- getLiteralString var
-            return (t, t, name, DataString $ SourceFrom [t])
-        TA_Assignment _ op lhs rhs -> maybeToList $ do
-            guard $ op `elem` ["=", "*=", "/=", "%=", "+=", "-=", "<<=", ">>=", "&=", "^=", "|="]
-            name <- getLiteralString lhs
-            return (t, t, name, DataString $ SourceFrom [rhs])
-
-        -- Count [[ -v foo ]] as an "assignment".
-        -- This is to prevent [ -v foo ] being unassigned or unused.
-        TC_Unary id _ "-v" token -> maybeToList $ do
-            str <- fmap (takeWhile (/= '[')) $ -- Quoted index
-                    flip getLiteralStringExt token $ \x ->
-                case x of
-                    T_Glob _ s -> return s -- Unquoted index
-                    _ -> Nothing
-
-            guard . not . null $ str
-            return (t, token, str, DataString $ SourceChecked)
-
-        T_DollarBraced _ l -> maybeToList $ do
-            let string = bracedString t
-            let modifier = getBracedModifier string
-            guard $ ":=" `isPrefixOf` modifier
-            return (t, t, getBracedReference string, DataString $ SourceFrom [l])
-
-        t@(T_FdRedirect _ ('{':var) op) -> -- {foo}>&2 modifies foo
-            [(t, t, takeWhile (/= '}') var, DataString SourceInteger) | not $ isClosingFileOp op]
-
-        t@(T_CoProc _ name _) ->
-            [(t, t, fromMaybe "COPROC" name, DataArray SourceInteger)]
-
-        --Points to 'for' rather than variable
-        T_ForIn id str [] _ -> [(t, t, str, DataString SourceExternal)]
-        T_ForIn id str words _ -> [(t, t, str, DataString $ SourceFrom words)]
-        T_SelectIn id str words _ -> [(t, t, str, DataString $ SourceFrom words)]
-        _ -> []
-
-isClosingFileOp op =
-    case op of
-        T_IoDuplicate _ (T_GREATAND _) "-" -> True
-        T_IoDuplicate _ (T_LESSAND  _) "-" -> True
-        _ -> False
-
-
--- Consider 'export/declare -x' a reference, since it makes the var available
-getReferencedVariableCommand base@(T_SimpleCommand _ _ (T_NormalWord _ (T_Literal _ x:_):rest)) =
-    case x of
-        "export" -> if "f" `elem` flags
-            then []
-            else concatMap getReference rest
-        "declare" -> if any (`elem` flags) ["x", "p"]
-            then concatMap getReference rest
-            else []
-        "readonly" ->
-            if any (`elem` flags) ["f", "p"]
-            then []
-            else concatMap getReference rest
-        "trap" ->
-            case rest of
-                head:_ -> map (\x -> (head, head, x)) $ getVariablesFromLiteralToken head
-                _ -> []
-        _ -> []
-  where
-    getReference t@(T_Assignment _ _ name _ value) = [(t, t, name)]
-    getReference t@(T_NormalWord _ [T_Literal _ name]) | not ("-" `isPrefixOf` name) = [(t, t, name)]
-    getReference _ = []
-    flags = map snd $ getAllFlags base
-
-getReferencedVariableCommand _ = []
-
-getModifiedVariableCommand base@(T_SimpleCommand _ _ (T_NormalWord _ (T_Literal _ x:_):rest)) =
-   filter (\(_,_,s,_) -> not ("-" `isPrefixOf` s)) $
-    case x of
-        "read" ->
-            let params = map getLiteral rest in
-                catMaybes . takeWhile isJust . reverse $ params
-        "getopts" ->
-            case rest of
-                opts:var:_ -> maybeToList $ getLiteral var
-                _ -> []
-
-        "let" -> concatMap letParamToLiteral rest
-
-        "export" ->
-            if "f" `elem` flags then [] else concatMap getModifierParamString rest
-
-        "declare" -> if any (`elem` flags) ["F", "f", "p"] then [] else declaredVars
-        "typeset" -> declaredVars
-
-        "local" -> concatMap getModifierParamString rest
-        "readonly" ->
-            if any (`elem` flags) ["f", "p"]
-            then []
-            else concatMap getModifierParamString rest
-        "set" -> maybeToList $ do
-            params <- getSetParams rest
-            return (base, base, "@", DataString $ SourceFrom params)
-
-        "printf" -> maybeToList $ getPrintfVariable rest
-
-        "mapfile" -> maybeToList $ getMapfileArray base rest
-        "readarray" -> maybeToList $ getMapfileArray base rest
-
-        _ -> []
-  where
-    flags = map snd $ getAllFlags base
-    stripEquals s = let rest = dropWhile (/= '=') s in
-        if rest == "" then "" else tail rest
-    stripEqualsFrom (T_NormalWord id1 (T_Literal id2 s:rs)) =
-        T_NormalWord id1 (T_Literal id2 (stripEquals s):rs)
-    stripEqualsFrom (T_NormalWord id1 [T_DoubleQuoted id2 [T_Literal id3 s]]) =
-        T_NormalWord id1 [T_DoubleQuoted id2 [T_Literal id3 (stripEquals s)]]
-    stripEqualsFrom t = t
-
-    declaredVars = concatMap (getModifierParam defaultType) rest
-      where
-        defaultType = if any (`elem` flags) ["a", "A"] then DataArray else DataString
-
-    getLiteral t = do
-        s <- getLiteralString t
-        when ("-" `isPrefixOf` s) $ fail "argument"
-        return (base, t, s, DataString SourceExternal)
-
-    getModifierParamString = getModifierParam DataString
-
-    getModifierParam def t@(T_Assignment _ _ name _ value) =
-        [(base, t, name, dataTypeFrom def value)]
-    getModifierParam def t@T_NormalWord {} = maybeToList $ do
-        name <- getLiteralString t
-        guard $ isVariableName name
-        return (base, t, name, def SourceDeclaration)
-    getModifierParam _ _ = []
-
-    letParamToLiteral token =
-          if var == ""
-            then []
-            else [(base, token, var, DataString $ SourceFrom [stripEqualsFrom token])]
-        where var = takeWhile isVariableChar $ dropWhile (`elem` "+-") $ concat $ oversimplify token
-
-    getSetParams (t:_:rest) | getLiteralString t == Just "-o" = getSetParams rest
-    getSetParams (t:rest) =
-        let s = getLiteralString t in
-            case s of
-                Just "--" -> return rest
-                Just ('-':_) -> getSetParams rest
-                _ -> return (t:fromMaybe [] (getSetParams rest))
-    getSetParams [] = Nothing
-
-    getPrintfVariable list = f $ map (\x -> (x, getLiteralString x)) list
-      where
-        f ((_, Just "-v") : (t, Just var) : _) = return (base, t, var, DataString $ SourceFrom list)
-        f (_:rest) = f rest
-        f [] = fail "not found"
-
-    -- mapfile has some curious syntax allowing flags plus 0..n variable names
-    -- where only the first non-option one is used if any. Here we cheat and
-    -- just get the last one, if it's a variable name.
-    getMapfileArray base arguments = do
-        lastArg <- listToMaybe (reverse arguments)
-        name <- getLiteralString lastArg
-        guard $ isVariableName name
-        return (base, lastArg, name, DataArray SourceExternal)
-
-getModifiedVariableCommand _ = []
-
-getIndexReferences s = fromMaybe [] $ do
-    match <- matchRegex re s
-    index <- match !!! 0
-    return $ matchAllStrings variableNameRegex index
-  where
-    re = mkRegex "(\\[.*\\])"
-
-getOffsetReferences mods = fromMaybe [] $ do
-    match <- matchRegex re mods
-    offsets <- match !!! 0
-    return $ matchAllStrings variableNameRegex offsets
-  where
-    re = mkRegex "^ *:([^-=?+].*)"
-
-getReferencedVariables parents t =
-    case t of
-        T_DollarBraced id l -> let str = bracedString t in
-            (t, t, getBracedReference str) :
-                map (\x -> (l, l, x)) (
-                    getIndexReferences str
-                    ++ getOffsetReferences (getBracedModifier str))
-        TA_Expansion id _ ->
-            if isArithmeticAssignment t
-            then []
-            else getIfReference t t
-        T_Assignment id mode str _ word ->
-            [(t, t, str) | mode == Append] ++ specialReferences str t word
-
-        TC_Unary id _ "-v" token -> getIfReference t token
-        TC_Unary id _ "-R" token -> getIfReference t token
-        TC_Binary id DoubleBracket op lhs rhs ->
-            if isDereferencing op
-            then concatMap (getIfReference t) [lhs, rhs]
-            else []
-
-        t@(T_FdRedirect _ ('{':var) op) -> -- {foo}>&- references and closes foo
-            [(t, t, takeWhile (/= '}') var) | isClosingFileOp op]
-        x -> getReferencedVariableCommand x
-  where
-    -- Try to reduce false positives for unused vars only referenced from evaluated vars
-    specialReferences name base word =
-        if name `elem` [
-            "PS1", "PS2", "PS3", "PS4",
-            "PROMPT_COMMAND"
-          ]
-        then
-            map (\x -> (base, base, x)) $
-                getVariablesFromLiteralToken word
-        else []
-
-    literalizer t = case t of
-        TA_Index {} -> return ""  -- x[0] becomes a reference of x
-        T_Glob _ s -> return s    -- Also when parsed as globs
-        _ -> Nothing
-
-    getIfReference context token = maybeToList $ do
-            str <- getLiteralStringExt literalizer token
-            guard . not $ null str
-            when (isDigit $ head str) $ fail "is a number"
-            return (context, token, getBracedReference str)
-
-    isDereferencing = (`elem` ["-eq", "-ne", "-lt", "-le", "-gt", "-ge"])
-
-    isArithmeticAssignment t = case getPath parents t of
-        this: TA_Assignment _ "=" lhs _ :_ -> lhs == t
-        _ -> False
-
-dataTypeFrom defaultType v = (case v of T_Array {} -> DataArray; _ -> defaultType) $ SourceFrom [v]
-
-
---- Command specific checks
-
--- Compare a command to a string: t `isCommand` "sed" (also matches /usr/bin/sed)
-isCommand token str = isCommandMatch token (\cmd -> cmd  == str || ('/' : str) `isSuffixOf` cmd)
-
--- Compare a command to a literal. Like above, but checks full path.
-isUnqualifiedCommand token str = isCommandMatch token (== str)
-
-isCommandMatch token matcher = fromMaybe False $ do
-    cmd <- getCommandName token
-    return $ matcher cmd
-
--- Does this regex look like it was intended as a glob?
--- True:  *foo*
--- False: .*foo.*
-isConfusedGlobRegex :: String -> Bool
-isConfusedGlobRegex ('*':_) = True
-isConfusedGlobRegex [x,'*'] | x /= '\\' = True
-isConfusedGlobRegex _ = False
-
-isVariableStartChar x = x == '_' || isAsciiLower x || isAsciiUpper x
-isVariableChar x = isVariableStartChar x || isDigit x
-variableNameRegex = mkRegex "[_a-zA-Z][_a-zA-Z0-9]*"
-
-prop_isVariableName1 = isVariableName "_fo123"
-prop_isVariableName2 = not $ isVariableName "4"
-prop_isVariableName3 = not $ isVariableName "test: "
-isVariableName (x:r) = isVariableStartChar x && all isVariableChar r
-isVariableName _ = False
-
-getVariablesFromLiteralToken token =
-    getVariablesFromLiteral (fromJust $ getLiteralStringExt (const $ return " ") token)
-
--- Try to get referenced variables from a literal string like "$foo"
--- Ignores tons of cases like arithmetic evaluation and array indices.
-prop_getVariablesFromLiteral1 =
-    getVariablesFromLiteral "$foo${bar//a/b}$BAZ" == ["foo", "bar", "BAZ"]
-getVariablesFromLiteral string =
-    map (!! 0) $ matchAllSubgroups variableRegex string
-  where
-    variableRegex = mkRegex "\\$\\{?([A-Za-z0-9_]+)"
-
--- Get the variable name from an expansion like ${var:-foo}
-prop_getBracedReference1 = getBracedReference "foo" == "foo"
-prop_getBracedReference2 = getBracedReference "#foo" == "foo"
-prop_getBracedReference3 = getBracedReference "#" == "#"
-prop_getBracedReference4 = getBracedReference "##" == "#"
-prop_getBracedReference5 = getBracedReference "#!" == "!"
-prop_getBracedReference6 = getBracedReference "!#" == "#"
-prop_getBracedReference7 = getBracedReference "!foo#?" == "foo"
-prop_getBracedReference8 = getBracedReference "foo-bar" == "foo"
-prop_getBracedReference9 = getBracedReference "foo:-bar" == "foo"
-prop_getBracedReference10= getBracedReference "foo: -1" == "foo"
-prop_getBracedReference11= getBracedReference "!os*" == ""
-prop_getBracedReference12= getBracedReference "!os?bar**" == ""
-prop_getBracedReference13= getBracedReference "foo[bar]" == "foo"
-getBracedReference s = fromMaybe s $
-    nameExpansion s `mplus` takeName noPrefix `mplus` getSpecial noPrefix `mplus` getSpecial s
-  where
-    noPrefix = dropPrefix s
-    dropPrefix (c:rest) = if c `elem` "!#" then rest else c:rest
-    dropPrefix "" = ""
-    takeName s = do
-        let name = takeWhile isVariableChar s
-        guard . not $ null name
-        return name
-    getSpecial (c:_) =
-        if c `elem` "*@#?-$!" then return [c] else fail "not special"
-    getSpecial _ = fail "empty"
-
-    nameExpansion ('!':rest) = do -- e.g. ${!foo*bar*}
-        let suffix = dropWhile isVariableChar rest
-        guard $ suffix /= rest -- e.g. ${!@}
-        first <- suffix !!! 0
-        guard $ first `elem` "*?"
-        return ""
-    nameExpansion _ = Nothing
-
-prop_getBracedModifier1 = getBracedModifier "foo:bar:baz" == ":bar:baz"
-prop_getBracedModifier2 = getBracedModifier "!var:-foo" == ":-foo"
-prop_getBracedModifier3 = getBracedModifier "foo[bar]" == "[bar]"
-getBracedModifier s = fromMaybe "" . listToMaybe $ do
-    let var = getBracedReference s
-    a <- dropModifier s
-    dropPrefix var a
-  where
-    dropPrefix [] t = return t
-    dropPrefix (a:b) (c:d) | a == c = dropPrefix b d
-    dropPrefix _ _ = []
-
-    dropModifier (c:rest) | c `elem` "#!" = [rest, c:rest]
-    dropModifier x = [x]
-
--- Useful generic functions.
-
--- Run an action in a Maybe (or do nothing).
--- Example:
--- potentially $ do
---   s <- getLiteralString cmd
---   guard $ s `elem` ["--recursive", "-r"]
---   return $ warn .. "Something something recursive"
-potentially :: Monad m => Maybe (m ()) -> m ()
-potentially = fromMaybe (return ())
-
--- Get element 0 or a default. Like `head` but safe.
-headOrDefault _ (a:_) = a
-headOrDefault def _ = def
-
---- Get element n of a list, or Nothing. Like `!!` but safe.
-(!!!) list i =
-    case drop i list of
-        [] -> Nothing
-        (r:_) -> Just r
-
--- Run a command if the shell is in the given list
-whenShell l c = do
-    shell <- asks shellType
-    when (shell `elem` l ) c
-
-
-filterByAnnotation asSpec params =
-    filter (not . shouldIgnore)
-  where
-    token = asScript asSpec
-    idFor (TokenComment id _) = id
-    shouldIgnore note =
-        any (shouldIgnoreFor (getCode note)) $
-            getPath parents (T_Bang $ idFor note)
-    shouldIgnoreFor num (T_Annotation _ anns _) =
-        any hasNum anns
-      where
-        hasNum (DisableComment ts) = num == ts
-        hasNum _ = False
-    shouldIgnoreFor _ T_Include {} = not $ asCheckSourced asSpec
-    shouldIgnoreFor _ _ = False
-    parents = parentMap params
-    getCode (TokenComment _ (Comment _ c _)) = c
-
--- Is this a ${#anything}, to get string length or array count?
-isCountingReference (T_DollarBraced id token) =
-    case concat $ oversimplify token of
-        '#':_ -> True
-        _ -> False
-isCountingReference _ = False
-
--- FIXME: doesn't handle ${a:+$var} vs ${a:+"$var"}
-isQuotedAlternativeReference t =
-    case t of
-        T_DollarBraced _ _ ->
-            getBracedModifier (bracedString t) `matches` re
-        _ -> False
-  where
-    re = mkRegex "(^|\\]):?\\+"
-
-
-
-return []
-runTests =  $( [| $(forAllProperties) (quickCheckWithResult (stdArgs { maxSuccess = 1 }) ) |])
diff --git a/ShellCheck/Checker.hs b/ShellCheck/Checker.hs
deleted file mode 100644
--- a/ShellCheck/Checker.hs
+++ /dev/null
@@ -1,198 +0,0 @@
-{-
-    Copyright 2012-2015 Vidar Holen
-
-    This file is part of ShellCheck.
-    http://www.vidarholen.net/contents/shellcheck
-
-    ShellCheck is free software: you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation, either version 3 of the License, or
-    (at your option) any later version.
-
-    ShellCheck is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program.  If not, see <http://www.gnu.org/licenses/>.
--}
-{-# LANGUAGE TemplateHaskell #-}
-module ShellCheck.Checker (checkScript, ShellCheck.Checker.runTests) where
-
-import ShellCheck.Interface
-import ShellCheck.Parser
-import ShellCheck.Analyzer
-
-import Data.Either
-import Data.Functor
-import Data.List
-import Data.Maybe
-import Data.Ord
-import Control.Monad.Identity
-import qualified Data.Map as Map
-import qualified System.IO
-import Prelude hiding (readFile)
-import Control.Monad
-
-import Test.QuickCheck.All
-
-tokenToPosition map (TokenComment id c) = fromMaybe fail $ do
-    position <- Map.lookup id map
-    return $ PositionedComment position position c
-  where
-    fail = error "Internal shellcheck error: id doesn't exist. Please report!"
-
-checkScript :: Monad m => SystemInterface m -> CheckSpec -> m CheckResult
-checkScript sys spec = do
-    results <- checkScript (csScript spec)
-    return CheckResult {
-        crFilename = csFilename spec,
-        crComments = results
-    }
-  where
-    checkScript contents = do
-        result <- parseScript sys ParseSpec {
-            psFilename = csFilename spec,
-            psScript = contents,
-            psCheckSourced = csCheckSourced spec
-        }
-        let parseMessages = prComments result
-        let analysisMessages =
-                fromMaybe [] $
-                    (arComments . analyzeScript . analysisSpec)
-                        <$> prRoot result
-        let translator = tokenToPosition (prTokenPositions result)
-        return . nub . sortMessages . filter shouldInclude $
-            (parseMessages ++ map translator analysisMessages)
-
-    shouldInclude (PositionedComment _ _ (Comment _ code _)) =
-        code `notElem` csExcludedWarnings spec
-
-    sortMessages = sortBy (comparing order)
-    order (PositionedComment pos _ (Comment severity code message)) =
-        (posFile pos, posLine pos, posColumn pos, severity, code, message)
-    getPosition (PositionedComment pos _ _) = pos
-
-    analysisSpec root =
-        AnalysisSpec {
-            asScript = root,
-            asShellType = csShellTypeOverride spec,
-            asCheckSourced = csCheckSourced spec,
-            asExecutionMode = Executed
-         }
-
-getErrors sys spec =
-    sort . map getCode . crComments $
-        runIdentity (checkScript sys spec)
-  where
-    getCode (PositionedComment _ _ (Comment _ code _)) = code
-
-check = checkWithIncludes []
-
-checkWithSpec includes =
-    getErrors (mockedSystemInterface includes)
-
-checkWithIncludes includes src =
-    checkWithSpec includes emptyCheckSpec {
-        csScript = src,
-        csExcludedWarnings = [2148]
-    }
-
-checkRecursive includes src =
-    checkWithSpec includes emptyCheckSpec {
-        csScript = src,
-        csExcludedWarnings = [2148],
-        csCheckSourced = True
-    }
-
-prop_findsParseIssue = check "echo \"$12\"" == [1037]
-
-prop_commentDisablesParseIssue1 =
-    null $ check "#shellcheck disable=SC1037\necho \"$12\""
-prop_commentDisablesParseIssue2 =
-    null $ check "#shellcheck disable=SC1037\n#lol\necho \"$12\""
-
-prop_findsAnalysisIssue =
-    check "echo $1" == [2086]
-prop_commentDisablesAnalysisIssue1 =
-    null $ check "#shellcheck disable=SC2086\necho $1"
-prop_commentDisablesAnalysisIssue2 =
-    null $ check "#shellcheck disable=SC2086\n#lol\necho $1"
-
-prop_optionDisablesIssue1 =
-    null $ getErrors
-                (mockedSystemInterface [])
-                emptyCheckSpec {
-                    csScript = "echo $1",
-                    csExcludedWarnings = [2148, 2086]
-                }
-
-prop_optionDisablesIssue2 =
-    null $ getErrors
-                (mockedSystemInterface [])
-                emptyCheckSpec {
-                    csScript = "echo \"$10\"",
-                    csExcludedWarnings = [2148, 1037]
-                }
-
-prop_canParseDevNull =
-    [] == check "source /dev/null"
-
-prop_failsWhenNotSourcing =
-    [1091, 2154] == check "source lol; echo \"$bar\""
-
-prop_worksWhenSourcing =
-    null $ checkWithIncludes [("lib", "bar=1")] "source lib; echo \"$bar\""
-
-prop_worksWhenDotting =
-    null $ checkWithIncludes [("lib", "bar=1")] ". lib; echo \"$bar\""
-
-prop_noInfiniteSourcing =
-    [] == checkWithIncludes  [("lib", "source lib")] "source lib"
-
-prop_canSourceBadSyntax =
-    [1094, 2086] == checkWithIncludes [("lib", "for f; do")] "source lib; echo $1"
-
-prop_cantSourceDynamic =
-    [1090] == checkWithIncludes [("lib", "")] ". \"$1\""
-
-prop_cantSourceDynamic2 =
-    [1090] == checkWithIncludes [("lib", "")] "source ~/foo"
-
-prop_canSourceDynamicWhenRedirected =
-    null $ checkWithIncludes [("lib", "")] "#shellcheck source=lib\n. \"$1\""
-
-prop_recursiveAnalysis =
-    [2086] == checkRecursive [("lib", "echo $1")] "source lib"
-
-prop_recursiveParsing =
-    [1037] == checkRecursive [("lib", "echo \"$10\"")] "source lib"
-
-prop_sourceDirectiveDoesntFollowFile =
-    null $ checkWithIncludes
-                [("foo", "source bar"), ("bar", "baz=3")]
-                "#shellcheck source=foo\n. \"$1\"; echo \"$baz\""
-
-prop_filewideAnnotationBase = [2086] == check "#!/bin/sh\necho $1"
-prop_filewideAnnotation1 = null $
-    check "#!/bin/sh\n# shellcheck disable=2086\necho $1"
-prop_filewideAnnotation2 = null $
-    check "#!/bin/sh\n# shellcheck disable=2086\ntrue\necho $1"
-prop_filewideAnnotation3 = null $
-    check "#!/bin/sh\n#unerlated\n# shellcheck disable=2086\ntrue\necho $1"
-prop_filewideAnnotation4 = null $
-    check "#!/bin/sh\n# shellcheck disable=2086\n#unrelated\ntrue\necho $1"
-prop_filewideAnnotation5 = null $
-    check "#!/bin/sh\n\n\n\n#shellcheck disable=2086\ntrue\necho $1"
-prop_filewideAnnotation6 = null $
-    check "#shellcheck shell=sh\n#unrelated\n#shellcheck disable=2086\ntrue\necho $1"
-prop_filewideAnnotation7 = null $
-    check "#!/bin/sh\n# shellcheck disable=2086\n#unrelated\ntrue\necho $1"
-
-prop_filewideAnnotationBase2 = [2086, 2181] == check "true\n[ $? == 0 ] && echo $1"
-prop_filewideAnnotation8 = null $
-    check "# Disable $? warning\n#shellcheck disable=SC2181\n# Disable quoting warning\n#shellcheck disable=2086\ntrue\n[ $? == 0 ] && echo $1"
-
-return []
-runTests = $quickCheckAll
diff --git a/ShellCheck/Checks/Commands.hs b/ShellCheck/Checks/Commands.hs
deleted file mode 100644
--- a/ShellCheck/Checks/Commands.hs
+++ /dev/null
@@ -1,854 +0,0 @@
-{-
-    Copyright 2012-2015 Vidar Holen
-
-    This file is part of ShellCheck.
-    http://www.vidarholen.net/contents/shellcheck
-
-    ShellCheck is free software: you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation, either version 3 of the License, or
-    (at your option) any later version.
-
-    ShellCheck is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program.  If not, see <http://www.gnu.org/licenses/>.
--}
-{-# LANGUAGE TemplateHaskell #-}
-{-# LANGUAGE FlexibleContexts #-}
-
--- This module contains checks that examine specific commands by name.
-module ShellCheck.Checks.Commands (checker
-    , ShellCheck.Checks.Commands.runTests
-) where
-
-import ShellCheck.AST
-import ShellCheck.ASTLib
-import ShellCheck.AnalyzerLib
-import ShellCheck.Data
-import ShellCheck.Interface
-import ShellCheck.Parser
-import ShellCheck.Regex
-
-import Control.Monad
-import Control.Monad.RWS
-import Data.Char
-import Data.List
-import Data.Maybe
-import qualified Data.Map.Strict as Map
-import Test.QuickCheck.All (forAllProperties)
-import Test.QuickCheck.Test (quickCheckWithResult, stdArgs, maxSuccess)
-
-data CommandName = Exactly String | Basename String
-    deriving (Eq, Ord)
-
-data CommandCheck =
-    CommandCheck CommandName (Token -> Analysis)
-
-
-verify :: CommandCheck -> String -> Bool
-verify f s = producesComments (getChecker [f]) s == Just True
-verifyNot f s = producesComments (getChecker [f]) s == Just False
-
-arguments (T_SimpleCommand _ _ (cmd:args)) = args
-
-commandChecks :: [CommandCheck]
-commandChecks = [
-    checkTr
-    ,checkFindNameGlob
-    ,checkNeedlessExpr
-    ,checkGrepRe
-    ,checkTrapQuotes
-    ,checkReturn
-    ,checkFindExecWithSingleArgument
-    ,checkUnusedEchoEscapes
-    ,checkInjectableFindSh
-    ,checkFindActionPrecedence
-    ,checkMkdirDashPM
-    ,checkNonportableSignals
-    ,checkInteractiveSu
-    ,checkSshCommandString
-    ,checkPrintfVar
-    ,checkUuoeCmd
-    ,checkSetAssignment
-    ,checkExportedExpansions
-    ,checkAliasesUsesArgs
-    ,checkAliasesExpandEarly
-    ,checkUnsetGlobs
-    ,checkFindWithoutPath
-    ,checkTimeParameters
-    ,checkTimedCommand
-    ,checkLocalScope
-    ,checkDeprecatedTempfile
-    ,checkDeprecatedEgrep
-    ,checkDeprecatedFgrep
-    ,checkWhileGetoptsCase
-    ,checkCatastrophicRm
-    ,checkLetUsage
-    ]
-
-buildCommandMap :: [CommandCheck] -> Map.Map CommandName (Token -> Analysis)
-buildCommandMap = foldl' addCheck Map.empty
-  where
-    addCheck map (CommandCheck name function) =
-        Map.insertWith composeAnalyzers name function map
-
-
-checkCommand :: Map.Map CommandName (Token -> Analysis) -> Token -> Analysis
-checkCommand map t@(T_SimpleCommand id _ (cmd:rest)) = fromMaybe (return ()) $ do
-    name <- getLiteralString cmd
-    return $
-        if '/' `elem` name
-        then
-            Map.findWithDefault nullCheck (Basename $ basename name) map t
-        else do
-            Map.findWithDefault nullCheck (Exactly name) map t
-            Map.findWithDefault nullCheck (Basename name) map t
-
-  where
-    basename = reverse . takeWhile (/= '/') . reverse
-checkCommand _ _ = return ()
-
-getChecker :: [CommandCheck] -> Checker
-getChecker list = Checker {
-    perScript = const $ return (),
-    perToken = checkCommand map
-    }
-  where
-    map = buildCommandMap list
-
-
-checker :: Parameters -> Checker
-checker params = getChecker commandChecks
-
-prop_checkTr1 = verify checkTr "tr [a-f] [A-F]"
-prop_checkTr2 = verify checkTr "tr 'a-z' 'A-Z'"
-prop_checkTr2a= verify checkTr "tr '[a-z]' '[A-Z]'"
-prop_checkTr3 = verifyNot checkTr "tr -d '[:lower:]'"
-prop_checkTr3a= verifyNot checkTr "tr -d '[:upper:]'"
-prop_checkTr3b= verifyNot checkTr "tr -d '|/_[:upper:]'"
-prop_checkTr4 = verifyNot checkTr "ls [a-z]"
-prop_checkTr5 = verify checkTr "tr foo bar"
-prop_checkTr6 = verify checkTr "tr 'hello' 'world'"
-prop_checkTr8 = verifyNot checkTr "tr aeiou _____"
-prop_checkTr9 = verifyNot checkTr "a-z n-za-m"
-prop_checkTr10= verifyNot checkTr "tr --squeeze-repeats rl lr"
-prop_checkTr11= verifyNot checkTr "tr abc '[d*]'"
-checkTr = CommandCheck (Basename "tr") (mapM_ f . arguments)
-  where
-    f w | isGlob w = -- The user will go [ab] -> '[ab]' -> 'ab'. Fixme?
-        warn (getId w) 2060 "Quote parameters to tr to prevent glob expansion."
-    f word =
-      case getLiteralString word of
-        Just "a-z" -> info (getId word) 2018 "Use '[:lower:]' to support accents and foreign alphabets."
-        Just "A-Z" -> info (getId word) 2019 "Use '[:upper:]' to support accents and foreign alphabets."
-        Just s -> do  -- Eliminate false positives by only looking for dupes in SET2?
-          when (not ("-" `isPrefixOf` s || "[:" `isInfixOf` s) && duplicated s) $
-            info (getId word) 2020 "tr replaces sets of chars, not words (mentioned due to duplicates)."
-          unless ("[:" `isPrefixOf` s) $
-            when ("[" `isPrefixOf` s && "]" `isSuffixOf` s && (length s > 2) && ('*' `notElem` s)) $
-              info (getId word) 2021 "Don't use [] around classes in tr, it replaces literal square brackets."
-        Nothing -> return ()
-
-    duplicated s =
-        let relevant = filter isAlpha s
-        in relevant /= nub relevant
-
-prop_checkFindNameGlob1 = verify checkFindNameGlob "find / -name *.php"
-prop_checkFindNameGlob2 = verify checkFindNameGlob "find / -type f -ipath *(foo)"
-prop_checkFindNameGlob3 = verifyNot checkFindNameGlob "find * -name '*.php'"
-checkFindNameGlob = CommandCheck (Basename "find") (f . arguments)  where
-    acceptsGlob (Just s) = s `elem` [ "-ilname", "-iname", "-ipath", "-iregex", "-iwholename", "-lname", "-name", "-path", "-regex", "-wholename" ]
-    acceptsGlob _ = False
-    f [] = return ()
-    f [x] = return ()
-    f (a:b:r) = do
-        when (acceptsGlob (getLiteralString a) && isGlob b) $ do
-            let (Just s) = getLiteralString a
-            warn (getId b) 2061 $ "Quote the parameter to " ++ s ++ " so the shell won't interpret it."
-        f (b:r)
-
-
-prop_checkNeedlessExpr = verify checkNeedlessExpr "foo=$(expr 3 + 2)"
-prop_checkNeedlessExpr2 = verify checkNeedlessExpr "foo=`echo \\`expr 3 + 2\\``"
-prop_checkNeedlessExpr3 = verifyNot checkNeedlessExpr "foo=$(expr foo : regex)"
-prop_checkNeedlessExpr4 = verifyNot checkNeedlessExpr "foo=$(expr foo \\< regex)"
-checkNeedlessExpr = CommandCheck (Basename "expr") f where
-    f t =
-        when (all (`notElem` exceptions) (words $ arguments t)) $
-            style (getId t) 2003
-                "expr is antiquated. Consider rewriting this using $((..)), ${} or [[ ]]."
-    -- These operators are hard to replicate in POSIX
-    exceptions = [ ":", "<", ">", "<=", ">=" ]
-    words = mapMaybe getLiteralString
-
-
-prop_checkGrepRe1 = verify checkGrepRe "cat foo | grep *.mp3"
-prop_checkGrepRe2 = verify checkGrepRe "grep -Ev cow*test *.mp3"
-prop_checkGrepRe3 = verify checkGrepRe "grep --regex=*.mp3 file"
-prop_checkGrepRe4 = verifyNot checkGrepRe "grep foo *.mp3"
-prop_checkGrepRe5 = verifyNot checkGrepRe "grep-v  --regex=moo *"
-prop_checkGrepRe6 = verifyNot checkGrepRe "grep foo \\*.mp3"
-prop_checkGrepRe7 = verify checkGrepRe "grep *foo* file"
-prop_checkGrepRe8 = verify checkGrepRe "ls | grep foo*.jpg"
-prop_checkGrepRe9 = verifyNot checkGrepRe "grep '[0-9]*' file"
-prop_checkGrepRe10= verifyNot checkGrepRe "grep '^aa*' file"
-prop_checkGrepRe11= verifyNot checkGrepRe "grep --include=*.png foo"
-prop_checkGrepRe12= verifyNot checkGrepRe "grep -F 'Foo*' file"
-prop_checkGrepRe13= verifyNot checkGrepRe "grep -- -foo bar*"
-prop_checkGrepRe14= verifyNot checkGrepRe "grep -e -foo bar*"
-prop_checkGrepRe15= verifyNot checkGrepRe "grep --regex -foo bar*"
-
-checkGrepRe = CommandCheck (Basename "grep") check where
-    check cmd = f cmd (arguments cmd)
-    -- --regex=*(extglob) doesn't work. Fixme?
-    skippable (Just s) = not ("--regex=" `isPrefixOf` s) && "-" `isPrefixOf` s
-    skippable _ = False
-    f _ [] = return ()
-    f cmd (x:r) =
-        let str = getLiteralStringExt (const $ return "_") x
-        in
-            if str `elem` [Just "--", Just "-e", Just "--regex"]
-            then checkRE cmd r -- Regex is *after* this
-            else
-                if skippable str
-                then f cmd r           -- Regex is elsewhere
-                else checkRE cmd (x:r) -- Regex is this
-
-    checkRE _ [] = return ()
-    checkRE cmd (re:_) = do
-        when (isGlob re) $
-            warn (getId re) 2062 "Quote the grep pattern so the shell won't interpret it."
-
-        unless (cmd `hasFlag` "F") $ do
-            let string = concat $ oversimplify re
-            if isConfusedGlobRegex string then
-                warn (getId re) 2063 "Grep uses regex, but this looks like a glob."
-              else potentially $ do
-                char <- getSuspiciousRegexWildcard string
-                return $ info (getId re) 2022 $
-                    "Note that unlike globs, " ++ [char] ++ "* here matches '" ++ [char, char, char] ++ "' but not '" ++ wordStartingWith char ++ "'."
-
-    wordStartingWith c =
-        head . filter ([c] `isPrefixOf`) $ candidates
-      where
-        candidates =
-            sampleWords ++ map (\(x:r) -> toUpper x : r) sampleWords ++ [c:"test"]
-
-    getSuspiciousRegexWildcard str =
-        if not $ str `matches` contra
-        then do
-            match <- matchRegex suspicious str
-            str <- match !!! 0
-            str !!! 0
-        else
-            fail "looks good"
-      where
-        suspicious = mkRegex "([A-Za-z1-9])\\*"
-        contra = mkRegex "[^a-zA-Z1-9]\\*|[][^$+\\\\]"
-
-
-prop_checkTrapQuotes1 = verify checkTrapQuotes "trap \"echo $num\" INT"
-prop_checkTrapQuotes1a= verify checkTrapQuotes "trap \"echo `ls`\" INT"
-prop_checkTrapQuotes2 = verifyNot checkTrapQuotes "trap 'echo $num' INT"
-prop_checkTrapQuotes3 = verify checkTrapQuotes "trap \"echo $((1+num))\" EXIT DEBUG"
-checkTrapQuotes = CommandCheck (Exactly "trap") (f . arguments) where
-    f (x:_) = checkTrap x
-    f _ = return ()
-    checkTrap (T_NormalWord _ [T_DoubleQuoted _ rs]) = mapM_ checkExpansions rs
-    checkTrap _ = return ()
-    warning id = warn id 2064 "Use single quotes, otherwise this expands now rather than when signalled."
-    checkExpansions (T_DollarExpansion id _) = warning id
-    checkExpansions (T_Backticked id _) = warning id
-    checkExpansions (T_DollarBraced id _) = warning id
-    checkExpansions (T_DollarArithmetic id _) = warning id
-    checkExpansions _ = return ()
-
-
-prop_checkReturn1 = verifyNot checkReturn "return"
-prop_checkReturn2 = verifyNot checkReturn "return 1"
-prop_checkReturn3 = verifyNot checkReturn "return $var"
-prop_checkReturn4 = verifyNot checkReturn "return $((a|b))"
-prop_checkReturn5 = verify checkReturn "return -1"
-prop_checkReturn6 = verify checkReturn "return 1000"
-prop_checkReturn7 = verify checkReturn "return 'hello world'"
-checkReturn = CommandCheck (Exactly "return") (f . arguments)
-  where
-    f (first:second:_) =
-        err (getId second) 2151
-            "Only one integer 0-255 can be returned. Use stdout for other data."
-    f [value] =
-        when (isInvalid $ literal value) $
-            err (getId value) 2152
-                "Can only return 0-255. Other data should be written to stdout."
-    f _ = return ()
-
-    isInvalid s = s == "" || any (not . isDigit) s || length s > 5
-        || let value = (read s :: Integer) in value > 255
-
-    literal token = fromJust $ getLiteralStringExt lit token
-    lit (T_DollarBraced {}) = return "0"
-    lit (T_DollarArithmetic {}) = return "0"
-    lit (T_DollarExpansion {}) = return "0"
-    lit (T_Backticked {}) = return "0"
-    lit _ = return "WTF"
-
-
-prop_checkFindExecWithSingleArgument1 = verify checkFindExecWithSingleArgument "find . -exec 'cat {} | wc -l' \\;"
-prop_checkFindExecWithSingleArgument2 = verify checkFindExecWithSingleArgument "find . -execdir 'cat {} | wc -l' +"
-prop_checkFindExecWithSingleArgument3 = verifyNot checkFindExecWithSingleArgument "find . -exec wc -l {} \\;"
-checkFindExecWithSingleArgument = CommandCheck (Basename "find") (f . arguments)
-  where
-    f = void . sequence . mapMaybe check . tails
-    check (exec:arg:term:_) = do
-        execS <- getLiteralString exec
-        termS <- getLiteralString term
-        cmdS <- getLiteralStringExt (const $ return " ") arg
-
-        guard $ execS `elem` ["-exec", "-execdir"] && termS `elem` [";", "+"]
-        guard $ cmdS `matches` commandRegex
-        return $ warn (getId exec) 2150 "-exec does not invoke a shell. Rewrite or use -exec sh -c .. ."
-    check _ = Nothing
-    commandRegex = mkRegex "[ |;]"
-
-
-prop_checkUnusedEchoEscapes1 = verify checkUnusedEchoEscapes "echo 'foo\\nbar\\n'"
-prop_checkUnusedEchoEscapes2 = verifyNot checkUnusedEchoEscapes "echo -e 'foi\\nbar'"
-prop_checkUnusedEchoEscapes3 = verify checkUnusedEchoEscapes "echo \"n:\\t42\""
-prop_checkUnusedEchoEscapes4 = verifyNot checkUnusedEchoEscapes "echo lol"
-prop_checkUnusedEchoEscapes5 = verifyNot checkUnusedEchoEscapes "echo -n -e '\n'"
-checkUnusedEchoEscapes = CommandCheck (Basename "echo") (f . arguments)
-  where
-    isDashE = mkRegex "^-.*e"
-    hasEscapes = mkRegex "\\\\[rnt]"
-    f args | concat (concatMap oversimplify allButLast) `matches` isDashE =
-        return ()
-      where allButLast = reverse . drop 1 . reverse $ args
-    f args = mapM_ checkEscapes args
-
-    checkEscapes (T_NormalWord _ args) =
-        mapM_ checkEscapes args
-    checkEscapes (T_DoubleQuoted id args) =
-        mapM_ checkEscapes args
-    checkEscapes (T_Literal id str) = examine id str
-    checkEscapes (T_SingleQuoted id str) = examine id str
-    checkEscapes _ = return ()
-
-    examine id str =
-        when (str `matches` hasEscapes) $
-            info id 2028 "echo won't expand escape sequences. Consider printf."
-
-
-prop_checkInjectableFindSh1 = verify checkInjectableFindSh "find . -exec sh -c 'echo {}' \\;"
-prop_checkInjectableFindSh2 = verify checkInjectableFindSh "find . -execdir bash -c 'rm \"{}\"' ';'"
-prop_checkInjectableFindSh3 = verifyNot checkInjectableFindSh "find . -exec sh -c 'rm \"$@\"' _ {} \\;"
-checkInjectableFindSh = CommandCheck (Basename "find") (check . arguments)
-  where
-    check args = do
-        let idStrings = map (\x -> (getId x, onlyLiteralString x)) args
-        match pattern idStrings
-
-    match _ [] = return ()
-    match [] (next:_) = action next
-    match (p:tests) ((id, arg):args) = do
-        when (p arg) $ match tests args
-        match (p:tests) args
-
-    pattern = [
-        (`elem` ["-exec", "-execdir"]),
-        (`elem` ["sh", "bash", "dash", "ksh"]),
-        (== "-c")
-        ]
-    action (id, arg) =
-        when ("{}" `isInfixOf` arg) $
-            warn id 2156 "Injecting filenames is fragile and insecure. Use parameters."
-
-
-prop_checkFindActionPrecedence1 = verify checkFindActionPrecedence "find . -name '*.wav' -o -name '*.au' -exec rm {} +"
-prop_checkFindActionPrecedence2 = verifyNot checkFindActionPrecedence "find . -name '*.wav' -o \\( -name '*.au' -exec rm {} + \\)"
-prop_checkFindActionPrecedence3 = verifyNot checkFindActionPrecedence "find . -name '*.wav' -o -name '*.au'"
-checkFindActionPrecedence = CommandCheck (Basename "find") (f . arguments)
-  where
-    pattern = [isMatch, const True, isParam ["-o", "-or"], isMatch, const True, isAction]
-    f list | length list < length pattern = return ()
-    f list@(_:rest) =
-        if and (zipWith ($) pattern list)
-        then warnFor (list !! (length pattern - 1))
-        else f rest
-    isMatch = isParam [ "-name", "-regex", "-iname", "-iregex", "-wholename", "-iwholename" ]
-    isAction = isParam [ "-exec", "-execdir", "-delete", "-print", "-print0", "-fls", "-fprint", "-fprint0", "-fprintf", "-ls", "-ok", "-okdir", "-printf" ]
-    isParam strs t = fromMaybe False $ do
-        param <- getLiteralString t
-        return $ param `elem` strs
-    warnFor t = warn (getId t) 2146 "This action ignores everything before the -o. Use \\( \\) to group."
-
-
-prop_checkMkdirDashPM0 = verify checkMkdirDashPM "mkdir -p -m 0755 a/b"
-prop_checkMkdirDashPM1 = verify checkMkdirDashPM "mkdir -pm 0755 $dir"
-prop_checkMkdirDashPM2 = verify checkMkdirDashPM "mkdir -vpm 0755 a/b"
-prop_checkMkdirDashPM3 = verify checkMkdirDashPM "mkdir -pm 0755 -v a/b"
-prop_checkMkdirDashPM4 = verify checkMkdirDashPM "mkdir --parents --mode=0755 a/b"
-prop_checkMkdirDashPM5 = verify checkMkdirDashPM "mkdir --parents --mode 0755 a/b"
-prop_checkMkdirDashPM6 = verify checkMkdirDashPM "mkdir -p --mode=0755 a/b"
-prop_checkMkdirDashPM7 = verify checkMkdirDashPM "mkdir --parents -m 0755 a/b"
-prop_checkMkdirDashPM8 = verifyNot checkMkdirDashPM "mkdir -p a/b"
-prop_checkMkdirDashPM9 = verifyNot checkMkdirDashPM "mkdir -m 0755 a/b"
-prop_checkMkdirDashPM10 = verifyNot checkMkdirDashPM "mkdir a/b"
-prop_checkMkdirDashPM11 = verifyNot checkMkdirDashPM "mkdir --parents a/b"
-prop_checkMkdirDashPM12 = verifyNot checkMkdirDashPM "mkdir --mode=0755 a/b"
-prop_checkMkdirDashPM13 = verifyNot checkMkdirDashPM "mkdir_func -pm 0755 a/b"
-prop_checkMkdirDashPM14 = verifyNot checkMkdirDashPM "mkdir -p -m 0755 singlelevel"
-prop_checkMkdirDashPM15 = verifyNot checkMkdirDashPM "mkdir -p -m 0755 ../bin"
-prop_checkMkdirDashPM16 = verify checkMkdirDashPM "mkdir -p -m 0755 ../bin/laden"
-prop_checkMkdirDashPM17 = verifyNot checkMkdirDashPM "mkdir -p -m 0755 ./bin"
-prop_checkMkdirDashPM18 = verify checkMkdirDashPM "mkdir -p -m 0755 ./bin/laden"
-prop_checkMkdirDashPM19 = verifyNot checkMkdirDashPM "mkdir -p -m 0755 ./../bin"
-prop_checkMkdirDashPM20 = verifyNot checkMkdirDashPM "mkdir -p -m 0755 .././bin"
-prop_checkMkdirDashPM21 = verifyNot checkMkdirDashPM "mkdir -p -m 0755 ../../bin"
-checkMkdirDashPM = CommandCheck (Basename "mkdir") check
-  where
-    check t = potentially $ do
-        let flags = getAllFlags t
-        dashP <- find ((\f -> f == "p" || f == "parents") . snd) flags
-        dashM <- find ((\f -> f == "m" || f == "mode") . snd) flags
-        -- mkdir -pm 0700 dir  is fine, so is ../dir, but dir/subdir is not.
-        guard $ any couldHaveSubdirs (drop 1 $ arguments t)
-        return $ warn (getId $ fst dashM) 2174 "When used with -p, -m only applies to the deepest directory."
-    couldHaveSubdirs t = fromMaybe True $ do
-        name <- getLiteralString t
-        return $ '/' `elem` name && not (name `matches` re)
-    re = mkRegex "^(\\.\\.?\\/)+[^/]+$"
-
-
-prop_checkNonportableSignals1 = verify checkNonportableSignals "trap f 8"
-prop_checkNonportableSignals2 = verifyNot checkNonportableSignals "trap f 0"
-prop_checkNonportableSignals3 = verifyNot checkNonportableSignals "trap f 14"
-prop_checkNonportableSignals4 = verify checkNonportableSignals "trap f SIGKILL"
-prop_checkNonportableSignals5 = verify checkNonportableSignals "trap f 9"
-prop_checkNonportableSignals6 = verify checkNonportableSignals "trap f stop"
-prop_checkNonportableSignals7 = verifyNot checkNonportableSignals "trap 'stop' int"
-checkNonportableSignals = CommandCheck (Exactly "trap") (f . arguments)
-  where
-    f args = case args of
-        first:rest -> unless (isFlag first) $ mapM_ check rest
-        _ -> return ()
-
-    check param = potentially $ do
-        str <- getLiteralString param
-        let id = getId param
-        return $ sequence_ $ mapMaybe (\f -> f id str) [
-            checkNumeric,
-            checkUntrappable
-            ]
-
-    checkNumeric id str = do
-        guard $ not (null str)
-        guard $ all isDigit str
-        guard $ str /= "0" -- POSIX exit trap
-        guard $ str `notElem` ["1", "2", "3", "6", "9", "14", "15" ] -- XSI
-        return $ warn id 2172
-            "Trapping signals by number is not well defined. Prefer signal names."
-
-    checkUntrappable id str = do
-        guard $ map toLower str `elem` ["kill", "9", "sigkill", "stop", "sigstop"]
-        return $ err id 2173
-            "SIGKILL/SIGSTOP can not be trapped."
-
-
-prop_checkInteractiveSu1 = verify checkInteractiveSu "su; rm file; su $USER"
-prop_checkInteractiveSu2 = verify checkInteractiveSu "su foo; something; exit"
-prop_checkInteractiveSu3 = verifyNot checkInteractiveSu "echo rm | su foo"
-prop_checkInteractiveSu4 = verifyNot checkInteractiveSu "su root < script"
-checkInteractiveSu = CommandCheck (Basename "su") f
-  where
-    f cmd = when (length (arguments cmd) <= 1) $ do
-        path <- pathTo cmd
-        when (all undirected path) $
-            info (getId cmd) 2117
-                "To run commands as another user, use su -c or sudo."
-
-    undirected (T_Pipeline _ _ l) = length l <= 1
-    -- This should really just be modifications to stdin, but meh
-    undirected (T_Redirecting _ list _) = null list
-    undirected _ = True
-
-
--- This is hard to get right without properly parsing ssh args
-prop_checkSshCmdStr1 = verify checkSshCommandString "ssh host \"echo $PS1\""
-prop_checkSshCmdStr2 = verifyNot checkSshCommandString "ssh host \"ls foo\""
-prop_checkSshCmdStr3 = verifyNot checkSshCommandString "ssh \"$host\""
-checkSshCommandString = CommandCheck (Basename "ssh") (f . arguments)
-  where
-    nonOptions =
-        filter (\x -> not $ "-" `isPrefixOf` concat (oversimplify x))
-    f args =
-        case nonOptions args of
-            (hostport:r@(_:_)) -> checkArg $ last r
-            _ -> return ()
-    checkArg (T_NormalWord _ [T_DoubleQuoted id parts]) =
-        case filter (not . isConstant) parts of
-            [] -> return ()
-            (x:_) -> info (getId x) 2029
-                "Note that, unescaped, this expands on the client side."
-    checkArg _ = return ()
-
-
-prop_checkPrintfVar1 = verify checkPrintfVar "printf \"Lol: $s\""
-prop_checkPrintfVar2 = verifyNot checkPrintfVar "printf 'Lol: $s'"
-prop_checkPrintfVar3 = verify checkPrintfVar "printf -v cow $(cmd)"
-prop_checkPrintfVar4 = verifyNot checkPrintfVar "printf \"%${count}s\" var"
-prop_checkPrintfVar5 = verify checkPrintfVar "printf '%s %s %s' foo bar"
-prop_checkPrintfVar6 = verify checkPrintfVar "printf foo bar baz"
-prop_checkPrintfVar7 = verify checkPrintfVar "printf -- foo bar baz"
-prop_checkPrintfVar8 = verifyNot checkPrintfVar "printf '%s %s %s' \"${var[@]}\""
-prop_checkPrintfVar9 = verifyNot checkPrintfVar "printf '%s %s %s\\n' *.png"
-prop_checkPrintfVar10= verifyNot checkPrintfVar "printf '%s %s %s' foo bar baz"
-prop_checkPrintfVar11= verifyNot checkPrintfVar "printf '%(%s%s)T' -1"
-checkPrintfVar = CommandCheck (Exactly "printf") (f . arguments) where
-    f (doubledash:rest) | getLiteralString doubledash == Just "--" = f rest
-    f (dashv:var:rest) | getLiteralString dashv == Just "-v" = f rest
-    f (format:params) = check format params
-    f _ = return ()
-
-    countFormats string =
-        case string of
-            '%':'%':rest -> countFormats rest
-            '%':'(':rest -> 1 + countFormats (dropWhile (/= ')') rest)
-            '%':rest -> 1 + countFormats rest
-            _:rest -> countFormats rest
-            [] -> 0
-
-    check format more = do
-        fromMaybe (return ()) $ do
-            string <- getLiteralString format
-            let vars = countFormats string
-
-            return $ do
-                when (vars == 0 && more /= []) $
-                    err (getId format) 2182
-                        "This printf format string has no variables. Other arguments are ignored."
-
-                when (vars > 0
-                        && length more < vars
-                        && all (not . mayBecomeMultipleArgs) more) $
-                    warn (getId format) 2183 $
-                        "This format string has " ++ show vars ++ " variables, but is passed " ++ show (length more) ++ " arguments."
-
-
-        unless ('%' `elem` concat (oversimplify format) || isLiteral format) $
-          info (getId format) 2059
-              "Don't use variables in the printf format string. Use printf \"..%s..\" \"$foo\"."
-
-
-
-
-prop_checkUuoeCmd1 = verify checkUuoeCmd "echo $(date)"
-prop_checkUuoeCmd2 = verify checkUuoeCmd "echo `date`"
-prop_checkUuoeCmd3 = verify checkUuoeCmd "echo \"$(date)\""
-prop_checkUuoeCmd4 = verify checkUuoeCmd "echo \"`date`\""
-prop_checkUuoeCmd5 = verifyNot checkUuoeCmd "echo \"The time is $(date)\""
-prop_checkUuoeCmd6 = verifyNot checkUuoeCmd "echo \"$(<file)\""
-checkUuoeCmd = CommandCheck (Exactly "echo") (f . arguments) where
-    msg id = style id 2005 "Useless echo? Instead of 'echo $(cmd)', just use 'cmd'."
-    f [token] = when (tokenIsJustCommandOutput token) $ msg (getId token)
-    f _ = return ()
-
-
-prop_checkSetAssignment1 = verify checkSetAssignment "set foo 42"
-prop_checkSetAssignment2 = verify checkSetAssignment "set foo = 42"
-prop_checkSetAssignment3 = verify checkSetAssignment "set foo=42"
-prop_checkSetAssignment4 = verifyNot checkSetAssignment "set -- if=/dev/null"
-prop_checkSetAssignment5 = verifyNot checkSetAssignment "set 'a=5'"
-prop_checkSetAssignment6 = verifyNot checkSetAssignment "set"
-checkSetAssignment = CommandCheck (Exactly "set") (f . arguments)
-  where
-    f (var:value:rest) =
-        let str = literal var in
-            when (isVariableName str || isAssignment str) $
-                msg (getId var)
-    f (var:_) =
-        when (isAssignment $ literal var) $
-            msg (getId var)
-    f _ = return ()
-
-    msg id = warn id 2121 "To assign a variable, use just 'var=value', no 'set ..'."
-
-    isAssignment str = '=' `elem` str
-    literal (T_NormalWord _ l) = concatMap literal l
-    literal (T_Literal _ str) = str
-    literal _ = "*"
-
-
-prop_checkExportedExpansions1 = verify checkExportedExpansions "export $foo"
-prop_checkExportedExpansions2 = verify checkExportedExpansions "export \"$foo\""
-prop_checkExportedExpansions3 = verifyNot checkExportedExpansions "export foo"
-checkExportedExpansions = CommandCheck (Exactly "export") (check . arguments)
-  where
-    check = mapM_ checkForVariables
-    checkForVariables f =
-        case getWordParts f of
-            [t@(T_DollarBraced {})] ->
-                warn (getId t) 2163 "Exporting an expansion rather than a variable."
-            _ -> return ()
-
-
-prop_checkAliasesUsesArgs1 = verify checkAliasesUsesArgs "alias a='cp $1 /a'"
-prop_checkAliasesUsesArgs2 = verifyNot checkAliasesUsesArgs "alias $1='foo'"
-prop_checkAliasesUsesArgs3 = verify checkAliasesUsesArgs "alias a=\"echo \\${@}\""
-checkAliasesUsesArgs = CommandCheck (Exactly "alias") (f . arguments)
-  where
-    re = mkRegex "\\$\\{?[0-9*@]"
-    f = mapM_ checkArg
-    checkArg arg =
-        let string = fromJust $ getLiteralStringExt (const $ return "_") arg in
-            when ('=' `elem` string && string `matches` re) $
-                err (getId arg) 2142
-                    "Aliases can't use positional parameters. Use a function."
-
-
-prop_checkAliasesExpandEarly1 = verify checkAliasesExpandEarly "alias foo=\"echo $PWD\""
-prop_checkAliasesExpandEarly2 = verifyNot checkAliasesExpandEarly "alias -p"
-prop_checkAliasesExpandEarly3 = verifyNot checkAliasesExpandEarly "alias foo='echo {1..10}'"
-checkAliasesExpandEarly = CommandCheck (Exactly "alias") (f . arguments)
-  where
-    f = mapM_ checkArg
-    checkArg arg | '=' `elem` concat (oversimplify arg) =
-        forM_ (take 1 $ filter (not . isLiteral) $ getWordParts arg) $
-            \x -> warn (getId x) 2139 "This expands when defined, not when used. Consider escaping."
-    checkArg _ = return ()
-
-
-prop_checkUnsetGlobs1 = verify checkUnsetGlobs "unset foo[1]"
-prop_checkUnsetGlobs2 = verifyNot checkUnsetGlobs "unset foo"
-checkUnsetGlobs = CommandCheck (Exactly "unset") (mapM_ check . arguments)
-  where
-    check arg =
-        when (isGlob arg) $
-            warn (getId arg) 2184 "Quote arguments to unset so they're not glob expanded."
-
-
-prop_checkFindWithoutPath1 = verify checkFindWithoutPath "find -type f"
-prop_checkFindWithoutPath2 = verify checkFindWithoutPath "find"
-prop_checkFindWithoutPath3 = verifyNot checkFindWithoutPath "find . -type f"
-prop_checkFindWithoutPath4 = verifyNot checkFindWithoutPath "find -H -L \"$path\" -print"
-prop_checkFindWithoutPath5 = verifyNot checkFindWithoutPath "find -O3 ."
-prop_checkFindWithoutPath6 = verifyNot checkFindWithoutPath "find -D exec ."
-checkFindWithoutPath = CommandCheck (Basename "find") f
-  where
-    f (T_SimpleCommand _ _ (cmd:args)) =
-        unless (hasPath args) $
-            info (getId cmd) 2185 "Some finds don't have a default path. Specify '.' explicitly."
-
-    -- This is a bit of a kludge. find supports flag arguments both before and after the path,
-    -- as well as multiple non-flag arguments that are not the path. We assume that all the
-    -- pre-path flags are single characters, which is generally the case except for -O3.
-    hasPath (first:rest) =
-        let flag = fromJust $ getLiteralStringExt (const $ return "___") first in
-            not ("-" `isPrefixOf` flag) || isLeadingFlag flag && hasPath rest
-    hasPath [] = False
-    isLeadingFlag flag = length flag <= 2 || "-O" `isPrefixOf` flag
-
-
-prop_checkTimeParameters1 = verify checkTimeParameters "time -f lol sleep 10"
-prop_checkTimeParameters2 = verifyNot checkTimeParameters "time sleep 10"
-prop_checkTimeParameters3 = verifyNot checkTimeParameters "time -p foo"
-prop_checkTimeParameters4 = verifyNot checkTimeParameters "command time -f lol sleep 10"
-checkTimeParameters = CommandCheck (Exactly "time") f
-  where
-    f (T_SimpleCommand _ _ (cmd:args:_)) =
-        whenShell [Bash, Sh] $
-            let s = concat $ oversimplify args in
-                when ("-" `isPrefixOf` s && s /= "-p") $
-                    info (getId cmd) 2023 "The shell may override 'time' as seen in man time(1). Use 'command time ..' for that one."
-
-    f _ = return ()
-
-prop_checkTimedCommand1 = verify checkTimedCommand "#!/bin/sh\ntime -p foo | bar"
-prop_checkTimedCommand2 = verify checkTimedCommand "#!/bin/dash\ntime ( foo; bar; )"
-prop_checkTimedCommand3 = verifyNot checkTimedCommand "#!/bin/sh\ntime sleep 1"
-checkTimedCommand = CommandCheck (Exactly "time") f where
-    f (T_SimpleCommand _ _ (c:args@(_:_))) =
-        whenShell [Sh, Dash] $ do
-            let cmd = last args -- "time" is parsed with a command as argument
-            when (isPiped cmd) $
-                warn (getId c) 2176 "'time' is undefined for pipelines. time single stage or bash -c instead."
-            when (isSimple cmd == Just False) $
-                warn (getId cmd) 2177 "'time' is undefined for compound commands, time sh -c instead."
-    f _ = return ()
-    isPiped cmd =
-        case cmd of
-            T_Pipeline _ _ (_:_:_) -> True
-            _ -> False
-    getCommand cmd =
-        case cmd of
-            T_Pipeline _ _ (T_Redirecting _ _ a : _) -> return a
-            _ -> fail ""
-    isSimple cmd = do
-        innerCommand <- getCommand cmd
-        case innerCommand of
-            T_SimpleCommand {} -> return True
-            _ -> return False
-
-prop_checkLocalScope1 = verify checkLocalScope "local foo=3"
-prop_checkLocalScope2 = verifyNot checkLocalScope "f() { local foo=3; }"
-checkLocalScope = CommandCheck (Exactly "local") $ \t ->
-    whenShell [Bash, Dash] $ do -- Ksh allows it, Sh doesn't support local
-        path <- getPathM t
-        unless (any isFunction path) $
-            err (getId t) 2168 "'local' is only valid in functions."
-
-prop_checkDeprecatedTempfile1 = verify checkDeprecatedTempfile "var=$(tempfile)"
-prop_checkDeprecatedTempfile2 = verifyNot checkDeprecatedTempfile "tempfile=$(mktemp)"
-checkDeprecatedTempfile = CommandCheck (Basename "tempfile") $
-    \t -> warn (getId t) 2186 "tempfile is deprecated. Use mktemp instead."
-
-prop_checkDeprecatedEgrep = verify checkDeprecatedEgrep "egrep '.+'"
-checkDeprecatedEgrep = CommandCheck (Basename "egrep") $
-    \t -> info (getId t) 2196 "egrep is non-standard and deprecated. Use grep -E instead."
-
-prop_checkDeprecatedFgrep = verify checkDeprecatedFgrep "fgrep '*' files"
-checkDeprecatedFgrep = CommandCheck (Basename "fgrep") $
-    \t -> info (getId t) 2197 "fgrep is non-standard and deprecated. Use grep -F instead."
-
-prop_checkWhileGetoptsCase1 = verify checkWhileGetoptsCase "while getopts 'a:b' x; do case $x in a) foo;; esac; done"
-prop_checkWhileGetoptsCase2 = verify checkWhileGetoptsCase "while getopts 'a:' x; do case $x in a) foo;; b) bar;; esac; done"
-prop_checkWhileGetoptsCase3 = verifyNot checkWhileGetoptsCase "while getopts 'a:b' x; do case $x in a) foo;; b) bar;; *) :;esac; done"
-prop_checkWhileGetoptsCase4 = verifyNot checkWhileGetoptsCase "while getopts 'a:123' x; do case $x in a) foo;; [0-9]) bar;; esac; done"
-prop_checkWhileGetoptsCase5 = verifyNot checkWhileGetoptsCase "while getopts 'a:' x; do case $x in a) foo;; \\?) bar;; *) baz;; esac; done"
-checkWhileGetoptsCase = CommandCheck (Exactly "getopts") f
-  where
-    f :: Token -> Analysis
-    f t@(T_SimpleCommand _ _ (cmd:arg1:_))  = do
-        path <- getPathM t
-        potentially $ do
-            options <- getLiteralString arg1
-            (T_WhileExpression _ _ body) <- findFirst whileLoop path
-            caseCmd <- mapMaybe findCase body !!! 0
-            return $ check (getId arg1) (map (:[]) $ filter (/= ':') options) caseCmd
-    f _ = return ()
-
-    check :: Id -> [String] -> Token -> Analysis
-    check optId opts (T_CaseExpression id _ list) = do
-            unless (Nothing `Map.member` handledMap) $ do
-                mapM_ (warnUnhandled optId id) $ catMaybes $ Map.keys notHandled
-
-                unless (any (`Map.member` handledMap) [Just "*",Just "?"]) $
-                    warn id 2220 "Invalid flags are not handled. Add a *) case."
-
-            mapM_ warnRedundant $ Map.toList notRequested
-
-        where
-            handledMap = Map.fromList (concatMap getHandledStrings list)
-            requestedMap = Map.fromList $ map (\x -> (Just x, ())) opts
-
-            notHandled = Map.difference requestedMap handledMap
-            notRequested = Map.difference handledMap requestedMap
-
-    warnUnhandled optId caseId str =
-        warn caseId 2213 $ "getopts specified -" ++ str ++ ", but it's not handled by this 'case'."
-
-    warnRedundant (key, expr) = potentially $ do
-        str <- key
-        guard $ str `notElem` ["*", ":", "?"]
-        return $ warn (getId expr) 2214 "This case is not specified by getopts."
-
-    getHandledStrings (_, globs, _) =
-        map (\x -> (literal x, x)) globs
-
-    literal :: Token -> Maybe String
-    literal t = do
-        getLiteralString t <> fromGlob t
-
-    fromGlob t =
-        case t of
-            T_Glob _ ('[':c:']':[]) -> return [c]
-            T_Glob _ "*" -> return "*"
-            T_Glob _ "?" -> return "?"
-            _ -> Nothing
-
-    whileLoop t =
-        case t of
-            T_WhileExpression {} -> return True
-            T_Script {} -> return False
-            _ -> Nothing
-
-    findCase t =
-        case t of
-            T_Annotation _ _ x -> findCase x
-            T_Pipeline _ _ [x] -> findCase x
-            T_Redirecting _ _ x@(T_CaseExpression {}) -> return x
-            _ -> Nothing
-
-prop_checkCatastrophicRm1 = verify checkCatastrophicRm "rm -r $1/$2"
-prop_checkCatastrophicRm2 = verify checkCatastrophicRm "rm -r /home/$foo"
-prop_checkCatastrophicRm3 = verifyNot checkCatastrophicRm "rm -r /home/${USER:?}/*"
-prop_checkCatastrophicRm4 = verify checkCatastrophicRm "rm -fr /home/$(whoami)/*"
-prop_checkCatastrophicRm5 = verifyNot checkCatastrophicRm "rm -r /home/${USER:-thing}/*"
-prop_checkCatastrophicRm6 = verify checkCatastrophicRm "rm --recursive /etc/*$config*"
-prop_checkCatastrophicRm8 = verify checkCatastrophicRm "rm -rf /home"
-prop_checkCatastrophicRm10= verifyNot checkCatastrophicRm "rm -r \"${DIR}\"/{.gitignore,.gitattributes,ci}"
-prop_checkCatastrophicRm11= verify checkCatastrophicRm "rm -r /{bin,sbin}/$exec"
-prop_checkCatastrophicRm12= verify checkCatastrophicRm "rm -r /{{usr,},{bin,sbin}}/$exec"
-prop_checkCatastrophicRm13= verifyNot checkCatastrophicRm "rm -r /{{a,b},{c,d}}/$exec"
-prop_checkCatastrophicRmA = verify checkCatastrophicRm "rm -rf /usr /lib/nvidia-current/xorg/xorg"
-prop_checkCatastrophicRmB = verify checkCatastrophicRm "rm -rf \"$STEAMROOT/\"*"
-checkCatastrophicRm = CommandCheck (Basename "rm") $ \t ->
-    when (isRecursive t) $
-        mapM_ (mapM_ checkWord . braceExpand) $ arguments t
-  where
-    isRecursive = any (`elem` ["r", "R", "recursive"]) . map snd . getAllFlags
-
-    checkWord token =
-        case getLiteralString token of
-            Just str ->
-                when (fixPath str `elem` importantPaths) $
-                    warn (getId token) 2114 "Warning: deletes a system directory."
-            Nothing ->
-                checkWord' token
-
-    checkWord' token = fromMaybe (return ()) $ do
-        filename <- getPotentialPath token
-        let path = fixPath filename
-        return . when (path `elem` importantPaths) $
-            warn (getId token) 2115 $ "Use \"${var:?}\" to ensure this never expands to " ++ path ++ " ."
-
-    fixPath filename =
-        let normalized = skipRepeating '/' . skipRepeating '*' $ filename in
-            if normalized == "/" then normalized else stripTrailing '/' normalized
-
-    getPotentialPath = getLiteralStringExt f
-      where
-        f (T_Glob _ str) = return str
-        f (T_DollarBraced _ word) =
-            let var = onlyLiteralString word in
-                -- This shouldn't handle non-colon cases.
-                if any (`isInfixOf` var) [":?", ":-", ":="]
-                then Nothing
-                else return ""
-        f _ = return ""
-
-    stripTrailing c = reverse . dropWhile (== c) . reverse
-    skipRepeating c (a:b:rest) | a == b && b == c = skipRepeating c (b:rest)
-    skipRepeating c (a:r) = a:skipRepeating c r
-    skipRepeating _ [] = []
-
-    paths = [
-        "", "/bin", "/etc", "/home", "/mnt", "/usr", "/usr/share", "/usr/local",
-        "/var", "/lib", "/dev", "/media", "/boot", "/lib64", "/usr/bin"
-        ]
-    importantPaths = filter (not . null) $
-        ["", "/", "/*", "/*/*"] >>= (\x -> map (++x) paths)
-
-
-prop_checkLetUsage1 = verify checkLetUsage "let a=1"
-prop_checkLetUsage2 = verifyNot checkLetUsage "(( a=1 ))"
-checkLetUsage = CommandCheck (Exactly "let") f
-  where
-    f t = whenShell [Bash,Ksh] $ do
-        style (getId t) 2219 $ "Instead of 'let expr', prefer (( expr )) ."
-
-return []
-runTests =  $( [| $(forAllProperties) (quickCheckWithResult (stdArgs { maxSuccess = 1 }) ) |])
diff --git a/ShellCheck/Checks/ShellSupport.hs b/ShellCheck/Checks/ShellSupport.hs
deleted file mode 100644
--- a/ShellCheck/Checks/ShellSupport.hs
+++ /dev/null
@@ -1,420 +0,0 @@
-{-
-    Copyright 2012-2016 Vidar Holen
-
-    This file is part of ShellCheck.
-    http://www.vidarholen.net/contents/shellcheck
-
-    ShellCheck is free software: you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation, either version 3 of the License, or
-    (at your option) any later version.
-
-    ShellCheck is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program.  If not, see <http://www.gnu.org/licenses/>.
--}
-{-# LANGUAGE TemplateHaskell #-}
-{-# LANGUAGE FlexibleContexts #-}
-module ShellCheck.Checks.ShellSupport (checker
-    , ShellCheck.Checks.ShellSupport.runTests
-) where
-
-import ShellCheck.AST
-import ShellCheck.ASTLib
-import ShellCheck.AnalyzerLib
-import ShellCheck.Interface
-import ShellCheck.Regex
-
-import Control.Monad
-import Control.Monad.RWS
-import Data.Char
-import Data.List
-import Data.Maybe
-import qualified Data.Map as Map
-import Test.QuickCheck.All (forAllProperties)
-import Test.QuickCheck.Test (quickCheckWithResult, stdArgs, maxSuccess)
-
-data ForShell = ForShell [Shell] (Token -> Analysis)
-
-getChecker params list = Checker {
-        perScript = nullCheck,
-        perToken = foldl composeAnalyzers nullCheck $ mapMaybe include list
-    }
-  where
-    shell = shellType params
-    include (ForShell list a) = do
-        guard $ shell `elem` list
-        return a
-
-checker params = getChecker params checks
-
-checks = [
-    checkForDecimals
-    ,checkBashisms
-    ,checkEchoSed
-    ,checkBraceExpansionVars
-    ,checkMultiDimensionalArrays
-    ,checkPS1Assignments
-    ]
-
-testChecker (ForShell _ t) =
-    Checker {
-        perScript = nullCheck,
-        perToken = t
-    }
-verify c s = producesComments (testChecker c) s == Just True
-verifyNot c s = producesComments (testChecker c) s == Just False
-
-prop_checkForDecimals1 = verify checkForDecimals "((3.14*c))"
-prop_checkForDecimals2 = verify checkForDecimals "foo[1.2]=bar"
-prop_checkForDecimals3 = verifyNot checkForDecimals "declare -A foo; foo[1.2]=bar"
-checkForDecimals = ForShell [Sh, Dash, Bash] f
-  where
-    f t@(TA_Expansion id _) = potentially $ do
-        str <- getLiteralString t
-        first <- str !!! 0
-        guard $ isDigit first && '.' `elem` str
-        return $ err id 2079 "(( )) doesn't support decimals. Use bc or awk."
-    f _ = return ()
-
-
-prop_checkBashisms = verify checkBashisms "while read a; do :; done < <(a)"
-prop_checkBashisms2 = verify checkBashisms "[ foo -nt bar ]"
-prop_checkBashisms3 = verify checkBashisms "echo $((i++))"
-prop_checkBashisms4 = verify checkBashisms "rm !(*.hs)"
-prop_checkBashisms5 = verify checkBashisms "source file"
-prop_checkBashisms6 = verify checkBashisms "[ \"$a\" == 42 ]"
-prop_checkBashisms7 = verify checkBashisms "echo ${var[1]}"
-prop_checkBashisms8 = verify checkBashisms "echo ${!var[@]}"
-prop_checkBashisms9 = verify checkBashisms "echo ${!var*}"
-prop_checkBashisms10= verify checkBashisms "echo ${var:4:12}"
-prop_checkBashisms11= verifyNot checkBashisms "echo ${var:-4}"
-prop_checkBashisms12= verify checkBashisms "echo ${var//foo/bar}"
-prop_checkBashisms13= verify checkBashisms "exec -c env"
-prop_checkBashisms14= verify checkBashisms "echo -n \"Foo: \""
-prop_checkBashisms15= verify checkBashisms "let n++"
-prop_checkBashisms16= verify checkBashisms "echo $RANDOM"
-prop_checkBashisms17= verify checkBashisms "echo $((RANDOM%6+1))"
-prop_checkBashisms18= verify checkBashisms "foo &> /dev/null"
-prop_checkBashisms19= verify checkBashisms "foo > file*.txt"
-prop_checkBashisms20= verify checkBashisms "read -ra foo"
-prop_checkBashisms21= verify checkBashisms "[ -a foo ]"
-prop_checkBashisms22= verifyNot checkBashisms "[ foo -a bar ]"
-prop_checkBashisms23= verify checkBashisms "trap mything ERR INT"
-prop_checkBashisms24= verifyNot checkBashisms "trap mything INT TERM"
-prop_checkBashisms25= verify checkBashisms "cat < /dev/tcp/host/123"
-prop_checkBashisms26= verify checkBashisms "trap mything ERR SIGTERM"
-prop_checkBashisms27= verify checkBashisms "echo *[^0-9]*"
-prop_checkBashisms28= verify checkBashisms "exec {n}>&2"
-prop_checkBashisms29= verify checkBashisms "echo ${!var}"
-prop_checkBashisms30= verify checkBashisms "printf -v '%s' \"$1\""
-prop_checkBashisms31= verify checkBashisms "printf '%q' \"$1\""
-prop_checkBashisms32= verifyNot checkBashisms "#!/bin/dash\n[ foo -nt bar ]"
-prop_checkBashisms33= verify checkBashisms "#!/bin/sh\necho -n foo"
-prop_checkBashisms34= verifyNot checkBashisms "#!/bin/dash\necho -n foo"
-prop_checkBashisms35= verifyNot checkBashisms "#!/bin/dash\nlocal foo"
-prop_checkBashisms36= verifyNot checkBashisms "#!/bin/dash\nread -p foo -r bar"
-prop_checkBashisms37= verifyNot checkBashisms "HOSTNAME=foo; echo $HOSTNAME"
-prop_checkBashisms38= verify checkBashisms "RANDOM=9; echo $RANDOM"
-prop_checkBashisms39= verify checkBashisms "foo-bar() { true; }"
-prop_checkBashisms40= verify checkBashisms "echo $(<file)"
-prop_checkBashisms41= verify checkBashisms "echo `<file`"
-prop_checkBashisms42= verify checkBashisms "trap foo int"
-prop_checkBashisms43= verify checkBashisms "trap foo sigint"
-prop_checkBashisms44= verifyNot checkBashisms "#!/bin/dash\ntrap foo int"
-prop_checkBashisms45= verifyNot checkBashisms "#!/bin/dash\ntrap foo INT"
-prop_checkBashisms46= verify checkBashisms "#!/bin/dash\ntrap foo SIGINT"
-prop_checkBashisms47= verify checkBashisms "#!/bin/dash\necho foo 42>/dev/null"
-prop_checkBashisms48= verifyNot checkBashisms "#!/bin/dash\necho $LINENO"
-prop_checkBashisms49= verify checkBashisms "#!/bin/dash\necho $MACHTYPE"
-prop_checkBashisms50= verify checkBashisms "#!/bin/sh\ncmd >& file"
-prop_checkBashisms51= verifyNot checkBashisms "#!/bin/sh\ncmd 2>&1"
-prop_checkBashisms52= verifyNot checkBashisms "#!/bin/sh\ncmd >&2"
-prop_checkBashisms53= verifyNot checkBashisms "#!/bin/sh\nprintf -- -f\n"
-prop_checkBashisms54= verify checkBashisms "#!/bin/sh\nfoo+=bar"
-checkBashisms = ForShell [Sh, Dash] $ \t -> do
-    params <- ask
-    kludge params t
- where
-  -- This code was copy-pasted from Analytics where params was a variable
-  kludge params = bashism
-   where
-    isDash = shellType params == Dash
-    warnMsg id s =
-        if isDash
-        then warn id 2169 $ "In dash, " ++ s ++ " not supported."
-        else warn id 2039 $ "In POSIX sh, " ++ s ++ " undefined."
-
-    bashism (T_ProcSub id _ _) = warnMsg id "process substitution is"
-    bashism (T_Extglob id _ _) = warnMsg id "extglob is"
-    bashism (T_DollarSingleQuoted id _) = warnMsg id "$'..' is"
-    bashism (T_DollarDoubleQuoted id _) = warnMsg id "$\"..\" is"
-    bashism (T_ForArithmetic id _ _ _ _) = warnMsg id "arithmetic for loops are"
-    bashism (T_Arithmetic id _) = warnMsg id "standalone ((..)) is"
-    bashism (T_DollarBracket id _) = warnMsg id "$[..] in place of $((..)) is"
-    bashism (T_SelectIn id _ _ _) = warnMsg id "select loops are"
-    bashism (T_BraceExpansion id _) = warnMsg id "brace expansion is"
-    bashism (T_Condition id DoubleBracket _) = warnMsg id "[[ ]] is"
-    bashism (T_HereString id _) = warnMsg id "here-strings are"
-    bashism (TC_Binary id SingleBracket op _ _)
-        | op `elem` [ "<", ">", "\\<", "\\>", "<=", ">=", "\\<=", "\\>="] =
-            unless isDash $ warnMsg id $ "lexicographical " ++ op ++ " is"
-    bashism (TC_Binary id SingleBracket op _ _)
-        | op `elem` [ "-nt", "-ef" ] =
-            unless isDash $ warnMsg id $ op ++ " is"
-    bashism (TC_Binary id SingleBracket "==" _ _) =
-            warnMsg id "== in place of = is"
-    bashism (TC_Binary id SingleBracket "=~" _ _) =
-            warnMsg id "=~ regex matching is"
-    bashism (TC_Unary id _ "-a" _) =
-            warnMsg id "unary -a in place of -e is"
-    bashism (TA_Unary id op _)
-        | op `elem` [ "|++", "|--", "++|", "--|"] =
-            warnMsg id $ filter (/= '|') op ++ " is"
-    bashism (TA_Binary id "**" _ _) = warnMsg id "exponentials are"
-    bashism (T_FdRedirect id "&" (T_IoFile _ (T_Greater _) _)) = warnMsg id "&> is"
-    bashism (T_FdRedirect id "" (T_IoFile _ (T_GREATAND _) _)) = warnMsg id ">& is"
-    bashism (T_FdRedirect id ('{':_) _) = warnMsg id "named file descriptors are"
-    bashism (T_FdRedirect id num _)
-        | all isDigit num && length num > 1 = warnMsg id "FDs outside 0-9 are"
-    bashism (T_Assignment id Append _ _ _) =
-        warnMsg id "+= is"
-    bashism (T_IoFile id _ word) | isNetworked =
-            warnMsg id "/dev/{tcp,udp} is"
-        where
-            file = onlyLiteralString word
-            isNetworked = any (`isPrefixOf` file) ["/dev/tcp", "/dev/udp"]
-    bashism (T_Glob id str) | "[^" `isInfixOf` str =
-            warnMsg id "^ in place of ! in glob bracket expressions is"
-
-    bashism t@(TA_Expansion id _) | isBashism =
-        warnMsg id $ fromJust str ++ " is"
-      where
-        str = getLiteralString t
-        isBashism = isJust str && isBashVariable (fromJust str)
-    bashism t@(T_DollarBraced id token) = do
-        mapM_ check expansion
-        when (isBashVariable var) $
-                    warnMsg id $ var ++ " is"
-      where
-        str = bracedString t
-        var = getBracedReference str
-        check (regex, feature) =
-            when (isJust $ matchRegex regex str) $ warnMsg id feature
-
-    bashism t@(T_Pipe id "|&") =
-        warnMsg id "|& in place of 2>&1 | is"
-    bashism (T_Array id _) =
-        warnMsg id "arrays are"
-    bashism (T_IoFile id _ t) | isGlob t =
-        warnMsg id "redirecting to/from globs is"
-    bashism (T_CoProc id _ _) =
-        warnMsg id "coproc is"
-
-    bashism (T_Function id _ _ str _) | not (isVariableName str) =
-        warnMsg id "naming functions outside [a-zA-Z_][a-zA-Z0-9_]* is"
-
-    bashism (T_DollarExpansion id [x]) | isOnlyRedirection x =
-        warnMsg id "$(<file) to read files is"
-    bashism (T_Backticked id [x]) | isOnlyRedirection x =
-        warnMsg id "`<file` to read files is"
-
-    bashism t@(T_SimpleCommand _ _ (cmd:arg:_))
-        | t `isCommand` "echo" && "-" `isPrefixOf` argString =
-            unless ("--" `isPrefixOf` argString) $ -- echo "-----"
-                if isDash
-                then
-                    when (argString /= "-n") $
-                        warnMsg (getId arg) "echo flags besides -n"
-                else
-                    warnMsg (getId arg) "echo flags are"
-      where argString = concat $ oversimplify arg
-    bashism t@(T_SimpleCommand _ _ (cmd:arg:_))
-        | t `isCommand` "exec" && "-" `isPrefixOf` concat (oversimplify arg) =
-            warnMsg (getId arg) "exec flags are"
-    bashism t@(T_SimpleCommand id _ _)
-        | t `isCommand` "let" = warnMsg id "'let' is"
-
-    bashism t@(T_SimpleCommand id _ (cmd:rest)) =
-        let name = fromMaybe "" $ getCommandName t
-            flags = getLeadingFlags t
-        in do
-            when (name `elem` unsupportedCommands) $
-                warnMsg id $ "'" ++ name ++ "' is"
-            potentially $ do
-                allowed <- Map.lookup name allowedFlags
-                (word, flag) <- listToMaybe $
-                    filter (\x -> (not . null . snd $ x) && snd x `notElem` allowed) flags
-                return . warnMsg (getId word) $ name ++ " -" ++ flag ++ " is"
-
-            when (name == "source") $ warnMsg id "'source' in place of '.' is"
-            when (name == "trap") $
-                let
-                    check token = potentially $ do
-                        str <- getLiteralString token
-                        let upper = map toUpper str
-                        return $ do
-                            when (upper `elem` ["ERR", "DEBUG", "RETURN"]) $
-                                warnMsg (getId token) $ "trapping " ++ str ++ " is"
-                            when ("SIG" `isPrefixOf` upper) $
-                                warnMsg (getId token)
-                                    "prefixing signal names with 'SIG' is"
-                            when (not isDash && upper /= str) $
-                                warnMsg (getId token)
-                                    "using lower/mixed case for signal names is"
-                in
-                    mapM_ check (drop 1 rest)
-
-            when (name == "printf") $ potentially $ do
-                format <- rest !!! 0  -- flags are covered by allowedFlags
-                let literal = onlyLiteralString format
-                guard $ "%q" `isInfixOf` literal
-                return $ warnMsg (getId format) "printf %q is"
-      where
-        unsupportedCommands = [
-            "let", "caller", "builtin", "complete", "compgen", "declare", "dirs", "disown",
-            "enable", "mapfile", "readarray", "pushd", "popd", "shopt", "suspend",
-            "typeset"
-            ] ++ if not isDash then ["local", "type"] else []
-        allowedFlags = Map.fromList [
-            ("read", if isDash then ["r", "p"] else ["r"]),
-            ("ulimit", ["f"]),
-            ("printf", []),
-            ("exec", [])
-            ]
-
-    bashism _ = return ()
-
-    varChars="_0-9a-zA-Z"
-    expansion = let re = mkRegex in [
-        (re $ "^![" ++ varChars ++ "]", "indirect expansion is"),
-        (re $ "^[" ++ varChars ++ "]+\\[.*\\]$", "array references are"),
-        (re $ "^![" ++ varChars ++ "]+\\[[*@]]$", "array key expansion is"),
-        (re $ "^![" ++ varChars ++ "]+[*@]$", "name matching prefixes are"),
-        (re $ "^[" ++ varChars ++ "]+:[^-=?+]", "string indexing is"),
-        (re $ "^[" ++ varChars ++ "]+(\\[.*\\])?/", "string replacement is")
-        ]
-    bashVars = [
-        "LINENO", "OSTYPE", "MACHTYPE", "HOSTTYPE", "HOSTNAME",
-        "DIRSTACK", "EUID", "UID", "SHLVL", "PIPESTATUS", "SHELLOPTS"
-        ]
-    bashDynamicVars = [ "RANDOM", "SECONDS" ]
-    dashVars = [ "LINENO" ]
-    isBashVariable var =
-        (var `elem` bashDynamicVars
-            || var `elem` bashVars && not (isAssigned var))
-        && not (isDash && var `elem` dashVars)
-    isAssigned var = any f (variableFlow params)
-      where
-        f x = case x of
-                Assignment (_, _, name, _) -> name == var
-                _ -> False
-
-prop_checkEchoSed1 = verify checkEchoSed "FOO=$(echo \"$cow\" | sed 's/foo/bar/g')"
-prop_checkEchoSed2 = verify checkEchoSed "rm $(echo $cow | sed -e 's,foo,bar,')"
-checkEchoSed = ForShell [Bash, Ksh] f
-  where
-    f (T_Pipeline id _ [a, b]) =
-        when (acmd == ["echo", "${VAR}"]) $
-            case bcmd of
-                ["sed", v] -> checkIn v
-                ["sed", "-e", v] -> checkIn v
-                _ -> return ()
-      where
-        -- This should have used backreferences, but TDFA doesn't support them
-        sedRe = mkRegex "^s(.)([^\n]*)g?$"
-        isSimpleSed s = fromMaybe False $ do
-            [first,rest] <- matchRegex sedRe s
-            let delimiters = filter (== head first) rest
-            guard $ length delimiters == 2
-            return True
-
-        acmd = oversimplify a
-        bcmd = oversimplify b
-        checkIn s =
-            when (isSimpleSed s) $
-                style id 2001 "See if you can use ${variable//search/replace} instead."
-    f _ = return ()
-
-
-prop_checkBraceExpansionVars1 = verify checkBraceExpansionVars "echo {1..$n}"
-prop_checkBraceExpansionVars2 = verifyNot checkBraceExpansionVars "echo {1,3,$n}"
-prop_checkBraceExpansionVars3 = verify checkBraceExpansionVars "eval echo DSC{0001..$n}.jpg"
-prop_checkBraceExpansionVars4 = verify checkBraceExpansionVars "echo {$i..100}"
-checkBraceExpansionVars = ForShell [Bash] f
-  where
-    f t@(T_BraceExpansion id list) = mapM_ check list
-      where
-        check element =
-            when (any (`isInfixOf` toString element) ["$..", "..$"]) $ do
-                c <- isEvaled element
-                if c
-                    then style id 2175 "Quote this invalid brace expansion since it should be passed literally to eval."
-                    else warn id 2051 "Bash doesn't support variables in brace range expansions."
-    f _ = return ()
-
-    literalExt t =
-        case t of
-            T_DollarBraced {} -> return "$"
-            T_DollarExpansion {} -> return "$"
-            T_DollarArithmetic {} -> return "$"
-            otherwise -> return "-"
-    toString t = fromJust $ getLiteralStringExt literalExt t
-    isEvaled t = do
-        cmd <- getClosestCommandM t
-        return $ isJust cmd && fromJust cmd `isUnqualifiedCommand` "eval"
-
-
-prop_checkMultiDimensionalArrays1 = verify checkMultiDimensionalArrays "foo[a][b]=3"
-prop_checkMultiDimensionalArrays2 = verifyNot checkMultiDimensionalArrays "foo[a]=3"
-prop_checkMultiDimensionalArrays3 = verify checkMultiDimensionalArrays "foo=( [a][b]=c )"
-prop_checkMultiDimensionalArrays4 = verifyNot checkMultiDimensionalArrays "foo=( [a]=c )"
-prop_checkMultiDimensionalArrays5 = verify checkMultiDimensionalArrays "echo ${foo[bar][baz]}"
-prop_checkMultiDimensionalArrays6 = verifyNot checkMultiDimensionalArrays "echo ${foo[bar]}"
-checkMultiDimensionalArrays = ForShell [Bash] f
-  where
-    f token =
-        case token of
-            T_Assignment _ _ name (first:second:_) _ -> about second
-            T_IndexedElement _ (first:second:_) _ -> about second
-            T_DollarBraced {} ->
-                when (isMultiDim token) $ about token
-            _ -> return ()
-    about t = warn (getId t) 2180 "Bash does not support multidimensional arrays. Use 1D or associative arrays."
-
-    re = mkRegex "^\\[.*\\]\\[.*\\]"  -- Fixme, this matches ${foo:- [][]} and such as well
-    isMultiDim t = getBracedModifier (bracedString t) `matches` re
-
-prop_checkPS11 = verify checkPS1Assignments "PS1='\\033[1;35m\\$ '"
-prop_checkPS11a= verify checkPS1Assignments "export PS1='\\033[1;35m\\$ '"
-prop_checkPSf2 = verify checkPS1Assignments "PS1='\\h \\e[0m\\$ '"
-prop_checkPS13 = verify checkPS1Assignments "PS1=$'\\x1b[c '"
-prop_checkPS14 = verify checkPS1Assignments "PS1=$'\\e[3m; '"
-prop_checkPS14a= verify checkPS1Assignments "export PS1=$'\\e[3m; '"
-prop_checkPS15 = verifyNot checkPS1Assignments "PS1='\\[\\033[1;35m\\]\\$ '"
-prop_checkPS16 = verifyNot checkPS1Assignments "PS1='\\[\\e1m\\e[1m\\]\\$ '"
-prop_checkPS17 = verifyNot checkPS1Assignments "PS1='e033x1B'"
-prop_checkPS18 = verifyNot checkPS1Assignments "PS1='\\[\\e\\]'"
-checkPS1Assignments = ForShell [Bash] f
-  where
-    f token = case token of
-        (T_Assignment _ _ "PS1" _ word) -> warnFor word
-        _ -> return ()
-
-    warnFor word =
-        let contents = concat $ oversimplify word in
-            when (containsUnescaped contents) $
-                info (getId word) 2025 "Make sure all escape sequences are enclosed in \\[..\\] to prevent line wrapping issues"
-    containsUnescaped s =
-        let unenclosed = subRegex enclosedRegex s "" in
-           isJust $ matchRegex escapeRegex unenclosed
-    enclosedRegex = mkRegex "\\\\\\[.*\\\\\\]" -- FIXME: shouldn't be eager
-    escapeRegex = mkRegex "\\\\x1[Bb]|\\\\e|\x1B|\\\\033"
-
-
-return []
-runTests =  $( [| $(forAllProperties) (quickCheckWithResult (stdArgs { maxSuccess = 1 }) ) |])
diff --git a/ShellCheck/Data.hs b/ShellCheck/Data.hs
deleted file mode 100644
--- a/ShellCheck/Data.hs
+++ /dev/null
@@ -1,117 +0,0 @@
-module ShellCheck.Data where
-
-import ShellCheck.Interface
-import Data.Version (showVersion)
-import Paths_ShellCheck (version)
-
-shellcheckVersion = showVersion version
-
-internalVariables = [
-    -- Generic
-    "", "_", "rest", "REST",
-
-    -- Bash
-    "BASH", "BASHOPTS", "BASHPID", "BASH_ALIASES", "BASH_ARGC",
-    "BASH_ARGV", "BASH_CMDS", "BASH_COMMAND", "BASH_EXECUTION_STRING",
-    "BASH_LINENO", "BASH_REMATCH", "BASH_SOURCE", "BASH_SUBSHELL",
-    "BASH_VERSINFO", "BASH_VERSION", "COMP_CWORD", "COMP_KEY",
-    "COMP_LINE", "COMP_POINT", "COMP_TYPE", "COMP_WORDBREAKS",
-    "COMP_WORDS", "COPROC", "DIRSTACK", "EUID", "FUNCNAME", "GROUPS",
-    "HISTCMD", "HOSTNAME", "HOSTTYPE", "LINENO", "MACHTYPE", "MAPFILE",
-    "OLDPWD", "OPTARG", "OPTIND", "OSTYPE", "PIPESTATUS", "PPID", "PWD",
-    "RANDOM", "READLINE_LINE", "READLINE_POINT", "REPLY", "SECONDS",
-    "SHELLOPTS", "SHLVL", "UID", "BASH_ENV", "BASH_XTRACEFD", "CDPATH",
-    "COLUMNS", "COMPREPLY", "EMACS", "ENV", "FCEDIT", "FIGNORE",
-    "FUNCNEST", "GLOBIGNORE", "HISTCONTROL", "HISTFILE", "HISTFILESIZE",
-    "HISTIGNORE", "HISTSIZE", "HISTTIMEFORMAT", "HOME", "HOSTFILE", "IFS",
-    "IGNOREEOF", "INPUTRC", "LANG", "LC_ALL", "LC_COLLATE", "LC_CTYPE",
-    "LC_MESSAGES", "LC_MONETARY", "LC_NUMERIC", "LC_TIME", "LINES", "MAIL",
-    "MAILCHECK", "MAILPATH", "OPTERR", "PATH", "POSIXLY_CORRECT",
-    "PROMPT_COMMAND", "PROMPT_DIRTRIM", "PS1", "PS2", "PS3", "PS4", "SHELL",
-    "TIMEFORMAT", "TMOUT", "TMPDIR", "auto_resume", "histchars", "COPROC",
-
-    -- Other
-    "USER", "TZ", "TERM", "LOGNAME", "LD_LIBRARY_PATH", "LANGUAGE", "DISPLAY",
-    "HOSTNAME", "KRB5CCNAME", "XAUTHORITY"
-
-    -- Ksh
-    , ".sh.version"
-  ]
-
-variablesWithoutSpaces = [
-    "$", "-", "?", "!",
-    "BASHPID", "BASH_ARGC", "BASH_LINENO", "BASH_SUBSHELL", "EUID", "LINENO",
-    "OPTIND", "PPID", "RANDOM", "SECONDS", "SHELLOPTS", "SHLVL", "UID",
-    "COLUMNS", "HISTFILESIZE", "HISTSIZE", "LINES"
-  ]
-
-arrayVariables = [
-    "BASH_ALIASES", "BASH_ARGC", "BASH_ARGV", "BASH_CMDS", "BASH_LINENO",
-    "BASH_REMATCH", "BASH_SOURCE", "BASH_VERSINFO", "COMP_WORDS", "COPROC",
-    "DIRSTACK", "FUNCNAME", "GROUPS", "MAPFILE", "PIPESTATUS", "COMPREPLY"
-  ]
-
-commonCommands = [
-    "admin", "alias", "ar", "asa", "at", "awk", "basename", "batch",
-    "bc", "bg", "break", "c99", "cal", "cat", "cd", "cflow", "chgrp",
-    "chmod", "chown", "cksum", "cmp", "colon", "comm", "command",
-    "compress", "continue", "cp", "crontab", "csplit", "ctags", "cut",
-    "cxref", "date", "dd", "delta", "df", "diff", "dirname", "dot",
-    "du", "echo", "ed", "env", "eval", "ex", "exec", "exit", "expand",
-    "export", "expr", "fc", "fg", "file", "find", "fold", "fort77",
-    "fuser", "gencat", "get", "getconf", "getopts", "grep", "hash",
-    "head", "iconv", "ipcrm", "ipcs", "jobs", "join", "kill", "lex",
-    "link", "ln", "locale", "localedef", "logger", "logname", "lp",
-    "ls", "m4", "mailx", "make", "man", "mesg", "mkdir", "mkfifo",
-    "more", "mv", "newgrp", "nice", "nl", "nm", "nohup", "od", "paste",
-    "patch", "pathchk", "pax", "pr", "printf", "prs", "ps", "pwd",
-    "qalter", "qdel", "qhold", "qmove", "qmsg", "qrerun", "qrls",
-    "qselect", "qsig", "qstat", "qsub", "read", "readonly", "renice",
-    "return", "rm", "rmdel", "rmdir", "sact", "sccs", "sed", "set",
-    "sh", "shift", "sleep", "sort", "split", "strings", "strip", "stty",
-    "tabs", "tail", "talk", "tee", "test", "time", "times", "touch",
-    "tput", "tr", "trap", "tsort", "tty", "type", "ulimit", "umask",
-    "unalias", "uname", "uncompress", "unexpand", "unget", "uniq",
-    "unlink", "unset", "uucp", "uudecode", "uuencode", "uustat", "uux",
-    "val", "vi", "wait", "wc", "what", "who", "write", "xargs", "yacc",
-    "zcat"
-  ]
-
-nonReadingCommands = [
-    "alias", "basename", "bg", "cal", "cd", "chgrp", "chmod", "chown",
-    "cp", "du", "echo", "export", "fg", "fuser", "getconf", "getopt",
-    "getopts", "ipcrm", "ipcs", "jobs", "kill", "ln", "ls", "locale", "mv",
-    "nice", "printf", "ps", "pwd", "renice", "rm", "rmdir", "set", "sleep",
-    "touch", "trap", "ulimit", "unalias", "uname"
-    ]
-
-sampleWords = [
-    "alpha", "bravo", "charlie", "delta", "echo", "foxtrot",
-    "golf", "hotel", "india", "juliett", "kilo", "lima", "mike",
-    "november", "oscar", "papa", "quebec", "romeo", "sierra",
-    "tango", "uniform", "victor", "whiskey", "xray", "yankee",
-    "zulu"
-  ]
-
-binaryTestOps = [
-    "-nt", "-ot", "-ef", "==", "!=", "<=", ">=", "-eq", "-ne", "-lt", "-le",
-    "-gt", "-ge", "=~", ">", "<", "=", "\\<", "\\>", "\\<=", "\\>="
-  ]
-
-unaryTestOps = [
-    "!", "-a", "-b", "-c", "-d", "-e", "-f", "-g", "-h", "-L", "-k", "-p",
-    "-r", "-s", "-S", "-t", "-u", "-w", "-x", "-O", "-G", "-N", "-z", "-n",
-    "-o", "-v", "-R"
-  ]
-
-shellForExecutable :: String -> Maybe Shell
-shellForExecutable name =
-    case name of
-        "sh"    -> return Sh
-        "bash"  -> return Bash
-        "dash"  -> return Dash
-        "ash"   -> return Dash -- There's also a warning for this.
-        "ksh"   -> return Ksh
-        "ksh88" -> return Ksh
-        "ksh93" -> return Ksh
-        otherwise -> Nothing
diff --git a/ShellCheck/Formatter/CheckStyle.hs b/ShellCheck/Formatter/CheckStyle.hs
deleted file mode 100644
--- a/ShellCheck/Formatter/CheckStyle.hs
+++ /dev/null
@@ -1,95 +0,0 @@
-{-
-    Copyright 2012-2015 Vidar Holen
-
-    This file is part of ShellCheck.
-    http://www.vidarholen.net/contents/shellcheck
-
-    ShellCheck is free software: you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation, either version 3 of the License, or
-    (at your option) any later version.
-
-    ShellCheck is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program.  If not, see <http://www.gnu.org/licenses/>.
--}
-module ShellCheck.Formatter.CheckStyle (format) where
-
-import ShellCheck.Interface
-import ShellCheck.Formatter.Format
-
-import Data.Char
-import Data.List
-import GHC.Exts
-import System.IO
-
-format :: IO Formatter
-format = return Formatter {
-    header = do
-        putStrLn "<?xml version='1.0' encoding='UTF-8'?>"
-        putStrLn "<checkstyle version='4.3'>",
-
-    onFailure = outputError,
-    onResult = outputResults,
-
-    footer = putStrLn "</checkstyle>"
-}
-
-outputResults cr sys =
-    if null comments
-    then outputFile (crFilename cr) "" []
-    else mapM_ outputGroup fileGroups
-  where
-    comments = crComments cr
-    fileGroups = groupWith sourceFile comments
-    outputGroup group = do
-        let filename = sourceFile (head group)
-        result <- (siReadFile sys) filename
-        let contents = either (const "") id result
-        outputFile filename contents group
-
-outputFile filename contents warnings = do
-    let comments = makeNonVirtual warnings contents
-    putStrLn . formatFile filename $ comments
-
-formatFile name comments = concat [
-    "<file ", attr "name" name, ">\n",
-        concatMap formatComment comments,
-    "</file>"
-    ]
-
-formatComment c = concat [
-    "<error ",
-    attr "line" $ show . lineNo $ c,
-    attr "column" $ show . colNo $ c,
-    attr "severity" . severity $ severityText c,
-    attr "message" $ messageText c,
-    attr "source" $ "ShellCheck.SC" ++ show (codeNo c),
-    "/>\n"
-    ]
-
-outputError file error = putStrLn $ concat [
-    "<file ", attr "name" file, ">\n",
-    "<error ",
-        attr "line" "1",
-        attr "column" "1",
-        attr "severity" "error",
-        attr "message" error,
-        attr "source" "ShellCheck",
-    "/>\n",
-    "</file>"
-    ]
-
-
-attr s v = concat [ s, "='", escape v, "' " ]
-escape = concatMap escape'
-escape' c = if isOk c then [c] else "&#" ++ show (ord c) ++ ";"
-isOk x = any ($x) [isAsciiUpper, isAsciiLower, isDigit, (`elem` " ./")]
-
-severity "error" = "error"
-severity "warning" = "warning"
-severity _ = "info"
diff --git a/ShellCheck/Formatter/Format.hs b/ShellCheck/Formatter/Format.hs
deleted file mode 100644
--- a/ShellCheck/Formatter/Format.hs
+++ /dev/null
@@ -1,67 +0,0 @@
-{-
-    Copyright 2012-2015 Vidar Holen
-
-    This file is part of ShellCheck.
-    http://www.vidarholen.net/contents/shellcheck
-
-    ShellCheck is free software: you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation, either version 3 of the License, or
-    (at your option) any later version.
-
-    ShellCheck is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program.  If not, see <http://www.gnu.org/licenses/>.
--}
-module ShellCheck.Formatter.Format where
-
-import ShellCheck.Data
-import ShellCheck.Interface
-
--- A formatter that carries along an arbitrary piece of data
-data Formatter = Formatter {
-    header ::  IO (),
-    onResult :: CheckResult -> SystemInterface IO -> IO (),
-    onFailure :: FilePath -> ErrorMessage -> IO (),
-    footer :: IO ()
-}
-
-sourceFile (PositionedComment pos _ _) = posFile pos
-lineNo (PositionedComment pos _ _) = posLine pos
-endLineNo (PositionedComment _ end _) = posLine end
-colNo  (PositionedComment pos _ _) = posColumn pos
-endColNo  (PositionedComment _ end _) = posColumn end
-codeNo (PositionedComment _ _ (Comment _ code _)) = code
-messageText (PositionedComment _ _ (Comment _ _ t)) = t
-
-severityText :: PositionedComment -> String
-severityText (PositionedComment _ _ (Comment c _ _)) =
-    case c of
-        ErrorC   -> "error"
-        WarningC -> "warning"
-        InfoC    -> "info"
-        StyleC   -> "style"
-
--- Realign comments from a tabstop of 8 to 1
-makeNonVirtual comments contents =
-    map fix comments
-  where
-    ls = lines contents
-    fix c@(PositionedComment start end comment) = PositionedComment start {
-        posColumn = realignColumn lineNo colNo c
-    } end {
-        posColumn = realignColumn endLineNo endColNo c
-    } comment
-    realignColumn lineNo colNo c =
-      if lineNo c > 0 && lineNo c <= fromIntegral (length ls)
-      then real (ls !! fromIntegral (lineNo c - 1)) 0 0 (colNo c)
-      else colNo c
-    real _ r v target | target <= v = r
-    real [] r v _ = r -- should never happen
-    real ('\t':rest) r v target =
-        real rest (r+1) (v + 8 - (v `mod` 8)) target
-    real (_:rest) r v target = real rest (r+1) (v+1) target
diff --git a/ShellCheck/Formatter/GCC.hs b/ShellCheck/Formatter/GCC.hs
deleted file mode 100644
--- a/ShellCheck/Formatter/GCC.hs
+++ /dev/null
@@ -1,65 +0,0 @@
-{-
-    Copyright 2012-2015 Vidar Holen
-
-    This file is part of ShellCheck.
-    http://www.vidarholen.net/contents/shellcheck
-
-    ShellCheck is free software: you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation, either version 3 of the License, or
-    (at your option) any later version.
-
-    ShellCheck is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program.  If not, see <http://www.gnu.org/licenses/>.
--}
-module ShellCheck.Formatter.GCC (format) where
-
-import ShellCheck.Interface
-import ShellCheck.Formatter.Format
-
-import Data.List
-import GHC.Exts
-import System.IO
-
-format :: IO Formatter
-format = return Formatter {
-    header = return (),
-    footer = return (),
-    onFailure = outputError,
-    onResult = outputAll
-}
-
-outputError file error = hPutStrLn stderr $ file ++ ": " ++ error
-
-outputAll cr sys = mapM_ f groups
-  where
-    comments = crComments cr
-    groups = groupWith sourceFile comments
-    f :: [PositionedComment] -> IO ()
-    f group = do
-        let filename = sourceFile (head group)
-        result <- (siReadFile sys) filename
-        let contents = either (const "") id result
-        outputResult filename contents group
-
-outputResult filename contents warnings = do
-    let comments = makeNonVirtual warnings contents
-    mapM_ (putStrLn . formatComment filename) comments
-
-formatComment filename c = concat [
-    filename, ":",
-    show $ lineNo c, ":",
-    show $ colNo c, ": ",
-    case severityText c of
-        "error" -> "error"
-        "warning" -> "warning"
-        _ -> "note",
-    ": ",
-    concat . lines $ messageText c,
-    " [SC", show $ codeNo c, "]"
-  ]
diff --git a/ShellCheck/Formatter/JSON.hs b/ShellCheck/Formatter/JSON.hs
deleted file mode 100644
--- a/ShellCheck/Formatter/JSON.hs
+++ /dev/null
@@ -1,60 +0,0 @@
-{-
-    Copyright 2012-2015 Vidar Holen
-
-    This file is part of ShellCheck.
-    http://www.vidarholen.net/contents/shellcheck
-
-    ShellCheck is free software: you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation, either version 3 of the License, or
-    (at your option) any later version.
-
-    ShellCheck is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program.  If not, see <http://www.gnu.org/licenses/>.
--}
-module ShellCheck.Formatter.JSON (format) where
-
-import ShellCheck.Interface
-import ShellCheck.Formatter.Format
-
-import Data.IORef
-import GHC.Exts
-import System.IO
-import Text.JSON
-
-format = do
-    ref <- newIORef []
-    return Formatter {
-        header = return (),
-        onResult = collectResult ref,
-        onFailure = outputError,
-        footer = finish ref
-    }
-
-instance JSON (PositionedComment) where
-  showJSON comment@(PositionedComment start end (Comment level code string)) = makeObj [
-      ("file", showJSON $ posFile start),
-      ("line", showJSON $ posLine start),
-      ("endLine", showJSON $ posLine end),
-      ("column", showJSON $ posColumn start),
-      ("endColumn", showJSON $ posColumn end),
-      ("level", showJSON $ severityText comment),
-      ("code", showJSON code),
-      ("message", showJSON string)
-      ]
-
-  readJSON = undefined
-
-outputError file msg = hPutStrLn stderr $ file ++ ": " ++ msg
-collectResult ref result _ =
-    modifyIORef ref (\x -> crComments result ++ x)
-
-finish ref = do
-    list <- readIORef ref
-    putStrLn $ encodeStrict list
-
diff --git a/ShellCheck/Formatter/TTY.hs b/ShellCheck/Formatter/TTY.hs
deleted file mode 100644
--- a/ShellCheck/Formatter/TTY.hs
+++ /dev/null
@@ -1,98 +0,0 @@
-{-
-    Copyright 2012-2015 Vidar Holen
-
-    This file is part of ShellCheck.
-    http://www.vidarholen.net/contents/shellcheck
-
-    ShellCheck is free software: you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation, either version 3 of the License, or
-    (at your option) any later version.
-
-    ShellCheck is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program.  If not, see <http://www.gnu.org/licenses/>.
--}
-module ShellCheck.Formatter.TTY (format) where
-
-import ShellCheck.Interface
-import ShellCheck.Formatter.Format
-
-import Data.List
-import GHC.Exts
-import System.Info
-import System.IO
-
-format :: FormatterOptions -> IO Formatter
-format options = return Formatter {
-    header = return (),
-    footer = return (),
-    onFailure = outputError options,
-    onResult = outputResult options
-}
-
-colorForLevel level =
-    case level of
-        "error"   -> 31 -- red
-        "warning" -> 33 -- yellow
-        "info"    -> 32 -- green
-        "style"   -> 32 -- green
-        "message" -> 1 -- bold
-        "source"  -> 0 -- none
-        _ -> 0         -- none
-
-outputError options file error = do
-    color <- getColorFunc $ foColorOption options
-    hPutStrLn stderr $ color "error" $ file ++ ": " ++ error
-
-outputResult options result sys = do
-    color <- getColorFunc $ foColorOption options
-    let comments = crComments result
-    let fileGroups = groupWith sourceFile comments
-    mapM_ (outputForFile color sys) fileGroups
-
-outputForFile color sys comments = do
-    let fileName = sourceFile (head comments)
-    result <- (siReadFile sys) fileName
-    let contents = either (const "") id result
-    let fileLines = lines contents
-    let lineCount = fromIntegral $ length fileLines
-    let groups = groupWith lineNo comments
-    mapM_ (\x -> do
-        let lineNum = lineNo (head x)
-        let line = if lineNum < 1 || lineNum > lineCount
-                        then ""
-                        else fileLines !! fromIntegral (lineNum - 1)
-        putStrLn ""
-        putStrLn $ color "message" $
-           "In " ++ fileName ++" line " ++ show lineNum ++ ":"
-        putStrLn (color "source" line)
-        mapM_ (\c -> putStrLn (color (severityText c) $ cuteIndent c)) x
-        putStrLn ""
-      ) groups
-
-cuteIndent :: PositionedComment -> String
-cuteIndent comment =
-    replicate (fromIntegral $ colNo comment - 1) ' ' ++
-        "^-- " ++ code (codeNo comment) ++ ": " ++ messageText comment
-
-code code = "SC" ++ show code
-
-getColorFunc colorOption = do
-    term <- hIsTerminalDevice stdout
-    let windows = "mingw" `isPrefixOf` os
-    let isUsableTty = term && not windows
-    let useColor = case colorOption of
-                       ColorAlways -> True
-                       ColorNever -> False
-                       ColorAuto -> isUsableTty
-    return $ if useColor then colorComment else const id
-  where
-    colorComment level comment =
-        ansi (colorForLevel level) ++ comment ++ clear
-    clear = ansi 0
-    ansi n = "\x1B[" ++ show n ++ "m"
diff --git a/ShellCheck/Interface.hs b/ShellCheck/Interface.hs
deleted file mode 100644
--- a/ShellCheck/Interface.hs
+++ /dev/null
@@ -1,121 +0,0 @@
-{-
-    Copyright 2012-2015 Vidar Holen
-
-    This file is part of ShellCheck.
-    http://www.vidarholen.net/contents/shellcheck
-
-    ShellCheck is free software: you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation, either version 3 of the License, or
-    (at your option) any later version.
-
-    ShellCheck is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program.  If not, see <http://www.gnu.org/licenses/>.
--}
-module ShellCheck.Interface where
-
-import ShellCheck.AST
-import Control.Monad.Identity
-import qualified Data.Map as Map
-
-
-newtype SystemInterface m = SystemInterface {
-    -- Read a file by filename, or return an error
-    siReadFile :: String -> m (Either ErrorMessage String)
-}
-
--- ShellCheck input and output
-data CheckSpec = CheckSpec {
-    csFilename :: String,
-    csScript :: String,
-    csCheckSourced :: Bool,
-    csExcludedWarnings :: [Integer],
-    csShellTypeOverride :: Maybe Shell
-} deriving (Show, Eq)
-
-data CheckResult = CheckResult {
-    crFilename :: String,
-    crComments :: [PositionedComment]
-} deriving (Show, Eq)
-
-emptyCheckSpec :: CheckSpec
-emptyCheckSpec = CheckSpec {
-    csFilename = "",
-    csScript = "",
-    csCheckSourced = False,
-    csExcludedWarnings = [],
-    csShellTypeOverride = Nothing
-}
-
--- Parser input and output
-data ParseSpec = ParseSpec {
-    psFilename :: String,
-    psScript :: String,
-    psCheckSourced :: Bool
-} deriving (Show, Eq)
-
-data ParseResult = ParseResult {
-    prComments :: [PositionedComment],
-    prTokenPositions :: Map.Map Id Position,
-    prRoot :: Maybe Token
-} deriving (Show, Eq)
-
--- Analyzer input and output
-data AnalysisSpec = AnalysisSpec {
-    asScript :: Token,
-    asShellType :: Maybe Shell,
-    asExecutionMode :: ExecutionMode,
-    asCheckSourced :: Bool
-}
-
-newtype AnalysisResult = AnalysisResult {
-    arComments :: [TokenComment]
-}
-
-
--- Formatter options
-newtype FormatterOptions = FormatterOptions {
-    foColorOption :: ColorOption
-}
-
-
--- Supporting data types
-data Shell = Ksh | Sh | Bash | Dash deriving (Show, Eq)
-data ExecutionMode = Executed | Sourced deriving (Show, Eq)
-
-type ErrorMessage = String
-type Code = Integer
-
-data Severity = ErrorC | WarningC | InfoC | StyleC deriving (Show, Eq, Ord)
-data Position = Position {
-    posFile :: String,    -- Filename
-    posLine :: Integer,   -- 1 based source line
-    posColumn :: Integer  -- 1 based source column, where tabs are 8
-} deriving (Show, Eq)
-
-data Comment = Comment Severity Code String deriving (Show, Eq)
-data PositionedComment = PositionedComment Position Position Comment deriving (Show, Eq)
-data TokenComment = TokenComment Id Comment deriving (Show, Eq)
-
-data ColorOption =
-    ColorAuto
-    | ColorAlways
-    | ColorNever
-  deriving (Ord, Eq, Show)
-
--- For testing
-mockedSystemInterface :: [(String, String)] -> SystemInterface Identity
-mockedSystemInterface files = SystemInterface {
-    siReadFile = rf
-}
-  where
-    rf file =
-        case filter ((== file) . fst) files of
-            [] -> return $ Left "File not included in mock."
-            [(_, contents)] -> return $ Right contents
-
diff --git a/ShellCheck/Parser.hs b/ShellCheck/Parser.hs
deleted file mode 100644
--- a/ShellCheck/Parser.hs
+++ /dev/null
@@ -1,2992 +0,0 @@
-{-
-    Copyright 2012-2015 Vidar Holen
-
-    This file is part of ShellCheck.
-    http://www.vidarholen.net/contents/shellcheck
-
-    ShellCheck is free software: you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation, either version 3 of the License, or
-    (at your option) any later version.
-
-    ShellCheck is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program.  If not, see <http://www.gnu.org/licenses/>.
--}
-{-# LANGUAGE NoMonomorphismRestriction, TemplateHaskell, FlexibleContexts #-}
-module ShellCheck.Parser (parseScript, runTests) where
-
-import ShellCheck.AST
-import ShellCheck.ASTLib
-import ShellCheck.Data
-import ShellCheck.Interface
-
-import Control.Applicative ((<*))
-import Control.Monad
-import Control.Monad.Identity
-import Control.Monad.Trans
-import Data.Char
-import Data.Functor
-import Data.List (isPrefixOf, isInfixOf, isSuffixOf, partition, sortBy, intercalate, nub)
-import Data.Maybe
-import Data.Monoid
-import Debug.Trace
-import GHC.Exts (sortWith)
-import Prelude hiding (readList)
-import System.IO
-import Text.Parsec hiding (runParser, (<?>))
-import Text.Parsec.Error
-import Text.Parsec.Pos
-import qualified Control.Monad.Reader as Mr
-import qualified Control.Monad.State as Ms
-import qualified Data.Map as Map
-
-import Test.QuickCheck.All (quickCheckAll)
-
-type SCBase m = Mr.ReaderT (Environment m) (Ms.StateT SystemState m)
-type SCParser m v = ParsecT String UserState (SCBase m) v
-
-backslash :: Monad m => SCParser m Char
-backslash = char '\\'
-linefeed :: Monad m => SCParser m Char
-linefeed = do
-    optional carriageReturn
-    c <- char '\n'
-    readPendingHereDocs
-    return c
-singleQuote = char '\''
-doubleQuote = char '"'
-variableStart = upper <|> lower <|> oneOf "_"
-variableChars = upper <|> lower <|> digit <|> oneOf "_"
--- Chars to allow in function names
-functionChars = variableChars <|> oneOf ":+?-./^"
--- Chars to allow in functions using the 'function' keyword
-extendedFunctionChars = functionChars <|> oneOf "[]*=!"
-specialVariable = oneOf "@*#?-$!"
-paramSubSpecialChars = oneOf "/:+-=%"
-quotableChars = "|&;<>()\\ '\t\n\r\xA0" ++ doubleQuotableChars
-quotable = almostSpace <|> oneOf quotableChars
-bracedQuotable = oneOf "}\"$`'"
-doubleQuotableChars = "\\\"$`"
-doubleQuotable = oneOf doubleQuotableChars
-whitespace = oneOf " \t" <|> carriageReturn <|> almostSpace <|> linefeed
-linewhitespace = oneOf " \t" <|> almostSpace
-
-suspectCharAfterQuotes = variableChars <|> char '%'
-
-extglobStartChars = "?*@!+"
-extglobStart = oneOf extglobStartChars
-
-unicodeDoubleQuotes = "\x201C\x201D\x2033\x2036"
-unicodeSingleQuotes = "\x2018\x2019"
-
-prop_spacing = isOk spacing "  \\\n # Comment"
-spacing = do
-    x <- many (many1 linewhitespace <|> try (string "\\\n" >> return ""))
-    optional readComment
-    return $ concat x
-
-spacing1 = do
-    spacing <- spacing
-    when (null spacing) $ fail "Expected whitespace"
-    return spacing
-
-prop_allspacing = isOk allspacing "#foo"
-prop_allspacing2 = isOk allspacing " #foo\n # bar\n#baz\n"
-prop_allspacing3 = isOk allspacing "#foo\n#bar\n#baz\n"
-allspacing = do
-    s <- spacing
-    more <- option False (linefeed >> return True)
-    if more then do
-        rest <- allspacing
-        return $ s ++ "\n" ++ rest
-      else
-        return s
-
-allspacingOrFail = do
-    s <- allspacing
-    when (null s) $ fail "Expected whitespace"
-
-readUnicodeQuote = do
-    pos <- getPosition
-    c <- oneOf (unicodeSingleQuotes ++ unicodeDoubleQuotes)
-    parseProblemAt pos WarningC 1110 "This is a unicode quote. Delete and retype it (or quote to make literal)."
-    id <- getNextIdAt pos
-    return $ T_Literal id [c]
-
-carriageReturn = do
-    parseNote ErrorC 1017 "Literal carriage return. Run script through tr -d '\\r' ."
-    char '\r'
-
-almostSpace =
-    choice [
-        check '\xA0' "unicode non-breaking space",
-        check '\x200B' "unicode zerowidth space"
-    ]
-  where
-    check c name = do
-        parseNote ErrorC 1018 $ "This is a " ++ name ++ ". Delete and retype it."
-        char c
-        return ' '
-
---------- Message/position annotation on top of user state
-data Note = Note Id Severity Code String deriving (Show, Eq)
-data ParseNote = ParseNote SourcePos SourcePos Severity Code String deriving (Show, Eq)
-data Context =
-        ContextName SourcePos String
-        | ContextAnnotation [Annotation]
-        | ContextSource String
-    deriving (Show)
-
-data HereDocContext =
-        HereDocPending Token -- on linefeed, read this T_HereDoc
-        | HereDocBoundary -- but don't consider heredocs before this
-    deriving (Show)
-
-data UserState = UserState {
-    lastId :: Id,
-    positionMap :: Map.Map Id SourcePos,
-    parseNotes :: [ParseNote],
-    hereDocMap :: Map.Map Id [Token],
-    pendingHereDocs :: [HereDocContext]
-}
-initialUserState = UserState {
-    lastId = Id $ -1,
-    positionMap = Map.empty,
-    parseNotes = [],
-    hereDocMap = Map.empty,
-    pendingHereDocs = []
-}
-
-codeForParseNote (ParseNote _ _ _ code _) = code
-noteToParseNote map (Note id severity code message) =
-        ParseNote pos pos severity code message
-    where
-        pos = fromJust $ Map.lookup id map
-
-getLastId = lastId <$> getState
-
-getNextIdAt sourcepos = do
-    state <- getState
-    let newId = incId (lastId state)
-    let newMap = Map.insert newId sourcepos (positionMap state)
-    putState $ state {
-        lastId = newId,
-        positionMap = newMap
-    }
-    return newId
-  where incId (Id n) = Id $ n+1
-
-getNextId :: Monad m => SCParser m Id
-getNextId = do
-    pos <- getPosition
-    getNextIdAt pos
-
-addToHereDocMap id list = do
-    state <- getState
-    let map = hereDocMap state
-    putState $ state {
-        hereDocMap = Map.insert id list map
-    }
-
-withHereDocBoundary p = do
-    pushBoundary
-    do
-        v <- p
-        popBoundary
-        return v
-     <|> do
-        popBoundary
-        fail ""
-  where
-    pushBoundary = do
-        state <- getState
-        let docs = pendingHereDocs state
-        putState $ state {
-            pendingHereDocs = HereDocBoundary : docs
-        }
-    popBoundary = do
-        state <- getState
-        let docs = tail $ dropWhile (not . isHereDocBoundary) $
-                    pendingHereDocs state
-        putState $ state {
-            pendingHereDocs = docs
-        }
-
-addPendingHereDoc t = do
-    state <- getState
-    let docs = pendingHereDocs state
-    putState $ state {
-        pendingHereDocs = HereDocPending t : docs
-    }
-
-popPendingHereDocs = do
-    state <- getState
-    let (pending, boundary) = break isHereDocBoundary $ pendingHereDocs state
-    putState $ state {
-        pendingHereDocs = boundary
-    }
-    return . map extract . reverse $ pendingHereDocs state
-  where
-    extract (HereDocPending t) = t
-
-isHereDocBoundary x = case x of
-    HereDocBoundary -> True
-    otherwise -> False
-
-getMap = positionMap <$> getState
-getParseNotes = parseNotes <$> getState
-
-addParseNote n = do
-    irrelevant <- shouldIgnoreCode (codeForParseNote n)
-    unless irrelevant $ do
-        state <- getState
-        putState $ state {
-            parseNotes = n : parseNotes state
-        }
-
-shouldIgnoreCode code = do
-    context <- getCurrentContexts
-    checkSourced <- Mr.asks checkSourced
-    return $ any (disabling checkSourced) context
-  where
-    disabling checkSourced item =
-        case item of
-            ContextAnnotation list -> any disabling' list
-            ContextSource _ -> not $ checkSourced
-            _ -> False
-    disabling' (DisableComment n) = code == n
-    disabling' _ = False
-
-shouldFollow file = do
-    context <- getCurrentContexts
-    if any isThisFile context
-      then return False
-      else
-        if length (filter isSource context) >= 100
-          then do
-            parseProblem ErrorC 1092 "Stopping at 100 'source' frames :O"
-            return False
-          else
-            return True
-  where
-    isSource (ContextSource _) = True
-    isSource _ = False
-    isThisFile (ContextSource name) | name == file = True
-    isThisFile _= False
-
-getSourceOverride = do
-    context <- getCurrentContexts
-    return . msum . map findFile $ takeWhile isSameFile context
-  where
-    isSameFile (ContextSource _) = False
-    isSameFile _ = True
-
-    findFile (ContextAnnotation list) = msum $ map getFile list
-    findFile _ = Nothing
-    getFile (SourceOverride str) = Just str
-    getFile _ = Nothing
-
--- Store potential parse problems outside of parsec
-
-data SystemState = SystemState {
-    contextStack :: [Context],
-    parseProblems :: [ParseNote]
-}
-initialSystemState = SystemState {
-    contextStack = [],
-    parseProblems = []
-}
-
-data Environment m = Environment {
-    systemInterface :: SystemInterface m,
-    checkSourced :: Bool
-}
-
-parseProblem level code msg = do
-    pos <- getPosition
-    parseProblemAt pos level code msg
-
-setCurrentContexts c = Ms.modify (\state -> state { contextStack = c })
-getCurrentContexts = contextStack <$> Ms.get
-
-popContext = do
-    v <- getCurrentContexts
-    if not $ null v
-        then do
-            let (a:r) = v
-            setCurrentContexts r
-            return $ Just a
-        else
-            return Nothing
-
-pushContext c = do
-    v <- getCurrentContexts
-    setCurrentContexts (c:v)
-
-parseProblemAtWithEnd start end level code msg = do
-    irrelevant <- shouldIgnoreCode code
-    unless irrelevant $
-        Ms.modify (\state -> state {
-            parseProblems = note:parseProblems state
-        })
-  where
-    note = ParseNote start end level code msg
-
-parseProblemAt pos = parseProblemAtWithEnd pos pos
-
-parseProblemAtId :: Monad m => Id -> Severity -> Integer -> String -> SCParser m ()
-parseProblemAtId id level code msg = do
-    map <- getMap
-    let pos = Map.findWithDefault
-                (error "Internal error (no position for id). Please report.") id map
-    parseProblemAt pos level code msg
-
--- Store non-parse problems inside
-
-parseNote c l a = do
-    pos <- getPosition
-    parseNoteAt pos c l a
-
-parseNoteAt pos c l a = addParseNote $ ParseNote pos pos c l a
-
-parseNoteAtWithEnd start end c l a = addParseNote $ ParseNote start end c l a
-
---------- Convenient combinators
-thenSkip main follow = do
-    r <- main
-    optional follow
-    return r
-
-unexpecting s p = try $
-    (try p >> fail ("Unexpected " ++ s)) <|> return ()
-
-notFollowedBy2 = unexpecting ""
-
-reluctantlyTill p end =
-    (lookAhead (void (try end) <|> eof) >> return []) <|> do
-        x <- p
-        more <- reluctantlyTill p end
-        return $ x:more
-      <|> return []
-
-reluctantlyTill1 p end = do
-    notFollowedBy2 end
-    x <- p
-    more <- reluctantlyTill p end
-    return $ x:more
-
-attempting rest branch =
-    (try branch >> rest) <|> rest
-
-orFail parser errorAction =
-    try parser <|> (errorAction >>= fail)
-
--- Construct a node with a parser, e.g. T_Literal `withParser` (readGenericLiteral ",")
-withParser node parser = do
-    id <- getNextId
-    contents <- parser
-    return $ node id contents
-
-wasIncluded p = option False (p >> return True)
-
-acceptButWarn parser level code note =
-    optional $ try (do
-        pos <- getPosition
-        parser
-        parseProblemAt pos level code note
-      )
-
-withContext entry p = do
-    pushContext entry
-    do
-        v <- p
-        popContext
-        return v
-     <|> do -- p failed without consuming input, abort context
-        v <- popContext
-        fail ""
-
-called s p = do
-    pos <- getPosition
-    withContext (ContextName pos s) p
-
-withAnnotations anns =
-    withContext (ContextAnnotation anns)
-
-readConditionContents single =
-    readCondContents `attempting` lookAhead (do
-                                pos <- getPosition
-                                s <- many1 letter
-                                when (s `elem` commonCommands) $
-                                    parseProblemAt pos WarningC 1014 "Use 'if cmd; then ..' to check exit code, or 'if [[ $(cmd) == .. ]]' to check output.")
-
-  where
-    spacingOrLf = condSpacing True
-    condSpacing required = do
-        pos <- getPosition
-        space <- allspacing
-        when (required && null space) $
-            parseProblemAt pos ErrorC 1035 "You are missing a required space here."
-        when (single && '\n' `elem` space) $
-            parseProblemAt pos ErrorC 1080 "When breaking lines in [ ], you need \\ before the linefeed."
-        return space
-
-    typ = if single then SingleBracket else DoubleBracket
-    readCondBinaryOp = try $ do
-        optional guardArithmetic
-        id <- getNextId
-        op <- getOp
-        spacingOrLf
-        return op
-      where
-        flaglessOps = [ "==", "!=", "<=", ">=", "=~", ">", "<", "=" ]
-
-        getOp = do
-            id <- getNextId
-            op <- readRegularOrEscaped anyOp
-            return $ TC_Binary id typ op
-
-        anyOp = flagOp <|> flaglessOp <|> fail
-                    "Expected comparison operator (don't wrap commands in []/[[]])"
-        flagOp = try $ do
-            s <- readOp
-            when (s == "-a" || s == "-o") $ fail "Unexpected operator"
-            return s
-        flaglessOp =
-            choice $ map (try . string) flaglessOps
-
-        -- hacks to read quoted operators without having to read a shell word
-    readEscaped p = try $ withEscape <|> withQuotes
-      where
-        withEscape = do
-            char '\\'
-            escaped <$> p
-        withQuotes = do
-            c <- oneOf "'\""
-            s <- p
-            char c
-            return $ escaped s
-        escaped s = if any (`elem` s) "<>()" then '\\':s else s
-
-    readRegularOrEscaped p = readEscaped p <|> p
-
-
-    guardArithmetic = do
-        try . lookAhead $ void (oneOf "+*/%") <|> void (string "- ")
-        parseProblem ErrorC 1076 $
-            if single
-            then "Trying to do math? Use e.g. [ $((i/2+7)) -ge 18 ]."
-            else "Trying to do math? Use e.g. [[ $((i/2+7)) -ge 18 ]]."
-
-    readCondUnaryExp = do
-      op <- readCondUnaryOp
-      pos <- getPosition
-      liftM op readCondWord `orFail` do
-          parseProblemAt pos ErrorC 1019 "Expected this to be an argument to the unary condition."
-          return "Expected an argument for the unary operator"
-
-    readCondUnaryOp = try $ do
-        id <- getNextId
-        s <- readOp
-        spacingOrLf
-        return $ TC_Unary id typ s
-
-    readOp = try $ do
-        char '-' <|> weirdDash
-        s <- many1 letter <|> fail "Expected a test operator"
-        return ('-':s)
-
-    weirdDash = do
-        pos <- getPosition
-        oneOf "\x058A\x05BE\x2010\x2011\x2012\x2013\x2014\x2015\xFE63\xFF0D"
-        parseProblemAt pos ErrorC 1100
-            "This is a unicode dash. Delete and retype as ASCII minus."
-        return '-'
-
-    readCondWord = do
-        notFollowedBy2 (try (spacing >> string "]"))
-        x <- readNormalWord
-        pos <- getPosition
-        when (endedWith "]" x) $ do
-            parseProblemAt pos ErrorC 1020 $
-                "You need a space before the " ++ (if single then "]" else "]]") ++ "."
-            fail "Missing space before ]"
-        when (single && endedWith ")" x) $ do
-            parseProblemAt pos ErrorC 1021
-                "You need a space before the \\)"
-            fail "Missing space before )"
-        void spacing
-        return x
-      where endedWith str (T_NormalWord id s@(_:_)) =
-                case last s of T_Literal id s -> str `isSuffixOf` s
-                               _ -> False
-            endedWith _ _ = False
-
-    readCondAndOp = do
-        id <- getNextId
-        x <- try (readAndOrOp "&&" False <|> readAndOrOp "-a" True)
-        return $ TC_And id typ x
-
-    readCondOrOp = do
-        optional guardArithmetic
-        id <- getNextId
-        x <- try (readAndOrOp "||" False <|> readAndOrOp "-o" True)
-        return $ TC_Or id typ x
-
-    readAndOrOp op requiresSpacing = do
-        optional $ lookAhead weirdDash
-        x <- string op
-        condSpacing requiresSpacing
-        return x
-
-    readCondNullaryOrBinary = do
-      id <- getNextId
-      x <- readCondWord `attempting` (do
-              pos <- getPosition
-              lookAhead (char '[')
-              parseProblemAt pos ErrorC 1026 $ if single
-                  then "If grouping expressions inside [..], use \\( ..\\)."
-                  else "If grouping expressions inside [[..]], use ( .. )."
-            )
-      (do
-            pos <- getPosition
-            isRegex <- regexOperatorAhead
-            op <- readCondBinaryOp
-            y <- if isRegex
-                    then readRegex
-                    else  readCondWord <|> (parseProblemAt pos ErrorC 1027 "Expected another argument for this operator." >> mzero)
-            return (x `op` y)
-          ) <|> ( do
-            checkTrailingOp x
-            return $ TC_Nullary id typ x
-          )
-
-    checkTrailingOp x = fromMaybe (return ()) $ do
-        (T_Literal id str) <- getTrailingUnquotedLiteral x
-        trailingOp <- listToMaybe (filter (`isSuffixOf` str) binaryTestOps)
-        return $ parseProblemAtId id ErrorC 1108 $
-            "You need a space before and after the " ++ trailingOp ++ " ."
-
-    readCondGroup = do
-        id <- getNextId
-        pos <- getPosition
-        lparen <- try $ readRegularOrEscaped (string "(")
-        when (single && lparen == "(") $
-            singleWarning pos
-        when (not single && lparen == "\\(") $
-            doubleWarning pos
-        condSpacing single
-        x <- readCondContents
-        cpos <- getPosition
-        rparen <- readRegularOrEscaped (string ")")
-        condSpacing single
-        when (single && rparen == ")") $
-            singleWarning cpos
-        when (not single && rparen == "\\)") $
-            doubleWarning cpos
-        return $ TC_Group id typ x
-
-      where
-        singleWarning pos =
-            parseProblemAt pos ErrorC 1028 "In [..] you have to escape \\( \\) or preferably combine [..] expressions."
-        doubleWarning pos =
-            parseProblemAt pos ErrorC 1029 "In [[..]] you shouldn't escape ( or )."
-
-
-    -- Currently a bit of a hack since parsing rules are obscure
-    regexOperatorAhead = lookAhead (do
-        try (string "=~") <|> try (string "~=")
-        return True)
-          <|> return False
-    readRegex = called "regex" $ do
-        id <- getNextId
-        parts <- many1 (
-                readGroup <|>
-                readSingleQuoted <|>
-                readDoubleQuoted <|>
-                readDollarExpression <|>
-                readNormalLiteral "( " <|>
-                readPipeLiteral <|>
-                readGlobLiteral)
-        void spacing
-        return $ T_NormalWord id parts
-      where
-        readGlobLiteral = do
-            id <- getNextId
-            s <- extglobStart <|> oneOf "{}[]$"
-            return $ T_Literal id [s]
-        readGroup = called "regex grouping" $ do
-            id <- getNextId
-            char '('
-            parts <- many (readGroup <|> readSingleQuoted <|> readDoubleQuoted <|> readDollarExpression <|> readRegexLiteral <|> readGlobLiteral)
-            char ')'
-            return $ T_NormalWord id parts
-        readRegexLiteral = do
-            id <- getNextId
-            str <- readGenericLiteral1 (singleQuote <|> doubleQuotable <|> oneOf "()")
-            return $ T_Literal id str
-        readPipeLiteral = do
-            id <- getNextId
-            str <- string "|"
-            return $ T_Literal id str
-
-    readCondTerm = do
-        term <- readCondNot <|> readCondExpr
-        condSpacing False
-        return term
-
-    readCondNot = do
-        id <- getNextId
-        char '!'
-        spacingOrLf
-        expr <- readCondExpr
-        return $ TC_Unary id typ "!" expr
-
-    readCondExpr =
-      readCondGroup <|> readCondUnaryExp <|> readCondNullaryOrBinary
-
-    readCondOr = chainl1 readCondAnd readCondAndOp
-    readCondAnd = chainl1 readCondTerm readCondOrOp
-    readCondContents = readCondOr
-
-
-prop_a1 = isOk readArithmeticContents " n++ + ++c"
-prop_a2 = isOk readArithmeticContents "$N*4-(3,2)"
-prop_a3 = isOk readArithmeticContents "n|=2<<1"
-prop_a4 = isOk readArithmeticContents "n &= 2 **3"
-prop_a5 = isOk readArithmeticContents "1 |= 4 && n >>= 4"
-prop_a6 = isOk readArithmeticContents " 1 | 2 ||3|4"
-prop_a7 = isOk readArithmeticContents "3*2**10"
-prop_a8 = isOk readArithmeticContents "3"
-prop_a9 = isOk readArithmeticContents "a^!-b"
-prop_a10= isOk readArithmeticContents "! $?"
-prop_a11= isOk readArithmeticContents "10#08 * 16#f"
-prop_a12= isOk readArithmeticContents "\"$((3+2))\" + '37'"
-prop_a13= isOk readArithmeticContents "foo[9*y+x]++"
-prop_a14= isOk readArithmeticContents "1+`echo 2`"
-prop_a15= isOk readArithmeticContents "foo[`echo foo | sed s/foo/4/g` * 3] + 4"
-prop_a16= isOk readArithmeticContents "$foo$bar"
-prop_a17= isOk readArithmeticContents "i<(0+(1+1))"
-prop_a18= isOk readArithmeticContents "a?b:c"
-prop_a19= isOk readArithmeticContents "\\\n3 +\\\n  2"
-prop_a20= isOk readArithmeticContents "a ? b ? c : d : e"
-prop_a21= isOk readArithmeticContents "a ? b : c ? d : e"
-prop_a22= isOk readArithmeticContents "!!a"
-readArithmeticContents :: Monad m => SCParser m Token
-readArithmeticContents =
-    readSequence
-  where
-    spacing =
-        let lf = try (string "\\\n") >> return '\n'
-        in many (whitespace <|> lf)
-
-    splitBy x ops = chainl1 x (readBinary ops)
-    readBinary ops = readComboOp ops TA_Binary
-    readComboOp op token = do
-        id <- getNextId
-        op <- choice (map (\x -> try $ do
-                                        s <- string x
-                                        failIfIncompleteOp
-                                        return s
-                            ) op)
-        spacing
-        return $ token id op
-
-    failIfIncompleteOp = notFollowedBy2 $ oneOf "&|<>="
-
-    -- Read binary minus, but also check for -lt, -gt and friends:
-    readMinusOp = do
-        id <- getNextId
-        pos <- getPosition
-        try $ do
-            char '-'
-            failIfIncompleteOp
-        optional $ do
-            (str, alt) <- lookAhead . choice $ map tryOp [
-                ("lt", "<"),
-                ("gt", ">"),
-                ("le", "<="),
-                ("ge", ">="),
-                ("eq", "=="),
-                ("ne", "!=")
-              ]
-            parseProblemAt pos ErrorC 1106 $ "In arithmetic contexts, use " ++ alt ++ " instead of -" ++ str
-        spacing
-        return $ TA_Binary id "-"
-      where
-        tryOp (str, alt) = try $ do
-            string str
-            spacing1
-            return (str, alt)
-
-
-    readArrayIndex = do
-        id <- getNextId
-        char '['
-        middle <- readArithmeticContents
-        char ']'
-        return $ TA_Index id middle
-
-    literal s = do
-        id <- getNextId
-        string s
-        return $ T_Literal id s
-
-    readArithmeticLiteral =
-        readArrayIndex <|> literal "#"
-
-    readExpansion = do
-        id <- getNextId
-        pieces <- many1 $ choice [
-            readArithmeticLiteral,
-            readSingleQuoted,
-            readDoubleQuoted,
-            readNormalDollar,
-            readBraced,
-            readUnquotedBackTicked,
-            readNormalLiteral "+-*/=%^,]?:"
-            ]
-        spacing
-        return $ TA_Expansion id pieces
-
-    readGroup = do
-        char '('
-        s <- readSequence
-        char ')'
-        spacing
-        return s
-
-    readArithTerm = readGroup <|> readExpansion
-
-    readSequence = do
-        spacing
-        id <- getNextId
-        l <- readAssignment `sepBy` (char ',' >> spacing)
-        return $ TA_Sequence id l
-
-    readAssignment = chainr1 readTrinary readAssignmentOp
-    readAssignmentOp = readComboOp ["=", "*=", "/=", "%=", "+=", "-=", "<<=", ">>=", "&=", "^=", "|="] TA_Assignment
-
-    readTrinary = do
-        x <- readLogicalOr
-        do
-            id <- getNextId
-            string "?"
-            spacing
-            y <- readTrinary
-            string ":"
-            spacing
-            z <- readTrinary
-            return $ TA_Trinary id x y z
-         <|>
-          return x
-
-    readLogicalOr  = readLogicalAnd `splitBy` ["||"]
-    readLogicalAnd = readBitOr `splitBy` ["&&"]
-    readBitOr  = readBitXor `splitBy` ["|"]
-    readBitXor = readBitAnd `splitBy` ["^"]
-    readBitAnd = readEquated `splitBy` ["&"]
-    readEquated = readCompared `splitBy` ["==", "!="]
-    readCompared = readShift `splitBy` ["<=", ">=", "<", ">"]
-    readShift = readAddition `splitBy` ["<<", ">>"]
-    readAddition = chainl1 readMultiplication (readBinary ["+"] <|> readMinusOp)
-    readMultiplication = readExponential `splitBy` ["*", "/", "%"]
-    readExponential = readAnyNegated `splitBy` ["**"]
-
-    readAnyNegated = readNegated <|> readAnySigned
-    readNegated = do
-        id <- getNextId
-        op <- oneOf "!~"
-        spacing
-        x <- readAnyNegated
-        return $ TA_Unary id [op] x
-
-    readAnySigned = readSigned <|> readAnycremented
-    readSigned = do
-        id <- getNextId
-        op <- choice (map readSignOp "+-")
-        spacing
-        x <- readAnycremented
-        return $ TA_Unary id [op] x
-     where
-        readSignOp c = try $ do
-            char c
-            notFollowedBy2 $ char c
-            spacing
-            return c
-
-    readAnycremented = readNormalOrPostfixIncremented <|> readPrefixIncremented
-    readPrefixIncremented = do
-        id <- getNextId
-        op <- try $ string "++" <|> string "--"
-        spacing
-        x <- readArithTerm
-        return $ TA_Unary id (op ++ "|") x
-
-    readNormalOrPostfixIncremented = do
-        x <- readArithTerm
-        spacing
-        do
-            id <- getNextId
-            op <- try $ string "++" <|> string "--"
-            spacing
-            return $ TA_Unary id ('|':op) x
-         <|>
-            return x
-
-
-
-prop_readCondition = isOk readCondition "[ \\( a = b \\) -a \\( c = d \\) ]"
-prop_readCondition2 = isOk readCondition "[[ (a = b) || (c = d) ]]"
-prop_readCondition3 = isOk readCondition "[[ $c = [[:alpha:].~-] ]]"
-prop_readCondition4 = isOk readCondition "[[ $c =~ *foo* ]]"
-prop_readCondition5 = isOk readCondition "[[ $c =~ f( ]] )* ]]"
-prop_readCondition5a= isOk readCondition "[[ $c =~ a(b) ]]"
-prop_readCondition5b= isOk readCondition "[[ $c =~ f( ($var ]]) )* ]]"
-prop_readCondition6 = isOk readCondition "[[ $c =~ ^[yY]$ ]]"
-prop_readCondition7 = isOk readCondition "[[ ${line} =~ ^[[:space:]]*# ]]"
-prop_readCondition8 = isOk readCondition "[[ $l =~ ogg|flac ]]"
-prop_readCondition9 = isOk readCondition "[ foo -a -f bar ]"
-prop_readCondition10= isOk readCondition "[[\na == b\n||\nc == d ]]"
-prop_readCondition10a= isOk readCondition "[[\na == b  ||\nc == d ]]"
-prop_readCondition10b= isOk readCondition "[[ a == b\n||\nc == d ]]"
-prop_readCondition11= isOk readCondition "[[ a == b ||\n c == d ]]"
-prop_readCondition12= isWarning readCondition "[ a == b \n -o c == d ]"
-prop_readCondition13= isOk readCondition "[[ foo =~ ^fo{1,3}$ ]]"
-prop_readCondition14= isOk readCondition "[ foo '>' bar ]"
-prop_readCondition15= isOk readCondition "[ foo \">=\" bar ]"
-prop_readCondition16= isOk readCondition "[ foo \\< bar ]"
-prop_readCondition17= isOk readCondition "[[ ${file::1} = [-.\\|/\\\\] ]]"
-prop_readCondition18= isOk readCondition "[ ]"
-prop_readCondition19= isOk readCondition "[ '(' x \")\" ]"
-readCondition = called "test expression" $ do
-    opos <- getPosition
-    id <- getNextId
-    open <- try (string "[[") <|> string "["
-    let single = open == "["
-    let typ = if single then SingleBracket else DoubleBracket
-
-    pos <- getPosition
-    space <- allspacing
-    when (null space) $
-        parseProblemAtWithEnd opos pos ErrorC 1035 $ "You need a space after the " ++
-            if single
-                then "[ and before the ]."
-                else "[[ and before the ]]."
-    when (single && '\n' `elem` space) $
-        parseProblemAt pos ErrorC 1080 "You need \\ before line feeds to break lines in [ ]."
-
-    condition <- readConditionContents single <|> do
-        guard . not . null $ space
-        lookAhead $ string "]"
-        id <- getNextIdAt pos
-        return $ TC_Empty id typ
-
-    cpos <- getPosition
-    close <- try (string "]]") <|> string "]" <|> fail "Expected test to end here (don't wrap commands in []/[[]])"
-    when (open == "[[" && close /= "]]") $ parseProblemAt cpos ErrorC 1033 "Did you mean ]] ?"
-    when (open == "[" && close /= "]" ) $ parseProblemAt opos ErrorC 1034 "Did you mean [[ ?"
-    spacing
-    many readCmdWord -- Read and throw away remainders to get then/do warnings. Fixme?
-    return $ T_Condition id typ condition
-
-readAnnotationPrefix = do
-    char '#'
-    many linewhitespace
-    string "shellcheck"
-
-prop_readAnnotation1 = isOk readAnnotation "# shellcheck disable=1234,5678\n"
-prop_readAnnotation2 = isOk readAnnotation "# shellcheck disable=SC1234 disable=SC5678\n"
-prop_readAnnotation3 = isOk readAnnotation "# shellcheck disable=SC1234 source=/dev/null disable=SC5678\n"
-prop_readAnnotation4 = isWarning readAnnotation "# shellcheck cats=dogs disable=SC1234\n"
-prop_readAnnotation5 = isOk readAnnotation "# shellcheck disable=SC2002 # All cats are precious\n"
-prop_readAnnotation6 = isOk readAnnotation "# shellcheck disable=SC1234 # shellcheck foo=bar\n"
-readAnnotation = called "shellcheck directive" $ do
-    try readAnnotationPrefix
-    many1 linewhitespace
-    values <- many1 readKey
-    optional readAnyComment
-    void linefeed <|> do
-        parseNote ErrorC 1125 "Invalid key=value pair? Ignoring the rest of this directive starting here."
-        many (noneOf "\n")
-        void linefeed <|> eof
-    many linewhitespace
-    return $ concat values
-  where
-    readKey = do
-        keyPos <- getPosition
-        key <- many1 letter
-        char '=' <|> fail "Expected '=' after directive key"
-        annotations <- case key of
-            "disable" -> readCode `sepBy` char ','
-              where
-                readCode = do
-                    optional $ string "SC"
-                    int <- many1 digit
-                    return $ DisableComment (read int)
-
-            "source" -> do
-                filename <- many1 $ noneOf " \n"
-                return [SourceOverride filename]
-
-            "shell" -> do
-                pos <- getPosition
-                shell <- many1 $ noneOf " \n"
-                when (isNothing $ shellForExecutable shell) $
-                    parseNoteAt pos ErrorC 1103
-                        "This shell type is unknown. Use e.g. sh or bash."
-                return [ShellOverride shell]
-
-            _ -> do
-                parseNoteAt keyPos WarningC 1107 "This directive is unknown. It will be ignored."
-                anyChar `reluctantlyTill` whitespace
-                return []
-
-        many linewhitespace
-        return annotations
-
-readAnnotations = do
-    annotations <- many (readAnnotation `thenSkip` allspacing)
-    return $ concat annotations
-
-readComment = do
-    unexpecting "shellcheck annotation" readAnnotationPrefix
-    readAnyComment
-
-readAnyComment = do
-    char '#'
-    many $ noneOf "\r\n"
-
-prop_readNormalWord = isOk readNormalWord "'foo'\"bar\"{1..3}baz$(lol)"
-prop_readNormalWord2 = isOk readNormalWord "foo**(foo)!!!(@@(bar))"
-prop_readNormalWord3 = isOk readNormalWord "foo#"
-prop_readNormalWord4 = isOk readNormalWord "$\"foo\"$'foo\nbar'"
-prop_readNormalWord5 = isWarning readNormalWord "${foo}}"
-prop_readNormalWord6 = isOk readNormalWord "foo/{}"
-prop_readNormalWord7 = isOk readNormalWord "foo\\\nbar"
-prop_readNormalWord8 = isWarning readSubshell "(foo\\ \nbar)"
-prop_readNormalWord9 = isOk readSubshell "(foo\\ ;\nbar)"
-prop_readNormalWord10 = isWarning readNormalWord "\x201Chello\x201D"
-prop_readNormalWord11 = isWarning readNormalWord "\x2018hello\x2019"
-prop_readNormalWord12 = isWarning readNormalWord "hello\x2018"
-readNormalWord = readNormalishWord ""
-
-readNormalishWord end = do
-    id <- getNextId
-    pos <- getPosition
-    x <- many1 (readNormalWordPart end)
-    checkPossibleTermination pos x
-    return $ T_NormalWord id x
-
-readIndexSpan = do
-    id <- getNextId
-    x <- many (readNormalWordPart "]" <|> someSpace <|> otherLiteral)
-    return $ T_NormalWord id x
-  where
-    someSpace = do
-        id <- getNextId
-        str <- spacing1
-        return $ T_Literal id str
-    otherLiteral = do
-        id <- getNextId
-        str <- many1 $ oneOf quotableChars
-        return $ T_Literal id str
-
-checkPossibleTermination pos [T_Literal _ x] =
-    when (x `elem` ["do", "done", "then", "fi", "esac"]) $
-        parseProblemAt pos WarningC 1010 $ "Use semicolon or linefeed before '" ++ x ++ "' (or quote to make it literal)."
-checkPossibleTermination _ _ = return ()
-
-readNormalWordPart end = do
-    notFollowedBy2 $ oneOf end
-    checkForParenthesis
-    choice [
-        readSingleQuoted,
-        readDoubleQuoted,
-        readGlob,
-        readNormalDollar,
-        readBraced,
-        readUnquotedBackTicked,
-        readProcSub,
-        readUnicodeQuote,
-        readNormalLiteral end,
-        readLiteralCurlyBraces
-      ]
-  where
-    checkForParenthesis =
-        return () `attempting` do
-            pos <- getPosition
-            lookAhead $ char '('
-            parseProblemAt pos ErrorC 1036 "'(' is invalid here. Did you forget to escape it?"
-
-    readLiteralCurlyBraces = do
-        id <- getNextId
-        str <- findParam <|> literalBraces
-        return $ T_Literal id str
-
-    findParam = try $ string "{}"
-    literalBraces = do
-        pos <- getPosition
-        c <- oneOf "{}"
-        parseProblemAt pos WarningC 1083 $
-            "This " ++ [c] ++ " is literal. Check expression (missing ;/\\n?) or quote it."
-        return [c]
-
-
-readSpacePart = do
-    id <- getNextId
-    x <- many1 whitespace
-    return $ T_Literal id x
-
-readDollarBracedWord = do
-    id <- getNextId
-    list <- many readDollarBracedPart
-    return $ T_NormalWord id list
-
-readDollarBracedPart = readSingleQuoted <|> readDoubleQuoted <|>
-                       readParamSubSpecialChar <|> readExtglob <|> readNormalDollar <|>
-                       readUnquotedBackTicked <|> readDollarBracedLiteral
-
-readDollarBracedLiteral = do
-    id <- getNextId
-    vars <- (readBraceEscaped <|> (anyChar >>= \x -> return [x])) `reluctantlyTill1` bracedQuotable
-    return $ T_Literal id $ concat vars
-
-readParamSubSpecialChar = do
-    id <- getNextId
-    T_ParamSubSpecialChar id <$> many1 paramSubSpecialChars
-
-prop_readProcSub1 = isOk readProcSub "<(echo test | wc -l)"
-prop_readProcSub2 = isOk readProcSub "<(  if true; then true; fi )"
-prop_readProcSub3 = isOk readProcSub "<( # nothing here \n)"
-readProcSub = called "process substitution" $ do
-    id <- getNextId
-    dir <- try $ do
-                    x <- oneOf "<>"
-                    char '('
-                    return [x]
-    list <- readCompoundListOrEmpty
-    allspacing
-    char ')'
-    return $ T_ProcSub id dir list
-
-prop_readSingleQuoted = isOk readSingleQuoted "'foo bar'"
-prop_readSingleQuoted2 = isWarning readSingleQuoted "'foo bar\\'"
-prop_readSingleQuoted4 = isWarning readNormalWord "'it's"
-prop_readSingleQuoted5 = isWarning readSimpleCommand "foo='bar\ncow 'arg"
-prop_readSingleQuoted6 = isOk readSimpleCommand "foo='bar cow 'arg"
-prop_readSingleQuoted7 = isOk readSingleQuoted "'foo\x201C\&bar'"
-prop_readSingleQuoted8 = isWarning readSingleQuoted "'foo\x2018\&bar'"
-readSingleQuoted = called "single quoted string" $ do
-    id <- getNextId
-    startPos <- getPosition
-    singleQuote
-    s <- many readSingleQuotedPart
-    let string = concat s
-    endPos <- getPosition
-    singleQuote <|> fail "Expected end of single quoted string"
-
-    optional $ do
-        c <- try . lookAhead $ suspectCharAfterQuotes <|> oneOf "'"
-        if not (null string) && isAlpha c && isAlpha (last string)
-          then
-            parseProblemAt endPos WarningC 1011
-                "This apostrophe terminated the single quoted string!"
-          else
-            when ('\n' `elem` string && not ("\n" `isPrefixOf` string)) $
-                suggestForgotClosingQuote startPos endPos "single quoted string"
-
-    return (T_SingleQuoted id string)
-
-readSingleQuotedLiteral = do
-    singleQuote
-    strs <- many1 readSingleQuotedPart
-    singleQuote
-    return $ concat strs
-
-readSingleQuotedPart =
-    readSingleEscaped
-    <|> many1 (noneOf $ "'\\" ++ unicodeSingleQuotes)
-    <|> readUnicodeQuote
-   where
-    readUnicodeQuote = do
-        pos <- getPosition
-        x <- oneOf unicodeSingleQuotes
-        parseProblemAt pos WarningC 1112
-            "This is a unicode quote. Delete and retype it (or ignore/doublequote for literal)."
-        return [x]
-
-
-prop_readBackTicked = isOk (readBackTicked False) "`ls *.mp3`"
-prop_readBackTicked2 = isOk (readBackTicked False) "`grep \"\\\"\"`"
-prop_readBackTicked3 = isWarning (readBackTicked False) "´grep \"\\\"\"´"
-prop_readBackTicked4 = isOk readSimpleCommand "`echo foo\necho bar`"
-prop_readBackTicked5 = isOk readSimpleCommand "echo `foo`bar"
-prop_readBackTicked6 = isWarning readSimpleCommand "echo `foo\necho `bar"
-prop_readBackTicked7 = isOk readSimpleCommand "`#inline comment`"
-prop_readBackTicked8 = isOk readSimpleCommand "echo `#comment` \\\nbar baz"
-readQuotedBackTicked = readBackTicked True
-readUnquotedBackTicked = readBackTicked False
-readBackTicked quoted = called "backtick expansion" $ do
-    id <- getNextId
-    startPos <- getPosition
-    backtick
-    subStart <- getPosition
-    subString <- readGenericLiteral "`´"
-    endPos <- getPosition
-    backtick
-
-    optional $ do
-        c <- try . lookAhead $ suspectCharAfterQuotes
-        when ('\n' `elem` subString && not ("\n" `isPrefixOf` subString)) $
-            suggestForgotClosingQuote startPos endPos "backtick expansion"
-
-    -- Result positions may be off due to escapes
-    result <- subParse subStart subParser (unEscape subString)
-    return $ T_Backticked id result
-  where
-    unEscape [] = []
-    unEscape ('\\':'"':rest) | quoted = '"' : unEscape rest
-    unEscape ('\\':x:rest) | x `elem` "$`\\" = x : unEscape rest
-    unEscape ('\\':'\n':rest) = unEscape rest
-    unEscape (c:rest) = c : unEscape rest
-    subParser = do
-        cmds <- readCompoundListOrEmpty
-        verifyEof
-        return cmds
-    backtick =
-      void (char '`') <|> do
-         pos <- getPosition
-         char '´'
-         parseProblemAt pos ErrorC 1077
-            "For command expansion, the tick should slant left (` vs ´). Use $(..) instead."
-
-subParse pos parser input = do
-    lastPosition <- getPosition
-    lastInput <- getInput
-    setPosition pos
-    setInput input
-    result <- parser
-    setInput lastInput
-    setPosition lastPosition
-    return result
-
--- Parse something, but forget all parseProblems
-inSeparateContext = parseForgettingContext True
--- Parse something, but forget all parseProblems on failure
-forgetOnFailure = parseForgettingContext False
-
-parseForgettingContext alsoOnSuccess parser = do
-    context <- Ms.get
-    success context <|> failure context
-  where
-    success c = do
-        res <- try parser
-        when alsoOnSuccess $ Ms.put c
-        return res
-    failure c = do
-        Ms.put c
-        fail ""
-
-prop_readDoubleQuoted = isOk readDoubleQuoted "\"Hello $FOO\""
-prop_readDoubleQuoted2 = isOk readDoubleQuoted "\"$'\""
-prop_readDoubleQuoted3 = isOk readDoubleQuoted "\"\x2018hello\x2019\""
-prop_readDoubleQuoted4 = isWarning readSimpleCommand "\"foo\nbar\"foo"
-prop_readDoubleQuoted5 = isOk readSimpleCommand "lol \"foo\nbar\" etc"
-prop_readDoubleQuoted6 = isOk readSimpleCommand "echo \"${ ls; }\""
-prop_readDoubleQuoted7 = isOk readSimpleCommand "echo \"${ ls;}bar\""
-prop_readDoubleQuoted8 = isWarning readDoubleQuoted "\"\x201Chello\x201D\""
-prop_readDoubleQuoted9 = isWarning readDoubleQuoted "\"foo\\n\""
-prop_readDoubleQuoted10 = isOk readDoubleQuoted "\"foo\\\\n\""
-readDoubleQuoted = called "double quoted string" $ do
-    id <- getNextId
-    startPos <- getPosition
-    doubleQuote
-    x <- many doubleQuotedPart
-    endPos <- getPosition
-    doubleQuote <|> fail "Expected end of double quoted string"
-    optional $ do
-        try . lookAhead $ suspectCharAfterQuotes <|> oneOf "$\""
-        when (any hasLineFeed x && not (startsWithLineFeed x)) $
-            suggestForgotClosingQuote startPos endPos "double quoted string"
-    return $ T_DoubleQuoted id x
-  where
-    startsWithLineFeed (T_Literal _ ('\n':_):_) = True
-    startsWithLineFeed _ = False
-    hasLineFeed (T_Literal _ str) | '\n' `elem` str = True
-    hasLineFeed _ = False
-
-suggestForgotClosingQuote startPos endPos name = do
-    parseProblemAt startPos WarningC 1078 $
-        "Did you forget to close this " ++ name ++ "?"
-    parseProblemAt endPos InfoC 1079
-        "This is actually an end quote, but due to next char it looks suspect."
-
-doubleQuotedPart = readDoubleLiteral <|> readDoubleQuotedDollar <|> readQuotedBackTicked <|> readUnicodeQuote
-  where
-    readUnicodeQuote = do
-        pos <- getPosition
-        id <- getNextId
-        c <- oneOf unicodeDoubleQuotes
-        parseProblemAt pos WarningC 1111
-            "This is a unicode quote. Delete and retype it (or ignore/singlequote for literal)."
-        return $ T_Literal id [c]
-
-readDoubleQuotedLiteral = do
-    doubleQuote
-    x <- readDoubleLiteral
-    doubleQuote
-    return x
-
-readDoubleLiteral = do
-    id <- getNextId
-    s <- many1 readDoubleLiteralPart
-    return $ T_Literal id (concat s)
-
-readDoubleLiteralPart = do
-    x <- many1 (readDoubleEscaped <|> many1 (noneOf (doubleQuotableChars ++ unicodeDoubleQuotes)))
-    return $ concat x
-
-readNormalLiteral end = do
-    id <- getNextId
-    s <- many1 (readNormalLiteralPart end)
-    return $ T_Literal id (concat s)
-
-prop_readGlob1 = isOk readGlob "*"
-prop_readGlob2 = isOk readGlob "[^0-9]"
-prop_readGlob3 = isOk readGlob "[a[:alpha:]]"
-prop_readGlob4 = isOk readGlob "[[:alnum:]]"
-prop_readGlob5 = isOk readGlob "[^[:alpha:]1-9]"
-prop_readGlob6 = isOk readGlob "[\\|]"
-prop_readGlob7 = isOk readGlob "[^[]"
-prop_readGlob8 = isOk readGlob "[*?]"
-readGlob = readExtglob <|> readSimple <|> readClass <|> readGlobbyLiteral
-    where
-        readSimple = do
-            id <- getNextId
-            c <- oneOf "*?"
-            return $ T_Glob id [c]
-        -- Doesn't handle weird things like [^]a] and [$foo]. fixme?
-        readClass = try $ do
-            id <- getNextId
-            char '['
-            s <- many1 (predefined <|> readNormalLiteralPart "]" <|> globchars)
-            char ']'
-            return $ T_Glob id $ "[" ++ concat s ++ "]"
-          where
-           globchars = liftM return . oneOf $ "!$[" ++ extglobStartChars
-           predefined = do
-              try $ string "[:"
-              s <- many1 letter
-              string ":]"
-              return $ "[:" ++ s ++ ":]"
-
-        readGlobbyLiteral = do
-            id <- getNextId
-            c <- extglobStart <|> char '['
-            return $ T_Literal id [c]
-
-readNormalLiteralPart customEnd =
-    readNormalEscaped <|>
-        many1 (noneOf (customEnd ++ standardEnd))
-  where
-    standardEnd = "[{}"
-        ++ quotableChars
-        ++ extglobStartChars
-        ++ unicodeDoubleQuotes
-        ++ unicodeSingleQuotes
-
-readNormalEscaped = called "escaped char" $ do
-    pos <- getPosition
-    backslash
-    do
-        next <- quotable <|> oneOf "?*@!+[]{}.,~#"
-        when (next == ' ') $ checkTrailingSpaces pos <|> return ()
-        return $ if next == '\n' then "" else [next]
-      <|>
-        do
-            next <- anyChar
-            case escapedChar next of
-                Just name -> parseNoteAt pos WarningC 1012 $ "\\" ++ [next] ++ " is just literal '" ++ [next] ++ "' here. For " ++ name ++ ", use " ++ alternative next ++ " instead."
-                Nothing -> parseNoteAt pos InfoC 1001 $ "This \\" ++ [next] ++ " will be a regular '" ++ [next] ++ "' in this context."
-            return [next]
-  where
-    alternative 'n' = "a quoted, literal line feed"
-    alternative t = "\"$(printf \"\\" ++ [t] ++ "\")\""
-    escapedChar 'n' = Just "line feed"
-    escapedChar 't' = Just "tab"
-    escapedChar 'r' = Just "carriage return"
-    escapedChar _ = Nothing
-
-    checkTrailingSpaces pos = lookAhead . try $ do
-        many linewhitespace
-        void linefeed <|> eof
-        parseProblemAt pos ErrorC 1101 "Delete trailing spaces after \\ to break line (or use quotes for literal space)."
-
-
-prop_readExtglob1 = isOk readExtglob "!(*.mp3)"
-prop_readExtglob2 = isOk readExtglob "!(*.mp3|*.wmv)"
-prop_readExtglob4 = isOk readExtglob "+(foo \\) bar)"
-prop_readExtglob5 = isOk readExtglob "+(!(foo *(bar)))"
-prop_readExtglob6 = isOk readExtglob "*(((||))|())"
-prop_readExtglob7 = isOk readExtglob "*(<>)"
-prop_readExtglob8 = isOk readExtglob "@(|*())"
-readExtglob = called "extglob" $ do
-    id <- getNextId
-    c <- try $ do
-            f <- extglobStart
-            char '('
-            return f
-    contents <- readExtglobPart `sepBy` char '|'
-    char ')'
-    return $ T_Extglob id [c] contents
-
-readExtglobPart = do
-    id <- getNextId
-    x <- many (readExtglobGroup <|> readNormalWordPart "" <|> readSpacePart <|> readExtglobLiteral)
-    return $ T_NormalWord id x
-  where
-    readExtglobGroup = do
-        id <- getNextId
-        char '('
-        contents <- readExtglobPart `sepBy` char '|'
-        char ')'
-        return $ T_Extglob id "" contents
-    readExtglobLiteral = do
-        id <- getNextId
-        str <- many1 (oneOf "<>#;&")
-        return $ T_Literal id str
-
-
-readSingleEscaped = do
-    pos <- getPosition
-    s <- backslash
-    x <- lookAhead anyChar
-
-    case x of
-        '\'' -> parseProblemAt pos InfoC 1003 "Want to escape a single quote? echo 'This is how it'\\''s done'.";
-        '\n' -> parseProblemAt pos InfoC 1004 "This backslash+linefeed is literal. Break outside single quotes if you just want to break the line."
-        _ -> return ()
-
-    return [s]
-
-readDoubleEscaped = do
-    pos <- getPosition
-    bs <- backslash
-    (linefeed >> return "")
-        <|> liftM return doubleQuotable
-        <|> do
-            c <- anyChar
-            parseNoteAt pos StyleC 1117 $
-                "Backslash is literal in \"\\" ++ [c] ++ "\". Prefer explicit escaping: \"\\\\" ++ [c] ++ "\"."
-            return [bs, c]
-
-readBraceEscaped = do
-    bs <- backslash
-    (linefeed >> return "")
-        <|> liftM return bracedQuotable
-        <|> liftM (\ x -> [bs, x]) anyChar
-
-
-readGenericLiteral endChars = do
-    strings <- many (readGenericEscaped <|> many1 (noneOf ('\\':endChars)))
-    return $ concat strings
-
-readGenericLiteral1 endExp = do
-    strings <- (readGenericEscaped <|> (anyChar >>= \x -> return [x])) `reluctantlyTill1` endExp
-    return $ concat strings
-
-readGenericEscaped = do
-    backslash
-    x <- anyChar
-    return $ if x == '\n' then [] else ['\\', x]
-
-prop_readBraced = isOk readBraced "{1..4}"
-prop_readBraced2 = isOk readBraced "{foo,bar,\"baz lol\"}"
-prop_readBraced3 = isOk readBraced "{1,\\},2}"
-prop_readBraced4 = isOk readBraced "{1,{2,3}}"
-prop_readBraced5 = isOk readBraced "{JP{,E}G,jp{,e}g}"
-prop_readBraced6 = isOk readBraced "{foo,bar,$((${var}))}"
-prop_readBraced7 = isNotOk readBraced "{}"
-prop_readBraced8 = isNotOk readBraced "{foo}"
-readBraced = try braceExpansion
-  where
-    braceExpansion =
-        T_BraceExpansion `withParser` do
-            char '{'
-            elements <- bracedElement `sepBy1` char ','
-            guard $
-                case elements of
-                    (_:_:_) -> True
-                    [t] -> ".." `isInfixOf` onlyLiteralString t
-                    [] -> False
-            char '}'
-            return elements
-    bracedElement =
-        T_NormalWord `withParser` do
-            many $ choice [
-                braceExpansion,
-                readDollarExpression,
-                readSingleQuoted,
-                readDoubleQuoted,
-                braceLiteral
-                ]
-    braceLiteral =
-        T_Literal `withParser` readGenericLiteral1 (oneOf "{}\"$'," <|> whitespace)
-
-ensureDollar =
-    -- The grammar should have been designed along the lines of readDollarExpr = char '$' >> stuff, but
-    -- instead, each subunit parses its own $. This results in ~7 1-3 char lookaheads instead of one 1-char.
-    -- Instead of optimizing the grammar, here's a green cut that decreases shellcheck runtime by 10%:
-    lookAhead $ char '$'
-
-readNormalDollar = do
-    ensureDollar
-    readDollarExp <|> readDollarDoubleQuote <|> readDollarSingleQuote <|> readDollarLonely
-readDoubleQuotedDollar = do
-    ensureDollar
-    readDollarExp <|> readDollarLonely
-
-
-prop_readDollarExpression1 = isOk readDollarExpression "$(((1) && 3))"
-prop_readDollarExpression2 = isWarning readDollarExpression "$(((1)) && 3)"
-prop_readDollarExpression3 = isWarning readDollarExpression "$((\"$@\" &); foo;)"
-readDollarExpression :: Monad m => SCParser m Token
-readDollarExpression = do
-    ensureDollar
-    readDollarExp
-
-readDollarExp = arithmetic <|> readDollarExpansion <|> readDollarBracket <|> readDollarBraceCommandExpansion <|> readDollarBraced <|> readDollarVariable
-  where
-    arithmetic = readAmbiguous "$((" readDollarArithmetic readDollarExpansion (\pos ->
-        parseNoteAt pos WarningC 1102 "Shells disambiguate $(( differently or not at all. For $(command substition), add space after $( . For $((arithmetics)), fix parsing errors.")
-
-prop_readDollarSingleQuote = isOk readDollarSingleQuote "$'foo\\\'lol'"
-readDollarSingleQuote = called "$'..' expression" $ do
-    id <- getNextId
-    try $ string "$'"
-    str <- readGenericLiteral "'"
-    char '\''
-    return $ T_DollarSingleQuoted id str
-
-prop_readDollarDoubleQuote = isOk readDollarDoubleQuote "$\"hello\""
-readDollarDoubleQuote = do
-    lookAhead . try $ string "$\""
-    id <- getNextId
-    char '$'
-    doubleQuote
-    x <- many doubleQuotedPart
-    doubleQuote <|> fail "Expected end of translated double quoted string"
-    return $ T_DollarDoubleQuoted id x
-
-prop_readDollarArithmetic = isOk readDollarArithmetic "$(( 3 * 4 +5))"
-prop_readDollarArithmetic2 = isOk readDollarArithmetic "$(((3*4)+(1*2+(3-1))))"
-readDollarArithmetic = called "$((..)) expression" $ do
-    id <- getNextId
-    try (string "$((")
-    c <- readArithmeticContents
-    pos <- getPosition
-    char ')'
-    char ')' <|> fail "Expected a double )) to end the $((..))"
-    return (T_DollarArithmetic id c)
-
-readDollarBracket = called "$[..] expression" $ do
-    id <- getNextId
-    try (string "$[")
-    c <- readArithmeticContents
-    string "]"
-    return (T_DollarBracket id c)
-
-prop_readArithmeticExpression = isOk readArithmeticExpression "((a?b:c))"
-readArithmeticExpression = called "((..)) command" $ do
-    id <- getNextId
-    try (string "((")
-    c <- readArithmeticContents
-    string "))"
-    return (T_Arithmetic id c)
-
--- If the next characters match prefix, try two different parsers and warn if the alternate parser had to be used
-readAmbiguous :: Monad m => String -> SCParser m p -> SCParser m p -> (SourcePos -> SCParser m ()) -> SCParser m p
-readAmbiguous prefix expected alternative warner = do
-    pos <- getPosition
-    try . lookAhead $ string prefix
-    -- If the expected parser fails, try the alt.
-    -- If the alt fails, run the expected one again for the errors.
-    try expected <|> try (withAlt pos) <|> expected
-  where
-    withAlt pos = do
-        t <- forgetOnFailure alternative
-        warner pos
-        return t
-
-prop_readDollarBraceCommandExpansion1 = isOk readDollarBraceCommandExpansion "${ ls; }"
-prop_readDollarBraceCommandExpansion2 = isOk readDollarBraceCommandExpansion "${\nls\n}"
-readDollarBraceCommandExpansion = called "ksh ${ ..; } command expansion" $ do
-    id <- getNextId
-    try $ do
-        string "${"
-        whitespace
-    allspacing
-    term <- readTerm
-    char '}' <|> fail "Expected } to end the ksh ${ ..; } command expansion"
-    return $ T_DollarBraceCommandExpansion id term
-
-prop_readDollarBraced1 = isOk readDollarBraced "${foo//bar/baz}"
-prop_readDollarBraced2 = isOk readDollarBraced "${foo/'{cow}'}"
-prop_readDollarBraced3 = isOk readDollarBraced "${foo%%$(echo cow\\})}"
-prop_readDollarBraced4 = isOk readDollarBraced "${foo#\\}}"
-readDollarBraced = called "parameter expansion" $ do
-    id <- getNextId
-    try (string "${")
-    word <- readDollarBracedWord
-    char '}'
-    return $ T_DollarBraced id word
-
-prop_readDollarExpansion1= isOk readDollarExpansion "$(echo foo; ls\n)"
-prop_readDollarExpansion2= isOk readDollarExpansion "$(  )"
-prop_readDollarExpansion3= isOk readDollarExpansion "$( command \n#comment \n)"
-readDollarExpansion = called "command expansion" $ do
-    id <- getNextId
-    try (string "$(")
-    cmds <- readCompoundListOrEmpty
-    char ')' <|> fail "Expected end of $(..) expression"
-    return $ T_DollarExpansion id cmds
-
-prop_readDollarVariable = isOk readDollarVariable "$@"
-prop_readDollarVariable2 = isOk (readDollarVariable >> anyChar) "$?!"
-prop_readDollarVariable3 = isWarning (readDollarVariable >> anyChar) "$10"
-prop_readDollarVariable4 = isWarning (readDollarVariable >> string "[@]") "$arr[@]"
-
-readDollarVariable = do
-    id <- getNextId
-    pos <- getPosition
-
-    let singleCharred p = do
-        n <- p
-        value <- wrap [n]
-        return (T_DollarBraced id value)
-
-    let positional = do
-        value <- singleCharred digit
-        return value `attempting` do
-            lookAhead digit
-            parseNoteAt pos ErrorC 1037 "Braces are required for positionals over 9, e.g. ${10}."
-
-    let special = singleCharred specialVariable
-
-    let regular = do
-        name <- readVariableName
-        value <- wrap name
-        return (T_DollarBraced id value) `attempting` do
-            lookAhead $ void (string "[@]") <|> void (string "[*]") <|> void readArrayIndex
-            parseNoteAt pos ErrorC 1087 "Braces are required when expanding arrays, as in ${array[idx]}."
-
-    try $ char '$' >> (positional <|> special <|> regular)
-
-  where
-    wrap s = do
-        x <- getNextId
-        y <- getNextId
-        return $ T_NormalWord x [T_Literal y s]
-
-readVariableName = do
-    f <- variableStart
-    rest <- many variableChars
-    return (f:rest)
-
-readDollarLonely = do
-    id <- getNextId
-    pos <- getPosition
-    char '$'
-    n <- lookAhead (anyChar <|> (eof >> return '_'))
-    return $ T_Literal id "$"
-
-prop_readHereDoc = isOk readScript "cat << foo\nlol\ncow\nfoo"
-prop_readHereDoc2 = isWarning readScript "cat <<- EOF\n  cow\n  EOF"
-prop_readHereDoc3 = isOk readScript "cat << foo\n$\"\nfoo"
-prop_readHereDoc4 = isOk readScript "cat << foo\n`\nfoo"
-prop_readHereDoc5 = isOk readScript "cat <<- !foo\nbar\n!foo"
-prop_readHereDoc6 = isOk readScript "cat << foo\\ bar\ncow\nfoo bar"
-prop_readHereDoc7 = isOk readScript "cat << foo\n\\$(f ())\nfoo"
-prop_readHereDoc8 = isOk readScript "cat <<foo>>bar\netc\nfoo"
-prop_readHereDoc9 = isOk readScript "if true; then cat << foo; fi\nbar\nfoo\n"
-prop_readHereDoc10= isOk readScript "if true; then cat << foo << bar; fi\nfoo\nbar\n"
-prop_readHereDoc11= isOk readScript "cat << foo $(\nfoo\n)lol\nfoo\n"
-prop_readHereDoc12= isOk readScript "cat << foo|cat\nbar\nfoo"
-prop_readHereDoc13= isOk readScript "cat <<'#!'\nHello World\n#!\necho Done"
-prop_readHereDoc14= isWarning readScript "cat << foo\nbar\nfoo \n"
-prop_readHereDoc15= isWarning readScript "cat <<foo\nbar\nfoo bar\n"
-prop_readHereDoc16= isOk readScript "cat <<- ' foo'\nbar\n foo\n"
-prop_readHereDoc17= isWarning readScript "cat <<- ' foo'\nbar\n  foo\n"
-readHereDoc = called "here document" $ do
-    fid <- getNextId
-    pos <- getPosition
-    try $ string "<<"
-    dashed <- (char '-' >> return Dashed) <|> return Undashed
-    sp <- spacing
-    optional $ do
-        try . lookAhead $ char '('
-        let message = "Shells are space sensitive. Use '< <(cmd)', not '<<" ++ sp ++ "(cmd)'."
-        parseProblemAt pos ErrorC 1038 message
-    hid <- getNextId
-    (quoted, endToken) <- readToken
-
-    -- add empty tokens for now, read the rest in readPendingHereDocs
-    let doc = T_HereDoc hid dashed quoted endToken []
-    addPendingHereDoc doc
-    return doc
-  where
-    quotes = "\"'\\"
-    -- Fun fact: bash considers << foo"" quoted, but not << <("foo").
-    -- Instead of replicating this, just read a token and strip quotes.
-    readToken = do
-        str <- readStringForParser readNormalWord
-        return (if any (`elem` quotes) str then Quoted else Unquoted,
-                filter (not . (`elem` quotes)) str)
-
-
-readPendingHereDocs = do
-    docs <- popPendingHereDocs
-    mapM_ readDoc docs
-  where
-    readDoc (T_HereDoc id dashed quoted endToken _) = do
-        pos <- getPosition
-        hereData <- concat <$> rawLine `reluctantlyTill` do
-                        linewhitespace `reluctantlyTill` string endToken
-                        string endToken
-                        void linewhitespace <|> void (oneOf "\n;&#)") <|> eof
-        do
-            spaces <- linewhitespace `reluctantlyTill` string endToken
-            verifyHereDoc dashed quoted spaces hereData
-            string endToken
-            trailingPos <- getPosition
-            trailers <- lookAhead $ many (noneOf "\n")
-            let ppt = parseProblemAt trailingPos ErrorC
-            unless (null trailers) $
-                if all isSpace trailers
-                then ppt 1118 "Delete whitespace after the here-doc end token."
-                else case (head $ dropWhile isSpace trailers) of
-                    ')' -> ppt 1119 $ "Add a linefeed between end token and terminating ')'."
-                    '#' -> ppt 1120 "No comments allowed after here-doc token. Comment the next line instead."
-                    c | c `elem` ";&" ->
-                        ppt 1121 "Add ;/& terminators (and other syntax) on the line with the <<, not here."
-                    _ -> ppt 1122 "Nothing allowed after end token. To continue a command, put it on the line with the <<."
-            parsedData <- parseHereData quoted pos hereData
-            list <- parseHereData quoted pos hereData
-            addToHereDocMap id list
-
-         `attempting` (eof >> debugHereDoc pos endToken hereData)
-
-    rawLine = do
-        c <- many $ noneOf "\n"
-        void (char '\n') <|> eof
-        return $ c ++ "\n"
-
-    parseHereData Quoted startPos hereData = do
-        id <- getNextIdAt startPos
-        return [T_Literal id hereData]
-
-    parseHereData Unquoted startPos hereData =
-        subParse startPos readHereData hereData
-
-    readHereData = many $ try doubleQuotedPart <|> readHereLiteral
-
-    readHereLiteral = do
-        id <- getNextId
-        chars <- many1 $ noneOf "`$\\"
-        return $ T_Literal id chars
-
-    verifyHereDoc dashed quoted spacing hereInfo = do
-        when (dashed == Undashed && spacing /= "") $
-            parseNote ErrorC 1039 "Use <<- instead of << if you want to indent the end token."
-        when (dashed == Dashed && filter (/= '\t') spacing /= "" ) $
-            parseNote ErrorC 1040 "When using <<-, you can only indent with tabs."
-        return ()
-
-    debugHereDoc pos endToken doc
-        | endToken `isInfixOf` doc =
-            let lookAt line = when (endToken `isInfixOf` line) $
-                      parseProblemAt pos ErrorC 1042 ("Close matches include '" ++ line ++ "' (!= '" ++ endToken ++ "').")
-            in do
-                  parseProblemAt pos ErrorC 1041 ("Found '" ++ endToken ++ "' further down, but not on a separate line.")
-                  mapM_ lookAt (lines doc)
-        | map toLower endToken `isInfixOf` map toLower doc =
-            parseProblemAt pos ErrorC 1043 ("Found " ++ endToken ++ " further down, but with wrong casing.")
-        | otherwise =
-            parseProblemAt pos ErrorC 1044 ("Couldn't find end token `" ++ endToken ++ "' in the here document.")
-
-
-readFilename = readNormalWord
-readIoFileOp = choice [g_DGREAT, g_LESSGREAT, g_GREATAND, g_LESSAND, g_CLOBBER, redirToken '<' T_Less, redirToken '>' T_Greater ]
-
-readIoDuplicate = try $ do
-    id <- getNextId
-    op <- g_GREATAND <|> g_LESSAND
-    target <- readIoVariable <|> many1 digit <|> string "-"
-    return $ T_IoDuplicate id op target
-
-prop_readIoFile = isOk readIoFile ">> \"$(date +%YYmmDD)\""
-readIoFile = called "redirection" $ do
-    id <- getNextId
-    op <- readIoFileOp
-    spacing
-    file <- readFilename
-    return $ T_IoFile id op file
-
-readIoVariable = try $ do
-    char '{'
-    x <- readVariableName
-    char '}'
-    return $ "{" ++ x ++ "}"
-
-readIoSource = try $ do
-    x <- string "&" <|> readIoVariable <|> many digit
-    lookAhead $ void readIoFileOp <|> void (string "<<")
-    return x
-
-prop_readIoRedirect = isOk readIoRedirect "3>&2"
-prop_readIoRedirect2 = isOk readIoRedirect "2> lol"
-prop_readIoRedirect3 = isOk readIoRedirect "4>&-"
-prop_readIoRedirect4 = isOk readIoRedirect "&> lol"
-prop_readIoRedirect5 = isOk readIoRedirect "{foo}>&2"
-prop_readIoRedirect6 = isOk readIoRedirect "{foo}<&-"
-readIoRedirect = do
-    id <- getNextId
-    n <- readIoSource
-    redir <- readHereString <|> readHereDoc <|> readIoDuplicate <|> readIoFile
-    skipAnnotationAndWarn
-    spacing
-    return $ T_FdRedirect id n redir
-
-readRedirectList = many1 readIoRedirect
-
-prop_readHereString = isOk readHereString "<<< \"Hello $world\""
-readHereString = called "here string" $ do
-    id <- getNextId
-    try $ string "<<<"
-    spacing
-    id2 <- getNextId
-    word <- readNormalWord
-    return $ T_HereString id2 word
-
-readNewlineList = many1 ((linefeed <|> carriageReturn) `thenSkip` spacing)
-readLineBreak = optional readNewlineList
-
-prop_readSeparator1 = isWarning readScript "a &; b"
-prop_readSeparator2 = isOk readScript "a & b"
-prop_readSeparator3 = isWarning readScript "a &amp; b"
-prop_readSeparator4 = isWarning readScript "a &gt; file; b"
-readSeparatorOp = do
-    notFollowedBy2 (void g_AND_IF <|> void readCaseSeparator)
-    notFollowedBy2 (string "&>")
-    f <- try (do
-                    pos <- getPosition
-                    char '&'
-                    optional $ do
-                        s <- lookAhead . choice . map (try . string) $
-                            ["amp;", "gt;", "lt;"]
-                        parseProblemAt pos ErrorC 1109 "This is an unquoted HTML entity. Replace with corresponding character."
-
-                    spacing
-                    pos <- getPosition
-                    char ';'
-                    -- In case statements we might have foo & ;;
-                    notFollowedBy2 $ char ';'
-                    parseProblemAt pos ErrorC 1045 "It's not 'foo &; bar', just 'foo & bar'."
-                    return '&'
-            ) <|> char ';' <|> char '&'
-    spacing
-    return f
-
-readSequentialSep = void (g_Semi >> readLineBreak) <|> void readNewlineList
-readSeparator =
-    do
-        separator <- readSeparatorOp
-        readLineBreak
-        return separator
-     <|>
-        do
-            readNewlineList
-            return '\n'
-
-makeSimpleCommand id1 id2 prefix cmd suffix =
-    let
-        (preAssigned, preRest) = partition assignment prefix
-        (preRedirected, preRest2) = partition redirection preRest
-        (postRedirected, postRest) = partition redirection suffix
-
-        redirs = preRedirected ++ postRedirected
-        assigns = preAssigned
-        args = cmd ++ preRest2 ++ postRest
-    in
-        T_Redirecting id1 redirs $ T_SimpleCommand id2 assigns args
-  where
-    assignment (T_Assignment {}) = True
-    assignment _ = False
-    redirection (T_FdRedirect {}) = True
-    redirection _ = False
-
-prop_readSimpleCommand = isOk readSimpleCommand "echo test > file"
-prop_readSimpleCommand2 = isOk readSimpleCommand "cmd &> file"
-prop_readSimpleCommand3 = isOk readSimpleCommand "export foo=(bar baz)"
-prop_readSimpleCommand4 = isOk readSimpleCommand "typeset -a foo=(lol)"
-prop_readSimpleCommand5 = isOk readSimpleCommand "time if true; then echo foo; fi"
-prop_readSimpleCommand6 = isOk readSimpleCommand "time -p ( ls -l; )"
-prop_readSimpleCommand7 = isOk readSimpleCommand "\\ls"
-readSimpleCommand = called "simple command" $ do
-    pos <- getPosition
-    id1 <- getNextId
-    id2 <- getNextId
-    prefix <- option [] readCmdPrefix
-    skipAnnotationAndWarn
-    cmd <- option Nothing $ do { f <- readCmdName; return $ Just f; }
-    when (null prefix && isNothing cmd) $ fail "Expected a command"
-    case cmd of
-      Nothing -> return $ makeSimpleCommand id1 id2 prefix [] []
-      Just cmd -> do
-            suffix <- option [] $ getParser readCmdSuffix cmd [
-                        (["declare", "export", "local", "readonly", "typeset"], readModifierSuffix),
-                        (["time"], readTimeSuffix),
-                        (["let"], readLetSuffix),
-                        (["eval"], readEvalSuffix)
-                    ]
-
-            let result = makeSimpleCommand id1 id2 prefix [cmd] suffix
-            if isCommand ["source", "."] cmd
-                then readSource pos result
-                else return result
-  where
-    isCommand strings (T_NormalWord _ [T_Literal _ s]) = s `elem` strings
-    isCommand _ _ = False
-    getParser def cmd [] = def
-    getParser def cmd ((list, action):rest) =
-        if isCommand list cmd
-        then action
-        else getParser def cmd rest
-
-
-readSource :: Monad m => SourcePos -> Token -> SCParser m Token
-readSource pos t@(T_Redirecting _ _ (T_SimpleCommand _ _ (cmd:file:_))) = do
-    override <- getSourceOverride
-    let literalFile = do
-        name <- override `mplus` getLiteralString file
-        -- Hack to avoid 'source ~/foo' trying to read from literal tilde
-        guard . not $ "~/" `isPrefixOf` name
-        return name
-    case literalFile of
-        Nothing -> do
-            parseNoteAt pos WarningC 1090
-                "Can't follow non-constant source. Use a directive to specify location."
-            return t
-        Just filename -> do
-            proceed <- shouldFollow filename
-            if not proceed
-              then do
-                parseNoteAt pos InfoC 1093
-                    "This file appears to be recursively sourced. Ignoring."
-                return t
-              else do
-                sys <- Mr.asks systemInterface
-                input <-
-                    if filename == "/dev/null" -- always allow /dev/null
-                    then return (Right "")
-                    else system $ siReadFile sys filename
-                case input of
-                    Left err -> do
-                        parseNoteAt pos InfoC 1091 $
-                            "Not following: " ++ err
-                        return t
-                    Right script -> do
-                        id <- getNextIdAt pos
-
-                        let included = do
-                            src <- subRead filename script
-                            return $ T_Include id t src
-
-                        let failed = do
-                            parseNoteAt pos WarningC 1094
-                                "Parsing of sourced file failed. Ignoring it."
-                            return t
-
-                        included <|> failed
-  where
-    subRead name script =
-        withContext (ContextSource name) $
-            inSeparateContext $
-                subParse (initialPos name) readScript script
-readSource _ t = return t
-
-
-prop_readPipeline = isOk readPipeline "! cat /etc/issue | grep -i ubuntu"
-prop_readPipeline2 = isWarning readPipeline "!cat /etc/issue | grep -i ubuntu"
-prop_readPipeline3 = isOk readPipeline "for f; do :; done|cat"
-readPipeline = do
-    unexpecting "keyword/token" readKeyword
-    do
-        (T_Bang id) <- g_Bang
-        pipe <- readPipeSequence
-        return $ T_Banged id pipe
-      <|>
-        readPipeSequence
-
-prop_readAndOr = isOk readAndOr "grep -i lol foo || exit 1"
-prop_readAndOr1 = isOk readAndOr "# shellcheck disable=1\nfoo"
-prop_readAndOr2 = isOk readAndOr "# shellcheck disable=1\n# lol\n# shellcheck disable=3\nfoo"
-readAndOr = do
-    aid <- getNextId
-    apos <- getPosition
-    annotations <- readAnnotations
-
-    unless (null annotations) $ optional $ do
-        try . lookAhead $ readKeyword
-        parseProblemAt apos ErrorC 1123 "ShellCheck directives are only valid in front of complete compound commands, like 'if', not e.g. individual 'elif' branches."
-
-    andOr <- withAnnotations annotations $
-        chainr1 readPipeline $ do
-            op <- g_AND_IF <|> g_OR_IF
-            readLineBreak
-            return $ case op of T_AND_IF id -> T_AndIf id
-                                T_OR_IF  id -> T_OrIf id
-
-    return $ if null annotations
-                then andOr
-                else T_Annotation aid annotations andOr
-
-readTermOrNone = do
-    allspacing
-    readTerm <|> do
-        eof
-        return []
-
-prop_readTerm = isOk readTerm "time ( foo; bar; )"
-readTerm = do
-    allspacing
-    m <- readAndOr
-    readTerm' m
-
-readTerm' current =
-    do
-        id <- getNextId
-        sep <- readSeparator
-        more <- option (T_EOF id) readAndOr
-        case more of (T_EOF _) -> return [transformWithSeparator id sep current]
-                     _         -> do
-                                list <- readTerm' more
-                                return (transformWithSeparator id sep current : list)
-      <|>
-        return [current]
-
-transformWithSeparator i '&' = T_Backgrounded i
-transformWithSeparator i _  = id
-
-
-readPipeSequence = do
-    id <- getNextId
-    (cmds, pipes) <- sepBy1WithSeparators readCommand
-                        (readPipe `thenSkip` (spacing >> readLineBreak))
-    spacing
-    return $ T_Pipeline id pipes cmds
-  where
-    sepBy1WithSeparators p s = do
-        let elems = p >>= \x -> return ([x], [])
-        let seps = do
-            separator <- s
-            return $ \(a,b) (c,d) -> (a++c, b ++ d ++ [separator])
-        elems `chainl1` seps
-
-readPipe = do
-    notFollowedBy2 g_OR_IF
-    id <- getNextId
-    char '|'
-    qualifier <- string "&" <|> return ""
-    spacing
-    return $ T_Pipe id ('|':qualifier)
-
-readCommand = choice [
-    readCompoundCommand,
-    readCoProc,
-    readSimpleCommand
-    ]
-
-readCmdName = do
-    -- Ignore alias suppression
-    optional . try $ do
-        char '\\'
-        lookAhead $ variableChars
-    readCmdWord
-
-readCmdWord = do
-    skipAnnotationAndWarn
-    readNormalWord <* spacing
-
--- Due to poor planning, annotations after commands isn't handled well.
--- At the time this function is used, it's usually too late to skip
--- comments, so you end up with a parse failure instead.
-skipAnnotationAndWarn = optional $ do
-        try . lookAhead $ readAnnotationPrefix
-        parseProblem ErrorC 1126 "Place shellcheck directives before commands, not after."
-        readAnyComment
-
-prop_readIfClause = isOk readIfClause "if false; then foo; elif true; then stuff; more stuff; else cows; fi"
-prop_readIfClause2 = isWarning readIfClause "if false; then; echo oo; fi"
-prop_readIfClause3 = isWarning readIfClause "if false; then true; else; echo lol; fi"
-prop_readIfClause4 = isWarning readIfClause "if false; then true; else if true; then echo lol; fi"
-prop_readIfClause5 = isOk readIfClause "if false; then true; else\nif true; then echo lol; fi; fi"
-readIfClause = called "if expression" $ do
-    id <- getNextId
-    pos <- getPosition
-    (condition, action) <- readIfPart
-    elifs <- many readElifPart
-    elses <- option [] readElsePart
-
-    g_Fi `orFail` do
-        parseProblemAt pos ErrorC 1046 "Couldn't find 'fi' for this 'if'."
-        parseProblem ErrorC 1047 "Expected 'fi' matching previously mentioned 'if'."
-        return "Expected 'fi'"
-
-    return $ T_IfExpression id ((condition, action):elifs) elses
-
-
-verifyNotEmptyIf s =
-    optional (do
-                emptyPos <- getPosition
-                try . lookAhead $ (g_Fi <|> g_Elif <|> g_Else)
-                parseProblemAt emptyPos ErrorC 1048 $ "Can't have empty " ++ s ++ " clauses (use 'true' as a no-op).")
-readIfPart = do
-    pos <- getPosition
-    g_If
-    allspacing
-    condition <- readTerm
-
-    ifNextToken (g_Fi <|> g_Elif <|> g_Else) $
-        parseProblemAt pos ErrorC 1049 "Did you forget the 'then' for this 'if'?"
-
-    called "then clause" $ do
-        g_Then `orFail` do
-            parseProblem ErrorC 1050 "Expected 'then'."
-            return "Expected 'then'"
-
-        acceptButWarn g_Semi ErrorC 1051 "Semicolons directly after 'then' are not allowed. Just remove it."
-        allspacing
-        verifyNotEmptyIf "then"
-
-        action <- readTerm
-        return (condition, action)
-
-readElifPart = called "elif clause" $ do
-    pos <- getPosition
-    correctElif <- elif
-    unless correctElif $
-        parseProblemAt pos ErrorC 1075 "Use 'elif' instead of 'else if' (or put 'if' on new line if nesting)."
-    allspacing
-    condition <- readTerm
-
-    ifNextToken (g_Fi <|> g_Elif <|> g_Else) $
-        parseProblemAt pos ErrorC 1049 "Did you forget the 'then' for this 'elif'?"
-
-    g_Then
-    acceptButWarn g_Semi ErrorC 1052 "Semicolons directly after 'then' are not allowed. Just remove it."
-    allspacing
-    verifyNotEmptyIf "then"
-    action <- readTerm
-    return (condition, action)
-  where
-    elif = (g_Elif >> return True) <|>
-        try (g_Else >> g_If >> return False)
-
-readElsePart = called "else clause" $ do
-    pos <- getPosition
-    g_Else
-    acceptButWarn g_Semi ErrorC 1053 "Semicolons directly after 'else' are not allowed. Just remove it."
-    allspacing
-    verifyNotEmptyIf "else"
-    readTerm
-
-ifNextToken parser action =
-    optional $ do
-        try . lookAhead $ parser
-        action
-
-prop_readSubshell = isOk readSubshell "( cd /foo; tar cf stuff.tar * )"
-readSubshell = called "explicit subshell" $ do
-    id <- getNextId
-    char '('
-    allspacing
-    list <- readCompoundList
-    allspacing
-    char ')' <|> fail ") closing the subshell"
-    return $ T_Subshell id list
-
-prop_readBraceGroup = isOk readBraceGroup "{ a; b | c | d; e; }"
-prop_readBraceGroup2 = isWarning readBraceGroup "{foo;}"
-prop_readBraceGroup3 = isOk readBraceGroup "{(foo)}"
-readBraceGroup = called "brace group" $ do
-    id <- getNextId
-    char '{'
-    void allspacingOrFail <|> optional (do
-        lookAhead $ noneOf "(" -- {( is legal
-        parseProblem ErrorC 1054 "You need a space after the '{'.")
-    optional $ do
-        pos <- getPosition
-        lookAhead $ char '}'
-        parseProblemAt pos ErrorC 1055 "You need at least one command here. Use 'true;' as a no-op."
-    list <- readTerm
-    char '}' <|> do
-        parseProblem ErrorC 1056 "Expected a '}'. If you have one, try a ; or \\n in front of it."
-        fail "Missing '}'"
-    return $ T_BraceGroup id list
-
-prop_readWhileClause = isOk readWhileClause "while [[ -e foo ]]; do sleep 1; done"
-readWhileClause = called "while loop" $ do
-    pos <- getPosition
-    (T_While id) <- g_While
-    condition <- readTerm
-    statements <- readDoGroup pos
-    return $ T_WhileExpression id condition statements
-
-prop_readUntilClause = isOk readUntilClause "until kill -0 $PID; do sleep 1; done"
-readUntilClause = called "until loop" $ do
-    pos <- getPosition
-    (T_Until id) <- g_Until
-    condition <- readTerm
-    statements <- readDoGroup pos
-    return $ T_UntilExpression id condition statements
-
-readDoGroup loopPos = do
-    pos <- getPosition
-    optional (do
-                try . lookAhead $ g_Done
-                parseProblemAt loopPos ErrorC 1057 "Did you forget the 'do' for this loop?")
-
-    g_Do `orFail` do
-        parseProblem ErrorC 1058 "Expected 'do'."
-        return "Expected 'do'"
-
-    acceptButWarn g_Semi ErrorC 1059 "No semicolons directly after 'do'."
-    allspacing
-
-    optional (do
-                try . lookAhead $ g_Done
-                parseProblemAt loopPos ErrorC 1060 "Can't have empty do clauses (use 'true' as a no-op).")
-
-    commands <- readCompoundList
-    g_Done `orFail` do
-            parseProblemAt pos ErrorC 1061 "Couldn't find 'done' for this 'do'."
-            parseProblem ErrorC 1062 "Expected 'done' matching previously mentioned 'do'."
-            return "Expected 'done'"
-    return commands
-
-
-prop_readForClause = isOk readForClause "for f in *; do rm \"$f\"; done"
-prop_readForClause3 = isOk readForClause "for f; do foo; done"
-prop_readForClause4 = isOk readForClause "for((i=0; i<10; i++)); do echo $i; done"
-prop_readForClause5 = isOk readForClause "for ((i=0;i<10 && n>x;i++,--n))\ndo \necho $i\ndone"
-prop_readForClause6 = isOk readForClause "for ((;;))\ndo echo $i\ndone"
-prop_readForClause7 = isOk readForClause "for ((;;)) do echo $i\ndone"
-prop_readForClause8 = isOk readForClause "for ((;;)) ; do echo $i\ndone"
-prop_readForClause9 = isOk readForClause "for i do true; done"
-prop_readForClause10= isOk readForClause "for ((;;)) { true; }"
-prop_readForClause12= isWarning readForClause "for $a in *; do echo \"$a\"; done"
-prop_readForClause13= isOk readForClause "for foo\nin\\\n  bar\\\n  baz\ndo true; done"
-readForClause = called "for loop" $ do
-    pos <- getPosition
-    (T_For id) <- g_For
-    spacing
-    readArithmetic id pos <|> readRegular id pos
-  where
-    readArithmetic id pos = called "arithmetic for condition" $ do
-        try $ string "(("
-        x <- readArithmeticContents
-        char ';' >> spacing
-        y <- readArithmeticContents
-        char ';' >> spacing
-        z <- readArithmeticContents
-        spacing
-        string "))"
-        spacing
-        optional $ readSequentialSep >> spacing
-        group <- readBraced <|> readDoGroup pos
-        return $ T_ForArithmetic id x y z group
-
-    readBraced = do
-        (T_BraceGroup _ list) <- readBraceGroup
-        return list
-
-    readRegular id pos = do
-        acceptButWarn (char '$') ErrorC 1086
-            "Don't use $ on the iterator name in for loops."
-        name <- readVariableName `thenSkip` allspacing
-        values <- readInClause <|> (optional readSequentialSep >> return [])
-        group <- readDoGroup pos
-        return $ T_ForIn id name values group
-
-prop_readSelectClause1 = isOk readSelectClause "select foo in *; do echo $foo; done"
-prop_readSelectClause2 = isOk readSelectClause "select foo; do echo $foo; done"
-readSelectClause = called "select loop" $ do
-    pos <- getPosition
-    (T_Select id) <- g_Select
-    spacing
-    typ <- readRegular
-    group <- readDoGroup pos
-    typ id group
-  where
-    readRegular = do
-        name <- readVariableName
-        spacing
-        values <- readInClause <|> (readSequentialSep >> return [])
-        return $ \id group -> (return $ T_SelectIn id name values group)
-
-readInClause = do
-    g_In
-    things <- readCmdWord `reluctantlyTill`
-                (void g_Semi <|> void linefeed <|> void g_Do)
-
-    do {
-        lookAhead g_Do;
-        parseNote ErrorC 1063 "You need a line feed or semicolon before the 'do'.";
-    } <|> do {
-        optional g_Semi;
-        void allspacing;
-    }
-
-    return things
-
-prop_readCaseClause = isOk readCaseClause "case foo in a ) lol; cow;; b|d) fooo; esac"
-prop_readCaseClause2 = isOk readCaseClause "case foo\n in * ) echo bar;; esac"
-prop_readCaseClause3 = isOk readCaseClause "case foo\n in * ) echo bar & ;; esac"
-prop_readCaseClause4 = isOk readCaseClause "case foo\n in *) echo bar ;& bar) foo; esac"
-prop_readCaseClause5 = isOk readCaseClause "case foo\n in *) echo bar;;& foo) baz;; esac"
-readCaseClause = called "case expression" $ do
-    id <- getNextId
-    g_Case
-    word <- readNormalWord
-    allspacing
-    g_In <|> fail "Expected 'in'"
-    readLineBreak
-    list <- readCaseList
-    g_Esac <|> fail "Expected 'esac' to close the case statement"
-    return $ T_CaseExpression id word list
-
-readCaseList = many readCaseItem
-
-readCaseItem = called "case item" $ do
-    notFollowedBy2 g_Esac
-    optional $ do
-        try . lookAhead $ readAnnotationPrefix
-        parseProblem ErrorC 1124 "ShellCheck directives are only valid in front of complete commands like 'case' statements, not individual case branches."
-    optional g_Lparen
-    spacing
-    pattern' <- readPattern
-    void g_Rparen <|> do
-        parseProblem ErrorC 1085
-            "Did you forget to move the ;; after extending this case item?"
-        fail "Expected ) to open a new case item"
-    readLineBreak
-    list <- (lookAhead readCaseSeparator >> return []) <|> readCompoundList
-    separator <- readCaseSeparator `attempting` do
-        pos <- getPosition
-        lookAhead g_Rparen
-        parseProblemAt pos ErrorC 1074
-            "Did you forget the ;; after the previous case item?"
-    readLineBreak
-    return (separator, pattern', list)
-
-readCaseSeparator = choice [
-    tryToken ";;&" (const ()) >> return CaseContinue,
-    tryToken ";&" (const ()) >> return CaseFallThrough,
-    g_DSEMI >> return CaseBreak,
-    lookAhead (readLineBreak >> g_Esac) >> return CaseBreak
-    ]
-
-prop_readFunctionDefinition = isOk readFunctionDefinition "foo() { command foo --lol \"$@\"; }"
-prop_readFunctionDefinition1 = isOk readFunctionDefinition "foo   (){ command foo --lol \"$@\"; }"
-prop_readFunctionDefinition4 = isWarning readFunctionDefinition "foo(a, b) { true; }"
-prop_readFunctionDefinition5 = isOk readFunctionDefinition ":(){ :|:;}"
-prop_readFunctionDefinition6 = isOk readFunctionDefinition "?(){ foo; }"
-prop_readFunctionDefinition7 = isOk readFunctionDefinition "..(){ cd ..; }"
-prop_readFunctionDefinition8 = isOk readFunctionDefinition "foo() (ls)"
-prop_readFunctionDefinition9 = isOk readFunctionDefinition "function foo { true; }"
-prop_readFunctionDefinition10= isOk readFunctionDefinition "function foo () { true; }"
-prop_readFunctionDefinition11= isWarning readFunctionDefinition "function foo{\ntrue\n}"
-prop_readFunctionDefinition12= isOk readFunctionDefinition "function []!() { true; }"
-readFunctionDefinition = called "function" $ do
-    functionSignature <- try readFunctionSignature
-    allspacing
-    void (lookAhead $ oneOf "{(") <|> parseProblem ErrorC 1064 "Expected a { to open the function definition."
-    group <- readBraceGroup <|> readSubshell
-    return $ functionSignature group
-  where
-    readFunctionSignature =
-        readWithFunction <|> readWithoutFunction
-      where
-        readWithFunction = do
-            id <- getNextId
-            try $ do
-                string "function"
-                whitespace
-            spacing
-            name <- many1 extendedFunctionChars
-            spaces <- spacing
-            hasParens <- wasIncluded readParens
-            when (not hasParens && null spaces) $
-                acceptButWarn (lookAhead (oneOf "{("))
-                    ErrorC 1095 "You need a space or linefeed between the function name and body."
-            return $ T_Function id (FunctionKeyword True) (FunctionParentheses hasParens) name
-
-        readWithoutFunction = try $ do
-            id <- getNextId
-            name <- many1 functionChars
-            guard $ name /= "time"  -- Interfers with time ( foo )
-            spacing
-            readParens
-            return $ T_Function id (FunctionKeyword False) (FunctionParentheses True) name
-
-        readParens = do
-            g_Lparen
-            spacing
-            g_Rparen <|> do
-                parseProblem ErrorC 1065 "Trying to declare parameters? Don't. Use () and refer to params as $1, $2.."
-                many $ noneOf "\n){"
-                g_Rparen
-            return ()
-
-prop_readCoProc1 = isOk readCoProc "coproc foo { echo bar; }"
-prop_readCoProc2 = isOk readCoProc "coproc { echo bar; }"
-prop_readCoProc3 = isOk readCoProc "coproc echo bar"
-readCoProc = called "coproc" $ do
-    id <- getNextId
-    try $ do
-        string "coproc"
-        whitespace
-    choice [ try $ readCompoundCoProc id, readSimpleCoProc id ]
-  where
-    readCompoundCoProc id = do
-        var <- optionMaybe $
-            readVariableName `thenSkip` whitespace
-        body <- readBody readCompoundCommand
-        return $ T_CoProc id var body
-    readSimpleCoProc id = do
-        body <- readBody readSimpleCommand
-        return $ T_CoProc id Nothing body
-    readBody parser = do
-        id <- getNextId
-        body <- parser
-        return $ T_CoProcBody id body
-
-
-readPattern = (readNormalWord `thenSkip` spacing) `sepBy1` (char '|' `thenSkip` spacing)
-
-prop_readCompoundCommand = isOk readCompoundCommand "{ echo foo; }>/dev/null"
-readCompoundCommand = do
-    id <- getNextId
-    cmd <- choice [
-        readBraceGroup,
-        readAmbiguous "((" readArithmeticExpression readSubshell (\pos ->
-            parseNoteAt pos WarningC 1105 "Shells disambiguate (( differently or not at all. For subshell, add spaces around ( . For ((, fix parsing errors."),
-        readSubshell,
-        readCondition,
-        readWhileClause,
-        readUntilClause,
-        readIfClause,
-        readForClause,
-        readSelectClause,
-        readCaseClause,
-        readFunctionDefinition
-        ]
-    spacing
-    redirs <- many readIoRedirect
-    unless (null redirs) $ optional $ do
-        lookAhead $ try (spacing >> needsSeparator)
-        parseProblem WarningC 1013 "Bash requires ; or \\n here, after redirecting nested compound commands."
-    return $ T_Redirecting id redirs cmd
-  where
-    needsSeparator = choice [ g_Then, g_Else, g_Elif, g_Fi, g_Do, g_Done, g_Esac, g_Rbrace ]
-
-
-readCompoundList = readTerm
-readCompoundListOrEmpty = do
-    allspacing
-    readTerm <|> return []
-
-readCmdPrefix = many1 (readIoRedirect <|> readAssignmentWord)
-readCmdSuffix = many1 (readIoRedirect <|> readCmdWord)
-readModifierSuffix = many1 (readIoRedirect <|> readWellFormedAssignment <|> readCmdWord)
-readTimeSuffix = do
-    flags <- many readFlag
-    pipeline <- readPipeline
-    return $ flags ++ [pipeline]
-  where
-    -- This fails for quoted variables and such. Fixme?
-    readFlag = do
-        lookAhead $ char '-'
-        readCmdWord
-
--- Fixme: this is a hack that doesn't handle let c='4'"5" or let a\>b
-readLetSuffix :: Monad m => SCParser m [Token]
-readLetSuffix = many1 (readIoRedirect <|> try readLetExpression <|> readCmdWord)
-  where
-    readLetExpression :: Monad m => SCParser m Token
-    readLetExpression = do
-        startPos <- getPosition
-        expression <- readStringForParser readCmdWord
-        let (unQuoted, newPos) = kludgeAwayQuotes expression startPos
-        subParse newPos readArithmeticContents unQuoted
-
-    kludgeAwayQuotes :: String -> SourcePos -> (String, SourcePos)
-    kludgeAwayQuotes s p =
-        case s of
-            first:rest@(_:_) ->
-                let (last:backwards) = reverse rest
-                    middle = reverse backwards
-                in
-                    if first `elem` "'\"" && first == last
-                    then (middle, updatePosChar p first)
-                    else (s, p)
-            x -> (s, p)
-
-
--- bash allows a=(b), ksh allows $a=(b). dash allows neither. Let's warn.
-readEvalSuffix = many1 (readIoRedirect <|> readCmdWord <|> evalFallback)
-  where
-    evalFallback = do
-        pos <- getPosition
-        lookAhead $ char '('
-        parseProblemAt pos WarningC 1098 "Quote/escape special characters when using eval, e.g. eval \"a=(b)\"."
-        fail "Unexpected parentheses. Make sure to quote when eval'ing as shell parsers differ."
-
--- Get whatever a parser would parse as a string
-readStringForParser parser = do
-    pos <- inSeparateContext $ lookAhead (parser >> getPosition)
-    readUntil pos
-  where
-    readUntil endPos = anyChar `reluctantlyTill` (getPosition >>= guard . (== endPos))
-
-prop_readAssignmentWord = isOk readAssignmentWord "a=42"
-prop_readAssignmentWord2 = isOk readAssignmentWord "b=(1 2 3)"
-prop_readAssignmentWord3 = isWarning readAssignmentWord "$b = 13"
-prop_readAssignmentWord4 = isWarning readAssignmentWord "b = $(lol)"
-prop_readAssignmentWord5 = isOk readAssignmentWord "b+=lol"
-prop_readAssignmentWord6 = isWarning readAssignmentWord "b += (1 2 3)"
-prop_readAssignmentWord7 = isOk readAssignmentWord "a[3$n'']=42"
-prop_readAssignmentWord8 = isOk readAssignmentWord "a[4''$(cat foo)]=42"
-prop_readAssignmentWord9 = isOk readAssignmentWord "IFS= "
-prop_readAssignmentWord9a= isOk readAssignmentWord "foo="
-prop_readAssignmentWord9b= isOk readAssignmentWord "foo=  "
-prop_readAssignmentWord9c= isOk readAssignmentWord "foo=  #bar"
-prop_readAssignmentWord10= isWarning readAssignmentWord "foo$n=42"
-prop_readAssignmentWord11= isOk readAssignmentWord "foo=([a]=b [c] [d]= [e f )"
-prop_readAssignmentWord12= isOk readAssignmentWord "a[b <<= 3 + c]='thing'"
-prop_readAssignmentWord13= isOk readAssignmentWord "var=( (1 2) (3 4) )"
-prop_readAssignmentWord14= isOk readAssignmentWord "var=( 1 [2]=(3 4) )"
-prop_readAssignmentWord15= isOk readAssignmentWord "var=(1 [2]=(3 4))"
-readAssignmentWord = readAssignmentWordExt True
-readWellFormedAssignment = readAssignmentWordExt False
-readAssignmentWordExt lenient = try $ do
-    id <- getNextId
-    pos <- getPosition
-    when lenient $
-        optional (char '$' >> parseNote ErrorC 1066 "Don't use $ on the left side of assignments.")
-    variable <- readVariableName
-    when lenient $
-        optional (readNormalDollar >> parseNoteAt pos ErrorC
-                                1067 "For indirection, use (associative) arrays or 'read \"var$n\" <<< \"value\"'")
-    indices <- many readArrayIndex
-    hasLeftSpace <- liftM (not . null) spacing
-    pos <- getPosition
-    op <- readAssignmentOp
-    hasRightSpace <- liftM (not . null) spacing
-    isEndOfCommand <- liftM isJust $ optionMaybe (try . lookAhead $ (void (oneOf "\r\n;&|)") <|> eof))
-    if not hasLeftSpace && (hasRightSpace || isEndOfCommand)
-      then do
-        when (variable /= "IFS" && hasRightSpace && not isEndOfCommand) $
-            parseNoteAt pos WarningC 1007
-                "Remove space after = if trying to assign a value (for empty string, use var='' ... )."
-        value <- readEmptyLiteral
-        return $ T_Assignment id op variable indices value
-      else do
-        when (hasLeftSpace || hasRightSpace) $
-            parseNoteAt pos ErrorC 1068 $
-                "Don't put spaces around the "
-                ++ if op == Append
-                    then "+= when appending."
-                    else "= in assignments."
-        value <- readArray <|> readNormalWord
-        spacing
-        return $ T_Assignment id op variable indices value
-  where
-    readAssignmentOp = do
-        pos <- getPosition
-        unexpecting "" $ string "==="
-        choice [
-            string "+=" >> return Append,
-            do
-                try (string "==")
-                parseProblemAt pos ErrorC 1097
-                    "Unexpected ==. For assignment, use =. For comparison, use [/[[."
-                return Assign,
-
-            string "=" >> return Assign
-            ]
-    readEmptyLiteral = do
-        id <- getNextId
-        return $ T_Literal id ""
-
-readArrayIndex = do
-    id <- getNextId
-    char '['
-    pos <- getPosition
-    str <- readStringForParser readIndexSpan
-    char ']'
-    return $ T_UnparsedIndex id pos str
-
-readArray :: Monad m => SCParser m Token
-readArray = called "array assignment" $ do
-    id <- getNextId
-    opening <- getPosition
-    char '('
-    optional $ do
-        lookAhead $ char '('
-        parseProblemAt opening ErrorC 1116 "Missing $ on a $((..)) expression? (or use ( ( for arrays)."
-    allspacing
-    words <- readElement `reluctantlyTill` char ')'
-    char ')' <|> fail "Expected ) to close array assignment"
-    return $ T_Array id words
-  where
-    readElement = (readIndexed <|> readRegular) `thenSkip` allspacing
-    readIndexed = do
-        id <- getNextId
-        index <- try $ do
-            x <- many1 readArrayIndex
-            char '='
-            return x
-        value <- readRegular <|> nothing
-        return $ T_IndexedElement id index value
-    readRegular = readArray <|> readNormalWord
-
-    nothing = do
-        id <- getNextId
-        return $ T_Literal id ""
-
-tryToken s t = try $ do
-    id <- getNextId
-    string s
-    spacing
-    return $ t id
-
-redirToken c t = try $ do
-    id <- getNextId
-    char c
-    notFollowedBy2 $ char '('
-    return $ t id
-
-tryWordToken s t = tryParseWordToken s t `thenSkip` spacing
-tryParseWordToken keyword t = try $ do
-    id <- getNextId
-    str <- anycaseString keyword
-
-    optional $ do
-        try . lookAhead $ char '['
-        parseProblem ErrorC 1069 "You need a space before the [."
-    optional $ do
-        try . lookAhead $ char '#'
-        parseProblem ErrorC 1099 "You need a space before the #."
-
-    lookAhead keywordSeparator
-    when (str /= keyword) $
-        parseProblem ErrorC 1081 $
-            "Scripts are case sensitive. Use '" ++ keyword ++ "', not '" ++ str ++ "'."
-    return $ t id
-
-anycaseString =
-    mapM anycaseChar
-  where
-    anycaseChar c = char (toLower c) <|> char (toUpper c)
-
-g_AND_IF = tryToken "&&" T_AND_IF
-g_OR_IF = tryToken "||" T_OR_IF
-g_DSEMI = tryToken ";;" T_DSEMI
-g_DLESS = tryToken "<<" T_DLESS
-g_DGREAT = tryToken ">>" T_DGREAT
-g_LESSAND = tryToken "<&" T_LESSAND
-g_GREATAND = tryToken ">&" T_GREATAND
-g_LESSGREAT = tryToken "<>" T_LESSGREAT
-g_DLESSDASH = tryToken "<<-" T_DLESSDASH
-g_CLOBBER = tryToken ">|" T_CLOBBER
-g_OPERATOR = g_AND_IF <|> g_OR_IF <|> g_DSEMI <|> g_DLESSDASH <|> g_DLESS <|> g_DGREAT <|> g_LESSAND <|> g_GREATAND <|> g_LESSGREAT
-
-g_If = tryWordToken "if" T_If
-g_Then = tryWordToken "then" T_Then
-g_Else = tryWordToken "else" T_Else
-g_Elif = tryWordToken "elif" T_Elif
-g_Fi = tryWordToken "fi" T_Fi
-g_Do = tryWordToken "do" T_Do
-g_Done = tryWordToken "done" T_Done
-g_Case = tryWordToken "case" T_Case
-g_Esac = tryWordToken "esac" T_Esac
-g_While = tryWordToken "while" T_While
-g_Until = tryWordToken "until" T_Until
-g_For = tryWordToken "for" T_For
-g_Select = tryWordToken "select" T_Select
-g_In = tryWordToken "in" T_In <* skipAnnotationAndWarn
-g_Lbrace = tryWordToken "{" T_Lbrace
-g_Rbrace = do -- handled specially due to ksh echo "${ foo; }bar"
-    id <- getNextId
-    char '}'
-    return $ T_Rbrace id
-
-g_Lparen = tryToken "(" T_Lparen
-g_Rparen = tryToken ")" T_Rparen
-g_Bang = do
-    id <- getNextId
-    char '!'
-    void spacing1 <|> do
-        pos <- getPosition
-        parseProblemAt pos ErrorC 1035
-            "You are missing a required space after the !."
-    return $ T_Bang id
-
-g_Semi = do
-    notFollowedBy2 g_DSEMI
-    tryToken ";" T_Semi
-
-keywordSeparator =
-    eof <|> void (try allspacingOrFail) <|> void (oneOf ";()[<>&|")
-
-readKeyword = choice [ g_Then, g_Else, g_Elif, g_Fi, g_Do, g_Done, g_Esac, g_Rbrace, g_Rparen, g_DSEMI ]
-
-ifParse p t f =
-    (lookAhead (try p) >> t) <|> f
-
-prop_readShebang1 = isOk readShebang "#!/bin/sh\n"
-prop_readShebang2 = isWarning readShebang "!# /bin/sh\n"
-prop_readShebang3 = isNotOk readShebang "#shellcheck shell=/bin/sh\n"
-prop_readShebang4 = isWarning readShebang "! /bin/sh"
-readShebang = do
-    choice $ map try [
-        readCorrect,
-        readSwapped,
-        readTooManySpaces,
-        readMissingHash,
-        readMissingBang
-        ]
-    many linewhitespace
-    str <- many $ noneOf "\r\n"
-    optional carriageReturn
-    optional linefeed
-    return str
-  where
-    readCorrect = void $ string "#!"
-
-    readSwapped = do
-        pos <- getPosition
-        string "!#"
-        parseProblemAt pos ErrorC 1084
-            "Use #!, not !#, for the shebang."
-
-    skipSpaces = liftM (not . null) $ many linewhitespace
-    readTooManySpaces = do
-        startPos <- getPosition
-        startSpaces <- skipSpaces
-        char '#'
-        middlePos <- getPosition
-        middleSpaces <- skipSpaces
-        char '!'
-        when startSpaces $
-            parseProblemAt startPos ErrorC 1114
-                "Remove leading spaces before the shebang."
-        when middleSpaces $
-            parseProblemAt middlePos ErrorC 1115
-                "Remove spaces between # and ! in the shebang."
-
-    readMissingHash = do
-        pos <- getPosition
-        char '!'
-        ensurePathAhead
-        parseProblemAt pos ErrorC 1104
-            "Use #!, not just !, for the shebang."
-
-    readMissingBang = do
-        char '#'
-        pos <- getPosition
-        ensurePathAhead
-        parseProblemAt pos ErrorC 1113
-            "Use #!, not just #, for the shebang."
-
-
-    ensurePathAhead = lookAhead $ do
-        many linewhitespace
-        char '/'
-
-verifyEof = eof <|> choice [
-        ifParsable g_Lparen $
-            parseProblem ErrorC 1088 "Parsing stopped here. Invalid use of parentheses?",
-
-        ifParsable readKeyword $
-            parseProblem ErrorC 1089 "Parsing stopped here. Is this keyword correctly matched up?",
-
-        parseProblem ErrorC 1070 "Parsing stopped here. Mismatched keywords or invalid parentheses?"
-    ]
-  where
-    ifParsable p action = do
-        try (lookAhead p)
-        action
-
-prop_readScript1 = isOk readScriptFile "#!/bin/bash\necho hello world\n"
-prop_readScript2 = isWarning readScriptFile "#!/bin/bash\r\necho hello world\n"
-prop_readScript3 = isWarning readScriptFile "#!/bin/bash\necho hello\xA0world"
-prop_readScript4 = isWarning readScriptFile "#!/usr/bin/perl\nfoo=("
-prop_readScript5 = isOk readScriptFile "#!/bin/bash\n#This is an empty script\n\n"
-readScriptFile = do
-    id <- getNextId
-    pos <- getPosition
-    optional $ do
-        readUtf8Bom
-        parseProblem ErrorC 1082
-            "This file has a UTF-8 BOM. Remove it with: LC_CTYPE=C sed '1s/^...//' < yourscript ."
-    sb <- option "" readShebang
-    verifyShell pos (getShell sb)
-    if isValidShell (getShell sb) /= Just False
-      then do
-            allspacing
-            annotationId <- getNextId
-            annotations <- readAnnotations
-            commands <- withAnnotations annotations readCompoundListOrEmpty
-            verifyEof
-            let script = T_Annotation annotationId annotations $  T_Script id sb commands
-            reparseIndices script
-        else do
-            many anyChar
-            return $ T_Script id sb []
-
-  where
-    basename s = reverse . takeWhile (/= '/') . reverse $ s
-    getShell sb =
-        case words sb of
-            [] -> ""
-            [x] -> basename x
-            (first:second:_) ->
-                if basename first == "env"
-                    then second
-                    else basename first
-
-    verifyShell pos s =
-        case isValidShell s of
-            Just True -> return ()
-            Just False -> parseProblemAt pos ErrorC 1071 "ShellCheck only supports sh/bash/dash/ksh scripts. Sorry!"
-            Nothing -> parseProblemAt pos InfoC 1008 "This shebang was unrecognized. Note that ShellCheck only handles sh/bash/dash/ksh."
-
-    isValidShell s =
-        let good = s == "" || any (`isPrefixOf` s) goodShells
-            bad = any (`isPrefixOf` s) badShells
-        in
-            if good
-                then Just True
-                else if bad
-                        then Just False
-                        else Nothing
-
-    goodShells = [
-        "sh",
-        "ash",
-        "dash",
-        "bash",
-        "ksh"
-        ]
-    badShells = [
-        "awk",
-        "csh",
-        "expect",
-        "perl",
-        "python",
-        "ruby",
-        "tcsh",
-        "zsh"
-        ]
-
-    readUtf8Bom = called "Byte Order Mark" $ string "\xFEFF"
-
-readScript = do
-    script <- readScriptFile
-    reparseIndices script
-
-
--- Interactively run a parser in ghci:
--- debugParse readScript "echo 'hello world'"
-debugParse p string = runIdentity $ do
-    (res, _) <- runParser testEnvironment p "-" string
-    return res
-
-testEnvironment =
-    Environment {
-        systemInterface = (mockedSystemInterface []),
-        checkSourced = False
-    }
-
-
-isOk p s =      parsesCleanly p s == Just True   -- The string parses with no warnings
-isWarning p s = parsesCleanly p s == Just False  -- The string parses with warnings
-isNotOk p s =   parsesCleanly p s == Nothing     -- The string does not parse
-
-parsesCleanly parser string = runIdentity $ do
-    (res, sys) <- runParser testEnvironment
-                    (parser >> eof >> getState) "-" string
-    case (res, sys) of
-        (Right userState, systemState) ->
-            return $ Just . null $ parseNotes userState ++ parseProblems systemState
-        (Left _, _) -> return Nothing
-
--- For printf debugging: print the value of an expression
--- Example: return $ dump $ T_Literal id [c]
-dump :: Show a => a -> a
-dump x = trace (show x) x
-
--- Like above, but print a specific expression:
--- Example: return $ dumps ("Returning: " ++ [c])  $ T_Literal id [c]
-dumps :: Show x => x -> a -> a
-dumps t = trace (show t)
-
-parseWithNotes parser = do
-    item <- parser
-    state <- getState
-    return (item, state)
-
-compareNotes (ParseNote pos1 pos1' level1 _ s1) (ParseNote pos2 pos2' level2 _ s2) = compare (pos1, pos1', level1) (pos2, pos2', level2)
-sortNotes = sortBy compareNotes
-
-
-makeErrorFor parsecError =
-    ParseNote pos pos ErrorC 1072 $
-        getStringFromParsec $ errorMessages parsecError
-    where
-      pos = errorPos parsecError
-
-getStringFromParsec errors =
-        case map f errors of
-            r -> unwords (take 1 $ catMaybes $ reverse r)  ++
-                " Fix any mentioned problems and try again."
-    where
-        f err =
-            case err of
-                UnExpect s    ->  Nothing -- Due to not knowing Parsec, none of these
-                SysUnExpect s ->  Nothing -- are actually helpful. <?> has been hidden
-                Expect s      ->  Nothing -- and we only show explicit fail statements.
-                Message s     ->  if null s then Nothing else return $ s ++ "."
-
-runParser :: Monad m =>
-    Environment m ->
-    SCParser m v ->
-    String ->
-    String ->
-    m (Either ParseError v, SystemState)
-
-runParser env p filename contents =
-    Ms.runStateT
-        (Mr.runReaderT
-            (runParserT p initialUserState filename contents)
-            env)
-        initialSystemState
-system = lift . lift . lift
-
-parseShell env name contents = do
-    (result, state) <- runParser env (parseWithNotes readScript) name contents
-    case result of
-        Right (script, userstate) ->
-            return ParseResult {
-                prComments = map toPositionedComment $ nub $ parseNotes userstate ++ parseProblems state,
-                prTokenPositions = Map.map posToPos (positionMap userstate),
-                prRoot = Just $
-                    reattachHereDocs script (hereDocMap userstate)
-            }
-        Left err ->
-            return ParseResult {
-                prComments =
-                    map toPositionedComment $
-                        notesForContext (contextStack state)
-                        ++ [makeErrorFor err]
-                        ++ parseProblems state,
-                prTokenPositions = Map.empty,
-                prRoot = Nothing
-            }
-  where
-    isName (ContextName _ _) = True
-    isName _ = False
-    notesForContext list = zipWith ($) [first, second] $ filter isName list
-    first (ContextName pos str) = ParseNote pos pos ErrorC 1073 $
-        "Couldn't parse this " ++ str ++ "."
-    second (ContextName pos str) = ParseNote pos pos InfoC 1009 $
-        "The mentioned parser error was in this " ++ str ++ "."
-
--- Go over all T_UnparsedIndex and reparse them as either arithmetic or text
--- depending on declare -A statements.
-reparseIndices root =
-   analyze blank blank f root
-  where
-    associative = getAssociativeArrays root
-    isAssociative s = s `elem` associative
-    f (T_Assignment id mode name indices value) = do
-        newIndices <- mapM (fixAssignmentIndex name) indices
-        newValue <- case value of
-            (T_Array id2 words) -> do
-                newWords <- mapM (fixIndexElement name) words
-                return $ T_Array id2 newWords
-            x -> return x
-        return $ T_Assignment id mode name newIndices newValue
-    f t = return t
-
-    fixIndexElement name word =
-        case word of
-            T_IndexedElement id indices value -> do
-                new <- mapM (fixAssignmentIndex name) indices
-                return $ T_IndexedElement id new value
-            otherwise -> return word
-
-    fixAssignmentIndex name word =
-        case word of
-            T_UnparsedIndex id pos src -> do
-                parsed name pos src
-            otherwise -> return word
-
-    parsed name pos src =
-        if isAssociative name
-        then subParse pos (called "associative array index" $ readIndexSpan) src
-        else subParse pos (called "arithmetic array index expression" $ optional space >> readArithmeticContents) src
-
-reattachHereDocs root map =
-    doTransform f root
-  where
-    f t@(T_HereDoc id dash quote string []) = fromMaybe t $ do
-        list <- Map.lookup id map
-        return $ T_HereDoc id dash quote string list
-    f t = t
-
-toPositionedComment :: ParseNote -> PositionedComment
-toPositionedComment (ParseNote start end severity code message) =
-    PositionedComment (posToPos start) (posToPos end) $ Comment severity code message
-
-posToPos :: SourcePos -> Position
-posToPos sp = Position {
-    posFile = sourceName sp,
-    posLine = fromIntegral $ sourceLine sp,
-    posColumn = fromIntegral $ sourceColumn sp
-}
-
--- TODO: Clean up crusty old code that this is layered on top of
-parseScript :: Monad m =>
-        SystemInterface m -> ParseSpec -> m ParseResult
-parseScript sys spec =
-    parseShell env (psFilename spec) (psScript spec)
-  where
-    env = Environment {
-        systemInterface = sys,
-        checkSourced = psCheckSourced spec
-    }
-
-
-return []
-runTests = $quickCheckAll
-
diff --git a/ShellCheck/Regex.hs b/ShellCheck/Regex.hs
deleted file mode 100644
--- a/ShellCheck/Regex.hs
+++ /dev/null
@@ -1,80 +0,0 @@
-{-
-    Copyright 2012-2015 Vidar Holen
-
-    This file is part of ShellCheck.
-    http://www.vidarholen.net/contents/shellcheck
-
-    ShellCheck is free software: you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation, either version 3 of the License, or
-    (at your option) any later version.
-
-    ShellCheck is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program.  If not, see <http://www.gnu.org/licenses/>.
--}
-{-# LANGUAGE FlexibleContexts #-}
-
--- Basically Text.Regex based on regex-tdfa instead of the buggy regex-posix.
-module ShellCheck.Regex where
-
-import Data.List
-import Data.Maybe
-import Control.Monad
-import Text.Regex.TDFA
-
--- Precompile the regex
-mkRegex :: String -> Regex
-mkRegex str =
-    let make :: RegexMaker Regex CompOption ExecOption String => String -> Regex
-        make = makeRegex
-    in
-        make str
-
--- Does the regex match?
-matches :: String -> Regex -> Bool
-matches = flip match
-
--- Get all subgroups of the first match
-matchRegex :: Regex -> String -> Maybe [String]
-matchRegex re str = do
-    (_, _, _, groups) <- matchM re str :: Maybe (String,String,String,[String])
-    return groups
-
--- Get all full matches
-matchAllStrings :: Regex -> String -> [String]
-matchAllStrings re = unfoldr f
-  where
-    f :: String -> Maybe (String, String)
-    f str = do
-        (_, match, rest, _) <- matchM re str :: Maybe (String, String, String, [String])
-        return (match, rest)
-
--- Get all subgroups from all matches
-matchAllSubgroups :: Regex -> String -> [[String]]
-matchAllSubgroups re = unfoldr f
-  where
-    f :: String -> Maybe ([String], String)
-    f str = do
-        (_, _, rest, groups) <- matchM re str :: Maybe (String, String, String, [String])
-        return (groups, rest)
-
--- Replace regex in input with string
-subRegex :: Regex -> String -> String -> String
-subRegex re input replacement = f input
-  where
-    f str = fromMaybe str $ do
-        (before, match, after) <- matchM re str :: Maybe (String, String, String)
-        when (null match) $ error ("Internal error: substituted empty in " ++ str)
-        return $ before ++ replacement ++ f after
-
--- Split a string based on a regex.
-splitOn :: String -> Regex -> [String]
-splitOn input re =
-    case matchM re input :: Maybe (String, String, String) of
-        Just (before, match, after) -> before : after `splitOn` re
-        Nothing -> [input]
diff --git a/shellcheck.1 b/shellcheck.1
--- a/shellcheck.1
+++ b/shellcheck.1
@@ -1,4 +1,4 @@
-.\" Automatically generated by Pandoc 1.19.2.1
+.\" Automatically generated by Pandoc 1.19.2.4
 .\"
 .TH "SHELLCHECK" "1" "" "Shell script analysis tool" ""
 .hy
@@ -287,7 +287,7 @@
 .PP
 Copyright 2012\-2015, Vidar Holen.
 Licensed under the GNU General Public License version 3 or later, see
-http://gnu.org/licenses/gpl.html
+https://gnu.org/licenses/gpl.html
 .SH SEE ALSO
 .PP
 sh(1) bash(1)
diff --git a/shellcheck.1.md b/shellcheck.1.md
--- a/shellcheck.1.md
+++ b/shellcheck.1.md
@@ -207,7 +207,7 @@
 # COPYRIGHT
 Copyright 2012-2015, Vidar Holen.
 Licensed under the GNU General Public License version 3 or later,
-see http://gnu.org/licenses/gpl.html
+see https://gnu.org/licenses/gpl.html
 
 
 # SEE ALSO
diff --git a/shellcheck.hs b/shellcheck.hs
--- a/shellcheck.hs
+++ b/shellcheck.hs
@@ -2,7 +2,7 @@
     Copyright 2012-2015 Vidar Holen
 
     This file is part of ShellCheck.
-    http://www.vidarholen.net/contents/shellcheck
+    https://www.shellcheck.net
 
     ShellCheck is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -15,37 +15,38 @@
     GNU General Public License for more details.
 
     You should have received a copy of the GNU General Public License
-    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 -}
-import ShellCheck.Data
-import ShellCheck.Checker
-import ShellCheck.Interface
-import ShellCheck.Regex
+import           ShellCheck.Checker
+import           ShellCheck.Data
+import           ShellCheck.Interface
+import           ShellCheck.Regex
 
-import ShellCheck.Formatter.Format
 import qualified ShellCheck.Formatter.CheckStyle
+import           ShellCheck.Formatter.Format
 import qualified ShellCheck.Formatter.GCC
 import qualified ShellCheck.Formatter.JSON
 import qualified ShellCheck.Formatter.TTY
 
-import Control.Exception
-import Control.Monad
-import Control.Monad.Except
-import Data.Bits
-import Data.Char
-import Data.Either
-import Data.Functor
-import Data.IORef
-import Data.List
-import qualified Data.Map as Map
-import Data.Maybe
-import Data.Monoid
-import Prelude hiding (catch)
-import System.Console.GetOpt
-import System.Directory
-import System.Environment
-import System.Exit
-import System.IO
+import           Control.Exception
+import           Control.Monad
+import           Control.Monad.Except
+import           Data.Bits
+import           Data.Char
+import           Data.Either
+import           Data.Functor
+import           Data.IORef
+import           Data.List
+import qualified Data.Map                        as Map
+import           Data.Maybe
+import           Data.Monoid
+import           Data.Semigroup                  (Semigroup (..))
+import           Prelude                         hiding (catch)
+import           System.Console.GetOpt
+import           System.Directory
+import           System.Environment
+import           System.Exit
+import           System.IO
 
 data Flag = Flag String String
 data Status =
@@ -56,13 +57,16 @@
     | RuntimeException
   deriving (Ord, Eq, Show)
 
+instance Semigroup Status where
+    (<>) = max
+
 instance Monoid Status where
     mempty = NoProblems
-    mappend = max
+    mappend = (Data.Semigroup.<>)
 
 data Options = Options {
-    checkSpec :: CheckSpec,
-    externalSources :: Bool,
+    checkSpec        :: CheckSpec,
+    externalSources  :: Bool,
     formatterOptions :: FormatterOptions
 }
 
@@ -117,9 +121,9 @@
   where
     names = Map.keys $ formats (formatterOptions defaultOptions)
 
-getOption [] _ = Nothing
+getOption [] _                  = Nothing
 getOption (Flag var val:_) name | name == var = return val
-getOption (_:rest) flag = getOption rest flag
+getOption (_:rest) flag         = getOption rest flag
 
 getOptions options name =
     map (\(Flag _ val) -> val) . filter (\(Flag var _) -> var == name) $ options
@@ -159,10 +163,10 @@
 
 statusToCode status =
     case status of
-        NoProblems -> ExitSuccess
-        SomeProblems -> ExitFailure 1
-        SyntaxFailure -> ExitFailure 3
-        SupportFailure -> ExitFailure 4
+        NoProblems       -> ExitSuccess
+        SomeProblems     -> ExitFailure 1
+        SyntaxFailure    -> ExitFailure 3
+        SupportFailure   -> ExitFailure 4
         RuntimeException -> ExitFailure 2
 
 process :: [Flag] -> [FilePath] -> ExceptT Status IO Status
@@ -203,7 +207,7 @@
 
     process :: FilePath -> IO Status
     process filename = do
-        input <- (siReadFile sys) filename
+        input <- siReadFile sys filename
         either (reportFailure filename) check input
       where
         check contents = do
@@ -220,10 +224,10 @@
 
 parseColorOption colorOption =
     case colorOption of
-        "auto" -> ColorAuto
+        "auto"   -> ColorAuto
         "always" -> ColorAlways
-        "never" -> ColorNever
-        _ -> error $ "Bad value for --color `" ++ colorOption ++ "'"
+        "never"  -> ColorNever
+        _        -> error $ "Bad value for --color `" ++ colorOption ++ "'"
 
 parseOption flag options =
     case flag of
@@ -292,7 +296,7 @@
     get cache inputs file = do
         map <- readIORef cache
         case Map.lookup file map of
-            Just x -> return $ Right x
+            Just x  -> return $ Right x
             Nothing -> fetch cache inputs file
 
     fetch cache inputs file = do
@@ -355,7 +359,7 @@
         in
             case next of
                 Just (n, remainder) -> chr n : decode remainder
-                Nothing -> c : decode rest
+                Nothing             -> c : decode rest
 
     construct x 0 rest = do
         guard $ x <= 0x10FFFF
@@ -378,4 +382,4 @@
     putStrLn   "ShellCheck - shell script analysis tool"
     putStrLn $ "version: " ++ shellcheckVersion
     putStrLn   "license: GNU General Public License, version 3"
-    putStrLn   "website: http://www.shellcheck.net"
+    putStrLn   "website: https://www.shellcheck.net"
diff --git a/src/ShellCheck/AST.hs b/src/ShellCheck/AST.hs
new file mode 100644
--- /dev/null
+++ b/src/ShellCheck/AST.hs
@@ -0,0 +1,389 @@
+{-
+    Copyright 2012-2015 Vidar Holen
+
+    This file is part of ShellCheck.
+    https://www.shellcheck.net
+
+    ShellCheck is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    ShellCheck is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <https://www.gnu.org/licenses/>.
+-}
+module ShellCheck.AST where
+
+import Control.Monad.Identity
+import Text.Parsec
+import qualified ShellCheck.Regex as Re
+import Prelude hiding (id)
+
+newtype Id = Id Int deriving (Show, Eq, Ord)
+
+data Quoted = Quoted | Unquoted deriving (Show, Eq)
+data Dashed = Dashed | Undashed deriving (Show, Eq)
+data AssignmentMode = Assign | Append deriving (Show, Eq)
+newtype FunctionKeyword = FunctionKeyword Bool deriving (Show, Eq)
+newtype FunctionParentheses = FunctionParentheses Bool deriving (Show, Eq)
+data CaseType = CaseBreak | CaseFallThrough | CaseContinue deriving (Show, Eq)
+
+newtype Root = Root Token
+data Token =
+    TA_Binary Id String Token Token
+    | TA_Assignment Id String Token Token
+    | TA_Variable Id String [Token]
+    | TA_Expansion Id [Token]
+    | TA_Sequence Id [Token]
+    | TA_Trinary Id Token Token Token
+    | TA_Unary Id String Token
+    | TC_And Id ConditionType String Token Token
+    | TC_Binary Id ConditionType String Token Token
+    | TC_Group Id ConditionType Token
+    | TC_Nullary Id ConditionType Token
+    | TC_Or Id ConditionType String Token Token
+    | TC_Unary Id ConditionType String Token
+    | TC_Empty Id ConditionType
+    | T_AND_IF Id
+    | T_AndIf Id Token Token
+    | T_Arithmetic Id Token
+    | T_Array Id [Token]
+    | T_IndexedElement Id [Token] Token
+    -- Store the index as string, and parse as arithmetic or string later
+    | T_UnparsedIndex Id SourcePos String
+    | T_Assignment Id AssignmentMode String [Token] Token
+    | T_Backgrounded Id Token
+    | T_Backticked Id [Token]
+    | T_Bang Id
+    | T_Banged Id Token
+    | T_BraceExpansion Id [Token]
+    | T_BraceGroup Id [Token]
+    | T_CLOBBER Id
+    | T_Case Id
+    | T_CaseExpression Id Token [(CaseType, [Token], [Token])]
+    | T_Condition Id ConditionType Token
+    | T_DGREAT Id
+    | T_DLESS Id
+    | T_DLESSDASH Id
+    | T_DSEMI Id
+    | T_Do Id
+    | T_DollarArithmetic Id Token
+    | T_DollarBraced Id Token
+    | T_DollarBracket Id Token
+    | T_DollarDoubleQuoted Id [Token]
+    | T_DollarExpansion Id [Token]
+    | T_DollarSingleQuoted Id String
+    | T_DollarBraceCommandExpansion Id [Token]
+    | T_Done Id
+    | T_DoubleQuoted Id [Token]
+    | T_EOF Id
+    | T_Elif Id
+    | T_Else Id
+    | T_Esac Id
+    | T_Extglob Id String [Token]
+    | T_FdRedirect Id String Token
+    | T_Fi Id
+    | T_For Id
+    | T_ForArithmetic Id Token Token Token [Token]
+    | T_ForIn Id String [Token] [Token]
+    | T_Function Id FunctionKeyword FunctionParentheses String Token
+    | T_GREATAND Id
+    | T_Glob Id String
+    | T_Greater Id
+    | T_HereDoc Id Dashed Quoted String [Token]
+    | T_HereString Id Token
+    | T_If Id
+    | T_IfExpression Id [([Token],[Token])] [Token]
+    | T_In  Id
+    | T_IoFile Id Token Token
+    | T_IoDuplicate Id Token String
+    | T_LESSAND Id
+    | T_LESSGREAT Id
+    | T_Lbrace Id
+    | T_Less Id
+    | T_Literal Id String
+    | T_Lparen Id
+    | T_NEWLINE Id
+    | T_NormalWord Id [Token]
+    | T_OR_IF Id
+    | T_OrIf Id Token Token
+    | T_ParamSubSpecialChar Id String -- e.g. '%' in ${foo%bar}  or '/' in ${foo/bar/baz}
+    | T_Pipeline Id [Token] [Token] -- [Pipe separators] [Commands]
+    | T_ProcSub Id String [Token]
+    | T_Rbrace Id
+    | T_Redirecting Id [Token] Token
+    | T_Rparen Id
+    | T_Script Id String [Token]
+    | T_Select Id
+    | T_SelectIn Id String [Token] [Token]
+    | T_Semi Id
+    | T_SimpleCommand Id [Token] [Token]
+    | T_SingleQuoted Id String
+    | T_Subshell Id [Token]
+    | T_Then Id
+    | T_Until Id
+    | T_UntilExpression Id [Token] [Token]
+    | T_While Id
+    | T_WhileExpression Id [Token] [Token]
+    | T_Annotation Id [Annotation] Token
+    | T_Pipe Id String
+    | T_CoProc Id (Maybe String) Token
+    | T_CoProcBody Id Token
+    | T_Include Id Token
+    | T_SourceCommand Id Token Token
+    deriving (Show)
+
+data Annotation =
+    DisableComment Integer
+    | SourceOverride String
+    | ShellOverride String
+    deriving (Show, Eq)
+data ConditionType = DoubleBracket | SingleBracket deriving (Show, Eq)
+
+-- This is an abomination.
+tokenEquals :: Token -> Token -> Bool
+tokenEquals a b = kludge a == kludge b
+    where kludge s = Re.subRegex (Re.mkRegex "\\(Id [0-9]+\\)") (show s) "(Id 0)"
+
+instance Eq Token where
+    (==) = tokenEquals
+
+analyze :: Monad m => (Token -> m ()) -> (Token -> m ()) -> (Token -> m Token) -> Token -> m Token
+analyze f g i =
+    round
+  where
+    round t = do
+        f t
+        newT <- delve t
+        g t
+        i newT
+    roundAll = mapM round
+
+    dl l v = do
+        x <- roundAll l
+        return $ v x
+    dll l m v = do
+        x <- roundAll l
+        y <- roundAll m
+        return $ v x y
+    d1 t v = do
+        x <- round t
+        return $ v x
+    d2 t1 t2 v = do
+        x <- round t1
+        y <- round t2
+        return $ v x y
+
+    delve (T_NormalWord id list) = dl list $ T_NormalWord id
+    delve (T_DoubleQuoted id list) = dl list $ T_DoubleQuoted id
+    delve (T_DollarDoubleQuoted id list) = dl list $ T_DollarDoubleQuoted id
+    delve (T_DollarExpansion id list) = dl list $ T_DollarExpansion id
+    delve (T_DollarBraceCommandExpansion id list) = dl list $ T_DollarBraceCommandExpansion id
+    delve (T_BraceExpansion id list) = dl list $ T_BraceExpansion id
+    delve (T_Backticked id list) = dl list $ T_Backticked id
+    delve (T_DollarArithmetic id c) = d1 c $ T_DollarArithmetic id
+    delve (T_DollarBracket id c) = d1 c $ T_DollarBracket id
+    delve (T_IoFile id op file) = d2 op file $ T_IoFile id
+    delve (T_IoDuplicate id op num) = d1 op $ \x -> T_IoDuplicate id x num
+    delve (T_HereString id word) = d1 word $ T_HereString id
+    delve (T_FdRedirect id v t) = d1 t $ T_FdRedirect id v
+    delve (T_Assignment id mode var indices value) = do
+        a <- roundAll indices
+        b <- round value
+        return $ T_Assignment id mode var a b
+    delve (T_Array id t) = dl t $ T_Array id
+    delve (T_IndexedElement id indices t) = do
+        a <- roundAll indices
+        b <- round t
+        return $ T_IndexedElement id a b
+    delve (T_Redirecting id redirs cmd) = do
+        a <- roundAll redirs
+        b <- round cmd
+        return $ T_Redirecting id a b
+    delve (T_SimpleCommand id vars cmds) = dll vars cmds $ T_SimpleCommand id
+    delve (T_Pipeline id l1 l2) = dll l1 l2 $ T_Pipeline id
+    delve (T_Banged id l) = d1 l $ T_Banged id
+    delve (T_AndIf id t u) = d2 t u $ T_AndIf id
+    delve (T_OrIf id t u) = d2 t u $ T_OrIf id
+    delve (T_Backgrounded id l) = d1 l $ T_Backgrounded id
+    delve (T_Subshell id l) = dl l $ T_Subshell id
+    delve (T_ProcSub id typ l) = dl l $ T_ProcSub id typ
+    delve (T_Arithmetic id c) = d1 c $ T_Arithmetic id
+    delve (T_IfExpression id conditions elses) = do
+        newConds <- mapM (\(c, t) -> do
+                            x <- mapM round c
+                            y <- mapM round t
+                            return (x,y)
+                    ) conditions
+        newElses <- roundAll elses
+        return $ T_IfExpression id newConds newElses
+    delve (T_BraceGroup id l) = dl l $ T_BraceGroup id
+    delve (T_WhileExpression id c l) = dll c l $ T_WhileExpression id
+    delve (T_UntilExpression id c l) = dll c l $ T_UntilExpression id
+    delve (T_ForIn id v w l) = dll w l $ T_ForIn id v
+    delve (T_SelectIn id v w l) = dll w l $ T_SelectIn id v
+    delve (T_CaseExpression id word cases) = do
+        newWord <- round word
+        newCases <- mapM (\(o, c, t) -> do
+                            x <- mapM round c
+                            y <- mapM round t
+                            return (o, x,y)
+                        ) cases
+        return $ T_CaseExpression id newWord newCases
+
+    delve (T_ForArithmetic id a b c group) = do
+        x <- round a
+        y <- round b
+        z <- round c
+        list <- mapM round group
+        return $ T_ForArithmetic id x y z list
+
+    delve (T_Script id s l) = dl l $ T_Script id s
+    delve (T_Function id a b name body) = d1 body $ T_Function id a b name
+    delve (T_Condition id typ token) = d1 token $ T_Condition id typ
+    delve (T_Extglob id str l) = dl l $ T_Extglob id str
+    delve (T_DollarBraced id op) = d1 op $ T_DollarBraced id
+    delve (T_HereDoc id d q str l) = dl l $ T_HereDoc id d q str
+
+    delve (TC_And id typ str t1 t2) = d2 t1 t2 $ TC_And id typ str
+    delve (TC_Or id typ str t1 t2) = d2 t1 t2 $ TC_Or id typ str
+    delve (TC_Group id typ token) = d1 token $ TC_Group id typ
+    delve (TC_Binary id typ op lhs rhs) = d2 lhs rhs $ TC_Binary id typ op
+    delve (TC_Unary id typ op token) = d1 token $ TC_Unary id typ op
+    delve (TC_Nullary id typ token) = d1 token $ TC_Nullary id typ
+
+    delve (TA_Binary id op t1 t2) = d2 t1 t2 $ TA_Binary id op
+    delve (TA_Assignment id op t1 t2) = d2 t1 t2 $ TA_Assignment id op
+    delve (TA_Unary id op t1) = d1 t1 $ TA_Unary id op
+    delve (TA_Sequence id l) = dl l $ TA_Sequence id
+    delve (TA_Trinary id t1 t2 t3) = do
+        a <- round t1
+        b <- round t2
+        c <- round t3
+        return $ TA_Trinary id a b c
+    delve (TA_Expansion id t) = dl t $ TA_Expansion id
+    delve (TA_Variable id str t) = dl t $ TA_Variable id str
+    delve (T_Annotation id anns t) = d1 t $ T_Annotation id anns
+    delve (T_CoProc id var body) = d1 body $ T_CoProc id var
+    delve (T_CoProcBody id t) = d1 t $ T_CoProcBody id
+    delve (T_Include id script) = d1 script $ T_Include id
+    delve (T_SourceCommand id includer t_include) = d2 includer t_include $ T_SourceCommand id
+    delve t = return t
+
+getId :: Token -> Id
+getId t = case t of
+        T_AND_IF id  -> id
+        T_OR_IF id  -> id
+        T_DSEMI id  -> id
+        T_Semi id  -> id
+        T_DLESS id  -> id
+        T_DGREAT id  -> id
+        T_LESSAND id  -> id
+        T_GREATAND id  -> id
+        T_LESSGREAT id  -> id
+        T_DLESSDASH id  -> id
+        T_CLOBBER id  -> id
+        T_If id  -> id
+        T_Then id  -> id
+        T_Else id  -> id
+        T_Elif id  -> id
+        T_Fi id  -> id
+        T_Do id  -> id
+        T_Done id  -> id
+        T_Case id  -> id
+        T_Esac id  -> id
+        T_While id  -> id
+        T_Until id  -> id
+        T_For id  -> id
+        T_Select id  -> id
+        T_Lbrace id  -> id
+        T_Rbrace id  -> id
+        T_Lparen id  -> id
+        T_Rparen id  -> id
+        T_Bang id  -> id
+        T_In  id  -> id
+        T_NEWLINE id  -> id
+        T_EOF id  -> id
+        T_Less id  -> id
+        T_Greater id  -> id
+        T_SingleQuoted id _  -> id
+        T_Literal id _  -> id
+        T_NormalWord id _  -> id
+        T_DoubleQuoted id _  -> id
+        T_DollarExpansion id _  -> id
+        T_DollarBraced id _  -> id
+        T_DollarArithmetic id _  -> id
+        T_BraceExpansion id _  -> id
+        T_ParamSubSpecialChar id _ -> id
+        T_DollarBraceCommandExpansion id _  -> id
+        T_IoFile id _ _  -> id
+        T_IoDuplicate id _ _  -> id
+        T_HereDoc id _ _ _ _ -> id
+        T_HereString id _  -> id
+        T_FdRedirect id _ _  -> id
+        T_Assignment id _ _ _ _  -> id
+        T_Array id _  -> id
+        T_IndexedElement id _ _  -> id
+        T_Redirecting id _ _  -> id
+        T_SimpleCommand id _ _  -> id
+        T_Pipeline id _ _  -> id
+        T_Banged id _  -> id
+        T_AndIf id _ _ -> id
+        T_OrIf id _ _ -> id
+        T_Backgrounded id _  -> id
+        T_IfExpression id _ _  -> id
+        T_Subshell id _  -> id
+        T_BraceGroup id _  -> id
+        T_WhileExpression id _ _  -> id
+        T_UntilExpression id _ _  -> id
+        T_ForIn id _ _ _  -> id
+        T_SelectIn id _ _ _  -> id
+        T_CaseExpression id _ _ -> id
+        T_Function id _ _ _ _  -> id
+        T_Arithmetic id _  -> id
+        T_Script id _ _  -> id
+        T_Condition id _ _  -> id
+        T_Extglob id _ _ -> id
+        T_Backticked id _ -> id
+        TC_And id _ _ _ _  -> id
+        TC_Or id _ _ _ _  -> id
+        TC_Group id _ _  -> id
+        TC_Binary id _ _ _ _  -> id
+        TC_Unary id _ _ _  -> id
+        TC_Nullary id _ _  -> id
+        TA_Binary id _ _ _  -> id
+        TA_Assignment id _ _ _  -> id
+        TA_Unary id _ _  -> id
+        TA_Sequence id _  -> id
+        TA_Trinary id _ _ _  -> id
+        TA_Expansion id _  -> id
+        T_ProcSub id _ _ -> id
+        T_Glob id _ -> id
+        T_ForArithmetic id _ _ _ _ -> id
+        T_DollarSingleQuoted id _ -> id
+        T_DollarDoubleQuoted id _ -> id
+        T_DollarBracket id _ -> id
+        T_Annotation id _ _ -> id
+        T_Pipe id _ -> id
+        T_CoProc id _ _ -> id
+        T_CoProcBody id _ -> id
+        T_Include id _ -> id
+        T_SourceCommand id _ _ -> id
+        T_UnparsedIndex id _ _ -> id
+        TC_Empty id _ -> id
+        TA_Variable id _ _ -> id
+
+blank :: Monad m => Token -> m ()
+blank = const $ return ()
+doAnalysis :: Monad m => (Token -> m ()) -> Token -> m Token
+doAnalysis f = analyze f blank return
+doStackAnalysis :: Monad m => (Token -> m ()) -> (Token -> m ()) -> Token -> m Token
+doStackAnalysis startToken endToken = analyze startToken endToken return
+doTransform :: (Token -> Token) -> Token -> Token
+doTransform i = runIdentity . analyze blank blank (return . i)
+
diff --git a/src/ShellCheck/ASTLib.hs b/src/ShellCheck/ASTLib.hs
new file mode 100644
--- /dev/null
+++ b/src/ShellCheck/ASTLib.hs
@@ -0,0 +1,446 @@
+{-
+    Copyright 2012-2015 Vidar Holen
+
+    This file is part of ShellCheck.
+    https://www.shellcheck.net
+
+    ShellCheck is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    ShellCheck is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <https://www.gnu.org/licenses/>.
+-}
+module ShellCheck.ASTLib where
+
+import ShellCheck.AST
+
+import Control.Monad.Writer
+import Control.Monad
+import Data.Functor
+import Data.List
+import Data.Maybe
+
+-- Is this a type of loop?
+isLoop t = case t of
+        T_WhileExpression {} -> True
+        T_UntilExpression {} -> True
+        T_ForIn {} -> True
+        T_ForArithmetic {} -> True
+        T_SelectIn {}  -> True
+        _ -> False
+
+-- Will this split into multiple words when used as an argument?
+willSplit x =
+  case x of
+    T_DollarBraced {} -> True
+    T_DollarExpansion {} -> True
+    T_Backticked {} -> True
+    T_BraceExpansion {} -> True
+    T_Glob {} -> True
+    T_Extglob {} -> True
+    T_NormalWord _ l -> any willSplit l
+    _ -> False
+
+isGlob T_Extglob {} = True
+isGlob T_Glob {} = True
+isGlob (T_NormalWord _ l) = any isGlob l
+isGlob _ = False
+
+-- Is this shell word a constant?
+isConstant token =
+    case token of
+        -- This ignores some cases like ~"foo":
+        T_NormalWord _ (T_Literal _ ('~':_) : _)  -> False
+        T_NormalWord _ l   -> all isConstant l
+        T_DoubleQuoted _ l -> all isConstant l
+        T_SingleQuoted _ _ -> True
+        T_Literal _ _ -> True
+        _ -> False
+
+-- Is this an empty literal?
+isEmpty token =
+    case token of
+        T_NormalWord _ l   -> all isEmpty l
+        T_DoubleQuoted _ l -> all isEmpty l
+        T_SingleQuoted _ "" -> True
+        T_Literal _ "" -> True
+        _ -> False
+
+-- Quick&lazy oversimplification of commands, throwing away details
+-- and returning a list like  ["find", ".", "-name", "${VAR}*" ].
+oversimplify token =
+    case token of
+        (T_NormalWord _ l) -> [concat (concatMap oversimplify l)]
+        (T_DoubleQuoted _ l) -> [concat (concatMap oversimplify l)]
+        (T_SingleQuoted _ s) -> [s]
+        (T_DollarBraced _ _) -> ["${VAR}"]
+        (T_DollarArithmetic _ _) -> ["${VAR}"]
+        (T_DollarExpansion _ _) -> ["${VAR}"]
+        (T_Backticked _ _) -> ["${VAR}"]
+        (T_Glob _ s) -> [s]
+        (T_Pipeline _ _ [x]) -> oversimplify x
+        (T_Literal _ x) -> [x]
+        (T_ParamSubSpecialChar _ x) -> [x]
+        (T_SimpleCommand _ vars words) -> concatMap oversimplify words
+        (T_Redirecting _ _ foo) -> oversimplify foo
+        (T_DollarSingleQuoted _ s) -> [s]
+        (T_Annotation _ _ s) -> oversimplify s
+        -- Workaround for let "foo = bar" parsing
+        (TA_Sequence _ [TA_Expansion _ v]) -> concatMap oversimplify v
+        _ -> []
+
+
+-- Turn a SimpleCommand foo -avz --bar=baz into args "a", "v", "z", "bar",
+-- each in a tuple of (token, stringFlag). Non-flag arguments are added with
+-- stringFlag == "".
+getFlagsUntil stopCondition (T_SimpleCommand _ _ (_:args)) =
+    let tokenAndText = map (\x -> (x, concat $ oversimplify x)) args
+        (flagArgs, rest) = break (stopCondition . snd) tokenAndText
+    in
+        concatMap flag flagArgs ++ map (\(t, _) -> (t, "")) rest
+  where
+    flag (x, '-':'-':arg) = [ (x, takeWhile (/= '=') arg) ]
+    flag (x, '-':args) = map (\v -> (x, [v])) args
+    flag (x, _) = [ (x, "") ]
+getFlagsUntil _ _ = error "Internal shellcheck error, please report! (getFlags on non-command)"
+
+-- Get all flags in a GNU way, up until --
+getAllFlags :: Token -> [(Token, String)]
+getAllFlags = getFlagsUntil (== "--")
+-- Get all flags in a BSD way, up until first non-flag argument or --
+getLeadingFlags = getFlagsUntil (\x -> x == "--" || (not $ "-" `isPrefixOf` x))
+
+-- Check if a command has a flag.
+hasFlag cmd str = str `elem` (map snd $ getAllFlags cmd)
+
+-- Is this token a word that starts with a dash?
+isFlag token =
+    case getWordParts token of
+        T_Literal _ ('-':_) : _ -> True
+        _ -> False
+
+-- Is this token a flag where the - is unquoted?
+isUnquotedFlag token = fromMaybe False $ do
+    str <- getLeadingUnquotedString token
+    return $ "-" `isPrefixOf` str
+
+-- Given a T_DollarBraced, return a simplified version of the string contents.
+bracedString (T_DollarBraced _ l) = concat $ oversimplify l
+bracedString _ = error "Internal shellcheck error, please report! (bracedString on non-variable)"
+
+-- Is this an expansion of multiple items of an array?
+isArrayExpansion t@(T_DollarBraced _ _) =
+    let string = bracedString t in
+        "@" `isPrefixOf` string ||
+            not ("#" `isPrefixOf` string) && "[@]" `isInfixOf` string
+isArrayExpansion _ = False
+
+-- Is it possible that this arg becomes multiple args?
+mayBecomeMultipleArgs t = willBecomeMultipleArgs t || f t
+  where
+    f t@(T_DollarBraced _ _) =
+        let string = bracedString t in
+            "!" `isPrefixOf` string
+    f (T_DoubleQuoted _ parts) = any f parts
+    f (T_NormalWord _ parts) = any f parts
+    f _ = False
+
+-- Is it certain that this word will becomes multiple words?
+willBecomeMultipleArgs t = willConcatInAssignment t || f t
+  where
+    f T_Extglob {} = True
+    f T_Glob {} = True
+    f T_BraceExpansion {} = True
+    f (T_DoubleQuoted _ parts) = any f parts
+    f (T_NormalWord _ parts) = any f parts
+    f _ = False
+
+-- This does token cause implicit concatenation in assignments?
+willConcatInAssignment token =
+    case token of
+        t@T_DollarBraced {} -> isArrayExpansion t
+        (T_DoubleQuoted _ parts) -> any willConcatInAssignment parts
+        (T_NormalWord _ parts) -> any willConcatInAssignment parts
+        _ -> False
+
+-- Maybe get the literal string corresponding to this token
+getLiteralString :: Token -> Maybe String
+getLiteralString = getLiteralStringExt (const Nothing)
+
+-- Definitely get a literal string, skipping over all non-literals
+onlyLiteralString :: Token -> String
+onlyLiteralString = fromJust . getLiteralStringExt (const $ return "")
+
+-- Maybe get a literal string, but only if it's an unquoted argument.
+getUnquotedLiteral (T_NormalWord _ list) =
+    concat <$> mapM str list
+  where
+    str (T_Literal _ s) = return s
+    str _ = Nothing
+getUnquotedLiteral _ = Nothing
+
+-- Get the last unquoted T_Literal in a word like "${var}foo"THIS
+-- or nothing if the word does not end in an unquoted literal.
+getTrailingUnquotedLiteral :: Token -> Maybe Token
+getTrailingUnquotedLiteral t =
+    case t of
+        (T_NormalWord _ list@(_:_)) ->
+            from (last list)
+        _ -> Nothing
+  where
+    from t =
+        case t of
+            T_Literal {} -> return t
+            _ -> Nothing
+
+-- Get the leading, unquoted, literal string of a token (if any).
+getLeadingUnquotedString :: Token -> Maybe String
+getLeadingUnquotedString t =
+    case t of
+        T_NormalWord _ ((T_Literal _ s) : _) -> return s
+        _ -> Nothing
+
+-- Maybe get the literal string of this token and any globs in it.
+getGlobOrLiteralString = getLiteralStringExt f
+  where
+    f (T_Glob _ str) = return str
+    f _ = Nothing
+
+-- Maybe get the literal value of a token, using a custom function
+-- to map unrecognized Tokens into strings.
+getLiteralStringExt :: (Token -> Maybe String) -> Token -> Maybe String
+getLiteralStringExt more = g
+  where
+    allInList = fmap concat . mapM g
+    g (T_DoubleQuoted _ l) = allInList l
+    g (T_DollarDoubleQuoted _ l) = allInList l
+    g (T_NormalWord _ l) = allInList l
+    g (TA_Expansion _ l) = allInList l
+    g (T_SingleQuoted _ s) = return s
+    g (T_Literal _ s) = return s
+    g (T_ParamSubSpecialChar _ s) = return s
+    g x = more x
+
+-- Is this token a string literal?
+isLiteral t = isJust $ getLiteralString t
+
+
+-- Turn a NormalWord like foo="bar $baz" into a series of constituent elements like [foo=,bar ,$baz]
+getWordParts (T_NormalWord _ l)   = concatMap getWordParts l
+getWordParts (T_DoubleQuoted _ l) = l
+-- TA_Expansion is basically T_NormalWord for arithmetic expressions
+getWordParts (TA_Expansion _ l)   = concatMap getWordParts l
+getWordParts other                = [other]
+
+-- Return a list of NormalWords that would result from brace expansion
+braceExpand (T_NormalWord id list) = take 1000 $ do
+    items <- mapM part list
+    return $ T_NormalWord id items
+  where
+    part (T_BraceExpansion id items) = do
+        item <- items
+        braceExpand item
+    part x = return x
+
+-- Maybe get a SimpleCommand from immediate wrappers like T_Redirections
+getCommand t =
+    case t of
+        T_Redirecting _ _ w -> getCommand w
+        T_SimpleCommand _ _ (w:_) -> return t
+        T_Annotation _ _ t -> getCommand t
+        _ -> Nothing
+
+-- Maybe get the command name of a token representing a command
+getCommandName t = do
+    (T_SimpleCommand _ _ (w:rest)) <- getCommand t
+    s <- getLiteralString w
+    if "busybox" `isSuffixOf` s || "builtin" == s
+        then
+            case rest of
+                (applet:_) -> getLiteralString applet
+                _ -> return s
+        else
+            return s
+
+-- If a command substitution is a single command, get its name.
+--  $(date +%s) = Just "date"
+getCommandNameFromExpansion :: Token -> Maybe String
+getCommandNameFromExpansion t =
+    case t of
+        T_DollarExpansion _ [c] -> extract c
+        T_Backticked _ [c] -> extract c
+        T_DollarBraceCommandExpansion _ [c] -> extract c
+        _ -> Nothing
+  where
+    extract (T_Pipeline _ _ [cmd]) = getCommandName cmd
+    extract _ = Nothing
+
+-- Get the basename of a token representing a command
+getCommandBasename = fmap basename . getCommandName
+  where
+    basename = reverse . takeWhile (/= '/') . reverse
+
+isAssignment t =
+    case t of
+        T_Redirecting _ _ w -> isAssignment w
+        T_SimpleCommand _ (w:_) [] -> True
+        T_Assignment {} -> True
+        T_Annotation _ _ w -> isAssignment w
+        _ -> False
+
+isOnlyRedirection t =
+    case t of
+        T_Pipeline _ _ [x] -> isOnlyRedirection x
+        T_Annotation _ _ w -> isOnlyRedirection w
+        T_Redirecting _ (_:_) c -> isOnlyRedirection c
+        T_SimpleCommand _ [] [] -> True
+        _ -> False
+
+isFunction t = case t of T_Function {} -> True; _ -> False
+
+isBraceExpansion t = case t of T_BraceExpansion {} -> True; _ -> False
+
+-- Get the lists of commands from tokens that contain them, such as
+-- the body of while loops or branches of if statements.
+getCommandSequences :: Token -> [[Token]]
+getCommandSequences t =
+    case t of
+        T_Script _ _ cmds -> [cmds]
+        T_BraceGroup _ cmds -> [cmds]
+        T_Subshell _ cmds -> [cmds]
+        T_WhileExpression _ _ cmds -> [cmds]
+        T_UntilExpression _ _ cmds -> [cmds]
+        T_ForIn _ _ _ cmds -> [cmds]
+        T_ForArithmetic _ _ _ _ cmds -> [cmds]
+        T_IfExpression _ thens elses -> map snd thens ++ [elses]
+        T_Annotation _ _ t -> getCommandSequences t
+        _ -> []
+
+-- Get a list of names of associative arrays
+getAssociativeArrays t =
+    nub . execWriter $ doAnalysis f t
+  where
+    f :: Token -> Writer [String] ()
+    f t@T_SimpleCommand {} = fromMaybe (return ()) $ do
+        name <- getCommandName t
+        let assocNames = ["declare","local","typeset"]
+        guard $ elem name assocNames
+        let flags = getAllFlags t
+        guard $ elem "A" $ map snd flags
+        let args = map fst . filter ((==) "" . snd) $ flags
+        let names = mapMaybe (getLiteralStringExt nameAssignments) args
+        return $ tell names
+    f _ = return ()
+
+    nameAssignments t =
+        case t of
+            T_Assignment _ _ name _ _ -> return name
+            _ -> Nothing
+
+-- A Pseudoglob is a wildcard pattern used for checking if a match can succeed.
+-- For example, [[ $(cmd).jpg == [a-z] ]] will give the patterns *.jpg and ?, which
+-- can be proven never to match.
+data PseudoGlob = PGAny | PGMany | PGChar Char
+    deriving (Eq, Show)
+
+-- Turn a word into a PG pattern, replacing all unknown/runtime values with
+-- PGMany.
+wordToPseudoGlob :: Token -> Maybe [PseudoGlob]
+wordToPseudoGlob word =
+    simplifyPseudoGlob . concat <$> mapM f (getWordParts word)
+  where
+    f x = case x of
+        T_Literal _ s -> return $ map PGChar s
+        T_SingleQuoted _ s -> return $ map PGChar s
+
+        T_DollarBraced {} -> return [PGMany]
+        T_DollarExpansion {} -> return [PGMany]
+        T_Backticked {} -> return [PGMany]
+
+        T_Glob _ "?" -> return [PGAny]
+        T_Glob _ ('[':_)  -> return [PGAny]
+        T_Glob {} -> return [PGMany]
+
+        T_Extglob {} -> return [PGMany]
+
+        _ -> return [PGMany]
+
+-- Turn a word into a PG pattern, but only if we can preserve
+-- exact semantics.
+wordToExactPseudoGlob :: Token -> Maybe [PseudoGlob]
+wordToExactPseudoGlob word =
+    simplifyPseudoGlob . concat <$> mapM f (getWordParts word)
+  where
+    f x = case x of
+        T_Literal _ s -> return $ map PGChar s
+        T_SingleQuoted _ s -> return $ map PGChar s
+        T_Glob _ "?" -> return [PGAny]
+        T_Glob _ "*" -> return [PGMany]
+        _ -> fail "Unknown token type"
+
+-- Reorder a PseudoGlob for more efficient matching, e.g.
+-- f?*?**g -> f??*g
+simplifyPseudoGlob :: [PseudoGlob] -> [PseudoGlob]
+simplifyPseudoGlob = f
+  where
+    f [] = []
+    f (x@(PGChar _) : rest ) = x : f rest
+    f list =
+        let (anys, rest) = span (\x -> x == PGMany || x == PGAny) list in
+            order anys ++ f rest
+
+    order s = let (any, many) = partition (== PGAny) s in
+        any ++ take 1 many
+
+-- Check whether the two patterns can ever overlap.
+pseudoGlobsCanOverlap :: [PseudoGlob] -> [PseudoGlob] -> Bool
+pseudoGlobsCanOverlap = matchable
+  where
+    matchable x@(xf:xs) y@(yf:ys) =
+        case (xf, yf) of
+            (PGMany, _) -> matchable x ys || matchable xs y
+            (_, PGMany) -> matchable x ys || matchable xs y
+            (PGAny, _) -> matchable xs ys
+            (_, PGAny) -> matchable xs ys
+            (_, _) -> xf == yf && matchable xs ys
+
+    matchable [] [] = True
+    matchable (PGMany : rest) [] = matchable rest []
+    matchable (_:_) [] = False
+    matchable [] r = matchable r []
+
+-- Check whether the first pattern always overlaps the second.
+pseudoGlobIsSuperSetof :: [PseudoGlob] -> [PseudoGlob] -> Bool
+pseudoGlobIsSuperSetof = matchable
+  where
+    matchable x@(xf:xs) y@(yf:ys) =
+        case (xf, yf) of
+            (PGMany, PGMany) -> matchable x ys
+            (PGMany, _) -> matchable x ys || matchable xs y
+            (_, PGMany) -> False
+            (PGAny, _) -> matchable xs ys
+            (_, PGAny) -> False
+            (_, _) -> xf == yf && matchable xs ys
+
+    matchable [] [] = True
+    matchable (PGMany : rest) [] = matchable rest []
+    matchable _ _ = False
+
+wordsCanBeEqual x y = fromMaybe True $
+    liftM2 pseudoGlobsCanOverlap (wordToPseudoGlob x) (wordToPseudoGlob y)
+
+-- Is this an expansion that can be quoted,
+-- e.g. $(foo) `foo` $foo (but not {foo,})?
+isQuoteableExpansion t = case t of
+    T_DollarExpansion {} -> True
+    T_DollarBraceCommandExpansion {} -> True
+    T_Backticked {} -> True
+    T_DollarBraced {} -> True
+    _ -> False
diff --git a/src/ShellCheck/Analytics.hs b/src/ShellCheck/Analytics.hs
new file mode 100644
--- /dev/null
+++ b/src/ShellCheck/Analytics.hs
@@ -0,0 +1,2985 @@
+{-
+    Copyright 2012-2015 Vidar Holen
+
+    This file is part of ShellCheck.
+    https://www.shellcheck.net
+
+    ShellCheck is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    ShellCheck is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <https://www.gnu.org/licenses/>.
+-}
+{-# LANGUAGE TemplateHaskell #-}
+{-# LANGUAGE FlexibleContexts #-}
+module ShellCheck.Analytics (runAnalytics, ShellCheck.Analytics.runTests) where
+
+import ShellCheck.AST
+import ShellCheck.ASTLib
+import ShellCheck.AnalyzerLib hiding (producesComments)
+import ShellCheck.Data
+import ShellCheck.Parser
+import ShellCheck.Interface
+import ShellCheck.Regex
+
+import Control.Arrow (first)
+import Control.Monad
+import Control.Monad.Identity
+import Control.Monad.State
+import Control.Monad.Writer
+import Control.Monad.Reader
+import Data.Char
+import Data.Functor
+import Data.Function (on)
+import Data.List
+import Data.Maybe
+import Data.Ord
+import Debug.Trace
+import qualified Data.Map.Strict as Map
+import Test.QuickCheck.All (forAllProperties)
+import Test.QuickCheck.Test (quickCheckWithResult, stdArgs, maxSuccess)
+
+-- Checks that are run on the AST root
+treeChecks :: [Parameters -> Token -> [TokenComment]]
+treeChecks = [
+    runNodeAnalysis
+        (\p t -> (mapM_ ((\ f -> f t) . (\ f -> f p))
+            nodeChecks))
+    ,subshellAssignmentCheck
+    ,checkSpacefulness
+    ,checkQuotesInLiterals
+    ,checkShebangParameters
+    ,checkFunctionsUsedExternally
+    ,checkUnusedAssignments
+    ,checkUnpassedInFunctions
+    ,checkArrayWithoutIndex
+    ,checkShebang
+    ,checkUnassignedReferences
+    ,checkUncheckedCdPushdPopd
+    ,checkArrayAssignmentIndices
+    ,checkUseBeforeDefinition
+    ]
+
+runAnalytics :: AnalysisSpec -> [TokenComment]
+runAnalytics options =
+        runList options treeChecks
+
+runList :: AnalysisSpec -> [Parameters -> Token -> [TokenComment]]
+    -> [TokenComment]
+runList spec list = notes
+    where
+        root = asScript spec
+        params = makeParameters spec
+        notes = concatMap (\f -> f params root) list
+
+
+checkList l t = concatMap (\f -> f t) l
+
+
+-- Checks that are run on each node in the AST
+runNodeAnalysis f p t = execWriter (doAnalysis (f p) t)
+
+nodeChecks :: [Parameters -> Token -> Writer [TokenComment] ()]
+nodeChecks = [
+    checkUuoc
+    ,checkPipePitfalls
+    ,checkForInQuoted
+    ,checkForInLs
+    ,checkShorthandIf
+    ,checkDollarStar
+    ,checkUnquotedDollarAt
+    ,checkStderrRedirect
+    ,checkUnquotedN
+    ,checkNumberComparisons
+    ,checkSingleBracketOperators
+    ,checkDoubleBracketOperators
+    ,checkLiteralBreakingTest
+    ,checkConstantNullary
+    ,checkDivBeforeMult
+    ,checkArithmeticDeref
+    ,checkArithmeticBadOctal
+    ,checkComparisonAgainstGlob
+    ,checkCommarrays
+    ,checkOrNeq
+    ,checkEchoWc
+    ,checkConstantIfs
+    ,checkPipedAssignment
+    ,checkAssignAteCommand
+    ,checkUuoeVar
+    ,checkQuotedCondRegex
+    ,checkForInCat
+    ,checkFindExec
+    ,checkValidCondOps
+    ,checkGlobbedRegex
+    ,checkTestRedirects
+    ,checkIndirectExpansion
+    ,checkPS1Assignments
+    ,checkBackticks
+    ,checkInexplicablyUnquoted
+    ,checkTildeInQuotes
+    ,checkLonelyDotDash
+    ,checkSpuriousExec
+    ,checkSpuriousExpansion
+    ,checkDollarBrackets
+    ,checkSshHereDoc
+    ,checkGlobsAsOptions
+    ,checkWhileReadPitfalls
+    ,checkArithmeticOpCommand
+    ,checkCharRangeGlob
+    ,checkUnquotedExpansions
+    ,checkSingleQuotedVariables
+    ,checkRedirectToSame
+    ,checkPrefixAssignmentReference
+    ,checkLoopKeywordScope
+    ,checkCdAndBack
+    ,checkWrongArithmeticAssignment
+    ,checkConditionalAndOrs
+    ,checkFunctionDeclarations
+    ,checkStderrPipe
+    ,checkOverridingPath
+    ,checkArrayAsString
+    ,checkUnsupported
+    ,checkMultipleAppends
+    ,checkSuspiciousIFS
+    ,checkShouldUseGrepQ
+    ,checkTestArgumentSplitting
+    ,checkConcatenatedDollarAt
+    ,checkTildeInPath
+    ,checkMaskedReturns
+    ,checkReadWithoutR
+    ,checkLoopVariableReassignment
+    ,checkTrailingBracket
+    ,checkReturnAgainstZero
+    ,checkRedirectedNowhere
+    ,checkUnmatchableCases
+    ,checkSubshellAsTest
+    ,checkSplittingInArrays
+    ,checkRedirectionToNumber
+    ,checkGlobAsCommand
+    ,checkFlagAsCommand
+    ,checkEmptyCondition
+    ,checkPipeToNowhere
+    ,checkForLoopGlobVariables
+    ,checkSubshelledTests
+    ]
+
+
+wouldHaveBeenGlob s = '*' `elem` s
+
+verify :: (Parameters -> Token -> Writer [TokenComment] ()) -> String -> Bool
+verify f s = checkNode f s == Just True
+
+verifyNot :: (Parameters -> Token -> Writer [TokenComment] ()) -> String -> Bool
+verifyNot f s = checkNode f s == Just False
+
+verifyTree :: (Parameters -> Token -> [TokenComment]) -> String -> Bool
+verifyTree f s = producesComments f s == Just True
+
+verifyNotTree :: (Parameters -> Token -> [TokenComment]) -> String -> Bool
+verifyNotTree f s = producesComments f s == Just False
+
+checkCommand str f t@(T_SimpleCommand id _ (cmd:rest)) =
+    when (t `isCommand` str) $ f cmd rest
+checkCommand _ _ _ = return ()
+
+checkUnqualifiedCommand str f t@(T_SimpleCommand id _ (cmd:rest)) =
+    when (t `isUnqualifiedCommand` str) $ f cmd rest
+checkUnqualifiedCommand _ _ _ = return ()
+
+
+checkNode f = producesComments (runNodeAnalysis f)
+producesComments :: (Parameters -> Token -> [TokenComment]) -> String -> Maybe Bool
+producesComments f s = do
+        root <- pScript s
+        return . not . null $ runList (defaultSpec root) [f]
+
+-- Copied from https://wiki.haskell.org/Edit_distance
+dist :: Eq a => [a] -> [a] -> Int
+dist a b
+    = last (if lab == 0 then mainDiag
+            else if lab > 0 then lowers !! (lab - 1)
+                 else{- < 0 -}   uppers !! (-1 - lab))
+    where mainDiag = oneDiag a b (head uppers) (-1 : head lowers)
+          uppers = eachDiag a b (mainDiag : uppers) -- upper diagonals
+          lowers = eachDiag b a (mainDiag : lowers) -- lower diagonals
+          eachDiag a [] diags = []
+          eachDiag a (bch:bs) (lastDiag:diags) = oneDiag a bs nextDiag lastDiag : eachDiag a bs diags
+              where nextDiag = head (tail diags)
+          oneDiag a b diagAbove diagBelow = thisdiag
+              where doDiag [] b nw n w = []
+                    doDiag a [] nw n w = []
+                    doDiag (ach:as) (bch:bs) nw n w = me : doDiag as bs me (tail n) (tail w)
+                        where me = if ach == bch then nw else 1 + min3 (head w) nw (head n)
+                    firstelt = 1 + head diagBelow
+                    thisdiag = firstelt : doDiag a b firstelt diagAbove (tail diagBelow)
+          lab = length a - length b
+          min3 x y z = if x < y then x else min y z
+
+hasFloatingPoint params = shellType params == Ksh
+
+-- Checks whether the current parent path is part of a condition
+isCondition [] = False
+isCondition [_] = False
+isCondition (child:parent:rest) =
+    getId child `elem` map getId (getConditionChildren parent) || isCondition (parent:rest)
+  where
+    getConditionChildren t =
+        case t of
+            T_AndIf _ left right -> [left]
+            T_OrIf id left right -> [left]
+            T_IfExpression id conditions elses -> concatMap (take 1 . reverse . fst) conditions
+            T_WhileExpression id c l -> take 1 . reverse $ c
+            T_UntilExpression id c l -> take 1 . reverse $ c
+            _ -> []
+
+prop_checkEchoWc3 = verify checkEchoWc "n=$(echo $foo | wc -c)"
+checkEchoWc _ (T_Pipeline id _ [a, b]) =
+    when (acmd == ["echo", "${VAR}"]) $
+        case bcmd of
+            ["wc", "-c"] -> countMsg
+            ["wc", "-m"] -> countMsg
+            _ -> return ()
+  where
+    acmd = oversimplify a
+    bcmd = oversimplify b
+    countMsg = style id 2000 "See if you can use ${#variable} instead."
+checkEchoWc _ _ = return ()
+
+prop_checkPipedAssignment1 = verify checkPipedAssignment "A=ls | grep foo"
+prop_checkPipedAssignment2 = verifyNot checkPipedAssignment "A=foo cmd | grep foo"
+prop_checkPipedAssignment3 = verifyNot checkPipedAssignment "A=foo"
+checkPipedAssignment _ (T_Pipeline _ _ (T_Redirecting _ _ (T_SimpleCommand id (_:_) []):_:_)) =
+    warn id 2036 "If you wanted to assign the output of the pipeline, use a=$(b | c) ."
+checkPipedAssignment _ _ = return ()
+
+prop_checkAssignAteCommand1 = verify checkAssignAteCommand "A=ls -l"
+prop_checkAssignAteCommand2 = verify checkAssignAteCommand "A=ls --sort=$foo"
+prop_checkAssignAteCommand3 = verify checkAssignAteCommand "A=cat foo | grep bar"
+prop_checkAssignAteCommand4 = verifyNot checkAssignAteCommand "A=foo ls -l"
+prop_checkAssignAteCommand5 = verify checkAssignAteCommand "PAGER=cat grep bar"
+prop_checkAssignAteCommand6 = verifyNot checkAssignAteCommand "PAGER=\"cat\" grep bar"
+prop_checkAssignAteCommand7 = verify checkAssignAteCommand "here=pwd"
+checkAssignAteCommand _ (T_SimpleCommand id (T_Assignment _ _ _ _ assignmentTerm:[]) list) =
+    -- Check if first word is intended as an argument (flag or glob).
+    if firstWordIsArg list
+    then
+        err id 2037 "To assign the output of a command, use var=$(cmd) ."
+    else
+        -- Check if it's a known, unquoted command name.
+        when (isCommonCommand $ getUnquotedLiteral assignmentTerm) $
+            warn id 2209 "Use var=$(command) to assign output (or quote to assign string)."
+  where
+    isCommonCommand (Just s) = s `elem` commonCommands
+    isCommonCommand _ = False
+    firstWordIsArg list = fromMaybe False $ do
+        head <- list !!! 0
+        return $ isGlob head || isUnquotedFlag head
+
+checkAssignAteCommand _ _ = return ()
+
+prop_checkArithmeticOpCommand1 = verify checkArithmeticOpCommand "i=i + 1"
+prop_checkArithmeticOpCommand2 = verify checkArithmeticOpCommand "foo=bar * 2"
+prop_checkArithmeticOpCommand3 = verifyNot checkArithmeticOpCommand "foo + opts"
+checkArithmeticOpCommand _ (T_SimpleCommand id [T_Assignment {}] (firstWord:_)) =
+    fromMaybe (return ()) $ check <$> getGlobOrLiteralString firstWord
+  where
+    check op =
+        when (op `elem` ["+", "-", "*", "/"]) $
+            warn (getId firstWord) 2099 $
+                "Use $((..)) for arithmetics, e.g. i=$((i " ++ op ++ " 2))"
+checkArithmeticOpCommand _ _ = return ()
+
+prop_checkWrongArit = verify checkWrongArithmeticAssignment "i=i+1"
+prop_checkWrongArit2 = verify checkWrongArithmeticAssignment "n=2; i=n*2"
+checkWrongArithmeticAssignment params (T_SimpleCommand id (T_Assignment _ _ _ _ val:[]) []) =
+  fromMaybe (return ()) $ do
+    str <- getNormalString val
+    match <- matchRegex regex str
+    var <- match !!! 0
+    op <- match !!! 1
+    Map.lookup var references
+    return . warn (getId val) 2100 $
+        "Use $((..)) for arithmetics, e.g. i=$((i " ++ op ++ " 2))"
+  where
+    regex = mkRegex "^([_a-zA-Z][_a-zA-Z0-9]*)([+*-]).+$"
+    references = foldl (flip ($)) Map.empty (map insertRef $ variableFlow params)
+    insertRef (Assignment (_, _, name, _)) =
+        Map.insert name ()
+    insertRef _ = Prelude.id
+
+    getNormalString (T_NormalWord _ words) = do
+        parts <- foldl (liftM2 (\x y -> x ++ [y])) (Just []) $ map getLiterals words
+        return $ concat parts
+    getNormalString _ = Nothing
+
+    getLiterals (T_Literal _ s) = return s
+    getLiterals (T_Glob _ s) = return s
+    getLiterals _ = Nothing
+checkWrongArithmeticAssignment _ _ = return ()
+
+
+prop_checkUuoc1 = verify checkUuoc "cat foo | grep bar"
+prop_checkUuoc2 = verifyNot checkUuoc "cat * | grep bar"
+prop_checkUuoc3 = verify checkUuoc "cat $var | grep bar"
+prop_checkUuoc4 = verifyNot checkUuoc "cat $var"
+prop_checkUuoc5 = verifyNot checkUuoc "cat \"$@\""
+prop_checkUuoc6 = verifyNot checkUuoc "cat -n | grep bar"
+checkUuoc _ (T_Pipeline _ _ (T_Redirecting _ _ cmd:_:_)) =
+    checkCommand "cat" (const f) cmd
+  where
+    f [word] = unless (mayBecomeMultipleArgs word || isOption word) $
+        style (getId word) 2002 "Useless cat. Consider 'cmd < file | ..' or 'cmd file | ..' instead."
+    f _ = return ()
+    isOption word = "-" `isPrefixOf` onlyLiteralString word
+checkUuoc _ _ = return ()
+
+prop_checkPipePitfalls3 = verify checkPipePitfalls "ls | grep -v mp3"
+prop_checkPipePitfalls4 = verifyNot checkPipePitfalls "find . -print0 | xargs -0 foo"
+prop_checkPipePitfalls5 = verifyNot checkPipePitfalls "ls -N | foo"
+prop_checkPipePitfalls6 = verify checkPipePitfalls "find . | xargs foo"
+prop_checkPipePitfalls7 = verifyNot checkPipePitfalls "find . -printf '%s\\n' | xargs foo"
+prop_checkPipePitfalls8 = verify checkPipePitfalls "foo | grep bar | wc -l"
+prop_checkPipePitfalls9 = verifyNot checkPipePitfalls "foo | grep -o bar | wc -l"
+prop_checkPipePitfalls10 = verifyNot checkPipePitfalls "foo | grep -o bar | wc"
+prop_checkPipePitfalls11 = verifyNot checkPipePitfalls "foo | grep bar | wc"
+prop_checkPipePitfalls12 = verifyNot checkPipePitfalls "foo | grep -o bar | wc -c"
+prop_checkPipePitfalls13 = verifyNot checkPipePitfalls "foo | grep bar | wc -c"
+prop_checkPipePitfalls14 = verifyNot checkPipePitfalls "foo | grep -o bar | wc -cmwL"
+prop_checkPipePitfalls15 = verifyNot checkPipePitfalls "foo | grep bar | wc -cmwL"
+prop_checkPipePitfalls16 = verifyNot checkPipePitfalls "foo | grep -r bar | wc -l"
+checkPipePitfalls _ (T_Pipeline id _ commands) = do
+    for ["find", "xargs"] $
+        \(find:xargs:_) ->
+          let args = oversimplify xargs ++ oversimplify find
+          in
+            unless (any ($ args) [
+                hasShortParameter '0',
+                hasParameter "null",
+                hasParameter "print0",
+                hasParameter "printf"
+              ]) $ warn (getId find) 2038
+                      "Use -print0/-0 or -exec + to allow for non-alphanumeric filenames."
+
+    for' ["ps", "grep"] $
+        \x -> info x 2009 "Consider using pgrep instead of grepping ps output."
+
+    for ["grep", "wc"] $
+        \(grep:wc:_) ->
+            let flagsGrep = fromMaybe [] $ map snd . getAllFlags <$> getCommand grep
+                flagsWc = fromMaybe [] $ map snd . getAllFlags <$> getCommand wc
+            in
+                unless (any (`elem` ["o", "only-matching", "r", "R", "recursive"]) flagsGrep || any (`elem` ["m", "chars", "w", "words", "c", "bytes", "L", "max-line-length"]) flagsWc || null flagsWc) $
+                    style (getId grep) 2126 "Consider using grep -c instead of grep|wc -l."
+
+    didLs <- fmap or . sequence $ [
+        for' ["ls", "grep"] $
+            \x -> warn x 2010 "Don't use ls | grep. Use a glob or a for loop with a condition to allow non-alphanumeric filenames.",
+        for' ["ls", "xargs"] $
+            \x -> warn x 2011 "Use 'find .. -print0 | xargs -0 ..' or 'find .. -exec .. +' to allow non-alphanumeric filenames."
+        ]
+    unless didLs $ do
+        for ["ls", "?"] $
+            \(ls:_) -> unless (hasShortParameter 'N' (oversimplify ls)) $
+                info (getId ls) 2012 "Use find instead of ls to better handle non-alphanumeric filenames."
+        return ()
+  where
+    for l f =
+        let indices = indexOfSublists l (map (headOrDefault "" . oversimplify) commands)
+        in do
+            mapM_ (f . (\ n -> take (length l) $ drop n commands)) indices
+            return . not . null $ indices
+    for' l f = for l (first f)
+    first func (x:_) = func (getId x)
+    first _ _ = return ()
+    hasShortParameter char = any (\x -> "-" `isPrefixOf` x && char `elem` x)
+    hasParameter string =
+        any (isPrefixOf string . dropWhile (== '-'))
+checkPipePitfalls _ _ = return ()
+
+indexOfSublists sub = f 0
+  where
+    f _ [] = []
+    f n a@(r:rest) =
+        let others = f (n+1) rest in
+            if match sub a
+              then n:others
+              else others
+    match ("?":r1) (_:r2) = match r1 r2
+    match (x1:r1) (x2:r2) | x1 == x2 = match r1 r2
+    match [] _ = True
+    match _ _ = False
+
+
+prop_checkShebangParameters1 = verifyTree checkShebangParameters "#!/usr/bin/env bash -x\necho cow"
+prop_checkShebangParameters2 = verifyNotTree checkShebangParameters "#! /bin/sh  -l "
+checkShebangParameters p (T_Annotation _ _ t) = checkShebangParameters p t
+checkShebangParameters _ (T_Script id sb _) =
+    [makeComment ErrorC id 2096 "On most OS, shebangs can only specify a single parameter." | length (words sb) > 2]
+
+prop_checkShebang1 = verifyNotTree checkShebang "#!/usr/bin/env bash -x\necho cow"
+prop_checkShebang2 = verifyNotTree checkShebang "#! /bin/sh  -l "
+prop_checkShebang3 = verifyTree checkShebang "ls -l"
+prop_checkShebang4 = verifyNotTree checkShebang "#shellcheck shell=sh\nfoo"
+prop_checkShebang5 = verifyTree checkShebang "#!/usr/bin/env ash"
+prop_checkShebang6 = verifyNotTree checkShebang "#!/usr/bin/env ash\n# shellcheck shell=dash\n"
+prop_checkShebang7 = verifyNotTree checkShebang "#!/usr/bin/env ash\n# shellcheck shell=sh\n"
+checkShebang params (T_Annotation _ list t) =
+    if any isOverride list then [] else checkShebang params t
+  where
+    isOverride (ShellOverride _) = True
+    isOverride _ = False
+checkShebang params (T_Script id sb _) = execWriter $
+    unless (shellTypeSpecified params) $ do
+        when (sb == "") $
+            err id 2148 "Tips depend on target shell and yours is unknown. Add a shebang."
+        when (executableFromShebang sb == "ash") $
+            warn id 2187 "Ash scripts will be checked as Dash. Add '# shellcheck shell=dash' to silence."
+
+
+prop_checkForInQuoted = verify checkForInQuoted "for f in \"$(ls)\"; do echo foo; done"
+prop_checkForInQuoted2 = verifyNot checkForInQuoted "for f in \"$@\"; do echo foo; done"
+prop_checkForInQuoted2a = verifyNot checkForInQuoted "for f in *.mp3; do echo foo; done"
+prop_checkForInQuoted2b = verify checkForInQuoted "for f in \"*.mp3\"; do echo foo; done"
+prop_checkForInQuoted3 = verify checkForInQuoted "for f in 'find /'; do true; done"
+prop_checkForInQuoted4 = verify checkForInQuoted "for f in 1,2,3; do true; done"
+prop_checkForInQuoted4a = verifyNot checkForInQuoted "for f in foo{1,2,3}; do true; done"
+prop_checkForInQuoted5 = verify checkForInQuoted "for f in ls; do true; done"
+prop_checkForInQuoted6 = verifyNot checkForInQuoted "for f in \"${!arr}\"; do true; done"
+checkForInQuoted _ (T_ForIn _ f [T_NormalWord _ [word@(T_DoubleQuoted id list)]] _) =
+    when (any (\x -> willSplit x && not (mayBecomeMultipleArgs x)) list
+            || (fmap wouldHaveBeenGlob (getLiteralString word) == Just True)) $
+        err id 2066 "Since you double quoted this, it will not word split, and the loop will only run once."
+checkForInQuoted _ (T_ForIn _ f [T_NormalWord _ [T_SingleQuoted id _]] _) =
+    warn id 2041 "This is a literal string. To run as a command, use $(..) instead of '..' . "
+checkForInQuoted _ (T_ForIn _ f [T_NormalWord _ [T_Literal id s]] _) =
+    if ',' `elem` s
+      then unless ('{' `elem` s) $
+            warn id 2042 "Use spaces, not commas, to separate loop elements."
+      else warn id 2043 "This loop will only ever run once for a constant value. Did you perhaps mean to loop over dir/*, $var or $(cmd)?"
+checkForInQuoted _ _ = return ()
+
+prop_checkForInCat1 = verify checkForInCat "for f in $(cat foo); do stuff; done"
+prop_checkForInCat1a= verify checkForInCat "for f in `cat foo`; do stuff; done"
+prop_checkForInCat2 = verify checkForInCat "for f in $(cat foo | grep lol); do stuff; done"
+prop_checkForInCat2a= verify checkForInCat "for f in `cat foo | grep lol`; do stuff; done"
+prop_checkForInCat3 = verifyNot checkForInCat "for f in $(cat foo | grep bar | wc -l); do stuff; done"
+checkForInCat _ (T_ForIn _ f [T_NormalWord _ w] _) = mapM_ checkF w
+  where
+    checkF (T_DollarExpansion id [T_Pipeline _ _ r])
+        | all isLineBased r =
+            info id 2013 "To read lines rather than words, pipe/redirect to a 'while read' loop."
+    checkF (T_Backticked id cmds) = checkF (T_DollarExpansion id cmds)
+    checkF _ = return ()
+    isLineBased cmd = any (cmd `isCommand`)
+                        ["grep", "fgrep", "egrep", "sed", "cat", "awk", "cut", "sort"]
+checkForInCat _ _ = return ()
+
+prop_checkForInLs = verify checkForInLs "for f in $(ls *.mp3); do mplayer \"$f\"; done"
+prop_checkForInLs2 = verify checkForInLs "for f in `ls *.mp3`; do mplayer \"$f\"; done"
+prop_checkForInLs3 = verify checkForInLs "for f in `find / -name '*.mp3'`; do mplayer \"$f\"; done"
+checkForInLs _ = try
+  where
+   try (T_ForIn _ f [T_NormalWord _ [T_DollarExpansion id [x]]] _) =
+        check id f x
+   try (T_ForIn _ f [T_NormalWord _ [T_Backticked id [x]]] _) =
+        check id f x
+   try _ = return ()
+   check id f x =
+    case oversimplify x of
+      ("ls":n) ->
+        let warntype = if any ("-" `isPrefixOf`) n then warn else err in
+          warntype id 2045 "Iterating over ls output is fragile. Use globs."
+      ("find":_) -> warn id 2044 "For loops over find output are fragile. Use find -exec or a while read loop."
+      _ -> return ()
+
+
+prop_checkFindExec1 = verify checkFindExec "find / -name '*.php' -exec rm {};"
+prop_checkFindExec2 = verify checkFindExec "find / -exec touch {} && ls {} \\;"
+prop_checkFindExec3 = verify checkFindExec "find / -execdir cat {} | grep lol +"
+prop_checkFindExec4 = verifyNot checkFindExec "find / -name '*.php' -exec foo {} +"
+prop_checkFindExec5 = verifyNot checkFindExec "find / -execdir bash -c 'a && b' \\;"
+prop_checkFindExec6 = verify checkFindExec "find / -type d -execdir rm *.jpg \\;"
+checkFindExec _ cmd@(T_SimpleCommand _ _ t@(h:r)) | cmd `isCommand` "find" = do
+    c <- broken r False
+    when c $
+        let wordId = getId $ last t in
+            err wordId 2067 "Missing ';' or + terminating -exec. You can't use |/||/&&, and ';' has to be a separate, quoted argument."
+
+  where
+    broken [] v = return v
+    broken (w:r) v = do
+        when v (mapM_ warnFor $ fromWord w)
+        case getLiteralString w of
+            Just "-exec" -> broken r True
+            Just "-execdir" -> broken r True
+            Just "+" -> broken r False
+            Just ";" -> broken r False
+            _ -> broken r v
+
+    shouldWarn x =
+      case x of
+        T_DollarExpansion _ _ -> True
+        T_Backticked _ _ -> True
+        T_Glob _ _ -> True
+        T_Extglob {} -> True
+        _ -> False
+
+    warnFor x =
+        when(shouldWarn x) $
+            info (getId x) 2014 "This will expand once before find runs, not per file found."
+
+    fromWord (T_NormalWord _ l) = l
+    fromWord _ = []
+checkFindExec _ _ = return ()
+
+
+prop_checkUnquotedExpansions1 = verify checkUnquotedExpansions "rm $(ls)"
+prop_checkUnquotedExpansions1a= verify checkUnquotedExpansions "rm `ls`"
+prop_checkUnquotedExpansions2 = verify checkUnquotedExpansions "rm foo$(date)"
+prop_checkUnquotedExpansions3 = verify checkUnquotedExpansions "[ $(foo) == cow ]"
+prop_checkUnquotedExpansions3a= verify checkUnquotedExpansions "[ ! $(foo) ]"
+prop_checkUnquotedExpansions4 = verifyNot checkUnquotedExpansions "[[ $(foo) == cow ]]"
+prop_checkUnquotedExpansions5 = verifyNot checkUnquotedExpansions "for f in $(cmd); do echo $f; done"
+prop_checkUnquotedExpansions6 = verifyNot checkUnquotedExpansions "$(cmd)"
+prop_checkUnquotedExpansions7 = verifyNot checkUnquotedExpansions "cat << foo\n$(ls)\nfoo"
+prop_checkUnquotedExpansions8 = verifyNot checkUnquotedExpansions "set -- $(seq 1 4)"
+prop_checkUnquotedExpansions9 = verifyNot checkUnquotedExpansions "echo foo `# inline comment`"
+checkUnquotedExpansions params =
+    check
+  where
+    check t@(T_DollarExpansion _ c) = examine t c
+    check t@(T_Backticked _ c) = examine t c
+    check t@(T_DollarBraceCommandExpansion _ c) = examine t c
+    check _ = return ()
+    tree = parentMap params
+    examine t contents =
+        unless (null contents || shouldBeSplit t || isQuoteFree tree t || usedAsCommandName tree t) $
+            warn (getId t) 2046 "Quote this to prevent word splitting."
+
+    shouldBeSplit t =
+        getCommandNameFromExpansion t == Just "seq"
+
+
+prop_checkRedirectToSame = verify checkRedirectToSame "cat foo > foo"
+prop_checkRedirectToSame2 = verify checkRedirectToSame "cat lol | sed -e 's/a/b/g' > lol"
+prop_checkRedirectToSame3 = verifyNot checkRedirectToSame "cat lol | sed -e 's/a/b/g' > foo.bar && mv foo.bar lol"
+prop_checkRedirectToSame4 = verifyNot checkRedirectToSame "foo /dev/null > /dev/null"
+prop_checkRedirectToSame5 = verifyNot checkRedirectToSame "foo > bar 2> bar"
+prop_checkRedirectToSame6 = verifyNot checkRedirectToSame "echo foo > foo"
+prop_checkRedirectToSame7 = verifyNot checkRedirectToSame "sed 's/foo/bar/g' file | sponge file"
+prop_checkRedirectToSame8 = verifyNot checkRedirectToSame "while read -r line; do _=\"$fname\"; done <\"$fname\""
+checkRedirectToSame params s@(T_Pipeline _ _ list) =
+    mapM_ (\l -> (mapM_ (\x -> doAnalysis (checkOccurrences x) l) (getAllRedirs list))) list
+  where
+    note x = makeComment InfoC x 2094
+                "Make sure not to read and write the same file in the same pipeline."
+    checkOccurrences t@(T_NormalWord exceptId x) u@(T_NormalWord newId y) =
+        when (exceptId /= newId
+                && x == y
+                && not (isOutput t && isOutput u)
+                && not (special t)
+                && not (any isHarmlessCommand [t,u])
+                && not (any containsAssignment [u])) $ do
+            addComment $ note newId
+            addComment $ note exceptId
+    checkOccurrences _ _ = return ()
+    getAllRedirs = concatMap (\t ->
+        case t of
+            T_Redirecting _ ls _ -> concatMap getRedirs ls
+            _ -> [])
+    getRedirs (T_FdRedirect _ _ (T_IoFile _ op file)) =
+            case op of T_Greater _ -> [file]
+                       T_Less _    -> [file]
+                       T_DGREAT _  -> [file]
+                       _ -> []
+    getRedirs _ = []
+    special x = "/dev/" `isPrefixOf` concat (oversimplify x)
+    isOutput t =
+        case drop 1 $ getPath (parentMap params) t of
+            T_IoFile _ op _:_ ->
+                case op of
+                    T_Greater _  -> True
+                    T_DGREAT _ -> True
+                    _ -> False
+            _ -> False
+    isHarmlessCommand arg = fromMaybe False $ do
+        cmd <- getClosestCommand (parentMap params) arg
+        name <- getCommandBasename cmd
+        return $ name `elem` ["echo", "printf", "sponge"]
+    containsAssignment arg = fromMaybe False $ do
+        cmd <- getClosestCommand (parentMap params) arg
+        return $ isAssignment cmd
+
+checkRedirectToSame _ _ = return ()
+
+
+prop_checkShorthandIf  = verify checkShorthandIf "[[ ! -z file ]] && scp file host || rm file"
+prop_checkShorthandIf2 = verifyNot checkShorthandIf "[[ ! -z file ]] && { scp file host || echo 'Eek'; }"
+prop_checkShorthandIf3 = verifyNot checkShorthandIf "foo && bar || echo baz"
+prop_checkShorthandIf4 = verifyNot checkShorthandIf "foo && a=b || a=c"
+prop_checkShorthandIf5 = verifyNot checkShorthandIf "foo && rm || printf b"
+prop_checkShorthandIf6 = verifyNot checkShorthandIf "if foo && bar || baz; then true; fi"
+prop_checkShorthandIf7 = verifyNot checkShorthandIf "while foo && bar || baz; do true; done"
+prop_checkShorthandIf8 = verify checkShorthandIf "if true; then foo && bar || baz; fi"
+checkShorthandIf params x@(T_AndIf id _ (T_OrIf _ _ (T_Pipeline _ _ t)))
+        | not (isOk t || inCondition) =
+    info id 2015 "Note that A && B || C is not if-then-else. C may run when A is true."
+  where
+    isOk [t] = isAssignment t || fromMaybe False (do
+        name <- getCommandBasename t
+        return $ name `elem` ["echo", "exit", "return", "printf"])
+    isOk _ = False
+    inCondition = isCondition $ getPath (parentMap params) x
+checkShorthandIf _ _ = return ()
+
+
+prop_checkDollarStar = verify checkDollarStar "for f in $*; do ..; done"
+prop_checkDollarStar2 = verifyNot checkDollarStar "a=$*"
+prop_checkDollarStar3 = verifyNot checkDollarStar "[[ $* = 'a b' ]]"
+checkDollarStar p t@(T_NormalWord _ [b@(T_DollarBraced id _)])
+      | bracedString b == "*"  =
+    unless (isStrictlyQuoteFree (parentMap p) t) $
+        warn id 2048 "Use \"$@\" (with quotes) to prevent whitespace problems."
+checkDollarStar _ _ = return ()
+
+
+prop_checkUnquotedDollarAt = verify checkUnquotedDollarAt "ls $@"
+prop_checkUnquotedDollarAt1= verifyNot checkUnquotedDollarAt "ls ${#@}"
+prop_checkUnquotedDollarAt2 = verify checkUnquotedDollarAt "ls ${foo[@]}"
+prop_checkUnquotedDollarAt3 = verifyNot checkUnquotedDollarAt "ls ${#foo[@]}"
+prop_checkUnquotedDollarAt4 = verifyNot checkUnquotedDollarAt "ls \"$@\""
+prop_checkUnquotedDollarAt5 = verifyNot checkUnquotedDollarAt "ls ${foo/@/ at }"
+prop_checkUnquotedDollarAt6 = verifyNot checkUnquotedDollarAt "a=$@"
+prop_checkUnquotedDollarAt7 = verify checkUnquotedDollarAt "for f in ${var[@]}; do true; done"
+prop_checkUnquotedDollarAt8 = verifyNot checkUnquotedDollarAt "echo \"${args[@]:+${args[@]}}\""
+prop_checkUnquotedDollarAt9 = verifyNot checkUnquotedDollarAt "echo ${args[@]:+\"${args[@]}\"}"
+prop_checkUnquotedDollarAt10 = verifyNot checkUnquotedDollarAt "echo ${@+\"$@\"}"
+checkUnquotedDollarAt p word@(T_NormalWord _ parts) | not $ isStrictlyQuoteFree (parentMap p) word =
+    forM_ (take 1 $ filter isArrayExpansion parts) $ \x ->
+        unless (isQuotedAlternativeReference x) $
+            err (getId x) 2068
+                "Double quote array expansions to avoid re-splitting elements."
+checkUnquotedDollarAt _ _ = return ()
+
+prop_checkConcatenatedDollarAt1 = verify checkConcatenatedDollarAt "echo \"foo$@\""
+prop_checkConcatenatedDollarAt2 = verify checkConcatenatedDollarAt "echo ${arr[@]}lol"
+prop_checkConcatenatedDollarAt3 = verify checkConcatenatedDollarAt "echo $a$@"
+prop_checkConcatenatedDollarAt4 = verifyNot checkConcatenatedDollarAt "echo $@"
+prop_checkConcatenatedDollarAt5 = verifyNot checkConcatenatedDollarAt "echo \"${arr[@]}\""
+checkConcatenatedDollarAt p word@T_NormalWord {}
+    | not $ isQuoteFree (parentMap p) word =
+        unless (null $ drop 1 parts) $
+            mapM_ for array
+  where
+    parts = getWordParts word
+    array = take 1 $ filter isArrayExpansion parts
+    for t = err (getId t) 2145 "Argument mixes string and array. Use * or separate argument."
+checkConcatenatedDollarAt _ _ = return ()
+
+prop_checkArrayAsString1 = verify checkArrayAsString "a=$@"
+prop_checkArrayAsString2 = verify checkArrayAsString "a=\"${arr[@]}\""
+prop_checkArrayAsString3 = verify checkArrayAsString "a=*.png"
+prop_checkArrayAsString4 = verify checkArrayAsString "a={1..10}"
+prop_checkArrayAsString5 = verifyNot checkArrayAsString "a='*.gif'"
+prop_checkArrayAsString6 = verifyNot checkArrayAsString "a=$*"
+prop_checkArrayAsString7 = verifyNot checkArrayAsString "a=( $@ )"
+checkArrayAsString _ (T_Assignment id _ _ _ word) =
+    if willConcatInAssignment word
+    then
+      warn (getId word) 2124
+        "Assigning an array to a string! Assign as array, or use * instead of @ to concatenate."
+    else
+      when (willBecomeMultipleArgs word) $
+        warn (getId word) 2125
+          "Brace expansions and globs are literal in assignments. Quote it or use an array."
+checkArrayAsString _ _ = return ()
+
+prop_checkArrayWithoutIndex1 = verifyTree checkArrayWithoutIndex "foo=(a b); echo $foo"
+prop_checkArrayWithoutIndex2 = verifyNotTree checkArrayWithoutIndex "foo='bar baz'; foo=($foo); echo ${foo[0]}"
+prop_checkArrayWithoutIndex3 = verifyTree checkArrayWithoutIndex "coproc foo while true; do echo cow; done; echo $foo"
+prop_checkArrayWithoutIndex4 = verifyTree checkArrayWithoutIndex "coproc tail -f log; echo $COPROC"
+prop_checkArrayWithoutIndex5 = verifyTree checkArrayWithoutIndex "a[0]=foo; echo $a"
+prop_checkArrayWithoutIndex6 = verifyTree checkArrayWithoutIndex "echo $PIPESTATUS"
+prop_checkArrayWithoutIndex7 = verifyTree checkArrayWithoutIndex "a=(a b); a+=c"
+prop_checkArrayWithoutIndex8 = verifyTree checkArrayWithoutIndex "declare -a foo; foo=bar;"
+checkArrayWithoutIndex params _ =
+    doVariableFlowAnalysis readF writeF defaultMap (variableFlow params)
+  where
+    defaultMap = Map.fromList $ map (\x -> (x,())) arrayVariables
+    readF _ (T_DollarBraced id token) _ = do
+        map <- get
+        return . maybeToList $ do
+            name <- getLiteralString token
+            assigned <- Map.lookup name map
+            return $ makeComment WarningC id 2128
+                    "Expanding an array without an index only gives the first element."
+    readF _ _ _ = return []
+
+    writeF _ (T_Assignment id mode name [] _) _ (DataString _) = do
+        isArray <- gets (isJust . Map.lookup name)
+        return $ if not isArray then [] else
+            case mode of
+                Assign -> [makeComment WarningC id 2178 "Variable was used as an array but is now assigned a string."]
+                Append -> [makeComment WarningC id 2179 "Use array+=(\"item\") to append items to an array."]
+
+    writeF _ t name (DataArray _) = do
+        modify (Map.insert name ())
+        return []
+    writeF _ expr name _ = do
+        if isIndexed expr
+          then modify (Map.insert name ())
+          else modify (Map.delete name)
+        return []
+
+    isIndexed expr =
+        case expr of
+            T_Assignment _ _ _ (_:_) _ -> True
+            _ -> False
+
+prop_checkStderrRedirect = verify checkStderrRedirect "test 2>&1 > cow"
+prop_checkStderrRedirect2 = verifyNot checkStderrRedirect "test > cow 2>&1"
+prop_checkStderrRedirect3 = verifyNot checkStderrRedirect "test 2>&1 > file | grep stderr"
+prop_checkStderrRedirect4 = verifyNot checkStderrRedirect "errors=$(test 2>&1 > file)"
+prop_checkStderrRedirect5 = verifyNot checkStderrRedirect "read < <(test 2>&1 > file)"
+prop_checkStderrRedirect6 = verify checkStderrRedirect "foo | bar 2>&1 > /dev/null"
+prop_checkStderrRedirect7 = verifyNot checkStderrRedirect "{ cmd > file; } 2>&1"
+checkStderrRedirect params redir@(T_Redirecting _ [
+    T_FdRedirect id "2" (T_IoDuplicate _ (T_GREATAND _) "1"),
+    T_FdRedirect _ _ (T_IoFile _ op _)
+    ] _) = case op of
+            T_Greater _ -> error
+            T_DGREAT _ -> error
+            _ -> return ()
+  where
+    usesOutput t =
+        case t of
+            (T_Pipeline _ _ list) -> length list > 1 && not (isParentOf (parentMap params) (last list) redir)
+            T_ProcSub {} -> True
+            T_DollarExpansion {} -> True
+            T_Backticked {} -> True
+            _ -> False
+    isCaptured = any usesOutput $ getPath (parentMap params) redir
+
+    error = unless isCaptured $
+        warn id 2069 "To redirect stdout+stderr, 2>&1 must be last (or use '{ cmd > file; } 2>&1' to clarify)."
+
+checkStderrRedirect _ _ = return ()
+
+lt x = trace ("Tracing " ++ show x) x
+ltt t = trace ("Tracing " ++ show t)
+
+
+prop_checkSingleQuotedVariables  = verify checkSingleQuotedVariables "echo '$foo'"
+prop_checkSingleQuotedVariables2 = verify checkSingleQuotedVariables "echo 'lol$1.jpg'"
+prop_checkSingleQuotedVariables3 = verifyNot checkSingleQuotedVariables "sed 's/foo$/bar/'"
+prop_checkSingleQuotedVariables3a= verify checkSingleQuotedVariables "sed 's/${foo}/bar/'"
+prop_checkSingleQuotedVariables3b= verify checkSingleQuotedVariables "sed 's/$(echo cow)/bar/'"
+prop_checkSingleQuotedVariables3c= verify checkSingleQuotedVariables "sed 's/$((1+foo))/bar/'"
+prop_checkSingleQuotedVariables4 = verifyNot checkSingleQuotedVariables "awk '{print $1}'"
+prop_checkSingleQuotedVariables5 = verifyNot checkSingleQuotedVariables "trap 'echo $SECONDS' EXIT"
+prop_checkSingleQuotedVariables6 = verifyNot checkSingleQuotedVariables "sed -n '$p'"
+prop_checkSingleQuotedVariables6a= verify checkSingleQuotedVariables "sed -n '$pattern'"
+prop_checkSingleQuotedVariables7 = verifyNot checkSingleQuotedVariables "PS1='$PWD \\$ '"
+prop_checkSingleQuotedVariables8 = verify checkSingleQuotedVariables "find . -exec echo '$1' {} +"
+prop_checkSingleQuotedVariables9 = verifyNot checkSingleQuotedVariables "find . -exec awk '{print $1}' {} \\;"
+prop_checkSingleQuotedVariables10= verify checkSingleQuotedVariables "echo '`pwd`'"
+prop_checkSingleQuotedVariables11= verifyNot checkSingleQuotedVariables "sed '${/lol/d}'"
+prop_checkSingleQuotedVariables12= verifyNot checkSingleQuotedVariables "eval 'echo $1'"
+prop_checkSingleQuotedVariables13= verifyNot checkSingleQuotedVariables "busybox awk '{print $1}'"
+prop_checkSingleQuotedVariables14= verifyNot checkSingleQuotedVariables "[ -v 'bar[$foo]' ]"
+prop_checkSingleQuotedVariables15= verifyNot checkSingleQuotedVariables "git filter-branch 'test $GIT_COMMIT'"
+prop_checkSingleQuotedVariables16= verify checkSingleQuotedVariables "git '$a'"
+prop_checkSingleQuotedVariables17= verifyNot checkSingleQuotedVariables "rename 's/(.)a/$1/g' *"
+
+checkSingleQuotedVariables params t@(T_SingleQuoted id s) =
+    when (s `matches` re) $
+        if "sed" == commandName
+        then unless (s `matches` sedContra) showMessage
+        else unless isProbablyOk showMessage
+  where
+    parents = parentMap params
+    showMessage = info id 2016
+        "Expressions don't expand in single quotes, use double quotes for that."
+    commandName = fromMaybe "" $ do
+        cmd <- getClosestCommand parents t
+        name <- getCommandBasename cmd
+        return $ if name == "find" then getFindCommand cmd else if name == "git" then getGitCommand cmd else name
+
+    isProbablyOk =
+            any isOkAssignment (take 3 $ getPath parents t)
+            || commandName `elem` [
+                "trap"
+                ,"sh"
+                ,"bash"
+                ,"ksh"
+                ,"zsh"
+                ,"ssh"
+                ,"eval"
+                ,"xprop"
+                ,"alias"
+                ,"sudo" -- covering "sudo sh" and such
+                ,"docker" -- like above
+                ,"dpkg-query"
+                ,"jq"  -- could also check that user provides --arg
+                ,"rename"
+                ,"unset"
+                ,"git filter-branch"
+                ]
+            || "awk" `isSuffixOf` commandName
+            || "perl" `isPrefixOf` commandName
+
+    commonlyQuoted = ["PS1", "PS2", "PS3", "PS4", "PROMPT_COMMAND"]
+    isOkAssignment t =
+        case t of
+            T_Assignment _ _ name _ _ -> name `elem` commonlyQuoted
+            TC_Unary _ _ "-v" _ -> True
+            _ -> False
+
+    re = mkRegex "\\$[{(0-9a-zA-Z_]|`.*`"
+    sedContra = mkRegex "\\$[{dpsaic]($|[^a-zA-Z])"
+
+    getFindCommand (T_SimpleCommand _ _ words) =
+        let list = map getLiteralString words
+            cmd  = dropWhile (\x -> x /= Just "-exec" && x /= Just "-execdir") list
+        in
+          case cmd of
+            (flag:cmd:rest) -> fromMaybe "find" cmd
+            _ -> "find"
+    getFindCommand (T_Redirecting _ _ cmd) = getFindCommand cmd
+    getFindCommand _ = "find"
+    getGitCommand (T_SimpleCommand _ _ words) =
+        case map getLiteralString words of
+            Just "git":Just "filter-branch":_ -> "git filter-branch"
+            _ -> "git"
+    getGitCommand (T_Redirecting _ _ cmd) = getGitCommand cmd
+    getGitCommand _ = "git"
+checkSingleQuotedVariables _ _ = return ()
+
+
+prop_checkUnquotedN = verify checkUnquotedN "if [ -n $foo ]; then echo cow; fi"
+prop_checkUnquotedN2 = verify checkUnquotedN "[ -n $cow ]"
+prop_checkUnquotedN3 = verifyNot checkUnquotedN "[[ -n $foo ]] && echo cow"
+prop_checkUnquotedN4 = verify checkUnquotedN "[ -n $cow -o -t 1 ]"
+prop_checkUnquotedN5 = verifyNot checkUnquotedN "[ -n \"$@\" ]"
+checkUnquotedN _ (TC_Unary _ SingleBracket "-n" (T_NormalWord id [t])) | willSplit t =
+       err id 2070 "-n doesn't work with unquoted arguments. Quote or use [[ ]]."
+checkUnquotedN _ _ = return ()
+
+prop_checkNumberComparisons1 = verify checkNumberComparisons "[[ $foo < 3 ]]"
+prop_checkNumberComparisons2 = verify checkNumberComparisons "[[ 0 >= $(cmd) ]]"
+prop_checkNumberComparisons3 = verifyNot checkNumberComparisons "[[ $foo ]] > 3"
+prop_checkNumberComparisons4 = verify checkNumberComparisons "[[ $foo > 2.72 ]]"
+prop_checkNumberComparisons5 = verify checkNumberComparisons "[[ $foo -le 2.72 ]]"
+prop_checkNumberComparisons6 = verify checkNumberComparisons "[[ 3.14 -eq $foo ]]"
+prop_checkNumberComparisons7 = verifyNot checkNumberComparisons "[[ 3.14 == $foo ]]"
+prop_checkNumberComparisons8 = verify checkNumberComparisons "[ foo <= bar ]"
+prop_checkNumberComparisons9 = verify checkNumberComparisons "[ foo \\>= bar ]"
+prop_checkNumberComparisons11 = verify checkNumberComparisons "[ $foo -eq 'N' ]"
+prop_checkNumberComparisons12 = verify checkNumberComparisons "[ x$foo -gt x${N} ]"
+prop_checkNumberComparisons13 = verify checkNumberComparisons "[ $foo > $bar ]"
+prop_checkNumberComparisons14 = verifyNot checkNumberComparisons "[[ foo < bar ]]"
+prop_checkNumberComparisons15 = verifyNot checkNumberComparisons "[ $foo '>' $bar ]"
+checkNumberComparisons params (TC_Binary id typ op lhs rhs) = do
+    if isNum lhs || isNum rhs
+      then do
+        when (isLtGt op) $
+          err id 2071 $
+            op ++ " is for string comparisons. Use " ++ eqv op ++ " instead."
+        when (isLeGe op && hasStringComparison) $
+            err id 2071 $ op ++ " is not a valid operator. " ++
+              "Use " ++ eqv op ++ " ."
+      else do
+        when (isLeGe op || isLtGt op) $
+            mapM_ checkDecimals [lhs, rhs]
+
+        when (isLeGe op && hasStringComparison) $
+            err id 2122 $ op ++ " is not a valid operator. " ++
+                "Use '! a " ++ esc ++ invert op ++ " b' instead."
+
+        when (typ == SingleBracket && op `elem` ["<", ">"]) $
+            case shellType params of
+                Sh -> return ()  -- These are unsupported and will be caught by bashism checks.
+                Dash -> err id 2073 $ "Escape \\" ++ op ++ " to prevent it redirecting."
+                _ -> err id 2073 $ "Escape \\" ++ op ++ " to prevent it redirecting (or switch to [[ .. ]])."
+
+    when (op `elem` ["-lt", "-gt", "-le", "-ge", "-eq"]) $ do
+        mapM_ checkDecimals [lhs, rhs]
+        when (typ == SingleBracket) $
+            checkStrings [lhs, rhs]
+
+  where
+      hasStringComparison = shellType params /= Sh
+      isLtGt = flip elem ["<", "\\<", ">", "\\>"]
+      isLeGe = flip elem ["<=", "\\<=", ">=", "\\>="]
+
+      checkDecimals hs =
+        when (isFraction hs && not (hasFloatingPoint params)) $
+            err (getId hs) 2072 decimalError
+      decimalError = "Decimals are not supported. " ++
+        "Either use integers only, or use bc or awk to compare."
+
+      checkStrings =
+        mapM_ stringError . take 1 . filter isNonNum
+
+      isNonNum t = fromMaybe False $ do
+        s <- getLiteralStringExt (const $ return "") t
+        return . not . all numChar $ s
+      numChar x = isDigit x || x `elem` "+-. "
+
+      stringError t = err (getId t) 2170 $
+          "Numerical " ++ op ++ " does not dereference in [..]. Expand or use string operator."
+
+      isNum t =
+        case oversimplify t of
+            [v] -> all isDigit v
+            _ -> False
+      isFraction t =
+        case oversimplify t of
+            [v] -> isJust $ matchRegex floatRegex v
+            _ -> False
+
+      eqv ('\\':s) = eqv s
+      eqv "<" = "-lt"
+      eqv ">" = "-gt"
+      eqv "<=" = "-le"
+      eqv ">=" = "-ge"
+      eqv _ = "the numerical equivalent"
+
+      esc = if typ == SingleBracket then "\\" else ""
+      seqv "-ge" = "! a " ++ esc ++ "< b"
+      seqv "-gt" = esc ++ ">"
+      seqv "-le" = "! a " ++ esc ++ "> b"
+      seqv "-lt" = esc ++ "<"
+      seqv "-eq" = "="
+      seqv "-ne" = "!="
+      seqv _ = "the string equivalent"
+
+      invert ('\\':s) = invert s
+      invert "<=" = ">"
+      invert ">=" = "<"
+
+      floatRegex = mkRegex "^[-+]?[0-9]+\\.[0-9]+$"
+checkNumberComparisons _ _ = return ()
+
+prop_checkSingleBracketOperators1 = verify checkSingleBracketOperators "[ test =~ foo ]"
+checkSingleBracketOperators params (TC_Binary id SingleBracket "=~" lhs rhs) =
+    when (shellType params `elem` [Bash, Ksh]) $
+        err id 2074 $ "Can't use =~ in [ ]. Use [[..]] instead."
+checkSingleBracketOperators _ _ = return ()
+
+prop_checkDoubleBracketOperators1 = verify checkDoubleBracketOperators "[[ 3 \\< 4 ]]"
+prop_checkDoubleBracketOperators3 = verifyNot checkDoubleBracketOperators "[[ foo < bar ]]"
+checkDoubleBracketOperators _ x@(TC_Binary id typ op lhs rhs)
+    | typ == DoubleBracket && op `elem` ["\\<", "\\>"] =
+        err id 2075 $ "Escaping " ++ op ++" is required in [..], but invalid in [[..]]"
+checkDoubleBracketOperators _ _ = return ()
+
+prop_checkConditionalAndOrs1 = verify checkConditionalAndOrs "[ foo && bar ]"
+prop_checkConditionalAndOrs2 = verify checkConditionalAndOrs "[[ foo -o bar ]]"
+prop_checkConditionalAndOrs3 = verifyNot checkConditionalAndOrs "[[ foo || bar ]]"
+prop_checkConditionalAndOrs4 = verify checkConditionalAndOrs "[ foo -a bar ]"
+prop_checkConditionalAndOrs5 = verify checkConditionalAndOrs "[ -z 3 -o a = b ]"
+checkConditionalAndOrs _ t =
+    case t of
+        (TC_And id SingleBracket "&&" _ _) ->
+            err id 2107 "Instead of [ a && b ], use [ a ] && [ b ]."
+        (TC_And id DoubleBracket "-a" _ _) ->
+            err id 2108 "In [[..]], use && instead of -a."
+        (TC_Or id SingleBracket "||" _ _) ->
+            err id 2109 "Instead of [ a || b ], use [ a ] || [ b ]."
+        (TC_Or id DoubleBracket "-o" _ _) ->
+            err id 2110 "In [[..]], use || instead of -o."
+
+        (TC_And id SingleBracket "-a" _ _) ->
+            warn id 2166 "Prefer [ p ] && [ q ] as [ p -a q ] is not well defined."
+        (TC_Or id SingleBracket "-o" _ _) ->
+            warn id 2166 "Prefer [ p ] || [ q ] as [ p -o q ] is not well defined."
+
+        _ -> return ()
+
+prop_checkQuotedCondRegex1 = verify checkQuotedCondRegex "[[ $foo =~ \"bar.*\" ]]"
+prop_checkQuotedCondRegex2 = verify checkQuotedCondRegex "[[ $foo =~ '(cow|bar)' ]]"
+prop_checkQuotedCondRegex3 = verifyNot checkQuotedCondRegex "[[ $foo =~ $foo ]]"
+prop_checkQuotedCondRegex4 = verifyNot checkQuotedCondRegex "[[ $foo =~ \"bar\" ]]"
+prop_checkQuotedCondRegex5 = verifyNot checkQuotedCondRegex "[[ $foo =~ 'cow bar' ]]"
+prop_checkQuotedCondRegex6 = verify checkQuotedCondRegex "[[ $foo =~ 'cow|bar' ]]"
+checkQuotedCondRegex _ (TC_Binary _ _ "=~" _ rhs) =
+    case rhs of
+        T_NormalWord id [T_DoubleQuoted _ _] -> error rhs
+        T_NormalWord id [T_SingleQuoted _ _] -> error rhs
+        _ -> return ()
+  where
+    error t =
+        unless (isConstantNonRe t) $
+            err (getId t) 2076
+                "Don't quote rhs of =~, it'll match literally rather than as a regex."
+    re = mkRegex "[][*.+()|]"
+    hasMetachars s = s `matches` re
+    isConstantNonRe t = fromMaybe False $ do
+        s <- getLiteralString t
+        return . not $ hasMetachars s
+checkQuotedCondRegex _ _ = return ()
+
+prop_checkGlobbedRegex1 = verify checkGlobbedRegex "[[ $foo =~ *foo* ]]"
+prop_checkGlobbedRegex2 = verify checkGlobbedRegex "[[ $foo =~ f* ]]"
+prop_checkGlobbedRegex2a = verify checkGlobbedRegex "[[ $foo =~ \\#* ]]"
+prop_checkGlobbedRegex3 = verifyNot checkGlobbedRegex "[[ $foo =~ $foo ]]"
+prop_checkGlobbedRegex4 = verifyNot checkGlobbedRegex "[[ $foo =~ ^c.* ]]"
+checkGlobbedRegex _ (TC_Binary _ DoubleBracket "=~" _ rhs) =
+    let s = concat $ oversimplify rhs in
+        when (isConfusedGlobRegex s) $
+            warn (getId rhs) 2049 "=~ is for regex. Use == for globs."
+checkGlobbedRegex _ _ = return ()
+
+
+prop_checkConstantIfs1 = verify checkConstantIfs "[[ foo != bar ]]"
+prop_checkConstantIfs2a= verify checkConstantIfs "[ n -le 4 ]"
+prop_checkConstantIfs2b= verifyNot checkConstantIfs "[[ n -le 4 ]]"
+prop_checkConstantIfs3 = verify checkConstantIfs "[[ $n -le 4 && n != 2 ]]"
+prop_checkConstantIfs4 = verifyNot checkConstantIfs "[[ $n -le 3 ]]"
+prop_checkConstantIfs5 = verifyNot checkConstantIfs "[[ $n -le $n ]]"
+prop_checkConstantIfs6 = verifyNot checkConstantIfs "[[ a -ot b ]]"
+prop_checkConstantIfs7 = verifyNot checkConstantIfs "[ a -nt b ]"
+prop_checkConstantIfs8 = verifyNot checkConstantIfs "[[ ~foo == '~foo' ]]"
+prop_checkConstantIfs9 = verify checkConstantIfs "[[ *.png == [a-z] ]]"
+checkConstantIfs _ (TC_Binary id typ op lhs rhs) | not isDynamic =
+    if isConstant lhs && isConstant rhs
+        then  warn id 2050 "This expression is constant. Did you forget the $ on a variable?"
+        else checkUnmatchable id op lhs rhs
+  where
+    isDynamic =
+        op `elem` [ "-lt", "-gt", "-le", "-ge", "-eq", "-ne" ]
+            && typ == DoubleBracket
+        || op `elem` [ "-nt", "-ot", "-ef"]
+
+    checkUnmatchable id op lhs rhs =
+        when (op `elem` ["=", "==", "!="] && not (wordsCanBeEqual lhs rhs)) $
+            warn id 2193 "The arguments to this comparison can never be equal. Make sure your syntax is correct."
+checkConstantIfs _ _ = return ()
+
+prop_checkLiteralBreakingTest = verify checkLiteralBreakingTest "[[ a==$foo ]]"
+prop_checkLiteralBreakingTest2 = verify checkLiteralBreakingTest "[ $foo=3 ]"
+prop_checkLiteralBreakingTest3 = verify checkLiteralBreakingTest "[ $foo!=3 ]"
+prop_checkLiteralBreakingTest4 = verify checkLiteralBreakingTest "[ \"$(ls) \" ]"
+prop_checkLiteralBreakingTest5 = verify checkLiteralBreakingTest "[ -n \"$(true) \" ]"
+prop_checkLiteralBreakingTest6 = verify checkLiteralBreakingTest "[ -z $(true)z ]"
+prop_checkLiteralBreakingTest7 = verifyNot checkLiteralBreakingTest "[ -z $(true) ]"
+prop_checkLiteralBreakingTest8 = verifyNot checkLiteralBreakingTest "[ $(true)$(true) ]"
+prop_checkLiteralBreakingTest10 = verify checkLiteralBreakingTest "[ -z foo ]"
+checkLiteralBreakingTest _ t = potentially $
+        case t of
+            (TC_Nullary _ _ w@(T_NormalWord _ l)) -> do
+                guard . not $ isConstant w -- Covered by SC2078
+                comparisonWarning l `mplus` tautologyWarning w "Argument to implicit -n is always true due to literal strings."
+            (TC_Unary _ _ op w@(T_NormalWord _ l)) ->
+                case op of
+                    "-n" -> tautologyWarning w "Argument to -n is always true due to literal strings."
+                    "-z" -> tautologyWarning w "Argument to -z is always false due to literal strings."
+                    _ -> fail "not relevant"
+            _ -> fail "not my problem"
+  where
+    hasEquals = matchToken ('=' `elem`)
+    isNonEmpty = matchToken (not . null)
+    matchToken m t = isJust $ do
+        str <- getLiteralString t
+        guard $ m str
+        return ()
+
+    comparisonWarning list = do
+        token <- listToMaybe $ filter hasEquals list
+        return $ err (getId token) 2077 "You need spaces around the comparison operator."
+    tautologyWarning t s = do
+        token <- listToMaybe $ filter isNonEmpty $ getWordParts t
+        return $ err (getId token) 2157 s
+
+prop_checkConstantNullary = verify checkConstantNullary "[[ '$(foo)' ]]"
+prop_checkConstantNullary2 = verify checkConstantNullary "[ \"-f lol\" ]"
+prop_checkConstantNullary3 = verify checkConstantNullary "[[ cmd ]]"
+prop_checkConstantNullary4 = verify checkConstantNullary "[[ ! cmd ]]"
+prop_checkConstantNullary5 = verify checkConstantNullary "[[ true ]]"
+prop_checkConstantNullary6 = verify checkConstantNullary "[ 1 ]"
+prop_checkConstantNullary7 = verify checkConstantNullary "[ false ]"
+checkConstantNullary _ (TC_Nullary _ _ t) | isConstant t =
+    case fromMaybe "" $ getLiteralString t of
+        "false" -> err (getId t) 2158 "[ false ] is true. Remove the brackets."
+        "0" -> err (getId t) 2159 "[ 0 ] is true. Use 'false' instead."
+        "true" -> style (getId t) 2160 "Instead of '[ true ]', just use 'true'."
+        "1" -> style (getId t) 2161 "Instead of '[ 1 ]', use 'true'."
+        _ -> err (getId t) 2078 "This expression is constant. Did you forget a $ somewhere?"
+  where
+    string = fromMaybe "" $ getLiteralString t
+
+checkConstantNullary _ _ = return ()
+
+prop_checkForDecimals1 = verify checkForDecimals "((3.14*c))"
+prop_checkForDecimals2 = verify checkForDecimals "foo[1.2]=bar"
+prop_checkForDecimals3 = verifyNot checkForDecimals "declare -A foo; foo[1.2]=bar"
+checkForDecimals params t@(TA_Expansion id _) = potentially $ do
+    guard $ not (hasFloatingPoint params)
+    str <- getLiteralString t
+    first <- str !!! 0
+    guard $ isDigit first && '.' `elem` str
+    return $ err id 2079 "(( )) doesn't support decimals. Use bc or awk."
+checkForDecimals _ _ = return ()
+
+prop_checkDivBeforeMult = verify checkDivBeforeMult "echo $((c/n*100))"
+prop_checkDivBeforeMult2 = verifyNot checkDivBeforeMult "echo $((c*100/n))"
+prop_checkDivBeforeMult3 = verifyNot checkDivBeforeMult "echo $((c/10*10))"
+checkDivBeforeMult params (TA_Binary _ "*" (TA_Binary id "/" _ x) y)
+    | not (hasFloatingPoint params) && x /= y =
+        info id 2017 "Increase precision by replacing a/b*c with a*c/b."
+checkDivBeforeMult _ _ = return ()
+
+prop_checkArithmeticDeref = verify checkArithmeticDeref "echo $((3+$foo))"
+prop_checkArithmeticDeref2 = verify checkArithmeticDeref "cow=14; (( s+= $cow ))"
+prop_checkArithmeticDeref3 = verifyNot checkArithmeticDeref "cow=1/40; (( s+= ${cow%%/*} ))"
+prop_checkArithmeticDeref4 = verifyNot checkArithmeticDeref "(( ! $? ))"
+prop_checkArithmeticDeref5 = verifyNot checkArithmeticDeref "(($1))"
+prop_checkArithmeticDeref6 = verify checkArithmeticDeref "(( a[$i] ))"
+prop_checkArithmeticDeref7 = verifyNot checkArithmeticDeref "(( 10#$n ))"
+prop_checkArithmeticDeref8 = verifyNot checkArithmeticDeref "let i=$i+1"
+prop_checkArithmeticDeref9 = verifyNot checkArithmeticDeref "(( a[foo] ))"
+prop_checkArithmeticDeref10= verifyNot checkArithmeticDeref "(( a[\\$foo] ))"
+prop_checkArithmeticDeref11= verifyNot checkArithmeticDeref "a[$foo]=wee"
+prop_checkArithmeticDeref12= verify checkArithmeticDeref "for ((i=0; $i < 3; i)); do true; done"
+prop_checkArithmeticDeref13= verifyNot checkArithmeticDeref "(( $$ ))"
+prop_checkArithmeticDeref14= verifyNot checkArithmeticDeref "(( $! ))"
+prop_checkArithmeticDeref15= verifyNot checkArithmeticDeref "(( ${!var} ))"
+checkArithmeticDeref params t@(TA_Expansion _ [b@(T_DollarBraced id _)]) =
+    unless (isException $ bracedString b) getWarning
+  where
+    isException [] = True
+    isException s = any (`elem` "/.:#%?*@$-!") s || isDigit (head s)
+    getWarning = fromMaybe noWarning . msum . map warningFor $ parents params t
+    warningFor t =
+        case t of
+            T_Arithmetic {} -> return normalWarning
+            T_DollarArithmetic {} -> return normalWarning
+            T_ForArithmetic {} -> return normalWarning
+            T_SimpleCommand {} -> return noWarning
+            _ -> Nothing
+
+    normalWarning = style id 2004 "$/${} is unnecessary on arithmetic variables."
+    noWarning = return ()
+checkArithmeticDeref _ _ = return ()
+
+prop_checkArithmeticBadOctal1 = verify checkArithmeticBadOctal "(( 0192 ))"
+prop_checkArithmeticBadOctal2 = verifyNot checkArithmeticBadOctal "(( 0x192 ))"
+prop_checkArithmeticBadOctal3 = verifyNot checkArithmeticBadOctal "(( 1 ^ 0777 ))"
+checkArithmeticBadOctal _ t@(TA_Expansion id _) = potentially $ do
+    str <- getLiteralString t
+    guard $ str `matches` octalRE
+    return $ err id 2080 "Numbers with leading 0 are considered octal."
+  where
+    octalRE = mkRegex "^0[0-7]*[8-9]"
+checkArithmeticBadOctal _ _ = return ()
+
+prop_checkComparisonAgainstGlob = verify checkComparisonAgainstGlob "[[ $cow == $bar ]]"
+prop_checkComparisonAgainstGlob2 = verifyNot checkComparisonAgainstGlob "[[ $cow == \"$bar\" ]]"
+prop_checkComparisonAgainstGlob3 = verify checkComparisonAgainstGlob "[ $cow = *foo* ]"
+prop_checkComparisonAgainstGlob4 = verifyNot checkComparisonAgainstGlob "[ $cow = foo ]"
+prop_checkComparisonAgainstGlob5 = verify checkComparisonAgainstGlob "[[ $cow != $bar ]]"
+checkComparisonAgainstGlob _ (TC_Binary _ DoubleBracket op _ (T_NormalWord id [T_DollarBraced _ _]))
+    | op `elem` ["=", "==", "!="] =
+        warn id 2053 $ "Quote the rhs of " ++ op ++ " in [[ ]] to prevent glob matching."
+checkComparisonAgainstGlob _ (TC_Binary _ SingleBracket op _ word)
+        | (op == "=" || op == "==") && isGlob word =
+    err (getId word) 2081 "[ .. ] can't match globs. Use [[ .. ]] or case statement."
+checkComparisonAgainstGlob _ _ = return ()
+
+prop_checkCommarrays1 = verify checkCommarrays "a=(1, 2)"
+prop_checkCommarrays2 = verify checkCommarrays "a+=(1,2,3)"
+prop_checkCommarrays3 = verifyNot checkCommarrays "cow=(1 \"foo,bar\" 3)"
+prop_checkCommarrays4 = verifyNot checkCommarrays "cow=('one,' 'two')"
+prop_checkCommarrays5 = verify checkCommarrays "a=([a]=b, [c]=d)"
+prop_checkCommarrays6 = verify checkCommarrays "a=([a]=b,[c]=d,[e]=f)"
+checkCommarrays _ (T_Array id l) =
+    when (any (isCommaSeparated . literal) l) $
+        warn id 2054 "Use spaces, not commas, to separate array elements."
+  where
+    literal (T_IndexedElement _ _ l) = literal l
+    literal (T_NormalWord _ l) = concatMap literal l
+    literal (T_Literal _ str) = str
+    literal _ = "str"
+
+    isCommaSeparated str = "," `isSuffixOf` str || length (filter (== ',') str) > 1
+checkCommarrays _ _ = return ()
+
+prop_checkOrNeq1 = verify checkOrNeq "if [[ $lol -ne cow || $lol -ne foo ]]; then echo foo; fi"
+prop_checkOrNeq2 = verify checkOrNeq "(( a!=lol || a!=foo ))"
+prop_checkOrNeq3 = verify checkOrNeq "[ \"$a\" != lol || \"$a\" != foo ]"
+prop_checkOrNeq4 = verifyNot checkOrNeq "[ a != $cow || b != $foo ]"
+prop_checkOrNeq5 = verifyNot checkOrNeq "[[ $a != /home || $a != */public_html/* ]]"
+-- This only catches the most idiomatic cases. Fixme?
+checkOrNeq _ (TC_Or id typ op (TC_Binary _ _ op1 lhs1 rhs1 ) (TC_Binary _ _ op2 lhs2 rhs2))
+    | lhs1 == lhs2 && (op1 == op2 && (op1 == "-ne" || op1 == "!=")) && not (any isGlob [rhs1,rhs2]) =
+        warn id 2055 $ "You probably wanted " ++ (if typ == SingleBracket then "-a" else "&&") ++ " here."
+
+checkOrNeq _ (TA_Binary id "||" (TA_Binary _ "!=" word1 _) (TA_Binary _ "!=" word2 _))
+    | word1 == word2 =
+        warn id 2056 "You probably wanted && here."
+checkOrNeq _ _ = return ()
+
+
+prop_checkValidCondOps1 = verify checkValidCondOps "[[ a -xz b ]]"
+prop_checkValidCondOps2 = verify checkValidCondOps "[ -M a ]"
+prop_checkValidCondOps2a= verifyNot checkValidCondOps "[ 3 \\> 2 ]"
+prop_checkValidCondOps3 = verifyNot checkValidCondOps "[ 1 = 2 -a 3 -ge 4 ]"
+prop_checkValidCondOps4 = verifyNot checkValidCondOps "[[ ! -v foo ]]"
+checkValidCondOps _ (TC_Binary id _ s _ _)
+    | s `notElem` binaryTestOps =
+        warn id 2057 "Unknown binary operator."
+checkValidCondOps _ (TC_Unary id _ s _)
+    | s `notElem`  unaryTestOps =
+        warn id 2058 "Unknown unary operator."
+checkValidCondOps _ _ = return ()
+
+prop_checkUuoeVar1 = verify checkUuoeVar "for f in $(echo $tmp); do echo lol; done"
+prop_checkUuoeVar2 = verify checkUuoeVar "date +`echo \"$format\"`"
+prop_checkUuoeVar3 = verifyNot checkUuoeVar "foo \"$(echo -e '\r')\""
+prop_checkUuoeVar4 = verifyNot checkUuoeVar "echo $tmp"
+prop_checkUuoeVar5 = verify checkUuoeVar "foo \"$(echo \"$(date) value:\" $value)\""
+prop_checkUuoeVar6 = verifyNot checkUuoeVar "foo \"$(echo files: *.png)\""
+prop_checkUuoeVar7 = verifyNot checkUuoeVar "foo $(echo $(bar))" -- covered by 2005
+prop_checkUuoeVar8 = verifyNot checkUuoeVar "#!/bin/sh\nz=$(echo)"
+prop_checkUuoeVar9 = verify checkUuoeVar "foo $(echo $(<file))"
+checkUuoeVar _ p =
+    case p of
+        T_Backticked id [cmd] -> check id cmd
+        T_DollarExpansion id [cmd] -> check id cmd
+        _ -> return ()
+  where
+    couldBeOptimized f = case f of
+        T_Glob {} -> False
+        T_Extglob {} -> False
+        T_BraceExpansion {} -> False
+        T_NormalWord _ l -> all couldBeOptimized l
+        T_DoubleQuoted _ l -> all couldBeOptimized l
+        _ -> True
+
+    check id (T_Pipeline _ _ [T_Redirecting _ _ c]) = warnForEcho id c
+    check _ _ = return ()
+    isCovered first rest = null rest && tokenIsJustCommandOutput first
+    warnForEcho id = checkUnqualifiedCommand "echo" $ \_ vars ->
+        case vars of
+          (first:rest) ->
+            unless (isCovered first rest || "-" `isPrefixOf` onlyLiteralString first) $
+                when (all couldBeOptimized vars) $ style id 2116
+                    "Useless echo? Instead of 'cmd $(echo foo)', just use 'cmd foo'."
+          _ -> return ()
+
+
+prop_checkTestRedirects1 = verify checkTestRedirects "test 3 > 1"
+prop_checkTestRedirects2 = verifyNot checkTestRedirects "test 3 \\> 1"
+prop_checkTestRedirects3 = verify checkTestRedirects "/usr/bin/test $var > $foo"
+prop_checkTestRedirects4 = verifyNot checkTestRedirects "test 1 -eq 2 2> file"
+checkTestRedirects _ (T_Redirecting id redirs cmd) | cmd `isCommand` "test" =
+    mapM_ check redirs
+  where
+    check t =
+        when (suspicious t) $
+            warn (getId t) 2065 "This is interpreted as a shell file redirection, not a comparison."
+    suspicious t = -- Ignore redirections of stderr because these are valid for squashing e.g. int errors,
+        case t of  -- and >> and similar redirections because these are probably not comparisons.
+            T_FdRedirect _ fd (T_IoFile _ op _) -> fd /= "2" && isComparison op
+            _ -> False
+    isComparison t =
+        case t of
+            T_Greater _ -> True
+            T_Less _ -> True
+            _ -> False
+checkTestRedirects _ _ = return ()
+
+prop_checkPS11 = verify checkPS1Assignments "PS1='\\033[1;35m\\$ '"
+prop_checkPS11a= verify checkPS1Assignments "export PS1='\\033[1;35m\\$ '"
+prop_checkPSf2 = verify checkPS1Assignments "PS1='\\h \\e[0m\\$ '"
+prop_checkPS13 = verify checkPS1Assignments "PS1=$'\\x1b[c '"
+prop_checkPS14 = verify checkPS1Assignments "PS1=$'\\e[3m; '"
+prop_checkPS14a= verify checkPS1Assignments "export PS1=$'\\e[3m; '"
+prop_checkPS15 = verifyNot checkPS1Assignments "PS1='\\[\\033[1;35m\\]\\$ '"
+prop_checkPS16 = verifyNot checkPS1Assignments "PS1='\\[\\e1m\\e[1m\\]\\$ '"
+prop_checkPS17 = verifyNot checkPS1Assignments "PS1='e033x1B'"
+prop_checkPS18 = verifyNot checkPS1Assignments "PS1='\\[\\e\\]'"
+checkPS1Assignments _ (T_Assignment _ _ "PS1" _ word) = warnFor word
+  where
+    warnFor word =
+        let contents = concat $ oversimplify word in
+            when (containsUnescaped contents) $
+                info (getId word) 2025 "Make sure all escape sequences are enclosed in \\[..\\] to prevent line wrapping issues"
+    containsUnescaped s =
+        let unenclosed = subRegex enclosedRegex s "" in
+           isJust $ matchRegex escapeRegex unenclosed
+    enclosedRegex = mkRegex "\\\\\\[.*\\\\\\]" -- FIXME: shouldn't be eager
+    escapeRegex = mkRegex "\\\\x1[Bb]|\\\\e|\x1B|\\\\033"
+checkPS1Assignments _ _ = return ()
+
+prop_checkBackticks1 = verify checkBackticks "echo `foo`"
+prop_checkBackticks2 = verifyNot checkBackticks "echo $(foo)"
+prop_checkBackticks3 = verifyNot checkBackticks "echo `#inlined comment` foo"
+checkBackticks _ (T_Backticked id list) | not (null list) =
+    style id 2006 "Use $(..) instead of legacy `..`."
+checkBackticks _ _ = return ()
+
+prop_checkIndirectExpansion1 = verify checkIndirectExpansion "${foo$n}"
+prop_checkIndirectExpansion2 = verifyNot checkIndirectExpansion "${foo//$n/lol}"
+prop_checkIndirectExpansion3 = verify checkIndirectExpansion "${$#}"
+prop_checkIndirectExpansion4 = verify checkIndirectExpansion "${var${n}_$((i%2))}"
+prop_checkIndirectExpansion5 = verifyNot checkIndirectExpansion "${bar}"
+checkIndirectExpansion _ (T_DollarBraced i (T_NormalWord _ contents)) =
+    when (isIndirection contents) $
+        err i 2082 "To expand via indirection, use arrays, ${!name} or (for sh only) eval."
+  where
+    isIndirection vars =
+        let list = mapMaybe isIndirectionPart vars in
+            not (null list) && and list
+    isIndirectionPart t =
+        case t of T_DollarExpansion _ _ ->  Just True
+                  T_Backticked _ _ ->       Just True
+                  T_DollarBraced _ _ ->     Just True
+                  T_DollarArithmetic _ _ -> Just True
+                  T_Literal _ s -> if all isVariableChar s
+                                    then Nothing
+                                    else Just False
+                  _ -> Just False
+
+checkIndirectExpansion _ _ = return ()
+
+prop_checkInexplicablyUnquoted1 = verify checkInexplicablyUnquoted "echo 'var='value';'"
+prop_checkInexplicablyUnquoted2 = verifyNot checkInexplicablyUnquoted "'foo'*"
+prop_checkInexplicablyUnquoted3 = verifyNot checkInexplicablyUnquoted "wget --user-agent='something'"
+prop_checkInexplicablyUnquoted4 = verify checkInexplicablyUnquoted "echo \"VALUES (\"id\")\""
+prop_checkInexplicablyUnquoted5 = verifyNot checkInexplicablyUnquoted "\"$dir\"/\"$file\""
+prop_checkInexplicablyUnquoted6 = verifyNot checkInexplicablyUnquoted "\"$dir\"some_stuff\"$file\""
+prop_checkInexplicablyUnquoted7 = verifyNot checkInexplicablyUnquoted "${dir/\"foo\"/\"bar\"}"
+prop_checkInexplicablyUnquoted8 = verifyNot checkInexplicablyUnquoted "  'foo'\\\n  'bar'"
+checkInexplicablyUnquoted _ (T_NormalWord id tokens) = mapM_ check (tails tokens)
+  where
+    check (T_SingleQuoted _ _:T_Literal id str:_)
+        | not (null str) && all isAlphaNum str =
+        info id 2026 "This word is outside of quotes. Did you intend to 'nest '\"'single quotes'\"' instead'? "
+
+    check (T_DoubleQuoted _ a:trapped:T_DoubleQuoted _ b:_) =
+        case trapped of
+            T_DollarExpansion id _ -> warnAboutExpansion id
+            T_DollarBraced id _ -> warnAboutExpansion id
+            T_Literal id s ->
+                unless (quotesSingleThing a && quotesSingleThing b) $
+                    warnAboutLiteral id
+            _ -> return ()
+
+    check _ = return ()
+
+    -- If the surrounding quotes quote single things, like "$foo"_and_then_some_"$stuff",
+    -- the quotes were probably intentional and harmless.
+    quotesSingleThing x = case x of
+        [T_DollarExpansion _ _] -> True
+        [T_DollarBraced _ _] -> True
+        [T_Backticked _ _] -> True
+        _ -> False
+
+    warnAboutExpansion id =
+        warn id 2027 "The surrounding quotes actually unquote this. Remove or escape them."
+    warnAboutLiteral id =
+        warn id 2140 "Word is of the form \"A\"B\"C\" (B indicated). Did you mean \"ABC\" or \"A\\\"B\\\"C\"?"
+checkInexplicablyUnquoted _ _ = return ()
+
+prop_checkTildeInQuotes1 = verify checkTildeInQuotes "var=\"~/out.txt\""
+prop_checkTildeInQuotes2 = verify checkTildeInQuotes "foo > '~/dir'"
+prop_checkTildeInQuotes4 = verifyNot checkTildeInQuotes "~/file"
+prop_checkTildeInQuotes5 = verifyNot checkTildeInQuotes "echo '/~foo/cow'"
+prop_checkTildeInQuotes6 = verifyNot checkTildeInQuotes "awk '$0 ~ /foo/'"
+checkTildeInQuotes _ = check
+  where
+    verify id ('~':'/':_) = warn id 2088 "Tilde does not expand in quotes. Use $HOME."
+    verify _ _ = return ()
+    check (T_NormalWord _ (T_SingleQuoted id str:_)) =
+        verify id str
+    check (T_NormalWord _ (T_DoubleQuoted _ (T_Literal id str:_):_)) =
+        verify id str
+    check _ = return ()
+
+prop_checkLonelyDotDash1 = verify checkLonelyDotDash "./ file"
+prop_checkLonelyDotDash2 = verifyNot checkLonelyDotDash "./file"
+checkLonelyDotDash _ t@(T_Redirecting id _ _)
+    | isUnqualifiedCommand t "./" =
+        err id 2083 "Don't add spaces after the slash in './file'."
+checkLonelyDotDash _ _ = return ()
+
+
+prop_checkSpuriousExec1 = verify checkSpuriousExec "exec foo; true"
+prop_checkSpuriousExec2 = verify checkSpuriousExec "if a; then exec b; exec c; fi"
+prop_checkSpuriousExec3 = verifyNot checkSpuriousExec "echo cow; exec foo"
+prop_checkSpuriousExec4 = verifyNot checkSpuriousExec "if a; then exec b; fi"
+prop_checkSpuriousExec5 = verifyNot checkSpuriousExec "exec > file; cmd"
+prop_checkSpuriousExec6 = verify checkSpuriousExec "exec foo > file; cmd"
+prop_checkSpuriousExec7 = verifyNot checkSpuriousExec "exec file; echo failed; exit 3"
+prop_checkSpuriousExec8 = verifyNot checkSpuriousExec "exec {origout}>&1- >tmp.log 2>&1; bar"
+checkSpuriousExec _ = doLists
+  where
+    doLists (T_Script _ _ cmds) = doList cmds
+    doLists (T_BraceGroup _ cmds) = doList cmds
+    doLists (T_WhileExpression _ _ cmds) = doList cmds
+    doLists (T_UntilExpression _ _ cmds) = doList cmds
+    doLists (T_ForIn _ _ _ cmds) = doList cmds
+    doLists (T_ForArithmetic _ _ _ _ cmds) = doList cmds
+    doLists (T_IfExpression _ thens elses) = do
+        mapM_ (\(_, l) -> doList l) thens
+        doList elses
+    doLists _ = return ()
+
+    stripCleanup = reverse . dropWhile cleanup . reverse
+    cleanup (T_Pipeline _ _ [cmd]) =
+        isCommandMatch cmd (`elem` ["echo", "exit"])
+    cleanup _ = False
+
+    doList = doList' . stripCleanup
+    doList' t@(current:following:_) = do
+        commentIfExec current
+        doList (tail t)
+    doList' _ = return ()
+
+    commentIfExec (T_Pipeline id _ list) =
+      mapM_ commentIfExec $ take 1 list
+    commentIfExec (T_Redirecting _ _ f@(
+      T_SimpleCommand id _ (cmd:arg:_))) =
+        when (f `isUnqualifiedCommand` "exec") $
+          warn id 2093
+            "Remove \"exec \" if script should continue after this command."
+    commentIfExec _ = return ()
+
+
+prop_checkSpuriousExpansion1 = verify checkSpuriousExpansion "if $(true); then true; fi"
+prop_checkSpuriousExpansion2 = verify checkSpuriousExpansion "while \"$(cmd)\"; do :; done"
+prop_checkSpuriousExpansion3 = verifyNot checkSpuriousExpansion "$(cmd) --flag1 --flag2"
+prop_checkSpuriousExpansion4 = verify checkSpuriousExpansion "$((i++))"
+checkSpuriousExpansion _ (T_SimpleCommand _ _ [T_NormalWord _ [word]]) = check word
+  where
+    check word = case word of
+        T_DollarExpansion id _ ->
+            warn id 2091 "Remove surrounding $() to avoid executing output."
+        T_Backticked id _ ->
+            warn id 2092 "Remove backticks to avoid executing output."
+        T_DollarArithmetic id _ ->
+            err id 2084 "Remove '$' or use '_=$((expr))' to avoid executing output."
+        T_DoubleQuoted id [subword] -> check subword
+        _ -> return ()
+checkSpuriousExpansion _ _ = return ()
+
+
+prop_checkDollarBrackets1 = verify checkDollarBrackets "echo $[1+2]"
+prop_checkDollarBrackets2 = verifyNot checkDollarBrackets "echo $((1+2))"
+checkDollarBrackets _ (T_DollarBracket id _) =
+    style id 2007 "Use $((..)) instead of deprecated $[..]"
+checkDollarBrackets _ _ = return ()
+
+prop_checkSshHereDoc1 = verify checkSshHereDoc "ssh host << foo\necho $PATH\nfoo"
+prop_checkSshHereDoc2 = verifyNot checkSshHereDoc "ssh host << 'foo'\necho $PATH\nfoo"
+checkSshHereDoc _ (T_Redirecting _ redirs cmd)
+        | cmd `isCommand` "ssh" =
+    mapM_ checkHereDoc redirs
+  where
+    hasVariables = mkRegex "[`$]"
+    checkHereDoc (T_FdRedirect _ _ (T_HereDoc id _ Unquoted token tokens))
+        | not (all isConstant tokens) =
+        warn id 2087 $ "Quote '" ++ token ++ "' to make here document expansions happen on the server side rather than on the client."
+    checkHereDoc _ = return ()
+checkSshHereDoc _ _ = return ()
+
+--- Subshell detection
+prop_subshellAssignmentCheck = verifyTree     subshellAssignmentCheck "cat foo | while read bar; do a=$bar; done; echo \"$a\""
+prop_subshellAssignmentCheck2 = verifyNotTree subshellAssignmentCheck "while read bar; do a=$bar; done < file; echo \"$a\""
+prop_subshellAssignmentCheck3 = verifyTree    subshellAssignmentCheck "( A=foo; ); rm $A"
+prop_subshellAssignmentCheck4 = verifyNotTree subshellAssignmentCheck "( A=foo; rm $A; )"
+prop_subshellAssignmentCheck5 = verifyTree    subshellAssignmentCheck "cat foo | while read cow; do true; done; echo $cow;"
+prop_subshellAssignmentCheck6 = verifyTree    subshellAssignmentCheck "( export lol=$(ls); ); echo $lol;"
+prop_subshellAssignmentCheck6a= verifyTree    subshellAssignmentCheck "( typeset -a lol=a; ); echo $lol;"
+prop_subshellAssignmentCheck7 = verifyTree    subshellAssignmentCheck "cmd | while read foo; do (( n++ )); done; echo \"$n lines\""
+prop_subshellAssignmentCheck8 = verifyTree    subshellAssignmentCheck "n=3 & echo $((n++))"
+prop_subshellAssignmentCheck9 = verifyTree    subshellAssignmentCheck "read n & n=foo$n"
+prop_subshellAssignmentCheck10 = verifyTree    subshellAssignmentCheck "(( n <<= 3 )) & (( n |= 4 )) &"
+prop_subshellAssignmentCheck11 = verifyTree subshellAssignmentCheck "cat /etc/passwd | while read line; do let n=n+1; done\necho $n"
+prop_subshellAssignmentCheck12 = verifyTree subshellAssignmentCheck "cat /etc/passwd | while read line; do let ++n; done\necho $n"
+prop_subshellAssignmentCheck13 = verifyTree subshellAssignmentCheck "#!/bin/bash\necho foo | read bar; echo $bar"
+prop_subshellAssignmentCheck14 = verifyNotTree subshellAssignmentCheck "#!/bin/ksh93\necho foo | read bar; echo $bar"
+prop_subshellAssignmentCheck15 = verifyNotTree subshellAssignmentCheck "#!/bin/ksh\ncat foo | while read bar; do a=$bar; done\necho \"$a\""
+prop_subshellAssignmentCheck16 = verifyNotTree subshellAssignmentCheck "(set -e); echo $@"
+prop_subshellAssignmentCheck17 = verifyNotTree subshellAssignmentCheck "foo=${ { bar=$(baz); } 2>&1; }; echo $foo $bar"
+prop_subshellAssignmentCheck18 = verifyTree subshellAssignmentCheck "( exec {n}>&2; ); echo $n"
+prop_subshellAssignmentCheck19 = verifyNotTree subshellAssignmentCheck "#!/bin/bash\nshopt -s lastpipe; echo a | read -r b; echo \"$b\""
+subshellAssignmentCheck params t =
+    let flow = variableFlow params
+        check = findSubshelled flow [("oops",[])] Map.empty
+    in execWriter check
+
+
+findSubshelled [] _ _ = return ()
+findSubshelled (Assignment x@(_, _, str, _):rest) ((reason,scope):lol) deadVars =
+    findSubshelled rest ((reason, x:scope):lol) $ Map.insert str Alive deadVars
+findSubshelled (Reference (_, readToken, str):rest) scopes deadVars = do
+    unless (shouldIgnore str) $ case Map.findWithDefault Alive str deadVars of
+        Alive -> return ()
+        Dead writeToken reason -> do
+                    info (getId writeToken) 2030 $ "Modification of " ++ str ++ " is local (to subshell caused by "++ reason ++")."
+                    info (getId readToken) 2031 $ str ++ " was modified in a subshell. That change might be lost."
+    findSubshelled rest scopes deadVars
+  where
+    shouldIgnore str =
+        str `elem` ["@", "*", "IFS"]
+
+findSubshelled (StackScope (SubshellScope reason):rest) scopes deadVars =
+    findSubshelled rest ((reason,[]):scopes) deadVars
+
+findSubshelled (StackScopeEnd:rest) ((reason, scope):oldScopes) deadVars =
+    findSubshelled rest oldScopes $
+        foldl (\m (_, token, var, _) ->
+            Map.insert var (Dead token reason) m) deadVars scope
+
+
+-- FIXME: This is a very strange way of doing it.
+-- For each variable read/write, run a stateful function that emits
+-- comments. The comments are collected and returned.
+doVariableFlowAnalysis ::
+    (Token -> Token -> String -> State t [v])
+    -> (Token -> Token -> String -> DataType -> State t [v])
+    -> t
+    -> [StackData]
+    -> [v]
+
+doVariableFlowAnalysis readFunc writeFunc empty flow = evalState (
+    foldM (\list x -> do { l <- doFlow x;  return $ l ++ list; }) [] flow
+    ) empty
+  where
+    doFlow (Reference (base, token, name)) =
+        readFunc base token name
+    doFlow (Assignment (base, token, name, values)) =
+        writeFunc base token name values
+    doFlow _ = return []
+
+---- Check whether variables could have spaces/globs
+prop_checkSpacefulness1 = verifyTree checkSpacefulness "a='cow moo'; echo $a"
+prop_checkSpacefulness2 = verifyNotTree checkSpacefulness "a='cow moo'; [[ $a ]]"
+prop_checkSpacefulness3 = verifyNotTree checkSpacefulness "a='cow*.mp3'; echo \"$a\""
+prop_checkSpacefulness4 = verifyTree checkSpacefulness "for f in *.mp3; do echo $f; done"
+prop_checkSpacefulness4a= verifyNotTree checkSpacefulness "foo=3; foo=$(echo $foo)"
+prop_checkSpacefulness5 = verifyTree checkSpacefulness "a='*'; b=$a; c=lol${b//foo/bar}; echo $c"
+prop_checkSpacefulness6 = verifyTree checkSpacefulness "a=foo$(lol); echo $a"
+prop_checkSpacefulness7 = verifyTree checkSpacefulness "a=foo\\ bar; rm $a"
+prop_checkSpacefulness8 = verifyNotTree checkSpacefulness "a=foo\\ bar; a=foo; rm $a"
+prop_checkSpacefulness10= verifyTree checkSpacefulness "rm $1"
+prop_checkSpacefulness11= verifyTree checkSpacefulness "rm ${10//foo/bar}"
+prop_checkSpacefulness12= verifyNotTree checkSpacefulness "(( $1 + 3 ))"
+prop_checkSpacefulness13= verifyNotTree checkSpacefulness "if [[ $2 -gt 14 ]]; then true; fi"
+prop_checkSpacefulness14= verifyNotTree checkSpacefulness "foo=$3 env"
+prop_checkSpacefulness15= verifyNotTree checkSpacefulness "local foo=$1"
+prop_checkSpacefulness16= verifyNotTree checkSpacefulness "declare foo=$1"
+prop_checkSpacefulness17= verifyTree checkSpacefulness "echo foo=$1"
+prop_checkSpacefulness18= verifyNotTree checkSpacefulness "$1 --flags"
+prop_checkSpacefulness19= verifyTree checkSpacefulness "echo $PWD"
+prop_checkSpacefulness20= verifyNotTree checkSpacefulness "n+='foo bar'"
+prop_checkSpacefulness21= verifyNotTree checkSpacefulness "select foo in $bar; do true; done"
+prop_checkSpacefulness22= verifyNotTree checkSpacefulness "echo $\"$1\""
+prop_checkSpacefulness23= verifyNotTree checkSpacefulness "a=(1); echo ${a[@]}"
+prop_checkSpacefulness24= verifyTree checkSpacefulness "a='a    b'; cat <<< $a"
+prop_checkSpacefulness25= verifyTree checkSpacefulness "a='s/[0-9]//g'; sed $a"
+prop_checkSpacefulness26= verifyTree checkSpacefulness "a='foo bar'; echo {1,2,$a}"
+prop_checkSpacefulness27= verifyNotTree checkSpacefulness "echo ${a:+'foo'}"
+prop_checkSpacefulness28= verifyNotTree checkSpacefulness "exec {n}>&1; echo $n"
+prop_checkSpacefulness29= verifyNotTree checkSpacefulness "n=$(stuff); exec {n}>&-;"
+prop_checkSpacefulness30= verifyTree checkSpacefulness "file='foo bar'; echo foo > $file;"
+prop_checkSpacefulness31= verifyNotTree checkSpacefulness "echo \"`echo \\\"$1\\\"`\""
+prop_checkSpacefulness32= verifyNotTree checkSpacefulness "var=$1; [ -v var ]"
+prop_checkSpacefulness33= verifyTree checkSpacefulness "for file; do echo $file; done"
+prop_checkSpacefulness34= verifyTree checkSpacefulness "declare foo$n=$1"
+prop_checkSpacefulness35= verifyNotTree checkSpacefulness "echo ${1+\"$1\"}"
+
+checkSpacefulness params t =
+    doVariableFlowAnalysis readF writeF (Map.fromList defaults) (variableFlow params)
+  where
+    defaults = zip variablesWithoutSpaces (repeat False)
+
+    hasSpaces name = do
+        map <- get
+        return $ Map.findWithDefault True name map
+
+    setSpaces name bool =
+        modify $ Map.insert name bool
+
+    readF _ token name = do
+        spaces <- hasSpaces name
+        return [warning |
+                  isExpansion token && spaces
+                  && not (isArrayExpansion token) -- There's another warning for this
+                  && not (isCountingReference token)
+                  && not (isQuoteFree parents token)
+                  && not (isQuotedAlternativeReference token)
+                  && not (usedAsCommandName parents token)]
+      where
+        warning =
+            if isDefaultAssignment (parentMap params) token
+            then
+                makeComment InfoC (getId token) 2223
+                    "This default assignment may cause DoS due to globbing. Quote it."
+            else
+                makeComment InfoC (getId token) 2086
+                    "Double quote to prevent globbing and word splitting."
+
+    writeF _ _ name (DataString SourceExternal) = setSpaces name True >> return []
+    writeF _ _ name (DataString SourceInteger) = setSpaces name False >> return []
+
+    writeF _ _ name (DataString (SourceFrom vals)) = do
+        map <- get
+        setSpaces name
+            (isSpacefulWord (\x -> Map.findWithDefault True x map) vals)
+        return []
+
+    writeF _ _ _ _ = return []
+
+    parents = parentMap params
+
+    isExpansion t =
+        case t of
+            (T_DollarBraced _ _ ) -> True
+            _ -> False
+
+    isSpacefulWord :: (String -> Bool) -> [Token] -> Bool
+    isSpacefulWord f = any (isSpaceful f)
+    isSpaceful :: (String -> Bool) -> Token -> Bool
+    isSpaceful spacefulF x =
+        case x of
+          T_DollarExpansion _ _ -> True
+          T_Backticked _ _ -> True
+          T_Glob _ _         -> True
+          T_Extglob {}       -> True
+          T_Literal _ s      -> s `containsAny` globspace
+          T_SingleQuoted _ s -> s `containsAny` globspace
+          T_DollarBraced _ _ -> spacefulF $ getBracedReference $ bracedString x
+          T_NormalWord _ w   -> isSpacefulWord spacefulF w
+          T_DoubleQuoted _ w -> isSpacefulWord spacefulF w
+          _ -> False
+      where
+        globspace = "*?[] \t\n"
+        containsAny s = any (`elem` s)
+
+    isDefaultAssignment parents token =
+        let modifier = getBracedModifier $ bracedString token in
+            isExpansion token
+            && any (`isPrefixOf` modifier) ["=", ":="]
+            && isParamTo parents ":" token
+
+prop_checkQuotesInLiterals1 = verifyTree checkQuotesInLiterals "param='--foo=\"bar\"'; app $param"
+prop_checkQuotesInLiterals1a= verifyTree checkQuotesInLiterals "param=\"--foo='lolbar'\"; app $param"
+prop_checkQuotesInLiterals2 = verifyNotTree checkQuotesInLiterals "param='--foo=\"bar\"'; app \"$param\""
+prop_checkQuotesInLiterals3 =verifyNotTree checkQuotesInLiterals "param=('--foo='); app \"${param[@]}\""
+prop_checkQuotesInLiterals4 = verifyNotTree checkQuotesInLiterals "param=\"don't bother with this one\"; app $param"
+prop_checkQuotesInLiterals5 = verifyNotTree checkQuotesInLiterals "param=\"--foo='lolbar'\"; eval app $param"
+prop_checkQuotesInLiterals6 = verifyTree checkQuotesInLiterals "param='my\\ file'; cmd=\"rm $param\"; $cmd"
+prop_checkQuotesInLiterals6a= verifyNotTree checkQuotesInLiterals "param='my\\ file'; cmd=\"rm ${#param}\"; $cmd"
+prop_checkQuotesInLiterals7 = verifyTree checkQuotesInLiterals "param='my\\ file'; rm $param"
+prop_checkQuotesInLiterals8 = verifyTree checkQuotesInLiterals "param=\"/foo/'bar baz'/etc\"; rm $param"
+prop_checkQuotesInLiterals9 = verifyNotTree checkQuotesInLiterals "param=\"/foo/'bar baz'/etc\"; rm ${#param}"
+checkQuotesInLiterals params t =
+    doVariableFlowAnalysis readF writeF Map.empty (variableFlow params)
+  where
+    getQuotes name = fmap (Map.lookup name) get
+    setQuotes name ref = modify $ Map.insert name ref
+    deleteQuotes = modify . Map.delete
+    parents = parentMap params
+    quoteRegex = mkRegex "\"|([/= ]|^)'|'( |$)|\\\\ "
+    containsQuotes s = s `matches` quoteRegex
+
+    writeF _ _ name (DataString (SourceFrom values)) = do
+        quoteMap <- get
+        let quotedVars = msum $ map (forToken quoteMap) values
+        case quotedVars of
+            Nothing -> deleteQuotes name
+            Just x -> setQuotes name x
+        return []
+    writeF _ _ _ _ = return []
+
+    forToken map (T_DollarBraced id t) =
+        -- skip getBracedReference here to avoid false positives on PE
+        Map.lookup (concat . oversimplify $ t) map
+    forToken quoteMap (T_DoubleQuoted id tokens) =
+        msum $ map (forToken quoteMap) tokens
+    forToken quoteMap (T_NormalWord id tokens) =
+        msum $ map (forToken quoteMap) tokens
+    forToken _ t =
+        if containsQuotes (concat $ oversimplify t)
+        then return $ getId t
+        else Nothing
+
+    squashesQuotes t =
+        case t of
+            T_DollarBraced id _ -> "#" `isPrefixOf` bracedString t
+            _ -> False
+
+    readF _ expr name = do
+        assignment <- getQuotes name
+        return
+          (if isJust assignment
+              && not (isParamTo parents "eval" expr)
+              && not (isQuoteFree parents expr)
+              && not (squashesQuotes expr)
+              then [
+                  makeComment WarningC (fromJust assignment) 2089
+                      "Quotes/backslashes will be treated literally. Use an array.",
+                  makeComment WarningC (getId expr) 2090
+                      "Quotes/backslashes in this variable will not be respected."
+                ]
+              else [])
+
+
+prop_checkFunctionsUsedExternally1 =
+  verifyTree checkFunctionsUsedExternally "foo() { :; }; sudo foo"
+prop_checkFunctionsUsedExternally2 =
+  verifyTree checkFunctionsUsedExternally "alias f='a'; xargs -n 1 f"
+prop_checkFunctionsUsedExternally3 =
+  verifyNotTree checkFunctionsUsedExternally "f() { :; }; echo f"
+prop_checkFunctionsUsedExternally4 =
+  verifyNotTree checkFunctionsUsedExternally "foo() { :; }; sudo \"foo\""
+checkFunctionsUsedExternally params t =
+    runNodeAnalysis checkCommand params t
+  where
+    invokingCmds = [
+        "chroot",
+        "find",
+        "screen",
+        "ssh",
+        "su",
+        "sudo",
+        "xargs"
+        ]
+    checkCommand _ t@(T_SimpleCommand _ _ (cmd:args)) =
+        let name = fromMaybe "" $ getCommandBasename t in
+          when (name `elem` invokingCmds) $
+            mapM_ (checkArg name) args
+    checkCommand _ _ = return ()
+
+    analyse f t = execState (doAnalysis f t) []
+    functions = Map.fromList $ analyse findFunctions t
+    findFunctions (T_Function id _ _ name _) = modify ((name, id):)
+    findFunctions t@(T_SimpleCommand id _ (_:args))
+        | t `isUnqualifiedCommand` "alias" = mapM_ getAlias args
+    findFunctions _ = return ()
+    getAlias arg =
+        let string = concat $ oversimplify arg
+        in when ('=' `elem` string) $
+            modify ((takeWhile (/= '=') string, getId arg):)
+    checkArg cmd arg = potentially $ do
+        literalArg <- getUnquotedLiteral arg  -- only consider unquoted literals
+        definitionId <- Map.lookup literalArg functions
+        return $ do
+            warn (getId arg) 2033
+              "Shell functions can't be passed to external commands."
+            info definitionId 2032 $
+              "Use own script or sh -c '..' to run this from " ++ cmd ++ "."
+
+prop_checkUnused0 = verifyNotTree checkUnusedAssignments "var=foo; echo $var"
+prop_checkUnused1 = verifyTree checkUnusedAssignments "var=foo; echo $bar"
+prop_checkUnused2 = verifyNotTree checkUnusedAssignments "var=foo; export var;"
+prop_checkUnused3 = verifyTree checkUnusedAssignments "for f in *; do echo '$f'; done"
+prop_checkUnused4 = verifyTree checkUnusedAssignments "local i=0"
+prop_checkUnused5 = verifyNotTree checkUnusedAssignments "read lol; echo $lol"
+prop_checkUnused6 = verifyNotTree checkUnusedAssignments "var=4; (( var++ ))"
+prop_checkUnused7 = verifyNotTree checkUnusedAssignments "var=2; $((var))"
+prop_checkUnused8 = verifyTree checkUnusedAssignments "var=2; var=3;"
+prop_checkUnused9 = verifyNotTree checkUnusedAssignments "read ''"
+prop_checkUnused10= verifyNotTree checkUnusedAssignments "read -p 'test: '"
+prop_checkUnused11= verifyNotTree checkUnusedAssignments "bar=5; export foo[$bar]=3"
+prop_checkUnused12= verifyNotTree checkUnusedAssignments "read foo; echo ${!foo}"
+prop_checkUnused13= verifyNotTree checkUnusedAssignments "x=(1); (( x[0] ))"
+prop_checkUnused14= verifyNotTree checkUnusedAssignments "x=(1); n=0; echo ${x[n]}"
+prop_checkUnused15= verifyNotTree checkUnusedAssignments "x=(1); n=0; (( x[n] ))"
+prop_checkUnused16= verifyNotTree checkUnusedAssignments "foo=5; declare -x foo"
+prop_checkUnused17= verifyNotTree checkUnusedAssignments "read -i 'foo' -e -p 'Input: ' bar; $bar;"
+prop_checkUnused18= verifyNotTree checkUnusedAssignments "a=1; arr=( [$a]=42 ); echo \"${arr[@]}\""
+prop_checkUnused19= verifyNotTree checkUnusedAssignments "a=1; let b=a+1; echo $b"
+prop_checkUnused20= verifyNotTree checkUnusedAssignments "a=1; PS1='$a'"
+prop_checkUnused21= verifyNotTree checkUnusedAssignments "a=1; trap 'echo $a' INT"
+prop_checkUnused22= verifyNotTree checkUnusedAssignments "a=1; [ -v a ]"
+prop_checkUnused23= verifyNotTree checkUnusedAssignments "a=1; [ -R a ]"
+prop_checkUnused24= verifyNotTree checkUnusedAssignments "mapfile -C a b; echo ${b[@]}"
+prop_checkUnused25= verifyNotTree checkUnusedAssignments "readarray foo; echo ${foo[@]}"
+prop_checkUnused26= verifyNotTree checkUnusedAssignments "declare -F foo"
+prop_checkUnused27= verifyTree checkUnusedAssignments "var=3; [ var -eq 3 ]"
+prop_checkUnused28= verifyNotTree checkUnusedAssignments "var=3; [[ var -eq 3 ]]"
+prop_checkUnused29= verifyNotTree checkUnusedAssignments "var=(a b); declare -p var"
+prop_checkUnused30= verifyTree checkUnusedAssignments "let a=1"
+prop_checkUnused31= verifyTree checkUnusedAssignments "let 'a=1'"
+prop_checkUnused32= verifyTree checkUnusedAssignments "let a=b=c; echo $a"
+prop_checkUnused33= verifyNotTree checkUnusedAssignments "a=foo; [[ foo =~ ^{$a}$ ]]"
+prop_checkUnused34= verifyNotTree checkUnusedAssignments "foo=1; (( t = foo )); echo $t"
+prop_checkUnused35= verifyNotTree checkUnusedAssignments "a=foo; b=2; echo ${a:b}"
+prop_checkUnused36= verifyNotTree checkUnusedAssignments "if [[ -v foo ]]; then true; fi"
+prop_checkUnused37= verifyNotTree checkUnusedAssignments "fd=2; exec {fd}>&-"
+prop_checkUnused38= verifyTree checkUnusedAssignments "(( a=42 ))"
+prop_checkUnused39= verifyNotTree checkUnusedAssignments "declare -x -f foo"
+prop_checkUnused40= verifyNotTree checkUnusedAssignments "arr=(1 2); num=2; echo \"${arr[@]:num}\""
+checkUnusedAssignments params t = execWriter (mapM_ warnFor unused)
+  where
+    flow = variableFlow params
+    references = foldl (flip ($)) defaultMap (map insertRef flow)
+    insertRef (Reference (base, token, name)) =
+        Map.insert (stripSuffix name) ()
+    insertRef _ = id
+
+    assignments = foldl (flip ($)) Map.empty (map insertAssignment flow)
+    insertAssignment (Assignment (_, token, name, _)) | isVariableName name =
+        Map.insert name token
+    insertAssignment _ = id
+
+    unused = Map.assocs $ Map.difference assignments references
+
+    warnFor (name, token) =
+        warn (getId token) 2034 $
+            name ++ " appears unused. Verify use (or export if used externally)."
+
+    stripSuffix = takeWhile isVariableChar
+    defaultMap = Map.fromList $ zip internalVariables $ repeat ()
+
+prop_checkUnassignedReferences1 = verifyTree checkUnassignedReferences "echo $foo"
+prop_checkUnassignedReferences2 = verifyNotTree checkUnassignedReferences "foo=hello; echo $foo"
+prop_checkUnassignedReferences3 = verifyTree checkUnassignedReferences "MY_VALUE=3; echo $MYVALUE"
+prop_checkUnassignedReferences4 = verifyNotTree checkUnassignedReferences "RANDOM2=foo; echo $RANDOM"
+prop_checkUnassignedReferences5 = verifyNotTree checkUnassignedReferences "declare -A foo=([bar]=baz); echo ${foo[bar]}"
+prop_checkUnassignedReferences6 = verifyNotTree checkUnassignedReferences "foo=..; echo ${foo-bar}"
+prop_checkUnassignedReferences7 = verifyNotTree checkUnassignedReferences "getopts ':h' foo; echo $foo"
+prop_checkUnassignedReferences8 = verifyNotTree checkUnassignedReferences "let 'foo = 1'; echo $foo"
+prop_checkUnassignedReferences9 = verifyNotTree checkUnassignedReferences "echo ${foo-bar}"
+prop_checkUnassignedReferences10= verifyNotTree checkUnassignedReferences "echo ${foo:?}"
+prop_checkUnassignedReferences11= verifyNotTree checkUnassignedReferences "declare -A foo; echo \"${foo[@]}\""
+prop_checkUnassignedReferences12= verifyNotTree checkUnassignedReferences "typeset -a foo; echo \"${foo[@]}\""
+prop_checkUnassignedReferences13= verifyNotTree checkUnassignedReferences "f() { local foo; echo $foo; }"
+prop_checkUnassignedReferences14= verifyNotTree checkUnassignedReferences "foo=; echo $foo"
+prop_checkUnassignedReferences15= verifyNotTree checkUnassignedReferences "f() { true; }; export -f f"
+prop_checkUnassignedReferences16= verifyNotTree checkUnassignedReferences "declare -A foo=( [a b]=bar ); echo ${foo[a b]}"
+prop_checkUnassignedReferences17= verifyNotTree checkUnassignedReferences "USERS=foo; echo $USER"
+prop_checkUnassignedReferences18= verifyNotTree checkUnassignedReferences "FOOBAR=42; export FOOBAR="
+prop_checkUnassignedReferences19= verifyNotTree checkUnassignedReferences "readonly foo=bar; echo $foo"
+prop_checkUnassignedReferences20= verifyNotTree checkUnassignedReferences "printf -v foo bar; echo $foo"
+prop_checkUnassignedReferences21= verifyTree checkUnassignedReferences "echo ${#foo}"
+prop_checkUnassignedReferences22= verifyNotTree checkUnassignedReferences "echo ${!os*}"
+prop_checkUnassignedReferences23= verifyTree checkUnassignedReferences "declare -a foo; foo[bar]=42;"
+prop_checkUnassignedReferences24= verifyNotTree checkUnassignedReferences "declare -A foo; foo[bar]=42;"
+prop_checkUnassignedReferences25= verifyNotTree checkUnassignedReferences "declare -A foo=(); foo[bar]=42;"
+prop_checkUnassignedReferences26= verifyNotTree checkUnassignedReferences "a::b() { foo; }; readonly -f a::b"
+prop_checkUnassignedReferences27= verifyNotTree checkUnassignedReferences ": ${foo:=bar}"
+prop_checkUnassignedReferences28= verifyNotTree checkUnassignedReferences "#!/bin/ksh\necho \"${.sh.version}\"\n"
+prop_checkUnassignedReferences29= verifyNotTree checkUnassignedReferences "if [[ -v foo ]]; then echo $foo; fi"
+prop_checkUnassignedReferences30= verifyNotTree checkUnassignedReferences "if [[ -v foo[3] ]]; then echo ${foo[3]}; fi"
+prop_checkUnassignedReferences31= verifyNotTree checkUnassignedReferences "X=1; if [[ -v foo[$X+42] ]]; then echo ${foo[$X+42]}; fi"
+prop_checkUnassignedReferences32= verifyNotTree checkUnassignedReferences "if [[ -v \"foo[1]\" ]]; then echo ${foo[@]}; fi"
+prop_checkUnassignedReferences33= verifyNotTree checkUnassignedReferences "f() { local -A foo; echo \"${foo[@]}\"; }"
+prop_checkUnassignedReferences34= verifyNotTree checkUnassignedReferences "declare -A foo; (( foo[bar] ))"
+prop_checkUnassignedReferences35= verifyNotTree checkUnassignedReferences "echo ${arr[foo-bar]:?fail}"
+checkUnassignedReferences params t = warnings
+  where
+    (readMap, writeMap) = execState (mapM tally $ variableFlow params) (Map.empty, Map.empty)
+    defaultAssigned = Map.fromList $ map (\a -> (a, ())) $ filter (not . null) internalVariables
+
+    tally (Assignment (_, _, name, _))  =
+        modify (\(read, written) -> (read, Map.insert name () written))
+    tally (Reference (_, place, name)) =
+        modify (\(read, written) -> (Map.insertWith (const id) name place read, written))
+    tally _ = return ()
+
+    unassigned = Map.toList $ Map.difference (Map.difference readMap writeMap) defaultAssigned
+    writtenVars = filter isVariableName $ Map.keys writeMap
+
+    getBestMatch var = do
+        (match, score) <- listToMaybe best
+        guard $ goodMatch var match score
+        return match
+      where
+        matches = map (\x -> (x, match var x)) writtenVars
+        best = sortBy (comparing snd) matches
+        goodMatch var match score =
+            let l = length match in
+                l > 3 && score <= 1
+                || l > 7 && score <= 2
+
+    isLocal = any isLower
+
+    warningForGlobals var place = do
+        match <- getBestMatch var
+        return $ warn (getId place) 2153 $
+            "Possible misspelling: " ++ var ++ " may not be assigned, but " ++ match ++ " is."
+
+    warningForLocals var place =
+        return $ warn (getId place) 2154 $
+            var ++ " is referenced but not assigned" ++ optionalTip ++ "."
+      where
+        optionalTip =
+            if var `elem` commonCommands
+            then " (for output from commands, use \"$(" ++ var ++ " ..." ++ ")\" )"
+            else fromMaybe "" $ do
+                    match <- getBestMatch var
+                    return $ " (did you mean '" ++ match ++ "'?)"
+
+    warningFor var place = do
+        guard . not $ isInArray var place || isGuarded place
+        (if isLocal var then warningForLocals else warningForGlobals) var place
+
+    warnings = execWriter . sequence $ mapMaybe (uncurry warningFor) unassigned
+
+    -- Due to parsing, foo=( [bar]=baz ) parses 'bar' as a reference even for assoc arrays.
+    -- Similarly, ${foo[bar baz]} may not be referencing bar/baz. Just skip these.
+    isInArray var t = any isArray $ getPath (parentMap params) t
+      where
+        isArray T_Array {} = True
+        isArray b@(T_DollarBraced _ _) | var /= getBracedReference (bracedString b) = True
+        isArray _ = False
+
+    isGuarded (T_DollarBraced _ v) =
+        rest `matches` guardRegex
+      where
+        name = concat $ oversimplify v
+        rest = dropWhile isVariableChar $ dropWhile (`elem` "#!") name
+    isGuarded _ = False
+    --  :? or :- with optional array index and colon
+    guardRegex = mkRegex "^(\\[.*\\])?:?[-?]"
+
+    match var candidate =
+        if var /= candidate && map toLower var == map toLower candidate
+        then 1
+        else dist var candidate
+
+
+prop_checkGlobsAsOptions1 = verify checkGlobsAsOptions "rm *.txt"
+prop_checkGlobsAsOptions2 = verify checkGlobsAsOptions "ls ??.*"
+prop_checkGlobsAsOptions3 = verifyNot checkGlobsAsOptions "rm -- *.txt"
+prop_checkGlobsAsOptions4 = verifyNot checkGlobsAsOptions "*.txt"
+checkGlobsAsOptions _ (T_SimpleCommand _ _ args) =
+    mapM_ check $ takeWhile (not . isEndOfArgs) (drop 1 args)
+  where
+    check v@(T_NormalWord _ (T_Glob id s:_)) | s == "*" || s == "?" =
+        info id 2035 "Use ./*glob* or -- *glob* so names with dashes won't become options."
+    check _ = return ()
+
+    isEndOfArgs t =
+        case concat $ oversimplify t of
+            "--" -> True
+            ":::" -> True
+            "::::" -> True
+            _ -> False
+
+checkGlobsAsOptions _ _ = return ()
+
+
+prop_checkWhileReadPitfalls1 = verify checkWhileReadPitfalls "while read foo; do ssh $foo uptime; done < file"
+prop_checkWhileReadPitfalls2 = verifyNot checkWhileReadPitfalls "while read -u 3 foo; do ssh $foo uptime; done 3< file"
+prop_checkWhileReadPitfalls3 = verifyNot checkWhileReadPitfalls "while true; do ssh host uptime; done"
+prop_checkWhileReadPitfalls4 = verifyNot checkWhileReadPitfalls "while read foo; do ssh $foo hostname < /dev/null; done"
+prop_checkWhileReadPitfalls5 = verifyNot checkWhileReadPitfalls "while read foo; do echo ls | ssh $foo; done"
+prop_checkWhileReadPitfalls6 = verifyNot checkWhileReadPitfalls "while read foo <&3; do ssh $foo; done 3< foo"
+prop_checkWhileReadPitfalls7 = verify checkWhileReadPitfalls "while read foo; do if true; then ssh $foo uptime; fi; done < file"
+prop_checkWhileReadPitfalls8 = verifyNot checkWhileReadPitfalls "while read foo; do ssh -n $foo uptime; done < file"
+
+checkWhileReadPitfalls _ (T_WhileExpression id [command] contents)
+        | isStdinReadCommand command =
+    mapM_ checkMuncher contents
+  where
+    munchers = [ "ssh", "ffmpeg", "mplayer", "HandBrakeCLI" ]
+    preventionFlags = ["n", "noconsolecontrols" ]
+
+    isStdinReadCommand (T_Pipeline _ _ [T_Redirecting id redirs cmd]) =
+        let plaintext = oversimplify cmd
+        in head (plaintext ++ [""]) == "read"
+            && ("-u" `notElem` plaintext)
+            && all (not . stdinRedirect) redirs
+    isStdinReadCommand _ = False
+
+    checkMuncher (T_Pipeline _ _ (T_Redirecting _ redirs cmd:_)) | not $ any stdinRedirect redirs =
+        case cmd of
+            (T_IfExpression _ thens elses) ->
+              mapM_ checkMuncher . concat $ map fst thens ++ map snd thens ++ [elses]
+
+            _ -> potentially $ do
+                name <- getCommandBasename cmd
+                guard $ name `elem` munchers
+
+                -- Sloppily check if the command has a flag to prevent eating stdin.
+                let flags = getAllFlags cmd
+                guard . not $ any (`elem` preventionFlags) $ map snd flags
+                return $ do
+                    info id 2095 $
+                        name ++ " may swallow stdin, preventing this loop from working properly."
+                    warn (getId cmd) 2095 $
+                        "Add < /dev/null to prevent " ++ name ++ " from swallowing stdin."
+    checkMuncher _ = return ()
+
+    stdinRedirect (T_FdRedirect _ fd _)
+        | fd == "" || fd == "0" = True
+    stdinRedirect _ = False
+checkWhileReadPitfalls _ _ = return ()
+
+
+prop_checkPrefixAssign1 = verify checkPrefixAssignmentReference "var=foo echo $var"
+prop_checkPrefixAssign2 = verifyNot checkPrefixAssignmentReference "var=$(echo $var) cmd"
+checkPrefixAssignmentReference params t@(T_DollarBraced id value) =
+    check path
+  where
+    name = getBracedReference $ bracedString t
+    path = getPath (parentMap params) t
+    idPath = map getId path
+
+    check [] = return ()
+    check (t:rest) =
+        case t of
+            T_SimpleCommand _ vars (_:_) -> mapM_ checkVar vars
+            _ -> check rest
+    checkVar (T_Assignment aId mode aName [] value) |
+            aName == name && (aId `notElem` idPath) = do
+        warn aId 2097 "This assignment is only seen by the forked process."
+        warn id 2098 "This expansion will not see the mentioned assignment."
+    checkVar _ = return ()
+
+checkPrefixAssignmentReference _ _ = return ()
+
+prop_checkCharRangeGlob1 = verify checkCharRangeGlob "ls *[:digit:].jpg"
+prop_checkCharRangeGlob2 = verifyNot checkCharRangeGlob "ls *[[:digit:]].jpg"
+prop_checkCharRangeGlob3 = verify checkCharRangeGlob "ls [10-15]"
+prop_checkCharRangeGlob4 = verifyNot checkCharRangeGlob "ls [a-zA-Z]"
+prop_checkCharRangeGlob5 = verifyNot checkCharRangeGlob "tr -d [a-zA-Z]" -- tr has 2060
+checkCharRangeGlob p t@(T_Glob id str) |
+  isCharClass str && not (isParamTo (parentMap p) "tr" t) =
+    if ":" `isPrefixOf` contents
+        && ":" `isSuffixOf` contents
+        && contents /= ":"
+    then warn id 2101 "Named class needs outer [], e.g. [[:digit:]]."
+    else
+        when ('[' `notElem` contents && hasDupes) $
+            info id 2102 "Ranges can only match single chars (mentioned due to duplicates)."
+  where
+    isCharClass str = "[" `isPrefixOf` str && "]" `isSuffixOf` str
+    contents = drop 1 . take (length str - 1) $ str
+    hasDupes = any (>1) . map length . group . sort . filter (/= '-') $ contents
+checkCharRangeGlob _ _ = return ()
+
+
+
+prop_checkCdAndBack1 = verify checkCdAndBack "for f in *; do cd $f; git pull; cd ..; done"
+prop_checkCdAndBack2 = verifyNot checkCdAndBack "for f in *; do cd $f || continue; git pull; cd ..; done"
+prop_checkCdAndBack3 = verifyNot checkCdAndBack "while [[ $PWD != / ]]; do cd ..; done"
+prop_checkCdAndBack4 = verify checkCdAndBack "cd $tmp; foo; cd -"
+checkCdAndBack params = doLists
+  where
+    shell = shellType params
+    doLists (T_ForIn _ _ _ cmds) = doList cmds
+    doLists (T_ForArithmetic _ _ _ _ cmds) = doList cmds
+    doLists (T_WhileExpression _ _ cmds) = doList cmds
+    doLists (T_UntilExpression _ _ cmds) = doList cmds
+    doLists (T_Script _ _ cmds) = doList cmds
+    doLists (T_IfExpression _ thens elses) = do
+        mapM_ (\(_, l) -> doList l) thens
+        doList elses
+    doLists _ = return ()
+
+    isCdRevert t =
+        case oversimplify t of
+            ["cd", p] -> p `elem` ["..", "-"]
+            _ -> False
+
+    getCmd (T_Annotation id _ x) = getCmd x
+    getCmd (T_Pipeline id _ [x]) = getCommandName x
+    getCmd _ = Nothing
+
+    doList list =
+        let cds = filter ((== Just "cd") . getCmd) list in
+            when (length cds >= 2 && isCdRevert (last cds)) $
+               info (getId $ last cds) 2103 message
+
+    message = "Use a ( subshell ) to avoid having to cd back."
+
+prop_checkLoopKeywordScope1 = verify checkLoopKeywordScope "continue 2"
+prop_checkLoopKeywordScope2 = verify checkLoopKeywordScope "for f; do ( break; ); done"
+prop_checkLoopKeywordScope3 = verify checkLoopKeywordScope "if true; then continue; fi"
+prop_checkLoopKeywordScope4 = verifyNot checkLoopKeywordScope "while true; do break; done"
+prop_checkLoopKeywordScope5 = verify checkLoopKeywordScope "if true; then break; fi"
+prop_checkLoopKeywordScope6 = verify checkLoopKeywordScope "while true; do true | { break; }; done"
+prop_checkLoopKeywordScope7 = verifyNot checkLoopKeywordScope "#!/bin/ksh\nwhile true; do true | { break; }; done"
+checkLoopKeywordScope params t |
+        name `elem` map Just ["continue", "break"] =
+    if not $ any isLoop path
+    then if any isFunction $ take 1 path
+        -- breaking at a source/function invocation is an abomination. Let's ignore it.
+        then err (getId t) 2104 $ "In functions, use return instead of " ++ fromJust name ++ "."
+        else err (getId t) 2105 $ fromJust name ++ " is only valid in loops."
+    else case map subshellType $ filter (not . isFunction) path of
+        Just str:_ -> warn (getId t) 2106 $
+            "This only exits the subshell caused by the " ++ str ++ "."
+        _ -> return ()
+  where
+    name = getCommandName t
+    path = let p = getPath (parentMap params) t in filter relevant p
+    subshellType t = case leadType params t of
+        NoneScope -> Nothing
+        SubshellScope str -> return str
+    relevant t = isLoop t || isFunction t || isJust (subshellType t)
+checkLoopKeywordScope _ _ = return ()
+
+
+prop_checkFunctionDeclarations1 = verify checkFunctionDeclarations "#!/bin/ksh\nfunction foo() { command foo --lol \"$@\"; }"
+prop_checkFunctionDeclarations2 = verify checkFunctionDeclarations "#!/bin/dash\nfunction foo { lol; }"
+prop_checkFunctionDeclarations3 = verifyNot checkFunctionDeclarations "foo() { echo bar; }"
+checkFunctionDeclarations params
+        (T_Function id (FunctionKeyword hasKeyword) (FunctionParentheses hasParens) _ _) =
+    case shellType params of
+        Bash -> return ()
+        Ksh ->
+            when (hasKeyword && hasParens) $
+                err id 2111 "ksh does not allow 'function' keyword and '()' at the same time."
+        Dash -> forSh
+        Sh   -> forSh
+
+    where
+        forSh = do
+            when (hasKeyword && hasParens) $
+                warn id 2112 "'function' keyword is non-standard. Delete it."
+            when (hasKeyword && not hasParens) $
+                warn id 2113 "'function' keyword is non-standard. Use 'foo()' instead of 'function foo'."
+checkFunctionDeclarations _ _ = return ()
+
+
+
+prop_checkStderrPipe1 = verify checkStderrPipe "#!/bin/ksh\nfoo |& bar"
+prop_checkStderrPipe2 = verifyNot checkStderrPipe "#!/bin/bash\nfoo |& bar"
+checkStderrPipe params =
+    case shellType params of
+        Ksh -> match
+        _ -> const $ return ()
+  where
+    match (T_Pipe id "|&") =
+        err id 2118 "Ksh does not support |&. Use 2>&1 |."
+    match _ = return ()
+
+prop_checkUnpassedInFunctions1 = verifyTree checkUnpassedInFunctions "foo() { echo $1; }; foo"
+prop_checkUnpassedInFunctions2 = verifyNotTree checkUnpassedInFunctions "foo() { echo $1; };"
+prop_checkUnpassedInFunctions3 = verifyNotTree checkUnpassedInFunctions "foo() { echo $lol; }; foo"
+prop_checkUnpassedInFunctions4 = verifyNotTree checkUnpassedInFunctions "foo() { echo $0; }; foo"
+prop_checkUnpassedInFunctions5 = verifyNotTree checkUnpassedInFunctions "foo() { echo $1; }; foo 'lol'; foo"
+prop_checkUnpassedInFunctions6 = verifyNotTree checkUnpassedInFunctions "foo() { set -- *; echo $1; }; foo"
+prop_checkUnpassedInFunctions7 = verifyTree checkUnpassedInFunctions "foo() { echo $1; }; foo; foo;"
+prop_checkUnpassedInFunctions8 = verifyNotTree checkUnpassedInFunctions "foo() { echo $((1)); }; foo;"
+prop_checkUnpassedInFunctions9 = verifyNotTree checkUnpassedInFunctions "foo() { echo $(($b)); }; foo;"
+prop_checkUnpassedInFunctions10= verifyNotTree checkUnpassedInFunctions "foo() { echo $!; }; foo;"
+prop_checkUnpassedInFunctions11= verifyNotTree checkUnpassedInFunctions "foo() { bar() { echo $1; }; bar baz; }; foo;"
+prop_checkUnpassedInFunctions12= verifyNotTree checkUnpassedInFunctions "foo() { echo ${!var*}; }; foo;"
+checkUnpassedInFunctions params root =
+    execWriter $ mapM_ warnForGroup referenceGroups
+  where
+    functionMap :: Map.Map String Token
+    functionMap = Map.fromList $
+        map (\t@(T_Function _ _ _ name _) -> (name,t)) functions
+    functions = execWriter $ doAnalysis (tell . maybeToList . findFunction) root
+
+    findFunction t@(T_Function id _ _ name body) =
+        let flow = getVariableFlow params body
+        in
+          if any (isPositionalReference t) flow && not (any isPositionalAssignment flow)
+            then return t
+            else Nothing
+    findFunction _ = Nothing
+
+    isPositionalAssignment x =
+        case x of
+            Assignment (_, _, str, _) -> isPositional str
+            _ -> False
+    isPositionalReference function x =
+        case x of
+            Reference (_, t, str) -> isPositional str && t `isDirectChildOf` function
+            _ -> False
+
+    isDirectChildOf child parent = fromMaybe False $ do
+        function <- find (\x -> case x of
+            T_Function {} -> True
+            T_Script {} -> True  -- for sourced files
+            _ -> False) $
+                getPath (parentMap params) child
+        return $ getId parent == getId function
+
+    referenceList :: [(String, Bool, Token)]
+    referenceList = execWriter $
+        doAnalysis (fromMaybe (return ()) . checkCommand) root
+    checkCommand :: Token -> Maybe (Writer [(String, Bool, Token)] ())
+    checkCommand t@(T_SimpleCommand _ _ (cmd:args)) = do
+        str <- getLiteralString cmd
+        guard $ Map.member str functionMap
+        return $ tell [(str, null args, t)]
+    checkCommand _ = Nothing
+
+    isPositional str = str == "*" || str == "@"
+        || (all isDigit str && str /= "0" && str /= "")
+
+    isArgumentless (_, b, _) = b
+    referenceGroups = Map.elems $ foldr updateWith Map.empty referenceList
+    updateWith x@(name, _, _) = Map.insertWith (++) name [x]
+
+    warnForGroup group =
+        when (all isArgumentless group) $ do
+            mapM_ suggestParams group
+            warnForDeclaration group
+
+    suggestParams (name, _, thing) =
+        info (getId thing) 2119 $
+            "Use " ++ name ++ " \"$@\" if function's $1 should mean script's $1."
+    warnForDeclaration ((name, _, _):_) =
+        warn (getId . fromJust $ Map.lookup name functionMap) 2120 $
+            name ++ " references arguments, but none are ever passed."
+
+
+prop_checkOverridingPath1 = verify checkOverridingPath "PATH=\"$var/$foo\""
+prop_checkOverridingPath2 = verify checkOverridingPath "PATH=\"mydir\""
+prop_checkOverridingPath3 = verify checkOverridingPath "PATH=/cow/foo"
+prop_checkOverridingPath4 = verifyNot checkOverridingPath "PATH=/cow/foo/bin"
+prop_checkOverridingPath5 = verifyNot checkOverridingPath "PATH='/bin:/sbin'"
+prop_checkOverridingPath6 = verifyNot checkOverridingPath "PATH=\"$var/$foo\" cmd"
+prop_checkOverridingPath7 = verifyNot checkOverridingPath "PATH=$OLDPATH"
+prop_checkOverridingPath8 = verifyNot checkOverridingPath "PATH=$PATH:/stuff"
+checkOverridingPath _ (T_SimpleCommand _ vars []) =
+    mapM_ checkVar vars
+  where
+    checkVar (T_Assignment id Assign "PATH" [] word) =
+        let string = concat $ oversimplify word
+        in unless (any (`isInfixOf` string) ["/bin", "/sbin" ]) $ do
+            when ('/' `elem` string && ':' `notElem` string) $ notify id
+            when (isLiteral word && ':' `notElem` string && '/' `notElem` string) $ notify id
+    checkVar _ = return ()
+    notify id = warn id 2123 "PATH is the shell search path. Use another name."
+checkOverridingPath _ _ = return ()
+
+prop_checkTildeInPath1 = verify checkTildeInPath "PATH=\"$PATH:~/bin\""
+prop_checkTildeInPath2 = verify checkTildeInPath "PATH='~foo/bin'"
+prop_checkTildeInPath3 = verifyNot checkTildeInPath "PATH=~/bin"
+checkTildeInPath _ (T_SimpleCommand _ vars _) =
+    mapM_ checkVar vars
+  where
+    checkVar (T_Assignment id Assign "PATH" [] (T_NormalWord _ parts)) =
+        when (any (\x -> isQuoted x && hasTilde x) parts) $
+            warn id 2147 "Literal tilde in PATH works poorly across programs."
+    checkVar _ = return ()
+
+    hasTilde t = fromMaybe False (liftM2 elem (return '~') (getLiteralStringExt (const $ return "") t))
+    isQuoted T_DoubleQuoted {} = True
+    isQuoted T_SingleQuoted {} = True
+    isQuoted _ = False
+checkTildeInPath _ _ = return ()
+
+prop_checkUnsupported3 = verify checkUnsupported "#!/bin/sh\ncase foo in bar) baz ;& esac"
+prop_checkUnsupported4 = verify checkUnsupported "#!/bin/ksh\ncase foo in bar) baz ;;& esac"
+prop_checkUnsupported5 = verify checkUnsupported "#!/bin/bash\necho \"${ ls; }\""
+checkUnsupported params t =
+    when (not (null support) && (shellType params `notElem` support)) $
+        report name
+ where
+    (name, support) = shellSupport t
+    report s = err (getId t) 2127 $
+        "To use " ++ s ++ ", specify #!/usr/bin/env " ++
+            (map toLower . intercalate " or " . map show $ support)
+
+-- TODO: Move more of these checks here
+shellSupport t =
+  case t of
+    T_CaseExpression _ _ list -> forCase (map (\(a,_,_) -> a) list)
+    T_DollarBraceCommandExpansion {} -> ("${ ..; } command expansion", [Ksh])
+    _ -> ("", [])
+  where
+    forCase seps | CaseContinue `elem` seps = ("cases with ;;&", [Bash])
+    forCase seps | CaseFallThrough `elem` seps = ("cases with ;&", [Bash, Ksh])
+    forCase _ = ("", [])
+
+
+groupWith f = groupBy ((==) `on` f)
+
+prop_checkMultipleAppends1 = verify checkMultipleAppends "foo >> file; bar >> file; baz >> file;"
+prop_checkMultipleAppends2 = verify checkMultipleAppends "foo >> file; bar | grep f >> file; baz >> file;"
+prop_checkMultipleAppends3 = verifyNot checkMultipleAppends "foo < file; bar < file; baz < file;"
+checkMultipleAppends params t =
+    mapM_ checkList $ getCommandSequences t
+  where
+    checkList list =
+        mapM_ checkGroup (groupWith (fmap fst) $ map getTarget list)
+    checkGroup (f:_:_:_) | isJust f =
+        style (snd $ fromJust f) 2129
+            "Consider using { cmd1; cmd2; } >> file instead of individual redirects."
+    checkGroup _ = return ()
+    getTarget (T_Annotation _ _ t) = getTarget t
+    getTarget (T_Pipeline _ _ args@(_:_)) = getTarget (last args)
+    getTarget (T_Redirecting id list _) = do
+        file <- mapMaybe getAppend list !!! 0
+        return (file, id)
+    getTarget _ = Nothing
+    getAppend (T_FdRedirect _ _ (T_IoFile _ T_DGREAT {} f)) = return f
+    getAppend _ = Nothing
+
+
+prop_checkSuspiciousIFS1 = verify checkSuspiciousIFS "IFS=\"\\n\""
+prop_checkSuspiciousIFS2 = verifyNot checkSuspiciousIFS "IFS=$'\\t'"
+checkSuspiciousIFS params (T_Assignment id Assign "IFS" [] value) =
+    potentially $ do
+        str <- getLiteralString value
+        return $ check str
+  where
+    n = if shellType params == Sh then "'<literal linefeed here>'" else "$'\\n'"
+    t = if shellType params == Sh then "\"$(printf '\\t')\"" else "$'\\t'"
+    check value =
+        case value of
+            "\\n" -> suggest n
+            "/n" -> suggest n
+            "\\t" -> suggest t
+            "/t" -> suggest t
+            _ -> return ()
+    suggest r = warn id 2141 $ "Did you mean IFS=" ++ r ++ " ?"
+checkSuspiciousIFS _ _ = return ()
+
+
+prop_checkGrepQ1= verify checkShouldUseGrepQ "[[ $(foo | grep bar) ]]"
+prop_checkGrepQ2= verify checkShouldUseGrepQ "[ -z $(fgrep lol) ]"
+prop_checkGrepQ3= verify checkShouldUseGrepQ "[ -n \"$(foo | zgrep lol)\" ]"
+prop_checkGrepQ4= verifyNot checkShouldUseGrepQ "[ -z $(grep bar | cmd) ]"
+prop_checkGrepQ5= verifyNot checkShouldUseGrepQ "rm $(ls | grep file)"
+prop_checkGrepQ6= verifyNot checkShouldUseGrepQ "[[ -n $(pgrep foo) ]]"
+checkShouldUseGrepQ params t =
+    potentially $ case t of
+        TC_Nullary id _ token -> check id True token
+        TC_Unary id _ "-n" token -> check id True token
+        TC_Unary id _ "-z" token -> check id False token
+        _ -> fail "not check"
+  where
+    check id bool token = do
+        name <- getFinalGrep token
+        let op = if bool then "-n" else "-z"
+        let flip = if bool then "" else "! "
+        return . style id 2143 $
+            "Use " ++ flip ++ name ++ " -q instead of " ++
+                "comparing output with [ " ++ op ++ " .. ]."
+
+    getFinalGrep t = do
+        cmds <- getPipeline t
+        guard . not . null $ cmds
+        name <- getCommandBasename $ last cmds
+        guard . isGrep $ name
+        return name
+    getPipeline t =
+        case t of
+            T_NormalWord _ [x] -> getPipeline x
+            T_DoubleQuoted _ [x] -> getPipeline x
+            T_DollarExpansion _ [x] -> getPipeline x
+            T_Pipeline _ _ cmds -> return cmds
+            _ -> fail "unknown"
+    isGrep = (`elem` ["grep", "egrep", "fgrep", "zgrep"])
+
+prop_checkTestArgumentSplitting1 = verify checkTestArgumentSplitting "[ -e *.mp3 ]"
+prop_checkTestArgumentSplitting2 = verifyNot checkTestArgumentSplitting "[[ $a == *b* ]]"
+prop_checkTestArgumentSplitting3 = verify checkTestArgumentSplitting "[[ *.png == '' ]]"
+prop_checkTestArgumentSplitting4 = verify checkTestArgumentSplitting "[[ foo == f{o,oo,ooo} ]]"
+prop_checkTestArgumentSplitting5 = verify checkTestArgumentSplitting "[[ $@ ]]"
+prop_checkTestArgumentSplitting6 = verify checkTestArgumentSplitting "[ -e $@ ]"
+prop_checkTestArgumentSplitting7 = verify checkTestArgumentSplitting "[ $@ == $@ ]"
+prop_checkTestArgumentSplitting8 = verify checkTestArgumentSplitting "[[ $@ = $@ ]]"
+prop_checkTestArgumentSplitting9 = verifyNot checkTestArgumentSplitting "[[ foo =~ bar{1,2} ]]"
+prop_checkTestArgumentSplitting10 = verifyNot checkTestArgumentSplitting "[ \"$@\" ]"
+prop_checkTestArgumentSplitting11 = verify checkTestArgumentSplitting "[[ \"$@\" ]]"
+prop_checkTestArgumentSplitting12 = verify checkTestArgumentSplitting "[ *.png ]"
+prop_checkTestArgumentSplitting13 = verify checkTestArgumentSplitting "[ \"$@\" == \"\" ]"
+prop_checkTestArgumentSplitting14 = verify checkTestArgumentSplitting "[[ \"$@\" == \"\" ]]"
+prop_checkTestArgumentSplitting15 = verifyNot checkTestArgumentSplitting "[[ \"$*\" == \"\" ]]"
+prop_checkTestArgumentSplitting16 = verifyNot checkTestArgumentSplitting "[[ -v foo[123] ]]"
+checkTestArgumentSplitting :: Parameters -> Token -> Writer [TokenComment] ()
+checkTestArgumentSplitting _ t =
+    case t of
+        (TC_Unary _ typ op token) | isGlob token ->
+            if op == "-v"
+            then
+                when (typ == SingleBracket) $
+                    err (getId token) 2208 $
+                      "Use [[ ]] or quote arguments to -v to avoid glob expansion."
+            else
+                err (getId token) 2144 $
+                   op ++ " doesn't work with globs. Use a for loop."
+
+        (TC_Nullary _ typ token) -> do
+            checkBraces typ token
+            checkGlobs typ token
+            when (typ == DoubleBracket) $
+                checkArrays typ token
+
+        (TC_Unary _ typ op token) -> checkAll typ token
+
+        (TC_Binary _ typ op lhs rhs) ->
+            if op `elem` ["=", "==", "!=", "=~"]
+            then do
+                checkAll typ lhs
+                checkArrays typ rhs
+                checkBraces typ rhs
+            else mapM_ (checkAll typ) [lhs, rhs]
+        _ -> return ()
+  where
+    checkAll typ token = do
+        checkArrays typ token
+        checkBraces typ token
+        checkGlobs typ token
+
+    checkArrays typ token =
+        when (any isArrayExpansion $ getWordParts token) $
+            if typ == SingleBracket
+            then warn (getId token) 2198 "Arrays don't work as operands in [ ]. Use a loop (or concatenate with * instead of @)."
+            else err (getId token) 2199 "Arrays implicitly concatenate in [[ ]]. Use a loop (or explicit * instead of @)."
+
+    checkBraces typ token =
+        when (any isBraceExpansion $ getWordParts token) $
+            if typ == SingleBracket
+            then warn (getId token) 2200 "Brace expansions don't work as operands in [ ]. Use a loop."
+            else err (getId token) 2201 "Brace expansion doesn't happen in [[ ]]. Use a loop."
+
+    checkGlobs typ token =
+        when (isGlob token) $
+            if typ == SingleBracket
+            then warn (getId token) 2202 "Globs don't work as operands in [ ]. Use a loop."
+            else err (getId token) 2203 "Globs are ignored in [[ ]] except right of =/!=. Use a loop."
+
+
+prop_checkMaskedReturns1 = verify checkMaskedReturns "f() { local a=$(false); }"
+prop_checkMaskedReturns2 = verify checkMaskedReturns "declare a=$(false)"
+prop_checkMaskedReturns3 = verify checkMaskedReturns "declare a=\"`false`\""
+prop_checkMaskedReturns4 = verifyNot checkMaskedReturns "declare a; a=$(false)"
+prop_checkMaskedReturns5 = verifyNot checkMaskedReturns "f() { local -r a=$(false); }"
+checkMaskedReturns _ t@(T_SimpleCommand id _ (cmd:rest)) = potentially $ do
+    name <- getCommandName t
+    guard $ name `elem` ["declare", "export"]
+        || name == "local" && "r" `notElem` map snd (getAllFlags t)
+    return $ mapM_ checkArgs rest
+  where
+    checkArgs (T_Assignment id _ _ _ word) | any hasReturn $ getWordParts word =
+        warn id 2155 "Declare and assign separately to avoid masking return values."
+    checkArgs _ = return ()
+
+    hasReturn t = case t of
+        T_Backticked {} -> True
+        T_DollarExpansion {} -> True
+        _ -> False
+checkMaskedReturns _ _ = return ()
+
+
+prop_checkReadWithoutR1 = verify checkReadWithoutR "read -a foo"
+prop_checkReadWithoutR2 = verifyNot checkReadWithoutR "read -ar foo"
+checkReadWithoutR _ t@T_SimpleCommand {} | t `isUnqualifiedCommand` "read" =
+    unless ("r" `elem` map snd (getAllFlags t)) $
+        info (getId t) 2162 "read without -r will mangle backslashes."
+checkReadWithoutR _ _ = return ()
+
+prop_checkUncheckedCd1 = verifyTree checkUncheckedCdPushdPopd "cd ~/src; rm -r foo"
+prop_checkUncheckedCd2 = verifyNotTree checkUncheckedCdPushdPopd "cd ~/src || exit; rm -r foo"
+prop_checkUncheckedCd3 = verifyNotTree checkUncheckedCdPushdPopd "set -e; cd ~/src; rm -r foo"
+prop_checkUncheckedCd4 = verifyNotTree checkUncheckedCdPushdPopd "if cd foo; then rm foo; fi"
+prop_checkUncheckedCd5 = verifyTree checkUncheckedCdPushdPopd "if true; then cd foo; fi"
+prop_checkUncheckedCd6 = verifyNotTree checkUncheckedCdPushdPopd "cd .."
+prop_checkUncheckedCd7 = verifyNotTree checkUncheckedCdPushdPopd "#!/bin/bash -e\ncd foo\nrm bar"
+prop_checkUncheckedCd8 = verifyNotTree checkUncheckedCdPushdPopd "set -o errexit; cd foo; rm bar"
+prop_checkUncheckedCd9 = verifyTree checkUncheckedCdPushdPopd "builtin cd ~/src; rm -r foo"
+prop_checkUncheckedPushd1 = verifyTree checkUncheckedCdPushdPopd "pushd ~/src; rm -r foo"
+prop_checkUncheckedPushd2 = verifyNotTree checkUncheckedCdPushdPopd "pushd ~/src || exit; rm -r foo"
+prop_checkUncheckedPushd3 = verifyNotTree checkUncheckedCdPushdPopd "set -e; pushd ~/src; rm -r foo"
+prop_checkUncheckedPushd4 = verifyNotTree checkUncheckedCdPushdPopd "if pushd foo; then rm foo; fi"
+prop_checkUncheckedPushd5 = verifyTree checkUncheckedCdPushdPopd "if true; then pushd foo; fi"
+prop_checkUncheckedPushd6 = verifyNotTree checkUncheckedCdPushdPopd "pushd .."
+prop_checkUncheckedPushd7 = verifyNotTree checkUncheckedCdPushdPopd "#!/bin/bash -e\npushd foo\nrm bar"
+prop_checkUncheckedPushd8 = verifyNotTree checkUncheckedCdPushdPopd "set -o errexit; pushd foo; rm bar"
+prop_checkUncheckedPushd9 = verifyNotTree checkUncheckedCdPushdPopd "pushd -n foo"
+prop_checkUncheckedPopd1 = verifyTree checkUncheckedCdPushdPopd "popd; rm -r foo"
+prop_checkUncheckedPopd2 = verifyNotTree checkUncheckedCdPushdPopd "popd || exit; rm -r foo"
+prop_checkUncheckedPopd3 = verifyNotTree checkUncheckedCdPushdPopd "set -e; popd; rm -r foo"
+prop_checkUncheckedPopd4 = verifyNotTree checkUncheckedCdPushdPopd "if popd; then rm foo; fi"
+prop_checkUncheckedPopd5 = verifyTree checkUncheckedCdPushdPopd "if true; then popd; fi"
+prop_checkUncheckedPopd6 = verifyTree checkUncheckedCdPushdPopd "popd"
+prop_checkUncheckedPopd7 = verifyNotTree checkUncheckedCdPushdPopd "#!/bin/bash -e\npopd\nrm bar"
+prop_checkUncheckedPopd8 = verifyNotTree checkUncheckedCdPushdPopd "set -o errexit; popd; rm bar"
+
+checkUncheckedCdPushdPopd params root =
+    if hasSetE params then
+        []
+    else execWriter $ doAnalysis checkElement root
+  where
+    checkElement t@T_SimpleCommand {} =
+        when(name t `elem` ["cd", "pushd", "popd"]
+            && not (isSafeDir t)
+            && not (name t == "pushd" && ("n" `elem` map snd (getAllFlags t)))
+            && not (isCondition $ getPath (parentMap params) t)) $
+                warn (getId t) 2164 "Use 'cd ... || exit' or 'cd ... || return' in case cd fails."
+    checkElement _ = return ()
+    name t = fromMaybe "" $ getCommandName t
+    isSafeDir t = case oversimplify t of
+          [_, ".."] -> True;
+          _ -> False
+
+prop_checkLoopVariableReassignment1 = verify checkLoopVariableReassignment "for i in *; do for i in *.bar; do true; done; done"
+prop_checkLoopVariableReassignment2 = verify checkLoopVariableReassignment "for i in *; do for((i=0; i<3; i++)); do true; done; done"
+prop_checkLoopVariableReassignment3 = verifyNot checkLoopVariableReassignment "for i in *; do for j in *.bar; do true; done; done"
+checkLoopVariableReassignment params token =
+    potentially $ case token of
+        T_ForIn {} -> check
+        T_ForArithmetic {} -> check
+        _ -> Nothing
+  where
+    check = do
+        str <- loopVariable token
+        next <- listToMaybe $ filter (\x -> loopVariable x == Just str) path
+        return $ do
+            warn (getId token) 2165 "This nested loop overrides the index variable of its parent."
+            warn (getId next)  2167 "This parent loop has its index variable overridden."
+    path = drop 1 $ getPath (parentMap params) token
+    loopVariable :: Token -> Maybe String
+    loopVariable t =
+        case t of
+            T_ForIn _ s _ _ -> return s
+            T_ForArithmetic _
+                (TA_Sequence _
+                    [TA_Assignment _ "="
+                        (TA_Variable _ var _ ) _])
+                            _ _ _ -> return var
+            _ -> fail "not loop"
+
+prop_checkTrailingBracket1 = verify checkTrailingBracket "if -z n ]]; then true; fi "
+prop_checkTrailingBracket2 = verifyNot checkTrailingBracket "if [[ -z n ]]; then true; fi "
+prop_checkTrailingBracket3 = verify checkTrailingBracket "a || b ] && thing"
+prop_checkTrailingBracket4 = verifyNot checkTrailingBracket "run [ foo ]"
+prop_checkTrailingBracket5 = verifyNot checkTrailingBracket "run bar ']'"
+checkTrailingBracket _ token =
+    case token of
+        T_SimpleCommand _ _ tokens@(_:_) -> check (last tokens) token
+        _ -> return ()
+  where
+    check t command =
+        case t of
+            T_NormalWord id [T_Literal _ str] -> potentially $ do
+                guard $ str `elem` [ "]]", "]" ]
+                let opposite = invert str
+                    parameters = oversimplify command
+                guard $ opposite `notElem` parameters
+                return $ warn id 2171 $
+                    "Found trailing " ++ str ++ " outside test. Missing " ++ opposite ++ "?"
+            _ -> return ()
+    invert s =
+        case s of
+            "]]" -> "[["
+            "]" -> "["
+            x -> x
+
+prop_checkReturnAgainstZero1 = verify checkReturnAgainstZero "[ $? -eq 0 ]"
+prop_checkReturnAgainstZero2 = verify checkReturnAgainstZero "[[ \"$?\" -gt 0 ]]"
+prop_checkReturnAgainstZero3 = verify checkReturnAgainstZero "[[ 0 -ne $? ]]"
+prop_checkReturnAgainstZero4 = verifyNot checkReturnAgainstZero "[[ $? -eq 4 ]]"
+prop_checkReturnAgainstZero5 = verify checkReturnAgainstZero "[[ 0 -eq $? ]]"
+prop_checkReturnAgainstZero6 = verifyNot checkReturnAgainstZero "[[ $R -eq 0 ]]"
+prop_checkReturnAgainstZero7 = verify checkReturnAgainstZero "(( $? == 0 ))"
+prop_checkReturnAgainstZero8 = verify checkReturnAgainstZero "(( $? ))"
+prop_checkReturnAgainstZero9 = verify checkReturnAgainstZero "(( ! $? ))"
+checkReturnAgainstZero _ token =
+    case token of
+        TC_Binary id _ _ lhs rhs -> check lhs rhs
+        TA_Binary id _ lhs rhs -> check lhs rhs
+        TA_Unary id _ exp ->
+            when (isExitCode exp) $ message (getId exp)
+        TA_Sequence _ [exp] ->
+            when (isExitCode exp) $ message (getId exp)
+        _ -> return ()
+  where
+    check lhs rhs =
+        if isZero rhs && isExitCode lhs
+        then message (getId lhs)
+        else when (isZero lhs && isExitCode rhs) $ message (getId rhs)
+    isZero t = getLiteralString t == Just "0"
+    isExitCode t =
+        case getWordParts t of
+            [exp@T_DollarBraced {}] -> bracedString exp == "?"
+            _ -> False
+    message id = style id 2181 "Check exit code directly with e.g. 'if mycmd;', not indirectly with $?."
+
+prop_checkRedirectedNowhere1 = verify checkRedirectedNowhere "> file"
+prop_checkRedirectedNowhere2 = verify checkRedirectedNowhere "> file | grep foo"
+prop_checkRedirectedNowhere3 = verify checkRedirectedNowhere "grep foo | > bar"
+prop_checkRedirectedNowhere4 = verifyNot checkRedirectedNowhere "grep foo > bar"
+prop_checkRedirectedNowhere5 = verifyNot checkRedirectedNowhere "foo | grep bar > baz"
+prop_checkRedirectedNowhere6 = verifyNot checkRedirectedNowhere "var=$(value) 2> /dev/null"
+prop_checkRedirectedNowhere7 = verifyNot checkRedirectedNowhere "var=$(< file)"
+prop_checkRedirectedNowhere8 = verifyNot checkRedirectedNowhere "var=`< file`"
+checkRedirectedNowhere params token =
+    case token of
+        T_Pipeline _ _ [single] -> potentially $ do
+            redir <- getDanglingRedirect single
+            guard . not $ isInExpansion token
+            return $ warn (getId redir) 2188 "This redirection doesn't have a command. Move to its command (or use 'true' as no-op)."
+
+        T_Pipeline _ _ list -> forM_ list $ \x -> potentially $ do
+            redir <- getDanglingRedirect x
+            return $ err (getId redir) 2189 "You can't have | between this redirection and the command it should apply to."
+
+        _ -> return ()
+  where
+    isInExpansion t =
+        case drop 1 $ getPath (parentMap params) t of
+            T_DollarExpansion _ [_] : _ -> True
+            T_Backticked _ [_] : _ -> True
+            T_Annotation _ _ u : _ -> isInExpansion u
+            _ -> False
+    getDanglingRedirect token =
+        case token of
+            T_Redirecting _ (first:_) (T_SimpleCommand _ [] []) -> return first
+            _ -> Nothing
+
+
+prop_checkArrayAssignmentIndices1 = verifyTree checkArrayAssignmentIndices "declare -A foo; foo=(bar)"
+prop_checkArrayAssignmentIndices2 = verifyNotTree checkArrayAssignmentIndices "declare -a foo; foo=(bar)"
+prop_checkArrayAssignmentIndices3 = verifyNotTree checkArrayAssignmentIndices "declare -A foo; foo=([i]=bar)"
+prop_checkArrayAssignmentIndices4 = verifyTree checkArrayAssignmentIndices "typeset -A foo; foo+=(bar)"
+prop_checkArrayAssignmentIndices5 = verifyTree checkArrayAssignmentIndices "arr=( [foo]= bar )"
+prop_checkArrayAssignmentIndices6 = verifyTree checkArrayAssignmentIndices "arr=( [foo] = bar )"
+prop_checkArrayAssignmentIndices7 = verifyTree checkArrayAssignmentIndices "arr=( var=value )"
+prop_checkArrayAssignmentIndices8 = verifyNotTree checkArrayAssignmentIndices "arr=( [foo]=bar )"
+prop_checkArrayAssignmentIndices9 = verifyNotTree checkArrayAssignmentIndices "arr=( [foo]=\"\" )"
+checkArrayAssignmentIndices params root =
+    runNodeAnalysis check params root
+  where
+    assocs = getAssociativeArrays root
+    check _ t =
+        case t of
+            T_Assignment _ _ name [] (T_Array _ list) ->
+                let isAssoc = name `elem` assocs in
+                    mapM_ (checkElement isAssoc) list
+            _ -> return ()
+
+    checkElement isAssociative t =
+        case t of
+            T_IndexedElement _ _ (T_Literal id "") ->
+                warn id 2192 "This array element has no value. Remove spaces after = or use \"\" for empty string."
+            T_IndexedElement {} ->
+                return ()
+
+            T_NormalWord _ parts ->
+                let literalEquals = do
+                    part <- parts
+                    (id, str) <- case part of
+                        T_Literal id str -> [(id,str)]
+                        _ -> []
+                    guard $ '=' `elem` str
+                    return $ warn id 2191 "The = here is literal. To assign by index, use ( [index]=value ) with no spaces. To keep as literal, quote it."
+                in
+                    if null literalEquals && isAssociative
+                    then warn (getId t) 2190 "Elements in associative arrays need index, e.g. array=( [index]=value ) ."
+                    else sequence_ literalEquals
+
+            _ -> return ()
+
+prop_checkUnmatchableCases1 = verify checkUnmatchableCases "case foo in bar) true; esac"
+prop_checkUnmatchableCases2 = verify checkUnmatchableCases "case foo-$bar in ??|*) true; esac"
+prop_checkUnmatchableCases3 = verify checkUnmatchableCases "case foo in foo) true; esac"
+prop_checkUnmatchableCases4 = verifyNot checkUnmatchableCases "case foo-$bar in foo*|*bar|*baz*) true; esac"
+prop_checkUnmatchableCases5 = verify checkUnmatchableCases "case $f in *.txt) true;; f??.txt) false;; esac"
+prop_checkUnmatchableCases6 = verifyNot checkUnmatchableCases "case $f in ?*) true;; *) false;; esac"
+prop_checkUnmatchableCases7 = verifyNot checkUnmatchableCases "case $f in $(x)) true;; asdf) false;; esac"
+prop_checkUnmatchableCases8 = verify checkUnmatchableCases "case $f in cow) true;; bar|cow) false;; esac"
+checkUnmatchableCases _ t =
+    case t of
+        T_CaseExpression _ word list -> do
+            let patterns = concatMap snd3 list
+
+            if isConstant word
+                then warn (getId word) 2194
+                        "This word is constant. Did you forget the $ on a variable?"
+                else  potentially $ do
+                    pg <- wordToPseudoGlob word
+                    return $ mapM_ (check pg) patterns
+
+            let exactGlobs = tupMap wordToExactPseudoGlob patterns
+            let fuzzyGlobs = tupMap wordToPseudoGlob patterns
+            let dominators = zip exactGlobs (tails $ drop 1 fuzzyGlobs)
+
+            mapM_ checkDoms dominators
+
+        _ -> return ()
+  where
+    snd3 (_,x,_) = x
+    check target candidate = potentially $ do
+        candidateGlob <- wordToPseudoGlob candidate
+        guard . not $ pseudoGlobsCanOverlap target candidateGlob
+        return $ warn (getId candidate) 2195
+                    "This pattern will never match the case statement's word. Double check them."
+
+    tupMap f l = zip l (map f l)
+    checkDoms ((glob, Just x), rest) =
+        case filter (\(_, p) -> x `pseudoGlobIsSuperSetof` p) valids of
+            ((first,_):_) -> do
+                warn (getId glob) 2221 "This pattern always overrides a later one."
+                warn (getId first) 2222 "This pattern never matches because of a previous pattern."
+            _ -> return ()
+      where
+        valids = concatMap f rest
+        f (x, Just y) = [(x,y)]
+        f _ = []
+    checkDoms _ = return ()
+
+
+prop_checkSubshellAsTest1 = verify checkSubshellAsTest "( -e file )"
+prop_checkSubshellAsTest2 = verify checkSubshellAsTest "( 1 -gt 2 )"
+prop_checkSubshellAsTest3 = verifyNot checkSubshellAsTest "( grep -c foo bar )"
+prop_checkSubshellAsTest4 = verifyNot checkSubshellAsTest "[ 1 -gt 2 ]"
+prop_checkSubshellAsTest5 = verify checkSubshellAsTest "( -e file && -x file )"
+prop_checkSubshellAsTest6 = verify checkSubshellAsTest "( -e file || -x file && -t 1 )"
+prop_checkSubshellAsTest7 = verify checkSubshellAsTest "( ! -d file )"
+checkSubshellAsTest _ t =
+    case t of
+        T_Subshell id [w] -> check id w
+        _ -> return ()
+  where
+    check id t = case t of
+        (T_Banged _ w) -> check id w
+        (T_AndIf _ w _) -> check id w
+        (T_OrIf _ w _) -> check id w
+        (T_Pipeline _ _ [T_Redirecting _ _ (T_SimpleCommand _ [] (first:second:_))]) ->
+            checkParams id first second
+        _ -> return ()
+
+
+    checkParams id first second = do
+        when (fromMaybe False $ (`elem` unaryTestOps) <$> getLiteralString first) $
+            err id 2204 "(..) is a subshell. Did you mean [ .. ], a test expression?"
+        when (fromMaybe False $ (`elem` binaryTestOps) <$> getLiteralString second) $
+            warn id 2205 "(..) is a subshell. Did you mean [ .. ], a test expression?"
+
+
+prop_checkSplittingInArrays1 = verify checkSplittingInArrays "a=( $var )"
+prop_checkSplittingInArrays2 = verify checkSplittingInArrays "a=( $(cmd) )"
+prop_checkSplittingInArrays3 = verifyNot checkSplittingInArrays "a=( \"$var\" )"
+prop_checkSplittingInArrays4 = verifyNot checkSplittingInArrays "a=( \"$(cmd)\" )"
+prop_checkSplittingInArrays5 = verifyNot checkSplittingInArrays "a=( $! $$ $# )"
+prop_checkSplittingInArrays6 = verifyNot checkSplittingInArrays "a=( ${#arr[@]} )"
+prop_checkSplittingInArrays7 = verifyNot checkSplittingInArrays "a=( foo{1,2} )"
+prop_checkSplittingInArrays8 = verifyNot checkSplittingInArrays "a=( * )"
+checkSplittingInArrays params t =
+    case t of
+        T_Array _ elements -> mapM_ check elements
+        _ -> return ()
+  where
+    check word = case word of
+        T_NormalWord _ parts -> mapM_ checkPart parts
+        _ -> return ()
+    checkPart part = case part of
+        T_DollarExpansion id _ -> forCommand id
+        T_DollarBraceCommandExpansion id _ -> forCommand id
+        T_Backticked id _ -> forCommand id
+        T_DollarBraced id str |
+            not (isCountingReference part)
+            && not (isQuotedAlternativeReference part)
+            && not (getBracedReference (bracedString part) `elem` variablesWithoutSpaces)
+            -> warn id 2206 $
+                if shellType params == Ksh
+                then "Quote to prevent word splitting, or split robustly with read -A or while read."
+                else "Quote to prevent word splitting, or split robustly with mapfile or read -a."
+        _ -> return ()
+
+    forCommand id =
+        warn id 2207 $
+            if shellType params == Ksh
+            then "Prefer read -A or while read to split command output (or quote to avoid splitting)."
+            else "Prefer mapfile or read -a to split command output (or quote to avoid splitting)."
+
+
+prop_checkRedirectionToNumber1 = verify checkRedirectionToNumber "( 1 > 2 )"
+prop_checkRedirectionToNumber2 = verify checkRedirectionToNumber "foo 1>2"
+prop_checkRedirectionToNumber3 = verifyNot checkRedirectionToNumber "echo foo > '2'"
+prop_checkRedirectionToNumber4 = verifyNot checkRedirectionToNumber "foo 1>&2"
+checkRedirectionToNumber _ t = case t of
+    T_IoFile id _ word -> potentially $ do
+        file <- getUnquotedLiteral word
+        guard $ all isDigit file
+        return $ warn id 2210 "This is a file redirection. Was it supposed to be a comparison or fd operation?"
+    _ -> return ()
+
+prop_checkGlobAsCommand1 = verify checkGlobAsCommand "foo*"
+prop_checkGlobAsCommand2 = verify checkGlobAsCommand "$(var[i])"
+prop_checkGlobAsCommand3 = verifyNot checkGlobAsCommand "echo foo*"
+checkGlobAsCommand _ t = case t of
+    T_SimpleCommand _ _ (first:_) ->
+        when (isGlob first) $
+            warn (getId first) 2211 "This is a glob used as a command name. Was it supposed to be in ${..}, array, or is it missing quoting?"
+    _ -> return ()
+
+
+prop_checkFlagAsCommand1 = verify checkFlagAsCommand "-e file"
+prop_checkFlagAsCommand2 = verify checkFlagAsCommand "foo\n  --bar=baz"
+prop_checkFlagAsCommand3 = verifyNot checkFlagAsCommand "'--myexec--' args"
+prop_checkFlagAsCommand4 = verifyNot checkFlagAsCommand "var=cmd --arg"  -- Handled by SC2037
+checkFlagAsCommand _ t = case t of
+    T_SimpleCommand _ [] (first:_) ->
+        when (isUnquotedFlag first) $
+            warn (getId first) 2215 "This flag is used as a command name. Bad line break or missing [ .. ]?"
+    _ -> return ()
+
+
+prop_checkEmptyCondition1 = verify checkEmptyCondition "if [ ]; then ..; fi"
+prop_checkEmptyCondition2 = verifyNot checkEmptyCondition "[ foo -o bar ]"
+checkEmptyCondition _ t = case t of
+    TC_Empty id _ -> style id 2212 "Use 'false' instead of empty [/[[ conditionals."
+    _ -> return ()
+
+prop_checkPipeToNowhere1 = verify checkPipeToNowhere "foo | echo bar"
+prop_checkPipeToNowhere2 = verify checkPipeToNowhere "basename < file.txt"
+prop_checkPipeToNowhere3 = verify checkPipeToNowhere "printf 'Lol' <<< str"
+prop_checkPipeToNowhere4 = verify checkPipeToNowhere "printf 'Lol' << eof\nlol\neof\n"
+prop_checkPipeToNowhere5 = verifyNot checkPipeToNowhere "echo foo | xargs du"
+prop_checkPipeToNowhere6 = verifyNot checkPipeToNowhere "ls | echo $(cat)"
+prop_checkPipeToNowhere7 = verifyNot checkPipeToNowhere "echo foo | var=$(cat) ls"
+prop_checkPipeToNowhere8 = verify checkPipeToNowhere "foo | true"
+checkPipeToNowhere :: Parameters -> Token -> WriterT [TokenComment] Identity ()
+checkPipeToNowhere _ t =
+    case t of
+        T_Pipeline _ _ (first:rest) -> mapM_ checkPipe rest
+        T_Redirecting _ redirects cmd -> when (any redirectsStdin redirects) $ checkRedir cmd
+        _ -> return ()
+  where
+    checkPipe redir = potentially $ do
+        cmd <- getCommand redir
+        name <- getCommandBasename cmd
+        guard $ name `elem` nonReadingCommands
+        guard . not $ hasAdditionalConsumers cmd
+        return $ warn (getId cmd) 2216 $
+            "Piping to '" ++ name ++ "', a command that doesn't read stdin. Wrong command or missing xargs?"
+
+    checkRedir cmd = potentially $ do
+        name <- getCommandBasename cmd
+        guard $ name `elem` nonReadingCommands
+        guard . not $ hasAdditionalConsumers cmd
+        return $ warn (getId cmd) 2217 $
+            "Redirecting to '" ++ name ++ "', a command that doesn't read stdin. Bad quoting or missing xargs?"
+
+    -- Could any words in a SimpleCommand consume stdin (e.g. echo "$(cat)")?
+    hasAdditionalConsumers t = fromMaybe True $ do
+        doAnalysis (guard . not . mayConsume) t
+        return False
+
+    mayConsume t =
+        case t of
+            T_ProcSub {} -> True
+            T_Backticked {} -> True
+            T_DollarExpansion {} -> True
+            _ -> False
+
+    redirectsStdin t =
+        case t of
+            T_FdRedirect _ _ (T_IoFile _ T_Less {} _) -> True
+            T_FdRedirect _ _ T_HereDoc {} -> True
+            T_FdRedirect _ _ T_HereString {} -> True
+            _ -> False
+
+prop_checkUseBeforeDefinition1 = verifyTree checkUseBeforeDefinition "f; f() { true; }"
+prop_checkUseBeforeDefinition2 = verifyNotTree checkUseBeforeDefinition "f() { true; }; f"
+prop_checkUseBeforeDefinition3 = verifyNotTree checkUseBeforeDefinition "if ! mycmd --version; then mycmd() { true; }; fi"
+prop_checkUseBeforeDefinition4 = verifyNotTree checkUseBeforeDefinition "mycmd || mycmd() { f; }"
+checkUseBeforeDefinition _ t =
+    execWriter $ evalStateT (mapM_ examine $ revCommands) Map.empty
+  where
+    examine t = case t of
+        T_Pipeline _ _ [T_Redirecting _ _ (T_Function _ _ _ name _)] ->
+            modify $ Map.insert name t
+        T_Annotation _ _ w -> examine w
+        T_Pipeline _ _ cmds -> do
+            m <- get
+            unless (Map.null m) $
+                mapM_ (checkUsage m) $ concatMap recursiveSequences cmds
+        _ -> return ()
+
+    checkUsage map cmd = potentially $ do
+        name <- getCommandName cmd
+        def <- Map.lookup name map
+        return $
+            err (getId cmd) 2218
+                "This function is only defined later. Move the definition up."
+
+    revCommands = reverse $ concat $ getCommandSequences t
+    recursiveSequences x =
+        let list = concat $ getCommandSequences x in
+            if null list
+            then [x]
+            else concatMap recursiveSequences list
+
+prop_checkForLoopGlobVariables1 = verify checkForLoopGlobVariables "for i in $var/*.txt; do true; done"
+prop_checkForLoopGlobVariables2 = verifyNot checkForLoopGlobVariables "for i in \"$var\"/*.txt; do true; done"
+prop_checkForLoopGlobVariables3 = verifyNot checkForLoopGlobVariables "for i in $var; do true; done"
+checkForLoopGlobVariables _ t =
+    case t of
+        T_ForIn _ _ words _ -> mapM_ check words
+        _ -> return ()
+  where
+    check (T_NormalWord _ parts) =
+        when (any isGlob parts) $
+            mapM_ suggest $ filter isQuoteableExpansion parts
+    suggest t = info (getId t) 2231
+        "Quote expansions in this for loop glob to prevent wordsplitting, e.g. \"$dir\"/*.txt ."
+
+prop_checkSubshelledTests1 = verify checkSubshelledTests "a && ( [ b ] || ! [ c ] )"
+prop_checkSubshelledTests2 = verify checkSubshelledTests "( [ a ] )"
+prop_checkSubshelledTests3 = verify checkSubshelledTests "( [ a ] && [ b ] || test c )"
+checkSubshelledTests params t =
+    case t of
+        T_Subshell id list | isSubshelledTest t ->
+            case () of
+                -- Special case for if (test) and while (test)
+                _ | isCompoundCondition (getPath (parentMap params) t) ->
+                    style id 2233 "Remove superfluous (..) around condition."
+
+                -- Special case for ([ x ])
+                _ | isSingleTest list ->
+                    style id 2234 "Remove superfluous (..) around test command."
+
+                -- General case for ([ x ] || [ y ] && etc)
+                _ -> style id 2235 "Use { ..; } instead of (..) to avoid subshell overhead."
+        _ -> return ()
+  where
+
+    isSingleTest cmds =
+        case cmds of
+            [c] | isTestCommand c -> True
+            _ -> False
+
+    isSubshelledTest t =
+        case t of
+            T_Subshell _ list -> all isSubshelledTest list
+            T_AndIf _ a b -> isSubshelledTest a && isSubshelledTest b
+            T_OrIf  _ a b -> isSubshelledTest a && isSubshelledTest b
+            T_Annotation _ _ t -> isSubshelledTest t
+            _ -> isTestCommand t
+
+    isTestCommand t =
+        case t of
+            T_Banged _ t -> isTestCommand t
+            T_Pipeline _ [] [T_Redirecting _ _ T_Condition {}] -> True
+            T_Pipeline _ [] [T_Redirecting _ _ cmd] -> cmd `isCommand` "test"
+            _ -> False
+
+    -- Check if a T_Subshell is used as a condition, e.g. if ( test )
+    -- This technically also triggers for `if true; then ( test ); fi`
+    -- but it's still a valid suggestion.
+    isCompoundCondition chain =
+        case dropWhile skippable (drop 1 chain) of
+            T_IfExpression {}    : _ -> True
+            T_WhileExpression {} : _ -> True
+            T_UntilExpression {} : _ -> True
+            _ -> False
+
+    -- Skip any parent of a T_Subshell until we reach something interesting
+    skippable t =
+        case t of
+            T_Redirecting _ [] _ -> True
+            T_Pipeline _ [] _ -> True
+            T_Annotation {} -> True
+            _ -> False
+
+return []
+runTests =  $( [| $(forAllProperties) (quickCheckWithResult (stdArgs { maxSuccess = 1 }) ) |])
diff --git a/src/ShellCheck/Analyzer.hs b/src/ShellCheck/Analyzer.hs
new file mode 100644
--- /dev/null
+++ b/src/ShellCheck/Analyzer.hs
@@ -0,0 +1,45 @@
+{-
+    Copyright 2012-2015 Vidar Holen
+
+    This file is part of ShellCheck.
+    https://www.shellcheck.net
+
+    ShellCheck is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    ShellCheck is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <https://www.gnu.org/licenses/>.
+-}
+module ShellCheck.Analyzer (analyzeScript) where
+
+import ShellCheck.Analytics
+import ShellCheck.AnalyzerLib
+import ShellCheck.Interface
+import Data.List
+import Data.Monoid
+import qualified ShellCheck.Checks.Commands
+import qualified ShellCheck.Checks.ShellSupport
+
+
+-- TODO: Clean up the cruft this is layered on
+analyzeScript :: AnalysisSpec -> AnalysisResult
+analyzeScript spec = AnalysisResult {
+    arComments =
+        filterByAnnotation spec params . nub $
+            runAnalytics spec
+            ++ runChecker params (checkers params)
+}
+  where
+    params = makeParameters spec
+
+checkers params = mconcat $ map ($ params) [
+    ShellCheck.Checks.Commands.checker,
+    ShellCheck.Checks.ShellSupport.checker
+    ]
diff --git a/src/ShellCheck/AnalyzerLib.hs b/src/ShellCheck/AnalyzerLib.hs
new file mode 100644
--- /dev/null
+++ b/src/ShellCheck/AnalyzerLib.hs
@@ -0,0 +1,879 @@
+{-
+    Copyright 2012-2015 Vidar Holen
+
+    This file is part of ShellCheck.
+    https://www.shellcheck.net
+
+    ShellCheck is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    ShellCheck is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <https://www.gnu.org/licenses/>.
+-}
+{-# LANGUAGE FlexibleContexts #-}
+{-# LANGUAGE TemplateHaskell  #-}
+module ShellCheck.AnalyzerLib where
+import           ShellCheck.AST
+import           ShellCheck.ASTLib
+import           ShellCheck.Data
+import           ShellCheck.Interface
+import           ShellCheck.Parser
+import           ShellCheck.Regex
+
+import           Control.Arrow          (first)
+import           Control.Monad.Identity
+import           Control.Monad.RWS
+import           Control.Monad.State
+import           Control.Monad.Writer
+import           Data.Char
+import           Data.List
+import qualified Data.Map               as Map
+import           Data.Maybe
+import           Data.Semigroup
+
+import           Test.QuickCheck.All    (forAllProperties)
+import           Test.QuickCheck.Test   (maxSuccess, quickCheckWithResult, stdArgs)
+
+type Analysis = AnalyzerM ()
+type AnalyzerM a = RWS Parameters [TokenComment] Cache a
+nullCheck = const $ return ()
+
+
+data Checker = Checker {
+    perScript :: Root -> Analysis,
+    perToken  :: Token -> Analysis
+}
+
+runChecker :: Parameters -> Checker -> [TokenComment]
+runChecker params checker = notes
+    where
+        root = rootNode params
+        check = perScript checker `composeAnalyzers` (\(Root x) -> void $ doAnalysis (perToken checker) x)
+        notes = snd $ evalRWS (check $ Root root) params Cache
+
+instance Semigroup Checker where
+    (<>) x y = Checker {
+        perScript = perScript x `composeAnalyzers` perScript y,
+        perToken = perToken x `composeAnalyzers` perToken y
+        }
+
+instance Monoid Checker where
+    mempty = Checker {
+        perScript = nullCheck,
+        perToken = nullCheck
+        }
+    mappend = (Data.Semigroup.<>)
+
+composeAnalyzers :: (a -> Analysis) -> (a -> Analysis) -> a -> Analysis
+composeAnalyzers f g x = f x >> g x
+
+data Parameters = Parameters {
+    hasLastpipe        :: Bool,           -- Whether this script has the 'lastpipe' option set/default.
+    hasSetE            :: Bool,           -- Whether this script has 'set -e' anywhere.
+    variableFlow       :: [StackData],   -- A linear (bad) analysis of data flow
+    parentMap          :: Map.Map Id Token, -- A map from Id to parent Token
+    shellType          :: Shell,            -- The shell type, such as Bash or Ksh
+    shellTypeSpecified :: Bool,    -- True if shell type was forced via flags
+    rootNode           :: Token              -- The root node of the AST
+    }
+
+-- TODO: Cache results of common AST ops here
+data Cache = Cache {}
+
+data Scope = SubshellScope String | NoneScope deriving (Show, Eq)
+data StackData =
+    StackScope Scope
+    | StackScopeEnd
+    -- (Base expression, specific position, var name, assigned values)
+    | Assignment (Token, Token, String, DataType)
+    | Reference (Token, Token, String)
+  deriving (Show)
+
+data DataType = DataString DataSource | DataArray DataSource
+  deriving (Show)
+
+data DataSource =
+    SourceFrom [Token]
+    | SourceExternal
+    | SourceDeclaration
+    | SourceInteger
+    | SourceChecked
+  deriving (Show)
+
+data VariableState = Dead Token String | Alive deriving (Show)
+
+defaultSpec root = AnalysisSpec {
+    asScript = root,
+    asShellType = Nothing,
+    asCheckSourced = False,
+    asExecutionMode = Executed
+}
+
+pScript s =
+  let
+    pSpec = ParseSpec {
+        psFilename = "script",
+        psScript = s,
+        psCheckSourced = False
+    }
+  in prRoot . runIdentity $ parseScript (mockedSystemInterface []) pSpec
+
+-- For testing. If parsed, returns whether there are any comments
+producesComments :: Checker -> String -> Maybe Bool
+producesComments c s = do
+        root <- pScript s
+        let spec = defaultSpec root
+        let params = makeParameters spec
+        return . not . null $ runChecker params c
+
+makeComment :: Severity -> Id -> Code -> String -> TokenComment
+makeComment severity id code note =
+    TokenComment id $ Comment severity code note
+
+addComment note = tell [note]
+
+warn :: MonadWriter [TokenComment] m => Id -> Code -> String -> m ()
+warn  id code str = addComment $ makeComment WarningC id code str
+err   id code str = addComment $ makeComment ErrorC id code str
+info  id code str = addComment $ makeComment InfoC id code str
+style id code str = addComment $ makeComment StyleC id code str
+
+makeParameters spec =
+    let params = Parameters {
+        rootNode = root,
+        shellType = fromMaybe (determineShell root) $ asShellType spec,
+        hasSetE = containsSetE root,
+        hasLastpipe =
+            case shellType params of
+                Bash -> containsLastpipe root
+                Dash -> False
+                Sh   -> False
+                Ksh  -> True,
+
+        shellTypeSpecified = isJust $ asShellType spec,
+        parentMap = getParentTree root,
+        variableFlow = getVariableFlow params root
+    } in params
+  where root = asScript spec
+
+
+-- Does this script mention 'set -e' anywhere?
+-- Used as a hack to disable certain warnings.
+containsSetE root = isNothing $ doAnalysis (guard . not . isSetE) root
+  where
+    isSetE t =
+        case t of
+            T_Script _ str _ -> str `matches` re
+            T_SimpleCommand {}  ->
+                t `isUnqualifiedCommand` "set" &&
+                    ("errexit" `elem` oversimplify t ||
+                        "e" `elem` map snd (getAllFlags t))
+            _ -> False
+    re = mkRegex "[[:space:]]-[^-]*e"
+
+-- Does this script mention 'shopt -s lastpipe' anywhere?
+-- Also used as a hack.
+containsLastpipe root =
+        isNothing $ doAnalysis (guard . not . isShoptLastPipe) root
+    where
+        isShoptLastPipe t =
+            case t of
+                T_SimpleCommand {}  ->
+                    t `isUnqualifiedCommand` "shopt" &&
+                        ("lastpipe" `elem` oversimplify t)
+                _ -> False
+
+
+prop_determineShell0 = determineShell (fromJust $ pScript "#!/bin/sh") == Sh
+prop_determineShell1 = determineShell (fromJust $ pScript "#!/usr/bin/env ksh") == Ksh
+prop_determineShell2 = determineShell (fromJust $ pScript "") == Bash
+prop_determineShell3 = determineShell (fromJust $ pScript "#!/bin/sh -e") == Sh
+prop_determineShell4 = determineShell (fromJust $ pScript
+    "#!/bin/ksh\n#shellcheck shell=sh\nfoo") == Sh
+prop_determineShell5 = determineShell (fromJust $ pScript
+    "#shellcheck shell=sh\nfoo") == Sh
+prop_determineShell6 = determineShell (fromJust $ pScript "#! /bin/sh") == Sh
+prop_determineShell7 = determineShell (fromJust $ pScript "#! /bin/ash") == Dash
+determineShell t = fromMaybe Bash $ do
+    shellString <- foldl mplus Nothing $ getCandidates t
+    shellForExecutable shellString
+  where
+    forAnnotation t =
+        case t of
+            (ShellOverride s) -> return s
+            _                 -> fail ""
+    getCandidates :: Token -> [Maybe String]
+    getCandidates t@T_Script {} = [Just $ fromShebang t]
+    getCandidates (T_Annotation _ annotations s) =
+        map forAnnotation annotations ++
+           [Just $ fromShebang s]
+    fromShebang (T_Script _ s t) = executableFromShebang s
+
+-- Given a string like "/bin/bash" or "/usr/bin/env dash",
+-- return the shell basename like "bash" or "dash"
+executableFromShebang :: String -> String
+executableFromShebang = shellFor
+  where
+    shellFor s | "/env " `isInfixOf` s = head (drop 1 (words s)++[""])
+    shellFor s | ' ' `elem` s = shellFor $ takeWhile (/= ' ') s
+    shellFor s = reverse . takeWhile (/= '/') . reverse $ s
+
+
+
+-- Given a root node, make a map from Id to parent Token.
+-- This is used to populate parentMap in Parameters
+getParentTree :: Token -> Map.Map Id Token
+getParentTree t =
+    snd . snd $ runState (doStackAnalysis pre post t) ([], Map.empty)
+  where
+    pre t = modify (first ((:) t))
+    post t = do
+        (_:rest, map) <- get
+        case rest of []    -> put (rest, map)
+                     (x:_) -> put (rest, Map.insert (getId t) x map)
+
+-- Given a root node, make a map from Id to Token
+getTokenMap :: Token -> Map.Map Id Token
+getTokenMap t =
+    execState (doAnalysis f t) Map.empty
+  where
+    f t = modify (Map.insert (getId t) t)
+
+
+-- Is this token in a quoting free context? (i.e. would variable expansion split)
+-- True:  Assignments, [[ .. ]], here docs, already in double quotes
+-- False: Regular words
+isStrictlyQuoteFree = isQuoteFreeNode True
+
+-- Like above, but also allow some cases where splitting may be desired.
+-- True:  Like above + for loops
+-- False: Like above
+isQuoteFree = isQuoteFreeNode False
+
+
+isQuoteFreeNode strict tree t =
+    (isQuoteFreeElement t == Just True) ||
+        head (mapMaybe isQuoteFreeContext (drop 1 $ getPath tree t) ++ [False])
+  where
+    -- Is this node self-quoting in itself?
+    isQuoteFreeElement t =
+        case t of
+            T_Assignment {} -> return True
+            T_FdRedirect {} -> return True
+            _               -> Nothing
+
+    -- Are any subnodes inherently self-quoting?
+    isQuoteFreeContext t =
+        case t of
+            TC_Nullary _ DoubleBracket _    -> return True
+            TC_Unary _ DoubleBracket _ _    -> return True
+            TC_Binary _ DoubleBracket _ _ _ -> return True
+            TA_Sequence {}                  -> return True
+            T_Arithmetic {}                 -> return True
+            T_Assignment {}                 -> return True
+            T_Redirecting {}                -> return False
+            T_DoubleQuoted _ _              -> return True
+            T_DollarDoubleQuoted _ _        -> return True
+            T_CaseExpression {}             -> return True
+            T_HereDoc {}                    -> return True
+            T_DollarBraced {}               -> return True
+            -- When non-strict, pragmatically assume it's desirable to split here
+            T_ForIn {}                      -> return (not strict)
+            T_SelectIn {}                   -> return (not strict)
+            _                               -> Nothing
+
+-- Check if a token is a parameter to a certain command by name:
+-- Example: isParamTo (parentMap params) "sed" t
+isParamTo :: Map.Map Id Token -> String -> Token -> Bool
+isParamTo tree cmd =
+    go
+  where
+    go x = case Map.lookup (getId x) tree of
+                Nothing     -> False
+                Just parent -> check parent
+    check t =
+        case t of
+            T_SingleQuoted _ _ -> go t
+            T_DoubleQuoted _ _ -> go t
+            T_NormalWord _ _   -> go t
+            T_SimpleCommand {} -> isCommand t cmd
+            T_Redirecting {}   -> isCommand t cmd
+            _                  -> False
+
+-- Get the parent command (T_Redirecting) of a Token, if any.
+getClosestCommand :: Map.Map Id Token -> Token -> Maybe Token
+getClosestCommand tree t =
+    findFirst findCommand $ getPath tree t
+  where
+    findCommand t =
+        case t of
+            T_Redirecting {} -> return True
+            T_Script {}      -> return False
+            _                -> Nothing
+
+-- Like above, if koala_man knew Haskell when starting this project.
+getClosestCommandM t = do
+    tree <- asks parentMap
+    return $ getClosestCommand tree t
+
+-- Is the token used as a command name (the first word in a T_SimpleCommand)?
+usedAsCommandName tree token = go (getId token) (tail $ getPath tree token)
+  where
+    go currentId (T_NormalWord id [word]:rest)
+        | currentId == getId word = go id rest
+    go currentId (T_DoubleQuoted id [word]:rest)
+        | currentId == getId word = go id rest
+    go currentId (T_SimpleCommand _ _ (word:_):_)
+        | currentId == getId word = True
+    go _ _ = False
+
+-- A list of the element and all its parents up to the root node.
+getPath tree t = t :
+    case Map.lookup (getId t) tree of
+        Nothing     -> []
+        Just parent -> getPath tree parent
+
+-- Version of the above taking the map from the current context
+-- Todo: give this the name "getPath"
+getPathM t = do
+    map <- asks parentMap
+    return $ getPath map t
+
+isParentOf tree parent child =
+    elem (getId parent) . map getId $ getPath tree child
+
+parents params = getPath (parentMap params)
+
+pathTo t = do
+    parents <- reader parentMap
+    return $ getPath parents t
+
+-- Find the first match in a list where the predicate is Just True.
+-- Stops if it's Just False and ignores Nothing.
+findFirst :: (a -> Maybe Bool) -> [a] -> Maybe a
+findFirst p l =
+    case l of
+        [] -> Nothing
+        (x:xs) ->
+            case p x of
+                Just True  -> return x
+                Just False -> Nothing
+                Nothing    -> findFirst p xs
+
+-- Check whether a word is entirely output from a single command
+tokenIsJustCommandOutput t = case t of
+    T_NormalWord id [T_DollarExpansion _ cmds] -> check cmds
+    T_NormalWord id [T_DoubleQuoted _ [T_DollarExpansion _ cmds]] -> check cmds
+    T_NormalWord id [T_Backticked _ cmds] -> check cmds
+    T_NormalWord id [T_DoubleQuoted _ [T_Backticked _ cmds]] -> check cmds
+    _ -> False
+  where
+    check [x] = not $ isOnlyRedirection x
+    check _   = False
+
+-- TODO: Replace this with a proper Control Flow Graph
+getVariableFlow params t =
+    let (_, stack) = runState (doStackAnalysis startScope endScope t) []
+    in reverse stack
+  where
+    startScope t =
+        let scopeType = leadType params t
+        in do
+            when (scopeType /= NoneScope) $ modify (StackScope scopeType:)
+            when (assignFirst t) $ setWritten t
+
+    endScope t =
+        let scopeType = leadType params t
+        in do
+            setRead t
+            unless (assignFirst t) $ setWritten t
+            when (scopeType /= NoneScope) $ modify (StackScopeEnd:)
+
+    assignFirst T_ForIn {}    = True
+    assignFirst T_SelectIn {} = True
+    assignFirst _             = False
+
+    setRead t =
+        let read    = getReferencedVariables (parentMap params) t
+        in mapM_ (\v -> modify (Reference v:)) read
+
+    setWritten t =
+        let written = getModifiedVariables t
+        in mapM_ (\v -> modify (Assignment v:)) written
+
+
+leadType params t =
+    case t of
+        T_DollarExpansion _ _  -> SubshellScope "$(..) expansion"
+        T_Backticked _ _  -> SubshellScope "`..` expansion"
+        T_Backgrounded _ _  -> SubshellScope "backgrounding &"
+        T_Subshell _ _  -> SubshellScope "(..) group"
+        T_CoProcBody _ _  -> SubshellScope "coproc"
+        T_Redirecting {}  ->
+            if fromMaybe False causesSubshell
+            then SubshellScope "pipeline"
+            else NoneScope
+        _ -> NoneScope
+  where
+    parentPipeline = do
+        parent <- Map.lookup (getId t) (parentMap params)
+        case parent of
+            T_Pipeline {} -> return parent
+            _             -> Nothing
+
+    causesSubshell = do
+        (T_Pipeline _ _ list) <- parentPipeline
+        if length list <= 1
+            then return False
+            else if not $ hasLastpipe params
+                then return True
+                else return . not $ (getId . head $ reverse list) == getId t
+
+getModifiedVariables t =
+    case t of
+        T_SimpleCommand _ vars [] ->
+            concatMap (\x -> case x of
+                                T_Assignment id _ name _ w  ->
+                                    [(x, x, name, dataTypeFrom DataString w)]
+                                _ -> []
+                      ) vars
+        c@T_SimpleCommand {} ->
+            getModifiedVariableCommand c
+
+        TA_Unary _ "++|" v@(TA_Variable _ name _)  ->
+            [(t, v, name, DataString $ SourceFrom [v])]
+        TA_Unary _ "|++" v@(TA_Variable _ name _)  ->
+            [(t, v, name, DataString $ SourceFrom [v])]
+        TA_Assignment _ op (TA_Variable _ name _) rhs -> maybeToList $ do
+            guard $ op `elem` ["=", "*=", "/=", "%=", "+=", "-=", "<<=", ">>=", "&=", "^=", "|="]
+            return (t, t, name, DataString $ SourceFrom [rhs])
+
+        -- Count [[ -v foo ]] as an "assignment".
+        -- This is to prevent [ -v foo ] being unassigned or unused.
+        TC_Unary id _ "-v" token -> maybeToList $ do
+            str <- fmap (takeWhile (/= '[')) $ -- Quoted index
+                    flip getLiteralStringExt token $ \x ->
+                case x of
+                    T_Glob _ s -> return s -- Unquoted index
+                    _          -> Nothing
+
+            guard . not . null $ str
+            return (t, token, str, DataString SourceChecked)
+
+        T_DollarBraced _ l -> maybeToList $ do
+            let string = bracedString t
+            let modifier = getBracedModifier string
+            guard $ ":=" `isPrefixOf` modifier
+            return (t, t, getBracedReference string, DataString $ SourceFrom [l])
+
+        t@(T_FdRedirect _ ('{':var) op) -> -- {foo}>&2 modifies foo
+            [(t, t, takeWhile (/= '}') var, DataString SourceInteger) | not $ isClosingFileOp op]
+
+        t@(T_CoProc _ name _) ->
+            [(t, t, fromMaybe "COPROC" name, DataArray SourceInteger)]
+
+        --Points to 'for' rather than variable
+        T_ForIn id str [] _ -> [(t, t, str, DataString SourceExternal)]
+        T_ForIn id str words _ -> [(t, t, str, DataString $ SourceFrom words)]
+        T_SelectIn id str words _ -> [(t, t, str, DataString $ SourceFrom words)]
+        _ -> []
+
+isClosingFileOp op =
+    case op of
+        T_IoDuplicate _ (T_GREATAND _) "-" -> True
+        T_IoDuplicate _ (T_LESSAND  _) "-" -> True
+        _                                  -> False
+
+
+-- Consider 'export/declare -x' a reference, since it makes the var available
+getReferencedVariableCommand base@(T_SimpleCommand _ _ (T_NormalWord _ (T_Literal _ x:_):rest)) =
+    case x of
+        "export" -> if "f" `elem` flags
+            then []
+            else concatMap getReference rest
+        "declare" -> if
+                any (`elem` flags) ["x", "p"] &&
+                    (not $ any (`elem` flags) ["f", "F"])
+            then concatMap getReference rest
+            else []
+        "readonly" ->
+            if any (`elem` flags) ["f", "p"]
+            then []
+            else concatMap getReference rest
+        "trap" ->
+            case rest of
+                head:_ -> map (\x -> (head, head, x)) $ getVariablesFromLiteralToken head
+                _ -> []
+        _ -> []
+  where
+    getReference t@(T_Assignment _ _ name _ value) = [(t, t, name)]
+    getReference t@(T_NormalWord _ [T_Literal _ name]) | not ("-" `isPrefixOf` name) = [(t, t, name)]
+    getReference _ = []
+    flags = map snd $ getAllFlags base
+
+getReferencedVariableCommand _ = []
+
+getModifiedVariableCommand base@(T_SimpleCommand _ _ (T_NormalWord _ (T_Literal _ x:_):rest)) =
+   filter (\(_,_,s,_) -> not ("-" `isPrefixOf` s)) $
+    case x of
+        "read" ->
+            let params = map getLiteral rest in
+                catMaybes . takeWhile isJust . reverse $ params
+        "getopts" ->
+            case rest of
+                opts:var:_ -> maybeToList $ getLiteral var
+                _          -> []
+
+        "let" -> concatMap letParamToLiteral rest
+
+        "export" ->
+            if "f" `elem` flags then [] else concatMap getModifierParamString rest
+
+        "declare" -> if any (`elem` flags) ["F", "f", "p"] then [] else declaredVars
+        "typeset" -> declaredVars
+
+        "local" -> concatMap getModifierParamString rest
+        "readonly" ->
+            if any (`elem` flags) ["f", "p"]
+            then []
+            else concatMap getModifierParamString rest
+        "set" -> maybeToList $ do
+            params <- getSetParams rest
+            return (base, base, "@", DataString $ SourceFrom params)
+
+        "printf" -> maybeToList $ getPrintfVariable rest
+
+        "mapfile" -> maybeToList $ getMapfileArray base rest
+        "readarray" -> maybeToList $ getMapfileArray base rest
+
+        _ -> []
+  where
+    flags = map snd $ getAllFlags base
+    stripEquals s = let rest = dropWhile (/= '=') s in
+        if rest == "" then "" else tail rest
+    stripEqualsFrom (T_NormalWord id1 (T_Literal id2 s:rs)) =
+        T_NormalWord id1 (T_Literal id2 (stripEquals s):rs)
+    stripEqualsFrom (T_NormalWord id1 [T_DoubleQuoted id2 [T_Literal id3 s]]) =
+        T_NormalWord id1 [T_DoubleQuoted id2 [T_Literal id3 (stripEquals s)]]
+    stripEqualsFrom t = t
+
+    declaredVars = concatMap (getModifierParam defaultType) rest
+      where
+        defaultType = if any (`elem` flags) ["a", "A"] then DataArray else DataString
+
+    getLiteral t = do
+        s <- getLiteralString t
+        when ("-" `isPrefixOf` s) $ fail "argument"
+        return (base, t, s, DataString SourceExternal)
+
+    getModifierParamString = getModifierParam DataString
+
+    getModifierParam def t@(T_Assignment _ _ name _ value) =
+        [(base, t, name, dataTypeFrom def value)]
+    getModifierParam def t@T_NormalWord {} = maybeToList $ do
+        name <- getLiteralString t
+        guard $ isVariableName name
+        return (base, t, name, def SourceDeclaration)
+    getModifierParam _ _ = []
+
+    letParamToLiteral token =
+          if var == ""
+            then []
+            else [(base, token, var, DataString $ SourceFrom [stripEqualsFrom token])]
+        where var = takeWhile isVariableChar $ dropWhile (`elem` "+-") $ concat $ oversimplify token
+
+    getSetParams (t:_:rest) | getLiteralString t == Just "-o" = getSetParams rest
+    getSetParams (t:rest) =
+        let s = getLiteralString t in
+            case s of
+                Just "--"    -> return rest
+                Just ('-':_) -> getSetParams rest
+                _            -> return (t:fromMaybe [] (getSetParams rest))
+    getSetParams [] = Nothing
+
+    getPrintfVariable list = f $ map (\x -> (x, getLiteralString x)) list
+      where
+        f ((_, Just "-v") : (t, Just var) : _) = return (base, t, var, DataString $ SourceFrom list)
+        f (_:rest) = f rest
+        f [] = fail "not found"
+
+    -- mapfile has some curious syntax allowing flags plus 0..n variable names
+    -- where only the first non-option one is used if any. Here we cheat and
+    -- just get the last one, if it's a variable name.
+    getMapfileArray base arguments = do
+        lastArg <- listToMaybe (reverse arguments)
+        name <- getLiteralString lastArg
+        guard $ isVariableName name
+        return (base, lastArg, name, DataArray SourceExternal)
+
+getModifiedVariableCommand _ = []
+
+getIndexReferences s = fromMaybe [] $ do
+    match <- matchRegex re s
+    index <- match !!! 0
+    return $ matchAllStrings variableNameRegex index
+  where
+    re = mkRegex "(\\[.*\\])"
+
+prop_getOffsetReferences1 = getOffsetReferences ":bar" == ["bar"]
+prop_getOffsetReferences2 = getOffsetReferences ":bar:baz" == ["bar", "baz"]
+prop_getOffsetReferences3 = getOffsetReferences "[foo]:bar" == ["bar"]
+prop_getOffsetReferences4 = getOffsetReferences "[foo]:bar:baz" == ["bar", "baz"]
+getOffsetReferences mods = fromMaybe [] $ do
+-- if mods start with [, then drop until ]
+    match <- matchRegex re mods
+    offsets <- match !!! 1
+    return $ matchAllStrings variableNameRegex offsets
+  where
+    re = mkRegex "^(\\[.+\\])? *:([^-=?+].*)"
+
+getReferencedVariables parents t =
+    case t of
+        T_DollarBraced id l -> let str = bracedString t in
+            (t, t, getBracedReference str) :
+                map (\x -> (l, l, x)) (
+                    getIndexReferences str
+                    ++ getOffsetReferences (getBracedModifier str))
+        TA_Variable id name _ ->
+            if isArithmeticAssignment t
+            then []
+            else [(t, t, name)]
+        T_Assignment id mode str _ word ->
+            [(t, t, str) | mode == Append] ++ specialReferences str t word
+
+        TC_Unary id _ "-v" token -> getIfReference t token
+        TC_Unary id _ "-R" token -> getIfReference t token
+        TC_Binary id DoubleBracket op lhs rhs ->
+            if isDereferencing op
+            then concatMap (getIfReference t) [lhs, rhs]
+            else []
+
+        t@(T_FdRedirect _ ('{':var) op) -> -- {foo}>&- references and closes foo
+            [(t, t, takeWhile (/= '}') var) | isClosingFileOp op]
+        x -> getReferencedVariableCommand x
+  where
+    -- Try to reduce false positives for unused vars only referenced from evaluated vars
+    specialReferences name base word =
+        if name `elem` [
+            "PS1", "PS2", "PS3", "PS4",
+            "PROMPT_COMMAND"
+          ]
+        then
+            map (\x -> (base, base, x)) $
+                getVariablesFromLiteralToken word
+        else []
+
+    literalizer t = case t of
+        T_Glob _ s -> return s    -- Also when parsed as globs
+        _          -> Nothing
+
+    getIfReference context token = maybeToList $ do
+            str <- getLiteralStringExt literalizer token
+            guard . not $ null str
+            when (isDigit $ head str) $ fail "is a number"
+            return (context, token, getBracedReference str)
+
+    isDereferencing = (`elem` ["-eq", "-ne", "-lt", "-le", "-gt", "-ge"])
+
+    isArithmeticAssignment t = case getPath parents t of
+        this: TA_Assignment _ "=" lhs _ :_ -> lhs == t
+        _                                  -> False
+
+dataTypeFrom defaultType v = (case v of T_Array {} -> DataArray; _ -> defaultType) $ SourceFrom [v]
+
+
+--- Command specific checks
+
+-- Compare a command to a string: t `isCommand` "sed" (also matches /usr/bin/sed)
+isCommand token str = isCommandMatch token (\cmd -> cmd  == str || ('/' : str) `isSuffixOf` cmd)
+
+-- Compare a command to a literal. Like above, but checks full path.
+isUnqualifiedCommand token str = isCommandMatch token (== str)
+
+isCommandMatch token matcher = fromMaybe False $
+    fmap matcher (getCommandName token)
+
+-- Does this regex look like it was intended as a glob?
+-- True:  *foo*
+-- False: .*foo.*
+isConfusedGlobRegex :: String -> Bool
+isConfusedGlobRegex ('*':_) = True
+isConfusedGlobRegex [x,'*'] | x /= '\\' = True
+isConfusedGlobRegex _       = False
+
+isVariableStartChar x = x == '_' || isAsciiLower x || isAsciiUpper x
+isVariableChar x = isVariableStartChar x || isDigit x
+variableNameRegex = mkRegex "[_a-zA-Z][_a-zA-Z0-9]*"
+
+prop_isVariableName1 = isVariableName "_fo123"
+prop_isVariableName2 = not $ isVariableName "4"
+prop_isVariableName3 = not $ isVariableName "test: "
+isVariableName (x:r) = isVariableStartChar x && all isVariableChar r
+isVariableName _     = False
+
+getVariablesFromLiteralToken token =
+    getVariablesFromLiteral (fromJust $ getLiteralStringExt (const $ return " ") token)
+
+-- Try to get referenced variables from a literal string like "$foo"
+-- Ignores tons of cases like arithmetic evaluation and array indices.
+prop_getVariablesFromLiteral1 =
+    getVariablesFromLiteral "$foo${bar//a/b}$BAZ" == ["foo", "bar", "BAZ"]
+getVariablesFromLiteral string =
+    map (!! 0) $ matchAllSubgroups variableRegex string
+  where
+    variableRegex = mkRegex "\\$\\{?([A-Za-z0-9_]+)"
+
+-- Get the variable name from an expansion like ${var:-foo}
+prop_getBracedReference1 = getBracedReference "foo" == "foo"
+prop_getBracedReference2 = getBracedReference "#foo" == "foo"
+prop_getBracedReference3 = getBracedReference "#" == "#"
+prop_getBracedReference4 = getBracedReference "##" == "#"
+prop_getBracedReference5 = getBracedReference "#!" == "!"
+prop_getBracedReference6 = getBracedReference "!#" == "#"
+prop_getBracedReference7 = getBracedReference "!foo#?" == "foo"
+prop_getBracedReference8 = getBracedReference "foo-bar" == "foo"
+prop_getBracedReference9 = getBracedReference "foo:-bar" == "foo"
+prop_getBracedReference10= getBracedReference "foo: -1" == "foo"
+prop_getBracedReference11= getBracedReference "!os*" == ""
+prop_getBracedReference12= getBracedReference "!os?bar**" == ""
+prop_getBracedReference13= getBracedReference "foo[bar]" == "foo"
+getBracedReference s = fromMaybe s $
+    nameExpansion s `mplus` takeName noPrefix `mplus` getSpecial noPrefix `mplus` getSpecial s
+  where
+    noPrefix = dropPrefix s
+    dropPrefix (c:rest) = if c `elem` "!#" then rest else c:rest
+    dropPrefix ""       = ""
+    takeName s = do
+        let name = takeWhile isVariableChar s
+        guard . not $ null name
+        return name
+    getSpecial (c:_) =
+        if c `elem` "*@#?-$!" then return [c] else fail "not special"
+    getSpecial _ = fail "empty"
+
+    nameExpansion ('!':rest) = do -- e.g. ${!foo*bar*}
+        let suffix = dropWhile isVariableChar rest
+        guard $ suffix /= rest -- e.g. ${!@}
+        first <- suffix !!! 0
+        guard $ first `elem` "*?"
+        return ""
+    nameExpansion _ = Nothing
+
+prop_getBracedModifier1 = getBracedModifier "foo:bar:baz" == ":bar:baz"
+prop_getBracedModifier2 = getBracedModifier "!var:-foo" == ":-foo"
+prop_getBracedModifier3 = getBracedModifier "foo[bar]" == "[bar]"
+getBracedModifier s = fromMaybe "" . listToMaybe $ do
+    let var = getBracedReference s
+    a <- dropModifier s
+    dropPrefix var a
+  where
+    dropPrefix [] t        = return t
+    dropPrefix (a:b) (c:d) | a == c = dropPrefix b d
+    dropPrefix _ _         = []
+
+    dropModifier (c:rest) | c `elem` "#!" = [rest, c:rest]
+    dropModifier x        = [x]
+
+-- Useful generic functions.
+
+-- Run an action in a Maybe (or do nothing).
+-- Example:
+-- potentially $ do
+--   s <- getLiteralString cmd
+--   guard $ s `elem` ["--recursive", "-r"]
+--   return $ warn .. "Something something recursive"
+potentially :: Monad m => Maybe (m ()) -> m ()
+potentially = fromMaybe (return ())
+
+-- Get element 0 or a default. Like `head` but safe.
+headOrDefault _ (a:_) = a
+headOrDefault def _   = def
+
+--- Get element n of a list, or Nothing. Like `!!` but safe.
+(!!!) list i =
+    case drop i list of
+        []    -> Nothing
+        (r:_) -> Just r
+
+-- Run a command if the shell is in the given list
+whenShell l c = do
+    shell <- asks shellType
+    when (shell `elem` l ) c
+
+
+filterByAnnotation asSpec params =
+    filter (not . shouldIgnore)
+  where
+    token = asScript asSpec
+    idFor (TokenComment id _) = id
+    shouldIgnore note =
+        any (shouldIgnoreFor (getCode note)) $
+            getPath parents (T_Bang $ idFor note)
+    shouldIgnoreFor num (T_Annotation _ anns _) =
+        any hasNum anns
+      where
+        hasNum (DisableComment ts) = num == ts
+        hasNum _                   = False
+    shouldIgnoreFor _ T_Include {} = not $ asCheckSourced asSpec
+    shouldIgnoreFor _ _ = False
+    parents = parentMap params
+    getCode (TokenComment _ (Comment _ c _)) = c
+
+-- Is this a ${#anything}, to get string length or array count?
+isCountingReference (T_DollarBraced id token) =
+    case concat $ oversimplify token of
+        '#':_ -> True
+        _     -> False
+isCountingReference _ = False
+
+-- FIXME: doesn't handle ${a:+$var} vs ${a:+"$var"}
+isQuotedAlternativeReference t =
+    case t of
+        T_DollarBraced _ _ ->
+            getBracedModifier (bracedString t) `matches` re
+        _ -> False
+  where
+    re = mkRegex "(^|\\]):?\\+"
+
+-- getGnuOpts "erd:u:" will parse a SimpleCommand like
+--     read -re -d : -u 3 bar
+-- into
+--     Just [("r", -re), ("e", -re), ("d", :), ("u", 3), ("", bar)]
+-- where flags with arguments map to arguments, while others map to themselves.
+-- Any unrecognized flag will result in Nothing.
+getGnuOpts = getOpts getAllFlags
+getBsdOpts = getOpts getLeadingFlags
+getOpts :: (Token -> [(Token, String)]) -> String -> Token -> Maybe [(String, Token)]
+getOpts flagTokenizer string cmd = process flags
+  where
+    flags = flagTokenizer cmd
+    flagList (c:':':rest) = ([c], True) : flagList rest
+    flagList (c:rest)     = ([c], False) : flagList rest
+    flagList []           = []
+    flagMap = Map.fromList $ ("", False) : flagList string
+
+    process [] = return []
+    process [(token, flag)] = do
+        takesArg <- Map.lookup flag flagMap
+        guard $ not takesArg
+        return [(flag, token)]
+    process ((token1, flag1):rest2@((token2, flag2):rest)) = do
+        takesArg <- Map.lookup flag1 flagMap
+        if takesArg
+            then do
+                guard $ flag2 == ""
+                more <- process rest
+                return $ (flag1, token2) : more
+            else do
+                more <- process rest2
+                return $ (flag1, token1) : more
+
+return []
+runTests =  $( [| $(forAllProperties) (quickCheckWithResult (stdArgs { maxSuccess = 1 }) ) |])
diff --git a/src/ShellCheck/Checker.hs b/src/ShellCheck/Checker.hs
new file mode 100644
--- /dev/null
+++ b/src/ShellCheck/Checker.hs
@@ -0,0 +1,202 @@
+{-
+    Copyright 2012-2015 Vidar Holen
+
+    This file is part of ShellCheck.
+    https://www.shellcheck.net
+
+    ShellCheck is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    ShellCheck is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <https://www.gnu.org/licenses/>.
+-}
+{-# LANGUAGE TemplateHaskell #-}
+module ShellCheck.Checker (checkScript, ShellCheck.Checker.runTests) where
+
+import ShellCheck.Interface
+import ShellCheck.Parser
+import ShellCheck.Analyzer
+
+import Data.Either
+import Data.Functor
+import Data.List
+import Data.Maybe
+import Data.Ord
+import Control.Monad.Identity
+import qualified Data.Map as Map
+import qualified System.IO
+import Prelude hiding (readFile)
+import Control.Monad
+
+import Test.QuickCheck.All
+
+tokenToPosition map (TokenComment id c) = fromMaybe fail $ do
+    position <- Map.lookup id map
+    return $ PositionedComment position position c
+  where
+    fail = error "Internal shellcheck error: id doesn't exist. Please report!"
+
+checkScript :: Monad m => SystemInterface m -> CheckSpec -> m CheckResult
+checkScript sys spec = do
+    results <- checkScript (csScript spec)
+    return CheckResult {
+        crFilename = csFilename spec,
+        crComments = results
+    }
+  where
+    checkScript contents = do
+        result <- parseScript sys ParseSpec {
+            psFilename = csFilename spec,
+            psScript = contents,
+            psCheckSourced = csCheckSourced spec
+        }
+        let parseMessages = prComments result
+        let analysisMessages =
+                fromMaybe [] $
+                    (arComments . analyzeScript . analysisSpec)
+                        <$> prRoot result
+        let translator = tokenToPosition (prTokenPositions result)
+        return . nub . sortMessages . filter shouldInclude $
+            (parseMessages ++ map translator analysisMessages)
+
+    shouldInclude (PositionedComment _ _ (Comment _ code _)) =
+        code `notElem` csExcludedWarnings spec
+
+    sortMessages = sortBy (comparing order)
+    order (PositionedComment pos _ (Comment severity code message)) =
+        (posFile pos, posLine pos, posColumn pos, severity, code, message)
+    getPosition (PositionedComment pos _ _) = pos
+
+    analysisSpec root =
+        AnalysisSpec {
+            asScript = root,
+            asShellType = csShellTypeOverride spec,
+            asCheckSourced = csCheckSourced spec,
+            asExecutionMode = Executed
+         }
+
+getErrors sys spec =
+    sort . map getCode . crComments $
+        runIdentity (checkScript sys spec)
+  where
+    getCode (PositionedComment _ _ (Comment _ code _)) = code
+
+check = checkWithIncludes []
+
+checkWithSpec includes =
+    getErrors (mockedSystemInterface includes)
+
+checkWithIncludes includes src =
+    checkWithSpec includes emptyCheckSpec {
+        csScript = src,
+        csExcludedWarnings = [2148]
+    }
+
+checkRecursive includes src =
+    checkWithSpec includes emptyCheckSpec {
+        csScript = src,
+        csExcludedWarnings = [2148],
+        csCheckSourced = True
+    }
+
+prop_findsParseIssue = check "echo \"$12\"" == [1037]
+
+prop_commentDisablesParseIssue1 =
+    null $ check "#shellcheck disable=SC1037\necho \"$12\""
+prop_commentDisablesParseIssue2 =
+    null $ check "#shellcheck disable=SC1037\n#lol\necho \"$12\""
+
+prop_findsAnalysisIssue =
+    check "echo $1" == [2086]
+prop_commentDisablesAnalysisIssue1 =
+    null $ check "#shellcheck disable=SC2086\necho $1"
+prop_commentDisablesAnalysisIssue2 =
+    null $ check "#shellcheck disable=SC2086\n#lol\necho $1"
+
+prop_optionDisablesIssue1 =
+    null $ getErrors
+                (mockedSystemInterface [])
+                emptyCheckSpec {
+                    csScript = "echo $1",
+                    csExcludedWarnings = [2148, 2086]
+                }
+
+prop_optionDisablesIssue2 =
+    null $ getErrors
+                (mockedSystemInterface [])
+                emptyCheckSpec {
+                    csScript = "echo \"$10\"",
+                    csExcludedWarnings = [2148, 1037]
+                }
+
+prop_canParseDevNull =
+    [] == check "source /dev/null"
+
+prop_failsWhenNotSourcing =
+    [1091, 2154] == check "source lol; echo \"$bar\""
+
+prop_worksWhenSourcing =
+    null $ checkWithIncludes [("lib", "bar=1")] "source lib; echo \"$bar\""
+
+prop_worksWhenDotting =
+    null $ checkWithIncludes [("lib", "bar=1")] ". lib; echo \"$bar\""
+
+prop_noInfiniteSourcing =
+    [] == checkWithIncludes  [("lib", "source lib")] "source lib"
+
+prop_canSourceBadSyntax =
+    [1094, 2086] == checkWithIncludes [("lib", "for f; do")] "source lib; echo $1"
+
+prop_cantSourceDynamic =
+    [1090] == checkWithIncludes [("lib", "")] ". \"$1\""
+
+prop_cantSourceDynamic2 =
+    [1090] == checkWithIncludes [("lib", "")] "source ~/foo"
+
+prop_canSourceDynamicWhenRedirected =
+    null $ checkWithIncludes [("lib", "")] "#shellcheck source=lib\n. \"$1\""
+
+prop_recursiveAnalysis =
+    [2086] == checkRecursive [("lib", "echo $1")] "source lib"
+
+prop_recursiveParsing =
+    [1037] == checkRecursive [("lib", "echo \"$10\"")] "source lib"
+
+prop_sourceDirectiveDoesntFollowFile =
+    null $ checkWithIncludes
+                [("foo", "source bar"), ("bar", "baz=3")]
+                "#shellcheck source=foo\n. \"$1\"; echo \"$baz\""
+
+prop_filewideAnnotationBase = [2086] == check "#!/bin/sh\necho $1"
+prop_filewideAnnotation1 = null $
+    check "#!/bin/sh\n# shellcheck disable=2086\necho $1"
+prop_filewideAnnotation2 = null $
+    check "#!/bin/sh\n# shellcheck disable=2086\ntrue\necho $1"
+prop_filewideAnnotation3 = null $
+    check "#!/bin/sh\n#unerlated\n# shellcheck disable=2086\ntrue\necho $1"
+prop_filewideAnnotation4 = null $
+    check "#!/bin/sh\n# shellcheck disable=2086\n#unrelated\ntrue\necho $1"
+prop_filewideAnnotation5 = null $
+    check "#!/bin/sh\n\n\n\n#shellcheck disable=2086\ntrue\necho $1"
+prop_filewideAnnotation6 = null $
+    check "#shellcheck shell=sh\n#unrelated\n#shellcheck disable=2086\ntrue\necho $1"
+prop_filewideAnnotation7 = null $
+    check "#!/bin/sh\n# shellcheck disable=2086\n#unrelated\ntrue\necho $1"
+
+prop_filewideAnnotationBase2 = [2086, 2181] == check "true\n[ $? == 0 ] && echo $1"
+prop_filewideAnnotation8 = null $
+    check "# Disable $? warning\n#shellcheck disable=SC2181\n# Disable quoting warning\n#shellcheck disable=2086\ntrue\n[ $? == 0 ] && echo $1"
+
+prop_sourcePartOfOriginalScript = -- #1181: -x disabled posix warning for 'source'
+    2039 `elem` checkWithIncludes [("./saywhat.sh", "echo foo")] "#!/bin/sh\nsource ./saywhat.sh"
+
+
+return []
+runTests = $quickCheckAll
diff --git a/src/ShellCheck/Checks/Commands.hs b/src/ShellCheck/Checks/Commands.hs
new file mode 100644
--- /dev/null
+++ b/src/ShellCheck/Checks/Commands.hs
@@ -0,0 +1,1003 @@
+{-
+    Copyright 2012-2015 Vidar Holen
+
+    This file is part of ShellCheck.
+    https://www.shellcheck.net
+
+    ShellCheck is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    ShellCheck is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <https://www.gnu.org/licenses/>.
+-}
+{-# LANGUAGE TemplateHaskell #-}
+{-# LANGUAGE FlexibleContexts #-}
+
+-- This module contains checks that examine specific commands by name.
+module ShellCheck.Checks.Commands (checker , ShellCheck.Checks.Commands.runTests) where
+
+import ShellCheck.AST
+import ShellCheck.ASTLib
+import ShellCheck.AnalyzerLib
+import ShellCheck.Data
+import ShellCheck.Interface
+import ShellCheck.Parser
+import ShellCheck.Regex
+
+import Control.Monad
+import Control.Monad.RWS
+import Data.Char
+import Data.List
+import Data.Maybe
+import qualified Data.Map.Strict as Map
+import Test.QuickCheck.All (forAllProperties)
+import Test.QuickCheck.Test (quickCheckWithResult, stdArgs, maxSuccess)
+
+data CommandName = Exactly String | Basename String
+    deriving (Eq, Ord)
+
+data CommandCheck =
+    CommandCheck CommandName (Token -> Analysis)
+
+
+verify :: CommandCheck -> String -> Bool
+verify f s = producesComments (getChecker [f]) s == Just True
+verifyNot f s = producesComments (getChecker [f]) s == Just False
+
+arguments (T_SimpleCommand _ _ (cmd:args)) = args
+
+commandChecks :: [CommandCheck]
+commandChecks = [
+    checkTr
+    ,checkFindNameGlob
+    ,checkNeedlessExpr
+    ,checkGrepRe
+    ,checkTrapQuotes
+    ,checkReturn
+    ,checkFindExecWithSingleArgument
+    ,checkUnusedEchoEscapes
+    ,checkInjectableFindSh
+    ,checkFindActionPrecedence
+    ,checkMkdirDashPM
+    ,checkNonportableSignals
+    ,checkInteractiveSu
+    ,checkSshCommandString
+    ,checkPrintfVar
+    ,checkUuoeCmd
+    ,checkSetAssignment
+    ,checkExportedExpansions
+    ,checkAliasesUsesArgs
+    ,checkAliasesExpandEarly
+    ,checkUnsetGlobs
+    ,checkFindWithoutPath
+    ,checkTimeParameters
+    ,checkTimedCommand
+    ,checkLocalScope
+    ,checkDeprecatedTempfile
+    ,checkDeprecatedEgrep
+    ,checkDeprecatedFgrep
+    ,checkWhileGetoptsCase
+    ,checkCatastrophicRm
+    ,checkLetUsage
+    ,checkMvArguments, checkCpArguments, checkLnArguments
+    ,checkFindRedirections
+    ,checkReadExpansions
+    ,checkWhich
+    ,checkSudoRedirect
+    ,checkSudoArgs
+    ]
+
+buildCommandMap :: [CommandCheck] -> Map.Map CommandName (Token -> Analysis)
+buildCommandMap = foldl' addCheck Map.empty
+  where
+    addCheck map (CommandCheck name function) =
+        Map.insertWith composeAnalyzers name function map
+
+
+checkCommand :: Map.Map CommandName (Token -> Analysis) -> Token -> Analysis
+checkCommand map t@(T_SimpleCommand id _ (cmd:rest)) = fromMaybe (return ()) $ do
+    name <- getLiteralString cmd
+    return $
+        if '/' `elem` name
+        then
+            Map.findWithDefault nullCheck (Basename $ basename name) map t
+        else do
+            Map.findWithDefault nullCheck (Exactly name) map t
+            Map.findWithDefault nullCheck (Basename name) map t
+
+  where
+    basename = reverse . takeWhile (/= '/') . reverse
+checkCommand _ _ = return ()
+
+getChecker :: [CommandCheck] -> Checker
+getChecker list = Checker {
+    perScript = const $ return (),
+    perToken = checkCommand map
+    }
+  where
+    map = buildCommandMap list
+
+
+checker :: Parameters -> Checker
+checker params = getChecker commandChecks
+
+prop_checkTr1 = verify checkTr "tr [a-f] [A-F]"
+prop_checkTr2 = verify checkTr "tr 'a-z' 'A-Z'"
+prop_checkTr2a= verify checkTr "tr '[a-z]' '[A-Z]'"
+prop_checkTr3 = verifyNot checkTr "tr -d '[:lower:]'"
+prop_checkTr3a= verifyNot checkTr "tr -d '[:upper:]'"
+prop_checkTr3b= verifyNot checkTr "tr -d '|/_[:upper:]'"
+prop_checkTr4 = verifyNot checkTr "ls [a-z]"
+prop_checkTr5 = verify checkTr "tr foo bar"
+prop_checkTr6 = verify checkTr "tr 'hello' 'world'"
+prop_checkTr8 = verifyNot checkTr "tr aeiou _____"
+prop_checkTr9 = verifyNot checkTr "a-z n-za-m"
+prop_checkTr10= verifyNot checkTr "tr --squeeze-repeats rl lr"
+prop_checkTr11= verifyNot checkTr "tr abc '[d*]'"
+checkTr = CommandCheck (Basename "tr") (mapM_ f . arguments)
+  where
+    f w | isGlob w = -- The user will go [ab] -> '[ab]' -> 'ab'. Fixme?
+        warn (getId w) 2060 "Quote parameters to tr to prevent glob expansion."
+    f word =
+      case getLiteralString word of
+        Just "a-z" -> info (getId word) 2018 "Use '[:lower:]' to support accents and foreign alphabets."
+        Just "A-Z" -> info (getId word) 2019 "Use '[:upper:]' to support accents and foreign alphabets."
+        Just s -> do  -- Eliminate false positives by only looking for dupes in SET2?
+          when (not ("-" `isPrefixOf` s || "[:" `isInfixOf` s) && duplicated s) $
+            info (getId word) 2020 "tr replaces sets of chars, not words (mentioned due to duplicates)."
+          unless ("[:" `isPrefixOf` s) $
+            when ("[" `isPrefixOf` s && "]" `isSuffixOf` s && (length s > 2) && ('*' `notElem` s)) $
+              info (getId word) 2021 "Don't use [] around classes in tr, it replaces literal square brackets."
+        Nothing -> return ()
+
+    duplicated s =
+        let relevant = filter isAlpha s
+        in relevant /= nub relevant
+
+prop_checkFindNameGlob1 = verify checkFindNameGlob "find / -name *.php"
+prop_checkFindNameGlob2 = verify checkFindNameGlob "find / -type f -ipath *(foo)"
+prop_checkFindNameGlob3 = verifyNot checkFindNameGlob "find * -name '*.php'"
+checkFindNameGlob = CommandCheck (Basename "find") (f . arguments)  where
+    acceptsGlob (Just s) = s `elem` [ "-ilname", "-iname", "-ipath", "-iregex", "-iwholename", "-lname", "-name", "-path", "-regex", "-wholename" ]
+    acceptsGlob _ = False
+    f [] = return ()
+    f [x] = return ()
+    f (a:b:r) = do
+        when (acceptsGlob (getLiteralString a) && isGlob b) $ do
+            let (Just s) = getLiteralString a
+            warn (getId b) 2061 $ "Quote the parameter to " ++ s ++ " so the shell won't interpret it."
+        f (b:r)
+
+
+prop_checkNeedlessExpr = verify checkNeedlessExpr "foo=$(expr 3 + 2)"
+prop_checkNeedlessExpr2 = verify checkNeedlessExpr "foo=`echo \\`expr 3 + 2\\``"
+prop_checkNeedlessExpr3 = verifyNot checkNeedlessExpr "foo=$(expr foo : regex)"
+prop_checkNeedlessExpr4 = verifyNot checkNeedlessExpr "foo=$(expr foo \\< regex)"
+checkNeedlessExpr = CommandCheck (Basename "expr") f where
+    f t =
+        when (all (`notElem` exceptions) (words $ arguments t)) $
+            style (getId t) 2003
+                "expr is antiquated. Consider rewriting this using $((..)), ${} or [[ ]]."
+    -- These operators are hard to replicate in POSIX
+    exceptions = [ ":", "<", ">", "<=", ">=" ]
+    words = mapMaybe getLiteralString
+
+
+prop_checkGrepRe1 = verify checkGrepRe "cat foo | grep *.mp3"
+prop_checkGrepRe2 = verify checkGrepRe "grep -Ev cow*test *.mp3"
+prop_checkGrepRe3 = verify checkGrepRe "grep --regex=*.mp3 file"
+prop_checkGrepRe4 = verifyNot checkGrepRe "grep foo *.mp3"
+prop_checkGrepRe5 = verifyNot checkGrepRe "grep-v  --regex=moo *"
+prop_checkGrepRe6 = verifyNot checkGrepRe "grep foo \\*.mp3"
+prop_checkGrepRe7 = verify checkGrepRe "grep *foo* file"
+prop_checkGrepRe8 = verify checkGrepRe "ls | grep foo*.jpg"
+prop_checkGrepRe9 = verifyNot checkGrepRe "grep '[0-9]*' file"
+prop_checkGrepRe10= verifyNot checkGrepRe "grep '^aa*' file"
+prop_checkGrepRe11= verifyNot checkGrepRe "grep --include=*.png foo"
+prop_checkGrepRe12= verifyNot checkGrepRe "grep -F 'Foo*' file"
+prop_checkGrepRe13= verifyNot checkGrepRe "grep -- -foo bar*"
+prop_checkGrepRe14= verifyNot checkGrepRe "grep -e -foo bar*"
+prop_checkGrepRe15= verifyNot checkGrepRe "grep --regex -foo bar*"
+
+checkGrepRe = CommandCheck (Basename "grep") check where
+    check cmd = f cmd (arguments cmd)
+    -- --regex=*(extglob) doesn't work. Fixme?
+    skippable (Just s) = not ("--regex=" `isPrefixOf` s) && "-" `isPrefixOf` s
+    skippable _ = False
+    f _ [] = return ()
+    f cmd (x:r) =
+        let str = getLiteralStringExt (const $ return "_") x
+        in
+            if str `elem` [Just "--", Just "-e", Just "--regex"]
+            then checkRE cmd r -- Regex is *after* this
+            else
+                if skippable str
+                then f cmd r           -- Regex is elsewhere
+                else checkRE cmd (x:r) -- Regex is this
+
+    checkRE _ [] = return ()
+    checkRE cmd (re:_) = do
+        when (isGlob re) $
+            warn (getId re) 2062 "Quote the grep pattern so the shell won't interpret it."
+
+        unless (cmd `hasFlag` "F") $ do
+            let string = concat $ oversimplify re
+            if isConfusedGlobRegex string then
+                warn (getId re) 2063 "Grep uses regex, but this looks like a glob."
+              else potentially $ do
+                char <- getSuspiciousRegexWildcard string
+                return $ info (getId re) 2022 $
+                    "Note that unlike globs, " ++ [char] ++ "* here matches '" ++ [char, char, char] ++ "' but not '" ++ wordStartingWith char ++ "'."
+
+    wordStartingWith c =
+        head . filter ([c] `isPrefixOf`) $ candidates
+      where
+        candidates =
+            sampleWords ++ map (\(x:r) -> toUpper x : r) sampleWords ++ [c:"test"]
+
+    getSuspiciousRegexWildcard str =
+        if not $ str `matches` contra
+        then do
+            match <- matchRegex suspicious str
+            str <- match !!! 0
+            str !!! 0
+        else
+            fail "looks good"
+      where
+        suspicious = mkRegex "([A-Za-z1-9])\\*"
+        contra = mkRegex "[^a-zA-Z1-9]\\*|[][^$+\\\\]"
+
+
+prop_checkTrapQuotes1 = verify checkTrapQuotes "trap \"echo $num\" INT"
+prop_checkTrapQuotes1a= verify checkTrapQuotes "trap \"echo `ls`\" INT"
+prop_checkTrapQuotes2 = verifyNot checkTrapQuotes "trap 'echo $num' INT"
+prop_checkTrapQuotes3 = verify checkTrapQuotes "trap \"echo $((1+num))\" EXIT DEBUG"
+checkTrapQuotes = CommandCheck (Exactly "trap") (f . arguments) where
+    f (x:_) = checkTrap x
+    f _ = return ()
+    checkTrap (T_NormalWord _ [T_DoubleQuoted _ rs]) = mapM_ checkExpansions rs
+    checkTrap _ = return ()
+    warning id = warn id 2064 "Use single quotes, otherwise this expands now rather than when signalled."
+    checkExpansions (T_DollarExpansion id _) = warning id
+    checkExpansions (T_Backticked id _) = warning id
+    checkExpansions (T_DollarBraced id _) = warning id
+    checkExpansions (T_DollarArithmetic id _) = warning id
+    checkExpansions _ = return ()
+
+
+prop_checkReturn1 = verifyNot checkReturn "return"
+prop_checkReturn2 = verifyNot checkReturn "return 1"
+prop_checkReturn3 = verifyNot checkReturn "return $var"
+prop_checkReturn4 = verifyNot checkReturn "return $((a|b))"
+prop_checkReturn5 = verify checkReturn "return -1"
+prop_checkReturn6 = verify checkReturn "return 1000"
+prop_checkReturn7 = verify checkReturn "return 'hello world'"
+checkReturn = CommandCheck (Exactly "return") (f . arguments)
+  where
+    f (first:second:_) =
+        err (getId second) 2151
+            "Only one integer 0-255 can be returned. Use stdout for other data."
+    f [value] =
+        when (isInvalid $ literal value) $
+            err (getId value) 2152
+                "Can only return 0-255. Other data should be written to stdout."
+    f _ = return ()
+
+    isInvalid s = s == "" || any (not . isDigit) s || length s > 5
+        || let value = (read s :: Integer) in value > 255
+
+    literal token = fromJust $ getLiteralStringExt lit token
+    lit (T_DollarBraced {}) = return "0"
+    lit (T_DollarArithmetic {}) = return "0"
+    lit (T_DollarExpansion {}) = return "0"
+    lit (T_Backticked {}) = return "0"
+    lit _ = return "WTF"
+
+
+prop_checkFindExecWithSingleArgument1 = verify checkFindExecWithSingleArgument "find . -exec 'cat {} | wc -l' \\;"
+prop_checkFindExecWithSingleArgument2 = verify checkFindExecWithSingleArgument "find . -execdir 'cat {} | wc -l' +"
+prop_checkFindExecWithSingleArgument3 = verifyNot checkFindExecWithSingleArgument "find . -exec wc -l {} \\;"
+checkFindExecWithSingleArgument = CommandCheck (Basename "find") (f . arguments)
+  where
+    f = void . sequence . mapMaybe check . tails
+    check (exec:arg:term:_) = do
+        execS <- getLiteralString exec
+        termS <- getLiteralString term
+        cmdS <- getLiteralStringExt (const $ return " ") arg
+
+        guard $ execS `elem` ["-exec", "-execdir"] && termS `elem` [";", "+"]
+        guard $ cmdS `matches` commandRegex
+        return $ warn (getId exec) 2150 "-exec does not invoke a shell. Rewrite or use -exec sh -c .. ."
+    check _ = Nothing
+    commandRegex = mkRegex "[ |;]"
+
+
+prop_checkUnusedEchoEscapes1 = verify checkUnusedEchoEscapes "echo 'foo\\nbar\\n'"
+prop_checkUnusedEchoEscapes2 = verifyNot checkUnusedEchoEscapes "echo -e 'foi\\nbar'"
+prop_checkUnusedEchoEscapes3 = verify checkUnusedEchoEscapes "echo \"n:\\t42\""
+prop_checkUnusedEchoEscapes4 = verifyNot checkUnusedEchoEscapes "echo lol"
+prop_checkUnusedEchoEscapes5 = verifyNot checkUnusedEchoEscapes "echo -n -e '\n'"
+checkUnusedEchoEscapes = CommandCheck (Basename "echo") (f . arguments)
+  where
+    isDashE = mkRegex "^-.*e"
+    hasEscapes = mkRegex "\\\\[rnt]"
+    f args | concat (concatMap oversimplify allButLast) `matches` isDashE =
+        return ()
+      where allButLast = reverse . drop 1 . reverse $ args
+    f args = mapM_ checkEscapes args
+
+    checkEscapes (T_NormalWord _ args) =
+        mapM_ checkEscapes args
+    checkEscapes (T_DoubleQuoted id args) =
+        mapM_ checkEscapes args
+    checkEscapes (T_Literal id str) = examine id str
+    checkEscapes (T_SingleQuoted id str) = examine id str
+    checkEscapes _ = return ()
+
+    examine id str =
+        when (str `matches` hasEscapes) $
+            info id 2028 "echo won't expand escape sequences. Consider printf."
+
+
+prop_checkInjectableFindSh1 = verify checkInjectableFindSh "find . -exec sh -c 'echo {}' \\;"
+prop_checkInjectableFindSh2 = verify checkInjectableFindSh "find . -execdir bash -c 'rm \"{}\"' ';'"
+prop_checkInjectableFindSh3 = verifyNot checkInjectableFindSh "find . -exec sh -c 'rm \"$@\"' _ {} \\;"
+checkInjectableFindSh = CommandCheck (Basename "find") (check . arguments)
+  where
+    check args = do
+        let idStrings = map (\x -> (getId x, onlyLiteralString x)) args
+        match pattern idStrings
+
+    match _ [] = return ()
+    match [] (next:_) = action next
+    match (p:tests) ((id, arg):args) = do
+        when (p arg) $ match tests args
+        match (p:tests) args
+
+    pattern = [
+        (`elem` ["-exec", "-execdir"]),
+        (`elem` ["sh", "bash", "dash", "ksh"]),
+        (== "-c")
+        ]
+    action (id, arg) =
+        when ("{}" `isInfixOf` arg) $
+            warn id 2156 "Injecting filenames is fragile and insecure. Use parameters."
+
+
+prop_checkFindActionPrecedence1 = verify checkFindActionPrecedence "find . -name '*.wav' -o -name '*.au' -exec rm {} +"
+prop_checkFindActionPrecedence2 = verifyNot checkFindActionPrecedence "find . -name '*.wav' -o \\( -name '*.au' -exec rm {} + \\)"
+prop_checkFindActionPrecedence3 = verifyNot checkFindActionPrecedence "find . -name '*.wav' -o -name '*.au'"
+checkFindActionPrecedence = CommandCheck (Basename "find") (f . arguments)
+  where
+    pattern = [isMatch, const True, isParam ["-o", "-or"], isMatch, const True, isAction]
+    f list | length list < length pattern = return ()
+    f list@(_:rest) =
+        if and (zipWith ($) pattern list)
+        then warnFor (list !! (length pattern - 1))
+        else f rest
+    isMatch = isParam [ "-name", "-regex", "-iname", "-iregex", "-wholename", "-iwholename" ]
+    isAction = isParam [ "-exec", "-execdir", "-delete", "-print", "-print0", "-fls", "-fprint", "-fprint0", "-fprintf", "-ls", "-ok", "-okdir", "-printf" ]
+    isParam strs t = fromMaybe False $ do
+        param <- getLiteralString t
+        return $ param `elem` strs
+    warnFor t = warn (getId t) 2146 "This action ignores everything before the -o. Use \\( \\) to group."
+
+
+prop_checkMkdirDashPM0 = verify checkMkdirDashPM "mkdir -p -m 0755 a/b"
+prop_checkMkdirDashPM1 = verify checkMkdirDashPM "mkdir -pm 0755 $dir"
+prop_checkMkdirDashPM2 = verify checkMkdirDashPM "mkdir -vpm 0755 a/b"
+prop_checkMkdirDashPM3 = verify checkMkdirDashPM "mkdir -pm 0755 -v a/b"
+prop_checkMkdirDashPM4 = verify checkMkdirDashPM "mkdir --parents --mode=0755 a/b"
+prop_checkMkdirDashPM5 = verify checkMkdirDashPM "mkdir --parents --mode 0755 a/b"
+prop_checkMkdirDashPM6 = verify checkMkdirDashPM "mkdir -p --mode=0755 a/b"
+prop_checkMkdirDashPM7 = verify checkMkdirDashPM "mkdir --parents -m 0755 a/b"
+prop_checkMkdirDashPM8 = verifyNot checkMkdirDashPM "mkdir -p a/b"
+prop_checkMkdirDashPM9 = verifyNot checkMkdirDashPM "mkdir -m 0755 a/b"
+prop_checkMkdirDashPM10 = verifyNot checkMkdirDashPM "mkdir a/b"
+prop_checkMkdirDashPM11 = verifyNot checkMkdirDashPM "mkdir --parents a/b"
+prop_checkMkdirDashPM12 = verifyNot checkMkdirDashPM "mkdir --mode=0755 a/b"
+prop_checkMkdirDashPM13 = verifyNot checkMkdirDashPM "mkdir_func -pm 0755 a/b"
+prop_checkMkdirDashPM14 = verifyNot checkMkdirDashPM "mkdir -p -m 0755 singlelevel"
+prop_checkMkdirDashPM15 = verifyNot checkMkdirDashPM "mkdir -p -m 0755 ../bin"
+prop_checkMkdirDashPM16 = verify checkMkdirDashPM "mkdir -p -m 0755 ../bin/laden"
+prop_checkMkdirDashPM17 = verifyNot checkMkdirDashPM "mkdir -p -m 0755 ./bin"
+prop_checkMkdirDashPM18 = verify checkMkdirDashPM "mkdir -p -m 0755 ./bin/laden"
+prop_checkMkdirDashPM19 = verifyNot checkMkdirDashPM "mkdir -p -m 0755 ./../bin"
+prop_checkMkdirDashPM20 = verifyNot checkMkdirDashPM "mkdir -p -m 0755 .././bin"
+prop_checkMkdirDashPM21 = verifyNot checkMkdirDashPM "mkdir -p -m 0755 ../../bin"
+checkMkdirDashPM = CommandCheck (Basename "mkdir") check
+  where
+    check t = potentially $ do
+        let flags = getAllFlags t
+        dashP <- find ((\f -> f == "p" || f == "parents") . snd) flags
+        dashM <- find ((\f -> f == "m" || f == "mode") . snd) flags
+        -- mkdir -pm 0700 dir  is fine, so is ../dir, but dir/subdir is not.
+        guard $ any couldHaveSubdirs (drop 1 $ arguments t)
+        return $ warn (getId $ fst dashM) 2174 "When used with -p, -m only applies to the deepest directory."
+    couldHaveSubdirs t = fromMaybe True $ do
+        name <- getLiteralString t
+        return $ '/' `elem` name && not (name `matches` re)
+    re = mkRegex "^(\\.\\.?\\/)+[^/]+$"
+
+
+prop_checkNonportableSignals1 = verify checkNonportableSignals "trap f 8"
+prop_checkNonportableSignals2 = verifyNot checkNonportableSignals "trap f 0"
+prop_checkNonportableSignals3 = verifyNot checkNonportableSignals "trap f 14"
+prop_checkNonportableSignals4 = verify checkNonportableSignals "trap f SIGKILL"
+prop_checkNonportableSignals5 = verify checkNonportableSignals "trap f 9"
+prop_checkNonportableSignals6 = verify checkNonportableSignals "trap f stop"
+prop_checkNonportableSignals7 = verifyNot checkNonportableSignals "trap 'stop' int"
+checkNonportableSignals = CommandCheck (Exactly "trap") (f . arguments)
+  where
+    f args = case args of
+        first:rest -> unless (isFlag first) $ mapM_ check rest
+        _ -> return ()
+
+    check param = potentially $ do
+        str <- getLiteralString param
+        let id = getId param
+        return $ sequence_ $ mapMaybe (\f -> f id str) [
+            checkNumeric,
+            checkUntrappable
+            ]
+
+    checkNumeric id str = do
+        guard $ not (null str)
+        guard $ all isDigit str
+        guard $ str /= "0" -- POSIX exit trap
+        guard $ str `notElem` ["1", "2", "3", "6", "9", "14", "15" ] -- XSI
+        return $ warn id 2172
+            "Trapping signals by number is not well defined. Prefer signal names."
+
+    checkUntrappable id str = do
+        guard $ map toLower str `elem` ["kill", "9", "sigkill", "stop", "sigstop"]
+        return $ err id 2173
+            "SIGKILL/SIGSTOP can not be trapped."
+
+
+prop_checkInteractiveSu1 = verify checkInteractiveSu "su; rm file; su $USER"
+prop_checkInteractiveSu2 = verify checkInteractiveSu "su foo; something; exit"
+prop_checkInteractiveSu3 = verifyNot checkInteractiveSu "echo rm | su foo"
+prop_checkInteractiveSu4 = verifyNot checkInteractiveSu "su root < script"
+checkInteractiveSu = CommandCheck (Basename "su") f
+  where
+    f cmd = when (length (arguments cmd) <= 1) $ do
+        path <- pathTo cmd
+        when (all undirected path) $
+            info (getId cmd) 2117
+                "To run commands as another user, use su -c or sudo."
+
+    undirected (T_Pipeline _ _ l) = length l <= 1
+    -- This should really just be modifications to stdin, but meh
+    undirected (T_Redirecting _ list _) = null list
+    undirected _ = True
+
+
+-- This is hard to get right without properly parsing ssh args
+prop_checkSshCmdStr1 = verify checkSshCommandString "ssh host \"echo $PS1\""
+prop_checkSshCmdStr2 = verifyNot checkSshCommandString "ssh host \"ls foo\""
+prop_checkSshCmdStr3 = verifyNot checkSshCommandString "ssh \"$host\""
+prop_checkSshCmdStr4 = verifyNot checkSshCommandString "ssh -i key \"$host\""
+checkSshCommandString = CommandCheck (Basename "ssh") (f . arguments)
+  where
+    isOption x = "-" `isPrefixOf` (concat $ oversimplify x)
+    f args =
+        case partition isOption args of
+            ([], hostport:r@(_:_)) -> checkArg $ last r
+            _ -> return ()
+    checkArg (T_NormalWord _ [T_DoubleQuoted id parts]) =
+        case filter (not . isConstant) parts of
+            [] -> return ()
+            (x:_) -> info (getId x) 2029
+                "Note that, unescaped, this expands on the client side."
+    checkArg _ = return ()
+
+
+prop_checkPrintfVar1 = verify checkPrintfVar "printf \"Lol: $s\""
+prop_checkPrintfVar2 = verifyNot checkPrintfVar "printf 'Lol: $s'"
+prop_checkPrintfVar3 = verify checkPrintfVar "printf -v cow $(cmd)"
+prop_checkPrintfVar4 = verifyNot checkPrintfVar "printf \"%${count}s\" var"
+prop_checkPrintfVar5 = verify checkPrintfVar "printf '%s %s %s' foo bar"
+prop_checkPrintfVar6 = verify checkPrintfVar "printf foo bar baz"
+prop_checkPrintfVar7 = verify checkPrintfVar "printf -- foo bar baz"
+prop_checkPrintfVar8 = verifyNot checkPrintfVar "printf '%s %s %s' \"${var[@]}\""
+prop_checkPrintfVar9 = verifyNot checkPrintfVar "printf '%s %s %s\\n' *.png"
+prop_checkPrintfVar10= verifyNot checkPrintfVar "printf '%s %s %s' foo bar baz"
+prop_checkPrintfVar11= verifyNot checkPrintfVar "printf '%(%s%s)T' -1"
+prop_checkPrintfVar12= verify checkPrintfVar "printf '%s %s\\n' 1 2 3"
+prop_checkPrintfVar13= verifyNot checkPrintfVar "printf '%s %s\\n' 1 2 3 4"
+prop_checkPrintfVar14= verify checkPrintfVar "printf '%*s\\n' 1"
+prop_checkPrintfVar15= verifyNot checkPrintfVar "printf '%*s\\n' 1 2"
+checkPrintfVar = CommandCheck (Exactly "printf") (f . arguments) where
+    f (doubledash:rest) | getLiteralString doubledash == Just "--" = f rest
+    f (dashv:var:rest) | getLiteralString dashv == Just "-v" = f rest
+    f (format:params) = check format params
+    f _ = return ()
+
+    countFormats string =
+        case string of
+            '%':'%':rest -> countFormats rest
+            '%':'(':rest -> 1 + countFormats (dropWhile (/= ')') rest)
+            '%':'*':rest -> 2 + countFormats rest -- width is specified as an argument
+            '%':rest -> 1 + countFormats rest
+            _:rest -> countFormats rest
+            [] -> 0
+
+    check format more = do
+        fromMaybe (return ()) $ do
+            string <- getLiteralString format
+            let vars = countFormats string
+
+            return $ do
+                when (vars == 0 && more /= []) $
+                    err (getId format) 2182
+                        "This printf format string has no variables. Other arguments are ignored."
+
+                when (vars > 0
+                        && ((length more) `mod` vars /= 0 || null more)
+                        && all (not . mayBecomeMultipleArgs) more) $
+                    warn (getId format) 2183 $
+                        "This format string has " ++ show vars ++ " variables, but is passed " ++ show (length more) ++ " arguments."
+
+
+        unless ('%' `elem` concat (oversimplify format) || isLiteral format) $
+          info (getId format) 2059
+              "Don't use variables in the printf format string. Use printf \"..%s..\" \"$foo\"."
+
+
+
+
+prop_checkUuoeCmd1 = verify checkUuoeCmd "echo $(date)"
+prop_checkUuoeCmd2 = verify checkUuoeCmd "echo `date`"
+prop_checkUuoeCmd3 = verify checkUuoeCmd "echo \"$(date)\""
+prop_checkUuoeCmd4 = verify checkUuoeCmd "echo \"`date`\""
+prop_checkUuoeCmd5 = verifyNot checkUuoeCmd "echo \"The time is $(date)\""
+prop_checkUuoeCmd6 = verifyNot checkUuoeCmd "echo \"$(<file)\""
+checkUuoeCmd = CommandCheck (Exactly "echo") (f . arguments) where
+    msg id = style id 2005 "Useless echo? Instead of 'echo $(cmd)', just use 'cmd'."
+    f [token] = when (tokenIsJustCommandOutput token) $ msg (getId token)
+    f _ = return ()
+
+
+prop_checkSetAssignment1 = verify checkSetAssignment "set foo 42"
+prop_checkSetAssignment2 = verify checkSetAssignment "set foo = 42"
+prop_checkSetAssignment3 = verify checkSetAssignment "set foo=42"
+prop_checkSetAssignment4 = verifyNot checkSetAssignment "set -- if=/dev/null"
+prop_checkSetAssignment5 = verifyNot checkSetAssignment "set 'a=5'"
+prop_checkSetAssignment6 = verifyNot checkSetAssignment "set"
+checkSetAssignment = CommandCheck (Exactly "set") (f . arguments)
+  where
+    f (var:value:rest) =
+        let str = literal var in
+            when (isVariableName str || isAssignment str) $
+                msg (getId var)
+    f (var:_) =
+        when (isAssignment $ literal var) $
+            msg (getId var)
+    f _ = return ()
+
+    msg id = warn id 2121 "To assign a variable, use just 'var=value', no 'set ..'."
+
+    isAssignment str = '=' `elem` str
+    literal (T_NormalWord _ l) = concatMap literal l
+    literal (T_Literal _ str) = str
+    literal _ = "*"
+
+
+prop_checkExportedExpansions1 = verify checkExportedExpansions "export $foo"
+prop_checkExportedExpansions2 = verify checkExportedExpansions "export \"$foo\""
+prop_checkExportedExpansions3 = verifyNot checkExportedExpansions "export foo"
+prop_checkExportedExpansions4 = verifyNot checkExportedExpansions "export ${foo?}"
+checkExportedExpansions = CommandCheck (Exactly "export") (mapM_ check . arguments)
+  where
+    check t = potentially $ do
+        var <- getSingleUnmodifiedVariable t
+        let name = bracedString var
+        return . warn (getId t) 2163 $
+            "This does not export '" ++ name ++ "'. Remove $/${} for that, or use ${var?} to quiet."
+
+prop_checkReadExpansions1 = verify checkReadExpansions "read $var"
+prop_checkReadExpansions2 = verify checkReadExpansions "read -r $var"
+prop_checkReadExpansions3 = verifyNot checkReadExpansions "read -p $var"
+prop_checkReadExpansions4 = verifyNot checkReadExpansions "read -rd $delim name"
+prop_checkReadExpansions5 = verify checkReadExpansions "read \"$var\""
+prop_checkReadExpansions6 = verify checkReadExpansions "read -a $var"
+prop_checkReadExpansions7 = verifyNot checkReadExpansions "read $1"
+prop_checkReadExpansions8 = verifyNot checkReadExpansions "read ${var?}"
+checkReadExpansions = CommandCheck (Exactly "read") check
+  where
+    options = getGnuOpts "sreu:n:N:i:p:a:"
+    getVars cmd = fromMaybe [] $ do
+        opts <- options cmd
+        return . map snd $ filter (\(x,_) -> x == "" || x == "a") opts
+
+    check cmd = mapM_ warning $ getVars cmd
+    warning t = potentially $ do
+        var <- getSingleUnmodifiedVariable t
+        let name = bracedString var
+        guard $ isVariableName name   -- e.g. not $1
+        return . warn (getId t) 2229 $
+            "This does not read '" ++ name ++ "'. Remove $/${} for that, or use ${var?} to quiet."
+
+-- Return the single variable expansion that makes up this word, if any.
+-- e.g. $foo -> $foo, "$foo"'' -> $foo , "hello $name" -> Nothing
+getSingleUnmodifiedVariable :: Token -> Maybe Token
+getSingleUnmodifiedVariable word =
+    case getWordParts word of
+        [t@(T_DollarBraced {})] ->
+            let contents = bracedString t
+                name = getBracedReference contents
+            in guard (contents == name) >> return t
+        _ -> Nothing
+
+prop_checkAliasesUsesArgs1 = verify checkAliasesUsesArgs "alias a='cp $1 /a'"
+prop_checkAliasesUsesArgs2 = verifyNot checkAliasesUsesArgs "alias $1='foo'"
+prop_checkAliasesUsesArgs3 = verify checkAliasesUsesArgs "alias a=\"echo \\${@}\""
+checkAliasesUsesArgs = CommandCheck (Exactly "alias") (f . arguments)
+  where
+    re = mkRegex "\\$\\{?[0-9*@]"
+    f = mapM_ checkArg
+    checkArg arg =
+        let string = fromJust $ getLiteralStringExt (const $ return "_") arg in
+            when ('=' `elem` string && string `matches` re) $
+                err (getId arg) 2142
+                    "Aliases can't use positional parameters. Use a function."
+
+
+prop_checkAliasesExpandEarly1 = verify checkAliasesExpandEarly "alias foo=\"echo $PWD\""
+prop_checkAliasesExpandEarly2 = verifyNot checkAliasesExpandEarly "alias -p"
+prop_checkAliasesExpandEarly3 = verifyNot checkAliasesExpandEarly "alias foo='echo {1..10}'"
+checkAliasesExpandEarly = CommandCheck (Exactly "alias") (f . arguments)
+  where
+    f = mapM_ checkArg
+    checkArg arg | '=' `elem` concat (oversimplify arg) =
+        forM_ (take 1 $ filter (not . isLiteral) $ getWordParts arg) $
+            \x -> warn (getId x) 2139 "This expands when defined, not when used. Consider escaping."
+    checkArg _ = return ()
+
+
+prop_checkUnsetGlobs1 = verify checkUnsetGlobs "unset foo[1]"
+prop_checkUnsetGlobs2 = verifyNot checkUnsetGlobs "unset foo"
+checkUnsetGlobs = CommandCheck (Exactly "unset") (mapM_ check . arguments)
+  where
+    check arg =
+        when (isGlob arg) $
+            warn (getId arg) 2184 "Quote arguments to unset so they're not glob expanded."
+
+
+prop_checkFindWithoutPath1 = verify checkFindWithoutPath "find -type f"
+prop_checkFindWithoutPath2 = verify checkFindWithoutPath "find"
+prop_checkFindWithoutPath3 = verifyNot checkFindWithoutPath "find . -type f"
+prop_checkFindWithoutPath4 = verifyNot checkFindWithoutPath "find -H -L \"$path\" -print"
+prop_checkFindWithoutPath5 = verifyNot checkFindWithoutPath "find -O3 ."
+prop_checkFindWithoutPath6 = verifyNot checkFindWithoutPath "find -D exec ."
+checkFindWithoutPath = CommandCheck (Basename "find") f
+  where
+    f (T_SimpleCommand _ _ (cmd:args)) =
+        unless (hasPath args) $
+            info (getId cmd) 2185 "Some finds don't have a default path. Specify '.' explicitly."
+
+    -- This is a bit of a kludge. find supports flag arguments both before and after the path,
+    -- as well as multiple non-flag arguments that are not the path. We assume that all the
+    -- pre-path flags are single characters, which is generally the case except for -O3.
+    hasPath (first:rest) =
+        let flag = fromJust $ getLiteralStringExt (const $ return "___") first in
+            not ("-" `isPrefixOf` flag) || isLeadingFlag flag && hasPath rest
+    hasPath [] = False
+    isLeadingFlag flag = length flag <= 2 || "-O" `isPrefixOf` flag
+
+
+prop_checkTimeParameters1 = verify checkTimeParameters "time -f lol sleep 10"
+prop_checkTimeParameters2 = verifyNot checkTimeParameters "time sleep 10"
+prop_checkTimeParameters3 = verifyNot checkTimeParameters "time -p foo"
+prop_checkTimeParameters4 = verifyNot checkTimeParameters "command time -f lol sleep 10"
+checkTimeParameters = CommandCheck (Exactly "time") f
+  where
+    f (T_SimpleCommand _ _ (cmd:args:_)) =
+        whenShell [Bash, Sh] $
+            let s = concat $ oversimplify args in
+                when ("-" `isPrefixOf` s && s /= "-p") $
+                    info (getId cmd) 2023 "The shell may override 'time' as seen in man time(1). Use 'command time ..' for that one."
+
+    f _ = return ()
+
+prop_checkTimedCommand1 = verify checkTimedCommand "#!/bin/sh\ntime -p foo | bar"
+prop_checkTimedCommand2 = verify checkTimedCommand "#!/bin/dash\ntime ( foo; bar; )"
+prop_checkTimedCommand3 = verifyNot checkTimedCommand "#!/bin/sh\ntime sleep 1"
+checkTimedCommand = CommandCheck (Exactly "time") f where
+    f (T_SimpleCommand _ _ (c:args@(_:_))) =
+        whenShell [Sh, Dash] $ do
+            let cmd = last args -- "time" is parsed with a command as argument
+            when (isPiped cmd) $
+                warn (getId c) 2176 "'time' is undefined for pipelines. time single stage or bash -c instead."
+            when (isSimple cmd == Just False) $
+                warn (getId cmd) 2177 "'time' is undefined for compound commands, time sh -c instead."
+    f _ = return ()
+    isPiped cmd =
+        case cmd of
+            T_Pipeline _ _ (_:_:_) -> True
+            _ -> False
+    getCommand cmd =
+        case cmd of
+            T_Pipeline _ _ (T_Redirecting _ _ a : _) -> return a
+            _ -> fail ""
+    isSimple cmd = do
+        innerCommand <- getCommand cmd
+        case innerCommand of
+            T_SimpleCommand {} -> return True
+            _ -> return False
+
+prop_checkLocalScope1 = verify checkLocalScope "local foo=3"
+prop_checkLocalScope2 = verifyNot checkLocalScope "f() { local foo=3; }"
+checkLocalScope = CommandCheck (Exactly "local") $ \t ->
+    whenShell [Bash, Dash] $ do -- Ksh allows it, Sh doesn't support local
+        path <- getPathM t
+        unless (any isFunction path) $
+            err (getId t) 2168 "'local' is only valid in functions."
+
+prop_checkDeprecatedTempfile1 = verify checkDeprecatedTempfile "var=$(tempfile)"
+prop_checkDeprecatedTempfile2 = verifyNot checkDeprecatedTempfile "tempfile=$(mktemp)"
+checkDeprecatedTempfile = CommandCheck (Basename "tempfile") $
+    \t -> warn (getId t) 2186 "tempfile is deprecated. Use mktemp instead."
+
+prop_checkDeprecatedEgrep = verify checkDeprecatedEgrep "egrep '.+'"
+checkDeprecatedEgrep = CommandCheck (Basename "egrep") $
+    \t -> info (getId t) 2196 "egrep is non-standard and deprecated. Use grep -E instead."
+
+prop_checkDeprecatedFgrep = verify checkDeprecatedFgrep "fgrep '*' files"
+checkDeprecatedFgrep = CommandCheck (Basename "fgrep") $
+    \t -> info (getId t) 2197 "fgrep is non-standard and deprecated. Use grep -F instead."
+
+prop_checkWhileGetoptsCase1 = verify checkWhileGetoptsCase "while getopts 'a:b' x; do case $x in a) foo;; esac; done"
+prop_checkWhileGetoptsCase2 = verify checkWhileGetoptsCase "while getopts 'a:' x; do case $x in a) foo;; b) bar;; esac; done"
+prop_checkWhileGetoptsCase3 = verifyNot checkWhileGetoptsCase "while getopts 'a:b' x; do case $x in a) foo;; b) bar;; *) :;esac; done"
+prop_checkWhileGetoptsCase4 = verifyNot checkWhileGetoptsCase "while getopts 'a:123' x; do case $x in a) foo;; [0-9]) bar;; esac; done"
+prop_checkWhileGetoptsCase5 = verifyNot checkWhileGetoptsCase "while getopts 'a:' x; do case $x in a) foo;; \\?) bar;; *) baz;; esac; done"
+checkWhileGetoptsCase = CommandCheck (Exactly "getopts") f
+  where
+    f :: Token -> Analysis
+    f t@(T_SimpleCommand _ _ (cmd:arg1:_))  = do
+        path <- getPathM t
+        potentially $ do
+            options <- getLiteralString arg1
+            (T_WhileExpression _ _ body) <- findFirst whileLoop path
+            caseCmd <- mapMaybe findCase body !!! 0
+            return $ check (getId arg1) (map (:[]) $ filter (/= ':') options) caseCmd
+    f _ = return ()
+
+    check :: Id -> [String] -> Token -> Analysis
+    check optId opts (T_CaseExpression id _ list) = do
+            unless (Nothing `Map.member` handledMap) $ do
+                mapM_ (warnUnhandled optId id) $ catMaybes $ Map.keys notHandled
+
+                unless (any (`Map.member` handledMap) [Just "*",Just "?"]) $
+                    warn id 2220 "Invalid flags are not handled. Add a *) case."
+
+            mapM_ warnRedundant $ Map.toList notRequested
+
+        where
+            handledMap = Map.fromList (concatMap getHandledStrings list)
+            requestedMap = Map.fromList $ map (\x -> (Just x, ())) opts
+
+            notHandled = Map.difference requestedMap handledMap
+            notRequested = Map.difference handledMap requestedMap
+
+    warnUnhandled optId caseId str =
+        warn caseId 2213 $ "getopts specified -" ++ str ++ ", but it's not handled by this 'case'."
+
+    warnRedundant (key, expr) = potentially $ do
+        str <- key
+        guard $ str `notElem` ["*", ":", "?"]
+        return $ warn (getId expr) 2214 "This case is not specified by getopts."
+
+    getHandledStrings (_, globs, _) =
+        map (\x -> (literal x, x)) globs
+
+    literal :: Token -> Maybe String
+    literal t = do
+        getLiteralString t <> fromGlob t
+
+    fromGlob t =
+        case t of
+            T_Glob _ ('[':c:']':[]) -> return [c]
+            T_Glob _ "*" -> return "*"
+            T_Glob _ "?" -> return "?"
+            _ -> Nothing
+
+    whileLoop t =
+        case t of
+            T_WhileExpression {} -> return True
+            T_Script {} -> return False
+            _ -> Nothing
+
+    findCase t =
+        case t of
+            T_Annotation _ _ x -> findCase x
+            T_Pipeline _ _ [x] -> findCase x
+            T_Redirecting _ _ x@(T_CaseExpression {}) -> return x
+            _ -> Nothing
+
+prop_checkCatastrophicRm1 = verify checkCatastrophicRm "rm -r $1/$2"
+prop_checkCatastrophicRm2 = verify checkCatastrophicRm "rm -r /home/$foo"
+prop_checkCatastrophicRm3 = verifyNot checkCatastrophicRm "rm -r /home/${USER:?}/*"
+prop_checkCatastrophicRm4 = verify checkCatastrophicRm "rm -fr /home/$(whoami)/*"
+prop_checkCatastrophicRm5 = verifyNot checkCatastrophicRm "rm -r /home/${USER:-thing}/*"
+prop_checkCatastrophicRm6 = verify checkCatastrophicRm "rm --recursive /etc/*$config*"
+prop_checkCatastrophicRm8 = verify checkCatastrophicRm "rm -rf /home"
+prop_checkCatastrophicRm10= verifyNot checkCatastrophicRm "rm -r \"${DIR}\"/{.gitignore,.gitattributes,ci}"
+prop_checkCatastrophicRm11= verify checkCatastrophicRm "rm -r /{bin,sbin}/$exec"
+prop_checkCatastrophicRm12= verify checkCatastrophicRm "rm -r /{{usr,},{bin,sbin}}/$exec"
+prop_checkCatastrophicRm13= verifyNot checkCatastrophicRm "rm -r /{{a,b},{c,d}}/$exec"
+prop_checkCatastrophicRmA = verify checkCatastrophicRm "rm -rf /usr /lib/nvidia-current/xorg/xorg"
+prop_checkCatastrophicRmB = verify checkCatastrophicRm "rm -rf \"$STEAMROOT/\"*"
+checkCatastrophicRm = CommandCheck (Basename "rm") $ \t ->
+    when (isRecursive t) $
+        mapM_ (mapM_ checkWord . braceExpand) $ arguments t
+  where
+    isRecursive = any (`elem` ["r", "R", "recursive"]) . map snd . getAllFlags
+
+    checkWord token =
+        case getLiteralString token of
+            Just str ->
+                when (fixPath str `elem` importantPaths) $
+                    warn (getId token) 2114 "Warning: deletes a system directory."
+            Nothing ->
+                checkWord' token
+
+    checkWord' token = fromMaybe (return ()) $ do
+        filename <- getPotentialPath token
+        let path = fixPath filename
+        return . when (path `elem` importantPaths) $
+            warn (getId token) 2115 $ "Use \"${var:?}\" to ensure this never expands to " ++ path ++ " ."
+
+    fixPath filename =
+        let normalized = skipRepeating '/' . skipRepeating '*' $ filename in
+            if normalized == "/" then normalized else stripTrailing '/' normalized
+
+    getPotentialPath = getLiteralStringExt f
+      where
+        f (T_Glob _ str) = return str
+        f (T_DollarBraced _ word) =
+            let var = onlyLiteralString word in
+                -- This shouldn't handle non-colon cases.
+                if any (`isInfixOf` var) [":?", ":-", ":="]
+                then Nothing
+                else return ""
+        f _ = return ""
+
+    stripTrailing c = reverse . dropWhile (== c) . reverse
+    skipRepeating c (a:b:rest) | a == b && b == c = skipRepeating c (b:rest)
+    skipRepeating c (a:r) = a:skipRepeating c r
+    skipRepeating _ [] = []
+
+    paths = [
+        "", "/bin", "/etc", "/home", "/mnt", "/usr", "/usr/share", "/usr/local",
+        "/var", "/lib", "/dev", "/media", "/boot", "/lib64", "/usr/bin"
+        ]
+    importantPaths = filter (not . null) $
+        ["", "/", "/*", "/*/*"] >>= (\x -> map (++x) paths)
+
+
+prop_checkLetUsage1 = verify checkLetUsage "let a=1"
+prop_checkLetUsage2 = verifyNot checkLetUsage "(( a=1 ))"
+checkLetUsage = CommandCheck (Exactly "let") f
+  where
+    f t = whenShell [Bash,Ksh] $ do
+        style (getId t) 2219 $ "Instead of 'let expr', prefer (( expr )) ."
+
+
+missingDestination handler token = do
+    case params of
+        [single] -> do
+            unless (hasTarget || mayBecomeMultipleArgs single) $
+                handler token
+        _ -> return ()
+  where
+    args = getAllFlags token
+    params = map fst $ filter (\(_,x) -> x == "") args
+    hasTarget =
+        any (\x -> x /= "" && x `isPrefixOf` "target-directory") $
+            map snd args
+
+prop_checkMvArguments1 = verify    checkMvArguments "mv 'foo bar'"
+prop_checkMvArguments2 = verifyNot checkMvArguments "mv foo bar"
+prop_checkMvArguments3 = verifyNot checkMvArguments "mv 'foo bar'{,bak}"
+prop_checkMvArguments4 = verifyNot checkMvArguments "mv \"$@\""
+prop_checkMvArguments5 = verifyNot checkMvArguments "mv -t foo bar"
+prop_checkMvArguments6 = verifyNot checkMvArguments "mv --target-directory=foo bar"
+prop_checkMvArguments7 = verifyNot checkMvArguments "mv --target-direc=foo bar"
+prop_checkMvArguments8 = verifyNot checkMvArguments "mv --version"
+prop_checkMvArguments9 = verifyNot checkMvArguments "mv \"${!var}\""
+checkMvArguments = CommandCheck (Basename "mv") $ missingDestination f
+  where
+    f t = err (getId t) 2224 "This mv has no destination. Check the arguments."
+
+checkCpArguments = CommandCheck (Basename "cp") $ missingDestination f
+  where
+    f t = err (getId t) 2225 "This cp has no destination. Check the arguments."
+
+checkLnArguments = CommandCheck (Basename "ln") $ missingDestination f
+  where
+    f t = warn (getId t) 2226 "This ln has no destination. Check the arguments, or specify '.' explicitly."
+
+
+prop_checkFindRedirections1 = verify    checkFindRedirections "find . -exec echo {} > file \\;"
+prop_checkFindRedirections2 = verifyNot checkFindRedirections "find . -exec echo {} \\; > file"
+prop_checkFindRedirections3 = verifyNot checkFindRedirections "find . -execdir sh -c 'foo > file' \\;"
+checkFindRedirections = CommandCheck (Basename "find") f
+  where
+    f t = do
+        redirecting <- getClosestCommandM t
+        case redirecting of
+            Just (T_Redirecting _ redirs@(_:_) (T_SimpleCommand _ _ args@(_:_:_))) -> do
+                -- This assumes IDs are sequential, which is mostly but not always true.
+                let minRedir = minimum $ map getId redirs
+                let maxArg   = maximum $ map getId args
+                when (minRedir < maxArg) $
+                    warn minRedir 2227
+                        "Redirection applies to the find command itself. Rewrite to work per action (or move to end)."
+            _ -> return ()
+
+prop_checkWhich = verify checkWhich "which '.+'"
+checkWhich = CommandCheck (Basename "which") $
+    \t -> info (getId t) 2230 "which is non-standard. Use builtin 'command -v' instead."
+
+prop_checkSudoRedirect1 = verify checkSudoRedirect "sudo echo 3 > /proc/file"
+prop_checkSudoRedirect2 = verify checkSudoRedirect "sudo cmd < input"
+prop_checkSudoRedirect3 = verify checkSudoRedirect "sudo cmd >> file"
+prop_checkSudoRedirect4 = verify checkSudoRedirect "sudo cmd &> file"
+prop_checkSudoRedirect5 = verifyNot checkSudoRedirect "sudo cmd 2>&1"
+prop_checkSudoRedirect6 = verifyNot checkSudoRedirect "sudo cmd 2> log"
+prop_checkSudoRedirect7 = verifyNot checkSudoRedirect "sudo cmd > /dev/null 2>&1"
+checkSudoRedirect = CommandCheck (Basename "sudo") f
+  where
+    f t = do
+        t_redir <- getClosestCommandM t
+        case t_redir of
+            Just (T_Redirecting _ redirs _) ->
+                mapM_ warnAbout redirs
+    warnAbout (T_FdRedirect _ s (T_IoFile id op file))
+        | (s == "" || s == "&") && not (special file) =
+        case op of
+            T_Less _ ->
+              info (getId op) 2024
+                "sudo doesn't affect redirects. Use sudo cat file | .."
+            T_Greater _ ->
+              warn (getId op) 2024
+                "sudo doesn't affect redirects. Use ..| sudo tee file"
+            T_DGREAT _ ->
+              warn (getId op) 2024
+                "sudo doesn't affect redirects. Use .. | sudo tee -a file"
+            _ -> return ()
+    warnAbout _ = return ()
+    special file = concat (oversimplify file) == "/dev/null"
+
+prop_checkSudoArgs1 = verify checkSudoArgs "sudo cd /root"
+prop_checkSudoArgs2 = verify checkSudoArgs "sudo export x=3"
+prop_checkSudoArgs3 = verifyNot checkSudoArgs "sudo ls /usr/local/protected"
+prop_checkSudoArgs4 = verifyNot checkSudoArgs "sudo ls && export x=3"
+prop_checkSudoArgs5 = verifyNot checkSudoArgs "sudo echo ls"
+prop_checkSudoArgs6 = verifyNot checkSudoArgs "sudo -n -u export ls"
+prop_checkSudoArgs7 = verifyNot checkSudoArgs "sudo docker export foo"
+checkSudoArgs = CommandCheck (Basename "sudo") f
+  where
+    f t = potentially $ do
+        opts <- parseOpts t
+        let nonFlags = map snd $ filter (\(flag, _) -> flag == "") opts
+        commandArg <- nonFlags !!! 0
+        command <- getLiteralString commandArg
+        guard $ command `elem` builtins
+        return $ warn (getId t) 2232 $ "Can't use sudo with builtins like " ++ command ++ ". Did you want sudo sh -c .. instead?"
+    builtins = [ "cd", "eval", "export", "history", "read", "source", "wait" ]
+    -- This mess is why ShellCheck prefers not to know.
+    parseOpts = getBsdOpts "vAknSbEHPa:g:h:p:u:c:T:r:"
+
+return []
+runTests =  $( [| $(forAllProperties) (quickCheckWithResult (stdArgs { maxSuccess = 1 }) ) |])
diff --git a/src/ShellCheck/Checks/ShellSupport.hs b/src/ShellCheck/Checks/ShellSupport.hs
new file mode 100644
--- /dev/null
+++ b/src/ShellCheck/Checks/ShellSupport.hs
@@ -0,0 +1,423 @@
+{-
+    Copyright 2012-2016 Vidar Holen
+
+    This file is part of ShellCheck.
+    https://www.shellcheck.net
+
+    ShellCheck is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    ShellCheck is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <https://www.gnu.org/licenses/>.
+-}
+{-# LANGUAGE TemplateHaskell #-}
+{-# LANGUAGE FlexibleContexts #-}
+module ShellCheck.Checks.ShellSupport (checker , ShellCheck.Checks.ShellSupport.runTests) where
+
+import ShellCheck.AST
+import ShellCheck.ASTLib
+import ShellCheck.AnalyzerLib
+import ShellCheck.Interface
+import ShellCheck.Regex
+
+import Control.Monad
+import Control.Monad.RWS
+import Data.Char
+import Data.List
+import Data.Maybe
+import qualified Data.Map as Map
+import Test.QuickCheck.All (forAllProperties)
+import Test.QuickCheck.Test (quickCheckWithResult, stdArgs, maxSuccess)
+
+data ForShell = ForShell [Shell] (Token -> Analysis)
+
+getChecker params list = Checker {
+        perScript = nullCheck,
+        perToken = foldl composeAnalyzers nullCheck $ mapMaybe include list
+    }
+  where
+    shell = shellType params
+    include (ForShell list a) = do
+        guard $ shell `elem` list
+        return a
+
+checker params = getChecker params checks
+
+checks = [
+    checkForDecimals
+    ,checkBashisms
+    ,checkEchoSed
+    ,checkBraceExpansionVars
+    ,checkMultiDimensionalArrays
+    ,checkPS1Assignments
+    ]
+
+testChecker (ForShell _ t) =
+    Checker {
+        perScript = nullCheck,
+        perToken = t
+    }
+verify c s = producesComments (testChecker c) s == Just True
+verifyNot c s = producesComments (testChecker c) s == Just False
+
+prop_checkForDecimals1 = verify checkForDecimals "((3.14*c))"
+prop_checkForDecimals2 = verify checkForDecimals "foo[1.2]=bar"
+prop_checkForDecimals3 = verifyNot checkForDecimals "declare -A foo; foo[1.2]=bar"
+checkForDecimals = ForShell [Sh, Dash, Bash] f
+  where
+    f t@(TA_Expansion id _) = potentially $ do
+        str <- getLiteralString t
+        first <- str !!! 0
+        guard $ isDigit first && '.' `elem` str
+        return $ err id 2079 "(( )) doesn't support decimals. Use bc or awk."
+    f _ = return ()
+
+
+prop_checkBashisms = verify checkBashisms "while read a; do :; done < <(a)"
+prop_checkBashisms2 = verify checkBashisms "[ foo -nt bar ]"
+prop_checkBashisms3 = verify checkBashisms "echo $((i++))"
+prop_checkBashisms4 = verify checkBashisms "rm !(*.hs)"
+prop_checkBashisms5 = verify checkBashisms "source file"
+prop_checkBashisms6 = verify checkBashisms "[ \"$a\" == 42 ]"
+prop_checkBashisms7 = verify checkBashisms "echo ${var[1]}"
+prop_checkBashisms8 = verify checkBashisms "echo ${!var[@]}"
+prop_checkBashisms9 = verify checkBashisms "echo ${!var*}"
+prop_checkBashisms10= verify checkBashisms "echo ${var:4:12}"
+prop_checkBashisms11= verifyNot checkBashisms "echo ${var:-4}"
+prop_checkBashisms12= verify checkBashisms "echo ${var//foo/bar}"
+prop_checkBashisms13= verify checkBashisms "exec -c env"
+prop_checkBashisms14= verify checkBashisms "echo -n \"Foo: \""
+prop_checkBashisms15= verify checkBashisms "let n++"
+prop_checkBashisms16= verify checkBashisms "echo $RANDOM"
+prop_checkBashisms17= verify checkBashisms "echo $((RANDOM%6+1))"
+prop_checkBashisms18= verify checkBashisms "foo &> /dev/null"
+prop_checkBashisms19= verify checkBashisms "foo > file*.txt"
+prop_checkBashisms20= verify checkBashisms "read -ra foo"
+prop_checkBashisms21= verify checkBashisms "[ -a foo ]"
+prop_checkBashisms22= verifyNot checkBashisms "[ foo -a bar ]"
+prop_checkBashisms23= verify checkBashisms "trap mything ERR INT"
+prop_checkBashisms24= verifyNot checkBashisms "trap mything INT TERM"
+prop_checkBashisms25= verify checkBashisms "cat < /dev/tcp/host/123"
+prop_checkBashisms26= verify checkBashisms "trap mything ERR SIGTERM"
+prop_checkBashisms27= verify checkBashisms "echo *[^0-9]*"
+prop_checkBashisms28= verify checkBashisms "exec {n}>&2"
+prop_checkBashisms29= verify checkBashisms "echo ${!var}"
+prop_checkBashisms30= verify checkBashisms "printf -v '%s' \"$1\""
+prop_checkBashisms31= verify checkBashisms "printf '%q' \"$1\""
+prop_checkBashisms32= verifyNot checkBashisms "#!/bin/dash\n[ foo -nt bar ]"
+prop_checkBashisms33= verify checkBashisms "#!/bin/sh\necho -n foo"
+prop_checkBashisms34= verifyNot checkBashisms "#!/bin/dash\necho -n foo"
+prop_checkBashisms35= verifyNot checkBashisms "#!/bin/dash\nlocal foo"
+prop_checkBashisms36= verifyNot checkBashisms "#!/bin/dash\nread -p foo -r bar"
+prop_checkBashisms37= verifyNot checkBashisms "HOSTNAME=foo; echo $HOSTNAME"
+prop_checkBashisms38= verify checkBashisms "RANDOM=9; echo $RANDOM"
+prop_checkBashisms39= verify checkBashisms "foo-bar() { true; }"
+prop_checkBashisms40= verify checkBashisms "echo $(<file)"
+prop_checkBashisms41= verify checkBashisms "echo `<file`"
+prop_checkBashisms42= verify checkBashisms "trap foo int"
+prop_checkBashisms43= verify checkBashisms "trap foo sigint"
+prop_checkBashisms44= verifyNot checkBashisms "#!/bin/dash\ntrap foo int"
+prop_checkBashisms45= verifyNot checkBashisms "#!/bin/dash\ntrap foo INT"
+prop_checkBashisms46= verify checkBashisms "#!/bin/dash\ntrap foo SIGINT"
+prop_checkBashisms47= verify checkBashisms "#!/bin/dash\necho foo 42>/dev/null"
+prop_checkBashisms48= verifyNot checkBashisms "#!/bin/dash\necho $LINENO"
+prop_checkBashisms49= verify checkBashisms "#!/bin/dash\necho $MACHTYPE"
+prop_checkBashisms50= verify checkBashisms "#!/bin/sh\ncmd >& file"
+prop_checkBashisms51= verifyNot checkBashisms "#!/bin/sh\ncmd 2>&1"
+prop_checkBashisms52= verifyNot checkBashisms "#!/bin/sh\ncmd >&2"
+prop_checkBashisms53= verifyNot checkBashisms "#!/bin/sh\nprintf -- -f\n"
+prop_checkBashisms54= verify checkBashisms "#!/bin/sh\nfoo+=bar"
+prop_checkBashisms55= verify checkBashisms "#!/bin/sh\necho ${@%foo}"
+prop_checkBashisms56= verifyNot checkBashisms "#!/bin/sh\necho ${##}"
+checkBashisms = ForShell [Sh, Dash] $ \t -> do
+    params <- ask
+    kludge params t
+ where
+  -- This code was copy-pasted from Analytics where params was a variable
+  kludge params = bashism
+   where
+    isDash = shellType params == Dash
+    warnMsg id s =
+        if isDash
+        then warn id 2169 $ "In dash, " ++ s ++ " not supported."
+        else warn id 2039 $ "In POSIX sh, " ++ s ++ " undefined."
+
+    bashism (T_ProcSub id _ _) = warnMsg id "process substitution is"
+    bashism (T_Extglob id _ _) = warnMsg id "extglob is"
+    bashism (T_DollarSingleQuoted id _) = warnMsg id "$'..' is"
+    bashism (T_DollarDoubleQuoted id _) = warnMsg id "$\"..\" is"
+    bashism (T_ForArithmetic id _ _ _ _) = warnMsg id "arithmetic for loops are"
+    bashism (T_Arithmetic id _) = warnMsg id "standalone ((..)) is"
+    bashism (T_DollarBracket id _) = warnMsg id "$[..] in place of $((..)) is"
+    bashism (T_SelectIn id _ _ _) = warnMsg id "select loops are"
+    bashism (T_BraceExpansion id _) = warnMsg id "brace expansion is"
+    bashism (T_Condition id DoubleBracket _) = warnMsg id "[[ ]] is"
+    bashism (T_HereString id _) = warnMsg id "here-strings are"
+    bashism (TC_Binary id SingleBracket op _ _)
+        | op `elem` [ "<", ">", "\\<", "\\>", "<=", ">=", "\\<=", "\\>="] =
+            unless isDash $ warnMsg id $ "lexicographical " ++ op ++ " is"
+    bashism (TC_Binary id SingleBracket op _ _)
+        | op `elem` [ "-nt", "-ef" ] =
+            unless isDash $ warnMsg id $ op ++ " is"
+    bashism (TC_Binary id SingleBracket "==" _ _) =
+            warnMsg id "== in place of = is"
+    bashism (TC_Binary id SingleBracket "=~" _ _) =
+            warnMsg id "=~ regex matching is"
+    bashism (TC_Unary id _ "-a" _) =
+            warnMsg id "unary -a in place of -e is"
+    bashism (TA_Unary id op _)
+        | op `elem` [ "|++", "|--", "++|", "--|"] =
+            warnMsg id $ filter (/= '|') op ++ " is"
+    bashism (TA_Binary id "**" _ _) = warnMsg id "exponentials are"
+    bashism (T_FdRedirect id "&" (T_IoFile _ (T_Greater _) _)) = warnMsg id "&> is"
+    bashism (T_FdRedirect id "" (T_IoFile _ (T_GREATAND _) _)) = warnMsg id ">& is"
+    bashism (T_FdRedirect id ('{':_) _) = warnMsg id "named file descriptors are"
+    bashism (T_FdRedirect id num _)
+        | all isDigit num && length num > 1 = warnMsg id "FDs outside 0-9 are"
+    bashism (T_Assignment id Append _ _ _) =
+        warnMsg id "+= is"
+    bashism (T_IoFile id _ word) | isNetworked =
+            warnMsg id "/dev/{tcp,udp} is"
+        where
+            file = onlyLiteralString word
+            isNetworked = any (`isPrefixOf` file) ["/dev/tcp", "/dev/udp"]
+    bashism (T_Glob id str) | "[^" `isInfixOf` str =
+            warnMsg id "^ in place of ! in glob bracket expressions is"
+
+    bashism t@(TA_Variable id str _) | isBashVariable str =
+        warnMsg id $ str ++ " is"
+
+    bashism t@(T_DollarBraced id token) = do
+        mapM_ check expansion
+        when (isBashVariable var) $
+                    warnMsg id $ var ++ " is"
+      where
+        str = bracedString t
+        var = getBracedReference str
+        check (regex, feature) =
+            when (isJust $ matchRegex regex str) $ warnMsg id feature
+
+    bashism t@(T_Pipe id "|&") =
+        warnMsg id "|& in place of 2>&1 | is"
+    bashism (T_Array id _) =
+        warnMsg id "arrays are"
+    bashism (T_IoFile id _ t) | isGlob t =
+        warnMsg id "redirecting to/from globs is"
+    bashism (T_CoProc id _ _) =
+        warnMsg id "coproc is"
+
+    bashism (T_Function id _ _ str _) | not (isVariableName str) =
+        warnMsg id "naming functions outside [a-zA-Z_][a-zA-Z0-9_]* is"
+
+    bashism (T_DollarExpansion id [x]) | isOnlyRedirection x =
+        warnMsg id "$(<file) to read files is"
+    bashism (T_Backticked id [x]) | isOnlyRedirection x =
+        warnMsg id "`<file` to read files is"
+
+    bashism t@(T_SimpleCommand _ _ (cmd:arg:_))
+        | t `isCommand` "echo" && "-" `isPrefixOf` argString =
+            unless ("--" `isPrefixOf` argString) $ -- echo "-----"
+                if isDash
+                then
+                    when (argString /= "-n") $
+                        warnMsg (getId arg) "echo flags besides -n"
+                else
+                    warnMsg (getId arg) "echo flags are"
+      where argString = concat $ oversimplify arg
+    bashism t@(T_SimpleCommand _ _ (cmd:arg:_))
+        | t `isCommand` "exec" && "-" `isPrefixOf` concat (oversimplify arg) =
+            warnMsg (getId arg) "exec flags are"
+    bashism t@(T_SimpleCommand id _ _)
+        | t `isCommand` "let" = warnMsg id "'let' is"
+
+    bashism t@(T_SimpleCommand id _ (cmd:rest)) =
+        let name = fromMaybe "" $ getCommandName t
+            flags = getLeadingFlags t
+        in do
+            when (name `elem` unsupportedCommands) $
+                warnMsg id $ "'" ++ name ++ "' is"
+            potentially $ do
+                allowed <- Map.lookup name allowedFlags
+                (word, flag) <- listToMaybe $
+                    filter (\x -> (not . null . snd $ x) && snd x `notElem` allowed) flags
+                return . warnMsg (getId word) $ name ++ " -" ++ flag ++ " is"
+
+            when (name == "source") $ warnMsg id "'source' in place of '.' is"
+            when (name == "trap") $
+                let
+                    check token = potentially $ do
+                        str <- getLiteralString token
+                        let upper = map toUpper str
+                        return $ do
+                            when (upper `elem` ["ERR", "DEBUG", "RETURN"]) $
+                                warnMsg (getId token) $ "trapping " ++ str ++ " is"
+                            when ("SIG" `isPrefixOf` upper) $
+                                warnMsg (getId token)
+                                    "prefixing signal names with 'SIG' is"
+                            when (not isDash && upper /= str) $
+                                warnMsg (getId token)
+                                    "using lower/mixed case for signal names is"
+                in
+                    mapM_ check (drop 1 rest)
+
+            when (name == "printf") $ potentially $ do
+                format <- rest !!! 0  -- flags are covered by allowedFlags
+                let literal = onlyLiteralString format
+                guard $ "%q" `isInfixOf` literal
+                return $ warnMsg (getId format) "printf %q is"
+      where
+        unsupportedCommands = [
+            "let", "caller", "builtin", "complete", "compgen", "declare", "dirs", "disown",
+            "enable", "mapfile", "readarray", "pushd", "popd", "shopt", "suspend",
+            "typeset"
+            ] ++ if not isDash then ["local"] else []
+        allowedFlags = Map.fromList [
+            ("exec", []),
+            ("export", ["-p"]),
+            ("printf", []),
+            ("read", if isDash then ["r", "p"] else ["r"]),
+            ("ulimit", ["f"])
+            ]
+    bashism t@(T_SourceCommand id src _) =
+        let name = fromMaybe "" $ getCommandName src
+        in do
+            when (name == "source") $ warnMsg id "'source' in place of '.' is"
+    bashism _ = return ()
+
+    varChars="_0-9a-zA-Z"
+    expansion = let re = mkRegex in [
+        (re $ "^![" ++ varChars ++ "]", "indirect expansion is"),
+        (re $ "^[" ++ varChars ++ "]+\\[.*\\]$", "array references are"),
+        (re $ "^![" ++ varChars ++ "]+\\[[*@]]$", "array key expansion is"),
+        (re $ "^![" ++ varChars ++ "]+[*@]$", "name matching prefixes are"),
+        (re $ "^[" ++ varChars ++ "*@]+:[^-=?+]", "string indexing is"),
+        (re $ "^([*@][%#]|#[@*])", "string operations on $@/$* are"),
+        (re $ "^[" ++ varChars ++ "*@]+(\\[.*\\])?/", "string replacement is")
+        ]
+    bashVars = [
+        "LINENO", "OSTYPE", "MACHTYPE", "HOSTTYPE", "HOSTNAME",
+        "DIRSTACK", "EUID", "UID", "SHLVL", "PIPESTATUS", "SHELLOPTS"
+        ]
+    bashDynamicVars = [ "RANDOM", "SECONDS" ]
+    dashVars = [ "LINENO" ]
+    isBashVariable var =
+        (var `elem` bashDynamicVars
+            || var `elem` bashVars && not (isAssigned var))
+        && not (isDash && var `elem` dashVars)
+    isAssigned var = any f (variableFlow params)
+      where
+        f x = case x of
+                Assignment (_, _, name, _) -> name == var
+                _ -> False
+
+prop_checkEchoSed1 = verify checkEchoSed "FOO=$(echo \"$cow\" | sed 's/foo/bar/g')"
+prop_checkEchoSed2 = verify checkEchoSed "rm $(echo $cow | sed -e 's,foo,bar,')"
+checkEchoSed = ForShell [Bash, Ksh] f
+  where
+    f (T_Pipeline id _ [a, b]) =
+        when (acmd == ["echo", "${VAR}"]) $
+            case bcmd of
+                ["sed", v] -> checkIn v
+                ["sed", "-e", v] -> checkIn v
+                _ -> return ()
+      where
+        -- This should have used backreferences, but TDFA doesn't support them
+        sedRe = mkRegex "^s(.)([^\n]*)g?$"
+        isSimpleSed s = fromMaybe False $ do
+            [first,rest] <- matchRegex sedRe s
+            let delimiters = filter (== head first) rest
+            guard $ length delimiters == 2
+            return True
+
+        acmd = oversimplify a
+        bcmd = oversimplify b
+        checkIn s =
+            when (isSimpleSed s) $
+                style id 2001 "See if you can use ${variable//search/replace} instead."
+    f _ = return ()
+
+
+prop_checkBraceExpansionVars1 = verify checkBraceExpansionVars "echo {1..$n}"
+prop_checkBraceExpansionVars2 = verifyNot checkBraceExpansionVars "echo {1,3,$n}"
+prop_checkBraceExpansionVars3 = verify checkBraceExpansionVars "eval echo DSC{0001..$n}.jpg"
+prop_checkBraceExpansionVars4 = verify checkBraceExpansionVars "echo {$i..100}"
+checkBraceExpansionVars = ForShell [Bash] f
+  where
+    f t@(T_BraceExpansion id list) = mapM_ check list
+      where
+        check element =
+            when (any (`isInfixOf` toString element) ["$..", "..$"]) $ do
+                c <- isEvaled element
+                if c
+                    then style id 2175 "Quote this invalid brace expansion since it should be passed literally to eval."
+                    else warn id 2051 "Bash doesn't support variables in brace range expansions."
+    f _ = return ()
+
+    literalExt t =
+        case t of
+            T_DollarBraced {} -> return "$"
+            T_DollarExpansion {} -> return "$"
+            T_DollarArithmetic {} -> return "$"
+            otherwise -> return "-"
+    toString t = fromJust $ getLiteralStringExt literalExt t
+    isEvaled t = do
+        cmd <- getClosestCommandM t
+        return $ isJust cmd && fromJust cmd `isUnqualifiedCommand` "eval"
+
+
+prop_checkMultiDimensionalArrays1 = verify checkMultiDimensionalArrays "foo[a][b]=3"
+prop_checkMultiDimensionalArrays2 = verifyNot checkMultiDimensionalArrays "foo[a]=3"
+prop_checkMultiDimensionalArrays3 = verify checkMultiDimensionalArrays "foo=( [a][b]=c )"
+prop_checkMultiDimensionalArrays4 = verifyNot checkMultiDimensionalArrays "foo=( [a]=c )"
+prop_checkMultiDimensionalArrays5 = verify checkMultiDimensionalArrays "echo ${foo[bar][baz]}"
+prop_checkMultiDimensionalArrays6 = verifyNot checkMultiDimensionalArrays "echo ${foo[bar]}"
+checkMultiDimensionalArrays = ForShell [Bash] f
+  where
+    f token =
+        case token of
+            T_Assignment _ _ name (first:second:_) _ -> about second
+            T_IndexedElement _ (first:second:_) _ -> about second
+            T_DollarBraced {} ->
+                when (isMultiDim token) $ about token
+            _ -> return ()
+    about t = warn (getId t) 2180 "Bash does not support multidimensional arrays. Use 1D or associative arrays."
+
+    re = mkRegex "^\\[.*\\]\\[.*\\]"  -- Fixme, this matches ${foo:- [][]} and such as well
+    isMultiDim t = getBracedModifier (bracedString t) `matches` re
+
+prop_checkPS11 = verify checkPS1Assignments "PS1='\\033[1;35m\\$ '"
+prop_checkPS11a= verify checkPS1Assignments "export PS1='\\033[1;35m\\$ '"
+prop_checkPSf2 = verify checkPS1Assignments "PS1='\\h \\e[0m\\$ '"
+prop_checkPS13 = verify checkPS1Assignments "PS1=$'\\x1b[c '"
+prop_checkPS14 = verify checkPS1Assignments "PS1=$'\\e[3m; '"
+prop_checkPS14a= verify checkPS1Assignments "export PS1=$'\\e[3m; '"
+prop_checkPS15 = verifyNot checkPS1Assignments "PS1='\\[\\033[1;35m\\]\\$ '"
+prop_checkPS16 = verifyNot checkPS1Assignments "PS1='\\[\\e1m\\e[1m\\]\\$ '"
+prop_checkPS17 = verifyNot checkPS1Assignments "PS1='e033x1B'"
+prop_checkPS18 = verifyNot checkPS1Assignments "PS1='\\[\\e\\]'"
+checkPS1Assignments = ForShell [Bash] f
+  where
+    f token = case token of
+        (T_Assignment _ _ "PS1" _ word) -> warnFor word
+        _ -> return ()
+
+    warnFor word =
+        let contents = concat $ oversimplify word in
+            when (containsUnescaped contents) $
+                info (getId word) 2025 "Make sure all escape sequences are enclosed in \\[..\\] to prevent line wrapping issues"
+    containsUnescaped s =
+        let unenclosed = subRegex enclosedRegex s "" in
+           isJust $ matchRegex escapeRegex unenclosed
+    enclosedRegex = mkRegex "\\\\\\[.*\\\\\\]" -- FIXME: shouldn't be eager
+    escapeRegex = mkRegex "\\\\x1[Bb]|\\\\e|\x1B|\\\\033"
+
+
+return []
+runTests =  $( [| $(forAllProperties) (quickCheckWithResult (stdArgs { maxSuccess = 1 }) ) |])
diff --git a/src/ShellCheck/Data.hs b/src/ShellCheck/Data.hs
new file mode 100644
--- /dev/null
+++ b/src/ShellCheck/Data.hs
@@ -0,0 +1,117 @@
+module ShellCheck.Data where
+
+import ShellCheck.Interface
+import Data.Version (showVersion)
+import Paths_ShellCheck (version)
+
+shellcheckVersion = showVersion version
+
+internalVariables = [
+    -- Generic
+    "", "_", "rest", "REST",
+
+    -- Bash
+    "BASH", "BASHOPTS", "BASHPID", "BASH_ALIASES", "BASH_ARGC",
+    "BASH_ARGV", "BASH_CMDS", "BASH_COMMAND", "BASH_EXECUTION_STRING",
+    "BASH_LINENO", "BASH_REMATCH", "BASH_SOURCE", "BASH_SUBSHELL",
+    "BASH_VERSINFO", "BASH_VERSION", "COMP_CWORD", "COMP_KEY",
+    "COMP_LINE", "COMP_POINT", "COMP_TYPE", "COMP_WORDBREAKS",
+    "COMP_WORDS", "COPROC", "DIRSTACK", "EUID", "FUNCNAME", "GROUPS",
+    "HISTCMD", "HOSTNAME", "HOSTTYPE", "LINENO", "MACHTYPE", "MAPFILE",
+    "OLDPWD", "OPTARG", "OPTIND", "OSTYPE", "PIPESTATUS", "PPID", "PWD",
+    "RANDOM", "READLINE_LINE", "READLINE_POINT", "REPLY", "SECONDS",
+    "SHELLOPTS", "SHLVL", "UID", "BASH_ENV", "BASH_XTRACEFD", "CDPATH",
+    "COLUMNS", "COMPREPLY", "EMACS", "ENV", "FCEDIT", "FIGNORE",
+    "FUNCNEST", "GLOBIGNORE", "HISTCONTROL", "HISTFILE", "HISTFILESIZE",
+    "HISTIGNORE", "HISTSIZE", "HISTTIMEFORMAT", "HOME", "HOSTFILE", "IFS",
+    "IGNOREEOF", "INPUTRC", "LANG", "LC_ALL", "LC_COLLATE", "LC_CTYPE",
+    "LC_MESSAGES", "LC_MONETARY", "LC_NUMERIC", "LC_TIME", "LINES", "MAIL",
+    "MAILCHECK", "MAILPATH", "OPTERR", "PATH", "POSIXLY_CORRECT",
+    "PROMPT_COMMAND", "PROMPT_DIRTRIM", "PS1", "PS2", "PS3", "PS4", "SHELL",
+    "TIMEFORMAT", "TMOUT", "TMPDIR", "auto_resume", "histchars", "COPROC",
+
+    -- Other
+    "USER", "TZ", "TERM", "LOGNAME", "LD_LIBRARY_PATH", "LANGUAGE", "DISPLAY",
+    "HOSTNAME", "KRB5CCNAME", "XAUTHORITY"
+
+    -- Ksh
+    , ".sh.version"
+  ]
+
+variablesWithoutSpaces = [
+    "$", "-", "?", "!",
+    "BASHPID", "BASH_ARGC", "BASH_LINENO", "BASH_SUBSHELL", "EUID", "LINENO",
+    "OPTIND", "PPID", "RANDOM", "SECONDS", "SHELLOPTS", "SHLVL", "UID",
+    "COLUMNS", "HISTFILESIZE", "HISTSIZE", "LINES"
+  ]
+
+arrayVariables = [
+    "BASH_ALIASES", "BASH_ARGC", "BASH_ARGV", "BASH_CMDS", "BASH_LINENO",
+    "BASH_REMATCH", "BASH_SOURCE", "BASH_VERSINFO", "COMP_WORDS", "COPROC",
+    "DIRSTACK", "FUNCNAME", "GROUPS", "MAPFILE", "PIPESTATUS", "COMPREPLY"
+  ]
+
+commonCommands = [
+    "admin", "alias", "ar", "asa", "at", "awk", "basename", "batch",
+    "bc", "bg", "break", "c99", "cal", "cat", "cd", "cflow", "chgrp",
+    "chmod", "chown", "cksum", "cmp", "colon", "comm", "command",
+    "compress", "continue", "cp", "crontab", "csplit", "ctags", "cut",
+    "cxref", "date", "dd", "delta", "df", "diff", "dirname", "dot",
+    "du", "echo", "ed", "env", "eval", "ex", "exec", "exit", "expand",
+    "export", "expr", "fc", "fg", "file", "find", "fold", "fort77",
+    "fuser", "gencat", "get", "getconf", "getopts", "grep", "hash",
+    "head", "iconv", "ipcrm", "ipcs", "jobs", "join", "kill", "lex",
+    "link", "ln", "locale", "localedef", "logger", "logname", "lp",
+    "ls", "m4", "mailx", "make", "man", "mesg", "mkdir", "mkfifo",
+    "more", "mv", "newgrp", "nice", "nl", "nm", "nohup", "od", "paste",
+    "patch", "pathchk", "pax", "pr", "printf", "prs", "ps", "pwd",
+    "qalter", "qdel", "qhold", "qmove", "qmsg", "qrerun", "qrls",
+    "qselect", "qsig", "qstat", "qsub", "read", "readonly", "renice",
+    "return", "rm", "rmdel", "rmdir", "sact", "sccs", "sed", "set",
+    "sh", "shift", "sleep", "sort", "split", "strings", "strip", "stty",
+    "tabs", "tail", "talk", "tee", "test", "time", "times", "touch",
+    "tput", "tr", "trap", "tsort", "tty", "type", "ulimit", "umask",
+    "unalias", "uname", "uncompress", "unexpand", "unget", "uniq",
+    "unlink", "unset", "uucp", "uudecode", "uuencode", "uustat", "uux",
+    "val", "vi", "wait", "wc", "what", "who", "write", "xargs", "yacc",
+    "zcat"
+  ]
+
+nonReadingCommands = [
+    "alias", "basename", "bg", "cal", "cd", "chgrp", "chmod", "chown",
+    "cp", "du", "echo", "export", "false", "fg", "fuser", "getconf",
+    "getopt", "getopts", "ipcrm", "ipcs", "jobs", "kill", "ln", "ls",
+    "locale", "mv", "printf", "ps", "pwd", "renice", "rm", "rmdir",
+    "set", "sleep", "touch", "trap", "true", "ulimit", "unalias", "uname"
+    ]
+
+sampleWords = [
+    "alpha", "bravo", "charlie", "delta", "echo", "foxtrot",
+    "golf", "hotel", "india", "juliett", "kilo", "lima", "mike",
+    "november", "oscar", "papa", "quebec", "romeo", "sierra",
+    "tango", "uniform", "victor", "whiskey", "xray", "yankee",
+    "zulu"
+  ]
+
+binaryTestOps = [
+    "-nt", "-ot", "-ef", "==", "!=", "<=", ">=", "-eq", "-ne", "-lt", "-le",
+    "-gt", "-ge", "=~", ">", "<", "=", "\\<", "\\>", "\\<=", "\\>="
+  ]
+
+unaryTestOps = [
+    "!", "-a", "-b", "-c", "-d", "-e", "-f", "-g", "-h", "-L", "-k", "-p",
+    "-r", "-s", "-S", "-t", "-u", "-w", "-x", "-O", "-G", "-N", "-z", "-n",
+    "-o", "-v", "-R"
+  ]
+
+shellForExecutable :: String -> Maybe Shell
+shellForExecutable name =
+    case name of
+        "sh"    -> return Sh
+        "bash"  -> return Bash
+        "dash"  -> return Dash
+        "ash"   -> return Dash -- There's also a warning for this.
+        "ksh"   -> return Ksh
+        "ksh88" -> return Ksh
+        "ksh93" -> return Ksh
+        otherwise -> Nothing
diff --git a/src/ShellCheck/Formatter/CheckStyle.hs b/src/ShellCheck/Formatter/CheckStyle.hs
new file mode 100644
--- /dev/null
+++ b/src/ShellCheck/Formatter/CheckStyle.hs
@@ -0,0 +1,95 @@
+{-
+    Copyright 2012-2015 Vidar Holen
+
+    This file is part of ShellCheck.
+    https://www.shellcheck.net
+
+    ShellCheck is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    ShellCheck is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <https://www.gnu.org/licenses/>.
+-}
+module ShellCheck.Formatter.CheckStyle (format) where
+
+import ShellCheck.Interface
+import ShellCheck.Formatter.Format
+
+import Data.Char
+import Data.List
+import GHC.Exts
+import System.IO
+
+format :: IO Formatter
+format = return Formatter {
+    header = do
+        putStrLn "<?xml version='1.0' encoding='UTF-8'?>"
+        putStrLn "<checkstyle version='4.3'>",
+
+    onFailure = outputError,
+    onResult = outputResults,
+
+    footer = putStrLn "</checkstyle>"
+}
+
+outputResults cr sys =
+    if null comments
+    then outputFile (crFilename cr) "" []
+    else mapM_ outputGroup fileGroups
+  where
+    comments = crComments cr
+    fileGroups = groupWith sourceFile comments
+    outputGroup group = do
+        let filename = sourceFile (head group)
+        result <- (siReadFile sys) filename
+        let contents = either (const "") id result
+        outputFile filename contents group
+
+outputFile filename contents warnings = do
+    let comments = makeNonVirtual warnings contents
+    putStrLn . formatFile filename $ comments
+
+formatFile name comments = concat [
+    "<file ", attr "name" name, ">\n",
+        concatMap formatComment comments,
+    "</file>"
+    ]
+
+formatComment c = concat [
+    "<error ",
+    attr "line" $ show . lineNo $ c,
+    attr "column" $ show . colNo $ c,
+    attr "severity" . severity $ severityText c,
+    attr "message" $ messageText c,
+    attr "source" $ "ShellCheck.SC" ++ show (codeNo c),
+    "/>\n"
+    ]
+
+outputError file error = putStrLn $ concat [
+    "<file ", attr "name" file, ">\n",
+    "<error ",
+        attr "line" "1",
+        attr "column" "1",
+        attr "severity" "error",
+        attr "message" error,
+        attr "source" "ShellCheck",
+    "/>\n",
+    "</file>"
+    ]
+
+
+attr s v = concat [ s, "='", escape v, "' " ]
+escape = concatMap escape'
+escape' c = if isOk c then [c] else "&#" ++ show (ord c) ++ ";"
+isOk x = any ($x) [isAsciiUpper, isAsciiLower, isDigit, (`elem` " ./")]
+
+severity "error" = "error"
+severity "warning" = "warning"
+severity _ = "info"
diff --git a/src/ShellCheck/Formatter/Format.hs b/src/ShellCheck/Formatter/Format.hs
new file mode 100644
--- /dev/null
+++ b/src/ShellCheck/Formatter/Format.hs
@@ -0,0 +1,67 @@
+{-
+    Copyright 2012-2015 Vidar Holen
+
+    This file is part of ShellCheck.
+    https://www.shellcheck.net
+
+    ShellCheck is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    ShellCheck is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <https://www.gnu.org/licenses/>.
+-}
+module ShellCheck.Formatter.Format where
+
+import ShellCheck.Data
+import ShellCheck.Interface
+
+-- A formatter that carries along an arbitrary piece of data
+data Formatter = Formatter {
+    header ::  IO (),
+    onResult :: CheckResult -> SystemInterface IO -> IO (),
+    onFailure :: FilePath -> ErrorMessage -> IO (),
+    footer :: IO ()
+}
+
+sourceFile (PositionedComment pos _ _) = posFile pos
+lineNo (PositionedComment pos _ _) = posLine pos
+endLineNo (PositionedComment _ end _) = posLine end
+colNo  (PositionedComment pos _ _) = posColumn pos
+endColNo  (PositionedComment _ end _) = posColumn end
+codeNo (PositionedComment _ _ (Comment _ code _)) = code
+messageText (PositionedComment _ _ (Comment _ _ t)) = t
+
+severityText :: PositionedComment -> String
+severityText (PositionedComment _ _ (Comment c _ _)) =
+    case c of
+        ErrorC   -> "error"
+        WarningC -> "warning"
+        InfoC    -> "info"
+        StyleC   -> "style"
+
+-- Realign comments from a tabstop of 8 to 1
+makeNonVirtual comments contents =
+    map fix comments
+  where
+    ls = lines contents
+    fix c@(PositionedComment start end comment) = PositionedComment start {
+        posColumn = realignColumn lineNo colNo c
+    } end {
+        posColumn = realignColumn endLineNo endColNo c
+    } comment
+    realignColumn lineNo colNo c =
+      if lineNo c > 0 && lineNo c <= fromIntegral (length ls)
+      then real (ls !! fromIntegral (lineNo c - 1)) 0 0 (colNo c)
+      else colNo c
+    real _ r v target | target <= v = r
+    real [] r v _ = r -- should never happen
+    real ('\t':rest) r v target =
+        real rest (r+1) (v + 8 - (v `mod` 8)) target
+    real (_:rest) r v target = real rest (r+1) (v+1) target
diff --git a/src/ShellCheck/Formatter/GCC.hs b/src/ShellCheck/Formatter/GCC.hs
new file mode 100644
--- /dev/null
+++ b/src/ShellCheck/Formatter/GCC.hs
@@ -0,0 +1,65 @@
+{-
+    Copyright 2012-2015 Vidar Holen
+
+    This file is part of ShellCheck.
+    https://www.shellcheck.net
+
+    ShellCheck is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    ShellCheck is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <https://www.gnu.org/licenses/>.
+-}
+module ShellCheck.Formatter.GCC (format) where
+
+import ShellCheck.Interface
+import ShellCheck.Formatter.Format
+
+import Data.List
+import GHC.Exts
+import System.IO
+
+format :: IO Formatter
+format = return Formatter {
+    header = return (),
+    footer = return (),
+    onFailure = outputError,
+    onResult = outputAll
+}
+
+outputError file error = hPutStrLn stderr $ file ++ ": " ++ error
+
+outputAll cr sys = mapM_ f groups
+  where
+    comments = crComments cr
+    groups = groupWith sourceFile comments
+    f :: [PositionedComment] -> IO ()
+    f group = do
+        let filename = sourceFile (head group)
+        result <- (siReadFile sys) filename
+        let contents = either (const "") id result
+        outputResult filename contents group
+
+outputResult filename contents warnings = do
+    let comments = makeNonVirtual warnings contents
+    mapM_ (putStrLn . formatComment filename) comments
+
+formatComment filename c = concat [
+    filename, ":",
+    show $ lineNo c, ":",
+    show $ colNo c, ": ",
+    case severityText c of
+        "error" -> "error"
+        "warning" -> "warning"
+        _ -> "note",
+    ": ",
+    concat . lines $ messageText c,
+    " [SC", show $ codeNo c, "]"
+  ]
diff --git a/src/ShellCheck/Formatter/JSON.hs b/src/ShellCheck/Formatter/JSON.hs
new file mode 100644
--- /dev/null
+++ b/src/ShellCheck/Formatter/JSON.hs
@@ -0,0 +1,74 @@
+{-# LANGUAGE OverloadedStrings #-}
+{-
+    Copyright 2012-2015 Vidar Holen
+
+    This file is part of ShellCheck.
+    https://www.shellcheck.net
+
+    ShellCheck is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    ShellCheck is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <https://www.gnu.org/licenses/>.
+-}
+module ShellCheck.Formatter.JSON (format) where
+
+import ShellCheck.Interface
+import ShellCheck.Formatter.Format
+
+import Data.Aeson
+import Data.IORef
+import Data.Monoid
+import GHC.Exts
+import System.IO
+import qualified Data.ByteString.Lazy.Char8 as BL
+
+format = do
+    ref <- newIORef []
+    return Formatter {
+        header = return (),
+        onResult = collectResult ref,
+        onFailure = outputError,
+        footer = finish ref
+    }
+
+instance ToJSON (PositionedComment) where
+  toJSON comment@(PositionedComment start end (Comment level code string)) =
+    object [
+      "file" .= posFile start,
+      "line" .= posLine start,
+      "endLine" .= posLine end,
+      "column" .= posColumn start,
+      "endColumn" .= posColumn end,
+      "level" .= severityText comment,
+      "code" .= code,
+      "message" .= string
+    ]
+
+  toEncoding comment@(PositionedComment start end (Comment level code string)) =
+    pairs (
+         "file" .= posFile start
+      <> "line" .= posLine start
+      <> "endLine" .= posLine end
+      <> "column" .= posColumn start
+      <> "endColumn" .= posColumn end
+      <> "level" .= severityText comment
+      <> "code" .= code
+      <> "message" .= string
+    )
+
+outputError file msg = hPutStrLn stderr $ file ++ ": " ++ msg
+collectResult ref result _ =
+    modifyIORef ref (\x -> crComments result ++ x)
+
+finish ref = do
+    list <- readIORef ref
+    BL.putStrLn $ encode list
+
diff --git a/src/ShellCheck/Formatter/TTY.hs b/src/ShellCheck/Formatter/TTY.hs
new file mode 100644
--- /dev/null
+++ b/src/ShellCheck/Formatter/TTY.hs
@@ -0,0 +1,98 @@
+{-
+    Copyright 2012-2015 Vidar Holen
+
+    This file is part of ShellCheck.
+    https://www.shellcheck.net
+
+    ShellCheck is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    ShellCheck is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <https://www.gnu.org/licenses/>.
+-}
+module ShellCheck.Formatter.TTY (format) where
+
+import ShellCheck.Interface
+import ShellCheck.Formatter.Format
+
+import Data.List
+import GHC.Exts
+import System.Info
+import System.IO
+
+format :: FormatterOptions -> IO Formatter
+format options = return Formatter {
+    header = return (),
+    footer = return (),
+    onFailure = outputError options,
+    onResult = outputResult options
+}
+
+colorForLevel level =
+    case level of
+        "error"   -> 31 -- red
+        "warning" -> 33 -- yellow
+        "info"    -> 32 -- green
+        "style"   -> 32 -- green
+        "message" -> 1 -- bold
+        "source"  -> 0 -- none
+        _ -> 0         -- none
+
+outputError options file error = do
+    color <- getColorFunc $ foColorOption options
+    hPutStrLn stderr $ color "error" $ file ++ ": " ++ error
+
+outputResult options result sys = do
+    color <- getColorFunc $ foColorOption options
+    let comments = crComments result
+    let fileGroups = groupWith sourceFile comments
+    mapM_ (outputForFile color sys) fileGroups
+
+outputForFile color sys comments = do
+    let fileName = sourceFile (head comments)
+    result <- (siReadFile sys) fileName
+    let contents = either (const "") id result
+    let fileLines = lines contents
+    let lineCount = fromIntegral $ length fileLines
+    let groups = groupWith lineNo comments
+    mapM_ (\x -> do
+        let lineNum = lineNo (head x)
+        let line = if lineNum < 1 || lineNum > lineCount
+                        then ""
+                        else fileLines !! fromIntegral (lineNum - 1)
+        putStrLn ""
+        putStrLn $ color "message" $
+           "In " ++ fileName ++" line " ++ show lineNum ++ ":"
+        putStrLn (color "source" line)
+        mapM_ (\c -> putStrLn (color (severityText c) $ cuteIndent c)) x
+        putStrLn ""
+      ) groups
+
+cuteIndent :: PositionedComment -> String
+cuteIndent comment =
+    replicate (fromIntegral $ colNo comment - 1) ' ' ++
+        "^-- " ++ code (codeNo comment) ++ ": " ++ messageText comment
+
+code code = "SC" ++ show code
+
+getColorFunc colorOption = do
+    term <- hIsTerminalDevice stdout
+    let windows = "mingw" `isPrefixOf` os
+    let isUsableTty = term && not windows
+    let useColor = case colorOption of
+                       ColorAlways -> True
+                       ColorNever -> False
+                       ColorAuto -> isUsableTty
+    return $ if useColor then colorComment else const id
+  where
+    colorComment level comment =
+        ansi (colorForLevel level) ++ comment ++ clear
+    clear = ansi 0
+    ansi n = "\x1B[" ++ show n ++ "m"
diff --git a/src/ShellCheck/Interface.hs b/src/ShellCheck/Interface.hs
new file mode 100644
--- /dev/null
+++ b/src/ShellCheck/Interface.hs
@@ -0,0 +1,121 @@
+{-
+    Copyright 2012-2015 Vidar Holen
+
+    This file is part of ShellCheck.
+    https://www.shellcheck.net
+
+    ShellCheck is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    ShellCheck is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <https://www.gnu.org/licenses/>.
+-}
+module ShellCheck.Interface where
+
+import ShellCheck.AST
+import Control.Monad.Identity
+import qualified Data.Map as Map
+
+
+newtype SystemInterface m = SystemInterface {
+    -- Read a file by filename, or return an error
+    siReadFile :: String -> m (Either ErrorMessage String)
+}
+
+-- ShellCheck input and output
+data CheckSpec = CheckSpec {
+    csFilename :: String,
+    csScript :: String,
+    csCheckSourced :: Bool,
+    csExcludedWarnings :: [Integer],
+    csShellTypeOverride :: Maybe Shell
+} deriving (Show, Eq)
+
+data CheckResult = CheckResult {
+    crFilename :: String,
+    crComments :: [PositionedComment]
+} deriving (Show, Eq)
+
+emptyCheckSpec :: CheckSpec
+emptyCheckSpec = CheckSpec {
+    csFilename = "",
+    csScript = "",
+    csCheckSourced = False,
+    csExcludedWarnings = [],
+    csShellTypeOverride = Nothing
+}
+
+-- Parser input and output
+data ParseSpec = ParseSpec {
+    psFilename :: String,
+    psScript :: String,
+    psCheckSourced :: Bool
+} deriving (Show, Eq)
+
+data ParseResult = ParseResult {
+    prComments :: [PositionedComment],
+    prTokenPositions :: Map.Map Id Position,
+    prRoot :: Maybe Token
+} deriving (Show, Eq)
+
+-- Analyzer input and output
+data AnalysisSpec = AnalysisSpec {
+    asScript :: Token,
+    asShellType :: Maybe Shell,
+    asExecutionMode :: ExecutionMode,
+    asCheckSourced :: Bool
+}
+
+newtype AnalysisResult = AnalysisResult {
+    arComments :: [TokenComment]
+}
+
+
+-- Formatter options
+newtype FormatterOptions = FormatterOptions {
+    foColorOption :: ColorOption
+}
+
+
+-- Supporting data types
+data Shell = Ksh | Sh | Bash | Dash deriving (Show, Eq)
+data ExecutionMode = Executed | Sourced deriving (Show, Eq)
+
+type ErrorMessage = String
+type Code = Integer
+
+data Severity = ErrorC | WarningC | InfoC | StyleC deriving (Show, Eq, Ord)
+data Position = Position {
+    posFile :: String,    -- Filename
+    posLine :: Integer,   -- 1 based source line
+    posColumn :: Integer  -- 1 based source column, where tabs are 8
+} deriving (Show, Eq)
+
+data Comment = Comment Severity Code String deriving (Show, Eq)
+data PositionedComment = PositionedComment Position Position Comment deriving (Show, Eq)
+data TokenComment = TokenComment Id Comment deriving (Show, Eq)
+
+data ColorOption =
+    ColorAuto
+    | ColorAlways
+    | ColorNever
+  deriving (Ord, Eq, Show)
+
+-- For testing
+mockedSystemInterface :: [(String, String)] -> SystemInterface Identity
+mockedSystemInterface files = SystemInterface {
+    siReadFile = rf
+}
+  where
+    rf file =
+        case filter ((== file) . fst) files of
+            [] -> return $ Left "File not included in mock."
+            [(_, contents)] -> return $ Right contents
+
diff --git a/src/ShellCheck/Parser.hs b/src/ShellCheck/Parser.hs
new file mode 100644
--- /dev/null
+++ b/src/ShellCheck/Parser.hs
@@ -0,0 +1,3129 @@
+{-
+    Copyright 2012-2015 Vidar Holen
+
+    This file is part of ShellCheck.
+    https://www.shellcheck.net
+
+    ShellCheck is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    ShellCheck is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <https://www.gnu.org/licenses/>.
+-}
+{-# LANGUAGE TemplateHaskell #-}
+{-# LANGUAGE NoMonomorphismRestriction #-}
+{-# LANGUAGE FlexibleContexts #-}
+{-# LANGUAGE MultiWayIf #-}
+module ShellCheck.Parser (parseScript, runTests) where
+
+import ShellCheck.AST
+import ShellCheck.ASTLib
+import ShellCheck.Data
+import ShellCheck.Interface
+
+import Control.Applicative ((<*), (*>))
+import Control.Monad
+import Control.Monad.Identity
+import Control.Monad.Trans
+import Data.Char
+import Data.Functor
+import Data.List (isPrefixOf, isInfixOf, isSuffixOf, partition, sortBy, intercalate, nub)
+import Data.Maybe
+import Data.Monoid
+import Debug.Trace
+import GHC.Exts (sortWith)
+import Prelude hiding (readList)
+import System.IO
+import Text.Parsec hiding (runParser, (<?>))
+import Text.Parsec.Error
+import Text.Parsec.Pos
+import qualified Control.Monad.Reader as Mr
+import qualified Control.Monad.State as Ms
+import qualified Data.Map as Map
+
+import Test.QuickCheck.All (quickCheckAll)
+
+type SCBase m = Mr.ReaderT (Environment m) (Ms.StateT SystemState m)
+type SCParser m v = ParsecT String UserState (SCBase m) v
+
+backslash :: Monad m => SCParser m Char
+backslash = char '\\'
+linefeed :: Monad m => SCParser m Char
+linefeed = do
+    optional carriageReturn
+    c <- char '\n'
+    readPendingHereDocs
+    return c
+singleQuote = char '\''
+doubleQuote = char '"'
+variableStart = upper <|> lower <|> oneOf "_"
+variableChars = upper <|> lower <|> digit <|> oneOf "_"
+-- Chars to allow in function names
+functionChars = variableChars <|> oneOf ":+?-./^"
+-- Chars to allow in functions using the 'function' keyword
+extendedFunctionChars = functionChars <|> oneOf "[]*=!"
+specialVariable = oneOf "@*#?-$!"
+paramSubSpecialChars = oneOf "/:+-=%"
+quotableChars = "|&;<>()\\ '\t\n\r\xA0" ++ doubleQuotableChars
+quotable = almostSpace <|> oneOf quotableChars
+bracedQuotable = oneOf "}\"$`'"
+doubleQuotableChars = "\\\"$`"
+doubleQuotable = oneOf doubleQuotableChars
+whitespace = oneOf " \t" <|> carriageReturn <|> almostSpace <|> linefeed
+linewhitespace = oneOf " \t" <|> almostSpace
+
+suspectCharAfterQuotes = variableChars <|> char '%'
+
+extglobStartChars = "?*@!+"
+extglobStart = oneOf extglobStartChars
+
+unicodeDoubleQuotes = "\x201C\x201D\x2033\x2036"
+unicodeSingleQuotes = "\x2018\x2019"
+
+prop_spacing = isOk spacing "  \\\n # Comment"
+spacing = do
+    x <- many (many1 linewhitespace <|> try (string "\\\n" >> return ""))
+    optional readComment
+    return $ concat x
+
+spacing1 = do
+    spacing <- spacing
+    when (null spacing) $ fail "Expected whitespace"
+    return spacing
+
+prop_allspacing = isOk allspacing "#foo"
+prop_allspacing2 = isOk allspacing " #foo\n # bar\n#baz\n"
+prop_allspacing3 = isOk allspacing "#foo\n#bar\n#baz\n"
+allspacing = do
+    s <- spacing
+    more <- option False (linefeed >> return True)
+    if more then do
+        rest <- allspacing
+        return $ s ++ "\n" ++ rest
+      else
+        return s
+
+allspacingOrFail = do
+    s <- allspacing
+    when (null s) $ fail "Expected whitespace"
+
+readUnicodeQuote = do
+    pos <- getPosition
+    c <- oneOf (unicodeSingleQuotes ++ unicodeDoubleQuotes)
+    parseProblemAt pos WarningC 1110 "This is a unicode quote. Delete and retype it (or quote to make literal)."
+    id <- getNextIdAt pos
+    return $ T_Literal id [c]
+
+carriageReturn = do
+    parseNote ErrorC 1017 "Literal carriage return. Run script through tr -d '\\r' ."
+    char '\r'
+
+almostSpace =
+    choice [
+        check '\xA0' "unicode non-breaking space",
+        check '\x200B' "unicode zerowidth space"
+    ]
+  where
+    check c name = do
+        parseNote ErrorC 1018 $ "This is a " ++ name ++ ". Delete and retype it."
+        char c
+        return ' '
+
+--------- Message/position annotation on top of user state
+data Note = Note Id Severity Code String deriving (Show, Eq)
+data ParseNote = ParseNote SourcePos SourcePos Severity Code String deriving (Show, Eq)
+data Context =
+        ContextName SourcePos String
+        | ContextAnnotation [Annotation]
+        | ContextSource String
+    deriving (Show)
+
+data HereDocContext =
+        HereDocPending Token [Context] -- on linefeed, read this T_HereDoc
+    deriving (Show)
+
+data UserState = UserState {
+    lastId :: Id,
+    positionMap :: Map.Map Id SourcePos,
+    parseNotes :: [ParseNote],
+    hereDocMap :: Map.Map Id [Token],
+    pendingHereDocs :: [HereDocContext]
+}
+initialUserState = UserState {
+    lastId = Id $ -1,
+    positionMap = Map.empty,
+    parseNotes = [],
+    hereDocMap = Map.empty,
+    pendingHereDocs = []
+}
+
+codeForParseNote (ParseNote _ _ _ code _) = code
+noteToParseNote map (Note id severity code message) =
+        ParseNote pos pos severity code message
+    where
+        pos = fromJust $ Map.lookup id map
+
+getLastId = lastId <$> getState
+
+getNextIdAt sourcepos = do
+    state <- getState
+    let newId = incId (lastId state)
+    let newMap = Map.insert newId sourcepos (positionMap state)
+    putState $ state {
+        lastId = newId,
+        positionMap = newMap
+    }
+    return newId
+  where incId (Id n) = Id $ n+1
+
+getNextId :: Monad m => SCParser m Id
+getNextId = do
+    pos <- getPosition
+    getNextIdAt pos
+
+addToHereDocMap id list = do
+    state <- getState
+    let map = hereDocMap state
+    putState $ state {
+        hereDocMap = Map.insert id list map
+    }
+
+addPendingHereDoc t = do
+    state <- getState
+    context <- getCurrentContexts
+    let docs = pendingHereDocs state
+    putState $ state {
+        pendingHereDocs = HereDocPending t context : docs
+    }
+
+popPendingHereDocs = do
+    state <- getState
+    let pending = pendingHereDocs state
+    putState $ state {
+        pendingHereDocs = []
+    }
+    return . reverse $ pendingHereDocs state
+
+getMap = positionMap <$> getState
+getParseNotes = parseNotes <$> getState
+
+addParseNote n = do
+    irrelevant <- shouldIgnoreCode (codeForParseNote n)
+    unless irrelevant $ do
+        state <- getState
+        putState $ state {
+            parseNotes = n : parseNotes state
+        }
+
+shouldIgnoreCode code = do
+    context <- getCurrentContexts
+    checkSourced <- Mr.asks checkSourced
+    return $ any (disabling checkSourced) context
+  where
+    disabling checkSourced item =
+        case item of
+            ContextAnnotation list -> any disabling' list
+            ContextSource _ -> not $ checkSourced
+            _ -> False
+    disabling' (DisableComment n) = code == n
+    disabling' _ = False
+
+shouldFollow file = do
+    context <- getCurrentContexts
+    if any isThisFile context
+      then return False
+      else
+        if length (filter isSource context) >= 100
+          then do
+            parseProblem ErrorC 1092 "Stopping at 100 'source' frames :O"
+            return False
+          else
+            return True
+  where
+    isSource (ContextSource _) = True
+    isSource _ = False
+    isThisFile (ContextSource name) | name == file = True
+    isThisFile _= False
+
+getSourceOverride = do
+    context <- getCurrentContexts
+    return . msum . map findFile $ takeWhile isSameFile context
+  where
+    isSameFile (ContextSource _) = False
+    isSameFile _ = True
+
+    findFile (ContextAnnotation list) = msum $ map getFile list
+    findFile _ = Nothing
+    getFile (SourceOverride str) = Just str
+    getFile _ = Nothing
+
+-- Store potential parse problems outside of parsec
+
+data SystemState = SystemState {
+    contextStack :: [Context],
+    parseProblems :: [ParseNote]
+}
+initialSystemState = SystemState {
+    contextStack = [],
+    parseProblems = []
+}
+
+data Environment m = Environment {
+    systemInterface :: SystemInterface m,
+    checkSourced :: Bool
+}
+
+parseProblem level code msg = do
+    pos <- getPosition
+    parseProblemAt pos level code msg
+
+setCurrentContexts c = Ms.modify (\state -> state { contextStack = c })
+getCurrentContexts = contextStack <$> Ms.get
+
+popContext = do
+    v <- getCurrentContexts
+    if not $ null v
+        then do
+            let (a:r) = v
+            setCurrentContexts r
+            return $ Just a
+        else
+            return Nothing
+
+pushContext c = do
+    v <- getCurrentContexts
+    setCurrentContexts (c:v)
+
+parseProblemAtWithEnd start end level code msg = do
+    irrelevant <- shouldIgnoreCode code
+    unless irrelevant $
+        addParseProblem note
+  where
+    note = ParseNote start end level code msg
+
+addParseProblem note =
+    Ms.modify (\state -> state {
+        parseProblems = note:parseProblems state
+    })
+
+parseProblemAt pos = parseProblemAtWithEnd pos pos
+
+parseProblemAtId :: Monad m => Id -> Severity -> Integer -> String -> SCParser m ()
+parseProblemAtId id level code msg = do
+    map <- getMap
+    let pos = Map.findWithDefault
+                (error "Internal error (no position for id). Please report.") id map
+    parseProblemAt pos level code msg
+
+-- Store non-parse problems inside
+
+parseNote c l a = do
+    pos <- getPosition
+    parseNoteAt pos c l a
+
+parseNoteAt pos c l a = addParseNote $ ParseNote pos pos c l a
+
+parseNoteAtWithEnd start end c l a = addParseNote $ ParseNote start end c l a
+
+--------- Convenient combinators
+thenSkip main follow = do
+    r <- main
+    optional follow
+    return r
+
+unexpecting s p = try $
+    (try p >> fail ("Unexpected " ++ s)) <|> return ()
+
+notFollowedBy2 = unexpecting ""
+
+reluctantlyTill p end =
+    (lookAhead (void (try end) <|> eof) >> return []) <|> do
+        x <- p
+        more <- reluctantlyTill p end
+        return $ x:more
+      <|> return []
+
+reluctantlyTill1 p end = do
+    notFollowedBy2 end
+    x <- p
+    more <- reluctantlyTill p end
+    return $ x:more
+
+attempting rest branch =
+    (try branch >> rest) <|> rest
+
+orFail parser errorAction =
+    try parser <|> (errorAction >>= fail)
+
+-- Construct a node with a parser, e.g. T_Literal `withParser` (readGenericLiteral ",")
+withParser node parser = do
+    id <- getNextId
+    contents <- parser
+    return $ node id contents
+
+wasIncluded p = option False (p >> return True)
+
+acceptButWarn parser level code note =
+    optional $ try (do
+        pos <- getPosition
+        parser
+        parseProblemAt pos level code note
+      )
+
+parsecBracket before after op = do
+    val <- before
+    (op val <* optional (after val)) <|> (after val *> fail "")
+
+swapContext contexts p =
+    parsecBracket (getCurrentContexts <* setCurrentContexts contexts)
+                  setCurrentContexts
+                  (const p)
+
+withContext entry p = parsecBracket (pushContext entry) (const popContext) (const p)
+
+called s p = do
+    pos <- getPosition
+    withContext (ContextName pos s) p
+
+withAnnotations anns =
+    withContext (ContextAnnotation anns)
+
+readConditionContents single =
+    readCondContents `attempting` lookAhead (do
+                                pos <- getPosition
+                                s <- readVariableName
+                                when (s `elem` commonCommands) $
+                                    parseProblemAt pos WarningC 1014 "Use 'if cmd; then ..' to check exit code, or 'if [[ $(cmd) == .. ]]' to check output.")
+
+  where
+    spacingOrLf = condSpacing True
+    condSpacing required = do
+        pos <- getPosition
+        space <- allspacing
+        when (required && null space) $
+            parseProblemAt pos ErrorC 1035 "You are missing a required space here."
+        when (single && '\n' `elem` space) $
+            parseProblemAt pos ErrorC 1080 "When breaking lines in [ ], you need \\ before the linefeed."
+        return space
+
+    typ = if single then SingleBracket else DoubleBracket
+    readCondBinaryOp = try $ do
+        optional guardArithmetic
+        id <- getNextId
+        op <- getOp
+        spacingOrLf
+        return op
+      where
+        flaglessOps = [ "==", "!=", "<=", ">=", "=~", ">", "<", "=" ]
+
+        getOp = do
+            id <- getNextId
+            op <- readRegularOrEscaped anyOp
+            return $ TC_Binary id typ op
+
+        anyOp = flagOp <|> flaglessOp <|> fail
+                    "Expected comparison operator (don't wrap commands in []/[[]])"
+        flagOp = try $ do
+            s <- readOp
+            when (s == "-a" || s == "-o") $ fail "Unexpected operator"
+            return s
+        flaglessOp =
+            choice $ map (try . string) flaglessOps
+
+        -- hacks to read quoted operators without having to read a shell word
+    readEscaped p = try $ withEscape <|> withQuotes
+      where
+        withEscape = do
+            char '\\'
+            escaped <$> p
+        withQuotes = do
+            c <- oneOf "'\""
+            s <- p
+            char c
+            return $ escaped s
+        escaped s = if any (`elem` s) "<>()" then '\\':s else s
+
+    readRegularOrEscaped p = readEscaped p <|> p
+
+
+    guardArithmetic = do
+        try . lookAhead $ void (oneOf "+*/%") <|> void (string "- ")
+        parseProblem ErrorC 1076 $
+            if single
+            then "Trying to do math? Use e.g. [ $((i/2+7)) -ge 18 ]."
+            else "Trying to do math? Use e.g. [[ $((i/2+7)) -ge 18 ]]."
+
+    readCondUnaryExp = do
+      op <- readCondUnaryOp
+      pos <- getPosition
+      liftM op readCondWord `orFail` do
+          parseProblemAt pos ErrorC 1019 "Expected this to be an argument to the unary condition."
+          return "Expected an argument for the unary operator"
+
+    readCondUnaryOp = try $ do
+        id <- getNextId
+        s <- readOp
+        spacingOrLf
+        return $ TC_Unary id typ s
+
+    readOp = try $ do
+        char '-' <|> weirdDash
+        s <- many1 letter <|> fail "Expected a test operator"
+        return ('-':s)
+
+    weirdDash = do
+        pos <- getPosition
+        oneOf "\x058A\x05BE\x2010\x2011\x2012\x2013\x2014\x2015\xFE63\xFF0D"
+        parseProblemAt pos ErrorC 1100
+            "This is a unicode dash. Delete and retype as ASCII minus."
+        return '-'
+
+    readCondWord = do
+        notFollowedBy2 (try (spacing >> string "]"))
+        x <- readNormalWord
+        pos <- getPosition
+        when (endedWith "]" x) $ do
+            parseProblemAt pos ErrorC 1020 $
+                "You need a space before the " ++ (if single then "]" else "]]") ++ "."
+            fail "Missing space before ]"
+        when (single && endedWith ")" x) $ do
+            parseProblemAt pos ErrorC 1021
+                "You need a space before the \\)"
+            fail "Missing space before )"
+        void spacing
+        return x
+      where endedWith str (T_NormalWord id s@(_:_)) =
+                case last s of T_Literal id s -> str `isSuffixOf` s
+                               _ -> False
+            endedWith _ _ = False
+
+    readCondAndOp = do
+        id <- getNextId
+        x <- try (readAndOrOp "&&" False <|> readAndOrOp "-a" True)
+        return $ TC_And id typ x
+
+    readCondOrOp = do
+        optional guardArithmetic
+        id <- getNextId
+        x <- try (readAndOrOp "||" False <|> readAndOrOp "-o" True)
+        return $ TC_Or id typ x
+
+    readAndOrOp op requiresSpacing = do
+        optional $ lookAhead weirdDash
+        x <- string op
+        condSpacing requiresSpacing
+        return x
+
+    readCondNullaryOrBinary = do
+      id <- getNextId
+      x <- readCondWord `attempting` (do
+              pos <- getPosition
+              lookAhead (char '[')
+              parseProblemAt pos ErrorC 1026 $ if single
+                  then "If grouping expressions inside [..], use \\( ..\\)."
+                  else "If grouping expressions inside [[..]], use ( .. )."
+            )
+      (do
+            pos <- getPosition
+            isRegex <- regexOperatorAhead
+            op <- readCondBinaryOp
+            y <- if isRegex
+                    then readRegex
+                    else  readCondWord <|> (parseProblemAt pos ErrorC 1027 "Expected another argument for this operator." >> mzero)
+            return (x `op` y)
+          ) <|> ( do
+            checkTrailingOp x
+            return $ TC_Nullary id typ x
+          )
+
+    checkTrailingOp x = fromMaybe (return ()) $ do
+        (T_Literal id str) <- getTrailingUnquotedLiteral x
+        trailingOp <- listToMaybe (filter (`isSuffixOf` str) binaryTestOps)
+        return $ parseProblemAtId id ErrorC 1108 $
+            "You need a space before and after the " ++ trailingOp ++ " ."
+
+    readCondGroup = do
+        id <- getNextId
+        pos <- getPosition
+        lparen <- try $ readRegularOrEscaped (string "(")
+        when (single && lparen == "(") $
+            singleWarning pos
+        when (not single && lparen == "\\(") $
+            doubleWarning pos
+        condSpacing single
+        x <- readCondContents
+        cpos <- getPosition
+        rparen <- readRegularOrEscaped (string ")")
+        condSpacing single
+        when (single && rparen == ")") $
+            singleWarning cpos
+        when (not single && rparen == "\\)") $
+            doubleWarning cpos
+        return $ TC_Group id typ x
+
+      where
+        singleWarning pos =
+            parseProblemAt pos ErrorC 1028 "In [..] you have to escape \\( \\) or preferably combine [..] expressions."
+        doubleWarning pos =
+            parseProblemAt pos ErrorC 1029 "In [[..]] you shouldn't escape ( or )."
+
+
+    -- Currently a bit of a hack since parsing rules are obscure
+    regexOperatorAhead = lookAhead (do
+        try (string "=~") <|> try (string "~=")
+        return True)
+          <|> return False
+    readRegex = called "regex" $ do
+        id <- getNextId
+        parts <- many1 readPart
+        void spacing
+        return $ T_NormalWord id parts
+      where
+        readPart = choice [
+            readGroup,
+            readSingleQuoted,
+            readDoubleQuoted,
+            readDollarExpression,
+            readNormalLiteral "( ",
+            readPipeLiteral,
+            readGlobLiteral
+            ]
+        readGlobLiteral = do
+            id <- getNextId
+            s <- extglobStart <|> oneOf "{}[]$"
+            return $ T_Literal id [s]
+        readGroup = called "regex grouping" $ do
+            id <- getNextId
+            char '('
+            parts <- many (readPart <|> readRegexLiteral)
+            char ')'
+            return $ T_NormalWord id parts
+        readRegexLiteral = do
+            id <- getNextId
+            str <- readGenericLiteral1 (singleQuote <|> doubleQuotable <|> oneOf "()")
+            return $ T_Literal id str
+        readPipeLiteral = do
+            id <- getNextId
+            str <- string "|"
+            return $ T_Literal id str
+
+    readCondTerm = do
+        term <- readCondNot <|> readCondExpr
+        condSpacing False
+        return term
+
+    readCondNot = do
+        id <- getNextId
+        char '!'
+        spacingOrLf
+        expr <- readCondExpr
+        return $ TC_Unary id typ "!" expr
+
+    readCondExpr =
+      readCondGroup <|> readCondUnaryExp <|> readCondNullaryOrBinary
+
+    readCondOr = chainl1 readCondAnd readCondAndOp
+    readCondAnd = chainl1 readCondTerm readCondOrOp
+    readCondContents = readCondOr
+
+
+prop_a1 = isOk readArithmeticContents " n++ + ++c"
+prop_a2 = isOk readArithmeticContents "$N*4-(3,2)"
+prop_a3 = isOk readArithmeticContents "n|=2<<1"
+prop_a4 = isOk readArithmeticContents "n &= 2 **3"
+prop_a5 = isOk readArithmeticContents "1 |= 4 && n >>= 4"
+prop_a6 = isOk readArithmeticContents " 1 | 2 ||3|4"
+prop_a7 = isOk readArithmeticContents "3*2**10"
+prop_a8 = isOk readArithmeticContents "3"
+prop_a9 = isOk readArithmeticContents "a^!-b"
+prop_a10= isOk readArithmeticContents "! $?"
+prop_a11= isOk readArithmeticContents "10#08 * 16#f"
+prop_a12= isOk readArithmeticContents "\"$((3+2))\" + '37'"
+prop_a13= isOk readArithmeticContents "foo[9*y+x]++"
+prop_a14= isOk readArithmeticContents "1+`echo 2`"
+prop_a15= isOk readArithmeticContents "foo[`echo foo | sed s/foo/4/g` * 3] + 4"
+prop_a16= isOk readArithmeticContents "$foo$bar"
+prop_a17= isOk readArithmeticContents "i<(0+(1+1))"
+prop_a18= isOk readArithmeticContents "a?b:c"
+prop_a19= isOk readArithmeticContents "\\\n3 +\\\n  2"
+prop_a20= isOk readArithmeticContents "a ? b ? c : d : e"
+prop_a21= isOk readArithmeticContents "a ? b : c ? d : e"
+prop_a22= isOk readArithmeticContents "!!a"
+readArithmeticContents :: Monad m => SCParser m Token
+readArithmeticContents =
+    readSequence
+  where
+    spacing =
+        let lf = try (string "\\\n") >> return '\n'
+        in many (whitespace <|> lf)
+
+    splitBy x ops = chainl1 x (readBinary ops)
+    readBinary ops = readComboOp ops TA_Binary
+    readComboOp op token = do
+        id <- getNextId
+        op <- choice (map (\x -> try $ do
+                                        s <- string x
+                                        failIfIncompleteOp
+                                        return s
+                            ) op)
+        spacing
+        return $ token id op
+
+    failIfIncompleteOp = notFollowedBy2 $ oneOf "&|<>="
+
+    -- Read binary minus, but also check for -lt, -gt and friends:
+    readMinusOp = do
+        id <- getNextId
+        pos <- getPosition
+        try $ do
+            char '-'
+            failIfIncompleteOp
+        optional $ do
+            (str, alt) <- lookAhead . choice $ map tryOp [
+                ("lt", "<"),
+                ("gt", ">"),
+                ("le", "<="),
+                ("ge", ">="),
+                ("eq", "=="),
+                ("ne", "!=")
+              ]
+            parseProblemAt pos ErrorC 1106 $ "In arithmetic contexts, use " ++ alt ++ " instead of -" ++ str
+        spacing
+        return $ TA_Binary id "-"
+      where
+        tryOp (str, alt) = try $ do
+            string str
+            spacing1
+            return (str, alt)
+
+    readArrayIndex = do
+        id <- getNextId
+        char '['
+        pos <- getPosition
+        middle <- readStringForParser readArithmeticContents
+        char ']'
+        return $ T_UnparsedIndex id pos middle
+
+    literal s = do
+        id <- getNextId
+        string s
+        return $ T_Literal id s
+
+    readVariable = do
+        id <- getNextId
+        name <- readVariableName
+        indices <- many readArrayIndex
+        spacing
+        return $ TA_Variable id name indices
+
+    readExpansion = do
+        id <- getNextId
+        pieces <- many1 $ choice [
+            readSingleQuoted,
+            readDoubleQuoted,
+            readNormalDollar,
+            readBraced,
+            readUnquotedBackTicked,
+            literal "#",
+            readNormalLiteral "+-*/=%^,]?:"
+            ]
+        spacing
+        return $ TA_Expansion id pieces
+
+    readGroup = do
+        char '('
+        s <- readSequence
+        char ')'
+        spacing
+        return s
+
+    readArithTerm = readGroup <|> readVariable <|> readExpansion
+
+    readSequence = do
+        spacing
+        id <- getNextId
+        l <- readAssignment `sepBy` (char ',' >> spacing)
+        return $ TA_Sequence id l
+
+    readAssignment = chainr1 readTrinary readAssignmentOp
+    readAssignmentOp = readComboOp ["=", "*=", "/=", "%=", "+=", "-=", "<<=", ">>=", "&=", "^=", "|="] TA_Assignment
+
+    readTrinary = do
+        x <- readLogicalOr
+        do
+            id <- getNextId
+            string "?"
+            spacing
+            y <- readTrinary
+            string ":"
+            spacing
+            z <- readTrinary
+            return $ TA_Trinary id x y z
+         <|>
+          return x
+
+    readLogicalOr  = readLogicalAnd `splitBy` ["||"]
+    readLogicalAnd = readBitOr `splitBy` ["&&"]
+    readBitOr  = readBitXor `splitBy` ["|"]
+    readBitXor = readBitAnd `splitBy` ["^"]
+    readBitAnd = readEquated `splitBy` ["&"]
+    readEquated = readCompared `splitBy` ["==", "!="]
+    readCompared = readShift `splitBy` ["<=", ">=", "<", ">"]
+    readShift = readAddition `splitBy` ["<<", ">>"]
+    readAddition = chainl1 readMultiplication (readBinary ["+"] <|> readMinusOp)
+    readMultiplication = readExponential `splitBy` ["*", "/", "%"]
+    readExponential = readAnyNegated `splitBy` ["**"]
+
+    readAnyNegated = readNegated <|> readAnySigned
+    readNegated = do
+        id <- getNextId
+        op <- oneOf "!~"
+        spacing
+        x <- readAnyNegated
+        return $ TA_Unary id [op] x
+
+    readAnySigned = readSigned <|> readAnycremented
+    readSigned = do
+        id <- getNextId
+        op <- choice (map readSignOp "+-")
+        spacing
+        x <- readAnycremented
+        return $ TA_Unary id [op] x
+     where
+        readSignOp c = try $ do
+            char c
+            notFollowedBy2 $ char c
+            spacing
+            return c
+
+    readAnycremented = readNormalOrPostfixIncremented <|> readPrefixIncremented
+    readPrefixIncremented = do
+        id <- getNextId
+        op <- try $ string "++" <|> string "--"
+        spacing
+        x <- readArithTerm
+        return $ TA_Unary id (op ++ "|") x
+
+    readNormalOrPostfixIncremented = do
+        x <- readArithTerm
+        spacing
+        do
+            id <- getNextId
+            op <- try $ string "++" <|> string "--"
+            spacing
+            return $ TA_Unary id ('|':op) x
+         <|>
+            return x
+
+
+
+prop_readCondition = isOk readCondition "[ \\( a = b \\) -a \\( c = d \\) ]"
+prop_readCondition2 = isOk readCondition "[[ (a = b) || (c = d) ]]"
+prop_readCondition3 = isOk readCondition "[[ $c = [[:alpha:].~-] ]]"
+prop_readCondition4 = isOk readCondition "[[ $c =~ *foo* ]]"
+prop_readCondition5 = isOk readCondition "[[ $c =~ f( ]] )* ]]"
+prop_readCondition5a= isOk readCondition "[[ $c =~ a(b) ]]"
+prop_readCondition5b= isOk readCondition "[[ $c =~ f( ($var ]]) )* ]]"
+prop_readCondition6 = isOk readCondition "[[ $c =~ ^[yY]$ ]]"
+prop_readCondition7 = isOk readCondition "[[ ${line} =~ ^[[:space:]]*# ]]"
+prop_readCondition8 = isOk readCondition "[[ $l =~ ogg|flac ]]"
+prop_readCondition9 = isOk readCondition "[ foo -a -f bar ]"
+prop_readCondition10= isOk readCondition "[[\na == b\n||\nc == d ]]"
+prop_readCondition10a= isOk readCondition "[[\na == b  ||\nc == d ]]"
+prop_readCondition10b= isOk readCondition "[[ a == b\n||\nc == d ]]"
+prop_readCondition11= isOk readCondition "[[ a == b ||\n c == d ]]"
+prop_readCondition12= isWarning readCondition "[ a == b \n -o c == d ]"
+prop_readCondition13= isOk readCondition "[[ foo =~ ^fo{1,3}$ ]]"
+prop_readCondition14= isOk readCondition "[ foo '>' bar ]"
+prop_readCondition15= isOk readCondition "[ foo \">=\" bar ]"
+prop_readCondition16= isOk readCondition "[ foo \\< bar ]"
+prop_readCondition17= isOk readCondition "[[ ${file::1} = [-.\\|/\\\\] ]]"
+prop_readCondition18= isOk readCondition "[ ]"
+prop_readCondition19= isOk readCondition "[ '(' x \")\" ]"
+prop_readCondition20= isOk readCondition "[[ echo_rc -eq 0 ]]"
+prop_readCondition21= isOk readCondition "[[ $1 =~ ^(a\\ b)$ ]]"
+prop_readCondition22= isOk readCondition "[[ $1 =~ \\.a\\.(\\.b\\.)\\.c\\. ]]"
+readCondition = called "test expression" $ do
+    opos <- getPosition
+    id <- getNextId
+    open <- try (string "[[") <|> string "["
+    let single = open == "["
+    let typ = if single then SingleBracket else DoubleBracket
+
+    pos <- getPosition
+    space <- allspacing
+    when (null space) $
+        parseProblemAtWithEnd opos pos ErrorC 1035 $ "You need a space after the " ++
+            if single
+                then "[ and before the ]."
+                else "[[ and before the ]]."
+    when (single && '\n' `elem` space) $
+        parseProblemAt pos ErrorC 1080 "You need \\ before line feeds to break lines in [ ]."
+
+    condition <- readConditionContents single <|> do
+        guard . not . null $ space
+        lookAhead $ string "]"
+        id <- getNextIdAt pos
+        return $ TC_Empty id typ
+
+    cpos <- getPosition
+    close <- try (string "]]") <|> string "]" <|> fail "Expected test to end here (don't wrap commands in []/[[]])"
+    when (open == "[[" && close /= "]]") $ parseProblemAt cpos ErrorC 1033 "Did you mean ]] ?"
+    when (open == "[" && close /= "]" ) $ parseProblemAt opos ErrorC 1034 "Did you mean [[ ?"
+    spacing
+    many readCmdWord -- Read and throw away remainders to get then/do warnings. Fixme?
+    return $ T_Condition id typ condition
+
+readAnnotationPrefix = do
+    char '#'
+    many linewhitespace
+    string "shellcheck"
+
+prop_readAnnotation1 = isOk readAnnotation "# shellcheck disable=1234,5678\n"
+prop_readAnnotation2 = isOk readAnnotation "# shellcheck disable=SC1234 disable=SC5678\n"
+prop_readAnnotation3 = isOk readAnnotation "# shellcheck disable=SC1234 source=/dev/null disable=SC5678\n"
+prop_readAnnotation4 = isWarning readAnnotation "# shellcheck cats=dogs disable=SC1234\n"
+prop_readAnnotation5 = isOk readAnnotation "# shellcheck disable=SC2002 # All cats are precious\n"
+prop_readAnnotation6 = isOk readAnnotation "# shellcheck disable=SC1234 # shellcheck foo=bar\n"
+readAnnotation = called "shellcheck directive" $ do
+    try readAnnotationPrefix
+    many1 linewhitespace
+    values <- many1 readKey
+    optional readAnyComment
+    void linefeed <|> do
+        parseNote ErrorC 1125 "Invalid key=value pair? Ignoring the rest of this directive starting here."
+        many (noneOf "\n")
+        void linefeed <|> eof
+    many linewhitespace
+    return $ concat values
+  where
+    readKey = do
+        keyPos <- getPosition
+        key <- many1 letter
+        char '=' <|> fail "Expected '=' after directive key"
+        annotations <- case key of
+            "disable" -> readCode `sepBy` char ','
+              where
+                readCode = do
+                    optional $ string "SC"
+                    int <- many1 digit
+                    return $ DisableComment (read int)
+
+            "source" -> do
+                filename <- many1 $ noneOf " \n"
+                return [SourceOverride filename]
+
+            "shell" -> do
+                pos <- getPosition
+                shell <- many1 $ noneOf " \n"
+                when (isNothing $ shellForExecutable shell) $
+                    parseNoteAt pos ErrorC 1103
+                        "This shell type is unknown. Use e.g. sh or bash."
+                return [ShellOverride shell]
+
+            _ -> do
+                parseNoteAt keyPos WarningC 1107 "This directive is unknown. It will be ignored."
+                anyChar `reluctantlyTill` whitespace
+                return []
+
+        many linewhitespace
+        return annotations
+
+readAnnotations = do
+    annotations <- many (readAnnotation `thenSkip` allspacing)
+    return $ concat annotations
+
+readComment = do
+    unexpecting "shellcheck annotation" readAnnotationPrefix
+    readAnyComment
+
+readAnyComment = do
+    char '#'
+    many $ noneOf "\r\n"
+
+prop_readNormalWord = isOk readNormalWord "'foo'\"bar\"{1..3}baz$(lol)"
+prop_readNormalWord2 = isOk readNormalWord "foo**(foo)!!!(@@(bar))"
+prop_readNormalWord3 = isOk readNormalWord "foo#"
+prop_readNormalWord4 = isOk readNormalWord "$\"foo\"$'foo\nbar'"
+prop_readNormalWord5 = isWarning readNormalWord "${foo}}"
+prop_readNormalWord6 = isOk readNormalWord "foo/{}"
+prop_readNormalWord7 = isOk readNormalWord "foo\\\nbar"
+prop_readNormalWord8 = isWarning readSubshell "(foo\\ \nbar)"
+prop_readNormalWord9 = isOk readSubshell "(foo\\ ;\nbar)"
+prop_readNormalWord10 = isWarning readNormalWord "\x201Chello\x201D"
+prop_readNormalWord11 = isWarning readNormalWord "\x2018hello\x2019"
+prop_readNormalWord12 = isWarning readNormalWord "hello\x2018"
+readNormalWord = readNormalishWord ""
+
+readNormalishWord end = do
+    id <- getNextId
+    pos <- getPosition
+    x <- many1 (readNormalWordPart end)
+    checkPossibleTermination pos x
+    return $ T_NormalWord id x
+
+readIndexSpan = do
+    id <- getNextId
+    x <- many (readNormalWordPart "]" <|> someSpace <|> otherLiteral)
+    return $ T_NormalWord id x
+  where
+    someSpace = do
+        id <- getNextId
+        str <- spacing1
+        return $ T_Literal id str
+    otherLiteral = do
+        id <- getNextId
+        str <- many1 $ oneOf quotableChars
+        return $ T_Literal id str
+
+checkPossibleTermination pos [T_Literal _ x] =
+    when (x `elem` ["do", "done", "then", "fi", "esac"]) $
+        parseProblemAt pos WarningC 1010 $ "Use semicolon or linefeed before '" ++ x ++ "' (or quote to make it literal)."
+checkPossibleTermination _ _ = return ()
+
+readNormalWordPart end = do
+    notFollowedBy2 $ oneOf end
+    checkForParenthesis
+    choice [
+        readSingleQuoted,
+        readDoubleQuoted,
+        readGlob,
+        readNormalDollar,
+        readBraced,
+        readUnquotedBackTicked,
+        readProcSub,
+        readUnicodeQuote,
+        readNormalLiteral end,
+        readLiteralCurlyBraces
+      ]
+  where
+    checkForParenthesis =
+        return () `attempting` do
+            pos <- getPosition
+            lookAhead $ char '('
+            parseProblemAt pos ErrorC 1036 "'(' is invalid here. Did you forget to escape it?"
+
+    readLiteralCurlyBraces = do
+        id <- getNextId
+        str <- findParam <|> literalBraces
+        return $ T_Literal id str
+
+    findParam = try $ string "{}"
+    literalBraces = do
+        pos <- getPosition
+        c <- oneOf "{}"
+        parseProblemAt pos WarningC 1083 $
+            "This " ++ [c] ++ " is literal. Check expression (missing ;/\\n?) or quote it."
+        return [c]
+
+
+readSpacePart = do
+    id <- getNextId
+    x <- many1 whitespace
+    return $ T_Literal id x
+
+readDollarBracedWord = do
+    id <- getNextId
+    list <- many readDollarBracedPart
+    return $ T_NormalWord id list
+
+readDollarBracedPart = readSingleQuoted <|> readDoubleQuoted <|>
+                       readParamSubSpecialChar <|> readExtglob <|> readNormalDollar <|>
+                       readUnquotedBackTicked <|> readDollarBracedLiteral
+
+readDollarBracedLiteral = do
+    id <- getNextId
+    vars <- (readBraceEscaped <|> (anyChar >>= \x -> return [x])) `reluctantlyTill1` bracedQuotable
+    return $ T_Literal id $ concat vars
+
+readParamSubSpecialChar = do
+    id <- getNextId
+    T_ParamSubSpecialChar id <$> many1 paramSubSpecialChars
+
+prop_readProcSub1 = isOk readProcSub "<(echo test | wc -l)"
+prop_readProcSub2 = isOk readProcSub "<(  if true; then true; fi )"
+prop_readProcSub3 = isOk readProcSub "<( # nothing here \n)"
+readProcSub = called "process substitution" $ do
+    id <- getNextId
+    dir <- try $ do
+                    x <- oneOf "<>"
+                    char '('
+                    return [x]
+    list <- readCompoundListOrEmpty
+    allspacing
+    char ')'
+    return $ T_ProcSub id dir list
+
+prop_readSingleQuoted = isOk readSingleQuoted "'foo bar'"
+prop_readSingleQuoted2 = isWarning readSingleQuoted "'foo bar\\'"
+prop_readSingleQuoted4 = isWarning readNormalWord "'it's"
+prop_readSingleQuoted5 = isWarning readSimpleCommand "foo='bar\ncow 'arg"
+prop_readSingleQuoted6 = isOk readSimpleCommand "foo='bar cow 'arg"
+prop_readSingleQuoted7 = isOk readSingleQuoted "'foo\x201C\&bar'"
+prop_readSingleQuoted8 = isWarning readSingleQuoted "'foo\x2018\&bar'"
+readSingleQuoted = called "single quoted string" $ do
+    id <- getNextId
+    startPos <- getPosition
+    singleQuote
+    s <- many readSingleQuotedPart
+    let string = concat s
+    endPos <- getPosition
+    singleQuote <|> fail "Expected end of single quoted string"
+
+    optional $ do
+        c <- try . lookAhead $ suspectCharAfterQuotes <|> oneOf "'"
+        if not (null string) && isAlpha c && isAlpha (last string)
+          then
+            parseProblemAt endPos WarningC 1011
+                "This apostrophe terminated the single quoted string!"
+          else
+            when ('\n' `elem` string && not ("\n" `isPrefixOf` string)) $
+                suggestForgotClosingQuote startPos endPos "single quoted string"
+
+    return (T_SingleQuoted id string)
+
+readSingleQuotedLiteral = do
+    singleQuote
+    strs <- many1 readSingleQuotedPart
+    singleQuote
+    return $ concat strs
+
+readSingleQuotedPart =
+    readSingleEscaped
+    <|> many1 (noneOf $ "'\\" ++ unicodeSingleQuotes)
+    <|> readUnicodeQuote
+   where
+    readUnicodeQuote = do
+        pos <- getPosition
+        x <- oneOf unicodeSingleQuotes
+        parseProblemAt pos WarningC 1112
+            "This is a unicode quote. Delete and retype it (or ignore/doublequote for literal)."
+        return [x]
+
+
+prop_readBackTicked = isOk (readBackTicked False) "`ls *.mp3`"
+prop_readBackTicked2 = isOk (readBackTicked False) "`grep \"\\\"\"`"
+prop_readBackTicked3 = isWarning (readBackTicked False) "´grep \"\\\"\"´"
+prop_readBackTicked4 = isOk readSimpleCommand "`echo foo\necho bar`"
+prop_readBackTicked5 = isOk readSimpleCommand "echo `foo`bar"
+prop_readBackTicked6 = isWarning readSimpleCommand "echo `foo\necho `bar"
+prop_readBackTicked7 = isOk readSimpleCommand "`#inline comment`"
+prop_readBackTicked8 = isOk readSimpleCommand "echo `#comment` \\\nbar baz"
+readQuotedBackTicked = readBackTicked True
+readUnquotedBackTicked = readBackTicked False
+readBackTicked quoted = called "backtick expansion" $ do
+    id <- getNextId
+    startPos <- getPosition
+    backtick
+    subStart <- getPosition
+    subString <- readGenericLiteral "`´"
+    endPos <- getPosition
+    backtick
+
+    optional $ do
+        c <- try . lookAhead $ suspectCharAfterQuotes
+        when ('\n' `elem` subString && not ("\n" `isPrefixOf` subString)) $
+            suggestForgotClosingQuote startPos endPos "backtick expansion"
+
+    -- Result positions may be off due to escapes
+    result <- subParse subStart subParser (unEscape subString)
+    return $ T_Backticked id result
+  where
+    unEscape [] = []
+    unEscape ('\\':'"':rest) | quoted = '"' : unEscape rest
+    unEscape ('\\':x:rest) | x `elem` "$`\\" = x : unEscape rest
+    unEscape ('\\':'\n':rest) = unEscape rest
+    unEscape (c:rest) = c : unEscape rest
+    subParser = do
+        cmds <- readCompoundListOrEmpty
+        verifyEof
+        return cmds
+    backtick =
+      void (char '`') <|> do
+         pos <- getPosition
+         char '´'
+         parseProblemAt pos ErrorC 1077
+            "For command expansion, the tick should slant left (` vs ´). Use $(..) instead."
+
+-- Run a parser on a new input, such as for `..` or here documents.
+subParse pos parser input = do
+    lastPosition <- getPosition
+    lastInput <- getInput
+    setPosition pos
+    setInput input
+    result <- parser
+    setInput lastInput
+    setPosition lastPosition
+    return result
+
+-- Parse something, but forget all parseProblems
+inSeparateContext = parseForgettingContext True
+-- Parse something, but forget all parseProblems on failure
+forgetOnFailure = parseForgettingContext False
+
+parseForgettingContext alsoOnSuccess parser = do
+    context <- Ms.get
+    success context <|> failure context
+  where
+    success c = do
+        res <- try parser
+        when alsoOnSuccess $ Ms.put c
+        return res
+    failure c = do
+        Ms.put c
+        fail ""
+
+prop_readDoubleQuoted = isOk readDoubleQuoted "\"Hello $FOO\""
+prop_readDoubleQuoted2 = isOk readDoubleQuoted "\"$'\""
+prop_readDoubleQuoted3 = isOk readDoubleQuoted "\"\x2018hello\x2019\""
+prop_readDoubleQuoted4 = isWarning readSimpleCommand "\"foo\nbar\"foo"
+prop_readDoubleQuoted5 = isOk readSimpleCommand "lol \"foo\nbar\" etc"
+prop_readDoubleQuoted6 = isOk readSimpleCommand "echo \"${ ls; }\""
+prop_readDoubleQuoted7 = isOk readSimpleCommand "echo \"${ ls;}bar\""
+prop_readDoubleQuoted8 = isWarning readDoubleQuoted "\"\x201Chello\x201D\""
+prop_readDoubleQuoted9 = isWarning readDoubleQuoted "\"foo\\n\""
+prop_readDoubleQuoted10 = isOk readDoubleQuoted "\"foo\\\\n\""
+readDoubleQuoted = called "double quoted string" $ do
+    id <- getNextId
+    startPos <- getPosition
+    doubleQuote
+    x <- many doubleQuotedPart
+    endPos <- getPosition
+    doubleQuote <|> fail "Expected end of double quoted string"
+    optional $ do
+        try . lookAhead $ suspectCharAfterQuotes <|> oneOf "$\""
+        when (any hasLineFeed x && not (startsWithLineFeed x)) $
+            suggestForgotClosingQuote startPos endPos "double quoted string"
+    return $ T_DoubleQuoted id x
+  where
+    startsWithLineFeed (T_Literal _ ('\n':_):_) = True
+    startsWithLineFeed _ = False
+    hasLineFeed (T_Literal _ str) | '\n' `elem` str = True
+    hasLineFeed _ = False
+
+suggestForgotClosingQuote startPos endPos name = do
+    parseProblemAt startPos WarningC 1078 $
+        "Did you forget to close this " ++ name ++ "?"
+    parseProblemAt endPos InfoC 1079
+        "This is actually an end quote, but due to next char it looks suspect."
+
+doubleQuotedPart = readDoubleLiteral <|> readDoubleQuotedDollar <|> readQuotedBackTicked <|> readUnicodeQuote
+  where
+    readUnicodeQuote = do
+        pos <- getPosition
+        id <- getNextId
+        c <- oneOf unicodeDoubleQuotes
+        parseProblemAt pos WarningC 1111
+            "This is a unicode quote. Delete and retype it (or ignore/singlequote for literal)."
+        return $ T_Literal id [c]
+
+readDoubleLiteral = do
+    id <- getNextId
+    s <- many1 readDoubleLiteralPart
+    return $ T_Literal id (concat s)
+
+readDoubleLiteralPart = do
+    x <- many1 (readDoubleEscaped <|> many1 (noneOf (doubleQuotableChars ++ unicodeDoubleQuotes)))
+    return $ concat x
+
+readNormalLiteral end = do
+    id <- getNextId
+    s <- many1 (readNormalLiteralPart end)
+    return $ T_Literal id (concat s)
+
+prop_readGlob1 = isOk readGlob "*"
+prop_readGlob2 = isOk readGlob "[^0-9]"
+prop_readGlob3 = isOk readGlob "[a[:alpha:]]"
+prop_readGlob4 = isOk readGlob "[[:alnum:]]"
+prop_readGlob5 = isOk readGlob "[^[:alpha:]1-9]"
+prop_readGlob6 = isOk readGlob "[\\|]"
+prop_readGlob7 = isOk readGlob "[^[]"
+prop_readGlob8 = isOk readGlob "[*?]"
+readGlob = readExtglob <|> readSimple <|> readClass <|> readGlobbyLiteral
+    where
+        readSimple = do
+            id <- getNextId
+            c <- oneOf "*?"
+            return $ T_Glob id [c]
+        -- Doesn't handle weird things like [^]a] and [$foo]. fixme?
+        readClass = try $ do
+            id <- getNextId
+            char '['
+            s <- many1 (predefined <|> readNormalLiteralPart "]" <|> globchars)
+            char ']'
+            return $ T_Glob id $ "[" ++ concat s ++ "]"
+          where
+           globchars = liftM return . oneOf $ "!$[" ++ extglobStartChars
+           predefined = do
+              try $ string "[:"
+              s <- many1 letter
+              string ":]"
+              return $ "[:" ++ s ++ ":]"
+
+        readGlobbyLiteral = do
+            id <- getNextId
+            c <- extglobStart <|> char '['
+            return $ T_Literal id [c]
+
+readNormalLiteralPart customEnd =
+    readNormalEscaped <|>
+        many1 (noneOf (customEnd ++ standardEnd))
+  where
+    standardEnd = "[{}"
+        ++ quotableChars
+        ++ extglobStartChars
+        ++ unicodeDoubleQuotes
+        ++ unicodeSingleQuotes
+
+readNormalEscaped = called "escaped char" $ do
+    pos <- getPosition
+    backslash
+    do
+        next <- quotable <|> oneOf "?*@!+[]{}.,~#"
+        when (next == ' ') $ checkTrailingSpaces pos <|> return ()
+        return $ if next == '\n' then "" else [next]
+      <|>
+        do
+            next <- anyChar
+            case escapedChar next of
+                Just name -> parseNoteAt pos WarningC 1012 $ "\\" ++ [next] ++ " is just literal '" ++ [next] ++ "' here. For " ++ name ++ ", use " ++ alternative next ++ " instead."
+                Nothing -> parseNoteAt pos InfoC 1001 $ "This \\" ++ [next] ++ " will be a regular '" ++ [next] ++ "' in this context."
+            return [next]
+  where
+    alternative 'n' = "a quoted, literal line feed"
+    alternative t = "\"$(printf \"\\" ++ [t] ++ "\")\""
+    escapedChar 'n' = Just "line feed"
+    escapedChar 't' = Just "tab"
+    escapedChar 'r' = Just "carriage return"
+    escapedChar _ = Nothing
+
+    checkTrailingSpaces pos = lookAhead . try $ do
+        many linewhitespace
+        void linefeed <|> eof
+        parseProblemAt pos ErrorC 1101 "Delete trailing spaces after \\ to break line (or use quotes for literal space)."
+
+
+prop_readExtglob1 = isOk readExtglob "!(*.mp3)"
+prop_readExtglob2 = isOk readExtglob "!(*.mp3|*.wmv)"
+prop_readExtglob4 = isOk readExtglob "+(foo \\) bar)"
+prop_readExtglob5 = isOk readExtglob "+(!(foo *(bar)))"
+prop_readExtglob6 = isOk readExtglob "*(((||))|())"
+prop_readExtglob7 = isOk readExtglob "*(<>)"
+prop_readExtglob8 = isOk readExtglob "@(|*())"
+readExtglob = called "extglob" $ do
+    id <- getNextId
+    c <- try $ do
+            f <- extglobStart
+            char '('
+            return f
+    contents <- readExtglobPart `sepBy` char '|'
+    char ')'
+    return $ T_Extglob id [c] contents
+
+readExtglobPart = do
+    id <- getNextId
+    x <- many (readExtglobGroup <|> readNormalWordPart "" <|> readSpacePart <|> readExtglobLiteral)
+    return $ T_NormalWord id x
+  where
+    readExtglobGroup = do
+        id <- getNextId
+        char '('
+        contents <- readExtglobPart `sepBy` char '|'
+        char ')'
+        return $ T_Extglob id "" contents
+    readExtglobLiteral = do
+        id <- getNextId
+        str <- many1 (oneOf "<>#;&")
+        return $ T_Literal id str
+
+
+readSingleEscaped = do
+    pos <- getPosition
+    s <- backslash
+    x <- lookAhead anyChar
+
+    case x of
+        '\'' -> parseProblemAt pos InfoC 1003 "Want to escape a single quote? echo 'This is how it'\\''s done'.";
+        '\n' -> parseProblemAt pos InfoC 1004 "This backslash+linefeed is literal. Break outside single quotes if you just want to break the line."
+        _ -> return ()
+
+    return [s]
+
+readDoubleEscaped = do
+    pos <- getPosition
+    bs <- backslash
+    (linefeed >> return "")
+        <|> liftM return doubleQuotable
+        <|> do
+            c <- anyChar
+            parseNoteAt pos StyleC 1117 $
+                "Backslash is literal in \"\\" ++ [c] ++ "\". Prefer explicit escaping: \"\\\\" ++ [c] ++ "\"."
+            return [bs, c]
+
+readBraceEscaped = do
+    bs <- backslash
+    (linefeed >> return "")
+        <|> liftM return bracedQuotable
+        <|> liftM (\ x -> [bs, x]) anyChar
+
+
+readGenericLiteral endChars = do
+    strings <- many (readGenericEscaped <|> many1 (noneOf ('\\':endChars)))
+    return $ concat strings
+
+readGenericLiteral1 endExp = do
+    strings <- (readGenericEscaped <|> (anyChar >>= \x -> return [x])) `reluctantlyTill1` endExp
+    return $ concat strings
+
+readGenericEscaped = do
+    backslash
+    x <- anyChar
+    return $ if x == '\n' then [] else ['\\', x]
+
+prop_readBraced = isOk readBraced "{1..4}"
+prop_readBraced2 = isOk readBraced "{foo,bar,\"baz lol\"}"
+prop_readBraced3 = isOk readBraced "{1,\\},2}"
+prop_readBraced4 = isOk readBraced "{1,{2,3}}"
+prop_readBraced5 = isOk readBraced "{JP{,E}G,jp{,e}g}"
+prop_readBraced6 = isOk readBraced "{foo,bar,$((${var}))}"
+prop_readBraced7 = isNotOk readBraced "{}"
+prop_readBraced8 = isNotOk readBraced "{foo}"
+readBraced = try braceExpansion
+  where
+    braceExpansion =
+        T_BraceExpansion `withParser` do
+            char '{'
+            elements <- bracedElement `sepBy1` char ','
+            guard $
+                case elements of
+                    (_:_:_) -> True
+                    [t] -> ".." `isInfixOf` onlyLiteralString t
+                    [] -> False
+            char '}'
+            return elements
+    bracedElement =
+        T_NormalWord `withParser` do
+            many $ choice [
+                braceExpansion,
+                readDollarExpression,
+                readSingleQuoted,
+                readDoubleQuoted,
+                braceLiteral
+                ]
+    braceLiteral =
+        T_Literal `withParser` readGenericLiteral1 (oneOf "{}\"$'," <|> whitespace)
+
+ensureDollar =
+    -- The grammar should have been designed along the lines of readDollarExpr = char '$' >> stuff, but
+    -- instead, each subunit parses its own $. This results in ~7 1-3 char lookaheads instead of one 1-char.
+    -- Instead of optimizing the grammar, here's a green cut that decreases shellcheck runtime by 10%:
+    lookAhead $ char '$'
+
+readNormalDollar = do
+    ensureDollar
+    readDollarExp <|> readDollarDoubleQuote <|> readDollarSingleQuote <|> readDollarLonely
+readDoubleQuotedDollar = do
+    ensureDollar
+    readDollarExp <|> readDollarLonely
+
+
+prop_readDollarExpression1 = isOk readDollarExpression "$(((1) && 3))"
+prop_readDollarExpression2 = isWarning readDollarExpression "$(((1)) && 3)"
+prop_readDollarExpression3 = isWarning readDollarExpression "$((\"$@\" &); foo;)"
+readDollarExpression :: Monad m => SCParser m Token
+readDollarExpression = do
+    ensureDollar
+    readDollarExp
+
+readDollarExp = arithmetic <|> readDollarExpansion <|> readDollarBracket <|> readDollarBraceCommandExpansion <|> readDollarBraced <|> readDollarVariable
+  where
+    arithmetic = readAmbiguous "$((" readDollarArithmetic readDollarExpansion (\pos ->
+        parseNoteAt pos WarningC 1102 "Shells disambiguate $(( differently or not at all. For $(command substition), add space after $( . For $((arithmetics)), fix parsing errors.")
+
+prop_readDollarSingleQuote = isOk readDollarSingleQuote "$'foo\\\'lol'"
+readDollarSingleQuote = called "$'..' expression" $ do
+    id <- getNextId
+    try $ string "$'"
+    str <- readGenericLiteral "'"
+    char '\''
+    return $ T_DollarSingleQuoted id str
+
+prop_readDollarDoubleQuote = isOk readDollarDoubleQuote "$\"hello\""
+readDollarDoubleQuote = do
+    lookAhead . try $ string "$\""
+    id <- getNextId
+    char '$'
+    doubleQuote
+    x <- many doubleQuotedPart
+    doubleQuote <|> fail "Expected end of translated double quoted string"
+    return $ T_DollarDoubleQuoted id x
+
+prop_readDollarArithmetic = isOk readDollarArithmetic "$(( 3 * 4 +5))"
+prop_readDollarArithmetic2 = isOk readDollarArithmetic "$(((3*4)+(1*2+(3-1))))"
+readDollarArithmetic = called "$((..)) expression" $ do
+    id <- getNextId
+    try (string "$((")
+    c <- readArithmeticContents
+    pos <- getPosition
+    char ')'
+    char ')' <|> fail "Expected a double )) to end the $((..))"
+    return (T_DollarArithmetic id c)
+
+readDollarBracket = called "$[..] expression" $ do
+    id <- getNextId
+    try (string "$[")
+    c <- readArithmeticContents
+    string "]"
+    return (T_DollarBracket id c)
+
+prop_readArithmeticExpression = isOk readArithmeticExpression "((a?b:c))"
+readArithmeticExpression = called "((..)) command" $ do
+    id <- getNextId
+    try (string "((")
+    c <- readArithmeticContents
+    string "))"
+    return (T_Arithmetic id c)
+
+-- If the next characters match prefix, try two different parsers and warn if the alternate parser had to be used
+readAmbiguous :: Monad m => String -> SCParser m p -> SCParser m p -> (SourcePos -> SCParser m ()) -> SCParser m p
+readAmbiguous prefix expected alternative warner = do
+    pos <- getPosition
+    try . lookAhead $ string prefix
+    -- If the expected parser fails, try the alt.
+    -- If the alt fails, run the expected one again for the errors.
+    try expected <|> try (withAlt pos) <|> expected
+  where
+    withAlt pos = do
+        t <- forgetOnFailure alternative
+        warner pos
+        return t
+
+prop_readDollarBraceCommandExpansion1 = isOk readDollarBraceCommandExpansion "${ ls; }"
+prop_readDollarBraceCommandExpansion2 = isOk readDollarBraceCommandExpansion "${\nls\n}"
+readDollarBraceCommandExpansion = called "ksh ${ ..; } command expansion" $ do
+    id <- getNextId
+    try $ do
+        string "${"
+        whitespace
+    allspacing
+    term <- readTerm
+    char '}' <|> fail "Expected } to end the ksh ${ ..; } command expansion"
+    return $ T_DollarBraceCommandExpansion id term
+
+prop_readDollarBraced1 = isOk readDollarBraced "${foo//bar/baz}"
+prop_readDollarBraced2 = isOk readDollarBraced "${foo/'{cow}'}"
+prop_readDollarBraced3 = isOk readDollarBraced "${foo%%$(echo cow\\})}"
+prop_readDollarBraced4 = isOk readDollarBraced "${foo#\\}}"
+readDollarBraced = called "parameter expansion" $ do
+    id <- getNextId
+    try (string "${")
+    word <- readDollarBracedWord
+    char '}'
+    return $ T_DollarBraced id word
+
+prop_readDollarExpansion1= isOk readDollarExpansion "$(echo foo; ls\n)"
+prop_readDollarExpansion2= isOk readDollarExpansion "$(  )"
+prop_readDollarExpansion3= isOk readDollarExpansion "$( command \n#comment \n)"
+readDollarExpansion = called "command expansion" $ do
+    id <- getNextId
+    try (string "$(")
+    cmds <- readCompoundListOrEmpty
+    char ')' <|> fail "Expected end of $(..) expression"
+    return $ T_DollarExpansion id cmds
+
+prop_readDollarVariable = isOk readDollarVariable "$@"
+prop_readDollarVariable2 = isOk (readDollarVariable >> anyChar) "$?!"
+prop_readDollarVariable3 = isWarning (readDollarVariable >> anyChar) "$10"
+prop_readDollarVariable4 = isWarning (readDollarVariable >> string "[@]") "$arr[@]"
+prop_readDollarVariable5 = isWarning (readDollarVariable >> string "[f") "$arr[f"
+
+readDollarVariable = do
+    id <- getNextId
+    pos <- getPosition
+
+    let singleCharred p = do
+        n <- p
+        value <- wrap [n]
+        return (T_DollarBraced id value)
+
+    let positional = do
+        value <- singleCharred digit
+        return value `attempting` do
+            lookAhead digit
+            parseNoteAt pos ErrorC 1037 "Braces are required for positionals over 9, e.g. ${10}."
+
+    let special = singleCharred specialVariable
+
+    let regular = do
+        name <- readVariableName
+        value <- wrap name
+        return (T_DollarBraced id value) `attempting` do
+            lookAhead $ char '['
+            parseNoteAt pos ErrorC 1087 "Use braces when expanding arrays, e.g. ${array[idx]} (or ${var}[.. to quiet)."
+
+    try $ char '$' >> (positional <|> special <|> regular)
+
+  where
+    wrap s = do
+        x <- getNextId
+        y <- getNextId
+        return $ T_NormalWord x [T_Literal y s]
+
+readVariableName = do
+    f <- variableStart
+    rest <- many variableChars
+    return (f:rest)
+
+readDollarLonely = do
+    id <- getNextId
+    pos <- getPosition
+    char '$'
+    n <- lookAhead (anyChar <|> (eof >> return '_'))
+    return $ T_Literal id "$"
+
+prop_readHereDoc = isOk readScript "cat << foo\nlol\ncow\nfoo"
+prop_readHereDoc2 = isNotOk readScript "cat <<- EOF\n  cow\n  EOF"
+prop_readHereDoc3 = isOk readScript "cat << foo\n$\"\nfoo"
+prop_readHereDoc4 = isNotOk readScript "cat << foo\n`\nfoo"
+prop_readHereDoc5 = isOk readScript "cat <<- !foo\nbar\n!foo"
+prop_readHereDoc6 = isOk readScript "cat << foo\\ bar\ncow\nfoo bar"
+prop_readHereDoc7 = isOk readScript "cat << foo\n\\$(f ())\nfoo"
+prop_readHereDoc8 = isOk readScript "cat <<foo>>bar\netc\nfoo"
+prop_readHereDoc9 = isOk readScript "if true; then cat << foo; fi\nbar\nfoo\n"
+prop_readHereDoc10= isOk readScript "if true; then cat << foo << bar; fi\nfoo\nbar\n"
+prop_readHereDoc11= isOk readScript "cat << foo $(\nfoo\n)lol\nfoo\n"
+prop_readHereDoc12= isOk readScript "cat << foo|cat\nbar\nfoo"
+prop_readHereDoc13= isOk readScript "cat <<'#!'\nHello World\n#!\necho Done"
+prop_readHereDoc14= isWarning readScript "cat << foo\nbar\nfoo \n"
+prop_readHereDoc15= isWarning readScript "cat <<foo\nbar\nfoo bar\nfoo"
+prop_readHereDoc16= isOk readScript "cat <<- ' foo'\nbar\n foo\n"
+prop_readHereDoc17= isWarning readScript "cat <<- ' foo'\nbar\n  foo\n foo\n"
+prop_readHereDoc18= isWarning readScript "cat << foo\nLoose \\t\nfoo"
+prop_readHereDoc19= isOk readScript "# shellcheck disable=SC1117\ncat << foo\nLoose \\t\nfoo"
+prop_readHereDoc20= isWarning readScript "cat << foo\n  foo\n()\nfoo\n"
+prop_readHereDoc21= isOk readScript "# shellcheck disable=SC1039\ncat << foo\n  foo\n()\nfoo\n"
+readHereDoc = called "here document" $ do
+    fid <- getNextId
+    pos <- getPosition
+    try $ string "<<"
+    dashed <- (char '-' >> return Dashed) <|> return Undashed
+    sp <- spacing
+    optional $ do
+        try . lookAhead $ char '('
+        let message = "Shells are space sensitive. Use '< <(cmd)', not '<<" ++ sp ++ "(cmd)'."
+        parseProblemAt pos ErrorC 1038 message
+    hid <- getNextId
+    (quoted, endToken) <- readToken
+
+    -- add empty tokens for now, read the rest in readPendingHereDocs
+    let doc = T_HereDoc hid dashed quoted endToken []
+    addPendingHereDoc doc
+    return doc
+  where
+    quotes = "\"'\\"
+    -- Fun fact: bash considers << foo"" quoted, but not << <("foo").
+    -- Instead of replicating this, just read a token and strip quotes.
+    readToken = do
+        str <- readStringForParser readNormalWord
+        return (if any (`elem` quotes) str then Quoted else Unquoted,
+                filter (not . (`elem` quotes)) str)
+
+
+readPendingHereDocs = do
+    docs <- popPendingHereDocs
+    mapM_ readDoc docs
+  where
+    readDoc (HereDocPending (T_HereDoc id dashed quoted endToken _) ctx) =
+      swapContext ctx $
+      do
+        docPos <- getPosition
+        tokenPos <- Map.findWithDefault (error "Missing ID") id <$> getMap
+        (terminated, wasWarned, lines) <- readDocLines dashed endToken
+        let hereData = unlines lines
+        unless terminated $ do
+            unless wasWarned $
+                debugHereDoc tokenPos endToken hereData
+            fail "Here document was not correctly terminated"
+        list <- parseHereData quoted docPos hereData
+        addToHereDocMap id list
+
+    -- Read the lines making up the here doc. Returns (IsTerminated, Lines)
+    readDocLines :: Monad m => Dashed -> String -> SCParser m (Bool, Bool, [String])
+    readDocLines dashed endToken = do
+        pos <- getPosition
+        str <- rawLine
+        isEof <- option False (eof >> return True)
+        (isEnd, wasWarned) <- subParse pos checkEnd str
+        if
+            | isEnd -> return (True, wasWarned, [])
+            | isEof -> return (False, wasWarned, [str])
+            | True -> do
+                (ok, previousWarning, rest) <- readDocLines dashed endToken
+                return (ok, wasWarned || previousWarning, str:rest)
+      where
+        -- Check if this is the actual end, or a plausible false end
+        checkEnd = option (False, False) $ try $ do
+            -- Match what's basically '^( *)token( *)(.*)$'
+            leadingSpacePos <- getPosition
+            leadingSpace <- linewhitespace `reluctantlyTill` string endToken
+            string endToken
+            trailingSpacePos <- getPosition
+            trailingSpace <- many linewhitespace
+            trailerPos <- getPosition
+            trailer <- many anyChar
+
+            let leadingSpacesAreTabs = all (== '\t') leadingSpace
+            let thereIsNoTrailer = null trailingSpace && null trailer
+            let leaderIsOk = null leadingSpace
+                    || dashed == Dashed && leadingSpacesAreTabs
+            let trailerStart = if null trailer then '\0' else head trailer
+            let hasTrailingSpace = not $ null trailingSpace
+            let hasTrailer = not $ null trailer
+            let ppt = parseProblemAt trailerPos ErrorC
+
+            if leaderIsOk && thereIsNoTrailer
+              then return (True, False)
+              else do
+                let foundCause = return (False, True)
+                let skipLine = return (False, False)
+                -- This may be intended as an end token. Debug why it isn't.
+                if
+                    | trailerStart == ')' -> do
+                        ppt 1119 $ "Add a linefeed between end token and terminating ')'."
+                        foundCause
+                    | trailerStart == '#' -> do
+                        ppt 1120 "No comments allowed after here-doc token. Comment the next line instead."
+                        foundCause
+                    | trailerStart `elem` ";>|&" -> do
+                        ppt 1121 "Add ;/& terminators (and other syntax) on the line with the <<, not here."
+                        foundCause
+                    | hasTrailingSpace && hasTrailer -> do
+                        ppt 1122 "Nothing allowed after end token. To continue a command, put it on the line with the <<."
+                        foundCause
+                    | leaderIsOk && hasTrailingSpace && not hasTrailer -> do
+                        parseProblemAt trailingSpacePos ErrorC 1118 "Delete whitespace after the here-doc end token."
+                        -- Parse as if it's the actual end token. Will koala_man regret this once again?
+                        return (True, True)
+                    | not hasTrailingSpace && hasTrailer ->
+                        -- The end token is just a prefix
+                        skipLine
+                    | hasTrailer ->
+                        error "ShellCheck bug, please report (here doc trailer)."
+
+                    -- The following cases assume no trailing text:
+                    | dashed == Undashed && (not $ null leadingSpace) -> do
+                        parseProblemAt leadingSpacePos ErrorC 1039 "Remove indentation before end token (or use <<- and indent with tabs)."
+                        foundCause
+                    | dashed == Dashed && not leadingSpacesAreTabs -> do
+                        parseProblemAt leadingSpacePos ErrorC 1040 "When using <<-, you can only indent with tabs."
+                        foundCause
+                    | True -> skipLine
+
+    rawLine = do
+        c <- many $ noneOf "\n"
+        void (char '\n') <|> eof
+        return c
+
+    parseHereData Quoted startPos hereData = do
+        id <- getNextIdAt startPos
+        return [T_Literal id hereData]
+
+    parseHereData Unquoted startPos hereData =
+        subParse startPos readHereData hereData
+
+    readHereData = many $ doubleQuotedPart <|> readHereLiteral
+
+    readHereLiteral = do
+        id <- getNextId
+        chars <- many1 $ noneOf "`$\\"
+        return $ T_Literal id chars
+
+    debugHereDoc pos endToken doc
+        | endToken `isInfixOf` doc =
+            let lookAt line = when (endToken `isInfixOf` line) $
+                      parseProblemAt pos ErrorC 1042 ("Close matches include '" ++ line ++ "' (!= '" ++ endToken ++ "').")
+            in do
+                  parseProblemAt pos ErrorC 1041 ("Found '" ++ endToken ++ "' further down, but not on a separate line.")
+                  mapM_ lookAt (lines doc)
+        | map toLower endToken `isInfixOf` map toLower doc =
+            parseProblemAt pos ErrorC 1043 ("Found " ++ endToken ++ " further down, but with wrong casing.")
+        | otherwise =
+            parseProblemAt pos ErrorC 1044 ("Couldn't find end token `" ++ endToken ++ "' in the here document.")
+
+
+readFilename = readNormalWord
+readIoFileOp = choice [g_DGREAT, g_LESSGREAT, g_GREATAND, g_LESSAND, g_CLOBBER, redirToken '<' T_Less, redirToken '>' T_Greater ]
+
+readIoDuplicate = try $ do
+    id <- getNextId
+    op <- g_GREATAND <|> g_LESSAND
+    target <- readIoVariable <|> digitsAndOrDash
+    return $ T_IoDuplicate id op target
+  where
+    -- either digits with optional dash, or a required dash
+    digitsAndOrDash = do
+        str <- many digit
+        dash <- (if null str then id else option "") $ string "-"
+        return $ str ++ dash
+
+
+prop_readIoFile = isOk readIoFile ">> \"$(date +%YYmmDD)\""
+readIoFile = called "redirection" $ do
+    id <- getNextId
+    op <- readIoFileOp
+    spacing
+    file <- readFilename
+    return $ T_IoFile id op file
+
+readIoVariable = try $ do
+    char '{'
+    x <- readVariableName
+    char '}'
+    return $ "{" ++ x ++ "}"
+
+readIoSource = try $ do
+    x <- string "&" <|> readIoVariable <|> many digit
+    lookAhead $ void readIoFileOp <|> void (string "<<")
+    return x
+
+prop_readIoRedirect = isOk readIoRedirect "3>&2"
+prop_readIoRedirect2 = isOk readIoRedirect "2> lol"
+prop_readIoRedirect3 = isOk readIoRedirect "4>&-"
+prop_readIoRedirect4 = isOk readIoRedirect "&> lol"
+prop_readIoRedirect5 = isOk readIoRedirect "{foo}>&2"
+prop_readIoRedirect6 = isOk readIoRedirect "{foo}<&-"
+prop_readIoRedirect7 = isOk readIoRedirect "{foo}>&1-"
+readIoRedirect = do
+    id <- getNextId
+    n <- readIoSource
+    redir <- readHereString <|> readHereDoc <|> readIoDuplicate <|> readIoFile
+    skipAnnotationAndWarn
+    spacing
+    return $ T_FdRedirect id n redir
+
+readRedirectList = many1 readIoRedirect
+
+prop_readHereString = isOk readHereString "<<< \"Hello $world\""
+readHereString = called "here string" $ do
+    id <- getNextId
+    try $ string "<<<"
+    spacing
+    id2 <- getNextId
+    word <- readNormalWord
+    return $ T_HereString id2 word
+
+readNewlineList = many1 ((linefeed <|> carriageReturn) `thenSkip` spacing)
+readLineBreak = optional readNewlineList
+
+prop_readSeparator1 = isWarning readScript "a &; b"
+prop_readSeparator2 = isOk readScript "a & b"
+prop_readSeparator3 = isWarning readScript "a &amp; b"
+prop_readSeparator4 = isWarning readScript "a &gt; file; b"
+readSeparatorOp = do
+    notFollowedBy2 (void g_AND_IF <|> void readCaseSeparator)
+    notFollowedBy2 (string "&>")
+    f <- try (do
+                    pos <- getPosition
+                    char '&'
+                    optional $ do
+                        s <- lookAhead . choice . map (try . string) $
+                            ["amp;", "gt;", "lt;"]
+                        parseProblemAt pos ErrorC 1109 "This is an unquoted HTML entity. Replace with corresponding character."
+
+                    spacing
+                    pos <- getPosition
+                    char ';'
+                    -- In case statements we might have foo & ;;
+                    notFollowedBy2 $ char ';'
+                    parseProblemAt pos ErrorC 1045 "It's not 'foo &; bar', just 'foo & bar'."
+                    return '&'
+            ) <|> char ';' <|> char '&'
+    spacing
+    return f
+
+readSequentialSep = void (g_Semi >> readLineBreak) <|> void readNewlineList
+readSeparator =
+    do
+        separator <- readSeparatorOp
+        readLineBreak
+        return separator
+     <|>
+        do
+            readNewlineList
+            return '\n'
+
+makeSimpleCommand id1 id2 prefix cmd suffix =
+    let
+        (preAssigned, preRest) = partition assignment prefix
+        (preRedirected, preRest2) = partition redirection preRest
+        (postRedirected, postRest) = partition redirection suffix
+
+        redirs = preRedirected ++ postRedirected
+        assigns = preAssigned
+        args = cmd ++ preRest2 ++ postRest
+    in
+        T_Redirecting id1 redirs $ T_SimpleCommand id2 assigns args
+  where
+    assignment (T_Assignment {}) = True
+    assignment _ = False
+    redirection (T_FdRedirect {}) = True
+    redirection _ = False
+
+prop_readSimpleCommand = isOk readSimpleCommand "echo test > file"
+prop_readSimpleCommand2 = isOk readSimpleCommand "cmd &> file"
+prop_readSimpleCommand3 = isOk readSimpleCommand "export foo=(bar baz)"
+prop_readSimpleCommand4 = isOk readSimpleCommand "typeset -a foo=(lol)"
+prop_readSimpleCommand5 = isOk readSimpleCommand "time if true; then echo foo; fi"
+prop_readSimpleCommand6 = isOk readSimpleCommand "time -p ( ls -l; )"
+prop_readSimpleCommand7 = isOk readSimpleCommand "\\ls"
+prop_readSimpleCommand8 = isWarning readSimpleCommand "// Lol"
+prop_readSimpleCommand9 = isWarning readSimpleCommand "/* Lolbert */"
+prop_readSimpleCommand10 = isWarning readSimpleCommand "/**** Lolbert */"
+prop_readSimpleCommand11 = isOk readSimpleCommand "/\\* foo"
+prop_readSimpleCommand12 = isWarning readSimpleCommand "elsif foo"
+prop_readSimpleCommand13 = isWarning readSimpleCommand "ElseIf foo"
+prop_readSimpleCommand14 = isWarning readSimpleCommand "elseif[$i==2]"
+readSimpleCommand = called "simple command" $ do
+    id1 <- getNextId
+    id2 <- getNextId
+    prefix <- option [] readCmdPrefix
+    skipAnnotationAndWarn
+    pos <- getPosition
+    cmd <- option Nothing $ Just <$> readCmdName
+    when (null prefix && isNothing cmd) $ fail "Expected a command"
+
+    case cmd of
+      Nothing -> return $ makeSimpleCommand id1 id2 prefix [] []
+      Just cmd -> do
+            validateCommand pos cmd
+            suffix <- option [] $ getParser readCmdSuffix cmd [
+                        (["declare", "export", "local", "readonly", "typeset"], readModifierSuffix),
+                        (["time"], readTimeSuffix),
+                        (["let"], readLetSuffix),
+                        (["eval"], readEvalSuffix)
+                    ]
+
+            let result = makeSimpleCommand id1 id2 prefix [cmd] suffix
+            if isCommand ["source", "."] cmd
+                then readSource pos result
+                else return result
+  where
+    isCommand strings (T_NormalWord _ [T_Literal _ s]) = s `elem` strings
+    isCommand _ _ = False
+    getParser def cmd [] = def
+    getParser def cmd ((list, action):rest) =
+        if isCommand list cmd
+        then action
+        else getParser def cmd rest
+
+    cStyleComment cmd =
+        case cmd of
+            _ -> False
+
+    validateCommand pos cmd =
+        case cmd of
+            (T_NormalWord _ [T_Literal _ "//"]) -> commentWarning pos
+            (T_NormalWord _ (T_Literal _ "/" : T_Glob _ "*" :_)) -> commentWarning pos
+            (T_NormalWord _ (T_Literal _ str:_)) -> do
+                let cmd = map toLower $ takeWhile isAlpha str
+                when (cmd `elem` ["elsif", "elseif"]) $
+                    parseProblemAt pos ErrorC 1131 "Use 'elif' to start another branch."
+            _ -> return ()
+
+    commentWarning pos =
+        parseProblemAt pos ErrorC 1127 "Was this intended as a comment? Use # in sh."
+
+readSource :: Monad m => SourcePos -> Token -> SCParser m Token
+readSource pos t@(T_Redirecting _ _ (T_SimpleCommand _ _ (cmd:file:_))) = do
+    override <- getSourceOverride
+    let literalFile = do
+        name <- override `mplus` getLiteralString file
+        -- Hack to avoid 'source ~/foo' trying to read from literal tilde
+        guard . not $ "~/" `isPrefixOf` name
+        return name
+    case literalFile of
+        Nothing -> do
+            parseNoteAt pos WarningC 1090
+                "Can't follow non-constant source. Use a directive to specify location."
+            return t
+        Just filename -> do
+            proceed <- shouldFollow filename
+            if not proceed
+              then do
+                parseNoteAt pos InfoC 1093
+                    "This file appears to be recursively sourced. Ignoring."
+                return t
+              else do
+                sys <- Mr.asks systemInterface
+                input <-
+                    if filename == "/dev/null" -- always allow /dev/null
+                    then return (Right "")
+                    else system $ siReadFile sys filename
+                case input of
+                    Left err -> do
+                        parseNoteAt pos InfoC 1091 $
+                            "Not following: " ++ err
+                        return t
+                    Right script -> do
+                        id1 <- getNextIdAt pos
+                        id2 <- getNextIdAt pos
+
+                        let included = do
+                            src <- subRead filename script
+                            return $ T_SourceCommand id1 t (T_Include id2 src)
+
+                        let failed = do
+                            parseNoteAt pos WarningC 1094
+                                "Parsing of sourced file failed. Ignoring it."
+                            return t
+
+                        included <|> failed
+  where
+    subRead name script =
+        withContext (ContextSource name) $
+            inSeparateContext $
+                subParse (initialPos name) readScript script
+readSource _ t = return t
+
+
+prop_readPipeline = isOk readPipeline "! cat /etc/issue | grep -i ubuntu"
+prop_readPipeline2 = isWarning readPipeline "!cat /etc/issue | grep -i ubuntu"
+prop_readPipeline3 = isOk readPipeline "for f; do :; done|cat"
+readPipeline = do
+    unexpecting "keyword/token" readKeyword
+    do
+        (T_Bang id) <- g_Bang
+        pipe <- readPipeSequence
+        return $ T_Banged id pipe
+      <|>
+        readPipeSequence
+
+prop_readAndOr = isOk readAndOr "grep -i lol foo || exit 1"
+prop_readAndOr1 = isOk readAndOr "# shellcheck disable=1\nfoo"
+prop_readAndOr2 = isOk readAndOr "# shellcheck disable=1\n# lol\n# shellcheck disable=3\nfoo"
+readAndOr = do
+    aid <- getNextId
+    apos <- getPosition
+    annotations <- readAnnotations
+
+    unless (null annotations) $ optional $ do
+        try . lookAhead $ readKeyword
+        parseProblemAt apos ErrorC 1123 "ShellCheck directives are only valid in front of complete compound commands, like 'if', not e.g. individual 'elif' branches."
+
+    andOr <- withAnnotations annotations $
+        chainr1 readPipeline $ do
+            op <- g_AND_IF <|> g_OR_IF
+            readLineBreak
+            return $ case op of T_AND_IF id -> T_AndIf id
+                                T_OR_IF  id -> T_OrIf id
+
+    return $ if null annotations
+                then andOr
+                else T_Annotation aid annotations andOr
+
+readTermOrNone = do
+    allspacing
+    readTerm <|> do
+        eof
+        return []
+
+prop_readTerm = isOk readTerm "time ( foo; bar; )"
+readTerm = do
+    allspacing
+    m <- readAndOr
+    readTerm' m
+
+readTerm' current =
+    do
+        id <- getNextId
+        sep <- readSeparator
+        more <- option (T_EOF id) readAndOr
+        case more of (T_EOF _) -> return [transformWithSeparator id sep current]
+                     _         -> do
+                                list <- readTerm' more
+                                return (transformWithSeparator id sep current : list)
+      <|>
+        return [current]
+
+transformWithSeparator i '&' = T_Backgrounded i
+transformWithSeparator i _  = id
+
+
+readPipeSequence = do
+    id <- getNextId
+    (cmds, pipes) <- sepBy1WithSeparators readCommand
+                        (readPipe `thenSkip` (spacing >> readLineBreak))
+    spacing
+    return $ T_Pipeline id pipes cmds
+  where
+    sepBy1WithSeparators p s = do
+        let elems = p >>= \x -> return ([x], [])
+        let seps = do
+            separator <- s
+            return $ \(a,b) (c,d) -> (a++c, b ++ d ++ [separator])
+        elems `chainl1` seps
+
+readPipe = do
+    notFollowedBy2 g_OR_IF
+    id <- getNextId
+    char '|'
+    qualifier <- string "&" <|> return ""
+    spacing
+    return $ T_Pipe id ('|':qualifier)
+
+readCommand = choice [
+    readCompoundCommand,
+    readCoProc,
+    readSimpleCommand
+    ]
+
+readCmdName = do
+    -- Ignore alias suppression
+    optional . try $ do
+        char '\\'
+        lookAhead $ variableChars
+    readCmdWord
+
+readCmdWord = do
+    skipAnnotationAndWarn
+    readNormalWord <* spacing
+
+-- Due to poor planning, annotations after commands isn't handled well.
+-- At the time this function is used, it's usually too late to skip
+-- comments, so you end up with a parse failure instead.
+skipAnnotationAndWarn = optional $ do
+        try . lookAhead $ readAnnotationPrefix
+        parseProblem ErrorC 1126 "Place shellcheck directives before commands, not after."
+        readAnyComment
+
+prop_readIfClause = isOk readIfClause "if false; then foo; elif true; then stuff; more stuff; else cows; fi"
+prop_readIfClause2 = isWarning readIfClause "if false; then; echo oo; fi"
+prop_readIfClause3 = isWarning readIfClause "if false; then true; else; echo lol; fi"
+prop_readIfClause4 = isWarning readIfClause "if false; then true; else if true; then echo lol; fi; fi"
+prop_readIfClause5 = isOk readIfClause "if false; then true; else\nif true; then echo lol; fi; fi"
+readIfClause = called "if expression" $ do
+    id <- getNextId
+    pos <- getPosition
+    (condition, action) <- readIfPart
+    elifs <- many readElifPart
+    elses <- option [] readElsePart
+
+    g_Fi `orFail` do
+        parseProblemAt pos ErrorC 1046 "Couldn't find 'fi' for this 'if'."
+        parseProblem ErrorC 1047 "Expected 'fi' matching previously mentioned 'if'."
+        return "Expected 'fi'"
+
+    return $ T_IfExpression id ((condition, action):elifs) elses
+
+
+verifyNotEmptyIf s =
+    optional (do
+                emptyPos <- getPosition
+                try . lookAhead $ (g_Fi <|> g_Elif <|> g_Else)
+                parseProblemAt emptyPos ErrorC 1048 $ "Can't have empty " ++ s ++ " clauses (use 'true' as a no-op).")
+readIfPart = do
+    pos <- getPosition
+    g_If
+    allspacing
+    condition <- readTerm
+
+    ifNextToken (g_Fi <|> g_Elif <|> g_Else) $
+        parseProblemAt pos ErrorC 1049 "Did you forget the 'then' for this 'if'?"
+
+    called "then clause" $ do
+        g_Then `orFail` do
+            parseProblem ErrorC 1050 "Expected 'then'."
+            return "Expected 'then'"
+
+        acceptButWarn g_Semi ErrorC 1051 "Semicolons directly after 'then' are not allowed. Just remove it."
+        allspacing
+        verifyNotEmptyIf "then"
+
+        action <- readTerm
+        return (condition, action)
+
+readElifPart = called "elif clause" $ do
+    pos <- getPosition
+    g_Elif
+    allspacing
+    condition <- readTerm
+    ifNextToken (g_Fi <|> g_Elif <|> g_Else) $
+        parseProblemAt pos ErrorC 1049 "Did you forget the 'then' for this 'elif'?"
+
+    g_Then
+    acceptButWarn g_Semi ErrorC 1052 "Semicolons directly after 'then' are not allowed. Just remove it."
+    allspacing
+    verifyNotEmptyIf "then"
+    action <- readTerm
+    return (condition, action)
+
+readElsePart = called "else clause" $ do
+    pos <- getPosition
+    g_Else
+    optional $ do
+        try . lookAhead $ g_If
+        parseProblemAt pos ErrorC 1075 "Use 'elif' instead of 'else if' (or put 'if' on new line if nesting)."
+
+    acceptButWarn g_Semi ErrorC 1053 "Semicolons directly after 'else' are not allowed. Just remove it."
+    allspacing
+    verifyNotEmptyIf "else"
+    readTerm
+
+ifNextToken parser action =
+    optional $ do
+        try . lookAhead $ parser
+        action
+
+prop_readSubshell = isOk readSubshell "( cd /foo; tar cf stuff.tar * )"
+readSubshell = called "explicit subshell" $ do
+    id <- getNextId
+    char '('
+    allspacing
+    list <- readCompoundList
+    allspacing
+    char ')' <|> fail ") closing the subshell"
+    return $ T_Subshell id list
+
+prop_readBraceGroup = isOk readBraceGroup "{ a; b | c | d; e; }"
+prop_readBraceGroup2 = isWarning readBraceGroup "{foo;}"
+prop_readBraceGroup3 = isOk readBraceGroup "{(foo)}"
+readBraceGroup = called "brace group" $ do
+    id <- getNextId
+    char '{'
+    void allspacingOrFail <|> optional (do
+        lookAhead $ noneOf "(" -- {( is legal
+        parseProblem ErrorC 1054 "You need a space after the '{'.")
+    optional $ do
+        pos <- getPosition
+        lookAhead $ char '}'
+        parseProblemAt pos ErrorC 1055 "You need at least one command here. Use 'true;' as a no-op."
+    list <- readTerm
+    char '}' <|> do
+        parseProblem ErrorC 1056 "Expected a '}'. If you have one, try a ; or \\n in front of it."
+        fail "Missing '}'"
+    return $ T_BraceGroup id list
+
+prop_readWhileClause = isOk readWhileClause "while [[ -e foo ]]; do sleep 1; done"
+readWhileClause = called "while loop" $ do
+    pos <- getPosition
+    (T_While id) <- g_While
+    condition <- readTerm
+    statements <- readDoGroup pos
+    return $ T_WhileExpression id condition statements
+
+prop_readUntilClause = isOk readUntilClause "until kill -0 $PID; do sleep 1; done"
+readUntilClause = called "until loop" $ do
+    pos <- getPosition
+    (T_Until id) <- g_Until
+    condition <- readTerm
+    statements <- readDoGroup pos
+    return $ T_UntilExpression id condition statements
+
+readDoGroup loopPos = do
+    pos <- getPosition
+    optional (do
+                try . lookAhead $ g_Done
+                parseProblemAt loopPos ErrorC 1057 "Did you forget the 'do' for this loop?")
+
+    g_Do `orFail` do
+        parseProblem ErrorC 1058 "Expected 'do'."
+        return "Expected 'do'"
+
+    acceptButWarn g_Semi ErrorC 1059 "No semicolons directly after 'do'."
+    allspacing
+
+    optional (do
+                try . lookAhead $ g_Done
+                parseProblemAt loopPos ErrorC 1060 "Can't have empty do clauses (use 'true' as a no-op).")
+
+    commands <- readCompoundList
+    g_Done `orFail` do
+            parseProblemAt pos ErrorC 1061 "Couldn't find 'done' for this 'do'."
+            parseProblem ErrorC 1062 "Expected 'done' matching previously mentioned 'do'."
+            return "Expected 'done'"
+    return commands
+
+
+prop_readForClause = isOk readForClause "for f in *; do rm \"$f\"; done"
+prop_readForClause3 = isOk readForClause "for f; do foo; done"
+prop_readForClause4 = isOk readForClause "for((i=0; i<10; i++)); do echo $i; done"
+prop_readForClause5 = isOk readForClause "for ((i=0;i<10 && n>x;i++,--n))\ndo \necho $i\ndone"
+prop_readForClause6 = isOk readForClause "for ((;;))\ndo echo $i\ndone"
+prop_readForClause7 = isOk readForClause "for ((;;)) do echo $i\ndone"
+prop_readForClause8 = isOk readForClause "for ((;;)) ; do echo $i\ndone"
+prop_readForClause9 = isOk readForClause "for i do true; done"
+prop_readForClause10= isOk readForClause "for ((;;)) { true; }"
+prop_readForClause12= isWarning readForClause "for $a in *; do echo \"$a\"; done"
+prop_readForClause13= isOk readForClause "for foo\nin\\\n  bar\\\n  baz\ndo true; done"
+readForClause = called "for loop" $ do
+    pos <- getPosition
+    (T_For id) <- g_For
+    spacing
+    readArithmetic id pos <|> readRegular id pos
+  where
+    readArithmetic id pos = called "arithmetic for condition" $ do
+        try $ string "(("
+        x <- readArithmeticContents
+        char ';' >> spacing
+        y <- readArithmeticContents
+        char ';' >> spacing
+        z <- readArithmeticContents
+        spacing
+        string "))"
+        spacing
+        optional $ readSequentialSep >> spacing
+        group <- readBraced <|> readDoGroup pos
+        return $ T_ForArithmetic id x y z group
+
+    readBraced = do
+        (T_BraceGroup _ list) <- readBraceGroup
+        return list
+
+    readRegular id pos = do
+        acceptButWarn (char '$') ErrorC 1086
+            "Don't use $ on the iterator name in for loops."
+        name <- readVariableName `thenSkip` allspacing
+        values <- readInClause <|> (optional readSequentialSep >> return [])
+        group <- readDoGroup pos
+        return $ T_ForIn id name values group
+
+prop_readSelectClause1 = isOk readSelectClause "select foo in *; do echo $foo; done"
+prop_readSelectClause2 = isOk readSelectClause "select foo; do echo $foo; done"
+readSelectClause = called "select loop" $ do
+    pos <- getPosition
+    (T_Select id) <- g_Select
+    spacing
+    typ <- readRegular
+    group <- readDoGroup pos
+    typ id group
+  where
+    readRegular = do
+        name <- readVariableName
+        spacing
+        values <- readInClause <|> (readSequentialSep >> return [])
+        return $ \id group -> (return $ T_SelectIn id name values group)
+
+readInClause = do
+    g_In
+    things <- readCmdWord `reluctantlyTill`
+                (void g_Semi <|> void linefeed <|> void g_Do)
+
+    do {
+        lookAhead g_Do;
+        parseNote ErrorC 1063 "You need a line feed or semicolon before the 'do'.";
+    } <|> do {
+        optional g_Semi;
+        void allspacing;
+    }
+
+    return things
+
+prop_readCaseClause = isOk readCaseClause "case foo in a ) lol; cow;; b|d) fooo; esac"
+prop_readCaseClause2 = isOk readCaseClause "case foo\n in * ) echo bar;; esac"
+prop_readCaseClause3 = isOk readCaseClause "case foo\n in * ) echo bar & ;; esac"
+prop_readCaseClause4 = isOk readCaseClause "case foo\n in *) echo bar ;& bar) foo; esac"
+prop_readCaseClause5 = isOk readCaseClause "case foo\n in *) echo bar;;& foo) baz;; esac"
+readCaseClause = called "case expression" $ do
+    id <- getNextId
+    g_Case
+    word <- readNormalWord
+    allspacing
+    g_In <|> fail "Expected 'in'"
+    readLineBreak
+    list <- readCaseList
+    g_Esac <|> fail "Expected 'esac' to close the case statement"
+    return $ T_CaseExpression id word list
+
+readCaseList = many readCaseItem
+
+readCaseItem = called "case item" $ do
+    notFollowedBy2 g_Esac
+    optional $ do
+        try . lookAhead $ readAnnotationPrefix
+        parseProblem ErrorC 1124 "ShellCheck directives are only valid in front of complete commands like 'case' statements, not individual case branches."
+    optional g_Lparen
+    spacing
+    pattern' <- readPattern
+    void g_Rparen <|> do
+        parseProblem ErrorC 1085
+            "Did you forget to move the ;; after extending this case item?"
+        fail "Expected ) to open a new case item"
+    readLineBreak
+    list <- (lookAhead readCaseSeparator >> return []) <|> readCompoundList
+    separator <- readCaseSeparator `attempting` do
+        pos <- getPosition
+        lookAhead g_Rparen
+        parseProblemAt pos ErrorC 1074
+            "Did you forget the ;; after the previous case item?"
+    readLineBreak
+    return (separator, pattern', list)
+
+readCaseSeparator = choice [
+    tryToken ";;&" (const ()) >> return CaseContinue,
+    tryToken ";&" (const ()) >> return CaseFallThrough,
+    g_DSEMI >> return CaseBreak,
+    lookAhead (readLineBreak >> g_Esac) >> return CaseBreak
+    ]
+
+prop_readFunctionDefinition = isOk readFunctionDefinition "foo() { command foo --lol \"$@\"; }"
+prop_readFunctionDefinition1 = isOk readFunctionDefinition "foo   (){ command foo --lol \"$@\"; }"
+prop_readFunctionDefinition4 = isWarning readFunctionDefinition "foo(a, b) { true; }"
+prop_readFunctionDefinition5 = isOk readFunctionDefinition ":(){ :|:;}"
+prop_readFunctionDefinition6 = isOk readFunctionDefinition "?(){ foo; }"
+prop_readFunctionDefinition7 = isOk readFunctionDefinition "..(){ cd ..; }"
+prop_readFunctionDefinition8 = isOk readFunctionDefinition "foo() (ls)"
+prop_readFunctionDefinition9 = isOk readFunctionDefinition "function foo { true; }"
+prop_readFunctionDefinition10= isOk readFunctionDefinition "function foo () { true; }"
+prop_readFunctionDefinition11= isWarning readFunctionDefinition "function foo{\ntrue\n}"
+prop_readFunctionDefinition12= isOk readFunctionDefinition "function []!() { true; }"
+readFunctionDefinition = called "function" $ do
+    functionSignature <- try readFunctionSignature
+    allspacing
+    void (lookAhead $ oneOf "{(") <|> parseProblem ErrorC 1064 "Expected a { to open the function definition."
+    group <- readBraceGroup <|> readSubshell
+    return $ functionSignature group
+  where
+    readFunctionSignature =
+        readWithFunction <|> readWithoutFunction
+      where
+        readWithFunction = do
+            id <- getNextId
+            try $ do
+                string "function"
+                whitespace
+            spacing
+            name <- many1 extendedFunctionChars
+            spaces <- spacing
+            hasParens <- wasIncluded readParens
+            when (not hasParens && null spaces) $
+                acceptButWarn (lookAhead (oneOf "{("))
+                    ErrorC 1095 "You need a space or linefeed between the function name and body."
+            return $ T_Function id (FunctionKeyword True) (FunctionParentheses hasParens) name
+
+        readWithoutFunction = try $ do
+            id <- getNextId
+            name <- many1 functionChars
+            guard $ name /= "time"  -- Interfers with time ( foo )
+            spacing
+            readParens
+            return $ T_Function id (FunctionKeyword False) (FunctionParentheses True) name
+
+        readParens = do
+            g_Lparen
+            spacing
+            g_Rparen <|> do
+                parseProblem ErrorC 1065 "Trying to declare parameters? Don't. Use () and refer to params as $1, $2.."
+                many $ noneOf "\n){"
+                g_Rparen
+            return ()
+
+prop_readCoProc1 = isOk readCoProc "coproc foo { echo bar; }"
+prop_readCoProc2 = isOk readCoProc "coproc { echo bar; }"
+prop_readCoProc3 = isOk readCoProc "coproc echo bar"
+readCoProc = called "coproc" $ do
+    id <- getNextId
+    try $ do
+        string "coproc"
+        whitespace
+    choice [ try $ readCompoundCoProc id, readSimpleCoProc id ]
+  where
+    readCompoundCoProc id = do
+        var <- optionMaybe $
+            readVariableName `thenSkip` whitespace
+        body <- readBody readCompoundCommand
+        return $ T_CoProc id var body
+    readSimpleCoProc id = do
+        body <- readBody readSimpleCommand
+        return $ T_CoProc id Nothing body
+    readBody parser = do
+        id <- getNextId
+        body <- parser
+        return $ T_CoProcBody id body
+
+
+readPattern = (readNormalWord `thenSkip` spacing) `sepBy1` (char '|' `thenSkip` spacing)
+
+prop_readCompoundCommand = isOk readCompoundCommand "{ echo foo; }>/dev/null"
+readCompoundCommand = do
+    id <- getNextId
+    cmd <- choice [
+        readBraceGroup,
+        readAmbiguous "((" readArithmeticExpression readSubshell (\pos ->
+            parseNoteAt pos WarningC 1105 "Shells disambiguate (( differently or not at all. For subshell, add spaces around ( . For ((, fix parsing errors."),
+        readSubshell,
+        readCondition,
+        readWhileClause,
+        readUntilClause,
+        readIfClause,
+        readForClause,
+        readSelectClause,
+        readCaseClause,
+        readFunctionDefinition
+        ]
+    spacing
+    redirs <- many readIoRedirect
+    unless (null redirs) $ optional $ do
+        lookAhead $ try (spacing >> needsSeparator)
+        parseProblem WarningC 1013 "Bash requires ; or \\n here, after redirecting nested compound commands."
+    return $ T_Redirecting id redirs cmd
+  where
+    needsSeparator = choice [ g_Then, g_Else, g_Elif, g_Fi, g_Do, g_Done, g_Esac, g_Rbrace ]
+
+
+readCompoundList = readTerm
+readCompoundListOrEmpty = do
+    allspacing
+    readTerm <|> return []
+
+readCmdPrefix = many1 (readIoRedirect <|> readAssignmentWord)
+readCmdSuffix = many1 (readIoRedirect <|> readCmdWord)
+readModifierSuffix = many1 (readIoRedirect <|> readWellFormedAssignment <|> readCmdWord)
+readTimeSuffix = do
+    flags <- many readFlag
+    pipeline <- readPipeline
+    return $ flags ++ [pipeline]
+  where
+    -- This fails for quoted variables and such. Fixme?
+    readFlag = do
+        lookAhead $ char '-'
+        readCmdWord
+
+-- Fixme: this is a hack that doesn't handle let c='4'"5" or let a\>b
+readLetSuffix :: Monad m => SCParser m [Token]
+readLetSuffix = many1 (readIoRedirect <|> try readLetExpression <|> readCmdWord)
+  where
+    readLetExpression :: Monad m => SCParser m Token
+    readLetExpression = do
+        startPos <- getPosition
+        expression <- readStringForParser readCmdWord
+        let (unQuoted, newPos) = kludgeAwayQuotes expression startPos
+        subParse newPos readArithmeticContents unQuoted
+
+    kludgeAwayQuotes :: String -> SourcePos -> (String, SourcePos)
+    kludgeAwayQuotes s p =
+        case s of
+            first:rest@(_:_) ->
+                let (last:backwards) = reverse rest
+                    middle = reverse backwards
+                in
+                    if first `elem` "'\"" && first == last
+                    then (middle, updatePosChar p first)
+                    else (s, p)
+            x -> (s, p)
+
+
+-- bash allows a=(b), ksh allows $a=(b). dash allows neither. Let's warn.
+readEvalSuffix = many1 (readIoRedirect <|> readCmdWord <|> evalFallback)
+  where
+    evalFallback = do
+        pos <- getPosition
+        lookAhead $ char '('
+        parseProblemAt pos WarningC 1098 "Quote/escape special characters when using eval, e.g. eval \"a=(b)\"."
+        fail "Unexpected parentheses. Make sure to quote when eval'ing as shell parsers differ."
+
+-- Get whatever a parser would parse as a string
+readStringForParser parser = do
+    pos <- inSeparateContext $ lookAhead (parser >> getPosition)
+    readUntil pos
+  where
+    readUntil endPos = anyChar `reluctantlyTill` (getPosition >>= guard . (== endPos))
+
+prop_readAssignmentWord = isOk readAssignmentWord "a=42"
+prop_readAssignmentWord2 = isOk readAssignmentWord "b=(1 2 3)"
+prop_readAssignmentWord3 = isWarning readAssignmentWord "$b = 13"
+prop_readAssignmentWord4 = isWarning readAssignmentWord "b = $(lol)"
+prop_readAssignmentWord5 = isOk readAssignmentWord "b+=lol"
+prop_readAssignmentWord6 = isWarning readAssignmentWord "b += (1 2 3)"
+prop_readAssignmentWord7 = isOk readAssignmentWord "a[3$n'']=42"
+prop_readAssignmentWord8 = isOk readAssignmentWord "a[4''$(cat foo)]=42"
+prop_readAssignmentWord9 = isOk readAssignmentWord "IFS= "
+prop_readAssignmentWord9a= isOk readAssignmentWord "foo="
+prop_readAssignmentWord9b= isOk readAssignmentWord "foo=  "
+prop_readAssignmentWord9c= isOk readAssignmentWord "foo=  #bar"
+prop_readAssignmentWord10= isWarning readAssignmentWord "foo$n=42"
+prop_readAssignmentWord11= isOk readAssignmentWord "foo=([a]=b [c] [d]= [e f )"
+prop_readAssignmentWord12= isOk readAssignmentWord "a[b <<= 3 + c]='thing'"
+prop_readAssignmentWord13= isOk readAssignmentWord "var=( (1 2) (3 4) )"
+prop_readAssignmentWord14= isOk readAssignmentWord "var=( 1 [2]=(3 4) )"
+prop_readAssignmentWord15= isOk readAssignmentWord "var=(1 [2]=(3 4))"
+readAssignmentWord = readAssignmentWordExt True
+readWellFormedAssignment = readAssignmentWordExt False
+readAssignmentWordExt lenient = try $ do
+    id <- getNextId
+    pos <- getPosition
+    when lenient $
+        optional (char '$' >> parseNote ErrorC 1066 "Don't use $ on the left side of assignments.")
+    variable <- readVariableName
+    when lenient $
+        optional (readNormalDollar >> parseNoteAt pos ErrorC
+                                1067 "For indirection, use (associative) arrays or 'read \"var$n\" <<< \"value\"'")
+    indices <- many readArrayIndex
+    hasLeftSpace <- liftM (not . null) spacing
+    pos <- getPosition
+    op <- readAssignmentOp
+    hasRightSpace <- liftM (not . null) spacing
+    isEndOfCommand <- liftM isJust $ optionMaybe (try . lookAhead $ (void (oneOf "\r\n;&|)") <|> eof))
+    if not hasLeftSpace && (hasRightSpace || isEndOfCommand)
+      then do
+        when (variable /= "IFS" && hasRightSpace && not isEndOfCommand) $
+            parseNoteAt pos WarningC 1007
+                "Remove space after = if trying to assign a value (for empty string, use var='' ... )."
+        value <- readEmptyLiteral
+        return $ T_Assignment id op variable indices value
+      else do
+        when (hasLeftSpace || hasRightSpace) $
+            parseNoteAt pos ErrorC 1068 $
+                "Don't put spaces around the "
+                ++ if op == Append
+                    then "+= when appending."
+                    else "= in assignments."
+        value <- readArray <|> readNormalWord
+        spacing
+        return $ T_Assignment id op variable indices value
+  where
+    readAssignmentOp = do
+        pos <- getPosition
+        unexpecting "" $ string "==="
+        choice [
+            string "+=" >> return Append,
+            do
+                try (string "==")
+                parseProblemAt pos ErrorC 1097
+                    "Unexpected ==. For assignment, use =. For comparison, use [/[[."
+                return Assign,
+
+            string "=" >> return Assign
+            ]
+    readEmptyLiteral = do
+        id <- getNextId
+        return $ T_Literal id ""
+
+readArrayIndex = do
+    id <- getNextId
+    char '['
+    pos <- getPosition
+    str <- readStringForParser readIndexSpan
+    char ']'
+    return $ T_UnparsedIndex id pos str
+
+readArray :: Monad m => SCParser m Token
+readArray = called "array assignment" $ do
+    id <- getNextId
+    opening <- getPosition
+    char '('
+    optional $ do
+        lookAhead $ char '('
+        parseProblemAt opening ErrorC 1116 "Missing $ on a $((..)) expression? (or use ( ( for arrays)."
+    allspacing
+    words <- readElement `reluctantlyTill` char ')'
+    char ')' <|> fail "Expected ) to close array assignment"
+    return $ T_Array id words
+  where
+    readElement = (readIndexed <|> readRegular) `thenSkip` allspacing
+    readIndexed = do
+        id <- getNextId
+        index <- try $ do
+            x <- many1 readArrayIndex
+            char '='
+            return x
+        value <- readRegular <|> nothing
+        return $ T_IndexedElement id index value
+    readRegular = readArray <|> readNormalWord
+
+    nothing = do
+        id <- getNextId
+        return $ T_Literal id ""
+
+tryToken s t = try $ do
+    id <- getNextId
+    string s
+    spacing
+    return $ t id
+
+redirToken c t = try $ do
+    id <- getNextId
+    char c
+    notFollowedBy2 $ char '('
+    return $ t id
+
+tryWordToken s t = tryParseWordToken s t `thenSkip` spacing
+tryParseWordToken keyword t = try $ do
+    id <- getNextId
+    str <- anycaseString keyword
+
+    optional $ do
+        c <- try . lookAhead $ anyChar
+        let warning code = parseProblem ErrorC code $ "You need a space before the " ++ [c] ++ "."
+        case c of
+            '[' -> warning 1069
+            '#' -> warning 1099
+            '!' -> warning 1129
+            ':' -> warning 1130
+            _ -> return ()
+
+    lookAhead keywordSeparator
+    when (str /= keyword) $
+        parseProblem ErrorC 1081 $
+            "Scripts are case sensitive. Use '" ++ keyword ++ "', not '" ++ str ++ "'."
+    return $ t id
+
+anycaseString =
+    mapM anycaseChar
+  where
+    anycaseChar c = char (toLower c) <|> char (toUpper c)
+
+g_AND_IF = tryToken "&&" T_AND_IF
+g_OR_IF = tryToken "||" T_OR_IF
+g_DSEMI = tryToken ";;" T_DSEMI
+g_DLESS = tryToken "<<" T_DLESS
+g_DGREAT = tryToken ">>" T_DGREAT
+g_LESSAND = tryToken "<&" T_LESSAND
+g_GREATAND = tryToken ">&" T_GREATAND
+g_LESSGREAT = tryToken "<>" T_LESSGREAT
+g_DLESSDASH = tryToken "<<-" T_DLESSDASH
+g_CLOBBER = tryToken ">|" T_CLOBBER
+g_OPERATOR = g_AND_IF <|> g_OR_IF <|> g_DSEMI <|> g_DLESSDASH <|> g_DLESS <|> g_DGREAT <|> g_LESSAND <|> g_GREATAND <|> g_LESSGREAT
+
+g_If = tryWordToken "if" T_If
+g_Then = tryWordToken "then" T_Then
+g_Else = tryWordToken "else" T_Else
+g_Elif = tryWordToken "elif" T_Elif
+g_Fi = tryWordToken "fi" T_Fi
+g_Do = tryWordToken "do" T_Do
+g_Done = tryWordToken "done" T_Done
+g_Case = tryWordToken "case" T_Case
+g_Esac = tryWordToken "esac" T_Esac
+g_While = tryWordToken "while" T_While
+g_Until = tryWordToken "until" T_Until
+g_For = tryWordToken "for" T_For
+g_Select = tryWordToken "select" T_Select
+g_In = tryWordToken "in" T_In <* skipAnnotationAndWarn
+g_Lbrace = tryWordToken "{" T_Lbrace
+g_Rbrace = do -- handled specially due to ksh echo "${ foo; }bar"
+    id <- getNextId
+    char '}'
+    return $ T_Rbrace id
+
+g_Lparen = tryToken "(" T_Lparen
+g_Rparen = tryToken ")" T_Rparen
+g_Bang = do
+    id <- getNextId
+    char '!'
+    void spacing1 <|> do
+        pos <- getPosition
+        parseProblemAt pos ErrorC 1035
+            "You are missing a required space after the !."
+    return $ T_Bang id
+
+g_Semi = do
+    notFollowedBy2 g_DSEMI
+    tryToken ";" T_Semi
+
+keywordSeparator =
+    eof <|> void (try allspacingOrFail) <|> void (oneOf ";()[<>&|")
+
+readKeyword = choice [ g_Then, g_Else, g_Elif, g_Fi, g_Do, g_Done, g_Esac, g_Rbrace, g_Rparen, g_DSEMI ]
+
+ifParse p t f =
+    (lookAhead (try p) >> t) <|> f
+
+prop_readShebang1 = isOk readShebang "#!/bin/sh\n"
+prop_readShebang2 = isWarning readShebang "!# /bin/sh\n"
+prop_readShebang3 = isNotOk readShebang "#shellcheck shell=/bin/sh\n"
+prop_readShebang4 = isWarning readShebang "! /bin/sh"
+prop_readShebang5 = isWarning readShebang "\n#!/bin/sh"
+prop_readShebang6 = isWarning readShebang " # Copyright \n!#/bin/bash"
+prop_readShebang7 = isNotOk readShebang "# Copyright \nfoo\n#!/bin/bash"
+readShebang = do
+    anyShebang <|> try readMissingBang <|> withHeader
+    many linewhitespace
+    str <- many $ noneOf "\r\n"
+    optional carriageReturn
+    optional linefeed
+    return str
+  where
+    anyShebang = choice $ map try [
+        readCorrect,
+        readSwapped,
+        readTooManySpaces,
+        readMissingHash
+        ]
+    readCorrect = void $ string "#!"
+
+    readSwapped = do
+        pos <- getPosition
+        string "!#"
+        parseProblemAt pos ErrorC 1084
+            "Use #!, not !#, for the shebang."
+
+    skipSpaces = fmap (not . null) $ many linewhitespace
+    readTooManySpaces = do
+        startPos <- getPosition
+        startSpaces <- skipSpaces
+        char '#'
+        middlePos <- getPosition
+        middleSpaces <- skipSpaces
+        char '!'
+        when startSpaces $
+            parseProblemAt startPos ErrorC 1114
+                "Remove leading spaces before the shebang."
+        when middleSpaces $
+            parseProblemAt middlePos ErrorC 1115
+                "Remove spaces between # and ! in the shebang."
+
+    readMissingHash = do
+        pos <- getPosition
+        char '!'
+        ensurePathAhead
+        parseProblemAt pos ErrorC 1104
+            "Use #!, not just !, for the shebang."
+
+    readMissingBang = do
+        char '#'
+        pos <- getPosition
+        ensurePathAhead
+        parseProblemAt pos ErrorC 1113
+            "Use #!, not just #, for the shebang."
+
+    ensurePathAhead = lookAhead $ do
+        many linewhitespace
+        char '/'
+
+    withHeader = try $ do
+        many1 headerLine
+        pos <- getPosition
+        anyShebang <*
+            parseProblemAt pos ErrorC 1128 "The shebang must be on the first line. Delete blanks and move comments."
+
+    headerLine = do
+        notFollowedBy2 anyShebang
+        many linewhitespace
+        optional readAnyComment
+        linefeed
+
+verifyEof = eof <|> choice [
+        ifParsable g_Lparen $
+            parseProblem ErrorC 1088 "Parsing stopped here. Invalid use of parentheses?",
+
+        ifParsable readKeyword $
+            parseProblem ErrorC 1089 "Parsing stopped here. Is this keyword correctly matched up?",
+
+        parseProblem ErrorC 1070 "Parsing stopped here. Mismatched keywords or invalid parentheses?"
+    ]
+  where
+    ifParsable p action = do
+        try (lookAhead p)
+        action
+
+prop_readScript1 = isOk readScriptFile "#!/bin/bash\necho hello world\n"
+prop_readScript2 = isWarning readScriptFile "#!/bin/bash\r\necho hello world\n"
+prop_readScript3 = isWarning readScriptFile "#!/bin/bash\necho hello\xA0world"
+prop_readScript4 = isWarning readScriptFile "#!/usr/bin/perl\nfoo=("
+prop_readScript5 = isOk readScriptFile "#!/bin/bash\n#This is an empty script\n\n"
+readScriptFile = do
+    id <- getNextId
+    pos <- getPosition
+    optional $ do
+        readUtf8Bom
+        parseProblem ErrorC 1082
+            "This file has a UTF-8 BOM. Remove it with: LC_CTYPE=C sed '1s/^...//' < yourscript ."
+    sb <- option "" readShebang
+    verifyShell pos (getShell sb)
+    if isValidShell (getShell sb) /= Just False
+      then do
+            allspacing
+            annotationId <- getNextId
+            annotations <- readAnnotations
+            commands <- withAnnotations annotations readCompoundListOrEmpty
+            verifyEof
+            let script = T_Annotation annotationId annotations $  T_Script id sb commands
+            reparseIndices script
+        else do
+            many anyChar
+            return $ T_Script id sb []
+
+  where
+    basename s = reverse . takeWhile (/= '/') . reverse $ s
+    getShell sb =
+        case words sb of
+            [] -> ""
+            [x] -> basename x
+            (first:second:_) ->
+                if basename first == "env"
+                    then second
+                    else basename first
+
+    verifyShell pos s =
+        case isValidShell s of
+            Just True -> return ()
+            Just False -> parseProblemAt pos ErrorC 1071 "ShellCheck only supports sh/bash/dash/ksh scripts. Sorry!"
+            Nothing -> parseProblemAt pos InfoC 1008 "This shebang was unrecognized. Note that ShellCheck only handles sh/bash/dash/ksh."
+
+    isValidShell s =
+        let good = s == "" || any (`isPrefixOf` s) goodShells
+            bad = any (`isPrefixOf` s) badShells
+        in
+            if good
+                then Just True
+                else if bad
+                        then Just False
+                        else Nothing
+
+    goodShells = [
+        "sh",
+        "ash",
+        "dash",
+        "bash",
+        "ksh"
+        ]
+    badShells = [
+        "awk",
+        "csh",
+        "expect",
+        "perl",
+        "python",
+        "ruby",
+        "tcsh",
+        "zsh"
+        ]
+
+    readUtf8Bom = called "Byte Order Mark" $ string "\xFEFF"
+
+readScript = readScriptFile
+
+-- Interactively run a specific parser in ghci:
+-- debugParse readSimpleCommand "echo 'hello world'"
+debugParse p string = runIdentity $ do
+    (res, _) <- runParser testEnvironment p "-" string
+    return res
+
+-- Interactively run the complete parser in ghci:
+-- debugParseScript "#!/bin/bash\necho 'Hello World'\n"
+debugParseScript string =
+    result {
+        -- Remove the noisiest parts
+        prTokenPositions = Map.fromList [
+            (Id 0, Position {
+                posFile = "removed for clarity",
+                posLine = -1,
+                posColumn = -1
+            })]
+    }
+  where
+    result = runIdentity $
+        parseScript (mockedSystemInterface []) $ ParseSpec {
+            psFilename = "debug",
+            psScript = string,
+            psCheckSourced = False
+        }
+
+testEnvironment =
+    Environment {
+        systemInterface = (mockedSystemInterface []),
+        checkSourced = False
+    }
+
+
+isOk p s =      parsesCleanly p s == Just True   -- The string parses with no warnings
+isWarning p s = parsesCleanly p s == Just False  -- The string parses with warnings
+isNotOk p s =   parsesCleanly p s == Nothing     -- The string does not parse
+
+parsesCleanly parser string = runIdentity $ do
+    (res, sys) <- runParser testEnvironment
+                    (parser >> eof >> getState) "-" string
+    case (res, sys) of
+        (Right userState, systemState) ->
+            return $ Just . null $ parseNotes userState ++ parseProblems systemState
+        (Left _, _) -> return Nothing
+
+-- For printf debugging: print the value of an expression
+-- Example: return $ dump $ T_Literal id [c]
+dump :: Show a => a -> a
+dump x = trace (show x) x
+
+-- Like above, but print a specific expression:
+-- Example: return $ dumps ("Returning: " ++ [c])  $ T_Literal id [c]
+dumps :: Show x => x -> a -> a
+dumps t = trace (show t)
+
+parseWithNotes parser = do
+    item <- parser
+    state <- getState
+    return (item, state)
+
+compareNotes (ParseNote pos1 pos1' level1 _ s1) (ParseNote pos2 pos2' level2 _ s2) = compare (pos1, pos1', level1) (pos2, pos2', level2)
+sortNotes = sortBy compareNotes
+
+
+makeErrorFor parsecError =
+    ParseNote pos pos ErrorC 1072 $
+        getStringFromParsec $ errorMessages parsecError
+    where
+      pos = errorPos parsecError
+
+getStringFromParsec errors =
+        case map f errors of
+            r -> unwords (take 1 $ catMaybes $ reverse r)  ++
+                " Fix any mentioned problems and try again."
+    where
+        f err =
+            case err of
+                UnExpect s    ->  Nothing -- Due to not knowing Parsec, none of these
+                SysUnExpect s ->  Nothing -- are actually helpful. <?> has been hidden
+                Expect s      ->  Nothing -- and we only show explicit fail statements.
+                Message s     ->  if null s then Nothing else return $ s ++ "."
+
+runParser :: Monad m =>
+    Environment m ->
+    SCParser m v ->
+    String ->
+    String ->
+    m (Either ParseError v, SystemState)
+
+runParser env p filename contents =
+    Ms.runStateT
+        (Mr.runReaderT
+            (runParserT p initialUserState filename contents)
+            env)
+        initialSystemState
+system = lift . lift . lift
+
+parseShell env name contents = do
+    (result, state) <- runParser env (parseWithNotes readScript) name contents
+    case result of
+        Right (script, userstate) ->
+            return ParseResult {
+                prComments = map toPositionedComment $ nub $ parseNotes userstate ++ parseProblems state,
+                prTokenPositions = Map.map posToPos (positionMap userstate),
+                prRoot = Just $
+                    reattachHereDocs script (hereDocMap userstate)
+            }
+        Left err ->
+            return ParseResult {
+                prComments =
+                    map toPositionedComment $
+                        notesForContext (contextStack state)
+                        ++ [makeErrorFor err]
+                        ++ parseProblems state,
+                prTokenPositions = Map.empty,
+                prRoot = Nothing
+            }
+
+notesForContext list = zipWith ($) [first, second] $ filter isName list
+  where
+    isName (ContextName _ _) = True
+    isName _ = False
+    first (ContextName pos str) = ParseNote pos pos ErrorC 1073 $
+        "Couldn't parse this " ++ str ++ ". Fix to allow more checks."
+    second (ContextName pos str) = ParseNote pos pos InfoC 1009 $
+        "The mentioned syntax error was in this " ++ str ++ "."
+
+-- Go over all T_UnparsedIndex and reparse them as either arithmetic or text
+-- depending on declare -A statements.
+reparseIndices root =
+   analyze blank blank f root
+  where
+    associative = getAssociativeArrays root
+    isAssociative s = s `elem` associative
+    f (T_Assignment id mode name indices value) = do
+        newIndices <- mapM (fixAssignmentIndex name) indices
+        newValue <- case value of
+            (T_Array id2 words) -> do
+                newWords <- mapM (fixIndexElement name) words
+                return $ T_Array id2 newWords
+            x -> return x
+        return $ T_Assignment id mode name newIndices newValue
+    f (TA_Variable id name indices) = do
+        newIndices <- mapM (fixAssignmentIndex name) indices
+        return $ TA_Variable id name newIndices
+    f t = return t
+
+    fixIndexElement name word =
+        case word of
+            T_IndexedElement id indices value -> do
+                new <- mapM (fixAssignmentIndex name) indices
+                return $ T_IndexedElement id new value
+            _ -> return word
+
+    fixAssignmentIndex name word =
+        case word of
+            T_UnparsedIndex id pos src ->
+                parsed name pos src
+            _ -> return word
+
+    parsed name pos src =
+        if isAssociative name
+        then subParse pos (called "associative array index" $ readIndexSpan) src
+        else subParse pos (called "arithmetic array index expression" $ optional space >> readArithmeticContents) src
+
+reattachHereDocs root map =
+    doTransform f root
+  where
+    f t@(T_HereDoc id dash quote string []) = fromMaybe t $ do
+        list <- Map.lookup id map
+        return $ T_HereDoc id dash quote string list
+    f t = t
+
+toPositionedComment :: ParseNote -> PositionedComment
+toPositionedComment (ParseNote start end severity code message) =
+    PositionedComment (posToPos start) (posToPos end) $ Comment severity code message
+
+posToPos :: SourcePos -> Position
+posToPos sp = Position {
+    posFile = sourceName sp,
+    posLine = fromIntegral $ sourceLine sp,
+    posColumn = fromIntegral $ sourceColumn sp
+}
+
+-- TODO: Clean up crusty old code that this is layered on top of
+parseScript :: Monad m =>
+        SystemInterface m -> ParseSpec -> m ParseResult
+parseScript sys spec =
+    parseShell env (psFilename spec) (psScript spec)
+  where
+    env = Environment {
+        systemInterface = sys,
+        checkSourced = psCheckSourced spec
+    }
+
+-- Same as 'try' but emit syntax errors if the parse fails.
+tryWithErrors :: Monad m => SCParser m v -> SCParser m v
+tryWithErrors parser = do
+    userstate <- getState
+    oldContext <- getCurrentContexts
+    input <- getInput
+    pos <- getPosition
+    result <- lift $ runParserT (setPosition pos >> getResult parser) userstate (sourceName pos) input
+    case result of
+        Right (result, endPos, endInput, endState) -> do
+            -- 'many' objects if we don't consume anything at all, so read a dummy value
+            void anyChar <|> eof
+            putState endState
+            setPosition endPos
+            setInput endInput
+            return result
+
+        Left err -> do
+            newContext <- getCurrentContexts
+            addParseProblem $ makeErrorFor err
+            mapM_ addParseProblem $ notesForContext newContext
+            setCurrentContexts oldContext
+            fail ""
+  where
+    getResult p = do
+        result <- p
+        endPos <- getPosition
+        endInput <- getInput
+        endState <- getState
+        return (result, endPos, endInput, endState)
+
+return []
+runTests = $quickCheckAll
+
diff --git a/src/ShellCheck/Regex.hs b/src/ShellCheck/Regex.hs
new file mode 100644
--- /dev/null
+++ b/src/ShellCheck/Regex.hs
@@ -0,0 +1,80 @@
+{-
+    Copyright 2012-2015 Vidar Holen
+
+    This file is part of ShellCheck.
+    https://www.shellcheck.net
+
+    ShellCheck is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    ShellCheck is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <https://www.gnu.org/licenses/>.
+-}
+{-# LANGUAGE FlexibleContexts #-}
+
+-- Basically Text.Regex based on regex-tdfa instead of the buggy regex-posix.
+module ShellCheck.Regex where
+
+import Data.List
+import Data.Maybe
+import Control.Monad
+import Text.Regex.TDFA
+
+-- Precompile the regex
+mkRegex :: String -> Regex
+mkRegex str =
+    let make :: RegexMaker Regex CompOption ExecOption String => String -> Regex
+        make = makeRegex
+    in
+        make str
+
+-- Does the regex match?
+matches :: String -> Regex -> Bool
+matches = flip match
+
+-- Get all subgroups of the first match
+matchRegex :: Regex -> String -> Maybe [String]
+matchRegex re str = do
+    (_, _, _, groups) <- matchM re str :: Maybe (String,String,String,[String])
+    return groups
+
+-- Get all full matches
+matchAllStrings :: Regex -> String -> [String]
+matchAllStrings re = unfoldr f
+  where
+    f :: String -> Maybe (String, String)
+    f str = do
+        (_, match, rest, _) <- matchM re str :: Maybe (String, String, String, [String])
+        return (match, rest)
+
+-- Get all subgroups from all matches
+matchAllSubgroups :: Regex -> String -> [[String]]
+matchAllSubgroups re = unfoldr f
+  where
+    f :: String -> Maybe ([String], String)
+    f str = do
+        (_, _, rest, groups) <- matchM re str :: Maybe (String, String, String, [String])
+        return (groups, rest)
+
+-- Replace regex in input with string
+subRegex :: Regex -> String -> String -> String
+subRegex re input replacement = f input
+  where
+    f str = fromMaybe str $ do
+        (before, match, after) <- matchM re str :: Maybe (String, String, String)
+        when (null match) $ error ("Internal error: substituted empty in " ++ str)
+        return $ before ++ replacement ++ f after
+
+-- Split a string based on a regex.
+splitOn :: String -> Regex -> [String]
+splitOn input re =
+    case matchM re input :: Maybe (String, String, String) of
+        Just (before, match, after) -> before : after `splitOn` re
+        Nothing -> [input]
