From e3966c833f48c3fa6c6d88cdfba7a44bc075fdc3 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Thu, 4 Feb 2016 12:35:33 +0100 Subject: [PATCH] Fix `he'll` from warning for `hell` Related to wooorm/alex#73. --- component.json | 1 + index.js | 24 ++++++++++++++++++++++-- package.json | 1 + test.js | 18 +++++++++++++++++- 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/component.json b/component.json index 54748e6..d89b888 100644 --- a/component.json +++ b/component.json @@ -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", diff --git a/index.js b/index.js index 696f6be..48cec66 100644 --- a/index.js +++ b/index.js @@ -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. * @@ -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. @@ -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.} 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), '“', '”') + ',', @@ -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; diff --git a/package.json b/package.json index d981b7b..ff7c01a 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "author": "Titus Wormer (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", diff --git a/test.js b/test.js index 3d20553..279e67e 100644 --- a/test.js +++ b/test.js @@ -23,7 +23,7 @@ var profanities = require('./'); */ test('profanities', function (t) { - t.plan(4); + t.plan(6); retext() .use(profanities) @@ -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' + ); + }); });