@gravionlabs/helix-lint 22.9.1 → 22.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +77 -4
- package/allow.mjs +36 -0
- package/eslint.mjs +21 -2
- package/package.json +3 -1
- package/rules/no-raw-control.mjs +67 -0
- package/stylelint.mjs +35 -2
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# @gravionlabs/helix-lint
|
|
2
2
|
|
|
3
|
-
The lint preset of Helix: a Stylelint config and an ESLint flat config that enforce the UX style guide.
|
|
4
|
-
[
|
|
3
|
+
The lint preset of Helix: a Stylelint config and an ESLint flat config that enforce the UX style guide. Full docs:
|
|
4
|
+
[Lint preset](https://github.com/GravionLabs/helix/blob/main/docs/LINT.md).
|
|
5
5
|
|
|
6
6
|
- `@gravionlabs/helix-lint/stylelint`: the Stylelint config.
|
|
7
7
|
- `@gravionlabs/helix-lint/eslint`: the ESLint flat config.
|
|
@@ -9,7 +9,80 @@ The lint preset of Helix: a Stylelint config and an ESLint flat config that enfo
|
|
|
9
9
|
`stylelint`, `eslint` and `@angular-eslint/template-parser` are peer dependencies.
|
|
10
10
|
|
|
11
11
|
```sh
|
|
12
|
-
pnpm add -D @gravionlabs/helix-lint
|
|
12
|
+
pnpm add -D @gravionlabs/helix-lint stylelint eslint @angular-eslint/template-parser
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
## Commands
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
stylelint "**/*.{css,scss}"
|
|
19
|
+
eslint "**/*.html"
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Stylelint
|
|
23
|
+
|
|
24
|
+
| Rule | Result |
|
|
25
|
+
| ----------------------------------------- | ----------------------------------------------------------- |
|
|
26
|
+
| `color-no-hex` | no hex colors |
|
|
27
|
+
| `function-disallowed-list` | no `rgb()`, `rgba()`, `hsl()` or `hsla()` |
|
|
28
|
+
| `declaration-property-value-allowed-list` | `font-size` only from the type scale, a keyword or a `calc` |
|
|
29
|
+
|
|
30
|
+
Allowed as `font-size`: `var(--h-font-size-*)`, `inherit`, `initial`, `unset`, `revert`, `smaller`, `larger`, and
|
|
31
|
+
`calc()` or `clamp()` over `--h-font-size-*` tokens and unitless numbers. Every other `var()`, component tokens
|
|
32
|
+
included, is reported.
|
|
33
|
+
|
|
34
|
+
The preset sets no `extends` and no formatting rules; add it next to your base config. `helixAllow` writes an
|
|
35
|
+
exception as an `overrides` entry. It throws without `files`, `rules` or a `reason`.
|
|
36
|
+
|
|
37
|
+
```js
|
|
38
|
+
// stylelint.config.mjs
|
|
39
|
+
import { helixAllow } from '@gravionlabs/helix-lint/stylelint';
|
|
40
|
+
|
|
41
|
+
export default {
|
|
42
|
+
extends: ['stylelint-config-standard-scss', '@gravionlabs/helix-lint/stylelint'],
|
|
43
|
+
overrides: [
|
|
44
|
+
helixAllow({
|
|
45
|
+
files: ['src/app/legacy/**'],
|
|
46
|
+
rules: ['declaration-property-value-allowed-list'],
|
|
47
|
+
reason: 'Off-scale sizes until the legacy screens move to the type scale',
|
|
48
|
+
}),
|
|
49
|
+
],
|
|
50
|
+
};
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## ESLint
|
|
54
|
+
|
|
55
|
+
The preset checks the templates (`**/*.html`) with `helix/no-raw-control`:
|
|
56
|
+
|
|
57
|
+
| Raw element | Result |
|
|
58
|
+
| ------------ | ---------------------------------------------------------- |
|
|
59
|
+
| `<button>` | needs `hx-button` or `hx-list-row` |
|
|
60
|
+
| `<input>` | needs `hx-input`, `hx-checkbox`, `hx-radio` or `hx-switch` |
|
|
61
|
+
| `<textarea>` | needs `hx-input` |
|
|
62
|
+
| `<select>` | always reported, use `hx-select` |
|
|
63
|
+
| `<progress>` | always reported, use `hx-progress-bar` |
|
|
64
|
+
|
|
65
|
+
`<input type="hidden">`, and `<input type="file">` inside `hx-file-upload`, are not reported.
|
|
66
|
+
|
|
67
|
+
`helixAllow` writes an exception. It throws without `files`, `rules` or a `reason`. Put its entries after the preset.
|
|
68
|
+
|
|
69
|
+
```js
|
|
70
|
+
// eslint.config.mjs
|
|
71
|
+
import helix, { helixAllow } from '@gravionlabs/helix-lint/eslint';
|
|
72
|
+
import { defineConfig } from 'eslint/config';
|
|
73
|
+
|
|
74
|
+
export default defineConfig(
|
|
75
|
+
helix,
|
|
76
|
+
helixAllow({
|
|
77
|
+
files: ['src/app/legacy/**'],
|
|
78
|
+
rules: ['helix/no-raw-control'],
|
|
79
|
+
reason: 'Native file input until the upload screen moves to hx-file-upload',
|
|
80
|
+
}),
|
|
81
|
+
);
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Adopting the preset in an app
|
|
85
|
+
|
|
86
|
+
Start with an allowlist phase (ADR 0008, Consequences): add the preset, write one `helixAllow` entry with its reason
|
|
87
|
+
for what you do not fix in the same change, let the lint run block in CI, then migrate the files and remove the
|
|
88
|
+
entries. A new or stricter rule of a later release is adopted the same way.
|
package/allow.mjs
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// The allowlist of the Helix lint preset (ADR 0009): every exception names its files, its rules and a reason.
|
|
2
|
+
|
|
3
|
+
const isNameList = (value) =>
|
|
4
|
+
Array.isArray(value) &&
|
|
5
|
+
value.length > 0 &&
|
|
6
|
+
value.every((entry) => typeof entry === 'string' && entry.trim() !== '');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Checks one allowlist entry, for the ESLint and the Stylelint `helixAllow`.
|
|
10
|
+
*
|
|
11
|
+
* @param {{ files: string[], rules: string[], reason: string }} entry
|
|
12
|
+
* @throws {Error} without a reason, files or rules.
|
|
13
|
+
*/
|
|
14
|
+
export function assertAllowEntry({ files, rules, reason }) {
|
|
15
|
+
if (typeof reason !== 'string' || reason.trim() === '') {
|
|
16
|
+
throw new Error('helixAllow: every allowlist entry needs a `reason`.');
|
|
17
|
+
}
|
|
18
|
+
if (!isNameList(files)) throw new Error(`helixAllow: \`files\` is missing (${reason}).`);
|
|
19
|
+
if (!isNameList(rules)) throw new Error(`helixAllow: \`rules\` is missing (${reason}).`);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* One allowlist entry as an ESLint flat config object that turns `rules` off for `files`. Put it after the preset.
|
|
24
|
+
*
|
|
25
|
+
* @param {{ files: string[], rules: string[], reason: string }} entry
|
|
26
|
+
* @returns {import('eslint').Linter.Config}
|
|
27
|
+
* @throws {Error} without a reason, files or rules.
|
|
28
|
+
*/
|
|
29
|
+
export function helixAllow({ files, rules, reason } = {}) {
|
|
30
|
+
assertAllowEntry({ files, rules, reason });
|
|
31
|
+
return {
|
|
32
|
+
name: `helix/allow: ${reason}`,
|
|
33
|
+
files,
|
|
34
|
+
rules: Object.fromEntries(rules.map((rule) => [rule, 'off'])),
|
|
35
|
+
};
|
|
36
|
+
}
|
package/eslint.mjs
CHANGED
|
@@ -1,3 +1,22 @@
|
|
|
1
|
-
// The ESLint flat config of the Helix lint preset (ADR 0009).
|
|
1
|
+
// The ESLint flat config of the Helix lint preset (ADR 0009).
|
|
2
|
+
import * as templateParser from '@angular-eslint/template-parser';
|
|
3
|
+
import noRawControl from './rules/no-raw-control.mjs';
|
|
4
|
+
|
|
5
|
+
export { helixAllow } from './allow.mjs';
|
|
6
|
+
|
|
7
|
+
/** The ESLint plugin with the `helix/*` template rules. */
|
|
8
|
+
export const plugin = {
|
|
9
|
+
meta: { name: '@gravionlabs/helix-lint' },
|
|
10
|
+
rules: { 'no-raw-control': noRawControl },
|
|
11
|
+
};
|
|
12
|
+
|
|
2
13
|
/** @type {import('eslint').Linter.Config[]} */
|
|
3
|
-
export default [
|
|
14
|
+
export default [
|
|
15
|
+
{
|
|
16
|
+
name: 'helix/templates',
|
|
17
|
+
files: ['**/*.html'],
|
|
18
|
+
languageOptions: { parser: templateParser },
|
|
19
|
+
plugins: { helix: plugin },
|
|
20
|
+
rules: { 'helix/no-raw-control': 'error' },
|
|
21
|
+
},
|
|
22
|
+
];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gravionlabs/helix-lint",
|
|
3
|
-
"version": "22.
|
|
3
|
+
"version": "22.10.0",
|
|
4
4
|
"description": "Helix lint preset: Stylelint config and ESLint flat config with the helix template rules.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -32,6 +32,8 @@
|
|
|
32
32
|
"files": [
|
|
33
33
|
"stylelint.mjs",
|
|
34
34
|
"eslint.mjs",
|
|
35
|
+
"allow.mjs",
|
|
36
|
+
"rules",
|
|
35
37
|
"README.md",
|
|
36
38
|
"LICENSE.md"
|
|
37
39
|
]
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// helix/no-raw-control (ADR 0008, ADR 0009): a raw control in an Angular template needs a Helix directive, or has to
|
|
2
|
+
// be replaced by a Helix component. Runs on the AST of @angular-eslint/template-parser.
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The Helix carriers per raw element: `directives` (one of them on the element makes it pass) or `component` (the raw
|
|
6
|
+
* element is always reported). scripts/tests/lint-preset-template.spec.ts checks them against the helix-ui selectors.
|
|
7
|
+
*/
|
|
8
|
+
export const HELIX_CONTROLS = Object.freeze({
|
|
9
|
+
button: { directives: ['hx-button', 'hx-list-row'] },
|
|
10
|
+
input: { directives: ['hx-input', 'hx-checkbox', 'hx-radio', 'hx-switch'] },
|
|
11
|
+
textarea: { directives: ['hx-input'] },
|
|
12
|
+
select: { component: 'hx-select' },
|
|
13
|
+
progress: { component: 'hx-progress-bar' },
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
/** The component whose own `<input type="file">` is not reported. */
|
|
17
|
+
export const FILE_UPLOAD = 'hx-file-upload';
|
|
18
|
+
|
|
19
|
+
const list = (names) => names.map((name) => `\`${name}\``).join(' or ');
|
|
20
|
+
|
|
21
|
+
/** The value of the static attribute `name`, lower case; `undefined` when the element has none. */
|
|
22
|
+
const staticValue = (node, name) =>
|
|
23
|
+
node.attributes.find((attribute) => attribute.name === name)?.value.toLowerCase();
|
|
24
|
+
|
|
25
|
+
/** Angular matches an attribute selector against static attributes and property bindings. */
|
|
26
|
+
const hasDirective = (node, directives) =>
|
|
27
|
+
[...node.attributes, ...node.inputs].some(({ name }) => directives.includes(name));
|
|
28
|
+
|
|
29
|
+
/** @type {import('eslint').Rule.RuleModule} */
|
|
30
|
+
export default {
|
|
31
|
+
meta: {
|
|
32
|
+
type: 'problem',
|
|
33
|
+
docs: { description: 'Disallow raw controls without a Helix directive in Angular templates' },
|
|
34
|
+
schema: [],
|
|
35
|
+
messages: {
|
|
36
|
+
directive: 'Raw `<{{element}}>`: add the Helix directive {{directives}}.',
|
|
37
|
+
component: 'Raw `<{{element}}>`: use `<{{component}}>` instead.',
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
create(context) {
|
|
41
|
+
const insideFileUpload = (node) =>
|
|
42
|
+
context.sourceCode.getAncestors(node).some((ancestor) => ancestor.name === FILE_UPLOAD);
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
Element(node) {
|
|
46
|
+
const element = node.name;
|
|
47
|
+
if (!Object.hasOwn(HELIX_CONTROLS, element)) return;
|
|
48
|
+
const { directives, component } = HELIX_CONTROLS[element];
|
|
49
|
+
const loc = context.sourceCode.parserServices.convertElementSourceSpanToLoc(context, node);
|
|
50
|
+
if (component) {
|
|
51
|
+
context.report({ loc, messageId: 'component', data: { element, component } });
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
if (element === 'input') {
|
|
55
|
+
const type = staticValue(node, 'type');
|
|
56
|
+
if (type === 'hidden' || (type === 'file' && insideFileUpload(node))) return;
|
|
57
|
+
}
|
|
58
|
+
if (hasDirective(node, directives)) return;
|
|
59
|
+
context.report({
|
|
60
|
+
loc,
|
|
61
|
+
messageId: 'directive',
|
|
62
|
+
data: { element, directives: list(directives) },
|
|
63
|
+
});
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
},
|
|
67
|
+
};
|
package/stylelint.mjs
CHANGED
|
@@ -1,3 +1,36 @@
|
|
|
1
|
-
// The Stylelint config of the Helix lint preset (ADR 0009)
|
|
1
|
+
// The Stylelint config of the Helix lint preset (ADR 0008, ADR 0009): colors and font sizes come from the tokens.
|
|
2
|
+
// No `extends` and no formatting rules: an app adds the preset next to its own base config.
|
|
3
|
+
import { assertAllowEntry } from './allow.mjs';
|
|
4
|
+
|
|
5
|
+
/** A type scale token, without a fallback value. */
|
|
6
|
+
const TOKEN = String.raw`var\(\s*--h-font-size-[a-z0-9]+\s*\)`;
|
|
7
|
+
|
|
8
|
+
const FONT_SIZES = [
|
|
9
|
+
new RegExp(`^${TOKEN}$`),
|
|
10
|
+
/^(inherit|initial|unset|revert|smaller|larger)$/,
|
|
11
|
+
// calc() and clamp() over at least one token: numbers and operators, no literal with a unit and no other var()
|
|
12
|
+
new RegExp(
|
|
13
|
+
String.raw`^(?=[\s\S]*var\()(?:calc|clamp)\((?:${TOKEN}|(?:calc|clamp)\(|[\d.\s+*/(),-])*\)$`,
|
|
14
|
+
),
|
|
15
|
+
];
|
|
16
|
+
|
|
2
17
|
/** @type {import('stylelint').Config} */
|
|
3
|
-
export default {
|
|
18
|
+
export default {
|
|
19
|
+
rules: {
|
|
20
|
+
'color-no-hex': true,
|
|
21
|
+
'function-disallowed-list': ['rgb', 'rgba', 'hsl', 'hsla'],
|
|
22
|
+
'declaration-property-value-allowed-list': { 'font-size': FONT_SIZES },
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* One allowlist entry as a Stylelint `overrides` entry that turns `rules` off for `files`.
|
|
28
|
+
*
|
|
29
|
+
* @param {{ files: string[], rules: string[], reason: string }} entry
|
|
30
|
+
* @returns {{ files: string[], rules: Record<string, null> }}
|
|
31
|
+
* @throws {Error} without a reason, files or rules.
|
|
32
|
+
*/
|
|
33
|
+
export function helixAllow({ files, rules, reason } = {}) {
|
|
34
|
+
assertAllowEntry({ files, rules, reason });
|
|
35
|
+
return { files, rules: Object.fromEntries(rules.map((rule) => [rule, null])) };
|
|
36
|
+
}
|