-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlog-args.js
100 lines (87 loc) · 2.61 KB
/
log-args.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
/**
* @fileoverview Enforce correct log arguments
* @author Adam Davies
*/
'use strict';
//------------------------------------------------------------------------------
// Constants
//------------------------------------------------------------------------------
const moduleUtil = require('../util/modules');
const objectUtil = require('../util/objects');
const LOG_MEMBERS = ['debug', 'audit', 'error', 'emergency'];
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
type: 'suggestion',
messages: {
titleRequired: "A log title is required with 'Log.{{ prop }}'",
detailsRequired: "Log details are required with 'Log.{{ prop }}'",
},
schema: [
{
type: 'object',
properties: {
requireTitle: {
type: 'boolean',
},
requireDetails: {
type: 'boolean',
},
},
additionalProperties: false,
},
],
},
create: function (context) {
let logModule;
return {
'CallExpression[callee.name=define]': function (node) {
logModule = moduleUtil.getModuleNodePair(node, 'N/log');
},
'CallExpression[callee.object.type=Identifier]': function (node) {
let config = context.options[0] || { requireTitle: true, requireDetails: true };
const args = node.arguments;
if (args.length === 0 || (!config.requireTitle && !config.requireDetails)) {
return;
}
const callee = node.callee;
const logVar = logModule ? logModule.variable.name : 'log';
if (
callee.object.name !== logVar ||
!LOG_MEMBERS.includes(callee.property.name)
) {
return;
}
if (
config.requireTitle &&
args[0].type === 'ObjectExpression' &&
!objectUtil.getPropByKey(args[0], 'title')
) {
context.report({
node,
messageId: 'titleRequired',
data: {
prop: callee.property.name,
},
});
}
if (
config.requireDetails &&
((args[0].type !== 'ObjectExpression' && !args[1]) ||
(args[0].type === 'ObjectExpression' &&
!objectUtil.getPropByKey(args[0], 'details')))
) {
context.report({
node,
messageId: 'detailsRequired',
data: {
prop: callee.property.name,
},
});
}
},
};
},
};