UNPKG

3.34 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = extract;
7
8var _parse = require('./parse');
9
10var _parse2 = _interopRequireDefault(_parse);
11
12var _fs = require('fs');
13
14var _fs2 = _interopRequireDefault(_fs);
15
16var _babelTraverse = require('babel-traverse');
17
18var _babelTraverse2 = _interopRequireDefault(_babelTraverse);
19
20var _doctrine = require('doctrine');
21
22var _doctrine2 = _interopRequireDefault(_doctrine);
23
24var _babelTypes = require('babel-types');
25
26function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
27
28/**
29 * transform function which operates on each discovered example block
30 * @callback transformCallback
31 * @param {Object} options
32 * @param {ast} options.comment
33 * @param {string} options.name
34 * @param {string} options.filename
35 * @param {string} options.type
36 */
37
38/**
39 * Extracts comment blocks from the source code in the specified file
40 * @param {transformCallback} transform
41 * @param {string} filename
42 * @returns {Array<ast>}
43 */
44function extract(transform, filename) {
45 // eslint-disable-next-line no-sync
46 var code = _fs2.default.readFileSync(filename, { encoding: 'utf8' });
47
48 var ast = (0, _parse2.default)(code, { sourceFilename: filename });
49
50 var results = [];
51
52 var done = false;
53 (0, _babelTraverse2.default)(ast, {
54 enter: function enter(path) {
55 if (path.node.leadingComments) {
56 path.node.leadingComments.filter(isJSDocComment).forEach(function (comment) {
57 var result = _doctrine2.default.parse(comment.value, {
58 unwrap: true,
59 sloppy: true,
60 recoverable: true,
61 lineNumbers: true
62 });
63
64 if (result.tags) {
65 result.tags.forEach(function (tag) {
66 if (tag.title === 'example') {
67 results.push(transform({
68 comment: tag.description,
69 name: getNodeName(path.node),
70 filename: path.node.loc.filename,
71 type: path.node.type
72 }));
73 }
74 });
75 }
76 });
77 }
78 },
79
80 Program: {
81 exit: function exit(path) {
82 if ((0, _babelTypes.isProgram)(path)) {
83 if (done) {
84 return;
85 }
86 path.pushContainer('body', results);
87 done = true;
88 }
89 }
90 }
91 });
92
93 return ast;
94}
95
96/**
97 * Extracts the name from the specified node
98 * @param {Node} node
99 * @returns {string}
100 */
101/*!
102 * Copyright (c) 2015-2017 Cisco Systems, Inc. See LICENSE file.
103 */
104
105function getNodeName(node) {
106 if (node.id) {
107 return node.id.name;
108 }
109
110 if (node.key) {
111 return node.key.name;
112 }
113
114 throw new Error('Could not find name for node');
115}
116/**
117 * Indicates if the specified comment block is a doc block
118 * @param {CommentBlock} comment
119 * @returns {Boolean}
120 */
121function isJSDocComment(comment) {
122 var asterisks = comment.value.match(/^(\*+)/);
123 if (comment.value.startsWith('/*') && comment.value.endsWith('*/')) {
124 return false;
125 }
126
127 // eslint-disable-next-line
128 return (comment.type === 'CommentBlock' || // estree
129 // eslint-disable-next-line
130 comment.type === 'Block') && // get-comments / traditional
131 // eslint-disable-next-line
132 asterisks && asterisks[1].length === 1;
133}
134//# sourceMappingURL=extract.js.map