-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
45 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,48 @@ | ||
module Nix.Linter.Tools.FreeVars (freeVars, freeVars') where | ||
module Nix.Linter.Tools.FreeVars (freeVars, freeVars', freeVarsIgnoreTopBinds | ||
, freeVarsIgnoreTopBinds') where | ||
import Data.Set (Set) | ||
|
||
import Nix.Expr.Types | ||
import Nix.Expr.Types.Annotated | ||
import Nix.TH (freeVars) | ||
|
||
import qualified Data.Set as Set | ||
import Data.Maybe (mapMaybe) | ||
import Data.Fix (unFix, Fix (..)) | ||
|
||
freeVars' :: NExprLoc -> Set VarName | ||
freeVars' = freeVars . stripAnnotation | ||
|
||
freeVarsIgnoreTopBinds' :: NExprLoc -> Set VarName | ||
freeVarsIgnoreTopBinds' = freeVarsIgnoreTopBinds . stripAnnotation | ||
|
||
-- gets the free variables of the expression, assuming the top level bindings | ||
-- were not bound. Note that this function is *not* recursive, since we want to | ||
-- count bindings deeper in the tree | ||
freeVarsIgnoreTopBinds :: NExpr -> Set VarName | ||
freeVarsIgnoreTopBinds e = | ||
case unFix e of | ||
(NSet NRecursive bindings) -> bindFreeVars bindings | ||
(NAbs (Param _) expr) -> freeVars expr | ||
(NAbs (ParamSet set _ _) expr) -> | ||
freeVars expr <> (Set.unions $ freeVars <$> mapMaybe snd set) | ||
(NLet bindings expr) -> freeVars expr <> bindFreeVars bindings | ||
noTopBinds -> freeVars $ Fix noTopBinds | ||
where | ||
bindFreeVars :: Foldable t => t (Binding NExpr) -> Set VarName | ||
bindFreeVars = foldMap bind1Free | ||
where | ||
bind1Free :: Binding NExpr -> Set VarName | ||
bind1Free (Inherit Nothing keys _) = Set.fromList $ mapMaybe staticKey keys | ||
bind1Free (Inherit (Just scope) _ _) = freeVars scope | ||
bind1Free (NamedVar path expr _) = pathFree path <> freeVars expr | ||
|
||
staticKey :: NKeyName r -> Maybe VarName | ||
staticKey (StaticKey varname) = pure varname | ||
staticKey (DynamicKey _ ) = mempty | ||
|
||
pathFree :: NAttrPath NExpr -> Set VarName | ||
pathFree = foldMap mapFreeVars | ||
|
||
mapFreeVars :: Foldable t => t NExpr -> Set VarName | ||
mapFreeVars = foldMap freeVars |