Skip to content

Commit 1a66c8f

Browse files
committed
reorganize
1 parent 8df520f commit 1a66c8f

27 files changed

+258
-822
lines changed

index.js

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
'use strict';
22

3-
// require('time-require');
4-
const assign = Object.assign;
53
const Emitter = require('events');
64
const extract = require('extract-comments');
75
const tokenize = require('tokenize-comment');
8-
const { expects, allows, format, validate, normalize, utils, parse } = require('./lib');
9-
const { typeOf, get, set } = utils;
6+
const lib = require('./lib');
7+
const { utils } = lib;
108

119
/**
1210
* Create an instance of `Comments` with the given `options`.
@@ -19,7 +17,7 @@ const { typeOf, get, set } = utils;
1917
class Comments extends Emitter {
2018
constructor(options) {
2119
super();
22-
this.options = assign({}, options);
20+
this.options = Object.assign({}, options);
2321
this.comments = [];
2422
this.ast = {};
2523
this.cache = {
@@ -235,7 +233,7 @@ class Comments extends Emitter {
235233
*/
236234

237235
tokenize(str, options) {
238-
return tokenize(str, assign({}, this.options, options));
236+
return tokenize(str, Object.assign({}, this.options, options));
239237
}
240238

241239
/**
@@ -272,8 +270,8 @@ class Comments extends Emitter {
272270
*/
273271

274272
parseComment(comment, options) {
275-
const opts = assign({}, this.options, options);
276-
const parsers = assign({}, this.plugins.middleware, opts.parse);
273+
const opts = Object.assign({}, this.options, options);
274+
const parsers = Object.assign({}, this.plugins.middleware, opts.parse);
277275

278276
if (typeof parsers.comment === 'function') {
279277
comment = parsers.comment.call(this, comment, opts);
@@ -285,8 +283,8 @@ class Comments extends Emitter {
285283
let tok = this.tokenize(comment.value, opts);
286284
this.tokens.push(tok);
287285

288-
comment = assign({}, comment, tok);
289-
normalize.examples(comment, opts);
286+
comment = Object.assign({}, comment, tok);
287+
lib.normalize.examples(comment, opts);
290288
comment.tags = this.parseTags(comment, options);
291289

292290
// let name = get(comment, 'code.context.name');
@@ -306,7 +304,7 @@ class Comments extends Emitter {
306304

307305
// optionally format comment object
308306
if (opts.format === true) {
309-
comment = format.call(this, comment, opts);
307+
comment = lib.format.call(this, comment, opts);
310308
}
311309

312310
this.emit('comment', comment);
@@ -330,8 +328,8 @@ class Comments extends Emitter {
330328
*/
331329

332330
parseTags(comment, options) {
333-
let opts = assign({}, this.options, options);
334-
let parsers = assign({}, this.plugins.middleware, opts.parse);
331+
let opts = Object.assign({}, this.options, options);
332+
let parsers = Object.assign({}, this.plugins.middleware, opts.parse);
335333
let tags = [];
336334

337335
if (typeof parsers.parseTags === 'function') {
@@ -366,8 +364,8 @@ class Comments extends Emitter {
366364
*/
367365

368366
parseTag(tok, options) {
369-
const opts = assign({}, this.options, options);
370-
const parsers = assign({}, this.plugins.middleware, opts.parse);
367+
const opts = Object.assign({}, this.options, options);
368+
const parsers = Object.assign({}, this.plugins.middleware, opts.parse);
371369
let tag;
372370

373371
if (typeof tok === 'string') {
@@ -379,33 +377,33 @@ class Comments extends Emitter {
379377
}
380378

381379
try {
382-
tag = parse.tag(tok);
380+
tag = lib.parse.tag(tok);
383381
} catch (err) {
384382
if (opts.strict) throw err;
385383
return null;
386384
}
387385

388-
if (!tag || tag.rawType && !allows.type(tok)) {
386+
if (!tag || tag.rawType && !lib.allows.type(tok)) {
389387
return null;
390388
}
391389

392390
if (tag.rawType) {
393391
tag.type = this.parseType(tag.rawType.slice(1, -1), tag, options);
394392
}
395393

396-
if (tag && expects.type(tag) && !tag.type) {
394+
if (tag && lib.expects.type(tag) && !tag.type) {
397395
if (opts.strict === true) {
398396
return null;
399397
}
400398
tag.type = null;
401399
}
402400

403-
tag = normalize.tag(tag, opts);
401+
tag = lib.normalize.tag(tag, opts);
404402
if (!tag) {
405403
return null;
406404
}
407405

408-
tag = validate.tag(tag, opts);
406+
tag = lib.validate.tag(tag, opts);
409407
if (!tag) {
410408
return null;
411409
}
@@ -453,7 +451,7 @@ class Comments extends Emitter {
453451
return parsers.inlineTag.call(this, str, opts);
454452
}
455453

456-
return parse.inline(str, opts);
454+
return lib.parse.inline(str, opts);
457455
}
458456

459457
/**
@@ -482,8 +480,8 @@ class Comments extends Emitter {
482480
return parsers.type.call(this, str, tag, opts);
483481
}
484482

485-
return parse.type(str, tag, opts);
486-
};
483+
return lib.parse.type(str, tag, opts);
484+
}
487485

488486
parseParamType(str, options) {
489487
if (typeof str !== 'string') {
@@ -514,11 +512,11 @@ class Comments extends Emitter {
514512
}
515513

516514
let comments = [];
517-
const opts = assign({}, this.options, options);
515+
const opts = Object.assign({}, this.options, options);
518516
const res = [];
519517

520518
if (typeof opts.extract === 'function') {
521-
comments = utils.arrayify(opts.extract.call(this, str, opts));
519+
comments = [].concat(opts.extract.call(this, str, opts) || []);
522520
} else {
523521
comments = extract.block(str, opts);
524522
}
@@ -531,8 +529,6 @@ class Comments extends Emitter {
531529
let comment = this.preprocess(comments[i], options);
532530
if (typeof fn === 'function') {
533531
comment = fn.call(this, comment) || comment;
534-
} else {
535-
comment = comment;
536532
}
537533

538534
res.push(comment);
@@ -542,7 +538,7 @@ class Comments extends Emitter {
542538
}
543539

544540
preprocess(comment, options) {
545-
let opts = assign({}, this.options, options);
541+
let opts = Object.assign({}, this.options, options);
546542

547543
if (typeof opts.preprocess === 'function') {
548544
return opts.preprocess.call(this, comment, opts);
@@ -571,7 +567,7 @@ class Comments extends Emitter {
571567
throw new TypeError('expected comment to be an object');
572568
}
573569

574-
let opts = assign({}, this.options, options);
570+
let opts = Object.assign({}, this.options, options);
575571
if (typeof opts.isValid === 'function') {
576572
return opts.isValid(comment);
577573
}

lib/format.js renamed to lib/format/format.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
const Schema = require('map-schema');
44
const inflection = require('inflection');
5-
const normalize = require('./normalize');
6-
const utils = require('./utils');
7-
const pluralize = str => inflection.pluralize(str);
5+
const normalize = require('../normalize');
6+
const utils = require('../utils');
7+
8+
const nameCache = {};
9+
const pluralize = name => {
10+
return nameCache[name] || (nameCache[name] = inflection.pluralize(name));
11+
};
812

913
function formatType(type, tag, comment) {
1014
tag.types = tag.types || [];

lib/format/formatter.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict';
2+
3+
class Formatter {
4+
constructor() {
5+
this.rules = {};
6+
}
7+
8+
rule(name, rule) {
9+
this.rules[name] = this.rules[name] || [];
10+
this.rules[name].push(rule);
11+
return this;
12+
}
13+
14+
run(name, comment) {
15+
this.rules[name].forEach(rule => rule(comment));
16+
return comment;
17+
}
18+
19+
format(comment) {
20+
Object.keys(this.rules).forEach(name => this.run(name, comment));
21+
return comment;
22+
}
23+
}
24+
25+
module.exports = Formatter;
26+
27+
const formatter = new Formatter();
28+
const set = (key, value) => comment => (comment[key] = value);
29+
30+
formatter.rule('foo', set('a', true));
31+
formatter.rule('foo', set('b', true));
32+
formatter.rule('foo', set('c', true));
33+
formatter.rule('foo', set('d', true));
34+
formatter.rule('foo', set('e', true));
35+
formatter.rule('bar', set('x', false));
36+
formatter.rule('bar', set('y', false));
37+
formatter.rule('bar', set('z', false));
38+
39+
const comment = { foo: {} };
40+
formatter.format(comment);
41+
console.log(comment);

lib/formatter.js

Lines changed: 0 additions & 43 deletions
This file was deleted.

lib/index.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
'use strict';
22

3-
define('allows', () => require('./allows'));
4-
define('expects', () => require('./expects'));
3+
// normalization
4+
define('normalize', () => require('./normalize'));
5+
6+
// formatting
57
define('format', () => require('./format'));
68
define('format1', () => require('./format1'));
79
define('formatter', () => require('./formatter'));
8-
define('normalize', () => require('./normalize'));
10+
11+
// parsing
912
define('parse', () => require('./parse'));
10-
define('parser', () => require('./parser'));
11-
define('utils', () => require('./utils'));
13+
14+
// validation
1215
define('validate', () => require('./validate'));
16+
define('allows', () => require('./validate/allows'));
17+
define('expects', () => require('./validate/expects'));
18+
19+
// utils
20+
define('utils', () => require('./utils'));
1321

1422
function define(key, get) {
1523
Reflect.defineProperty(exports, key, { get });

lib/normalize.js renamed to lib/normalize/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict';
22

33
const normalize = module.exports;
4-
const parseName = require('./parse/name');
5-
const allows = require('./allows');
6-
const utils = require('./utils');
4+
const parseName = require('../parse/name');
5+
const allows = require('../validate/allows');
6+
const utils = require('../utils');
77
const { isNumber, define, get } = utils;
88

99
/**

lib/parse/name.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const Expression = require('../nodes/expression');
3+
const Expression = require('./nodes/expression');
44
const utils = require('../utils');
55

66
module.exports = (tok, options = {}) => {

lib/nodes/expression.js renamed to lib/parse/nodes/expression.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
'use strict';
22

33
const Node = require('snapdragon-node');
4-
const flags = require('../parse/flags');
5-
const utils = require('../utils');
6-
const { isNumber, define } = utils;
4+
const flags = require('../flags');
5+
const { clean, define, isNumber } = require('../../utils');
76

87
class Expression extends Node {
98
constructor(name, type) {
@@ -51,7 +50,7 @@ class Expression extends Node {
5150
break;
5251
default: {
5352
this.type = 'NameExpression';
54-
this.name = name ? utils.clean(name) : null;
53+
this.name = name ? clean(name) : null;
5554
define(this, 'value', this.name);
5655

5756
let tok = flags(this);
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)