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.
Files changed (151) hide show
  1. package/.plans/001-fcis-analyzer.md +832 -0
  2. package/.plans/002-fcis-analyzer-improvements.md +205 -0
  3. package/README.md +272 -0
  4. package/TECHNICAL.md +386 -0
  5. package/dist/cli.d.ts +1 -0
  6. package/dist/cli.js +1836 -0
  7. package/dist/cli.js.map +1 -0
  8. package/dist/index.d.ts +709 -0
  9. package/dist/index.js +1845 -0
  10. package/dist/index.js.map +1 -0
  11. package/package.json +47 -0
  12. package/pnpm-workspace.yaml +0 -0
  13. package/src/analyzer.ts +266 -0
  14. package/src/classification/classifier.ts +156 -0
  15. package/src/classification/derive-status.ts +171 -0
  16. package/src/classification/quality-scorer.ts +481 -0
  17. package/src/cli.ts +286 -0
  18. package/src/detection/detect-markers.ts +480 -0
  19. package/src/detection/markers.ts +332 -0
  20. package/src/extraction/extract-functions.ts +570 -0
  21. package/src/extraction/extractor.ts +188 -0
  22. package/src/index.ts +111 -0
  23. package/src/reporting/report-console.ts +416 -0
  24. package/src/reporting/report-json.ts +232 -0
  25. package/src/scoring/scorer.ts +504 -0
  26. package/src/types.ts +248 -0
  27. package/tests/classifier.test.ts +480 -0
  28. package/tests/derive-status.test.ts +464 -0
  29. package/tests/detect-markers.test.ts +639 -0
  30. package/tests/extractor.test.ts +155 -0
  31. package/tests/integration.test.ts +706 -0
  32. package/tests/quality-scorer.test.ts +650 -0
  33. package/tests/scorer.test.ts +768 -0
  34. package/tsconfig.json +34 -0
  35. package/tsup.config.ts +17 -0
  36. package/vendor/ts-morph/.editorconfig +10 -0
  37. package/vendor/ts-morph/.gitattributes +11 -0
  38. package/vendor/ts-morph/.github/CODE_OF_CONDUCT.md +77 -0
  39. package/vendor/ts-morph/.github/ISSUE_TEMPLATE/bug_report.md +29 -0
  40. package/vendor/ts-morph/.github/ISSUE_TEMPLATE/custom.md +4 -0
  41. package/vendor/ts-morph/.github/ISSUE_TEMPLATE/feature_request.md +18 -0
  42. package/vendor/ts-morph/.github/workflows/ci.yml +50 -0
  43. package/vendor/ts-morph/.github/workflows/publish.yml +53 -0
  44. package/vendor/ts-morph/.vscode/settings.json +10 -0
  45. package/vendor/ts-morph/CONTRIBUTING.md +23 -0
  46. package/vendor/ts-morph/DEVELOPMENT.md +32 -0
  47. package/vendor/ts-morph/LICENSE +21 -0
  48. package/vendor/ts-morph/deno.json +8 -0
  49. package/vendor/ts-morph/deno.lock +1233 -0
  50. package/vendor/ts-morph/docs/CNAME +1 -0
  51. package/vendor/ts-morph/docs/Gemfile +2 -0
  52. package/vendor/ts-morph/docs/_config.yml +5 -0
  53. package/vendor/ts-morph/docs/_layouts/default.html +159 -0
  54. package/vendor/ts-morph/docs/_script-templates/main.ts +116 -0
  55. package/vendor/ts-morph/docs/assets/css/style.scss +212 -0
  56. package/vendor/ts-morph/docs/details/ambient.md +38 -0
  57. package/vendor/ts-morph/docs/details/async.md +31 -0
  58. package/vendor/ts-morph/docs/details/classes.md +314 -0
  59. package/vendor/ts-morph/docs/details/comment-ranges.md +7 -0
  60. package/vendor/ts-morph/docs/details/comments.md +122 -0
  61. package/vendor/ts-morph/docs/details/decorators.md +119 -0
  62. package/vendor/ts-morph/docs/details/documentation.md +73 -0
  63. package/vendor/ts-morph/docs/details/enums.md +117 -0
  64. package/vendor/ts-morph/docs/details/exports.md +308 -0
  65. package/vendor/ts-morph/docs/details/expressions.md +46 -0
  66. package/vendor/ts-morph/docs/details/functions.md +150 -0
  67. package/vendor/ts-morph/docs/details/generators.md +27 -0
  68. package/vendor/ts-morph/docs/details/identifiers.md +79 -0
  69. package/vendor/ts-morph/docs/details/imports.md +191 -0
  70. package/vendor/ts-morph/docs/details/index.md +52 -0
  71. package/vendor/ts-morph/docs/details/initializers.md +40 -0
  72. package/vendor/ts-morph/docs/details/interfaces.md +218 -0
  73. package/vendor/ts-morph/docs/details/literals.md +20 -0
  74. package/vendor/ts-morph/docs/details/modifiers.md +38 -0
  75. package/vendor/ts-morph/docs/details/modules.md +113 -0
  76. package/vendor/ts-morph/docs/details/namespaces.md +7 -0
  77. package/vendor/ts-morph/docs/details/object-literal-expressions.md +106 -0
  78. package/vendor/ts-morph/docs/details/parameters.md +64 -0
  79. package/vendor/ts-morph/docs/details/signatures.md +41 -0
  80. package/vendor/ts-morph/docs/details/source-files.md +292 -0
  81. package/vendor/ts-morph/docs/details/type-aliases.md +34 -0
  82. package/vendor/ts-morph/docs/details/type-parameters.md +72 -0
  83. package/vendor/ts-morph/docs/details/types.md +254 -0
  84. package/vendor/ts-morph/docs/details/variables.md +110 -0
  85. package/vendor/ts-morph/docs/emitting.md +151 -0
  86. package/vendor/ts-morph/docs/index.md +25 -0
  87. package/vendor/ts-morph/docs/manipulation/code-writer.md +20 -0
  88. package/vendor/ts-morph/docs/manipulation/formatting.md +76 -0
  89. package/vendor/ts-morph/docs/manipulation/index.md +136 -0
  90. package/vendor/ts-morph/docs/manipulation/order.md +14 -0
  91. package/vendor/ts-morph/docs/manipulation/performance.md +222 -0
  92. package/vendor/ts-morph/docs/manipulation/removing.md +31 -0
  93. package/vendor/ts-morph/docs/manipulation/renaming.md +106 -0
  94. package/vendor/ts-morph/docs/manipulation/settings.md +76 -0
  95. package/vendor/ts-morph/docs/manipulation/structures.md +117 -0
  96. package/vendor/ts-morph/docs/manipulation/transforms.md +84 -0
  97. package/vendor/ts-morph/docs/metrics/performance.json +4 -0
  98. package/vendor/ts-morph/docs/navigation/ambient-modules.md +22 -0
  99. package/vendor/ts-morph/docs/navigation/compiler-nodes.md +82 -0
  100. package/vendor/ts-morph/docs/navigation/directories.md +287 -0
  101. package/vendor/ts-morph/docs/navigation/example.md +50 -0
  102. package/vendor/ts-morph/docs/navigation/finding-references.md +53 -0
  103. package/vendor/ts-morph/docs/navigation/getting-source-files.md +59 -0
  104. package/vendor/ts-morph/docs/navigation/images/getChildrenVsForEachChild.gif +0 -0
  105. package/vendor/ts-morph/docs/navigation/index.md +94 -0
  106. package/vendor/ts-morph/docs/navigation/language-service.md +23 -0
  107. package/vendor/ts-morph/docs/navigation/program.md +25 -0
  108. package/vendor/ts-morph/docs/navigation/type-checker.md +33 -0
  109. package/vendor/ts-morph/docs/setup/adding-source-files.md +145 -0
  110. package/vendor/ts-morph/docs/setup/ast-viewers.md +46 -0
  111. package/vendor/ts-morph/docs/setup/diagnostics.md +109 -0
  112. package/vendor/ts-morph/docs/setup/file-system.md +106 -0
  113. package/vendor/ts-morph/docs/setup/images/atom-ast.png +0 -0
  114. package/vendor/ts-morph/docs/setup/images/atom-ast_small.png +0 -0
  115. package/vendor/ts-morph/docs/setup/images/atom-command-palette.png +0 -0
  116. package/vendor/ts-morph/docs/setup/images/atom-file.png +0 -0
  117. package/vendor/ts-morph/docs/setup/images/ts-ast-viewer.png +0 -0
  118. package/vendor/ts-morph/docs/setup/index.md +94 -0
  119. package/vendor/ts-morph/docs/utilities.md +55 -0
  120. package/vendor/ts-morph/dprint.json +23 -0
  121. package/vendor/ts-morph/package.json +30 -0
  122. package/vendor/ts-morph/packages/bootstrap/LICENSE +21 -0
  123. package/vendor/ts-morph/packages/bootstrap/lib/ts-morph-bootstrap.d.ts +397 -0
  124. package/vendor/ts-morph/packages/bootstrap/package.json +46 -0
  125. package/vendor/ts-morph/packages/bootstrap/readme.md +200 -0
  126. package/vendor/ts-morph/packages/common/LICENSE +21 -0
  127. package/vendor/ts-morph/packages/common/lib/ts-morph-common.d.ts +1082 -0
  128. package/vendor/ts-morph/packages/common/lib/typescript.d.ts +11439 -0
  129. package/vendor/ts-morph/packages/common/package.json +65 -0
  130. package/vendor/ts-morph/packages/common/readme.md +5 -0
  131. package/vendor/ts-morph/packages/scripts/changeTypeScriptVersion.ts +28 -0
  132. package/vendor/ts-morph/packages/scripts/createDeclarationProject.ts +47 -0
  133. package/vendor/ts-morph/packages/scripts/deps.ts +2 -0
  134. package/vendor/ts-morph/packages/scripts/execScript.ts +31 -0
  135. package/vendor/ts-morph/packages/scripts/folders.ts +11 -0
  136. package/vendor/ts-morph/packages/scripts/getDevCompilerVersions.ts +19 -0
  137. package/vendor/ts-morph/packages/scripts/mod.ts +7 -0
  138. package/vendor/ts-morph/packages/scripts/utils/Memoize.ts +36 -0
  139. package/vendor/ts-morph/packages/scripts/utils/forEachTypeText.ts +23 -0
  140. package/vendor/ts-morph/packages/scripts/utils/makeConstructorsPrivate.ts +26 -0
  141. package/vendor/ts-morph/packages/scripts/utils/mod.ts +4 -0
  142. package/vendor/ts-morph/packages/scripts/utils/printDiagnostics.ts +10 -0
  143. package/vendor/ts-morph/packages/ts-morph/LICENSE +21 -0
  144. package/vendor/ts-morph/packages/ts-morph/lib/ts-morph.d.ts +11198 -0
  145. package/vendor/ts-morph/packages/ts-morph/package.json +78 -0
  146. package/vendor/ts-morph/packages/ts-morph/readme.md +111 -0
  147. package/vendor/ts-morph/readme.md +14 -0
  148. package/vendor/ts-morph/rfcs/README.md +13 -0
  149. package/vendor/ts-morph/rfcs/RFC-0001 - Inserting Into Statements Handling Comments.md +181 -0
  150. package/vendor/ts-morph/tsconfig.common.json +17 -0
  151. package/vitest.config.ts +16 -0
@@ -0,0 +1,314 @@
1
+ ---
2
+ title: Classes
3
+ ---
4
+
5
+ ## Class Declarations
6
+
7
+ Class declarations can be retrieved from source files, namespaces, or function bodies:
8
+
9
+ ```ts
10
+ const classes = sourceFile.getClasses();
11
+ const class1 = sourceFile.getClass("Class1");
12
+ const firstClassWithConstructor = sourceFile.getClass(c => c.getConstructors().length > 0);
13
+ ```
14
+
15
+ ### Name
16
+
17
+ It's important to note that class declarations may not have a name. For example:
18
+
19
+ ```ts
20
+ export default class {
21
+ // etc...
22
+ }
23
+ ```
24
+
25
+ For this reason, the methods like `.getName()` and `.getNameNode()` are nullable on `ClassDeclaration`.
26
+
27
+ ### Add/Insert
28
+
29
+ Add or insert classes to a source file, namespace, or function like declarations by calling `addClass()`, `addClasses()`, `insertClass()`, or `insertClasses()`.
30
+
31
+ ```ts
32
+ const classDeclaration = sourceFile.addClass({
33
+ name: "ClassName",
34
+ });
35
+ ```
36
+
37
+ ### Remove
38
+
39
+ Call `.remove()`:
40
+
41
+ ```ts
42
+ classDeclaration.remove();
43
+ ```
44
+
45
+ ### Extends expression
46
+
47
+ Will return [`ExpressionWithTypeArguments | undefined`](expressions):
48
+
49
+ ```ts
50
+ const extendsExpression = classDeclaration.getExtends();
51
+ ```
52
+
53
+ Set the extends expression:
54
+
55
+ ```ts
56
+ classDeclaration.setExtends("BaseClass");
57
+ ```
58
+
59
+ Remove it:
60
+
61
+ ```ts
62
+ classDeclaration.removeExtends();
63
+ ```
64
+
65
+ ### Implements expressions
66
+
67
+ Will return [`ExpressionWithTypeArguments[]`](expressions):
68
+
69
+ ```ts
70
+ const implementsExpressions = classDeclaration.getImplements();
71
+ ```
72
+
73
+ Add or insert implements expressions:
74
+
75
+ ```ts
76
+ classDeclaration.addImplements("Named");
77
+ classDeclaration.addImplements(["Named", "Aged"]);
78
+ classDeclaration.insertImplements(1, "Named");
79
+ classDeclaration.insertImplements(1, ["Named", "Aged"]);
80
+ ```
81
+
82
+ Remove an expression:
83
+
84
+ ```ts
85
+ classDeclaration.removeImplements(0); // index
86
+ classDeclaration.removeImplements(classDeclaration.getImplements()[0]); // node
87
+ ```
88
+
89
+ ### Base Types
90
+
91
+ Get the base types:
92
+
93
+ ```ts
94
+ const baseTypes = classDeclaration.getBaseTypes(); // returns: Type[]
95
+ ```
96
+
97
+ This is useful to use if you don't know if the class could possibly extend a mixin or a class.
98
+
99
+ ### Base Class
100
+
101
+ Get the base class:
102
+
103
+ ```ts
104
+ const baseClass = classDeclaration.getBaseClass(); // returns: ClassDeclaration | undefined
105
+ ```
106
+
107
+ Note: This is not a useful method to use if the base could possibly be a mixin. If you expect mixins, then use `.getBaseTypes()`.
108
+
109
+ ### Derived Classes
110
+
111
+ Will return all the class declarations that derive from the current class:
112
+
113
+ ```ts
114
+ const derivedClasses = classDeclaration.getDerivedClasses();
115
+ ```
116
+
117
+ ### Constructor
118
+
119
+ Constructors can be retreived via `getConstructors`. This returns all the constructors in an ambient context, but will only return the
120
+ implementation constructor otherwise.
121
+
122
+ ```ts
123
+ const constructors = classDeclaration.getConstructors();
124
+ ```
125
+
126
+ Add or insert a constructor or constructors by calling `addConstructor()`, `addConstructors()`, `insertConstructor()`, or `insertConstructors()`.
127
+
128
+ ```ts
129
+ const ctor = classDeclaration.addConstructor({
130
+ /* options like parameters may go here */
131
+ });
132
+ ```
133
+
134
+ ### Methods
135
+
136
+ Get instance methods:
137
+
138
+ ```ts
139
+ const instanceMethods = classDeclaration.getInstanceMethods();
140
+ const myMethod = classDeclaration.getInstanceMethod("myMethod");
141
+ const firstMethodWith2Params = classDeclaration.getInstanceMethod(m => m.getParameters().length === 2);
142
+ ```
143
+
144
+ Get the static methods:
145
+
146
+ ```ts
147
+ const staticMethods = classDeclaration.getStaticMethods();
148
+ const myStaticMethod = classDeclaration.getStaticMethod("myMethod");
149
+ const firstStaticMethodWith2Params = classDeclaration.getStaticMethod(m => m.getParameters().length === 2);
150
+ ```
151
+
152
+ #### Add/Insert
153
+
154
+ Add or insert methods by using `insertMethods()`, `insertMethod`, `addMethod`, or `addMethods`:
155
+
156
+ ```ts
157
+ const method = classDeclaration.addMethod({ isStatic: true, name: "myMethod", returnType: "string" });
158
+ ```
159
+
160
+ #### Remove
161
+
162
+ Call `.remove()`:
163
+
164
+ ```ts
165
+ method.remove();
166
+ ```
167
+
168
+ ### Properties
169
+
170
+ Get the instance properties (includes parameter properties):
171
+
172
+ <!-- dprint-ignore -->
173
+ ```ts
174
+ const instanceProperties = classDeclaration.getInstanceProperties();
175
+ const myProperty = classDeclaration.getInstanceProperty("myProperty");
176
+ const myStringProperty = classDeclaration.getInstanceProperty(p =>
177
+ Node.isPropertyDeclaration(p) && p.getType().getText() === "string");
178
+ ```
179
+
180
+ Get the static properties:
181
+
182
+ <!-- dprint-ignore -->
183
+ ```ts
184
+ const staticProperties = classDeclaration.getStaticProperties();
185
+ const myStaticProperty = classDeclaration.getStaticProperty("myStaticProperty");
186
+ const myStaticStringProperty = classDeclaration.getStaticProperty(p =>
187
+ Node.isPropertyDeclaration(p) && p.getType().getText() === "string");
188
+ ```
189
+
190
+ #### Add/Insert
191
+
192
+ Add or insert properties by using `insertProperties()`, `insertProperty`, `addProperty`, or `addProperties`:
193
+
194
+ ```ts
195
+ const property = classDeclaration.addProperty({
196
+ isStatic: true,
197
+ name: "prop",
198
+ type: "string",
199
+ });
200
+ ```
201
+
202
+ Add or insert get accessors by using `insertGetAccessors()`, `insertGetAccessor`, `addGetAccessor`, or `addGetAccessors`:
203
+
204
+ ```ts
205
+ const getAccessor = classDeclaration.addGetAccessor({
206
+ name: "someNumber",
207
+ returnType: "number",
208
+ statements: ["return 5;"],
209
+ });
210
+ ```
211
+
212
+ Add or insert set accessors by using `insertSetAccessors()`, `insertSetAccessor`, `addSetAccessor`, or `addSetAccessors`:
213
+
214
+ ```ts
215
+ const setAccessor = classDeclaration.addSetAccessor({
216
+ name: "someNumber",
217
+ parameters: [{ name: "value", type: "number" }],
218
+ statements: ["_someNumber = value;"],
219
+ });
220
+ ```
221
+
222
+ #### Remove
223
+
224
+ Call `.remove()`:
225
+
226
+ ```ts
227
+ propertyDeclaration.remove();
228
+ ```
229
+
230
+ ### Get members
231
+
232
+ Get all the members regardless of whether static or instance:
233
+
234
+ ```ts
235
+ const allMembers = classDeclaration.getMembers();
236
+ ```
237
+
238
+ Get instance members including parameter properties:
239
+
240
+ ```ts
241
+ const instanceMembers = classDeclaration.getInstanceMembers();
242
+ ```
243
+
244
+ Get static members:
245
+
246
+ ```ts
247
+ const staticMembers = classDeclaration.getStaticMembers();
248
+ ```
249
+
250
+ ### Extracting an Interface
251
+
252
+ An interface declaration structure can be extracted from a class by calling `classDeclaration.extractInterface()`. For example:
253
+
254
+ ```ts
255
+ // the passed in name is optional and defaults to the class name
256
+ const interfaceStructure = classDeclaration.extractInterface(`I${classDeclaration.getName()}`);
257
+ ```
258
+
259
+ Alternatively the static interface declaration structure of a class can be extracted by calling:
260
+
261
+ ```ts
262
+ const interfaceStructure = classDeclaration.extractStaticInterface(`${classDeclaration.getName()}Static`);
263
+ ```
264
+
265
+ ## Abstract
266
+
267
+ Nodes on a class may be abstract.
268
+
269
+ Get if it's abstract:
270
+
271
+ ```ts
272
+ method.isAbstract(); // returns: boolean
273
+ ```
274
+
275
+ Get the abstract keyword:
276
+
277
+ ```ts
278
+ method.getAbstractKeyword(); // returns: node | undefined
279
+ ```
280
+
281
+ Set if abstract:
282
+
283
+ ```ts
284
+ method.setIsAbstract(true); // set as abstract
285
+ method.setIsAbstract(false); // set as not abstract
286
+ ```
287
+
288
+ ## Constructors
289
+
290
+ Constructors implement common functions found on function like declarations, but also include a scope.
291
+
292
+ ## Methods
293
+
294
+ Explore the functionality available via auto-complete.
295
+
296
+ ## Properties
297
+
298
+ Explore the functionality available via auto-complete.
299
+
300
+ ## Get Accessors
301
+
302
+ If it exists, get the corresponding set accessor:
303
+
304
+ ```ts
305
+ const setAccessor = getAccessor.getSetAccessor();
306
+ ```
307
+
308
+ ## Set Accessors
309
+
310
+ If it exists, get the corresponding get accessor:
311
+
312
+ ```ts
313
+ const getAccessor = setAccessor.getGetAccessor();
314
+ ```
@@ -0,0 +1,7 @@
1
+ ---
2
+ title: Comment Ranges
3
+ ---
4
+
5
+ ## Comment Ranges
6
+
7
+ See [Comments](comments.md).
@@ -0,0 +1,122 @@
1
+ ---
2
+ title: Comments
3
+ ---
4
+
5
+ ## Comments
6
+
7
+ _ts-morph_ parses out certain kinds of comments to make them easier to work with. This breaks away from the behaviour in the compiler API because although comments are not very important for compiling, they can be important for programmatic refactoring tasks.
8
+
9
+ - **Comment Ranges** - Comments the compiler api parses out.
10
+ - **Comment Nodes** - Comments ts-morph parses out in certain scenarios. They extend from `ts.Node`.
11
+
12
+ ## Comment Ranges
13
+
14
+ Comment ranges are not part of the AST and are generated on request.
15
+
16
+ - **Leading:** Comments that preceed the node after the previous significant token—between `node.getPos()` and `node.getStart()`.
17
+ - **Trailing:** Comments following the node before the next siginificant token or newline.
18
+
19
+ WARNING: Since comments are generated on demand and not part of the AST, using one after a
20
+ subsequent manipulation to the source file will throw an error.
21
+
22
+ ### Retrieving
23
+
24
+ Leading and trailing comment ranges can be retrieved from any node by calling:
25
+
26
+ ```ts
27
+ const leadingComments = node.getLeadingCommentRanges();
28
+ const trailingComments = node.getTrailingCommentRanges();
29
+ ```
30
+
31
+ ### Information
32
+
33
+ Once you have a comment range, there are several self explanatory methods:
34
+
35
+ - `.getKind()` - Returns `SyntaxKind.SingleLineCommentTrivia` or `SyntaxKind.MultiLineCommentTrivia`.
36
+ - `.getPos()` - Position.
37
+ - `.getEnd()` - End position.
38
+ - `.getWidth()` - Width (`end - pos`)
39
+ - `.getText()` - Text.
40
+ - `.wasForgotten()` - Returns true if the node was forgotten because a manipulation occured to the source file or its associated node was forgotten.
41
+
42
+ ### More Details
43
+
44
+ Read more about comments in the compiler API documentation here: https://github.com/Microsoft/TypeScript/wiki/Architectural-Overview#trivia
45
+
46
+ ## Comment Nodes
47
+
48
+ These are nodes ts-morph parses out. They do not include all comments, but only the first comment on a line if there are no significant tokens on that line and only in certain contexts (ex. source file statements, class declaration body, function body, namespace body, etc..).
49
+
50
+ For example, in the following scenario:
51
+
52
+ ```ts setup: let functionCall: any;
53
+ // do something
54
+ functionCall(); // ok
55
+ ```
56
+
57
+ ...only the first comment will be parsed out. The second comment is considered a trailing comment range of the function call.
58
+
59
+ ### Retrieving
60
+
61
+ Comment ranges can be retrieved using various `#getXWithComments()` methods.
62
+
63
+ For example:
64
+
65
+ ```ts setup: let classDec: any, interfaceDec: any;
66
+ sourceFile.getStatementsWithComments();
67
+ classDec.getMembersWithComments();
68
+ interfaceDec.getMembersWithComments();
69
+ objectLiteralExpression.getPropertiesWithComments();
70
+ ```
71
+
72
+ They also may appear in the results of `#getChildren()` in certain scenarios. For example:
73
+
74
+ ```ts
75
+ const children = sourceFile.getChildSyntaxListOrThrow().getChildren();
76
+ ```
77
+
78
+ This extends the behaviour of the compiler API.
79
+
80
+ ### Why?
81
+
82
+ The reason this is done is to make inserting before or after comments possible. For example, given the following source file:
83
+
84
+ ```ts setup: let functionCall: any;
85
+ // 1
86
+ // 2
87
+ functionCall();
88
+ ```
89
+
90
+ It is possible to insert a statement after the first comment by specifying `sourceFile.insertStatement(1, "// new comment");`, which would modify the source file to be:
91
+
92
+ ```ts setup: let functionCall: any;
93
+ // 1
94
+ // new comment
95
+ // 2
96
+ functionCall();
97
+ ```
98
+
99
+ ### Removing Comments
100
+
101
+ To remove a comment node, call `#remove()` on it:
102
+
103
+ ```ts
104
+ sourceFile.getStatementsWithComments()[0].remove();
105
+ ```
106
+
107
+ This will remove the comment and any of its trailing comment nodes.
108
+
109
+ ### Trailing comment ranges of comment nodes
110
+
111
+ Comment nodes may have trailing comment ranges. For example, given the following source file:
112
+
113
+ <!-- dprint-ignore -->
114
+ ```ts
115
+ /* 1 */ // 2
116
+ ```
117
+
118
+ ...the text `/* 1 */` is considered the comment node, while `// 2` is the trailing comment range. `// 2` can be retrieved with the following code:
119
+
120
+ ```ts
121
+ const secondComment = sourceFile.getStatementsWithComments()[0].getTrailingCommentRanges()[0];
122
+ ```
@@ -0,0 +1,119 @@
1
+ ---
2
+ title: Decorators
3
+ ---
4
+
5
+ ## Decorators
6
+
7
+ Decorators can be retrieved from class related nodes by calling the `getDecorators()` method.
8
+
9
+ ```ts
10
+ const decorators = classDeclaration.getDecorators();
11
+ ```
12
+
13
+ ### Name
14
+
15
+ Get the name or fully qualified name of a decorator by using the `getName()` or `getFullName()` functions respectively.
16
+
17
+ For example, given the following code:
18
+
19
+ ```ts setup: const obj: { decorator: any; };
20
+ @obj.decorator
21
+ class Identifier {
22
+ }
23
+ ```
24
+
25
+ The following happens:
26
+
27
+ ```ts
28
+ decorator.getName(); // "decorator"
29
+ decorator.getFullName(); // "obj.decorator"
30
+ ```
31
+
32
+ ### Decorator factory
33
+
34
+ Decorators with parenthesis (ex. `@decorator(3)`) are decorator factories, while decorators without (ex. `@decorator`) are not.
35
+
36
+ ```ts
37
+ decorator.isDecoratorFactory(); // returns: boolean
38
+ ```
39
+
40
+ Set as a decorator factory or not:
41
+
42
+ ```ts
43
+ decorator.setIsDecoratorFactory(true);
44
+ ```
45
+
46
+ ### Arguments
47
+
48
+ Get the decorator's arguments by calling `.getArguments()`:
49
+
50
+ ```ts
51
+ const args = decorator.getArguments(); // returns: Expression[]
52
+ ```
53
+
54
+ Add and insert via `.addArgument(...)`, `.insertArguments(...)`, `.addArgument(...)`, or `.addArguments(...)`.
55
+
56
+ ```ts
57
+ const args = decorator.insertArguments(1, ["5", "6"]);
58
+ ```
59
+
60
+ And remove them by calling `.removeArgument()`:
61
+
62
+ ```ts setup: const args: any;
63
+ // specify the index
64
+ decorator.removeArgument(0);
65
+ // or specify the argument node
66
+ decorator.removeArgument(args[0]);
67
+ ```
68
+
69
+ ### Type arguments
70
+
71
+ Get the decorator's type arguments by calling `.getTypeArguments()`:
72
+
73
+ ```ts
74
+ const typeArgs = decorator.getTypeArguments(); // returns: TypeNode[]
75
+ ```
76
+
77
+ Add and insert via `.insertTypeArgument(...)`, `.insertTypeArguments(...)`, `.addTypeArgument(...)`, or `.addTypeArguments(...)`.
78
+
79
+ ```ts
80
+ const typeArgs = decorator.insertTypeArguments(1, ["string", "number"]);
81
+ ```
82
+
83
+ And remove them by calling `.removeTypeArgument()`:
84
+
85
+ ```ts setup: let typeArgs: TypeNode[];
86
+ // specify the index
87
+ decorator.removeTypeArgument(0);
88
+ // or specify the type argument node
89
+ decorator.removeTypeArgument(typeArgs[0]);
90
+ ```
91
+
92
+ ### Call expression
93
+
94
+ Decorator factories are call expressions. Get the call expression by calling:
95
+
96
+ ```ts
97
+ const callExpression = decorator.getCallExpression(); // returns: CallExpression | undefined
98
+ ```
99
+
100
+ ### Add/Insert decorators
101
+
102
+ Decorators can be added or inserted by calling `addDecorator(decorator)`, `addDecorators(decorators)`, `insertDecorator(index, decorator)`, or `insertDecorators(index, decorators)`.
103
+
104
+ For example:
105
+
106
+ ```ts
107
+ classDeclaration.addDecorator({
108
+ name: "MyDecorator",
109
+ arguments: ["3", `"some string"`],
110
+ });
111
+ ```
112
+
113
+ ### Remove decorators
114
+
115
+ Call `.remove()`:
116
+
117
+ ```ts
118
+ decorator.remove();
119
+ ```
@@ -0,0 +1,73 @@
1
+ ---
2
+ title: JS Docs
3
+ ---
4
+
5
+ ## JS Docs
6
+
7
+ Certain nodes can have JS docs. For example:
8
+
9
+ ```ts setup: interface Person {}
10
+ /**
11
+ * Gets the name.
12
+ * @param person - Person to get the name from.
13
+ */
14
+ function getName(person: Person) {
15
+ // ...
16
+ }
17
+ ```
18
+
19
+ ### Get all JS doc nodes
20
+
21
+ Get all the JS doc nodes by using `getJsDocs()`:
22
+
23
+ ```ts
24
+ functionDeclaration.getJsDocs(); // returns: JSDoc[]
25
+ ```
26
+
27
+ ### Add/insert docs
28
+
29
+ Add or insert JS doc comments using the `addJsDoc()`, `addJsDocs()`, `insertJsDoc()`, and `insertJsDocs()` methods.
30
+
31
+ For example:
32
+
33
+ ```ts
34
+ // adds /** Some description... */
35
+ const docNode = classDeclaration.addJsDoc({
36
+ description: "Some description...",
37
+ });
38
+ // or to force it to be multi-line, add a newline to the front of the string
39
+ classDeclaration.addJsDoc({
40
+ description: "\nSome description...",
41
+ });
42
+ // or with tags
43
+ classDeclaration.addJsDoc({
44
+ description: "Some description...",
45
+ tags: [{
46
+ tagName: "param",
47
+ text: "value - My value.",
48
+ }],
49
+ });
50
+ ```
51
+
52
+ ### JSDoc Nodes
53
+
54
+ Get the description:
55
+
56
+ ```ts
57
+ // Getting the node from the example at the top of this file.
58
+ const jsDoc = functionDeclaration.getJsDocs()[0];
59
+ jsDoc.getDescription(); // returns string: "Gets the name."
60
+ ```
61
+
62
+ Get the tags:
63
+
64
+ ```ts
65
+ const tags = jsDoc.getTags();
66
+ tags[0].getText(); // "@param person - Person to get the name from."
67
+ ```
68
+
69
+ Get the inner text (the text without the surrounding comment):
70
+
71
+ ```ts
72
+ jsDoc.getInnerText(); // "Gets the name.\n@param person - Person to get the name from."
73
+ ```