1
0
mirror of https://github.com/simple-icons/simple-icons.git synced 2024-12-16 01:10:30 +02:00
simple-icons/scripts/lint/ourlint.js

146 lines
4.4 KiB
JavaScript
Raw Normal View History

2024-03-24 19:38:18 +02:00
#!/usr/bin/env node
/**
2024-06-06 14:40:35 +02:00
* @file
* Linters for the package that can't easily be implemented in the existing
* linters (e.g. jsonlint/svglint).
*/
2024-06-06 14:40:35 +02:00
/**
* @typedef {import("../../sdk.mjs").IconData} IconData
* @typedef {import("../../types.js").CustomLicense} CustomLicense
* @typedef {IconData[]} IconsData
*/
import process from 'node:process';
import fakeDiff from 'fake-diff';
2024-03-24 19:38:18 +02:00
import {collator, getIconsDataString, normalizeNewlines} from '../../sdk.mjs';
/**
* Contains our tests so they can be isolated from each other.
2024-06-06 14:40:35 +02:00
* @type {{[k: string]: (arg0: {icons: IconsData}, arg1: string) => string | undefined}}
*/
const TESTS = {
2024-06-06 14:40:35 +02:00
/**
* Tests whether our icons are in alphabetical order
* @param {{icons: IconsData}} data Icons data
* @returns {string|undefined} Error message or undefined
*/
2024-03-24 19:38:18 +02:00
alphabetical(data) {
2024-06-06 14:40:35 +02:00
/**
* Collects invalid alphabet ordered icons
* @param {IconData[]} invalidEntries Invalid icons reference
* @param {IconData} icon Icon to check
* @param {number} index Index of the icon
* @param {IconData[]} array Array of icons
* @returns {IconData[]} Invalid icons
*/
const collector = (invalidEntries, icon, index, array) => {
if (index > 0) {
2024-03-24 19:38:18 +02:00
const previous = array[index - 1];
const comparison = collator.compare(icon.title, previous.title);
if (comparison < 0) {
invalidEntries.push(icon);
2024-03-24 19:38:18 +02:00
} else if (
comparison === 0 &&
previous.slug &&
(!icon.slug || collator.compare(icon.slug, previous.slug) < 0)
) {
invalidEntries.push(icon);
}
}
2024-03-24 19:38:18 +02:00
return invalidEntries;
};
2024-03-24 19:38:18 +02:00
2024-06-06 14:40:35 +02:00
/**
* Format an icon for display in the error message
* @param {IconData} icon Icon to format
* @returns {string} Formatted icon
*/
const format = (icon) => {
if (icon.slug) {
return `${icon.title} (${icon.slug})`;
}
2024-03-24 19:38:18 +02:00
return icon.title;
};
2024-03-24 19:38:18 +02:00
// eslint-disable-next-line unicorn/no-array-reduce, unicorn/no-array-callback-reference
const invalids = data.icons.reduce(collector, []);
2024-03-24 19:38:18 +02:00
if (invalids.length > 0) {
return `Some icons aren't in alphabetical order:
${invalids.map((icon) => format(icon)).join(', ')}`;
}
},
/* Check the formatting of the data file */
2024-03-24 19:38:18 +02:00
prettified(data, dataString) {
const normalizedDataString = normalizeNewlines(dataString);
const dataPretty = `${JSON.stringify(data, null, 4)}\n`;
if (normalizedDataString !== dataPretty) {
const dataDiff = fakeDiff(normalizedDataString, dataPretty);
return `Data file is formatted incorrectly:\n\n${dataDiff}`;
}
},
/* Check redundant trailing slash in URL */
2024-03-24 19:38:18 +02:00
checkUrl(data) {
2024-06-06 14:40:35 +02:00
/**
* Check if an URL has a redundant trailing slash.
* @param {string} url URL to check
* @returns {boolean} Whether the URL has a redundant trailing slash
*/
const hasRedundantTrailingSlash = (url) => {
2024-03-24 19:38:18 +02:00
const {origin} = new global.URL(url);
return /^\/+$/.test(url.replace(origin, ''));
};
const allUrlFields = [
...new Set(
data.icons.flatMap((icon) => {
/** @type {string[]} */
const license =
icon.license !== undefined && Object.hasOwn(icon.license, 'url')
? [
// TODO: `hasOwn` is not currently supported by TS.
// See https://github.com/microsoft/TypeScript/issues/44253
/** @type {string} */
// @ts-ignore
icon.license.url,
]
: [];
const guidelines = icon.guidelines ? [icon.guidelines] : [];
return [icon.source, ...guidelines, ...license];
}),
),
];
const invalidUrls = allUrlFields.filter((url) =>
hasRedundantTrailingSlash(url),
);
if (invalidUrls.length > 0) {
return `Some URLs have a redundant trailing slash:\n\n${invalidUrls.join(
'\n',
)}`;
}
},
};
2024-06-06 14:40:35 +02:00
const iconsDataString = await getIconsDataString();
const iconsData = JSON.parse(iconsDataString);
const errors = (
2024-06-06 14:40:35 +02:00
await Promise.all(
Object.values(TESTS).map((test) => test(iconsData, iconsDataString)),
)
2024-03-24 19:38:18 +02:00
)
// eslint-disable-next-line unicorn/no-await-expression-member
.filter(Boolean);
if (errors.length > 0) {
2024-03-24 19:38:18 +02:00
for (const error of errors) console.error(`\u001B[31m${error}\u001B[0m`);
process.exit(1);
}