Skip to content

Commit

Permalink
Merge pull request #67 from primer/no-undefined-vars-update
Browse files Browse the repository at this point in the history
Cache variable definitions in no-undefined-vars rule
  • Loading branch information
colebemis authored Oct 2, 2020
2 parents 8446994 + 0faaf5b commit 4d7b154
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# 9.2.1

### :bug: Bug fixes

- Fix slow runtime by caching variable definitions in `primer/no-undefined-vars` rule
- Fix duplicate errors in `primer/no-undefined-vars` rule

# 9.2.0

### :rocket: Enhancements
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "stylelint-config-primer",
"version": "9.2.0",
"version": "9.2.1",
"description": "Sharable stylelint config used by GitHub's CSS",
"homepage": "http://primer.style/css/tools/linting",
"author": "GitHub, Inc.",
Expand Down
32 changes: 24 additions & 8 deletions plugins/no-undefined-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const fs = require('fs')
const stylelint = require('stylelint')
const matchAll = require('string.prototype.matchall')
const globby = require('globby')
const TapMap = require('tap-map')

const ruleName = 'primer/no-undefined-vars'
const messages = stylelint.utils.ruleMessages(ruleName, {
Expand All @@ -25,9 +26,18 @@ module.exports = stylelint.createPlugin(ruleName, (enabled, options = {}) => {
const log = verbose ? (...args) => console.warn(...args) : noop
const definedVariables = getDefinedVariables(files, log)

// Keep track of declarations we've already seen
const seen = new WeakMap()

return (root, result) => {
root.walkRules(rule => {
rule.walkDecls(decl => {
if (seen.has(decl)) {
return
} else {
seen.set(decl, true)
}

for (const [, variableName] of matchAll(decl.value, variableReferenceRegex)) {
if (!definedVariables.has(variableName)) {
stylelint.utils.report({
Expand All @@ -43,18 +53,24 @@ module.exports = stylelint.createPlugin(ruleName, (enabled, options = {}) => {
}
})

const cwd = process.cwd()
const cache = new TapMap()

function getDefinedVariables(files, log) {
const definedVariables = new Set()
const cacheKey = JSON.stringify({files, cwd})
return cache.tap(cacheKey, () => {
const definedVariables = new Set()

for (const file of globby.sync(files)) {
const css = fs.readFileSync(file, 'utf-8')
for (const [, variableName] of matchAll(css, variableDefinitionRegex)) {
log(`${variableName} defined in ${file}`)
definedVariables.add(variableName)
for (const file of globby.sync(files)) {
const css = fs.readFileSync(file, 'utf-8')
for (const [, variableName] of matchAll(css, variableDefinitionRegex)) {
log(`${variableName} defined in ${file}`)
definedVariables.add(variableName)
}
}
}

return definedVariables
return definedVariables
})
}

function noop() {}
Expand Down

0 comments on commit 4d7b154

Please sign in to comment.