@mimik/eslint-plugin-logger 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +42 -0
- package/index.js +27 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -31,9 +31,25 @@ Validates that logger calls (on default-imported modules) use the correct argume
|
|
|
31
31
|
|
|
32
32
|
### Valid Signatures
|
|
33
33
|
|
|
34
|
+
All three access patterns are supported:
|
|
35
|
+
|
|
34
36
|
```js
|
|
35
37
|
import logger from '@mimik/sumologic-winston-logger';
|
|
36
38
|
|
|
39
|
+
// Dot notation
|
|
40
|
+
logger.info('User logged in', correlationId);
|
|
41
|
+
|
|
42
|
+
// Bracket notation with string literal
|
|
43
|
+
logger['info']('User logged in', correlationId);
|
|
44
|
+
|
|
45
|
+
// Bracket notation with variable (any variable name)
|
|
46
|
+
logger[level]('User logged in', correlationId);
|
|
47
|
+
logger[severity]('User logged in', correlationId);
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
The four argument signatures:
|
|
51
|
+
|
|
52
|
+
```js
|
|
37
53
|
// (message, correlationId)
|
|
38
54
|
logger.info('User logged in', correlationId);
|
|
39
55
|
|
|
@@ -64,6 +80,9 @@ logger.info('User logged in', { userId: 123 }, correlationId, { type: 'audit' })
|
|
|
64
80
|
|
|
65
81
|
- The rule only applies to **default imports** (e.g. `import logger from '...'`)
|
|
66
82
|
- Named imports (`import { logger } from '...'`) are ignored
|
|
83
|
+
- Supports dot notation (`logger.info`), bracket notation with a string literal (`logger['info']`), and bracket notation with a variable (`logger[level]`)
|
|
84
|
+
- For bracket notation with a string literal, the value must be a recognised log level
|
|
85
|
+
- For bracket notation with a variable, the call is always validated (the variable is assumed to hold a valid level)
|
|
67
86
|
- Minimum 2 arguments, maximum 4
|
|
68
87
|
- `message` must be a string (when passed as a literal)
|
|
69
88
|
- `correlationId` must be a string (when passed as a literal)
|
|
@@ -104,6 +123,13 @@ Reports an error when the options object is missing a <code>type</code> property
|
|
|
104
123
|
<code>type</code> value is a non-string literal. Variable references (e.g. <code>{ type: someVar }</code>)
|
|
105
124
|
are allowed because their runtime type cannot be determined statically.</p>
|
|
106
125
|
</dd>
|
|
126
|
+
<dt><a href="#resolveLevel">resolveLevel(callee)</a> ⇒ <code>string</code> | <code>null</code></dt>
|
|
127
|
+
<dd><p>Resolve the log level from a MemberExpression callee.
|
|
128
|
+
Supports dot notation (<code>logger.info</code>), bracket notation with a string literal
|
|
129
|
+
(<code>logger['info']</code>), and bracket notation with a variable (<code>logger[level]</code>).
|
|
130
|
+
For string literals the value must be a recognised level; variables are assumed
|
|
131
|
+
to hold a valid level and the variable name is returned for error messages.</p>
|
|
132
|
+
</dd>
|
|
107
133
|
<dt><a href="#validateStringArg">validateStringArg(context, argNode, level, messageId)</a></dt>
|
|
108
134
|
<dd><p>Validate that a logger argument is a string when it is a literal.
|
|
109
135
|
Non-literal nodes (identifiers, call expressions) are skipped because their
|
|
@@ -182,6 +208,22 @@ are allowed because their runtime type cannot be determined statically.
|
|
|
182
208
|
| optionsArg | <code>Object</code> | The options argument AST node. |
|
|
183
209
|
| level | <code>string</code> | The logger level (e.g. 'info', 'error'). |
|
|
184
210
|
|
|
211
|
+
<a name="resolveLevel"></a>
|
|
212
|
+
|
|
213
|
+
## resolveLevel(callee) ⇒ <code>string</code> \| <code>null</code>
|
|
214
|
+
Resolve the log level from a MemberExpression callee.
|
|
215
|
+
Supports dot notation (`logger.info`), bracket notation with a string literal
|
|
216
|
+
(`logger['info']`), and bracket notation with a variable (`logger[level]`).
|
|
217
|
+
For string literals the value must be a recognised level; variables are assumed
|
|
218
|
+
to hold a valid level and the variable name is returned for error messages.
|
|
219
|
+
|
|
220
|
+
**Kind**: global function
|
|
221
|
+
**Returns**: <code>string</code> \| <code>null</code> - The resolved level name, or null when the call should be skipped.
|
|
222
|
+
|
|
223
|
+
| Param | Type | Description |
|
|
224
|
+
| --- | --- | --- |
|
|
225
|
+
| callee | <code>Object</code> | The MemberExpression callee node. |
|
|
226
|
+
|
|
185
227
|
<a name="validateStringArg"></a>
|
|
186
228
|
|
|
187
229
|
## validateStringArg(context, argNode, level, messageId)
|
package/index.js
CHANGED
|
@@ -63,6 +63,31 @@ const validateOptions = (context, optionsArg, level) => {
|
|
|
63
63
|
}
|
|
64
64
|
};
|
|
65
65
|
|
|
66
|
+
/**
|
|
67
|
+
* Resolve the log level from a MemberExpression callee.
|
|
68
|
+
* Supports dot notation (`logger.info`), bracket notation with a string literal
|
|
69
|
+
* (`logger['info']`), and bracket notation with a variable (`logger[level]`).
|
|
70
|
+
* For string literals the value must be a recognised level; variables are assumed
|
|
71
|
+
* to hold a valid level and the variable name is returned for error messages.
|
|
72
|
+
* @param {Object} callee - The MemberExpression callee node.
|
|
73
|
+
* @returns {string|null} The resolved level name, or null when the call should be skipped.
|
|
74
|
+
*/
|
|
75
|
+
const resolveLevel = (callee) => {
|
|
76
|
+
if (!callee.computed) {
|
|
77
|
+
const level = callee.property.name;
|
|
78
|
+
return LEVELS.has(level) ? level : null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const prop = callee.property;
|
|
82
|
+
if (prop.type === 'Literal' && typeof prop.value === 'string') {
|
|
83
|
+
return LEVELS.has(prop.value) ? prop.value : null;
|
|
84
|
+
}
|
|
85
|
+
if (prop.type === 'Identifier') {
|
|
86
|
+
return prop.name;
|
|
87
|
+
}
|
|
88
|
+
return null;
|
|
89
|
+
};
|
|
90
|
+
|
|
66
91
|
/**
|
|
67
92
|
* Validate that a logger argument is a string when it is a literal.
|
|
68
93
|
* Non-literal nodes (identifiers, call expressions) are skipped because their
|
|
@@ -117,10 +142,9 @@ const plugin = {
|
|
|
117
142
|
if (node.callee.type !== 'MemberExpression') return;
|
|
118
143
|
if (node.callee.object.type !== 'Identifier') return;
|
|
119
144
|
if (!defaultImports.has(node.callee.object.name)) return;
|
|
120
|
-
if (node.callee.computed) return;
|
|
121
145
|
|
|
122
|
-
const level = node.callee
|
|
123
|
-
if (!
|
|
146
|
+
const level = resolveLevel(node.callee);
|
|
147
|
+
if (!level) return;
|
|
124
148
|
|
|
125
149
|
const args = node.arguments;
|
|
126
150
|
|