diff --git a/hledger-lib/Hledger/Data/AccountName.hs b/hledger-lib/Hledger/Data/AccountName.hs index ea61ca2c1..b1779a1ed 100644 --- a/hledger-lib/Hledger/Data/AccountName.hs +++ b/hledger-lib/Hledger/Data/AccountName.hs @@ -59,6 +59,7 @@ parentAccountNames a = parentAccountNames' $ parentAccountName a parentAccountNames' "" = [] parentAccountNames' a = a : parentAccountNames' (parentAccountName a) +-- | Is the first account a parent or other ancestor of (and not the same as) the second ? isAccountNamePrefixOf :: AccountName -> AccountName -> Bool isAccountNamePrefixOf = isPrefixOf . (++ [acctsepchar]) diff --git a/hledger-lib/Hledger/Data/Posting.hs b/hledger-lib/Hledger/Data/Posting.hs index 7b3c78a41..d599dd49d 100644 --- a/hledger-lib/Hledger/Data/Posting.hs +++ b/hledger-lib/Hledger/Data/Posting.hs @@ -226,7 +226,7 @@ accountNameApplyAliases aliases a = accountNameWithPostingType atype aname' where (aname,atype) = (accountNameWithoutPostingType a, accountNamePostingType a) aname' = foldl - (\acct alias -> dbg6 "got" $ aliasReplace (dbg6 "alias" alias) acct) + (\acct alias -> dbg6 "result" $ aliasReplace (dbg6 "alias" alias) (dbg6 "account" acct)) aname aliases @@ -235,8 +235,9 @@ accountNameApplyAliases aliases a = accountNameWithPostingType atype aname' -- aliasMatches (RegexAlias re _) a = regexMatchesCI re a aliasReplace :: AccountAlias -> AccountName -> AccountName -aliasReplace (BasicAlias old new) a | old `isAccountNamePrefixOf` a = new ++ drop (length old) a - | otherwise = a +aliasReplace (BasicAlias old new) a + | old `isAccountNamePrefixOf` a || old == a = new ++ drop (length old) a + | otherwise = a aliasReplace (RegexAlias re repl) a = regexReplaceCI re repl a diff --git a/tests/misc/aliases.test b/tests/misc/aliases.test index 67ea9f180..6e804d7d2 100644 --- a/tests/misc/aliases.test +++ b/tests/misc/aliases.test @@ -1,5 +1,7 @@ # alias-related tests +# simple aliases + # simple alias directive hledgerdev -f- accounts <<< @@ -23,6 +25,18 @@ A:B:c a:bb:d >>>=0 +# a simple alias matching the whole account name works +hledgerdev -f- accounts +<<< +alias a:b = A:B +1/1 + (a:b) 1 +>>> +A:B +>>>=0 + +# regular expression aliases + # regex alias directive hledgerdev -f- accounts <<< @@ -68,6 +82,8 @@ alias /A (.)/=\1 >>>=0 +# CLI + # --alias command-line options are applied in the order written. # Spaces are allowed if quoted. #