eslint-plugin-sitecore-pagebuilder 0.1.0 → 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 CHANGED
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
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
+
3
8
  ## 0.1.0
4
9
 
5
10
  First release.
package/README.md CHANGED
@@ -1,36 +1,102 @@
1
1
  # eslint-plugin-sitecore-pagebuilder
2
2
 
3
- ESLint rules for [Sitecore Content SDK](https://doc.sitecore.com/sai/en/developers/content-sdk/) apps that catch code which hides empty fields and components from authors in Page Builder.
3
+ [![npm version](https://img.shields.io/npm/v/eslint-plugin-sitecore-pagebuilder.svg)](https://www.npmjs.com/package/eslint-plugin-sitecore-pagebuilder)
4
+ [![license: MIT](https://img.shields.io/npm/l/eslint-plugin-sitecore-pagebuilder.svg)](https://github.com/Krushna285/sitecore-pagebuilder-kit/blob/main/LICENSE)
4
5
 
5
- These bugs pass code review and work fine on the live site. They're usually found late, by an author who can't fill in a field. This plugin finds them while you write the component.
6
-
7
- The fixes it suggests come from [`sitecore-pagebuilder-kit`](../kit).
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.
8
7
 
9
8
  > Community package. Not affiliated with or endorsed by Sitecore.
10
9
 
11
- ## Install
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
12
52
 
13
53
  ```sh
14
54
  npm install --save-dev eslint-plugin-sitecore-pagebuilder
15
55
  ```
16
56
 
17
- Requires ESLint 9 with flat config, which Content SDK apps use by default from version 1.1.
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
18
68
 
19
- ## Usage
69
+ Add the recommended config to your `eslint.config.mjs`. It turns on both rules as warnings.
20
70
 
21
71
  ```js
22
72
  // eslint.config.mjs
23
73
  import pagebuilder from 'eslint-plugin-sitecore-pagebuilder';
24
74
 
25
75
  export default [
26
- // ...your existing config
76
+ // ...your existing config (Next.js, TypeScript, etc.)
27
77
  pagebuilder.configs.recommended,
28
78
  ];
29
79
  ```
30
80
 
31
- Or pick rules yourself:
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
32
96
 
33
97
  ```js
98
+ import pagebuilder from 'eslint-plugin-sitecore-pagebuilder';
99
+
34
100
  export default [
35
101
  {
36
102
  plugins: { 'sitecore-pagebuilder': pagebuilder },
@@ -42,51 +108,99 @@ export default [
42
108
  ];
43
109
  ```
44
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
+
45
113
  ## Rules
46
114
 
47
- | Rule | What it catches | Recommended |
115
+ | Rule | What it catches | In `recommended` |
48
116
  | --- | --- | --- |
49
117
  | [`no-conditional-field-render`](#no-conditional-field-render) | A field component hidden behind a check on its own value | warn |
50
- | [`no-empty-early-return`](#no-empty-early-return) | A component that returns nothing for an empty field | warn |
118
+ | [`no-empty-early-return`](#no-empty-early-return) | A component that returns nothing when a field is empty | warn |
119
+
120
+ ---
51
121
 
52
122
  ### no-conditional-field-render
53
123
 
54
- In Page Builder, the SDK's field components render a placeholder for empty fields so authors can click and type. A condition on the field's value removes the component before it gets the chance.
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
55
131
 
56
132
  ```tsx
57
- // ❌
58
133
  {fields.title.value && <Text field={fields.title} tag="h2" />}
134
+
59
135
  {fields.image?.value?.src ? <Image field={fields.image} /> : null}
136
+
60
137
  {!fields.cta.value.href ? null : <Link field={fields.cta} />}
61
138
 
62
- // ✅ Let the SDK handle empty fields
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
63
155
  <Text field={fields.title} tag="h2" />
64
156
 
65
- // ✅ Wrapper markup that should disappear on the live site
157
+ // A guard that knows about Page Builder
66
158
  <FieldGuard field={fields.title}>
67
159
  <div className="card__title"><Text field={fields.title} tag="h2" /></div>
68
160
  </FieldGuard>
69
161
 
70
- // ✅ Editing-aware conditions are allowed
162
+ // The condition also checks for editing mode
71
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} />}
72
168
  ```
73
169
 
74
- It only reports when the condition and the rendered component refer to the same field, so `{fields.showTitle.value && <Text field={fields.title} />}` is fine. Destructured fields and TypeScript non-null assertions are understood.
170
+ A condition counts as editing-aware if it mentions `isEditing`, `isPageEditing`, `isEditMode` or `pageEditing`.
171
+
172
+ #### Options
75
173
 
76
- **Options**
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. |
77
177
 
78
- - `components` (`string[]`): extra component names that render a field through a `field` prop, such as your own wrappers. Defaults cover `Text`, `RichText`, `Image`, `NextImage`, `Link`, `DateField` and `File`.
178
+ Built-in components: `Text`, `RichText`, `Image`, `NextImage`, `Link`, `DateField`, `File`. Member expressions also match, so `<Sdk.Text>` counts as `Text`.
79
179
 
80
180
  ```js
81
- 'sitecore-pagebuilder/no-conditional-field-render': ['warn', { components: ['Heading', 'CtaLink'] }]
181
+ 'sitecore-pagebuilder/no-conditional-field-render': [
182
+ 'warn',
183
+ { components: ['Heading', 'CtaLink', 'ResponsiveImage'] },
184
+ ],
82
185
  ```
83
186
 
187
+ #### Message
188
+
189
+ > 'fields.title' is hidden when empty, so authors can't fill it in Page Builder. Wrap it in &lt;FieldGuard field={fields.title}&gt; or render the field component without the condition.
190
+
191
+ ---
192
+
84
193
  ### no-empty-early-return
85
194
 
86
- A component that returns `null` when a field is empty disappears entirely in Page Builder, leaving authors nothing to click.
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
87
202
 
88
203
  ```tsx
89
- // ❌
90
204
  function Promo({ fields }) {
91
205
  if (!fields.image.value.src) return null;
92
206
  return <Image field={fields.image} />;
@@ -97,20 +211,127 @@ function RelatedArticles(props) {
97
211
  return <ul>...</ul>;
98
212
  }
99
213
 
100
- // ✅ Keep it on the live site, show it while editing
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
101
226
  function Promo({ fields }) {
102
227
  const { page } = useSitecore();
103
228
  if (!page.mode.isEditing && !fields.image.value.src) return null;
104
229
  return <Image field={fields.image} />;
105
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
+ }
106
244
  ```
107
245
 
108
- A bare `if (!fields) return null` is not reported. Missing datasources are better handled by the SDK's `withDatasourceCheck()`. Functions that don't return JSX are ignored.
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
+ ```
109
301
 
110
302
  ## Limitations
111
303
 
112
- These rules use syntax, not types, so they follow the patterns teams actually write rather than every possible one. A field passed through a variable with a different name, or checked in a helper function, won't be matched. Reports have no auto-fix yet, because the right fix depends on the design.
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.
113
334
 
114
335
  ## License
115
336
 
116
- MIT © Krushna Patel
337
+ [MIT](https://github.com/Krushna285/sitecore-pagebuilder-kit/blob/main/LICENSE) © Krushna Patel
package/dist/index.js CHANGED
@@ -5,7 +5,7 @@ const rules = {
5
5
  'no-empty-early-return': noEmptyEarlyReturn,
6
6
  };
7
7
  const plugin = {
8
- meta: { name: 'eslint-plugin-sitecore-pagebuilder', version: '0.1.0' },
8
+ meta: { name: 'eslint-plugin-sitecore-pagebuilder', version: '0.1.1' },
9
9
  rules,
10
10
  configs: {},
11
11
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-sitecore-pagebuilder",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "ESLint rules that catch Sitecore Content SDK code which hides empty fields and components from authors in Page Builder.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",