eslint-plugin-sfmc 5.2.0 → 5.2.2

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.
Files changed (35) hide show
  1. package/README.md +7 -7
  2. package/docs/unicorn-compatibility.md +22 -15
  3. package/package.json +10 -10
  4. package/src/ampscript-processor.js +1 -5
  5. package/src/index.js +8 -4
  6. package/src/processor.js +1 -5
  7. package/src/rules/amp/argument-types.js +6 -8
  8. package/src/rules/amp/function-arity.js +6 -11
  9. package/src/rules/amp/naming-convention.js +7 -9
  10. package/src/rules/amp/no-loop-counter-assign.js +12 -11
  11. package/src/rules/amp/prefer-attribute-value.js +6 -11
  12. package/src/rules/amp/prefer-boolean-literal.js +6 -5
  13. package/src/rules/amp/require-variable-declaration.js +15 -14
  14. package/src/rules/hbs/_shared.js +8 -18
  15. package/src/rules/hbs/no-mcn-unsupported.js +3 -4
  16. package/src/rules/ssjs/cache-loop-length.js +8 -11
  17. package/src/rules/ssjs/http-property-value.js +7 -11
  18. package/src/rules/ssjs/no-clr-header-access.js +7 -16
  19. package/src/rules/ssjs/no-deprecated-function.js +9 -14
  20. package/src/rules/ssjs/no-invalid-property-access.js +7 -11
  21. package/src/rules/ssjs/no-mcn-unsupported.js +5 -4
  22. package/src/rules/ssjs/no-property-call.js +4 -4
  23. package/src/rules/ssjs/no-treatascontent-injection.js +7 -10
  24. package/src/rules/ssjs/no-unavailable-method.js +14 -14
  25. package/src/rules/ssjs/no-unknown-function.js +39 -37
  26. package/src/rules/ssjs/platform-function-arity.js +54 -57
  27. package/src/rules/ssjs/prefer-parsejson-safe-argument.js +1 -4
  28. package/src/rules/ssjs/require-hasownproperty.js +70 -91
  29. package/src/rules/ssjs/require-platform-load-order.js +3 -7
  30. package/src/rules/ssjs/require-platform-load.js +1 -4
  31. package/src/rules/ssjs/require-string-clr-content.js +5 -14
  32. package/src/rules/ssjs/ssjs-argument-types.js +22 -28
  33. package/src/rules/ssjs/ssjs-core-method-arity.js +20 -24
  34. package/src/rules/ssjs/ssjs-no-nonfunctional-method.js +20 -26
  35. package/src/ssjs-processor.js +1 -5
@@ -35,70 +35,69 @@ export default {
35
35
 
36
36
  const hasGuard = statements.some((statement) => containsHasOwnProperty(statement));
37
37
 
38
- if (!hasGuard) {
39
- const keyName = getKeyName(node.left);
40
- const objectText = context.sourceCode.getText(node.right);
41
-
42
- if (!keyName) {
43
- context.report({
44
- node,
45
- messageId: 'missingGuard',
46
- });
47
- return;
48
- }
38
+ if (hasGuard) {
39
+ return;
40
+ }
41
+ const keyName = getKeyName(node.left);
42
+ const objectText = context.sourceCode.getText(node.right);
49
43
 
44
+ if (!keyName) {
50
45
  context.report({
51
46
  node,
52
47
  messageId: 'missingGuard',
53
- fix(fixer) {
54
- const sourceCode = context.sourceCode;
55
- const innerStatements =
56
- body.type === 'BlockStatement' ? body.body : [body];
57
-
58
- // Single-line loops keep compact output to avoid reformatting
59
- // code the author intentionally wrote on one line.
60
- const isSingleLine = node.loc.start.line === node.loc.end.line;
61
- if (isSingleLine) {
62
- const compactBody = innerStatements
63
- .map((statement) => sourceCode.getText(statement))
64
- .join(' ');
65
- return fixer.replaceText(
66
- body,
67
- `{ if (${objectText}.hasOwnProperty(${keyName})) { ${compactBody} } }`,
68
- );
69
- }
70
-
71
- // Multi-line loops: emit a properly-indented guard block.
72
- const loopLine = sourceCode.lines[node.loc.start.line - 1] ?? '';
73
- const baseIndent = (loopLine.match(/^[\t ]*/) ?? [''])[0];
74
- const firstStatement = innerStatements[0];
75
- const firstStatementLine = firstStatement
76
- ? (sourceCode.lines[firstStatement.loc.start.line - 1] ?? '')
77
- : '';
78
- const firstStatementIndent = (firstStatementLine.match(/^[\t ]*/) ?? [
79
- '',
80
- ])[0];
81
- const unit =
82
- firstStatementIndent.length > baseIndent.length
83
- ? firstStatementIndent.slice(baseIndent.length)
84
- : ' '.repeat(4);
85
- const guardIndent = baseIndent + unit;
86
- const statementIndent = guardIndent + unit;
87
-
88
- const guardedBody = innerStatements
89
- .map(
90
- (statement) =>
91
- `${statementIndent}${sourceCode.getText(statement)}`,
92
- )
93
- .join('\n');
48
+ });
49
+ return;
50
+ }
94
51
 
52
+ context.report({
53
+ node,
54
+ messageId: 'missingGuard',
55
+ fix(fixer) {
56
+ const sourceCode = context.sourceCode;
57
+ const innerStatements = body.type === 'BlockStatement' ? body.body : [body];
58
+
59
+ // Single-line loops keep compact output to avoid reformatting
60
+ // code the author intentionally wrote on one line.
61
+ const isSingleLine = node.loc.start.line === node.loc.end.line;
62
+ if (isSingleLine) {
63
+ const compactBody = innerStatements
64
+ .map((statement) => sourceCode.getText(statement))
65
+ .join(' ');
95
66
  return fixer.replaceText(
96
67
  body,
97
- `{\n${guardIndent}if (${objectText}.hasOwnProperty(${keyName})) {\n${guardedBody}\n${guardIndent}}\n${baseIndent}}`,
68
+ `{ if (${objectText}.hasOwnProperty(${keyName})) { ${compactBody} } }`,
98
69
  );
99
- },
100
- });
101
- }
70
+ }
71
+
72
+ // Multi-line loops: emit a properly-indented guard block.
73
+ const loopLine = sourceCode.lines[node.loc.start.line - 1] ?? '';
74
+ const baseIndent = (loopLine.match(/^[\t ]*/) ?? [''])[0];
75
+ const firstStatement = innerStatements[0];
76
+ const firstStatementLine = firstStatement
77
+ ? (sourceCode.lines[firstStatement.loc.start.line - 1] ?? '')
78
+ : '';
79
+ const firstStatementIndent = (firstStatementLine.match(/^[\t ]*/) ?? [
80
+ '',
81
+ ])[0];
82
+ const unit =
83
+ firstStatementIndent.length > baseIndent.length
84
+ ? firstStatementIndent.slice(baseIndent.length)
85
+ : ' '.repeat(4);
86
+ const guardIndent = baseIndent + unit;
87
+ const statementIndent = guardIndent + unit;
88
+
89
+ const guardedBody = innerStatements
90
+ .map(
91
+ (statement) => `${statementIndent}${sourceCode.getText(statement)}`,
92
+ )
93
+ .join('\n');
94
+
95
+ return fixer.replaceText(
96
+ body,
97
+ `{\n${guardIndent}if (${objectText}.hasOwnProperty(${keyName})) {\n${guardedBody}\n${guardIndent}}\n${baseIndent}}`,
98
+ );
99
+ },
100
+ });
102
101
  },
103
102
  };
104
103
  },
@@ -120,19 +119,12 @@ function containsHasOwnProperty(node) {
120
119
  return true;
121
120
  }
122
121
 
123
- if (current.type === 'IfStatement') {
124
- return (
125
- hasOwnPropertyTest(current.test) ||
126
- containsHasOwnProperty(current.consequent) ||
127
- containsHasOwnProperty(current.alternate)
128
- );
129
- }
130
-
131
- if (current.type === 'BlockStatement') {
132
- return current.body.some((child) => containsHasOwnProperty(child));
133
- }
134
-
135
- return false;
122
+ return current.type === 'IfStatement'
123
+ ? hasOwnPropertyTest(current.test) ||
124
+ containsHasOwnProperty(current.consequent) ||
125
+ containsHasOwnProperty(current.alternate)
126
+ : current.type === 'BlockStatement' &&
127
+ current.body.some((child) => containsHasOwnProperty(child));
136
128
  }
137
129
 
138
130
  function hasOwnPropertyTest(node) {
@@ -143,24 +135,14 @@ function hasOwnPropertyTest(node) {
143
135
  current = current.argument;
144
136
  }
145
137
 
146
- if (!current) {
147
- return false;
148
- }
149
-
150
- if (
151
- current.type === 'CallExpression' &&
152
- current.callee.type === 'MemberExpression' &&
153
- current.callee.property.type === 'Identifier' &&
154
- current.callee.property.name === 'hasOwnProperty'
155
- ) {
156
- return true;
157
- }
158
-
159
- if (current.type === 'LogicalExpression') {
160
- return hasOwnPropertyTest(current.left) || hasOwnPropertyTest(current.right);
161
- }
162
-
163
- return false;
138
+ return current
139
+ ? (current.type === 'CallExpression' &&
140
+ current.callee.type === 'MemberExpression' &&
141
+ current.callee.property.type === 'Identifier' &&
142
+ current.callee.property.name === 'hasOwnProperty') ||
143
+ (current.type === 'LogicalExpression' &&
144
+ (hasOwnPropertyTest(current.left) || hasOwnPropertyTest(current.right)))
145
+ : false;
164
146
  }
165
147
 
166
148
  /**
@@ -175,8 +157,5 @@ function getKeyName(left) {
175
157
  const id = left.declarations[0].id;
176
158
  return id.type === 'Identifier' ? id.name : null;
177
159
  }
178
- if (left.type === 'Identifier') {
179
- return left.name;
180
- }
181
- return null;
160
+ return left.type === 'Identifier' ? left.name : null;
182
161
  }
@@ -103,14 +103,10 @@ function getCoreObjectUsage(node) {
103
103
  return `${callee.object.object.name}.${callee.object.property.name}`;
104
104
  }
105
105
 
106
- if (
107
- callee.object.type === 'Identifier' &&
106
+ return callee.object.type === 'Identifier' &&
108
107
  TOP_LEVEL_CORE_NAMES.has(callee.object.name) &&
109
108
  callee.property.type === 'Identifier' &&
110
109
  callee.property.name === 'Init'
111
- ) {
112
- return callee.object.name;
113
- }
114
-
115
- return null;
110
+ ? callee.object.name
111
+ : null;
116
112
  }
@@ -105,10 +105,7 @@ function getCoreObjectUsage(node) {
105
105
 
106
106
  // Bare call: Now(), Write(), GUID(), Base64Encode(), Redirect(), …
107
107
  if (callee.type === 'Identifier') {
108
- if (CORE_LOAD_DEPENDENT_GLOBALS.has(callee.name.toLowerCase())) {
109
- return callee.name;
110
- }
111
- return null;
108
+ return CORE_LOAD_DEPENDENT_GLOBALS.has(callee.name.toLowerCase()) ? callee.name : null;
112
109
  }
113
110
 
114
111
  if (callee.type !== 'MemberExpression') {
@@ -46,16 +46,10 @@ function isHttpConstructorMember(node) {
46
46
  * @returns {boolean} Whether the expression yields an HTTP request instance
47
47
  */
48
48
  function isHttpRequestInit(node) {
49
- if (!node) {
50
- return false;
51
- }
52
- if (node.type === 'NewExpression') {
53
- return isHttpConstructorMember(node.callee);
54
- }
55
- if (node.type === 'CallExpression') {
56
- return isHttpConstructorMember(node.callee);
57
- }
58
- return false;
49
+ return node
50
+ ? (node.type === 'NewExpression' || node.type === 'CallExpression') &&
51
+ isHttpConstructorMember(node.callee)
52
+ : false;
59
53
  }
60
54
 
61
55
  /**
@@ -153,10 +147,7 @@ export default {
153
147
 
154
148
  // resp.content — non-computed member read on a tracked response var.
155
149
  'MemberExpression[computed=false]'(node) {
156
- if (!isTrackedContentMember(node)) {
157
- return;
158
- }
159
- if (isWrappedInString(node)) {
150
+ if (!isTrackedContentMember(node) || isWrappedInString(node)) {
160
151
  return;
161
152
  }
162
153
  pending.push(node);
@@ -143,10 +143,7 @@ export default {
143
143
  return;
144
144
  }
145
145
 
146
- if (callee.type !== 'MemberExpression') {
147
- return;
148
- }
149
- if (callee.property.type !== 'Identifier') {
146
+ if (callee.type !== 'MemberExpression' || callee.property.type !== 'Identifier') {
150
147
  return;
151
148
  }
152
149
  const methodName = callee.property.name;
@@ -228,14 +225,16 @@ export default {
228
225
 
229
226
  // Multi-part static: DataExtension.Rows.Add(...), TriggeredSend.Tracking.Clicks.Retrieve(...)
230
227
  const objectPath = getMemberPath(callee.object);
231
- if (objectPath && coreObjectNames.has(objectPath)) {
232
- const classLookup = coreMethodArityLookup.get(objectPath.toLowerCase());
233
- if (classLookup) {
234
- const entry = classLookup.get(methodName.toLowerCase());
235
- if (entry) {
236
- checkArguments(entry, node.arguments, `${objectPath}.${methodName}`);
237
- }
238
- }
228
+ if (!objectPath || !coreObjectNames.has(objectPath)) {
229
+ return;
230
+ }
231
+ const classLookup = coreMethodArityLookup.get(objectPath.toLowerCase());
232
+ if (!classLookup) {
233
+ return;
234
+ }
235
+ const entry = classLookup.get(methodName.toLowerCase());
236
+ if (entry) {
237
+ checkArguments(entry, node.arguments, `${objectPath}.${methodName}`);
239
238
  }
240
239
  },
241
240
  };
@@ -247,10 +246,11 @@ function getCoreInitType(node) {
247
246
  return null;
248
247
  }
249
248
  const callee = node.callee;
250
- if (callee.type !== 'MemberExpression') {
251
- return null;
252
- }
253
- if (callee.property.type !== 'Identifier' || callee.property.name !== 'Init') {
249
+ if (
250
+ callee.type !== 'MemberExpression' ||
251
+ callee.property.type !== 'Identifier' ||
252
+ callee.property.name !== 'Init'
253
+ ) {
254
254
  return null;
255
255
  }
256
256
  if (callee.object.type === 'Identifier' && coreObjectNames.has(callee.object.name)) {
@@ -313,19 +313,13 @@ function inferArgumentType(node) {
313
313
  ) {
314
314
  return 'string[]';
315
315
  }
316
- if (
317
- elements.every(
318
- (element) => element.type === 'Literal' && typeof element.value === 'number',
319
- )
320
- ) {
321
- return 'number[]';
322
- }
323
- return 'array';
316
+ return elements.every(
317
+ (element) => element.type === 'Literal' && typeof element.value === 'number',
318
+ )
319
+ ? 'number[]'
320
+ : 'array';
324
321
  }
325
- if (node.type === 'ObjectExpression') {
326
- return 'object';
327
- }
328
- return null;
322
+ return node.type === 'ObjectExpression' ? 'object' : null;
329
323
  }
330
324
 
331
325
  function isTypeCompatible(actual, expected) {
@@ -79,10 +79,7 @@ export default {
79
79
 
80
80
  CallExpression(node) {
81
81
  const callee = node.callee;
82
- if (callee.type !== 'MemberExpression') {
83
- return;
84
- }
85
- if (callee.property.type !== 'Identifier') {
82
+ if (callee.type !== 'MemberExpression' || callee.property.type !== 'Identifier') {
86
83
  return;
87
84
  }
88
85
  const methodName = callee.property.name;
@@ -141,23 +138,21 @@ export default {
141
138
  // Instance sub-path: de.Rows.Add(...) where `de` is a tracked
142
139
  // DataExtension.Init(...) instance. Substitute the instance's
143
140
  // core type for the leftmost identifier and resolve the class key.
144
- if (objectPath) {
145
- const segments = objectPath.split('.');
146
- const rootCoreType = coreVariables.get(segments[0]);
147
- if (rootCoreType) {
148
- const resolvedPath = [rootCoreType, ...segments.slice(1)].join('.');
149
- const classLookup = coreMethodArityLookup.get(resolvedPath.toLowerCase());
150
- if (classLookup) {
151
- const entry = classLookup.get(methodName.toLowerCase());
152
- checkArity(
153
- entry,
154
- node.arguments,
155
- `${resolvedPath}.${methodName}`,
156
- callee.property,
157
- );
158
- }
159
- }
141
+ if (!objectPath) {
142
+ return;
143
+ }
144
+ const segments = objectPath.split('.');
145
+ const rootCoreType = coreVariables.get(segments[0]);
146
+ if (!rootCoreType) {
147
+ return;
148
+ }
149
+ const resolvedPath = [rootCoreType, ...segments.slice(1)].join('.');
150
+ const classLookup = coreMethodArityLookup.get(resolvedPath.toLowerCase());
151
+ if (!classLookup) {
152
+ return;
160
153
  }
154
+ const entry = classLookup.get(methodName.toLowerCase());
155
+ checkArity(entry, node.arguments, `${resolvedPath}.${methodName}`, callee.property);
161
156
  },
162
157
  };
163
158
  },
@@ -168,10 +163,11 @@ function getCoreInitType(node) {
168
163
  return null;
169
164
  }
170
165
  const callee = node.callee;
171
- if (callee.type !== 'MemberExpression') {
172
- return null;
173
- }
174
- if (callee.property.type !== 'Identifier' || callee.property.name !== 'Init') {
166
+ if (
167
+ callee.type !== 'MemberExpression' ||
168
+ callee.property.type !== 'Identifier' ||
169
+ callee.property.name !== 'Init'
170
+ ) {
175
171
  return null;
176
172
  }
177
173
  if (callee.object.type === 'Identifier' && coreObjectNames.has(callee.object.name)) {
@@ -101,10 +101,7 @@ export default {
101
101
 
102
102
  CallExpression(node) {
103
103
  const callee = node.callee;
104
- if (callee.type !== 'MemberExpression') {
105
- return;
106
- }
107
- if (callee.property.type !== 'Identifier') {
104
+ if (callee.type !== 'MemberExpression' || callee.property.type !== 'Identifier') {
108
105
  return;
109
106
  }
110
107
  const methodName = callee.property.name;
@@ -167,25 +164,21 @@ export default {
167
164
  // Instance sub-path: de.Rows.Add(...) where `de` is a tracked
168
165
  // Init(...) instance. Substitute the instance's core type for the
169
166
  // leftmost identifier and resolve the class key.
170
- if (objectPath) {
171
- const segments = objectPath.split('.');
172
- const rootCoreType = coreVariables.get(segments[0]);
173
- if (rootCoreType) {
174
- const resolvedPath = [rootCoreType, ...segments.slice(1)].join('.');
175
- const classLookup = coreNonFunctionalMethodLookup.get(
176
- resolvedPath.toLowerCase(),
177
- );
178
- if (classLookup) {
179
- const entry = classLookup.get(methodName.toLowerCase());
180
- checkNonFunctional(
181
- entry,
182
- `${resolvedPath}.${methodName}`,
183
- callee.property,
184
- true,
185
- );
186
- }
187
- }
167
+ if (!objectPath) {
168
+ return;
188
169
  }
170
+ const segments = objectPath.split('.');
171
+ const rootCoreType = coreVariables.get(segments[0]);
172
+ if (!rootCoreType) {
173
+ return;
174
+ }
175
+ const resolvedPath = [rootCoreType, ...segments.slice(1)].join('.');
176
+ const classLookup = coreNonFunctionalMethodLookup.get(resolvedPath.toLowerCase());
177
+ if (!classLookup) {
178
+ return;
179
+ }
180
+ const entry = classLookup.get(methodName.toLowerCase());
181
+ checkNonFunctional(entry, `${resolvedPath}.${methodName}`, callee.property, true);
189
182
  },
190
183
  };
191
184
  },
@@ -202,10 +195,11 @@ function getCoreInitType(node) {
202
195
  return null;
203
196
  }
204
197
  const callee = node.callee;
205
- if (callee.type !== 'MemberExpression') {
206
- return null;
207
- }
208
- if (callee.property.type !== 'Identifier' || callee.property.name !== 'Init') {
198
+ if (
199
+ callee.type !== 'MemberExpression' ||
200
+ callee.property.type !== 'Identifier' ||
201
+ callee.property.name !== 'Init'
202
+ ) {
209
203
  return null;
210
204
  }
211
205
  if (callee.object.type === 'Identifier' && coreObjectNames.has(callee.object.name)) {
@@ -36,11 +36,7 @@ export function preprocess(text, filename) {
36
36
  });
37
37
  }
38
38
 
39
- if (blocks.length === 0) {
40
- return [text];
41
- }
42
-
43
- return blocks;
39
+ return blocks.length === 0 ? [text] : blocks;
44
40
  }
45
41
 
46
42
  export function postprocess(messages, _filename) {