| 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
138
139
140
141
142
143 |
1×
1×
1×
1×
1×
1×
1×
103×
103×
103×
103×
103×
103×
103×
103×
103×
103×
103×
103×
103×
2×
103×
103×
251×
103×
1×
1×
1×
39×
39×
39×
39×
39×
39×
39×
1×
37×
1×
2×
1×
| /**
* @file htmlcs methods
* @author nighca<nighca@live.cn>
*/
var htmlGenner = require('html-code-gen');
var util = require('./util');
var parse = require('./parse');
var rules = require('./rules');
var Reporter = require('./reporter');
var otherFormatters = require('./other-formatter');
/**
* The report item.
*
* @typedef {Object} Report
* @property {string} type - typeof the message, one of "info", "warn", "error"
* @property {number} line - line number of the report
* @property {number} column - column number of the report
* @property {string} code - code of the report
* @property {string} message - message of the report
* @property {string} rule - name of the report's rule
*/
/**
* Do hint with given code & config.
*
* @param {string} code - given code
* @param {Object} cfg - given config
* @return {Report[]} the hint result, list of reports
*/
var hint = function (code, cfg) {
// get rid of \r
code = code.replace(/\r\n?/g, '\n');
cfg = cfg || {};
// max error num
var maxError = cfg['max-error'];
delete cfg['max-error'];
// get reporter
var reporter = new Reporter();
// get parser
var parser = parse.getParser();
// collect inline configs
var inlineCfg = rules.collectInlineCfg(parser);
// configure reporter with inline reporter-config ( enable / disable )
reporter.config(inlineCfg.reporter);
// lint parser
rules.lintParser(parser, reporter, cfg, inlineCfg.rules, code);
// parse & lint document
var document = parse(code, parser);
rules.lintDocument(document, reporter, cfg, inlineCfg.rules, code);
// get result
var result = reporter.result();
// num control
if (maxError) {
result = result.slice(0, maxError);
}
// do position ( pos -> line & column )
var position = util.getPosition(code);
result.forEach(function (item) {
util.extend(item, position(item.pos));
});
return result;
};
/**
* Do hint with given code & config.
*
* @param {string} code - given code
* @param {Object} cfg - given config
* @return {Promise<Report[]>} the hint result, list of reports
*/
var hintAsync = function (code, cfg) {
return Promise.resolve(hint(code, cfg));
};
var formatWithGivenPrint = function (code, cfg, print) {
cfg = cfg || {};
// get parser
var parser = parse.getParser();
// collect inline configs
var inlineCfg = rules.collectInlineCfg(parser);
// parse document
var document = parse(code, parser);
// format options
var options = util.extend({
'indent-size': 4,
'indent-char': 'space',
'max-char': 120,
'formatter': otherFormatters
}, cfg.format);
rules.format(document, cfg, inlineCfg.rules, options);
return print(document, options);
};
/**
* Do format with given code & config.
*
* @param {string} code - given code
* @param {Object} cfg - given config
* @return {string} the formatted code
*/
var format = function (code, cfg) {
return formatWithGivenPrint(code, cfg, htmlGenner.print);
};
/**
* Do async format with given code & config.
*
* @param {string} code - given code
* @param {Object} cfg - given config
* @return {Promise<string>} the formatted code
*/
var formatAsync = function (code, cfg) {
return formatWithGivenPrint(code, cfg, htmlGenner.printAsync);
};
module.exports = {
addRule: rules.add.bind(rules),
hint: hint,
hintAsync: hintAsync,
format: format,
formatAsync: formatAsync
};
|