-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathparser.js
88 lines (72 loc) · 2.19 KB
/
parser.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
let AnnotationsApi = require('./annotation').default;
let ScssCommentParser = require('scss-comment-parser');
let through = require('through2');
let path = require('path');
let utils = require('./utils');
export default class Parser {
constructor(config, additionalAnnotations) {
this.annotations = new AnnotationsApi(config);
this.annotations.addAnnotations(additionalAnnotations);
this.scssParser = new ScssCommentParser(this.annotations.list, config);
this.scssParser.commentParser.on('warning', warning => {
config.logger.warn(warning);
});
}
parse(code, id) {
return this.scssParser.parse(code, id);
}
/**
* Invoke the `resolve` function of an annotation if present.
* Called with all found annotations except with type "unkown".
*/
postProcess(data) {
Object.keys(this.annotations.list).forEach(key => {
let annotation = this.annotations.list[key];
if (annotation.resolve) {
annotation.resolve(data);
}
});
return data;
}
/**
* Return a transform stream meant to be piped in a stream of SCSS
* files. Each file will be passed-through as-is, but they are all
* parsed to generate a SassDoc data array.
*
* The returned stream has an additional `promise` property, containing
* a `Promise` object that will be resolved when the stream is done and
* the data is fulfiled.
*
* @param {Object} parser
* @return {Object}
*/
stream() {
let data = [];
let deferred = utils.defer();
let transform = (chunk, enc, cb) => {
// Pass-through.
cb(null, chunk);
if (!chunk.isBuffer()) {
return;
}
var name = path.basename(chunk.relative);
let fileData = this.parse(chunk.contents.toString(enc), name);
fileData.forEach(item => {
item.file = {
path: chunk.relative,
name: name,
};
data.push(item);
});
};
let flush = cb => {
data = data.filter(item => item.context.type !== 'unknown');
data = this.postProcess(data);
deferred.resolve(data);
cb();
};
let filter = through.obj(transform, flush);
filter.promise = deferred.promise;
return filter;
}
}