-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathparse-utilities.ts
83 lines (76 loc) · 2.34 KB
/
parse-utilities.ts
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
76
77
78
79
80
81
82
83
import { getCombinedModifierFlags, ModifierFlags, Node, SyntaxKind, TypeNode } from 'typescript';
import { DeclarationVisibility } from '../declarations/DeclarationVisibility';
import { File } from '../resources/File';
import { Resource } from '../resources/Resource';
/**
* Checks if the given typescript node has the exported flag.
* (e.g. export class Foobar).
*
* @export
* @param {Node} node
* @returns {boolean}
*/
export function isNodeExported(node: Node): boolean {
const flags = getCombinedModifierFlags(node);
return (flags & ModifierFlags.Export) === ModifierFlags.Export;
}
/**
* Checks if the given typescript node has the default flag.
* (e.g. export default class Foobar).
*
* @export
* @param {Node} node
* @returns {boolean}
*/
export function isNodeDefaultExported(node: Node): boolean {
const flags = getCombinedModifierFlags(node);
return (flags & ModifierFlags.Default) === ModifierFlags.Default;
}
/**
* Returns the type text (type information) for a given node.
*
* @export
* @param {(TypeNode | undefined)} node
* @returns {(string | undefined)}
*/
export function getNodeType(node: TypeNode | undefined): string | undefined {
return node ? node.getText() : undefined;
}
/**
* Returns the enum value (visibility) of a node.
*
* @export
* @param {Node} node
* @returns {(DeclarationVisibility | undefined)}
*/
export function getNodeVisibility(node: Node): DeclarationVisibility | undefined {
if (!node.modifiers) {
return undefined;
}
for (const modifier of node.modifiers) {
switch (modifier.kind) {
case SyntaxKind.PublicKeyword:
return DeclarationVisibility.Public;
case SyntaxKind.ProtectedKeyword:
return DeclarationVisibility.Protected;
case SyntaxKind.PrivateKeyword:
return DeclarationVisibility.Private;
default:
break;
}
}
}
/**
* Function that calculates the default name of a resource.
* This is used when a default export has no name (i.e. export class {}).
*
* @export
* @param {TsResource} resource
* @returns {string}
*/
export function getDefaultResourceIdentifier(resource: Resource): string {
if (resource instanceof File && resource.isWorkspaceFile) {
return resource.parsedPath.name;
}
return resource.identifier;
}