1
1
mirror of https://github.com/github/semantic.git synced 2024-12-24 23:42:31 +03:00

Fix up TypeScript import-graph output and tests

This commit is contained in:
Timothy Clem 2018-03-08 11:37:41 -08:00
parent 8c8b2437ba
commit 74675d6731
4 changed files with 87 additions and 73 deletions

View File

@ -122,18 +122,29 @@ instance CustomHasDeclaration whole Declaration.Class where
= Just $ ClassDeclaration (getSource identifierAnn) (getClassSource blob (In ann decl)) blobLanguage
where getSource = toText . flip Source.slice blobSource . getField
instance CustomHasDeclaration (Union fs) Declaration.Import where
customToDeclaration Blob{..} _ (Declaration.Import (Term (In fromAnn _), _) symbols)
= Just $ ImportDeclaration ((stripQuotes . getSource) fromAnn) "" (fmap getSymbol symbols) blobLanguage
where
stripQuotes = T.dropAround (`elem` ['"', '\''])
getSource = toText . flip Source.slice blobSource . getField
getSymbol = let f = (T.decodeUtf8 . friendlyName) in bimap f f
instance CustomHasDeclaration (Union fs) Declaration.QualifiedImport where
customToDeclaration Blob{..} _ (Declaration.QualifiedImport (Term (In fromAnn _), _) (Term (In aliasAnn _), _) symbols)
= Just $ ImportDeclaration name (getAlias blobLanguage (getSource aliasAnn)) (fmap getSymbol symbols) blobLanguage
= Just $ ImportDeclaration ((stripQuotes . getSource) fromAnn) (getSource aliasAnn) (fmap getSymbol symbols) blobLanguage
where
name = getSource fromAnn
getAlias lang alias | Just TypeScript <- lang, T.null alias = basename name
| Just Go <- lang, T.null alias = basename name
| otherwise = alias
basename = last . T.splitOn "/"
getSource = T.dropAround (`elem` ['"', '\'']) . toText . flip Source.slice blobSource . getField
stripQuotes = T.dropAround (`elem` ['"', '\''])
getSource = toText . flip Source.slice blobSource . getField
getSymbol = let f = (T.decodeUtf8 . friendlyName) in bimap f f
instance CustomHasDeclaration (Union fs) Declaration.WildcardImport where
customToDeclaration Blob{..} _ (Declaration.WildcardImport (Term (In fromAnn _), _) _)
= Just $ ImportDeclaration ((stripQuotes . getSource) fromAnn) "" [] blobLanguage
where
stripQuotes = T.dropAround (`elem` ['"', '\''])
getSource = toText . flip Source.slice blobSource . getField
instance (Expression.MemberAccess :< fs) => CustomHasDeclaration (Union fs) Expression.Call where
customToDeclaration Blob{..} _ (Expression.Call _ (Term (In fromAnn fromF), _) _ _)
| Just (Expression.MemberAccess (Term (In leftAnn leftF)) (Term (In idenAnn _))) <- prj fromF = Just $ CallReference (getSource idenAnn) (memberAccess leftAnn leftF)
@ -168,7 +179,9 @@ class HasDeclarationWithStrategy (strategy :: Strategy) whole syntax where
type family DeclarationStrategy syntax where
DeclarationStrategy Declaration.Class = 'Custom
DeclarationStrategy Declaration.Function = 'Custom
DeclarationStrategy Declaration.Import = 'Custom
DeclarationStrategy Declaration.QualifiedImport = 'Custom
DeclarationStrategy Declaration.WildcardImport = 'Custom
DeclarationStrategy Declaration.Method = 'Custom
DeclarationStrategy Markdown.Heading = 'Custom
DeclarationStrategy Expression.Call = 'Custom

View File

@ -38,7 +38,7 @@ spec = parallel $ do
expected <- readFileVerbatim "test/fixtures/go/import-graph/main.json"
toVerbatimOutput output `shouldBe` expected
xit "works for TypeScript" $ do
it "works for TypeScript" $ do
output <- parseToImports typescriptParser "test/fixtures/typescript/import-graph/app.ts"
expected <- readFileVerbatim "test/fixtures/typescript/import-graph/app.json"
toVerbatimOutput output `shouldBe` expected

View File

@ -14,8 +14,13 @@
]
},
"path": "foo",
"symbols": [],
"alias": "foo"
"symbols": [
{
"alias": "defaultMember",
"name": "defaultMember"
}
],
"alias": ""
},
{
"span": {
@ -46,11 +51,11 @@
"path": "ant",
"symbols": [
{
"alias": "",
"alias": "member",
"name": "member"
}
],
"alias": "ant"
"alias": ""
},
{
"span": {
@ -66,31 +71,15 @@
"path": "antelope",
"symbols": [
{
"alias": "",
"alias": "member1",
"name": "member1"
}
],
"alias": "antelope"
},
{
"span": {
"start": [
4,
1
],
"end": [
4,
46
]
},
"path": "antelope",
"symbols": [
},
{
"alias": "",
"alias": "member2",
"name": "member2"
}
],
"alias": "antelope"
"alias": ""
},
{
"span": {
@ -106,46 +95,15 @@
"path": "ant-eater",
"symbols": [
{
"alias": "",
"alias": "member1",
"name": "member1"
}
],
"alias": "ant-eater"
},
{
"span": {
"start": [
5,
1
],
"end": [
5,
57
]
},
"path": "ant-eater",
"symbols": [
},
{
"alias": "alias2",
"name": "member2"
}
],
"alias": "ant-eater"
},
{
"span": {
"start": [
6,
1
],
"end": [
6,
70
]
},
"path": "anaconda",
"symbols": [],
"alias": "anaconda"
"alias": ""
},
{
"span": {
@ -161,11 +119,11 @@
"path": "anaconda",
"symbols": [
{
"alias": "",
"name": "member1"
"alias": "defaultMember",
"name": "defaultMember"
}
],
"alias": "anaconda"
"alias": ""
},
{
"span": {
@ -180,12 +138,16 @@
},
"path": "anaconda",
"symbols": [
{
"alias": "member1",
"name": "member1"
},
{
"alias": "alias2",
"name": "member2"
}
],
"alias": "anaconda"
"alias": ""
},
{
"span": {
@ -199,8 +161,13 @@
]
},
"path": "alligator",
"symbols": [],
"alias": "alligator"
"symbols": [
{
"alias": "defaultMember",
"name": "defaultMember"
}
],
"alias": ""
},
{
"span": {
@ -230,7 +197,7 @@
},
"path": "arctic-tern",
"symbols": [],
"alias": "arctic-tern"
"alias": ""
},
{
"span": {
@ -284,6 +251,36 @@
},
"symbol": "someFunction",
"targets": []
},
{
"span": {
"start": [
16,
1
],
"end": [
16,
16
]
},
"symbol": "defaultMember",
"targets": []
},
{
"span": {
"start": [
17,
1
],
"end": [
17,
11
]
},
"symbol": "foo",
"targets": [
"name"
]
}
]
}

View File

@ -11,3 +11,7 @@ import zip = require("../zip");
function someFunction(arg1, arg2): string { arg2; };
someFunction(arg1, "arg2");
defaultMember()
name.foo()