Skip to content

Commit 9df2066

Browse files
tykus160avelad
authored andcommitted
chore: Forbid using instanceof on arrays (#7648)
Roughly related to #6279. Whenever possible, we should limit usage of `instanceof` operator for the sake of `documentPip` mode.
1 parent edea6a2 commit 9df2066

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*! @license
2+
* Shaka Player
3+
* Copyright 2016 Google LLC
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
module.exports = {
8+
meta: {
9+
type: 'suggestion',
10+
docs: {
11+
description: 'Usage of Array.isArray() instead of instanceof Array',
12+
category: 'Best Practices',
13+
recommended: false,
14+
},
15+
fixable: 'code',
16+
schema: [],
17+
},
18+
create: (ctx) => ({
19+
BinaryExpression: (node) => {
20+
if (node.operator === 'instanceof' &&
21+
node.right.type === 'Identifier' && node.right.name === 'Array') {
22+
ctx.report({
23+
node,
24+
message: 'Do not use instanceof Array',
25+
fix: (fixer) => {
26+
const source = ctx.sourceCode;
27+
const text = source.getText();
28+
const leftSide = text.slice(node.left.start, node.left.end);
29+
return fixer.replaceText(node, `Array.isArray(${leftSide})`);
30+
},
31+
});
32+
}
33+
},
34+
}),
35+
};

build/eslint-plugin-shaka-rules/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module.exports = {
1616

1717
const RULES = [
1818
'arg-comment-spacing',
19+
'array-no-instanceof',
1920
'private',
2021
];
2122
for (const rule of RULES) {

test/test/util/manifest_parser_util.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ shaka.test.ManifestParser = class {
5959
partialReferences = [], tilesLayout = '', syncTime = null) {
6060
const getUris = () => {
6161
const uris = [];
62-
if (uri instanceof Array) {
62+
if (Array.isArray(uri)) {
6363
for (const url of uri) {
6464
if (url.length) {
6565
uris.push(baseUri + url);

test/test/util/ttml_utils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ shaka.test.TtmlUtils = class {
1717
cue.region = jasmine.objectContaining(cue.region);
1818
}
1919

20-
if (cue.nestedCues && (cue.nestedCues instanceof Array)) {
20+
if (Array.isArray(cue.nestedCues)) {
2121
cue.nestedCues = cue.nestedCues.map(mapExpected);
2222
}
2323

0 commit comments

Comments
 (0)