@maple-kit/lint 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/LICENSE +202 -0
- package/NOTICE +9 -0
- package/README.md +50 -0
- package/dist/color.d.ts +35 -0
- package/dist/color.js +190 -0
- package/dist/comment.d.ts +35 -0
- package/dist/comment.js +49 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +8 -0
- package/dist/named-colors.d.ts +11 -0
- package/dist/named-colors.js +745 -0
- package/dist/page/page.iife.js +425 -0
- package/dist/rendered/audit.d.ts +79 -0
- package/dist/rendered/audit.js +141 -0
- package/dist/rendered/collect.d.ts +45 -0
- package/dist/rendered/collect.js +101 -0
- package/dist/rendered/rules.d.ts +30 -0
- package/dist/rendered/rules.js +131 -0
- package/dist/tokens.d.ts +39 -0
- package/dist/tokens.js +49 -0
- package/dist/types.d.ts +26 -0
- package/package.json +43 -0
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 2026 The Maple Authors
|
|
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/NOTICE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
Maple
|
|
2
|
+
Copyright 2026 The Maple Authors
|
|
3
|
+
|
|
4
|
+
This product includes software developed by The Maple Authors
|
|
5
|
+
(https://github.com/maple-kit/maple).
|
|
6
|
+
|
|
7
|
+
Portions of this software are derived from third-party sources. Each derived
|
|
8
|
+
file carries an attribution header naming its origin and license. A summary is
|
|
9
|
+
kept in packages/core/src/lib/README.md.
|
package/README.md
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# @maple-kit/lint
|
|
2
|
+
|
|
3
|
+
Design-system lint for Maple. This package is the **rendered tier**: the rules
|
|
4
|
+
that need a browser to have laid the page out before they can judge it.
|
|
5
|
+
|
|
6
|
+
A static linter reads the source. A rendered rule reads what the cascade, the
|
|
7
|
+
theme and the media query finally produced, which is where an off-token colour
|
|
8
|
+
that arrived through three layers of `var()` actually becomes visible.
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
import { lintRendered } from "@maple-kit/lint";
|
|
12
|
+
|
|
13
|
+
const findings = await lintRendered({
|
|
14
|
+
url: "https://preview-123.example.app",
|
|
15
|
+
tokenFiles: ["src/styles/tokens.css"],
|
|
16
|
+
bypassHeaders: { "x-vercel-protection-bypass": process.env.PREVIEW_BYPASS! },
|
|
17
|
+
});
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Every finding carries the anchor cascade's own `Anchor`, recorded by
|
|
21
|
+
`describeElement` in the page, so it means the same thing by "where" that a
|
|
22
|
+
comment does. `findingComment(finding, { branch, context })` turns one into a
|
|
23
|
+
`Comment` the overlay pins; a run does not do that on its own.
|
|
24
|
+
|
|
25
|
+
## Rules
|
|
26
|
+
|
|
27
|
+
| Rule | What it finds |
|
|
28
|
+
| -------------------------------- | ------------------------------------------------- |
|
|
29
|
+
| `maple/rendered-color-token` | Colours the token set does not declare. |
|
|
30
|
+
| `maple/rendered-type-scale` | Font sizes off the type scale. |
|
|
31
|
+
| `maple/rendered-touch-target` | Interactive elements under 24px on either axis. |
|
|
32
|
+
| `maple/rendered-contrast` | Text under the WCAG AA ratio for its size. |
|
|
33
|
+
| `maple/rendered-motion-property` | Motion on anything but `opacity` and `transform`. |
|
|
34
|
+
| `maple/rendered-reduced-motion` | Motion that survives `prefers-reduced-motion`. |
|
|
35
|
+
|
|
36
|
+
[docs/lint.md](../../docs/lint.md) covers each of them and the tiers around this
|
|
37
|
+
one.
|
|
38
|
+
|
|
39
|
+
## What it cannot read
|
|
40
|
+
|
|
41
|
+
Hex, `rgb()`, `hsl()`, `color(srgb …)` and the named colours all read. A wider
|
|
42
|
+
gamut — `oklch()`, `color(display-p3 …)` — does not, and a run warns through
|
|
43
|
+
Maple's logger naming what it skipped rather than reporting a clean page it
|
|
44
|
+
did not fully check.
|
|
45
|
+
|
|
46
|
+
## Authentication
|
|
47
|
+
|
|
48
|
+
A run sends the preview platform's bypass as request headers and nothing else.
|
|
49
|
+
Reviewer cookies are never used: CI's lint run does not borrow a person's
|
|
50
|
+
session to see a page.
|
package/dist/color.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
//#region src/color.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Colour parsing and WCAG contrast, implemented here rather than taken from a
|
|
4
|
+
* package.
|
|
5
|
+
*
|
|
6
|
+
* Three functions are needed and the formulas are published, so this follows
|
|
7
|
+
* the dependency rule in CLAUDE.md. Relative luminance and the contrast ratio
|
|
8
|
+
* are from WCAG 2.1, https://www.w3.org/TR/WCAG21/#dfn-relative-luminance and
|
|
9
|
+
* #dfn-contrast-ratio.
|
|
10
|
+
*/
|
|
11
|
+
/** A colour in sRGB, each channel 0-255, alpha 0-1. */
|
|
12
|
+
export interface Rgb {
|
|
13
|
+
readonly r: number;
|
|
14
|
+
readonly g: number;
|
|
15
|
+
readonly b: number;
|
|
16
|
+
readonly a: number;
|
|
17
|
+
}
|
|
18
|
+
/** Whether a value looks like an attempt at a colour this cannot read. */
|
|
19
|
+
export declare function isUnreadableColor(value: string): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Parses what `getComputedStyle` returns and what a token file declares: hex
|
|
22
|
+
* in four lengths, `rgb()`, `hsl()`, `color(srgb …)` and the named colours. A
|
|
23
|
+
* wider gamut — `oklch()`, `color(display-p3 …)` — returns undefined, which
|
|
24
|
+
* `isUnreadableColor` reports rather than passing over in silence.
|
|
25
|
+
*/
|
|
26
|
+
export declare function parseColor(value: string): Rgb | undefined;
|
|
27
|
+
/** Normalises a colour to `r,g,b,a`, so two spellings of one colour compare equal. */
|
|
28
|
+
export declare function colorKey(color: Rgb): string;
|
|
29
|
+
/** Composites a colour over an opaque backdrop, which is what the eye sees. */
|
|
30
|
+
export declare function over(color: Rgb, backdrop: Rgb): Rgb;
|
|
31
|
+
/** WCAG relative luminance, on a colour already composited to opaque. */
|
|
32
|
+
export declare function relativeLuminance(color: Rgb): number;
|
|
33
|
+
/** WCAG contrast ratio, between 1 and 21. Order of the arguments does not matter. */
|
|
34
|
+
export declare function contrastRatio(foreground: Rgb, background: Rgb): number;
|
|
35
|
+
//#endregion
|
package/dist/color.js
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { NAMED_COLORS } from "./named-colors.js";
|
|
2
|
+
//#region src/color.ts
|
|
3
|
+
const HEX_SHORT = /^#([\da-f])([\da-f])([\da-f])([\da-f])?$/i;
|
|
4
|
+
const HEX_LONG = /^#([\da-f]{2})([\da-f]{2})([\da-f]{2})([\da-f]{2})?$/i;
|
|
5
|
+
const FUNCTIONAL = /^rgba?\(([^)]+)\)$/i;
|
|
6
|
+
const HSL = /^hsla?\(([^)]+)\)$/i;
|
|
7
|
+
const SRGB = /^color\(\s?srgb\s([^)]+)\)$/i;
|
|
8
|
+
const TURNS = {
|
|
9
|
+
deg: 1,
|
|
10
|
+
grad: .9,
|
|
11
|
+
rad: 180 / Math.PI,
|
|
12
|
+
turn: 360
|
|
13
|
+
};
|
|
14
|
+
function channel(part, scale) {
|
|
15
|
+
const text = part.trim();
|
|
16
|
+
const value = Number.parseFloat(text);
|
|
17
|
+
if (Number.isNaN(value)) return NaN;
|
|
18
|
+
return text.endsWith("%") ? value / 100 * scale : value;
|
|
19
|
+
}
|
|
20
|
+
function parseHex(text) {
|
|
21
|
+
const match = HEX_SHORT.exec(text) ?? HEX_LONG.exec(text);
|
|
22
|
+
if (!match) return void 0;
|
|
23
|
+
const short = match[0].length <= 5;
|
|
24
|
+
const read = (part) => Number.parseInt(short ? part + part : part, 16);
|
|
25
|
+
const alpha = match[4];
|
|
26
|
+
return {
|
|
27
|
+
r: read(match[1]),
|
|
28
|
+
g: read(match[2]),
|
|
29
|
+
b: read(match[3]),
|
|
30
|
+
a: alpha === void 0 ? 1 : read(alpha) / 255
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function argumentsOf(body) {
|
|
34
|
+
const [channels, slashed] = body.split("/");
|
|
35
|
+
const parts = channels.trim().split(/[\s,]+/).filter(Boolean);
|
|
36
|
+
return {
|
|
37
|
+
parts,
|
|
38
|
+
alpha: slashed ?? parts[3]
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function parseFunctional(text) {
|
|
42
|
+
const call = FUNCTIONAL.exec(text);
|
|
43
|
+
if (!call) return void 0;
|
|
44
|
+
const { parts, alpha } = argumentsOf(call[1]);
|
|
45
|
+
if (parts.length < 3) return void 0;
|
|
46
|
+
const [r, g, b] = parts.map((part) => channel(part, 255));
|
|
47
|
+
const a = channel(alpha ?? "1", 1);
|
|
48
|
+
if ([
|
|
49
|
+
r,
|
|
50
|
+
g,
|
|
51
|
+
b,
|
|
52
|
+
a
|
|
53
|
+
].some((value) => Number.isNaN(value))) return void 0;
|
|
54
|
+
return {
|
|
55
|
+
r,
|
|
56
|
+
g,
|
|
57
|
+
b,
|
|
58
|
+
a
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function hue(part) {
|
|
62
|
+
const text = part.trim();
|
|
63
|
+
const value = Number.parseFloat(text);
|
|
64
|
+
if (Number.isNaN(value)) return NaN;
|
|
65
|
+
const lower = text.toLowerCase();
|
|
66
|
+
const unit = Object.entries(TURNS).find(([name]) => lower.endsWith(name));
|
|
67
|
+
return value * (unit === void 0 ? 1 : unit[1]);
|
|
68
|
+
}
|
|
69
|
+
function fromHsl(degrees, saturation, lightness) {
|
|
70
|
+
const turned = (degrees % 360 + 360) % 360;
|
|
71
|
+
const reach = saturation * Math.min(lightness, 1 - lightness);
|
|
72
|
+
const at = (offset) => {
|
|
73
|
+
const k = (offset + turned / 30) % 12;
|
|
74
|
+
return (lightness - reach * Math.max(-1, Math.min(k - 3, 9 - k, 1))) * 255;
|
|
75
|
+
};
|
|
76
|
+
return {
|
|
77
|
+
r: at(0),
|
|
78
|
+
g: at(8),
|
|
79
|
+
b: at(4)
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function parseHsl(text) {
|
|
83
|
+
const call = HSL.exec(text);
|
|
84
|
+
if (!call) return void 0;
|
|
85
|
+
const { parts, alpha } = argumentsOf(call[1]);
|
|
86
|
+
if (parts.length < 3) return void 0;
|
|
87
|
+
const degrees = hue(parts[0]);
|
|
88
|
+
const saturation = channel(parts[1], 1) / (parts[1].includes("%") ? 1 : 100);
|
|
89
|
+
const lightness = channel(parts[2], 1) / (parts[2].includes("%") ? 1 : 100);
|
|
90
|
+
const a = channel(alpha ?? "1", 1);
|
|
91
|
+
if ([
|
|
92
|
+
degrees,
|
|
93
|
+
saturation,
|
|
94
|
+
lightness,
|
|
95
|
+
a
|
|
96
|
+
].some((value) => Number.isNaN(value))) return void 0;
|
|
97
|
+
return {
|
|
98
|
+
...fromHsl(degrees, saturation, lightness),
|
|
99
|
+
a
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
function parseSrgb(text) {
|
|
103
|
+
const call = SRGB.exec(text);
|
|
104
|
+
if (!call) return void 0;
|
|
105
|
+
const { parts, alpha } = argumentsOf(call[1]);
|
|
106
|
+
if (parts.length < 3) return void 0;
|
|
107
|
+
const [r, g, b] = parts.map((part) => channel(part, 1) * 255);
|
|
108
|
+
const a = channel(alpha ?? "1", 1);
|
|
109
|
+
if ([
|
|
110
|
+
r,
|
|
111
|
+
g,
|
|
112
|
+
b,
|
|
113
|
+
a
|
|
114
|
+
].some((value) => Number.isNaN(value))) return void 0;
|
|
115
|
+
return {
|
|
116
|
+
r,
|
|
117
|
+
g,
|
|
118
|
+
b,
|
|
119
|
+
a
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
function parseNamed(text) {
|
|
123
|
+
const name = text.toLowerCase();
|
|
124
|
+
if (name === "transparent") return {
|
|
125
|
+
r: 0,
|
|
126
|
+
g: 0,
|
|
127
|
+
b: 0,
|
|
128
|
+
a: 0
|
|
129
|
+
};
|
|
130
|
+
const found = NAMED_COLORS[name];
|
|
131
|
+
return found === void 0 ? void 0 : {
|
|
132
|
+
r: found[0],
|
|
133
|
+
g: found[1],
|
|
134
|
+
b: found[2],
|
|
135
|
+
a: 1
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
const PARSERS = [
|
|
139
|
+
parseHex,
|
|
140
|
+
parseFunctional,
|
|
141
|
+
parseHsl,
|
|
142
|
+
parseSrgb,
|
|
143
|
+
parseNamed
|
|
144
|
+
];
|
|
145
|
+
const COLOUR_SHAPED = /^(#|rgba?\(|hsla?\(|hwb\(|lab\(|lch\(|oklab\(|oklch\(|color\()/i;
|
|
146
|
+
function isUnreadableColor(value) {
|
|
147
|
+
const text = value.trim();
|
|
148
|
+
if (parseColor(text) !== void 0) return false;
|
|
149
|
+
return COLOUR_SHAPED.test(text);
|
|
150
|
+
}
|
|
151
|
+
function parseColor(value) {
|
|
152
|
+
const text = value.trim();
|
|
153
|
+
for (const parse of PARSERS) {
|
|
154
|
+
const found = parse(text);
|
|
155
|
+
if (found) return found;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
function colorKey(color) {
|
|
159
|
+
const round = (value) => Math.round(value);
|
|
160
|
+
return `${round(color.r)},${round(color.g)},${round(color.b)},${color.a.toFixed(3)}`;
|
|
161
|
+
}
|
|
162
|
+
function over(color, backdrop) {
|
|
163
|
+
const mix = (top, bottom) => top * color.a + bottom * (1 - color.a);
|
|
164
|
+
return {
|
|
165
|
+
r: mix(color.r, backdrop.r),
|
|
166
|
+
g: mix(color.g, backdrop.g),
|
|
167
|
+
b: mix(color.b, backdrop.b),
|
|
168
|
+
a: 1
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
function relativeLuminance(color) {
|
|
172
|
+
const [r, g, b] = [
|
|
173
|
+
color.r,
|
|
174
|
+
color.g,
|
|
175
|
+
color.b
|
|
176
|
+
].map((value) => {
|
|
177
|
+
const channelValue = value / 255;
|
|
178
|
+
return channelValue <= .04045 ? channelValue / 12.92 : ((channelValue + .055) / 1.055) ** 2.4;
|
|
179
|
+
});
|
|
180
|
+
return .2126 * r + .7152 * g + .0722 * b;
|
|
181
|
+
}
|
|
182
|
+
function contrastRatio(foreground, background) {
|
|
183
|
+
const first = relativeLuminance(foreground);
|
|
184
|
+
const second = relativeLuminance(background);
|
|
185
|
+
const lighter = Math.max(first, second);
|
|
186
|
+
const darker = Math.min(first, second);
|
|
187
|
+
return (lighter + .05) / (darker + .05);
|
|
188
|
+
}
|
|
189
|
+
//#endregion
|
|
190
|
+
export { colorKey, contrastRatio, isUnreadableColor, over, parseColor, relativeLuminance };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Finding } from "./types.js";
|
|
2
|
+
import { RenderedRun } from "./rendered/audit.js";
|
|
3
|
+
import { Comment, CommentAuthor, CommentContext } from "@maple-kit/core";
|
|
4
|
+
//#region src/comment.d.ts
|
|
5
|
+
/** Who a finding is from. Not a person, and it says so. */
|
|
6
|
+
export declare const LINT_AUTHOR: CommentAuthor;
|
|
7
|
+
/** What a comment needs that a finding does not carry. */
|
|
8
|
+
export interface FindingCommentOptions {
|
|
9
|
+
/** The branch or pull request the preview is for. */
|
|
10
|
+
readonly branch: string;
|
|
11
|
+
/** The environment it was seen in, from the run that found it. */
|
|
12
|
+
readonly context: CommentContext;
|
|
13
|
+
/** The commit the preview was serving, where the caller knows it. */
|
|
14
|
+
readonly commit?: string;
|
|
15
|
+
readonly author?: CommentAuthor;
|
|
16
|
+
/** When the run happened. Defaults to now; a test passes its own. */
|
|
17
|
+
readonly now?: string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* A stable id for a finding on a branch. Two runs over an unchanged page
|
|
21
|
+
* produce the same one, so a store that already holds it can say so.
|
|
22
|
+
*/
|
|
23
|
+
export declare function findingCommentId(finding: Finding, branch: string): string;
|
|
24
|
+
/** One finding as a comment the overlay can pin. */
|
|
25
|
+
export declare function findingComment(finding: Finding, options: FindingCommentOptions): Comment;
|
|
26
|
+
/** Every finding as a comment, in the order they were found. */
|
|
27
|
+
export declare function findingComments(findings: readonly Finding[], options: FindingCommentOptions): readonly Comment[];
|
|
28
|
+
/** What a run needs beyond itself to become comments. */
|
|
29
|
+
export type RunCommentOptions = Omit<FindingCommentOptions, "context">;
|
|
30
|
+
/**
|
|
31
|
+
* A whole run as comments, each keeping the environment it was seen in. A
|
|
32
|
+
* finding found only at 375px stores 375px rather than the run's widest.
|
|
33
|
+
*/
|
|
34
|
+
export declare function commentsForRun(run: RenderedRun, options: RunCommentOptions): readonly Comment[];
|
|
35
|
+
//#endregion
|
package/dist/comment.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
//#region src/comment.ts
|
|
2
|
+
const LINT_AUTHOR = {
|
|
3
|
+
id: "maple-lint",
|
|
4
|
+
name: "Maple lint",
|
|
5
|
+
provenance: "server"
|
|
6
|
+
};
|
|
7
|
+
function hash(value) {
|
|
8
|
+
let result = 2166136261;
|
|
9
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
10
|
+
result ^= value.charCodeAt(index);
|
|
11
|
+
result = Math.imul(result, 16777619) >>> 0;
|
|
12
|
+
}
|
|
13
|
+
return result.toString(36);
|
|
14
|
+
}
|
|
15
|
+
function place(finding) {
|
|
16
|
+
const { anchor } = finding;
|
|
17
|
+
return anchor.key ?? anchor.source ?? anchor.selector ?? anchor.component ?? "";
|
|
18
|
+
}
|
|
19
|
+
function findingCommentId(finding, branch) {
|
|
20
|
+
return `lint_${hash(`${branch}|${finding.rule}|${place(finding)}|${finding.message}`)}`;
|
|
21
|
+
}
|
|
22
|
+
function bodyOf(finding) {
|
|
23
|
+
const rule = finding.url === void 0 ? finding.rule : `${finding.rule} — ${finding.url}`;
|
|
24
|
+
return `${finding.message}\n\n${rule}`;
|
|
25
|
+
}
|
|
26
|
+
function findingComment(finding, options) {
|
|
27
|
+
return {
|
|
28
|
+
id: findingCommentId(finding, options.branch),
|
|
29
|
+
branch: options.branch,
|
|
30
|
+
...options.commit === void 0 ? {} : { commit: options.commit },
|
|
31
|
+
body: bodyOf(finding),
|
|
32
|
+
status: "open",
|
|
33
|
+
createdAt: options.now ?? (/* @__PURE__ */ new Date()).toISOString(),
|
|
34
|
+
author: options.author ?? LINT_AUTHOR,
|
|
35
|
+
anchor: finding.anchor,
|
|
36
|
+
context: options.context
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
function findingComments(findings, options) {
|
|
40
|
+
return findings.map((finding) => findingComment(finding, options));
|
|
41
|
+
}
|
|
42
|
+
function commentsForRun(run, options) {
|
|
43
|
+
return run.findings.map((finding, index) => findingComment(finding, {
|
|
44
|
+
...options,
|
|
45
|
+
context: run.contexts[index] ?? run.context
|
|
46
|
+
}));
|
|
47
|
+
}
|
|
48
|
+
//#endregion
|
|
49
|
+
export { LINT_AUTHOR, commentsForRun, findingComment, findingCommentId, findingComments };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Rgb, colorKey, contrastRatio, isUnreadableColor, over, parseColor, relativeLuminance } from "./color.js";
|
|
2
|
+
import { Finding, Severity, Tier } from "./types.js";
|
|
3
|
+
import { DEFAULT_VIEWPORTS, Pass, RenderedLintOptions, RenderedRun, Seen, Viewport, dedupe, lintRendered } from "./rendered/audit.js";
|
|
4
|
+
import { FindingCommentOptions, LINT_AUTHOR, RunCommentOptions, commentsForRun, findingComment, findingCommentId, findingComments } from "./comment.js";
|
|
5
|
+
import { NAMED_COLORS } from "./named-colors.js";
|
|
6
|
+
import { Reading, StyleRecord, readPage } from "./rendered/collect.js";
|
|
7
|
+
import { ROOT_FONT_SIZE, TokenSet, lengthToPx, parseTokens, readTokenFiles } from "./tokens.js";
|
|
8
|
+
import { MIN_TOUCH_TARGET, MOTION_SAFE, RENDERED_RULES, RuleDefinition, renderedFindings, unreadableColors } from "./rendered/rules.js";
|
|
9
|
+
export { DEFAULT_VIEWPORTS, type Finding, type FindingCommentOptions, LINT_AUTHOR, MIN_TOUCH_TARGET, MOTION_SAFE, NAMED_COLORS, type Pass, RENDERED_RULES, ROOT_FONT_SIZE, type Reading, type RenderedLintOptions, type RenderedRun, type Rgb, type RuleDefinition, type RunCommentOptions, type Seen, type Severity, type StyleRecord, type Tier, type TokenSet, type Viewport, colorKey, commentsForRun, contrastRatio, dedupe, findingComment, findingCommentId, findingComments, isUnreadableColor, lengthToPx, lintRendered, over, parseColor, parseTokens, readPage, readTokenFiles, relativeLuminance, renderedFindings, unreadableColors };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { NAMED_COLORS } from "./named-colors.js";
|
|
2
|
+
import { colorKey, contrastRatio, isUnreadableColor, over, parseColor, relativeLuminance } from "./color.js";
|
|
3
|
+
import { LINT_AUTHOR, commentsForRun, findingComment, findingCommentId, findingComments } from "./comment.js";
|
|
4
|
+
import { ROOT_FONT_SIZE, lengthToPx, parseTokens, readTokenFiles } from "./tokens.js";
|
|
5
|
+
import { MIN_TOUCH_TARGET, MOTION_SAFE, RENDERED_RULES, renderedFindings, unreadableColors } from "./rendered/rules.js";
|
|
6
|
+
import { DEFAULT_VIEWPORTS, dedupe, lintRendered } from "./rendered/audit.js";
|
|
7
|
+
import { readPage } from "./rendered/collect.js";
|
|
8
|
+
export { DEFAULT_VIEWPORTS, LINT_AUTHOR, MIN_TOUCH_TARGET, MOTION_SAFE, NAMED_COLORS, RENDERED_RULES, ROOT_FONT_SIZE, colorKey, commentsForRun, contrastRatio, dedupe, findingComment, findingCommentId, findingComments, isUnreadableColor, lengthToPx, lintRendered, over, parseColor, parseTokens, readPage, readTokenFiles, relativeLuminance, renderedFindings, unreadableColors };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
//#region src/named-colors.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* The CSS named colours, as sRGB.
|
|
4
|
+
*
|
|
5
|
+
* A token file may well say `--ink: black`, and a name nobody can read is a
|
|
6
|
+
* token every element painted from it would be reported against. The values
|
|
7
|
+
* are Chromium's own, read back from `getComputedStyle` for each name.
|
|
8
|
+
*/
|
|
9
|
+
/** Every CSS named colour, lowercased, as `[r, g, b]`. */
|
|
10
|
+
export declare const NAMED_COLORS: Readonly<Record<string, readonly [number, number, number]>>;
|
|
11
|
+
//#endregion
|