-
Notifications
You must be signed in to change notification settings - Fork 3
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
Maintenance: linting #123
Maintenance: linting #123
Conversation
lib/Intlc/Linter.hs
Outdated
countInterpolations :: NEStream -> Int | ||
countInterpolations = foldr go 0 | ||
where | ||
count = \case |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I count three pattern matches with case
that could be inlined into their functions. 😛 e.g.
-- before
f x = case x of y -> z
-- or
f = \case y -> z
-- after
f y = z
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've found these two helpful to change 5cc9666
I'm guessing the third one is count = \case
although if the equivalent would be
count String = 0
count Number = 0
count (Bool {}) = 1
Would this be very similar to using point-free case
here? Or perhaps there is another equivalent I'm not seeing 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep that's the one, and yeah it's very similar, just a case of common idioms. 🙂
Certainly! It'd be great to get output something like this:
This needs some research. At the moment our AST doesn't hold onto source information after parsing is complete. As with nicer parser errors this could come later, it's not a blocker. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nearly there, however I don't think this handles (pre-)nested interpolations yet? In the following f
will be correctly flagged however g
should be too and isn't:
{
"f": { "message": "{x, select, xa {}} {y, select, ya {}}" },
"g": { "message": "{x, select, xa {{y, select, ya {}}}}" }
}
I think this test catches it:
diff --git a/test/Intlc/LinterSpec.hs b/test/Intlc/LinterSpec.hs
index 6eb1541..f63276e 100644
--- a/test/Intlc/LinterSpec.hs
+++ b/test/Intlc/LinterSpec.hs
@@ -28,3 +28,5 @@ spec = describe "linter" $ do
it "does not lint dynamic streams with 2 or more complex interpolations" $ do
lint (Dynamic (fromList [Interpolation (Arg "Hello" (Callback [])), Interpolation (Arg "Hello" (Bool [] []))])) `shouldBe` Failure TooManyInterpolations
+ it "does not lint nested streams" $ do
+ lint (Dynamic (fromList [Interpolation (Arg "outer" (Callback [Interpolation (Arg "inner" (Callback []))]))])) `shouldBe` Failure TooManyInterpolations
As an aside because errors in Haskell are lazy you can cheaply validate that we stop iterating like so (could also use undefined
):
diff --git a/test/Intlc/LinterSpec.hs b/test/Intlc/LinterSpec.hs
index 6eb1541..9ba075f 100644
--- a/test/Intlc/LinterSpec.hs
+++ b/test/Intlc/LinterSpec.hs
@@ -28,3 +28,5 @@ spec = describe "linter" $ do
it "does not lint dynamic streams with 2 or more complex interpolations" $ do
lint (Dynamic (fromList [Interpolation (Arg "Hello" (Callback [])), Interpolation (Arg "Hello" (Bool [] []))])) `shouldBe` Failure TooManyInterpolations
+ it "stops iterating after second stream" $ do
+ lint (Dynamic (fromList [Interpolation (Arg "Hello" (Callback [])), Interpolation (Arg "Hello" (Bool [] [])), error "should not reach here"])) `shouldBe` Failure TooManyInterpolations
Hmm. Just so I understand, if something is nested that means it got flattened right? But we're going to lint against english translations and we don't commit flattened format in english files I think 🤔 |
They can show up prior to flattening, for example:
|
@samhh Wasn't easy to figure out! Mind taking a look at cd03f4e? I didn't carry on with pattern matching the |
I'd like a better way. This mimics the struggle of depply mapping in the Compiler module for flattening.
Pushed some updates. A lot of commits but @Magellol already did all the heavy lifting. 🙂 Summary:
|
One more thing we need is to add CI output for the "internal" binary. That can wait for another PR. |
Invoke the linting too through
intlc-internal
A "faulty" json file can be found here https://github.com/unsplash/unsplash-web/blob/master/app/routes/Photos/lang/en-US.translations.json
A good one here: https://github.com/unsplash/unsplash-web/blob/master/app/routes/Press/lang/en-US.translations.json
e.g
intlc-internal lint app/routes/Photos/lang/en-US.translations.json TooManyInterpolations # exit 1
intlc-internal lint app/routes/Press/lang/en-US.translations.json Success # exit 0
TODO
Failure msg
Nice to have