eslint-plugin-sfmc 4.15.0 → 4.16.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.
@@ -1,37 +1,51 @@
1
1
  # `sfmc/ssjs-no-switch-default`
2
2
 
3
- Disallow `default` case in `switch` statements.
3
+ Discourage relying on a `default` case in `switch` statements.
4
4
 
5
5
  ## Why
6
6
 
7
- SFMC's SSJS engine may silently skip the `default` case in `switch` statements. This is a known engine bug that causes code in the `default` block to never execute, leading to hard-to-debug logic errors.
7
+ Community lore held that the `default` case of a `switch` "may not execute" in SFMC SSJS. Live CloudPage probing disproved that framing: a last-clause `default` on a no-match path executes reliably.
8
+
9
+ The real defect is in the underlying [Jint](https://github.com/sebastienros/jint) engine that SFMC SSJS runs on. Because of [jint#2607](https://github.com/sebastienros/jint/pull/2607), an executed `break` inside a `switch` that sits inside a **function** can abnormally complete the function — the function returns `undefined` and any statements after the `switch` are skipped. The fault is intermittent across compilations. When the swallowed code was the caller's fallback (often reached via `default`), it looks like "`default` didn't run", but the trigger is the executed `break`, not the `default` keyword.
10
+
11
+ Detecting the `break`-in-function-scoped-`switch` case precisely is out of scope for a lint rule, so this rule flags the `default` clause as a cheap heuristic hint: a `switch` that leans on `default` is exactly the shape most likely to depend on this fragile control flow.
8
12
 
9
13
  ## Recommended approach
10
14
 
11
- Enumerate all expected values as explicit `case` labels:
15
+ Prefer a lookup map or `if`/`else if` for control flow that must survive a `break`:
12
16
 
13
17
  ```js
14
- // Bad -- 'default' may not execute
15
- switch (status) {
16
- case 'active':
17
- doActive();
18
- break;
19
- default:
20
- doFallback();
21
- break;
18
+ // Bad -- function-scoped switch/break; return may be skipped, caller sees undefined
19
+ function classify(status) {
20
+ var out = 'PRE';
21
+ switch (status) {
22
+ case 'active':
23
+ out = 'A';
24
+ break;
25
+ default:
26
+ out = 'DEF';
27
+ }
28
+ return out;
29
+ }
30
+
31
+ // Good -- lookup map, no switch/break to escape
32
+ function classify(status) {
33
+ var map = { active: 'A' };
34
+ return map[status] || 'DEF';
22
35
  }
23
36
 
24
- // Good -- explicit cases
25
- switch (status) {
26
- case 'active':
27
- doActive();
28
- break;
29
- case 'inactive':
30
- doFallback();
31
- break;
37
+ // Good -- if / else if
38
+ function classify(status) {
39
+ if (status === 'active') return 'A';
40
+ return 'DEF';
32
41
  }
33
42
  ```
34
43
 
44
+ ## Related
45
+
46
+ - ssjs.guide known bug: [`switch` `break` can escape a function](https://ssjs.guide/engine-limitations/known-bugs/#switch-break-escape)
47
+ - Upstream fix: [jint#2607](https://github.com/sebastienros/jint/pull/2607)
48
+
35
49
  ## Settings
36
50
 
37
51
  This rule has no options.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-sfmc",
3
- "version": "4.15.0",
3
+ "version": "4.16.1",
4
4
  "description": "ESLint plugin for Salesforce Marketing Cloud Engagement+Next - AMPscript, Server-Side JavaScript (SSJS) and Handlebars",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -36,13 +36,13 @@
36
36
  "mcn"
37
37
  ],
38
38
  "dependencies": {
39
- "@handlebars/parser": "^2.2.2",
40
- "ampscript-data": "^3.5.0",
39
+ "ampscript-data": "^4.0.0",
41
40
  "ampscript-parser": "^0.3.1",
42
41
  "eslint-plugin-mso-email": "^2.0.0",
43
- "handlebars-data": "^0.3.1",
42
+ "handlebars-data": "^0.4.0",
44
43
  "mso-conditional-parser": "^2.0.1",
45
- "ssjs-data": "^1.6.0"
44
+ "sfmc-handlebars-parser": "^0.1.0",
45
+ "ssjs-data": "^2.0.0"
46
46
  },
47
47
  "peerDependencies": {
48
48
  "eslint": ">=9.0.0"
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * ESLint-compatible parser for Marketing Cloud Next Handlebars.
3
3
  *
4
- * Wraps the `@handlebars/parser` package and adds the metadata ESLint needs:
4
+ * Wraps the `sfmc-handlebars-parser` package and adds the metadata ESLint needs:
5
5
  * - `range: [start, end]` on every node (derived from the parser's
6
6
  * 1-based-line / 0-based-column `loc` via a line-start table)
7
7
  * - `loc: { start: {line,column}, end: {line,column} }` (kept from the parser,
@@ -13,7 +13,7 @@
13
13
  * runs (the processor does this) so Handlebars never sees `%%[ ... ]%%` syntax.
14
14
  */
15
15
 
16
- import { parse as hbsParse } from '@handlebars/parser';
16
+ import { parse as hbsParse } from 'sfmc-handlebars-parser';
17
17
 
18
18
  // ── Node renaming for ESTree collisions ───────────────────────────────────
19
19
  //
@@ -97,7 +97,7 @@ function annotateNode(node, lineStarts, textLength, fallback) {
97
97
  }
98
98
 
99
99
  // ESLint requires every traversed node to expose a numeric `range` and a
100
- // matching `loc`. `@handlebars/parser` attaches `loc` to most nodes but a
100
+ // matching `loc`. `sfmc-handlebars-parser` attaches `loc` to most nodes but a
101
101
  // few (e.g. Hash, HashPair) can omit it — inherit the parent's span so the
102
102
  // traverser never sees an undefined range.
103
103
  let span = fallback;
package/src/index.js CHANGED
@@ -55,6 +55,7 @@ import ssjsPreferPlatformLoadVersion from './rules/ssjs/prefer-platform-load-ver
55
55
  import ssjsNoUnavailableMethod from './rules/ssjs/no-unavailable-method.js';
56
56
  import ssjsPreferParsejsonSafeArgument from './rules/ssjs/prefer-parsejson-safe-argument.js';
57
57
  import ssjsNoSwitchDefault from './rules/ssjs/no-switch-default.js';
58
+ import ssjsNoSwitchFallthrough from './rules/ssjs/no-switch-fallthrough.js';
58
59
  import ssjsNoTreatAsContentInjection from './rules/ssjs/no-treatascontent-injection.js';
59
60
  import ssjsArgumentTypes from './rules/ssjs/ssjs-argument-types.js';
60
61
  import ssjsCoreMethodArity from './rules/ssjs/ssjs-core-method-arity.js';
@@ -141,6 +142,7 @@ const plugin = {
141
142
  'ssjs-no-unavailable-method': ssjsNoUnavailableMethod,
142
143
  'ssjs-prefer-parsejson-safe-arg': ssjsPreferParsejsonSafeArgument,
143
144
  'ssjs-no-switch-default': ssjsNoSwitchDefault,
145
+ 'ssjs-no-switch-fallthrough': ssjsNoSwitchFallthrough,
144
146
  'ssjs-no-treatascontent-injection': ssjsNoTreatAsContentInjection,
145
147
  'ssjs-arg-types': ssjsArgumentTypes,
146
148
  'ssjs-core-method-arity': ssjsCoreMethodArity,
@@ -190,6 +192,7 @@ const ssjsMcnRules = {
190
192
  'sfmc/ssjs-no-unavailable-method': 'off',
191
193
  'sfmc/ssjs-prefer-parsejson-safe-arg': 'off',
192
194
  'sfmc/ssjs-no-switch-default': 'off',
195
+ 'sfmc/ssjs-no-switch-fallthrough': 'off',
193
196
  'sfmc/ssjs-no-treatascontent-injection': 'off',
194
197
  'sfmc/ssjs-arg-types': 'off',
195
198
  'sfmc/ssjs-core-method-arity': 'off',
@@ -266,6 +269,7 @@ const ssjsRecommendedRules = {
266
269
  'sfmc/ssjs-no-unavailable-method': 'warn',
267
270
  'sfmc/ssjs-prefer-parsejson-safe-arg': 'warn',
268
271
  'sfmc/ssjs-no-switch-default': 'warn',
272
+ 'sfmc/ssjs-no-switch-fallthrough': 'warn',
269
273
  'sfmc/ssjs-no-treatascontent-injection': 'warn',
270
274
  'sfmc/ssjs-arg-types': 'warn',
271
275
  'sfmc/ssjs-core-method-arity': 'warn',
@@ -293,6 +297,7 @@ const ssjsStrictRules = {
293
297
  'sfmc/ssjs-no-unavailable-method': 'warn',
294
298
  'sfmc/ssjs-prefer-parsejson-safe-arg': 'error',
295
299
  'sfmc/ssjs-no-switch-default': 'error',
300
+ 'sfmc/ssjs-no-switch-fallthrough': 'error',
296
301
  'sfmc/ssjs-no-treatascontent-injection': 'error',
297
302
  'sfmc/ssjs-arg-types': 'warn',
298
303
  'sfmc/ssjs-core-method-arity': 'error',
@@ -1,8 +1,16 @@
1
1
  /**
2
2
  * Rule: no-switch-default
3
3
  *
4
- * The `default` case in switch statements may silently fail to execute in
5
- * SFMC's SSJS engine. Explicitly enumerate all expected values instead.
4
+ * SFMC's SSJS runs on an old Jint build affected by jint#2607: an executed
5
+ * `break` inside a `switch` that sits inside a function can abnormally complete
6
+ * the function, so it returns `undefined` and statements after the `switch` are
7
+ * skipped (intermittent across compilations). Community lore mis-reported this
8
+ * as "the `default` case may not execute", but a last-clause `default` itself
9
+ * runs fine — the trigger is the executed `break`.
10
+ *
11
+ * Flagging the `default` clause is a cheap heuristic hint to avoid relying on a
12
+ * function-scoped `switch`/`break` for critical control flow. Prefer a lookup
13
+ * map or `if`/`else if`.
6
14
  */
7
15
 
8
16
  export default {
@@ -10,12 +18,14 @@ export default {
10
18
  type: 'problem',
11
19
  docs: {
12
20
  description:
13
- "Disallow 'default' case in switch statements because SFMC SSJS may not process it",
21
+ "Discourage relying on a 'default' case in switch statements: a function-scoped switch/break can abnormally complete the function in SFMC SSJS (Jint bug)",
14
22
  },
15
23
  messages: {
16
24
  noDefault:
17
- "The 'default' case in a switch statement may not execute in SFMC SSJS. " +
18
- 'List all expected values as explicit cases instead.',
25
+ 'A `break` inside a function-scoped `switch` can abnormally complete the ' +
26
+ 'function in SFMC SSJS (Jint bug), skipping code after the `switch` — often ' +
27
+ 'the `default` fallback. Prefer a lookup map or `if`/`else if` for critical ' +
28
+ 'control flow.',
19
29
  },
20
30
  schema: [],
21
31
  },
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Rule: no-switch-fallthrough
3
+ *
4
+ * SFMC's SSJS engine has NO switch fall-through. A matched `case` runs only its
5
+ * own statements up to the next `case`/`default` — an empty leading label does
6
+ * not share the next label's body, a break-less body does not cascade into the
7
+ * following case, and a matched case never falls into `default`. Give every
8
+ * clause its own break-terminated body, or use if / a lookup map instead.
9
+ */
10
+
11
+ // Statement types that stop execution before the next clause, so a body ending
12
+ // in one of them is not relying on fall-through.
13
+ const TERMINATING_STATEMENTS = new Set([
14
+ 'BreakStatement',
15
+ 'ReturnStatement',
16
+ 'ThrowStatement',
17
+ 'ContinueStatement',
18
+ ]);
19
+
20
+ export default {
21
+ meta: {
22
+ type: 'problem',
23
+ docs: {
24
+ description:
25
+ 'Disallow reliance on switch fall-through because SFMC SSJS never falls through',
26
+ },
27
+ messages: {
28
+ emptyLabelFallthrough:
29
+ 'This empty case relies on fall-through into the next label, but SFMC SSJS has no ' +
30
+ 'fall-through — the shared body never runs. Give this case its own break-terminated ' +
31
+ 'body, or use if / a lookup map.',
32
+ bodyFallthrough:
33
+ 'This case body has no terminating break/return/throw, so it relies on cascading ' +
34
+ 'into the next case — but SFMC SSJS has no fall-through and each case runs only its ' +
35
+ 'own statements. End every case with break, or use if / a lookup map.',
36
+ },
37
+ schema: [],
38
+ },
39
+
40
+ create(context) {
41
+ return {
42
+ SwitchStatement(node) {
43
+ const clauses = node.cases;
44
+ for (let index = 0; index < clauses.length; index++) {
45
+ // The last clause has nothing to fall into, so it is never a problem.
46
+ if (index === clauses.length - 1) {
47
+ continue;
48
+ }
49
+ const clause = clauses[index];
50
+ if (clause.consequent.length === 0) {
51
+ // Empty label stacked before another case/default.
52
+ context.report({ node: clause, messageId: 'emptyLabelFallthrough' });
53
+ continue;
54
+ }
55
+ // Non-empty body: flag when it does not end in a terminating statement.
56
+ const last = clause.consequent.at(-1);
57
+ if (!TERMINATING_STATEMENTS.has(last.type)) {
58
+ context.report({ node: clause, messageId: 'bodyFallthrough' });
59
+ }
60
+ }
61
+ },
62
+ };
63
+ },
64
+ };
@@ -7,7 +7,6 @@
7
7
  * Auto-fix:
8
8
  * - let/const declarations → var
9
9
  * - nullish coalescing (??) → || (semantics differ for 0, '', false)
10
- * - direct object literal return → extract to variable
11
10
  */
12
11
 
13
12
  import { unsupportedByNodeType } from 'ssjs-data';
@@ -32,14 +31,6 @@ function nullishCoalescingFix(context, node) {
32
31
  };
33
32
  }
34
33
 
35
- function directObjectReturnFix(context, node) {
36
- return (fixer) => {
37
- const objectText = context.sourceCode.getText(node.argument);
38
- const indent = ' '.repeat(node.loc.start.column);
39
- return fixer.replaceText(node, `var _result = ${objectText};\n${indent}return _result;`);
40
- };
41
- }
42
-
43
34
  export default {
44
35
  meta: {
45
36
  type: 'problem',
@@ -96,8 +87,6 @@ export default {
96
87
  report.fix = AUTO_FIX[entry.feature](node);
97
88
  } else if (entry.feature === 'NullishCoalescing') {
98
89
  report.fix = nullishCoalescingFix(context, node);
99
- } else if (entry.feature === 'DirectObjectReturn') {
100
- report.fix = directObjectReturnFix(context, node);
101
90
  }
102
91
 
103
92
  context.report(report);