Add lint for checking redundant slashes (#8692)

* Add lint for checking redundant slashes

* Update function names

* Better condition

* Fix typo
This commit is contained in:
LitoMore 2023-05-29 09:34:33 +08:00 committed by GitHub
parent 11b65e7da7
commit 7330e01f85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 746 additions and 718 deletions

File diff suppressed because it is too large Load Diff

View File

@ -5,6 +5,7 @@
* linters (e.g. jsonlint/svglint).
*/
import { URL } from 'node:url';
import fakeDiff from 'fake-diff';
import { getIconsDataString, normalizeNewlines, collator } from '../../sdk.mjs';
@ -55,6 +56,33 @@ const TESTS = {
return `Data file is formatted incorrectly:\n\n${dataDiff}`;
}
},
/* Check redundant trailing slash in URL */
checkUrl: (data) => {
const hasRedundantTrailingSlash = (url) => {
const origin = new URL(url).origin;
return /^\/+$/.test(url.replace(origin, ''));
};
const allUrlFields = [
...new Set(
data.icons
.map((icon) => [icon.source, icon.guidelines, icon.license?.url])
.flat()
.filter(Boolean),
),
];
const invalidUrls = allUrlFields.filter((url) =>
hasRedundantTrailingSlash(url),
);
if (invalidUrls.length > 0) {
return `Some URLs have a redundant trailing slash:\n\n${invalidUrls.join(
'\n',
)}`;
}
},
};
// execute all tests and log all errors