forked from KatChaotic/sveltedoc-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
106 lines (83 loc) · 3.46 KB
/
index.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
const { loadFileStructureFromOptions } = require('./lib/helpers');
const SvelteVersionDetector = require('./lib/detector');
const DEFAULT_ENCODING = 'utf8';
const DEFAULT_IGNORED_VISIBILITIES = ['protected', 'private'];
function validateOptions(options) {
if (!options || (!options.filename && !options.fileContent)) {
throw new Error('One of options.filename or options.filecontent is required');
}
}
function normalizeOptions(options) {
options.encoding = options.encoding || DEFAULT_ENCODING;
options.ignoredVisibilities = options.ignoredVisibilities || DEFAULT_IGNORED_VISIBILITIES;
}
function parseSvelte2(structure, options, resolve, reject) {
const component = {
version: SvelteVersionDetector.SVELTE_VERSION_2
};
const Parser = require('./lib/parser');
// Convert structure object to old version source options
const hasScript = structure.scripts && structure.scripts.length > 0;
const hasStyle = structure.styles && structure.styles.length > 0;
options.source = {
template: structure.template,
script: hasScript ? structure.scripts[0].content : '',
scriptOffset: hasScript ? structure.scripts[0].offset : 0,
style: hasStyle ? structure.styles[0].content : '',
styleOffset: hasStyle ? structure.styles[0].offset : 0,
};
const parser = new Parser(options);
parser.features.forEach((feature) => {
switch (feature) {
case 'name':
case 'description':
component[feature] = null;
parser.on(feature, (value) => (component[feature] = value));
break;
case 'keywords':
component[feature] = [];
parser.on(feature, (value) => (component[feature] = value));
break;
default:
component[feature] = [];
const eventName = Parser.getEventName(feature);
parser.on(eventName, (value) => {
const itemIndex = component[feature].findIndex(item => item.name === value.name);
if (itemIndex < 0) {
component[feature].push(value);
} else {
component[feature][itemIndex] = value;
}
});
}
});
parser.on('end', () => {
parser.features.forEach((feature) => {
if (component[feature] instanceof Array) {
component[feature] = component[feature].filter((item) => {
return !options.ignoredVisibilities.includes(item.visibility);
});
}
});
resolve(component);
});
parser.on('failure', (error) => {
reject(error);
});
parser.walk();
}
module.exports.parse = (options) => new Promise((resolve, reject) => {
try {
validateOptions(options);
normalizeOptions(options);
const structure = loadFileStructureFromOptions(options);
const version = options.version || SvelteVersionDetector.detectVersionFromStructure(structure);
if (version === SvelteVersionDetector.SVELTE_VERSION_2) {
parseSvelte2(structure, options, resolve, reject);
return;
}
reject(new Error(`Svelte V${version} is not supported`));
} catch (error) {
reject(error);
}
});