From 4da4827bcbcdd1ef163f635d9b29416ff272b0bb Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Mon, 27 Nov 2023 12:48:14 -0500 Subject: Add JSDoc type annotations to project (rebased) --- dev/bin/generate-css-json.js | 1 + 1 file changed, 1 insertion(+) (limited to 'dev/bin') diff --git a/dev/bin/generate-css-json.js b/dev/bin/generate-css-json.js index 48b42c65..73c406e0 100644 --- a/dev/bin/generate-css-json.js +++ b/dev/bin/generate-css-json.js @@ -19,6 +19,7 @@ import fs from 'fs'; import {formatRulesJson, generateRules, getTargets} from '../generate-css-json.js'; +/** */ function main() { for (const {cssFile, overridesCssFile, outputPath} of getTargets()) { const json = formatRulesJson(generateRules(cssFile, overridesCssFile)); -- cgit v1.2.3 From e215656ce9b965360e540da93ebf5c381cbe4e41 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Wed, 29 Nov 2023 20:13:15 -0500 Subject: Update types --- .eslintrc.json | 1 - dev/bin/build.js | 64 ++++++++++--- dev/bin/dictionary-validate.js | 1 + dev/bin/schema-validate.js | 5 +- dev/build-libs.js | 11 ++- dev/dictionary-validate.js | 39 ++++++-- dev/generate-css-json.js | 61 +++++++++--- dev/jsconfig.json | 7 +- dev/lint/global-declarations.js | 157 ------------------------------- dev/lint/html-scripts.js | 202 ---------------------------------------- dev/manifest-util.js | 84 ++++++++++++++++- dev/schema-validate.js | 25 ++++- dev/util.js | 21 ++++- vitest.config.js | 1 + 14 files changed, 269 insertions(+), 410 deletions(-) delete mode 100644 dev/lint/global-declarations.js delete mode 100644 dev/lint/html-scripts.js (limited to 'dev/bin') diff --git a/.eslintrc.json b/.eslintrc.json index 640a67e2..58262714 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -765,7 +765,6 @@ }, { "files": [ - "dev/**/*.js", "test/**/*.js" ], "rules": { diff --git a/dev/bin/build.js b/dev/bin/build.js index 282f0414..c5814dd3 100644 --- a/dev/bin/build.js +++ b/dev/bin/build.js @@ -21,13 +21,22 @@ import childProcess from 'child_process'; import fs from 'fs'; import path from 'path'; import readline from 'readline'; -import {fileURLToPath} from 'url'; +import JSZip from 'jszip'; +import {fileURLToPath} from 'node:url'; import {buildLibs} from '../build-libs.js'; import {ManifestUtil} from '../manifest-util.js'; import {getAllFiles, getArgs, testMain} from '../util.js'; const dirname = path.dirname(fileURLToPath(import.meta.url)); +/** + * @param {string} directory + * @param {string[]} excludeFiles + * @param {string} outputFileName + * @param {string[]} sevenZipExes + * @param {?import('jszip').OnUpdateCallback} onUpdate + * @param {boolean} dryRun + */ async function createZip(directory, excludeFiles, outputFileName, sevenZipExes, onUpdate, dryRun) { try { fs.unlinkSync(outputFileName); @@ -57,11 +66,17 @@ async function createZip(directory, excludeFiles, outputFileName, sevenZipExes, } } } - return await createJSZip(directory, excludeFiles, outputFileName, onUpdate, dryRun); + await createJSZip(directory, excludeFiles, outputFileName, onUpdate, dryRun); } +/** + * @param {string} directory + * @param {string[]} excludeFiles + * @param {string} outputFileName + * @param {?import('jszip').OnUpdateCallback} onUpdate + * @param {boolean} dryRun + */ async function createJSZip(directory, excludeFiles, outputFileName, onUpdate, dryRun) { - const JSZip = null; const files = getAllFiles(directory); removeItemsFromArray(files, excludeFiles); const zip = new JSZip(); @@ -89,6 +104,10 @@ async function createJSZip(directory, excludeFiles, outputFileName, onUpdate, dr } } +/** + * @param {string[]} array + * @param {string[]} removeItems + */ function removeItemsFromArray(array, removeItems) { for (const item of removeItems) { const index = getIndexOfFilePath(array, item); @@ -98,6 +117,11 @@ function removeItemsFromArray(array, removeItems) { } } +/** + * @param {string[]} array + * @param {string} item + * @returns {number} + */ function getIndexOfFilePath(array, item) { const pattern = /\\/g; const separator = '/'; @@ -110,6 +134,16 @@ function getIndexOfFilePath(array, item) { return -1; } +/** + * @param {string} buildDir + * @param {string} extDir + * @param {ManifestUtil} manifestUtil + * @param {string[]} variantNames + * @param {string} manifestPath + * @param {boolean} dryRun + * @param {boolean} dryRunBuildZip + * @param {string} yomitanVersion + */ async function build(buildDir, extDir, manifestUtil, variantNames, manifestPath, dryRun, dryRunBuildZip, yomitanVersion) { const sevenZipExes = ['7za', '7z']; @@ -119,6 +153,7 @@ async function build(buildDir, extDir, manifestUtil, variantNames, manifestPath, } const dontLogOnUpdate = !process.stdout.isTTY; + /** @type {import('jszip').OnUpdateCallback} */ const onUpdate = (metadata) => { if (dontLogOnUpdate) { return; } @@ -127,7 +162,7 @@ async function build(buildDir, extDir, manifestUtil, variantNames, manifestPath, message += ` (${metadata.currentFile})`; } - readline.clearLine(process.stdout); + readline.clearLine(process.stdout, 0); readline.cursorTo(process.stdout, 0); process.stdout.write(message); }; @@ -173,6 +208,10 @@ async function build(buildDir, extDir, manifestUtil, variantNames, manifestPath, } } +/** + * @param {string} directory + * @param {string[]} files + */ function ensureFilesExist(directory, files) { for (const file of files) { assert.ok(fs.existsSync(path.join(directory, file))); @@ -180,8 +219,11 @@ function ensureFilesExist(directory, files) { } +/** + * @param {string[]} argv + */ export async function main(argv) { - const args = getArgs(argv, new Map([ + const args = getArgs(argv, new Map(/** @type {[key: string, value: (boolean|null|number|string|string[])][]} */ ([ ['all', false], ['default', false], ['manifest', null], @@ -189,11 +231,11 @@ export async function main(argv) { ['dry-run-build-zip', false], ['yomitan-version', '0.0.0.0'], [null, []] - ])); + ]))); - const dryRun = args.get('dry-run'); - const dryRunBuildZip = args.get('dry-run-build-zip'); - const yomitanVersion = args.get('yomitan-version'); + const dryRun = /** @type {boolean} */ (args.get('dry-run')); + const dryRunBuildZip = /** @type {boolean} */ (args.get('dry-run-build-zip')); + const yomitanVersion = /** @type {string} */ (args.get('yomitan-version')); const manifestUtil = new ManifestUtil(); @@ -204,11 +246,11 @@ export async function main(argv) { try { await buildLibs(); - const variantNames = ( + const variantNames = /** @type {string[]} */ (( argv.length === 0 || args.get('all') ? manifestUtil.getVariants().filter(({buildable}) => buildable !== false).map(({name}) => name) : args.get(null) - ); + )); await build(buildDir, extDir, manifestUtil, variantNames, manifestPath, dryRun, dryRunBuildZip, yomitanVersion); } finally { // Restore manifest diff --git a/dev/bin/dictionary-validate.js b/dev/bin/dictionary-validate.js index 78ad5198..0affb919 100644 --- a/dev/bin/dictionary-validate.js +++ b/dev/bin/dictionary-validate.js @@ -28,6 +28,7 @@ async function main() { return; } + /** @type {import('dev/schema-validate').ValidateMode} */ let mode = null; if (dictionaryFileNames[0] === '--ajv') { mode = 'ajv'; diff --git a/dev/bin/schema-validate.js b/dev/bin/schema-validate.js index 86cfebae..319c0d2c 100644 --- a/dev/bin/schema-validate.js +++ b/dev/bin/schema-validate.js @@ -17,8 +17,8 @@ */ import fs from 'fs'; -import performance from 'perf_hooks'; -import {createJsonSchema} from '../util.js'; +import {performance} from 'perf_hooks'; +import {createJsonSchema} from '../schema-validate.js'; function main() { const args = process.argv.slice(2); @@ -30,6 +30,7 @@ function main() { return; } + /** @type {import('dev/schema-validate').ValidateMode} */ let mode = null; if (args[0] === '--ajv') { mode = 'ajv'; diff --git a/dev/build-libs.js b/dev/build-libs.js index d33c1420..eee007f6 100644 --- a/dev/build-libs.js +++ b/dev/build-libs.js @@ -26,15 +26,18 @@ import {fileURLToPath} from 'url'; const dirname = path.dirname(fileURLToPath(import.meta.url)); const extDir = path.join(dirname, '..', 'ext'); -async function buildLib(p) { +/** + * @param {string} scriptPath + */ +async function buildLib(scriptPath) { await esbuild.build({ - entryPoints: [p], + entryPoints: [scriptPath], bundle: true, minify: false, sourcemap: true, target: 'es2020', format: 'esm', - outfile: path.join(extDir, 'lib', path.basename(p)), + outfile: path.join(extDir, 'lib', path.basename(scriptPath)), external: ['fs'], banner: { js: '// @ts-nocheck' @@ -55,7 +58,7 @@ export async function buildLibs() { const schemaDir = path.join(extDir, 'data/schemas/'); const schemaFileNames = fs.readdirSync(schemaDir); - const schemas = schemaFileNames.map((schemaFileName) => JSON.parse(fs.readFileSync(path.join(schemaDir, schemaFileName)))); + const schemas = schemaFileNames.map((schemaFileName) => JSON.parse(fs.readFileSync(path.join(schemaDir, schemaFileName), {encoding: 'utf8'}))); const ajv = new Ajv({schemas: schemas, code: {source: true, esm: true}}); const moduleCode = standaloneCode(ajv); diff --git a/dev/dictionary-validate.js b/dev/dictionary-validate.js index eb40beda..b3654e75 100644 --- a/dev/dictionary-validate.js +++ b/dev/dictionary-validate.js @@ -22,23 +22,34 @@ import path from 'path'; import {performance} from 'perf_hooks'; import {createJsonSchema} from './schema-validate.js'; +/** + * @param {string} relativeFileName + * @returns {import('dev/dictionary-validate').Schema} + */ function readSchema(relativeFileName) { const fileName = path.join(__dirname, relativeFileName); const source = fs.readFileSync(fileName, {encoding: 'utf8'}); return JSON.parse(source); } +/** + * @param {import('dev/schema-validate').ValidateMode} mode + * @param {import('jszip')} zip + * @param {string} fileNameFormat + * @param {import('dev/dictionary-validate').Schema} schema + */ async function validateDictionaryBanks(mode, zip, fileNameFormat, schema) { let jsonSchema; try { jsonSchema = createJsonSchema(mode, schema); } catch (e) { - e.message += `\n(in file ${fileNameFormat})}`; - throw e; + const e2 = e instanceof Error ? e : new Error(`${e}`); + e2.message += `\n(in file ${fileNameFormat})}`; + throw e2; } let index = 1; while (true) { - const fileName = fileNameFormat.replace(/\?/, index); + const fileName = fileNameFormat.replace(/\?/, `${index}`); const file = zip.files[fileName]; if (!file) { break; } @@ -47,14 +58,20 @@ async function validateDictionaryBanks(mode, zip, fileNameFormat, schema) { try { jsonSchema.validate(data); } catch (e) { - e.message += `\n(in file ${fileName})}`; - throw e; + const e2 = e instanceof Error ? e : new Error(`${e}`); + e2.message += `\n(in file ${fileName})}`; + throw e2; } ++index; } } +/** + * @param {import('dev/schema-validate').ValidateMode} mode + * @param {import('jszip')} archive + * @param {import('dev/dictionary-validate').Schemas} schemas + */ export async function validateDictionary(mode, archive, schemas) { const fileName = 'index.json'; const indexFile = archive.files[fileName]; @@ -69,8 +86,9 @@ export async function validateDictionary(mode, archive, schemas) { const jsonSchema = createJsonSchema(mode, schemas.index); jsonSchema.validate(index); } catch (e) { - e.message += `\n(in file ${fileName})}`; - throw e; + const e2 = e instanceof Error ? e : new Error(`${e}`); + e2.message += `\n(in file ${fileName})}`; + throw e2; } await validateDictionaryBanks(mode, archive, 'term_bank_?.json', version === 1 ? schemas.termBankV1 : schemas.termBankV3); @@ -80,6 +98,9 @@ export async function validateDictionary(mode, archive, schemas) { await validateDictionaryBanks(mode, archive, 'tag_bank_?.json', schemas.tagBankV3); } +/** + * @returns {import('dev/dictionary-validate').Schemas} + */ export function getSchemas() { return { index: readSchema('../ext/data/schemas/dictionary-index-schema.json'), @@ -93,6 +114,10 @@ export function getSchemas() { }; } +/** + * @param {import('dev/schema-validate').ValidateMode} mode + * @param {string[]} dictionaryFileNames + */ export async function testDictionaryFiles(mode, dictionaryFileNames) { const schemas = getSchemas(); diff --git a/dev/generate-css-json.js b/dev/generate-css-json.js index 914c1452..02e54530 100644 --- a/dev/generate-css-json.js +++ b/dev/generate-css-json.js @@ -16,9 +16,13 @@ * along with this program. If not, see . */ +import css from 'css'; import fs from 'fs'; import path from 'path'; +/** + * @returns {{cssFile: string, overridesCssFile: string, outputPath: string}[]} + */ export function getTargets() { return [ { @@ -34,8 +38,11 @@ export function getTargets() { ]; } -import css from 'css'; - +/** + * @param {import('css-style-applier').RawStyleData} rules + * @param {string[]} selectors + * @returns {number} + */ function indexOfRule(rules, selectors) { const jj = selectors.length; for (let i = 0, ii = rules.length; i < ii; ++i) { @@ -53,6 +60,12 @@ function indexOfRule(rules, selectors) { return -1; } +/** + * @param {import('css-style-applier').RawStyleDataStyleArray} styles + * @param {string} property + * @param {Map} removedProperties + * @returns {number} + */ function removeProperty(styles, property, removedProperties) { let removeCount = removedProperties.get(property); if (typeof removeCount !== 'undefined') { return removeCount; } @@ -69,6 +82,10 @@ function removeProperty(styles, property, removedProperties) { return removeCount; } +/** + * @param {import('css-style-applier').RawStyleData} rules + * @returns {string} + */ export function formatRulesJson(rules) { // Manually format JSON, for improved compactness // return JSON.stringify(rules, null, 4); @@ -102,27 +119,39 @@ export function formatRulesJson(rules) { return result; } +/** + * @param {string} cssFile + * @param {string} overridesCssFile + * @returns {import('css-style-applier').RawStyleData} + * @throws {Error} + */ export function generateRules(cssFile, overridesCssFile) { const content1 = fs.readFileSync(cssFile, {encoding: 'utf8'}); const content2 = fs.readFileSync(overridesCssFile, {encoding: 'utf8'}); - const stylesheet1 = css.parse(content1, {}).stylesheet; - const stylesheet2 = css.parse(content2, {}).stylesheet; + const stylesheet1 = /** @type {css.StyleRules} */ (css.parse(content1, {}).stylesheet); + const stylesheet2 = /** @type {css.StyleRules} */ (css.parse(content2, {}).stylesheet); const removePropertyPattern = /^remove-property\s+([\w\W]+)$/; const removeRulePattern = /^remove-rule$/; const propertySeparator = /\s+/; + /** @type {import('css-style-applier').RawStyleData} */ const rules = []; // Default stylesheet for (const rule of stylesheet1.rules) { if (rule.type !== 'rule') { continue; } - const {selectors, declarations} = rule; + const {selectors, declarations} = /** @type {css.Rule} */ (rule); + if (typeof selectors === 'undefined') { continue; } + /** @type {import('css-style-applier').RawStyleDataStyleArray} */ const styles = []; - for (const declaration of declarations) { - if (declaration.type !== 'declaration') { console.log(declaration); continue; } - const {property, value} = declaration; - styles.push([property, value]); + if (typeof declarations !== 'undefined') { + for (const declaration of declarations) { + if (declaration.type !== 'declaration') { console.log(declaration); continue; } + const {property, value} = /** @type {css.Declaration} */ (declaration); + if (typeof property !== 'string' || typeof value !== 'string') { continue; } + styles.push([property, value]); + } } if (styles.length > 0) { rules.push({selectors, styles}); @@ -132,7 +161,9 @@ export function generateRules(cssFile, overridesCssFile) { // Overrides for (const rule of stylesheet2.rules) { if (rule.type !== 'rule') { continue; } - const {selectors, declarations} = rule; + const {selectors, declarations} = /** @type {css.Rule} */ (rule); + if (typeof selectors === 'undefined' || typeof declarations === 'undefined') { continue; } + /** @type {Map} */ const removedProperties = new Map(); for (const declaration of declarations) { switch (declaration.type) { @@ -146,16 +177,18 @@ export function generateRules(cssFile, overridesCssFile) { entry = {selectors, styles: []}; rules.push(entry); } - const {property, value} = declaration; - removeProperty(entry.styles, property, removedProperties); - entry.styles.push([property, value]); + const {property, value} = /** @type {css.Declaration} */ (declaration); + if (typeof property === 'string' && typeof value === 'string') { + removeProperty(entry.styles, property, removedProperties); + entry.styles.push([property, value]); + } } break; case 'comment': { const index = indexOfRule(rules, selectors); if (index < 0) { throw new Error('Could not find rule with matching selectors'); } - const comment = declaration.comment.trim(); + const comment = (/** @type {css.Comment} */ (declaration).comment || '').trim(); let m; if ((m = removePropertyPattern.exec(comment)) !== null) { for (const property of m[1].split(propertySeparator)) { diff --git a/dev/jsconfig.json b/dev/jsconfig.json index 5b1c450c..e0074980 100644 --- a/dev/jsconfig.json +++ b/dev/jsconfig.json @@ -1,9 +1,9 @@ { "compilerOptions": { - "module": "ES2015", + "module": "ES2022", "target": "ES2022", "checkJs": true, - "moduleResolution": "node", + "moduleResolution": "bundler", "strict": true, "strictNullChecks": true, "noImplicitAny": true, @@ -73,6 +73,7 @@ "../types/other/globals.d.ts" ], "exclude": [ - "../node_modules" + "../node_modules", + "lib" ] } \ No newline at end of file diff --git a/dev/lint/global-declarations.js b/dev/lint/global-declarations.js deleted file mode 100644 index 648ad368..00000000 --- a/dev/lint/global-declarations.js +++ /dev/null @@ -1,157 +0,0 @@ -/* - * Copyright (C) 2023 Yomitan Authors - * Copyright (C) 2020-2022 Yomichan Authors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -const fs = require('fs'); -const path = require('path'); -const assert = require('assert'); -const {getAllFiles} = require('../util'); - - -/** - * @param {string} string - * @returns {string} - */ -function escapeRegExp(string) { - return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&'); -} - -/** - * @param {string} string - * @param {RegExp} pattern - * @returns {number} - */ -function countOccurences(string, pattern) { - return (string.match(pattern) || []).length; -} - -/** - * @param {string} string - * @returns {'\r'|'\n'|'\r\n'} - */ -function getNewline(string) { - const count1 = countOccurences(string, /(?:^|[^\r])\n/g); - const count2 = countOccurences(string, /\r\n/g); - const count3 = countOccurences(string, /\r(?:[^\n]|$)/g); - if (count2 > count1) { - return (count3 > count2) ? '\r' : '\r\n'; - } else { - return (count3 > count1) ? '\r' : '\n'; - } -} - -/** - * @param {string} string - * @param {string} substring - * @returns {number} - */ -function getSubstringCount(string, substring) { - let count = 0; - const pattern = new RegExp(`\\b${escapeRegExp(substring)}\\b`, 'g'); - while (true) { - const match = pattern.exec(string); - if (match === null) { break; } - ++count; - } - return count; -} - - -/** - * @param {string} fileName - * @param {boolean} fix - * @returns {boolean} - */ -function validateGlobals(fileName, fix) { - const pattern = /\/\*\s*global\s+([\w\W]*?)\*\//g; - const trimPattern = /^[\s,*]+|[\s,*]+$/g; - const splitPattern = /[\s,*]+/; - const source = fs.readFileSync(fileName, {encoding: 'utf8'}); - let match; - let first = true; - let endIndex = 0; - let newSource = ''; - const allGlobals = []; - const newline = getNewline(source); - while ((match = pattern.exec(source)) !== null) { - if (!first) { - console.error(`Encountered more than one global declaration in ${fileName}`); - return false; - } - first = false; - - const parts = match[1].replace(trimPattern, '').split(splitPattern); - parts.sort(); - - const actual = match[0]; - const expected = `/* global${parts.map((v) => `${newline} * ${v}`).join('')}${newline} */`; - - try { - assert.strictEqual(actual, expected); - } catch (e) { - console.error(`Global declaration error encountered in ${fileName}:`); - console.error(e instanceof Error ? e.message : `${e}`); - if (!fix) { - return false; - } - } - - newSource += source.substring(0, match.index); - newSource += expected; - endIndex = match.index + match[0].length; - - allGlobals.push(...parts); - } - - newSource += source.substring(endIndex); - - // This is an approximate check to see if a global variable is unused. - // If the global appears in a comment, string, or similar, the check will pass. - let errorCount = 0; - for (const global of allGlobals) { - if (getSubstringCount(newSource, global) <= 1) { - console.error(`Global variable ${global} appears to be unused in ${fileName}`); - ++errorCount; - } - } - - if (fix) { - fs.writeFileSync(fileName, newSource, {encoding: 'utf8'}); - } - - return errorCount === 0; -} - - -/** */ -function main() { - const fix = (process.argv.length >= 2 && process.argv[2] === '--fix'); - const directory = path.resolve(__dirname, '..', '..', 'ext'); - const pattern = /\.js$/; - const ignorePattern = /^lib[\\/]/; - const fileNames = getAllFiles(directory, (f) => pattern.test(f) && !ignorePattern.test(f)); - for (const fileName of fileNames) { - if (!validateGlobals(path.join(directory, fileName), fix)) { - process.exit(-1); - return; - } - } - process.exit(0); -} - - -if (require.main === module) { main(); } diff --git a/dev/lint/html-scripts.js b/dev/lint/html-scripts.js deleted file mode 100644 index da8c2c71..00000000 --- a/dev/lint/html-scripts.js +++ /dev/null @@ -1,202 +0,0 @@ -/* - * Copyright (C) 2023 Yomitan Authors - * Copyright (C) 2020-2022 Yomichan Authors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -const fs = require('fs'); -const path = require('path'); -const assert = require('assert'); -const {JSDOM} = require('jsdom'); -const {getAllFiles} = require('../util'); - - -/** - * @param {string} fileName - * @returns {?fs.Stats} - */ -function lstatSyncSafe(fileName) { - try { - return fs.lstatSync(fileName); - } catch (e) { - return null; - } -} - -/** - * @param {string} src - * @param {string} fileName - * @param {string} extDir - */ -function validatePath(src, fileName, extDir) { - assert.ok(typeof src === 'string', `