Skip to content

Commit 88878ff

Browse files
committed
clean up
1 parent 761a65e commit 88878ff

File tree

2 files changed

+51
-242
lines changed

2 files changed

+51
-242
lines changed

index.js

Lines changed: 25 additions & 216 deletions
Original file line numberDiff line numberDiff line change
@@ -20,208 +20,13 @@ const { utils } = lib;
2020
*/
2121

2222
class Comments extends Emitter {
23-
constructor(options) {
23+
constructor(options = {}) {
2424
super();
25-
this.options = Object.assign({}, options);
25+
this.options = options;
2626
this.comments = [];
27-
this.ast = {};
28-
this.cache = {
29-
comments: {},
30-
files: {},
31-
links: {}
32-
};
33-
3427
this.parsers = {};
3528
this.tokens = [];
36-
this.plugins = {
37-
fns: [],
38-
middleware: {},
39-
before: {},
40-
after: {}
41-
};
42-
}
43-
44-
/**
45-
* Register a parser function of the given `type`
46-
*
47-
* @param {string|object} `type`
48-
* @param {Function} `fn`
49-
* @return {Object}
50-
* @api public
51-
*/
52-
53-
parser(type, fn) {
54-
this.parsers[type] = fn;
55-
return this;
56-
}
57-
58-
/**
59-
* Register a compiler plugin `fn`. Plugin functions should take an
60-
* options object, and return a function that takes an instance of
61-
* comments.
62-
*
63-
* ```js
64-
* // plugin example
65-
* function yourPlugin(options) {
66-
* return function(comments) {
67-
* // do stuff
68-
* };
69-
* }
70-
* // usage
71-
* comments.use(yourPlugin());
72-
* ```
73-
*
74-
* @param {Function} `fn` plugin function
75-
* @return {Object} Returns the comments instance for chaining.
76-
* @api public
77-
*/
78-
79-
use(fn) {
80-
this.plugins.fns = this.plugins.fns.concat(fn);
81-
return this;
82-
}
83-
84-
/**
85-
* Register a handler function to be called on a node of the given `type`.
86-
* Override a built-in handler `type`, or register a new type.
87-
*
88-
* ```js
89-
* comments.set('param', function(node) {
90-
* // do stuff to node
91-
* });
92-
* ```
93-
* @param {String} `type` The `node.type` to call the handler on. You can override built-in middleware by registering a handler of the same name, or register a handler for rendering a new type.
94-
* @param {Function} `fn` The handler function
95-
* @return {Object} Returns the instance for chaining.
96-
* @api public
97-
*/
98-
99-
set(type, fn) {
100-
if (Array.isArray(type)) {
101-
for (let i = 0; i < type.length; i++) {
102-
this.set(type[i], fn);
103-
}
104-
} else {
105-
this.plugins.middleware[type] = fn;
106-
}
107-
return this;
108-
}
109-
110-
/**
111-
* Register a handler that will be called by the compiler on every node
112-
* of the given `type`, _before other middleware are called_ on that node.
113-
*
114-
* ```js
115-
* comments.before('param', function(node) {
116-
* // do stuff to node
117-
* });
118-
*
119-
* // or
120-
* comments.before(['param', 'returns'], function(node) {
121-
* // do stuff to node
122-
* });
123-
*
124-
* // or
125-
* comments.before({
126-
* param: function(node) {
127-
* // do stuff to node
128-
* },
129-
* returns: function(node) {
130-
* // do stuff to node
131-
* }
132-
* });
133-
* ```
134-
* @param {String|Object|Array} `type` Handler name(s), or an object of middleware
135-
* @param {Function} `fn` Handler function, if `type` is a string or array. Otherwise this argument is ignored.
136-
* @return {Object} Returns the instance for chaining.
137-
* @api public
138-
*/
139-
140-
before(type, fn) {
141-
let before = this.plugins.before;
142-
if (utils.isObject(type)) {
143-
for (let key of Object.keys(type)) {
144-
this.before(key, type[key]);
145-
}
146-
} else if (Array.isArray(type)) {
147-
for (let key of type) this.before(key, fn);
148-
} else {
149-
before[type] = before[type] || [];
150-
before[type].push(fn);
151-
}
152-
return this;
153-
}
154-
155-
/**
156-
* Register a handler that will be called by the compiler on every node
157-
* of the given `type`, _after other middleware are called_ on that node.
158-
*
159-
* ```js
160-
* comments.after('param', function(node) {
161-
* // do stuff to node
162-
* });
163-
*
164-
* // or
165-
* comments.after(['param', 'returns'], function(node) {
166-
* // do stuff to node
167-
* });
168-
*
169-
* // or
170-
* comments.after({
171-
* param: function(node) {
172-
* // do stuff to node
173-
* },
174-
* returns: function(node) {
175-
* // do stuff to node
176-
* }
177-
* });
178-
* ```
179-
* @param {String|Object|Array} `type` Handler name(s), or an object of middleware
180-
* @param {Function} `fn` Handler function, if `type` is a string or array. Otherwise this argument is ignored.
181-
* @return {Object} Returns the instance for chaining.
182-
* @api public
183-
*/
184-
185-
after(type, fn) {
186-
let after = this.plugins.after;
187-
if (utils.isObject(type)) {
188-
for (let key of Object.keys(type)) {
189-
this.after(key, type[key]);
190-
}
191-
} else if (Array.isArray(type)) {
192-
for (let key of type) this.after(key, fn);
193-
} else {
194-
after[type] = after[type] || [];
195-
after[type].push(fn);
196-
}
197-
return this;
198-
}
199-
200-
/**
201-
* Run plugin functions on a node of the given `type`.
202-
*
203-
* @param {String} `type` Either `before` or `after`
204-
* @param {Object} `compiler` Snapdragon compiler instance
205-
* @return {Function} Returns a function that takes a `node`. Any plugins registered for that `node.type` will be run on the node.
206-
*/
207-
208-
run(type, compiler) {
209-
let plugins = this.plugins[type];
210-
211-
return node => {
212-
let fns = plugins[node.type] || [];
213-
214-
for (let fn of fns) {
215-
if (typeof fn !== 'function') {
216-
let err = new TypeError('expected plugin to be a function:' + fn);
217-
err.node = node;
218-
err.type = type;
219-
throw err;
220-
}
221-
node = fn.call(compiler, node) || node;
222-
}
223-
return node;
224-
};
29+
this.ast = {};
22530
}
22631

22732
/**
@@ -282,7 +87,7 @@ class Comments extends Emitter {
28287

28388
parseComment(comment, options) {
28489
let opts = Object.assign({}, this.options, options);
285-
let parsers = Object.assign({}, this.plugins.middleware, opts.parse);
90+
let parsers = opts.parse || {};
28691

28792
if (typeof parsers.comment === 'function') {
28893
comment = parsers.comment.call(this, comment, opts);
@@ -297,11 +102,6 @@ class Comments extends Emitter {
297102
comment = Object.assign({}, comment, tok);
298103
lib.normalize.examples(comment, opts);
299104
comment.tags = this.parseTags(comment, options);
300-
301-
// let name = get(comment, 'code.context.name');
302-
// if (name) {
303-
// set(this.cache, name, comment);
304-
// }
305105
}
306106

307107
// parse inline tags
@@ -338,7 +138,7 @@ class Comments extends Emitter {
338138

339139
parseTags(comment, options) {
340140
let opts = Object.assign({}, this.options, options);
341-
let parsers = Object.assign({}, this.plugins.middleware, opts.parse);
141+
let parsers = opts.parse || {};
342142
let tags = [];
343143

344144
if (typeof parsers.parseTags === 'function') {
@@ -374,7 +174,7 @@ class Comments extends Emitter {
374174

375175
parseTag(tok, options) {
376176
let opts = Object.assign({}, this.options, options);
377-
let parsers = Object.assign({}, this.plugins.middleware, opts.parse);
177+
let parsers = opts.parse || {};
378178
let tag;
379179

380180
if (typeof tok === 'string') {
@@ -452,7 +252,7 @@ class Comments extends Emitter {
452252
}
453253

454254
let opts = { ...this.options, ...options };
455-
let parsers = { ...this.plugins.middleware, ...opts.parse };
255+
let parsers = opts.parse || {};
456256

457257
if (typeof parsers.inlineTag === 'function') {
458258
return parsers.inlineTag.call(this, str, opts);
@@ -481,7 +281,7 @@ class Comments extends Emitter {
481281
}
482282

483283
let opts = { ...this.options, ...options };
484-
let parsers = { ...this.plugins.middleware, ...opts.parse };
284+
let parsers = opts.parse || {};
485285

486286
if (typeof parsers.type === 'function') {
487287
return parsers.type.call(this, str, tag, opts);
@@ -497,7 +297,7 @@ class Comments extends Emitter {
497297
}
498298

499299
let opts = { ...this.options, ...options };
500-
let parsers = { ...this.plugins.middleware, ...opts.parse };
300+
let parsers = opts.parse || {};
501301

502302
if (typeof parsers.paramType === 'function') {
503303
return parsers.paramType.call(this, str, opts);
@@ -558,10 +358,10 @@ class Comments extends Emitter {
558358
}
559359

560360
/**
561-
* Returns true if the given `comment` is valid. By default, comments
562-
* are considered valid when they begin with `/**`, and do not contain
563-
* `jslint`, `jshint`, `eshint`, or `eslint`. A custom `isValid` function may be
564-
* passed on the constructor options.
361+
* Returns true if the given `comment` is valid (meaning the comment
362+
* may be parsed). Comments are considered valid when they begin with
363+
* `/**`, and do not contain `jslint`, `jshint`, `eshint`, or `eslint`.
364+
* A custom `isValid` function may be passed on the constructor options.
565365
*
566366
* @param {Object} `comment`
567367
* @param {Object} `options`
@@ -582,12 +382,21 @@ class Comments extends Emitter {
582382
if (!utils.isValidBlockComment(comment, options)) {
583383
return false;
584384
}
385+
return true;
386+
}
585387

586-
if (opts.protected === false && utils.isProtectedComment(comment.raw)) {
587-
return false;
388+
isConfigComment(comment, names) {
389+
if (!utils.isObject(comment)) {
390+
throw new TypeError('expected comment to be an object');
588391
}
392+
return utils.isConfigComment(comment.value, names);
393+
}
589394

590-
return !utils.isConfigComment(comment.value);
395+
isProtectedComment(comment) {
396+
if (!utils.isObject(comment)) {
397+
throw new TypeError('expected comment to be an object');
398+
}
399+
return utils.isProtectedComment(comment.raw);
591400
}
592401

593402
static parse(str, options) {

0 commit comments

Comments
 (0)