refactor: replace Yaegi CSS/HTML gate plugins with stylelint + htmlhint
This commit is contained in:
1 parent
880faa0b5f
commit
5a4a31ac10
3 files changed
+24
-408
No files matched your search
@@ -1,54 +1,43 @@
|
||||
---
|
||||
name: archie-wf-frontend
|
||||
description: >
|
||||
Validates HTML, CSS, and JS in the worktree before commit. Runs as a
|
||||
gate stage: parses CSS for syntax errors, checks HTML structure, and
|
||||
verifies responsive requirements. Use in any project with web assets.
|
||||
version: 1.0.0
|
||||
Runs frontend quality checks: stylelint for CSS, htmlhint for HTML.
|
||||
Gate stage runs these after Go vet/build/test. Use in any project
|
||||
with web assets to catch syntax errors, broken selectors, and
|
||||
structural issues before they reach a PR.
|
||||
version: 1.1.0
|
||||
metadata:
|
||||
archie:
|
||||
workflow: frontend
|
||||
tools: [go]
|
||||
tools: [stylelint, htmlhint]
|
||||
engine: any
|
||||
plugins:
|
||||
- plugins/css-gate.go
|
||||
- plugins/html-gate.go
|
||||
---
|
||||
|
||||
# Frontend Quality Gate
|
||||
|
||||
## When this runs
|
||||
|
||||
This skill registers a `frontend` workflow. Repos that include web assets
|
||||
(HTML, CSS, JS) can opt into it by labelling issues `frontend` or declaring
|
||||
`workflow: frontend` in their SKILL.md frontmatter.
|
||||
This skill adds frontend linting to the gate stage. After Go checks pass,
|
||||
the gate runs:
|
||||
|
||||
The bundled plugins run during the gate stage:
|
||||
1. `stylelint "**/*.css"` — catches syntax errors, invalid properties,
|
||||
duplicate selectors, calc() misuse, missing units
|
||||
2. `htmlhint "**/*.html"` — catches unclosed tags, mismatched pairs,
|
||||
duplicate IDs, inline style abuse, missing viewport meta
|
||||
|
||||
- `css-gate.go` — parses all .css files and `<style>` blocks for syntax
|
||||
errors, missing units, invalid calc() expressions, and common responsive
|
||||
anti-patterns
|
||||
- `html-gate.go` — checks HTML files for orphaned tags, missing closing
|
||||
elements, and structural issues
|
||||
|
||||
Both plugins return `Finding` structs. Error-level findings block the
|
||||
commit. Warn-level findings are logged.
|
||||
Both must exit 0. If either fails, the stage parks with the lint output.
|
||||
|
||||
## How to use in your project
|
||||
|
||||
1. Copy `.agents/skills/archie-wf-frontend/` into your repo
|
||||
2. Ensure `workflow: frontend` is declared in the skill frontmatter
|
||||
3. Label UI/design issues `frontend` — archie-core routes them here
|
||||
4. The gate runs after Go vet/build/test (unchanged — this skill adds
|
||||
to the existing gate, it doesn't replace it)
|
||||
2. Add to your daemon config gate:
|
||||
```toml
|
||||
gate = [
|
||||
["go", "vet", "./..."],
|
||||
["go", "test", "./..."],
|
||||
["stylelint", "**/*.css"],
|
||||
["htmlhint", "**/*.html"],
|
||||
]
|
||||
```
|
||||
3. Or declare `workflow: frontend` in the skill frontmatter — archie-core
|
||||
loads the skill body and the agent knows to run these during build
|
||||
|
||||
## Verification
|
||||
|
||||
After a frontend-labelled issue is implemented, archie-core runs:
|
||||
```
|
||||
go vet ./... # existing
|
||||
go build ./... # existing
|
||||
go test ./... # existing
|
||||
css-gate # new — CSS validation
|
||||
html-gate # new — HTML validation
|
||||
```
|
||||
@@ -1,180 +0,0 @@
|
||||
// css-gate validates every .css file and <style> block in the worktree.
|
||||
// It catches syntax errors, missing units, invalid calc() expressions,
|
||||
// and common responsive anti-patterns that browsers silently ignore but
|
||||
// that make mobile layouts break.
|
||||
//
|
||||
// Exports: func Gate() gate.Finding
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/gate"
|
||||
)
|
||||
|
||||
// Gate reads .css files in the worktree and returns findings.
|
||||
// Run during the gate stage after Go checks pass.
|
||||
func Gate(ctx gate.Context) []gate.Finding {
|
||||
var findings []gate.Finding
|
||||
|
||||
for _, f := range ctx.ChangedFiles {
|
||||
ext := strings.ToLower(filepath.Ext(f))
|
||||
if ext == ".css" {
|
||||
findings = append(findings, checkCSSFile(filepath.Join(ctx.Dir, f), f)...)
|
||||
}
|
||||
if ext == ".html" || ext == ".htm" {
|
||||
findings = append(findings, checkStyleBlocks(filepath.Join(ctx.Dir, f), f)...)
|
||||
}
|
||||
}
|
||||
|
||||
return findings
|
||||
}
|
||||
|
||||
var (
|
||||
// Matches orphaned property lines — lines after a closing brace that
|
||||
// look like CSS properties but aren't inside a selector block.
|
||||
orphanLine = regexp.MustCompile(`^\s+(background|border|padding|margin|display|overflow|white-space|text-overflow|word-break|font-size|line-height):`)
|
||||
|
||||
// Matches invalid calc() expressions that use grid-only functions.
|
||||
badCalc = regexp.MustCompile(`calc\([^)]*\bminmax\b`)
|
||||
|
||||
// Matches CSS custom properties used in calc without fallback (valid but risky).
|
||||
// This is a warn, not an error.
|
||||
|
||||
// Matches duplicate property declarations within a block.
|
||||
dupProp = regexp.MustCompile(`^\s*([a-z-]+)\s*:`)
|
||||
)
|
||||
|
||||
func checkCSSFile(path, relPath string) []gate.Finding {
|
||||
var findings []gate.Finding
|
||||
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return []gate.Finding{{
|
||||
Level: "warn",
|
||||
File: relPath,
|
||||
Message: fmt.Sprintf("cannot read CSS file: %v", err),
|
||||
}}
|
||||
}
|
||||
|
||||
file := string(data)
|
||||
lines := strings.Split(file, "\n")
|
||||
inBlock := 0 // brace depth
|
||||
|
||||
for i, line := range lines {
|
||||
trimmed := strings.TrimSpace(line)
|
||||
|
||||
// Track brace depth.
|
||||
for _, ch := range line {
|
||||
if ch == '{' {
|
||||
inBlock++
|
||||
}
|
||||
if ch == '}' {
|
||||
inBlock--
|
||||
}
|
||||
}
|
||||
|
||||
// Orphaned property: looks like CSS but depth is 0.
|
||||
if inBlock <= 0 && orphanLine.MatchString(line) {
|
||||
findings = append(findings, gate.Finding{
|
||||
Level: "error",
|
||||
File: relPath,
|
||||
Line: i + 1,
|
||||
Message: fmt.Sprintf("orphaned CSS property (missing opening brace): %s", trimmed),
|
||||
})
|
||||
}
|
||||
|
||||
// Bad calc expressions.
|
||||
if badCalc.MatchString(line) {
|
||||
findings = append(findings, gate.Finding{
|
||||
Level: "error",
|
||||
File: relPath,
|
||||
Line: i + 1,
|
||||
Message: "invalid calc() expression: minmax() is a grid function, not valid in calc()",
|
||||
})
|
||||
}
|
||||
|
||||
// Unclosed brace depth at EOF.
|
||||
if i == len(lines)-1 && inBlock > 0 {
|
||||
findings = append(findings, gate.Finding{
|
||||
Level: "error",
|
||||
File: relPath,
|
||||
Message: fmt.Sprintf("unclosed brace at end of file (%d blocks still open)", inBlock),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Check for common responsive anti-patterns in the full file.
|
||||
if strings.Contains(file, "@media") && !strings.Contains(file, "max-width") && !strings.Contains(file, "min-width") {
|
||||
// Has @media but no width breakpoint — probably not responsive.
|
||||
// This is a warn, not an error — some @media uses are fine without width.
|
||||
}
|
||||
|
||||
return findings
|
||||
}
|
||||
|
||||
func checkStyleBlocks(path, relPath string) []gate.Finding {
|
||||
var findings []gate.Finding
|
||||
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
file := string(data)
|
||||
|
||||
// Extract <style>...</style> blocks.
|
||||
styleRE := regexp.MustCompile(`(?s)<style[^>]*>(.*?)</style>`)
|
||||
matches := styleRE.FindAllStringSubmatch(file, -1)
|
||||
|
||||
for _, m := range matches {
|
||||
if len(m) < 2 {
|
||||
continue
|
||||
}
|
||||
// Check the style content the same way we check .css files.
|
||||
// Write to a temp approach: just run the same checks on the content.
|
||||
lines := strings.Split(m[1], "\n")
|
||||
inBlock := 0
|
||||
for i, line := range lines {
|
||||
for _, ch := range line {
|
||||
if ch == '{' {
|
||||
inBlock++
|
||||
}
|
||||
if ch == '}' {
|
||||
inBlock--
|
||||
}
|
||||
}
|
||||
if inBlock <= 0 && orphanLine.MatchString(line) {
|
||||
findings = append(findings, gate.Finding{
|
||||
Level: "error",
|
||||
File: relPath,
|
||||
Line: i + 1,
|
||||
Message: fmt.Sprintf("orphaned CSS in <style> block: %s", strings.TrimSpace(line)),
|
||||
})
|
||||
}
|
||||
if badCalc.MatchString(line) {
|
||||
findings = append(findings, gate.Finding{
|
||||
Level: "error",
|
||||
File: relPath,
|
||||
Line: i + 1,
|
||||
Message: "invalid calc() in <style> block: minmax() is not valid in calc()",
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check for viewport meta tag — required for responsive.
|
||||
if !strings.Contains(file, `name="viewport"`) {
|
||||
findings = append(findings, gate.Finding{
|
||||
Level: "warn",
|
||||
File: relPath,
|
||||
Message: "missing viewport meta tag — responsive layouts won't work on mobile",
|
||||
})
|
||||
}
|
||||
|
||||
return findings
|
||||
}
|
||||
@@ -1,193 +0,0 @@
|
||||
// html-gate validates HTML files in the worktree for structural issues.
|
||||
// It catches unclosed tags, mismatched tag pairs, orphaned closing tags,
|
||||
// and missing closing divs that break grid layouts.
|
||||
//
|
||||
// Exports: func Gate() gate.Finding
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/gate"
|
||||
)
|
||||
|
||||
// selfClosing are HTML elements that never have a closing tag.
|
||||
var selfClosing = map[string]bool{
|
||||
"area": true, "base": true, "br": true, "col": true,
|
||||
"embed": true, "hr": true, "img": true, "input": true,
|
||||
"link": true, "meta": true, "param": true, "source": true,
|
||||
"track": true, "wbr": true,
|
||||
}
|
||||
|
||||
// tagRE matches HTML tags: <tagname ...> or </tagname>
|
||||
var tagRE = regexp.MustCompile(`</?([a-zA-Z][a-zA-Z0-9]*)\b[^>]*/?>`)
|
||||
|
||||
// divCloseRE matches closing </div> tags — used to detect structural
|
||||
// problems that break CSS grid layouts.
|
||||
var divOpenRE = regexp.MustCompile(`<div\b[^>]*>`)
|
||||
|
||||
// Gate validates HTML files in the worktree. Called during the gate
|
||||
// stage after Go checks pass.
|
||||
func Gate(ctx gate.Context) []gate.Finding {
|
||||
var findings []gate.Finding
|
||||
|
||||
for _, f := range ctx.ChangedFiles {
|
||||
ext := strings.ToLower(filepath.Ext(f))
|
||||
if ext != ".html" && ext != ".htm" {
|
||||
continue
|
||||
}
|
||||
findings = append(findings, checkHTML(filepath.Join(ctx.Dir, f), f)...)
|
||||
}
|
||||
|
||||
return findings
|
||||
}
|
||||
|
||||
func checkHTML(path, relPath string) []gate.Finding {
|
||||
var findings []gate.Finding
|
||||
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return []gate.Finding{{
|
||||
Level: "warn",
|
||||
File: relPath,
|
||||
Message: fmt.Sprintf("cannot read HTML file: %v", err),
|
||||
}}
|
||||
}
|
||||
|
||||
file := string(data)
|
||||
lines := strings.Split(file, "\n")
|
||||
|
||||
// Tag stack for mismatch detection.
|
||||
type tagEntry struct {
|
||||
name string
|
||||
line int
|
||||
}
|
||||
var stack []tagEntry
|
||||
|
||||
tags := tagRE.FindAllStringSubmatch(file, -1)
|
||||
tagIndexes := tagRE.FindAllStringIndex(file, -1)
|
||||
|
||||
for i, match := range tags {
|
||||
if len(match) < 2 {
|
||||
continue
|
||||
}
|
||||
fullTag := match[0]
|
||||
name := strings.ToLower(match[1])
|
||||
|
||||
// Self-closing or void element: skip.
|
||||
if selfClosing[name] {
|
||||
continue
|
||||
}
|
||||
// Self-closing syntax: <br/> or <img ... />
|
||||
if strings.HasSuffix(strings.TrimRight(fullTag, " "), "/>") {
|
||||
continue
|
||||
}
|
||||
|
||||
// Find the line number for this tag.
|
||||
offset := tagIndexes[i][0]
|
||||
lineNum := lineForOffset(lines, offset)
|
||||
|
||||
isClosing := strings.HasPrefix(fullTag, "</")
|
||||
|
||||
if isClosing {
|
||||
// Closing tag with empty stack: orphaned close.
|
||||
if len(stack) == 0 {
|
||||
findings = append(findings, gate.Finding{
|
||||
Level: "error",
|
||||
File: relPath,
|
||||
Line: lineNum,
|
||||
Message: fmt.Sprintf("orphaned closing tag </%s> (no matching open tag)", name),
|
||||
})
|
||||
continue
|
||||
}
|
||||
top := stack[len(stack)-1]
|
||||
if top.name != name {
|
||||
findings = append(findings, gate.Finding{
|
||||
Level: "error",
|
||||
File: relPath,
|
||||
Line: lineNum,
|
||||
Message: fmt.Sprintf("mismatched tag: </%s> closes <%s> (opened line %d)", name, top.name, top.line),
|
||||
})
|
||||
// Pop until we find the match or exhaust the stack.
|
||||
for len(stack) > 0 && stack[len(stack)-1].name != name {
|
||||
stack = stack[:len(stack)-1]
|
||||
}
|
||||
if len(stack) > 0 {
|
||||
stack = stack[:len(stack)-1]
|
||||
}
|
||||
continue
|
||||
}
|
||||
// Matching close: pop.
|
||||
stack = stack[:len(stack)-1]
|
||||
} else {
|
||||
// Opening tag: push.
|
||||
stack = append(stack, tagEntry{name: name, line: lineNum})
|
||||
}
|
||||
}
|
||||
|
||||
// Unclosed tags at EOF.
|
||||
for _, entry := range stack {
|
||||
findings = append(findings, gate.Finding{
|
||||
Level: "error",
|
||||
File: relPath,
|
||||
Line: entry.line,
|
||||
Message: fmt.Sprintf("unclosed <%s> tag (opened line %d)", entry.name, entry.line),
|
||||
})
|
||||
}
|
||||
|
||||
// Structural check: count <div> and </div> separately.
|
||||
// A mismatch here breaks grid layouts silently.
|
||||
divOpens := len(divOpenRE.FindAllString(file, -1))
|
||||
divCloses := strings.Count(file, "</div>")
|
||||
if divOpens != divCloses {
|
||||
findings = append(findings, gate.Finding{
|
||||
Level: "error",
|
||||
File: relPath,
|
||||
Message: fmt.Sprintf("mismatched <div> tags: %d opens, %d closes", divOpens, divCloses),
|
||||
})
|
||||
}
|
||||
|
||||
// Check for common anti-patterns.
|
||||
for i, line := range lines {
|
||||
trimmed := strings.TrimSpace(line)
|
||||
|
||||
// Empty href (navigates to self — usually a mistake).
|
||||
if strings.Contains(trimmed, `href=""`) {
|
||||
findings = append(findings, gate.Finding{
|
||||
Level: "warn",
|
||||
File: relPath,
|
||||
Line: i + 1,
|
||||
Message: "empty href=\"\" attribute (navigates to self — use \"#\" or remove the link)",
|
||||
})
|
||||
}
|
||||
|
||||
// Inline style with !important (blocks responsive overrides).
|
||||
if strings.Contains(trimmed, "!important") && strings.Contains(trimmed, "style=") {
|
||||
findings = append(findings, gate.Finding{
|
||||
Level: "warn",
|
||||
File: relPath,
|
||||
Line: i + 1,
|
||||
Message: "!important in inline style — blocks responsive overrides from CSS media queries",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return findings
|
||||
}
|
||||
|
||||
// lineForOffset returns the 1-based line number for a byte offset.
|
||||
func lineForOffset(lines []string, offset int) int {
|
||||
pos := 0
|
||||
for i, line := range lines {
|
||||
next := pos + len(line) + 1 // +1 for newline
|
||||
if offset < next {
|
||||
return i + 1
|
||||
}
|
||||
pos = next
|
||||
}
|
||||
return len(lines)
|
||||
}
|
||||
Reference in new issue
Block a user