From 45583efadde5ef463e27ee8261bbb72087a15717 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Mond=C3=A9jar?= Date: Sun, 13 Dec 2020 21:17:41 +0100 Subject: [PATCH] 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 --- .editorconfig | 1 - package.json | 1 + scripts/lint.js | 29 ++++++++++++++++++++++++++--- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/.editorconfig b/.editorconfig index 48c0b4e29..5715afa79 100644 --- a/.editorconfig +++ b/.editorconfig @@ -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 diff --git a/package.json b/package.json index 5b38aff79..400ea2e83 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/scripts/lint.js b/scripts/lint.js index f8961634c..a9833b4a1 100644 --- a/scripts/lint.js +++ b/scripts/lint.js @@ -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}`; + } } };