Skip to content

Commit ded2b84

Browse files
committed
Require consistent return values.
This enables the eslint rule that requires all functions to consistently either return a value or not return a value. Change-Id: I98b579f3689c3b6c74968116824231bb792bd9dd
1 parent b9ea01e commit ded2b84

File tree

19 files changed

+81
-64
lines changed

19 files changed

+81
-64
lines changed

.eslintrc.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ module.exports = {
3939
// Things we should probably fix, but in stages in multiple commits: {{{
4040

4141
// These could catch real bugs
42-
"consistent-return": "off",
4342
"default-case": "off",
44-
"no-extra-bind": "off",
43+
// TODO: Enable no-loop-func in next eslint release. We can't use it
44+
// now since it doesn't allow capturing "const" variables, which is safe
4545
"no-loop-func": "off",
4646
"no-unused-expressions": "off", // Conflicts with some Closure declarations
4747
"prefer-promise-reject-errors": "off",
@@ -82,6 +82,7 @@ module.exports = {
8282
// "Best practices" rules we should be able to pass, but are not part of "eslint:recommended": {{{
8383
"accessor-pairs": "error",
8484
"array-callback-return": "error",
85+
"consistent-return": "error",
8586
"no-alert": "error",
8687
"no-caller": "error",
8788
"no-catch-shadow": "error",

demo/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,7 @@ class ShakaDemoMain {
733733
return objOn[split[0]];
734734
}
735735
}
736+
return undefined;
736737
}
737738

738739
/**

lib/cast/cast_proxy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ shaka.cast.CastProxy.prototype.cast = function() {
199199
return this.sender_.cast(initState).then(() => {
200200
if (!this.localPlayer_) {
201201
// We've already been destroyed.
202-
return;
202+
return null;
203203
}
204204

205205
// Unload the local manifest when casting succeeds.

lib/dash/dash_parser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ shaka.dash.DashParser.prototype.requestManifest_ = function() {
417417
return operation.promise.then((response) => {
418418
// Detect calls to stop().
419419
if (!this.playerInterface_) {
420-
return;
420+
return null;
421421
}
422422

423423
// For redirections add the response uri to the first entry in the

lib/hls/hls_parser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ shaka.hls.HlsParser.prototype.stop = function() {
279279
*/
280280
shaka.hls.HlsParser.prototype.update = function() {
281281
if (!this.isLive_()) {
282-
return;
282+
return Promise.resolve();
283283
}
284284

285285
/** @type {!Array.<!Promise>} */

lib/hls/manifest_text_parser.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ shaka.hls.ManifestTextParser.prototype.parseSegments_ =
153153
}
154154
} else if (shaka.hls.Utils.isComment(line)) {
155155
// Skip comments.
156-
return [];
157156
} else {
158157
const verbatimSegmentUri = line.trim();
159158
const absoluteSegmentUri = shaka.hls.Utils.constructAbsoluteUri(

lib/media/drm_engine.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ shaka.media.DrmEngine.prototype.attach = function(video) {
491491
const setServerCertificate = this.setServerCertificate();
492492

493493
return Promise.all([setMediaKeys, setServerCertificate]).then(() => {
494-
if (this.isDestroying_) { return Promise.reject(); }
494+
if (this.isDestroying_) { return; }
495495

496496
this.createOrLoad();
497497
if (!this.currentDrmInfo_.initData.length &&
@@ -504,7 +504,7 @@ shaka.media.DrmEngine.prototype.attach = function(video) {
504504
}
505505
}).catch((error) => {
506506
if (this.isDestroying_) { return; }
507-
return Promise.reject(error);
507+
throw error;
508508
});
509509
};
510510

@@ -530,11 +530,11 @@ shaka.media.DrmEngine.prototype.setServerCertificate = async function() {
530530
' system. The server certificate has been ignored.');
531531
}
532532
} catch (exception) {
533-
return Promise.reject(new shaka.util.Error(
533+
throw new shaka.util.Error(
534534
shaka.util.Error.Severity.CRITICAL,
535535
shaka.util.Error.Category.DRM,
536536
shaka.util.Error.Code.INVALID_SERVER_CERTIFICATE,
537-
exception.message));
537+
exception.message);
538538
}
539539
}
540540
};
@@ -873,21 +873,21 @@ shaka.media.DrmEngine.prototype.queryMediaKeys_ = function(configsByKeySystem) {
873873
if (hasLicenseServer != shouldHaveLicenseServer) return;
874874

875875
p = p.catch(() => {
876-
if (this.isDestroying_) { return; }
876+
if (this.isDestroying_) { return null; }
877877
return navigator.requestMediaKeySystemAccess(keySystem, [config]);
878878
});
879879
});
880880
});
881881

882882
p = p.catch(() => {
883-
return Promise.reject(new shaka.util.Error(
883+
throw new shaka.util.Error(
884884
shaka.util.Error.Severity.CRITICAL,
885885
shaka.util.Error.Category.DRM,
886-
shaka.util.Error.Code.REQUESTED_KEY_SYSTEM_CONFIG_UNAVAILABLE));
886+
shaka.util.Error.Code.REQUESTED_KEY_SYSTEM_CONFIG_UNAVAILABLE);
887887
});
888888

889889
p = p.then((mediaKeySystemAccess) => {
890-
if (this.isDestroying_) { return Promise.reject(); }
890+
if (this.isDestroying_) { return null; }
891891

892892
// Get the set of supported content types from the audio and video
893893
// capabilities. Avoid duplicates so that it is easier to read what is
@@ -915,16 +915,16 @@ shaka.media.DrmEngine.prototype.queryMediaKeys_ = function(configsByKeySystem) {
915915
configsByKeySystem.get(mediaKeySystemAccess.keySystem));
916916

917917
if (!this.currentDrmInfo_.licenseServerUri) {
918-
return Promise.reject(new shaka.util.Error(
918+
throw new shaka.util.Error(
919919
shaka.util.Error.Severity.CRITICAL,
920920
shaka.util.Error.Category.DRM,
921921
shaka.util.Error.Code.NO_LICENSE_SERVER_GIVEN,
922-
this.currentDrmInfo_.keySystem));
922+
this.currentDrmInfo_.keySystem);
923923
}
924924

925925
return mediaKeySystemAccess.createMediaKeys();
926926
}).then((mediaKeys) => {
927-
if (this.isDestroying_) { return Promise.reject(); }
927+
if (this.isDestroying_) { return; }
928928
shaka.log.info('Created MediaKeys object for key system',
929929
this.currentDrmInfo_.keySystem);
930930

@@ -937,15 +937,15 @@ shaka.media.DrmEngine.prototype.queryMediaKeys_ = function(configsByKeySystem) {
937937
this.currentDrmInfo_ = null;
938938
this.supportedTypes_.clear();
939939
if (exception instanceof shaka.util.Error) {
940-
return Promise.reject(exception);
940+
throw exception;
941941
}
942942

943943
// We failed to create MediaKeys. This generally shouldn't happen.
944-
return Promise.reject(new shaka.util.Error(
944+
throw new shaka.util.Error(
945945
shaka.util.Error.Severity.CRITICAL,
946946
shaka.util.Error.Category.DRM,
947947
shaka.util.Error.Code.FAILED_TO_CREATE_CDM,
948-
exception.message));
948+
exception.message);
949949
});
950950

951951
instigator.reject();
@@ -1041,7 +1041,7 @@ shaka.media.DrmEngine.prototype.loadOfflineSession_ = function(sessionId) {
10411041
this.activeSessions_.set(session, metadata);
10421042

10431043
return session.load(sessionId).then((present) => {
1044-
if (this.isDestroying_) { return Promise.reject(); }
1044+
if (this.isDestroying_) { return null; }
10451045
shaka.log.v2('Loaded offline session', sessionId, present);
10461046

10471047
if (!present) {
@@ -1051,7 +1051,7 @@ shaka.media.DrmEngine.prototype.loadOfflineSession_ = function(sessionId) {
10511051
shaka.util.Error.Severity.CRITICAL,
10521052
shaka.util.Error.Category.DRM,
10531053
shaka.util.Error.Code.OFFLINE_SESSION_REMOVED));
1054-
return;
1054+
return null;
10551055
}
10561056

10571057
// TODO: We should get a key status change event. Remove once Chrome CDM

lib/media/streaming_engine.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ shaka.media.StreamingEngine.prototype.start = async function() {
434434
this.manifest_.periods[needPeriodIndex]);
435435
if (!initialStreams.variant && !initialStreams.text) {
436436
shaka.log.error('init: no Streams chosen');
437-
return new shaka.util.Error(
437+
throw new shaka.util.Error(
438438
shaka.util.Error.Severity.CRITICAL,
439439
shaka.util.Error.Category.STREAMING,
440440
shaka.util.Error.Code.INVALID_STREAMS_CHOSEN);
@@ -1082,7 +1082,7 @@ shaka.media.StreamingEngine.prototype.setupPeriod_ = function(periodIndex) {
10821082

10831083
// Serialize Period set up.
10841084
this.setupPeriodPromise_ = this.setupPeriodPromise_.then(() => {
1085-
if (this.destroyed_) return;
1085+
if (this.destroyed_) return null;
10861086
return this.setupStreams_(streams);
10871087
}).then(() => {
10881088
if (this.destroyed_) return;
@@ -1644,7 +1644,7 @@ shaka.media.StreamingEngine.prototype.fetchAndAppend_ = function(
16441644

16451645

16461646
Promise.all([initSourceBuffer, fetchSegment]).then((results) => {
1647-
if (this.destroyed_ || this.fatalError_) return;
1647+
if (this.destroyed_ || this.fatalError_) return null;
16481648
return this.append_(mediaState,
16491649
presentationTime,
16501650
currentPeriod,
@@ -1848,7 +1848,7 @@ shaka.media.StreamingEngine.prototype.initSourceBuffer_ = function(
18481848
const fetchInit =
18491849
this.fetch_(mediaState, mediaState.stream.initSegmentReference);
18501850
const appendInit = fetchInit.then((initSegment) => {
1851-
if (this.destroyed_) return;
1851+
if (this.destroyed_) return null;
18521852
shaka.log.v1(logPrefix, 'appending init segment');
18531853
const hasClosedCaptions = mediaState.stream.closedCaptions &&
18541854
mediaState.stream.closedCaptions.size > 0;
@@ -1892,7 +1892,7 @@ shaka.media.StreamingEngine.prototype.append_ = function(
18921892
}
18931893

18941894
return this.evict_(mediaState, presentationTime).then(() => {
1895-
if (this.destroyed_) return;
1895+
if (this.destroyed_) return null;
18961896
shaka.log.v1(logPrefix, 'appending media segment');
18971897

18981898
// MediaSourceEngine expects times relative to the start of the
@@ -1909,8 +1909,6 @@ shaka.media.StreamingEngine.prototype.append_ = function(
19091909
// We must use |stream| because switch() may have been called.
19101910
mediaState.lastStream = stream;
19111911
mediaState.lastSegmentReference = reference;
1912-
1913-
return Promise.resolve();
19141912
});
19151913
};
19161914

@@ -2410,6 +2408,8 @@ shaka.media.StreamingEngine.prototype.clearBuffer_ =
24102408
if (!this.destroyed_ && flush) {
24112409
return this.playerInterface_.mediaSourceEngine.flush(
24122410
mediaState.type);
2411+
} else {
2412+
return null;
24132413
}
24142414
});
24152415
}

lib/net/backoff.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,10 @@ shaka.net.Backoff.prototype.attempt = async function() {
105105
if (this.autoReset_) {
106106
this.reset_();
107107
} else {
108-
return Promise.reject();
108+
throw new shaka.util.Error(
109+
shaka.util.Error.Severity.CRITICAL,
110+
shaka.util.Error.Category.PLAYER,
111+
shaka.util.Error.Code.ATTEMPTS_EXHAUSTED);
109112
}
110113
}
111114

lib/net/networking_engine.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -525,16 +525,19 @@ shaka.net.NetworkingEngine.prototype.send_ = function(
525525

526526
return responseAndGotProgress;
527527
}, (error) => {
528-
if (error && error.code == shaka.util.Error.Code.OPERATION_ABORTED) {
529-
// Don't change anything if the operation was aborted.
530-
throw error;
531-
}
532-
533528
if (this.destroyed_) {
534529
return shaka.util.AbortableOperation.aborted();
535530
}
536531

537-
if (error && error.severity == shaka.util.Error.Severity.RECOVERABLE) {
532+
if (error.code == shaka.util.Error.Code.OPERATION_ABORTED) {
533+
// Don't change anything if the operation was aborted.
534+
throw error;
535+
} else if (error.code == shaka.util.Error.Code.ATTEMPTS_EXHAUSTED) {
536+
goog.asserts.assert(lastError, 'Should have last error');
537+
throw lastError;
538+
}
539+
540+
if (error.severity == shaka.util.Error.Severity.RECOVERABLE) {
538541
// Don't pass in a non-shaka error, even if one is somehow thrown;
539542
// instead, call the listener with a null error.
540543
const errorOrNull = error instanceof shaka.util.Error ? error : null;
@@ -548,8 +551,7 @@ shaka.net.NetworkingEngine.prototype.send_ = function(
548551
}
549552

550553
// The error was not recoverable, so do not try again.
551-
// Rethrow the error so the Promise chain stays rejected.
552-
throw error || lastError;
554+
throw error;
553555
});
554556

555557
return sendOperation;

0 commit comments

Comments
 (0)