1
1
mirror of https://github.com/primer/css.git synced 2024-11-30 01:04:04 +03:00

clean up doc tests

This commit is contained in:
Shawn Allen 2017-09-11 15:48:20 -07:00
parent 4d603c9ea5
commit 7a7222893a

View File

@ -1,9 +1,11 @@
const test = require("ava") const test = require("ava")
const css = require(process.env.PWD) const css = require(process.cwd())
const fs = require("fs") const fs = require("fs")
const glob = require("glob") const glob = require("glob")
var selectors, classnames = null let selectors
let classnames
const classRegex = /class="([^"]+)"/ig const classRegex = /class="([^"]+)"/ig
// Find unique selectors from the cssstats selector list // Find unique selectors from the cssstats selector list
@ -11,7 +13,8 @@ function uniqueSelectors(s) {
s = s.map(s => { s = s.map(s => {
// split multi-selectors into last class used .foo .bar .baz // split multi-selectors into last class used .foo .bar .baz
return s.split(" ").pop() return s.split(" ").pop()
}).filter(s => { })
.filter(s => {
// remove any selector that aren't just regular classnames eg. ::hover [type] // remove any selector that aren't just regular classnames eg. ::hover [type]
return s.match(/^\.[a-z\-_]+$/ig) return s.match(/^\.[a-z\-_]+$/ig)
}) })
@ -22,37 +25,37 @@ function uniqueSelectors(s) {
// From the given glob sources array, read the files and return found classnames // From the given glob sources array, read the files and return found classnames
function documentedClassnames(sources) { function documentedClassnames(sources) {
var cn = [] const classes = []
sources.forEach( f => { sources.forEach(f => {
glob.sync(f).forEach( g => { glob.sync(f).forEach(g => {
var match = null var match = null
// While we match a classRegex in the source // While we match a classRegex in the source
while ((match = classRegex.exec(fs.readFileSync(g, "utf8"))) != null) { while ((match = classRegex.exec(fs.readFileSync(g, "utf8"))) != null) {
// Get the matched classnames "..." and split by space into classes // Get the matched classnames "..." and split by space into classes
cn = cn.concat(match[1].split(" ")) classes.push(...match[1].split(" "))
} }
}) })
}) })
// return only the unique classnames // return only the unique classnames
return [...new Set(cn)] return Array.from(new Set(classes))
} }
// Before all the tests get the selectors and classnames // Before all the tests get the selectors and classnames
test.before(t => { test.before(t => {
selectors = uniqueSelectors(css.cssstats.selectors.values) selectors = uniqueSelectors(css.cssstats.selectors.values)
classnames = documentedClassnames([ classnames = documentedClassnames([
'docs/*.md', "docs/*.md",
'README.md' "README.md"
]) ])
}) })
test("Every selector class is documented", t => { test("Every selector class is documented", t => {
var undocumented = [] const undocumented = []
selectors.forEach( selector => { selectors.forEach(selector => {
if (!classnames.includes(selector.replace(".", ""))) { if (!classnames.includes(selector.replace(/^\./, ""))) {
undocumented.push(selector) undocumented.push(selector)
} }
}) })