Skip to content

Commit

Permalink
refactor(ts): enable noImplicitThis (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith committed Nov 16, 2018
1 parent c9b841a commit f67acd6
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 105 deletions.
139 changes: 81 additions & 58 deletions src/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,15 @@ entity.KEY_SYMBOL = Symbol('KEY');
* const datastore = new Datastore();
* const aDouble = datastore.double(7.3);
*/
function Double(value) {
/**
* @name Double#value
* @type {number}
*/
this.value = value;
class Double {
value: number;
constructor(value: number) {
/**
* @name Double#value
* @type {number}
*/
this.value = value;
}
}

entity.Double = Double;
Expand Down Expand Up @@ -85,12 +88,15 @@ entity.isDsDouble = isDsDouble;
* const datastore = new Datastore();
* const anInt = datastore.int(7);
*/
function Int(value) {
/**
* @name Int#value
* @type {string}
*/
this.value = value.toString();
class Int {
value: string;
constructor(value: number|string) {
/**
* @name Int#value
* @type {string}
*/
this.value = value.toString();
}
}

entity.Int = Int;
Expand All @@ -108,6 +114,11 @@ function isDsInt(value) {

entity.isDsInt = isDsInt;

export interface Coordinates {
latitude: number;
longitude: number;
}

/**
* Build a Datastore Geo Point object.
*
Expand All @@ -126,16 +137,19 @@ entity.isDsInt = isDsInt;
*
* const geoPoint = datastore.geoPoint(coordinates);
*/
function GeoPoint(coordinates) {
/**
* Coordinate value.
*
* @name GeoPoint#coordinates
* @type {object}
* @property {number} latitude Latitudinal value.
* @property {number} longitude Longitudinal value.
*/
this.value = coordinates;
class GeoPoint {
value: Coordinates;
constructor(coordinates: Coordinates) {
/**
* Coordinate value.
*
* @name GeoPoint#coordinates
* @type {object}
* @property {number} latitude Latitudinal value.
* @property {number} longitude Longitudinal value.
*/
this.value = coordinates;
}
}

entity.GeoPoint = GeoPoint;
Expand Down Expand Up @@ -169,46 +183,55 @@ entity.isDsGeoPoint = isDsGeoPoint;
* path: ['Company', 123]
* });
*/
function Key(options) {
/**
* @name Key#namespace
* @type {string}
*/
this.namespace = options.namespace;

options.path = [].slice.call(options.path);

if (options.path.length % 2 === 0) {
const identifier = options.path.pop();

if (is.number(identifier) || isDsInt(identifier)) {
this.id = identifier.value || identifier;
} else if (is.string(identifier)) {
this.name = identifier;
class Key {

namespace: string;
id?: string;
name?: string;
kind: string;
parent?: Key;

constructor(options) {
/**
* @name Key#namespace
* @type {string}
*/
this.namespace = options.namespace;

options.path = [].slice.call(options.path);

if (options.path.length % 2 === 0) {
const identifier = options.path.pop();

if (is.number(identifier) || isDsInt(identifier)) {
this.id = identifier.value || identifier;
} else if (is.string(identifier)) {
this.name = identifier;
}
}
}

this.kind = options.path.pop();
this.kind = options.path.pop();

if (options.path.length > 0) {
this.parent = new Key(options);
}
if (options.path.length > 0) {
this.parent = new Key(options);
}

// `path` is computed on demand to consider any changes that may have been
// made to the key.
/**
* @name Key#path
* @type {array}
*/
Object.defineProperty(this, 'path', {
enumerable: true,
get: function() {
return arrify(this.parent && this.parent.path).concat([
this.kind,
this.name || this.id,
]);
},
});
// `path` is computed on demand to consider any changes that may have been
// made to the key.
/**
* @name Key#path
* @type {array}
*/
Object.defineProperty(this, 'path', {
enumerable: true,
get: function() {
return arrify(this.parent && this.parent.path).concat([
this.kind,
this.name || this.id,
]);
},
});
}
}

entity.Key = Key;
Expand Down
14 changes: 10 additions & 4 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,18 @@ const fakeGoogleGax = {
},
};

function FakeQuery() {
this.calledWith_ = arguments;
class FakeQuery {
calledWith_: IArguments;
constructor() {
this.calledWith_ = arguments;
}
}

function FakeTransaction() {
this.calledWith_ = arguments;
class FakeTransaction {
calledWith_: IArguments;
constructor() {
this.calledWith_ = arguments;
}
}

function FakeV1() {}
Expand Down
71 changes: 30 additions & 41 deletions test/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ const fakeV1 = {
},
};

function FakeQuery() {
this.calledWith_ = arguments;
}
class FakeQuery extends Query {}

let pjyOverride;

Expand All @@ -59,7 +57,7 @@ describe('Request', function() {
let request;

let key;
let sandbox;
const sandbox = sinon.createSandbox();

before(function() {
Request = proxyquire('../src/request', {
Expand All @@ -71,25 +69,21 @@ describe('Request', function() {
}).DatastoreRequest;
});

after(function() {
after(() => {
v1FakeClientOverride = null;
});

beforeEach(function() {
sandbox = sinon.createSandbox();
pjyOverride = null;
key = new entity.Key({
namespace: 'namespace',
path: ['Company', 123],
});
FakeQuery.prototype = new Query();
v1FakeClientOverride = null;
request = new Request();
});

afterEach(() => {
sandbox.restore();
});
afterEach(() => sandbox.restore());

describe('instantiation', function() {
it('should promisify all the things', function() {
Expand Down Expand Up @@ -474,7 +468,7 @@ describe('Request', function() {
request
.createReadStream(key)
.on('error', done)
.on('data', function(entity) {
.on('data', entity => {
assert.deepStrictEqual(entity, expectedResult);
})
.on('end', done)
Expand All @@ -484,19 +478,18 @@ describe('Request', function() {
it('should not push more results if stream was ended', function(done) {
let entitiesEmitted = 0;

request.request_ = function(config, callback) {
setImmediate(function() {
request.request_ = (config, callback) => {
setImmediate(() => {
callback(null, apiResponseWithMultiEntities);
});
};

request
.createReadStream([key, key])
.on('data', function() {
const stream = request.createReadStream([key, key]);
stream.on('data', () => {
entitiesEmitted++;
this.end();
stream.end();
})
.on('end', function() {
.on('end', () => {
assert.strictEqual(entitiesEmitted, 1);
done();
})
Expand All @@ -513,13 +506,11 @@ describe('Request', function() {
});
};

request
.createReadStream(key)
const stream = request.createReadStream(key);
stream
.on('error', done)
.on('data', function() {
this.end();
})
.on('end', function() {
.on('data', () => stream.end())
.on('end', () => {
assert.strictEqual(lookupCount, 1);
done();
})
Expand Down Expand Up @@ -930,16 +921,16 @@ describe('Request', function() {
);
startCalled = true;
return this;
};
}

FakeQuery.prototype.offset = function(offset_) {
sandbox.stub(FakeQuery.prototype, 'offset').callsFake(offset_ => {
const offset = query.offsetVal - apiResponse.batch.skippedResults;
assert.strictEqual(offset_, offset);
offsetCalled = true;
return this;
};
});

FakeQuery.prototype.limit = function(limit_) {
sandbox.stub(FakeQuery.prototype, 'limit').callsFake(limit_ => {
if (timesRequestCalled === 1) {
assert.strictEqual(
limit_,
Expand All @@ -950,7 +941,7 @@ describe('Request', function() {
assert.strictEqual(limit_, query.limitVal);
}
return this;
};
});

sandbox.stub(entity, 'queryToQueryProto').callsFake(query_ => {
if (timesRequestCalled > 1) {
Expand Down Expand Up @@ -1012,10 +1003,10 @@ describe('Request', function() {

sandbox.stub(entity, 'queryToQueryProto').returns({});

FakeQuery.prototype.limit = function() {
sandbox.stub(FakeQuery.prototype, 'limit').callsFake(() => {
limitCalled = true;
return this;
};
});

request
.runQueryStream(query)
Expand Down Expand Up @@ -1050,13 +1041,13 @@ describe('Request', function() {
}
};

request
const stream = request
.runQueryStream({})
.on('data', function() {
.on('data', () => {
entitiesEmitted++;
this.end();
stream.end();
})
.on('end', function() {
.on('end', () => {
assert.strictEqual(entitiesEmitted, 1);
done();
});
Expand All @@ -1070,13 +1061,11 @@ describe('Request', function() {
callback(null, apiResponse);
};

request
.runQueryStream({})
const stream = request.runQueryStream({});
stream
.on('error', done)
.on('data', function() {
this.end();
})
.on('end', function() {
.on('data', () => stream.end())
.on('end', () => {
assert.strictEqual(timesRequestCalled, 1);
done();
});
Expand Down
3 changes: 1 addition & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"compilerOptions": {
"rootDir": ".",
"outDir": "build",
"noImplicitAny": false,
"noImplicitThis": false
"noImplicitAny": false
},
"include": [
"src/*.ts",
Expand Down

0 comments on commit f67acd6

Please sign in to comment.