Skip to content

Commit d576ea5

Browse files
committed
cleanup #58
1 parent 5ca65c4 commit d576ea5

File tree

5 files changed

+11
-8
lines changed

5 files changed

+11
-8
lines changed

docs/rules/no-array-instanceof.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
# Require `Array.isArray()` check instead of `instanceof Array`
1+
# Require `Array.isArray()` instead of `instanceof Array`
2+
3+
The `instanceof Array` check doesn't work across realms/contexts, for example, frames/windows in browsers or the `vm` module in Node.js.
24

3-
While the `instanceof Array` works, it is unreliable across environments Eg: multi-frame DOM environments (iframes). Hence, `Array.isArray()` is recommended.
45

56
## Fail
67

@@ -9,6 +10,7 @@ array instanceof Array;
910
[1,2,3] instanceof Array;
1011
```
1112

13+
1214
## Pass
1315

1416
```js

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ module.exports = {
1919
'unicorn/no-abusive-eslint-disable': 'error',
2020
'unicorn/no-process-exit': 'error',
2121
'unicorn/throw-new-error': 'error',
22-
'unicorn/no-array-instanceof': 'error',
23-
'unicorn/number-literal-case': 'error'
22+
'unicorn/number-literal-case': 'error',
23+
'unicorn/no-array-instanceof': 'error'
2424
}
2525
}
2626
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Configure it in `package.json`.
5656
- [no-process-exit](docs/rules/no-process-exit.md) - Disallow `process.exit()`.
5757
- [throw-new-error](docs/rules/throw-new-error.md) - Require `new` when throwing an error. *(fixable)*
5858
- [number-literal-case](docs/rules/number-literal-case.md) - Enforce lowercase identifier and uppercase value for number literals. *(fixable)*
59-
- [no-array-instanceof](docs/rules/no-array-instanceof.md) - Disallow `instanceof Array`, instead use `Array.isArray()`. *(fixable)*
59+
- [no-array-instanceof](docs/rules/no-array-instanceof.md) - Require `Array.isArray()` instead of `instanceof Array`. *(fixable)*
6060

6161

6262
## Recommended config

rules/no-array-instanceof.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
const create = context => ({
44
BinaryExpression: node => {
55
if (node.operator === 'instanceof' && node.right.type === 'Identifier' && node.right.name === 'Array') {
6-
// Get the source code and extract the left part.
6+
// get the source code and extract the left part
77
const arraySourceCode = context.getSourceCode().text.slice(node.left.range[0], node.left.range[1]);
8+
89
context.report({
910
node,
10-
message: 'Use `Array.isArray()` instead of `instanceof Array`',
11+
message: 'Use `Array.isArray()` instead of `instanceof Array`.',
1112
fix: fixer => fixer.replaceText(node, `Array.isArray(${arraySourceCode})`)
1213
});
1314
}

test/no-array-instanceof.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const ruleTester = avaRuleTester(test, {
1010

1111
const errors = [{
1212
ruleId: 'no-array-instanceof',
13-
message: 'Use `Array.isArray()` instead of `instanceof Array`'
13+
message: 'Use `Array.isArray()` instead of `instanceof Array`.'
1414
}];
1515

1616
ruleTester.run('avoid-array-instanceof', rule, {

0 commit comments

Comments
 (0)