-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add linter to check for gendered pronouns in comments
- Loading branch information
Showing
4 changed files
with
87 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
{-# LANGUAGE NamedFieldPuns #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE Strict #-} | ||
{-# LANGUAGE StrictData #-} | ||
module Tokstyle.Linter.Comments (analyse) where | ||
|
||
import Control.Monad (when) | ||
import Control.Monad.State.Strict (State) | ||
import qualified Control.Monad.State.Strict as State | ||
import Data.Fix (Fix (..)) | ||
import Data.Text (Text) | ||
import Language.Cimple (Lexeme (..), Node, CommentF (..), NodeF (..), lexemeText) | ||
import Language.Cimple.Diagnostics (Diagnostics, warn) | ||
import Language.Cimple.TraverseAst (AstActions, astActions, doComment, doNode, | ||
traverseAst) | ||
|
||
|
||
isGendered :: Text -> Bool | ||
isGendered = (`elem` | ||
[ "she" | ||
, "her" | ||
, "herself" | ||
, "he" | ||
, "him" | ||
, "his" | ||
, "himself" | ||
]) | ||
|
||
checkCommentLexeme :: FilePath -> Lexeme Text -> Diagnostics () | ||
checkCommentLexeme file w = | ||
when (isGendered $ lexemeText w) $ | ||
warn file w $ "inappropriately gendered pronoun: " <> lexemeText w | ||
|
||
linter :: AstActions (State [Text]) Text | ||
linter = astActions | ||
{ doComment = \file comment act -> | ||
case unFix comment of | ||
DocWord w -> checkCommentLexeme file w | ||
_ -> act | ||
, doNode = \file node act -> | ||
case unFix node of | ||
Comment _ _ ws _ -> mapM_ (checkCommentLexeme file) ws | ||
_ -> act | ||
|
||
} | ||
|
||
analyse :: (FilePath, [Node (Lexeme Text)]) -> [Text] | ||
analyse = reverse . flip State.execState [] . traverseAst linter |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
module Tokstyle.Linter.CommentsSpec where | ||
|
||
import Test.Hspec (Spec, it, shouldBe) | ||
|
||
import Tokstyle.Linter (allWarnings, analyse) | ||
import Tokstyle.LinterSpec (mustParse) | ||
|
||
|
||
spec :: Spec | ||
spec = do | ||
it "rejects gendered pronouns in plain comments" $ do | ||
ast <- mustParse | ||
[ "/* This is the peer himself. */" | ||
] | ||
analyse allWarnings ("test.c", ast) | ||
`shouldBe` | ||
[ "test.c:1: inappropriately gendered pronoun: himself [-Wcomments]" | ||
] | ||
it "rejects gendered pronouns in doxygen comments" $ do | ||
ast <- mustParse | ||
[ "/** @brief Checks peer goodness." | ||
, " *" | ||
, " * Checks a peer to see if she is good." | ||
, " */" | ||
] | ||
analyse allWarnings ("test.c", ast) | ||
`shouldBe` | ||
[ "test.c:3: inappropriately gendered pronoun: she [-Wcomments]" | ||
] | ||
it "accepts gendered pronouns in code" $ do | ||
ast <- mustParse | ||
[ "void f(int ge, int he) { return; }" | ||
] | ||
analyse allWarnings ("test.c", ast) `shouldBe` [] |
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