Skip to content

Commit 7629567

Browse files
authored
Replace lint-vars.sh with a Node.js script. (#24860)
Also, include it in the `css` npm script since it's instant.
1 parent 671bb27 commit 7629567

File tree

4 files changed

+94
-37
lines changed

4 files changed

+94
-37
lines changed

build/lint-vars.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env node
2+
3+
/*!
4+
* Script to find unused Sass variables.
5+
*
6+
* Copyright 2017 The Bootstrap Authors
7+
* Copyright 2017 Twitter, Inc.
8+
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
9+
*/
10+
11+
'use strict'
12+
13+
const fs = require('fs')
14+
const path = require('path')
15+
const glob = require('glob')
16+
17+
// Blame TC39... https://github.com/benjamingr/RegExp.escape/issues/37
18+
function regExpQuote(str) {
19+
return str.replace(/[-\\^$*+?.()|[\]{}]/g, '\\$&')
20+
}
21+
22+
let globalSuccess = true
23+
24+
function findUnusedVars(dir) {
25+
if (!(fs.existsSync(dir) && fs.statSync(dir).isDirectory())) {
26+
console.log(`"${dir}": Not a valid directory!`)
27+
process.exit(1)
28+
}
29+
30+
console.log(`Finding unused variables in "${dir}"...`)
31+
32+
// A variable to handle success/failure message in this function
33+
let unusedVarsFound = false
34+
35+
// Array of all Sass files' content
36+
const sassFiles = glob.sync(path.join(dir, '**/*.scss'))
37+
// String of all Sass files' content
38+
let sassFilesString = ''
39+
40+
sassFiles.forEach((file) => {
41+
sassFilesString += fs.readFileSync(file, 'utf8')
42+
})
43+
44+
// Array of all Sass variables
45+
const variables = sassFilesString.match(/(^\$[a-zA-Z0-9_-]+[^:])/gm)
46+
47+
console.log(`There's a total of ${variables.length} variables.`)
48+
49+
// Loop through each variable
50+
variables.forEach((variable) => {
51+
const re = new RegExp(regExpQuote(variable), 'g')
52+
const count = (sassFilesString.match(re) || []).length
53+
54+
if (count === 1) {
55+
console.log(`Variable "${variable}" is only used once!`)
56+
unusedVarsFound = true
57+
globalSuccess = false
58+
}
59+
})
60+
61+
if (unusedVarsFound === false) {
62+
console.log(`No unused variables found in "${dir}".`)
63+
}
64+
}
65+
66+
function main(args) {
67+
if (args.length < 1) {
68+
console.log('Wrong arguments!')
69+
console.log('Usage: lint-vars.js folder [, folder2...]')
70+
process.exit(1)
71+
}
72+
73+
args.forEach((arg) => {
74+
findUnusedVars(arg)
75+
})
76+
77+
if (globalSuccess === false) {
78+
process.exit(1)
79+
}
80+
}
81+
82+
// The first and second args are: path/to/node script.js
83+
main(process.argv.slice(2))

build/lint-vars.sh

Lines changed: 0 additions & 28 deletions
This file was deleted.

package-lock.json

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"css-compile-docs": "node-sass --output-style expanded --source-map true --source-map-contents true --precision 6 assets/scss/docs.scss assets/css/docs.min.css",
2626
"css-lint": "stylelint --config build/.stylelintrc --syntax scss \"scss/**/*.scss\"",
2727
"css-lint-docs": "stylelint --config build/.stylelintrc --syntax scss \"assets/scss/*.scss\" && stylelint --config docs/4.0/examples/.stylelintrc \"docs/**/*.css\"",
28+
"css-lint-vars": "node build/lint-vars.js scss/ assets/scss/",
2829
"css-prefix": "postcss --config build/postcss.config.js --replace \"dist/css/*.css\" \"!dist/css/*.min.css\"",
2930
"css-prefix-docs": "postcss --config build/postcss.config.js --replace \"assets/css/docs.min.css\" \"docs/**/*.css\"",
3031
"css-minify": "cleancss --level 1 --source-map --source-map-inline-sources --output dist/css/bootstrap.min.css dist/css/bootstrap.css && cleancss --level 1 --source-map --source-map-inline-sources --output dist/css/bootstrap-grid.min.css dist/css/bootstrap-grid.css && cleancss --level 1 --source-map --source-map-inline-sources --output dist/css/bootstrap-reboot.min.css dist/css/bootstrap-reboot.css",
@@ -94,6 +95,7 @@
9495
"cross-env": "^5.1.1",
9596
"eslint": "^4.11.0",
9697
"eslint-plugin-compat": "^2.1.0",
98+
"glob": "^7.1.2",
9799
"htmllint-cli": "^0.0.6",
98100
"jsunitsaucelabs": "^1.3.0",
99101
"karma": "^1.7.1",

0 commit comments

Comments
 (0)