eslint-plugin-crisp 1.7.8 → 1.7.9

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-crisp",
3
- "version": "1.7.8",
3
+ "version": "1.7.9",
4
4
  "description": "Custom ESLint Rules for Crisp",
5
5
  "author": "Crisp IM SAS",
6
6
  "main": "index.js",
@@ -94,6 +94,38 @@ function isReference(node) {
94
94
  );
95
95
  }
96
96
 
97
+ // Checks a fallback expression like `value || "none"` or `value ?? other`
98
+ function isFallback(node) {
99
+ node = unwrapTransparentExpression(node);
100
+
101
+ if (
102
+ !node ||
103
+ node.type !== "LogicalExpression" ||
104
+ (node.operator !== "||" && node.operator !== "??")
105
+ ) {
106
+ return false;
107
+ }
108
+
109
+ const isOperandValid = (operand) => {
110
+ operand = unwrapTransparentExpression(operand);
111
+
112
+ return (
113
+ operand.type === "Literal" ||
114
+ isReference(operand) ||
115
+ isFallback(operand)
116
+ );
117
+ };
118
+
119
+ return isReference(node.left) || isFallback(node.left)
120
+ ? isOperandValid(node.right)
121
+ : false;
122
+ }
123
+
124
+ // Checks a logger argument value is a reference, a fallback or an object of those
125
+ function isAllowedValue(node) {
126
+ return isReference(node) || isFallback(node) || isReferenceObject(node);
127
+ }
128
+
97
129
  // Checks an inline object only contains references (no computed values)
98
130
  function isReferenceObject(node) {
99
131
  node = unwrapTransparentExpression(node);
@@ -115,7 +147,7 @@ function isReferenceObject(node) {
115
147
  return false;
116
148
  }
117
149
 
118
- return isReference(property.value) || isReferenceObject(property.value);
150
+ return isAllowedValue(property.value);
119
151
  });
120
152
  }
121
153
 
@@ -132,7 +164,7 @@ export default {
132
164
  message:
133
165
  "Logger messages must be single-line, double-quoted string literals.",
134
166
  reference:
135
- "Logger interpolation arguments must be references or inline objects of references, not computed expressions."
167
+ "Logger interpolation arguments must be references, fallbacks (`||`, `??`) or inline objects of those, not computed expressions."
136
168
  }
137
169
  },
138
170
 
@@ -161,7 +193,7 @@ export default {
161
193
  }
162
194
 
163
195
  for (const value of values) {
164
- if (!isReference(value) && !isReferenceObject(value)) {
196
+ if (!isAllowedValue(value)) {
165
197
  context.report({
166
198
  node: value,
167
199
  messageId: "reference"
@@ -15,6 +15,10 @@ ruleTester.run("logger-arguments", rule, {
15
15
  value
16
16
  );`,
17
17
  "App.logger.info(\"This intentionally long logger message may exceed the usual source line length when necessary.\");",
18
+ "App.logger.debug(\"Loaded value: %s\", value || \"default\");",
19
+ "App.logger.debug(\"Loaded value: %s\", this.value ?? \"none\");",
20
+ "App.logger.debug(\"Loaded value: %s\", first || second || \"none\");",
21
+ "App.logger.debug(\"Loaded value: %s\", { user: userId || \"none\" });",
18
22
  "App.logger.warn(\"Shutting down\", { bufferedCount });",
19
23
  "App.logger.warn(\"Shutting down\", { count: this.count, ...meta });",
20
24
  "App.logger.warn(\"Shutting down\", { nested: { bufferedCount } }, logMeta);",
@@ -80,7 +84,15 @@ ruleTester.run("logger-arguments", rule, {
80
84
  ]
81
85
  },
82
86
  {
83
- code: "App.logger.debug(\"Loaded value: %s\", value || \"default\");",
87
+ code: "App.logger.debug(\"Loaded value: %s\", value || compute());",
88
+ errors: [
89
+ {
90
+ messageId: "reference"
91
+ }
92
+ ]
93
+ },
94
+ {
95
+ code: "App.logger.debug(\"Loaded value: %s\", value && \"default\");",
84
96
  errors: [
85
97
  {
86
98
  messageId: "reference"
@@ -120,7 +132,7 @@ ruleTester.run("logger-arguments", rule, {
120
132
  ]
121
133
  },
122
134
  {
123
- code: "App.logger.debug(\"Values: %s / %s\", values.join(\", \"), fallback || \"none\");",
135
+ code: "App.logger.debug(\"Values: %s / %s\", values.join(\", \"), fallback || compute());",
124
136
  errors: [
125
137
  {
126
138
  messageId: "reference"