Skip to content

Commit

Permalink
Add support for ignoring phrases in options
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jan 16, 2016
1 parent 651310b commit 4c3531f
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 36 deletions.
4 changes: 3 additions & 1 deletion component.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
79 changes: 48 additions & 31 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.<string>?} [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;
}

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
"bugs": "https://github.com/wooorm/retext-simplify/issues",
"author": "Titus Wormer <[email protected]> (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": {
Expand Down
9 changes: 7 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.<string>`)
— List of phrases to *not* warn about.

## License

Expand Down
26 changes: 25 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ var simplify = require('./');
*/

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

retext()
.use(simplify)
.process([
Expand All @@ -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'
);
});
});

0 comments on commit 4c3531f

Please sign in to comment.