diff --git a/component.json b/component.json index 85fed74..0319536 100644 --- a/component.json +++ b/component.json @@ -14,8 +14,10 @@ ], "repository": "wooorm/retext-simplify", "dependencies": { - "wooorm/nlcst-to-string": "^1.1.0", + "sindresorhus/array-differ": "^1.0.0", "wooorm/nlcst-search": "^1.0.0", + "wooorm/nlcst-to-string": "^1.1.0", + "ljharb/object-keys": "^1.0.9", "wooorm/quotation": "^1.0.0" }, "scripts": [ diff --git a/index.js b/index.js index 43c3393..71625e7 100644 --- a/index.js +++ b/index.js @@ -14,51 +14,68 @@ * 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); + /** - * Search `tree` for validations. + * Attacher. * - * @param {Node} tree - NLCST node. - * @param {VFile} file - Virtual file. + * @param {Retext} processor + * - Instance. + * @param {Object?} [options] + * - Configuration. + * @param {Array.?} [options.ignore] + * - List of phrases to *not* warn about. + * @return {Function} - `transformer`. */ -function transformer(tree, file) { - search(tree, patterns, function (match, position, parent, phrase) { - var pattern = patterns[phrase]; - var replace = pattern.replace; - var value = quotation(nlcstToString(match), '“', '”'); - var message; +function attacher(processor, options) { + var ignore = (options || {}).ignore || []; + var phrases = difference(list, ignore); - if (pattern.omit && !replace.length) { - message = 'Remove ' + value; - } else { - message = 'Replace ' + value + ' with ' + - quotation(replace, '“', '”').join(', '); + /** + * 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) { - message += ', or remove it'; + 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 = file.warn(message, { + 'start': match[0].position.start, + 'end': match[match.length - 1].position.end + }); - message.ruleId = phrase; - message.source = 'retext-simplify'; - }); -} + message.ruleId = phrase; + message.source = 'retext-simplify'; + }); + } -/** - * Attacher. - * - * @return {Function} - `transformer`. - */ -function attacher() { return transformer; } diff --git a/package.json b/package.json index 3bb56df..9ae0e1d 100644 --- a/package.json +++ b/package.json @@ -23,8 +23,10 @@ "bugs": "https://github.com/wooorm/retext-simplify/issues", "author": "Titus Wormer (http://wooorm.com)", "dependencies": { - "nlcst-to-string": "^1.1.0", + "array-differ": "^1.0.0", "nlcst-search": "^1.0.0", + "nlcst-to-string": "^1.1.0", + "object-keys": "^1.0.9", "quotation": "^1.0.0" }, "devDependencies": { diff --git a/readme.md b/readme.md index 1fe1bd7..0de4866 100644 --- a/readme.md +++ b/readme.md @@ -44,13 +44,18 @@ Yields: ## API -### `retext.use(simplify)` +### `retext.use(simplify[, options])` Check phrases for simpler alternatives. **Parameters** -* `simplify` — This plug-in. +* `simplify` — This plug-in; + +* `options` (`Object?`, optional): + + * `ignore` (`Array.`) + — List of phrases to *not* warn about. ## License diff --git a/test.js b/test.js index b21a852..144c816 100644 --- a/test.js +++ b/test.js @@ -23,6 +23,8 @@ var simplify = require('./'); */ test('simplify', function (t) { + t.plan(4); + retext() .use(simplify) .process([ @@ -44,5 +46,27 @@ test('simplify', function (t) { ); }); - t.end(); + retext() + .use(simplify, { + 'ignore': [ + 'utilize' + ] + }) + .process([ + 'You can utilize a shorter word.', + 'Be advised, don’t do this.', + 'That’s the appropriate thing to do.' + ].join('\n'), function (err, file) { + t.ifError(err, 'should not fail (#2)'); + + t.deepEqual( + file.messages.map(String), + [ + '2:1-2:11: Remove “Be advised”', + '3:12-3:23: Replace “appropriate” with “proper”, ' + + '“right”, or remove it' + ], + 'should not warn for `ignore`d phrases' + ); + }); });