Skip to content

Commit

Permalink
feat: support member from constructor parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
yorkyao committed Nov 7, 2018
1 parent d4d5303 commit bf087cb
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
2 changes: 1 addition & 1 deletion demo/cases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ class ClassType1 {
}

class ClassType2 extends ClassType1 {
classMember3: string
constructor(public classMember3: string) { super() }
classMember4: number
}

Expand Down
2 changes: 1 addition & 1 deletion demo/debug.json
Original file line number Diff line number Diff line change
Expand Up @@ -2154,7 +2154,7 @@
"position": {
"file": "demo/cases.ts",
"line": 300,
"character": 16
"character": 35
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion online/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ class ClassType1 {
}
class ClassType2 extends ClassType1 {
classMember3: string
constructor(public classMember3: string) { super() }
classMember4: number
}
Expand Down
30 changes: 22 additions & 8 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ export class Parser {
declaration,
members,
minProperties,
maxProperties,
sourceFile
maxProperties
))
if (additionalPropertiesFromHeritageClauses) {
additionalProperties = additionalPropertiesFromHeritageClauses
Expand Down Expand Up @@ -166,8 +165,7 @@ export class Parser {
declaration: ts.InterfaceDeclaration | ts.ClassDeclaration,
members: Member[],
minProperties: number,
maxProperties: number,
sourceFile: ts.SourceFile
maxProperties: number
) {
let additionalProperties: Type | undefined | boolean
if (declaration.heritageClauses) {
Expand Down Expand Up @@ -800,6 +798,7 @@ export class Parser {
}
}

// tslint:disable-next-line:cognitive-complexity
private getObjectMembers(elements: ts.NodeArray<ts.TypeElement | ts.ClassElement>, sourceFile: ts.SourceFile): MembersInfo {
const members: Member[] = []
let minProperties = 0
Expand All @@ -808,7 +807,7 @@ export class Parser {
for (const element of elements) {
if (element.kind === ts.SyntaxKind.PropertySignature || element.kind === ts.SyntaxKind.PropertyDeclaration) {
const property = element as ts.PropertySignature | ts.PropertyDeclaration
const member = this.getObjectMemberOfPropertyOrMethod(property, sourceFile)
const member = this.getObjectMemberOfPropertyOrMethodOrConstructorParameter(property, sourceFile)
members.push(member)
if (!property.questionToken) {
minProperties++
Expand All @@ -821,8 +820,23 @@ export class Parser {
}
} else if (element.kind === ts.SyntaxKind.MethodSignature || element.kind === ts.SyntaxKind.MethodDeclaration) {
const methodSignature = element as ts.MethodSignature | ts.MethodDeclaration
const member = this.getObjectMemberOfPropertyOrMethod(methodSignature, sourceFile)
const member = this.getObjectMemberOfPropertyOrMethodOrConstructorParameter(methodSignature, sourceFile)
members.push(member)
} else if (element.kind === ts.SyntaxKind.Constructor) {
const constructorDeclaration = element as ts.ConstructorDeclaration
for (const parameter of constructorDeclaration.parameters) {
if (parameter.modifiers
&& parameter.modifiers.some((modifier) => modifier.kind === ts.SyntaxKind.PublicKeyword
|| modifier.kind === ts.SyntaxKind.PrivateKeyword
|| modifier.kind === ts.SyntaxKind.ProtectedKeyword)) {
const member = this.getObjectMemberOfPropertyOrMethodOrConstructorParameter(parameter, sourceFile)
members.push(member)
if (!parameter.questionToken) {
minProperties++
}
maxProperties++
}
}
}
}
return { members, minProperties, maxProperties, additionalProperties }
Expand Down Expand Up @@ -918,9 +932,9 @@ export class Parser {
}
}

private getObjectMemberOfPropertyOrMethod(
private getObjectMemberOfPropertyOrMethodOrConstructorParameter(
// tslint:disable-next-line:max-union-size
property: ts.PropertySignature | ts.PropertyDeclaration | ts.MethodSignature | ts.MethodDeclaration,
property: ts.PropertySignature | ts.PropertyDeclaration | ts.MethodSignature | ts.MethodDeclaration | ts.ParameterDeclaration,
sourceFile: ts.SourceFile
) {
const name = property.name as ts.Identifier
Expand Down

0 comments on commit bf087cb

Please sign in to comment.