eslint-plugin-sfmc 4.9.1 → 4.11.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 +25 -24
- package/docs/rules/ssjs/no-deprecated-function.md +17 -0
- package/docs/rules/ssjs/no-invalid-property-access.md +73 -0
- package/docs/rules/ssjs/no-property-call.md +6 -2
- package/docs/rules/ssjs/prefer-platform-load-version.md +8 -0
- package/package.json +2 -2
- package/src/index.js +5 -0
- package/src/rules/ssjs/no-deprecated-function.js +130 -6
- package/src/rules/ssjs/no-invalid-property-access.js +205 -0
- package/src/rules/ssjs/prefer-platform-load-version.js +33 -11
package/README.md
CHANGED
|
@@ -122,30 +122,31 @@ export default [...sfmc.configs['recommended-next'], ...sfmc.configs['embedded-n
|
|
|
122
122
|
|
|
123
123
|
## SSJS Rules (`ssjs-*`)
|
|
124
124
|
|
|
125
|
-
| Rule | Default | Description
|
|
126
|
-
| ------------------------------------------------------------------------------------------- | ------------------------ |
|
|
127
|
-
| [`sfmc/ssjs-require-platform-load`](docs/rules/ssjs/require-platform-load.md) | `error` | Require `Platform.Load("core")` before Core or requiresCoreLoad globals
|
|
128
|
-
| [`sfmc/ssjs-no-unsupported-syntax`](docs/rules/ssjs/no-unsupported-syntax.md) | `error` | Flag ES6+ syntax not supported by SFMC
|
|
129
|
-
| [`sfmc/ssjs-no-unknown-function`](docs/rules/ssjs/no-unknown-function.md) | `error` | Disallow unknown methods on Platform.\*, HTTP, Core Library, and WSProxy
|
|
130
|
-
| [`sfmc/ssjs-no-mcn-unsupported`](docs/rules/ssjs/no-mcn-unsupported.md) | off (`error` in `-next`) | Flag all SSJS API usage as unsupported in Marketing Cloud Next
|
|
131
|
-
| [`sfmc/ssjs-no-deprecated-function`](docs/rules/ssjs/no-deprecated-function.md) | `error` | Flag
|
|
132
|
-
| [`sfmc/ssjs-no-nonexistent-global`](docs/rules/ssjs/no-nonexistent-global.md) | `error` | Flag documented SSJS globals that throw ReferenceError at runtime
|
|
133
|
-
| [`sfmc/ssjs-no-property-call`](docs/rules/ssjs/no-property-call.md) | `error` | Disallow calling Platform.Request/Response properties as functions
|
|
134
|
-
| [`sfmc/ssjs-no-
|
|
135
|
-
| [`sfmc/ssjs-
|
|
136
|
-
| [`sfmc/ssjs-
|
|
137
|
-
| [`sfmc/ssjs-
|
|
138
|
-
| [`sfmc/ssjs-
|
|
139
|
-
| [`sfmc/ssjs-
|
|
140
|
-
| [`sfmc/ssjs-
|
|
141
|
-
| [`sfmc/ssjs-
|
|
142
|
-
| [`sfmc/ssjs-
|
|
143
|
-
| [`sfmc/ssjs-no-
|
|
144
|
-
| [`sfmc/ssjs-
|
|
145
|
-
| [`sfmc/ssjs-
|
|
146
|
-
| [`sfmc/ssjs-no-
|
|
147
|
-
| [`sfmc/ssjs-
|
|
148
|
-
| [`sfmc/ssjs-
|
|
125
|
+
| Rule | Default | Description |
|
|
126
|
+
| ------------------------------------------------------------------------------------------- | ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
127
|
+
| [`sfmc/ssjs-require-platform-load`](docs/rules/ssjs/require-platform-load.md) | `error` | Require `Platform.Load("core")` before Core or requiresCoreLoad globals |
|
|
128
|
+
| [`sfmc/ssjs-no-unsupported-syntax`](docs/rules/ssjs/no-unsupported-syntax.md) | `error` | Flag ES6+ syntax not supported by SFMC |
|
|
129
|
+
| [`sfmc/ssjs-no-unknown-function`](docs/rules/ssjs/no-unknown-function.md) | `error` | Disallow unknown methods on Platform.\*, HTTP, Core Library, and WSProxy |
|
|
130
|
+
| [`sfmc/ssjs-no-mcn-unsupported`](docs/rules/ssjs/no-mcn-unsupported.md) | off (`error` in `-next`) | Flag all SSJS API usage as unsupported in Marketing Cloud Next |
|
|
131
|
+
| [`sfmc/ssjs-no-deprecated-function`](docs/rules/ssjs/no-deprecated-function.md) | `error` | Flag deprecated SFMC SSJS APIs (Classic Content classes, ErrorUtil); reports a stronger "undefined at runtime" message when the loaded Core version no longer provides the API |
|
|
132
|
+
| [`sfmc/ssjs-no-nonexistent-global`](docs/rules/ssjs/no-nonexistent-global.md) | `error` | Flag documented SSJS globals that throw ReferenceError at runtime |
|
|
133
|
+
| [`sfmc/ssjs-no-property-call`](docs/rules/ssjs/no-property-call.md) | `error` | Disallow calling Platform.Request/Response properties as functions |
|
|
134
|
+
| [`sfmc/ssjs-no-invalid-property-access`](docs/rules/ssjs/no-invalid-property-access.md) | `error` | Disallow reading write-only properties (`postData`) and assigning to read-only ones (`Platform.Request.*`) |
|
|
135
|
+
| [`sfmc/ssjs-no-clr-header-access`](docs/rules/ssjs/no-clr-header-access.md) | `error` | Disallow CLR-unsafe reads of `HttpResponse.headers`; read via `for..in` |
|
|
136
|
+
| [`sfmc/ssjs-require-string-clr-content`](docs/rules/ssjs/require-string-clr-content.md) | `error` | Require wrapping `HttpResponse.content` with `String()` before use |
|
|
137
|
+
| [`sfmc/ssjs-platform-function-arity`](docs/rules/ssjs/platform-function-arity.md) | `error` | Enforce correct arity for `Platform.Function.*` |
|
|
138
|
+
| [`sfmc/ssjs-require-platform-load-order`](docs/rules/ssjs/require-platform-load-order.md) | `error` | Require `Platform.Load()` before Core usage in order |
|
|
139
|
+
| [`sfmc/ssjs-no-hardcoded-credentials`](docs/rules/ssjs/no-hardcoded-credentials.md) | `error` | Flag hardcoded keys in encryption calls |
|
|
140
|
+
| [`sfmc/ssjs-cache-loop-length`](docs/rules/ssjs/cache-loop-length.md) | `warn` | Require caching `.length` in for-loops |
|
|
141
|
+
| [`sfmc/ssjs-require-hasownproperty`](docs/rules/ssjs/require-hasownproperty.md) | `warn` | Require `hasOwnProperty` guard in for-in loops |
|
|
142
|
+
| [`sfmc/ssjs-prefer-platform-load-version`](docs/rules/ssjs/prefer-platform-load-version.md) | `warn` | Enforce a minimum `Platform.Load` version string; autofix is withheld in files using `ErrorUtil` |
|
|
143
|
+
| [`sfmc/ssjs-no-unavailable-method`](docs/rules/ssjs/no-unavailable-method.md) | `warn` | Flag Array/String methods unavailable or broken in SFMC's ES3 engine |
|
|
144
|
+
| [`sfmc/ssjs-no-nonfunctional-method`](docs/rules/ssjs/no-nonfunctional-method.md) | `warn` | Flag Core Library methods that never take effect at runtime |
|
|
145
|
+
| [`sfmc/ssjs-prefer-parsejson-safe-arg`](docs/rules/ssjs/prefer-parsejson-safe-arg.md) | `warn` | Require string coercion on `ParseJSON` argument |
|
|
146
|
+
| [`sfmc/ssjs-no-switch-default`](docs/rules/ssjs/no-switch-default.md) | `warn` | Disallow `default` clause in `switch` statements |
|
|
147
|
+
| [`sfmc/ssjs-no-treatascontent-injection`](docs/rules/ssjs/no-treatascontent-injection.md) | `warn` | Flag dynamic string concatenation in `TreatAsContent` calls |
|
|
148
|
+
| [`sfmc/ssjs-core-method-arity`](docs/rules/ssjs/core-method-arity.md) | `warn` | Enforce correct argument counts for Core Library object methods |
|
|
149
|
+
| [`sfmc/ssjs-arg-types`](docs/rules/ssjs/arg-types.md) | `warn` | Check that literal arguments match expected parameter types |
|
|
149
150
|
|
|
150
151
|
## Handlebars Rules (`hbs-*`)
|
|
151
152
|
|
|
@@ -22,6 +22,23 @@ plus the legacy `ErrorUtil` helper that only exists under the oldest Core versio
|
|
|
22
22
|
| `Email.*` | `Email` class is deprecated in favor of Content Builder / Journey Builder sends |
|
|
23
23
|
| `ErrorUtil.ThrowWSProxyError(…)` | Only exists under `Platform.Load("Core", "1")`; undefined in newer Core versions — check `result.Status` and `throw new Error(…)` instead |
|
|
24
24
|
|
|
25
|
+
## `ErrorUtil` — two message variants
|
|
26
|
+
|
|
27
|
+
`ErrorUtil` is bound to a maximum Core version (`maxCoreVersion: "1"` in `ssjs-data`), so
|
|
28
|
+
the rule picks its wording from the `Platform.Load("Core", …)` version the file loads:
|
|
29
|
+
|
|
30
|
+
| Loaded Core version | Message | Meaning |
|
|
31
|
+
|---|---|---|
|
|
32
|
+
| `"1"`, or no version argument, or no `Platform.Load` at all | *"… is deprecated …"* | The call works today, but the API is retired — migrate away from it |
|
|
33
|
+
| Anything above `"1"` (`"1.1.1"`, `"1.1.5"`, `"2"`, …) | *"… is undefined under Platform.Load("Core", "…") …"* | `ErrorUtil` does not exist at runtime — the call throws a `TypeError` |
|
|
34
|
+
|
|
35
|
+
The applicable load is the nearest preceding `Platform.Load("Core", …)`; when every load
|
|
36
|
+
appears after the call, the first one in the file is used (it still governs at runtime).
|
|
37
|
+
|
|
38
|
+
Because bumping the Core version would break such a file,
|
|
39
|
+
[`sfmc/ssjs-prefer-platform-load-version`](./prefer-platform-load-version.md) withholds
|
|
40
|
+
its autofix whenever `ErrorUtil` is used.
|
|
41
|
+
|
|
25
42
|
## Examples
|
|
26
43
|
|
|
27
44
|
### ❌ Incorrect
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# `sfmc/ssjs-no-invalid-property-access`
|
|
2
|
+
|
|
3
|
+
Flags property accesses that go against the direction the SFMC runtime actually supports —
|
|
4
|
+
reading a property that can only be written, or writing one that can only be read.
|
|
5
|
+
|
|
6
|
+
The restriction lives in [`ssjs-data`](../../../..) as an `access` field on the property
|
|
7
|
+
entry (exposed via `propertyAccessLookup`), so this rule and the VS Code / Cursor
|
|
8
|
+
diagnostic (`ssjs/invalid-property-access`) share one source of truth, and flagging a
|
|
9
|
+
future property is a one-field data change.
|
|
10
|
+
|
|
11
|
+
## The three restrictions
|
|
12
|
+
|
|
13
|
+
| `access` | Runtime behaviour | Flagged usage | Members |
|
|
14
|
+
| --- | --- | --- | --- |
|
|
15
|
+
| `write-only` | Assignment works; reading throws `"Property Get method was not found."` — outside a `try`/`catch` that throw aborts the whole page | read | `Script.Util.HttpRequest.postData` |
|
|
16
|
+
| `write-only-opaque` | Assignment works; reading returns an opaque CLR value instead of the assigned string (no throw) | read | `Platform.Response.ContentType`, `Platform.Response.CharacterSet` |
|
|
17
|
+
| `read-only` | Reading works; assignment is silently ineffective | write | `Platform.Request.Browser`, `ClientIP`, `HasSSL`, `IsSSL`, `Method`, `QueryString`, `ReferrerURL`, `RequestURL`, `UserAgent` |
|
|
18
|
+
|
|
19
|
+
## Detection
|
|
20
|
+
|
|
21
|
+
The rule tracks data flow for HTTP instances, so it only fires on a genuine request object:
|
|
22
|
+
|
|
23
|
+
1. A **request** variable is one assigned from `new Script.Util.HttpRequest(...)` or
|
|
24
|
+
`Script.Util.HttpGet(...)`. A property read on an untracked variable is ignored.
|
|
25
|
+
2. `Platform.Request.*` / `Platform.Response.*` are matched on the literal member path.
|
|
26
|
+
3. A member expression that is the left-hand side of an assignment counts as a **write**;
|
|
27
|
+
any other member access counts as a **read**.
|
|
28
|
+
|
|
29
|
+
Related: [`sfmc/ssjs-no-property-call`](no-property-call.md) reports the *call* form of a
|
|
30
|
+
read-only write (`Platform.Request.Method("POST")`). This rule handles the *assignment*
|
|
31
|
+
form (`Platform.Request.Method = "POST"`), which that rule does not see. There is no
|
|
32
|
+
overlap.
|
|
33
|
+
|
|
34
|
+
## Examples
|
|
35
|
+
|
|
36
|
+
### ❌ Incorrect
|
|
37
|
+
|
|
38
|
+
```js
|
|
39
|
+
var req = new Script.Util.HttpRequest("https://api.example.com/data");
|
|
40
|
+
req.postData = Stringify(payload);
|
|
41
|
+
Write(req.postData); // ← throws and aborts the page
|
|
42
|
+
|
|
43
|
+
var contentType = Platform.Response.ContentType; // ← opaque CLR value, not the assigned string
|
|
44
|
+
|
|
45
|
+
Platform.Request.Method = "POST"; // ← read-only, has no effect
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### ✅ Correct
|
|
49
|
+
|
|
50
|
+
```js
|
|
51
|
+
var req = new Script.Util.HttpRequest("https://api.example.com/data");
|
|
52
|
+
var body = Stringify(payload);
|
|
53
|
+
req.postData = body;
|
|
54
|
+
Write(body); // keep your own variable
|
|
55
|
+
|
|
56
|
+
Platform.Response.ContentType = "application/json";
|
|
57
|
+
|
|
58
|
+
var method = String(Platform.Request.Method);
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Fix
|
|
62
|
+
|
|
63
|
+
Not fixable and no suggestions — the correct fix is to keep the value in your own
|
|
64
|
+
JavaScript variable (or to drop the ineffective assignment), which is a refactor rather
|
|
65
|
+
than a mechanical edit.
|
|
66
|
+
|
|
67
|
+
## Rule details
|
|
68
|
+
|
|
69
|
+
- **Type:** `problem`
|
|
70
|
+
- **Fixable:** No
|
|
71
|
+
- **Recommended:** Yes (`error`)
|
|
72
|
+
- **Strict:** Yes (`error`)
|
|
73
|
+
- **MCN:** Off (SSJS is not supported on Marketing Cloud Next)
|
|
@@ -9,6 +9,11 @@ The rule is fixable:
|
|
|
9
9
|
- **Single-arg calls on `Platform.Response.*`** (`ContentType("text/html")`) → rewrites to an assignment (`= value`), including when the call appears in a comma (`SequenceExpression`) expression.
|
|
10
10
|
- **Any-arg calls on `Platform.Request.*`** → reported as an error with no auto-fix, because `Request` properties are read-only.
|
|
11
11
|
|
|
12
|
+
This rule only sees the **call** form. The assignment form of the same mistake
|
|
13
|
+
(`Platform.Request.Method = "POST"`), and reads that return nothing useful
|
|
14
|
+
(`var ct = Platform.Response.ContentType`), are handled by
|
|
15
|
+
[`sfmc/ssjs-no-invalid-property-access`](no-invalid-property-access.md).
|
|
16
|
+
|
|
12
17
|
## Properties affected
|
|
13
18
|
|
|
14
19
|
### `Platform.Request` — read-only
|
|
@@ -25,7 +30,7 @@ The rule is fixable:
|
|
|
25
30
|
| `RequestURL` |
|
|
26
31
|
| `UserAgent` |
|
|
27
32
|
|
|
28
|
-
### `Platform.Response` —
|
|
33
|
+
### `Platform.Response` — writable (reads return an opaque value)
|
|
29
34
|
|
|
30
35
|
| Property |
|
|
31
36
|
|---|
|
|
@@ -82,7 +87,6 @@ Platform.Request.Method("POST"); // ← read-only, cannot be set
|
|
|
82
87
|
// Reading properties — no parentheses
|
|
83
88
|
var method = Platform.Request.Method;
|
|
84
89
|
var ip = Platform.Request.ClientIP;
|
|
85
|
-
var ct = Platform.Response.ContentType;
|
|
86
90
|
|
|
87
91
|
// Setting writable Response properties — use assignment
|
|
88
92
|
Platform.Response.ContentType = "application/json";
|
|
@@ -51,6 +51,14 @@ What the fix does:
|
|
|
51
51
|
- When the version argument is present but wrong: replaces the version string literal with the required version.
|
|
52
52
|
- When the version argument is missing entirely: inserts `, "1.1.5"` after the `"core"` argument.
|
|
53
53
|
|
|
54
|
+
### Files using `ErrorUtil` are reported but not fixed
|
|
55
|
+
|
|
56
|
+
`ErrorUtil` is provided only by `Platform.Load("Core", "1")` — under any newer version it is
|
|
57
|
+
`undefined` and calling it throws a `TypeError`. When the file contains an `ErrorUtil` usage,
|
|
58
|
+
the version is still reported but **no fix is attached**, so `eslint --fix` cannot silently
|
|
59
|
+
break working code. Migrate off `ErrorUtil` first (see
|
|
60
|
+
[`sfmc/ssjs-no-deprecated-function`](./no-deprecated-function.md)), then bump the version.
|
|
61
|
+
|
|
54
62
|
## Configuration Example
|
|
55
63
|
|
|
56
64
|
```js
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-sfmc",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.11.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",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"eslint-plugin-mso-email": "^2.0.0",
|
|
43
43
|
"handlebars-data": "^0.3.1",
|
|
44
44
|
"mso-conditional-parser": "^2.0.1",
|
|
45
|
-
"ssjs-data": "^1.
|
|
45
|
+
"ssjs-data": "^1.5.0"
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {
|
|
48
48
|
"eslint": ">=9.0.0"
|
package/src/index.js
CHANGED
|
@@ -61,6 +61,7 @@ import ssjsNoNonfunctionalMethod from './rules/ssjs/ssjs-no-nonfunctional-method
|
|
|
61
61
|
import ssjsNoClrHeaderAccess from './rules/ssjs/no-clr-header-access.js';
|
|
62
62
|
import ssjsRequireStringClrContent from './rules/ssjs/require-string-clr-content.js';
|
|
63
63
|
import ssjsHttpPropertyValue from './rules/ssjs/http-property-value.js';
|
|
64
|
+
import ssjsNoInvalidPropertyAccess from './rules/ssjs/no-invalid-property-access.js';
|
|
64
65
|
|
|
65
66
|
// ── Handlebars (MCN) rules ──────────────────────────────────────────────────────
|
|
66
67
|
|
|
@@ -145,6 +146,7 @@ const plugin = {
|
|
|
145
146
|
'ssjs-no-clr-header-access': ssjsNoClrHeaderAccess,
|
|
146
147
|
'ssjs-require-string-clr-content': ssjsRequireStringClrContent,
|
|
147
148
|
'ssjs-http-property-value': ssjsHttpPropertyValue,
|
|
149
|
+
'ssjs-no-invalid-property-access': ssjsNoInvalidPropertyAccess,
|
|
148
150
|
|
|
149
151
|
// Handlebars (MCN) rules (hbs- prefix)
|
|
150
152
|
'hbs-no-unknown-helper': hbsNoUnknownHelper,
|
|
@@ -193,6 +195,7 @@ const ssjsMcnRules = {
|
|
|
193
195
|
'sfmc/ssjs-no-clr-header-access': 'off',
|
|
194
196
|
'sfmc/ssjs-require-string-clr-content': 'off',
|
|
195
197
|
'sfmc/ssjs-http-property-value': 'off',
|
|
198
|
+
'sfmc/ssjs-no-invalid-property-access': 'off',
|
|
196
199
|
'no-cond-assign': 'off',
|
|
197
200
|
};
|
|
198
201
|
|
|
@@ -266,6 +269,7 @@ const ssjsRecommendedRules = {
|
|
|
266
269
|
'sfmc/ssjs-no-clr-header-access': 'error',
|
|
267
270
|
'sfmc/ssjs-require-string-clr-content': 'error',
|
|
268
271
|
'sfmc/ssjs-http-property-value': 'error',
|
|
272
|
+
'sfmc/ssjs-no-invalid-property-access': 'error',
|
|
269
273
|
'no-cond-assign': 'error',
|
|
270
274
|
};
|
|
271
275
|
|
|
@@ -292,6 +296,7 @@ const ssjsStrictRules = {
|
|
|
292
296
|
'sfmc/ssjs-no-clr-header-access': 'error',
|
|
293
297
|
'sfmc/ssjs-require-string-clr-content': 'error',
|
|
294
298
|
'sfmc/ssjs-http-property-value': 'error',
|
|
299
|
+
'sfmc/ssjs-no-invalid-property-access': 'error',
|
|
295
300
|
'no-cond-assign': 'error',
|
|
296
301
|
};
|
|
297
302
|
|
|
@@ -12,7 +12,9 @@
|
|
|
12
12
|
* both static (`Portfolio.Retrieve(...)`) and instance
|
|
13
13
|
* (`var p = Portfolio.Init(...); p.Update(...)`) call styles, including
|
|
14
14
|
* multi-part static/instance paths (`Send.Definition.Add(...)`).
|
|
15
|
-
* - ErrorUtil.ThrowWSProxyError(...) — deprecated; only exists under Platform.Load("Core", "1")
|
|
15
|
+
* - ErrorUtil.ThrowWSProxyError(...) — deprecated; only exists under Platform.Load("Core", "1").
|
|
16
|
+
* When the file loads a Core version above the entry's `maxCoreVersion` the call is
|
|
17
|
+
* undefined at runtime, so the stronger `unavailableInCoreVersion` message is used.
|
|
16
18
|
*/
|
|
17
19
|
|
|
18
20
|
import {
|
|
@@ -21,6 +23,7 @@ import {
|
|
|
21
23
|
coreObjectNames,
|
|
22
24
|
coreDeprecatedMethodLookup,
|
|
23
25
|
ERROR_UTIL_METHODS,
|
|
26
|
+
maxCoreVersionLookup,
|
|
24
27
|
} from 'ssjs-data';
|
|
25
28
|
|
|
26
29
|
// Lookup Map: lowercase name → entry, for SSJS_GLOBALS entries that are deprecated.
|
|
@@ -93,6 +96,77 @@ function getCoreInitType(node) {
|
|
|
93
96
|
return objectPath && coreObjectNames.has(objectPath) ? objectPath : null;
|
|
94
97
|
}
|
|
95
98
|
|
|
99
|
+
/**
|
|
100
|
+
* Detect `Platform.Load("core", …)` and return the version literal it loads.
|
|
101
|
+
*
|
|
102
|
+
* @param {object} node - a CallExpression node
|
|
103
|
+
* @returns {string|null} the version string, '' when the version argument is
|
|
104
|
+
* missing or not a string literal, or null when this is not a core load
|
|
105
|
+
*/
|
|
106
|
+
function getCoreLoadVersion(node) {
|
|
107
|
+
const callee = node.callee;
|
|
108
|
+
if (
|
|
109
|
+
callee.type !== 'MemberExpression' ||
|
|
110
|
+
callee.object.type !== 'Identifier' ||
|
|
111
|
+
callee.object.name !== 'Platform' ||
|
|
112
|
+
callee.property.type !== 'Identifier' ||
|
|
113
|
+
callee.property.name !== 'Load'
|
|
114
|
+
) {
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
const arguments_ = node.arguments;
|
|
118
|
+
if (
|
|
119
|
+
arguments_.length === 0 ||
|
|
120
|
+
arguments_[0].type !== 'Literal' ||
|
|
121
|
+
typeof arguments_[0].value !== 'string' ||
|
|
122
|
+
arguments_[0].value.toLowerCase() !== 'core'
|
|
123
|
+
) {
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
const versionArgument = arguments_[1];
|
|
127
|
+
if (
|
|
128
|
+
versionArgument &&
|
|
129
|
+
versionArgument.type === 'Literal' &&
|
|
130
|
+
typeof versionArgument.value === 'string'
|
|
131
|
+
) {
|
|
132
|
+
return versionArgument.value;
|
|
133
|
+
}
|
|
134
|
+
return '';
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Split a Core version string into three numeric segments, padding missing
|
|
139
|
+
* ones with 0 so "1" and "1.0.0" are equivalent.
|
|
140
|
+
*
|
|
141
|
+
* @param {string} version - version string, e.g. "1" or "1.1.5"
|
|
142
|
+
* @returns {number[]} three numeric segments
|
|
143
|
+
*/
|
|
144
|
+
function parseCoreVersion(version) {
|
|
145
|
+
const parts = version.split('.').map((p) => Number(p) || 0);
|
|
146
|
+
while (parts.length < 3) {
|
|
147
|
+
parts.push(0);
|
|
148
|
+
}
|
|
149
|
+
return parts;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Compare two Core version strings ("1", "1.1.5", …) numerically.
|
|
154
|
+
*
|
|
155
|
+
* @param {string} a - left version
|
|
156
|
+
* @param {string} b - right version
|
|
157
|
+
* @returns {number} negative when a < b, 0 when equal, positive when a > b
|
|
158
|
+
*/
|
|
159
|
+
function compareCoreVersions(a, b) {
|
|
160
|
+
const left = parseCoreVersion(a);
|
|
161
|
+
const right = parseCoreVersion(b);
|
|
162
|
+
for (const [index, element] of left.entries()) {
|
|
163
|
+
if (element !== right[index]) {
|
|
164
|
+
return element - right[index];
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return 0;
|
|
168
|
+
}
|
|
169
|
+
|
|
96
170
|
/**
|
|
97
171
|
* Extract the trailing "Deprecated — ..." sentence from a deprecated
|
|
98
172
|
* method's `description` so the message surfaces each class's specific
|
|
@@ -121,6 +195,8 @@ export default {
|
|
|
121
195
|
"'{{method}}' called on a {{className}} variable is deprecated. {{note}}",
|
|
122
196
|
deprecatedErrorUtil:
|
|
123
197
|
"'ErrorUtil.{{name}}' is deprecated — it only exists under Platform.Load(\"Core\", \"1\") and is undefined in newer Core versions. Check 'result.Status' and 'throw new Error(...)' instead.",
|
|
198
|
+
unavailableInCoreVersion:
|
|
199
|
+
'\'ErrorUtil.{{name}}\' is undefined under Platform.Load("Core", "{{version}}") — it only exists in Core version "{{max}}", so this call throws a TypeError at runtime. Check \'result.Status\' and \'throw new Error(...)\' instead.',
|
|
124
200
|
},
|
|
125
201
|
schema: [],
|
|
126
202
|
},
|
|
@@ -129,6 +205,11 @@ export default {
|
|
|
129
205
|
// Track variable names assigned via <CoreClass>.Init(...) → resolved class name,
|
|
130
206
|
// e.g. `var p = Portfolio.Init(...)` → coreVariables.set('p', 'Portfolio').
|
|
131
207
|
const coreVariables = new Map();
|
|
208
|
+
// Every Platform.Load("core", <version>) in the file, as { line, version }.
|
|
209
|
+
const coreLoads = [];
|
|
210
|
+
// ErrorUtil call sites, resolved to the applicable Core version on Program:exit
|
|
211
|
+
// (a load may appear after the usage in source order).
|
|
212
|
+
const errorUtilityUsages = [];
|
|
132
213
|
|
|
133
214
|
return {
|
|
134
215
|
VariableDeclaration(node) {
|
|
@@ -160,6 +241,13 @@ export default {
|
|
|
160
241
|
CallExpression(node) {
|
|
161
242
|
const callee = node.callee;
|
|
162
243
|
|
|
244
|
+
// ── Platform.Load("core", <version>) — remember for ErrorUtil ──
|
|
245
|
+
const loadedVersion = getCoreLoadVersion(node);
|
|
246
|
+
if (loadedVersion !== null) {
|
|
247
|
+
coreLoads.push({ line: node.loc.start.line, version: loadedVersion });
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
|
|
163
251
|
// ── Bare globals: ContentArea(…) and ContentAreaByName(…) ──────
|
|
164
252
|
if (callee.type === 'Identifier') {
|
|
165
253
|
const entry = DEPRECATED_GLOBALS.get(callee.name.toLowerCase());
|
|
@@ -203,16 +291,14 @@ export default {
|
|
|
203
291
|
}
|
|
204
292
|
|
|
205
293
|
// ── ErrorUtil.ThrowWSProxyError(…) — deprecated ───────────────
|
|
294
|
+
// Reported on Program:exit: the wording depends on the loaded Core
|
|
295
|
+
// version, which may be declared after this call site.
|
|
206
296
|
if (
|
|
207
297
|
callee.object.type === 'Identifier' &&
|
|
208
298
|
callee.object.name === 'ErrorUtil' &&
|
|
209
299
|
ERRORUTIL_DEPRECATED.has(methodName.toLowerCase())
|
|
210
300
|
) {
|
|
211
|
-
|
|
212
|
-
node: property,
|
|
213
|
-
messageId: 'deprecatedErrorUtil',
|
|
214
|
-
data: { name: methodName },
|
|
215
|
-
});
|
|
301
|
+
errorUtilityUsages.push({ node: property, name: methodName });
|
|
216
302
|
return;
|
|
217
303
|
}
|
|
218
304
|
|
|
@@ -271,6 +357,44 @@ export default {
|
|
|
271
357
|
});
|
|
272
358
|
}
|
|
273
359
|
},
|
|
360
|
+
|
|
361
|
+
'Program:exit'() {
|
|
362
|
+
const maxCoreVersion = maxCoreVersionLookup.get('errorutil')?.maxCoreVersion;
|
|
363
|
+
for (const usage of errorUtilityUsages) {
|
|
364
|
+
// The applicable load is the nearest preceding one; when the file
|
|
365
|
+
// only loads Core after the usage, that load still governs at
|
|
366
|
+
// runtime, so fall back to the first load in the file.
|
|
367
|
+
const usageLine = usage.node.loc.start.line;
|
|
368
|
+
const preceding = coreLoads.filter((l) => l.line <= usageLine);
|
|
369
|
+
const applicable = preceding.length > 0 ? preceding.at(-1) : coreLoads[0];
|
|
370
|
+
|
|
371
|
+
if (
|
|
372
|
+
maxCoreVersion &&
|
|
373
|
+
applicable &&
|
|
374
|
+
applicable.version &&
|
|
375
|
+
compareCoreVersions(applicable.version, maxCoreVersion) > 0
|
|
376
|
+
) {
|
|
377
|
+
context.report({
|
|
378
|
+
node: usage.node,
|
|
379
|
+
messageId: 'unavailableInCoreVersion',
|
|
380
|
+
data: {
|
|
381
|
+
name: usage.name,
|
|
382
|
+
version: applicable.version,
|
|
383
|
+
max: maxCoreVersion,
|
|
384
|
+
},
|
|
385
|
+
});
|
|
386
|
+
continue;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
// No core load at all, no version argument, or a version within the
|
|
390
|
+
// supported range — ssjs-require-platform-load owns the missing-load case.
|
|
391
|
+
context.report({
|
|
392
|
+
node: usage.node,
|
|
393
|
+
messageId: 'deprecatedErrorUtil',
|
|
394
|
+
data: { name: usage.name },
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
},
|
|
274
398
|
};
|
|
275
399
|
},
|
|
276
400
|
};
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rule: ssjs-no-invalid-property-access
|
|
3
|
+
*
|
|
4
|
+
* Flags property accesses that go against the direction the runtime supports.
|
|
5
|
+
* Three distinct runtime behaviours, all driven by the `access` field in
|
|
6
|
+
* ssjs-data (`propertyAccessLookup`) so flagging a future property is a
|
|
7
|
+
* one-field data change:
|
|
8
|
+
*
|
|
9
|
+
* 1. write-only — assignment works, reading THROWS:
|
|
10
|
+
* req.postData = body; // fine
|
|
11
|
+
* Write(req.postData); // throws "Property Get method was not
|
|
12
|
+
* // found." and aborts the whole page
|
|
13
|
+
*
|
|
14
|
+
* 2. write-only-opaque — assignment works, reading returns an opaque CLR
|
|
15
|
+
* value instead of the assigned string (no throw):
|
|
16
|
+
* Platform.Response.ContentType = "application/json"; // fine
|
|
17
|
+
* var ct = Platform.Response.ContentType; // opaque value
|
|
18
|
+
*
|
|
19
|
+
* 3. read-only — reading works, assignment is silently ineffective:
|
|
20
|
+
* var m = Platform.Request.Method; // fine
|
|
21
|
+
* Platform.Request.Method = "POST"; // no effect
|
|
22
|
+
*
|
|
23
|
+
* Note `ssjs-no-property-call` already reports the CALL form of a read-only
|
|
24
|
+
* write (`Platform.Request.Method("POST")`). This rule handles the ASSIGNMENT
|
|
25
|
+
* form, which that rule does not see. There is no overlap.
|
|
26
|
+
*
|
|
27
|
+
* Not fixable — the correct fix (keep the value in your own variable) is a
|
|
28
|
+
* refactor, not a mechanical edit.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
import { propertyAccessLookup } from 'ssjs-data';
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Returns true when the node is `Script.Util.HttpRequest` or
|
|
35
|
+
* `Script.Util.HttpGet` (a MemberExpression, optionally the callee of `new`).
|
|
36
|
+
*
|
|
37
|
+
* @param {import('eslint').Rule.Node} node - MemberExpression to test
|
|
38
|
+
* @returns {boolean} Whether it references a Script.Util HTTP constructor
|
|
39
|
+
*/
|
|
40
|
+
function isHttpConstructorMember(node) {
|
|
41
|
+
return (
|
|
42
|
+
node.type === 'MemberExpression' &&
|
|
43
|
+
node.property.type === 'Identifier' &&
|
|
44
|
+
(node.property.name === 'HttpRequest' || node.property.name === 'HttpGet') &&
|
|
45
|
+
node.object.type === 'MemberExpression' &&
|
|
46
|
+
node.object.property.type === 'Identifier' &&
|
|
47
|
+
node.object.property.name === 'Util' &&
|
|
48
|
+
node.object.object.type === 'Identifier' &&
|
|
49
|
+
node.object.object.name === 'Script'
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Returns true when the node constructs a Script.Util HTTP request, i.e.
|
|
55
|
+
* `new Script.Util.HttpRequest(...)` or `Script.Util.HttpGet(...)`.
|
|
56
|
+
*
|
|
57
|
+
* @param {import('eslint').Rule.Node} node - init expression of a declarator
|
|
58
|
+
* @returns {boolean} Whether the expression yields an HTTP request instance
|
|
59
|
+
*/
|
|
60
|
+
function isHttpRequestInit(node) {
|
|
61
|
+
if (!node) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
if (node.type === 'NewExpression' || node.type === 'CallExpression') {
|
|
65
|
+
return isHttpConstructorMember(node.callee);
|
|
66
|
+
}
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Resolves the owner namespace of a member expression's object, but only for
|
|
72
|
+
* the shapes this rule tracks: a `Platform.Request` / `Platform.Response`
|
|
73
|
+
* member expression, or an identifier holding a Script.Util HTTP instance.
|
|
74
|
+
*
|
|
75
|
+
* @param {import('eslint').Rule.Node} objectNode - the `object` of the MemberExpression
|
|
76
|
+
* @param {Set.<string>} requestVariables - identifiers holding an HTTP request instance
|
|
77
|
+
* @returns {string|null} Qualified owner name, or null when not tracked
|
|
78
|
+
*/
|
|
79
|
+
function resolveOwner(objectNode, requestVariables) {
|
|
80
|
+
if (objectNode.type === 'Identifier') {
|
|
81
|
+
return requestVariables.has(objectNode.name) ? 'Script.Util.HttpRequest' : null;
|
|
82
|
+
}
|
|
83
|
+
if (
|
|
84
|
+
objectNode.type === 'MemberExpression' &&
|
|
85
|
+
!objectNode.computed &&
|
|
86
|
+
objectNode.object.type === 'Identifier' &&
|
|
87
|
+
objectNode.object.name === 'Platform' &&
|
|
88
|
+
objectNode.property.type === 'Identifier'
|
|
89
|
+
) {
|
|
90
|
+
const ns = objectNode.property.name;
|
|
91
|
+
if (ns === 'Request' || ns === 'Response') {
|
|
92
|
+
return `Platform.${ns}`;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Looks up the access restriction for a member expression, or null when the
|
|
100
|
+
* accessed member has no restriction (or is not on a tracked owner).
|
|
101
|
+
*
|
|
102
|
+
* @param {import('eslint').Rule.Node} node - the MemberExpression being accessed
|
|
103
|
+
* @param {Set.<string>} requestVariables - identifiers holding an HTTP request instance
|
|
104
|
+
* @returns {{name: string, owner: string, access: string}|null} the matching entry or null
|
|
105
|
+
*/
|
|
106
|
+
function lookupAccess(node, requestVariables) {
|
|
107
|
+
if (node.computed || node.property.type !== 'Identifier') {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
const owner = resolveOwner(node.object, requestVariables);
|
|
111
|
+
if (!owner) {
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
return propertyAccessLookup.get(`${owner}.${node.property.name}`.toLowerCase()) ?? null;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Returns true when the member expression is the target of an assignment
|
|
119
|
+
* (`x.y = …`, including compound assignments such as `x.y += …`).
|
|
120
|
+
*
|
|
121
|
+
* @param {import('eslint').Rule.Node} node - the MemberExpression to test
|
|
122
|
+
* @returns {boolean} Whether the node is an assignment target
|
|
123
|
+
*/
|
|
124
|
+
function isAssignmentTarget(node) {
|
|
125
|
+
return node.parent.type === 'AssignmentExpression' && node.parent.left === node;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const MESSAGE_BY_ACCESS = {
|
|
129
|
+
'write-only': 'writeOnlyRead',
|
|
130
|
+
'write-only-opaque': 'opaqueRead',
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
export default {
|
|
134
|
+
meta: {
|
|
135
|
+
type: 'problem',
|
|
136
|
+
docs: {
|
|
137
|
+
description:
|
|
138
|
+
'Disallow reading write-only SSJS properties (e.g. postData) and assigning to read-only ones (e.g. Platform.Request.Method)',
|
|
139
|
+
},
|
|
140
|
+
messages: {
|
|
141
|
+
writeOnlyRead:
|
|
142
|
+
"'{{owner}}.{{name}}' is write-only. Reading it throws " +
|
|
143
|
+
'"Property Get method was not found." at runtime — outside a try/catch that throw ' +
|
|
144
|
+
'aborts the whole page. Keep the value in your own variable instead.',
|
|
145
|
+
opaqueRead:
|
|
146
|
+
"'{{owner}}.{{name}}' does not read back the value you assigned — the runtime " +
|
|
147
|
+
'returns an opaque CLR value. Keep the value in your own variable instead.',
|
|
148
|
+
readOnlyWrite: "'{{owner}}.{{name}}' is read-only. Assigning to it has no effect.",
|
|
149
|
+
},
|
|
150
|
+
schema: [],
|
|
151
|
+
},
|
|
152
|
+
|
|
153
|
+
create(context) {
|
|
154
|
+
const requestVariables = new Set();
|
|
155
|
+
const pending = [];
|
|
156
|
+
|
|
157
|
+
return {
|
|
158
|
+
VariableDeclarator(node) {
|
|
159
|
+
if (node.id.type === 'Identifier' && isHttpRequestInit(node.init)) {
|
|
160
|
+
requestVariables.add(node.id.name);
|
|
161
|
+
}
|
|
162
|
+
},
|
|
163
|
+
|
|
164
|
+
MemberExpression(node) {
|
|
165
|
+
const entry = lookupAccess(node, requestVariables);
|
|
166
|
+
if (!entry) {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
// Reads of a write-only property: the node must NOT be the
|
|
170
|
+
// left-hand side of an assignment — that direction is allowed.
|
|
171
|
+
// The call form (`Platform.Response.ContentType()`) belongs to
|
|
172
|
+
// `ssjs-no-property-call`, so it is skipped here.
|
|
173
|
+
if (
|
|
174
|
+
entry.access === 'read-only' ||
|
|
175
|
+
isAssignmentTarget(node) ||
|
|
176
|
+
(node.parent.type === 'CallExpression' && node.parent.callee === node)
|
|
177
|
+
) {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
pending.push({ node, entry, messageId: MESSAGE_BY_ACCESS[entry.access] });
|
|
181
|
+
},
|
|
182
|
+
|
|
183
|
+
AssignmentExpression(node) {
|
|
184
|
+
if (node.left.type !== 'MemberExpression') {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
const entry = lookupAccess(node.left, requestVariables);
|
|
188
|
+
if (!entry || entry.access !== 'read-only') {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
pending.push({ node: node.left, entry, messageId: 'readOnlyWrite' });
|
|
192
|
+
},
|
|
193
|
+
|
|
194
|
+
'Program:exit'() {
|
|
195
|
+
for (const { node, entry, messageId } of pending) {
|
|
196
|
+
context.report({
|
|
197
|
+
node,
|
|
198
|
+
messageId,
|
|
199
|
+
data: { owner: entry.owner, name: entry.name },
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
},
|
|
203
|
+
};
|
|
204
|
+
},
|
|
205
|
+
};
|
|
@@ -4,6 +4,10 @@
|
|
|
4
4
|
* Warns when Platform.Load("Core", version) uses a version string other
|
|
5
5
|
* than the recommended "1.1.5". Older versions like "1" or "1.1.1" miss
|
|
6
6
|
* bug-fixes and features available in the latest Core library release.
|
|
7
|
+
*
|
|
8
|
+
* The autofix is withheld for files that use `ErrorUtil`, which only exists
|
|
9
|
+
* under Core "1" — bumping the version there would turn working code into a
|
|
10
|
+
* runtime TypeError. Such files must migrate off ErrorUtil first.
|
|
7
11
|
*/
|
|
8
12
|
|
|
9
13
|
const DEFAULT_VERSION = '1.1.5';
|
|
@@ -36,8 +40,18 @@ export default {
|
|
|
36
40
|
create(context) {
|
|
37
41
|
const options = context.options[0] || {};
|
|
38
42
|
const expectedVersion = options.version || DEFAULT_VERSION;
|
|
43
|
+
// Pending reports — resolved on Program:exit so the ErrorUtil scan is
|
|
44
|
+
// complete before deciding whether to attach a fixer.
|
|
45
|
+
const pendingReports = [];
|
|
46
|
+
let isUsesErrorUtility = false;
|
|
39
47
|
|
|
40
48
|
return {
|
|
49
|
+
MemberExpression(node) {
|
|
50
|
+
if (node.object.type === 'Identifier' && node.object.name === 'ErrorUtil') {
|
|
51
|
+
isUsesErrorUtility = true;
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
|
|
41
55
|
CallExpression(node) {
|
|
42
56
|
const callee = node.callee;
|
|
43
57
|
if (
|
|
@@ -61,13 +75,11 @@ export default {
|
|
|
61
75
|
}
|
|
62
76
|
|
|
63
77
|
if (arguments_.length < 2) {
|
|
64
|
-
|
|
78
|
+
pendingReports.push({
|
|
65
79
|
node,
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
return fixer.insertTextAfter(arguments_[0], `, "${expectedVersion}"`);
|
|
70
|
-
},
|
|
80
|
+
actual: '(none)',
|
|
81
|
+
fix: (fixer) =>
|
|
82
|
+
fixer.insertTextAfter(arguments_[0], `, "${expectedVersion}"`),
|
|
71
83
|
});
|
|
72
84
|
return;
|
|
73
85
|
}
|
|
@@ -78,13 +90,23 @@ export default {
|
|
|
78
90
|
typeof versionArgument.value === 'string' &&
|
|
79
91
|
versionArgument.value !== expectedVersion
|
|
80
92
|
) {
|
|
81
|
-
|
|
93
|
+
pendingReports.push({
|
|
82
94
|
node: versionArgument,
|
|
95
|
+
actual: versionArgument.value,
|
|
96
|
+
fix: (fixer) => fixer.replaceText(versionArgument, `"${expectedVersion}"`),
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
|
|
101
|
+
'Program:exit'() {
|
|
102
|
+
for (const report of pendingReports) {
|
|
103
|
+
context.report({
|
|
104
|
+
node: report.node,
|
|
83
105
|
messageId: 'outdatedVersion',
|
|
84
|
-
data: { actual:
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
},
|
|
106
|
+
data: { actual: report.actual, expected: expectedVersion },
|
|
107
|
+
// Bumping the Core version would make ErrorUtil undefined at
|
|
108
|
+
// runtime, so the fix is withheld and only the report remains.
|
|
109
|
+
...(!isUsesErrorUtility && { fix: report.fix }),
|
|
88
110
|
});
|
|
89
111
|
}
|
|
90
112
|
},
|