Skip to content

Commit df0b505

Browse files
authored
Merge pull request #88 from nlfurniss/fix-anonymous-once
Fix no-anonymous-once not recognizing context function
2 parents cf0e27b + 4efdda2 commit df0b505

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed

lib/rules/no-anonymous-once.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
const { getCaller, cleanCaller, isCallingWithApply, isCallingWithCall } = require('../utils/caller');
77
const { collectObjectPatternBindings } = require('../utils/destructed-binding');
88
const { getEmberImportBinding } = require('../utils/imports');
9+
const { get } = require('../utils/get');
910
const MESSAGE
1011
= `The uniqueness once offers is based on function uniqueness, each invocation of this line will always create a new
1112
function instance, resulting in uniqueness checks, that will never be hit. Please replace with a named function.
@@ -49,9 +50,14 @@ function normalizeArguments(caller, args) {
4950

5051
return mut;
5152
}
52-
53+
/**
54+
* Determines whether a function is anonymous based on whether it was a name
55+
* or if it is a method on the current context.
56+
* @param {ASTNode} fn
57+
* @return {Boolean}
58+
*/
5359
function isAnonymousFunction(fn) {
54-
return fn && !fn.name;
60+
return !(get(fn, 'name') || get(fn, 'object.type') === 'ThisExpression');
5561
}
5662

5763
function isString(node) {

tests/lib/rules/no-anonymous-once.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,76 @@ ruleTester.run('no-anonymous-once', rule, {
4444
run.once.call(this, noop);
4545
}
4646
});`
47+
},
48+
{
49+
code: `
50+
export default Ember.Component({
51+
cb: () => {
52+
// do stuff
53+
},
54+
55+
perform() {
56+
Ember.run.once(this, 'cb');
57+
}
58+
});`
59+
},
60+
{
61+
code: `
62+
export default Ember.Component({
63+
cb: () => {
64+
// do stuff
65+
},
66+
67+
perform() {
68+
Ember.run.once(this, this.cb);
69+
}
70+
});`
71+
},
72+
{
73+
code: `
74+
function cb() {
75+
//do stuff
76+
}
77+
export default Ember.Component({
78+
perform() {
79+
Ember.run.once(this, cb);
80+
}
81+
});`
82+
},
83+
{
84+
code: `
85+
export default Ember.Component({
86+
cb: () => {
87+
// do stuff
88+
},
89+
90+
perform() {
91+
Ember.run.scheduleOnce('afterRender', this, 'cb');
92+
}
93+
});`
94+
},
95+
{
96+
code: `
97+
export default Ember.Component({
98+
cb: () => {
99+
// do stuff
100+
},
101+
102+
perform() {
103+
Ember.run.scheduleOnce('afterRender', this, this.cb);
104+
}
105+
});`
106+
},
107+
{
108+
code: `
109+
function cb() {
110+
//do stuff
111+
}
112+
export default Ember.Component({
113+
perform() {
114+
Ember.run.scheduleOnce('afterRender', this, cb);
115+
}
116+
});`
47117
}
48118
],
49119
invalid: [

0 commit comments

Comments
 (0)