@krizaka/config 0.0.0-stage → 0.1.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/.editorconfig +17 -0
- package/LICENSE +202 -0
- package/README.md +118 -2
- package/bin/krizaka-ratchet.mjs +83 -0
- package/eslint/index.js +70 -0
- package/eslint/krizaka-ui.js +61 -0
- package/eslint/next.js +18 -0
- package/eslint/no-locale-ternary.js +68 -0
- package/package.json +110 -3
- package/patterns.js +33 -0
- package/prettier.config.js +15 -0
- package/ratchet/ratchet.js +159 -0
- package/tsconfig/base.json +18 -0
- package/tsconfig/next.json +14 -0
- package/tsconfig/react-library.json +9 -0
- package/tsconfig/react-native.json +12 -0
package/.editorconfig
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Krizaka EditorConfig — copy to the repository root: cp node_modules/@krizaka/config/.editorconfig .
|
|
2
|
+
root = true
|
|
3
|
+
|
|
4
|
+
[*]
|
|
5
|
+
charset = utf-8
|
|
6
|
+
end_of_line = lf
|
|
7
|
+
indent_style = space
|
|
8
|
+
indent_size = 2
|
|
9
|
+
insert_final_newline = true
|
|
10
|
+
trim_trailing_whitespace = true
|
|
11
|
+
max_line_length = 120
|
|
12
|
+
|
|
13
|
+
[*.md]
|
|
14
|
+
trim_trailing_whitespace = false
|
|
15
|
+
|
|
16
|
+
[{Makefile,*.mk}]
|
|
17
|
+
indent_style = tab
|
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,119 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @krizaka/config
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**The shared configuration of every Krizaka JavaScript repository.** ESLint flat configs (base, Next.js, the four UI
|
|
4
|
+
rules, no locale ternary), tsconfig bases, Prettier, EditorConfig — and `krizaka-ratchet`, the counter that keeps the
|
|
5
|
+
UI debt from coming back.
|
|
6
|
+
|
|
7
|
+
[](https://www.npmjs.com/package/@krizaka/config)
|
|
8
|
+
[](../../LICENSE)
|
|
9
|
+
|
|
10
|
+
## Adopt it in three lines
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install -D @krizaka/config eslint typescript # + eslint-config-next for a Next.js app, prettier if you format
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
```js
|
|
17
|
+
// eslint.config.mjs — a Next.js app (a library: `krizakaBase` from "@krizaka/config/eslint")
|
|
18
|
+
import { krizakaNext } from "@krizaka/config/eslint/next";
|
|
19
|
+
import { krizakaUi } from "@krizaka/config/eslint/krizaka-ui";
|
|
20
|
+
import { noLocaleTernary } from "@krizaka/config/eslint/no-locale-ternary";
|
|
21
|
+
|
|
22
|
+
export default [...krizakaNext, ...krizakaUi(), ...noLocaleTernary()];
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
```jsonc
|
|
26
|
+
// tsconfig.json — base · react-library · next · react-native
|
|
27
|
+
{ "extends": "@krizaka/config/tsconfig/next.json", "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"] }
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Prettier: `export { default } from "@krizaka/config/prettier";` in `prettier.config.js` (120 columns, double quotes,
|
|
31
|
+
trailing commas). EditorConfig: `cp node_modules/@krizaka/config/.editorconfig .`.
|
|
32
|
+
|
|
33
|
+
`eslint` ^9 and `typescript` ^5 are peer dependencies; `eslint-config-next` (Next.js apps) and `prettier` are optional
|
|
34
|
+
peers. The plugins (`typescript-eslint`, `eslint-plugin-react-hooks`, `eslint-plugin-simple-import-sort`) come with
|
|
35
|
+
the package.
|
|
36
|
+
|
|
37
|
+
## What is inside
|
|
38
|
+
|
|
39
|
+
| Export | What it gives |
|
|
40
|
+
| :-- | :-- |
|
|
41
|
+
| `@krizaka/config/eslint` | `krizakaBase`: `@eslint/js` + `typescript-eslint` recommended, React hooks, import ordering (a warning, autofixable), ignores of generated output, `_`-prefixed unused arguments allowed. |
|
|
42
|
+
| `@krizaka/config/eslint/next` | `krizakaNext`: `eslint-config-next` (core web vitals + TypeScript, which already carry typescript-eslint, React hooks and import) + the same basics. |
|
|
43
|
+
| `@krizaka/config/eslint/krizaka-ui` | `krizakaUi(options?)`: the four UI rules below. Also `krizakaUiRules` and `krizakaUiRestrictedSyntax(options?)`. |
|
|
44
|
+
| `@krizaka/config/eslint/no-locale-ternary` | `noLocaleTernary(options?)`: `krizaka/no-locale-ternary` — no `locale === "fr" ? … : …` outside routing code. |
|
|
45
|
+
| `@krizaka/config/tsconfig/*.json` | `base` (strict, ES2022, bundler resolution), `react-library` (+ DOM, `react-jsx`), `next` (+ the Next plugin, `noEmit`, `incremental`), `react-native` (`jsx: react-native`, `customConditions`; with Expo: `"extends": ["expo/tsconfig.base", "@krizaka/config/tsconfig/react-native.json"]`). |
|
|
46
|
+
| `@krizaka/config/prettier` · `.editorconfig` | The formatting. |
|
|
47
|
+
| `krizaka-ratchet` (bin) | The debt counter, see below. |
|
|
48
|
+
|
|
49
|
+
### The four UI rules
|
|
50
|
+
|
|
51
|
+
On every string literal inside a `className` (in `cn(…)`, a ternary…), with no plugin — `no-restricted-syntax`
|
|
52
|
+
selectors:
|
|
53
|
+
|
|
54
|
+
1. **No raw palette colour** — `bg-zinc-900`, `text-violet-400/80`: use a role of
|
|
55
|
+
[`@krizaka/tailwind`](../tailwind) (`bg-surface-1`, `text-fg-secondary`, `border-border-default`, `text-accent`,
|
|
56
|
+
`text-danger`).
|
|
57
|
+
2. **No `light:`** — a theme is a set of tokens; a dark island takes `.theme-dark` on its ancestor.
|
|
58
|
+
3. **No arbitrary `[var(--…)]` utility** — the variable has a utility in `@krizaka/tailwind`.
|
|
59
|
+
4. **No template literal in `className`** — `cn(…)` merges the classes and lets the override win.
|
|
60
|
+
|
|
61
|
+
```js
|
|
62
|
+
krizakaUi({
|
|
63
|
+
allow: ["emerald"], // palette families tolerated for documented, isolated cases
|
|
64
|
+
files: ["**/*.{jsx,tsx}"], // default
|
|
65
|
+
ignores: ["app/og/**"], // e.g. generated OG images
|
|
66
|
+
severity: "error", // "warn" while the ratchet brings the debt down
|
|
67
|
+
});
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Flat config replaces a rule's options instead of concatenating them: a repository that already sets
|
|
71
|
+
`no-restricted-syntax` merges both lists with `krizakaUiRestrictedSyntax()`. `no-locale-ternary` is a plugin rule for
|
|
72
|
+
that reason — it composes with anything.
|
|
73
|
+
|
|
74
|
+
`noLocaleTernary({ files, routing })`: `routing` lists the files allowed to compare locales (default: `proxy.ts`,
|
|
75
|
+
`middleware.ts`, `i18n.ts`, `seo.ts`, `app/[locale]/layout.tsx`, `I18nProvider.tsx`).
|
|
76
|
+
|
|
77
|
+
## The ratchet
|
|
78
|
+
|
|
79
|
+
A repository with debt cannot turn the UI rules on in one pull request. The ratchet lets it adopt them now: it counts
|
|
80
|
+
the four debts, records the counts in `lint-ratchet.json` at the root, and **fails when a count goes up**. Every
|
|
81
|
+
migration pull request lowers them; when a count reaches zero, the matching rule becomes a strict `error`.
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
npx krizaka-ratchet --init # once: creates lint-ratchet.json with today's counts — commit it
|
|
85
|
+
npx krizaka-ratchet # locally: compares (exit 1 when a count went up)
|
|
86
|
+
npx krizaka-ratchet --update # after a migration: lowers the recorded counts (never raises them) — commit it
|
|
87
|
+
npx krizaka-ratchet --json # in CI: the same check, machine-readable
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
| Counter | Pattern (same regex as the ESLint rule) |
|
|
91
|
+
| :-- | :-- |
|
|
92
|
+
| `palette` | `\b(bg\|text\|border\|from\|via\|to\|ring\|shadow\|fill\|stroke)-(zinc\|slate\|…\|indigo)-[0-9]` |
|
|
93
|
+
| `light` | `\blight:` |
|
|
94
|
+
| `arbitraryVar` | `\[var\(--` |
|
|
95
|
+
| `classNameTemplate` | `` className=\{\s*` `` |
|
|
96
|
+
|
|
97
|
+
It scans `app/**/*.tsx`, `components/**/*.tsx` and `src/**/*.tsx` by default (`node_modules`, `dist`, `.next` never),
|
|
98
|
+
or the globs given on the command line; `--init` stores them in the file so that every later run counts the same set.
|
|
99
|
+
The counts apply the regex to the whole source text — a class held in a constant or a `cn()` argument counts too —
|
|
100
|
+
so the ratchet is never looser than the rule. Exit codes: `0` ok, `1` a count went up, `2` usage or file error.
|
|
101
|
+
|
|
102
|
+
```jsonc
|
|
103
|
+
// lint-ratchet.json
|
|
104
|
+
{
|
|
105
|
+
"globs": ["app/**/*.tsx", "components/**/*.tsx", "src/**/*.tsx"],
|
|
106
|
+
"allow": [], // optional: same families as krizakaUi({ allow })
|
|
107
|
+
"counters": { "palette": 2400, "light": 1151, "arbitraryVar": 0, "classNameTemplate": 128 }
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
The CI step, after the install:
|
|
112
|
+
|
|
113
|
+
```yaml
|
|
114
|
+
- run: npx krizaka-ratchet --json
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## License
|
|
118
|
+
|
|
119
|
+
[Apache-2.0](../../LICENSE)
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// krizaka-ratchet — the UI debt can only go down. See the README of @krizaka/config.
|
|
3
|
+
import { parseArgs } from "node:util";
|
|
4
|
+
|
|
5
|
+
import { COUNTERS, RatchetError, runRatchet } from "../ratchet/ratchet.js";
|
|
6
|
+
|
|
7
|
+
const USAGE = `Usage: krizaka-ratchet [globs…] [--init | --update] [--json] [--root <dir>] [--file <name>]
|
|
8
|
+
|
|
9
|
+
Counts raw palette colours, light: variants, [var(--…)] utilities and className template strings in the
|
|
10
|
+
source files (default: app/**/*.tsx components/**/*.tsx src/**/*.tsx) and compares them with lint-ratchet.json.
|
|
11
|
+
|
|
12
|
+
(no flag) fail (exit 1) when a counter is above its recorded value
|
|
13
|
+
--init create lint-ratchet.json with the current counters
|
|
14
|
+
--update lower the recorded counters to the current values (never raises them)
|
|
15
|
+
--json machine-readable report on stdout (for CI)
|
|
16
|
+
--root repository root (default: current directory)
|
|
17
|
+
--file ratchet file name (default: lint-ratchet.json)`;
|
|
18
|
+
|
|
19
|
+
const LABELS = {
|
|
20
|
+
palette: "raw palette colours",
|
|
21
|
+
light: "light: variants",
|
|
22
|
+
arbitraryVar: "[var(--…)] utilities",
|
|
23
|
+
classNameTemplate: "className template strings",
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
let args;
|
|
27
|
+
try {
|
|
28
|
+
args = parseArgs({
|
|
29
|
+
allowPositionals: true,
|
|
30
|
+
options: {
|
|
31
|
+
init: { type: "boolean" },
|
|
32
|
+
update: { type: "boolean" },
|
|
33
|
+
json: { type: "boolean" },
|
|
34
|
+
root: { type: "string" },
|
|
35
|
+
file: { type: "string" },
|
|
36
|
+
help: { type: "boolean", short: "h" },
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
} catch (error) {
|
|
40
|
+
console.error(`${error instanceof Error ? error.message : error}\n\n${USAGE}`);
|
|
41
|
+
process.exit(2);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const { values, positionals } = args;
|
|
45
|
+
if (values.help) {
|
|
46
|
+
console.log(USAGE);
|
|
47
|
+
process.exit(0);
|
|
48
|
+
}
|
|
49
|
+
if (values.init && values.update) {
|
|
50
|
+
console.error("--init and --update are exclusive.");
|
|
51
|
+
process.exit(2);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
try {
|
|
55
|
+
const result = await runRatchet({
|
|
56
|
+
root: values.root ?? process.cwd(),
|
|
57
|
+
mode: values.init ? "init" : values.update ? "update" : "check",
|
|
58
|
+
globs: positionals,
|
|
59
|
+
file: values.file,
|
|
60
|
+
});
|
|
61
|
+
if (values.json) {
|
|
62
|
+
console.log(JSON.stringify(result, null, 2));
|
|
63
|
+
} else {
|
|
64
|
+
console.log(`krizaka-ratchet — ${result.files} files (${result.globs.join(", ")})`);
|
|
65
|
+
for (const name of COUNTERS) {
|
|
66
|
+
const { recorded, current, delta } = result.counters[name];
|
|
67
|
+
const mark = delta > 0 ? "✗" : delta < 0 ? "↓" : "=";
|
|
68
|
+
const sign = delta > 0 ? `+${delta}` : `${delta}`;
|
|
69
|
+
console.log(` ${mark} ${LABELS[name].padEnd(28)} ${String(current).padStart(6)} (recorded ${recorded}, ${sign})`);
|
|
70
|
+
}
|
|
71
|
+
if (result.mode === "init") console.log(`Created ${result.file}.`);
|
|
72
|
+
else if (result.written) console.log(`Lowered ${result.file}: commit it.`);
|
|
73
|
+
else if (result.decreased.length > 0) console.log(`Debt went down: run krizaka-ratchet --update and commit ${result.file}.`);
|
|
74
|
+
if (!result.ok) console.error(`Debt went up (${result.increased.join(", ")}): use the roles of @krizaka/tailwind and cn().`);
|
|
75
|
+
}
|
|
76
|
+
process.exit(result.ok ? 0 : 1);
|
|
77
|
+
} catch (error) {
|
|
78
|
+
if (error instanceof RatchetError || error instanceof SyntaxError) {
|
|
79
|
+
console.error(`krizaka-ratchet: ${error.message}`);
|
|
80
|
+
process.exit(2);
|
|
81
|
+
}
|
|
82
|
+
throw error;
|
|
83
|
+
}
|
package/eslint/index.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// The base flat config of every Krizaka JavaScript repository: @eslint/js and typescript-eslint recommended,
|
|
2
|
+
// the React hooks rules, and a light import ordering (autofixable, a warning).
|
|
3
|
+
import js from "@eslint/js";
|
|
4
|
+
import reactHooks from "eslint-plugin-react-hooks";
|
|
5
|
+
import simpleImportSort from "eslint-plugin-simple-import-sort";
|
|
6
|
+
import globals from "globals";
|
|
7
|
+
import tseslint from "typescript-eslint";
|
|
8
|
+
|
|
9
|
+
/** Generated or vendored output, never linted. */
|
|
10
|
+
export const KRIZAKA_IGNORES = Object.freeze([
|
|
11
|
+
"**/node_modules/**",
|
|
12
|
+
"**/dist/**",
|
|
13
|
+
"**/build/**",
|
|
14
|
+
"**/coverage/**",
|
|
15
|
+
"**/.next/**",
|
|
16
|
+
"**/out/**",
|
|
17
|
+
"**/.turbo/**",
|
|
18
|
+
"**/.expo/**",
|
|
19
|
+
"**/storybook-static/**",
|
|
20
|
+
"**/next-env.d.ts",
|
|
21
|
+
]);
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Shared by krizakaBase and krizakaNext: ignores, globals, import ordering.
|
|
25
|
+
* @type {import("eslint").Linter.Config[]}
|
|
26
|
+
*/
|
|
27
|
+
export const krizakaCommon = [
|
|
28
|
+
{ name: "krizaka/ignores", ignores: [...KRIZAKA_IGNORES] },
|
|
29
|
+
{
|
|
30
|
+
name: "krizaka/language",
|
|
31
|
+
languageOptions: {
|
|
32
|
+
ecmaVersion: "latest",
|
|
33
|
+
sourceType: "module",
|
|
34
|
+
globals: { ...globals.browser, ...globals.node },
|
|
35
|
+
},
|
|
36
|
+
linterOptions: { reportUnusedDisableDirectives: "error" },
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
name: "krizaka/imports",
|
|
40
|
+
plugins: { "simple-import-sort": simpleImportSort },
|
|
41
|
+
rules: {
|
|
42
|
+
"simple-import-sort/imports": "warn",
|
|
43
|
+
"simple-import-sort/exports": "warn",
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
];
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Unused variables are errors, except the `_`-prefixed ones (an intentionally ignored argument).
|
|
50
|
+
* @type {import("eslint").Linter.RuleEntry}
|
|
51
|
+
*/
|
|
52
|
+
const UNUSED_VARS = [
|
|
53
|
+
"error",
|
|
54
|
+
{ argsIgnorePattern: "^_", varsIgnorePattern: "^_", caughtErrorsIgnorePattern: "^_", ignoreRestSiblings: true },
|
|
55
|
+
];
|
|
56
|
+
|
|
57
|
+
/** @type {import("eslint").Linter.Config[]} */
|
|
58
|
+
export const krizakaBase = [
|
|
59
|
+
...krizakaCommon,
|
|
60
|
+
{ name: "krizaka/js", ...js.configs.recommended },
|
|
61
|
+
.../** @type {import("eslint").Linter.Config[]} */ (tseslint.configs.recommended),
|
|
62
|
+
{ name: "krizaka/react-hooks", files: ["**/*.{js,jsx,ts,tsx}"], ...reactHooks.configs.flat.recommended },
|
|
63
|
+
{
|
|
64
|
+
name: "krizaka/typescript",
|
|
65
|
+
files: ["**/*.{ts,tsx,mts,cts}"],
|
|
66
|
+
rules: { "@typescript-eslint/no-unused-vars": UNUSED_VARS },
|
|
67
|
+
},
|
|
68
|
+
];
|
|
69
|
+
|
|
70
|
+
export default krizakaBase;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// The four non-negotiable UI rules of the refactor (study §2.12), with no plugin: AST selectors on className literals.
|
|
2
|
+
// The ratchet (bin/krizaka-ratchet.mjs) counts the same patterns, so a repository can adopt the rules before it is clean.
|
|
3
|
+
import { ARBITRARY_VAR_SOURCE, LIGHT_VARIANT_SOURCE, paletteSource } from "../patterns.js";
|
|
4
|
+
|
|
5
|
+
const CLASSNAME = "JSXAttribute[name.name='className']";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The `no-restricted-syntax` entries, for a repository that already uses that rule and must merge both lists
|
|
9
|
+
* (flat config replaces a rule's options, it never concatenates them).
|
|
10
|
+
* @param {{ allow?: string[] }} [options] `allow`: palette families tolerated for documented, isolated cases.
|
|
11
|
+
* @returns {{ selector: string, message: string }[]}
|
|
12
|
+
*/
|
|
13
|
+
export function krizakaUiRestrictedSyntax({ allow = [] } = {}) {
|
|
14
|
+
return [
|
|
15
|
+
{
|
|
16
|
+
selector: `${CLASSNAME} Literal[value=/${paletteSource(allow)}/]`,
|
|
17
|
+
message:
|
|
18
|
+
"Raw palette colour: use a role (bg-surface-1, text-fg-secondary, border-border-default, text-accent, text-danger).",
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
selector: `${CLASSNAME} Literal[value=/${LIGHT_VARIANT_SOURCE}/]`,
|
|
22
|
+
message: "`light:` is forbidden in products: a theme is a set of tokens. A dark island? `.theme-dark` on the ancestor.",
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
selector: `${CLASSNAME} Literal[value=/${ARBITRARY_VAR_SOURCE}/]`,
|
|
26
|
+
message: "Arbitrary utility: the variable has a utility in @krizaka/tailwind.",
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
selector: `${CLASSNAME} TemplateLiteral`,
|
|
30
|
+
message: "No template string in className: cn(...) merges classes and lets the override win.",
|
|
31
|
+
},
|
|
32
|
+
];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The rule set, for a spread into a config object's `rules`. */
|
|
36
|
+
export const krizakaUiRules = {
|
|
37
|
+
"no-restricted-syntax": ["error", ...krizakaUiRestrictedSyntax()],
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Flat config: the four UI rules on the given files.
|
|
42
|
+
* @param {{ allow?: string[], files?: string[], ignores?: string[], severity?: "error" | "warn" }} [options]
|
|
43
|
+
* @returns {import("eslint").Linter.Config[]}
|
|
44
|
+
*/
|
|
45
|
+
export function krizakaUi({
|
|
46
|
+
allow = [],
|
|
47
|
+
files = ["**/*.{jsx,tsx}"],
|
|
48
|
+
ignores = [],
|
|
49
|
+
severity = "error",
|
|
50
|
+
} = {}) {
|
|
51
|
+
return [
|
|
52
|
+
{
|
|
53
|
+
name: "krizaka/ui",
|
|
54
|
+
files,
|
|
55
|
+
...(ignores.length > 0 ? { ignores } : {}),
|
|
56
|
+
rules: { "no-restricted-syntax": [severity, ...krizakaUiRestrictedSyntax({ allow })] },
|
|
57
|
+
},
|
|
58
|
+
];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export default krizakaUi;
|
package/eslint/next.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// The flat config of a Next.js app: eslint-config-next (core web vitals + TypeScript, which already carry
|
|
2
|
+
// typescript-eslint, React hooks and import), on top of the shared Krizaka basics. eslint-config-next is an
|
|
3
|
+
// optional peer dependency: only Next.js apps install it.
|
|
4
|
+
import js from "@eslint/js";
|
|
5
|
+
import nextVitals from "eslint-config-next/core-web-vitals";
|
|
6
|
+
import nextTs from "eslint-config-next/typescript";
|
|
7
|
+
|
|
8
|
+
import { krizakaCommon } from "./index.js";
|
|
9
|
+
|
|
10
|
+
/** @type {import("eslint").Linter.Config[]} */
|
|
11
|
+
export const krizakaNext = [
|
|
12
|
+
...krizakaCommon,
|
|
13
|
+
{ name: "krizaka/js", ...js.configs.recommended },
|
|
14
|
+
...nextVitals,
|
|
15
|
+
...nextTs,
|
|
16
|
+
];
|
|
17
|
+
|
|
18
|
+
export default krizakaNext;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// A component never chooses its text with `locale === "fr" ? … : …`: every user-facing string lives in the
|
|
2
|
+
// message catalogues. Only routing code compares locales. Same selectors as the rule in force on krizaka.com,
|
|
3
|
+
// shipped as a plugin rule (`krizaka/no-locale-ternary`) so it never collides with a repository's own
|
|
4
|
+
// `no-restricted-syntax` options.
|
|
5
|
+
|
|
6
|
+
const MESSAGE = "Put the text in the message catalogues (messages/<locale>.json) and read it from the dictionary (t.…).";
|
|
7
|
+
|
|
8
|
+
/** The selectors, also usable in `no-restricted-syntax`. */
|
|
9
|
+
export const LOCALE_TERNARY_SELECTORS = Object.freeze([
|
|
10
|
+
"ConditionalExpression[test.type='BinaryExpression'][test.right.value=/^(fr|en)$/]",
|
|
11
|
+
"ConditionalExpression > Identifier.test[name=/^(isFr|isEn|fr)$/]",
|
|
12
|
+
]);
|
|
13
|
+
|
|
14
|
+
/** @type {import("eslint").Rule.RuleModule} */
|
|
15
|
+
const rule = {
|
|
16
|
+
meta: {
|
|
17
|
+
type: "problem",
|
|
18
|
+
docs: { description: "Forbid choosing a text with a locale ternary outside routing code." },
|
|
19
|
+
schema: [],
|
|
20
|
+
messages: { localeTernary: MESSAGE },
|
|
21
|
+
},
|
|
22
|
+
create(context) {
|
|
23
|
+
/** @type {Record<string, (node: import("estree").Node) => void>} */
|
|
24
|
+
const visitors = {};
|
|
25
|
+
for (const selector of LOCALE_TERNARY_SELECTORS) {
|
|
26
|
+
visitors[selector] = (node) => context.report({ node, messageId: "localeTernary" });
|
|
27
|
+
}
|
|
28
|
+
return visitors;
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/** The plugin carrying the rule. */
|
|
33
|
+
export const krizakaPlugin = {
|
|
34
|
+
meta: { name: "@krizaka/config" },
|
|
35
|
+
rules: { "no-locale-ternary": rule },
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/** Routing files that legitimately compare locales (the krizaka.com layout, a common convention). */
|
|
39
|
+
export const DEFAULT_ROUTING_FILES = Object.freeze([
|
|
40
|
+
"**/proxy.ts",
|
|
41
|
+
"**/middleware.ts",
|
|
42
|
+
"**/i18n.ts",
|
|
43
|
+
"**/seo.ts",
|
|
44
|
+
"**/app/[[]locale[]]/layout.tsx",
|
|
45
|
+
"**/I18nProvider.tsx",
|
|
46
|
+
]);
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Flat config: `krizaka/no-locale-ternary` on the given files, except routing code.
|
|
50
|
+
* @param {{ files?: string[], routing?: string[] }} [options] `routing`: files allowed to compare locales.
|
|
51
|
+
* @returns {import("eslint").Linter.Config[]}
|
|
52
|
+
*/
|
|
53
|
+
export function noLocaleTernary({
|
|
54
|
+
files = ["**/*.{js,jsx,ts,tsx}"],
|
|
55
|
+
routing = [...DEFAULT_ROUTING_FILES],
|
|
56
|
+
} = {}) {
|
|
57
|
+
return [
|
|
58
|
+
{
|
|
59
|
+
name: "krizaka/no-locale-ternary",
|
|
60
|
+
files,
|
|
61
|
+
ignores: routing,
|
|
62
|
+
plugins: { krizaka: krizakaPlugin },
|
|
63
|
+
rules: { "krizaka/no-locale-ternary": "error" },
|
|
64
|
+
},
|
|
65
|
+
];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export default noLocaleTernary;
|
package/package.json
CHANGED
|
@@ -1,6 +1,113 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@krizaka/config",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "The shared configuration of every Krizaka JavaScript repository: ESLint flat configs (base, Next.js, the four UI rules, no locale ternary), tsconfig bases, Prettier, EditorConfig, and the krizaka-ratchet debt counter.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"krizaka",
|
|
7
|
+
"eslint-config",
|
|
8
|
+
"tsconfig",
|
|
9
|
+
"prettier-config",
|
|
10
|
+
"ratchet",
|
|
11
|
+
"design-system"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://github.com/krizaka/krizaka-ui/tree/main/packages/config#readme",
|
|
14
|
+
"bugs": "https://github.com/krizaka/krizaka-ui/issues",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/krizaka/krizaka-ui.git",
|
|
18
|
+
"directory": "packages/config"
|
|
19
|
+
},
|
|
20
|
+
"license": "Apache-2.0",
|
|
21
|
+
"author": "Krizaka (https://www.krizaka.com)",
|
|
22
|
+
"type": "module",
|
|
23
|
+
"exports": {
|
|
24
|
+
"./eslint": "./eslint/index.js",
|
|
25
|
+
"./eslint/next": "./eslint/next.js",
|
|
26
|
+
"./eslint/krizaka-ui": "./eslint/krizaka-ui.js",
|
|
27
|
+
"./eslint/no-locale-ternary": "./eslint/no-locale-ternary.js",
|
|
28
|
+
"./patterns": "./patterns.js",
|
|
29
|
+
"./ratchet": "./ratchet/ratchet.js",
|
|
30
|
+
"./prettier": "./prettier.config.js",
|
|
31
|
+
"./tsconfig/base.json": "./tsconfig/base.json",
|
|
32
|
+
"./tsconfig/react-library.json": "./tsconfig/react-library.json",
|
|
33
|
+
"./tsconfig/next.json": "./tsconfig/next.json",
|
|
34
|
+
"./tsconfig/react-native.json": "./tsconfig/react-native.json",
|
|
35
|
+
"./editorconfig": "./.editorconfig",
|
|
36
|
+
"./package.json": "./package.json"
|
|
37
|
+
},
|
|
38
|
+
"bin": {
|
|
39
|
+
"krizaka-ratchet": "./bin/krizaka-ratchet.mjs"
|
|
40
|
+
},
|
|
41
|
+
"files": [
|
|
42
|
+
"bin",
|
|
43
|
+
"eslint",
|
|
44
|
+
"ratchet",
|
|
45
|
+
"tsconfig",
|
|
46
|
+
"patterns.js",
|
|
47
|
+
"prettier.config.js",
|
|
48
|
+
".editorconfig",
|
|
49
|
+
"README.md",
|
|
50
|
+
"LICENSE"
|
|
51
|
+
],
|
|
52
|
+
"sideEffects": false,
|
|
53
|
+
"dependencies": {
|
|
54
|
+
"@eslint/js": "^9.39.0",
|
|
55
|
+
"eslint-plugin-react-hooks": "^7.1.1",
|
|
56
|
+
"eslint-plugin-simple-import-sort": "^14.0.0",
|
|
57
|
+
"globals": "^17.13.0",
|
|
58
|
+
"tinyglobby": "^0.2.17",
|
|
59
|
+
"typescript-eslint": "^8.71.1"
|
|
60
|
+
},
|
|
61
|
+
"peerDependencies": {
|
|
62
|
+
"eslint": "^9",
|
|
63
|
+
"eslint-config-next": ">=16",
|
|
64
|
+
"prettier": "^3",
|
|
65
|
+
"typescript": "^5"
|
|
66
|
+
},
|
|
67
|
+
"peerDependenciesMeta": {
|
|
68
|
+
"eslint-config-next": {
|
|
69
|
+
"optional": true
|
|
70
|
+
},
|
|
71
|
+
"prettier": {
|
|
72
|
+
"optional": true
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
"devDependencies": {
|
|
76
|
+
"@size-limit/file": "^14.2.0",
|
|
77
|
+
"@types/estree": "^1.0.8",
|
|
78
|
+
"@types/node": "^22.15.0",
|
|
79
|
+
"eslint": "^9.39.0",
|
|
80
|
+
"eslint-config-next": "^16.4.0",
|
|
81
|
+
"next": "^16.4.0",
|
|
82
|
+
"prettier": "^3.9.9",
|
|
83
|
+
"publint": "^0.3.25",
|
|
84
|
+
"size-limit": "^14.2.0",
|
|
85
|
+
"typescript": "^5.9.0",
|
|
86
|
+
"vitest": "^4.1.11"
|
|
87
|
+
},
|
|
88
|
+
"engines": {
|
|
89
|
+
"node": ">=22"
|
|
90
|
+
},
|
|
91
|
+
"publishConfig": {
|
|
92
|
+
"access": "public"
|
|
93
|
+
},
|
|
94
|
+
"size-limit": [
|
|
95
|
+
{
|
|
96
|
+
"name": "krizaka-ratchet",
|
|
97
|
+
"path": [
|
|
98
|
+
"bin/krizaka-ratchet.mjs",
|
|
99
|
+
"ratchet/ratchet.js",
|
|
100
|
+
"patterns.js"
|
|
101
|
+
],
|
|
102
|
+
"limit": "5 kB",
|
|
103
|
+
"gzip": true
|
|
104
|
+
}
|
|
105
|
+
],
|
|
106
|
+
"scripts": {
|
|
107
|
+
"lint": "eslint . --max-warnings 0 && tsc --noEmit --noUnusedLocals --noUnusedParameters --noFallthroughCasesInSwitch --noImplicitReturns",
|
|
108
|
+
"typecheck": "tsc --noEmit",
|
|
109
|
+
"test": "vitest run",
|
|
110
|
+
"size": "size-limit",
|
|
111
|
+
"publint": "publint"
|
|
112
|
+
}
|
|
6
113
|
}
|
package/patterns.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// The four debts the UI refactor removes, as regex sources. One definition, read by the ESLint rules
|
|
2
|
+
// (eslint/krizaka-ui.js, on className literals) and by the ratchet (bin/krizaka-ratchet.mjs, on the source text).
|
|
3
|
+
|
|
4
|
+
/** Tailwind palette families a product never writes: a component reads a role (bg-surface-1, text-accent…). */
|
|
5
|
+
export const PALETTE_FAMILIES = Object.freeze([
|
|
6
|
+
"zinc", "slate", "gray", "neutral", "stone",
|
|
7
|
+
"fuchsia", "violet", "purple", "pink", "rose",
|
|
8
|
+
"emerald", "green", "amber", "yellow", "red",
|
|
9
|
+
"blue", "sky", "cyan", "indigo",
|
|
10
|
+
]);
|
|
11
|
+
|
|
12
|
+
/** Utilities that take a colour. */
|
|
13
|
+
export const COLOR_UTILITIES = Object.freeze(["bg", "text", "border", "from", "via", "to", "ring", "shadow", "fill", "stroke"]);
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* `bg-zinc-900`, `text-violet-400/80`… — a raw palette step.
|
|
17
|
+
* @param {readonly string[]} [allow] families allowed for documented, isolated cases (e.g. `["emerald"]`).
|
|
18
|
+
* @returns {string} regex source
|
|
19
|
+
*/
|
|
20
|
+
export function paletteSource(allow = []) {
|
|
21
|
+
const families = PALETTE_FAMILIES.filter((family) => !allow.includes(family));
|
|
22
|
+
if (families.length === 0) return "(?!)"; // everything allowed: matches nothing
|
|
23
|
+
return `\\b(${COLOR_UTILITIES.join("|")})-(${families.join("|")})-[0-9]`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** `light:bg-white` — a theme is a set of tokens, never a variant. */
|
|
27
|
+
export const LIGHT_VARIANT_SOURCE = "\\blight:";
|
|
28
|
+
|
|
29
|
+
/** `bg-[var(--surface-1)]` — the variable has a utility in @krizaka/tailwind. */
|
|
30
|
+
export const ARBITRARY_VAR_SOURCE = "\\[var\\(--";
|
|
31
|
+
|
|
32
|
+
/** `className={`…${x}`}` — the ratchet's text approximation of the TemplateLiteral rule. */
|
|
33
|
+
export const CLASSNAME_TEMPLATE_SOURCE = "className=\\{\\s*`";
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// The Krizaka Prettier config: `export { default } from "@krizaka/config/prettier";` in prettier.config.js.
|
|
2
|
+
/** @type {import("prettier").Config} */
|
|
3
|
+
const config = {
|
|
4
|
+
printWidth: 120,
|
|
5
|
+
singleQuote: false,
|
|
6
|
+
jsxSingleQuote: false,
|
|
7
|
+
trailingComma: "all",
|
|
8
|
+
semi: true,
|
|
9
|
+
tabWidth: 2,
|
|
10
|
+
useTabs: false,
|
|
11
|
+
arrowParens: "always",
|
|
12
|
+
endOfLine: "lf",
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export default config;
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
// The ratchet: counts the four UI debts in a repository and refuses any increase against lint-ratchet.json.
|
|
2
|
+
// Same patterns as the ESLint rules (../patterns.js), applied to the source text: the counter sees every
|
|
3
|
+
// occurrence (a class in a `cn()` argument or a constant too), so it can only be stricter than the rule.
|
|
4
|
+
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
5
|
+
import { relative, resolve } from "node:path";
|
|
6
|
+
|
|
7
|
+
import { glob } from "tinyglobby";
|
|
8
|
+
|
|
9
|
+
import { ARBITRARY_VAR_SOURCE, CLASSNAME_TEMPLATE_SOURCE, LIGHT_VARIANT_SOURCE, paletteSource } from "../patterns.js";
|
|
10
|
+
|
|
11
|
+
export const RATCHET_FILE = "lint-ratchet.json";
|
|
12
|
+
export const DEFAULT_GLOBS = Object.freeze(["app/**/*.tsx", "components/**/*.tsx", "src/**/*.tsx"]);
|
|
13
|
+
const IGNORE = ["**/node_modules/**", "**/dist/**", "**/.next/**", "**/storybook-static/**", "**/coverage/**"];
|
|
14
|
+
|
|
15
|
+
/** @typedef {"palette" | "light" | "arbitraryVar" | "classNameTemplate"} CounterName */
|
|
16
|
+
/** @typedef {Record<CounterName, number>} Counters */
|
|
17
|
+
/** @typedef {{ globs?: string[], allow?: string[], counters: Counters }} RatchetFile */
|
|
18
|
+
|
|
19
|
+
/** The counters, in report order. */
|
|
20
|
+
export const COUNTERS = /** @type {const} */ (["palette", "light", "arbitraryVar", "classNameTemplate"]);
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @param {string[]} [allow] palette families tolerated (same option as krizakaUi)
|
|
24
|
+
* @returns {Record<CounterName, RegExp>}
|
|
25
|
+
*/
|
|
26
|
+
export function counterPatterns(allow = []) {
|
|
27
|
+
return {
|
|
28
|
+
palette: new RegExp(paletteSource(allow), "g"),
|
|
29
|
+
light: new RegExp(LIGHT_VARIANT_SOURCE, "g"),
|
|
30
|
+
arbitraryVar: new RegExp(ARBITRARY_VAR_SOURCE, "g"),
|
|
31
|
+
classNameTemplate: new RegExp(CLASSNAME_TEMPLATE_SOURCE, "g"),
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Counts the four debts in one source text.
|
|
37
|
+
* @param {string} source
|
|
38
|
+
* @param {string[]} [allow]
|
|
39
|
+
* @returns {Counters}
|
|
40
|
+
*/
|
|
41
|
+
export function countSource(source, allow = []) {
|
|
42
|
+
const patterns = counterPatterns(allow);
|
|
43
|
+
/** @type {Counters} */
|
|
44
|
+
const counters = { palette: 0, light: 0, arbitraryVar: 0, classNameTemplate: 0 };
|
|
45
|
+
for (const name of COUNTERS) counters[name] = source.match(patterns[name])?.length ?? 0;
|
|
46
|
+
return counters;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Counts the debts in every file matched by the globs under `root`.
|
|
51
|
+
* @param {{ root: string, globs?: readonly string[], allow?: string[] }} options
|
|
52
|
+
* @returns {Promise<{ counters: Counters, files: number }>}
|
|
53
|
+
*/
|
|
54
|
+
export async function countRepository({ root, globs = DEFAULT_GLOBS, allow = [] }) {
|
|
55
|
+
const files = await glob([...globs], { cwd: root, ignore: IGNORE, absolute: true });
|
|
56
|
+
/** @type {Counters} */
|
|
57
|
+
const counters = { palette: 0, light: 0, arbitraryVar: 0, classNameTemplate: 0 };
|
|
58
|
+
for (const file of files) {
|
|
59
|
+
const found = countSource(readFileSync(file, "utf8"), allow);
|
|
60
|
+
for (const name of COUNTERS) counters[name] += found[name];
|
|
61
|
+
}
|
|
62
|
+
return { counters, files: files.length };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* @param {string} path
|
|
67
|
+
* @returns {RatchetFile}
|
|
68
|
+
*/
|
|
69
|
+
function readRatchet(path) {
|
|
70
|
+
const data = JSON.parse(readFileSync(path, "utf8"));
|
|
71
|
+
for (const name of COUNTERS) {
|
|
72
|
+
if (!Number.isInteger(data?.counters?.[name]) || data.counters[name] < 0) {
|
|
73
|
+
throw new RatchetError(`${path}: counters.${name} must be a non-negative integer.`);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return data;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* @param {string} path
|
|
81
|
+
* @param {RatchetFile} data
|
|
82
|
+
*/
|
|
83
|
+
function writeRatchet(path, data) {
|
|
84
|
+
writeFileSync(path, `${JSON.stringify(data, null, 2)}\n`);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** A usage or file error (exit code 2), as opposed to a counter going up (exit code 1). */
|
|
88
|
+
export class RatchetError extends Error {}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* @typedef {object} RatchetResult
|
|
92
|
+
* @property {boolean} ok no counter above its recorded value
|
|
93
|
+
* @property {"check" | "init" | "update"} mode
|
|
94
|
+
* @property {string} file the ratchet file, relative to root
|
|
95
|
+
* @property {number} files source files scanned
|
|
96
|
+
* @property {string[]} globs
|
|
97
|
+
* @property {Record<CounterName, { recorded: number, current: number, delta: number }>} counters
|
|
98
|
+
* @property {CounterName[]} increased
|
|
99
|
+
* @property {CounterName[]} decreased
|
|
100
|
+
* @property {boolean} written the ratchet file was created or lowered
|
|
101
|
+
*/
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Runs the ratchet.
|
|
105
|
+
* - `check` (default): fails when a counter is above its recorded value.
|
|
106
|
+
* - `init`: creates the file with the current counters (refuses to overwrite one).
|
|
107
|
+
* - `update`: lowers each recorded value to the current one, never raises it (still fails on an increase).
|
|
108
|
+
* @param {{ root: string, mode?: "check" | "init" | "update", globs?: string[], file?: string }} options
|
|
109
|
+
* @returns {Promise<RatchetResult>}
|
|
110
|
+
*/
|
|
111
|
+
export async function runRatchet({ root, mode = "check", globs, file = RATCHET_FILE }) {
|
|
112
|
+
const path = resolve(root, file);
|
|
113
|
+
const exists = existsSync(path);
|
|
114
|
+
if (mode === "init" && exists) throw new RatchetError(`${file} already exists: use --update to lower it.`);
|
|
115
|
+
if (mode !== "init" && !exists) throw new RatchetError(`${file} not found in ${root}: run krizaka-ratchet --init first.`);
|
|
116
|
+
|
|
117
|
+
const recordedFile = exists ? readRatchet(path) : undefined;
|
|
118
|
+
const useGlobs = globs && globs.length > 0 ? globs : (recordedFile?.globs ?? [...DEFAULT_GLOBS]);
|
|
119
|
+
const allow = recordedFile?.allow ?? [];
|
|
120
|
+
const { counters: current, files } = await countRepository({ root, globs: useGlobs, allow });
|
|
121
|
+
const recorded = recordedFile?.counters ?? current;
|
|
122
|
+
|
|
123
|
+
/** @type {RatchetResult["counters"]} */
|
|
124
|
+
const report = /** @type {any} */ ({});
|
|
125
|
+
/** @type {CounterName[]} */ const increased = [];
|
|
126
|
+
/** @type {CounterName[]} */ const decreased = [];
|
|
127
|
+
for (const name of COUNTERS) {
|
|
128
|
+
const delta = current[name] - recorded[name];
|
|
129
|
+
report[name] = { recorded: recorded[name], current: current[name], delta };
|
|
130
|
+
if (delta > 0) increased.push(name);
|
|
131
|
+
if (delta < 0) decreased.push(name);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
let written = false;
|
|
135
|
+
if (mode === "init") {
|
|
136
|
+
writeRatchet(path, { globs: useGlobs, ...(allow.length > 0 ? { allow } : {}), counters: current });
|
|
137
|
+
written = true;
|
|
138
|
+
} else if (mode === "update" && decreased.length > 0 && recordedFile) {
|
|
139
|
+
/** @type {Counters} */
|
|
140
|
+
const lowered = { ...recordedFile.counters };
|
|
141
|
+
for (const name of COUNTERS) lowered[name] = Math.min(recorded[name], current[name]);
|
|
142
|
+
writeRatchet(path, { ...recordedFile, counters: lowered });
|
|
143
|
+
for (const name of decreased) report[name].recorded = lowered[name];
|
|
144
|
+
written = true;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const shown = relative(root, path);
|
|
148
|
+
return {
|
|
149
|
+
ok: increased.length === 0,
|
|
150
|
+
mode,
|
|
151
|
+
file: shown.startsWith("..") ? path : shown,
|
|
152
|
+
files,
|
|
153
|
+
globs: useGlobs,
|
|
154
|
+
counters: report,
|
|
155
|
+
increased,
|
|
156
|
+
decreased,
|
|
157
|
+
written,
|
|
158
|
+
};
|
|
159
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json.schemastore.org/tsconfig",
|
|
3
|
+
"display": "Krizaka — base",
|
|
4
|
+
"compilerOptions": {
|
|
5
|
+
"target": "ES2022",
|
|
6
|
+
"lib": ["ES2022"],
|
|
7
|
+
"module": "ESNext",
|
|
8
|
+
"moduleResolution": "Bundler",
|
|
9
|
+
"moduleDetection": "force",
|
|
10
|
+
"resolveJsonModule": true,
|
|
11
|
+
"strict": true,
|
|
12
|
+
"skipLibCheck": true,
|
|
13
|
+
"isolatedModules": true,
|
|
14
|
+
"esModuleInterop": true,
|
|
15
|
+
"forceConsistentCasingInFileNames": true,
|
|
16
|
+
"noFallthroughCasesInSwitch": true
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json.schemastore.org/tsconfig",
|
|
3
|
+
"display": "Krizaka — Next.js app",
|
|
4
|
+
"extends": "./base.json",
|
|
5
|
+
"compilerOptions": {
|
|
6
|
+
"target": "ES2022",
|
|
7
|
+
"lib": ["DOM", "DOM.Iterable", "ESNext"],
|
|
8
|
+
"jsx": "react-jsx",
|
|
9
|
+
"allowJs": true,
|
|
10
|
+
"noEmit": true,
|
|
11
|
+
"incremental": true,
|
|
12
|
+
"plugins": [{ "name": "next" }]
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json.schemastore.org/tsconfig",
|
|
3
|
+
"display": "Krizaka — React Native (Expo)",
|
|
4
|
+
"extends": "./base.json",
|
|
5
|
+
"compilerOptions": {
|
|
6
|
+
"lib": ["DOM", "ESNext"],
|
|
7
|
+
"jsx": "react-native",
|
|
8
|
+
"allowJs": true,
|
|
9
|
+
"noEmit": true,
|
|
10
|
+
"customConditions": ["react-native"]
|
|
11
|
+
}
|
|
12
|
+
}
|