eslint-plugin-sfmc 5.0.0 → 5.2.0
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 +41 -24
- package/docs/rules/amp/arg-types.md +13 -2
- package/docs/rules/amp/prefer-boolean-literal.md +50 -0
- package/docs/rules/ssjs/no-switch-fallthrough.md +26 -0
- package/docs/rules/ssjs/no-unavailable-method.md +60 -36
- package/package.json +6 -4
- package/src/index.js +23 -1
- package/src/rules/amp/argument-types.js +49 -11
- package/src/rules/amp/prefer-boolean-literal.js +98 -0
- package/src/rules/ssjs/no-unavailable-method.js +2 -2
package/README.md
CHANGED
|
@@ -57,6 +57,19 @@ Add the following to your `.vscode/settings.json`:
|
|
|
57
57
|
|
|
58
58
|
> **Why `eslint.validate` and not `eslint.probe`?** `eslint.probe` silently skips files for language IDs that the ESLint extension does not natively recognise. `eslint.validate` forces the extension to process those files regardless of language ID.
|
|
59
59
|
|
|
60
|
+
## Diagnostic documentation links
|
|
61
|
+
|
|
62
|
+
SFMC rule metadata links to the owning plugin version's documentation under `blob/v<package-version>/docs/rules/`. These links also work with custom plugin namespaces. Local development uses the package version deterministically; links for a prospective release may not resolve until that version and its documentation are tagged and published. ESLint parser failures (`ruleId: null`) have no rule metadata link. Core and third-party rules retain their own documentation metadata.
|
|
63
|
+
|
|
64
|
+
### 5.2.0 release notes
|
|
65
|
+
|
|
66
|
+
- Added `sfmc/amp-prefer-boolean-literal` at `warn` severity in the recommended configs. It recognizes all eight accepted boolean-like forms and recommends bare `true` or `false`.
|
|
67
|
+
- Added safe autofixes and editor quick fixes for numeric and quoted boolean alternatives while leaving invalid values to `sfmc/amp-arg-types`.
|
|
68
|
+
- Updated `ampscript-data` to `^4.1.0` and `ssjs-data` to `^2.1.1`, including the complete boolean-like parameter metadata used by both AMPscript rules.
|
|
69
|
+
- No existing rule IDs, public configuration shapes, or supported inputs were removed or changed incompatibly.
|
|
70
|
+
|
|
71
|
+
Version **5.2.0** exports 53 SFMC rules. Every rule retains version-pinned documentation metadata, and release checks verify the tagged documentation and packed npm artifact.
|
|
72
|
+
|
|
60
73
|
## Configs
|
|
61
74
|
|
|
62
75
|
### Marketing Cloud Engagement (default)
|
|
@@ -96,30 +109,31 @@ export default [...sfmc.configs['recommended-next'], ...sfmc.configs['embedded-n
|
|
|
96
109
|
|
|
97
110
|
## AMPscript Rules (`amp-*`)
|
|
98
111
|
|
|
99
|
-
| Rule | Default | Description
|
|
100
|
-
| ------------------------------------------------------------------------------------------- | ------------------------ |
|
|
101
|
-
| [`sfmc/amp-no-unknown-function`](docs/rules/amp/no-unknown-function.md) | `error` | Disallow calls to unknown AMPscript functions
|
|
102
|
-
| [`sfmc/amp-no-mcn-unsupported`](docs/rules/amp/no-mcn-unsupported.md) | off (`error` in `-next`) | Flag AMPscript functions unavailable in the targeted MCN API version (`apiVersion`)
|
|
103
|
-
| [`sfmc/amp-function-arity`](docs/rules/amp/function-arity.md) | `error` | Enforce correct argument counts
|
|
104
|
-
| [`sfmc/amp-arg-types`](docs/rules/amp/arg-types.md) | `error` | Check that literal arguments match expected parameter types and allowed values
|
|
105
|
-
| [`sfmc/amp-
|
|
106
|
-
| [`sfmc/amp-
|
|
107
|
-
| [`sfmc/amp-no-
|
|
108
|
-
| [`sfmc/amp-no-
|
|
109
|
-
| [`sfmc/amp-no-
|
|
110
|
-
| [`sfmc/amp-no-
|
|
111
|
-
| [`sfmc/amp-no-
|
|
112
|
-
| [`sfmc/amp-no-
|
|
113
|
-
| [`sfmc/amp-
|
|
114
|
-
| [`sfmc/amp-
|
|
115
|
-
| [`sfmc/amp-
|
|
116
|
-
| [`sfmc/amp-
|
|
117
|
-
| [`sfmc/amp-no-
|
|
118
|
-
| [`sfmc/amp-no-
|
|
119
|
-
| [`sfmc/amp-no-nested-
|
|
120
|
-
| [`sfmc/amp-
|
|
121
|
-
| [`sfmc/amp-
|
|
122
|
-
| [`sfmc/amp-
|
|
112
|
+
| Rule | Default | Description |
|
|
113
|
+
| ------------------------------------------------------------------------------------------- | ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
114
|
+
| [`sfmc/amp-no-unknown-function`](docs/rules/amp/no-unknown-function.md) | `error` | Disallow calls to unknown AMPscript functions |
|
|
115
|
+
| [`sfmc/amp-no-mcn-unsupported`](docs/rules/amp/no-mcn-unsupported.md) | off (`error` in `-next`) | Flag AMPscript functions unavailable in the targeted MCN API version (`apiVersion`) |
|
|
116
|
+
| [`sfmc/amp-function-arity`](docs/rules/amp/function-arity.md) | `error` | Enforce correct argument counts |
|
|
117
|
+
| [`sfmc/amp-arg-types`](docs/rules/amp/arg-types.md) | `error` | Check that literal arguments match expected parameter types and allowed values |
|
|
118
|
+
| [`sfmc/amp-prefer-boolean-literal`](docs/rules/amp/prefer-boolean-literal.md) | `warn` | All catalogued boolean-like parameters accept exactly eight forms: bare `true`/`false`, numeric `1`/`0`, or quoted `"true"`/`"false"`/`"1"`/`"0"` (either quote style; words case-insensitive); recommends and auto-fixes to bare booleans |
|
|
119
|
+
| [`sfmc/amp-set-requires-target`](docs/rules/amp/set-requires-target.md) | `error` | Require `set` to have a target variable |
|
|
120
|
+
| [`sfmc/amp-no-smart-quotes`](docs/rules/amp/no-smart-quotes.md) | `error` | Disallow smart/curly quotes in strings |
|
|
121
|
+
| [`sfmc/amp-no-var-redeclaration`](docs/rules/amp/no-var-redeclaration.md) | `warn` | Disallow re-declaring a variable with `var` |
|
|
122
|
+
| [`sfmc/amp-no-empty-block`](docs/rules/amp/no-empty-block.md) | `warn` | Disallow empty `%%[ ]%%` blocks |
|
|
123
|
+
| [`sfmc/amp-no-loop-counter-assign`](docs/rules/amp/no-loop-counter-assign.md) | `warn` | Disallow assigning to the `for` loop counter |
|
|
124
|
+
| [`sfmc/amp-no-inline-statement`](docs/rules/amp/no-inline-statement.md) | `warn` | Disallow statements inside inline expressions |
|
|
125
|
+
| [`sfmc/amp-no-deprecated-function`](docs/rules/amp/no-deprecated-function.md) | `warn` | Flag deprecated functions and suggest replacements |
|
|
126
|
+
| [`sfmc/amp-no-nonfunctional-function`](docs/rules/amp/no-nonfunctional-function.md) | `error` | Flag functions that resolve but have no working runtime invocation |
|
|
127
|
+
| [`sfmc/amp-naming-convention`](docs/rules/amp/naming-convention.md) | `warn` | Enforce variable naming convention |
|
|
128
|
+
| [`sfmc/amp-no-empty-then`](docs/rules/amp/no-empty-then.md) | `warn` | Disallow IF with empty THEN branch |
|
|
129
|
+
| [`sfmc/amp-require-rowcount-check`](docs/rules/amp/require-rowcount-check.md) | `warn` | Require RowCount check before FOR on LookupRows |
|
|
130
|
+
| [`sfmc/amp-no-html-comment`](docs/rules/amp/no-html-comment.md) | `warn` | Disallow HTML comments inside AMPscript blocks |
|
|
131
|
+
| [`sfmc/amp-no-js-line-comment`](docs/rules/amp/no-js-line-comment.md) | `warn` | Disallow JS-style `//` line comments in AMPscript |
|
|
132
|
+
| [`sfmc/amp-no-nested-script-tag`](docs/rules/amp/no-nested-script-tag.md) | `error` | Disallow `<script>` tags nested inside AMPscript script tags |
|
|
133
|
+
| [`sfmc/amp-no-nested-ampscript-delimiter`](docs/rules/amp/no-nested-ampscript-delimiter.md) | `error` | Disallow AMPscript delimiters nested inside AMPscript blocks |
|
|
134
|
+
| [`sfmc/amp-prefer-attribute-value`](docs/rules/amp/prefer-attribute-value.md) | off | Prefer `AttributeValue()` over bare personalization |
|
|
135
|
+
| [`sfmc/amp-require-variable-declaration`](docs/rules/amp/require-variable-declaration.md) | off | Require `var` before `set` |
|
|
136
|
+
| [`sfmc/amp-no-email-excluded-function`](docs/rules/amp/no-email-excluded-function.md) | off | Flag functions unavailable in email context |
|
|
123
137
|
|
|
124
138
|
## SSJS Rules (`ssjs-*`)
|
|
125
139
|
|
|
@@ -145,9 +159,12 @@ export default [...sfmc.configs['recommended-next'], ...sfmc.configs['embedded-n
|
|
|
145
159
|
| [`sfmc/ssjs-no-nonfunctional-method`](docs/rules/ssjs/no-nonfunctional-method.md) | `warn` | Flag Core Library methods that never take effect at runtime |
|
|
146
160
|
| [`sfmc/ssjs-prefer-parsejson-safe-arg`](docs/rules/ssjs/prefer-parsejson-safe-arg.md) | `warn` | Require string coercion on `ParseJSON` argument |
|
|
147
161
|
| [`sfmc/ssjs-no-switch-default`](docs/rules/ssjs/no-switch-default.md) | `warn` | Disallow `default` clause in `switch` statements |
|
|
162
|
+
| [`sfmc/ssjs-no-switch-fallthrough`](docs/rules/ssjs/no-switch-fallthrough.md) | `warn` | Flag stacked labels and unterminated clauses that rely on switch fall-through |
|
|
148
163
|
| [`sfmc/ssjs-no-treatascontent-injection`](docs/rules/ssjs/no-treatascontent-injection.md) | `warn` | Flag dynamic string concatenation in `TreatAsContent` calls |
|
|
149
164
|
| [`sfmc/ssjs-core-method-arity`](docs/rules/ssjs/core-method-arity.md) | `warn` | Enforce correct argument counts for Core Library object methods |
|
|
150
165
|
| [`sfmc/ssjs-arg-types`](docs/rules/ssjs/arg-types.md) | `warn` | Check that literal arguments match expected parameter types |
|
|
166
|
+
| [`sfmc/ssjs-http-property-value`](docs/rules/ssjs/http-property-value.md) | `error` | Validate literal HTTP request property values |
|
|
167
|
+
| [`sfmc/ssjs-no-invalid-property-access`](docs/rules/ssjs/no-invalid-property-access.md) | `error` | Flag reads of write-only properties and writes to read-only properties |
|
|
151
168
|
|
|
152
169
|
## Handlebars Rules (`hbs-*`)
|
|
153
170
|
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
|
|
13
13
|
Some AMPscript function parameters only accept a fixed set of keyword values. For example, the second argument of `DatePart` must be one of `year`, `Y`, `month`, `M`, `monthName`, `day`, `D`, `hour`, `H`, `minute`, or `MI`. Passing any other value is a bug that surfaces only at send/render time.
|
|
14
14
|
|
|
15
|
-
The Salesforce catalog records these allowed values as an `enum` on the parameter. This rule checks every static literal argument — strings, numbers, and booleans — against the parameter's `enum`
|
|
15
|
+
The Salesforce catalog records these allowed values as an `enum` on the parameter. This rule checks every static literal argument — strings, numbers, and booleans — against the parameter's `enum` and reports a mismatch. Literal primitive types remain distinct: `1` does not match `"1"`, and `true` does not match `"true"` unless both forms are explicitly present in the enum. String-to-string comparisons are case-insensitive. Variables and expressions are skipped because their value cannot be determined statically.
|
|
16
16
|
|
|
17
17
|
This rule is the AMPscript counterpart of [`sfmc/ssjs-arg-types`](../ssjs/arg-types.md) and may be expanded later to cover additional argument-type checks.
|
|
18
18
|
|
|
@@ -35,6 +35,10 @@ This rule has no configuration options.
|
|
|
35
35
|
|
|
36
36
|
/* a number is not a valid datePart either */
|
|
37
37
|
set @y = DatePart('2026-01-15', 5)
|
|
38
|
+
|
|
39
|
+
/* unsupported boolean-like values are rejected */
|
|
40
|
+
RaiseError('stop', false, '', 0, 2)
|
|
41
|
+
RaiseError('stop', false, '', 0, 'yes')
|
|
38
42
|
]%%
|
|
39
43
|
```
|
|
40
44
|
|
|
@@ -45,9 +49,16 @@ This rule has no configuration options.
|
|
|
45
49
|
/* exact-case enum value */
|
|
46
50
|
set @x = DatePart('2026-01-15', 'Y')
|
|
47
51
|
|
|
48
|
-
/* case-insensitive match */
|
|
52
|
+
/* case-insensitive string match */
|
|
49
53
|
set @y = DatePart('2026-01-15', 'year')
|
|
50
54
|
|
|
55
|
+
/* mixed enums can explicitly allow several primitive forms */
|
|
56
|
+
RaiseError('stop', false, '', 0, true)
|
|
57
|
+
RaiseError('stop', false, '', 0, 1)
|
|
58
|
+
RaiseError('stop', false, '', 0, 'TRUE')
|
|
59
|
+
RaiseError('stop', false, '', 0, '1')
|
|
60
|
+
RaiseError('stop', false, '', 0, '0')
|
|
61
|
+
|
|
51
62
|
/* variable argument — not statically checkable, skipped */
|
|
52
63
|
set @z = DatePart(@d, @part)
|
|
53
64
|
]%%
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# `sfmc/amp-prefer-boolean-literal`
|
|
2
|
+
|
|
3
|
+
> Recommend bare boolean literals for AMPscript parameters that also accept numeric or quoted alternatives.
|
|
4
|
+
|
|
5
|
+
| | |
|
|
6
|
+
|---|---|
|
|
7
|
+
| **Type** | `suggestion` |
|
|
8
|
+
| **Default severity** | `warn` in `recommended` and `strict` |
|
|
9
|
+
| **Fixable** | Yes — `eslint --fix` or editor quick fix |
|
|
10
|
+
|
|
11
|
+
## Why This Rule Exists
|
|
12
|
+
|
|
13
|
+
All catalogued boolean-like AMPscript parameters accept the same eight values: bare `true` and `false`; numeric `1` and `0`; and quoted `'true'`, `'false'`, `'1'`, and `'0'` (with either quote style and case-insensitive word matching). All eight are valid, so `sfmc/amp-arg-types` accepts them. This separate style rule recommends the clearer bare `true` or `false` form without describing the alternatives as invalid syntax.
|
|
14
|
+
|
|
15
|
+
The rule derives its scope from `ampscript-data` metadata and checks every catalogued boolean-like parameter. Each discouraged static alternative is safely auto-fixed by replacing only that argument with bare `true` or `false`; surrounding code and formatting remain unchanged. Apply fixes with `eslint --fix` or an editor's ESLint quick fix.
|
|
16
|
+
|
|
17
|
+
## Settings
|
|
18
|
+
|
|
19
|
+
| Setting | Values | Default |
|
|
20
|
+
|---------|--------|---------|
|
|
21
|
+
| severity | `"error"` \| `"warn"` \| `"off"` | `"warn"` |
|
|
22
|
+
|
|
23
|
+
This rule has no configuration options.
|
|
24
|
+
|
|
25
|
+
### Recommended
|
|
26
|
+
|
|
27
|
+
```ampscript
|
|
28
|
+
%%[
|
|
29
|
+
RaiseError('stop', false, '', 0, true)
|
|
30
|
+
RaiseError('stop', false, '', 0, false)
|
|
31
|
+
]%%
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### Produces a recommendation warning
|
|
35
|
+
|
|
36
|
+
```ampscript
|
|
37
|
+
%%[
|
|
38
|
+
RaiseError('stop', false, '', 0, 1)
|
|
39
|
+
RaiseError('stop', false, '', 0, 'FALSE')
|
|
40
|
+
]%%
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Invalid values such as `2` or `'yes'` are not reported by this rule. They remain errors from `sfmc/amp-arg-types`.
|
|
44
|
+
|
|
45
|
+
## When to Disable
|
|
46
|
+
|
|
47
|
+
```js
|
|
48
|
+
// eslint.config.js
|
|
49
|
+
rules: { 'sfmc/amp-prefer-boolean-literal': 'off' }
|
|
50
|
+
```
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# `sfmc/ssjs-no-switch-fallthrough`
|
|
2
|
+
|
|
3
|
+
Flag switch clauses that appear to depend on execution continuing into the next clause.
|
|
4
|
+
|
|
5
|
+
## Why
|
|
6
|
+
|
|
7
|
+
Marketing Cloud Engagement's SSJS engine does not implement JavaScript switch fall-through. Stacked labels therefore do not share the following body, and an unterminated case does not execute the next case's statements. Code that assumes either behavior can silently skip work.
|
|
8
|
+
|
|
9
|
+
## What this rule reports
|
|
10
|
+
|
|
11
|
+
- An empty case or default clause before another clause: for example, two adjacent case labels with a single shared body.
|
|
12
|
+
- A non-final clause whose last statement is not a break, return, throw, or continue: for example, an assignment followed immediately by the next case label.
|
|
13
|
+
|
|
14
|
+
The final clause is not reported because there is no subsequent clause to enter. A non-final clause ending directly in a terminating statement is accepted. This is a structural check, not control-flow analysis: a clause ending in an if statement can still be reported even when all its branches return.
|
|
15
|
+
|
|
16
|
+
## How to fix
|
|
17
|
+
|
|
18
|
+
Use an if/else chain when multiple values should perform the same work. Alternatively, give each case its own explicit body and terminator rather than stacking labels or expecting execution to cascade. A final clause needs no fall-through correction.
|
|
19
|
+
|
|
20
|
+
Adding break makes intent explicit, but does not repair code that actually needs the next case's work. Move that shared work into an explicit shared path instead. Also consider the separate function-scoped switch/break engine hazard described by [no-switch-default](no-switch-default.md); an if/else chain avoids both issues.
|
|
21
|
+
|
|
22
|
+
## Configuration and applicability
|
|
23
|
+
|
|
24
|
+
This rule has no options or automatic fix. It is enabled at warning severity in the Engagement recommended and embedded configurations, and error severity in strict configurations. It applies to standalone and HTML-embedded SSJS. Next configurations disable this rule and report SSJS as unsupported instead.
|
|
25
|
+
|
|
26
|
+
To suppress an intentional empty or unterminated clause, configure `sfmc/ssjs-no-switch-fallthrough` as `off`, or use a narrowly scoped ESLint disable-next-line comment naming this rule and explaining why the clause does not depend on fall-through. Replace `sfmc` with your configured plugin namespace if using an alias.
|
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
> Flag Array methods that are unavailable or broken in SFMC SSJS and suggest inserting a polyfill.
|
|
4
4
|
|
|
5
|
-
|
|
|
6
|
-
|
|
7
|
-
| **Type**
|
|
8
|
-
| **Default severity** | `warn` in `recommended` and `strict`
|
|
9
|
-
| **Fixable**
|
|
5
|
+
| | |
|
|
6
|
+
| -------------------- | ----------------------------------------------------- |
|
|
7
|
+
| **Type** | `problem` |
|
|
8
|
+
| **Default severity** | `warn` in `recommended` and `strict` |
|
|
9
|
+
| **Fixable** | **Suggestion** only (lightbulb / `Ctrl+.` in VS Code) |
|
|
10
10
|
|
|
11
11
|
## Why This Rule Exists
|
|
12
12
|
|
|
@@ -26,32 +26,46 @@ the language-server quick-fix so both tools place polyfills consistently.
|
|
|
26
26
|
|
|
27
27
|
## Covered methods
|
|
28
28
|
|
|
29
|
-
| Method
|
|
30
|
-
|
|
31
|
-
| `Array.prototype.copyWithin`
|
|
32
|
-
| `Array.prototype.entries`
|
|
33
|
-
| `Array.prototype.fill`
|
|
34
|
-
| `Array.prototype.filter`
|
|
35
|
-
| `Array.prototype.find`
|
|
36
|
-
| `Array.prototype.findIndex`
|
|
37
|
-
| `Array.prototype.forEach`
|
|
38
|
-
| `Array.prototype.includes`
|
|
39
|
-
| `Array.prototype.indexOf`
|
|
40
|
-
| `Array.prototype.lastIndexOf` | broken
|
|
41
|
-
| `Array.prototype.map`
|
|
42
|
-
| `Array.prototype.reduce`
|
|
43
|
-
| `Array.prototype.reduceRight` | unavailable |
|
|
44
|
-
| `Array.prototype.some`
|
|
45
|
-
| `Array.prototype.splice`
|
|
46
|
-
| `Array.isArray`
|
|
47
|
-
| `Array.of`
|
|
29
|
+
| Method | Category | Notes |
|
|
30
|
+
| ----------------------------- | ----------- | --------------------------------------------------------------- |
|
|
31
|
+
| `Array.prototype.copyWithin` | unavailable | |
|
|
32
|
+
| `Array.prototype.entries` | unavailable | Returns a minimal iterator; `for...of` is also unsupported |
|
|
33
|
+
| `Array.prototype.fill` | unavailable | |
|
|
34
|
+
| `Array.prototype.filter` | unavailable | |
|
|
35
|
+
| `Array.prototype.find` | unavailable | |
|
|
36
|
+
| `Array.prototype.findIndex` | unavailable | |
|
|
37
|
+
| `Array.prototype.forEach` | unavailable | |
|
|
38
|
+
| `Array.prototype.includes` | unavailable | |
|
|
39
|
+
| `Array.prototype.indexOf` | unavailable | Only flagged on array literals; string `.indexOf()` still works |
|
|
40
|
+
| `Array.prototype.lastIndexOf` | broken | Natively always returns `-1` |
|
|
41
|
+
| `Array.prototype.map` | unavailable | |
|
|
42
|
+
| `Array.prototype.reduce` | unavailable | |
|
|
43
|
+
| `Array.prototype.reduceRight` | unavailable | |
|
|
44
|
+
| `Array.prototype.some` | unavailable | |
|
|
45
|
+
| `Array.prototype.splice` | broken | Natively ignores `startIndex` and `deleteCount` |
|
|
46
|
+
| `Array.isArray` | unavailable | |
|
|
47
|
+
| `Array.of` | unavailable | |
|
|
48
|
+
|
|
49
|
+
## Catalog entries without bundled polyfills
|
|
50
|
+
|
|
51
|
+
The rule also reads `KNOWN_UNSUPPORTED` from `ssjs-data`. These reports carry the
|
|
52
|
+
catalog's guidance but no automatic fix or polyfill insertion suggestion. Absence
|
|
53
|
+
of a bundled verified polyfill does not mean a polyfill is impossible.
|
|
54
|
+
|
|
55
|
+
With `ssjs-data` 2.1.0, this includes `Object.fromEntries`, `Object.groupBy`,
|
|
56
|
+
`Array.prototype.toReversed`, `toSorted`, `toSpliced`, `findLastIndex`, and
|
|
57
|
+
`String.prototype.replaceAll` and `matchAll`. Their absence was checked in
|
|
58
|
+
Engagement CloudPages before Core loading and with Core 1.1.1 and 1.1.5; email and
|
|
59
|
+
other contexts were not tested. The catalog also records global `structuredClone`,
|
|
60
|
+
but this rule examines member calls, not bare global calls, so it does not report
|
|
61
|
+
`structuredClone()`.
|
|
48
62
|
|
|
49
63
|
## Settings
|
|
50
64
|
|
|
51
|
-
| Setting
|
|
52
|
-
|
|
53
|
-
| severity | `"error"` \| `"warn"` \| `"off"`
|
|
54
|
-
| `ignore` | Array of method names to suppress | `[]`
|
|
65
|
+
| Setting | Values | Default |
|
|
66
|
+
| -------- | --------------------------------- | -------- |
|
|
67
|
+
| severity | `"error"` \| `"warn"` \| `"off"` | `"warn"` |
|
|
68
|
+
| `ignore` | Array of method names to suppress | `[]` |
|
|
55
69
|
|
|
56
70
|
Use `ignore` when polyfills are loaded externally via a Content Block or Code Resource
|
|
57
71
|
that runs before your script.
|
|
@@ -69,9 +83,11 @@ rules: {
|
|
|
69
83
|
|
|
70
84
|
```js
|
|
71
85
|
var nums = [1, 2, 3];
|
|
72
|
-
var doubled = nums.map(function (n) {
|
|
86
|
+
var doubled = nums.map(function (n) {
|
|
87
|
+
return n * 2;
|
|
88
|
+
}); // unavailable
|
|
73
89
|
|
|
74
|
-
var pos = nums.lastIndexOf(2);
|
|
90
|
+
var pos = nums.lastIndexOf(2); // broken — always returns -1 natively
|
|
75
91
|
|
|
76
92
|
var found = Array.isArray(nums); // unavailable
|
|
77
93
|
```
|
|
@@ -80,9 +96,13 @@ var found = Array.isArray(nums); // unavailable
|
|
|
80
96
|
|
|
81
97
|
```js
|
|
82
98
|
// Polyfill defined in the same file — rule is suppressed
|
|
83
|
-
Array.prototype.map = function (callback) {
|
|
99
|
+
Array.prototype.map = function (callback) {
|
|
100
|
+
/* ... */
|
|
101
|
+
};
|
|
84
102
|
|
|
85
|
-
var doubled = nums.map(function (n) {
|
|
103
|
+
var doubled = nums.map(function (n) {
|
|
104
|
+
return n * 2;
|
|
105
|
+
}); // ok
|
|
86
106
|
|
|
87
107
|
// Native methods that work correctly are never flagged
|
|
88
108
|
nums.push(4);
|
|
@@ -102,14 +122,18 @@ rules: {
|
|
|
102
122
|
|
|
103
123
|
```js
|
|
104
124
|
// No warning — 'map' and 'filter' are in the ignore list
|
|
105
|
-
var doubled = nums.map(function (n) {
|
|
106
|
-
|
|
125
|
+
var doubled = nums.map(function (n) {
|
|
126
|
+
return n * 2;
|
|
127
|
+
});
|
|
128
|
+
var evens = nums.filter(function (n) {
|
|
129
|
+
return n % 2 === 0;
|
|
130
|
+
});
|
|
107
131
|
```
|
|
108
132
|
|
|
109
133
|
## Suggestion (lightbulb)
|
|
110
134
|
|
|
111
|
-
When a
|
|
112
|
-
|
|
135
|
+
When a bundled verified polyfill exists, a suggestion is available to insert it
|
|
136
|
+
at the **top of the file**, after a leading global directive if present. Apply it via:
|
|
113
137
|
|
|
114
138
|
- Click the **lightbulb** or press `Ctrl+.` in VS Code (requires the ESLint extension)
|
|
115
139
|
- `eslint --fix` does **not** apply suggestions
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-sfmc",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.2.0",
|
|
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,19 +36,21 @@
|
|
|
36
36
|
"mcn"
|
|
37
37
|
],
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"ampscript-data": "^4.
|
|
39
|
+
"ampscript-data": "^4.1.0",
|
|
40
40
|
"ampscript-parser": "^0.3.1",
|
|
41
41
|
"eslint-plugin-mso-email": "^2.0.0",
|
|
42
42
|
"handlebars-data": "^0.4.0",
|
|
43
43
|
"mso-conditional-parser": "^2.0.1",
|
|
44
44
|
"sfmc-handlebars-parser": "^0.1.1",
|
|
45
|
-
"ssjs-data": "^2.
|
|
45
|
+
"ssjs-data": "^2.1.1"
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {
|
|
48
48
|
"eslint": ">=10.4.0"
|
|
49
49
|
},
|
|
50
50
|
"scripts": {
|
|
51
|
-
"test": "node --test tests/rules.test.js && node --test tests/deprecated-catalog.test.js && node --test tests/fixtures.test.mjs && node --test tests/autofix-manual.test.mjs && node --test tests/unicorn-protection.test.js tests/unicorn-hazards.test.js tests/unicorn-configs.test.js && node --test tests/unicorn-minimum.test.js",
|
|
51
|
+
"test": "node --test tests/release-safeguards.test.js tests/metadata.test.js && node --test tests/rules.test.js && node --test tests/deprecated-catalog.test.js && node --test tests/fixtures.test.mjs && node --test tests/autofix-manual.test.mjs && node --test tests/unicorn-protection.test.js tests/unicorn-hazards.test.js tests/unicorn-configs.test.js && node --test tests/unicorn-minimum.test.js",
|
|
52
|
+
"test:release": "node --test tests/release-safeguards.test.js",
|
|
53
|
+
"validate:release-docs": "node scripts/validate-release-documentation.js",
|
|
52
54
|
"test:autofix-manual": "node --test tests/autofix-manual.test.mjs",
|
|
53
55
|
"test:fixtures": "node --test tests/fixtures.test.mjs",
|
|
54
56
|
"test:fixtures:update": "node --test tests/fixtures.test.mjs -- --update-snapshots",
|
package/src/index.js
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
* - vscode-sfmc-language (IntelliSense, diagnostics)
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
+
import { createRequire } from 'node:module';
|
|
12
13
|
import * as ampscriptParser from './ampscript-parser.js';
|
|
13
14
|
import * as handlebarsParser from './handlebars-parser.js';
|
|
14
15
|
|
|
@@ -26,6 +27,7 @@ import ampNoInlineStatement from './rules/amp/no-inline-statement.js';
|
|
|
26
27
|
import ampRequireVariableDeclaration from './rules/amp/require-variable-declaration.js';
|
|
27
28
|
import ampFunctionArity from './rules/amp/function-arity.js';
|
|
28
29
|
import ampArgumentTypes from './rules/amp/argument-types.js';
|
|
30
|
+
import ampPreferBooleanLiteral from './rules/amp/prefer-boolean-literal.js';
|
|
29
31
|
import ampNoEmailExcludedFunction from './rules/amp/no-email-excluded-function.js';
|
|
30
32
|
import ampNoDeprecatedFunction from './rules/amp/no-deprecated-function.js';
|
|
31
33
|
import ampNoNonfunctionalFunction from './rules/amp/no-nonfunctional-function.js';
|
|
@@ -94,10 +96,12 @@ import { SSJS_GLOBALS_MAP } from 'ssjs-data';
|
|
|
94
96
|
|
|
95
97
|
// ── Plugin definition ─────────────────────────────────────────────────────────
|
|
96
98
|
|
|
99
|
+
const { version } = createRequire(import.meta.url)('../package.json');
|
|
100
|
+
|
|
97
101
|
const plugin = {
|
|
98
102
|
meta: {
|
|
99
103
|
name: 'eslint-plugin-sfmc',
|
|
100
|
-
version
|
|
104
|
+
version,
|
|
101
105
|
},
|
|
102
106
|
|
|
103
107
|
rules: {
|
|
@@ -114,6 +118,7 @@ const plugin = {
|
|
|
114
118
|
'amp-require-variable-declaration': ampRequireVariableDeclaration,
|
|
115
119
|
'amp-function-arity': ampFunctionArity,
|
|
116
120
|
'amp-arg-types': ampArgumentTypes,
|
|
121
|
+
'amp-prefer-boolean-literal': ampPreferBooleanLiteral,
|
|
117
122
|
'amp-no-email-excluded-function': ampNoEmailExcludedFunction,
|
|
118
123
|
'amp-no-deprecated-function': ampNoDeprecatedFunction,
|
|
119
124
|
'amp-no-nonfunctional-function': ampNoNonfunctionalFunction,
|
|
@@ -167,6 +172,21 @@ const plugin = {
|
|
|
167
172
|
},
|
|
168
173
|
};
|
|
169
174
|
|
|
175
|
+
// Derive paths from public IDs, not source filenames or the configured namespace.
|
|
176
|
+
for (const [ruleId, rule] of Object.entries(plugin.rules)) {
|
|
177
|
+
const documentationPath = ruleId.replace('-', '/');
|
|
178
|
+
plugin.rules[ruleId] = {
|
|
179
|
+
...rule,
|
|
180
|
+
meta: {
|
|
181
|
+
...rule.meta,
|
|
182
|
+
docs: {
|
|
183
|
+
...rule.meta.docs,
|
|
184
|
+
url: `https://github.com/JoernBerkefeld/eslint-plugin-sfmc/blob/v${version}/docs/rules/${documentationPath}.md`,
|
|
185
|
+
},
|
|
186
|
+
},
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|
|
170
190
|
// ── MCN SSJS rule set (all SSJS rules off except ssjs-no-unknown-function) ────
|
|
171
191
|
|
|
172
192
|
/**
|
|
@@ -216,6 +236,7 @@ const ampRecommendedRules = {
|
|
|
216
236
|
'sfmc/amp-no-inline-statement': 'warn',
|
|
217
237
|
'sfmc/amp-function-arity': 'error',
|
|
218
238
|
'sfmc/amp-arg-types': 'error',
|
|
239
|
+
'sfmc/amp-prefer-boolean-literal': 'warn',
|
|
219
240
|
'sfmc/amp-no-deprecated-function': 'warn',
|
|
220
241
|
'sfmc/amp-no-nonfunctional-function': 'error',
|
|
221
242
|
'sfmc/amp-naming-convention': 'warn',
|
|
@@ -239,6 +260,7 @@ const ampStrictRules = {
|
|
|
239
260
|
'sfmc/amp-require-variable-declaration': 'warn',
|
|
240
261
|
'sfmc/amp-function-arity': 'error',
|
|
241
262
|
'sfmc/amp-arg-types': 'error',
|
|
263
|
+
'sfmc/amp-prefer-boolean-literal': 'warn',
|
|
242
264
|
'sfmc/amp-no-email-excluded-function': 'off',
|
|
243
265
|
'sfmc/amp-no-deprecated-function': 'warn',
|
|
244
266
|
'sfmc/amp-no-nonfunctional-function': 'error',
|
|
@@ -4,9 +4,10 @@
|
|
|
4
4
|
* Validates literal arguments passed to AMPscript functions against the
|
|
5
5
|
* constraints declared in the ampscript-data catalog. Currently it checks
|
|
6
6
|
* parameters that declare an `enum` of allowed values: a static literal
|
|
7
|
-
* argument (string, number, or boolean) must be one of those values
|
|
8
|
-
*
|
|
9
|
-
* value cannot be
|
|
7
|
+
* argument (string, number, or boolean) must be one of those values. Primitive
|
|
8
|
+
* types are preserved; string values compare case-insensitively only with other
|
|
9
|
+
* strings. Variables and expressions are skipped because their value cannot be
|
|
10
|
+
* determined statically.
|
|
10
11
|
*
|
|
11
12
|
* This rule is the AMPscript counterpart of `ssjs-arg-types` and may be
|
|
12
13
|
* expanded later to cover additional argument-type checks.
|
|
@@ -20,21 +21,56 @@ import { functionLookup } from 'ampscript-data';
|
|
|
20
21
|
const STATIC_LITERAL_TYPES = new Set(['StringLiteral', 'NumberLiteral', 'BooleanLiteral']);
|
|
21
22
|
|
|
22
23
|
/**
|
|
23
|
-
* Returns the
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* enum.
|
|
24
|
+
* Returns the primitive value of a static AMPscript literal node, or null when
|
|
25
|
+
* the argument is not a static literal (e.g. a variable or expression) and
|
|
26
|
+
* cannot be validated against an enum.
|
|
27
27
|
*
|
|
28
28
|
* @param {object} argument - AMPscript argument AST node.
|
|
29
|
-
* @returns {string | null} The literal value
|
|
29
|
+
* @returns {string | number | boolean | null} The type-preserved literal value, or null.
|
|
30
30
|
*/
|
|
31
31
|
function staticLiteralValue(argument) {
|
|
32
32
|
if (!argument || !STATIC_LITERAL_TYPES.has(argument.type)) {
|
|
33
33
|
return null;
|
|
34
34
|
}
|
|
35
|
+
if (argument.type === 'NumberLiteral') {
|
|
36
|
+
return Number(argument.value);
|
|
37
|
+
}
|
|
38
|
+
if (argument.type === 'BooleanLiteral') {
|
|
39
|
+
return String(argument.value).toLowerCase() === 'true';
|
|
40
|
+
}
|
|
35
41
|
return String(argument.value);
|
|
36
42
|
}
|
|
37
43
|
|
|
44
|
+
/**
|
|
45
|
+
* Compare a static literal with one catalog enum member without collapsing
|
|
46
|
+
* strings, numbers, and booleans into the same textual value. String matching
|
|
47
|
+
* remains case-insensitive.
|
|
48
|
+
*
|
|
49
|
+
* @param {string | number | boolean} allowed - Catalog enum member.
|
|
50
|
+
* @param {string | number | boolean} actual - Static AMPscript literal value.
|
|
51
|
+
* @returns {boolean} Whether the values match with type-sensitive semantics.
|
|
52
|
+
*/
|
|
53
|
+
function enumValueMatches(allowed, actual) {
|
|
54
|
+
if (typeof allowed !== typeof actual) {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
if (typeof allowed === 'string' && typeof actual === 'string') {
|
|
58
|
+
return allowed.toLowerCase() === actual.toLowerCase();
|
|
59
|
+
}
|
|
60
|
+
return allowed === actual;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Format a catalog enum member as an AMPscript literal so string members are
|
|
65
|
+
* visibly distinct from number and boolean members in diagnostics.
|
|
66
|
+
*
|
|
67
|
+
* @param {string | number | boolean} value - Catalog enum member.
|
|
68
|
+
* @returns {string} AMPscript literal text.
|
|
69
|
+
*/
|
|
70
|
+
function formatEnumLiteral(value) {
|
|
71
|
+
return typeof value === 'string' ? `"${value}"` : String(value);
|
|
72
|
+
}
|
|
73
|
+
|
|
38
74
|
export default {
|
|
39
75
|
meta: {
|
|
40
76
|
type: 'suggestion',
|
|
@@ -73,8 +109,8 @@ export default {
|
|
|
73
109
|
if (actual === null) {
|
|
74
110
|
continue;
|
|
75
111
|
}
|
|
76
|
-
const isAllowed = parameter.enum.some(
|
|
77
|
-
(
|
|
112
|
+
const isAllowed = parameter.enum.some((value) =>
|
|
113
|
+
enumValueMatches(value, actual),
|
|
78
114
|
);
|
|
79
115
|
if (!isAllowed) {
|
|
80
116
|
context.report({
|
|
@@ -83,7 +119,9 @@ export default {
|
|
|
83
119
|
data: {
|
|
84
120
|
name: entry.name,
|
|
85
121
|
param: parameter.name,
|
|
86
|
-
allowed: parameter.enum
|
|
122
|
+
allowed: parameter.enum
|
|
123
|
+
.map((value) => formatEnumLiteral(value))
|
|
124
|
+
.join(', '),
|
|
87
125
|
actual,
|
|
88
126
|
},
|
|
89
127
|
});
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rule: prefer-boolean-literal
|
|
3
|
+
*
|
|
4
|
+
* Recommends and safely fixes bare boolean literals for every catalogued
|
|
5
|
+
* boolean-like parameter, all of which accept the same eight static forms.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { functionLookup } from 'ampscript-data';
|
|
9
|
+
|
|
10
|
+
const BOOLEAN_LIKE_ENUM = [true, false, 1, 0, 'true', 'false', '1', '0'];
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Return the preferred boolean for an accepted alternative literal.
|
|
14
|
+
*
|
|
15
|
+
* @param {object} argument - AMPscript argument AST node.
|
|
16
|
+
* @returns {boolean | null} Preferred boolean, or null when no recommendation applies.
|
|
17
|
+
*/
|
|
18
|
+
function preferredBoolean(argument) {
|
|
19
|
+
if (argument?.type === 'NumberLiteral') {
|
|
20
|
+
const value = Number(argument.value);
|
|
21
|
+
return value === 1 ? true : value === 0 ? false : null;
|
|
22
|
+
}
|
|
23
|
+
if (argument?.type === 'StringLiteral') {
|
|
24
|
+
const value = String(argument.value).toLowerCase();
|
|
25
|
+
if (value === 'true' || value === '1') {
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
if (value === 'false' || value === '0') {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Check whether catalog metadata marks a parameter as boolean-like.
|
|
37
|
+
*
|
|
38
|
+
* @param {object} parameter - AMPscript catalog parameter.
|
|
39
|
+
* @returns {boolean} Whether the enum contains exactly the complete eight-value set.
|
|
40
|
+
*/
|
|
41
|
+
export function isBooleanLikeParameter(parameter) {
|
|
42
|
+
if (!Array.isArray(parameter?.enum) || parameter.enum.length !== BOOLEAN_LIKE_ENUM.length) {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
return BOOLEAN_LIKE_ENUM.every((value) => parameter.enum.includes(value));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export default {
|
|
49
|
+
meta: {
|
|
50
|
+
type: 'suggestion',
|
|
51
|
+
docs: {
|
|
52
|
+
description:
|
|
53
|
+
'Auto-fix accepted boolean-like AMPscript alternatives to recommended bare booleans',
|
|
54
|
+
recommended: true,
|
|
55
|
+
},
|
|
56
|
+
fixable: 'code',
|
|
57
|
+
messages: {
|
|
58
|
+
preferBooleanLiteral:
|
|
59
|
+
"Use bare {{preferred}} for boolean-like argument '{{param}}' of '{{name}}' instead of {{actual}}.",
|
|
60
|
+
},
|
|
61
|
+
schema: [],
|
|
62
|
+
},
|
|
63
|
+
|
|
64
|
+
create(context) {
|
|
65
|
+
return {
|
|
66
|
+
FunctionCall(node) {
|
|
67
|
+
const entry = functionLookup.get(node.name.toLowerCase());
|
|
68
|
+
if (!entry || !Array.isArray(entry.params)) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
for (const [index, argument] of node.arguments.entries()) {
|
|
73
|
+
const parameter = entry.params[index];
|
|
74
|
+
if (!isBooleanLikeParameter(parameter)) {
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
const preferred = preferredBoolean(argument);
|
|
78
|
+
if (preferred === null) {
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
context.report({
|
|
82
|
+
node: argument,
|
|
83
|
+
messageId: 'preferBooleanLiteral',
|
|
84
|
+
data: {
|
|
85
|
+
name: entry.name,
|
|
86
|
+
param: parameter.name,
|
|
87
|
+
preferred: String(preferred),
|
|
88
|
+
actual: context.sourceCode.getText(argument),
|
|
89
|
+
},
|
|
90
|
+
fix(fixer) {
|
|
91
|
+
return fixer.replaceText(argument, String(preferred));
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
},
|
|
98
|
+
};
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
*
|
|
9
9
|
* 1. POLYFILLABLE_METHODS — a shipped ES3-safe polyfill exists. The report
|
|
10
10
|
* carries a suggestion that inserts the polyfill at the top of the file.
|
|
11
|
-
* 2. KNOWN_UNSUPPORTED — no polyfill is
|
|
11
|
+
* 2. KNOWN_UNSUPPORTED — no verified polyfill is bundled. The report has no fix; the
|
|
12
12
|
* message carries the ssjs-data `suggestion` (e.g. use Platform.Function.X).
|
|
13
13
|
*
|
|
14
14
|
* - category 'unavailable': method does not exist; calling it throws a runtime error.
|
|
@@ -250,7 +250,7 @@ export default {
|
|
|
250
250
|
const ownerDisplay = entry.owner.replace(/\.prototype$/, '');
|
|
251
251
|
|
|
252
252
|
if (noPolyfill) {
|
|
253
|
-
// No polyfill is
|
|
253
|
+
// No verified polyfill is bundled — report with the ssjs-data suggestion
|
|
254
254
|
// and no auto-insert fix.
|
|
255
255
|
context.report({
|
|
256
256
|
node,
|