|
1 |
| -# SimpleLanguageService [![NPM version][npm-image]][npm-url] |
2 |
| -> A service that parses and reflects on the AST generated by Typescript's language service. |
3 |
| -> With it, we can extract metadata such as initialization values and types, arguments and import declarations. |
| 1 | +# CodeAnalyzer [![NPM version][npm-image]][npm-url] |
| 2 | +> A service that can analyze your code in great detail ahead of time. |
4 | 3 |
|
5 | 4 | ## Installation
|
6 |
| -Simply do: `npm install simplelanguageservice`. |
| 5 | +Simply do: `npm install @wessberg/codeanalyzer`. |
| 6 | + |
| 7 | +## Description |
| 8 | +The service is a very flexible and powerful tool for extracting metadata and state snapshots of your code and the identifiers that lives within it. |
| 9 | + |
| 10 | +It builds upon the Typescript AST and understands the entirety of the Typescript syntax. |
| 11 | + |
| 12 | +Here's *some* of what SimpleLanguageService does: |
| 13 | + |
| 14 | +- It computes the initialization values of variables and return values of function calls - ahead of time. |
| 15 | +- It tracks the types of class fields, function/method/constructor parameters, generic call- or new expression arguments and similar. |
| 16 | +- It breaks code up into expressions that can be resolved at any time. |
| 17 | + |
| 18 | +For example, |
| 19 | +consider the following code: |
| 20 | +```typescript |
| 21 | +const service = new CodeAnalyzer(); |
| 22 | +const fileName = "a_file.ts"; |
| 23 | +service.addFile(fileName, ` |
| 24 | + function foo () { |
| 25 | + let arr = []; |
| 26 | + for (let i = 1; i <= 5; i++) { |
| 27 | + arr.push(i); |
| 28 | + } |
| 29 | + return arr; |
| 30 | + } |
| 31 | + const aVariable: number[] = foo(); |
| 32 | +`); |
| 33 | + |
| 34 | +const variables = service.getVariableAssignmentsForFile(fileName); |
| 35 | +const variable = variables["aVariable"]; |
| 36 | +console.log(variable.value.resolve()); // [1,2,3,4,5] |
| 37 | +console.log(variable.name); // aVariable |
| 38 | +console.log(variable.type.flattened); // number[] |
| 39 | +``` |
| 40 | + |
| 41 | +This is useful if you want to reduce complexity by replacing heavy function calls with values. |
| 42 | + |
| 43 | +But more than that, it can |
7 | 44 |
|
8 | 45 | ## Usage
|
9 | 46 | ```typescript
|
@@ -72,5 +109,5 @@ The LanguageService will not track any mutations for already-initialized variabl
|
72 | 109 |
|
73 | 110 | - First release.
|
74 | 111 |
|
75 |
| -[npm-url]: https://npmjs.org/package/@wessberg/simplelanguageservice |
76 |
| -[npm-image]: https://badge.fury.io/js/@wessberg/simplelanguageservice.svg |
| 112 | +[npm-url]: https://npmjs.org/package/@wessberg/codeanalyzer |
| 113 | +[npm-image]: https://badge.fury.io/js/@wessberg/codeanalyzer.svg |
0 commit comments