Skip to content

Commit

Permalink
feat: [ts] add simple block import
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Jan 10, 2020
1 parent 18553ca commit 4c5ead7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pkg/domain/code_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type CodeMember struct {

type CodeFile struct {
FullName string
Imports string
Imports []string
Members []CodeMember
ClassNodes []JClassNode
}
14 changes: 14 additions & 0 deletions trial/pkg/application/ts/ts_ident_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,17 @@ func Test_ShouldGetClassFields(t *testing.T) {
g.Expect(len(fields)).To(Equal(5))
g.Expect(fields[0].Modifier).To(Equal("public"))
}

func Test_ShouldReturnImports(t *testing.T) {
g := NewGomegaWithT(t)

app := new(TypeScriptApiApp)

results := app.Analysis(`
import { ZipCodeValidator } from "./ZipCodeValidator";
`, "")

g.Expect(len(results.Imports)).To(Equal(1))
g.Expect(results.Imports[0]).To(Equal("./ZipCodeValidator"))
}
21 changes: 14 additions & 7 deletions trial/pkg/ast/typescript_ident_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
parser "github.com/phodal/coca/languages/ts"
"github.com/phodal/coca/pkg/domain"
"reflect"
"strings"
)

var currentNode *domain.JClassNode
Expand All @@ -13,6 +14,7 @@ var classNodes []domain.JClassNode

var defaultClass = "default"
var filePath string
var codeFile domain.CodeFile

type TypeScriptIdentListener struct {
parser.BaseTypeScriptParserListener
Expand All @@ -22,6 +24,11 @@ func NewTypeScriptIdentListener(fileName string) *TypeScriptIdentListener {
classNodes = nil
filePath = fileName
currentNode = domain.NewClassNode()
codeFile = domain.CodeFile{
FullName: filePath,
Imports: nil,
ClassNodes: nil,
}
return &TypeScriptIdentListener{}
}

Expand All @@ -32,16 +39,16 @@ func (s *TypeScriptIdentListener) GetNodeInfo() domain.CodeFile {
currentNode = domain.NewClassNode()
}

codeFile := &domain.CodeFile{
FullName: filePath,
Imports: "",
ClassNodes: classNodes,
}
return *codeFile
codeFile.ClassNodes = classNodes
return codeFile
}

func (s *TypeScriptIdentListener) EnterProgram(ctx *parser.ProgramContext) {
func (s *TypeScriptIdentListener) EnterFromBlock(ctx *parser.FromBlockContext) {
importText := ctx.StringLiteral().GetText()
replaceDoubleQuote := strings.ReplaceAll(importText, "\"", "")
replaceSingleQuote := strings.ReplaceAll(replaceDoubleQuote, "'", "")

codeFile.Imports = append(codeFile.Imports, replaceSingleQuote)
}

func (s *TypeScriptIdentListener) EnterInterfaceDeclaration(ctx *parser.InterfaceDeclarationContext) {
Expand Down

0 comments on commit 4c5ead7

Please sign in to comment.