Skip to content

Commit

Permalink
feat: [ts] add basic field for interface support
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Jan 10, 2020
1 parent f5423a3 commit 052efb5
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 45 deletions.
4 changes: 2 additions & 2 deletions languages/g4/TypeScriptParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ typeMemberList
;

typeMember
: propertySignatur
: propertySignature
| callSignature
| constructSignature
| indexSignature
Expand Down Expand Up @@ -185,7 +185,7 @@ typeQueryExpression
| (identifierName '.')+ identifierName
;

propertySignatur
propertySignature
: ReadOnly? propertyName '?'? typeAnnotation? ('=>' type_)?
;

Expand Down
2 changes: 1 addition & 1 deletion languages/ts/TypeScriptParser.interp
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ functionType
constructorType
typeQuery
typeQueryExpression
propertySignatur
propertySignature
typeAnnotation
callSignature
parameterList
Expand Down
68 changes: 34 additions & 34 deletions languages/ts/typescript_parser.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions languages/ts/typescriptparser_base_listener.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions languages/ts/typescriptparser_listener.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions trial/pkg/application/ts/ts_ident_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,21 @@ interface IEmployee extends IPerson{
g.Expect(results[1].Extend).To(Equal("IPerson"))
}

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

app := new(TypeScriptApiApp)

results := app.Analysis(`
export interface IPerson {
name: string;
gender: string;
}
`)

g.Expect(len(results[0].Fields)).To(Equal(2))
}

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

Expand Down
25 changes: 25 additions & 0 deletions trial/pkg/ast/typescript_ident_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,31 @@ func (s *TypeScriptIdentListener) EnterInterfaceDeclaration(ctx *parser.Interfac
implements := BuildImplements(extendsContext.ClassOrInterfaceTypeList())
currentNode.Extend = implements[0]
}

objectTypeCtx := ctx.ObjectType().(*parser.ObjectTypeContext)
if objectTypeCtx.TypeBody() != nil {
typeMemberListCtx := objectTypeCtx.TypeBody().(*parser.TypeBodyContext).TypeMemberList().(*parser.TypeMemberListContext)
BuildInterfaceTypeBody(typeMemberListCtx, currentNode)
}
}

func BuildInterfaceTypeBody(ctx *parser.TypeMemberListContext, classNode *domain.JClassNode) {
for _, typeMember := range ctx.AllTypeMember() {
typeMemberContext := typeMember.(*parser.TypeMemberContext).GetChild(0)
currentType := reflect.TypeOf(typeMemberContext).String()
switch currentType {
case "*parser.PropertySignatureContext":
{
signatureCtx := typeMemberContext.(*parser.PropertySignatureContext)
field := &domain.JAppField{
Type: BuildAnnotationType(signatureCtx.TypeAnnotation().(*parser.TypeAnnotationContext)),
Value: signatureCtx.PropertyName().GetText(),
}

classNode.Fields = append(classNode.Fields, *field)
}
}
}
}

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

0 comments on commit 052efb5

Please sign in to comment.