Skip to content

Add linting #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// The ESLint ecmaVersion argument is inconsistently used. Some rules will ignore it entirely, so if the rule has
// been set, it will still error even if it's not applicable to that version number. Since Google sets these
// rules, we have to turn them off ourselves.
const DISABLED_ES6_OPTIONS = {
'no-var': 'off',
'prefer-rest-params': 'off'
};

const SHAREDB_RULES = {
// Comma dangle is not supported in ES3
'comma-dangle': ['error', 'never'],
// We control our own objects and prototypes, so no need for this check
'guard-for-in': 'off',
// Google prescribes different indents for different cases. Let's just use 2 spaces everywhere. Note that we have
// to override ESLint's default of 0 indents for this.
'indent': ['error', 2, {
'SwitchCase': 1
}],
// Less aggressive line length than Google, which is especially useful when we have a lot of callbacks in our code
'max-len': ['error',
{
code: 120,
tabWidth: 2,
ignoreUrls: true,
}
],
// Google overrides the default ESLint behaviour here, which is slightly better for catching erroneously unused variables
'no-unused-vars': ['error', {vars: 'all', args: 'after-used'}],
// It's more readable to ensure we only have one statement per line
'max-statements-per-line': ['error', {max: 1}],
// as-needed quote props are easier to write
'quote-props': ['error', 'as-needed'],
'require-jsdoc': 'off',
'valid-jsdoc': 'off'
};

module.exports = {
extends: 'google',
parserOptions: {
ecmaVersion: 3
},
rules: Object.assign(
{},
DISABLED_ES6_OPTIONS,
SHAREDB_RULES
),
};
95 changes: 56 additions & 39 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ ShareDbMongo.prototype._writeSnapshot = function(collectionName, id, snapshot, o
if (err) return callback(err);
var doc = castToDoc(id, snapshot, opLink);
if (doc._v === 1) {
collection.insertOne(doc, function(err, result) {
collection.insertOne(doc, function(err) {
if (err) {
// Return non-success instead of duplicate key error, since this is
// expected to occur during simultaneous creates on the same id
Expand Down Expand Up @@ -277,7 +277,6 @@ ShareDbMongo.prototype.getSnapshotBulk = function(collectionName, ids, fields, o
var snapshot = castToSnapshot(docs[i]);
snapshotMap[snapshot.id] = snapshot;
}
var uncreated = [];
for (var i = 0; i < ids.length; i++) {
var id = ids[i];
if (snapshotMap[id]) continue;
Expand Down Expand Up @@ -538,7 +537,7 @@ function getLatestDeleteOp(ops) {
}

function getLinkedOps(ops, to, link) {
var linkedOps = []
var linkedOps = [];
for (var i = ops.length; i-- && link;) {
var op = ops[i];
if (link.equals ? !link.equals(op._id) : link !== op._id) continue;
Expand All @@ -556,7 +555,7 @@ function getOpsQuery(id, from, to) {
from = from == null ? 0 : from;
var query = {
d: id,
v: { $gte: from }
v: {$gte: from}
};

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

var db = this;
this.getOpCollection(collectionName, function (error, collection) {
this.getOpCollection(collectionName, function(error, collection) {
if (error) return callback(error);

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

var query = {
d: id,
v: { $gte: to }
v: {$gte: to}
};

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

var cursor = collection.find(query).sort({ v: 1 }).project(projection);
var cursor = collection.find(query).sort({v: 1}).project(projection);

getFirstOpWithUniqueVersion(cursor, null, function (error, op) {
getFirstOpWithUniqueVersion(cursor, null, function(error, op) {
if (error) return callback(error);
if (op) return callback(null, { _o: op.o, _v: op.v });
if (op) return callback(null, {_o: op.o, _v: op.v});

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

cursor.next(function (error, op) {
cursor.next(function(error, op) {
if (error) {
return closeCursor(cursor, callback, error);
}
Expand All @@ -685,7 +684,7 @@ function getFirstOpWithUniqueVersion(cursor, opLinkValidator, callback) {
}

function closeCursor(cursor, callback, error, returnValue) {
cursor.close(function (closeError) {
cursor.close(function(closeError) {
error = error || closeError;
callback(error, returnValue);
});
Expand Down Expand Up @@ -1086,7 +1085,7 @@ function parseQuery(inputQuery) {
var cursorOperationValue = null;

if (inputQuery.$query) {
throw new Error("unexpected $query: should have called checkQuery");
throw new Error('unexpected $query: should have called checkQuery');
} else {
for (var key in inputQuery) {
if (collectionOperationsMap[key]) {
Expand Down Expand Up @@ -1276,8 +1275,8 @@ function castToDoc(id, snapshot, opLink) {
var data = snapshot.data;
var doc =
(isObject(data)) ? shallowClone(data) :
(data === undefined) ? {} :
{_data: data};
(data === undefined) ? {} :
{_data: data};
doc._id = id;
doc._type = snapshot.type;
doc._v = snapshot.v;
Expand Down Expand Up @@ -1361,10 +1360,10 @@ function getProjection(fields, options) {
}

var collectionOperationsMap = {
'$distinct': function(collection, query, value, cb) {
$distinct: function(collection, query, value, cb) {
collection.distinct(value.field, query, cb);
},
'$aggregate': function(collection, query, value, cb) {
$aggregate: function(collection, query, value, cb) {
collection.aggregate(value, function(err, resultsOrCursor) {
if (err) {
return cb(err);
Expand All @@ -1384,7 +1383,7 @@ var collectionOperationsMap = {
}
});
},
'$mapReduce': function(collection, query, value, cb) {
$mapReduce: function(collection, query, value, cb) {
if (typeof value !== 'object') {
var err = ShareDbMongo.malformedQueryOperatorError('$mapReduce');
return cb(err);
Expand All @@ -1400,61 +1399,79 @@ var collectionOperationsMap = {
};

var cursorOperationsMap = {
'$count': function(cursor, value, cb) {
$count: function(cursor, value, cb) {
cursor.count(cb);
},
'$explain': function(cursor, verbosity, cb) {
$explain: function(cursor, verbosity, cb) {
cursor.explain(verbosity, cb);
},
'$map': function(cursor, fn, cb) {
$map: function(cursor, fn, cb) {
cursor.map(fn, cb);
}
};

var cursorTransformsMap = {
'$batchSize': function(cursor, size) { return cursor.batchSize(size); },
'$comment': function(cursor, text) { return cursor.comment(text); },
'$hint': function(cursor, index) { return cursor.hint(index); },
'$max': function(cursor, value) { return cursor.max(value); },
'$maxScan': function(cursor, value) { return cursor.maxScan(value); },
'$maxTimeMS': function(cursor, milliseconds) {
$batchSize: function(cursor, size) {
return cursor.batchSize(size);
},
$comment: function(cursor, text) {
return cursor.comment(text);
},
$hint: function(cursor, index) {
return cursor.hint(index);
},
$max: function(cursor, value) {
return cursor.max(value);
},
$maxScan: function(cursor, value) {
return cursor.maxScan(value);
},
$maxTimeMS: function(cursor, milliseconds) {
return cursor.maxTimeMS(milliseconds);
},
'$min': function(cursor, value) { return cursor.min(value); },
'$noCursorTimeout': function(cursor) {
$min: function(cursor, value) {
return cursor.min(value);
},
$noCursorTimeout: function(cursor) {
// no argument to cursor method
return cursor.noCursorTimeout();
},
'$orderby': function(cursor, value) {
$orderby: function(cursor, value) {
console.warn('Deprecated: $orderby; Use $sort.');
return cursor.sort(value);
},
'$readConcern': function(cursor, level) {
$readConcern: function(cursor, level) {
return cursor.readConcern(level);
},
'$readPref': function(cursor, value) {
$readPref: function(cursor, value) {
// The Mongo driver cursor method takes two argments. Our queries
// have a single value for the '$readPref' property. Interpret as
// an object with {mode, tagSet}.
if (typeof value !== 'object') return null;
return cursor.readPref(value.mode, value.tagSet);
},
'$returnKey': function(cursor) {
$returnKey: function(cursor) {
// no argument to cursor method
return cursor.returnKey();
},
'$snapshot': function(cursor) {
$snapshot: function(cursor) {
// no argument to cursor method
return cursor.snapshot();
},
'$sort': function(cursor, value) { return cursor.sort(value); },
'$skip': function(cursor, value) { return cursor.skip(value); },
'$limit': function(cursor, value) { return cursor.limit(value); },
'$showDiskLoc': function(cursor, value) {
$sort: function(cursor, value) {
return cursor.sort(value);
},
$skip: function(cursor, value) {
return cursor.skip(value);
},
$limit: function(cursor, value) {
return cursor.limit(value);
},
$showDiskLoc: function(cursor, value) {
console.warn('Deprecated: $showDiskLoc; Use $showRecordId.');
return cursor.showRecordId(value);
},
'$showRecordId': function(cursor) {
$showRecordId: function(cursor) {
// no argument to cursor method
return cursor.showRecordId();
}
Expand Down Expand Up @@ -1483,7 +1500,7 @@ ShareDbMongo.$queryDeprecatedError = function() {
return {code: 4106, message: '$query property deprecated in queries'};
};
ShareDbMongo.malformedQueryOperatorError = function(operator) {
return {code: 4107, message: "Malformed query operator: " + operator};
return {code: 4107, message: 'Malformed query operator: ' + operator};
};
ShareDbMongo.onlyOneCollectionOperationError = function(operation1, operation2) {
return {
Expand Down
16 changes: 8 additions & 8 deletions op-link-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,41 +37,41 @@ function OpLinkValidator() {
this.oneBeforePreviousOp = undefined;
}

OpLinkValidator.prototype.push = function (op) {
OpLinkValidator.prototype.push = function(op) {
this.oneBeforePreviousOp = this.previousOp;
this.previousOp = this.currentOp;
this.currentOp = op;
};

OpLinkValidator.prototype.opWithUniqueVersion = function () {
OpLinkValidator.prototype.opWithUniqueVersion = function() {
return this._previousVersionWasUnique() ? this.previousOp : null;
};

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

OpLinkValidator.prototype._previousVersionWasUnique = function () {
const previousVersion = this._previousVersion();
OpLinkValidator.prototype._previousVersionWasUnique = function() {
var previousVersion = this._previousVersion();

return typeof previousVersion === 'number'
&& previousVersion !== this._currentVersion()
&& previousVersion !== this._oneBeforePreviousVersion();
};

OpLinkValidator.prototype._currentVersion = function () {
OpLinkValidator.prototype._currentVersion = function() {
return this.currentOp && this.currentOp.v;
};

OpLinkValidator.prototype._previousVersion = function () {
OpLinkValidator.prototype._previousVersion = function() {
return this.previousOp && this.previousOp.v;
};

OpLinkValidator.prototype._oneBeforePreviousVersion = function () {
OpLinkValidator.prototype._oneBeforePreviousVersion = function() {
return this.oneBeforePreviousOp && this.oneBeforePreviousOp.v;
};

Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@
},
"devDependencies": {
"coveralls": "^2.11.8",
"eslint": "^5.16.0",
"eslint-config-google": "^0.13.0",
"expect.js": "^0.3.1",
"istanbul": "^0.4.2",
"mocha": "^2.3.3",
"sharedb-mingo-memory": "^1.0.2",
"sinon": "^6.1.5"
},
"scripts": {
"test": "node_modules/.bin/mocha",
"test-cover": "node_modules/istanbul/lib/cli.js cover node_modules/mocha/bin/_mocha"
"lint": "./node_modules/.bin/eslint --ignore-path .gitignore '**/*.js'",
"lint:fix": "npm run lint -- --fix",
"test": "npm run lint && node_modules/.bin/mocha",
"test-cover": "npm run lint && node_modules/istanbul/lib/cli.js cover node_modules/mocha/bin/_mocha"
},
"repository": "git://github.com/share/sharedb-mongo.git",
"author": "Nate Smith and Joseph Gentle",
Expand Down
Loading