eslint-plugin-sfmc 5.2.3 → 5.3.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 +27 -26
- package/docs/rules/amp/no-ambiguous-personalization-delimiter.md +78 -0
- package/docs/rules/ssjs/require-platform-load.md +12 -1
- package/package.json +4 -4
- package/src/ampscript-processor.js +14 -0
- package/src/index.js +4 -0
- package/src/processor.js +80 -17
- package/src/rules/amp/no-ambiguous-personalization-delimiter.js +99 -0
- package/src/rules/amp/no-nested-ampscript-delimiter.js +58 -18
package/README.md
CHANGED
|
@@ -68,7 +68,7 @@ SFMC rule metadata links to the owning plugin version's documentation under `blo
|
|
|
68
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
69
|
- No existing rule IDs, public configuration shapes, or supported inputs were removed or changed incompatibly.
|
|
70
70
|
|
|
71
|
-
Version **5.
|
|
71
|
+
Version **5.3.0** exports 54 SFMC rules. Every rule retains version-pinned documentation metadata, and release checks verify the tagged documentation and packed npm artifact. New in this version: `sfmc/amp-no-ambiguous-personalization-delimiter`, and `sfmc/ssjs-require-platform-load` now also covers `HTTP.Get` / `HTTP.Post`. The AMPscript parser adapter exposes ESLint-compatible comment tokens, allowing core comment rules such as `spaced-comment` to validate and autofix comments without changing the canonical AMPscript AST.
|
|
72
72
|
|
|
73
73
|
## Configs
|
|
74
74
|
|
|
@@ -109,31 +109,32 @@ export default [...sfmc.configs['recommended-next'], ...sfmc.configs['embedded-n
|
|
|
109
109
|
|
|
110
110
|
## AMPscript Rules (`amp-*`)
|
|
111
111
|
|
|
112
|
-
| Rule
|
|
113
|
-
|
|
|
114
|
-
| [`sfmc/amp-no-unknown-function`](docs/rules/amp/no-unknown-function.md)
|
|
115
|
-
| [`sfmc/amp-no-mcn-unsupported`](docs/rules/amp/no-mcn-unsupported.md)
|
|
116
|
-
| [`sfmc/amp-function-arity`](docs/rules/amp/function-arity.md)
|
|
117
|
-
| [`sfmc/amp-arg-types`](docs/rules/amp/arg-types.md)
|
|
118
|
-
| [`sfmc/amp-prefer-boolean-literal`](docs/rules/amp/prefer-boolean-literal.md)
|
|
119
|
-
| [`sfmc/amp-set-requires-target`](docs/rules/amp/set-requires-target.md)
|
|
120
|
-
| [`sfmc/amp-no-smart-quotes`](docs/rules/amp/no-smart-quotes.md)
|
|
121
|
-
| [`sfmc/amp-no-var-redeclaration`](docs/rules/amp/no-var-redeclaration.md)
|
|
122
|
-
| [`sfmc/amp-no-empty-block`](docs/rules/amp/no-empty-block.md)
|
|
123
|
-
| [`sfmc/amp-no-loop-counter-assign`](docs/rules/amp/no-loop-counter-assign.md)
|
|
124
|
-
| [`sfmc/amp-no-inline-statement`](docs/rules/amp/no-inline-statement.md)
|
|
125
|
-
| [`sfmc/amp-no-deprecated-function`](docs/rules/amp/no-deprecated-function.md)
|
|
126
|
-
| [`sfmc/amp-no-nonfunctional-function`](docs/rules/amp/no-nonfunctional-function.md)
|
|
127
|
-
| [`sfmc/amp-naming-convention`](docs/rules/amp/naming-convention.md)
|
|
128
|
-
| [`sfmc/amp-no-empty-then`](docs/rules/amp/no-empty-then.md)
|
|
129
|
-
| [`sfmc/amp-require-rowcount-check`](docs/rules/amp/require-rowcount-check.md)
|
|
130
|
-
| [`sfmc/amp-no-html-comment`](docs/rules/amp/no-html-comment.md)
|
|
131
|
-
| [`sfmc/amp-no-js-line-comment`](docs/rules/amp/no-js-line-comment.md)
|
|
132
|
-
| [`sfmc/amp-no-nested-script-tag`](docs/rules/amp/no-nested-script-tag.md)
|
|
133
|
-
| [`sfmc/amp-no-nested-ampscript-delimiter`](docs/rules/amp/no-nested-ampscript-delimiter.md)
|
|
134
|
-
| [`sfmc/amp-
|
|
135
|
-
| [`sfmc/amp-
|
|
136
|
-
| [`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-no-ambiguous-personalization-delimiter`](docs/rules/amp/no-ambiguous-personalization-delimiter.md) | `warn` | Flag a personalization string directly followed by `[` or `=`, which may or may not start AMPscript |
|
|
135
|
+
| [`sfmc/amp-prefer-attribute-value`](docs/rules/amp/prefer-attribute-value.md) | off | Prefer `AttributeValue()` over bare personalization |
|
|
136
|
+
| [`sfmc/amp-require-variable-declaration`](docs/rules/amp/require-variable-declaration.md) | off | Require `var` before `set` |
|
|
137
|
+
| [`sfmc/amp-no-email-excluded-function`](docs/rules/amp/no-email-excluded-function.md) | off | Flag functions unavailable in email context |
|
|
137
138
|
|
|
138
139
|
## SSJS Rules (`ssjs-*`)
|
|
139
140
|
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# `sfmc/amp-no-ambiguous-personalization-delimiter`
|
|
2
|
+
|
|
3
|
+
> Disallow a personalization string directly followed by `[` or `=`, which may or may not form an AMPscript delimiter.
|
|
4
|
+
|
|
5
|
+
| | |
|
|
6
|
+
|---|---|
|
|
7
|
+
| **Type** | `problem` |
|
|
8
|
+
| **Default severity** | `warn` in `recommended` and `strict` |
|
|
9
|
+
| **Fixable** | No |
|
|
10
|
+
|
|
11
|
+
## Why This Rule Exists
|
|
12
|
+
|
|
13
|
+
Marketing Cloud replaces a personalization string such as `%%emailaddr%%` before it parses AMPscript — but only when the name resolves. When the closing `%%` is directly followed by `[` or `=`, the outcome therefore depends on the name:
|
|
14
|
+
|
|
15
|
+
- **The name resolves** — the string is replaced by its value first. The `%%[` or `%%=` that seemed to follow never exists, so no AMPscript starts and everything after the value is output as written. `%%memberid%%=v(@a)=%%` renders as the member ID followed by the literal text `=v(@a)=%%`.
|
|
16
|
+
- **The name does not resolve** — the string is left alone and its closing `%%` joins the next character to form a real `%%[` or `%%=`. `50%%off%%[ set @x = 1 ]%%` runs the block.
|
|
17
|
+
|
|
18
|
+
A linter cannot know whether a name resolves: profile attributes and sendable data extension fields differ per account and per send, and most of them are not available on a CloudPage. The same line can therefore behave differently depending on where it runs. Separating the personalization string from the following character removes the doubt.
|
|
19
|
+
|
|
20
|
+
The rule reports two cases with different messages:
|
|
21
|
+
|
|
22
|
+
- **System personalization strings** (`%%emailaddr%%`, `%%memberid%%`, …) always resolve, so the text after them is never AMPscript.
|
|
23
|
+
- **Any other name** — the result depends on the context the content runs in.
|
|
24
|
+
|
|
25
|
+
Only the character directly after the closing `%%` matters. The opposite side (`=%%emailaddr%%`, as in a URL parameter) is the normal way to use a personalization string and is not reported. Strings inside AMPscript blocks, inline expressions and server-side JavaScript are not checked.
|
|
26
|
+
|
|
27
|
+
## Settings
|
|
28
|
+
|
|
29
|
+
| Setting | Values | Default |
|
|
30
|
+
|---------|--------|---------|
|
|
31
|
+
| severity | `"error"` \| `"warn"` \| `"off"` | `"warn"` |
|
|
32
|
+
|
|
33
|
+
This rule has no configuration options.
|
|
34
|
+
|
|
35
|
+
### Examples
|
|
36
|
+
|
|
37
|
+
**Not allowed — system personalization string followed by `=`:**
|
|
38
|
+
|
|
39
|
+
```html
|
|
40
|
+
<a href="https://example.com/?e=%%emailaddr%%=1">Open</a>
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
```html
|
|
44
|
+
%%emailaddr%%=v(@a)=%%
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
**Not allowed — other name followed by `[` or `=`:**
|
|
48
|
+
|
|
49
|
+
```html
|
|
50
|
+
<td>%%First Name%%[x]</td>
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
```html
|
|
54
|
+
50%%off%%[ set @x = 1 ]%%
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
**Allowed:**
|
|
58
|
+
|
|
59
|
+
```html
|
|
60
|
+
<a href="https://example.com/?e=%%emailaddr%%&a=1">Open</a>
|
|
61
|
+
<p>%%emailaddr%% = 1</p>
|
|
62
|
+
<td>%%First Name%% [x]</td>
|
|
63
|
+
<p>%%=v(@a)=%%%%=v(@b)=%%</p>
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Fix
|
|
67
|
+
|
|
68
|
+
This rule has no auto-fix: inserting a character changes the rendered output, and only the author knows whether literal text or AMPscript was intended.
|
|
69
|
+
|
|
70
|
+
- If the following text is meant literally, put a space or another character between the personalization string and the `[` or `=`.
|
|
71
|
+
- If AMPscript was intended, move the block or inline expression away from the personalization string.
|
|
72
|
+
|
|
73
|
+
## When to Disable
|
|
74
|
+
|
|
75
|
+
```js
|
|
76
|
+
// eslint.config.js
|
|
77
|
+
rules: { 'sfmc/amp-no-ambiguous-personalization-delimiter': 'off' }
|
|
78
|
+
```
|
|
@@ -12,6 +12,8 @@
|
|
|
12
12
|
|
|
13
13
|
SFMC's SSJS runtime requires `Platform.Load("core", "1.1.5")` to be called before any Core library object (`DataExtension`, `Subscriber`, `Email`, `SFMC`, etc.) is used. Without it, calls to `DataExtension.Init()` and similar methods crash at runtime with an object-not-found error. The Platform.Load call initialises the Core library and makes its classes available.
|
|
14
14
|
|
|
15
|
+
The same applies to the globals that only exist once the Core library is loaded. This includes the `HTTP` object: `HTTP.Get(...)` and `HTTP.Post(...)` are reported when the file has no `Platform.Load("core", ...)` call.
|
|
16
|
+
|
|
15
17
|
## Settings
|
|
16
18
|
|
|
17
19
|
| Setting | Values | Default |
|
|
@@ -29,6 +31,10 @@ var de = DataExtension.Init("MyDE");
|
|
|
29
31
|
var rows = de.Rows.Retrieve();
|
|
30
32
|
```
|
|
31
33
|
|
|
34
|
+
```js
|
|
35
|
+
var response = HTTP.Get("https://example.com/api");
|
|
36
|
+
```
|
|
37
|
+
|
|
32
38
|
**Allowed:**
|
|
33
39
|
|
|
34
40
|
```js
|
|
@@ -37,6 +43,11 @@ var de = DataExtension.Init("MyDE");
|
|
|
37
43
|
var rows = de.Rows.Retrieve();
|
|
38
44
|
```
|
|
39
45
|
|
|
46
|
+
```js
|
|
47
|
+
Platform.Load("core", "1.1.5");
|
|
48
|
+
var response = HTTP.Get("https://example.com/api");
|
|
49
|
+
```
|
|
50
|
+
|
|
40
51
|
## Fix
|
|
41
52
|
|
|
42
53
|
This rule provides an **auto-fix**. Applied by:
|
|
@@ -49,7 +60,7 @@ What the fix does: inserts `Platform.Load("core", "1.1.5");` at the very beginni
|
|
|
49
60
|
|
|
50
61
|
## When to Disable
|
|
51
62
|
|
|
52
|
-
Only disable this rule if you are intentionally writing SSJS that
|
|
63
|
+
Only disable this rule if you are intentionally writing SSJS that uses neither Core library objects nor globals that depend on the Core library, such as `HTTP`.
|
|
53
64
|
|
|
54
65
|
```js
|
|
55
66
|
// eslint.config.js
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-sfmc",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.3.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",
|
|
@@ -37,12 +37,12 @@
|
|
|
37
37
|
],
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"ampscript-data": "^4.2.0",
|
|
40
|
-
"ampscript-parser": "^0.
|
|
41
|
-
"eslint-plugin-mso-email": "^2.
|
|
40
|
+
"ampscript-parser": "^0.4.0",
|
|
41
|
+
"eslint-plugin-mso-email": "^2.1.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.2"
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {
|
|
48
48
|
"eslint": ">=10.0.0"
|
|
@@ -10,6 +10,8 @@
|
|
|
10
10
|
* Line offsets are preserved so ESLint reports errors at correct locations.
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
+
import { matchPersonalizationString } from 'ampscript-parser';
|
|
14
|
+
|
|
13
15
|
const SCRIPT_OPEN_RE =
|
|
14
16
|
/^<script\b(?=[^>]*\brunat\s*=\s*['"]server['"])(?=[^>]*\blanguage\s*=\s*['"]ampscript['"])[^>]*>/i;
|
|
15
17
|
const SCRIPT_CLOSE_G = /<\/script\s*>/gi;
|
|
@@ -110,6 +112,8 @@ export function preprocess(text, filename) {
|
|
|
110
112
|
const expressionStart = index;
|
|
111
113
|
const expressionEnd = findInlineExpressionEnd(text, index + 3);
|
|
112
114
|
if (expressionEnd === -1) {
|
|
115
|
+
// Unclosed opener: step past it, otherwise the scan never advances.
|
|
116
|
+
index += 3;
|
|
113
117
|
continue;
|
|
114
118
|
}
|
|
115
119
|
index = expressionEnd;
|
|
@@ -127,6 +131,16 @@ export function preprocess(text, filename) {
|
|
|
127
131
|
continue;
|
|
128
132
|
}
|
|
129
133
|
|
|
134
|
+
// %%name%% personalisation string: plain text. Skip it whole so its
|
|
135
|
+
// closing %% is never read as the start of a %%[ / %%= delimiter.
|
|
136
|
+
if (text[index] === '%' && text[index + 1] === '%') {
|
|
137
|
+
const personalizationEnd = matchPersonalizationString(text, index);
|
|
138
|
+
if (personalizationEnd !== -1) {
|
|
139
|
+
index = personalizationEnd;
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
130
144
|
index++;
|
|
131
145
|
}
|
|
132
146
|
|
package/src/index.js
CHANGED
|
@@ -38,6 +38,7 @@ import ampNoHtmlComment from './rules/amp/no-html-comment.js';
|
|
|
38
38
|
import ampNoJsLineComment from './rules/amp/no-js-line-comment.js';
|
|
39
39
|
import ampNoNestedScriptTag from './rules/amp/no-nested-script-tag.js';
|
|
40
40
|
import ampNoNestedAmpscriptDelimiter from './rules/amp/no-nested-ampscript-delimiter.js';
|
|
41
|
+
import ampNoAmbiguousPersonalizationDelimiter from './rules/amp/no-ambiguous-personalization-delimiter.js';
|
|
41
42
|
|
|
42
43
|
// ── SSJS rules ────────────────────────────────────────────────────────────────
|
|
43
44
|
|
|
@@ -129,6 +130,7 @@ const plugin = {
|
|
|
129
130
|
'amp-no-js-line-comment': ampNoJsLineComment,
|
|
130
131
|
'amp-no-nested-script-tag': ampNoNestedScriptTag,
|
|
131
132
|
'amp-no-nested-ampscript-delimiter': ampNoNestedAmpscriptDelimiter,
|
|
133
|
+
'amp-no-ambiguous-personalization-delimiter': ampNoAmbiguousPersonalizationDelimiter,
|
|
132
134
|
|
|
133
135
|
// SSJS rules (ssjs- prefix)
|
|
134
136
|
'ssjs-require-platform-load': ssjsRequirePlatformLoad,
|
|
@@ -246,6 +248,7 @@ const ampRecommendedRules = {
|
|
|
246
248
|
'sfmc/amp-no-js-line-comment': 'warn',
|
|
247
249
|
'sfmc/amp-no-nested-script-tag': 'error',
|
|
248
250
|
'sfmc/amp-no-nested-ampscript-delimiter': 'error',
|
|
251
|
+
'sfmc/amp-no-ambiguous-personalization-delimiter': 'warn',
|
|
249
252
|
};
|
|
250
253
|
|
|
251
254
|
const ampStrictRules = {
|
|
@@ -271,6 +274,7 @@ const ampStrictRules = {
|
|
|
271
274
|
'sfmc/amp-no-js-line-comment': 'error',
|
|
272
275
|
'sfmc/amp-no-nested-script-tag': 'error',
|
|
273
276
|
'sfmc/amp-no-nested-ampscript-delimiter': 'error',
|
|
277
|
+
'sfmc/amp-no-ambiguous-personalization-delimiter': 'warn',
|
|
274
278
|
};
|
|
275
279
|
|
|
276
280
|
// ── SSJS rule sets ────────────────────────────────────────────────────────────
|
package/src/processor.js
CHANGED
|
@@ -9,6 +9,9 @@
|
|
|
9
9
|
* 4. <script runat="server"> (non-ampscript) → virtual .js files
|
|
10
10
|
* 5. {{ ... }} Handlebars expressions and {!$...} bindings → one virtual .hbs
|
|
11
11
|
* file holding the whole document with AMPscript regions blanked out
|
|
12
|
+
* 6. %%name%% personalization strings directly followed by [ or = → one
|
|
13
|
+
* virtual .amp file holding only those strings at their original offsets,
|
|
14
|
+
* so the AMPscript rule for them can check the text between the regions
|
|
12
15
|
*
|
|
13
16
|
* Returns extracted regions as virtual files for ESLint to lint with the
|
|
14
17
|
* appropriate parser/rules based on file extension matching.
|
|
@@ -20,6 +23,7 @@
|
|
|
20
23
|
* processor (this one) runs on every HTML file and no processor conflict arises.
|
|
21
24
|
*/
|
|
22
25
|
|
|
26
|
+
import { matchPersonalizationString } from 'ampscript-parser';
|
|
23
27
|
import { preprocess as msoPreprocess } from 'eslint-plugin-mso-email/processor';
|
|
24
28
|
import { MSO_COMMENT_PATTERN } from 'eslint-plugin-mso-email/comment-pattern';
|
|
25
29
|
|
|
@@ -30,13 +34,12 @@ const SSJS_SCRIPT_OPEN_RE =
|
|
|
30
34
|
const SCRIPT_CLOSE_G = /<\/script\s*>/gi;
|
|
31
35
|
|
|
32
36
|
/**
|
|
33
|
-
* AMPscript
|
|
37
|
+
* AMPscript script tags blanked before the Handlebars parser runs. The
|
|
38
|
+
* %%[ ... ]%% and %%= ... =%% regions are blanked from the offsets found by the
|
|
39
|
+
* delimiter scan in `preprocess`, so both agree on what a region is.
|
|
34
40
|
*/
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
/%%=[\s\S]*?=%%/g,
|
|
38
|
-
/<script\s[^>]*language\s*=\s*["']ampscript["'][^>]*>[\s\S]*?<\/script>/gi,
|
|
39
|
-
];
|
|
41
|
+
const AMPSCRIPT_SCRIPT_REGION_RE =
|
|
42
|
+
/<script\s[^>]*language\s*=\s*["']ampscript["'][^>]*>[\s\S]*?<\/script>/gi;
|
|
40
43
|
|
|
41
44
|
function countNewlinesBefore(text, pos) {
|
|
42
45
|
let count = 0;
|
|
@@ -73,29 +76,53 @@ function findInlineExpressionEnd(text, from) {
|
|
|
73
76
|
* Mirrors `getSanitizedHandlebarsText` in sfmc-language-lsp.
|
|
74
77
|
*
|
|
75
78
|
* @param {string} text - Full document text.
|
|
79
|
+
* @param {[number, number][]} delimitedRegions - Start (inclusive) and end
|
|
80
|
+
* (exclusive) offsets of the %%[ ... ]%% and %%= ... =%% regions.
|
|
76
81
|
* @returns {string} Text with AMPscript regions blanked out.
|
|
77
82
|
*/
|
|
78
|
-
function sanitizeAmpscriptRegions(text) {
|
|
83
|
+
function sanitizeAmpscriptRegions(text, delimitedRegions) {
|
|
79
84
|
const chars = Array.from(text);
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
85
|
+
const regions = [...delimitedRegions];
|
|
86
|
+
AMPSCRIPT_SCRIPT_REGION_RE.lastIndex = 0;
|
|
87
|
+
let match;
|
|
88
|
+
while ((match = AMPSCRIPT_SCRIPT_REGION_RE.exec(text)) !== null) {
|
|
89
|
+
regions.push([match.index, match.index + match[0].length]);
|
|
90
|
+
}
|
|
91
|
+
for (const [start, end] of regions) {
|
|
92
|
+
for (let index = start; index < end && index < chars.length; index++) {
|
|
93
|
+
if (chars[index] !== '\n' && chars[index] !== '\r') {
|
|
94
|
+
chars[index] = ' ';
|
|
89
95
|
}
|
|
90
96
|
}
|
|
91
97
|
}
|
|
92
98
|
return chars.join('');
|
|
93
99
|
}
|
|
94
100
|
|
|
101
|
+
/**
|
|
102
|
+
* Returns a copy of the document where everything outside the given regions is
|
|
103
|
+
* replaced with spaces, preserving newlines and overall offsets.
|
|
104
|
+
*
|
|
105
|
+
* @param {string} text - Full document text.
|
|
106
|
+
* @param {[number, number][]} regions - Start (inclusive) and end (exclusive)
|
|
107
|
+
* offsets of the regions to keep.
|
|
108
|
+
* @returns {string} Text with only the given regions left in place.
|
|
109
|
+
*/
|
|
110
|
+
function keepOnlyRegions(text, regions) {
|
|
111
|
+
let result = '';
|
|
112
|
+
let position = 0;
|
|
113
|
+
for (const [start, end] of regions) {
|
|
114
|
+
result += text.slice(position, start).replaceAll(/[^\n\r]/g, ' ') + text.slice(start, end);
|
|
115
|
+
position = end;
|
|
116
|
+
}
|
|
117
|
+
return result + text.slice(position).replaceAll(/[^\n\r]/g, ' ');
|
|
118
|
+
}
|
|
119
|
+
|
|
95
120
|
export function preprocess(text, filename) {
|
|
96
121
|
const blocks = [];
|
|
97
122
|
let ampCount = 0;
|
|
98
123
|
let ssjsCount = 0;
|
|
124
|
+
const delimitedRegions = [];
|
|
125
|
+
const ambiguousPersonalizations = [];
|
|
99
126
|
|
|
100
127
|
// --- Pass 1: extract AMPscript regions ---
|
|
101
128
|
let index = 0;
|
|
@@ -144,6 +171,7 @@ export function preprocess(text, filename) {
|
|
|
144
171
|
if (depth !== 0) {
|
|
145
172
|
continue;
|
|
146
173
|
}
|
|
174
|
+
delimitedRegions.push([blockStart, index]);
|
|
147
175
|
const fullBlock = text.slice(blockStart, index);
|
|
148
176
|
const padding = '\n'.repeat(countNewlinesBefore(text, blockStart));
|
|
149
177
|
blocks.push({
|
|
@@ -158,9 +186,12 @@ export function preprocess(text, filename) {
|
|
|
158
186
|
const expressionStart = index;
|
|
159
187
|
const expressionEnd = findInlineExpressionEnd(text, index + 3);
|
|
160
188
|
if (expressionEnd === -1) {
|
|
189
|
+
// Unclosed opener: step past it, otherwise the scan never advances.
|
|
190
|
+
index += 3;
|
|
161
191
|
continue;
|
|
162
192
|
}
|
|
163
193
|
index = expressionEnd;
|
|
194
|
+
delimitedRegions.push([expressionStart, index]);
|
|
164
195
|
const innerCode = text.slice(expressionStart + 3, index - 3);
|
|
165
196
|
const wrappedBlock = `%%[${innerCode}]%%`;
|
|
166
197
|
const padding = '\n'.repeat(countNewlinesBefore(text, expressionStart));
|
|
@@ -171,10 +202,24 @@ export function preprocess(text, filename) {
|
|
|
171
202
|
continue;
|
|
172
203
|
}
|
|
173
204
|
|
|
205
|
+
// %%name%% personalisation string: plain text. Skip it whole so its
|
|
206
|
+
// closing %% is never read as the start of a %%[ / %%= delimiter.
|
|
207
|
+
if (text[index] === '%' && text[index + 1] === '%') {
|
|
208
|
+
const personalizationEnd = matchPersonalizationString(text, index);
|
|
209
|
+
if (personalizationEnd !== -1) {
|
|
210
|
+
if (text[personalizationEnd] === '[' || text[personalizationEnd] === '=') {
|
|
211
|
+
ambiguousPersonalizations.push([index, personalizationEnd + 1]);
|
|
212
|
+
}
|
|
213
|
+
index = personalizationEnd;
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
174
218
|
index++;
|
|
175
219
|
}
|
|
176
220
|
|
|
177
221
|
// --- Pass 2: extract SSJS regions ---
|
|
222
|
+
const ssjsRegions = [];
|
|
178
223
|
let match;
|
|
179
224
|
SSJS_SCRIPT_OPEN_RE.lastIndex = 0;
|
|
180
225
|
while ((match = SSJS_SCRIPT_OPEN_RE.exec(text)) !== null) {
|
|
@@ -185,6 +230,7 @@ export function preprocess(text, filename) {
|
|
|
185
230
|
break;
|
|
186
231
|
}
|
|
187
232
|
|
|
233
|
+
ssjsRegions.push([openEnd, closeMatch.index]);
|
|
188
234
|
const jsCode = text.slice(openEnd, closeMatch.index);
|
|
189
235
|
const linesBefore = text.slice(0, openEnd).split('\n');
|
|
190
236
|
const startLine = linesBefore.length - 1;
|
|
@@ -203,7 +249,7 @@ export function preprocess(text, filename) {
|
|
|
203
249
|
// a single virtual .hbs file. Only emit it when the document actually
|
|
204
250
|
// contains Handlebars/binding syntax to avoid paying the parse cost on plain
|
|
205
251
|
// HTML/AMPscript content.
|
|
206
|
-
const sanitizedForHbs = sanitizeAmpscriptRegions(text);
|
|
252
|
+
const sanitizedForHbs = sanitizeAmpscriptRegions(text, delimitedRegions);
|
|
207
253
|
if (sanitizedForHbs.includes('{{') || sanitizedForHbs.includes('{!$')) {
|
|
208
254
|
blocks.push({
|
|
209
255
|
text: sanitizedForHbs,
|
|
@@ -211,6 +257,23 @@ export function preprocess(text, filename) {
|
|
|
211
257
|
});
|
|
212
258
|
}
|
|
213
259
|
|
|
260
|
+
// --- Personalization strings between the AMPscript regions ---
|
|
261
|
+
// AMPscript rules only see the extracted regions. A personalization string
|
|
262
|
+
// directly followed by [ or = sits in the text between them, so those strings
|
|
263
|
+
// (and nothing else — the surrounding HTML is blanked, offsets preserved) are
|
|
264
|
+
// emitted as one more virtual .amp file for
|
|
265
|
+
// sfmc/amp-no-ambiguous-personalization-delimiter. Strings inside server-side
|
|
266
|
+
// JavaScript are left out. Only emitted when such a string exists.
|
|
267
|
+
const reportablePersonalizations = ambiguousPersonalizations.filter(([start]) =>
|
|
268
|
+
ssjsRegions.every(([from, to]) => !(start >= from && start < to)),
|
|
269
|
+
);
|
|
270
|
+
if (reportablePersonalizations.length > 0) {
|
|
271
|
+
blocks.push({
|
|
272
|
+
text: keepOnlyRegions(text, reportablePersonalizations),
|
|
273
|
+
filename: `${filename}/ampscript-personalization.amp`,
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
|
|
214
277
|
// --- Pass 4: delegate MSO / Outlook conditional-comment extraction ---
|
|
215
278
|
// Only when the document actually contains MSO markup, so plain HTML/AMPscript
|
|
216
279
|
// files don't get an extra full-document virtual block. MSO_COMMENT_PATTERN is
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { matchPersonalizationString } from 'ampscript-parser';
|
|
2
|
+
import { isSystemPersonalizationString } from 'ampscript-data';
|
|
3
|
+
|
|
4
|
+
// Marketing Cloud replaces a personalization string whose name resolves BEFORE
|
|
5
|
+
// AMPscript is parsed. When its closing %% is directly followed by [ or =, the
|
|
6
|
+
// two only form a %%[ / %%= delimiter if the name does NOT resolve. Whether a
|
|
7
|
+
// non-system name resolves depends on the account and the send context, so the
|
|
8
|
+
// construct is reported and the author is asked to separate the two.
|
|
9
|
+
//
|
|
10
|
+
// Personalization strings live in the template text outside AMPscript regions,
|
|
11
|
+
// i.e. in the top-level Content nodes. Server-side JavaScript bodies are part of
|
|
12
|
+
// that text for the AMPscript parser and are skipped here.
|
|
13
|
+
const SSJS_SCRIPT_OPEN_RE = /<script\b(?=[^>]*\brunat\s*=\s*['"]server['"])[^>]*>/iy;
|
|
14
|
+
const SCRIPT_CLOSE_RE = /<\/script\s*>/gi;
|
|
15
|
+
|
|
16
|
+
const FOLLOWING_CHARACTERS = new Set(['[', '=']);
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Finds the end of a server-side script block that opens at `index`.
|
|
20
|
+
*
|
|
21
|
+
* @param {string} text - Full source text.
|
|
22
|
+
* @param {number} index - Offset of a `<` character.
|
|
23
|
+
* @returns {number} Offset just past the closing script tag (or the end of the
|
|
24
|
+
* text when it is not closed), or -1 when no server-side script opens here.
|
|
25
|
+
*/
|
|
26
|
+
function matchServerScript(text, index) {
|
|
27
|
+
SSJS_SCRIPT_OPEN_RE.lastIndex = index;
|
|
28
|
+
if (!SSJS_SCRIPT_OPEN_RE.test(text)) {
|
|
29
|
+
return -1;
|
|
30
|
+
}
|
|
31
|
+
SCRIPT_CLOSE_RE.lastIndex = SSJS_SCRIPT_OPEN_RE.lastIndex;
|
|
32
|
+
return SCRIPT_CLOSE_RE.test(text) ? SCRIPT_CLOSE_RE.lastIndex : text.length;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export default {
|
|
36
|
+
meta: {
|
|
37
|
+
type: 'problem',
|
|
38
|
+
docs: {
|
|
39
|
+
description:
|
|
40
|
+
'Disallow a personalization string directly followed by [ or =, which may or may not form an AMPscript delimiter.',
|
|
41
|
+
recommended: true,
|
|
42
|
+
},
|
|
43
|
+
messages: {
|
|
44
|
+
systemString:
|
|
45
|
+
'Personalization string {{string}} is replaced before AMPscript is parsed, so the "%%{{character}}" it forms with the following "{{character}}" does not start AMPscript and the text after it is output as written. Add a space or another character in between if literal text is intended, or move the AMPscript away from the string.',
|
|
46
|
+
unknownName:
|
|
47
|
+
'Whether the "%%{{character}}" formed by {{string}} and the following "{{character}}" starts AMPscript depends on whether Marketing Cloud can resolve the name where the content runs (attribute or sendable data extension field in a send; often not on a CloudPage): if it resolves, what follows is plain text. Add a space or another character in between to make the result predictable.',
|
|
48
|
+
},
|
|
49
|
+
schema: [],
|
|
50
|
+
},
|
|
51
|
+
|
|
52
|
+
create(context) {
|
|
53
|
+
const sourceCode = context.sourceCode;
|
|
54
|
+
|
|
55
|
+
return {
|
|
56
|
+
Program(node) {
|
|
57
|
+
const text = sourceCode.getText();
|
|
58
|
+
|
|
59
|
+
for (const child of node.body) {
|
|
60
|
+
if (child.type !== 'Content') {
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
const [contentStart, contentEnd] = child.range;
|
|
64
|
+
let index = contentStart;
|
|
65
|
+
while (index < contentEnd) {
|
|
66
|
+
if (text[index] === '<') {
|
|
67
|
+
const scriptEnd = matchServerScript(text, index);
|
|
68
|
+
if (scriptEnd !== -1) {
|
|
69
|
+
index = scriptEnd;
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
} else if (text.startsWith('%%', index)) {
|
|
73
|
+
const end = matchPersonalizationString(text, index);
|
|
74
|
+
if (end !== -1) {
|
|
75
|
+
const character = text[end];
|
|
76
|
+
if (FOLLOWING_CHARACTERS.has(character)) {
|
|
77
|
+
const name = text.slice(index + 2, end - 2);
|
|
78
|
+
context.report({
|
|
79
|
+
loc: {
|
|
80
|
+
start: sourceCode.getLocFromIndex(index),
|
|
81
|
+
end: sourceCode.getLocFromIndex(end + 1),
|
|
82
|
+
},
|
|
83
|
+
messageId: isSystemPersonalizationString(name)
|
|
84
|
+
? 'systemString'
|
|
85
|
+
: 'unknownName',
|
|
86
|
+
data: { string: text.slice(index, end), character },
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
index = end;
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
index++;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
},
|
|
99
|
+
};
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { matchPersonalizationString } from 'ampscript-parser';
|
|
2
|
+
|
|
1
3
|
// The processor extracts each AMPscript region into its own virtual .amp file,
|
|
2
4
|
// including the outer delimiters (%%[...]/%%] or <script>...</script>).
|
|
3
5
|
// Any %%[ or %%= found INSIDE an already-open block is nested and invalid.
|
|
@@ -8,10 +10,55 @@
|
|
|
8
10
|
// an opener is only nested when depth > 0.
|
|
9
11
|
const TOKEN_RE = /%%\[|%%=|\]%%|=%%/g;
|
|
10
12
|
const SCRIPT_TAG_RE = /^[\n]*<script\b/i;
|
|
13
|
+
const SCRIPT_CLOSE_RE = /<\/script\s*>/i;
|
|
11
14
|
|
|
12
15
|
const OPEN_TOKENS = new Set(['%%[', '%%=']);
|
|
13
16
|
const CLOSE_FOR_OPEN = { '%%[': ']%%', '%%=': '=%%' };
|
|
14
17
|
|
|
18
|
+
/**
|
|
19
|
+
* Finds every %%[ / %%= opener that sits inside an already-open region.
|
|
20
|
+
*
|
|
21
|
+
* Outside any region a %%name%% personalisation string is plain text and is
|
|
22
|
+
* skipped whole, so its closing %% is never read as the start of an opener and
|
|
23
|
+
* closers are not looked for. Inside a region openers and closers are matched
|
|
24
|
+
* in document order.
|
|
25
|
+
*
|
|
26
|
+
* @param {string} text - Source text of the file or virtual block.
|
|
27
|
+
* @param {number} [from] - Offset to start scanning at.
|
|
28
|
+
* @returns {{delimiter: string, start: number}[]} Nested openers.
|
|
29
|
+
*/
|
|
30
|
+
function findNestedOpeners(text, from = 0) {
|
|
31
|
+
const nested = [];
|
|
32
|
+
let depth = 0;
|
|
33
|
+
let index = from;
|
|
34
|
+
while (index < text.length) {
|
|
35
|
+
if (text.startsWith('%%', index)) {
|
|
36
|
+
const delimiter = text.slice(index, index + 3);
|
|
37
|
+
if (OPEN_TOKENS.has(delimiter)) {
|
|
38
|
+
if (depth > 0) {
|
|
39
|
+
nested.push({ delimiter, start: index });
|
|
40
|
+
}
|
|
41
|
+
depth++;
|
|
42
|
+
index += 3;
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (depth === 0) {
|
|
46
|
+
const personalizationEnd = matchPersonalizationString(text, index);
|
|
47
|
+
if (personalizationEnd !== -1) {
|
|
48
|
+
index = personalizationEnd;
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
} else if (depth > 0 && (text.startsWith(']%%', index) || text.startsWith('=%%', index))) {
|
|
53
|
+
depth--;
|
|
54
|
+
index += 3;
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
index++;
|
|
58
|
+
}
|
|
59
|
+
return nested;
|
|
60
|
+
}
|
|
61
|
+
|
|
15
62
|
export default {
|
|
16
63
|
meta: {
|
|
17
64
|
type: 'problem',
|
|
@@ -31,9 +78,7 @@ export default {
|
|
|
31
78
|
create(context) {
|
|
32
79
|
const sourceCode = context.sourceCode;
|
|
33
80
|
|
|
34
|
-
const reportNested = (text,
|
|
35
|
-
const delimiter = match[0];
|
|
36
|
-
const start = match.index;
|
|
81
|
+
const reportNested = (text, delimiter, start, isScriptBlock) => {
|
|
37
82
|
const end = start + delimiter.length;
|
|
38
83
|
const closeToken = CLOSE_FOR_OPEN[delimiter];
|
|
39
84
|
const closeIndex = text.indexOf(closeToken, end);
|
|
@@ -60,33 +105,28 @@ export default {
|
|
|
60
105
|
const text = sourceCode.getText();
|
|
61
106
|
const isScriptBlock = SCRIPT_TAG_RE.test(text);
|
|
62
107
|
|
|
108
|
+
let scriptEnd = 0;
|
|
63
109
|
if (isScriptBlock) {
|
|
64
110
|
// Inside a <script language="ampscript"> block the whole body is
|
|
65
111
|
// already AMPscript; every %%[ / %%= delimiter is redundant.
|
|
112
|
+
// The block ends at the first </script>; what follows is
|
|
113
|
+
// ordinary template content again.
|
|
114
|
+
const scriptClose = SCRIPT_CLOSE_RE.exec(text);
|
|
115
|
+
scriptEnd = scriptClose ? scriptClose.index : text.length;
|
|
116
|
+
const scriptText = text.slice(0, scriptEnd);
|
|
66
117
|
TOKEN_RE.lastIndex = 0;
|
|
67
118
|
let match;
|
|
68
|
-
while ((match = TOKEN_RE.exec(
|
|
119
|
+
while ((match = TOKEN_RE.exec(scriptText)) !== null) {
|
|
69
120
|
if (OPEN_TOKENS.has(match[0])) {
|
|
70
|
-
reportNested(
|
|
121
|
+
reportNested(scriptText, match[0], match.index, true);
|
|
71
122
|
}
|
|
72
123
|
}
|
|
73
|
-
return;
|
|
74
124
|
}
|
|
75
125
|
|
|
76
126
|
// Depth-based scan for raw %%[...]%% / %%=...=%% content. Sibling blocks
|
|
77
127
|
// return depth to 0 between them, so their openers are not flagged.
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
let match;
|
|
81
|
-
while ((match = TOKEN_RE.exec(text)) !== null) {
|
|
82
|
-
if (OPEN_TOKENS.has(match[0])) {
|
|
83
|
-
if (depth > 0) {
|
|
84
|
-
reportNested(text, match, false);
|
|
85
|
-
}
|
|
86
|
-
depth++;
|
|
87
|
-
} else {
|
|
88
|
-
depth = Math.max(0, depth - 1);
|
|
89
|
-
}
|
|
128
|
+
for (const { delimiter, start } of findNestedOpeners(text, scriptEnd)) {
|
|
129
|
+
reportNested(text, delimiter, start, false);
|
|
90
130
|
}
|
|
91
131
|
},
|
|
92
132
|
};
|