Skip to content

Commit 0d4a1ad

Browse files
authored
Merge pull request #79 from share/add-linting
Add linting
2 parents 46f4cb6 + d9d8c5a commit 0d4a1ad

File tree

8 files changed

+213
-142
lines changed

8 files changed

+213
-142
lines changed

.eslintrc.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// The ESLint ecmaVersion argument is inconsistently used. Some rules will ignore it entirely, so if the rule has
2+
// been set, it will still error even if it's not applicable to that version number. Since Google sets these
3+
// rules, we have to turn them off ourselves.
4+
const DISABLED_ES6_OPTIONS = {
5+
'no-var': 'off',
6+
'prefer-rest-params': 'off'
7+
};
8+
9+
const SHAREDB_RULES = {
10+
// Comma dangle is not supported in ES3
11+
'comma-dangle': ['error', 'never'],
12+
// We control our own objects and prototypes, so no need for this check
13+
'guard-for-in': 'off',
14+
// Google prescribes different indents for different cases. Let's just use 2 spaces everywhere. Note that we have
15+
// to override ESLint's default of 0 indents for this.
16+
'indent': ['error', 2, {
17+
'SwitchCase': 1
18+
}],
19+
// Less aggressive line length than Google, which is especially useful when we have a lot of callbacks in our code
20+
'max-len': ['error',
21+
{
22+
code: 120,
23+
tabWidth: 2,
24+
ignoreUrls: true,
25+
}
26+
],
27+
// Google overrides the default ESLint behaviour here, which is slightly better for catching erroneously unused variables
28+
'no-unused-vars': ['error', {vars: 'all', args: 'after-used'}],
29+
// It's more readable to ensure we only have one statement per line
30+
'max-statements-per-line': ['error', {max: 1}],
31+
// as-needed quote props are easier to write
32+
'quote-props': ['error', 'as-needed'],
33+
'require-jsdoc': 'off',
34+
'valid-jsdoc': 'off'
35+
};
36+
37+
module.exports = {
38+
extends: 'google',
39+
parserOptions: {
40+
ecmaVersion: 3
41+
},
42+
rules: Object.assign(
43+
{},
44+
DISABLED_ES6_OPTIONS,
45+
SHAREDB_RULES
46+
),
47+
};

index.js

Lines changed: 56 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ ShareDbMongo.prototype._writeSnapshot = function(collectionName, id, snapshot, o
228228
if (err) return callback(err);
229229
var doc = castToDoc(id, snapshot, opLink);
230230
if (doc._v === 1) {
231-
collection.insertOne(doc, function(err, result) {
231+
collection.insertOne(doc, function(err) {
232232
if (err) {
233233
// Return non-success instead of duplicate key error, since this is
234234
// expected to occur during simultaneous creates on the same id
@@ -277,7 +277,6 @@ ShareDbMongo.prototype.getSnapshotBulk = function(collectionName, ids, fields, o
277277
var snapshot = castToSnapshot(docs[i]);
278278
snapshotMap[snapshot.id] = snapshot;
279279
}
280-
var uncreated = [];
281280
for (var i = 0; i < ids.length; i++) {
282281
var id = ids[i];
283282
if (snapshotMap[id]) continue;
@@ -538,7 +537,7 @@ function getLatestDeleteOp(ops) {
538537
}
539538

540539
function getLinkedOps(ops, to, link) {
541-
var linkedOps = []
540+
var linkedOps = [];
542541
for (var i = ops.length; i-- && link;) {
543542
var op = ops[i];
544543
if (link.equals ? !link.equals(op._id) : link !== op._id) continue;
@@ -556,7 +555,7 @@ function getOpsQuery(id, from, to) {
556555
from = from == null ? 0 : from;
557556
var query = {
558557
d: id,
559-
v: { $gte: from }
558+
v: {$gte: from}
560559
};
561560

562561
if (to != null) {
@@ -624,7 +623,7 @@ ShareDbMongo.prototype._getOpLink = function(collectionName, id, to, callback) {
624623
if (!this.getOpsWithoutStrictLinking) return this._getSnapshotOpLink(collectionName, id, callback);
625624

626625
var db = this;
627-
this.getOpCollection(collectionName, function (error, collection) {
626+
this.getOpCollection(collectionName, function(error, collection) {
628627
if (error) return callback(error);
629628

630629
// If to is null, we want the most recent version, so just return the
@@ -635,7 +634,7 @@ ShareDbMongo.prototype._getOpLink = function(collectionName, id, to, callback) {
635634

636635
var query = {
637636
d: id,
638-
v: { $gte: to }
637+
v: {$gte: to}
639638
};
640639

641640
var projection = {
@@ -644,11 +643,11 @@ ShareDbMongo.prototype._getOpLink = function(collectionName, id, to, callback) {
644643
o: 1
645644
};
646645

647-
var cursor = collection.find(query).sort({ v: 1 }).project(projection);
646+
var cursor = collection.find(query).sort({v: 1}).project(projection);
648647

649-
getFirstOpWithUniqueVersion(cursor, null, function (error, op) {
648+
getFirstOpWithUniqueVersion(cursor, null, function(error, op) {
650649
if (error) return callback(error);
651-
if (op) return callback(null, { _o: op.o, _v: op.v });
650+
if (op) return callback(null, {_o: op.o, _v: op.v});
652651

653652
// If we couldn't find an op to link back from, then fall back to using the current
654653
// snapshot, which is guaranteed to have a link to a valid op.
@@ -674,7 +673,7 @@ function getFirstOpWithUniqueVersion(cursor, opLinkValidator, callback) {
674673
return closeCursor(cursor, callback, error, opWithUniqueVersion);
675674
}
676675

677-
cursor.next(function (error, op) {
676+
cursor.next(function(error, op) {
678677
if (error) {
679678
return closeCursor(cursor, callback, error);
680679
}
@@ -685,7 +684,7 @@ function getFirstOpWithUniqueVersion(cursor, opLinkValidator, callback) {
685684
}
686685

687686
function closeCursor(cursor, callback, error, returnValue) {
688-
cursor.close(function (closeError) {
687+
cursor.close(function(closeError) {
689688
error = error || closeError;
690689
callback(error, returnValue);
691690
});
@@ -1086,7 +1085,7 @@ function parseQuery(inputQuery) {
10861085
var cursorOperationValue = null;
10871086

10881087
if (inputQuery.$query) {
1089-
throw new Error("unexpected $query: should have called checkQuery");
1088+
throw new Error('unexpected $query: should have called checkQuery');
10901089
} else {
10911090
for (var key in inputQuery) {
10921091
if (collectionOperationsMap[key]) {
@@ -1276,8 +1275,8 @@ function castToDoc(id, snapshot, opLink) {
12761275
var data = snapshot.data;
12771276
var doc =
12781277
(isObject(data)) ? shallowClone(data) :
1279-
(data === undefined) ? {} :
1280-
{_data: data};
1278+
(data === undefined) ? {} :
1279+
{_data: data};
12811280
doc._id = id;
12821281
doc._type = snapshot.type;
12831282
doc._v = snapshot.v;
@@ -1361,10 +1360,10 @@ function getProjection(fields, options) {
13611360
}
13621361

13631362
var collectionOperationsMap = {
1364-
'$distinct': function(collection, query, value, cb) {
1363+
$distinct: function(collection, query, value, cb) {
13651364
collection.distinct(value.field, query, cb);
13661365
},
1367-
'$aggregate': function(collection, query, value, cb) {
1366+
$aggregate: function(collection, query, value, cb) {
13681367
collection.aggregate(value, function(err, resultsOrCursor) {
13691368
if (err) {
13701369
return cb(err);
@@ -1384,7 +1383,7 @@ var collectionOperationsMap = {
13841383
}
13851384
});
13861385
},
1387-
'$mapReduce': function(collection, query, value, cb) {
1386+
$mapReduce: function(collection, query, value, cb) {
13881387
if (typeof value !== 'object') {
13891388
var err = ShareDbMongo.malformedQueryOperatorError('$mapReduce');
13901389
return cb(err);
@@ -1400,61 +1399,79 @@ var collectionOperationsMap = {
14001399
};
14011400

14021401
var cursorOperationsMap = {
1403-
'$count': function(cursor, value, cb) {
1402+
$count: function(cursor, value, cb) {
14041403
cursor.count(cb);
14051404
},
1406-
'$explain': function(cursor, verbosity, cb) {
1405+
$explain: function(cursor, verbosity, cb) {
14071406
cursor.explain(verbosity, cb);
14081407
},
1409-
'$map': function(cursor, fn, cb) {
1408+
$map: function(cursor, fn, cb) {
14101409
cursor.map(fn, cb);
14111410
}
14121411
};
14131412

14141413
var cursorTransformsMap = {
1415-
'$batchSize': function(cursor, size) { return cursor.batchSize(size); },
1416-
'$comment': function(cursor, text) { return cursor.comment(text); },
1417-
'$hint': function(cursor, index) { return cursor.hint(index); },
1418-
'$max': function(cursor, value) { return cursor.max(value); },
1419-
'$maxScan': function(cursor, value) { return cursor.maxScan(value); },
1420-
'$maxTimeMS': function(cursor, milliseconds) {
1414+
$batchSize: function(cursor, size) {
1415+
return cursor.batchSize(size);
1416+
},
1417+
$comment: function(cursor, text) {
1418+
return cursor.comment(text);
1419+
},
1420+
$hint: function(cursor, index) {
1421+
return cursor.hint(index);
1422+
},
1423+
$max: function(cursor, value) {
1424+
return cursor.max(value);
1425+
},
1426+
$maxScan: function(cursor, value) {
1427+
return cursor.maxScan(value);
1428+
},
1429+
$maxTimeMS: function(cursor, milliseconds) {
14211430
return cursor.maxTimeMS(milliseconds);
14221431
},
1423-
'$min': function(cursor, value) { return cursor.min(value); },
1424-
'$noCursorTimeout': function(cursor) {
1432+
$min: function(cursor, value) {
1433+
return cursor.min(value);
1434+
},
1435+
$noCursorTimeout: function(cursor) {
14251436
// no argument to cursor method
14261437
return cursor.noCursorTimeout();
14271438
},
1428-
'$orderby': function(cursor, value) {
1439+
$orderby: function(cursor, value) {
14291440
console.warn('Deprecated: $orderby; Use $sort.');
14301441
return cursor.sort(value);
14311442
},
1432-
'$readConcern': function(cursor, level) {
1443+
$readConcern: function(cursor, level) {
14331444
return cursor.readConcern(level);
14341445
},
1435-
'$readPref': function(cursor, value) {
1446+
$readPref: function(cursor, value) {
14361447
// The Mongo driver cursor method takes two argments. Our queries
14371448
// have a single value for the '$readPref' property. Interpret as
14381449
// an object with {mode, tagSet}.
14391450
if (typeof value !== 'object') return null;
14401451
return cursor.readPref(value.mode, value.tagSet);
14411452
},
1442-
'$returnKey': function(cursor) {
1453+
$returnKey: function(cursor) {
14431454
// no argument to cursor method
14441455
return cursor.returnKey();
14451456
},
1446-
'$snapshot': function(cursor) {
1457+
$snapshot: function(cursor) {
14471458
// no argument to cursor method
14481459
return cursor.snapshot();
14491460
},
1450-
'$sort': function(cursor, value) { return cursor.sort(value); },
1451-
'$skip': function(cursor, value) { return cursor.skip(value); },
1452-
'$limit': function(cursor, value) { return cursor.limit(value); },
1453-
'$showDiskLoc': function(cursor, value) {
1461+
$sort: function(cursor, value) {
1462+
return cursor.sort(value);
1463+
},
1464+
$skip: function(cursor, value) {
1465+
return cursor.skip(value);
1466+
},
1467+
$limit: function(cursor, value) {
1468+
return cursor.limit(value);
1469+
},
1470+
$showDiskLoc: function(cursor, value) {
14541471
console.warn('Deprecated: $showDiskLoc; Use $showRecordId.');
14551472
return cursor.showRecordId(value);
14561473
},
1457-
'$showRecordId': function(cursor) {
1474+
$showRecordId: function(cursor) {
14581475
// no argument to cursor method
14591476
return cursor.showRecordId();
14601477
}
@@ -1483,7 +1500,7 @@ ShareDbMongo.$queryDeprecatedError = function() {
14831500
return {code: 4106, message: '$query property deprecated in queries'};
14841501
};
14851502
ShareDbMongo.malformedQueryOperatorError = function(operator) {
1486-
return {code: 4107, message: "Malformed query operator: " + operator};
1503+
return {code: 4107, message: 'Malformed query operator: ' + operator};
14871504
};
14881505
ShareDbMongo.onlyOneCollectionOperationError = function(operation1, operation2) {
14891506
return {

op-link-validator.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,41 +37,41 @@ function OpLinkValidator() {
3737
this.oneBeforePreviousOp = undefined;
3838
}
3939

40-
OpLinkValidator.prototype.push = function (op) {
40+
OpLinkValidator.prototype.push = function(op) {
4141
this.oneBeforePreviousOp = this.previousOp;
4242
this.previousOp = this.currentOp;
4343
this.currentOp = op;
4444
};
4545

46-
OpLinkValidator.prototype.opWithUniqueVersion = function () {
46+
OpLinkValidator.prototype.opWithUniqueVersion = function() {
4747
return this._previousVersionWasUnique() ? this.previousOp : null;
4848
};
4949

50-
OpLinkValidator.prototype.isAtEndOfList = function () {
50+
OpLinkValidator.prototype.isAtEndOfList = function() {
5151
// We ascribe a special meaning to a current op of null
5252
// being that we're at the end of the list, because this
5353
// is the value that the Mongo cursor will return when
5454
// the cursor is exhausted
5555
return this.currentOp === null;
5656
};
5757

58-
OpLinkValidator.prototype._previousVersionWasUnique = function () {
59-
const previousVersion = this._previousVersion();
58+
OpLinkValidator.prototype._previousVersionWasUnique = function() {
59+
var previousVersion = this._previousVersion();
6060

6161
return typeof previousVersion === 'number'
6262
&& previousVersion !== this._currentVersion()
6363
&& previousVersion !== this._oneBeforePreviousVersion();
6464
};
6565

66-
OpLinkValidator.prototype._currentVersion = function () {
66+
OpLinkValidator.prototype._currentVersion = function() {
6767
return this.currentOp && this.currentOp.v;
6868
};
6969

70-
OpLinkValidator.prototype._previousVersion = function () {
70+
OpLinkValidator.prototype._previousVersion = function() {
7171
return this.previousOp && this.previousOp.v;
7272
};
7373

74-
OpLinkValidator.prototype._oneBeforePreviousVersion = function () {
74+
OpLinkValidator.prototype._oneBeforePreviousVersion = function() {
7575
return this.oneBeforePreviousOp && this.oneBeforePreviousOp.v;
7676
};
7777

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,19 @@
1010
},
1111
"devDependencies": {
1212
"coveralls": "^2.11.8",
13+
"eslint": "^5.16.0",
14+
"eslint-config-google": "^0.13.0",
1315
"expect.js": "^0.3.1",
1416
"istanbul": "^0.4.2",
1517
"mocha": "^2.3.3",
1618
"sharedb-mingo-memory": "^1.0.2",
1719
"sinon": "^6.1.5"
1820
},
1921
"scripts": {
20-
"test": "node_modules/.bin/mocha",
21-
"test-cover": "node_modules/istanbul/lib/cli.js cover node_modules/mocha/bin/_mocha"
22+
"lint": "./node_modules/.bin/eslint --ignore-path .gitignore '**/*.js'",
23+
"lint:fix": "npm run lint -- --fix",
24+
"test": "npm run lint && node_modules/.bin/mocha",
25+
"test-cover": "npm run lint && node_modules/istanbul/lib/cli.js cover node_modules/mocha/bin/_mocha"
2226
},
2327
"repository": "git://github.com/share/sharedb-mongo.git",
2428
"author": "Nate Smith and Joseph Gentle",

0 commit comments

Comments
 (0)