/
no-extra-bind.js
102 lines (96 loc) · 3.63 KB
/
no-extra-bind.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* @fileoverview Tests for no-extra-bind rule
* @author Bence Dányi <bence@danyi.me>
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const rule = require("../../../lib/rules/no-extra-bind"),
{ RuleTester } = require("../../../lib/rule-tester");
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
const ruleTester = new RuleTester();
const errors = [{ messageId: "unexpected", type: "CallExpression" }];
ruleTester.run("no-extra-bind", rule, {
valid: [
"var a = function(b) { return b }.bind(c, d)",
{ code: "var a = function(b) { return b }.bind(...c)", parserOptions: { ecmaVersion: 6 } },
"var a = function() { this.b }()",
"var a = function() { this.b }.foo()",
"var a = f.bind(a)",
"var a = function() { return this.b }.bind(c)",
{ code: "var a = (() => { return b }).bind(c, d)", parserOptions: { ecmaVersion: 6 } },
"(function() { (function() { this.b }.bind(this)) }.bind(c))",
"var a = function() { return 1; }[bind](b)",
{ code: "var a = function() { return 1; }[`bi${n}d`](b)", parserOptions: { ecmaVersion: 6 } },
{ code: "var a = function() { return () => this; }.bind(b)", parserOptions: { ecmaVersion: 6 } }
],
invalid: [
{
code: "var a = function() { return 1; }.bind(b)",
output: "var a = function() { return 1; }",
errors
},
{
code: "var a = function() { return 1; }['bind'](b)",
output: "var a = function() { return 1; }",
errors
},
{
code: "var a = function() { return 1; }[`bind`](b)",
output: "var a = function() { return 1; }",
parserOptions: { ecmaVersion: 6 },
errors
},
{
code: "var a = (() => { return 1; }).bind(b)",
output: "var a = (() => { return 1; })",
parserOptions: { ecmaVersion: 6 },
errors
},
{
code: "var a = (() => { return this; }).bind(b)",
output: "var a = (() => { return this; })",
parserOptions: { ecmaVersion: 6 },
errors
},
{
code: "var a = function() { (function(){ this.c }) }.bind(b)",
output: "var a = function() { (function(){ this.c }) }",
errors
},
{
code: "var a = function() { function c(){ this.d } }.bind(b)",
output: "var a = function() { function c(){ this.d } }",
errors
},
{
code: "var a = function() { return 1; }.bind(this)",
output: "var a = function() { return 1; }",
errors
},
{
code: "var a = function() { (function(){ (function(){ this.d }.bind(c)) }) }.bind(b)",
output: "var a = function() { (function(){ (function(){ this.d }.bind(c)) }) }",
errors: [{ messageId: "unexpected", type: "CallExpression", column: 71 }]
},
// Should not autofix if bind expression args have side effects
{
code: "var a = function() {}.bind(b++)",
output: null,
errors
},
{
code: "var a = function() {}.bind(b())",
output: null,
errors
},
{
code: "var a = function() {}.bind(b.c)",
output: null,
errors
}
]
});