Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lint against lone plural wildcard #159

Merged
merged 1 commit into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/Intlc/Linter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Prelude

data ExternalLint
= RedundantSelect (NonEmpty Text)
| RedundantPlural (NonEmpty Text)
deriving (Eq, Show)

data InternalLint
Expand Down Expand Up @@ -41,6 +42,7 @@ lintWith rules (Message stream) = toStatus $ rules `flap` stream
lintExternal :: Message -> Status ExternalLint
lintExternal = lintWith
[ redundantSelectRule
, redundantPluralRule
]

lintInternal :: Message -> Status InternalLint
Expand All @@ -58,6 +60,7 @@ lintDatasetWith linter fmt xs = pureIf (not $ M.null lints) msg
lintDatasetExternal :: Dataset Translation -> Maybe Text
lintDatasetExternal = lintDatasetWith lintExternal . formatFailureWith $ \case
RedundantSelect xs -> "Redundant select: " <> T.intercalate ", " (toList xs)
RedundantPlural xs -> "Redundant plural: " <> T.intercalate ", " (toList xs)

lintDatasetInternal :: Dataset Translation -> Maybe Text
lintDatasetInternal = lintDatasetWith lintInternal . formatFailureWith $ \case
Expand Down Expand Up @@ -86,6 +89,23 @@ redundantSelectRule = fmap RedundantSelect . nonEmpty . idents where
redundantIdent (Interpolation n (Select (That _w))) = Just n
redundantIdent _ = Nothing

-- Plural interpolations with only wildcards are redundant: they could be
-- replaced with plain number interpolations.
redundantPluralRule :: Rule ExternalLint
redundantPluralRule = fmap RedundantPlural . nonEmpty . idents where
idents :: Stream -> [Text]
idents [] = []
idents (x:xs) = mconcat
[ maybeToList (redundantIdent x)
, maybeToMonoid (idents <$> getStream x)
, idents xs
]
redundantIdent (Interpolation n (Plural p)) = case p of
CardinalInexact [] [] _ -> Just n
Ordinal [] [] _ -> Just n
_ -> Nothing
redundantIdent _ = Nothing

-- Our translation vendor has poor support for ICU syntax, and their parser
-- particularly struggles with interpolations. This rule limits the use of these
-- interpolations to one per message, with caveats (see below "complex").
Expand Down
27 changes: 27 additions & 0 deletions test/Intlc/LinterSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,33 @@ spec = describe "linter" $ do
lint (Message [Interpolation "x" (s [Interpolation "y" (s [])]), Interpolation "z" (s [])])
`shouldBe` Failure (pure $ RedundantSelect ("x" :| ["y", "z"]))

describe "redundant plural" $ do
let lint = lintWith' redundantPluralRule

it "succeeds on exact cardinal plural" $ do
lint (Message [Interpolation "x" (Plural $ CardinalExact (pure $ PluralCase (PluralExact "42") []))])
`shouldBe` Success

it "succeeds on ordinal plural with any non-wildcard case" $ do
lint (Message [Interpolation "x" (Plural $ Ordinal [PluralCase (PluralExact "42") []] [] (PluralWildcard []))])
`shouldBe` Success
lint (Message [Interpolation "x" (Plural $ Ordinal [] [PluralCase Two []] (PluralWildcard []))])
`shouldBe` Success

it "succeeds on inexact cardinal plural with any non-wildcard case" $ do
lint (Message [Interpolation "x" (Plural $ CardinalInexact [PluralCase (PluralExact "42") []] [] (PluralWildcard []))])
`shouldBe` Success
lint (Message [Interpolation "x" (Plural $ CardinalInexact [] [PluralCase Two []] (PluralWildcard []))])
`shouldBe` Success

it "fails on ordinal plural with only a wildcard" $ do
lint (Message [Interpolation "x" (Plural $ Ordinal [] [] (PluralWildcard []))])
`shouldBe` Failure (pure . RedundantPlural . pure $ "x")

it "fails on inexact cardinal plural with only a wildcard" $ do
lint (Message [Interpolation "x" (Plural $ CardinalInexact [] [] (PluralWildcard []))])
`shouldBe` Failure (pure . RedundantPlural . pure $ "x")

describe "internal" $ do
describe "unicode" $ do
let lint = lintWith' unsupportedUnicodeRule
Expand Down