Add lint to check JSON data file prettification (#4320)

* Add test for check JSON data file prettification

* Move prettification test to out linting script

* Add other newline in error message to improve readability

* Invert the diff to show the solution to the user

* Add 'jest-diff' as direct dependency

* Remove annotations and colors

* Replace CRLF newlines with LFs in JSON prettification lint

* Restore colors in JSON prettification lint
This commit is contained in:
Álvaro Mondéjar 2020-12-13 21:17:41 +01:00 committed by GitHub
parent bf026d737d
commit 45583efadd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 4 deletions

View file

@ -19,6 +19,5 @@ insert_final_newline=false
trim_trailing_whitespace=false # Templates with trailing whitespace are more usable
[_data/simple-icons.json]
indent_style=space
indent_size=4
trim_trailing_whitespace=true

View file

@ -20,6 +20,7 @@
"devDependencies": {
"editorconfig-checker": "3.3.0",
"jest": "26.6.3",
"jest-diff": "26.6.2",
"jsonlint2": "1.7.1",
"npm-run-all": "4.1.5",
"svg-path-bbox": "0.1.5",

View file

@ -2,15 +2,22 @@
/**
* @fileoverview Lints for the package that can't be implemented in the existing linters (e.g. jsonlint/svglint)
*/
const fs = require("fs");
const path = require("path");
const { icons } = require("../_data/simple-icons.json");
const { diffLinesUnified } = require("jest-diff");
const simpleIconsData = require("../_data/simple-icons.json");
const simpleIconsDataFile = path.resolve(
__dirname, "..", "_data", "simple-icons.json");
/**
* Contains our tests so they can be isolated from eachother; I don't think each test is worth its own file
* @type {{[k:string]: () => (string|undefined)}}
*/
const TESTS = {
/** Tests whether our icons are in alphabetical order */
/* Tests whether our icons are in alphabetical order */
alphabetical: function() {
const collector = (invalidEntries, icon, index, array) => {
if (index > 0) {
@ -22,11 +29,27 @@ const TESTS = {
return invalidEntries;
};
const invalids = icons.reduce(collector, []);
const invalids = simpleIconsData.icons.reduce(collector, []);
if (invalids.length) {
return `Some icons aren't in alphabetical order:
${invalids.map(icon => icon.title).join(", ")}`;
}
},
/* Check the prettification of the data file */
prettified: function() {
const simpleIconsDataString = fs.readFileSync(
simpleIconsDataFile, "utf8").replace(/\r\n/g, '\n');
const simpleIconsDataPretty = `${JSON.stringify(simpleIconsData, null, " ")}\n`;
if (simpleIconsDataString !== simpleIconsDataPretty) {
const dataDiff = diffLinesUnified(simpleIconsDataString.split("\n"),
simpleIconsDataPretty.split("\n"),
{
expand: false,
omitAnnotationLines: true
});
return `Data file is not prettified:\n\n${dataDiff}`;
}
}
};