Skip to content

Commit

Permalink
Enable additional ES6 linter rules.
Browse files Browse the repository at this point in the history
Change-Id: I6861541b27153ba034364a5972a9b086de581cef
  • Loading branch information
TheModMaker committed Jun 11, 2019
1 parent d438e03 commit 3da8090
Show file tree
Hide file tree
Showing 37 changed files with 223 additions and 92 deletions.
82 changes: 67 additions & 15 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,31 @@ const testNameRegex =
'/^([fx]?it|(drm|quarantined)It|(before|after)(Each|All))$/';
const testCall = `CallExpression[callee.name=${testNameRegex}]`;

const commonNoRestrictedSyntax = [
{
'selector':
'MemberExpression[object.name="goog"][property.name="inherits"]',
'message': 'Don\'t use goog.inherits.',
},
{
'selector': ':not(MethodDefinition) > FunctionExpression',
'message': 'Use arrow functions instead of "function" functions.',
},
{
'selector': 'CallExpression[callee.property.name="forEach"] >' +
':function[params.length=1]',
'message': 'Use for-of instead of forEach',
},
{
'selector': 'CallExpression[callee.property.name=/^(bind|call|apply)$/]',
'message': 'Don\'t use Function bind/call/apply.',
},
{
'selector': 'MemberExpression[property.name="prototype"]',
'message': 'Use ES6 classes not .prototype.',
},
];

module.exports = {
'env': {
'browser': true,
Expand Down Expand Up @@ -147,21 +172,7 @@ module.exports = {
],
'no-restricted-syntax': [
'error',
{
'selector': 'CallExpression[callee.name="beforeAll"] ' +
':matches(' +
'CallExpression[callee.property.name="createSpy"],' +
'CallExpression[callee.name="spyOn"])',
'message': 'Create spies in beforeEach, not beforeAll.',
},
{
'selector': testCall + ' > :function[async=true][params.length>0]',
'message': 'Don\'t use both async and done.',
},
{
'selector': testCall + ' > CallExpression',
'message': 'Use filterDescribe instead of checkAndRun calls',
},
...commonNoRestrictedSyntax,
],
'no-whitespace-before-property': 'error',
'nonblock-statement-body-position': ['error', 'below'],
Expand All @@ -176,6 +187,47 @@ module.exports = {
// }}}
},
'overrides': [
{
'rules': {
'no-restricted-syntax': [
'error',
{
'selector': 'CallExpression[callee.name="beforeAll"] ' +
':matches(' +
'CallExpression[callee.property.name="createSpy"],' +
'CallExpression[callee.name="spyOn"])',
'message': 'Create spies in beforeEach, not beforeAll.',
},
{
'selector': testCall + ' > :function[params.length>0]',
'message': 'Use async instead of "done" in tests.',
},
{
'selector': testCall + ' > CallExpression',
'message': 'Use filterDescribe instead of checkAndRun calls',
},
{
'selector': 'CatchClause',
'message': 'Use expect.toFail or expectAsync.toBeRejected',
},
...commonNoRestrictedSyntax,
],
},
'files': [
'test/**/*.js',
],
},
{
'rules': {
'no-restricted-syntax': 'off',
},
'files': [
'demo/load.js',
'externs/**/*.js',
'test/test/externs/*.js',
'ui/externs/*.js',
],
},
{
'rules': {
'no-var': 'off',
Expand Down
4 changes: 2 additions & 2 deletions build/generateLocalizations.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def AsQuotedString(input_string):
def GenerateLocalizations(localizations, class_name):
"""Generates JavaScript code to insert the localization data.
This creates a function called "apply" in the class called |class_name| that,
This creates a function called "addTo" in the class called |class_name| that,
when called, will insert the data from |localizations|.
Args:
Expand Down Expand Up @@ -166,7 +166,7 @@ def GenerateLocalizations(localizations, class_name):
* @param {!shaka.ui.Localization} localization
*/""")

doc.Code('%s.apply = function(localization) {' % class_name)
doc.Code('%s.addTo = function(localization) {' % class_name)

message_ids = set()

Expand Down
1 change: 1 addition & 0 deletions demo/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,7 @@ class ShakaDemoMain {
// Make an alias for "this" so that it can be captured inside the
// non-arrow function below.
const self = this;
// eslint-disable-next-line no-restricted-syntax
const textDisplayer = function() {
if (self.nativeControlsEnabled_) {
return new shaka.text.SimpleTextDisplayer(self.video_);
Expand Down
2 changes: 2 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ function allUsableBrowserLaunchers(config) {
// no DEFAULT_CMD. Some launchers have DEFAULT_CMD, but not for this
// platform. Finally, WebDriver has DEFAULT_CMD, but still requires
// configuration, so we simply blacklist it by name.
// eslint-disable-next-line no-restricted-syntax
const DEFAULT_CMD = pluginConstructor.prototype.DEFAULT_CMD;
if (!DEFAULT_CMD || !DEFAULT_CMD[process.platform]) {
continue;
Expand All @@ -359,6 +360,7 @@ function allUsableBrowserLaunchers(config) {
// Now that we've filtered out the browsers that can't be launched without
// custom config or that can't be launched on this platform, we filter out
// the browsers you don't have installed.
// eslint-disable-next-line no-restricted-syntax
const ENV_CMD = pluginConstructor.prototype.ENV_CMD;
const browserPath = process.env[ENV_CMD] || DEFAULT_CMD[process.platform];

Expand Down
3 changes: 3 additions & 0 deletions lib/cast/cast_proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,7 @@ shaka.cast.CastProxy = class extends shaka.util.FakeEventTarget {
if (!this.sender_ || !this.sender_.isCasting()) {
let value = this.localVideo_[name];
if (typeof value == 'function') {
// eslint-disable-next-line no-restricted-syntax
value = value.bind(this.localVideo_);
}
return value;
Expand Down Expand Up @@ -678,6 +679,7 @@ shaka.cast.CastProxy = class extends shaka.util.FakeEventTarget {
const value = /** @type {Object} */(this.localPlayer_)[name];
goog.asserts.assert(typeof value == 'function',
'only methods on Player');
// eslint-disable-next-line no-restricted-syntax
return value.bind(this.localPlayer_);
}
}
Expand All @@ -687,6 +689,7 @@ shaka.cast.CastProxy = class extends shaka.util.FakeEventTarget {
const value = /** @type {Object} */(this.localPlayer_)[name];
goog.asserts.assert(typeof value == 'function',
'only methods on Player');
// eslint-disable-next-line no-restricted-syntax
return value.bind(this.localPlayer_);
}

Expand Down
2 changes: 2 additions & 0 deletions lib/cast/cast_receiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,7 @@ shaka.cast.CastReceiver = class extends shaka.util.FakeEventTarget {
const methodName = message['methodName'];
const args = message['args'];
const target = this.targets_[targetName];
// eslint-disable-next-line no-restricted-syntax
target[methodName].apply(target, args);
break;
}
Expand All @@ -576,6 +577,7 @@ shaka.cast.CastReceiver = class extends shaka.util.FakeEventTarget {
const id = message['id'];
const senderId = event.senderId;
const target = this.targets_[targetName];
// eslint-disable-next-line no-restricted-syntax
let p = target[methodName].apply(target, args);
if (targetName == 'player' && methodName == 'load') {
// Wait until the manifest has actually loaded to send another media
Expand Down
1 change: 1 addition & 0 deletions lib/media/drm_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -1486,6 +1486,7 @@ shaka.media.DrmEngine = class {
!!window.navigator &&
!!window.navigator.requestMediaKeySystemAccess &&
!!window.MediaKeySystemAccess &&
// eslint-disable-next-line no-restricted-syntax
!!window.MediaKeySystemAccess.prototype.getConfiguration;

return basic;
Expand Down
46 changes: 23 additions & 23 deletions lib/media/presentation_timeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,32 +476,32 @@ shaka.media.PresentationTimeline = class {
return Math.max(
0, now - this.maxSegmentDuration_ - this.presentationStartTime_);
}
};

if (goog.DEBUG) {
/**
* Debug only: assert that the timeline parameters make sense for the type
* of presentation (VOD, IPR, live).
*/
shaka.media.PresentationTimeline.prototype.assertIsValid = function() {
if (this.isLive()) {
// Implied by isLive(): infinite and dynamic.
// Live streams should have a start time.
goog.asserts.assert(this.presentationStartTime_ != null,
'Detected as live stream, but does not match our model of live!');
} else if (this.isInProgress()) {
// Implied by isInProgress(): finite and dynamic.
// IPR streams should have a start time, and segments should not expire.
goog.asserts.assert(this.presentationStartTime_ != null &&
this.segmentAvailabilityDuration_ == Infinity,
'Detected as IPR stream, but does not match our model of IPR!');
} else { // VOD
// VOD segments should not expire and the presentation should be finite
// and static.
goog.asserts.assert(this.segmentAvailabilityDuration_ == Infinity &&
this.duration_ != Infinity &&
this.static_,
'Detected as VOD stream, but does not match our model of VOD!');
assertIsValid() {
if (goog.DEBUG) {
if (this.isLive()) {
// Implied by isLive(): infinite and dynamic.
// Live streams should have a start time.
goog.asserts.assert(this.presentationStartTime_ != null,
'Detected as live stream, but does not match our model of live!');
} else if (this.isInProgress()) {
// Implied by isInProgress(): finite and dynamic.
// IPR streams should have a start time, and segments should not expire.
goog.asserts.assert(this.presentationStartTime_ != null &&
this.segmentAvailabilityDuration_ == Infinity,
'Detected as IPR stream, but does not match our model of IPR!');
} else { // VOD
// VOD segments should not expire and the presentation should be finite
// and static.
goog.asserts.assert(this.segmentAvailabilityDuration_ == Infinity &&
this.duration_ != Infinity &&
this.static_,
'Detected as VOD stream, but does not match our model of VOD!');
}
}
};
}
}
};
2 changes: 1 addition & 1 deletion lib/offline/storage_muxer.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ shaka.offline.StorageMuxer = class {
* @param {function(!shaka.extern.EmeSessionStorageCell)} callback
*/
forEachEmeSessionCell(callback) {
this.mechanisms_.forEach((mechanism) => {
this.mechanisms_.forEach((mechanism, name) => {
callback(mechanism.getEmeSessionCell());
});
}
Expand Down
2 changes: 2 additions & 0 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,7 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
static isBrowserSupported() {
// Basic features needed for the library to be usable.
const basicSupport = !!window.Promise && !!window.Uint8Array &&
// eslint-disable-next-line no-restricted-syntax
!!Array.prototype.forEach;
if (!basicSupport) {
return false;
Expand Down Expand Up @@ -3513,6 +3514,7 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
// at the time we call the factory. Bind can't be used here because we
// call the factory with "new", effectively removing any binding to "this".
const self = this;
// eslint-disable-next-line no-restricted-syntax
config.textDisplayFactory = function() {
return new shaka.text.SimpleTextDisplayer(self.video_);
};
Expand Down
6 changes: 4 additions & 2 deletions lib/polyfill/fullscreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ shaka.polyfill.Fullscreen = class {
return;
}

// eslint-disable-next-line no-restricted-syntax
let proto = Element.prototype;
proto.requestFullscreen = proto.requestFullscreen ||
proto.mozRequestFullScreen ||
proto.msRequestFullscreen ||
proto.webkitRequestFullscreen;

// eslint-disable-next-line no-restricted-syntax
proto = Document.prototype;
proto.exitFullscreen = proto.exitFullscreen ||
proto.mozCancelFullScreen ||
Expand All @@ -50,14 +52,14 @@ shaka.polyfill.Fullscreen = class {

if (!('fullscreenElement' in document)) {
Object.defineProperty(document, 'fullscreenElement', {
get: function() {
get: () => {
return document.mozFullScreenElement ||
document.msFullscreenElement ||
document.webkitFullscreenElement;
},
});
Object.defineProperty(document, 'fullscreenEnabled', {
get: function() {
get: () => {
return document.mozFullScreenEnabled ||
document.msFullscreenEnabled ||
document.webkitSupportsFullscreen ||
Expand Down
6 changes: 6 additions & 0 deletions lib/polyfill/input_event.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,22 @@ shaka.polyfill.InputEvent = class {
// So we can't compare addEventListener with the polyfill directly.
// Instead, store the original in a globally accessible place and check if
// that has been used yet.
// eslint-disable-next-line no-restricted-syntax
if (HTMLInputElement.prototype['originalAddEventListener']) {
// The polyfill was already installed.
return;
}

shaka.log.info('Patching input event support on IE.');

// eslint-disable-next-line no-restricted-syntax
HTMLInputElement.prototype['originalAddEventListener'] =
// eslint-disable-next-line no-restricted-syntax
HTMLInputElement.prototype.addEventListener;

// eslint-disable-next-line no-restricted-syntax
HTMLInputElement.prototype['addEventListener'] =
// eslint-disable-next-line no-restricted-syntax
shaka.polyfill.InputEvent.addEventListener_;
}

Expand All @@ -80,6 +85,7 @@ shaka.polyfill.InputEvent = class {
}
}

// eslint-disable-next-line no-restricted-syntax
HTMLInputElement.prototype['originalAddEventListener'].call(
this, type, listener, options);
}
Expand Down
11 changes: 11 additions & 0 deletions lib/polyfill/mediasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,14 @@ shaka.polyfill.MediaSource = class {
* @private
*/
static stubAbort_() {
/* eslint-disable no-restricted-syntax */
const addSourceBuffer = MediaSource.prototype.addSourceBuffer;
MediaSource.prototype.addSourceBuffer = function(...varArgs) {
const sourceBuffer = addSourceBuffer.apply(this, varArgs);
sourceBuffer.abort = function() {}; // Stub out for buggy implementations.
return sourceBuffer;
};
/* eslint-enable no-restricted-syntax */
}

/**
Expand All @@ -139,9 +141,12 @@ shaka.polyfill.MediaSource = class {
* @private
*/
static patchRemovalRange_() {
// eslint-disable-next-line no-restricted-syntax
const originalRemove = SourceBuffer.prototype.remove;

// eslint-disable-next-line no-restricted-syntax
SourceBuffer.prototype.remove = function(startTime, endTime) {
// eslint-disable-next-line no-restricted-syntax
return originalRemove.call(this, startTime, endTime - 0.001);
};
}
Expand All @@ -154,7 +159,9 @@ shaka.polyfill.MediaSource = class {
* @private
*/
static patchEndOfStreamEvents_() {
// eslint-disable-next-line no-restricted-syntax
const endOfStream = MediaSource.prototype.endOfStream;
// eslint-disable-next-line no-restricted-syntax
MediaSource.prototype.endOfStream = function(...varArgs) {
// This bug manifests only when endOfStream() results in the truncation
// of the MediaSource's duration. So first we must calculate what the
Expand All @@ -175,14 +182,18 @@ shaka.polyfill.MediaSource = class {
}
}

// eslint-disable-next-line no-restricted-syntax
return endOfStream.apply(this, varArgs);
};

let cleanUpHandlerInstalled = false;
// eslint-disable-next-line no-restricted-syntax
const addSourceBuffer = MediaSource.prototype.addSourceBuffer;
// eslint-disable-next-line no-restricted-syntax
MediaSource.prototype.addSourceBuffer = function(...varArgs) {
// After adding a new source buffer, add an event listener to allow us to
// suppress events.
// eslint-disable-next-line no-restricted-syntax
const sourceBuffer = addSourceBuffer.apply(this, varArgs);
sourceBuffer['mediaSource_'] = this;
sourceBuffer.addEventListener('updateend',
Expand Down
Loading

0 comments on commit 3da8090

Please sign in to comment.