Repository navigation
Mechanize UI color token enforcement check #614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -30,6 +30,7 @@ const sheets = Object.fromEntries( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| components.map((name) => [name, stripCssComments(read(`web/${name}`))]), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const theme = stripCssComments(read("web/theme.css")); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const recordingCss = stripCssComments(read("web/recording/recording.css")); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Every CSS named color (CSS Color 4, 148 names). A system color such as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Highlight is left out: it is the platform's, and forced-colors rules need it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -59,6 +60,25 @@ const CSS_NAMED_COLORS = ` | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .trim() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .split(/\s+/); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const colorFunction = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /\b(?:rgba?|hsla?|hwb|lab|lch|oklab|oklch|color)\(|#[0-9a-f]{3,8}\b/i; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const namedColor = new RegExp(`\\b(?:${CSS_NAMED_COLORS.join("|")})\\b`, "i"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// The declarations in a sheet that paint a color literal. Declarations, not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// lines: a value wrapped onto the lines after its property is still read, and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// a selector such as `#face-check` is not a hex color. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const paintedColors = (name, css) => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [...css.matchAll(/\{([^{}]*)\}/g)].flatMap(([, body]) => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| body.split(";").flatMap((declaration) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const [, property, value] = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| declaration.match(/^\s*([\w-]+)\s*:([\s\S]*)$/) ?? []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // A mask paints coverage, not color: its stops are alpha only. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!property || /^(?:-webkit-)?mask$/.test(property)) return []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!colorFunction.test(value) && !namedColor.test(value)) return []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return [`${name}: ${property}:${value.replace(/\s+/g, " ")}`]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// The stylesheets a page links, in order, whatever order a <link> writes its | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// attributes in. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const linkedSheets = (html) => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -100,26 +120,43 @@ test("the shipped override file declares nothing", () => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| test("component stylesheets paint no color a theme cannot override", () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This replacement removes the existing checks for component-level theme-role redeclarations and undeclared token references. With the helper scope repaired, |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const colorFunction = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /\b(?:rgba?|hsla?|hwb|lab|lch|oklab|oklch|color)\(|#[0-9a-f]{3,8}\b/i; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const namedColor = new RegExp(`\\b(?:${CSS_NAMED_COLORS.join("|")})\\b`, "i"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Declarations, not lines: a value wrapped onto the lines after its property | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // is still read, and a selector such as `#face-check` is not a hex color. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (const [name, css] of Object.entries(sheets)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert.deepEqual(paintedColors(name, css), [], `${name} paints colors`); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+122
to
126
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a second test with the same name as the one just above it, and it runs the same check on the same
Comment on lines
122
to
+126
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nothing at this head defines
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| test("a declaration does not shadow a token read in the same rule", () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // A later declaration must not shadow a token read by an earlier declaration. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (const [name, css] of Object.entries(sheets)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (const [, body] of css.matchAll(/\{([^{}]*)\}/g)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const properties = new Map(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (const declaration of body.split(";")) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const [, property, value] = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| declaration.match(/^\s*([\w-]+)\s*:([\s\S]*)$/) ?? []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // A mask paints coverage, not color: its stops are alpha only. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!property || /^(?:-webkit-)?mask$/.test(property)) continue; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const where = `${name}: ${property}:${value.replace(/\s+/g, " ")}`; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert.doesNotMatch(value, colorFunction, where); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert.doesNotMatch(value, namedColor, where); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!property) continue; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const hasVar = value.includes("var("); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (properties.has(property) && (properties.get(property) || hasVar)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert.fail( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| `${name} declares ${property} twice in one rule, one of them a token read: ${value.trim()}`, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+138
to
+142
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The condition also fires when the earlier declaration is a literal and the later one reads a token (
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| properties.set(property, hasVar); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| test("recording.css confines color literals to its own :root", () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The diff deletes |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const recordingRest = recordingCss.replace(/:root\s*\{[^{}]*\}/, (block) => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| block.replace(/--[\w-]+\s*:[^;]*;/g, ""), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert.deepEqual( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| paintedColors("recording.css", recordingRest), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "recording.css paints colors outside variables", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| test("a component sheet neither redeclares a theme role nor reads an undeclared one", () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const themed = declared(theme); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // peer.css sits on top of styles.css, so a component may read a layout | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the redeclaration test removed, nothing in this file reads
themeany more, so theeslintgate inscripts/test.shfails here withno-unused-vars('theme' is assigned a value but never used). Nothing at this head checkstheme.cssfor unused tokens either, although the reply on this PR says the test now does. Restoring the test that compares each component sheet'sdeclared(css)againstdeclared(theme)reads it again; if that test stays out, delete this line.