1
0
mirror of https://github.com/simple-icons/simple-icons.git synced 2024-11-16 00:59:07 +02:00

Update svglint ignore list, add ignore list updater (#3433)

This updates the ignore list as per #3250 and embeds code for updating
the ignore file when the environment variable `SI_UPDATE_IGNORE=true`
is set and the linter is run like so `SI_UPDATE_IGNORE=true npm run svglint`
-- running this command will display all remaining linting failures and
use these to update the ignore file.

This also:

* Normalises the top-level object key in the ignore file to match each
  linter name
* Sorts the ignore output on top-level key and icon name value to
  make output consistent
* Speeds up icon center checking if ignored
* Ignores icons in size linter if icon is otherwise ignored
This commit is contained in:
David Beitey 2020-10-17 21:01:56 +10:00 committed by GitHub
parent 79a5a08dc2
commit c290479a78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 317 additions and 146 deletions

File diff suppressed because one or more lines are too long

View File

@ -1,3 +1,5 @@
const fs = require('fs');
const data = require("./_data/simple-icons.json"); const data = require("./_data/simple-icons.json");
const { htmlFriendlyToTitle } = require("./scripts/utils.js"); const { htmlFriendlyToTitle } = require("./scripts/utils.js");
const getBounds = require("svg-path-bounding-box"); const getBounds = require("svg-path-bounding-box");
@ -8,7 +10,58 @@ const svgRegexp = /^<svg( [^\s]*=".*"){3}><title>.*<\/title><path d=".*"\/><\/sv
const iconSize = 24; const iconSize = 24;
const iconFloatPrecision = 3; const iconFloatPrecision = 3;
const iconTolerance = 0.001; const iconTolerance = 0.001;
const iconIgnored = require("./.svglint-ignored.json");
// set env SI_UPDATE_IGNORE to recreate the ignore file
const updateIgnoreFile = process.env.SI_UPDATE_IGNORE === 'true'
const ignoreFile = "./.svglint-ignored.json";
const iconIgnored = !updateIgnoreFile ? require(ignoreFile) : {};
function sortObjectByKey(obj) {
return Object
.keys(obj)
.sort()
.reduce((r, k) => Object.assign(r, { [k]: obj[k] }), {});
}
function sortObjectByValue(obj) {
return Object
.keys(obj)
.sort((a, b) => ('' + obj[a]).localeCompare(obj[b]))
.reduce((r, k) => Object.assign(r, { [k]: obj[k] }), {});
}
if (updateIgnoreFile) {
process.on('exit', () => {
// ensure object output order is consistent due to async svglint processing
const sorted = sortObjectByKey(iconIgnored)
for (const linterName in sorted) {
sorted[linterName] = sortObjectByValue(sorted[linterName])
}
fs.writeFileSync(
ignoreFile,
JSON.stringify(sorted, null, 2) + '\n',
{flag: 'w'}
);
});
}
function isIgnored(linterName, path) {
return iconIgnored[linterName]
.map(ignored => ignored.hasOwnProperty(path))
.some(v => v === true);
}
function ignoreIcon(linterName, path, $) {
if (!iconIgnored[linterName]) {
iconIgnored[linterName] = {};
}
const title = $.find("title").text().replace(/(.*) icon/, '$1');
const iconName = htmlFriendlyToTitle(title);
iconIgnored[linterName][path] = iconName;
}
module.exports = { module.exports = {
rules: { rules: {
@ -23,7 +76,6 @@ module.exports = {
"role": "img", "role": "img",
"viewBox": `0 0 ${iconSize} ${iconSize}`, "viewBox": `0 0 ${iconSize} ${iconSize}`,
"xmlns": "http://www.w3.org/2000/svg", "xmlns": "http://www.w3.org/2000/svg",
"rule::selector": "svg", "rule::selector": "svg",
"rule::whitelist": true, "rule::whitelist": true,
}, },
@ -59,7 +111,7 @@ module.exports = {
reporter.name = "icon-size"; reporter.name = "icon-size";
const iconPath = $.find("path").attr("d"); const iconPath = $.find("path").attr("d");
if (iconIgnored.size.hasOwnProperty(iconPath)) { if (!updateIgnoreFile && isIgnored(reporter.name, iconPath)) {
return; return;
} }
@ -69,8 +121,14 @@ module.exports = {
if (width === 0 && height === 0) { if (width === 0 && height === 0) {
reporter.error("Path bounds were reported as 0 x 0; check if the path is valid"); reporter.error("Path bounds were reported as 0 x 0; check if the path is valid");
if (updateIgnoreFile) {
ignoreIcon(reporter.name, iconPath, $);
}
} else if (width !== iconSize && height !== iconSize) { } else if (width !== iconSize && height !== iconSize) {
reporter.error(`Size of <path> must be exactly ${iconSize} in one dimension; the size is currently ${width} x ${height}`); reporter.error(`Size of <path> must be exactly ${iconSize} in one dimension; the size is currently ${width} x ${height}`);
if (updateIgnoreFile) {
ignoreIcon(reporter.name, iconPath, $);
}
} }
}, },
function(reporter, $, ast) { function(reporter, $, ast) {
@ -83,16 +141,13 @@ module.exports = {
}, },
function(reporter, $, ast) { function(reporter, $, ast) {
reporter.name = "icon-centered"; reporter.name = "icon-centered";
const iconPath = $.find("path").attr("d");
const bounds = getBounds(iconPath);
if ( const iconPath = $.find("path").attr("d");
iconIgnored.size.hasOwnProperty(iconPath) || if (!updateIgnoreFile && isIgnored(reporter.name, iconPath)) {
iconIgnored.center.hasOwnProperty(iconPath)
) {
return; return;
} }
const bounds = getBounds(iconPath);
const targetCenter = iconSize / 2; const targetCenter = iconSize / 2;
const centerX = +((bounds.minX + bounds.maxX) / 2).toFixed(iconFloatPrecision); const centerX = +((bounds.minX + bounds.maxX) / 2).toFixed(iconFloatPrecision);
const devianceX = centerX - targetCenter; const devianceX = centerX - targetCenter;
@ -104,6 +159,9 @@ module.exports = {
Math.abs(devianceY) > iconTolerance Math.abs(devianceY) > iconTolerance
) { ) {
reporter.error(`<path> must be centered at (${targetCenter}, ${targetCenter}); the center is currently (${centerX}, ${centerY})`); reporter.error(`<path> must be centered at (${targetCenter}, ${targetCenter}); the center is currently (${centerX}, ${centerY})`);
if (updateIgnoreFile) {
ignoreIcon(reporter.name, iconPath, $);
}
} }
} }
] ]