Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Nov 6, 2018
1 parent f0e4a1f commit be34003
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 55 deletions.
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage/
retext-passive.js
retext-passive.min.js
56 changes: 30 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,52 @@
'use strict';
'use strict'

var difference = require('lodash.difference');
var search = require('nlcst-search');
var toString = require('nlcst-to-string');
var position = require('unist-util-position');
var findBefore = require('unist-util-find-before');
var patterns = require('./list');
var difference = require('lodash.difference')
var search = require('nlcst-search')
var toString = require('nlcst-to-string')
var position = require('unist-util-position')
var findBefore = require('unist-util-find-before')
var patterns = require('./list')

module.exports = passive;
module.exports = passive

var verbs = ['am', 'are', 'were', 'being', 'is', 'been', 'was', 'be'];
var verbs = ['am', 'are', 'were', 'being', 'is', 'been', 'was', 'be']

function passive(options) {
var ignore = (options || {}).ignore || [];
var list = difference(patterns, ignore);
var ignore = (options || {}).ignore || []
var list = difference(patterns, ignore)

return transformer;
return transformer

/* Search `tree` for violations. */
function transformer(tree, file) {
search(tree, list, searcher);
search(tree, list, searcher)

function searcher(match, index, parent, phrase) {
var before = findBefore(parent, index, 'WordNode');
var message;
var before = findBefore(parent, index, 'WordNode')
var message

if (!before) {
return;
return
}

before = toString(before).toLowerCase();
before = toString(before).toLowerCase()

if (verbs.indexOf(before) === -1) {
return;
return
}

message = file.warn('Don’t use the passive voice', {
start: position.start(match[0]),
end: position.end(match[match.length - 1])
}, phrase.replace(/\s+/g, '-').toLowerCase());

message.source = 'retext-passive';
message.actual = toString(match);
message.expected = null;
message = file.warn(
'Don’t use the passive voice',
{
start: position.start(match[0]),
end: position.end(match[match.length - 1])
},
phrase.replace(/\s+/g, '-').toLowerCase()
)

message.source = 'retext-passive'
message.actual = toString(match)
message.expected = null
}
}
}
20 changes: 14 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,38 @@
"browserify": "^16.0.0",
"esmangle": "^1.0.1",
"nyc": "^13.0.0",
"prettier": "^1.14.3",
"remark-cli": "^6.0.0",
"remark-preset-wooorm": "^4.0.0",
"retext": "^5.0.0",
"tape": "^4.0.0",
"xo": "^0.23.0"
},
"scripts": {
"build-md": "remark . -qfo",
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix",
"build-bundle": "browserify index.js --bare -s retextPassive > retext-passive.js",
"build-mangle": "esmangle retext-passive.js > retext-passive.min.js",
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
"lint": "xo",
"test-api": "node test.js",
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test.js",
"test": "npm run build && npm run lint && npm run test-coverage"
"test": "npm run format && npm run build && npm run test-coverage"
},
"nyc": {
"check-coverage": true,
"lines": 100,
"functions": 100,
"branches": 100
},
"prettier": {
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"bracketSpacing": false,
"semi": false,
"trailingComma": "none"
},
"xo": {
"space": true,
"prettier": true,
"esnext": false,
"ignores": [
"retext-passive.js"
Expand Down
18 changes: 9 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ He was withheld while we were being fed.
And our script, `example.js`, looks like this:

```javascript
var vfile = require('to-vfile');
var report = require('vfile-reporter');
var unified = require('unified');
var english = require('retext-english');
var stringify = require('retext-stringify');
var passive = require('retext-passive');
var vfile = require('to-vfile')
var report = require('vfile-reporter')
var unified = require('unified')
var english = require('retext-english')
var stringify = require('retext-stringify')
var passive = require('retext-passive')

unified()
.use(english)
.use(passive)
.use(stringify)
.process(vfile.readSync('example.txt'), function (err, file) {
console.error(report(err || file));
});
.process(vfile.readSync('example.txt'), function(err, file) {
console.error(report(err || file))
})
```

Now, running `node example` yields:
Expand Down
28 changes: 14 additions & 14 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
'use strict';
'use strict'

var test = require('tape');
var retext = require('retext');
var passive = require('.');
var test = require('tape')
var retext = require('retext')
var passive = require('.')

test('passive', function (t) {
t.plan(2);
test('passive', function(t) {
t.plan(2)

var doc = [
'He was withheld while we were being fed.',
'Fed.',
'The fed.'
].join('\n');
].join('\n')

retext()
.use(passive)
.process(doc, function (err, file) {
.process(doc, function(err, file) {
t.deepEqual(
[err, file.messages.map(String)],
[
Expand All @@ -26,16 +26,16 @@ test('passive', function (t) {
]
],
'should work'
);
});
)
})

retext()
.use(passive, {ignore: ['fed']})
.process(doc, function (err, file) {
.process(doc, function(err, file) {
t.deepEqual(
[err, file.messages.map(String)],
[null, ['1:8-1:16: Don’t use the passive voice']],
'should `ignore`'
);
});
});
)
})
})

0 comments on commit be34003

Please sign in to comment.