-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathinterface-parser.ts
More file actions
75 lines (69 loc) · 2.91 KB
/
interface-parser.ts
File metadata and controls
75 lines (69 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { Identifier, InterfaceDeclaration, SyntaxKind } from 'typescript';
import { DeclarationVisibility } from '../declarations/DeclarationVisibility';
import { DefaultDeclaration } from '../declarations/DefaultDeclaration';
import { InterfaceDeclaration as TshInterface } from '../declarations/InterfaceDeclaration';
import { MethodDeclaration } from '../declarations/MethodDeclaration';
import { PropertyDeclaration } from '../declarations/PropertyDeclaration';
import { Resource } from '../resources/Resource';
import { isMethodSignature, isPropertySignature } from '../type-guards/TypescriptGuards';
import { parseMethodParams } from './function-parser';
import {
containsModifier,
getDefaultResourceIdentifier,
getNodeType,
isNodeDefaultExported,
isNodeExported,
} from './parse-utilities';
/**
* Parses an interface node into its declaration.
* Calculates the property and method defintions of the interface as well.
*
* @export
* @param {Resource} resource
* @param {InterfaceDeclaration} node
*/
export function parseInterface(resource: Resource, node: InterfaceDeclaration): void {
const name = node.name ? node.name.text : getDefaultResourceIdentifier(resource);
const interfaceDeclaration = new TshInterface(
name, isNodeExported(node), node.getStart(), node.getEnd(),
);
if (isNodeDefaultExported(node)) {
interfaceDeclaration.isExported = false;
resource.declarations.push(new DefaultDeclaration(interfaceDeclaration.name, resource));
}
if (node.members) {
node.members.forEach((o) => {
if (isPropertySignature(o)) {
interfaceDeclaration.properties.push(
new PropertyDeclaration(
(o.name as Identifier).text,
DeclarationVisibility.Public,
getNodeType(o.type),
!!o.questionToken,
containsModifier(o, SyntaxKind.StaticKeyword),
o.getStart(),
o.getEnd(),
),
);
} else if (isMethodSignature(o)) {
const method = new MethodDeclaration(
(o.name as Identifier).text,
true,
DeclarationVisibility.Public,
getNodeType(o.type),
!!o.questionToken,
containsModifier(o, SyntaxKind.StaticKeyword),
containsModifier(o, SyntaxKind.AsyncKeyword),
o.getStart(),
o.getEnd(),
);
method.parameters = parseMethodParams(o);
interfaceDeclaration.methods.push(method);
}
});
}
if (node.typeParameters) {
interfaceDeclaration.typeParameters = node.typeParameters.map(param => param.getText());
}
resource.declarations.push(interfaceDeclaration);
}