-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
110 lines (92 loc) · 2.47 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
107
108
109
110
/**
* @author Titus Wormer
* @copyright 2016 Titus Wormer
* @license MIT
* @module retext:profanities
* @fileoverview Check for profane and vulgar wording with retext.
*/
'use strict';
/* eslint-env commonjs */
/*
* Dependencies.
*/
var keys = require('object-keys');
var difference = require('array-differ');
var intersection = require('array-intersection');
var nlcstToString = require('nlcst-to-string');
var quotation = require('quotation');
var search = require('nlcst-search');
var cuss = require('cuss');
/*
* List of values not to normalize.
*/
var APOSTROPHES = ['hell'];
/*
* Map of `cuss` ratings to suffixes.
*/
var PREFIX = [
'Be careful with',
'Reconsider using',
'Don’t use'
];
/*
* Map of `cuss` ratings to suffixes.
*/
var SUFFIX = [
'it’s profane in some cases',
'it may be profane',
'it’s profane'
];
/**
* 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(keys(cuss), ignore);
var apostrophes = difference(phrases, APOSTROPHES);
var noApostrophes = intersection(APOSTROPHES, phrases);
/**
* Search `tree` for validations.
*
* @param {Node} tree - NLCST node.
* @param {VFile} file - Virtual file.
*/
function transformer(tree, file) {
/**
* Handle a match.
*
* @param {Array.<Node>} match - Matched nodes.
* @param {Position} position - Location.
* @param {Node} parent - Parent of `match`.
* @param {string} phrase - Matched value.
*/
function handle(match, position, parent, phrase) {
var rating = cuss[phrase];
var message = file.warn([
PREFIX[rating],
quotation(nlcstToString(match), '“', '”') + ',',
SUFFIX[rating]
].join(' '), {
'start': match[0].position.start,
'end': match[match.length - 1].position.end
});
message.ruleId = phrase;
message.source = 'retext-profanities';
}
search(tree, apostrophes, handle);
search(tree, noApostrophes, handle, true);
}
return transformer;
}
/*
* Expose.
*/
module.exports = attacher;