Skip to content

Commit 2ced8f1

Browse files
committed
Integrate eslint
1 parent 3675d2b commit 2ced8f1

File tree

9 files changed

+154
-136
lines changed

9 files changed

+154
-136
lines changed

.eslintrc

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "standard",
3+
"env": {
4+
"mocha": true
5+
},
6+
"rules": {
7+
"no-new-func": "off"
8+
}
9+
}

index.js

+56-58
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
var Result = require('./pg').Result
2-
var prepare = require('./pg').prepareValue
3-
var EventEmitter = require('events').EventEmitter;
4-
var util = require('util');
1+
'use strict'
2+
const Result = require('./pg').Result
3+
const prepare = require('./pg').prepareValue
4+
const EventEmitter = require('events').EventEmitter
5+
const util = require('util')
56

67
function Cursor (text, values) {
7-
EventEmitter.call(this);
8+
EventEmitter.call(this)
89

910
this.text = text
1011
this.values = values ? values.map(prepare) : null
@@ -18,11 +19,10 @@ function Cursor (text, values) {
1819

1920
util.inherits(Cursor, EventEmitter)
2021

21-
Cursor.prototype.submit = function(connection) {
22+
Cursor.prototype.submit = function (connection) {
2223
this.connection = connection
2324

24-
var con = connection
25-
var self = this
25+
const con = connection
2626

2727
con.parse({
2828
text: this.text
@@ -34,145 +34,143 @@ Cursor.prototype.submit = function(connection) {
3434

3535
con.describe({
3636
type: 'P',
37-
name: '' //use unamed portal
37+
name: '' // use unamed portal
3838
}, true)
3939

4040
con.flush()
4141

42+
const ifNoData = () => {
43+
this.state = 'idle'
44+
this._shiftQueue()
45+
}
46+
4247
con.once('noData', ifNoData)
4348
con.once('rowDescription', function () {
44-
con.removeListener('noData', ifNoData);
45-
});
46-
47-
function ifNoData () {
48-
self.state = 'idle'
49-
self._shiftQueue();
50-
}
49+
con.removeListener('noData', ifNoData)
50+
})
5151
}
5252

5353
Cursor.prototype._shiftQueue = function () {
54-
if(this._queue.length) {
54+
if (this._queue.length) {
5555
this._getRows.apply(this, this._queue.shift())
5656
}
5757
}
5858

59-
Cursor.prototype.handleRowDescription = function(msg) {
59+
Cursor.prototype.handleRowDescription = function (msg) {
6060
this._result.addFields(msg.fields)
6161
this.state = 'idle'
62-
this._shiftQueue();
62+
this._shiftQueue()
6363
}
6464

65-
Cursor.prototype.handleDataRow = function(msg) {
66-
var row = this._result.parseRow(msg.fields)
65+
Cursor.prototype.handleDataRow = function (msg) {
66+
const row = this._result.parseRow(msg.fields)
6767
this.emit('row', row, this._result)
6868
this._rows.push(row)
6969
}
7070

71-
Cursor.prototype._sendRows = function() {
71+
Cursor.prototype._sendRows = function () {
7272
this.state = 'idle'
73-
setImmediate(function() {
74-
var cb = this._cb
75-
//remove callback before calling it
76-
//because likely a new one will be added
77-
//within the call to this callback
73+
setImmediate(() => {
74+
const cb = this._cb
75+
// remove callback before calling it
76+
// because likely a new one will be added
77+
// within the call to this callback
7878
this._cb = null
79-
if(cb) {
79+
if (cb) {
8080
this._result.rows = this._rows
8181
cb(null, this._rows, this._result)
8282
}
8383
this._rows = []
84-
}.bind(this))
84+
})
8585
}
8686

87-
Cursor.prototype.handleCommandComplete = function() {
87+
Cursor.prototype.handleCommandComplete = function () {
8888
this.connection.sync()
8989
}
9090

91-
Cursor.prototype.handlePortalSuspended = function() {
91+
Cursor.prototype.handlePortalSuspended = function () {
9292
this._sendRows()
9393
}
9494

95-
Cursor.prototype.handleReadyForQuery = function() {
95+
Cursor.prototype.handleReadyForQuery = function () {
9696
this._sendRows()
9797
this.emit('end', this._result)
9898
this.state = 'done'
9999
}
100100

101-
Cursor.prototype.handleEmptyQuery = function() {
101+
Cursor.prototype.handleEmptyQuery = function () {
102102
this.connection.sync()
103-
};
103+
}
104104

105-
Cursor.prototype.handleError = function(msg) {
105+
Cursor.prototype.handleError = function (msg) {
106106
this.state = 'error'
107107
this._error = msg
108-
//satisfy any waiting callback
109-
if(this._cb) {
108+
// satisfy any waiting callback
109+
if (this._cb) {
110110
this._cb(msg)
111111
}
112-
//dispatch error to all waiting callbacks
113-
for(var i = 0; i < this._queue.length; i++) {
112+
// dispatch error to all waiting callbacks
113+
for (var i = 0; i < this._queue.length; i++) {
114114
this._queue.pop()[1](msg)
115115
}
116116

117117
if (this.listenerCount('error') > 0) {
118-
//only dispatch error events if we have a listener
118+
// only dispatch error events if we have a listener
119119
this.emit('error', msg)
120120
}
121-
//call sync to keep this connection from hanging
121+
// call sync to keep this connection from hanging
122122
this.connection.sync()
123123
}
124124

125-
Cursor.prototype._getRows = function(rows, cb) {
125+
Cursor.prototype._getRows = function (rows, cb) {
126126
this.state = 'busy'
127127
this._cb = cb
128128
this._rows = []
129-
var msg = {
129+
const msg = {
130130
portal: '',
131131
rows: rows
132132
}
133133
this.connection.execute(msg, true)
134134
this.connection.flush()
135135
}
136136

137-
Cursor.prototype.end = function(cb) {
138-
if(this.state != 'initialized') {
137+
Cursor.prototype.end = function (cb) {
138+
if (this.state !== 'initialized') {
139139
this.connection.sync()
140140
}
141141
this.connection.stream.once('end', cb)
142142
console.log('calling end on connection')
143143
this.connection.end()
144144
}
145145

146-
Cursor.prototype.close = function(cb) {
147-
if (this.state == 'done') {
146+
Cursor.prototype.close = function (cb) {
147+
if (this.state === 'done') {
148148
return setImmediate(cb)
149149
}
150150
this.connection.close({type: 'P'})
151151
this.connection.sync()
152152
this.state = 'done'
153-
if(cb) {
154-
this.connection.once('closeComplete', function() {
153+
if (cb) {
154+
this.connection.once('closeComplete', function () {
155155
cb()
156156
})
157157
}
158158
}
159159

160-
Cursor.prototype.read = function(rows, cb) {
161-
var self = this
162-
if(this.state == 'idle') {
160+
Cursor.prototype.read = function (rows, cb) {
161+
if (this.state === 'idle') {
163162
return this._getRows(rows, cb)
164163
}
165-
if(this.state == 'busy' || this.state == 'initialized') {
164+
if (this.state === 'busy' || this.state === 'initialized') {
166165
return this._queue.push([rows, cb])
167166
}
168-
if(this.state == 'error') {
167+
if (this.state === 'error') {
169168
return setImmediate(() => cb(this._error))
170169
}
171-
if(this.state == 'done') {
170+
if (this.state === 'done') {
172171
return setImmediate(() => cb(null, []))
173-
}
174-
else {
175-
throw new Error("Unknown state: " + this.state)
172+
} else {
173+
throw new Error('Unknown state: ' + this.state)
176174
}
177175
}
178176

package.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@
77
"test": "test"
88
},
99
"scripts": {
10-
"test": "mocha"
10+
"test": " mocha && eslint ."
1111
},
1212
"author": "Brian M. Carlson",
1313
"license": "MIT",
1414
"devDependencies": {
15+
"eslint": "^4.4.0",
16+
"eslint-config-standard": "^10.2.1",
17+
"eslint-plugin-import": "^2.7.0",
18+
"eslint-plugin-node": "^5.1.1",
19+
"eslint-plugin-promise": "^3.5.0",
20+
"eslint-plugin-standard": "^3.0.1",
1521
"mocha": "^3.5.0",
1622
"pg": "~6.0.0"
1723
},

pg.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
//support both pg & pg.js
2-
//this will eventually go away when i break native bindings
3-
//out into their own module
1+
// support both pg & pg.js
2+
// this will eventually go away when i break native bindings
3+
// out into their own module
44
try {
55
module.exports.Result = require('pg/lib/result.js')
66
module.exports.prepareValue = require('pg/lib/utils.js').prepareValue
7-
} catch(e) {
7+
} catch (e) {
88
module.exports.Result = require('pg.js/lib/result.js')
99
module.exports.prepareValue = require('pg.js/lib/utils.js').prepareValue
1010
}

test/close.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,30 @@ var Cursor = require('../')
33
var pg = require('pg')
44

55
var text = 'SELECT generate_series as num FROM generate_series(0, 50)'
6-
describe('close', function() {
7-
beforeEach(function(done) {
6+
describe('close', function () {
7+
beforeEach(function (done) {
88
var client = this.client = new pg.Client()
99
client.connect(done)
1010
client.on('drain', client.end.bind(client))
1111
})
1212

13-
it('closes cursor early', function(done) {
13+
it('closes cursor early', function (done) {
1414
var cursor = new Cursor(text)
1515
this.client.query(cursor)
1616
this.client.query('SELECT NOW()', done)
17-
cursor.read(25, function(err, res) {
17+
cursor.read(25, function (err, res) {
1818
assert.ifError(err)
1919
cursor.close()
2020
})
2121
})
2222

23-
it('works with callback style', function(done) {
23+
it('works with callback style', function (done) {
2424
var cursor = new Cursor(text)
2525
var client = this.client
2626
client.query(cursor)
27-
cursor.read(25, function(err, res) {
27+
cursor.read(25, function (err, res) {
2828
assert.ifError(err)
29-
cursor.close(function(err) {
29+
cursor.close(function (err) {
3030
assert.ifError(err)
3131
client.query('SELECT NOW()', done)
3232
})

test/error-handling.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ var pg = require('pg')
55

66
var text = 'SELECT generate_series as num FROM generate_series(0, 4)'
77

8-
describe('error handling', function() {
9-
it('can continue after error', function(done) {
8+
describe('error handling', function () {
9+
it('can continue after error', function (done) {
1010
var client = new pg.Client()
1111
client.connect()
1212
var cursor = client.query(new Cursor('asdfdffsdf'))
13-
cursor.read(1, function(err) {
13+
cursor.read(1, function (err) {
1414
assert(err)
15-
client.query('SELECT NOW()', function(err, res) {
15+
client.query('SELECT NOW()', function (err, res) {
1616
assert.ifError(err)
1717
client.end()
1818
done()
@@ -27,7 +27,7 @@ describe('read callback does not fire sync', () => {
2727
client.connect()
2828
var cursor = client.query(new Cursor('asdfdffsdf'))
2929
let after = false
30-
cursor.read(1, function(err) {
30+
cursor.read(1, function (err) {
3131
assert(err, 'error should be returned')
3232
assert.equal(after, true, 'should not call read sync')
3333
after = false
@@ -47,11 +47,14 @@ describe('read callback does not fire sync', () => {
4747
client.connect()
4848
var cursor = client.query(new Cursor('SELECT NOW()'))
4949
let after = false
50-
cursor.read(1, function(err) {
50+
cursor.read(1, function (err) {
51+
assert(!err)
5152
assert.equal(after, true, 'should not call read sync')
5253
cursor.read(1, function (err) {
54+
assert(!err)
5355
after = false
5456
cursor.read(1, function (err) {
57+
assert(!err)
5558
assert.equal(after, true, 'should not call read sync')
5659
client.end()
5760
done()

0 commit comments

Comments
 (0)