-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
no-invalid-debug-function-arguments.js
62 lines (51 loc) · 1.64 KB
/
no-invalid-debug-function-arguments.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
'use strict';
const utils = require('../utils/utils');
const emberUtils = require('../utils/ember');
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
function getErrorMessage(debugFunction) {
return `Usage of Ember's \`${debugFunction}\` function has its arguments passed in the wrong order. \`String description\` should come before \`Boolean condition\`.`;
}
const DEBUG_FUNCTIONS = ['assert', 'deprecate', 'warn'];
module.exports = {
meta: {
docs: {
description: "Catch usages of Ember's `assert()` / `warn()` / `deprecate()` functions that have the arguments passed in the wrong order.",
category: 'Possible Errors',
recommended: false
},
fixable: null
},
getErrorMessage,
DEBUG_FUNCTIONS,
create(context) {
return {
CallExpression(node) {
if (isDebugFunctionWithReversedArgs(node)) {
context.report({
node,
message: getErrorMessage(getDebugFunction(node))
});
}
}
};
}
};
function isDebugFunctionWithReversedArgs(node) {
return (
isDebugFunction(node) &&
node.arguments.length >= 2 &&
!isString(node.arguments[0]) &&
isString(node.arguments[1])
);
}
function isDebugFunction(node) {
return getDebugFunction(node) !== undefined;
}
function getDebugFunction(node) {
return DEBUG_FUNCTIONS.find(debugFunction => emberUtils.isModule(node, debugFunction));
}
function isString(node) {
return utils.isTemplateLiteral(node) || (utils.isLiteral(node) && typeof node.value === 'string');
}