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,117 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Enums
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
## Enums
|
|
6
|
+
|
|
7
|
+
Enums can be retrieved from source files, namespaces, or function bodies:
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
const enums = sourceFile.getEnums();
|
|
11
|
+
const enum1 = sourceFile.getEnum("Enum1");
|
|
12
|
+
const enum2 = sourceFile.getEnum(e => e.getMembers().length === 5);
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
### Add/Insert
|
|
16
|
+
|
|
17
|
+
Add or insert enums to a source file, namespace, or function like declarations by calling `addEnum()`, `addEnums()`, `insertEnum()`, or `insertEnums()`.
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
const enumDeclaration = sourceFile.addEnum({
|
|
21
|
+
name: "EnumName",
|
|
22
|
+
members: [{
|
|
23
|
+
name: "member",
|
|
24
|
+
}],
|
|
25
|
+
});
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### Remove
|
|
29
|
+
|
|
30
|
+
Call `.remove()`:
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
enumDeclaration.remove();
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### Get all members
|
|
37
|
+
|
|
38
|
+
Use `getMembers()`:
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
const members = enumDeclaration.getMembers();
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Get member
|
|
45
|
+
|
|
46
|
+
Use `getMember()`:
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
const member1 = enumDeclaration.getMember("member1");
|
|
50
|
+
const member2 = enumDeclaration.getMember(m => m.getValue() === 1);
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Add/Insert member
|
|
54
|
+
|
|
55
|
+
Members can be added by calling `addMember()`, `addMembers()`, `insertMember()`, or `insertMembers()`:
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
const member = enumDeclaration.addMember({
|
|
59
|
+
name: "newMember",
|
|
60
|
+
value: 10,
|
|
61
|
+
});
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Const enum
|
|
65
|
+
|
|
66
|
+
Check if it's a const enum via `isConstEnum()`:
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
enumDeclaration.isConstEnum(); // returns: boolean
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Get the `const` keyword via `getConstKeyword()`:
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
enumDeclaration.getConstKeyword(); // returns: Node | undefined
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Set if it's a const enum via `setIsConstEnum(value)`:
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
enumDeclaration.setIsConstEnum(true);
|
|
82
|
+
enumDeclaration.setIsConstEnum(false);
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Enum Members
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
const member = enumDeclaration.getMember("member");
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Value
|
|
92
|
+
|
|
93
|
+
The value can be retrieved whether it is implicitly or explicitly defined:
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
enumMember.getValue(); // returns: string | number
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
It can also be set to a number:
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
enumMember.setValue(5);
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Or a string:
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
enumMember.setValue("string value");
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### Removing
|
|
112
|
+
|
|
113
|
+
Call `remove()` on it:
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
enumMember.remove();
|
|
117
|
+
```
|
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Export
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
## Export
|
|
6
|
+
|
|
7
|
+
Certain nodes in TypeScript can be [exported](https://www.typescriptlang.org/docs/handbook/modules.html).
|
|
8
|
+
|
|
9
|
+
### Is export
|
|
10
|
+
|
|
11
|
+
Use `isExported()`, `isNamedExport()`, or `isDefaultExport()` methods:
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
functionDeclaration.isExported(); // returns: boolean
|
|
15
|
+
functionDeclaration.isNamedExport(); // returns: boolean
|
|
16
|
+
functionDeclaration.isDefaultExport(); // returns: boolean
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
### `export` and `default` keyword
|
|
20
|
+
|
|
21
|
+
Use the `has` methods to check for the `export` and `default` keywords:
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
functionDeclaration.hasExportKeyword(); // returns: boolean
|
|
25
|
+
functionDeclaration.hasDefaultKeyword(); // returns: boolean
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
And use the `get` methods to get the keywords:
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
functionDeclaration.getExportKeyword();
|
|
32
|
+
functionDeclaration.getDefaultKeyword();
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### Setting as default export
|
|
36
|
+
|
|
37
|
+
Use `setIsDefaultExport` to set a node as being a default export or not:
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
functionDeclaration.setIsDefaultExport(true); // be one
|
|
41
|
+
functionDeclaration.setIsDefaultExport(false); // don't be one
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Note: This will throw an exception if the node's parent is not a source file.
|
|
45
|
+
|
|
46
|
+
### Setting as export
|
|
47
|
+
|
|
48
|
+
Use `setIsExported` to set a node as being a named export if the parent is a source file or an export of a namespace if the parent is a namespace:
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
functionDeclaration.setIsExported(true); // be one
|
|
52
|
+
functionDeclaration.setIsExported(false); // don't be one
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Get default export symbol
|
|
56
|
+
|
|
57
|
+
If it exists, the default export symbol can be retrieved from source file or module:
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
const defaultExportSymbol = sourceFile.getDefaultExportSymbol(); // returns: Symbol | undefined
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Remove default export
|
|
64
|
+
|
|
65
|
+
Use:
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
sourceFile.removeDefaultExport();
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Note: This is safe to call even when there is no default export.
|
|
72
|
+
|
|
73
|
+
### Getting Exported Declarations
|
|
74
|
+
|
|
75
|
+
The exported declarations of a file or module can be retrieved via `.getExportedDeclarations()`. This will return a map keyed on the export name with a value of the exported declarations for that name.
|
|
76
|
+
|
|
77
|
+
For example, given the following setup:
|
|
78
|
+
|
|
79
|
+
<!-- dprint-ignore -->
|
|
80
|
+
```ts
|
|
81
|
+
// main.ts
|
|
82
|
+
export * from "./classes";
|
|
83
|
+
export { Interface1 as AliasedInterface } from "./interfaces";
|
|
84
|
+
|
|
85
|
+
namespace MergedNamespace { let t; }
|
|
86
|
+
namespace MergedNamespace { let u; }
|
|
87
|
+
|
|
88
|
+
export { MergedNamespace };
|
|
89
|
+
|
|
90
|
+
export default 5;
|
|
91
|
+
|
|
92
|
+
// classes.ts
|
|
93
|
+
export * from "./Class1";
|
|
94
|
+
export * from "./Class2";
|
|
95
|
+
|
|
96
|
+
// Class1.ts
|
|
97
|
+
export class Class1 {}
|
|
98
|
+
|
|
99
|
+
// Class2.ts
|
|
100
|
+
export class Class2 {}
|
|
101
|
+
|
|
102
|
+
// interfaces.ts
|
|
103
|
+
export interface Interface1 {}
|
|
104
|
+
export interface Interface2 {}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
The following code:
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
import { ExportedDeclarations, Project } from "ts-morph";
|
|
111
|
+
|
|
112
|
+
const project = new Project();
|
|
113
|
+
project.addSourceFilesAtPaths("**/*.ts");
|
|
114
|
+
const mainFile = project.getSourceFileOrThrow("main.ts");
|
|
115
|
+
|
|
116
|
+
for (const [name, declarations] of mainFile.getExportedDeclarations())
|
|
117
|
+
console.log(`${name}: ${declarations.map(d => d.getText()).join(", ")}`);
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
Outputs the following:
|
|
121
|
+
|
|
122
|
+
```
|
|
123
|
+
Class1: export class Class1 {}
|
|
124
|
+
Class2: export class Class2 {}
|
|
125
|
+
AliasedInterface: export interface Interface1 {}
|
|
126
|
+
MergedNamespace: namespace MergedNamespace { let t; }, namespace MergedNamespace { let u; }
|
|
127
|
+
default: 5
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Export Declarations
|
|
131
|
+
|
|
132
|
+
Export declarations look like this:
|
|
133
|
+
|
|
134
|
+
```ts setup: class OtherClass {}
|
|
135
|
+
export { MyClass } from "./other-file";
|
|
136
|
+
export * from "./some-file";
|
|
137
|
+
export { OtherClass };
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
Get the export declarations by calling:
|
|
141
|
+
|
|
142
|
+
```ts
|
|
143
|
+
const exportDeclarations = sourceFile.getExportDeclarations();
|
|
144
|
+
// or to get the first one that matches a condition
|
|
145
|
+
const exportDeclaration = sourceFile.getExportDeclaration(d => d.hasNamedExports());
|
|
146
|
+
const exportDecForModule = sourceFile.getExportDeclaration("module-specifier-text");
|
|
147
|
+
|
|
148
|
+
// tell if it has named exports
|
|
149
|
+
exportDeclaration.hasNamedExports();
|
|
150
|
+
// or if it's a namespace export
|
|
151
|
+
exportDeclaration.isNamespaceExport();
|
|
152
|
+
// get/set the module specifier
|
|
153
|
+
exportDeclaration.getModuleSpecifier(); // returns: StringLiteral | undefined
|
|
154
|
+
exportDeclaration.getModuleSpecifierValue(); // returns: string | undefined
|
|
155
|
+
exportDeclaration.setModuleSpecifier("./new-file");
|
|
156
|
+
exportDeclaration.setModuleSpecifier(sourceFile);
|
|
157
|
+
exportDeclaration.removeModuleSpecifier();
|
|
158
|
+
exportDeclaration.hasModuleSpecifier(); // returns: boolean
|
|
159
|
+
exportDeclaration.isModuleSpecifierRelative(); // if the module specifier starts with ./ or ../
|
|
160
|
+
exportDeclaration.getModuleSpecifierSourceFile(); // returns: SourceFile | undefined
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### Add/Insert
|
|
164
|
+
|
|
165
|
+
Add or insert use `insertExportDeclaration`, `insertExportDeclarations`, `addExportDeclaration`, or `addExportDeclarations`:
|
|
166
|
+
|
|
167
|
+
```ts
|
|
168
|
+
const exportDeclaration = sourceFile.addExportDeclaration({
|
|
169
|
+
namedExports: ["MyClass"],
|
|
170
|
+
moduleSpecifier: "./file",
|
|
171
|
+
});
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### Remove
|
|
175
|
+
|
|
176
|
+
Call `.remove()`:
|
|
177
|
+
|
|
178
|
+
```ts
|
|
179
|
+
exportDeclaration.remove();
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
### To Namespace Export
|
|
183
|
+
|
|
184
|
+
Given an export declaration with named exports:
|
|
185
|
+
|
|
186
|
+
```ts
|
|
187
|
+
export { Export1, Export2, Export3 } from "./other-file";
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
Calling `exportDeclaration.toNamespaceExport();` will change the code to the following:
|
|
191
|
+
|
|
192
|
+
```ts
|
|
193
|
+
export * from "./other-file";
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
### Named Exports
|
|
197
|
+
|
|
198
|
+
Get the named exports from an export declaration:
|
|
199
|
+
|
|
200
|
+
```ts
|
|
201
|
+
const namedExports = exportDeclaration.getNamedExports();
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
Adding or inserting named exports can be done via the `addNamedExport`, `addNamedExports`, `insertNamedExport`, or `insertNamedExports` methods.
|
|
205
|
+
|
|
206
|
+
```ts
|
|
207
|
+
const namedExport = exportDeclaration.addNamedExport({
|
|
208
|
+
name: "MyClass",
|
|
209
|
+
alias: "MyAliasName", // alias is optional
|
|
210
|
+
});
|
|
211
|
+
// or
|
|
212
|
+
exportDeclaration.addNamedExport("MyClass");
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
Removing one named export:
|
|
216
|
+
|
|
217
|
+
```ts
|
|
218
|
+
namedExport.remove();
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
#### Export specifier
|
|
222
|
+
|
|
223
|
+
Export specifiers are the individual named exports.
|
|
224
|
+
|
|
225
|
+
##### Name
|
|
226
|
+
|
|
227
|
+
```ts
|
|
228
|
+
namedExport.getNameNode(); // returns: Identifier
|
|
229
|
+
namedExport.setName("NewName");
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
##### Alias
|
|
233
|
+
|
|
234
|
+
```ts
|
|
235
|
+
namedExport.getAliasNode(); // returns: Identifier | undefined
|
|
236
|
+
namedExport.setAlias("NewAliasName");
|
|
237
|
+
namedExport.renameAlias("NewAliasName");
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
_Note:_ Renaming the alias will set or rename any uses of the alias or identifier to the new value.
|
|
241
|
+
|
|
242
|
+
##### Local Target Declarations
|
|
243
|
+
|
|
244
|
+
The local target declarations are the declarations that the export specifier is referencing:
|
|
245
|
+
|
|
246
|
+
```ts
|
|
247
|
+
const declarations = namedExport.getLocalTargetDeclarations(); // returns: Node[]
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
##### Parent export declaration
|
|
251
|
+
|
|
252
|
+
```ts
|
|
253
|
+
namedExport.getExportDeclaration(); // returns: ExportDeclaration
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
## Export Assignments
|
|
257
|
+
|
|
258
|
+
Export assignments look like the following:
|
|
259
|
+
|
|
260
|
+
```ts setup: let name: string;
|
|
261
|
+
export = 5;
|
|
262
|
+
export default name;
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
Get the export assignments by calling:
|
|
266
|
+
|
|
267
|
+
```ts
|
|
268
|
+
const exportAssignments = sourceFile.getExportAssignments();
|
|
269
|
+
// or to get the first one that matches a condition
|
|
270
|
+
const exportAssignment = sourceFile.getExportAssignment(d => d.isExportEquals());
|
|
271
|
+
|
|
272
|
+
// get if it's `export =` or `export default`
|
|
273
|
+
const isExportEquals = exportAssignment.isExportEquals();
|
|
274
|
+
// get the expression
|
|
275
|
+
const expression = exportAssignment.getExpression();
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
Set whether one is an export equals or export default:
|
|
279
|
+
|
|
280
|
+
```ts
|
|
281
|
+
exportAssignment.setIsExportEquals(false); // sets to export default
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
Set its expression:
|
|
285
|
+
|
|
286
|
+
```ts
|
|
287
|
+
exportAssignment.setExpression(writer => writer.write("5"));
|
|
288
|
+
exportAssignment.setExpression("6");
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
### Add/Insert
|
|
292
|
+
|
|
293
|
+
Add or insert use `insertExportAssignment`, `insertExportAssignments`, `addExportAssignment`, or `addExportAssignments`:
|
|
294
|
+
|
|
295
|
+
```ts
|
|
296
|
+
const exportAssignment = sourceFile.addExportAssignment({
|
|
297
|
+
isExportEquals: true, // defaults to true
|
|
298
|
+
expression: "5",
|
|
299
|
+
});
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
### Remove
|
|
303
|
+
|
|
304
|
+
Call `.remove()`:
|
|
305
|
+
|
|
306
|
+
```ts
|
|
307
|
+
exportAssignment.remove();
|
|
308
|
+
```
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Expressions
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
## Expression With Type Arguments
|
|
6
|
+
|
|
7
|
+
These are found in certain areas. For example `extends` and `implements` expressions.
|
|
8
|
+
|
|
9
|
+
### Getting expression
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
const expression = expressionWithTypeArgs.getExpression(); // returns: Node
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
### Getting type arguments
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
const typeArgs = expressionWithTypeArgs.getTypeArguments(); // returns: TypeNode[]
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Call Expressions
|
|
22
|
+
|
|
23
|
+
Call expressions are statements that call a function:
|
|
24
|
+
|
|
25
|
+
```ts setup: const doSomething: () => void;
|
|
26
|
+
doSomething();
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### Getting call signatures
|
|
30
|
+
|
|
31
|
+
From a given node, get all the children or descendants that are call expressions.
|
|
32
|
+
|
|
33
|
+
For example:
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
const childCallExpressions = node.getChildrenOfKind(SyntaxKind.CallExpression);
|
|
37
|
+
const descendantCallExpressions = node.getDescendantsOfKind(SyntaxKind.CallExpression);
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Return type
|
|
41
|
+
|
|
42
|
+
Use the following:
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
const returnType = callExpression.getReturnType();
|
|
46
|
+
```
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Functions
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
## Function Declarations
|
|
6
|
+
|
|
7
|
+
Functions can be retrieved from source files, other namespaces, or function bodies:
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
const functions = sourceFile.getFunctions();
|
|
11
|
+
const function1 = sourceFile.getFunction("Function1");
|
|
12
|
+
const firstFunctionWithChildFunction = sourceFile.getFunction(f => f.getFunctions().length > 0);
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Most of the information you can get about functions is covered in other sections.
|
|
16
|
+
|
|
17
|
+
### Name
|
|
18
|
+
|
|
19
|
+
It's important to note that function declarations may not have a name. For example:
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
export default function() {
|
|
23
|
+
// etc...
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
For this reason, the methods like `.getName()` and `.getNameNode()` are nullable on `FunctionDeclaration`.
|
|
28
|
+
|
|
29
|
+
### Add/Insert
|
|
30
|
+
|
|
31
|
+
Add or insert functions to a source file, namespace, or function like declarations by calling `addFunction()`, `addFunctions()`, `insertFunction()`, or `insertFunctions()`.
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
const functionDeclaration = sourceFile.addFunction({
|
|
35
|
+
name: "FunctionName",
|
|
36
|
+
});
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Remove
|
|
40
|
+
|
|
41
|
+
Call `.remove()`:
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
functionDeclaration.remove();
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Overloads
|
|
48
|
+
|
|
49
|
+
By default, in ambient contexts or for ambient nodes, all overloads will be returned. In non-ambient contexts, only the implementation will be returned.
|
|
50
|
+
|
|
51
|
+
Get the overloads by calling:
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
const overloads = functionDeclaration.getOverloads(); // returns: FunctionDeclaration[]
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Or tell if the current declaration is an overload by calling either:
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
functionDeclaration.isOverload();
|
|
61
|
+
functionDeclaration.isImplementation();
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
From the overloads, get the implementation by calling:
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
const implementation = functionOverload.getImplementation();
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
#### Add/Insert
|
|
71
|
+
|
|
72
|
+
Add or insert overloads by using either the `.addOverload()`, `.addOverloads()`, `.insertOverload()`, or `insertOverloads()` methods.
|
|
73
|
+
|
|
74
|
+
#### Remove
|
|
75
|
+
|
|
76
|
+
Call `.remove()` on the overload:
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
functionOverload.remove();
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Set body text
|
|
83
|
+
|
|
84
|
+
The body text can be set via the `.setBodyText()` method:
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
functionDeclaration.setBodyText("const myNumber = 5;");
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Or alternatively, write the body text with [code-block-writer](https://github.com/dsherret/code-block-writer):
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
functionDeclaration.setBodyText(writer =>
|
|
94
|
+
writer.writeLine("const myNumber = 5;")
|
|
95
|
+
.write("if (myNumber === 5)").block(() => {
|
|
96
|
+
writer.writeLine("console.log('yes')");
|
|
97
|
+
})
|
|
98
|
+
);
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Using the writer is very useful because it will write code out using the indentation and newline settings of the AST. It's also easier to use.
|
|
102
|
+
|
|
103
|
+
### Get body text
|
|
104
|
+
|
|
105
|
+
This returns the body text without leading indentation or leading and trailing whitespace.
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
console.log(functionDeclaration.getBodyText());
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### Unwrap
|
|
112
|
+
|
|
113
|
+
A function declaration can be replaced with its body using the `.unwrap()` method.
|
|
114
|
+
|
|
115
|
+
Given the following code:
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
function someFunction() {
|
|
119
|
+
function innerFunction() {
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const someDeclaration = 5;
|
|
123
|
+
}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
Calling `.unwrap()` on the function will change the code to the following:
|
|
127
|
+
|
|
128
|
+
```ts
|
|
129
|
+
function innerFunction() {
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const someDeclaration = 5;
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## Function Expressions
|
|
136
|
+
|
|
137
|
+
They exist in an expression:
|
|
138
|
+
|
|
139
|
+
```ts
|
|
140
|
+
const add = function(a: number, b: number) {
|
|
141
|
+
return a + b;
|
|
142
|
+
};
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
In this case, it can be retrieved via the variable declaration's [initializer](initializers).
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
const functionExpression = sourceFile.getVariableDeclarationOrThrow("add")
|
|
149
|
+
.getInitializerIfKindOrThrow(SyntaxKind.FunctionExpression);
|
|
150
|
+
```
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Generators
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
## Generators
|
|
6
|
+
|
|
7
|
+
Nodes like `FunctionDeclaration` and `MethodDeclaration` can be generators.
|
|
8
|
+
|
|
9
|
+
### Tell if a generator
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
functionDeclaration.isGenerator(); // returns: boolean
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
### Set as a generator
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
functionDeclaration.setIsGenerator(true); // or false to set as not one
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
### Get asterisk token (`*`)
|
|
22
|
+
|
|
23
|
+
Gets the asterisk token or undefined if not exists:
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
functionDeclaration.getAsteriskToken();
|
|
27
|
+
```
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Identifiers
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
## Identifiers
|
|
6
|
+
|
|
7
|
+
Identifiers are nodes that reference or define the name of a node.
|
|
8
|
+
|
|
9
|
+
For example, in the following code the identifiers are the variable name and the use of the variable:
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
const identifier = 5;
|
|
13
|
+
console.log(identifier);
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
### Getting identifiers
|
|
17
|
+
|
|
18
|
+
A lot of the time, a node will have a name and you can retrieve the identifier via `.getNameNode()`.
|
|
19
|
+
|
|
20
|
+
If not, from a given node, you can get all the children or descendants that are identifiers. For example:
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
const childIdentifiers = node.getChildrenOfKind(SyntaxKind.Identifier);
|
|
24
|
+
const descendantIdentifiers = node.getDescendantsOfKind(SyntaxKind.Identifier);
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### Text
|
|
28
|
+
|
|
29
|
+
Get the text:
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
const text = identifier.getText();
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### Rename
|
|
36
|
+
|
|
37
|
+
Rename an identifier:
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
identifier.rename("someNewName");
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Find References
|
|
44
|
+
|
|
45
|
+
Find all the references:
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
const references = identifier.findReferences();
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Get Definitions
|
|
52
|
+
|
|
53
|
+
Gets the definitions of the identifier. This is similar to "go to definition" functionality that exists with TypeScript in most IDEs.
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
const definitions = identifier.getDefinitions();
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Or just get the nodes:
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
const nodes = identifier.getDefinitionNodes();
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Get Implementations
|
|
66
|
+
|
|
67
|
+
Gets the implementations of the identifier. This is similar to "go to implementation" functionality that exists with TypeScript in most IDEs.
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
const implementations = identifier.getImplementations();
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Type
|
|
74
|
+
|
|
75
|
+
Get the type of an identifier:
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
const identifierType = identifier.getType();
|
|
79
|
+
```
|