From 8c8a78f39abb1f51e22e3adc753f5941b984c52f Mon Sep 17 00:00:00 2001 From: Justin Beckwith Date: Thu, 20 Sep 2018 12:29:44 -0700 Subject: [PATCH] Enable prefer-const in the eslint config (#181) --- .eslintrc.yml | 1 + smoke-test/speech_smoke_test.js | 3 +- src/helpers.js | 18 ++--- src/index.js | 4 +- src/v1/speech_client.js | 2 +- src/v1p1beta1/speech_client.js | 2 +- system-test/speech_smoke_test.js | 18 ++--- system-test/speech_smoke_test_v1p1beta1.js | 18 ++--- test/gapic-v1.test.js | 80 +++++++++++----------- test/helpers.test.js | 56 +++++++-------- 10 files changed, 102 insertions(+), 100 deletions(-) diff --git a/.eslintrc.yml b/.eslintrc.yml index 65f1dce6..73eeec27 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -12,3 +12,4 @@ rules: eqeqeq: error no-warning-comments: warn no-var: error + prefer-const: error diff --git a/smoke-test/speech_smoke_test.js b/smoke-test/speech_smoke_test.js index 1cbcef2d..756f84f1 100644 --- a/smoke-test/speech_smoke_test.js +++ b/smoke-test/speech_smoke_test.js @@ -38,7 +38,8 @@ describe('SpeechSmokeTest', () => { config: config, audio: audio, }; - client.recognize(request) + client + .recognize(request) .then(responses => { const response = responses[0]; console.log(response); diff --git a/src/helpers.js b/src/helpers.js index 94b12c5f..5dc37e5d 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -16,17 +16,17 @@ 'use strict'; -let common = require('@google-cloud/common'); -let pumpify = require('pumpify'); -let streamEvents = require('stream-events'); -let through = require('through2'); +const common = require('@google-cloud/common'); +const pumpify = require('pumpify'); +const streamEvents = require('stream-events'); +const through = require('through2'); /*! * Return a dictionary-like object with helpers to augment the Speech * GAPIC. */ module.exports = () => { - let methods = {}; + const methods = {}; /** * Performs bidirectional streaming speech recognition: receive results while @@ -69,9 +69,9 @@ module.exports = () => { } // Format the audio content as input request for pipeline - let recognizeStream = streamEvents(pumpify.obj()); + const recognizeStream = streamEvents(pumpify.obj()); - let requestStream = this._innerApiCalls + const requestStream = this._innerApiCalls .streamingRecognize(options) .on('error', err => { recognizeStream.destroy(err); @@ -87,7 +87,7 @@ module.exports = () => { // config) is delayed until we get the first burst of data. recognizeStream.once('writing', () => { // The first message should contain the streaming config. - let firstMessage = true; + const firstMessage = true; // Set up appropriate piping between the stream returned by // the underlying API method and the one that we return. @@ -96,7 +96,7 @@ module.exports = () => { // This entails that the user sends raw audio; it is wrapped in // the appropriate request structure. through.obj((obj, _, next) => { - let payload = {}; + const payload = {}; if (firstMessage && config !== undefined) { // Write the initial configuration to the stream. payload.streamingConfig = config; diff --git a/src/index.js b/src/index.js index 12a2d57b..77c89531 100644 --- a/src/index.js +++ b/src/index.js @@ -48,8 +48,8 @@ const gapic = Object.freeze({ }); // Augment the SpeechClient objects with the helpers. -for (let gapicVersion of Object.keys(gapic)) { - let clientProto = gapic[gapicVersion].SpeechClient.prototype; +for (const gapicVersion of Object.keys(gapic)) { + const clientProto = gapic[gapicVersion].SpeechClient.prototype; Object.assign(clientProto, helpers()); } diff --git a/src/v1/speech_client.js b/src/v1/speech_client.js index 0e0709ed..bab27c65 100644 --- a/src/v1/speech_client.js +++ b/src/v1/speech_client.js @@ -166,7 +166,7 @@ class SpeechClient { 'longRunningRecognize', 'streamingRecognize', ]; - for (let methodName of speechStubMethods) { + for (const methodName of speechStubMethods) { this._innerApiCalls[methodName] = gax.createApiCall( speechStub.then( stub => diff --git a/src/v1p1beta1/speech_client.js b/src/v1p1beta1/speech_client.js index da842e87..10e307e1 100644 --- a/src/v1p1beta1/speech_client.js +++ b/src/v1p1beta1/speech_client.js @@ -166,7 +166,7 @@ class SpeechClient { 'longRunningRecognize', 'streamingRecognize', ]; - for (let methodName of speechStubMethods) { + for (const methodName of speechStubMethods) { this._innerApiCalls[methodName] = gax.createApiCall( speechStub.then( stub => diff --git a/system-test/speech_smoke_test.js b/system-test/speech_smoke_test.js index 5cc849d9..4d90054d 100644 --- a/system-test/speech_smoke_test.js +++ b/system-test/speech_smoke_test.js @@ -18,30 +18,30 @@ describe('SpeechSmokeTest', () => { it('successfully makes a call to the service', done => { const speech = require('../src'); - let client = new speech.v1.SpeechClient({ + const client = new speech.v1.SpeechClient({ // optional auth parameters. }); - let languageCode = 'en-US'; - let sampleRateHertz = 44100; - let encoding = 'FLAC'; - let config = { + const languageCode = 'en-US'; + const sampleRateHertz = 44100; + const encoding = 'FLAC'; + const config = { languageCode: languageCode, sampleRateHertz: sampleRateHertz, encoding: encoding, }; - let uri = 'gs://gapic-toolkit/hello.flac'; - let audio = { + const uri = 'gs://gapic-toolkit/hello.flac'; + const audio = { uri: uri, }; - let request = { + const request = { config: config, audio: audio, }; client .recognize(request) .then(responses => { - let response = responses[0]; + const response = responses[0]; console.log(response); }) .then(done) diff --git a/system-test/speech_smoke_test_v1p1beta1.js b/system-test/speech_smoke_test_v1p1beta1.js index cad4d5a9..9e37155d 100644 --- a/system-test/speech_smoke_test_v1p1beta1.js +++ b/system-test/speech_smoke_test_v1p1beta1.js @@ -18,30 +18,30 @@ describe('SpeechSmokeTest v1p1beta1', () => { it('successfully makes a call to the service', done => { const speech = require('../src'); - let client = new speech.v1p1beta1.SpeechClient({ + const client = new speech.v1p1beta1.SpeechClient({ // optional auth parameters. }); - let languageCode = 'en-US'; - let sampleRateHertz = 44100; - let encoding = 'FLAC'; - let config = { + const languageCode = 'en-US'; + const sampleRateHertz = 44100; + const encoding = 'FLAC'; + const config = { languageCode: languageCode, sampleRateHertz: sampleRateHertz, encoding: encoding, }; - let uri = 'gs://gapic-toolkit/hello.flac'; - let audio = { + const uri = 'gs://gapic-toolkit/hello.flac'; + const audio = { uri: uri, }; - let request = { + const request = { config: config, audio: audio, }; client .recognize(request) .then(responses => { - let response = responses[0]; + const response = responses[0]; console.log(response); }) .then(done) diff --git a/test/gapic-v1.test.js b/test/gapic-v1.test.js index 957c2780..dce086a3 100644 --- a/test/gapic-v1.test.js +++ b/test/gapic-v1.test.js @@ -18,38 +18,38 @@ const assert = require('assert'); const speechModule = require('../src'); -let FAKE_STATUS_CODE = 1; -let error = new Error(); +const FAKE_STATUS_CODE = 1; +const error = new Error(); error.code = FAKE_STATUS_CODE; describe('SpeechClient', () => { describe('recognize', () => { it('invokes recognize without error', done => { - let client = new speechModule.v1.SpeechClient({ + const client = new speechModule.v1.SpeechClient({ credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); // Mock request - let encoding = 'FLAC'; - let sampleRateHertz = 44100; - let languageCode = 'en-US'; - let config = { + const encoding = 'FLAC'; + const sampleRateHertz = 44100; + const languageCode = 'en-US'; + const config = { encoding: encoding, sampleRateHertz: sampleRateHertz, languageCode: languageCode, }; - let uri = 'gs://bucket_name/file_name.flac'; - let audio = { + const uri = 'gs://bucket_name/file_name.flac'; + const audio = { uri: uri, }; - let request = { + const request = { config: config, audio: audio, }; // Mock response - let expectedResponse = {}; + const expectedResponse = {}; // Mock Grpc layer client._innerApiCalls.recognize = mockSimpleGrpcMethod( @@ -65,25 +65,25 @@ describe('SpeechClient', () => { }); it('invokes recognize with error', done => { - let client = new speechModule.v1.SpeechClient({ + const client = new speechModule.v1.SpeechClient({ credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); // Mock request - let encoding = 'FLAC'; - let sampleRateHertz = 44100; - let languageCode = 'en-US'; - let config = { + const encoding = 'FLAC'; + const sampleRateHertz = 44100; + const languageCode = 'en-US'; + const config = { encoding: encoding, sampleRateHertz: sampleRateHertz, languageCode: languageCode, }; - let uri = 'gs://bucket_name/file_name.flac'; - let audio = { + const uri = 'gs://bucket_name/file_name.flac'; + const audio = { uri: uri, }; - let request = { + const request = { config: config, audio: audio, }; @@ -106,31 +106,31 @@ describe('SpeechClient', () => { describe('longRunningRecognize', function() { it('invokes longRunningRecognize without error', done => { - let client = new speechModule.v1.SpeechClient({ + const client = new speechModule.v1.SpeechClient({ credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); // Mock request - let encoding = 'FLAC'; - let sampleRateHertz = 44100; - let languageCode = 'en-US'; - let config = { + const encoding = 'FLAC'; + const sampleRateHertz = 44100; + const languageCode = 'en-US'; + const config = { encoding: encoding, sampleRateHertz: sampleRateHertz, languageCode: languageCode, }; - let uri = 'gs://bucket_name/file_name.flac'; - let audio = { + const uri = 'gs://bucket_name/file_name.flac'; + const audio = { uri: uri, }; - let request = { + const request = { config: config, audio: audio, }; // Mock response - let expectedResponse = {}; + const expectedResponse = {}; // Mock Grpc layer client._innerApiCalls.longRunningRecognize = mockLongRunningGrpcMethod( @@ -141,7 +141,7 @@ describe('SpeechClient', () => { client .longRunningRecognize(request) .then(responses => { - let operation = responses[0]; + const operation = responses[0]; return operation.promise(); }) .then(responses => { @@ -154,25 +154,25 @@ describe('SpeechClient', () => { }); it('invokes longRunningRecognize with error', done => { - let client = new speechModule.v1.SpeechClient({ + const client = new speechModule.v1.SpeechClient({ credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); // Mock request - let encoding = 'FLAC'; - let sampleRateHertz = 44100; - let languageCode = 'en-US'; - let config = { + const encoding = 'FLAC'; + const sampleRateHertz = 44100; + const languageCode = 'en-US'; + const config = { encoding: encoding, sampleRateHertz: sampleRateHertz, languageCode: languageCode, }; - let uri = 'gs://bucket_name/file_name.flac'; - let audio = { + const uri = 'gs://bucket_name/file_name.flac'; + const audio = { uri: uri, }; - let request = { + const request = { config: config, audio: audio, }; @@ -187,7 +187,7 @@ describe('SpeechClient', () => { client .longRunningRecognize(request) .then(responses => { - let operation = responses[0]; + const operation = responses[0]; return operation.promise(); }) .then(() => { @@ -201,7 +201,7 @@ describe('SpeechClient', () => { }); it('has longrunning decoder functions', () => { - let client = new speechModule.v1.SpeechClient({ + const client = new speechModule.v1.SpeechClient({ credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); @@ -233,7 +233,7 @@ function mockSimpleGrpcMethod(expectedRequest, response, error) { function mockLongRunningGrpcMethod(expectedRequest, response, error) { return request => { assert.deepStrictEqual(request, expectedRequest); - let mockOperation = { + const mockOperation = { promise: function() { return new Promise((resolve, reject) => { if (error) { diff --git a/test/helpers.test.js b/test/helpers.test.js index 5f2f9280..5531c3e6 100644 --- a/test/helpers.test.js +++ b/test/helpers.test.js @@ -16,17 +16,17 @@ 'use strict'; -let assert = require('assert'); -let Buffer = require('safe-buffer').Buffer; -let common = require('@google-cloud/common'); -let proxyquire = require('proxyquire'); -let sinon = require('sinon'); -let stream = require('stream'); +const assert = require('assert'); +const Buffer = require('safe-buffer').Buffer; +const common = require('@google-cloud/common'); +const proxyquire = require('proxyquire'); +const sinon = require('sinon'); +const stream = require('stream'); describe('Speech helper methods', () => { let client; let FakeApiErrorOverride; - let sandbox = sinon.createSandbox(); + const sandbox = sinon.createSandbox(); let speech; class FakeApiError extends common.util.ApiError { @@ -60,22 +60,22 @@ describe('Speech helper methods', () => { }); describe('streamingRecognize', () => { - let CONFIG = { + const CONFIG = { config: {encoding: 'LINEAR16', languageCode: 'en-us', sampleRate: 16000}, }; - let OPTIONS = {timeout: Infinity}; + const OPTIONS = {timeout: Infinity}; it('writes the config to the resulting stream', done => { // Stub the underlying _streamingRecognize method to just return // a bogus stream. - let requestStream = new stream.PassThrough({objectMode: true}); - let sr = sandbox + const requestStream = new stream.PassThrough({objectMode: true}); + const sr = sandbox .stub(client._innerApiCalls, 'streamingRecognize') .returns(requestStream); // Call the new helper method and establish that the config was // forwarded as expected. - let userStream = client.streamingRecognize(CONFIG, OPTIONS); + const userStream = client.streamingRecognize(CONFIG, OPTIONS); // Establish that the underlying streamingRecognize was called with // the options. @@ -96,12 +96,12 @@ describe('Speech helper methods', () => { it('does not require options', () => { // Stub the underlying _streamingRecognize method to just return // a bogus stream. - let requestStream = new stream.PassThrough({objectMode: true}); - let sr = sandbox + const requestStream = new stream.PassThrough({objectMode: true}); + const sr = sandbox .stub(client._innerApiCalls, 'streamingRecognize') .returns(requestStream); - let userStream = client.streamingRecognize(CONFIG); + const userStream = client.streamingRecognize(CONFIG); userStream.emit('writing'); @@ -112,14 +112,14 @@ describe('Speech helper methods', () => { it('destroys the user stream when the request stream errors', done => { // Stub the underlying _streamingRecognize method to just return // a bogus stream. - let requestStream = new stream.PassThrough({objectMode: true}); + const requestStream = new stream.PassThrough({objectMode: true}); sandbox .stub(client._innerApiCalls, 'streamingRecognize') .returns(requestStream); - let userStream = client.streamingRecognize(CONFIG, OPTIONS); + const userStream = client.streamingRecognize(CONFIG, OPTIONS); - let error = new Error('Request stream error'); + const error = new Error('Request stream error'); userStream.once('error', err => { assert.strictEqual(err, error); @@ -132,15 +132,15 @@ describe('Speech helper methods', () => { it('destroys the user stream when the response contains an error', done => { // Stub the underlying _streamingRecognize method to just return // a bogus stream. - let requestStream = new stream.PassThrough({objectMode: true}); + const requestStream = new stream.PassThrough({objectMode: true}); sandbox .stub(client._innerApiCalls, 'streamingRecognize') .returns(requestStream); - let userStream = client.streamingRecognize(CONFIG, OPTIONS); + const userStream = client.streamingRecognize(CONFIG, OPTIONS); - let error = {}; - let fakeApiError = {}; + const error = {}; + const fakeApiError = {}; FakeApiErrorOverride = err => { assert.strictEqual(err, error); @@ -159,14 +159,14 @@ describe('Speech helper methods', () => { it('re-emits response from the request stream', done => { // Stub the underlying _streamingRecognize method to just return // a bogus stream. - let requestStream = new stream.PassThrough({objectMode: true}); + const requestStream = new stream.PassThrough({objectMode: true}); sandbox .stub(client._innerApiCalls, 'streamingRecognize') .returns(requestStream); - let userStream = client.streamingRecognize(CONFIG, OPTIONS); + const userStream = client.streamingRecognize(CONFIG, OPTIONS); - let response = {}; + const response = {}; userStream.on('response', _response => { assert.strictEqual(_response, response); @@ -180,13 +180,13 @@ describe('Speech helper methods', () => { it('wraps incoming audio data', done => { // Stub the underlying _streamingRecognize method to just return // a bogus stream. - let requestStream = new stream.PassThrough({objectMode: true}); + const requestStream = new stream.PassThrough({objectMode: true}); sandbox .stub(client._innerApiCalls, 'streamingRecognize') .returns(requestStream); - let userStream = client.streamingRecognize(CONFIG, OPTIONS); - let audioContent = Buffer.from('audio content'); + const userStream = client.streamingRecognize(CONFIG, OPTIONS); + const audioContent = Buffer.from('audio content'); requestStream._write = (data, enc, next) => { assert.deepStrictEqual(data, {