forked from retextjs/retext-simplify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
74 lines (63 loc) · 1.84 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
/**
* @author Titus Wormer
* @copyright 2016 Titus Wormer
* @license MIT
* @module retext:simplify
* @fileoverview Check phrases for simpler alternatives.
*/
'use strict';
/* Dependencies. */
var keys = require('object-keys');
var difference = require('lodash.difference');
var nlcstToString = require('nlcst-to-string');
var quotation = require('quotation');
var search = require('nlcst-search');
var patterns = require('./index.json');
/* Expose. */
module.exports = simplify;
/* List of all phrases. */
var list = keys(patterns);
/**
* Attacher.
*
* @param {Retext} processor
* - Instance.
* @param {Object?} [options]
* - Configuration.
* @param {Array.<string>?} [options.ignore]
* - List of phrases to *not* warn about.
* @return {Function} - `transformer`.
*/
function simplify(processor, options) {
var ignore = (options || {}).ignore || [];
var phrases = difference(list, ignore);
return transformer;
/**
* Search `tree` for validations.
*
* @param {Node} tree - NLCST node.
* @param {VFile} file - Virtual file.
*/
function transformer(tree, file) {
search(tree, phrases, function (match, position, parent, phrase) {
var pattern = patterns[phrase];
var replace = pattern.replace;
var value = quotation(nlcstToString(match), '“', '”');
var message;
if (pattern.omit && !replace.length) {
message = 'Remove ' + value;
} else {
message = 'Replace ' + value + ' with ' + quotation(replace, '“', '”').join(', ');
if (pattern.omit) {
message += ', or remove it';
}
}
message = file.warn(message, {
start: match[0].position.start,
end: match[match.length - 1].position.end
});
message.ruleId = phrase.replace(/\s+/g, '-').toLowerCase();
message.source = 'retext-simplify';
});
}
}