@allxsmith/eslint-plugin-bestax 1.0.2 → 1.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/README.md +36 -9
- package/dist/configs/recommended.d.ts +4 -0
- package/dist/configs/recommended.d.ts.map +1 -1
- package/dist/configs/recommended.js +4 -1
- package/dist/generated/metadata.d.ts +16 -0
- package/dist/generated/metadata.d.ts.map +1 -1
- package/dist/generated/metadata.js +86 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/lib/elements.d.ts +34 -0
- package/dist/lib/elements.d.ts.map +1 -1
- package/dist/lib/elements.js +250 -20
- package/dist/rules/no-bulma-component-class.d.ts +34 -0
- package/dist/rules/no-bulma-component-class.d.ts.map +1 -0
- package/dist/rules/no-bulma-component-class.js +65 -0
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/@allxsmith/eslint-plugin-bestax)
|
|
4
4
|
[](https://www.npmjs.com/package/@allxsmith/eslint-plugin-bestax)
|
|
5
|
+
[](https://www.npmjs.com/package/@allxsmith/eslint-plugin-bestax#provenance)
|
|
5
6
|
[](https://opensource.org/licenses/MIT)
|
|
6
7
|
[](https://socket.dev/npm/package/@allxsmith/eslint-plugin-bestax/overview)
|
|
7
8
|
[](https://scorecard.dev/viewer/?uri=github.com/allxsmith/bestax)
|
|
@@ -109,23 +110,27 @@ In `recommended` — each reports code that does not do what it says:
|
|
|
109
110
|
| `no-deprecated-props` | partly | Props the library deprecated: `isFullWidth`, `gapSize*`, `icon`. Fixes only the renames that cannot change what renders |
|
|
110
111
|
| `no-inert-flex-props` | — | `justifyContent` and friends without a flex `display`, which emit nothing |
|
|
111
112
|
|
|
112
|
-
Opt-in —
|
|
113
|
+
Opt-in — these report code that works, and buy explicitness instead:
|
|
113
114
|
|
|
114
|
-
| Rule
|
|
115
|
-
|
|
|
116
|
-
| `no-color-as-surface`
|
|
115
|
+
| Rule | Fixable | What it asks for |
|
|
116
|
+
| -------------------------- | ------- | ----------------------------------------------------------------------------------------------------------- |
|
|
117
|
+
| `no-color-as-surface` | yes | `textColor` instead of the `color` alias, where `color` cannot mean a surface |
|
|
118
|
+
| `no-bulma-component-class` | — | The bestax component instead of a plain element styled with its Bulma class (`<button className="button">`) |
|
|
117
119
|
|
|
118
|
-
|
|
119
|
-
your own `<Box>` nor a local shadowing the imported
|
|
120
|
-
Bulma's rules, and each skips a value it cannot read as a
|
|
120
|
+
The rules that judge bestax elements resolve them through the import and
|
|
121
|
+
through scope, so neither your own `<Box>` nor a local shadowing the imported
|
|
122
|
+
one is linted against Bulma's rules, and each skips a value it cannot read as a
|
|
123
|
+
literal. `no-bulma-component-class` reads plain HTML elements instead, and
|
|
124
|
+
leaves components, custom elements and anything inside `<svg>` alone.
|
|
121
125
|
|
|
122
126
|
A spread is handled by what it can change: `no-color-as-surface` and
|
|
123
127
|
`no-inert-flex-props` go silent, since a spread may carry the prop that makes
|
|
124
128
|
the code correct; `no-deprecated-props` reports but offers no fix; and
|
|
125
129
|
`valid-helper-value` judges only the values that actually render, so a spread
|
|
126
130
|
BEFORE the attribute leaves it reporting and a spread after it does not: JSX is
|
|
127
|
-
last-wins throughout, spreads included.
|
|
128
|
-
|
|
131
|
+
last-wins throughout, spreads included. `no-bulma-component-class` reports
|
|
132
|
+
either way, since the class is written right there. No autofix changes what the
|
|
133
|
+
code renders.
|
|
129
134
|
|
|
130
135
|
### valid-helper-value
|
|
131
136
|
|
|
@@ -173,6 +178,28 @@ code.
|
|
|
173
178
|
<Box bgColor="info" color="primary" />
|
|
174
179
|
```
|
|
175
180
|
|
|
181
|
+
### no-bulma-component-class
|
|
182
|
+
|
|
183
|
+
Off by default. It reports a plain element styled with a Bulma class that
|
|
184
|
+
bestax has a component for, and names the component. Turn it on in an app that
|
|
185
|
+
has moved onto bestax, with `bestax-migrate bulma-classes` or by hand, to keep
|
|
186
|
+
raw Bulma markup from coming back.
|
|
187
|
+
|
|
188
|
+
```jsx
|
|
189
|
+
<button className="button is-primary" /> // ✗ bestax renders .button as Button
|
|
190
|
+
<div className="dropdown" /> // ✗ bestax has Dropdown, converted by hand
|
|
191
|
+
<div className="has-text-centered mt-4" /> // ✓ helper classes are left alone
|
|
192
|
+
<p className="help" /> // ✓ bestax renders .help inside its form controls
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
It reads the classes a `className` spells out, including the strings in a
|
|
196
|
+
ternary, a template, or a call to `clsx`, `classnames` or `tailwind-merge`
|
|
197
|
+
(however the file imports or requires it), and reports one class per element:
|
|
198
|
+
the one the codemod would decide by. A function your app defines, a `cn` in `lib/utils`
|
|
199
|
+
included, is one it cannot see into, so it reads nothing there. It only reports. Whether an element converts
|
|
200
|
+
depends on its tag, attributes and children, and `bestax-migrate bulma-classes`
|
|
201
|
+
is the one place that judges that.
|
|
202
|
+
|
|
176
203
|
### no-inert-flex-props
|
|
177
204
|
|
|
178
205
|
```jsx
|
|
@@ -9,6 +9,10 @@
|
|
|
9
9
|
* works, so the rule buys explicitness rather than correctness. Linting the
|
|
10
10
|
* library's own documentation with it on reported dozens of correct examples,
|
|
11
11
|
* which is the evidence it does not belong in a default-on set.
|
|
12
|
+
*
|
|
13
|
+
* `no-bulma-component-class` is opt-in for the same reason: Bulma's classes
|
|
14
|
+
* on plain markup work, and preferring the component is a choice an app makes
|
|
15
|
+
* (typically after `bestax-migrate bulma-classes`), not a bug in its code.
|
|
12
16
|
*/
|
|
13
17
|
import type { Linter } from 'eslint';
|
|
14
18
|
export declare const RECOMMENDED_RULES: Linter.RulesRecord;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"recommended.d.ts","sourceRoot":"","sources":["../../src/configs/recommended.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"recommended.d.ts","sourceRoot":"","sources":["../../src/configs/recommended.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAErC,eAAO,MAAM,iBAAiB,EAAE,MAAM,CAAC,WAItC,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,YAAY,EAAE,SAAS,MAAM,EAGzC,CAAC"}
|
|
@@ -8,4 +8,7 @@ export const RECOMMENDED_RULES = {
|
|
|
8
8
|
* inferred, so a new rule that nobody wires into a config fails the surface
|
|
9
9
|
* test instead of shipping switched off by accident.
|
|
10
10
|
*/
|
|
11
|
-
export const OPT_IN_RULES = [
|
|
11
|
+
export const OPT_IN_RULES = [
|
|
12
|
+
'no-color-as-surface',
|
|
13
|
+
'no-bulma-component-class',
|
|
14
|
+
];
|
|
@@ -23,4 +23,20 @@ export declare const DEPRECATED_PROPS: Readonly<Record<string, Readonly<Record<s
|
|
|
23
23
|
* this set is evidence of a text alias and never of a real variant.
|
|
24
24
|
*/
|
|
25
25
|
export declare const TEXT_ALIAS_COLOR_ELEMENTS: readonly string[];
|
|
26
|
+
/** A Bulma class that names a bestax component. */
|
|
27
|
+
export interface BulmaComponentClass {
|
|
28
|
+
/** The bestax component, dotted for a part (`Hero.Body`). */
|
|
29
|
+
readonly component: string;
|
|
30
|
+
/**
|
|
31
|
+
* Whether `bestax-migrate bulma-classes` converts the element. False for a
|
|
32
|
+
* family whose component renders its own parts, converted by hand.
|
|
33
|
+
*/
|
|
34
|
+
readonly converts: boolean;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Bulma classes that name a bestax component, from bestax-migrate's
|
|
38
|
+
* bulma-classes table. In the order the codemod decides an element's
|
|
39
|
+
* component when it carries several: a family first, then by precedence.
|
|
40
|
+
*/
|
|
41
|
+
export declare const BULMA_COMPONENT_CLASSES: ReadonlyMap<string, BulmaComponentClass>;
|
|
26
42
|
//# sourceMappingURL=metadata.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"metadata.d.ts","sourceRoot":"","sources":["../../src/generated/metadata.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"metadata.d.ts","sourceRoot":"","sources":["../../src/generated/metadata.ts"],"names":[],"mappings":"AAIA,uEAAuE;AACvE,MAAM,WAAW,WAAW;IAC1B,4EAA4E;IAC5E,QAAQ,CAAC,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IACpC,uEAAuE;IACvE,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;CAC9B;AAED;;;GAGG;AACH,eAAO,MAAM,gBAAgB,EAAE,QAAQ,CACrC,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC,CAiItD,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,yBAAyB,EAAE,SAAS,MAAM,EAUtD,CAAC;AAEF,mDAAmD;AACnD,MAAM,WAAW,mBAAmB;IAClC,6DAA6D;IAC7D,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B;;;OAGG;IACH,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC;CAC5B;AAED;;;;GAIG;AACH,eAAO,MAAM,uBAAuB,EAAE,WAAW,CAAC,MAAM,EAAE,mBAAmB,CA+EzE,CAAC"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
// Generated by scripts/gen-eslint-meta.mjs from the library's TSDoc
|
|
1
|
+
// Generated by scripts/gen-eslint-meta.mjs from the library's TSDoc and
|
|
2
|
+
// bestax-migrate's bulma-classes table.
|
|
2
3
|
// Do not edit by hand — run `pnpm gen:eslint-meta`. CI fails on a stale one.
|
|
3
4
|
/**
|
|
4
5
|
* Deprecated props, keyed by the JSX element name as written — so a compound
|
|
@@ -156,3 +157,87 @@ export const TEXT_ALIAS_COLOR_ELEMENTS = [
|
|
|
156
157
|
'IconText',
|
|
157
158
|
'Image',
|
|
158
159
|
];
|
|
160
|
+
/**
|
|
161
|
+
* Bulma classes that name a bestax component, from bestax-migrate's
|
|
162
|
+
* bulma-classes table. In the order the codemod decides an element's
|
|
163
|
+
* component when it carries several: a family first, then by precedence.
|
|
164
|
+
*/
|
|
165
|
+
export const BULMA_COMPONENT_CLASSES = new Map([
|
|
166
|
+
['breadcrumb', { component: 'Breadcrumb', converts: false }],
|
|
167
|
+
['cell', { component: 'Cell', converts: false }],
|
|
168
|
+
['grid', { component: 'Grid', converts: false }],
|
|
169
|
+
['fixed-grid', { component: 'Grid', converts: false }],
|
|
170
|
+
['checkbox', { component: 'Checkbox', converts: false }],
|
|
171
|
+
['checkboxes', { component: 'Checkboxes', converts: false }],
|
|
172
|
+
['radio', { component: 'Radio', converts: false }],
|
|
173
|
+
['radios', { component: 'Radios', converts: false }],
|
|
174
|
+
['select', { component: 'Select', converts: false }],
|
|
175
|
+
['file', { component: 'File', converts: false }],
|
|
176
|
+
['dropdown', { component: 'Dropdown', converts: false }],
|
|
177
|
+
['icon', { component: 'Icon', converts: false }],
|
|
178
|
+
['icon-text', { component: 'IconText', converts: false }],
|
|
179
|
+
['image', { component: 'Image', converts: false }],
|
|
180
|
+
['menu', { component: 'Menu', converts: false }],
|
|
181
|
+
['message', { component: 'Message', converts: false }],
|
|
182
|
+
['modal', { component: 'Modal', converts: false }],
|
|
183
|
+
['navbar-burger', { component: 'Navbar.Burger', converts: false }],
|
|
184
|
+
['navbar-link', { component: 'Navbar.Link', converts: false }],
|
|
185
|
+
['pagination', { component: 'Pagination', converts: false }],
|
|
186
|
+
['panel', { component: 'Panel', converts: false }],
|
|
187
|
+
['tabs', { component: 'Tabs', converts: false }],
|
|
188
|
+
['table-container', { component: 'Table', converts: false }],
|
|
189
|
+
['skeleton-block', { component: 'Skeleton', converts: false }],
|
|
190
|
+
['skeleton-lines', { component: 'Skeleton', converts: false }],
|
|
191
|
+
['columns', { component: 'Columns', converts: true }],
|
|
192
|
+
['column', { component: 'Column', converts: true }],
|
|
193
|
+
['container', { component: 'Container', converts: true }],
|
|
194
|
+
['section', { component: 'Section', converts: true }],
|
|
195
|
+
['hero', { component: 'Hero', converts: true }],
|
|
196
|
+
['hero-head', { component: 'Hero.Head', converts: true }],
|
|
197
|
+
['hero-body', { component: 'Hero.Body', converts: true }],
|
|
198
|
+
['hero-foot', { component: 'Hero.Foot', converts: true }],
|
|
199
|
+
['footer', { component: 'Footer', converts: true }],
|
|
200
|
+
['level', { component: 'Level', converts: true }],
|
|
201
|
+
['level-left', { component: 'Level.Left', converts: true }],
|
|
202
|
+
['level-right', { component: 'Level.Right', converts: true }],
|
|
203
|
+
['level-item', { component: 'Level.Item', converts: true }],
|
|
204
|
+
['media', { component: 'Media', converts: true }],
|
|
205
|
+
['media-left', { component: 'Media.Left', converts: true }],
|
|
206
|
+
['media-content', { component: 'Media.Content', converts: true }],
|
|
207
|
+
['media-right', { component: 'Media.Right', converts: true }],
|
|
208
|
+
['card', { component: 'Card', converts: true }],
|
|
209
|
+
['card-header', { component: 'Card.Header', converts: true }],
|
|
210
|
+
['card-header-title', { component: 'Card.Header.Title', converts: true }],
|
|
211
|
+
['card-header-icon', { component: 'Card.Header.Icon', converts: true }],
|
|
212
|
+
['card-image', { component: 'Card.Image', converts: true }],
|
|
213
|
+
['card-content', { component: 'Card.Content', converts: true }],
|
|
214
|
+
['card-footer', { component: 'Card.Footer', converts: true }],
|
|
215
|
+
['card-footer-item', { component: 'Card.FooterItem', converts: true }],
|
|
216
|
+
['navbar', { component: 'Navbar', converts: true }],
|
|
217
|
+
['navbar-brand', { component: 'Navbar.Brand', converts: true }],
|
|
218
|
+
['navbar-menu', { component: 'Navbar.Menu', converts: true }],
|
|
219
|
+
['navbar-start', { component: 'Navbar.Start', converts: true }],
|
|
220
|
+
['navbar-end', { component: 'Navbar.End', converts: true }],
|
|
221
|
+
['navbar-item', { component: 'Navbar.Item', converts: true }],
|
|
222
|
+
['navbar-dropdown', { component: 'Navbar.DropdownMenu', converts: true }],
|
|
223
|
+
['navbar-divider', { component: 'Navbar.Divider', converts: true }],
|
|
224
|
+
['field', { component: 'Field', converts: true }],
|
|
225
|
+
['field-label', { component: 'Field.Label', converts: true }],
|
|
226
|
+
['field-body', { component: 'Field.Body', converts: true }],
|
|
227
|
+
['control', { component: 'Control', converts: true }],
|
|
228
|
+
['input', { component: 'InputBase', converts: true }],
|
|
229
|
+
['textarea', { component: 'TextAreaBase', converts: true }],
|
|
230
|
+
['buttons', { component: 'Buttons', converts: true }],
|
|
231
|
+
['tags', { component: 'Tags', converts: true }],
|
|
232
|
+
['button', { component: 'Button', converts: true }],
|
|
233
|
+
['tag', { component: 'Tag', converts: true }],
|
|
234
|
+
['delete', { component: 'Delete', converts: true }],
|
|
235
|
+
['progress', { component: 'Progress', converts: true }],
|
|
236
|
+
['table', { component: 'Table', converts: true }],
|
|
237
|
+
['title', { component: 'Title', converts: true }],
|
|
238
|
+
['subtitle', { component: 'SubTitle', converts: true }],
|
|
239
|
+
['notification', { component: 'Notification', converts: true }],
|
|
240
|
+
['box', { component: 'Box', converts: true }],
|
|
241
|
+
['content', { component: 'Content', converts: true }],
|
|
242
|
+
['block', { component: 'Block', converts: true }],
|
|
243
|
+
]);
|
package/dist/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ export declare const rules: {
|
|
|
4
4
|
'no-deprecated-props': import("eslint").Rule.RuleModule;
|
|
5
5
|
'no-color-as-surface': import("eslint").Rule.RuleModule;
|
|
6
6
|
'no-inert-flex-props': import("eslint").Rule.RuleModule;
|
|
7
|
+
'no-bulma-component-class': import("eslint").Rule.RuleModule;
|
|
7
8
|
};
|
|
8
9
|
declare const plugin: ESLint.Plugin & {
|
|
9
10
|
configs: Record<string, Linter.Config>;
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAQ7C,eAAO,MAAM,KAAK;;;;;;CAMgB,CAAC;AAkBnC,QAAA,MAAM,MAAM,EAAE,MAAM,CAAC,MAAM,GAAG;IAC5B,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;CAKxC,CAAC;AAyCF,eAAO,MAAM,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,CAAkB,CAAC;AACrE,eAAe,MAAM,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
* export default [bestax.configs.recommended];
|
|
10
10
|
*/
|
|
11
11
|
import { createRequire } from 'node:module';
|
|
12
|
+
import noBulmaComponentClass from './rules/no-bulma-component-class.js';
|
|
12
13
|
import noColorAsSurface from './rules/no-color-as-surface.js';
|
|
13
14
|
import noDeprecatedProps from './rules/no-deprecated-props.js';
|
|
14
15
|
import noInertFlexProps from './rules/no-inert-flex-props.js';
|
|
@@ -19,6 +20,7 @@ export const rules = {
|
|
|
19
20
|
'no-deprecated-props': noDeprecatedProps,
|
|
20
21
|
'no-color-as-surface': noColorAsSurface,
|
|
21
22
|
'no-inert-flex-props': noInertFlexProps,
|
|
23
|
+
'no-bulma-component-class': noBulmaComponentClass,
|
|
22
24
|
};
|
|
23
25
|
/**
|
|
24
26
|
* Read at runtime rather than inlined, so semantic-release's version bump is
|
package/dist/lib/elements.d.ts
CHANGED
|
@@ -142,6 +142,40 @@ export declare function isTrueValue(attr: any): boolean;
|
|
|
142
142
|
* cannot see is not a value it can judge.
|
|
143
143
|
*/
|
|
144
144
|
export declare function literalValue(attr: any): string | null;
|
|
145
|
+
/**
|
|
146
|
+
* Whether a callee is a class joiner by its name alone, for a caller with no
|
|
147
|
+
* scope to consult. A rule passes `classJoinerAt`, which follows the binding.
|
|
148
|
+
*/
|
|
149
|
+
export declare function isClassJoinerName(callee: any): boolean;
|
|
150
|
+
/**
|
|
151
|
+
* Whether a callee, seen at `node`, joins class names: a binding imported or
|
|
152
|
+
* required from a package that does, under any name (`import cls from
|
|
153
|
+
* 'clsx'`, `const cx = require('clsx')`).
|
|
154
|
+
*
|
|
155
|
+
* Nothing else is: a function the file or the app defines is one the rule
|
|
156
|
+
* cannot see into. `const cx = classNames.bind(styles)` looks up CSS-module
|
|
157
|
+
* names, so `cx('card')` renders a hashed class, and a `cx` that maps a key
|
|
158
|
+
* to a class does the same. A joiner the app wrote (a `cn` in `lib/utils`) is
|
|
159
|
+
* missed for the same reason, as is a name nothing declares, and a scope that
|
|
160
|
+
* cannot be read: on an opt-in rule, silence is the safe direction.
|
|
161
|
+
*/
|
|
162
|
+
export declare function classJoinerAt(context: Rule.RuleContext, node: unknown): (callee: any) => boolean;
|
|
163
|
+
/**
|
|
164
|
+
* The class names a `className` value spells out, as the rule that reads
|
|
165
|
+
* plain elements needs them: a string, the static text of a template or a
|
|
166
|
+
* `+` chain, the branches of a ternary or a `&&`, the arguments of a class
|
|
167
|
+
* joiner (`clsx(…)`, `cn(…)`), an array's elements, and an object's keys.
|
|
168
|
+
*
|
|
169
|
+
* It reads only what the source spells. A word glued to an expression
|
|
170
|
+
* (`button${size}`) is not read as `button`, a computed lookup
|
|
171
|
+
* (`styles['box']`) is not a class, a call that is not a joiner is not read
|
|
172
|
+
* into, and a value it cannot see into yields nothing. Silence, never a
|
|
173
|
+
* guess.
|
|
174
|
+
*
|
|
175
|
+
* `joins` decides which calls are joiners; a rule passes `classJoinerAt`,
|
|
176
|
+
* which also follows the callee's binding.
|
|
177
|
+
*/
|
|
178
|
+
export declare function classNameTokens(attr: any, joins?: (callee: any) => boolean): Set<string>;
|
|
145
179
|
/**
|
|
146
180
|
* The attribute that WINS for `name`, or undefined.
|
|
147
181
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"elements.d.ts","sourceRoot":"","sources":["../../src/lib/elements.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAEnC,eAAO,MAAM,OAAO,4BAA4B,CAAC;AAKjD,MAAM,WAAW,aAAa;IAC5B,qDAAqD;IACrD,QAAQ,CAAC,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACpC,gCAAgC;IAChC,QAAQ,CAAC,UAAU,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;CAClC;AAED,eAAO,MAAM,YAAY,QAAO,aAG9B,CAAC;AAQH,iEAAiE;AACjE,wBAAgB,aAAa,CAAC,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,aAAa,GAAG,IAAI,CAUlE;AAED;;;;;;;;;;;;GAYG;AACH;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAoBhE;
|
|
1
|
+
{"version":3,"file":"elements.d.ts","sourceRoot":"","sources":["../../src/lib/elements.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAEnC,eAAO,MAAM,OAAO,4BAA4B,CAAC;AAKjD,MAAM,WAAW,aAAa;IAC5B,qDAAqD;IACrD,QAAQ,CAAC,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACpC,gCAAgC;IAChC,QAAQ,CAAC,UAAU,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;CAClC;AAED,eAAO,MAAM,YAAY,QAAO,aAG9B,CAAC;AAQH,iEAAiE;AACjE,wBAAgB,aAAa,CAAC,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,aAAa,GAAG,IAAI,CAUlE;AAED;;;;;;;;;;;;GAYG;AACH;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAoBhE;AAgBD,wBAAgB,gBAAgB,CAAC,UAAU,EAAE,GAAG,GAAG,OAAO,CAEzD;AAED,wBAAgB,cAAc,CAAC,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,aAAa,GAAG,IAAI,CAenE;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,cAAc,CAC5B,QAAQ,EAAE,GAAG,EACb,OAAO,EAAE,aAAa,EACtB,aAAa,GAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAoB,GACpD,MAAM,GAAG,IAAI,CAsBf;AAED;;;;;;;;;GASG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,GAAG,GAAG,MAAM,GAAG,IAAI,CAiBrD;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,GAAG,GAAG,GAAG,EAAE,CAKrD;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,GAAG,GAAG,GAAG,EAAE,CAOpD;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,GAAG,GAAG,OAAO,CAYpD;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,GAAG,GAAG,OAAO,CAOpD;AAED;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,GAAG,GAAG,OAAO,CAQ9C;AAED;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,GAAG,GAAG,MAAM,GAAG,IAAI,CAYrD;AAmFD;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,GAAG,GAAG,OAAO,CAEtD;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,aAAa,CAC3B,OAAO,EAAE,IAAI,CAAC,WAAW,EACzB,IAAI,EAAE,OAAO,GACZ,CAAC,MAAM,EAAE,GAAG,KAAK,OAAO,CAa1B;AA6ED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,eAAe,CAC7B,IAAI,EAAE,GAAG,EACT,KAAK,GAAE,CAAC,MAAM,EAAE,GAAG,KAAK,OAA2B,GAClD,GAAG,CAAC,MAAM,CAAC,CAUb;AAED;;;;;;;;GAQG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,MAAM,GAAG,GAAG,CAKzD;AAED,sEAAsE;AACtE,wBAAgB,YAAY,CAAC,OAAO,EAAE,GAAG,GAAG,GAAG,EAAE,CAIhD;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,GAAG,GAAG,GAAG,CAAC,MAAM,CAAC,CAUtD;AAED,6EAA6E;AAC7E,wBAAgB,SAAS,CAAC,OAAO,EAAE,GAAG,GAAG,OAAO,CAI/C;AAiCD;;;;;;GAMG;AACH,wBAAgB,eAAe,CAC7B,OAAO,EAAE,IAAI,CAAC,WAAW,EACzB,IAAI,EAAE,OAAO,GACZ,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAc3B;AAED;;;GAGG;AACH,wBAAgB,SAAS,CACvB,OAAO,EAAE,IAAI,CAAC,WAAW,EACzB,OAAO,EAAE,GAAG,EACZ,OAAO,EAAE,aAAa,GACrB,MAAM,GAAG,IAAI,CAMf;AAED;;;;;;;;;GASG;AACH,wBAAgB,WAAW,IAAI;IAC7B,OAAO,EAAE,aAAa,CAAC;IACvB,OAAO,EAAE,IAAI,CAAC,YAAY,CAAC;CAC5B,CA0CA"}
|
package/dist/lib/elements.js
CHANGED
|
@@ -64,12 +64,16 @@ export function patternBinds(pattern, name) {
|
|
|
64
64
|
return false;
|
|
65
65
|
}
|
|
66
66
|
}
|
|
67
|
-
|
|
67
|
+
/** Whether a declarator's value is `require(<specifier>)` for a matching one. */
|
|
68
|
+
function requires(declarator, matches) {
|
|
68
69
|
const init = declarator?.init;
|
|
69
70
|
return (init?.type === 'CallExpression' &&
|
|
70
71
|
init.callee?.name === 'require' &&
|
|
71
72
|
init.arguments?.length === 1 &&
|
|
72
|
-
init.arguments[0]?.value
|
|
73
|
+
matches(init.arguments[0]?.value));
|
|
74
|
+
}
|
|
75
|
+
export function isLibraryRequire(declarator) {
|
|
76
|
+
return requires(declarator, specifier => specifier === PACKAGE);
|
|
73
77
|
}
|
|
74
78
|
export function collectRequire(node, into) {
|
|
75
79
|
if (!isLibraryRequire(node))
|
|
@@ -281,6 +285,215 @@ export function literalValue(attr) {
|
|
|
281
285
|
}
|
|
282
286
|
return null;
|
|
283
287
|
}
|
|
288
|
+
/** A gap in a class string: text an expression supplies at runtime. */
|
|
289
|
+
const HOLE = Symbol('hole');
|
|
290
|
+
/** Adjacent text is one run: `'card' + '-' + x` glues `card` to the hole. */
|
|
291
|
+
function coalesce(parts) {
|
|
292
|
+
const out = [];
|
|
293
|
+
for (const part of parts) {
|
|
294
|
+
const last = out[out.length - 1];
|
|
295
|
+
if (part !== HOLE && typeof last === 'string') {
|
|
296
|
+
out[out.length - 1] = last + part;
|
|
297
|
+
}
|
|
298
|
+
else {
|
|
299
|
+
out.push(part);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
return out;
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* The static text of a template literal or a `+` chain, with a HOLE where an
|
|
306
|
+
* expression goes, or null for any other node.
|
|
307
|
+
*/
|
|
308
|
+
function spelledParts(node) {
|
|
309
|
+
if (node?.type === 'TemplateLiteral') {
|
|
310
|
+
const parts = [];
|
|
311
|
+
node.quasis.forEach((quasi, i) => {
|
|
312
|
+
if (i > 0)
|
|
313
|
+
parts.push(HOLE);
|
|
314
|
+
parts.push(quasi.value.cooked ?? '');
|
|
315
|
+
});
|
|
316
|
+
return parts;
|
|
317
|
+
}
|
|
318
|
+
if (node?.type === 'BinaryExpression' && node.operator === '+') {
|
|
319
|
+
const side = (operand) => {
|
|
320
|
+
if (operand?.type === 'Literal' && typeof operand.value === 'string') {
|
|
321
|
+
return [operand.value];
|
|
322
|
+
}
|
|
323
|
+
return spelledParts(operand) ?? [HOLE];
|
|
324
|
+
};
|
|
325
|
+
return coalesce([...side(node.left), ...side(node.right)]);
|
|
326
|
+
}
|
|
327
|
+
return null;
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* The class names in spelled text. A word touching a hole is glued to
|
|
331
|
+
* whatever the expression supplies (`button${size}` is not `button`), so it is
|
|
332
|
+
* not a class this can read.
|
|
333
|
+
*/
|
|
334
|
+
function wordsOf(parts, into) {
|
|
335
|
+
parts.forEach((part, i) => {
|
|
336
|
+
if (part === HOLE)
|
|
337
|
+
return;
|
|
338
|
+
const words = part.split(/\s+/);
|
|
339
|
+
if (i > 0 && parts[i - 1] === HOLE)
|
|
340
|
+
words[0] = '';
|
|
341
|
+
if (i < parts.length - 1 && parts[i + 1] === HOLE) {
|
|
342
|
+
words[words.length - 1] = '';
|
|
343
|
+
}
|
|
344
|
+
for (const word of words)
|
|
345
|
+
if (word)
|
|
346
|
+
into.add(word);
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
/** Class-joining functions, by the names apps give them. */
|
|
350
|
+
const JOINER_NAMES = new Set([
|
|
351
|
+
'clsx',
|
|
352
|
+
'classnames',
|
|
353
|
+
'classNames',
|
|
354
|
+
'cx',
|
|
355
|
+
'cn',
|
|
356
|
+
'twMerge',
|
|
357
|
+
'twJoin',
|
|
358
|
+
]);
|
|
359
|
+
/** Packages whose export joins class names, whatever the import calls it. */
|
|
360
|
+
const JOINER_PACKAGES = new Set([
|
|
361
|
+
'clsx',
|
|
362
|
+
'clsx/lite',
|
|
363
|
+
'classnames',
|
|
364
|
+
'classnames/dedupe',
|
|
365
|
+
'tailwind-merge',
|
|
366
|
+
]);
|
|
367
|
+
/**
|
|
368
|
+
* Whether a callee is a class joiner by its name alone, for a caller with no
|
|
369
|
+
* scope to consult. A rule passes `classJoinerAt`, which follows the binding.
|
|
370
|
+
*/
|
|
371
|
+
export function isClassJoinerName(callee) {
|
|
372
|
+
return callee?.type === 'Identifier' && JOINER_NAMES.has(callee.name);
|
|
373
|
+
}
|
|
374
|
+
/**
|
|
375
|
+
* Whether a callee, seen at `node`, joins class names: a binding imported or
|
|
376
|
+
* required from a package that does, under any name (`import cls from
|
|
377
|
+
* 'clsx'`, `const cx = require('clsx')`).
|
|
378
|
+
*
|
|
379
|
+
* Nothing else is: a function the file or the app defines is one the rule
|
|
380
|
+
* cannot see into. `const cx = classNames.bind(styles)` looks up CSS-module
|
|
381
|
+
* names, so `cx('card')` renders a hashed class, and a `cx` that maps a key
|
|
382
|
+
* to a class does the same. A joiner the app wrote (a `cn` in `lib/utils`) is
|
|
383
|
+
* missed for the same reason, as is a name nothing declares, and a scope that
|
|
384
|
+
* cannot be read: on an opt-in rule, silence is the safe direction.
|
|
385
|
+
*/
|
|
386
|
+
export function classJoinerAt(context, node) {
|
|
387
|
+
const lookup = variableAt(context, node);
|
|
388
|
+
return callee => {
|
|
389
|
+
if (callee?.type !== 'Identifier')
|
|
390
|
+
return false;
|
|
391
|
+
const variable = lookup(callee.name);
|
|
392
|
+
if (!variable)
|
|
393
|
+
return false;
|
|
394
|
+
return (variable.defs ?? []).some((d) => d.type === 'ImportBinding'
|
|
395
|
+
? JOINER_PACKAGES.has(String(d.parent?.source?.value))
|
|
396
|
+
: d.type === 'Variable' &&
|
|
397
|
+
requires(d.node, specifier => JOINER_PACKAGES.has(String(specifier))));
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
function collectClassNames(node, into, joins) {
|
|
401
|
+
if (!node)
|
|
402
|
+
return;
|
|
403
|
+
const read = (child) => collectClassNames(child, into, joins);
|
|
404
|
+
const spelled = spelledParts(node);
|
|
405
|
+
if (spelled) {
|
|
406
|
+
wordsOf(spelled, into);
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
switch (node.type) {
|
|
410
|
+
case 'Literal':
|
|
411
|
+
if (typeof node.value === 'string')
|
|
412
|
+
wordsOf([node.value], into);
|
|
413
|
+
return;
|
|
414
|
+
case 'ConditionalExpression':
|
|
415
|
+
read(node.consequent);
|
|
416
|
+
read(node.alternate);
|
|
417
|
+
return;
|
|
418
|
+
case 'LogicalExpression':
|
|
419
|
+
read(node.left);
|
|
420
|
+
read(node.right);
|
|
421
|
+
return;
|
|
422
|
+
case 'CallExpression': {
|
|
423
|
+
// Only a class joiner's arguments are class text (`clsx('box', …)`);
|
|
424
|
+
// any other call may take a lookup key (`t('button')`).
|
|
425
|
+
if (joins(node.callee)) {
|
|
426
|
+
for (const arg of node.arguments ?? [])
|
|
427
|
+
read(arg);
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
// `['box', x].join(' ')`: an array joined into a string. Only a
|
|
431
|
+
// whitespace separator keeps each element a class of its own:
|
|
432
|
+
// `['card', x].join('-')` renders `card-<x>`, and no argument joins on
|
|
433
|
+
// a comma, so neither keeps its words.
|
|
434
|
+
const callee = node.callee;
|
|
435
|
+
const [separator] = node.arguments ?? [];
|
|
436
|
+
if (callee?.type === 'MemberExpression' &&
|
|
437
|
+
!callee.computed &&
|
|
438
|
+
callee.property?.name === 'join' &&
|
|
439
|
+
callee.object?.type === 'ArrayExpression' &&
|
|
440
|
+
separator?.type === 'Literal' &&
|
|
441
|
+
typeof separator.value === 'string' &&
|
|
442
|
+
/^\s+$/.test(separator.value)) {
|
|
443
|
+
read(callee.object);
|
|
444
|
+
}
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
447
|
+
case 'ArrayExpression':
|
|
448
|
+
for (const element of node.elements ?? []) {
|
|
449
|
+
read(element?.type === 'SpreadElement' ? element.argument : element);
|
|
450
|
+
}
|
|
451
|
+
return;
|
|
452
|
+
case 'ObjectExpression':
|
|
453
|
+
// `{ 'is-active': on }`: the keys are the classes, the values decide.
|
|
454
|
+
for (const property of node.properties ?? []) {
|
|
455
|
+
if (property?.type !== 'Property' || property.computed)
|
|
456
|
+
continue;
|
|
457
|
+
if (property.key?.type === 'Identifier')
|
|
458
|
+
into.add(property.key.name);
|
|
459
|
+
else
|
|
460
|
+
read(property.key);
|
|
461
|
+
}
|
|
462
|
+
return;
|
|
463
|
+
case 'TSAsExpression':
|
|
464
|
+
case 'TSSatisfiesExpression':
|
|
465
|
+
case 'TSNonNullExpression':
|
|
466
|
+
case 'ChainExpression':
|
|
467
|
+
read(node.expression);
|
|
468
|
+
return;
|
|
469
|
+
default:
|
|
470
|
+
// An identifier, a member lookup, anything else: text this cannot see.
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
/**
|
|
475
|
+
* The class names a `className` value spells out, as the rule that reads
|
|
476
|
+
* plain elements needs them: a string, the static text of a template or a
|
|
477
|
+
* `+` chain, the branches of a ternary or a `&&`, the arguments of a class
|
|
478
|
+
* joiner (`clsx(…)`, `cn(…)`), an array's elements, and an object's keys.
|
|
479
|
+
*
|
|
480
|
+
* It reads only what the source spells. A word glued to an expression
|
|
481
|
+
* (`button${size}`) is not read as `button`, a computed lookup
|
|
482
|
+
* (`styles['box']`) is not a class, a call that is not a joiner is not read
|
|
483
|
+
* into, and a value it cannot see into yields nothing. Silence, never a
|
|
484
|
+
* guess.
|
|
485
|
+
*
|
|
486
|
+
* `joins` decides which calls are joiners; a rule passes `classJoinerAt`,
|
|
487
|
+
* which also follows the callee's binding.
|
|
488
|
+
*/
|
|
489
|
+
export function classNameTokens(attr, joins = isClassJoinerName) {
|
|
490
|
+
const tokens = new Set();
|
|
491
|
+
const v = attr?.value;
|
|
492
|
+
if (!v)
|
|
493
|
+
return tokens;
|
|
494
|
+
collectClassNames(v.type === 'JSXExpressionContainer' ? v.expression : v, tokens, joins);
|
|
495
|
+
return tokens;
|
|
496
|
+
}
|
|
284
497
|
/**
|
|
285
498
|
* The attribute that WINS for `name`, or undefined.
|
|
286
499
|
*
|
|
@@ -328,6 +541,33 @@ export function doubledNames(opening) {
|
|
|
328
541
|
export function hasSpread(opening) {
|
|
329
542
|
return (opening?.attributes ?? []).some((a) => a?.type === 'JSXSpreadAttribute');
|
|
330
543
|
}
|
|
544
|
+
/**
|
|
545
|
+
* The variable a name resolves to from `node`'s scope: the variable, null
|
|
546
|
+
* when nothing declares the name, or undefined when the scope cannot be read.
|
|
547
|
+
* The scope is read once, however many names are asked about.
|
|
548
|
+
*/
|
|
549
|
+
function variableAt(context, node) {
|
|
550
|
+
let scope;
|
|
551
|
+
let unreadable = false;
|
|
552
|
+
return name => {
|
|
553
|
+
if (scope === undefined && !unreadable) {
|
|
554
|
+
try {
|
|
555
|
+
scope = context.sourceCode.getScope(node);
|
|
556
|
+
}
|
|
557
|
+
catch {
|
|
558
|
+
unreadable = true;
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
if (unreadable)
|
|
562
|
+
return undefined;
|
|
563
|
+
for (let current = scope ?? null; current; current = current.upper) {
|
|
564
|
+
const variable = current.variables.find((v) => v.name === name);
|
|
565
|
+
if (variable)
|
|
566
|
+
return variable;
|
|
567
|
+
}
|
|
568
|
+
return null;
|
|
569
|
+
};
|
|
570
|
+
}
|
|
331
571
|
/**
|
|
332
572
|
* True when `name`, seen at `node`, resolves to an import binding rather than
|
|
333
573
|
* to a local declaration that shadows it.
|
|
@@ -336,26 +576,16 @@ export function hasSpread(opening) {
|
|
|
336
576
|
* here costs a report on library code, which is less bad than going quiet.
|
|
337
577
|
*/
|
|
338
578
|
export function bindsToImportAt(context, node) {
|
|
579
|
+
const lookup = variableAt(context, node);
|
|
339
580
|
return name => {
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
scope = context.sourceCode.getScope(node);
|
|
343
|
-
}
|
|
344
|
-
catch {
|
|
581
|
+
const variable = lookup(name);
|
|
582
|
+
if (!variable)
|
|
345
583
|
return true;
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
// is the same binding by another spelling. Anything else is a local
|
|
352
|
-
// that shadows ours.
|
|
353
|
-
return (variable.defs ?? []).some((d) => d.type === 'ImportBinding' ||
|
|
354
|
-
(d.type === 'Variable' && isLibraryRequire(d.node)));
|
|
355
|
-
}
|
|
356
|
-
scope = scope.upper;
|
|
357
|
-
}
|
|
358
|
-
return true;
|
|
584
|
+
// An import binding, or the `const { Box } = require(…)` form, which is
|
|
585
|
+
// the same binding by another spelling. Anything else is a local that
|
|
586
|
+
// shadows ours.
|
|
587
|
+
return (variable.defs ?? []).some((d) => d.type === 'ImportBinding' ||
|
|
588
|
+
(d.type === 'Variable' && isLibraryRequire(d.node)));
|
|
359
589
|
};
|
|
360
590
|
}
|
|
361
591
|
/**
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Report a plain element styled with a Bulma class bestax has a component
|
|
3
|
+
* for: `<button className="button is-primary">` is bestax `Button`.
|
|
4
|
+
*
|
|
5
|
+
* The first rule here that reads intrinsic elements rather than the
|
|
6
|
+
* library's own, which is why it does not start from the bestax import: a
|
|
7
|
+
* lowercase tag is plain HTML whatever the file imports. A custom element
|
|
8
|
+
* (`<my-card>`) and anything inside `<svg>` or `<math>` are not HTML, and a
|
|
9
|
+
* capitalised tag is a component, so all three are left alone.
|
|
10
|
+
*
|
|
11
|
+
* OPT-IN, like `no-color-as-surface`: the markup works, and this rule is a
|
|
12
|
+
* preference for the component over the class. It is for an app that has
|
|
13
|
+
* moved onto bestax (by hand or with `bestax-migrate bulma-classes`) and
|
|
14
|
+
* wants to keep raw Bulma markup from coming back.
|
|
15
|
+
*
|
|
16
|
+
* Report only: no fix, no suggestion. Whether an element converts depends on
|
|
17
|
+
* its tag, attributes and children, and the codemod is the one place that
|
|
18
|
+
* judges that; a second converter here would drift from it.
|
|
19
|
+
*
|
|
20
|
+
* Which classes: those bestax-migrate's table names a component for, read
|
|
21
|
+
* into `BULMA_COMPONENT_CLASSES`. Helper classes are not reported (valid on
|
|
22
|
+
* any tag, and a `<div>` has no bestax wrapper to move them to), nor classes
|
|
23
|
+
* bestax renders only inside a component (`.label`, `.help`), nor the parts
|
|
24
|
+
* of a family, since the family's own class is reported where it sits. An
|
|
25
|
+
* element carrying several is reported once, for the class the codemod
|
|
26
|
+
* would decide by.
|
|
27
|
+
*
|
|
28
|
+
* A spread does not silence it: the report is about the class the author
|
|
29
|
+
* wrote, which is there whether or not a spread later replaces it.
|
|
30
|
+
*/
|
|
31
|
+
import type { Rule } from 'eslint';
|
|
32
|
+
declare const rule: Rule.RuleModule;
|
|
33
|
+
export default rule;
|
|
34
|
+
//# sourceMappingURL=no-bulma-component-class.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"no-bulma-component-class.d.ts","sourceRoot":"","sources":["../../src/rules/no-bulma-component-class.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAiCnC,QAAA,MAAM,IAAI,EAAE,IAAI,CAAC,UAgDhB,CAAC;AAEF,eAAe,IAAI,CAAC"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { BULMA_COMPONENT_CLASSES } from '../generated/metadata.js';
|
|
2
|
+
import { attributesOf, classJoinerAt, classNameTokens, namedAttr, } from '../lib/elements.js';
|
|
3
|
+
/** A lowercase HTML tag: not a component, not a custom element. */
|
|
4
|
+
const INTRINSIC = /^[a-z][a-z0-9]*$/;
|
|
5
|
+
/** Subtrees whose tags are not HTML. */
|
|
6
|
+
const FOREIGN = new Set(['svg', 'math']);
|
|
7
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
8
|
+
function tagOf(opening) {
|
|
9
|
+
const name = opening?.name;
|
|
10
|
+
return name?.type === 'JSXIdentifier' && INTRINSIC.test(name.name)
|
|
11
|
+
? name.name
|
|
12
|
+
: null;
|
|
13
|
+
}
|
|
14
|
+
/** Whether an ancestor element is `<svg>` or `<math>`. */
|
|
15
|
+
function insideForeign(ancestors) {
|
|
16
|
+
return ancestors.some(ancestor => ancestor?.type === 'JSXElement' &&
|
|
17
|
+
FOREIGN.has(tagOf(ancestor.openingElement) ?? ''));
|
|
18
|
+
}
|
|
19
|
+
/* eslint-enable @typescript-eslint/no-explicit-any */
|
|
20
|
+
const rule = {
|
|
21
|
+
meta: {
|
|
22
|
+
type: 'suggestion',
|
|
23
|
+
docs: {
|
|
24
|
+
description: 'prefer the bestax component over a plain element styled with its Bulma class',
|
|
25
|
+
url: 'https://bestax.io/docs/guides/getting-started/eslint-plugin#no-bulma-component-class',
|
|
26
|
+
},
|
|
27
|
+
schema: [],
|
|
28
|
+
messages: {
|
|
29
|
+
converts: "`<{{tag}}>` is styled with Bulma's `.{{cls}}`, which bestax renders as `{{component}}`. `bestax-migrate bulma-classes` converts elements like this one where the markup allows.",
|
|
30
|
+
family: "`<{{tag}}>` is styled with Bulma's `.{{cls}}`, and bestax has `{{component}}` for it. `bestax-migrate bulma-classes` leaves this markup for you to convert by hand.",
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
create(context) {
|
|
34
|
+
return {
|
|
35
|
+
JSXOpeningElement(node) {
|
|
36
|
+
const tag = tagOf(node);
|
|
37
|
+
if (tag === null || FOREIGN.has(tag))
|
|
38
|
+
return;
|
|
39
|
+
// The cheap check first: most elements carry no className at all.
|
|
40
|
+
const attr = namedAttr(attributesOf(node), 'className');
|
|
41
|
+
if (!attr)
|
|
42
|
+
return;
|
|
43
|
+
if (insideForeign(context.sourceCode.getAncestors(node))) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
const tokens = [...classNameTokens(attr, classJoinerAt(context, node))];
|
|
47
|
+
// The class the codemod decides by: the first family class in the
|
|
48
|
+
// order the element carries them, else the converted root it ranks
|
|
49
|
+
// first, which is the table's own order.
|
|
50
|
+
const family = tokens.find(token => BULMA_COMPONENT_CLASSES.get(token)?.converts === false);
|
|
51
|
+
const cls = family ??
|
|
52
|
+
[...BULMA_COMPONENT_CLASSES.keys()].find(key => tokens.includes(key));
|
|
53
|
+
if (cls === undefined)
|
|
54
|
+
return;
|
|
55
|
+
const entry = BULMA_COMPONENT_CLASSES.get(cls);
|
|
56
|
+
context.report({
|
|
57
|
+
node: attr,
|
|
58
|
+
messageId: entry.converts ? 'converts' : 'family',
|
|
59
|
+
data: { tag, cls, component: entry.component },
|
|
60
|
+
});
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
export default rule;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@allxsmith/eslint-plugin-bestax",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "ESLint rules for @allxsmith/bestax-bulma — catches helper-prop values the library silently drops, deprecated props, and the color-as-surface mistake",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"url": "https://github.com/allxsmith/bestax/issues"
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@allxsmith/bestax-bulma": "^5.16.
|
|
44
|
+
"@allxsmith/bestax-bulma": "^5.16.9"
|
|
45
45
|
},
|
|
46
46
|
"peerDependencies": {
|
|
47
47
|
"eslint": "^10.0.0"
|
|
@@ -72,5 +72,5 @@
|
|
|
72
72
|
"clean": "rimraf dist",
|
|
73
73
|
"release": "npx semantic-release"
|
|
74
74
|
},
|
|
75
|
-
"readme": "# @allxsmith/eslint-plugin-bestax\n\n[](https://www.npmjs.com/package/@allxsmith/eslint-plugin-bestax)\n[](https://www.npmjs.com/package/@allxsmith/eslint-plugin-bestax)\n[](https://opensource.org/licenses/MIT)\n[](https://socket.dev/npm/package/@allxsmith/eslint-plugin-bestax/overview)\n[](https://scorecard.dev/viewer/?uri=github.com/allxsmith/bestax)\n[](https://www.bestpractices.dev/projects/14361)\n[](https://github.com/allxsmith/bestax/blob/main/SECURITY.md)\n\nESLint rules for [`@allxsmith/bestax-bulma`](https://www.npmjs.com/package/@allxsmith/bestax-bulma).\n\nEvery rule here reports code that does not do what it looks like it does. The\nlibrary's helper props validate by membership and emit nothing for a value they\ndo not recognise, with no throw and no warning, so `textColor=\"blue\"` renders\nnothing and says nothing.\n\n**Scope, up front:** the helper props are typed as literal unions, so if you\nwrite bestax in `.tsx` and run `tsc`, it already catches a wrong literal. This\nplugin is for the places that check does not reach: JavaScript and JSX\nprojects, and lint stages that run before or instead of typechecking. The deprecation and flex rules are additive everywhere, since\nno type error marks a deprecated prop or a flex prop that emits nothing.\n\n## Requirements\n\nESLint 10, using [flat config](https://eslint.org/docs/latest/use/configure/configuration-files).\nThe plugin is ESM-only, which does not rule out a CommonJS config. An\n`eslint.config.cjs` can `require()` it on a Node that supports `require(esm)`,\nand `configs.recommended` reads straight off the result with no interop hop,\nbecause `rules` and `configs` are named exports alongside the default. On a\nNode without that support it throws `ERR_REQUIRE_ESM`, and an `import` from\n`eslint.config.mjs` works either way.\n\n## Setup\n\n```bash\nnpm install --save-dev @allxsmith/eslint-plugin-bestax\n```\n\n### TypeScript\n\nThe preset sets no `parser`, so a TypeScript parser has to come from a config\nobject of your own. Put it first; the preset after:\n\n```js\n// eslint.config.js\nimport bestax from '@allxsmith/eslint-plugin-bestax';\nimport parser from '@typescript-eslint/parser';\n\nexport default [\n { files: ['**/*.{ts,tsx}'], languageOptions: { parser } },\n bestax.configs.recommended,\n];\n```\n\nAny TypeScript setup that sets a parser works the same way, `typescript-eslint`\nincluded, precisely because the preset does not set one of its own.\n\nThe parser is a requirement, not a nicety. The preset matches `.tsx` because\nthat is where a TypeScript project's JSX lives, and with no parser supplied it\nhands that file to the default one, which stops at the first type annotation:\n`Parsing error: The keyword 'interface' is reserved`.\n\n### JavaScript and JSX\n\nIf the project contains no `.tsx` at all, the preset alone is enough:\n\n```js\n// eslint.config.js\nimport bestax from '@allxsmith/eslint-plugin-bestax';\n\nexport default [bestax.configs.recommended];\n```\n\nAdd one `.tsx` file later and you need the parser block above. The failure is\nloud rather than silent, which is the intended trade: leaving `.tsx` out of the\npreset's own `files` would mean a TypeScript project got no rules on any `.tsx`\nfile and no indication why.\n\n### Picking rules yourself\n\n`files` is load-bearing: a flat config object without it inherits ESLint's\ndefault `**/*.{js,mjs,cjs}` set, so leaving it out silently lints none of your\nJSX.\n\n```js\nimport bestax from '@allxsmith/eslint-plugin-bestax';\n\nexport default [\n {\n files: ['**/*.{jsx,tsx}'],\n plugins: { '@allxsmith/bestax': bestax },\n languageOptions: { parserOptions: { ecmaFeatures: { jsx: true } } },\n rules: {\n '@allxsmith/bestax/valid-helper-value': 'error',\n '@allxsmith/bestax/no-deprecated-props': 'warn',\n },\n },\n];\n```\n\n## Rules\n\nIn `recommended` — each reports code that does not do what it says:\n\n| Rule | Fixable | What it catches |\n| --------------------- | ------- | ----------------------------------------------------------------------------------------------------------------------- |\n| `valid-helper-value` | — | Helper values the library silently drops: `textAlign=\"center\"` (it is `centered`), `textSize=\"8\"`, `mt=\"1rem\"` |\n| `no-deprecated-props` | partly | Props the library deprecated: `isFullWidth`, `gapSize*`, `icon`. Fixes only the renames that cannot change what renders |\n| `no-inert-flex-props` | — | `justifyContent` and friends without a flex `display`, which emit nothing |\n\nOpt-in — this one reports code that works, and buys explicitness instead:\n\n| Rule | Fixable | What it asks for |\n| --------------------- | ------- | ----------------------------------------------------------------------------- |\n| `no-color-as-surface` | yes | `textColor` instead of the `color` alias, where `color` cannot mean a surface |\n\nEach rule resolves elements through the import and through scope, so neither\nyour own `<Box>` nor a local shadowing the imported one is linted against\nBulma's rules, and each skips a value it cannot read as a literal.\n\nA spread is handled by what it can change: `no-color-as-surface` and\n`no-inert-flex-props` go silent, since a spread may carry the prop that makes\nthe code correct; `no-deprecated-props` reports but offers no fix; and\n`valid-helper-value` judges only the values that actually render, so a spread\nBEFORE the attribute leaves it reporting and a spread after it does not: JSX is\nlast-wins throughout, spreads included. No autofix changes what the code\nrenders.\n\n### valid-helper-value\n\n```jsx\n<Box textAlign=\"center\" /> // ✗ not a value textAlign accepts. Did you mean `centered`?\n<Box textAlign=\"centered\" /> // ✓\n```\n\nThe valid values come from the library's own exported tuples at lint time, by\nway of its `/constants` subpath, rather than from a list copied into this\npackage. The version those tuples come from is the copy of\n`@allxsmith/bestax-bulma` this plugin resolves, which in an ordinary deduped\ninstall is the same one your app uses; across a major bump it may not be, so\nkeep the two in step.\n\nOne family is outside its reach, knowingly: component-specific `color` props\nhave their own unions, so `<Button color=\"ghost\">` is correct and reporting it\nwould be worse than reporting nothing. And one element shadows a helper name:\n`Theme` takes a prop per Bulma CSS variable, so its `radius` sets\n`--bulma-radius` rather than emitting a class, while its type still says\notherwise. The rule skips that one pair until the library picks a meaning.\n\n### no-deprecated-props\n\n```jsx\n<Button isFullWidth /> // ✗ deprecated → fixes to isFullwidth\n<Tabs color=\"info\" /> // ✗ no .tabs.is-<color> CSS exists; no fix offered\n```\n\nThe table is generated from the library's own TSDoc, so a rename lands here\nwith the library rather than after someone notices.\n\n### no-color-as-surface\n\nOff by default. `color` on the content elements is a documented alias for\n`textColor`, so this rule is about saying which you meant, not about broken\ncode.\n\n```jsx\n<Box color=\"primary\" /> // ✗ ambiguous → fixes to textColor\n<Box bgColor=\"primary\" /> // ✓ a coloured surface\n<Button color=\"primary\" /> // ✓ Button really has an is-primary variant\n\n// ✓ silent: the background is explicit, so `color` is plainly the text half\n<Box bgColor=\"info\" color=\"primary\" />\n```\n\n### no-inert-flex-props\n\n```jsx\n<Box justifyContent=\"center\" /> // ✗ emits nothing\n<Box display=\"flex\" justifyContent=\"center\" /> // ✓\n<Box flexGrow=\"1\" /> // ✓ child props always apply\n```\n\n## Links\n\n- [Documentation](https://bestax.io/docs/guides/getting-started/eslint-plugin)\n- [Component library](https://www.npmjs.com/package/@allxsmith/bestax-bulma)\n- [Repository](https://github.com/allxsmith/bestax)\n\n## License\n\nMIT\n"
|
|
75
|
+
"readme": "# @allxsmith/eslint-plugin-bestax\n\n[](https://www.npmjs.com/package/@allxsmith/eslint-plugin-bestax)\n[](https://www.npmjs.com/package/@allxsmith/eslint-plugin-bestax)\n[](https://www.npmjs.com/package/@allxsmith/eslint-plugin-bestax#provenance)\n[](https://opensource.org/licenses/MIT)\n[](https://socket.dev/npm/package/@allxsmith/eslint-plugin-bestax/overview)\n[](https://scorecard.dev/viewer/?uri=github.com/allxsmith/bestax)\n[](https://www.bestpractices.dev/projects/14361)\n[](https://github.com/allxsmith/bestax/blob/main/SECURITY.md)\n\nESLint rules for [`@allxsmith/bestax-bulma`](https://www.npmjs.com/package/@allxsmith/bestax-bulma).\n\nEvery rule here reports code that does not do what it looks like it does. The\nlibrary's helper props validate by membership and emit nothing for a value they\ndo not recognise, with no throw and no warning, so `textColor=\"blue\"` renders\nnothing and says nothing.\n\n**Scope, up front:** the helper props are typed as literal unions, so if you\nwrite bestax in `.tsx` and run `tsc`, it already catches a wrong literal. This\nplugin is for the places that check does not reach: JavaScript and JSX\nprojects, and lint stages that run before or instead of typechecking. The deprecation and flex rules are additive everywhere, since\nno type error marks a deprecated prop or a flex prop that emits nothing.\n\n## Requirements\n\nESLint 10, using [flat config](https://eslint.org/docs/latest/use/configure/configuration-files).\nThe plugin is ESM-only, which does not rule out a CommonJS config. An\n`eslint.config.cjs` can `require()` it on a Node that supports `require(esm)`,\nand `configs.recommended` reads straight off the result with no interop hop,\nbecause `rules` and `configs` are named exports alongside the default. On a\nNode without that support it throws `ERR_REQUIRE_ESM`, and an `import` from\n`eslint.config.mjs` works either way.\n\n## Setup\n\n```bash\nnpm install --save-dev @allxsmith/eslint-plugin-bestax\n```\n\n### TypeScript\n\nThe preset sets no `parser`, so a TypeScript parser has to come from a config\nobject of your own. Put it first; the preset after:\n\n```js\n// eslint.config.js\nimport bestax from '@allxsmith/eslint-plugin-bestax';\nimport parser from '@typescript-eslint/parser';\n\nexport default [\n { files: ['**/*.{ts,tsx}'], languageOptions: { parser } },\n bestax.configs.recommended,\n];\n```\n\nAny TypeScript setup that sets a parser works the same way, `typescript-eslint`\nincluded, precisely because the preset does not set one of its own.\n\nThe parser is a requirement, not a nicety. The preset matches `.tsx` because\nthat is where a TypeScript project's JSX lives, and with no parser supplied it\nhands that file to the default one, which stops at the first type annotation:\n`Parsing error: The keyword 'interface' is reserved`.\n\n### JavaScript and JSX\n\nIf the project contains no `.tsx` at all, the preset alone is enough:\n\n```js\n// eslint.config.js\nimport bestax from '@allxsmith/eslint-plugin-bestax';\n\nexport default [bestax.configs.recommended];\n```\n\nAdd one `.tsx` file later and you need the parser block above. The failure is\nloud rather than silent, which is the intended trade: leaving `.tsx` out of the\npreset's own `files` would mean a TypeScript project got no rules on any `.tsx`\nfile and no indication why.\n\n### Picking rules yourself\n\n`files` is load-bearing: a flat config object without it inherits ESLint's\ndefault `**/*.{js,mjs,cjs}` set, so leaving it out silently lints none of your\nJSX.\n\n```js\nimport bestax from '@allxsmith/eslint-plugin-bestax';\n\nexport default [\n {\n files: ['**/*.{jsx,tsx}'],\n plugins: { '@allxsmith/bestax': bestax },\n languageOptions: { parserOptions: { ecmaFeatures: { jsx: true } } },\n rules: {\n '@allxsmith/bestax/valid-helper-value': 'error',\n '@allxsmith/bestax/no-deprecated-props': 'warn',\n },\n },\n];\n```\n\n## Rules\n\nIn `recommended` — each reports code that does not do what it says:\n\n| Rule | Fixable | What it catches |\n| --------------------- | ------- | ----------------------------------------------------------------------------------------------------------------------- |\n| `valid-helper-value` | — | Helper values the library silently drops: `textAlign=\"center\"` (it is `centered`), `textSize=\"8\"`, `mt=\"1rem\"` |\n| `no-deprecated-props` | partly | Props the library deprecated: `isFullWidth`, `gapSize*`, `icon`. Fixes only the renames that cannot change what renders |\n| `no-inert-flex-props` | — | `justifyContent` and friends without a flex `display`, which emit nothing |\n\nOpt-in — these report code that works, and buy explicitness instead:\n\n| Rule | Fixable | What it asks for |\n| -------------------------- | ------- | ----------------------------------------------------------------------------------------------------------- |\n| `no-color-as-surface` | yes | `textColor` instead of the `color` alias, where `color` cannot mean a surface |\n| `no-bulma-component-class` | — | The bestax component instead of a plain element styled with its Bulma class (`<button className=\"button\">`) |\n\nThe rules that judge bestax elements resolve them through the import and\nthrough scope, so neither your own `<Box>` nor a local shadowing the imported\none is linted against Bulma's rules, and each skips a value it cannot read as a\nliteral. `no-bulma-component-class` reads plain HTML elements instead, and\nleaves components, custom elements and anything inside `<svg>` alone.\n\nA spread is handled by what it can change: `no-color-as-surface` and\n`no-inert-flex-props` go silent, since a spread may carry the prop that makes\nthe code correct; `no-deprecated-props` reports but offers no fix; and\n`valid-helper-value` judges only the values that actually render, so a spread\nBEFORE the attribute leaves it reporting and a spread after it does not: JSX is\nlast-wins throughout, spreads included. `no-bulma-component-class` reports\neither way, since the class is written right there. No autofix changes what the\ncode renders.\n\n### valid-helper-value\n\n```jsx\n<Box textAlign=\"center\" /> // ✗ not a value textAlign accepts. Did you mean `centered`?\n<Box textAlign=\"centered\" /> // ✓\n```\n\nThe valid values come from the library's own exported tuples at lint time, by\nway of its `/constants` subpath, rather than from a list copied into this\npackage. The version those tuples come from is the copy of\n`@allxsmith/bestax-bulma` this plugin resolves, which in an ordinary deduped\ninstall is the same one your app uses; across a major bump it may not be, so\nkeep the two in step.\n\nOne family is outside its reach, knowingly: component-specific `color` props\nhave their own unions, so `<Button color=\"ghost\">` is correct and reporting it\nwould be worse than reporting nothing. And one element shadows a helper name:\n`Theme` takes a prop per Bulma CSS variable, so its `radius` sets\n`--bulma-radius` rather than emitting a class, while its type still says\notherwise. The rule skips that one pair until the library picks a meaning.\n\n### no-deprecated-props\n\n```jsx\n<Button isFullWidth /> // ✗ deprecated → fixes to isFullwidth\n<Tabs color=\"info\" /> // ✗ no .tabs.is-<color> CSS exists; no fix offered\n```\n\nThe table is generated from the library's own TSDoc, so a rename lands here\nwith the library rather than after someone notices.\n\n### no-color-as-surface\n\nOff by default. `color` on the content elements is a documented alias for\n`textColor`, so this rule is about saying which you meant, not about broken\ncode.\n\n```jsx\n<Box color=\"primary\" /> // ✗ ambiguous → fixes to textColor\n<Box bgColor=\"primary\" /> // ✓ a coloured surface\n<Button color=\"primary\" /> // ✓ Button really has an is-primary variant\n\n// ✓ silent: the background is explicit, so `color` is plainly the text half\n<Box bgColor=\"info\" color=\"primary\" />\n```\n\n### no-bulma-component-class\n\nOff by default. It reports a plain element styled with a Bulma class that\nbestax has a component for, and names the component. Turn it on in an app that\nhas moved onto bestax, with `bestax-migrate bulma-classes` or by hand, to keep\nraw Bulma markup from coming back.\n\n```jsx\n<button className=\"button is-primary\" /> // ✗ bestax renders .button as Button\n<div className=\"dropdown\" /> // ✗ bestax has Dropdown, converted by hand\n<div className=\"has-text-centered mt-4\" /> // ✓ helper classes are left alone\n<p className=\"help\" /> // ✓ bestax renders .help inside its form controls\n```\n\nIt reads the classes a `className` spells out, including the strings in a\nternary, a template, or a call to `clsx`, `classnames` or `tailwind-merge`\n(however the file imports or requires it), and reports one class per element:\nthe one the codemod would decide by. A function your app defines, a `cn` in `lib/utils`\nincluded, is one it cannot see into, so it reads nothing there. It only reports. Whether an element converts\ndepends on its tag, attributes and children, and `bestax-migrate bulma-classes`\nis the one place that judges that.\n\n### no-inert-flex-props\n\n```jsx\n<Box justifyContent=\"center\" /> // ✗ emits nothing\n<Box display=\"flex\" justifyContent=\"center\" /> // ✓\n<Box flexGrow=\"1\" /> // ✓ child props always apply\n```\n\n## Links\n\n- [Documentation](https://bestax.io/docs/guides/getting-started/eslint-plugin)\n- [Component library](https://www.npmjs.com/package/@allxsmith/bestax-bulma)\n- [Repository](https://github.com/allxsmith/bestax)\n\n## License\n\nMIT\n"
|
|
76
76
|
}
|