Proposal: Check outlying precision while running linter (#3750)

* Check outlying precision while running linter

* Prevent reduce error on empty array

* Rebase on branch develop

* Add svgpath dep

* Improve decimal precision verification

* Check outlying precision while running linter

* Prevent reduce error on empty array

* Fix ignored file

* Fix reverted dep version

* Check decimal according to review proposal

* Pin svgpath version

* Use te maximum precision only

* Run linter again

* Fix uglify-js version

* Add supported engines to package.json

* Improve message

* Revert engines and use version check in linter

* Move function

* Update node version to 12 in Travis

* Add skip ignored
This commit is contained in:
Alexandre Paradis 2020-11-19 15:49:49 -05:00 committed by GitHub
parent 3ff22a7cf6
commit 9cee719fcb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 57 additions and 6 deletions

File diff suppressed because one or more lines are too long

View File

@ -3,12 +3,14 @@ const fs = require('fs');
const data = require("./_data/simple-icons.json");
const { htmlFriendlyToTitle } = require("./scripts/utils.js");
const getBounds = require("svg-path-bounding-box");
const parsePath = require("svgpath/lib/path_parse");
const titleRegexp = /(.+) icon$/;
const svgRegexp = /^<svg( [^\s]*=".*"){3}><title>.*<\/title><path d=".*"\/><\/svg>\r?\n?$/;
const iconSize = 24;
const iconFloatPrecision = 3;
const iconMaxFloatPrecision = 5;
const iconTolerance = 0.001;
// set env SI_UPDATE_IGNORE to recreate the ignore file
@ -30,6 +32,11 @@ function sortObjectByValue(obj) {
.reduce((r, k) => Object.assign(r, { [k]: obj[k] }), {});
}
if (Array.prototype.flat === undefined) {
console.error(`Minimum NodeJS v11.15.0 is required, but you are running ${process.version}.`);
process.exit(1);
}
if (updateIgnoreFile) {
process.on('exit', () => {
// ensure object output order is consistent due to async svglint processing
@ -129,6 +136,38 @@ module.exports = {
}
}
},
function(reporter, $, ast) {
reporter.name = "icon-precision";
const iconPath = $.find("path").attr("d");
if (!updateIgnoreFile && isIgnored(reporter.name, iconPath)) {
return;
}
const { segments } = parsePath(iconPath);
const segmentParts = segments.flat().filter((num) => (typeof num === 'number'));
const countDecimals = (num) => {
if (num && num % 1) {
let [base, op, trail] = num.toExponential().split(/e([+-])/);
let elen = parseInt(trail, 10);
let idx = base.indexOf('.');
return idx == -1 ? elen : base.length - idx - 1 + (op === '+' ? -elen : elen);
}
return 0;
};
const precisionArray = segmentParts.map(countDecimals);
const precisionMax = precisionArray && precisionArray.length > 0 ?
Math.max(...precisionArray) :
0;
if (precisionMax > iconMaxFloatPrecision) {
reporter.error(`Maximum precision should not be greater than ${iconMaxFloatPrecision}; it is currently ${precisionMax}`);
if (updateIgnoreFile) {
ignoreIcon(reporter.name, iconPath, $);
}
}
},
function(reporter, $, ast) {
reporter.name = "extraneous";

View File

@ -5,7 +5,7 @@ jobs:
- stage: "Test"
name: "Lint"
language: node_js
node_js: 10
node_js: 12
git:
depth: 1
script:
@ -27,7 +27,7 @@ jobs:
- jekyll build
- name: "Test package"
language: node_js
node_js: 10
node_js: 12
git:
depth: 1
script:
@ -51,7 +51,7 @@ jobs:
cleanup: false
- name: "NPM Package"
language: node_js
node_js: 10
node_js: 12
git:
depth: 1
if: branch = master

6
package-lock.json generated
View File

@ -7014,9 +7014,9 @@
}
},
"svgpath": {
"version": "2.2.3",
"resolved": "https://registry.npmjs.org/svgpath/-/svgpath-2.2.3.tgz",
"integrity": "sha512-xA0glXYpJ9SYT4JeMp3c0psbqdZsG1c0ywGvdJUPY2FKEgwJV7NgkeYuuQiOxMp+XsK9nCqjm3KDw0LkM1YLXw==",
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/svgpath/-/svgpath-2.3.0.tgz",
"integrity": "sha512-N/4UDu3Y2ICik0daMmFW1tplw0XPs1nVIEVYkTiQfj9/JQZeEtAKaSYwheCwje1I4pQ5r22fGpoaNIvGgsyJyg==",
"dev": true
},
"symbol-tree": {

View File

@ -25,6 +25,7 @@
"svg-path-bounding-box": "1.0.4",
"svglint": "1.0.6",
"svgo": "1.3.2",
"svgpath": "2.3.0",
"uglify-js": "3.11.6"
},
"scripts": {