eslint-plugin-sfmc 5.1.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
CHANGED
|
@@ -61,13 +61,14 @@ Add the following to your `.vscode/settings.json`:
|
|
|
61
61
|
|
|
62
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
63
|
|
|
64
|
-
### 5.
|
|
64
|
+
### 5.2.0 release notes
|
|
65
65
|
|
|
66
|
-
-
|
|
67
|
-
- Added
|
|
68
|
-
-
|
|
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.
|
|
69
70
|
|
|
70
|
-
Version **5.
|
|
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.
|
|
71
72
|
|
|
72
73
|
## Configs
|
|
73
74
|
|
|
@@ -108,30 +109,31 @@ export default [...sfmc.configs['recommended-next'], ...sfmc.configs['embedded-n
|
|
|
108
109
|
|
|
109
110
|
## AMPscript Rules (`amp-*`)
|
|
110
111
|
|
|
111
|
-
| Rule | Default | Description
|
|
112
|
-
| ------------------------------------------------------------------------------------------- | ------------------------ |
|
|
113
|
-
| [`sfmc/amp-no-unknown-function`](docs/rules/amp/no-unknown-function.md) | `error` | Disallow calls to unknown AMPscript functions
|
|
114
|
-
| [`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`)
|
|
115
|
-
| [`sfmc/amp-function-arity`](docs/rules/amp/function-arity.md) | `error` | Enforce correct argument counts
|
|
116
|
-
| [`sfmc/amp-arg-types`](docs/rules/amp/arg-types.md) | `error` | Check that literal arguments match expected parameter types and allowed values
|
|
117
|
-
| [`sfmc/amp-
|
|
118
|
-
| [`sfmc/amp-
|
|
119
|
-
| [`sfmc/amp-no-
|
|
120
|
-
| [`sfmc/amp-no-
|
|
121
|
-
| [`sfmc/amp-no-
|
|
122
|
-
| [`sfmc/amp-no-
|
|
123
|
-
| [`sfmc/amp-no-
|
|
124
|
-
| [`sfmc/amp-no-
|
|
125
|
-
| [`sfmc/amp-
|
|
126
|
-
| [`sfmc/amp-
|
|
127
|
-
| [`sfmc/amp-
|
|
128
|
-
| [`sfmc/amp-
|
|
129
|
-
| [`sfmc/amp-no-
|
|
130
|
-
| [`sfmc/amp-no-
|
|
131
|
-
| [`sfmc/amp-no-nested-
|
|
132
|
-
| [`sfmc/amp-
|
|
133
|
-
| [`sfmc/amp-
|
|
134
|
-
| [`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 |
|
|
135
137
|
|
|
136
138
|
## SSJS Rules (`ssjs-*`)
|
|
137
139
|
|
|
@@ -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
|
+
```
|
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,13 +36,13 @@
|
|
|
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.1.
|
|
45
|
+
"ssjs-data": "^2.1.1"
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {
|
|
48
48
|
"eslint": ">=10.4.0"
|
package/src/index.js
CHANGED
|
@@ -27,6 +27,7 @@ import ampNoInlineStatement from './rules/amp/no-inline-statement.js';
|
|
|
27
27
|
import ampRequireVariableDeclaration from './rules/amp/require-variable-declaration.js';
|
|
28
28
|
import ampFunctionArity from './rules/amp/function-arity.js';
|
|
29
29
|
import ampArgumentTypes from './rules/amp/argument-types.js';
|
|
30
|
+
import ampPreferBooleanLiteral from './rules/amp/prefer-boolean-literal.js';
|
|
30
31
|
import ampNoEmailExcludedFunction from './rules/amp/no-email-excluded-function.js';
|
|
31
32
|
import ampNoDeprecatedFunction from './rules/amp/no-deprecated-function.js';
|
|
32
33
|
import ampNoNonfunctionalFunction from './rules/amp/no-nonfunctional-function.js';
|
|
@@ -117,6 +118,7 @@ const plugin = {
|
|
|
117
118
|
'amp-require-variable-declaration': ampRequireVariableDeclaration,
|
|
118
119
|
'amp-function-arity': ampFunctionArity,
|
|
119
120
|
'amp-arg-types': ampArgumentTypes,
|
|
121
|
+
'amp-prefer-boolean-literal': ampPreferBooleanLiteral,
|
|
120
122
|
'amp-no-email-excluded-function': ampNoEmailExcludedFunction,
|
|
121
123
|
'amp-no-deprecated-function': ampNoDeprecatedFunction,
|
|
122
124
|
'amp-no-nonfunctional-function': ampNoNonfunctionalFunction,
|
|
@@ -234,6 +236,7 @@ const ampRecommendedRules = {
|
|
|
234
236
|
'sfmc/amp-no-inline-statement': 'warn',
|
|
235
237
|
'sfmc/amp-function-arity': 'error',
|
|
236
238
|
'sfmc/amp-arg-types': 'error',
|
|
239
|
+
'sfmc/amp-prefer-boolean-literal': 'warn',
|
|
237
240
|
'sfmc/amp-no-deprecated-function': 'warn',
|
|
238
241
|
'sfmc/amp-no-nonfunctional-function': 'error',
|
|
239
242
|
'sfmc/amp-naming-convention': 'warn',
|
|
@@ -257,6 +260,7 @@ const ampStrictRules = {
|
|
|
257
260
|
'sfmc/amp-require-variable-declaration': 'warn',
|
|
258
261
|
'sfmc/amp-function-arity': 'error',
|
|
259
262
|
'sfmc/amp-arg-types': 'error',
|
|
263
|
+
'sfmc/amp-prefer-boolean-literal': 'warn',
|
|
260
264
|
'sfmc/amp-no-email-excluded-function': 'off',
|
|
261
265
|
'sfmc/amp-no-deprecated-function': 'warn',
|
|
262
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
|
+
};
|