Skip to content

Commit

Permalink
Support lintTextSync()
Browse files Browse the repository at this point in the history
standard-engine@7 restores the asynchronous lintText() of
standard-engine@5, and adds a new lintTextSync() method.

This commit uses either lintTextSync() or the asynchronous lintText().
Support for the standard-engine@6 behavior is removed. Presumably it
wasn't widely deployed, and it's a whole bunch of complex logic to
maintain.
  • Loading branch information
novemberborn committed Apr 5, 2017
1 parent fb4ec3a commit 7a983f2
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 60 deletions.
42 changes: 13 additions & 29 deletions lib/worker.js
Expand Up @@ -36,35 +36,19 @@ process.on('message', ({ filename, fix, id, text }) => {
}

try {
/* In standard-engine <6.0.0 the linter.lintText was async, but from
* version 6.0.0 it changed to be sync. This callback is left in here to
* handle the older versions of standard-engine based linters. We can
* remove it in a later major version, if it becomes a liability.
*/
let returnValue
returnValue = linter.lintText(text, { filename, fix }, (err, { results } = {}) => {
if (returnValue) {
return
}

if (err) {
process.send({
error: cleanYamlObject(err),
id
})
} else {
process.send({
id,
results
})
}
})

if (returnValue) {
const { results } = returnValue
return process.send({
id,
results
if (linter.lintTextSync) {
const { results } = linter.lintTextSync(text, { filename, fix })
process.send({ id, results })
} else {
linter.lintText(text, { filename, fix }, (err, { results } = {}) => {
if (err) {
process.send({
error: cleanYamlObject(err),
id
})
} else {
process.send({ id, results })
}
})
}
} catch (err) {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/stubForWorker/errOnLint-sync.js
@@ -1,3 +1,3 @@
exports.lintText = (source, opts) => {
exports.lintTextSync = (source, opts) => {
throw new Error('err when linting')
}
4 changes: 0 additions & 4 deletions test/fixtures/stubForWorker/lintText-faulty.js

This file was deleted.

2 changes: 1 addition & 1 deletion test/fixtures/stubForWorker/lintText-sync.js
@@ -1,3 +1,3 @@
exports.lintText = (source, opts) => {
exports.lintTextSync = (source, opts) => {
return { results: [] }
}
25 changes: 0 additions & 25 deletions test/lib/worker.spec.js
Expand Up @@ -177,29 +177,4 @@ describe('handle both async and sync lintText', () => {
})
})
})
it('should protect against faulty standard-engine implementations', () => {
/* This test is supposed to guard verify that a faulty lintText implementation
* is not going to trigger two messages. It is a bit brittle, as I have to rely
* on a timeout being queued after receiving the first message, to check that
* no other messages are received at a later point.
*/
const child = childProcess.fork(workerPath, [require.resolve('../fixtures/stubForWorker/lintText-faulty')])
let calls = []
let calledOnce = false
const promise = new Promise(resolve => {
child.on('message', m => {
calls.push(m)
if (!calledOnce) {
calledOnce = true
setTimeout(() => resolve(calls), 100)
}
})
})

child.send({ id: 1, source: '' })

return expect(promise, 'when fulfilled to equal', [
{ id: 1, results: [ { callback: false } ] }
])
})
})

0 comments on commit 7a983f2

Please sign in to comment.