1
1
mirror of https://github.com/github/semantic.git synced 2024-11-28 10:15:55 +03:00

Merge pull request #1377 from github/🔥-identity-renderers

🔥 identity renderers
This commit is contained in:
Rob Rix 2017-10-02 14:30:59 -04:00 committed by GitHub
commit 08aa166838
5 changed files with 11 additions and 37 deletions

View File

@ -19,18 +19,13 @@ module Renderer
import Data.Aeson (Value) import Data.Aeson (Value)
import Data.ByteString (ByteString) import Data.ByteString (ByteString)
import Data.Diff
import qualified Data.Map as Map import qualified Data.Map as Map
import Data.Output import Data.Output
import Data.Record
import Data.Term
import Data.Text (Text) import Data.Text (Text)
import Info (DefaultFields)
import Renderer.JSON as R import Renderer.JSON as R
import Renderer.Patch as R import Renderer.Patch as R
import Renderer.SExpression as R import Renderer.SExpression as R
import Renderer.TOC as R import Renderer.TOC as R
import Syntax as S
-- | Specification of renderers for diffs, producing output in the parameter type. -- | Specification of renderers for diffs, producing output in the parameter type.
data DiffRenderer output where data DiffRenderer output where
@ -44,8 +39,6 @@ data DiffRenderer output where
JSONDiffRenderer :: DiffRenderer (Map.Map Text Value) JSONDiffRenderer :: DiffRenderer (Map.Map Text Value)
-- | Render to a 'ByteString' formatted as nested s-expressions with patches indicated. -- | Render to a 'ByteString' formatted as nested s-expressions with patches indicated.
SExpressionDiffRenderer :: DiffRenderer ByteString SExpressionDiffRenderer :: DiffRenderer ByteString
-- | “Render” by returning the computed 'Diff'. This renderer is not surfaced in the command-line interface, and is intended strictly for tests. Further, as it cannot render à la carte terms, it should be regarded as a (very) short-term hack until such time as we have a better idea for TOCSpec.hs.
IdentityDiffRenderer :: DiffRenderer (Maybe (Diff Syntax (Record (Maybe Declaration ': DefaultFields)) (Record (Maybe Declaration ': DefaultFields))))
deriving instance Eq (DiffRenderer output) deriving instance Eq (DiffRenderer output)
deriving instance Show (DiffRenderer output) deriving instance Show (DiffRenderer output)
@ -58,8 +51,6 @@ data TermRenderer output where
JSONTermRenderer :: TermRenderer [Value] JSONTermRenderer :: TermRenderer [Value]
-- | Render to a 'ByteString' formatted as nested s-expressions. -- | Render to a 'ByteString' formatted as nested s-expressions.
SExpressionTermRenderer :: TermRenderer ByteString SExpressionTermRenderer :: TermRenderer ByteString
-- | “Render” by returning the computed 'Term'. This renderer is not surfaced in the command-line interface, and is intended strictly for tests. Further, as it cannot render à la carte terms, it should be regarded as a (very) short-term hack until such time as we have a better idea for SemanticSpec.hs.
IdentityTermRenderer :: TermRenderer (Maybe (Term Syntax (Record DefaultFields)))
deriving instance Eq (TermRenderer output) deriving instance Eq (TermRenderer output)
deriving instance Show (TermRenderer output) deriving instance Show (TermRenderer output)

View File

@ -59,11 +59,6 @@ parseBlob renderer blob@Blob{..} = case (renderer, blobLanguage) of
(SExpressionTermRenderer, Just Language.TypeScript) -> parse typescriptParser blob >>= decorate constructorLabel >>= render renderSExpressionTerm . fmap keepConstructorLabel (SExpressionTermRenderer, Just Language.TypeScript) -> parse typescriptParser blob >>= decorate constructorLabel >>= render renderSExpressionTerm . fmap keepConstructorLabel
(SExpressionTermRenderer, Just Language.Ruby) -> parse rubyParser blob >>= decorate constructorLabel >>= render renderSExpressionTerm . fmap keepConstructorLabel (SExpressionTermRenderer, Just Language.Ruby) -> parse rubyParser blob >>= decorate constructorLabel >>= render renderSExpressionTerm . fmap keepConstructorLabel
(SExpressionTermRenderer, _) | Just syntaxParser <- syntaxParser -> parse syntaxParser blob >>= render renderSExpressionTerm . fmap keepCategory (SExpressionTermRenderer, _) | Just syntaxParser <- syntaxParser -> parse syntaxParser blob >>= render renderSExpressionTerm . fmap keepCategory
(IdentityTermRenderer, Just Language.JSON) -> pure Nothing
(IdentityTermRenderer, Just Language.Markdown) -> pure Nothing
(IdentityTermRenderer, Just Language.Python) -> pure Nothing
(IdentityTermRenderer, Just Language.TypeScript) -> pure Nothing
(IdentityTermRenderer, _) | Just syntaxParser <- syntaxParser -> Just <$> parse syntaxParser blob
_ -> throwError (SomeException (NoParserForLanguage blobPath blobLanguage)) _ -> throwError (SomeException (NoParserForLanguage blobPath blobLanguage))
where syntaxParser = blobLanguage >>= parserForLanguage where syntaxParser = blobLanguage >>= parserForLanguage
@ -105,7 +100,6 @@ diffBlobPair renderer blobs = case (renderer, effectiveLanguage) of
(SExpressionDiffRenderer, Just Language.Ruby) -> run (decorate constructorLabel <=< parse rubyParser) diffTerms (renderSExpressionDiff . bimap keepConstructorLabel keepConstructorLabel) (SExpressionDiffRenderer, Just Language.Ruby) -> run (decorate constructorLabel <=< parse rubyParser) diffTerms (renderSExpressionDiff . bimap keepConstructorLabel keepConstructorLabel)
(SExpressionDiffRenderer, Just Language.TypeScript) -> run (decorate constructorLabel <=< parse typescriptParser) diffTerms (renderSExpressionDiff . bimap keepConstructorLabel keepConstructorLabel) (SExpressionDiffRenderer, Just Language.TypeScript) -> run (decorate constructorLabel <=< parse typescriptParser) diffTerms (renderSExpressionDiff . bimap keepConstructorLabel keepConstructorLabel)
(SExpressionDiffRenderer, _) | Just syntaxParser <- syntaxParser -> run (parse syntaxParser) diffSyntaxTerms (renderSExpressionDiff . bimap keepCategory keepCategory) (SExpressionDiffRenderer, _) | Just syntaxParser <- syntaxParser -> run (parse syntaxParser) diffSyntaxTerms (renderSExpressionDiff . bimap keepCategory keepCategory)
(IdentityDiffRenderer, _) | Just syntaxParser <- syntaxParser -> run (\ blob -> parse syntaxParser blob >>= decorate (syntaxDeclarationAlgebra blob)) diffSyntaxTerms Just
_ -> throwError (SomeException (NoParserForLanguage effectivePath effectiveLanguage)) _ -> throwError (SomeException (NoParserForLanguage effectivePath effectiveLanguage))
where (effectivePath, effectiveLanguage) = case runJoin blobs of where (effectivePath, effectiveLanguage) = case runJoin blobs of
(Blob { blobLanguage = Just lang, blobPath = path }, _) -> (path, Just lang) (Blob { blobLanguage = Just lang, blobPath = path }, _) -> (path, Just lang)

View File

@ -49,10 +49,3 @@ diffWithParser :: (HasField fields Data.Span.Span,
diffWithParser parser = run (\ blob -> parse parser blob >>= decorate (declarationAlgebra blob)) diffWithParser parser = run (\ blob -> parse parser blob >>= decorate (declarationAlgebra blob))
where where
run parse sourceBlobs = distributeFor sourceBlobs parse >>= runBothWith (diffTermPair sourceBlobs diffTerms) run parse sourceBlobs = distributeFor sourceBlobs parse >>= runBothWith (diffTermPair sourceBlobs diffTerms)
-- diffRecursively :: (Declaration.Method :< fs, Declaration.Function :< fs, Apply Eq1 fs, Apply GAlign fs, Apply Show1 fs, Apply Foldable fs, Apply Functor fs, Apply Traversable fs, Apply Diffable fs)
-- => Term (Union fs) (Record fields1)
-- -> Term (Union fs) (Record fields2)
-- -> Diff (Union fs) (Record fields1) (Record fields2)
-- diffRecursively = decoratingWith constructorNameAndConstantFields constructorNameAndConstantFields (diffTermsWith algorithmForTerms comparableByConstructor equivalentTerms)

View File

@ -18,10 +18,6 @@ import Test.Hspec.Expectations.Pretty
spec :: Spec spec :: Spec
spec = parallel $ do spec = parallel $ do
describe "parseBlob" $ do describe "parseBlob" $ do
it "parses in the specified language" $ do
Just term <- runTask $ parseBlob IdentityTermRenderer methodsBlob
void term `shouldBe` Term (() `In` Indexed [ Term (() `In` Method [] (Term (() `In` Leaf "foo")) Nothing [] []) ])
it "throws if not given a language" $ do it "throws if not given a language" $ do
runTask (parseBlob SExpressionTermRenderer methodsBlob { blobLanguage = Nothing }) `shouldThrow` (\ code -> case code of runTask (parseBlob SExpressionTermRenderer methodsBlob { blobLanguage = Nothing }) `shouldThrow` (\ code -> case code of
ExitFailure 1 -> True ExitFailure 1 -> True

View File

@ -2,7 +2,7 @@
{-# LANGUAGE DataKinds, TypeOperators #-} {-# LANGUAGE DataKinds, TypeOperators #-}
module TOCSpec where module TOCSpec where
import Category as C import Category as C hiding (Go)
import Data.Aeson import Data.Aeson
import Data.Bifunctor import Data.Bifunctor
import Data.Blob import Data.Blob
@ -20,7 +20,7 @@ import Data.Source
import Data.Term import Data.Term
import Data.Text (Text) import Data.Text (Text)
import Data.These import Data.These
import Info import Info hiding (Go)
import Interpreter import Interpreter
import Language import Language
import Prelude hiding (readFile) import Prelude hiding (readFile)
@ -31,7 +31,7 @@ import Semantic
import Semantic.Task import Semantic.Task
import Semantic.Util import Semantic.Util
import SpecHelpers import SpecHelpers
import Syntax as S import Syntax as S hiding (Go)
import Test.Hspec (Spec, describe, it, parallel) import Test.Hspec (Spec, describe, it, parallel)
import Test.Hspec.Expectations.Pretty import Test.Hspec.Expectations.Pretty
import Test.Hspec.LeanCheck import Test.Hspec.LeanCheck
@ -69,11 +69,10 @@ spec = parallel $ do
it "summarizes changed methods" $ do it "summarizes changed methods" $ do
sourceBlobs <- blobsForPaths (both "ruby/methods.A.rb" "ruby/methods.B.rb") sourceBlobs <- blobsForPaths (both "ruby/methods.A.rb" "ruby/methods.B.rb")
Just diff <- runTask (diffBlobPair IdentityDiffRenderer sourceBlobs) diff <- runTask $ diffWithParser rubyParser sourceBlobs
diffTOC diff `shouldBe` diffTOC diff `shouldBe`
[ JSONSummary "Method" "self.foo" (sourceSpanBetween (1, 1) (2, 4)) "added" [ JSONSummary "Method" "self.foo" (sourceSpanBetween (1, 1) (2, 4)) "modified"
, JSONSummary "Method" "bar" (sourceSpanBetween (4, 1) (6, 4)) "modified" , JSONSummary "Method" "bar" (sourceSpanBetween (4, 1) (6, 4)) "modified" ]
, JSONSummary "Method" "baz" (sourceSpanBetween (4, 1) (5, 4)) "removed" ]
it "dedupes changes in same parent method" $ do it "dedupes changes in same parent method" $ do
sourceBlobs <- blobsForPaths (both "javascript/duplicate-parent.A.js" "javascript/duplicate-parent.B.js") sourceBlobs <- blobsForPaths (both "javascript/duplicate-parent.A.js" "javascript/duplicate-parent.B.js")
@ -89,25 +88,26 @@ spec = parallel $ do
it "summarizes Go methods with receivers with special formatting" $ do it "summarizes Go methods with receivers with special formatting" $ do
sourceBlobs <- blobsForPaths (both "go/method-with-receiver.A.go" "go/method-with-receiver.B.go") sourceBlobs <- blobsForPaths (both "go/method-with-receiver.A.go" "go/method-with-receiver.B.go")
Just diff <- runTask (diffBlobPair IdentityDiffRenderer sourceBlobs) let Just goParser = parserForLanguage Go
diff <- runTask $ distributeFor sourceBlobs (\ blob -> parse goParser blob >>= decorate (syntaxDeclarationAlgebra blob)) >>= runBothWith (diffTermPair sourceBlobs diffSyntaxTerms)
diffTOC diff `shouldBe` diffTOC diff `shouldBe`
[ JSONSummary "Method" "(*apiClient) CheckAuth" (sourceSpanBetween (3,1) (3,101)) "added" ] [ JSONSummary "Method" "(*apiClient) CheckAuth" (sourceSpanBetween (3,1) (3,101)) "added" ]
it "summarizes Ruby methods that start with two identifiers" $ do it "summarizes Ruby methods that start with two identifiers" $ do
sourceBlobs <- blobsForPaths (both "ruby/method-starts-with-two-identifiers.A.rb" "ruby/method-starts-with-two-identifiers.B.rb") sourceBlobs <- blobsForPaths (both "ruby/method-starts-with-two-identifiers.A.rb" "ruby/method-starts-with-two-identifiers.B.rb")
Just diff <- runTask (diffBlobPair IdentityDiffRenderer sourceBlobs) diff <- runTask $ diffWithParser rubyParser sourceBlobs
diffTOC diff `shouldBe` diffTOC diff `shouldBe`
[ JSONSummary "Method" "foo" (sourceSpanBetween (1, 1) (4, 4)) "modified" ] [ JSONSummary "Method" "foo" (sourceSpanBetween (1, 1) (4, 4)) "modified" ]
it "handles unicode characters in file" $ do it "handles unicode characters in file" $ do
sourceBlobs <- blobsForPaths (both "ruby/unicode.A.rb" "ruby/unicode.B.rb") sourceBlobs <- blobsForPaths (both "ruby/unicode.A.rb" "ruby/unicode.B.rb")
Just diff <- runTask (diffBlobPair IdentityDiffRenderer sourceBlobs) diff <- runTask $ diffWithParser rubyParser sourceBlobs
diffTOC diff `shouldBe` diffTOC diff `shouldBe`
[ JSONSummary "Method" "foo" (sourceSpanBetween (6, 1) (7, 4)) "added" ] [ JSONSummary "Method" "foo" (sourceSpanBetween (6, 1) (7, 4)) "added" ]
it "properly slices source blob that starts with a newline and has multi-byte chars" $ do it "properly slices source blob that starts with a newline and has multi-byte chars" $ do
sourceBlobs <- blobsForPaths (both "javascript/starts-with-newline.js" "javascript/starts-with-newline.js") sourceBlobs <- blobsForPaths (both "javascript/starts-with-newline.js" "javascript/starts-with-newline.js")
Just diff <- runTask (diffBlobPair IdentityDiffRenderer sourceBlobs) diff <- runTask $ diffWithParser rubyParser sourceBlobs
diffTOC diff `shouldBe` [] diffTOC diff `shouldBe` []
prop "inserts of methods and functions are summarized" $ prop "inserts of methods and functions are summarized" $