1import * as common from '../common/index.mjs';
2import * as fixtures from '../common/fixtures.mjs';
3
4import assert from 'assert';
5import fs from 'fs';
6
7import * as json from '../../tools/doc/json.mjs';
8import {
9  remarkParse,
10  unified,
11} from '../../tools/doc/deps.mjs';
12
13function toJSON(input, filename, cb) {
14  function nullCompiler() {
15    this.Compiler = (tree) => tree;
16  }
17
18  unified()
19    .use(remarkParse)
20    .use(json.jsonAPI, { filename })
21    .use(nullCompiler)
22    .process(input, cb);
23}
24
25// Outputs valid json with the expected fields when given simple markdown
26// Test data is a list of objects with two properties.
27// The file property is the file path.
28// The json property is some json which will be generated by the doctool.
29const testData = [
30  {
31    file: fixtures.path('sample_document.md'),
32    json: {
33      type: 'module',
34      source: 'foo',
35      modules: [{
36        textRaw: 'Sample Markdown',
37        name: 'sample_markdown',
38        modules: [{
39          textRaw: 'Seussian Rhymes',
40          name: 'seussian_rhymes',
41          desc: '<ol>\n<li>fish</li>\n<li>fish</li>\n</ol>\n' +
42                  '<ul>\n<li>Red fish</li>\n<li>Blue fish</li>\n</ul>',
43          type: 'module',
44          displayName: 'Seussian Rhymes',
45        }],
46        type: 'module',
47        displayName: 'Sample Markdown',
48      }],
49    },
50  },
51  {
52    file: fixtures.path('order_of_end_tags_5873.md'),
53    json: {
54      type: 'module',
55      source: 'foo',
56      modules: [{
57        textRaw: 'Title',
58        name: 'title',
59        modules: [{
60          textRaw: 'Subsection',
61          name: 'subsection',
62          classMethods: [{
63            textRaw: 'Static method: Buffer.from(array)',
64            type: 'classMethod',
65            name: 'from',
66            signatures: [
67              {
68                params: [{
69                  textRaw: '`array` {Array}',
70                  name: 'array',
71                  type: 'Array',
72                }],
73              },
74            ],
75          }],
76          type: 'module',
77          displayName: 'Subsection',
78        }],
79        type: 'module',
80        displayName: 'Title',
81      }],
82    },
83  },
84  {
85    file: fixtures.path('doc_with_yaml.md'),
86    json: {
87      type: 'module',
88      source: 'foo',
89      modules: [
90        {
91          textRaw: 'Sample Markdown with YAML info',
92          name: 'sample_markdown_with_yaml_info',
93          modules: [
94            {
95              textRaw: 'Foobar',
96              name: 'foobar',
97              meta: {
98                added: ['v1.0.0'],
99                changes: [],
100              },
101              desc: '<p>Describe <code>Foobar</code> in more detail ' +
102                'here.</p>',
103              type: 'module',
104              displayName: 'Foobar',
105            },
106            {
107              textRaw: 'Foobar II',
108              name: 'foobar_ii',
109              meta: {
110                added: ['v5.3.0', 'v4.2.0'],
111                changes: [
112                  { 'version': 'v4.2.0',
113                    'pr-url': 'https://github.com/nodejs/node/pull/3276',
114                    'description': 'The `error` parameter can now be ' +
115                      'an arrow function.' },
116                ],
117              },
118              desc: '<p>Describe <code>Foobar II</code> in more detail ' +
119                'here. fg(1)</p>',
120              type: 'module',
121              displayName: 'Foobar II',
122            },
123            {
124              textRaw: 'Deprecated thingy',
125              name: 'deprecated_thingy',
126              meta: {
127                added: ['v1.0.0'],
128                deprecated: ['v2.0.0'],
129                changes: [],
130              },
131              desc: '<p>Describe <code>Deprecated thingy</code> in more ' +
132                'detail here. fg(1p)</p>',
133              type: 'module',
134              displayName: 'Deprecated thingy',
135            },
136            {
137              textRaw: 'Something',
138              name: 'something',
139              desc: '<!-- This is not a metadata comment -->\n<p>' +
140                'Describe <code>Something</code> in more detail here.</p>',
141              type: 'module',
142              displayName: 'Something',
143            },
144          ],
145          type: 'module',
146          displayName: 'Sample Markdown with YAML info',
147        },
148      ],
149    },
150  },
151  {
152    file: fixtures.path('doc_with_backticks_in_headings.md'),
153    json: {
154      type: 'module',
155      source: 'foo',
156      modules: [
157        {
158          textRaw: 'Fhqwhgads',
159          name: 'fhqwhgads',
160          properties: [
161            {
162              name: 'fullName',
163              textRaw: '`Fqhqwhgads.fullName`',
164            },
165          ],
166          classMethods: [
167            {
168              name: 'again',
169              signatures: [
170                {
171                  params: [],
172                },
173              ],
174              textRaw: 'Static method: `Fhqwhgads.again()`',
175              type: 'classMethod',
176            },
177          ],
178          classes: [
179            {
180              textRaw: 'Class: `ComeOn`',
181              type: 'class',
182              name: 'ComeOn',
183            },
184          ],
185          ctors: [
186            {
187              name: 'Fhqwhgads',
188              signatures: [
189                {
190                  params: [],
191                },
192              ],
193              textRaw: 'Constructor: `new Fhqwhgads()`',
194              type: 'ctor',
195            },
196          ],
197          methods: [
198            {
199              textRaw: '`everybody.to(limit)`',
200              type: 'method',
201              name: 'to',
202              signatures: [{ params: [] }],
203            },
204          ],
205          events: [
206            {
207              textRaw: "Event: `'FHQWHfest'`",
208              type: 'event',
209              name: 'FHQWHfest',
210              params: [],
211            },
212          ],
213          type: 'module',
214          displayName: 'Fhqwhgads',
215        },
216      ],
217    },
218  },
219];
220
221testData.forEach((item) => {
222  fs.readFile(item.file, 'utf8', common.mustSucceed((input) => {
223    toJSON(input, 'foo', common.mustSucceed((output) => {
224      assert.deepStrictEqual(output.json, item.json);
225    }));
226  }));
227});
228