-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathformatters.js
137 lines (119 loc) · 2.96 KB
/
formatters.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
'use strict';
const fs = require('fs');
const path = require('path');
const Schema = require('map-schema');
const Comments = require('..');
const comments = new Comments();
const fp = path.join(__dirname, 'fixtures/cache.js');
function normalizeType(type, tag, comment) {
tag.types = tag.types || [];
if (!type) return;
let schema = new Schema()
.field('name', 'string', {
normalize(val) {
if (typeof val === 'string') {
if (val === '*') val = 'any';
if (tag.types.indexOf(val) === -1) {
tag.types.push(val);
}
}
return val;
}
})
.field('nodes', 'array', {
normalize(val) {
if (Array.isArray(val)) {
for (let i = 0; i < val.length; i++) {
schema.normalize(val[i]);
}
}
return val;
}
})
return schema.normalize(type);
}
function normalizeTag(tag, comment) {
var schema = new Schema()
.field('description', 'string')
.field('title', 'string', {
normalize(val) {
switch (val) {
case 'ctor':
case 'constructor':
comment.is.ctor = true;
break;
case 'api':
comment[tag.name] = true;
break;
default:
break;
}
// console.log(val);
return val;
}
})
.field('name', 'string')
.field('type', 'object', {
normalize(val, key, tag) {
return normalizeType(val, tag, comment);
}
})
return schema.normalize(tag);
}
function normalizeCode(code, comment) {
let schema = new Schema()
.field('val', 'string', function(val) {
return val;
})
.field('context', 'object', function(val) {
if (val.name && typeof comment.name === 'undefined') {
comment.name = val.name;
}
return val;
});
return schema.normalize(code);
}
function normalizeComment(comment, cache) {
comment.is = comment.is || {};
let schema = new Schema()
.field('tags', tags => {
for (let i = 0; i < tags.length; i++) {
normalizeTag(tags[i], comment);
}
return tags;
})
.field('code', 'object', {
normalize(val) {
return normalizeCode(val, comment);
}
})
.field('name', 'string', {
normalize(val) {
if (!comment.public) return;
if (comment.is.ctor) {
cache.ctor = val;
} else if (val && cache.ctor) {
val = cache.ctor + '#' + val;
} else if (val) {
console.log(comment)
} else {
}
return val;
}
})
return schema.normalize(comment);
}
module.exports = normalizeComment;
// function parse(str, options) {
// let arr = comments.parse(str, options);
// let cache = {};
// let res = [];
// for (let i = 0; i < arr.length; i++) {
// let comment = normalizeComment(arr[i], cache);
// if (comment.public === true) {
// res.push(comment);
// }
// }
// return res;
// }
// parse(fs.readFileSync(fp, 'utf8'));