fcis 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.plans/001-fcis-analyzer.md +832 -0
- package/.plans/002-fcis-analyzer-improvements.md +205 -0
- package/README.md +272 -0
- package/TECHNICAL.md +386 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +1836 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.d.ts +709 -0
- package/dist/index.js +1845 -0
- package/dist/index.js.map +1 -0
- package/package.json +47 -0
- package/pnpm-workspace.yaml +0 -0
- package/src/analyzer.ts +266 -0
- package/src/classification/classifier.ts +156 -0
- package/src/classification/derive-status.ts +171 -0
- package/src/classification/quality-scorer.ts +481 -0
- package/src/cli.ts +286 -0
- package/src/detection/detect-markers.ts +480 -0
- package/src/detection/markers.ts +332 -0
- package/src/extraction/extract-functions.ts +570 -0
- package/src/extraction/extractor.ts +188 -0
- package/src/index.ts +111 -0
- package/src/reporting/report-console.ts +416 -0
- package/src/reporting/report-json.ts +232 -0
- package/src/scoring/scorer.ts +504 -0
- package/src/types.ts +248 -0
- package/tests/classifier.test.ts +480 -0
- package/tests/derive-status.test.ts +464 -0
- package/tests/detect-markers.test.ts +639 -0
- package/tests/extractor.test.ts +155 -0
- package/tests/integration.test.ts +706 -0
- package/tests/quality-scorer.test.ts +650 -0
- package/tests/scorer.test.ts +768 -0
- package/tsconfig.json +34 -0
- package/tsup.config.ts +17 -0
- package/vendor/ts-morph/.editorconfig +10 -0
- package/vendor/ts-morph/.gitattributes +11 -0
- package/vendor/ts-morph/.github/CODE_OF_CONDUCT.md +77 -0
- package/vendor/ts-morph/.github/ISSUE_TEMPLATE/bug_report.md +29 -0
- package/vendor/ts-morph/.github/ISSUE_TEMPLATE/custom.md +4 -0
- package/vendor/ts-morph/.github/ISSUE_TEMPLATE/feature_request.md +18 -0
- package/vendor/ts-morph/.github/workflows/ci.yml +50 -0
- package/vendor/ts-morph/.github/workflows/publish.yml +53 -0
- package/vendor/ts-morph/.vscode/settings.json +10 -0
- package/vendor/ts-morph/CONTRIBUTING.md +23 -0
- package/vendor/ts-morph/DEVELOPMENT.md +32 -0
- package/vendor/ts-morph/LICENSE +21 -0
- package/vendor/ts-morph/deno.json +8 -0
- package/vendor/ts-morph/deno.lock +1233 -0
- package/vendor/ts-morph/docs/CNAME +1 -0
- package/vendor/ts-morph/docs/Gemfile +2 -0
- package/vendor/ts-morph/docs/_config.yml +5 -0
- package/vendor/ts-morph/docs/_layouts/default.html +159 -0
- package/vendor/ts-morph/docs/_script-templates/main.ts +116 -0
- package/vendor/ts-morph/docs/assets/css/style.scss +212 -0
- package/vendor/ts-morph/docs/details/ambient.md +38 -0
- package/vendor/ts-morph/docs/details/async.md +31 -0
- package/vendor/ts-morph/docs/details/classes.md +314 -0
- package/vendor/ts-morph/docs/details/comment-ranges.md +7 -0
- package/vendor/ts-morph/docs/details/comments.md +122 -0
- package/vendor/ts-morph/docs/details/decorators.md +119 -0
- package/vendor/ts-morph/docs/details/documentation.md +73 -0
- package/vendor/ts-morph/docs/details/enums.md +117 -0
- package/vendor/ts-morph/docs/details/exports.md +308 -0
- package/vendor/ts-morph/docs/details/expressions.md +46 -0
- package/vendor/ts-morph/docs/details/functions.md +150 -0
- package/vendor/ts-morph/docs/details/generators.md +27 -0
- package/vendor/ts-morph/docs/details/identifiers.md +79 -0
- package/vendor/ts-morph/docs/details/imports.md +191 -0
- package/vendor/ts-morph/docs/details/index.md +52 -0
- package/vendor/ts-morph/docs/details/initializers.md +40 -0
- package/vendor/ts-morph/docs/details/interfaces.md +218 -0
- package/vendor/ts-morph/docs/details/literals.md +20 -0
- package/vendor/ts-morph/docs/details/modifiers.md +38 -0
- package/vendor/ts-morph/docs/details/modules.md +113 -0
- package/vendor/ts-morph/docs/details/namespaces.md +7 -0
- package/vendor/ts-morph/docs/details/object-literal-expressions.md +106 -0
- package/vendor/ts-morph/docs/details/parameters.md +64 -0
- package/vendor/ts-morph/docs/details/signatures.md +41 -0
- package/vendor/ts-morph/docs/details/source-files.md +292 -0
- package/vendor/ts-morph/docs/details/type-aliases.md +34 -0
- package/vendor/ts-morph/docs/details/type-parameters.md +72 -0
- package/vendor/ts-morph/docs/details/types.md +254 -0
- package/vendor/ts-morph/docs/details/variables.md +110 -0
- package/vendor/ts-morph/docs/emitting.md +151 -0
- package/vendor/ts-morph/docs/index.md +25 -0
- package/vendor/ts-morph/docs/manipulation/code-writer.md +20 -0
- package/vendor/ts-morph/docs/manipulation/formatting.md +76 -0
- package/vendor/ts-morph/docs/manipulation/index.md +136 -0
- package/vendor/ts-morph/docs/manipulation/order.md +14 -0
- package/vendor/ts-morph/docs/manipulation/performance.md +222 -0
- package/vendor/ts-morph/docs/manipulation/removing.md +31 -0
- package/vendor/ts-morph/docs/manipulation/renaming.md +106 -0
- package/vendor/ts-morph/docs/manipulation/settings.md +76 -0
- package/vendor/ts-morph/docs/manipulation/structures.md +117 -0
- package/vendor/ts-morph/docs/manipulation/transforms.md +84 -0
- package/vendor/ts-morph/docs/metrics/performance.json +4 -0
- package/vendor/ts-morph/docs/navigation/ambient-modules.md +22 -0
- package/vendor/ts-morph/docs/navigation/compiler-nodes.md +82 -0
- package/vendor/ts-morph/docs/navigation/directories.md +287 -0
- package/vendor/ts-morph/docs/navigation/example.md +50 -0
- package/vendor/ts-morph/docs/navigation/finding-references.md +53 -0
- package/vendor/ts-morph/docs/navigation/getting-source-files.md +59 -0
- package/vendor/ts-morph/docs/navigation/images/getChildrenVsForEachChild.gif +0 -0
- package/vendor/ts-morph/docs/navigation/index.md +94 -0
- package/vendor/ts-morph/docs/navigation/language-service.md +23 -0
- package/vendor/ts-morph/docs/navigation/program.md +25 -0
- package/vendor/ts-morph/docs/navigation/type-checker.md +33 -0
- package/vendor/ts-morph/docs/setup/adding-source-files.md +145 -0
- package/vendor/ts-morph/docs/setup/ast-viewers.md +46 -0
- package/vendor/ts-morph/docs/setup/diagnostics.md +109 -0
- package/vendor/ts-morph/docs/setup/file-system.md +106 -0
- package/vendor/ts-morph/docs/setup/images/atom-ast.png +0 -0
- package/vendor/ts-morph/docs/setup/images/atom-ast_small.png +0 -0
- package/vendor/ts-morph/docs/setup/images/atom-command-palette.png +0 -0
- package/vendor/ts-morph/docs/setup/images/atom-file.png +0 -0
- package/vendor/ts-morph/docs/setup/images/ts-ast-viewer.png +0 -0
- package/vendor/ts-morph/docs/setup/index.md +94 -0
- package/vendor/ts-morph/docs/utilities.md +55 -0
- package/vendor/ts-morph/dprint.json +23 -0
- package/vendor/ts-morph/package.json +30 -0
- package/vendor/ts-morph/packages/bootstrap/LICENSE +21 -0
- package/vendor/ts-morph/packages/bootstrap/lib/ts-morph-bootstrap.d.ts +397 -0
- package/vendor/ts-morph/packages/bootstrap/package.json +46 -0
- package/vendor/ts-morph/packages/bootstrap/readme.md +200 -0
- package/vendor/ts-morph/packages/common/LICENSE +21 -0
- package/vendor/ts-morph/packages/common/lib/ts-morph-common.d.ts +1082 -0
- package/vendor/ts-morph/packages/common/lib/typescript.d.ts +11439 -0
- package/vendor/ts-morph/packages/common/package.json +65 -0
- package/vendor/ts-morph/packages/common/readme.md +5 -0
- package/vendor/ts-morph/packages/scripts/changeTypeScriptVersion.ts +28 -0
- package/vendor/ts-morph/packages/scripts/createDeclarationProject.ts +47 -0
- package/vendor/ts-morph/packages/scripts/deps.ts +2 -0
- package/vendor/ts-morph/packages/scripts/execScript.ts +31 -0
- package/vendor/ts-morph/packages/scripts/folders.ts +11 -0
- package/vendor/ts-morph/packages/scripts/getDevCompilerVersions.ts +19 -0
- package/vendor/ts-morph/packages/scripts/mod.ts +7 -0
- package/vendor/ts-morph/packages/scripts/utils/Memoize.ts +36 -0
- package/vendor/ts-morph/packages/scripts/utils/forEachTypeText.ts +23 -0
- package/vendor/ts-morph/packages/scripts/utils/makeConstructorsPrivate.ts +26 -0
- package/vendor/ts-morph/packages/scripts/utils/mod.ts +4 -0
- package/vendor/ts-morph/packages/scripts/utils/printDiagnostics.ts +10 -0
- package/vendor/ts-morph/packages/ts-morph/LICENSE +21 -0
- package/vendor/ts-morph/packages/ts-morph/lib/ts-morph.d.ts +11198 -0
- package/vendor/ts-morph/packages/ts-morph/package.json +78 -0
- package/vendor/ts-morph/packages/ts-morph/readme.md +111 -0
- package/vendor/ts-morph/readme.md +14 -0
- package/vendor/ts-morph/rfcs/README.md +13 -0
- package/vendor/ts-morph/rfcs/RFC-0001 - Inserting Into Statements Handling Comments.md +181 -0
- package/vendor/ts-morph/tsconfig.common.json +17 -0
- package/vitest.config.ts +16 -0
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Types
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
## Types
|
|
6
|
+
|
|
7
|
+
Types are accessed by calling `.getType()` on nodes that are typed. For example:
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
const type = parameter.getType();
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
There are other ways for accessing a type. For example:
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
const returnType = functionDeclaration.getReturnType();
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
### Checking type is assignable to another type
|
|
20
|
+
|
|
21
|
+
Note: Requires ts-morph 22+
|
|
22
|
+
|
|
23
|
+
To check if a type is assignable to another type, use the `isAssignableTo` method:
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
if (stringLitType.isAssignableTo(stringType)) {
|
|
27
|
+
// ...
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### Compiler Type
|
|
32
|
+
|
|
33
|
+
The underlying compiler type can be accessed via:
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
const compilerType = type.compilerType;
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Apparent type
|
|
40
|
+
|
|
41
|
+
Given the following variable declaration:
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
const myVar = 4;
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
The type is `4` and the apparent type is `Number`.
|
|
48
|
+
|
|
49
|
+
Retrieve the apparent type via the following:
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
const apparentType = type.getApparentType();
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Text
|
|
56
|
+
|
|
57
|
+
Getting the type text can be achieved by calling `.getText()`:
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
const text = type.getText();
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Sometimes this may not be good enough. If not, try to provide the enclosing node:
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
const text = type.getText(parameter);
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Format it by providing `TypeFormatFlags`:
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
const text = type.getText(parameter, TypeFormatFlags.NoTruncation | TypeFormatFlags.WriteArrayAsGenericType);
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Look at the declaration file for more available options for `TypeFormatFlags`.
|
|
76
|
+
|
|
77
|
+
### Constraint and Default
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
const constraintType = type.getConstraint();
|
|
81
|
+
const defaultType = type.getDefault();
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### Intersection types
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
const intersectionTypes = type.getIntersectionTypes();
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Union types
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
const unionTypes = type.getUnionTypes();
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Properties
|
|
97
|
+
|
|
98
|
+
Get the properties or property of a type:
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
const properties = type.getProperties();
|
|
102
|
+
const prop1 = type.getProperty("prop1");
|
|
103
|
+
const prop2 = type.getProperty(p => p.getName() === "prop2");
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Or the apparent properties:
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
const apparentProperties = type.getApparentProperties();
|
|
110
|
+
const prop1 = type.getApparentProperty("prop1");
|
|
111
|
+
const prop2 = type.getApparentProperty(p => p.getName() === "prop2");
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### Base types
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
const baseTypes = type.getBaseTypes();
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Base type of a literal type
|
|
121
|
+
|
|
122
|
+
```ts setup: let numberLiteralType: Type;
|
|
123
|
+
const numberType = numberLiteralType.getBaseTypeOfLiteralType();
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### Call signatures
|
|
127
|
+
|
|
128
|
+
```ts
|
|
129
|
+
const callSignatures = type.getCallSignatures();
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### Construct signatures
|
|
133
|
+
|
|
134
|
+
Get the construct signatures (new signatures) of a type:
|
|
135
|
+
|
|
136
|
+
```ts
|
|
137
|
+
const constructSignatures = type.getConstructSignatures();
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### Index types
|
|
141
|
+
|
|
142
|
+
Get either the string index type (ex. for `{ [index: string]: Date; }` it would be `Date`)
|
|
143
|
+
or the number index type (ex. for `{ [index: number]: object; }` it would be `object`):
|
|
144
|
+
|
|
145
|
+
```ts
|
|
146
|
+
const stringIndexType = type.getStringIndexType();
|
|
147
|
+
const numberIndexType = type.getNumberIndexType();
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### Tuple element types
|
|
151
|
+
|
|
152
|
+
```ts
|
|
153
|
+
const tupleElements = type.getTupleElements();
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
For example, for the type `[string, number]`, the above would return an array containing the type for `string` and `number`.
|
|
157
|
+
|
|
158
|
+
### Non-nullable type
|
|
159
|
+
|
|
160
|
+
Gets the non-nullable type from a nullable type:
|
|
161
|
+
|
|
162
|
+
```ts
|
|
163
|
+
const nonNullableType = type.getNonNullableType();
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
For example, `string | undefined` would return `string`.
|
|
167
|
+
|
|
168
|
+
### Type flags
|
|
169
|
+
|
|
170
|
+
This has information about the type, such as `TypeFlags.BooleanLike`.
|
|
171
|
+
|
|
172
|
+
```ts
|
|
173
|
+
const flags = type.getFlags();
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
Generally a method that starts with "is" exists on the type and you can easily use that instead of checking the flags (same with Object flags below).
|
|
177
|
+
|
|
178
|
+
### Object flags
|
|
179
|
+
|
|
180
|
+
This has information about object types, such as `ObjectFlags.Mapped`.
|
|
181
|
+
|
|
182
|
+
```ts
|
|
183
|
+
const objectFlags = type.getObjectFlags();
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
### Symbol
|
|
187
|
+
|
|
188
|
+
Get the symbol of the type if it exists:
|
|
189
|
+
|
|
190
|
+
```ts
|
|
191
|
+
const typeSymbol = type.getSymbol();
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### Alias symbol
|
|
195
|
+
|
|
196
|
+
```ts
|
|
197
|
+
const aliasSymbol = type.getAliasSymbol();
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### Alias type arguments
|
|
201
|
+
|
|
202
|
+
```ts
|
|
203
|
+
const aliasTypeArgs = type.getAliasTypeArguments();
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
### Telling type
|
|
207
|
+
|
|
208
|
+
Use any of the following methods:
|
|
209
|
+
|
|
210
|
+
```ts
|
|
211
|
+
type.isAnonymous();
|
|
212
|
+
type.isAny();
|
|
213
|
+
type.isArray();
|
|
214
|
+
type.isBoolean();
|
|
215
|
+
type.isBooleanLiteral();
|
|
216
|
+
type.isClass();
|
|
217
|
+
type.isClassOrInterface();
|
|
218
|
+
type.isEnum();
|
|
219
|
+
type.isEnumLiteral();
|
|
220
|
+
type.isInterface();
|
|
221
|
+
type.isIntersection();
|
|
222
|
+
type.isLiteral();
|
|
223
|
+
type.isNull();
|
|
224
|
+
type.isNumber();
|
|
225
|
+
type.isNumberLiteral();
|
|
226
|
+
type.isObject();
|
|
227
|
+
type.isString();
|
|
228
|
+
type.isStringLiteral();
|
|
229
|
+
type.isTemplateLiteral();
|
|
230
|
+
type.isTuple();
|
|
231
|
+
type.isUndefined();
|
|
232
|
+
type.isUnion();
|
|
233
|
+
type.isUnionOrIntersection();
|
|
234
|
+
type.isUnknown();
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
If you see something that doesn't exist here and should (there's a lot missing), then please log an issue or submit a pull request.
|
|
238
|
+
|
|
239
|
+
### Removing a Type
|
|
240
|
+
|
|
241
|
+
Remove a type or a return type from a node:
|
|
242
|
+
|
|
243
|
+
```ts
|
|
244
|
+
propertyDeclaration.removeType();
|
|
245
|
+
functionDeclaration.removeReturnType();
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
### TODO
|
|
249
|
+
|
|
250
|
+
Not implemented. Getting...
|
|
251
|
+
|
|
252
|
+
- Enum member types
|
|
253
|
+
- Destructuring pattern
|
|
254
|
+
- More...?
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Variables
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
## Variable Statement
|
|
6
|
+
|
|
7
|
+
A variable statement looks like the following:
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
export const var1 = "5", var2 = "6";
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
### Get a variable statement
|
|
14
|
+
|
|
15
|
+
Can be retrieved from source files, namespaces, or function bodies:
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
const variableStatements = sourceFile.getVariableStatements();
|
|
19
|
+
const firstExportedVariableStatement = sourceFile.getVariableStatement(s => s.hasExportKeyword());
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
### Add/Insert
|
|
23
|
+
|
|
24
|
+
Add or insert variable statements to a source file, namespace, or function like declarations by calling `addVariableStatement()`, `addVariableStatements()`,
|
|
25
|
+
`insertVariableStatement()`, or `insertVariableStatements()`.
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { Project, VariableDeclarationKind } from "ts-morph";
|
|
29
|
+
|
|
30
|
+
const variableStatement = sourceFile.addVariableStatement({
|
|
31
|
+
declarationKind: VariableDeclarationKind.Const, // defaults to "let"
|
|
32
|
+
declarations: [{
|
|
33
|
+
name: "myNumber",
|
|
34
|
+
initializer: "5",
|
|
35
|
+
}, {
|
|
36
|
+
name: "myString",
|
|
37
|
+
type: "string",
|
|
38
|
+
initializer: `'my string'`,
|
|
39
|
+
}],
|
|
40
|
+
});
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Remove
|
|
44
|
+
|
|
45
|
+
Call `.remove()`:
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
variableStatement.remove();
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Declaration type
|
|
52
|
+
|
|
53
|
+
Get:
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
const declarationKind = variableStatement.getDeclarationKind();
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
It will return one of the following values:
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
import { VariableDeclarationKind } from "ts-morph";
|
|
63
|
+
|
|
64
|
+
VariableDeclarationKind.Let;
|
|
65
|
+
VariableDeclarationKind.Const;
|
|
66
|
+
VariableDeclarationKind.Var;
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Set:
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
variableStatement.setDeclarationKind(VariableDeclarationKind.Const);
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Variable Declaration
|
|
76
|
+
|
|
77
|
+
These are the individual declarations within a variable statement.
|
|
78
|
+
|
|
79
|
+
### Get variable declaration
|
|
80
|
+
|
|
81
|
+
Get them from the variable statement:
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
const variableDeclarations = variableStatement.getDeclarations();
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Or from source files, namespaces, and function bodies:
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
const variableDeclarations = sourceFile.getVariableDeclarations();
|
|
91
|
+
const variableDeclaration = sourceFile.getVariableDeclaration("myVar");
|
|
92
|
+
const firstStringTypedVariableDeclaration = sourceFile.getVariableDeclaration(v => v.getType().getText() === "string");
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Add/Insert
|
|
96
|
+
|
|
97
|
+
Add or insert variable declarations to a variable statement by calling `addDeclaration()`, `addDeclarations()`,
|
|
98
|
+
`insertDeclaration()`, or `insertDeclarations()`.
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
const declaration = variableStatement.addDeclaration({ name: "num", type: "number" });
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Remove
|
|
105
|
+
|
|
106
|
+
Call `.remove()`:
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
variableDeclaration.remove();
|
|
110
|
+
```
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Emitting
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
## Emitting
|
|
6
|
+
|
|
7
|
+
Emitting is is the process of taking the original TypeScript files and outputting them as JavaScript (`.js`) and/or declaration files (`.d.ts`).
|
|
8
|
+
|
|
9
|
+
Here's an example:
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
const project = new Project({ compilerOptions: { outDir: "dist", declaration: true } });
|
|
13
|
+
project.createSourceFile("MyFile.ts", "const num = 1;");
|
|
14
|
+
project.emit(); // async
|
|
15
|
+
|
|
16
|
+
// or
|
|
17
|
+
project.emitSync(); // slow
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
This outputs two files in the `dist` folder:
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
// MyFile.js
|
|
24
|
+
var num = 1;
|
|
25
|
+
|
|
26
|
+
// MyFile.d.ts
|
|
27
|
+
declare const num = 1;
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### Emitting a single file
|
|
31
|
+
|
|
32
|
+
Call `.emit()` on the source file:
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
const sourceFile = project.getSourceFileOrThrow("MyFile.ts");
|
|
36
|
+
sourceFile.emit(); // async, fast
|
|
37
|
+
|
|
38
|
+
// or
|
|
39
|
+
sourceFile.emitSync(); // slow
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Or get its emit output:
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
const emitOutput = sourceFile.getEmitOutput();
|
|
46
|
+
emitOutput.getEmitSkipped(); // returns: boolean
|
|
47
|
+
for (const outputFile of emitOutput.getOutputFiles()) {
|
|
48
|
+
outputFile.getFilePath();
|
|
49
|
+
outputFile.getWriteByteOrderMark();
|
|
50
|
+
outputFile.getText();
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Emitting only declaration files (.d.ts)
|
|
55
|
+
|
|
56
|
+
Specify the `emitOnlyDtsFiles` flag:
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
project.emit({ emitOnlyDtsFiles: true });
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Emit Diagnostics
|
|
63
|
+
|
|
64
|
+
Diagnostics about the emit can be found on the result:
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
const emitResult = await project.emit();
|
|
68
|
+
for (const diagnostic of emitResult.getDiagnostics())
|
|
69
|
+
console.log(diagnostic.getMessageText());
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
These are good to always check when emitting to ensure everything went smoothly. They will explain why files aren't being emitted.
|
|
73
|
+
|
|
74
|
+
### Emitting with custom transformations
|
|
75
|
+
|
|
76
|
+
You can emit using the compiler API's custom transformations by specifying them on the `customTransformers` option.
|
|
77
|
+
|
|
78
|
+
The following example will emit the code with all numeric literals change to string literals:
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
project.emit({
|
|
82
|
+
customTransformers: {
|
|
83
|
+
// optional transformers to evaluate before built in .js transformations
|
|
84
|
+
before: [context => sourceFile => visitSourceFile(sourceFile, context, numericLiteralToStringLiteral)],
|
|
85
|
+
// optional transformers to evaluate after built in .js transformations
|
|
86
|
+
after: [],
|
|
87
|
+
// optional transformers to evaluate after built in .d.ts transformations
|
|
88
|
+
afterDeclarations: [],
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
function visitSourceFile(
|
|
93
|
+
sourceFile: ts.SourceFile,
|
|
94
|
+
context: ts.TransformationContext,
|
|
95
|
+
visitNode: (node: ts.Node, context: ts.TransformationContext) => ts.Node,
|
|
96
|
+
) {
|
|
97
|
+
return visitNodeAndChildren(sourceFile) as ts.SourceFile;
|
|
98
|
+
|
|
99
|
+
function visitNodeAndChildren(node: ts.Node): ts.Node {
|
|
100
|
+
return ts.visitEachChild(visitNode(node, context), visitNodeAndChildren, context);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function numericLiteralToStringLiteral(node: ts.Node, context: ts.TransformationContext) {
|
|
105
|
+
if (ts.isNumericLiteral(node))
|
|
106
|
+
return context.factory.createStringLiteral(node.text);
|
|
107
|
+
return node;
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## Emitting to Memory
|
|
112
|
+
|
|
113
|
+
If you don't want to emit to the file system, you can call `emitToMemory()`:
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
const project = new Project({ compilerOptions: { outDir: "dist" } });
|
|
117
|
+
project.createSourceFile("MyFile.ts", "const num = 1;");
|
|
118
|
+
const result = project.emitToMemory();
|
|
119
|
+
|
|
120
|
+
// output the emitted files to the console
|
|
121
|
+
for (const file of result.getFiles()) {
|
|
122
|
+
console.log("----");
|
|
123
|
+
console.log(file.filePath);
|
|
124
|
+
console.log("----");
|
|
125
|
+
console.log(file.text);
|
|
126
|
+
console.log("\n");
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// or finally save this result to the underlying file system (or use `saveFilesSync()`)
|
|
130
|
+
result.saveFiles().then(() => console.log("written"));
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
To manipulate after emitting, you may load the result into a new project and manipulate that:
|
|
134
|
+
|
|
135
|
+
```ts
|
|
136
|
+
const project = new Project({ compilerOptions: { outDir: "dist" } });
|
|
137
|
+
project.createSourceFile("MyFile.ts", "const num = 1;");
|
|
138
|
+
const result = project.emitToMemory();
|
|
139
|
+
|
|
140
|
+
// load the javascript files into a new project
|
|
141
|
+
const newProject = new Project();
|
|
142
|
+
for (const file of result.getFiles())
|
|
143
|
+
newProject.createSourceFile(file.filePath, file.text, { overwrite: true });
|
|
144
|
+
|
|
145
|
+
// ...manipulate the javascript files here...
|
|
146
|
+
|
|
147
|
+
// save the new files to the file system
|
|
148
|
+
await newProject.save();
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
...but consider using the custom transformers discussed above if you want it to be faster.
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Documentation
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
## Purpose
|
|
6
|
+
|
|
7
|
+
Setup, navigation, and manipulation of the TypeScript AST can be a challenge. This library wraps the TypeScript compiler API so it's simple.
|
|
8
|
+
|
|
9
|
+
## Installing
|
|
10
|
+
|
|
11
|
+
Install as usual via npm:
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
npm install --save-dev ts-morph
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Or if you're using Deno and want to install via JSR:
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
deno add ts-morph@jsr:@ts-morph/ts-morph
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Documentation Progress
|
|
24
|
+
|
|
25
|
+
I've been still slowly updating the documentation. I'm keeping it up to date with any new features, but some existing features don't have documentation.
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Code Writer
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
## Code Writer
|
|
6
|
+
|
|
7
|
+
With manipulation methods that accept a `string` for the new code, it's possible to write text using a provided [code-block-writer](https://github.com/dsherret/code-block-writer).
|
|
8
|
+
|
|
9
|
+
Using the writer is very useful because it will write code out using the indentation and newline settings of the project. It's also easier to use.
|
|
10
|
+
|
|
11
|
+
### Example
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
functionDeclaration.setBodyText(writer =>
|
|
15
|
+
writer.writeLine("let myNumber = 5;")
|
|
16
|
+
.write("if (myNumber === 5)").block(() => {
|
|
17
|
+
writer.writeLine("console.log('yes')");
|
|
18
|
+
})
|
|
19
|
+
);
|
|
20
|
+
```
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Formatting
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
## Formatting
|
|
6
|
+
|
|
7
|
+
Sometimes you might encounter code that looks terrible. For example:
|
|
8
|
+
|
|
9
|
+
<!-- dprint-ignore -->
|
|
10
|
+
```ts
|
|
11
|
+
// BadlyFormattedFile.ts
|
|
12
|
+
var myVariable : string | number;
|
|
13
|
+
function myFunction(param : string){
|
|
14
|
+
return "";
|
|
15
|
+
}
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Automatically format the text of this file by calling format text on it:
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
sourceFile.formatText();
|
|
22
|
+
// or provide optional formatting settings
|
|
23
|
+
sourceFile.formatText({
|
|
24
|
+
placeOpenBraceOnNewLineForFunctions: true,
|
|
25
|
+
});
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
This will run the source file's text through the TypeScript compiler's formatting API, which will change the source file to contain the following text:
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
// BadlyFormattedFile.ts (not anymore!)
|
|
32
|
+
var myVariable: string | number;
|
|
33
|
+
function myFunction(param: string) {
|
|
34
|
+
return "";
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Individual Nodes
|
|
39
|
+
|
|
40
|
+
Individual nodes can also be formatted. For example, say you have a file that looks like this:
|
|
41
|
+
|
|
42
|
+
<!-- dprint-ignore -->
|
|
43
|
+
```ts
|
|
44
|
+
// file.ts
|
|
45
|
+
export class MyClass {
|
|
46
|
+
prop ! : string ;
|
|
47
|
+
|
|
48
|
+
myMethod( example: string ) {
|
|
49
|
+
console.log( example );
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
You can select down to the specific node you want to format:
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
project.getSourceFileOrThrow("file.ts")
|
|
58
|
+
.getClassOrThrow("MyClass")
|
|
59
|
+
.getInstanceMethodOrThrow("myMethod")
|
|
60
|
+
.getStatements()[0]
|
|
61
|
+
.formatText();
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Which would selectively only format the first statement in the method:
|
|
65
|
+
|
|
66
|
+
<!-- dprint-ignore -->
|
|
67
|
+
```ts
|
|
68
|
+
// file.ts
|
|
69
|
+
export class MyClass {
|
|
70
|
+
prop ! : string ;
|
|
71
|
+
|
|
72
|
+
myMethod( example: string ) {
|
|
73
|
+
console.log(example);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
```
|