Skip to content

Commit

Permalink
Fix he'll from warning for hell
Browse files Browse the repository at this point in the history
Related to get-alex/alex#73.
  • Loading branch information
wooorm committed Feb 4, 2016
1 parent 0b63dab commit e3966c8
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
1 change: 1 addition & 0 deletions component.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"repository": "wooorm/retext-profanities",
"dependencies": {
"sindresorhus/array-differ": "^1.0.0",
"jonschlinkert/array-intersection": "^0.1.2",
"wooorm/nlcst-to-string": "^1.1.0",
"wooorm/nlcst-search": "^1.0.0",
"wooorm/profanities": "^1.0.0",
Expand Down
24 changes: 22 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@
*/

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 profanities = require('profanities');

/*
* List of values not to normalize.
*/

var APOSTROPHES = ['hell'];

/**
* Attacher.
*
Expand All @@ -34,6 +41,8 @@ var profanities = require('profanities');
function attacher(processor, options) {
var ignore = (options || {}).ignore || [];
var phrases = difference(profanities, ignore);
var apostrophes = difference(phrases, APOSTROPHES);
var noApostrophes = intersection(APOSTROPHES, phrases);

/**
* Search `tree` for validations.
Expand All @@ -42,7 +51,15 @@ function attacher(processor, options) {
* @param {VFile} file - Virtual file.
*/
function transformer(tree, file) {
search(tree, phrases, function (match, position, parent, phrase) {
/**
* 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 message = file.warn([
'Don’t use',
quotation(nlcstToString(match), '“', '”') + ',',
Expand All @@ -54,7 +71,10 @@ function attacher(processor, options) {

message.ruleId = phrase;
message.source = 'retext-profanities';
});
}

search(tree, apostrophes, handle);
search(tree, noApostrophes, handle, true);
}

return transformer;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"author": "Titus Wormer <[email protected]> (http://wooorm.com)",
"dependencies": {
"array-differ": "^1.0.0",
"array-intersection": "^0.1.2",
"nlcst-search": "^1.0.0",
"nlcst-to-string": "^1.1.0",
"profanities": "^1.0.0",
Expand Down
18 changes: 17 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var profanities = require('./');
*/

test('profanities', function (t) {
t.plan(4);
t.plan(6);

retext()
.use(profanities)
Expand Down Expand Up @@ -56,4 +56,20 @@ test('profanities', function (t) {
'should not warn for `ignore`d phrases'
);
});

retext()
.use(profanities)
.process([
'When he’ll freeze over, hell freezes over.'
].join('\n'), function (err, file) {
t.ifError(err, 'should not fail (#3)');

t.deepEqual(
file.messages.map(String),
[
'1:25-1:29: Don’t use “hell”, it’s profane'
],
'should correctly depend on apostrophes'
);
});
});

0 comments on commit e3966c8

Please sign in to comment.