-
Notifications
You must be signed in to change notification settings - Fork 879
/
classes.ts
192 lines (182 loc) · 4.98 KB
/
classes.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
* @license
* Copyright 2022 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/
/**
* @fileoverview
*
* Utilities for analyzing class declarations
*/
import ts from 'typescript';
import {DiagnosticsError} from '../errors.js';
import {
ClassDeclaration,
AnalyzerInterface,
DeclarationInfo,
ClassHeritage,
Reference,
ClassField,
ClassMethod,
} from '../model.js';
import {
isLitElementSubclass,
getLitElementDeclaration,
} from '../lit-element/lit-element.js';
import {getReferenceForIdentifier} from '../references.js';
import {parseNodeJSDocInfo} from './jsdoc.js';
import {
hasDefaultModifier,
hasStaticModifier,
hasExportModifier,
getPrivacy,
} from '../utils.js';
import {getFunctionLikeInfo} from './functions.js';
import {getTypeForNode} from '../types.js';
/**
* Returns an analyzer `ClassDeclaration` model for the given
* ts.ClassDeclaration.
*/
const getClassDeclaration = (
declaration: ts.ClassDeclaration,
analyzer: AnalyzerInterface
) => {
if (isLitElementSubclass(declaration, analyzer)) {
return getLitElementDeclaration(declaration, analyzer);
}
return new ClassDeclaration({
// TODO(kschaaf): support anonymous class expressions when assigned to a const
name: declaration.name?.text ?? '',
node: declaration,
getHeritage: () => getHeritage(declaration, analyzer),
...parseNodeJSDocInfo(declaration),
...getClassMembers(declaration, analyzer),
});
};
/**
* Returns the `fields` and `methods` of a class.
*/
export const getClassMembers = (
declaration: ts.ClassDeclaration,
analyzer: AnalyzerInterface
) => {
const fieldMap = new Map<string, ClassField>();
const methodMap = new Map<string, ClassMethod>();
declaration.members.forEach((node) => {
if (ts.isMethodDeclaration(node)) {
methodMap.set(
node.name.getText(),
new ClassMethod({
...getMemberInfo(node),
...getFunctionLikeInfo(node, analyzer),
...parseNodeJSDocInfo(node),
})
);
} else if (ts.isPropertyDeclaration(node)) {
fieldMap.set(
node.name.getText(),
new ClassField({
...getMemberInfo(node),
default: node.initializer?.getText(),
type: getTypeForNode(node, analyzer),
...parseNodeJSDocInfo(node),
})
);
}
});
return {
fieldMap,
methodMap,
};
};
const getMemberInfo = (node: ts.MethodDeclaration | ts.PropertyDeclaration) => {
return {
name: node.name.getText(),
static: hasStaticModifier(node),
privacy: getPrivacy(node),
};
};
/**
* Returns the name of a class declaration.
*/
const getClassDeclarationName = (declaration: ts.ClassDeclaration) => {
const name =
declaration.name?.text ??
// The only time a class declaration will not have a name is when it is
// a default export, aka `export default class { }`
(hasDefaultModifier(declaration) ? 'default' : undefined);
if (name === undefined) {
throw new DiagnosticsError(
declaration,
'Unexpected class declaration without a name'
);
}
return name;
};
/**
* Returns name and model factory for a class declaration.
*/
export const getClassDeclarationInfo = (
declaration: ts.ClassDeclaration,
analyzer: AnalyzerInterface
): DeclarationInfo => {
return {
name: getClassDeclarationName(declaration),
factory: () => getClassDeclaration(declaration, analyzer),
isExport: hasExportModifier(declaration),
};
};
/**
* Returns the superClass and any applied mixins for a given class declaration.
*/
export const getHeritage = (
declaration: ts.ClassLikeDeclarationBase,
analyzer: AnalyzerInterface
): ClassHeritage => {
const extendsClause = declaration.heritageClauses?.find(
(c) => c.token === ts.SyntaxKind.ExtendsKeyword
);
if (extendsClause !== undefined) {
if (extendsClause.types.length !== 1) {
throw new DiagnosticsError(
extendsClause,
'Internal error: did not expect extends clause to have multiple types'
);
}
return getHeritageFromExpression(
extendsClause.types[0].expression,
analyzer
);
}
// No extends clause; return empty heritage
return {
mixins: [],
superClass: undefined,
};
};
export const getHeritageFromExpression = (
expression: ts.Expression,
analyzer: AnalyzerInterface
): ClassHeritage => {
// TODO(kschaaf): Support for extracting mixing applications from the heritage
// expression https://github.com/lit/lit/issues/2998
const mixins: Reference[] = [];
const superClass = getSuperClass(expression, analyzer);
return {
superClass,
mixins,
};
};
export const getSuperClass = (
expression: ts.Expression,
analyzer: AnalyzerInterface
): Reference => {
// TODO(kschaaf) Could add support for inline class expressions here as well
if (ts.isIdentifier(expression)) {
return getReferenceForIdentifier(expression, analyzer);
}
throw new DiagnosticsError(
expression,
`Expected expression to be a concrete superclass. Mixins are not yet supported.`
);
};