Switch to find-unused-sass-variables for finding unused Sass variables.

This commit is contained in:
XhmikosR 2018-09-17 09:57:07 +03:00
parent bf4dab72df
commit 47107d4647
3 changed files with 76 additions and 81 deletions

View File

@ -1,80 +0,0 @@
#!/usr/bin/env node
/*!
* Script to find unused Sass variables.
* Copyright 2017-2018 The Bootstrap Authors
* Copyright 2017-2018 Twitter, Inc.
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
*/
const fs = require('fs')
const path = require('path')
const glob = require('glob')
// Blame TC39... https://github.com/benjamingr/RegExp.escape/issues/37
function regExpQuote(str) {
return str.replace(/[-\\^$*+?.()|[\]{}]/g, '\\$&')
}
let globalSuccess = true
function findUnusedVars(dir) {
if (!(fs.existsSync(dir) && fs.statSync(dir).isDirectory())) {
console.log(`"${dir}": Not a valid directory!`)
process.exit(1)
}
console.log(`Finding unused variables in "${dir}"...`)
// A variable to handle success/failure message in this function
let unusedVarsFound = false
// Array of all Sass files' content
const sassFiles = glob.sync(path.join(dir, '**/*.scss'))
// String of all Sass files' content
let sassFilesString = ''
sassFiles.forEach((file) => {
sassFilesString += fs.readFileSync(file, 'utf8')
})
// Array of all Sass variables
const variables = sassFilesString.match(/(^\$[a-zA-Z0-9_-]+[^:])/gm)
console.log(`Found ${variables.length} total variables.`)
// Loop through each variable
variables.forEach((variable) => {
const re = new RegExp(regExpQuote(variable), 'g')
const count = (sassFilesString.match(re) || []).length
if (count === 1) {
console.log(`Variable "${variable}" is not being used.`)
unusedVarsFound = true
globalSuccess = false
}
})
if (unusedVarsFound === false) {
console.log(`No unused variables found in "${dir}".`)
}
}
function main(args) {
if (args.length < 1) {
console.log('Wrong arguments!')
console.log('Usage: lint-vars.js folder [, folder2...]')
process.exit(1)
}
args.forEach((arg) => {
findUnusedVars(arg)
})
if (globalSuccess === false) {
process.exit(1)
}
}
// The first and second args are: path/to/node script.js
main(process.argv.slice(2))

74
package-lock.json generated
View File

@ -2044,6 +2044,12 @@
"restore-cursor": "^2.0.0"
}
},
"cli-spinners": {
"version": "1.3.1",
"resolved": "https://registry.npmjs.org/cli-spinners/-/cli-spinners-1.3.1.tgz",
"integrity": "sha512-1QL4544moEsDVH9T/l6Cemov/37iv1RtoKf7NJ04A60+4MREXNfx/QvavbH6QoGdsD4N4Mwy49cmaINR/o2mdg==",
"dev": true
},
"cli-table": {
"version": "0.3.1",
"resolved": "https://registry.npmjs.org/cli-table/-/cli-table-0.3.1.tgz",
@ -2087,6 +2093,12 @@
}
}
},
"clone": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/clone/-/clone-1.0.4.tgz",
"integrity": "sha1-2jCcwmPfFZlMaIypAheco8fNfH4=",
"dev": true
},
"clone-regexp": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/clone-regexp/-/clone-regexp-1.0.1.tgz",
@ -2481,6 +2493,15 @@
"os-name": "~1.0.3"
}
},
"defaults": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/defaults/-/defaults-1.0.3.tgz",
"integrity": "sha1-xlYFHpgX2f8I7YgUd/P+QBnz730=",
"dev": true,
"requires": {
"clone": "^1.0.2"
}
},
"define-properties": {
"version": "1.1.3",
"resolved": "https://registry.npmjs.org/define-properties/-/define-properties-1.1.3.tgz",
@ -3584,6 +3605,19 @@
}
}
},
"find-unused-sass-variables": {
"version": "0.2.1",
"resolved": "https://registry.npmjs.org/find-unused-sass-variables/-/find-unused-sass-variables-0.2.1.tgz",
"integrity": "sha512-AluOKUZHVERMDSjv6SUJ+k/ad6wgpqFsU6vZOTeGk+wZiE7K77I679qyVJP8pw9gr8zHiSdFRiqnRVpXNMyW1Q==",
"dev": true,
"requires": {
"chalk": "^2.4.1",
"glob": "^7.1.3",
"ora": "^3.0.0",
"postcss": "^7.0.2",
"postcss-scss": "^2.0.0"
}
},
"find-up": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/find-up/-/find-up-3.0.0.tgz",
@ -7397,6 +7431,37 @@
"wordwrap": "~1.0.0"
}
},
"ora": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/ora/-/ora-3.0.0.tgz",
"integrity": "sha512-LBS97LFe2RV6GJmXBi6OKcETKyklHNMV0xw7BtsVn2MlsgsydyZetSCbCANr+PFLmDyv4KV88nn0eCKza665Mg==",
"dev": true,
"requires": {
"chalk": "^2.3.1",
"cli-cursor": "^2.1.0",
"cli-spinners": "^1.1.0",
"log-symbols": "^2.2.0",
"strip-ansi": "^4.0.0",
"wcwidth": "^1.0.1"
},
"dependencies": {
"ansi-regex": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-3.0.0.tgz",
"integrity": "sha1-7QMXwyIGT3lGbAKWa922Bas32Zg=",
"dev": true
},
"strip-ansi": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-4.0.0.tgz",
"integrity": "sha1-qEeQIusaw2iocTibY1JixQXuNo8=",
"dev": true,
"requires": {
"ansi-regex": "^3.0.0"
}
}
}
},
"os-homedir": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/os-homedir/-/os-homedir-1.0.2.tgz",
@ -11481,6 +11546,15 @@
}
}
},
"wcwidth": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/wcwidth/-/wcwidth-1.0.1.tgz",
"integrity": "sha1-8LDc+RW8X/FSivrbLA4XtTLaL+g=",
"dev": true,
"requires": {
"defaults": "^1.0.3"
}
},
"which": {
"version": "1.3.1",
"resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz",

View File

@ -30,7 +30,7 @@
"css-copy": "shx mkdir -p site/docs/4.1/dist/ && shx cp -r dist/css/ site/docs/4.1/dist/",
"css-lint": "stylelint --syntax scss \"scss/**/*.scss\"",
"css-lint-docs": "stylelint --syntax scss \"site/docs/4.1/assets/scss/*.scss\" && stylelint \"site/docs/**/*.css\"",
"css-lint-vars": "node build/lint-vars.js scss/ site/docs/",
"css-lint-vars": "fusv scss/ site/docs/",
"css-prefix": "postcss --config build/postcss.config.js --replace \"dist/css/*.css\" \"!dist/css/*.min.css\"",
"css-prefix-docs": "postcss --config build/postcss.config.js --replace \"site/docs/**/*.css\" \"site/docs/**/*.css\"",
"css-minify": "cleancss --level 1 --format breaksWith=lf --source-map --source-map-inline-sources --output dist/css/bootstrap.min.css dist/css/bootstrap.css && cleancss --level 1 --format breaksWith=lf --source-map --source-map-inline-sources --output dist/css/bootstrap-grid.min.css dist/css/bootstrap-grid.css && cleancss --level 1 --format breaksWith=lf --source-map --source-map-inline-sources --output dist/css/bootstrap-reboot.min.css dist/css/bootstrap-reboot.css",
@ -110,6 +110,7 @@
"coveralls": "^3.0.2",
"cross-env": "^5.2.0",
"eslint": "^5.5.0",
"find-unused-sass-variables": "^0.2.1",
"glob": "^7.1.3",
"htmllint-cli": "^0.0.7",
"http-server": "^0.11.1",