-
Notifications
You must be signed in to change notification settings - Fork 189
/
HeritageClauseableNode.ts
40 lines (35 loc) · 1.82 KB
/
HeritageClauseableNode.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
import { errors, getSyntaxKindName, SyntaxKind, ts } from "@ts-morph/common";
import { Constructor } from "../../../types";
import { Node } from "../common";
import { HeritageClause } from "../general/HeritageClause";
export type HeritageClauseableNodeExtensionType = Node<ts.Node & { heritageClauses?: ts.NodeArray<ts.HeritageClause>; }>;
export interface HeritageClauseableNode {
/**
* Gets the heritage clauses of the node.
*/
getHeritageClauses(): HeritageClause[];
/**
* Gets the heritage clause by kind.
* @kind - Kind of heritage clause.
*/
getHeritageClauseByKind(kind: SyntaxKind.ExtendsKeyword | SyntaxKind.ImplementsKeyword): HeritageClause | undefined;
/**
* Gets the heritage clause by kind or throws if it doesn't exist.
* @kind - Kind of heritage clause.
*/
getHeritageClauseByKindOrThrow(kind: SyntaxKind.ExtendsKeyword | SyntaxKind.ImplementsKeyword): HeritageClause;
}
export function HeritageClauseableNode<T extends Constructor<HeritageClauseableNodeExtensionType>>(Base: T): Constructor<HeritageClauseableNode> & T {
return class extends Base implements HeritageClauseableNode {
getHeritageClauses(): HeritageClause[] {
const heritageClauses = this.compilerNode.heritageClauses;
return heritageClauses?.map(c => this._getNodeFromCompilerNode(c)) ?? [];
}
getHeritageClauseByKindOrThrow(kind: SyntaxKind.ExtendsKeyword | SyntaxKind.ImplementsKeyword) {
return errors.throwIfNullOrUndefined(this.getHeritageClauseByKind(kind), `Expected to have heritage clause of kind ${getSyntaxKindName(kind)}.`);
}
getHeritageClauseByKind(kind: SyntaxKind.ExtendsKeyword | SyntaxKind.ImplementsKeyword) {
return this.getHeritageClauses().find(c => c.compilerNode.token === kind);
}
};
}