forked from ember-cli/eslint-plugin-ember
-
Notifications
You must be signed in to change notification settings - Fork 0
/
no-runloop.js
135 lines (122 loc) · 4.12 KB
/
no-runloop.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
'use strict';
const { getImportIdentifier } = require('../utils/import');
const { isIdentifier, isMemberExpression } = require('../utils/types');
//------------------------------------------------------------------------------
// General rule - Don’t use runloop functions
//------------------------------------------------------------------------------
/**
* Map of runloop functions to ember-lifeline recommended replacements
*/
const RUNLOOP_TO_LIFELINE_MAP = Object.freeze({
later: 'runTask',
next: 'runTask',
debounce: 'debounceTask',
schedule: 'scheduleTask',
throttle: 'throttleTask',
});
const ERROR_MESSAGE =
"Don't use @ember/runloop functions. Use ember-lifeline, ember-concurrency, or @ember/destroyable instead.";
// https://api.emberjs.com/ember/3.24/classes/@ember%2Frunloop
const EMBER_RUNLOOP_FUNCTIONS = [
'begin',
'bind',
'cancel',
'debounce',
'end',
'join',
'later',
'next',
'once',
'run',
'schedule',
'scheduleOnce',
'throttle',
];
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'disallow usage of `@ember/runloop` functions',
category: 'Miscellaneous',
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/no-runloop.md',
},
fixable: null,
schema: [
{
type: 'array',
uniqueItems: true,
items: { type: 'string', enum: EMBER_RUNLOOP_FUNCTIONS },
},
],
messages: {
main: ERROR_MESSAGE,
lifelineReplacement: `${ERROR_MESSAGE} For this case, you can replace \`{{actualMethodUsed}}\` with \`{{lifelineEquivalent}}\` from ember-lifeline.`,
},
},
create(context) {
// List of allowed runloop functions
const allowList = context.options[0] ?? [];
// Maps local names to imported names
const localMap = {};
/**
* Reports a node with usage of a disallowed runloop function
* @param {Node} node
* @param {String} [runloopFn] the name of the runloop function that is not allowed
* @param {String} [localName] the locally used name of the runloop function
*/
const report = function (node, runloopFn, localName) {
if (Object.keys(RUNLOOP_TO_LIFELINE_MAP).includes(runloopFn)) {
// If there is a recommended lifeline replacement, include the suggestion
context.report({
node,
messageId: 'lifelineReplacement',
data: {
actualMethodUsed: localName,
lifelineEquivalent: RUNLOOP_TO_LIFELINE_MAP[runloopFn],
},
});
} else {
// Otherwise, show a generic error message
context.report({ node, messageId: 'main' });
}
};
return {
ImportDeclaration(node) {
if (node.source.value === '@ember/runloop') {
for (const fn of EMBER_RUNLOOP_FUNCTIONS) {
const importIdentifier = getImportIdentifier(node, '@ember/runloop', fn);
localMap[importIdentifier] = fn;
}
}
},
CallExpression(node) {
// Examples: run(...), later(...)
if (isIdentifier(node.callee)) {
const name = node.callee.name;
const runloopFn = localMap[name];
const isNotAllowed = runloopFn && !allowList.includes(runloopFn);
if (isNotAllowed) {
report(node, runloopFn, name);
}
}
// runloop functions (aside from run itself) can chain onto `run`, so we need to check for this
// Examples: run.later(...), run.schedule(...)
if (isMemberExpression(node.callee) && isIdentifier(node.callee.object)) {
const objectName = node.callee.object.name;
const objectRunloopFn = localMap[objectName];
if (objectRunloopFn === 'run' && isIdentifier(node.callee.property)) {
const runloopFn = node.callee.property.name;
if (
EMBER_RUNLOOP_FUNCTIONS.includes(runloopFn) &&
runloopFn !== 'run' &&
!allowList.includes(runloopFn)
) {
report(node, runloopFn, `${objectName}.${runloopFn}`);
}
}
}
},
};
},
};