From 6bf7b0055765c4f2011c9614753d6714dc09be65 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Wed, 14 Feb 2024 22:26:29 -0500 Subject: Eslint rule updates (#673) * Install unicorn * Add rules * Fix issues * Install sonarjs * Set up rules * Fix issues * Install eslint-plugin-import and fix import extensions * Simplify permitted error names --- dev/bin/build.js | 10 ++++------ dev/build-libs.js | 1 + dev/data-error.js | 8 +++----- dev/generate-css-json.js | 10 +++++----- dev/lib/dexie.js | 5 ++--- dev/schema-validate.js | 3 ++- dev/util.js | 6 ++---- 7 files changed, 19 insertions(+), 24 deletions(-) (limited to 'dev') diff --git a/dev/bin/build.js b/dev/bin/build.js index bc0a8cb8..e22159d6 100644 --- a/dev/bin/build.js +++ b/dev/bin/build.js @@ -195,12 +195,10 @@ async function build(buildDir, extDir, manifestUtil, variantNames, manifestPath, await createZip(extDir, excludeFiles, fullFileName, sevenZipExes, onUpdate, dryRun); } - if (!dryRun) { - if (Array.isArray(fileCopies)) { - for (const fileName2 of fileCopies) { - const fileName2Safe = path.basename(fileName2); - fs.copyFileSync(fullFileName, path.join(buildDir, fileName2Safe)); - } + if (!dryRun && Array.isArray(fileCopies)) { + for (const fileName2 of fileCopies) { + const fileName2Safe = path.basename(fileName2); + fs.copyFileSync(fullFileName, path.join(buildDir, fileName2Safe)); } } } diff --git a/dev/build-libs.js b/dev/build-libs.js index 15ab3c8d..f1570fad 100644 --- a/dev/build-libs.js +++ b/dev/build-libs.js @@ -64,6 +64,7 @@ export async function buildLibs() { const schemaFileNames = fs.readdirSync(schemaDir); const schemas = schemaFileNames.map((schemaFileName) => { /** @type {import('ajv').AnySchema} */ + // eslint-disable-next-line sonarjs/prefer-immediate-return const result = parseJson(fs.readFileSync(path.join(schemaDir, schemaFileName), {encoding: 'utf8'})); return result; }); diff --git a/dev/data-error.js b/dev/data-error.js index 5659245b..eb7f71bc 100644 --- a/dev/data-error.js +++ b/dev/data-error.js @@ -18,12 +18,14 @@ /** * Schema validation error type. */ -class DataError extends Error { +export class DataError extends Error { /** * @param {string} message */ constructor(message) { super(message); + /** @type {string} */ + this.name = 'DataError'; /** @type {unknown} */ this._data = void 0; } @@ -32,7 +34,3 @@ class DataError extends Error { get data() { return this._data; } set data(value) { this._data = value; } } - -module.exports = { - DataError -}; diff --git a/dev/generate-css-json.js b/dev/generate-css-json.js index 26bdfa64..a837c9e2 100644 --- a/dev/generate-css-json.js +++ b/dev/generate-css-json.js @@ -101,11 +101,11 @@ export function formatRulesJson(rules) { for (const {selectors, styles} of rules) { if (ruleIndex > 0) { result += ','; } result += `\n${indent1}{\n${indent2}"selectors": `; - if (selectors.length === 1) { - result += `[${JSON.stringify(selectors[0], null, 4)}]`; - } else { - result += JSON.stringify(selectors, null, 4).replace(/\n/g, '\n' + indent2); - } + result += ( + selectors.length === 1 ? + `[${JSON.stringify(selectors[0], null, 4)}]` : + JSON.stringify(selectors, null, 4).replace(/\n/g, '\n' + indent2) + ); result += `,\n${indent2}"styles": [`; let styleIndex = 0; for (const [key, value] of styles) { diff --git a/dev/lib/dexie.js b/dev/lib/dexie.js index ffbd2923..83015e25 100644 --- a/dev/lib/dexie.js +++ b/dev/lib/dexie.js @@ -15,7 +15,6 @@ * along with this program. If not, see . */ -import Dexie from 'dexie'; -import 'dexie-export-import'; +export {default as Dexie} from 'dexie'; -export {Dexie}; +import 'dexie-export-import'; diff --git a/dev/schema-validate.js b/dev/schema-validate.js index d1ffcf82..3ad247d9 100644 --- a/dev/schema-validate.js +++ b/dev/schema-validate.js @@ -18,6 +18,7 @@ import Ajv from 'ajv'; import {readFileSync} from 'fs'; +import {fileURLToPath} from 'url'; import {JsonSchema} from '../ext/js/data/json-schema.js'; import {DataError} from './data-error.js'; import {parseJson} from './json.js'; @@ -32,7 +33,7 @@ class JsonSchemaAjv { strictTuples: false, allowUnionTypes: true }); - const metaSchemaPath = require.resolve('ajv/dist/refs/json-schema-draft-07.json'); + const metaSchemaPath = fileURLToPath(import.meta.resolve('ajv/dist/refs/json-schema-draft-07.json')); /** @type {import('ajv').AnySchemaObject} */ const metaSchema = parseJson(readFileSync(metaSchemaPath, {encoding: 'utf8'})); ajv.addMetaSchema(metaSchema); diff --git a/dev/util.js b/dev/util.js index 89bd95da..eee16964 100644 --- a/dev/util.js +++ b/dev/util.js @@ -40,10 +40,8 @@ export function getAllFiles(baseDirectory, predicate = null) { if (typeof predicate !== 'function' || predicate(relativeFileName, false)) { results.push(relativeFileName); } - } else if (stats.isDirectory()) { - if (typeof predicate !== 'function' || predicate(relativeFileName, true)) { - directories.push(fullFileName); - } + } else if (stats.isDirectory() && (typeof predicate !== 'function' || predicate(relativeFileName, true))) { + directories.push(fullFileName); } } } -- cgit v1.2.3