eslint-plugin-sitecore-pagebuilder 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/LICENSE +21 -0
- package/README.md +336 -2
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +22 -0
- package/dist/index.js.map +1 -0
- package/dist/rules/no-conditional-field-render.d.ts +14 -0
- package/dist/rules/no-conditional-field-render.d.ts.map +1 -0
- package/dist/rules/no-conditional-field-render.js +68 -0
- package/dist/rules/no-conditional-field-render.js.map +1 -0
- package/dist/rules/no-empty-early-return.d.ts +16 -0
- package/dist/rules/no-empty-early-return.d.ts.map +1 -0
- package/dist/rules/no-empty-early-return.js +80 -0
- package/dist/rules/no-empty-early-return.js.map +1 -0
- package/dist/utils.d.ts +28 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +125 -0
- package/dist/utils.js.map +1 -0
- package/package.json +59 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.1
|
|
4
|
+
|
|
5
|
+
- Full README: what each rule catches and allows, options, messages, how to fix reports, how to disable a rule for one line, limitations and an FAQ.
|
|
6
|
+
- Build now works on Windows (replaced `rm -rf` with a Node one-liner).
|
|
7
|
+
|
|
8
|
+
## 0.1.0
|
|
9
|
+
|
|
10
|
+
First release.
|
|
11
|
+
|
|
12
|
+
- `no-conditional-field-render`: flags field components hidden behind a check on their own value.
|
|
13
|
+
- `no-empty-early-return`: flags components that return nothing for empty fields without handling Page Builder.
|
|
14
|
+
- `recommended` flat config.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Krushna Patel
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,337 @@
|
|
|
1
|
-
#
|
|
1
|
+
# eslint-plugin-sitecore-pagebuilder
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/eslint-plugin-sitecore-pagebuilder)
|
|
4
|
+
[](https://github.com/Krushna285/sitecore-pagebuilder-kit/blob/main/LICENSE)
|
|
5
|
+
|
|
6
|
+
ESLint rules for [Sitecore Content SDK](https://doc.sitecore.com/sai/en/developers/content-sdk/) projects that catch code which hides empty fields and components from content authors in Page Builder.
|
|
7
|
+
|
|
8
|
+
> Community package. Not affiliated with or endorsed by Sitecore.
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## Contents
|
|
13
|
+
|
|
14
|
+
- [The problem](#the-problem)
|
|
15
|
+
- [Requirements](#requirements)
|
|
16
|
+
- [Installation](#installation)
|
|
17
|
+
- [Configuration](#configuration)
|
|
18
|
+
- [Rules](#rules)
|
|
19
|
+
- [no-conditional-field-render](#no-conditional-field-render)
|
|
20
|
+
- [no-empty-early-return](#no-empty-early-return)
|
|
21
|
+
- [Fixing what the rules report](#fixing-what-the-rules-report)
|
|
22
|
+
- [Turning a rule off for one line](#turning-a-rule-off-for-one-line)
|
|
23
|
+
- [Limitations](#limitations)
|
|
24
|
+
- [FAQ](#faq)
|
|
25
|
+
- [License](#license)
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## The problem
|
|
30
|
+
|
|
31
|
+
This is a very common way to write a Sitecore component:
|
|
32
|
+
|
|
33
|
+
```tsx
|
|
34
|
+
{fields.title.value && <Text field={fields.title} tag="h2" />}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
It reads as "only show the title if there is one", and on the live site that's exactly what happens.
|
|
38
|
+
|
|
39
|
+
In Page Builder, though, a freshly added component has an empty title. The condition is false, so `<Text>` never renders, and neither does the "click to add text" placeholder the SDK would have shown. The author sees nothing to click and can't fill the field in.
|
|
40
|
+
|
|
41
|
+
This kind of bug gets past code review because the code looks reasonable, and it works in every environment developers usually test in. It tends to show up weeks later as a ticket from an author.
|
|
42
|
+
|
|
43
|
+
This plugin catches it in your editor, as you type.
|
|
44
|
+
|
|
45
|
+
## Requirements
|
|
46
|
+
|
|
47
|
+
- ESLint **9** or later, with the flat config format (`eslint.config.js` / `.mjs`). Content SDK starter apps use flat config by default.
|
|
48
|
+
- Node.js 18.18 or later.
|
|
49
|
+
- To lint `.tsx` files you need a TypeScript parser, such as `typescript-eslint`, which Content SDK apps already include.
|
|
50
|
+
|
|
51
|
+
## Installation
|
|
52
|
+
|
|
53
|
+
```sh
|
|
54
|
+
npm install --save-dev eslint-plugin-sitecore-pagebuilder
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
```sh
|
|
58
|
+
yarn add --dev eslint-plugin-sitecore-pagebuilder
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
```sh
|
|
62
|
+
pnpm add -D eslint-plugin-sitecore-pagebuilder
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Configuration
|
|
66
|
+
|
|
67
|
+
### Recommended setup
|
|
68
|
+
|
|
69
|
+
Add the recommended config to your `eslint.config.mjs`. It turns on both rules as warnings.
|
|
70
|
+
|
|
71
|
+
```js
|
|
72
|
+
// eslint.config.mjs
|
|
73
|
+
import pagebuilder from 'eslint-plugin-sitecore-pagebuilder';
|
|
74
|
+
|
|
75
|
+
export default [
|
|
76
|
+
// ...your existing config (Next.js, TypeScript, etc.)
|
|
77
|
+
pagebuilder.configs.recommended,
|
|
78
|
+
];
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
That's all you need. Run `npx eslint .` or check your editor.
|
|
82
|
+
|
|
83
|
+
### Only lint your component folder
|
|
84
|
+
|
|
85
|
+
```js
|
|
86
|
+
export default [
|
|
87
|
+
// ...
|
|
88
|
+
{
|
|
89
|
+
...pagebuilder.configs.recommended,
|
|
90
|
+
files: ['src/components/**/*.{jsx,tsx}'],
|
|
91
|
+
},
|
|
92
|
+
];
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Choose rules and severity yourself
|
|
96
|
+
|
|
97
|
+
```js
|
|
98
|
+
import pagebuilder from 'eslint-plugin-sitecore-pagebuilder';
|
|
99
|
+
|
|
100
|
+
export default [
|
|
101
|
+
{
|
|
102
|
+
plugins: { 'sitecore-pagebuilder': pagebuilder },
|
|
103
|
+
rules: {
|
|
104
|
+
'sitecore-pagebuilder/no-conditional-field-render': 'error',
|
|
105
|
+
'sitecore-pagebuilder/no-empty-early-return': 'warn',
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
];
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Use `'error'` if you want CI to fail on these. The recommended config uses `'warn'` so adding the plugin to an existing project doesn't break the build.
|
|
112
|
+
|
|
113
|
+
## Rules
|
|
114
|
+
|
|
115
|
+
| Rule | What it catches | In `recommended` |
|
|
116
|
+
| --- | --- | --- |
|
|
117
|
+
| [`no-conditional-field-render`](#no-conditional-field-render) | A field component hidden behind a check on its own value | warn |
|
|
118
|
+
| [`no-empty-early-return`](#no-empty-early-return) | A component that returns nothing when a field is empty | warn |
|
|
119
|
+
|
|
120
|
+
---
|
|
121
|
+
|
|
122
|
+
### no-conditional-field-render
|
|
123
|
+
|
|
124
|
+
Reports a Content SDK field component (`Text`, `RichText`, `Image`, etc.) that's only rendered when its **own field** has a value.
|
|
125
|
+
|
|
126
|
+
#### Why
|
|
127
|
+
|
|
128
|
+
In Page Builder the SDK's field components render an editable placeholder for empty fields. A condition on the field's value removes the component before that placeholder can appear.
|
|
129
|
+
|
|
130
|
+
#### Examples of code this rule reports
|
|
131
|
+
|
|
132
|
+
```tsx
|
|
133
|
+
{fields.title.value && <Text field={fields.title} tag="h2" />}
|
|
134
|
+
|
|
135
|
+
{fields.image?.value?.src ? <Image field={fields.image} /> : null}
|
|
136
|
+
|
|
137
|
+
{!fields.cta.value.href ? null : <Link field={fields.cta} />}
|
|
138
|
+
|
|
139
|
+
{fields.title.value && (
|
|
140
|
+
<div className="card__title">
|
|
141
|
+
<Text field={fields.title} tag="h2" />
|
|
142
|
+
</div>
|
|
143
|
+
)}
|
|
144
|
+
|
|
145
|
+
// Destructured fields and TypeScript non-null assertions are understood too
|
|
146
|
+
const { title } = fields;
|
|
147
|
+
{title.value && <Text field={title} />}
|
|
148
|
+
{fields.title!.value && <Text field={fields.title!} />}
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
#### Examples of code this rule allows
|
|
152
|
+
|
|
153
|
+
```tsx
|
|
154
|
+
// No condition: the SDK handles empty fields itself
|
|
155
|
+
<Text field={fields.title} tag="h2" />
|
|
156
|
+
|
|
157
|
+
// A guard that knows about Page Builder
|
|
158
|
+
<FieldGuard field={fields.title}>
|
|
159
|
+
<div className="card__title"><Text field={fields.title} tag="h2" /></div>
|
|
160
|
+
</FieldGuard>
|
|
161
|
+
|
|
162
|
+
// The condition also checks for editing mode
|
|
163
|
+
{(page.mode.isEditing || fields.title.value) && <Text field={fields.title} />}
|
|
164
|
+
{(isEditing || fields.title.value) && <Text field={fields.title} />}
|
|
165
|
+
|
|
166
|
+
// The condition is about a *different* field (e.g. a checkbox toggle)
|
|
167
|
+
{fields.showTitle.value && <Text field={fields.title} />}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
A condition counts as editing-aware if it mentions `isEditing`, `isPageEditing`, `isEditMode` or `pageEditing`.
|
|
171
|
+
|
|
172
|
+
#### Options
|
|
173
|
+
|
|
174
|
+
| Option | Type | Default | Description |
|
|
175
|
+
| --- | --- | --- | --- |
|
|
176
|
+
| `components` | `string[]` | `[]` | Extra component names that render a field through a `field` prop, such as your own wrappers. They're added to the built-in list. |
|
|
177
|
+
|
|
178
|
+
Built-in components: `Text`, `RichText`, `Image`, `NextImage`, `Link`, `DateField`, `File`. Member expressions also match, so `<Sdk.Text>` counts as `Text`.
|
|
179
|
+
|
|
180
|
+
```js
|
|
181
|
+
'sitecore-pagebuilder/no-conditional-field-render': [
|
|
182
|
+
'warn',
|
|
183
|
+
{ components: ['Heading', 'CtaLink', 'ResponsiveImage'] },
|
|
184
|
+
],
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
#### Message
|
|
188
|
+
|
|
189
|
+
> 'fields.title' is hidden when empty, so authors can't fill it in Page Builder. Wrap it in <FieldGuard field={fields.title}> or render the field component without the condition.
|
|
190
|
+
|
|
191
|
+
---
|
|
192
|
+
|
|
193
|
+
### no-empty-early-return
|
|
194
|
+
|
|
195
|
+
Reports an `if` statement in a component that returns `null` (or `undefined`, `false`, `''`) because a field is empty, without checking for Page Builder first.
|
|
196
|
+
|
|
197
|
+
#### Why
|
|
198
|
+
|
|
199
|
+
On the live site, skipping a component with no content is fine. In Page Builder, the whole component disappears from the page. The author just added it and can't see it, select it or fill it in.
|
|
200
|
+
|
|
201
|
+
#### Examples of code this rule reports
|
|
202
|
+
|
|
203
|
+
```tsx
|
|
204
|
+
function Promo({ fields }) {
|
|
205
|
+
if (!fields.image.value.src) return null;
|
|
206
|
+
return <Image field={fields.image} />;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function RelatedArticles(props) {
|
|
210
|
+
if (!props.fields.items?.length) return null;
|
|
211
|
+
return <ul>...</ul>;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const Banner = ({ fields }) => {
|
|
215
|
+
if (!fields.heading.value) {
|
|
216
|
+
return null;
|
|
217
|
+
}
|
|
218
|
+
return <Text field={fields.heading} tag="h1" />;
|
|
219
|
+
};
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
#### Examples of code this rule allows
|
|
223
|
+
|
|
224
|
+
```tsx
|
|
225
|
+
// Checks the mode first
|
|
226
|
+
function Promo({ fields }) {
|
|
227
|
+
const { page } = useSitecore();
|
|
228
|
+
if (!page.mode.isEditing && !fields.image.value.src) return null;
|
|
229
|
+
return <Image field={fields.image} />;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// Missing datasource: a bare `fields` check is not reported.
|
|
233
|
+
// Prefer the SDK's withDatasourceCheck() for this case.
|
|
234
|
+
function Promo({ fields }) {
|
|
235
|
+
if (!fields) return null;
|
|
236
|
+
return <Image field={fields.image} />;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// Not a component: functions that don't return JSX are ignored
|
|
240
|
+
function getTitle(fields) {
|
|
241
|
+
if (!fields.title.value) return null;
|
|
242
|
+
return fields.title.value;
|
|
243
|
+
}
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
#### Options
|
|
247
|
+
|
|
248
|
+
None.
|
|
249
|
+
|
|
250
|
+
#### Message
|
|
251
|
+
|
|
252
|
+
> This component returns nothing when 'fields.image.value.src' is empty, so it disappears in Page Builder. Check page.mode.isEditing first, or keep rendering so authors can fill the field in.
|
|
253
|
+
|
|
254
|
+
---
|
|
255
|
+
|
|
256
|
+
## Fixing what the rules report
|
|
257
|
+
|
|
258
|
+
There are usually three fixes, roughly in order of preference:
|
|
259
|
+
|
|
260
|
+
1. **Remove the condition.** If the condition was only wrapping a bare `<Text>`, `<Image>` or `<Link>`, delete it. The SDK already renders nothing for empty fields on the live site.
|
|
261
|
+
|
|
262
|
+
2. **Use `FieldGuard`** from [`sitecore-pagebuilder-kit`](https://www.npmjs.com/package/sitecore-pagebuilder-kit) when there's wrapper markup you don't want left empty on the live site. It works in server components with no hook:
|
|
263
|
+
|
|
264
|
+
```tsx
|
|
265
|
+
import { FieldGuard } from 'sitecore-pagebuilder-kit';
|
|
266
|
+
|
|
267
|
+
<FieldGuard field={fields.title}>
|
|
268
|
+
<div className="card__title"><Text field={fields.title} tag="h2" /></div>
|
|
269
|
+
</FieldGuard>
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
3. **Check the editing mode yourself**, especially for early returns:
|
|
273
|
+
|
|
274
|
+
```tsx
|
|
275
|
+
const { page } = useSitecore();
|
|
276
|
+
if (!page.mode.isEditing && !fields.title.value) return null;
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
Or with the kit's client hook:
|
|
280
|
+
|
|
281
|
+
```tsx
|
|
282
|
+
import { useIsEditing } from 'sitecore-pagebuilder-kit/client';
|
|
283
|
+
|
|
284
|
+
const isEditing = useIsEditing();
|
|
285
|
+
if (!isEditing && !fields.title.value) return null;
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
## Turning a rule off for one line
|
|
289
|
+
|
|
290
|
+
Sometimes hiding a field is deliberate, for example in a component that's never placed through Page Builder. Disable the rule for that spot and leave a note explaining why:
|
|
291
|
+
|
|
292
|
+
```tsx
|
|
293
|
+
{/* eslint-disable-next-line sitecore-pagebuilder/no-conditional-field-render -- footer is not authorable in Page Builder */}
|
|
294
|
+
{fields.copyright.value && <Text field={fields.copyright} />}
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
```tsx
|
|
298
|
+
// eslint-disable-next-line sitecore-pagebuilder/no-empty-early-return -- rendered by a listing page, not placed by authors
|
|
299
|
+
if (!fields.items.length) return null;
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
## Limitations
|
|
303
|
+
|
|
304
|
+
These rules look at the shape of your code, not at TypeScript types. They cover the patterns teams actually write, but not every possible variation. In particular, these **won't** be reported:
|
|
305
|
+
|
|
306
|
+
- A field renamed through a variable: `const t = fields.title; {fields.title.value && <Text field={t} />}`
|
|
307
|
+
- An empty check moved into a helper: `{hasTitle(fields) && <Text field={fields.title} />}`
|
|
308
|
+
- A field passed down to a child component that does the check.
|
|
309
|
+
|
|
310
|
+
There's no auto-fix yet. The right fix depends on the design: removing the condition, adding a `FieldGuard`, or checking the mode are all valid, so the rules leave that choice to you.
|
|
311
|
+
|
|
312
|
+
## FAQ
|
|
313
|
+
|
|
314
|
+
**Does it work with the old `.eslintrc` format?**
|
|
315
|
+
No. The plugin targets ESLint 9 flat config only.
|
|
316
|
+
|
|
317
|
+
**Will it slow down linting?**
|
|
318
|
+
No. Each rule only looks at `&&` expressions, ternaries and `if` statements, and does a small amount of work on each.
|
|
319
|
+
|
|
320
|
+
**I use my own `<Heading field={...}>` wrapper. Is it covered?**
|
|
321
|
+
Not by default. Add it to the `components` option of `no-conditional-field-render`.
|
|
322
|
+
|
|
323
|
+
**Does it work for plain JavaScript (`.jsx`)?**
|
|
324
|
+
Yes. The rules work on any JSX that ESLint can parse.
|
|
325
|
+
|
|
326
|
+
## Related
|
|
327
|
+
|
|
328
|
+
- [`sitecore-pagebuilder-kit`](https://www.npmjs.com/package/sitecore-pagebuilder-kit): `FieldGuard`, field empty checks, editor-only hints and editing-aware hooks. These are the fixes for what this plugin finds.
|
|
329
|
+
- [Sitecore Content SDK documentation](https://doc.sitecore.com/sai/en/developers/content-sdk/)
|
|
330
|
+
|
|
331
|
+
## Contributing
|
|
332
|
+
|
|
333
|
+
Found a pattern the rules miss, or a false positive? Open an issue on [GitHub](https://github.com/Krushna285/sitecore-pagebuilder-kit/issues) with a small code example.
|
|
334
|
+
|
|
335
|
+
## License
|
|
336
|
+
|
|
337
|
+
[MIT](https://github.com/Krushna285/sitecore-pagebuilder-kit/blob/main/LICENSE) © Krushna Patel
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Linter } from 'eslint';
|
|
2
|
+
declare const rules: {
|
|
3
|
+
'no-conditional-field-render': import("eslint").Rule.RuleModule;
|
|
4
|
+
'no-empty-early-return': import("eslint").Rule.RuleModule;
|
|
5
|
+
};
|
|
6
|
+
declare const plugin: {
|
|
7
|
+
meta: {
|
|
8
|
+
name: string;
|
|
9
|
+
version: string;
|
|
10
|
+
};
|
|
11
|
+
rules: {
|
|
12
|
+
'no-conditional-field-render': import("eslint").Rule.RuleModule;
|
|
13
|
+
'no-empty-early-return': import("eslint").Rule.RuleModule;
|
|
14
|
+
};
|
|
15
|
+
configs: Record<string, Linter.Config>;
|
|
16
|
+
};
|
|
17
|
+
export { rules };
|
|
18
|
+
export default plugin;
|
|
19
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAU,MAAM,EAAE,MAAM,QAAQ,CAAC;AAI7C,QAAA,MAAM,KAAK;;;CAGV,CAAC;AAEF,QAAA,MAAM,MAAM;;;;;;;;;aAGK,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC;CACrB,CAAC;AAW1B,OAAO,EAAE,KAAK,EAAE,CAAC;AACjB,eAAe,MAAM,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import noConditionalFieldRender from './rules/no-conditional-field-render.js';
|
|
2
|
+
import noEmptyEarlyReturn from './rules/no-empty-early-return.js';
|
|
3
|
+
const rules = {
|
|
4
|
+
'no-conditional-field-render': noConditionalFieldRender,
|
|
5
|
+
'no-empty-early-return': noEmptyEarlyReturn,
|
|
6
|
+
};
|
|
7
|
+
const plugin = {
|
|
8
|
+
meta: { name: 'eslint-plugin-sitecore-pagebuilder', version: '0.1.1' },
|
|
9
|
+
rules,
|
|
10
|
+
configs: {},
|
|
11
|
+
};
|
|
12
|
+
plugin.configs.recommended = {
|
|
13
|
+
name: 'sitecore-pagebuilder/recommended',
|
|
14
|
+
plugins: { 'sitecore-pagebuilder': plugin },
|
|
15
|
+
rules: {
|
|
16
|
+
'sitecore-pagebuilder/no-conditional-field-render': 'warn',
|
|
17
|
+
'sitecore-pagebuilder/no-empty-early-return': 'warn',
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
export { rules };
|
|
21
|
+
export default plugin;
|
|
22
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,wBAAwB,MAAM,wCAAwC,CAAC;AAC9E,OAAO,kBAAkB,MAAM,kCAAkC,CAAC;AAElE,MAAM,KAAK,GAAG;IACZ,6BAA6B,EAAE,wBAAwB;IACvD,uBAAuB,EAAE,kBAAkB;CAC5C,CAAC;AAEF,MAAM,MAAM,GAAG;IACb,IAAI,EAAE,EAAE,IAAI,EAAE,oCAAoC,EAAE,OAAO,EAAE,OAAO,EAAE;IACtE,KAAK;IACL,OAAO,EAAE,EAAmC;CACrB,CAAC;AAE1B,MAAM,CAAC,OAAO,CAAC,WAAW,GAAG;IAC3B,IAAI,EAAE,kCAAkC;IACxC,OAAO,EAAE,EAAE,sBAAsB,EAAE,MAAM,EAAE;IAC3C,KAAK,EAAE;QACL,kDAAkD,EAAE,MAAM;QAC1D,4CAA4C,EAAE,MAAM;KACrD;CACF,CAAC;AAEF,OAAO,EAAE,KAAK,EAAE,CAAC;AACjB,eAAe,MAAM,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type RuleModule } from '../utils.js';
|
|
2
|
+
/**
|
|
3
|
+
* Flags conditions on a field's value that wrap the Content SDK component
|
|
4
|
+
* rendering the same field:
|
|
5
|
+
*
|
|
6
|
+
* {fields.title.value && <Text field={fields.title} />}
|
|
7
|
+
* {fields.image?.value?.src ? <Image field={fields.image} /> : null}
|
|
8
|
+
*
|
|
9
|
+
* In Page Builder the SDK renders an empty-field placeholder so authors can
|
|
10
|
+
* fill the field in, but the condition removes the component first.
|
|
11
|
+
*/
|
|
12
|
+
declare const rule: RuleModule;
|
|
13
|
+
export default rule;
|
|
14
|
+
//# sourceMappingURL=no-conditional-field-render.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"no-conditional-field-render.d.ts","sourceRoot":"","sources":["../../src/rules/no-conditional-field-render.ts"],"names":[],"mappings":"AACA,OAAO,EAOL,KAAK,UAAU,EAChB,MAAM,aAAa,CAAC;AAMrB;;;;;;;;;GASG;AACH,QAAA,MAAM,IAAI,EAAE,UAqDX,CAAC;AAEF,eAAe,IAAI,CAAC"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { DEFAULT_FIELD_COMPONENTS, collectRenderedFields, collectValueReads, mentionsEditing, rendersNothing, } from '../utils.js';
|
|
2
|
+
/**
|
|
3
|
+
* Flags conditions on a field's value that wrap the Content SDK component
|
|
4
|
+
* rendering the same field:
|
|
5
|
+
*
|
|
6
|
+
* {fields.title.value && <Text field={fields.title} />}
|
|
7
|
+
* {fields.image?.value?.src ? <Image field={fields.image} /> : null}
|
|
8
|
+
*
|
|
9
|
+
* In Page Builder the SDK renders an empty-field placeholder so authors can
|
|
10
|
+
* fill the field in, but the condition removes the component first.
|
|
11
|
+
*/
|
|
12
|
+
const rule = {
|
|
13
|
+
meta: {
|
|
14
|
+
type: 'problem',
|
|
15
|
+
docs: {
|
|
16
|
+
description: 'Disallow hiding a Sitecore field component behind a check on its own value',
|
|
17
|
+
recommended: true,
|
|
18
|
+
url: 'https://github.com/Krushna285/sitecore-pagebuilder-kit/blob/main/packages/eslint-plugin/README.md#no-conditional-field-render',
|
|
19
|
+
},
|
|
20
|
+
schema: [
|
|
21
|
+
{
|
|
22
|
+
type: 'object',
|
|
23
|
+
properties: {
|
|
24
|
+
components: { type: 'array', items: { type: 'string' }, uniqueItems: true },
|
|
25
|
+
},
|
|
26
|
+
additionalProperties: false,
|
|
27
|
+
},
|
|
28
|
+
],
|
|
29
|
+
messages: {
|
|
30
|
+
hidden: "'{{field}}' is hidden when empty, so authors can't fill it in Page Builder. Wrap it in <FieldGuard field={{{field}}}> or render the field component without the condition.",
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
create(context) {
|
|
34
|
+
const options = (context.options[0] ?? {});
|
|
35
|
+
const components = [...DEFAULT_FIELD_COMPONENTS, ...(options.components ?? [])];
|
|
36
|
+
const sourceCode = context.sourceCode;
|
|
37
|
+
const check = (node, test, rendered) => {
|
|
38
|
+
if (mentionsEditing(test, sourceCode))
|
|
39
|
+
return;
|
|
40
|
+
const reads = collectValueReads(test, sourceCode);
|
|
41
|
+
if (reads.size === 0)
|
|
42
|
+
return;
|
|
43
|
+
for (const branch of rendered) {
|
|
44
|
+
for (const field of collectRenderedFields(branch, sourceCode, components)) {
|
|
45
|
+
if (reads.has(field)) {
|
|
46
|
+
context.report({ node, messageId: 'hidden', data: { field } });
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
return {
|
|
53
|
+
LogicalExpression(node) {
|
|
54
|
+
if (node.operator !== '&&')
|
|
55
|
+
return;
|
|
56
|
+
check(node, node.left, [node.right]);
|
|
57
|
+
},
|
|
58
|
+
ConditionalExpression(node) {
|
|
59
|
+
if (rendersNothing(node.alternate))
|
|
60
|
+
check(node, node.test, [node.consequent]);
|
|
61
|
+
else if (rendersNothing(node.consequent))
|
|
62
|
+
check(node, node.test, [node.alternate]);
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
export default rule;
|
|
68
|
+
//# sourceMappingURL=no-conditional-field-render.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"no-conditional-field-render.js","sourceRoot":"","sources":["../../src/rules/no-conditional-field-render.ts"],"names":[],"mappings":"AACA,OAAO,EACL,wBAAwB,EACxB,qBAAqB,EACrB,iBAAiB,EACjB,eAAe,EACf,cAAc,GAGf,MAAM,aAAa,CAAC;AAMrB;;;;;;;;;GASG;AACH,MAAM,IAAI,GAAe;IACvB,IAAI,EAAE;QACJ,IAAI,EAAE,SAAS;QACf,IAAI,EAAE;YACJ,WAAW,EAAE,4EAA4E;YACzF,WAAW,EAAE,IAAI;YACjB,GAAG,EAAE,+HAA+H;SACrI;QACD,MAAM,EAAE;YACN;gBACE,IAAI,EAAE,QAAQ;gBACd,UAAU,EAAE;oBACV,UAAU,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,EAAE,WAAW,EAAE,IAAI,EAAE;iBAC5E;gBACD,oBAAoB,EAAE,KAAK;aAC5B;SACF;QACD,QAAQ,EAAE;YACR,MAAM,EACJ,4KAA4K;SAC/K;KACF;IAED,MAAM,CAAC,OAAO;QACZ,MAAM,OAAO,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,EAAE,CAAY,CAAC;QACtD,MAAM,UAAU,GAAG,CAAC,GAAG,wBAAwB,EAAE,GAAG,CAAC,OAAO,CAAC,UAAU,IAAI,EAAE,CAAC,CAAC,CAAC;QAChF,MAAM,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;QAEtC,MAAM,KAAK,GAAG,CAAC,IAAa,EAAE,IAAa,EAAE,QAAmB,EAAE,EAAE;YAClE,IAAI,eAAe,CAAC,IAAI,EAAE,UAAU,CAAC;gBAAE,OA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without
|
|
4
|
+
* checking for Page Builder first:
|
|
5
|
+
*
|
|
6
|
+
* if (!fields.title.value) return null;
|
|
7
|
+
* if (!props.fields.items?.length) return null;
|
|
8
|
+
*
|
|
9
|
+
* On the live site that's fine, but in Page Builder the whole component
|
|
10
|
+
* disappears and authors have nothing to click to fill it in.
|
|
11
|
+
* A bare `if (!fields) return null` is left alone; use the SDK's
|
|
12
|
+
* withDatasourceCheck() for missing datasources.
|
|
13
|
+
*/
|
|
14
|
+
declare const rule: RuleModule;
|
|
15
|
+
export default rule;
|
|
16
|
+
//# sourceMappingURL=no-empty-early-return.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"no-empty-early-return.d.ts","sourceRoot":"","sources":["../../src/rules/no-empty-early-return.ts"],"names":[],"mappings":"AACA,OAAO,EAA2E,KAAK,UAAU,EAAE,MAAM,aAAa,CAAC;AAEvH;;;;;;;;;;;GAWG;AACH,QAAA,MAAM,IAAI,EAAE,UAgEX,CAAC;AAEF,eAAe,IAAI,CAAC"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { collectFieldChecks, mentionsEditing, rendersNothing, walk } from '../utils.js';
|
|
2
|
+
/**
|
|
3
|
+
* Flags components that return nothing when a field is empty, without
|
|
4
|
+
* checking for Page Builder first:
|
|
5
|
+
*
|
|
6
|
+
* if (!fields.title.value) return null;
|
|
7
|
+
* if (!props.fields.items?.length) return null;
|
|
8
|
+
*
|
|
9
|
+
* On the live site that's fine, but in Page Builder the whole component
|
|
10
|
+
* disappears and authors have nothing to click to fill it in.
|
|
11
|
+
* A bare `if (!fields) return null` is left alone; use the SDK's
|
|
12
|
+
* withDatasourceCheck() for missing datasources.
|
|
13
|
+
*/
|
|
14
|
+
const rule = {
|
|
15
|
+
meta: {
|
|
16
|
+
type: 'problem',
|
|
17
|
+
docs: {
|
|
18
|
+
description: 'Disallow returning nothing for empty fields without handling Page Builder editing',
|
|
19
|
+
recommended: true,
|
|
20
|
+
url: 'https://github.com/Krushna285/sitecore-pagebuilder-kit/blob/main/packages/eslint-plugin/README.md#no-empty-early-return',
|
|
21
|
+
},
|
|
22
|
+
schema: [],
|
|
23
|
+
messages: {
|
|
24
|
+
vanishes: "This component returns nothing when '{{field}}' is empty, so it disappears in Page Builder. Check page.mode.isEditing first, or keep rendering so authors can fill the field in.",
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
create(context) {
|
|
28
|
+
const sourceCode = context.sourceCode;
|
|
29
|
+
const returnsNothing = (statement) => {
|
|
30
|
+
if (!statement)
|
|
31
|
+
return false;
|
|
32
|
+
if (statement.type === 'ReturnStatement')
|
|
33
|
+
return rendersNothing(statement.argument);
|
|
34
|
+
if (statement.type === 'BlockStatement') {
|
|
35
|
+
const last = statement.body[statement.body.length - 1];
|
|
36
|
+
return Boolean(last) && last.type === 'ReturnStatement' && rendersNothing(last.argument);
|
|
37
|
+
}
|
|
38
|
+
return false;
|
|
39
|
+
};
|
|
40
|
+
const containsJsx = (fn) => {
|
|
41
|
+
let found = false;
|
|
42
|
+
walk(fn.body, sourceCode, (n) => {
|
|
43
|
+
if (found)
|
|
44
|
+
return false;
|
|
45
|
+
if (n.type === 'JSXElement' || n.type === 'JSXFragment')
|
|
46
|
+
found = true;
|
|
47
|
+
});
|
|
48
|
+
return found;
|
|
49
|
+
};
|
|
50
|
+
const enclosingFunction = (node) => {
|
|
51
|
+
let current = node.parent;
|
|
52
|
+
while (current) {
|
|
53
|
+
if (current.type === 'FunctionDeclaration' ||
|
|
54
|
+
current.type === 'FunctionExpression' ||
|
|
55
|
+
current.type === 'ArrowFunctionExpression') {
|
|
56
|
+
return current;
|
|
57
|
+
}
|
|
58
|
+
current = current.parent;
|
|
59
|
+
}
|
|
60
|
+
return null;
|
|
61
|
+
};
|
|
62
|
+
return {
|
|
63
|
+
IfStatement(node) {
|
|
64
|
+
if (!returnsNothing(node.consequent))
|
|
65
|
+
return;
|
|
66
|
+
const fn = enclosingFunction(node);
|
|
67
|
+
if (!fn || !containsJsx(fn))
|
|
68
|
+
return;
|
|
69
|
+
if (mentionsEditing(node.test, sourceCode))
|
|
70
|
+
return;
|
|
71
|
+
const checks = collectFieldChecks(node.test, sourceCode);
|
|
72
|
+
if (checks.length === 0)
|
|
73
|
+
return;
|
|
74
|
+
context.report({ node, messageId: 'vanishes', data: { field: checks[0] } });
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
export default rule;
|
|
80
|
+
//# sourceMappingURL=no-empty-early-return.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"no-empty-early-return.js","sourceRoot":"","sources":["../../src/rules/no-empty-early-return.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,eAAe,EAAE,cAAc,EAAE,IAAI,EAAiC,MAAM,aAAa,CAAC;AAEvH;;;;;;;;;;;GAWG;AACH,MAAM,IAAI,GAAe;IACvB,IAAI,EAAE;QACJ,IAAI,EAAE,SAAS;QACf,IAAI,EAAE;YACJ,WAAW,EAAE,mFAAmF;YAChG,WAAW,EAAE,IAAI;YACjB,GAAG,EAAE,yHAAyH;SAC/H;QACD,MAAM,EAAE,EAAE;QACV,QAAQ,EAAE;YACR,QAAQ,EACN,kLAAkL;SACrL;KACF;IAED,MAAM,CAAC,OAAO;QACZ,MAAM,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;QAEtC,MAAM,cAAc,GAAG,CAAC,SAAkB,EAAW,EAAE;YACrD,IAAI,CAAC,SAAS;gBAAE,OAAO,KAAK,CAAC;YAC7B,IAAI,SAAS,CAAC,IAAI,KAAK,iBAAiB;gBAAE,OAAO,cAAc,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC;YACpF,IAAI,SAAS,CAAC,IAAI,KAAK,gBAAgB,EAAE,CAAC;gBACxC,MAAM,IAAI,GAAG,SAAS,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAA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AAI,CAAC,IAAI,EAAE,UAAU,CAAC;oBAAE,OAAO;gBACnD,MAAM,MAAM,GAAG,kBAAkB,CAAC,IAAI,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC;gBACzD,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC;oBAAE,OAAO;gBAChC,OAAO,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,UAAU,EAAE,IAAI,EAAE,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;YAC9E,CAAC;SACmB,CAAC;IACzB,CAAC;CACF,CAAC;AAEF,eAAe,IAAI,CAAC"}
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Rule, SourceCode } from 'eslint';
|
|
2
|
+
/** ESTree nodes, including JSX and TypeScript nodes the core types don't model. */
|
|
3
|
+
export type AnyNode = any;
|
|
4
|
+
/** Content SDK components that render a Sitecore field through a `field` prop. */
|
|
5
|
+
export declare const DEFAULT_FIELD_COMPONENTS: string[];
|
|
6
|
+
/** Calls `visit` on `node` and every descendant. Return false from `visit` to skip children. */
|
|
7
|
+
export declare function walk(node: AnyNode, sourceCode: SourceCode, visit: (n: AnyNode) => boolean | void): void;
|
|
8
|
+
/** Source text with optional chaining, non-null assertions and whitespace removed. */
|
|
9
|
+
export declare function normalize(node: AnyNode, sourceCode: SourceCode): string;
|
|
10
|
+
/**
|
|
11
|
+
* Finds every `X.value` read inside `node` and returns the normalized text of `X`.
|
|
12
|
+
* `fields.title.value`, `fields.title?.value?.src` and `!!title.value` all yield the field expression.
|
|
13
|
+
*/
|
|
14
|
+
export declare function collectValueReads(node: AnyNode, sourceCode: SourceCode): Set<string>;
|
|
15
|
+
/** Normalized `field={...}` expressions of Content SDK field components inside `node`. */
|
|
16
|
+
export declare function collectRenderedFields(node: AnyNode, sourceCode: SourceCode, components: string[]): Set<string>;
|
|
17
|
+
/** True for `null`, `undefined`, `false` and `''`: branches that render nothing. */
|
|
18
|
+
export declare function rendersNothing(node: AnyNode): boolean;
|
|
19
|
+
/** True when the expression mentions `isEditing` (e.g. `page.mode.isEditing`). */
|
|
20
|
+
export declare function mentionsEditing(node: AnyNode, sourceCode: SourceCode): boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Finds reads of component fields deeper than `fields` itself, such as
|
|
23
|
+
* `fields.items`, `props.fields.items.length` or `fields.title.value`.
|
|
24
|
+
* A bare `fields` / `props.fields` check (a missing datasource) is not included.
|
|
25
|
+
*/
|
|
26
|
+
export declare function collectFieldChecks(node: AnyNode, sourceCode: SourceCode): string[];
|
|
27
|
+
export type RuleModule = Rule.RuleModule;
|
|
28
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AAE/C,mFAAmF;AAEnF,MAAM,MAAM,OAAO,GAAG,GAAG,CAAC;AAE1B,kFAAkF;AAClF,eAAO,MAAM,wBAAwB,UAA0E,CAAC;AAEhH,gGAAgG;AAChG,wBAAgB,IAAI,CAAC,IAAI,EAAE,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC,CAAC,EAAE,OAAO,KAAK,OAAO,GAAG,IAAI,GAAG,IAAI,CASvG;AAED,sFAAsF;AACtF,wBAAgB,SAAS,CAAC,IAAI,EAAE,OAAO,EAAE,UAAU,EAAE,UAAU,GAAG,MAAM,CAMvE;AAiBD;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,OAAO,EAAE,UAAU,EAAE,UAAU,GAAG,GAAG,CAAC,MAAM,CAAC,CAQpF;AASD,0FAA0F;AAC1F,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,EAAE,GAAG,GAAG,CAAC,MAAM,CAAC,CAiB9G;AAED,oFAAoF;AACpF,wBAAgB,cAAc,CAAC,IAAI,EAAE,OAAO,GAAG,OAAO,CAKrD;AAED,kFAAkF;AAClF,wBAAgB,eAAe,CAAC,IAAI,EAAE,OAAO,EAAE,UAAU,EAAE,UAAU,GAAG,OAAO,CAQ9E;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,OAAO,EAAE,UAAU,EAAE,UAAU,GAAG,MAAM,EAAE,CAiBlF;AAED,MAAM,MAAM,UAAU,GAAG,IAAI,CAAC,UAAU,CAAC"}
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/** Content SDK components that render a Sitecore field through a `field` prop. */
|
|
2
|
+
export const DEFAULT_FIELD_COMPONENTS = ['Text', 'RichText', 'Image', 'NextImage', 'Link', 'DateField', 'File'];
|
|
3
|
+
/** Calls `visit` on `node` and every descendant. Return false from `visit` to skip children. */
|
|
4
|
+
export function walk(node, sourceCode, visit) {
|
|
5
|
+
if (!node || typeof node.type !== 'string')
|
|
6
|
+
return;
|
|
7
|
+
if (visit(node) === false)
|
|
8
|
+
return;
|
|
9
|
+
const keys = sourceCode.visitorKeys[node.type] ?? [];
|
|
10
|
+
for (const key of keys) {
|
|
11
|
+
const child = node[key];
|
|
12
|
+
if (Array.isArray(child))
|
|
13
|
+
child.forEach((c) => walk(c, sourceCode, visit));
|
|
14
|
+
else
|
|
15
|
+
walk(child, sourceCode, visit);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/** Source text with optional chaining, non-null assertions and whitespace removed. */
|
|
19
|
+
export function normalize(node, sourceCode) {
|
|
20
|
+
return sourceCode
|
|
21
|
+
.getText(node)
|
|
22
|
+
.replace(/\s+/g, '')
|
|
23
|
+
.replace(/\?\./g, '.')
|
|
24
|
+
.replace(/!(?=[.\[]|$)/g, '');
|
|
25
|
+
}
|
|
26
|
+
const propertyName = (member) => member.property?.type === 'Identifier' && !member.computed
|
|
27
|
+
? member.property.name
|
|
28
|
+
: member.property?.type === 'Literal'
|
|
29
|
+
? String(member.property.value)
|
|
30
|
+
: undefined;
|
|
31
|
+
const unwrap = (node) => {
|
|
32
|
+
let current = node;
|
|
33
|
+
while (current && (current.type === 'TSNonNullExpression' || current.type === 'ChainExpression')) {
|
|
34
|
+
current = current.expression;
|
|
35
|
+
}
|
|
36
|
+
return current;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Finds every `X.value` read inside `node` and returns the normalized text of `X`.
|
|
40
|
+
* `fields.title.value`, `fields.title?.value?.src` and `!!title.value` all yield the field expression.
|
|
41
|
+
*/
|
|
42
|
+
export function collectValueReads(node, sourceCode) {
|
|
43
|
+
const reads = new Set();
|
|
44
|
+
walk(node, sourceCode, (n) => {
|
|
45
|
+
if (n.type === 'MemberExpression' && propertyName(n) === 'value') {
|
|
46
|
+
reads.add(normalize(unwrap(n.object), sourceCode));
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
return reads;
|
|
50
|
+
}
|
|
51
|
+
const jsxName = (name) => {
|
|
52
|
+
if (!name)
|
|
53
|
+
return undefined;
|
|
54
|
+
if (name.type === 'JSXIdentifier')
|
|
55
|
+
return name.name;
|
|
56
|
+
if (name.type === 'JSXMemberExpression')
|
|
57
|
+
return name.property?.name;
|
|
58
|
+
return undefined;
|
|
59
|
+
};
|
|
60
|
+
/** Normalized `field={...}` expressions of Content SDK field components inside `node`. */
|
|
61
|
+
export function collectRenderedFields(node, sourceCode, components) {
|
|
62
|
+
const rendered = new Set();
|
|
63
|
+
walk(node, sourceCode, (n) => {
|
|
64
|
+
if (n.type !== 'JSXOpeningElement')
|
|
65
|
+
return;
|
|
66
|
+
const name = jsxName(n.name);
|
|
67
|
+
if (!name || !components.includes(name))
|
|
68
|
+
return;
|
|
69
|
+
for (const attr of n.attributes ?? []) {
|
|
70
|
+
if (attr.type === 'JSXAttribute' &&
|
|
71
|
+
attr.name?.name === 'field' &&
|
|
72
|
+
attr.value?.type === 'JSXExpressionContainer') {
|
|
73
|
+
rendered.add(normalize(unwrap(attr.value.expression), sourceCode));
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
return rendered;
|
|
78
|
+
}
|
|
79
|
+
/** True for `null`, `undefined`, `false` and `''`: branches that render nothing. */
|
|
80
|
+
export function rendersNothing(node) {
|
|
81
|
+
const n = unwrap(node);
|
|
82
|
+
if (!n)
|
|
83
|
+
return true;
|
|
84
|
+
if (n.type === 'Literal')
|
|
85
|
+
return n.value === null || n.value === false || n.value === '';
|
|
86
|
+
return n.type === 'Identifier' && n.name === 'undefined';
|
|
87
|
+
}
|
|
88
|
+
/** True when the expression mentions `isEditing` (e.g. `page.mode.isEditing`). */
|
|
89
|
+
export function mentionsEditing(node, sourceCode) {
|
|
90
|
+
let found = false;
|
|
91
|
+
walk(node, sourceCode, (n) => {
|
|
92
|
+
if (found)
|
|
93
|
+
return false;
|
|
94
|
+
if (n.type === 'Identifier' && /^is(Editing|PageEditing|EditMode)$/.test(n.name))
|
|
95
|
+
found = true;
|
|
96
|
+
if (n.type === 'MemberExpression' && /^(isEditing|pageEditing)$/.test(propertyName(n) ?? ''))
|
|
97
|
+
found = true;
|
|
98
|
+
});
|
|
99
|
+
return found;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Finds reads of component fields deeper than `fields` itself, such as
|
|
103
|
+
* `fields.items`, `props.fields.items.length` or `fields.title.value`.
|
|
104
|
+
* A bare `fields` / `props.fields` check (a missing datasource) is not included.
|
|
105
|
+
*/
|
|
106
|
+
export function collectFieldChecks(node, sourceCode) {
|
|
107
|
+
const checks = [];
|
|
108
|
+
walk(node, sourceCode, (n) => {
|
|
109
|
+
if (n.type !== 'MemberExpression')
|
|
110
|
+
return;
|
|
111
|
+
const text = normalize(n, sourceCode);
|
|
112
|
+
const parts = text.split('.');
|
|
113
|
+
const index = parts.indexOf('fields');
|
|
114
|
+
if (index >= 0 && parts.length > index + 1) {
|
|
115
|
+
checks.push(text);
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
if (propertyName(n) === 'value') {
|
|
119
|
+
checks.push(text);
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
return checks;
|
|
124
|
+
}
|
|
125
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAMA,kFAAkF;AAClF,MAAM,CAAC,MAAM,wBAAwB,GAAG,CAAC,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,CAAC,CAAC;AAEhH,gGAAgG;AAChG,MAAM,UAAU,IAAI,CAAC,IAAa,EAAE,UAAsB,EAAE,KAAqC;IAC/F,IAAI,CAAC,IAAI,IAAI,OAAO,IAAI,CAAC,IAAI,KAAK,QAAQ;QAAE,OAAO;IACnD,IAAI,KAAK,CAAC,IAAI,CAAC,KAAK,KAAK;QAAE,OAAO;IAClC,MAAM,IAAI,GAAG,UAAU,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;IACrD,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;QACvB,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC;QACxB,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;YAAE,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,EAAE,UAAU,EAAE,KAAK,CAAC,CAAC,CAAC;;YACtE,IAAI,CAAC,KAAK,EAAE,UAAU,EAAE,KAAK,CAAC,CAAC;IACtC,CAAC;AACH,CAAC;AAED,sFAAsF;AACtF,MAAM,UAAU,SAAS,CAAC,IAAa,EAAE,UAAsB;IAC7D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M,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAC9B,MAAM,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;QACtC,IAAI,KAAK,IAAI,CAAC,IAAI,KAAK,CAAC,MAAM,GAAG,KAAK,GAAG,CAAC,EAAE,CAAC;YAC3C,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YAClB,OAAO,KAAK,CAAC;QACf,CAAC;QACD,IAAI,YAAY,CAAC,CAAC,CAAC,KAAK,OAAO,EAAE,CAAC;YAChC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YAClB,OAAO,KAAK,CAAC;QACf,CAAC;IACH,CAAC,CAAC,CAAC;IACH,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,61 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-sitecore-pagebuilder",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "ESLint rules that catch Sitecore Content SDK code which hides empty fields and components from authors in Page Builder.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"README.md",
|
|
17
|
+
"LICENSE",
|
|
18
|
+
"CHANGELOG.md"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\" && tsc -p tsconfig.build.json",
|
|
22
|
+
"test": "vitest run",
|
|
23
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
24
|
+
"prepublishOnly": "npm run build"
|
|
25
|
+
},
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"eslint": ">=9.0.0"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/estree": "^1.0.6",
|
|
31
|
+
"@typescript-eslint/parser": "^8.0.0",
|
|
32
|
+
"eslint": "^9.15.0"
|
|
33
|
+
},
|
|
34
|
+
"keywords": [
|
|
35
|
+
"eslint",
|
|
36
|
+
"eslintplugin",
|
|
37
|
+
"eslint-plugin",
|
|
38
|
+
"sitecore",
|
|
39
|
+
"content-sdk",
|
|
40
|
+
"page-builder",
|
|
41
|
+
"sitecoreai",
|
|
42
|
+
"xm-cloud"
|
|
43
|
+
],
|
|
44
|
+
"author": "Krushna Patel",
|
|
45
|
+
"license": "MIT",
|
|
46
|
+
"repository": {
|
|
47
|
+
"type": "git",
|
|
48
|
+
"url": "git+https://github.com/Krushna285/sitecore-pagebuilder-kit.git",
|
|
49
|
+
"directory": "packages/eslint-plugin"
|
|
50
|
+
},
|
|
51
|
+
"homepage": "https://github.com/Krushna285/sitecore-pagebuilder-kit/tree/main/packages/eslint-plugin#readme",
|
|
52
|
+
"bugs": {
|
|
53
|
+
"url": "https://github.com/Krushna285/sitecore-pagebuilder-kit/issues"
|
|
54
|
+
},
|
|
55
|
+
"publishConfig": {
|
|
56
|
+
"access": "public"
|
|
57
|
+
},
|
|
58
|
+
"engines": {
|
|
59
|
+
"node": ">=18.18"
|
|
60
|
+
}
|
|
61
|
+
}
|