diff --git a/lib/connectors/kv-memory.js b/lib/connectors/kv-memory.js index 9962ff49..8ac84c0a 100644 --- a/lib/connectors/kv-memory.js +++ b/lib/connectors/kv-memory.js @@ -33,14 +33,13 @@ KeyValueMemoryConnector.prototype._setupRegularCleanup = function() { // in order to release memory. Note that GET operation checks // key expiration too, the scheduled cleanup is merely a performance // optimization. - const self = this; - var timer = this._cleanupTimer = setInterval( - function() { - if (self && self._removeExpiredItems) { - self._removeExpiredItems(); + this._cleanupTimer = setInterval( + () => { + if (this && this._removeExpiredItems) { + this._removeExpiredItems(); } else { // The datasource/connector was destroyed - cancel the timer - clearInterval(timer); + clearInterval(this._cleanupTimer); } }, 1000 diff --git a/lib/connectors/memory.js b/lib/connectors/memory.js index 9d44c8d1..d3f37fe0 100644 --- a/lib/connectors/memory.js +++ b/lib/connectors/memory.js @@ -119,7 +119,7 @@ Memory.prototype.setupFileQueue = function() { const file = self.settings.file; if (task.operation === 'write') { // Flush out the models/ids - var data = JSON.stringify({ + const data = JSON.stringify({ ids: self.ids, models: self.cache, }, null, ' '); @@ -130,7 +130,7 @@ Memory.prototype.setupFileQueue = function() { callback(err, task.data); }); } else if (task.operation === 'read') { - debug('Reading cache from %s: %s', file, data); + debug('Reading cache from %s', file); fs.readFile(file, { encoding: 'utf8', flag: 'r', diff --git a/lib/connectors/transient.js b/lib/connectors/transient.js index 8baa5b5d..6edb9739 100644 --- a/lib/connectors/transient.js +++ b/lib/connectors/transient.js @@ -91,8 +91,9 @@ Transient.prototype.count = function count(model, callback, where) { Transient.prototype.create = function create(model, data, callback) { const props = this._models[model].properties; const idName = this.idName(model); + let id = undefined; if (idName && props[idName]) { - var id = this.getIdValue(model, data) || this.generateId(model, data, idName); + id = this.getIdValue(model, data) || this.generateId(model, data, idName); id = (props[idName] && props[idName].type && props[idName].type(id)) || id; this.setIdValue(model, data, id); } diff --git a/lib/dao.js b/lib/dao.js index a6f95d57..cf7dc7ea 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -89,9 +89,9 @@ function convertSubsetOfPropertiesByType(inst, data) { function applyStrictCheck(model, strict, data, inst, cb) { const props = model.definition.properties; const keys = Object.keys(data); - let result = {}, key; + const result = {}; for (let i = 0; i < keys.length; i++) { - key = keys[i]; + const key = keys[i]; if (props[key]) { result[key] = data[key]; } else if (strict && strict !== 'filter') { @@ -1105,7 +1105,8 @@ DataAccessObject.findOrCreate = function findOrCreate(query, data, options, cb) if (err) return cb(err); data = ctx.data; - let obj, Model = self.lookupModel(data); + const Model = self.lookupModel(data); + let obj; if (data) { obj = new Model(data, {fields: query.fields, applySetters: false, @@ -2594,9 +2595,10 @@ DataAccessObject.replaceById = function(id, data, options, cb) { const pkName = idName(this); if (!data[pkName]) data[pkName] = id; + let Model = this; + let inst; try { - var Model = this; - var inst = new Model(data, {persisted: true}); + inst = new Model(data, {persisted: true}); const enforced = {}; this.applyProperties(enforced, inst); @@ -2808,7 +2810,7 @@ function(data, options, cb) { for (let i = 0, n = idNames.length; i < n; i++) { const idName = idNames[i]; if (data[idName] !== undefined && !idEquals(data[idName], inst[idName])) { - var err = new Error(g.f('{{id}} cannot be updated from ' + + const err = new Error(g.f('{{id}} cannot be updated from ' + '%s to %s when {{forceId}} is set to true', inst[idName], data[idName])); err.statusCode = 400; diff --git a/lib/datasource.js b/lib/datasource.js index 0383fdda..e55652c5 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -2275,7 +2275,7 @@ DataSource.prototype.transaction = function(execute, options, cb) { }); }); - var done = function(err) { + let done = function(err) { if (err) { transaction.rollback(function(error) { cb(err || error); diff --git a/lib/geo.js b/lib/geo.js index 094fd5e7..9314b64c 100644 --- a/lib/geo.js +++ b/lib/geo.js @@ -12,6 +12,10 @@ const assert = require('assert'); */ exports.nearFilter = function nearFilter(where) { + const nearResults = []; + nearSearch(where); + return (!nearResults.length ? false : nearResults); + function nearSearch(clause, parentKeys) { if (typeof clause !== 'object') { return false; @@ -41,10 +45,6 @@ exports.nearFilter = function nearFilter(where) { } }); } - var nearResults = []; - nearSearch(where); - - return (!nearResults.length ? false : nearResults); }; /*! diff --git a/lib/include.js b/lib/include.js index 28e9e6f3..117c6e4d 100644 --- a/lib/include.js +++ b/lib/include.js @@ -505,8 +505,10 @@ Inclusion.include = function(objects, include, options, cb) { function linkManyToMany(target, next) { const targetId = target[modelToIdName]; if (!targetId) { - const err = new Error(g.f('LinkManyToMany received target that doesn\'t contain required "%s"', - modelToIdName)); + const err = new Error(g.f( + 'LinkManyToMany received target that doesn\'t contain required "%s"', + modelToIdName + )); return next(err); } const objList = targetObjsMap[targetId.toString()]; diff --git a/lib/model-builder.js b/lib/model-builder.js index c9908095..cd831f43 100644 --- a/lib/model-builder.js +++ b/lib/model-builder.js @@ -229,7 +229,7 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett hiddenProperty(ModelClass, '_warned', {}); // inherit ModelBaseClass static methods - for (var i in ModelBaseClass) { + for (const i in ModelBaseClass) { // We need to skip properties that are already in the subclass, for example, the event emitter methods if (i !== '_mixins' && !(i in ModelClass)) { ModelClass[i] = ModelBaseClass[i]; @@ -306,7 +306,7 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett idNames = modelDefinition.idNames(); // Reload it after rebuild // Create a virtual property 'id' if (idNames.length === 1) { - var idProp = idNames[0]; + const idProp = idNames[0]; if (idProp !== 'id') { Object.defineProperty(ModelClass.prototype, 'id', { get: function() { @@ -323,7 +323,7 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett get: function() { const compositeId = {}; const idNames = ModelClass.definition.idNames(); - for (var i = 0, p; i < idNames.length; i++) { + for (let i = 0, p; i < idNames.length; i++) { p = idNames[i]; compositeId[p] = this.__data[p]; } @@ -339,7 +339,7 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett let forceId = ModelClass.settings.forceId; if (idNames.length > 0) { const idName = modelDefinition.idName(); - idProp = ModelClass.definition.rawProperties[idName]; + const idProp = ModelClass.definition.rawProperties[idName]; if (idProp.generated && forceId !== false) { forceId = 'auto'; } else if (!idProp.generated && forceId === 'auto') { @@ -624,7 +624,7 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett const props = ModelClass.definition.properties; let keys = Object.keys(props); let size = keys.length; - for (i = 0; i < size; i++) { + for (let i = 0; i < size; i++) { const propertyName = keys[i]; ModelClass.registerProperty(propertyName); } @@ -632,8 +632,8 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett const mixinSettings = settings.mixins || {}; keys = Object.keys(mixinSettings); size = keys.length; - for (i = 0; i < size; i++) { - var name = keys[i]; + for (let i = 0; i < size; i++) { + const name = keys[i]; let mixin = mixinSettings[name]; if (mixin === true) { mixin = {}; @@ -929,12 +929,9 @@ ModelBuilder.prototype.buildModels = function(schemas, createModel) { for (let s = 0, n = schemas.length; s < n; s++) { const name = this.getSchemaName(schemas[s].name); schemas[s].name = name; - var model; - if (typeof createModel === 'function') { - model = createModel(schemas[s].name, schemas[s].properties, schemas[s].options); - } else { - model = this.define(schemas[s].name, schemas[s].properties, schemas[s].options); - } + const model = typeof createModel === 'function' ? + createModel(schemas[s].name, schemas[s].properties, schemas[s].options) : + this.define(schemas[s].name, schemas[s].properties, schemas[s].options); models[name] = model; relations = relations.concat(model.definition.relations); } diff --git a/lib/model.js b/lib/model.js index d4c2aad1..5b2bc33e 100644 --- a/lib/model.js +++ b/lib/model.js @@ -187,7 +187,7 @@ ModelBaseClass.prototype._initProperties = function(data, options) { let size = keys.length; let p, propVal; - for (var k = 0; k < size; k++) { + for (let k = 0; k < size; k++) { p = keys[k]; propVal = data[p]; if (typeof propVal === 'function') { @@ -208,7 +208,7 @@ ModelBaseClass.prototype._initProperties = function(data, options) { } else if (ctor.relations[p]) { const relationType = ctor.relations[p].type; - var modelTo; + let modelTo; if (!properties[p]) { modelTo = ctor.relations[p].modelTo || ModelBaseClass; const multiple = ctor.relations[p].multiple; @@ -272,7 +272,7 @@ ModelBaseClass.prototype._initProperties = function(data, options) { size = keys.length; - for (k = 0; k < size; k++) { + for (let k = 0; k < size; k++) { p = keys[k]; propVal = self.__data[p]; const type = properties[p].type; @@ -446,7 +446,7 @@ ModelBaseClass.prototype.toObject = function(onlySchema, removeHidden, removePro let keys = Object.keys(props); let propertyName, val; - for (var i = 0; i < keys.length; i++) { + for (let i = 0; i < keys.length; i++) { propertyName = keys[i]; val = self[propertyName]; @@ -483,7 +483,7 @@ ModelBaseClass.prototype.toObject = function(onlySchema, removeHidden, removePro // triggered to add it to __data keys = Object.keys(self); let size = keys.length; - for (i = 0; i < size; i++) { + for (let i = 0; i < size; i++) { propertyName = keys[i]; if (props[propertyName]) { continue; @@ -517,7 +517,7 @@ ModelBaseClass.prototype.toObject = function(onlySchema, removeHidden, removePro // Now continue to check __data keys = Object.keys(self.__data); size = keys.length; - for (i = 0; i < size; i++) { + for (let i = 0; i < size; i++) { propertyName = keys[i]; if (propertyName.indexOf('__') === 0) { continue; diff --git a/lib/relation-definition.js b/lib/relation-definition.js index 0376797c..6d31e025 100644 --- a/lib/relation-definition.js +++ b/lib/relation-definition.js @@ -110,14 +110,13 @@ function bindRelationMethods(relation, relationMethod, definition) { function preventFkOverride(inst, data, fkProp) { if (!fkProp) return undefined; if (data[fkProp] !== undefined && !idEquals(data[fkProp], inst[fkProp])) { - var err = new Error(g.f( + return new Error(g.f( 'Cannot override foreign key %s from %s to %s', fkProp, inst[fkProp], data[fkProp] )); } - return err; } /** diff --git a/lib/scope.js b/lib/scope.js index 17ffd93b..a163a0bb 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -99,10 +99,11 @@ ScopeDefinition.prototype.related = function(receiver, scopeParams, condOrRefres const scopeOnRelatedModel = params.collect && params.include.scope !== null && typeof params.include.scope === 'object'; + let filter, queryRelated; if (scopeOnRelatedModel) { - var filter = params.include; + filter = params.include; // The filter applied on relatedModel - var queryRelated = filter.scope; + queryRelated = filter.scope; delete params.include.scope; } @@ -239,7 +240,7 @@ function defineScope(cls, targetClass, name, params, methods, options) { const targetModel = definition.targetModel(this); const self = this; - var f = function(condOrRefresh, options, cb) { + const f = function(condOrRefresh, options, cb) { if (arguments.length === 0) { if (typeof f.value === 'function') { return f.value(self); diff --git a/lib/validations.js b/lib/validations.js index fb978ca7..82184e0a 100644 --- a/lib/validations.js +++ b/lib/validations.js @@ -513,7 +513,8 @@ function getConfigurator(name, opts) { */ Validatable.prototype.isValid = function(callback, data, options) { options = options || {}; - let valid = true, inst = this, wait = 0, async = false; + let valid = true, wait = 0, async = false; + const inst = this; const validations = this.constructor.validations; const reportDiscardedProperties = this.__strict && @@ -539,8 +540,8 @@ Validatable.prototype.isValid = function(callback, data, options) { }); this.trigger('validate', function(validationsDone) { - let inst = this, - asyncFail = false; + const inst = this; + let asyncFail = false; const attrs = Object.keys(validations || {}); @@ -687,7 +688,7 @@ function skipValidation(inst, conf, kind) { return !doValidate; } -var defaultMessages = { +const defaultMessages = { presence: 'can\'t be blank', absence: 'can\'t be set', 'unknown-property': 'is not defined in the model', @@ -894,7 +895,7 @@ function ValidationError(obj) { util.inherits(ValidationError, Error); -var errorHasStackProperty = !!(new Error).stack; +const errorHasStackProperty = !!(new Error).stack; ValidationError.maxPropertyStringLength = 32; diff --git a/test/datasource.test.js b/test/datasource.test.js index fe7e090b..c7dd8ddb 100644 --- a/test/datasource.test.js +++ b/test/datasource.test.js @@ -108,7 +108,7 @@ describe('DataSource', function() { * new DataSource(connectorInstance) */ it('should accept resolved connector', function() { - var mockConnector = { + const mockConnector = { name: 'loopback-connector-mock', initialize: function(ds, cb) { ds.connector = mockConnector; @@ -125,7 +125,7 @@ describe('DataSource', function() { * new DataSource(dsName, connectorInstance) */ it('should accept dsName and resolved connector', function() { - var mockConnector = { + const mockConnector = { name: 'loopback-connector-mock', initialize: function(ds, cb) { ds.connector = mockConnector; @@ -142,7 +142,7 @@ describe('DataSource', function() { * new DataSource(connectorInstance, settings) */ it('should accept resolved connector and settings', function() { - var mockConnector = { + const mockConnector = { name: 'loopback-connector-mock', initialize: function(ds, cb) { ds.connector = mockConnector; @@ -156,7 +156,7 @@ describe('DataSource', function() { }); it('should set states correctly with eager connect', function(done) { - var mockConnector = { + const mockConnector = { name: 'loopback-connector-mock', initialize: function(ds, cb) { ds.connector = mockConnector; @@ -211,7 +211,7 @@ describe('DataSource', function() { }); it('should set states correctly with deferred connect', function(done) { - var mockConnector = { + const mockConnector = { name: 'loopback-connector-mock', initialize: function(ds, cb) { ds.connector = mockConnector; @@ -267,7 +267,7 @@ describe('DataSource', function() { }); it('should set states correctly with lazyConnect = true', function(done) { - var mockConnector = { + const mockConnector = { name: 'loopback-connector-mock', initialize: function(ds, cb) { ds.connector = mockConnector; diff --git a/test/datatype.test.js b/test/datatype.test.js index 1dbe12c1..ee069191 100644 --- a/test/datatype.test.js +++ b/test/datatype.test.js @@ -53,7 +53,8 @@ describe('datatypes', function() { }); it('should keep types when get read data from db', function(done) { - let d = new Date('2015-01-01T12:00:00'), id; + const d = new Date('2015-01-01T12:00:00'); + let id; Model.create({ str: 'hello', date: d, num: '3', bool: 1, list: ['test'], arr: [1, 'str'], @@ -101,7 +102,8 @@ describe('datatypes', function() { }); it('should respect data types when updating attributes', function(done) { - const d = new Date, id; + const d = new Date; + let id; Model.create({ str: 'hello', date: d, num: '3', bool: 1}, function(err, m) { diff --git a/test/discovery.test.js b/test/discovery.test.js index cf501762..0f569c0f 100644 --- a/test/discovery.test.js +++ b/test/discovery.test.js @@ -632,7 +632,8 @@ describe('Mock connector', function() { }); describe('Default memory connector', function() { - let ds, nonExistantError = 'Table \'NONEXISTENT\' does not exist.'; + const nonExistantError = 'Table \'NONEXISTENT\' does not exist.'; + let ds; before(function() { ds = new DataSource({connector: 'memory'}); diff --git a/test/hooks.test.js b/test/hooks.test.js index 65ffa9c4..bf4e1e0f 100644 --- a/test/hooks.test.js +++ b/test/hooks.test.js @@ -9,12 +9,12 @@ /* global getSchema:false */ const should = require('./init.js'); -let j = require('../'), +const j = require('../'), Schema = j.Schema, AbstractClass = j.AbstractClass, - Hookable = j.Hookable, + Hookable = j.Hookable; - db, User; +let db, User; describe('hooks', function() { before(function(done) { @@ -436,7 +436,8 @@ describe('hooks', function() { }); function addHooks(name, done) { - let called = false, random = String(Math.floor(Math.random() * 1000)); + const random = String(Math.floor(Math.random() * 1000)); + let called = false; User['before' + name] = function(next, data) { called = true; data.email = random; diff --git a/test/include.test.js b/test/include.test.js index aac7e472..4703bbf6 100644 --- a/test/include.test.js +++ b/test/include.test.js @@ -1493,10 +1493,10 @@ describe('include', function() { }); }); -var createdUsers = []; -var createdPassports = []; -var createdProfiles = []; -var createdPosts = []; +let createdUsers = []; +let createdPassports = []; +let createdProfiles = []; +let createdPosts = []; function setup(done) { db = getSchema(); City = db.define('City'); diff --git a/test/loopback-dl.test.js b/test/loopback-dl.test.js index 669a7dce..91f70bea 100644 --- a/test/loopback-dl.test.js +++ b/test/loopback-dl.test.js @@ -615,8 +615,7 @@ describe('DataSource define model', function() { it('updates instances with unknown properties in non-strict mode', function(done) { const ds = new DataSource('memory');// define models - let Post; - Post = ds.define('Post', { + const Post = ds.define('Post', { title: {type: String, length: 255, index: true}, content: {type: String}, }); diff --git a/test/manipulation.test.js b/test/manipulation.test.js index 164939c2..9c939c0d 100644 --- a/test/manipulation.test.js +++ b/test/manipulation.test.js @@ -137,7 +137,7 @@ describe('manipulation', function() { }); it('should not return instance of object', function(done) { - var person = Person.create(function(err, p) { + const person = Person.create(function(err, p) { if (err) return done(err); should.exist(p.id); if (person) person.should.not.be.an.instanceOf(Person); @@ -254,7 +254,7 @@ describe('manipulation', function() { {name: 'Boltay'}, {}, ]; - var res = Person.create(batch, function(e, ps) { + const res = Person.create(batch, function(e, ps) { if (res) res.should.not.be.instanceOf(Array); should.not.exist(e); should.exist(ps); diff --git a/test/memory.test.js b/test/memory.test.js index 8ff100d8..16dccc21 100644 --- a/test/memory.test.js +++ b/test/memory.test.js @@ -923,7 +923,8 @@ describe('Unoptimized connector', function() { }); describe('Memory connector with options', function() { - let ds, savedOptions = {}, Post; + const savedOptions = {}; + let ds, Post; before(function() { ds = new DataSource({connector: 'memory'}); diff --git a/test/persistence-hooks.suite.js b/test/persistence-hooks.suite.js index 398e2a95..8f1c0081 100644 --- a/test/persistence-hooks.suite.js +++ b/test/persistence-hooks.suite.js @@ -143,8 +143,7 @@ module.exports = function(dataSource, should, connectorCapabilities) { it('triggers the loaded hook multiple times when multiple instances exist when near filter is used', function(done) { - let hookMonitorGeoModel; - hookMonitorGeoModel = new HookMonitor({includeModelName: false}); + const hookMonitorGeoModel = new HookMonitor({includeModelName: false}); function monitorHookExecutionGeoModel(hookNames) { hookMonitorGeoModel.install(GeoModel, hookNames); diff --git a/test/relations.test.js b/test/relations.test.js index 5bd3b5f2..1d5b0970 100644 --- a/test/relations.test.js +++ b/test/relations.test.js @@ -2784,7 +2784,8 @@ describe('relations', function() { scope4.should.have.property('include', 'imageable'); }); - let article, employee, pictures = []; + let article, employee; + const pictures = []; it('should create polymorphic relation - article', function(done) { Article.create({name: 'Article 1'}, function(err, a) { if (err) return done(err); diff --git a/test/schema.test.js b/test/schema.test.js index 3b58df9a..eefe04e4 100644 --- a/test/schema.test.js +++ b/test/schema.test.js @@ -9,7 +9,9 @@ /* global getSchema:false */ const should = require('./init.js'); -let db = getSchema(), slave = getSchema(), Model, SlaveModel; +const db = getSchema(); +const slave = getSchema(); +let Model, SlaveModel; describe('dataSource', function() { it('should define Model', function() { diff --git a/test/scope.test.js b/test/scope.test.js index abe06ed0..b73d47a8 100644 --- a/test/scope.test.js +++ b/test/scope.test.js @@ -284,13 +284,13 @@ describe('scope - filtered count, updateAll and destroyAll', function() { verify(); }); - var verify = function() { + function verify() { Station.flagged.count(function(err, count) { if (err) return done(err); count.should.equal(2); done(); }); - }; + } }); it('should allow filtered updateAll', function(done) { @@ -300,13 +300,13 @@ describe('scope - filtered count, updateAll and destroyAll', function() { verify(); }); - var verify = function() { + function verify() { Station.flagged.count(function(err, count) { if (err) return done(err); count.should.equal(2); done(); }); - }; + } }); it('should allow filtered destroyAll', function(done) { @@ -315,7 +315,7 @@ describe('scope - filtered count, updateAll and destroyAll', function() { verify(); }); - var verify = function() { + function verify() { Station.ordered.count(function(err, count) { if (err) return done(err); count.should.equal(2); @@ -325,7 +325,7 @@ describe('scope - filtered count, updateAll and destroyAll', function() { done(); }); }); - }; + } }); }); diff --git a/test/validations.test.js b/test/validations.test.js index a092b8ac..c336b4c0 100644 --- a/test/validations.test.js +++ b/test/validations.test.js @@ -10,7 +10,8 @@ const should = require('./init.js'); const async = require('async'); -let j = require('../'), db, User; +const j = require('../'); +let db, User; const ValidationError = j.ValidationError; function getValidAttributes() { @@ -1521,7 +1522,7 @@ describe('validations', function() { }); }); -var empData = [{ +const empData = [{ id: 1, name: 'Foo', age: 1,