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,78 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ts-morph",
|
|
3
|
+
"version": "27.0.2",
|
|
4
|
+
"description": "TypeScript compiler wrapper for static analysis and code manipulation.",
|
|
5
|
+
"main": "dist/ts-morph.js",
|
|
6
|
+
"types": "lib/ts-morph.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"dopublish": "deno task type-check-docs && deno task code-generate && deno task package && deno task publish-code-verification && echo \"Run: npm publish\"",
|
|
9
|
+
"build": "deno task build:declarations && deno task build:node",
|
|
10
|
+
"build:node": "rimraf dist && rollup -c ",
|
|
11
|
+
"build:deno": "deno task build:declarations && rimraf dist-deno && rollup -c --environment BUILD:deno && deno run -A scripts/buildDeno.ts",
|
|
12
|
+
"build:declarations": "deno run -A scripts/generation/main.ts create-declaration-file",
|
|
13
|
+
"test": "deno run -A npm:mocha",
|
|
14
|
+
"test:debug": "deno task test --inspect-brk",
|
|
15
|
+
"test:watch": "deno task test --watch-extensions ts --watch",
|
|
16
|
+
"test:ci": "deno task test",
|
|
17
|
+
"test:ts-versions": "deno run -A scripts/test/testTypeScriptVersions.ts",
|
|
18
|
+
"type-check": "deno run -A scripts/typeCheckLibrary.ts",
|
|
19
|
+
"code-generate": "deno run -A scripts/generation/main.ts",
|
|
20
|
+
"output-wrapped-nodes": "deno run -A scripts/generation/outputWrappedNodesInfo.ts",
|
|
21
|
+
"package": "deno task build",
|
|
22
|
+
"publish-code-verification": "deno task code-verification && deno task ensure-no-declaration-file-errors",
|
|
23
|
+
"code-verification": "deno run -A scripts/verification/main.ts ensure-structures-match-classes ensure-overload-structures-match ensure-array-inputs-readonly ensure-classes-implement-structure-methods ensure-mixin-not-applied-multiple-times validate-public-api-class-member-names validate-compiler-node-to-wrapped-type validate-code-fences",
|
|
24
|
+
"ensure-structures-match-classes": "deno run -A scripts/verification/main.ts ensure-structures-match-classes",
|
|
25
|
+
"ensure-overload-structures-match": "deno run -A scripts/verification/main.ts ensure-overload-structures-match",
|
|
26
|
+
"ensure-no-project-compile-errors": "deno run -A scripts/verification/ensureNoProjectCompileErrors.ts",
|
|
27
|
+
"ensure-no-declaration-file-errors": "deno run -A scripts/verification/ensureNoDeclarationFileErrors.ts",
|
|
28
|
+
"ensure-array-inputs-readonly": "deno run -A scripts/verification/main ensure-array-inputs-readonly.ts",
|
|
29
|
+
"ensure-or-throw-exists": "deno run -A scripts/verification/main ensure-or-throw-exists.ts",
|
|
30
|
+
"type-check-docs": "deno run -A scripts/typeCheckDocumentation.ts"
|
|
31
|
+
},
|
|
32
|
+
"repository": "git+https://github.com/dsherret/ts-morph.git",
|
|
33
|
+
"keywords": [
|
|
34
|
+
"typescript",
|
|
35
|
+
"ast",
|
|
36
|
+
"static analysis",
|
|
37
|
+
"code generation",
|
|
38
|
+
"code refactor"
|
|
39
|
+
],
|
|
40
|
+
"author": "David Sherret",
|
|
41
|
+
"license": "MIT",
|
|
42
|
+
"bugs": {
|
|
43
|
+
"url": "https://github.com/dsherret/ts-morph/issues"
|
|
44
|
+
},
|
|
45
|
+
"homepage": "https://github.com/dsherret/ts-morph#readme",
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"@ts-morph/common": "~0.28.1",
|
|
48
|
+
"code-block-writer": "^13.0.3"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@rollup/plugin-typescript": "^12.1.2",
|
|
52
|
+
"@types/chai": "^5.2.1",
|
|
53
|
+
"@types/diff": "^7.0.2",
|
|
54
|
+
"@types/mocha": "^10.0.10",
|
|
55
|
+
"@types/node": "^22.14.1",
|
|
56
|
+
"chai": "^5.2.0",
|
|
57
|
+
"conditional-type-checks": "^1.0.6",
|
|
58
|
+
"cross-env": "^7.0.3",
|
|
59
|
+
"diff": "^7.0.0",
|
|
60
|
+
"mocha": "11.1.0",
|
|
61
|
+
"rimraf": "^6.0.1",
|
|
62
|
+
"rollup": "=4.40.0",
|
|
63
|
+
"ts-node": "10.9.2",
|
|
64
|
+
"typescript": "~5.9.2"
|
|
65
|
+
},
|
|
66
|
+
"browser": {
|
|
67
|
+
"fs": false,
|
|
68
|
+
"os": false,
|
|
69
|
+
"fs.realpath": false,
|
|
70
|
+
"mkdirp": false,
|
|
71
|
+
"dir-glob": false,
|
|
72
|
+
"graceful-fs": false,
|
|
73
|
+
"source-map-support": false,
|
|
74
|
+
"glob-parent": false,
|
|
75
|
+
"glob": false,
|
|
76
|
+
"tinyglobby": false
|
|
77
|
+
}
|
|
78
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# ts-morph
|
|
2
|
+
|
|
3
|
+
[](https://badge.fury.io/js/ts-morph)
|
|
4
|
+
[](https://github.com/dsherret/ts-morph/actions?query=workflow%3ACI)
|
|
5
|
+
[](http://github.com/badges/stability-badges)
|
|
6
|
+
|
|
7
|
+
[TypeScript](https://github.com/Microsoft/TypeScript) Compiler API wrapper. Provides an easier way to programmatically navigate and manipulate TypeScript and JavaScript code.
|
|
8
|
+
|
|
9
|
+
Formerly `ts-simple-ast`.
|
|
10
|
+
|
|
11
|
+
## Overview
|
|
12
|
+
|
|
13
|
+
- [Documentation](https://ts-morph.com/)
|
|
14
|
+
- [Declaration file](https://github.com/dsherret/ts-morph/blob/latest/packages/ts-morph/lib/ts-morph.d.ts)
|
|
15
|
+
|
|
16
|
+
## Main Features
|
|
17
|
+
|
|
18
|
+
1. Wraps the compiler API objects to provide helper methods for getting information and programmatically changing files.
|
|
19
|
+
2. Allows falling back to the compiler API objects if necessary (ex. `classDeclaration.compilerNode` or `typeChecker.compilerObject`).
|
|
20
|
+
3. All changes are kept in memory (including file and directory moves) until specifying to save to the underlying file system.
|
|
21
|
+
4. Changes are made to the text and wrapped nodes can be held on to between manipulations.
|
|
22
|
+
|
|
23
|
+
## Getting Started
|
|
24
|
+
|
|
25
|
+
1. [Installing](https://ts-morph.com/)
|
|
26
|
+
2. [Instantiating](https://ts-morph.com/setup/)
|
|
27
|
+
3. [Adding source files](https://ts-morph.com/setup/adding-source-files)
|
|
28
|
+
4. [Getting source files](https://ts-morph.com/navigation/getting-source-files)
|
|
29
|
+
5. [Navigating](https://ts-morph.com/navigation/example)
|
|
30
|
+
6. [Manipulating](https://ts-morph.com/manipulation/)
|
|
31
|
+
|
|
32
|
+
## Library Progress
|
|
33
|
+
|
|
34
|
+
This library is still under active development. Most common code manipulation/generation use cases are implemented, but there's still a lot of work to do. Please open an issue if you find a feature missing, bug, or question that isn't in the issue tracker.
|
|
35
|
+
|
|
36
|
+
- [Breaking changes](https://github.com/dsherret/ts-morph/blob/latest/packages/ts-morph/breaking-changes.md)
|
|
37
|
+
- [Change log](https://github.com/dsherret/ts-morph/blob/latest/packages/ts-morph/CHANGELOG.md)
|
|
38
|
+
- [Wrapped nodes progress report](https://github.com/dsherret/ts-morph/blob/latest/packages/ts-morph/wrapped-nodes.md)
|
|
39
|
+
|
|
40
|
+
## Example
|
|
41
|
+
|
|
42
|
+
<!-- dprint-ignore -->
|
|
43
|
+
```ts
|
|
44
|
+
import { Project, StructureKind } from "ts-morph";
|
|
45
|
+
|
|
46
|
+
// initialize
|
|
47
|
+
const project = new Project({
|
|
48
|
+
// Optionally specify compiler options, tsconfig.json, in-memory file system, and more here.
|
|
49
|
+
// If you initialize with a tsconfig.json, then it will automatically populate the project
|
|
50
|
+
// with the associated source files.
|
|
51
|
+
// Read more: https://ts-morph.com/setup/
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// add source files
|
|
55
|
+
project.addSourceFilesAtPaths("src/**/*.ts");
|
|
56
|
+
const myClassFile = project.createSourceFile("src/MyClass.ts", "export class MyClass {}");
|
|
57
|
+
const myEnumFile = project.createSourceFile("src/MyEnum.ts", {
|
|
58
|
+
statements: [{
|
|
59
|
+
kind: StructureKind.Enum,
|
|
60
|
+
name: "MyEnum",
|
|
61
|
+
isExported: true,
|
|
62
|
+
members: [{ name: "member" }],
|
|
63
|
+
}],
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
// get information
|
|
67
|
+
const myClass = myClassFile.getClassOrThrow("MyClass");
|
|
68
|
+
myClass.getName(); // returns: "MyClass"
|
|
69
|
+
myClass.hasExportKeyword(); // returns: true
|
|
70
|
+
myClass.isDefaultExport(); // returns: false
|
|
71
|
+
|
|
72
|
+
// manipulate
|
|
73
|
+
const myInterface = myClassFile.addInterface({
|
|
74
|
+
name: "IMyInterface",
|
|
75
|
+
isExported: true,
|
|
76
|
+
properties: [{
|
|
77
|
+
name: "myProp",
|
|
78
|
+
type: "number",
|
|
79
|
+
}],
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
myClass.rename("NewName");
|
|
83
|
+
myClass.addImplements(myInterface.getName());
|
|
84
|
+
myClass.addProperty({
|
|
85
|
+
name: "myProp",
|
|
86
|
+
initializer: "5",
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
project.getSourceFileOrThrow("src/ExistingFile.ts").delete();
|
|
90
|
+
|
|
91
|
+
// asynchronously save all the changes above
|
|
92
|
+
await project.save();
|
|
93
|
+
|
|
94
|
+
// get underlying compiler node from the typescript AST from any node
|
|
95
|
+
const compilerNode = myClassFile.compilerNode;
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Or navigate existing compiler nodes created with the TypeScript compiler (the `ts` named export is the TypeScript compiler):
|
|
99
|
+
|
|
100
|
+
```ts ignore-error: 1109
|
|
101
|
+
import { createWrappedNode, ClassDeclaration, ts } from "ts-morph";
|
|
102
|
+
|
|
103
|
+
// some code that creates a class declaration using the ts object
|
|
104
|
+
const classNode: ts.ClassDeclaration = ...;
|
|
105
|
+
|
|
106
|
+
// create and use a wrapped node
|
|
107
|
+
const classDec = createWrappedNode(classNode) as ClassDeclaration;
|
|
108
|
+
const firstProperty = classDec.getProperties()[0];
|
|
109
|
+
|
|
110
|
+
// ... do more stuff here ...
|
|
111
|
+
```
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# ts-morph
|
|
2
|
+
|
|
3
|
+
[](https://github.com/dsherret/ts-morph/actions?query=workflow%3ACI)
|
|
4
|
+
|
|
5
|
+
Monorepo for [ts-morph](packages/ts-morph) and related projects.
|
|
6
|
+
|
|
7
|
+
## Packages
|
|
8
|
+
|
|
9
|
+
- [ts-morph](packages/ts-morph) - TypeScript Compiler API wrapper. Provides an easier way to programmatically navigate and manipulate TypeScript and JavaScript code.
|
|
10
|
+
- [@ts-morph/bootstrap](packages/bootstrap) - Separate library for getting quickly setup with the Compiler API.
|
|
11
|
+
|
|
12
|
+
## Resources
|
|
13
|
+
|
|
14
|
+
- [TypeScript AST Viewer](https://ts-ast-viewer.com)
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# RFCs
|
|
2
|
+
|
|
3
|
+
This is a very loose RFC process. The implementation may slightly differ.
|
|
4
|
+
|
|
5
|
+
[Issues](https://github.com/dsherret/ts-morph/issues?utf8=%E2%9C%93&q=label%3A%22requires+proposal%22+)
|
|
6
|
+
|
|
7
|
+
## Implemented
|
|
8
|
+
|
|
9
|
+
- [RFC-0001](RFC-0001 - Inserting Into Statements Handling Comments.md) - Inserting into statements handling comments.
|
|
10
|
+
|
|
11
|
+
## In Progress
|
|
12
|
+
|
|
13
|
+
- None
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
# RFC-0001 - Inserting Into Statements Handling Comments
|
|
2
|
+
|
|
3
|
+
Ported from: https://github.com/dsherret/ts-morph/issues/575
|
|
4
|
+
|
|
5
|
+
## Prerequisites
|
|
6
|
+
|
|
7
|
+
- Understand comment ownership: https://github.com/Microsoft/TypeScript/wiki/Architectural-Overview#trivia
|
|
8
|
+
- Current api is for inserting statements: `insertStatements(index: number, statements: string | WriterFunction | (string | WriterFunction)[]): Statement[];` where `index` is the `node.statements` array index of the node.
|
|
9
|
+
|
|
10
|
+
## Issues
|
|
11
|
+
|
|
12
|
+
- [#189](https://github.com/dsherret/ts-morph/issues/189) - Node inserted before comment when calling `addClass`-like methods
|
|
13
|
+
- [#565](https://github.com/dsherret/ts-morph/issues/565) - Inserting at a specific position
|
|
14
|
+
|
|
15
|
+
## Overview
|
|
16
|
+
|
|
17
|
+
The parser rightfully excludes comments from the AST in order to reduce complexity in the compiler. The compiler API then provides a way to get the comments via the `ts.getLeadingCommentRanges` and `ts.getTrailingCommentRanges` functions. These functions are provided the source file text and position to know where to start parsing at.
|
|
18
|
+
|
|
19
|
+
Although not so useful for the compilation process, comments are useful to know about for programmatic refactoring purposes in certain scenarios.
|
|
20
|
+
|
|
21
|
+
For example, given the following code:
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
// initialize
|
|
25
|
+
const u = 5;
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
How could one say to insert after the comment?
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
// initialize
|
|
32
|
+
const t = 4;
|
|
33
|
+
const u = 5;
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Or before?
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
const t = 4;
|
|
40
|
+
// this value is special
|
|
41
|
+
const u = 5;
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Right now, there doesn't exist an easy way to do this in ts-morph (possible by inserting with `insertText`, which takes a position).
|
|
45
|
+
|
|
46
|
+
## Option 1: Insert API Change
|
|
47
|
+
|
|
48
|
+
One option would be to change the API for `insertStatements` to take a position.
|
|
49
|
+
|
|
50
|
+
### Upsides
|
|
51
|
+
|
|
52
|
+
- Close functionality to the compiler API.
|
|
53
|
+
|
|
54
|
+
### Downsides
|
|
55
|
+
|
|
56
|
+
- API is not intuitive.
|
|
57
|
+
- Users confused why comments aren't a statement (had people ask about this multiple times).
|
|
58
|
+
- Users will need to parse for comments themselves in order to find out they're there.
|
|
59
|
+
|
|
60
|
+
## Option 2: Leading Comments as Statements
|
|
61
|
+
|
|
62
|
+
Given the following code:
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
// comment 1
|
|
66
|
+
/* comment 2 */
|
|
67
|
+
/* comment 3 */ const t = 4; // comment 4
|
|
68
|
+
const u = 5;
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Calling `.getStatementsWithComments()` would return any leading comments that are on a different line at the start of the line.
|
|
72
|
+
|
|
73
|
+
```
|
|
74
|
+
{
|
|
75
|
+
kind: ts.SyntaxKind.SingleLineCommentTrivia,
|
|
76
|
+
text: "// comment 1"
|
|
77
|
+
}, {
|
|
78
|
+
kind: ts.SyntaxKind.MultiLineCommentTrivia,
|
|
79
|
+
text: "/* comment 2 */"
|
|
80
|
+
}, {
|
|
81
|
+
kind: ts.SyntaxKind.VariableStatement,
|
|
82
|
+
// ...etc...
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
So in other words, trailing comments are never included as well as leading comments on the same line.
|
|
87
|
+
|
|
88
|
+
### JS Docs
|
|
89
|
+
|
|
90
|
+
JS docs will not be considered a statement unless they do not have a following declaration.
|
|
91
|
+
|
|
92
|
+
### Comment on same line as header
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
function getResult() { // description
|
|
96
|
+
return 5;
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
This comment should be ignored and considered as a trialing comment of the header. Most of the time these are a description of the header and not useful to know about for statemented purposes.
|
|
101
|
+
|
|
102
|
+
### No actual statements, but has comments
|
|
103
|
+
|
|
104
|
+
For example, given the following source file:
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
// comment 1
|
|
108
|
+
/* comment 2 */
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
It should return both comments when calling `#getStatementsWithComments()`.
|
|
112
|
+
|
|
113
|
+
### Multiple comments on same line
|
|
114
|
+
|
|
115
|
+
This is an edge case, but if someone does the following:
|
|
116
|
+
|
|
117
|
+
<!-- dprint-ignore -->
|
|
118
|
+
```ts
|
|
119
|
+
/* comment 1 */ // comment 2
|
|
120
|
+
/* comment 3 */ /* comment 4 */ // comment 5
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Then it should just return comment 1 and 3. Comment 2, 4, and 5 should return as a trailing comments of their respective comment nodes.
|
|
124
|
+
|
|
125
|
+
### Preceding comment on same line as close brace token
|
|
126
|
+
|
|
127
|
+
This comment should be parsed as if it's a member if it has no other token on the same line.
|
|
128
|
+
|
|
129
|
+
<!-- dprint-ignore -->
|
|
130
|
+
```ts
|
|
131
|
+
class C {
|
|
132
|
+
prop;
|
|
133
|
+
/* test */ }
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
The reason is that generally if someone writes this they would have meant to place it on the next line and so it shouldn't hide the comment.
|
|
137
|
+
|
|
138
|
+
### Other areas
|
|
139
|
+
|
|
140
|
+
It is also useful, but less so, to know about comments in nodes that have members (ex. class, interface, enum, object literal expression, type element expression, etc. members). In these cases, a separate `#getMembersWithComments()`/`#getPropertiesWithComments()`-like methods will be added.
|
|
141
|
+
|
|
142
|
+
### Node methods
|
|
143
|
+
|
|
144
|
+
One annoyance might be that standard `Node` methods like `#getStart()` aren't available. To fix this, the compiler node should actually be parsed to extend `ts.Node` then have an `ExtendedCommentNode` class that extends from `Node`.
|
|
145
|
+
|
|
146
|
+
### `#getPos()`
|
|
147
|
+
|
|
148
|
+
Behaviour should always stay the same as the compiler api. That means node positions may overlap comment positions (as already occurs). This behaviour is similar to JS doc comments.
|
|
149
|
+
|
|
150
|
+
### `#getChildren()`
|
|
151
|
+
|
|
152
|
+
`#getChildren()` should be updated in order to return these newly parsed comments.
|
|
153
|
+
|
|
154
|
+
Ultimately I have decided to break away from the compiler API here because the code becomes too complex when I don't consider these as children. Returning the comments with `#getChildren()` also ensures the comments are counted when calling `#getChildIndex()`.
|
|
155
|
+
|
|
156
|
+
### `#forEachChild`
|
|
157
|
+
|
|
158
|
+
`#forEachChild` will remain the same. It's better to keep this change as limited as possible.
|
|
159
|
+
|
|
160
|
+
### `#getChildIndex()`
|
|
161
|
+
|
|
162
|
+
This method will return the index in the result of `#getChildren()`.
|
|
163
|
+
|
|
164
|
+
### `#getChildCount()`
|
|
165
|
+
|
|
166
|
+
Should be updated to instead return the result of `#getChildren().length` (not use the compiler api value).
|
|
167
|
+
|
|
168
|
+
### Conditional behaviour?
|
|
169
|
+
|
|
170
|
+
Should this behaviour be conditional (ex. `parseComments: false` or something in the project options)? No, that would increase the complexity far too much.
|
|
171
|
+
|
|
172
|
+
### Benefits
|
|
173
|
+
|
|
174
|
+
- Users approaching the problem for the first time do not understand why comments are not considered statements. It might be more intuitive to do this.
|
|
175
|
+
- Makes it very easy to deal with comments and decide whether to insert before or after one (or remove them).
|
|
176
|
+
|
|
177
|
+
### Drawbacks
|
|
178
|
+
|
|
179
|
+
- Might confuse some users who are used to the compiler api behaviour when using `#getChildren()`.
|
|
180
|
+
- This is slightly unfortunate, but users would be even more confused about methods like `#getChildIndex()` not including comments. It would also increase complexity internally within the library.
|
|
181
|
+
- Most users won't have any experience with the compiler API so this is probably not an issue.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es2022",
|
|
4
|
+
"module": "CommonJS",
|
|
5
|
+
"strict": true,
|
|
6
|
+
"sourceMap": false,
|
|
7
|
+
"removeComments": false,
|
|
8
|
+
"experimentalDecorators": false,
|
|
9
|
+
"forceConsistentCasingInFileNames": true,
|
|
10
|
+
"esModuleInterop": true,
|
|
11
|
+
"noFallthroughCasesInSwitch": true,
|
|
12
|
+
"noImplicitReturns": true,
|
|
13
|
+
"newLine": "lf",
|
|
14
|
+
"importHelpers": false
|
|
15
|
+
},
|
|
16
|
+
"compileOnSave": false
|
|
17
|
+
}
|
package/vitest.config.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { defineConfig } from 'vitest/config'
|
|
2
|
+
|
|
3
|
+
export default defineConfig({
|
|
4
|
+
test: {
|
|
5
|
+
globals: true,
|
|
6
|
+
environment: 'node',
|
|
7
|
+
include: ['tests/**/*.test.ts'],
|
|
8
|
+
coverage: {
|
|
9
|
+
provider: 'v8',
|
|
10
|
+
reporter: ['text', 'json', 'html'],
|
|
11
|
+
include: ['src/**/*.ts'],
|
|
12
|
+
exclude: ['src/cli.ts', 'src/index.ts'],
|
|
13
|
+
},
|
|
14
|
+
testTimeout: 10000,
|
|
15
|
+
},
|
|
16
|
+
})
|