-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
86 lines (71 loc) · 2.01 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
/**
* @author Adam Hollett
* @copyright 2016 Adam Hollett
* @license MIT
* @module retext:usage
* @fileoverview Check for incorrect English usage.
*/
'use strict';
/* eslint-env commonjs */
/*
* Dependencies.
*/
var keys = require('object-keys');
var difference = require('array-differ');
var nlcstToString = require('nlcst-to-string');
var quotation = require('quotation');
var search = require('nlcst-search');
var patterns = require('./data/index.json');
/*
* 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 attacher(processor, options) {
var ignore = (options || {}).ignore || [];
var phrases = difference(list, ignore);
/**
* 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;
message.source = 'retext-usage';
});
}
return transformer;
}
/*
* Expose.
*/
module.exports = attacher;