@yumemi-inc/statictrace
Advanced tools
Comparing version 1.0.0-beta.14 to 1.0.0-beta.15
{ | ||
"name": "@yumemi-inc/statictrace", | ||
"version": "1.0.0-beta.14", | ||
"version": "1.0.0-beta.15", | ||
"description": "A library for semi-automatic static testing.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -7,6 +7,12 @@ # statictrace | ||
## Usage | ||
## Installation | ||
įžå¨ã¯ããŊãŧãšããããĢãããäģĨå¤ãŽäŊŋį¨æšæŗããããžããã | ||
### Install | ||
`npm install @yumemi-inc/statictrace` | ||
`statictrace`ã¯ããã¸ã§ã¯ããã¨ãĢã¤ãŗãšããŧãĢã§ããããã°ããŧããĢãĒãããąãŧã¸ã¨ããĻãã¤ãŗãšããŧãĢã§ããžãã | ||
### Build from source | ||
``` | ||
@@ -17,13 +23,12 @@ pnpm install | ||
### Use as CLI | ||
### `ts-node` | ||
``` | ||
pnpm run build | ||
pnpm run parse -- -p /absolute/path/to/tsconfig.json | ||
`pnpx ts-node src/lib.ts -p /absolute/path/to/tsconfig.json` | ||
``` | ||
`ts-node`ãŽå ´å: | ||
### Use as CLI | ||
``` | ||
`pnpx ts-node src/lib.ts -p /absolute/path/to/tsconfig.json` | ||
statictrace -- -p /absolute/path/to/tsconfig.json | ||
``` | ||
@@ -33,2 +38,6 @@ | ||
``` | ||
TS_PROJECT_CONFIG=/absolute/path/to/tsconfig.json | ||
``` | ||
**ããŽäģãŽãĒããˇã§ãŗ** | ||
@@ -38,6 +47,62 @@ | ||
### äŊŋãæš | ||
`statictrace`ã¯ãéįēč ãįšæŽãĒãŗãĄãŗãã§ããŗãããįŽæãããŗãŧããŽéįãĒåæãã¯ããããäžãã°äģĨä¸ãŽãããĢįģé˛ãããŧãåæãããããĢããããŧãã¯ããžããã¨ã襨ãããŗã (`@entrypoint ãããŧãŽåå`) ãéĸæ°ãĢ JSDoc åŊĸåŧã§čŋŊå ããžãã | ||
```ts | ||
/** | ||
* @entrypoint Registration | ||
*/ | ||
function startRegistration() { | ||
processRegistration(); | ||
finishRegistration(); | ||
untracedFunction(); | ||
cleanupSomething(); | ||
} | ||
``` | ||
TS_PROJECT_CONFIG=/absolute/path/to/tsconfig.json | ||
ããã ãã§ã¯äŊããĸãĻãããããããĒããã`statictrace` 㯠`startRegistration()` ãŽä¸ã§åŧã°ããéĸæ°ããĄãŊãããããšãĻææĄããžããéįēč ãããšãããããĨãĄãŗããŽįŽįã§ãåŧãŗåēããŽæįĄãģé įĒãģčĻĒåéĸäŋãč¨é˛ãããå ´åã¯ãæ°ãĢãĒãéĸæ°ã¨ãĄãŊããã ãåĨãŽįšæŽãĒããŗãã§ããŧã¯ã§ãããããŽããŗã㯠`@trace`ã | ||
```ts | ||
/** @trace */ | ||
function processRegistration() { | ||
someRegistrationProcedure(); | ||
} | ||
``` | ||
ãã㧠`statictrace` ãåŽčĄããã¨äģĨä¸ãŽãĸãĻãããããåžãããīŧ | ||
``` | ||
Entrypoint: Registration | ||
startRegistration | ||
processRegistration | ||
someRegistrationProcedure | ||
``` | ||
ããŽãĸãĻããããããšããããˇã§ãããŽãããĢäŊŋãŖãĻãåĨŊãŋãŽããšããŠã¤ããŠãĒã§ãĒããĄã¯ãŋãĒãŗã°åã¨åžãŽåˇŽåã§ãããŧãå¤ããŖãĻããĒããã¨ãäŋč¨ŧã§ããããžã㯠[`mermaid`](https://mermaid-js.github.io/mermaid/#/) ã¨ããĻįĩæããĸãĻããããã§ããīŧäģĨä¸ãĢįģåãããīŧã | ||
### äž | ||
- ããããŦãŧãŽãšãŋãã¯ããŦãŧãšãŽãããĢã¤ãŗããŗãããããããšãã¨ããĻãĸãĻããããããīŧ | ||
```sh | ||
$ statictrace | ||
======================= | ||
Entrypoint: SomeEntrypoint | ||
begin | ||
funcA | ||
funcC | ||
beingNestedEntrypoint | ||
funcA | ||
funcC | ||
funcB | ||
funcB | ||
``` | ||
- mermaid ã°ãŠãã¨ããĻããŧã¯ããĻãŗããĄã¤ãĢãĢãĸãĻãããããã: `statictrace -u mermaid > graphs.md` | ||
ããŽå ´åãŽã°ãŠããäģĨä¸ãŽãããĢ襨į¤ēãããžãīŧ | ||
![mermaid](./assets/mermaid.png) | ||
### Use API programmatically | ||
@@ -47,14 +112,8 @@ | ||
const { run } = require('./build/lib'); | ||
const output = run('/absolute/path/to/tsconfig.json'); | ||
const output = run('/absolute/path/to/tsconfig.json', 'text'); | ||
// ...do something with output | ||
``` | ||
#### `run(pathToTsConfig: string, printer?: Printer): any` | ||
#### `run(pathToTsConfig: string, printerType: "text" | "mermaid"): any` | ||
å ¨ãĻãŽããã¸ã§ã¯ããŽããĄã¤ãĢãããŧããã`@entrypoint`ã`@trace`ã§ããŧã¯ãããĻããå ¨ãĻãŽéĸæ°ãŽãŗãŧãĢãŽã°ãŠããäŊãã`Printer`ã¯ãã°ãŠããããĒãŗãīŧãĒããããŽæšæŗã§čĄ¨į¤ēã§ããīŧãã¨ã襨ãã¤ãŗãŋãŧãã§ãŧãšã§ãããŽã¤ãŗãŋãŧãã§ãŧãšã implement ããããŽãĒããĒãã§ããĒããˇã§ããĢãĒåŧæ°ã¨ããĻæ¸Ąããã¨ãã§ããžããäŊãæ¸ĄããĒããŖãå ´åã¯ãč§ŖæãŽįĩæã`TextPrinter`ãæååã¨ããĻæģããžãã | ||
```ts | ||
interface Printer { | ||
print(graph: Into<Printable>): any; | ||
} | ||
``` | ||
å ¨ãĻãŽããã¸ã§ã¯ããŽããĄã¤ãĢãããŧããã`@entrypoint`ã`@trace`ã§ããŧã¯ãããĻããå ¨ãĻãŽéĸæ°ãŽãŗãŧãĢãŽã°ãŠããäŊããįŦŦäēãŽåŧæ°ã¨ããĻããĒãŗãŋãŧãŋã¤ããæåŽãããããĒãŗãŋãŧã¨ãããŽã¯éįåæãŽįĩæããĒããããŽåŊĸã§čĄ¨į¤ēã§ããã¤ãŗãŋãŧãã§ãŧãšãæããžããįžå¨ãįŦčĒãŽåŽčŖ ã¯ä¸å¯čŊã§ããããŠãĢããŽãŋã¤ãããé¸æããåŋ čĻãããã |
@@ -9,6 +9,12 @@ # statictrace | ||
## Usage | ||
## Installation | ||
Currently you can only use this tool by building it from source. | ||
### Install | ||
`npm install @yumemi-inc/statictrace` | ||
You can install `statictrace` either per-project or globally. | ||
### Build from source | ||
``` | ||
@@ -19,6 +25,6 @@ pnpm install | ||
With `ts-node`: | ||
### `ts-node`: | ||
``` | ||
`pnpx ts-node src/lib.ts -p /absolute/path/to/tsconfig.json` | ||
pnpx ts-node src/lib.ts -p /absolute/path/to/tsconfig.json | ||
``` | ||
@@ -35,2 +41,6 @@ | ||
``` | ||
TS_PROJECT_CONFIG=/absolute/path/to/tsconfig.json | ||
``` | ||
**Other options** | ||
@@ -40,10 +50,63 @@ | ||
**Examples** | ||
## Usage | ||
- Output the result printed as mermaid graphs to a markdown file: `pnpm run parse -- -u mermaid > graphs.md` | ||
`statictrace` begins static analysis of your code from a point that is explicitly hinted by a developer. For example, if you want to analyse the registration flow like below, you need to add a JSDoc hint to the function where the flow begins: `@entrypoint YourFlowName`. | ||
```ts | ||
/** | ||
* @entrypoint Registration | ||
*/ | ||
function startRegistration() { | ||
processRegistration(); | ||
finishRegistration(); | ||
untracedFunction(); | ||
cleanupSomething(); | ||
} | ||
``` | ||
TS_PROJECT_CONFIG=/absolute/path/to/tsconfig.json | ||
Just doing this produces no output but `statictrace` internally tracks all function and method calls that occur within `startRegistration()` and every function calls within those functions until there are no calls. In other words, it builds a static stacktrace. If there are any particular functions and methods that you want to test or document, for example to know whether some functions are called, their call order and parent/child relationship, you need to mark relevant functions with another special comment: `@trace`. | ||
```ts | ||
/** @trace */ | ||
function processRegistration() { | ||
someRegistrationProcedure(); | ||
} | ||
``` | ||
With this `statictrace` produces the following output: | ||
``` | ||
Entrypoint: Registration | ||
startRegistration | ||
processRegistration | ||
someRegistrationProcedure | ||
``` | ||
You can use this output as a snapshot of a stacktrace, and use it from your testing library of choice to guarantee that the flow does not change after e.g. refactoring. You can also output the stacktrace as [`mermaid`](https://mermaid-js.github.io/mermaid/#/) graphs for documentation purposes (see picture below). | ||
## Examples | ||
- Output the result as indented text similar to a debugger stacktrace: | ||
```sh | ||
$ statictrace | ||
======================= | ||
Entrypoint: SomeEntrypoint | ||
begin | ||
funcA | ||
funcC | ||
beingNestedEntrypoint | ||
funcA | ||
funcC | ||
funcB | ||
funcB | ||
``` | ||
- Output the result printed as mermaid graphs to a markdown file: `statictrace -u mermaid > graphs.md` | ||
This is how rendered mermaid graphs look like: | ||
![mermaid](./assets/mermaid.png) | ||
### Use API programmatically | ||
@@ -53,3 +116,3 @@ | ||
const { run } = require('./build/lib'); | ||
const output = run('/absolute/path/to/tsconfig.json'); | ||
const output = run('/absolute/path/to/tsconfig.json', 'text'); | ||
// ...do something with output | ||
@@ -56,0 +119,0 @@ ``` |
19744
119