1 | # jest-docblock
|
2 |
|
3 | `jest-docblock` is a package that can extract and parse a specially-formatted comment called a "docblock" at the top of a file.
|
4 |
|
5 | A docblock looks like this:
|
6 |
|
7 | ```js
|
8 | /**
|
9 | * Stuff goes here!
|
10 | */
|
11 | ```
|
12 |
|
13 | Docblocks can contain pragmas, which are words prefixed by `@`:
|
14 |
|
15 | ```js
|
16 | /**
|
17 | * Pragma incoming!
|
18 | *
|
19 | * @flow
|
20 | */
|
21 | ```
|
22 |
|
23 | Pragmas can also take arguments:
|
24 |
|
25 | ```js
|
26 | /**
|
27 | * Check this out:
|
28 | *
|
29 | * @myPragma it is so cool
|
30 | */
|
31 | ```
|
32 |
|
33 | `jest-docblock` can:
|
34 |
|
35 | - extract the docblock from some code as a string
|
36 | - parse a docblock string's pragmas into an object
|
37 | - print an object and some comments back to a string
|
38 |
|
39 | ## Installation
|
40 |
|
41 | ```sh
|
42 | # with yarn
|
43 | $ yarn add jest-docblock
|
44 | # with npm
|
45 | $ npm install jest-docblock
|
46 | ```
|
47 |
|
48 | ## Usage
|
49 |
|
50 | ```js
|
51 | const code = `
|
52 | /**
|
53 | * Everything is awesome!
|
54 | *
|
55 | * @everything is:awesome
|
56 | * @flow
|
57 | */
|
58 |
|
59 | export const everything = Object.create(null);
|
60 | export default function isAwesome(something) {
|
61 | return something === everything;
|
62 | }
|
63 | `;
|
64 |
|
65 | const {
|
66 | extract,
|
67 | strip,
|
68 | parse,
|
69 | parseWithComments,
|
70 | print,
|
71 | } = require('jest-docblock');
|
72 |
|
73 | const docblock = extract(code);
|
74 | console.log(docblock); // "/**\n * Everything is awesome!\n * \n * @everything is:awesome\n * @flow\n */"
|
75 |
|
76 | const stripped = strip(code);
|
77 | console.log(stripped); // "export const everything = Object.create(null);\n export default function isAwesome(something) {\n return something === everything;\n }"
|
78 |
|
79 | const pragmas = parse(docblock);
|
80 | console.log(pragmas); // { everything: "is:awesome", flow: "" }
|
81 |
|
82 | const parsed = parseWithComments(docblock);
|
83 | console.log(parsed); // { comments: "Everything is awesome!", pragmas: { everything: "is:awesome", flow: "" } }
|
84 |
|
85 | console.log(print({pragmas, comments: 'hi!'})); // /**\n * hi!\n *\n * @everything is:awesome\n * @flow\n */;
|
86 | ```
|
87 |
|
88 | ## API Documentation
|
89 |
|
90 | ### `extract(contents: string): string`
|
91 |
|
92 | Extracts a docblock from some file contents. Returns the docblock contained in `contents`. If `contents` did not contain a docblock, it will return the empty string (`""`).
|
93 |
|
94 | ### `strip(contents: string): string`
|
95 |
|
96 | Strips the top docblock from a file and return the result. If a file does not have a docblock at the top, then return the file unchanged.
|
97 |
|
98 | ### `parse(docblock: string): {[key: string]: string | string[] }`
|
99 |
|
100 | Parses the pragmas in a docblock string into an object whose keys are the pragma tags and whose values are the arguments to those pragmas.
|
101 |
|
102 | ### `parseWithComments(docblock: string): { comments: string, pragmas: {[key: string]: string | string[]} }`
|
103 |
|
104 | Similar to `parse` except this method also returns the comments from the docblock. Useful when used with `print()`.
|
105 |
|
106 | ### `print({ comments?: string, pragmas?: {[key: string]: string | string[]} }): string`
|
107 |
|
108 | Prints an object of key-value pairs back into a docblock. If `comments` are provided, they will be positioned on the top of the docblock.
|